]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merged from miles@gnu.org--gnu-2005 (patch 470-473)
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern Lisp_Object do_mouse_tracking;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
268
269 int make_cursor_line_fully_visible_p;
270
271 /* Margin around tool bar buttons in pixels. */
272
273 Lisp_Object Vtool_bar_button_margin;
274
275 /* Thickness of shadow to draw around tool bar buttons. */
276
277 EMACS_INT tool_bar_button_relief;
278
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
281
282 int auto_resize_tool_bars_p;
283
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
287
288 int x_stretch_cursor_p;
289
290 /* Non-nil means don't actually do any redisplay. */
291
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
293
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
295
296 int inhibit_eval_during_redisplay;
297
298 /* Names of text properties relevant for redisplay. */
299
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
302
303 /* Symbols used in text property values. */
304
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
316
317 /* Non-nil means highlight trailing whitespace. */
318
319 Lisp_Object Vshow_trailing_whitespace;
320
321 /* Non-nil means escape non-break space and hyphens. */
322
323 Lisp_Object Vnobreak_char_display;
324
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern Lisp_Object Voverflow_newline_into_fringe;
327
328 /* Test if overflow newline into fringe. Called with iterator IT
329 at or past right window margin, and with IT->current_x set. */
330
331 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
332 (!NILP (Voverflow_newline_into_fringe) \
333 && FRAME_WINDOW_P (it->f) \
334 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
335 && it->current_x == it->last_visible_x)
336
337 #endif /* HAVE_WINDOW_SYSTEM */
338
339 /* Non-nil means show the text cursor in void text areas
340 i.e. in blank areas after eol and eob. This used to be
341 the default in 21.3. */
342
343 Lisp_Object Vvoid_text_area_pointer;
344
345 /* Name of the face used to highlight trailing whitespace. */
346
347 Lisp_Object Qtrailing_whitespace;
348
349 /* Name and number of the face used to highlight escape glyphs. */
350
351 Lisp_Object Qescape_glyph;
352
353 /* Name and number of the face used to highlight non-breaking spaces. */
354
355 Lisp_Object Qnobreak_space;
356
357 /* The symbol `image' which is the car of the lists used to represent
358 images in Lisp. */
359
360 Lisp_Object Qimage;
361
362 /* The image map types. */
363 Lisp_Object QCmap, QCpointer;
364 Lisp_Object Qrect, Qcircle, Qpoly;
365
366 /* Non-zero means print newline to stdout before next mini-buffer
367 message. */
368
369 int noninteractive_need_newline;
370
371 /* Non-zero means print newline to message log before next message. */
372
373 static int message_log_need_newline;
374
375 /* Three markers that message_dolog uses.
376 It could allocate them itself, but that causes trouble
377 in handling memory-full errors. */
378 static Lisp_Object message_dolog_marker1;
379 static Lisp_Object message_dolog_marker2;
380 static Lisp_Object message_dolog_marker3;
381 \f
382 /* The buffer position of the first character appearing entirely or
383 partially on the line of the selected window which contains the
384 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
385 redisplay optimization in redisplay_internal. */
386
387 static struct text_pos this_line_start_pos;
388
389 /* Number of characters past the end of the line above, including the
390 terminating newline. */
391
392 static struct text_pos this_line_end_pos;
393
394 /* The vertical positions and the height of this line. */
395
396 static int this_line_vpos;
397 static int this_line_y;
398 static int this_line_pixel_height;
399
400 /* X position at which this display line starts. Usually zero;
401 negative if first character is partially visible. */
402
403 static int this_line_start_x;
404
405 /* Buffer that this_line_.* variables are referring to. */
406
407 static struct buffer *this_line_buffer;
408
409 /* Nonzero means truncate lines in all windows less wide than the
410 frame. */
411
412 int truncate_partial_width_windows;
413
414 /* A flag to control how to display unibyte 8-bit character. */
415
416 int unibyte_display_via_language_environment;
417
418 /* Nonzero means we have more than one non-mini-buffer-only frame.
419 Not guaranteed to be accurate except while parsing
420 frame-title-format. */
421
422 int multiple_frames;
423
424 Lisp_Object Vglobal_mode_string;
425
426
427 /* List of variables (symbols) which hold markers for overlay arrows.
428 The symbols on this list are examined during redisplay to determine
429 where to display overlay arrows. */
430
431 Lisp_Object Voverlay_arrow_variable_list;
432
433 /* Marker for where to display an arrow on top of the buffer text. */
434
435 Lisp_Object Voverlay_arrow_position;
436
437 /* String to display for the arrow. Only used on terminal frames. */
438
439 Lisp_Object Voverlay_arrow_string;
440
441 /* Values of those variables at last redisplay are stored as
442 properties on `overlay-arrow-position' symbol. However, if
443 Voverlay_arrow_position is a marker, last-arrow-position is its
444 numerical position. */
445
446 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
447
448 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
449 properties on a symbol in overlay-arrow-variable-list. */
450
451 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
452
453 /* Like mode-line-format, but for the title bar on a visible frame. */
454
455 Lisp_Object Vframe_title_format;
456
457 /* Like mode-line-format, but for the title bar on an iconified frame. */
458
459 Lisp_Object Vicon_title_format;
460
461 /* List of functions to call when a window's size changes. These
462 functions get one arg, a frame on which one or more windows' sizes
463 have changed. */
464
465 static Lisp_Object Vwindow_size_change_functions;
466
467 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
468
469 /* Nonzero if an overlay arrow has been displayed in this window. */
470
471 static int overlay_arrow_seen;
472
473 /* Nonzero means highlight the region even in nonselected windows. */
474
475 int highlight_nonselected_windows;
476
477 /* If cursor motion alone moves point off frame, try scrolling this
478 many lines up or down if that will bring it back. */
479
480 static EMACS_INT scroll_step;
481
482 /* Nonzero means scroll just far enough to bring point back on the
483 screen, when appropriate. */
484
485 static EMACS_INT scroll_conservatively;
486
487 /* Recenter the window whenever point gets within this many lines of
488 the top or bottom of the window. This value is translated into a
489 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
490 that there is really a fixed pixel height scroll margin. */
491
492 EMACS_INT scroll_margin;
493
494 /* Number of windows showing the buffer of the selected window (or
495 another buffer with the same base buffer). keyboard.c refers to
496 this. */
497
498 int buffer_shared;
499
500 /* Vector containing glyphs for an ellipsis `...'. */
501
502 static Lisp_Object default_invis_vector[3];
503
504 /* Zero means display the mode-line/header-line/menu-bar in the default face
505 (this slightly odd definition is for compatibility with previous versions
506 of emacs), non-zero means display them using their respective faces.
507
508 This variable is deprecated. */
509
510 int mode_line_inverse_video;
511
512 /* Prompt to display in front of the mini-buffer contents. */
513
514 Lisp_Object minibuf_prompt;
515
516 /* Width of current mini-buffer prompt. Only set after display_line
517 of the line that contains the prompt. */
518
519 int minibuf_prompt_width;
520
521 /* This is the window where the echo area message was displayed. It
522 is always a mini-buffer window, but it may not be the same window
523 currently active as a mini-buffer. */
524
525 Lisp_Object echo_area_window;
526
527 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
528 pushes the current message and the value of
529 message_enable_multibyte on the stack, the function restore_message
530 pops the stack and displays MESSAGE again. */
531
532 Lisp_Object Vmessage_stack;
533
534 /* Nonzero means multibyte characters were enabled when the echo area
535 message was specified. */
536
537 int message_enable_multibyte;
538
539 /* Nonzero if we should redraw the mode lines on the next redisplay. */
540
541 int update_mode_lines;
542
543 /* Nonzero if window sizes or contents have changed since last
544 redisplay that finished. */
545
546 int windows_or_buffers_changed;
547
548 /* Nonzero means a frame's cursor type has been changed. */
549
550 int cursor_type_changed;
551
552 /* Nonzero after display_mode_line if %l was used and it displayed a
553 line number. */
554
555 int line_number_displayed;
556
557 /* Maximum buffer size for which to display line numbers. */
558
559 Lisp_Object Vline_number_display_limit;
560
561 /* Line width to consider when repositioning for line number display. */
562
563 static EMACS_INT line_number_display_limit_width;
564
565 /* Number of lines to keep in the message log buffer. t means
566 infinite. nil means don't log at all. */
567
568 Lisp_Object Vmessage_log_max;
569
570 /* The name of the *Messages* buffer, a string. */
571
572 static Lisp_Object Vmessages_buffer_name;
573
574 /* Current, index 0, and last displayed echo area message. Either
575 buffers from echo_buffers, or nil to indicate no message. */
576
577 Lisp_Object echo_area_buffer[2];
578
579 /* The buffers referenced from echo_area_buffer. */
580
581 static Lisp_Object echo_buffer[2];
582
583 /* A vector saved used in with_area_buffer to reduce consing. */
584
585 static Lisp_Object Vwith_echo_area_save_vector;
586
587 /* Non-zero means display_echo_area should display the last echo area
588 message again. Set by redisplay_preserve_echo_area. */
589
590 static int display_last_displayed_message_p;
591
592 /* Nonzero if echo area is being used by print; zero if being used by
593 message. */
594
595 int message_buf_print;
596
597 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
598
599 Lisp_Object Qinhibit_menubar_update;
600 int inhibit_menubar_update;
601
602 /* Maximum height for resizing mini-windows. Either a float
603 specifying a fraction of the available height, or an integer
604 specifying a number of lines. */
605
606 Lisp_Object Vmax_mini_window_height;
607
608 /* Non-zero means messages should be displayed with truncated
609 lines instead of being continued. */
610
611 int message_truncate_lines;
612 Lisp_Object Qmessage_truncate_lines;
613
614 /* Set to 1 in clear_message to make redisplay_internal aware
615 of an emptied echo area. */
616
617 static int message_cleared_p;
618
619 /* How to blink the default frame cursor off. */
620 Lisp_Object Vblink_cursor_alist;
621
622 /* A scratch glyph row with contents used for generating truncation
623 glyphs. Also used in direct_output_for_insert. */
624
625 #define MAX_SCRATCH_GLYPHS 100
626 struct glyph_row scratch_glyph_row;
627 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
628
629 /* Ascent and height of the last line processed by move_it_to. */
630
631 static int last_max_ascent, last_height;
632
633 /* Non-zero if there's a help-echo in the echo area. */
634
635 int help_echo_showing_p;
636
637 /* If >= 0, computed, exact values of mode-line and header-line height
638 to use in the macros CURRENT_MODE_LINE_HEIGHT and
639 CURRENT_HEADER_LINE_HEIGHT. */
640
641 int current_mode_line_height, current_header_line_height;
642
643 /* The maximum distance to look ahead for text properties. Values
644 that are too small let us call compute_char_face and similar
645 functions too often which is expensive. Values that are too large
646 let us call compute_char_face and alike too often because we
647 might not be interested in text properties that far away. */
648
649 #define TEXT_PROP_DISTANCE_LIMIT 100
650
651 #if GLYPH_DEBUG
652
653 /* Variables to turn off display optimizations from Lisp. */
654
655 int inhibit_try_window_id, inhibit_try_window_reusing;
656 int inhibit_try_cursor_movement;
657
658 /* Non-zero means print traces of redisplay if compiled with
659 GLYPH_DEBUG != 0. */
660
661 int trace_redisplay_p;
662
663 #endif /* GLYPH_DEBUG */
664
665 #ifdef DEBUG_TRACE_MOVE
666 /* Non-zero means trace with TRACE_MOVE to stderr. */
667 int trace_move;
668
669 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
670 #else
671 #define TRACE_MOVE(x) (void) 0
672 #endif
673
674 /* Non-zero means automatically scroll windows horizontally to make
675 point visible. */
676
677 int automatic_hscrolling_p;
678
679 /* How close to the margin can point get before the window is scrolled
680 horizontally. */
681 EMACS_INT hscroll_margin;
682
683 /* How much to scroll horizontally when point is inside the above margin. */
684 Lisp_Object Vhscroll_step;
685
686 /* The variable `resize-mini-windows'. If nil, don't resize
687 mini-windows. If t, always resize them to fit the text they
688 display. If `grow-only', let mini-windows grow only until they
689 become empty. */
690
691 Lisp_Object Vresize_mini_windows;
692
693 /* Buffer being redisplayed -- for redisplay_window_error. */
694
695 struct buffer *displayed_buffer;
696
697 /* Value returned from text property handlers (see below). */
698
699 enum prop_handled
700 {
701 HANDLED_NORMALLY,
702 HANDLED_RECOMPUTE_PROPS,
703 HANDLED_OVERLAY_STRING_CONSUMED,
704 HANDLED_RETURN
705 };
706
707 /* A description of text properties that redisplay is interested
708 in. */
709
710 struct props
711 {
712 /* The name of the property. */
713 Lisp_Object *name;
714
715 /* A unique index for the property. */
716 enum prop_idx idx;
717
718 /* A handler function called to set up iterator IT from the property
719 at IT's current position. Value is used to steer handle_stop. */
720 enum prop_handled (*handler) P_ ((struct it *it));
721 };
722
723 static enum prop_handled handle_face_prop P_ ((struct it *));
724 static enum prop_handled handle_invisible_prop P_ ((struct it *));
725 static enum prop_handled handle_display_prop P_ ((struct it *));
726 static enum prop_handled handle_composition_prop P_ ((struct it *));
727 static enum prop_handled handle_overlay_change P_ ((struct it *));
728 static enum prop_handled handle_fontified_prop P_ ((struct it *));
729
730 /* Properties handled by iterators. */
731
732 static struct props it_props[] =
733 {
734 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
735 /* Handle `face' before `display' because some sub-properties of
736 `display' need to know the face. */
737 {&Qface, FACE_PROP_IDX, handle_face_prop},
738 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
739 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
740 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
741 {NULL, 0, NULL}
742 };
743
744 /* Value is the position described by X. If X is a marker, value is
745 the marker_position of X. Otherwise, value is X. */
746
747 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
748
749 /* Enumeration returned by some move_it_.* functions internally. */
750
751 enum move_it_result
752 {
753 /* Not used. Undefined value. */
754 MOVE_UNDEFINED,
755
756 /* Move ended at the requested buffer position or ZV. */
757 MOVE_POS_MATCH_OR_ZV,
758
759 /* Move ended at the requested X pixel position. */
760 MOVE_X_REACHED,
761
762 /* Move within a line ended at the end of a line that must be
763 continued. */
764 MOVE_LINE_CONTINUED,
765
766 /* Move within a line ended at the end of a line that would
767 be displayed truncated. */
768 MOVE_LINE_TRUNCATED,
769
770 /* Move within a line ended at a line end. */
771 MOVE_NEWLINE_OR_CR
772 };
773
774 /* This counter is used to clear the face cache every once in a while
775 in redisplay_internal. It is incremented for each redisplay.
776 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
777 cleared. */
778
779 #define CLEAR_FACE_CACHE_COUNT 500
780 static int clear_face_cache_count;
781
782 /* Similarly for the image cache. */
783
784 #ifdef HAVE_WINDOW_SYSTEM
785 #define CLEAR_IMAGE_CACHE_COUNT 101
786 static int clear_image_cache_count;
787 #endif
788
789 /* Non-zero while redisplay_internal is in progress. */
790
791 int redisplaying_p;
792
793 /* Non-zero means don't free realized faces. Bound while freeing
794 realized faces is dangerous because glyph matrices might still
795 reference them. */
796
797 int inhibit_free_realized_faces;
798 Lisp_Object Qinhibit_free_realized_faces;
799
800 /* If a string, XTread_socket generates an event to display that string.
801 (The display is done in read_char.) */
802
803 Lisp_Object help_echo_string;
804 Lisp_Object help_echo_window;
805 Lisp_Object help_echo_object;
806 int help_echo_pos;
807
808 /* Temporary variable for XTread_socket. */
809
810 Lisp_Object previous_help_echo_string;
811
812 /* Null glyph slice */
813
814 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
815
816 \f
817 /* Function prototypes. */
818
819 static void setup_for_ellipsis P_ ((struct it *, int));
820 static void mark_window_display_accurate_1 P_ ((struct window *, int));
821 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
822 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
823 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
824 static int redisplay_mode_lines P_ ((Lisp_Object, int));
825 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
826
827 #if 0
828 static int invisible_text_between_p P_ ((struct it *, int, int));
829 #endif
830
831 static void pint2str P_ ((char *, int, int));
832 static void pint2hrstr P_ ((char *, int, int));
833 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
834 struct text_pos));
835 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
836 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
837 static void store_mode_line_noprop_char P_ ((char));
838 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
839 static void x_consider_frame_title P_ ((Lisp_Object));
840 static void handle_stop P_ ((struct it *));
841 static int tool_bar_lines_needed P_ ((struct frame *));
842 static int single_display_spec_intangible_p P_ ((Lisp_Object));
843 static void ensure_echo_area_buffers P_ ((void));
844 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
845 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
846 static int with_echo_area_buffer P_ ((struct window *, int,
847 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
848 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
849 static void clear_garbaged_frames P_ ((void));
850 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
851 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
853 static int display_echo_area P_ ((struct window *));
854 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
855 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
856 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
857 static int string_char_and_length P_ ((const unsigned char *, int, int *));
858 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
859 struct text_pos));
860 static int compute_window_start_on_continuation_line P_ ((struct window *));
861 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
862 static void insert_left_trunc_glyphs P_ ((struct it *));
863 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
864 Lisp_Object));
865 static void extend_face_to_end_of_line P_ ((struct it *));
866 static int append_space_for_newline P_ ((struct it *, int));
867 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
868 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
869 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
870 static int trailing_whitespace_p P_ ((int));
871 static int message_log_check_duplicate P_ ((int, int, int, int));
872 static void push_it P_ ((struct it *));
873 static void pop_it P_ ((struct it *));
874 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
875 static void select_frame_for_redisplay P_ ((Lisp_Object));
876 static void redisplay_internal P_ ((int));
877 static int echo_area_display P_ ((int));
878 static void redisplay_windows P_ ((Lisp_Object));
879 static void redisplay_window P_ ((Lisp_Object, int));
880 static Lisp_Object redisplay_window_error ();
881 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
882 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
883 static void update_menu_bar P_ ((struct frame *, int));
884 static int try_window_reusing_current_matrix P_ ((struct window *));
885 static int try_window_id P_ ((struct window *));
886 static int display_line P_ ((struct it *));
887 static int display_mode_lines P_ ((struct window *));
888 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
889 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
890 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
891 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
892 static void display_menu_bar P_ ((struct window *));
893 static int display_count_lines P_ ((int, int, int, int, int *));
894 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
895 int, int, struct it *, int, int, int, int));
896 static void compute_line_metrics P_ ((struct it *));
897 static void run_redisplay_end_trigger_hook P_ ((struct it *));
898 static int get_overlay_strings P_ ((struct it *, int));
899 static void next_overlay_string P_ ((struct it *));
900 static void reseat P_ ((struct it *, struct text_pos, int));
901 static void reseat_1 P_ ((struct it *, struct text_pos, int));
902 static void back_to_previous_visible_line_start P_ ((struct it *));
903 void reseat_at_previous_visible_line_start P_ ((struct it *));
904 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
905 static int next_element_from_ellipsis P_ ((struct it *));
906 static int next_element_from_display_vector P_ ((struct it *));
907 static int next_element_from_string P_ ((struct it *));
908 static int next_element_from_c_string P_ ((struct it *));
909 static int next_element_from_buffer P_ ((struct it *));
910 static int next_element_from_composition P_ ((struct it *));
911 static int next_element_from_image P_ ((struct it *));
912 static int next_element_from_stretch P_ ((struct it *));
913 static void load_overlay_strings P_ ((struct it *, int));
914 static int init_from_display_pos P_ ((struct it *, struct window *,
915 struct display_pos *));
916 static void reseat_to_string P_ ((struct it *, unsigned char *,
917 Lisp_Object, int, int, int, int));
918 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
919 int, int, int));
920 void move_it_vertically_backward P_ ((struct it *, int));
921 static void init_to_row_start P_ ((struct it *, struct window *,
922 struct glyph_row *));
923 static int init_to_row_end P_ ((struct it *, struct window *,
924 struct glyph_row *));
925 static void back_to_previous_line_start P_ ((struct it *));
926 static int forward_to_next_line_start P_ ((struct it *, int *));
927 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
928 Lisp_Object, int));
929 static struct text_pos string_pos P_ ((int, Lisp_Object));
930 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
931 static int number_of_chars P_ ((unsigned char *, int));
932 static void compute_stop_pos P_ ((struct it *));
933 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
934 Lisp_Object));
935 static int face_before_or_after_it_pos P_ ((struct it *, int));
936 static int next_overlay_change P_ ((int));
937 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
938 Lisp_Object, struct text_pos *,
939 int));
940 static int underlying_face_id P_ ((struct it *));
941 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
942 struct window *));
943
944 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
945 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
946
947 #ifdef HAVE_WINDOW_SYSTEM
948
949 static void update_tool_bar P_ ((struct frame *, int));
950 static void build_desired_tool_bar_string P_ ((struct frame *f));
951 static int redisplay_tool_bar P_ ((struct frame *));
952 static void display_tool_bar_line P_ ((struct it *));
953 static void notice_overwritten_cursor P_ ((struct window *,
954 enum glyph_row_area,
955 int, int, int, int));
956
957
958
959 #endif /* HAVE_WINDOW_SYSTEM */
960
961 \f
962 /***********************************************************************
963 Window display dimensions
964 ***********************************************************************/
965
966 /* Return the bottom boundary y-position for text lines in window W.
967 This is the first y position at which a line cannot start.
968 It is relative to the top of the window.
969
970 This is the height of W minus the height of a mode line, if any. */
971
972 INLINE int
973 window_text_bottom_y (w)
974 struct window *w;
975 {
976 int height = WINDOW_TOTAL_HEIGHT (w);
977
978 if (WINDOW_WANTS_MODELINE_P (w))
979 height -= CURRENT_MODE_LINE_HEIGHT (w);
980 return height;
981 }
982
983 /* Return the pixel width of display area AREA of window W. AREA < 0
984 means return the total width of W, not including fringes to
985 the left and right of the window. */
986
987 INLINE int
988 window_box_width (w, area)
989 struct window *w;
990 int area;
991 {
992 int cols = XFASTINT (w->total_cols);
993 int pixels = 0;
994
995 if (!w->pseudo_window_p)
996 {
997 cols -= WINDOW_SCROLL_BAR_COLS (w);
998
999 if (area == TEXT_AREA)
1000 {
1001 if (INTEGERP (w->left_margin_cols))
1002 cols -= XFASTINT (w->left_margin_cols);
1003 if (INTEGERP (w->right_margin_cols))
1004 cols -= XFASTINT (w->right_margin_cols);
1005 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1006 }
1007 else if (area == LEFT_MARGIN_AREA)
1008 {
1009 cols = (INTEGERP (w->left_margin_cols)
1010 ? XFASTINT (w->left_margin_cols) : 0);
1011 pixels = 0;
1012 }
1013 else if (area == RIGHT_MARGIN_AREA)
1014 {
1015 cols = (INTEGERP (w->right_margin_cols)
1016 ? XFASTINT (w->right_margin_cols) : 0);
1017 pixels = 0;
1018 }
1019 }
1020
1021 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1022 }
1023
1024
1025 /* Return the pixel height of the display area of window W, not
1026 including mode lines of W, if any. */
1027
1028 INLINE int
1029 window_box_height (w)
1030 struct window *w;
1031 {
1032 struct frame *f = XFRAME (w->frame);
1033 int height = WINDOW_TOTAL_HEIGHT (w);
1034
1035 xassert (height >= 0);
1036
1037 /* Note: the code below that determines the mode-line/header-line
1038 height is essentially the same as that contained in the macro
1039 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1040 the appropriate glyph row has its `mode_line_p' flag set,
1041 and if it doesn't, uses estimate_mode_line_height instead. */
1042
1043 if (WINDOW_WANTS_MODELINE_P (w))
1044 {
1045 struct glyph_row *ml_row
1046 = (w->current_matrix && w->current_matrix->rows
1047 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1048 : 0);
1049 if (ml_row && ml_row->mode_line_p)
1050 height -= ml_row->height;
1051 else
1052 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1053 }
1054
1055 if (WINDOW_WANTS_HEADER_LINE_P (w))
1056 {
1057 struct glyph_row *hl_row
1058 = (w->current_matrix && w->current_matrix->rows
1059 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1060 : 0);
1061 if (hl_row && hl_row->mode_line_p)
1062 height -= hl_row->height;
1063 else
1064 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1065 }
1066
1067 /* With a very small font and a mode-line that's taller than
1068 default, we might end up with a negative height. */
1069 return max (0, height);
1070 }
1071
1072 /* Return the window-relative coordinate of the left edge of display
1073 area AREA of window W. AREA < 0 means return the left edge of the
1074 whole window, to the right of the left fringe of W. */
1075
1076 INLINE int
1077 window_box_left_offset (w, area)
1078 struct window *w;
1079 int area;
1080 {
1081 int x;
1082
1083 if (w->pseudo_window_p)
1084 return 0;
1085
1086 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1087
1088 if (area == TEXT_AREA)
1089 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1090 + window_box_width (w, LEFT_MARGIN_AREA));
1091 else if (area == RIGHT_MARGIN_AREA)
1092 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1093 + window_box_width (w, LEFT_MARGIN_AREA)
1094 + window_box_width (w, TEXT_AREA)
1095 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1096 ? 0
1097 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1098 else if (area == LEFT_MARGIN_AREA
1099 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1100 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1101
1102 return x;
1103 }
1104
1105
1106 /* Return the window-relative coordinate of the right edge of display
1107 area AREA of window W. AREA < 0 means return the left edge of the
1108 whole window, to the left of the right fringe of W. */
1109
1110 INLINE int
1111 window_box_right_offset (w, area)
1112 struct window *w;
1113 int area;
1114 {
1115 return window_box_left_offset (w, area) + window_box_width (w, area);
1116 }
1117
1118 /* Return the frame-relative coordinate of the left edge of display
1119 area AREA of window W. AREA < 0 means return the left edge of the
1120 whole window, to the right of the left fringe of W. */
1121
1122 INLINE int
1123 window_box_left (w, area)
1124 struct window *w;
1125 int area;
1126 {
1127 struct frame *f = XFRAME (w->frame);
1128 int x;
1129
1130 if (w->pseudo_window_p)
1131 return FRAME_INTERNAL_BORDER_WIDTH (f);
1132
1133 x = (WINDOW_LEFT_EDGE_X (w)
1134 + window_box_left_offset (w, area));
1135
1136 return x;
1137 }
1138
1139
1140 /* Return the frame-relative coordinate of the right edge of display
1141 area AREA of window W. AREA < 0 means return the left edge of the
1142 whole window, to the left of the right fringe of W. */
1143
1144 INLINE int
1145 window_box_right (w, area)
1146 struct window *w;
1147 int area;
1148 {
1149 return window_box_left (w, area) + window_box_width (w, area);
1150 }
1151
1152 /* Get the bounding box of the display area AREA of window W, without
1153 mode lines, in frame-relative coordinates. AREA < 0 means the
1154 whole window, not including the left and right fringes of
1155 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1156 coordinates of the upper-left corner of the box. Return in
1157 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1158
1159 INLINE void
1160 window_box (w, area, box_x, box_y, box_width, box_height)
1161 struct window *w;
1162 int area;
1163 int *box_x, *box_y, *box_width, *box_height;
1164 {
1165 if (box_width)
1166 *box_width = window_box_width (w, area);
1167 if (box_height)
1168 *box_height = window_box_height (w);
1169 if (box_x)
1170 *box_x = window_box_left (w, area);
1171 if (box_y)
1172 {
1173 *box_y = WINDOW_TOP_EDGE_Y (w);
1174 if (WINDOW_WANTS_HEADER_LINE_P (w))
1175 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1176 }
1177 }
1178
1179
1180 /* Get the bounding box of the display area AREA of window W, without
1181 mode lines. AREA < 0 means the whole window, not including the
1182 left and right fringe of the window. Return in *TOP_LEFT_X
1183 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1184 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1185 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1186 box. */
1187
1188 INLINE void
1189 window_box_edges (w, area, top_left_x, top_left_y,
1190 bottom_right_x, bottom_right_y)
1191 struct window *w;
1192 int area;
1193 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1194 {
1195 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1196 bottom_right_y);
1197 *bottom_right_x += *top_left_x;
1198 *bottom_right_y += *top_left_y;
1199 }
1200
1201
1202 \f
1203 /***********************************************************************
1204 Utilities
1205 ***********************************************************************/
1206
1207 /* Return the bottom y-position of the line the iterator IT is in.
1208 This can modify IT's settings. */
1209
1210 int
1211 line_bottom_y (it)
1212 struct it *it;
1213 {
1214 int line_height = it->max_ascent + it->max_descent;
1215 int line_top_y = it->current_y;
1216
1217 if (line_height == 0)
1218 {
1219 if (last_height)
1220 line_height = last_height;
1221 else if (IT_CHARPOS (*it) < ZV)
1222 {
1223 move_it_by_lines (it, 1, 1);
1224 line_height = (it->max_ascent || it->max_descent
1225 ? it->max_ascent + it->max_descent
1226 : last_height);
1227 }
1228 else
1229 {
1230 struct glyph_row *row = it->glyph_row;
1231
1232 /* Use the default character height. */
1233 it->glyph_row = NULL;
1234 it->what = IT_CHARACTER;
1235 it->c = ' ';
1236 it->len = 1;
1237 PRODUCE_GLYPHS (it);
1238 line_height = it->ascent + it->descent;
1239 it->glyph_row = row;
1240 }
1241 }
1242
1243 return line_top_y + line_height;
1244 }
1245
1246
1247 /* Return 1 if position CHARPOS is visible in window W.
1248 If visible, set *X and *Y to pixel coordinates of top left corner.
1249 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1250 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1251 and header-lines heights. */
1252
1253 int
1254 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1255 struct window *w;
1256 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1257 {
1258 struct it it;
1259 struct text_pos top;
1260 int visible_p = 0;
1261 struct buffer *old_buffer = NULL;
1262
1263 if (noninteractive)
1264 return visible_p;
1265
1266 if (XBUFFER (w->buffer) != current_buffer)
1267 {
1268 old_buffer = current_buffer;
1269 set_buffer_internal_1 (XBUFFER (w->buffer));
1270 }
1271
1272 SET_TEXT_POS_FROM_MARKER (top, w->start);
1273
1274 /* Compute exact mode line heights, if requested. */
1275 if (exact_mode_line_heights_p)
1276 {
1277 if (WINDOW_WANTS_MODELINE_P (w))
1278 current_mode_line_height
1279 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1280 current_buffer->mode_line_format);
1281
1282 if (WINDOW_WANTS_HEADER_LINE_P (w))
1283 current_header_line_height
1284 = display_mode_line (w, HEADER_LINE_FACE_ID,
1285 current_buffer->header_line_format);
1286 }
1287
1288 start_display (&it, w, top);
1289 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1290 MOVE_TO_POS | MOVE_TO_Y);
1291
1292 /* Note that we may overshoot because of invisible text. */
1293 if (IT_CHARPOS (it) >= charpos)
1294 {
1295 int top_x = it.current_x;
1296 int top_y = it.current_y;
1297 int bottom_y = (last_height = 0, line_bottom_y (&it));
1298 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1299
1300 if (top_y < window_top_y)
1301 visible_p = bottom_y > window_top_y;
1302 else if (top_y < it.last_visible_y)
1303 visible_p = 1;
1304 if (visible_p)
1305 {
1306 *x = top_x;
1307 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1308 *rtop = max (0, window_top_y - top_y);
1309 *rbot = max (0, bottom_y - it.last_visible_y);
1310 }
1311 }
1312 else
1313 {
1314 struct it it2;
1315
1316 it2 = it;
1317 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1318 move_it_by_lines (&it, 1, 0);
1319 if (charpos < IT_CHARPOS (it))
1320 {
1321 visible_p = 1;
1322 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1323 *x = it2.current_x;
1324 *y = it2.current_y + it2.max_ascent - it2.ascent;
1325 *rtop = max (0, -it2.current_y);
1326 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1327 - it.last_visible_y));
1328 }
1329 }
1330
1331 if (old_buffer)
1332 set_buffer_internal_1 (old_buffer);
1333
1334 current_header_line_height = current_mode_line_height = -1;
1335
1336 return visible_p;
1337 }
1338
1339
1340 /* Return the next character from STR which is MAXLEN bytes long.
1341 Return in *LEN the length of the character. This is like
1342 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1343 we find one, we return a `?', but with the length of the invalid
1344 character. */
1345
1346 static INLINE int
1347 string_char_and_length (str, maxlen, len)
1348 const unsigned char *str;
1349 int maxlen, *len;
1350 {
1351 int c;
1352
1353 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1354 if (!CHAR_VALID_P (c, 1))
1355 /* We may not change the length here because other places in Emacs
1356 don't use this function, i.e. they silently accept invalid
1357 characters. */
1358 c = '?';
1359
1360 return c;
1361 }
1362
1363
1364
1365 /* Given a position POS containing a valid character and byte position
1366 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1367
1368 static struct text_pos
1369 string_pos_nchars_ahead (pos, string, nchars)
1370 struct text_pos pos;
1371 Lisp_Object string;
1372 int nchars;
1373 {
1374 xassert (STRINGP (string) && nchars >= 0);
1375
1376 if (STRING_MULTIBYTE (string))
1377 {
1378 int rest = SBYTES (string) - BYTEPOS (pos);
1379 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1380 int len;
1381
1382 while (nchars--)
1383 {
1384 string_char_and_length (p, rest, &len);
1385 p += len, rest -= len;
1386 xassert (rest >= 0);
1387 CHARPOS (pos) += 1;
1388 BYTEPOS (pos) += len;
1389 }
1390 }
1391 else
1392 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1393
1394 return pos;
1395 }
1396
1397
1398 /* Value is the text position, i.e. character and byte position,
1399 for character position CHARPOS in STRING. */
1400
1401 static INLINE struct text_pos
1402 string_pos (charpos, string)
1403 int charpos;
1404 Lisp_Object string;
1405 {
1406 struct text_pos pos;
1407 xassert (STRINGP (string));
1408 xassert (charpos >= 0);
1409 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1410 return pos;
1411 }
1412
1413
1414 /* Value is a text position, i.e. character and byte position, for
1415 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1416 means recognize multibyte characters. */
1417
1418 static struct text_pos
1419 c_string_pos (charpos, s, multibyte_p)
1420 int charpos;
1421 unsigned char *s;
1422 int multibyte_p;
1423 {
1424 struct text_pos pos;
1425
1426 xassert (s != NULL);
1427 xassert (charpos >= 0);
1428
1429 if (multibyte_p)
1430 {
1431 int rest = strlen (s), len;
1432
1433 SET_TEXT_POS (pos, 0, 0);
1434 while (charpos--)
1435 {
1436 string_char_and_length (s, rest, &len);
1437 s += len, rest -= len;
1438 xassert (rest >= 0);
1439 CHARPOS (pos) += 1;
1440 BYTEPOS (pos) += len;
1441 }
1442 }
1443 else
1444 SET_TEXT_POS (pos, charpos, charpos);
1445
1446 return pos;
1447 }
1448
1449
1450 /* Value is the number of characters in C string S. MULTIBYTE_P
1451 non-zero means recognize multibyte characters. */
1452
1453 static int
1454 number_of_chars (s, multibyte_p)
1455 unsigned char *s;
1456 int multibyte_p;
1457 {
1458 int nchars;
1459
1460 if (multibyte_p)
1461 {
1462 int rest = strlen (s), len;
1463 unsigned char *p = (unsigned char *) s;
1464
1465 for (nchars = 0; rest > 0; ++nchars)
1466 {
1467 string_char_and_length (p, rest, &len);
1468 rest -= len, p += len;
1469 }
1470 }
1471 else
1472 nchars = strlen (s);
1473
1474 return nchars;
1475 }
1476
1477
1478 /* Compute byte position NEWPOS->bytepos corresponding to
1479 NEWPOS->charpos. POS is a known position in string STRING.
1480 NEWPOS->charpos must be >= POS.charpos. */
1481
1482 static void
1483 compute_string_pos (newpos, pos, string)
1484 struct text_pos *newpos, pos;
1485 Lisp_Object string;
1486 {
1487 xassert (STRINGP (string));
1488 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1489
1490 if (STRING_MULTIBYTE (string))
1491 *newpos = string_pos_nchars_ahead (pos, string,
1492 CHARPOS (*newpos) - CHARPOS (pos));
1493 else
1494 BYTEPOS (*newpos) = CHARPOS (*newpos);
1495 }
1496
1497 /* EXPORT:
1498 Return an estimation of the pixel height of mode or top lines on
1499 frame F. FACE_ID specifies what line's height to estimate. */
1500
1501 int
1502 estimate_mode_line_height (f, face_id)
1503 struct frame *f;
1504 enum face_id face_id;
1505 {
1506 #ifdef HAVE_WINDOW_SYSTEM
1507 if (FRAME_WINDOW_P (f))
1508 {
1509 int height = FONT_HEIGHT (FRAME_FONT (f));
1510
1511 /* This function is called so early when Emacs starts that the face
1512 cache and mode line face are not yet initialized. */
1513 if (FRAME_FACE_CACHE (f))
1514 {
1515 struct face *face = FACE_FROM_ID (f, face_id);
1516 if (face)
1517 {
1518 if (face->font)
1519 height = FONT_HEIGHT (face->font);
1520 if (face->box_line_width > 0)
1521 height += 2 * face->box_line_width;
1522 }
1523 }
1524
1525 return height;
1526 }
1527 #endif
1528
1529 return 1;
1530 }
1531
1532 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1533 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1534 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1535 not force the value into range. */
1536
1537 void
1538 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1539 FRAME_PTR f;
1540 register int pix_x, pix_y;
1541 int *x, *y;
1542 NativeRectangle *bounds;
1543 int noclip;
1544 {
1545
1546 #ifdef HAVE_WINDOW_SYSTEM
1547 if (FRAME_WINDOW_P (f))
1548 {
1549 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1550 even for negative values. */
1551 if (pix_x < 0)
1552 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1553 if (pix_y < 0)
1554 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1555
1556 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1557 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1558
1559 if (bounds)
1560 STORE_NATIVE_RECT (*bounds,
1561 FRAME_COL_TO_PIXEL_X (f, pix_x),
1562 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1563 FRAME_COLUMN_WIDTH (f) - 1,
1564 FRAME_LINE_HEIGHT (f) - 1);
1565
1566 if (!noclip)
1567 {
1568 if (pix_x < 0)
1569 pix_x = 0;
1570 else if (pix_x > FRAME_TOTAL_COLS (f))
1571 pix_x = FRAME_TOTAL_COLS (f);
1572
1573 if (pix_y < 0)
1574 pix_y = 0;
1575 else if (pix_y > FRAME_LINES (f))
1576 pix_y = FRAME_LINES (f);
1577 }
1578 }
1579 #endif
1580
1581 *x = pix_x;
1582 *y = pix_y;
1583 }
1584
1585
1586 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1587 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1588 can't tell the positions because W's display is not up to date,
1589 return 0. */
1590
1591 int
1592 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1593 struct window *w;
1594 int hpos, vpos;
1595 int *frame_x, *frame_y;
1596 {
1597 #ifdef HAVE_WINDOW_SYSTEM
1598 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1599 {
1600 int success_p;
1601
1602 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1603 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1604
1605 if (display_completed)
1606 {
1607 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1608 struct glyph *glyph = row->glyphs[TEXT_AREA];
1609 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1610
1611 hpos = row->x;
1612 vpos = row->y;
1613 while (glyph < end)
1614 {
1615 hpos += glyph->pixel_width;
1616 ++glyph;
1617 }
1618
1619 /* If first glyph is partially visible, its first visible position is still 0. */
1620 if (hpos < 0)
1621 hpos = 0;
1622
1623 success_p = 1;
1624 }
1625 else
1626 {
1627 hpos = vpos = 0;
1628 success_p = 0;
1629 }
1630
1631 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1632 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1633 return success_p;
1634 }
1635 #endif
1636
1637 *frame_x = hpos;
1638 *frame_y = vpos;
1639 return 1;
1640 }
1641
1642
1643 #ifdef HAVE_WINDOW_SYSTEM
1644
1645 /* Find the glyph under window-relative coordinates X/Y in window W.
1646 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1647 strings. Return in *HPOS and *VPOS the row and column number of
1648 the glyph found. Return in *AREA the glyph area containing X.
1649 Value is a pointer to the glyph found or null if X/Y is not on
1650 text, or we can't tell because W's current matrix is not up to
1651 date. */
1652
1653 static struct glyph *
1654 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1655 struct window *w;
1656 int x, y;
1657 int *hpos, *vpos, *dx, *dy, *area;
1658 {
1659 struct glyph *glyph, *end;
1660 struct glyph_row *row = NULL;
1661 int x0, i;
1662
1663 /* Find row containing Y. Give up if some row is not enabled. */
1664 for (i = 0; i < w->current_matrix->nrows; ++i)
1665 {
1666 row = MATRIX_ROW (w->current_matrix, i);
1667 if (!row->enabled_p)
1668 return NULL;
1669 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1670 break;
1671 }
1672
1673 *vpos = i;
1674 *hpos = 0;
1675
1676 /* Give up if Y is not in the window. */
1677 if (i == w->current_matrix->nrows)
1678 return NULL;
1679
1680 /* Get the glyph area containing X. */
1681 if (w->pseudo_window_p)
1682 {
1683 *area = TEXT_AREA;
1684 x0 = 0;
1685 }
1686 else
1687 {
1688 if (x < window_box_left_offset (w, TEXT_AREA))
1689 {
1690 *area = LEFT_MARGIN_AREA;
1691 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1692 }
1693 else if (x < window_box_right_offset (w, TEXT_AREA))
1694 {
1695 *area = TEXT_AREA;
1696 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1697 }
1698 else
1699 {
1700 *area = RIGHT_MARGIN_AREA;
1701 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1702 }
1703 }
1704
1705 /* Find glyph containing X. */
1706 glyph = row->glyphs[*area];
1707 end = glyph + row->used[*area];
1708 x -= x0;
1709 while (glyph < end && x >= glyph->pixel_width)
1710 {
1711 x -= glyph->pixel_width;
1712 ++glyph;
1713 }
1714
1715 if (glyph == end)
1716 return NULL;
1717
1718 if (dx)
1719 {
1720 *dx = x;
1721 *dy = y - (row->y + row->ascent - glyph->ascent);
1722 }
1723
1724 *hpos = glyph - row->glyphs[*area];
1725 return glyph;
1726 }
1727
1728
1729 /* EXPORT:
1730 Convert frame-relative x/y to coordinates relative to window W.
1731 Takes pseudo-windows into account. */
1732
1733 void
1734 frame_to_window_pixel_xy (w, x, y)
1735 struct window *w;
1736 int *x, *y;
1737 {
1738 if (w->pseudo_window_p)
1739 {
1740 /* A pseudo-window is always full-width, and starts at the
1741 left edge of the frame, plus a frame border. */
1742 struct frame *f = XFRAME (w->frame);
1743 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1744 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1745 }
1746 else
1747 {
1748 *x -= WINDOW_LEFT_EDGE_X (w);
1749 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1750 }
1751 }
1752
1753 /* EXPORT:
1754 Return in *R the clipping rectangle for glyph string S. */
1755
1756 void
1757 get_glyph_string_clip_rect (s, nr)
1758 struct glyph_string *s;
1759 NativeRectangle *nr;
1760 {
1761 XRectangle r;
1762
1763 if (s->row->full_width_p)
1764 {
1765 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1766 r.x = WINDOW_LEFT_EDGE_X (s->w);
1767 r.width = WINDOW_TOTAL_WIDTH (s->w);
1768
1769 /* Unless displaying a mode or menu bar line, which are always
1770 fully visible, clip to the visible part of the row. */
1771 if (s->w->pseudo_window_p)
1772 r.height = s->row->visible_height;
1773 else
1774 r.height = s->height;
1775 }
1776 else
1777 {
1778 /* This is a text line that may be partially visible. */
1779 r.x = window_box_left (s->w, s->area);
1780 r.width = window_box_width (s->w, s->area);
1781 r.height = s->row->visible_height;
1782 }
1783
1784 if (s->clip_head)
1785 if (r.x < s->clip_head->x)
1786 {
1787 if (r.width >= s->clip_head->x - r.x)
1788 r.width -= s->clip_head->x - r.x;
1789 else
1790 r.width = 0;
1791 r.x = s->clip_head->x;
1792 }
1793 if (s->clip_tail)
1794 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1795 {
1796 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1797 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1798 else
1799 r.width = 0;
1800 }
1801
1802 /* If S draws overlapping rows, it's sufficient to use the top and
1803 bottom of the window for clipping because this glyph string
1804 intentionally draws over other lines. */
1805 if (s->for_overlaps_p)
1806 {
1807 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1808 r.height = window_text_bottom_y (s->w) - r.y;
1809 }
1810 else
1811 {
1812 /* Don't use S->y for clipping because it doesn't take partially
1813 visible lines into account. For example, it can be negative for
1814 partially visible lines at the top of a window. */
1815 if (!s->row->full_width_p
1816 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1817 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1818 else
1819 r.y = max (0, s->row->y);
1820
1821 /* If drawing a tool-bar window, draw it over the internal border
1822 at the top of the window. */
1823 if (WINDOWP (s->f->tool_bar_window)
1824 && s->w == XWINDOW (s->f->tool_bar_window))
1825 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1826 }
1827
1828 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1829
1830 /* If drawing the cursor, don't let glyph draw outside its
1831 advertised boundaries. Cleartype does this under some circumstances. */
1832 if (s->hl == DRAW_CURSOR)
1833 {
1834 struct glyph *glyph = s->first_glyph;
1835 int height, max_y;
1836
1837 if (s->x > r.x)
1838 {
1839 r.width -= s->x - r.x;
1840 r.x = s->x;
1841 }
1842 r.width = min (r.width, glyph->pixel_width);
1843
1844 /* If r.y is below window bottom, ensure that we still see a cursor. */
1845 height = min (glyph->ascent + glyph->descent,
1846 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1847 max_y = window_text_bottom_y (s->w) - height;
1848 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1849 if (s->ybase - glyph->ascent > max_y)
1850 {
1851 r.y = max_y;
1852 r.height = height;
1853 }
1854 else
1855 {
1856 /* Don't draw cursor glyph taller than our actual glyph. */
1857 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1858 if (height < r.height)
1859 {
1860 max_y = r.y + r.height;
1861 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1862 r.height = min (max_y - r.y, height);
1863 }
1864 }
1865 }
1866
1867 #ifdef CONVERT_FROM_XRECT
1868 CONVERT_FROM_XRECT (r, *nr);
1869 #else
1870 *nr = r;
1871 #endif
1872 }
1873
1874
1875 /* EXPORT:
1876 Return the position and height of the phys cursor in window W.
1877 Set w->phys_cursor_width to width of phys cursor.
1878 */
1879
1880 int
1881 get_phys_cursor_geometry (w, row, glyph, heightp)
1882 struct window *w;
1883 struct glyph_row *row;
1884 struct glyph *glyph;
1885 int *heightp;
1886 {
1887 struct frame *f = XFRAME (WINDOW_FRAME (w));
1888 int y, wd, h, h0, y0;
1889
1890 /* Compute the width of the rectangle to draw. If on a stretch
1891 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1892 rectangle as wide as the glyph, but use a canonical character
1893 width instead. */
1894 wd = glyph->pixel_width - 1;
1895 #ifdef HAVE_NTGUI
1896 wd++; /* Why? */
1897 #endif
1898 if (glyph->type == STRETCH_GLYPH
1899 && !x_stretch_cursor_p)
1900 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1901 w->phys_cursor_width = wd;
1902
1903 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1904
1905 /* If y is below window bottom, ensure that we still see a cursor. */
1906 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1907
1908 h = max (h0, glyph->ascent + glyph->descent);
1909 h0 = min (h0, glyph->ascent + glyph->descent);
1910
1911 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1912 if (y < y0)
1913 {
1914 h = max (h - (y0 - y) + 1, h0);
1915 y = y0 - 1;
1916 }
1917 else
1918 {
1919 y0 = window_text_bottom_y (w) - h0;
1920 if (y > y0)
1921 {
1922 h += y - y0;
1923 y = y0;
1924 }
1925 }
1926
1927 *heightp = h - 1;
1928 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1929 }
1930
1931
1932 #endif /* HAVE_WINDOW_SYSTEM */
1933
1934 \f
1935 /***********************************************************************
1936 Lisp form evaluation
1937 ***********************************************************************/
1938
1939 /* Error handler for safe_eval and safe_call. */
1940
1941 static Lisp_Object
1942 safe_eval_handler (arg)
1943 Lisp_Object arg;
1944 {
1945 add_to_log ("Error during redisplay: %s", arg, Qnil);
1946 return Qnil;
1947 }
1948
1949
1950 /* Evaluate SEXPR and return the result, or nil if something went
1951 wrong. Prevent redisplay during the evaluation. */
1952
1953 Lisp_Object
1954 safe_eval (sexpr)
1955 Lisp_Object sexpr;
1956 {
1957 Lisp_Object val;
1958
1959 if (inhibit_eval_during_redisplay)
1960 val = Qnil;
1961 else
1962 {
1963 int count = SPECPDL_INDEX ();
1964 struct gcpro gcpro1;
1965
1966 GCPRO1 (sexpr);
1967 specbind (Qinhibit_redisplay, Qt);
1968 /* Use Qt to ensure debugger does not run,
1969 so there is no possibility of wanting to redisplay. */
1970 val = internal_condition_case_1 (Feval, sexpr, Qt,
1971 safe_eval_handler);
1972 UNGCPRO;
1973 val = unbind_to (count, val);
1974 }
1975
1976 return val;
1977 }
1978
1979
1980 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1981 Return the result, or nil if something went wrong. Prevent
1982 redisplay during the evaluation. */
1983
1984 Lisp_Object
1985 safe_call (nargs, args)
1986 int nargs;
1987 Lisp_Object *args;
1988 {
1989 Lisp_Object val;
1990
1991 if (inhibit_eval_during_redisplay)
1992 val = Qnil;
1993 else
1994 {
1995 int count = SPECPDL_INDEX ();
1996 struct gcpro gcpro1;
1997
1998 GCPRO1 (args[0]);
1999 gcpro1.nvars = nargs;
2000 specbind (Qinhibit_redisplay, Qt);
2001 /* Use Qt to ensure debugger does not run,
2002 so there is no possibility of wanting to redisplay. */
2003 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2004 safe_eval_handler);
2005 UNGCPRO;
2006 val = unbind_to (count, val);
2007 }
2008
2009 return val;
2010 }
2011
2012
2013 /* Call function FN with one argument ARG.
2014 Return the result, or nil if something went wrong. */
2015
2016 Lisp_Object
2017 safe_call1 (fn, arg)
2018 Lisp_Object fn, arg;
2019 {
2020 Lisp_Object args[2];
2021 args[0] = fn;
2022 args[1] = arg;
2023 return safe_call (2, args);
2024 }
2025
2026
2027 \f
2028 /***********************************************************************
2029 Debugging
2030 ***********************************************************************/
2031
2032 #if 0
2033
2034 /* Define CHECK_IT to perform sanity checks on iterators.
2035 This is for debugging. It is too slow to do unconditionally. */
2036
2037 static void
2038 check_it (it)
2039 struct it *it;
2040 {
2041 if (it->method == GET_FROM_STRING)
2042 {
2043 xassert (STRINGP (it->string));
2044 xassert (IT_STRING_CHARPOS (*it) >= 0);
2045 }
2046 else
2047 {
2048 xassert (IT_STRING_CHARPOS (*it) < 0);
2049 if (it->method == GET_FROM_BUFFER)
2050 {
2051 /* Check that character and byte positions agree. */
2052 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2053 }
2054 }
2055
2056 if (it->dpvec)
2057 xassert (it->current.dpvec_index >= 0);
2058 else
2059 xassert (it->current.dpvec_index < 0);
2060 }
2061
2062 #define CHECK_IT(IT) check_it ((IT))
2063
2064 #else /* not 0 */
2065
2066 #define CHECK_IT(IT) (void) 0
2067
2068 #endif /* not 0 */
2069
2070
2071 #if GLYPH_DEBUG
2072
2073 /* Check that the window end of window W is what we expect it
2074 to be---the last row in the current matrix displaying text. */
2075
2076 static void
2077 check_window_end (w)
2078 struct window *w;
2079 {
2080 if (!MINI_WINDOW_P (w)
2081 && !NILP (w->window_end_valid))
2082 {
2083 struct glyph_row *row;
2084 xassert ((row = MATRIX_ROW (w->current_matrix,
2085 XFASTINT (w->window_end_vpos)),
2086 !row->enabled_p
2087 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2088 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2089 }
2090 }
2091
2092 #define CHECK_WINDOW_END(W) check_window_end ((W))
2093
2094 #else /* not GLYPH_DEBUG */
2095
2096 #define CHECK_WINDOW_END(W) (void) 0
2097
2098 #endif /* not GLYPH_DEBUG */
2099
2100
2101 \f
2102 /***********************************************************************
2103 Iterator initialization
2104 ***********************************************************************/
2105
2106 /* Initialize IT for displaying current_buffer in window W, starting
2107 at character position CHARPOS. CHARPOS < 0 means that no buffer
2108 position is specified which is useful when the iterator is assigned
2109 a position later. BYTEPOS is the byte position corresponding to
2110 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2111
2112 If ROW is not null, calls to produce_glyphs with IT as parameter
2113 will produce glyphs in that row.
2114
2115 BASE_FACE_ID is the id of a base face to use. It must be one of
2116 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2117 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2118 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2119
2120 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2121 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2122 will be initialized to use the corresponding mode line glyph row of
2123 the desired matrix of W. */
2124
2125 void
2126 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2127 struct it *it;
2128 struct window *w;
2129 int charpos, bytepos;
2130 struct glyph_row *row;
2131 enum face_id base_face_id;
2132 {
2133 int highlight_region_p;
2134
2135 /* Some precondition checks. */
2136 xassert (w != NULL && it != NULL);
2137 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2138 && charpos <= ZV));
2139
2140 /* If face attributes have been changed since the last redisplay,
2141 free realized faces now because they depend on face definitions
2142 that might have changed. Don't free faces while there might be
2143 desired matrices pending which reference these faces. */
2144 if (face_change_count && !inhibit_free_realized_faces)
2145 {
2146 face_change_count = 0;
2147 free_all_realized_faces (Qnil);
2148 }
2149
2150 /* Use one of the mode line rows of W's desired matrix if
2151 appropriate. */
2152 if (row == NULL)
2153 {
2154 if (base_face_id == MODE_LINE_FACE_ID
2155 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2156 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2157 else if (base_face_id == HEADER_LINE_FACE_ID)
2158 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2159 }
2160
2161 /* Clear IT. */
2162 bzero (it, sizeof *it);
2163 it->current.overlay_string_index = -1;
2164 it->current.dpvec_index = -1;
2165 it->base_face_id = base_face_id;
2166 it->string = Qnil;
2167 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2168
2169 /* The window in which we iterate over current_buffer: */
2170 XSETWINDOW (it->window, w);
2171 it->w = w;
2172 it->f = XFRAME (w->frame);
2173
2174 /* Extra space between lines (on window systems only). */
2175 if (base_face_id == DEFAULT_FACE_ID
2176 && FRAME_WINDOW_P (it->f))
2177 {
2178 if (NATNUMP (current_buffer->extra_line_spacing))
2179 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2180 else if (FLOATP (current_buffer->extra_line_spacing))
2181 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2182 * FRAME_LINE_HEIGHT (it->f));
2183 else if (it->f->extra_line_spacing > 0)
2184 it->extra_line_spacing = it->f->extra_line_spacing;
2185 it->max_extra_line_spacing = 0;
2186 }
2187
2188 /* If realized faces have been removed, e.g. because of face
2189 attribute changes of named faces, recompute them. When running
2190 in batch mode, the face cache of the initial frame is null. If
2191 we happen to get called, make a dummy face cache. */
2192 if (FRAME_FACE_CACHE (it->f) == NULL)
2193 init_frame_faces (it->f);
2194 if (FRAME_FACE_CACHE (it->f)->used == 0)
2195 recompute_basic_faces (it->f);
2196
2197 /* Current value of the `slice', `space-width', and 'height' properties. */
2198 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2199 it->space_width = Qnil;
2200 it->font_height = Qnil;
2201 it->override_ascent = -1;
2202
2203 /* Are control characters displayed as `^C'? */
2204 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2205
2206 /* -1 means everything between a CR and the following line end
2207 is invisible. >0 means lines indented more than this value are
2208 invisible. */
2209 it->selective = (INTEGERP (current_buffer->selective_display)
2210 ? XFASTINT (current_buffer->selective_display)
2211 : (!NILP (current_buffer->selective_display)
2212 ? -1 : 0));
2213 it->selective_display_ellipsis_p
2214 = !NILP (current_buffer->selective_display_ellipses);
2215
2216 /* Display table to use. */
2217 it->dp = window_display_table (w);
2218
2219 /* Are multibyte characters enabled in current_buffer? */
2220 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2221
2222 /* Non-zero if we should highlight the region. */
2223 highlight_region_p
2224 = (!NILP (Vtransient_mark_mode)
2225 && !NILP (current_buffer->mark_active)
2226 && XMARKER (current_buffer->mark)->buffer != 0);
2227
2228 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2229 start and end of a visible region in window IT->w. Set both to
2230 -1 to indicate no region. */
2231 if (highlight_region_p
2232 /* Maybe highlight only in selected window. */
2233 && (/* Either show region everywhere. */
2234 highlight_nonselected_windows
2235 /* Or show region in the selected window. */
2236 || w == XWINDOW (selected_window)
2237 /* Or show the region if we are in the mini-buffer and W is
2238 the window the mini-buffer refers to. */
2239 || (MINI_WINDOW_P (XWINDOW (selected_window))
2240 && WINDOWP (minibuf_selected_window)
2241 && w == XWINDOW (minibuf_selected_window))))
2242 {
2243 int charpos = marker_position (current_buffer->mark);
2244 it->region_beg_charpos = min (PT, charpos);
2245 it->region_end_charpos = max (PT, charpos);
2246 }
2247 else
2248 it->region_beg_charpos = it->region_end_charpos = -1;
2249
2250 /* Get the position at which the redisplay_end_trigger hook should
2251 be run, if it is to be run at all. */
2252 if (MARKERP (w->redisplay_end_trigger)
2253 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2254 it->redisplay_end_trigger_charpos
2255 = marker_position (w->redisplay_end_trigger);
2256 else if (INTEGERP (w->redisplay_end_trigger))
2257 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2258
2259 /* Correct bogus values of tab_width. */
2260 it->tab_width = XINT (current_buffer->tab_width);
2261 if (it->tab_width <= 0 || it->tab_width > 1000)
2262 it->tab_width = 8;
2263
2264 /* Are lines in the display truncated? */
2265 it->truncate_lines_p
2266 = (base_face_id != DEFAULT_FACE_ID
2267 || XINT (it->w->hscroll)
2268 || (truncate_partial_width_windows
2269 && !WINDOW_FULL_WIDTH_P (it->w))
2270 || !NILP (current_buffer->truncate_lines));
2271
2272 /* Get dimensions of truncation and continuation glyphs. These are
2273 displayed as fringe bitmaps under X, so we don't need them for such
2274 frames. */
2275 if (!FRAME_WINDOW_P (it->f))
2276 {
2277 if (it->truncate_lines_p)
2278 {
2279 /* We will need the truncation glyph. */
2280 xassert (it->glyph_row == NULL);
2281 produce_special_glyphs (it, IT_TRUNCATION);
2282 it->truncation_pixel_width = it->pixel_width;
2283 }
2284 else
2285 {
2286 /* We will need the continuation glyph. */
2287 xassert (it->glyph_row == NULL);
2288 produce_special_glyphs (it, IT_CONTINUATION);
2289 it->continuation_pixel_width = it->pixel_width;
2290 }
2291
2292 /* Reset these values to zero because the produce_special_glyphs
2293 above has changed them. */
2294 it->pixel_width = it->ascent = it->descent = 0;
2295 it->phys_ascent = it->phys_descent = 0;
2296 }
2297
2298 /* Set this after getting the dimensions of truncation and
2299 continuation glyphs, so that we don't produce glyphs when calling
2300 produce_special_glyphs, above. */
2301 it->glyph_row = row;
2302 it->area = TEXT_AREA;
2303
2304 /* Get the dimensions of the display area. The display area
2305 consists of the visible window area plus a horizontally scrolled
2306 part to the left of the window. All x-values are relative to the
2307 start of this total display area. */
2308 if (base_face_id != DEFAULT_FACE_ID)
2309 {
2310 /* Mode lines, menu bar in terminal frames. */
2311 it->first_visible_x = 0;
2312 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2313 }
2314 else
2315 {
2316 it->first_visible_x
2317 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2318 it->last_visible_x = (it->first_visible_x
2319 + window_box_width (w, TEXT_AREA));
2320
2321 /* If we truncate lines, leave room for the truncator glyph(s) at
2322 the right margin. Otherwise, leave room for the continuation
2323 glyph(s). Truncation and continuation glyphs are not inserted
2324 for window-based redisplay. */
2325 if (!FRAME_WINDOW_P (it->f))
2326 {
2327 if (it->truncate_lines_p)
2328 it->last_visible_x -= it->truncation_pixel_width;
2329 else
2330 it->last_visible_x -= it->continuation_pixel_width;
2331 }
2332
2333 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2334 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2335 }
2336
2337 /* Leave room for a border glyph. */
2338 if (!FRAME_WINDOW_P (it->f)
2339 && !WINDOW_RIGHTMOST_P (it->w))
2340 it->last_visible_x -= 1;
2341
2342 it->last_visible_y = window_text_bottom_y (w);
2343
2344 /* For mode lines and alike, arrange for the first glyph having a
2345 left box line if the face specifies a box. */
2346 if (base_face_id != DEFAULT_FACE_ID)
2347 {
2348 struct face *face;
2349
2350 it->face_id = base_face_id;
2351
2352 /* If we have a boxed mode line, make the first character appear
2353 with a left box line. */
2354 face = FACE_FROM_ID (it->f, base_face_id);
2355 if (face->box != FACE_NO_BOX)
2356 it->start_of_box_run_p = 1;
2357 }
2358
2359 /* If a buffer position was specified, set the iterator there,
2360 getting overlays and face properties from that position. */
2361 if (charpos >= BUF_BEG (current_buffer))
2362 {
2363 it->end_charpos = ZV;
2364 it->face_id = -1;
2365 IT_CHARPOS (*it) = charpos;
2366
2367 /* Compute byte position if not specified. */
2368 if (bytepos < charpos)
2369 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2370 else
2371 IT_BYTEPOS (*it) = bytepos;
2372
2373 it->start = it->current;
2374
2375 /* Compute faces etc. */
2376 reseat (it, it->current.pos, 1);
2377 }
2378
2379 CHECK_IT (it);
2380 }
2381
2382
2383 /* Initialize IT for the display of window W with window start POS. */
2384
2385 void
2386 start_display (it, w, pos)
2387 struct it *it;
2388 struct window *w;
2389 struct text_pos pos;
2390 {
2391 struct glyph_row *row;
2392 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2393
2394 row = w->desired_matrix->rows + first_vpos;
2395 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2396 it->first_vpos = first_vpos;
2397
2398 if (!it->truncate_lines_p)
2399 {
2400 int start_at_line_beg_p;
2401 int first_y = it->current_y;
2402
2403 /* If window start is not at a line start, skip forward to POS to
2404 get the correct continuation lines width. */
2405 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2406 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2407 if (!start_at_line_beg_p)
2408 {
2409 int new_x;
2410
2411 reseat_at_previous_visible_line_start (it);
2412 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2413
2414 new_x = it->current_x + it->pixel_width;
2415
2416 /* If lines are continued, this line may end in the middle
2417 of a multi-glyph character (e.g. a control character
2418 displayed as \003, or in the middle of an overlay
2419 string). In this case move_it_to above will not have
2420 taken us to the start of the continuation line but to the
2421 end of the continued line. */
2422 if (it->current_x > 0
2423 && !it->truncate_lines_p /* Lines are continued. */
2424 && (/* And glyph doesn't fit on the line. */
2425 new_x > it->last_visible_x
2426 /* Or it fits exactly and we're on a window
2427 system frame. */
2428 || (new_x == it->last_visible_x
2429 && FRAME_WINDOW_P (it->f))))
2430 {
2431 if (it->current.dpvec_index >= 0
2432 || it->current.overlay_string_index >= 0)
2433 {
2434 set_iterator_to_next (it, 1);
2435 move_it_in_display_line_to (it, -1, -1, 0);
2436 }
2437
2438 it->continuation_lines_width += it->current_x;
2439 }
2440
2441 /* We're starting a new display line, not affected by the
2442 height of the continued line, so clear the appropriate
2443 fields in the iterator structure. */
2444 it->max_ascent = it->max_descent = 0;
2445 it->max_phys_ascent = it->max_phys_descent = 0;
2446
2447 it->current_y = first_y;
2448 it->vpos = 0;
2449 it->current_x = it->hpos = 0;
2450 }
2451 }
2452
2453 #if 0 /* Don't assert the following because start_display is sometimes
2454 called intentionally with a window start that is not at a
2455 line start. Please leave this code in as a comment. */
2456
2457 /* Window start should be on a line start, now. */
2458 xassert (it->continuation_lines_width
2459 || IT_CHARPOS (it) == BEGV
2460 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2461 #endif /* 0 */
2462 }
2463
2464
2465 /* Return 1 if POS is a position in ellipses displayed for invisible
2466 text. W is the window we display, for text property lookup. */
2467
2468 static int
2469 in_ellipses_for_invisible_text_p (pos, w)
2470 struct display_pos *pos;
2471 struct window *w;
2472 {
2473 Lisp_Object prop, window;
2474 int ellipses_p = 0;
2475 int charpos = CHARPOS (pos->pos);
2476
2477 /* If POS specifies a position in a display vector, this might
2478 be for an ellipsis displayed for invisible text. We won't
2479 get the iterator set up for delivering that ellipsis unless
2480 we make sure that it gets aware of the invisible text. */
2481 if (pos->dpvec_index >= 0
2482 && pos->overlay_string_index < 0
2483 && CHARPOS (pos->string_pos) < 0
2484 && charpos > BEGV
2485 && (XSETWINDOW (window, w),
2486 prop = Fget_char_property (make_number (charpos),
2487 Qinvisible, window),
2488 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2489 {
2490 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2491 window);
2492 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2493 }
2494
2495 return ellipses_p;
2496 }
2497
2498
2499 /* Initialize IT for stepping through current_buffer in window W,
2500 starting at position POS that includes overlay string and display
2501 vector/ control character translation position information. Value
2502 is zero if there are overlay strings with newlines at POS. */
2503
2504 static int
2505 init_from_display_pos (it, w, pos)
2506 struct it *it;
2507 struct window *w;
2508 struct display_pos *pos;
2509 {
2510 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2511 int i, overlay_strings_with_newlines = 0;
2512
2513 /* If POS specifies a position in a display vector, this might
2514 be for an ellipsis displayed for invisible text. We won't
2515 get the iterator set up for delivering that ellipsis unless
2516 we make sure that it gets aware of the invisible text. */
2517 if (in_ellipses_for_invisible_text_p (pos, w))
2518 {
2519 --charpos;
2520 bytepos = 0;
2521 }
2522
2523 /* Keep in mind: the call to reseat in init_iterator skips invisible
2524 text, so we might end up at a position different from POS. This
2525 is only a problem when POS is a row start after a newline and an
2526 overlay starts there with an after-string, and the overlay has an
2527 invisible property. Since we don't skip invisible text in
2528 display_line and elsewhere immediately after consuming the
2529 newline before the row start, such a POS will not be in a string,
2530 but the call to init_iterator below will move us to the
2531 after-string. */
2532 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2533
2534 /* This only scans the current chunk -- it should scan all chunks.
2535 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2536 to 16 in 22.1 to make this a lesser problem. */
2537 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2538 {
2539 const char *s = SDATA (it->overlay_strings[i]);
2540 const char *e = s + SBYTES (it->overlay_strings[i]);
2541
2542 while (s < e && *s != '\n')
2543 ++s;
2544
2545 if (s < e)
2546 {
2547 overlay_strings_with_newlines = 1;
2548 break;
2549 }
2550 }
2551
2552 /* If position is within an overlay string, set up IT to the right
2553 overlay string. */
2554 if (pos->overlay_string_index >= 0)
2555 {
2556 int relative_index;
2557
2558 /* If the first overlay string happens to have a `display'
2559 property for an image, the iterator will be set up for that
2560 image, and we have to undo that setup first before we can
2561 correct the overlay string index. */
2562 if (it->method == GET_FROM_IMAGE)
2563 pop_it (it);
2564
2565 /* We already have the first chunk of overlay strings in
2566 IT->overlay_strings. Load more until the one for
2567 pos->overlay_string_index is in IT->overlay_strings. */
2568 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2569 {
2570 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2571 it->current.overlay_string_index = 0;
2572 while (n--)
2573 {
2574 load_overlay_strings (it, 0);
2575 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2576 }
2577 }
2578
2579 it->current.overlay_string_index = pos->overlay_string_index;
2580 relative_index = (it->current.overlay_string_index
2581 % OVERLAY_STRING_CHUNK_SIZE);
2582 it->string = it->overlay_strings[relative_index];
2583 xassert (STRINGP (it->string));
2584 it->current.string_pos = pos->string_pos;
2585 it->method = GET_FROM_STRING;
2586 }
2587
2588 #if 0 /* This is bogus because POS not having an overlay string
2589 position does not mean it's after the string. Example: A
2590 line starting with a before-string and initialization of IT
2591 to the previous row's end position. */
2592 else if (it->current.overlay_string_index >= 0)
2593 {
2594 /* If POS says we're already after an overlay string ending at
2595 POS, make sure to pop the iterator because it will be in
2596 front of that overlay string. When POS is ZV, we've thereby
2597 also ``processed'' overlay strings at ZV. */
2598 while (it->sp)
2599 pop_it (it);
2600 it->current.overlay_string_index = -1;
2601 it->method = GET_FROM_BUFFER;
2602 if (CHARPOS (pos->pos) == ZV)
2603 it->overlay_strings_at_end_processed_p = 1;
2604 }
2605 #endif /* 0 */
2606
2607 if (CHARPOS (pos->string_pos) >= 0)
2608 {
2609 /* Recorded position is not in an overlay string, but in another
2610 string. This can only be a string from a `display' property.
2611 IT should already be filled with that string. */
2612 it->current.string_pos = pos->string_pos;
2613 xassert (STRINGP (it->string));
2614 }
2615
2616 /* Restore position in display vector translations, control
2617 character translations or ellipses. */
2618 if (pos->dpvec_index >= 0)
2619 {
2620 if (it->dpvec == NULL)
2621 get_next_display_element (it);
2622 xassert (it->dpvec && it->current.dpvec_index == 0);
2623 it->current.dpvec_index = pos->dpvec_index;
2624 }
2625
2626 CHECK_IT (it);
2627 return !overlay_strings_with_newlines;
2628 }
2629
2630
2631 /* Initialize IT for stepping through current_buffer in window W
2632 starting at ROW->start. */
2633
2634 static void
2635 init_to_row_start (it, w, row)
2636 struct it *it;
2637 struct window *w;
2638 struct glyph_row *row;
2639 {
2640 init_from_display_pos (it, w, &row->start);
2641 it->start = row->start;
2642 it->continuation_lines_width = row->continuation_lines_width;
2643 CHECK_IT (it);
2644 }
2645
2646
2647 /* Initialize IT for stepping through current_buffer in window W
2648 starting in the line following ROW, i.e. starting at ROW->end.
2649 Value is zero if there are overlay strings with newlines at ROW's
2650 end position. */
2651
2652 static int
2653 init_to_row_end (it, w, row)
2654 struct it *it;
2655 struct window *w;
2656 struct glyph_row *row;
2657 {
2658 int success = 0;
2659
2660 if (init_from_display_pos (it, w, &row->end))
2661 {
2662 if (row->continued_p)
2663 it->continuation_lines_width
2664 = row->continuation_lines_width + row->pixel_width;
2665 CHECK_IT (it);
2666 success = 1;
2667 }
2668
2669 return success;
2670 }
2671
2672
2673
2674 \f
2675 /***********************************************************************
2676 Text properties
2677 ***********************************************************************/
2678
2679 /* Called when IT reaches IT->stop_charpos. Handle text property and
2680 overlay changes. Set IT->stop_charpos to the next position where
2681 to stop. */
2682
2683 static void
2684 handle_stop (it)
2685 struct it *it;
2686 {
2687 enum prop_handled handled;
2688 int handle_overlay_change_p = 1;
2689 struct props *p;
2690
2691 it->dpvec = NULL;
2692 it->current.dpvec_index = -1;
2693
2694 /* Use face of preceding text for ellipsis (if invisible) */
2695 if (it->selective_display_ellipsis_p)
2696 it->saved_face_id = it->face_id;
2697
2698 do
2699 {
2700 handled = HANDLED_NORMALLY;
2701
2702 /* Call text property handlers. */
2703 for (p = it_props; p->handler; ++p)
2704 {
2705 handled = p->handler (it);
2706
2707 if (handled == HANDLED_RECOMPUTE_PROPS)
2708 break;
2709 else if (handled == HANDLED_RETURN)
2710 return;
2711 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2712 handle_overlay_change_p = 0;
2713 }
2714
2715 if (handled != HANDLED_RECOMPUTE_PROPS)
2716 {
2717 /* Don't check for overlay strings below when set to deliver
2718 characters from a display vector. */
2719 if (it->method == GET_FROM_DISPLAY_VECTOR)
2720 handle_overlay_change_p = 0;
2721
2722 /* Handle overlay changes. */
2723 if (handle_overlay_change_p)
2724 handled = handle_overlay_change (it);
2725
2726 /* Determine where to stop next. */
2727 if (handled == HANDLED_NORMALLY)
2728 compute_stop_pos (it);
2729 }
2730 }
2731 while (handled == HANDLED_RECOMPUTE_PROPS);
2732 }
2733
2734
2735 /* Compute IT->stop_charpos from text property and overlay change
2736 information for IT's current position. */
2737
2738 static void
2739 compute_stop_pos (it)
2740 struct it *it;
2741 {
2742 register INTERVAL iv, next_iv;
2743 Lisp_Object object, limit, position;
2744
2745 /* If nowhere else, stop at the end. */
2746 it->stop_charpos = it->end_charpos;
2747
2748 if (STRINGP (it->string))
2749 {
2750 /* Strings are usually short, so don't limit the search for
2751 properties. */
2752 object = it->string;
2753 limit = Qnil;
2754 position = make_number (IT_STRING_CHARPOS (*it));
2755 }
2756 else
2757 {
2758 int charpos;
2759
2760 /* If next overlay change is in front of the current stop pos
2761 (which is IT->end_charpos), stop there. Note: value of
2762 next_overlay_change is point-max if no overlay change
2763 follows. */
2764 charpos = next_overlay_change (IT_CHARPOS (*it));
2765 if (charpos < it->stop_charpos)
2766 it->stop_charpos = charpos;
2767
2768 /* If showing the region, we have to stop at the region
2769 start or end because the face might change there. */
2770 if (it->region_beg_charpos > 0)
2771 {
2772 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2773 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2774 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2775 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2776 }
2777
2778 /* Set up variables for computing the stop position from text
2779 property changes. */
2780 XSETBUFFER (object, current_buffer);
2781 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2782 position = make_number (IT_CHARPOS (*it));
2783
2784 }
2785
2786 /* Get the interval containing IT's position. Value is a null
2787 interval if there isn't such an interval. */
2788 iv = validate_interval_range (object, &position, &position, 0);
2789 if (!NULL_INTERVAL_P (iv))
2790 {
2791 Lisp_Object values_here[LAST_PROP_IDX];
2792 struct props *p;
2793
2794 /* Get properties here. */
2795 for (p = it_props; p->handler; ++p)
2796 values_here[p->idx] = textget (iv->plist, *p->name);
2797
2798 /* Look for an interval following iv that has different
2799 properties. */
2800 for (next_iv = next_interval (iv);
2801 (!NULL_INTERVAL_P (next_iv)
2802 && (NILP (limit)
2803 || XFASTINT (limit) > next_iv->position));
2804 next_iv = next_interval (next_iv))
2805 {
2806 for (p = it_props; p->handler; ++p)
2807 {
2808 Lisp_Object new_value;
2809
2810 new_value = textget (next_iv->plist, *p->name);
2811 if (!EQ (values_here[p->idx], new_value))
2812 break;
2813 }
2814
2815 if (p->handler)
2816 break;
2817 }
2818
2819 if (!NULL_INTERVAL_P (next_iv))
2820 {
2821 if (INTEGERP (limit)
2822 && next_iv->position >= XFASTINT (limit))
2823 /* No text property change up to limit. */
2824 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2825 else
2826 /* Text properties change in next_iv. */
2827 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2828 }
2829 }
2830
2831 xassert (STRINGP (it->string)
2832 || (it->stop_charpos >= BEGV
2833 && it->stop_charpos >= IT_CHARPOS (*it)));
2834 }
2835
2836
2837 /* Return the position of the next overlay change after POS in
2838 current_buffer. Value is point-max if no overlay change
2839 follows. This is like `next-overlay-change' but doesn't use
2840 xmalloc. */
2841
2842 static int
2843 next_overlay_change (pos)
2844 int pos;
2845 {
2846 int noverlays;
2847 int endpos;
2848 Lisp_Object *overlays;
2849 int i;
2850
2851 /* Get all overlays at the given position. */
2852 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2853
2854 /* If any of these overlays ends before endpos,
2855 use its ending point instead. */
2856 for (i = 0; i < noverlays; ++i)
2857 {
2858 Lisp_Object oend;
2859 int oendpos;
2860
2861 oend = OVERLAY_END (overlays[i]);
2862 oendpos = OVERLAY_POSITION (oend);
2863 endpos = min (endpos, oendpos);
2864 }
2865
2866 return endpos;
2867 }
2868
2869
2870 \f
2871 /***********************************************************************
2872 Fontification
2873 ***********************************************************************/
2874
2875 /* Handle changes in the `fontified' property of the current buffer by
2876 calling hook functions from Qfontification_functions to fontify
2877 regions of text. */
2878
2879 static enum prop_handled
2880 handle_fontified_prop (it)
2881 struct it *it;
2882 {
2883 Lisp_Object prop, pos;
2884 enum prop_handled handled = HANDLED_NORMALLY;
2885
2886 /* Get the value of the `fontified' property at IT's current buffer
2887 position. (The `fontified' property doesn't have a special
2888 meaning in strings.) If the value is nil, call functions from
2889 Qfontification_functions. */
2890 if (!STRINGP (it->string)
2891 && it->s == NULL
2892 && !NILP (Vfontification_functions)
2893 && !NILP (Vrun_hooks)
2894 && (pos = make_number (IT_CHARPOS (*it)),
2895 prop = Fget_char_property (pos, Qfontified, Qnil),
2896 NILP (prop)))
2897 {
2898 int count = SPECPDL_INDEX ();
2899 Lisp_Object val;
2900
2901 val = Vfontification_functions;
2902 specbind (Qfontification_functions, Qnil);
2903
2904 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2905 safe_call1 (val, pos);
2906 else
2907 {
2908 Lisp_Object globals, fn;
2909 struct gcpro gcpro1, gcpro2;
2910
2911 globals = Qnil;
2912 GCPRO2 (val, globals);
2913
2914 for (; CONSP (val); val = XCDR (val))
2915 {
2916 fn = XCAR (val);
2917
2918 if (EQ (fn, Qt))
2919 {
2920 /* A value of t indicates this hook has a local
2921 binding; it means to run the global binding too.
2922 In a global value, t should not occur. If it
2923 does, we must ignore it to avoid an endless
2924 loop. */
2925 for (globals = Fdefault_value (Qfontification_functions);
2926 CONSP (globals);
2927 globals = XCDR (globals))
2928 {
2929 fn = XCAR (globals);
2930 if (!EQ (fn, Qt))
2931 safe_call1 (fn, pos);
2932 }
2933 }
2934 else
2935 safe_call1 (fn, pos);
2936 }
2937
2938 UNGCPRO;
2939 }
2940
2941 unbind_to (count, Qnil);
2942
2943 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2944 something. This avoids an endless loop if they failed to
2945 fontify the text for which reason ever. */
2946 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2947 handled = HANDLED_RECOMPUTE_PROPS;
2948 }
2949
2950 return handled;
2951 }
2952
2953
2954 \f
2955 /***********************************************************************
2956 Faces
2957 ***********************************************************************/
2958
2959 /* Set up iterator IT from face properties at its current position.
2960 Called from handle_stop. */
2961
2962 static enum prop_handled
2963 handle_face_prop (it)
2964 struct it *it;
2965 {
2966 int new_face_id, next_stop;
2967
2968 if (!STRINGP (it->string))
2969 {
2970 new_face_id
2971 = face_at_buffer_position (it->w,
2972 IT_CHARPOS (*it),
2973 it->region_beg_charpos,
2974 it->region_end_charpos,
2975 &next_stop,
2976 (IT_CHARPOS (*it)
2977 + TEXT_PROP_DISTANCE_LIMIT),
2978 0);
2979
2980 /* Is this a start of a run of characters with box face?
2981 Caveat: this can be called for a freshly initialized
2982 iterator; face_id is -1 in this case. We know that the new
2983 face will not change until limit, i.e. if the new face has a
2984 box, all characters up to limit will have one. But, as
2985 usual, we don't know whether limit is really the end. */
2986 if (new_face_id != it->face_id)
2987 {
2988 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2989
2990 /* If new face has a box but old face has not, this is
2991 the start of a run of characters with box, i.e. it has
2992 a shadow on the left side. The value of face_id of the
2993 iterator will be -1 if this is the initial call that gets
2994 the face. In this case, we have to look in front of IT's
2995 position and see whether there is a face != new_face_id. */
2996 it->start_of_box_run_p
2997 = (new_face->box != FACE_NO_BOX
2998 && (it->face_id >= 0
2999 || IT_CHARPOS (*it) == BEG
3000 || new_face_id != face_before_it_pos (it)));
3001 it->face_box_p = new_face->box != FACE_NO_BOX;
3002 }
3003 }
3004 else
3005 {
3006 int base_face_id, bufpos;
3007
3008 if (it->current.overlay_string_index >= 0)
3009 bufpos = IT_CHARPOS (*it);
3010 else
3011 bufpos = 0;
3012
3013 /* For strings from a buffer, i.e. overlay strings or strings
3014 from a `display' property, use the face at IT's current
3015 buffer position as the base face to merge with, so that
3016 overlay strings appear in the same face as surrounding
3017 text, unless they specify their own faces. */
3018 base_face_id = underlying_face_id (it);
3019
3020 new_face_id = face_at_string_position (it->w,
3021 it->string,
3022 IT_STRING_CHARPOS (*it),
3023 bufpos,
3024 it->region_beg_charpos,
3025 it->region_end_charpos,
3026 &next_stop,
3027 base_face_id, 0);
3028
3029 #if 0 /* This shouldn't be neccessary. Let's check it. */
3030 /* If IT is used to display a mode line we would really like to
3031 use the mode line face instead of the frame's default face. */
3032 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3033 && new_face_id == DEFAULT_FACE_ID)
3034 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3035 #endif
3036
3037 /* Is this a start of a run of characters with box? Caveat:
3038 this can be called for a freshly allocated iterator; face_id
3039 is -1 is this case. We know that the new face will not
3040 change until the next check pos, i.e. if the new face has a
3041 box, all characters up to that position will have a
3042 box. But, as usual, we don't know whether that position
3043 is really the end. */
3044 if (new_face_id != it->face_id)
3045 {
3046 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3047 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3048
3049 /* If new face has a box but old face hasn't, this is the
3050 start of a run of characters with box, i.e. it has a
3051 shadow on the left side. */
3052 it->start_of_box_run_p
3053 = new_face->box && (old_face == NULL || !old_face->box);
3054 it->face_box_p = new_face->box != FACE_NO_BOX;
3055 }
3056 }
3057
3058 it->face_id = new_face_id;
3059 return HANDLED_NORMALLY;
3060 }
3061
3062
3063 /* Return the ID of the face ``underlying'' IT's current position,
3064 which is in a string. If the iterator is associated with a
3065 buffer, return the face at IT's current buffer position.
3066 Otherwise, use the iterator's base_face_id. */
3067
3068 static int
3069 underlying_face_id (it)
3070 struct it *it;
3071 {
3072 int face_id = it->base_face_id, i;
3073
3074 xassert (STRINGP (it->string));
3075
3076 for (i = it->sp - 1; i >= 0; --i)
3077 if (NILP (it->stack[i].string))
3078 face_id = it->stack[i].face_id;
3079
3080 return face_id;
3081 }
3082
3083
3084 /* Compute the face one character before or after the current position
3085 of IT. BEFORE_P non-zero means get the face in front of IT's
3086 position. Value is the id of the face. */
3087
3088 static int
3089 face_before_or_after_it_pos (it, before_p)
3090 struct it *it;
3091 int before_p;
3092 {
3093 int face_id, limit;
3094 int next_check_charpos;
3095 struct text_pos pos;
3096
3097 xassert (it->s == NULL);
3098
3099 if (STRINGP (it->string))
3100 {
3101 int bufpos, base_face_id;
3102
3103 /* No face change past the end of the string (for the case
3104 we are padding with spaces). No face change before the
3105 string start. */
3106 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3107 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3108 return it->face_id;
3109
3110 /* Set pos to the position before or after IT's current position. */
3111 if (before_p)
3112 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3113 else
3114 /* For composition, we must check the character after the
3115 composition. */
3116 pos = (it->what == IT_COMPOSITION
3117 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3118 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3119
3120 if (it->current.overlay_string_index >= 0)
3121 bufpos = IT_CHARPOS (*it);
3122 else
3123 bufpos = 0;
3124
3125 base_face_id = underlying_face_id (it);
3126
3127 /* Get the face for ASCII, or unibyte. */
3128 face_id = face_at_string_position (it->w,
3129 it->string,
3130 CHARPOS (pos),
3131 bufpos,
3132 it->region_beg_charpos,
3133 it->region_end_charpos,
3134 &next_check_charpos,
3135 base_face_id, 0);
3136
3137 /* Correct the face for charsets different from ASCII. Do it
3138 for the multibyte case only. The face returned above is
3139 suitable for unibyte text if IT->string is unibyte. */
3140 if (STRING_MULTIBYTE (it->string))
3141 {
3142 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3143 int rest = SBYTES (it->string) - BYTEPOS (pos);
3144 int c, len;
3145 struct face *face = FACE_FROM_ID (it->f, face_id);
3146
3147 c = string_char_and_length (p, rest, &len);
3148 face_id = FACE_FOR_CHAR (it->f, face, c);
3149 }
3150 }
3151 else
3152 {
3153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3154 || (IT_CHARPOS (*it) <= BEGV && before_p))
3155 return it->face_id;
3156
3157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3158 pos = it->current.pos;
3159
3160 if (before_p)
3161 DEC_TEXT_POS (pos, it->multibyte_p);
3162 else
3163 {
3164 if (it->what == IT_COMPOSITION)
3165 /* For composition, we must check the position after the
3166 composition. */
3167 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3168 else
3169 INC_TEXT_POS (pos, it->multibyte_p);
3170 }
3171
3172 /* Determine face for CHARSET_ASCII, or unibyte. */
3173 face_id = face_at_buffer_position (it->w,
3174 CHARPOS (pos),
3175 it->region_beg_charpos,
3176 it->region_end_charpos,
3177 &next_check_charpos,
3178 limit, 0);
3179
3180 /* Correct the face for charsets different from ASCII. Do it
3181 for the multibyte case only. The face returned above is
3182 suitable for unibyte text if current_buffer is unibyte. */
3183 if (it->multibyte_p)
3184 {
3185 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3186 struct face *face = FACE_FROM_ID (it->f, face_id);
3187 face_id = FACE_FOR_CHAR (it->f, face, c);
3188 }
3189 }
3190
3191 return face_id;
3192 }
3193
3194
3195 \f
3196 /***********************************************************************
3197 Invisible text
3198 ***********************************************************************/
3199
3200 /* Set up iterator IT from invisible properties at its current
3201 position. Called from handle_stop. */
3202
3203 static enum prop_handled
3204 handle_invisible_prop (it)
3205 struct it *it;
3206 {
3207 enum prop_handled handled = HANDLED_NORMALLY;
3208
3209 if (STRINGP (it->string))
3210 {
3211 extern Lisp_Object Qinvisible;
3212 Lisp_Object prop, end_charpos, limit, charpos;
3213
3214 /* Get the value of the invisible text property at the
3215 current position. Value will be nil if there is no such
3216 property. */
3217 charpos = make_number (IT_STRING_CHARPOS (*it));
3218 prop = Fget_text_property (charpos, Qinvisible, it->string);
3219
3220 if (!NILP (prop)
3221 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3222 {
3223 handled = HANDLED_RECOMPUTE_PROPS;
3224
3225 /* Get the position at which the next change of the
3226 invisible text property can be found in IT->string.
3227 Value will be nil if the property value is the same for
3228 all the rest of IT->string. */
3229 XSETINT (limit, SCHARS (it->string));
3230 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3231 it->string, limit);
3232
3233 /* Text at current position is invisible. The next
3234 change in the property is at position end_charpos.
3235 Move IT's current position to that position. */
3236 if (INTEGERP (end_charpos)
3237 && XFASTINT (end_charpos) < XFASTINT (limit))
3238 {
3239 struct text_pos old;
3240 old = it->current.string_pos;
3241 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3242 compute_string_pos (&it->current.string_pos, old, it->string);
3243 }
3244 else
3245 {
3246 /* The rest of the string is invisible. If this is an
3247 overlay string, proceed with the next overlay string
3248 or whatever comes and return a character from there. */
3249 if (it->current.overlay_string_index >= 0)
3250 {
3251 next_overlay_string (it);
3252 /* Don't check for overlay strings when we just
3253 finished processing them. */
3254 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3255 }
3256 else
3257 {
3258 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3259 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3260 }
3261 }
3262 }
3263 }
3264 else
3265 {
3266 int invis_p, newpos, next_stop, start_charpos;
3267 Lisp_Object pos, prop, overlay;
3268
3269 /* First of all, is there invisible text at this position? */
3270 start_charpos = IT_CHARPOS (*it);
3271 pos = make_number (IT_CHARPOS (*it));
3272 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3273 &overlay);
3274 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3275
3276 /* If we are on invisible text, skip over it. */
3277 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3278 {
3279 /* Record whether we have to display an ellipsis for the
3280 invisible text. */
3281 int display_ellipsis_p = invis_p == 2;
3282
3283 handled = HANDLED_RECOMPUTE_PROPS;
3284
3285 /* Loop skipping over invisible text. The loop is left at
3286 ZV or with IT on the first char being visible again. */
3287 do
3288 {
3289 /* Try to skip some invisible text. Return value is the
3290 position reached which can be equal to IT's position
3291 if there is nothing invisible here. This skips both
3292 over invisible text properties and overlays with
3293 invisible property. */
3294 newpos = skip_invisible (IT_CHARPOS (*it),
3295 &next_stop, ZV, it->window);
3296
3297 /* If we skipped nothing at all we weren't at invisible
3298 text in the first place. If everything to the end of
3299 the buffer was skipped, end the loop. */
3300 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3301 invis_p = 0;
3302 else
3303 {
3304 /* We skipped some characters but not necessarily
3305 all there are. Check if we ended up on visible
3306 text. Fget_char_property returns the property of
3307 the char before the given position, i.e. if we
3308 get invis_p = 0, this means that the char at
3309 newpos is visible. */
3310 pos = make_number (newpos);
3311 prop = Fget_char_property (pos, Qinvisible, it->window);
3312 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3313 }
3314
3315 /* If we ended up on invisible text, proceed to
3316 skip starting with next_stop. */
3317 if (invis_p)
3318 IT_CHARPOS (*it) = next_stop;
3319 }
3320 while (invis_p);
3321
3322 /* The position newpos is now either ZV or on visible text. */
3323 IT_CHARPOS (*it) = newpos;
3324 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3325
3326 /* If there are before-strings at the start of invisible
3327 text, and the text is invisible because of a text
3328 property, arrange to show before-strings because 20.x did
3329 it that way. (If the text is invisible because of an
3330 overlay property instead of a text property, this is
3331 already handled in the overlay code.) */
3332 if (NILP (overlay)
3333 && get_overlay_strings (it, start_charpos))
3334 {
3335 handled = HANDLED_RECOMPUTE_PROPS;
3336 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3337 }
3338 else if (display_ellipsis_p)
3339 setup_for_ellipsis (it, 0);
3340 }
3341 }
3342
3343 return handled;
3344 }
3345
3346
3347 /* Make iterator IT return `...' next.
3348 Replaces LEN characters from buffer. */
3349
3350 static void
3351 setup_for_ellipsis (it, len)
3352 struct it *it;
3353 int len;
3354 {
3355 /* Use the display table definition for `...'. Invalid glyphs
3356 will be handled by the method returning elements from dpvec. */
3357 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3358 {
3359 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3360 it->dpvec = v->contents;
3361 it->dpend = v->contents + v->size;
3362 }
3363 else
3364 {
3365 /* Default `...'. */
3366 it->dpvec = default_invis_vector;
3367 it->dpend = default_invis_vector + 3;
3368 }
3369
3370 it->dpvec_char_len = len;
3371 it->current.dpvec_index = 0;
3372 it->dpvec_face_id = -1;
3373
3374 /* Remember the current face id in case glyphs specify faces.
3375 IT's face is restored in set_iterator_to_next.
3376 saved_face_id was set to preceding char's face in handle_stop. */
3377 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3378 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3379
3380 it->method = GET_FROM_DISPLAY_VECTOR;
3381 it->ellipsis_p = 1;
3382 }
3383
3384
3385 \f
3386 /***********************************************************************
3387 'display' property
3388 ***********************************************************************/
3389
3390 /* Set up iterator IT from `display' property at its current position.
3391 Called from handle_stop.
3392 We return HANDLED_RETURN if some part of the display property
3393 overrides the display of the buffer text itself.
3394 Otherwise we return HANDLED_NORMALLY. */
3395
3396 static enum prop_handled
3397 handle_display_prop (it)
3398 struct it *it;
3399 {
3400 Lisp_Object prop, object;
3401 struct text_pos *position;
3402 /* Nonzero if some property replaces the display of the text itself. */
3403 int display_replaced_p = 0;
3404
3405 if (STRINGP (it->string))
3406 {
3407 object = it->string;
3408 position = &it->current.string_pos;
3409 }
3410 else
3411 {
3412 object = it->w->buffer;
3413 position = &it->current.pos;
3414 }
3415
3416 /* Reset those iterator values set from display property values. */
3417 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3418 it->space_width = Qnil;
3419 it->font_height = Qnil;
3420 it->voffset = 0;
3421
3422 /* We don't support recursive `display' properties, i.e. string
3423 values that have a string `display' property, that have a string
3424 `display' property etc. */
3425 if (!it->string_from_display_prop_p)
3426 it->area = TEXT_AREA;
3427
3428 prop = Fget_char_property (make_number (position->charpos),
3429 Qdisplay, object);
3430 if (NILP (prop))
3431 return HANDLED_NORMALLY;
3432
3433 if (CONSP (prop)
3434 /* Simple properties. */
3435 && !EQ (XCAR (prop), Qimage)
3436 && !EQ (XCAR (prop), Qspace)
3437 && !EQ (XCAR (prop), Qwhen)
3438 && !EQ (XCAR (prop), Qslice)
3439 && !EQ (XCAR (prop), Qspace_width)
3440 && !EQ (XCAR (prop), Qheight)
3441 && !EQ (XCAR (prop), Qraise)
3442 /* Marginal area specifications. */
3443 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3444 && !EQ (XCAR (prop), Qleft_fringe)
3445 && !EQ (XCAR (prop), Qright_fringe)
3446 && !NILP (XCAR (prop)))
3447 {
3448 for (; CONSP (prop); prop = XCDR (prop))
3449 {
3450 if (handle_single_display_spec (it, XCAR (prop), object,
3451 position, display_replaced_p))
3452 display_replaced_p = 1;
3453 }
3454 }
3455 else if (VECTORP (prop))
3456 {
3457 int i;
3458 for (i = 0; i < ASIZE (prop); ++i)
3459 if (handle_single_display_spec (it, AREF (prop, i), object,
3460 position, display_replaced_p))
3461 display_replaced_p = 1;
3462 }
3463 else
3464 {
3465 int ret = handle_single_display_spec (it, prop, object, position, 0);
3466 if (ret < 0) /* Replaced by "", i.e. nothing. */
3467 return HANDLED_RECOMPUTE_PROPS;
3468 if (ret)
3469 display_replaced_p = 1;
3470 }
3471
3472 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3473 }
3474
3475
3476 /* Value is the position of the end of the `display' property starting
3477 at START_POS in OBJECT. */
3478
3479 static struct text_pos
3480 display_prop_end (it, object, start_pos)
3481 struct it *it;
3482 Lisp_Object object;
3483 struct text_pos start_pos;
3484 {
3485 Lisp_Object end;
3486 struct text_pos end_pos;
3487
3488 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3489 Qdisplay, object, Qnil);
3490 CHARPOS (end_pos) = XFASTINT (end);
3491 if (STRINGP (object))
3492 compute_string_pos (&end_pos, start_pos, it->string);
3493 else
3494 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3495
3496 return end_pos;
3497 }
3498
3499
3500 /* Set up IT from a single `display' specification PROP. OBJECT
3501 is the object in which the `display' property was found. *POSITION
3502 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3503 means that we previously saw a display specification which already
3504 replaced text display with something else, for example an image;
3505 we ignore such properties after the first one has been processed.
3506
3507 If PROP is a `space' or `image' specification, and in some other
3508 cases too, set *POSITION to the position where the `display'
3509 property ends.
3510
3511 Value is non-zero if something was found which replaces the display
3512 of buffer or string text. Specifically, the value is -1 if that
3513 "something" is "nothing". */
3514
3515 static int
3516 handle_single_display_spec (it, spec, object, position,
3517 display_replaced_before_p)
3518 struct it *it;
3519 Lisp_Object spec;
3520 Lisp_Object object;
3521 struct text_pos *position;
3522 int display_replaced_before_p;
3523 {
3524 Lisp_Object form;
3525 Lisp_Object location, value;
3526 struct text_pos start_pos;
3527 int valid_p;
3528
3529 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3530 If the result is non-nil, use VALUE instead of SPEC. */
3531 form = Qt;
3532 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3533 {
3534 spec = XCDR (spec);
3535 if (!CONSP (spec))
3536 return 0;
3537 form = XCAR (spec);
3538 spec = XCDR (spec);
3539 }
3540
3541 if (!NILP (form) && !EQ (form, Qt))
3542 {
3543 int count = SPECPDL_INDEX ();
3544 struct gcpro gcpro1;
3545
3546 /* Bind `object' to the object having the `display' property, a
3547 buffer or string. Bind `position' to the position in the
3548 object where the property was found, and `buffer-position'
3549 to the current position in the buffer. */
3550 specbind (Qobject, object);
3551 specbind (Qposition, make_number (CHARPOS (*position)));
3552 specbind (Qbuffer_position,
3553 make_number (STRINGP (object)
3554 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3555 GCPRO1 (form);
3556 form = safe_eval (form);
3557 UNGCPRO;
3558 unbind_to (count, Qnil);
3559 }
3560
3561 if (NILP (form))
3562 return 0;
3563
3564 /* Handle `(height HEIGHT)' specifications. */
3565 if (CONSP (spec)
3566 && EQ (XCAR (spec), Qheight)
3567 && CONSP (XCDR (spec)))
3568 {
3569 if (!FRAME_WINDOW_P (it->f))
3570 return 0;
3571
3572 it->font_height = XCAR (XCDR (spec));
3573 if (!NILP (it->font_height))
3574 {
3575 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3576 int new_height = -1;
3577
3578 if (CONSP (it->font_height)
3579 && (EQ (XCAR (it->font_height), Qplus)
3580 || EQ (XCAR (it->font_height), Qminus))
3581 && CONSP (XCDR (it->font_height))
3582 && INTEGERP (XCAR (XCDR (it->font_height))))
3583 {
3584 /* `(+ N)' or `(- N)' where N is an integer. */
3585 int steps = XINT (XCAR (XCDR (it->font_height)));
3586 if (EQ (XCAR (it->font_height), Qplus))
3587 steps = - steps;
3588 it->face_id = smaller_face (it->f, it->face_id, steps);
3589 }
3590 else if (FUNCTIONP (it->font_height))
3591 {
3592 /* Call function with current height as argument.
3593 Value is the new height. */
3594 Lisp_Object height;
3595 height = safe_call1 (it->font_height,
3596 face->lface[LFACE_HEIGHT_INDEX]);
3597 if (NUMBERP (height))
3598 new_height = XFLOATINT (height);
3599 }
3600 else if (NUMBERP (it->font_height))
3601 {
3602 /* Value is a multiple of the canonical char height. */
3603 struct face *face;
3604
3605 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3606 new_height = (XFLOATINT (it->font_height)
3607 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3608 }
3609 else
3610 {
3611 /* Evaluate IT->font_height with `height' bound to the
3612 current specified height to get the new height. */
3613 int count = SPECPDL_INDEX ();
3614
3615 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3616 value = safe_eval (it->font_height);
3617 unbind_to (count, Qnil);
3618
3619 if (NUMBERP (value))
3620 new_height = XFLOATINT (value);
3621 }
3622
3623 if (new_height > 0)
3624 it->face_id = face_with_height (it->f, it->face_id, new_height);
3625 }
3626
3627 return 0;
3628 }
3629
3630 /* Handle `(space_width WIDTH)'. */
3631 if (CONSP (spec)
3632 && EQ (XCAR (spec), Qspace_width)
3633 && CONSP (XCDR (spec)))
3634 {
3635 if (!FRAME_WINDOW_P (it->f))
3636 return 0;
3637
3638 value = XCAR (XCDR (spec));
3639 if (NUMBERP (value) && XFLOATINT (value) > 0)
3640 it->space_width = value;
3641
3642 return 0;
3643 }
3644
3645 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3646 if (CONSP (spec)
3647 && EQ (XCAR (spec), Qslice))
3648 {
3649 Lisp_Object tem;
3650
3651 if (!FRAME_WINDOW_P (it->f))
3652 return 0;
3653
3654 if (tem = XCDR (spec), CONSP (tem))
3655 {
3656 it->slice.x = XCAR (tem);
3657 if (tem = XCDR (tem), CONSP (tem))
3658 {
3659 it->slice.y = XCAR (tem);
3660 if (tem = XCDR (tem), CONSP (tem))
3661 {
3662 it->slice.width = XCAR (tem);
3663 if (tem = XCDR (tem), CONSP (tem))
3664 it->slice.height = XCAR (tem);
3665 }
3666 }
3667 }
3668
3669 return 0;
3670 }
3671
3672 /* Handle `(raise FACTOR)'. */
3673 if (CONSP (spec)
3674 && EQ (XCAR (spec), Qraise)
3675 && CONSP (XCDR (spec)))
3676 {
3677 if (!FRAME_WINDOW_P (it->f))
3678 return 0;
3679
3680 #ifdef HAVE_WINDOW_SYSTEM
3681 value = XCAR (XCDR (spec));
3682 if (NUMBERP (value))
3683 {
3684 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3685 it->voffset = - (XFLOATINT (value)
3686 * (FONT_HEIGHT (face->font)));
3687 }
3688 #endif /* HAVE_WINDOW_SYSTEM */
3689
3690 return 0;
3691 }
3692
3693 /* Don't handle the other kinds of display specifications
3694 inside a string that we got from a `display' property. */
3695 if (it->string_from_display_prop_p)
3696 return 0;
3697
3698 /* Characters having this form of property are not displayed, so
3699 we have to find the end of the property. */
3700 start_pos = *position;
3701 *position = display_prop_end (it, object, start_pos);
3702 value = Qnil;
3703
3704 /* Stop the scan at that end position--we assume that all
3705 text properties change there. */
3706 it->stop_charpos = position->charpos;
3707
3708 /* Handle `(left-fringe BITMAP [FACE])'
3709 and `(right-fringe BITMAP [FACE])'. */
3710 if (CONSP (spec)
3711 && (EQ (XCAR (spec), Qleft_fringe)
3712 || EQ (XCAR (spec), Qright_fringe))
3713 && CONSP (XCDR (spec)))
3714 {
3715 int face_id = DEFAULT_FACE_ID;
3716 int fringe_bitmap;
3717
3718 if (!FRAME_WINDOW_P (it->f))
3719 /* If we return here, POSITION has been advanced
3720 across the text with this property. */
3721 return 0;
3722
3723 #ifdef HAVE_WINDOW_SYSTEM
3724 value = XCAR (XCDR (spec));
3725 if (!SYMBOLP (value)
3726 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3727 /* If we return here, POSITION has been advanced
3728 across the text with this property. */
3729 return 0;
3730
3731 if (CONSP (XCDR (XCDR (spec))))
3732 {
3733 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3734 int face_id2 = lookup_derived_face (it->f, face_name,
3735 'A', FRINGE_FACE_ID, 0);
3736 if (face_id2 >= 0)
3737 face_id = face_id2;
3738 }
3739
3740 /* Save current settings of IT so that we can restore them
3741 when we are finished with the glyph property value. */
3742
3743 push_it (it);
3744
3745 it->area = TEXT_AREA;
3746 it->what = IT_IMAGE;
3747 it->image_id = -1; /* no image */
3748 it->position = start_pos;
3749 it->object = NILP (object) ? it->w->buffer : object;
3750 it->method = GET_FROM_IMAGE;
3751 it->face_id = face_id;
3752
3753 /* Say that we haven't consumed the characters with
3754 `display' property yet. The call to pop_it in
3755 set_iterator_to_next will clean this up. */
3756 *position = start_pos;
3757
3758 if (EQ (XCAR (spec), Qleft_fringe))
3759 {
3760 it->left_user_fringe_bitmap = fringe_bitmap;
3761 it->left_user_fringe_face_id = face_id;
3762 }
3763 else
3764 {
3765 it->right_user_fringe_bitmap = fringe_bitmap;
3766 it->right_user_fringe_face_id = face_id;
3767 }
3768 #endif /* HAVE_WINDOW_SYSTEM */
3769 return 1;
3770 }
3771
3772 /* Prepare to handle `((margin left-margin) ...)',
3773 `((margin right-margin) ...)' and `((margin nil) ...)'
3774 prefixes for display specifications. */
3775 location = Qunbound;
3776 if (CONSP (spec) && CONSP (XCAR (spec)))
3777 {
3778 Lisp_Object tem;
3779
3780 value = XCDR (spec);
3781 if (CONSP (value))
3782 value = XCAR (value);
3783
3784 tem = XCAR (spec);
3785 if (EQ (XCAR (tem), Qmargin)
3786 && (tem = XCDR (tem),
3787 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3788 (NILP (tem)
3789 || EQ (tem, Qleft_margin)
3790 || EQ (tem, Qright_margin))))
3791 location = tem;
3792 }
3793
3794 if (EQ (location, Qunbound))
3795 {
3796 location = Qnil;
3797 value = spec;
3798 }
3799
3800 /* After this point, VALUE is the property after any
3801 margin prefix has been stripped. It must be a string,
3802 an image specification, or `(space ...)'.
3803
3804 LOCATION specifies where to display: `left-margin',
3805 `right-margin' or nil. */
3806
3807 valid_p = (STRINGP (value)
3808 #ifdef HAVE_WINDOW_SYSTEM
3809 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
3810 #endif /* not HAVE_WINDOW_SYSTEM */
3811 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3812
3813 if (valid_p && !display_replaced_before_p)
3814 {
3815 /* Save current settings of IT so that we can restore them
3816 when we are finished with the glyph property value. */
3817 push_it (it);
3818 if (NILP (location))
3819 it->area = TEXT_AREA;
3820 else if (EQ (location, Qleft_margin))
3821 it->area = LEFT_MARGIN_AREA;
3822 else
3823 it->area = RIGHT_MARGIN_AREA;
3824
3825 if (STRINGP (value))
3826 {
3827 if (SCHARS (value) == 0)
3828 {
3829 pop_it (it);
3830 return -1; /* Replaced by "", i.e. nothing. */
3831 }
3832 it->string = value;
3833 it->multibyte_p = STRING_MULTIBYTE (it->string);
3834 it->current.overlay_string_index = -1;
3835 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3836 it->end_charpos = it->string_nchars = SCHARS (it->string);
3837 it->method = GET_FROM_STRING;
3838 it->stop_charpos = 0;
3839 it->string_from_display_prop_p = 1;
3840 /* Say that we haven't consumed the characters with
3841 `display' property yet. The call to pop_it in
3842 set_iterator_to_next will clean this up. */
3843 *position = start_pos;
3844 }
3845 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3846 {
3847 it->method = GET_FROM_STRETCH;
3848 it->object = value;
3849 it->current.pos = it->position = start_pos;
3850
3851 }
3852 #ifdef HAVE_WINDOW_SYSTEM
3853 else
3854 {
3855 it->what = IT_IMAGE;
3856 it->image_id = lookup_image (it->f, value);
3857 it->position = start_pos;
3858 it->object = NILP (object) ? it->w->buffer : object;
3859 it->method = GET_FROM_IMAGE;
3860
3861 /* Say that we haven't consumed the characters with
3862 `display' property yet. The call to pop_it in
3863 set_iterator_to_next will clean this up. */
3864 *position = start_pos;
3865 }
3866 #endif /* HAVE_WINDOW_SYSTEM */
3867
3868 return 1;
3869 }
3870
3871 /* Invalid property or property not supported. Restore
3872 POSITION to what it was before. */
3873 *position = start_pos;
3874 return 0;
3875 }
3876
3877
3878 /* Check if SPEC is a display sub-property value whose text should be
3879 treated as intangible. */
3880
3881 static int
3882 single_display_spec_intangible_p (prop)
3883 Lisp_Object prop;
3884 {
3885 /* Skip over `when FORM'. */
3886 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3887 {
3888 prop = XCDR (prop);
3889 if (!CONSP (prop))
3890 return 0;
3891 prop = XCDR (prop);
3892 }
3893
3894 if (STRINGP (prop))
3895 return 1;
3896
3897 if (!CONSP (prop))
3898 return 0;
3899
3900 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3901 we don't need to treat text as intangible. */
3902 if (EQ (XCAR (prop), Qmargin))
3903 {
3904 prop = XCDR (prop);
3905 if (!CONSP (prop))
3906 return 0;
3907
3908 prop = XCDR (prop);
3909 if (!CONSP (prop)
3910 || EQ (XCAR (prop), Qleft_margin)
3911 || EQ (XCAR (prop), Qright_margin))
3912 return 0;
3913 }
3914
3915 return (CONSP (prop)
3916 && (EQ (XCAR (prop), Qimage)
3917 || EQ (XCAR (prop), Qspace)));
3918 }
3919
3920
3921 /* Check if PROP is a display property value whose text should be
3922 treated as intangible. */
3923
3924 int
3925 display_prop_intangible_p (prop)
3926 Lisp_Object prop;
3927 {
3928 if (CONSP (prop)
3929 && CONSP (XCAR (prop))
3930 && !EQ (Qmargin, XCAR (XCAR (prop))))
3931 {
3932 /* A list of sub-properties. */
3933 while (CONSP (prop))
3934 {
3935 if (single_display_spec_intangible_p (XCAR (prop)))
3936 return 1;
3937 prop = XCDR (prop);
3938 }
3939 }
3940 else if (VECTORP (prop))
3941 {
3942 /* A vector of sub-properties. */
3943 int i;
3944 for (i = 0; i < ASIZE (prop); ++i)
3945 if (single_display_spec_intangible_p (AREF (prop, i)))
3946 return 1;
3947 }
3948 else
3949 return single_display_spec_intangible_p (prop);
3950
3951 return 0;
3952 }
3953
3954
3955 /* Return 1 if PROP is a display sub-property value containing STRING. */
3956
3957 static int
3958 single_display_spec_string_p (prop, string)
3959 Lisp_Object prop, string;
3960 {
3961 if (EQ (string, prop))
3962 return 1;
3963
3964 /* Skip over `when FORM'. */
3965 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3966 {
3967 prop = XCDR (prop);
3968 if (!CONSP (prop))
3969 return 0;
3970 prop = XCDR (prop);
3971 }
3972
3973 if (CONSP (prop))
3974 /* Skip over `margin LOCATION'. */
3975 if (EQ (XCAR (prop), Qmargin))
3976 {
3977 prop = XCDR (prop);
3978 if (!CONSP (prop))
3979 return 0;
3980
3981 prop = XCDR (prop);
3982 if (!CONSP (prop))
3983 return 0;
3984 }
3985
3986 return CONSP (prop) && EQ (XCAR (prop), string);
3987 }
3988
3989
3990 /* Return 1 if STRING appears in the `display' property PROP. */
3991
3992 static int
3993 display_prop_string_p (prop, string)
3994 Lisp_Object prop, string;
3995 {
3996 if (CONSP (prop)
3997 && CONSP (XCAR (prop))
3998 && !EQ (Qmargin, XCAR (XCAR (prop))))
3999 {
4000 /* A list of sub-properties. */
4001 while (CONSP (prop))
4002 {
4003 if (single_display_spec_string_p (XCAR (prop), string))
4004 return 1;
4005 prop = XCDR (prop);
4006 }
4007 }
4008 else if (VECTORP (prop))
4009 {
4010 /* A vector of sub-properties. */
4011 int i;
4012 for (i = 0; i < ASIZE (prop); ++i)
4013 if (single_display_spec_string_p (AREF (prop, i), string))
4014 return 1;
4015 }
4016 else
4017 return single_display_spec_string_p (prop, string);
4018
4019 return 0;
4020 }
4021
4022
4023 /* Determine from which buffer position in W's buffer STRING comes
4024 from. AROUND_CHARPOS is an approximate position where it could
4025 be from. Value is the buffer position or 0 if it couldn't be
4026 determined.
4027
4028 W's buffer must be current.
4029
4030 This function is necessary because we don't record buffer positions
4031 in glyphs generated from strings (to keep struct glyph small).
4032 This function may only use code that doesn't eval because it is
4033 called asynchronously from note_mouse_highlight. */
4034
4035 int
4036 string_buffer_position (w, string, around_charpos)
4037 struct window *w;
4038 Lisp_Object string;
4039 int around_charpos;
4040 {
4041 Lisp_Object limit, prop, pos;
4042 const int MAX_DISTANCE = 1000;
4043 int found = 0;
4044
4045 pos = make_number (around_charpos);
4046 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4047 while (!found && !EQ (pos, limit))
4048 {
4049 prop = Fget_char_property (pos, Qdisplay, Qnil);
4050 if (!NILP (prop) && display_prop_string_p (prop, string))
4051 found = 1;
4052 else
4053 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4054 }
4055
4056 if (!found)
4057 {
4058 pos = make_number (around_charpos);
4059 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4060 while (!found && !EQ (pos, limit))
4061 {
4062 prop = Fget_char_property (pos, Qdisplay, Qnil);
4063 if (!NILP (prop) && display_prop_string_p (prop, string))
4064 found = 1;
4065 else
4066 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4067 limit);
4068 }
4069 }
4070
4071 return found ? XINT (pos) : 0;
4072 }
4073
4074
4075 \f
4076 /***********************************************************************
4077 `composition' property
4078 ***********************************************************************/
4079
4080 /* Set up iterator IT from `composition' property at its current
4081 position. Called from handle_stop. */
4082
4083 static enum prop_handled
4084 handle_composition_prop (it)
4085 struct it *it;
4086 {
4087 Lisp_Object prop, string;
4088 int pos, pos_byte, end;
4089 enum prop_handled handled = HANDLED_NORMALLY;
4090
4091 if (STRINGP (it->string))
4092 {
4093 pos = IT_STRING_CHARPOS (*it);
4094 pos_byte = IT_STRING_BYTEPOS (*it);
4095 string = it->string;
4096 }
4097 else
4098 {
4099 pos = IT_CHARPOS (*it);
4100 pos_byte = IT_BYTEPOS (*it);
4101 string = Qnil;
4102 }
4103
4104 /* If there's a valid composition and point is not inside of the
4105 composition (in the case that the composition is from the current
4106 buffer), draw a glyph composed from the composition components. */
4107 if (find_composition (pos, -1, &pos, &end, &prop, string)
4108 && COMPOSITION_VALID_P (pos, end, prop)
4109 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4110 {
4111 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4112
4113 if (id >= 0)
4114 {
4115 it->method = GET_FROM_COMPOSITION;
4116 it->cmp_id = id;
4117 it->cmp_len = COMPOSITION_LENGTH (prop);
4118 /* For a terminal, draw only the first character of the
4119 components. */
4120 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4121 it->len = (STRINGP (it->string)
4122 ? string_char_to_byte (it->string, end)
4123 : CHAR_TO_BYTE (end)) - pos_byte;
4124 it->stop_charpos = end;
4125 handled = HANDLED_RETURN;
4126 }
4127 }
4128
4129 return handled;
4130 }
4131
4132
4133 \f
4134 /***********************************************************************
4135 Overlay strings
4136 ***********************************************************************/
4137
4138 /* The following structure is used to record overlay strings for
4139 later sorting in load_overlay_strings. */
4140
4141 struct overlay_entry
4142 {
4143 Lisp_Object overlay;
4144 Lisp_Object string;
4145 int priority;
4146 int after_string_p;
4147 };
4148
4149
4150 /* Set up iterator IT from overlay strings at its current position.
4151 Called from handle_stop. */
4152
4153 static enum prop_handled
4154 handle_overlay_change (it)
4155 struct it *it;
4156 {
4157 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4158 return HANDLED_RECOMPUTE_PROPS;
4159 else
4160 return HANDLED_NORMALLY;
4161 }
4162
4163
4164 /* Set up the next overlay string for delivery by IT, if there is an
4165 overlay string to deliver. Called by set_iterator_to_next when the
4166 end of the current overlay string is reached. If there are more
4167 overlay strings to display, IT->string and
4168 IT->current.overlay_string_index are set appropriately here.
4169 Otherwise IT->string is set to nil. */
4170
4171 static void
4172 next_overlay_string (it)
4173 struct it *it;
4174 {
4175 ++it->current.overlay_string_index;
4176 if (it->current.overlay_string_index == it->n_overlay_strings)
4177 {
4178 /* No more overlay strings. Restore IT's settings to what
4179 they were before overlay strings were processed, and
4180 continue to deliver from current_buffer. */
4181 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4182
4183 pop_it (it);
4184 xassert (it->stop_charpos >= BEGV
4185 && it->stop_charpos <= it->end_charpos);
4186 it->string = Qnil;
4187 it->current.overlay_string_index = -1;
4188 SET_TEXT_POS (it->current.string_pos, -1, -1);
4189 it->n_overlay_strings = 0;
4190 it->method = GET_FROM_BUFFER;
4191
4192 /* If we're at the end of the buffer, record that we have
4193 processed the overlay strings there already, so that
4194 next_element_from_buffer doesn't try it again. */
4195 if (IT_CHARPOS (*it) >= it->end_charpos)
4196 it->overlay_strings_at_end_processed_p = 1;
4197
4198 /* If we have to display `...' for invisible text, set
4199 the iterator up for that. */
4200 if (display_ellipsis_p)
4201 setup_for_ellipsis (it, 0);
4202 }
4203 else
4204 {
4205 /* There are more overlay strings to process. If
4206 IT->current.overlay_string_index has advanced to a position
4207 where we must load IT->overlay_strings with more strings, do
4208 it. */
4209 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4210
4211 if (it->current.overlay_string_index && i == 0)
4212 load_overlay_strings (it, 0);
4213
4214 /* Initialize IT to deliver display elements from the overlay
4215 string. */
4216 it->string = it->overlay_strings[i];
4217 it->multibyte_p = STRING_MULTIBYTE (it->string);
4218 SET_TEXT_POS (it->current.string_pos, 0, 0);
4219 it->method = GET_FROM_STRING;
4220 it->stop_charpos = 0;
4221 }
4222
4223 CHECK_IT (it);
4224 }
4225
4226
4227 /* Compare two overlay_entry structures E1 and E2. Used as a
4228 comparison function for qsort in load_overlay_strings. Overlay
4229 strings for the same position are sorted so that
4230
4231 1. All after-strings come in front of before-strings, except
4232 when they come from the same overlay.
4233
4234 2. Within after-strings, strings are sorted so that overlay strings
4235 from overlays with higher priorities come first.
4236
4237 2. Within before-strings, strings are sorted so that overlay
4238 strings from overlays with higher priorities come last.
4239
4240 Value is analogous to strcmp. */
4241
4242
4243 static int
4244 compare_overlay_entries (e1, e2)
4245 void *e1, *e2;
4246 {
4247 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4248 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4249 int result;
4250
4251 if (entry1->after_string_p != entry2->after_string_p)
4252 {
4253 /* Let after-strings appear in front of before-strings if
4254 they come from different overlays. */
4255 if (EQ (entry1->overlay, entry2->overlay))
4256 result = entry1->after_string_p ? 1 : -1;
4257 else
4258 result = entry1->after_string_p ? -1 : 1;
4259 }
4260 else if (entry1->after_string_p)
4261 /* After-strings sorted in order of decreasing priority. */
4262 result = entry2->priority - entry1->priority;
4263 else
4264 /* Before-strings sorted in order of increasing priority. */
4265 result = entry1->priority - entry2->priority;
4266
4267 return result;
4268 }
4269
4270
4271 /* Load the vector IT->overlay_strings with overlay strings from IT's
4272 current buffer position, or from CHARPOS if that is > 0. Set
4273 IT->n_overlays to the total number of overlay strings found.
4274
4275 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4276 a time. On entry into load_overlay_strings,
4277 IT->current.overlay_string_index gives the number of overlay
4278 strings that have already been loaded by previous calls to this
4279 function.
4280
4281 IT->add_overlay_start contains an additional overlay start
4282 position to consider for taking overlay strings from, if non-zero.
4283 This position comes into play when the overlay has an `invisible'
4284 property, and both before and after-strings. When we've skipped to
4285 the end of the overlay, because of its `invisible' property, we
4286 nevertheless want its before-string to appear.
4287 IT->add_overlay_start will contain the overlay start position
4288 in this case.
4289
4290 Overlay strings are sorted so that after-string strings come in
4291 front of before-string strings. Within before and after-strings,
4292 strings are sorted by overlay priority. See also function
4293 compare_overlay_entries. */
4294
4295 static void
4296 load_overlay_strings (it, charpos)
4297 struct it *it;
4298 int charpos;
4299 {
4300 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4301 Lisp_Object overlay, window, str, invisible;
4302 struct Lisp_Overlay *ov;
4303 int start, end;
4304 int size = 20;
4305 int n = 0, i, j, invis_p;
4306 struct overlay_entry *entries
4307 = (struct overlay_entry *) alloca (size * sizeof *entries);
4308
4309 if (charpos <= 0)
4310 charpos = IT_CHARPOS (*it);
4311
4312 /* Append the overlay string STRING of overlay OVERLAY to vector
4313 `entries' which has size `size' and currently contains `n'
4314 elements. AFTER_P non-zero means STRING is an after-string of
4315 OVERLAY. */
4316 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4317 do \
4318 { \
4319 Lisp_Object priority; \
4320 \
4321 if (n == size) \
4322 { \
4323 int new_size = 2 * size; \
4324 struct overlay_entry *old = entries; \
4325 entries = \
4326 (struct overlay_entry *) alloca (new_size \
4327 * sizeof *entries); \
4328 bcopy (old, entries, size * sizeof *entries); \
4329 size = new_size; \
4330 } \
4331 \
4332 entries[n].string = (STRING); \
4333 entries[n].overlay = (OVERLAY); \
4334 priority = Foverlay_get ((OVERLAY), Qpriority); \
4335 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4336 entries[n].after_string_p = (AFTER_P); \
4337 ++n; \
4338 } \
4339 while (0)
4340
4341 /* Process overlay before the overlay center. */
4342 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4343 {
4344 XSETMISC (overlay, ov);
4345 xassert (OVERLAYP (overlay));
4346 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4347 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4348
4349 if (end < charpos)
4350 break;
4351
4352 /* Skip this overlay if it doesn't start or end at IT's current
4353 position. */
4354 if (end != charpos && start != charpos)
4355 continue;
4356
4357 /* Skip this overlay if it doesn't apply to IT->w. */
4358 window = Foverlay_get (overlay, Qwindow);
4359 if (WINDOWP (window) && XWINDOW (window) != it->w)
4360 continue;
4361
4362 /* If the text ``under'' the overlay is invisible, both before-
4363 and after-strings from this overlay are visible; start and
4364 end position are indistinguishable. */
4365 invisible = Foverlay_get (overlay, Qinvisible);
4366 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4367
4368 /* If overlay has a non-empty before-string, record it. */
4369 if ((start == charpos || (end == charpos && invis_p))
4370 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4371 && SCHARS (str))
4372 RECORD_OVERLAY_STRING (overlay, str, 0);
4373
4374 /* If overlay has a non-empty after-string, record it. */
4375 if ((end == charpos || (start == charpos && invis_p))
4376 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4377 && SCHARS (str))
4378 RECORD_OVERLAY_STRING (overlay, str, 1);
4379 }
4380
4381 /* Process overlays after the overlay center. */
4382 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4383 {
4384 XSETMISC (overlay, ov);
4385 xassert (OVERLAYP (overlay));
4386 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4387 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4388
4389 if (start > charpos)
4390 break;
4391
4392 /* Skip this overlay if it doesn't start or end at IT's current
4393 position. */
4394 if (end != charpos && start != charpos)
4395 continue;
4396
4397 /* Skip this overlay if it doesn't apply to IT->w. */
4398 window = Foverlay_get (overlay, Qwindow);
4399 if (WINDOWP (window) && XWINDOW (window) != it->w)
4400 continue;
4401
4402 /* If the text ``under'' the overlay is invisible, it has a zero
4403 dimension, and both before- and after-strings apply. */
4404 invisible = Foverlay_get (overlay, Qinvisible);
4405 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4406
4407 /* If overlay has a non-empty before-string, record it. */
4408 if ((start == charpos || (end == charpos && invis_p))
4409 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4410 && SCHARS (str))
4411 RECORD_OVERLAY_STRING (overlay, str, 0);
4412
4413 /* If overlay has a non-empty after-string, record it. */
4414 if ((end == charpos || (start == charpos && invis_p))
4415 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4416 && SCHARS (str))
4417 RECORD_OVERLAY_STRING (overlay, str, 1);
4418 }
4419
4420 #undef RECORD_OVERLAY_STRING
4421
4422 /* Sort entries. */
4423 if (n > 1)
4424 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4425
4426 /* Record the total number of strings to process. */
4427 it->n_overlay_strings = n;
4428
4429 /* IT->current.overlay_string_index is the number of overlay strings
4430 that have already been consumed by IT. Copy some of the
4431 remaining overlay strings to IT->overlay_strings. */
4432 i = 0;
4433 j = it->current.overlay_string_index;
4434 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4435 it->overlay_strings[i++] = entries[j++].string;
4436
4437 CHECK_IT (it);
4438 }
4439
4440
4441 /* Get the first chunk of overlay strings at IT's current buffer
4442 position, or at CHARPOS if that is > 0. Value is non-zero if at
4443 least one overlay string was found. */
4444
4445 static int
4446 get_overlay_strings (it, charpos)
4447 struct it *it;
4448 int charpos;
4449 {
4450 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4451 process. This fills IT->overlay_strings with strings, and sets
4452 IT->n_overlay_strings to the total number of strings to process.
4453 IT->pos.overlay_string_index has to be set temporarily to zero
4454 because load_overlay_strings needs this; it must be set to -1
4455 when no overlay strings are found because a zero value would
4456 indicate a position in the first overlay string. */
4457 it->current.overlay_string_index = 0;
4458 load_overlay_strings (it, charpos);
4459
4460 /* If we found overlay strings, set up IT to deliver display
4461 elements from the first one. Otherwise set up IT to deliver
4462 from current_buffer. */
4463 if (it->n_overlay_strings)
4464 {
4465 /* Make sure we know settings in current_buffer, so that we can
4466 restore meaningful values when we're done with the overlay
4467 strings. */
4468 compute_stop_pos (it);
4469 xassert (it->face_id >= 0);
4470
4471 /* Save IT's settings. They are restored after all overlay
4472 strings have been processed. */
4473 xassert (it->sp == 0);
4474 push_it (it);
4475
4476 /* Set up IT to deliver display elements from the first overlay
4477 string. */
4478 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4479 it->string = it->overlay_strings[0];
4480 it->stop_charpos = 0;
4481 xassert (STRINGP (it->string));
4482 it->end_charpos = SCHARS (it->string);
4483 it->multibyte_p = STRING_MULTIBYTE (it->string);
4484 it->method = GET_FROM_STRING;
4485 }
4486 else
4487 {
4488 it->string = Qnil;
4489 it->current.overlay_string_index = -1;
4490 it->method = GET_FROM_BUFFER;
4491 }
4492
4493 CHECK_IT (it);
4494
4495 /* Value is non-zero if we found at least one overlay string. */
4496 return STRINGP (it->string);
4497 }
4498
4499
4500 \f
4501 /***********************************************************************
4502 Saving and restoring state
4503 ***********************************************************************/
4504
4505 /* Save current settings of IT on IT->stack. Called, for example,
4506 before setting up IT for an overlay string, to be able to restore
4507 IT's settings to what they were after the overlay string has been
4508 processed. */
4509
4510 static void
4511 push_it (it)
4512 struct it *it;
4513 {
4514 struct iterator_stack_entry *p;
4515
4516 xassert (it->sp < 2);
4517 p = it->stack + it->sp;
4518
4519 p->stop_charpos = it->stop_charpos;
4520 xassert (it->face_id >= 0);
4521 p->face_id = it->face_id;
4522 p->string = it->string;
4523 p->pos = it->current;
4524 p->end_charpos = it->end_charpos;
4525 p->string_nchars = it->string_nchars;
4526 p->area = it->area;
4527 p->multibyte_p = it->multibyte_p;
4528 p->slice = it->slice;
4529 p->space_width = it->space_width;
4530 p->font_height = it->font_height;
4531 p->voffset = it->voffset;
4532 p->string_from_display_prop_p = it->string_from_display_prop_p;
4533 p->display_ellipsis_p = 0;
4534 ++it->sp;
4535 }
4536
4537
4538 /* Restore IT's settings from IT->stack. Called, for example, when no
4539 more overlay strings must be processed, and we return to delivering
4540 display elements from a buffer, or when the end of a string from a
4541 `display' property is reached and we return to delivering display
4542 elements from an overlay string, or from a buffer. */
4543
4544 static void
4545 pop_it (it)
4546 struct it *it;
4547 {
4548 struct iterator_stack_entry *p;
4549
4550 xassert (it->sp > 0);
4551 --it->sp;
4552 p = it->stack + it->sp;
4553 it->stop_charpos = p->stop_charpos;
4554 it->face_id = p->face_id;
4555 it->string = p->string;
4556 it->current = p->pos;
4557 it->end_charpos = p->end_charpos;
4558 it->string_nchars = p->string_nchars;
4559 it->area = p->area;
4560 it->multibyte_p = p->multibyte_p;
4561 it->slice = p->slice;
4562 it->space_width = p->space_width;
4563 it->font_height = p->font_height;
4564 it->voffset = p->voffset;
4565 it->string_from_display_prop_p = p->string_from_display_prop_p;
4566 }
4567
4568
4569 \f
4570 /***********************************************************************
4571 Moving over lines
4572 ***********************************************************************/
4573
4574 /* Set IT's current position to the previous line start. */
4575
4576 static void
4577 back_to_previous_line_start (it)
4578 struct it *it;
4579 {
4580 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4581 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4582 }
4583
4584
4585 /* Move IT to the next line start.
4586
4587 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4588 we skipped over part of the text (as opposed to moving the iterator
4589 continuously over the text). Otherwise, don't change the value
4590 of *SKIPPED_P.
4591
4592 Newlines may come from buffer text, overlay strings, or strings
4593 displayed via the `display' property. That's the reason we can't
4594 simply use find_next_newline_no_quit.
4595
4596 Note that this function may not skip over invisible text that is so
4597 because of text properties and immediately follows a newline. If
4598 it would, function reseat_at_next_visible_line_start, when called
4599 from set_iterator_to_next, would effectively make invisible
4600 characters following a newline part of the wrong glyph row, which
4601 leads to wrong cursor motion. */
4602
4603 static int
4604 forward_to_next_line_start (it, skipped_p)
4605 struct it *it;
4606 int *skipped_p;
4607 {
4608 int old_selective, newline_found_p, n;
4609 const int MAX_NEWLINE_DISTANCE = 500;
4610
4611 /* If already on a newline, just consume it to avoid unintended
4612 skipping over invisible text below. */
4613 if (it->what == IT_CHARACTER
4614 && it->c == '\n'
4615 && CHARPOS (it->position) == IT_CHARPOS (*it))
4616 {
4617 set_iterator_to_next (it, 0);
4618 it->c = 0;
4619 return 1;
4620 }
4621
4622 /* Don't handle selective display in the following. It's (a)
4623 unnecessary because it's done by the caller, and (b) leads to an
4624 infinite recursion because next_element_from_ellipsis indirectly
4625 calls this function. */
4626 old_selective = it->selective;
4627 it->selective = 0;
4628
4629 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4630 from buffer text. */
4631 for (n = newline_found_p = 0;
4632 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4633 n += STRINGP (it->string) ? 0 : 1)
4634 {
4635 if (!get_next_display_element (it))
4636 return 0;
4637 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4638 set_iterator_to_next (it, 0);
4639 }
4640
4641 /* If we didn't find a newline near enough, see if we can use a
4642 short-cut. */
4643 if (!newline_found_p)
4644 {
4645 int start = IT_CHARPOS (*it);
4646 int limit = find_next_newline_no_quit (start, 1);
4647 Lisp_Object pos;
4648
4649 xassert (!STRINGP (it->string));
4650
4651 /* If there isn't any `display' property in sight, and no
4652 overlays, we can just use the position of the newline in
4653 buffer text. */
4654 if (it->stop_charpos >= limit
4655 || ((pos = Fnext_single_property_change (make_number (start),
4656 Qdisplay,
4657 Qnil, make_number (limit)),
4658 NILP (pos))
4659 && next_overlay_change (start) == ZV))
4660 {
4661 IT_CHARPOS (*it) = limit;
4662 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4663 *skipped_p = newline_found_p = 1;
4664 }
4665 else
4666 {
4667 while (get_next_display_element (it)
4668 && !newline_found_p)
4669 {
4670 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4671 set_iterator_to_next (it, 0);
4672 }
4673 }
4674 }
4675
4676 it->selective = old_selective;
4677 return newline_found_p;
4678 }
4679
4680
4681 /* Set IT's current position to the previous visible line start. Skip
4682 invisible text that is so either due to text properties or due to
4683 selective display. Caution: this does not change IT->current_x and
4684 IT->hpos. */
4685
4686 static void
4687 back_to_previous_visible_line_start (it)
4688 struct it *it;
4689 {
4690 while (IT_CHARPOS (*it) > BEGV)
4691 {
4692 back_to_previous_line_start (it);
4693 if (IT_CHARPOS (*it) <= BEGV)
4694 break;
4695
4696 /* If selective > 0, then lines indented more than that values
4697 are invisible. */
4698 if (it->selective > 0
4699 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4700 (double) it->selective)) /* iftc */
4701 continue;
4702
4703 /* Check the newline before point for invisibility. */
4704 {
4705 Lisp_Object prop;
4706 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4707 Qinvisible, it->window);
4708 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4709 continue;
4710 }
4711
4712 /* If newline has a display property that replaces the newline with something
4713 else (image or text), find start of overlay or interval and continue search
4714 from that point. */
4715 if (IT_CHARPOS (*it) > BEGV)
4716 {
4717 struct it it2 = *it;
4718 int pos;
4719 int beg, end;
4720 Lisp_Object val, overlay;
4721
4722 pos = --IT_CHARPOS (it2);
4723 --IT_BYTEPOS (it2);
4724 it2.sp = 0;
4725 if (handle_display_prop (&it2) == HANDLED_RETURN
4726 && !NILP (val = get_char_property_and_overlay
4727 (make_number (pos), Qdisplay, Qnil, &overlay))
4728 && (OVERLAYP (overlay)
4729 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4730 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4731 {
4732 if (beg < BEGV)
4733 beg = BEGV;
4734 IT_CHARPOS (*it) = beg;
4735 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4736 continue;
4737 }
4738 }
4739
4740 break;
4741 }
4742
4743 xassert (IT_CHARPOS (*it) >= BEGV);
4744 xassert (IT_CHARPOS (*it) == BEGV
4745 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4746 CHECK_IT (it);
4747 }
4748
4749
4750 /* Reseat iterator IT at the previous visible line start. Skip
4751 invisible text that is so either due to text properties or due to
4752 selective display. At the end, update IT's overlay information,
4753 face information etc. */
4754
4755 void
4756 reseat_at_previous_visible_line_start (it)
4757 struct it *it;
4758 {
4759 back_to_previous_visible_line_start (it);
4760 reseat (it, it->current.pos, 1);
4761 CHECK_IT (it);
4762 }
4763
4764
4765 /* Reseat iterator IT on the next visible line start in the current
4766 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4767 preceding the line start. Skip over invisible text that is so
4768 because of selective display. Compute faces, overlays etc at the
4769 new position. Note that this function does not skip over text that
4770 is invisible because of text properties. */
4771
4772 static void
4773 reseat_at_next_visible_line_start (it, on_newline_p)
4774 struct it *it;
4775 int on_newline_p;
4776 {
4777 int newline_found_p, skipped_p = 0;
4778
4779 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4780
4781 /* Skip over lines that are invisible because they are indented
4782 more than the value of IT->selective. */
4783 if (it->selective > 0)
4784 while (IT_CHARPOS (*it) < ZV
4785 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4786 (double) it->selective)) /* iftc */
4787 {
4788 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4789 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4790 }
4791
4792 /* Position on the newline if that's what's requested. */
4793 if (on_newline_p && newline_found_p)
4794 {
4795 if (STRINGP (it->string))
4796 {
4797 if (IT_STRING_CHARPOS (*it) > 0)
4798 {
4799 --IT_STRING_CHARPOS (*it);
4800 --IT_STRING_BYTEPOS (*it);
4801 }
4802 }
4803 else if (IT_CHARPOS (*it) > BEGV)
4804 {
4805 --IT_CHARPOS (*it);
4806 --IT_BYTEPOS (*it);
4807 reseat (it, it->current.pos, 0);
4808 }
4809 }
4810 else if (skipped_p)
4811 reseat (it, it->current.pos, 0);
4812
4813 CHECK_IT (it);
4814 }
4815
4816
4817 \f
4818 /***********************************************************************
4819 Changing an iterator's position
4820 ***********************************************************************/
4821
4822 /* Change IT's current position to POS in current_buffer. If FORCE_P
4823 is non-zero, always check for text properties at the new position.
4824 Otherwise, text properties are only looked up if POS >=
4825 IT->check_charpos of a property. */
4826
4827 static void
4828 reseat (it, pos, force_p)
4829 struct it *it;
4830 struct text_pos pos;
4831 int force_p;
4832 {
4833 int original_pos = IT_CHARPOS (*it);
4834
4835 reseat_1 (it, pos, 0);
4836
4837 /* Determine where to check text properties. Avoid doing it
4838 where possible because text property lookup is very expensive. */
4839 if (force_p
4840 || CHARPOS (pos) > it->stop_charpos
4841 || CHARPOS (pos) < original_pos)
4842 handle_stop (it);
4843
4844 CHECK_IT (it);
4845 }
4846
4847
4848 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4849 IT->stop_pos to POS, also. */
4850
4851 static void
4852 reseat_1 (it, pos, set_stop_p)
4853 struct it *it;
4854 struct text_pos pos;
4855 int set_stop_p;
4856 {
4857 /* Don't call this function when scanning a C string. */
4858 xassert (it->s == NULL);
4859
4860 /* POS must be a reasonable value. */
4861 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4862
4863 it->current.pos = it->position = pos;
4864 XSETBUFFER (it->object, current_buffer);
4865 it->end_charpos = ZV;
4866 it->dpvec = NULL;
4867 it->current.dpvec_index = -1;
4868 it->current.overlay_string_index = -1;
4869 IT_STRING_CHARPOS (*it) = -1;
4870 IT_STRING_BYTEPOS (*it) = -1;
4871 it->string = Qnil;
4872 it->method = GET_FROM_BUFFER;
4873 /* RMS: I added this to fix a bug in move_it_vertically_backward
4874 where it->area continued to relate to the starting point
4875 for the backward motion. Bug report from
4876 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4877 However, I am not sure whether reseat still does the right thing
4878 in general after this change. */
4879 it->area = TEXT_AREA;
4880 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4881 it->sp = 0;
4882 it->face_before_selective_p = 0;
4883
4884 if (set_stop_p)
4885 it->stop_charpos = CHARPOS (pos);
4886 }
4887
4888
4889 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4890 If S is non-null, it is a C string to iterate over. Otherwise,
4891 STRING gives a Lisp string to iterate over.
4892
4893 If PRECISION > 0, don't return more then PRECISION number of
4894 characters from the string.
4895
4896 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4897 characters have been returned. FIELD_WIDTH < 0 means an infinite
4898 field width.
4899
4900 MULTIBYTE = 0 means disable processing of multibyte characters,
4901 MULTIBYTE > 0 means enable it,
4902 MULTIBYTE < 0 means use IT->multibyte_p.
4903
4904 IT must be initialized via a prior call to init_iterator before
4905 calling this function. */
4906
4907 static void
4908 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4909 struct it *it;
4910 unsigned char *s;
4911 Lisp_Object string;
4912 int charpos;
4913 int precision, field_width, multibyte;
4914 {
4915 /* No region in strings. */
4916 it->region_beg_charpos = it->region_end_charpos = -1;
4917
4918 /* No text property checks performed by default, but see below. */
4919 it->stop_charpos = -1;
4920
4921 /* Set iterator position and end position. */
4922 bzero (&it->current, sizeof it->current);
4923 it->current.overlay_string_index = -1;
4924 it->current.dpvec_index = -1;
4925 xassert (charpos >= 0);
4926
4927 /* If STRING is specified, use its multibyteness, otherwise use the
4928 setting of MULTIBYTE, if specified. */
4929 if (multibyte >= 0)
4930 it->multibyte_p = multibyte > 0;
4931
4932 if (s == NULL)
4933 {
4934 xassert (STRINGP (string));
4935 it->string = string;
4936 it->s = NULL;
4937 it->end_charpos = it->string_nchars = SCHARS (string);
4938 it->method = GET_FROM_STRING;
4939 it->current.string_pos = string_pos (charpos, string);
4940 }
4941 else
4942 {
4943 it->s = s;
4944 it->string = Qnil;
4945
4946 /* Note that we use IT->current.pos, not it->current.string_pos,
4947 for displaying C strings. */
4948 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4949 if (it->multibyte_p)
4950 {
4951 it->current.pos = c_string_pos (charpos, s, 1);
4952 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4953 }
4954 else
4955 {
4956 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4957 it->end_charpos = it->string_nchars = strlen (s);
4958 }
4959
4960 it->method = GET_FROM_C_STRING;
4961 }
4962
4963 /* PRECISION > 0 means don't return more than PRECISION characters
4964 from the string. */
4965 if (precision > 0 && it->end_charpos - charpos > precision)
4966 it->end_charpos = it->string_nchars = charpos + precision;
4967
4968 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4969 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4970 FIELD_WIDTH < 0 means infinite field width. This is useful for
4971 padding with `-' at the end of a mode line. */
4972 if (field_width < 0)
4973 field_width = INFINITY;
4974 if (field_width > it->end_charpos - charpos)
4975 it->end_charpos = charpos + field_width;
4976
4977 /* Use the standard display table for displaying strings. */
4978 if (DISP_TABLE_P (Vstandard_display_table))
4979 it->dp = XCHAR_TABLE (Vstandard_display_table);
4980
4981 it->stop_charpos = charpos;
4982 CHECK_IT (it);
4983 }
4984
4985
4986 \f
4987 /***********************************************************************
4988 Iteration
4989 ***********************************************************************/
4990
4991 /* Map enum it_method value to corresponding next_element_from_* function. */
4992
4993 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
4994 {
4995 next_element_from_buffer,
4996 next_element_from_display_vector,
4997 next_element_from_composition,
4998 next_element_from_string,
4999 next_element_from_c_string,
5000 next_element_from_image,
5001 next_element_from_stretch
5002 };
5003
5004
5005 /* Load IT's display element fields with information about the next
5006 display element from the current position of IT. Value is zero if
5007 end of buffer (or C string) is reached. */
5008
5009 int
5010 get_next_display_element (it)
5011 struct it *it;
5012 {
5013 /* Non-zero means that we found a display element. Zero means that
5014 we hit the end of what we iterate over. Performance note: the
5015 function pointer `method' used here turns out to be faster than
5016 using a sequence of if-statements. */
5017 int success_p;
5018
5019 get_next:
5020 success_p = (*get_next_element[it->method]) (it);
5021
5022 if (it->what == IT_CHARACTER)
5023 {
5024 /* Map via display table or translate control characters.
5025 IT->c, IT->len etc. have been set to the next character by
5026 the function call above. If we have a display table, and it
5027 contains an entry for IT->c, translate it. Don't do this if
5028 IT->c itself comes from a display table, otherwise we could
5029 end up in an infinite recursion. (An alternative could be to
5030 count the recursion depth of this function and signal an
5031 error when a certain maximum depth is reached.) Is it worth
5032 it? */
5033 if (success_p && it->dpvec == NULL)
5034 {
5035 Lisp_Object dv;
5036
5037 if (it->dp
5038 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5039 VECTORP (dv)))
5040 {
5041 struct Lisp_Vector *v = XVECTOR (dv);
5042
5043 /* Return the first character from the display table
5044 entry, if not empty. If empty, don't display the
5045 current character. */
5046 if (v->size)
5047 {
5048 it->dpvec_char_len = it->len;
5049 it->dpvec = v->contents;
5050 it->dpend = v->contents + v->size;
5051 it->current.dpvec_index = 0;
5052 it->dpvec_face_id = -1;
5053 it->saved_face_id = it->face_id;
5054 it->method = GET_FROM_DISPLAY_VECTOR;
5055 it->ellipsis_p = 0;
5056 }
5057 else
5058 {
5059 set_iterator_to_next (it, 0);
5060 }
5061 goto get_next;
5062 }
5063
5064 /* Translate control characters into `\003' or `^C' form.
5065 Control characters coming from a display table entry are
5066 currently not translated because we use IT->dpvec to hold
5067 the translation. This could easily be changed but I
5068 don't believe that it is worth doing.
5069
5070 If it->multibyte_p is nonzero, eight-bit characters and
5071 non-printable multibyte characters are also translated to
5072 octal form.
5073
5074 If it->multibyte_p is zero, eight-bit characters that
5075 don't have corresponding multibyte char code are also
5076 translated to octal form. */
5077 else if ((it->c < ' '
5078 && (it->area != TEXT_AREA
5079 /* In mode line, treat \n like other crl chars. */
5080 || (it->c != '\t'
5081 && it->glyph_row && it->glyph_row->mode_line_p)
5082 || (it->c != '\n' && it->c != '\t')))
5083 || (it->multibyte_p
5084 ? ((it->c >= 127
5085 && it->len == 1)
5086 || !CHAR_PRINTABLE_P (it->c)
5087 || (!NILP (Vnobreak_char_display)
5088 && (it->c == 0x8a0 || it->c == 0x8ad
5089 || it->c == 0x920 || it->c == 0x92d
5090 || it->c == 0xe20 || it->c == 0xe2d
5091 || it->c == 0xf20 || it->c == 0xf2d)))
5092 : (it->c >= 127
5093 && (!unibyte_display_via_language_environment
5094 || it->c == unibyte_char_to_multibyte (it->c)))))
5095 {
5096 /* IT->c is a control character which must be displayed
5097 either as '\003' or as `^C' where the '\\' and '^'
5098 can be defined in the display table. Fill
5099 IT->ctl_chars with glyphs for what we have to
5100 display. Then, set IT->dpvec to these glyphs. */
5101 GLYPH g;
5102 int ctl_len;
5103 int face_id, lface_id = 0 ;
5104 GLYPH escape_glyph;
5105
5106 /* Handle control characters with ^. */
5107
5108 if (it->c < 128 && it->ctl_arrow_p)
5109 {
5110 g = '^'; /* default glyph for Control */
5111 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5112 if (it->dp
5113 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5114 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5115 {
5116 g = XINT (DISP_CTRL_GLYPH (it->dp));
5117 lface_id = FAST_GLYPH_FACE (g);
5118 }
5119 if (lface_id)
5120 {
5121 g = FAST_GLYPH_CHAR (g);
5122 face_id = merge_faces (it->f, Qt, lface_id,
5123 it->face_id);
5124 }
5125 else
5126 {
5127 /* Merge the escape-glyph face into the current face. */
5128 face_id = merge_faces (it->f, Qescape_glyph, 0,
5129 it->face_id);
5130 }
5131
5132 XSETINT (it->ctl_chars[0], g);
5133 g = it->c ^ 0100;
5134 XSETINT (it->ctl_chars[1], g);
5135 ctl_len = 2;
5136 goto display_control;
5137 }
5138
5139 /* Handle non-break space in the mode where it only gets
5140 highlighting. */
5141
5142 if (EQ (Vnobreak_char_display, Qt)
5143 && (it->c == 0x8a0 || it->c == 0x920
5144 || it->c == 0xe20 || it->c == 0xf20))
5145 {
5146 /* Merge the no-break-space face into the current face. */
5147 face_id = merge_faces (it->f, Qnobreak_space, 0,
5148 it->face_id);
5149
5150 g = it->c = ' ';
5151 XSETINT (it->ctl_chars[0], g);
5152 ctl_len = 1;
5153 goto display_control;
5154 }
5155
5156 /* Handle sequences that start with the "escape glyph". */
5157
5158 /* the default escape glyph is \. */
5159 escape_glyph = '\\';
5160
5161 if (it->dp
5162 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5163 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5164 {
5165 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5166 lface_id = FAST_GLYPH_FACE (escape_glyph);
5167 }
5168 if (lface_id)
5169 {
5170 /* The display table specified a face.
5171 Merge it into face_id and also into escape_glyph. */
5172 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5173 face_id = merge_faces (it->f, Qt, lface_id,
5174 it->face_id);
5175 }
5176 else
5177 {
5178 /* Merge the escape-glyph face into the current face. */
5179 face_id = merge_faces (it->f, Qescape_glyph, 0,
5180 it->face_id);
5181 }
5182
5183 /* Handle soft hyphens in the mode where they only get
5184 highlighting. */
5185
5186 if (EQ (Vnobreak_char_display, Qt)
5187 && (it->c == 0x8ad || it->c == 0x92d
5188 || it->c == 0xe2d || it->c == 0xf2d))
5189 {
5190 g = it->c = '-';
5191 XSETINT (it->ctl_chars[0], g);
5192 ctl_len = 1;
5193 goto display_control;
5194 }
5195
5196 /* Handle non-break space and soft hyphen
5197 with the escape glyph. */
5198
5199 if (it->c == 0x8a0 || it->c == 0x8ad
5200 || it->c == 0x920 || it->c == 0x92d
5201 || it->c == 0xe20 || it->c == 0xe2d
5202 || it->c == 0xf20 || it->c == 0xf2d)
5203 {
5204 XSETINT (it->ctl_chars[0], escape_glyph);
5205 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5206 XSETINT (it->ctl_chars[1], g);
5207 ctl_len = 2;
5208 goto display_control;
5209 }
5210
5211 {
5212 unsigned char str[MAX_MULTIBYTE_LENGTH];
5213 int len;
5214 int i;
5215
5216 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5217 if (SINGLE_BYTE_CHAR_P (it->c))
5218 str[0] = it->c, len = 1;
5219 else
5220 {
5221 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5222 if (len < 0)
5223 {
5224 /* It's an invalid character, which shouldn't
5225 happen actually, but due to bugs it may
5226 happen. Let's print the char as is, there's
5227 not much meaningful we can do with it. */
5228 str[0] = it->c;
5229 str[1] = it->c >> 8;
5230 str[2] = it->c >> 16;
5231 str[3] = it->c >> 24;
5232 len = 4;
5233 }
5234 }
5235
5236 for (i = 0; i < len; i++)
5237 {
5238 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5239 /* Insert three more glyphs into IT->ctl_chars for
5240 the octal display of the character. */
5241 g = ((str[i] >> 6) & 7) + '0';
5242 XSETINT (it->ctl_chars[i * 4 + 1], g);
5243 g = ((str[i] >> 3) & 7) + '0';
5244 XSETINT (it->ctl_chars[i * 4 + 2], g);
5245 g = (str[i] & 7) + '0';
5246 XSETINT (it->ctl_chars[i * 4 + 3], g);
5247 }
5248 ctl_len = len * 4;
5249 }
5250
5251 display_control:
5252 /* Set up IT->dpvec and return first character from it. */
5253 it->dpvec_char_len = it->len;
5254 it->dpvec = it->ctl_chars;
5255 it->dpend = it->dpvec + ctl_len;
5256 it->current.dpvec_index = 0;
5257 it->dpvec_face_id = face_id;
5258 it->saved_face_id = it->face_id;
5259 it->method = GET_FROM_DISPLAY_VECTOR;
5260 it->ellipsis_p = 0;
5261 goto get_next;
5262 }
5263 }
5264
5265 /* Adjust face id for a multibyte character. There are no
5266 multibyte character in unibyte text. */
5267 if (it->multibyte_p
5268 && success_p
5269 && FRAME_WINDOW_P (it->f))
5270 {
5271 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5272 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5273 }
5274 }
5275
5276 /* Is this character the last one of a run of characters with
5277 box? If yes, set IT->end_of_box_run_p to 1. */
5278 if (it->face_box_p
5279 && it->s == NULL)
5280 {
5281 int face_id;
5282 struct face *face;
5283
5284 it->end_of_box_run_p
5285 = ((face_id = face_after_it_pos (it),
5286 face_id != it->face_id)
5287 && (face = FACE_FROM_ID (it->f, face_id),
5288 face->box == FACE_NO_BOX));
5289 }
5290
5291 /* Value is 0 if end of buffer or string reached. */
5292 return success_p;
5293 }
5294
5295
5296 /* Move IT to the next display element.
5297
5298 RESEAT_P non-zero means if called on a newline in buffer text,
5299 skip to the next visible line start.
5300
5301 Functions get_next_display_element and set_iterator_to_next are
5302 separate because I find this arrangement easier to handle than a
5303 get_next_display_element function that also increments IT's
5304 position. The way it is we can first look at an iterator's current
5305 display element, decide whether it fits on a line, and if it does,
5306 increment the iterator position. The other way around we probably
5307 would either need a flag indicating whether the iterator has to be
5308 incremented the next time, or we would have to implement a
5309 decrement position function which would not be easy to write. */
5310
5311 void
5312 set_iterator_to_next (it, reseat_p)
5313 struct it *it;
5314 int reseat_p;
5315 {
5316 /* Reset flags indicating start and end of a sequence of characters
5317 with box. Reset them at the start of this function because
5318 moving the iterator to a new position might set them. */
5319 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5320
5321 switch (it->method)
5322 {
5323 case GET_FROM_BUFFER:
5324 /* The current display element of IT is a character from
5325 current_buffer. Advance in the buffer, and maybe skip over
5326 invisible lines that are so because of selective display. */
5327 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5328 reseat_at_next_visible_line_start (it, 0);
5329 else
5330 {
5331 xassert (it->len != 0);
5332 IT_BYTEPOS (*it) += it->len;
5333 IT_CHARPOS (*it) += 1;
5334 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5335 }
5336 break;
5337
5338 case GET_FROM_COMPOSITION:
5339 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5340 if (STRINGP (it->string))
5341 {
5342 IT_STRING_BYTEPOS (*it) += it->len;
5343 IT_STRING_CHARPOS (*it) += it->cmp_len;
5344 it->method = GET_FROM_STRING;
5345 goto consider_string_end;
5346 }
5347 else
5348 {
5349 IT_BYTEPOS (*it) += it->len;
5350 IT_CHARPOS (*it) += it->cmp_len;
5351 it->method = GET_FROM_BUFFER;
5352 }
5353 break;
5354
5355 case GET_FROM_C_STRING:
5356 /* Current display element of IT is from a C string. */
5357 IT_BYTEPOS (*it) += it->len;
5358 IT_CHARPOS (*it) += 1;
5359 break;
5360
5361 case GET_FROM_DISPLAY_VECTOR:
5362 /* Current display element of IT is from a display table entry.
5363 Advance in the display table definition. Reset it to null if
5364 end reached, and continue with characters from buffers/
5365 strings. */
5366 ++it->current.dpvec_index;
5367
5368 /* Restore face of the iterator to what they were before the
5369 display vector entry (these entries may contain faces). */
5370 it->face_id = it->saved_face_id;
5371
5372 if (it->dpvec + it->current.dpvec_index == it->dpend)
5373 {
5374 if (it->s)
5375 it->method = GET_FROM_C_STRING;
5376 else if (STRINGP (it->string))
5377 it->method = GET_FROM_STRING;
5378 else
5379 it->method = GET_FROM_BUFFER;
5380
5381 it->dpvec = NULL;
5382 it->current.dpvec_index = -1;
5383
5384 /* Skip over characters which were displayed via IT->dpvec. */
5385 if (it->dpvec_char_len < 0)
5386 reseat_at_next_visible_line_start (it, 1);
5387 else if (it->dpvec_char_len > 0)
5388 {
5389 it->len = it->dpvec_char_len;
5390 set_iterator_to_next (it, reseat_p);
5391 }
5392
5393 /* Recheck faces after display vector */
5394 it->stop_charpos = IT_CHARPOS (*it);
5395 }
5396 break;
5397
5398 case GET_FROM_STRING:
5399 /* Current display element is a character from a Lisp string. */
5400 xassert (it->s == NULL && STRINGP (it->string));
5401 IT_STRING_BYTEPOS (*it) += it->len;
5402 IT_STRING_CHARPOS (*it) += 1;
5403
5404 consider_string_end:
5405
5406 if (it->current.overlay_string_index >= 0)
5407 {
5408 /* IT->string is an overlay string. Advance to the
5409 next, if there is one. */
5410 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5411 next_overlay_string (it);
5412 }
5413 else
5414 {
5415 /* IT->string is not an overlay string. If we reached
5416 its end, and there is something on IT->stack, proceed
5417 with what is on the stack. This can be either another
5418 string, this time an overlay string, or a buffer. */
5419 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5420 && it->sp > 0)
5421 {
5422 pop_it (it);
5423 if (STRINGP (it->string))
5424 goto consider_string_end;
5425 it->method = GET_FROM_BUFFER;
5426 }
5427 }
5428 break;
5429
5430 case GET_FROM_IMAGE:
5431 case GET_FROM_STRETCH:
5432 /* The position etc with which we have to proceed are on
5433 the stack. The position may be at the end of a string,
5434 if the `display' property takes up the whole string. */
5435 xassert (it->sp > 0);
5436 pop_it (it);
5437 it->image_id = 0;
5438 if (STRINGP (it->string))
5439 {
5440 it->method = GET_FROM_STRING;
5441 goto consider_string_end;
5442 }
5443 it->method = GET_FROM_BUFFER;
5444 break;
5445
5446 default:
5447 /* There are no other methods defined, so this should be a bug. */
5448 abort ();
5449 }
5450
5451 xassert (it->method != GET_FROM_STRING
5452 || (STRINGP (it->string)
5453 && IT_STRING_CHARPOS (*it) >= 0));
5454 }
5455
5456 /* Load IT's display element fields with information about the next
5457 display element which comes from a display table entry or from the
5458 result of translating a control character to one of the forms `^C'
5459 or `\003'.
5460
5461 IT->dpvec holds the glyphs to return as characters.
5462 IT->saved_face_id holds the face id before the display vector--
5463 it is restored into IT->face_idin set_iterator_to_next. */
5464
5465 static int
5466 next_element_from_display_vector (it)
5467 struct it *it;
5468 {
5469 /* Precondition. */
5470 xassert (it->dpvec && it->current.dpvec_index >= 0);
5471
5472 it->face_id = it->saved_face_id;
5473
5474 if (INTEGERP (*it->dpvec)
5475 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5476 {
5477 GLYPH g;
5478
5479 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5480 it->c = FAST_GLYPH_CHAR (g);
5481 it->len = CHAR_BYTES (it->c);
5482
5483 /* The entry may contain a face id to use. Such a face id is
5484 the id of a Lisp face, not a realized face. A face id of
5485 zero means no face is specified. */
5486 if (it->dpvec_face_id >= 0)
5487 it->face_id = it->dpvec_face_id;
5488 else
5489 {
5490 int lface_id = FAST_GLYPH_FACE (g);
5491 if (lface_id > 0)
5492 it->face_id = merge_faces (it->f, Qt, lface_id,
5493 it->saved_face_id);
5494 }
5495 }
5496 else
5497 /* Display table entry is invalid. Return a space. */
5498 it->c = ' ', it->len = 1;
5499
5500 /* Don't change position and object of the iterator here. They are
5501 still the values of the character that had this display table
5502 entry or was translated, and that's what we want. */
5503 it->what = IT_CHARACTER;
5504 return 1;
5505 }
5506
5507
5508 /* Load IT with the next display element from Lisp string IT->string.
5509 IT->current.string_pos is the current position within the string.
5510 If IT->current.overlay_string_index >= 0, the Lisp string is an
5511 overlay string. */
5512
5513 static int
5514 next_element_from_string (it)
5515 struct it *it;
5516 {
5517 struct text_pos position;
5518
5519 xassert (STRINGP (it->string));
5520 xassert (IT_STRING_CHARPOS (*it) >= 0);
5521 position = it->current.string_pos;
5522
5523 /* Time to check for invisible text? */
5524 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5525 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5526 {
5527 handle_stop (it);
5528
5529 /* Since a handler may have changed IT->method, we must
5530 recurse here. */
5531 return get_next_display_element (it);
5532 }
5533
5534 if (it->current.overlay_string_index >= 0)
5535 {
5536 /* Get the next character from an overlay string. In overlay
5537 strings, There is no field width or padding with spaces to
5538 do. */
5539 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5540 {
5541 it->what = IT_EOB;
5542 return 0;
5543 }
5544 else if (STRING_MULTIBYTE (it->string))
5545 {
5546 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5547 const unsigned char *s = (SDATA (it->string)
5548 + IT_STRING_BYTEPOS (*it));
5549 it->c = string_char_and_length (s, remaining, &it->len);
5550 }
5551 else
5552 {
5553 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5554 it->len = 1;
5555 }
5556 }
5557 else
5558 {
5559 /* Get the next character from a Lisp string that is not an
5560 overlay string. Such strings come from the mode line, for
5561 example. We may have to pad with spaces, or truncate the
5562 string. See also next_element_from_c_string. */
5563 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5564 {
5565 it->what = IT_EOB;
5566 return 0;
5567 }
5568 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5569 {
5570 /* Pad with spaces. */
5571 it->c = ' ', it->len = 1;
5572 CHARPOS (position) = BYTEPOS (position) = -1;
5573 }
5574 else if (STRING_MULTIBYTE (it->string))
5575 {
5576 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5577 const unsigned char *s = (SDATA (it->string)
5578 + IT_STRING_BYTEPOS (*it));
5579 it->c = string_char_and_length (s, maxlen, &it->len);
5580 }
5581 else
5582 {
5583 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5584 it->len = 1;
5585 }
5586 }
5587
5588 /* Record what we have and where it came from. Note that we store a
5589 buffer position in IT->position although it could arguably be a
5590 string position. */
5591 it->what = IT_CHARACTER;
5592 it->object = it->string;
5593 it->position = position;
5594 return 1;
5595 }
5596
5597
5598 /* Load IT with next display element from C string IT->s.
5599 IT->string_nchars is the maximum number of characters to return
5600 from the string. IT->end_charpos may be greater than
5601 IT->string_nchars when this function is called, in which case we
5602 may have to return padding spaces. Value is zero if end of string
5603 reached, including padding spaces. */
5604
5605 static int
5606 next_element_from_c_string (it)
5607 struct it *it;
5608 {
5609 int success_p = 1;
5610
5611 xassert (it->s);
5612 it->what = IT_CHARACTER;
5613 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5614 it->object = Qnil;
5615
5616 /* IT's position can be greater IT->string_nchars in case a field
5617 width or precision has been specified when the iterator was
5618 initialized. */
5619 if (IT_CHARPOS (*it) >= it->end_charpos)
5620 {
5621 /* End of the game. */
5622 it->what = IT_EOB;
5623 success_p = 0;
5624 }
5625 else if (IT_CHARPOS (*it) >= it->string_nchars)
5626 {
5627 /* Pad with spaces. */
5628 it->c = ' ', it->len = 1;
5629 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5630 }
5631 else if (it->multibyte_p)
5632 {
5633 /* Implementation note: The calls to strlen apparently aren't a
5634 performance problem because there is no noticeable performance
5635 difference between Emacs running in unibyte or multibyte mode. */
5636 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5637 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5638 maxlen, &it->len);
5639 }
5640 else
5641 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5642
5643 return success_p;
5644 }
5645
5646
5647 /* Set up IT to return characters from an ellipsis, if appropriate.
5648 The definition of the ellipsis glyphs may come from a display table
5649 entry. This function Fills IT with the first glyph from the
5650 ellipsis if an ellipsis is to be displayed. */
5651
5652 static int
5653 next_element_from_ellipsis (it)
5654 struct it *it;
5655 {
5656 if (it->selective_display_ellipsis_p)
5657 setup_for_ellipsis (it, it->len);
5658 else
5659 {
5660 /* The face at the current position may be different from the
5661 face we find after the invisible text. Remember what it
5662 was in IT->saved_face_id, and signal that it's there by
5663 setting face_before_selective_p. */
5664 it->saved_face_id = it->face_id;
5665 it->method = GET_FROM_BUFFER;
5666 reseat_at_next_visible_line_start (it, 1);
5667 it->face_before_selective_p = 1;
5668 }
5669
5670 return get_next_display_element (it);
5671 }
5672
5673
5674 /* Deliver an image display element. The iterator IT is already
5675 filled with image information (done in handle_display_prop). Value
5676 is always 1. */
5677
5678
5679 static int
5680 next_element_from_image (it)
5681 struct it *it;
5682 {
5683 it->what = IT_IMAGE;
5684 return 1;
5685 }
5686
5687
5688 /* Fill iterator IT with next display element from a stretch glyph
5689 property. IT->object is the value of the text property. Value is
5690 always 1. */
5691
5692 static int
5693 next_element_from_stretch (it)
5694 struct it *it;
5695 {
5696 it->what = IT_STRETCH;
5697 return 1;
5698 }
5699
5700
5701 /* Load IT with the next display element from current_buffer. Value
5702 is zero if end of buffer reached. IT->stop_charpos is the next
5703 position at which to stop and check for text properties or buffer
5704 end. */
5705
5706 static int
5707 next_element_from_buffer (it)
5708 struct it *it;
5709 {
5710 int success_p = 1;
5711
5712 /* Check this assumption, otherwise, we would never enter the
5713 if-statement, below. */
5714 xassert (IT_CHARPOS (*it) >= BEGV
5715 && IT_CHARPOS (*it) <= it->stop_charpos);
5716
5717 if (IT_CHARPOS (*it) >= it->stop_charpos)
5718 {
5719 if (IT_CHARPOS (*it) >= it->end_charpos)
5720 {
5721 int overlay_strings_follow_p;
5722
5723 /* End of the game, except when overlay strings follow that
5724 haven't been returned yet. */
5725 if (it->overlay_strings_at_end_processed_p)
5726 overlay_strings_follow_p = 0;
5727 else
5728 {
5729 it->overlay_strings_at_end_processed_p = 1;
5730 overlay_strings_follow_p = get_overlay_strings (it, 0);
5731 }
5732
5733 if (overlay_strings_follow_p)
5734 success_p = get_next_display_element (it);
5735 else
5736 {
5737 it->what = IT_EOB;
5738 it->position = it->current.pos;
5739 success_p = 0;
5740 }
5741 }
5742 else
5743 {
5744 handle_stop (it);
5745 return get_next_display_element (it);
5746 }
5747 }
5748 else
5749 {
5750 /* No face changes, overlays etc. in sight, so just return a
5751 character from current_buffer. */
5752 unsigned char *p;
5753
5754 /* Maybe run the redisplay end trigger hook. Performance note:
5755 This doesn't seem to cost measurable time. */
5756 if (it->redisplay_end_trigger_charpos
5757 && it->glyph_row
5758 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5759 run_redisplay_end_trigger_hook (it);
5760
5761 /* Get the next character, maybe multibyte. */
5762 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5763 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5764 {
5765 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5766 - IT_BYTEPOS (*it));
5767 it->c = string_char_and_length (p, maxlen, &it->len);
5768 }
5769 else
5770 it->c = *p, it->len = 1;
5771
5772 /* Record what we have and where it came from. */
5773 it->what = IT_CHARACTER;;
5774 it->object = it->w->buffer;
5775 it->position = it->current.pos;
5776
5777 /* Normally we return the character found above, except when we
5778 really want to return an ellipsis for selective display. */
5779 if (it->selective)
5780 {
5781 if (it->c == '\n')
5782 {
5783 /* A value of selective > 0 means hide lines indented more
5784 than that number of columns. */
5785 if (it->selective > 0
5786 && IT_CHARPOS (*it) + 1 < ZV
5787 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5788 IT_BYTEPOS (*it) + 1,
5789 (double) it->selective)) /* iftc */
5790 {
5791 success_p = next_element_from_ellipsis (it);
5792 it->dpvec_char_len = -1;
5793 }
5794 }
5795 else if (it->c == '\r' && it->selective == -1)
5796 {
5797 /* A value of selective == -1 means that everything from the
5798 CR to the end of the line is invisible, with maybe an
5799 ellipsis displayed for it. */
5800 success_p = next_element_from_ellipsis (it);
5801 it->dpvec_char_len = -1;
5802 }
5803 }
5804 }
5805
5806 /* Value is zero if end of buffer reached. */
5807 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5808 return success_p;
5809 }
5810
5811
5812 /* Run the redisplay end trigger hook for IT. */
5813
5814 static void
5815 run_redisplay_end_trigger_hook (it)
5816 struct it *it;
5817 {
5818 Lisp_Object args[3];
5819
5820 /* IT->glyph_row should be non-null, i.e. we should be actually
5821 displaying something, or otherwise we should not run the hook. */
5822 xassert (it->glyph_row);
5823
5824 /* Set up hook arguments. */
5825 args[0] = Qredisplay_end_trigger_functions;
5826 args[1] = it->window;
5827 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5828 it->redisplay_end_trigger_charpos = 0;
5829
5830 /* Since we are *trying* to run these functions, don't try to run
5831 them again, even if they get an error. */
5832 it->w->redisplay_end_trigger = Qnil;
5833 Frun_hook_with_args (3, args);
5834
5835 /* Notice if it changed the face of the character we are on. */
5836 handle_face_prop (it);
5837 }
5838
5839
5840 /* Deliver a composition display element. The iterator IT is already
5841 filled with composition information (done in
5842 handle_composition_prop). Value is always 1. */
5843
5844 static int
5845 next_element_from_composition (it)
5846 struct it *it;
5847 {
5848 it->what = IT_COMPOSITION;
5849 it->position = (STRINGP (it->string)
5850 ? it->current.string_pos
5851 : it->current.pos);
5852 return 1;
5853 }
5854
5855
5856 \f
5857 /***********************************************************************
5858 Moving an iterator without producing glyphs
5859 ***********************************************************************/
5860
5861 /* Move iterator IT to a specified buffer or X position within one
5862 line on the display without producing glyphs.
5863
5864 OP should be a bit mask including some or all of these bits:
5865 MOVE_TO_X: Stop on reaching x-position TO_X.
5866 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5867 Regardless of OP's value, stop in reaching the end of the display line.
5868
5869 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5870 This means, in particular, that TO_X includes window's horizontal
5871 scroll amount.
5872
5873 The return value has several possible values that
5874 say what condition caused the scan to stop:
5875
5876 MOVE_POS_MATCH_OR_ZV
5877 - when TO_POS or ZV was reached.
5878
5879 MOVE_X_REACHED
5880 -when TO_X was reached before TO_POS or ZV were reached.
5881
5882 MOVE_LINE_CONTINUED
5883 - when we reached the end of the display area and the line must
5884 be continued.
5885
5886 MOVE_LINE_TRUNCATED
5887 - when we reached the end of the display area and the line is
5888 truncated.
5889
5890 MOVE_NEWLINE_OR_CR
5891 - when we stopped at a line end, i.e. a newline or a CR and selective
5892 display is on. */
5893
5894 static enum move_it_result
5895 move_it_in_display_line_to (it, to_charpos, to_x, op)
5896 struct it *it;
5897 int to_charpos, to_x, op;
5898 {
5899 enum move_it_result result = MOVE_UNDEFINED;
5900 struct glyph_row *saved_glyph_row;
5901
5902 /* Don't produce glyphs in produce_glyphs. */
5903 saved_glyph_row = it->glyph_row;
5904 it->glyph_row = NULL;
5905
5906 #define BUFFER_POS_REACHED_P() \
5907 ((op & MOVE_TO_POS) != 0 \
5908 && BUFFERP (it->object) \
5909 && IT_CHARPOS (*it) >= to_charpos \
5910 && (it->method == GET_FROM_BUFFER \
5911 || (it->method == GET_FROM_DISPLAY_VECTOR \
5912 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5913
5914
5915 while (1)
5916 {
5917 int x, i, ascent = 0, descent = 0;
5918
5919 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5920 if ((op & MOVE_TO_POS) != 0
5921 && BUFFERP (it->object)
5922 && it->method == GET_FROM_BUFFER
5923 && IT_CHARPOS (*it) > to_charpos)
5924 {
5925 result = MOVE_POS_MATCH_OR_ZV;
5926 break;
5927 }
5928
5929 /* Stop when ZV reached.
5930 We used to stop here when TO_CHARPOS reached as well, but that is
5931 too soon if this glyph does not fit on this line. So we handle it
5932 explicitly below. */
5933 if (!get_next_display_element (it)
5934 || (it->truncate_lines_p
5935 && BUFFER_POS_REACHED_P ()))
5936 {
5937 result = MOVE_POS_MATCH_OR_ZV;
5938 break;
5939 }
5940
5941 /* The call to produce_glyphs will get the metrics of the
5942 display element IT is loaded with. We record in x the
5943 x-position before this display element in case it does not
5944 fit on the line. */
5945 x = it->current_x;
5946
5947 /* Remember the line height so far in case the next element doesn't
5948 fit on the line. */
5949 if (!it->truncate_lines_p)
5950 {
5951 ascent = it->max_ascent;
5952 descent = it->max_descent;
5953 }
5954
5955 PRODUCE_GLYPHS (it);
5956
5957 if (it->area != TEXT_AREA)
5958 {
5959 set_iterator_to_next (it, 1);
5960 continue;
5961 }
5962
5963 /* The number of glyphs we get back in IT->nglyphs will normally
5964 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5965 character on a terminal frame, or (iii) a line end. For the
5966 second case, IT->nglyphs - 1 padding glyphs will be present
5967 (on X frames, there is only one glyph produced for a
5968 composite character.
5969
5970 The behavior implemented below means, for continuation lines,
5971 that as many spaces of a TAB as fit on the current line are
5972 displayed there. For terminal frames, as many glyphs of a
5973 multi-glyph character are displayed in the current line, too.
5974 This is what the old redisplay code did, and we keep it that
5975 way. Under X, the whole shape of a complex character must
5976 fit on the line or it will be completely displayed in the
5977 next line.
5978
5979 Note that both for tabs and padding glyphs, all glyphs have
5980 the same width. */
5981 if (it->nglyphs)
5982 {
5983 /* More than one glyph or glyph doesn't fit on line. All
5984 glyphs have the same width. */
5985 int single_glyph_width = it->pixel_width / it->nglyphs;
5986 int new_x;
5987
5988 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5989 {
5990 new_x = x + single_glyph_width;
5991
5992 /* We want to leave anything reaching TO_X to the caller. */
5993 if ((op & MOVE_TO_X) && new_x > to_x)
5994 {
5995 if (BUFFER_POS_REACHED_P ())
5996 goto buffer_pos_reached;
5997 it->current_x = x;
5998 result = MOVE_X_REACHED;
5999 break;
6000 }
6001 else if (/* Lines are continued. */
6002 !it->truncate_lines_p
6003 && (/* And glyph doesn't fit on the line. */
6004 new_x > it->last_visible_x
6005 /* Or it fits exactly and we're on a window
6006 system frame. */
6007 || (new_x == it->last_visible_x
6008 && FRAME_WINDOW_P (it->f))))
6009 {
6010 if (/* IT->hpos == 0 means the very first glyph
6011 doesn't fit on the line, e.g. a wide image. */
6012 it->hpos == 0
6013 || (new_x == it->last_visible_x
6014 && FRAME_WINDOW_P (it->f)))
6015 {
6016 ++it->hpos;
6017 it->current_x = new_x;
6018 if (i == it->nglyphs - 1)
6019 {
6020 set_iterator_to_next (it, 1);
6021 #ifdef HAVE_WINDOW_SYSTEM
6022 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6023 {
6024 if (!get_next_display_element (it))
6025 {
6026 result = MOVE_POS_MATCH_OR_ZV;
6027 break;
6028 }
6029 if (BUFFER_POS_REACHED_P ())
6030 {
6031 if (ITERATOR_AT_END_OF_LINE_P (it))
6032 result = MOVE_POS_MATCH_OR_ZV;
6033 else
6034 result = MOVE_LINE_CONTINUED;
6035 break;
6036 }
6037 if (ITERATOR_AT_END_OF_LINE_P (it))
6038 {
6039 result = MOVE_NEWLINE_OR_CR;
6040 break;
6041 }
6042 }
6043 #endif /* HAVE_WINDOW_SYSTEM */
6044 }
6045 }
6046 else
6047 {
6048 it->current_x = x;
6049 it->max_ascent = ascent;
6050 it->max_descent = descent;
6051 }
6052
6053 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6054 IT_CHARPOS (*it)));
6055 result = MOVE_LINE_CONTINUED;
6056 break;
6057 }
6058 else if (BUFFER_POS_REACHED_P ())
6059 goto buffer_pos_reached;
6060 else if (new_x > it->first_visible_x)
6061 {
6062 /* Glyph is visible. Increment number of glyphs that
6063 would be displayed. */
6064 ++it->hpos;
6065 }
6066 else
6067 {
6068 /* Glyph is completely off the left margin of the display
6069 area. Nothing to do. */
6070 }
6071 }
6072
6073 if (result != MOVE_UNDEFINED)
6074 break;
6075 }
6076 else if (BUFFER_POS_REACHED_P ())
6077 {
6078 buffer_pos_reached:
6079 it->current_x = x;
6080 it->max_ascent = ascent;
6081 it->max_descent = descent;
6082 result = MOVE_POS_MATCH_OR_ZV;
6083 break;
6084 }
6085 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6086 {
6087 /* Stop when TO_X specified and reached. This check is
6088 necessary here because of lines consisting of a line end,
6089 only. The line end will not produce any glyphs and we
6090 would never get MOVE_X_REACHED. */
6091 xassert (it->nglyphs == 0);
6092 result = MOVE_X_REACHED;
6093 break;
6094 }
6095
6096 /* Is this a line end? If yes, we're done. */
6097 if (ITERATOR_AT_END_OF_LINE_P (it))
6098 {
6099 result = MOVE_NEWLINE_OR_CR;
6100 break;
6101 }
6102
6103 /* The current display element has been consumed. Advance
6104 to the next. */
6105 set_iterator_to_next (it, 1);
6106
6107 /* Stop if lines are truncated and IT's current x-position is
6108 past the right edge of the window now. */
6109 if (it->truncate_lines_p
6110 && it->current_x >= it->last_visible_x)
6111 {
6112 #ifdef HAVE_WINDOW_SYSTEM
6113 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6114 {
6115 if (!get_next_display_element (it)
6116 || BUFFER_POS_REACHED_P ())
6117 {
6118 result = MOVE_POS_MATCH_OR_ZV;
6119 break;
6120 }
6121 if (ITERATOR_AT_END_OF_LINE_P (it))
6122 {
6123 result = MOVE_NEWLINE_OR_CR;
6124 break;
6125 }
6126 }
6127 #endif /* HAVE_WINDOW_SYSTEM */
6128 result = MOVE_LINE_TRUNCATED;
6129 break;
6130 }
6131 }
6132
6133 #undef BUFFER_POS_REACHED_P
6134
6135 /* Restore the iterator settings altered at the beginning of this
6136 function. */
6137 it->glyph_row = saved_glyph_row;
6138 return result;
6139 }
6140
6141
6142 /* Move IT forward until it satisfies one or more of the criteria in
6143 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6144
6145 OP is a bit-mask that specifies where to stop, and in particular,
6146 which of those four position arguments makes a difference. See the
6147 description of enum move_operation_enum.
6148
6149 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6150 screen line, this function will set IT to the next position >
6151 TO_CHARPOS. */
6152
6153 void
6154 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6155 struct it *it;
6156 int to_charpos, to_x, to_y, to_vpos;
6157 int op;
6158 {
6159 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6160 int line_height;
6161 int reached = 0;
6162
6163 for (;;)
6164 {
6165 if (op & MOVE_TO_VPOS)
6166 {
6167 /* If no TO_CHARPOS and no TO_X specified, stop at the
6168 start of the line TO_VPOS. */
6169 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6170 {
6171 if (it->vpos == to_vpos)
6172 {
6173 reached = 1;
6174 break;
6175 }
6176 else
6177 skip = move_it_in_display_line_to (it, -1, -1, 0);
6178 }
6179 else
6180 {
6181 /* TO_VPOS >= 0 means stop at TO_X in the line at
6182 TO_VPOS, or at TO_POS, whichever comes first. */
6183 if (it->vpos == to_vpos)
6184 {
6185 reached = 2;
6186 break;
6187 }
6188
6189 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6190
6191 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6192 {
6193 reached = 3;
6194 break;
6195 }
6196 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6197 {
6198 /* We have reached TO_X but not in the line we want. */
6199 skip = move_it_in_display_line_to (it, to_charpos,
6200 -1, MOVE_TO_POS);
6201 if (skip == MOVE_POS_MATCH_OR_ZV)
6202 {
6203 reached = 4;
6204 break;
6205 }
6206 }
6207 }
6208 }
6209 else if (op & MOVE_TO_Y)
6210 {
6211 struct it it_backup;
6212
6213 /* TO_Y specified means stop at TO_X in the line containing
6214 TO_Y---or at TO_CHARPOS if this is reached first. The
6215 problem is that we can't really tell whether the line
6216 contains TO_Y before we have completely scanned it, and
6217 this may skip past TO_X. What we do is to first scan to
6218 TO_X.
6219
6220 If TO_X is not specified, use a TO_X of zero. The reason
6221 is to make the outcome of this function more predictable.
6222 If we didn't use TO_X == 0, we would stop at the end of
6223 the line which is probably not what a caller would expect
6224 to happen. */
6225 skip = move_it_in_display_line_to (it, to_charpos,
6226 ((op & MOVE_TO_X)
6227 ? to_x : 0),
6228 (MOVE_TO_X
6229 | (op & MOVE_TO_POS)));
6230
6231 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6232 if (skip == MOVE_POS_MATCH_OR_ZV)
6233 {
6234 reached = 5;
6235 break;
6236 }
6237
6238 /* If TO_X was reached, we would like to know whether TO_Y
6239 is in the line. This can only be said if we know the
6240 total line height which requires us to scan the rest of
6241 the line. */
6242 if (skip == MOVE_X_REACHED)
6243 {
6244 it_backup = *it;
6245 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6246 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6247 op & MOVE_TO_POS);
6248 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6249 }
6250
6251 /* Now, decide whether TO_Y is in this line. */
6252 line_height = it->max_ascent + it->max_descent;
6253 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6254
6255 if (to_y >= it->current_y
6256 && to_y < it->current_y + line_height)
6257 {
6258 if (skip == MOVE_X_REACHED)
6259 /* If TO_Y is in this line and TO_X was reached above,
6260 we scanned too far. We have to restore IT's settings
6261 to the ones before skipping. */
6262 *it = it_backup;
6263 reached = 6;
6264 }
6265 else if (skip == MOVE_X_REACHED)
6266 {
6267 skip = skip2;
6268 if (skip == MOVE_POS_MATCH_OR_ZV)
6269 reached = 7;
6270 }
6271
6272 if (reached)
6273 break;
6274 }
6275 else
6276 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6277
6278 switch (skip)
6279 {
6280 case MOVE_POS_MATCH_OR_ZV:
6281 reached = 8;
6282 goto out;
6283
6284 case MOVE_NEWLINE_OR_CR:
6285 set_iterator_to_next (it, 1);
6286 it->continuation_lines_width = 0;
6287 break;
6288
6289 case MOVE_LINE_TRUNCATED:
6290 it->continuation_lines_width = 0;
6291 reseat_at_next_visible_line_start (it, 0);
6292 if ((op & MOVE_TO_POS) != 0
6293 && IT_CHARPOS (*it) > to_charpos)
6294 {
6295 reached = 9;
6296 goto out;
6297 }
6298 break;
6299
6300 case MOVE_LINE_CONTINUED:
6301 it->continuation_lines_width += it->current_x;
6302 break;
6303
6304 default:
6305 abort ();
6306 }
6307
6308 /* Reset/increment for the next run. */
6309 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6310 it->current_x = it->hpos = 0;
6311 it->current_y += it->max_ascent + it->max_descent;
6312 ++it->vpos;
6313 last_height = it->max_ascent + it->max_descent;
6314 last_max_ascent = it->max_ascent;
6315 it->max_ascent = it->max_descent = 0;
6316 }
6317
6318 out:
6319
6320 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6321 }
6322
6323
6324 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6325
6326 If DY > 0, move IT backward at least that many pixels. DY = 0
6327 means move IT backward to the preceding line start or BEGV. This
6328 function may move over more than DY pixels if IT->current_y - DY
6329 ends up in the middle of a line; in this case IT->current_y will be
6330 set to the top of the line moved to. */
6331
6332 void
6333 move_it_vertically_backward (it, dy)
6334 struct it *it;
6335 int dy;
6336 {
6337 int nlines, h;
6338 struct it it2, it3;
6339 int start_pos;
6340
6341 move_further_back:
6342 xassert (dy >= 0);
6343
6344 start_pos = IT_CHARPOS (*it);
6345
6346 /* Estimate how many newlines we must move back. */
6347 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6348
6349 /* Set the iterator's position that many lines back. */
6350 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6351 back_to_previous_visible_line_start (it);
6352
6353 /* Reseat the iterator here. When moving backward, we don't want
6354 reseat to skip forward over invisible text, set up the iterator
6355 to deliver from overlay strings at the new position etc. So,
6356 use reseat_1 here. */
6357 reseat_1 (it, it->current.pos, 1);
6358
6359 /* We are now surely at a line start. */
6360 it->current_x = it->hpos = 0;
6361 it->continuation_lines_width = 0;
6362
6363 /* Move forward and see what y-distance we moved. First move to the
6364 start of the next line so that we get its height. We need this
6365 height to be able to tell whether we reached the specified
6366 y-distance. */
6367 it2 = *it;
6368 it2.max_ascent = it2.max_descent = 0;
6369 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6370 MOVE_TO_POS | MOVE_TO_VPOS);
6371 xassert (IT_CHARPOS (*it) >= BEGV);
6372 it3 = it2;
6373
6374 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6375 xassert (IT_CHARPOS (*it) >= BEGV);
6376 /* H is the actual vertical distance from the position in *IT
6377 and the starting position. */
6378 h = it2.current_y - it->current_y;
6379 /* NLINES is the distance in number of lines. */
6380 nlines = it2.vpos - it->vpos;
6381
6382 /* Correct IT's y and vpos position
6383 so that they are relative to the starting point. */
6384 it->vpos -= nlines;
6385 it->current_y -= h;
6386
6387 if (dy == 0)
6388 {
6389 /* DY == 0 means move to the start of the screen line. The
6390 value of nlines is > 0 if continuation lines were involved. */
6391 if (nlines > 0)
6392 move_it_by_lines (it, nlines, 1);
6393 #if 0
6394 /* I think this assert is bogus if buffer contains
6395 invisible text or images. KFS. */
6396 xassert (IT_CHARPOS (*it) <= start_pos);
6397 #endif
6398 }
6399 else
6400 {
6401 /* The y-position we try to reach, relative to *IT.
6402 Note that H has been subtracted in front of the if-statement. */
6403 int target_y = it->current_y + h - dy;
6404 int y0 = it3.current_y;
6405 int y1 = line_bottom_y (&it3);
6406 int line_height = y1 - y0;
6407
6408 /* If we did not reach target_y, try to move further backward if
6409 we can. If we moved too far backward, try to move forward. */
6410 if (target_y < it->current_y
6411 /* This is heuristic. In a window that's 3 lines high, with
6412 a line height of 13 pixels each, recentering with point
6413 on the bottom line will try to move -39/2 = 19 pixels
6414 backward. Try to avoid moving into the first line. */
6415 && (it->current_y - target_y
6416 > min (window_box_height (it->w), line_height * 2 / 3))
6417 && IT_CHARPOS (*it) > BEGV)
6418 {
6419 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6420 target_y - it->current_y));
6421 dy = it->current_y - target_y;
6422 goto move_further_back;
6423 }
6424 else if (target_y >= it->current_y + line_height
6425 && IT_CHARPOS (*it) < ZV)
6426 {
6427 /* Should move forward by at least one line, maybe more.
6428
6429 Note: Calling move_it_by_lines can be expensive on
6430 terminal frames, where compute_motion is used (via
6431 vmotion) to do the job, when there are very long lines
6432 and truncate-lines is nil. That's the reason for
6433 treating terminal frames specially here. */
6434
6435 if (!FRAME_WINDOW_P (it->f))
6436 move_it_vertically (it, target_y - (it->current_y + line_height));
6437 else
6438 {
6439 do
6440 {
6441 move_it_by_lines (it, 1, 1);
6442 }
6443 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6444 }
6445
6446 #if 0
6447 /* I think this assert is bogus if buffer contains
6448 invisible text or images. KFS. */
6449 xassert (IT_CHARPOS (*it) >= BEGV);
6450 #endif
6451 }
6452 }
6453 }
6454
6455
6456 /* Move IT by a specified amount of pixel lines DY. DY negative means
6457 move backwards. DY = 0 means move to start of screen line. At the
6458 end, IT will be on the start of a screen line. */
6459
6460 void
6461 move_it_vertically (it, dy)
6462 struct it *it;
6463 int dy;
6464 {
6465 if (dy <= 0)
6466 move_it_vertically_backward (it, -dy);
6467 else
6468 {
6469 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6470 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6471 MOVE_TO_POS | MOVE_TO_Y);
6472 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6473
6474 /* If buffer ends in ZV without a newline, move to the start of
6475 the line to satisfy the post-condition. */
6476 if (IT_CHARPOS (*it) == ZV
6477 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6478 move_it_by_lines (it, 0, 0);
6479 }
6480 }
6481
6482
6483 /* Move iterator IT past the end of the text line it is in. */
6484
6485 void
6486 move_it_past_eol (it)
6487 struct it *it;
6488 {
6489 enum move_it_result rc;
6490
6491 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6492 if (rc == MOVE_NEWLINE_OR_CR)
6493 set_iterator_to_next (it, 0);
6494 }
6495
6496
6497 #if 0 /* Currently not used. */
6498
6499 /* Return non-zero if some text between buffer positions START_CHARPOS
6500 and END_CHARPOS is invisible. IT->window is the window for text
6501 property lookup. */
6502
6503 static int
6504 invisible_text_between_p (it, start_charpos, end_charpos)
6505 struct it *it;
6506 int start_charpos, end_charpos;
6507 {
6508 Lisp_Object prop, limit;
6509 int invisible_found_p;
6510
6511 xassert (it != NULL && start_charpos <= end_charpos);
6512
6513 /* Is text at START invisible? */
6514 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6515 it->window);
6516 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6517 invisible_found_p = 1;
6518 else
6519 {
6520 limit = Fnext_single_char_property_change (make_number (start_charpos),
6521 Qinvisible, Qnil,
6522 make_number (end_charpos));
6523 invisible_found_p = XFASTINT (limit) < end_charpos;
6524 }
6525
6526 return invisible_found_p;
6527 }
6528
6529 #endif /* 0 */
6530
6531
6532 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6533 negative means move up. DVPOS == 0 means move to the start of the
6534 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6535 NEED_Y_P is zero, IT->current_y will be left unchanged.
6536
6537 Further optimization ideas: If we would know that IT->f doesn't use
6538 a face with proportional font, we could be faster for
6539 truncate-lines nil. */
6540
6541 void
6542 move_it_by_lines (it, dvpos, need_y_p)
6543 struct it *it;
6544 int dvpos, need_y_p;
6545 {
6546 struct position pos;
6547
6548 if (!FRAME_WINDOW_P (it->f))
6549 {
6550 struct text_pos textpos;
6551
6552 /* We can use vmotion on frames without proportional fonts. */
6553 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6554 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6555 reseat (it, textpos, 1);
6556 it->vpos += pos.vpos;
6557 it->current_y += pos.vpos;
6558 }
6559 else if (dvpos == 0)
6560 {
6561 /* DVPOS == 0 means move to the start of the screen line. */
6562 move_it_vertically_backward (it, 0);
6563 xassert (it->current_x == 0 && it->hpos == 0);
6564 /* Let next call to line_bottom_y calculate real line height */
6565 last_height = 0;
6566 }
6567 else if (dvpos > 0)
6568 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6569 else
6570 {
6571 struct it it2;
6572 int start_charpos, i;
6573
6574 /* Start at the beginning of the screen line containing IT's
6575 position. */
6576 move_it_vertically_backward (it, 0);
6577
6578 /* Go back -DVPOS visible lines and reseat the iterator there. */
6579 start_charpos = IT_CHARPOS (*it);
6580 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6581 back_to_previous_visible_line_start (it);
6582 reseat (it, it->current.pos, 1);
6583 it->current_x = it->hpos = 0;
6584
6585 /* Above call may have moved too far if continuation lines
6586 are involved. Scan forward and see if it did. */
6587 it2 = *it;
6588 it2.vpos = it2.current_y = 0;
6589 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6590 it->vpos -= it2.vpos;
6591 it->current_y -= it2.current_y;
6592 it->current_x = it->hpos = 0;
6593
6594 /* If we moved too far back, move IT some lines forward. */
6595 if (it2.vpos > -dvpos)
6596 {
6597 int delta = it2.vpos + dvpos;
6598 it2 = *it;
6599 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6600 /* Move back again if we got too far ahead. */
6601 if (IT_CHARPOS (*it) >= start_charpos)
6602 *it = it2;
6603 }
6604 }
6605 }
6606
6607 /* Return 1 if IT points into the middle of a display vector. */
6608
6609 int
6610 in_display_vector_p (it)
6611 struct it *it;
6612 {
6613 return (it->method == GET_FROM_DISPLAY_VECTOR
6614 && it->current.dpvec_index > 0
6615 && it->dpvec + it->current.dpvec_index != it->dpend);
6616 }
6617
6618 \f
6619 /***********************************************************************
6620 Messages
6621 ***********************************************************************/
6622
6623
6624 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6625 to *Messages*. */
6626
6627 void
6628 add_to_log (format, arg1, arg2)
6629 char *format;
6630 Lisp_Object arg1, arg2;
6631 {
6632 Lisp_Object args[3];
6633 Lisp_Object msg, fmt;
6634 char *buffer;
6635 int len;
6636 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6637 USE_SAFE_ALLOCA;
6638
6639 /* Do nothing if called asynchronously. Inserting text into
6640 a buffer may call after-change-functions and alike and
6641 that would means running Lisp asynchronously. */
6642 if (handling_signal)
6643 return;
6644
6645 fmt = msg = Qnil;
6646 GCPRO4 (fmt, msg, arg1, arg2);
6647
6648 args[0] = fmt = build_string (format);
6649 args[1] = arg1;
6650 args[2] = arg2;
6651 msg = Fformat (3, args);
6652
6653 len = SBYTES (msg) + 1;
6654 SAFE_ALLOCA (buffer, char *, len);
6655 bcopy (SDATA (msg), buffer, len);
6656
6657 message_dolog (buffer, len - 1, 1, 0);
6658 SAFE_FREE ();
6659
6660 UNGCPRO;
6661 }
6662
6663
6664 /* Output a newline in the *Messages* buffer if "needs" one. */
6665
6666 void
6667 message_log_maybe_newline ()
6668 {
6669 if (message_log_need_newline)
6670 message_dolog ("", 0, 1, 0);
6671 }
6672
6673
6674 /* Add a string M of length NBYTES to the message log, optionally
6675 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6676 nonzero, means interpret the contents of M as multibyte. This
6677 function calls low-level routines in order to bypass text property
6678 hooks, etc. which might not be safe to run. */
6679
6680 void
6681 message_dolog (m, nbytes, nlflag, multibyte)
6682 const char *m;
6683 int nbytes, nlflag, multibyte;
6684 {
6685 if (!NILP (Vmemory_full))
6686 return;
6687
6688 if (!NILP (Vmessage_log_max))
6689 {
6690 struct buffer *oldbuf;
6691 Lisp_Object oldpoint, oldbegv, oldzv;
6692 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6693 int point_at_end = 0;
6694 int zv_at_end = 0;
6695 Lisp_Object old_deactivate_mark, tem;
6696 struct gcpro gcpro1;
6697
6698 old_deactivate_mark = Vdeactivate_mark;
6699 oldbuf = current_buffer;
6700 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6701 current_buffer->undo_list = Qt;
6702
6703 oldpoint = message_dolog_marker1;
6704 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6705 oldbegv = message_dolog_marker2;
6706 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6707 oldzv = message_dolog_marker3;
6708 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6709 GCPRO1 (old_deactivate_mark);
6710
6711 if (PT == Z)
6712 point_at_end = 1;
6713 if (ZV == Z)
6714 zv_at_end = 1;
6715
6716 BEGV = BEG;
6717 BEGV_BYTE = BEG_BYTE;
6718 ZV = Z;
6719 ZV_BYTE = Z_BYTE;
6720 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6721
6722 /* Insert the string--maybe converting multibyte to single byte
6723 or vice versa, so that all the text fits the buffer. */
6724 if (multibyte
6725 && NILP (current_buffer->enable_multibyte_characters))
6726 {
6727 int i, c, char_bytes;
6728 unsigned char work[1];
6729
6730 /* Convert a multibyte string to single-byte
6731 for the *Message* buffer. */
6732 for (i = 0; i < nbytes; i += char_bytes)
6733 {
6734 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6735 work[0] = (SINGLE_BYTE_CHAR_P (c)
6736 ? c
6737 : multibyte_char_to_unibyte (c, Qnil));
6738 insert_1_both (work, 1, 1, 1, 0, 0);
6739 }
6740 }
6741 else if (! multibyte
6742 && ! NILP (current_buffer->enable_multibyte_characters))
6743 {
6744 int i, c, char_bytes;
6745 unsigned char *msg = (unsigned char *) m;
6746 unsigned char str[MAX_MULTIBYTE_LENGTH];
6747 /* Convert a single-byte string to multibyte
6748 for the *Message* buffer. */
6749 for (i = 0; i < nbytes; i++)
6750 {
6751 c = unibyte_char_to_multibyte (msg[i]);
6752 char_bytes = CHAR_STRING (c, str);
6753 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6754 }
6755 }
6756 else if (nbytes)
6757 insert_1 (m, nbytes, 1, 0, 0);
6758
6759 if (nlflag)
6760 {
6761 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6762 insert_1 ("\n", 1, 1, 0, 0);
6763
6764 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6765 this_bol = PT;
6766 this_bol_byte = PT_BYTE;
6767
6768 /* See if this line duplicates the previous one.
6769 If so, combine duplicates. */
6770 if (this_bol > BEG)
6771 {
6772 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6773 prev_bol = PT;
6774 prev_bol_byte = PT_BYTE;
6775
6776 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6777 this_bol, this_bol_byte);
6778 if (dup)
6779 {
6780 del_range_both (prev_bol, prev_bol_byte,
6781 this_bol, this_bol_byte, 0);
6782 if (dup > 1)
6783 {
6784 char dupstr[40];
6785 int duplen;
6786
6787 /* If you change this format, don't forget to also
6788 change message_log_check_duplicate. */
6789 sprintf (dupstr, " [%d times]", dup);
6790 duplen = strlen (dupstr);
6791 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6792 insert_1 (dupstr, duplen, 1, 0, 1);
6793 }
6794 }
6795 }
6796
6797 /* If we have more than the desired maximum number of lines
6798 in the *Messages* buffer now, delete the oldest ones.
6799 This is safe because we don't have undo in this buffer. */
6800
6801 if (NATNUMP (Vmessage_log_max))
6802 {
6803 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6804 -XFASTINT (Vmessage_log_max) - 1, 0);
6805 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6806 }
6807 }
6808 BEGV = XMARKER (oldbegv)->charpos;
6809 BEGV_BYTE = marker_byte_position (oldbegv);
6810
6811 if (zv_at_end)
6812 {
6813 ZV = Z;
6814 ZV_BYTE = Z_BYTE;
6815 }
6816 else
6817 {
6818 ZV = XMARKER (oldzv)->charpos;
6819 ZV_BYTE = marker_byte_position (oldzv);
6820 }
6821
6822 if (point_at_end)
6823 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6824 else
6825 /* We can't do Fgoto_char (oldpoint) because it will run some
6826 Lisp code. */
6827 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6828 XMARKER (oldpoint)->bytepos);
6829
6830 UNGCPRO;
6831 unchain_marker (XMARKER (oldpoint));
6832 unchain_marker (XMARKER (oldbegv));
6833 unchain_marker (XMARKER (oldzv));
6834
6835 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6836 set_buffer_internal (oldbuf);
6837 if (NILP (tem))
6838 windows_or_buffers_changed = old_windows_or_buffers_changed;
6839 message_log_need_newline = !nlflag;
6840 Vdeactivate_mark = old_deactivate_mark;
6841 }
6842 }
6843
6844
6845 /* We are at the end of the buffer after just having inserted a newline.
6846 (Note: We depend on the fact we won't be crossing the gap.)
6847 Check to see if the most recent message looks a lot like the previous one.
6848 Return 0 if different, 1 if the new one should just replace it, or a
6849 value N > 1 if we should also append " [N times]". */
6850
6851 static int
6852 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6853 int prev_bol, this_bol;
6854 int prev_bol_byte, this_bol_byte;
6855 {
6856 int i;
6857 int len = Z_BYTE - 1 - this_bol_byte;
6858 int seen_dots = 0;
6859 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6860 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6861
6862 for (i = 0; i < len; i++)
6863 {
6864 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6865 seen_dots = 1;
6866 if (p1[i] != p2[i])
6867 return seen_dots;
6868 }
6869 p1 += len;
6870 if (*p1 == '\n')
6871 return 2;
6872 if (*p1++ == ' ' && *p1++ == '[')
6873 {
6874 int n = 0;
6875 while (*p1 >= '0' && *p1 <= '9')
6876 n = n * 10 + *p1++ - '0';
6877 if (strncmp (p1, " times]\n", 8) == 0)
6878 return n+1;
6879 }
6880 return 0;
6881 }
6882 \f
6883
6884 /* Display an echo area message M with a specified length of NBYTES
6885 bytes. The string may include null characters. If M is 0, clear
6886 out any existing message, and let the mini-buffer text show
6887 through.
6888
6889 The buffer M must continue to exist until after the echo area gets
6890 cleared or some other message gets displayed there. This means do
6891 not pass text that is stored in a Lisp string; do not pass text in
6892 a buffer that was alloca'd. */
6893
6894 void
6895 message2 (m, nbytes, multibyte)
6896 const char *m;
6897 int nbytes;
6898 int multibyte;
6899 {
6900 /* First flush out any partial line written with print. */
6901 message_log_maybe_newline ();
6902 if (m)
6903 message_dolog (m, nbytes, 1, multibyte);
6904 message2_nolog (m, nbytes, multibyte);
6905 }
6906
6907
6908 /* The non-logging counterpart of message2. */
6909
6910 void
6911 message2_nolog (m, nbytes, multibyte)
6912 const char *m;
6913 int nbytes, multibyte;
6914 {
6915 struct frame *sf = SELECTED_FRAME ();
6916 message_enable_multibyte = multibyte;
6917
6918 if (noninteractive)
6919 {
6920 if (noninteractive_need_newline)
6921 putc ('\n', stderr);
6922 noninteractive_need_newline = 0;
6923 if (m)
6924 fwrite (m, nbytes, 1, stderr);
6925 if (cursor_in_echo_area == 0)
6926 fprintf (stderr, "\n");
6927 fflush (stderr);
6928 }
6929 /* A null message buffer means that the frame hasn't really been
6930 initialized yet. Error messages get reported properly by
6931 cmd_error, so this must be just an informative message; toss it. */
6932 else if (INTERACTIVE
6933 && sf->glyphs_initialized_p
6934 && FRAME_MESSAGE_BUF (sf))
6935 {
6936 Lisp_Object mini_window;
6937 struct frame *f;
6938
6939 /* Get the frame containing the mini-buffer
6940 that the selected frame is using. */
6941 mini_window = FRAME_MINIBUF_WINDOW (sf);
6942 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6943
6944 FRAME_SAMPLE_VISIBILITY (f);
6945 if (FRAME_VISIBLE_P (sf)
6946 && ! FRAME_VISIBLE_P (f))
6947 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6948
6949 if (m)
6950 {
6951 set_message (m, Qnil, nbytes, multibyte);
6952 if (minibuffer_auto_raise)
6953 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6954 }
6955 else
6956 clear_message (1, 1);
6957
6958 do_pending_window_change (0);
6959 echo_area_display (1);
6960 do_pending_window_change (0);
6961 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6962 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6963 }
6964 }
6965
6966
6967 /* Display an echo area message M with a specified length of NBYTES
6968 bytes. The string may include null characters. If M is not a
6969 string, clear out any existing message, and let the mini-buffer
6970 text show through.
6971
6972 This function cancels echoing. */
6973
6974 void
6975 message3 (m, nbytes, multibyte)
6976 Lisp_Object m;
6977 int nbytes;
6978 int multibyte;
6979 {
6980 struct gcpro gcpro1;
6981
6982 GCPRO1 (m);
6983 clear_message (1,1);
6984 cancel_echoing ();
6985
6986 /* First flush out any partial line written with print. */
6987 message_log_maybe_newline ();
6988 if (STRINGP (m))
6989 message_dolog (SDATA (m), nbytes, 1, multibyte);
6990 message3_nolog (m, nbytes, multibyte);
6991
6992 UNGCPRO;
6993 }
6994
6995
6996 /* The non-logging version of message3.
6997 This does not cancel echoing, because it is used for echoing.
6998 Perhaps we need to make a separate function for echoing
6999 and make this cancel echoing. */
7000
7001 void
7002 message3_nolog (m, nbytes, multibyte)
7003 Lisp_Object m;
7004 int nbytes, multibyte;
7005 {
7006 struct frame *sf = SELECTED_FRAME ();
7007 message_enable_multibyte = multibyte;
7008
7009 if (noninteractive)
7010 {
7011 if (noninteractive_need_newline)
7012 putc ('\n', stderr);
7013 noninteractive_need_newline = 0;
7014 if (STRINGP (m))
7015 fwrite (SDATA (m), nbytes, 1, stderr);
7016 if (cursor_in_echo_area == 0)
7017 fprintf (stderr, "\n");
7018 fflush (stderr);
7019 }
7020 /* A null message buffer means that the frame hasn't really been
7021 initialized yet. Error messages get reported properly by
7022 cmd_error, so this must be just an informative message; toss it. */
7023 else if (INTERACTIVE
7024 && sf->glyphs_initialized_p
7025 && FRAME_MESSAGE_BUF (sf))
7026 {
7027 Lisp_Object mini_window;
7028 Lisp_Object frame;
7029 struct frame *f;
7030
7031 /* Get the frame containing the mini-buffer
7032 that the selected frame is using. */
7033 mini_window = FRAME_MINIBUF_WINDOW (sf);
7034 frame = XWINDOW (mini_window)->frame;
7035 f = XFRAME (frame);
7036
7037 FRAME_SAMPLE_VISIBILITY (f);
7038 if (FRAME_VISIBLE_P (sf)
7039 && !FRAME_VISIBLE_P (f))
7040 Fmake_frame_visible (frame);
7041
7042 if (STRINGP (m) && SCHARS (m) > 0)
7043 {
7044 set_message (NULL, m, nbytes, multibyte);
7045 if (minibuffer_auto_raise)
7046 Fraise_frame (frame);
7047 }
7048 else
7049 clear_message (1, 1);
7050
7051 do_pending_window_change (0);
7052 echo_area_display (1);
7053 do_pending_window_change (0);
7054 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7055 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
7056 }
7057 }
7058
7059
7060 /* Display a null-terminated echo area message M. If M is 0, clear
7061 out any existing message, and let the mini-buffer text show through.
7062
7063 The buffer M must continue to exist until after the echo area gets
7064 cleared or some other message gets displayed there. Do not pass
7065 text that is stored in a Lisp string. Do not pass text in a buffer
7066 that was alloca'd. */
7067
7068 void
7069 message1 (m)
7070 char *m;
7071 {
7072 message2 (m, (m ? strlen (m) : 0), 0);
7073 }
7074
7075
7076 /* The non-logging counterpart of message1. */
7077
7078 void
7079 message1_nolog (m)
7080 char *m;
7081 {
7082 message2_nolog (m, (m ? strlen (m) : 0), 0);
7083 }
7084
7085 /* Display a message M which contains a single %s
7086 which gets replaced with STRING. */
7087
7088 void
7089 message_with_string (m, string, log)
7090 char *m;
7091 Lisp_Object string;
7092 int log;
7093 {
7094 CHECK_STRING (string);
7095
7096 if (noninteractive)
7097 {
7098 if (m)
7099 {
7100 if (noninteractive_need_newline)
7101 putc ('\n', stderr);
7102 noninteractive_need_newline = 0;
7103 fprintf (stderr, m, SDATA (string));
7104 if (cursor_in_echo_area == 0)
7105 fprintf (stderr, "\n");
7106 fflush (stderr);
7107 }
7108 }
7109 else if (INTERACTIVE)
7110 {
7111 /* The frame whose minibuffer we're going to display the message on.
7112 It may be larger than the selected frame, so we need
7113 to use its buffer, not the selected frame's buffer. */
7114 Lisp_Object mini_window;
7115 struct frame *f, *sf = SELECTED_FRAME ();
7116
7117 /* Get the frame containing the minibuffer
7118 that the selected frame is using. */
7119 mini_window = FRAME_MINIBUF_WINDOW (sf);
7120 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7121
7122 /* A null message buffer means that the frame hasn't really been
7123 initialized yet. Error messages get reported properly by
7124 cmd_error, so this must be just an informative message; toss it. */
7125 if (FRAME_MESSAGE_BUF (f))
7126 {
7127 Lisp_Object args[2], message;
7128 struct gcpro gcpro1, gcpro2;
7129
7130 args[0] = build_string (m);
7131 args[1] = message = string;
7132 GCPRO2 (args[0], message);
7133 gcpro1.nvars = 2;
7134
7135 message = Fformat (2, args);
7136
7137 if (log)
7138 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7139 else
7140 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7141
7142 UNGCPRO;
7143
7144 /* Print should start at the beginning of the message
7145 buffer next time. */
7146 message_buf_print = 0;
7147 }
7148 }
7149 }
7150
7151
7152 /* Dump an informative message to the minibuf. If M is 0, clear out
7153 any existing message, and let the mini-buffer text show through. */
7154
7155 /* VARARGS 1 */
7156 void
7157 message (m, a1, a2, a3)
7158 char *m;
7159 EMACS_INT a1, a2, a3;
7160 {
7161 if (noninteractive)
7162 {
7163 if (m)
7164 {
7165 if (noninteractive_need_newline)
7166 putc ('\n', stderr);
7167 noninteractive_need_newline = 0;
7168 fprintf (stderr, m, a1, a2, a3);
7169 if (cursor_in_echo_area == 0)
7170 fprintf (stderr, "\n");
7171 fflush (stderr);
7172 }
7173 }
7174 else if (INTERACTIVE)
7175 {
7176 /* The frame whose mini-buffer we're going to display the message
7177 on. It may be larger than the selected frame, so we need to
7178 use its buffer, not the selected frame's buffer. */
7179 Lisp_Object mini_window;
7180 struct frame *f, *sf = SELECTED_FRAME ();
7181
7182 /* Get the frame containing the mini-buffer
7183 that the selected frame is using. */
7184 mini_window = FRAME_MINIBUF_WINDOW (sf);
7185 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7186
7187 /* A null message buffer means that the frame hasn't really been
7188 initialized yet. Error messages get reported properly by
7189 cmd_error, so this must be just an informative message; toss
7190 it. */
7191 if (FRAME_MESSAGE_BUF (f))
7192 {
7193 if (m)
7194 {
7195 int len;
7196 #ifdef NO_ARG_ARRAY
7197 char *a[3];
7198 a[0] = (char *) a1;
7199 a[1] = (char *) a2;
7200 a[2] = (char *) a3;
7201
7202 len = doprnt (FRAME_MESSAGE_BUF (f),
7203 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7204 #else
7205 len = doprnt (FRAME_MESSAGE_BUF (f),
7206 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7207 (char **) &a1);
7208 #endif /* NO_ARG_ARRAY */
7209
7210 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7211 }
7212 else
7213 message1 (0);
7214
7215 /* Print should start at the beginning of the message
7216 buffer next time. */
7217 message_buf_print = 0;
7218 }
7219 }
7220 }
7221
7222
7223 /* The non-logging version of message. */
7224
7225 void
7226 message_nolog (m, a1, a2, a3)
7227 char *m;
7228 EMACS_INT a1, a2, a3;
7229 {
7230 Lisp_Object old_log_max;
7231 old_log_max = Vmessage_log_max;
7232 Vmessage_log_max = Qnil;
7233 message (m, a1, a2, a3);
7234 Vmessage_log_max = old_log_max;
7235 }
7236
7237
7238 /* Display the current message in the current mini-buffer. This is
7239 only called from error handlers in process.c, and is not time
7240 critical. */
7241
7242 void
7243 update_echo_area ()
7244 {
7245 if (!NILP (echo_area_buffer[0]))
7246 {
7247 Lisp_Object string;
7248 string = Fcurrent_message ();
7249 message3 (string, SBYTES (string),
7250 !NILP (current_buffer->enable_multibyte_characters));
7251 }
7252 }
7253
7254
7255 /* Make sure echo area buffers in `echo_buffers' are live.
7256 If they aren't, make new ones. */
7257
7258 static void
7259 ensure_echo_area_buffers ()
7260 {
7261 int i;
7262
7263 for (i = 0; i < 2; ++i)
7264 if (!BUFFERP (echo_buffer[i])
7265 || NILP (XBUFFER (echo_buffer[i])->name))
7266 {
7267 char name[30];
7268 Lisp_Object old_buffer;
7269 int j;
7270
7271 old_buffer = echo_buffer[i];
7272 sprintf (name, " *Echo Area %d*", i);
7273 echo_buffer[i] = Fget_buffer_create (build_string (name));
7274 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7275
7276 for (j = 0; j < 2; ++j)
7277 if (EQ (old_buffer, echo_area_buffer[j]))
7278 echo_area_buffer[j] = echo_buffer[i];
7279 }
7280 }
7281
7282
7283 /* Call FN with args A1..A4 with either the current or last displayed
7284 echo_area_buffer as current buffer.
7285
7286 WHICH zero means use the current message buffer
7287 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7288 from echo_buffer[] and clear it.
7289
7290 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7291 suitable buffer from echo_buffer[] and clear it.
7292
7293 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7294 that the current message becomes the last displayed one, make
7295 choose a suitable buffer for echo_area_buffer[0], and clear it.
7296
7297 Value is what FN returns. */
7298
7299 static int
7300 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7301 struct window *w;
7302 int which;
7303 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7304 EMACS_INT a1;
7305 Lisp_Object a2;
7306 EMACS_INT a3, a4;
7307 {
7308 Lisp_Object buffer;
7309 int this_one, the_other, clear_buffer_p, rc;
7310 int count = SPECPDL_INDEX ();
7311
7312 /* If buffers aren't live, make new ones. */
7313 ensure_echo_area_buffers ();
7314
7315 clear_buffer_p = 0;
7316
7317 if (which == 0)
7318 this_one = 0, the_other = 1;
7319 else if (which > 0)
7320 this_one = 1, the_other = 0;
7321 else
7322 {
7323 this_one = 0, the_other = 1;
7324 clear_buffer_p = 1;
7325
7326 /* We need a fresh one in case the current echo buffer equals
7327 the one containing the last displayed echo area message. */
7328 if (!NILP (echo_area_buffer[this_one])
7329 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7330 echo_area_buffer[this_one] = Qnil;
7331 }
7332
7333 /* Choose a suitable buffer from echo_buffer[] is we don't
7334 have one. */
7335 if (NILP (echo_area_buffer[this_one]))
7336 {
7337 echo_area_buffer[this_one]
7338 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7339 ? echo_buffer[the_other]
7340 : echo_buffer[this_one]);
7341 clear_buffer_p = 1;
7342 }
7343
7344 buffer = echo_area_buffer[this_one];
7345
7346 /* Don't get confused by reusing the buffer used for echoing
7347 for a different purpose. */
7348 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7349 cancel_echoing ();
7350
7351 record_unwind_protect (unwind_with_echo_area_buffer,
7352 with_echo_area_buffer_unwind_data (w));
7353
7354 /* Make the echo area buffer current. Note that for display
7355 purposes, it is not necessary that the displayed window's buffer
7356 == current_buffer, except for text property lookup. So, let's
7357 only set that buffer temporarily here without doing a full
7358 Fset_window_buffer. We must also change w->pointm, though,
7359 because otherwise an assertions in unshow_buffer fails, and Emacs
7360 aborts. */
7361 set_buffer_internal_1 (XBUFFER (buffer));
7362 if (w)
7363 {
7364 w->buffer = buffer;
7365 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7366 }
7367
7368 current_buffer->undo_list = Qt;
7369 current_buffer->read_only = Qnil;
7370 specbind (Qinhibit_read_only, Qt);
7371 specbind (Qinhibit_modification_hooks, Qt);
7372
7373 if (clear_buffer_p && Z > BEG)
7374 del_range (BEG, Z);
7375
7376 xassert (BEGV >= BEG);
7377 xassert (ZV <= Z && ZV >= BEGV);
7378
7379 rc = fn (a1, a2, a3, a4);
7380
7381 xassert (BEGV >= BEG);
7382 xassert (ZV <= Z && ZV >= BEGV);
7383
7384 unbind_to (count, Qnil);
7385 return rc;
7386 }
7387
7388
7389 /* Save state that should be preserved around the call to the function
7390 FN called in with_echo_area_buffer. */
7391
7392 static Lisp_Object
7393 with_echo_area_buffer_unwind_data (w)
7394 struct window *w;
7395 {
7396 int i = 0;
7397 Lisp_Object vector;
7398
7399 /* Reduce consing by keeping one vector in
7400 Vwith_echo_area_save_vector. */
7401 vector = Vwith_echo_area_save_vector;
7402 Vwith_echo_area_save_vector = Qnil;
7403
7404 if (NILP (vector))
7405 vector = Fmake_vector (make_number (7), Qnil);
7406
7407 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7408 AREF (vector, i) = Vdeactivate_mark, ++i;
7409 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7410
7411 if (w)
7412 {
7413 XSETWINDOW (AREF (vector, i), w); ++i;
7414 AREF (vector, i) = w->buffer; ++i;
7415 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7416 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7417 }
7418 else
7419 {
7420 int end = i + 4;
7421 for (; i < end; ++i)
7422 AREF (vector, i) = Qnil;
7423 }
7424
7425 xassert (i == ASIZE (vector));
7426 return vector;
7427 }
7428
7429
7430 /* Restore global state from VECTOR which was created by
7431 with_echo_area_buffer_unwind_data. */
7432
7433 static Lisp_Object
7434 unwind_with_echo_area_buffer (vector)
7435 Lisp_Object vector;
7436 {
7437 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7438 Vdeactivate_mark = AREF (vector, 1);
7439 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7440
7441 if (WINDOWP (AREF (vector, 3)))
7442 {
7443 struct window *w;
7444 Lisp_Object buffer, charpos, bytepos;
7445
7446 w = XWINDOW (AREF (vector, 3));
7447 buffer = AREF (vector, 4);
7448 charpos = AREF (vector, 5);
7449 bytepos = AREF (vector, 6);
7450
7451 w->buffer = buffer;
7452 set_marker_both (w->pointm, buffer,
7453 XFASTINT (charpos), XFASTINT (bytepos));
7454 }
7455
7456 Vwith_echo_area_save_vector = vector;
7457 return Qnil;
7458 }
7459
7460
7461 /* Set up the echo area for use by print functions. MULTIBYTE_P
7462 non-zero means we will print multibyte. */
7463
7464 void
7465 setup_echo_area_for_printing (multibyte_p)
7466 int multibyte_p;
7467 {
7468 /* If we can't find an echo area any more, exit. */
7469 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7470 Fkill_emacs (Qnil);
7471
7472 ensure_echo_area_buffers ();
7473
7474 if (!message_buf_print)
7475 {
7476 /* A message has been output since the last time we printed.
7477 Choose a fresh echo area buffer. */
7478 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7479 echo_area_buffer[0] = echo_buffer[1];
7480 else
7481 echo_area_buffer[0] = echo_buffer[0];
7482
7483 /* Switch to that buffer and clear it. */
7484 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7485 current_buffer->truncate_lines = Qnil;
7486
7487 if (Z > BEG)
7488 {
7489 int count = SPECPDL_INDEX ();
7490 specbind (Qinhibit_read_only, Qt);
7491 /* Note that undo recording is always disabled. */
7492 del_range (BEG, Z);
7493 unbind_to (count, Qnil);
7494 }
7495 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7496
7497 /* Set up the buffer for the multibyteness we need. */
7498 if (multibyte_p
7499 != !NILP (current_buffer->enable_multibyte_characters))
7500 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7501
7502 /* Raise the frame containing the echo area. */
7503 if (minibuffer_auto_raise)
7504 {
7505 struct frame *sf = SELECTED_FRAME ();
7506 Lisp_Object mini_window;
7507 mini_window = FRAME_MINIBUF_WINDOW (sf);
7508 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7509 }
7510
7511 message_log_maybe_newline ();
7512 message_buf_print = 1;
7513 }
7514 else
7515 {
7516 if (NILP (echo_area_buffer[0]))
7517 {
7518 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7519 echo_area_buffer[0] = echo_buffer[1];
7520 else
7521 echo_area_buffer[0] = echo_buffer[0];
7522 }
7523
7524 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7525 {
7526 /* Someone switched buffers between print requests. */
7527 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7528 current_buffer->truncate_lines = Qnil;
7529 }
7530 }
7531 }
7532
7533
7534 /* Display an echo area message in window W. Value is non-zero if W's
7535 height is changed. If display_last_displayed_message_p is
7536 non-zero, display the message that was last displayed, otherwise
7537 display the current message. */
7538
7539 static int
7540 display_echo_area (w)
7541 struct window *w;
7542 {
7543 int i, no_message_p, window_height_changed_p, count;
7544
7545 /* Temporarily disable garbage collections while displaying the echo
7546 area. This is done because a GC can print a message itself.
7547 That message would modify the echo area buffer's contents while a
7548 redisplay of the buffer is going on, and seriously confuse
7549 redisplay. */
7550 count = inhibit_garbage_collection ();
7551
7552 /* If there is no message, we must call display_echo_area_1
7553 nevertheless because it resizes the window. But we will have to
7554 reset the echo_area_buffer in question to nil at the end because
7555 with_echo_area_buffer will sets it to an empty buffer. */
7556 i = display_last_displayed_message_p ? 1 : 0;
7557 no_message_p = NILP (echo_area_buffer[i]);
7558
7559 window_height_changed_p
7560 = with_echo_area_buffer (w, display_last_displayed_message_p,
7561 display_echo_area_1,
7562 (EMACS_INT) w, Qnil, 0, 0);
7563
7564 if (no_message_p)
7565 echo_area_buffer[i] = Qnil;
7566
7567 unbind_to (count, Qnil);
7568 return window_height_changed_p;
7569 }
7570
7571
7572 /* Helper for display_echo_area. Display the current buffer which
7573 contains the current echo area message in window W, a mini-window,
7574 a pointer to which is passed in A1. A2..A4 are currently not used.
7575 Change the height of W so that all of the message is displayed.
7576 Value is non-zero if height of W was changed. */
7577
7578 static int
7579 display_echo_area_1 (a1, a2, a3, a4)
7580 EMACS_INT a1;
7581 Lisp_Object a2;
7582 EMACS_INT a3, a4;
7583 {
7584 struct window *w = (struct window *) a1;
7585 Lisp_Object window;
7586 struct text_pos start;
7587 int window_height_changed_p = 0;
7588
7589 /* Do this before displaying, so that we have a large enough glyph
7590 matrix for the display. */
7591 window_height_changed_p = resize_mini_window (w, 0);
7592
7593 /* Display. */
7594 clear_glyph_matrix (w->desired_matrix);
7595 XSETWINDOW (window, w);
7596 SET_TEXT_POS (start, BEG, BEG_BYTE);
7597 try_window (window, start, 0);
7598
7599 return window_height_changed_p;
7600 }
7601
7602
7603 /* Resize the echo area window to exactly the size needed for the
7604 currently displayed message, if there is one. If a mini-buffer
7605 is active, don't shrink it. */
7606
7607 void
7608 resize_echo_area_exactly ()
7609 {
7610 if (BUFFERP (echo_area_buffer[0])
7611 && WINDOWP (echo_area_window))
7612 {
7613 struct window *w = XWINDOW (echo_area_window);
7614 int resized_p;
7615 Lisp_Object resize_exactly;
7616
7617 if (minibuf_level == 0)
7618 resize_exactly = Qt;
7619 else
7620 resize_exactly = Qnil;
7621
7622 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7623 (EMACS_INT) w, resize_exactly, 0, 0);
7624 if (resized_p)
7625 {
7626 ++windows_or_buffers_changed;
7627 ++update_mode_lines;
7628 redisplay_internal (0);
7629 }
7630 }
7631 }
7632
7633
7634 /* Callback function for with_echo_area_buffer, when used from
7635 resize_echo_area_exactly. A1 contains a pointer to the window to
7636 resize, EXACTLY non-nil means resize the mini-window exactly to the
7637 size of the text displayed. A3 and A4 are not used. Value is what
7638 resize_mini_window returns. */
7639
7640 static int
7641 resize_mini_window_1 (a1, exactly, a3, a4)
7642 EMACS_INT a1;
7643 Lisp_Object exactly;
7644 EMACS_INT a3, a4;
7645 {
7646 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7647 }
7648
7649
7650 /* Resize mini-window W to fit the size of its contents. EXACT:P
7651 means size the window exactly to the size needed. Otherwise, it's
7652 only enlarged until W's buffer is empty. Value is non-zero if
7653 the window height has been changed. */
7654
7655 int
7656 resize_mini_window (w, exact_p)
7657 struct window *w;
7658 int exact_p;
7659 {
7660 struct frame *f = XFRAME (w->frame);
7661 int window_height_changed_p = 0;
7662
7663 xassert (MINI_WINDOW_P (w));
7664
7665 /* Don't resize windows while redisplaying a window; it would
7666 confuse redisplay functions when the size of the window they are
7667 displaying changes from under them. Such a resizing can happen,
7668 for instance, when which-func prints a long message while
7669 we are running fontification-functions. We're running these
7670 functions with safe_call which binds inhibit-redisplay to t. */
7671 if (!NILP (Vinhibit_redisplay))
7672 return 0;
7673
7674 /* Nil means don't try to resize. */
7675 if (NILP (Vresize_mini_windows)
7676 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7677 return 0;
7678
7679 if (!FRAME_MINIBUF_ONLY_P (f))
7680 {
7681 struct it it;
7682 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7683 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7684 int height, max_height;
7685 int unit = FRAME_LINE_HEIGHT (f);
7686 struct text_pos start;
7687 struct buffer *old_current_buffer = NULL;
7688
7689 if (current_buffer != XBUFFER (w->buffer))
7690 {
7691 old_current_buffer = current_buffer;
7692 set_buffer_internal (XBUFFER (w->buffer));
7693 }
7694
7695 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7696
7697 /* Compute the max. number of lines specified by the user. */
7698 if (FLOATP (Vmax_mini_window_height))
7699 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7700 else if (INTEGERP (Vmax_mini_window_height))
7701 max_height = XINT (Vmax_mini_window_height);
7702 else
7703 max_height = total_height / 4;
7704
7705 /* Correct that max. height if it's bogus. */
7706 max_height = max (1, max_height);
7707 max_height = min (total_height, max_height);
7708
7709 /* Find out the height of the text in the window. */
7710 if (it.truncate_lines_p)
7711 height = 1;
7712 else
7713 {
7714 last_height = 0;
7715 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7716 if (it.max_ascent == 0 && it.max_descent == 0)
7717 height = it.current_y + last_height;
7718 else
7719 height = it.current_y + it.max_ascent + it.max_descent;
7720 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7721 height = (height + unit - 1) / unit;
7722 }
7723
7724 /* Compute a suitable window start. */
7725 if (height > max_height)
7726 {
7727 height = max_height;
7728 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7729 move_it_vertically_backward (&it, (height - 1) * unit);
7730 start = it.current.pos;
7731 }
7732 else
7733 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7734 SET_MARKER_FROM_TEXT_POS (w->start, start);
7735
7736 if (EQ (Vresize_mini_windows, Qgrow_only))
7737 {
7738 /* Let it grow only, until we display an empty message, in which
7739 case the window shrinks again. */
7740 if (height > WINDOW_TOTAL_LINES (w))
7741 {
7742 int old_height = WINDOW_TOTAL_LINES (w);
7743 freeze_window_starts (f, 1);
7744 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7745 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7746 }
7747 else if (height < WINDOW_TOTAL_LINES (w)
7748 && (exact_p || BEGV == ZV))
7749 {
7750 int old_height = WINDOW_TOTAL_LINES (w);
7751 freeze_window_starts (f, 0);
7752 shrink_mini_window (w);
7753 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7754 }
7755 }
7756 else
7757 {
7758 /* Always resize to exact size needed. */
7759 if (height > WINDOW_TOTAL_LINES (w))
7760 {
7761 int old_height = WINDOW_TOTAL_LINES (w);
7762 freeze_window_starts (f, 1);
7763 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7764 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7765 }
7766 else if (height < WINDOW_TOTAL_LINES (w))
7767 {
7768 int old_height = WINDOW_TOTAL_LINES (w);
7769 freeze_window_starts (f, 0);
7770 shrink_mini_window (w);
7771
7772 if (height)
7773 {
7774 freeze_window_starts (f, 1);
7775 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7776 }
7777
7778 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7779 }
7780 }
7781
7782 if (old_current_buffer)
7783 set_buffer_internal (old_current_buffer);
7784 }
7785
7786 return window_height_changed_p;
7787 }
7788
7789
7790 /* Value is the current message, a string, or nil if there is no
7791 current message. */
7792
7793 Lisp_Object
7794 current_message ()
7795 {
7796 Lisp_Object msg;
7797
7798 if (NILP (echo_area_buffer[0]))
7799 msg = Qnil;
7800 else
7801 {
7802 with_echo_area_buffer (0, 0, current_message_1,
7803 (EMACS_INT) &msg, Qnil, 0, 0);
7804 if (NILP (msg))
7805 echo_area_buffer[0] = Qnil;
7806 }
7807
7808 return msg;
7809 }
7810
7811
7812 static int
7813 current_message_1 (a1, a2, a3, a4)
7814 EMACS_INT a1;
7815 Lisp_Object a2;
7816 EMACS_INT a3, a4;
7817 {
7818 Lisp_Object *msg = (Lisp_Object *) a1;
7819
7820 if (Z > BEG)
7821 *msg = make_buffer_string (BEG, Z, 1);
7822 else
7823 *msg = Qnil;
7824 return 0;
7825 }
7826
7827
7828 /* Push the current message on Vmessage_stack for later restauration
7829 by restore_message. Value is non-zero if the current message isn't
7830 empty. This is a relatively infrequent operation, so it's not
7831 worth optimizing. */
7832
7833 int
7834 push_message ()
7835 {
7836 Lisp_Object msg;
7837 msg = current_message ();
7838 Vmessage_stack = Fcons (msg, Vmessage_stack);
7839 return STRINGP (msg);
7840 }
7841
7842
7843 /* Restore message display from the top of Vmessage_stack. */
7844
7845 void
7846 restore_message ()
7847 {
7848 Lisp_Object msg;
7849
7850 xassert (CONSP (Vmessage_stack));
7851 msg = XCAR (Vmessage_stack);
7852 if (STRINGP (msg))
7853 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7854 else
7855 message3_nolog (msg, 0, 0);
7856 }
7857
7858
7859 /* Handler for record_unwind_protect calling pop_message. */
7860
7861 Lisp_Object
7862 pop_message_unwind (dummy)
7863 Lisp_Object dummy;
7864 {
7865 pop_message ();
7866 return Qnil;
7867 }
7868
7869 /* Pop the top-most entry off Vmessage_stack. */
7870
7871 void
7872 pop_message ()
7873 {
7874 xassert (CONSP (Vmessage_stack));
7875 Vmessage_stack = XCDR (Vmessage_stack);
7876 }
7877
7878
7879 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7880 exits. If the stack is not empty, we have a missing pop_message
7881 somewhere. */
7882
7883 void
7884 check_message_stack ()
7885 {
7886 if (!NILP (Vmessage_stack))
7887 abort ();
7888 }
7889
7890
7891 /* Truncate to NCHARS what will be displayed in the echo area the next
7892 time we display it---but don't redisplay it now. */
7893
7894 void
7895 truncate_echo_area (nchars)
7896 int nchars;
7897 {
7898 if (nchars == 0)
7899 echo_area_buffer[0] = Qnil;
7900 /* A null message buffer means that the frame hasn't really been
7901 initialized yet. Error messages get reported properly by
7902 cmd_error, so this must be just an informative message; toss it. */
7903 else if (!noninteractive
7904 && INTERACTIVE
7905 && !NILP (echo_area_buffer[0]))
7906 {
7907 struct frame *sf = SELECTED_FRAME ();
7908 if (FRAME_MESSAGE_BUF (sf))
7909 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7910 }
7911 }
7912
7913
7914 /* Helper function for truncate_echo_area. Truncate the current
7915 message to at most NCHARS characters. */
7916
7917 static int
7918 truncate_message_1 (nchars, a2, a3, a4)
7919 EMACS_INT nchars;
7920 Lisp_Object a2;
7921 EMACS_INT a3, a4;
7922 {
7923 if (BEG + nchars < Z)
7924 del_range (BEG + nchars, Z);
7925 if (Z == BEG)
7926 echo_area_buffer[0] = Qnil;
7927 return 0;
7928 }
7929
7930
7931 /* Set the current message to a substring of S or STRING.
7932
7933 If STRING is a Lisp string, set the message to the first NBYTES
7934 bytes from STRING. NBYTES zero means use the whole string. If
7935 STRING is multibyte, the message will be displayed multibyte.
7936
7937 If S is not null, set the message to the first LEN bytes of S. LEN
7938 zero means use the whole string. MULTIBYTE_P non-zero means S is
7939 multibyte. Display the message multibyte in that case. */
7940
7941 void
7942 set_message (s, string, nbytes, multibyte_p)
7943 const char *s;
7944 Lisp_Object string;
7945 int nbytes, multibyte_p;
7946 {
7947 message_enable_multibyte
7948 = ((s && multibyte_p)
7949 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7950
7951 with_echo_area_buffer (0, -1, set_message_1,
7952 (EMACS_INT) s, string, nbytes, multibyte_p);
7953 message_buf_print = 0;
7954 help_echo_showing_p = 0;
7955 }
7956
7957
7958 /* Helper function for set_message. Arguments have the same meaning
7959 as there, with A1 corresponding to S and A2 corresponding to STRING
7960 This function is called with the echo area buffer being
7961 current. */
7962
7963 static int
7964 set_message_1 (a1, a2, nbytes, multibyte_p)
7965 EMACS_INT a1;
7966 Lisp_Object a2;
7967 EMACS_INT nbytes, multibyte_p;
7968 {
7969 const char *s = (const char *) a1;
7970 Lisp_Object string = a2;
7971
7972 /* Change multibyteness of the echo buffer appropriately. */
7973 if (message_enable_multibyte
7974 != !NILP (current_buffer->enable_multibyte_characters))
7975 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7976
7977 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7978
7979 /* Insert new message at BEG. */
7980 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7981
7982 if (STRINGP (string))
7983 {
7984 int nchars;
7985
7986 if (nbytes == 0)
7987 nbytes = SBYTES (string);
7988 nchars = string_byte_to_char (string, nbytes);
7989
7990 /* This function takes care of single/multibyte conversion. We
7991 just have to ensure that the echo area buffer has the right
7992 setting of enable_multibyte_characters. */
7993 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7994 }
7995 else if (s)
7996 {
7997 if (nbytes == 0)
7998 nbytes = strlen (s);
7999
8000 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8001 {
8002 /* Convert from multi-byte to single-byte. */
8003 int i, c, n;
8004 unsigned char work[1];
8005
8006 /* Convert a multibyte string to single-byte. */
8007 for (i = 0; i < nbytes; i += n)
8008 {
8009 c = string_char_and_length (s + i, nbytes - i, &n);
8010 work[0] = (SINGLE_BYTE_CHAR_P (c)
8011 ? c
8012 : multibyte_char_to_unibyte (c, Qnil));
8013 insert_1_both (work, 1, 1, 1, 0, 0);
8014 }
8015 }
8016 else if (!multibyte_p
8017 && !NILP (current_buffer->enable_multibyte_characters))
8018 {
8019 /* Convert from single-byte to multi-byte. */
8020 int i, c, n;
8021 const unsigned char *msg = (const unsigned char *) s;
8022 unsigned char str[MAX_MULTIBYTE_LENGTH];
8023
8024 /* Convert a single-byte string to multibyte. */
8025 for (i = 0; i < nbytes; i++)
8026 {
8027 c = unibyte_char_to_multibyte (msg[i]);
8028 n = CHAR_STRING (c, str);
8029 insert_1_both (str, 1, n, 1, 0, 0);
8030 }
8031 }
8032 else
8033 insert_1 (s, nbytes, 1, 0, 0);
8034 }
8035
8036 return 0;
8037 }
8038
8039
8040 /* Clear messages. CURRENT_P non-zero means clear the current
8041 message. LAST_DISPLAYED_P non-zero means clear the message
8042 last displayed. */
8043
8044 void
8045 clear_message (current_p, last_displayed_p)
8046 int current_p, last_displayed_p;
8047 {
8048 if (current_p)
8049 {
8050 echo_area_buffer[0] = Qnil;
8051 message_cleared_p = 1;
8052 }
8053
8054 if (last_displayed_p)
8055 echo_area_buffer[1] = Qnil;
8056
8057 message_buf_print = 0;
8058 }
8059
8060 /* Clear garbaged frames.
8061
8062 This function is used where the old redisplay called
8063 redraw_garbaged_frames which in turn called redraw_frame which in
8064 turn called clear_frame. The call to clear_frame was a source of
8065 flickering. I believe a clear_frame is not necessary. It should
8066 suffice in the new redisplay to invalidate all current matrices,
8067 and ensure a complete redisplay of all windows. */
8068
8069 static void
8070 clear_garbaged_frames ()
8071 {
8072 if (frame_garbaged)
8073 {
8074 Lisp_Object tail, frame;
8075 int changed_count = 0;
8076
8077 FOR_EACH_FRAME (tail, frame)
8078 {
8079 struct frame *f = XFRAME (frame);
8080
8081 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8082 {
8083 if (f->resized_p)
8084 {
8085 Fredraw_frame (frame);
8086 f->force_flush_display_p = 1;
8087 }
8088 clear_current_matrices (f);
8089 changed_count++;
8090 f->garbaged = 0;
8091 f->resized_p = 0;
8092 }
8093 }
8094
8095 frame_garbaged = 0;
8096 if (changed_count)
8097 ++windows_or_buffers_changed;
8098 }
8099 }
8100
8101
8102 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8103 is non-zero update selected_frame. Value is non-zero if the
8104 mini-windows height has been changed. */
8105
8106 static int
8107 echo_area_display (update_frame_p)
8108 int update_frame_p;
8109 {
8110 Lisp_Object mini_window;
8111 struct window *w;
8112 struct frame *f;
8113 int window_height_changed_p = 0;
8114 struct frame *sf = SELECTED_FRAME ();
8115
8116 mini_window = FRAME_MINIBUF_WINDOW (sf);
8117 w = XWINDOW (mini_window);
8118 f = XFRAME (WINDOW_FRAME (w));
8119
8120 /* Don't display if frame is invisible or not yet initialized. */
8121 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8122 return 0;
8123
8124 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8125 #ifndef MAC_OS8
8126 #ifdef HAVE_WINDOW_SYSTEM
8127 /* When Emacs starts, selected_frame may be the initial terminal
8128 frame. If we let this through, a message would be displayed on
8129 the terminal. */
8130 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8131 return 0;
8132 #endif /* HAVE_WINDOW_SYSTEM */
8133 #endif
8134
8135 /* Redraw garbaged frames. */
8136 if (frame_garbaged)
8137 clear_garbaged_frames ();
8138
8139 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8140 {
8141 echo_area_window = mini_window;
8142 window_height_changed_p = display_echo_area (w);
8143 w->must_be_updated_p = 1;
8144
8145 /* Update the display, unless called from redisplay_internal.
8146 Also don't update the screen during redisplay itself. The
8147 update will happen at the end of redisplay, and an update
8148 here could cause confusion. */
8149 if (update_frame_p && !redisplaying_p)
8150 {
8151 int n = 0;
8152
8153 /* If the display update has been interrupted by pending
8154 input, update mode lines in the frame. Due to the
8155 pending input, it might have been that redisplay hasn't
8156 been called, so that mode lines above the echo area are
8157 garbaged. This looks odd, so we prevent it here. */
8158 if (!display_completed)
8159 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8160
8161 if (window_height_changed_p
8162 /* Don't do this if Emacs is shutting down. Redisplay
8163 needs to run hooks. */
8164 && !NILP (Vrun_hooks))
8165 {
8166 /* Must update other windows. Likewise as in other
8167 cases, don't let this update be interrupted by
8168 pending input. */
8169 int count = SPECPDL_INDEX ();
8170 specbind (Qredisplay_dont_pause, Qt);
8171 windows_or_buffers_changed = 1;
8172 redisplay_internal (0);
8173 unbind_to (count, Qnil);
8174 }
8175 else if (FRAME_WINDOW_P (f) && n == 0)
8176 {
8177 /* Window configuration is the same as before.
8178 Can do with a display update of the echo area,
8179 unless we displayed some mode lines. */
8180 update_single_window (w, 1);
8181 FRAME_RIF (f)->flush_display (f);
8182 }
8183 else
8184 update_frame (f, 1, 1);
8185
8186 /* If cursor is in the echo area, make sure that the next
8187 redisplay displays the minibuffer, so that the cursor will
8188 be replaced with what the minibuffer wants. */
8189 if (cursor_in_echo_area)
8190 ++windows_or_buffers_changed;
8191 }
8192 }
8193 else if (!EQ (mini_window, selected_window))
8194 windows_or_buffers_changed++;
8195
8196 /* Last displayed message is now the current message. */
8197 echo_area_buffer[1] = echo_area_buffer[0];
8198 /* Inform read_char that we're not echoing. */
8199 echo_message_buffer = Qnil;
8200
8201 /* Prevent redisplay optimization in redisplay_internal by resetting
8202 this_line_start_pos. This is done because the mini-buffer now
8203 displays the message instead of its buffer text. */
8204 if (EQ (mini_window, selected_window))
8205 CHARPOS (this_line_start_pos) = 0;
8206
8207 return window_height_changed_p;
8208 }
8209
8210
8211 \f
8212 /***********************************************************************
8213 Mode Lines and Frame Titles
8214 ***********************************************************************/
8215
8216 /* A buffer for constructing non-propertized mode-line strings and
8217 frame titles in it; allocated from the heap in init_xdisp and
8218 resized as needed in store_mode_line_noprop_char. */
8219
8220 static char *mode_line_noprop_buf;
8221
8222 /* The buffer's end, and a current output position in it. */
8223
8224 static char *mode_line_noprop_buf_end;
8225 static char *mode_line_noprop_ptr;
8226
8227 #define MODE_LINE_NOPROP_LEN(start) \
8228 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8229
8230 static enum {
8231 MODE_LINE_DISPLAY = 0,
8232 MODE_LINE_TITLE,
8233 MODE_LINE_NOPROP,
8234 MODE_LINE_STRING
8235 } mode_line_target;
8236
8237 /* Alist that caches the results of :propertize.
8238 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8239 static Lisp_Object mode_line_proptrans_alist;
8240
8241 /* List of strings making up the mode-line. */
8242 static Lisp_Object mode_line_string_list;
8243
8244 /* Base face property when building propertized mode line string. */
8245 static Lisp_Object mode_line_string_face;
8246 static Lisp_Object mode_line_string_face_prop;
8247
8248
8249 /* Unwind data for mode line strings */
8250
8251 static Lisp_Object Vmode_line_unwind_vector;
8252
8253 static Lisp_Object
8254 format_mode_line_unwind_data (obuf)
8255 struct buffer *obuf;
8256 {
8257 Lisp_Object vector;
8258
8259 /* Reduce consing by keeping one vector in
8260 Vwith_echo_area_save_vector. */
8261 vector = Vmode_line_unwind_vector;
8262 Vmode_line_unwind_vector = Qnil;
8263
8264 if (NILP (vector))
8265 vector = Fmake_vector (make_number (7), Qnil);
8266
8267 AREF (vector, 0) = make_number (mode_line_target);
8268 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8269 AREF (vector, 2) = mode_line_string_list;
8270 AREF (vector, 3) = mode_line_proptrans_alist;
8271 AREF (vector, 4) = mode_line_string_face;
8272 AREF (vector, 5) = mode_line_string_face_prop;
8273
8274 if (obuf)
8275 XSETBUFFER (AREF (vector, 6), obuf);
8276 else
8277 AREF (vector, 6) = Qnil;
8278
8279 return vector;
8280 }
8281
8282 static Lisp_Object
8283 unwind_format_mode_line (vector)
8284 Lisp_Object vector;
8285 {
8286 mode_line_target = XINT (AREF (vector, 0));
8287 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8288 mode_line_string_list = AREF (vector, 2);
8289 mode_line_proptrans_alist = AREF (vector, 3);
8290 mode_line_string_face = AREF (vector, 4);
8291 mode_line_string_face_prop = AREF (vector, 5);
8292
8293 if (!NILP (AREF (vector, 6)))
8294 {
8295 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8296 AREF (vector, 6) = Qnil;
8297 }
8298
8299 Vmode_line_unwind_vector = vector;
8300 return Qnil;
8301 }
8302
8303
8304 /* Store a single character C for the frame title in mode_line_noprop_buf.
8305 Re-allocate mode_line_noprop_buf if necessary. */
8306
8307 static void
8308 #ifdef PROTOTYPES
8309 store_mode_line_noprop_char (char c)
8310 #else
8311 store_mode_line_noprop_char (c)
8312 char c;
8313 #endif
8314 {
8315 /* If output position has reached the end of the allocated buffer,
8316 double the buffer's size. */
8317 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8318 {
8319 int len = MODE_LINE_NOPROP_LEN (0);
8320 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8321 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8322 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8323 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8324 }
8325
8326 *mode_line_noprop_ptr++ = c;
8327 }
8328
8329
8330 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8331 mode_line_noprop_ptr. STR is the string to store. Do not copy
8332 characters that yield more columns than PRECISION; PRECISION <= 0
8333 means copy the whole string. Pad with spaces until FIELD_WIDTH
8334 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8335 pad. Called from display_mode_element when it is used to build a
8336 frame title. */
8337
8338 static int
8339 store_mode_line_noprop (str, field_width, precision)
8340 const unsigned char *str;
8341 int field_width, precision;
8342 {
8343 int n = 0;
8344 int dummy, nbytes;
8345
8346 /* Copy at most PRECISION chars from STR. */
8347 nbytes = strlen (str);
8348 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8349 while (nbytes--)
8350 store_mode_line_noprop_char (*str++);
8351
8352 /* Fill up with spaces until FIELD_WIDTH reached. */
8353 while (field_width > 0
8354 && n < field_width)
8355 {
8356 store_mode_line_noprop_char (' ');
8357 ++n;
8358 }
8359
8360 return n;
8361 }
8362
8363 /***********************************************************************
8364 Frame Titles
8365 ***********************************************************************/
8366
8367 #ifdef HAVE_WINDOW_SYSTEM
8368
8369 /* Set the title of FRAME, if it has changed. The title format is
8370 Vicon_title_format if FRAME is iconified, otherwise it is
8371 frame_title_format. */
8372
8373 static void
8374 x_consider_frame_title (frame)
8375 Lisp_Object frame;
8376 {
8377 struct frame *f = XFRAME (frame);
8378
8379 if (FRAME_WINDOW_P (f)
8380 || FRAME_MINIBUF_ONLY_P (f)
8381 || f->explicit_name)
8382 {
8383 /* Do we have more than one visible frame on this X display? */
8384 Lisp_Object tail;
8385 Lisp_Object fmt;
8386 int title_start;
8387 char *title;
8388 int len;
8389 struct it it;
8390 int count = SPECPDL_INDEX ();
8391
8392 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8393 {
8394 Lisp_Object other_frame = XCAR (tail);
8395 struct frame *tf = XFRAME (other_frame);
8396
8397 if (tf != f
8398 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8399 && !FRAME_MINIBUF_ONLY_P (tf)
8400 && !EQ (other_frame, tip_frame)
8401 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8402 break;
8403 }
8404
8405 /* Set global variable indicating that multiple frames exist. */
8406 multiple_frames = CONSP (tail);
8407
8408 /* Switch to the buffer of selected window of the frame. Set up
8409 mode_line_target so that display_mode_element will output into
8410 mode_line_noprop_buf; then display the title. */
8411 record_unwind_protect (unwind_format_mode_line,
8412 format_mode_line_unwind_data (current_buffer));
8413
8414 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8415 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8416
8417 mode_line_target = MODE_LINE_TITLE;
8418 title_start = MODE_LINE_NOPROP_LEN (0);
8419 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8420 NULL, DEFAULT_FACE_ID);
8421 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8422 len = MODE_LINE_NOPROP_LEN (title_start);
8423 title = mode_line_noprop_buf + title_start;
8424 unbind_to (count, Qnil);
8425
8426 /* Set the title only if it's changed. This avoids consing in
8427 the common case where it hasn't. (If it turns out that we've
8428 already wasted too much time by walking through the list with
8429 display_mode_element, then we might need to optimize at a
8430 higher level than this.) */
8431 if (! STRINGP (f->name)
8432 || SBYTES (f->name) != len
8433 || bcmp (title, SDATA (f->name), len) != 0)
8434 x_implicitly_set_name (f, make_string (title, len), Qnil);
8435 }
8436 }
8437
8438 #endif /* not HAVE_WINDOW_SYSTEM */
8439
8440
8441
8442 \f
8443 /***********************************************************************
8444 Menu Bars
8445 ***********************************************************************/
8446
8447
8448 /* Prepare for redisplay by updating menu-bar item lists when
8449 appropriate. This can call eval. */
8450
8451 void
8452 prepare_menu_bars ()
8453 {
8454 int all_windows;
8455 struct gcpro gcpro1, gcpro2;
8456 struct frame *f;
8457 Lisp_Object tooltip_frame;
8458
8459 #ifdef HAVE_WINDOW_SYSTEM
8460 tooltip_frame = tip_frame;
8461 #else
8462 tooltip_frame = Qnil;
8463 #endif
8464
8465 /* Update all frame titles based on their buffer names, etc. We do
8466 this before the menu bars so that the buffer-menu will show the
8467 up-to-date frame titles. */
8468 #ifdef HAVE_WINDOW_SYSTEM
8469 if (windows_or_buffers_changed || update_mode_lines)
8470 {
8471 Lisp_Object tail, frame;
8472
8473 FOR_EACH_FRAME (tail, frame)
8474 {
8475 f = XFRAME (frame);
8476 if (!EQ (frame, tooltip_frame)
8477 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8478 x_consider_frame_title (frame);
8479 }
8480 }
8481 #endif /* HAVE_WINDOW_SYSTEM */
8482
8483 /* Update the menu bar item lists, if appropriate. This has to be
8484 done before any actual redisplay or generation of display lines. */
8485 all_windows = (update_mode_lines
8486 || buffer_shared > 1
8487 || windows_or_buffers_changed);
8488 if (all_windows)
8489 {
8490 Lisp_Object tail, frame;
8491 int count = SPECPDL_INDEX ();
8492
8493 record_unwind_save_match_data ();
8494
8495 FOR_EACH_FRAME (tail, frame)
8496 {
8497 f = XFRAME (frame);
8498
8499 /* Ignore tooltip frame. */
8500 if (EQ (frame, tooltip_frame))
8501 continue;
8502
8503 /* If a window on this frame changed size, report that to
8504 the user and clear the size-change flag. */
8505 if (FRAME_WINDOW_SIZES_CHANGED (f))
8506 {
8507 Lisp_Object functions;
8508
8509 /* Clear flag first in case we get an error below. */
8510 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8511 functions = Vwindow_size_change_functions;
8512 GCPRO2 (tail, functions);
8513
8514 while (CONSP (functions))
8515 {
8516 call1 (XCAR (functions), frame);
8517 functions = XCDR (functions);
8518 }
8519 UNGCPRO;
8520 }
8521
8522 GCPRO1 (tail);
8523 update_menu_bar (f, 0);
8524 #ifdef HAVE_WINDOW_SYSTEM
8525 update_tool_bar (f, 0);
8526 #endif
8527 UNGCPRO;
8528 }
8529
8530 unbind_to (count, Qnil);
8531 }
8532 else
8533 {
8534 struct frame *sf = SELECTED_FRAME ();
8535 update_menu_bar (sf, 1);
8536 #ifdef HAVE_WINDOW_SYSTEM
8537 update_tool_bar (sf, 1);
8538 #endif
8539 }
8540
8541 /* Motif needs this. See comment in xmenu.c. Turn it off when
8542 pending_menu_activation is not defined. */
8543 #ifdef USE_X_TOOLKIT
8544 pending_menu_activation = 0;
8545 #endif
8546 }
8547
8548
8549 /* Update the menu bar item list for frame F. This has to be done
8550 before we start to fill in any display lines, because it can call
8551 eval.
8552
8553 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8554
8555 static void
8556 update_menu_bar (f, save_match_data)
8557 struct frame *f;
8558 int save_match_data;
8559 {
8560 Lisp_Object window;
8561 register struct window *w;
8562
8563 /* If called recursively during a menu update, do nothing. This can
8564 happen when, for instance, an activate-menubar-hook causes a
8565 redisplay. */
8566 if (inhibit_menubar_update)
8567 return;
8568
8569 window = FRAME_SELECTED_WINDOW (f);
8570 w = XWINDOW (window);
8571
8572 #if 0 /* The if statement below this if statement used to include the
8573 condition !NILP (w->update_mode_line), rather than using
8574 update_mode_lines directly, and this if statement may have
8575 been added to make that condition work. Now the if
8576 statement below matches its comment, this isn't needed. */
8577 if (update_mode_lines)
8578 w->update_mode_line = Qt;
8579 #endif
8580
8581 if (FRAME_WINDOW_P (f)
8582 ?
8583 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8584 || defined (USE_GTK)
8585 FRAME_EXTERNAL_MENU_BAR (f)
8586 #else
8587 FRAME_MENU_BAR_LINES (f) > 0
8588 #endif
8589 : FRAME_MENU_BAR_LINES (f) > 0)
8590 {
8591 /* If the user has switched buffers or windows, we need to
8592 recompute to reflect the new bindings. But we'll
8593 recompute when update_mode_lines is set too; that means
8594 that people can use force-mode-line-update to request
8595 that the menu bar be recomputed. The adverse effect on
8596 the rest of the redisplay algorithm is about the same as
8597 windows_or_buffers_changed anyway. */
8598 if (windows_or_buffers_changed
8599 /* This used to test w->update_mode_line, but we believe
8600 there is no need to recompute the menu in that case. */
8601 || update_mode_lines
8602 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8603 < BUF_MODIFF (XBUFFER (w->buffer)))
8604 != !NILP (w->last_had_star))
8605 || ((!NILP (Vtransient_mark_mode)
8606 && !NILP (XBUFFER (w->buffer)->mark_active))
8607 != !NILP (w->region_showing)))
8608 {
8609 struct buffer *prev = current_buffer;
8610 int count = SPECPDL_INDEX ();
8611
8612 specbind (Qinhibit_menubar_update, Qt);
8613
8614 set_buffer_internal_1 (XBUFFER (w->buffer));
8615 if (save_match_data)
8616 record_unwind_save_match_data ();
8617 if (NILP (Voverriding_local_map_menu_flag))
8618 {
8619 specbind (Qoverriding_terminal_local_map, Qnil);
8620 specbind (Qoverriding_local_map, Qnil);
8621 }
8622
8623 /* Run the Lucid hook. */
8624 safe_run_hooks (Qactivate_menubar_hook);
8625
8626 /* If it has changed current-menubar from previous value,
8627 really recompute the menu-bar from the value. */
8628 if (! NILP (Vlucid_menu_bar_dirty_flag))
8629 call0 (Qrecompute_lucid_menubar);
8630
8631 safe_run_hooks (Qmenu_bar_update_hook);
8632 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8633
8634 /* Redisplay the menu bar in case we changed it. */
8635 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8636 || defined (USE_GTK)
8637 if (FRAME_WINDOW_P (f)
8638 #if defined (MAC_OS)
8639 /* All frames on Mac OS share the same menubar. So only the
8640 selected frame should be allowed to set it. */
8641 && f == SELECTED_FRAME ()
8642 #endif
8643 )
8644 set_frame_menubar (f, 0, 0);
8645 else
8646 /* On a terminal screen, the menu bar is an ordinary screen
8647 line, and this makes it get updated. */
8648 w->update_mode_line = Qt;
8649 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8650 /* In the non-toolkit version, the menu bar is an ordinary screen
8651 line, and this makes it get updated. */
8652 w->update_mode_line = Qt;
8653 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8654
8655 unbind_to (count, Qnil);
8656 set_buffer_internal_1 (prev);
8657 }
8658 }
8659 }
8660
8661
8662 \f
8663 /***********************************************************************
8664 Output Cursor
8665 ***********************************************************************/
8666
8667 #ifdef HAVE_WINDOW_SYSTEM
8668
8669 /* EXPORT:
8670 Nominal cursor position -- where to draw output.
8671 HPOS and VPOS are window relative glyph matrix coordinates.
8672 X and Y are window relative pixel coordinates. */
8673
8674 struct cursor_pos output_cursor;
8675
8676
8677 /* EXPORT:
8678 Set the global variable output_cursor to CURSOR. All cursor
8679 positions are relative to updated_window. */
8680
8681 void
8682 set_output_cursor (cursor)
8683 struct cursor_pos *cursor;
8684 {
8685 output_cursor.hpos = cursor->hpos;
8686 output_cursor.vpos = cursor->vpos;
8687 output_cursor.x = cursor->x;
8688 output_cursor.y = cursor->y;
8689 }
8690
8691
8692 /* EXPORT for RIF:
8693 Set a nominal cursor position.
8694
8695 HPOS and VPOS are column/row positions in a window glyph matrix. X
8696 and Y are window text area relative pixel positions.
8697
8698 If this is done during an update, updated_window will contain the
8699 window that is being updated and the position is the future output
8700 cursor position for that window. If updated_window is null, use
8701 selected_window and display the cursor at the given position. */
8702
8703 void
8704 x_cursor_to (vpos, hpos, y, x)
8705 int vpos, hpos, y, x;
8706 {
8707 struct window *w;
8708
8709 /* If updated_window is not set, work on selected_window. */
8710 if (updated_window)
8711 w = updated_window;
8712 else
8713 w = XWINDOW (selected_window);
8714
8715 /* Set the output cursor. */
8716 output_cursor.hpos = hpos;
8717 output_cursor.vpos = vpos;
8718 output_cursor.x = x;
8719 output_cursor.y = y;
8720
8721 /* If not called as part of an update, really display the cursor.
8722 This will also set the cursor position of W. */
8723 if (updated_window == NULL)
8724 {
8725 BLOCK_INPUT;
8726 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8727 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8728 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8729 UNBLOCK_INPUT;
8730 }
8731 }
8732
8733 #endif /* HAVE_WINDOW_SYSTEM */
8734
8735 \f
8736 /***********************************************************************
8737 Tool-bars
8738 ***********************************************************************/
8739
8740 #ifdef HAVE_WINDOW_SYSTEM
8741
8742 /* Where the mouse was last time we reported a mouse event. */
8743
8744 FRAME_PTR last_mouse_frame;
8745
8746 /* Tool-bar item index of the item on which a mouse button was pressed
8747 or -1. */
8748
8749 int last_tool_bar_item;
8750
8751
8752 /* Update the tool-bar item list for frame F. This has to be done
8753 before we start to fill in any display lines. Called from
8754 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8755 and restore it here. */
8756
8757 static void
8758 update_tool_bar (f, save_match_data)
8759 struct frame *f;
8760 int save_match_data;
8761 {
8762 #ifdef USE_GTK
8763 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8764 #else
8765 int do_update = WINDOWP (f->tool_bar_window)
8766 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8767 #endif
8768
8769 if (do_update)
8770 {
8771 Lisp_Object window;
8772 struct window *w;
8773
8774 window = FRAME_SELECTED_WINDOW (f);
8775 w = XWINDOW (window);
8776
8777 /* If the user has switched buffers or windows, we need to
8778 recompute to reflect the new bindings. But we'll
8779 recompute when update_mode_lines is set too; that means
8780 that people can use force-mode-line-update to request
8781 that the menu bar be recomputed. The adverse effect on
8782 the rest of the redisplay algorithm is about the same as
8783 windows_or_buffers_changed anyway. */
8784 if (windows_or_buffers_changed
8785 || !NILP (w->update_mode_line)
8786 || update_mode_lines
8787 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8788 < BUF_MODIFF (XBUFFER (w->buffer)))
8789 != !NILP (w->last_had_star))
8790 || ((!NILP (Vtransient_mark_mode)
8791 && !NILP (XBUFFER (w->buffer)->mark_active))
8792 != !NILP (w->region_showing)))
8793 {
8794 struct buffer *prev = current_buffer;
8795 int count = SPECPDL_INDEX ();
8796 Lisp_Object new_tool_bar;
8797 int new_n_tool_bar;
8798 struct gcpro gcpro1;
8799
8800 /* Set current_buffer to the buffer of the selected
8801 window of the frame, so that we get the right local
8802 keymaps. */
8803 set_buffer_internal_1 (XBUFFER (w->buffer));
8804
8805 /* Save match data, if we must. */
8806 if (save_match_data)
8807 record_unwind_save_match_data ();
8808
8809 /* Make sure that we don't accidentally use bogus keymaps. */
8810 if (NILP (Voverriding_local_map_menu_flag))
8811 {
8812 specbind (Qoverriding_terminal_local_map, Qnil);
8813 specbind (Qoverriding_local_map, Qnil);
8814 }
8815
8816 GCPRO1 (new_tool_bar);
8817
8818 /* Build desired tool-bar items from keymaps. */
8819 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8820 &new_n_tool_bar);
8821
8822 /* Redisplay the tool-bar if we changed it. */
8823 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8824 {
8825 /* Redisplay that happens asynchronously due to an expose event
8826 may access f->tool_bar_items. Make sure we update both
8827 variables within BLOCK_INPUT so no such event interrupts. */
8828 BLOCK_INPUT;
8829 f->tool_bar_items = new_tool_bar;
8830 f->n_tool_bar_items = new_n_tool_bar;
8831 w->update_mode_line = Qt;
8832 UNBLOCK_INPUT;
8833 }
8834
8835 UNGCPRO;
8836
8837 unbind_to (count, Qnil);
8838 set_buffer_internal_1 (prev);
8839 }
8840 }
8841 }
8842
8843
8844 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8845 F's desired tool-bar contents. F->tool_bar_items must have
8846 been set up previously by calling prepare_menu_bars. */
8847
8848 static void
8849 build_desired_tool_bar_string (f)
8850 struct frame *f;
8851 {
8852 int i, size, size_needed;
8853 struct gcpro gcpro1, gcpro2, gcpro3;
8854 Lisp_Object image, plist, props;
8855
8856 image = plist = props = Qnil;
8857 GCPRO3 (image, plist, props);
8858
8859 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8860 Otherwise, make a new string. */
8861
8862 /* The size of the string we might be able to reuse. */
8863 size = (STRINGP (f->desired_tool_bar_string)
8864 ? SCHARS (f->desired_tool_bar_string)
8865 : 0);
8866
8867 /* We need one space in the string for each image. */
8868 size_needed = f->n_tool_bar_items;
8869
8870 /* Reuse f->desired_tool_bar_string, if possible. */
8871 if (size < size_needed || NILP (f->desired_tool_bar_string))
8872 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8873 make_number (' '));
8874 else
8875 {
8876 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8877 Fremove_text_properties (make_number (0), make_number (size),
8878 props, f->desired_tool_bar_string);
8879 }
8880
8881 /* Put a `display' property on the string for the images to display,
8882 put a `menu_item' property on tool-bar items with a value that
8883 is the index of the item in F's tool-bar item vector. */
8884 for (i = 0; i < f->n_tool_bar_items; ++i)
8885 {
8886 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8887
8888 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8889 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8890 int hmargin, vmargin, relief, idx, end;
8891 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8892
8893 /* If image is a vector, choose the image according to the
8894 button state. */
8895 image = PROP (TOOL_BAR_ITEM_IMAGES);
8896 if (VECTORP (image))
8897 {
8898 if (enabled_p)
8899 idx = (selected_p
8900 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8901 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8902 else
8903 idx = (selected_p
8904 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8905 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8906
8907 xassert (ASIZE (image) >= idx);
8908 image = AREF (image, idx);
8909 }
8910 else
8911 idx = -1;
8912
8913 /* Ignore invalid image specifications. */
8914 if (!valid_image_p (image))
8915 continue;
8916
8917 /* Display the tool-bar button pressed, or depressed. */
8918 plist = Fcopy_sequence (XCDR (image));
8919
8920 /* Compute margin and relief to draw. */
8921 relief = (tool_bar_button_relief >= 0
8922 ? tool_bar_button_relief
8923 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8924 hmargin = vmargin = relief;
8925
8926 if (INTEGERP (Vtool_bar_button_margin)
8927 && XINT (Vtool_bar_button_margin) > 0)
8928 {
8929 hmargin += XFASTINT (Vtool_bar_button_margin);
8930 vmargin += XFASTINT (Vtool_bar_button_margin);
8931 }
8932 else if (CONSP (Vtool_bar_button_margin))
8933 {
8934 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8935 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8936 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8937
8938 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8939 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8940 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8941 }
8942
8943 if (auto_raise_tool_bar_buttons_p)
8944 {
8945 /* Add a `:relief' property to the image spec if the item is
8946 selected. */
8947 if (selected_p)
8948 {
8949 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8950 hmargin -= relief;
8951 vmargin -= relief;
8952 }
8953 }
8954 else
8955 {
8956 /* If image is selected, display it pressed, i.e. with a
8957 negative relief. If it's not selected, display it with a
8958 raised relief. */
8959 plist = Fplist_put (plist, QCrelief,
8960 (selected_p
8961 ? make_number (-relief)
8962 : make_number (relief)));
8963 hmargin -= relief;
8964 vmargin -= relief;
8965 }
8966
8967 /* Put a margin around the image. */
8968 if (hmargin || vmargin)
8969 {
8970 if (hmargin == vmargin)
8971 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8972 else
8973 plist = Fplist_put (plist, QCmargin,
8974 Fcons (make_number (hmargin),
8975 make_number (vmargin)));
8976 }
8977
8978 /* If button is not enabled, and we don't have special images
8979 for the disabled state, make the image appear disabled by
8980 applying an appropriate algorithm to it. */
8981 if (!enabled_p && idx < 0)
8982 plist = Fplist_put (plist, QCconversion, Qdisabled);
8983
8984 /* Put a `display' text property on the string for the image to
8985 display. Put a `menu-item' property on the string that gives
8986 the start of this item's properties in the tool-bar items
8987 vector. */
8988 image = Fcons (Qimage, plist);
8989 props = list4 (Qdisplay, image,
8990 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8991
8992 /* Let the last image hide all remaining spaces in the tool bar
8993 string. The string can be longer than needed when we reuse a
8994 previous string. */
8995 if (i + 1 == f->n_tool_bar_items)
8996 end = SCHARS (f->desired_tool_bar_string);
8997 else
8998 end = i + 1;
8999 Fadd_text_properties (make_number (i), make_number (end),
9000 props, f->desired_tool_bar_string);
9001 #undef PROP
9002 }
9003
9004 UNGCPRO;
9005 }
9006
9007
9008 /* Display one line of the tool-bar of frame IT->f. */
9009
9010 static void
9011 display_tool_bar_line (it)
9012 struct it *it;
9013 {
9014 struct glyph_row *row = it->glyph_row;
9015 int max_x = it->last_visible_x;
9016 struct glyph *last;
9017
9018 prepare_desired_row (row);
9019 row->y = it->current_y;
9020
9021 /* Note that this isn't made use of if the face hasn't a box,
9022 so there's no need to check the face here. */
9023 it->start_of_box_run_p = 1;
9024
9025 while (it->current_x < max_x)
9026 {
9027 int x_before, x, n_glyphs_before, i, nglyphs;
9028
9029 /* Get the next display element. */
9030 if (!get_next_display_element (it))
9031 break;
9032
9033 /* Produce glyphs. */
9034 x_before = it->current_x;
9035 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9036 PRODUCE_GLYPHS (it);
9037
9038 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9039 i = 0;
9040 x = x_before;
9041 while (i < nglyphs)
9042 {
9043 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9044
9045 if (x + glyph->pixel_width > max_x)
9046 {
9047 /* Glyph doesn't fit on line. */
9048 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9049 it->current_x = x;
9050 goto out;
9051 }
9052
9053 ++it->hpos;
9054 x += glyph->pixel_width;
9055 ++i;
9056 }
9057
9058 /* Stop at line ends. */
9059 if (ITERATOR_AT_END_OF_LINE_P (it))
9060 break;
9061
9062 set_iterator_to_next (it, 1);
9063 }
9064
9065 out:;
9066
9067 row->displays_text_p = row->used[TEXT_AREA] != 0;
9068 extend_face_to_end_of_line (it);
9069 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9070 last->right_box_line_p = 1;
9071 if (last == row->glyphs[TEXT_AREA])
9072 last->left_box_line_p = 1;
9073 compute_line_metrics (it);
9074
9075 /* If line is empty, make it occupy the rest of the tool-bar. */
9076 if (!row->displays_text_p)
9077 {
9078 row->height = row->phys_height = it->last_visible_y - row->y;
9079 row->ascent = row->phys_ascent = 0;
9080 row->extra_line_spacing = 0;
9081 }
9082
9083 row->full_width_p = 1;
9084 row->continued_p = 0;
9085 row->truncated_on_left_p = 0;
9086 row->truncated_on_right_p = 0;
9087
9088 it->current_x = it->hpos = 0;
9089 it->current_y += row->height;
9090 ++it->vpos;
9091 ++it->glyph_row;
9092 }
9093
9094
9095 /* Value is the number of screen lines needed to make all tool-bar
9096 items of frame F visible. */
9097
9098 static int
9099 tool_bar_lines_needed (f)
9100 struct frame *f;
9101 {
9102 struct window *w = XWINDOW (f->tool_bar_window);
9103 struct it it;
9104
9105 /* Initialize an iterator for iteration over
9106 F->desired_tool_bar_string in the tool-bar window of frame F. */
9107 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9108 it.first_visible_x = 0;
9109 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9110 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9111
9112 while (!ITERATOR_AT_END_P (&it))
9113 {
9114 it.glyph_row = w->desired_matrix->rows;
9115 clear_glyph_row (it.glyph_row);
9116 display_tool_bar_line (&it);
9117 }
9118
9119 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9120 }
9121
9122
9123 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9124 0, 1, 0,
9125 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9126 (frame)
9127 Lisp_Object frame;
9128 {
9129 struct frame *f;
9130 struct window *w;
9131 int nlines = 0;
9132
9133 if (NILP (frame))
9134 frame = selected_frame;
9135 else
9136 CHECK_FRAME (frame);
9137 f = XFRAME (frame);
9138
9139 if (WINDOWP (f->tool_bar_window)
9140 || (w = XWINDOW (f->tool_bar_window),
9141 WINDOW_TOTAL_LINES (w) > 0))
9142 {
9143 update_tool_bar (f, 1);
9144 if (f->n_tool_bar_items)
9145 {
9146 build_desired_tool_bar_string (f);
9147 nlines = tool_bar_lines_needed (f);
9148 }
9149 }
9150
9151 return make_number (nlines);
9152 }
9153
9154
9155 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9156 height should be changed. */
9157
9158 static int
9159 redisplay_tool_bar (f)
9160 struct frame *f;
9161 {
9162 struct window *w;
9163 struct it it;
9164 struct glyph_row *row;
9165 int change_height_p = 0;
9166
9167 #ifdef USE_GTK
9168 if (FRAME_EXTERNAL_TOOL_BAR (f))
9169 update_frame_tool_bar (f);
9170 return 0;
9171 #endif
9172
9173 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9174 do anything. This means you must start with tool-bar-lines
9175 non-zero to get the auto-sizing effect. Or in other words, you
9176 can turn off tool-bars by specifying tool-bar-lines zero. */
9177 if (!WINDOWP (f->tool_bar_window)
9178 || (w = XWINDOW (f->tool_bar_window),
9179 WINDOW_TOTAL_LINES (w) == 0))
9180 return 0;
9181
9182 /* Set up an iterator for the tool-bar window. */
9183 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9184 it.first_visible_x = 0;
9185 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9186 row = it.glyph_row;
9187
9188 /* Build a string that represents the contents of the tool-bar. */
9189 build_desired_tool_bar_string (f);
9190 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9191
9192 /* Display as many lines as needed to display all tool-bar items. */
9193 while (it.current_y < it.last_visible_y)
9194 display_tool_bar_line (&it);
9195
9196 /* It doesn't make much sense to try scrolling in the tool-bar
9197 window, so don't do it. */
9198 w->desired_matrix->no_scrolling_p = 1;
9199 w->must_be_updated_p = 1;
9200
9201 if (auto_resize_tool_bars_p)
9202 {
9203 int nlines;
9204
9205 /* If we couldn't display everything, change the tool-bar's
9206 height. */
9207 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9208 change_height_p = 1;
9209
9210 /* If there are blank lines at the end, except for a partially
9211 visible blank line at the end that is smaller than
9212 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9213 row = it.glyph_row - 1;
9214 if (!row->displays_text_p
9215 && row->height >= FRAME_LINE_HEIGHT (f))
9216 change_height_p = 1;
9217
9218 /* If row displays tool-bar items, but is partially visible,
9219 change the tool-bar's height. */
9220 if (row->displays_text_p
9221 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9222 change_height_p = 1;
9223
9224 /* Resize windows as needed by changing the `tool-bar-lines'
9225 frame parameter. */
9226 if (change_height_p
9227 && (nlines = tool_bar_lines_needed (f),
9228 nlines != WINDOW_TOTAL_LINES (w)))
9229 {
9230 extern Lisp_Object Qtool_bar_lines;
9231 Lisp_Object frame;
9232 int old_height = WINDOW_TOTAL_LINES (w);
9233
9234 XSETFRAME (frame, f);
9235 clear_glyph_matrix (w->desired_matrix);
9236 Fmodify_frame_parameters (frame,
9237 Fcons (Fcons (Qtool_bar_lines,
9238 make_number (nlines)),
9239 Qnil));
9240 if (WINDOW_TOTAL_LINES (w) != old_height)
9241 fonts_changed_p = 1;
9242 }
9243 }
9244
9245 return change_height_p;
9246 }
9247
9248
9249 /* Get information about the tool-bar item which is displayed in GLYPH
9250 on frame F. Return in *PROP_IDX the index where tool-bar item
9251 properties start in F->tool_bar_items. Value is zero if
9252 GLYPH doesn't display a tool-bar item. */
9253
9254 static int
9255 tool_bar_item_info (f, glyph, prop_idx)
9256 struct frame *f;
9257 struct glyph *glyph;
9258 int *prop_idx;
9259 {
9260 Lisp_Object prop;
9261 int success_p;
9262 int charpos;
9263
9264 /* This function can be called asynchronously, which means we must
9265 exclude any possibility that Fget_text_property signals an
9266 error. */
9267 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9268 charpos = max (0, charpos);
9269
9270 /* Get the text property `menu-item' at pos. The value of that
9271 property is the start index of this item's properties in
9272 F->tool_bar_items. */
9273 prop = Fget_text_property (make_number (charpos),
9274 Qmenu_item, f->current_tool_bar_string);
9275 if (INTEGERP (prop))
9276 {
9277 *prop_idx = XINT (prop);
9278 success_p = 1;
9279 }
9280 else
9281 success_p = 0;
9282
9283 return success_p;
9284 }
9285
9286 \f
9287 /* Get information about the tool-bar item at position X/Y on frame F.
9288 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9289 the current matrix of the tool-bar window of F, or NULL if not
9290 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9291 item in F->tool_bar_items. Value is
9292
9293 -1 if X/Y is not on a tool-bar item
9294 0 if X/Y is on the same item that was highlighted before.
9295 1 otherwise. */
9296
9297 static int
9298 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9299 struct frame *f;
9300 int x, y;
9301 struct glyph **glyph;
9302 int *hpos, *vpos, *prop_idx;
9303 {
9304 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9305 struct window *w = XWINDOW (f->tool_bar_window);
9306 int area;
9307
9308 /* Find the glyph under X/Y. */
9309 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9310 if (*glyph == NULL)
9311 return -1;
9312
9313 /* Get the start of this tool-bar item's properties in
9314 f->tool_bar_items. */
9315 if (!tool_bar_item_info (f, *glyph, prop_idx))
9316 return -1;
9317
9318 /* Is mouse on the highlighted item? */
9319 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9320 && *vpos >= dpyinfo->mouse_face_beg_row
9321 && *vpos <= dpyinfo->mouse_face_end_row
9322 && (*vpos > dpyinfo->mouse_face_beg_row
9323 || *hpos >= dpyinfo->mouse_face_beg_col)
9324 && (*vpos < dpyinfo->mouse_face_end_row
9325 || *hpos < dpyinfo->mouse_face_end_col
9326 || dpyinfo->mouse_face_past_end))
9327 return 0;
9328
9329 return 1;
9330 }
9331
9332
9333 /* EXPORT:
9334 Handle mouse button event on the tool-bar of frame F, at
9335 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9336 0 for button release. MODIFIERS is event modifiers for button
9337 release. */
9338
9339 void
9340 handle_tool_bar_click (f, x, y, down_p, modifiers)
9341 struct frame *f;
9342 int x, y, down_p;
9343 unsigned int modifiers;
9344 {
9345 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9346 struct window *w = XWINDOW (f->tool_bar_window);
9347 int hpos, vpos, prop_idx;
9348 struct glyph *glyph;
9349 Lisp_Object enabled_p;
9350
9351 /* If not on the highlighted tool-bar item, return. */
9352 frame_to_window_pixel_xy (w, &x, &y);
9353 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9354 return;
9355
9356 /* If item is disabled, do nothing. */
9357 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9358 if (NILP (enabled_p))
9359 return;
9360
9361 if (down_p)
9362 {
9363 /* Show item in pressed state. */
9364 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9365 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9366 last_tool_bar_item = prop_idx;
9367 }
9368 else
9369 {
9370 Lisp_Object key, frame;
9371 struct input_event event;
9372 EVENT_INIT (event);
9373
9374 /* Show item in released state. */
9375 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9376 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9377
9378 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9379
9380 XSETFRAME (frame, f);
9381 event.kind = TOOL_BAR_EVENT;
9382 event.frame_or_window = frame;
9383 event.arg = frame;
9384 kbd_buffer_store_event (&event);
9385
9386 event.kind = TOOL_BAR_EVENT;
9387 event.frame_or_window = frame;
9388 event.arg = key;
9389 event.modifiers = modifiers;
9390 kbd_buffer_store_event (&event);
9391 last_tool_bar_item = -1;
9392 }
9393 }
9394
9395
9396 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9397 tool-bar window-relative coordinates X/Y. Called from
9398 note_mouse_highlight. */
9399
9400 static void
9401 note_tool_bar_highlight (f, x, y)
9402 struct frame *f;
9403 int x, y;
9404 {
9405 Lisp_Object window = f->tool_bar_window;
9406 struct window *w = XWINDOW (window);
9407 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9408 int hpos, vpos;
9409 struct glyph *glyph;
9410 struct glyph_row *row;
9411 int i;
9412 Lisp_Object enabled_p;
9413 int prop_idx;
9414 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9415 int mouse_down_p, rc;
9416
9417 /* Function note_mouse_highlight is called with negative x(y
9418 values when mouse moves outside of the frame. */
9419 if (x <= 0 || y <= 0)
9420 {
9421 clear_mouse_face (dpyinfo);
9422 return;
9423 }
9424
9425 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9426 if (rc < 0)
9427 {
9428 /* Not on tool-bar item. */
9429 clear_mouse_face (dpyinfo);
9430 return;
9431 }
9432 else if (rc == 0)
9433 /* On same tool-bar item as before. */
9434 goto set_help_echo;
9435
9436 clear_mouse_face (dpyinfo);
9437
9438 /* Mouse is down, but on different tool-bar item? */
9439 mouse_down_p = (dpyinfo->grabbed
9440 && f == last_mouse_frame
9441 && FRAME_LIVE_P (f));
9442 if (mouse_down_p
9443 && last_tool_bar_item != prop_idx)
9444 return;
9445
9446 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9447 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9448
9449 /* If tool-bar item is not enabled, don't highlight it. */
9450 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9451 if (!NILP (enabled_p))
9452 {
9453 /* Compute the x-position of the glyph. In front and past the
9454 image is a space. We include this in the highlighted area. */
9455 row = MATRIX_ROW (w->current_matrix, vpos);
9456 for (i = x = 0; i < hpos; ++i)
9457 x += row->glyphs[TEXT_AREA][i].pixel_width;
9458
9459 /* Record this as the current active region. */
9460 dpyinfo->mouse_face_beg_col = hpos;
9461 dpyinfo->mouse_face_beg_row = vpos;
9462 dpyinfo->mouse_face_beg_x = x;
9463 dpyinfo->mouse_face_beg_y = row->y;
9464 dpyinfo->mouse_face_past_end = 0;
9465
9466 dpyinfo->mouse_face_end_col = hpos + 1;
9467 dpyinfo->mouse_face_end_row = vpos;
9468 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9469 dpyinfo->mouse_face_end_y = row->y;
9470 dpyinfo->mouse_face_window = window;
9471 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9472
9473 /* Display it as active. */
9474 show_mouse_face (dpyinfo, draw);
9475 dpyinfo->mouse_face_image_state = draw;
9476 }
9477
9478 set_help_echo:
9479
9480 /* Set help_echo_string to a help string to display for this tool-bar item.
9481 XTread_socket does the rest. */
9482 help_echo_object = help_echo_window = Qnil;
9483 help_echo_pos = -1;
9484 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9485 if (NILP (help_echo_string))
9486 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9487 }
9488
9489 #endif /* HAVE_WINDOW_SYSTEM */
9490
9491
9492 \f
9493 /************************************************************************
9494 Horizontal scrolling
9495 ************************************************************************/
9496
9497 static int hscroll_window_tree P_ ((Lisp_Object));
9498 static int hscroll_windows P_ ((Lisp_Object));
9499
9500 /* For all leaf windows in the window tree rooted at WINDOW, set their
9501 hscroll value so that PT is (i) visible in the window, and (ii) so
9502 that it is not within a certain margin at the window's left and
9503 right border. Value is non-zero if any window's hscroll has been
9504 changed. */
9505
9506 static int
9507 hscroll_window_tree (window)
9508 Lisp_Object window;
9509 {
9510 int hscrolled_p = 0;
9511 int hscroll_relative_p = FLOATP (Vhscroll_step);
9512 int hscroll_step_abs = 0;
9513 double hscroll_step_rel = 0;
9514
9515 if (hscroll_relative_p)
9516 {
9517 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9518 if (hscroll_step_rel < 0)
9519 {
9520 hscroll_relative_p = 0;
9521 hscroll_step_abs = 0;
9522 }
9523 }
9524 else if (INTEGERP (Vhscroll_step))
9525 {
9526 hscroll_step_abs = XINT (Vhscroll_step);
9527 if (hscroll_step_abs < 0)
9528 hscroll_step_abs = 0;
9529 }
9530 else
9531 hscroll_step_abs = 0;
9532
9533 while (WINDOWP (window))
9534 {
9535 struct window *w = XWINDOW (window);
9536
9537 if (WINDOWP (w->hchild))
9538 hscrolled_p |= hscroll_window_tree (w->hchild);
9539 else if (WINDOWP (w->vchild))
9540 hscrolled_p |= hscroll_window_tree (w->vchild);
9541 else if (w->cursor.vpos >= 0)
9542 {
9543 int h_margin;
9544 int text_area_width;
9545 struct glyph_row *current_cursor_row
9546 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9547 struct glyph_row *desired_cursor_row
9548 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9549 struct glyph_row *cursor_row
9550 = (desired_cursor_row->enabled_p
9551 ? desired_cursor_row
9552 : current_cursor_row);
9553
9554 text_area_width = window_box_width (w, TEXT_AREA);
9555
9556 /* Scroll when cursor is inside this scroll margin. */
9557 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9558
9559 if ((XFASTINT (w->hscroll)
9560 && w->cursor.x <= h_margin)
9561 || (cursor_row->enabled_p
9562 && cursor_row->truncated_on_right_p
9563 && (w->cursor.x >= text_area_width - h_margin)))
9564 {
9565 struct it it;
9566 int hscroll;
9567 struct buffer *saved_current_buffer;
9568 int pt;
9569 int wanted_x;
9570
9571 /* Find point in a display of infinite width. */
9572 saved_current_buffer = current_buffer;
9573 current_buffer = XBUFFER (w->buffer);
9574
9575 if (w == XWINDOW (selected_window))
9576 pt = BUF_PT (current_buffer);
9577 else
9578 {
9579 pt = marker_position (w->pointm);
9580 pt = max (BEGV, pt);
9581 pt = min (ZV, pt);
9582 }
9583
9584 /* Move iterator to pt starting at cursor_row->start in
9585 a line with infinite width. */
9586 init_to_row_start (&it, w, cursor_row);
9587 it.last_visible_x = INFINITY;
9588 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9589 current_buffer = saved_current_buffer;
9590
9591 /* Position cursor in window. */
9592 if (!hscroll_relative_p && hscroll_step_abs == 0)
9593 hscroll = max (0, (it.current_x
9594 - (ITERATOR_AT_END_OF_LINE_P (&it)
9595 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9596 : (text_area_width / 2))))
9597 / FRAME_COLUMN_WIDTH (it.f);
9598 else if (w->cursor.x >= text_area_width - h_margin)
9599 {
9600 if (hscroll_relative_p)
9601 wanted_x = text_area_width * (1 - hscroll_step_rel)
9602 - h_margin;
9603 else
9604 wanted_x = text_area_width
9605 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9606 - h_margin;
9607 hscroll
9608 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9609 }
9610 else
9611 {
9612 if (hscroll_relative_p)
9613 wanted_x = text_area_width * hscroll_step_rel
9614 + h_margin;
9615 else
9616 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9617 + h_margin;
9618 hscroll
9619 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9620 }
9621 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9622
9623 /* Don't call Fset_window_hscroll if value hasn't
9624 changed because it will prevent redisplay
9625 optimizations. */
9626 if (XFASTINT (w->hscroll) != hscroll)
9627 {
9628 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9629 w->hscroll = make_number (hscroll);
9630 hscrolled_p = 1;
9631 }
9632 }
9633 }
9634
9635 window = w->next;
9636 }
9637
9638 /* Value is non-zero if hscroll of any leaf window has been changed. */
9639 return hscrolled_p;
9640 }
9641
9642
9643 /* Set hscroll so that cursor is visible and not inside horizontal
9644 scroll margins for all windows in the tree rooted at WINDOW. See
9645 also hscroll_window_tree above. Value is non-zero if any window's
9646 hscroll has been changed. If it has, desired matrices on the frame
9647 of WINDOW are cleared. */
9648
9649 static int
9650 hscroll_windows (window)
9651 Lisp_Object window;
9652 {
9653 int hscrolled_p;
9654
9655 if (automatic_hscrolling_p)
9656 {
9657 hscrolled_p = hscroll_window_tree (window);
9658 if (hscrolled_p)
9659 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9660 }
9661 else
9662 hscrolled_p = 0;
9663 return hscrolled_p;
9664 }
9665
9666
9667 \f
9668 /************************************************************************
9669 Redisplay
9670 ************************************************************************/
9671
9672 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9673 to a non-zero value. This is sometimes handy to have in a debugger
9674 session. */
9675
9676 #if GLYPH_DEBUG
9677
9678 /* First and last unchanged row for try_window_id. */
9679
9680 int debug_first_unchanged_at_end_vpos;
9681 int debug_last_unchanged_at_beg_vpos;
9682
9683 /* Delta vpos and y. */
9684
9685 int debug_dvpos, debug_dy;
9686
9687 /* Delta in characters and bytes for try_window_id. */
9688
9689 int debug_delta, debug_delta_bytes;
9690
9691 /* Values of window_end_pos and window_end_vpos at the end of
9692 try_window_id. */
9693
9694 EMACS_INT debug_end_pos, debug_end_vpos;
9695
9696 /* Append a string to W->desired_matrix->method. FMT is a printf
9697 format string. A1...A9 are a supplement for a variable-length
9698 argument list. If trace_redisplay_p is non-zero also printf the
9699 resulting string to stderr. */
9700
9701 static void
9702 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9703 struct window *w;
9704 char *fmt;
9705 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9706 {
9707 char buffer[512];
9708 char *method = w->desired_matrix->method;
9709 int len = strlen (method);
9710 int size = sizeof w->desired_matrix->method;
9711 int remaining = size - len - 1;
9712
9713 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9714 if (len && remaining)
9715 {
9716 method[len] = '|';
9717 --remaining, ++len;
9718 }
9719
9720 strncpy (method + len, buffer, remaining);
9721
9722 if (trace_redisplay_p)
9723 fprintf (stderr, "%p (%s): %s\n",
9724 w,
9725 ((BUFFERP (w->buffer)
9726 && STRINGP (XBUFFER (w->buffer)->name))
9727 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9728 : "no buffer"),
9729 buffer);
9730 }
9731
9732 #endif /* GLYPH_DEBUG */
9733
9734
9735 /* Value is non-zero if all changes in window W, which displays
9736 current_buffer, are in the text between START and END. START is a
9737 buffer position, END is given as a distance from Z. Used in
9738 redisplay_internal for display optimization. */
9739
9740 static INLINE int
9741 text_outside_line_unchanged_p (w, start, end)
9742 struct window *w;
9743 int start, end;
9744 {
9745 int unchanged_p = 1;
9746
9747 /* If text or overlays have changed, see where. */
9748 if (XFASTINT (w->last_modified) < MODIFF
9749 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9750 {
9751 /* Gap in the line? */
9752 if (GPT < start || Z - GPT < end)
9753 unchanged_p = 0;
9754
9755 /* Changes start in front of the line, or end after it? */
9756 if (unchanged_p
9757 && (BEG_UNCHANGED < start - 1
9758 || END_UNCHANGED < end))
9759 unchanged_p = 0;
9760
9761 /* If selective display, can't optimize if changes start at the
9762 beginning of the line. */
9763 if (unchanged_p
9764 && INTEGERP (current_buffer->selective_display)
9765 && XINT (current_buffer->selective_display) > 0
9766 && (BEG_UNCHANGED < start || GPT <= start))
9767 unchanged_p = 0;
9768
9769 /* If there are overlays at the start or end of the line, these
9770 may have overlay strings with newlines in them. A change at
9771 START, for instance, may actually concern the display of such
9772 overlay strings as well, and they are displayed on different
9773 lines. So, quickly rule out this case. (For the future, it
9774 might be desirable to implement something more telling than
9775 just BEG/END_UNCHANGED.) */
9776 if (unchanged_p)
9777 {
9778 if (BEG + BEG_UNCHANGED == start
9779 && overlay_touches_p (start))
9780 unchanged_p = 0;
9781 if (END_UNCHANGED == end
9782 && overlay_touches_p (Z - end))
9783 unchanged_p = 0;
9784 }
9785 }
9786
9787 return unchanged_p;
9788 }
9789
9790
9791 /* Do a frame update, taking possible shortcuts into account. This is
9792 the main external entry point for redisplay.
9793
9794 If the last redisplay displayed an echo area message and that message
9795 is no longer requested, we clear the echo area or bring back the
9796 mini-buffer if that is in use. */
9797
9798 void
9799 redisplay ()
9800 {
9801 redisplay_internal (0);
9802 }
9803
9804
9805 static Lisp_Object
9806 overlay_arrow_string_or_property (var)
9807 Lisp_Object var;
9808 {
9809 Lisp_Object val;
9810
9811 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9812 return val;
9813
9814 return Voverlay_arrow_string;
9815 }
9816
9817 /* Return 1 if there are any overlay-arrows in current_buffer. */
9818 static int
9819 overlay_arrow_in_current_buffer_p ()
9820 {
9821 Lisp_Object vlist;
9822
9823 for (vlist = Voverlay_arrow_variable_list;
9824 CONSP (vlist);
9825 vlist = XCDR (vlist))
9826 {
9827 Lisp_Object var = XCAR (vlist);
9828 Lisp_Object val;
9829
9830 if (!SYMBOLP (var))
9831 continue;
9832 val = find_symbol_value (var);
9833 if (MARKERP (val)
9834 && current_buffer == XMARKER (val)->buffer)
9835 return 1;
9836 }
9837 return 0;
9838 }
9839
9840
9841 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9842 has changed. */
9843
9844 static int
9845 overlay_arrows_changed_p ()
9846 {
9847 Lisp_Object vlist;
9848
9849 for (vlist = Voverlay_arrow_variable_list;
9850 CONSP (vlist);
9851 vlist = XCDR (vlist))
9852 {
9853 Lisp_Object var = XCAR (vlist);
9854 Lisp_Object val, pstr;
9855
9856 if (!SYMBOLP (var))
9857 continue;
9858 val = find_symbol_value (var);
9859 if (!MARKERP (val))
9860 continue;
9861 if (! EQ (COERCE_MARKER (val),
9862 Fget (var, Qlast_arrow_position))
9863 || ! (pstr = overlay_arrow_string_or_property (var),
9864 EQ (pstr, Fget (var, Qlast_arrow_string))))
9865 return 1;
9866 }
9867 return 0;
9868 }
9869
9870 /* Mark overlay arrows to be updated on next redisplay. */
9871
9872 static void
9873 update_overlay_arrows (up_to_date)
9874 int up_to_date;
9875 {
9876 Lisp_Object vlist;
9877
9878 for (vlist = Voverlay_arrow_variable_list;
9879 CONSP (vlist);
9880 vlist = XCDR (vlist))
9881 {
9882 Lisp_Object var = XCAR (vlist);
9883
9884 if (!SYMBOLP (var))
9885 continue;
9886
9887 if (up_to_date > 0)
9888 {
9889 Lisp_Object val = find_symbol_value (var);
9890 Fput (var, Qlast_arrow_position,
9891 COERCE_MARKER (val));
9892 Fput (var, Qlast_arrow_string,
9893 overlay_arrow_string_or_property (var));
9894 }
9895 else if (up_to_date < 0
9896 || !NILP (Fget (var, Qlast_arrow_position)))
9897 {
9898 Fput (var, Qlast_arrow_position, Qt);
9899 Fput (var, Qlast_arrow_string, Qt);
9900 }
9901 }
9902 }
9903
9904
9905 /* Return overlay arrow string to display at row.
9906 Return integer (bitmap number) for arrow bitmap in left fringe.
9907 Return nil if no overlay arrow. */
9908
9909 static Lisp_Object
9910 overlay_arrow_at_row (it, row)
9911 struct it *it;
9912 struct glyph_row *row;
9913 {
9914 Lisp_Object vlist;
9915
9916 for (vlist = Voverlay_arrow_variable_list;
9917 CONSP (vlist);
9918 vlist = XCDR (vlist))
9919 {
9920 Lisp_Object var = XCAR (vlist);
9921 Lisp_Object val;
9922
9923 if (!SYMBOLP (var))
9924 continue;
9925
9926 val = find_symbol_value (var);
9927
9928 if (MARKERP (val)
9929 && current_buffer == XMARKER (val)->buffer
9930 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9931 {
9932 if (FRAME_WINDOW_P (it->f)
9933 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9934 {
9935 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9936 {
9937 int fringe_bitmap;
9938 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9939 return make_number (fringe_bitmap);
9940 }
9941 return make_number (-1); /* Use default arrow bitmap */
9942 }
9943 return overlay_arrow_string_or_property (var);
9944 }
9945 }
9946
9947 return Qnil;
9948 }
9949
9950 /* Return 1 if point moved out of or into a composition. Otherwise
9951 return 0. PREV_BUF and PREV_PT are the last point buffer and
9952 position. BUF and PT are the current point buffer and position. */
9953
9954 int
9955 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9956 struct buffer *prev_buf, *buf;
9957 int prev_pt, pt;
9958 {
9959 int start, end;
9960 Lisp_Object prop;
9961 Lisp_Object buffer;
9962
9963 XSETBUFFER (buffer, buf);
9964 /* Check a composition at the last point if point moved within the
9965 same buffer. */
9966 if (prev_buf == buf)
9967 {
9968 if (prev_pt == pt)
9969 /* Point didn't move. */
9970 return 0;
9971
9972 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9973 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9974 && COMPOSITION_VALID_P (start, end, prop)
9975 && start < prev_pt && end > prev_pt)
9976 /* The last point was within the composition. Return 1 iff
9977 point moved out of the composition. */
9978 return (pt <= start || pt >= end);
9979 }
9980
9981 /* Check a composition at the current point. */
9982 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9983 && find_composition (pt, -1, &start, &end, &prop, buffer)
9984 && COMPOSITION_VALID_P (start, end, prop)
9985 && start < pt && end > pt);
9986 }
9987
9988
9989 /* Reconsider the setting of B->clip_changed which is displayed
9990 in window W. */
9991
9992 static INLINE void
9993 reconsider_clip_changes (w, b)
9994 struct window *w;
9995 struct buffer *b;
9996 {
9997 if (b->clip_changed
9998 && !NILP (w->window_end_valid)
9999 && w->current_matrix->buffer == b
10000 && w->current_matrix->zv == BUF_ZV (b)
10001 && w->current_matrix->begv == BUF_BEGV (b))
10002 b->clip_changed = 0;
10003
10004 /* If display wasn't paused, and W is not a tool bar window, see if
10005 point has been moved into or out of a composition. In that case,
10006 we set b->clip_changed to 1 to force updating the screen. If
10007 b->clip_changed has already been set to 1, we can skip this
10008 check. */
10009 if (!b->clip_changed
10010 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10011 {
10012 int pt;
10013
10014 if (w == XWINDOW (selected_window))
10015 pt = BUF_PT (current_buffer);
10016 else
10017 pt = marker_position (w->pointm);
10018
10019 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10020 || pt != XINT (w->last_point))
10021 && check_point_in_composition (w->current_matrix->buffer,
10022 XINT (w->last_point),
10023 XBUFFER (w->buffer), pt))
10024 b->clip_changed = 1;
10025 }
10026 }
10027 \f
10028
10029 /* Select FRAME to forward the values of frame-local variables into C
10030 variables so that the redisplay routines can access those values
10031 directly. */
10032
10033 static void
10034 select_frame_for_redisplay (frame)
10035 Lisp_Object frame;
10036 {
10037 Lisp_Object tail, sym, val;
10038 Lisp_Object old = selected_frame;
10039
10040 selected_frame = frame;
10041
10042 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10043 if (CONSP (XCAR (tail))
10044 && (sym = XCAR (XCAR (tail)),
10045 SYMBOLP (sym))
10046 && (sym = indirect_variable (sym),
10047 val = SYMBOL_VALUE (sym),
10048 (BUFFER_LOCAL_VALUEP (val)
10049 || SOME_BUFFER_LOCAL_VALUEP (val)))
10050 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10051 Fsymbol_value (sym);
10052
10053 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10054 if (CONSP (XCAR (tail))
10055 && (sym = XCAR (XCAR (tail)),
10056 SYMBOLP (sym))
10057 && (sym = indirect_variable (sym),
10058 val = SYMBOL_VALUE (sym),
10059 (BUFFER_LOCAL_VALUEP (val)
10060 || SOME_BUFFER_LOCAL_VALUEP (val)))
10061 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10062 Fsymbol_value (sym);
10063 }
10064
10065
10066 #define STOP_POLLING \
10067 do { if (! polling_stopped_here) stop_polling (); \
10068 polling_stopped_here = 1; } while (0)
10069
10070 #define RESUME_POLLING \
10071 do { if (polling_stopped_here) start_polling (); \
10072 polling_stopped_here = 0; } while (0)
10073
10074
10075 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10076 response to any user action; therefore, we should preserve the echo
10077 area. (Actually, our caller does that job.) Perhaps in the future
10078 avoid recentering windows if it is not necessary; currently that
10079 causes some problems. */
10080
10081 static void
10082 redisplay_internal (preserve_echo_area)
10083 int preserve_echo_area;
10084 {
10085 struct window *w = XWINDOW (selected_window);
10086 struct frame *f = XFRAME (w->frame);
10087 int pause;
10088 int must_finish = 0;
10089 struct text_pos tlbufpos, tlendpos;
10090 int number_of_visible_frames;
10091 int count;
10092 struct frame *sf = SELECTED_FRAME ();
10093 int polling_stopped_here = 0;
10094
10095 /* Non-zero means redisplay has to consider all windows on all
10096 frames. Zero means, only selected_window is considered. */
10097 int consider_all_windows_p;
10098
10099 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10100
10101 /* No redisplay if running in batch mode or frame is not yet fully
10102 initialized, or redisplay is explicitly turned off by setting
10103 Vinhibit_redisplay. */
10104 if (noninteractive
10105 || !NILP (Vinhibit_redisplay)
10106 || !f->glyphs_initialized_p)
10107 return;
10108
10109 /* The flag redisplay_performed_directly_p is set by
10110 direct_output_for_insert when it already did the whole screen
10111 update necessary. */
10112 if (redisplay_performed_directly_p)
10113 {
10114 redisplay_performed_directly_p = 0;
10115 if (!hscroll_windows (selected_window))
10116 return;
10117 }
10118
10119 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10120 if (popup_activated ())
10121 return;
10122 #endif
10123
10124 /* I don't think this happens but let's be paranoid. */
10125 if (redisplaying_p)
10126 return;
10127
10128 /* Record a function that resets redisplaying_p to its old value
10129 when we leave this function. */
10130 count = SPECPDL_INDEX ();
10131 record_unwind_protect (unwind_redisplay,
10132 Fcons (make_number (redisplaying_p), selected_frame));
10133 ++redisplaying_p;
10134 specbind (Qinhibit_free_realized_faces, Qnil);
10135
10136 retry:
10137 pause = 0;
10138 reconsider_clip_changes (w, current_buffer);
10139
10140 /* If new fonts have been loaded that make a glyph matrix adjustment
10141 necessary, do it. */
10142 if (fonts_changed_p)
10143 {
10144 adjust_glyphs (NULL);
10145 ++windows_or_buffers_changed;
10146 fonts_changed_p = 0;
10147 }
10148
10149 /* If face_change_count is non-zero, init_iterator will free all
10150 realized faces, which includes the faces referenced from current
10151 matrices. So, we can't reuse current matrices in this case. */
10152 if (face_change_count)
10153 ++windows_or_buffers_changed;
10154
10155 if (FRAME_TERMCAP_P (sf)
10156 && FRAME_TTY (sf)->previous_terminal_frame != sf)
10157 {
10158 /* Since frames on a single ASCII terminal share the same
10159 display area, displaying a different frame means redisplay
10160 the whole thing. */
10161 windows_or_buffers_changed++;
10162 SET_FRAME_GARBAGED (sf);
10163 FRAME_TTY (sf)->previous_terminal_frame = sf;
10164 }
10165
10166 /* Set the visible flags for all frames. Do this before checking
10167 for resized or garbaged frames; they want to know if their frames
10168 are visible. See the comment in frame.h for
10169 FRAME_SAMPLE_VISIBILITY. */
10170 {
10171 Lisp_Object tail, frame;
10172
10173 number_of_visible_frames = 0;
10174
10175 FOR_EACH_FRAME (tail, frame)
10176 {
10177 struct frame *f = XFRAME (frame);
10178
10179 FRAME_SAMPLE_VISIBILITY (f);
10180 if (FRAME_VISIBLE_P (f))
10181 ++number_of_visible_frames;
10182 clear_desired_matrices (f);
10183 }
10184 }
10185
10186
10187 /* Notice any pending interrupt request to change frame size. */
10188 do_pending_window_change (1);
10189
10190 /* Clear frames marked as garbaged. */
10191 if (frame_garbaged)
10192 clear_garbaged_frames ();
10193
10194 /* Build menubar and tool-bar items. */
10195 prepare_menu_bars ();
10196
10197 if (windows_or_buffers_changed)
10198 update_mode_lines++;
10199
10200 /* Detect case that we need to write or remove a star in the mode line. */
10201 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10202 {
10203 w->update_mode_line = Qt;
10204 if (buffer_shared > 1)
10205 update_mode_lines++;
10206 }
10207
10208 /* If %c is in the mode line, update it if needed. */
10209 if (!NILP (w->column_number_displayed)
10210 /* This alternative quickly identifies a common case
10211 where no change is needed. */
10212 && !(PT == XFASTINT (w->last_point)
10213 && XFASTINT (w->last_modified) >= MODIFF
10214 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10215 && (XFASTINT (w->column_number_displayed)
10216 != (int) current_column ())) /* iftc */
10217 w->update_mode_line = Qt;
10218
10219 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10220
10221 /* The variable buffer_shared is set in redisplay_window and
10222 indicates that we redisplay a buffer in different windows. See
10223 there. */
10224 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10225 || cursor_type_changed);
10226
10227 /* If specs for an arrow have changed, do thorough redisplay
10228 to ensure we remove any arrow that should no longer exist. */
10229 if (overlay_arrows_changed_p ())
10230 consider_all_windows_p = windows_or_buffers_changed = 1;
10231
10232 /* Normally the message* functions will have already displayed and
10233 updated the echo area, but the frame may have been trashed, or
10234 the update may have been preempted, so display the echo area
10235 again here. Checking message_cleared_p captures the case that
10236 the echo area should be cleared. */
10237 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10238 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10239 || (message_cleared_p
10240 && minibuf_level == 0
10241 /* If the mini-window is currently selected, this means the
10242 echo-area doesn't show through. */
10243 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10244 {
10245 int window_height_changed_p = echo_area_display (0);
10246 must_finish = 1;
10247
10248 /* If we don't display the current message, don't clear the
10249 message_cleared_p flag, because, if we did, we wouldn't clear
10250 the echo area in the next redisplay which doesn't preserve
10251 the echo area. */
10252 if (!display_last_displayed_message_p)
10253 message_cleared_p = 0;
10254
10255 if (fonts_changed_p)
10256 goto retry;
10257 else if (window_height_changed_p)
10258 {
10259 consider_all_windows_p = 1;
10260 ++update_mode_lines;
10261 ++windows_or_buffers_changed;
10262
10263 /* If window configuration was changed, frames may have been
10264 marked garbaged. Clear them or we will experience
10265 surprises wrt scrolling. */
10266 if (frame_garbaged)
10267 clear_garbaged_frames ();
10268 }
10269 }
10270 else if (EQ (selected_window, minibuf_window)
10271 && (current_buffer->clip_changed
10272 || XFASTINT (w->last_modified) < MODIFF
10273 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10274 && resize_mini_window (w, 0))
10275 {
10276 /* Resized active mini-window to fit the size of what it is
10277 showing if its contents might have changed. */
10278 must_finish = 1;
10279 consider_all_windows_p = 1;
10280 ++windows_or_buffers_changed;
10281 ++update_mode_lines;
10282
10283 /* If window configuration was changed, frames may have been
10284 marked garbaged. Clear them or we will experience
10285 surprises wrt scrolling. */
10286 if (frame_garbaged)
10287 clear_garbaged_frames ();
10288 }
10289
10290
10291 /* If showing the region, and mark has changed, we must redisplay
10292 the whole window. The assignment to this_line_start_pos prevents
10293 the optimization directly below this if-statement. */
10294 if (((!NILP (Vtransient_mark_mode)
10295 && !NILP (XBUFFER (w->buffer)->mark_active))
10296 != !NILP (w->region_showing))
10297 || (!NILP (w->region_showing)
10298 && !EQ (w->region_showing,
10299 Fmarker_position (XBUFFER (w->buffer)->mark))))
10300 CHARPOS (this_line_start_pos) = 0;
10301
10302 /* Optimize the case that only the line containing the cursor in the
10303 selected window has changed. Variables starting with this_ are
10304 set in display_line and record information about the line
10305 containing the cursor. */
10306 tlbufpos = this_line_start_pos;
10307 tlendpos = this_line_end_pos;
10308 if (!consider_all_windows_p
10309 && CHARPOS (tlbufpos) > 0
10310 && NILP (w->update_mode_line)
10311 && !current_buffer->clip_changed
10312 && !current_buffer->prevent_redisplay_optimizations_p
10313 && FRAME_VISIBLE_P (XFRAME (w->frame))
10314 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10315 /* Make sure recorded data applies to current buffer, etc. */
10316 && this_line_buffer == current_buffer
10317 && current_buffer == XBUFFER (w->buffer)
10318 && NILP (w->force_start)
10319 && NILP (w->optional_new_start)
10320 /* Point must be on the line that we have info recorded about. */
10321 && PT >= CHARPOS (tlbufpos)
10322 && PT <= Z - CHARPOS (tlendpos)
10323 /* All text outside that line, including its final newline,
10324 must be unchanged */
10325 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10326 CHARPOS (tlendpos)))
10327 {
10328 if (CHARPOS (tlbufpos) > BEGV
10329 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10330 && (CHARPOS (tlbufpos) == ZV
10331 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10332 /* Former continuation line has disappeared by becoming empty */
10333 goto cancel;
10334 else if (XFASTINT (w->last_modified) < MODIFF
10335 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10336 || MINI_WINDOW_P (w))
10337 {
10338 /* We have to handle the case of continuation around a
10339 wide-column character (See the comment in indent.c around
10340 line 885).
10341
10342 For instance, in the following case:
10343
10344 -------- Insert --------
10345 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10346 J_I_ ==> J_I_ `^^' are cursors.
10347 ^^ ^^
10348 -------- --------
10349
10350 As we have to redraw the line above, we should goto cancel. */
10351
10352 struct it it;
10353 int line_height_before = this_line_pixel_height;
10354
10355 /* Note that start_display will handle the case that the
10356 line starting at tlbufpos is a continuation lines. */
10357 start_display (&it, w, tlbufpos);
10358
10359 /* Implementation note: It this still necessary? */
10360 if (it.current_x != this_line_start_x)
10361 goto cancel;
10362
10363 TRACE ((stderr, "trying display optimization 1\n"));
10364 w->cursor.vpos = -1;
10365 overlay_arrow_seen = 0;
10366 it.vpos = this_line_vpos;
10367 it.current_y = this_line_y;
10368 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10369 display_line (&it);
10370
10371 /* If line contains point, is not continued,
10372 and ends at same distance from eob as before, we win */
10373 if (w->cursor.vpos >= 0
10374 /* Line is not continued, otherwise this_line_start_pos
10375 would have been set to 0 in display_line. */
10376 && CHARPOS (this_line_start_pos)
10377 /* Line ends as before. */
10378 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10379 /* Line has same height as before. Otherwise other lines
10380 would have to be shifted up or down. */
10381 && this_line_pixel_height == line_height_before)
10382 {
10383 /* If this is not the window's last line, we must adjust
10384 the charstarts of the lines below. */
10385 if (it.current_y < it.last_visible_y)
10386 {
10387 struct glyph_row *row
10388 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10389 int delta, delta_bytes;
10390
10391 if (Z - CHARPOS (tlendpos) == ZV)
10392 {
10393 /* This line ends at end of (accessible part of)
10394 buffer. There is no newline to count. */
10395 delta = (Z
10396 - CHARPOS (tlendpos)
10397 - MATRIX_ROW_START_CHARPOS (row));
10398 delta_bytes = (Z_BYTE
10399 - BYTEPOS (tlendpos)
10400 - MATRIX_ROW_START_BYTEPOS (row));
10401 }
10402 else
10403 {
10404 /* This line ends in a newline. Must take
10405 account of the newline and the rest of the
10406 text that follows. */
10407 delta = (Z
10408 - CHARPOS (tlendpos)
10409 - MATRIX_ROW_START_CHARPOS (row));
10410 delta_bytes = (Z_BYTE
10411 - BYTEPOS (tlendpos)
10412 - MATRIX_ROW_START_BYTEPOS (row));
10413 }
10414
10415 increment_matrix_positions (w->current_matrix,
10416 this_line_vpos + 1,
10417 w->current_matrix->nrows,
10418 delta, delta_bytes);
10419 }
10420
10421 /* If this row displays text now but previously didn't,
10422 or vice versa, w->window_end_vpos may have to be
10423 adjusted. */
10424 if ((it.glyph_row - 1)->displays_text_p)
10425 {
10426 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10427 XSETINT (w->window_end_vpos, this_line_vpos);
10428 }
10429 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10430 && this_line_vpos > 0)
10431 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10432 w->window_end_valid = Qnil;
10433
10434 /* Update hint: No need to try to scroll in update_window. */
10435 w->desired_matrix->no_scrolling_p = 1;
10436
10437 #if GLYPH_DEBUG
10438 *w->desired_matrix->method = 0;
10439 debug_method_add (w, "optimization 1");
10440 #endif
10441 #ifdef HAVE_WINDOW_SYSTEM
10442 update_window_fringes (w, 0);
10443 #endif
10444 goto update;
10445 }
10446 else
10447 goto cancel;
10448 }
10449 else if (/* Cursor position hasn't changed. */
10450 PT == XFASTINT (w->last_point)
10451 /* Make sure the cursor was last displayed
10452 in this window. Otherwise we have to reposition it. */
10453 && 0 <= w->cursor.vpos
10454 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10455 {
10456 if (!must_finish)
10457 {
10458 do_pending_window_change (1);
10459
10460 /* We used to always goto end_of_redisplay here, but this
10461 isn't enough if we have a blinking cursor. */
10462 if (w->cursor_off_p == w->last_cursor_off_p)
10463 goto end_of_redisplay;
10464 }
10465 goto update;
10466 }
10467 /* If highlighting the region, or if the cursor is in the echo area,
10468 then we can't just move the cursor. */
10469 else if (! (!NILP (Vtransient_mark_mode)
10470 && !NILP (current_buffer->mark_active))
10471 && (EQ (selected_window, current_buffer->last_selected_window)
10472 || highlight_nonselected_windows)
10473 && NILP (w->region_showing)
10474 && NILP (Vshow_trailing_whitespace)
10475 && !cursor_in_echo_area)
10476 {
10477 struct it it;
10478 struct glyph_row *row;
10479
10480 /* Skip from tlbufpos to PT and see where it is. Note that
10481 PT may be in invisible text. If so, we will end at the
10482 next visible position. */
10483 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10484 NULL, DEFAULT_FACE_ID);
10485 it.current_x = this_line_start_x;
10486 it.current_y = this_line_y;
10487 it.vpos = this_line_vpos;
10488
10489 /* The call to move_it_to stops in front of PT, but
10490 moves over before-strings. */
10491 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10492
10493 if (it.vpos == this_line_vpos
10494 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10495 row->enabled_p))
10496 {
10497 xassert (this_line_vpos == it.vpos);
10498 xassert (this_line_y == it.current_y);
10499 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10500 #if GLYPH_DEBUG
10501 *w->desired_matrix->method = 0;
10502 debug_method_add (w, "optimization 3");
10503 #endif
10504 goto update;
10505 }
10506 else
10507 goto cancel;
10508 }
10509
10510 cancel:
10511 /* Text changed drastically or point moved off of line. */
10512 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10513 }
10514
10515 CHARPOS (this_line_start_pos) = 0;
10516 consider_all_windows_p |= buffer_shared > 1;
10517 ++clear_face_cache_count;
10518 #ifdef HAVE_WINDOW_SYSTEM
10519 ++clear_image_cache_count;
10520 #endif
10521
10522 /* Build desired matrices, and update the display. If
10523 consider_all_windows_p is non-zero, do it for all windows on all
10524 frames. Otherwise do it for selected_window, only. */
10525
10526 if (consider_all_windows_p)
10527 {
10528 Lisp_Object tail, frame;
10529 int i, n = 0, size = 50;
10530 struct frame **updated
10531 = (struct frame **) alloca (size * sizeof *updated);
10532
10533 /* Recompute # windows showing selected buffer. This will be
10534 incremented each time such a window is displayed. */
10535 buffer_shared = 0;
10536
10537 FOR_EACH_FRAME (tail, frame)
10538 {
10539 struct frame *f = XFRAME (frame);
10540
10541 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10542 {
10543 if (! EQ (frame, selected_frame))
10544 /* Select the frame, for the sake of frame-local
10545 variables. */
10546 select_frame_for_redisplay (frame);
10547
10548 /* Mark all the scroll bars to be removed; we'll redeem
10549 the ones we want when we redisplay their windows. */
10550 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10551 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10552
10553 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10554 redisplay_windows (FRAME_ROOT_WINDOW (f));
10555
10556 /* Any scroll bars which redisplay_windows should have
10557 nuked should now go away. */
10558 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10559 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10560
10561 /* If fonts changed, display again. */
10562 /* ??? rms: I suspect it is a mistake to jump all the way
10563 back to retry here. It should just retry this frame. */
10564 if (fonts_changed_p)
10565 goto retry;
10566
10567 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10568 {
10569 /* See if we have to hscroll. */
10570 if (hscroll_windows (f->root_window))
10571 goto retry;
10572
10573 /* Prevent various kinds of signals during display
10574 update. stdio is not robust about handling
10575 signals, which can cause an apparent I/O
10576 error. */
10577 if (interrupt_input)
10578 unrequest_sigio ();
10579 STOP_POLLING;
10580
10581 /* Update the display. */
10582 set_window_update_flags (XWINDOW (f->root_window), 1);
10583 pause |= update_frame (f, 0, 0);
10584 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10585 if (pause)
10586 break;
10587 #endif
10588
10589 if (n == size)
10590 {
10591 int nbytes = size * sizeof *updated;
10592 struct frame **p = (struct frame **) alloca (2 * nbytes);
10593 bcopy (updated, p, nbytes);
10594 size *= 2;
10595 }
10596
10597 updated[n++] = f;
10598 }
10599 }
10600 }
10601
10602 if (!pause)
10603 {
10604 /* Do the mark_window_display_accurate after all windows have
10605 been redisplayed because this call resets flags in buffers
10606 which are needed for proper redisplay. */
10607 for (i = 0; i < n; ++i)
10608 {
10609 struct frame *f = updated[i];
10610 mark_window_display_accurate (f->root_window, 1);
10611 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10612 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10613 }
10614 }
10615 }
10616 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10617 {
10618 Lisp_Object mini_window;
10619 struct frame *mini_frame;
10620
10621 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10622 /* Use list_of_error, not Qerror, so that
10623 we catch only errors and don't run the debugger. */
10624 internal_condition_case_1 (redisplay_window_1, selected_window,
10625 list_of_error,
10626 redisplay_window_error);
10627
10628 /* Compare desired and current matrices, perform output. */
10629
10630 update:
10631 /* If fonts changed, display again. */
10632 if (fonts_changed_p)
10633 goto retry;
10634
10635 /* Prevent various kinds of signals during display update.
10636 stdio is not robust about handling signals,
10637 which can cause an apparent I/O error. */
10638 if (interrupt_input)
10639 unrequest_sigio ();
10640 STOP_POLLING;
10641
10642 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10643 {
10644 if (hscroll_windows (selected_window))
10645 goto retry;
10646
10647 XWINDOW (selected_window)->must_be_updated_p = 1;
10648 pause = update_frame (sf, 0, 0);
10649 }
10650
10651 /* We may have called echo_area_display at the top of this
10652 function. If the echo area is on another frame, that may
10653 have put text on a frame other than the selected one, so the
10654 above call to update_frame would not have caught it. Catch
10655 it here. */
10656 mini_window = FRAME_MINIBUF_WINDOW (sf);
10657 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10658
10659 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10660 {
10661 XWINDOW (mini_window)->must_be_updated_p = 1;
10662 pause |= update_frame (mini_frame, 0, 0);
10663 if (!pause && hscroll_windows (mini_window))
10664 goto retry;
10665 }
10666 }
10667
10668 /* If display was paused because of pending input, make sure we do a
10669 thorough update the next time. */
10670 if (pause)
10671 {
10672 /* Prevent the optimization at the beginning of
10673 redisplay_internal that tries a single-line update of the
10674 line containing the cursor in the selected window. */
10675 CHARPOS (this_line_start_pos) = 0;
10676
10677 /* Let the overlay arrow be updated the next time. */
10678 update_overlay_arrows (0);
10679
10680 /* If we pause after scrolling, some rows in the current
10681 matrices of some windows are not valid. */
10682 if (!WINDOW_FULL_WIDTH_P (w)
10683 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10684 update_mode_lines = 1;
10685 }
10686 else
10687 {
10688 if (!consider_all_windows_p)
10689 {
10690 /* This has already been done above if
10691 consider_all_windows_p is set. */
10692 mark_window_display_accurate_1 (w, 1);
10693
10694 /* Say overlay arrows are up to date. */
10695 update_overlay_arrows (1);
10696
10697 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10698 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10699 }
10700
10701 update_mode_lines = 0;
10702 windows_or_buffers_changed = 0;
10703 cursor_type_changed = 0;
10704 }
10705
10706 /* Start SIGIO interrupts coming again. Having them off during the
10707 code above makes it less likely one will discard output, but not
10708 impossible, since there might be stuff in the system buffer here.
10709 But it is much hairier to try to do anything about that. */
10710 if (interrupt_input)
10711 request_sigio ();
10712 RESUME_POLLING;
10713
10714 /* If a frame has become visible which was not before, redisplay
10715 again, so that we display it. Expose events for such a frame
10716 (which it gets when becoming visible) don't call the parts of
10717 redisplay constructing glyphs, so simply exposing a frame won't
10718 display anything in this case. So, we have to display these
10719 frames here explicitly. */
10720 if (!pause)
10721 {
10722 Lisp_Object tail, frame;
10723 int new_count = 0;
10724
10725 FOR_EACH_FRAME (tail, frame)
10726 {
10727 int this_is_visible = 0;
10728
10729 if (XFRAME (frame)->visible)
10730 this_is_visible = 1;
10731 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10732 if (XFRAME (frame)->visible)
10733 this_is_visible = 1;
10734
10735 if (this_is_visible)
10736 new_count++;
10737 }
10738
10739 if (new_count != number_of_visible_frames)
10740 windows_or_buffers_changed++;
10741 }
10742
10743 /* Change frame size now if a change is pending. */
10744 do_pending_window_change (1);
10745
10746 /* If we just did a pending size change, or have additional
10747 visible frames, redisplay again. */
10748 if (windows_or_buffers_changed && !pause)
10749 goto retry;
10750
10751 /* Clear the face cache eventually. */
10752 if (consider_all_windows_p)
10753 {
10754 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10755 {
10756 clear_face_cache (0);
10757 clear_face_cache_count = 0;
10758 }
10759 #ifdef HAVE_WINDOW_SYSTEM
10760 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10761 {
10762 Lisp_Object tail, frame;
10763 FOR_EACH_FRAME (tail, frame)
10764 {
10765 struct frame *f = XFRAME (frame);
10766 if (FRAME_WINDOW_P (f))
10767 clear_image_cache (f, 0);
10768 }
10769 clear_image_cache_count = 0;
10770 }
10771 #endif /* HAVE_WINDOW_SYSTEM */
10772 }
10773
10774 end_of_redisplay:
10775 unbind_to (count, Qnil);
10776 RESUME_POLLING;
10777 }
10778
10779
10780 /* Redisplay, but leave alone any recent echo area message unless
10781 another message has been requested in its place.
10782
10783 This is useful in situations where you need to redisplay but no
10784 user action has occurred, making it inappropriate for the message
10785 area to be cleared. See tracking_off and
10786 wait_reading_process_output for examples of these situations.
10787
10788 FROM_WHERE is an integer saying from where this function was
10789 called. This is useful for debugging. */
10790
10791 void
10792 redisplay_preserve_echo_area (from_where)
10793 int from_where;
10794 {
10795 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10796
10797 if (!NILP (echo_area_buffer[1]))
10798 {
10799 /* We have a previously displayed message, but no current
10800 message. Redisplay the previous message. */
10801 display_last_displayed_message_p = 1;
10802 redisplay_internal (1);
10803 display_last_displayed_message_p = 0;
10804 }
10805 else
10806 redisplay_internal (1);
10807
10808 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
10809 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10810 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
10811 }
10812
10813
10814 /* Function registered with record_unwind_protect in
10815 redisplay_internal. Reset redisplaying_p to the value it had
10816 before redisplay_internal was called, and clear
10817 prevent_freeing_realized_faces_p. It also selects the previously
10818 selected frame. */
10819
10820 static Lisp_Object
10821 unwind_redisplay (val)
10822 Lisp_Object val;
10823 {
10824 Lisp_Object old_redisplaying_p, old_frame;
10825
10826 old_redisplaying_p = XCAR (val);
10827 redisplaying_p = XFASTINT (old_redisplaying_p);
10828 old_frame = XCDR (val);
10829 if (! EQ (old_frame, selected_frame))
10830 select_frame_for_redisplay (old_frame);
10831 return Qnil;
10832 }
10833
10834
10835 /* Mark the display of window W as accurate or inaccurate. If
10836 ACCURATE_P is non-zero mark display of W as accurate. If
10837 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10838 redisplay_internal is called. */
10839
10840 static void
10841 mark_window_display_accurate_1 (w, accurate_p)
10842 struct window *w;
10843 int accurate_p;
10844 {
10845 if (BUFFERP (w->buffer))
10846 {
10847 struct buffer *b = XBUFFER (w->buffer);
10848
10849 w->last_modified
10850 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10851 w->last_overlay_modified
10852 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10853 w->last_had_star
10854 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10855
10856 if (accurate_p)
10857 {
10858 b->clip_changed = 0;
10859 b->prevent_redisplay_optimizations_p = 0;
10860
10861 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10862 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10863 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10864 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10865
10866 w->current_matrix->buffer = b;
10867 w->current_matrix->begv = BUF_BEGV (b);
10868 w->current_matrix->zv = BUF_ZV (b);
10869
10870 w->last_cursor = w->cursor;
10871 w->last_cursor_off_p = w->cursor_off_p;
10872
10873 if (w == XWINDOW (selected_window))
10874 w->last_point = make_number (BUF_PT (b));
10875 else
10876 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10877 }
10878 }
10879
10880 if (accurate_p)
10881 {
10882 w->window_end_valid = w->buffer;
10883 #if 0 /* This is incorrect with variable-height lines. */
10884 xassert (XINT (w->window_end_vpos)
10885 < (WINDOW_TOTAL_LINES (w)
10886 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10887 #endif
10888 w->update_mode_line = Qnil;
10889 }
10890 }
10891
10892
10893 /* Mark the display of windows in the window tree rooted at WINDOW as
10894 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10895 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10896 be redisplayed the next time redisplay_internal is called. */
10897
10898 void
10899 mark_window_display_accurate (window, accurate_p)
10900 Lisp_Object window;
10901 int accurate_p;
10902 {
10903 struct window *w;
10904
10905 for (; !NILP (window); window = w->next)
10906 {
10907 w = XWINDOW (window);
10908 mark_window_display_accurate_1 (w, accurate_p);
10909
10910 if (!NILP (w->vchild))
10911 mark_window_display_accurate (w->vchild, accurate_p);
10912 if (!NILP (w->hchild))
10913 mark_window_display_accurate (w->hchild, accurate_p);
10914 }
10915
10916 if (accurate_p)
10917 {
10918 update_overlay_arrows (1);
10919 }
10920 else
10921 {
10922 /* Force a thorough redisplay the next time by setting
10923 last_arrow_position and last_arrow_string to t, which is
10924 unequal to any useful value of Voverlay_arrow_... */
10925 update_overlay_arrows (-1);
10926 }
10927 }
10928
10929
10930 /* Return value in display table DP (Lisp_Char_Table *) for character
10931 C. Since a display table doesn't have any parent, we don't have to
10932 follow parent. Do not call this function directly but use the
10933 macro DISP_CHAR_VECTOR. */
10934
10935 Lisp_Object
10936 disp_char_vector (dp, c)
10937 struct Lisp_Char_Table *dp;
10938 int c;
10939 {
10940 int code[4], i;
10941 Lisp_Object val;
10942
10943 if (SINGLE_BYTE_CHAR_P (c))
10944 return (dp->contents[c]);
10945
10946 SPLIT_CHAR (c, code[0], code[1], code[2]);
10947 if (code[1] < 32)
10948 code[1] = -1;
10949 else if (code[2] < 32)
10950 code[2] = -1;
10951
10952 /* Here, the possible range of code[0] (== charset ID) is
10953 128..max_charset. Since the top level char table contains data
10954 for multibyte characters after 256th element, we must increment
10955 code[0] by 128 to get a correct index. */
10956 code[0] += 128;
10957 code[3] = -1; /* anchor */
10958
10959 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10960 {
10961 val = dp->contents[code[i]];
10962 if (!SUB_CHAR_TABLE_P (val))
10963 return (NILP (val) ? dp->defalt : val);
10964 }
10965
10966 /* Here, val is a sub char table. We return the default value of
10967 it. */
10968 return (dp->defalt);
10969 }
10970
10971
10972 \f
10973 /***********************************************************************
10974 Window Redisplay
10975 ***********************************************************************/
10976
10977 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10978
10979 static void
10980 redisplay_windows (window)
10981 Lisp_Object window;
10982 {
10983 while (!NILP (window))
10984 {
10985 struct window *w = XWINDOW (window);
10986
10987 if (!NILP (w->hchild))
10988 redisplay_windows (w->hchild);
10989 else if (!NILP (w->vchild))
10990 redisplay_windows (w->vchild);
10991 else
10992 {
10993 displayed_buffer = XBUFFER (w->buffer);
10994 /* Use list_of_error, not Qerror, so that
10995 we catch only errors and don't run the debugger. */
10996 internal_condition_case_1 (redisplay_window_0, window,
10997 list_of_error,
10998 redisplay_window_error);
10999 }
11000
11001 window = w->next;
11002 }
11003 }
11004
11005 static Lisp_Object
11006 redisplay_window_error ()
11007 {
11008 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11009 return Qnil;
11010 }
11011
11012 static Lisp_Object
11013 redisplay_window_0 (window)
11014 Lisp_Object window;
11015 {
11016 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11017 redisplay_window (window, 0);
11018 return Qnil;
11019 }
11020
11021 static Lisp_Object
11022 redisplay_window_1 (window)
11023 Lisp_Object window;
11024 {
11025 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11026 redisplay_window (window, 1);
11027 return Qnil;
11028 }
11029 \f
11030
11031 /* Increment GLYPH until it reaches END or CONDITION fails while
11032 adding (GLYPH)->pixel_width to X. */
11033
11034 #define SKIP_GLYPHS(glyph, end, x, condition) \
11035 do \
11036 { \
11037 (x) += (glyph)->pixel_width; \
11038 ++(glyph); \
11039 } \
11040 while ((glyph) < (end) && (condition))
11041
11042
11043 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11044 DELTA is the number of bytes by which positions recorded in ROW
11045 differ from current buffer positions. */
11046
11047 void
11048 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11049 struct window *w;
11050 struct glyph_row *row;
11051 struct glyph_matrix *matrix;
11052 int delta, delta_bytes, dy, dvpos;
11053 {
11054 struct glyph *glyph = row->glyphs[TEXT_AREA];
11055 struct glyph *end = glyph + row->used[TEXT_AREA];
11056 struct glyph *cursor = NULL;
11057 /* The first glyph that starts a sequence of glyphs from string. */
11058 struct glyph *string_start;
11059 /* The X coordinate of string_start. */
11060 int string_start_x;
11061 /* The last known character position. */
11062 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11063 /* The last known character position before string_start. */
11064 int string_before_pos;
11065 int x = row->x;
11066 int cursor_x = x;
11067 int cursor_from_overlay_pos = 0;
11068 int pt_old = PT - delta;
11069
11070 /* Skip over glyphs not having an object at the start of the row.
11071 These are special glyphs like truncation marks on terminal
11072 frames. */
11073 if (row->displays_text_p)
11074 while (glyph < end
11075 && INTEGERP (glyph->object)
11076 && glyph->charpos < 0)
11077 {
11078 x += glyph->pixel_width;
11079 ++glyph;
11080 }
11081
11082 string_start = NULL;
11083 while (glyph < end
11084 && !INTEGERP (glyph->object)
11085 && (!BUFFERP (glyph->object)
11086 || (last_pos = glyph->charpos) < pt_old))
11087 {
11088 if (! STRINGP (glyph->object))
11089 {
11090 string_start = NULL;
11091 x += glyph->pixel_width;
11092 ++glyph;
11093 if (cursor_from_overlay_pos
11094 && last_pos > cursor_from_overlay_pos)
11095 {
11096 cursor_from_overlay_pos = 0;
11097 cursor = 0;
11098 }
11099 }
11100 else
11101 {
11102 string_before_pos = last_pos;
11103 string_start = glyph;
11104 string_start_x = x;
11105 /* Skip all glyphs from string. */
11106 do
11107 {
11108 int pos;
11109 if ((cursor == NULL || glyph > cursor)
11110 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11111 Qcursor, (glyph)->object))
11112 && (pos = string_buffer_position (w, glyph->object,
11113 string_before_pos),
11114 (pos == 0 /* From overlay */
11115 || pos == pt_old)))
11116 {
11117 /* Estimate overlay buffer position from the buffer
11118 positions of the glyphs before and after the overlay.
11119 Add 1 to last_pos so that if point corresponds to the
11120 glyph right after the overlay, we still use a 'cursor'
11121 property found in that overlay. */
11122 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11123 cursor = glyph;
11124 cursor_x = x;
11125 }
11126 x += glyph->pixel_width;
11127 ++glyph;
11128 }
11129 while (glyph < end && STRINGP (glyph->object));
11130 }
11131 }
11132
11133 if (cursor != NULL)
11134 {
11135 glyph = cursor;
11136 x = cursor_x;
11137 }
11138 else if (row->ends_in_ellipsis_p && glyph == end)
11139 {
11140 /* Scan back over the ellipsis glyphs, decrementing positions. */
11141 while (glyph > row->glyphs[TEXT_AREA]
11142 && (glyph - 1)->charpos == last_pos)
11143 glyph--, x -= glyph->pixel_width;
11144 /* That loop always goes one position too far,
11145 including the glyph before the ellipsis.
11146 So scan forward over that one. */
11147 x += glyph->pixel_width;
11148 glyph++;
11149 }
11150 else if (string_start
11151 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11152 {
11153 /* We may have skipped over point because the previous glyphs
11154 are from string. As there's no easy way to know the
11155 character position of the current glyph, find the correct
11156 glyph on point by scanning from string_start again. */
11157 Lisp_Object limit;
11158 Lisp_Object string;
11159 int pos;
11160
11161 limit = make_number (pt_old + 1);
11162 end = glyph;
11163 glyph = string_start;
11164 x = string_start_x;
11165 string = glyph->object;
11166 pos = string_buffer_position (w, string, string_before_pos);
11167 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11168 because we always put cursor after overlay strings. */
11169 while (pos == 0 && glyph < end)
11170 {
11171 string = glyph->object;
11172 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11173 if (glyph < end)
11174 pos = string_buffer_position (w, glyph->object, string_before_pos);
11175 }
11176
11177 while (glyph < end)
11178 {
11179 pos = XINT (Fnext_single_char_property_change
11180 (make_number (pos), Qdisplay, Qnil, limit));
11181 if (pos > pt_old)
11182 break;
11183 /* Skip glyphs from the same string. */
11184 string = glyph->object;
11185 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11186 /* Skip glyphs from an overlay. */
11187 while (glyph < end
11188 && ! string_buffer_position (w, glyph->object, pos))
11189 {
11190 string = glyph->object;
11191 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11192 }
11193 }
11194 }
11195
11196 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11197 w->cursor.x = x;
11198 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11199 w->cursor.y = row->y + dy;
11200
11201 if (w == XWINDOW (selected_window))
11202 {
11203 if (!row->continued_p
11204 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11205 && row->x == 0)
11206 {
11207 this_line_buffer = XBUFFER (w->buffer);
11208
11209 CHARPOS (this_line_start_pos)
11210 = MATRIX_ROW_START_CHARPOS (row) + delta;
11211 BYTEPOS (this_line_start_pos)
11212 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11213
11214 CHARPOS (this_line_end_pos)
11215 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11216 BYTEPOS (this_line_end_pos)
11217 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11218
11219 this_line_y = w->cursor.y;
11220 this_line_pixel_height = row->height;
11221 this_line_vpos = w->cursor.vpos;
11222 this_line_start_x = row->x;
11223 }
11224 else
11225 CHARPOS (this_line_start_pos) = 0;
11226 }
11227 }
11228
11229
11230 /* Run window scroll functions, if any, for WINDOW with new window
11231 start STARTP. Sets the window start of WINDOW to that position.
11232
11233 We assume that the window's buffer is really current. */
11234
11235 static INLINE struct text_pos
11236 run_window_scroll_functions (window, startp)
11237 Lisp_Object window;
11238 struct text_pos startp;
11239 {
11240 struct window *w = XWINDOW (window);
11241 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11242
11243 if (current_buffer != XBUFFER (w->buffer))
11244 abort ();
11245
11246 if (!NILP (Vwindow_scroll_functions))
11247 {
11248 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11249 make_number (CHARPOS (startp)));
11250 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11251 /* In case the hook functions switch buffers. */
11252 if (current_buffer != XBUFFER (w->buffer))
11253 set_buffer_internal_1 (XBUFFER (w->buffer));
11254 }
11255
11256 return startp;
11257 }
11258
11259
11260 /* Make sure the line containing the cursor is fully visible.
11261 A value of 1 means there is nothing to be done.
11262 (Either the line is fully visible, or it cannot be made so,
11263 or we cannot tell.)
11264
11265 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11266 is higher than window.
11267
11268 A value of 0 means the caller should do scrolling
11269 as if point had gone off the screen. */
11270
11271 static int
11272 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11273 struct window *w;
11274 int force_p;
11275 {
11276 struct glyph_matrix *matrix;
11277 struct glyph_row *row;
11278 int window_height;
11279
11280 if (!make_cursor_line_fully_visible_p)
11281 return 1;
11282
11283 /* It's not always possible to find the cursor, e.g, when a window
11284 is full of overlay strings. Don't do anything in that case. */
11285 if (w->cursor.vpos < 0)
11286 return 1;
11287
11288 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11289 row = MATRIX_ROW (matrix, w->cursor.vpos);
11290
11291 /* If the cursor row is not partially visible, there's nothing to do. */
11292 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11293 return 1;
11294
11295 /* If the row the cursor is in is taller than the window's height,
11296 it's not clear what to do, so do nothing. */
11297 window_height = window_box_height (w);
11298 if (row->height >= window_height)
11299 {
11300 if (!force_p || w->vscroll)
11301 return 1;
11302 }
11303 return 0;
11304
11305 #if 0
11306 /* This code used to try to scroll the window just enough to make
11307 the line visible. It returned 0 to say that the caller should
11308 allocate larger glyph matrices. */
11309
11310 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11311 {
11312 int dy = row->height - row->visible_height;
11313 w->vscroll = 0;
11314 w->cursor.y += dy;
11315 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11316 }
11317 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11318 {
11319 int dy = - (row->height - row->visible_height);
11320 w->vscroll = dy;
11321 w->cursor.y += dy;
11322 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11323 }
11324
11325 /* When we change the cursor y-position of the selected window,
11326 change this_line_y as well so that the display optimization for
11327 the cursor line of the selected window in redisplay_internal uses
11328 the correct y-position. */
11329 if (w == XWINDOW (selected_window))
11330 this_line_y = w->cursor.y;
11331
11332 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11333 redisplay with larger matrices. */
11334 if (matrix->nrows < required_matrix_height (w))
11335 {
11336 fonts_changed_p = 1;
11337 return 0;
11338 }
11339
11340 return 1;
11341 #endif /* 0 */
11342 }
11343
11344
11345 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11346 non-zero means only WINDOW is redisplayed in redisplay_internal.
11347 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11348 in redisplay_window to bring a partially visible line into view in
11349 the case that only the cursor has moved.
11350
11351 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11352 last screen line's vertical height extends past the end of the screen.
11353
11354 Value is
11355
11356 1 if scrolling succeeded
11357
11358 0 if scrolling didn't find point.
11359
11360 -1 if new fonts have been loaded so that we must interrupt
11361 redisplay, adjust glyph matrices, and try again. */
11362
11363 enum
11364 {
11365 SCROLLING_SUCCESS,
11366 SCROLLING_FAILED,
11367 SCROLLING_NEED_LARGER_MATRICES
11368 };
11369
11370 static int
11371 try_scrolling (window, just_this_one_p, scroll_conservatively,
11372 scroll_step, temp_scroll_step, last_line_misfit)
11373 Lisp_Object window;
11374 int just_this_one_p;
11375 EMACS_INT scroll_conservatively, scroll_step;
11376 int temp_scroll_step;
11377 int last_line_misfit;
11378 {
11379 struct window *w = XWINDOW (window);
11380 struct frame *f = XFRAME (w->frame);
11381 struct text_pos scroll_margin_pos;
11382 struct text_pos pos;
11383 struct text_pos startp;
11384 struct it it;
11385 Lisp_Object window_end;
11386 int this_scroll_margin;
11387 int dy = 0;
11388 int scroll_max;
11389 int rc;
11390 int amount_to_scroll = 0;
11391 Lisp_Object aggressive;
11392 int height;
11393 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11394
11395 #if GLYPH_DEBUG
11396 debug_method_add (w, "try_scrolling");
11397 #endif
11398
11399 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11400
11401 /* Compute scroll margin height in pixels. We scroll when point is
11402 within this distance from the top or bottom of the window. */
11403 if (scroll_margin > 0)
11404 {
11405 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11406 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11407 }
11408 else
11409 this_scroll_margin = 0;
11410
11411 /* Force scroll_conservatively to have a reasonable value so it doesn't
11412 cause an overflow while computing how much to scroll. */
11413 if (scroll_conservatively)
11414 scroll_conservatively = min (scroll_conservatively,
11415 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11416
11417 /* Compute how much we should try to scroll maximally to bring point
11418 into view. */
11419 if (scroll_step || scroll_conservatively || temp_scroll_step)
11420 scroll_max = max (scroll_step,
11421 max (scroll_conservatively, temp_scroll_step));
11422 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11423 || NUMBERP (current_buffer->scroll_up_aggressively))
11424 /* We're trying to scroll because of aggressive scrolling
11425 but no scroll_step is set. Choose an arbitrary one. Maybe
11426 there should be a variable for this. */
11427 scroll_max = 10;
11428 else
11429 scroll_max = 0;
11430 scroll_max *= FRAME_LINE_HEIGHT (f);
11431
11432 /* Decide whether we have to scroll down. Start at the window end
11433 and move this_scroll_margin up to find the position of the scroll
11434 margin. */
11435 window_end = Fwindow_end (window, Qt);
11436
11437 too_near_end:
11438
11439 CHARPOS (scroll_margin_pos) = XINT (window_end);
11440 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11441
11442 if (this_scroll_margin || extra_scroll_margin_lines)
11443 {
11444 start_display (&it, w, scroll_margin_pos);
11445 if (this_scroll_margin)
11446 move_it_vertically_backward (&it, this_scroll_margin);
11447 if (extra_scroll_margin_lines)
11448 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11449 scroll_margin_pos = it.current.pos;
11450 }
11451
11452 if (PT >= CHARPOS (scroll_margin_pos))
11453 {
11454 int y0;
11455
11456 /* Point is in the scroll margin at the bottom of the window, or
11457 below. Compute a new window start that makes point visible. */
11458
11459 /* Compute the distance from the scroll margin to PT.
11460 Give up if the distance is greater than scroll_max. */
11461 start_display (&it, w, scroll_margin_pos);
11462 y0 = it.current_y;
11463 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11464 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11465
11466 /* To make point visible, we have to move the window start
11467 down so that the line the cursor is in is visible, which
11468 means we have to add in the height of the cursor line. */
11469 dy = line_bottom_y (&it) - y0;
11470
11471 if (dy > scroll_max)
11472 return SCROLLING_FAILED;
11473
11474 /* Move the window start down. If scrolling conservatively,
11475 move it just enough down to make point visible. If
11476 scroll_step is set, move it down by scroll_step. */
11477 start_display (&it, w, startp);
11478
11479 if (scroll_conservatively)
11480 /* Set AMOUNT_TO_SCROLL to at least one line,
11481 and at most scroll_conservatively lines. */
11482 amount_to_scroll
11483 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11484 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11485 else if (scroll_step || temp_scroll_step)
11486 amount_to_scroll = scroll_max;
11487 else
11488 {
11489 aggressive = current_buffer->scroll_up_aggressively;
11490 height = WINDOW_BOX_TEXT_HEIGHT (w);
11491 if (NUMBERP (aggressive))
11492 {
11493 double float_amount = XFLOATINT (aggressive) * height;
11494 amount_to_scroll = float_amount;
11495 if (amount_to_scroll == 0 && float_amount > 0)
11496 amount_to_scroll = 1;
11497 }
11498 }
11499
11500 if (amount_to_scroll <= 0)
11501 return SCROLLING_FAILED;
11502
11503 /* If moving by amount_to_scroll leaves STARTP unchanged,
11504 move it down one screen line. */
11505
11506 move_it_vertically (&it, amount_to_scroll);
11507 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11508 move_it_by_lines (&it, 1, 1);
11509 startp = it.current.pos;
11510 }
11511 else
11512 {
11513 /* See if point is inside the scroll margin at the top of the
11514 window. */
11515 scroll_margin_pos = startp;
11516 if (this_scroll_margin)
11517 {
11518 start_display (&it, w, startp);
11519 move_it_vertically (&it, this_scroll_margin);
11520 scroll_margin_pos = it.current.pos;
11521 }
11522
11523 if (PT < CHARPOS (scroll_margin_pos))
11524 {
11525 /* Point is in the scroll margin at the top of the window or
11526 above what is displayed in the window. */
11527 int y0;
11528
11529 /* Compute the vertical distance from PT to the scroll
11530 margin position. Give up if distance is greater than
11531 scroll_max. */
11532 SET_TEXT_POS (pos, PT, PT_BYTE);
11533 start_display (&it, w, pos);
11534 y0 = it.current_y;
11535 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11536 it.last_visible_y, -1,
11537 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11538 dy = it.current_y - y0;
11539 if (dy > scroll_max)
11540 return SCROLLING_FAILED;
11541
11542 /* Compute new window start. */
11543 start_display (&it, w, startp);
11544
11545 if (scroll_conservatively)
11546 amount_to_scroll
11547 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11548 else if (scroll_step || temp_scroll_step)
11549 amount_to_scroll = scroll_max;
11550 else
11551 {
11552 aggressive = current_buffer->scroll_down_aggressively;
11553 height = WINDOW_BOX_TEXT_HEIGHT (w);
11554 if (NUMBERP (aggressive))
11555 {
11556 double float_amount = XFLOATINT (aggressive) * height;
11557 amount_to_scroll = float_amount;
11558 if (amount_to_scroll == 0 && float_amount > 0)
11559 amount_to_scroll = 1;
11560 }
11561 }
11562
11563 if (amount_to_scroll <= 0)
11564 return SCROLLING_FAILED;
11565
11566 move_it_vertically_backward (&it, amount_to_scroll);
11567 startp = it.current.pos;
11568 }
11569 }
11570
11571 /* Run window scroll functions. */
11572 startp = run_window_scroll_functions (window, startp);
11573
11574 /* Display the window. Give up if new fonts are loaded, or if point
11575 doesn't appear. */
11576 if (!try_window (window, startp, 0))
11577 rc = SCROLLING_NEED_LARGER_MATRICES;
11578 else if (w->cursor.vpos < 0)
11579 {
11580 clear_glyph_matrix (w->desired_matrix);
11581 rc = SCROLLING_FAILED;
11582 }
11583 else
11584 {
11585 /* Maybe forget recorded base line for line number display. */
11586 if (!just_this_one_p
11587 || current_buffer->clip_changed
11588 || BEG_UNCHANGED < CHARPOS (startp))
11589 w->base_line_number = Qnil;
11590
11591 /* If cursor ends up on a partially visible line,
11592 treat that as being off the bottom of the screen. */
11593 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11594 {
11595 clear_glyph_matrix (w->desired_matrix);
11596 ++extra_scroll_margin_lines;
11597 goto too_near_end;
11598 }
11599 rc = SCROLLING_SUCCESS;
11600 }
11601
11602 return rc;
11603 }
11604
11605
11606 /* Compute a suitable window start for window W if display of W starts
11607 on a continuation line. Value is non-zero if a new window start
11608 was computed.
11609
11610 The new window start will be computed, based on W's width, starting
11611 from the start of the continued line. It is the start of the
11612 screen line with the minimum distance from the old start W->start. */
11613
11614 static int
11615 compute_window_start_on_continuation_line (w)
11616 struct window *w;
11617 {
11618 struct text_pos pos, start_pos;
11619 int window_start_changed_p = 0;
11620
11621 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11622
11623 /* If window start is on a continuation line... Window start may be
11624 < BEGV in case there's invisible text at the start of the
11625 buffer (M-x rmail, for example). */
11626 if (CHARPOS (start_pos) > BEGV
11627 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11628 {
11629 struct it it;
11630 struct glyph_row *row;
11631
11632 /* Handle the case that the window start is out of range. */
11633 if (CHARPOS (start_pos) < BEGV)
11634 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11635 else if (CHARPOS (start_pos) > ZV)
11636 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11637
11638 /* Find the start of the continued line. This should be fast
11639 because scan_buffer is fast (newline cache). */
11640 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11641 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11642 row, DEFAULT_FACE_ID);
11643 reseat_at_previous_visible_line_start (&it);
11644
11645 /* If the line start is "too far" away from the window start,
11646 say it takes too much time to compute a new window start. */
11647 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11648 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11649 {
11650 int min_distance, distance;
11651
11652 /* Move forward by display lines to find the new window
11653 start. If window width was enlarged, the new start can
11654 be expected to be > the old start. If window width was
11655 decreased, the new window start will be < the old start.
11656 So, we're looking for the display line start with the
11657 minimum distance from the old window start. */
11658 pos = it.current.pos;
11659 min_distance = INFINITY;
11660 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11661 distance < min_distance)
11662 {
11663 min_distance = distance;
11664 pos = it.current.pos;
11665 move_it_by_lines (&it, 1, 0);
11666 }
11667
11668 /* Set the window start there. */
11669 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11670 window_start_changed_p = 1;
11671 }
11672 }
11673
11674 return window_start_changed_p;
11675 }
11676
11677
11678 /* Try cursor movement in case text has not changed in window WINDOW,
11679 with window start STARTP. Value is
11680
11681 CURSOR_MOVEMENT_SUCCESS if successful
11682
11683 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11684
11685 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11686 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11687 we want to scroll as if scroll-step were set to 1. See the code.
11688
11689 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11690 which case we have to abort this redisplay, and adjust matrices
11691 first. */
11692
11693 enum
11694 {
11695 CURSOR_MOVEMENT_SUCCESS,
11696 CURSOR_MOVEMENT_CANNOT_BE_USED,
11697 CURSOR_MOVEMENT_MUST_SCROLL,
11698 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11699 };
11700
11701 static int
11702 try_cursor_movement (window, startp, scroll_step)
11703 Lisp_Object window;
11704 struct text_pos startp;
11705 int *scroll_step;
11706 {
11707 struct window *w = XWINDOW (window);
11708 struct frame *f = XFRAME (w->frame);
11709 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11710
11711 #if GLYPH_DEBUG
11712 if (inhibit_try_cursor_movement)
11713 return rc;
11714 #endif
11715
11716 /* Handle case where text has not changed, only point, and it has
11717 not moved off the frame. */
11718 if (/* Point may be in this window. */
11719 PT >= CHARPOS (startp)
11720 /* Selective display hasn't changed. */
11721 && !current_buffer->clip_changed
11722 /* Function force-mode-line-update is used to force a thorough
11723 redisplay. It sets either windows_or_buffers_changed or
11724 update_mode_lines. So don't take a shortcut here for these
11725 cases. */
11726 && !update_mode_lines
11727 && !windows_or_buffers_changed
11728 && !cursor_type_changed
11729 /* Can't use this case if highlighting a region. When a
11730 region exists, cursor movement has to do more than just
11731 set the cursor. */
11732 && !(!NILP (Vtransient_mark_mode)
11733 && !NILP (current_buffer->mark_active))
11734 && NILP (w->region_showing)
11735 && NILP (Vshow_trailing_whitespace)
11736 /* Right after splitting windows, last_point may be nil. */
11737 && INTEGERP (w->last_point)
11738 /* This code is not used for mini-buffer for the sake of the case
11739 of redisplaying to replace an echo area message; since in
11740 that case the mini-buffer contents per se are usually
11741 unchanged. This code is of no real use in the mini-buffer
11742 since the handling of this_line_start_pos, etc., in redisplay
11743 handles the same cases. */
11744 && !EQ (window, minibuf_window)
11745 /* When splitting windows or for new windows, it happens that
11746 redisplay is called with a nil window_end_vpos or one being
11747 larger than the window. This should really be fixed in
11748 window.c. I don't have this on my list, now, so we do
11749 approximately the same as the old redisplay code. --gerd. */
11750 && INTEGERP (w->window_end_vpos)
11751 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11752 && (FRAME_WINDOW_P (f)
11753 || !overlay_arrow_in_current_buffer_p ()))
11754 {
11755 int this_scroll_margin, top_scroll_margin;
11756 struct glyph_row *row = NULL;
11757
11758 #if GLYPH_DEBUG
11759 debug_method_add (w, "cursor movement");
11760 #endif
11761
11762 /* Scroll if point within this distance from the top or bottom
11763 of the window. This is a pixel value. */
11764 this_scroll_margin = max (0, scroll_margin);
11765 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11766 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11767
11768 top_scroll_margin = this_scroll_margin;
11769 if (WINDOW_WANTS_HEADER_LINE_P (w))
11770 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11771
11772 /* Start with the row the cursor was displayed during the last
11773 not paused redisplay. Give up if that row is not valid. */
11774 if (w->last_cursor.vpos < 0
11775 || w->last_cursor.vpos >= w->current_matrix->nrows)
11776 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11777 else
11778 {
11779 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11780 if (row->mode_line_p)
11781 ++row;
11782 if (!row->enabled_p)
11783 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11784 }
11785
11786 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11787 {
11788 int scroll_p = 0;
11789 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11790
11791 if (PT > XFASTINT (w->last_point))
11792 {
11793 /* Point has moved forward. */
11794 while (MATRIX_ROW_END_CHARPOS (row) < PT
11795 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11796 {
11797 xassert (row->enabled_p);
11798 ++row;
11799 }
11800
11801 /* The end position of a row equals the start position
11802 of the next row. If PT is there, we would rather
11803 display it in the next line. */
11804 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11805 && MATRIX_ROW_END_CHARPOS (row) == PT
11806 && !cursor_row_p (w, row))
11807 ++row;
11808
11809 /* If within the scroll margin, scroll. Note that
11810 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11811 the next line would be drawn, and that
11812 this_scroll_margin can be zero. */
11813 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11814 || PT > MATRIX_ROW_END_CHARPOS (row)
11815 /* Line is completely visible last line in window
11816 and PT is to be set in the next line. */
11817 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11818 && PT == MATRIX_ROW_END_CHARPOS (row)
11819 && !row->ends_at_zv_p
11820 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11821 scroll_p = 1;
11822 }
11823 else if (PT < XFASTINT (w->last_point))
11824 {
11825 /* Cursor has to be moved backward. Note that PT >=
11826 CHARPOS (startp) because of the outer if-statement. */
11827 while (!row->mode_line_p
11828 && (MATRIX_ROW_START_CHARPOS (row) > PT
11829 || (MATRIX_ROW_START_CHARPOS (row) == PT
11830 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11831 && (row->y > top_scroll_margin
11832 || CHARPOS (startp) == BEGV))
11833 {
11834 xassert (row->enabled_p);
11835 --row;
11836 }
11837
11838 /* Consider the following case: Window starts at BEGV,
11839 there is invisible, intangible text at BEGV, so that
11840 display starts at some point START > BEGV. It can
11841 happen that we are called with PT somewhere between
11842 BEGV and START. Try to handle that case. */
11843 if (row < w->current_matrix->rows
11844 || row->mode_line_p)
11845 {
11846 row = w->current_matrix->rows;
11847 if (row->mode_line_p)
11848 ++row;
11849 }
11850
11851 /* Due to newlines in overlay strings, we may have to
11852 skip forward over overlay strings. */
11853 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11854 && MATRIX_ROW_END_CHARPOS (row) == PT
11855 && !cursor_row_p (w, row))
11856 ++row;
11857
11858 /* If within the scroll margin, scroll. */
11859 if (row->y < top_scroll_margin
11860 && CHARPOS (startp) != BEGV)
11861 scroll_p = 1;
11862 }
11863 else
11864 {
11865 /* Cursor did not move. So don't scroll even if cursor line
11866 is partially visible, as it was so before. */
11867 rc = CURSOR_MOVEMENT_SUCCESS;
11868 }
11869
11870 if (PT < MATRIX_ROW_START_CHARPOS (row)
11871 || PT > MATRIX_ROW_END_CHARPOS (row))
11872 {
11873 /* if PT is not in the glyph row, give up. */
11874 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11875 }
11876 else if (rc != CURSOR_MOVEMENT_SUCCESS
11877 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11878 && make_cursor_line_fully_visible_p)
11879 {
11880 if (PT == MATRIX_ROW_END_CHARPOS (row)
11881 && !row->ends_at_zv_p
11882 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11883 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11884 else if (row->height > window_box_height (w))
11885 {
11886 /* If we end up in a partially visible line, let's
11887 make it fully visible, except when it's taller
11888 than the window, in which case we can't do much
11889 about it. */
11890 *scroll_step = 1;
11891 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11892 }
11893 else
11894 {
11895 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11896 if (!cursor_row_fully_visible_p (w, 0, 1))
11897 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11898 else
11899 rc = CURSOR_MOVEMENT_SUCCESS;
11900 }
11901 }
11902 else if (scroll_p)
11903 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11904 else
11905 {
11906 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11907 rc = CURSOR_MOVEMENT_SUCCESS;
11908 }
11909 }
11910 }
11911
11912 return rc;
11913 }
11914
11915 void
11916 set_vertical_scroll_bar (w)
11917 struct window *w;
11918 {
11919 int start, end, whole;
11920
11921 /* Calculate the start and end positions for the current window.
11922 At some point, it would be nice to choose between scrollbars
11923 which reflect the whole buffer size, with special markers
11924 indicating narrowing, and scrollbars which reflect only the
11925 visible region.
11926
11927 Note that mini-buffers sometimes aren't displaying any text. */
11928 if (!MINI_WINDOW_P (w)
11929 || (w == XWINDOW (minibuf_window)
11930 && NILP (echo_area_buffer[0])))
11931 {
11932 struct buffer *buf = XBUFFER (w->buffer);
11933 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11934 start = marker_position (w->start) - BUF_BEGV (buf);
11935 /* I don't think this is guaranteed to be right. For the
11936 moment, we'll pretend it is. */
11937 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11938
11939 if (end < start)
11940 end = start;
11941 if (whole < (end - start))
11942 whole = end - start;
11943 }
11944 else
11945 start = end = whole = 0;
11946
11947 /* Indicate what this scroll bar ought to be displaying now. */
11948 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11949 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11950 (w, end - start, whole, start);
11951 }
11952
11953
11954 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11955 selected_window is redisplayed.
11956
11957 We can return without actually redisplaying the window if
11958 fonts_changed_p is nonzero. In that case, redisplay_internal will
11959 retry. */
11960
11961 static void
11962 redisplay_window (window, just_this_one_p)
11963 Lisp_Object window;
11964 int just_this_one_p;
11965 {
11966 struct window *w = XWINDOW (window);
11967 struct frame *f = XFRAME (w->frame);
11968 struct buffer *buffer = XBUFFER (w->buffer);
11969 struct buffer *old = current_buffer;
11970 struct text_pos lpoint, opoint, startp;
11971 int update_mode_line;
11972 int tem;
11973 struct it it;
11974 /* Record it now because it's overwritten. */
11975 int current_matrix_up_to_date_p = 0;
11976 int used_current_matrix_p = 0;
11977 /* This is less strict than current_matrix_up_to_date_p.
11978 It indictes that the buffer contents and narrowing are unchanged. */
11979 int buffer_unchanged_p = 0;
11980 int temp_scroll_step = 0;
11981 int count = SPECPDL_INDEX ();
11982 int rc;
11983 int centering_position = -1;
11984 int last_line_misfit = 0;
11985
11986 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11987 opoint = lpoint;
11988
11989 /* W must be a leaf window here. */
11990 xassert (!NILP (w->buffer));
11991 #if GLYPH_DEBUG
11992 *w->desired_matrix->method = 0;
11993 #endif
11994
11995 specbind (Qinhibit_point_motion_hooks, Qt);
11996
11997 reconsider_clip_changes (w, buffer);
11998
11999 /* Has the mode line to be updated? */
12000 update_mode_line = (!NILP (w->update_mode_line)
12001 || update_mode_lines
12002 || buffer->clip_changed
12003 || buffer->prevent_redisplay_optimizations_p);
12004
12005 if (MINI_WINDOW_P (w))
12006 {
12007 if (w == XWINDOW (echo_area_window)
12008 && !NILP (echo_area_buffer[0]))
12009 {
12010 if (update_mode_line)
12011 /* We may have to update a tty frame's menu bar or a
12012 tool-bar. Example `M-x C-h C-h C-g'. */
12013 goto finish_menu_bars;
12014 else
12015 /* We've already displayed the echo area glyphs in this window. */
12016 goto finish_scroll_bars;
12017 }
12018 else if ((w != XWINDOW (minibuf_window)
12019 || minibuf_level == 0)
12020 /* When buffer is nonempty, redisplay window normally. */
12021 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12022 /* Quail displays non-mini buffers in minibuffer window.
12023 In that case, redisplay the window normally. */
12024 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12025 {
12026 /* W is a mini-buffer window, but it's not active, so clear
12027 it. */
12028 int yb = window_text_bottom_y (w);
12029 struct glyph_row *row;
12030 int y;
12031
12032 for (y = 0, row = w->desired_matrix->rows;
12033 y < yb;
12034 y += row->height, ++row)
12035 blank_row (w, row, y);
12036 goto finish_scroll_bars;
12037 }
12038
12039 clear_glyph_matrix (w->desired_matrix);
12040 }
12041
12042 /* Otherwise set up data on this window; select its buffer and point
12043 value. */
12044 /* Really select the buffer, for the sake of buffer-local
12045 variables. */
12046 set_buffer_internal_1 (XBUFFER (w->buffer));
12047 SET_TEXT_POS (opoint, PT, PT_BYTE);
12048
12049 current_matrix_up_to_date_p
12050 = (!NILP (w->window_end_valid)
12051 && !current_buffer->clip_changed
12052 && !current_buffer->prevent_redisplay_optimizations_p
12053 && XFASTINT (w->last_modified) >= MODIFF
12054 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12055
12056 buffer_unchanged_p
12057 = (!NILP (w->window_end_valid)
12058 && !current_buffer->clip_changed
12059 && XFASTINT (w->last_modified) >= MODIFF
12060 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12061
12062 /* When windows_or_buffers_changed is non-zero, we can't rely on
12063 the window end being valid, so set it to nil there. */
12064 if (windows_or_buffers_changed)
12065 {
12066 /* If window starts on a continuation line, maybe adjust the
12067 window start in case the window's width changed. */
12068 if (XMARKER (w->start)->buffer == current_buffer)
12069 compute_window_start_on_continuation_line (w);
12070
12071 w->window_end_valid = Qnil;
12072 }
12073
12074 /* Some sanity checks. */
12075 CHECK_WINDOW_END (w);
12076 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12077 abort ();
12078 if (BYTEPOS (opoint) < CHARPOS (opoint))
12079 abort ();
12080
12081 /* If %c is in mode line, update it if needed. */
12082 if (!NILP (w->column_number_displayed)
12083 /* This alternative quickly identifies a common case
12084 where no change is needed. */
12085 && !(PT == XFASTINT (w->last_point)
12086 && XFASTINT (w->last_modified) >= MODIFF
12087 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12088 && (XFASTINT (w->column_number_displayed)
12089 != (int) current_column ())) /* iftc */
12090 update_mode_line = 1;
12091
12092 /* Count number of windows showing the selected buffer. An indirect
12093 buffer counts as its base buffer. */
12094 if (!just_this_one_p)
12095 {
12096 struct buffer *current_base, *window_base;
12097 current_base = current_buffer;
12098 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12099 if (current_base->base_buffer)
12100 current_base = current_base->base_buffer;
12101 if (window_base->base_buffer)
12102 window_base = window_base->base_buffer;
12103 if (current_base == window_base)
12104 buffer_shared++;
12105 }
12106
12107 /* Point refers normally to the selected window. For any other
12108 window, set up appropriate value. */
12109 if (!EQ (window, selected_window))
12110 {
12111 int new_pt = XMARKER (w->pointm)->charpos;
12112 int new_pt_byte = marker_byte_position (w->pointm);
12113 if (new_pt < BEGV)
12114 {
12115 new_pt = BEGV;
12116 new_pt_byte = BEGV_BYTE;
12117 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12118 }
12119 else if (new_pt > (ZV - 1))
12120 {
12121 new_pt = ZV;
12122 new_pt_byte = ZV_BYTE;
12123 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12124 }
12125
12126 /* We don't use SET_PT so that the point-motion hooks don't run. */
12127 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12128 }
12129
12130 /* If any of the character widths specified in the display table
12131 have changed, invalidate the width run cache. It's true that
12132 this may be a bit late to catch such changes, but the rest of
12133 redisplay goes (non-fatally) haywire when the display table is
12134 changed, so why should we worry about doing any better? */
12135 if (current_buffer->width_run_cache)
12136 {
12137 struct Lisp_Char_Table *disptab = buffer_display_table ();
12138
12139 if (! disptab_matches_widthtab (disptab,
12140 XVECTOR (current_buffer->width_table)))
12141 {
12142 invalidate_region_cache (current_buffer,
12143 current_buffer->width_run_cache,
12144 BEG, Z);
12145 recompute_width_table (current_buffer, disptab);
12146 }
12147 }
12148
12149 /* If window-start is screwed up, choose a new one. */
12150 if (XMARKER (w->start)->buffer != current_buffer)
12151 goto recenter;
12152
12153 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12154
12155 /* If someone specified a new starting point but did not insist,
12156 check whether it can be used. */
12157 if (!NILP (w->optional_new_start)
12158 && CHARPOS (startp) >= BEGV
12159 && CHARPOS (startp) <= ZV)
12160 {
12161 w->optional_new_start = Qnil;
12162 start_display (&it, w, startp);
12163 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12164 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12165 if (IT_CHARPOS (it) == PT)
12166 w->force_start = Qt;
12167 /* IT may overshoot PT if text at PT is invisible. */
12168 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12169 w->force_start = Qt;
12170
12171
12172 }
12173
12174 /* Handle case where place to start displaying has been specified,
12175 unless the specified location is outside the accessible range. */
12176 if (!NILP (w->force_start)
12177 || w->frozen_window_start_p)
12178 {
12179 /* We set this later on if we have to adjust point. */
12180 int new_vpos = -1;
12181 int val;
12182
12183 w->force_start = Qnil;
12184 w->vscroll = 0;
12185 w->window_end_valid = Qnil;
12186
12187 /* Forget any recorded base line for line number display. */
12188 if (!buffer_unchanged_p)
12189 w->base_line_number = Qnil;
12190
12191 /* Redisplay the mode line. Select the buffer properly for that.
12192 Also, run the hook window-scroll-functions
12193 because we have scrolled. */
12194 /* Note, we do this after clearing force_start because
12195 if there's an error, it is better to forget about force_start
12196 than to get into an infinite loop calling the hook functions
12197 and having them get more errors. */
12198 if (!update_mode_line
12199 || ! NILP (Vwindow_scroll_functions))
12200 {
12201 update_mode_line = 1;
12202 w->update_mode_line = Qt;
12203 startp = run_window_scroll_functions (window, startp);
12204 }
12205
12206 w->last_modified = make_number (0);
12207 w->last_overlay_modified = make_number (0);
12208 if (CHARPOS (startp) < BEGV)
12209 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12210 else if (CHARPOS (startp) > ZV)
12211 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12212
12213 /* Redisplay, then check if cursor has been set during the
12214 redisplay. Give up if new fonts were loaded. */
12215 val = try_window (window, startp, 1);
12216 if (!val)
12217 {
12218 w->force_start = Qt;
12219 clear_glyph_matrix (w->desired_matrix);
12220 goto need_larger_matrices;
12221 }
12222 /* Point was outside the scroll margins. */
12223 if (val < 0)
12224 new_vpos = window_box_height (w) / 2;
12225
12226 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12227 {
12228 /* If point does not appear, try to move point so it does
12229 appear. The desired matrix has been built above, so we
12230 can use it here. */
12231 new_vpos = window_box_height (w) / 2;
12232 }
12233
12234 if (!cursor_row_fully_visible_p (w, 0, 0))
12235 {
12236 /* Point does appear, but on a line partly visible at end of window.
12237 Move it back to a fully-visible line. */
12238 new_vpos = window_box_height (w);
12239 }
12240
12241 /* If we need to move point for either of the above reasons,
12242 now actually do it. */
12243 if (new_vpos >= 0)
12244 {
12245 struct glyph_row *row;
12246
12247 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12248 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12249 ++row;
12250
12251 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12252 MATRIX_ROW_START_BYTEPOS (row));
12253
12254 if (w != XWINDOW (selected_window))
12255 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12256 else if (current_buffer == old)
12257 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12258
12259 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12260
12261 /* If we are highlighting the region, then we just changed
12262 the region, so redisplay to show it. */
12263 if (!NILP (Vtransient_mark_mode)
12264 && !NILP (current_buffer->mark_active))
12265 {
12266 clear_glyph_matrix (w->desired_matrix);
12267 if (!try_window (window, startp, 0))
12268 goto need_larger_matrices;
12269 }
12270 }
12271
12272 #if GLYPH_DEBUG
12273 debug_method_add (w, "forced window start");
12274 #endif
12275 goto done;
12276 }
12277
12278 /* Handle case where text has not changed, only point, and it has
12279 not moved off the frame, and we are not retrying after hscroll.
12280 (current_matrix_up_to_date_p is nonzero when retrying.) */
12281 if (current_matrix_up_to_date_p
12282 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12283 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12284 {
12285 switch (rc)
12286 {
12287 case CURSOR_MOVEMENT_SUCCESS:
12288 used_current_matrix_p = 1;
12289 goto done;
12290
12291 #if 0 /* try_cursor_movement never returns this value. */
12292 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12293 goto need_larger_matrices;
12294 #endif
12295
12296 case CURSOR_MOVEMENT_MUST_SCROLL:
12297 goto try_to_scroll;
12298
12299 default:
12300 abort ();
12301 }
12302 }
12303 /* If current starting point was originally the beginning of a line
12304 but no longer is, find a new starting point. */
12305 else if (!NILP (w->start_at_line_beg)
12306 && !(CHARPOS (startp) <= BEGV
12307 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12308 {
12309 #if GLYPH_DEBUG
12310 debug_method_add (w, "recenter 1");
12311 #endif
12312 goto recenter;
12313 }
12314
12315 /* Try scrolling with try_window_id. Value is > 0 if update has
12316 been done, it is -1 if we know that the same window start will
12317 not work. It is 0 if unsuccessful for some other reason. */
12318 else if ((tem = try_window_id (w)) != 0)
12319 {
12320 #if GLYPH_DEBUG
12321 debug_method_add (w, "try_window_id %d", tem);
12322 #endif
12323
12324 if (fonts_changed_p)
12325 goto need_larger_matrices;
12326 if (tem > 0)
12327 goto done;
12328
12329 /* Otherwise try_window_id has returned -1 which means that we
12330 don't want the alternative below this comment to execute. */
12331 }
12332 else if (CHARPOS (startp) >= BEGV
12333 && CHARPOS (startp) <= ZV
12334 && PT >= CHARPOS (startp)
12335 && (CHARPOS (startp) < ZV
12336 /* Avoid starting at end of buffer. */
12337 || CHARPOS (startp) == BEGV
12338 || (XFASTINT (w->last_modified) >= MODIFF
12339 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12340 {
12341 #if GLYPH_DEBUG
12342 debug_method_add (w, "same window start");
12343 #endif
12344
12345 /* Try to redisplay starting at same place as before.
12346 If point has not moved off frame, accept the results. */
12347 if (!current_matrix_up_to_date_p
12348 /* Don't use try_window_reusing_current_matrix in this case
12349 because a window scroll function can have changed the
12350 buffer. */
12351 || !NILP (Vwindow_scroll_functions)
12352 || MINI_WINDOW_P (w)
12353 || !(used_current_matrix_p
12354 = try_window_reusing_current_matrix (w)))
12355 {
12356 IF_DEBUG (debug_method_add (w, "1"));
12357 if (try_window (window, startp, 1) < 0)
12358 /* -1 means we need to scroll.
12359 0 means we need new matrices, but fonts_changed_p
12360 is set in that case, so we will detect it below. */
12361 goto try_to_scroll;
12362 }
12363
12364 if (fonts_changed_p)
12365 goto need_larger_matrices;
12366
12367 if (w->cursor.vpos >= 0)
12368 {
12369 if (!just_this_one_p
12370 || current_buffer->clip_changed
12371 || BEG_UNCHANGED < CHARPOS (startp))
12372 /* Forget any recorded base line for line number display. */
12373 w->base_line_number = Qnil;
12374
12375 if (!cursor_row_fully_visible_p (w, 1, 0))
12376 {
12377 clear_glyph_matrix (w->desired_matrix);
12378 last_line_misfit = 1;
12379 }
12380 /* Drop through and scroll. */
12381 else
12382 goto done;
12383 }
12384 else
12385 clear_glyph_matrix (w->desired_matrix);
12386 }
12387
12388 try_to_scroll:
12389
12390 w->last_modified = make_number (0);
12391 w->last_overlay_modified = make_number (0);
12392
12393 /* Redisplay the mode line. Select the buffer properly for that. */
12394 if (!update_mode_line)
12395 {
12396 update_mode_line = 1;
12397 w->update_mode_line = Qt;
12398 }
12399
12400 /* Try to scroll by specified few lines. */
12401 if ((scroll_conservatively
12402 || scroll_step
12403 || temp_scroll_step
12404 || NUMBERP (current_buffer->scroll_up_aggressively)
12405 || NUMBERP (current_buffer->scroll_down_aggressively))
12406 && !current_buffer->clip_changed
12407 && CHARPOS (startp) >= BEGV
12408 && CHARPOS (startp) <= ZV)
12409 {
12410 /* The function returns -1 if new fonts were loaded, 1 if
12411 successful, 0 if not successful. */
12412 int rc = try_scrolling (window, just_this_one_p,
12413 scroll_conservatively,
12414 scroll_step,
12415 temp_scroll_step, last_line_misfit);
12416 switch (rc)
12417 {
12418 case SCROLLING_SUCCESS:
12419 goto done;
12420
12421 case SCROLLING_NEED_LARGER_MATRICES:
12422 goto need_larger_matrices;
12423
12424 case SCROLLING_FAILED:
12425 break;
12426
12427 default:
12428 abort ();
12429 }
12430 }
12431
12432 /* Finally, just choose place to start which centers point */
12433
12434 recenter:
12435 if (centering_position < 0)
12436 centering_position = window_box_height (w) / 2;
12437
12438 #if GLYPH_DEBUG
12439 debug_method_add (w, "recenter");
12440 #endif
12441
12442 /* w->vscroll = 0; */
12443
12444 /* Forget any previously recorded base line for line number display. */
12445 if (!buffer_unchanged_p)
12446 w->base_line_number = Qnil;
12447
12448 /* Move backward half the height of the window. */
12449 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12450 it.current_y = it.last_visible_y;
12451 move_it_vertically_backward (&it, centering_position);
12452 xassert (IT_CHARPOS (it) >= BEGV);
12453
12454 /* The function move_it_vertically_backward may move over more
12455 than the specified y-distance. If it->w is small, e.g. a
12456 mini-buffer window, we may end up in front of the window's
12457 display area. Start displaying at the start of the line
12458 containing PT in this case. */
12459 if (it.current_y <= 0)
12460 {
12461 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12462 move_it_vertically_backward (&it, 0);
12463 #if 0
12464 /* I think this assert is bogus if buffer contains
12465 invisible text or images. KFS. */
12466 xassert (IT_CHARPOS (it) <= PT);
12467 #endif
12468 it.current_y = 0;
12469 }
12470
12471 it.current_x = it.hpos = 0;
12472
12473 /* Set startp here explicitly in case that helps avoid an infinite loop
12474 in case the window-scroll-functions functions get errors. */
12475 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12476
12477 /* Run scroll hooks. */
12478 startp = run_window_scroll_functions (window, it.current.pos);
12479
12480 /* Redisplay the window. */
12481 if (!current_matrix_up_to_date_p
12482 || windows_or_buffers_changed
12483 || cursor_type_changed
12484 /* Don't use try_window_reusing_current_matrix in this case
12485 because it can have changed the buffer. */
12486 || !NILP (Vwindow_scroll_functions)
12487 || !just_this_one_p
12488 || MINI_WINDOW_P (w)
12489 || !(used_current_matrix_p
12490 = try_window_reusing_current_matrix (w)))
12491 try_window (window, startp, 0);
12492
12493 /* If new fonts have been loaded (due to fontsets), give up. We
12494 have to start a new redisplay since we need to re-adjust glyph
12495 matrices. */
12496 if (fonts_changed_p)
12497 goto need_larger_matrices;
12498
12499 /* If cursor did not appear assume that the middle of the window is
12500 in the first line of the window. Do it again with the next line.
12501 (Imagine a window of height 100, displaying two lines of height
12502 60. Moving back 50 from it->last_visible_y will end in the first
12503 line.) */
12504 if (w->cursor.vpos < 0)
12505 {
12506 if (!NILP (w->window_end_valid)
12507 && PT >= Z - XFASTINT (w->window_end_pos))
12508 {
12509 clear_glyph_matrix (w->desired_matrix);
12510 move_it_by_lines (&it, 1, 0);
12511 try_window (window, it.current.pos, 0);
12512 }
12513 else if (PT < IT_CHARPOS (it))
12514 {
12515 clear_glyph_matrix (w->desired_matrix);
12516 move_it_by_lines (&it, -1, 0);
12517 try_window (window, it.current.pos, 0);
12518 }
12519 else
12520 {
12521 /* Not much we can do about it. */
12522 }
12523 }
12524
12525 /* Consider the following case: Window starts at BEGV, there is
12526 invisible, intangible text at BEGV, so that display starts at
12527 some point START > BEGV. It can happen that we are called with
12528 PT somewhere between BEGV and START. Try to handle that case. */
12529 if (w->cursor.vpos < 0)
12530 {
12531 struct glyph_row *row = w->current_matrix->rows;
12532 if (row->mode_line_p)
12533 ++row;
12534 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12535 }
12536
12537 if (!cursor_row_fully_visible_p (w, 0, 0))
12538 {
12539 /* If vscroll is enabled, disable it and try again. */
12540 if (w->vscroll)
12541 {
12542 w->vscroll = 0;
12543 clear_glyph_matrix (w->desired_matrix);
12544 goto recenter;
12545 }
12546
12547 /* If centering point failed to make the whole line visible,
12548 put point at the top instead. That has to make the whole line
12549 visible, if it can be done. */
12550 if (centering_position == 0)
12551 goto done;
12552
12553 clear_glyph_matrix (w->desired_matrix);
12554 centering_position = 0;
12555 goto recenter;
12556 }
12557
12558 done:
12559
12560 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12561 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12562 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12563 ? Qt : Qnil);
12564
12565 /* Display the mode line, if we must. */
12566 if ((update_mode_line
12567 /* If window not full width, must redo its mode line
12568 if (a) the window to its side is being redone and
12569 (b) we do a frame-based redisplay. This is a consequence
12570 of how inverted lines are drawn in frame-based redisplay. */
12571 || (!just_this_one_p
12572 && !FRAME_WINDOW_P (f)
12573 && !WINDOW_FULL_WIDTH_P (w))
12574 /* Line number to display. */
12575 || INTEGERP (w->base_line_pos)
12576 /* Column number is displayed and different from the one displayed. */
12577 || (!NILP (w->column_number_displayed)
12578 && (XFASTINT (w->column_number_displayed)
12579 != (int) current_column ()))) /* iftc */
12580 /* This means that the window has a mode line. */
12581 && (WINDOW_WANTS_MODELINE_P (w)
12582 || WINDOW_WANTS_HEADER_LINE_P (w)))
12583 {
12584 display_mode_lines (w);
12585
12586 /* If mode line height has changed, arrange for a thorough
12587 immediate redisplay using the correct mode line height. */
12588 if (WINDOW_WANTS_MODELINE_P (w)
12589 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12590 {
12591 fonts_changed_p = 1;
12592 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12593 = DESIRED_MODE_LINE_HEIGHT (w);
12594 }
12595
12596 /* If top line height has changed, arrange for a thorough
12597 immediate redisplay using the correct mode line height. */
12598 if (WINDOW_WANTS_HEADER_LINE_P (w)
12599 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12600 {
12601 fonts_changed_p = 1;
12602 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12603 = DESIRED_HEADER_LINE_HEIGHT (w);
12604 }
12605
12606 if (fonts_changed_p)
12607 goto need_larger_matrices;
12608 }
12609
12610 if (!line_number_displayed
12611 && !BUFFERP (w->base_line_pos))
12612 {
12613 w->base_line_pos = Qnil;
12614 w->base_line_number = Qnil;
12615 }
12616
12617 finish_menu_bars:
12618
12619 /* When we reach a frame's selected window, redo the frame's menu bar. */
12620 if (update_mode_line
12621 && EQ (FRAME_SELECTED_WINDOW (f), window))
12622 {
12623 int redisplay_menu_p = 0;
12624 int redisplay_tool_bar_p = 0;
12625
12626 if (FRAME_WINDOW_P (f))
12627 {
12628 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12629 || defined (USE_GTK)
12630 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12631 #else
12632 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12633 #endif
12634 }
12635 else
12636 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12637
12638 if (redisplay_menu_p)
12639 display_menu_bar (w);
12640
12641 #ifdef HAVE_WINDOW_SYSTEM
12642 if (FRAME_WINDOW_P (f))
12643 {
12644 #ifdef USE_GTK
12645 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12646 #else
12647 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12648 && (FRAME_TOOL_BAR_LINES (f) > 0
12649 || auto_resize_tool_bars_p);
12650 #endif
12651
12652 if (redisplay_tool_bar_p)
12653 redisplay_tool_bar (f);
12654 }
12655 #endif
12656 }
12657
12658 #ifdef HAVE_WINDOW_SYSTEM
12659 if (FRAME_WINDOW_P (f)
12660 && update_window_fringes (w, 0)
12661 && !just_this_one_p
12662 && (used_current_matrix_p || overlay_arrow_seen)
12663 && !w->pseudo_window_p)
12664 {
12665 update_begin (f);
12666 BLOCK_INPUT;
12667 if (draw_window_fringes (w, 1))
12668 x_draw_vertical_border (w);
12669 UNBLOCK_INPUT;
12670 update_end (f);
12671 }
12672 #endif /* HAVE_WINDOW_SYSTEM */
12673
12674 /* We go to this label, with fonts_changed_p nonzero,
12675 if it is necessary to try again using larger glyph matrices.
12676 We have to redeem the scroll bar even in this case,
12677 because the loop in redisplay_internal expects that. */
12678 need_larger_matrices:
12679 ;
12680 finish_scroll_bars:
12681
12682 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12683 {
12684 /* Set the thumb's position and size. */
12685 set_vertical_scroll_bar (w);
12686
12687 /* Note that we actually used the scroll bar attached to this
12688 window, so it shouldn't be deleted at the end of redisplay. */
12689 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12690 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12691 }
12692
12693 /* Restore current_buffer and value of point in it. */
12694 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12695 set_buffer_internal_1 (old);
12696 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12697
12698 unbind_to (count, Qnil);
12699 }
12700
12701
12702 /* Build the complete desired matrix of WINDOW with a window start
12703 buffer position POS.
12704
12705 Value is 1 if successful. It is zero if fonts were loaded during
12706 redisplay which makes re-adjusting glyph matrices necessary, and -1
12707 if point would appear in the scroll margins.
12708 (We check that only if CHECK_MARGINS is nonzero. */
12709
12710 int
12711 try_window (window, pos, check_margins)
12712 Lisp_Object window;
12713 struct text_pos pos;
12714 int check_margins;
12715 {
12716 struct window *w = XWINDOW (window);
12717 struct it it;
12718 struct glyph_row *last_text_row = NULL;
12719
12720 /* Make POS the new window start. */
12721 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12722
12723 /* Mark cursor position as unknown. No overlay arrow seen. */
12724 w->cursor.vpos = -1;
12725 overlay_arrow_seen = 0;
12726
12727 /* Initialize iterator and info to start at POS. */
12728 start_display (&it, w, pos);
12729
12730 /* Display all lines of W. */
12731 while (it.current_y < it.last_visible_y)
12732 {
12733 if (display_line (&it))
12734 last_text_row = it.glyph_row - 1;
12735 if (fonts_changed_p)
12736 return 0;
12737 }
12738
12739 /* Don't let the cursor end in the scroll margins. */
12740 if (check_margins)
12741 {
12742 int this_scroll_margin, cursor_height;
12743
12744 this_scroll_margin = max (0, scroll_margin);
12745 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12746 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12747 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12748
12749 if ((w->cursor.y < this_scroll_margin
12750 && CHARPOS (pos) > BEGV)
12751 /* rms: considering make_cursor_line_fully_visible_p here
12752 seems to give wrong results. We don't want to recenter
12753 when the last line is partly visible, we want to allow
12754 that case to be handled in the usual way. */
12755 || (w->cursor.y + 1) > it.last_visible_y)
12756 {
12757 w->cursor.vpos = -1;
12758 clear_glyph_matrix (w->desired_matrix);
12759 return -1;
12760 }
12761 }
12762
12763 /* If bottom moved off end of frame, change mode line percentage. */
12764 if (XFASTINT (w->window_end_pos) <= 0
12765 && Z != IT_CHARPOS (it))
12766 w->update_mode_line = Qt;
12767
12768 /* Set window_end_pos to the offset of the last character displayed
12769 on the window from the end of current_buffer. Set
12770 window_end_vpos to its row number. */
12771 if (last_text_row)
12772 {
12773 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12774 w->window_end_bytepos
12775 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12776 w->window_end_pos
12777 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12778 w->window_end_vpos
12779 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12780 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12781 ->displays_text_p);
12782 }
12783 else
12784 {
12785 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12786 w->window_end_pos = make_number (Z - ZV);
12787 w->window_end_vpos = make_number (0);
12788 }
12789
12790 /* But that is not valid info until redisplay finishes. */
12791 w->window_end_valid = Qnil;
12792 return 1;
12793 }
12794
12795
12796 \f
12797 /************************************************************************
12798 Window redisplay reusing current matrix when buffer has not changed
12799 ************************************************************************/
12800
12801 /* Try redisplay of window W showing an unchanged buffer with a
12802 different window start than the last time it was displayed by
12803 reusing its current matrix. Value is non-zero if successful.
12804 W->start is the new window start. */
12805
12806 static int
12807 try_window_reusing_current_matrix (w)
12808 struct window *w;
12809 {
12810 struct frame *f = XFRAME (w->frame);
12811 struct glyph_row *row, *bottom_row;
12812 struct it it;
12813 struct run run;
12814 struct text_pos start, new_start;
12815 int nrows_scrolled, i;
12816 struct glyph_row *last_text_row;
12817 struct glyph_row *last_reused_text_row;
12818 struct glyph_row *start_row;
12819 int start_vpos, min_y, max_y;
12820
12821 #if GLYPH_DEBUG
12822 if (inhibit_try_window_reusing)
12823 return 0;
12824 #endif
12825
12826 if (/* This function doesn't handle terminal frames. */
12827 !FRAME_WINDOW_P (f)
12828 /* Don't try to reuse the display if windows have been split
12829 or such. */
12830 || windows_or_buffers_changed
12831 || cursor_type_changed)
12832 return 0;
12833
12834 /* Can't do this if region may have changed. */
12835 if ((!NILP (Vtransient_mark_mode)
12836 && !NILP (current_buffer->mark_active))
12837 || !NILP (w->region_showing)
12838 || !NILP (Vshow_trailing_whitespace))
12839 return 0;
12840
12841 /* If top-line visibility has changed, give up. */
12842 if (WINDOW_WANTS_HEADER_LINE_P (w)
12843 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12844 return 0;
12845
12846 /* Give up if old or new display is scrolled vertically. We could
12847 make this function handle this, but right now it doesn't. */
12848 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12849 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12850 return 0;
12851
12852 /* The variable new_start now holds the new window start. The old
12853 start `start' can be determined from the current matrix. */
12854 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12855 start = start_row->start.pos;
12856 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12857
12858 /* Clear the desired matrix for the display below. */
12859 clear_glyph_matrix (w->desired_matrix);
12860
12861 if (CHARPOS (new_start) <= CHARPOS (start))
12862 {
12863 int first_row_y;
12864
12865 /* Don't use this method if the display starts with an ellipsis
12866 displayed for invisible text. It's not easy to handle that case
12867 below, and it's certainly not worth the effort since this is
12868 not a frequent case. */
12869 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12870 return 0;
12871
12872 IF_DEBUG (debug_method_add (w, "twu1"));
12873
12874 /* Display up to a row that can be reused. The variable
12875 last_text_row is set to the last row displayed that displays
12876 text. Note that it.vpos == 0 if or if not there is a
12877 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12878 start_display (&it, w, new_start);
12879 first_row_y = it.current_y;
12880 w->cursor.vpos = -1;
12881 last_text_row = last_reused_text_row = NULL;
12882
12883 while (it.current_y < it.last_visible_y
12884 && !fonts_changed_p)
12885 {
12886 /* If we have reached into the characters in the START row,
12887 that means the line boundaries have changed. So we
12888 can't start copying with the row START. Maybe it will
12889 work to start copying with the following row. */
12890 while (IT_CHARPOS (it) > CHARPOS (start))
12891 {
12892 /* Advance to the next row as the "start". */
12893 start_row++;
12894 start = start_row->start.pos;
12895 /* If there are no more rows to try, or just one, give up. */
12896 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12897 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12898 || CHARPOS (start) == ZV)
12899 {
12900 clear_glyph_matrix (w->desired_matrix);
12901 return 0;
12902 }
12903
12904 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12905 }
12906 /* If we have reached alignment,
12907 we can copy the rest of the rows. */
12908 if (IT_CHARPOS (it) == CHARPOS (start))
12909 break;
12910
12911 if (display_line (&it))
12912 last_text_row = it.glyph_row - 1;
12913 }
12914
12915 /* A value of current_y < last_visible_y means that we stopped
12916 at the previous window start, which in turn means that we
12917 have at least one reusable row. */
12918 if (it.current_y < it.last_visible_y)
12919 {
12920 /* IT.vpos always starts from 0; it counts text lines. */
12921 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12922
12923 /* Find PT if not already found in the lines displayed. */
12924 if (w->cursor.vpos < 0)
12925 {
12926 int dy = it.current_y - start_row->y;
12927
12928 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12929 row = row_containing_pos (w, PT, row, NULL, dy);
12930 if (row)
12931 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12932 dy, nrows_scrolled);
12933 else
12934 {
12935 clear_glyph_matrix (w->desired_matrix);
12936 return 0;
12937 }
12938 }
12939
12940 /* Scroll the display. Do it before the current matrix is
12941 changed. The problem here is that update has not yet
12942 run, i.e. part of the current matrix is not up to date.
12943 scroll_run_hook will clear the cursor, and use the
12944 current matrix to get the height of the row the cursor is
12945 in. */
12946 run.current_y = start_row->y;
12947 run.desired_y = it.current_y;
12948 run.height = it.last_visible_y - it.current_y;
12949
12950 if (run.height > 0 && run.current_y != run.desired_y)
12951 {
12952 update_begin (f);
12953 FRAME_RIF (f)->update_window_begin_hook (w);
12954 FRAME_RIF (f)->clear_window_mouse_face (w);
12955 FRAME_RIF (f)->scroll_run_hook (w, &run);
12956 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12957 update_end (f);
12958 }
12959
12960 /* Shift current matrix down by nrows_scrolled lines. */
12961 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12962 rotate_matrix (w->current_matrix,
12963 start_vpos,
12964 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12965 nrows_scrolled);
12966
12967 /* Disable lines that must be updated. */
12968 for (i = 0; i < it.vpos; ++i)
12969 (start_row + i)->enabled_p = 0;
12970
12971 /* Re-compute Y positions. */
12972 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12973 max_y = it.last_visible_y;
12974 for (row = start_row + nrows_scrolled;
12975 row < bottom_row;
12976 ++row)
12977 {
12978 row->y = it.current_y;
12979 row->visible_height = row->height;
12980
12981 if (row->y < min_y)
12982 row->visible_height -= min_y - row->y;
12983 if (row->y + row->height > max_y)
12984 row->visible_height -= row->y + row->height - max_y;
12985 row->redraw_fringe_bitmaps_p = 1;
12986
12987 it.current_y += row->height;
12988
12989 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12990 last_reused_text_row = row;
12991 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12992 break;
12993 }
12994
12995 /* Disable lines in the current matrix which are now
12996 below the window. */
12997 for (++row; row < bottom_row; ++row)
12998 row->enabled_p = 0;
12999 }
13000
13001 /* Update window_end_pos etc.; last_reused_text_row is the last
13002 reused row from the current matrix containing text, if any.
13003 The value of last_text_row is the last displayed line
13004 containing text. */
13005 if (last_reused_text_row)
13006 {
13007 w->window_end_bytepos
13008 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13009 w->window_end_pos
13010 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13011 w->window_end_vpos
13012 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13013 w->current_matrix));
13014 }
13015 else if (last_text_row)
13016 {
13017 w->window_end_bytepos
13018 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13019 w->window_end_pos
13020 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13021 w->window_end_vpos
13022 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13023 }
13024 else
13025 {
13026 /* This window must be completely empty. */
13027 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13028 w->window_end_pos = make_number (Z - ZV);
13029 w->window_end_vpos = make_number (0);
13030 }
13031 w->window_end_valid = Qnil;
13032
13033 /* Update hint: don't try scrolling again in update_window. */
13034 w->desired_matrix->no_scrolling_p = 1;
13035
13036 #if GLYPH_DEBUG
13037 debug_method_add (w, "try_window_reusing_current_matrix 1");
13038 #endif
13039 return 1;
13040 }
13041 else if (CHARPOS (new_start) > CHARPOS (start))
13042 {
13043 struct glyph_row *pt_row, *row;
13044 struct glyph_row *first_reusable_row;
13045 struct glyph_row *first_row_to_display;
13046 int dy;
13047 int yb = window_text_bottom_y (w);
13048
13049 /* Find the row starting at new_start, if there is one. Don't
13050 reuse a partially visible line at the end. */
13051 first_reusable_row = start_row;
13052 while (first_reusable_row->enabled_p
13053 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13054 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13055 < CHARPOS (new_start)))
13056 ++first_reusable_row;
13057
13058 /* Give up if there is no row to reuse. */
13059 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13060 || !first_reusable_row->enabled_p
13061 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13062 != CHARPOS (new_start)))
13063 return 0;
13064
13065 /* We can reuse fully visible rows beginning with
13066 first_reusable_row to the end of the window. Set
13067 first_row_to_display to the first row that cannot be reused.
13068 Set pt_row to the row containing point, if there is any. */
13069 pt_row = NULL;
13070 for (first_row_to_display = first_reusable_row;
13071 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13072 ++first_row_to_display)
13073 {
13074 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13075 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13076 pt_row = first_row_to_display;
13077 }
13078
13079 /* Start displaying at the start of first_row_to_display. */
13080 xassert (first_row_to_display->y < yb);
13081 init_to_row_start (&it, w, first_row_to_display);
13082
13083 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13084 - start_vpos);
13085 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13086 - nrows_scrolled);
13087 it.current_y = (first_row_to_display->y - first_reusable_row->y
13088 + WINDOW_HEADER_LINE_HEIGHT (w));
13089
13090 /* Display lines beginning with first_row_to_display in the
13091 desired matrix. Set last_text_row to the last row displayed
13092 that displays text. */
13093 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13094 if (pt_row == NULL)
13095 w->cursor.vpos = -1;
13096 last_text_row = NULL;
13097 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13098 if (display_line (&it))
13099 last_text_row = it.glyph_row - 1;
13100
13101 /* Give up If point isn't in a row displayed or reused. */
13102 if (w->cursor.vpos < 0)
13103 {
13104 clear_glyph_matrix (w->desired_matrix);
13105 return 0;
13106 }
13107
13108 /* If point is in a reused row, adjust y and vpos of the cursor
13109 position. */
13110 if (pt_row)
13111 {
13112 w->cursor.vpos -= nrows_scrolled;
13113 w->cursor.y -= first_reusable_row->y - start_row->y;
13114 }
13115
13116 /* Scroll the display. */
13117 run.current_y = first_reusable_row->y;
13118 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13119 run.height = it.last_visible_y - run.current_y;
13120 dy = run.current_y - run.desired_y;
13121
13122 if (run.height)
13123 {
13124 update_begin (f);
13125 FRAME_RIF (f)->update_window_begin_hook (w);
13126 FRAME_RIF (f)->clear_window_mouse_face (w);
13127 FRAME_RIF (f)->scroll_run_hook (w, &run);
13128 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13129 update_end (f);
13130 }
13131
13132 /* Adjust Y positions of reused rows. */
13133 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13134 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13135 max_y = it.last_visible_y;
13136 for (row = first_reusable_row; row < first_row_to_display; ++row)
13137 {
13138 row->y -= dy;
13139 row->visible_height = row->height;
13140 if (row->y < min_y)
13141 row->visible_height -= min_y - row->y;
13142 if (row->y + row->height > max_y)
13143 row->visible_height -= row->y + row->height - max_y;
13144 row->redraw_fringe_bitmaps_p = 1;
13145 }
13146
13147 /* Scroll the current matrix. */
13148 xassert (nrows_scrolled > 0);
13149 rotate_matrix (w->current_matrix,
13150 start_vpos,
13151 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13152 -nrows_scrolled);
13153
13154 /* Disable rows not reused. */
13155 for (row -= nrows_scrolled; row < bottom_row; ++row)
13156 row->enabled_p = 0;
13157
13158 /* Point may have moved to a different line, so we cannot assume that
13159 the previous cursor position is valid; locate the correct row. */
13160 if (pt_row)
13161 {
13162 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13163 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13164 row++)
13165 {
13166 w->cursor.vpos++;
13167 w->cursor.y = row->y;
13168 }
13169 if (row < bottom_row)
13170 {
13171 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13172 while (glyph->charpos < PT)
13173 {
13174 w->cursor.hpos++;
13175 w->cursor.x += glyph->pixel_width;
13176 glyph++;
13177 }
13178 }
13179 }
13180
13181 /* Adjust window end. A null value of last_text_row means that
13182 the window end is in reused rows which in turn means that
13183 only its vpos can have changed. */
13184 if (last_text_row)
13185 {
13186 w->window_end_bytepos
13187 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13188 w->window_end_pos
13189 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13190 w->window_end_vpos
13191 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13192 }
13193 else
13194 {
13195 w->window_end_vpos
13196 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13197 }
13198
13199 w->window_end_valid = Qnil;
13200 w->desired_matrix->no_scrolling_p = 1;
13201
13202 #if GLYPH_DEBUG
13203 debug_method_add (w, "try_window_reusing_current_matrix 2");
13204 #endif
13205 return 1;
13206 }
13207
13208 return 0;
13209 }
13210
13211
13212 \f
13213 /************************************************************************
13214 Window redisplay reusing current matrix when buffer has changed
13215 ************************************************************************/
13216
13217 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13218 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13219 int *, int *));
13220 static struct glyph_row *
13221 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13222 struct glyph_row *));
13223
13224
13225 /* Return the last row in MATRIX displaying text. If row START is
13226 non-null, start searching with that row. IT gives the dimensions
13227 of the display. Value is null if matrix is empty; otherwise it is
13228 a pointer to the row found. */
13229
13230 static struct glyph_row *
13231 find_last_row_displaying_text (matrix, it, start)
13232 struct glyph_matrix *matrix;
13233 struct it *it;
13234 struct glyph_row *start;
13235 {
13236 struct glyph_row *row, *row_found;
13237
13238 /* Set row_found to the last row in IT->w's current matrix
13239 displaying text. The loop looks funny but think of partially
13240 visible lines. */
13241 row_found = NULL;
13242 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13243 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13244 {
13245 xassert (row->enabled_p);
13246 row_found = row;
13247 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13248 break;
13249 ++row;
13250 }
13251
13252 return row_found;
13253 }
13254
13255
13256 /* Return the last row in the current matrix of W that is not affected
13257 by changes at the start of current_buffer that occurred since W's
13258 current matrix was built. Value is null if no such row exists.
13259
13260 BEG_UNCHANGED us the number of characters unchanged at the start of
13261 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13262 first changed character in current_buffer. Characters at positions <
13263 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13264 when the current matrix was built. */
13265
13266 static struct glyph_row *
13267 find_last_unchanged_at_beg_row (w)
13268 struct window *w;
13269 {
13270 int first_changed_pos = BEG + BEG_UNCHANGED;
13271 struct glyph_row *row;
13272 struct glyph_row *row_found = NULL;
13273 int yb = window_text_bottom_y (w);
13274
13275 /* Find the last row displaying unchanged text. */
13276 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13277 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13278 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13279 {
13280 if (/* If row ends before first_changed_pos, it is unchanged,
13281 except in some case. */
13282 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13283 /* When row ends in ZV and we write at ZV it is not
13284 unchanged. */
13285 && !row->ends_at_zv_p
13286 /* When first_changed_pos is the end of a continued line,
13287 row is not unchanged because it may be no longer
13288 continued. */
13289 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13290 && (row->continued_p
13291 || row->exact_window_width_line_p)))
13292 row_found = row;
13293
13294 /* Stop if last visible row. */
13295 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13296 break;
13297
13298 ++row;
13299 }
13300
13301 return row_found;
13302 }
13303
13304
13305 /* Find the first glyph row in the current matrix of W that is not
13306 affected by changes at the end of current_buffer since the
13307 time W's current matrix was built.
13308
13309 Return in *DELTA the number of chars by which buffer positions in
13310 unchanged text at the end of current_buffer must be adjusted.
13311
13312 Return in *DELTA_BYTES the corresponding number of bytes.
13313
13314 Value is null if no such row exists, i.e. all rows are affected by
13315 changes. */
13316
13317 static struct glyph_row *
13318 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13319 struct window *w;
13320 int *delta, *delta_bytes;
13321 {
13322 struct glyph_row *row;
13323 struct glyph_row *row_found = NULL;
13324
13325 *delta = *delta_bytes = 0;
13326
13327 /* Display must not have been paused, otherwise the current matrix
13328 is not up to date. */
13329 if (NILP (w->window_end_valid))
13330 abort ();
13331
13332 /* A value of window_end_pos >= END_UNCHANGED means that the window
13333 end is in the range of changed text. If so, there is no
13334 unchanged row at the end of W's current matrix. */
13335 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13336 return NULL;
13337
13338 /* Set row to the last row in W's current matrix displaying text. */
13339 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13340
13341 /* If matrix is entirely empty, no unchanged row exists. */
13342 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13343 {
13344 /* The value of row is the last glyph row in the matrix having a
13345 meaningful buffer position in it. The end position of row
13346 corresponds to window_end_pos. This allows us to translate
13347 buffer positions in the current matrix to current buffer
13348 positions for characters not in changed text. */
13349 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13350 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13351 int last_unchanged_pos, last_unchanged_pos_old;
13352 struct glyph_row *first_text_row
13353 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13354
13355 *delta = Z - Z_old;
13356 *delta_bytes = Z_BYTE - Z_BYTE_old;
13357
13358 /* Set last_unchanged_pos to the buffer position of the last
13359 character in the buffer that has not been changed. Z is the
13360 index + 1 of the last character in current_buffer, i.e. by
13361 subtracting END_UNCHANGED we get the index of the last
13362 unchanged character, and we have to add BEG to get its buffer
13363 position. */
13364 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13365 last_unchanged_pos_old = last_unchanged_pos - *delta;
13366
13367 /* Search backward from ROW for a row displaying a line that
13368 starts at a minimum position >= last_unchanged_pos_old. */
13369 for (; row > first_text_row; --row)
13370 {
13371 /* This used to abort, but it can happen.
13372 It is ok to just stop the search instead here. KFS. */
13373 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13374 break;
13375
13376 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13377 row_found = row;
13378 }
13379 }
13380
13381 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13382 abort ();
13383
13384 return row_found;
13385 }
13386
13387
13388 /* Make sure that glyph rows in the current matrix of window W
13389 reference the same glyph memory as corresponding rows in the
13390 frame's frame matrix. This function is called after scrolling W's
13391 current matrix on a terminal frame in try_window_id and
13392 try_window_reusing_current_matrix. */
13393
13394 static void
13395 sync_frame_with_window_matrix_rows (w)
13396 struct window *w;
13397 {
13398 struct frame *f = XFRAME (w->frame);
13399 struct glyph_row *window_row, *window_row_end, *frame_row;
13400
13401 /* Preconditions: W must be a leaf window and full-width. Its frame
13402 must have a frame matrix. */
13403 xassert (NILP (w->hchild) && NILP (w->vchild));
13404 xassert (WINDOW_FULL_WIDTH_P (w));
13405 xassert (!FRAME_WINDOW_P (f));
13406
13407 /* If W is a full-width window, glyph pointers in W's current matrix
13408 have, by definition, to be the same as glyph pointers in the
13409 corresponding frame matrix. Note that frame matrices have no
13410 marginal areas (see build_frame_matrix). */
13411 window_row = w->current_matrix->rows;
13412 window_row_end = window_row + w->current_matrix->nrows;
13413 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13414 while (window_row < window_row_end)
13415 {
13416 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13417 struct glyph *end = window_row->glyphs[LAST_AREA];
13418
13419 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13420 frame_row->glyphs[TEXT_AREA] = start;
13421 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13422 frame_row->glyphs[LAST_AREA] = end;
13423
13424 /* Disable frame rows whose corresponding window rows have
13425 been disabled in try_window_id. */
13426 if (!window_row->enabled_p)
13427 frame_row->enabled_p = 0;
13428
13429 ++window_row, ++frame_row;
13430 }
13431 }
13432
13433
13434 /* Find the glyph row in window W containing CHARPOS. Consider all
13435 rows between START and END (not inclusive). END null means search
13436 all rows to the end of the display area of W. Value is the row
13437 containing CHARPOS or null. */
13438
13439 struct glyph_row *
13440 row_containing_pos (w, charpos, start, end, dy)
13441 struct window *w;
13442 int charpos;
13443 struct glyph_row *start, *end;
13444 int dy;
13445 {
13446 struct glyph_row *row = start;
13447 int last_y;
13448
13449 /* If we happen to start on a header-line, skip that. */
13450 if (row->mode_line_p)
13451 ++row;
13452
13453 if ((end && row >= end) || !row->enabled_p)
13454 return NULL;
13455
13456 last_y = window_text_bottom_y (w) - dy;
13457
13458 while (1)
13459 {
13460 /* Give up if we have gone too far. */
13461 if (end && row >= end)
13462 return NULL;
13463 /* This formerly returned if they were equal.
13464 I think that both quantities are of a "last plus one" type;
13465 if so, when they are equal, the row is within the screen. -- rms. */
13466 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13467 return NULL;
13468
13469 /* If it is in this row, return this row. */
13470 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13471 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13472 /* The end position of a row equals the start
13473 position of the next row. If CHARPOS is there, we
13474 would rather display it in the next line, except
13475 when this line ends in ZV. */
13476 && !row->ends_at_zv_p
13477 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13478 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13479 return row;
13480 ++row;
13481 }
13482 }
13483
13484
13485 /* Try to redisplay window W by reusing its existing display. W's
13486 current matrix must be up to date when this function is called,
13487 i.e. window_end_valid must not be nil.
13488
13489 Value is
13490
13491 1 if display has been updated
13492 0 if otherwise unsuccessful
13493 -1 if redisplay with same window start is known not to succeed
13494
13495 The following steps are performed:
13496
13497 1. Find the last row in the current matrix of W that is not
13498 affected by changes at the start of current_buffer. If no such row
13499 is found, give up.
13500
13501 2. Find the first row in W's current matrix that is not affected by
13502 changes at the end of current_buffer. Maybe there is no such row.
13503
13504 3. Display lines beginning with the row + 1 found in step 1 to the
13505 row found in step 2 or, if step 2 didn't find a row, to the end of
13506 the window.
13507
13508 4. If cursor is not known to appear on the window, give up.
13509
13510 5. If display stopped at the row found in step 2, scroll the
13511 display and current matrix as needed.
13512
13513 6. Maybe display some lines at the end of W, if we must. This can
13514 happen under various circumstances, like a partially visible line
13515 becoming fully visible, or because newly displayed lines are displayed
13516 in smaller font sizes.
13517
13518 7. Update W's window end information. */
13519
13520 static int
13521 try_window_id (w)
13522 struct window *w;
13523 {
13524 struct frame *f = XFRAME (w->frame);
13525 struct glyph_matrix *current_matrix = w->current_matrix;
13526 struct glyph_matrix *desired_matrix = w->desired_matrix;
13527 struct glyph_row *last_unchanged_at_beg_row;
13528 struct glyph_row *first_unchanged_at_end_row;
13529 struct glyph_row *row;
13530 struct glyph_row *bottom_row;
13531 int bottom_vpos;
13532 struct it it;
13533 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13534 struct text_pos start_pos;
13535 struct run run;
13536 int first_unchanged_at_end_vpos = 0;
13537 struct glyph_row *last_text_row, *last_text_row_at_end;
13538 struct text_pos start;
13539 int first_changed_charpos, last_changed_charpos;
13540
13541 #if GLYPH_DEBUG
13542 if (inhibit_try_window_id)
13543 return 0;
13544 #endif
13545
13546 /* This is handy for debugging. */
13547 #if 0
13548 #define GIVE_UP(X) \
13549 do { \
13550 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13551 return 0; \
13552 } while (0)
13553 #else
13554 #define GIVE_UP(X) return 0
13555 #endif
13556
13557 SET_TEXT_POS_FROM_MARKER (start, w->start);
13558
13559 /* Don't use this for mini-windows because these can show
13560 messages and mini-buffers, and we don't handle that here. */
13561 if (MINI_WINDOW_P (w))
13562 GIVE_UP (1);
13563
13564 /* This flag is used to prevent redisplay optimizations. */
13565 if (windows_or_buffers_changed || cursor_type_changed)
13566 GIVE_UP (2);
13567
13568 /* Verify that narrowing has not changed.
13569 Also verify that we were not told to prevent redisplay optimizations.
13570 It would be nice to further
13571 reduce the number of cases where this prevents try_window_id. */
13572 if (current_buffer->clip_changed
13573 || current_buffer->prevent_redisplay_optimizations_p)
13574 GIVE_UP (3);
13575
13576 /* Window must either use window-based redisplay or be full width. */
13577 if (!FRAME_WINDOW_P (f)
13578 && (!FRAME_LINE_INS_DEL_OK (f)
13579 || !WINDOW_FULL_WIDTH_P (w)))
13580 GIVE_UP (4);
13581
13582 /* Give up if point is not known NOT to appear in W. */
13583 if (PT < CHARPOS (start))
13584 GIVE_UP (5);
13585
13586 /* Another way to prevent redisplay optimizations. */
13587 if (XFASTINT (w->last_modified) == 0)
13588 GIVE_UP (6);
13589
13590 /* Verify that window is not hscrolled. */
13591 if (XFASTINT (w->hscroll) != 0)
13592 GIVE_UP (7);
13593
13594 /* Verify that display wasn't paused. */
13595 if (NILP (w->window_end_valid))
13596 GIVE_UP (8);
13597
13598 /* Can't use this if highlighting a region because a cursor movement
13599 will do more than just set the cursor. */
13600 if (!NILP (Vtransient_mark_mode)
13601 && !NILP (current_buffer->mark_active))
13602 GIVE_UP (9);
13603
13604 /* Likewise if highlighting trailing whitespace. */
13605 if (!NILP (Vshow_trailing_whitespace))
13606 GIVE_UP (11);
13607
13608 /* Likewise if showing a region. */
13609 if (!NILP (w->region_showing))
13610 GIVE_UP (10);
13611
13612 /* Can use this if overlay arrow position and or string have changed. */
13613 if (overlay_arrows_changed_p ())
13614 GIVE_UP (12);
13615
13616
13617 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13618 only if buffer has really changed. The reason is that the gap is
13619 initially at Z for freshly visited files. The code below would
13620 set end_unchanged to 0 in that case. */
13621 if (MODIFF > SAVE_MODIFF
13622 /* This seems to happen sometimes after saving a buffer. */
13623 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13624 {
13625 if (GPT - BEG < BEG_UNCHANGED)
13626 BEG_UNCHANGED = GPT - BEG;
13627 if (Z - GPT < END_UNCHANGED)
13628 END_UNCHANGED = Z - GPT;
13629 }
13630
13631 /* The position of the first and last character that has been changed. */
13632 first_changed_charpos = BEG + BEG_UNCHANGED;
13633 last_changed_charpos = Z - END_UNCHANGED;
13634
13635 /* If window starts after a line end, and the last change is in
13636 front of that newline, then changes don't affect the display.
13637 This case happens with stealth-fontification. Note that although
13638 the display is unchanged, glyph positions in the matrix have to
13639 be adjusted, of course. */
13640 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13641 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13642 && ((last_changed_charpos < CHARPOS (start)
13643 && CHARPOS (start) == BEGV)
13644 || (last_changed_charpos < CHARPOS (start) - 1
13645 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13646 {
13647 int Z_old, delta, Z_BYTE_old, delta_bytes;
13648 struct glyph_row *r0;
13649
13650 /* Compute how many chars/bytes have been added to or removed
13651 from the buffer. */
13652 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13653 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13654 delta = Z - Z_old;
13655 delta_bytes = Z_BYTE - Z_BYTE_old;
13656
13657 /* Give up if PT is not in the window. Note that it already has
13658 been checked at the start of try_window_id that PT is not in
13659 front of the window start. */
13660 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13661 GIVE_UP (13);
13662
13663 /* If window start is unchanged, we can reuse the whole matrix
13664 as is, after adjusting glyph positions. No need to compute
13665 the window end again, since its offset from Z hasn't changed. */
13666 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13667 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13668 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13669 /* PT must not be in a partially visible line. */
13670 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13671 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13672 {
13673 /* Adjust positions in the glyph matrix. */
13674 if (delta || delta_bytes)
13675 {
13676 struct glyph_row *r1
13677 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13678 increment_matrix_positions (w->current_matrix,
13679 MATRIX_ROW_VPOS (r0, current_matrix),
13680 MATRIX_ROW_VPOS (r1, current_matrix),
13681 delta, delta_bytes);
13682 }
13683
13684 /* Set the cursor. */
13685 row = row_containing_pos (w, PT, r0, NULL, 0);
13686 if (row)
13687 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13688 else
13689 abort ();
13690 return 1;
13691 }
13692 }
13693
13694 /* Handle the case that changes are all below what is displayed in
13695 the window, and that PT is in the window. This shortcut cannot
13696 be taken if ZV is visible in the window, and text has been added
13697 there that is visible in the window. */
13698 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13699 /* ZV is not visible in the window, or there are no
13700 changes at ZV, actually. */
13701 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13702 || first_changed_charpos == last_changed_charpos))
13703 {
13704 struct glyph_row *r0;
13705
13706 /* Give up if PT is not in the window. Note that it already has
13707 been checked at the start of try_window_id that PT is not in
13708 front of the window start. */
13709 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13710 GIVE_UP (14);
13711
13712 /* If window start is unchanged, we can reuse the whole matrix
13713 as is, without changing glyph positions since no text has
13714 been added/removed in front of the window end. */
13715 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13716 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13717 /* PT must not be in a partially visible line. */
13718 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13719 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13720 {
13721 /* We have to compute the window end anew since text
13722 can have been added/removed after it. */
13723 w->window_end_pos
13724 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13725 w->window_end_bytepos
13726 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13727
13728 /* Set the cursor. */
13729 row = row_containing_pos (w, PT, r0, NULL, 0);
13730 if (row)
13731 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13732 else
13733 abort ();
13734 return 2;
13735 }
13736 }
13737
13738 /* Give up if window start is in the changed area.
13739
13740 The condition used to read
13741
13742 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13743
13744 but why that was tested escapes me at the moment. */
13745 if (CHARPOS (start) >= first_changed_charpos
13746 && CHARPOS (start) <= last_changed_charpos)
13747 GIVE_UP (15);
13748
13749 /* Check that window start agrees with the start of the first glyph
13750 row in its current matrix. Check this after we know the window
13751 start is not in changed text, otherwise positions would not be
13752 comparable. */
13753 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13754 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13755 GIVE_UP (16);
13756
13757 /* Give up if the window ends in strings. Overlay strings
13758 at the end are difficult to handle, so don't try. */
13759 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13760 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13761 GIVE_UP (20);
13762
13763 /* Compute the position at which we have to start displaying new
13764 lines. Some of the lines at the top of the window might be
13765 reusable because they are not displaying changed text. Find the
13766 last row in W's current matrix not affected by changes at the
13767 start of current_buffer. Value is null if changes start in the
13768 first line of window. */
13769 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13770 if (last_unchanged_at_beg_row)
13771 {
13772 /* Avoid starting to display in the moddle of a character, a TAB
13773 for instance. This is easier than to set up the iterator
13774 exactly, and it's not a frequent case, so the additional
13775 effort wouldn't really pay off. */
13776 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13777 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13778 && last_unchanged_at_beg_row > w->current_matrix->rows)
13779 --last_unchanged_at_beg_row;
13780
13781 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13782 GIVE_UP (17);
13783
13784 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13785 GIVE_UP (18);
13786 start_pos = it.current.pos;
13787
13788 /* Start displaying new lines in the desired matrix at the same
13789 vpos we would use in the current matrix, i.e. below
13790 last_unchanged_at_beg_row. */
13791 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13792 current_matrix);
13793 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13794 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13795
13796 xassert (it.hpos == 0 && it.current_x == 0);
13797 }
13798 else
13799 {
13800 /* There are no reusable lines at the start of the window.
13801 Start displaying in the first text line. */
13802 start_display (&it, w, start);
13803 it.vpos = it.first_vpos;
13804 start_pos = it.current.pos;
13805 }
13806
13807 /* Find the first row that is not affected by changes at the end of
13808 the buffer. Value will be null if there is no unchanged row, in
13809 which case we must redisplay to the end of the window. delta
13810 will be set to the value by which buffer positions beginning with
13811 first_unchanged_at_end_row have to be adjusted due to text
13812 changes. */
13813 first_unchanged_at_end_row
13814 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13815 IF_DEBUG (debug_delta = delta);
13816 IF_DEBUG (debug_delta_bytes = delta_bytes);
13817
13818 /* Set stop_pos to the buffer position up to which we will have to
13819 display new lines. If first_unchanged_at_end_row != NULL, this
13820 is the buffer position of the start of the line displayed in that
13821 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13822 that we don't stop at a buffer position. */
13823 stop_pos = 0;
13824 if (first_unchanged_at_end_row)
13825 {
13826 xassert (last_unchanged_at_beg_row == NULL
13827 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13828
13829 /* If this is a continuation line, move forward to the next one
13830 that isn't. Changes in lines above affect this line.
13831 Caution: this may move first_unchanged_at_end_row to a row
13832 not displaying text. */
13833 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13834 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13835 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13836 < it.last_visible_y))
13837 ++first_unchanged_at_end_row;
13838
13839 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13840 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13841 >= it.last_visible_y))
13842 first_unchanged_at_end_row = NULL;
13843 else
13844 {
13845 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13846 + delta);
13847 first_unchanged_at_end_vpos
13848 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13849 xassert (stop_pos >= Z - END_UNCHANGED);
13850 }
13851 }
13852 else if (last_unchanged_at_beg_row == NULL)
13853 GIVE_UP (19);
13854
13855
13856 #if GLYPH_DEBUG
13857
13858 /* Either there is no unchanged row at the end, or the one we have
13859 now displays text. This is a necessary condition for the window
13860 end pos calculation at the end of this function. */
13861 xassert (first_unchanged_at_end_row == NULL
13862 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13863
13864 debug_last_unchanged_at_beg_vpos
13865 = (last_unchanged_at_beg_row
13866 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13867 : -1);
13868 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13869
13870 #endif /* GLYPH_DEBUG != 0 */
13871
13872
13873 /* Display new lines. Set last_text_row to the last new line
13874 displayed which has text on it, i.e. might end up as being the
13875 line where the window_end_vpos is. */
13876 w->cursor.vpos = -1;
13877 last_text_row = NULL;
13878 overlay_arrow_seen = 0;
13879 while (it.current_y < it.last_visible_y
13880 && !fonts_changed_p
13881 && (first_unchanged_at_end_row == NULL
13882 || IT_CHARPOS (it) < stop_pos))
13883 {
13884 if (display_line (&it))
13885 last_text_row = it.glyph_row - 1;
13886 }
13887
13888 if (fonts_changed_p)
13889 return -1;
13890
13891
13892 /* Compute differences in buffer positions, y-positions etc. for
13893 lines reused at the bottom of the window. Compute what we can
13894 scroll. */
13895 if (first_unchanged_at_end_row
13896 /* No lines reused because we displayed everything up to the
13897 bottom of the window. */
13898 && it.current_y < it.last_visible_y)
13899 {
13900 dvpos = (it.vpos
13901 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13902 current_matrix));
13903 dy = it.current_y - first_unchanged_at_end_row->y;
13904 run.current_y = first_unchanged_at_end_row->y;
13905 run.desired_y = run.current_y + dy;
13906 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13907 }
13908 else
13909 {
13910 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13911 first_unchanged_at_end_row = NULL;
13912 }
13913 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13914
13915
13916 /* Find the cursor if not already found. We have to decide whether
13917 PT will appear on this window (it sometimes doesn't, but this is
13918 not a very frequent case.) This decision has to be made before
13919 the current matrix is altered. A value of cursor.vpos < 0 means
13920 that PT is either in one of the lines beginning at
13921 first_unchanged_at_end_row or below the window. Don't care for
13922 lines that might be displayed later at the window end; as
13923 mentioned, this is not a frequent case. */
13924 if (w->cursor.vpos < 0)
13925 {
13926 /* Cursor in unchanged rows at the top? */
13927 if (PT < CHARPOS (start_pos)
13928 && last_unchanged_at_beg_row)
13929 {
13930 row = row_containing_pos (w, PT,
13931 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13932 last_unchanged_at_beg_row + 1, 0);
13933 if (row)
13934 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13935 }
13936
13937 /* Start from first_unchanged_at_end_row looking for PT. */
13938 else if (first_unchanged_at_end_row)
13939 {
13940 row = row_containing_pos (w, PT - delta,
13941 first_unchanged_at_end_row, NULL, 0);
13942 if (row)
13943 set_cursor_from_row (w, row, w->current_matrix, delta,
13944 delta_bytes, dy, dvpos);
13945 }
13946
13947 /* Give up if cursor was not found. */
13948 if (w->cursor.vpos < 0)
13949 {
13950 clear_glyph_matrix (w->desired_matrix);
13951 return -1;
13952 }
13953 }
13954
13955 /* Don't let the cursor end in the scroll margins. */
13956 {
13957 int this_scroll_margin, cursor_height;
13958
13959 this_scroll_margin = max (0, scroll_margin);
13960 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13961 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13962 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13963
13964 if ((w->cursor.y < this_scroll_margin
13965 && CHARPOS (start) > BEGV)
13966 /* Old redisplay didn't take scroll margin into account at the bottom,
13967 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13968 || (w->cursor.y + (make_cursor_line_fully_visible_p
13969 ? cursor_height + this_scroll_margin
13970 : 1)) > it.last_visible_y)
13971 {
13972 w->cursor.vpos = -1;
13973 clear_glyph_matrix (w->desired_matrix);
13974 return -1;
13975 }
13976 }
13977
13978 /* Scroll the display. Do it before changing the current matrix so
13979 that xterm.c doesn't get confused about where the cursor glyph is
13980 found. */
13981 if (dy && run.height)
13982 {
13983 update_begin (f);
13984
13985 if (FRAME_WINDOW_P (f))
13986 {
13987 FRAME_RIF (f)->update_window_begin_hook (w);
13988 FRAME_RIF (f)->clear_window_mouse_face (w);
13989 FRAME_RIF (f)->scroll_run_hook (w, &run);
13990 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13991 }
13992 else
13993 {
13994 /* Terminal frame. In this case, dvpos gives the number of
13995 lines to scroll by; dvpos < 0 means scroll up. */
13996 int first_unchanged_at_end_vpos
13997 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13998 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13999 int end = (WINDOW_TOP_EDGE_LINE (w)
14000 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14001 + window_internal_height (w));
14002
14003 /* Perform the operation on the screen. */
14004 if (dvpos > 0)
14005 {
14006 /* Scroll last_unchanged_at_beg_row to the end of the
14007 window down dvpos lines. */
14008 set_terminal_window (f, end);
14009
14010 /* On dumb terminals delete dvpos lines at the end
14011 before inserting dvpos empty lines. */
14012 if (!FRAME_SCROLL_REGION_OK (f))
14013 ins_del_lines (f, end - dvpos, -dvpos);
14014
14015 /* Insert dvpos empty lines in front of
14016 last_unchanged_at_beg_row. */
14017 ins_del_lines (f, from, dvpos);
14018 }
14019 else if (dvpos < 0)
14020 {
14021 /* Scroll up last_unchanged_at_beg_vpos to the end of
14022 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14023 set_terminal_window (f, end);
14024
14025 /* Delete dvpos lines in front of
14026 last_unchanged_at_beg_vpos. ins_del_lines will set
14027 the cursor to the given vpos and emit |dvpos| delete
14028 line sequences. */
14029 ins_del_lines (f, from + dvpos, dvpos);
14030
14031 /* On a dumb terminal insert dvpos empty lines at the
14032 end. */
14033 if (!FRAME_SCROLL_REGION_OK (f))
14034 ins_del_lines (f, end + dvpos, -dvpos);
14035 }
14036
14037 set_terminal_window (f, 0);
14038 }
14039
14040 update_end (f);
14041 }
14042
14043 /* Shift reused rows of the current matrix to the right position.
14044 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14045 text. */
14046 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14047 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14048 if (dvpos < 0)
14049 {
14050 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14051 bottom_vpos, dvpos);
14052 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14053 bottom_vpos, 0);
14054 }
14055 else if (dvpos > 0)
14056 {
14057 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14058 bottom_vpos, dvpos);
14059 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14060 first_unchanged_at_end_vpos + dvpos, 0);
14061 }
14062
14063 /* For frame-based redisplay, make sure that current frame and window
14064 matrix are in sync with respect to glyph memory. */
14065 if (!FRAME_WINDOW_P (f))
14066 sync_frame_with_window_matrix_rows (w);
14067
14068 /* Adjust buffer positions in reused rows. */
14069 if (delta)
14070 increment_matrix_positions (current_matrix,
14071 first_unchanged_at_end_vpos + dvpos,
14072 bottom_vpos, delta, delta_bytes);
14073
14074 /* Adjust Y positions. */
14075 if (dy)
14076 shift_glyph_matrix (w, current_matrix,
14077 first_unchanged_at_end_vpos + dvpos,
14078 bottom_vpos, dy);
14079
14080 if (first_unchanged_at_end_row)
14081 {
14082 first_unchanged_at_end_row += dvpos;
14083 if (first_unchanged_at_end_row->y >= it.last_visible_y
14084 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14085 first_unchanged_at_end_row = NULL;
14086 }
14087
14088 /* If scrolling up, there may be some lines to display at the end of
14089 the window. */
14090 last_text_row_at_end = NULL;
14091 if (dy < 0)
14092 {
14093 /* Scrolling up can leave for example a partially visible line
14094 at the end of the window to be redisplayed. */
14095 /* Set last_row to the glyph row in the current matrix where the
14096 window end line is found. It has been moved up or down in
14097 the matrix by dvpos. */
14098 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14099 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14100
14101 /* If last_row is the window end line, it should display text. */
14102 xassert (last_row->displays_text_p);
14103
14104 /* If window end line was partially visible before, begin
14105 displaying at that line. Otherwise begin displaying with the
14106 line following it. */
14107 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14108 {
14109 init_to_row_start (&it, w, last_row);
14110 it.vpos = last_vpos;
14111 it.current_y = last_row->y;
14112 }
14113 else
14114 {
14115 init_to_row_end (&it, w, last_row);
14116 it.vpos = 1 + last_vpos;
14117 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14118 ++last_row;
14119 }
14120
14121 /* We may start in a continuation line. If so, we have to
14122 get the right continuation_lines_width and current_x. */
14123 it.continuation_lines_width = last_row->continuation_lines_width;
14124 it.hpos = it.current_x = 0;
14125
14126 /* Display the rest of the lines at the window end. */
14127 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14128 while (it.current_y < it.last_visible_y
14129 && !fonts_changed_p)
14130 {
14131 /* Is it always sure that the display agrees with lines in
14132 the current matrix? I don't think so, so we mark rows
14133 displayed invalid in the current matrix by setting their
14134 enabled_p flag to zero. */
14135 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14136 if (display_line (&it))
14137 last_text_row_at_end = it.glyph_row - 1;
14138 }
14139 }
14140
14141 /* Update window_end_pos and window_end_vpos. */
14142 if (first_unchanged_at_end_row
14143 && !last_text_row_at_end)
14144 {
14145 /* Window end line if one of the preserved rows from the current
14146 matrix. Set row to the last row displaying text in current
14147 matrix starting at first_unchanged_at_end_row, after
14148 scrolling. */
14149 xassert (first_unchanged_at_end_row->displays_text_p);
14150 row = find_last_row_displaying_text (w->current_matrix, &it,
14151 first_unchanged_at_end_row);
14152 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14153
14154 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14155 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14156 w->window_end_vpos
14157 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14158 xassert (w->window_end_bytepos >= 0);
14159 IF_DEBUG (debug_method_add (w, "A"));
14160 }
14161 else if (last_text_row_at_end)
14162 {
14163 w->window_end_pos
14164 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14165 w->window_end_bytepos
14166 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14167 w->window_end_vpos
14168 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14169 xassert (w->window_end_bytepos >= 0);
14170 IF_DEBUG (debug_method_add (w, "B"));
14171 }
14172 else if (last_text_row)
14173 {
14174 /* We have displayed either to the end of the window or at the
14175 end of the window, i.e. the last row with text is to be found
14176 in the desired matrix. */
14177 w->window_end_pos
14178 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14179 w->window_end_bytepos
14180 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14181 w->window_end_vpos
14182 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14183 xassert (w->window_end_bytepos >= 0);
14184 }
14185 else if (first_unchanged_at_end_row == NULL
14186 && last_text_row == NULL
14187 && last_text_row_at_end == NULL)
14188 {
14189 /* Displayed to end of window, but no line containing text was
14190 displayed. Lines were deleted at the end of the window. */
14191 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14192 int vpos = XFASTINT (w->window_end_vpos);
14193 struct glyph_row *current_row = current_matrix->rows + vpos;
14194 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14195
14196 for (row = NULL;
14197 row == NULL && vpos >= first_vpos;
14198 --vpos, --current_row, --desired_row)
14199 {
14200 if (desired_row->enabled_p)
14201 {
14202 if (desired_row->displays_text_p)
14203 row = desired_row;
14204 }
14205 else if (current_row->displays_text_p)
14206 row = current_row;
14207 }
14208
14209 xassert (row != NULL);
14210 w->window_end_vpos = make_number (vpos + 1);
14211 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14212 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14213 xassert (w->window_end_bytepos >= 0);
14214 IF_DEBUG (debug_method_add (w, "C"));
14215 }
14216 else
14217 abort ();
14218
14219 #if 0 /* This leads to problems, for instance when the cursor is
14220 at ZV, and the cursor line displays no text. */
14221 /* Disable rows below what's displayed in the window. This makes
14222 debugging easier. */
14223 enable_glyph_matrix_rows (current_matrix,
14224 XFASTINT (w->window_end_vpos) + 1,
14225 bottom_vpos, 0);
14226 #endif
14227
14228 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14229 debug_end_vpos = XFASTINT (w->window_end_vpos));
14230
14231 /* Record that display has not been completed. */
14232 w->window_end_valid = Qnil;
14233 w->desired_matrix->no_scrolling_p = 1;
14234 return 3;
14235
14236 #undef GIVE_UP
14237 }
14238
14239
14240 \f
14241 /***********************************************************************
14242 More debugging support
14243 ***********************************************************************/
14244
14245 #if GLYPH_DEBUG
14246
14247 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14248 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14249 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14250
14251
14252 /* Dump the contents of glyph matrix MATRIX on stderr.
14253
14254 GLYPHS 0 means don't show glyph contents.
14255 GLYPHS 1 means show glyphs in short form
14256 GLYPHS > 1 means show glyphs in long form. */
14257
14258 void
14259 dump_glyph_matrix (matrix, glyphs)
14260 struct glyph_matrix *matrix;
14261 int glyphs;
14262 {
14263 int i;
14264 for (i = 0; i < matrix->nrows; ++i)
14265 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14266 }
14267
14268
14269 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14270 the glyph row and area where the glyph comes from. */
14271
14272 void
14273 dump_glyph (row, glyph, area)
14274 struct glyph_row *row;
14275 struct glyph *glyph;
14276 int area;
14277 {
14278 if (glyph->type == CHAR_GLYPH)
14279 {
14280 fprintf (stderr,
14281 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14282 glyph - row->glyphs[TEXT_AREA],
14283 'C',
14284 glyph->charpos,
14285 (BUFFERP (glyph->object)
14286 ? 'B'
14287 : (STRINGP (glyph->object)
14288 ? 'S'
14289 : '-')),
14290 glyph->pixel_width,
14291 glyph->u.ch,
14292 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14293 ? glyph->u.ch
14294 : '.'),
14295 glyph->face_id,
14296 glyph->left_box_line_p,
14297 glyph->right_box_line_p);
14298 }
14299 else if (glyph->type == STRETCH_GLYPH)
14300 {
14301 fprintf (stderr,
14302 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14303 glyph - row->glyphs[TEXT_AREA],
14304 'S',
14305 glyph->charpos,
14306 (BUFFERP (glyph->object)
14307 ? 'B'
14308 : (STRINGP (glyph->object)
14309 ? 'S'
14310 : '-')),
14311 glyph->pixel_width,
14312 0,
14313 '.',
14314 glyph->face_id,
14315 glyph->left_box_line_p,
14316 glyph->right_box_line_p);
14317 }
14318 else if (glyph->type == IMAGE_GLYPH)
14319 {
14320 fprintf (stderr,
14321 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14322 glyph - row->glyphs[TEXT_AREA],
14323 'I',
14324 glyph->charpos,
14325 (BUFFERP (glyph->object)
14326 ? 'B'
14327 : (STRINGP (glyph->object)
14328 ? 'S'
14329 : '-')),
14330 glyph->pixel_width,
14331 glyph->u.img_id,
14332 '.',
14333 glyph->face_id,
14334 glyph->left_box_line_p,
14335 glyph->right_box_line_p);
14336 }
14337 }
14338
14339
14340 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14341 GLYPHS 0 means don't show glyph contents.
14342 GLYPHS 1 means show glyphs in short form
14343 GLYPHS > 1 means show glyphs in long form. */
14344
14345 void
14346 dump_glyph_row (row, vpos, glyphs)
14347 struct glyph_row *row;
14348 int vpos, glyphs;
14349 {
14350 if (glyphs != 1)
14351 {
14352 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14353 fprintf (stderr, "======================================================================\n");
14354
14355 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14356 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14357 vpos,
14358 MATRIX_ROW_START_CHARPOS (row),
14359 MATRIX_ROW_END_CHARPOS (row),
14360 row->used[TEXT_AREA],
14361 row->contains_overlapping_glyphs_p,
14362 row->enabled_p,
14363 row->truncated_on_left_p,
14364 row->truncated_on_right_p,
14365 row->continued_p,
14366 MATRIX_ROW_CONTINUATION_LINE_P (row),
14367 row->displays_text_p,
14368 row->ends_at_zv_p,
14369 row->fill_line_p,
14370 row->ends_in_middle_of_char_p,
14371 row->starts_in_middle_of_char_p,
14372 row->mouse_face_p,
14373 row->x,
14374 row->y,
14375 row->pixel_width,
14376 row->height,
14377 row->visible_height,
14378 row->ascent,
14379 row->phys_ascent);
14380 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14381 row->end.overlay_string_index,
14382 row->continuation_lines_width);
14383 fprintf (stderr, "%9d %5d\n",
14384 CHARPOS (row->start.string_pos),
14385 CHARPOS (row->end.string_pos));
14386 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14387 row->end.dpvec_index);
14388 }
14389
14390 if (glyphs > 1)
14391 {
14392 int area;
14393
14394 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14395 {
14396 struct glyph *glyph = row->glyphs[area];
14397 struct glyph *glyph_end = glyph + row->used[area];
14398
14399 /* Glyph for a line end in text. */
14400 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14401 ++glyph_end;
14402
14403 if (glyph < glyph_end)
14404 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14405
14406 for (; glyph < glyph_end; ++glyph)
14407 dump_glyph (row, glyph, area);
14408 }
14409 }
14410 else if (glyphs == 1)
14411 {
14412 int area;
14413
14414 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14415 {
14416 char *s = (char *) alloca (row->used[area] + 1);
14417 int i;
14418
14419 for (i = 0; i < row->used[area]; ++i)
14420 {
14421 struct glyph *glyph = row->glyphs[area] + i;
14422 if (glyph->type == CHAR_GLYPH
14423 && glyph->u.ch < 0x80
14424 && glyph->u.ch >= ' ')
14425 s[i] = glyph->u.ch;
14426 else
14427 s[i] = '.';
14428 }
14429
14430 s[i] = '\0';
14431 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14432 }
14433 }
14434 }
14435
14436
14437 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14438 Sdump_glyph_matrix, 0, 1, "p",
14439 doc: /* Dump the current matrix of the selected window to stderr.
14440 Shows contents of glyph row structures. With non-nil
14441 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14442 glyphs in short form, otherwise show glyphs in long form. */)
14443 (glyphs)
14444 Lisp_Object glyphs;
14445 {
14446 struct window *w = XWINDOW (selected_window);
14447 struct buffer *buffer = XBUFFER (w->buffer);
14448
14449 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14450 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14451 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14452 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14453 fprintf (stderr, "=============================================\n");
14454 dump_glyph_matrix (w->current_matrix,
14455 NILP (glyphs) ? 0 : XINT (glyphs));
14456 return Qnil;
14457 }
14458
14459
14460 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14461 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14462 ()
14463 {
14464 struct frame *f = XFRAME (selected_frame);
14465 dump_glyph_matrix (f->current_matrix, 1);
14466 return Qnil;
14467 }
14468
14469
14470 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14471 doc: /* Dump glyph row ROW to stderr.
14472 GLYPH 0 means don't dump glyphs.
14473 GLYPH 1 means dump glyphs in short form.
14474 GLYPH > 1 or omitted means dump glyphs in long form. */)
14475 (row, glyphs)
14476 Lisp_Object row, glyphs;
14477 {
14478 struct glyph_matrix *matrix;
14479 int vpos;
14480
14481 CHECK_NUMBER (row);
14482 matrix = XWINDOW (selected_window)->current_matrix;
14483 vpos = XINT (row);
14484 if (vpos >= 0 && vpos < matrix->nrows)
14485 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14486 vpos,
14487 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14488 return Qnil;
14489 }
14490
14491
14492 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14493 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14494 GLYPH 0 means don't dump glyphs.
14495 GLYPH 1 means dump glyphs in short form.
14496 GLYPH > 1 or omitted means dump glyphs in long form. */)
14497 (row, glyphs)
14498 Lisp_Object row, glyphs;
14499 {
14500 struct frame *sf = SELECTED_FRAME ();
14501 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14502 int vpos;
14503
14504 CHECK_NUMBER (row);
14505 vpos = XINT (row);
14506 if (vpos >= 0 && vpos < m->nrows)
14507 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14508 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14509 return Qnil;
14510 }
14511
14512
14513 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14514 doc: /* Toggle tracing of redisplay.
14515 With ARG, turn tracing on if and only if ARG is positive. */)
14516 (arg)
14517 Lisp_Object arg;
14518 {
14519 if (NILP (arg))
14520 trace_redisplay_p = !trace_redisplay_p;
14521 else
14522 {
14523 arg = Fprefix_numeric_value (arg);
14524 trace_redisplay_p = XINT (arg) > 0;
14525 }
14526
14527 return Qnil;
14528 }
14529
14530
14531 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14532 doc: /* Like `format', but print result to stderr.
14533 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14534 (nargs, args)
14535 int nargs;
14536 Lisp_Object *args;
14537 {
14538 Lisp_Object s = Fformat (nargs, args);
14539 fprintf (stderr, "%s", SDATA (s));
14540 return Qnil;
14541 }
14542
14543 #endif /* GLYPH_DEBUG */
14544
14545
14546 \f
14547 /***********************************************************************
14548 Building Desired Matrix Rows
14549 ***********************************************************************/
14550
14551 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14552 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14553
14554 static struct glyph_row *
14555 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14556 struct window *w;
14557 Lisp_Object overlay_arrow_string;
14558 {
14559 struct frame *f = XFRAME (WINDOW_FRAME (w));
14560 struct buffer *buffer = XBUFFER (w->buffer);
14561 struct buffer *old = current_buffer;
14562 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14563 int arrow_len = SCHARS (overlay_arrow_string);
14564 const unsigned char *arrow_end = arrow_string + arrow_len;
14565 const unsigned char *p;
14566 struct it it;
14567 int multibyte_p;
14568 int n_glyphs_before;
14569
14570 set_buffer_temp (buffer);
14571 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14572 it.glyph_row->used[TEXT_AREA] = 0;
14573 SET_TEXT_POS (it.position, 0, 0);
14574
14575 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14576 p = arrow_string;
14577 while (p < arrow_end)
14578 {
14579 Lisp_Object face, ilisp;
14580
14581 /* Get the next character. */
14582 if (multibyte_p)
14583 it.c = string_char_and_length (p, arrow_len, &it.len);
14584 else
14585 it.c = *p, it.len = 1;
14586 p += it.len;
14587
14588 /* Get its face. */
14589 ilisp = make_number (p - arrow_string);
14590 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14591 it.face_id = compute_char_face (f, it.c, face);
14592
14593 /* Compute its width, get its glyphs. */
14594 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14595 SET_TEXT_POS (it.position, -1, -1);
14596 PRODUCE_GLYPHS (&it);
14597
14598 /* If this character doesn't fit any more in the line, we have
14599 to remove some glyphs. */
14600 if (it.current_x > it.last_visible_x)
14601 {
14602 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14603 break;
14604 }
14605 }
14606
14607 set_buffer_temp (old);
14608 return it.glyph_row;
14609 }
14610
14611
14612 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14613 glyphs are only inserted for terminal frames since we can't really
14614 win with truncation glyphs when partially visible glyphs are
14615 involved. Which glyphs to insert is determined by
14616 produce_special_glyphs. */
14617
14618 static void
14619 insert_left_trunc_glyphs (it)
14620 struct it *it;
14621 {
14622 struct it truncate_it;
14623 struct glyph *from, *end, *to, *toend;
14624
14625 xassert (!FRAME_WINDOW_P (it->f));
14626
14627 /* Get the truncation glyphs. */
14628 truncate_it = *it;
14629 truncate_it.current_x = 0;
14630 truncate_it.face_id = DEFAULT_FACE_ID;
14631 truncate_it.glyph_row = &scratch_glyph_row;
14632 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14633 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14634 truncate_it.object = make_number (0);
14635 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14636
14637 /* Overwrite glyphs from IT with truncation glyphs. */
14638 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14639 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14640 to = it->glyph_row->glyphs[TEXT_AREA];
14641 toend = to + it->glyph_row->used[TEXT_AREA];
14642
14643 while (from < end)
14644 *to++ = *from++;
14645
14646 /* There may be padding glyphs left over. Overwrite them too. */
14647 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14648 {
14649 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14650 while (from < end)
14651 *to++ = *from++;
14652 }
14653
14654 if (to > toend)
14655 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14656 }
14657
14658
14659 /* Compute the pixel height and width of IT->glyph_row.
14660
14661 Most of the time, ascent and height of a display line will be equal
14662 to the max_ascent and max_height values of the display iterator
14663 structure. This is not the case if
14664
14665 1. We hit ZV without displaying anything. In this case, max_ascent
14666 and max_height will be zero.
14667
14668 2. We have some glyphs that don't contribute to the line height.
14669 (The glyph row flag contributes_to_line_height_p is for future
14670 pixmap extensions).
14671
14672 The first case is easily covered by using default values because in
14673 these cases, the line height does not really matter, except that it
14674 must not be zero. */
14675
14676 static void
14677 compute_line_metrics (it)
14678 struct it *it;
14679 {
14680 struct glyph_row *row = it->glyph_row;
14681 int area, i;
14682
14683 if (FRAME_WINDOW_P (it->f))
14684 {
14685 int i, min_y, max_y;
14686
14687 /* The line may consist of one space only, that was added to
14688 place the cursor on it. If so, the row's height hasn't been
14689 computed yet. */
14690 if (row->height == 0)
14691 {
14692 if (it->max_ascent + it->max_descent == 0)
14693 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14694 row->ascent = it->max_ascent;
14695 row->height = it->max_ascent + it->max_descent;
14696 row->phys_ascent = it->max_phys_ascent;
14697 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14698 row->extra_line_spacing = it->max_extra_line_spacing;
14699 }
14700
14701 /* Compute the width of this line. */
14702 row->pixel_width = row->x;
14703 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14704 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14705
14706 xassert (row->pixel_width >= 0);
14707 xassert (row->ascent >= 0 && row->height > 0);
14708
14709 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14710 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14711
14712 /* If first line's physical ascent is larger than its logical
14713 ascent, use the physical ascent, and make the row taller.
14714 This makes accented characters fully visible. */
14715 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14716 && row->phys_ascent > row->ascent)
14717 {
14718 row->height += row->phys_ascent - row->ascent;
14719 row->ascent = row->phys_ascent;
14720 }
14721
14722 /* Compute how much of the line is visible. */
14723 row->visible_height = row->height;
14724
14725 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14726 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14727
14728 if (row->y < min_y)
14729 row->visible_height -= min_y - row->y;
14730 if (row->y + row->height > max_y)
14731 row->visible_height -= row->y + row->height - max_y;
14732 }
14733 else
14734 {
14735 row->pixel_width = row->used[TEXT_AREA];
14736 if (row->continued_p)
14737 row->pixel_width -= it->continuation_pixel_width;
14738 else if (row->truncated_on_right_p)
14739 row->pixel_width -= it->truncation_pixel_width;
14740 row->ascent = row->phys_ascent = 0;
14741 row->height = row->phys_height = row->visible_height = 1;
14742 row->extra_line_spacing = 0;
14743 }
14744
14745 /* Compute a hash code for this row. */
14746 row->hash = 0;
14747 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14748 for (i = 0; i < row->used[area]; ++i)
14749 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14750 + row->glyphs[area][i].u.val
14751 + row->glyphs[area][i].face_id
14752 + row->glyphs[area][i].padding_p
14753 + (row->glyphs[area][i].type << 2));
14754
14755 it->max_ascent = it->max_descent = 0;
14756 it->max_phys_ascent = it->max_phys_descent = 0;
14757 }
14758
14759
14760 /* Append one space to the glyph row of iterator IT if doing a
14761 window-based redisplay. The space has the same face as
14762 IT->face_id. Value is non-zero if a space was added.
14763
14764 This function is called to make sure that there is always one glyph
14765 at the end of a glyph row that the cursor can be set on under
14766 window-systems. (If there weren't such a glyph we would not know
14767 how wide and tall a box cursor should be displayed).
14768
14769 At the same time this space let's a nicely handle clearing to the
14770 end of the line if the row ends in italic text. */
14771
14772 static int
14773 append_space_for_newline (it, default_face_p)
14774 struct it *it;
14775 int default_face_p;
14776 {
14777 if (FRAME_WINDOW_P (it->f))
14778 {
14779 int n = it->glyph_row->used[TEXT_AREA];
14780
14781 if (it->glyph_row->glyphs[TEXT_AREA] + n
14782 < it->glyph_row->glyphs[1 + TEXT_AREA])
14783 {
14784 /* Save some values that must not be changed.
14785 Must save IT->c and IT->len because otherwise
14786 ITERATOR_AT_END_P wouldn't work anymore after
14787 append_space_for_newline has been called. */
14788 enum display_element_type saved_what = it->what;
14789 int saved_c = it->c, saved_len = it->len;
14790 int saved_x = it->current_x;
14791 int saved_face_id = it->face_id;
14792 struct text_pos saved_pos;
14793 Lisp_Object saved_object;
14794 struct face *face;
14795
14796 saved_object = it->object;
14797 saved_pos = it->position;
14798
14799 it->what = IT_CHARACTER;
14800 bzero (&it->position, sizeof it->position);
14801 it->object = make_number (0);
14802 it->c = ' ';
14803 it->len = 1;
14804
14805 if (default_face_p)
14806 it->face_id = DEFAULT_FACE_ID;
14807 else if (it->face_before_selective_p)
14808 it->face_id = it->saved_face_id;
14809 face = FACE_FROM_ID (it->f, it->face_id);
14810 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14811
14812 PRODUCE_GLYPHS (it);
14813
14814 it->override_ascent = -1;
14815 it->constrain_row_ascent_descent_p = 0;
14816 it->current_x = saved_x;
14817 it->object = saved_object;
14818 it->position = saved_pos;
14819 it->what = saved_what;
14820 it->face_id = saved_face_id;
14821 it->len = saved_len;
14822 it->c = saved_c;
14823 return 1;
14824 }
14825 }
14826
14827 return 0;
14828 }
14829
14830
14831 /* Extend the face of the last glyph in the text area of IT->glyph_row
14832 to the end of the display line. Called from display_line.
14833 If the glyph row is empty, add a space glyph to it so that we
14834 know the face to draw. Set the glyph row flag fill_line_p. */
14835
14836 static void
14837 extend_face_to_end_of_line (it)
14838 struct it *it;
14839 {
14840 struct face *face;
14841 struct frame *f = it->f;
14842
14843 /* If line is already filled, do nothing. */
14844 if (it->current_x >= it->last_visible_x)
14845 return;
14846
14847 /* Face extension extends the background and box of IT->face_id
14848 to the end of the line. If the background equals the background
14849 of the frame, we don't have to do anything. */
14850 if (it->face_before_selective_p)
14851 face = FACE_FROM_ID (it->f, it->saved_face_id);
14852 else
14853 face = FACE_FROM_ID (f, it->face_id);
14854
14855 if (FRAME_WINDOW_P (f)
14856 && face->box == FACE_NO_BOX
14857 && face->background == FRAME_BACKGROUND_PIXEL (f)
14858 && !face->stipple)
14859 return;
14860
14861 /* Set the glyph row flag indicating that the face of the last glyph
14862 in the text area has to be drawn to the end of the text area. */
14863 it->glyph_row->fill_line_p = 1;
14864
14865 /* If current character of IT is not ASCII, make sure we have the
14866 ASCII face. This will be automatically undone the next time
14867 get_next_display_element returns a multibyte character. Note
14868 that the character will always be single byte in unibyte text. */
14869 if (!SINGLE_BYTE_CHAR_P (it->c))
14870 {
14871 it->face_id = FACE_FOR_CHAR (f, face, 0);
14872 }
14873
14874 if (FRAME_WINDOW_P (f))
14875 {
14876 /* If the row is empty, add a space with the current face of IT,
14877 so that we know which face to draw. */
14878 if (it->glyph_row->used[TEXT_AREA] == 0)
14879 {
14880 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14881 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14882 it->glyph_row->used[TEXT_AREA] = 1;
14883 }
14884 }
14885 else
14886 {
14887 /* Save some values that must not be changed. */
14888 int saved_x = it->current_x;
14889 struct text_pos saved_pos;
14890 Lisp_Object saved_object;
14891 enum display_element_type saved_what = it->what;
14892 int saved_face_id = it->face_id;
14893
14894 saved_object = it->object;
14895 saved_pos = it->position;
14896
14897 it->what = IT_CHARACTER;
14898 bzero (&it->position, sizeof it->position);
14899 it->object = make_number (0);
14900 it->c = ' ';
14901 it->len = 1;
14902 it->face_id = face->id;
14903
14904 PRODUCE_GLYPHS (it);
14905
14906 while (it->current_x <= it->last_visible_x)
14907 PRODUCE_GLYPHS (it);
14908
14909 /* Don't count these blanks really. It would let us insert a left
14910 truncation glyph below and make us set the cursor on them, maybe. */
14911 it->current_x = saved_x;
14912 it->object = saved_object;
14913 it->position = saved_pos;
14914 it->what = saved_what;
14915 it->face_id = saved_face_id;
14916 }
14917 }
14918
14919
14920 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14921 trailing whitespace. */
14922
14923 static int
14924 trailing_whitespace_p (charpos)
14925 int charpos;
14926 {
14927 int bytepos = CHAR_TO_BYTE (charpos);
14928 int c = 0;
14929
14930 while (bytepos < ZV_BYTE
14931 && (c = FETCH_CHAR (bytepos),
14932 c == ' ' || c == '\t'))
14933 ++bytepos;
14934
14935 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14936 {
14937 if (bytepos != PT_BYTE)
14938 return 1;
14939 }
14940 return 0;
14941 }
14942
14943
14944 /* Highlight trailing whitespace, if any, in ROW. */
14945
14946 void
14947 highlight_trailing_whitespace (f, row)
14948 struct frame *f;
14949 struct glyph_row *row;
14950 {
14951 int used = row->used[TEXT_AREA];
14952
14953 if (used)
14954 {
14955 struct glyph *start = row->glyphs[TEXT_AREA];
14956 struct glyph *glyph = start + used - 1;
14957
14958 /* Skip over glyphs inserted to display the cursor at the
14959 end of a line, for extending the face of the last glyph
14960 to the end of the line on terminals, and for truncation
14961 and continuation glyphs. */
14962 while (glyph >= start
14963 && glyph->type == CHAR_GLYPH
14964 && INTEGERP (glyph->object))
14965 --glyph;
14966
14967 /* If last glyph is a space or stretch, and it's trailing
14968 whitespace, set the face of all trailing whitespace glyphs in
14969 IT->glyph_row to `trailing-whitespace'. */
14970 if (glyph >= start
14971 && BUFFERP (glyph->object)
14972 && (glyph->type == STRETCH_GLYPH
14973 || (glyph->type == CHAR_GLYPH
14974 && glyph->u.ch == ' '))
14975 && trailing_whitespace_p (glyph->charpos))
14976 {
14977 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14978 if (face_id < 0)
14979 return;
14980
14981 while (glyph >= start
14982 && BUFFERP (glyph->object)
14983 && (glyph->type == STRETCH_GLYPH
14984 || (glyph->type == CHAR_GLYPH
14985 && glyph->u.ch == ' ')))
14986 (glyph--)->face_id = face_id;
14987 }
14988 }
14989 }
14990
14991
14992 /* Value is non-zero if glyph row ROW in window W should be
14993 used to hold the cursor. */
14994
14995 static int
14996 cursor_row_p (w, row)
14997 struct window *w;
14998 struct glyph_row *row;
14999 {
15000 int cursor_row_p = 1;
15001
15002 if (PT == MATRIX_ROW_END_CHARPOS (row))
15003 {
15004 /* If the row ends with a newline from a string, we don't want
15005 the cursor there (if the row is continued it doesn't end in a
15006 newline). */
15007 if (CHARPOS (row->end.string_pos) >= 0)
15008 cursor_row_p = row->continued_p;
15009 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15010 {
15011 /* If the row ends in middle of a real character,
15012 and the line is continued, we want the cursor here.
15013 That's because MATRIX_ROW_END_CHARPOS would equal
15014 PT if PT is before the character. */
15015 if (!row->ends_in_ellipsis_p)
15016 cursor_row_p = row->continued_p;
15017 else
15018 /* If the row ends in an ellipsis, then
15019 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15020 We want that position to be displayed after the ellipsis. */
15021 cursor_row_p = 0;
15022 }
15023 /* If the row ends at ZV, display the cursor at the end of that
15024 row instead of at the start of the row below. */
15025 else if (row->ends_at_zv_p)
15026 cursor_row_p = 1;
15027 else
15028 cursor_row_p = 0;
15029 }
15030
15031 return cursor_row_p;
15032 }
15033
15034
15035 /* Construct the glyph row IT->glyph_row in the desired matrix of
15036 IT->w from text at the current position of IT. See dispextern.h
15037 for an overview of struct it. Value is non-zero if
15038 IT->glyph_row displays text, as opposed to a line displaying ZV
15039 only. */
15040
15041 static int
15042 display_line (it)
15043 struct it *it;
15044 {
15045 struct glyph_row *row = it->glyph_row;
15046 Lisp_Object overlay_arrow_string;
15047
15048 /* We always start displaying at hpos zero even if hscrolled. */
15049 xassert (it->hpos == 0 && it->current_x == 0);
15050
15051 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15052 >= it->w->desired_matrix->nrows)
15053 {
15054 it->w->nrows_scale_factor++;
15055 fonts_changed_p = 1;
15056 return 0;
15057 }
15058
15059 /* Is IT->w showing the region? */
15060 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15061
15062 /* Clear the result glyph row and enable it. */
15063 prepare_desired_row (row);
15064
15065 row->y = it->current_y;
15066 row->start = it->start;
15067 row->continuation_lines_width = it->continuation_lines_width;
15068 row->displays_text_p = 1;
15069 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15070 it->starts_in_middle_of_char_p = 0;
15071
15072 /* Arrange the overlays nicely for our purposes. Usually, we call
15073 display_line on only one line at a time, in which case this
15074 can't really hurt too much, or we call it on lines which appear
15075 one after another in the buffer, in which case all calls to
15076 recenter_overlay_lists but the first will be pretty cheap. */
15077 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15078
15079 /* Move over display elements that are not visible because we are
15080 hscrolled. This may stop at an x-position < IT->first_visible_x
15081 if the first glyph is partially visible or if we hit a line end. */
15082 if (it->current_x < it->first_visible_x)
15083 {
15084 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15085 MOVE_TO_POS | MOVE_TO_X);
15086 }
15087
15088 /* Get the initial row height. This is either the height of the
15089 text hscrolled, if there is any, or zero. */
15090 row->ascent = it->max_ascent;
15091 row->height = it->max_ascent + it->max_descent;
15092 row->phys_ascent = it->max_phys_ascent;
15093 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15094 row->extra_line_spacing = it->max_extra_line_spacing;
15095
15096 /* Loop generating characters. The loop is left with IT on the next
15097 character to display. */
15098 while (1)
15099 {
15100 int n_glyphs_before, hpos_before, x_before;
15101 int x, i, nglyphs;
15102 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15103
15104 /* Retrieve the next thing to display. Value is zero if end of
15105 buffer reached. */
15106 if (!get_next_display_element (it))
15107 {
15108 /* Maybe add a space at the end of this line that is used to
15109 display the cursor there under X. Set the charpos of the
15110 first glyph of blank lines not corresponding to any text
15111 to -1. */
15112 #ifdef HAVE_WINDOW_SYSTEM
15113 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15114 row->exact_window_width_line_p = 1;
15115 else
15116 #endif /* HAVE_WINDOW_SYSTEM */
15117 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15118 || row->used[TEXT_AREA] == 0)
15119 {
15120 row->glyphs[TEXT_AREA]->charpos = -1;
15121 row->displays_text_p = 0;
15122
15123 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15124 && (!MINI_WINDOW_P (it->w)
15125 || (minibuf_level && EQ (it->window, minibuf_window))))
15126 row->indicate_empty_line_p = 1;
15127 }
15128
15129 it->continuation_lines_width = 0;
15130 row->ends_at_zv_p = 1;
15131 break;
15132 }
15133
15134 /* Now, get the metrics of what we want to display. This also
15135 generates glyphs in `row' (which is IT->glyph_row). */
15136 n_glyphs_before = row->used[TEXT_AREA];
15137 x = it->current_x;
15138
15139 /* Remember the line height so far in case the next element doesn't
15140 fit on the line. */
15141 if (!it->truncate_lines_p)
15142 {
15143 ascent = it->max_ascent;
15144 descent = it->max_descent;
15145 phys_ascent = it->max_phys_ascent;
15146 phys_descent = it->max_phys_descent;
15147 }
15148
15149 PRODUCE_GLYPHS (it);
15150
15151 /* If this display element was in marginal areas, continue with
15152 the next one. */
15153 if (it->area != TEXT_AREA)
15154 {
15155 row->ascent = max (row->ascent, it->max_ascent);
15156 row->height = max (row->height, it->max_ascent + it->max_descent);
15157 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15158 row->phys_height = max (row->phys_height,
15159 it->max_phys_ascent + it->max_phys_descent);
15160 row->extra_line_spacing = max (row->extra_line_spacing,
15161 it->max_extra_line_spacing);
15162 set_iterator_to_next (it, 1);
15163 continue;
15164 }
15165
15166 /* Does the display element fit on the line? If we truncate
15167 lines, we should draw past the right edge of the window. If
15168 we don't truncate, we want to stop so that we can display the
15169 continuation glyph before the right margin. If lines are
15170 continued, there are two possible strategies for characters
15171 resulting in more than 1 glyph (e.g. tabs): Display as many
15172 glyphs as possible in this line and leave the rest for the
15173 continuation line, or display the whole element in the next
15174 line. Original redisplay did the former, so we do it also. */
15175 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15176 hpos_before = it->hpos;
15177 x_before = x;
15178
15179 if (/* Not a newline. */
15180 nglyphs > 0
15181 /* Glyphs produced fit entirely in the line. */
15182 && it->current_x < it->last_visible_x)
15183 {
15184 it->hpos += nglyphs;
15185 row->ascent = max (row->ascent, it->max_ascent);
15186 row->height = max (row->height, it->max_ascent + it->max_descent);
15187 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15188 row->phys_height = max (row->phys_height,
15189 it->max_phys_ascent + it->max_phys_descent);
15190 row->extra_line_spacing = max (row->extra_line_spacing,
15191 it->max_extra_line_spacing);
15192 if (it->current_x - it->pixel_width < it->first_visible_x)
15193 row->x = x - it->first_visible_x;
15194 }
15195 else
15196 {
15197 int new_x;
15198 struct glyph *glyph;
15199
15200 for (i = 0; i < nglyphs; ++i, x = new_x)
15201 {
15202 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15203 new_x = x + glyph->pixel_width;
15204
15205 if (/* Lines are continued. */
15206 !it->truncate_lines_p
15207 && (/* Glyph doesn't fit on the line. */
15208 new_x > it->last_visible_x
15209 /* Or it fits exactly on a window system frame. */
15210 || (new_x == it->last_visible_x
15211 && FRAME_WINDOW_P (it->f))))
15212 {
15213 /* End of a continued line. */
15214
15215 if (it->hpos == 0
15216 || (new_x == it->last_visible_x
15217 && FRAME_WINDOW_P (it->f)))
15218 {
15219 /* Current glyph is the only one on the line or
15220 fits exactly on the line. We must continue
15221 the line because we can't draw the cursor
15222 after the glyph. */
15223 row->continued_p = 1;
15224 it->current_x = new_x;
15225 it->continuation_lines_width += new_x;
15226 ++it->hpos;
15227 if (i == nglyphs - 1)
15228 {
15229 set_iterator_to_next (it, 1);
15230 #ifdef HAVE_WINDOW_SYSTEM
15231 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15232 {
15233 if (!get_next_display_element (it))
15234 {
15235 row->exact_window_width_line_p = 1;
15236 it->continuation_lines_width = 0;
15237 row->continued_p = 0;
15238 row->ends_at_zv_p = 1;
15239 }
15240 else if (ITERATOR_AT_END_OF_LINE_P (it))
15241 {
15242 row->continued_p = 0;
15243 row->exact_window_width_line_p = 1;
15244 }
15245 }
15246 #endif /* HAVE_WINDOW_SYSTEM */
15247 }
15248 }
15249 else if (CHAR_GLYPH_PADDING_P (*glyph)
15250 && !FRAME_WINDOW_P (it->f))
15251 {
15252 /* A padding glyph that doesn't fit on this line.
15253 This means the whole character doesn't fit
15254 on the line. */
15255 row->used[TEXT_AREA] = n_glyphs_before;
15256
15257 /* Fill the rest of the row with continuation
15258 glyphs like in 20.x. */
15259 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15260 < row->glyphs[1 + TEXT_AREA])
15261 produce_special_glyphs (it, IT_CONTINUATION);
15262
15263 row->continued_p = 1;
15264 it->current_x = x_before;
15265 it->continuation_lines_width += x_before;
15266
15267 /* Restore the height to what it was before the
15268 element not fitting on the line. */
15269 it->max_ascent = ascent;
15270 it->max_descent = descent;
15271 it->max_phys_ascent = phys_ascent;
15272 it->max_phys_descent = phys_descent;
15273 }
15274 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15275 {
15276 /* A TAB that extends past the right edge of the
15277 window. This produces a single glyph on
15278 window system frames. We leave the glyph in
15279 this row and let it fill the row, but don't
15280 consume the TAB. */
15281 it->continuation_lines_width += it->last_visible_x;
15282 row->ends_in_middle_of_char_p = 1;
15283 row->continued_p = 1;
15284 glyph->pixel_width = it->last_visible_x - x;
15285 it->starts_in_middle_of_char_p = 1;
15286 }
15287 else
15288 {
15289 /* Something other than a TAB that draws past
15290 the right edge of the window. Restore
15291 positions to values before the element. */
15292 row->used[TEXT_AREA] = n_glyphs_before + i;
15293
15294 /* Display continuation glyphs. */
15295 if (!FRAME_WINDOW_P (it->f))
15296 produce_special_glyphs (it, IT_CONTINUATION);
15297 row->continued_p = 1;
15298
15299 it->continuation_lines_width += x;
15300
15301 if (nglyphs > 1 && i > 0)
15302 {
15303 row->ends_in_middle_of_char_p = 1;
15304 it->starts_in_middle_of_char_p = 1;
15305 }
15306
15307 /* Restore the height to what it was before the
15308 element not fitting on the line. */
15309 it->max_ascent = ascent;
15310 it->max_descent = descent;
15311 it->max_phys_ascent = phys_ascent;
15312 it->max_phys_descent = phys_descent;
15313 }
15314
15315 break;
15316 }
15317 else if (new_x > it->first_visible_x)
15318 {
15319 /* Increment number of glyphs actually displayed. */
15320 ++it->hpos;
15321
15322 if (x < it->first_visible_x)
15323 /* Glyph is partially visible, i.e. row starts at
15324 negative X position. */
15325 row->x = x - it->first_visible_x;
15326 }
15327 else
15328 {
15329 /* Glyph is completely off the left margin of the
15330 window. This should not happen because of the
15331 move_it_in_display_line at the start of this
15332 function, unless the text display area of the
15333 window is empty. */
15334 xassert (it->first_visible_x <= it->last_visible_x);
15335 }
15336 }
15337
15338 row->ascent = max (row->ascent, it->max_ascent);
15339 row->height = max (row->height, it->max_ascent + it->max_descent);
15340 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15341 row->phys_height = max (row->phys_height,
15342 it->max_phys_ascent + it->max_phys_descent);
15343 row->extra_line_spacing = max (row->extra_line_spacing,
15344 it->max_extra_line_spacing);
15345
15346 /* End of this display line if row is continued. */
15347 if (row->continued_p || row->ends_at_zv_p)
15348 break;
15349 }
15350
15351 at_end_of_line:
15352 /* Is this a line end? If yes, we're also done, after making
15353 sure that a non-default face is extended up to the right
15354 margin of the window. */
15355 if (ITERATOR_AT_END_OF_LINE_P (it))
15356 {
15357 int used_before = row->used[TEXT_AREA];
15358
15359 row->ends_in_newline_from_string_p = STRINGP (it->object);
15360
15361 #ifdef HAVE_WINDOW_SYSTEM
15362 /* Add a space at the end of the line that is used to
15363 display the cursor there. */
15364 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15365 append_space_for_newline (it, 0);
15366 #endif /* HAVE_WINDOW_SYSTEM */
15367
15368 /* Extend the face to the end of the line. */
15369 extend_face_to_end_of_line (it);
15370
15371 /* Make sure we have the position. */
15372 if (used_before == 0)
15373 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15374
15375 /* Consume the line end. This skips over invisible lines. */
15376 set_iterator_to_next (it, 1);
15377 it->continuation_lines_width = 0;
15378 break;
15379 }
15380
15381 /* Proceed with next display element. Note that this skips
15382 over lines invisible because of selective display. */
15383 set_iterator_to_next (it, 1);
15384
15385 /* If we truncate lines, we are done when the last displayed
15386 glyphs reach past the right margin of the window. */
15387 if (it->truncate_lines_p
15388 && (FRAME_WINDOW_P (it->f)
15389 ? (it->current_x >= it->last_visible_x)
15390 : (it->current_x > it->last_visible_x)))
15391 {
15392 /* Maybe add truncation glyphs. */
15393 if (!FRAME_WINDOW_P (it->f))
15394 {
15395 int i, n;
15396
15397 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15398 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15399 break;
15400
15401 for (n = row->used[TEXT_AREA]; i < n; ++i)
15402 {
15403 row->used[TEXT_AREA] = i;
15404 produce_special_glyphs (it, IT_TRUNCATION);
15405 }
15406 }
15407 #ifdef HAVE_WINDOW_SYSTEM
15408 else
15409 {
15410 /* Don't truncate if we can overflow newline into fringe. */
15411 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15412 {
15413 if (!get_next_display_element (it))
15414 {
15415 it->continuation_lines_width = 0;
15416 row->ends_at_zv_p = 1;
15417 row->exact_window_width_line_p = 1;
15418 break;
15419 }
15420 if (ITERATOR_AT_END_OF_LINE_P (it))
15421 {
15422 row->exact_window_width_line_p = 1;
15423 goto at_end_of_line;
15424 }
15425 }
15426 }
15427 #endif /* HAVE_WINDOW_SYSTEM */
15428
15429 row->truncated_on_right_p = 1;
15430 it->continuation_lines_width = 0;
15431 reseat_at_next_visible_line_start (it, 0);
15432 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15433 it->hpos = hpos_before;
15434 it->current_x = x_before;
15435 break;
15436 }
15437 }
15438
15439 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15440 at the left window margin. */
15441 if (it->first_visible_x
15442 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15443 {
15444 if (!FRAME_WINDOW_P (it->f))
15445 insert_left_trunc_glyphs (it);
15446 row->truncated_on_left_p = 1;
15447 }
15448
15449 /* If the start of this line is the overlay arrow-position, then
15450 mark this glyph row as the one containing the overlay arrow.
15451 This is clearly a mess with variable size fonts. It would be
15452 better to let it be displayed like cursors under X. */
15453 if ((row->displays_text_p || !overlay_arrow_seen)
15454 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15455 !NILP (overlay_arrow_string)))
15456 {
15457 /* Overlay arrow in window redisplay is a fringe bitmap. */
15458 if (STRINGP (overlay_arrow_string))
15459 {
15460 struct glyph_row *arrow_row
15461 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15462 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15463 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15464 struct glyph *p = row->glyphs[TEXT_AREA];
15465 struct glyph *p2, *end;
15466
15467 /* Copy the arrow glyphs. */
15468 while (glyph < arrow_end)
15469 *p++ = *glyph++;
15470
15471 /* Throw away padding glyphs. */
15472 p2 = p;
15473 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15474 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15475 ++p2;
15476 if (p2 > p)
15477 {
15478 while (p2 < end)
15479 *p++ = *p2++;
15480 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15481 }
15482 }
15483 else
15484 {
15485 xassert (INTEGERP (overlay_arrow_string));
15486 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15487 }
15488 overlay_arrow_seen = 1;
15489 }
15490
15491 /* Compute pixel dimensions of this line. */
15492 compute_line_metrics (it);
15493
15494 /* Remember the position at which this line ends. */
15495 row->end = it->current;
15496
15497 /* Record whether this row ends inside an ellipsis. */
15498 row->ends_in_ellipsis_p
15499 = (it->method == GET_FROM_DISPLAY_VECTOR
15500 && it->ellipsis_p);
15501
15502 /* Save fringe bitmaps in this row. */
15503 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15504 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15505 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15506 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15507
15508 it->left_user_fringe_bitmap = 0;
15509 it->left_user_fringe_face_id = 0;
15510 it->right_user_fringe_bitmap = 0;
15511 it->right_user_fringe_face_id = 0;
15512
15513 /* Maybe set the cursor. */
15514 if (it->w->cursor.vpos < 0
15515 && PT >= MATRIX_ROW_START_CHARPOS (row)
15516 && PT <= MATRIX_ROW_END_CHARPOS (row)
15517 && cursor_row_p (it->w, row))
15518 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15519
15520 /* Highlight trailing whitespace. */
15521 if (!NILP (Vshow_trailing_whitespace))
15522 highlight_trailing_whitespace (it->f, it->glyph_row);
15523
15524 /* Prepare for the next line. This line starts horizontally at (X
15525 HPOS) = (0 0). Vertical positions are incremented. As a
15526 convenience for the caller, IT->glyph_row is set to the next
15527 row to be used. */
15528 it->current_x = it->hpos = 0;
15529 it->current_y += row->height;
15530 ++it->vpos;
15531 ++it->glyph_row;
15532 it->start = it->current;
15533 return row->displays_text_p;
15534 }
15535
15536
15537 \f
15538 /***********************************************************************
15539 Menu Bar
15540 ***********************************************************************/
15541
15542 /* Redisplay the menu bar in the frame for window W.
15543
15544 The menu bar of X frames that don't have X toolkit support is
15545 displayed in a special window W->frame->menu_bar_window.
15546
15547 The menu bar of terminal frames is treated specially as far as
15548 glyph matrices are concerned. Menu bar lines are not part of
15549 windows, so the update is done directly on the frame matrix rows
15550 for the menu bar. */
15551
15552 static void
15553 display_menu_bar (w)
15554 struct window *w;
15555 {
15556 struct frame *f = XFRAME (WINDOW_FRAME (w));
15557 struct it it;
15558 Lisp_Object items;
15559 int i;
15560
15561 /* Don't do all this for graphical frames. */
15562 #ifdef HAVE_NTGUI
15563 if (!NILP (Vwindow_system))
15564 return;
15565 #endif
15566 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15567 if (FRAME_X_P (f))
15568 return;
15569 #endif
15570 #ifdef MAC_OS
15571 if (FRAME_MAC_P (f))
15572 return;
15573 #endif
15574
15575 #ifdef USE_X_TOOLKIT
15576 xassert (!FRAME_WINDOW_P (f));
15577 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15578 it.first_visible_x = 0;
15579 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15580 #else /* not USE_X_TOOLKIT */
15581 if (FRAME_WINDOW_P (f))
15582 {
15583 /* Menu bar lines are displayed in the desired matrix of the
15584 dummy window menu_bar_window. */
15585 struct window *menu_w;
15586 xassert (WINDOWP (f->menu_bar_window));
15587 menu_w = XWINDOW (f->menu_bar_window);
15588 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15589 MENU_FACE_ID);
15590 it.first_visible_x = 0;
15591 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15592 }
15593 else
15594 {
15595 /* This is a TTY frame, i.e. character hpos/vpos are used as
15596 pixel x/y. */
15597 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15598 MENU_FACE_ID);
15599 it.first_visible_x = 0;
15600 it.last_visible_x = FRAME_COLS (f);
15601 }
15602 #endif /* not USE_X_TOOLKIT */
15603
15604 if (! mode_line_inverse_video)
15605 /* Force the menu-bar to be displayed in the default face. */
15606 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15607
15608 /* Clear all rows of the menu bar. */
15609 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15610 {
15611 struct glyph_row *row = it.glyph_row + i;
15612 clear_glyph_row (row);
15613 row->enabled_p = 1;
15614 row->full_width_p = 1;
15615 }
15616
15617 /* Display all items of the menu bar. */
15618 items = FRAME_MENU_BAR_ITEMS (it.f);
15619 for (i = 0; i < XVECTOR (items)->size; i += 4)
15620 {
15621 Lisp_Object string;
15622
15623 /* Stop at nil string. */
15624 string = AREF (items, i + 1);
15625 if (NILP (string))
15626 break;
15627
15628 /* Remember where item was displayed. */
15629 AREF (items, i + 3) = make_number (it.hpos);
15630
15631 /* Display the item, pad with one space. */
15632 if (it.current_x < it.last_visible_x)
15633 display_string (NULL, string, Qnil, 0, 0, &it,
15634 SCHARS (string) + 1, 0, 0, -1);
15635 }
15636
15637 /* Fill out the line with spaces. */
15638 if (it.current_x < it.last_visible_x)
15639 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15640
15641 /* Compute the total height of the lines. */
15642 compute_line_metrics (&it);
15643 }
15644
15645
15646 \f
15647 /***********************************************************************
15648 Mode Line
15649 ***********************************************************************/
15650
15651 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15652 FORCE is non-zero, redisplay mode lines unconditionally.
15653 Otherwise, redisplay only mode lines that are garbaged. Value is
15654 the number of windows whose mode lines were redisplayed. */
15655
15656 static int
15657 redisplay_mode_lines (window, force)
15658 Lisp_Object window;
15659 int force;
15660 {
15661 int nwindows = 0;
15662
15663 while (!NILP (window))
15664 {
15665 struct window *w = XWINDOW (window);
15666
15667 if (WINDOWP (w->hchild))
15668 nwindows += redisplay_mode_lines (w->hchild, force);
15669 else if (WINDOWP (w->vchild))
15670 nwindows += redisplay_mode_lines (w->vchild, force);
15671 else if (force
15672 || FRAME_GARBAGED_P (XFRAME (w->frame))
15673 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15674 {
15675 struct text_pos lpoint;
15676 struct buffer *old = current_buffer;
15677
15678 /* Set the window's buffer for the mode line display. */
15679 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15680 set_buffer_internal_1 (XBUFFER (w->buffer));
15681
15682 /* Point refers normally to the selected window. For any
15683 other window, set up appropriate value. */
15684 if (!EQ (window, selected_window))
15685 {
15686 struct text_pos pt;
15687
15688 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15689 if (CHARPOS (pt) < BEGV)
15690 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15691 else if (CHARPOS (pt) > (ZV - 1))
15692 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15693 else
15694 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15695 }
15696
15697 /* Display mode lines. */
15698 clear_glyph_matrix (w->desired_matrix);
15699 if (display_mode_lines (w))
15700 {
15701 ++nwindows;
15702 w->must_be_updated_p = 1;
15703 }
15704
15705 /* Restore old settings. */
15706 set_buffer_internal_1 (old);
15707 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15708 }
15709
15710 window = w->next;
15711 }
15712
15713 return nwindows;
15714 }
15715
15716
15717 /* Display the mode and/or top line of window W. Value is the number
15718 of mode lines displayed. */
15719
15720 static int
15721 display_mode_lines (w)
15722 struct window *w;
15723 {
15724 Lisp_Object old_selected_window, old_selected_frame;
15725 int n = 0;
15726
15727 old_selected_frame = selected_frame;
15728 selected_frame = w->frame;
15729 old_selected_window = selected_window;
15730 XSETWINDOW (selected_window, w);
15731
15732 /* These will be set while the mode line specs are processed. */
15733 line_number_displayed = 0;
15734 w->column_number_displayed = Qnil;
15735
15736 if (WINDOW_WANTS_MODELINE_P (w))
15737 {
15738 struct window *sel_w = XWINDOW (old_selected_window);
15739
15740 /* Select mode line face based on the real selected window. */
15741 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15742 current_buffer->mode_line_format);
15743 ++n;
15744 }
15745
15746 if (WINDOW_WANTS_HEADER_LINE_P (w))
15747 {
15748 display_mode_line (w, HEADER_LINE_FACE_ID,
15749 current_buffer->header_line_format);
15750 ++n;
15751 }
15752
15753 selected_frame = old_selected_frame;
15754 selected_window = old_selected_window;
15755 return n;
15756 }
15757
15758
15759 /* Display mode or top line of window W. FACE_ID specifies which line
15760 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15761 FORMAT is the mode line format to display. Value is the pixel
15762 height of the mode line displayed. */
15763
15764 static int
15765 display_mode_line (w, face_id, format)
15766 struct window *w;
15767 enum face_id face_id;
15768 Lisp_Object format;
15769 {
15770 struct it it;
15771 struct face *face;
15772 int count = SPECPDL_INDEX ();
15773
15774 init_iterator (&it, w, -1, -1, NULL, face_id);
15775 prepare_desired_row (it.glyph_row);
15776
15777 it.glyph_row->mode_line_p = 1;
15778
15779 if (! mode_line_inverse_video)
15780 /* Force the mode-line to be displayed in the default face. */
15781 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15782
15783 record_unwind_protect (unwind_format_mode_line,
15784 format_mode_line_unwind_data (NULL));
15785
15786 mode_line_target = MODE_LINE_DISPLAY;
15787
15788 /* Temporarily make frame's keyboard the current kboard so that
15789 kboard-local variables in the mode_line_format will get the right
15790 values. */
15791 push_frame_kboard (it.f);
15792 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15793 pop_frame_kboard ();
15794
15795 unbind_to (count, Qnil);
15796
15797 /* Fill up with spaces. */
15798 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15799
15800 compute_line_metrics (&it);
15801 it.glyph_row->full_width_p = 1;
15802 it.glyph_row->continued_p = 0;
15803 it.glyph_row->truncated_on_left_p = 0;
15804 it.glyph_row->truncated_on_right_p = 0;
15805
15806 /* Make a 3D mode-line have a shadow at its right end. */
15807 face = FACE_FROM_ID (it.f, face_id);
15808 extend_face_to_end_of_line (&it);
15809 if (face->box != FACE_NO_BOX)
15810 {
15811 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15812 + it.glyph_row->used[TEXT_AREA] - 1);
15813 last->right_box_line_p = 1;
15814 }
15815
15816 return it.glyph_row->height;
15817 }
15818
15819 /* Contribute ELT to the mode line for window IT->w. How it
15820 translates into text depends on its data type.
15821
15822 IT describes the display environment in which we display, as usual.
15823
15824 DEPTH is the depth in recursion. It is used to prevent
15825 infinite recursion here.
15826
15827 FIELD_WIDTH is the number of characters the display of ELT should
15828 occupy in the mode line, and PRECISION is the maximum number of
15829 characters to display from ELT's representation. See
15830 display_string for details.
15831
15832 Returns the hpos of the end of the text generated by ELT.
15833
15834 PROPS is a property list to add to any string we encounter.
15835
15836 If RISKY is nonzero, remove (disregard) any properties in any string
15837 we encounter, and ignore :eval and :propertize.
15838
15839 The global variable `mode_line_target' determines whether the
15840 output is passed to `store_mode_line_noprop',
15841 `store_mode_line_string', or `display_string'. */
15842
15843 static int
15844 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15845 struct it *it;
15846 int depth;
15847 int field_width, precision;
15848 Lisp_Object elt, props;
15849 int risky;
15850 {
15851 int n = 0, field, prec;
15852 int literal = 0;
15853
15854 tail_recurse:
15855 if (depth > 100)
15856 elt = build_string ("*too-deep*");
15857
15858 depth++;
15859
15860 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15861 {
15862 case Lisp_String:
15863 {
15864 /* A string: output it and check for %-constructs within it. */
15865 unsigned char c;
15866 const unsigned char *this, *lisp_string;
15867
15868 if (!NILP (props) || risky)
15869 {
15870 Lisp_Object oprops, aelt;
15871 oprops = Ftext_properties_at (make_number (0), elt);
15872
15873 /* If the starting string's properties are not what
15874 we want, translate the string. Also, if the string
15875 is risky, do that anyway. */
15876
15877 if (NILP (Fequal (props, oprops)) || risky)
15878 {
15879 /* If the starting string has properties,
15880 merge the specified ones onto the existing ones. */
15881 if (! NILP (oprops) && !risky)
15882 {
15883 Lisp_Object tem;
15884
15885 oprops = Fcopy_sequence (oprops);
15886 tem = props;
15887 while (CONSP (tem))
15888 {
15889 oprops = Fplist_put (oprops, XCAR (tem),
15890 XCAR (XCDR (tem)));
15891 tem = XCDR (XCDR (tem));
15892 }
15893 props = oprops;
15894 }
15895
15896 aelt = Fassoc (elt, mode_line_proptrans_alist);
15897 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15898 {
15899 mode_line_proptrans_alist
15900 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15901 elt = XCAR (aelt);
15902 }
15903 else
15904 {
15905 Lisp_Object tem;
15906
15907 elt = Fcopy_sequence (elt);
15908 Fset_text_properties (make_number (0), Flength (elt),
15909 props, elt);
15910 /* Add this item to mode_line_proptrans_alist. */
15911 mode_line_proptrans_alist
15912 = Fcons (Fcons (elt, props),
15913 mode_line_proptrans_alist);
15914 /* Truncate mode_line_proptrans_alist
15915 to at most 50 elements. */
15916 tem = Fnthcdr (make_number (50),
15917 mode_line_proptrans_alist);
15918 if (! NILP (tem))
15919 XSETCDR (tem, Qnil);
15920 }
15921 }
15922 }
15923
15924 this = SDATA (elt);
15925 lisp_string = this;
15926
15927 if (literal)
15928 {
15929 prec = precision - n;
15930 switch (mode_line_target)
15931 {
15932 case MODE_LINE_NOPROP:
15933 case MODE_LINE_TITLE:
15934 n += store_mode_line_noprop (SDATA (elt), -1, prec);
15935 break;
15936 case MODE_LINE_STRING:
15937 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15938 break;
15939 case MODE_LINE_DISPLAY:
15940 n += display_string (NULL, elt, Qnil, 0, 0, it,
15941 0, prec, 0, STRING_MULTIBYTE (elt));
15942 break;
15943 }
15944
15945 break;
15946 }
15947
15948 while ((precision <= 0 || n < precision)
15949 && *this
15950 && (mode_line_target != MODE_LINE_DISPLAY
15951 || it->current_x < it->last_visible_x))
15952 {
15953 const unsigned char *last = this;
15954
15955 /* Advance to end of string or next format specifier. */
15956 while ((c = *this++) != '\0' && c != '%')
15957 ;
15958
15959 if (this - 1 != last)
15960 {
15961 int nchars, nbytes;
15962
15963 /* Output to end of string or up to '%'. Field width
15964 is length of string. Don't output more than
15965 PRECISION allows us. */
15966 --this;
15967
15968 prec = c_string_width (last, this - last, precision - n,
15969 &nchars, &nbytes);
15970
15971 switch (mode_line_target)
15972 {
15973 case MODE_LINE_NOPROP:
15974 case MODE_LINE_TITLE:
15975 n += store_mode_line_noprop (last, 0, prec);
15976 break;
15977 case MODE_LINE_STRING:
15978 {
15979 int bytepos = last - lisp_string;
15980 int charpos = string_byte_to_char (elt, bytepos);
15981 int endpos = (precision <= 0
15982 ? string_byte_to_char (elt,
15983 this - lisp_string)
15984 : charpos + nchars);
15985
15986 n += store_mode_line_string (NULL,
15987 Fsubstring (elt, make_number (charpos),
15988 make_number (endpos)),
15989 0, 0, 0, Qnil);
15990 }
15991 break;
15992 case MODE_LINE_DISPLAY:
15993 {
15994 int bytepos = last - lisp_string;
15995 int charpos = string_byte_to_char (elt, bytepos);
15996 n += display_string (NULL, elt, Qnil, 0, charpos,
15997 it, 0, prec, 0,
15998 STRING_MULTIBYTE (elt));
15999 }
16000 break;
16001 }
16002 }
16003 else /* c == '%' */
16004 {
16005 const unsigned char *percent_position = this;
16006
16007 /* Get the specified minimum width. Zero means
16008 don't pad. */
16009 field = 0;
16010 while ((c = *this++) >= '0' && c <= '9')
16011 field = field * 10 + c - '0';
16012
16013 /* Don't pad beyond the total padding allowed. */
16014 if (field_width - n > 0 && field > field_width - n)
16015 field = field_width - n;
16016
16017 /* Note that either PRECISION <= 0 or N < PRECISION. */
16018 prec = precision - n;
16019
16020 if (c == 'M')
16021 n += display_mode_element (it, depth, field, prec,
16022 Vglobal_mode_string, props,
16023 risky);
16024 else if (c != 0)
16025 {
16026 int multibyte;
16027 int bytepos, charpos;
16028 unsigned char *spec;
16029
16030 bytepos = percent_position - lisp_string;
16031 charpos = (STRING_MULTIBYTE (elt)
16032 ? string_byte_to_char (elt, bytepos)
16033 : bytepos);
16034
16035 spec
16036 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16037
16038 switch (mode_line_target)
16039 {
16040 case MODE_LINE_NOPROP:
16041 case MODE_LINE_TITLE:
16042 n += store_mode_line_noprop (spec, field, prec);
16043 break;
16044 case MODE_LINE_STRING:
16045 {
16046 int len = strlen (spec);
16047 Lisp_Object tem = make_string (spec, len);
16048 props = Ftext_properties_at (make_number (charpos), elt);
16049 /* Should only keep face property in props */
16050 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16051 }
16052 break;
16053 case MODE_LINE_DISPLAY:
16054 {
16055 int nglyphs_before, nwritten;
16056
16057 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16058 nwritten = display_string (spec, Qnil, elt,
16059 charpos, 0, it,
16060 field, prec, 0,
16061 multibyte);
16062
16063 /* Assign to the glyphs written above the
16064 string where the `%x' came from, position
16065 of the `%'. */
16066 if (nwritten > 0)
16067 {
16068 struct glyph *glyph
16069 = (it->glyph_row->glyphs[TEXT_AREA]
16070 + nglyphs_before);
16071 int i;
16072
16073 for (i = 0; i < nwritten; ++i)
16074 {
16075 glyph[i].object = elt;
16076 glyph[i].charpos = charpos;
16077 }
16078
16079 n += nwritten;
16080 }
16081 }
16082 break;
16083 }
16084 }
16085 else /* c == 0 */
16086 break;
16087 }
16088 }
16089 }
16090 break;
16091
16092 case Lisp_Symbol:
16093 /* A symbol: process the value of the symbol recursively
16094 as if it appeared here directly. Avoid error if symbol void.
16095 Special case: if value of symbol is a string, output the string
16096 literally. */
16097 {
16098 register Lisp_Object tem;
16099
16100 /* If the variable is not marked as risky to set
16101 then its contents are risky to use. */
16102 if (NILP (Fget (elt, Qrisky_local_variable)))
16103 risky = 1;
16104
16105 tem = Fboundp (elt);
16106 if (!NILP (tem))
16107 {
16108 tem = Fsymbol_value (elt);
16109 /* If value is a string, output that string literally:
16110 don't check for % within it. */
16111 if (STRINGP (tem))
16112 literal = 1;
16113
16114 if (!EQ (tem, elt))
16115 {
16116 /* Give up right away for nil or t. */
16117 elt = tem;
16118 goto tail_recurse;
16119 }
16120 }
16121 }
16122 break;
16123
16124 case Lisp_Cons:
16125 {
16126 register Lisp_Object car, tem;
16127
16128 /* A cons cell: five distinct cases.
16129 If first element is :eval or :propertize, do something special.
16130 If first element is a string or a cons, process all the elements
16131 and effectively concatenate them.
16132 If first element is a negative number, truncate displaying cdr to
16133 at most that many characters. If positive, pad (with spaces)
16134 to at least that many characters.
16135 If first element is a symbol, process the cadr or caddr recursively
16136 according to whether the symbol's value is non-nil or nil. */
16137 car = XCAR (elt);
16138 if (EQ (car, QCeval))
16139 {
16140 /* An element of the form (:eval FORM) means evaluate FORM
16141 and use the result as mode line elements. */
16142
16143 if (risky)
16144 break;
16145
16146 if (CONSP (XCDR (elt)))
16147 {
16148 Lisp_Object spec;
16149 spec = safe_eval (XCAR (XCDR (elt)));
16150 n += display_mode_element (it, depth, field_width - n,
16151 precision - n, spec, props,
16152 risky);
16153 }
16154 }
16155 else if (EQ (car, QCpropertize))
16156 {
16157 /* An element of the form (:propertize ELT PROPS...)
16158 means display ELT but applying properties PROPS. */
16159
16160 if (risky)
16161 break;
16162
16163 if (CONSP (XCDR (elt)))
16164 n += display_mode_element (it, depth, field_width - n,
16165 precision - n, XCAR (XCDR (elt)),
16166 XCDR (XCDR (elt)), risky);
16167 }
16168 else if (SYMBOLP (car))
16169 {
16170 tem = Fboundp (car);
16171 elt = XCDR (elt);
16172 if (!CONSP (elt))
16173 goto invalid;
16174 /* elt is now the cdr, and we know it is a cons cell.
16175 Use its car if CAR has a non-nil value. */
16176 if (!NILP (tem))
16177 {
16178 tem = Fsymbol_value (car);
16179 if (!NILP (tem))
16180 {
16181 elt = XCAR (elt);
16182 goto tail_recurse;
16183 }
16184 }
16185 /* Symbol's value is nil (or symbol is unbound)
16186 Get the cddr of the original list
16187 and if possible find the caddr and use that. */
16188 elt = XCDR (elt);
16189 if (NILP (elt))
16190 break;
16191 else if (!CONSP (elt))
16192 goto invalid;
16193 elt = XCAR (elt);
16194 goto tail_recurse;
16195 }
16196 else if (INTEGERP (car))
16197 {
16198 register int lim = XINT (car);
16199 elt = XCDR (elt);
16200 if (lim < 0)
16201 {
16202 /* Negative int means reduce maximum width. */
16203 if (precision <= 0)
16204 precision = -lim;
16205 else
16206 precision = min (precision, -lim);
16207 }
16208 else if (lim > 0)
16209 {
16210 /* Padding specified. Don't let it be more than
16211 current maximum. */
16212 if (precision > 0)
16213 lim = min (precision, lim);
16214
16215 /* If that's more padding than already wanted, queue it.
16216 But don't reduce padding already specified even if
16217 that is beyond the current truncation point. */
16218 field_width = max (lim, field_width);
16219 }
16220 goto tail_recurse;
16221 }
16222 else if (STRINGP (car) || CONSP (car))
16223 {
16224 register int limit = 50;
16225 /* Limit is to protect against circular lists. */
16226 while (CONSP (elt)
16227 && --limit > 0
16228 && (precision <= 0 || n < precision))
16229 {
16230 n += display_mode_element (it, depth,
16231 /* Do padding only after the last
16232 element in the list. */
16233 (! CONSP (XCDR (elt))
16234 ? field_width - n
16235 : 0),
16236 precision - n, XCAR (elt),
16237 props, risky);
16238 elt = XCDR (elt);
16239 }
16240 }
16241 }
16242 break;
16243
16244 default:
16245 invalid:
16246 elt = build_string ("*invalid*");
16247 goto tail_recurse;
16248 }
16249
16250 /* Pad to FIELD_WIDTH. */
16251 if (field_width > 0 && n < field_width)
16252 {
16253 switch (mode_line_target)
16254 {
16255 case MODE_LINE_NOPROP:
16256 case MODE_LINE_TITLE:
16257 n += store_mode_line_noprop ("", field_width - n, 0);
16258 break;
16259 case MODE_LINE_STRING:
16260 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16261 break;
16262 case MODE_LINE_DISPLAY:
16263 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16264 0, 0, 0);
16265 break;
16266 }
16267 }
16268
16269 return n;
16270 }
16271
16272 /* Store a mode-line string element in mode_line_string_list.
16273
16274 If STRING is non-null, display that C string. Otherwise, the Lisp
16275 string LISP_STRING is displayed.
16276
16277 FIELD_WIDTH is the minimum number of output glyphs to produce.
16278 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16279 with spaces. FIELD_WIDTH <= 0 means don't pad.
16280
16281 PRECISION is the maximum number of characters to output from
16282 STRING. PRECISION <= 0 means don't truncate the string.
16283
16284 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16285 properties to the string.
16286
16287 PROPS are the properties to add to the string.
16288 The mode_line_string_face face property is always added to the string.
16289 */
16290
16291 static int
16292 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16293 char *string;
16294 Lisp_Object lisp_string;
16295 int copy_string;
16296 int field_width;
16297 int precision;
16298 Lisp_Object props;
16299 {
16300 int len;
16301 int n = 0;
16302
16303 if (string != NULL)
16304 {
16305 len = strlen (string);
16306 if (precision > 0 && len > precision)
16307 len = precision;
16308 lisp_string = make_string (string, len);
16309 if (NILP (props))
16310 props = mode_line_string_face_prop;
16311 else if (!NILP (mode_line_string_face))
16312 {
16313 Lisp_Object face = Fplist_get (props, Qface);
16314 props = Fcopy_sequence (props);
16315 if (NILP (face))
16316 face = mode_line_string_face;
16317 else
16318 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16319 props = Fplist_put (props, Qface, face);
16320 }
16321 Fadd_text_properties (make_number (0), make_number (len),
16322 props, lisp_string);
16323 }
16324 else
16325 {
16326 len = XFASTINT (Flength (lisp_string));
16327 if (precision > 0 && len > precision)
16328 {
16329 len = precision;
16330 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16331 precision = -1;
16332 }
16333 if (!NILP (mode_line_string_face))
16334 {
16335 Lisp_Object face;
16336 if (NILP (props))
16337 props = Ftext_properties_at (make_number (0), lisp_string);
16338 face = Fplist_get (props, Qface);
16339 if (NILP (face))
16340 face = mode_line_string_face;
16341 else
16342 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16343 props = Fcons (Qface, Fcons (face, Qnil));
16344 if (copy_string)
16345 lisp_string = Fcopy_sequence (lisp_string);
16346 }
16347 if (!NILP (props))
16348 Fadd_text_properties (make_number (0), make_number (len),
16349 props, lisp_string);
16350 }
16351
16352 if (len > 0)
16353 {
16354 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16355 n += len;
16356 }
16357
16358 if (field_width > len)
16359 {
16360 field_width -= len;
16361 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16362 if (!NILP (props))
16363 Fadd_text_properties (make_number (0), make_number (field_width),
16364 props, lisp_string);
16365 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16366 n += field_width;
16367 }
16368
16369 return n;
16370 }
16371
16372
16373 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16374 1, 4, 0,
16375 doc: /* Format a string out of a mode line format specification.
16376 First arg FORMAT specifies the mode line format (see `mode-line-format'
16377 for details) to use.
16378
16379 Optional second arg FACE specifies the face property to put
16380 on all characters for which no face is specified.
16381 t means whatever face the window's mode line currently uses
16382 \(either `mode-line' or `mode-line-inactive', depending).
16383 nil means the default is no face property.
16384 If FACE is an integer, the value string has no text properties.
16385
16386 Optional third and fourth args WINDOW and BUFFER specify the window
16387 and buffer to use as the context for the formatting (defaults
16388 are the selected window and the window's buffer). */)
16389 (format, face, window, buffer)
16390 Lisp_Object format, face, window, buffer;
16391 {
16392 struct it it;
16393 int len;
16394 struct window *w;
16395 struct buffer *old_buffer = NULL;
16396 int face_id = -1;
16397 int no_props = INTEGERP (face);
16398 int count = SPECPDL_INDEX ();
16399 Lisp_Object str;
16400 int string_start = 0;
16401
16402 if (NILP (window))
16403 window = selected_window;
16404 CHECK_WINDOW (window);
16405 w = XWINDOW (window);
16406
16407 if (NILP (buffer))
16408 buffer = w->buffer;
16409 CHECK_BUFFER (buffer);
16410
16411 if (NILP (format))
16412 return build_string ("");
16413
16414 if (no_props)
16415 face = Qnil;
16416
16417 if (!NILP (face))
16418 {
16419 if (EQ (face, Qt))
16420 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16421 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16422 }
16423
16424 if (face_id < 0)
16425 face_id = DEFAULT_FACE_ID;
16426
16427 if (XBUFFER (buffer) != current_buffer)
16428 old_buffer = current_buffer;
16429
16430 record_unwind_protect (unwind_format_mode_line,
16431 format_mode_line_unwind_data (old_buffer));
16432
16433 if (old_buffer)
16434 set_buffer_internal_1 (XBUFFER (buffer));
16435
16436 init_iterator (&it, w, -1, -1, NULL, face_id);
16437
16438 if (no_props)
16439 {
16440 mode_line_target = MODE_LINE_NOPROP;
16441 mode_line_string_face_prop = Qnil;
16442 mode_line_string_list = Qnil;
16443 string_start = MODE_LINE_NOPROP_LEN (0);
16444 }
16445 else
16446 {
16447 mode_line_target = MODE_LINE_STRING;
16448 mode_line_string_list = Qnil;
16449 mode_line_string_face = face;
16450 mode_line_string_face_prop
16451 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16452 }
16453
16454 push_frame_kboard (it.f);
16455 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16456 pop_frame_kboard ();
16457
16458 if (no_props)
16459 {
16460 len = MODE_LINE_NOPROP_LEN (string_start);
16461 str = make_string (mode_line_noprop_buf + string_start, len);
16462 }
16463 else
16464 {
16465 mode_line_string_list = Fnreverse (mode_line_string_list);
16466 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16467 make_string ("", 0));
16468 }
16469
16470 unbind_to (count, Qnil);
16471 return str;
16472 }
16473
16474 /* Write a null-terminated, right justified decimal representation of
16475 the positive integer D to BUF using a minimal field width WIDTH. */
16476
16477 static void
16478 pint2str (buf, width, d)
16479 register char *buf;
16480 register int width;
16481 register int d;
16482 {
16483 register char *p = buf;
16484
16485 if (d <= 0)
16486 *p++ = '0';
16487 else
16488 {
16489 while (d > 0)
16490 {
16491 *p++ = d % 10 + '0';
16492 d /= 10;
16493 }
16494 }
16495
16496 for (width -= (int) (p - buf); width > 0; --width)
16497 *p++ = ' ';
16498 *p-- = '\0';
16499 while (p > buf)
16500 {
16501 d = *buf;
16502 *buf++ = *p;
16503 *p-- = d;
16504 }
16505 }
16506
16507 /* Write a null-terminated, right justified decimal and "human
16508 readable" representation of the nonnegative integer D to BUF using
16509 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16510
16511 static const char power_letter[] =
16512 {
16513 0, /* not used */
16514 'k', /* kilo */
16515 'M', /* mega */
16516 'G', /* giga */
16517 'T', /* tera */
16518 'P', /* peta */
16519 'E', /* exa */
16520 'Z', /* zetta */
16521 'Y' /* yotta */
16522 };
16523
16524 static void
16525 pint2hrstr (buf, width, d)
16526 char *buf;
16527 int width;
16528 int d;
16529 {
16530 /* We aim to represent the nonnegative integer D as
16531 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16532 int quotient = d;
16533 int remainder = 0;
16534 /* -1 means: do not use TENTHS. */
16535 int tenths = -1;
16536 int exponent = 0;
16537
16538 /* Length of QUOTIENT.TENTHS as a string. */
16539 int length;
16540
16541 char * psuffix;
16542 char * p;
16543
16544 if (1000 <= quotient)
16545 {
16546 /* Scale to the appropriate EXPONENT. */
16547 do
16548 {
16549 remainder = quotient % 1000;
16550 quotient /= 1000;
16551 exponent++;
16552 }
16553 while (1000 <= quotient);
16554
16555 /* Round to nearest and decide whether to use TENTHS or not. */
16556 if (quotient <= 9)
16557 {
16558 tenths = remainder / 100;
16559 if (50 <= remainder % 100)
16560 {
16561 if (tenths < 9)
16562 tenths++;
16563 else
16564 {
16565 quotient++;
16566 if (quotient == 10)
16567 tenths = -1;
16568 else
16569 tenths = 0;
16570 }
16571 }
16572 }
16573 else
16574 if (500 <= remainder)
16575 {
16576 if (quotient < 999)
16577 quotient++;
16578 else
16579 {
16580 quotient = 1;
16581 exponent++;
16582 tenths = 0;
16583 }
16584 }
16585 }
16586
16587 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16588 if (tenths == -1 && quotient <= 99)
16589 if (quotient <= 9)
16590 length = 1;
16591 else
16592 length = 2;
16593 else
16594 length = 3;
16595 p = psuffix = buf + max (width, length);
16596
16597 /* Print EXPONENT. */
16598 if (exponent)
16599 *psuffix++ = power_letter[exponent];
16600 *psuffix = '\0';
16601
16602 /* Print TENTHS. */
16603 if (tenths >= 0)
16604 {
16605 *--p = '0' + tenths;
16606 *--p = '.';
16607 }
16608
16609 /* Print QUOTIENT. */
16610 do
16611 {
16612 int digit = quotient % 10;
16613 *--p = '0' + digit;
16614 }
16615 while ((quotient /= 10) != 0);
16616
16617 /* Print leading spaces. */
16618 while (buf < p)
16619 *--p = ' ';
16620 }
16621
16622 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16623 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16624 type of CODING_SYSTEM. Return updated pointer into BUF. */
16625
16626 static unsigned char invalid_eol_type[] = "(*invalid*)";
16627
16628 static char *
16629 decode_mode_spec_coding (coding_system, buf, eol_flag)
16630 Lisp_Object coding_system;
16631 register char *buf;
16632 int eol_flag;
16633 {
16634 Lisp_Object val;
16635 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16636 const unsigned char *eol_str;
16637 int eol_str_len;
16638 /* The EOL conversion we are using. */
16639 Lisp_Object eoltype;
16640
16641 val = Fget (coding_system, Qcoding_system);
16642 eoltype = Qnil;
16643
16644 if (!VECTORP (val)) /* Not yet decided. */
16645 {
16646 if (multibyte)
16647 *buf++ = '-';
16648 if (eol_flag)
16649 eoltype = eol_mnemonic_undecided;
16650 /* Don't mention EOL conversion if it isn't decided. */
16651 }
16652 else
16653 {
16654 Lisp_Object eolvalue;
16655
16656 eolvalue = Fget (coding_system, Qeol_type);
16657
16658 if (multibyte)
16659 *buf++ = XFASTINT (AREF (val, 1));
16660
16661 if (eol_flag)
16662 {
16663 /* The EOL conversion that is normal on this system. */
16664
16665 if (NILP (eolvalue)) /* Not yet decided. */
16666 eoltype = eol_mnemonic_undecided;
16667 else if (VECTORP (eolvalue)) /* Not yet decided. */
16668 eoltype = eol_mnemonic_undecided;
16669 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16670 eoltype = (XFASTINT (eolvalue) == 0
16671 ? eol_mnemonic_unix
16672 : (XFASTINT (eolvalue) == 1
16673 ? eol_mnemonic_dos : eol_mnemonic_mac));
16674 }
16675 }
16676
16677 if (eol_flag)
16678 {
16679 /* Mention the EOL conversion if it is not the usual one. */
16680 if (STRINGP (eoltype))
16681 {
16682 eol_str = SDATA (eoltype);
16683 eol_str_len = SBYTES (eoltype);
16684 }
16685 else if (INTEGERP (eoltype)
16686 && CHAR_VALID_P (XINT (eoltype), 0))
16687 {
16688 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16689 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16690 eol_str = tmp;
16691 }
16692 else
16693 {
16694 eol_str = invalid_eol_type;
16695 eol_str_len = sizeof (invalid_eol_type) - 1;
16696 }
16697 bcopy (eol_str, buf, eol_str_len);
16698 buf += eol_str_len;
16699 }
16700
16701 return buf;
16702 }
16703
16704 /* Return a string for the output of a mode line %-spec for window W,
16705 generated by character C. PRECISION >= 0 means don't return a
16706 string longer than that value. FIELD_WIDTH > 0 means pad the
16707 string returned with spaces to that value. Return 1 in *MULTIBYTE
16708 if the result is multibyte text.
16709
16710 Note we operate on the current buffer for most purposes,
16711 the exception being w->base_line_pos. */
16712
16713 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16714
16715 static char *
16716 decode_mode_spec (w, c, field_width, precision, multibyte)
16717 struct window *w;
16718 register int c;
16719 int field_width, precision;
16720 int *multibyte;
16721 {
16722 Lisp_Object obj;
16723 struct frame *f = XFRAME (WINDOW_FRAME (w));
16724 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16725 struct buffer *b = current_buffer;
16726
16727 obj = Qnil;
16728 *multibyte = 0;
16729
16730 switch (c)
16731 {
16732 case '*':
16733 if (!NILP (b->read_only))
16734 return "%";
16735 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16736 return "*";
16737 return "-";
16738
16739 case '+':
16740 /* This differs from %* only for a modified read-only buffer. */
16741 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16742 return "*";
16743 if (!NILP (b->read_only))
16744 return "%";
16745 return "-";
16746
16747 case '&':
16748 /* This differs from %* in ignoring read-only-ness. */
16749 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16750 return "*";
16751 return "-";
16752
16753 case '%':
16754 return "%";
16755
16756 case '[':
16757 {
16758 int i;
16759 char *p;
16760
16761 if (command_loop_level > 5)
16762 return "[[[... ";
16763 p = decode_mode_spec_buf;
16764 for (i = 0; i < command_loop_level; i++)
16765 *p++ = '[';
16766 *p = 0;
16767 return decode_mode_spec_buf;
16768 }
16769
16770 case ']':
16771 {
16772 int i;
16773 char *p;
16774
16775 if (command_loop_level > 5)
16776 return " ...]]]";
16777 p = decode_mode_spec_buf;
16778 for (i = 0; i < command_loop_level; i++)
16779 *p++ = ']';
16780 *p = 0;
16781 return decode_mode_spec_buf;
16782 }
16783
16784 case '-':
16785 {
16786 register int i;
16787
16788 /* Let lots_of_dashes be a string of infinite length. */
16789 if (mode_line_target == MODE_LINE_NOPROP ||
16790 mode_line_target == MODE_LINE_STRING)
16791 return "--";
16792 if (field_width <= 0
16793 || field_width > sizeof (lots_of_dashes))
16794 {
16795 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16796 decode_mode_spec_buf[i] = '-';
16797 decode_mode_spec_buf[i] = '\0';
16798 return decode_mode_spec_buf;
16799 }
16800 else
16801 return lots_of_dashes;
16802 }
16803
16804 case 'b':
16805 obj = b->name;
16806 break;
16807
16808 case 'c':
16809 {
16810 int col = (int) current_column (); /* iftc */
16811 w->column_number_displayed = make_number (col);
16812 pint2str (decode_mode_spec_buf, field_width, col);
16813 return decode_mode_spec_buf;
16814 }
16815
16816 case 'F':
16817 /* %F displays the frame name. */
16818 if (!NILP (f->title))
16819 return (char *) SDATA (f->title);
16820 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16821 return (char *) SDATA (f->name);
16822 return "Emacs";
16823
16824 case 'f':
16825 obj = b->filename;
16826 break;
16827
16828 case 'i':
16829 {
16830 int size = ZV - BEGV;
16831 pint2str (decode_mode_spec_buf, field_width, size);
16832 return decode_mode_spec_buf;
16833 }
16834
16835 case 'I':
16836 {
16837 int size = ZV - BEGV;
16838 pint2hrstr (decode_mode_spec_buf, field_width, size);
16839 return decode_mode_spec_buf;
16840 }
16841
16842 case 'l':
16843 {
16844 int startpos = XMARKER (w->start)->charpos;
16845 int startpos_byte = marker_byte_position (w->start);
16846 int line, linepos, linepos_byte, topline;
16847 int nlines, junk;
16848 int height = WINDOW_TOTAL_LINES (w);
16849
16850 /* If we decided that this buffer isn't suitable for line numbers,
16851 don't forget that too fast. */
16852 if (EQ (w->base_line_pos, w->buffer))
16853 goto no_value;
16854 /* But do forget it, if the window shows a different buffer now. */
16855 else if (BUFFERP (w->base_line_pos))
16856 w->base_line_pos = Qnil;
16857
16858 /* If the buffer is very big, don't waste time. */
16859 if (INTEGERP (Vline_number_display_limit)
16860 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16861 {
16862 w->base_line_pos = Qnil;
16863 w->base_line_number = Qnil;
16864 goto no_value;
16865 }
16866
16867 if (!NILP (w->base_line_number)
16868 && !NILP (w->base_line_pos)
16869 && XFASTINT (w->base_line_pos) <= startpos)
16870 {
16871 line = XFASTINT (w->base_line_number);
16872 linepos = XFASTINT (w->base_line_pos);
16873 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16874 }
16875 else
16876 {
16877 line = 1;
16878 linepos = BUF_BEGV (b);
16879 linepos_byte = BUF_BEGV_BYTE (b);
16880 }
16881
16882 /* Count lines from base line to window start position. */
16883 nlines = display_count_lines (linepos, linepos_byte,
16884 startpos_byte,
16885 startpos, &junk);
16886
16887 topline = nlines + line;
16888
16889 /* Determine a new base line, if the old one is too close
16890 or too far away, or if we did not have one.
16891 "Too close" means it's plausible a scroll-down would
16892 go back past it. */
16893 if (startpos == BUF_BEGV (b))
16894 {
16895 w->base_line_number = make_number (topline);
16896 w->base_line_pos = make_number (BUF_BEGV (b));
16897 }
16898 else if (nlines < height + 25 || nlines > height * 3 + 50
16899 || linepos == BUF_BEGV (b))
16900 {
16901 int limit = BUF_BEGV (b);
16902 int limit_byte = BUF_BEGV_BYTE (b);
16903 int position;
16904 int distance = (height * 2 + 30) * line_number_display_limit_width;
16905
16906 if (startpos - distance > limit)
16907 {
16908 limit = startpos - distance;
16909 limit_byte = CHAR_TO_BYTE (limit);
16910 }
16911
16912 nlines = display_count_lines (startpos, startpos_byte,
16913 limit_byte,
16914 - (height * 2 + 30),
16915 &position);
16916 /* If we couldn't find the lines we wanted within
16917 line_number_display_limit_width chars per line,
16918 give up on line numbers for this window. */
16919 if (position == limit_byte && limit == startpos - distance)
16920 {
16921 w->base_line_pos = w->buffer;
16922 w->base_line_number = Qnil;
16923 goto no_value;
16924 }
16925
16926 w->base_line_number = make_number (topline - nlines);
16927 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16928 }
16929
16930 /* Now count lines from the start pos to point. */
16931 nlines = display_count_lines (startpos, startpos_byte,
16932 PT_BYTE, PT, &junk);
16933
16934 /* Record that we did display the line number. */
16935 line_number_displayed = 1;
16936
16937 /* Make the string to show. */
16938 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16939 return decode_mode_spec_buf;
16940 no_value:
16941 {
16942 char* p = decode_mode_spec_buf;
16943 int pad = field_width - 2;
16944 while (pad-- > 0)
16945 *p++ = ' ';
16946 *p++ = '?';
16947 *p++ = '?';
16948 *p = '\0';
16949 return decode_mode_spec_buf;
16950 }
16951 }
16952 break;
16953
16954 case 'm':
16955 obj = b->mode_name;
16956 break;
16957
16958 case 'n':
16959 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16960 return " Narrow";
16961 break;
16962
16963 case 'p':
16964 {
16965 int pos = marker_position (w->start);
16966 int total = BUF_ZV (b) - BUF_BEGV (b);
16967
16968 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16969 {
16970 if (pos <= BUF_BEGV (b))
16971 return "All";
16972 else
16973 return "Bottom";
16974 }
16975 else if (pos <= BUF_BEGV (b))
16976 return "Top";
16977 else
16978 {
16979 if (total > 1000000)
16980 /* Do it differently for a large value, to avoid overflow. */
16981 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16982 else
16983 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16984 /* We can't normally display a 3-digit number,
16985 so get us a 2-digit number that is close. */
16986 if (total == 100)
16987 total = 99;
16988 sprintf (decode_mode_spec_buf, "%2d%%", total);
16989 return decode_mode_spec_buf;
16990 }
16991 }
16992
16993 /* Display percentage of size above the bottom of the screen. */
16994 case 'P':
16995 {
16996 int toppos = marker_position (w->start);
16997 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16998 int total = BUF_ZV (b) - BUF_BEGV (b);
16999
17000 if (botpos >= BUF_ZV (b))
17001 {
17002 if (toppos <= BUF_BEGV (b))
17003 return "All";
17004 else
17005 return "Bottom";
17006 }
17007 else
17008 {
17009 if (total > 1000000)
17010 /* Do it differently for a large value, to avoid overflow. */
17011 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17012 else
17013 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17014 /* We can't normally display a 3-digit number,
17015 so get us a 2-digit number that is close. */
17016 if (total == 100)
17017 total = 99;
17018 if (toppos <= BUF_BEGV (b))
17019 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17020 else
17021 sprintf (decode_mode_spec_buf, "%2d%%", total);
17022 return decode_mode_spec_buf;
17023 }
17024 }
17025
17026 case 's':
17027 /* status of process */
17028 obj = Fget_buffer_process (Fcurrent_buffer ());
17029 if (NILP (obj))
17030 return "no process";
17031 #ifdef subprocesses
17032 obj = Fsymbol_name (Fprocess_status (obj));
17033 #endif
17034 break;
17035
17036 case 't': /* indicate TEXT or BINARY */
17037 #ifdef MODE_LINE_BINARY_TEXT
17038 return MODE_LINE_BINARY_TEXT (b);
17039 #else
17040 return "T";
17041 #endif
17042
17043 case 'z':
17044 /* coding-system (not including end-of-line format) */
17045 case 'Z':
17046 /* coding-system (including end-of-line type) */
17047 {
17048 int eol_flag = (c == 'Z');
17049 char *p = decode_mode_spec_buf;
17050
17051 if (! FRAME_WINDOW_P (f))
17052 {
17053 /* No need to mention EOL here--the terminal never needs
17054 to do EOL conversion. */
17055 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
17056 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
17057 }
17058 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17059 p, eol_flag);
17060
17061 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17062 #ifdef subprocesses
17063 obj = Fget_buffer_process (Fcurrent_buffer ());
17064 if (PROCESSP (obj))
17065 {
17066 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17067 p, eol_flag);
17068 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17069 p, eol_flag);
17070 }
17071 #endif /* subprocesses */
17072 #endif /* 0 */
17073 *p = 0;
17074 return decode_mode_spec_buf;
17075 }
17076 }
17077
17078 if (STRINGP (obj))
17079 {
17080 *multibyte = STRING_MULTIBYTE (obj);
17081 return (char *) SDATA (obj);
17082 }
17083 else
17084 return "";
17085 }
17086
17087
17088 /* Count up to COUNT lines starting from START / START_BYTE.
17089 But don't go beyond LIMIT_BYTE.
17090 Return the number of lines thus found (always nonnegative).
17091
17092 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17093
17094 static int
17095 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17096 int start, start_byte, limit_byte, count;
17097 int *byte_pos_ptr;
17098 {
17099 register unsigned char *cursor;
17100 unsigned char *base;
17101
17102 register int ceiling;
17103 register unsigned char *ceiling_addr;
17104 int orig_count = count;
17105
17106 /* If we are not in selective display mode,
17107 check only for newlines. */
17108 int selective_display = (!NILP (current_buffer->selective_display)
17109 && !INTEGERP (current_buffer->selective_display));
17110
17111 if (count > 0)
17112 {
17113 while (start_byte < limit_byte)
17114 {
17115 ceiling = BUFFER_CEILING_OF (start_byte);
17116 ceiling = min (limit_byte - 1, ceiling);
17117 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17118 base = (cursor = BYTE_POS_ADDR (start_byte));
17119 while (1)
17120 {
17121 if (selective_display)
17122 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17123 ;
17124 else
17125 while (*cursor != '\n' && ++cursor != ceiling_addr)
17126 ;
17127
17128 if (cursor != ceiling_addr)
17129 {
17130 if (--count == 0)
17131 {
17132 start_byte += cursor - base + 1;
17133 *byte_pos_ptr = start_byte;
17134 return orig_count;
17135 }
17136 else
17137 if (++cursor == ceiling_addr)
17138 break;
17139 }
17140 else
17141 break;
17142 }
17143 start_byte += cursor - base;
17144 }
17145 }
17146 else
17147 {
17148 while (start_byte > limit_byte)
17149 {
17150 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17151 ceiling = max (limit_byte, ceiling);
17152 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17153 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17154 while (1)
17155 {
17156 if (selective_display)
17157 while (--cursor != ceiling_addr
17158 && *cursor != '\n' && *cursor != 015)
17159 ;
17160 else
17161 while (--cursor != ceiling_addr && *cursor != '\n')
17162 ;
17163
17164 if (cursor != ceiling_addr)
17165 {
17166 if (++count == 0)
17167 {
17168 start_byte += cursor - base + 1;
17169 *byte_pos_ptr = start_byte;
17170 /* When scanning backwards, we should
17171 not count the newline posterior to which we stop. */
17172 return - orig_count - 1;
17173 }
17174 }
17175 else
17176 break;
17177 }
17178 /* Here we add 1 to compensate for the last decrement
17179 of CURSOR, which took it past the valid range. */
17180 start_byte += cursor - base + 1;
17181 }
17182 }
17183
17184 *byte_pos_ptr = limit_byte;
17185
17186 if (count < 0)
17187 return - orig_count + count;
17188 return orig_count - count;
17189
17190 }
17191
17192
17193 \f
17194 /***********************************************************************
17195 Displaying strings
17196 ***********************************************************************/
17197
17198 /* Display a NUL-terminated string, starting with index START.
17199
17200 If STRING is non-null, display that C string. Otherwise, the Lisp
17201 string LISP_STRING is displayed.
17202
17203 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17204 FACE_STRING. Display STRING or LISP_STRING with the face at
17205 FACE_STRING_POS in FACE_STRING:
17206
17207 Display the string in the environment given by IT, but use the
17208 standard display table, temporarily.
17209
17210 FIELD_WIDTH is the minimum number of output glyphs to produce.
17211 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17212 with spaces. If STRING has more characters, more than FIELD_WIDTH
17213 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17214
17215 PRECISION is the maximum number of characters to output from
17216 STRING. PRECISION < 0 means don't truncate the string.
17217
17218 This is roughly equivalent to printf format specifiers:
17219
17220 FIELD_WIDTH PRECISION PRINTF
17221 ----------------------------------------
17222 -1 -1 %s
17223 -1 10 %.10s
17224 10 -1 %10s
17225 20 10 %20.10s
17226
17227 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17228 display them, and < 0 means obey the current buffer's value of
17229 enable_multibyte_characters.
17230
17231 Value is the number of glyphs produced. */
17232
17233 static int
17234 display_string (string, lisp_string, face_string, face_string_pos,
17235 start, it, field_width, precision, max_x, multibyte)
17236 unsigned char *string;
17237 Lisp_Object lisp_string;
17238 Lisp_Object face_string;
17239 int face_string_pos;
17240 int start;
17241 struct it *it;
17242 int field_width, precision, max_x;
17243 int multibyte;
17244 {
17245 int hpos_at_start = it->hpos;
17246 int saved_face_id = it->face_id;
17247 struct glyph_row *row = it->glyph_row;
17248
17249 /* Initialize the iterator IT for iteration over STRING beginning
17250 with index START. */
17251 reseat_to_string (it, string, lisp_string, start,
17252 precision, field_width, multibyte);
17253
17254 /* If displaying STRING, set up the face of the iterator
17255 from LISP_STRING, if that's given. */
17256 if (STRINGP (face_string))
17257 {
17258 int endptr;
17259 struct face *face;
17260
17261 it->face_id
17262 = face_at_string_position (it->w, face_string, face_string_pos,
17263 0, it->region_beg_charpos,
17264 it->region_end_charpos,
17265 &endptr, it->base_face_id, 0);
17266 face = FACE_FROM_ID (it->f, it->face_id);
17267 it->face_box_p = face->box != FACE_NO_BOX;
17268 }
17269
17270 /* Set max_x to the maximum allowed X position. Don't let it go
17271 beyond the right edge of the window. */
17272 if (max_x <= 0)
17273 max_x = it->last_visible_x;
17274 else
17275 max_x = min (max_x, it->last_visible_x);
17276
17277 /* Skip over display elements that are not visible. because IT->w is
17278 hscrolled. */
17279 if (it->current_x < it->first_visible_x)
17280 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17281 MOVE_TO_POS | MOVE_TO_X);
17282
17283 row->ascent = it->max_ascent;
17284 row->height = it->max_ascent + it->max_descent;
17285 row->phys_ascent = it->max_phys_ascent;
17286 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17287 row->extra_line_spacing = it->max_extra_line_spacing;
17288
17289 /* This condition is for the case that we are called with current_x
17290 past last_visible_x. */
17291 while (it->current_x < max_x)
17292 {
17293 int x_before, x, n_glyphs_before, i, nglyphs;
17294
17295 /* Get the next display element. */
17296 if (!get_next_display_element (it))
17297 break;
17298
17299 /* Produce glyphs. */
17300 x_before = it->current_x;
17301 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17302 PRODUCE_GLYPHS (it);
17303
17304 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17305 i = 0;
17306 x = x_before;
17307 while (i < nglyphs)
17308 {
17309 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17310
17311 if (!it->truncate_lines_p
17312 && x + glyph->pixel_width > max_x)
17313 {
17314 /* End of continued line or max_x reached. */
17315 if (CHAR_GLYPH_PADDING_P (*glyph))
17316 {
17317 /* A wide character is unbreakable. */
17318 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17319 it->current_x = x_before;
17320 }
17321 else
17322 {
17323 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17324 it->current_x = x;
17325 }
17326 break;
17327 }
17328 else if (x + glyph->pixel_width > it->first_visible_x)
17329 {
17330 /* Glyph is at least partially visible. */
17331 ++it->hpos;
17332 if (x < it->first_visible_x)
17333 it->glyph_row->x = x - it->first_visible_x;
17334 }
17335 else
17336 {
17337 /* Glyph is off the left margin of the display area.
17338 Should not happen. */
17339 abort ();
17340 }
17341
17342 row->ascent = max (row->ascent, it->max_ascent);
17343 row->height = max (row->height, it->max_ascent + it->max_descent);
17344 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17345 row->phys_height = max (row->phys_height,
17346 it->max_phys_ascent + it->max_phys_descent);
17347 row->extra_line_spacing = max (row->extra_line_spacing,
17348 it->max_extra_line_spacing);
17349 x += glyph->pixel_width;
17350 ++i;
17351 }
17352
17353 /* Stop if max_x reached. */
17354 if (i < nglyphs)
17355 break;
17356
17357 /* Stop at line ends. */
17358 if (ITERATOR_AT_END_OF_LINE_P (it))
17359 {
17360 it->continuation_lines_width = 0;
17361 break;
17362 }
17363
17364 set_iterator_to_next (it, 1);
17365
17366 /* Stop if truncating at the right edge. */
17367 if (it->truncate_lines_p
17368 && it->current_x >= it->last_visible_x)
17369 {
17370 /* Add truncation mark, but don't do it if the line is
17371 truncated at a padding space. */
17372 if (IT_CHARPOS (*it) < it->string_nchars)
17373 {
17374 if (!FRAME_WINDOW_P (it->f))
17375 {
17376 int i, n;
17377
17378 if (it->current_x > it->last_visible_x)
17379 {
17380 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17381 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17382 break;
17383 for (n = row->used[TEXT_AREA]; i < n; ++i)
17384 {
17385 row->used[TEXT_AREA] = i;
17386 produce_special_glyphs (it, IT_TRUNCATION);
17387 }
17388 }
17389 produce_special_glyphs (it, IT_TRUNCATION);
17390 }
17391 it->glyph_row->truncated_on_right_p = 1;
17392 }
17393 break;
17394 }
17395 }
17396
17397 /* Maybe insert a truncation at the left. */
17398 if (it->first_visible_x
17399 && IT_CHARPOS (*it) > 0)
17400 {
17401 if (!FRAME_WINDOW_P (it->f))
17402 insert_left_trunc_glyphs (it);
17403 it->glyph_row->truncated_on_left_p = 1;
17404 }
17405
17406 it->face_id = saved_face_id;
17407
17408 /* Value is number of columns displayed. */
17409 return it->hpos - hpos_at_start;
17410 }
17411
17412
17413 \f
17414 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17415 appears as an element of LIST or as the car of an element of LIST.
17416 If PROPVAL is a list, compare each element against LIST in that
17417 way, and return 1/2 if any element of PROPVAL is found in LIST.
17418 Otherwise return 0. This function cannot quit.
17419 The return value is 2 if the text is invisible but with an ellipsis
17420 and 1 if it's invisible and without an ellipsis. */
17421
17422 int
17423 invisible_p (propval, list)
17424 register Lisp_Object propval;
17425 Lisp_Object list;
17426 {
17427 register Lisp_Object tail, proptail;
17428
17429 for (tail = list; CONSP (tail); tail = XCDR (tail))
17430 {
17431 register Lisp_Object tem;
17432 tem = XCAR (tail);
17433 if (EQ (propval, tem))
17434 return 1;
17435 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17436 return NILP (XCDR (tem)) ? 1 : 2;
17437 }
17438
17439 if (CONSP (propval))
17440 {
17441 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17442 {
17443 Lisp_Object propelt;
17444 propelt = XCAR (proptail);
17445 for (tail = list; CONSP (tail); tail = XCDR (tail))
17446 {
17447 register Lisp_Object tem;
17448 tem = XCAR (tail);
17449 if (EQ (propelt, tem))
17450 return 1;
17451 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17452 return NILP (XCDR (tem)) ? 1 : 2;
17453 }
17454 }
17455 }
17456
17457 return 0;
17458 }
17459
17460 /* Calculate a width or height in pixels from a specification using
17461 the following elements:
17462
17463 SPEC ::=
17464 NUM - a (fractional) multiple of the default font width/height
17465 (NUM) - specifies exactly NUM pixels
17466 UNIT - a fixed number of pixels, see below.
17467 ELEMENT - size of a display element in pixels, see below.
17468 (NUM . SPEC) - equals NUM * SPEC
17469 (+ SPEC SPEC ...) - add pixel values
17470 (- SPEC SPEC ...) - subtract pixel values
17471 (- SPEC) - negate pixel value
17472
17473 NUM ::=
17474 INT or FLOAT - a number constant
17475 SYMBOL - use symbol's (buffer local) variable binding.
17476
17477 UNIT ::=
17478 in - pixels per inch *)
17479 mm - pixels per 1/1000 meter *)
17480 cm - pixels per 1/100 meter *)
17481 width - width of current font in pixels.
17482 height - height of current font in pixels.
17483
17484 *) using the ratio(s) defined in display-pixels-per-inch.
17485
17486 ELEMENT ::=
17487
17488 left-fringe - left fringe width in pixels
17489 right-fringe - right fringe width in pixels
17490
17491 left-margin - left margin width in pixels
17492 right-margin - right margin width in pixels
17493
17494 scroll-bar - scroll-bar area width in pixels
17495
17496 Examples:
17497
17498 Pixels corresponding to 5 inches:
17499 (5 . in)
17500
17501 Total width of non-text areas on left side of window (if scroll-bar is on left):
17502 '(space :width (+ left-fringe left-margin scroll-bar))
17503
17504 Align to first text column (in header line):
17505 '(space :align-to 0)
17506
17507 Align to middle of text area minus half the width of variable `my-image'
17508 containing a loaded image:
17509 '(space :align-to (0.5 . (- text my-image)))
17510
17511 Width of left margin minus width of 1 character in the default font:
17512 '(space :width (- left-margin 1))
17513
17514 Width of left margin minus width of 2 characters in the current font:
17515 '(space :width (- left-margin (2 . width)))
17516
17517 Center 1 character over left-margin (in header line):
17518 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17519
17520 Different ways to express width of left fringe plus left margin minus one pixel:
17521 '(space :width (- (+ left-fringe left-margin) (1)))
17522 '(space :width (+ left-fringe left-margin (- (1))))
17523 '(space :width (+ left-fringe left-margin (-1)))
17524
17525 */
17526
17527 #define NUMVAL(X) \
17528 ((INTEGERP (X) || FLOATP (X)) \
17529 ? XFLOATINT (X) \
17530 : - 1)
17531
17532 int
17533 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17534 double *res;
17535 struct it *it;
17536 Lisp_Object prop;
17537 void *font;
17538 int width_p, *align_to;
17539 {
17540 double pixels;
17541
17542 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17543 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17544
17545 if (NILP (prop))
17546 return OK_PIXELS (0);
17547
17548 if (SYMBOLP (prop))
17549 {
17550 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17551 {
17552 char *unit = SDATA (SYMBOL_NAME (prop));
17553
17554 if (unit[0] == 'i' && unit[1] == 'n')
17555 pixels = 1.0;
17556 else if (unit[0] == 'm' && unit[1] == 'm')
17557 pixels = 25.4;
17558 else if (unit[0] == 'c' && unit[1] == 'm')
17559 pixels = 2.54;
17560 else
17561 pixels = 0;
17562 if (pixels > 0)
17563 {
17564 double ppi;
17565 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17566 || (CONSP (Vdisplay_pixels_per_inch)
17567 && (ppi = (width_p
17568 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17569 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17570 ppi > 0)))
17571 return OK_PIXELS (ppi / pixels);
17572
17573 return 0;
17574 }
17575 }
17576
17577 #ifdef HAVE_WINDOW_SYSTEM
17578 if (EQ (prop, Qheight))
17579 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17580 if (EQ (prop, Qwidth))
17581 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17582 #else
17583 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17584 return OK_PIXELS (1);
17585 #endif
17586
17587 if (EQ (prop, Qtext))
17588 return OK_PIXELS (width_p
17589 ? window_box_width (it->w, TEXT_AREA)
17590 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17591
17592 if (align_to && *align_to < 0)
17593 {
17594 *res = 0;
17595 if (EQ (prop, Qleft))
17596 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17597 if (EQ (prop, Qright))
17598 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17599 if (EQ (prop, Qcenter))
17600 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17601 + window_box_width (it->w, TEXT_AREA) / 2);
17602 if (EQ (prop, Qleft_fringe))
17603 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17604 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17605 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17606 if (EQ (prop, Qright_fringe))
17607 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17608 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17609 : window_box_right_offset (it->w, TEXT_AREA));
17610 if (EQ (prop, Qleft_margin))
17611 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17612 if (EQ (prop, Qright_margin))
17613 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17614 if (EQ (prop, Qscroll_bar))
17615 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17616 ? 0
17617 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17618 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17619 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17620 : 0)));
17621 }
17622 else
17623 {
17624 if (EQ (prop, Qleft_fringe))
17625 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17626 if (EQ (prop, Qright_fringe))
17627 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17628 if (EQ (prop, Qleft_margin))
17629 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17630 if (EQ (prop, Qright_margin))
17631 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17632 if (EQ (prop, Qscroll_bar))
17633 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17634 }
17635
17636 prop = Fbuffer_local_value (prop, it->w->buffer);
17637 }
17638
17639 if (INTEGERP (prop) || FLOATP (prop))
17640 {
17641 int base_unit = (width_p
17642 ? FRAME_COLUMN_WIDTH (it->f)
17643 : FRAME_LINE_HEIGHT (it->f));
17644 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17645 }
17646
17647 if (CONSP (prop))
17648 {
17649 Lisp_Object car = XCAR (prop);
17650 Lisp_Object cdr = XCDR (prop);
17651
17652 if (SYMBOLP (car))
17653 {
17654 #ifdef HAVE_WINDOW_SYSTEM
17655 if (valid_image_p (prop))
17656 {
17657 int id = lookup_image (it->f, prop);
17658 struct image *img = IMAGE_FROM_ID (it->f, id);
17659
17660 return OK_PIXELS (width_p ? img->width : img->height);
17661 }
17662 #endif
17663 if (EQ (car, Qplus) || EQ (car, Qminus))
17664 {
17665 int first = 1;
17666 double px;
17667
17668 pixels = 0;
17669 while (CONSP (cdr))
17670 {
17671 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17672 font, width_p, align_to))
17673 return 0;
17674 if (first)
17675 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17676 else
17677 pixels += px;
17678 cdr = XCDR (cdr);
17679 }
17680 if (EQ (car, Qminus))
17681 pixels = -pixels;
17682 return OK_PIXELS (pixels);
17683 }
17684
17685 car = Fbuffer_local_value (car, it->w->buffer);
17686 }
17687
17688 if (INTEGERP (car) || FLOATP (car))
17689 {
17690 double fact;
17691 pixels = XFLOATINT (car);
17692 if (NILP (cdr))
17693 return OK_PIXELS (pixels);
17694 if (calc_pixel_width_or_height (&fact, it, cdr,
17695 font, width_p, align_to))
17696 return OK_PIXELS (pixels * fact);
17697 return 0;
17698 }
17699
17700 return 0;
17701 }
17702
17703 return 0;
17704 }
17705
17706 \f
17707 /***********************************************************************
17708 Glyph Display
17709 ***********************************************************************/
17710
17711 #ifdef HAVE_WINDOW_SYSTEM
17712
17713 #if GLYPH_DEBUG
17714
17715 void
17716 dump_glyph_string (s)
17717 struct glyph_string *s;
17718 {
17719 fprintf (stderr, "glyph string\n");
17720 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17721 s->x, s->y, s->width, s->height);
17722 fprintf (stderr, " ybase = %d\n", s->ybase);
17723 fprintf (stderr, " hl = %d\n", s->hl);
17724 fprintf (stderr, " left overhang = %d, right = %d\n",
17725 s->left_overhang, s->right_overhang);
17726 fprintf (stderr, " nchars = %d\n", s->nchars);
17727 fprintf (stderr, " extends to end of line = %d\n",
17728 s->extends_to_end_of_line_p);
17729 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17730 fprintf (stderr, " bg width = %d\n", s->background_width);
17731 }
17732
17733 #endif /* GLYPH_DEBUG */
17734
17735 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17736 of XChar2b structures for S; it can't be allocated in
17737 init_glyph_string because it must be allocated via `alloca'. W
17738 is the window on which S is drawn. ROW and AREA are the glyph row
17739 and area within the row from which S is constructed. START is the
17740 index of the first glyph structure covered by S. HL is a
17741 face-override for drawing S. */
17742
17743 #ifdef HAVE_NTGUI
17744 #define OPTIONAL_HDC(hdc) hdc,
17745 #define DECLARE_HDC(hdc) HDC hdc;
17746 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17747 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17748 #endif
17749
17750 #ifndef OPTIONAL_HDC
17751 #define OPTIONAL_HDC(hdc)
17752 #define DECLARE_HDC(hdc)
17753 #define ALLOCATE_HDC(hdc, f)
17754 #define RELEASE_HDC(hdc, f)
17755 #endif
17756
17757 static void
17758 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17759 struct glyph_string *s;
17760 DECLARE_HDC (hdc)
17761 XChar2b *char2b;
17762 struct window *w;
17763 struct glyph_row *row;
17764 enum glyph_row_area area;
17765 int start;
17766 enum draw_glyphs_face hl;
17767 {
17768 bzero (s, sizeof *s);
17769 s->w = w;
17770 s->f = XFRAME (w->frame);
17771 #ifdef HAVE_NTGUI
17772 s->hdc = hdc;
17773 #endif
17774 s->display = FRAME_X_DISPLAY (s->f);
17775 s->window = FRAME_X_WINDOW (s->f);
17776 s->char2b = char2b;
17777 s->hl = hl;
17778 s->row = row;
17779 s->area = area;
17780 s->first_glyph = row->glyphs[area] + start;
17781 s->height = row->height;
17782 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17783
17784 /* Display the internal border below the tool-bar window. */
17785 if (WINDOWP (s->f->tool_bar_window)
17786 && s->w == XWINDOW (s->f->tool_bar_window))
17787 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17788
17789 s->ybase = s->y + row->ascent;
17790 }
17791
17792
17793 /* Append the list of glyph strings with head H and tail T to the list
17794 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17795
17796 static INLINE void
17797 append_glyph_string_lists (head, tail, h, t)
17798 struct glyph_string **head, **tail;
17799 struct glyph_string *h, *t;
17800 {
17801 if (h)
17802 {
17803 if (*head)
17804 (*tail)->next = h;
17805 else
17806 *head = h;
17807 h->prev = *tail;
17808 *tail = t;
17809 }
17810 }
17811
17812
17813 /* Prepend the list of glyph strings with head H and tail T to the
17814 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17815 result. */
17816
17817 static INLINE void
17818 prepend_glyph_string_lists (head, tail, h, t)
17819 struct glyph_string **head, **tail;
17820 struct glyph_string *h, *t;
17821 {
17822 if (h)
17823 {
17824 if (*head)
17825 (*head)->prev = t;
17826 else
17827 *tail = t;
17828 t->next = *head;
17829 *head = h;
17830 }
17831 }
17832
17833
17834 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17835 Set *HEAD and *TAIL to the resulting list. */
17836
17837 static INLINE void
17838 append_glyph_string (head, tail, s)
17839 struct glyph_string **head, **tail;
17840 struct glyph_string *s;
17841 {
17842 s->next = s->prev = NULL;
17843 append_glyph_string_lists (head, tail, s, s);
17844 }
17845
17846
17847 /* Get face and two-byte form of character glyph GLYPH on frame F.
17848 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17849 a pointer to a realized face that is ready for display. */
17850
17851 static INLINE struct face *
17852 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17853 struct frame *f;
17854 struct glyph *glyph;
17855 XChar2b *char2b;
17856 int *two_byte_p;
17857 {
17858 struct face *face;
17859
17860 xassert (glyph->type == CHAR_GLYPH);
17861 face = FACE_FROM_ID (f, glyph->face_id);
17862
17863 if (two_byte_p)
17864 *two_byte_p = 0;
17865
17866 if (!glyph->multibyte_p)
17867 {
17868 /* Unibyte case. We don't have to encode, but we have to make
17869 sure to use a face suitable for unibyte. */
17870 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17871 }
17872 else if (glyph->u.ch < 128
17873 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17874 {
17875 /* Case of ASCII in a face known to fit ASCII. */
17876 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17877 }
17878 else
17879 {
17880 int c1, c2, charset;
17881
17882 /* Split characters into bytes. If c2 is -1 afterwards, C is
17883 really a one-byte character so that byte1 is zero. */
17884 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17885 if (c2 > 0)
17886 STORE_XCHAR2B (char2b, c1, c2);
17887 else
17888 STORE_XCHAR2B (char2b, 0, c1);
17889
17890 /* Maybe encode the character in *CHAR2B. */
17891 if (charset != CHARSET_ASCII)
17892 {
17893 struct font_info *font_info
17894 = FONT_INFO_FROM_ID (f, face->font_info_id);
17895 if (font_info)
17896 glyph->font_type
17897 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17898 }
17899 }
17900
17901 /* Make sure X resources of the face are allocated. */
17902 xassert (face != NULL);
17903 PREPARE_FACE_FOR_DISPLAY (f, face);
17904 return face;
17905 }
17906
17907
17908 /* Fill glyph string S with composition components specified by S->cmp.
17909
17910 FACES is an array of faces for all components of this composition.
17911 S->gidx is the index of the first component for S.
17912 OVERLAPS_P non-zero means S should draw the foreground only, and
17913 use its physical height for clipping.
17914
17915 Value is the index of a component not in S. */
17916
17917 static int
17918 fill_composite_glyph_string (s, faces, overlaps_p)
17919 struct glyph_string *s;
17920 struct face **faces;
17921 int overlaps_p;
17922 {
17923 int i;
17924
17925 xassert (s);
17926
17927 s->for_overlaps_p = overlaps_p;
17928
17929 s->face = faces[s->gidx];
17930 s->font = s->face->font;
17931 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17932
17933 /* For all glyphs of this composition, starting at the offset
17934 S->gidx, until we reach the end of the definition or encounter a
17935 glyph that requires the different face, add it to S. */
17936 ++s->nchars;
17937 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17938 ++s->nchars;
17939
17940 /* All glyph strings for the same composition has the same width,
17941 i.e. the width set for the first component of the composition. */
17942
17943 s->width = s->first_glyph->pixel_width;
17944
17945 /* If the specified font could not be loaded, use the frame's
17946 default font, but record the fact that we couldn't load it in
17947 the glyph string so that we can draw rectangles for the
17948 characters of the glyph string. */
17949 if (s->font == NULL)
17950 {
17951 s->font_not_found_p = 1;
17952 s->font = FRAME_FONT (s->f);
17953 }
17954
17955 /* Adjust base line for subscript/superscript text. */
17956 s->ybase += s->first_glyph->voffset;
17957
17958 xassert (s->face && s->face->gc);
17959
17960 /* This glyph string must always be drawn with 16-bit functions. */
17961 s->two_byte_p = 1;
17962
17963 return s->gidx + s->nchars;
17964 }
17965
17966
17967 /* Fill glyph string S from a sequence of character glyphs.
17968
17969 FACE_ID is the face id of the string. START is the index of the
17970 first glyph to consider, END is the index of the last + 1.
17971 OVERLAPS_P non-zero means S should draw the foreground only, and
17972 use its physical height for clipping.
17973
17974 Value is the index of the first glyph not in S. */
17975
17976 static int
17977 fill_glyph_string (s, face_id, start, end, overlaps_p)
17978 struct glyph_string *s;
17979 int face_id;
17980 int start, end, overlaps_p;
17981 {
17982 struct glyph *glyph, *last;
17983 int voffset;
17984 int glyph_not_available_p;
17985
17986 xassert (s->f == XFRAME (s->w->frame));
17987 xassert (s->nchars == 0);
17988 xassert (start >= 0 && end > start);
17989
17990 s->for_overlaps_p = overlaps_p,
17991 glyph = s->row->glyphs[s->area] + start;
17992 last = s->row->glyphs[s->area] + end;
17993 voffset = glyph->voffset;
17994
17995 glyph_not_available_p = glyph->glyph_not_available_p;
17996
17997 while (glyph < last
17998 && glyph->type == CHAR_GLYPH
17999 && glyph->voffset == voffset
18000 /* Same face id implies same font, nowadays. */
18001 && glyph->face_id == face_id
18002 && glyph->glyph_not_available_p == glyph_not_available_p)
18003 {
18004 int two_byte_p;
18005
18006 s->face = get_glyph_face_and_encoding (s->f, glyph,
18007 s->char2b + s->nchars,
18008 &two_byte_p);
18009 s->two_byte_p = two_byte_p;
18010 ++s->nchars;
18011 xassert (s->nchars <= end - start);
18012 s->width += glyph->pixel_width;
18013 ++glyph;
18014 }
18015
18016 s->font = s->face->font;
18017 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18018
18019 /* If the specified font could not be loaded, use the frame's font,
18020 but record the fact that we couldn't load it in
18021 S->font_not_found_p so that we can draw rectangles for the
18022 characters of the glyph string. */
18023 if (s->font == NULL || glyph_not_available_p)
18024 {
18025 s->font_not_found_p = 1;
18026 s->font = FRAME_FONT (s->f);
18027 }
18028
18029 /* Adjust base line for subscript/superscript text. */
18030 s->ybase += voffset;
18031
18032 xassert (s->face && s->face->gc);
18033 return glyph - s->row->glyphs[s->area];
18034 }
18035
18036
18037 /* Fill glyph string S from image glyph S->first_glyph. */
18038
18039 static void
18040 fill_image_glyph_string (s)
18041 struct glyph_string *s;
18042 {
18043 xassert (s->first_glyph->type == IMAGE_GLYPH);
18044 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18045 xassert (s->img);
18046 s->slice = s->first_glyph->slice;
18047 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18048 s->font = s->face->font;
18049 s->width = s->first_glyph->pixel_width;
18050
18051 /* Adjust base line for subscript/superscript text. */
18052 s->ybase += s->first_glyph->voffset;
18053 }
18054
18055
18056 /* Fill glyph string S from a sequence of stretch glyphs.
18057
18058 ROW is the glyph row in which the glyphs are found, AREA is the
18059 area within the row. START is the index of the first glyph to
18060 consider, END is the index of the last + 1.
18061
18062 Value is the index of the first glyph not in S. */
18063
18064 static int
18065 fill_stretch_glyph_string (s, row, area, start, end)
18066 struct glyph_string *s;
18067 struct glyph_row *row;
18068 enum glyph_row_area area;
18069 int start, end;
18070 {
18071 struct glyph *glyph, *last;
18072 int voffset, face_id;
18073
18074 xassert (s->first_glyph->type == STRETCH_GLYPH);
18075
18076 glyph = s->row->glyphs[s->area] + start;
18077 last = s->row->glyphs[s->area] + end;
18078 face_id = glyph->face_id;
18079 s->face = FACE_FROM_ID (s->f, face_id);
18080 s->font = s->face->font;
18081 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18082 s->width = glyph->pixel_width;
18083 voffset = glyph->voffset;
18084
18085 for (++glyph;
18086 (glyph < last
18087 && glyph->type == STRETCH_GLYPH
18088 && glyph->voffset == voffset
18089 && glyph->face_id == face_id);
18090 ++glyph)
18091 s->width += glyph->pixel_width;
18092
18093 /* Adjust base line for subscript/superscript text. */
18094 s->ybase += voffset;
18095
18096 /* The case that face->gc == 0 is handled when drawing the glyph
18097 string by calling PREPARE_FACE_FOR_DISPLAY. */
18098 xassert (s->face);
18099 return glyph - s->row->glyphs[s->area];
18100 }
18101
18102
18103 /* EXPORT for RIF:
18104 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18105 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18106 assumed to be zero. */
18107
18108 void
18109 x_get_glyph_overhangs (glyph, f, left, right)
18110 struct glyph *glyph;
18111 struct frame *f;
18112 int *left, *right;
18113 {
18114 *left = *right = 0;
18115
18116 if (glyph->type == CHAR_GLYPH)
18117 {
18118 XFontStruct *font;
18119 struct face *face;
18120 struct font_info *font_info;
18121 XChar2b char2b;
18122 XCharStruct *pcm;
18123
18124 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18125 font = face->font;
18126 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18127 if (font /* ++KFS: Should this be font_info ? */
18128 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
18129 {
18130 if (pcm->rbearing > pcm->width)
18131 *right = pcm->rbearing - pcm->width;
18132 if (pcm->lbearing < 0)
18133 *left = -pcm->lbearing;
18134 }
18135 }
18136 }
18137
18138
18139 /* Return the index of the first glyph preceding glyph string S that
18140 is overwritten by S because of S's left overhang. Value is -1
18141 if no glyphs are overwritten. */
18142
18143 static int
18144 left_overwritten (s)
18145 struct glyph_string *s;
18146 {
18147 int k;
18148
18149 if (s->left_overhang)
18150 {
18151 int x = 0, i;
18152 struct glyph *glyphs = s->row->glyphs[s->area];
18153 int first = s->first_glyph - glyphs;
18154
18155 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18156 x -= glyphs[i].pixel_width;
18157
18158 k = i + 1;
18159 }
18160 else
18161 k = -1;
18162
18163 return k;
18164 }
18165
18166
18167 /* Return the index of the first glyph preceding glyph string S that
18168 is overwriting S because of its right overhang. Value is -1 if no
18169 glyph in front of S overwrites S. */
18170
18171 static int
18172 left_overwriting (s)
18173 struct glyph_string *s;
18174 {
18175 int i, k, x;
18176 struct glyph *glyphs = s->row->glyphs[s->area];
18177 int first = s->first_glyph - glyphs;
18178
18179 k = -1;
18180 x = 0;
18181 for (i = first - 1; i >= 0; --i)
18182 {
18183 int left, right;
18184 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18185 if (x + right > 0)
18186 k = i;
18187 x -= glyphs[i].pixel_width;
18188 }
18189
18190 return k;
18191 }
18192
18193
18194 /* Return the index of the last glyph following glyph string S that is
18195 not overwritten by S because of S's right overhang. Value is -1 if
18196 no such glyph is found. */
18197
18198 static int
18199 right_overwritten (s)
18200 struct glyph_string *s;
18201 {
18202 int k = -1;
18203
18204 if (s->right_overhang)
18205 {
18206 int x = 0, i;
18207 struct glyph *glyphs = s->row->glyphs[s->area];
18208 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18209 int end = s->row->used[s->area];
18210
18211 for (i = first; i < end && s->right_overhang > x; ++i)
18212 x += glyphs[i].pixel_width;
18213
18214 k = i;
18215 }
18216
18217 return k;
18218 }
18219
18220
18221 /* Return the index of the last glyph following glyph string S that
18222 overwrites S because of its left overhang. Value is negative
18223 if no such glyph is found. */
18224
18225 static int
18226 right_overwriting (s)
18227 struct glyph_string *s;
18228 {
18229 int i, k, x;
18230 int end = s->row->used[s->area];
18231 struct glyph *glyphs = s->row->glyphs[s->area];
18232 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18233
18234 k = -1;
18235 x = 0;
18236 for (i = first; i < end; ++i)
18237 {
18238 int left, right;
18239 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18240 if (x - left < 0)
18241 k = i;
18242 x += glyphs[i].pixel_width;
18243 }
18244
18245 return k;
18246 }
18247
18248
18249 /* Get face and two-byte form of character C in face FACE_ID on frame
18250 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18251 means we want to display multibyte text. DISPLAY_P non-zero means
18252 make sure that X resources for the face returned are allocated.
18253 Value is a pointer to a realized face that is ready for display if
18254 DISPLAY_P is non-zero. */
18255
18256 static INLINE struct face *
18257 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18258 struct frame *f;
18259 int c, face_id;
18260 XChar2b *char2b;
18261 int multibyte_p, display_p;
18262 {
18263 struct face *face = FACE_FROM_ID (f, face_id);
18264
18265 if (!multibyte_p)
18266 {
18267 /* Unibyte case. We don't have to encode, but we have to make
18268 sure to use a face suitable for unibyte. */
18269 STORE_XCHAR2B (char2b, 0, c);
18270 face_id = FACE_FOR_CHAR (f, face, c);
18271 face = FACE_FROM_ID (f, face_id);
18272 }
18273 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18274 {
18275 /* Case of ASCII in a face known to fit ASCII. */
18276 STORE_XCHAR2B (char2b, 0, c);
18277 }
18278 else
18279 {
18280 int c1, c2, charset;
18281
18282 /* Split characters into bytes. If c2 is -1 afterwards, C is
18283 really a one-byte character so that byte1 is zero. */
18284 SPLIT_CHAR (c, charset, c1, c2);
18285 if (c2 > 0)
18286 STORE_XCHAR2B (char2b, c1, c2);
18287 else
18288 STORE_XCHAR2B (char2b, 0, c1);
18289
18290 /* Maybe encode the character in *CHAR2B. */
18291 if (face->font != NULL)
18292 {
18293 struct font_info *font_info
18294 = FONT_INFO_FROM_ID (f, face->font_info_id);
18295 if (font_info)
18296 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
18297 }
18298 }
18299
18300 /* Make sure X resources of the face are allocated. */
18301 #ifdef HAVE_X_WINDOWS
18302 if (display_p)
18303 #endif
18304 {
18305 xassert (face != NULL);
18306 PREPARE_FACE_FOR_DISPLAY (f, face);
18307 }
18308
18309 return face;
18310 }
18311
18312
18313 /* Set background width of glyph string S. START is the index of the
18314 first glyph following S. LAST_X is the right-most x-position + 1
18315 in the drawing area. */
18316
18317 static INLINE void
18318 set_glyph_string_background_width (s, start, last_x)
18319 struct glyph_string *s;
18320 int start;
18321 int last_x;
18322 {
18323 /* If the face of this glyph string has to be drawn to the end of
18324 the drawing area, set S->extends_to_end_of_line_p. */
18325 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18326
18327 if (start == s->row->used[s->area]
18328 && s->area == TEXT_AREA
18329 && ((s->hl == DRAW_NORMAL_TEXT
18330 && (s->row->fill_line_p
18331 || s->face->background != default_face->background
18332 || s->face->stipple != default_face->stipple
18333 || s->row->mouse_face_p))
18334 || s->hl == DRAW_MOUSE_FACE
18335 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18336 && s->row->fill_line_p)))
18337 s->extends_to_end_of_line_p = 1;
18338
18339 /* If S extends its face to the end of the line, set its
18340 background_width to the distance to the right edge of the drawing
18341 area. */
18342 if (s->extends_to_end_of_line_p)
18343 s->background_width = last_x - s->x + 1;
18344 else
18345 s->background_width = s->width;
18346 }
18347
18348
18349 /* Compute overhangs and x-positions for glyph string S and its
18350 predecessors, or successors. X is the starting x-position for S.
18351 BACKWARD_P non-zero means process predecessors. */
18352
18353 static void
18354 compute_overhangs_and_x (s, x, backward_p)
18355 struct glyph_string *s;
18356 int x;
18357 int backward_p;
18358 {
18359 if (backward_p)
18360 {
18361 while (s)
18362 {
18363 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18364 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18365 x -= s->width;
18366 s->x = x;
18367 s = s->prev;
18368 }
18369 }
18370 else
18371 {
18372 while (s)
18373 {
18374 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18375 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18376 s->x = x;
18377 x += s->width;
18378 s = s->next;
18379 }
18380 }
18381 }
18382
18383
18384
18385 /* The following macros are only called from draw_glyphs below.
18386 They reference the following parameters of that function directly:
18387 `w', `row', `area', and `overlap_p'
18388 as well as the following local variables:
18389 `s', `f', and `hdc' (in W32) */
18390
18391 #ifdef HAVE_NTGUI
18392 /* On W32, silently add local `hdc' variable to argument list of
18393 init_glyph_string. */
18394 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18395 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18396 #else
18397 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18398 init_glyph_string (s, char2b, w, row, area, start, hl)
18399 #endif
18400
18401 /* Add a glyph string for a stretch glyph to the list of strings
18402 between HEAD and TAIL. START is the index of the stretch glyph in
18403 row area AREA of glyph row ROW. END is the index of the last glyph
18404 in that glyph row area. X is the current output position assigned
18405 to the new glyph string constructed. HL overrides that face of the
18406 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18407 is the right-most x-position of the drawing area. */
18408
18409 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18410 and below -- keep them on one line. */
18411 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18412 do \
18413 { \
18414 s = (struct glyph_string *) alloca (sizeof *s); \
18415 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18416 START = fill_stretch_glyph_string (s, row, area, START, END); \
18417 append_glyph_string (&HEAD, &TAIL, s); \
18418 s->x = (X); \
18419 } \
18420 while (0)
18421
18422
18423 /* Add a glyph string for an image glyph to the list of strings
18424 between HEAD and TAIL. START is the index of the image glyph in
18425 row area AREA of glyph row ROW. END is the index of the last glyph
18426 in that glyph row area. X is the current output position assigned
18427 to the new glyph string constructed. HL overrides that face of the
18428 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18429 is the right-most x-position of the drawing area. */
18430
18431 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18432 do \
18433 { \
18434 s = (struct glyph_string *) alloca (sizeof *s); \
18435 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18436 fill_image_glyph_string (s); \
18437 append_glyph_string (&HEAD, &TAIL, s); \
18438 ++START; \
18439 s->x = (X); \
18440 } \
18441 while (0)
18442
18443
18444 /* Add a glyph string for a sequence of character glyphs to the list
18445 of strings between HEAD and TAIL. START is the index of the first
18446 glyph in row area AREA of glyph row ROW that is part of the new
18447 glyph string. END is the index of the last glyph in that glyph row
18448 area. X is the current output position assigned to the new glyph
18449 string constructed. HL overrides that face of the glyph; e.g. it
18450 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18451 right-most x-position of the drawing area. */
18452
18453 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18454 do \
18455 { \
18456 int c, face_id; \
18457 XChar2b *char2b; \
18458 \
18459 c = (row)->glyphs[area][START].u.ch; \
18460 face_id = (row)->glyphs[area][START].face_id; \
18461 \
18462 s = (struct glyph_string *) alloca (sizeof *s); \
18463 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18464 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18465 append_glyph_string (&HEAD, &TAIL, s); \
18466 s->x = (X); \
18467 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18468 } \
18469 while (0)
18470
18471
18472 /* Add a glyph string for a composite sequence to the list of strings
18473 between HEAD and TAIL. START is the index of the first glyph in
18474 row area AREA of glyph row ROW that is part of the new glyph
18475 string. END is the index of the last glyph in that glyph row area.
18476 X is the current output position assigned to the new glyph string
18477 constructed. HL overrides that face of the glyph; e.g. it is
18478 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18479 x-position of the drawing area. */
18480
18481 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18482 do { \
18483 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18484 int face_id = (row)->glyphs[area][START].face_id; \
18485 struct face *base_face = FACE_FROM_ID (f, face_id); \
18486 struct composition *cmp = composition_table[cmp_id]; \
18487 int glyph_len = cmp->glyph_len; \
18488 XChar2b *char2b; \
18489 struct face **faces; \
18490 struct glyph_string *first_s = NULL; \
18491 int n; \
18492 \
18493 base_face = base_face->ascii_face; \
18494 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18495 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18496 /* At first, fill in `char2b' and `faces'. */ \
18497 for (n = 0; n < glyph_len; n++) \
18498 { \
18499 int c = COMPOSITION_GLYPH (cmp, n); \
18500 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18501 faces[n] = FACE_FROM_ID (f, this_face_id); \
18502 get_char_face_and_encoding (f, c, this_face_id, \
18503 char2b + n, 1, 1); \
18504 } \
18505 \
18506 /* Make glyph_strings for each glyph sequence that is drawable by \
18507 the same face, and append them to HEAD/TAIL. */ \
18508 for (n = 0; n < cmp->glyph_len;) \
18509 { \
18510 s = (struct glyph_string *) alloca (sizeof *s); \
18511 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18512 append_glyph_string (&(HEAD), &(TAIL), s); \
18513 s->cmp = cmp; \
18514 s->gidx = n; \
18515 s->x = (X); \
18516 \
18517 if (n == 0) \
18518 first_s = s; \
18519 \
18520 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18521 } \
18522 \
18523 ++START; \
18524 s = first_s; \
18525 } while (0)
18526
18527
18528 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18529 of AREA of glyph row ROW on window W between indices START and END.
18530 HL overrides the face for drawing glyph strings, e.g. it is
18531 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18532 x-positions of the drawing area.
18533
18534 This is an ugly monster macro construct because we must use alloca
18535 to allocate glyph strings (because draw_glyphs can be called
18536 asynchronously). */
18537
18538 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18539 do \
18540 { \
18541 HEAD = TAIL = NULL; \
18542 while (START < END) \
18543 { \
18544 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18545 switch (first_glyph->type) \
18546 { \
18547 case CHAR_GLYPH: \
18548 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18549 HL, X, LAST_X); \
18550 break; \
18551 \
18552 case COMPOSITE_GLYPH: \
18553 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18554 HL, X, LAST_X); \
18555 break; \
18556 \
18557 case STRETCH_GLYPH: \
18558 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18559 HL, X, LAST_X); \
18560 break; \
18561 \
18562 case IMAGE_GLYPH: \
18563 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18564 HL, X, LAST_X); \
18565 break; \
18566 \
18567 default: \
18568 abort (); \
18569 } \
18570 \
18571 set_glyph_string_background_width (s, START, LAST_X); \
18572 (X) += s->width; \
18573 } \
18574 } \
18575 while (0)
18576
18577
18578 /* Draw glyphs between START and END in AREA of ROW on window W,
18579 starting at x-position X. X is relative to AREA in W. HL is a
18580 face-override with the following meaning:
18581
18582 DRAW_NORMAL_TEXT draw normally
18583 DRAW_CURSOR draw in cursor face
18584 DRAW_MOUSE_FACE draw in mouse face.
18585 DRAW_INVERSE_VIDEO draw in mode line face
18586 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18587 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18588
18589 If OVERLAPS_P is non-zero, draw only the foreground of characters
18590 and clip to the physical height of ROW.
18591
18592 Value is the x-position reached, relative to AREA of W. */
18593
18594 static int
18595 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18596 struct window *w;
18597 int x;
18598 struct glyph_row *row;
18599 enum glyph_row_area area;
18600 int start, end;
18601 enum draw_glyphs_face hl;
18602 int overlaps_p;
18603 {
18604 struct glyph_string *head, *tail;
18605 struct glyph_string *s;
18606 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18607 int last_x, area_width;
18608 int x_reached;
18609 int i, j;
18610 struct frame *f = XFRAME (WINDOW_FRAME (w));
18611 DECLARE_HDC (hdc);
18612
18613 ALLOCATE_HDC (hdc, f);
18614
18615 /* Let's rather be paranoid than getting a SEGV. */
18616 end = min (end, row->used[area]);
18617 start = max (0, start);
18618 start = min (end, start);
18619
18620 /* Translate X to frame coordinates. Set last_x to the right
18621 end of the drawing area. */
18622 if (row->full_width_p)
18623 {
18624 /* X is relative to the left edge of W, without scroll bars
18625 or fringes. */
18626 x += WINDOW_LEFT_EDGE_X (w);
18627 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18628 }
18629 else
18630 {
18631 int area_left = window_box_left (w, area);
18632 x += area_left;
18633 area_width = window_box_width (w, area);
18634 last_x = area_left + area_width;
18635 }
18636
18637 /* Build a doubly-linked list of glyph_string structures between
18638 head and tail from what we have to draw. Note that the macro
18639 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18640 the reason we use a separate variable `i'. */
18641 i = start;
18642 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18643 if (tail)
18644 x_reached = tail->x + tail->background_width;
18645 else
18646 x_reached = x;
18647
18648 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18649 the row, redraw some glyphs in front or following the glyph
18650 strings built above. */
18651 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18652 {
18653 int dummy_x = 0;
18654 struct glyph_string *h, *t;
18655
18656 /* Compute overhangs for all glyph strings. */
18657 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
18658 for (s = head; s; s = s->next)
18659 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
18660
18661 /* Prepend glyph strings for glyphs in front of the first glyph
18662 string that are overwritten because of the first glyph
18663 string's left overhang. The background of all strings
18664 prepended must be drawn because the first glyph string
18665 draws over it. */
18666 i = left_overwritten (head);
18667 if (i >= 0)
18668 {
18669 j = i;
18670 BUILD_GLYPH_STRINGS (j, start, h, t,
18671 DRAW_NORMAL_TEXT, dummy_x, last_x);
18672 start = i;
18673 compute_overhangs_and_x (t, head->x, 1);
18674 prepend_glyph_string_lists (&head, &tail, h, t);
18675 clip_head = head;
18676 }
18677
18678 /* Prepend glyph strings for glyphs in front of the first glyph
18679 string that overwrite that glyph string because of their
18680 right overhang. For these strings, only the foreground must
18681 be drawn, because it draws over the glyph string at `head'.
18682 The background must not be drawn because this would overwrite
18683 right overhangs of preceding glyphs for which no glyph
18684 strings exist. */
18685 i = left_overwriting (head);
18686 if (i >= 0)
18687 {
18688 clip_head = head;
18689 BUILD_GLYPH_STRINGS (i, start, h, t,
18690 DRAW_NORMAL_TEXT, dummy_x, last_x);
18691 for (s = h; s; s = s->next)
18692 s->background_filled_p = 1;
18693 compute_overhangs_and_x (t, head->x, 1);
18694 prepend_glyph_string_lists (&head, &tail, h, t);
18695 }
18696
18697 /* Append glyphs strings for glyphs following the last glyph
18698 string tail that are overwritten by tail. The background of
18699 these strings has to be drawn because tail's foreground draws
18700 over it. */
18701 i = right_overwritten (tail);
18702 if (i >= 0)
18703 {
18704 BUILD_GLYPH_STRINGS (end, i, h, t,
18705 DRAW_NORMAL_TEXT, x, last_x);
18706 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18707 append_glyph_string_lists (&head, &tail, h, t);
18708 clip_tail = tail;
18709 }
18710
18711 /* Append glyph strings for glyphs following the last glyph
18712 string tail that overwrite tail. The foreground of such
18713 glyphs has to be drawn because it writes into the background
18714 of tail. The background must not be drawn because it could
18715 paint over the foreground of following glyphs. */
18716 i = right_overwriting (tail);
18717 if (i >= 0)
18718 {
18719 clip_tail = tail;
18720 BUILD_GLYPH_STRINGS (end, i, h, t,
18721 DRAW_NORMAL_TEXT, x, last_x);
18722 for (s = h; s; s = s->next)
18723 s->background_filled_p = 1;
18724 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18725 append_glyph_string_lists (&head, &tail, h, t);
18726 }
18727 if (clip_head || clip_tail)
18728 for (s = head; s; s = s->next)
18729 {
18730 s->clip_head = clip_head;
18731 s->clip_tail = clip_tail;
18732 }
18733 }
18734
18735 /* Draw all strings. */
18736 for (s = head; s; s = s->next)
18737 FRAME_RIF (f)->draw_glyph_string (s);
18738
18739 if (area == TEXT_AREA
18740 && !row->full_width_p
18741 /* When drawing overlapping rows, only the glyph strings'
18742 foreground is drawn, which doesn't erase a cursor
18743 completely. */
18744 && !overlaps_p)
18745 {
18746 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18747 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18748 : (tail ? tail->x + tail->background_width : x));
18749
18750 int text_left = window_box_left (w, TEXT_AREA);
18751 x0 -= text_left;
18752 x1 -= text_left;
18753
18754 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18755 row->y, MATRIX_ROW_BOTTOM_Y (row));
18756 }
18757
18758 /* Value is the x-position up to which drawn, relative to AREA of W.
18759 This doesn't include parts drawn because of overhangs. */
18760 if (row->full_width_p)
18761 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18762 else
18763 x_reached -= window_box_left (w, area);
18764
18765 RELEASE_HDC (hdc, f);
18766
18767 return x_reached;
18768 }
18769
18770 /* Expand row matrix if too narrow. Don't expand if area
18771 is not present. */
18772
18773 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18774 { \
18775 if (!fonts_changed_p \
18776 && (it->glyph_row->glyphs[area] \
18777 < it->glyph_row->glyphs[area + 1])) \
18778 { \
18779 it->w->ncols_scale_factor++; \
18780 fonts_changed_p = 1; \
18781 } \
18782 }
18783
18784 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18785 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18786
18787 static INLINE void
18788 append_glyph (it)
18789 struct it *it;
18790 {
18791 struct glyph *glyph;
18792 enum glyph_row_area area = it->area;
18793
18794 xassert (it->glyph_row);
18795 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18796
18797 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18798 if (glyph < it->glyph_row->glyphs[area + 1])
18799 {
18800 glyph->charpos = CHARPOS (it->position);
18801 glyph->object = it->object;
18802 glyph->pixel_width = it->pixel_width;
18803 glyph->ascent = it->ascent;
18804 glyph->descent = it->descent;
18805 glyph->voffset = it->voffset;
18806 glyph->type = CHAR_GLYPH;
18807 glyph->multibyte_p = it->multibyte_p;
18808 glyph->left_box_line_p = it->start_of_box_run_p;
18809 glyph->right_box_line_p = it->end_of_box_run_p;
18810 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18811 || it->phys_descent > it->descent);
18812 glyph->padding_p = 0;
18813 glyph->glyph_not_available_p = it->glyph_not_available_p;
18814 glyph->face_id = it->face_id;
18815 glyph->u.ch = it->char_to_display;
18816 glyph->slice = null_glyph_slice;
18817 glyph->font_type = FONT_TYPE_UNKNOWN;
18818 ++it->glyph_row->used[area];
18819 }
18820 else
18821 IT_EXPAND_MATRIX_WIDTH (it, area);
18822 }
18823
18824 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18825 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18826
18827 static INLINE void
18828 append_composite_glyph (it)
18829 struct it *it;
18830 {
18831 struct glyph *glyph;
18832 enum glyph_row_area area = it->area;
18833
18834 xassert (it->glyph_row);
18835
18836 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18837 if (glyph < it->glyph_row->glyphs[area + 1])
18838 {
18839 glyph->charpos = CHARPOS (it->position);
18840 glyph->object = it->object;
18841 glyph->pixel_width = it->pixel_width;
18842 glyph->ascent = it->ascent;
18843 glyph->descent = it->descent;
18844 glyph->voffset = it->voffset;
18845 glyph->type = COMPOSITE_GLYPH;
18846 glyph->multibyte_p = it->multibyte_p;
18847 glyph->left_box_line_p = it->start_of_box_run_p;
18848 glyph->right_box_line_p = it->end_of_box_run_p;
18849 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18850 || it->phys_descent > it->descent);
18851 glyph->padding_p = 0;
18852 glyph->glyph_not_available_p = 0;
18853 glyph->face_id = it->face_id;
18854 glyph->u.cmp_id = it->cmp_id;
18855 glyph->slice = null_glyph_slice;
18856 glyph->font_type = FONT_TYPE_UNKNOWN;
18857 ++it->glyph_row->used[area];
18858 }
18859 else
18860 IT_EXPAND_MATRIX_WIDTH (it, area);
18861 }
18862
18863
18864 /* Change IT->ascent and IT->height according to the setting of
18865 IT->voffset. */
18866
18867 static INLINE void
18868 take_vertical_position_into_account (it)
18869 struct it *it;
18870 {
18871 if (it->voffset)
18872 {
18873 if (it->voffset < 0)
18874 /* Increase the ascent so that we can display the text higher
18875 in the line. */
18876 it->ascent -= it->voffset;
18877 else
18878 /* Increase the descent so that we can display the text lower
18879 in the line. */
18880 it->descent += it->voffset;
18881 }
18882 }
18883
18884
18885 /* Produce glyphs/get display metrics for the image IT is loaded with.
18886 See the description of struct display_iterator in dispextern.h for
18887 an overview of struct display_iterator. */
18888
18889 static void
18890 produce_image_glyph (it)
18891 struct it *it;
18892 {
18893 struct image *img;
18894 struct face *face;
18895 int glyph_ascent;
18896 struct glyph_slice slice;
18897
18898 xassert (it->what == IT_IMAGE);
18899
18900 face = FACE_FROM_ID (it->f, it->face_id);
18901 xassert (face);
18902 /* Make sure X resources of the face is loaded. */
18903 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18904
18905 if (it->image_id < 0)
18906 {
18907 /* Fringe bitmap. */
18908 it->ascent = it->phys_ascent = 0;
18909 it->descent = it->phys_descent = 0;
18910 it->pixel_width = 0;
18911 it->nglyphs = 0;
18912 return;
18913 }
18914
18915 img = IMAGE_FROM_ID (it->f, it->image_id);
18916 xassert (img);
18917 /* Make sure X resources of the image is loaded. */
18918 prepare_image_for_display (it->f, img);
18919
18920 slice.x = slice.y = 0;
18921 slice.width = img->width;
18922 slice.height = img->height;
18923
18924 if (INTEGERP (it->slice.x))
18925 slice.x = XINT (it->slice.x);
18926 else if (FLOATP (it->slice.x))
18927 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18928
18929 if (INTEGERP (it->slice.y))
18930 slice.y = XINT (it->slice.y);
18931 else if (FLOATP (it->slice.y))
18932 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18933
18934 if (INTEGERP (it->slice.width))
18935 slice.width = XINT (it->slice.width);
18936 else if (FLOATP (it->slice.width))
18937 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18938
18939 if (INTEGERP (it->slice.height))
18940 slice.height = XINT (it->slice.height);
18941 else if (FLOATP (it->slice.height))
18942 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18943
18944 if (slice.x >= img->width)
18945 slice.x = img->width;
18946 if (slice.y >= img->height)
18947 slice.y = img->height;
18948 if (slice.x + slice.width >= img->width)
18949 slice.width = img->width - slice.x;
18950 if (slice.y + slice.height > img->height)
18951 slice.height = img->height - slice.y;
18952
18953 if (slice.width == 0 || slice.height == 0)
18954 return;
18955
18956 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18957
18958 it->descent = slice.height - glyph_ascent;
18959 if (slice.y == 0)
18960 it->descent += img->vmargin;
18961 if (slice.y + slice.height == img->height)
18962 it->descent += img->vmargin;
18963 it->phys_descent = it->descent;
18964
18965 it->pixel_width = slice.width;
18966 if (slice.x == 0)
18967 it->pixel_width += img->hmargin;
18968 if (slice.x + slice.width == img->width)
18969 it->pixel_width += img->hmargin;
18970
18971 /* It's quite possible for images to have an ascent greater than
18972 their height, so don't get confused in that case. */
18973 if (it->descent < 0)
18974 it->descent = 0;
18975
18976 #if 0 /* this breaks image tiling */
18977 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18978 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18979 if (face_ascent > it->ascent)
18980 it->ascent = it->phys_ascent = face_ascent;
18981 #endif
18982
18983 it->nglyphs = 1;
18984
18985 if (face->box != FACE_NO_BOX)
18986 {
18987 if (face->box_line_width > 0)
18988 {
18989 if (slice.y == 0)
18990 it->ascent += face->box_line_width;
18991 if (slice.y + slice.height == img->height)
18992 it->descent += face->box_line_width;
18993 }
18994
18995 if (it->start_of_box_run_p && slice.x == 0)
18996 it->pixel_width += abs (face->box_line_width);
18997 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18998 it->pixel_width += abs (face->box_line_width);
18999 }
19000
19001 take_vertical_position_into_account (it);
19002
19003 if (it->glyph_row)
19004 {
19005 struct glyph *glyph;
19006 enum glyph_row_area area = it->area;
19007
19008 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19009 if (glyph < it->glyph_row->glyphs[area + 1])
19010 {
19011 glyph->charpos = CHARPOS (it->position);
19012 glyph->object = it->object;
19013 glyph->pixel_width = it->pixel_width;
19014 glyph->ascent = glyph_ascent;
19015 glyph->descent = it->descent;
19016 glyph->voffset = it->voffset;
19017 glyph->type = IMAGE_GLYPH;
19018 glyph->multibyte_p = it->multibyte_p;
19019 glyph->left_box_line_p = it->start_of_box_run_p;
19020 glyph->right_box_line_p = it->end_of_box_run_p;
19021 glyph->overlaps_vertically_p = 0;
19022 glyph->padding_p = 0;
19023 glyph->glyph_not_available_p = 0;
19024 glyph->face_id = it->face_id;
19025 glyph->u.img_id = img->id;
19026 glyph->slice = slice;
19027 glyph->font_type = FONT_TYPE_UNKNOWN;
19028 ++it->glyph_row->used[area];
19029 }
19030 else
19031 IT_EXPAND_MATRIX_WIDTH (it, area);
19032 }
19033 }
19034
19035
19036 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19037 of the glyph, WIDTH and HEIGHT are the width and height of the
19038 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19039
19040 static void
19041 append_stretch_glyph (it, object, width, height, ascent)
19042 struct it *it;
19043 Lisp_Object object;
19044 int width, height;
19045 int ascent;
19046 {
19047 struct glyph *glyph;
19048 enum glyph_row_area area = it->area;
19049
19050 xassert (ascent >= 0 && ascent <= height);
19051
19052 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19053 if (glyph < it->glyph_row->glyphs[area + 1])
19054 {
19055 glyph->charpos = CHARPOS (it->position);
19056 glyph->object = object;
19057 glyph->pixel_width = width;
19058 glyph->ascent = ascent;
19059 glyph->descent = height - ascent;
19060 glyph->voffset = it->voffset;
19061 glyph->type = STRETCH_GLYPH;
19062 glyph->multibyte_p = it->multibyte_p;
19063 glyph->left_box_line_p = it->start_of_box_run_p;
19064 glyph->right_box_line_p = it->end_of_box_run_p;
19065 glyph->overlaps_vertically_p = 0;
19066 glyph->padding_p = 0;
19067 glyph->glyph_not_available_p = 0;
19068 glyph->face_id = it->face_id;
19069 glyph->u.stretch.ascent = ascent;
19070 glyph->u.stretch.height = height;
19071 glyph->slice = null_glyph_slice;
19072 glyph->font_type = FONT_TYPE_UNKNOWN;
19073 ++it->glyph_row->used[area];
19074 }
19075 else
19076 IT_EXPAND_MATRIX_WIDTH (it, area);
19077 }
19078
19079
19080 /* Produce a stretch glyph for iterator IT. IT->object is the value
19081 of the glyph property displayed. The value must be a list
19082 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19083 being recognized:
19084
19085 1. `:width WIDTH' specifies that the space should be WIDTH *
19086 canonical char width wide. WIDTH may be an integer or floating
19087 point number.
19088
19089 2. `:relative-width FACTOR' specifies that the width of the stretch
19090 should be computed from the width of the first character having the
19091 `glyph' property, and should be FACTOR times that width.
19092
19093 3. `:align-to HPOS' specifies that the space should be wide enough
19094 to reach HPOS, a value in canonical character units.
19095
19096 Exactly one of the above pairs must be present.
19097
19098 4. `:height HEIGHT' specifies that the height of the stretch produced
19099 should be HEIGHT, measured in canonical character units.
19100
19101 5. `:relative-height FACTOR' specifies that the height of the
19102 stretch should be FACTOR times the height of the characters having
19103 the glyph property.
19104
19105 Either none or exactly one of 4 or 5 must be present.
19106
19107 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19108 of the stretch should be used for the ascent of the stretch.
19109 ASCENT must be in the range 0 <= ASCENT <= 100. */
19110
19111 static void
19112 produce_stretch_glyph (it)
19113 struct it *it;
19114 {
19115 /* (space :width WIDTH :height HEIGHT ...) */
19116 Lisp_Object prop, plist;
19117 int width = 0, height = 0, align_to = -1;
19118 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19119 int ascent = 0;
19120 double tem;
19121 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19122 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19123
19124 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19125
19126 /* List should start with `space'. */
19127 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19128 plist = XCDR (it->object);
19129
19130 /* Compute the width of the stretch. */
19131 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19132 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19133 {
19134 /* Absolute width `:width WIDTH' specified and valid. */
19135 zero_width_ok_p = 1;
19136 width = (int)tem;
19137 }
19138 else if (prop = Fplist_get (plist, QCrelative_width),
19139 NUMVAL (prop) > 0)
19140 {
19141 /* Relative width `:relative-width FACTOR' specified and valid.
19142 Compute the width of the characters having the `glyph'
19143 property. */
19144 struct it it2;
19145 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19146
19147 it2 = *it;
19148 if (it->multibyte_p)
19149 {
19150 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19151 - IT_BYTEPOS (*it));
19152 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19153 }
19154 else
19155 it2.c = *p, it2.len = 1;
19156
19157 it2.glyph_row = NULL;
19158 it2.what = IT_CHARACTER;
19159 x_produce_glyphs (&it2);
19160 width = NUMVAL (prop) * it2.pixel_width;
19161 }
19162 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19163 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19164 {
19165 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19166 align_to = (align_to < 0
19167 ? 0
19168 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19169 else if (align_to < 0)
19170 align_to = window_box_left_offset (it->w, TEXT_AREA);
19171 width = max (0, (int)tem + align_to - it->current_x);
19172 zero_width_ok_p = 1;
19173 }
19174 else
19175 /* Nothing specified -> width defaults to canonical char width. */
19176 width = FRAME_COLUMN_WIDTH (it->f);
19177
19178 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19179 width = 1;
19180
19181 /* Compute height. */
19182 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19183 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19184 {
19185 height = (int)tem;
19186 zero_height_ok_p = 1;
19187 }
19188 else if (prop = Fplist_get (plist, QCrelative_height),
19189 NUMVAL (prop) > 0)
19190 height = FONT_HEIGHT (font) * NUMVAL (prop);
19191 else
19192 height = FONT_HEIGHT (font);
19193
19194 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19195 height = 1;
19196
19197 /* Compute percentage of height used for ascent. If
19198 `:ascent ASCENT' is present and valid, use that. Otherwise,
19199 derive the ascent from the font in use. */
19200 if (prop = Fplist_get (plist, QCascent),
19201 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19202 ascent = height * NUMVAL (prop) / 100.0;
19203 else if (!NILP (prop)
19204 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19205 ascent = min (max (0, (int)tem), height);
19206 else
19207 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19208
19209 if (width > 0 && height > 0 && it->glyph_row)
19210 {
19211 Lisp_Object object = it->stack[it->sp - 1].string;
19212 if (!STRINGP (object))
19213 object = it->w->buffer;
19214 append_stretch_glyph (it, object, width, height, ascent);
19215 }
19216
19217 it->pixel_width = width;
19218 it->ascent = it->phys_ascent = ascent;
19219 it->descent = it->phys_descent = height - it->ascent;
19220 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19221
19222 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19223 {
19224 if (face->box_line_width > 0)
19225 {
19226 it->ascent += face->box_line_width;
19227 it->descent += face->box_line_width;
19228 }
19229
19230 if (it->start_of_box_run_p)
19231 it->pixel_width += abs (face->box_line_width);
19232 if (it->end_of_box_run_p)
19233 it->pixel_width += abs (face->box_line_width);
19234 }
19235
19236 take_vertical_position_into_account (it);
19237 }
19238
19239 /* Get line-height and line-spacing property at point.
19240 If line-height has format (HEIGHT TOTAL), return TOTAL
19241 in TOTAL_HEIGHT. */
19242
19243 static Lisp_Object
19244 get_line_height_property (it, prop)
19245 struct it *it;
19246 Lisp_Object prop;
19247 {
19248 Lisp_Object position;
19249
19250 if (STRINGP (it->object))
19251 position = make_number (IT_STRING_CHARPOS (*it));
19252 else if (BUFFERP (it->object))
19253 position = make_number (IT_CHARPOS (*it));
19254 else
19255 return Qnil;
19256
19257 return Fget_char_property (position, prop, it->object);
19258 }
19259
19260 /* Calculate line-height and line-spacing properties.
19261 An integer value specifies explicit pixel value.
19262 A float value specifies relative value to current face height.
19263 A cons (float . face-name) specifies relative value to
19264 height of specified face font.
19265
19266 Returns height in pixels, or nil. */
19267
19268
19269 static Lisp_Object
19270 calc_line_height_property (it, val, font, boff, override)
19271 struct it *it;
19272 Lisp_Object val;
19273 XFontStruct *font;
19274 int boff, override;
19275 {
19276 Lisp_Object face_name = Qnil;
19277 int ascent, descent, height;
19278
19279 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19280 return val;
19281
19282 if (CONSP (val))
19283 {
19284 face_name = XCAR (val);
19285 val = XCDR (val);
19286 if (!NUMBERP (val))
19287 val = make_number (1);
19288 if (NILP (face_name))
19289 {
19290 height = it->ascent + it->descent;
19291 goto scale;
19292 }
19293 }
19294
19295 if (NILP (face_name))
19296 {
19297 font = FRAME_FONT (it->f);
19298 boff = FRAME_BASELINE_OFFSET (it->f);
19299 }
19300 else if (EQ (face_name, Qt))
19301 {
19302 override = 0;
19303 }
19304 else
19305 {
19306 int face_id;
19307 struct face *face;
19308 struct font_info *font_info;
19309
19310 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19311 if (face_id < 0)
19312 return make_number (-1);
19313
19314 face = FACE_FROM_ID (it->f, face_id);
19315 font = face->font;
19316 if (font == NULL)
19317 return make_number (-1);
19318
19319 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19320 boff = font_info->baseline_offset;
19321 if (font_info->vertical_centering)
19322 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19323 }
19324
19325 ascent = FONT_BASE (font) + boff;
19326 descent = FONT_DESCENT (font) - boff;
19327
19328 if (override)
19329 {
19330 it->override_ascent = ascent;
19331 it->override_descent = descent;
19332 it->override_boff = boff;
19333 }
19334
19335 height = ascent + descent;
19336
19337 scale:
19338 if (FLOATP (val))
19339 height = (int)(XFLOAT_DATA (val) * height);
19340 else if (INTEGERP (val))
19341 height *= XINT (val);
19342
19343 return make_number (height);
19344 }
19345
19346
19347 /* RIF:
19348 Produce glyphs/get display metrics for the display element IT is
19349 loaded with. See the description of struct display_iterator in
19350 dispextern.h for an overview of struct display_iterator. */
19351
19352 void
19353 x_produce_glyphs (it)
19354 struct it *it;
19355 {
19356 int extra_line_spacing = it->extra_line_spacing;
19357
19358 it->glyph_not_available_p = 0;
19359
19360 if (it->what == IT_CHARACTER)
19361 {
19362 XChar2b char2b;
19363 XFontStruct *font;
19364 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19365 XCharStruct *pcm;
19366 int font_not_found_p;
19367 struct font_info *font_info;
19368 int boff; /* baseline offset */
19369 /* We may change it->multibyte_p upon unibyte<->multibyte
19370 conversion. So, save the current value now and restore it
19371 later.
19372
19373 Note: It seems that we don't have to record multibyte_p in
19374 struct glyph because the character code itself tells if or
19375 not the character is multibyte. Thus, in the future, we must
19376 consider eliminating the field `multibyte_p' in the struct
19377 glyph. */
19378 int saved_multibyte_p = it->multibyte_p;
19379
19380 /* Maybe translate single-byte characters to multibyte, or the
19381 other way. */
19382 it->char_to_display = it->c;
19383 if (!ASCII_BYTE_P (it->c))
19384 {
19385 if (unibyte_display_via_language_environment
19386 && SINGLE_BYTE_CHAR_P (it->c)
19387 && (it->c >= 0240
19388 || !NILP (Vnonascii_translation_table)))
19389 {
19390 it->char_to_display = unibyte_char_to_multibyte (it->c);
19391 it->multibyte_p = 1;
19392 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19393 face = FACE_FROM_ID (it->f, it->face_id);
19394 }
19395 else if (!SINGLE_BYTE_CHAR_P (it->c)
19396 && !it->multibyte_p)
19397 {
19398 it->multibyte_p = 1;
19399 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19400 face = FACE_FROM_ID (it->f, it->face_id);
19401 }
19402 }
19403
19404 /* Get font to use. Encode IT->char_to_display. */
19405 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19406 &char2b, it->multibyte_p, 0);
19407 font = face->font;
19408
19409 /* When no suitable font found, use the default font. */
19410 font_not_found_p = font == NULL;
19411 if (font_not_found_p)
19412 {
19413 font = FRAME_FONT (it->f);
19414 boff = FRAME_BASELINE_OFFSET (it->f);
19415 font_info = NULL;
19416 }
19417 else
19418 {
19419 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19420 boff = font_info->baseline_offset;
19421 if (font_info->vertical_centering)
19422 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19423 }
19424
19425 if (it->char_to_display >= ' '
19426 && (!it->multibyte_p || it->char_to_display < 128))
19427 {
19428 /* Either unibyte or ASCII. */
19429 int stretched_p;
19430
19431 it->nglyphs = 1;
19432
19433 pcm = FRAME_RIF (it->f)->per_char_metric
19434 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19435
19436 if (it->override_ascent >= 0)
19437 {
19438 it->ascent = it->override_ascent;
19439 it->descent = it->override_descent;
19440 boff = it->override_boff;
19441 }
19442 else
19443 {
19444 it->ascent = FONT_BASE (font) + boff;
19445 it->descent = FONT_DESCENT (font) - boff;
19446 }
19447
19448 if (pcm)
19449 {
19450 it->phys_ascent = pcm->ascent + boff;
19451 it->phys_descent = pcm->descent - boff;
19452 it->pixel_width = pcm->width;
19453 }
19454 else
19455 {
19456 it->glyph_not_available_p = 1;
19457 it->phys_ascent = it->ascent;
19458 it->phys_descent = it->descent;
19459 it->pixel_width = FONT_WIDTH (font);
19460 }
19461
19462 if (it->constrain_row_ascent_descent_p)
19463 {
19464 if (it->descent > it->max_descent)
19465 {
19466 it->ascent += it->descent - it->max_descent;
19467 it->descent = it->max_descent;
19468 }
19469 if (it->ascent > it->max_ascent)
19470 {
19471 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19472 it->ascent = it->max_ascent;
19473 }
19474 it->phys_ascent = min (it->phys_ascent, it->ascent);
19475 it->phys_descent = min (it->phys_descent, it->descent);
19476 extra_line_spacing = 0;
19477 }
19478
19479 /* If this is a space inside a region of text with
19480 `space-width' property, change its width. */
19481 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19482 if (stretched_p)
19483 it->pixel_width *= XFLOATINT (it->space_width);
19484
19485 /* If face has a box, add the box thickness to the character
19486 height. If character has a box line to the left and/or
19487 right, add the box line width to the character's width. */
19488 if (face->box != FACE_NO_BOX)
19489 {
19490 int thick = face->box_line_width;
19491
19492 if (thick > 0)
19493 {
19494 it->ascent += thick;
19495 it->descent += thick;
19496 }
19497 else
19498 thick = -thick;
19499
19500 if (it->start_of_box_run_p)
19501 it->pixel_width += thick;
19502 if (it->end_of_box_run_p)
19503 it->pixel_width += thick;
19504 }
19505
19506 /* If face has an overline, add the height of the overline
19507 (1 pixel) and a 1 pixel margin to the character height. */
19508 if (face->overline_p)
19509 it->ascent += 2;
19510
19511 if (it->constrain_row_ascent_descent_p)
19512 {
19513 if (it->ascent > it->max_ascent)
19514 it->ascent = it->max_ascent;
19515 if (it->descent > it->max_descent)
19516 it->descent = it->max_descent;
19517 }
19518
19519 take_vertical_position_into_account (it);
19520
19521 /* If we have to actually produce glyphs, do it. */
19522 if (it->glyph_row)
19523 {
19524 if (stretched_p)
19525 {
19526 /* Translate a space with a `space-width' property
19527 into a stretch glyph. */
19528 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19529 / FONT_HEIGHT (font));
19530 append_stretch_glyph (it, it->object, it->pixel_width,
19531 it->ascent + it->descent, ascent);
19532 }
19533 else
19534 append_glyph (it);
19535
19536 /* If characters with lbearing or rbearing are displayed
19537 in this line, record that fact in a flag of the
19538 glyph row. This is used to optimize X output code. */
19539 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19540 it->glyph_row->contains_overlapping_glyphs_p = 1;
19541 }
19542 }
19543 else if (it->char_to_display == '\n')
19544 {
19545 /* A newline has no width but we need the height of the line.
19546 But if previous part of the line set a height, don't
19547 increase that height */
19548
19549 Lisp_Object height;
19550 Lisp_Object total_height = Qnil;
19551
19552 it->override_ascent = -1;
19553 it->pixel_width = 0;
19554 it->nglyphs = 0;
19555
19556 height = get_line_height_property(it, Qline_height);
19557 /* Split (line-height total-height) list */
19558 if (CONSP (height)
19559 && CONSP (XCDR (height))
19560 && NILP (XCDR (XCDR (height))))
19561 {
19562 total_height = XCAR (XCDR (height));
19563 height = XCAR (height);
19564 }
19565 height = calc_line_height_property(it, height, font, boff, 1);
19566
19567 if (it->override_ascent >= 0)
19568 {
19569 it->ascent = it->override_ascent;
19570 it->descent = it->override_descent;
19571 boff = it->override_boff;
19572 }
19573 else
19574 {
19575 it->ascent = FONT_BASE (font) + boff;
19576 it->descent = FONT_DESCENT (font) - boff;
19577 }
19578
19579 if (EQ (height, Qt))
19580 {
19581 if (it->descent > it->max_descent)
19582 {
19583 it->ascent += it->descent - it->max_descent;
19584 it->descent = it->max_descent;
19585 }
19586 if (it->ascent > it->max_ascent)
19587 {
19588 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19589 it->ascent = it->max_ascent;
19590 }
19591 it->phys_ascent = min (it->phys_ascent, it->ascent);
19592 it->phys_descent = min (it->phys_descent, it->descent);
19593 it->constrain_row_ascent_descent_p = 1;
19594 extra_line_spacing = 0;
19595 }
19596 else
19597 {
19598 Lisp_Object spacing;
19599
19600 it->phys_ascent = it->ascent;
19601 it->phys_descent = it->descent;
19602
19603 if ((it->max_ascent > 0 || it->max_descent > 0)
19604 && face->box != FACE_NO_BOX
19605 && face->box_line_width > 0)
19606 {
19607 it->ascent += face->box_line_width;
19608 it->descent += face->box_line_width;
19609 }
19610 if (!NILP (height)
19611 && XINT (height) > it->ascent + it->descent)
19612 it->ascent = XINT (height) - it->descent;
19613
19614 if (!NILP (total_height))
19615 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19616 else
19617 {
19618 spacing = get_line_height_property(it, Qline_spacing);
19619 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19620 }
19621 if (INTEGERP (spacing))
19622 {
19623 extra_line_spacing = XINT (spacing);
19624 if (!NILP (total_height))
19625 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19626 }
19627 }
19628 }
19629 else if (it->char_to_display == '\t')
19630 {
19631 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19632 int x = it->current_x + it->continuation_lines_width;
19633 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19634
19635 /* If the distance from the current position to the next tab
19636 stop is less than a space character width, use the
19637 tab stop after that. */
19638 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19639 next_tab_x += tab_width;
19640
19641 it->pixel_width = next_tab_x - x;
19642 it->nglyphs = 1;
19643 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19644 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19645
19646 if (it->glyph_row)
19647 {
19648 append_stretch_glyph (it, it->object, it->pixel_width,
19649 it->ascent + it->descent, it->ascent);
19650 }
19651 }
19652 else
19653 {
19654 /* A multi-byte character. Assume that the display width of the
19655 character is the width of the character multiplied by the
19656 width of the font. */
19657
19658 /* If we found a font, this font should give us the right
19659 metrics. If we didn't find a font, use the frame's
19660 default font and calculate the width of the character
19661 from the charset width; this is what old redisplay code
19662 did. */
19663
19664 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19665 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19666
19667 if (font_not_found_p || !pcm)
19668 {
19669 int charset = CHAR_CHARSET (it->char_to_display);
19670
19671 it->glyph_not_available_p = 1;
19672 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19673 * CHARSET_WIDTH (charset));
19674 it->phys_ascent = FONT_BASE (font) + boff;
19675 it->phys_descent = FONT_DESCENT (font) - boff;
19676 }
19677 else
19678 {
19679 it->pixel_width = pcm->width;
19680 it->phys_ascent = pcm->ascent + boff;
19681 it->phys_descent = pcm->descent - boff;
19682 if (it->glyph_row
19683 && (pcm->lbearing < 0
19684 || pcm->rbearing > pcm->width))
19685 it->glyph_row->contains_overlapping_glyphs_p = 1;
19686 }
19687 it->nglyphs = 1;
19688 it->ascent = FONT_BASE (font) + boff;
19689 it->descent = FONT_DESCENT (font) - boff;
19690 if (face->box != FACE_NO_BOX)
19691 {
19692 int thick = face->box_line_width;
19693
19694 if (thick > 0)
19695 {
19696 it->ascent += thick;
19697 it->descent += thick;
19698 }
19699 else
19700 thick = - thick;
19701
19702 if (it->start_of_box_run_p)
19703 it->pixel_width += thick;
19704 if (it->end_of_box_run_p)
19705 it->pixel_width += thick;
19706 }
19707
19708 /* If face has an overline, add the height of the overline
19709 (1 pixel) and a 1 pixel margin to the character height. */
19710 if (face->overline_p)
19711 it->ascent += 2;
19712
19713 take_vertical_position_into_account (it);
19714
19715 if (it->glyph_row)
19716 append_glyph (it);
19717 }
19718 it->multibyte_p = saved_multibyte_p;
19719 }
19720 else if (it->what == IT_COMPOSITION)
19721 {
19722 /* Note: A composition is represented as one glyph in the
19723 glyph matrix. There are no padding glyphs. */
19724 XChar2b char2b;
19725 XFontStruct *font;
19726 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19727 XCharStruct *pcm;
19728 int font_not_found_p;
19729 struct font_info *font_info;
19730 int boff; /* baseline offset */
19731 struct composition *cmp = composition_table[it->cmp_id];
19732
19733 /* Maybe translate single-byte characters to multibyte. */
19734 it->char_to_display = it->c;
19735 if (unibyte_display_via_language_environment
19736 && SINGLE_BYTE_CHAR_P (it->c)
19737 && (it->c >= 0240
19738 || (it->c >= 0200
19739 && !NILP (Vnonascii_translation_table))))
19740 {
19741 it->char_to_display = unibyte_char_to_multibyte (it->c);
19742 }
19743
19744 /* Get face and font to use. Encode IT->char_to_display. */
19745 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19746 face = FACE_FROM_ID (it->f, it->face_id);
19747 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19748 &char2b, it->multibyte_p, 0);
19749 font = face->font;
19750
19751 /* When no suitable font found, use the default font. */
19752 font_not_found_p = font == NULL;
19753 if (font_not_found_p)
19754 {
19755 font = FRAME_FONT (it->f);
19756 boff = FRAME_BASELINE_OFFSET (it->f);
19757 font_info = NULL;
19758 }
19759 else
19760 {
19761 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19762 boff = font_info->baseline_offset;
19763 if (font_info->vertical_centering)
19764 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19765 }
19766
19767 /* There are no padding glyphs, so there is only one glyph to
19768 produce for the composition. Important is that pixel_width,
19769 ascent and descent are the values of what is drawn by
19770 draw_glyphs (i.e. the values of the overall glyphs composed). */
19771 it->nglyphs = 1;
19772
19773 /* If we have not yet calculated pixel size data of glyphs of
19774 the composition for the current face font, calculate them
19775 now. Theoretically, we have to check all fonts for the
19776 glyphs, but that requires much time and memory space. So,
19777 here we check only the font of the first glyph. This leads
19778 to incorrect display very rarely, and C-l (recenter) can
19779 correct the display anyway. */
19780 if (cmp->font != (void *) font)
19781 {
19782 /* Ascent and descent of the font of the first character of
19783 this composition (adjusted by baseline offset). Ascent
19784 and descent of overall glyphs should not be less than
19785 them respectively. */
19786 int font_ascent = FONT_BASE (font) + boff;
19787 int font_descent = FONT_DESCENT (font) - boff;
19788 /* Bounding box of the overall glyphs. */
19789 int leftmost, rightmost, lowest, highest;
19790 int i, width, ascent, descent;
19791
19792 cmp->font = (void *) font;
19793
19794 /* Initialize the bounding box. */
19795 if (font_info
19796 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19797 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19798 {
19799 width = pcm->width;
19800 ascent = pcm->ascent;
19801 descent = pcm->descent;
19802 }
19803 else
19804 {
19805 width = FONT_WIDTH (font);
19806 ascent = FONT_BASE (font);
19807 descent = FONT_DESCENT (font);
19808 }
19809
19810 rightmost = width;
19811 lowest = - descent + boff;
19812 highest = ascent + boff;
19813 leftmost = 0;
19814
19815 if (font_info
19816 && font_info->default_ascent
19817 && CHAR_TABLE_P (Vuse_default_ascent)
19818 && !NILP (Faref (Vuse_default_ascent,
19819 make_number (it->char_to_display))))
19820 highest = font_info->default_ascent + boff;
19821
19822 /* Draw the first glyph at the normal position. It may be
19823 shifted to right later if some other glyphs are drawn at
19824 the left. */
19825 cmp->offsets[0] = 0;
19826 cmp->offsets[1] = boff;
19827
19828 /* Set cmp->offsets for the remaining glyphs. */
19829 for (i = 1; i < cmp->glyph_len; i++)
19830 {
19831 int left, right, btm, top;
19832 int ch = COMPOSITION_GLYPH (cmp, i);
19833 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19834
19835 face = FACE_FROM_ID (it->f, face_id);
19836 get_char_face_and_encoding (it->f, ch, face->id,
19837 &char2b, it->multibyte_p, 0);
19838 font = face->font;
19839 if (font == NULL)
19840 {
19841 font = FRAME_FONT (it->f);
19842 boff = FRAME_BASELINE_OFFSET (it->f);
19843 font_info = NULL;
19844 }
19845 else
19846 {
19847 font_info
19848 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19849 boff = font_info->baseline_offset;
19850 if (font_info->vertical_centering)
19851 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19852 }
19853
19854 if (font_info
19855 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19856 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19857 {
19858 width = pcm->width;
19859 ascent = pcm->ascent;
19860 descent = pcm->descent;
19861 }
19862 else
19863 {
19864 width = FONT_WIDTH (font);
19865 ascent = 1;
19866 descent = 0;
19867 }
19868
19869 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19870 {
19871 /* Relative composition with or without
19872 alternate chars. */
19873 left = (leftmost + rightmost - width) / 2;
19874 btm = - descent + boff;
19875 if (font_info && font_info->relative_compose
19876 && (! CHAR_TABLE_P (Vignore_relative_composition)
19877 || NILP (Faref (Vignore_relative_composition,
19878 make_number (ch)))))
19879 {
19880
19881 if (- descent >= font_info->relative_compose)
19882 /* One extra pixel between two glyphs. */
19883 btm = highest + 1;
19884 else if (ascent <= 0)
19885 /* One extra pixel between two glyphs. */
19886 btm = lowest - 1 - ascent - descent;
19887 }
19888 }
19889 else
19890 {
19891 /* A composition rule is specified by an integer
19892 value that encodes global and new reference
19893 points (GREF and NREF). GREF and NREF are
19894 specified by numbers as below:
19895
19896 0---1---2 -- ascent
19897 | |
19898 | |
19899 | |
19900 9--10--11 -- center
19901 | |
19902 ---3---4---5--- baseline
19903 | |
19904 6---7---8 -- descent
19905 */
19906 int rule = COMPOSITION_RULE (cmp, i);
19907 int gref, nref, grefx, grefy, nrefx, nrefy;
19908
19909 COMPOSITION_DECODE_RULE (rule, gref, nref);
19910 grefx = gref % 3, nrefx = nref % 3;
19911 grefy = gref / 3, nrefy = nref / 3;
19912
19913 left = (leftmost
19914 + grefx * (rightmost - leftmost) / 2
19915 - nrefx * width / 2);
19916 btm = ((grefy == 0 ? highest
19917 : grefy == 1 ? 0
19918 : grefy == 2 ? lowest
19919 : (highest + lowest) / 2)
19920 - (nrefy == 0 ? ascent + descent
19921 : nrefy == 1 ? descent - boff
19922 : nrefy == 2 ? 0
19923 : (ascent + descent) / 2));
19924 }
19925
19926 cmp->offsets[i * 2] = left;
19927 cmp->offsets[i * 2 + 1] = btm + descent;
19928
19929 /* Update the bounding box of the overall glyphs. */
19930 right = left + width;
19931 top = btm + descent + ascent;
19932 if (left < leftmost)
19933 leftmost = left;
19934 if (right > rightmost)
19935 rightmost = right;
19936 if (top > highest)
19937 highest = top;
19938 if (btm < lowest)
19939 lowest = btm;
19940 }
19941
19942 /* If there are glyphs whose x-offsets are negative,
19943 shift all glyphs to the right and make all x-offsets
19944 non-negative. */
19945 if (leftmost < 0)
19946 {
19947 for (i = 0; i < cmp->glyph_len; i++)
19948 cmp->offsets[i * 2] -= leftmost;
19949 rightmost -= leftmost;
19950 }
19951
19952 cmp->pixel_width = rightmost;
19953 cmp->ascent = highest;
19954 cmp->descent = - lowest;
19955 if (cmp->ascent < font_ascent)
19956 cmp->ascent = font_ascent;
19957 if (cmp->descent < font_descent)
19958 cmp->descent = font_descent;
19959 }
19960
19961 it->pixel_width = cmp->pixel_width;
19962 it->ascent = it->phys_ascent = cmp->ascent;
19963 it->descent = it->phys_descent = cmp->descent;
19964
19965 if (face->box != FACE_NO_BOX)
19966 {
19967 int thick = face->box_line_width;
19968
19969 if (thick > 0)
19970 {
19971 it->ascent += thick;
19972 it->descent += thick;
19973 }
19974 else
19975 thick = - thick;
19976
19977 if (it->start_of_box_run_p)
19978 it->pixel_width += thick;
19979 if (it->end_of_box_run_p)
19980 it->pixel_width += thick;
19981 }
19982
19983 /* If face has an overline, add the height of the overline
19984 (1 pixel) and a 1 pixel margin to the character height. */
19985 if (face->overline_p)
19986 it->ascent += 2;
19987
19988 take_vertical_position_into_account (it);
19989
19990 if (it->glyph_row)
19991 append_composite_glyph (it);
19992 }
19993 else if (it->what == IT_IMAGE)
19994 produce_image_glyph (it);
19995 else if (it->what == IT_STRETCH)
19996 produce_stretch_glyph (it);
19997
19998 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19999 because this isn't true for images with `:ascent 100'. */
20000 xassert (it->ascent >= 0 && it->descent >= 0);
20001 if (it->area == TEXT_AREA)
20002 it->current_x += it->pixel_width;
20003
20004 if (extra_line_spacing > 0)
20005 {
20006 it->descent += extra_line_spacing;
20007 if (extra_line_spacing > it->max_extra_line_spacing)
20008 it->max_extra_line_spacing = extra_line_spacing;
20009 }
20010
20011 it->max_ascent = max (it->max_ascent, it->ascent);
20012 it->max_descent = max (it->max_descent, it->descent);
20013 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20014 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20015 }
20016
20017 /* EXPORT for RIF:
20018 Output LEN glyphs starting at START at the nominal cursor position.
20019 Advance the nominal cursor over the text. The global variable
20020 updated_window contains the window being updated, updated_row is
20021 the glyph row being updated, and updated_area is the area of that
20022 row being updated. */
20023
20024 void
20025 x_write_glyphs (start, len)
20026 struct glyph *start;
20027 int len;
20028 {
20029 int x, hpos;
20030
20031 xassert (updated_window && updated_row);
20032 BLOCK_INPUT;
20033
20034 /* Write glyphs. */
20035
20036 hpos = start - updated_row->glyphs[updated_area];
20037 x = draw_glyphs (updated_window, output_cursor.x,
20038 updated_row, updated_area,
20039 hpos, hpos + len,
20040 DRAW_NORMAL_TEXT, 0);
20041
20042 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20043 if (updated_area == TEXT_AREA
20044 && updated_window->phys_cursor_on_p
20045 && updated_window->phys_cursor.vpos == output_cursor.vpos
20046 && updated_window->phys_cursor.hpos >= hpos
20047 && updated_window->phys_cursor.hpos < hpos + len)
20048 updated_window->phys_cursor_on_p = 0;
20049
20050 UNBLOCK_INPUT;
20051
20052 /* Advance the output cursor. */
20053 output_cursor.hpos += len;
20054 output_cursor.x = x;
20055 }
20056
20057
20058 /* EXPORT for RIF:
20059 Insert LEN glyphs from START at the nominal cursor position. */
20060
20061 void
20062 x_insert_glyphs (start, len)
20063 struct glyph *start;
20064 int len;
20065 {
20066 struct frame *f;
20067 struct window *w;
20068 int line_height, shift_by_width, shifted_region_width;
20069 struct glyph_row *row;
20070 struct glyph *glyph;
20071 int frame_x, frame_y, hpos;
20072
20073 xassert (updated_window && updated_row);
20074 BLOCK_INPUT;
20075 w = updated_window;
20076 f = XFRAME (WINDOW_FRAME (w));
20077
20078 /* Get the height of the line we are in. */
20079 row = updated_row;
20080 line_height = row->height;
20081
20082 /* Get the width of the glyphs to insert. */
20083 shift_by_width = 0;
20084 for (glyph = start; glyph < start + len; ++glyph)
20085 shift_by_width += glyph->pixel_width;
20086
20087 /* Get the width of the region to shift right. */
20088 shifted_region_width = (window_box_width (w, updated_area)
20089 - output_cursor.x
20090 - shift_by_width);
20091
20092 /* Shift right. */
20093 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20094 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20095
20096 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20097 line_height, shift_by_width);
20098
20099 /* Write the glyphs. */
20100 hpos = start - row->glyphs[updated_area];
20101 draw_glyphs (w, output_cursor.x, row, updated_area,
20102 hpos, hpos + len,
20103 DRAW_NORMAL_TEXT, 0);
20104
20105 /* Advance the output cursor. */
20106 output_cursor.hpos += len;
20107 output_cursor.x += shift_by_width;
20108 UNBLOCK_INPUT;
20109 }
20110
20111
20112 /* EXPORT for RIF:
20113 Erase the current text line from the nominal cursor position
20114 (inclusive) to pixel column TO_X (exclusive). The idea is that
20115 everything from TO_X onward is already erased.
20116
20117 TO_X is a pixel position relative to updated_area of
20118 updated_window. TO_X == -1 means clear to the end of this area. */
20119
20120 void
20121 x_clear_end_of_line (to_x)
20122 int to_x;
20123 {
20124 struct frame *f;
20125 struct window *w = updated_window;
20126 int max_x, min_y, max_y;
20127 int from_x, from_y, to_y;
20128
20129 xassert (updated_window && updated_row);
20130 f = XFRAME (w->frame);
20131
20132 if (updated_row->full_width_p)
20133 max_x = WINDOW_TOTAL_WIDTH (w);
20134 else
20135 max_x = window_box_width (w, updated_area);
20136 max_y = window_text_bottom_y (w);
20137
20138 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20139 of window. For TO_X > 0, truncate to end of drawing area. */
20140 if (to_x == 0)
20141 return;
20142 else if (to_x < 0)
20143 to_x = max_x;
20144 else
20145 to_x = min (to_x, max_x);
20146
20147 to_y = min (max_y, output_cursor.y + updated_row->height);
20148
20149 /* Notice if the cursor will be cleared by this operation. */
20150 if (!updated_row->full_width_p)
20151 notice_overwritten_cursor (w, updated_area,
20152 output_cursor.x, -1,
20153 updated_row->y,
20154 MATRIX_ROW_BOTTOM_Y (updated_row));
20155
20156 from_x = output_cursor.x;
20157
20158 /* Translate to frame coordinates. */
20159 if (updated_row->full_width_p)
20160 {
20161 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20162 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20163 }
20164 else
20165 {
20166 int area_left = window_box_left (w, updated_area);
20167 from_x += area_left;
20168 to_x += area_left;
20169 }
20170
20171 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20172 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20173 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20174
20175 /* Prevent inadvertently clearing to end of the X window. */
20176 if (to_x > from_x && to_y > from_y)
20177 {
20178 BLOCK_INPUT;
20179 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
20180 to_x - from_x, to_y - from_y);
20181 UNBLOCK_INPUT;
20182 }
20183 }
20184
20185 #endif /* HAVE_WINDOW_SYSTEM */
20186
20187
20188 \f
20189 /***********************************************************************
20190 Cursor types
20191 ***********************************************************************/
20192
20193 /* Value is the internal representation of the specified cursor type
20194 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20195 of the bar cursor. */
20196
20197 static enum text_cursor_kinds
20198 get_specified_cursor_type (arg, width)
20199 Lisp_Object arg;
20200 int *width;
20201 {
20202 enum text_cursor_kinds type;
20203
20204 if (NILP (arg))
20205 return NO_CURSOR;
20206
20207 if (EQ (arg, Qbox))
20208 return FILLED_BOX_CURSOR;
20209
20210 if (EQ (arg, Qhollow))
20211 return HOLLOW_BOX_CURSOR;
20212
20213 if (EQ (arg, Qbar))
20214 {
20215 *width = 2;
20216 return BAR_CURSOR;
20217 }
20218
20219 if (CONSP (arg)
20220 && EQ (XCAR (arg), Qbar)
20221 && INTEGERP (XCDR (arg))
20222 && XINT (XCDR (arg)) >= 0)
20223 {
20224 *width = XINT (XCDR (arg));
20225 return BAR_CURSOR;
20226 }
20227
20228 if (EQ (arg, Qhbar))
20229 {
20230 *width = 2;
20231 return HBAR_CURSOR;
20232 }
20233
20234 if (CONSP (arg)
20235 && EQ (XCAR (arg), Qhbar)
20236 && INTEGERP (XCDR (arg))
20237 && XINT (XCDR (arg)) >= 0)
20238 {
20239 *width = XINT (XCDR (arg));
20240 return HBAR_CURSOR;
20241 }
20242
20243 /* Treat anything unknown as "hollow box cursor".
20244 It was bad to signal an error; people have trouble fixing
20245 .Xdefaults with Emacs, when it has something bad in it. */
20246 type = HOLLOW_BOX_CURSOR;
20247
20248 return type;
20249 }
20250
20251 /* Set the default cursor types for specified frame. */
20252 void
20253 set_frame_cursor_types (f, arg)
20254 struct frame *f;
20255 Lisp_Object arg;
20256 {
20257 int width;
20258 Lisp_Object tem;
20259
20260 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20261 FRAME_CURSOR_WIDTH (f) = width;
20262
20263 /* By default, set up the blink-off state depending on the on-state. */
20264
20265 tem = Fassoc (arg, Vblink_cursor_alist);
20266 if (!NILP (tem))
20267 {
20268 FRAME_BLINK_OFF_CURSOR (f)
20269 = get_specified_cursor_type (XCDR (tem), &width);
20270 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20271 }
20272 else
20273 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20274 }
20275
20276
20277 /* Return the cursor we want to be displayed in window W. Return
20278 width of bar/hbar cursor through WIDTH arg. Return with
20279 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20280 (i.e. if the `system caret' should track this cursor).
20281
20282 In a mini-buffer window, we want the cursor only to appear if we
20283 are reading input from this window. For the selected window, we
20284 want the cursor type given by the frame parameter or buffer local
20285 setting of cursor-type. If explicitly marked off, draw no cursor.
20286 In all other cases, we want a hollow box cursor. */
20287
20288 static enum text_cursor_kinds
20289 get_window_cursor_type (w, glyph, width, active_cursor)
20290 struct window *w;
20291 struct glyph *glyph;
20292 int *width;
20293 int *active_cursor;
20294 {
20295 struct frame *f = XFRAME (w->frame);
20296 struct buffer *b = XBUFFER (w->buffer);
20297 int cursor_type = DEFAULT_CURSOR;
20298 Lisp_Object alt_cursor;
20299 int non_selected = 0;
20300
20301 *active_cursor = 1;
20302
20303 /* Echo area */
20304 if (cursor_in_echo_area
20305 && FRAME_HAS_MINIBUF_P (f)
20306 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20307 {
20308 if (w == XWINDOW (echo_area_window))
20309 {
20310 *width = FRAME_CURSOR_WIDTH (f);
20311 return FRAME_DESIRED_CURSOR (f);
20312 }
20313
20314 *active_cursor = 0;
20315 non_selected = 1;
20316 }
20317
20318 /* Nonselected window or nonselected frame. */
20319 else if (w != XWINDOW (f->selected_window)
20320 #ifdef HAVE_WINDOW_SYSTEM
20321 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20322 #endif
20323 )
20324 {
20325 *active_cursor = 0;
20326
20327 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20328 return NO_CURSOR;
20329
20330 non_selected = 1;
20331 }
20332
20333 /* Never display a cursor in a window in which cursor-type is nil. */
20334 if (NILP (b->cursor_type))
20335 return NO_CURSOR;
20336
20337 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20338 if (non_selected)
20339 {
20340 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20341 return get_specified_cursor_type (alt_cursor, width);
20342 }
20343
20344 /* Get the normal cursor type for this window. */
20345 if (EQ (b->cursor_type, Qt))
20346 {
20347 cursor_type = FRAME_DESIRED_CURSOR (f);
20348 *width = FRAME_CURSOR_WIDTH (f);
20349 }
20350 else
20351 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20352
20353 /* Use normal cursor if not blinked off. */
20354 if (!w->cursor_off_p)
20355 {
20356 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20357 if (cursor_type == FILLED_BOX_CURSOR)
20358 cursor_type = HOLLOW_BOX_CURSOR;
20359 }
20360 return cursor_type;
20361 }
20362
20363 /* Cursor is blinked off, so determine how to "toggle" it. */
20364
20365 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20366 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20367 return get_specified_cursor_type (XCDR (alt_cursor), width);
20368
20369 /* Then see if frame has specified a specific blink off cursor type. */
20370 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20371 {
20372 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20373 return FRAME_BLINK_OFF_CURSOR (f);
20374 }
20375
20376 #if 0
20377 /* Some people liked having a permanently visible blinking cursor,
20378 while others had very strong opinions against it. So it was
20379 decided to remove it. KFS 2003-09-03 */
20380
20381 /* Finally perform built-in cursor blinking:
20382 filled box <-> hollow box
20383 wide [h]bar <-> narrow [h]bar
20384 narrow [h]bar <-> no cursor
20385 other type <-> no cursor */
20386
20387 if (cursor_type == FILLED_BOX_CURSOR)
20388 return HOLLOW_BOX_CURSOR;
20389
20390 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20391 {
20392 *width = 1;
20393 return cursor_type;
20394 }
20395 #endif
20396
20397 return NO_CURSOR;
20398 }
20399
20400
20401 #ifdef HAVE_WINDOW_SYSTEM
20402
20403 /* Notice when the text cursor of window W has been completely
20404 overwritten by a drawing operation that outputs glyphs in AREA
20405 starting at X0 and ending at X1 in the line starting at Y0 and
20406 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20407 the rest of the line after X0 has been written. Y coordinates
20408 are window-relative. */
20409
20410 static void
20411 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20412 struct window *w;
20413 enum glyph_row_area area;
20414 int x0, y0, x1, y1;
20415 {
20416 int cx0, cx1, cy0, cy1;
20417 struct glyph_row *row;
20418
20419 if (!w->phys_cursor_on_p)
20420 return;
20421 if (area != TEXT_AREA)
20422 return;
20423
20424 if (w->phys_cursor.vpos < 0
20425 || w->phys_cursor.vpos >= w->current_matrix->nrows
20426 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20427 !(row->enabled_p && row->displays_text_p)))
20428 return;
20429
20430 if (row->cursor_in_fringe_p)
20431 {
20432 row->cursor_in_fringe_p = 0;
20433 draw_fringe_bitmap (w, row, 0);
20434 w->phys_cursor_on_p = 0;
20435 return;
20436 }
20437
20438 cx0 = w->phys_cursor.x;
20439 cx1 = cx0 + w->phys_cursor_width;
20440 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20441 return;
20442
20443 /* The cursor image will be completely removed from the
20444 screen if the output area intersects the cursor area in
20445 y-direction. When we draw in [y0 y1[, and some part of
20446 the cursor is at y < y0, that part must have been drawn
20447 before. When scrolling, the cursor is erased before
20448 actually scrolling, so we don't come here. When not
20449 scrolling, the rows above the old cursor row must have
20450 changed, and in this case these rows must have written
20451 over the cursor image.
20452
20453 Likewise if part of the cursor is below y1, with the
20454 exception of the cursor being in the first blank row at
20455 the buffer and window end because update_text_area
20456 doesn't draw that row. (Except when it does, but
20457 that's handled in update_text_area.) */
20458
20459 cy0 = w->phys_cursor.y;
20460 cy1 = cy0 + w->phys_cursor_height;
20461 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20462 return;
20463
20464 w->phys_cursor_on_p = 0;
20465 }
20466
20467 #endif /* HAVE_WINDOW_SYSTEM */
20468
20469 \f
20470 /************************************************************************
20471 Mouse Face
20472 ************************************************************************/
20473
20474 #ifdef HAVE_WINDOW_SYSTEM
20475
20476 /* EXPORT for RIF:
20477 Fix the display of area AREA of overlapping row ROW in window W. */
20478
20479 void
20480 x_fix_overlapping_area (w, row, area)
20481 struct window *w;
20482 struct glyph_row *row;
20483 enum glyph_row_area area;
20484 {
20485 int i, x;
20486
20487 BLOCK_INPUT;
20488
20489 x = 0;
20490 for (i = 0; i < row->used[area];)
20491 {
20492 if (row->glyphs[area][i].overlaps_vertically_p)
20493 {
20494 int start = i, start_x = x;
20495
20496 do
20497 {
20498 x += row->glyphs[area][i].pixel_width;
20499 ++i;
20500 }
20501 while (i < row->used[area]
20502 && row->glyphs[area][i].overlaps_vertically_p);
20503
20504 draw_glyphs (w, start_x, row, area,
20505 start, i,
20506 DRAW_NORMAL_TEXT, 1);
20507 }
20508 else
20509 {
20510 x += row->glyphs[area][i].pixel_width;
20511 ++i;
20512 }
20513 }
20514
20515 UNBLOCK_INPUT;
20516 }
20517
20518
20519 /* EXPORT:
20520 Draw the cursor glyph of window W in glyph row ROW. See the
20521 comment of draw_glyphs for the meaning of HL. */
20522
20523 void
20524 draw_phys_cursor_glyph (w, row, hl)
20525 struct window *w;
20526 struct glyph_row *row;
20527 enum draw_glyphs_face hl;
20528 {
20529 /* If cursor hpos is out of bounds, don't draw garbage. This can
20530 happen in mini-buffer windows when switching between echo area
20531 glyphs and mini-buffer. */
20532 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20533 {
20534 int on_p = w->phys_cursor_on_p;
20535 int x1;
20536 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20537 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20538 hl, 0);
20539 w->phys_cursor_on_p = on_p;
20540
20541 if (hl == DRAW_CURSOR)
20542 w->phys_cursor_width = x1 - w->phys_cursor.x;
20543 /* When we erase the cursor, and ROW is overlapped by other
20544 rows, make sure that these overlapping parts of other rows
20545 are redrawn. */
20546 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20547 {
20548 if (row > w->current_matrix->rows
20549 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20550 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20551
20552 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20553 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20554 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20555 }
20556 }
20557 }
20558
20559
20560 /* EXPORT:
20561 Erase the image of a cursor of window W from the screen. */
20562
20563 void
20564 erase_phys_cursor (w)
20565 struct window *w;
20566 {
20567 struct frame *f = XFRAME (w->frame);
20568 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20569 int hpos = w->phys_cursor.hpos;
20570 int vpos = w->phys_cursor.vpos;
20571 int mouse_face_here_p = 0;
20572 struct glyph_matrix *active_glyphs = w->current_matrix;
20573 struct glyph_row *cursor_row;
20574 struct glyph *cursor_glyph;
20575 enum draw_glyphs_face hl;
20576
20577 /* No cursor displayed or row invalidated => nothing to do on the
20578 screen. */
20579 if (w->phys_cursor_type == NO_CURSOR)
20580 goto mark_cursor_off;
20581
20582 /* VPOS >= active_glyphs->nrows means that window has been resized.
20583 Don't bother to erase the cursor. */
20584 if (vpos >= active_glyphs->nrows)
20585 goto mark_cursor_off;
20586
20587 /* If row containing cursor is marked invalid, there is nothing we
20588 can do. */
20589 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20590 if (!cursor_row->enabled_p)
20591 goto mark_cursor_off;
20592
20593 /* If line spacing is > 0, old cursor may only be partially visible in
20594 window after split-window. So adjust visible height. */
20595 cursor_row->visible_height = min (cursor_row->visible_height,
20596 window_text_bottom_y (w) - cursor_row->y);
20597
20598 /* If row is completely invisible, don't attempt to delete a cursor which
20599 isn't there. This can happen if cursor is at top of a window, and
20600 we switch to a buffer with a header line in that window. */
20601 if (cursor_row->visible_height <= 0)
20602 goto mark_cursor_off;
20603
20604 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20605 if (cursor_row->cursor_in_fringe_p)
20606 {
20607 cursor_row->cursor_in_fringe_p = 0;
20608 draw_fringe_bitmap (w, cursor_row, 0);
20609 goto mark_cursor_off;
20610 }
20611
20612 /* This can happen when the new row is shorter than the old one.
20613 In this case, either draw_glyphs or clear_end_of_line
20614 should have cleared the cursor. Note that we wouldn't be
20615 able to erase the cursor in this case because we don't have a
20616 cursor glyph at hand. */
20617 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20618 goto mark_cursor_off;
20619
20620 /* If the cursor is in the mouse face area, redisplay that when
20621 we clear the cursor. */
20622 if (! NILP (dpyinfo->mouse_face_window)
20623 && w == XWINDOW (dpyinfo->mouse_face_window)
20624 && (vpos > dpyinfo->mouse_face_beg_row
20625 || (vpos == dpyinfo->mouse_face_beg_row
20626 && hpos >= dpyinfo->mouse_face_beg_col))
20627 && (vpos < dpyinfo->mouse_face_end_row
20628 || (vpos == dpyinfo->mouse_face_end_row
20629 && hpos < dpyinfo->mouse_face_end_col))
20630 /* Don't redraw the cursor's spot in mouse face if it is at the
20631 end of a line (on a newline). The cursor appears there, but
20632 mouse highlighting does not. */
20633 && cursor_row->used[TEXT_AREA] > hpos)
20634 mouse_face_here_p = 1;
20635
20636 /* Maybe clear the display under the cursor. */
20637 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20638 {
20639 int x, y;
20640 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20641 int width;
20642
20643 cursor_glyph = get_phys_cursor_glyph (w);
20644 if (cursor_glyph == NULL)
20645 goto mark_cursor_off;
20646
20647 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20648 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20649 width = min (cursor_glyph->pixel_width,
20650 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20651
20652 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20653 }
20654
20655 /* Erase the cursor by redrawing the character underneath it. */
20656 if (mouse_face_here_p)
20657 hl = DRAW_MOUSE_FACE;
20658 else
20659 hl = DRAW_NORMAL_TEXT;
20660 draw_phys_cursor_glyph (w, cursor_row, hl);
20661
20662 mark_cursor_off:
20663 w->phys_cursor_on_p = 0;
20664 w->phys_cursor_type = NO_CURSOR;
20665 }
20666
20667
20668 /* EXPORT:
20669 Display or clear cursor of window W. If ON is zero, clear the
20670 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20671 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20672
20673 void
20674 display_and_set_cursor (w, on, hpos, vpos, x, y)
20675 struct window *w;
20676 int on, hpos, vpos, x, y;
20677 {
20678 struct frame *f = XFRAME (w->frame);
20679 int new_cursor_type;
20680 int new_cursor_width;
20681 int active_cursor;
20682 struct glyph_row *glyph_row;
20683 struct glyph *glyph;
20684
20685 /* This is pointless on invisible frames, and dangerous on garbaged
20686 windows and frames; in the latter case, the frame or window may
20687 be in the midst of changing its size, and x and y may be off the
20688 window. */
20689 if (! FRAME_VISIBLE_P (f)
20690 || FRAME_GARBAGED_P (f)
20691 || vpos >= w->current_matrix->nrows
20692 || hpos >= w->current_matrix->matrix_w)
20693 return;
20694
20695 /* If cursor is off and we want it off, return quickly. */
20696 if (!on && !w->phys_cursor_on_p)
20697 return;
20698
20699 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20700 /* If cursor row is not enabled, we don't really know where to
20701 display the cursor. */
20702 if (!glyph_row->enabled_p)
20703 {
20704 w->phys_cursor_on_p = 0;
20705 return;
20706 }
20707
20708 glyph = NULL;
20709 if (!glyph_row->exact_window_width_line_p
20710 || hpos < glyph_row->used[TEXT_AREA])
20711 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20712
20713 xassert (interrupt_input_blocked);
20714
20715 /* Set new_cursor_type to the cursor we want to be displayed. */
20716 new_cursor_type = get_window_cursor_type (w, glyph,
20717 &new_cursor_width, &active_cursor);
20718
20719 /* If cursor is currently being shown and we don't want it to be or
20720 it is in the wrong place, or the cursor type is not what we want,
20721 erase it. */
20722 if (w->phys_cursor_on_p
20723 && (!on
20724 || w->phys_cursor.x != x
20725 || w->phys_cursor.y != y
20726 || new_cursor_type != w->phys_cursor_type
20727 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20728 && new_cursor_width != w->phys_cursor_width)))
20729 erase_phys_cursor (w);
20730
20731 /* Don't check phys_cursor_on_p here because that flag is only set
20732 to zero in some cases where we know that the cursor has been
20733 completely erased, to avoid the extra work of erasing the cursor
20734 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20735 still not be visible, or it has only been partly erased. */
20736 if (on)
20737 {
20738 w->phys_cursor_ascent = glyph_row->ascent;
20739 w->phys_cursor_height = glyph_row->height;
20740
20741 /* Set phys_cursor_.* before x_draw_.* is called because some
20742 of them may need the information. */
20743 w->phys_cursor.x = x;
20744 w->phys_cursor.y = glyph_row->y;
20745 w->phys_cursor.hpos = hpos;
20746 w->phys_cursor.vpos = vpos;
20747 }
20748
20749 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
20750 new_cursor_type, new_cursor_width,
20751 on, active_cursor);
20752 }
20753
20754
20755 /* Switch the display of W's cursor on or off, according to the value
20756 of ON. */
20757
20758 static void
20759 update_window_cursor (w, on)
20760 struct window *w;
20761 int on;
20762 {
20763 /* Don't update cursor in windows whose frame is in the process
20764 of being deleted. */
20765 if (w->current_matrix)
20766 {
20767 BLOCK_INPUT;
20768 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20769 w->phys_cursor.x, w->phys_cursor.y);
20770 UNBLOCK_INPUT;
20771 }
20772 }
20773
20774
20775 /* Call update_window_cursor with parameter ON_P on all leaf windows
20776 in the window tree rooted at W. */
20777
20778 static void
20779 update_cursor_in_window_tree (w, on_p)
20780 struct window *w;
20781 int on_p;
20782 {
20783 while (w)
20784 {
20785 if (!NILP (w->hchild))
20786 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20787 else if (!NILP (w->vchild))
20788 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20789 else
20790 update_window_cursor (w, on_p);
20791
20792 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20793 }
20794 }
20795
20796
20797 /* EXPORT:
20798 Display the cursor on window W, or clear it, according to ON_P.
20799 Don't change the cursor's position. */
20800
20801 void
20802 x_update_cursor (f, on_p)
20803 struct frame *f;
20804 int on_p;
20805 {
20806 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20807 }
20808
20809
20810 /* EXPORT:
20811 Clear the cursor of window W to background color, and mark the
20812 cursor as not shown. This is used when the text where the cursor
20813 is is about to be rewritten. */
20814
20815 void
20816 x_clear_cursor (w)
20817 struct window *w;
20818 {
20819 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20820 update_window_cursor (w, 0);
20821 }
20822
20823
20824 /* EXPORT:
20825 Display the active region described by mouse_face_* according to DRAW. */
20826
20827 void
20828 show_mouse_face (dpyinfo, draw)
20829 Display_Info *dpyinfo;
20830 enum draw_glyphs_face draw;
20831 {
20832 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20833 struct frame *f = XFRAME (WINDOW_FRAME (w));
20834
20835 if (/* If window is in the process of being destroyed, don't bother
20836 to do anything. */
20837 w->current_matrix != NULL
20838 /* Don't update mouse highlight if hidden */
20839 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20840 /* Recognize when we are called to operate on rows that don't exist
20841 anymore. This can happen when a window is split. */
20842 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20843 {
20844 int phys_cursor_on_p = w->phys_cursor_on_p;
20845 struct glyph_row *row, *first, *last;
20846
20847 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20848 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20849
20850 for (row = first; row <= last && row->enabled_p; ++row)
20851 {
20852 int start_hpos, end_hpos, start_x;
20853
20854 /* For all but the first row, the highlight starts at column 0. */
20855 if (row == first)
20856 {
20857 start_hpos = dpyinfo->mouse_face_beg_col;
20858 start_x = dpyinfo->mouse_face_beg_x;
20859 }
20860 else
20861 {
20862 start_hpos = 0;
20863 start_x = 0;
20864 }
20865
20866 if (row == last)
20867 end_hpos = dpyinfo->mouse_face_end_col;
20868 else
20869 end_hpos = row->used[TEXT_AREA];
20870
20871 if (end_hpos > start_hpos)
20872 {
20873 draw_glyphs (w, start_x, row, TEXT_AREA,
20874 start_hpos, end_hpos,
20875 draw, 0);
20876
20877 row->mouse_face_p
20878 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20879 }
20880 }
20881
20882 /* When we've written over the cursor, arrange for it to
20883 be displayed again. */
20884 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20885 {
20886 BLOCK_INPUT;
20887 display_and_set_cursor (w, 1,
20888 w->phys_cursor.hpos, w->phys_cursor.vpos,
20889 w->phys_cursor.x, w->phys_cursor.y);
20890 UNBLOCK_INPUT;
20891 }
20892 }
20893
20894 /* Change the mouse cursor. */
20895 if (draw == DRAW_NORMAL_TEXT)
20896 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20897 else if (draw == DRAW_MOUSE_FACE)
20898 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20899 else
20900 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20901 }
20902
20903 /* EXPORT:
20904 Clear out the mouse-highlighted active region.
20905 Redraw it un-highlighted first. Value is non-zero if mouse
20906 face was actually drawn unhighlighted. */
20907
20908 int
20909 clear_mouse_face (dpyinfo)
20910 Display_Info *dpyinfo;
20911 {
20912 int cleared = 0;
20913
20914 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20915 {
20916 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20917 cleared = 1;
20918 }
20919
20920 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20921 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20922 dpyinfo->mouse_face_window = Qnil;
20923 dpyinfo->mouse_face_overlay = Qnil;
20924 return cleared;
20925 }
20926
20927
20928 /* EXPORT:
20929 Non-zero if physical cursor of window W is within mouse face. */
20930
20931 int
20932 cursor_in_mouse_face_p (w)
20933 struct window *w;
20934 {
20935 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20936 int in_mouse_face = 0;
20937
20938 if (WINDOWP (dpyinfo->mouse_face_window)
20939 && XWINDOW (dpyinfo->mouse_face_window) == w)
20940 {
20941 int hpos = w->phys_cursor.hpos;
20942 int vpos = w->phys_cursor.vpos;
20943
20944 if (vpos >= dpyinfo->mouse_face_beg_row
20945 && vpos <= dpyinfo->mouse_face_end_row
20946 && (vpos > dpyinfo->mouse_face_beg_row
20947 || hpos >= dpyinfo->mouse_face_beg_col)
20948 && (vpos < dpyinfo->mouse_face_end_row
20949 || hpos < dpyinfo->mouse_face_end_col
20950 || dpyinfo->mouse_face_past_end))
20951 in_mouse_face = 1;
20952 }
20953
20954 return in_mouse_face;
20955 }
20956
20957
20958
20959 \f
20960 /* Find the glyph matrix position of buffer position CHARPOS in window
20961 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20962 current glyphs must be up to date. If CHARPOS is above window
20963 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20964 of last line in W. In the row containing CHARPOS, stop before glyphs
20965 having STOP as object. */
20966
20967 #if 1 /* This is a version of fast_find_position that's more correct
20968 in the presence of hscrolling, for example. I didn't install
20969 it right away because the problem fixed is minor, it failed
20970 in 20.x as well, and I think it's too risky to install
20971 so near the release of 21.1. 2001-09-25 gerd. */
20972
20973 static int
20974 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20975 struct window *w;
20976 int charpos;
20977 int *hpos, *vpos, *x, *y;
20978 Lisp_Object stop;
20979 {
20980 struct glyph_row *row, *first;
20981 struct glyph *glyph, *end;
20982 int past_end = 0;
20983
20984 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20985 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20986 {
20987 *x = first->x;
20988 *y = first->y;
20989 *hpos = 0;
20990 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20991 return 1;
20992 }
20993
20994 row = row_containing_pos (w, charpos, first, NULL, 0);
20995 if (row == NULL)
20996 {
20997 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20998 past_end = 1;
20999 }
21000
21001 /* If whole rows or last part of a row came from a display overlay,
21002 row_containing_pos will skip over such rows because their end pos
21003 equals the start pos of the overlay or interval.
21004
21005 Move back if we have a STOP object and previous row's
21006 end glyph came from STOP. */
21007 if (!NILP (stop))
21008 {
21009 struct glyph_row *prev;
21010 while ((prev = row - 1, prev >= first)
21011 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21012 && prev->used[TEXT_AREA] > 0)
21013 {
21014 struct glyph *beg = prev->glyphs[TEXT_AREA];
21015 glyph = beg + prev->used[TEXT_AREA];
21016 while (--glyph >= beg
21017 && INTEGERP (glyph->object));
21018 if (glyph < beg
21019 || !EQ (stop, glyph->object))
21020 break;
21021 row = prev;
21022 }
21023 }
21024
21025 *x = row->x;
21026 *y = row->y;
21027 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21028
21029 glyph = row->glyphs[TEXT_AREA];
21030 end = glyph + row->used[TEXT_AREA];
21031
21032 /* Skip over glyphs not having an object at the start of the row.
21033 These are special glyphs like truncation marks on terminal
21034 frames. */
21035 if (row->displays_text_p)
21036 while (glyph < end
21037 && INTEGERP (glyph->object)
21038 && !EQ (stop, glyph->object)
21039 && glyph->charpos < 0)
21040 {
21041 *x += glyph->pixel_width;
21042 ++glyph;
21043 }
21044
21045 while (glyph < end
21046 && !INTEGERP (glyph->object)
21047 && !EQ (stop, glyph->object)
21048 && (!BUFFERP (glyph->object)
21049 || glyph->charpos < charpos))
21050 {
21051 *x += glyph->pixel_width;
21052 ++glyph;
21053 }
21054
21055 *hpos = glyph - row->glyphs[TEXT_AREA];
21056 return !past_end;
21057 }
21058
21059 #else /* not 1 */
21060
21061 static int
21062 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21063 struct window *w;
21064 int pos;
21065 int *hpos, *vpos, *x, *y;
21066 Lisp_Object stop;
21067 {
21068 int i;
21069 int lastcol;
21070 int maybe_next_line_p = 0;
21071 int line_start_position;
21072 int yb = window_text_bottom_y (w);
21073 struct glyph_row *row, *best_row;
21074 int row_vpos, best_row_vpos;
21075 int current_x;
21076
21077 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21078 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21079
21080 while (row->y < yb)
21081 {
21082 if (row->used[TEXT_AREA])
21083 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21084 else
21085 line_start_position = 0;
21086
21087 if (line_start_position > pos)
21088 break;
21089 /* If the position sought is the end of the buffer,
21090 don't include the blank lines at the bottom of the window. */
21091 else if (line_start_position == pos
21092 && pos == BUF_ZV (XBUFFER (w->buffer)))
21093 {
21094 maybe_next_line_p = 1;
21095 break;
21096 }
21097 else if (line_start_position > 0)
21098 {
21099 best_row = row;
21100 best_row_vpos = row_vpos;
21101 }
21102
21103 if (row->y + row->height >= yb)
21104 break;
21105
21106 ++row;
21107 ++row_vpos;
21108 }
21109
21110 /* Find the right column within BEST_ROW. */
21111 lastcol = 0;
21112 current_x = best_row->x;
21113 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21114 {
21115 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21116 int charpos = glyph->charpos;
21117
21118 if (BUFFERP (glyph->object))
21119 {
21120 if (charpos == pos)
21121 {
21122 *hpos = i;
21123 *vpos = best_row_vpos;
21124 *x = current_x;
21125 *y = best_row->y;
21126 return 1;
21127 }
21128 else if (charpos > pos)
21129 break;
21130 }
21131 else if (EQ (glyph->object, stop))
21132 break;
21133
21134 if (charpos > 0)
21135 lastcol = i;
21136 current_x += glyph->pixel_width;
21137 }
21138
21139 /* If we're looking for the end of the buffer,
21140 and we didn't find it in the line we scanned,
21141 use the start of the following line. */
21142 if (maybe_next_line_p)
21143 {
21144 ++best_row;
21145 ++best_row_vpos;
21146 lastcol = 0;
21147 current_x = best_row->x;
21148 }
21149
21150 *vpos = best_row_vpos;
21151 *hpos = lastcol + 1;
21152 *x = current_x;
21153 *y = best_row->y;
21154 return 0;
21155 }
21156
21157 #endif /* not 1 */
21158
21159
21160 /* Find the position of the glyph for position POS in OBJECT in
21161 window W's current matrix, and return in *X, *Y the pixel
21162 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21163
21164 RIGHT_P non-zero means return the position of the right edge of the
21165 glyph, RIGHT_P zero means return the left edge position.
21166
21167 If no glyph for POS exists in the matrix, return the position of
21168 the glyph with the next smaller position that is in the matrix, if
21169 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21170 exists in the matrix, return the position of the glyph with the
21171 next larger position in OBJECT.
21172
21173 Value is non-zero if a glyph was found. */
21174
21175 static int
21176 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21177 struct window *w;
21178 int pos;
21179 Lisp_Object object;
21180 int *hpos, *vpos, *x, *y;
21181 int right_p;
21182 {
21183 int yb = window_text_bottom_y (w);
21184 struct glyph_row *r;
21185 struct glyph *best_glyph = NULL;
21186 struct glyph_row *best_row = NULL;
21187 int best_x = 0;
21188
21189 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21190 r->enabled_p && r->y < yb;
21191 ++r)
21192 {
21193 struct glyph *g = r->glyphs[TEXT_AREA];
21194 struct glyph *e = g + r->used[TEXT_AREA];
21195 int gx;
21196
21197 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21198 if (EQ (g->object, object))
21199 {
21200 if (g->charpos == pos)
21201 {
21202 best_glyph = g;
21203 best_x = gx;
21204 best_row = r;
21205 goto found;
21206 }
21207 else if (best_glyph == NULL
21208 || ((abs (g->charpos - pos)
21209 < abs (best_glyph->charpos - pos))
21210 && (right_p
21211 ? g->charpos < pos
21212 : g->charpos > pos)))
21213 {
21214 best_glyph = g;
21215 best_x = gx;
21216 best_row = r;
21217 }
21218 }
21219 }
21220
21221 found:
21222
21223 if (best_glyph)
21224 {
21225 *x = best_x;
21226 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21227
21228 if (right_p)
21229 {
21230 *x += best_glyph->pixel_width;
21231 ++*hpos;
21232 }
21233
21234 *y = best_row->y;
21235 *vpos = best_row - w->current_matrix->rows;
21236 }
21237
21238 return best_glyph != NULL;
21239 }
21240
21241
21242 /* See if position X, Y is within a hot-spot of an image. */
21243
21244 static int
21245 on_hot_spot_p (hot_spot, x, y)
21246 Lisp_Object hot_spot;
21247 int x, y;
21248 {
21249 if (!CONSP (hot_spot))
21250 return 0;
21251
21252 if (EQ (XCAR (hot_spot), Qrect))
21253 {
21254 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21255 Lisp_Object rect = XCDR (hot_spot);
21256 Lisp_Object tem;
21257 if (!CONSP (rect))
21258 return 0;
21259 if (!CONSP (XCAR (rect)))
21260 return 0;
21261 if (!CONSP (XCDR (rect)))
21262 return 0;
21263 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21264 return 0;
21265 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21266 return 0;
21267 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21268 return 0;
21269 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21270 return 0;
21271 return 1;
21272 }
21273 else if (EQ (XCAR (hot_spot), Qcircle))
21274 {
21275 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21276 Lisp_Object circ = XCDR (hot_spot);
21277 Lisp_Object lr, lx0, ly0;
21278 if (CONSP (circ)
21279 && CONSP (XCAR (circ))
21280 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21281 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21282 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21283 {
21284 double r = XFLOATINT (lr);
21285 double dx = XINT (lx0) - x;
21286 double dy = XINT (ly0) - y;
21287 return (dx * dx + dy * dy <= r * r);
21288 }
21289 }
21290 else if (EQ (XCAR (hot_spot), Qpoly))
21291 {
21292 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21293 if (VECTORP (XCDR (hot_spot)))
21294 {
21295 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21296 Lisp_Object *poly = v->contents;
21297 int n = v->size;
21298 int i;
21299 int inside = 0;
21300 Lisp_Object lx, ly;
21301 int x0, y0;
21302
21303 /* Need an even number of coordinates, and at least 3 edges. */
21304 if (n < 6 || n & 1)
21305 return 0;
21306
21307 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21308 If count is odd, we are inside polygon. Pixels on edges
21309 may or may not be included depending on actual geometry of the
21310 polygon. */
21311 if ((lx = poly[n-2], !INTEGERP (lx))
21312 || (ly = poly[n-1], !INTEGERP (lx)))
21313 return 0;
21314 x0 = XINT (lx), y0 = XINT (ly);
21315 for (i = 0; i < n; i += 2)
21316 {
21317 int x1 = x0, y1 = y0;
21318 if ((lx = poly[i], !INTEGERP (lx))
21319 || (ly = poly[i+1], !INTEGERP (ly)))
21320 return 0;
21321 x0 = XINT (lx), y0 = XINT (ly);
21322
21323 /* Does this segment cross the X line? */
21324 if (x0 >= x)
21325 {
21326 if (x1 >= x)
21327 continue;
21328 }
21329 else if (x1 < x)
21330 continue;
21331 if (y > y0 && y > y1)
21332 continue;
21333 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21334 inside = !inside;
21335 }
21336 return inside;
21337 }
21338 }
21339 return 0;
21340 }
21341
21342 Lisp_Object
21343 find_hot_spot (map, x, y)
21344 Lisp_Object map;
21345 int x, y;
21346 {
21347 while (CONSP (map))
21348 {
21349 if (CONSP (XCAR (map))
21350 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21351 return XCAR (map);
21352 map = XCDR (map);
21353 }
21354
21355 return Qnil;
21356 }
21357
21358 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21359 3, 3, 0,
21360 doc: /* Lookup in image map MAP coordinates X and Y.
21361 An image map is an alist where each element has the format (AREA ID PLIST).
21362 An AREA is specified as either a rectangle, a circle, or a polygon:
21363 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21364 pixel coordinates of the upper left and bottom right corners.
21365 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21366 and the radius of the circle; r may be a float or integer.
21367 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21368 vector describes one corner in the polygon.
21369 Returns the alist element for the first matching AREA in MAP. */)
21370 (map, x, y)
21371 Lisp_Object map;
21372 Lisp_Object x, y;
21373 {
21374 if (NILP (map))
21375 return Qnil;
21376
21377 CHECK_NUMBER (x);
21378 CHECK_NUMBER (y);
21379
21380 return find_hot_spot (map, XINT (x), XINT (y));
21381 }
21382
21383
21384 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21385 static void
21386 define_frame_cursor1 (f, cursor, pointer)
21387 struct frame *f;
21388 Cursor cursor;
21389 Lisp_Object pointer;
21390 {
21391 /* Do not change cursor shape while dragging mouse. */
21392 if (!NILP (do_mouse_tracking))
21393 return;
21394
21395 if (!NILP (pointer))
21396 {
21397 if (EQ (pointer, Qarrow))
21398 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21399 else if (EQ (pointer, Qhand))
21400 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21401 else if (EQ (pointer, Qtext))
21402 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21403 else if (EQ (pointer, intern ("hdrag")))
21404 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21405 #ifdef HAVE_X_WINDOWS
21406 else if (EQ (pointer, intern ("vdrag")))
21407 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21408 #endif
21409 else if (EQ (pointer, intern ("hourglass")))
21410 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21411 else if (EQ (pointer, Qmodeline))
21412 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21413 else
21414 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21415 }
21416
21417 if (cursor != No_Cursor)
21418 FRAME_RIF (f)->define_frame_cursor (f, cursor);
21419 }
21420
21421 /* Take proper action when mouse has moved to the mode or header line
21422 or marginal area AREA of window W, x-position X and y-position Y.
21423 X is relative to the start of the text display area of W, so the
21424 width of bitmap areas and scroll bars must be subtracted to get a
21425 position relative to the start of the mode line. */
21426
21427 static void
21428 note_mode_line_or_margin_highlight (window, x, y, area)
21429 Lisp_Object window;
21430 int x, y;
21431 enum window_part area;
21432 {
21433 struct window *w = XWINDOW (window);
21434 struct frame *f = XFRAME (w->frame);
21435 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21436 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21437 Lisp_Object pointer = Qnil;
21438 int charpos, dx, dy, width, height;
21439 Lisp_Object string, object = Qnil;
21440 Lisp_Object pos, help;
21441
21442 Lisp_Object mouse_face;
21443 int original_x_pixel = x;
21444 struct glyph * glyph = NULL;
21445 struct glyph_row *row;
21446
21447 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21448 {
21449 int x0;
21450 struct glyph *end;
21451
21452 string = mode_line_string (w, area, &x, &y, &charpos,
21453 &object, &dx, &dy, &width, &height);
21454
21455 row = (area == ON_MODE_LINE
21456 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21457 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21458
21459 /* Find glyph */
21460 if (row->mode_line_p && row->enabled_p)
21461 {
21462 glyph = row->glyphs[TEXT_AREA];
21463 end = glyph + row->used[TEXT_AREA];
21464
21465 for (x0 = original_x_pixel;
21466 glyph < end && x0 >= glyph->pixel_width;
21467 ++glyph)
21468 x0 -= glyph->pixel_width;
21469
21470 if (glyph >= end)
21471 glyph = NULL;
21472 }
21473 }
21474 else
21475 {
21476 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21477 string = marginal_area_string (w, area, &x, &y, &charpos,
21478 &object, &dx, &dy, &width, &height);
21479 }
21480
21481 help = Qnil;
21482
21483 if (IMAGEP (object))
21484 {
21485 Lisp_Object image_map, hotspot;
21486 if ((image_map = Fplist_get (XCDR (object), QCmap),
21487 !NILP (image_map))
21488 && (hotspot = find_hot_spot (image_map, dx, dy),
21489 CONSP (hotspot))
21490 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21491 {
21492 Lisp_Object area_id, plist;
21493
21494 area_id = XCAR (hotspot);
21495 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21496 If so, we could look for mouse-enter, mouse-leave
21497 properties in PLIST (and do something...). */
21498 hotspot = XCDR (hotspot);
21499 if (CONSP (hotspot)
21500 && (plist = XCAR (hotspot), CONSP (plist)))
21501 {
21502 pointer = Fplist_get (plist, Qpointer);
21503 if (NILP (pointer))
21504 pointer = Qhand;
21505 help = Fplist_get (plist, Qhelp_echo);
21506 if (!NILP (help))
21507 {
21508 help_echo_string = help;
21509 /* Is this correct? ++kfs */
21510 XSETWINDOW (help_echo_window, w);
21511 help_echo_object = w->buffer;
21512 help_echo_pos = charpos;
21513 }
21514 }
21515 }
21516 if (NILP (pointer))
21517 pointer = Fplist_get (XCDR (object), QCpointer);
21518 }
21519
21520 if (STRINGP (string))
21521 {
21522 pos = make_number (charpos);
21523 /* If we're on a string with `help-echo' text property, arrange
21524 for the help to be displayed. This is done by setting the
21525 global variable help_echo_string to the help string. */
21526 if (NILP (help))
21527 {
21528 help = Fget_text_property (pos, Qhelp_echo, string);
21529 if (!NILP (help))
21530 {
21531 help_echo_string = help;
21532 XSETWINDOW (help_echo_window, w);
21533 help_echo_object = string;
21534 help_echo_pos = charpos;
21535 }
21536 }
21537
21538 if (NILP (pointer))
21539 pointer = Fget_text_property (pos, Qpointer, string);
21540
21541 /* Change the mouse pointer according to what is under X/Y. */
21542 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21543 {
21544 Lisp_Object map;
21545 map = Fget_text_property (pos, Qlocal_map, string);
21546 if (!KEYMAPP (map))
21547 map = Fget_text_property (pos, Qkeymap, string);
21548 if (!KEYMAPP (map))
21549 cursor = dpyinfo->vertical_scroll_bar_cursor;
21550 }
21551
21552 /* Change the mouse face according to what is under X/Y. */
21553 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21554 if (!NILP (mouse_face)
21555 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21556 && glyph)
21557 {
21558 Lisp_Object b, e;
21559
21560 struct glyph * tmp_glyph;
21561
21562 int gpos;
21563 int gseq_length;
21564 int total_pixel_width;
21565 int ignore;
21566
21567 int vpos, hpos;
21568
21569 b = Fprevious_single_property_change (make_number (charpos + 1),
21570 Qmouse_face, string, Qnil);
21571 if (NILP (b))
21572 b = make_number (0);
21573
21574 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21575 if (NILP (e))
21576 e = make_number (SCHARS (string));
21577
21578 /* Calculate the position(glyph position: GPOS) of GLYPH in
21579 displayed string. GPOS is different from CHARPOS.
21580
21581 CHARPOS is the position of glyph in internal string
21582 object. A mode line string format has structures which
21583 is converted to a flatten by emacs lisp interpreter.
21584 The internal string is an element of the structures.
21585 The displayed string is the flatten string. */
21586 for (tmp_glyph = glyph - 1, gpos = 0;
21587 tmp_glyph->charpos >= XINT (b);
21588 tmp_glyph--, gpos++)
21589 {
21590 if (!EQ (tmp_glyph->object, glyph->object))
21591 break;
21592 }
21593
21594 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21595 displayed string holding GLYPH.
21596
21597 GSEQ_LENGTH is different from SCHARS (STRING).
21598 SCHARS (STRING) returns the length of the internal string. */
21599 for (tmp_glyph = glyph, gseq_length = gpos;
21600 tmp_glyph->charpos < XINT (e);
21601 tmp_glyph++, gseq_length++)
21602 {
21603 if (!EQ (tmp_glyph->object, glyph->object))
21604 break;
21605 }
21606
21607 total_pixel_width = 0;
21608 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21609 total_pixel_width += tmp_glyph->pixel_width;
21610
21611 /* Pre calculation of re-rendering position */
21612 vpos = (x - gpos);
21613 hpos = (area == ON_MODE_LINE
21614 ? (w->current_matrix)->nrows - 1
21615 : 0);
21616
21617 /* If the re-rendering position is included in the last
21618 re-rendering area, we should do nothing. */
21619 if ( EQ (window, dpyinfo->mouse_face_window)
21620 && dpyinfo->mouse_face_beg_col <= vpos
21621 && vpos < dpyinfo->mouse_face_end_col
21622 && dpyinfo->mouse_face_beg_row == hpos )
21623 return;
21624
21625 if (clear_mouse_face (dpyinfo))
21626 cursor = No_Cursor;
21627
21628 dpyinfo->mouse_face_beg_col = vpos;
21629 dpyinfo->mouse_face_beg_row = hpos;
21630
21631 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21632 dpyinfo->mouse_face_beg_y = 0;
21633
21634 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21635 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21636
21637 dpyinfo->mouse_face_end_x = 0;
21638 dpyinfo->mouse_face_end_y = 0;
21639
21640 dpyinfo->mouse_face_past_end = 0;
21641 dpyinfo->mouse_face_window = window;
21642
21643 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21644 charpos,
21645 0, 0, 0, &ignore,
21646 glyph->face_id, 1);
21647 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21648
21649 if (NILP (pointer))
21650 pointer = Qhand;
21651 }
21652 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21653 clear_mouse_face (dpyinfo);
21654 }
21655 define_frame_cursor1 (f, cursor, pointer);
21656 }
21657
21658
21659 /* EXPORT:
21660 Take proper action when the mouse has moved to position X, Y on
21661 frame F as regards highlighting characters that have mouse-face
21662 properties. Also de-highlighting chars where the mouse was before.
21663 X and Y can be negative or out of range. */
21664
21665 void
21666 note_mouse_highlight (f, x, y)
21667 struct frame *f;
21668 int x, y;
21669 {
21670 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21671 enum window_part part;
21672 Lisp_Object window;
21673 struct window *w;
21674 Cursor cursor = No_Cursor;
21675 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21676 struct buffer *b;
21677
21678 /* When a menu is active, don't highlight because this looks odd. */
21679 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21680 if (popup_activated ())
21681 return;
21682 #endif
21683
21684 if (NILP (Vmouse_highlight)
21685 || !f->glyphs_initialized_p)
21686 return;
21687
21688 dpyinfo->mouse_face_mouse_x = x;
21689 dpyinfo->mouse_face_mouse_y = y;
21690 dpyinfo->mouse_face_mouse_frame = f;
21691
21692 if (dpyinfo->mouse_face_defer)
21693 return;
21694
21695 if (gc_in_progress)
21696 {
21697 dpyinfo->mouse_face_deferred_gc = 1;
21698 return;
21699 }
21700
21701 /* Which window is that in? */
21702 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21703
21704 /* If we were displaying active text in another window, clear that.
21705 Also clear if we move out of text area in same window. */
21706 if (! EQ (window, dpyinfo->mouse_face_window)
21707 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21708 && !NILP (dpyinfo->mouse_face_window)))
21709 clear_mouse_face (dpyinfo);
21710
21711 /* Not on a window -> return. */
21712 if (!WINDOWP (window))
21713 return;
21714
21715 /* Reset help_echo_string. It will get recomputed below. */
21716 help_echo_string = Qnil;
21717
21718 /* Convert to window-relative pixel coordinates. */
21719 w = XWINDOW (window);
21720 frame_to_window_pixel_xy (w, &x, &y);
21721
21722 /* Handle tool-bar window differently since it doesn't display a
21723 buffer. */
21724 if (EQ (window, f->tool_bar_window))
21725 {
21726 note_tool_bar_highlight (f, x, y);
21727 return;
21728 }
21729
21730 /* Mouse is on the mode, header line or margin? */
21731 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21732 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21733 {
21734 note_mode_line_or_margin_highlight (window, x, y, part);
21735 return;
21736 }
21737
21738 if (part == ON_VERTICAL_BORDER)
21739 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21740 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21741 || part == ON_SCROLL_BAR)
21742 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21743 else
21744 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21745
21746 /* Are we in a window whose display is up to date?
21747 And verify the buffer's text has not changed. */
21748 b = XBUFFER (w->buffer);
21749 if (part == ON_TEXT
21750 && EQ (w->window_end_valid, w->buffer)
21751 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21752 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21753 {
21754 int hpos, vpos, pos, i, dx, dy, area;
21755 struct glyph *glyph;
21756 Lisp_Object object;
21757 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21758 Lisp_Object *overlay_vec = NULL;
21759 int noverlays;
21760 struct buffer *obuf;
21761 int obegv, ozv, same_region;
21762
21763 /* Find the glyph under X/Y. */
21764 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21765
21766 /* Look for :pointer property on image. */
21767 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21768 {
21769 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21770 if (img != NULL && IMAGEP (img->spec))
21771 {
21772 Lisp_Object image_map, hotspot;
21773 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21774 !NILP (image_map))
21775 && (hotspot = find_hot_spot (image_map,
21776 glyph->slice.x + dx,
21777 glyph->slice.y + dy),
21778 CONSP (hotspot))
21779 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21780 {
21781 Lisp_Object area_id, plist;
21782
21783 area_id = XCAR (hotspot);
21784 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21785 If so, we could look for mouse-enter, mouse-leave
21786 properties in PLIST (and do something...). */
21787 hotspot = XCDR (hotspot);
21788 if (CONSP (hotspot)
21789 && (plist = XCAR (hotspot), CONSP (plist)))
21790 {
21791 pointer = Fplist_get (plist, Qpointer);
21792 if (NILP (pointer))
21793 pointer = Qhand;
21794 help_echo_string = Fplist_get (plist, Qhelp_echo);
21795 if (!NILP (help_echo_string))
21796 {
21797 help_echo_window = window;
21798 help_echo_object = glyph->object;
21799 help_echo_pos = glyph->charpos;
21800 }
21801 }
21802 }
21803 if (NILP (pointer))
21804 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21805 }
21806 }
21807
21808 /* Clear mouse face if X/Y not over text. */
21809 if (glyph == NULL
21810 || area != TEXT_AREA
21811 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21812 {
21813 if (clear_mouse_face (dpyinfo))
21814 cursor = No_Cursor;
21815 if (NILP (pointer))
21816 {
21817 if (area != TEXT_AREA)
21818 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21819 else
21820 pointer = Vvoid_text_area_pointer;
21821 }
21822 goto set_cursor;
21823 }
21824
21825 pos = glyph->charpos;
21826 object = glyph->object;
21827 if (!STRINGP (object) && !BUFFERP (object))
21828 goto set_cursor;
21829
21830 /* If we get an out-of-range value, return now; avoid an error. */
21831 if (BUFFERP (object) && pos > BUF_Z (b))
21832 goto set_cursor;
21833
21834 /* Make the window's buffer temporarily current for
21835 overlays_at and compute_char_face. */
21836 obuf = current_buffer;
21837 current_buffer = b;
21838 obegv = BEGV;
21839 ozv = ZV;
21840 BEGV = BEG;
21841 ZV = Z;
21842
21843 /* Is this char mouse-active or does it have help-echo? */
21844 position = make_number (pos);
21845
21846 if (BUFFERP (object))
21847 {
21848 /* Put all the overlays we want in a vector in overlay_vec. */
21849 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21850 /* Sort overlays into increasing priority order. */
21851 noverlays = sort_overlays (overlay_vec, noverlays, w);
21852 }
21853 else
21854 noverlays = 0;
21855
21856 same_region = (EQ (window, dpyinfo->mouse_face_window)
21857 && vpos >= dpyinfo->mouse_face_beg_row
21858 && vpos <= dpyinfo->mouse_face_end_row
21859 && (vpos > dpyinfo->mouse_face_beg_row
21860 || hpos >= dpyinfo->mouse_face_beg_col)
21861 && (vpos < dpyinfo->mouse_face_end_row
21862 || hpos < dpyinfo->mouse_face_end_col
21863 || dpyinfo->mouse_face_past_end));
21864
21865 if (same_region)
21866 cursor = No_Cursor;
21867
21868 /* Check mouse-face highlighting. */
21869 if (! same_region
21870 /* If there exists an overlay with mouse-face overlapping
21871 the one we are currently highlighting, we have to
21872 check if we enter the overlapping overlay, and then
21873 highlight only that. */
21874 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21875 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21876 {
21877 /* Find the highest priority overlay that has a mouse-face
21878 property. */
21879 overlay = Qnil;
21880 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21881 {
21882 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21883 if (!NILP (mouse_face))
21884 overlay = overlay_vec[i];
21885 }
21886
21887 /* If we're actually highlighting the same overlay as
21888 before, there's no need to do that again. */
21889 if (!NILP (overlay)
21890 && EQ (overlay, dpyinfo->mouse_face_overlay))
21891 goto check_help_echo;
21892
21893 dpyinfo->mouse_face_overlay = overlay;
21894
21895 /* Clear the display of the old active region, if any. */
21896 if (clear_mouse_face (dpyinfo))
21897 cursor = No_Cursor;
21898
21899 /* If no overlay applies, get a text property. */
21900 if (NILP (overlay))
21901 mouse_face = Fget_text_property (position, Qmouse_face, object);
21902
21903 /* Handle the overlay case. */
21904 if (!NILP (overlay))
21905 {
21906 /* Find the range of text around this char that
21907 should be active. */
21908 Lisp_Object before, after;
21909 int ignore;
21910
21911 before = Foverlay_start (overlay);
21912 after = Foverlay_end (overlay);
21913 /* Record this as the current active region. */
21914 fast_find_position (w, XFASTINT (before),
21915 &dpyinfo->mouse_face_beg_col,
21916 &dpyinfo->mouse_face_beg_row,
21917 &dpyinfo->mouse_face_beg_x,
21918 &dpyinfo->mouse_face_beg_y, Qnil);
21919
21920 dpyinfo->mouse_face_past_end
21921 = !fast_find_position (w, XFASTINT (after),
21922 &dpyinfo->mouse_face_end_col,
21923 &dpyinfo->mouse_face_end_row,
21924 &dpyinfo->mouse_face_end_x,
21925 &dpyinfo->mouse_face_end_y, Qnil);
21926 dpyinfo->mouse_face_window = window;
21927
21928 dpyinfo->mouse_face_face_id
21929 = face_at_buffer_position (w, pos, 0, 0,
21930 &ignore, pos + 1,
21931 !dpyinfo->mouse_face_hidden);
21932
21933 /* Display it as active. */
21934 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21935 cursor = No_Cursor;
21936 }
21937 /* Handle the text property case. */
21938 else if (!NILP (mouse_face) && BUFFERP (object))
21939 {
21940 /* Find the range of text around this char that
21941 should be active. */
21942 Lisp_Object before, after, beginning, end;
21943 int ignore;
21944
21945 beginning = Fmarker_position (w->start);
21946 end = make_number (BUF_Z (XBUFFER (object))
21947 - XFASTINT (w->window_end_pos));
21948 before
21949 = Fprevious_single_property_change (make_number (pos + 1),
21950 Qmouse_face,
21951 object, beginning);
21952 after
21953 = Fnext_single_property_change (position, Qmouse_face,
21954 object, end);
21955
21956 /* Record this as the current active region. */
21957 fast_find_position (w, XFASTINT (before),
21958 &dpyinfo->mouse_face_beg_col,
21959 &dpyinfo->mouse_face_beg_row,
21960 &dpyinfo->mouse_face_beg_x,
21961 &dpyinfo->mouse_face_beg_y, Qnil);
21962 dpyinfo->mouse_face_past_end
21963 = !fast_find_position (w, XFASTINT (after),
21964 &dpyinfo->mouse_face_end_col,
21965 &dpyinfo->mouse_face_end_row,
21966 &dpyinfo->mouse_face_end_x,
21967 &dpyinfo->mouse_face_end_y, Qnil);
21968 dpyinfo->mouse_face_window = window;
21969
21970 if (BUFFERP (object))
21971 dpyinfo->mouse_face_face_id
21972 = face_at_buffer_position (w, pos, 0, 0,
21973 &ignore, pos + 1,
21974 !dpyinfo->mouse_face_hidden);
21975
21976 /* Display it as active. */
21977 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21978 cursor = No_Cursor;
21979 }
21980 else if (!NILP (mouse_face) && STRINGP (object))
21981 {
21982 Lisp_Object b, e;
21983 int ignore;
21984
21985 b = Fprevious_single_property_change (make_number (pos + 1),
21986 Qmouse_face,
21987 object, Qnil);
21988 e = Fnext_single_property_change (position, Qmouse_face,
21989 object, Qnil);
21990 if (NILP (b))
21991 b = make_number (0);
21992 if (NILP (e))
21993 e = make_number (SCHARS (object) - 1);
21994
21995 fast_find_string_pos (w, XINT (b), object,
21996 &dpyinfo->mouse_face_beg_col,
21997 &dpyinfo->mouse_face_beg_row,
21998 &dpyinfo->mouse_face_beg_x,
21999 &dpyinfo->mouse_face_beg_y, 0);
22000 fast_find_string_pos (w, XINT (e), object,
22001 &dpyinfo->mouse_face_end_col,
22002 &dpyinfo->mouse_face_end_row,
22003 &dpyinfo->mouse_face_end_x,
22004 &dpyinfo->mouse_face_end_y, 1);
22005 dpyinfo->mouse_face_past_end = 0;
22006 dpyinfo->mouse_face_window = window;
22007 dpyinfo->mouse_face_face_id
22008 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22009 glyph->face_id, 1);
22010 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22011 cursor = No_Cursor;
22012 }
22013 else if (STRINGP (object) && NILP (mouse_face))
22014 {
22015 /* A string which doesn't have mouse-face, but
22016 the text ``under'' it might have. */
22017 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22018 int start = MATRIX_ROW_START_CHARPOS (r);
22019
22020 pos = string_buffer_position (w, object, start);
22021 if (pos > 0)
22022 mouse_face = get_char_property_and_overlay (make_number (pos),
22023 Qmouse_face,
22024 w->buffer,
22025 &overlay);
22026 if (!NILP (mouse_face) && !NILP (overlay))
22027 {
22028 Lisp_Object before = Foverlay_start (overlay);
22029 Lisp_Object after = Foverlay_end (overlay);
22030 int ignore;
22031
22032 /* Note that we might not be able to find position
22033 BEFORE in the glyph matrix if the overlay is
22034 entirely covered by a `display' property. In
22035 this case, we overshoot. So let's stop in
22036 the glyph matrix before glyphs for OBJECT. */
22037 fast_find_position (w, XFASTINT (before),
22038 &dpyinfo->mouse_face_beg_col,
22039 &dpyinfo->mouse_face_beg_row,
22040 &dpyinfo->mouse_face_beg_x,
22041 &dpyinfo->mouse_face_beg_y,
22042 object);
22043
22044 dpyinfo->mouse_face_past_end
22045 = !fast_find_position (w, XFASTINT (after),
22046 &dpyinfo->mouse_face_end_col,
22047 &dpyinfo->mouse_face_end_row,
22048 &dpyinfo->mouse_face_end_x,
22049 &dpyinfo->mouse_face_end_y,
22050 Qnil);
22051 dpyinfo->mouse_face_window = window;
22052 dpyinfo->mouse_face_face_id
22053 = face_at_buffer_position (w, pos, 0, 0,
22054 &ignore, pos + 1,
22055 !dpyinfo->mouse_face_hidden);
22056
22057 /* Display it as active. */
22058 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22059 cursor = No_Cursor;
22060 }
22061 }
22062 }
22063
22064 check_help_echo:
22065
22066 /* Look for a `help-echo' property. */
22067 if (NILP (help_echo_string)) {
22068 Lisp_Object help, overlay;
22069
22070 /* Check overlays first. */
22071 help = overlay = Qnil;
22072 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22073 {
22074 overlay = overlay_vec[i];
22075 help = Foverlay_get (overlay, Qhelp_echo);
22076 }
22077
22078 if (!NILP (help))
22079 {
22080 help_echo_string = help;
22081 help_echo_window = window;
22082 help_echo_object = overlay;
22083 help_echo_pos = pos;
22084 }
22085 else
22086 {
22087 Lisp_Object object = glyph->object;
22088 int charpos = glyph->charpos;
22089
22090 /* Try text properties. */
22091 if (STRINGP (object)
22092 && charpos >= 0
22093 && charpos < SCHARS (object))
22094 {
22095 help = Fget_text_property (make_number (charpos),
22096 Qhelp_echo, object);
22097 if (NILP (help))
22098 {
22099 /* If the string itself doesn't specify a help-echo,
22100 see if the buffer text ``under'' it does. */
22101 struct glyph_row *r
22102 = MATRIX_ROW (w->current_matrix, vpos);
22103 int start = MATRIX_ROW_START_CHARPOS (r);
22104 int pos = string_buffer_position (w, object, start);
22105 if (pos > 0)
22106 {
22107 help = Fget_char_property (make_number (pos),
22108 Qhelp_echo, w->buffer);
22109 if (!NILP (help))
22110 {
22111 charpos = pos;
22112 object = w->buffer;
22113 }
22114 }
22115 }
22116 }
22117 else if (BUFFERP (object)
22118 && charpos >= BEGV
22119 && charpos < ZV)
22120 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22121 object);
22122
22123 if (!NILP (help))
22124 {
22125 help_echo_string = help;
22126 help_echo_window = window;
22127 help_echo_object = object;
22128 help_echo_pos = charpos;
22129 }
22130 }
22131 }
22132
22133 /* Look for a `pointer' property. */
22134 if (NILP (pointer))
22135 {
22136 /* Check overlays first. */
22137 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22138 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22139
22140 if (NILP (pointer))
22141 {
22142 Lisp_Object object = glyph->object;
22143 int charpos = glyph->charpos;
22144
22145 /* Try text properties. */
22146 if (STRINGP (object)
22147 && charpos >= 0
22148 && charpos < SCHARS (object))
22149 {
22150 pointer = Fget_text_property (make_number (charpos),
22151 Qpointer, object);
22152 if (NILP (pointer))
22153 {
22154 /* If the string itself doesn't specify a pointer,
22155 see if the buffer text ``under'' it does. */
22156 struct glyph_row *r
22157 = MATRIX_ROW (w->current_matrix, vpos);
22158 int start = MATRIX_ROW_START_CHARPOS (r);
22159 int pos = string_buffer_position (w, object, start);
22160 if (pos > 0)
22161 pointer = Fget_char_property (make_number (pos),
22162 Qpointer, w->buffer);
22163 }
22164 }
22165 else if (BUFFERP (object)
22166 && charpos >= BEGV
22167 && charpos < ZV)
22168 pointer = Fget_text_property (make_number (charpos),
22169 Qpointer, object);
22170 }
22171 }
22172
22173 BEGV = obegv;
22174 ZV = ozv;
22175 current_buffer = obuf;
22176 }
22177
22178 set_cursor:
22179
22180 define_frame_cursor1 (f, cursor, pointer);
22181 }
22182
22183
22184 /* EXPORT for RIF:
22185 Clear any mouse-face on window W. This function is part of the
22186 redisplay interface, and is called from try_window_id and similar
22187 functions to ensure the mouse-highlight is off. */
22188
22189 void
22190 x_clear_window_mouse_face (w)
22191 struct window *w;
22192 {
22193 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22194 Lisp_Object window;
22195
22196 BLOCK_INPUT;
22197 XSETWINDOW (window, w);
22198 if (EQ (window, dpyinfo->mouse_face_window))
22199 clear_mouse_face (dpyinfo);
22200 UNBLOCK_INPUT;
22201 }
22202
22203
22204 /* EXPORT:
22205 Just discard the mouse face information for frame F, if any.
22206 This is used when the size of F is changed. */
22207
22208 void
22209 cancel_mouse_face (f)
22210 struct frame *f;
22211 {
22212 Lisp_Object window;
22213 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22214
22215 window = dpyinfo->mouse_face_window;
22216 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22217 {
22218 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22219 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22220 dpyinfo->mouse_face_window = Qnil;
22221 }
22222 }
22223
22224
22225 #endif /* HAVE_WINDOW_SYSTEM */
22226
22227 \f
22228 /***********************************************************************
22229 Exposure Events
22230 ***********************************************************************/
22231
22232 #ifdef HAVE_WINDOW_SYSTEM
22233
22234 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22235 which intersects rectangle R. R is in window-relative coordinates. */
22236
22237 static void
22238 expose_area (w, row, r, area)
22239 struct window *w;
22240 struct glyph_row *row;
22241 XRectangle *r;
22242 enum glyph_row_area area;
22243 {
22244 struct glyph *first = row->glyphs[area];
22245 struct glyph *end = row->glyphs[area] + row->used[area];
22246 struct glyph *last;
22247 int first_x, start_x, x;
22248
22249 if (area == TEXT_AREA && row->fill_line_p)
22250 /* If row extends face to end of line write the whole line. */
22251 draw_glyphs (w, 0, row, area,
22252 0, row->used[area],
22253 DRAW_NORMAL_TEXT, 0);
22254 else
22255 {
22256 /* Set START_X to the window-relative start position for drawing glyphs of
22257 AREA. The first glyph of the text area can be partially visible.
22258 The first glyphs of other areas cannot. */
22259 start_x = window_box_left_offset (w, area);
22260 x = start_x;
22261 if (area == TEXT_AREA)
22262 x += row->x;
22263
22264 /* Find the first glyph that must be redrawn. */
22265 while (first < end
22266 && x + first->pixel_width < r->x)
22267 {
22268 x += first->pixel_width;
22269 ++first;
22270 }
22271
22272 /* Find the last one. */
22273 last = first;
22274 first_x = x;
22275 while (last < end
22276 && x < r->x + r->width)
22277 {
22278 x += last->pixel_width;
22279 ++last;
22280 }
22281
22282 /* Repaint. */
22283 if (last > first)
22284 draw_glyphs (w, first_x - start_x, row, area,
22285 first - row->glyphs[area], last - row->glyphs[area],
22286 DRAW_NORMAL_TEXT, 0);
22287 }
22288 }
22289
22290
22291 /* Redraw the parts of the glyph row ROW on window W intersecting
22292 rectangle R. R is in window-relative coordinates. Value is
22293 non-zero if mouse-face was overwritten. */
22294
22295 static int
22296 expose_line (w, row, r)
22297 struct window *w;
22298 struct glyph_row *row;
22299 XRectangle *r;
22300 {
22301 xassert (row->enabled_p);
22302
22303 if (row->mode_line_p || w->pseudo_window_p)
22304 draw_glyphs (w, 0, row, TEXT_AREA,
22305 0, row->used[TEXT_AREA],
22306 DRAW_NORMAL_TEXT, 0);
22307 else
22308 {
22309 if (row->used[LEFT_MARGIN_AREA])
22310 expose_area (w, row, r, LEFT_MARGIN_AREA);
22311 if (row->used[TEXT_AREA])
22312 expose_area (w, row, r, TEXT_AREA);
22313 if (row->used[RIGHT_MARGIN_AREA])
22314 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22315 draw_row_fringe_bitmaps (w, row);
22316 }
22317
22318 return row->mouse_face_p;
22319 }
22320
22321
22322 /* Redraw those parts of glyphs rows during expose event handling that
22323 overlap other rows. Redrawing of an exposed line writes over parts
22324 of lines overlapping that exposed line; this function fixes that.
22325
22326 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22327 row in W's current matrix that is exposed and overlaps other rows.
22328 LAST_OVERLAPPING_ROW is the last such row. */
22329
22330 static void
22331 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22332 struct window *w;
22333 struct glyph_row *first_overlapping_row;
22334 struct glyph_row *last_overlapping_row;
22335 {
22336 struct glyph_row *row;
22337
22338 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22339 if (row->overlapping_p)
22340 {
22341 xassert (row->enabled_p && !row->mode_line_p);
22342
22343 if (row->used[LEFT_MARGIN_AREA])
22344 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22345
22346 if (row->used[TEXT_AREA])
22347 x_fix_overlapping_area (w, row, TEXT_AREA);
22348
22349 if (row->used[RIGHT_MARGIN_AREA])
22350 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22351 }
22352 }
22353
22354
22355 /* Return non-zero if W's cursor intersects rectangle R. */
22356
22357 static int
22358 phys_cursor_in_rect_p (w, r)
22359 struct window *w;
22360 XRectangle *r;
22361 {
22362 XRectangle cr, result;
22363 struct glyph *cursor_glyph;
22364
22365 cursor_glyph = get_phys_cursor_glyph (w);
22366 if (cursor_glyph)
22367 {
22368 /* r is relative to W's box, but w->phys_cursor.x is relative
22369 to left edge of W's TEXT area. Adjust it. */
22370 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22371 cr.y = w->phys_cursor.y;
22372 cr.width = cursor_glyph->pixel_width;
22373 cr.height = w->phys_cursor_height;
22374 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22375 I assume the effect is the same -- and this is portable. */
22376 return x_intersect_rectangles (&cr, r, &result);
22377 }
22378 /* If we don't understand the format, pretend we're not in the hot-spot. */
22379 return 0;
22380 }
22381
22382
22383 /* EXPORT:
22384 Draw a vertical window border to the right of window W if W doesn't
22385 have vertical scroll bars. */
22386
22387 void
22388 x_draw_vertical_border (w)
22389 struct window *w;
22390 {
22391 struct frame *f = XFRAME (WINDOW_FRAME (w));
22392
22393 /* We could do better, if we knew what type of scroll-bar the adjacent
22394 windows (on either side) have... But we don't :-(
22395 However, I think this works ok. ++KFS 2003-04-25 */
22396
22397 /* Redraw borders between horizontally adjacent windows. Don't
22398 do it for frames with vertical scroll bars because either the
22399 right scroll bar of a window, or the left scroll bar of its
22400 neighbor will suffice as a border. */
22401 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22402 return;
22403
22404 if (!WINDOW_RIGHTMOST_P (w)
22405 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22406 {
22407 int x0, x1, y0, y1;
22408
22409 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22410 y1 -= 1;
22411
22412 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22413 x1 -= 1;
22414
22415 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
22416 }
22417 else if (!WINDOW_LEFTMOST_P (w)
22418 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22419 {
22420 int x0, x1, y0, y1;
22421
22422 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22423 y1 -= 1;
22424
22425 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22426 x0 -= 1;
22427
22428 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
22429 }
22430 }
22431
22432
22433 /* Redraw the part of window W intersection rectangle FR. Pixel
22434 coordinates in FR are frame-relative. Call this function with
22435 input blocked. Value is non-zero if the exposure overwrites
22436 mouse-face. */
22437
22438 static int
22439 expose_window (w, fr)
22440 struct window *w;
22441 XRectangle *fr;
22442 {
22443 struct frame *f = XFRAME (w->frame);
22444 XRectangle wr, r;
22445 int mouse_face_overwritten_p = 0;
22446
22447 /* If window is not yet fully initialized, do nothing. This can
22448 happen when toolkit scroll bars are used and a window is split.
22449 Reconfiguring the scroll bar will generate an expose for a newly
22450 created window. */
22451 if (w->current_matrix == NULL)
22452 return 0;
22453
22454 /* When we're currently updating the window, display and current
22455 matrix usually don't agree. Arrange for a thorough display
22456 later. */
22457 if (w == updated_window)
22458 {
22459 SET_FRAME_GARBAGED (f);
22460 return 0;
22461 }
22462
22463 /* Frame-relative pixel rectangle of W. */
22464 wr.x = WINDOW_LEFT_EDGE_X (w);
22465 wr.y = WINDOW_TOP_EDGE_Y (w);
22466 wr.width = WINDOW_TOTAL_WIDTH (w);
22467 wr.height = WINDOW_TOTAL_HEIGHT (w);
22468
22469 if (x_intersect_rectangles (fr, &wr, &r))
22470 {
22471 int yb = window_text_bottom_y (w);
22472 struct glyph_row *row;
22473 int cursor_cleared_p;
22474 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22475
22476 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22477 r.x, r.y, r.width, r.height));
22478
22479 /* Convert to window coordinates. */
22480 r.x -= WINDOW_LEFT_EDGE_X (w);
22481 r.y -= WINDOW_TOP_EDGE_Y (w);
22482
22483 /* Turn off the cursor. */
22484 if (!w->pseudo_window_p
22485 && phys_cursor_in_rect_p (w, &r))
22486 {
22487 x_clear_cursor (w);
22488 cursor_cleared_p = 1;
22489 }
22490 else
22491 cursor_cleared_p = 0;
22492
22493 /* Update lines intersecting rectangle R. */
22494 first_overlapping_row = last_overlapping_row = NULL;
22495 for (row = w->current_matrix->rows;
22496 row->enabled_p;
22497 ++row)
22498 {
22499 int y0 = row->y;
22500 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22501
22502 if ((y0 >= r.y && y0 < r.y + r.height)
22503 || (y1 > r.y && y1 < r.y + r.height)
22504 || (r.y >= y0 && r.y < y1)
22505 || (r.y + r.height > y0 && r.y + r.height < y1))
22506 {
22507 /* A header line may be overlapping, but there is no need
22508 to fix overlapping areas for them. KFS 2005-02-12 */
22509 if (row->overlapping_p && !row->mode_line_p)
22510 {
22511 if (first_overlapping_row == NULL)
22512 first_overlapping_row = row;
22513 last_overlapping_row = row;
22514 }
22515
22516 if (expose_line (w, row, &r))
22517 mouse_face_overwritten_p = 1;
22518 }
22519
22520 if (y1 >= yb)
22521 break;
22522 }
22523
22524 /* Display the mode line if there is one. */
22525 if (WINDOW_WANTS_MODELINE_P (w)
22526 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22527 row->enabled_p)
22528 && row->y < r.y + r.height)
22529 {
22530 if (expose_line (w, row, &r))
22531 mouse_face_overwritten_p = 1;
22532 }
22533
22534 if (!w->pseudo_window_p)
22535 {
22536 /* Fix the display of overlapping rows. */
22537 if (first_overlapping_row)
22538 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22539
22540 /* Draw border between windows. */
22541 x_draw_vertical_border (w);
22542
22543 /* Turn the cursor on again. */
22544 if (cursor_cleared_p)
22545 update_window_cursor (w, 1);
22546 }
22547 }
22548
22549 return mouse_face_overwritten_p;
22550 }
22551
22552
22553
22554 /* Redraw (parts) of all windows in the window tree rooted at W that
22555 intersect R. R contains frame pixel coordinates. Value is
22556 non-zero if the exposure overwrites mouse-face. */
22557
22558 static int
22559 expose_window_tree (w, r)
22560 struct window *w;
22561 XRectangle *r;
22562 {
22563 struct frame *f = XFRAME (w->frame);
22564 int mouse_face_overwritten_p = 0;
22565
22566 while (w && !FRAME_GARBAGED_P (f))
22567 {
22568 if (!NILP (w->hchild))
22569 mouse_face_overwritten_p
22570 |= expose_window_tree (XWINDOW (w->hchild), r);
22571 else if (!NILP (w->vchild))
22572 mouse_face_overwritten_p
22573 |= expose_window_tree (XWINDOW (w->vchild), r);
22574 else
22575 mouse_face_overwritten_p |= expose_window (w, r);
22576
22577 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22578 }
22579
22580 return mouse_face_overwritten_p;
22581 }
22582
22583
22584 /* EXPORT:
22585 Redisplay an exposed area of frame F. X and Y are the upper-left
22586 corner of the exposed rectangle. W and H are width and height of
22587 the exposed area. All are pixel values. W or H zero means redraw
22588 the entire frame. */
22589
22590 void
22591 expose_frame (f, x, y, w, h)
22592 struct frame *f;
22593 int x, y, w, h;
22594 {
22595 XRectangle r;
22596 int mouse_face_overwritten_p = 0;
22597
22598 TRACE ((stderr, "expose_frame "));
22599
22600 /* No need to redraw if frame will be redrawn soon. */
22601 if (FRAME_GARBAGED_P (f))
22602 {
22603 TRACE ((stderr, " garbaged\n"));
22604 return;
22605 }
22606
22607 /* If basic faces haven't been realized yet, there is no point in
22608 trying to redraw anything. This can happen when we get an expose
22609 event while Emacs is starting, e.g. by moving another window. */
22610 if (FRAME_FACE_CACHE (f) == NULL
22611 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22612 {
22613 TRACE ((stderr, " no faces\n"));
22614 return;
22615 }
22616
22617 if (w == 0 || h == 0)
22618 {
22619 r.x = r.y = 0;
22620 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22621 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22622 }
22623 else
22624 {
22625 r.x = x;
22626 r.y = y;
22627 r.width = w;
22628 r.height = h;
22629 }
22630
22631 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22632 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22633
22634 if (WINDOWP (f->tool_bar_window))
22635 mouse_face_overwritten_p
22636 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22637
22638 #ifdef HAVE_X_WINDOWS
22639 #ifndef MSDOS
22640 #ifndef USE_X_TOOLKIT
22641 if (WINDOWP (f->menu_bar_window))
22642 mouse_face_overwritten_p
22643 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22644 #endif /* not USE_X_TOOLKIT */
22645 #endif
22646 #endif
22647
22648 /* Some window managers support a focus-follows-mouse style with
22649 delayed raising of frames. Imagine a partially obscured frame,
22650 and moving the mouse into partially obscured mouse-face on that
22651 frame. The visible part of the mouse-face will be highlighted,
22652 then the WM raises the obscured frame. With at least one WM, KDE
22653 2.1, Emacs is not getting any event for the raising of the frame
22654 (even tried with SubstructureRedirectMask), only Expose events.
22655 These expose events will draw text normally, i.e. not
22656 highlighted. Which means we must redo the highlight here.
22657 Subsume it under ``we love X''. --gerd 2001-08-15 */
22658 /* Included in Windows version because Windows most likely does not
22659 do the right thing if any third party tool offers
22660 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22661 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22662 {
22663 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22664 if (f == dpyinfo->mouse_face_mouse_frame)
22665 {
22666 int x = dpyinfo->mouse_face_mouse_x;
22667 int y = dpyinfo->mouse_face_mouse_y;
22668 clear_mouse_face (dpyinfo);
22669 note_mouse_highlight (f, x, y);
22670 }
22671 }
22672 }
22673
22674
22675 /* EXPORT:
22676 Determine the intersection of two rectangles R1 and R2. Return
22677 the intersection in *RESULT. Value is non-zero if RESULT is not
22678 empty. */
22679
22680 int
22681 x_intersect_rectangles (r1, r2, result)
22682 XRectangle *r1, *r2, *result;
22683 {
22684 XRectangle *left, *right;
22685 XRectangle *upper, *lower;
22686 int intersection_p = 0;
22687
22688 /* Rearrange so that R1 is the left-most rectangle. */
22689 if (r1->x < r2->x)
22690 left = r1, right = r2;
22691 else
22692 left = r2, right = r1;
22693
22694 /* X0 of the intersection is right.x0, if this is inside R1,
22695 otherwise there is no intersection. */
22696 if (right->x <= left->x + left->width)
22697 {
22698 result->x = right->x;
22699
22700 /* The right end of the intersection is the minimum of the
22701 the right ends of left and right. */
22702 result->width = (min (left->x + left->width, right->x + right->width)
22703 - result->x);
22704
22705 /* Same game for Y. */
22706 if (r1->y < r2->y)
22707 upper = r1, lower = r2;
22708 else
22709 upper = r2, lower = r1;
22710
22711 /* The upper end of the intersection is lower.y0, if this is inside
22712 of upper. Otherwise, there is no intersection. */
22713 if (lower->y <= upper->y + upper->height)
22714 {
22715 result->y = lower->y;
22716
22717 /* The lower end of the intersection is the minimum of the lower
22718 ends of upper and lower. */
22719 result->height = (min (lower->y + lower->height,
22720 upper->y + upper->height)
22721 - result->y);
22722 intersection_p = 1;
22723 }
22724 }
22725
22726 return intersection_p;
22727 }
22728
22729 #endif /* HAVE_WINDOW_SYSTEM */
22730
22731 \f
22732 /***********************************************************************
22733 Initialization
22734 ***********************************************************************/
22735
22736 void
22737 syms_of_xdisp ()
22738 {
22739 Vwith_echo_area_save_vector = Qnil;
22740 staticpro (&Vwith_echo_area_save_vector);
22741
22742 Vmessage_stack = Qnil;
22743 staticpro (&Vmessage_stack);
22744
22745 Qinhibit_redisplay = intern ("inhibit-redisplay");
22746 staticpro (&Qinhibit_redisplay);
22747
22748 message_dolog_marker1 = Fmake_marker ();
22749 staticpro (&message_dolog_marker1);
22750 message_dolog_marker2 = Fmake_marker ();
22751 staticpro (&message_dolog_marker2);
22752 message_dolog_marker3 = Fmake_marker ();
22753 staticpro (&message_dolog_marker3);
22754
22755 #if GLYPH_DEBUG
22756 defsubr (&Sdump_frame_glyph_matrix);
22757 defsubr (&Sdump_glyph_matrix);
22758 defsubr (&Sdump_glyph_row);
22759 defsubr (&Sdump_tool_bar_row);
22760 defsubr (&Strace_redisplay);
22761 defsubr (&Strace_to_stderr);
22762 #endif
22763 #ifdef HAVE_WINDOW_SYSTEM
22764 defsubr (&Stool_bar_lines_needed);
22765 defsubr (&Slookup_image_map);
22766 #endif
22767 defsubr (&Sformat_mode_line);
22768
22769 staticpro (&Qmenu_bar_update_hook);
22770 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22771
22772 staticpro (&Qoverriding_terminal_local_map);
22773 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22774
22775 staticpro (&Qoverriding_local_map);
22776 Qoverriding_local_map = intern ("overriding-local-map");
22777
22778 staticpro (&Qwindow_scroll_functions);
22779 Qwindow_scroll_functions = intern ("window-scroll-functions");
22780
22781 staticpro (&Qredisplay_end_trigger_functions);
22782 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22783
22784 staticpro (&Qinhibit_point_motion_hooks);
22785 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22786
22787 QCdata = intern (":data");
22788 staticpro (&QCdata);
22789 Qdisplay = intern ("display");
22790 staticpro (&Qdisplay);
22791 Qspace_width = intern ("space-width");
22792 staticpro (&Qspace_width);
22793 Qraise = intern ("raise");
22794 staticpro (&Qraise);
22795 Qslice = intern ("slice");
22796 staticpro (&Qslice);
22797 Qspace = intern ("space");
22798 staticpro (&Qspace);
22799 Qmargin = intern ("margin");
22800 staticpro (&Qmargin);
22801 Qpointer = intern ("pointer");
22802 staticpro (&Qpointer);
22803 Qleft_margin = intern ("left-margin");
22804 staticpro (&Qleft_margin);
22805 Qright_margin = intern ("right-margin");
22806 staticpro (&Qright_margin);
22807 Qcenter = intern ("center");
22808 staticpro (&Qcenter);
22809 Qline_height = intern ("line-height");
22810 staticpro (&Qline_height);
22811 QCalign_to = intern (":align-to");
22812 staticpro (&QCalign_to);
22813 QCrelative_width = intern (":relative-width");
22814 staticpro (&QCrelative_width);
22815 QCrelative_height = intern (":relative-height");
22816 staticpro (&QCrelative_height);
22817 QCeval = intern (":eval");
22818 staticpro (&QCeval);
22819 QCpropertize = intern (":propertize");
22820 staticpro (&QCpropertize);
22821 QCfile = intern (":file");
22822 staticpro (&QCfile);
22823 Qfontified = intern ("fontified");
22824 staticpro (&Qfontified);
22825 Qfontification_functions = intern ("fontification-functions");
22826 staticpro (&Qfontification_functions);
22827 Qtrailing_whitespace = intern ("trailing-whitespace");
22828 staticpro (&Qtrailing_whitespace);
22829 Qescape_glyph = intern ("escape-glyph");
22830 staticpro (&Qescape_glyph);
22831 Qnobreak_space = intern ("nobreak-space");
22832 staticpro (&Qnobreak_space);
22833 Qimage = intern ("image");
22834 staticpro (&Qimage);
22835 QCmap = intern (":map");
22836 staticpro (&QCmap);
22837 QCpointer = intern (":pointer");
22838 staticpro (&QCpointer);
22839 Qrect = intern ("rect");
22840 staticpro (&Qrect);
22841 Qcircle = intern ("circle");
22842 staticpro (&Qcircle);
22843 Qpoly = intern ("poly");
22844 staticpro (&Qpoly);
22845 Qmessage_truncate_lines = intern ("message-truncate-lines");
22846 staticpro (&Qmessage_truncate_lines);
22847 Qgrow_only = intern ("grow-only");
22848 staticpro (&Qgrow_only);
22849 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22850 staticpro (&Qinhibit_menubar_update);
22851 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22852 staticpro (&Qinhibit_eval_during_redisplay);
22853 Qposition = intern ("position");
22854 staticpro (&Qposition);
22855 Qbuffer_position = intern ("buffer-position");
22856 staticpro (&Qbuffer_position);
22857 Qobject = intern ("object");
22858 staticpro (&Qobject);
22859 Qbar = intern ("bar");
22860 staticpro (&Qbar);
22861 Qhbar = intern ("hbar");
22862 staticpro (&Qhbar);
22863 Qbox = intern ("box");
22864 staticpro (&Qbox);
22865 Qhollow = intern ("hollow");
22866 staticpro (&Qhollow);
22867 Qhand = intern ("hand");
22868 staticpro (&Qhand);
22869 Qarrow = intern ("arrow");
22870 staticpro (&Qarrow);
22871 Qtext = intern ("text");
22872 staticpro (&Qtext);
22873 Qrisky_local_variable = intern ("risky-local-variable");
22874 staticpro (&Qrisky_local_variable);
22875 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22876 staticpro (&Qinhibit_free_realized_faces);
22877
22878 list_of_error = Fcons (Fcons (intern ("error"),
22879 Fcons (intern ("void-variable"), Qnil)),
22880 Qnil);
22881 staticpro (&list_of_error);
22882
22883 Qlast_arrow_position = intern ("last-arrow-position");
22884 staticpro (&Qlast_arrow_position);
22885 Qlast_arrow_string = intern ("last-arrow-string");
22886 staticpro (&Qlast_arrow_string);
22887
22888 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22889 staticpro (&Qoverlay_arrow_string);
22890 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22891 staticpro (&Qoverlay_arrow_bitmap);
22892
22893 echo_buffer[0] = echo_buffer[1] = Qnil;
22894 staticpro (&echo_buffer[0]);
22895 staticpro (&echo_buffer[1]);
22896
22897 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22898 staticpro (&echo_area_buffer[0]);
22899 staticpro (&echo_area_buffer[1]);
22900
22901 Vmessages_buffer_name = build_string ("*Messages*");
22902 staticpro (&Vmessages_buffer_name);
22903
22904 mode_line_proptrans_alist = Qnil;
22905 staticpro (&mode_line_proptrans_alist);
22906 mode_line_string_list = Qnil;
22907 staticpro (&mode_line_string_list);
22908 mode_line_string_face = Qnil;
22909 staticpro (&mode_line_string_face);
22910 mode_line_string_face_prop = Qnil;
22911 staticpro (&mode_line_string_face_prop);
22912 Vmode_line_unwind_vector = Qnil;
22913 staticpro (&Vmode_line_unwind_vector);
22914
22915 help_echo_string = Qnil;
22916 staticpro (&help_echo_string);
22917 help_echo_object = Qnil;
22918 staticpro (&help_echo_object);
22919 help_echo_window = Qnil;
22920 staticpro (&help_echo_window);
22921 previous_help_echo_string = Qnil;
22922 staticpro (&previous_help_echo_string);
22923 help_echo_pos = -1;
22924
22925 #ifdef HAVE_WINDOW_SYSTEM
22926 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22927 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22928 For example, if a block cursor is over a tab, it will be drawn as
22929 wide as that tab on the display. */);
22930 x_stretch_cursor_p = 0;
22931 #endif
22932
22933 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22934 doc: /* *Non-nil means highlight trailing whitespace.
22935 The face used for trailing whitespace is `trailing-whitespace'. */);
22936 Vshow_trailing_whitespace = Qnil;
22937
22938 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
22939 doc: /* *Control highlighting of nobreak space and soft hyphen.
22940 A value of t means highlight the character itself (for nobreak space,
22941 use face `nobreak-space').
22942 A value of nil means no highlighting.
22943 Other values mean display the escape glyph followed by an ordinary
22944 space or ordinary hyphen. */);
22945 Vnobreak_char_display = Qt;
22946
22947 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22948 doc: /* *The pointer shape to show in void text areas.
22949 A value of nil means to show the text pointer. Other options are `arrow',
22950 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22951 Vvoid_text_area_pointer = Qarrow;
22952
22953 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22954 doc: /* Non-nil means don't actually do any redisplay.
22955 This is used for internal purposes. */);
22956 Vinhibit_redisplay = Qnil;
22957
22958 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22959 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22960 Vglobal_mode_string = Qnil;
22961
22962 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22963 doc: /* Marker for where to display an arrow on top of the buffer text.
22964 This must be the beginning of a line in order to work.
22965 See also `overlay-arrow-string'. */);
22966 Voverlay_arrow_position = Qnil;
22967
22968 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22969 doc: /* String to display as an arrow in non-window frames.
22970 See also `overlay-arrow-position'. */);
22971 Voverlay_arrow_string = build_string ("=>");
22972
22973 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22974 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22975 The symbols on this list are examined during redisplay to determine
22976 where to display overlay arrows. */);
22977 Voverlay_arrow_variable_list
22978 = Fcons (intern ("overlay-arrow-position"), Qnil);
22979
22980 DEFVAR_INT ("scroll-step", &scroll_step,
22981 doc: /* *The number of lines to try scrolling a window by when point moves out.
22982 If that fails to bring point back on frame, point is centered instead.
22983 If this is zero, point is always centered after it moves off frame.
22984 If you want scrolling to always be a line at a time, you should set
22985 `scroll-conservatively' to a large value rather than set this to 1. */);
22986
22987 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22988 doc: /* *Scroll up to this many lines, to bring point back on screen.
22989 A value of zero means to scroll the text to center point vertically
22990 in the window. */);
22991 scroll_conservatively = 0;
22992
22993 DEFVAR_INT ("scroll-margin", &scroll_margin,
22994 doc: /* *Number of lines of margin at the top and bottom of a window.
22995 Recenter the window whenever point gets within this many lines
22996 of the top or bottom of the window. */);
22997 scroll_margin = 0;
22998
22999 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23000 doc: /* Pixels per inch on current display.
23001 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23002 Vdisplay_pixels_per_inch = make_float (72.0);
23003
23004 #if GLYPH_DEBUG
23005 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23006 #endif
23007
23008 DEFVAR_BOOL ("truncate-partial-width-windows",
23009 &truncate_partial_width_windows,
23010 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23011 truncate_partial_width_windows = 1;
23012
23013 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23014 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23015 Any other value means to use the appropriate face, `mode-line',
23016 `header-line', or `menu' respectively. */);
23017 mode_line_inverse_video = 1;
23018
23019 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23020 doc: /* *Maximum buffer size for which line number should be displayed.
23021 If the buffer is bigger than this, the line number does not appear
23022 in the mode line. A value of nil means no limit. */);
23023 Vline_number_display_limit = Qnil;
23024
23025 DEFVAR_INT ("line-number-display-limit-width",
23026 &line_number_display_limit_width,
23027 doc: /* *Maximum line width (in characters) for line number display.
23028 If the average length of the lines near point is bigger than this, then the
23029 line number may be omitted from the mode line. */);
23030 line_number_display_limit_width = 200;
23031
23032 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23033 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23034 highlight_nonselected_windows = 0;
23035
23036 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23037 doc: /* Non-nil if more than one frame is visible on this display.
23038 Minibuffer-only frames don't count, but iconified frames do.
23039 This variable is not guaranteed to be accurate except while processing
23040 `frame-title-format' and `icon-title-format'. */);
23041
23042 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23043 doc: /* Template for displaying the title bar of visible frames.
23044 \(Assuming the window manager supports this feature.)
23045 This variable has the same structure as `mode-line-format' (which see),
23046 and is used only on frames for which no explicit name has been set
23047 \(see `modify-frame-parameters'). */);
23048
23049 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23050 doc: /* Template for displaying the title bar of an iconified frame.
23051 \(Assuming the window manager supports this feature.)
23052 This variable has the same structure as `mode-line-format' (which see),
23053 and is used only on frames for which no explicit name has been set
23054 \(see `modify-frame-parameters'). */);
23055 Vicon_title_format
23056 = Vframe_title_format
23057 = Fcons (intern ("multiple-frames"),
23058 Fcons (build_string ("%b"),
23059 Fcons (Fcons (empty_string,
23060 Fcons (intern ("invocation-name"),
23061 Fcons (build_string ("@"),
23062 Fcons (intern ("system-name"),
23063 Qnil)))),
23064 Qnil)));
23065
23066 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23067 doc: /* Maximum number of lines to keep in the message log buffer.
23068 If nil, disable message logging. If t, log messages but don't truncate
23069 the buffer when it becomes large. */);
23070 Vmessage_log_max = make_number (50);
23071
23072 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23073 doc: /* Functions called before redisplay, if window sizes have changed.
23074 The value should be a list of functions that take one argument.
23075 Just before redisplay, for each frame, if any of its windows have changed
23076 size since the last redisplay, or have been split or deleted,
23077 all the functions in the list are called, with the frame as argument. */);
23078 Vwindow_size_change_functions = Qnil;
23079
23080 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23081 doc: /* List of functions to call before redisplaying a window with scrolling.
23082 Each function is called with two arguments, the window
23083 and its new display-start position. Note that the value of `window-end'
23084 is not valid when these functions are called. */);
23085 Vwindow_scroll_functions = Qnil;
23086
23087 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23088 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23089 mouse_autoselect_window = 0;
23090
23091 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23092 doc: /* *Non-nil means automatically resize tool-bars.
23093 This increases a tool-bar's height if not all tool-bar items are visible.
23094 It decreases a tool-bar's height when it would display blank lines
23095 otherwise. */);
23096 auto_resize_tool_bars_p = 1;
23097
23098 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23099 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23100 auto_raise_tool_bar_buttons_p = 1;
23101
23102 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23103 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23104 make_cursor_line_fully_visible_p = 1;
23105
23106 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23107 doc: /* *Margin around tool-bar buttons in pixels.
23108 If an integer, use that for both horizontal and vertical margins.
23109 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23110 HORZ specifying the horizontal margin, and VERT specifying the
23111 vertical margin. */);
23112 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23113
23114 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23115 doc: /* *Relief thickness of tool-bar buttons. */);
23116 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23117
23118 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23119 doc: /* List of functions to call to fontify regions of text.
23120 Each function is called with one argument POS. Functions must
23121 fontify a region starting at POS in the current buffer, and give
23122 fontified regions the property `fontified'. */);
23123 Vfontification_functions = Qnil;
23124 Fmake_variable_buffer_local (Qfontification_functions);
23125
23126 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23127 &unibyte_display_via_language_environment,
23128 doc: /* *Non-nil means display unibyte text according to language environment.
23129 Specifically this means that unibyte non-ASCII characters
23130 are displayed by converting them to the equivalent multibyte characters
23131 according to the current language environment. As a result, they are
23132 displayed according to the current fontset. */);
23133 unibyte_display_via_language_environment = 0;
23134
23135 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23136 doc: /* *Maximum height for resizing mini-windows.
23137 If a float, it specifies a fraction of the mini-window frame's height.
23138 If an integer, it specifies a number of lines. */);
23139 Vmax_mini_window_height = make_float (0.25);
23140
23141 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23142 doc: /* *How to resize mini-windows.
23143 A value of nil means don't automatically resize mini-windows.
23144 A value of t means resize them to fit the text displayed in them.
23145 A value of `grow-only', the default, means let mini-windows grow
23146 only, until their display becomes empty, at which point the windows
23147 go back to their normal size. */);
23148 Vresize_mini_windows = Qgrow_only;
23149
23150 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23151 doc: /* Alist specifying how to blink the cursor off.
23152 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23153 `cursor-type' frame-parameter or variable equals ON-STATE,
23154 comparing using `equal', Emacs uses OFF-STATE to specify
23155 how to blink it off. */);
23156 Vblink_cursor_alist = Qnil;
23157
23158 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23159 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23160 automatic_hscrolling_p = 1;
23161
23162 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23163 doc: /* *How many columns away from the window edge point is allowed to get
23164 before automatic hscrolling will horizontally scroll the window. */);
23165 hscroll_margin = 5;
23166
23167 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23168 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23169 When point is less than `automatic-hscroll-margin' columns from the window
23170 edge, automatic hscrolling will scroll the window by the amount of columns
23171 determined by this variable. If its value is a positive integer, scroll that
23172 many columns. If it's a positive floating-point number, it specifies the
23173 fraction of the window's width to scroll. If it's nil or zero, point will be
23174 centered horizontally after the scroll. Any other value, including negative
23175 numbers, are treated as if the value were zero.
23176
23177 Automatic hscrolling always moves point outside the scroll margin, so if
23178 point was more than scroll step columns inside the margin, the window will
23179 scroll more than the value given by the scroll step.
23180
23181 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23182 and `scroll-right' overrides this variable's effect. */);
23183 Vhscroll_step = make_number (0);
23184
23185 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23186 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23187 Bind this around calls to `message' to let it take effect. */);
23188 message_truncate_lines = 0;
23189
23190 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23191 doc: /* Normal hook run to update the menu bar definitions.
23192 Redisplay runs this hook before it redisplays the menu bar.
23193 This is used to update submenus such as Buffers,
23194 whose contents depend on various data. */);
23195 Vmenu_bar_update_hook = Qnil;
23196
23197 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23198 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23199 inhibit_menubar_update = 0;
23200
23201 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23202 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23203 inhibit_eval_during_redisplay = 0;
23204
23205 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23206 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23207 inhibit_free_realized_faces = 0;
23208
23209 #if GLYPH_DEBUG
23210 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23211 doc: /* Inhibit try_window_id display optimization. */);
23212 inhibit_try_window_id = 0;
23213
23214 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23215 doc: /* Inhibit try_window_reusing display optimization. */);
23216 inhibit_try_window_reusing = 0;
23217
23218 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23219 doc: /* Inhibit try_cursor_movement display optimization. */);
23220 inhibit_try_cursor_movement = 0;
23221 #endif /* GLYPH_DEBUG */
23222 }
23223
23224
23225 /* Initialize this module when Emacs starts. */
23226
23227 void
23228 init_xdisp ()
23229 {
23230 Lisp_Object root_window;
23231 struct window *mini_w;
23232
23233 current_header_line_height = current_mode_line_height = -1;
23234
23235 CHARPOS (this_line_start_pos) = 0;
23236
23237 mini_w = XWINDOW (minibuf_window);
23238 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23239
23240 if (!noninteractive)
23241 {
23242 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23243 int i;
23244
23245 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23246 set_window_height (root_window,
23247 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23248 0);
23249 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23250 set_window_height (minibuf_window, 1, 0);
23251
23252 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23253 mini_w->total_cols = make_number (FRAME_COLS (f));
23254
23255 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23256 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23257 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23258
23259 /* The default ellipsis glyphs `...'. */
23260 for (i = 0; i < 3; ++i)
23261 default_invis_vector[i] = make_number ('.');
23262 }
23263
23264 {
23265 /* Allocate the buffer for frame titles.
23266 Also used for `format-mode-line'. */
23267 int size = 100;
23268 mode_line_noprop_buf = (char *) xmalloc (size);
23269 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23270 mode_line_noprop_ptr = mode_line_noprop_buf;
23271 mode_line_target = MODE_LINE_DISPLAY;
23272 }
23273
23274 help_echo_showing_p = 0;
23275 }
23276
23277
23278 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23279 (do not change this comment) */