Merged from emacs@sv.gnu.org
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin around tool bar buttons in pixels. */
273
274 Lisp_Object Vtool_bar_button_margin;
275
276 /* Thickness of shadow to draw around tool bar buttons. */
277
278 EMACS_INT tool_bar_button_relief;
279
280 /* Non-zero means automatically resize tool-bars so that all tool-bar
281 items are visible, and no blank lines remain. */
282
283 int auto_resize_tool_bars_p;
284
285 /* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
288
289 int x_stretch_cursor_p;
290
291 /* Non-nil means don't actually do any redisplay. */
292
293 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294
295 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296
297 int inhibit_eval_during_redisplay;
298
299 /* Names of text properties relevant for redisplay. */
300
301 Lisp_Object Qdisplay;
302 extern Lisp_Object Qface, Qinvisible, Qwidth;
303
304 /* Symbols used in text property values. */
305
306 Lisp_Object Vdisplay_pixels_per_inch;
307 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
308 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
309 Lisp_Object Qslice;
310 Lisp_Object Qcenter;
311 Lisp_Object Qmargin, Qpointer;
312 Lisp_Object Qline_height;
313 extern Lisp_Object Qheight;
314 extern Lisp_Object QCwidth, QCheight, QCascent;
315 extern Lisp_Object Qscroll_bar;
316 extern Lisp_Object Qcursor;
317
318 /* Non-nil means highlight trailing whitespace. */
319
320 Lisp_Object Vshow_trailing_whitespace;
321
322 /* Non-nil means escape non-break space and hyphens. */
323
324 Lisp_Object Vnobreak_char_display;
325
326 #ifdef HAVE_WINDOW_SYSTEM
327 extern Lisp_Object Voverflow_newline_into_fringe;
328
329 /* Test if overflow newline into fringe. Called with iterator IT
330 at or past right window margin, and with IT->current_x set. */
331
332 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
337
338 #endif /* HAVE_WINDOW_SYSTEM */
339
340 /* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
343
344 Lisp_Object Vvoid_text_area_pointer;
345
346 /* Name of the face used to highlight trailing whitespace. */
347
348 Lisp_Object Qtrailing_whitespace;
349
350 /* Name and number of the face used to highlight escape glyphs. */
351
352 Lisp_Object Qescape_glyph;
353
354 /* Name and number of the face used to highlight non-breaking spaces. */
355
356 Lisp_Object Qnobreak_space;
357
358 /* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
360
361 Lisp_Object Qimage;
362
363 /* The image map types. */
364 Lisp_Object QCmap, QCpointer;
365 Lisp_Object Qrect, Qcircle, Qpoly;
366
367 /* Non-zero means print newline to stdout before next mini-buffer
368 message. */
369
370 int noninteractive_need_newline;
371
372 /* Non-zero means print newline to message log before next message. */
373
374 static int message_log_need_newline;
375
376 /* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379 static Lisp_Object message_dolog_marker1;
380 static Lisp_Object message_dolog_marker2;
381 static Lisp_Object message_dolog_marker3;
382 \f
383 /* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
387
388 static struct text_pos this_line_start_pos;
389
390 /* Number of characters past the end of the line above, including the
391 terminating newline. */
392
393 static struct text_pos this_line_end_pos;
394
395 /* The vertical positions and the height of this line. */
396
397 static int this_line_vpos;
398 static int this_line_y;
399 static int this_line_pixel_height;
400
401 /* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
403
404 static int this_line_start_x;
405
406 /* Buffer that this_line_.* variables are referring to. */
407
408 static struct buffer *this_line_buffer;
409
410 /* Nonzero means truncate lines in all windows less wide than the
411 frame. */
412
413 int truncate_partial_width_windows;
414
415 /* A flag to control how to display unibyte 8-bit character. */
416
417 int unibyte_display_via_language_environment;
418
419 /* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
422
423 int multiple_frames;
424
425 Lisp_Object Vglobal_mode_string;
426
427
428 /* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
430 where to display overlay arrows. */
431
432 Lisp_Object Voverlay_arrow_variable_list;
433
434 /* Marker for where to display an arrow on top of the buffer text. */
435
436 Lisp_Object Voverlay_arrow_position;
437
438 /* String to display for the arrow. Only used on terminal frames. */
439
440 Lisp_Object Voverlay_arrow_string;
441
442 /* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
445 numerical position. */
446
447 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448
449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
451
452 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
453
454 /* Like mode-line-format, but for the title bar on a visible frame. */
455
456 Lisp_Object Vframe_title_format;
457
458 /* Like mode-line-format, but for the title bar on an iconified frame. */
459
460 Lisp_Object Vicon_title_format;
461
462 /* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
465
466 static Lisp_Object Vwindow_size_change_functions;
467
468 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
469
470 /* Nonzero if an overlay arrow has been displayed in this window. */
471
472 static int overlay_arrow_seen;
473
474 /* Nonzero means highlight the region even in nonselected windows. */
475
476 int highlight_nonselected_windows;
477
478 /* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
480
481 static EMACS_INT scroll_step;
482
483 /* Nonzero means scroll just far enough to bring point back on the
484 screen, when appropriate. */
485
486 static EMACS_INT scroll_conservatively;
487
488 /* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
492
493 EMACS_INT scroll_margin;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
508
509 This variable is deprecated. */
510
511 int mode_line_inverse_video;
512
513 /* Prompt to display in front of the mini-buffer contents. */
514
515 Lisp_Object minibuf_prompt;
516
517 /* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
519
520 int minibuf_prompt_width;
521
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
526 Lisp_Object echo_area_window;
527
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533 Lisp_Object Vmessage_stack;
534
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
537
538 int message_enable_multibyte;
539
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541
542 int update_mode_lines;
543
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
546
547 int windows_or_buffers_changed;
548
549 /* Nonzero means a frame's cursor type has been changed. */
550
551 int cursor_type_changed;
552
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
556 int line_number_displayed;
557
558 /* Maximum buffer size for which to display line numbers. */
559
560 Lisp_Object Vline_number_display_limit;
561
562 /* Line width to consider when repositioning for line number display. */
563
564 static EMACS_INT line_number_display_limit_width;
565
566 /* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
568
569 Lisp_Object Vmessage_log_max;
570
571 /* The name of the *Messages* buffer, a string. */
572
573 static Lisp_Object Vmessages_buffer_name;
574
575 /* Current, index 0, and last displayed echo area message. Either
576 buffers from echo_buffers, or nil to indicate no message. */
577
578 Lisp_Object echo_area_buffer[2];
579
580 /* The buffers referenced from echo_area_buffer. */
581
582 static Lisp_Object echo_buffer[2];
583
584 /* A vector saved used in with_area_buffer to reduce consing. */
585
586 static Lisp_Object Vwith_echo_area_save_vector;
587
588 /* Non-zero means display_echo_area should display the last echo area
589 message again. Set by redisplay_preserve_echo_area. */
590
591 static int display_last_displayed_message_p;
592
593 /* Nonzero if echo area is being used by print; zero if being used by
594 message. */
595
596 int message_buf_print;
597
598 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
599
600 Lisp_Object Qinhibit_menubar_update;
601 int inhibit_menubar_update;
602
603 /* Maximum height for resizing mini-windows. Either a float
604 specifying a fraction of the available height, or an integer
605 specifying a number of lines. */
606
607 Lisp_Object Vmax_mini_window_height;
608
609 /* Non-zero means messages should be displayed with truncated
610 lines instead of being continued. */
611
612 int message_truncate_lines;
613 Lisp_Object Qmessage_truncate_lines;
614
615 /* Set to 1 in clear_message to make redisplay_internal aware
616 of an emptied echo area. */
617
618 static int message_cleared_p;
619
620 /* How to blink the default frame cursor off. */
621 Lisp_Object Vblink_cursor_alist;
622
623 /* A scratch glyph row with contents used for generating truncation
624 glyphs. Also used in direct_output_for_insert. */
625
626 #define MAX_SCRATCH_GLYPHS 100
627 struct glyph_row scratch_glyph_row;
628 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
629
630 /* Ascent and height of the last line processed by move_it_to. */
631
632 static int last_max_ascent, last_height;
633
634 /* Non-zero if there's a help-echo in the echo area. */
635
636 int help_echo_showing_p;
637
638 /* If >= 0, computed, exact values of mode-line and header-line height
639 to use in the macros CURRENT_MODE_LINE_HEIGHT and
640 CURRENT_HEADER_LINE_HEIGHT. */
641
642 int current_mode_line_height, current_header_line_height;
643
644 /* The maximum distance to look ahead for text properties. Values
645 that are too small let us call compute_char_face and similar
646 functions too often which is expensive. Values that are too large
647 let us call compute_char_face and alike too often because we
648 might not be interested in text properties that far away. */
649
650 #define TEXT_PROP_DISTANCE_LIMIT 100
651
652 #if GLYPH_DEBUG
653
654 /* Variables to turn off display optimizations from Lisp. */
655
656 int inhibit_try_window_id, inhibit_try_window_reusing;
657 int inhibit_try_cursor_movement;
658
659 /* Non-zero means print traces of redisplay if compiled with
660 GLYPH_DEBUG != 0. */
661
662 int trace_redisplay_p;
663
664 #endif /* GLYPH_DEBUG */
665
666 #ifdef DEBUG_TRACE_MOVE
667 /* Non-zero means trace with TRACE_MOVE to stderr. */
668 int trace_move;
669
670 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
671 #else
672 #define TRACE_MOVE(x) (void) 0
673 #endif
674
675 /* Non-zero means automatically scroll windows horizontally to make
676 point visible. */
677
678 int automatic_hscrolling_p;
679
680 /* How close to the margin can point get before the window is scrolled
681 horizontally. */
682 EMACS_INT hscroll_margin;
683
684 /* How much to scroll horizontally when point is inside the above margin. */
685 Lisp_Object Vhscroll_step;
686
687 /* The variable `resize-mini-windows'. If nil, don't resize
688 mini-windows. If t, always resize them to fit the text they
689 display. If `grow-only', let mini-windows grow only until they
690 become empty. */
691
692 Lisp_Object Vresize_mini_windows;
693
694 /* Buffer being redisplayed -- for redisplay_window_error. */
695
696 struct buffer *displayed_buffer;
697
698 /* Value returned from text property handlers (see below). */
699
700 enum prop_handled
701 {
702 HANDLED_NORMALLY,
703 HANDLED_RECOMPUTE_PROPS,
704 HANDLED_OVERLAY_STRING_CONSUMED,
705 HANDLED_RETURN
706 };
707
708 /* A description of text properties that redisplay is interested
709 in. */
710
711 struct props
712 {
713 /* The name of the property. */
714 Lisp_Object *name;
715
716 /* A unique index for the property. */
717 enum prop_idx idx;
718
719 /* A handler function called to set up iterator IT from the property
720 at IT's current position. Value is used to steer handle_stop. */
721 enum prop_handled (*handler) P_ ((struct it *it));
722 };
723
724 static enum prop_handled handle_face_prop P_ ((struct it *));
725 static enum prop_handled handle_invisible_prop P_ ((struct it *));
726 static enum prop_handled handle_display_prop P_ ((struct it *));
727 static enum prop_handled handle_composition_prop P_ ((struct it *));
728 static enum prop_handled handle_overlay_change P_ ((struct it *));
729 static enum prop_handled handle_fontified_prop P_ ((struct it *));
730
731 /* Properties handled by iterators. */
732
733 static struct props it_props[] =
734 {
735 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
736 /* Handle `face' before `display' because some sub-properties of
737 `display' need to know the face. */
738 {&Qface, FACE_PROP_IDX, handle_face_prop},
739 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
740 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
741 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
742 {NULL, 0, NULL}
743 };
744
745 /* Value is the position described by X. If X is a marker, value is
746 the marker_position of X. Otherwise, value is X. */
747
748 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
749
750 /* Enumeration returned by some move_it_.* functions internally. */
751
752 enum move_it_result
753 {
754 /* Not used. Undefined value. */
755 MOVE_UNDEFINED,
756
757 /* Move ended at the requested buffer position or ZV. */
758 MOVE_POS_MATCH_OR_ZV,
759
760 /* Move ended at the requested X pixel position. */
761 MOVE_X_REACHED,
762
763 /* Move within a line ended at the end of a line that must be
764 continued. */
765 MOVE_LINE_CONTINUED,
766
767 /* Move within a line ended at the end of a line that would
768 be displayed truncated. */
769 MOVE_LINE_TRUNCATED,
770
771 /* Move within a line ended at a line end. */
772 MOVE_NEWLINE_OR_CR
773 };
774
775 /* This counter is used to clear the face cache every once in a while
776 in redisplay_internal. It is incremented for each redisplay.
777 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
778 cleared. */
779
780 #define CLEAR_FACE_CACHE_COUNT 500
781 static int clear_face_cache_count;
782
783 /* Similarly for the image cache. */
784
785 #ifdef HAVE_WINDOW_SYSTEM
786 #define CLEAR_IMAGE_CACHE_COUNT 101
787 static int clear_image_cache_count;
788 #endif
789
790 /* Non-zero while redisplay_internal is in progress. */
791
792 int redisplaying_p;
793
794 /* Non-zero means don't free realized faces. Bound while freeing
795 realized faces is dangerous because glyph matrices might still
796 reference them. */
797
798 int inhibit_free_realized_faces;
799 Lisp_Object Qinhibit_free_realized_faces;
800
801 /* If a string, XTread_socket generates an event to display that string.
802 (The display is done in read_char.) */
803
804 Lisp_Object help_echo_string;
805 Lisp_Object help_echo_window;
806 Lisp_Object help_echo_object;
807 int help_echo_pos;
808
809 /* Temporary variable for XTread_socket. */
810
811 Lisp_Object previous_help_echo_string;
812
813 /* Null glyph slice */
814
815 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
816
817 \f
818 /* Function prototypes. */
819
820 static void setup_for_ellipsis P_ ((struct it *, int));
821 static void mark_window_display_accurate_1 P_ ((struct window *, int));
822 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
823 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
824 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
825 static int redisplay_mode_lines P_ ((Lisp_Object, int));
826 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
827
828 #if 0
829 static int invisible_text_between_p P_ ((struct it *, int, int));
830 #endif
831
832 static void pint2str P_ ((char *, int, int));
833 static void pint2hrstr P_ ((char *, int, int));
834 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
835 struct text_pos));
836 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
837 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
838 static void store_mode_line_noprop_char P_ ((char));
839 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
840 static void x_consider_frame_title P_ ((Lisp_Object));
841 static void handle_stop P_ ((struct it *));
842 static int tool_bar_lines_needed P_ ((struct frame *));
843 static int single_display_spec_intangible_p P_ ((Lisp_Object));
844 static void ensure_echo_area_buffers P_ ((void));
845 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
846 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
847 static int with_echo_area_buffer P_ ((struct window *, int,
848 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
849 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
850 static void clear_garbaged_frames P_ ((void));
851 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
853 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
854 static int display_echo_area P_ ((struct window *));
855 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
856 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
857 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
858 static int string_char_and_length P_ ((const unsigned char *, int, int *));
859 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
860 struct text_pos));
861 static int compute_window_start_on_continuation_line P_ ((struct window *));
862 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
863 static void insert_left_trunc_glyphs P_ ((struct it *));
864 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
865 Lisp_Object));
866 static void extend_face_to_end_of_line P_ ((struct it *));
867 static int append_space_for_newline P_ ((struct it *, int));
868 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
869 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
870 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
871 static int trailing_whitespace_p P_ ((int));
872 static int message_log_check_duplicate P_ ((int, int, int, int));
873 static void push_it P_ ((struct it *));
874 static void pop_it P_ ((struct it *));
875 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
876 static void select_frame_for_redisplay P_ ((Lisp_Object));
877 static void redisplay_internal P_ ((int));
878 static int echo_area_display P_ ((int));
879 static void redisplay_windows P_ ((Lisp_Object));
880 static void redisplay_window P_ ((Lisp_Object, int));
881 static Lisp_Object redisplay_window_error ();
882 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
883 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
884 static void update_menu_bar P_ ((struct frame *, int));
885 static int try_window_reusing_current_matrix P_ ((struct window *));
886 static int try_window_id P_ ((struct window *));
887 static int display_line P_ ((struct it *));
888 static int display_mode_lines P_ ((struct window *));
889 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
890 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
891 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
892 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
893 static void display_menu_bar P_ ((struct window *));
894 static int display_count_lines P_ ((int, int, int, int, int *));
895 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
896 int, int, struct it *, int, int, int, int));
897 static void compute_line_metrics P_ ((struct it *));
898 static void run_redisplay_end_trigger_hook P_ ((struct it *));
899 static int get_overlay_strings P_ ((struct it *, int));
900 static void next_overlay_string P_ ((struct it *));
901 static void reseat P_ ((struct it *, struct text_pos, int));
902 static void reseat_1 P_ ((struct it *, struct text_pos, int));
903 static void back_to_previous_visible_line_start P_ ((struct it *));
904 void reseat_at_previous_visible_line_start P_ ((struct it *));
905 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
906 static int next_element_from_ellipsis P_ ((struct it *));
907 static int next_element_from_display_vector P_ ((struct it *));
908 static int next_element_from_string P_ ((struct it *));
909 static int next_element_from_c_string P_ ((struct it *));
910 static int next_element_from_buffer P_ ((struct it *));
911 static int next_element_from_composition P_ ((struct it *));
912 static int next_element_from_image P_ ((struct it *));
913 static int next_element_from_stretch P_ ((struct it *));
914 static void load_overlay_strings P_ ((struct it *, int));
915 static int init_from_display_pos P_ ((struct it *, struct window *,
916 struct display_pos *));
917 static void reseat_to_string P_ ((struct it *, unsigned char *,
918 Lisp_Object, int, int, int, int));
919 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
920 int, int, int));
921 void move_it_vertically_backward P_ ((struct it *, int));
922 static void init_to_row_start P_ ((struct it *, struct window *,
923 struct glyph_row *));
924 static int init_to_row_end P_ ((struct it *, struct window *,
925 struct glyph_row *));
926 static void back_to_previous_line_start P_ ((struct it *));
927 static int forward_to_next_line_start P_ ((struct it *, int *));
928 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
929 Lisp_Object, int));
930 static struct text_pos string_pos P_ ((int, Lisp_Object));
931 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
932 static int number_of_chars P_ ((unsigned char *, int));
933 static void compute_stop_pos P_ ((struct it *));
934 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
935 Lisp_Object));
936 static int face_before_or_after_it_pos P_ ((struct it *, int));
937 static int next_overlay_change P_ ((int));
938 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
939 Lisp_Object, struct text_pos *,
940 int));
941 static int underlying_face_id P_ ((struct it *));
942 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
943 struct window *));
944
945 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
946 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
947
948 #ifdef HAVE_WINDOW_SYSTEM
949
950 static void update_tool_bar P_ ((struct frame *, int));
951 static void build_desired_tool_bar_string P_ ((struct frame *f));
952 static int redisplay_tool_bar P_ ((struct frame *));
953 static void display_tool_bar_line P_ ((struct it *));
954 static void notice_overwritten_cursor P_ ((struct window *,
955 enum glyph_row_area,
956 int, int, int, int));
957
958
959
960 #endif /* HAVE_WINDOW_SYSTEM */
961
962 \f
963 /***********************************************************************
964 Window display dimensions
965 ***********************************************************************/
966
967 /* Return the bottom boundary y-position for text lines in window W.
968 This is the first y position at which a line cannot start.
969 It is relative to the top of the window.
970
971 This is the height of W minus the height of a mode line, if any. */
972
973 INLINE int
974 window_text_bottom_y (w)
975 struct window *w;
976 {
977 int height = WINDOW_TOTAL_HEIGHT (w);
978
979 if (WINDOW_WANTS_MODELINE_P (w))
980 height -= CURRENT_MODE_LINE_HEIGHT (w);
981 return height;
982 }
983
984 /* Return the pixel width of display area AREA of window W. AREA < 0
985 means return the total width of W, not including fringes to
986 the left and right of the window. */
987
988 INLINE int
989 window_box_width (w, area)
990 struct window *w;
991 int area;
992 {
993 int cols = XFASTINT (w->total_cols);
994 int pixels = 0;
995
996 if (!w->pseudo_window_p)
997 {
998 cols -= WINDOW_SCROLL_BAR_COLS (w);
999
1000 if (area == TEXT_AREA)
1001 {
1002 if (INTEGERP (w->left_margin_cols))
1003 cols -= XFASTINT (w->left_margin_cols);
1004 if (INTEGERP (w->right_margin_cols))
1005 cols -= XFASTINT (w->right_margin_cols);
1006 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1007 }
1008 else if (area == LEFT_MARGIN_AREA)
1009 {
1010 cols = (INTEGERP (w->left_margin_cols)
1011 ? XFASTINT (w->left_margin_cols) : 0);
1012 pixels = 0;
1013 }
1014 else if (area == RIGHT_MARGIN_AREA)
1015 {
1016 cols = (INTEGERP (w->right_margin_cols)
1017 ? XFASTINT (w->right_margin_cols) : 0);
1018 pixels = 0;
1019 }
1020 }
1021
1022 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1023 }
1024
1025
1026 /* Return the pixel height of the display area of window W, not
1027 including mode lines of W, if any. */
1028
1029 INLINE int
1030 window_box_height (w)
1031 struct window *w;
1032 {
1033 struct frame *f = XFRAME (w->frame);
1034 int height = WINDOW_TOTAL_HEIGHT (w);
1035
1036 xassert (height >= 0);
1037
1038 /* Note: the code below that determines the mode-line/header-line
1039 height is essentially the same as that contained in the macro
1040 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1041 the appropriate glyph row has its `mode_line_p' flag set,
1042 and if it doesn't, uses estimate_mode_line_height instead. */
1043
1044 if (WINDOW_WANTS_MODELINE_P (w))
1045 {
1046 struct glyph_row *ml_row
1047 = (w->current_matrix && w->current_matrix->rows
1048 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1049 : 0);
1050 if (ml_row && ml_row->mode_line_p)
1051 height -= ml_row->height;
1052 else
1053 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1054 }
1055
1056 if (WINDOW_WANTS_HEADER_LINE_P (w))
1057 {
1058 struct glyph_row *hl_row
1059 = (w->current_matrix && w->current_matrix->rows
1060 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1061 : 0);
1062 if (hl_row && hl_row->mode_line_p)
1063 height -= hl_row->height;
1064 else
1065 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1066 }
1067
1068 /* With a very small font and a mode-line that's taller than
1069 default, we might end up with a negative height. */
1070 return max (0, height);
1071 }
1072
1073 /* Return the window-relative coordinate of the left edge of display
1074 area AREA of window W. AREA < 0 means return the left edge of the
1075 whole window, to the right of the left fringe of W. */
1076
1077 INLINE int
1078 window_box_left_offset (w, area)
1079 struct window *w;
1080 int area;
1081 {
1082 int x;
1083
1084 if (w->pseudo_window_p)
1085 return 0;
1086
1087 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1088
1089 if (area == TEXT_AREA)
1090 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1091 + window_box_width (w, LEFT_MARGIN_AREA));
1092 else if (area == RIGHT_MARGIN_AREA)
1093 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1094 + window_box_width (w, LEFT_MARGIN_AREA)
1095 + window_box_width (w, TEXT_AREA)
1096 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1097 ? 0
1098 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1099 else if (area == LEFT_MARGIN_AREA
1100 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1101 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1102
1103 return x;
1104 }
1105
1106
1107 /* Return the window-relative coordinate of the right edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the left of the right fringe of W. */
1110
1111 INLINE int
1112 window_box_right_offset (w, area)
1113 struct window *w;
1114 int area;
1115 {
1116 return window_box_left_offset (w, area) + window_box_width (w, area);
1117 }
1118
1119 /* Return the frame-relative coordinate of the left edge of display
1120 area AREA of window W. AREA < 0 means return the left edge of the
1121 whole window, to the right of the left fringe of W. */
1122
1123 INLINE int
1124 window_box_left (w, area)
1125 struct window *w;
1126 int area;
1127 {
1128 struct frame *f = XFRAME (w->frame);
1129 int x;
1130
1131 if (w->pseudo_window_p)
1132 return FRAME_INTERNAL_BORDER_WIDTH (f);
1133
1134 x = (WINDOW_LEFT_EDGE_X (w)
1135 + window_box_left_offset (w, area));
1136
1137 return x;
1138 }
1139
1140
1141 /* Return the frame-relative coordinate of the right edge of display
1142 area AREA of window W. AREA < 0 means return the left edge of the
1143 whole window, to the left of the right fringe of W. */
1144
1145 INLINE int
1146 window_box_right (w, area)
1147 struct window *w;
1148 int area;
1149 {
1150 return window_box_left (w, area) + window_box_width (w, area);
1151 }
1152
1153 /* Get the bounding box of the display area AREA of window W, without
1154 mode lines, in frame-relative coordinates. AREA < 0 means the
1155 whole window, not including the left and right fringes of
1156 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1157 coordinates of the upper-left corner of the box. Return in
1158 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1159
1160 INLINE void
1161 window_box (w, area, box_x, box_y, box_width, box_height)
1162 struct window *w;
1163 int area;
1164 int *box_x, *box_y, *box_width, *box_height;
1165 {
1166 if (box_width)
1167 *box_width = window_box_width (w, area);
1168 if (box_height)
1169 *box_height = window_box_height (w);
1170 if (box_x)
1171 *box_x = window_box_left (w, area);
1172 if (box_y)
1173 {
1174 *box_y = WINDOW_TOP_EDGE_Y (w);
1175 if (WINDOW_WANTS_HEADER_LINE_P (w))
1176 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1177 }
1178 }
1179
1180
1181 /* Get the bounding box of the display area AREA of window W, without
1182 mode lines. AREA < 0 means the whole window, not including the
1183 left and right fringe of the window. Return in *TOP_LEFT_X
1184 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1185 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1186 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1187 box. */
1188
1189 INLINE void
1190 window_box_edges (w, area, top_left_x, top_left_y,
1191 bottom_right_x, bottom_right_y)
1192 struct window *w;
1193 int area;
1194 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1195 {
1196 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1197 bottom_right_y);
1198 *bottom_right_x += *top_left_x;
1199 *bottom_right_y += *top_left_y;
1200 }
1201
1202
1203 \f
1204 /***********************************************************************
1205 Utilities
1206 ***********************************************************************/
1207
1208 /* Return the bottom y-position of the line the iterator IT is in.
1209 This can modify IT's settings. */
1210
1211 int
1212 line_bottom_y (it)
1213 struct it *it;
1214 {
1215 int line_height = it->max_ascent + it->max_descent;
1216 int line_top_y = it->current_y;
1217
1218 if (line_height == 0)
1219 {
1220 if (last_height)
1221 line_height = last_height;
1222 else if (IT_CHARPOS (*it) < ZV)
1223 {
1224 move_it_by_lines (it, 1, 1);
1225 line_height = (it->max_ascent || it->max_descent
1226 ? it->max_ascent + it->max_descent
1227 : last_height);
1228 }
1229 else
1230 {
1231 struct glyph_row *row = it->glyph_row;
1232
1233 /* Use the default character height. */
1234 it->glyph_row = NULL;
1235 it->what = IT_CHARACTER;
1236 it->c = ' ';
1237 it->len = 1;
1238 PRODUCE_GLYPHS (it);
1239 line_height = it->ascent + it->descent;
1240 it->glyph_row = row;
1241 }
1242 }
1243
1244 return line_top_y + line_height;
1245 }
1246
1247
1248 /* Return 1 if position CHARPOS is visible in window W.
1249 If visible, set *X and *Y to pixel coordinates of top left corner.
1250 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1251 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1252 and header-lines heights. */
1253
1254 int
1255 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1256 struct window *w;
1257 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1258 {
1259 struct it it;
1260 struct text_pos top;
1261 int visible_p = 0;
1262 struct buffer *old_buffer = NULL;
1263
1264 if (noninteractive)
1265 return visible_p;
1266
1267 if (XBUFFER (w->buffer) != current_buffer)
1268 {
1269 old_buffer = current_buffer;
1270 set_buffer_internal_1 (XBUFFER (w->buffer));
1271 }
1272
1273 SET_TEXT_POS_FROM_MARKER (top, w->start);
1274
1275 /* Compute exact mode line heights, if requested. */
1276 if (exact_mode_line_heights_p)
1277 {
1278 if (WINDOW_WANTS_MODELINE_P (w))
1279 current_mode_line_height
1280 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1281 current_buffer->mode_line_format);
1282
1283 if (WINDOW_WANTS_HEADER_LINE_P (w))
1284 current_header_line_height
1285 = display_mode_line (w, HEADER_LINE_FACE_ID,
1286 current_buffer->header_line_format);
1287 }
1288
1289 start_display (&it, w, top);
1290 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1291 MOVE_TO_POS | MOVE_TO_Y);
1292
1293 /* Note that we may overshoot because of invisible text. */
1294 if (IT_CHARPOS (it) >= charpos)
1295 {
1296 int top_x = it.current_x;
1297 int top_y = it.current_y;
1298 int bottom_y = (last_height = 0, line_bottom_y (&it));
1299 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1300
1301 if (top_y < window_top_y)
1302 visible_p = bottom_y > window_top_y;
1303 else if (top_y < it.last_visible_y)
1304 visible_p = 1;
1305 if (visible_p)
1306 {
1307 *x = top_x;
1308 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1309 *rtop = max (0, window_top_y - top_y);
1310 *rbot = max (0, bottom_y - it.last_visible_y);
1311 }
1312 }
1313 else
1314 {
1315 struct it it2;
1316
1317 it2 = it;
1318 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1319 move_it_by_lines (&it, 1, 0);
1320 if (charpos < IT_CHARPOS (it))
1321 {
1322 visible_p = 1;
1323 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1324 *x = it2.current_x;
1325 *y = it2.current_y + it2.max_ascent - it2.ascent;
1326 *rtop = max (0, -it2.current_y);
1327 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1328 - it.last_visible_y));
1329 }
1330 }
1331
1332 if (old_buffer)
1333 set_buffer_internal_1 (old_buffer);
1334
1335 current_header_line_height = current_mode_line_height = -1;
1336
1337 if (visible_p && XFASTINT (w->hscroll) > 0)
1338 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1339
1340 return visible_p;
1341 }
1342
1343
1344 /* Return the next character from STR which is MAXLEN bytes long.
1345 Return in *LEN the length of the character. This is like
1346 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1347 we find one, we return a `?', but with the length of the invalid
1348 character. */
1349
1350 static INLINE int
1351 string_char_and_length (str, maxlen, len)
1352 const unsigned char *str;
1353 int maxlen, *len;
1354 {
1355 int c;
1356
1357 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1358 if (!CHAR_VALID_P (c, 1))
1359 /* We may not change the length here because other places in Emacs
1360 don't use this function, i.e. they silently accept invalid
1361 characters. */
1362 c = '?';
1363
1364 return c;
1365 }
1366
1367
1368
1369 /* Given a position POS containing a valid character and byte position
1370 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1371
1372 static struct text_pos
1373 string_pos_nchars_ahead (pos, string, nchars)
1374 struct text_pos pos;
1375 Lisp_Object string;
1376 int nchars;
1377 {
1378 xassert (STRINGP (string) && nchars >= 0);
1379
1380 if (STRING_MULTIBYTE (string))
1381 {
1382 int rest = SBYTES (string) - BYTEPOS (pos);
1383 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1384 int len;
1385
1386 while (nchars--)
1387 {
1388 string_char_and_length (p, rest, &len);
1389 p += len, rest -= len;
1390 xassert (rest >= 0);
1391 CHARPOS (pos) += 1;
1392 BYTEPOS (pos) += len;
1393 }
1394 }
1395 else
1396 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1397
1398 return pos;
1399 }
1400
1401
1402 /* Value is the text position, i.e. character and byte position,
1403 for character position CHARPOS in STRING. */
1404
1405 static INLINE struct text_pos
1406 string_pos (charpos, string)
1407 int charpos;
1408 Lisp_Object string;
1409 {
1410 struct text_pos pos;
1411 xassert (STRINGP (string));
1412 xassert (charpos >= 0);
1413 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1414 return pos;
1415 }
1416
1417
1418 /* Value is a text position, i.e. character and byte position, for
1419 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1420 means recognize multibyte characters. */
1421
1422 static struct text_pos
1423 c_string_pos (charpos, s, multibyte_p)
1424 int charpos;
1425 unsigned char *s;
1426 int multibyte_p;
1427 {
1428 struct text_pos pos;
1429
1430 xassert (s != NULL);
1431 xassert (charpos >= 0);
1432
1433 if (multibyte_p)
1434 {
1435 int rest = strlen (s), len;
1436
1437 SET_TEXT_POS (pos, 0, 0);
1438 while (charpos--)
1439 {
1440 string_char_and_length (s, rest, &len);
1441 s += len, rest -= len;
1442 xassert (rest >= 0);
1443 CHARPOS (pos) += 1;
1444 BYTEPOS (pos) += len;
1445 }
1446 }
1447 else
1448 SET_TEXT_POS (pos, charpos, charpos);
1449
1450 return pos;
1451 }
1452
1453
1454 /* Value is the number of characters in C string S. MULTIBYTE_P
1455 non-zero means recognize multibyte characters. */
1456
1457 static int
1458 number_of_chars (s, multibyte_p)
1459 unsigned char *s;
1460 int multibyte_p;
1461 {
1462 int nchars;
1463
1464 if (multibyte_p)
1465 {
1466 int rest = strlen (s), len;
1467 unsigned char *p = (unsigned char *) s;
1468
1469 for (nchars = 0; rest > 0; ++nchars)
1470 {
1471 string_char_and_length (p, rest, &len);
1472 rest -= len, p += len;
1473 }
1474 }
1475 else
1476 nchars = strlen (s);
1477
1478 return nchars;
1479 }
1480
1481
1482 /* Compute byte position NEWPOS->bytepos corresponding to
1483 NEWPOS->charpos. POS is a known position in string STRING.
1484 NEWPOS->charpos must be >= POS.charpos. */
1485
1486 static void
1487 compute_string_pos (newpos, pos, string)
1488 struct text_pos *newpos, pos;
1489 Lisp_Object string;
1490 {
1491 xassert (STRINGP (string));
1492 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1493
1494 if (STRING_MULTIBYTE (string))
1495 *newpos = string_pos_nchars_ahead (pos, string,
1496 CHARPOS (*newpos) - CHARPOS (pos));
1497 else
1498 BYTEPOS (*newpos) = CHARPOS (*newpos);
1499 }
1500
1501 /* EXPORT:
1502 Return an estimation of the pixel height of mode or top lines on
1503 frame F. FACE_ID specifies what line's height to estimate. */
1504
1505 int
1506 estimate_mode_line_height (f, face_id)
1507 struct frame *f;
1508 enum face_id face_id;
1509 {
1510 #ifdef HAVE_WINDOW_SYSTEM
1511 if (FRAME_WINDOW_P (f))
1512 {
1513 int height = FONT_HEIGHT (FRAME_FONT (f));
1514
1515 /* This function is called so early when Emacs starts that the face
1516 cache and mode line face are not yet initialized. */
1517 if (FRAME_FACE_CACHE (f))
1518 {
1519 struct face *face = FACE_FROM_ID (f, face_id);
1520 if (face)
1521 {
1522 if (face->font)
1523 height = FONT_HEIGHT (face->font);
1524 if (face->box_line_width > 0)
1525 height += 2 * face->box_line_width;
1526 }
1527 }
1528
1529 return height;
1530 }
1531 #endif
1532
1533 return 1;
1534 }
1535
1536 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1537 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1538 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1539 not force the value into range. */
1540
1541 void
1542 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1543 FRAME_PTR f;
1544 register int pix_x, pix_y;
1545 int *x, *y;
1546 NativeRectangle *bounds;
1547 int noclip;
1548 {
1549
1550 #ifdef HAVE_WINDOW_SYSTEM
1551 if (FRAME_WINDOW_P (f))
1552 {
1553 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1554 even for negative values. */
1555 if (pix_x < 0)
1556 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1557 if (pix_y < 0)
1558 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1559
1560 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1561 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1562
1563 if (bounds)
1564 STORE_NATIVE_RECT (*bounds,
1565 FRAME_COL_TO_PIXEL_X (f, pix_x),
1566 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1567 FRAME_COLUMN_WIDTH (f) - 1,
1568 FRAME_LINE_HEIGHT (f) - 1);
1569
1570 if (!noclip)
1571 {
1572 if (pix_x < 0)
1573 pix_x = 0;
1574 else if (pix_x > FRAME_TOTAL_COLS (f))
1575 pix_x = FRAME_TOTAL_COLS (f);
1576
1577 if (pix_y < 0)
1578 pix_y = 0;
1579 else if (pix_y > FRAME_LINES (f))
1580 pix_y = FRAME_LINES (f);
1581 }
1582 }
1583 #endif
1584
1585 *x = pix_x;
1586 *y = pix_y;
1587 }
1588
1589
1590 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1591 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1592 can't tell the positions because W's display is not up to date,
1593 return 0. */
1594
1595 int
1596 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1597 struct window *w;
1598 int hpos, vpos;
1599 int *frame_x, *frame_y;
1600 {
1601 #ifdef HAVE_WINDOW_SYSTEM
1602 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1603 {
1604 int success_p;
1605
1606 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1607 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1608
1609 if (display_completed)
1610 {
1611 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1612 struct glyph *glyph = row->glyphs[TEXT_AREA];
1613 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1614
1615 hpos = row->x;
1616 vpos = row->y;
1617 while (glyph < end)
1618 {
1619 hpos += glyph->pixel_width;
1620 ++glyph;
1621 }
1622
1623 /* If first glyph is partially visible, its first visible position is still 0. */
1624 if (hpos < 0)
1625 hpos = 0;
1626
1627 success_p = 1;
1628 }
1629 else
1630 {
1631 hpos = vpos = 0;
1632 success_p = 0;
1633 }
1634
1635 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1636 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1637 return success_p;
1638 }
1639 #endif
1640
1641 *frame_x = hpos;
1642 *frame_y = vpos;
1643 return 1;
1644 }
1645
1646
1647 #ifdef HAVE_WINDOW_SYSTEM
1648
1649 /* Find the glyph under window-relative coordinates X/Y in window W.
1650 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1651 strings. Return in *HPOS and *VPOS the row and column number of
1652 the glyph found. Return in *AREA the glyph area containing X.
1653 Value is a pointer to the glyph found or null if X/Y is not on
1654 text, or we can't tell because W's current matrix is not up to
1655 date. */
1656
1657 static struct glyph *
1658 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1659 struct window *w;
1660 int x, y;
1661 int *hpos, *vpos, *dx, *dy, *area;
1662 {
1663 struct glyph *glyph, *end;
1664 struct glyph_row *row = NULL;
1665 int x0, i;
1666
1667 /* Find row containing Y. Give up if some row is not enabled. */
1668 for (i = 0; i < w->current_matrix->nrows; ++i)
1669 {
1670 row = MATRIX_ROW (w->current_matrix, i);
1671 if (!row->enabled_p)
1672 return NULL;
1673 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1674 break;
1675 }
1676
1677 *vpos = i;
1678 *hpos = 0;
1679
1680 /* Give up if Y is not in the window. */
1681 if (i == w->current_matrix->nrows)
1682 return NULL;
1683
1684 /* Get the glyph area containing X. */
1685 if (w->pseudo_window_p)
1686 {
1687 *area = TEXT_AREA;
1688 x0 = 0;
1689 }
1690 else
1691 {
1692 if (x < window_box_left_offset (w, TEXT_AREA))
1693 {
1694 *area = LEFT_MARGIN_AREA;
1695 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1696 }
1697 else if (x < window_box_right_offset (w, TEXT_AREA))
1698 {
1699 *area = TEXT_AREA;
1700 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1701 }
1702 else
1703 {
1704 *area = RIGHT_MARGIN_AREA;
1705 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1706 }
1707 }
1708
1709 /* Find glyph containing X. */
1710 glyph = row->glyphs[*area];
1711 end = glyph + row->used[*area];
1712 x -= x0;
1713 while (glyph < end && x >= glyph->pixel_width)
1714 {
1715 x -= glyph->pixel_width;
1716 ++glyph;
1717 }
1718
1719 if (glyph == end)
1720 return NULL;
1721
1722 if (dx)
1723 {
1724 *dx = x;
1725 *dy = y - (row->y + row->ascent - glyph->ascent);
1726 }
1727
1728 *hpos = glyph - row->glyphs[*area];
1729 return glyph;
1730 }
1731
1732
1733 /* EXPORT:
1734 Convert frame-relative x/y to coordinates relative to window W.
1735 Takes pseudo-windows into account. */
1736
1737 void
1738 frame_to_window_pixel_xy (w, x, y)
1739 struct window *w;
1740 int *x, *y;
1741 {
1742 if (w->pseudo_window_p)
1743 {
1744 /* A pseudo-window is always full-width, and starts at the
1745 left edge of the frame, plus a frame border. */
1746 struct frame *f = XFRAME (w->frame);
1747 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1748 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1749 }
1750 else
1751 {
1752 *x -= WINDOW_LEFT_EDGE_X (w);
1753 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1754 }
1755 }
1756
1757 /* EXPORT:
1758 Return in RECTS[] at most N clipping rectangles for glyph string S.
1759 Return the number of stored rectangles. */
1760
1761 int
1762 get_glyph_string_clip_rects (s, rects, n)
1763 struct glyph_string *s;
1764 NativeRectangle *rects;
1765 int n;
1766 {
1767 XRectangle r;
1768
1769 if (n <= 0)
1770 return 0;
1771
1772 if (s->row->full_width_p)
1773 {
1774 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1775 r.x = WINDOW_LEFT_EDGE_X (s->w);
1776 r.width = WINDOW_TOTAL_WIDTH (s->w);
1777
1778 /* Unless displaying a mode or menu bar line, which are always
1779 fully visible, clip to the visible part of the row. */
1780 if (s->w->pseudo_window_p)
1781 r.height = s->row->visible_height;
1782 else
1783 r.height = s->height;
1784 }
1785 else
1786 {
1787 /* This is a text line that may be partially visible. */
1788 r.x = window_box_left (s->w, s->area);
1789 r.width = window_box_width (s->w, s->area);
1790 r.height = s->row->visible_height;
1791 }
1792
1793 if (s->clip_head)
1794 if (r.x < s->clip_head->x)
1795 {
1796 if (r.width >= s->clip_head->x - r.x)
1797 r.width -= s->clip_head->x - r.x;
1798 else
1799 r.width = 0;
1800 r.x = s->clip_head->x;
1801 }
1802 if (s->clip_tail)
1803 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1804 {
1805 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1806 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1807 else
1808 r.width = 0;
1809 }
1810
1811 /* If S draws overlapping rows, it's sufficient to use the top and
1812 bottom of the window for clipping because this glyph string
1813 intentionally draws over other lines. */
1814 if (s->for_overlaps)
1815 {
1816 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1817 r.height = window_text_bottom_y (s->w) - r.y;
1818
1819 /* Alas, the above simple strategy does not work for the
1820 environments with anti-aliased text: if the same text is
1821 drawn onto the same place multiple times, it gets thicker.
1822 If the overlap we are processing is for the erased cursor, we
1823 take the intersection with the rectagle of the cursor. */
1824 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1825 {
1826 XRectangle rc, r_save = r;
1827
1828 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1829 rc.y = s->w->phys_cursor.y;
1830 rc.width = s->w->phys_cursor_width;
1831 rc.height = s->w->phys_cursor_height;
1832
1833 x_intersect_rectangles (&r_save, &rc, &r);
1834 }
1835 }
1836 else
1837 {
1838 /* Don't use S->y for clipping because it doesn't take partially
1839 visible lines into account. For example, it can be negative for
1840 partially visible lines at the top of a window. */
1841 if (!s->row->full_width_p
1842 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1843 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1844 else
1845 r.y = max (0, s->row->y);
1846
1847 /* If drawing a tool-bar window, draw it over the internal border
1848 at the top of the window. */
1849 if (WINDOWP (s->f->tool_bar_window)
1850 && s->w == XWINDOW (s->f->tool_bar_window))
1851 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1852 }
1853
1854 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1855
1856 /* If drawing the cursor, don't let glyph draw outside its
1857 advertised boundaries. Cleartype does this under some circumstances. */
1858 if (s->hl == DRAW_CURSOR)
1859 {
1860 struct glyph *glyph = s->first_glyph;
1861 int height, max_y;
1862
1863 if (s->x > r.x)
1864 {
1865 r.width -= s->x - r.x;
1866 r.x = s->x;
1867 }
1868 r.width = min (r.width, glyph->pixel_width);
1869
1870 /* If r.y is below window bottom, ensure that we still see a cursor. */
1871 height = min (glyph->ascent + glyph->descent,
1872 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1873 max_y = window_text_bottom_y (s->w) - height;
1874 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1875 if (s->ybase - glyph->ascent > max_y)
1876 {
1877 r.y = max_y;
1878 r.height = height;
1879 }
1880 else
1881 {
1882 /* Don't draw cursor glyph taller than our actual glyph. */
1883 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1884 if (height < r.height)
1885 {
1886 max_y = r.y + r.height;
1887 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1888 r.height = min (max_y - r.y, height);
1889 }
1890 }
1891 }
1892
1893 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1894 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1895 {
1896 #ifdef CONVERT_FROM_XRECT
1897 CONVERT_FROM_XRECT (r, *rects);
1898 #else
1899 *rects = r;
1900 #endif
1901 return 1;
1902 }
1903 else
1904 {
1905 /* If we are processing overlapping and allowed to return
1906 multiple clipping rectangles, we exclude the row of the glyph
1907 string from the clipping rectangle. This is to avoid drawing
1908 the same text on the environment with anti-aliasing. */
1909 #ifdef CONVERT_FROM_XRECT
1910 XRectangle rs[2];
1911 #else
1912 XRectangle *rs = rects;
1913 #endif
1914 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1915
1916 if (s->for_overlaps & OVERLAPS_PRED)
1917 {
1918 rs[i] = r;
1919 if (r.y + r.height > row_y)
1920 {
1921 if (r.y < row_y)
1922 rs[i].height = row_y - r.y;
1923 else
1924 rs[i].height = 0;
1925 }
1926 i++;
1927 }
1928 if (s->for_overlaps & OVERLAPS_SUCC)
1929 {
1930 rs[i] = r;
1931 if (r.y < row_y + s->row->visible_height)
1932 {
1933 if (r.y + r.height > row_y + s->row->visible_height)
1934 {
1935 rs[i].y = row_y + s->row->visible_height;
1936 rs[i].height = r.y + r.height - rs[i].y;
1937 }
1938 else
1939 rs[i].height = 0;
1940 }
1941 i++;
1942 }
1943
1944 n = i;
1945 #ifdef CONVERT_FROM_XRECT
1946 for (i = 0; i < n; i++)
1947 CONVERT_FROM_XRECT (rs[i], rects[i]);
1948 #endif
1949 return n;
1950 }
1951 }
1952
1953 /* EXPORT:
1954 Return in *NR the clipping rectangle for glyph string S. */
1955
1956 void
1957 get_glyph_string_clip_rect (s, nr)
1958 struct glyph_string *s;
1959 NativeRectangle *nr;
1960 {
1961 get_glyph_string_clip_rects (s, nr, 1);
1962 }
1963
1964
1965 /* EXPORT:
1966 Return the position and height of the phys cursor in window W.
1967 Set w->phys_cursor_width to width of phys cursor.
1968 */
1969
1970 int
1971 get_phys_cursor_geometry (w, row, glyph, heightp)
1972 struct window *w;
1973 struct glyph_row *row;
1974 struct glyph *glyph;
1975 int *heightp;
1976 {
1977 struct frame *f = XFRAME (WINDOW_FRAME (w));
1978 int y, wd, h, h0, y0;
1979
1980 /* Compute the width of the rectangle to draw. If on a stretch
1981 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1982 rectangle as wide as the glyph, but use a canonical character
1983 width instead. */
1984 wd = glyph->pixel_width - 1;
1985 #ifdef HAVE_NTGUI
1986 wd++; /* Why? */
1987 #endif
1988 if (glyph->type == STRETCH_GLYPH
1989 && !x_stretch_cursor_p)
1990 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1991 w->phys_cursor_width = wd;
1992
1993 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1994
1995 /* If y is below window bottom, ensure that we still see a cursor. */
1996 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1997
1998 h = max (h0, glyph->ascent + glyph->descent);
1999 h0 = min (h0, glyph->ascent + glyph->descent);
2000
2001 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2002 if (y < y0)
2003 {
2004 h = max (h - (y0 - y) + 1, h0);
2005 y = y0 - 1;
2006 }
2007 else
2008 {
2009 y0 = window_text_bottom_y (w) - h0;
2010 if (y > y0)
2011 {
2012 h += y - y0;
2013 y = y0;
2014 }
2015 }
2016
2017 *heightp = h - 1;
2018 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2019 }
2020
2021 /*
2022 * Remember which glyph the mouse is over.
2023 */
2024
2025 void
2026 remember_mouse_glyph (f, gx, gy, rect)
2027 struct frame *f;
2028 int gx, gy;
2029 NativeRectangle *rect;
2030 {
2031 Lisp_Object window;
2032 struct window *w;
2033 struct glyph_row *r, *gr, *end_row;
2034 enum window_part part;
2035 enum glyph_row_area area;
2036 int x, y, width, height;
2037
2038 /* Try to determine frame pixel position and size of the glyph under
2039 frame pixel coordinates X/Y on frame F. */
2040
2041 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2042 if (NILP (window))
2043 {
2044 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2045 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2046 goto virtual_glyph;
2047 }
2048
2049 w = XWINDOW (window);
2050 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2051 height = WINDOW_FRAME_LINE_HEIGHT (w);
2052
2053 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2054 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2055
2056 if (w->pseudo_window_p)
2057 {
2058 area = TEXT_AREA;
2059 part = ON_MODE_LINE; /* Don't adjust margin. */
2060 goto text_glyph;
2061 }
2062
2063 switch (part)
2064 {
2065 case ON_LEFT_MARGIN:
2066 area = LEFT_MARGIN_AREA;
2067 goto text_glyph;
2068
2069 case ON_RIGHT_MARGIN:
2070 area = RIGHT_MARGIN_AREA;
2071 goto text_glyph;
2072
2073 case ON_HEADER_LINE:
2074 case ON_MODE_LINE:
2075 gr = (part == ON_HEADER_LINE
2076 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2077 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2078 gy = gr->y;
2079 area = TEXT_AREA;
2080 goto text_glyph_row_found;
2081
2082 case ON_TEXT:
2083 area = TEXT_AREA;
2084
2085 text_glyph:
2086 gr = 0; gy = 0;
2087 for (; r <= end_row && r->enabled_p; ++r)
2088 if (r->y + r->height > y)
2089 {
2090 gr = r; gy = r->y;
2091 break;
2092 }
2093
2094 text_glyph_row_found:
2095 if (gr && gy <= y)
2096 {
2097 struct glyph *g = gr->glyphs[area];
2098 struct glyph *end = g + gr->used[area];
2099
2100 height = gr->height;
2101 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2102 if (gx + g->pixel_width > x)
2103 break;
2104
2105 if (g < end)
2106 {
2107 if (g->type == IMAGE_GLYPH)
2108 {
2109 /* Don't remember when mouse is over image, as
2110 image may have hot-spots. */
2111 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2112 return;
2113 }
2114 width = g->pixel_width;
2115 }
2116 else
2117 {
2118 /* Use nominal char spacing at end of line. */
2119 x -= gx;
2120 gx += (x / width) * width;
2121 }
2122
2123 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2124 gx += window_box_left_offset (w, area);
2125 }
2126 else
2127 {
2128 /* Use nominal line height at end of window. */
2129 gx = (x / width) * width;
2130 y -= gy;
2131 gy += (y / height) * height;
2132 }
2133 break;
2134
2135 case ON_LEFT_FRINGE:
2136 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2137 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2138 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2139 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2140 goto row_glyph;
2141
2142 case ON_RIGHT_FRINGE:
2143 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2144 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2145 : window_box_right_offset (w, TEXT_AREA));
2146 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2147 goto row_glyph;
2148
2149 case ON_SCROLL_BAR:
2150 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2151 ? 0
2152 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2153 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2154 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2155 : 0)));
2156 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2157
2158 row_glyph:
2159 gr = 0, gy = 0;
2160 for (; r <= end_row && r->enabled_p; ++r)
2161 if (r->y + r->height > y)
2162 {
2163 gr = r; gy = r->y;
2164 break;
2165 }
2166
2167 if (gr && gy <= y)
2168 height = gr->height;
2169 else
2170 {
2171 /* Use nominal line height at end of window. */
2172 y -= gy;
2173 gy += (y / height) * height;
2174 }
2175 break;
2176
2177 default:
2178 ;
2179 virtual_glyph:
2180 /* If there is no glyph under the mouse, then we divide the screen
2181 into a grid of the smallest glyph in the frame, and use that
2182 as our "glyph". */
2183
2184 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2185 round down even for negative values. */
2186 if (gx < 0)
2187 gx -= width - 1;
2188 if (gy < 0)
2189 gy -= height - 1;
2190
2191 gx = (gx / width) * width;
2192 gy = (gy / height) * height;
2193
2194 goto store_rect;
2195 }
2196
2197 gx += WINDOW_LEFT_EDGE_X (w);
2198 gy += WINDOW_TOP_EDGE_Y (w);
2199
2200 store_rect:
2201 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2202
2203 /* Visible feedback for debugging. */
2204 #if 0
2205 #if HAVE_X_WINDOWS
2206 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2207 f->output_data.x->normal_gc,
2208 gx, gy, width, height);
2209 #endif
2210 #endif
2211 }
2212
2213
2214 #endif /* HAVE_WINDOW_SYSTEM */
2215
2216 \f
2217 /***********************************************************************
2218 Lisp form evaluation
2219 ***********************************************************************/
2220
2221 /* Error handler for safe_eval and safe_call. */
2222
2223 static Lisp_Object
2224 safe_eval_handler (arg)
2225 Lisp_Object arg;
2226 {
2227 add_to_log ("Error during redisplay: %s", arg, Qnil);
2228 return Qnil;
2229 }
2230
2231
2232 /* Evaluate SEXPR and return the result, or nil if something went
2233 wrong. Prevent redisplay during the evaluation. */
2234
2235 Lisp_Object
2236 safe_eval (sexpr)
2237 Lisp_Object sexpr;
2238 {
2239 Lisp_Object val;
2240
2241 if (inhibit_eval_during_redisplay)
2242 val = Qnil;
2243 else
2244 {
2245 int count = SPECPDL_INDEX ();
2246 struct gcpro gcpro1;
2247
2248 GCPRO1 (sexpr);
2249 specbind (Qinhibit_redisplay, Qt);
2250 /* Use Qt to ensure debugger does not run,
2251 so there is no possibility of wanting to redisplay. */
2252 val = internal_condition_case_1 (Feval, sexpr, Qt,
2253 safe_eval_handler);
2254 UNGCPRO;
2255 val = unbind_to (count, val);
2256 }
2257
2258 return val;
2259 }
2260
2261
2262 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2263 Return the result, or nil if something went wrong. Prevent
2264 redisplay during the evaluation. */
2265
2266 Lisp_Object
2267 safe_call (nargs, args)
2268 int nargs;
2269 Lisp_Object *args;
2270 {
2271 Lisp_Object val;
2272
2273 if (inhibit_eval_during_redisplay)
2274 val = Qnil;
2275 else
2276 {
2277 int count = SPECPDL_INDEX ();
2278 struct gcpro gcpro1;
2279
2280 GCPRO1 (args[0]);
2281 gcpro1.nvars = nargs;
2282 specbind (Qinhibit_redisplay, Qt);
2283 /* Use Qt to ensure debugger does not run,
2284 so there is no possibility of wanting to redisplay. */
2285 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2286 safe_eval_handler);
2287 UNGCPRO;
2288 val = unbind_to (count, val);
2289 }
2290
2291 return val;
2292 }
2293
2294
2295 /* Call function FN with one argument ARG.
2296 Return the result, or nil if something went wrong. */
2297
2298 Lisp_Object
2299 safe_call1 (fn, arg)
2300 Lisp_Object fn, arg;
2301 {
2302 Lisp_Object args[2];
2303 args[0] = fn;
2304 args[1] = arg;
2305 return safe_call (2, args);
2306 }
2307
2308
2309 \f
2310 /***********************************************************************
2311 Debugging
2312 ***********************************************************************/
2313
2314 #if 0
2315
2316 /* Define CHECK_IT to perform sanity checks on iterators.
2317 This is for debugging. It is too slow to do unconditionally. */
2318
2319 static void
2320 check_it (it)
2321 struct it *it;
2322 {
2323 if (it->method == GET_FROM_STRING)
2324 {
2325 xassert (STRINGP (it->string));
2326 xassert (IT_STRING_CHARPOS (*it) >= 0);
2327 }
2328 else
2329 {
2330 xassert (IT_STRING_CHARPOS (*it) < 0);
2331 if (it->method == GET_FROM_BUFFER)
2332 {
2333 /* Check that character and byte positions agree. */
2334 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2335 }
2336 }
2337
2338 if (it->dpvec)
2339 xassert (it->current.dpvec_index >= 0);
2340 else
2341 xassert (it->current.dpvec_index < 0);
2342 }
2343
2344 #define CHECK_IT(IT) check_it ((IT))
2345
2346 #else /* not 0 */
2347
2348 #define CHECK_IT(IT) (void) 0
2349
2350 #endif /* not 0 */
2351
2352
2353 #if GLYPH_DEBUG
2354
2355 /* Check that the window end of window W is what we expect it
2356 to be---the last row in the current matrix displaying text. */
2357
2358 static void
2359 check_window_end (w)
2360 struct window *w;
2361 {
2362 if (!MINI_WINDOW_P (w)
2363 && !NILP (w->window_end_valid))
2364 {
2365 struct glyph_row *row;
2366 xassert ((row = MATRIX_ROW (w->current_matrix,
2367 XFASTINT (w->window_end_vpos)),
2368 !row->enabled_p
2369 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2370 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2371 }
2372 }
2373
2374 #define CHECK_WINDOW_END(W) check_window_end ((W))
2375
2376 #else /* not GLYPH_DEBUG */
2377
2378 #define CHECK_WINDOW_END(W) (void) 0
2379
2380 #endif /* not GLYPH_DEBUG */
2381
2382
2383 \f
2384 /***********************************************************************
2385 Iterator initialization
2386 ***********************************************************************/
2387
2388 /* Initialize IT for displaying current_buffer in window W, starting
2389 at character position CHARPOS. CHARPOS < 0 means that no buffer
2390 position is specified which is useful when the iterator is assigned
2391 a position later. BYTEPOS is the byte position corresponding to
2392 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2393
2394 If ROW is not null, calls to produce_glyphs with IT as parameter
2395 will produce glyphs in that row.
2396
2397 BASE_FACE_ID is the id of a base face to use. It must be one of
2398 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2399 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2400 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2401
2402 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2403 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2404 will be initialized to use the corresponding mode line glyph row of
2405 the desired matrix of W. */
2406
2407 void
2408 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2409 struct it *it;
2410 struct window *w;
2411 int charpos, bytepos;
2412 struct glyph_row *row;
2413 enum face_id base_face_id;
2414 {
2415 int highlight_region_p;
2416
2417 /* Some precondition checks. */
2418 xassert (w != NULL && it != NULL);
2419 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2420 && charpos <= ZV));
2421
2422 /* If face attributes have been changed since the last redisplay,
2423 free realized faces now because they depend on face definitions
2424 that might have changed. Don't free faces while there might be
2425 desired matrices pending which reference these faces. */
2426 if (face_change_count && !inhibit_free_realized_faces)
2427 {
2428 face_change_count = 0;
2429 free_all_realized_faces (Qnil);
2430 }
2431
2432 /* Use one of the mode line rows of W's desired matrix if
2433 appropriate. */
2434 if (row == NULL)
2435 {
2436 if (base_face_id == MODE_LINE_FACE_ID
2437 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2438 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2439 else if (base_face_id == HEADER_LINE_FACE_ID)
2440 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2441 }
2442
2443 /* Clear IT. */
2444 bzero (it, sizeof *it);
2445 it->current.overlay_string_index = -1;
2446 it->current.dpvec_index = -1;
2447 it->base_face_id = base_face_id;
2448 it->string = Qnil;
2449 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2450
2451 /* The window in which we iterate over current_buffer: */
2452 XSETWINDOW (it->window, w);
2453 it->w = w;
2454 it->f = XFRAME (w->frame);
2455
2456 /* Extra space between lines (on window systems only). */
2457 if (base_face_id == DEFAULT_FACE_ID
2458 && FRAME_WINDOW_P (it->f))
2459 {
2460 if (NATNUMP (current_buffer->extra_line_spacing))
2461 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2462 else if (FLOATP (current_buffer->extra_line_spacing))
2463 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2464 * FRAME_LINE_HEIGHT (it->f));
2465 else if (it->f->extra_line_spacing > 0)
2466 it->extra_line_spacing = it->f->extra_line_spacing;
2467 it->max_extra_line_spacing = 0;
2468 }
2469
2470 /* If realized faces have been removed, e.g. because of face
2471 attribute changes of named faces, recompute them. When running
2472 in batch mode, the face cache of the initial frame is null. If
2473 we happen to get called, make a dummy face cache. */
2474 if (FRAME_FACE_CACHE (it->f) == NULL)
2475 init_frame_faces (it->f);
2476 if (FRAME_FACE_CACHE (it->f)->used == 0)
2477 recompute_basic_faces (it->f);
2478
2479 /* Current value of the `slice', `space-width', and 'height' properties. */
2480 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2481 it->space_width = Qnil;
2482 it->font_height = Qnil;
2483 it->override_ascent = -1;
2484
2485 /* Are control characters displayed as `^C'? */
2486 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2487
2488 /* -1 means everything between a CR and the following line end
2489 is invisible. >0 means lines indented more than this value are
2490 invisible. */
2491 it->selective = (INTEGERP (current_buffer->selective_display)
2492 ? XFASTINT (current_buffer->selective_display)
2493 : (!NILP (current_buffer->selective_display)
2494 ? -1 : 0));
2495 it->selective_display_ellipsis_p
2496 = !NILP (current_buffer->selective_display_ellipses);
2497
2498 /* Display table to use. */
2499 it->dp = window_display_table (w);
2500
2501 /* Are multibyte characters enabled in current_buffer? */
2502 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2503
2504 /* Non-zero if we should highlight the region. */
2505 highlight_region_p
2506 = (!NILP (Vtransient_mark_mode)
2507 && !NILP (current_buffer->mark_active)
2508 && XMARKER (current_buffer->mark)->buffer != 0);
2509
2510 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2511 start and end of a visible region in window IT->w. Set both to
2512 -1 to indicate no region. */
2513 if (highlight_region_p
2514 /* Maybe highlight only in selected window. */
2515 && (/* Either show region everywhere. */
2516 highlight_nonselected_windows
2517 /* Or show region in the selected window. */
2518 || w == XWINDOW (selected_window)
2519 /* Or show the region if we are in the mini-buffer and W is
2520 the window the mini-buffer refers to. */
2521 || (MINI_WINDOW_P (XWINDOW (selected_window))
2522 && WINDOWP (minibuf_selected_window)
2523 && w == XWINDOW (minibuf_selected_window))))
2524 {
2525 int charpos = marker_position (current_buffer->mark);
2526 it->region_beg_charpos = min (PT, charpos);
2527 it->region_end_charpos = max (PT, charpos);
2528 }
2529 else
2530 it->region_beg_charpos = it->region_end_charpos = -1;
2531
2532 /* Get the position at which the redisplay_end_trigger hook should
2533 be run, if it is to be run at all. */
2534 if (MARKERP (w->redisplay_end_trigger)
2535 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2536 it->redisplay_end_trigger_charpos
2537 = marker_position (w->redisplay_end_trigger);
2538 else if (INTEGERP (w->redisplay_end_trigger))
2539 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2540
2541 /* Correct bogus values of tab_width. */
2542 it->tab_width = XINT (current_buffer->tab_width);
2543 if (it->tab_width <= 0 || it->tab_width > 1000)
2544 it->tab_width = 8;
2545
2546 /* Are lines in the display truncated? */
2547 it->truncate_lines_p
2548 = (base_face_id != DEFAULT_FACE_ID
2549 || XINT (it->w->hscroll)
2550 || (truncate_partial_width_windows
2551 && !WINDOW_FULL_WIDTH_P (it->w))
2552 || !NILP (current_buffer->truncate_lines));
2553
2554 /* Get dimensions of truncation and continuation glyphs. These are
2555 displayed as fringe bitmaps under X, so we don't need them for such
2556 frames. */
2557 if (!FRAME_WINDOW_P (it->f))
2558 {
2559 if (it->truncate_lines_p)
2560 {
2561 /* We will need the truncation glyph. */
2562 xassert (it->glyph_row == NULL);
2563 produce_special_glyphs (it, IT_TRUNCATION);
2564 it->truncation_pixel_width = it->pixel_width;
2565 }
2566 else
2567 {
2568 /* We will need the continuation glyph. */
2569 xassert (it->glyph_row == NULL);
2570 produce_special_glyphs (it, IT_CONTINUATION);
2571 it->continuation_pixel_width = it->pixel_width;
2572 }
2573
2574 /* Reset these values to zero because the produce_special_glyphs
2575 above has changed them. */
2576 it->pixel_width = it->ascent = it->descent = 0;
2577 it->phys_ascent = it->phys_descent = 0;
2578 }
2579
2580 /* Set this after getting the dimensions of truncation and
2581 continuation glyphs, so that we don't produce glyphs when calling
2582 produce_special_glyphs, above. */
2583 it->glyph_row = row;
2584 it->area = TEXT_AREA;
2585
2586 /* Get the dimensions of the display area. The display area
2587 consists of the visible window area plus a horizontally scrolled
2588 part to the left of the window. All x-values are relative to the
2589 start of this total display area. */
2590 if (base_face_id != DEFAULT_FACE_ID)
2591 {
2592 /* Mode lines, menu bar in terminal frames. */
2593 it->first_visible_x = 0;
2594 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2595 }
2596 else
2597 {
2598 it->first_visible_x
2599 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2600 it->last_visible_x = (it->first_visible_x
2601 + window_box_width (w, TEXT_AREA));
2602
2603 /* If we truncate lines, leave room for the truncator glyph(s) at
2604 the right margin. Otherwise, leave room for the continuation
2605 glyph(s). Truncation and continuation glyphs are not inserted
2606 for window-based redisplay. */
2607 if (!FRAME_WINDOW_P (it->f))
2608 {
2609 if (it->truncate_lines_p)
2610 it->last_visible_x -= it->truncation_pixel_width;
2611 else
2612 it->last_visible_x -= it->continuation_pixel_width;
2613 }
2614
2615 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2616 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2617 }
2618
2619 /* Leave room for a border glyph. */
2620 if (!FRAME_WINDOW_P (it->f)
2621 && !WINDOW_RIGHTMOST_P (it->w))
2622 it->last_visible_x -= 1;
2623
2624 it->last_visible_y = window_text_bottom_y (w);
2625
2626 /* For mode lines and alike, arrange for the first glyph having a
2627 left box line if the face specifies a box. */
2628 if (base_face_id != DEFAULT_FACE_ID)
2629 {
2630 struct face *face;
2631
2632 it->face_id = base_face_id;
2633
2634 /* If we have a boxed mode line, make the first character appear
2635 with a left box line. */
2636 face = FACE_FROM_ID (it->f, base_face_id);
2637 if (face->box != FACE_NO_BOX)
2638 it->start_of_box_run_p = 1;
2639 }
2640
2641 /* If a buffer position was specified, set the iterator there,
2642 getting overlays and face properties from that position. */
2643 if (charpos >= BUF_BEG (current_buffer))
2644 {
2645 it->end_charpos = ZV;
2646 it->face_id = -1;
2647 IT_CHARPOS (*it) = charpos;
2648
2649 /* Compute byte position if not specified. */
2650 if (bytepos < charpos)
2651 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2652 else
2653 IT_BYTEPOS (*it) = bytepos;
2654
2655 it->start = it->current;
2656
2657 /* Compute faces etc. */
2658 reseat (it, it->current.pos, 1);
2659 }
2660
2661 CHECK_IT (it);
2662 }
2663
2664
2665 /* Initialize IT for the display of window W with window start POS. */
2666
2667 void
2668 start_display (it, w, pos)
2669 struct it *it;
2670 struct window *w;
2671 struct text_pos pos;
2672 {
2673 struct glyph_row *row;
2674 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2675
2676 row = w->desired_matrix->rows + first_vpos;
2677 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2678 it->first_vpos = first_vpos;
2679
2680 /* Don't reseat to previous visible line start if current start
2681 position is in a string or image. */
2682 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2683 {
2684 int start_at_line_beg_p;
2685 int first_y = it->current_y;
2686
2687 /* If window start is not at a line start, skip forward to POS to
2688 get the correct continuation lines width. */
2689 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2690 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2691 if (!start_at_line_beg_p)
2692 {
2693 int new_x;
2694
2695 reseat_at_previous_visible_line_start (it);
2696 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2697
2698 new_x = it->current_x + it->pixel_width;
2699
2700 /* If lines are continued, this line may end in the middle
2701 of a multi-glyph character (e.g. a control character
2702 displayed as \003, or in the middle of an overlay
2703 string). In this case move_it_to above will not have
2704 taken us to the start of the continuation line but to the
2705 end of the continued line. */
2706 if (it->current_x > 0
2707 && !it->truncate_lines_p /* Lines are continued. */
2708 && (/* And glyph doesn't fit on the line. */
2709 new_x > it->last_visible_x
2710 /* Or it fits exactly and we're on a window
2711 system frame. */
2712 || (new_x == it->last_visible_x
2713 && FRAME_WINDOW_P (it->f))))
2714 {
2715 if (it->current.dpvec_index >= 0
2716 || it->current.overlay_string_index >= 0)
2717 {
2718 set_iterator_to_next (it, 1);
2719 move_it_in_display_line_to (it, -1, -1, 0);
2720 }
2721
2722 it->continuation_lines_width += it->current_x;
2723 }
2724
2725 /* We're starting a new display line, not affected by the
2726 height of the continued line, so clear the appropriate
2727 fields in the iterator structure. */
2728 it->max_ascent = it->max_descent = 0;
2729 it->max_phys_ascent = it->max_phys_descent = 0;
2730
2731 it->current_y = first_y;
2732 it->vpos = 0;
2733 it->current_x = it->hpos = 0;
2734 }
2735 }
2736
2737 #if 0 /* Don't assert the following because start_display is sometimes
2738 called intentionally with a window start that is not at a
2739 line start. Please leave this code in as a comment. */
2740
2741 /* Window start should be on a line start, now. */
2742 xassert (it->continuation_lines_width
2743 || IT_CHARPOS (it) == BEGV
2744 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2745 #endif /* 0 */
2746 }
2747
2748
2749 /* Return 1 if POS is a position in ellipses displayed for invisible
2750 text. W is the window we display, for text property lookup. */
2751
2752 static int
2753 in_ellipses_for_invisible_text_p (pos, w)
2754 struct display_pos *pos;
2755 struct window *w;
2756 {
2757 Lisp_Object prop, window;
2758 int ellipses_p = 0;
2759 int charpos = CHARPOS (pos->pos);
2760
2761 /* If POS specifies a position in a display vector, this might
2762 be for an ellipsis displayed for invisible text. We won't
2763 get the iterator set up for delivering that ellipsis unless
2764 we make sure that it gets aware of the invisible text. */
2765 if (pos->dpvec_index >= 0
2766 && pos->overlay_string_index < 0
2767 && CHARPOS (pos->string_pos) < 0
2768 && charpos > BEGV
2769 && (XSETWINDOW (window, w),
2770 prop = Fget_char_property (make_number (charpos),
2771 Qinvisible, window),
2772 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2773 {
2774 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2775 window);
2776 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2777 }
2778
2779 return ellipses_p;
2780 }
2781
2782
2783 /* Initialize IT for stepping through current_buffer in window W,
2784 starting at position POS that includes overlay string and display
2785 vector/ control character translation position information. Value
2786 is zero if there are overlay strings with newlines at POS. */
2787
2788 static int
2789 init_from_display_pos (it, w, pos)
2790 struct it *it;
2791 struct window *w;
2792 struct display_pos *pos;
2793 {
2794 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2795 int i, overlay_strings_with_newlines = 0;
2796
2797 /* If POS specifies a position in a display vector, this might
2798 be for an ellipsis displayed for invisible text. We won't
2799 get the iterator set up for delivering that ellipsis unless
2800 we make sure that it gets aware of the invisible text. */
2801 if (in_ellipses_for_invisible_text_p (pos, w))
2802 {
2803 --charpos;
2804 bytepos = 0;
2805 }
2806
2807 /* Keep in mind: the call to reseat in init_iterator skips invisible
2808 text, so we might end up at a position different from POS. This
2809 is only a problem when POS is a row start after a newline and an
2810 overlay starts there with an after-string, and the overlay has an
2811 invisible property. Since we don't skip invisible text in
2812 display_line and elsewhere immediately after consuming the
2813 newline before the row start, such a POS will not be in a string,
2814 but the call to init_iterator below will move us to the
2815 after-string. */
2816 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2817
2818 /* This only scans the current chunk -- it should scan all chunks.
2819 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2820 to 16 in 22.1 to make this a lesser problem. */
2821 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2822 {
2823 const char *s = SDATA (it->overlay_strings[i]);
2824 const char *e = s + SBYTES (it->overlay_strings[i]);
2825
2826 while (s < e && *s != '\n')
2827 ++s;
2828
2829 if (s < e)
2830 {
2831 overlay_strings_with_newlines = 1;
2832 break;
2833 }
2834 }
2835
2836 /* If position is within an overlay string, set up IT to the right
2837 overlay string. */
2838 if (pos->overlay_string_index >= 0)
2839 {
2840 int relative_index;
2841
2842 /* If the first overlay string happens to have a `display'
2843 property for an image, the iterator will be set up for that
2844 image, and we have to undo that setup first before we can
2845 correct the overlay string index. */
2846 if (it->method == GET_FROM_IMAGE)
2847 pop_it (it);
2848
2849 /* We already have the first chunk of overlay strings in
2850 IT->overlay_strings. Load more until the one for
2851 pos->overlay_string_index is in IT->overlay_strings. */
2852 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2853 {
2854 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2855 it->current.overlay_string_index = 0;
2856 while (n--)
2857 {
2858 load_overlay_strings (it, 0);
2859 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2860 }
2861 }
2862
2863 it->current.overlay_string_index = pos->overlay_string_index;
2864 relative_index = (it->current.overlay_string_index
2865 % OVERLAY_STRING_CHUNK_SIZE);
2866 it->string = it->overlay_strings[relative_index];
2867 xassert (STRINGP (it->string));
2868 it->current.string_pos = pos->string_pos;
2869 it->method = GET_FROM_STRING;
2870 }
2871
2872 #if 0 /* This is bogus because POS not having an overlay string
2873 position does not mean it's after the string. Example: A
2874 line starting with a before-string and initialization of IT
2875 to the previous row's end position. */
2876 else if (it->current.overlay_string_index >= 0)
2877 {
2878 /* If POS says we're already after an overlay string ending at
2879 POS, make sure to pop the iterator because it will be in
2880 front of that overlay string. When POS is ZV, we've thereby
2881 also ``processed'' overlay strings at ZV. */
2882 while (it->sp)
2883 pop_it (it);
2884 it->current.overlay_string_index = -1;
2885 it->method = GET_FROM_BUFFER;
2886 if (CHARPOS (pos->pos) == ZV)
2887 it->overlay_strings_at_end_processed_p = 1;
2888 }
2889 #endif /* 0 */
2890
2891 if (CHARPOS (pos->string_pos) >= 0)
2892 {
2893 /* Recorded position is not in an overlay string, but in another
2894 string. This can only be a string from a `display' property.
2895 IT should already be filled with that string. */
2896 it->current.string_pos = pos->string_pos;
2897 xassert (STRINGP (it->string));
2898 }
2899
2900 /* Restore position in display vector translations, control
2901 character translations or ellipses. */
2902 if (pos->dpvec_index >= 0)
2903 {
2904 if (it->dpvec == NULL)
2905 get_next_display_element (it);
2906 xassert (it->dpvec && it->current.dpvec_index == 0);
2907 it->current.dpvec_index = pos->dpvec_index;
2908 }
2909
2910 CHECK_IT (it);
2911 return !overlay_strings_with_newlines;
2912 }
2913
2914
2915 /* Initialize IT for stepping through current_buffer in window W
2916 starting at ROW->start. */
2917
2918 static void
2919 init_to_row_start (it, w, row)
2920 struct it *it;
2921 struct window *w;
2922 struct glyph_row *row;
2923 {
2924 init_from_display_pos (it, w, &row->start);
2925 it->start = row->start;
2926 it->continuation_lines_width = row->continuation_lines_width;
2927 CHECK_IT (it);
2928 }
2929
2930
2931 /* Initialize IT for stepping through current_buffer in window W
2932 starting in the line following ROW, i.e. starting at ROW->end.
2933 Value is zero if there are overlay strings with newlines at ROW's
2934 end position. */
2935
2936 static int
2937 init_to_row_end (it, w, row)
2938 struct it *it;
2939 struct window *w;
2940 struct glyph_row *row;
2941 {
2942 int success = 0;
2943
2944 if (init_from_display_pos (it, w, &row->end))
2945 {
2946 if (row->continued_p)
2947 it->continuation_lines_width
2948 = row->continuation_lines_width + row->pixel_width;
2949 CHECK_IT (it);
2950 success = 1;
2951 }
2952
2953 return success;
2954 }
2955
2956
2957
2958 \f
2959 /***********************************************************************
2960 Text properties
2961 ***********************************************************************/
2962
2963 /* Called when IT reaches IT->stop_charpos. Handle text property and
2964 overlay changes. Set IT->stop_charpos to the next position where
2965 to stop. */
2966
2967 static void
2968 handle_stop (it)
2969 struct it *it;
2970 {
2971 enum prop_handled handled;
2972 int handle_overlay_change_p;
2973 struct props *p;
2974
2975 it->dpvec = NULL;
2976 it->current.dpvec_index = -1;
2977 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2978 it->ignore_overlay_strings_at_pos_p = 0;
2979
2980 /* Use face of preceding text for ellipsis (if invisible) */
2981 if (it->selective_display_ellipsis_p)
2982 it->saved_face_id = it->face_id;
2983
2984 do
2985 {
2986 handled = HANDLED_NORMALLY;
2987
2988 /* Call text property handlers. */
2989 for (p = it_props; p->handler; ++p)
2990 {
2991 handled = p->handler (it);
2992
2993 if (handled == HANDLED_RECOMPUTE_PROPS)
2994 break;
2995 else if (handled == HANDLED_RETURN)
2996 return;
2997 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2998 handle_overlay_change_p = 0;
2999 }
3000
3001 if (handled != HANDLED_RECOMPUTE_PROPS)
3002 {
3003 /* Don't check for overlay strings below when set to deliver
3004 characters from a display vector. */
3005 if (it->method == GET_FROM_DISPLAY_VECTOR)
3006 handle_overlay_change_p = 0;
3007
3008 /* Handle overlay changes. */
3009 if (handle_overlay_change_p)
3010 handled = handle_overlay_change (it);
3011
3012 /* Determine where to stop next. */
3013 if (handled == HANDLED_NORMALLY)
3014 compute_stop_pos (it);
3015 }
3016 }
3017 while (handled == HANDLED_RECOMPUTE_PROPS);
3018 }
3019
3020
3021 /* Compute IT->stop_charpos from text property and overlay change
3022 information for IT's current position. */
3023
3024 static void
3025 compute_stop_pos (it)
3026 struct it *it;
3027 {
3028 register INTERVAL iv, next_iv;
3029 Lisp_Object object, limit, position;
3030
3031 /* If nowhere else, stop at the end. */
3032 it->stop_charpos = it->end_charpos;
3033
3034 if (STRINGP (it->string))
3035 {
3036 /* Strings are usually short, so don't limit the search for
3037 properties. */
3038 object = it->string;
3039 limit = Qnil;
3040 position = make_number (IT_STRING_CHARPOS (*it));
3041 }
3042 else
3043 {
3044 int charpos;
3045
3046 /* If next overlay change is in front of the current stop pos
3047 (which is IT->end_charpos), stop there. Note: value of
3048 next_overlay_change is point-max if no overlay change
3049 follows. */
3050 charpos = next_overlay_change (IT_CHARPOS (*it));
3051 if (charpos < it->stop_charpos)
3052 it->stop_charpos = charpos;
3053
3054 /* If showing the region, we have to stop at the region
3055 start or end because the face might change there. */
3056 if (it->region_beg_charpos > 0)
3057 {
3058 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3059 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3060 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3061 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3062 }
3063
3064 /* Set up variables for computing the stop position from text
3065 property changes. */
3066 XSETBUFFER (object, current_buffer);
3067 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3068 position = make_number (IT_CHARPOS (*it));
3069
3070 }
3071
3072 /* Get the interval containing IT's position. Value is a null
3073 interval if there isn't such an interval. */
3074 iv = validate_interval_range (object, &position, &position, 0);
3075 if (!NULL_INTERVAL_P (iv))
3076 {
3077 Lisp_Object values_here[LAST_PROP_IDX];
3078 struct props *p;
3079
3080 /* Get properties here. */
3081 for (p = it_props; p->handler; ++p)
3082 values_here[p->idx] = textget (iv->plist, *p->name);
3083
3084 /* Look for an interval following iv that has different
3085 properties. */
3086 for (next_iv = next_interval (iv);
3087 (!NULL_INTERVAL_P (next_iv)
3088 && (NILP (limit)
3089 || XFASTINT (limit) > next_iv->position));
3090 next_iv = next_interval (next_iv))
3091 {
3092 for (p = it_props; p->handler; ++p)
3093 {
3094 Lisp_Object new_value;
3095
3096 new_value = textget (next_iv->plist, *p->name);
3097 if (!EQ (values_here[p->idx], new_value))
3098 break;
3099 }
3100
3101 if (p->handler)
3102 break;
3103 }
3104
3105 if (!NULL_INTERVAL_P (next_iv))
3106 {
3107 if (INTEGERP (limit)
3108 && next_iv->position >= XFASTINT (limit))
3109 /* No text property change up to limit. */
3110 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3111 else
3112 /* Text properties change in next_iv. */
3113 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3114 }
3115 }
3116
3117 xassert (STRINGP (it->string)
3118 || (it->stop_charpos >= BEGV
3119 && it->stop_charpos >= IT_CHARPOS (*it)));
3120 }
3121
3122
3123 /* Return the position of the next overlay change after POS in
3124 current_buffer. Value is point-max if no overlay change
3125 follows. This is like `next-overlay-change' but doesn't use
3126 xmalloc. */
3127
3128 static int
3129 next_overlay_change (pos)
3130 int pos;
3131 {
3132 int noverlays;
3133 int endpos;
3134 Lisp_Object *overlays;
3135 int i;
3136
3137 /* Get all overlays at the given position. */
3138 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3139
3140 /* If any of these overlays ends before endpos,
3141 use its ending point instead. */
3142 for (i = 0; i < noverlays; ++i)
3143 {
3144 Lisp_Object oend;
3145 int oendpos;
3146
3147 oend = OVERLAY_END (overlays[i]);
3148 oendpos = OVERLAY_POSITION (oend);
3149 endpos = min (endpos, oendpos);
3150 }
3151
3152 return endpos;
3153 }
3154
3155
3156 \f
3157 /***********************************************************************
3158 Fontification
3159 ***********************************************************************/
3160
3161 /* Handle changes in the `fontified' property of the current buffer by
3162 calling hook functions from Qfontification_functions to fontify
3163 regions of text. */
3164
3165 static enum prop_handled
3166 handle_fontified_prop (it)
3167 struct it *it;
3168 {
3169 Lisp_Object prop, pos;
3170 enum prop_handled handled = HANDLED_NORMALLY;
3171
3172 if (!NILP (Vmemory_full))
3173 return handled;
3174
3175 /* Get the value of the `fontified' property at IT's current buffer
3176 position. (The `fontified' property doesn't have a special
3177 meaning in strings.) If the value is nil, call functions from
3178 Qfontification_functions. */
3179 if (!STRINGP (it->string)
3180 && it->s == NULL
3181 && !NILP (Vfontification_functions)
3182 && !NILP (Vrun_hooks)
3183 && (pos = make_number (IT_CHARPOS (*it)),
3184 prop = Fget_char_property (pos, Qfontified, Qnil),
3185 NILP (prop)))
3186 {
3187 int count = SPECPDL_INDEX ();
3188 Lisp_Object val;
3189
3190 val = Vfontification_functions;
3191 specbind (Qfontification_functions, Qnil);
3192
3193 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3194 safe_call1 (val, pos);
3195 else
3196 {
3197 Lisp_Object globals, fn;
3198 struct gcpro gcpro1, gcpro2;
3199
3200 globals = Qnil;
3201 GCPRO2 (val, globals);
3202
3203 for (; CONSP (val); val = XCDR (val))
3204 {
3205 fn = XCAR (val);
3206
3207 if (EQ (fn, Qt))
3208 {
3209 /* A value of t indicates this hook has a local
3210 binding; it means to run the global binding too.
3211 In a global value, t should not occur. If it
3212 does, we must ignore it to avoid an endless
3213 loop. */
3214 for (globals = Fdefault_value (Qfontification_functions);
3215 CONSP (globals);
3216 globals = XCDR (globals))
3217 {
3218 fn = XCAR (globals);
3219 if (!EQ (fn, Qt))
3220 safe_call1 (fn, pos);
3221 }
3222 }
3223 else
3224 safe_call1 (fn, pos);
3225 }
3226
3227 UNGCPRO;
3228 }
3229
3230 unbind_to (count, Qnil);
3231
3232 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3233 something. This avoids an endless loop if they failed to
3234 fontify the text for which reason ever. */
3235 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3236 handled = HANDLED_RECOMPUTE_PROPS;
3237 }
3238
3239 return handled;
3240 }
3241
3242
3243 \f
3244 /***********************************************************************
3245 Faces
3246 ***********************************************************************/
3247
3248 /* Set up iterator IT from face properties at its current position.
3249 Called from handle_stop. */
3250
3251 static enum prop_handled
3252 handle_face_prop (it)
3253 struct it *it;
3254 {
3255 int new_face_id, next_stop;
3256
3257 if (!STRINGP (it->string))
3258 {
3259 new_face_id
3260 = face_at_buffer_position (it->w,
3261 IT_CHARPOS (*it),
3262 it->region_beg_charpos,
3263 it->region_end_charpos,
3264 &next_stop,
3265 (IT_CHARPOS (*it)
3266 + TEXT_PROP_DISTANCE_LIMIT),
3267 0);
3268
3269 /* Is this a start of a run of characters with box face?
3270 Caveat: this can be called for a freshly initialized
3271 iterator; face_id is -1 in this case. We know that the new
3272 face will not change until limit, i.e. if the new face has a
3273 box, all characters up to limit will have one. But, as
3274 usual, we don't know whether limit is really the end. */
3275 if (new_face_id != it->face_id)
3276 {
3277 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3278
3279 /* If new face has a box but old face has not, this is
3280 the start of a run of characters with box, i.e. it has
3281 a shadow on the left side. The value of face_id of the
3282 iterator will be -1 if this is the initial call that gets
3283 the face. In this case, we have to look in front of IT's
3284 position and see whether there is a face != new_face_id. */
3285 it->start_of_box_run_p
3286 = (new_face->box != FACE_NO_BOX
3287 && (it->face_id >= 0
3288 || IT_CHARPOS (*it) == BEG
3289 || new_face_id != face_before_it_pos (it)));
3290 it->face_box_p = new_face->box != FACE_NO_BOX;
3291 }
3292 }
3293 else
3294 {
3295 int base_face_id, bufpos;
3296
3297 if (it->current.overlay_string_index >= 0)
3298 bufpos = IT_CHARPOS (*it);
3299 else
3300 bufpos = 0;
3301
3302 /* For strings from a buffer, i.e. overlay strings or strings
3303 from a `display' property, use the face at IT's current
3304 buffer position as the base face to merge with, so that
3305 overlay strings appear in the same face as surrounding
3306 text, unless they specify their own faces. */
3307 base_face_id = underlying_face_id (it);
3308
3309 new_face_id = face_at_string_position (it->w,
3310 it->string,
3311 IT_STRING_CHARPOS (*it),
3312 bufpos,
3313 it->region_beg_charpos,
3314 it->region_end_charpos,
3315 &next_stop,
3316 base_face_id, 0);
3317
3318 #if 0 /* This shouldn't be neccessary. Let's check it. */
3319 /* If IT is used to display a mode line we would really like to
3320 use the mode line face instead of the frame's default face. */
3321 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3322 && new_face_id == DEFAULT_FACE_ID)
3323 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3324 #endif
3325
3326 /* Is this a start of a run of characters with box? Caveat:
3327 this can be called for a freshly allocated iterator; face_id
3328 is -1 is this case. We know that the new face will not
3329 change until the next check pos, i.e. if the new face has a
3330 box, all characters up to that position will have a
3331 box. But, as usual, we don't know whether that position
3332 is really the end. */
3333 if (new_face_id != it->face_id)
3334 {
3335 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3336 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3337
3338 /* If new face has a box but old face hasn't, this is the
3339 start of a run of characters with box, i.e. it has a
3340 shadow on the left side. */
3341 it->start_of_box_run_p
3342 = new_face->box && (old_face == NULL || !old_face->box);
3343 it->face_box_p = new_face->box != FACE_NO_BOX;
3344 }
3345 }
3346
3347 it->face_id = new_face_id;
3348 return HANDLED_NORMALLY;
3349 }
3350
3351
3352 /* Return the ID of the face ``underlying'' IT's current position,
3353 which is in a string. If the iterator is associated with a
3354 buffer, return the face at IT's current buffer position.
3355 Otherwise, use the iterator's base_face_id. */
3356
3357 static int
3358 underlying_face_id (it)
3359 struct it *it;
3360 {
3361 int face_id = it->base_face_id, i;
3362
3363 xassert (STRINGP (it->string));
3364
3365 for (i = it->sp - 1; i >= 0; --i)
3366 if (NILP (it->stack[i].string))
3367 face_id = it->stack[i].face_id;
3368
3369 return face_id;
3370 }
3371
3372
3373 /* Compute the face one character before or after the current position
3374 of IT. BEFORE_P non-zero means get the face in front of IT's
3375 position. Value is the id of the face. */
3376
3377 static int
3378 face_before_or_after_it_pos (it, before_p)
3379 struct it *it;
3380 int before_p;
3381 {
3382 int face_id, limit;
3383 int next_check_charpos;
3384 struct text_pos pos;
3385
3386 xassert (it->s == NULL);
3387
3388 if (STRINGP (it->string))
3389 {
3390 int bufpos, base_face_id;
3391
3392 /* No face change past the end of the string (for the case
3393 we are padding with spaces). No face change before the
3394 string start. */
3395 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3396 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3397 return it->face_id;
3398
3399 /* Set pos to the position before or after IT's current position. */
3400 if (before_p)
3401 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3402 else
3403 /* For composition, we must check the character after the
3404 composition. */
3405 pos = (it->what == IT_COMPOSITION
3406 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3407 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3408
3409 if (it->current.overlay_string_index >= 0)
3410 bufpos = IT_CHARPOS (*it);
3411 else
3412 bufpos = 0;
3413
3414 base_face_id = underlying_face_id (it);
3415
3416 /* Get the face for ASCII, or unibyte. */
3417 face_id = face_at_string_position (it->w,
3418 it->string,
3419 CHARPOS (pos),
3420 bufpos,
3421 it->region_beg_charpos,
3422 it->region_end_charpos,
3423 &next_check_charpos,
3424 base_face_id, 0);
3425
3426 /* Correct the face for charsets different from ASCII. Do it
3427 for the multibyte case only. The face returned above is
3428 suitable for unibyte text if IT->string is unibyte. */
3429 if (STRING_MULTIBYTE (it->string))
3430 {
3431 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3432 int rest = SBYTES (it->string) - BYTEPOS (pos);
3433 int c, len;
3434 struct face *face = FACE_FROM_ID (it->f, face_id);
3435
3436 c = string_char_and_length (p, rest, &len);
3437 face_id = FACE_FOR_CHAR (it->f, face, c);
3438 }
3439 }
3440 else
3441 {
3442 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3443 || (IT_CHARPOS (*it) <= BEGV && before_p))
3444 return it->face_id;
3445
3446 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3447 pos = it->current.pos;
3448
3449 if (before_p)
3450 DEC_TEXT_POS (pos, it->multibyte_p);
3451 else
3452 {
3453 if (it->what == IT_COMPOSITION)
3454 /* For composition, we must check the position after the
3455 composition. */
3456 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3457 else
3458 INC_TEXT_POS (pos, it->multibyte_p);
3459 }
3460
3461 /* Determine face for CHARSET_ASCII, or unibyte. */
3462 face_id = face_at_buffer_position (it->w,
3463 CHARPOS (pos),
3464 it->region_beg_charpos,
3465 it->region_end_charpos,
3466 &next_check_charpos,
3467 limit, 0);
3468
3469 /* Correct the face for charsets different from ASCII. Do it
3470 for the multibyte case only. The face returned above is
3471 suitable for unibyte text if current_buffer is unibyte. */
3472 if (it->multibyte_p)
3473 {
3474 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3475 struct face *face = FACE_FROM_ID (it->f, face_id);
3476 face_id = FACE_FOR_CHAR (it->f, face, c);
3477 }
3478 }
3479
3480 return face_id;
3481 }
3482
3483
3484 \f
3485 /***********************************************************************
3486 Invisible text
3487 ***********************************************************************/
3488
3489 /* Set up iterator IT from invisible properties at its current
3490 position. Called from handle_stop. */
3491
3492 static enum prop_handled
3493 handle_invisible_prop (it)
3494 struct it *it;
3495 {
3496 enum prop_handled handled = HANDLED_NORMALLY;
3497
3498 if (STRINGP (it->string))
3499 {
3500 extern Lisp_Object Qinvisible;
3501 Lisp_Object prop, end_charpos, limit, charpos;
3502
3503 /* Get the value of the invisible text property at the
3504 current position. Value will be nil if there is no such
3505 property. */
3506 charpos = make_number (IT_STRING_CHARPOS (*it));
3507 prop = Fget_text_property (charpos, Qinvisible, it->string);
3508
3509 if (!NILP (prop)
3510 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3511 {
3512 handled = HANDLED_RECOMPUTE_PROPS;
3513
3514 /* Get the position at which the next change of the
3515 invisible text property can be found in IT->string.
3516 Value will be nil if the property value is the same for
3517 all the rest of IT->string. */
3518 XSETINT (limit, SCHARS (it->string));
3519 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3520 it->string, limit);
3521
3522 /* Text at current position is invisible. The next
3523 change in the property is at position end_charpos.
3524 Move IT's current position to that position. */
3525 if (INTEGERP (end_charpos)
3526 && XFASTINT (end_charpos) < XFASTINT (limit))
3527 {
3528 struct text_pos old;
3529 old = it->current.string_pos;
3530 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3531 compute_string_pos (&it->current.string_pos, old, it->string);
3532 }
3533 else
3534 {
3535 /* The rest of the string is invisible. If this is an
3536 overlay string, proceed with the next overlay string
3537 or whatever comes and return a character from there. */
3538 if (it->current.overlay_string_index >= 0)
3539 {
3540 next_overlay_string (it);
3541 /* Don't check for overlay strings when we just
3542 finished processing them. */
3543 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3544 }
3545 else
3546 {
3547 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3548 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3549 }
3550 }
3551 }
3552 }
3553 else
3554 {
3555 int invis_p, newpos, next_stop, start_charpos;
3556 Lisp_Object pos, prop, overlay;
3557
3558 /* First of all, is there invisible text at this position? */
3559 start_charpos = IT_CHARPOS (*it);
3560 pos = make_number (IT_CHARPOS (*it));
3561 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3562 &overlay);
3563 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3564
3565 /* If we are on invisible text, skip over it. */
3566 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3567 {
3568 /* Record whether we have to display an ellipsis for the
3569 invisible text. */
3570 int display_ellipsis_p = invis_p == 2;
3571
3572 handled = HANDLED_RECOMPUTE_PROPS;
3573
3574 /* Loop skipping over invisible text. The loop is left at
3575 ZV or with IT on the first char being visible again. */
3576 do
3577 {
3578 /* Try to skip some invisible text. Return value is the
3579 position reached which can be equal to IT's position
3580 if there is nothing invisible here. This skips both
3581 over invisible text properties and overlays with
3582 invisible property. */
3583 newpos = skip_invisible (IT_CHARPOS (*it),
3584 &next_stop, ZV, it->window);
3585
3586 /* If we skipped nothing at all we weren't at invisible
3587 text in the first place. If everything to the end of
3588 the buffer was skipped, end the loop. */
3589 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3590 invis_p = 0;
3591 else
3592 {
3593 /* We skipped some characters but not necessarily
3594 all there are. Check if we ended up on visible
3595 text. Fget_char_property returns the property of
3596 the char before the given position, i.e. if we
3597 get invis_p = 0, this means that the char at
3598 newpos is visible. */
3599 pos = make_number (newpos);
3600 prop = Fget_char_property (pos, Qinvisible, it->window);
3601 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3602 }
3603
3604 /* If we ended up on invisible text, proceed to
3605 skip starting with next_stop. */
3606 if (invis_p)
3607 IT_CHARPOS (*it) = next_stop;
3608
3609 /* If there are adjacent invisible texts, don't lose the
3610 second one's ellipsis. */
3611 if (invis_p == 2)
3612 display_ellipsis_p = 1;
3613 }
3614 while (invis_p);
3615
3616 /* The position newpos is now either ZV or on visible text. */
3617 IT_CHARPOS (*it) = newpos;
3618 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3619
3620 /* If there are before-strings at the start of invisible
3621 text, and the text is invisible because of a text
3622 property, arrange to show before-strings because 20.x did
3623 it that way. (If the text is invisible because of an
3624 overlay property instead of a text property, this is
3625 already handled in the overlay code.) */
3626 if (NILP (overlay)
3627 && get_overlay_strings (it, start_charpos))
3628 {
3629 handled = HANDLED_RECOMPUTE_PROPS;
3630 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3631 }
3632 else if (display_ellipsis_p)
3633 {
3634 /* Make sure that the glyphs of the ellipsis will get
3635 correct `charpos' values. If we would not update
3636 it->position here, the glyphs would belong to the
3637 last visible character _before_ the invisible
3638 text, which confuses `set_cursor_from_row'.
3639
3640 We use the last invisible position instead of the
3641 first because this way the cursor is always drawn on
3642 the first "." of the ellipsis, whenever PT is inside
3643 the invisible text. Otherwise the cursor would be
3644 placed _after_ the ellipsis when the point is after the
3645 first invisible character. */
3646 it->position.charpos = IT_CHARPOS (*it) - 1;
3647 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3648 setup_for_ellipsis (it, 0);
3649 }
3650 }
3651 }
3652
3653 return handled;
3654 }
3655
3656
3657 /* Make iterator IT return `...' next.
3658 Replaces LEN characters from buffer. */
3659
3660 static void
3661 setup_for_ellipsis (it, len)
3662 struct it *it;
3663 int len;
3664 {
3665 /* Use the display table definition for `...'. Invalid glyphs
3666 will be handled by the method returning elements from dpvec. */
3667 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3668 {
3669 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3670 it->dpvec = v->contents;
3671 it->dpend = v->contents + v->size;
3672 }
3673 else
3674 {
3675 /* Default `...'. */
3676 it->dpvec = default_invis_vector;
3677 it->dpend = default_invis_vector + 3;
3678 }
3679
3680 it->dpvec_char_len = len;
3681 it->current.dpvec_index = 0;
3682 it->dpvec_face_id = -1;
3683
3684 /* Remember the current face id in case glyphs specify faces.
3685 IT's face is restored in set_iterator_to_next.
3686 saved_face_id was set to preceding char's face in handle_stop. */
3687 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3688 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3689
3690 it->method = GET_FROM_DISPLAY_VECTOR;
3691 it->ellipsis_p = 1;
3692 }
3693
3694
3695 \f
3696 /***********************************************************************
3697 'display' property
3698 ***********************************************************************/
3699
3700 /* Set up iterator IT from `display' property at its current position.
3701 Called from handle_stop.
3702 We return HANDLED_RETURN if some part of the display property
3703 overrides the display of the buffer text itself.
3704 Otherwise we return HANDLED_NORMALLY. */
3705
3706 static enum prop_handled
3707 handle_display_prop (it)
3708 struct it *it;
3709 {
3710 Lisp_Object prop, object;
3711 struct text_pos *position;
3712 /* Nonzero if some property replaces the display of the text itself. */
3713 int display_replaced_p = 0;
3714
3715 if (STRINGP (it->string))
3716 {
3717 object = it->string;
3718 position = &it->current.string_pos;
3719 }
3720 else
3721 {
3722 XSETWINDOW (object, it->w);
3723 position = &it->current.pos;
3724 }
3725
3726 /* Reset those iterator values set from display property values. */
3727 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3728 it->space_width = Qnil;
3729 it->font_height = Qnil;
3730 it->voffset = 0;
3731
3732 /* We don't support recursive `display' properties, i.e. string
3733 values that have a string `display' property, that have a string
3734 `display' property etc. */
3735 if (!it->string_from_display_prop_p)
3736 it->area = TEXT_AREA;
3737
3738 prop = Fget_char_property (make_number (position->charpos),
3739 Qdisplay, object);
3740 if (NILP (prop))
3741 return HANDLED_NORMALLY;
3742
3743 if (!STRINGP (it->string))
3744 object = it->w->buffer;
3745
3746 if (CONSP (prop)
3747 /* Simple properties. */
3748 && !EQ (XCAR (prop), Qimage)
3749 && !EQ (XCAR (prop), Qspace)
3750 && !EQ (XCAR (prop), Qwhen)
3751 && !EQ (XCAR (prop), Qslice)
3752 && !EQ (XCAR (prop), Qspace_width)
3753 && !EQ (XCAR (prop), Qheight)
3754 && !EQ (XCAR (prop), Qraise)
3755 /* Marginal area specifications. */
3756 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3757 && !EQ (XCAR (prop), Qleft_fringe)
3758 && !EQ (XCAR (prop), Qright_fringe)
3759 && !NILP (XCAR (prop)))
3760 {
3761 for (; CONSP (prop); prop = XCDR (prop))
3762 {
3763 if (handle_single_display_spec (it, XCAR (prop), object,
3764 position, display_replaced_p))
3765 display_replaced_p = 1;
3766 }
3767 }
3768 else if (VECTORP (prop))
3769 {
3770 int i;
3771 for (i = 0; i < ASIZE (prop); ++i)
3772 if (handle_single_display_spec (it, AREF (prop, i), object,
3773 position, display_replaced_p))
3774 display_replaced_p = 1;
3775 }
3776 else
3777 {
3778 int ret = handle_single_display_spec (it, prop, object, position, 0);
3779 if (ret < 0) /* Replaced by "", i.e. nothing. */
3780 return HANDLED_RECOMPUTE_PROPS;
3781 if (ret)
3782 display_replaced_p = 1;
3783 }
3784
3785 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3786 }
3787
3788
3789 /* Value is the position of the end of the `display' property starting
3790 at START_POS in OBJECT. */
3791
3792 static struct text_pos
3793 display_prop_end (it, object, start_pos)
3794 struct it *it;
3795 Lisp_Object object;
3796 struct text_pos start_pos;
3797 {
3798 Lisp_Object end;
3799 struct text_pos end_pos;
3800
3801 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3802 Qdisplay, object, Qnil);
3803 CHARPOS (end_pos) = XFASTINT (end);
3804 if (STRINGP (object))
3805 compute_string_pos (&end_pos, start_pos, it->string);
3806 else
3807 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3808
3809 return end_pos;
3810 }
3811
3812
3813 /* Set up IT from a single `display' specification PROP. OBJECT
3814 is the object in which the `display' property was found. *POSITION
3815 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3816 means that we previously saw a display specification which already
3817 replaced text display with something else, for example an image;
3818 we ignore such properties after the first one has been processed.
3819
3820 If PROP is a `space' or `image' specification, and in some other
3821 cases too, set *POSITION to the position where the `display'
3822 property ends.
3823
3824 Value is non-zero if something was found which replaces the display
3825 of buffer or string text. Specifically, the value is -1 if that
3826 "something" is "nothing". */
3827
3828 static int
3829 handle_single_display_spec (it, spec, object, position,
3830 display_replaced_before_p)
3831 struct it *it;
3832 Lisp_Object spec;
3833 Lisp_Object object;
3834 struct text_pos *position;
3835 int display_replaced_before_p;
3836 {
3837 Lisp_Object form;
3838 Lisp_Object location, value;
3839 struct text_pos start_pos;
3840 int valid_p;
3841
3842 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3843 If the result is non-nil, use VALUE instead of SPEC. */
3844 form = Qt;
3845 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3846 {
3847 spec = XCDR (spec);
3848 if (!CONSP (spec))
3849 return 0;
3850 form = XCAR (spec);
3851 spec = XCDR (spec);
3852 }
3853
3854 if (!NILP (form) && !EQ (form, Qt))
3855 {
3856 int count = SPECPDL_INDEX ();
3857 struct gcpro gcpro1;
3858
3859 /* Bind `object' to the object having the `display' property, a
3860 buffer or string. Bind `position' to the position in the
3861 object where the property was found, and `buffer-position'
3862 to the current position in the buffer. */
3863 specbind (Qobject, object);
3864 specbind (Qposition, make_number (CHARPOS (*position)));
3865 specbind (Qbuffer_position,
3866 make_number (STRINGP (object)
3867 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3868 GCPRO1 (form);
3869 form = safe_eval (form);
3870 UNGCPRO;
3871 unbind_to (count, Qnil);
3872 }
3873
3874 if (NILP (form))
3875 return 0;
3876
3877 /* Handle `(height HEIGHT)' specifications. */
3878 if (CONSP (spec)
3879 && EQ (XCAR (spec), Qheight)
3880 && CONSP (XCDR (spec)))
3881 {
3882 if (!FRAME_WINDOW_P (it->f))
3883 return 0;
3884
3885 it->font_height = XCAR (XCDR (spec));
3886 if (!NILP (it->font_height))
3887 {
3888 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3889 int new_height = -1;
3890
3891 if (CONSP (it->font_height)
3892 && (EQ (XCAR (it->font_height), Qplus)
3893 || EQ (XCAR (it->font_height), Qminus))
3894 && CONSP (XCDR (it->font_height))
3895 && INTEGERP (XCAR (XCDR (it->font_height))))
3896 {
3897 /* `(+ N)' or `(- N)' where N is an integer. */
3898 int steps = XINT (XCAR (XCDR (it->font_height)));
3899 if (EQ (XCAR (it->font_height), Qplus))
3900 steps = - steps;
3901 it->face_id = smaller_face (it->f, it->face_id, steps);
3902 }
3903 else if (FUNCTIONP (it->font_height))
3904 {
3905 /* Call function with current height as argument.
3906 Value is the new height. */
3907 Lisp_Object height;
3908 height = safe_call1 (it->font_height,
3909 face->lface[LFACE_HEIGHT_INDEX]);
3910 if (NUMBERP (height))
3911 new_height = XFLOATINT (height);
3912 }
3913 else if (NUMBERP (it->font_height))
3914 {
3915 /* Value is a multiple of the canonical char height. */
3916 struct face *face;
3917
3918 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3919 new_height = (XFLOATINT (it->font_height)
3920 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3921 }
3922 else
3923 {
3924 /* Evaluate IT->font_height with `height' bound to the
3925 current specified height to get the new height. */
3926 int count = SPECPDL_INDEX ();
3927
3928 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3929 value = safe_eval (it->font_height);
3930 unbind_to (count, Qnil);
3931
3932 if (NUMBERP (value))
3933 new_height = XFLOATINT (value);
3934 }
3935
3936 if (new_height > 0)
3937 it->face_id = face_with_height (it->f, it->face_id, new_height);
3938 }
3939
3940 return 0;
3941 }
3942
3943 /* Handle `(space_width WIDTH)'. */
3944 if (CONSP (spec)
3945 && EQ (XCAR (spec), Qspace_width)
3946 && CONSP (XCDR (spec)))
3947 {
3948 if (!FRAME_WINDOW_P (it->f))
3949 return 0;
3950
3951 value = XCAR (XCDR (spec));
3952 if (NUMBERP (value) && XFLOATINT (value) > 0)
3953 it->space_width = value;
3954
3955 return 0;
3956 }
3957
3958 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3959 if (CONSP (spec)
3960 && EQ (XCAR (spec), Qslice))
3961 {
3962 Lisp_Object tem;
3963
3964 if (!FRAME_WINDOW_P (it->f))
3965 return 0;
3966
3967 if (tem = XCDR (spec), CONSP (tem))
3968 {
3969 it->slice.x = XCAR (tem);
3970 if (tem = XCDR (tem), CONSP (tem))
3971 {
3972 it->slice.y = XCAR (tem);
3973 if (tem = XCDR (tem), CONSP (tem))
3974 {
3975 it->slice.width = XCAR (tem);
3976 if (tem = XCDR (tem), CONSP (tem))
3977 it->slice.height = XCAR (tem);
3978 }
3979 }
3980 }
3981
3982 return 0;
3983 }
3984
3985 /* Handle `(raise FACTOR)'. */
3986 if (CONSP (spec)
3987 && EQ (XCAR (spec), Qraise)
3988 && CONSP (XCDR (spec)))
3989 {
3990 if (!FRAME_WINDOW_P (it->f))
3991 return 0;
3992
3993 #ifdef HAVE_WINDOW_SYSTEM
3994 value = XCAR (XCDR (spec));
3995 if (NUMBERP (value))
3996 {
3997 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3998 it->voffset = - (XFLOATINT (value)
3999 * (FONT_HEIGHT (face->font)));
4000 }
4001 #endif /* HAVE_WINDOW_SYSTEM */
4002
4003 return 0;
4004 }
4005
4006 /* Don't handle the other kinds of display specifications
4007 inside a string that we got from a `display' property. */
4008 if (it->string_from_display_prop_p)
4009 return 0;
4010
4011 /* Characters having this form of property are not displayed, so
4012 we have to find the end of the property. */
4013 start_pos = *position;
4014 *position = display_prop_end (it, object, start_pos);
4015 value = Qnil;
4016
4017 /* Stop the scan at that end position--we assume that all
4018 text properties change there. */
4019 it->stop_charpos = position->charpos;
4020
4021 /* Handle `(left-fringe BITMAP [FACE])'
4022 and `(right-fringe BITMAP [FACE])'. */
4023 if (CONSP (spec)
4024 && (EQ (XCAR (spec), Qleft_fringe)
4025 || EQ (XCAR (spec), Qright_fringe))
4026 && CONSP (XCDR (spec)))
4027 {
4028 int face_id = DEFAULT_FACE_ID;
4029 int fringe_bitmap;
4030
4031 if (!FRAME_WINDOW_P (it->f))
4032 /* If we return here, POSITION has been advanced
4033 across the text with this property. */
4034 return 0;
4035
4036 #ifdef HAVE_WINDOW_SYSTEM
4037 value = XCAR (XCDR (spec));
4038 if (!SYMBOLP (value)
4039 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4040 /* If we return here, POSITION has been advanced
4041 across the text with this property. */
4042 return 0;
4043
4044 if (CONSP (XCDR (XCDR (spec))))
4045 {
4046 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4047 int face_id2 = lookup_derived_face (it->f, face_name,
4048 'A', FRINGE_FACE_ID, 0);
4049 if (face_id2 >= 0)
4050 face_id = face_id2;
4051 }
4052
4053 /* Save current settings of IT so that we can restore them
4054 when we are finished with the glyph property value. */
4055
4056 push_it (it);
4057
4058 it->area = TEXT_AREA;
4059 it->what = IT_IMAGE;
4060 it->image_id = -1; /* no image */
4061 it->position = start_pos;
4062 it->object = NILP (object) ? it->w->buffer : object;
4063 it->method = GET_FROM_IMAGE;
4064 it->face_id = face_id;
4065
4066 /* Say that we haven't consumed the characters with
4067 `display' property yet. The call to pop_it in
4068 set_iterator_to_next will clean this up. */
4069 *position = start_pos;
4070
4071 if (EQ (XCAR (spec), Qleft_fringe))
4072 {
4073 it->left_user_fringe_bitmap = fringe_bitmap;
4074 it->left_user_fringe_face_id = face_id;
4075 }
4076 else
4077 {
4078 it->right_user_fringe_bitmap = fringe_bitmap;
4079 it->right_user_fringe_face_id = face_id;
4080 }
4081 #endif /* HAVE_WINDOW_SYSTEM */
4082 return 1;
4083 }
4084
4085 /* Prepare to handle `((margin left-margin) ...)',
4086 `((margin right-margin) ...)' and `((margin nil) ...)'
4087 prefixes for display specifications. */
4088 location = Qunbound;
4089 if (CONSP (spec) && CONSP (XCAR (spec)))
4090 {
4091 Lisp_Object tem;
4092
4093 value = XCDR (spec);
4094 if (CONSP (value))
4095 value = XCAR (value);
4096
4097 tem = XCAR (spec);
4098 if (EQ (XCAR (tem), Qmargin)
4099 && (tem = XCDR (tem),
4100 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4101 (NILP (tem)
4102 || EQ (tem, Qleft_margin)
4103 || EQ (tem, Qright_margin))))
4104 location = tem;
4105 }
4106
4107 if (EQ (location, Qunbound))
4108 {
4109 location = Qnil;
4110 value = spec;
4111 }
4112
4113 /* After this point, VALUE is the property after any
4114 margin prefix has been stripped. It must be a string,
4115 an image specification, or `(space ...)'.
4116
4117 LOCATION specifies where to display: `left-margin',
4118 `right-margin' or nil. */
4119
4120 valid_p = (STRINGP (value)
4121 #ifdef HAVE_WINDOW_SYSTEM
4122 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4123 #endif /* not HAVE_WINDOW_SYSTEM */
4124 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4125
4126 if (valid_p && !display_replaced_before_p)
4127 {
4128 /* Save current settings of IT so that we can restore them
4129 when we are finished with the glyph property value. */
4130 push_it (it);
4131 if (NILP (location))
4132 it->area = TEXT_AREA;
4133 else if (EQ (location, Qleft_margin))
4134 it->area = LEFT_MARGIN_AREA;
4135 else
4136 it->area = RIGHT_MARGIN_AREA;
4137
4138 if (STRINGP (value))
4139 {
4140 if (SCHARS (value) == 0)
4141 {
4142 pop_it (it);
4143 return -1; /* Replaced by "", i.e. nothing. */
4144 }
4145 it->string = value;
4146 it->multibyte_p = STRING_MULTIBYTE (it->string);
4147 it->current.overlay_string_index = -1;
4148 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4149 it->end_charpos = it->string_nchars = SCHARS (it->string);
4150 it->method = GET_FROM_STRING;
4151 it->stop_charpos = 0;
4152 it->string_from_display_prop_p = 1;
4153 /* Say that we haven't consumed the characters with
4154 `display' property yet. The call to pop_it in
4155 set_iterator_to_next will clean this up. */
4156 *position = start_pos;
4157 }
4158 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4159 {
4160 it->method = GET_FROM_STRETCH;
4161 it->object = value;
4162 *position = it->position = start_pos;
4163 }
4164 #ifdef HAVE_WINDOW_SYSTEM
4165 else
4166 {
4167 it->what = IT_IMAGE;
4168 it->image_id = lookup_image (it->f, value);
4169 it->position = start_pos;
4170 it->object = NILP (object) ? it->w->buffer : object;
4171 it->method = GET_FROM_IMAGE;
4172
4173 /* Say that we haven't consumed the characters with
4174 `display' property yet. The call to pop_it in
4175 set_iterator_to_next will clean this up. */
4176 *position = start_pos;
4177 }
4178 #endif /* HAVE_WINDOW_SYSTEM */
4179
4180 return 1;
4181 }
4182
4183 /* Invalid property or property not supported. Restore
4184 POSITION to what it was before. */
4185 *position = start_pos;
4186 return 0;
4187 }
4188
4189
4190 /* Check if SPEC is a display sub-property value whose text should be
4191 treated as intangible. */
4192
4193 static int
4194 single_display_spec_intangible_p (prop)
4195 Lisp_Object prop;
4196 {
4197 /* Skip over `when FORM'. */
4198 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4199 {
4200 prop = XCDR (prop);
4201 if (!CONSP (prop))
4202 return 0;
4203 prop = XCDR (prop);
4204 }
4205
4206 if (STRINGP (prop))
4207 return 1;
4208
4209 if (!CONSP (prop))
4210 return 0;
4211
4212 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4213 we don't need to treat text as intangible. */
4214 if (EQ (XCAR (prop), Qmargin))
4215 {
4216 prop = XCDR (prop);
4217 if (!CONSP (prop))
4218 return 0;
4219
4220 prop = XCDR (prop);
4221 if (!CONSP (prop)
4222 || EQ (XCAR (prop), Qleft_margin)
4223 || EQ (XCAR (prop), Qright_margin))
4224 return 0;
4225 }
4226
4227 return (CONSP (prop)
4228 && (EQ (XCAR (prop), Qimage)
4229 || EQ (XCAR (prop), Qspace)));
4230 }
4231
4232
4233 /* Check if PROP is a display property value whose text should be
4234 treated as intangible. */
4235
4236 int
4237 display_prop_intangible_p (prop)
4238 Lisp_Object prop;
4239 {
4240 if (CONSP (prop)
4241 && CONSP (XCAR (prop))
4242 && !EQ (Qmargin, XCAR (XCAR (prop))))
4243 {
4244 /* A list of sub-properties. */
4245 while (CONSP (prop))
4246 {
4247 if (single_display_spec_intangible_p (XCAR (prop)))
4248 return 1;
4249 prop = XCDR (prop);
4250 }
4251 }
4252 else if (VECTORP (prop))
4253 {
4254 /* A vector of sub-properties. */
4255 int i;
4256 for (i = 0; i < ASIZE (prop); ++i)
4257 if (single_display_spec_intangible_p (AREF (prop, i)))
4258 return 1;
4259 }
4260 else
4261 return single_display_spec_intangible_p (prop);
4262
4263 return 0;
4264 }
4265
4266
4267 /* Return 1 if PROP is a display sub-property value containing STRING. */
4268
4269 static int
4270 single_display_spec_string_p (prop, string)
4271 Lisp_Object prop, string;
4272 {
4273 if (EQ (string, prop))
4274 return 1;
4275
4276 /* Skip over `when FORM'. */
4277 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4278 {
4279 prop = XCDR (prop);
4280 if (!CONSP (prop))
4281 return 0;
4282 prop = XCDR (prop);
4283 }
4284
4285 if (CONSP (prop))
4286 /* Skip over `margin LOCATION'. */
4287 if (EQ (XCAR (prop), Qmargin))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292
4293 prop = XCDR (prop);
4294 if (!CONSP (prop))
4295 return 0;
4296 }
4297
4298 return CONSP (prop) && EQ (XCAR (prop), string);
4299 }
4300
4301
4302 /* Return 1 if STRING appears in the `display' property PROP. */
4303
4304 static int
4305 display_prop_string_p (prop, string)
4306 Lisp_Object prop, string;
4307 {
4308 if (CONSP (prop)
4309 && CONSP (XCAR (prop))
4310 && !EQ (Qmargin, XCAR (XCAR (prop))))
4311 {
4312 /* A list of sub-properties. */
4313 while (CONSP (prop))
4314 {
4315 if (single_display_spec_string_p (XCAR (prop), string))
4316 return 1;
4317 prop = XCDR (prop);
4318 }
4319 }
4320 else if (VECTORP (prop))
4321 {
4322 /* A vector of sub-properties. */
4323 int i;
4324 for (i = 0; i < ASIZE (prop); ++i)
4325 if (single_display_spec_string_p (AREF (prop, i), string))
4326 return 1;
4327 }
4328 else
4329 return single_display_spec_string_p (prop, string);
4330
4331 return 0;
4332 }
4333
4334
4335 /* Determine from which buffer position in W's buffer STRING comes
4336 from. AROUND_CHARPOS is an approximate position where it could
4337 be from. Value is the buffer position or 0 if it couldn't be
4338 determined.
4339
4340 W's buffer must be current.
4341
4342 This function is necessary because we don't record buffer positions
4343 in glyphs generated from strings (to keep struct glyph small).
4344 This function may only use code that doesn't eval because it is
4345 called asynchronously from note_mouse_highlight. */
4346
4347 int
4348 string_buffer_position (w, string, around_charpos)
4349 struct window *w;
4350 Lisp_Object string;
4351 int around_charpos;
4352 {
4353 Lisp_Object limit, prop, pos;
4354 const int MAX_DISTANCE = 1000;
4355 int found = 0;
4356
4357 pos = make_number (around_charpos);
4358 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4359 while (!found && !EQ (pos, limit))
4360 {
4361 prop = Fget_char_property (pos, Qdisplay, Qnil);
4362 if (!NILP (prop) && display_prop_string_p (prop, string))
4363 found = 1;
4364 else
4365 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4366 }
4367
4368 if (!found)
4369 {
4370 pos = make_number (around_charpos);
4371 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4372 while (!found && !EQ (pos, limit))
4373 {
4374 prop = Fget_char_property (pos, Qdisplay, Qnil);
4375 if (!NILP (prop) && display_prop_string_p (prop, string))
4376 found = 1;
4377 else
4378 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4379 limit);
4380 }
4381 }
4382
4383 return found ? XINT (pos) : 0;
4384 }
4385
4386
4387 \f
4388 /***********************************************************************
4389 `composition' property
4390 ***********************************************************************/
4391
4392 /* Set up iterator IT from `composition' property at its current
4393 position. Called from handle_stop. */
4394
4395 static enum prop_handled
4396 handle_composition_prop (it)
4397 struct it *it;
4398 {
4399 Lisp_Object prop, string;
4400 int pos, pos_byte, end;
4401 enum prop_handled handled = HANDLED_NORMALLY;
4402
4403 if (STRINGP (it->string))
4404 {
4405 pos = IT_STRING_CHARPOS (*it);
4406 pos_byte = IT_STRING_BYTEPOS (*it);
4407 string = it->string;
4408 }
4409 else
4410 {
4411 pos = IT_CHARPOS (*it);
4412 pos_byte = IT_BYTEPOS (*it);
4413 string = Qnil;
4414 }
4415
4416 /* If there's a valid composition and point is not inside of the
4417 composition (in the case that the composition is from the current
4418 buffer), draw a glyph composed from the composition components. */
4419 if (find_composition (pos, -1, &pos, &end, &prop, string)
4420 && COMPOSITION_VALID_P (pos, end, prop)
4421 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4422 {
4423 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4424
4425 if (id >= 0)
4426 {
4427 it->method = GET_FROM_COMPOSITION;
4428 it->cmp_id = id;
4429 it->cmp_len = COMPOSITION_LENGTH (prop);
4430 /* For a terminal, draw only the first character of the
4431 components. */
4432 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4433 it->len = (STRINGP (it->string)
4434 ? string_char_to_byte (it->string, end)
4435 : CHAR_TO_BYTE (end)) - pos_byte;
4436 it->stop_charpos = end;
4437 handled = HANDLED_RETURN;
4438 }
4439 }
4440
4441 return handled;
4442 }
4443
4444
4445 \f
4446 /***********************************************************************
4447 Overlay strings
4448 ***********************************************************************/
4449
4450 /* The following structure is used to record overlay strings for
4451 later sorting in load_overlay_strings. */
4452
4453 struct overlay_entry
4454 {
4455 Lisp_Object overlay;
4456 Lisp_Object string;
4457 int priority;
4458 int after_string_p;
4459 };
4460
4461
4462 /* Set up iterator IT from overlay strings at its current position.
4463 Called from handle_stop. */
4464
4465 static enum prop_handled
4466 handle_overlay_change (it)
4467 struct it *it;
4468 {
4469 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4470 return HANDLED_RECOMPUTE_PROPS;
4471 else
4472 return HANDLED_NORMALLY;
4473 }
4474
4475
4476 /* Set up the next overlay string for delivery by IT, if there is an
4477 overlay string to deliver. Called by set_iterator_to_next when the
4478 end of the current overlay string is reached. If there are more
4479 overlay strings to display, IT->string and
4480 IT->current.overlay_string_index are set appropriately here.
4481 Otherwise IT->string is set to nil. */
4482
4483 static void
4484 next_overlay_string (it)
4485 struct it *it;
4486 {
4487 ++it->current.overlay_string_index;
4488 if (it->current.overlay_string_index == it->n_overlay_strings)
4489 {
4490 /* No more overlay strings. Restore IT's settings to what
4491 they were before overlay strings were processed, and
4492 continue to deliver from current_buffer. */
4493 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4494
4495 pop_it (it);
4496 xassert (it->stop_charpos >= BEGV
4497 && it->stop_charpos <= it->end_charpos);
4498 it->string = Qnil;
4499 it->current.overlay_string_index = -1;
4500 SET_TEXT_POS (it->current.string_pos, -1, -1);
4501 it->n_overlay_strings = 0;
4502 it->method = GET_FROM_BUFFER;
4503
4504 /* If we're at the end of the buffer, record that we have
4505 processed the overlay strings there already, so that
4506 next_element_from_buffer doesn't try it again. */
4507 if (IT_CHARPOS (*it) >= it->end_charpos)
4508 it->overlay_strings_at_end_processed_p = 1;
4509
4510 /* If we have to display `...' for invisible text, set
4511 the iterator up for that. */
4512 if (display_ellipsis_p)
4513 setup_for_ellipsis (it, 0);
4514 }
4515 else
4516 {
4517 /* There are more overlay strings to process. If
4518 IT->current.overlay_string_index has advanced to a position
4519 where we must load IT->overlay_strings with more strings, do
4520 it. */
4521 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4522
4523 if (it->current.overlay_string_index && i == 0)
4524 load_overlay_strings (it, 0);
4525
4526 /* Initialize IT to deliver display elements from the overlay
4527 string. */
4528 it->string = it->overlay_strings[i];
4529 it->multibyte_p = STRING_MULTIBYTE (it->string);
4530 SET_TEXT_POS (it->current.string_pos, 0, 0);
4531 it->method = GET_FROM_STRING;
4532 it->stop_charpos = 0;
4533 }
4534
4535 CHECK_IT (it);
4536 }
4537
4538
4539 /* Compare two overlay_entry structures E1 and E2. Used as a
4540 comparison function for qsort in load_overlay_strings. Overlay
4541 strings for the same position are sorted so that
4542
4543 1. All after-strings come in front of before-strings, except
4544 when they come from the same overlay.
4545
4546 2. Within after-strings, strings are sorted so that overlay strings
4547 from overlays with higher priorities come first.
4548
4549 2. Within before-strings, strings are sorted so that overlay
4550 strings from overlays with higher priorities come last.
4551
4552 Value is analogous to strcmp. */
4553
4554
4555 static int
4556 compare_overlay_entries (e1, e2)
4557 void *e1, *e2;
4558 {
4559 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4560 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4561 int result;
4562
4563 if (entry1->after_string_p != entry2->after_string_p)
4564 {
4565 /* Let after-strings appear in front of before-strings if
4566 they come from different overlays. */
4567 if (EQ (entry1->overlay, entry2->overlay))
4568 result = entry1->after_string_p ? 1 : -1;
4569 else
4570 result = entry1->after_string_p ? -1 : 1;
4571 }
4572 else if (entry1->after_string_p)
4573 /* After-strings sorted in order of decreasing priority. */
4574 result = entry2->priority - entry1->priority;
4575 else
4576 /* Before-strings sorted in order of increasing priority. */
4577 result = entry1->priority - entry2->priority;
4578
4579 return result;
4580 }
4581
4582
4583 /* Load the vector IT->overlay_strings with overlay strings from IT's
4584 current buffer position, or from CHARPOS if that is > 0. Set
4585 IT->n_overlays to the total number of overlay strings found.
4586
4587 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4588 a time. On entry into load_overlay_strings,
4589 IT->current.overlay_string_index gives the number of overlay
4590 strings that have already been loaded by previous calls to this
4591 function.
4592
4593 IT->add_overlay_start contains an additional overlay start
4594 position to consider for taking overlay strings from, if non-zero.
4595 This position comes into play when the overlay has an `invisible'
4596 property, and both before and after-strings. When we've skipped to
4597 the end of the overlay, because of its `invisible' property, we
4598 nevertheless want its before-string to appear.
4599 IT->add_overlay_start will contain the overlay start position
4600 in this case.
4601
4602 Overlay strings are sorted so that after-string strings come in
4603 front of before-string strings. Within before and after-strings,
4604 strings are sorted by overlay priority. See also function
4605 compare_overlay_entries. */
4606
4607 static void
4608 load_overlay_strings (it, charpos)
4609 struct it *it;
4610 int charpos;
4611 {
4612 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4613 Lisp_Object overlay, window, str, invisible;
4614 struct Lisp_Overlay *ov;
4615 int start, end;
4616 int size = 20;
4617 int n = 0, i, j, invis_p;
4618 struct overlay_entry *entries
4619 = (struct overlay_entry *) alloca (size * sizeof *entries);
4620
4621 if (charpos <= 0)
4622 charpos = IT_CHARPOS (*it);
4623
4624 /* Append the overlay string STRING of overlay OVERLAY to vector
4625 `entries' which has size `size' and currently contains `n'
4626 elements. AFTER_P non-zero means STRING is an after-string of
4627 OVERLAY. */
4628 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4629 do \
4630 { \
4631 Lisp_Object priority; \
4632 \
4633 if (n == size) \
4634 { \
4635 int new_size = 2 * size; \
4636 struct overlay_entry *old = entries; \
4637 entries = \
4638 (struct overlay_entry *) alloca (new_size \
4639 * sizeof *entries); \
4640 bcopy (old, entries, size * sizeof *entries); \
4641 size = new_size; \
4642 } \
4643 \
4644 entries[n].string = (STRING); \
4645 entries[n].overlay = (OVERLAY); \
4646 priority = Foverlay_get ((OVERLAY), Qpriority); \
4647 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4648 entries[n].after_string_p = (AFTER_P); \
4649 ++n; \
4650 } \
4651 while (0)
4652
4653 /* Process overlay before the overlay center. */
4654 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4655 {
4656 XSETMISC (overlay, ov);
4657 xassert (OVERLAYP (overlay));
4658 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4659 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4660
4661 if (end < charpos)
4662 break;
4663
4664 /* Skip this overlay if it doesn't start or end at IT's current
4665 position. */
4666 if (end != charpos && start != charpos)
4667 continue;
4668
4669 /* Skip this overlay if it doesn't apply to IT->w. */
4670 window = Foverlay_get (overlay, Qwindow);
4671 if (WINDOWP (window) && XWINDOW (window) != it->w)
4672 continue;
4673
4674 /* If the text ``under'' the overlay is invisible, both before-
4675 and after-strings from this overlay are visible; start and
4676 end position are indistinguishable. */
4677 invisible = Foverlay_get (overlay, Qinvisible);
4678 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4679
4680 /* If overlay has a non-empty before-string, record it. */
4681 if ((start == charpos || (end == charpos && invis_p))
4682 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4683 && SCHARS (str))
4684 RECORD_OVERLAY_STRING (overlay, str, 0);
4685
4686 /* If overlay has a non-empty after-string, record it. */
4687 if ((end == charpos || (start == charpos && invis_p))
4688 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4689 && SCHARS (str))
4690 RECORD_OVERLAY_STRING (overlay, str, 1);
4691 }
4692
4693 /* Process overlays after the overlay center. */
4694 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4695 {
4696 XSETMISC (overlay, ov);
4697 xassert (OVERLAYP (overlay));
4698 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4699 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4700
4701 if (start > charpos)
4702 break;
4703
4704 /* Skip this overlay if it doesn't start or end at IT's current
4705 position. */
4706 if (end != charpos && start != charpos)
4707 continue;
4708
4709 /* Skip this overlay if it doesn't apply to IT->w. */
4710 window = Foverlay_get (overlay, Qwindow);
4711 if (WINDOWP (window) && XWINDOW (window) != it->w)
4712 continue;
4713
4714 /* If the text ``under'' the overlay is invisible, it has a zero
4715 dimension, and both before- and after-strings apply. */
4716 invisible = Foverlay_get (overlay, Qinvisible);
4717 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4718
4719 /* If overlay has a non-empty before-string, record it. */
4720 if ((start == charpos || (end == charpos && invis_p))
4721 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4722 && SCHARS (str))
4723 RECORD_OVERLAY_STRING (overlay, str, 0);
4724
4725 /* If overlay has a non-empty after-string, record it. */
4726 if ((end == charpos || (start == charpos && invis_p))
4727 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4728 && SCHARS (str))
4729 RECORD_OVERLAY_STRING (overlay, str, 1);
4730 }
4731
4732 #undef RECORD_OVERLAY_STRING
4733
4734 /* Sort entries. */
4735 if (n > 1)
4736 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4737
4738 /* Record the total number of strings to process. */
4739 it->n_overlay_strings = n;
4740
4741 /* IT->current.overlay_string_index is the number of overlay strings
4742 that have already been consumed by IT. Copy some of the
4743 remaining overlay strings to IT->overlay_strings. */
4744 i = 0;
4745 j = it->current.overlay_string_index;
4746 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4747 it->overlay_strings[i++] = entries[j++].string;
4748
4749 CHECK_IT (it);
4750 }
4751
4752
4753 /* Get the first chunk of overlay strings at IT's current buffer
4754 position, or at CHARPOS if that is > 0. Value is non-zero if at
4755 least one overlay string was found. */
4756
4757 static int
4758 get_overlay_strings (it, charpos)
4759 struct it *it;
4760 int charpos;
4761 {
4762 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4763 process. This fills IT->overlay_strings with strings, and sets
4764 IT->n_overlay_strings to the total number of strings to process.
4765 IT->pos.overlay_string_index has to be set temporarily to zero
4766 because load_overlay_strings needs this; it must be set to -1
4767 when no overlay strings are found because a zero value would
4768 indicate a position in the first overlay string. */
4769 it->current.overlay_string_index = 0;
4770 load_overlay_strings (it, charpos);
4771
4772 /* If we found overlay strings, set up IT to deliver display
4773 elements from the first one. Otherwise set up IT to deliver
4774 from current_buffer. */
4775 if (it->n_overlay_strings)
4776 {
4777 /* Make sure we know settings in current_buffer, so that we can
4778 restore meaningful values when we're done with the overlay
4779 strings. */
4780 compute_stop_pos (it);
4781 xassert (it->face_id >= 0);
4782
4783 /* Save IT's settings. They are restored after all overlay
4784 strings have been processed. */
4785 xassert (it->sp == 0);
4786 push_it (it);
4787
4788 /* Set up IT to deliver display elements from the first overlay
4789 string. */
4790 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4791 it->string = it->overlay_strings[0];
4792 it->stop_charpos = 0;
4793 xassert (STRINGP (it->string));
4794 it->end_charpos = SCHARS (it->string);
4795 it->multibyte_p = STRING_MULTIBYTE (it->string);
4796 it->method = GET_FROM_STRING;
4797 }
4798 else
4799 {
4800 it->string = Qnil;
4801 it->current.overlay_string_index = -1;
4802 it->method = GET_FROM_BUFFER;
4803 }
4804
4805 CHECK_IT (it);
4806
4807 /* Value is non-zero if we found at least one overlay string. */
4808 return STRINGP (it->string);
4809 }
4810
4811
4812 \f
4813 /***********************************************************************
4814 Saving and restoring state
4815 ***********************************************************************/
4816
4817 /* Save current settings of IT on IT->stack. Called, for example,
4818 before setting up IT for an overlay string, to be able to restore
4819 IT's settings to what they were after the overlay string has been
4820 processed. */
4821
4822 static void
4823 push_it (it)
4824 struct it *it;
4825 {
4826 struct iterator_stack_entry *p;
4827
4828 xassert (it->sp < 2);
4829 p = it->stack + it->sp;
4830
4831 p->stop_charpos = it->stop_charpos;
4832 xassert (it->face_id >= 0);
4833 p->face_id = it->face_id;
4834 p->string = it->string;
4835 p->pos = it->current;
4836 p->end_charpos = it->end_charpos;
4837 p->string_nchars = it->string_nchars;
4838 p->area = it->area;
4839 p->multibyte_p = it->multibyte_p;
4840 p->slice = it->slice;
4841 p->space_width = it->space_width;
4842 p->font_height = it->font_height;
4843 p->voffset = it->voffset;
4844 p->string_from_display_prop_p = it->string_from_display_prop_p;
4845 p->display_ellipsis_p = 0;
4846 ++it->sp;
4847 }
4848
4849
4850 /* Restore IT's settings from IT->stack. Called, for example, when no
4851 more overlay strings must be processed, and we return to delivering
4852 display elements from a buffer, or when the end of a string from a
4853 `display' property is reached and we return to delivering display
4854 elements from an overlay string, or from a buffer. */
4855
4856 static void
4857 pop_it (it)
4858 struct it *it;
4859 {
4860 struct iterator_stack_entry *p;
4861
4862 xassert (it->sp > 0);
4863 --it->sp;
4864 p = it->stack + it->sp;
4865 it->stop_charpos = p->stop_charpos;
4866 it->face_id = p->face_id;
4867 it->string = p->string;
4868 it->current = p->pos;
4869 it->end_charpos = p->end_charpos;
4870 it->string_nchars = p->string_nchars;
4871 it->area = p->area;
4872 it->multibyte_p = p->multibyte_p;
4873 it->slice = p->slice;
4874 it->space_width = p->space_width;
4875 it->font_height = p->font_height;
4876 it->voffset = p->voffset;
4877 it->string_from_display_prop_p = p->string_from_display_prop_p;
4878 }
4879
4880
4881 \f
4882 /***********************************************************************
4883 Moving over lines
4884 ***********************************************************************/
4885
4886 /* Set IT's current position to the previous line start. */
4887
4888 static void
4889 back_to_previous_line_start (it)
4890 struct it *it;
4891 {
4892 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4893 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4894 }
4895
4896
4897 /* Move IT to the next line start.
4898
4899 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4900 we skipped over part of the text (as opposed to moving the iterator
4901 continuously over the text). Otherwise, don't change the value
4902 of *SKIPPED_P.
4903
4904 Newlines may come from buffer text, overlay strings, or strings
4905 displayed via the `display' property. That's the reason we can't
4906 simply use find_next_newline_no_quit.
4907
4908 Note that this function may not skip over invisible text that is so
4909 because of text properties and immediately follows a newline. If
4910 it would, function reseat_at_next_visible_line_start, when called
4911 from set_iterator_to_next, would effectively make invisible
4912 characters following a newline part of the wrong glyph row, which
4913 leads to wrong cursor motion. */
4914
4915 static int
4916 forward_to_next_line_start (it, skipped_p)
4917 struct it *it;
4918 int *skipped_p;
4919 {
4920 int old_selective, newline_found_p, n;
4921 const int MAX_NEWLINE_DISTANCE = 500;
4922
4923 /* If already on a newline, just consume it to avoid unintended
4924 skipping over invisible text below. */
4925 if (it->what == IT_CHARACTER
4926 && it->c == '\n'
4927 && CHARPOS (it->position) == IT_CHARPOS (*it))
4928 {
4929 set_iterator_to_next (it, 0);
4930 it->c = 0;
4931 return 1;
4932 }
4933
4934 /* Don't handle selective display in the following. It's (a)
4935 unnecessary because it's done by the caller, and (b) leads to an
4936 infinite recursion because next_element_from_ellipsis indirectly
4937 calls this function. */
4938 old_selective = it->selective;
4939 it->selective = 0;
4940
4941 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4942 from buffer text. */
4943 for (n = newline_found_p = 0;
4944 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4945 n += STRINGP (it->string) ? 0 : 1)
4946 {
4947 if (!get_next_display_element (it))
4948 return 0;
4949 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4950 set_iterator_to_next (it, 0);
4951 }
4952
4953 /* If we didn't find a newline near enough, see if we can use a
4954 short-cut. */
4955 if (!newline_found_p)
4956 {
4957 int start = IT_CHARPOS (*it);
4958 int limit = find_next_newline_no_quit (start, 1);
4959 Lisp_Object pos;
4960
4961 xassert (!STRINGP (it->string));
4962
4963 /* If there isn't any `display' property in sight, and no
4964 overlays, we can just use the position of the newline in
4965 buffer text. */
4966 if (it->stop_charpos >= limit
4967 || ((pos = Fnext_single_property_change (make_number (start),
4968 Qdisplay,
4969 Qnil, make_number (limit)),
4970 NILP (pos))
4971 && next_overlay_change (start) == ZV))
4972 {
4973 IT_CHARPOS (*it) = limit;
4974 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4975 *skipped_p = newline_found_p = 1;
4976 }
4977 else
4978 {
4979 while (get_next_display_element (it)
4980 && !newline_found_p)
4981 {
4982 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4983 set_iterator_to_next (it, 0);
4984 }
4985 }
4986 }
4987
4988 it->selective = old_selective;
4989 return newline_found_p;
4990 }
4991
4992
4993 /* Set IT's current position to the previous visible line start. Skip
4994 invisible text that is so either due to text properties or due to
4995 selective display. Caution: this does not change IT->current_x and
4996 IT->hpos. */
4997
4998 static void
4999 back_to_previous_visible_line_start (it)
5000 struct it *it;
5001 {
5002 while (IT_CHARPOS (*it) > BEGV)
5003 {
5004 back_to_previous_line_start (it);
5005 if (IT_CHARPOS (*it) <= BEGV)
5006 break;
5007
5008 /* If selective > 0, then lines indented more than that values
5009 are invisible. */
5010 if (it->selective > 0
5011 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5012 (double) it->selective)) /* iftc */
5013 continue;
5014
5015 /* Check the newline before point for invisibility. */
5016 {
5017 Lisp_Object prop;
5018 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5019 Qinvisible, it->window);
5020 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5021 continue;
5022 }
5023
5024 /* If newline has a display property that replaces the newline with something
5025 else (image or text), find start of overlay or interval and continue search
5026 from that point. */
5027 if (IT_CHARPOS (*it) > BEGV)
5028 {
5029 struct it it2 = *it;
5030 int pos;
5031 int beg, end;
5032 Lisp_Object val, overlay;
5033
5034 pos = --IT_CHARPOS (it2);
5035 --IT_BYTEPOS (it2);
5036 it2.sp = 0;
5037 if (handle_display_prop (&it2) == HANDLED_RETURN
5038 && !NILP (val = get_char_property_and_overlay
5039 (make_number (pos), Qdisplay, Qnil, &overlay))
5040 && (OVERLAYP (overlay)
5041 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5042 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5043 {
5044 if (beg < BEGV)
5045 beg = BEGV;
5046 IT_CHARPOS (*it) = beg;
5047 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5048 continue;
5049 }
5050 }
5051
5052 break;
5053 }
5054
5055 xassert (IT_CHARPOS (*it) >= BEGV);
5056 xassert (IT_CHARPOS (*it) == BEGV
5057 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5058 CHECK_IT (it);
5059 }
5060
5061
5062 /* Reseat iterator IT at the previous visible line start. Skip
5063 invisible text that is so either due to text properties or due to
5064 selective display. At the end, update IT's overlay information,
5065 face information etc. */
5066
5067 void
5068 reseat_at_previous_visible_line_start (it)
5069 struct it *it;
5070 {
5071 back_to_previous_visible_line_start (it);
5072 reseat (it, it->current.pos, 1);
5073 CHECK_IT (it);
5074 }
5075
5076
5077 /* Reseat iterator IT on the next visible line start in the current
5078 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5079 preceding the line start. Skip over invisible text that is so
5080 because of selective display. Compute faces, overlays etc at the
5081 new position. Note that this function does not skip over text that
5082 is invisible because of text properties. */
5083
5084 static void
5085 reseat_at_next_visible_line_start (it, on_newline_p)
5086 struct it *it;
5087 int on_newline_p;
5088 {
5089 int newline_found_p, skipped_p = 0;
5090
5091 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5092
5093 /* Skip over lines that are invisible because they are indented
5094 more than the value of IT->selective. */
5095 if (it->selective > 0)
5096 while (IT_CHARPOS (*it) < ZV
5097 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5098 (double) it->selective)) /* iftc */
5099 {
5100 xassert (IT_BYTEPOS (*it) == BEGV
5101 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5102 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5103 }
5104
5105 /* Position on the newline if that's what's requested. */
5106 if (on_newline_p && newline_found_p)
5107 {
5108 if (STRINGP (it->string))
5109 {
5110 if (IT_STRING_CHARPOS (*it) > 0)
5111 {
5112 --IT_STRING_CHARPOS (*it);
5113 --IT_STRING_BYTEPOS (*it);
5114 }
5115 }
5116 else if (IT_CHARPOS (*it) > BEGV)
5117 {
5118 --IT_CHARPOS (*it);
5119 --IT_BYTEPOS (*it);
5120 reseat (it, it->current.pos, 0);
5121 }
5122 }
5123 else if (skipped_p)
5124 reseat (it, it->current.pos, 0);
5125
5126 CHECK_IT (it);
5127 }
5128
5129
5130 \f
5131 /***********************************************************************
5132 Changing an iterator's position
5133 ***********************************************************************/
5134
5135 /* Change IT's current position to POS in current_buffer. If FORCE_P
5136 is non-zero, always check for text properties at the new position.
5137 Otherwise, text properties are only looked up if POS >=
5138 IT->check_charpos of a property. */
5139
5140 static void
5141 reseat (it, pos, force_p)
5142 struct it *it;
5143 struct text_pos pos;
5144 int force_p;
5145 {
5146 int original_pos = IT_CHARPOS (*it);
5147
5148 reseat_1 (it, pos, 0);
5149
5150 /* Determine where to check text properties. Avoid doing it
5151 where possible because text property lookup is very expensive. */
5152 if (force_p
5153 || CHARPOS (pos) > it->stop_charpos
5154 || CHARPOS (pos) < original_pos)
5155 handle_stop (it);
5156
5157 CHECK_IT (it);
5158 }
5159
5160
5161 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5162 IT->stop_pos to POS, also. */
5163
5164 static void
5165 reseat_1 (it, pos, set_stop_p)
5166 struct it *it;
5167 struct text_pos pos;
5168 int set_stop_p;
5169 {
5170 /* Don't call this function when scanning a C string. */
5171 xassert (it->s == NULL);
5172
5173 /* POS must be a reasonable value. */
5174 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5175
5176 it->current.pos = it->position = pos;
5177 XSETBUFFER (it->object, current_buffer);
5178 it->end_charpos = ZV;
5179 it->dpvec = NULL;
5180 it->current.dpvec_index = -1;
5181 it->current.overlay_string_index = -1;
5182 IT_STRING_CHARPOS (*it) = -1;
5183 IT_STRING_BYTEPOS (*it) = -1;
5184 it->string = Qnil;
5185 it->method = GET_FROM_BUFFER;
5186 /* RMS: I added this to fix a bug in move_it_vertically_backward
5187 where it->area continued to relate to the starting point
5188 for the backward motion. Bug report from
5189 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5190 However, I am not sure whether reseat still does the right thing
5191 in general after this change. */
5192 it->area = TEXT_AREA;
5193 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5194 it->sp = 0;
5195 it->face_before_selective_p = 0;
5196
5197 if (set_stop_p)
5198 it->stop_charpos = CHARPOS (pos);
5199 }
5200
5201
5202 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5203 If S is non-null, it is a C string to iterate over. Otherwise,
5204 STRING gives a Lisp string to iterate over.
5205
5206 If PRECISION > 0, don't return more then PRECISION number of
5207 characters from the string.
5208
5209 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5210 characters have been returned. FIELD_WIDTH < 0 means an infinite
5211 field width.
5212
5213 MULTIBYTE = 0 means disable processing of multibyte characters,
5214 MULTIBYTE > 0 means enable it,
5215 MULTIBYTE < 0 means use IT->multibyte_p.
5216
5217 IT must be initialized via a prior call to init_iterator before
5218 calling this function. */
5219
5220 static void
5221 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5222 struct it *it;
5223 unsigned char *s;
5224 Lisp_Object string;
5225 int charpos;
5226 int precision, field_width, multibyte;
5227 {
5228 /* No region in strings. */
5229 it->region_beg_charpos = it->region_end_charpos = -1;
5230
5231 /* No text property checks performed by default, but see below. */
5232 it->stop_charpos = -1;
5233
5234 /* Set iterator position and end position. */
5235 bzero (&it->current, sizeof it->current);
5236 it->current.overlay_string_index = -1;
5237 it->current.dpvec_index = -1;
5238 xassert (charpos >= 0);
5239
5240 /* If STRING is specified, use its multibyteness, otherwise use the
5241 setting of MULTIBYTE, if specified. */
5242 if (multibyte >= 0)
5243 it->multibyte_p = multibyte > 0;
5244
5245 if (s == NULL)
5246 {
5247 xassert (STRINGP (string));
5248 it->string = string;
5249 it->s = NULL;
5250 it->end_charpos = it->string_nchars = SCHARS (string);
5251 it->method = GET_FROM_STRING;
5252 it->current.string_pos = string_pos (charpos, string);
5253 }
5254 else
5255 {
5256 it->s = s;
5257 it->string = Qnil;
5258
5259 /* Note that we use IT->current.pos, not it->current.string_pos,
5260 for displaying C strings. */
5261 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5262 if (it->multibyte_p)
5263 {
5264 it->current.pos = c_string_pos (charpos, s, 1);
5265 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5266 }
5267 else
5268 {
5269 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5270 it->end_charpos = it->string_nchars = strlen (s);
5271 }
5272
5273 it->method = GET_FROM_C_STRING;
5274 }
5275
5276 /* PRECISION > 0 means don't return more than PRECISION characters
5277 from the string. */
5278 if (precision > 0 && it->end_charpos - charpos > precision)
5279 it->end_charpos = it->string_nchars = charpos + precision;
5280
5281 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5282 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5283 FIELD_WIDTH < 0 means infinite field width. This is useful for
5284 padding with `-' at the end of a mode line. */
5285 if (field_width < 0)
5286 field_width = INFINITY;
5287 if (field_width > it->end_charpos - charpos)
5288 it->end_charpos = charpos + field_width;
5289
5290 /* Use the standard display table for displaying strings. */
5291 if (DISP_TABLE_P (Vstandard_display_table))
5292 it->dp = XCHAR_TABLE (Vstandard_display_table);
5293
5294 it->stop_charpos = charpos;
5295 CHECK_IT (it);
5296 }
5297
5298
5299 \f
5300 /***********************************************************************
5301 Iteration
5302 ***********************************************************************/
5303
5304 /* Map enum it_method value to corresponding next_element_from_* function. */
5305
5306 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5307 {
5308 next_element_from_buffer,
5309 next_element_from_display_vector,
5310 next_element_from_composition,
5311 next_element_from_string,
5312 next_element_from_c_string,
5313 next_element_from_image,
5314 next_element_from_stretch
5315 };
5316
5317
5318 /* Load IT's display element fields with information about the next
5319 display element from the current position of IT. Value is zero if
5320 end of buffer (or C string) is reached. */
5321
5322 static struct frame *last_escape_glyph_frame = NULL;
5323 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5324 static int last_escape_glyph_merged_face_id = 0;
5325
5326 int
5327 get_next_display_element (it)
5328 struct it *it;
5329 {
5330 /* Non-zero means that we found a display element. Zero means that
5331 we hit the end of what we iterate over. Performance note: the
5332 function pointer `method' used here turns out to be faster than
5333 using a sequence of if-statements. */
5334 int success_p;
5335
5336 get_next:
5337 success_p = (*get_next_element[it->method]) (it);
5338
5339 if (it->what == IT_CHARACTER)
5340 {
5341 /* Map via display table or translate control characters.
5342 IT->c, IT->len etc. have been set to the next character by
5343 the function call above. If we have a display table, and it
5344 contains an entry for IT->c, translate it. Don't do this if
5345 IT->c itself comes from a display table, otherwise we could
5346 end up in an infinite recursion. (An alternative could be to
5347 count the recursion depth of this function and signal an
5348 error when a certain maximum depth is reached.) Is it worth
5349 it? */
5350 if (success_p && it->dpvec == NULL)
5351 {
5352 Lisp_Object dv;
5353
5354 if (it->dp
5355 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5356 VECTORP (dv)))
5357 {
5358 struct Lisp_Vector *v = XVECTOR (dv);
5359
5360 /* Return the first character from the display table
5361 entry, if not empty. If empty, don't display the
5362 current character. */
5363 if (v->size)
5364 {
5365 it->dpvec_char_len = it->len;
5366 it->dpvec = v->contents;
5367 it->dpend = v->contents + v->size;
5368 it->current.dpvec_index = 0;
5369 it->dpvec_face_id = -1;
5370 it->saved_face_id = it->face_id;
5371 it->method = GET_FROM_DISPLAY_VECTOR;
5372 it->ellipsis_p = 0;
5373 }
5374 else
5375 {
5376 set_iterator_to_next (it, 0);
5377 }
5378 goto get_next;
5379 }
5380
5381 /* Translate control characters into `\003' or `^C' form.
5382 Control characters coming from a display table entry are
5383 currently not translated because we use IT->dpvec to hold
5384 the translation. This could easily be changed but I
5385 don't believe that it is worth doing.
5386
5387 If it->multibyte_p is nonzero, eight-bit characters and
5388 non-printable multibyte characters are also translated to
5389 octal form.
5390
5391 If it->multibyte_p is zero, eight-bit characters that
5392 don't have corresponding multibyte char code are also
5393 translated to octal form. */
5394 else if ((it->c < ' '
5395 && (it->area != TEXT_AREA
5396 /* In mode line, treat \n like other crl chars. */
5397 || (it->c != '\t'
5398 && it->glyph_row && it->glyph_row->mode_line_p)
5399 || (it->c != '\n' && it->c != '\t')))
5400 || (it->multibyte_p
5401 ? ((it->c >= 127
5402 && it->len == 1)
5403 || !CHAR_PRINTABLE_P (it->c)
5404 || (!NILP (Vnobreak_char_display)
5405 && (it->c == 0x8a0 || it->c == 0x8ad
5406 || it->c == 0x920 || it->c == 0x92d
5407 || it->c == 0xe20 || it->c == 0xe2d
5408 || it->c == 0xf20 || it->c == 0xf2d)))
5409 : (it->c >= 127
5410 && (!unibyte_display_via_language_environment
5411 || it->c == unibyte_char_to_multibyte (it->c)))))
5412 {
5413 /* IT->c is a control character which must be displayed
5414 either as '\003' or as `^C' where the '\\' and '^'
5415 can be defined in the display table. Fill
5416 IT->ctl_chars with glyphs for what we have to
5417 display. Then, set IT->dpvec to these glyphs. */
5418 GLYPH g;
5419 int ctl_len;
5420 int face_id, lface_id = 0 ;
5421 GLYPH escape_glyph;
5422
5423 /* Handle control characters with ^. */
5424
5425 if (it->c < 128 && it->ctl_arrow_p)
5426 {
5427 g = '^'; /* default glyph for Control */
5428 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5429 if (it->dp
5430 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5431 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5432 {
5433 g = XINT (DISP_CTRL_GLYPH (it->dp));
5434 lface_id = FAST_GLYPH_FACE (g);
5435 }
5436 if (lface_id)
5437 {
5438 g = FAST_GLYPH_CHAR (g);
5439 face_id = merge_faces (it->f, Qt, lface_id,
5440 it->face_id);
5441 }
5442 else if (it->f == last_escape_glyph_frame
5443 && it->face_id == last_escape_glyph_face_id)
5444 {
5445 face_id = last_escape_glyph_merged_face_id;
5446 }
5447 else
5448 {
5449 /* Merge the escape-glyph face into the current face. */
5450 face_id = merge_faces (it->f, Qescape_glyph, 0,
5451 it->face_id);
5452 last_escape_glyph_frame = it->f;
5453 last_escape_glyph_face_id = it->face_id;
5454 last_escape_glyph_merged_face_id = face_id;
5455 }
5456
5457 XSETINT (it->ctl_chars[0], g);
5458 g = it->c ^ 0100;
5459 XSETINT (it->ctl_chars[1], g);
5460 ctl_len = 2;
5461 goto display_control;
5462 }
5463
5464 /* Handle non-break space in the mode where it only gets
5465 highlighting. */
5466
5467 if (EQ (Vnobreak_char_display, Qt)
5468 && (it->c == 0x8a0 || it->c == 0x920
5469 || it->c == 0xe20 || it->c == 0xf20))
5470 {
5471 /* Merge the no-break-space face into the current face. */
5472 face_id = merge_faces (it->f, Qnobreak_space, 0,
5473 it->face_id);
5474
5475 g = it->c = ' ';
5476 XSETINT (it->ctl_chars[0], g);
5477 ctl_len = 1;
5478 goto display_control;
5479 }
5480
5481 /* Handle sequences that start with the "escape glyph". */
5482
5483 /* the default escape glyph is \. */
5484 escape_glyph = '\\';
5485
5486 if (it->dp
5487 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5488 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5489 {
5490 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5491 lface_id = FAST_GLYPH_FACE (escape_glyph);
5492 }
5493 if (lface_id)
5494 {
5495 /* The display table specified a face.
5496 Merge it into face_id and also into escape_glyph. */
5497 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5498 face_id = merge_faces (it->f, Qt, lface_id,
5499 it->face_id);
5500 }
5501 else if (it->f == last_escape_glyph_frame
5502 && it->face_id == last_escape_glyph_face_id)
5503 {
5504 face_id = last_escape_glyph_merged_face_id;
5505 }
5506 else
5507 {
5508 /* Merge the escape-glyph face into the current face. */
5509 face_id = merge_faces (it->f, Qescape_glyph, 0,
5510 it->face_id);
5511 last_escape_glyph_frame = it->f;
5512 last_escape_glyph_face_id = it->face_id;
5513 last_escape_glyph_merged_face_id = face_id;
5514 }
5515
5516 /* Handle soft hyphens in the mode where they only get
5517 highlighting. */
5518
5519 if (EQ (Vnobreak_char_display, Qt)
5520 && (it->c == 0x8ad || it->c == 0x92d
5521 || it->c == 0xe2d || it->c == 0xf2d))
5522 {
5523 g = it->c = '-';
5524 XSETINT (it->ctl_chars[0], g);
5525 ctl_len = 1;
5526 goto display_control;
5527 }
5528
5529 /* Handle non-break space and soft hyphen
5530 with the escape glyph. */
5531
5532 if (it->c == 0x8a0 || it->c == 0x8ad
5533 || it->c == 0x920 || it->c == 0x92d
5534 || it->c == 0xe20 || it->c == 0xe2d
5535 || it->c == 0xf20 || it->c == 0xf2d)
5536 {
5537 XSETINT (it->ctl_chars[0], escape_glyph);
5538 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5539 XSETINT (it->ctl_chars[1], g);
5540 ctl_len = 2;
5541 goto display_control;
5542 }
5543
5544 {
5545 unsigned char str[MAX_MULTIBYTE_LENGTH];
5546 int len;
5547 int i;
5548
5549 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5550 if (SINGLE_BYTE_CHAR_P (it->c))
5551 str[0] = it->c, len = 1;
5552 else
5553 {
5554 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5555 if (len < 0)
5556 {
5557 /* It's an invalid character, which shouldn't
5558 happen actually, but due to bugs it may
5559 happen. Let's print the char as is, there's
5560 not much meaningful we can do with it. */
5561 str[0] = it->c;
5562 str[1] = it->c >> 8;
5563 str[2] = it->c >> 16;
5564 str[3] = it->c >> 24;
5565 len = 4;
5566 }
5567 }
5568
5569 for (i = 0; i < len; i++)
5570 {
5571 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5572 /* Insert three more glyphs into IT->ctl_chars for
5573 the octal display of the character. */
5574 g = ((str[i] >> 6) & 7) + '0';
5575 XSETINT (it->ctl_chars[i * 4 + 1], g);
5576 g = ((str[i] >> 3) & 7) + '0';
5577 XSETINT (it->ctl_chars[i * 4 + 2], g);
5578 g = (str[i] & 7) + '0';
5579 XSETINT (it->ctl_chars[i * 4 + 3], g);
5580 }
5581 ctl_len = len * 4;
5582 }
5583
5584 display_control:
5585 /* Set up IT->dpvec and return first character from it. */
5586 it->dpvec_char_len = it->len;
5587 it->dpvec = it->ctl_chars;
5588 it->dpend = it->dpvec + ctl_len;
5589 it->current.dpvec_index = 0;
5590 it->dpvec_face_id = face_id;
5591 it->saved_face_id = it->face_id;
5592 it->method = GET_FROM_DISPLAY_VECTOR;
5593 it->ellipsis_p = 0;
5594 goto get_next;
5595 }
5596 }
5597
5598 /* Adjust face id for a multibyte character. There are no
5599 multibyte character in unibyte text. */
5600 if (it->multibyte_p
5601 && success_p
5602 && FRAME_WINDOW_P (it->f))
5603 {
5604 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5605 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5606 }
5607 }
5608
5609 /* Is this character the last one of a run of characters with
5610 box? If yes, set IT->end_of_box_run_p to 1. */
5611 if (it->face_box_p
5612 && it->s == NULL)
5613 {
5614 int face_id;
5615 struct face *face;
5616
5617 it->end_of_box_run_p
5618 = ((face_id = face_after_it_pos (it),
5619 face_id != it->face_id)
5620 && (face = FACE_FROM_ID (it->f, face_id),
5621 face->box == FACE_NO_BOX));
5622 }
5623
5624 /* Value is 0 if end of buffer or string reached. */
5625 return success_p;
5626 }
5627
5628
5629 /* Move IT to the next display element.
5630
5631 RESEAT_P non-zero means if called on a newline in buffer text,
5632 skip to the next visible line start.
5633
5634 Functions get_next_display_element and set_iterator_to_next are
5635 separate because I find this arrangement easier to handle than a
5636 get_next_display_element function that also increments IT's
5637 position. The way it is we can first look at an iterator's current
5638 display element, decide whether it fits on a line, and if it does,
5639 increment the iterator position. The other way around we probably
5640 would either need a flag indicating whether the iterator has to be
5641 incremented the next time, or we would have to implement a
5642 decrement position function which would not be easy to write. */
5643
5644 void
5645 set_iterator_to_next (it, reseat_p)
5646 struct it *it;
5647 int reseat_p;
5648 {
5649 /* Reset flags indicating start and end of a sequence of characters
5650 with box. Reset them at the start of this function because
5651 moving the iterator to a new position might set them. */
5652 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5653
5654 switch (it->method)
5655 {
5656 case GET_FROM_BUFFER:
5657 /* The current display element of IT is a character from
5658 current_buffer. Advance in the buffer, and maybe skip over
5659 invisible lines that are so because of selective display. */
5660 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5661 reseat_at_next_visible_line_start (it, 0);
5662 else
5663 {
5664 xassert (it->len != 0);
5665 IT_BYTEPOS (*it) += it->len;
5666 IT_CHARPOS (*it) += 1;
5667 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5668 }
5669 break;
5670
5671 case GET_FROM_COMPOSITION:
5672 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5673 if (STRINGP (it->string))
5674 {
5675 IT_STRING_BYTEPOS (*it) += it->len;
5676 IT_STRING_CHARPOS (*it) += it->cmp_len;
5677 it->method = GET_FROM_STRING;
5678 goto consider_string_end;
5679 }
5680 else
5681 {
5682 IT_BYTEPOS (*it) += it->len;
5683 IT_CHARPOS (*it) += it->cmp_len;
5684 it->method = GET_FROM_BUFFER;
5685 }
5686 break;
5687
5688 case GET_FROM_C_STRING:
5689 /* Current display element of IT is from a C string. */
5690 IT_BYTEPOS (*it) += it->len;
5691 IT_CHARPOS (*it) += 1;
5692 break;
5693
5694 case GET_FROM_DISPLAY_VECTOR:
5695 /* Current display element of IT is from a display table entry.
5696 Advance in the display table definition. Reset it to null if
5697 end reached, and continue with characters from buffers/
5698 strings. */
5699 ++it->current.dpvec_index;
5700
5701 /* Restore face of the iterator to what they were before the
5702 display vector entry (these entries may contain faces). */
5703 it->face_id = it->saved_face_id;
5704
5705 if (it->dpvec + it->current.dpvec_index == it->dpend)
5706 {
5707 int recheck_faces = it->ellipsis_p;
5708
5709 if (it->s)
5710 it->method = GET_FROM_C_STRING;
5711 else if (STRINGP (it->string))
5712 it->method = GET_FROM_STRING;
5713 else
5714 it->method = GET_FROM_BUFFER;
5715
5716 it->dpvec = NULL;
5717 it->current.dpvec_index = -1;
5718
5719 /* Skip over characters which were displayed via IT->dpvec. */
5720 if (it->dpvec_char_len < 0)
5721 reseat_at_next_visible_line_start (it, 1);
5722 else if (it->dpvec_char_len > 0)
5723 {
5724 if (it->method == GET_FROM_STRING
5725 && it->n_overlay_strings > 0)
5726 it->ignore_overlay_strings_at_pos_p = 1;
5727 it->len = it->dpvec_char_len;
5728 set_iterator_to_next (it, reseat_p);
5729 }
5730
5731 /* Maybe recheck faces after display vector */
5732 if (recheck_faces)
5733 it->stop_charpos = IT_CHARPOS (*it);
5734 }
5735 break;
5736
5737 case GET_FROM_STRING:
5738 /* Current display element is a character from a Lisp string. */
5739 xassert (it->s == NULL && STRINGP (it->string));
5740 IT_STRING_BYTEPOS (*it) += it->len;
5741 IT_STRING_CHARPOS (*it) += 1;
5742
5743 consider_string_end:
5744
5745 if (it->current.overlay_string_index >= 0)
5746 {
5747 /* IT->string is an overlay string. Advance to the
5748 next, if there is one. */
5749 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5750 next_overlay_string (it);
5751 }
5752 else
5753 {
5754 /* IT->string is not an overlay string. If we reached
5755 its end, and there is something on IT->stack, proceed
5756 with what is on the stack. This can be either another
5757 string, this time an overlay string, or a buffer. */
5758 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5759 && it->sp > 0)
5760 {
5761 pop_it (it);
5762 if (STRINGP (it->string))
5763 goto consider_string_end;
5764 it->method = GET_FROM_BUFFER;
5765 }
5766 }
5767 break;
5768
5769 case GET_FROM_IMAGE:
5770 case GET_FROM_STRETCH:
5771 /* The position etc with which we have to proceed are on
5772 the stack. The position may be at the end of a string,
5773 if the `display' property takes up the whole string. */
5774 xassert (it->sp > 0);
5775 pop_it (it);
5776 it->image_id = 0;
5777 if (STRINGP (it->string))
5778 {
5779 it->method = GET_FROM_STRING;
5780 goto consider_string_end;
5781 }
5782 it->method = GET_FROM_BUFFER;
5783 break;
5784
5785 default:
5786 /* There are no other methods defined, so this should be a bug. */
5787 abort ();
5788 }
5789
5790 xassert (it->method != GET_FROM_STRING
5791 || (STRINGP (it->string)
5792 && IT_STRING_CHARPOS (*it) >= 0));
5793 }
5794
5795 /* Load IT's display element fields with information about the next
5796 display element which comes from a display table entry or from the
5797 result of translating a control character to one of the forms `^C'
5798 or `\003'.
5799
5800 IT->dpvec holds the glyphs to return as characters.
5801 IT->saved_face_id holds the face id before the display vector--
5802 it is restored into IT->face_idin set_iterator_to_next. */
5803
5804 static int
5805 next_element_from_display_vector (it)
5806 struct it *it;
5807 {
5808 /* Precondition. */
5809 xassert (it->dpvec && it->current.dpvec_index >= 0);
5810
5811 it->face_id = it->saved_face_id;
5812
5813 if (INTEGERP (*it->dpvec)
5814 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5815 {
5816 GLYPH g;
5817
5818 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5819 it->c = FAST_GLYPH_CHAR (g);
5820 it->len = CHAR_BYTES (it->c);
5821
5822 /* The entry may contain a face id to use. Such a face id is
5823 the id of a Lisp face, not a realized face. A face id of
5824 zero means no face is specified. */
5825 if (it->dpvec_face_id >= 0)
5826 it->face_id = it->dpvec_face_id;
5827 else
5828 {
5829 int lface_id = FAST_GLYPH_FACE (g);
5830 if (lface_id > 0)
5831 it->face_id = merge_faces (it->f, Qt, lface_id,
5832 it->saved_face_id);
5833 }
5834 }
5835 else
5836 /* Display table entry is invalid. Return a space. */
5837 it->c = ' ', it->len = 1;
5838
5839 /* Don't change position and object of the iterator here. They are
5840 still the values of the character that had this display table
5841 entry or was translated, and that's what we want. */
5842 it->what = IT_CHARACTER;
5843 return 1;
5844 }
5845
5846
5847 /* Load IT with the next display element from Lisp string IT->string.
5848 IT->current.string_pos is the current position within the string.
5849 If IT->current.overlay_string_index >= 0, the Lisp string is an
5850 overlay string. */
5851
5852 static int
5853 next_element_from_string (it)
5854 struct it *it;
5855 {
5856 struct text_pos position;
5857
5858 xassert (STRINGP (it->string));
5859 xassert (IT_STRING_CHARPOS (*it) >= 0);
5860 position = it->current.string_pos;
5861
5862 /* Time to check for invisible text? */
5863 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5864 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5865 {
5866 handle_stop (it);
5867
5868 /* Since a handler may have changed IT->method, we must
5869 recurse here. */
5870 return get_next_display_element (it);
5871 }
5872
5873 if (it->current.overlay_string_index >= 0)
5874 {
5875 /* Get the next character from an overlay string. In overlay
5876 strings, There is no field width or padding with spaces to
5877 do. */
5878 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5879 {
5880 it->what = IT_EOB;
5881 return 0;
5882 }
5883 else if (STRING_MULTIBYTE (it->string))
5884 {
5885 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5886 const unsigned char *s = (SDATA (it->string)
5887 + IT_STRING_BYTEPOS (*it));
5888 it->c = string_char_and_length (s, remaining, &it->len);
5889 }
5890 else
5891 {
5892 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5893 it->len = 1;
5894 }
5895 }
5896 else
5897 {
5898 /* Get the next character from a Lisp string that is not an
5899 overlay string. Such strings come from the mode line, for
5900 example. We may have to pad with spaces, or truncate the
5901 string. See also next_element_from_c_string. */
5902 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5903 {
5904 it->what = IT_EOB;
5905 return 0;
5906 }
5907 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5908 {
5909 /* Pad with spaces. */
5910 it->c = ' ', it->len = 1;
5911 CHARPOS (position) = BYTEPOS (position) = -1;
5912 }
5913 else if (STRING_MULTIBYTE (it->string))
5914 {
5915 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5916 const unsigned char *s = (SDATA (it->string)
5917 + IT_STRING_BYTEPOS (*it));
5918 it->c = string_char_and_length (s, maxlen, &it->len);
5919 }
5920 else
5921 {
5922 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5923 it->len = 1;
5924 }
5925 }
5926
5927 /* Record what we have and where it came from. Note that we store a
5928 buffer position in IT->position although it could arguably be a
5929 string position. */
5930 it->what = IT_CHARACTER;
5931 it->object = it->string;
5932 it->position = position;
5933 return 1;
5934 }
5935
5936
5937 /* Load IT with next display element from C string IT->s.
5938 IT->string_nchars is the maximum number of characters to return
5939 from the string. IT->end_charpos may be greater than
5940 IT->string_nchars when this function is called, in which case we
5941 may have to return padding spaces. Value is zero if end of string
5942 reached, including padding spaces. */
5943
5944 static int
5945 next_element_from_c_string (it)
5946 struct it *it;
5947 {
5948 int success_p = 1;
5949
5950 xassert (it->s);
5951 it->what = IT_CHARACTER;
5952 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5953 it->object = Qnil;
5954
5955 /* IT's position can be greater IT->string_nchars in case a field
5956 width or precision has been specified when the iterator was
5957 initialized. */
5958 if (IT_CHARPOS (*it) >= it->end_charpos)
5959 {
5960 /* End of the game. */
5961 it->what = IT_EOB;
5962 success_p = 0;
5963 }
5964 else if (IT_CHARPOS (*it) >= it->string_nchars)
5965 {
5966 /* Pad with spaces. */
5967 it->c = ' ', it->len = 1;
5968 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5969 }
5970 else if (it->multibyte_p)
5971 {
5972 /* Implementation note: The calls to strlen apparently aren't a
5973 performance problem because there is no noticeable performance
5974 difference between Emacs running in unibyte or multibyte mode. */
5975 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5976 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5977 maxlen, &it->len);
5978 }
5979 else
5980 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5981
5982 return success_p;
5983 }
5984
5985
5986 /* Set up IT to return characters from an ellipsis, if appropriate.
5987 The definition of the ellipsis glyphs may come from a display table
5988 entry. This function Fills IT with the first glyph from the
5989 ellipsis if an ellipsis is to be displayed. */
5990
5991 static int
5992 next_element_from_ellipsis (it)
5993 struct it *it;
5994 {
5995 if (it->selective_display_ellipsis_p)
5996 setup_for_ellipsis (it, it->len);
5997 else
5998 {
5999 /* The face at the current position may be different from the
6000 face we find after the invisible text. Remember what it
6001 was in IT->saved_face_id, and signal that it's there by
6002 setting face_before_selective_p. */
6003 it->saved_face_id = it->face_id;
6004 it->method = GET_FROM_BUFFER;
6005 reseat_at_next_visible_line_start (it, 1);
6006 it->face_before_selective_p = 1;
6007 }
6008
6009 return get_next_display_element (it);
6010 }
6011
6012
6013 /* Deliver an image display element. The iterator IT is already
6014 filled with image information (done in handle_display_prop). Value
6015 is always 1. */
6016
6017
6018 static int
6019 next_element_from_image (it)
6020 struct it *it;
6021 {
6022 it->what = IT_IMAGE;
6023 return 1;
6024 }
6025
6026
6027 /* Fill iterator IT with next display element from a stretch glyph
6028 property. IT->object is the value of the text property. Value is
6029 always 1. */
6030
6031 static int
6032 next_element_from_stretch (it)
6033 struct it *it;
6034 {
6035 it->what = IT_STRETCH;
6036 return 1;
6037 }
6038
6039
6040 /* Load IT with the next display element from current_buffer. Value
6041 is zero if end of buffer reached. IT->stop_charpos is the next
6042 position at which to stop and check for text properties or buffer
6043 end. */
6044
6045 static int
6046 next_element_from_buffer (it)
6047 struct it *it;
6048 {
6049 int success_p = 1;
6050
6051 /* Check this assumption, otherwise, we would never enter the
6052 if-statement, below. */
6053 xassert (IT_CHARPOS (*it) >= BEGV
6054 && IT_CHARPOS (*it) <= it->stop_charpos);
6055
6056 if (IT_CHARPOS (*it) >= it->stop_charpos)
6057 {
6058 if (IT_CHARPOS (*it) >= it->end_charpos)
6059 {
6060 int overlay_strings_follow_p;
6061
6062 /* End of the game, except when overlay strings follow that
6063 haven't been returned yet. */
6064 if (it->overlay_strings_at_end_processed_p)
6065 overlay_strings_follow_p = 0;
6066 else
6067 {
6068 it->overlay_strings_at_end_processed_p = 1;
6069 overlay_strings_follow_p = get_overlay_strings (it, 0);
6070 }
6071
6072 if (overlay_strings_follow_p)
6073 success_p = get_next_display_element (it);
6074 else
6075 {
6076 it->what = IT_EOB;
6077 it->position = it->current.pos;
6078 success_p = 0;
6079 }
6080 }
6081 else
6082 {
6083 handle_stop (it);
6084 return get_next_display_element (it);
6085 }
6086 }
6087 else
6088 {
6089 /* No face changes, overlays etc. in sight, so just return a
6090 character from current_buffer. */
6091 unsigned char *p;
6092
6093 /* Maybe run the redisplay end trigger hook. Performance note:
6094 This doesn't seem to cost measurable time. */
6095 if (it->redisplay_end_trigger_charpos
6096 && it->glyph_row
6097 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6098 run_redisplay_end_trigger_hook (it);
6099
6100 /* Get the next character, maybe multibyte. */
6101 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6102 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6103 {
6104 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6105 - IT_BYTEPOS (*it));
6106 it->c = string_char_and_length (p, maxlen, &it->len);
6107 }
6108 else
6109 it->c = *p, it->len = 1;
6110
6111 /* Record what we have and where it came from. */
6112 it->what = IT_CHARACTER;;
6113 it->object = it->w->buffer;
6114 it->position = it->current.pos;
6115
6116 /* Normally we return the character found above, except when we
6117 really want to return an ellipsis for selective display. */
6118 if (it->selective)
6119 {
6120 if (it->c == '\n')
6121 {
6122 /* A value of selective > 0 means hide lines indented more
6123 than that number of columns. */
6124 if (it->selective > 0
6125 && IT_CHARPOS (*it) + 1 < ZV
6126 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6127 IT_BYTEPOS (*it) + 1,
6128 (double) it->selective)) /* iftc */
6129 {
6130 success_p = next_element_from_ellipsis (it);
6131 it->dpvec_char_len = -1;
6132 }
6133 }
6134 else if (it->c == '\r' && it->selective == -1)
6135 {
6136 /* A value of selective == -1 means that everything from the
6137 CR to the end of the line is invisible, with maybe an
6138 ellipsis displayed for it. */
6139 success_p = next_element_from_ellipsis (it);
6140 it->dpvec_char_len = -1;
6141 }
6142 }
6143 }
6144
6145 /* Value is zero if end of buffer reached. */
6146 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6147 return success_p;
6148 }
6149
6150
6151 /* Run the redisplay end trigger hook for IT. */
6152
6153 static void
6154 run_redisplay_end_trigger_hook (it)
6155 struct it *it;
6156 {
6157 Lisp_Object args[3];
6158
6159 /* IT->glyph_row should be non-null, i.e. we should be actually
6160 displaying something, or otherwise we should not run the hook. */
6161 xassert (it->glyph_row);
6162
6163 /* Set up hook arguments. */
6164 args[0] = Qredisplay_end_trigger_functions;
6165 args[1] = it->window;
6166 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6167 it->redisplay_end_trigger_charpos = 0;
6168
6169 /* Since we are *trying* to run these functions, don't try to run
6170 them again, even if they get an error. */
6171 it->w->redisplay_end_trigger = Qnil;
6172 Frun_hook_with_args (3, args);
6173
6174 /* Notice if it changed the face of the character we are on. */
6175 handle_face_prop (it);
6176 }
6177
6178
6179 /* Deliver a composition display element. The iterator IT is already
6180 filled with composition information (done in
6181 handle_composition_prop). Value is always 1. */
6182
6183 static int
6184 next_element_from_composition (it)
6185 struct it *it;
6186 {
6187 it->what = IT_COMPOSITION;
6188 it->position = (STRINGP (it->string)
6189 ? it->current.string_pos
6190 : it->current.pos);
6191 return 1;
6192 }
6193
6194
6195 \f
6196 /***********************************************************************
6197 Moving an iterator without producing glyphs
6198 ***********************************************************************/
6199
6200 /* Check if iterator is at a position corresponding to a valid buffer
6201 position after some move_it_ call. */
6202
6203 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6204 ((it)->method == GET_FROM_STRING \
6205 ? IT_STRING_CHARPOS (*it) == 0 \
6206 : 1)
6207
6208
6209 /* Move iterator IT to a specified buffer or X position within one
6210 line on the display without producing glyphs.
6211
6212 OP should be a bit mask including some or all of these bits:
6213 MOVE_TO_X: Stop on reaching x-position TO_X.
6214 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6215 Regardless of OP's value, stop in reaching the end of the display line.
6216
6217 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6218 This means, in particular, that TO_X includes window's horizontal
6219 scroll amount.
6220
6221 The return value has several possible values that
6222 say what condition caused the scan to stop:
6223
6224 MOVE_POS_MATCH_OR_ZV
6225 - when TO_POS or ZV was reached.
6226
6227 MOVE_X_REACHED
6228 -when TO_X was reached before TO_POS or ZV were reached.
6229
6230 MOVE_LINE_CONTINUED
6231 - when we reached the end of the display area and the line must
6232 be continued.
6233
6234 MOVE_LINE_TRUNCATED
6235 - when we reached the end of the display area and the line is
6236 truncated.
6237
6238 MOVE_NEWLINE_OR_CR
6239 - when we stopped at a line end, i.e. a newline or a CR and selective
6240 display is on. */
6241
6242 static enum move_it_result
6243 move_it_in_display_line_to (it, to_charpos, to_x, op)
6244 struct it *it;
6245 int to_charpos, to_x, op;
6246 {
6247 enum move_it_result result = MOVE_UNDEFINED;
6248 struct glyph_row *saved_glyph_row;
6249
6250 /* Don't produce glyphs in produce_glyphs. */
6251 saved_glyph_row = it->glyph_row;
6252 it->glyph_row = NULL;
6253
6254 #define BUFFER_POS_REACHED_P() \
6255 ((op & MOVE_TO_POS) != 0 \
6256 && BUFFERP (it->object) \
6257 && IT_CHARPOS (*it) >= to_charpos \
6258 && (it->method == GET_FROM_BUFFER \
6259 || (it->method == GET_FROM_DISPLAY_VECTOR \
6260 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6261
6262
6263 while (1)
6264 {
6265 int x, i, ascent = 0, descent = 0;
6266
6267 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6268 if ((op & MOVE_TO_POS) != 0
6269 && BUFFERP (it->object)
6270 && it->method == GET_FROM_BUFFER
6271 && IT_CHARPOS (*it) > to_charpos)
6272 {
6273 result = MOVE_POS_MATCH_OR_ZV;
6274 break;
6275 }
6276
6277 /* Stop when ZV reached.
6278 We used to stop here when TO_CHARPOS reached as well, but that is
6279 too soon if this glyph does not fit on this line. So we handle it
6280 explicitly below. */
6281 if (!get_next_display_element (it)
6282 || (it->truncate_lines_p
6283 && BUFFER_POS_REACHED_P ()))
6284 {
6285 result = MOVE_POS_MATCH_OR_ZV;
6286 break;
6287 }
6288
6289 /* The call to produce_glyphs will get the metrics of the
6290 display element IT is loaded with. We record in x the
6291 x-position before this display element in case it does not
6292 fit on the line. */
6293 x = it->current_x;
6294
6295 /* Remember the line height so far in case the next element doesn't
6296 fit on the line. */
6297 if (!it->truncate_lines_p)
6298 {
6299 ascent = it->max_ascent;
6300 descent = it->max_descent;
6301 }
6302
6303 PRODUCE_GLYPHS (it);
6304
6305 if (it->area != TEXT_AREA)
6306 {
6307 set_iterator_to_next (it, 1);
6308 continue;
6309 }
6310
6311 /* The number of glyphs we get back in IT->nglyphs will normally
6312 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6313 character on a terminal frame, or (iii) a line end. For the
6314 second case, IT->nglyphs - 1 padding glyphs will be present
6315 (on X frames, there is only one glyph produced for a
6316 composite character.
6317
6318 The behavior implemented below means, for continuation lines,
6319 that as many spaces of a TAB as fit on the current line are
6320 displayed there. For terminal frames, as many glyphs of a
6321 multi-glyph character are displayed in the current line, too.
6322 This is what the old redisplay code did, and we keep it that
6323 way. Under X, the whole shape of a complex character must
6324 fit on the line or it will be completely displayed in the
6325 next line.
6326
6327 Note that both for tabs and padding glyphs, all glyphs have
6328 the same width. */
6329 if (it->nglyphs)
6330 {
6331 /* More than one glyph or glyph doesn't fit on line. All
6332 glyphs have the same width. */
6333 int single_glyph_width = it->pixel_width / it->nglyphs;
6334 int new_x;
6335 int x_before_this_char = x;
6336 int hpos_before_this_char = it->hpos;
6337
6338 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6339 {
6340 new_x = x + single_glyph_width;
6341
6342 /* We want to leave anything reaching TO_X to the caller. */
6343 if ((op & MOVE_TO_X) && new_x > to_x)
6344 {
6345 if (BUFFER_POS_REACHED_P ())
6346 goto buffer_pos_reached;
6347 it->current_x = x;
6348 result = MOVE_X_REACHED;
6349 break;
6350 }
6351 else if (/* Lines are continued. */
6352 !it->truncate_lines_p
6353 && (/* And glyph doesn't fit on the line. */
6354 new_x > it->last_visible_x
6355 /* Or it fits exactly and we're on a window
6356 system frame. */
6357 || (new_x == it->last_visible_x
6358 && FRAME_WINDOW_P (it->f))))
6359 {
6360 if (/* IT->hpos == 0 means the very first glyph
6361 doesn't fit on the line, e.g. a wide image. */
6362 it->hpos == 0
6363 || (new_x == it->last_visible_x
6364 && FRAME_WINDOW_P (it->f)))
6365 {
6366 ++it->hpos;
6367 it->current_x = new_x;
6368
6369 /* The character's last glyph just barely fits
6370 in this row. */
6371 if (i == it->nglyphs - 1)
6372 {
6373 /* If this is the destination position,
6374 return a position *before* it in this row,
6375 now that we know it fits in this row. */
6376 if (BUFFER_POS_REACHED_P ())
6377 {
6378 it->hpos = hpos_before_this_char;
6379 it->current_x = x_before_this_char;
6380 result = MOVE_POS_MATCH_OR_ZV;
6381 break;
6382 }
6383
6384 set_iterator_to_next (it, 1);
6385 #ifdef HAVE_WINDOW_SYSTEM
6386 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6387 {
6388 if (!get_next_display_element (it))
6389 {
6390 result = MOVE_POS_MATCH_OR_ZV;
6391 break;
6392 }
6393 if (BUFFER_POS_REACHED_P ())
6394 {
6395 if (ITERATOR_AT_END_OF_LINE_P (it))
6396 result = MOVE_POS_MATCH_OR_ZV;
6397 else
6398 result = MOVE_LINE_CONTINUED;
6399 break;
6400 }
6401 if (ITERATOR_AT_END_OF_LINE_P (it))
6402 {
6403 result = MOVE_NEWLINE_OR_CR;
6404 break;
6405 }
6406 }
6407 #endif /* HAVE_WINDOW_SYSTEM */
6408 }
6409 }
6410 else
6411 {
6412 it->current_x = x;
6413 it->max_ascent = ascent;
6414 it->max_descent = descent;
6415 }
6416
6417 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6418 IT_CHARPOS (*it)));
6419 result = MOVE_LINE_CONTINUED;
6420 break;
6421 }
6422 else if (BUFFER_POS_REACHED_P ())
6423 goto buffer_pos_reached;
6424 else if (new_x > it->first_visible_x)
6425 {
6426 /* Glyph is visible. Increment number of glyphs that
6427 would be displayed. */
6428 ++it->hpos;
6429 }
6430 else
6431 {
6432 /* Glyph is completely off the left margin of the display
6433 area. Nothing to do. */
6434 }
6435 }
6436
6437 if (result != MOVE_UNDEFINED)
6438 break;
6439 }
6440 else if (BUFFER_POS_REACHED_P ())
6441 {
6442 buffer_pos_reached:
6443 it->current_x = x;
6444 it->max_ascent = ascent;
6445 it->max_descent = descent;
6446 result = MOVE_POS_MATCH_OR_ZV;
6447 break;
6448 }
6449 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6450 {
6451 /* Stop when TO_X specified and reached. This check is
6452 necessary here because of lines consisting of a line end,
6453 only. The line end will not produce any glyphs and we
6454 would never get MOVE_X_REACHED. */
6455 xassert (it->nglyphs == 0);
6456 result = MOVE_X_REACHED;
6457 break;
6458 }
6459
6460 /* Is this a line end? If yes, we're done. */
6461 if (ITERATOR_AT_END_OF_LINE_P (it))
6462 {
6463 result = MOVE_NEWLINE_OR_CR;
6464 break;
6465 }
6466
6467 /* The current display element has been consumed. Advance
6468 to the next. */
6469 set_iterator_to_next (it, 1);
6470
6471 /* Stop if lines are truncated and IT's current x-position is
6472 past the right edge of the window now. */
6473 if (it->truncate_lines_p
6474 && it->current_x >= it->last_visible_x)
6475 {
6476 #ifdef HAVE_WINDOW_SYSTEM
6477 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6478 {
6479 if (!get_next_display_element (it)
6480 || BUFFER_POS_REACHED_P ())
6481 {
6482 result = MOVE_POS_MATCH_OR_ZV;
6483 break;
6484 }
6485 if (ITERATOR_AT_END_OF_LINE_P (it))
6486 {
6487 result = MOVE_NEWLINE_OR_CR;
6488 break;
6489 }
6490 }
6491 #endif /* HAVE_WINDOW_SYSTEM */
6492 result = MOVE_LINE_TRUNCATED;
6493 break;
6494 }
6495 }
6496
6497 #undef BUFFER_POS_REACHED_P
6498
6499 /* Restore the iterator settings altered at the beginning of this
6500 function. */
6501 it->glyph_row = saved_glyph_row;
6502 return result;
6503 }
6504
6505
6506 /* Move IT forward until it satisfies one or more of the criteria in
6507 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6508
6509 OP is a bit-mask that specifies where to stop, and in particular,
6510 which of those four position arguments makes a difference. See the
6511 description of enum move_operation_enum.
6512
6513 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6514 screen line, this function will set IT to the next position >
6515 TO_CHARPOS. */
6516
6517 void
6518 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6519 struct it *it;
6520 int to_charpos, to_x, to_y, to_vpos;
6521 int op;
6522 {
6523 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6524 int line_height;
6525 int reached = 0;
6526
6527 for (;;)
6528 {
6529 if (op & MOVE_TO_VPOS)
6530 {
6531 /* If no TO_CHARPOS and no TO_X specified, stop at the
6532 start of the line TO_VPOS. */
6533 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6534 {
6535 if (it->vpos == to_vpos)
6536 {
6537 reached = 1;
6538 break;
6539 }
6540 else
6541 skip = move_it_in_display_line_to (it, -1, -1, 0);
6542 }
6543 else
6544 {
6545 /* TO_VPOS >= 0 means stop at TO_X in the line at
6546 TO_VPOS, or at TO_POS, whichever comes first. */
6547 if (it->vpos == to_vpos)
6548 {
6549 reached = 2;
6550 break;
6551 }
6552
6553 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6554
6555 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6556 {
6557 reached = 3;
6558 break;
6559 }
6560 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6561 {
6562 /* We have reached TO_X but not in the line we want. */
6563 skip = move_it_in_display_line_to (it, to_charpos,
6564 -1, MOVE_TO_POS);
6565 if (skip == MOVE_POS_MATCH_OR_ZV)
6566 {
6567 reached = 4;
6568 break;
6569 }
6570 }
6571 }
6572 }
6573 else if (op & MOVE_TO_Y)
6574 {
6575 struct it it_backup;
6576
6577 /* TO_Y specified means stop at TO_X in the line containing
6578 TO_Y---or at TO_CHARPOS if this is reached first. The
6579 problem is that we can't really tell whether the line
6580 contains TO_Y before we have completely scanned it, and
6581 this may skip past TO_X. What we do is to first scan to
6582 TO_X.
6583
6584 If TO_X is not specified, use a TO_X of zero. The reason
6585 is to make the outcome of this function more predictable.
6586 If we didn't use TO_X == 0, we would stop at the end of
6587 the line which is probably not what a caller would expect
6588 to happen. */
6589 skip = move_it_in_display_line_to (it, to_charpos,
6590 ((op & MOVE_TO_X)
6591 ? to_x : 0),
6592 (MOVE_TO_X
6593 | (op & MOVE_TO_POS)));
6594
6595 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6596 if (skip == MOVE_POS_MATCH_OR_ZV)
6597 {
6598 reached = 5;
6599 break;
6600 }
6601
6602 /* If TO_X was reached, we would like to know whether TO_Y
6603 is in the line. This can only be said if we know the
6604 total line height which requires us to scan the rest of
6605 the line. */
6606 if (skip == MOVE_X_REACHED)
6607 {
6608 it_backup = *it;
6609 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6610 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6611 op & MOVE_TO_POS);
6612 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6613 }
6614
6615 /* Now, decide whether TO_Y is in this line. */
6616 line_height = it->max_ascent + it->max_descent;
6617 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6618
6619 if (to_y >= it->current_y
6620 && to_y < it->current_y + line_height)
6621 {
6622 if (skip == MOVE_X_REACHED)
6623 /* If TO_Y is in this line and TO_X was reached above,
6624 we scanned too far. We have to restore IT's settings
6625 to the ones before skipping. */
6626 *it = it_backup;
6627 reached = 6;
6628 }
6629 else if (skip == MOVE_X_REACHED)
6630 {
6631 skip = skip2;
6632 if (skip == MOVE_POS_MATCH_OR_ZV)
6633 reached = 7;
6634 }
6635
6636 if (reached)
6637 break;
6638 }
6639 else
6640 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6641
6642 switch (skip)
6643 {
6644 case MOVE_POS_MATCH_OR_ZV:
6645 reached = 8;
6646 goto out;
6647
6648 case MOVE_NEWLINE_OR_CR:
6649 set_iterator_to_next (it, 1);
6650 it->continuation_lines_width = 0;
6651 break;
6652
6653 case MOVE_LINE_TRUNCATED:
6654 it->continuation_lines_width = 0;
6655 reseat_at_next_visible_line_start (it, 0);
6656 if ((op & MOVE_TO_POS) != 0
6657 && IT_CHARPOS (*it) > to_charpos)
6658 {
6659 reached = 9;
6660 goto out;
6661 }
6662 break;
6663
6664 case MOVE_LINE_CONTINUED:
6665 it->continuation_lines_width += it->current_x;
6666 break;
6667
6668 default:
6669 abort ();
6670 }
6671
6672 /* Reset/increment for the next run. */
6673 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6674 it->current_x = it->hpos = 0;
6675 it->current_y += it->max_ascent + it->max_descent;
6676 ++it->vpos;
6677 last_height = it->max_ascent + it->max_descent;
6678 last_max_ascent = it->max_ascent;
6679 it->max_ascent = it->max_descent = 0;
6680 }
6681
6682 out:
6683
6684 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6685 }
6686
6687
6688 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6689
6690 If DY > 0, move IT backward at least that many pixels. DY = 0
6691 means move IT backward to the preceding line start or BEGV. This
6692 function may move over more than DY pixels if IT->current_y - DY
6693 ends up in the middle of a line; in this case IT->current_y will be
6694 set to the top of the line moved to. */
6695
6696 void
6697 move_it_vertically_backward (it, dy)
6698 struct it *it;
6699 int dy;
6700 {
6701 int nlines, h;
6702 struct it it2, it3;
6703 int start_pos;
6704
6705 move_further_back:
6706 xassert (dy >= 0);
6707
6708 start_pos = IT_CHARPOS (*it);
6709
6710 /* Estimate how many newlines we must move back. */
6711 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6712
6713 /* Set the iterator's position that many lines back. */
6714 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6715 back_to_previous_visible_line_start (it);
6716
6717 /* Reseat the iterator here. When moving backward, we don't want
6718 reseat to skip forward over invisible text, set up the iterator
6719 to deliver from overlay strings at the new position etc. So,
6720 use reseat_1 here. */
6721 reseat_1 (it, it->current.pos, 1);
6722
6723 /* We are now surely at a line start. */
6724 it->current_x = it->hpos = 0;
6725 it->continuation_lines_width = 0;
6726
6727 /* Move forward and see what y-distance we moved. First move to the
6728 start of the next line so that we get its height. We need this
6729 height to be able to tell whether we reached the specified
6730 y-distance. */
6731 it2 = *it;
6732 it2.max_ascent = it2.max_descent = 0;
6733 do
6734 {
6735 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6736 MOVE_TO_POS | MOVE_TO_VPOS);
6737 }
6738 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6739 xassert (IT_CHARPOS (*it) >= BEGV);
6740 it3 = it2;
6741
6742 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6743 xassert (IT_CHARPOS (*it) >= BEGV);
6744 /* H is the actual vertical distance from the position in *IT
6745 and the starting position. */
6746 h = it2.current_y - it->current_y;
6747 /* NLINES is the distance in number of lines. */
6748 nlines = it2.vpos - it->vpos;
6749
6750 /* Correct IT's y and vpos position
6751 so that they are relative to the starting point. */
6752 it->vpos -= nlines;
6753 it->current_y -= h;
6754
6755 if (dy == 0)
6756 {
6757 /* DY == 0 means move to the start of the screen line. The
6758 value of nlines is > 0 if continuation lines were involved. */
6759 if (nlines > 0)
6760 move_it_by_lines (it, nlines, 1);
6761 #if 0
6762 /* I think this assert is bogus if buffer contains
6763 invisible text or images. KFS. */
6764 xassert (IT_CHARPOS (*it) <= start_pos);
6765 #endif
6766 }
6767 else
6768 {
6769 /* The y-position we try to reach, relative to *IT.
6770 Note that H has been subtracted in front of the if-statement. */
6771 int target_y = it->current_y + h - dy;
6772 int y0 = it3.current_y;
6773 int y1 = line_bottom_y (&it3);
6774 int line_height = y1 - y0;
6775
6776 /* If we did not reach target_y, try to move further backward if
6777 we can. If we moved too far backward, try to move forward. */
6778 if (target_y < it->current_y
6779 /* This is heuristic. In a window that's 3 lines high, with
6780 a line height of 13 pixels each, recentering with point
6781 on the bottom line will try to move -39/2 = 19 pixels
6782 backward. Try to avoid moving into the first line. */
6783 && (it->current_y - target_y
6784 > min (window_box_height (it->w), line_height * 2 / 3))
6785 && IT_CHARPOS (*it) > BEGV)
6786 {
6787 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6788 target_y - it->current_y));
6789 dy = it->current_y - target_y;
6790 goto move_further_back;
6791 }
6792 else if (target_y >= it->current_y + line_height
6793 && IT_CHARPOS (*it) < ZV)
6794 {
6795 /* Should move forward by at least one line, maybe more.
6796
6797 Note: Calling move_it_by_lines can be expensive on
6798 terminal frames, where compute_motion is used (via
6799 vmotion) to do the job, when there are very long lines
6800 and truncate-lines is nil. That's the reason for
6801 treating terminal frames specially here. */
6802
6803 if (!FRAME_WINDOW_P (it->f))
6804 move_it_vertically (it, target_y - (it->current_y + line_height));
6805 else
6806 {
6807 do
6808 {
6809 move_it_by_lines (it, 1, 1);
6810 }
6811 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6812 }
6813
6814 #if 0
6815 /* I think this assert is bogus if buffer contains
6816 invisible text or images. KFS. */
6817 xassert (IT_CHARPOS (*it) >= BEGV);
6818 #endif
6819 }
6820 }
6821 }
6822
6823
6824 /* Move IT by a specified amount of pixel lines DY. DY negative means
6825 move backwards. DY = 0 means move to start of screen line. At the
6826 end, IT will be on the start of a screen line. */
6827
6828 void
6829 move_it_vertically (it, dy)
6830 struct it *it;
6831 int dy;
6832 {
6833 if (dy <= 0)
6834 move_it_vertically_backward (it, -dy);
6835 else
6836 {
6837 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6838 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6839 MOVE_TO_POS | MOVE_TO_Y);
6840 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6841
6842 /* If buffer ends in ZV without a newline, move to the start of
6843 the line to satisfy the post-condition. */
6844 if (IT_CHARPOS (*it) == ZV
6845 && ZV > BEGV
6846 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6847 move_it_by_lines (it, 0, 0);
6848 }
6849 }
6850
6851
6852 /* Move iterator IT past the end of the text line it is in. */
6853
6854 void
6855 move_it_past_eol (it)
6856 struct it *it;
6857 {
6858 enum move_it_result rc;
6859
6860 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6861 if (rc == MOVE_NEWLINE_OR_CR)
6862 set_iterator_to_next (it, 0);
6863 }
6864
6865
6866 #if 0 /* Currently not used. */
6867
6868 /* Return non-zero if some text between buffer positions START_CHARPOS
6869 and END_CHARPOS is invisible. IT->window is the window for text
6870 property lookup. */
6871
6872 static int
6873 invisible_text_between_p (it, start_charpos, end_charpos)
6874 struct it *it;
6875 int start_charpos, end_charpos;
6876 {
6877 Lisp_Object prop, limit;
6878 int invisible_found_p;
6879
6880 xassert (it != NULL && start_charpos <= end_charpos);
6881
6882 /* Is text at START invisible? */
6883 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6884 it->window);
6885 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6886 invisible_found_p = 1;
6887 else
6888 {
6889 limit = Fnext_single_char_property_change (make_number (start_charpos),
6890 Qinvisible, Qnil,
6891 make_number (end_charpos));
6892 invisible_found_p = XFASTINT (limit) < end_charpos;
6893 }
6894
6895 return invisible_found_p;
6896 }
6897
6898 #endif /* 0 */
6899
6900
6901 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6902 negative means move up. DVPOS == 0 means move to the start of the
6903 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6904 NEED_Y_P is zero, IT->current_y will be left unchanged.
6905
6906 Further optimization ideas: If we would know that IT->f doesn't use
6907 a face with proportional font, we could be faster for
6908 truncate-lines nil. */
6909
6910 void
6911 move_it_by_lines (it, dvpos, need_y_p)
6912 struct it *it;
6913 int dvpos, need_y_p;
6914 {
6915 struct position pos;
6916
6917 if (!FRAME_WINDOW_P (it->f))
6918 {
6919 struct text_pos textpos;
6920
6921 /* We can use vmotion on frames without proportional fonts. */
6922 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6923 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6924 reseat (it, textpos, 1);
6925 it->vpos += pos.vpos;
6926 it->current_y += pos.vpos;
6927 }
6928 else if (dvpos == 0)
6929 {
6930 /* DVPOS == 0 means move to the start of the screen line. */
6931 move_it_vertically_backward (it, 0);
6932 xassert (it->current_x == 0 && it->hpos == 0);
6933 /* Let next call to line_bottom_y calculate real line height */
6934 last_height = 0;
6935 }
6936 else if (dvpos > 0)
6937 {
6938 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6939 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6940 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6941 }
6942 else
6943 {
6944 struct it it2;
6945 int start_charpos, i;
6946
6947 /* Start at the beginning of the screen line containing IT's
6948 position. This may actually move vertically backwards,
6949 in case of overlays, so adjust dvpos accordingly. */
6950 dvpos += it->vpos;
6951 move_it_vertically_backward (it, 0);
6952 dvpos -= it->vpos;
6953
6954 /* Go back -DVPOS visible lines and reseat the iterator there. */
6955 start_charpos = IT_CHARPOS (*it);
6956 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6957 back_to_previous_visible_line_start (it);
6958 reseat (it, it->current.pos, 1);
6959
6960 /* Move further back if we end up in a string or an image. */
6961 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6962 {
6963 /* First try to move to start of display line. */
6964 dvpos += it->vpos;
6965 move_it_vertically_backward (it, 0);
6966 dvpos -= it->vpos;
6967 if (IT_POS_VALID_AFTER_MOVE_P (it))
6968 break;
6969 /* If start of line is still in string or image,
6970 move further back. */
6971 back_to_previous_visible_line_start (it);
6972 reseat (it, it->current.pos, 1);
6973 dvpos--;
6974 }
6975
6976 it->current_x = it->hpos = 0;
6977
6978 /* Above call may have moved too far if continuation lines
6979 are involved. Scan forward and see if it did. */
6980 it2 = *it;
6981 it2.vpos = it2.current_y = 0;
6982 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6983 it->vpos -= it2.vpos;
6984 it->current_y -= it2.current_y;
6985 it->current_x = it->hpos = 0;
6986
6987 /* If we moved too far back, move IT some lines forward. */
6988 if (it2.vpos > -dvpos)
6989 {
6990 int delta = it2.vpos + dvpos;
6991 it2 = *it;
6992 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6993 /* Move back again if we got too far ahead. */
6994 if (IT_CHARPOS (*it) >= start_charpos)
6995 *it = it2;
6996 }
6997 }
6998 }
6999
7000 /* Return 1 if IT points into the middle of a display vector. */
7001
7002 int
7003 in_display_vector_p (it)
7004 struct it *it;
7005 {
7006 return (it->method == GET_FROM_DISPLAY_VECTOR
7007 && it->current.dpvec_index > 0
7008 && it->dpvec + it->current.dpvec_index != it->dpend);
7009 }
7010
7011 \f
7012 /***********************************************************************
7013 Messages
7014 ***********************************************************************/
7015
7016
7017 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7018 to *Messages*. */
7019
7020 void
7021 add_to_log (format, arg1, arg2)
7022 char *format;
7023 Lisp_Object arg1, arg2;
7024 {
7025 Lisp_Object args[3];
7026 Lisp_Object msg, fmt;
7027 char *buffer;
7028 int len;
7029 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7030 USE_SAFE_ALLOCA;
7031
7032 /* Do nothing if called asynchronously. Inserting text into
7033 a buffer may call after-change-functions and alike and
7034 that would means running Lisp asynchronously. */
7035 if (handling_signal)
7036 return;
7037
7038 fmt = msg = Qnil;
7039 GCPRO4 (fmt, msg, arg1, arg2);
7040
7041 args[0] = fmt = build_string (format);
7042 args[1] = arg1;
7043 args[2] = arg2;
7044 msg = Fformat (3, args);
7045
7046 len = SBYTES (msg) + 1;
7047 SAFE_ALLOCA (buffer, char *, len);
7048 bcopy (SDATA (msg), buffer, len);
7049
7050 message_dolog (buffer, len - 1, 1, 0);
7051 SAFE_FREE ();
7052
7053 UNGCPRO;
7054 }
7055
7056
7057 /* Output a newline in the *Messages* buffer if "needs" one. */
7058
7059 void
7060 message_log_maybe_newline ()
7061 {
7062 if (message_log_need_newline)
7063 message_dolog ("", 0, 1, 0);
7064 }
7065
7066
7067 /* Add a string M of length NBYTES to the message log, optionally
7068 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7069 nonzero, means interpret the contents of M as multibyte. This
7070 function calls low-level routines in order to bypass text property
7071 hooks, etc. which might not be safe to run.
7072
7073 This may GC (insert may run before/after change hooks),
7074 so the buffer M must NOT point to a Lisp string. */
7075
7076 void
7077 message_dolog (m, nbytes, nlflag, multibyte)
7078 const char *m;
7079 int nbytes, nlflag, multibyte;
7080 {
7081 if (!NILP (Vmemory_full))
7082 return;
7083
7084 if (!NILP (Vmessage_log_max))
7085 {
7086 struct buffer *oldbuf;
7087 Lisp_Object oldpoint, oldbegv, oldzv;
7088 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7089 int point_at_end = 0;
7090 int zv_at_end = 0;
7091 Lisp_Object old_deactivate_mark, tem;
7092 struct gcpro gcpro1;
7093
7094 old_deactivate_mark = Vdeactivate_mark;
7095 oldbuf = current_buffer;
7096 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7097 current_buffer->undo_list = Qt;
7098
7099 oldpoint = message_dolog_marker1;
7100 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7101 oldbegv = message_dolog_marker2;
7102 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7103 oldzv = message_dolog_marker3;
7104 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7105 GCPRO1 (old_deactivate_mark);
7106
7107 if (PT == Z)
7108 point_at_end = 1;
7109 if (ZV == Z)
7110 zv_at_end = 1;
7111
7112 BEGV = BEG;
7113 BEGV_BYTE = BEG_BYTE;
7114 ZV = Z;
7115 ZV_BYTE = Z_BYTE;
7116 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7117
7118 /* Insert the string--maybe converting multibyte to single byte
7119 or vice versa, so that all the text fits the buffer. */
7120 if (multibyte
7121 && NILP (current_buffer->enable_multibyte_characters))
7122 {
7123 int i, c, char_bytes;
7124 unsigned char work[1];
7125
7126 /* Convert a multibyte string to single-byte
7127 for the *Message* buffer. */
7128 for (i = 0; i < nbytes; i += char_bytes)
7129 {
7130 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7131 work[0] = (SINGLE_BYTE_CHAR_P (c)
7132 ? c
7133 : multibyte_char_to_unibyte (c, Qnil));
7134 insert_1_both (work, 1, 1, 1, 0, 0);
7135 }
7136 }
7137 else if (! multibyte
7138 && ! NILP (current_buffer->enable_multibyte_characters))
7139 {
7140 int i, c, char_bytes;
7141 unsigned char *msg = (unsigned char *) m;
7142 unsigned char str[MAX_MULTIBYTE_LENGTH];
7143 /* Convert a single-byte string to multibyte
7144 for the *Message* buffer. */
7145 for (i = 0; i < nbytes; i++)
7146 {
7147 c = unibyte_char_to_multibyte (msg[i]);
7148 char_bytes = CHAR_STRING (c, str);
7149 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7150 }
7151 }
7152 else if (nbytes)
7153 insert_1 (m, nbytes, 1, 0, 0);
7154
7155 if (nlflag)
7156 {
7157 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7158 insert_1 ("\n", 1, 1, 0, 0);
7159
7160 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7161 this_bol = PT;
7162 this_bol_byte = PT_BYTE;
7163
7164 /* See if this line duplicates the previous one.
7165 If so, combine duplicates. */
7166 if (this_bol > BEG)
7167 {
7168 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7169 prev_bol = PT;
7170 prev_bol_byte = PT_BYTE;
7171
7172 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7173 this_bol, this_bol_byte);
7174 if (dup)
7175 {
7176 del_range_both (prev_bol, prev_bol_byte,
7177 this_bol, this_bol_byte, 0);
7178 if (dup > 1)
7179 {
7180 char dupstr[40];
7181 int duplen;
7182
7183 /* If you change this format, don't forget to also
7184 change message_log_check_duplicate. */
7185 sprintf (dupstr, " [%d times]", dup);
7186 duplen = strlen (dupstr);
7187 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7188 insert_1 (dupstr, duplen, 1, 0, 1);
7189 }
7190 }
7191 }
7192
7193 /* If we have more than the desired maximum number of lines
7194 in the *Messages* buffer now, delete the oldest ones.
7195 This is safe because we don't have undo in this buffer. */
7196
7197 if (NATNUMP (Vmessage_log_max))
7198 {
7199 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7200 -XFASTINT (Vmessage_log_max) - 1, 0);
7201 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7202 }
7203 }
7204 BEGV = XMARKER (oldbegv)->charpos;
7205 BEGV_BYTE = marker_byte_position (oldbegv);
7206
7207 if (zv_at_end)
7208 {
7209 ZV = Z;
7210 ZV_BYTE = Z_BYTE;
7211 }
7212 else
7213 {
7214 ZV = XMARKER (oldzv)->charpos;
7215 ZV_BYTE = marker_byte_position (oldzv);
7216 }
7217
7218 if (point_at_end)
7219 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7220 else
7221 /* We can't do Fgoto_char (oldpoint) because it will run some
7222 Lisp code. */
7223 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7224 XMARKER (oldpoint)->bytepos);
7225
7226 UNGCPRO;
7227 unchain_marker (XMARKER (oldpoint));
7228 unchain_marker (XMARKER (oldbegv));
7229 unchain_marker (XMARKER (oldzv));
7230
7231 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7232 set_buffer_internal (oldbuf);
7233 if (NILP (tem))
7234 windows_or_buffers_changed = old_windows_or_buffers_changed;
7235 message_log_need_newline = !nlflag;
7236 Vdeactivate_mark = old_deactivate_mark;
7237 }
7238 }
7239
7240
7241 /* We are at the end of the buffer after just having inserted a newline.
7242 (Note: We depend on the fact we won't be crossing the gap.)
7243 Check to see if the most recent message looks a lot like the previous one.
7244 Return 0 if different, 1 if the new one should just replace it, or a
7245 value N > 1 if we should also append " [N times]". */
7246
7247 static int
7248 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7249 int prev_bol, this_bol;
7250 int prev_bol_byte, this_bol_byte;
7251 {
7252 int i;
7253 int len = Z_BYTE - 1 - this_bol_byte;
7254 int seen_dots = 0;
7255 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7256 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7257
7258 for (i = 0; i < len; i++)
7259 {
7260 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7261 seen_dots = 1;
7262 if (p1[i] != p2[i])
7263 return seen_dots;
7264 }
7265 p1 += len;
7266 if (*p1 == '\n')
7267 return 2;
7268 if (*p1++ == ' ' && *p1++ == '[')
7269 {
7270 int n = 0;
7271 while (*p1 >= '0' && *p1 <= '9')
7272 n = n * 10 + *p1++ - '0';
7273 if (strncmp (p1, " times]\n", 8) == 0)
7274 return n+1;
7275 }
7276 return 0;
7277 }
7278 \f
7279
7280 /* Display an echo area message M with a specified length of NBYTES
7281 bytes. The string may include null characters. If M is 0, clear
7282 out any existing message, and let the mini-buffer text show
7283 through.
7284
7285 This may GC, so the buffer M must NOT point to a Lisp string. */
7286
7287 void
7288 message2 (m, nbytes, multibyte)
7289 const char *m;
7290 int nbytes;
7291 int multibyte;
7292 {
7293 /* First flush out any partial line written with print. */
7294 message_log_maybe_newline ();
7295 if (m)
7296 message_dolog (m, nbytes, 1, multibyte);
7297 message2_nolog (m, nbytes, multibyte);
7298 }
7299
7300
7301 /* The non-logging counterpart of message2. */
7302
7303 void
7304 message2_nolog (m, nbytes, multibyte)
7305 const char *m;
7306 int nbytes, multibyte;
7307 {
7308 struct frame *sf = SELECTED_FRAME ();
7309 message_enable_multibyte = multibyte;
7310
7311 if (noninteractive)
7312 {
7313 if (noninteractive_need_newline)
7314 putc ('\n', stderr);
7315 noninteractive_need_newline = 0;
7316 if (m)
7317 fwrite (m, nbytes, 1, stderr);
7318 if (cursor_in_echo_area == 0)
7319 fprintf (stderr, "\n");
7320 fflush (stderr);
7321 }
7322 /* A null message buffer means that the frame hasn't really been
7323 initialized yet. Error messages get reported properly by
7324 cmd_error, so this must be just an informative message; toss it. */
7325 else if (INTERACTIVE
7326 && sf->glyphs_initialized_p
7327 && FRAME_MESSAGE_BUF (sf))
7328 {
7329 Lisp_Object mini_window;
7330 struct frame *f;
7331
7332 /* Get the frame containing the mini-buffer
7333 that the selected frame is using. */
7334 mini_window = FRAME_MINIBUF_WINDOW (sf);
7335 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7336
7337 FRAME_SAMPLE_VISIBILITY (f);
7338 if (FRAME_VISIBLE_P (sf)
7339 && ! FRAME_VISIBLE_P (f))
7340 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7341
7342 if (m)
7343 {
7344 set_message (m, Qnil, nbytes, multibyte);
7345 if (minibuffer_auto_raise)
7346 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7347 }
7348 else
7349 clear_message (1, 1);
7350
7351 do_pending_window_change (0);
7352 echo_area_display (1);
7353 do_pending_window_change (0);
7354 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7355 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7356 }
7357 }
7358
7359
7360 /* Display an echo area message M with a specified length of NBYTES
7361 bytes. The string may include null characters. If M is not a
7362 string, clear out any existing message, and let the mini-buffer
7363 text show through.
7364
7365 This function cancels echoing. */
7366
7367 void
7368 message3 (m, nbytes, multibyte)
7369 Lisp_Object m;
7370 int nbytes;
7371 int multibyte;
7372 {
7373 struct gcpro gcpro1;
7374
7375 GCPRO1 (m);
7376 clear_message (1,1);
7377 cancel_echoing ();
7378
7379 /* First flush out any partial line written with print. */
7380 message_log_maybe_newline ();
7381 if (STRINGP (m))
7382 {
7383 char *buffer;
7384 USE_SAFE_ALLOCA;
7385
7386 SAFE_ALLOCA (buffer, char *, nbytes);
7387 bcopy (SDATA (m), buffer, nbytes);
7388 message_dolog (buffer, nbytes, 1, multibyte);
7389 SAFE_FREE ();
7390 }
7391 message3_nolog (m, nbytes, multibyte);
7392
7393 UNGCPRO;
7394 }
7395
7396
7397 /* The non-logging version of message3.
7398 This does not cancel echoing, because it is used for echoing.
7399 Perhaps we need to make a separate function for echoing
7400 and make this cancel echoing. */
7401
7402 void
7403 message3_nolog (m, nbytes, multibyte)
7404 Lisp_Object m;
7405 int nbytes, multibyte;
7406 {
7407 struct frame *sf = SELECTED_FRAME ();
7408 message_enable_multibyte = multibyte;
7409
7410 if (noninteractive)
7411 {
7412 if (noninteractive_need_newline)
7413 putc ('\n', stderr);
7414 noninteractive_need_newline = 0;
7415 if (STRINGP (m))
7416 fwrite (SDATA (m), nbytes, 1, stderr);
7417 if (cursor_in_echo_area == 0)
7418 fprintf (stderr, "\n");
7419 fflush (stderr);
7420 }
7421 /* A null message buffer means that the frame hasn't really been
7422 initialized yet. Error messages get reported properly by
7423 cmd_error, so this must be just an informative message; toss it. */
7424 else if (INTERACTIVE
7425 && sf->glyphs_initialized_p
7426 && FRAME_MESSAGE_BUF (sf))
7427 {
7428 Lisp_Object mini_window;
7429 Lisp_Object frame;
7430 struct frame *f;
7431
7432 /* Get the frame containing the mini-buffer
7433 that the selected frame is using. */
7434 mini_window = FRAME_MINIBUF_WINDOW (sf);
7435 frame = XWINDOW (mini_window)->frame;
7436 f = XFRAME (frame);
7437
7438 FRAME_SAMPLE_VISIBILITY (f);
7439 if (FRAME_VISIBLE_P (sf)
7440 && !FRAME_VISIBLE_P (f))
7441 Fmake_frame_visible (frame);
7442
7443 if (STRINGP (m) && SCHARS (m) > 0)
7444 {
7445 set_message (NULL, m, nbytes, multibyte);
7446 if (minibuffer_auto_raise)
7447 Fraise_frame (frame);
7448 /* Assume we are not echoing.
7449 (If we are, echo_now will override this.) */
7450 echo_message_buffer = Qnil;
7451 }
7452 else
7453 clear_message (1, 1);
7454
7455 do_pending_window_change (0);
7456 echo_area_display (1);
7457 do_pending_window_change (0);
7458 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7459 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7460 }
7461 }
7462
7463
7464 /* Display a null-terminated echo area message M. If M is 0, clear
7465 out any existing message, and let the mini-buffer text show through.
7466
7467 The buffer M must continue to exist until after the echo area gets
7468 cleared or some other message gets displayed there. Do not pass
7469 text that is stored in a Lisp string. Do not pass text in a buffer
7470 that was alloca'd. */
7471
7472 void
7473 message1 (m)
7474 char *m;
7475 {
7476 message2 (m, (m ? strlen (m) : 0), 0);
7477 }
7478
7479
7480 /* The non-logging counterpart of message1. */
7481
7482 void
7483 message1_nolog (m)
7484 char *m;
7485 {
7486 message2_nolog (m, (m ? strlen (m) : 0), 0);
7487 }
7488
7489 /* Display a message M which contains a single %s
7490 which gets replaced with STRING. */
7491
7492 void
7493 message_with_string (m, string, log)
7494 char *m;
7495 Lisp_Object string;
7496 int log;
7497 {
7498 CHECK_STRING (string);
7499
7500 if (noninteractive)
7501 {
7502 if (m)
7503 {
7504 if (noninteractive_need_newline)
7505 putc ('\n', stderr);
7506 noninteractive_need_newline = 0;
7507 fprintf (stderr, m, SDATA (string));
7508 if (cursor_in_echo_area == 0)
7509 fprintf (stderr, "\n");
7510 fflush (stderr);
7511 }
7512 }
7513 else if (INTERACTIVE)
7514 {
7515 /* The frame whose minibuffer we're going to display the message on.
7516 It may be larger than the selected frame, so we need
7517 to use its buffer, not the selected frame's buffer. */
7518 Lisp_Object mini_window;
7519 struct frame *f, *sf = SELECTED_FRAME ();
7520
7521 /* Get the frame containing the minibuffer
7522 that the selected frame is using. */
7523 mini_window = FRAME_MINIBUF_WINDOW (sf);
7524 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7525
7526 /* A null message buffer means that the frame hasn't really been
7527 initialized yet. Error messages get reported properly by
7528 cmd_error, so this must be just an informative message; toss it. */
7529 if (FRAME_MESSAGE_BUF (f))
7530 {
7531 Lisp_Object args[2], message;
7532 struct gcpro gcpro1, gcpro2;
7533
7534 args[0] = build_string (m);
7535 args[1] = message = string;
7536 GCPRO2 (args[0], message);
7537 gcpro1.nvars = 2;
7538
7539 message = Fformat (2, args);
7540
7541 if (log)
7542 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7543 else
7544 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7545
7546 UNGCPRO;
7547
7548 /* Print should start at the beginning of the message
7549 buffer next time. */
7550 message_buf_print = 0;
7551 }
7552 }
7553 }
7554
7555
7556 /* Dump an informative message to the minibuf. If M is 0, clear out
7557 any existing message, and let the mini-buffer text show through. */
7558
7559 /* VARARGS 1 */
7560 void
7561 message (m, a1, a2, a3)
7562 char *m;
7563 EMACS_INT a1, a2, a3;
7564 {
7565 if (noninteractive)
7566 {
7567 if (m)
7568 {
7569 if (noninteractive_need_newline)
7570 putc ('\n', stderr);
7571 noninteractive_need_newline = 0;
7572 fprintf (stderr, m, a1, a2, a3);
7573 if (cursor_in_echo_area == 0)
7574 fprintf (stderr, "\n");
7575 fflush (stderr);
7576 }
7577 }
7578 else if (INTERACTIVE)
7579 {
7580 /* The frame whose mini-buffer we're going to display the message
7581 on. It may be larger than the selected frame, so we need to
7582 use its buffer, not the selected frame's buffer. */
7583 Lisp_Object mini_window;
7584 struct frame *f, *sf = SELECTED_FRAME ();
7585
7586 /* Get the frame containing the mini-buffer
7587 that the selected frame is using. */
7588 mini_window = FRAME_MINIBUF_WINDOW (sf);
7589 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7590
7591 /* A null message buffer means that the frame hasn't really been
7592 initialized yet. Error messages get reported properly by
7593 cmd_error, so this must be just an informative message; toss
7594 it. */
7595 if (FRAME_MESSAGE_BUF (f))
7596 {
7597 if (m)
7598 {
7599 int len;
7600 #ifdef NO_ARG_ARRAY
7601 char *a[3];
7602 a[0] = (char *) a1;
7603 a[1] = (char *) a2;
7604 a[2] = (char *) a3;
7605
7606 len = doprnt (FRAME_MESSAGE_BUF (f),
7607 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7608 #else
7609 len = doprnt (FRAME_MESSAGE_BUF (f),
7610 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7611 (char **) &a1);
7612 #endif /* NO_ARG_ARRAY */
7613
7614 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7615 }
7616 else
7617 message1 (0);
7618
7619 /* Print should start at the beginning of the message
7620 buffer next time. */
7621 message_buf_print = 0;
7622 }
7623 }
7624 }
7625
7626
7627 /* The non-logging version of message. */
7628
7629 void
7630 message_nolog (m, a1, a2, a3)
7631 char *m;
7632 EMACS_INT a1, a2, a3;
7633 {
7634 Lisp_Object old_log_max;
7635 old_log_max = Vmessage_log_max;
7636 Vmessage_log_max = Qnil;
7637 message (m, a1, a2, a3);
7638 Vmessage_log_max = old_log_max;
7639 }
7640
7641
7642 /* Display the current message in the current mini-buffer. This is
7643 only called from error handlers in process.c, and is not time
7644 critical. */
7645
7646 void
7647 update_echo_area ()
7648 {
7649 if (!NILP (echo_area_buffer[0]))
7650 {
7651 Lisp_Object string;
7652 string = Fcurrent_message ();
7653 message3 (string, SBYTES (string),
7654 !NILP (current_buffer->enable_multibyte_characters));
7655 }
7656 }
7657
7658
7659 /* Make sure echo area buffers in `echo_buffers' are live.
7660 If they aren't, make new ones. */
7661
7662 static void
7663 ensure_echo_area_buffers ()
7664 {
7665 int i;
7666
7667 for (i = 0; i < 2; ++i)
7668 if (!BUFFERP (echo_buffer[i])
7669 || NILP (XBUFFER (echo_buffer[i])->name))
7670 {
7671 char name[30];
7672 Lisp_Object old_buffer;
7673 int j;
7674
7675 old_buffer = echo_buffer[i];
7676 sprintf (name, " *Echo Area %d*", i);
7677 echo_buffer[i] = Fget_buffer_create (build_string (name));
7678 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7679
7680 for (j = 0; j < 2; ++j)
7681 if (EQ (old_buffer, echo_area_buffer[j]))
7682 echo_area_buffer[j] = echo_buffer[i];
7683 }
7684 }
7685
7686
7687 /* Call FN with args A1..A4 with either the current or last displayed
7688 echo_area_buffer as current buffer.
7689
7690 WHICH zero means use the current message buffer
7691 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7692 from echo_buffer[] and clear it.
7693
7694 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7695 suitable buffer from echo_buffer[] and clear it.
7696
7697 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7698 that the current message becomes the last displayed one, make
7699 choose a suitable buffer for echo_area_buffer[0], and clear it.
7700
7701 Value is what FN returns. */
7702
7703 static int
7704 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7705 struct window *w;
7706 int which;
7707 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7708 EMACS_INT a1;
7709 Lisp_Object a2;
7710 EMACS_INT a3, a4;
7711 {
7712 Lisp_Object buffer;
7713 int this_one, the_other, clear_buffer_p, rc;
7714 int count = SPECPDL_INDEX ();
7715
7716 /* If buffers aren't live, make new ones. */
7717 ensure_echo_area_buffers ();
7718
7719 clear_buffer_p = 0;
7720
7721 if (which == 0)
7722 this_one = 0, the_other = 1;
7723 else if (which > 0)
7724 this_one = 1, the_other = 0;
7725 else
7726 {
7727 this_one = 0, the_other = 1;
7728 clear_buffer_p = 1;
7729
7730 /* We need a fresh one in case the current echo buffer equals
7731 the one containing the last displayed echo area message. */
7732 if (!NILP (echo_area_buffer[this_one])
7733 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7734 echo_area_buffer[this_one] = Qnil;
7735 }
7736
7737 /* Choose a suitable buffer from echo_buffer[] is we don't
7738 have one. */
7739 if (NILP (echo_area_buffer[this_one]))
7740 {
7741 echo_area_buffer[this_one]
7742 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7743 ? echo_buffer[the_other]
7744 : echo_buffer[this_one]);
7745 clear_buffer_p = 1;
7746 }
7747
7748 buffer = echo_area_buffer[this_one];
7749
7750 /* Don't get confused by reusing the buffer used for echoing
7751 for a different purpose. */
7752 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7753 cancel_echoing ();
7754
7755 record_unwind_protect (unwind_with_echo_area_buffer,
7756 with_echo_area_buffer_unwind_data (w));
7757
7758 /* Make the echo area buffer current. Note that for display
7759 purposes, it is not necessary that the displayed window's buffer
7760 == current_buffer, except for text property lookup. So, let's
7761 only set that buffer temporarily here without doing a full
7762 Fset_window_buffer. We must also change w->pointm, though,
7763 because otherwise an assertions in unshow_buffer fails, and Emacs
7764 aborts. */
7765 set_buffer_internal_1 (XBUFFER (buffer));
7766 if (w)
7767 {
7768 w->buffer = buffer;
7769 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7770 }
7771
7772 current_buffer->undo_list = Qt;
7773 current_buffer->read_only = Qnil;
7774 specbind (Qinhibit_read_only, Qt);
7775 specbind (Qinhibit_modification_hooks, Qt);
7776
7777 if (clear_buffer_p && Z > BEG)
7778 del_range (BEG, Z);
7779
7780 xassert (BEGV >= BEG);
7781 xassert (ZV <= Z && ZV >= BEGV);
7782
7783 rc = fn (a1, a2, a3, a4);
7784
7785 xassert (BEGV >= BEG);
7786 xassert (ZV <= Z && ZV >= BEGV);
7787
7788 unbind_to (count, Qnil);
7789 return rc;
7790 }
7791
7792
7793 /* Save state that should be preserved around the call to the function
7794 FN called in with_echo_area_buffer. */
7795
7796 static Lisp_Object
7797 with_echo_area_buffer_unwind_data (w)
7798 struct window *w;
7799 {
7800 int i = 0;
7801 Lisp_Object vector;
7802
7803 /* Reduce consing by keeping one vector in
7804 Vwith_echo_area_save_vector. */
7805 vector = Vwith_echo_area_save_vector;
7806 Vwith_echo_area_save_vector = Qnil;
7807
7808 if (NILP (vector))
7809 vector = Fmake_vector (make_number (7), Qnil);
7810
7811 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7812 AREF (vector, i) = Vdeactivate_mark, ++i;
7813 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7814
7815 if (w)
7816 {
7817 XSETWINDOW (AREF (vector, i), w); ++i;
7818 AREF (vector, i) = w->buffer; ++i;
7819 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7820 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7821 }
7822 else
7823 {
7824 int end = i + 4;
7825 for (; i < end; ++i)
7826 AREF (vector, i) = Qnil;
7827 }
7828
7829 xassert (i == ASIZE (vector));
7830 return vector;
7831 }
7832
7833
7834 /* Restore global state from VECTOR which was created by
7835 with_echo_area_buffer_unwind_data. */
7836
7837 static Lisp_Object
7838 unwind_with_echo_area_buffer (vector)
7839 Lisp_Object vector;
7840 {
7841 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7842 Vdeactivate_mark = AREF (vector, 1);
7843 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7844
7845 if (WINDOWP (AREF (vector, 3)))
7846 {
7847 struct window *w;
7848 Lisp_Object buffer, charpos, bytepos;
7849
7850 w = XWINDOW (AREF (vector, 3));
7851 buffer = AREF (vector, 4);
7852 charpos = AREF (vector, 5);
7853 bytepos = AREF (vector, 6);
7854
7855 w->buffer = buffer;
7856 set_marker_both (w->pointm, buffer,
7857 XFASTINT (charpos), XFASTINT (bytepos));
7858 }
7859
7860 Vwith_echo_area_save_vector = vector;
7861 return Qnil;
7862 }
7863
7864
7865 /* Set up the echo area for use by print functions. MULTIBYTE_P
7866 non-zero means we will print multibyte. */
7867
7868 void
7869 setup_echo_area_for_printing (multibyte_p)
7870 int multibyte_p;
7871 {
7872 /* If we can't find an echo area any more, exit. */
7873 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7874 Fkill_emacs (Qnil);
7875
7876 ensure_echo_area_buffers ();
7877
7878 if (!message_buf_print)
7879 {
7880 /* A message has been output since the last time we printed.
7881 Choose a fresh echo area buffer. */
7882 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7883 echo_area_buffer[0] = echo_buffer[1];
7884 else
7885 echo_area_buffer[0] = echo_buffer[0];
7886
7887 /* Switch to that buffer and clear it. */
7888 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7889 current_buffer->truncate_lines = Qnil;
7890
7891 if (Z > BEG)
7892 {
7893 int count = SPECPDL_INDEX ();
7894 specbind (Qinhibit_read_only, Qt);
7895 /* Note that undo recording is always disabled. */
7896 del_range (BEG, Z);
7897 unbind_to (count, Qnil);
7898 }
7899 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7900
7901 /* Set up the buffer for the multibyteness we need. */
7902 if (multibyte_p
7903 != !NILP (current_buffer->enable_multibyte_characters))
7904 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7905
7906 /* Raise the frame containing the echo area. */
7907 if (minibuffer_auto_raise)
7908 {
7909 struct frame *sf = SELECTED_FRAME ();
7910 Lisp_Object mini_window;
7911 mini_window = FRAME_MINIBUF_WINDOW (sf);
7912 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7913 }
7914
7915 message_log_maybe_newline ();
7916 message_buf_print = 1;
7917 }
7918 else
7919 {
7920 if (NILP (echo_area_buffer[0]))
7921 {
7922 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7923 echo_area_buffer[0] = echo_buffer[1];
7924 else
7925 echo_area_buffer[0] = echo_buffer[0];
7926 }
7927
7928 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7929 {
7930 /* Someone switched buffers between print requests. */
7931 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7932 current_buffer->truncate_lines = Qnil;
7933 }
7934 }
7935 }
7936
7937
7938 /* Display an echo area message in window W. Value is non-zero if W's
7939 height is changed. If display_last_displayed_message_p is
7940 non-zero, display the message that was last displayed, otherwise
7941 display the current message. */
7942
7943 static int
7944 display_echo_area (w)
7945 struct window *w;
7946 {
7947 int i, no_message_p, window_height_changed_p, count;
7948
7949 /* Temporarily disable garbage collections while displaying the echo
7950 area. This is done because a GC can print a message itself.
7951 That message would modify the echo area buffer's contents while a
7952 redisplay of the buffer is going on, and seriously confuse
7953 redisplay. */
7954 count = inhibit_garbage_collection ();
7955
7956 /* If there is no message, we must call display_echo_area_1
7957 nevertheless because it resizes the window. But we will have to
7958 reset the echo_area_buffer in question to nil at the end because
7959 with_echo_area_buffer will sets it to an empty buffer. */
7960 i = display_last_displayed_message_p ? 1 : 0;
7961 no_message_p = NILP (echo_area_buffer[i]);
7962
7963 window_height_changed_p
7964 = with_echo_area_buffer (w, display_last_displayed_message_p,
7965 display_echo_area_1,
7966 (EMACS_INT) w, Qnil, 0, 0);
7967
7968 if (no_message_p)
7969 echo_area_buffer[i] = Qnil;
7970
7971 unbind_to (count, Qnil);
7972 return window_height_changed_p;
7973 }
7974
7975
7976 /* Helper for display_echo_area. Display the current buffer which
7977 contains the current echo area message in window W, a mini-window,
7978 a pointer to which is passed in A1. A2..A4 are currently not used.
7979 Change the height of W so that all of the message is displayed.
7980 Value is non-zero if height of W was changed. */
7981
7982 static int
7983 display_echo_area_1 (a1, a2, a3, a4)
7984 EMACS_INT a1;
7985 Lisp_Object a2;
7986 EMACS_INT a3, a4;
7987 {
7988 struct window *w = (struct window *) a1;
7989 Lisp_Object window;
7990 struct text_pos start;
7991 int window_height_changed_p = 0;
7992
7993 /* Do this before displaying, so that we have a large enough glyph
7994 matrix for the display. If we can't get enough space for the
7995 whole text, display the last N lines. That works by setting w->start. */
7996 window_height_changed_p = resize_mini_window (w, 0);
7997
7998 /* Use the starting position chosen by resize_mini_window. */
7999 SET_TEXT_POS_FROM_MARKER (start, w->start);
8000
8001 /* Display. */
8002 clear_glyph_matrix (w->desired_matrix);
8003 XSETWINDOW (window, w);
8004 try_window (window, start, 0);
8005
8006 return window_height_changed_p;
8007 }
8008
8009
8010 /* Resize the echo area window to exactly the size needed for the
8011 currently displayed message, if there is one. If a mini-buffer
8012 is active, don't shrink it. */
8013
8014 void
8015 resize_echo_area_exactly ()
8016 {
8017 if (BUFFERP (echo_area_buffer[0])
8018 && WINDOWP (echo_area_window))
8019 {
8020 struct window *w = XWINDOW (echo_area_window);
8021 int resized_p;
8022 Lisp_Object resize_exactly;
8023
8024 if (minibuf_level == 0)
8025 resize_exactly = Qt;
8026 else
8027 resize_exactly = Qnil;
8028
8029 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8030 (EMACS_INT) w, resize_exactly, 0, 0);
8031 if (resized_p)
8032 {
8033 ++windows_or_buffers_changed;
8034 ++update_mode_lines;
8035 redisplay_internal (0);
8036 }
8037 }
8038 }
8039
8040
8041 /* Callback function for with_echo_area_buffer, when used from
8042 resize_echo_area_exactly. A1 contains a pointer to the window to
8043 resize, EXACTLY non-nil means resize the mini-window exactly to the
8044 size of the text displayed. A3 and A4 are not used. Value is what
8045 resize_mini_window returns. */
8046
8047 static int
8048 resize_mini_window_1 (a1, exactly, a3, a4)
8049 EMACS_INT a1;
8050 Lisp_Object exactly;
8051 EMACS_INT a3, a4;
8052 {
8053 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8054 }
8055
8056
8057 /* Resize mini-window W to fit the size of its contents. EXACT:P
8058 means size the window exactly to the size needed. Otherwise, it's
8059 only enlarged until W's buffer is empty.
8060
8061 Set W->start to the right place to begin display. If the whole
8062 contents fit, start at the beginning. Otherwise, start so as
8063 to make the end of the contents appear. This is particularly
8064 important for y-or-n-p, but seems desirable generally.
8065
8066 Value is non-zero if the window height has been changed. */
8067
8068 int
8069 resize_mini_window (w, exact_p)
8070 struct window *w;
8071 int exact_p;
8072 {
8073 struct frame *f = XFRAME (w->frame);
8074 int window_height_changed_p = 0;
8075
8076 xassert (MINI_WINDOW_P (w));
8077
8078 /* By default, start display at the beginning. */
8079 set_marker_both (w->start, w->buffer,
8080 BUF_BEGV (XBUFFER (w->buffer)),
8081 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8082
8083 /* Don't resize windows while redisplaying a window; it would
8084 confuse redisplay functions when the size of the window they are
8085 displaying changes from under them. Such a resizing can happen,
8086 for instance, when which-func prints a long message while
8087 we are running fontification-functions. We're running these
8088 functions with safe_call which binds inhibit-redisplay to t. */
8089 if (!NILP (Vinhibit_redisplay))
8090 return 0;
8091
8092 /* Nil means don't try to resize. */
8093 if (NILP (Vresize_mini_windows)
8094 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8095 return 0;
8096
8097 if (!FRAME_MINIBUF_ONLY_P (f))
8098 {
8099 struct it it;
8100 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8101 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8102 int height, max_height;
8103 int unit = FRAME_LINE_HEIGHT (f);
8104 struct text_pos start;
8105 struct buffer *old_current_buffer = NULL;
8106
8107 if (current_buffer != XBUFFER (w->buffer))
8108 {
8109 old_current_buffer = current_buffer;
8110 set_buffer_internal (XBUFFER (w->buffer));
8111 }
8112
8113 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8114
8115 /* Compute the max. number of lines specified by the user. */
8116 if (FLOATP (Vmax_mini_window_height))
8117 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8118 else if (INTEGERP (Vmax_mini_window_height))
8119 max_height = XINT (Vmax_mini_window_height);
8120 else
8121 max_height = total_height / 4;
8122
8123 /* Correct that max. height if it's bogus. */
8124 max_height = max (1, max_height);
8125 max_height = min (total_height, max_height);
8126
8127 /* Find out the height of the text in the window. */
8128 if (it.truncate_lines_p)
8129 height = 1;
8130 else
8131 {
8132 last_height = 0;
8133 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8134 if (it.max_ascent == 0 && it.max_descent == 0)
8135 height = it.current_y + last_height;
8136 else
8137 height = it.current_y + it.max_ascent + it.max_descent;
8138 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8139 height = (height + unit - 1) / unit;
8140 }
8141
8142 /* Compute a suitable window start. */
8143 if (height > max_height)
8144 {
8145 height = max_height;
8146 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8147 move_it_vertically_backward (&it, (height - 1) * unit);
8148 start = it.current.pos;
8149 }
8150 else
8151 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8152 SET_MARKER_FROM_TEXT_POS (w->start, start);
8153
8154 if (EQ (Vresize_mini_windows, Qgrow_only))
8155 {
8156 /* Let it grow only, until we display an empty message, in which
8157 case the window shrinks again. */
8158 if (height > WINDOW_TOTAL_LINES (w))
8159 {
8160 int old_height = WINDOW_TOTAL_LINES (w);
8161 freeze_window_starts (f, 1);
8162 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8163 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8164 }
8165 else if (height < WINDOW_TOTAL_LINES (w)
8166 && (exact_p || BEGV == ZV))
8167 {
8168 int old_height = WINDOW_TOTAL_LINES (w);
8169 freeze_window_starts (f, 0);
8170 shrink_mini_window (w);
8171 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8172 }
8173 }
8174 else
8175 {
8176 /* Always resize to exact size needed. */
8177 if (height > WINDOW_TOTAL_LINES (w))
8178 {
8179 int old_height = WINDOW_TOTAL_LINES (w);
8180 freeze_window_starts (f, 1);
8181 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8182 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8183 }
8184 else if (height < WINDOW_TOTAL_LINES (w))
8185 {
8186 int old_height = WINDOW_TOTAL_LINES (w);
8187 freeze_window_starts (f, 0);
8188 shrink_mini_window (w);
8189
8190 if (height)
8191 {
8192 freeze_window_starts (f, 1);
8193 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8194 }
8195
8196 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8197 }
8198 }
8199
8200 if (old_current_buffer)
8201 set_buffer_internal (old_current_buffer);
8202 }
8203
8204 return window_height_changed_p;
8205 }
8206
8207
8208 /* Value is the current message, a string, or nil if there is no
8209 current message. */
8210
8211 Lisp_Object
8212 current_message ()
8213 {
8214 Lisp_Object msg;
8215
8216 if (NILP (echo_area_buffer[0]))
8217 msg = Qnil;
8218 else
8219 {
8220 with_echo_area_buffer (0, 0, current_message_1,
8221 (EMACS_INT) &msg, Qnil, 0, 0);
8222 if (NILP (msg))
8223 echo_area_buffer[0] = Qnil;
8224 }
8225
8226 return msg;
8227 }
8228
8229
8230 static int
8231 current_message_1 (a1, a2, a3, a4)
8232 EMACS_INT a1;
8233 Lisp_Object a2;
8234 EMACS_INT a3, a4;
8235 {
8236 Lisp_Object *msg = (Lisp_Object *) a1;
8237
8238 if (Z > BEG)
8239 *msg = make_buffer_string (BEG, Z, 1);
8240 else
8241 *msg = Qnil;
8242 return 0;
8243 }
8244
8245
8246 /* Push the current message on Vmessage_stack for later restauration
8247 by restore_message. Value is non-zero if the current message isn't
8248 empty. This is a relatively infrequent operation, so it's not
8249 worth optimizing. */
8250
8251 int
8252 push_message ()
8253 {
8254 Lisp_Object msg;
8255 msg = current_message ();
8256 Vmessage_stack = Fcons (msg, Vmessage_stack);
8257 return STRINGP (msg);
8258 }
8259
8260
8261 /* Restore message display from the top of Vmessage_stack. */
8262
8263 void
8264 restore_message ()
8265 {
8266 Lisp_Object msg;
8267
8268 xassert (CONSP (Vmessage_stack));
8269 msg = XCAR (Vmessage_stack);
8270 if (STRINGP (msg))
8271 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8272 else
8273 message3_nolog (msg, 0, 0);
8274 }
8275
8276
8277 /* Handler for record_unwind_protect calling pop_message. */
8278
8279 Lisp_Object
8280 pop_message_unwind (dummy)
8281 Lisp_Object dummy;
8282 {
8283 pop_message ();
8284 return Qnil;
8285 }
8286
8287 /* Pop the top-most entry off Vmessage_stack. */
8288
8289 void
8290 pop_message ()
8291 {
8292 xassert (CONSP (Vmessage_stack));
8293 Vmessage_stack = XCDR (Vmessage_stack);
8294 }
8295
8296
8297 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8298 exits. If the stack is not empty, we have a missing pop_message
8299 somewhere. */
8300
8301 void
8302 check_message_stack ()
8303 {
8304 if (!NILP (Vmessage_stack))
8305 abort ();
8306 }
8307
8308
8309 /* Truncate to NCHARS what will be displayed in the echo area the next
8310 time we display it---but don't redisplay it now. */
8311
8312 void
8313 truncate_echo_area (nchars)
8314 int nchars;
8315 {
8316 if (nchars == 0)
8317 echo_area_buffer[0] = Qnil;
8318 /* A null message buffer means that the frame hasn't really been
8319 initialized yet. Error messages get reported properly by
8320 cmd_error, so this must be just an informative message; toss it. */
8321 else if (!noninteractive
8322 && INTERACTIVE
8323 && !NILP (echo_area_buffer[0]))
8324 {
8325 struct frame *sf = SELECTED_FRAME ();
8326 if (FRAME_MESSAGE_BUF (sf))
8327 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8328 }
8329 }
8330
8331
8332 /* Helper function for truncate_echo_area. Truncate the current
8333 message to at most NCHARS characters. */
8334
8335 static int
8336 truncate_message_1 (nchars, a2, a3, a4)
8337 EMACS_INT nchars;
8338 Lisp_Object a2;
8339 EMACS_INT a3, a4;
8340 {
8341 if (BEG + nchars < Z)
8342 del_range (BEG + nchars, Z);
8343 if (Z == BEG)
8344 echo_area_buffer[0] = Qnil;
8345 return 0;
8346 }
8347
8348
8349 /* Set the current message to a substring of S or STRING.
8350
8351 If STRING is a Lisp string, set the message to the first NBYTES
8352 bytes from STRING. NBYTES zero means use the whole string. If
8353 STRING is multibyte, the message will be displayed multibyte.
8354
8355 If S is not null, set the message to the first LEN bytes of S. LEN
8356 zero means use the whole string. MULTIBYTE_P non-zero means S is
8357 multibyte. Display the message multibyte in that case.
8358
8359 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8360 to t before calling set_message_1 (which calls insert).
8361 */
8362
8363 void
8364 set_message (s, string, nbytes, multibyte_p)
8365 const char *s;
8366 Lisp_Object string;
8367 int nbytes, multibyte_p;
8368 {
8369 message_enable_multibyte
8370 = ((s && multibyte_p)
8371 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8372
8373 with_echo_area_buffer (0, -1, set_message_1,
8374 (EMACS_INT) s, string, nbytes, multibyte_p);
8375 message_buf_print = 0;
8376 help_echo_showing_p = 0;
8377 }
8378
8379
8380 /* Helper function for set_message. Arguments have the same meaning
8381 as there, with A1 corresponding to S and A2 corresponding to STRING
8382 This function is called with the echo area buffer being
8383 current. */
8384
8385 static int
8386 set_message_1 (a1, a2, nbytes, multibyte_p)
8387 EMACS_INT a1;
8388 Lisp_Object a2;
8389 EMACS_INT nbytes, multibyte_p;
8390 {
8391 const char *s = (const char *) a1;
8392 Lisp_Object string = a2;
8393
8394 /* Change multibyteness of the echo buffer appropriately. */
8395 if (message_enable_multibyte
8396 != !NILP (current_buffer->enable_multibyte_characters))
8397 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8398
8399 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8400
8401 /* Insert new message at BEG. */
8402 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8403
8404 if (STRINGP (string))
8405 {
8406 int nchars;
8407
8408 if (nbytes == 0)
8409 nbytes = SBYTES (string);
8410 nchars = string_byte_to_char (string, nbytes);
8411
8412 /* This function takes care of single/multibyte conversion. We
8413 just have to ensure that the echo area buffer has the right
8414 setting of enable_multibyte_characters. */
8415 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8416 }
8417 else if (s)
8418 {
8419 if (nbytes == 0)
8420 nbytes = strlen (s);
8421
8422 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8423 {
8424 /* Convert from multi-byte to single-byte. */
8425 int i, c, n;
8426 unsigned char work[1];
8427
8428 /* Convert a multibyte string to single-byte. */
8429 for (i = 0; i < nbytes; i += n)
8430 {
8431 c = string_char_and_length (s + i, nbytes - i, &n);
8432 work[0] = (SINGLE_BYTE_CHAR_P (c)
8433 ? c
8434 : multibyte_char_to_unibyte (c, Qnil));
8435 insert_1_both (work, 1, 1, 1, 0, 0);
8436 }
8437 }
8438 else if (!multibyte_p
8439 && !NILP (current_buffer->enable_multibyte_characters))
8440 {
8441 /* Convert from single-byte to multi-byte. */
8442 int i, c, n;
8443 const unsigned char *msg = (const unsigned char *) s;
8444 unsigned char str[MAX_MULTIBYTE_LENGTH];
8445
8446 /* Convert a single-byte string to multibyte. */
8447 for (i = 0; i < nbytes; i++)
8448 {
8449 c = unibyte_char_to_multibyte (msg[i]);
8450 n = CHAR_STRING (c, str);
8451 insert_1_both (str, 1, n, 1, 0, 0);
8452 }
8453 }
8454 else
8455 insert_1 (s, nbytes, 1, 0, 0);
8456 }
8457
8458 return 0;
8459 }
8460
8461
8462 /* Clear messages. CURRENT_P non-zero means clear the current
8463 message. LAST_DISPLAYED_P non-zero means clear the message
8464 last displayed. */
8465
8466 void
8467 clear_message (current_p, last_displayed_p)
8468 int current_p, last_displayed_p;
8469 {
8470 if (current_p)
8471 {
8472 echo_area_buffer[0] = Qnil;
8473 message_cleared_p = 1;
8474 }
8475
8476 if (last_displayed_p)
8477 echo_area_buffer[1] = Qnil;
8478
8479 message_buf_print = 0;
8480 }
8481
8482 /* Clear garbaged frames.
8483
8484 This function is used where the old redisplay called
8485 redraw_garbaged_frames which in turn called redraw_frame which in
8486 turn called clear_frame. The call to clear_frame was a source of
8487 flickering. I believe a clear_frame is not necessary. It should
8488 suffice in the new redisplay to invalidate all current matrices,
8489 and ensure a complete redisplay of all windows. */
8490
8491 static void
8492 clear_garbaged_frames ()
8493 {
8494 if (frame_garbaged)
8495 {
8496 Lisp_Object tail, frame;
8497 int changed_count = 0;
8498
8499 FOR_EACH_FRAME (tail, frame)
8500 {
8501 struct frame *f = XFRAME (frame);
8502
8503 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8504 {
8505 if (f->resized_p)
8506 {
8507 Fredraw_frame (frame);
8508 f->force_flush_display_p = 1;
8509 }
8510 clear_current_matrices (f);
8511 changed_count++;
8512 f->garbaged = 0;
8513 f->resized_p = 0;
8514 }
8515 }
8516
8517 frame_garbaged = 0;
8518 if (changed_count)
8519 ++windows_or_buffers_changed;
8520 }
8521 }
8522
8523
8524 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8525 is non-zero update selected_frame. Value is non-zero if the
8526 mini-windows height has been changed. */
8527
8528 static int
8529 echo_area_display (update_frame_p)
8530 int update_frame_p;
8531 {
8532 Lisp_Object mini_window;
8533 struct window *w;
8534 struct frame *f;
8535 int window_height_changed_p = 0;
8536 struct frame *sf = SELECTED_FRAME ();
8537
8538 mini_window = FRAME_MINIBUF_WINDOW (sf);
8539 w = XWINDOW (mini_window);
8540 f = XFRAME (WINDOW_FRAME (w));
8541
8542 /* Don't display if frame is invisible or not yet initialized. */
8543 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8544 return 0;
8545
8546 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8547 #ifndef MAC_OS8
8548 #ifdef HAVE_WINDOW_SYSTEM
8549 /* When Emacs starts, selected_frame may be the initial terminal
8550 frame. If we let this through, a message would be displayed on
8551 the terminal. */
8552 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8553 return 0;
8554 #endif /* HAVE_WINDOW_SYSTEM */
8555 #endif
8556
8557 /* Redraw garbaged frames. */
8558 if (frame_garbaged)
8559 clear_garbaged_frames ();
8560
8561 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8562 {
8563 echo_area_window = mini_window;
8564 window_height_changed_p = display_echo_area (w);
8565 w->must_be_updated_p = 1;
8566
8567 /* Update the display, unless called from redisplay_internal.
8568 Also don't update the screen during redisplay itself. The
8569 update will happen at the end of redisplay, and an update
8570 here could cause confusion. */
8571 if (update_frame_p && !redisplaying_p)
8572 {
8573 int n = 0;
8574
8575 /* If the display update has been interrupted by pending
8576 input, update mode lines in the frame. Due to the
8577 pending input, it might have been that redisplay hasn't
8578 been called, so that mode lines above the echo area are
8579 garbaged. This looks odd, so we prevent it here. */
8580 if (!display_completed)
8581 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8582
8583 if (window_height_changed_p
8584 /* Don't do this if Emacs is shutting down. Redisplay
8585 needs to run hooks. */
8586 && !NILP (Vrun_hooks))
8587 {
8588 /* Must update other windows. Likewise as in other
8589 cases, don't let this update be interrupted by
8590 pending input. */
8591 int count = SPECPDL_INDEX ();
8592 specbind (Qredisplay_dont_pause, Qt);
8593 windows_or_buffers_changed = 1;
8594 redisplay_internal (0);
8595 unbind_to (count, Qnil);
8596 }
8597 else if (FRAME_WINDOW_P (f) && n == 0)
8598 {
8599 /* Window configuration is the same as before.
8600 Can do with a display update of the echo area,
8601 unless we displayed some mode lines. */
8602 update_single_window (w, 1);
8603 FRAME_RIF (f)->flush_display (f);
8604 }
8605 else
8606 update_frame (f, 1, 1);
8607
8608 /* If cursor is in the echo area, make sure that the next
8609 redisplay displays the minibuffer, so that the cursor will
8610 be replaced with what the minibuffer wants. */
8611 if (cursor_in_echo_area)
8612 ++windows_or_buffers_changed;
8613 }
8614 }
8615 else if (!EQ (mini_window, selected_window))
8616 windows_or_buffers_changed++;
8617
8618 /* Last displayed message is now the current message. */
8619 echo_area_buffer[1] = echo_area_buffer[0];
8620 /* Inform read_char that we're not echoing. */
8621 echo_message_buffer = Qnil;
8622
8623 /* Prevent redisplay optimization in redisplay_internal by resetting
8624 this_line_start_pos. This is done because the mini-buffer now
8625 displays the message instead of its buffer text. */
8626 if (EQ (mini_window, selected_window))
8627 CHARPOS (this_line_start_pos) = 0;
8628
8629 return window_height_changed_p;
8630 }
8631
8632
8633 \f
8634 /***********************************************************************
8635 Mode Lines and Frame Titles
8636 ***********************************************************************/
8637
8638 /* A buffer for constructing non-propertized mode-line strings and
8639 frame titles in it; allocated from the heap in init_xdisp and
8640 resized as needed in store_mode_line_noprop_char. */
8641
8642 static char *mode_line_noprop_buf;
8643
8644 /* The buffer's end, and a current output position in it. */
8645
8646 static char *mode_line_noprop_buf_end;
8647 static char *mode_line_noprop_ptr;
8648
8649 #define MODE_LINE_NOPROP_LEN(start) \
8650 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8651
8652 static enum {
8653 MODE_LINE_DISPLAY = 0,
8654 MODE_LINE_TITLE,
8655 MODE_LINE_NOPROP,
8656 MODE_LINE_STRING
8657 } mode_line_target;
8658
8659 /* Alist that caches the results of :propertize.
8660 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8661 static Lisp_Object mode_line_proptrans_alist;
8662
8663 /* List of strings making up the mode-line. */
8664 static Lisp_Object mode_line_string_list;
8665
8666 /* Base face property when building propertized mode line string. */
8667 static Lisp_Object mode_line_string_face;
8668 static Lisp_Object mode_line_string_face_prop;
8669
8670
8671 /* Unwind data for mode line strings */
8672
8673 static Lisp_Object Vmode_line_unwind_vector;
8674
8675 static Lisp_Object
8676 format_mode_line_unwind_data (obuf, save_proptrans)
8677 struct buffer *obuf;
8678 {
8679 Lisp_Object vector;
8680
8681 /* Reduce consing by keeping one vector in
8682 Vwith_echo_area_save_vector. */
8683 vector = Vmode_line_unwind_vector;
8684 Vmode_line_unwind_vector = Qnil;
8685
8686 if (NILP (vector))
8687 vector = Fmake_vector (make_number (7), Qnil);
8688
8689 AREF (vector, 0) = make_number (mode_line_target);
8690 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8691 AREF (vector, 2) = mode_line_string_list;
8692 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8693 AREF (vector, 4) = mode_line_string_face;
8694 AREF (vector, 5) = mode_line_string_face_prop;
8695
8696 if (obuf)
8697 XSETBUFFER (AREF (vector, 6), obuf);
8698 else
8699 AREF (vector, 6) = Qnil;
8700
8701 return vector;
8702 }
8703
8704 static Lisp_Object
8705 unwind_format_mode_line (vector)
8706 Lisp_Object vector;
8707 {
8708 mode_line_target = XINT (AREF (vector, 0));
8709 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8710 mode_line_string_list = AREF (vector, 2);
8711 if (! EQ (AREF (vector, 3), Qt))
8712 mode_line_proptrans_alist = AREF (vector, 3);
8713 mode_line_string_face = AREF (vector, 4);
8714 mode_line_string_face_prop = AREF (vector, 5);
8715
8716 if (!NILP (AREF (vector, 6)))
8717 {
8718 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8719 AREF (vector, 6) = Qnil;
8720 }
8721
8722 Vmode_line_unwind_vector = vector;
8723 return Qnil;
8724 }
8725
8726
8727 /* Store a single character C for the frame title in mode_line_noprop_buf.
8728 Re-allocate mode_line_noprop_buf if necessary. */
8729
8730 static void
8731 #ifdef PROTOTYPES
8732 store_mode_line_noprop_char (char c)
8733 #else
8734 store_mode_line_noprop_char (c)
8735 char c;
8736 #endif
8737 {
8738 /* If output position has reached the end of the allocated buffer,
8739 double the buffer's size. */
8740 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8741 {
8742 int len = MODE_LINE_NOPROP_LEN (0);
8743 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8744 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8745 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8746 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8747 }
8748
8749 *mode_line_noprop_ptr++ = c;
8750 }
8751
8752
8753 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8754 mode_line_noprop_ptr. STR is the string to store. Do not copy
8755 characters that yield more columns than PRECISION; PRECISION <= 0
8756 means copy the whole string. Pad with spaces until FIELD_WIDTH
8757 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8758 pad. Called from display_mode_element when it is used to build a
8759 frame title. */
8760
8761 static int
8762 store_mode_line_noprop (str, field_width, precision)
8763 const unsigned char *str;
8764 int field_width, precision;
8765 {
8766 int n = 0;
8767 int dummy, nbytes;
8768
8769 /* Copy at most PRECISION chars from STR. */
8770 nbytes = strlen (str);
8771 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8772 while (nbytes--)
8773 store_mode_line_noprop_char (*str++);
8774
8775 /* Fill up with spaces until FIELD_WIDTH reached. */
8776 while (field_width > 0
8777 && n < field_width)
8778 {
8779 store_mode_line_noprop_char (' ');
8780 ++n;
8781 }
8782
8783 return n;
8784 }
8785
8786 /***********************************************************************
8787 Frame Titles
8788 ***********************************************************************/
8789
8790 #ifdef HAVE_WINDOW_SYSTEM
8791
8792 /* Set the title of FRAME, if it has changed. The title format is
8793 Vicon_title_format if FRAME is iconified, otherwise it is
8794 frame_title_format. */
8795
8796 static void
8797 x_consider_frame_title (frame)
8798 Lisp_Object frame;
8799 {
8800 struct frame *f = XFRAME (frame);
8801
8802 if (FRAME_WINDOW_P (f)
8803 || FRAME_MINIBUF_ONLY_P (f)
8804 || f->explicit_name)
8805 {
8806 /* Do we have more than one visible frame on this X display? */
8807 Lisp_Object tail;
8808 Lisp_Object fmt;
8809 int title_start;
8810 char *title;
8811 int len;
8812 struct it it;
8813 int count = SPECPDL_INDEX ();
8814
8815 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8816 {
8817 Lisp_Object other_frame = XCAR (tail);
8818 struct frame *tf = XFRAME (other_frame);
8819
8820 if (tf != f
8821 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8822 && !FRAME_MINIBUF_ONLY_P (tf)
8823 && !EQ (other_frame, tip_frame)
8824 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8825 break;
8826 }
8827
8828 /* Set global variable indicating that multiple frames exist. */
8829 multiple_frames = CONSP (tail);
8830
8831 /* Switch to the buffer of selected window of the frame. Set up
8832 mode_line_target so that display_mode_element will output into
8833 mode_line_noprop_buf; then display the title. */
8834 record_unwind_protect (unwind_format_mode_line,
8835 format_mode_line_unwind_data (current_buffer, 0));
8836
8837 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8838 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8839
8840 mode_line_target = MODE_LINE_TITLE;
8841 title_start = MODE_LINE_NOPROP_LEN (0);
8842 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8843 NULL, DEFAULT_FACE_ID);
8844 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8845 len = MODE_LINE_NOPROP_LEN (title_start);
8846 title = mode_line_noprop_buf + title_start;
8847 unbind_to (count, Qnil);
8848
8849 /* Set the title only if it's changed. This avoids consing in
8850 the common case where it hasn't. (If it turns out that we've
8851 already wasted too much time by walking through the list with
8852 display_mode_element, then we might need to optimize at a
8853 higher level than this.) */
8854 if (! STRINGP (f->name)
8855 || SBYTES (f->name) != len
8856 || bcmp (title, SDATA (f->name), len) != 0)
8857 x_implicitly_set_name (f, make_string (title, len), Qnil);
8858 }
8859 }
8860
8861 #endif /* not HAVE_WINDOW_SYSTEM */
8862
8863
8864
8865 \f
8866 /***********************************************************************
8867 Menu Bars
8868 ***********************************************************************/
8869
8870
8871 /* Prepare for redisplay by updating menu-bar item lists when
8872 appropriate. This can call eval. */
8873
8874 void
8875 prepare_menu_bars ()
8876 {
8877 int all_windows;
8878 struct gcpro gcpro1, gcpro2;
8879 struct frame *f;
8880 Lisp_Object tooltip_frame;
8881
8882 #ifdef HAVE_WINDOW_SYSTEM
8883 tooltip_frame = tip_frame;
8884 #else
8885 tooltip_frame = Qnil;
8886 #endif
8887
8888 /* Update all frame titles based on their buffer names, etc. We do
8889 this before the menu bars so that the buffer-menu will show the
8890 up-to-date frame titles. */
8891 #ifdef HAVE_WINDOW_SYSTEM
8892 if (windows_or_buffers_changed || update_mode_lines)
8893 {
8894 Lisp_Object tail, frame;
8895
8896 FOR_EACH_FRAME (tail, frame)
8897 {
8898 f = XFRAME (frame);
8899 if (!EQ (frame, tooltip_frame)
8900 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8901 x_consider_frame_title (frame);
8902 }
8903 }
8904 #endif /* HAVE_WINDOW_SYSTEM */
8905
8906 /* Update the menu bar item lists, if appropriate. This has to be
8907 done before any actual redisplay or generation of display lines. */
8908 all_windows = (update_mode_lines
8909 || buffer_shared > 1
8910 || windows_or_buffers_changed);
8911 if (all_windows)
8912 {
8913 Lisp_Object tail, frame;
8914 int count = SPECPDL_INDEX ();
8915
8916 record_unwind_save_match_data ();
8917
8918 FOR_EACH_FRAME (tail, frame)
8919 {
8920 f = XFRAME (frame);
8921
8922 /* Ignore tooltip frame. */
8923 if (EQ (frame, tooltip_frame))
8924 continue;
8925
8926 /* If a window on this frame changed size, report that to
8927 the user and clear the size-change flag. */
8928 if (FRAME_WINDOW_SIZES_CHANGED (f))
8929 {
8930 Lisp_Object functions;
8931
8932 /* Clear flag first in case we get an error below. */
8933 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8934 functions = Vwindow_size_change_functions;
8935 GCPRO2 (tail, functions);
8936
8937 while (CONSP (functions))
8938 {
8939 call1 (XCAR (functions), frame);
8940 functions = XCDR (functions);
8941 }
8942 UNGCPRO;
8943 }
8944
8945 GCPRO1 (tail);
8946 update_menu_bar (f, 0);
8947 #ifdef HAVE_WINDOW_SYSTEM
8948 update_tool_bar (f, 0);
8949 #endif
8950 UNGCPRO;
8951 }
8952
8953 unbind_to (count, Qnil);
8954 }
8955 else
8956 {
8957 struct frame *sf = SELECTED_FRAME ();
8958 update_menu_bar (sf, 1);
8959 #ifdef HAVE_WINDOW_SYSTEM
8960 update_tool_bar (sf, 1);
8961 #endif
8962 }
8963
8964 /* Motif needs this. See comment in xmenu.c. Turn it off when
8965 pending_menu_activation is not defined. */
8966 #ifdef USE_X_TOOLKIT
8967 pending_menu_activation = 0;
8968 #endif
8969 }
8970
8971
8972 /* Update the menu bar item list for frame F. This has to be done
8973 before we start to fill in any display lines, because it can call
8974 eval.
8975
8976 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8977
8978 static void
8979 update_menu_bar (f, save_match_data)
8980 struct frame *f;
8981 int save_match_data;
8982 {
8983 Lisp_Object window;
8984 register struct window *w;
8985
8986 /* If called recursively during a menu update, do nothing. This can
8987 happen when, for instance, an activate-menubar-hook causes a
8988 redisplay. */
8989 if (inhibit_menubar_update)
8990 return;
8991
8992 window = FRAME_SELECTED_WINDOW (f);
8993 w = XWINDOW (window);
8994
8995 #if 0 /* The if statement below this if statement used to include the
8996 condition !NILP (w->update_mode_line), rather than using
8997 update_mode_lines directly, and this if statement may have
8998 been added to make that condition work. Now the if
8999 statement below matches its comment, this isn't needed. */
9000 if (update_mode_lines)
9001 w->update_mode_line = Qt;
9002 #endif
9003
9004 if (FRAME_WINDOW_P (f)
9005 ?
9006 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9007 || defined (USE_GTK)
9008 FRAME_EXTERNAL_MENU_BAR (f)
9009 #else
9010 FRAME_MENU_BAR_LINES (f) > 0
9011 #endif
9012 : FRAME_MENU_BAR_LINES (f) > 0)
9013 {
9014 /* If the user has switched buffers or windows, we need to
9015 recompute to reflect the new bindings. But we'll
9016 recompute when update_mode_lines is set too; that means
9017 that people can use force-mode-line-update to request
9018 that the menu bar be recomputed. The adverse effect on
9019 the rest of the redisplay algorithm is about the same as
9020 windows_or_buffers_changed anyway. */
9021 if (windows_or_buffers_changed
9022 /* This used to test w->update_mode_line, but we believe
9023 there is no need to recompute the menu in that case. */
9024 || update_mode_lines
9025 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9026 < BUF_MODIFF (XBUFFER (w->buffer)))
9027 != !NILP (w->last_had_star))
9028 || ((!NILP (Vtransient_mark_mode)
9029 && !NILP (XBUFFER (w->buffer)->mark_active))
9030 != !NILP (w->region_showing)))
9031 {
9032 struct buffer *prev = current_buffer;
9033 int count = SPECPDL_INDEX ();
9034
9035 specbind (Qinhibit_menubar_update, Qt);
9036
9037 set_buffer_internal_1 (XBUFFER (w->buffer));
9038 if (save_match_data)
9039 record_unwind_save_match_data ();
9040 if (NILP (Voverriding_local_map_menu_flag))
9041 {
9042 specbind (Qoverriding_terminal_local_map, Qnil);
9043 specbind (Qoverriding_local_map, Qnil);
9044 }
9045
9046 /* Run the Lucid hook. */
9047 safe_run_hooks (Qactivate_menubar_hook);
9048
9049 /* If it has changed current-menubar from previous value,
9050 really recompute the menu-bar from the value. */
9051 if (! NILP (Vlucid_menu_bar_dirty_flag))
9052 call0 (Qrecompute_lucid_menubar);
9053
9054 safe_run_hooks (Qmenu_bar_update_hook);
9055 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9056
9057 /* Redisplay the menu bar in case we changed it. */
9058 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9059 || defined (USE_GTK)
9060 if (FRAME_WINDOW_P (f))
9061 {
9062 #ifdef MAC_OS
9063 /* All frames on Mac OS share the same menubar. So only
9064 the selected frame should be allowed to set it. */
9065 if (f == SELECTED_FRAME ())
9066 #endif
9067 set_frame_menubar (f, 0, 0);
9068 }
9069 else
9070 /* On a terminal screen, the menu bar is an ordinary screen
9071 line, and this makes it get updated. */
9072 w->update_mode_line = Qt;
9073 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9074 /* In the non-toolkit version, the menu bar is an ordinary screen
9075 line, and this makes it get updated. */
9076 w->update_mode_line = Qt;
9077 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9078
9079 unbind_to (count, Qnil);
9080 set_buffer_internal_1 (prev);
9081 }
9082 }
9083 }
9084
9085
9086 \f
9087 /***********************************************************************
9088 Output Cursor
9089 ***********************************************************************/
9090
9091 #ifdef HAVE_WINDOW_SYSTEM
9092
9093 /* EXPORT:
9094 Nominal cursor position -- where to draw output.
9095 HPOS and VPOS are window relative glyph matrix coordinates.
9096 X and Y are window relative pixel coordinates. */
9097
9098 struct cursor_pos output_cursor;
9099
9100
9101 /* EXPORT:
9102 Set the global variable output_cursor to CURSOR. All cursor
9103 positions are relative to updated_window. */
9104
9105 void
9106 set_output_cursor (cursor)
9107 struct cursor_pos *cursor;
9108 {
9109 output_cursor.hpos = cursor->hpos;
9110 output_cursor.vpos = cursor->vpos;
9111 output_cursor.x = cursor->x;
9112 output_cursor.y = cursor->y;
9113 }
9114
9115
9116 /* EXPORT for RIF:
9117 Set a nominal cursor position.
9118
9119 HPOS and VPOS are column/row positions in a window glyph matrix. X
9120 and Y are window text area relative pixel positions.
9121
9122 If this is done during an update, updated_window will contain the
9123 window that is being updated and the position is the future output
9124 cursor position for that window. If updated_window is null, use
9125 selected_window and display the cursor at the given position. */
9126
9127 void
9128 x_cursor_to (vpos, hpos, y, x)
9129 int vpos, hpos, y, x;
9130 {
9131 struct window *w;
9132
9133 /* If updated_window is not set, work on selected_window. */
9134 if (updated_window)
9135 w = updated_window;
9136 else
9137 w = XWINDOW (selected_window);
9138
9139 /* Set the output cursor. */
9140 output_cursor.hpos = hpos;
9141 output_cursor.vpos = vpos;
9142 output_cursor.x = x;
9143 output_cursor.y = y;
9144
9145 /* If not called as part of an update, really display the cursor.
9146 This will also set the cursor position of W. */
9147 if (updated_window == NULL)
9148 {
9149 BLOCK_INPUT;
9150 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9151 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9152 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9153 UNBLOCK_INPUT;
9154 }
9155 }
9156
9157 #endif /* HAVE_WINDOW_SYSTEM */
9158
9159 \f
9160 /***********************************************************************
9161 Tool-bars
9162 ***********************************************************************/
9163
9164 #ifdef HAVE_WINDOW_SYSTEM
9165
9166 /* Where the mouse was last time we reported a mouse event. */
9167
9168 FRAME_PTR last_mouse_frame;
9169
9170 /* Tool-bar item index of the item on which a mouse button was pressed
9171 or -1. */
9172
9173 int last_tool_bar_item;
9174
9175
9176 /* Update the tool-bar item list for frame F. This has to be done
9177 before we start to fill in any display lines. Called from
9178 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9179 and restore it here. */
9180
9181 static void
9182 update_tool_bar (f, save_match_data)
9183 struct frame *f;
9184 int save_match_data;
9185 {
9186 #ifdef USE_GTK
9187 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9188 #else
9189 int do_update = WINDOWP (f->tool_bar_window)
9190 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9191 #endif
9192
9193 if (do_update)
9194 {
9195 Lisp_Object window;
9196 struct window *w;
9197
9198 window = FRAME_SELECTED_WINDOW (f);
9199 w = XWINDOW (window);
9200
9201 /* If the user has switched buffers or windows, we need to
9202 recompute to reflect the new bindings. But we'll
9203 recompute when update_mode_lines is set too; that means
9204 that people can use force-mode-line-update to request
9205 that the menu bar be recomputed. The adverse effect on
9206 the rest of the redisplay algorithm is about the same as
9207 windows_or_buffers_changed anyway. */
9208 if (windows_or_buffers_changed
9209 || !NILP (w->update_mode_line)
9210 || update_mode_lines
9211 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9212 < BUF_MODIFF (XBUFFER (w->buffer)))
9213 != !NILP (w->last_had_star))
9214 || ((!NILP (Vtransient_mark_mode)
9215 && !NILP (XBUFFER (w->buffer)->mark_active))
9216 != !NILP (w->region_showing)))
9217 {
9218 struct buffer *prev = current_buffer;
9219 int count = SPECPDL_INDEX ();
9220 Lisp_Object new_tool_bar;
9221 int new_n_tool_bar;
9222 struct gcpro gcpro1;
9223
9224 /* Set current_buffer to the buffer of the selected
9225 window of the frame, so that we get the right local
9226 keymaps. */
9227 set_buffer_internal_1 (XBUFFER (w->buffer));
9228
9229 /* Save match data, if we must. */
9230 if (save_match_data)
9231 record_unwind_save_match_data ();
9232
9233 /* Make sure that we don't accidentally use bogus keymaps. */
9234 if (NILP (Voverriding_local_map_menu_flag))
9235 {
9236 specbind (Qoverriding_terminal_local_map, Qnil);
9237 specbind (Qoverriding_local_map, Qnil);
9238 }
9239
9240 GCPRO1 (new_tool_bar);
9241
9242 /* Build desired tool-bar items from keymaps. */
9243 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9244 &new_n_tool_bar);
9245
9246 /* Redisplay the tool-bar if we changed it. */
9247 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9248 {
9249 /* Redisplay that happens asynchronously due to an expose event
9250 may access f->tool_bar_items. Make sure we update both
9251 variables within BLOCK_INPUT so no such event interrupts. */
9252 BLOCK_INPUT;
9253 f->tool_bar_items = new_tool_bar;
9254 f->n_tool_bar_items = new_n_tool_bar;
9255 w->update_mode_line = Qt;
9256 UNBLOCK_INPUT;
9257 }
9258
9259 UNGCPRO;
9260
9261 unbind_to (count, Qnil);
9262 set_buffer_internal_1 (prev);
9263 }
9264 }
9265 }
9266
9267
9268 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9269 F's desired tool-bar contents. F->tool_bar_items must have
9270 been set up previously by calling prepare_menu_bars. */
9271
9272 static void
9273 build_desired_tool_bar_string (f)
9274 struct frame *f;
9275 {
9276 int i, size, size_needed;
9277 struct gcpro gcpro1, gcpro2, gcpro3;
9278 Lisp_Object image, plist, props;
9279
9280 image = plist = props = Qnil;
9281 GCPRO3 (image, plist, props);
9282
9283 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9284 Otherwise, make a new string. */
9285
9286 /* The size of the string we might be able to reuse. */
9287 size = (STRINGP (f->desired_tool_bar_string)
9288 ? SCHARS (f->desired_tool_bar_string)
9289 : 0);
9290
9291 /* We need one space in the string for each image. */
9292 size_needed = f->n_tool_bar_items;
9293
9294 /* Reuse f->desired_tool_bar_string, if possible. */
9295 if (size < size_needed || NILP (f->desired_tool_bar_string))
9296 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9297 make_number (' '));
9298 else
9299 {
9300 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9301 Fremove_text_properties (make_number (0), make_number (size),
9302 props, f->desired_tool_bar_string);
9303 }
9304
9305 /* Put a `display' property on the string for the images to display,
9306 put a `menu_item' property on tool-bar items with a value that
9307 is the index of the item in F's tool-bar item vector. */
9308 for (i = 0; i < f->n_tool_bar_items; ++i)
9309 {
9310 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9311
9312 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9313 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9314 int hmargin, vmargin, relief, idx, end;
9315 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9316
9317 /* If image is a vector, choose the image according to the
9318 button state. */
9319 image = PROP (TOOL_BAR_ITEM_IMAGES);
9320 if (VECTORP (image))
9321 {
9322 if (enabled_p)
9323 idx = (selected_p
9324 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9325 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9326 else
9327 idx = (selected_p
9328 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9329 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9330
9331 xassert (ASIZE (image) >= idx);
9332 image = AREF (image, idx);
9333 }
9334 else
9335 idx = -1;
9336
9337 /* Ignore invalid image specifications. */
9338 if (!valid_image_p (image))
9339 continue;
9340
9341 /* Display the tool-bar button pressed, or depressed. */
9342 plist = Fcopy_sequence (XCDR (image));
9343
9344 /* Compute margin and relief to draw. */
9345 relief = (tool_bar_button_relief >= 0
9346 ? tool_bar_button_relief
9347 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9348 hmargin = vmargin = relief;
9349
9350 if (INTEGERP (Vtool_bar_button_margin)
9351 && XINT (Vtool_bar_button_margin) > 0)
9352 {
9353 hmargin += XFASTINT (Vtool_bar_button_margin);
9354 vmargin += XFASTINT (Vtool_bar_button_margin);
9355 }
9356 else if (CONSP (Vtool_bar_button_margin))
9357 {
9358 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9359 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9360 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9361
9362 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9363 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9364 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9365 }
9366
9367 if (auto_raise_tool_bar_buttons_p)
9368 {
9369 /* Add a `:relief' property to the image spec if the item is
9370 selected. */
9371 if (selected_p)
9372 {
9373 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9374 hmargin -= relief;
9375 vmargin -= relief;
9376 }
9377 }
9378 else
9379 {
9380 /* If image is selected, display it pressed, i.e. with a
9381 negative relief. If it's not selected, display it with a
9382 raised relief. */
9383 plist = Fplist_put (plist, QCrelief,
9384 (selected_p
9385 ? make_number (-relief)
9386 : make_number (relief)));
9387 hmargin -= relief;
9388 vmargin -= relief;
9389 }
9390
9391 /* Put a margin around the image. */
9392 if (hmargin || vmargin)
9393 {
9394 if (hmargin == vmargin)
9395 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9396 else
9397 plist = Fplist_put (plist, QCmargin,
9398 Fcons (make_number (hmargin),
9399 make_number (vmargin)));
9400 }
9401
9402 /* If button is not enabled, and we don't have special images
9403 for the disabled state, make the image appear disabled by
9404 applying an appropriate algorithm to it. */
9405 if (!enabled_p && idx < 0)
9406 plist = Fplist_put (plist, QCconversion, Qdisabled);
9407
9408 /* Put a `display' text property on the string for the image to
9409 display. Put a `menu-item' property on the string that gives
9410 the start of this item's properties in the tool-bar items
9411 vector. */
9412 image = Fcons (Qimage, plist);
9413 props = list4 (Qdisplay, image,
9414 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9415
9416 /* Let the last image hide all remaining spaces in the tool bar
9417 string. The string can be longer than needed when we reuse a
9418 previous string. */
9419 if (i + 1 == f->n_tool_bar_items)
9420 end = SCHARS (f->desired_tool_bar_string);
9421 else
9422 end = i + 1;
9423 Fadd_text_properties (make_number (i), make_number (end),
9424 props, f->desired_tool_bar_string);
9425 #undef PROP
9426 }
9427
9428 UNGCPRO;
9429 }
9430
9431
9432 /* Display one line of the tool-bar of frame IT->f. */
9433
9434 static void
9435 display_tool_bar_line (it)
9436 struct it *it;
9437 {
9438 struct glyph_row *row = it->glyph_row;
9439 int max_x = it->last_visible_x;
9440 struct glyph *last;
9441
9442 prepare_desired_row (row);
9443 row->y = it->current_y;
9444
9445 /* Note that this isn't made use of if the face hasn't a box,
9446 so there's no need to check the face here. */
9447 it->start_of_box_run_p = 1;
9448
9449 while (it->current_x < max_x)
9450 {
9451 int x_before, x, n_glyphs_before, i, nglyphs;
9452
9453 /* Get the next display element. */
9454 if (!get_next_display_element (it))
9455 break;
9456
9457 /* Produce glyphs. */
9458 x_before = it->current_x;
9459 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9460 PRODUCE_GLYPHS (it);
9461
9462 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9463 i = 0;
9464 x = x_before;
9465 while (i < nglyphs)
9466 {
9467 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9468
9469 if (x + glyph->pixel_width > max_x)
9470 {
9471 /* Glyph doesn't fit on line. */
9472 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9473 it->current_x = x;
9474 goto out;
9475 }
9476
9477 ++it->hpos;
9478 x += glyph->pixel_width;
9479 ++i;
9480 }
9481
9482 /* Stop at line ends. */
9483 if (ITERATOR_AT_END_OF_LINE_P (it))
9484 break;
9485
9486 set_iterator_to_next (it, 1);
9487 }
9488
9489 out:;
9490
9491 row->displays_text_p = row->used[TEXT_AREA] != 0;
9492 extend_face_to_end_of_line (it);
9493 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9494 last->right_box_line_p = 1;
9495 if (last == row->glyphs[TEXT_AREA])
9496 last->left_box_line_p = 1;
9497 compute_line_metrics (it);
9498
9499 /* If line is empty, make it occupy the rest of the tool-bar. */
9500 if (!row->displays_text_p)
9501 {
9502 row->height = row->phys_height = it->last_visible_y - row->y;
9503 row->ascent = row->phys_ascent = 0;
9504 row->extra_line_spacing = 0;
9505 }
9506
9507 row->full_width_p = 1;
9508 row->continued_p = 0;
9509 row->truncated_on_left_p = 0;
9510 row->truncated_on_right_p = 0;
9511
9512 it->current_x = it->hpos = 0;
9513 it->current_y += row->height;
9514 ++it->vpos;
9515 ++it->glyph_row;
9516 }
9517
9518
9519 /* Value is the number of screen lines needed to make all tool-bar
9520 items of frame F visible. */
9521
9522 static int
9523 tool_bar_lines_needed (f)
9524 struct frame *f;
9525 {
9526 struct window *w = XWINDOW (f->tool_bar_window);
9527 struct it it;
9528
9529 /* Initialize an iterator for iteration over
9530 F->desired_tool_bar_string in the tool-bar window of frame F. */
9531 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9532 it.first_visible_x = 0;
9533 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9534 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9535
9536 while (!ITERATOR_AT_END_P (&it))
9537 {
9538 it.glyph_row = w->desired_matrix->rows;
9539 clear_glyph_row (it.glyph_row);
9540 display_tool_bar_line (&it);
9541 }
9542
9543 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9544 }
9545
9546
9547 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9548 0, 1, 0,
9549 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9550 (frame)
9551 Lisp_Object frame;
9552 {
9553 struct frame *f;
9554 struct window *w;
9555 int nlines = 0;
9556
9557 if (NILP (frame))
9558 frame = selected_frame;
9559 else
9560 CHECK_FRAME (frame);
9561 f = XFRAME (frame);
9562
9563 if (WINDOWP (f->tool_bar_window)
9564 || (w = XWINDOW (f->tool_bar_window),
9565 WINDOW_TOTAL_LINES (w) > 0))
9566 {
9567 update_tool_bar (f, 1);
9568 if (f->n_tool_bar_items)
9569 {
9570 build_desired_tool_bar_string (f);
9571 nlines = tool_bar_lines_needed (f);
9572 }
9573 }
9574
9575 return make_number (nlines);
9576 }
9577
9578
9579 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9580 height should be changed. */
9581
9582 static int
9583 redisplay_tool_bar (f)
9584 struct frame *f;
9585 {
9586 struct window *w;
9587 struct it it;
9588 struct glyph_row *row;
9589 int change_height_p = 0;
9590
9591 #ifdef USE_GTK
9592 if (FRAME_EXTERNAL_TOOL_BAR (f))
9593 update_frame_tool_bar (f);
9594 return 0;
9595 #endif
9596
9597 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9598 do anything. This means you must start with tool-bar-lines
9599 non-zero to get the auto-sizing effect. Or in other words, you
9600 can turn off tool-bars by specifying tool-bar-lines zero. */
9601 if (!WINDOWP (f->tool_bar_window)
9602 || (w = XWINDOW (f->tool_bar_window),
9603 WINDOW_TOTAL_LINES (w) == 0))
9604 return 0;
9605
9606 /* Set up an iterator for the tool-bar window. */
9607 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9608 it.first_visible_x = 0;
9609 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9610 row = it.glyph_row;
9611
9612 /* Build a string that represents the contents of the tool-bar. */
9613 build_desired_tool_bar_string (f);
9614 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9615
9616 /* Display as many lines as needed to display all tool-bar items. */
9617 while (it.current_y < it.last_visible_y)
9618 display_tool_bar_line (&it);
9619
9620 /* It doesn't make much sense to try scrolling in the tool-bar
9621 window, so don't do it. */
9622 w->desired_matrix->no_scrolling_p = 1;
9623 w->must_be_updated_p = 1;
9624
9625 if (auto_resize_tool_bars_p)
9626 {
9627 int nlines;
9628
9629 /* If we couldn't display everything, change the tool-bar's
9630 height. */
9631 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9632 change_height_p = 1;
9633
9634 /* If there are blank lines at the end, except for a partially
9635 visible blank line at the end that is smaller than
9636 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9637 row = it.glyph_row - 1;
9638 if (!row->displays_text_p
9639 && row->height >= FRAME_LINE_HEIGHT (f))
9640 change_height_p = 1;
9641
9642 /* If row displays tool-bar items, but is partially visible,
9643 change the tool-bar's height. */
9644 if (row->displays_text_p
9645 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9646 change_height_p = 1;
9647
9648 /* Resize windows as needed by changing the `tool-bar-lines'
9649 frame parameter. */
9650 if (change_height_p
9651 && (nlines = tool_bar_lines_needed (f),
9652 nlines != WINDOW_TOTAL_LINES (w)))
9653 {
9654 extern Lisp_Object Qtool_bar_lines;
9655 Lisp_Object frame;
9656 int old_height = WINDOW_TOTAL_LINES (w);
9657
9658 XSETFRAME (frame, f);
9659 clear_glyph_matrix (w->desired_matrix);
9660 Fmodify_frame_parameters (frame,
9661 Fcons (Fcons (Qtool_bar_lines,
9662 make_number (nlines)),
9663 Qnil));
9664 if (WINDOW_TOTAL_LINES (w) != old_height)
9665 fonts_changed_p = 1;
9666 }
9667 }
9668
9669 return change_height_p;
9670 }
9671
9672
9673 /* Get information about the tool-bar item which is displayed in GLYPH
9674 on frame F. Return in *PROP_IDX the index where tool-bar item
9675 properties start in F->tool_bar_items. Value is zero if
9676 GLYPH doesn't display a tool-bar item. */
9677
9678 static int
9679 tool_bar_item_info (f, glyph, prop_idx)
9680 struct frame *f;
9681 struct glyph *glyph;
9682 int *prop_idx;
9683 {
9684 Lisp_Object prop;
9685 int success_p;
9686 int charpos;
9687
9688 /* This function can be called asynchronously, which means we must
9689 exclude any possibility that Fget_text_property signals an
9690 error. */
9691 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9692 charpos = max (0, charpos);
9693
9694 /* Get the text property `menu-item' at pos. The value of that
9695 property is the start index of this item's properties in
9696 F->tool_bar_items. */
9697 prop = Fget_text_property (make_number (charpos),
9698 Qmenu_item, f->current_tool_bar_string);
9699 if (INTEGERP (prop))
9700 {
9701 *prop_idx = XINT (prop);
9702 success_p = 1;
9703 }
9704 else
9705 success_p = 0;
9706
9707 return success_p;
9708 }
9709
9710 \f
9711 /* Get information about the tool-bar item at position X/Y on frame F.
9712 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9713 the current matrix of the tool-bar window of F, or NULL if not
9714 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9715 item in F->tool_bar_items. Value is
9716
9717 -1 if X/Y is not on a tool-bar item
9718 0 if X/Y is on the same item that was highlighted before.
9719 1 otherwise. */
9720
9721 static int
9722 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9723 struct frame *f;
9724 int x, y;
9725 struct glyph **glyph;
9726 int *hpos, *vpos, *prop_idx;
9727 {
9728 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9729 struct window *w = XWINDOW (f->tool_bar_window);
9730 int area;
9731
9732 /* Find the glyph under X/Y. */
9733 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9734 if (*glyph == NULL)
9735 return -1;
9736
9737 /* Get the start of this tool-bar item's properties in
9738 f->tool_bar_items. */
9739 if (!tool_bar_item_info (f, *glyph, prop_idx))
9740 return -1;
9741
9742 /* Is mouse on the highlighted item? */
9743 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9744 && *vpos >= dpyinfo->mouse_face_beg_row
9745 && *vpos <= dpyinfo->mouse_face_end_row
9746 && (*vpos > dpyinfo->mouse_face_beg_row
9747 || *hpos >= dpyinfo->mouse_face_beg_col)
9748 && (*vpos < dpyinfo->mouse_face_end_row
9749 || *hpos < dpyinfo->mouse_face_end_col
9750 || dpyinfo->mouse_face_past_end))
9751 return 0;
9752
9753 return 1;
9754 }
9755
9756
9757 /* EXPORT:
9758 Handle mouse button event on the tool-bar of frame F, at
9759 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9760 0 for button release. MODIFIERS is event modifiers for button
9761 release. */
9762
9763 void
9764 handle_tool_bar_click (f, x, y, down_p, modifiers)
9765 struct frame *f;
9766 int x, y, down_p;
9767 unsigned int modifiers;
9768 {
9769 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9770 struct window *w = XWINDOW (f->tool_bar_window);
9771 int hpos, vpos, prop_idx;
9772 struct glyph *glyph;
9773 Lisp_Object enabled_p;
9774
9775 /* If not on the highlighted tool-bar item, return. */
9776 frame_to_window_pixel_xy (w, &x, &y);
9777 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9778 return;
9779
9780 /* If item is disabled, do nothing. */
9781 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9782 if (NILP (enabled_p))
9783 return;
9784
9785 if (down_p)
9786 {
9787 /* Show item in pressed state. */
9788 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9789 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9790 last_tool_bar_item = prop_idx;
9791 }
9792 else
9793 {
9794 Lisp_Object key, frame;
9795 struct input_event event;
9796 EVENT_INIT (event);
9797
9798 /* Show item in released state. */
9799 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9800 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9801
9802 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9803
9804 XSETFRAME (frame, f);
9805 event.kind = TOOL_BAR_EVENT;
9806 event.frame_or_window = frame;
9807 event.arg = frame;
9808 kbd_buffer_store_event (&event);
9809
9810 event.kind = TOOL_BAR_EVENT;
9811 event.frame_or_window = frame;
9812 event.arg = key;
9813 event.modifiers = modifiers;
9814 kbd_buffer_store_event (&event);
9815 last_tool_bar_item = -1;
9816 }
9817 }
9818
9819
9820 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9821 tool-bar window-relative coordinates X/Y. Called from
9822 note_mouse_highlight. */
9823
9824 static void
9825 note_tool_bar_highlight (f, x, y)
9826 struct frame *f;
9827 int x, y;
9828 {
9829 Lisp_Object window = f->tool_bar_window;
9830 struct window *w = XWINDOW (window);
9831 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9832 int hpos, vpos;
9833 struct glyph *glyph;
9834 struct glyph_row *row;
9835 int i;
9836 Lisp_Object enabled_p;
9837 int prop_idx;
9838 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9839 int mouse_down_p, rc;
9840
9841 /* Function note_mouse_highlight is called with negative x(y
9842 values when mouse moves outside of the frame. */
9843 if (x <= 0 || y <= 0)
9844 {
9845 clear_mouse_face (dpyinfo);
9846 return;
9847 }
9848
9849 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9850 if (rc < 0)
9851 {
9852 /* Not on tool-bar item. */
9853 clear_mouse_face (dpyinfo);
9854 return;
9855 }
9856 else if (rc == 0)
9857 /* On same tool-bar item as before. */
9858 goto set_help_echo;
9859
9860 clear_mouse_face (dpyinfo);
9861
9862 /* Mouse is down, but on different tool-bar item? */
9863 mouse_down_p = (dpyinfo->grabbed
9864 && f == last_mouse_frame
9865 && FRAME_LIVE_P (f));
9866 if (mouse_down_p
9867 && last_tool_bar_item != prop_idx)
9868 return;
9869
9870 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9871 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9872
9873 /* If tool-bar item is not enabled, don't highlight it. */
9874 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9875 if (!NILP (enabled_p))
9876 {
9877 /* Compute the x-position of the glyph. In front and past the
9878 image is a space. We include this in the highlighted area. */
9879 row = MATRIX_ROW (w->current_matrix, vpos);
9880 for (i = x = 0; i < hpos; ++i)
9881 x += row->glyphs[TEXT_AREA][i].pixel_width;
9882
9883 /* Record this as the current active region. */
9884 dpyinfo->mouse_face_beg_col = hpos;
9885 dpyinfo->mouse_face_beg_row = vpos;
9886 dpyinfo->mouse_face_beg_x = x;
9887 dpyinfo->mouse_face_beg_y = row->y;
9888 dpyinfo->mouse_face_past_end = 0;
9889
9890 dpyinfo->mouse_face_end_col = hpos + 1;
9891 dpyinfo->mouse_face_end_row = vpos;
9892 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9893 dpyinfo->mouse_face_end_y = row->y;
9894 dpyinfo->mouse_face_window = window;
9895 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9896
9897 /* Display it as active. */
9898 show_mouse_face (dpyinfo, draw);
9899 dpyinfo->mouse_face_image_state = draw;
9900 }
9901
9902 set_help_echo:
9903
9904 /* Set help_echo_string to a help string to display for this tool-bar item.
9905 XTread_socket does the rest. */
9906 help_echo_object = help_echo_window = Qnil;
9907 help_echo_pos = -1;
9908 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9909 if (NILP (help_echo_string))
9910 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9911 }
9912
9913 #endif /* HAVE_WINDOW_SYSTEM */
9914
9915
9916 \f
9917 /************************************************************************
9918 Horizontal scrolling
9919 ************************************************************************/
9920
9921 static int hscroll_window_tree P_ ((Lisp_Object));
9922 static int hscroll_windows P_ ((Lisp_Object));
9923
9924 /* For all leaf windows in the window tree rooted at WINDOW, set their
9925 hscroll value so that PT is (i) visible in the window, and (ii) so
9926 that it is not within a certain margin at the window's left and
9927 right border. Value is non-zero if any window's hscroll has been
9928 changed. */
9929
9930 static int
9931 hscroll_window_tree (window)
9932 Lisp_Object window;
9933 {
9934 int hscrolled_p = 0;
9935 int hscroll_relative_p = FLOATP (Vhscroll_step);
9936 int hscroll_step_abs = 0;
9937 double hscroll_step_rel = 0;
9938
9939 if (hscroll_relative_p)
9940 {
9941 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9942 if (hscroll_step_rel < 0)
9943 {
9944 hscroll_relative_p = 0;
9945 hscroll_step_abs = 0;
9946 }
9947 }
9948 else if (INTEGERP (Vhscroll_step))
9949 {
9950 hscroll_step_abs = XINT (Vhscroll_step);
9951 if (hscroll_step_abs < 0)
9952 hscroll_step_abs = 0;
9953 }
9954 else
9955 hscroll_step_abs = 0;
9956
9957 while (WINDOWP (window))
9958 {
9959 struct window *w = XWINDOW (window);
9960
9961 if (WINDOWP (w->hchild))
9962 hscrolled_p |= hscroll_window_tree (w->hchild);
9963 else if (WINDOWP (w->vchild))
9964 hscrolled_p |= hscroll_window_tree (w->vchild);
9965 else if (w->cursor.vpos >= 0)
9966 {
9967 int h_margin;
9968 int text_area_width;
9969 struct glyph_row *current_cursor_row
9970 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9971 struct glyph_row *desired_cursor_row
9972 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9973 struct glyph_row *cursor_row
9974 = (desired_cursor_row->enabled_p
9975 ? desired_cursor_row
9976 : current_cursor_row);
9977
9978 text_area_width = window_box_width (w, TEXT_AREA);
9979
9980 /* Scroll when cursor is inside this scroll margin. */
9981 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9982
9983 if ((XFASTINT (w->hscroll)
9984 && w->cursor.x <= h_margin)
9985 || (cursor_row->enabled_p
9986 && cursor_row->truncated_on_right_p
9987 && (w->cursor.x >= text_area_width - h_margin)))
9988 {
9989 struct it it;
9990 int hscroll;
9991 struct buffer *saved_current_buffer;
9992 int pt;
9993 int wanted_x;
9994
9995 /* Find point in a display of infinite width. */
9996 saved_current_buffer = current_buffer;
9997 current_buffer = XBUFFER (w->buffer);
9998
9999 if (w == XWINDOW (selected_window))
10000 pt = BUF_PT (current_buffer);
10001 else
10002 {
10003 pt = marker_position (w->pointm);
10004 pt = max (BEGV, pt);
10005 pt = min (ZV, pt);
10006 }
10007
10008 /* Move iterator to pt starting at cursor_row->start in
10009 a line with infinite width. */
10010 init_to_row_start (&it, w, cursor_row);
10011 it.last_visible_x = INFINITY;
10012 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10013 current_buffer = saved_current_buffer;
10014
10015 /* Position cursor in window. */
10016 if (!hscroll_relative_p && hscroll_step_abs == 0)
10017 hscroll = max (0, (it.current_x
10018 - (ITERATOR_AT_END_OF_LINE_P (&it)
10019 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10020 : (text_area_width / 2))))
10021 / FRAME_COLUMN_WIDTH (it.f);
10022 else if (w->cursor.x >= text_area_width - h_margin)
10023 {
10024 if (hscroll_relative_p)
10025 wanted_x = text_area_width * (1 - hscroll_step_rel)
10026 - h_margin;
10027 else
10028 wanted_x = text_area_width
10029 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10030 - h_margin;
10031 hscroll
10032 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10033 }
10034 else
10035 {
10036 if (hscroll_relative_p)
10037 wanted_x = text_area_width * hscroll_step_rel
10038 + h_margin;
10039 else
10040 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10041 + h_margin;
10042 hscroll
10043 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10044 }
10045 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10046
10047 /* Don't call Fset_window_hscroll if value hasn't
10048 changed because it will prevent redisplay
10049 optimizations. */
10050 if (XFASTINT (w->hscroll) != hscroll)
10051 {
10052 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10053 w->hscroll = make_number (hscroll);
10054 hscrolled_p = 1;
10055 }
10056 }
10057 }
10058
10059 window = w->next;
10060 }
10061
10062 /* Value is non-zero if hscroll of any leaf window has been changed. */
10063 return hscrolled_p;
10064 }
10065
10066
10067 /* Set hscroll so that cursor is visible and not inside horizontal
10068 scroll margins for all windows in the tree rooted at WINDOW. See
10069 also hscroll_window_tree above. Value is non-zero if any window's
10070 hscroll has been changed. If it has, desired matrices on the frame
10071 of WINDOW are cleared. */
10072
10073 static int
10074 hscroll_windows (window)
10075 Lisp_Object window;
10076 {
10077 int hscrolled_p;
10078
10079 if (automatic_hscrolling_p)
10080 {
10081 hscrolled_p = hscroll_window_tree (window);
10082 if (hscrolled_p)
10083 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10084 }
10085 else
10086 hscrolled_p = 0;
10087 return hscrolled_p;
10088 }
10089
10090
10091 \f
10092 /************************************************************************
10093 Redisplay
10094 ************************************************************************/
10095
10096 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10097 to a non-zero value. This is sometimes handy to have in a debugger
10098 session. */
10099
10100 #if GLYPH_DEBUG
10101
10102 /* First and last unchanged row for try_window_id. */
10103
10104 int debug_first_unchanged_at_end_vpos;
10105 int debug_last_unchanged_at_beg_vpos;
10106
10107 /* Delta vpos and y. */
10108
10109 int debug_dvpos, debug_dy;
10110
10111 /* Delta in characters and bytes for try_window_id. */
10112
10113 int debug_delta, debug_delta_bytes;
10114
10115 /* Values of window_end_pos and window_end_vpos at the end of
10116 try_window_id. */
10117
10118 EMACS_INT debug_end_pos, debug_end_vpos;
10119
10120 /* Append a string to W->desired_matrix->method. FMT is a printf
10121 format string. A1...A9 are a supplement for a variable-length
10122 argument list. If trace_redisplay_p is non-zero also printf the
10123 resulting string to stderr. */
10124
10125 static void
10126 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10127 struct window *w;
10128 char *fmt;
10129 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10130 {
10131 char buffer[512];
10132 char *method = w->desired_matrix->method;
10133 int len = strlen (method);
10134 int size = sizeof w->desired_matrix->method;
10135 int remaining = size - len - 1;
10136
10137 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10138 if (len && remaining)
10139 {
10140 method[len] = '|';
10141 --remaining, ++len;
10142 }
10143
10144 strncpy (method + len, buffer, remaining);
10145
10146 if (trace_redisplay_p)
10147 fprintf (stderr, "%p (%s): %s\n",
10148 w,
10149 ((BUFFERP (w->buffer)
10150 && STRINGP (XBUFFER (w->buffer)->name))
10151 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10152 : "no buffer"),
10153 buffer);
10154 }
10155
10156 #endif /* GLYPH_DEBUG */
10157
10158
10159 /* Value is non-zero if all changes in window W, which displays
10160 current_buffer, are in the text between START and END. START is a
10161 buffer position, END is given as a distance from Z. Used in
10162 redisplay_internal for display optimization. */
10163
10164 static INLINE int
10165 text_outside_line_unchanged_p (w, start, end)
10166 struct window *w;
10167 int start, end;
10168 {
10169 int unchanged_p = 1;
10170
10171 /* If text or overlays have changed, see where. */
10172 if (XFASTINT (w->last_modified) < MODIFF
10173 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10174 {
10175 /* Gap in the line? */
10176 if (GPT < start || Z - GPT < end)
10177 unchanged_p = 0;
10178
10179 /* Changes start in front of the line, or end after it? */
10180 if (unchanged_p
10181 && (BEG_UNCHANGED < start - 1
10182 || END_UNCHANGED < end))
10183 unchanged_p = 0;
10184
10185 /* If selective display, can't optimize if changes start at the
10186 beginning of the line. */
10187 if (unchanged_p
10188 && INTEGERP (current_buffer->selective_display)
10189 && XINT (current_buffer->selective_display) > 0
10190 && (BEG_UNCHANGED < start || GPT <= start))
10191 unchanged_p = 0;
10192
10193 /* If there are overlays at the start or end of the line, these
10194 may have overlay strings with newlines in them. A change at
10195 START, for instance, may actually concern the display of such
10196 overlay strings as well, and they are displayed on different
10197 lines. So, quickly rule out this case. (For the future, it
10198 might be desirable to implement something more telling than
10199 just BEG/END_UNCHANGED.) */
10200 if (unchanged_p)
10201 {
10202 if (BEG + BEG_UNCHANGED == start
10203 && overlay_touches_p (start))
10204 unchanged_p = 0;
10205 if (END_UNCHANGED == end
10206 && overlay_touches_p (Z - end))
10207 unchanged_p = 0;
10208 }
10209 }
10210
10211 return unchanged_p;
10212 }
10213
10214
10215 /* Do a frame update, taking possible shortcuts into account. This is
10216 the main external entry point for redisplay.
10217
10218 If the last redisplay displayed an echo area message and that message
10219 is no longer requested, we clear the echo area or bring back the
10220 mini-buffer if that is in use. */
10221
10222 void
10223 redisplay ()
10224 {
10225 redisplay_internal (0);
10226 }
10227
10228
10229 static Lisp_Object
10230 overlay_arrow_string_or_property (var)
10231 Lisp_Object var;
10232 {
10233 Lisp_Object val;
10234
10235 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10236 return val;
10237
10238 return Voverlay_arrow_string;
10239 }
10240
10241 /* Return 1 if there are any overlay-arrows in current_buffer. */
10242 static int
10243 overlay_arrow_in_current_buffer_p ()
10244 {
10245 Lisp_Object vlist;
10246
10247 for (vlist = Voverlay_arrow_variable_list;
10248 CONSP (vlist);
10249 vlist = XCDR (vlist))
10250 {
10251 Lisp_Object var = XCAR (vlist);
10252 Lisp_Object val;
10253
10254 if (!SYMBOLP (var))
10255 continue;
10256 val = find_symbol_value (var);
10257 if (MARKERP (val)
10258 && current_buffer == XMARKER (val)->buffer)
10259 return 1;
10260 }
10261 return 0;
10262 }
10263
10264
10265 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10266 has changed. */
10267
10268 static int
10269 overlay_arrows_changed_p ()
10270 {
10271 Lisp_Object vlist;
10272
10273 for (vlist = Voverlay_arrow_variable_list;
10274 CONSP (vlist);
10275 vlist = XCDR (vlist))
10276 {
10277 Lisp_Object var = XCAR (vlist);
10278 Lisp_Object val, pstr;
10279
10280 if (!SYMBOLP (var))
10281 continue;
10282 val = find_symbol_value (var);
10283 if (!MARKERP (val))
10284 continue;
10285 if (! EQ (COERCE_MARKER (val),
10286 Fget (var, Qlast_arrow_position))
10287 || ! (pstr = overlay_arrow_string_or_property (var),
10288 EQ (pstr, Fget (var, Qlast_arrow_string))))
10289 return 1;
10290 }
10291 return 0;
10292 }
10293
10294 /* Mark overlay arrows to be updated on next redisplay. */
10295
10296 static void
10297 update_overlay_arrows (up_to_date)
10298 int up_to_date;
10299 {
10300 Lisp_Object vlist;
10301
10302 for (vlist = Voverlay_arrow_variable_list;
10303 CONSP (vlist);
10304 vlist = XCDR (vlist))
10305 {
10306 Lisp_Object var = XCAR (vlist);
10307
10308 if (!SYMBOLP (var))
10309 continue;
10310
10311 if (up_to_date > 0)
10312 {
10313 Lisp_Object val = find_symbol_value (var);
10314 Fput (var, Qlast_arrow_position,
10315 COERCE_MARKER (val));
10316 Fput (var, Qlast_arrow_string,
10317 overlay_arrow_string_or_property (var));
10318 }
10319 else if (up_to_date < 0
10320 || !NILP (Fget (var, Qlast_arrow_position)))
10321 {
10322 Fput (var, Qlast_arrow_position, Qt);
10323 Fput (var, Qlast_arrow_string, Qt);
10324 }
10325 }
10326 }
10327
10328
10329 /* Return overlay arrow string to display at row.
10330 Return integer (bitmap number) for arrow bitmap in left fringe.
10331 Return nil if no overlay arrow. */
10332
10333 static Lisp_Object
10334 overlay_arrow_at_row (it, row)
10335 struct it *it;
10336 struct glyph_row *row;
10337 {
10338 Lisp_Object vlist;
10339
10340 for (vlist = Voverlay_arrow_variable_list;
10341 CONSP (vlist);
10342 vlist = XCDR (vlist))
10343 {
10344 Lisp_Object var = XCAR (vlist);
10345 Lisp_Object val;
10346
10347 if (!SYMBOLP (var))
10348 continue;
10349
10350 val = find_symbol_value (var);
10351
10352 if (MARKERP (val)
10353 && current_buffer == XMARKER (val)->buffer
10354 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10355 {
10356 if (FRAME_WINDOW_P (it->f)
10357 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10358 {
10359 #ifdef HAVE_WINDOW_SYSTEM
10360 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10361 {
10362 int fringe_bitmap;
10363 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10364 return make_number (fringe_bitmap);
10365 }
10366 #endif
10367 return make_number (-1); /* Use default arrow bitmap */
10368 }
10369 return overlay_arrow_string_or_property (var);
10370 }
10371 }
10372
10373 return Qnil;
10374 }
10375
10376 /* Return 1 if point moved out of or into a composition. Otherwise
10377 return 0. PREV_BUF and PREV_PT are the last point buffer and
10378 position. BUF and PT are the current point buffer and position. */
10379
10380 int
10381 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10382 struct buffer *prev_buf, *buf;
10383 int prev_pt, pt;
10384 {
10385 int start, end;
10386 Lisp_Object prop;
10387 Lisp_Object buffer;
10388
10389 XSETBUFFER (buffer, buf);
10390 /* Check a composition at the last point if point moved within the
10391 same buffer. */
10392 if (prev_buf == buf)
10393 {
10394 if (prev_pt == pt)
10395 /* Point didn't move. */
10396 return 0;
10397
10398 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10399 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10400 && COMPOSITION_VALID_P (start, end, prop)
10401 && start < prev_pt && end > prev_pt)
10402 /* The last point was within the composition. Return 1 iff
10403 point moved out of the composition. */
10404 return (pt <= start || pt >= end);
10405 }
10406
10407 /* Check a composition at the current point. */
10408 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10409 && find_composition (pt, -1, &start, &end, &prop, buffer)
10410 && COMPOSITION_VALID_P (start, end, prop)
10411 && start < pt && end > pt);
10412 }
10413
10414
10415 /* Reconsider the setting of B->clip_changed which is displayed
10416 in window W. */
10417
10418 static INLINE void
10419 reconsider_clip_changes (w, b)
10420 struct window *w;
10421 struct buffer *b;
10422 {
10423 if (b->clip_changed
10424 && !NILP (w->window_end_valid)
10425 && w->current_matrix->buffer == b
10426 && w->current_matrix->zv == BUF_ZV (b)
10427 && w->current_matrix->begv == BUF_BEGV (b))
10428 b->clip_changed = 0;
10429
10430 /* If display wasn't paused, and W is not a tool bar window, see if
10431 point has been moved into or out of a composition. In that case,
10432 we set b->clip_changed to 1 to force updating the screen. If
10433 b->clip_changed has already been set to 1, we can skip this
10434 check. */
10435 if (!b->clip_changed
10436 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10437 {
10438 int pt;
10439
10440 if (w == XWINDOW (selected_window))
10441 pt = BUF_PT (current_buffer);
10442 else
10443 pt = marker_position (w->pointm);
10444
10445 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10446 || pt != XINT (w->last_point))
10447 && check_point_in_composition (w->current_matrix->buffer,
10448 XINT (w->last_point),
10449 XBUFFER (w->buffer), pt))
10450 b->clip_changed = 1;
10451 }
10452 }
10453 \f
10454
10455 /* Select FRAME to forward the values of frame-local variables into C
10456 variables so that the redisplay routines can access those values
10457 directly. */
10458
10459 static void
10460 select_frame_for_redisplay (frame)
10461 Lisp_Object frame;
10462 {
10463 Lisp_Object tail, sym, val;
10464 Lisp_Object old = selected_frame;
10465
10466 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10467
10468 selected_frame = frame;
10469
10470 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10471 if (CONSP (XCAR (tail))
10472 && (sym = XCAR (XCAR (tail)),
10473 SYMBOLP (sym))
10474 && (sym = indirect_variable (sym),
10475 val = SYMBOL_VALUE (sym),
10476 (BUFFER_LOCAL_VALUEP (val)
10477 || SOME_BUFFER_LOCAL_VALUEP (val)))
10478 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10479 /* Use find_symbol_value rather than Fsymbol_value
10480 to avoid an error if it is void. */
10481 find_symbol_value (sym);
10482
10483 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10484 if (CONSP (XCAR (tail))
10485 && (sym = XCAR (XCAR (tail)),
10486 SYMBOLP (sym))
10487 && (sym = indirect_variable (sym),
10488 val = SYMBOL_VALUE (sym),
10489 (BUFFER_LOCAL_VALUEP (val)
10490 || SOME_BUFFER_LOCAL_VALUEP (val)))
10491 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10492 find_symbol_value (sym);
10493 }
10494
10495
10496 #define STOP_POLLING \
10497 do { if (! polling_stopped_here) stop_polling (); \
10498 polling_stopped_here = 1; } while (0)
10499
10500 #define RESUME_POLLING \
10501 do { if (polling_stopped_here) start_polling (); \
10502 polling_stopped_here = 0; } while (0)
10503
10504
10505 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10506 response to any user action; therefore, we should preserve the echo
10507 area. (Actually, our caller does that job.) Perhaps in the future
10508 avoid recentering windows if it is not necessary; currently that
10509 causes some problems. */
10510
10511 static void
10512 redisplay_internal (preserve_echo_area)
10513 int preserve_echo_area;
10514 {
10515 struct window *w = XWINDOW (selected_window);
10516 struct frame *f = XFRAME (w->frame);
10517 int pause;
10518 int must_finish = 0;
10519 struct text_pos tlbufpos, tlendpos;
10520 int number_of_visible_frames;
10521 int count;
10522 struct frame *sf = SELECTED_FRAME ();
10523 int polling_stopped_here = 0;
10524
10525 /* Non-zero means redisplay has to consider all windows on all
10526 frames. Zero means, only selected_window is considered. */
10527 int consider_all_windows_p;
10528
10529 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10530
10531 /* No redisplay if running in batch mode or frame is not yet fully
10532 initialized, or redisplay is explicitly turned off by setting
10533 Vinhibit_redisplay. */
10534 if (noninteractive
10535 || !NILP (Vinhibit_redisplay)
10536 || !f->glyphs_initialized_p)
10537 return;
10538
10539 /* The flag redisplay_performed_directly_p is set by
10540 direct_output_for_insert when it already did the whole screen
10541 update necessary. */
10542 if (redisplay_performed_directly_p)
10543 {
10544 redisplay_performed_directly_p = 0;
10545 if (!hscroll_windows (selected_window))
10546 return;
10547 }
10548
10549 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10550 if (popup_activated ())
10551 return;
10552 #endif
10553
10554 /* I don't think this happens but let's be paranoid. */
10555 if (redisplaying_p)
10556 return;
10557
10558 /* Record a function that resets redisplaying_p to its old value
10559 when we leave this function. */
10560 count = SPECPDL_INDEX ();
10561 record_unwind_protect (unwind_redisplay,
10562 Fcons (make_number (redisplaying_p), selected_frame));
10563 ++redisplaying_p;
10564 specbind (Qinhibit_free_realized_faces, Qnil);
10565
10566 {
10567 Lisp_Object tail, frame;
10568
10569 FOR_EACH_FRAME (tail, frame)
10570 {
10571 struct frame *f = XFRAME (frame);
10572 f->already_hscrolled_p = 0;
10573 }
10574 }
10575
10576 retry:
10577 pause = 0;
10578 reconsider_clip_changes (w, current_buffer);
10579 last_escape_glyph_frame = NULL;
10580 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10581
10582 /* If new fonts have been loaded that make a glyph matrix adjustment
10583 necessary, do it. */
10584 if (fonts_changed_p)
10585 {
10586 adjust_glyphs (NULL);
10587 ++windows_or_buffers_changed;
10588 fonts_changed_p = 0;
10589 }
10590
10591 /* If face_change_count is non-zero, init_iterator will free all
10592 realized faces, which includes the faces referenced from current
10593 matrices. So, we can't reuse current matrices in this case. */
10594 if (face_change_count)
10595 ++windows_or_buffers_changed;
10596
10597 if (FRAME_TERMCAP_P (sf)
10598 && FRAME_TTY (sf)->previous_frame != sf)
10599 {
10600 /* Since frames on a single ASCII terminal share the same
10601 display area, displaying a different frame means redisplay
10602 the whole thing. */
10603 windows_or_buffers_changed++;
10604 SET_FRAME_GARBAGED (sf);
10605 FRAME_TTY (sf)->previous_frame = sf;
10606 }
10607
10608 /* Set the visible flags for all frames. Do this before checking
10609 for resized or garbaged frames; they want to know if their frames
10610 are visible. See the comment in frame.h for
10611 FRAME_SAMPLE_VISIBILITY. */
10612 {
10613 Lisp_Object tail, frame;
10614
10615 number_of_visible_frames = 0;
10616
10617 FOR_EACH_FRAME (tail, frame)
10618 {
10619 struct frame *f = XFRAME (frame);
10620
10621 FRAME_SAMPLE_VISIBILITY (f);
10622 if (FRAME_VISIBLE_P (f))
10623 ++number_of_visible_frames;
10624 clear_desired_matrices (f);
10625 }
10626 }
10627
10628
10629 /* Notice any pending interrupt request to change frame size. */
10630 do_pending_window_change (1);
10631
10632 /* Clear frames marked as garbaged. */
10633 if (frame_garbaged)
10634 clear_garbaged_frames ();
10635
10636 /* Build menubar and tool-bar items. */
10637 if (NILP (Vmemory_full))
10638 prepare_menu_bars ();
10639
10640 if (windows_or_buffers_changed)
10641 update_mode_lines++;
10642
10643 /* Detect case that we need to write or remove a star in the mode line. */
10644 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10645 {
10646 w->update_mode_line = Qt;
10647 if (buffer_shared > 1)
10648 update_mode_lines++;
10649 }
10650
10651 /* If %c is in the mode line, update it if needed. */
10652 if (!NILP (w->column_number_displayed)
10653 /* This alternative quickly identifies a common case
10654 where no change is needed. */
10655 && !(PT == XFASTINT (w->last_point)
10656 && XFASTINT (w->last_modified) >= MODIFF
10657 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10658 && (XFASTINT (w->column_number_displayed)
10659 != (int) current_column ())) /* iftc */
10660 w->update_mode_line = Qt;
10661
10662 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10663
10664 /* The variable buffer_shared is set in redisplay_window and
10665 indicates that we redisplay a buffer in different windows. See
10666 there. */
10667 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10668 || cursor_type_changed);
10669
10670 /* If specs for an arrow have changed, do thorough redisplay
10671 to ensure we remove any arrow that should no longer exist. */
10672 if (overlay_arrows_changed_p ())
10673 consider_all_windows_p = windows_or_buffers_changed = 1;
10674
10675 /* Normally the message* functions will have already displayed and
10676 updated the echo area, but the frame may have been trashed, or
10677 the update may have been preempted, so display the echo area
10678 again here. Checking message_cleared_p captures the case that
10679 the echo area should be cleared. */
10680 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10681 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10682 || (message_cleared_p
10683 && minibuf_level == 0
10684 /* If the mini-window is currently selected, this means the
10685 echo-area doesn't show through. */
10686 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10687 {
10688 int window_height_changed_p = echo_area_display (0);
10689 must_finish = 1;
10690
10691 /* If we don't display the current message, don't clear the
10692 message_cleared_p flag, because, if we did, we wouldn't clear
10693 the echo area in the next redisplay which doesn't preserve
10694 the echo area. */
10695 if (!display_last_displayed_message_p)
10696 message_cleared_p = 0;
10697
10698 if (fonts_changed_p)
10699 goto retry;
10700 else if (window_height_changed_p)
10701 {
10702 consider_all_windows_p = 1;
10703 ++update_mode_lines;
10704 ++windows_or_buffers_changed;
10705
10706 /* If window configuration was changed, frames may have been
10707 marked garbaged. Clear them or we will experience
10708 surprises wrt scrolling. */
10709 if (frame_garbaged)
10710 clear_garbaged_frames ();
10711 }
10712 }
10713 else if (EQ (selected_window, minibuf_window)
10714 && (current_buffer->clip_changed
10715 || XFASTINT (w->last_modified) < MODIFF
10716 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10717 && resize_mini_window (w, 0))
10718 {
10719 /* Resized active mini-window to fit the size of what it is
10720 showing if its contents might have changed. */
10721 must_finish = 1;
10722 consider_all_windows_p = 1;
10723 ++windows_or_buffers_changed;
10724 ++update_mode_lines;
10725
10726 /* If window configuration was changed, frames may have been
10727 marked garbaged. Clear them or we will experience
10728 surprises wrt scrolling. */
10729 if (frame_garbaged)
10730 clear_garbaged_frames ();
10731 }
10732
10733
10734 /* If showing the region, and mark has changed, we must redisplay
10735 the whole window. The assignment to this_line_start_pos prevents
10736 the optimization directly below this if-statement. */
10737 if (((!NILP (Vtransient_mark_mode)
10738 && !NILP (XBUFFER (w->buffer)->mark_active))
10739 != !NILP (w->region_showing))
10740 || (!NILP (w->region_showing)
10741 && !EQ (w->region_showing,
10742 Fmarker_position (XBUFFER (w->buffer)->mark))))
10743 CHARPOS (this_line_start_pos) = 0;
10744
10745 /* Optimize the case that only the line containing the cursor in the
10746 selected window has changed. Variables starting with this_ are
10747 set in display_line and record information about the line
10748 containing the cursor. */
10749 tlbufpos = this_line_start_pos;
10750 tlendpos = this_line_end_pos;
10751 if (!consider_all_windows_p
10752 && CHARPOS (tlbufpos) > 0
10753 && NILP (w->update_mode_line)
10754 && !current_buffer->clip_changed
10755 && !current_buffer->prevent_redisplay_optimizations_p
10756 && FRAME_VISIBLE_P (XFRAME (w->frame))
10757 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10758 /* Make sure recorded data applies to current buffer, etc. */
10759 && this_line_buffer == current_buffer
10760 && current_buffer == XBUFFER (w->buffer)
10761 && NILP (w->force_start)
10762 && NILP (w->optional_new_start)
10763 /* Point must be on the line that we have info recorded about. */
10764 && PT >= CHARPOS (tlbufpos)
10765 && PT <= Z - CHARPOS (tlendpos)
10766 /* All text outside that line, including its final newline,
10767 must be unchanged */
10768 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10769 CHARPOS (tlendpos)))
10770 {
10771 if (CHARPOS (tlbufpos) > BEGV
10772 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10773 && (CHARPOS (tlbufpos) == ZV
10774 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10775 /* Former continuation line has disappeared by becoming empty */
10776 goto cancel;
10777 else if (XFASTINT (w->last_modified) < MODIFF
10778 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10779 || MINI_WINDOW_P (w))
10780 {
10781 /* We have to handle the case of continuation around a
10782 wide-column character (See the comment in indent.c around
10783 line 885).
10784
10785 For instance, in the following case:
10786
10787 -------- Insert --------
10788 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10789 J_I_ ==> J_I_ `^^' are cursors.
10790 ^^ ^^
10791 -------- --------
10792
10793 As we have to redraw the line above, we should goto cancel. */
10794
10795 struct it it;
10796 int line_height_before = this_line_pixel_height;
10797
10798 /* Note that start_display will handle the case that the
10799 line starting at tlbufpos is a continuation lines. */
10800 start_display (&it, w, tlbufpos);
10801
10802 /* Implementation note: It this still necessary? */
10803 if (it.current_x != this_line_start_x)
10804 goto cancel;
10805
10806 TRACE ((stderr, "trying display optimization 1\n"));
10807 w->cursor.vpos = -1;
10808 overlay_arrow_seen = 0;
10809 it.vpos = this_line_vpos;
10810 it.current_y = this_line_y;
10811 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10812 display_line (&it);
10813
10814 /* If line contains point, is not continued,
10815 and ends at same distance from eob as before, we win */
10816 if (w->cursor.vpos >= 0
10817 /* Line is not continued, otherwise this_line_start_pos
10818 would have been set to 0 in display_line. */
10819 && CHARPOS (this_line_start_pos)
10820 /* Line ends as before. */
10821 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10822 /* Line has same height as before. Otherwise other lines
10823 would have to be shifted up or down. */
10824 && this_line_pixel_height == line_height_before)
10825 {
10826 /* If this is not the window's last line, we must adjust
10827 the charstarts of the lines below. */
10828 if (it.current_y < it.last_visible_y)
10829 {
10830 struct glyph_row *row
10831 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10832 int delta, delta_bytes;
10833
10834 if (Z - CHARPOS (tlendpos) == ZV)
10835 {
10836 /* This line ends at end of (accessible part of)
10837 buffer. There is no newline to count. */
10838 delta = (Z
10839 - CHARPOS (tlendpos)
10840 - MATRIX_ROW_START_CHARPOS (row));
10841 delta_bytes = (Z_BYTE
10842 - BYTEPOS (tlendpos)
10843 - MATRIX_ROW_START_BYTEPOS (row));
10844 }
10845 else
10846 {
10847 /* This line ends in a newline. Must take
10848 account of the newline and the rest of the
10849 text that follows. */
10850 delta = (Z
10851 - CHARPOS (tlendpos)
10852 - MATRIX_ROW_START_CHARPOS (row));
10853 delta_bytes = (Z_BYTE
10854 - BYTEPOS (tlendpos)
10855 - MATRIX_ROW_START_BYTEPOS (row));
10856 }
10857
10858 increment_matrix_positions (w->current_matrix,
10859 this_line_vpos + 1,
10860 w->current_matrix->nrows,
10861 delta, delta_bytes);
10862 }
10863
10864 /* If this row displays text now but previously didn't,
10865 or vice versa, w->window_end_vpos may have to be
10866 adjusted. */
10867 if ((it.glyph_row - 1)->displays_text_p)
10868 {
10869 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10870 XSETINT (w->window_end_vpos, this_line_vpos);
10871 }
10872 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10873 && this_line_vpos > 0)
10874 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10875 w->window_end_valid = Qnil;
10876
10877 /* Update hint: No need to try to scroll in update_window. */
10878 w->desired_matrix->no_scrolling_p = 1;
10879
10880 #if GLYPH_DEBUG
10881 *w->desired_matrix->method = 0;
10882 debug_method_add (w, "optimization 1");
10883 #endif
10884 #ifdef HAVE_WINDOW_SYSTEM
10885 update_window_fringes (w, 0);
10886 #endif
10887 goto update;
10888 }
10889 else
10890 goto cancel;
10891 }
10892 else if (/* Cursor position hasn't changed. */
10893 PT == XFASTINT (w->last_point)
10894 /* Make sure the cursor was last displayed
10895 in this window. Otherwise we have to reposition it. */
10896 && 0 <= w->cursor.vpos
10897 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10898 {
10899 if (!must_finish)
10900 {
10901 do_pending_window_change (1);
10902
10903 /* We used to always goto end_of_redisplay here, but this
10904 isn't enough if we have a blinking cursor. */
10905 if (w->cursor_off_p == w->last_cursor_off_p)
10906 goto end_of_redisplay;
10907 }
10908 goto update;
10909 }
10910 /* If highlighting the region, or if the cursor is in the echo area,
10911 then we can't just move the cursor. */
10912 else if (! (!NILP (Vtransient_mark_mode)
10913 && !NILP (current_buffer->mark_active))
10914 && (EQ (selected_window, current_buffer->last_selected_window)
10915 || highlight_nonselected_windows)
10916 && NILP (w->region_showing)
10917 && NILP (Vshow_trailing_whitespace)
10918 && !cursor_in_echo_area)
10919 {
10920 struct it it;
10921 struct glyph_row *row;
10922
10923 /* Skip from tlbufpos to PT and see where it is. Note that
10924 PT may be in invisible text. If so, we will end at the
10925 next visible position. */
10926 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10927 NULL, DEFAULT_FACE_ID);
10928 it.current_x = this_line_start_x;
10929 it.current_y = this_line_y;
10930 it.vpos = this_line_vpos;
10931
10932 /* The call to move_it_to stops in front of PT, but
10933 moves over before-strings. */
10934 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10935
10936 if (it.vpos == this_line_vpos
10937 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10938 row->enabled_p))
10939 {
10940 xassert (this_line_vpos == it.vpos);
10941 xassert (this_line_y == it.current_y);
10942 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10943 #if GLYPH_DEBUG
10944 *w->desired_matrix->method = 0;
10945 debug_method_add (w, "optimization 3");
10946 #endif
10947 goto update;
10948 }
10949 else
10950 goto cancel;
10951 }
10952
10953 cancel:
10954 /* Text changed drastically or point moved off of line. */
10955 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10956 }
10957
10958 CHARPOS (this_line_start_pos) = 0;
10959 consider_all_windows_p |= buffer_shared > 1;
10960 ++clear_face_cache_count;
10961 #ifdef HAVE_WINDOW_SYSTEM
10962 ++clear_image_cache_count;
10963 #endif
10964
10965 /* Build desired matrices, and update the display. If
10966 consider_all_windows_p is non-zero, do it for all windows on all
10967 frames. Otherwise do it for selected_window, only. */
10968
10969 if (consider_all_windows_p)
10970 {
10971 Lisp_Object tail, frame;
10972
10973 FOR_EACH_FRAME (tail, frame)
10974 XFRAME (frame)->updated_p = 0;
10975
10976 /* Recompute # windows showing selected buffer. This will be
10977 incremented each time such a window is displayed. */
10978 buffer_shared = 0;
10979
10980 FOR_EACH_FRAME (tail, frame)
10981 {
10982 struct frame *f = XFRAME (frame);
10983
10984 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10985 {
10986 if (! EQ (frame, selected_frame))
10987 /* Select the frame, for the sake of frame-local
10988 variables. */
10989 select_frame_for_redisplay (frame);
10990
10991 /* Mark all the scroll bars to be removed; we'll redeem
10992 the ones we want when we redisplay their windows. */
10993 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
10994 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
10995
10996 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10997 redisplay_windows (FRAME_ROOT_WINDOW (f));
10998
10999 /* Any scroll bars which redisplay_windows should have
11000 nuked should now go away. */
11001 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11002 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11003
11004 /* If fonts changed, display again. */
11005 /* ??? rms: I suspect it is a mistake to jump all the way
11006 back to retry here. It should just retry this frame. */
11007 if (fonts_changed_p)
11008 goto retry;
11009
11010 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11011 {
11012 /* See if we have to hscroll. */
11013 if (!f->already_hscrolled_p)
11014 {
11015 f->already_hscrolled_p = 1;
11016 if (hscroll_windows (f->root_window))
11017 goto retry;
11018 }
11019
11020 /* Prevent various kinds of signals during display
11021 update. stdio is not robust about handling
11022 signals, which can cause an apparent I/O
11023 error. */
11024 if (interrupt_input)
11025 unrequest_sigio ();
11026 STOP_POLLING;
11027
11028 /* Update the display. */
11029 set_window_update_flags (XWINDOW (f->root_window), 1);
11030 pause |= update_frame (f, 0, 0);
11031 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11032 if (pause)
11033 break;
11034 #endif
11035
11036 f->updated_p = 1;
11037 }
11038 }
11039 }
11040
11041 if (!pause)
11042 {
11043 /* Do the mark_window_display_accurate after all windows have
11044 been redisplayed because this call resets flags in buffers
11045 which are needed for proper redisplay. */
11046 FOR_EACH_FRAME (tail, frame)
11047 {
11048 struct frame *f = XFRAME (frame);
11049 if (f->updated_p)
11050 {
11051 mark_window_display_accurate (f->root_window, 1);
11052 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11053 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11054 }
11055 }
11056 }
11057 }
11058 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11059 {
11060 Lisp_Object mini_window;
11061 struct frame *mini_frame;
11062
11063 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11064 /* Use list_of_error, not Qerror, so that
11065 we catch only errors and don't run the debugger. */
11066 internal_condition_case_1 (redisplay_window_1, selected_window,
11067 list_of_error,
11068 redisplay_window_error);
11069
11070 /* Compare desired and current matrices, perform output. */
11071
11072 update:
11073 /* If fonts changed, display again. */
11074 if (fonts_changed_p)
11075 goto retry;
11076
11077 /* Prevent various kinds of signals during display update.
11078 stdio is not robust about handling signals,
11079 which can cause an apparent I/O error. */
11080 if (interrupt_input)
11081 unrequest_sigio ();
11082 STOP_POLLING;
11083
11084 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11085 {
11086 if (hscroll_windows (selected_window))
11087 goto retry;
11088
11089 XWINDOW (selected_window)->must_be_updated_p = 1;
11090 pause = update_frame (sf, 0, 0);
11091 }
11092
11093 /* We may have called echo_area_display at the top of this
11094 function. If the echo area is on another frame, that may
11095 have put text on a frame other than the selected one, so the
11096 above call to update_frame would not have caught it. Catch
11097 it here. */
11098 mini_window = FRAME_MINIBUF_WINDOW (sf);
11099 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11100
11101 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11102 {
11103 XWINDOW (mini_window)->must_be_updated_p = 1;
11104 pause |= update_frame (mini_frame, 0, 0);
11105 if (!pause && hscroll_windows (mini_window))
11106 goto retry;
11107 }
11108 }
11109
11110 /* If display was paused because of pending input, make sure we do a
11111 thorough update the next time. */
11112 if (pause)
11113 {
11114 /* Prevent the optimization at the beginning of
11115 redisplay_internal that tries a single-line update of the
11116 line containing the cursor in the selected window. */
11117 CHARPOS (this_line_start_pos) = 0;
11118
11119 /* Let the overlay arrow be updated the next time. */
11120 update_overlay_arrows (0);
11121
11122 /* If we pause after scrolling, some rows in the current
11123 matrices of some windows are not valid. */
11124 if (!WINDOW_FULL_WIDTH_P (w)
11125 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11126 update_mode_lines = 1;
11127 }
11128 else
11129 {
11130 if (!consider_all_windows_p)
11131 {
11132 /* This has already been done above if
11133 consider_all_windows_p is set. */
11134 mark_window_display_accurate_1 (w, 1);
11135
11136 /* Say overlay arrows are up to date. */
11137 update_overlay_arrows (1);
11138
11139 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11140 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11141 }
11142
11143 update_mode_lines = 0;
11144 windows_or_buffers_changed = 0;
11145 cursor_type_changed = 0;
11146 }
11147
11148 /* Start SIGIO interrupts coming again. Having them off during the
11149 code above makes it less likely one will discard output, but not
11150 impossible, since there might be stuff in the system buffer here.
11151 But it is much hairier to try to do anything about that. */
11152 if (interrupt_input)
11153 request_sigio ();
11154 RESUME_POLLING;
11155
11156 /* If a frame has become visible which was not before, redisplay
11157 again, so that we display it. Expose events for such a frame
11158 (which it gets when becoming visible) don't call the parts of
11159 redisplay constructing glyphs, so simply exposing a frame won't
11160 display anything in this case. So, we have to display these
11161 frames here explicitly. */
11162 if (!pause)
11163 {
11164 Lisp_Object tail, frame;
11165 int new_count = 0;
11166
11167 FOR_EACH_FRAME (tail, frame)
11168 {
11169 int this_is_visible = 0;
11170
11171 if (XFRAME (frame)->visible)
11172 this_is_visible = 1;
11173 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11174 if (XFRAME (frame)->visible)
11175 this_is_visible = 1;
11176
11177 if (this_is_visible)
11178 new_count++;
11179 }
11180
11181 if (new_count != number_of_visible_frames)
11182 windows_or_buffers_changed++;
11183 }
11184
11185 /* Change frame size now if a change is pending. */
11186 do_pending_window_change (1);
11187
11188 /* If we just did a pending size change, or have additional
11189 visible frames, redisplay again. */
11190 if (windows_or_buffers_changed && !pause)
11191 goto retry;
11192
11193 /* Clear the face cache eventually. */
11194 if (consider_all_windows_p)
11195 {
11196 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11197 {
11198 clear_face_cache (0);
11199 clear_face_cache_count = 0;
11200 }
11201 #ifdef HAVE_WINDOW_SYSTEM
11202 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11203 {
11204 Lisp_Object tail, frame;
11205 FOR_EACH_FRAME (tail, frame)
11206 {
11207 struct frame *f = XFRAME (frame);
11208 if (FRAME_WINDOW_P (f))
11209 clear_image_cache (f, 0);
11210 }
11211 clear_image_cache_count = 0;
11212 }
11213 #endif /* HAVE_WINDOW_SYSTEM */
11214 }
11215
11216 end_of_redisplay:
11217 unbind_to (count, Qnil);
11218 RESUME_POLLING;
11219 }
11220
11221
11222 /* Redisplay, but leave alone any recent echo area message unless
11223 another message has been requested in its place.
11224
11225 This is useful in situations where you need to redisplay but no
11226 user action has occurred, making it inappropriate for the message
11227 area to be cleared. See tracking_off and
11228 wait_reading_process_output for examples of these situations.
11229
11230 FROM_WHERE is an integer saying from where this function was
11231 called. This is useful for debugging. */
11232
11233 void
11234 redisplay_preserve_echo_area (from_where)
11235 int from_where;
11236 {
11237 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11238
11239 if (!NILP (echo_area_buffer[1]))
11240 {
11241 /* We have a previously displayed message, but no current
11242 message. Redisplay the previous message. */
11243 display_last_displayed_message_p = 1;
11244 redisplay_internal (1);
11245 display_last_displayed_message_p = 0;
11246 }
11247 else
11248 redisplay_internal (1);
11249
11250 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11251 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11252 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11253 }
11254
11255
11256 /* Function registered with record_unwind_protect in
11257 redisplay_internal. Reset redisplaying_p to the value it had
11258 before redisplay_internal was called, and clear
11259 prevent_freeing_realized_faces_p. It also selects the previously
11260 selected frame, unless it has been deleted (by an X connection
11261 failure during redisplay, for example). */
11262
11263 static Lisp_Object
11264 unwind_redisplay (val)
11265 Lisp_Object val;
11266 {
11267 Lisp_Object old_redisplaying_p, old_frame;
11268
11269 old_redisplaying_p = XCAR (val);
11270 redisplaying_p = XFASTINT (old_redisplaying_p);
11271 old_frame = XCDR (val);
11272 if (! EQ (old_frame, selected_frame)
11273 && FRAME_LIVE_P (XFRAME (old_frame)))
11274 select_frame_for_redisplay (old_frame);
11275 return Qnil;
11276 }
11277
11278
11279 /* Mark the display of window W as accurate or inaccurate. If
11280 ACCURATE_P is non-zero mark display of W as accurate. If
11281 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11282 redisplay_internal is called. */
11283
11284 static void
11285 mark_window_display_accurate_1 (w, accurate_p)
11286 struct window *w;
11287 int accurate_p;
11288 {
11289 if (BUFFERP (w->buffer))
11290 {
11291 struct buffer *b = XBUFFER (w->buffer);
11292
11293 w->last_modified
11294 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11295 w->last_overlay_modified
11296 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11297 w->last_had_star
11298 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11299
11300 if (accurate_p)
11301 {
11302 b->clip_changed = 0;
11303 b->prevent_redisplay_optimizations_p = 0;
11304
11305 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11306 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11307 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11308 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11309
11310 w->current_matrix->buffer = b;
11311 w->current_matrix->begv = BUF_BEGV (b);
11312 w->current_matrix->zv = BUF_ZV (b);
11313
11314 w->last_cursor = w->cursor;
11315 w->last_cursor_off_p = w->cursor_off_p;
11316
11317 if (w == XWINDOW (selected_window))
11318 w->last_point = make_number (BUF_PT (b));
11319 else
11320 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11321 }
11322 }
11323
11324 if (accurate_p)
11325 {
11326 w->window_end_valid = w->buffer;
11327 #if 0 /* This is incorrect with variable-height lines. */
11328 xassert (XINT (w->window_end_vpos)
11329 < (WINDOW_TOTAL_LINES (w)
11330 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11331 #endif
11332 w->update_mode_line = Qnil;
11333 }
11334 }
11335
11336
11337 /* Mark the display of windows in the window tree rooted at WINDOW as
11338 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11339 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11340 be redisplayed the next time redisplay_internal is called. */
11341
11342 void
11343 mark_window_display_accurate (window, accurate_p)
11344 Lisp_Object window;
11345 int accurate_p;
11346 {
11347 struct window *w;
11348
11349 for (; !NILP (window); window = w->next)
11350 {
11351 w = XWINDOW (window);
11352 mark_window_display_accurate_1 (w, accurate_p);
11353
11354 if (!NILP (w->vchild))
11355 mark_window_display_accurate (w->vchild, accurate_p);
11356 if (!NILP (w->hchild))
11357 mark_window_display_accurate (w->hchild, accurate_p);
11358 }
11359
11360 if (accurate_p)
11361 {
11362 update_overlay_arrows (1);
11363 }
11364 else
11365 {
11366 /* Force a thorough redisplay the next time by setting
11367 last_arrow_position and last_arrow_string to t, which is
11368 unequal to any useful value of Voverlay_arrow_... */
11369 update_overlay_arrows (-1);
11370 }
11371 }
11372
11373
11374 /* Return value in display table DP (Lisp_Char_Table *) for character
11375 C. Since a display table doesn't have any parent, we don't have to
11376 follow parent. Do not call this function directly but use the
11377 macro DISP_CHAR_VECTOR. */
11378
11379 Lisp_Object
11380 disp_char_vector (dp, c)
11381 struct Lisp_Char_Table *dp;
11382 int c;
11383 {
11384 int code[4], i;
11385 Lisp_Object val;
11386
11387 if (SINGLE_BYTE_CHAR_P (c))
11388 return (dp->contents[c]);
11389
11390 SPLIT_CHAR (c, code[0], code[1], code[2]);
11391 if (code[1] < 32)
11392 code[1] = -1;
11393 else if (code[2] < 32)
11394 code[2] = -1;
11395
11396 /* Here, the possible range of code[0] (== charset ID) is
11397 128..max_charset. Since the top level char table contains data
11398 for multibyte characters after 256th element, we must increment
11399 code[0] by 128 to get a correct index. */
11400 code[0] += 128;
11401 code[3] = -1; /* anchor */
11402
11403 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11404 {
11405 val = dp->contents[code[i]];
11406 if (!SUB_CHAR_TABLE_P (val))
11407 return (NILP (val) ? dp->defalt : val);
11408 }
11409
11410 /* Here, val is a sub char table. We return the default value of
11411 it. */
11412 return (dp->defalt);
11413 }
11414
11415
11416 \f
11417 /***********************************************************************
11418 Window Redisplay
11419 ***********************************************************************/
11420
11421 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11422
11423 static void
11424 redisplay_windows (window)
11425 Lisp_Object window;
11426 {
11427 while (!NILP (window))
11428 {
11429 struct window *w = XWINDOW (window);
11430
11431 if (!NILP (w->hchild))
11432 redisplay_windows (w->hchild);
11433 else if (!NILP (w->vchild))
11434 redisplay_windows (w->vchild);
11435 else
11436 {
11437 displayed_buffer = XBUFFER (w->buffer);
11438 /* Use list_of_error, not Qerror, so that
11439 we catch only errors and don't run the debugger. */
11440 internal_condition_case_1 (redisplay_window_0, window,
11441 list_of_error,
11442 redisplay_window_error);
11443 }
11444
11445 window = w->next;
11446 }
11447 }
11448
11449 static Lisp_Object
11450 redisplay_window_error ()
11451 {
11452 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11453 return Qnil;
11454 }
11455
11456 static Lisp_Object
11457 redisplay_window_0 (window)
11458 Lisp_Object window;
11459 {
11460 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11461 redisplay_window (window, 0);
11462 return Qnil;
11463 }
11464
11465 static Lisp_Object
11466 redisplay_window_1 (window)
11467 Lisp_Object window;
11468 {
11469 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11470 redisplay_window (window, 1);
11471 return Qnil;
11472 }
11473 \f
11474
11475 /* Increment GLYPH until it reaches END or CONDITION fails while
11476 adding (GLYPH)->pixel_width to X. */
11477
11478 #define SKIP_GLYPHS(glyph, end, x, condition) \
11479 do \
11480 { \
11481 (x) += (glyph)->pixel_width; \
11482 ++(glyph); \
11483 } \
11484 while ((glyph) < (end) && (condition))
11485
11486
11487 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11488 DELTA is the number of bytes by which positions recorded in ROW
11489 differ from current buffer positions. */
11490
11491 void
11492 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11493 struct window *w;
11494 struct glyph_row *row;
11495 struct glyph_matrix *matrix;
11496 int delta, delta_bytes, dy, dvpos;
11497 {
11498 struct glyph *glyph = row->glyphs[TEXT_AREA];
11499 struct glyph *end = glyph + row->used[TEXT_AREA];
11500 struct glyph *cursor = NULL;
11501 /* The first glyph that starts a sequence of glyphs from string. */
11502 struct glyph *string_start;
11503 /* The X coordinate of string_start. */
11504 int string_start_x;
11505 /* The last known character position. */
11506 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11507 /* The last known character position before string_start. */
11508 int string_before_pos;
11509 int x = row->x;
11510 int cursor_x = x;
11511 int cursor_from_overlay_pos = 0;
11512 int pt_old = PT - delta;
11513
11514 /* Skip over glyphs not having an object at the start of the row.
11515 These are special glyphs like truncation marks on terminal
11516 frames. */
11517 if (row->displays_text_p)
11518 while (glyph < end
11519 && INTEGERP (glyph->object)
11520 && glyph->charpos < 0)
11521 {
11522 x += glyph->pixel_width;
11523 ++glyph;
11524 }
11525
11526 string_start = NULL;
11527 while (glyph < end
11528 && !INTEGERP (glyph->object)
11529 && (!BUFFERP (glyph->object)
11530 || (last_pos = glyph->charpos) < pt_old))
11531 {
11532 if (! STRINGP (glyph->object))
11533 {
11534 string_start = NULL;
11535 x += glyph->pixel_width;
11536 ++glyph;
11537 if (cursor_from_overlay_pos
11538 && last_pos > cursor_from_overlay_pos)
11539 {
11540 cursor_from_overlay_pos = 0;
11541 cursor = 0;
11542 }
11543 }
11544 else
11545 {
11546 string_before_pos = last_pos;
11547 string_start = glyph;
11548 string_start_x = x;
11549 /* Skip all glyphs from string. */
11550 do
11551 {
11552 int pos;
11553 if ((cursor == NULL || glyph > cursor)
11554 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11555 Qcursor, (glyph)->object))
11556 && (pos = string_buffer_position (w, glyph->object,
11557 string_before_pos),
11558 (pos == 0 /* From overlay */
11559 || pos == pt_old)))
11560 {
11561 /* Estimate overlay buffer position from the buffer
11562 positions of the glyphs before and after the overlay.
11563 Add 1 to last_pos so that if point corresponds to the
11564 glyph right after the overlay, we still use a 'cursor'
11565 property found in that overlay. */
11566 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11567 cursor = glyph;
11568 cursor_x = x;
11569 }
11570 x += glyph->pixel_width;
11571 ++glyph;
11572 }
11573 while (glyph < end && STRINGP (glyph->object));
11574 }
11575 }
11576
11577 if (cursor != NULL)
11578 {
11579 glyph = cursor;
11580 x = cursor_x;
11581 }
11582 else if (row->ends_in_ellipsis_p && glyph == end)
11583 {
11584 /* Scan back over the ellipsis glyphs, decrementing positions. */
11585 while (glyph > row->glyphs[TEXT_AREA]
11586 && (glyph - 1)->charpos == last_pos)
11587 glyph--, x -= glyph->pixel_width;
11588 /* That loop always goes one position too far,
11589 including the glyph before the ellipsis.
11590 So scan forward over that one. */
11591 x += glyph->pixel_width;
11592 glyph++;
11593 }
11594 else if (string_start
11595 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11596 {
11597 /* We may have skipped over point because the previous glyphs
11598 are from string. As there's no easy way to know the
11599 character position of the current glyph, find the correct
11600 glyph on point by scanning from string_start again. */
11601 Lisp_Object limit;
11602 Lisp_Object string;
11603 int pos;
11604
11605 limit = make_number (pt_old + 1);
11606 end = glyph;
11607 glyph = string_start;
11608 x = string_start_x;
11609 string = glyph->object;
11610 pos = string_buffer_position (w, string, string_before_pos);
11611 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11612 because we always put cursor after overlay strings. */
11613 while (pos == 0 && glyph < end)
11614 {
11615 string = glyph->object;
11616 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11617 if (glyph < end)
11618 pos = string_buffer_position (w, glyph->object, string_before_pos);
11619 }
11620
11621 while (glyph < end)
11622 {
11623 pos = XINT (Fnext_single_char_property_change
11624 (make_number (pos), Qdisplay, Qnil, limit));
11625 if (pos > pt_old)
11626 break;
11627 /* Skip glyphs from the same string. */
11628 string = glyph->object;
11629 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11630 /* Skip glyphs from an overlay. */
11631 while (glyph < end
11632 && ! string_buffer_position (w, glyph->object, pos))
11633 {
11634 string = glyph->object;
11635 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11636 }
11637 }
11638 }
11639
11640 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11641 w->cursor.x = x;
11642 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11643 w->cursor.y = row->y + dy;
11644
11645 if (w == XWINDOW (selected_window))
11646 {
11647 if (!row->continued_p
11648 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11649 && row->x == 0)
11650 {
11651 this_line_buffer = XBUFFER (w->buffer);
11652
11653 CHARPOS (this_line_start_pos)
11654 = MATRIX_ROW_START_CHARPOS (row) + delta;
11655 BYTEPOS (this_line_start_pos)
11656 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11657
11658 CHARPOS (this_line_end_pos)
11659 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11660 BYTEPOS (this_line_end_pos)
11661 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11662
11663 this_line_y = w->cursor.y;
11664 this_line_pixel_height = row->height;
11665 this_line_vpos = w->cursor.vpos;
11666 this_line_start_x = row->x;
11667 }
11668 else
11669 CHARPOS (this_line_start_pos) = 0;
11670 }
11671 }
11672
11673
11674 /* Run window scroll functions, if any, for WINDOW with new window
11675 start STARTP. Sets the window start of WINDOW to that position.
11676
11677 We assume that the window's buffer is really current. */
11678
11679 static INLINE struct text_pos
11680 run_window_scroll_functions (window, startp)
11681 Lisp_Object window;
11682 struct text_pos startp;
11683 {
11684 struct window *w = XWINDOW (window);
11685 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11686
11687 if (current_buffer != XBUFFER (w->buffer))
11688 abort ();
11689
11690 if (!NILP (Vwindow_scroll_functions))
11691 {
11692 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11693 make_number (CHARPOS (startp)));
11694 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11695 /* In case the hook functions switch buffers. */
11696 if (current_buffer != XBUFFER (w->buffer))
11697 set_buffer_internal_1 (XBUFFER (w->buffer));
11698 }
11699
11700 return startp;
11701 }
11702
11703
11704 /* Make sure the line containing the cursor is fully visible.
11705 A value of 1 means there is nothing to be done.
11706 (Either the line is fully visible, or it cannot be made so,
11707 or we cannot tell.)
11708
11709 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11710 is higher than window.
11711
11712 A value of 0 means the caller should do scrolling
11713 as if point had gone off the screen. */
11714
11715 static int
11716 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11717 struct window *w;
11718 int force_p;
11719 int current_matrix_p;
11720 {
11721 struct glyph_matrix *matrix;
11722 struct glyph_row *row;
11723 int window_height;
11724
11725 if (!make_cursor_line_fully_visible_p)
11726 return 1;
11727
11728 /* It's not always possible to find the cursor, e.g, when a window
11729 is full of overlay strings. Don't do anything in that case. */
11730 if (w->cursor.vpos < 0)
11731 return 1;
11732
11733 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11734 row = MATRIX_ROW (matrix, w->cursor.vpos);
11735
11736 /* If the cursor row is not partially visible, there's nothing to do. */
11737 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11738 return 1;
11739
11740 /* If the row the cursor is in is taller than the window's height,
11741 it's not clear what to do, so do nothing. */
11742 window_height = window_box_height (w);
11743 if (row->height >= window_height)
11744 {
11745 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11746 return 1;
11747 }
11748 return 0;
11749
11750 #if 0
11751 /* This code used to try to scroll the window just enough to make
11752 the line visible. It returned 0 to say that the caller should
11753 allocate larger glyph matrices. */
11754
11755 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11756 {
11757 int dy = row->height - row->visible_height;
11758 w->vscroll = 0;
11759 w->cursor.y += dy;
11760 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11761 }
11762 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11763 {
11764 int dy = - (row->height - row->visible_height);
11765 w->vscroll = dy;
11766 w->cursor.y += dy;
11767 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11768 }
11769
11770 /* When we change the cursor y-position of the selected window,
11771 change this_line_y as well so that the display optimization for
11772 the cursor line of the selected window in redisplay_internal uses
11773 the correct y-position. */
11774 if (w == XWINDOW (selected_window))
11775 this_line_y = w->cursor.y;
11776
11777 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11778 redisplay with larger matrices. */
11779 if (matrix->nrows < required_matrix_height (w))
11780 {
11781 fonts_changed_p = 1;
11782 return 0;
11783 }
11784
11785 return 1;
11786 #endif /* 0 */
11787 }
11788
11789
11790 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11791 non-zero means only WINDOW is redisplayed in redisplay_internal.
11792 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11793 in redisplay_window to bring a partially visible line into view in
11794 the case that only the cursor has moved.
11795
11796 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11797 last screen line's vertical height extends past the end of the screen.
11798
11799 Value is
11800
11801 1 if scrolling succeeded
11802
11803 0 if scrolling didn't find point.
11804
11805 -1 if new fonts have been loaded so that we must interrupt
11806 redisplay, adjust glyph matrices, and try again. */
11807
11808 enum
11809 {
11810 SCROLLING_SUCCESS,
11811 SCROLLING_FAILED,
11812 SCROLLING_NEED_LARGER_MATRICES
11813 };
11814
11815 static int
11816 try_scrolling (window, just_this_one_p, scroll_conservatively,
11817 scroll_step, temp_scroll_step, last_line_misfit)
11818 Lisp_Object window;
11819 int just_this_one_p;
11820 EMACS_INT scroll_conservatively, scroll_step;
11821 int temp_scroll_step;
11822 int last_line_misfit;
11823 {
11824 struct window *w = XWINDOW (window);
11825 struct frame *f = XFRAME (w->frame);
11826 struct text_pos scroll_margin_pos;
11827 struct text_pos pos;
11828 struct text_pos startp;
11829 struct it it;
11830 Lisp_Object window_end;
11831 int this_scroll_margin;
11832 int dy = 0;
11833 int scroll_max;
11834 int rc;
11835 int amount_to_scroll = 0;
11836 Lisp_Object aggressive;
11837 int height;
11838 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11839
11840 #if GLYPH_DEBUG
11841 debug_method_add (w, "try_scrolling");
11842 #endif
11843
11844 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11845
11846 /* Compute scroll margin height in pixels. We scroll when point is
11847 within this distance from the top or bottom of the window. */
11848 if (scroll_margin > 0)
11849 {
11850 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11851 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11852 }
11853 else
11854 this_scroll_margin = 0;
11855
11856 /* Force scroll_conservatively to have a reasonable value so it doesn't
11857 cause an overflow while computing how much to scroll. */
11858 if (scroll_conservatively)
11859 scroll_conservatively = min (scroll_conservatively,
11860 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11861
11862 /* Compute how much we should try to scroll maximally to bring point
11863 into view. */
11864 if (scroll_step || scroll_conservatively || temp_scroll_step)
11865 scroll_max = max (scroll_step,
11866 max (scroll_conservatively, temp_scroll_step));
11867 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11868 || NUMBERP (current_buffer->scroll_up_aggressively))
11869 /* We're trying to scroll because of aggressive scrolling
11870 but no scroll_step is set. Choose an arbitrary one. Maybe
11871 there should be a variable for this. */
11872 scroll_max = 10;
11873 else
11874 scroll_max = 0;
11875 scroll_max *= FRAME_LINE_HEIGHT (f);
11876
11877 /* Decide whether we have to scroll down. Start at the window end
11878 and move this_scroll_margin up to find the position of the scroll
11879 margin. */
11880 window_end = Fwindow_end (window, Qt);
11881
11882 too_near_end:
11883
11884 CHARPOS (scroll_margin_pos) = XINT (window_end);
11885 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11886
11887 if (this_scroll_margin || extra_scroll_margin_lines)
11888 {
11889 start_display (&it, w, scroll_margin_pos);
11890 if (this_scroll_margin)
11891 move_it_vertically_backward (&it, this_scroll_margin);
11892 if (extra_scroll_margin_lines)
11893 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11894 scroll_margin_pos = it.current.pos;
11895 }
11896
11897 if (PT >= CHARPOS (scroll_margin_pos))
11898 {
11899 int y0;
11900
11901 /* Point is in the scroll margin at the bottom of the window, or
11902 below. Compute a new window start that makes point visible. */
11903
11904 /* Compute the distance from the scroll margin to PT.
11905 Give up if the distance is greater than scroll_max. */
11906 start_display (&it, w, scroll_margin_pos);
11907 y0 = it.current_y;
11908 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11909 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11910
11911 /* To make point visible, we have to move the window start
11912 down so that the line the cursor is in is visible, which
11913 means we have to add in the height of the cursor line. */
11914 dy = line_bottom_y (&it) - y0;
11915
11916 if (dy > scroll_max)
11917 return SCROLLING_FAILED;
11918
11919 /* Move the window start down. If scrolling conservatively,
11920 move it just enough down to make point visible. If
11921 scroll_step is set, move it down by scroll_step. */
11922 start_display (&it, w, startp);
11923
11924 if (scroll_conservatively)
11925 /* Set AMOUNT_TO_SCROLL to at least one line,
11926 and at most scroll_conservatively lines. */
11927 amount_to_scroll
11928 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11929 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11930 else if (scroll_step || temp_scroll_step)
11931 amount_to_scroll = scroll_max;
11932 else
11933 {
11934 aggressive = current_buffer->scroll_up_aggressively;
11935 height = WINDOW_BOX_TEXT_HEIGHT (w);
11936 if (NUMBERP (aggressive))
11937 {
11938 double float_amount = XFLOATINT (aggressive) * height;
11939 amount_to_scroll = float_amount;
11940 if (amount_to_scroll == 0 && float_amount > 0)
11941 amount_to_scroll = 1;
11942 }
11943 }
11944
11945 if (amount_to_scroll <= 0)
11946 return SCROLLING_FAILED;
11947
11948 /* If moving by amount_to_scroll leaves STARTP unchanged,
11949 move it down one screen line. */
11950
11951 move_it_vertically (&it, amount_to_scroll);
11952 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11953 move_it_by_lines (&it, 1, 1);
11954 startp = it.current.pos;
11955 }
11956 else
11957 {
11958 /* See if point is inside the scroll margin at the top of the
11959 window. */
11960 scroll_margin_pos = startp;
11961 if (this_scroll_margin)
11962 {
11963 start_display (&it, w, startp);
11964 move_it_vertically (&it, this_scroll_margin);
11965 scroll_margin_pos = it.current.pos;
11966 }
11967
11968 if (PT < CHARPOS (scroll_margin_pos))
11969 {
11970 /* Point is in the scroll margin at the top of the window or
11971 above what is displayed in the window. */
11972 int y0;
11973
11974 /* Compute the vertical distance from PT to the scroll
11975 margin position. Give up if distance is greater than
11976 scroll_max. */
11977 SET_TEXT_POS (pos, PT, PT_BYTE);
11978 start_display (&it, w, pos);
11979 y0 = it.current_y;
11980 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11981 it.last_visible_y, -1,
11982 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11983 dy = it.current_y - y0;
11984 if (dy > scroll_max)
11985 return SCROLLING_FAILED;
11986
11987 /* Compute new window start. */
11988 start_display (&it, w, startp);
11989
11990 if (scroll_conservatively)
11991 amount_to_scroll
11992 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11993 else if (scroll_step || temp_scroll_step)
11994 amount_to_scroll = scroll_max;
11995 else
11996 {
11997 aggressive = current_buffer->scroll_down_aggressively;
11998 height = WINDOW_BOX_TEXT_HEIGHT (w);
11999 if (NUMBERP (aggressive))
12000 {
12001 double float_amount = XFLOATINT (aggressive) * height;
12002 amount_to_scroll = float_amount;
12003 if (amount_to_scroll == 0 && float_amount > 0)
12004 amount_to_scroll = 1;
12005 }
12006 }
12007
12008 if (amount_to_scroll <= 0)
12009 return SCROLLING_FAILED;
12010
12011 move_it_vertically_backward (&it, amount_to_scroll);
12012 startp = it.current.pos;
12013 }
12014 }
12015
12016 /* Run window scroll functions. */
12017 startp = run_window_scroll_functions (window, startp);
12018
12019 /* Display the window. Give up if new fonts are loaded, or if point
12020 doesn't appear. */
12021 if (!try_window (window, startp, 0))
12022 rc = SCROLLING_NEED_LARGER_MATRICES;
12023 else if (w->cursor.vpos < 0)
12024 {
12025 clear_glyph_matrix (w->desired_matrix);
12026 rc = SCROLLING_FAILED;
12027 }
12028 else
12029 {
12030 /* Maybe forget recorded base line for line number display. */
12031 if (!just_this_one_p
12032 || current_buffer->clip_changed
12033 || BEG_UNCHANGED < CHARPOS (startp))
12034 w->base_line_number = Qnil;
12035
12036 /* If cursor ends up on a partially visible line,
12037 treat that as being off the bottom of the screen. */
12038 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12039 {
12040 clear_glyph_matrix (w->desired_matrix);
12041 ++extra_scroll_margin_lines;
12042 goto too_near_end;
12043 }
12044 rc = SCROLLING_SUCCESS;
12045 }
12046
12047 return rc;
12048 }
12049
12050
12051 /* Compute a suitable window start for window W if display of W starts
12052 on a continuation line. Value is non-zero if a new window start
12053 was computed.
12054
12055 The new window start will be computed, based on W's width, starting
12056 from the start of the continued line. It is the start of the
12057 screen line with the minimum distance from the old start W->start. */
12058
12059 static int
12060 compute_window_start_on_continuation_line (w)
12061 struct window *w;
12062 {
12063 struct text_pos pos, start_pos;
12064 int window_start_changed_p = 0;
12065
12066 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12067
12068 /* If window start is on a continuation line... Window start may be
12069 < BEGV in case there's invisible text at the start of the
12070 buffer (M-x rmail, for example). */
12071 if (CHARPOS (start_pos) > BEGV
12072 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12073 {
12074 struct it it;
12075 struct glyph_row *row;
12076
12077 /* Handle the case that the window start is out of range. */
12078 if (CHARPOS (start_pos) < BEGV)
12079 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12080 else if (CHARPOS (start_pos) > ZV)
12081 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12082
12083 /* Find the start of the continued line. This should be fast
12084 because scan_buffer is fast (newline cache). */
12085 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12086 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12087 row, DEFAULT_FACE_ID);
12088 reseat_at_previous_visible_line_start (&it);
12089
12090 /* If the line start is "too far" away from the window start,
12091 say it takes too much time to compute a new window start. */
12092 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12093 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12094 {
12095 int min_distance, distance;
12096
12097 /* Move forward by display lines to find the new window
12098 start. If window width was enlarged, the new start can
12099 be expected to be > the old start. If window width was
12100 decreased, the new window start will be < the old start.
12101 So, we're looking for the display line start with the
12102 minimum distance from the old window start. */
12103 pos = it.current.pos;
12104 min_distance = INFINITY;
12105 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12106 distance < min_distance)
12107 {
12108 min_distance = distance;
12109 pos = it.current.pos;
12110 move_it_by_lines (&it, 1, 0);
12111 }
12112
12113 /* Set the window start there. */
12114 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12115 window_start_changed_p = 1;
12116 }
12117 }
12118
12119 return window_start_changed_p;
12120 }
12121
12122
12123 /* Try cursor movement in case text has not changed in window WINDOW,
12124 with window start STARTP. Value is
12125
12126 CURSOR_MOVEMENT_SUCCESS if successful
12127
12128 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12129
12130 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12131 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12132 we want to scroll as if scroll-step were set to 1. See the code.
12133
12134 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12135 which case we have to abort this redisplay, and adjust matrices
12136 first. */
12137
12138 enum
12139 {
12140 CURSOR_MOVEMENT_SUCCESS,
12141 CURSOR_MOVEMENT_CANNOT_BE_USED,
12142 CURSOR_MOVEMENT_MUST_SCROLL,
12143 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12144 };
12145
12146 static int
12147 try_cursor_movement (window, startp, scroll_step)
12148 Lisp_Object window;
12149 struct text_pos startp;
12150 int *scroll_step;
12151 {
12152 struct window *w = XWINDOW (window);
12153 struct frame *f = XFRAME (w->frame);
12154 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12155
12156 #if GLYPH_DEBUG
12157 if (inhibit_try_cursor_movement)
12158 return rc;
12159 #endif
12160
12161 /* Handle case where text has not changed, only point, and it has
12162 not moved off the frame. */
12163 if (/* Point may be in this window. */
12164 PT >= CHARPOS (startp)
12165 /* Selective display hasn't changed. */
12166 && !current_buffer->clip_changed
12167 /* Function force-mode-line-update is used to force a thorough
12168 redisplay. It sets either windows_or_buffers_changed or
12169 update_mode_lines. So don't take a shortcut here for these
12170 cases. */
12171 && !update_mode_lines
12172 && !windows_or_buffers_changed
12173 && !cursor_type_changed
12174 /* Can't use this case if highlighting a region. When a
12175 region exists, cursor movement has to do more than just
12176 set the cursor. */
12177 && !(!NILP (Vtransient_mark_mode)
12178 && !NILP (current_buffer->mark_active))
12179 && NILP (w->region_showing)
12180 && NILP (Vshow_trailing_whitespace)
12181 /* Right after splitting windows, last_point may be nil. */
12182 && INTEGERP (w->last_point)
12183 /* This code is not used for mini-buffer for the sake of the case
12184 of redisplaying to replace an echo area message; since in
12185 that case the mini-buffer contents per se are usually
12186 unchanged. This code is of no real use in the mini-buffer
12187 since the handling of this_line_start_pos, etc., in redisplay
12188 handles the same cases. */
12189 && !EQ (window, minibuf_window)
12190 /* When splitting windows or for new windows, it happens that
12191 redisplay is called with a nil window_end_vpos or one being
12192 larger than the window. This should really be fixed in
12193 window.c. I don't have this on my list, now, so we do
12194 approximately the same as the old redisplay code. --gerd. */
12195 && INTEGERP (w->window_end_vpos)
12196 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12197 && (FRAME_WINDOW_P (f)
12198 || !overlay_arrow_in_current_buffer_p ()))
12199 {
12200 int this_scroll_margin, top_scroll_margin;
12201 struct glyph_row *row = NULL;
12202
12203 #if GLYPH_DEBUG
12204 debug_method_add (w, "cursor movement");
12205 #endif
12206
12207 /* Scroll if point within this distance from the top or bottom
12208 of the window. This is a pixel value. */
12209 this_scroll_margin = max (0, scroll_margin);
12210 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12211 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12212
12213 top_scroll_margin = this_scroll_margin;
12214 if (WINDOW_WANTS_HEADER_LINE_P (w))
12215 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12216
12217 /* Start with the row the cursor was displayed during the last
12218 not paused redisplay. Give up if that row is not valid. */
12219 if (w->last_cursor.vpos < 0
12220 || w->last_cursor.vpos >= w->current_matrix->nrows)
12221 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12222 else
12223 {
12224 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12225 if (row->mode_line_p)
12226 ++row;
12227 if (!row->enabled_p)
12228 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12229 }
12230
12231 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12232 {
12233 int scroll_p = 0;
12234 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12235
12236 if (PT > XFASTINT (w->last_point))
12237 {
12238 /* Point has moved forward. */
12239 while (MATRIX_ROW_END_CHARPOS (row) < PT
12240 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12241 {
12242 xassert (row->enabled_p);
12243 ++row;
12244 }
12245
12246 /* The end position of a row equals the start position
12247 of the next row. If PT is there, we would rather
12248 display it in the next line. */
12249 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12250 && MATRIX_ROW_END_CHARPOS (row) == PT
12251 && !cursor_row_p (w, row))
12252 ++row;
12253
12254 /* If within the scroll margin, scroll. Note that
12255 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12256 the next line would be drawn, and that
12257 this_scroll_margin can be zero. */
12258 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12259 || PT > MATRIX_ROW_END_CHARPOS (row)
12260 /* Line is completely visible last line in window
12261 and PT is to be set in the next line. */
12262 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12263 && PT == MATRIX_ROW_END_CHARPOS (row)
12264 && !row->ends_at_zv_p
12265 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12266 scroll_p = 1;
12267 }
12268 else if (PT < XFASTINT (w->last_point))
12269 {
12270 /* Cursor has to be moved backward. Note that PT >=
12271 CHARPOS (startp) because of the outer if-statement. */
12272 while (!row->mode_line_p
12273 && (MATRIX_ROW_START_CHARPOS (row) > PT
12274 || (MATRIX_ROW_START_CHARPOS (row) == PT
12275 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12276 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12277 row > w->current_matrix->rows
12278 && (row-1)->ends_in_newline_from_string_p))))
12279 && (row->y > top_scroll_margin
12280 || CHARPOS (startp) == BEGV))
12281 {
12282 xassert (row->enabled_p);
12283 --row;
12284 }
12285
12286 /* Consider the following case: Window starts at BEGV,
12287 there is invisible, intangible text at BEGV, so that
12288 display starts at some point START > BEGV. It can
12289 happen that we are called with PT somewhere between
12290 BEGV and START. Try to handle that case. */
12291 if (row < w->current_matrix->rows
12292 || row->mode_line_p)
12293 {
12294 row = w->current_matrix->rows;
12295 if (row->mode_line_p)
12296 ++row;
12297 }
12298
12299 /* Due to newlines in overlay strings, we may have to
12300 skip forward over overlay strings. */
12301 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12302 && MATRIX_ROW_END_CHARPOS (row) == PT
12303 && !cursor_row_p (w, row))
12304 ++row;
12305
12306 /* If within the scroll margin, scroll. */
12307 if (row->y < top_scroll_margin
12308 && CHARPOS (startp) != BEGV)
12309 scroll_p = 1;
12310 }
12311 else
12312 {
12313 /* Cursor did not move. So don't scroll even if cursor line
12314 is partially visible, as it was so before. */
12315 rc = CURSOR_MOVEMENT_SUCCESS;
12316 }
12317
12318 if (PT < MATRIX_ROW_START_CHARPOS (row)
12319 || PT > MATRIX_ROW_END_CHARPOS (row))
12320 {
12321 /* if PT is not in the glyph row, give up. */
12322 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12323 }
12324 else if (rc != CURSOR_MOVEMENT_SUCCESS
12325 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12326 && make_cursor_line_fully_visible_p)
12327 {
12328 if (PT == MATRIX_ROW_END_CHARPOS (row)
12329 && !row->ends_at_zv_p
12330 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12331 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12332 else if (row->height > window_box_height (w))
12333 {
12334 /* If we end up in a partially visible line, let's
12335 make it fully visible, except when it's taller
12336 than the window, in which case we can't do much
12337 about it. */
12338 *scroll_step = 1;
12339 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12340 }
12341 else
12342 {
12343 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12344 if (!cursor_row_fully_visible_p (w, 0, 1))
12345 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12346 else
12347 rc = CURSOR_MOVEMENT_SUCCESS;
12348 }
12349 }
12350 else if (scroll_p)
12351 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12352 else
12353 {
12354 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12355 rc = CURSOR_MOVEMENT_SUCCESS;
12356 }
12357 }
12358 }
12359
12360 return rc;
12361 }
12362
12363 void
12364 set_vertical_scroll_bar (w)
12365 struct window *w;
12366 {
12367 int start, end, whole;
12368
12369 /* Calculate the start and end positions for the current window.
12370 At some point, it would be nice to choose between scrollbars
12371 which reflect the whole buffer size, with special markers
12372 indicating narrowing, and scrollbars which reflect only the
12373 visible region.
12374
12375 Note that mini-buffers sometimes aren't displaying any text. */
12376 if (!MINI_WINDOW_P (w)
12377 || (w == XWINDOW (minibuf_window)
12378 && NILP (echo_area_buffer[0])))
12379 {
12380 struct buffer *buf = XBUFFER (w->buffer);
12381 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12382 start = marker_position (w->start) - BUF_BEGV (buf);
12383 /* I don't think this is guaranteed to be right. For the
12384 moment, we'll pretend it is. */
12385 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12386
12387 if (end < start)
12388 end = start;
12389 if (whole < (end - start))
12390 whole = end - start;
12391 }
12392 else
12393 start = end = whole = 0;
12394
12395 /* Indicate what this scroll bar ought to be displaying now. */
12396 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12397 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12398 (w, end - start, whole, start);
12399 }
12400
12401
12402 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12403 selected_window is redisplayed.
12404
12405 We can return without actually redisplaying the window if
12406 fonts_changed_p is nonzero. In that case, redisplay_internal will
12407 retry. */
12408
12409 static void
12410 redisplay_window (window, just_this_one_p)
12411 Lisp_Object window;
12412 int just_this_one_p;
12413 {
12414 struct window *w = XWINDOW (window);
12415 struct frame *f = XFRAME (w->frame);
12416 struct buffer *buffer = XBUFFER (w->buffer);
12417 struct buffer *old = current_buffer;
12418 struct text_pos lpoint, opoint, startp;
12419 int update_mode_line;
12420 int tem;
12421 struct it it;
12422 /* Record it now because it's overwritten. */
12423 int current_matrix_up_to_date_p = 0;
12424 int used_current_matrix_p = 0;
12425 /* This is less strict than current_matrix_up_to_date_p.
12426 It indictes that the buffer contents and narrowing are unchanged. */
12427 int buffer_unchanged_p = 0;
12428 int temp_scroll_step = 0;
12429 int count = SPECPDL_INDEX ();
12430 int rc;
12431 int centering_position = -1;
12432 int last_line_misfit = 0;
12433
12434 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12435 opoint = lpoint;
12436
12437 /* W must be a leaf window here. */
12438 xassert (!NILP (w->buffer));
12439 #if GLYPH_DEBUG
12440 *w->desired_matrix->method = 0;
12441 #endif
12442
12443 specbind (Qinhibit_point_motion_hooks, Qt);
12444
12445 reconsider_clip_changes (w, buffer);
12446
12447 /* Has the mode line to be updated? */
12448 update_mode_line = (!NILP (w->update_mode_line)
12449 || update_mode_lines
12450 || buffer->clip_changed
12451 || buffer->prevent_redisplay_optimizations_p);
12452
12453 if (MINI_WINDOW_P (w))
12454 {
12455 if (w == XWINDOW (echo_area_window)
12456 && !NILP (echo_area_buffer[0]))
12457 {
12458 if (update_mode_line)
12459 /* We may have to update a tty frame's menu bar or a
12460 tool-bar. Example `M-x C-h C-h C-g'. */
12461 goto finish_menu_bars;
12462 else
12463 /* We've already displayed the echo area glyphs in this window. */
12464 goto finish_scroll_bars;
12465 }
12466 else if ((w != XWINDOW (minibuf_window)
12467 || minibuf_level == 0)
12468 /* When buffer is nonempty, redisplay window normally. */
12469 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12470 /* Quail displays non-mini buffers in minibuffer window.
12471 In that case, redisplay the window normally. */
12472 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12473 {
12474 /* W is a mini-buffer window, but it's not active, so clear
12475 it. */
12476 int yb = window_text_bottom_y (w);
12477 struct glyph_row *row;
12478 int y;
12479
12480 for (y = 0, row = w->desired_matrix->rows;
12481 y < yb;
12482 y += row->height, ++row)
12483 blank_row (w, row, y);
12484 goto finish_scroll_bars;
12485 }
12486
12487 clear_glyph_matrix (w->desired_matrix);
12488 }
12489
12490 /* Otherwise set up data on this window; select its buffer and point
12491 value. */
12492 /* Really select the buffer, for the sake of buffer-local
12493 variables. */
12494 set_buffer_internal_1 (XBUFFER (w->buffer));
12495 SET_TEXT_POS (opoint, PT, PT_BYTE);
12496
12497 current_matrix_up_to_date_p
12498 = (!NILP (w->window_end_valid)
12499 && !current_buffer->clip_changed
12500 && !current_buffer->prevent_redisplay_optimizations_p
12501 && XFASTINT (w->last_modified) >= MODIFF
12502 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12503
12504 buffer_unchanged_p
12505 = (!NILP (w->window_end_valid)
12506 && !current_buffer->clip_changed
12507 && XFASTINT (w->last_modified) >= MODIFF
12508 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12509
12510 /* When windows_or_buffers_changed is non-zero, we can't rely on
12511 the window end being valid, so set it to nil there. */
12512 if (windows_or_buffers_changed)
12513 {
12514 /* If window starts on a continuation line, maybe adjust the
12515 window start in case the window's width changed. */
12516 if (XMARKER (w->start)->buffer == current_buffer)
12517 compute_window_start_on_continuation_line (w);
12518
12519 w->window_end_valid = Qnil;
12520 }
12521
12522 /* Some sanity checks. */
12523 CHECK_WINDOW_END (w);
12524 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12525 abort ();
12526 if (BYTEPOS (opoint) < CHARPOS (opoint))
12527 abort ();
12528
12529 /* If %c is in mode line, update it if needed. */
12530 if (!NILP (w->column_number_displayed)
12531 /* This alternative quickly identifies a common case
12532 where no change is needed. */
12533 && !(PT == XFASTINT (w->last_point)
12534 && XFASTINT (w->last_modified) >= MODIFF
12535 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12536 && (XFASTINT (w->column_number_displayed)
12537 != (int) current_column ())) /* iftc */
12538 update_mode_line = 1;
12539
12540 /* Count number of windows showing the selected buffer. An indirect
12541 buffer counts as its base buffer. */
12542 if (!just_this_one_p)
12543 {
12544 struct buffer *current_base, *window_base;
12545 current_base = current_buffer;
12546 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12547 if (current_base->base_buffer)
12548 current_base = current_base->base_buffer;
12549 if (window_base->base_buffer)
12550 window_base = window_base->base_buffer;
12551 if (current_base == window_base)
12552 buffer_shared++;
12553 }
12554
12555 /* Point refers normally to the selected window. For any other
12556 window, set up appropriate value. */
12557 if (!EQ (window, selected_window))
12558 {
12559 int new_pt = XMARKER (w->pointm)->charpos;
12560 int new_pt_byte = marker_byte_position (w->pointm);
12561 if (new_pt < BEGV)
12562 {
12563 new_pt = BEGV;
12564 new_pt_byte = BEGV_BYTE;
12565 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12566 }
12567 else if (new_pt > (ZV - 1))
12568 {
12569 new_pt = ZV;
12570 new_pt_byte = ZV_BYTE;
12571 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12572 }
12573
12574 /* We don't use SET_PT so that the point-motion hooks don't run. */
12575 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12576 }
12577
12578 /* If any of the character widths specified in the display table
12579 have changed, invalidate the width run cache. It's true that
12580 this may be a bit late to catch such changes, but the rest of
12581 redisplay goes (non-fatally) haywire when the display table is
12582 changed, so why should we worry about doing any better? */
12583 if (current_buffer->width_run_cache)
12584 {
12585 struct Lisp_Char_Table *disptab = buffer_display_table ();
12586
12587 if (! disptab_matches_widthtab (disptab,
12588 XVECTOR (current_buffer->width_table)))
12589 {
12590 invalidate_region_cache (current_buffer,
12591 current_buffer->width_run_cache,
12592 BEG, Z);
12593 recompute_width_table (current_buffer, disptab);
12594 }
12595 }
12596
12597 /* If window-start is screwed up, choose a new one. */
12598 if (XMARKER (w->start)->buffer != current_buffer)
12599 goto recenter;
12600
12601 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12602
12603 /* If someone specified a new starting point but did not insist,
12604 check whether it can be used. */
12605 if (!NILP (w->optional_new_start)
12606 && CHARPOS (startp) >= BEGV
12607 && CHARPOS (startp) <= ZV)
12608 {
12609 w->optional_new_start = Qnil;
12610 start_display (&it, w, startp);
12611 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12612 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12613 if (IT_CHARPOS (it) == PT)
12614 w->force_start = Qt;
12615 /* IT may overshoot PT if text at PT is invisible. */
12616 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12617 w->force_start = Qt;
12618
12619
12620 }
12621
12622 /* Handle case where place to start displaying has been specified,
12623 unless the specified location is outside the accessible range. */
12624 if (!NILP (w->force_start)
12625 || w->frozen_window_start_p)
12626 {
12627 /* We set this later on if we have to adjust point. */
12628 int new_vpos = -1;
12629 int val;
12630
12631 w->force_start = Qnil;
12632 w->vscroll = 0;
12633 w->window_end_valid = Qnil;
12634
12635 /* Forget any recorded base line for line number display. */
12636 if (!buffer_unchanged_p)
12637 w->base_line_number = Qnil;
12638
12639 /* Redisplay the mode line. Select the buffer properly for that.
12640 Also, run the hook window-scroll-functions
12641 because we have scrolled. */
12642 /* Note, we do this after clearing force_start because
12643 if there's an error, it is better to forget about force_start
12644 than to get into an infinite loop calling the hook functions
12645 and having them get more errors. */
12646 if (!update_mode_line
12647 || ! NILP (Vwindow_scroll_functions))
12648 {
12649 update_mode_line = 1;
12650 w->update_mode_line = Qt;
12651 startp = run_window_scroll_functions (window, startp);
12652 }
12653
12654 w->last_modified = make_number (0);
12655 w->last_overlay_modified = make_number (0);
12656 if (CHARPOS (startp) < BEGV)
12657 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12658 else if (CHARPOS (startp) > ZV)
12659 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12660
12661 /* Redisplay, then check if cursor has been set during the
12662 redisplay. Give up if new fonts were loaded. */
12663 val = try_window (window, startp, 1);
12664 if (!val)
12665 {
12666 w->force_start = Qt;
12667 clear_glyph_matrix (w->desired_matrix);
12668 goto need_larger_matrices;
12669 }
12670 /* Point was outside the scroll margins. */
12671 if (val < 0)
12672 new_vpos = window_box_height (w) / 2;
12673
12674 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12675 {
12676 /* If point does not appear, try to move point so it does
12677 appear. The desired matrix has been built above, so we
12678 can use it here. */
12679 new_vpos = window_box_height (w) / 2;
12680 }
12681
12682 if (!cursor_row_fully_visible_p (w, 0, 0))
12683 {
12684 /* Point does appear, but on a line partly visible at end of window.
12685 Move it back to a fully-visible line. */
12686 new_vpos = window_box_height (w);
12687 }
12688
12689 /* If we need to move point for either of the above reasons,
12690 now actually do it. */
12691 if (new_vpos >= 0)
12692 {
12693 struct glyph_row *row;
12694
12695 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12696 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12697 ++row;
12698
12699 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12700 MATRIX_ROW_START_BYTEPOS (row));
12701
12702 if (w != XWINDOW (selected_window))
12703 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12704 else if (current_buffer == old)
12705 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12706
12707 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12708
12709 /* If we are highlighting the region, then we just changed
12710 the region, so redisplay to show it. */
12711 if (!NILP (Vtransient_mark_mode)
12712 && !NILP (current_buffer->mark_active))
12713 {
12714 clear_glyph_matrix (w->desired_matrix);
12715 if (!try_window (window, startp, 0))
12716 goto need_larger_matrices;
12717 }
12718 }
12719
12720 #if GLYPH_DEBUG
12721 debug_method_add (w, "forced window start");
12722 #endif
12723 goto done;
12724 }
12725
12726 /* Handle case where text has not changed, only point, and it has
12727 not moved off the frame, and we are not retrying after hscroll.
12728 (current_matrix_up_to_date_p is nonzero when retrying.) */
12729 if (current_matrix_up_to_date_p
12730 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12731 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12732 {
12733 switch (rc)
12734 {
12735 case CURSOR_MOVEMENT_SUCCESS:
12736 used_current_matrix_p = 1;
12737 goto done;
12738
12739 #if 0 /* try_cursor_movement never returns this value. */
12740 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12741 goto need_larger_matrices;
12742 #endif
12743
12744 case CURSOR_MOVEMENT_MUST_SCROLL:
12745 goto try_to_scroll;
12746
12747 default:
12748 abort ();
12749 }
12750 }
12751 /* If current starting point was originally the beginning of a line
12752 but no longer is, find a new starting point. */
12753 else if (!NILP (w->start_at_line_beg)
12754 && !(CHARPOS (startp) <= BEGV
12755 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12756 {
12757 #if GLYPH_DEBUG
12758 debug_method_add (w, "recenter 1");
12759 #endif
12760 goto recenter;
12761 }
12762
12763 /* Try scrolling with try_window_id. Value is > 0 if update has
12764 been done, it is -1 if we know that the same window start will
12765 not work. It is 0 if unsuccessful for some other reason. */
12766 else if ((tem = try_window_id (w)) != 0)
12767 {
12768 #if GLYPH_DEBUG
12769 debug_method_add (w, "try_window_id %d", tem);
12770 #endif
12771
12772 if (fonts_changed_p)
12773 goto need_larger_matrices;
12774 if (tem > 0)
12775 goto done;
12776
12777 /* Otherwise try_window_id has returned -1 which means that we
12778 don't want the alternative below this comment to execute. */
12779 }
12780 else if (CHARPOS (startp) >= BEGV
12781 && CHARPOS (startp) <= ZV
12782 && PT >= CHARPOS (startp)
12783 && (CHARPOS (startp) < ZV
12784 /* Avoid starting at end of buffer. */
12785 || CHARPOS (startp) == BEGV
12786 || (XFASTINT (w->last_modified) >= MODIFF
12787 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12788 {
12789 #if GLYPH_DEBUG
12790 debug_method_add (w, "same window start");
12791 #endif
12792
12793 /* Try to redisplay starting at same place as before.
12794 If point has not moved off frame, accept the results. */
12795 if (!current_matrix_up_to_date_p
12796 /* Don't use try_window_reusing_current_matrix in this case
12797 because a window scroll function can have changed the
12798 buffer. */
12799 || !NILP (Vwindow_scroll_functions)
12800 || MINI_WINDOW_P (w)
12801 || !(used_current_matrix_p
12802 = try_window_reusing_current_matrix (w)))
12803 {
12804 IF_DEBUG (debug_method_add (w, "1"));
12805 if (try_window (window, startp, 1) < 0)
12806 /* -1 means we need to scroll.
12807 0 means we need new matrices, but fonts_changed_p
12808 is set in that case, so we will detect it below. */
12809 goto try_to_scroll;
12810 }
12811
12812 if (fonts_changed_p)
12813 goto need_larger_matrices;
12814
12815 if (w->cursor.vpos >= 0)
12816 {
12817 if (!just_this_one_p
12818 || current_buffer->clip_changed
12819 || BEG_UNCHANGED < CHARPOS (startp))
12820 /* Forget any recorded base line for line number display. */
12821 w->base_line_number = Qnil;
12822
12823 if (!cursor_row_fully_visible_p (w, 1, 0))
12824 {
12825 clear_glyph_matrix (w->desired_matrix);
12826 last_line_misfit = 1;
12827 }
12828 /* Drop through and scroll. */
12829 else
12830 goto done;
12831 }
12832 else
12833 clear_glyph_matrix (w->desired_matrix);
12834 }
12835
12836 try_to_scroll:
12837
12838 w->last_modified = make_number (0);
12839 w->last_overlay_modified = make_number (0);
12840
12841 /* Redisplay the mode line. Select the buffer properly for that. */
12842 if (!update_mode_line)
12843 {
12844 update_mode_line = 1;
12845 w->update_mode_line = Qt;
12846 }
12847
12848 /* Try to scroll by specified few lines. */
12849 if ((scroll_conservatively
12850 || scroll_step
12851 || temp_scroll_step
12852 || NUMBERP (current_buffer->scroll_up_aggressively)
12853 || NUMBERP (current_buffer->scroll_down_aggressively))
12854 && !current_buffer->clip_changed
12855 && CHARPOS (startp) >= BEGV
12856 && CHARPOS (startp) <= ZV)
12857 {
12858 /* The function returns -1 if new fonts were loaded, 1 if
12859 successful, 0 if not successful. */
12860 int rc = try_scrolling (window, just_this_one_p,
12861 scroll_conservatively,
12862 scroll_step,
12863 temp_scroll_step, last_line_misfit);
12864 switch (rc)
12865 {
12866 case SCROLLING_SUCCESS:
12867 goto done;
12868
12869 case SCROLLING_NEED_LARGER_MATRICES:
12870 goto need_larger_matrices;
12871
12872 case SCROLLING_FAILED:
12873 break;
12874
12875 default:
12876 abort ();
12877 }
12878 }
12879
12880 /* Finally, just choose place to start which centers point */
12881
12882 recenter:
12883 if (centering_position < 0)
12884 centering_position = window_box_height (w) / 2;
12885
12886 #if GLYPH_DEBUG
12887 debug_method_add (w, "recenter");
12888 #endif
12889
12890 /* w->vscroll = 0; */
12891
12892 /* Forget any previously recorded base line for line number display. */
12893 if (!buffer_unchanged_p)
12894 w->base_line_number = Qnil;
12895
12896 /* Move backward half the height of the window. */
12897 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12898 it.current_y = it.last_visible_y;
12899 move_it_vertically_backward (&it, centering_position);
12900 xassert (IT_CHARPOS (it) >= BEGV);
12901
12902 /* The function move_it_vertically_backward may move over more
12903 than the specified y-distance. If it->w is small, e.g. a
12904 mini-buffer window, we may end up in front of the window's
12905 display area. Start displaying at the start of the line
12906 containing PT in this case. */
12907 if (it.current_y <= 0)
12908 {
12909 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12910 move_it_vertically_backward (&it, 0);
12911 #if 0
12912 /* I think this assert is bogus if buffer contains
12913 invisible text or images. KFS. */
12914 xassert (IT_CHARPOS (it) <= PT);
12915 #endif
12916 it.current_y = 0;
12917 }
12918
12919 it.current_x = it.hpos = 0;
12920
12921 /* Set startp here explicitly in case that helps avoid an infinite loop
12922 in case the window-scroll-functions functions get errors. */
12923 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12924
12925 /* Run scroll hooks. */
12926 startp = run_window_scroll_functions (window, it.current.pos);
12927
12928 /* Redisplay the window. */
12929 if (!current_matrix_up_to_date_p
12930 || windows_or_buffers_changed
12931 || cursor_type_changed
12932 /* Don't use try_window_reusing_current_matrix in this case
12933 because it can have changed the buffer. */
12934 || !NILP (Vwindow_scroll_functions)
12935 || !just_this_one_p
12936 || MINI_WINDOW_P (w)
12937 || !(used_current_matrix_p
12938 = try_window_reusing_current_matrix (w)))
12939 try_window (window, startp, 0);
12940
12941 /* If new fonts have been loaded (due to fontsets), give up. We
12942 have to start a new redisplay since we need to re-adjust glyph
12943 matrices. */
12944 if (fonts_changed_p)
12945 goto need_larger_matrices;
12946
12947 /* If cursor did not appear assume that the middle of the window is
12948 in the first line of the window. Do it again with the next line.
12949 (Imagine a window of height 100, displaying two lines of height
12950 60. Moving back 50 from it->last_visible_y will end in the first
12951 line.) */
12952 if (w->cursor.vpos < 0)
12953 {
12954 if (!NILP (w->window_end_valid)
12955 && PT >= Z - XFASTINT (w->window_end_pos))
12956 {
12957 clear_glyph_matrix (w->desired_matrix);
12958 move_it_by_lines (&it, 1, 0);
12959 try_window (window, it.current.pos, 0);
12960 }
12961 else if (PT < IT_CHARPOS (it))
12962 {
12963 clear_glyph_matrix (w->desired_matrix);
12964 move_it_by_lines (&it, -1, 0);
12965 try_window (window, it.current.pos, 0);
12966 }
12967 else
12968 {
12969 /* Not much we can do about it. */
12970 }
12971 }
12972
12973 /* Consider the following case: Window starts at BEGV, there is
12974 invisible, intangible text at BEGV, so that display starts at
12975 some point START > BEGV. It can happen that we are called with
12976 PT somewhere between BEGV and START. Try to handle that case. */
12977 if (w->cursor.vpos < 0)
12978 {
12979 struct glyph_row *row = w->current_matrix->rows;
12980 if (row->mode_line_p)
12981 ++row;
12982 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12983 }
12984
12985 if (!cursor_row_fully_visible_p (w, 0, 0))
12986 {
12987 /* If vscroll is enabled, disable it and try again. */
12988 if (w->vscroll)
12989 {
12990 w->vscroll = 0;
12991 clear_glyph_matrix (w->desired_matrix);
12992 goto recenter;
12993 }
12994
12995 /* If centering point failed to make the whole line visible,
12996 put point at the top instead. That has to make the whole line
12997 visible, if it can be done. */
12998 if (centering_position == 0)
12999 goto done;
13000
13001 clear_glyph_matrix (w->desired_matrix);
13002 centering_position = 0;
13003 goto recenter;
13004 }
13005
13006 done:
13007
13008 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13009 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13010 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13011 ? Qt : Qnil);
13012
13013 /* Display the mode line, if we must. */
13014 if ((update_mode_line
13015 /* If window not full width, must redo its mode line
13016 if (a) the window to its side is being redone and
13017 (b) we do a frame-based redisplay. This is a consequence
13018 of how inverted lines are drawn in frame-based redisplay. */
13019 || (!just_this_one_p
13020 && !FRAME_WINDOW_P (f)
13021 && !WINDOW_FULL_WIDTH_P (w))
13022 /* Line number to display. */
13023 || INTEGERP (w->base_line_pos)
13024 /* Column number is displayed and different from the one displayed. */
13025 || (!NILP (w->column_number_displayed)
13026 && (XFASTINT (w->column_number_displayed)
13027 != (int) current_column ()))) /* iftc */
13028 /* This means that the window has a mode line. */
13029 && (WINDOW_WANTS_MODELINE_P (w)
13030 || WINDOW_WANTS_HEADER_LINE_P (w)))
13031 {
13032 display_mode_lines (w);
13033
13034 /* If mode line height has changed, arrange for a thorough
13035 immediate redisplay using the correct mode line height. */
13036 if (WINDOW_WANTS_MODELINE_P (w)
13037 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13038 {
13039 fonts_changed_p = 1;
13040 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13041 = DESIRED_MODE_LINE_HEIGHT (w);
13042 }
13043
13044 /* If top line height has changed, arrange for a thorough
13045 immediate redisplay using the correct mode line height. */
13046 if (WINDOW_WANTS_HEADER_LINE_P (w)
13047 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13048 {
13049 fonts_changed_p = 1;
13050 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13051 = DESIRED_HEADER_LINE_HEIGHT (w);
13052 }
13053
13054 if (fonts_changed_p)
13055 goto need_larger_matrices;
13056 }
13057
13058 if (!line_number_displayed
13059 && !BUFFERP (w->base_line_pos))
13060 {
13061 w->base_line_pos = Qnil;
13062 w->base_line_number = Qnil;
13063 }
13064
13065 finish_menu_bars:
13066
13067 /* When we reach a frame's selected window, redo the frame's menu bar. */
13068 if (update_mode_line
13069 && EQ (FRAME_SELECTED_WINDOW (f), window))
13070 {
13071 int redisplay_menu_p = 0;
13072 int redisplay_tool_bar_p = 0;
13073
13074 if (FRAME_WINDOW_P (f))
13075 {
13076 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13077 || defined (USE_GTK)
13078 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13079 #else
13080 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13081 #endif
13082 }
13083 else
13084 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13085
13086 if (redisplay_menu_p)
13087 display_menu_bar (w);
13088
13089 #ifdef HAVE_WINDOW_SYSTEM
13090 if (FRAME_WINDOW_P (f))
13091 {
13092 #ifdef USE_GTK
13093 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13094 #else
13095 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13096 && (FRAME_TOOL_BAR_LINES (f) > 0
13097 || auto_resize_tool_bars_p);
13098 #endif
13099
13100 if (redisplay_tool_bar_p)
13101 redisplay_tool_bar (f);
13102 }
13103 #endif
13104 }
13105
13106 #ifdef HAVE_WINDOW_SYSTEM
13107 if (FRAME_WINDOW_P (f)
13108 && update_window_fringes (w, (just_this_one_p
13109 || (!used_current_matrix_p && !overlay_arrow_seen)
13110 || w->pseudo_window_p)))
13111 {
13112 update_begin (f);
13113 BLOCK_INPUT;
13114 if (draw_window_fringes (w, 1))
13115 x_draw_vertical_border (w);
13116 UNBLOCK_INPUT;
13117 update_end (f);
13118 }
13119 #endif /* HAVE_WINDOW_SYSTEM */
13120
13121 /* We go to this label, with fonts_changed_p nonzero,
13122 if it is necessary to try again using larger glyph matrices.
13123 We have to redeem the scroll bar even in this case,
13124 because the loop in redisplay_internal expects that. */
13125 need_larger_matrices:
13126 ;
13127 finish_scroll_bars:
13128
13129 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13130 {
13131 /* Set the thumb's position and size. */
13132 set_vertical_scroll_bar (w);
13133
13134 /* Note that we actually used the scroll bar attached to this
13135 window, so it shouldn't be deleted at the end of redisplay. */
13136 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13137 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13138 }
13139
13140 /* Restore current_buffer and value of point in it. */
13141 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13142 set_buffer_internal_1 (old);
13143 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13144
13145 unbind_to (count, Qnil);
13146 }
13147
13148
13149 /* Build the complete desired matrix of WINDOW with a window start
13150 buffer position POS.
13151
13152 Value is 1 if successful. It is zero if fonts were loaded during
13153 redisplay which makes re-adjusting glyph matrices necessary, and -1
13154 if point would appear in the scroll margins.
13155 (We check that only if CHECK_MARGINS is nonzero. */
13156
13157 int
13158 try_window (window, pos, check_margins)
13159 Lisp_Object window;
13160 struct text_pos pos;
13161 int check_margins;
13162 {
13163 struct window *w = XWINDOW (window);
13164 struct it it;
13165 struct glyph_row *last_text_row = NULL;
13166
13167 /* Make POS the new window start. */
13168 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13169
13170 /* Mark cursor position as unknown. No overlay arrow seen. */
13171 w->cursor.vpos = -1;
13172 overlay_arrow_seen = 0;
13173
13174 /* Initialize iterator and info to start at POS. */
13175 start_display (&it, w, pos);
13176
13177 /* Display all lines of W. */
13178 while (it.current_y < it.last_visible_y)
13179 {
13180 if (display_line (&it))
13181 last_text_row = it.glyph_row - 1;
13182 if (fonts_changed_p)
13183 return 0;
13184 }
13185
13186 /* Don't let the cursor end in the scroll margins. */
13187 if (check_margins
13188 && !MINI_WINDOW_P (w))
13189 {
13190 int this_scroll_margin;
13191
13192 this_scroll_margin = max (0, scroll_margin);
13193 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13194 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13195
13196 if ((w->cursor.y < this_scroll_margin
13197 && CHARPOS (pos) > BEGV
13198 && IT_CHARPOS (it) < ZV)
13199 /* rms: considering make_cursor_line_fully_visible_p here
13200 seems to give wrong results. We don't want to recenter
13201 when the last line is partly visible, we want to allow
13202 that case to be handled in the usual way. */
13203 || (w->cursor.y + 1) > it.last_visible_y)
13204 {
13205 w->cursor.vpos = -1;
13206 clear_glyph_matrix (w->desired_matrix);
13207 return -1;
13208 }
13209 }
13210
13211 /* If bottom moved off end of frame, change mode line percentage. */
13212 if (XFASTINT (w->window_end_pos) <= 0
13213 && Z != IT_CHARPOS (it))
13214 w->update_mode_line = Qt;
13215
13216 /* Set window_end_pos to the offset of the last character displayed
13217 on the window from the end of current_buffer. Set
13218 window_end_vpos to its row number. */
13219 if (last_text_row)
13220 {
13221 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13222 w->window_end_bytepos
13223 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13224 w->window_end_pos
13225 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13226 w->window_end_vpos
13227 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13228 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13229 ->displays_text_p);
13230 }
13231 else
13232 {
13233 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13234 w->window_end_pos = make_number (Z - ZV);
13235 w->window_end_vpos = make_number (0);
13236 }
13237
13238 /* But that is not valid info until redisplay finishes. */
13239 w->window_end_valid = Qnil;
13240 return 1;
13241 }
13242
13243
13244 \f
13245 /************************************************************************
13246 Window redisplay reusing current matrix when buffer has not changed
13247 ************************************************************************/
13248
13249 /* Try redisplay of window W showing an unchanged buffer with a
13250 different window start than the last time it was displayed by
13251 reusing its current matrix. Value is non-zero if successful.
13252 W->start is the new window start. */
13253
13254 static int
13255 try_window_reusing_current_matrix (w)
13256 struct window *w;
13257 {
13258 struct frame *f = XFRAME (w->frame);
13259 struct glyph_row *row, *bottom_row;
13260 struct it it;
13261 struct run run;
13262 struct text_pos start, new_start;
13263 int nrows_scrolled, i;
13264 struct glyph_row *last_text_row;
13265 struct glyph_row *last_reused_text_row;
13266 struct glyph_row *start_row;
13267 int start_vpos, min_y, max_y;
13268
13269 #if GLYPH_DEBUG
13270 if (inhibit_try_window_reusing)
13271 return 0;
13272 #endif
13273
13274 if (/* This function doesn't handle terminal frames. */
13275 !FRAME_WINDOW_P (f)
13276 /* Don't try to reuse the display if windows have been split
13277 or such. */
13278 || windows_or_buffers_changed
13279 || cursor_type_changed)
13280 return 0;
13281
13282 /* Can't do this if region may have changed. */
13283 if ((!NILP (Vtransient_mark_mode)
13284 && !NILP (current_buffer->mark_active))
13285 || !NILP (w->region_showing)
13286 || !NILP (Vshow_trailing_whitespace))
13287 return 0;
13288
13289 /* If top-line visibility has changed, give up. */
13290 if (WINDOW_WANTS_HEADER_LINE_P (w)
13291 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13292 return 0;
13293
13294 /* Give up if old or new display is scrolled vertically. We could
13295 make this function handle this, but right now it doesn't. */
13296 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13297 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13298 return 0;
13299
13300 /* The variable new_start now holds the new window start. The old
13301 start `start' can be determined from the current matrix. */
13302 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13303 start = start_row->start.pos;
13304 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13305
13306 /* Clear the desired matrix for the display below. */
13307 clear_glyph_matrix (w->desired_matrix);
13308
13309 if (CHARPOS (new_start) <= CHARPOS (start))
13310 {
13311 int first_row_y;
13312
13313 /* Don't use this method if the display starts with an ellipsis
13314 displayed for invisible text. It's not easy to handle that case
13315 below, and it's certainly not worth the effort since this is
13316 not a frequent case. */
13317 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13318 return 0;
13319
13320 IF_DEBUG (debug_method_add (w, "twu1"));
13321
13322 /* Display up to a row that can be reused. The variable
13323 last_text_row is set to the last row displayed that displays
13324 text. Note that it.vpos == 0 if or if not there is a
13325 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13326 start_display (&it, w, new_start);
13327 first_row_y = it.current_y;
13328 w->cursor.vpos = -1;
13329 last_text_row = last_reused_text_row = NULL;
13330
13331 while (it.current_y < it.last_visible_y
13332 && !fonts_changed_p)
13333 {
13334 /* If we have reached into the characters in the START row,
13335 that means the line boundaries have changed. So we
13336 can't start copying with the row START. Maybe it will
13337 work to start copying with the following row. */
13338 while (IT_CHARPOS (it) > CHARPOS (start))
13339 {
13340 /* Advance to the next row as the "start". */
13341 start_row++;
13342 start = start_row->start.pos;
13343 /* If there are no more rows to try, or just one, give up. */
13344 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13345 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13346 || CHARPOS (start) == ZV)
13347 {
13348 clear_glyph_matrix (w->desired_matrix);
13349 return 0;
13350 }
13351
13352 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13353 }
13354 /* If we have reached alignment,
13355 we can copy the rest of the rows. */
13356 if (IT_CHARPOS (it) == CHARPOS (start))
13357 break;
13358
13359 if (display_line (&it))
13360 last_text_row = it.glyph_row - 1;
13361 }
13362
13363 /* A value of current_y < last_visible_y means that we stopped
13364 at the previous window start, which in turn means that we
13365 have at least one reusable row. */
13366 if (it.current_y < it.last_visible_y)
13367 {
13368 /* IT.vpos always starts from 0; it counts text lines. */
13369 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13370
13371 /* Find PT if not already found in the lines displayed. */
13372 if (w->cursor.vpos < 0)
13373 {
13374 int dy = it.current_y - start_row->y;
13375
13376 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13377 row = row_containing_pos (w, PT, row, NULL, dy);
13378 if (row)
13379 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13380 dy, nrows_scrolled);
13381 else
13382 {
13383 clear_glyph_matrix (w->desired_matrix);
13384 return 0;
13385 }
13386 }
13387
13388 /* Scroll the display. Do it before the current matrix is
13389 changed. The problem here is that update has not yet
13390 run, i.e. part of the current matrix is not up to date.
13391 scroll_run_hook will clear the cursor, and use the
13392 current matrix to get the height of the row the cursor is
13393 in. */
13394 run.current_y = start_row->y;
13395 run.desired_y = it.current_y;
13396 run.height = it.last_visible_y - it.current_y;
13397
13398 if (run.height > 0 && run.current_y != run.desired_y)
13399 {
13400 update_begin (f);
13401 FRAME_RIF (f)->update_window_begin_hook (w);
13402 FRAME_RIF (f)->clear_window_mouse_face (w);
13403 FRAME_RIF (f)->scroll_run_hook (w, &run);
13404 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13405 update_end (f);
13406 }
13407
13408 /* Shift current matrix down by nrows_scrolled lines. */
13409 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13410 rotate_matrix (w->current_matrix,
13411 start_vpos,
13412 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13413 nrows_scrolled);
13414
13415 /* Disable lines that must be updated. */
13416 for (i = 0; i < it.vpos; ++i)
13417 (start_row + i)->enabled_p = 0;
13418
13419 /* Re-compute Y positions. */
13420 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13421 max_y = it.last_visible_y;
13422 for (row = start_row + nrows_scrolled;
13423 row < bottom_row;
13424 ++row)
13425 {
13426 row->y = it.current_y;
13427 row->visible_height = row->height;
13428
13429 if (row->y < min_y)
13430 row->visible_height -= min_y - row->y;
13431 if (row->y + row->height > max_y)
13432 row->visible_height -= row->y + row->height - max_y;
13433 row->redraw_fringe_bitmaps_p = 1;
13434
13435 it.current_y += row->height;
13436
13437 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13438 last_reused_text_row = row;
13439 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13440 break;
13441 }
13442
13443 /* Disable lines in the current matrix which are now
13444 below the window. */
13445 for (++row; row < bottom_row; ++row)
13446 row->enabled_p = row->mode_line_p = 0;
13447 }
13448
13449 /* Update window_end_pos etc.; last_reused_text_row is the last
13450 reused row from the current matrix containing text, if any.
13451 The value of last_text_row is the last displayed line
13452 containing text. */
13453 if (last_reused_text_row)
13454 {
13455 w->window_end_bytepos
13456 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13457 w->window_end_pos
13458 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13459 w->window_end_vpos
13460 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13461 w->current_matrix));
13462 }
13463 else if (last_text_row)
13464 {
13465 w->window_end_bytepos
13466 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13467 w->window_end_pos
13468 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13469 w->window_end_vpos
13470 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13471 }
13472 else
13473 {
13474 /* This window must be completely empty. */
13475 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13476 w->window_end_pos = make_number (Z - ZV);
13477 w->window_end_vpos = make_number (0);
13478 }
13479 w->window_end_valid = Qnil;
13480
13481 /* Update hint: don't try scrolling again in update_window. */
13482 w->desired_matrix->no_scrolling_p = 1;
13483
13484 #if GLYPH_DEBUG
13485 debug_method_add (w, "try_window_reusing_current_matrix 1");
13486 #endif
13487 return 1;
13488 }
13489 else if (CHARPOS (new_start) > CHARPOS (start))
13490 {
13491 struct glyph_row *pt_row, *row;
13492 struct glyph_row *first_reusable_row;
13493 struct glyph_row *first_row_to_display;
13494 int dy;
13495 int yb = window_text_bottom_y (w);
13496
13497 /* Find the row starting at new_start, if there is one. Don't
13498 reuse a partially visible line at the end. */
13499 first_reusable_row = start_row;
13500 while (first_reusable_row->enabled_p
13501 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13502 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13503 < CHARPOS (new_start)))
13504 ++first_reusable_row;
13505
13506 /* Give up if there is no row to reuse. */
13507 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13508 || !first_reusable_row->enabled_p
13509 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13510 != CHARPOS (new_start)))
13511 return 0;
13512
13513 /* We can reuse fully visible rows beginning with
13514 first_reusable_row to the end of the window. Set
13515 first_row_to_display to the first row that cannot be reused.
13516 Set pt_row to the row containing point, if there is any. */
13517 pt_row = NULL;
13518 for (first_row_to_display = first_reusable_row;
13519 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13520 ++first_row_to_display)
13521 {
13522 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13523 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13524 pt_row = first_row_to_display;
13525 }
13526
13527 /* Start displaying at the start of first_row_to_display. */
13528 xassert (first_row_to_display->y < yb);
13529 init_to_row_start (&it, w, first_row_to_display);
13530
13531 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13532 - start_vpos);
13533 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13534 - nrows_scrolled);
13535 it.current_y = (first_row_to_display->y - first_reusable_row->y
13536 + WINDOW_HEADER_LINE_HEIGHT (w));
13537
13538 /* Display lines beginning with first_row_to_display in the
13539 desired matrix. Set last_text_row to the last row displayed
13540 that displays text. */
13541 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13542 if (pt_row == NULL)
13543 w->cursor.vpos = -1;
13544 last_text_row = NULL;
13545 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13546 if (display_line (&it))
13547 last_text_row = it.glyph_row - 1;
13548
13549 /* Give up If point isn't in a row displayed or reused. */
13550 if (w->cursor.vpos < 0)
13551 {
13552 clear_glyph_matrix (w->desired_matrix);
13553 return 0;
13554 }
13555
13556 /* If point is in a reused row, adjust y and vpos of the cursor
13557 position. */
13558 if (pt_row)
13559 {
13560 w->cursor.vpos -= nrows_scrolled;
13561 w->cursor.y -= first_reusable_row->y - start_row->y;
13562 }
13563
13564 /* Scroll the display. */
13565 run.current_y = first_reusable_row->y;
13566 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13567 run.height = it.last_visible_y - run.current_y;
13568 dy = run.current_y - run.desired_y;
13569
13570 if (run.height)
13571 {
13572 update_begin (f);
13573 FRAME_RIF (f)->update_window_begin_hook (w);
13574 FRAME_RIF (f)->clear_window_mouse_face (w);
13575 FRAME_RIF (f)->scroll_run_hook (w, &run);
13576 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13577 update_end (f);
13578 }
13579
13580 /* Adjust Y positions of reused rows. */
13581 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13582 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13583 max_y = it.last_visible_y;
13584 for (row = first_reusable_row; row < first_row_to_display; ++row)
13585 {
13586 row->y -= dy;
13587 row->visible_height = row->height;
13588 if (row->y < min_y)
13589 row->visible_height -= min_y - row->y;
13590 if (row->y + row->height > max_y)
13591 row->visible_height -= row->y + row->height - max_y;
13592 row->redraw_fringe_bitmaps_p = 1;
13593 }
13594
13595 /* Scroll the current matrix. */
13596 xassert (nrows_scrolled > 0);
13597 rotate_matrix (w->current_matrix,
13598 start_vpos,
13599 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13600 -nrows_scrolled);
13601
13602 /* Disable rows not reused. */
13603 for (row -= nrows_scrolled; row < bottom_row; ++row)
13604 row->enabled_p = 0;
13605
13606 /* Point may have moved to a different line, so we cannot assume that
13607 the previous cursor position is valid; locate the correct row. */
13608 if (pt_row)
13609 {
13610 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13611 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13612 row++)
13613 {
13614 w->cursor.vpos++;
13615 w->cursor.y = row->y;
13616 }
13617 if (row < bottom_row)
13618 {
13619 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13620 while (glyph->charpos < PT)
13621 {
13622 w->cursor.hpos++;
13623 w->cursor.x += glyph->pixel_width;
13624 glyph++;
13625 }
13626 }
13627 }
13628
13629 /* Adjust window end. A null value of last_text_row means that
13630 the window end is in reused rows which in turn means that
13631 only its vpos can have changed. */
13632 if (last_text_row)
13633 {
13634 w->window_end_bytepos
13635 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13636 w->window_end_pos
13637 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13638 w->window_end_vpos
13639 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13640 }
13641 else
13642 {
13643 w->window_end_vpos
13644 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13645 }
13646
13647 w->window_end_valid = Qnil;
13648 w->desired_matrix->no_scrolling_p = 1;
13649
13650 #if GLYPH_DEBUG
13651 debug_method_add (w, "try_window_reusing_current_matrix 2");
13652 #endif
13653 return 1;
13654 }
13655
13656 return 0;
13657 }
13658
13659
13660 \f
13661 /************************************************************************
13662 Window redisplay reusing current matrix when buffer has changed
13663 ************************************************************************/
13664
13665 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13666 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13667 int *, int *));
13668 static struct glyph_row *
13669 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13670 struct glyph_row *));
13671
13672
13673 /* Return the last row in MATRIX displaying text. If row START is
13674 non-null, start searching with that row. IT gives the dimensions
13675 of the display. Value is null if matrix is empty; otherwise it is
13676 a pointer to the row found. */
13677
13678 static struct glyph_row *
13679 find_last_row_displaying_text (matrix, it, start)
13680 struct glyph_matrix *matrix;
13681 struct it *it;
13682 struct glyph_row *start;
13683 {
13684 struct glyph_row *row, *row_found;
13685
13686 /* Set row_found to the last row in IT->w's current matrix
13687 displaying text. The loop looks funny but think of partially
13688 visible lines. */
13689 row_found = NULL;
13690 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13691 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13692 {
13693 xassert (row->enabled_p);
13694 row_found = row;
13695 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13696 break;
13697 ++row;
13698 }
13699
13700 return row_found;
13701 }
13702
13703
13704 /* Return the last row in the current matrix of W that is not affected
13705 by changes at the start of current_buffer that occurred since W's
13706 current matrix was built. Value is null if no such row exists.
13707
13708 BEG_UNCHANGED us the number of characters unchanged at the start of
13709 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13710 first changed character in current_buffer. Characters at positions <
13711 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13712 when the current matrix was built. */
13713
13714 static struct glyph_row *
13715 find_last_unchanged_at_beg_row (w)
13716 struct window *w;
13717 {
13718 int first_changed_pos = BEG + BEG_UNCHANGED;
13719 struct glyph_row *row;
13720 struct glyph_row *row_found = NULL;
13721 int yb = window_text_bottom_y (w);
13722
13723 /* Find the last row displaying unchanged text. */
13724 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13725 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13726 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13727 {
13728 if (/* If row ends before first_changed_pos, it is unchanged,
13729 except in some case. */
13730 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13731 /* When row ends in ZV and we write at ZV it is not
13732 unchanged. */
13733 && !row->ends_at_zv_p
13734 /* When first_changed_pos is the end of a continued line,
13735 row is not unchanged because it may be no longer
13736 continued. */
13737 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13738 && (row->continued_p
13739 || row->exact_window_width_line_p)))
13740 row_found = row;
13741
13742 /* Stop if last visible row. */
13743 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13744 break;
13745
13746 ++row;
13747 }
13748
13749 return row_found;
13750 }
13751
13752
13753 /* Find the first glyph row in the current matrix of W that is not
13754 affected by changes at the end of current_buffer since the
13755 time W's current matrix was built.
13756
13757 Return in *DELTA the number of chars by which buffer positions in
13758 unchanged text at the end of current_buffer must be adjusted.
13759
13760 Return in *DELTA_BYTES the corresponding number of bytes.
13761
13762 Value is null if no such row exists, i.e. all rows are affected by
13763 changes. */
13764
13765 static struct glyph_row *
13766 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13767 struct window *w;
13768 int *delta, *delta_bytes;
13769 {
13770 struct glyph_row *row;
13771 struct glyph_row *row_found = NULL;
13772
13773 *delta = *delta_bytes = 0;
13774
13775 /* Display must not have been paused, otherwise the current matrix
13776 is not up to date. */
13777 if (NILP (w->window_end_valid))
13778 abort ();
13779
13780 /* A value of window_end_pos >= END_UNCHANGED means that the window
13781 end is in the range of changed text. If so, there is no
13782 unchanged row at the end of W's current matrix. */
13783 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13784 return NULL;
13785
13786 /* Set row to the last row in W's current matrix displaying text. */
13787 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13788
13789 /* If matrix is entirely empty, no unchanged row exists. */
13790 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13791 {
13792 /* The value of row is the last glyph row in the matrix having a
13793 meaningful buffer position in it. The end position of row
13794 corresponds to window_end_pos. This allows us to translate
13795 buffer positions in the current matrix to current buffer
13796 positions for characters not in changed text. */
13797 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13798 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13799 int last_unchanged_pos, last_unchanged_pos_old;
13800 struct glyph_row *first_text_row
13801 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13802
13803 *delta = Z - Z_old;
13804 *delta_bytes = Z_BYTE - Z_BYTE_old;
13805
13806 /* Set last_unchanged_pos to the buffer position of the last
13807 character in the buffer that has not been changed. Z is the
13808 index + 1 of the last character in current_buffer, i.e. by
13809 subtracting END_UNCHANGED we get the index of the last
13810 unchanged character, and we have to add BEG to get its buffer
13811 position. */
13812 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13813 last_unchanged_pos_old = last_unchanged_pos - *delta;
13814
13815 /* Search backward from ROW for a row displaying a line that
13816 starts at a minimum position >= last_unchanged_pos_old. */
13817 for (; row > first_text_row; --row)
13818 {
13819 /* This used to abort, but it can happen.
13820 It is ok to just stop the search instead here. KFS. */
13821 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13822 break;
13823
13824 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13825 row_found = row;
13826 }
13827 }
13828
13829 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13830 abort ();
13831
13832 return row_found;
13833 }
13834
13835
13836 /* Make sure that glyph rows in the current matrix of window W
13837 reference the same glyph memory as corresponding rows in the
13838 frame's frame matrix. This function is called after scrolling W's
13839 current matrix on a terminal frame in try_window_id and
13840 try_window_reusing_current_matrix. */
13841
13842 static void
13843 sync_frame_with_window_matrix_rows (w)
13844 struct window *w;
13845 {
13846 struct frame *f = XFRAME (w->frame);
13847 struct glyph_row *window_row, *window_row_end, *frame_row;
13848
13849 /* Preconditions: W must be a leaf window and full-width. Its frame
13850 must have a frame matrix. */
13851 xassert (NILP (w->hchild) && NILP (w->vchild));
13852 xassert (WINDOW_FULL_WIDTH_P (w));
13853 xassert (!FRAME_WINDOW_P (f));
13854
13855 /* If W is a full-width window, glyph pointers in W's current matrix
13856 have, by definition, to be the same as glyph pointers in the
13857 corresponding frame matrix. Note that frame matrices have no
13858 marginal areas (see build_frame_matrix). */
13859 window_row = w->current_matrix->rows;
13860 window_row_end = window_row + w->current_matrix->nrows;
13861 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13862 while (window_row < window_row_end)
13863 {
13864 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13865 struct glyph *end = window_row->glyphs[LAST_AREA];
13866
13867 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13868 frame_row->glyphs[TEXT_AREA] = start;
13869 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13870 frame_row->glyphs[LAST_AREA] = end;
13871
13872 /* Disable frame rows whose corresponding window rows have
13873 been disabled in try_window_id. */
13874 if (!window_row->enabled_p)
13875 frame_row->enabled_p = 0;
13876
13877 ++window_row, ++frame_row;
13878 }
13879 }
13880
13881
13882 /* Find the glyph row in window W containing CHARPOS. Consider all
13883 rows between START and END (not inclusive). END null means search
13884 all rows to the end of the display area of W. Value is the row
13885 containing CHARPOS or null. */
13886
13887 struct glyph_row *
13888 row_containing_pos (w, charpos, start, end, dy)
13889 struct window *w;
13890 int charpos;
13891 struct glyph_row *start, *end;
13892 int dy;
13893 {
13894 struct glyph_row *row = start;
13895 int last_y;
13896
13897 /* If we happen to start on a header-line, skip that. */
13898 if (row->mode_line_p)
13899 ++row;
13900
13901 if ((end && row >= end) || !row->enabled_p)
13902 return NULL;
13903
13904 last_y = window_text_bottom_y (w) - dy;
13905
13906 while (1)
13907 {
13908 /* Give up if we have gone too far. */
13909 if (end && row >= end)
13910 return NULL;
13911 /* This formerly returned if they were equal.
13912 I think that both quantities are of a "last plus one" type;
13913 if so, when they are equal, the row is within the screen. -- rms. */
13914 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13915 return NULL;
13916
13917 /* If it is in this row, return this row. */
13918 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13919 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13920 /* The end position of a row equals the start
13921 position of the next row. If CHARPOS is there, we
13922 would rather display it in the next line, except
13923 when this line ends in ZV. */
13924 && !row->ends_at_zv_p
13925 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13926 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13927 return row;
13928 ++row;
13929 }
13930 }
13931
13932
13933 /* Try to redisplay window W by reusing its existing display. W's
13934 current matrix must be up to date when this function is called,
13935 i.e. window_end_valid must not be nil.
13936
13937 Value is
13938
13939 1 if display has been updated
13940 0 if otherwise unsuccessful
13941 -1 if redisplay with same window start is known not to succeed
13942
13943 The following steps are performed:
13944
13945 1. Find the last row in the current matrix of W that is not
13946 affected by changes at the start of current_buffer. If no such row
13947 is found, give up.
13948
13949 2. Find the first row in W's current matrix that is not affected by
13950 changes at the end of current_buffer. Maybe there is no such row.
13951
13952 3. Display lines beginning with the row + 1 found in step 1 to the
13953 row found in step 2 or, if step 2 didn't find a row, to the end of
13954 the window.
13955
13956 4. If cursor is not known to appear on the window, give up.
13957
13958 5. If display stopped at the row found in step 2, scroll the
13959 display and current matrix as needed.
13960
13961 6. Maybe display some lines at the end of W, if we must. This can
13962 happen under various circumstances, like a partially visible line
13963 becoming fully visible, or because newly displayed lines are displayed
13964 in smaller font sizes.
13965
13966 7. Update W's window end information. */
13967
13968 static int
13969 try_window_id (w)
13970 struct window *w;
13971 {
13972 struct frame *f = XFRAME (w->frame);
13973 struct glyph_matrix *current_matrix = w->current_matrix;
13974 struct glyph_matrix *desired_matrix = w->desired_matrix;
13975 struct glyph_row *last_unchanged_at_beg_row;
13976 struct glyph_row *first_unchanged_at_end_row;
13977 struct glyph_row *row;
13978 struct glyph_row *bottom_row;
13979 int bottom_vpos;
13980 struct it it;
13981 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13982 struct text_pos start_pos;
13983 struct run run;
13984 int first_unchanged_at_end_vpos = 0;
13985 struct glyph_row *last_text_row, *last_text_row_at_end;
13986 struct text_pos start;
13987 int first_changed_charpos, last_changed_charpos;
13988
13989 #if GLYPH_DEBUG
13990 if (inhibit_try_window_id)
13991 return 0;
13992 #endif
13993
13994 /* This is handy for debugging. */
13995 #if 0
13996 #define GIVE_UP(X) \
13997 do { \
13998 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13999 return 0; \
14000 } while (0)
14001 #else
14002 #define GIVE_UP(X) return 0
14003 #endif
14004
14005 SET_TEXT_POS_FROM_MARKER (start, w->start);
14006
14007 /* Don't use this for mini-windows because these can show
14008 messages and mini-buffers, and we don't handle that here. */
14009 if (MINI_WINDOW_P (w))
14010 GIVE_UP (1);
14011
14012 /* This flag is used to prevent redisplay optimizations. */
14013 if (windows_or_buffers_changed || cursor_type_changed)
14014 GIVE_UP (2);
14015
14016 /* Verify that narrowing has not changed.
14017 Also verify that we were not told to prevent redisplay optimizations.
14018 It would be nice to further
14019 reduce the number of cases where this prevents try_window_id. */
14020 if (current_buffer->clip_changed
14021 || current_buffer->prevent_redisplay_optimizations_p)
14022 GIVE_UP (3);
14023
14024 /* Window must either use window-based redisplay or be full width. */
14025 if (!FRAME_WINDOW_P (f)
14026 && (!FRAME_LINE_INS_DEL_OK (f)
14027 || !WINDOW_FULL_WIDTH_P (w)))
14028 GIVE_UP (4);
14029
14030 /* Give up if point is not known NOT to appear in W. */
14031 if (PT < CHARPOS (start))
14032 GIVE_UP (5);
14033
14034 /* Another way to prevent redisplay optimizations. */
14035 if (XFASTINT (w->last_modified) == 0)
14036 GIVE_UP (6);
14037
14038 /* Verify that window is not hscrolled. */
14039 if (XFASTINT (w->hscroll) != 0)
14040 GIVE_UP (7);
14041
14042 /* Verify that display wasn't paused. */
14043 if (NILP (w->window_end_valid))
14044 GIVE_UP (8);
14045
14046 /* Can't use this if highlighting a region because a cursor movement
14047 will do more than just set the cursor. */
14048 if (!NILP (Vtransient_mark_mode)
14049 && !NILP (current_buffer->mark_active))
14050 GIVE_UP (9);
14051
14052 /* Likewise if highlighting trailing whitespace. */
14053 if (!NILP (Vshow_trailing_whitespace))
14054 GIVE_UP (11);
14055
14056 /* Likewise if showing a region. */
14057 if (!NILP (w->region_showing))
14058 GIVE_UP (10);
14059
14060 /* Can use this if overlay arrow position and or string have changed. */
14061 if (overlay_arrows_changed_p ())
14062 GIVE_UP (12);
14063
14064
14065 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14066 only if buffer has really changed. The reason is that the gap is
14067 initially at Z for freshly visited files. The code below would
14068 set end_unchanged to 0 in that case. */
14069 if (MODIFF > SAVE_MODIFF
14070 /* This seems to happen sometimes after saving a buffer. */
14071 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14072 {
14073 if (GPT - BEG < BEG_UNCHANGED)
14074 BEG_UNCHANGED = GPT - BEG;
14075 if (Z - GPT < END_UNCHANGED)
14076 END_UNCHANGED = Z - GPT;
14077 }
14078
14079 /* The position of the first and last character that has been changed. */
14080 first_changed_charpos = BEG + BEG_UNCHANGED;
14081 last_changed_charpos = Z - END_UNCHANGED;
14082
14083 /* If window starts after a line end, and the last change is in
14084 front of that newline, then changes don't affect the display.
14085 This case happens with stealth-fontification. Note that although
14086 the display is unchanged, glyph positions in the matrix have to
14087 be adjusted, of course. */
14088 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14089 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14090 && ((last_changed_charpos < CHARPOS (start)
14091 && CHARPOS (start) == BEGV)
14092 || (last_changed_charpos < CHARPOS (start) - 1
14093 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14094 {
14095 int Z_old, delta, Z_BYTE_old, delta_bytes;
14096 struct glyph_row *r0;
14097
14098 /* Compute how many chars/bytes have been added to or removed
14099 from the buffer. */
14100 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14101 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14102 delta = Z - Z_old;
14103 delta_bytes = Z_BYTE - Z_BYTE_old;
14104
14105 /* Give up if PT is not in the window. Note that it already has
14106 been checked at the start of try_window_id that PT is not in
14107 front of the window start. */
14108 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14109 GIVE_UP (13);
14110
14111 /* If window start is unchanged, we can reuse the whole matrix
14112 as is, after adjusting glyph positions. No need to compute
14113 the window end again, since its offset from Z hasn't changed. */
14114 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14115 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14116 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14117 /* PT must not be in a partially visible line. */
14118 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14119 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14120 {
14121 /* Adjust positions in the glyph matrix. */
14122 if (delta || delta_bytes)
14123 {
14124 struct glyph_row *r1
14125 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14126 increment_matrix_positions (w->current_matrix,
14127 MATRIX_ROW_VPOS (r0, current_matrix),
14128 MATRIX_ROW_VPOS (r1, current_matrix),
14129 delta, delta_bytes);
14130 }
14131
14132 /* Set the cursor. */
14133 row = row_containing_pos (w, PT, r0, NULL, 0);
14134 if (row)
14135 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14136 else
14137 abort ();
14138 return 1;
14139 }
14140 }
14141
14142 /* Handle the case that changes are all below what is displayed in
14143 the window, and that PT is in the window. This shortcut cannot
14144 be taken if ZV is visible in the window, and text has been added
14145 there that is visible in the window. */
14146 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14147 /* ZV is not visible in the window, or there are no
14148 changes at ZV, actually. */
14149 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14150 || first_changed_charpos == last_changed_charpos))
14151 {
14152 struct glyph_row *r0;
14153
14154 /* Give up if PT is not in the window. Note that it already has
14155 been checked at the start of try_window_id that PT is not in
14156 front of the window start. */
14157 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14158 GIVE_UP (14);
14159
14160 /* If window start is unchanged, we can reuse the whole matrix
14161 as is, without changing glyph positions since no text has
14162 been added/removed in front of the window end. */
14163 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14164 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14165 /* PT must not be in a partially visible line. */
14166 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14167 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14168 {
14169 /* We have to compute the window end anew since text
14170 can have been added/removed after it. */
14171 w->window_end_pos
14172 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14173 w->window_end_bytepos
14174 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14175
14176 /* Set the cursor. */
14177 row = row_containing_pos (w, PT, r0, NULL, 0);
14178 if (row)
14179 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14180 else
14181 abort ();
14182 return 2;
14183 }
14184 }
14185
14186 /* Give up if window start is in the changed area.
14187
14188 The condition used to read
14189
14190 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14191
14192 but why that was tested escapes me at the moment. */
14193 if (CHARPOS (start) >= first_changed_charpos
14194 && CHARPOS (start) <= last_changed_charpos)
14195 GIVE_UP (15);
14196
14197 /* Check that window start agrees with the start of the first glyph
14198 row in its current matrix. Check this after we know the window
14199 start is not in changed text, otherwise positions would not be
14200 comparable. */
14201 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14202 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14203 GIVE_UP (16);
14204
14205 /* Give up if the window ends in strings. Overlay strings
14206 at the end are difficult to handle, so don't try. */
14207 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14208 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14209 GIVE_UP (20);
14210
14211 /* Compute the position at which we have to start displaying new
14212 lines. Some of the lines at the top of the window might be
14213 reusable because they are not displaying changed text. Find the
14214 last row in W's current matrix not affected by changes at the
14215 start of current_buffer. Value is null if changes start in the
14216 first line of window. */
14217 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14218 if (last_unchanged_at_beg_row)
14219 {
14220 /* Avoid starting to display in the moddle of a character, a TAB
14221 for instance. This is easier than to set up the iterator
14222 exactly, and it's not a frequent case, so the additional
14223 effort wouldn't really pay off. */
14224 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14225 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14226 && last_unchanged_at_beg_row > w->current_matrix->rows)
14227 --last_unchanged_at_beg_row;
14228
14229 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14230 GIVE_UP (17);
14231
14232 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14233 GIVE_UP (18);
14234 start_pos = it.current.pos;
14235
14236 /* Start displaying new lines in the desired matrix at the same
14237 vpos we would use in the current matrix, i.e. below
14238 last_unchanged_at_beg_row. */
14239 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14240 current_matrix);
14241 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14242 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14243
14244 xassert (it.hpos == 0 && it.current_x == 0);
14245 }
14246 else
14247 {
14248 /* There are no reusable lines at the start of the window.
14249 Start displaying in the first text line. */
14250 start_display (&it, w, start);
14251 it.vpos = it.first_vpos;
14252 start_pos = it.current.pos;
14253 }
14254
14255 /* Find the first row that is not affected by changes at the end of
14256 the buffer. Value will be null if there is no unchanged row, in
14257 which case we must redisplay to the end of the window. delta
14258 will be set to the value by which buffer positions beginning with
14259 first_unchanged_at_end_row have to be adjusted due to text
14260 changes. */
14261 first_unchanged_at_end_row
14262 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14263 IF_DEBUG (debug_delta = delta);
14264 IF_DEBUG (debug_delta_bytes = delta_bytes);
14265
14266 /* Set stop_pos to the buffer position up to which we will have to
14267 display new lines. If first_unchanged_at_end_row != NULL, this
14268 is the buffer position of the start of the line displayed in that
14269 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14270 that we don't stop at a buffer position. */
14271 stop_pos = 0;
14272 if (first_unchanged_at_end_row)
14273 {
14274 xassert (last_unchanged_at_beg_row == NULL
14275 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14276
14277 /* If this is a continuation line, move forward to the next one
14278 that isn't. Changes in lines above affect this line.
14279 Caution: this may move first_unchanged_at_end_row to a row
14280 not displaying text. */
14281 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14282 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14283 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14284 < it.last_visible_y))
14285 ++first_unchanged_at_end_row;
14286
14287 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14288 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14289 >= it.last_visible_y))
14290 first_unchanged_at_end_row = NULL;
14291 else
14292 {
14293 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14294 + delta);
14295 first_unchanged_at_end_vpos
14296 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14297 xassert (stop_pos >= Z - END_UNCHANGED);
14298 }
14299 }
14300 else if (last_unchanged_at_beg_row == NULL)
14301 GIVE_UP (19);
14302
14303
14304 #if GLYPH_DEBUG
14305
14306 /* Either there is no unchanged row at the end, or the one we have
14307 now displays text. This is a necessary condition for the window
14308 end pos calculation at the end of this function. */
14309 xassert (first_unchanged_at_end_row == NULL
14310 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14311
14312 debug_last_unchanged_at_beg_vpos
14313 = (last_unchanged_at_beg_row
14314 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14315 : -1);
14316 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14317
14318 #endif /* GLYPH_DEBUG != 0 */
14319
14320
14321 /* Display new lines. Set last_text_row to the last new line
14322 displayed which has text on it, i.e. might end up as being the
14323 line where the window_end_vpos is. */
14324 w->cursor.vpos = -1;
14325 last_text_row = NULL;
14326 overlay_arrow_seen = 0;
14327 while (it.current_y < it.last_visible_y
14328 && !fonts_changed_p
14329 && (first_unchanged_at_end_row == NULL
14330 || IT_CHARPOS (it) < stop_pos))
14331 {
14332 if (display_line (&it))
14333 last_text_row = it.glyph_row - 1;
14334 }
14335
14336 if (fonts_changed_p)
14337 return -1;
14338
14339
14340 /* Compute differences in buffer positions, y-positions etc. for
14341 lines reused at the bottom of the window. Compute what we can
14342 scroll. */
14343 if (first_unchanged_at_end_row
14344 /* No lines reused because we displayed everything up to the
14345 bottom of the window. */
14346 && it.current_y < it.last_visible_y)
14347 {
14348 dvpos = (it.vpos
14349 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14350 current_matrix));
14351 dy = it.current_y - first_unchanged_at_end_row->y;
14352 run.current_y = first_unchanged_at_end_row->y;
14353 run.desired_y = run.current_y + dy;
14354 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14355 }
14356 else
14357 {
14358 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14359 first_unchanged_at_end_row = NULL;
14360 }
14361 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14362
14363
14364 /* Find the cursor if not already found. We have to decide whether
14365 PT will appear on this window (it sometimes doesn't, but this is
14366 not a very frequent case.) This decision has to be made before
14367 the current matrix is altered. A value of cursor.vpos < 0 means
14368 that PT is either in one of the lines beginning at
14369 first_unchanged_at_end_row or below the window. Don't care for
14370 lines that might be displayed later at the window end; as
14371 mentioned, this is not a frequent case. */
14372 if (w->cursor.vpos < 0)
14373 {
14374 /* Cursor in unchanged rows at the top? */
14375 if (PT < CHARPOS (start_pos)
14376 && last_unchanged_at_beg_row)
14377 {
14378 row = row_containing_pos (w, PT,
14379 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14380 last_unchanged_at_beg_row + 1, 0);
14381 if (row)
14382 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14383 }
14384
14385 /* Start from first_unchanged_at_end_row looking for PT. */
14386 else if (first_unchanged_at_end_row)
14387 {
14388 row = row_containing_pos (w, PT - delta,
14389 first_unchanged_at_end_row, NULL, 0);
14390 if (row)
14391 set_cursor_from_row (w, row, w->current_matrix, delta,
14392 delta_bytes, dy, dvpos);
14393 }
14394
14395 /* Give up if cursor was not found. */
14396 if (w->cursor.vpos < 0)
14397 {
14398 clear_glyph_matrix (w->desired_matrix);
14399 return -1;
14400 }
14401 }
14402
14403 /* Don't let the cursor end in the scroll margins. */
14404 {
14405 int this_scroll_margin, cursor_height;
14406
14407 this_scroll_margin = max (0, scroll_margin);
14408 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14409 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14410 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14411
14412 if ((w->cursor.y < this_scroll_margin
14413 && CHARPOS (start) > BEGV)
14414 /* Old redisplay didn't take scroll margin into account at the bottom,
14415 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14416 || (w->cursor.y + (make_cursor_line_fully_visible_p
14417 ? cursor_height + this_scroll_margin
14418 : 1)) > it.last_visible_y)
14419 {
14420 w->cursor.vpos = -1;
14421 clear_glyph_matrix (w->desired_matrix);
14422 return -1;
14423 }
14424 }
14425
14426 /* Scroll the display. Do it before changing the current matrix so
14427 that xterm.c doesn't get confused about where the cursor glyph is
14428 found. */
14429 if (dy && run.height)
14430 {
14431 update_begin (f);
14432
14433 if (FRAME_WINDOW_P (f))
14434 {
14435 FRAME_RIF (f)->update_window_begin_hook (w);
14436 FRAME_RIF (f)->clear_window_mouse_face (w);
14437 FRAME_RIF (f)->scroll_run_hook (w, &run);
14438 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14439 }
14440 else
14441 {
14442 /* Terminal frame. In this case, dvpos gives the number of
14443 lines to scroll by; dvpos < 0 means scroll up. */
14444 int first_unchanged_at_end_vpos
14445 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14446 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14447 int end = (WINDOW_TOP_EDGE_LINE (w)
14448 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14449 + window_internal_height (w));
14450
14451 /* Perform the operation on the screen. */
14452 if (dvpos > 0)
14453 {
14454 /* Scroll last_unchanged_at_beg_row to the end of the
14455 window down dvpos lines. */
14456 set_terminal_window (f, end);
14457
14458 /* On dumb terminals delete dvpos lines at the end
14459 before inserting dvpos empty lines. */
14460 if (!FRAME_SCROLL_REGION_OK (f))
14461 ins_del_lines (f, end - dvpos, -dvpos);
14462
14463 /* Insert dvpos empty lines in front of
14464 last_unchanged_at_beg_row. */
14465 ins_del_lines (f, from, dvpos);
14466 }
14467 else if (dvpos < 0)
14468 {
14469 /* Scroll up last_unchanged_at_beg_vpos to the end of
14470 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14471 set_terminal_window (f, end);
14472
14473 /* Delete dvpos lines in front of
14474 last_unchanged_at_beg_vpos. ins_del_lines will set
14475 the cursor to the given vpos and emit |dvpos| delete
14476 line sequences. */
14477 ins_del_lines (f, from + dvpos, dvpos);
14478
14479 /* On a dumb terminal insert dvpos empty lines at the
14480 end. */
14481 if (!FRAME_SCROLL_REGION_OK (f))
14482 ins_del_lines (f, end + dvpos, -dvpos);
14483 }
14484
14485 set_terminal_window (f, 0);
14486 }
14487
14488 update_end (f);
14489 }
14490
14491 /* Shift reused rows of the current matrix to the right position.
14492 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14493 text. */
14494 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14495 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14496 if (dvpos < 0)
14497 {
14498 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14499 bottom_vpos, dvpos);
14500 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14501 bottom_vpos, 0);
14502 }
14503 else if (dvpos > 0)
14504 {
14505 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14506 bottom_vpos, dvpos);
14507 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14508 first_unchanged_at_end_vpos + dvpos, 0);
14509 }
14510
14511 /* For frame-based redisplay, make sure that current frame and window
14512 matrix are in sync with respect to glyph memory. */
14513 if (!FRAME_WINDOW_P (f))
14514 sync_frame_with_window_matrix_rows (w);
14515
14516 /* Adjust buffer positions in reused rows. */
14517 if (delta)
14518 increment_matrix_positions (current_matrix,
14519 first_unchanged_at_end_vpos + dvpos,
14520 bottom_vpos, delta, delta_bytes);
14521
14522 /* Adjust Y positions. */
14523 if (dy)
14524 shift_glyph_matrix (w, current_matrix,
14525 first_unchanged_at_end_vpos + dvpos,
14526 bottom_vpos, dy);
14527
14528 if (first_unchanged_at_end_row)
14529 {
14530 first_unchanged_at_end_row += dvpos;
14531 if (first_unchanged_at_end_row->y >= it.last_visible_y
14532 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14533 first_unchanged_at_end_row = NULL;
14534 }
14535
14536 /* If scrolling up, there may be some lines to display at the end of
14537 the window. */
14538 last_text_row_at_end = NULL;
14539 if (dy < 0)
14540 {
14541 /* Scrolling up can leave for example a partially visible line
14542 at the end of the window to be redisplayed. */
14543 /* Set last_row to the glyph row in the current matrix where the
14544 window end line is found. It has been moved up or down in
14545 the matrix by dvpos. */
14546 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14547 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14548
14549 /* If last_row is the window end line, it should display text. */
14550 xassert (last_row->displays_text_p);
14551
14552 /* If window end line was partially visible before, begin
14553 displaying at that line. Otherwise begin displaying with the
14554 line following it. */
14555 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14556 {
14557 init_to_row_start (&it, w, last_row);
14558 it.vpos = last_vpos;
14559 it.current_y = last_row->y;
14560 }
14561 else
14562 {
14563 init_to_row_end (&it, w, last_row);
14564 it.vpos = 1 + last_vpos;
14565 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14566 ++last_row;
14567 }
14568
14569 /* We may start in a continuation line. If so, we have to
14570 get the right continuation_lines_width and current_x. */
14571 it.continuation_lines_width = last_row->continuation_lines_width;
14572 it.hpos = it.current_x = 0;
14573
14574 /* Display the rest of the lines at the window end. */
14575 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14576 while (it.current_y < it.last_visible_y
14577 && !fonts_changed_p)
14578 {
14579 /* Is it always sure that the display agrees with lines in
14580 the current matrix? I don't think so, so we mark rows
14581 displayed invalid in the current matrix by setting their
14582 enabled_p flag to zero. */
14583 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14584 if (display_line (&it))
14585 last_text_row_at_end = it.glyph_row - 1;
14586 }
14587 }
14588
14589 /* Update window_end_pos and window_end_vpos. */
14590 if (first_unchanged_at_end_row
14591 && !last_text_row_at_end)
14592 {
14593 /* Window end line if one of the preserved rows from the current
14594 matrix. Set row to the last row displaying text in current
14595 matrix starting at first_unchanged_at_end_row, after
14596 scrolling. */
14597 xassert (first_unchanged_at_end_row->displays_text_p);
14598 row = find_last_row_displaying_text (w->current_matrix, &it,
14599 first_unchanged_at_end_row);
14600 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14601
14602 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14603 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14604 w->window_end_vpos
14605 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14606 xassert (w->window_end_bytepos >= 0);
14607 IF_DEBUG (debug_method_add (w, "A"));
14608 }
14609 else if (last_text_row_at_end)
14610 {
14611 w->window_end_pos
14612 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14613 w->window_end_bytepos
14614 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14615 w->window_end_vpos
14616 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14617 xassert (w->window_end_bytepos >= 0);
14618 IF_DEBUG (debug_method_add (w, "B"));
14619 }
14620 else if (last_text_row)
14621 {
14622 /* We have displayed either to the end of the window or at the
14623 end of the window, i.e. the last row with text is to be found
14624 in the desired matrix. */
14625 w->window_end_pos
14626 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14627 w->window_end_bytepos
14628 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14629 w->window_end_vpos
14630 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14631 xassert (w->window_end_bytepos >= 0);
14632 }
14633 else if (first_unchanged_at_end_row == NULL
14634 && last_text_row == NULL
14635 && last_text_row_at_end == NULL)
14636 {
14637 /* Displayed to end of window, but no line containing text was
14638 displayed. Lines were deleted at the end of the window. */
14639 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14640 int vpos = XFASTINT (w->window_end_vpos);
14641 struct glyph_row *current_row = current_matrix->rows + vpos;
14642 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14643
14644 for (row = NULL;
14645 row == NULL && vpos >= first_vpos;
14646 --vpos, --current_row, --desired_row)
14647 {
14648 if (desired_row->enabled_p)
14649 {
14650 if (desired_row->displays_text_p)
14651 row = desired_row;
14652 }
14653 else if (current_row->displays_text_p)
14654 row = current_row;
14655 }
14656
14657 xassert (row != NULL);
14658 w->window_end_vpos = make_number (vpos + 1);
14659 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14660 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14661 xassert (w->window_end_bytepos >= 0);
14662 IF_DEBUG (debug_method_add (w, "C"));
14663 }
14664 else
14665 abort ();
14666
14667 #if 0 /* This leads to problems, for instance when the cursor is
14668 at ZV, and the cursor line displays no text. */
14669 /* Disable rows below what's displayed in the window. This makes
14670 debugging easier. */
14671 enable_glyph_matrix_rows (current_matrix,
14672 XFASTINT (w->window_end_vpos) + 1,
14673 bottom_vpos, 0);
14674 #endif
14675
14676 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14677 debug_end_vpos = XFASTINT (w->window_end_vpos));
14678
14679 /* Record that display has not been completed. */
14680 w->window_end_valid = Qnil;
14681 w->desired_matrix->no_scrolling_p = 1;
14682 return 3;
14683
14684 #undef GIVE_UP
14685 }
14686
14687
14688 \f
14689 /***********************************************************************
14690 More debugging support
14691 ***********************************************************************/
14692
14693 #if GLYPH_DEBUG
14694
14695 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14696 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14697 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14698
14699
14700 /* Dump the contents of glyph matrix MATRIX on stderr.
14701
14702 GLYPHS 0 means don't show glyph contents.
14703 GLYPHS 1 means show glyphs in short form
14704 GLYPHS > 1 means show glyphs in long form. */
14705
14706 void
14707 dump_glyph_matrix (matrix, glyphs)
14708 struct glyph_matrix *matrix;
14709 int glyphs;
14710 {
14711 int i;
14712 for (i = 0; i < matrix->nrows; ++i)
14713 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14714 }
14715
14716
14717 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14718 the glyph row and area where the glyph comes from. */
14719
14720 void
14721 dump_glyph (row, glyph, area)
14722 struct glyph_row *row;
14723 struct glyph *glyph;
14724 int area;
14725 {
14726 if (glyph->type == CHAR_GLYPH)
14727 {
14728 fprintf (stderr,
14729 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14730 glyph - row->glyphs[TEXT_AREA],
14731 'C',
14732 glyph->charpos,
14733 (BUFFERP (glyph->object)
14734 ? 'B'
14735 : (STRINGP (glyph->object)
14736 ? 'S'
14737 : '-')),
14738 glyph->pixel_width,
14739 glyph->u.ch,
14740 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14741 ? glyph->u.ch
14742 : '.'),
14743 glyph->face_id,
14744 glyph->left_box_line_p,
14745 glyph->right_box_line_p);
14746 }
14747 else if (glyph->type == STRETCH_GLYPH)
14748 {
14749 fprintf (stderr,
14750 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14751 glyph - row->glyphs[TEXT_AREA],
14752 'S',
14753 glyph->charpos,
14754 (BUFFERP (glyph->object)
14755 ? 'B'
14756 : (STRINGP (glyph->object)
14757 ? 'S'
14758 : '-')),
14759 glyph->pixel_width,
14760 0,
14761 '.',
14762 glyph->face_id,
14763 glyph->left_box_line_p,
14764 glyph->right_box_line_p);
14765 }
14766 else if (glyph->type == IMAGE_GLYPH)
14767 {
14768 fprintf (stderr,
14769 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14770 glyph - row->glyphs[TEXT_AREA],
14771 'I',
14772 glyph->charpos,
14773 (BUFFERP (glyph->object)
14774 ? 'B'
14775 : (STRINGP (glyph->object)
14776 ? 'S'
14777 : '-')),
14778 glyph->pixel_width,
14779 glyph->u.img_id,
14780 '.',
14781 glyph->face_id,
14782 glyph->left_box_line_p,
14783 glyph->right_box_line_p);
14784 }
14785 }
14786
14787
14788 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14789 GLYPHS 0 means don't show glyph contents.
14790 GLYPHS 1 means show glyphs in short form
14791 GLYPHS > 1 means show glyphs in long form. */
14792
14793 void
14794 dump_glyph_row (row, vpos, glyphs)
14795 struct glyph_row *row;
14796 int vpos, glyphs;
14797 {
14798 if (glyphs != 1)
14799 {
14800 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14801 fprintf (stderr, "======================================================================\n");
14802
14803 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14804 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14805 vpos,
14806 MATRIX_ROW_START_CHARPOS (row),
14807 MATRIX_ROW_END_CHARPOS (row),
14808 row->used[TEXT_AREA],
14809 row->contains_overlapping_glyphs_p,
14810 row->enabled_p,
14811 row->truncated_on_left_p,
14812 row->truncated_on_right_p,
14813 row->continued_p,
14814 MATRIX_ROW_CONTINUATION_LINE_P (row),
14815 row->displays_text_p,
14816 row->ends_at_zv_p,
14817 row->fill_line_p,
14818 row->ends_in_middle_of_char_p,
14819 row->starts_in_middle_of_char_p,
14820 row->mouse_face_p,
14821 row->x,
14822 row->y,
14823 row->pixel_width,
14824 row->height,
14825 row->visible_height,
14826 row->ascent,
14827 row->phys_ascent);
14828 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14829 row->end.overlay_string_index,
14830 row->continuation_lines_width);
14831 fprintf (stderr, "%9d %5d\n",
14832 CHARPOS (row->start.string_pos),
14833 CHARPOS (row->end.string_pos));
14834 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14835 row->end.dpvec_index);
14836 }
14837
14838 if (glyphs > 1)
14839 {
14840 int area;
14841
14842 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14843 {
14844 struct glyph *glyph = row->glyphs[area];
14845 struct glyph *glyph_end = glyph + row->used[area];
14846
14847 /* Glyph for a line end in text. */
14848 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14849 ++glyph_end;
14850
14851 if (glyph < glyph_end)
14852 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14853
14854 for (; glyph < glyph_end; ++glyph)
14855 dump_glyph (row, glyph, area);
14856 }
14857 }
14858 else if (glyphs == 1)
14859 {
14860 int area;
14861
14862 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14863 {
14864 char *s = (char *) alloca (row->used[area] + 1);
14865 int i;
14866
14867 for (i = 0; i < row->used[area]; ++i)
14868 {
14869 struct glyph *glyph = row->glyphs[area] + i;
14870 if (glyph->type == CHAR_GLYPH
14871 && glyph->u.ch < 0x80
14872 && glyph->u.ch >= ' ')
14873 s[i] = glyph->u.ch;
14874 else
14875 s[i] = '.';
14876 }
14877
14878 s[i] = '\0';
14879 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14880 }
14881 }
14882 }
14883
14884
14885 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14886 Sdump_glyph_matrix, 0, 1, "p",
14887 doc: /* Dump the current matrix of the selected window to stderr.
14888 Shows contents of glyph row structures. With non-nil
14889 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14890 glyphs in short form, otherwise show glyphs in long form. */)
14891 (glyphs)
14892 Lisp_Object glyphs;
14893 {
14894 struct window *w = XWINDOW (selected_window);
14895 struct buffer *buffer = XBUFFER (w->buffer);
14896
14897 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14898 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14899 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14900 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14901 fprintf (stderr, "=============================================\n");
14902 dump_glyph_matrix (w->current_matrix,
14903 NILP (glyphs) ? 0 : XINT (glyphs));
14904 return Qnil;
14905 }
14906
14907
14908 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14909 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14910 ()
14911 {
14912 struct frame *f = XFRAME (selected_frame);
14913 dump_glyph_matrix (f->current_matrix, 1);
14914 return Qnil;
14915 }
14916
14917
14918 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14919 doc: /* Dump glyph row ROW to stderr.
14920 GLYPH 0 means don't dump glyphs.
14921 GLYPH 1 means dump glyphs in short form.
14922 GLYPH > 1 or omitted means dump glyphs in long form. */)
14923 (row, glyphs)
14924 Lisp_Object row, glyphs;
14925 {
14926 struct glyph_matrix *matrix;
14927 int vpos;
14928
14929 CHECK_NUMBER (row);
14930 matrix = XWINDOW (selected_window)->current_matrix;
14931 vpos = XINT (row);
14932 if (vpos >= 0 && vpos < matrix->nrows)
14933 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14934 vpos,
14935 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14936 return Qnil;
14937 }
14938
14939
14940 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14941 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14942 GLYPH 0 means don't dump glyphs.
14943 GLYPH 1 means dump glyphs in short form.
14944 GLYPH > 1 or omitted means dump glyphs in long form. */)
14945 (row, glyphs)
14946 Lisp_Object row, glyphs;
14947 {
14948 struct frame *sf = SELECTED_FRAME ();
14949 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14950 int vpos;
14951
14952 CHECK_NUMBER (row);
14953 vpos = XINT (row);
14954 if (vpos >= 0 && vpos < m->nrows)
14955 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14956 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14957 return Qnil;
14958 }
14959
14960
14961 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14962 doc: /* Toggle tracing of redisplay.
14963 With ARG, turn tracing on if and only if ARG is positive. */)
14964 (arg)
14965 Lisp_Object arg;
14966 {
14967 if (NILP (arg))
14968 trace_redisplay_p = !trace_redisplay_p;
14969 else
14970 {
14971 arg = Fprefix_numeric_value (arg);
14972 trace_redisplay_p = XINT (arg) > 0;
14973 }
14974
14975 return Qnil;
14976 }
14977
14978
14979 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14980 doc: /* Like `format', but print result to stderr.
14981 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14982 (nargs, args)
14983 int nargs;
14984 Lisp_Object *args;
14985 {
14986 Lisp_Object s = Fformat (nargs, args);
14987 fprintf (stderr, "%s", SDATA (s));
14988 return Qnil;
14989 }
14990
14991 #endif /* GLYPH_DEBUG */
14992
14993
14994 \f
14995 /***********************************************************************
14996 Building Desired Matrix Rows
14997 ***********************************************************************/
14998
14999 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15000 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15001
15002 static struct glyph_row *
15003 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15004 struct window *w;
15005 Lisp_Object overlay_arrow_string;
15006 {
15007 struct frame *f = XFRAME (WINDOW_FRAME (w));
15008 struct buffer *buffer = XBUFFER (w->buffer);
15009 struct buffer *old = current_buffer;
15010 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15011 int arrow_len = SCHARS (overlay_arrow_string);
15012 const unsigned char *arrow_end = arrow_string + arrow_len;
15013 const unsigned char *p;
15014 struct it it;
15015 int multibyte_p;
15016 int n_glyphs_before;
15017
15018 set_buffer_temp (buffer);
15019 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15020 it.glyph_row->used[TEXT_AREA] = 0;
15021 SET_TEXT_POS (it.position, 0, 0);
15022
15023 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15024 p = arrow_string;
15025 while (p < arrow_end)
15026 {
15027 Lisp_Object face, ilisp;
15028
15029 /* Get the next character. */
15030 if (multibyte_p)
15031 it.c = string_char_and_length (p, arrow_len, &it.len);
15032 else
15033 it.c = *p, it.len = 1;
15034 p += it.len;
15035
15036 /* Get its face. */
15037 ilisp = make_number (p - arrow_string);
15038 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15039 it.face_id = compute_char_face (f, it.c, face);
15040
15041 /* Compute its width, get its glyphs. */
15042 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15043 SET_TEXT_POS (it.position, -1, -1);
15044 PRODUCE_GLYPHS (&it);
15045
15046 /* If this character doesn't fit any more in the line, we have
15047 to remove some glyphs. */
15048 if (it.current_x > it.last_visible_x)
15049 {
15050 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15051 break;
15052 }
15053 }
15054
15055 set_buffer_temp (old);
15056 return it.glyph_row;
15057 }
15058
15059
15060 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15061 glyphs are only inserted for terminal frames since we can't really
15062 win with truncation glyphs when partially visible glyphs are
15063 involved. Which glyphs to insert is determined by
15064 produce_special_glyphs. */
15065
15066 static void
15067 insert_left_trunc_glyphs (it)
15068 struct it *it;
15069 {
15070 struct it truncate_it;
15071 struct glyph *from, *end, *to, *toend;
15072
15073 xassert (!FRAME_WINDOW_P (it->f));
15074
15075 /* Get the truncation glyphs. */
15076 truncate_it = *it;
15077 truncate_it.current_x = 0;
15078 truncate_it.face_id = DEFAULT_FACE_ID;
15079 truncate_it.glyph_row = &scratch_glyph_row;
15080 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15081 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15082 truncate_it.object = make_number (0);
15083 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15084
15085 /* Overwrite glyphs from IT with truncation glyphs. */
15086 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15087 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15088 to = it->glyph_row->glyphs[TEXT_AREA];
15089 toend = to + it->glyph_row->used[TEXT_AREA];
15090
15091 while (from < end)
15092 *to++ = *from++;
15093
15094 /* There may be padding glyphs left over. Overwrite them too. */
15095 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15096 {
15097 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15098 while (from < end)
15099 *to++ = *from++;
15100 }
15101
15102 if (to > toend)
15103 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15104 }
15105
15106
15107 /* Compute the pixel height and width of IT->glyph_row.
15108
15109 Most of the time, ascent and height of a display line will be equal
15110 to the max_ascent and max_height values of the display iterator
15111 structure. This is not the case if
15112
15113 1. We hit ZV without displaying anything. In this case, max_ascent
15114 and max_height will be zero.
15115
15116 2. We have some glyphs that don't contribute to the line height.
15117 (The glyph row flag contributes_to_line_height_p is for future
15118 pixmap extensions).
15119
15120 The first case is easily covered by using default values because in
15121 these cases, the line height does not really matter, except that it
15122 must not be zero. */
15123
15124 static void
15125 compute_line_metrics (it)
15126 struct it *it;
15127 {
15128 struct glyph_row *row = it->glyph_row;
15129 int area, i;
15130
15131 if (FRAME_WINDOW_P (it->f))
15132 {
15133 int i, min_y, max_y;
15134
15135 /* The line may consist of one space only, that was added to
15136 place the cursor on it. If so, the row's height hasn't been
15137 computed yet. */
15138 if (row->height == 0)
15139 {
15140 if (it->max_ascent + it->max_descent == 0)
15141 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15142 row->ascent = it->max_ascent;
15143 row->height = it->max_ascent + it->max_descent;
15144 row->phys_ascent = it->max_phys_ascent;
15145 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15146 row->extra_line_spacing = it->max_extra_line_spacing;
15147 }
15148
15149 /* Compute the width of this line. */
15150 row->pixel_width = row->x;
15151 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15152 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15153
15154 xassert (row->pixel_width >= 0);
15155 xassert (row->ascent >= 0 && row->height > 0);
15156
15157 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15158 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15159
15160 /* If first line's physical ascent is larger than its logical
15161 ascent, use the physical ascent, and make the row taller.
15162 This makes accented characters fully visible. */
15163 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15164 && row->phys_ascent > row->ascent)
15165 {
15166 row->height += row->phys_ascent - row->ascent;
15167 row->ascent = row->phys_ascent;
15168 }
15169
15170 /* Compute how much of the line is visible. */
15171 row->visible_height = row->height;
15172
15173 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15174 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15175
15176 if (row->y < min_y)
15177 row->visible_height -= min_y - row->y;
15178 if (row->y + row->height > max_y)
15179 row->visible_height -= row->y + row->height - max_y;
15180 }
15181 else
15182 {
15183 row->pixel_width = row->used[TEXT_AREA];
15184 if (row->continued_p)
15185 row->pixel_width -= it->continuation_pixel_width;
15186 else if (row->truncated_on_right_p)
15187 row->pixel_width -= it->truncation_pixel_width;
15188 row->ascent = row->phys_ascent = 0;
15189 row->height = row->phys_height = row->visible_height = 1;
15190 row->extra_line_spacing = 0;
15191 }
15192
15193 /* Compute a hash code for this row. */
15194 row->hash = 0;
15195 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15196 for (i = 0; i < row->used[area]; ++i)
15197 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15198 + row->glyphs[area][i].u.val
15199 + row->glyphs[area][i].face_id
15200 + row->glyphs[area][i].padding_p
15201 + (row->glyphs[area][i].type << 2));
15202
15203 it->max_ascent = it->max_descent = 0;
15204 it->max_phys_ascent = it->max_phys_descent = 0;
15205 }
15206
15207
15208 /* Append one space to the glyph row of iterator IT if doing a
15209 window-based redisplay. The space has the same face as
15210 IT->face_id. Value is non-zero if a space was added.
15211
15212 This function is called to make sure that there is always one glyph
15213 at the end of a glyph row that the cursor can be set on under
15214 window-systems. (If there weren't such a glyph we would not know
15215 how wide and tall a box cursor should be displayed).
15216
15217 At the same time this space let's a nicely handle clearing to the
15218 end of the line if the row ends in italic text. */
15219
15220 static int
15221 append_space_for_newline (it, default_face_p)
15222 struct it *it;
15223 int default_face_p;
15224 {
15225 if (FRAME_WINDOW_P (it->f))
15226 {
15227 int n = it->glyph_row->used[TEXT_AREA];
15228
15229 if (it->glyph_row->glyphs[TEXT_AREA] + n
15230 < it->glyph_row->glyphs[1 + TEXT_AREA])
15231 {
15232 /* Save some values that must not be changed.
15233 Must save IT->c and IT->len because otherwise
15234 ITERATOR_AT_END_P wouldn't work anymore after
15235 append_space_for_newline has been called. */
15236 enum display_element_type saved_what = it->what;
15237 int saved_c = it->c, saved_len = it->len;
15238 int saved_x = it->current_x;
15239 int saved_face_id = it->face_id;
15240 struct text_pos saved_pos;
15241 Lisp_Object saved_object;
15242 struct face *face;
15243
15244 saved_object = it->object;
15245 saved_pos = it->position;
15246
15247 it->what = IT_CHARACTER;
15248 bzero (&it->position, sizeof it->position);
15249 it->object = make_number (0);
15250 it->c = ' ';
15251 it->len = 1;
15252
15253 if (default_face_p)
15254 it->face_id = DEFAULT_FACE_ID;
15255 else if (it->face_before_selective_p)
15256 it->face_id = it->saved_face_id;
15257 face = FACE_FROM_ID (it->f, it->face_id);
15258 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15259
15260 PRODUCE_GLYPHS (it);
15261
15262 it->override_ascent = -1;
15263 it->constrain_row_ascent_descent_p = 0;
15264 it->current_x = saved_x;
15265 it->object = saved_object;
15266 it->position = saved_pos;
15267 it->what = saved_what;
15268 it->face_id = saved_face_id;
15269 it->len = saved_len;
15270 it->c = saved_c;
15271 return 1;
15272 }
15273 }
15274
15275 return 0;
15276 }
15277
15278
15279 /* Extend the face of the last glyph in the text area of IT->glyph_row
15280 to the end of the display line. Called from display_line.
15281 If the glyph row is empty, add a space glyph to it so that we
15282 know the face to draw. Set the glyph row flag fill_line_p. */
15283
15284 static void
15285 extend_face_to_end_of_line (it)
15286 struct it *it;
15287 {
15288 struct face *face;
15289 struct frame *f = it->f;
15290
15291 /* If line is already filled, do nothing. */
15292 if (it->current_x >= it->last_visible_x)
15293 return;
15294
15295 /* Face extension extends the background and box of IT->face_id
15296 to the end of the line. If the background equals the background
15297 of the frame, we don't have to do anything. */
15298 if (it->face_before_selective_p)
15299 face = FACE_FROM_ID (it->f, it->saved_face_id);
15300 else
15301 face = FACE_FROM_ID (f, it->face_id);
15302
15303 if (FRAME_WINDOW_P (f)
15304 && face->box == FACE_NO_BOX
15305 && face->background == FRAME_BACKGROUND_PIXEL (f)
15306 && !face->stipple)
15307 return;
15308
15309 /* Set the glyph row flag indicating that the face of the last glyph
15310 in the text area has to be drawn to the end of the text area. */
15311 it->glyph_row->fill_line_p = 1;
15312
15313 /* If current character of IT is not ASCII, make sure we have the
15314 ASCII face. This will be automatically undone the next time
15315 get_next_display_element returns a multibyte character. Note
15316 that the character will always be single byte in unibyte text. */
15317 if (!SINGLE_BYTE_CHAR_P (it->c))
15318 {
15319 it->face_id = FACE_FOR_CHAR (f, face, 0);
15320 }
15321
15322 if (FRAME_WINDOW_P (f))
15323 {
15324 /* If the row is empty, add a space with the current face of IT,
15325 so that we know which face to draw. */
15326 if (it->glyph_row->used[TEXT_AREA] == 0)
15327 {
15328 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15329 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15330 it->glyph_row->used[TEXT_AREA] = 1;
15331 }
15332 }
15333 else
15334 {
15335 /* Save some values that must not be changed. */
15336 int saved_x = it->current_x;
15337 struct text_pos saved_pos;
15338 Lisp_Object saved_object;
15339 enum display_element_type saved_what = it->what;
15340 int saved_face_id = it->face_id;
15341
15342 saved_object = it->object;
15343 saved_pos = it->position;
15344
15345 it->what = IT_CHARACTER;
15346 bzero (&it->position, sizeof it->position);
15347 it->object = make_number (0);
15348 it->c = ' ';
15349 it->len = 1;
15350 it->face_id = face->id;
15351
15352 PRODUCE_GLYPHS (it);
15353
15354 while (it->current_x <= it->last_visible_x)
15355 PRODUCE_GLYPHS (it);
15356
15357 /* Don't count these blanks really. It would let us insert a left
15358 truncation glyph below and make us set the cursor on them, maybe. */
15359 it->current_x = saved_x;
15360 it->object = saved_object;
15361 it->position = saved_pos;
15362 it->what = saved_what;
15363 it->face_id = saved_face_id;
15364 }
15365 }
15366
15367
15368 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15369 trailing whitespace. */
15370
15371 static int
15372 trailing_whitespace_p (charpos)
15373 int charpos;
15374 {
15375 int bytepos = CHAR_TO_BYTE (charpos);
15376 int c = 0;
15377
15378 while (bytepos < ZV_BYTE
15379 && (c = FETCH_CHAR (bytepos),
15380 c == ' ' || c == '\t'))
15381 ++bytepos;
15382
15383 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15384 {
15385 if (bytepos != PT_BYTE)
15386 return 1;
15387 }
15388 return 0;
15389 }
15390
15391
15392 /* Highlight trailing whitespace, if any, in ROW. */
15393
15394 void
15395 highlight_trailing_whitespace (f, row)
15396 struct frame *f;
15397 struct glyph_row *row;
15398 {
15399 int used = row->used[TEXT_AREA];
15400
15401 if (used)
15402 {
15403 struct glyph *start = row->glyphs[TEXT_AREA];
15404 struct glyph *glyph = start + used - 1;
15405
15406 /* Skip over glyphs inserted to display the cursor at the
15407 end of a line, for extending the face of the last glyph
15408 to the end of the line on terminals, and for truncation
15409 and continuation glyphs. */
15410 while (glyph >= start
15411 && glyph->type == CHAR_GLYPH
15412 && INTEGERP (glyph->object))
15413 --glyph;
15414
15415 /* If last glyph is a space or stretch, and it's trailing
15416 whitespace, set the face of all trailing whitespace glyphs in
15417 IT->glyph_row to `trailing-whitespace'. */
15418 if (glyph >= start
15419 && BUFFERP (glyph->object)
15420 && (glyph->type == STRETCH_GLYPH
15421 || (glyph->type == CHAR_GLYPH
15422 && glyph->u.ch == ' '))
15423 && trailing_whitespace_p (glyph->charpos))
15424 {
15425 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15426 if (face_id < 0)
15427 return;
15428
15429 while (glyph >= start
15430 && BUFFERP (glyph->object)
15431 && (glyph->type == STRETCH_GLYPH
15432 || (glyph->type == CHAR_GLYPH
15433 && glyph->u.ch == ' ')))
15434 (glyph--)->face_id = face_id;
15435 }
15436 }
15437 }
15438
15439
15440 /* Value is non-zero if glyph row ROW in window W should be
15441 used to hold the cursor. */
15442
15443 static int
15444 cursor_row_p (w, row)
15445 struct window *w;
15446 struct glyph_row *row;
15447 {
15448 int cursor_row_p = 1;
15449
15450 if (PT == MATRIX_ROW_END_CHARPOS (row))
15451 {
15452 /* If the row ends with a newline from a string, we don't want
15453 the cursor there, but we still want it at the start of the
15454 string if the string starts in this row.
15455 If the row is continued it doesn't end in a newline. */
15456 if (CHARPOS (row->end.string_pos) >= 0)
15457 cursor_row_p = (row->continued_p
15458 || PT >= MATRIX_ROW_START_CHARPOS (row));
15459 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15460 {
15461 /* If the row ends in middle of a real character,
15462 and the line is continued, we want the cursor here.
15463 That's because MATRIX_ROW_END_CHARPOS would equal
15464 PT if PT is before the character. */
15465 if (!row->ends_in_ellipsis_p)
15466 cursor_row_p = row->continued_p;
15467 else
15468 /* If the row ends in an ellipsis, then
15469 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15470 We want that position to be displayed after the ellipsis. */
15471 cursor_row_p = 0;
15472 }
15473 /* If the row ends at ZV, display the cursor at the end of that
15474 row instead of at the start of the row below. */
15475 else if (row->ends_at_zv_p)
15476 cursor_row_p = 1;
15477 else
15478 cursor_row_p = 0;
15479 }
15480
15481 return cursor_row_p;
15482 }
15483
15484
15485 /* Construct the glyph row IT->glyph_row in the desired matrix of
15486 IT->w from text at the current position of IT. See dispextern.h
15487 for an overview of struct it. Value is non-zero if
15488 IT->glyph_row displays text, as opposed to a line displaying ZV
15489 only. */
15490
15491 static int
15492 display_line (it)
15493 struct it *it;
15494 {
15495 struct glyph_row *row = it->glyph_row;
15496 Lisp_Object overlay_arrow_string;
15497
15498 /* We always start displaying at hpos zero even if hscrolled. */
15499 xassert (it->hpos == 0 && it->current_x == 0);
15500
15501 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15502 >= it->w->desired_matrix->nrows)
15503 {
15504 it->w->nrows_scale_factor++;
15505 fonts_changed_p = 1;
15506 return 0;
15507 }
15508
15509 /* Is IT->w showing the region? */
15510 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15511
15512 /* Clear the result glyph row and enable it. */
15513 prepare_desired_row (row);
15514
15515 row->y = it->current_y;
15516 row->start = it->start;
15517 row->continuation_lines_width = it->continuation_lines_width;
15518 row->displays_text_p = 1;
15519 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15520 it->starts_in_middle_of_char_p = 0;
15521
15522 /* Arrange the overlays nicely for our purposes. Usually, we call
15523 display_line on only one line at a time, in which case this
15524 can't really hurt too much, or we call it on lines which appear
15525 one after another in the buffer, in which case all calls to
15526 recenter_overlay_lists but the first will be pretty cheap. */
15527 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15528
15529 /* Move over display elements that are not visible because we are
15530 hscrolled. This may stop at an x-position < IT->first_visible_x
15531 if the first glyph is partially visible or if we hit a line end. */
15532 if (it->current_x < it->first_visible_x)
15533 {
15534 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15535 MOVE_TO_POS | MOVE_TO_X);
15536 }
15537
15538 /* Get the initial row height. This is either the height of the
15539 text hscrolled, if there is any, or zero. */
15540 row->ascent = it->max_ascent;
15541 row->height = it->max_ascent + it->max_descent;
15542 row->phys_ascent = it->max_phys_ascent;
15543 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15544 row->extra_line_spacing = it->max_extra_line_spacing;
15545
15546 /* Loop generating characters. The loop is left with IT on the next
15547 character to display. */
15548 while (1)
15549 {
15550 int n_glyphs_before, hpos_before, x_before;
15551 int x, i, nglyphs;
15552 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15553
15554 /* Retrieve the next thing to display. Value is zero if end of
15555 buffer reached. */
15556 if (!get_next_display_element (it))
15557 {
15558 /* Maybe add a space at the end of this line that is used to
15559 display the cursor there under X. Set the charpos of the
15560 first glyph of blank lines not corresponding to any text
15561 to -1. */
15562 #ifdef HAVE_WINDOW_SYSTEM
15563 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15564 row->exact_window_width_line_p = 1;
15565 else
15566 #endif /* HAVE_WINDOW_SYSTEM */
15567 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15568 || row->used[TEXT_AREA] == 0)
15569 {
15570 row->glyphs[TEXT_AREA]->charpos = -1;
15571 row->displays_text_p = 0;
15572
15573 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15574 && (!MINI_WINDOW_P (it->w)
15575 || (minibuf_level && EQ (it->window, minibuf_window))))
15576 row->indicate_empty_line_p = 1;
15577 }
15578
15579 it->continuation_lines_width = 0;
15580 row->ends_at_zv_p = 1;
15581 break;
15582 }
15583
15584 /* Now, get the metrics of what we want to display. This also
15585 generates glyphs in `row' (which is IT->glyph_row). */
15586 n_glyphs_before = row->used[TEXT_AREA];
15587 x = it->current_x;
15588
15589 /* Remember the line height so far in case the next element doesn't
15590 fit on the line. */
15591 if (!it->truncate_lines_p)
15592 {
15593 ascent = it->max_ascent;
15594 descent = it->max_descent;
15595 phys_ascent = it->max_phys_ascent;
15596 phys_descent = it->max_phys_descent;
15597 }
15598
15599 PRODUCE_GLYPHS (it);
15600
15601 /* If this display element was in marginal areas, continue with
15602 the next one. */
15603 if (it->area != TEXT_AREA)
15604 {
15605 row->ascent = max (row->ascent, it->max_ascent);
15606 row->height = max (row->height, it->max_ascent + it->max_descent);
15607 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15608 row->phys_height = max (row->phys_height,
15609 it->max_phys_ascent + it->max_phys_descent);
15610 row->extra_line_spacing = max (row->extra_line_spacing,
15611 it->max_extra_line_spacing);
15612 set_iterator_to_next (it, 1);
15613 continue;
15614 }
15615
15616 /* Does the display element fit on the line? If we truncate
15617 lines, we should draw past the right edge of the window. If
15618 we don't truncate, we want to stop so that we can display the
15619 continuation glyph before the right margin. If lines are
15620 continued, there are two possible strategies for characters
15621 resulting in more than 1 glyph (e.g. tabs): Display as many
15622 glyphs as possible in this line and leave the rest for the
15623 continuation line, or display the whole element in the next
15624 line. Original redisplay did the former, so we do it also. */
15625 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15626 hpos_before = it->hpos;
15627 x_before = x;
15628
15629 if (/* Not a newline. */
15630 nglyphs > 0
15631 /* Glyphs produced fit entirely in the line. */
15632 && it->current_x < it->last_visible_x)
15633 {
15634 it->hpos += nglyphs;
15635 row->ascent = max (row->ascent, it->max_ascent);
15636 row->height = max (row->height, it->max_ascent + it->max_descent);
15637 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15638 row->phys_height = max (row->phys_height,
15639 it->max_phys_ascent + it->max_phys_descent);
15640 row->extra_line_spacing = max (row->extra_line_spacing,
15641 it->max_extra_line_spacing);
15642 if (it->current_x - it->pixel_width < it->first_visible_x)
15643 row->x = x - it->first_visible_x;
15644 }
15645 else
15646 {
15647 int new_x;
15648 struct glyph *glyph;
15649
15650 for (i = 0; i < nglyphs; ++i, x = new_x)
15651 {
15652 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15653 new_x = x + glyph->pixel_width;
15654
15655 if (/* Lines are continued. */
15656 !it->truncate_lines_p
15657 && (/* Glyph doesn't fit on the line. */
15658 new_x > it->last_visible_x
15659 /* Or it fits exactly on a window system frame. */
15660 || (new_x == it->last_visible_x
15661 && FRAME_WINDOW_P (it->f))))
15662 {
15663 /* End of a continued line. */
15664
15665 if (it->hpos == 0
15666 || (new_x == it->last_visible_x
15667 && FRAME_WINDOW_P (it->f)))
15668 {
15669 /* Current glyph is the only one on the line or
15670 fits exactly on the line. We must continue
15671 the line because we can't draw the cursor
15672 after the glyph. */
15673 row->continued_p = 1;
15674 it->current_x = new_x;
15675 it->continuation_lines_width += new_x;
15676 ++it->hpos;
15677 if (i == nglyphs - 1)
15678 {
15679 set_iterator_to_next (it, 1);
15680 #ifdef HAVE_WINDOW_SYSTEM
15681 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15682 {
15683 if (!get_next_display_element (it))
15684 {
15685 row->exact_window_width_line_p = 1;
15686 it->continuation_lines_width = 0;
15687 row->continued_p = 0;
15688 row->ends_at_zv_p = 1;
15689 }
15690 else if (ITERATOR_AT_END_OF_LINE_P (it))
15691 {
15692 row->continued_p = 0;
15693 row->exact_window_width_line_p = 1;
15694 }
15695 }
15696 #endif /* HAVE_WINDOW_SYSTEM */
15697 }
15698 }
15699 else if (CHAR_GLYPH_PADDING_P (*glyph)
15700 && !FRAME_WINDOW_P (it->f))
15701 {
15702 /* A padding glyph that doesn't fit on this line.
15703 This means the whole character doesn't fit
15704 on the line. */
15705 row->used[TEXT_AREA] = n_glyphs_before;
15706
15707 /* Fill the rest of the row with continuation
15708 glyphs like in 20.x. */
15709 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15710 < row->glyphs[1 + TEXT_AREA])
15711 produce_special_glyphs (it, IT_CONTINUATION);
15712
15713 row->continued_p = 1;
15714 it->current_x = x_before;
15715 it->continuation_lines_width += x_before;
15716
15717 /* Restore the height to what it was before the
15718 element not fitting on the line. */
15719 it->max_ascent = ascent;
15720 it->max_descent = descent;
15721 it->max_phys_ascent = phys_ascent;
15722 it->max_phys_descent = phys_descent;
15723 }
15724 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15725 {
15726 /* A TAB that extends past the right edge of the
15727 window. This produces a single glyph on
15728 window system frames. We leave the glyph in
15729 this row and let it fill the row, but don't
15730 consume the TAB. */
15731 it->continuation_lines_width += it->last_visible_x;
15732 row->ends_in_middle_of_char_p = 1;
15733 row->continued_p = 1;
15734 glyph->pixel_width = it->last_visible_x - x;
15735 it->starts_in_middle_of_char_p = 1;
15736 }
15737 else
15738 {
15739 /* Something other than a TAB that draws past
15740 the right edge of the window. Restore
15741 positions to values before the element. */
15742 row->used[TEXT_AREA] = n_glyphs_before + i;
15743
15744 /* Display continuation glyphs. */
15745 if (!FRAME_WINDOW_P (it->f))
15746 produce_special_glyphs (it, IT_CONTINUATION);
15747 row->continued_p = 1;
15748
15749 it->current_x = x_before;
15750 it->continuation_lines_width += x;
15751 extend_face_to_end_of_line (it);
15752
15753 if (nglyphs > 1 && i > 0)
15754 {
15755 row->ends_in_middle_of_char_p = 1;
15756 it->starts_in_middle_of_char_p = 1;
15757 }
15758
15759 /* Restore the height to what it was before the
15760 element not fitting on the line. */
15761 it->max_ascent = ascent;
15762 it->max_descent = descent;
15763 it->max_phys_ascent = phys_ascent;
15764 it->max_phys_descent = phys_descent;
15765 }
15766
15767 break;
15768 }
15769 else if (new_x > it->first_visible_x)
15770 {
15771 /* Increment number of glyphs actually displayed. */
15772 ++it->hpos;
15773
15774 if (x < it->first_visible_x)
15775 /* Glyph is partially visible, i.e. row starts at
15776 negative X position. */
15777 row->x = x - it->first_visible_x;
15778 }
15779 else
15780 {
15781 /* Glyph is completely off the left margin of the
15782 window. This should not happen because of the
15783 move_it_in_display_line at the start of this
15784 function, unless the text display area of the
15785 window is empty. */
15786 xassert (it->first_visible_x <= it->last_visible_x);
15787 }
15788 }
15789
15790 row->ascent = max (row->ascent, it->max_ascent);
15791 row->height = max (row->height, it->max_ascent + it->max_descent);
15792 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15793 row->phys_height = max (row->phys_height,
15794 it->max_phys_ascent + it->max_phys_descent);
15795 row->extra_line_spacing = max (row->extra_line_spacing,
15796 it->max_extra_line_spacing);
15797
15798 /* End of this display line if row is continued. */
15799 if (row->continued_p || row->ends_at_zv_p)
15800 break;
15801 }
15802
15803 at_end_of_line:
15804 /* Is this a line end? If yes, we're also done, after making
15805 sure that a non-default face is extended up to the right
15806 margin of the window. */
15807 if (ITERATOR_AT_END_OF_LINE_P (it))
15808 {
15809 int used_before = row->used[TEXT_AREA];
15810
15811 row->ends_in_newline_from_string_p = STRINGP (it->object);
15812
15813 #ifdef HAVE_WINDOW_SYSTEM
15814 /* Add a space at the end of the line that is used to
15815 display the cursor there. */
15816 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15817 append_space_for_newline (it, 0);
15818 #endif /* HAVE_WINDOW_SYSTEM */
15819
15820 /* Extend the face to the end of the line. */
15821 extend_face_to_end_of_line (it);
15822
15823 /* Make sure we have the position. */
15824 if (used_before == 0)
15825 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15826
15827 /* Consume the line end. This skips over invisible lines. */
15828 set_iterator_to_next (it, 1);
15829 it->continuation_lines_width = 0;
15830 break;
15831 }
15832
15833 /* Proceed with next display element. Note that this skips
15834 over lines invisible because of selective display. */
15835 set_iterator_to_next (it, 1);
15836
15837 /* If we truncate lines, we are done when the last displayed
15838 glyphs reach past the right margin of the window. */
15839 if (it->truncate_lines_p
15840 && (FRAME_WINDOW_P (it->f)
15841 ? (it->current_x >= it->last_visible_x)
15842 : (it->current_x > it->last_visible_x)))
15843 {
15844 /* Maybe add truncation glyphs. */
15845 if (!FRAME_WINDOW_P (it->f))
15846 {
15847 int i, n;
15848
15849 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15850 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15851 break;
15852
15853 for (n = row->used[TEXT_AREA]; i < n; ++i)
15854 {
15855 row->used[TEXT_AREA] = i;
15856 produce_special_glyphs (it, IT_TRUNCATION);
15857 }
15858 }
15859 #ifdef HAVE_WINDOW_SYSTEM
15860 else
15861 {
15862 /* Don't truncate if we can overflow newline into fringe. */
15863 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15864 {
15865 if (!get_next_display_element (it))
15866 {
15867 it->continuation_lines_width = 0;
15868 row->ends_at_zv_p = 1;
15869 row->exact_window_width_line_p = 1;
15870 break;
15871 }
15872 if (ITERATOR_AT_END_OF_LINE_P (it))
15873 {
15874 row->exact_window_width_line_p = 1;
15875 goto at_end_of_line;
15876 }
15877 }
15878 }
15879 #endif /* HAVE_WINDOW_SYSTEM */
15880
15881 row->truncated_on_right_p = 1;
15882 it->continuation_lines_width = 0;
15883 reseat_at_next_visible_line_start (it, 0);
15884 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15885 it->hpos = hpos_before;
15886 it->current_x = x_before;
15887 break;
15888 }
15889 }
15890
15891 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15892 at the left window margin. */
15893 if (it->first_visible_x
15894 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15895 {
15896 if (!FRAME_WINDOW_P (it->f))
15897 insert_left_trunc_glyphs (it);
15898 row->truncated_on_left_p = 1;
15899 }
15900
15901 /* If the start of this line is the overlay arrow-position, then
15902 mark this glyph row as the one containing the overlay arrow.
15903 This is clearly a mess with variable size fonts. It would be
15904 better to let it be displayed like cursors under X. */
15905 if ((row->displays_text_p || !overlay_arrow_seen)
15906 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15907 !NILP (overlay_arrow_string)))
15908 {
15909 /* Overlay arrow in window redisplay is a fringe bitmap. */
15910 if (STRINGP (overlay_arrow_string))
15911 {
15912 struct glyph_row *arrow_row
15913 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15914 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15915 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15916 struct glyph *p = row->glyphs[TEXT_AREA];
15917 struct glyph *p2, *end;
15918
15919 /* Copy the arrow glyphs. */
15920 while (glyph < arrow_end)
15921 *p++ = *glyph++;
15922
15923 /* Throw away padding glyphs. */
15924 p2 = p;
15925 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15926 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15927 ++p2;
15928 if (p2 > p)
15929 {
15930 while (p2 < end)
15931 *p++ = *p2++;
15932 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15933 }
15934 }
15935 else
15936 {
15937 xassert (INTEGERP (overlay_arrow_string));
15938 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15939 }
15940 overlay_arrow_seen = 1;
15941 }
15942
15943 /* Compute pixel dimensions of this line. */
15944 compute_line_metrics (it);
15945
15946 /* Remember the position at which this line ends. */
15947 row->end = it->current;
15948
15949 /* Record whether this row ends inside an ellipsis. */
15950 row->ends_in_ellipsis_p
15951 = (it->method == GET_FROM_DISPLAY_VECTOR
15952 && it->ellipsis_p);
15953
15954 /* Save fringe bitmaps in this row. */
15955 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15956 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15957 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15958 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15959
15960 it->left_user_fringe_bitmap = 0;
15961 it->left_user_fringe_face_id = 0;
15962 it->right_user_fringe_bitmap = 0;
15963 it->right_user_fringe_face_id = 0;
15964
15965 /* Maybe set the cursor. */
15966 if (it->w->cursor.vpos < 0
15967 && PT >= MATRIX_ROW_START_CHARPOS (row)
15968 && PT <= MATRIX_ROW_END_CHARPOS (row)
15969 && cursor_row_p (it->w, row))
15970 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15971
15972 /* Highlight trailing whitespace. */
15973 if (!NILP (Vshow_trailing_whitespace))
15974 highlight_trailing_whitespace (it->f, it->glyph_row);
15975
15976 /* Prepare for the next line. This line starts horizontally at (X
15977 HPOS) = (0 0). Vertical positions are incremented. As a
15978 convenience for the caller, IT->glyph_row is set to the next
15979 row to be used. */
15980 it->current_x = it->hpos = 0;
15981 it->current_y += row->height;
15982 ++it->vpos;
15983 ++it->glyph_row;
15984 it->start = it->current;
15985 return row->displays_text_p;
15986 }
15987
15988
15989 \f
15990 /***********************************************************************
15991 Menu Bar
15992 ***********************************************************************/
15993
15994 /* Redisplay the menu bar in the frame for window W.
15995
15996 The menu bar of X frames that don't have X toolkit support is
15997 displayed in a special window W->frame->menu_bar_window.
15998
15999 The menu bar of terminal frames is treated specially as far as
16000 glyph matrices are concerned. Menu bar lines are not part of
16001 windows, so the update is done directly on the frame matrix rows
16002 for the menu bar. */
16003
16004 static void
16005 display_menu_bar (w)
16006 struct window *w;
16007 {
16008 struct frame *f = XFRAME (WINDOW_FRAME (w));
16009 struct it it;
16010 Lisp_Object items;
16011 int i;
16012
16013 /* Don't do all this for graphical frames. */
16014 #ifdef HAVE_NTGUI
16015 if (!NILP (Vwindow_system))
16016 return;
16017 #endif
16018 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16019 if (FRAME_X_P (f))
16020 return;
16021 #endif
16022 #ifdef MAC_OS
16023 if (FRAME_MAC_P (f))
16024 return;
16025 #endif
16026
16027 #ifdef USE_X_TOOLKIT
16028 xassert (!FRAME_WINDOW_P (f));
16029 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16030 it.first_visible_x = 0;
16031 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16032 #else /* not USE_X_TOOLKIT */
16033 if (FRAME_WINDOW_P (f))
16034 {
16035 /* Menu bar lines are displayed in the desired matrix of the
16036 dummy window menu_bar_window. */
16037 struct window *menu_w;
16038 xassert (WINDOWP (f->menu_bar_window));
16039 menu_w = XWINDOW (f->menu_bar_window);
16040 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16041 MENU_FACE_ID);
16042 it.first_visible_x = 0;
16043 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16044 }
16045 else
16046 {
16047 /* This is a TTY frame, i.e. character hpos/vpos are used as
16048 pixel x/y. */
16049 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16050 MENU_FACE_ID);
16051 it.first_visible_x = 0;
16052 it.last_visible_x = FRAME_COLS (f);
16053 }
16054 #endif /* not USE_X_TOOLKIT */
16055
16056 if (! mode_line_inverse_video)
16057 /* Force the menu-bar to be displayed in the default face. */
16058 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16059
16060 /* Clear all rows of the menu bar. */
16061 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16062 {
16063 struct glyph_row *row = it.glyph_row + i;
16064 clear_glyph_row (row);
16065 row->enabled_p = 1;
16066 row->full_width_p = 1;
16067 }
16068
16069 /* Display all items of the menu bar. */
16070 items = FRAME_MENU_BAR_ITEMS (it.f);
16071 for (i = 0; i < XVECTOR (items)->size; i += 4)
16072 {
16073 Lisp_Object string;
16074
16075 /* Stop at nil string. */
16076 string = AREF (items, i + 1);
16077 if (NILP (string))
16078 break;
16079
16080 /* Remember where item was displayed. */
16081 AREF (items, i + 3) = make_number (it.hpos);
16082
16083 /* Display the item, pad with one space. */
16084 if (it.current_x < it.last_visible_x)
16085 display_string (NULL, string, Qnil, 0, 0, &it,
16086 SCHARS (string) + 1, 0, 0, -1);
16087 }
16088
16089 /* Fill out the line with spaces. */
16090 if (it.current_x < it.last_visible_x)
16091 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16092
16093 /* Compute the total height of the lines. */
16094 compute_line_metrics (&it);
16095 }
16096
16097
16098 \f
16099 /***********************************************************************
16100 Mode Line
16101 ***********************************************************************/
16102
16103 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16104 FORCE is non-zero, redisplay mode lines unconditionally.
16105 Otherwise, redisplay only mode lines that are garbaged. Value is
16106 the number of windows whose mode lines were redisplayed. */
16107
16108 static int
16109 redisplay_mode_lines (window, force)
16110 Lisp_Object window;
16111 int force;
16112 {
16113 int nwindows = 0;
16114
16115 while (!NILP (window))
16116 {
16117 struct window *w = XWINDOW (window);
16118
16119 if (WINDOWP (w->hchild))
16120 nwindows += redisplay_mode_lines (w->hchild, force);
16121 else if (WINDOWP (w->vchild))
16122 nwindows += redisplay_mode_lines (w->vchild, force);
16123 else if (force
16124 || FRAME_GARBAGED_P (XFRAME (w->frame))
16125 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16126 {
16127 struct text_pos lpoint;
16128 struct buffer *old = current_buffer;
16129
16130 /* Set the window's buffer for the mode line display. */
16131 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16132 set_buffer_internal_1 (XBUFFER (w->buffer));
16133
16134 /* Point refers normally to the selected window. For any
16135 other window, set up appropriate value. */
16136 if (!EQ (window, selected_window))
16137 {
16138 struct text_pos pt;
16139
16140 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16141 if (CHARPOS (pt) < BEGV)
16142 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16143 else if (CHARPOS (pt) > (ZV - 1))
16144 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16145 else
16146 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16147 }
16148
16149 /* Display mode lines. */
16150 clear_glyph_matrix (w->desired_matrix);
16151 if (display_mode_lines (w))
16152 {
16153 ++nwindows;
16154 w->must_be_updated_p = 1;
16155 }
16156
16157 /* Restore old settings. */
16158 set_buffer_internal_1 (old);
16159 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16160 }
16161
16162 window = w->next;
16163 }
16164
16165 return nwindows;
16166 }
16167
16168
16169 /* Display the mode and/or top line of window W. Value is the number
16170 of mode lines displayed. */
16171
16172 static int
16173 display_mode_lines (w)
16174 struct window *w;
16175 {
16176 Lisp_Object old_selected_window, old_selected_frame;
16177 int n = 0;
16178
16179 old_selected_frame = selected_frame;
16180 selected_frame = w->frame;
16181 old_selected_window = selected_window;
16182 XSETWINDOW (selected_window, w);
16183
16184 /* These will be set while the mode line specs are processed. */
16185 line_number_displayed = 0;
16186 w->column_number_displayed = Qnil;
16187
16188 if (WINDOW_WANTS_MODELINE_P (w))
16189 {
16190 struct window *sel_w = XWINDOW (old_selected_window);
16191
16192 /* Select mode line face based on the real selected window. */
16193 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16194 current_buffer->mode_line_format);
16195 ++n;
16196 }
16197
16198 if (WINDOW_WANTS_HEADER_LINE_P (w))
16199 {
16200 display_mode_line (w, HEADER_LINE_FACE_ID,
16201 current_buffer->header_line_format);
16202 ++n;
16203 }
16204
16205 selected_frame = old_selected_frame;
16206 selected_window = old_selected_window;
16207 return n;
16208 }
16209
16210
16211 /* Display mode or top line of window W. FACE_ID specifies which line
16212 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16213 FORMAT is the mode line format to display. Value is the pixel
16214 height of the mode line displayed. */
16215
16216 static int
16217 display_mode_line (w, face_id, format)
16218 struct window *w;
16219 enum face_id face_id;
16220 Lisp_Object format;
16221 {
16222 struct it it;
16223 struct face *face;
16224 int count = SPECPDL_INDEX ();
16225
16226 init_iterator (&it, w, -1, -1, NULL, face_id);
16227 prepare_desired_row (it.glyph_row);
16228
16229 it.glyph_row->mode_line_p = 1;
16230
16231 if (! mode_line_inverse_video)
16232 /* Force the mode-line to be displayed in the default face. */
16233 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16234
16235 record_unwind_protect (unwind_format_mode_line,
16236 format_mode_line_unwind_data (NULL, 0));
16237
16238 mode_line_target = MODE_LINE_DISPLAY;
16239
16240 /* Temporarily make frame's keyboard the current kboard so that
16241 kboard-local variables in the mode_line_format will get the right
16242 values. */
16243 push_kboard (FRAME_KBOARD (it.f));
16244 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16245 pop_kboard ();
16246
16247 unbind_to (count, Qnil);
16248
16249 /* Fill up with spaces. */
16250 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16251
16252 compute_line_metrics (&it);
16253 it.glyph_row->full_width_p = 1;
16254 it.glyph_row->continued_p = 0;
16255 it.glyph_row->truncated_on_left_p = 0;
16256 it.glyph_row->truncated_on_right_p = 0;
16257
16258 /* Make a 3D mode-line have a shadow at its right end. */
16259 face = FACE_FROM_ID (it.f, face_id);
16260 extend_face_to_end_of_line (&it);
16261 if (face->box != FACE_NO_BOX)
16262 {
16263 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16264 + it.glyph_row->used[TEXT_AREA] - 1);
16265 last->right_box_line_p = 1;
16266 }
16267
16268 return it.glyph_row->height;
16269 }
16270
16271 /* Move element ELT in LIST to the front of LIST.
16272 Return the updated list. */
16273
16274 static Lisp_Object
16275 move_elt_to_front (elt, list)
16276 Lisp_Object elt, list;
16277 {
16278 register Lisp_Object tail, prev;
16279 register Lisp_Object tem;
16280
16281 tail = list;
16282 prev = Qnil;
16283 while (CONSP (tail))
16284 {
16285 tem = XCAR (tail);
16286
16287 if (EQ (elt, tem))
16288 {
16289 /* Splice out the link TAIL. */
16290 if (NILP (prev))
16291 list = XCDR (tail);
16292 else
16293 Fsetcdr (prev, XCDR (tail));
16294
16295 /* Now make it the first. */
16296 Fsetcdr (tail, list);
16297 return tail;
16298 }
16299 else
16300 prev = tail;
16301 tail = XCDR (tail);
16302 QUIT;
16303 }
16304
16305 /* Not found--return unchanged LIST. */
16306 return list;
16307 }
16308
16309 /* Contribute ELT to the mode line for window IT->w. How it
16310 translates into text depends on its data type.
16311
16312 IT describes the display environment in which we display, as usual.
16313
16314 DEPTH is the depth in recursion. It is used to prevent
16315 infinite recursion here.
16316
16317 FIELD_WIDTH is the number of characters the display of ELT should
16318 occupy in the mode line, and PRECISION is the maximum number of
16319 characters to display from ELT's representation. See
16320 display_string for details.
16321
16322 Returns the hpos of the end of the text generated by ELT.
16323
16324 PROPS is a property list to add to any string we encounter.
16325
16326 If RISKY is nonzero, remove (disregard) any properties in any string
16327 we encounter, and ignore :eval and :propertize.
16328
16329 The global variable `mode_line_target' determines whether the
16330 output is passed to `store_mode_line_noprop',
16331 `store_mode_line_string', or `display_string'. */
16332
16333 static int
16334 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16335 struct it *it;
16336 int depth;
16337 int field_width, precision;
16338 Lisp_Object elt, props;
16339 int risky;
16340 {
16341 int n = 0, field, prec;
16342 int literal = 0;
16343
16344 tail_recurse:
16345 if (depth > 100)
16346 elt = build_string ("*too-deep*");
16347
16348 depth++;
16349
16350 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16351 {
16352 case Lisp_String:
16353 {
16354 /* A string: output it and check for %-constructs within it. */
16355 unsigned char c;
16356 int offset = 0;
16357
16358 if (SCHARS (elt) > 0
16359 && (!NILP (props) || risky))
16360 {
16361 Lisp_Object oprops, aelt;
16362 oprops = Ftext_properties_at (make_number (0), elt);
16363
16364 /* If the starting string's properties are not what
16365 we want, translate the string. Also, if the string
16366 is risky, do that anyway. */
16367
16368 if (NILP (Fequal (props, oprops)) || risky)
16369 {
16370 /* If the starting string has properties,
16371 merge the specified ones onto the existing ones. */
16372 if (! NILP (oprops) && !risky)
16373 {
16374 Lisp_Object tem;
16375
16376 oprops = Fcopy_sequence (oprops);
16377 tem = props;
16378 while (CONSP (tem))
16379 {
16380 oprops = Fplist_put (oprops, XCAR (tem),
16381 XCAR (XCDR (tem)));
16382 tem = XCDR (XCDR (tem));
16383 }
16384 props = oprops;
16385 }
16386
16387 aelt = Fassoc (elt, mode_line_proptrans_alist);
16388 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16389 {
16390 /* AELT is what we want. Move it to the front
16391 without consing. */
16392 elt = XCAR (aelt);
16393 mode_line_proptrans_alist
16394 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16395 }
16396 else
16397 {
16398 Lisp_Object tem;
16399
16400 /* If AELT has the wrong props, it is useless.
16401 so get rid of it. */
16402 if (! NILP (aelt))
16403 mode_line_proptrans_alist
16404 = Fdelq (aelt, mode_line_proptrans_alist);
16405
16406 elt = Fcopy_sequence (elt);
16407 Fset_text_properties (make_number (0), Flength (elt),
16408 props, elt);
16409 /* Add this item to mode_line_proptrans_alist. */
16410 mode_line_proptrans_alist
16411 = Fcons (Fcons (elt, props),
16412 mode_line_proptrans_alist);
16413 /* Truncate mode_line_proptrans_alist
16414 to at most 50 elements. */
16415 tem = Fnthcdr (make_number (50),
16416 mode_line_proptrans_alist);
16417 if (! NILP (tem))
16418 XSETCDR (tem, Qnil);
16419 }
16420 }
16421 }
16422
16423 offset = 0;
16424
16425 if (literal)
16426 {
16427 prec = precision - n;
16428 switch (mode_line_target)
16429 {
16430 case MODE_LINE_NOPROP:
16431 case MODE_LINE_TITLE:
16432 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16433 break;
16434 case MODE_LINE_STRING:
16435 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16436 break;
16437 case MODE_LINE_DISPLAY:
16438 n += display_string (NULL, elt, Qnil, 0, 0, it,
16439 0, prec, 0, STRING_MULTIBYTE (elt));
16440 break;
16441 }
16442
16443 break;
16444 }
16445
16446 /* Handle the non-literal case. */
16447
16448 while ((precision <= 0 || n < precision)
16449 && SREF (elt, offset) != 0
16450 && (mode_line_target != MODE_LINE_DISPLAY
16451 || it->current_x < it->last_visible_x))
16452 {
16453 int last_offset = offset;
16454
16455 /* Advance to end of string or next format specifier. */
16456 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16457 ;
16458
16459 if (offset - 1 != last_offset)
16460 {
16461 int nchars, nbytes;
16462
16463 /* Output to end of string or up to '%'. Field width
16464 is length of string. Don't output more than
16465 PRECISION allows us. */
16466 offset--;
16467
16468 prec = c_string_width (SDATA (elt) + last_offset,
16469 offset - last_offset, precision - n,
16470 &nchars, &nbytes);
16471
16472 switch (mode_line_target)
16473 {
16474 case MODE_LINE_NOPROP:
16475 case MODE_LINE_TITLE:
16476 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16477 break;
16478 case MODE_LINE_STRING:
16479 {
16480 int bytepos = last_offset;
16481 int charpos = string_byte_to_char (elt, bytepos);
16482 int endpos = (precision <= 0
16483 ? string_byte_to_char (elt, offset)
16484 : charpos + nchars);
16485
16486 n += store_mode_line_string (NULL,
16487 Fsubstring (elt, make_number (charpos),
16488 make_number (endpos)),
16489 0, 0, 0, Qnil);
16490 }
16491 break;
16492 case MODE_LINE_DISPLAY:
16493 {
16494 int bytepos = last_offset;
16495 int charpos = string_byte_to_char (elt, bytepos);
16496
16497 if (precision <= 0)
16498 nchars = string_byte_to_char (elt, offset) - charpos;
16499 n += display_string (NULL, elt, Qnil, 0, charpos,
16500 it, 0, nchars, 0,
16501 STRING_MULTIBYTE (elt));
16502 }
16503 break;
16504 }
16505 }
16506 else /* c == '%' */
16507 {
16508 int percent_position = offset;
16509
16510 /* Get the specified minimum width. Zero means
16511 don't pad. */
16512 field = 0;
16513 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16514 field = field * 10 + c - '0';
16515
16516 /* Don't pad beyond the total padding allowed. */
16517 if (field_width - n > 0 && field > field_width - n)
16518 field = field_width - n;
16519
16520 /* Note that either PRECISION <= 0 or N < PRECISION. */
16521 prec = precision - n;
16522
16523 if (c == 'M')
16524 n += display_mode_element (it, depth, field, prec,
16525 Vglobal_mode_string, props,
16526 risky);
16527 else if (c != 0)
16528 {
16529 int multibyte;
16530 int bytepos, charpos;
16531 unsigned char *spec;
16532
16533 bytepos = percent_position;
16534 charpos = (STRING_MULTIBYTE (elt)
16535 ? string_byte_to_char (elt, bytepos)
16536 : bytepos);
16537
16538 spec
16539 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16540
16541 switch (mode_line_target)
16542 {
16543 case MODE_LINE_NOPROP:
16544 case MODE_LINE_TITLE:
16545 n += store_mode_line_noprop (spec, field, prec);
16546 break;
16547 case MODE_LINE_STRING:
16548 {
16549 int len = strlen (spec);
16550 Lisp_Object tem = make_string (spec, len);
16551 props = Ftext_properties_at (make_number (charpos), elt);
16552 /* Should only keep face property in props */
16553 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16554 }
16555 break;
16556 case MODE_LINE_DISPLAY:
16557 {
16558 int nglyphs_before, nwritten;
16559
16560 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16561 nwritten = display_string (spec, Qnil, elt,
16562 charpos, 0, it,
16563 field, prec, 0,
16564 multibyte);
16565
16566 /* Assign to the glyphs written above the
16567 string where the `%x' came from, position
16568 of the `%'. */
16569 if (nwritten > 0)
16570 {
16571 struct glyph *glyph
16572 = (it->glyph_row->glyphs[TEXT_AREA]
16573 + nglyphs_before);
16574 int i;
16575
16576 for (i = 0; i < nwritten; ++i)
16577 {
16578 glyph[i].object = elt;
16579 glyph[i].charpos = charpos;
16580 }
16581
16582 n += nwritten;
16583 }
16584 }
16585 break;
16586 }
16587 }
16588 else /* c == 0 */
16589 break;
16590 }
16591 }
16592 }
16593 break;
16594
16595 case Lisp_Symbol:
16596 /* A symbol: process the value of the symbol recursively
16597 as if it appeared here directly. Avoid error if symbol void.
16598 Special case: if value of symbol is a string, output the string
16599 literally. */
16600 {
16601 register Lisp_Object tem;
16602
16603 /* If the variable is not marked as risky to set
16604 then its contents are risky to use. */
16605 if (NILP (Fget (elt, Qrisky_local_variable)))
16606 risky = 1;
16607
16608 tem = Fboundp (elt);
16609 if (!NILP (tem))
16610 {
16611 tem = Fsymbol_value (elt);
16612 /* If value is a string, output that string literally:
16613 don't check for % within it. */
16614 if (STRINGP (tem))
16615 literal = 1;
16616
16617 if (!EQ (tem, elt))
16618 {
16619 /* Give up right away for nil or t. */
16620 elt = tem;
16621 goto tail_recurse;
16622 }
16623 }
16624 }
16625 break;
16626
16627 case Lisp_Cons:
16628 {
16629 register Lisp_Object car, tem;
16630
16631 /* A cons cell: five distinct cases.
16632 If first element is :eval or :propertize, do something special.
16633 If first element is a string or a cons, process all the elements
16634 and effectively concatenate them.
16635 If first element is a negative number, truncate displaying cdr to
16636 at most that many characters. If positive, pad (with spaces)
16637 to at least that many characters.
16638 If first element is a symbol, process the cadr or caddr recursively
16639 according to whether the symbol's value is non-nil or nil. */
16640 car = XCAR (elt);
16641 if (EQ (car, QCeval))
16642 {
16643 /* An element of the form (:eval FORM) means evaluate FORM
16644 and use the result as mode line elements. */
16645
16646 if (risky)
16647 break;
16648
16649 if (CONSP (XCDR (elt)))
16650 {
16651 Lisp_Object spec;
16652 spec = safe_eval (XCAR (XCDR (elt)));
16653 n += display_mode_element (it, depth, field_width - n,
16654 precision - n, spec, props,
16655 risky);
16656 }
16657 }
16658 else if (EQ (car, QCpropertize))
16659 {
16660 /* An element of the form (:propertize ELT PROPS...)
16661 means display ELT but applying properties PROPS. */
16662
16663 if (risky)
16664 break;
16665
16666 if (CONSP (XCDR (elt)))
16667 n += display_mode_element (it, depth, field_width - n,
16668 precision - n, XCAR (XCDR (elt)),
16669 XCDR (XCDR (elt)), risky);
16670 }
16671 else if (SYMBOLP (car))
16672 {
16673 tem = Fboundp (car);
16674 elt = XCDR (elt);
16675 if (!CONSP (elt))
16676 goto invalid;
16677 /* elt is now the cdr, and we know it is a cons cell.
16678 Use its car if CAR has a non-nil value. */
16679 if (!NILP (tem))
16680 {
16681 tem = Fsymbol_value (car);
16682 if (!NILP (tem))
16683 {
16684 elt = XCAR (elt);
16685 goto tail_recurse;
16686 }
16687 }
16688 /* Symbol's value is nil (or symbol is unbound)
16689 Get the cddr of the original list
16690 and if possible find the caddr and use that. */
16691 elt = XCDR (elt);
16692 if (NILP (elt))
16693 break;
16694 else if (!CONSP (elt))
16695 goto invalid;
16696 elt = XCAR (elt);
16697 goto tail_recurse;
16698 }
16699 else if (INTEGERP (car))
16700 {
16701 register int lim = XINT (car);
16702 elt = XCDR (elt);
16703 if (lim < 0)
16704 {
16705 /* Negative int means reduce maximum width. */
16706 if (precision <= 0)
16707 precision = -lim;
16708 else
16709 precision = min (precision, -lim);
16710 }
16711 else if (lim > 0)
16712 {
16713 /* Padding specified. Don't let it be more than
16714 current maximum. */
16715 if (precision > 0)
16716 lim = min (precision, lim);
16717
16718 /* If that's more padding than already wanted, queue it.
16719 But don't reduce padding already specified even if
16720 that is beyond the current truncation point. */
16721 field_width = max (lim, field_width);
16722 }
16723 goto tail_recurse;
16724 }
16725 else if (STRINGP (car) || CONSP (car))
16726 {
16727 register int limit = 50;
16728 /* Limit is to protect against circular lists. */
16729 while (CONSP (elt)
16730 && --limit > 0
16731 && (precision <= 0 || n < precision))
16732 {
16733 n += display_mode_element (it, depth,
16734 /* Do padding only after the last
16735 element in the list. */
16736 (! CONSP (XCDR (elt))
16737 ? field_width - n
16738 : 0),
16739 precision - n, XCAR (elt),
16740 props, risky);
16741 elt = XCDR (elt);
16742 }
16743 }
16744 }
16745 break;
16746
16747 default:
16748 invalid:
16749 elt = build_string ("*invalid*");
16750 goto tail_recurse;
16751 }
16752
16753 /* Pad to FIELD_WIDTH. */
16754 if (field_width > 0 && n < field_width)
16755 {
16756 switch (mode_line_target)
16757 {
16758 case MODE_LINE_NOPROP:
16759 case MODE_LINE_TITLE:
16760 n += store_mode_line_noprop ("", field_width - n, 0);
16761 break;
16762 case MODE_LINE_STRING:
16763 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16764 break;
16765 case MODE_LINE_DISPLAY:
16766 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16767 0, 0, 0);
16768 break;
16769 }
16770 }
16771
16772 return n;
16773 }
16774
16775 /* Store a mode-line string element in mode_line_string_list.
16776
16777 If STRING is non-null, display that C string. Otherwise, the Lisp
16778 string LISP_STRING is displayed.
16779
16780 FIELD_WIDTH is the minimum number of output glyphs to produce.
16781 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16782 with spaces. FIELD_WIDTH <= 0 means don't pad.
16783
16784 PRECISION is the maximum number of characters to output from
16785 STRING. PRECISION <= 0 means don't truncate the string.
16786
16787 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16788 properties to the string.
16789
16790 PROPS are the properties to add to the string.
16791 The mode_line_string_face face property is always added to the string.
16792 */
16793
16794 static int
16795 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16796 char *string;
16797 Lisp_Object lisp_string;
16798 int copy_string;
16799 int field_width;
16800 int precision;
16801 Lisp_Object props;
16802 {
16803 int len;
16804 int n = 0;
16805
16806 if (string != NULL)
16807 {
16808 len = strlen (string);
16809 if (precision > 0 && len > precision)
16810 len = precision;
16811 lisp_string = make_string (string, len);
16812 if (NILP (props))
16813 props = mode_line_string_face_prop;
16814 else if (!NILP (mode_line_string_face))
16815 {
16816 Lisp_Object face = Fplist_get (props, Qface);
16817 props = Fcopy_sequence (props);
16818 if (NILP (face))
16819 face = mode_line_string_face;
16820 else
16821 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16822 props = Fplist_put (props, Qface, face);
16823 }
16824 Fadd_text_properties (make_number (0), make_number (len),
16825 props, lisp_string);
16826 }
16827 else
16828 {
16829 len = XFASTINT (Flength (lisp_string));
16830 if (precision > 0 && len > precision)
16831 {
16832 len = precision;
16833 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16834 precision = -1;
16835 }
16836 if (!NILP (mode_line_string_face))
16837 {
16838 Lisp_Object face;
16839 if (NILP (props))
16840 props = Ftext_properties_at (make_number (0), lisp_string);
16841 face = Fplist_get (props, Qface);
16842 if (NILP (face))
16843 face = mode_line_string_face;
16844 else
16845 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16846 props = Fcons (Qface, Fcons (face, Qnil));
16847 if (copy_string)
16848 lisp_string = Fcopy_sequence (lisp_string);
16849 }
16850 if (!NILP (props))
16851 Fadd_text_properties (make_number (0), make_number (len),
16852 props, lisp_string);
16853 }
16854
16855 if (len > 0)
16856 {
16857 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16858 n += len;
16859 }
16860
16861 if (field_width > len)
16862 {
16863 field_width -= len;
16864 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16865 if (!NILP (props))
16866 Fadd_text_properties (make_number (0), make_number (field_width),
16867 props, lisp_string);
16868 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16869 n += field_width;
16870 }
16871
16872 return n;
16873 }
16874
16875
16876 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16877 1, 4, 0,
16878 doc: /* Format a string out of a mode line format specification.
16879 First arg FORMAT specifies the mode line format (see `mode-line-format'
16880 for details) to use.
16881
16882 Optional second arg FACE specifies the face property to put
16883 on all characters for which no face is specified.
16884 t means whatever face the window's mode line currently uses
16885 \(either `mode-line' or `mode-line-inactive', depending).
16886 nil means the default is no face property.
16887 If FACE is an integer, the value string has no text properties.
16888
16889 Optional third and fourth args WINDOW and BUFFER specify the window
16890 and buffer to use as the context for the formatting (defaults
16891 are the selected window and the window's buffer). */)
16892 (format, face, window, buffer)
16893 Lisp_Object format, face, window, buffer;
16894 {
16895 struct it it;
16896 int len;
16897 struct window *w;
16898 struct buffer *old_buffer = NULL;
16899 int face_id = -1;
16900 int no_props = INTEGERP (face);
16901 int count = SPECPDL_INDEX ();
16902 Lisp_Object str;
16903 int string_start = 0;
16904
16905 if (NILP (window))
16906 window = selected_window;
16907 CHECK_WINDOW (window);
16908 w = XWINDOW (window);
16909
16910 if (NILP (buffer))
16911 buffer = w->buffer;
16912 CHECK_BUFFER (buffer);
16913
16914 if (NILP (format))
16915 return build_string ("");
16916
16917 if (no_props)
16918 face = Qnil;
16919
16920 if (!NILP (face))
16921 {
16922 if (EQ (face, Qt))
16923 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16924 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16925 }
16926
16927 if (face_id < 0)
16928 face_id = DEFAULT_FACE_ID;
16929
16930 if (XBUFFER (buffer) != current_buffer)
16931 old_buffer = current_buffer;
16932
16933 /* Save things including mode_line_proptrans_alist,
16934 and set that to nil so that we don't alter the outer value. */
16935 record_unwind_protect (unwind_format_mode_line,
16936 format_mode_line_unwind_data (old_buffer, 1));
16937 mode_line_proptrans_alist = Qnil;
16938
16939 if (old_buffer)
16940 set_buffer_internal_1 (XBUFFER (buffer));
16941
16942 init_iterator (&it, w, -1, -1, NULL, face_id);
16943
16944 if (no_props)
16945 {
16946 mode_line_target = MODE_LINE_NOPROP;
16947 mode_line_string_face_prop = Qnil;
16948 mode_line_string_list = Qnil;
16949 string_start = MODE_LINE_NOPROP_LEN (0);
16950 }
16951 else
16952 {
16953 mode_line_target = MODE_LINE_STRING;
16954 mode_line_string_list = Qnil;
16955 mode_line_string_face = face;
16956 mode_line_string_face_prop
16957 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16958 }
16959
16960 push_kboard (FRAME_KBOARD (it.f));
16961 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16962 pop_kboard ();
16963
16964 if (no_props)
16965 {
16966 len = MODE_LINE_NOPROP_LEN (string_start);
16967 str = make_string (mode_line_noprop_buf + string_start, len);
16968 }
16969 else
16970 {
16971 mode_line_string_list = Fnreverse (mode_line_string_list);
16972 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16973 make_string ("", 0));
16974 }
16975
16976 unbind_to (count, Qnil);
16977 return str;
16978 }
16979
16980 /* Write a null-terminated, right justified decimal representation of
16981 the positive integer D to BUF using a minimal field width WIDTH. */
16982
16983 static void
16984 pint2str (buf, width, d)
16985 register char *buf;
16986 register int width;
16987 register int d;
16988 {
16989 register char *p = buf;
16990
16991 if (d <= 0)
16992 *p++ = '0';
16993 else
16994 {
16995 while (d > 0)
16996 {
16997 *p++ = d % 10 + '0';
16998 d /= 10;
16999 }
17000 }
17001
17002 for (width -= (int) (p - buf); width > 0; --width)
17003 *p++ = ' ';
17004 *p-- = '\0';
17005 while (p > buf)
17006 {
17007 d = *buf;
17008 *buf++ = *p;
17009 *p-- = d;
17010 }
17011 }
17012
17013 /* Write a null-terminated, right justified decimal and "human
17014 readable" representation of the nonnegative integer D to BUF using
17015 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17016
17017 static const char power_letter[] =
17018 {
17019 0, /* not used */
17020 'k', /* kilo */
17021 'M', /* mega */
17022 'G', /* giga */
17023 'T', /* tera */
17024 'P', /* peta */
17025 'E', /* exa */
17026 'Z', /* zetta */
17027 'Y' /* yotta */
17028 };
17029
17030 static void
17031 pint2hrstr (buf, width, d)
17032 char *buf;
17033 int width;
17034 int d;
17035 {
17036 /* We aim to represent the nonnegative integer D as
17037 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17038 int quotient = d;
17039 int remainder = 0;
17040 /* -1 means: do not use TENTHS. */
17041 int tenths = -1;
17042 int exponent = 0;
17043
17044 /* Length of QUOTIENT.TENTHS as a string. */
17045 int length;
17046
17047 char * psuffix;
17048 char * p;
17049
17050 if (1000 <= quotient)
17051 {
17052 /* Scale to the appropriate EXPONENT. */
17053 do
17054 {
17055 remainder = quotient % 1000;
17056 quotient /= 1000;
17057 exponent++;
17058 }
17059 while (1000 <= quotient);
17060
17061 /* Round to nearest and decide whether to use TENTHS or not. */
17062 if (quotient <= 9)
17063 {
17064 tenths = remainder / 100;
17065 if (50 <= remainder % 100)
17066 {
17067 if (tenths < 9)
17068 tenths++;
17069 else
17070 {
17071 quotient++;
17072 if (quotient == 10)
17073 tenths = -1;
17074 else
17075 tenths = 0;
17076 }
17077 }
17078 }
17079 else
17080 if (500 <= remainder)
17081 {
17082 if (quotient < 999)
17083 quotient++;
17084 else
17085 {
17086 quotient = 1;
17087 exponent++;
17088 tenths = 0;
17089 }
17090 }
17091 }
17092
17093 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17094 if (tenths == -1 && quotient <= 99)
17095 if (quotient <= 9)
17096 length = 1;
17097 else
17098 length = 2;
17099 else
17100 length = 3;
17101 p = psuffix = buf + max (width, length);
17102
17103 /* Print EXPONENT. */
17104 if (exponent)
17105 *psuffix++ = power_letter[exponent];
17106 *psuffix = '\0';
17107
17108 /* Print TENTHS. */
17109 if (tenths >= 0)
17110 {
17111 *--p = '0' + tenths;
17112 *--p = '.';
17113 }
17114
17115 /* Print QUOTIENT. */
17116 do
17117 {
17118 int digit = quotient % 10;
17119 *--p = '0' + digit;
17120 }
17121 while ((quotient /= 10) != 0);
17122
17123 /* Print leading spaces. */
17124 while (buf < p)
17125 *--p = ' ';
17126 }
17127
17128 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17129 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17130 type of CODING_SYSTEM. Return updated pointer into BUF. */
17131
17132 static unsigned char invalid_eol_type[] = "(*invalid*)";
17133
17134 static char *
17135 decode_mode_spec_coding (coding_system, buf, eol_flag)
17136 Lisp_Object coding_system;
17137 register char *buf;
17138 int eol_flag;
17139 {
17140 Lisp_Object val;
17141 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17142 const unsigned char *eol_str;
17143 int eol_str_len;
17144 /* The EOL conversion we are using. */
17145 Lisp_Object eoltype;
17146
17147 val = Fget (coding_system, Qcoding_system);
17148 eoltype = Qnil;
17149
17150 if (!VECTORP (val)) /* Not yet decided. */
17151 {
17152 if (multibyte)
17153 *buf++ = '-';
17154 if (eol_flag)
17155 eoltype = eol_mnemonic_undecided;
17156 /* Don't mention EOL conversion if it isn't decided. */
17157 }
17158 else
17159 {
17160 Lisp_Object eolvalue;
17161
17162 eolvalue = Fget (coding_system, Qeol_type);
17163
17164 if (multibyte)
17165 *buf++ = XFASTINT (AREF (val, 1));
17166
17167 if (eol_flag)
17168 {
17169 /* The EOL conversion that is normal on this system. */
17170
17171 if (NILP (eolvalue)) /* Not yet decided. */
17172 eoltype = eol_mnemonic_undecided;
17173 else if (VECTORP (eolvalue)) /* Not yet decided. */
17174 eoltype = eol_mnemonic_undecided;
17175 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17176 eoltype = (XFASTINT (eolvalue) == 0
17177 ? eol_mnemonic_unix
17178 : (XFASTINT (eolvalue) == 1
17179 ? eol_mnemonic_dos : eol_mnemonic_mac));
17180 }
17181 }
17182
17183 if (eol_flag)
17184 {
17185 /* Mention the EOL conversion if it is not the usual one. */
17186 if (STRINGP (eoltype))
17187 {
17188 eol_str = SDATA (eoltype);
17189 eol_str_len = SBYTES (eoltype);
17190 }
17191 else if (INTEGERP (eoltype)
17192 && CHAR_VALID_P (XINT (eoltype), 0))
17193 {
17194 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17195 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17196 eol_str = tmp;
17197 }
17198 else
17199 {
17200 eol_str = invalid_eol_type;
17201 eol_str_len = sizeof (invalid_eol_type) - 1;
17202 }
17203 bcopy (eol_str, buf, eol_str_len);
17204 buf += eol_str_len;
17205 }
17206
17207 return buf;
17208 }
17209
17210 /* Return a string for the output of a mode line %-spec for window W,
17211 generated by character C. PRECISION >= 0 means don't return a
17212 string longer than that value. FIELD_WIDTH > 0 means pad the
17213 string returned with spaces to that value. Return 1 in *MULTIBYTE
17214 if the result is multibyte text.
17215
17216 Note we operate on the current buffer for most purposes,
17217 the exception being w->base_line_pos. */
17218
17219 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17220
17221 static char *
17222 decode_mode_spec (w, c, field_width, precision, multibyte)
17223 struct window *w;
17224 register int c;
17225 int field_width, precision;
17226 int *multibyte;
17227 {
17228 Lisp_Object obj;
17229 struct frame *f = XFRAME (WINDOW_FRAME (w));
17230 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17231 struct buffer *b = current_buffer;
17232
17233 obj = Qnil;
17234 *multibyte = 0;
17235
17236 switch (c)
17237 {
17238 case '*':
17239 if (!NILP (b->read_only))
17240 return "%";
17241 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17242 return "*";
17243 return "-";
17244
17245 case '+':
17246 /* This differs from %* only for a modified read-only buffer. */
17247 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17248 return "*";
17249 if (!NILP (b->read_only))
17250 return "%";
17251 return "-";
17252
17253 case '&':
17254 /* This differs from %* in ignoring read-only-ness. */
17255 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17256 return "*";
17257 return "-";
17258
17259 case '%':
17260 return "%";
17261
17262 case '[':
17263 {
17264 int i;
17265 char *p;
17266
17267 if (command_loop_level > 5)
17268 return "[[[... ";
17269 p = decode_mode_spec_buf;
17270 for (i = 0; i < command_loop_level; i++)
17271 *p++ = '[';
17272 *p = 0;
17273 return decode_mode_spec_buf;
17274 }
17275
17276 case ']':
17277 {
17278 int i;
17279 char *p;
17280
17281 if (command_loop_level > 5)
17282 return " ...]]]";
17283 p = decode_mode_spec_buf;
17284 for (i = 0; i < command_loop_level; i++)
17285 *p++ = ']';
17286 *p = 0;
17287 return decode_mode_spec_buf;
17288 }
17289
17290 case '-':
17291 {
17292 register int i;
17293
17294 /* Let lots_of_dashes be a string of infinite length. */
17295 if (mode_line_target == MODE_LINE_NOPROP ||
17296 mode_line_target == MODE_LINE_STRING)
17297 return "--";
17298 if (field_width <= 0
17299 || field_width > sizeof (lots_of_dashes))
17300 {
17301 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17302 decode_mode_spec_buf[i] = '-';
17303 decode_mode_spec_buf[i] = '\0';
17304 return decode_mode_spec_buf;
17305 }
17306 else
17307 return lots_of_dashes;
17308 }
17309
17310 case 'b':
17311 obj = b->name;
17312 break;
17313
17314 case 'c':
17315 {
17316 int col = (int) current_column (); /* iftc */
17317 w->column_number_displayed = make_number (col);
17318 pint2str (decode_mode_spec_buf, field_width, col);
17319 return decode_mode_spec_buf;
17320 }
17321
17322 case 'e':
17323 #ifndef SYSTEM_MALLOC
17324 {
17325 if (NILP (Vmemory_full))
17326 return "";
17327 else
17328 return "!MEM FULL! ";
17329 }
17330 #else
17331 return "";
17332 #endif
17333
17334 case 'F':
17335 /* %F displays the frame name. */
17336 if (!NILP (f->title))
17337 return (char *) SDATA (f->title);
17338 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17339 return (char *) SDATA (f->name);
17340 return "Emacs";
17341
17342 case 'f':
17343 obj = b->filename;
17344 break;
17345
17346 case 'i':
17347 {
17348 int size = ZV - BEGV;
17349 pint2str (decode_mode_spec_buf, field_width, size);
17350 return decode_mode_spec_buf;
17351 }
17352
17353 case 'I':
17354 {
17355 int size = ZV - BEGV;
17356 pint2hrstr (decode_mode_spec_buf, field_width, size);
17357 return decode_mode_spec_buf;
17358 }
17359
17360 case 'l':
17361 {
17362 int startpos = XMARKER (w->start)->charpos;
17363 int startpos_byte = marker_byte_position (w->start);
17364 int line, linepos, linepos_byte, topline;
17365 int nlines, junk;
17366 int height = WINDOW_TOTAL_LINES (w);
17367
17368 /* If we decided that this buffer isn't suitable for line numbers,
17369 don't forget that too fast. */
17370 if (EQ (w->base_line_pos, w->buffer))
17371 goto no_value;
17372 /* But do forget it, if the window shows a different buffer now. */
17373 else if (BUFFERP (w->base_line_pos))
17374 w->base_line_pos = Qnil;
17375
17376 /* If the buffer is very big, don't waste time. */
17377 if (INTEGERP (Vline_number_display_limit)
17378 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17379 {
17380 w->base_line_pos = Qnil;
17381 w->base_line_number = Qnil;
17382 goto no_value;
17383 }
17384
17385 if (!NILP (w->base_line_number)
17386 && !NILP (w->base_line_pos)
17387 && XFASTINT (w->base_line_pos) <= startpos)
17388 {
17389 line = XFASTINT (w->base_line_number);
17390 linepos = XFASTINT (w->base_line_pos);
17391 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17392 }
17393 else
17394 {
17395 line = 1;
17396 linepos = BUF_BEGV (b);
17397 linepos_byte = BUF_BEGV_BYTE (b);
17398 }
17399
17400 /* Count lines from base line to window start position. */
17401 nlines = display_count_lines (linepos, linepos_byte,
17402 startpos_byte,
17403 startpos, &junk);
17404
17405 topline = nlines + line;
17406
17407 /* Determine a new base line, if the old one is too close
17408 or too far away, or if we did not have one.
17409 "Too close" means it's plausible a scroll-down would
17410 go back past it. */
17411 if (startpos == BUF_BEGV (b))
17412 {
17413 w->base_line_number = make_number (topline);
17414 w->base_line_pos = make_number (BUF_BEGV (b));
17415 }
17416 else if (nlines < height + 25 || nlines > height * 3 + 50
17417 || linepos == BUF_BEGV (b))
17418 {
17419 int limit = BUF_BEGV (b);
17420 int limit_byte = BUF_BEGV_BYTE (b);
17421 int position;
17422 int distance = (height * 2 + 30) * line_number_display_limit_width;
17423
17424 if (startpos - distance > limit)
17425 {
17426 limit = startpos - distance;
17427 limit_byte = CHAR_TO_BYTE (limit);
17428 }
17429
17430 nlines = display_count_lines (startpos, startpos_byte,
17431 limit_byte,
17432 - (height * 2 + 30),
17433 &position);
17434 /* If we couldn't find the lines we wanted within
17435 line_number_display_limit_width chars per line,
17436 give up on line numbers for this window. */
17437 if (position == limit_byte && limit == startpos - distance)
17438 {
17439 w->base_line_pos = w->buffer;
17440 w->base_line_number = Qnil;
17441 goto no_value;
17442 }
17443
17444 w->base_line_number = make_number (topline - nlines);
17445 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17446 }
17447
17448 /* Now count lines from the start pos to point. */
17449 nlines = display_count_lines (startpos, startpos_byte,
17450 PT_BYTE, PT, &junk);
17451
17452 /* Record that we did display the line number. */
17453 line_number_displayed = 1;
17454
17455 /* Make the string to show. */
17456 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17457 return decode_mode_spec_buf;
17458 no_value:
17459 {
17460 char* p = decode_mode_spec_buf;
17461 int pad = field_width - 2;
17462 while (pad-- > 0)
17463 *p++ = ' ';
17464 *p++ = '?';
17465 *p++ = '?';
17466 *p = '\0';
17467 return decode_mode_spec_buf;
17468 }
17469 }
17470 break;
17471
17472 case 'm':
17473 obj = b->mode_name;
17474 break;
17475
17476 case 'n':
17477 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17478 return " Narrow";
17479 break;
17480
17481 case 'p':
17482 {
17483 int pos = marker_position (w->start);
17484 int total = BUF_ZV (b) - BUF_BEGV (b);
17485
17486 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17487 {
17488 if (pos <= BUF_BEGV (b))
17489 return "All";
17490 else
17491 return "Bottom";
17492 }
17493 else if (pos <= BUF_BEGV (b))
17494 return "Top";
17495 else
17496 {
17497 if (total > 1000000)
17498 /* Do it differently for a large value, to avoid overflow. */
17499 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17500 else
17501 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17502 /* We can't normally display a 3-digit number,
17503 so get us a 2-digit number that is close. */
17504 if (total == 100)
17505 total = 99;
17506 sprintf (decode_mode_spec_buf, "%2d%%", total);
17507 return decode_mode_spec_buf;
17508 }
17509 }
17510
17511 /* Display percentage of size above the bottom of the screen. */
17512 case 'P':
17513 {
17514 int toppos = marker_position (w->start);
17515 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17516 int total = BUF_ZV (b) - BUF_BEGV (b);
17517
17518 if (botpos >= BUF_ZV (b))
17519 {
17520 if (toppos <= BUF_BEGV (b))
17521 return "All";
17522 else
17523 return "Bottom";
17524 }
17525 else
17526 {
17527 if (total > 1000000)
17528 /* Do it differently for a large value, to avoid overflow. */
17529 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17530 else
17531 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17532 /* We can't normally display a 3-digit number,
17533 so get us a 2-digit number that is close. */
17534 if (total == 100)
17535 total = 99;
17536 if (toppos <= BUF_BEGV (b))
17537 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17538 else
17539 sprintf (decode_mode_spec_buf, "%2d%%", total);
17540 return decode_mode_spec_buf;
17541 }
17542 }
17543
17544 case 's':
17545 /* status of process */
17546 obj = Fget_buffer_process (Fcurrent_buffer ());
17547 if (NILP (obj))
17548 return "no process";
17549 #ifdef subprocesses
17550 obj = Fsymbol_name (Fprocess_status (obj));
17551 #endif
17552 break;
17553
17554 case 't': /* indicate TEXT or BINARY */
17555 #ifdef MODE_LINE_BINARY_TEXT
17556 return MODE_LINE_BINARY_TEXT (b);
17557 #else
17558 return "T";
17559 #endif
17560
17561 case 'z':
17562 /* coding-system (not including end-of-line format) */
17563 case 'Z':
17564 /* coding-system (including end-of-line type) */
17565 {
17566 int eol_flag = (c == 'Z');
17567 char *p = decode_mode_spec_buf;
17568
17569 if (! FRAME_WINDOW_P (f))
17570 {
17571 /* No need to mention EOL here--the terminal never needs
17572 to do EOL conversion. */
17573 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
17574 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
17575 }
17576 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17577 p, eol_flag);
17578
17579 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17580 #ifdef subprocesses
17581 obj = Fget_buffer_process (Fcurrent_buffer ());
17582 if (PROCESSP (obj))
17583 {
17584 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17585 p, eol_flag);
17586 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17587 p, eol_flag);
17588 }
17589 #endif /* subprocesses */
17590 #endif /* 0 */
17591 *p = 0;
17592 return decode_mode_spec_buf;
17593 }
17594 }
17595
17596 if (STRINGP (obj))
17597 {
17598 *multibyte = STRING_MULTIBYTE (obj);
17599 return (char *) SDATA (obj);
17600 }
17601 else
17602 return "";
17603 }
17604
17605
17606 /* Count up to COUNT lines starting from START / START_BYTE.
17607 But don't go beyond LIMIT_BYTE.
17608 Return the number of lines thus found (always nonnegative).
17609
17610 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17611
17612 static int
17613 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17614 int start, start_byte, limit_byte, count;
17615 int *byte_pos_ptr;
17616 {
17617 register unsigned char *cursor;
17618 unsigned char *base;
17619
17620 register int ceiling;
17621 register unsigned char *ceiling_addr;
17622 int orig_count = count;
17623
17624 /* If we are not in selective display mode,
17625 check only for newlines. */
17626 int selective_display = (!NILP (current_buffer->selective_display)
17627 && !INTEGERP (current_buffer->selective_display));
17628
17629 if (count > 0)
17630 {
17631 while (start_byte < limit_byte)
17632 {
17633 ceiling = BUFFER_CEILING_OF (start_byte);
17634 ceiling = min (limit_byte - 1, ceiling);
17635 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17636 base = (cursor = BYTE_POS_ADDR (start_byte));
17637 while (1)
17638 {
17639 if (selective_display)
17640 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17641 ;
17642 else
17643 while (*cursor != '\n' && ++cursor != ceiling_addr)
17644 ;
17645
17646 if (cursor != ceiling_addr)
17647 {
17648 if (--count == 0)
17649 {
17650 start_byte += cursor - base + 1;
17651 *byte_pos_ptr = start_byte;
17652 return orig_count;
17653 }
17654 else
17655 if (++cursor == ceiling_addr)
17656 break;
17657 }
17658 else
17659 break;
17660 }
17661 start_byte += cursor - base;
17662 }
17663 }
17664 else
17665 {
17666 while (start_byte > limit_byte)
17667 {
17668 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17669 ceiling = max (limit_byte, ceiling);
17670 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17671 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17672 while (1)
17673 {
17674 if (selective_display)
17675 while (--cursor != ceiling_addr
17676 && *cursor != '\n' && *cursor != 015)
17677 ;
17678 else
17679 while (--cursor != ceiling_addr && *cursor != '\n')
17680 ;
17681
17682 if (cursor != ceiling_addr)
17683 {
17684 if (++count == 0)
17685 {
17686 start_byte += cursor - base + 1;
17687 *byte_pos_ptr = start_byte;
17688 /* When scanning backwards, we should
17689 not count the newline posterior to which we stop. */
17690 return - orig_count - 1;
17691 }
17692 }
17693 else
17694 break;
17695 }
17696 /* Here we add 1 to compensate for the last decrement
17697 of CURSOR, which took it past the valid range. */
17698 start_byte += cursor - base + 1;
17699 }
17700 }
17701
17702 *byte_pos_ptr = limit_byte;
17703
17704 if (count < 0)
17705 return - orig_count + count;
17706 return orig_count - count;
17707
17708 }
17709
17710
17711 \f
17712 /***********************************************************************
17713 Displaying strings
17714 ***********************************************************************/
17715
17716 /* Display a NUL-terminated string, starting with index START.
17717
17718 If STRING is non-null, display that C string. Otherwise, the Lisp
17719 string LISP_STRING is displayed.
17720
17721 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17722 FACE_STRING. Display STRING or LISP_STRING with the face at
17723 FACE_STRING_POS in FACE_STRING:
17724
17725 Display the string in the environment given by IT, but use the
17726 standard display table, temporarily.
17727
17728 FIELD_WIDTH is the minimum number of output glyphs to produce.
17729 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17730 with spaces. If STRING has more characters, more than FIELD_WIDTH
17731 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17732
17733 PRECISION is the maximum number of characters to output from
17734 STRING. PRECISION < 0 means don't truncate the string.
17735
17736 This is roughly equivalent to printf format specifiers:
17737
17738 FIELD_WIDTH PRECISION PRINTF
17739 ----------------------------------------
17740 -1 -1 %s
17741 -1 10 %.10s
17742 10 -1 %10s
17743 20 10 %20.10s
17744
17745 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17746 display them, and < 0 means obey the current buffer's value of
17747 enable_multibyte_characters.
17748
17749 Value is the number of columns displayed. */
17750
17751 static int
17752 display_string (string, lisp_string, face_string, face_string_pos,
17753 start, it, field_width, precision, max_x, multibyte)
17754 unsigned char *string;
17755 Lisp_Object lisp_string;
17756 Lisp_Object face_string;
17757 int face_string_pos;
17758 int start;
17759 struct it *it;
17760 int field_width, precision, max_x;
17761 int multibyte;
17762 {
17763 int hpos_at_start = it->hpos;
17764 int saved_face_id = it->face_id;
17765 struct glyph_row *row = it->glyph_row;
17766
17767 /* Initialize the iterator IT for iteration over STRING beginning
17768 with index START. */
17769 reseat_to_string (it, string, lisp_string, start,
17770 precision, field_width, multibyte);
17771
17772 /* If displaying STRING, set up the face of the iterator
17773 from LISP_STRING, if that's given. */
17774 if (STRINGP (face_string))
17775 {
17776 int endptr;
17777 struct face *face;
17778
17779 it->face_id
17780 = face_at_string_position (it->w, face_string, face_string_pos,
17781 0, it->region_beg_charpos,
17782 it->region_end_charpos,
17783 &endptr, it->base_face_id, 0);
17784 face = FACE_FROM_ID (it->f, it->face_id);
17785 it->face_box_p = face->box != FACE_NO_BOX;
17786 }
17787
17788 /* Set max_x to the maximum allowed X position. Don't let it go
17789 beyond the right edge of the window. */
17790 if (max_x <= 0)
17791 max_x = it->last_visible_x;
17792 else
17793 max_x = min (max_x, it->last_visible_x);
17794
17795 /* Skip over display elements that are not visible. because IT->w is
17796 hscrolled. */
17797 if (it->current_x < it->first_visible_x)
17798 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17799 MOVE_TO_POS | MOVE_TO_X);
17800
17801 row->ascent = it->max_ascent;
17802 row->height = it->max_ascent + it->max_descent;
17803 row->phys_ascent = it->max_phys_ascent;
17804 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17805 row->extra_line_spacing = it->max_extra_line_spacing;
17806
17807 /* This condition is for the case that we are called with current_x
17808 past last_visible_x. */
17809 while (it->current_x < max_x)
17810 {
17811 int x_before, x, n_glyphs_before, i, nglyphs;
17812
17813 /* Get the next display element. */
17814 if (!get_next_display_element (it))
17815 break;
17816
17817 /* Produce glyphs. */
17818 x_before = it->current_x;
17819 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17820 PRODUCE_GLYPHS (it);
17821
17822 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17823 i = 0;
17824 x = x_before;
17825 while (i < nglyphs)
17826 {
17827 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17828
17829 if (!it->truncate_lines_p
17830 && x + glyph->pixel_width > max_x)
17831 {
17832 /* End of continued line or max_x reached. */
17833 if (CHAR_GLYPH_PADDING_P (*glyph))
17834 {
17835 /* A wide character is unbreakable. */
17836 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17837 it->current_x = x_before;
17838 }
17839 else
17840 {
17841 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17842 it->current_x = x;
17843 }
17844 break;
17845 }
17846 else if (x + glyph->pixel_width > it->first_visible_x)
17847 {
17848 /* Glyph is at least partially visible. */
17849 ++it->hpos;
17850 if (x < it->first_visible_x)
17851 it->glyph_row->x = x - it->first_visible_x;
17852 }
17853 else
17854 {
17855 /* Glyph is off the left margin of the display area.
17856 Should not happen. */
17857 abort ();
17858 }
17859
17860 row->ascent = max (row->ascent, it->max_ascent);
17861 row->height = max (row->height, it->max_ascent + it->max_descent);
17862 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17863 row->phys_height = max (row->phys_height,
17864 it->max_phys_ascent + it->max_phys_descent);
17865 row->extra_line_spacing = max (row->extra_line_spacing,
17866 it->max_extra_line_spacing);
17867 x += glyph->pixel_width;
17868 ++i;
17869 }
17870
17871 /* Stop if max_x reached. */
17872 if (i < nglyphs)
17873 break;
17874
17875 /* Stop at line ends. */
17876 if (ITERATOR_AT_END_OF_LINE_P (it))
17877 {
17878 it->continuation_lines_width = 0;
17879 break;
17880 }
17881
17882 set_iterator_to_next (it, 1);
17883
17884 /* Stop if truncating at the right edge. */
17885 if (it->truncate_lines_p
17886 && it->current_x >= it->last_visible_x)
17887 {
17888 /* Add truncation mark, but don't do it if the line is
17889 truncated at a padding space. */
17890 if (IT_CHARPOS (*it) < it->string_nchars)
17891 {
17892 if (!FRAME_WINDOW_P (it->f))
17893 {
17894 int i, n;
17895
17896 if (it->current_x > it->last_visible_x)
17897 {
17898 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17899 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17900 break;
17901 for (n = row->used[TEXT_AREA]; i < n; ++i)
17902 {
17903 row->used[TEXT_AREA] = i;
17904 produce_special_glyphs (it, IT_TRUNCATION);
17905 }
17906 }
17907 produce_special_glyphs (it, IT_TRUNCATION);
17908 }
17909 it->glyph_row->truncated_on_right_p = 1;
17910 }
17911 break;
17912 }
17913 }
17914
17915 /* Maybe insert a truncation at the left. */
17916 if (it->first_visible_x
17917 && IT_CHARPOS (*it) > 0)
17918 {
17919 if (!FRAME_WINDOW_P (it->f))
17920 insert_left_trunc_glyphs (it);
17921 it->glyph_row->truncated_on_left_p = 1;
17922 }
17923
17924 it->face_id = saved_face_id;
17925
17926 /* Value is number of columns displayed. */
17927 return it->hpos - hpos_at_start;
17928 }
17929
17930
17931 \f
17932 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17933 appears as an element of LIST or as the car of an element of LIST.
17934 If PROPVAL is a list, compare each element against LIST in that
17935 way, and return 1/2 if any element of PROPVAL is found in LIST.
17936 Otherwise return 0. This function cannot quit.
17937 The return value is 2 if the text is invisible but with an ellipsis
17938 and 1 if it's invisible and without an ellipsis. */
17939
17940 int
17941 invisible_p (propval, list)
17942 register Lisp_Object propval;
17943 Lisp_Object list;
17944 {
17945 register Lisp_Object tail, proptail;
17946
17947 for (tail = list; CONSP (tail); tail = XCDR (tail))
17948 {
17949 register Lisp_Object tem;
17950 tem = XCAR (tail);
17951 if (EQ (propval, tem))
17952 return 1;
17953 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17954 return NILP (XCDR (tem)) ? 1 : 2;
17955 }
17956
17957 if (CONSP (propval))
17958 {
17959 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17960 {
17961 Lisp_Object propelt;
17962 propelt = XCAR (proptail);
17963 for (tail = list; CONSP (tail); tail = XCDR (tail))
17964 {
17965 register Lisp_Object tem;
17966 tem = XCAR (tail);
17967 if (EQ (propelt, tem))
17968 return 1;
17969 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17970 return NILP (XCDR (tem)) ? 1 : 2;
17971 }
17972 }
17973 }
17974
17975 return 0;
17976 }
17977
17978 /* Calculate a width or height in pixels from a specification using
17979 the following elements:
17980
17981 SPEC ::=
17982 NUM - a (fractional) multiple of the default font width/height
17983 (NUM) - specifies exactly NUM pixels
17984 UNIT - a fixed number of pixels, see below.
17985 ELEMENT - size of a display element in pixels, see below.
17986 (NUM . SPEC) - equals NUM * SPEC
17987 (+ SPEC SPEC ...) - add pixel values
17988 (- SPEC SPEC ...) - subtract pixel values
17989 (- SPEC) - negate pixel value
17990
17991 NUM ::=
17992 INT or FLOAT - a number constant
17993 SYMBOL - use symbol's (buffer local) variable binding.
17994
17995 UNIT ::=
17996 in - pixels per inch *)
17997 mm - pixels per 1/1000 meter *)
17998 cm - pixels per 1/100 meter *)
17999 width - width of current font in pixels.
18000 height - height of current font in pixels.
18001
18002 *) using the ratio(s) defined in display-pixels-per-inch.
18003
18004 ELEMENT ::=
18005
18006 left-fringe - left fringe width in pixels
18007 right-fringe - right fringe width in pixels
18008
18009 left-margin - left margin width in pixels
18010 right-margin - right margin width in pixels
18011
18012 scroll-bar - scroll-bar area width in pixels
18013
18014 Examples:
18015
18016 Pixels corresponding to 5 inches:
18017 (5 . in)
18018
18019 Total width of non-text areas on left side of window (if scroll-bar is on left):
18020 '(space :width (+ left-fringe left-margin scroll-bar))
18021
18022 Align to first text column (in header line):
18023 '(space :align-to 0)
18024
18025 Align to middle of text area minus half the width of variable `my-image'
18026 containing a loaded image:
18027 '(space :align-to (0.5 . (- text my-image)))
18028
18029 Width of left margin minus width of 1 character in the default font:
18030 '(space :width (- left-margin 1))
18031
18032 Width of left margin minus width of 2 characters in the current font:
18033 '(space :width (- left-margin (2 . width)))
18034
18035 Center 1 character over left-margin (in header line):
18036 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18037
18038 Different ways to express width of left fringe plus left margin minus one pixel:
18039 '(space :width (- (+ left-fringe left-margin) (1)))
18040 '(space :width (+ left-fringe left-margin (- (1))))
18041 '(space :width (+ left-fringe left-margin (-1)))
18042
18043 */
18044
18045 #define NUMVAL(X) \
18046 ((INTEGERP (X) || FLOATP (X)) \
18047 ? XFLOATINT (X) \
18048 : - 1)
18049
18050 int
18051 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18052 double *res;
18053 struct it *it;
18054 Lisp_Object prop;
18055 void *font;
18056 int width_p, *align_to;
18057 {
18058 double pixels;
18059
18060 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18061 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18062
18063 if (NILP (prop))
18064 return OK_PIXELS (0);
18065
18066 xassert (FRAME_LIVE_P (it->f));
18067
18068 if (SYMBOLP (prop))
18069 {
18070 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18071 {
18072 char *unit = SDATA (SYMBOL_NAME (prop));
18073
18074 if (unit[0] == 'i' && unit[1] == 'n')
18075 pixels = 1.0;
18076 else if (unit[0] == 'm' && unit[1] == 'm')
18077 pixels = 25.4;
18078 else if (unit[0] == 'c' && unit[1] == 'm')
18079 pixels = 2.54;
18080 else
18081 pixels = 0;
18082 if (pixels > 0)
18083 {
18084 double ppi;
18085 #ifdef HAVE_WINDOW_SYSTEM
18086 if (FRAME_WINDOW_P (it->f)
18087 && (ppi = (width_p
18088 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18089 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18090 ppi > 0))
18091 return OK_PIXELS (ppi / pixels);
18092 #endif
18093
18094 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18095 || (CONSP (Vdisplay_pixels_per_inch)
18096 && (ppi = (width_p
18097 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18098 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18099 ppi > 0)))
18100 return OK_PIXELS (ppi / pixels);
18101
18102 return 0;
18103 }
18104 }
18105
18106 #ifdef HAVE_WINDOW_SYSTEM
18107 if (EQ (prop, Qheight))
18108 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18109 if (EQ (prop, Qwidth))
18110 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18111 #else
18112 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18113 return OK_PIXELS (1);
18114 #endif
18115
18116 if (EQ (prop, Qtext))
18117 return OK_PIXELS (width_p
18118 ? window_box_width (it->w, TEXT_AREA)
18119 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18120
18121 if (align_to && *align_to < 0)
18122 {
18123 *res = 0;
18124 if (EQ (prop, Qleft))
18125 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18126 if (EQ (prop, Qright))
18127 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18128 if (EQ (prop, Qcenter))
18129 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18130 + window_box_width (it->w, TEXT_AREA) / 2);
18131 if (EQ (prop, Qleft_fringe))
18132 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18133 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18134 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18135 if (EQ (prop, Qright_fringe))
18136 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18137 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18138 : window_box_right_offset (it->w, TEXT_AREA));
18139 if (EQ (prop, Qleft_margin))
18140 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18141 if (EQ (prop, Qright_margin))
18142 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18143 if (EQ (prop, Qscroll_bar))
18144 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18145 ? 0
18146 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18147 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18148 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18149 : 0)));
18150 }
18151 else
18152 {
18153 if (EQ (prop, Qleft_fringe))
18154 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18155 if (EQ (prop, Qright_fringe))
18156 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18157 if (EQ (prop, Qleft_margin))
18158 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18159 if (EQ (prop, Qright_margin))
18160 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18161 if (EQ (prop, Qscroll_bar))
18162 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18163 }
18164
18165 prop = Fbuffer_local_value (prop, it->w->buffer);
18166 }
18167
18168 if (INTEGERP (prop) || FLOATP (prop))
18169 {
18170 int base_unit = (width_p
18171 ? FRAME_COLUMN_WIDTH (it->f)
18172 : FRAME_LINE_HEIGHT (it->f));
18173 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18174 }
18175
18176 if (CONSP (prop))
18177 {
18178 Lisp_Object car = XCAR (prop);
18179 Lisp_Object cdr = XCDR (prop);
18180
18181 if (SYMBOLP (car))
18182 {
18183 #ifdef HAVE_WINDOW_SYSTEM
18184 if (FRAME_WINDOW_P (it->f)
18185 && valid_image_p (prop))
18186 {
18187 int id = lookup_image (it->f, prop);
18188 struct image *img = IMAGE_FROM_ID (it->f, id);
18189
18190 return OK_PIXELS (width_p ? img->width : img->height);
18191 }
18192 #endif
18193 if (EQ (car, Qplus) || EQ (car, Qminus))
18194 {
18195 int first = 1;
18196 double px;
18197
18198 pixels = 0;
18199 while (CONSP (cdr))
18200 {
18201 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18202 font, width_p, align_to))
18203 return 0;
18204 if (first)
18205 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18206 else
18207 pixels += px;
18208 cdr = XCDR (cdr);
18209 }
18210 if (EQ (car, Qminus))
18211 pixels = -pixels;
18212 return OK_PIXELS (pixels);
18213 }
18214
18215 car = Fbuffer_local_value (car, it->w->buffer);
18216 }
18217
18218 if (INTEGERP (car) || FLOATP (car))
18219 {
18220 double fact;
18221 pixels = XFLOATINT (car);
18222 if (NILP (cdr))
18223 return OK_PIXELS (pixels);
18224 if (calc_pixel_width_or_height (&fact, it, cdr,
18225 font, width_p, align_to))
18226 return OK_PIXELS (pixels * fact);
18227 return 0;
18228 }
18229
18230 return 0;
18231 }
18232
18233 return 0;
18234 }
18235
18236 \f
18237 /***********************************************************************
18238 Glyph Display
18239 ***********************************************************************/
18240
18241 #ifdef HAVE_WINDOW_SYSTEM
18242
18243 #if GLYPH_DEBUG
18244
18245 void
18246 dump_glyph_string (s)
18247 struct glyph_string *s;
18248 {
18249 fprintf (stderr, "glyph string\n");
18250 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18251 s->x, s->y, s->width, s->height);
18252 fprintf (stderr, " ybase = %d\n", s->ybase);
18253 fprintf (stderr, " hl = %d\n", s->hl);
18254 fprintf (stderr, " left overhang = %d, right = %d\n",
18255 s->left_overhang, s->right_overhang);
18256 fprintf (stderr, " nchars = %d\n", s->nchars);
18257 fprintf (stderr, " extends to end of line = %d\n",
18258 s->extends_to_end_of_line_p);
18259 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18260 fprintf (stderr, " bg width = %d\n", s->background_width);
18261 }
18262
18263 #endif /* GLYPH_DEBUG */
18264
18265 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18266 of XChar2b structures for S; it can't be allocated in
18267 init_glyph_string because it must be allocated via `alloca'. W
18268 is the window on which S is drawn. ROW and AREA are the glyph row
18269 and area within the row from which S is constructed. START is the
18270 index of the first glyph structure covered by S. HL is a
18271 face-override for drawing S. */
18272
18273 #ifdef HAVE_NTGUI
18274 #define OPTIONAL_HDC(hdc) hdc,
18275 #define DECLARE_HDC(hdc) HDC hdc;
18276 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18277 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18278 #endif
18279
18280 #ifndef OPTIONAL_HDC
18281 #define OPTIONAL_HDC(hdc)
18282 #define DECLARE_HDC(hdc)
18283 #define ALLOCATE_HDC(hdc, f)
18284 #define RELEASE_HDC(hdc, f)
18285 #endif
18286
18287 static void
18288 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18289 struct glyph_string *s;
18290 DECLARE_HDC (hdc)
18291 XChar2b *char2b;
18292 struct window *w;
18293 struct glyph_row *row;
18294 enum glyph_row_area area;
18295 int start;
18296 enum draw_glyphs_face hl;
18297 {
18298 bzero (s, sizeof *s);
18299 s->w = w;
18300 s->f = XFRAME (w->frame);
18301 #ifdef HAVE_NTGUI
18302 s->hdc = hdc;
18303 #endif
18304 s->display = FRAME_X_DISPLAY (s->f);
18305 s->window = FRAME_X_WINDOW (s->f);
18306 s->char2b = char2b;
18307 s->hl = hl;
18308 s->row = row;
18309 s->area = area;
18310 s->first_glyph = row->glyphs[area] + start;
18311 s->height = row->height;
18312 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18313
18314 /* Display the internal border below the tool-bar window. */
18315 if (WINDOWP (s->f->tool_bar_window)
18316 && s->w == XWINDOW (s->f->tool_bar_window))
18317 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18318
18319 s->ybase = s->y + row->ascent;
18320 }
18321
18322
18323 /* Append the list of glyph strings with head H and tail T to the list
18324 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18325
18326 static INLINE void
18327 append_glyph_string_lists (head, tail, h, t)
18328 struct glyph_string **head, **tail;
18329 struct glyph_string *h, *t;
18330 {
18331 if (h)
18332 {
18333 if (*head)
18334 (*tail)->next = h;
18335 else
18336 *head = h;
18337 h->prev = *tail;
18338 *tail = t;
18339 }
18340 }
18341
18342
18343 /* Prepend the list of glyph strings with head H and tail T to the
18344 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18345 result. */
18346
18347 static INLINE void
18348 prepend_glyph_string_lists (head, tail, h, t)
18349 struct glyph_string **head, **tail;
18350 struct glyph_string *h, *t;
18351 {
18352 if (h)
18353 {
18354 if (*head)
18355 (*head)->prev = t;
18356 else
18357 *tail = t;
18358 t->next = *head;
18359 *head = h;
18360 }
18361 }
18362
18363
18364 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18365 Set *HEAD and *TAIL to the resulting list. */
18366
18367 static INLINE void
18368 append_glyph_string (head, tail, s)
18369 struct glyph_string **head, **tail;
18370 struct glyph_string *s;
18371 {
18372 s->next = s->prev = NULL;
18373 append_glyph_string_lists (head, tail, s, s);
18374 }
18375
18376
18377 /* Get face and two-byte form of character glyph GLYPH on frame F.
18378 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18379 a pointer to a realized face that is ready for display. */
18380
18381 static INLINE struct face *
18382 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18383 struct frame *f;
18384 struct glyph *glyph;
18385 XChar2b *char2b;
18386 int *two_byte_p;
18387 {
18388 struct face *face;
18389
18390 xassert (glyph->type == CHAR_GLYPH);
18391 face = FACE_FROM_ID (f, glyph->face_id);
18392
18393 if (two_byte_p)
18394 *two_byte_p = 0;
18395
18396 if (!glyph->multibyte_p)
18397 {
18398 /* Unibyte case. We don't have to encode, but we have to make
18399 sure to use a face suitable for unibyte. */
18400 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18401 }
18402 else if (glyph->u.ch < 128
18403 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18404 {
18405 /* Case of ASCII in a face known to fit ASCII. */
18406 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18407 }
18408 else
18409 {
18410 int c1, c2, charset;
18411
18412 /* Split characters into bytes. If c2 is -1 afterwards, C is
18413 really a one-byte character so that byte1 is zero. */
18414 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18415 if (c2 > 0)
18416 STORE_XCHAR2B (char2b, c1, c2);
18417 else
18418 STORE_XCHAR2B (char2b, 0, c1);
18419
18420 /* Maybe encode the character in *CHAR2B. */
18421 if (charset != CHARSET_ASCII)
18422 {
18423 struct font_info *font_info
18424 = FONT_INFO_FROM_ID (f, face->font_info_id);
18425 if (font_info)
18426 glyph->font_type
18427 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18428 }
18429 }
18430
18431 /* Make sure X resources of the face are allocated. */
18432 xassert (face != NULL);
18433 PREPARE_FACE_FOR_DISPLAY (f, face);
18434 return face;
18435 }
18436
18437
18438 /* Fill glyph string S with composition components specified by S->cmp.
18439
18440 FACES is an array of faces for all components of this composition.
18441 S->gidx is the index of the first component for S.
18442
18443 OVERLAPS non-zero means S should draw the foreground only, and use
18444 its physical height for clipping. See also draw_glyphs.
18445
18446 Value is the index of a component not in S. */
18447
18448 static int
18449 fill_composite_glyph_string (s, faces, overlaps)
18450 struct glyph_string *s;
18451 struct face **faces;
18452 int overlaps;
18453 {
18454 int i;
18455
18456 xassert (s);
18457
18458 s->for_overlaps = overlaps;
18459
18460 s->face = faces[s->gidx];
18461 s->font = s->face->font;
18462 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18463
18464 /* For all glyphs of this composition, starting at the offset
18465 S->gidx, until we reach the end of the definition or encounter a
18466 glyph that requires the different face, add it to S. */
18467 ++s->nchars;
18468 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18469 ++s->nchars;
18470
18471 /* All glyph strings for the same composition has the same width,
18472 i.e. the width set for the first component of the composition. */
18473
18474 s->width = s->first_glyph->pixel_width;
18475
18476 /* If the specified font could not be loaded, use the frame's
18477 default font, but record the fact that we couldn't load it in
18478 the glyph string so that we can draw rectangles for the
18479 characters of the glyph string. */
18480 if (s->font == NULL)
18481 {
18482 s->font_not_found_p = 1;
18483 s->font = FRAME_FONT (s->f);
18484 }
18485
18486 /* Adjust base line for subscript/superscript text. */
18487 s->ybase += s->first_glyph->voffset;
18488
18489 xassert (s->face && s->face->gc);
18490
18491 /* This glyph string must always be drawn with 16-bit functions. */
18492 s->two_byte_p = 1;
18493
18494 return s->gidx + s->nchars;
18495 }
18496
18497
18498 /* Fill glyph string S from a sequence of character glyphs.
18499
18500 FACE_ID is the face id of the string. START is the index of the
18501 first glyph to consider, END is the index of the last + 1.
18502 OVERLAPS non-zero means S should draw the foreground only, and use
18503 its physical height for clipping. See also draw_glyphs.
18504
18505 Value is the index of the first glyph not in S. */
18506
18507 static int
18508 fill_glyph_string (s, face_id, start, end, overlaps)
18509 struct glyph_string *s;
18510 int face_id;
18511 int start, end, overlaps;
18512 {
18513 struct glyph *glyph, *last;
18514 int voffset;
18515 int glyph_not_available_p;
18516
18517 xassert (s->f == XFRAME (s->w->frame));
18518 xassert (s->nchars == 0);
18519 xassert (start >= 0 && end > start);
18520
18521 s->for_overlaps = overlaps,
18522 glyph = s->row->glyphs[s->area] + start;
18523 last = s->row->glyphs[s->area] + end;
18524 voffset = glyph->voffset;
18525
18526 glyph_not_available_p = glyph->glyph_not_available_p;
18527
18528 while (glyph < last
18529 && glyph->type == CHAR_GLYPH
18530 && glyph->voffset == voffset
18531 /* Same face id implies same font, nowadays. */
18532 && glyph->face_id == face_id
18533 && glyph->glyph_not_available_p == glyph_not_available_p)
18534 {
18535 int two_byte_p;
18536
18537 s->face = get_glyph_face_and_encoding (s->f, glyph,
18538 s->char2b + s->nchars,
18539 &two_byte_p);
18540 s->two_byte_p = two_byte_p;
18541 ++s->nchars;
18542 xassert (s->nchars <= end - start);
18543 s->width += glyph->pixel_width;
18544 ++glyph;
18545 }
18546
18547 s->font = s->face->font;
18548 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18549
18550 /* If the specified font could not be loaded, use the frame's font,
18551 but record the fact that we couldn't load it in
18552 S->font_not_found_p so that we can draw rectangles for the
18553 characters of the glyph string. */
18554 if (s->font == NULL || glyph_not_available_p)
18555 {
18556 s->font_not_found_p = 1;
18557 s->font = FRAME_FONT (s->f);
18558 }
18559
18560 /* Adjust base line for subscript/superscript text. */
18561 s->ybase += voffset;
18562
18563 xassert (s->face && s->face->gc);
18564 return glyph - s->row->glyphs[s->area];
18565 }
18566
18567
18568 /* Fill glyph string S from image glyph S->first_glyph. */
18569
18570 static void
18571 fill_image_glyph_string (s)
18572 struct glyph_string *s;
18573 {
18574 xassert (s->first_glyph->type == IMAGE_GLYPH);
18575 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18576 xassert (s->img);
18577 s->slice = s->first_glyph->slice;
18578 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18579 s->font = s->face->font;
18580 s->width = s->first_glyph->pixel_width;
18581
18582 /* Adjust base line for subscript/superscript text. */
18583 s->ybase += s->first_glyph->voffset;
18584 }
18585
18586
18587 /* Fill glyph string S from a sequence of stretch glyphs.
18588
18589 ROW is the glyph row in which the glyphs are found, AREA is the
18590 area within the row. START is the index of the first glyph to
18591 consider, END is the index of the last + 1.
18592
18593 Value is the index of the first glyph not in S. */
18594
18595 static int
18596 fill_stretch_glyph_string (s, row, area, start, end)
18597 struct glyph_string *s;
18598 struct glyph_row *row;
18599 enum glyph_row_area area;
18600 int start, end;
18601 {
18602 struct glyph *glyph, *last;
18603 int voffset, face_id;
18604
18605 xassert (s->first_glyph->type == STRETCH_GLYPH);
18606
18607 glyph = s->row->glyphs[s->area] + start;
18608 last = s->row->glyphs[s->area] + end;
18609 face_id = glyph->face_id;
18610 s->face = FACE_FROM_ID (s->f, face_id);
18611 s->font = s->face->font;
18612 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18613 s->width = glyph->pixel_width;
18614 voffset = glyph->voffset;
18615
18616 for (++glyph;
18617 (glyph < last
18618 && glyph->type == STRETCH_GLYPH
18619 && glyph->voffset == voffset
18620 && glyph->face_id == face_id);
18621 ++glyph)
18622 s->width += glyph->pixel_width;
18623
18624 /* Adjust base line for subscript/superscript text. */
18625 s->ybase += voffset;
18626
18627 /* The case that face->gc == 0 is handled when drawing the glyph
18628 string by calling PREPARE_FACE_FOR_DISPLAY. */
18629 xassert (s->face);
18630 return glyph - s->row->glyphs[s->area];
18631 }
18632
18633
18634 /* EXPORT for RIF:
18635 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18636 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18637 assumed to be zero. */
18638
18639 void
18640 x_get_glyph_overhangs (glyph, f, left, right)
18641 struct glyph *glyph;
18642 struct frame *f;
18643 int *left, *right;
18644 {
18645 *left = *right = 0;
18646
18647 if (glyph->type == CHAR_GLYPH)
18648 {
18649 XFontStruct *font;
18650 struct face *face;
18651 struct font_info *font_info;
18652 XChar2b char2b;
18653 XCharStruct *pcm;
18654
18655 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18656 font = face->font;
18657 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18658 if (font /* ++KFS: Should this be font_info ? */
18659 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
18660 {
18661 if (pcm->rbearing > pcm->width)
18662 *right = pcm->rbearing - pcm->width;
18663 if (pcm->lbearing < 0)
18664 *left = -pcm->lbearing;
18665 }
18666 }
18667 }
18668
18669
18670 /* Return the index of the first glyph preceding glyph string S that
18671 is overwritten by S because of S's left overhang. Value is -1
18672 if no glyphs are overwritten. */
18673
18674 static int
18675 left_overwritten (s)
18676 struct glyph_string *s;
18677 {
18678 int k;
18679
18680 if (s->left_overhang)
18681 {
18682 int x = 0, i;
18683 struct glyph *glyphs = s->row->glyphs[s->area];
18684 int first = s->first_glyph - glyphs;
18685
18686 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18687 x -= glyphs[i].pixel_width;
18688
18689 k = i + 1;
18690 }
18691 else
18692 k = -1;
18693
18694 return k;
18695 }
18696
18697
18698 /* Return the index of the first glyph preceding glyph string S that
18699 is overwriting S because of its right overhang. Value is -1 if no
18700 glyph in front of S overwrites S. */
18701
18702 static int
18703 left_overwriting (s)
18704 struct glyph_string *s;
18705 {
18706 int i, k, x;
18707 struct glyph *glyphs = s->row->glyphs[s->area];
18708 int first = s->first_glyph - glyphs;
18709
18710 k = -1;
18711 x = 0;
18712 for (i = first - 1; i >= 0; --i)
18713 {
18714 int left, right;
18715 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18716 if (x + right > 0)
18717 k = i;
18718 x -= glyphs[i].pixel_width;
18719 }
18720
18721 return k;
18722 }
18723
18724
18725 /* Return the index of the last glyph following glyph string S that is
18726 not overwritten by S because of S's right overhang. Value is -1 if
18727 no such glyph is found. */
18728
18729 static int
18730 right_overwritten (s)
18731 struct glyph_string *s;
18732 {
18733 int k = -1;
18734
18735 if (s->right_overhang)
18736 {
18737 int x = 0, i;
18738 struct glyph *glyphs = s->row->glyphs[s->area];
18739 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18740 int end = s->row->used[s->area];
18741
18742 for (i = first; i < end && s->right_overhang > x; ++i)
18743 x += glyphs[i].pixel_width;
18744
18745 k = i;
18746 }
18747
18748 return k;
18749 }
18750
18751
18752 /* Return the index of the last glyph following glyph string S that
18753 overwrites S because of its left overhang. Value is negative
18754 if no such glyph is found. */
18755
18756 static int
18757 right_overwriting (s)
18758 struct glyph_string *s;
18759 {
18760 int i, k, x;
18761 int end = s->row->used[s->area];
18762 struct glyph *glyphs = s->row->glyphs[s->area];
18763 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18764
18765 k = -1;
18766 x = 0;
18767 for (i = first; i < end; ++i)
18768 {
18769 int left, right;
18770 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18771 if (x - left < 0)
18772 k = i;
18773 x += glyphs[i].pixel_width;
18774 }
18775
18776 return k;
18777 }
18778
18779
18780 /* Get face and two-byte form of character C in face FACE_ID on frame
18781 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18782 means we want to display multibyte text. DISPLAY_P non-zero means
18783 make sure that X resources for the face returned are allocated.
18784 Value is a pointer to a realized face that is ready for display if
18785 DISPLAY_P is non-zero. */
18786
18787 static INLINE struct face *
18788 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18789 struct frame *f;
18790 int c, face_id;
18791 XChar2b *char2b;
18792 int multibyte_p, display_p;
18793 {
18794 struct face *face = FACE_FROM_ID (f, face_id);
18795
18796 if (!multibyte_p)
18797 {
18798 /* Unibyte case. We don't have to encode, but we have to make
18799 sure to use a face suitable for unibyte. */
18800 STORE_XCHAR2B (char2b, 0, c);
18801 face_id = FACE_FOR_CHAR (f, face, c);
18802 face = FACE_FROM_ID (f, face_id);
18803 }
18804 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18805 {
18806 /* Case of ASCII in a face known to fit ASCII. */
18807 STORE_XCHAR2B (char2b, 0, c);
18808 }
18809 else
18810 {
18811 int c1, c2, charset;
18812
18813 /* Split characters into bytes. If c2 is -1 afterwards, C is
18814 really a one-byte character so that byte1 is zero. */
18815 SPLIT_CHAR (c, charset, c1, c2);
18816 if (c2 > 0)
18817 STORE_XCHAR2B (char2b, c1, c2);
18818 else
18819 STORE_XCHAR2B (char2b, 0, c1);
18820
18821 /* Maybe encode the character in *CHAR2B. */
18822 if (face->font != NULL)
18823 {
18824 struct font_info *font_info
18825 = FONT_INFO_FROM_ID (f, face->font_info_id);
18826 if (font_info)
18827 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
18828 }
18829 }
18830
18831 /* Make sure X resources of the face are allocated. */
18832 #ifdef HAVE_X_WINDOWS
18833 if (display_p)
18834 #endif
18835 {
18836 xassert (face != NULL);
18837 PREPARE_FACE_FOR_DISPLAY (f, face);
18838 }
18839
18840 return face;
18841 }
18842
18843
18844 /* Set background width of glyph string S. START is the index of the
18845 first glyph following S. LAST_X is the right-most x-position + 1
18846 in the drawing area. */
18847
18848 static INLINE void
18849 set_glyph_string_background_width (s, start, last_x)
18850 struct glyph_string *s;
18851 int start;
18852 int last_x;
18853 {
18854 /* If the face of this glyph string has to be drawn to the end of
18855 the drawing area, set S->extends_to_end_of_line_p. */
18856
18857 if (start == s->row->used[s->area]
18858 && s->area == TEXT_AREA
18859 && ((s->row->fill_line_p
18860 && (s->hl == DRAW_NORMAL_TEXT
18861 || s->hl == DRAW_IMAGE_RAISED
18862 || s->hl == DRAW_IMAGE_SUNKEN))
18863 || s->hl == DRAW_MOUSE_FACE))
18864 s->extends_to_end_of_line_p = 1;
18865
18866 /* If S extends its face to the end of the line, set its
18867 background_width to the distance to the right edge of the drawing
18868 area. */
18869 if (s->extends_to_end_of_line_p)
18870 s->background_width = last_x - s->x + 1;
18871 else
18872 s->background_width = s->width;
18873 }
18874
18875
18876 /* Compute overhangs and x-positions for glyph string S and its
18877 predecessors, or successors. X is the starting x-position for S.
18878 BACKWARD_P non-zero means process predecessors. */
18879
18880 static void
18881 compute_overhangs_and_x (s, x, backward_p)
18882 struct glyph_string *s;
18883 int x;
18884 int backward_p;
18885 {
18886 if (backward_p)
18887 {
18888 while (s)
18889 {
18890 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18891 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18892 x -= s->width;
18893 s->x = x;
18894 s = s->prev;
18895 }
18896 }
18897 else
18898 {
18899 while (s)
18900 {
18901 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18902 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18903 s->x = x;
18904 x += s->width;
18905 s = s->next;
18906 }
18907 }
18908 }
18909
18910
18911
18912 /* The following macros are only called from draw_glyphs below.
18913 They reference the following parameters of that function directly:
18914 `w', `row', `area', and `overlap_p'
18915 as well as the following local variables:
18916 `s', `f', and `hdc' (in W32) */
18917
18918 #ifdef HAVE_NTGUI
18919 /* On W32, silently add local `hdc' variable to argument list of
18920 init_glyph_string. */
18921 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18922 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18923 #else
18924 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18925 init_glyph_string (s, char2b, w, row, area, start, hl)
18926 #endif
18927
18928 /* Add a glyph string for a stretch glyph to the list of strings
18929 between HEAD and TAIL. START is the index of the stretch glyph in
18930 row area AREA of glyph row ROW. END is the index of the last glyph
18931 in that glyph row area. X is the current output position assigned
18932 to the new glyph string constructed. HL overrides that face of the
18933 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18934 is the right-most x-position of the drawing area. */
18935
18936 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18937 and below -- keep them on one line. */
18938 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18939 do \
18940 { \
18941 s = (struct glyph_string *) alloca (sizeof *s); \
18942 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18943 START = fill_stretch_glyph_string (s, row, area, START, END); \
18944 append_glyph_string (&HEAD, &TAIL, s); \
18945 s->x = (X); \
18946 } \
18947 while (0)
18948
18949
18950 /* Add a glyph string for an image glyph to the list of strings
18951 between HEAD and TAIL. START is the index of the image glyph in
18952 row area AREA of glyph row ROW. END is the index of the last glyph
18953 in that glyph row area. X is the current output position assigned
18954 to the new glyph string constructed. HL overrides that face of the
18955 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18956 is the right-most x-position of the drawing area. */
18957
18958 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18959 do \
18960 { \
18961 s = (struct glyph_string *) alloca (sizeof *s); \
18962 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18963 fill_image_glyph_string (s); \
18964 append_glyph_string (&HEAD, &TAIL, s); \
18965 ++START; \
18966 s->x = (X); \
18967 } \
18968 while (0)
18969
18970
18971 /* Add a glyph string for a sequence of character glyphs to the list
18972 of strings between HEAD and TAIL. START is the index of the first
18973 glyph in row area AREA of glyph row ROW that is part of the new
18974 glyph string. END is the index of the last glyph in that glyph row
18975 area. X is the current output position assigned to the new glyph
18976 string constructed. HL overrides that face of the glyph; e.g. it
18977 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18978 right-most x-position of the drawing area. */
18979
18980 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18981 do \
18982 { \
18983 int c, face_id; \
18984 XChar2b *char2b; \
18985 \
18986 c = (row)->glyphs[area][START].u.ch; \
18987 face_id = (row)->glyphs[area][START].face_id; \
18988 \
18989 s = (struct glyph_string *) alloca (sizeof *s); \
18990 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18991 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18992 append_glyph_string (&HEAD, &TAIL, s); \
18993 s->x = (X); \
18994 START = fill_glyph_string (s, face_id, START, END, overlaps); \
18995 } \
18996 while (0)
18997
18998
18999 /* Add a glyph string for a composite sequence to the list of strings
19000 between HEAD and TAIL. START is the index of the first glyph in
19001 row area AREA of glyph row ROW that is part of the new glyph
19002 string. END is the index of the last glyph in that glyph row area.
19003 X is the current output position assigned to the new glyph string
19004 constructed. HL overrides that face of the glyph; e.g. it is
19005 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19006 x-position of the drawing area. */
19007
19008 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19009 do { \
19010 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19011 int face_id = (row)->glyphs[area][START].face_id; \
19012 struct face *base_face = FACE_FROM_ID (f, face_id); \
19013 struct composition *cmp = composition_table[cmp_id]; \
19014 int glyph_len = cmp->glyph_len; \
19015 XChar2b *char2b; \
19016 struct face **faces; \
19017 struct glyph_string *first_s = NULL; \
19018 int n; \
19019 \
19020 base_face = base_face->ascii_face; \
19021 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19022 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19023 /* At first, fill in `char2b' and `faces'. */ \
19024 for (n = 0; n < glyph_len; n++) \
19025 { \
19026 int c = COMPOSITION_GLYPH (cmp, n); \
19027 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19028 faces[n] = FACE_FROM_ID (f, this_face_id); \
19029 get_char_face_and_encoding (f, c, this_face_id, \
19030 char2b + n, 1, 1); \
19031 } \
19032 \
19033 /* Make glyph_strings for each glyph sequence that is drawable by \
19034 the same face, and append them to HEAD/TAIL. */ \
19035 for (n = 0; n < cmp->glyph_len;) \
19036 { \
19037 s = (struct glyph_string *) alloca (sizeof *s); \
19038 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19039 append_glyph_string (&(HEAD), &(TAIL), s); \
19040 s->cmp = cmp; \
19041 s->gidx = n; \
19042 s->x = (X); \
19043 \
19044 if (n == 0) \
19045 first_s = s; \
19046 \
19047 n = fill_composite_glyph_string (s, faces, overlaps); \
19048 } \
19049 \
19050 ++START; \
19051 s = first_s; \
19052 } while (0)
19053
19054
19055 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19056 of AREA of glyph row ROW on window W between indices START and END.
19057 HL overrides the face for drawing glyph strings, e.g. it is
19058 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19059 x-positions of the drawing area.
19060
19061 This is an ugly monster macro construct because we must use alloca
19062 to allocate glyph strings (because draw_glyphs can be called
19063 asynchronously). */
19064
19065 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19066 do \
19067 { \
19068 HEAD = TAIL = NULL; \
19069 while (START < END) \
19070 { \
19071 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19072 switch (first_glyph->type) \
19073 { \
19074 case CHAR_GLYPH: \
19075 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19076 HL, X, LAST_X); \
19077 break; \
19078 \
19079 case COMPOSITE_GLYPH: \
19080 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19081 HL, X, LAST_X); \
19082 break; \
19083 \
19084 case STRETCH_GLYPH: \
19085 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19086 HL, X, LAST_X); \
19087 break; \
19088 \
19089 case IMAGE_GLYPH: \
19090 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19091 HL, X, LAST_X); \
19092 break; \
19093 \
19094 default: \
19095 abort (); \
19096 } \
19097 \
19098 set_glyph_string_background_width (s, START, LAST_X); \
19099 (X) += s->width; \
19100 } \
19101 } \
19102 while (0)
19103
19104
19105 /* Draw glyphs between START and END in AREA of ROW on window W,
19106 starting at x-position X. X is relative to AREA in W. HL is a
19107 face-override with the following meaning:
19108
19109 DRAW_NORMAL_TEXT draw normally
19110 DRAW_CURSOR draw in cursor face
19111 DRAW_MOUSE_FACE draw in mouse face.
19112 DRAW_INVERSE_VIDEO draw in mode line face
19113 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19114 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19115
19116 If OVERLAPS is non-zero, draw only the foreground of characters and
19117 clip to the physical height of ROW. Non-zero value also defines
19118 the overlapping part to be drawn:
19119
19120 OVERLAPS_PRED overlap with preceding rows
19121 OVERLAPS_SUCC overlap with succeeding rows
19122 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19123 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19124
19125 Value is the x-position reached, relative to AREA of W. */
19126
19127 static int
19128 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19129 struct window *w;
19130 int x;
19131 struct glyph_row *row;
19132 enum glyph_row_area area;
19133 int start, end;
19134 enum draw_glyphs_face hl;
19135 int overlaps;
19136 {
19137 struct glyph_string *head, *tail;
19138 struct glyph_string *s;
19139 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19140 int last_x, area_width;
19141 int x_reached;
19142 int i, j;
19143 struct frame *f = XFRAME (WINDOW_FRAME (w));
19144 DECLARE_HDC (hdc);
19145
19146 ALLOCATE_HDC (hdc, f);
19147
19148 /* Let's rather be paranoid than getting a SEGV. */
19149 end = min (end, row->used[area]);
19150 start = max (0, start);
19151 start = min (end, start);
19152
19153 /* Translate X to frame coordinates. Set last_x to the right
19154 end of the drawing area. */
19155 if (row->full_width_p)
19156 {
19157 /* X is relative to the left edge of W, without scroll bars
19158 or fringes. */
19159 x += WINDOW_LEFT_EDGE_X (w);
19160 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19161 }
19162 else
19163 {
19164 int area_left = window_box_left (w, area);
19165 x += area_left;
19166 area_width = window_box_width (w, area);
19167 last_x = area_left + area_width;
19168 }
19169
19170 /* Build a doubly-linked list of glyph_string structures between
19171 head and tail from what we have to draw. Note that the macro
19172 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19173 the reason we use a separate variable `i'. */
19174 i = start;
19175 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19176 if (tail)
19177 x_reached = tail->x + tail->background_width;
19178 else
19179 x_reached = x;
19180
19181 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19182 the row, redraw some glyphs in front or following the glyph
19183 strings built above. */
19184 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19185 {
19186 int dummy_x = 0;
19187 struct glyph_string *h, *t;
19188
19189 /* Compute overhangs for all glyph strings. */
19190 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19191 for (s = head; s; s = s->next)
19192 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19193
19194 /* Prepend glyph strings for glyphs in front of the first glyph
19195 string that are overwritten because of the first glyph
19196 string's left overhang. The background of all strings
19197 prepended must be drawn because the first glyph string
19198 draws over it. */
19199 i = left_overwritten (head);
19200 if (i >= 0)
19201 {
19202 j = i;
19203 BUILD_GLYPH_STRINGS (j, start, h, t,
19204 DRAW_NORMAL_TEXT, dummy_x, last_x);
19205 start = i;
19206 compute_overhangs_and_x (t, head->x, 1);
19207 prepend_glyph_string_lists (&head, &tail, h, t);
19208 clip_head = head;
19209 }
19210
19211 /* Prepend glyph strings for glyphs in front of the first glyph
19212 string that overwrite that glyph string because of their
19213 right overhang. For these strings, only the foreground must
19214 be drawn, because it draws over the glyph string at `head'.
19215 The background must not be drawn because this would overwrite
19216 right overhangs of preceding glyphs for which no glyph
19217 strings exist. */
19218 i = left_overwriting (head);
19219 if (i >= 0)
19220 {
19221 clip_head = head;
19222 BUILD_GLYPH_STRINGS (i, start, h, t,
19223 DRAW_NORMAL_TEXT, dummy_x, last_x);
19224 for (s = h; s; s = s->next)
19225 s->background_filled_p = 1;
19226 compute_overhangs_and_x (t, head->x, 1);
19227 prepend_glyph_string_lists (&head, &tail, h, t);
19228 }
19229
19230 /* Append glyphs strings for glyphs following the last glyph
19231 string tail that are overwritten by tail. The background of
19232 these strings has to be drawn because tail's foreground draws
19233 over it. */
19234 i = right_overwritten (tail);
19235 if (i >= 0)
19236 {
19237 BUILD_GLYPH_STRINGS (end, i, h, t,
19238 DRAW_NORMAL_TEXT, x, last_x);
19239 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19240 append_glyph_string_lists (&head, &tail, h, t);
19241 clip_tail = tail;
19242 }
19243
19244 /* Append glyph strings for glyphs following the last glyph
19245 string tail that overwrite tail. The foreground of such
19246 glyphs has to be drawn because it writes into the background
19247 of tail. The background must not be drawn because it could
19248 paint over the foreground of following glyphs. */
19249 i = right_overwriting (tail);
19250 if (i >= 0)
19251 {
19252 clip_tail = tail;
19253 BUILD_GLYPH_STRINGS (end, i, h, t,
19254 DRAW_NORMAL_TEXT, x, last_x);
19255 for (s = h; s; s = s->next)
19256 s->background_filled_p = 1;
19257 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19258 append_glyph_string_lists (&head, &tail, h, t);
19259 }
19260 if (clip_head || clip_tail)
19261 for (s = head; s; s = s->next)
19262 {
19263 s->clip_head = clip_head;
19264 s->clip_tail = clip_tail;
19265 }
19266 }
19267
19268 /* Draw all strings. */
19269 for (s = head; s; s = s->next)
19270 FRAME_RIF (f)->draw_glyph_string (s);
19271
19272 if (area == TEXT_AREA
19273 && !row->full_width_p
19274 /* When drawing overlapping rows, only the glyph strings'
19275 foreground is drawn, which doesn't erase a cursor
19276 completely. */
19277 && !overlaps)
19278 {
19279 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19280 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19281 : (tail ? tail->x + tail->background_width : x));
19282
19283 int text_left = window_box_left (w, TEXT_AREA);
19284 x0 -= text_left;
19285 x1 -= text_left;
19286
19287 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19288 row->y, MATRIX_ROW_BOTTOM_Y (row));
19289 }
19290
19291 /* Value is the x-position up to which drawn, relative to AREA of W.
19292 This doesn't include parts drawn because of overhangs. */
19293 if (row->full_width_p)
19294 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19295 else
19296 x_reached -= window_box_left (w, area);
19297
19298 RELEASE_HDC (hdc, f);
19299
19300 return x_reached;
19301 }
19302
19303 /* Expand row matrix if too narrow. Don't expand if area
19304 is not present. */
19305
19306 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19307 { \
19308 if (!fonts_changed_p \
19309 && (it->glyph_row->glyphs[area] \
19310 < it->glyph_row->glyphs[area + 1])) \
19311 { \
19312 it->w->ncols_scale_factor++; \
19313 fonts_changed_p = 1; \
19314 } \
19315 }
19316
19317 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19318 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19319
19320 static INLINE void
19321 append_glyph (it)
19322 struct it *it;
19323 {
19324 struct glyph *glyph;
19325 enum glyph_row_area area = it->area;
19326
19327 xassert (it->glyph_row);
19328 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19329
19330 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19331 if (glyph < it->glyph_row->glyphs[area + 1])
19332 {
19333 glyph->charpos = CHARPOS (it->position);
19334 glyph->object = it->object;
19335 glyph->pixel_width = it->pixel_width;
19336 glyph->ascent = it->ascent;
19337 glyph->descent = it->descent;
19338 glyph->voffset = it->voffset;
19339 glyph->type = CHAR_GLYPH;
19340 glyph->multibyte_p = it->multibyte_p;
19341 glyph->left_box_line_p = it->start_of_box_run_p;
19342 glyph->right_box_line_p = it->end_of_box_run_p;
19343 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19344 || it->phys_descent > it->descent);
19345 glyph->padding_p = 0;
19346 glyph->glyph_not_available_p = it->glyph_not_available_p;
19347 glyph->face_id = it->face_id;
19348 glyph->u.ch = it->char_to_display;
19349 glyph->slice = null_glyph_slice;
19350 glyph->font_type = FONT_TYPE_UNKNOWN;
19351 ++it->glyph_row->used[area];
19352 }
19353 else
19354 IT_EXPAND_MATRIX_WIDTH (it, area);
19355 }
19356
19357 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19358 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19359
19360 static INLINE void
19361 append_composite_glyph (it)
19362 struct it *it;
19363 {
19364 struct glyph *glyph;
19365 enum glyph_row_area area = it->area;
19366
19367 xassert (it->glyph_row);
19368
19369 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19370 if (glyph < it->glyph_row->glyphs[area + 1])
19371 {
19372 glyph->charpos = CHARPOS (it->position);
19373 glyph->object = it->object;
19374 glyph->pixel_width = it->pixel_width;
19375 glyph->ascent = it->ascent;
19376 glyph->descent = it->descent;
19377 glyph->voffset = it->voffset;
19378 glyph->type = COMPOSITE_GLYPH;
19379 glyph->multibyte_p = it->multibyte_p;
19380 glyph->left_box_line_p = it->start_of_box_run_p;
19381 glyph->right_box_line_p = it->end_of_box_run_p;
19382 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19383 || it->phys_descent > it->descent);
19384 glyph->padding_p = 0;
19385 glyph->glyph_not_available_p = 0;
19386 glyph->face_id = it->face_id;
19387 glyph->u.cmp_id = it->cmp_id;
19388 glyph->slice = null_glyph_slice;
19389 glyph->font_type = FONT_TYPE_UNKNOWN;
19390 ++it->glyph_row->used[area];
19391 }
19392 else
19393 IT_EXPAND_MATRIX_WIDTH (it, area);
19394 }
19395
19396
19397 /* Change IT->ascent and IT->height according to the setting of
19398 IT->voffset. */
19399
19400 static INLINE void
19401 take_vertical_position_into_account (it)
19402 struct it *it;
19403 {
19404 if (it->voffset)
19405 {
19406 if (it->voffset < 0)
19407 /* Increase the ascent so that we can display the text higher
19408 in the line. */
19409 it->ascent -= it->voffset;
19410 else
19411 /* Increase the descent so that we can display the text lower
19412 in the line. */
19413 it->descent += it->voffset;
19414 }
19415 }
19416
19417
19418 /* Produce glyphs/get display metrics for the image IT is loaded with.
19419 See the description of struct display_iterator in dispextern.h for
19420 an overview of struct display_iterator. */
19421
19422 static void
19423 produce_image_glyph (it)
19424 struct it *it;
19425 {
19426 struct image *img;
19427 struct face *face;
19428 int glyph_ascent;
19429 struct glyph_slice slice;
19430
19431 xassert (it->what == IT_IMAGE);
19432
19433 face = FACE_FROM_ID (it->f, it->face_id);
19434 xassert (face);
19435 /* Make sure X resources of the face is loaded. */
19436 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19437
19438 if (it->image_id < 0)
19439 {
19440 /* Fringe bitmap. */
19441 it->ascent = it->phys_ascent = 0;
19442 it->descent = it->phys_descent = 0;
19443 it->pixel_width = 0;
19444 it->nglyphs = 0;
19445 return;
19446 }
19447
19448 img = IMAGE_FROM_ID (it->f, it->image_id);
19449 xassert (img);
19450 /* Make sure X resources of the image is loaded. */
19451 prepare_image_for_display (it->f, img);
19452
19453 slice.x = slice.y = 0;
19454 slice.width = img->width;
19455 slice.height = img->height;
19456
19457 if (INTEGERP (it->slice.x))
19458 slice.x = XINT (it->slice.x);
19459 else if (FLOATP (it->slice.x))
19460 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19461
19462 if (INTEGERP (it->slice.y))
19463 slice.y = XINT (it->slice.y);
19464 else if (FLOATP (it->slice.y))
19465 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19466
19467 if (INTEGERP (it->slice.width))
19468 slice.width = XINT (it->slice.width);
19469 else if (FLOATP (it->slice.width))
19470 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19471
19472 if (INTEGERP (it->slice.height))
19473 slice.height = XINT (it->slice.height);
19474 else if (FLOATP (it->slice.height))
19475 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19476
19477 if (slice.x >= img->width)
19478 slice.x = img->width;
19479 if (slice.y >= img->height)
19480 slice.y = img->height;
19481 if (slice.x + slice.width >= img->width)
19482 slice.width = img->width - slice.x;
19483 if (slice.y + slice.height > img->height)
19484 slice.height = img->height - slice.y;
19485
19486 if (slice.width == 0 || slice.height == 0)
19487 return;
19488
19489 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19490
19491 it->descent = slice.height - glyph_ascent;
19492 if (slice.y == 0)
19493 it->descent += img->vmargin;
19494 if (slice.y + slice.height == img->height)
19495 it->descent += img->vmargin;
19496 it->phys_descent = it->descent;
19497
19498 it->pixel_width = slice.width;
19499 if (slice.x == 0)
19500 it->pixel_width += img->hmargin;
19501 if (slice.x + slice.width == img->width)
19502 it->pixel_width += img->hmargin;
19503
19504 /* It's quite possible for images to have an ascent greater than
19505 their height, so don't get confused in that case. */
19506 if (it->descent < 0)
19507 it->descent = 0;
19508
19509 #if 0 /* this breaks image tiling */
19510 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19511 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19512 if (face_ascent > it->ascent)
19513 it->ascent = it->phys_ascent = face_ascent;
19514 #endif
19515
19516 it->nglyphs = 1;
19517
19518 if (face->box != FACE_NO_BOX)
19519 {
19520 if (face->box_line_width > 0)
19521 {
19522 if (slice.y == 0)
19523 it->ascent += face->box_line_width;
19524 if (slice.y + slice.height == img->height)
19525 it->descent += face->box_line_width;
19526 }
19527
19528 if (it->start_of_box_run_p && slice.x == 0)
19529 it->pixel_width += abs (face->box_line_width);
19530 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19531 it->pixel_width += abs (face->box_line_width);
19532 }
19533
19534 take_vertical_position_into_account (it);
19535
19536 if (it->glyph_row)
19537 {
19538 struct glyph *glyph;
19539 enum glyph_row_area area = it->area;
19540
19541 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19542 if (glyph < it->glyph_row->glyphs[area + 1])
19543 {
19544 glyph->charpos = CHARPOS (it->position);
19545 glyph->object = it->object;
19546 glyph->pixel_width = it->pixel_width;
19547 glyph->ascent = glyph_ascent;
19548 glyph->descent = it->descent;
19549 glyph->voffset = it->voffset;
19550 glyph->type = IMAGE_GLYPH;
19551 glyph->multibyte_p = it->multibyte_p;
19552 glyph->left_box_line_p = it->start_of_box_run_p;
19553 glyph->right_box_line_p = it->end_of_box_run_p;
19554 glyph->overlaps_vertically_p = 0;
19555 glyph->padding_p = 0;
19556 glyph->glyph_not_available_p = 0;
19557 glyph->face_id = it->face_id;
19558 glyph->u.img_id = img->id;
19559 glyph->slice = slice;
19560 glyph->font_type = FONT_TYPE_UNKNOWN;
19561 ++it->glyph_row->used[area];
19562 }
19563 else
19564 IT_EXPAND_MATRIX_WIDTH (it, area);
19565 }
19566 }
19567
19568
19569 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19570 of the glyph, WIDTH and HEIGHT are the width and height of the
19571 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19572
19573 static void
19574 append_stretch_glyph (it, object, width, height, ascent)
19575 struct it *it;
19576 Lisp_Object object;
19577 int width, height;
19578 int ascent;
19579 {
19580 struct glyph *glyph;
19581 enum glyph_row_area area = it->area;
19582
19583 xassert (ascent >= 0 && ascent <= height);
19584
19585 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19586 if (glyph < it->glyph_row->glyphs[area + 1])
19587 {
19588 glyph->charpos = CHARPOS (it->position);
19589 glyph->object = object;
19590 glyph->pixel_width = width;
19591 glyph->ascent = ascent;
19592 glyph->descent = height - ascent;
19593 glyph->voffset = it->voffset;
19594 glyph->type = STRETCH_GLYPH;
19595 glyph->multibyte_p = it->multibyte_p;
19596 glyph->left_box_line_p = it->start_of_box_run_p;
19597 glyph->right_box_line_p = it->end_of_box_run_p;
19598 glyph->overlaps_vertically_p = 0;
19599 glyph->padding_p = 0;
19600 glyph->glyph_not_available_p = 0;
19601 glyph->face_id = it->face_id;
19602 glyph->u.stretch.ascent = ascent;
19603 glyph->u.stretch.height = height;
19604 glyph->slice = null_glyph_slice;
19605 glyph->font_type = FONT_TYPE_UNKNOWN;
19606 ++it->glyph_row->used[area];
19607 }
19608 else
19609 IT_EXPAND_MATRIX_WIDTH (it, area);
19610 }
19611
19612
19613 /* Produce a stretch glyph for iterator IT. IT->object is the value
19614 of the glyph property displayed. The value must be a list
19615 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19616 being recognized:
19617
19618 1. `:width WIDTH' specifies that the space should be WIDTH *
19619 canonical char width wide. WIDTH may be an integer or floating
19620 point number.
19621
19622 2. `:relative-width FACTOR' specifies that the width of the stretch
19623 should be computed from the width of the first character having the
19624 `glyph' property, and should be FACTOR times that width.
19625
19626 3. `:align-to HPOS' specifies that the space should be wide enough
19627 to reach HPOS, a value in canonical character units.
19628
19629 Exactly one of the above pairs must be present.
19630
19631 4. `:height HEIGHT' specifies that the height of the stretch produced
19632 should be HEIGHT, measured in canonical character units.
19633
19634 5. `:relative-height FACTOR' specifies that the height of the
19635 stretch should be FACTOR times the height of the characters having
19636 the glyph property.
19637
19638 Either none or exactly one of 4 or 5 must be present.
19639
19640 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19641 of the stretch should be used for the ascent of the stretch.
19642 ASCENT must be in the range 0 <= ASCENT <= 100. */
19643
19644 static void
19645 produce_stretch_glyph (it)
19646 struct it *it;
19647 {
19648 /* (space :width WIDTH :height HEIGHT ...) */
19649 Lisp_Object prop, plist;
19650 int width = 0, height = 0, align_to = -1;
19651 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19652 int ascent = 0;
19653 double tem;
19654 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19655 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19656
19657 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19658
19659 /* List should start with `space'. */
19660 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19661 plist = XCDR (it->object);
19662
19663 /* Compute the width of the stretch. */
19664 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19665 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19666 {
19667 /* Absolute width `:width WIDTH' specified and valid. */
19668 zero_width_ok_p = 1;
19669 width = (int)tem;
19670 }
19671 else if (prop = Fplist_get (plist, QCrelative_width),
19672 NUMVAL (prop) > 0)
19673 {
19674 /* Relative width `:relative-width FACTOR' specified and valid.
19675 Compute the width of the characters having the `glyph'
19676 property. */
19677 struct it it2;
19678 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19679
19680 it2 = *it;
19681 if (it->multibyte_p)
19682 {
19683 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19684 - IT_BYTEPOS (*it));
19685 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19686 }
19687 else
19688 it2.c = *p, it2.len = 1;
19689
19690 it2.glyph_row = NULL;
19691 it2.what = IT_CHARACTER;
19692 x_produce_glyphs (&it2);
19693 width = NUMVAL (prop) * it2.pixel_width;
19694 }
19695 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19696 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19697 {
19698 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19699 align_to = (align_to < 0
19700 ? 0
19701 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19702 else if (align_to < 0)
19703 align_to = window_box_left_offset (it->w, TEXT_AREA);
19704 width = max (0, (int)tem + align_to - it->current_x);
19705 zero_width_ok_p = 1;
19706 }
19707 else
19708 /* Nothing specified -> width defaults to canonical char width. */
19709 width = FRAME_COLUMN_WIDTH (it->f);
19710
19711 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19712 width = 1;
19713
19714 /* Compute height. */
19715 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19716 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19717 {
19718 height = (int)tem;
19719 zero_height_ok_p = 1;
19720 }
19721 else if (prop = Fplist_get (plist, QCrelative_height),
19722 NUMVAL (prop) > 0)
19723 height = FONT_HEIGHT (font) * NUMVAL (prop);
19724 else
19725 height = FONT_HEIGHT (font);
19726
19727 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19728 height = 1;
19729
19730 /* Compute percentage of height used for ascent. If
19731 `:ascent ASCENT' is present and valid, use that. Otherwise,
19732 derive the ascent from the font in use. */
19733 if (prop = Fplist_get (plist, QCascent),
19734 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19735 ascent = height * NUMVAL (prop) / 100.0;
19736 else if (!NILP (prop)
19737 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19738 ascent = min (max (0, (int)tem), height);
19739 else
19740 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19741
19742 if (width > 0 && !it->truncate_lines_p
19743 && it->current_x + width > it->last_visible_x)
19744 width = it->last_visible_x - it->current_x - 1;
19745
19746 if (width > 0 && height > 0 && it->glyph_row)
19747 {
19748 Lisp_Object object = it->stack[it->sp - 1].string;
19749 if (!STRINGP (object))
19750 object = it->w->buffer;
19751 append_stretch_glyph (it, object, width, height, ascent);
19752 }
19753
19754 it->pixel_width = width;
19755 it->ascent = it->phys_ascent = ascent;
19756 it->descent = it->phys_descent = height - it->ascent;
19757 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19758
19759 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19760 {
19761 if (face->box_line_width > 0)
19762 {
19763 it->ascent += face->box_line_width;
19764 it->descent += face->box_line_width;
19765 }
19766
19767 if (it->start_of_box_run_p)
19768 it->pixel_width += abs (face->box_line_width);
19769 if (it->end_of_box_run_p)
19770 it->pixel_width += abs (face->box_line_width);
19771 }
19772
19773 take_vertical_position_into_account (it);
19774 }
19775
19776 /* Get line-height and line-spacing property at point.
19777 If line-height has format (HEIGHT TOTAL), return TOTAL
19778 in TOTAL_HEIGHT. */
19779
19780 static Lisp_Object
19781 get_line_height_property (it, prop)
19782 struct it *it;
19783 Lisp_Object prop;
19784 {
19785 Lisp_Object position;
19786
19787 if (STRINGP (it->object))
19788 position = make_number (IT_STRING_CHARPOS (*it));
19789 else if (BUFFERP (it->object))
19790 position = make_number (IT_CHARPOS (*it));
19791 else
19792 return Qnil;
19793
19794 return Fget_char_property (position, prop, it->object);
19795 }
19796
19797 /* Calculate line-height and line-spacing properties.
19798 An integer value specifies explicit pixel value.
19799 A float value specifies relative value to current face height.
19800 A cons (float . face-name) specifies relative value to
19801 height of specified face font.
19802
19803 Returns height in pixels, or nil. */
19804
19805
19806 static Lisp_Object
19807 calc_line_height_property (it, val, font, boff, override)
19808 struct it *it;
19809 Lisp_Object val;
19810 XFontStruct *font;
19811 int boff, override;
19812 {
19813 Lisp_Object face_name = Qnil;
19814 int ascent, descent, height;
19815
19816 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19817 return val;
19818
19819 if (CONSP (val))
19820 {
19821 face_name = XCAR (val);
19822 val = XCDR (val);
19823 if (!NUMBERP (val))
19824 val = make_number (1);
19825 if (NILP (face_name))
19826 {
19827 height = it->ascent + it->descent;
19828 goto scale;
19829 }
19830 }
19831
19832 if (NILP (face_name))
19833 {
19834 font = FRAME_FONT (it->f);
19835 boff = FRAME_BASELINE_OFFSET (it->f);
19836 }
19837 else if (EQ (face_name, Qt))
19838 {
19839 override = 0;
19840 }
19841 else
19842 {
19843 int face_id;
19844 struct face *face;
19845 struct font_info *font_info;
19846
19847 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19848 if (face_id < 0)
19849 return make_number (-1);
19850
19851 face = FACE_FROM_ID (it->f, face_id);
19852 font = face->font;
19853 if (font == NULL)
19854 return make_number (-1);
19855
19856 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19857 boff = font_info->baseline_offset;
19858 if (font_info->vertical_centering)
19859 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19860 }
19861
19862 ascent = FONT_BASE (font) + boff;
19863 descent = FONT_DESCENT (font) - boff;
19864
19865 if (override)
19866 {
19867 it->override_ascent = ascent;
19868 it->override_descent = descent;
19869 it->override_boff = boff;
19870 }
19871
19872 height = ascent + descent;
19873
19874 scale:
19875 if (FLOATP (val))
19876 height = (int)(XFLOAT_DATA (val) * height);
19877 else if (INTEGERP (val))
19878 height *= XINT (val);
19879
19880 return make_number (height);
19881 }
19882
19883
19884 /* RIF:
19885 Produce glyphs/get display metrics for the display element IT is
19886 loaded with. See the description of struct it in dispextern.h
19887 for an overview of struct it. */
19888
19889 void
19890 x_produce_glyphs (it)
19891 struct it *it;
19892 {
19893 int extra_line_spacing = it->extra_line_spacing;
19894
19895 it->glyph_not_available_p = 0;
19896
19897 if (it->what == IT_CHARACTER)
19898 {
19899 XChar2b char2b;
19900 XFontStruct *font;
19901 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19902 XCharStruct *pcm;
19903 int font_not_found_p;
19904 struct font_info *font_info;
19905 int boff; /* baseline offset */
19906 /* We may change it->multibyte_p upon unibyte<->multibyte
19907 conversion. So, save the current value now and restore it
19908 later.
19909
19910 Note: It seems that we don't have to record multibyte_p in
19911 struct glyph because the character code itself tells if or
19912 not the character is multibyte. Thus, in the future, we must
19913 consider eliminating the field `multibyte_p' in the struct
19914 glyph. */
19915 int saved_multibyte_p = it->multibyte_p;
19916
19917 /* Maybe translate single-byte characters to multibyte, or the
19918 other way. */
19919 it->char_to_display = it->c;
19920 if (!ASCII_BYTE_P (it->c))
19921 {
19922 if (unibyte_display_via_language_environment
19923 && SINGLE_BYTE_CHAR_P (it->c)
19924 && (it->c >= 0240
19925 || !NILP (Vnonascii_translation_table)))
19926 {
19927 it->char_to_display = unibyte_char_to_multibyte (it->c);
19928 it->multibyte_p = 1;
19929 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19930 face = FACE_FROM_ID (it->f, it->face_id);
19931 }
19932 else if (!SINGLE_BYTE_CHAR_P (it->c)
19933 && !it->multibyte_p)
19934 {
19935 it->multibyte_p = 1;
19936 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19937 face = FACE_FROM_ID (it->f, it->face_id);
19938 }
19939 }
19940
19941 /* Get font to use. Encode IT->char_to_display. */
19942 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19943 &char2b, it->multibyte_p, 0);
19944 font = face->font;
19945
19946 /* When no suitable font found, use the default font. */
19947 font_not_found_p = font == NULL;
19948 if (font_not_found_p)
19949 {
19950 font = FRAME_FONT (it->f);
19951 boff = FRAME_BASELINE_OFFSET (it->f);
19952 font_info = NULL;
19953 }
19954 else
19955 {
19956 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19957 boff = font_info->baseline_offset;
19958 if (font_info->vertical_centering)
19959 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19960 }
19961
19962 if (it->char_to_display >= ' '
19963 && (!it->multibyte_p || it->char_to_display < 128))
19964 {
19965 /* Either unibyte or ASCII. */
19966 int stretched_p;
19967
19968 it->nglyphs = 1;
19969
19970 pcm = FRAME_RIF (it->f)->per_char_metric
19971 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19972
19973 if (it->override_ascent >= 0)
19974 {
19975 it->ascent = it->override_ascent;
19976 it->descent = it->override_descent;
19977 boff = it->override_boff;
19978 }
19979 else
19980 {
19981 it->ascent = FONT_BASE (font) + boff;
19982 it->descent = FONT_DESCENT (font) - boff;
19983 }
19984
19985 if (pcm)
19986 {
19987 it->phys_ascent = pcm->ascent + boff;
19988 it->phys_descent = pcm->descent - boff;
19989 it->pixel_width = pcm->width;
19990 }
19991 else
19992 {
19993 it->glyph_not_available_p = 1;
19994 it->phys_ascent = it->ascent;
19995 it->phys_descent = it->descent;
19996 it->pixel_width = FONT_WIDTH (font);
19997 }
19998
19999 if (it->constrain_row_ascent_descent_p)
20000 {
20001 if (it->descent > it->max_descent)
20002 {
20003 it->ascent += it->descent - it->max_descent;
20004 it->descent = it->max_descent;
20005 }
20006 if (it->ascent > it->max_ascent)
20007 {
20008 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20009 it->ascent = it->max_ascent;
20010 }
20011 it->phys_ascent = min (it->phys_ascent, it->ascent);
20012 it->phys_descent = min (it->phys_descent, it->descent);
20013 extra_line_spacing = 0;
20014 }
20015
20016 /* If this is a space inside a region of text with
20017 `space-width' property, change its width. */
20018 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20019 if (stretched_p)
20020 it->pixel_width *= XFLOATINT (it->space_width);
20021
20022 /* If face has a box, add the box thickness to the character
20023 height. If character has a box line to the left and/or
20024 right, add the box line width to the character's width. */
20025 if (face->box != FACE_NO_BOX)
20026 {
20027 int thick = face->box_line_width;
20028
20029 if (thick > 0)
20030 {
20031 it->ascent += thick;
20032 it->descent += thick;
20033 }
20034 else
20035 thick = -thick;
20036
20037 if (it->start_of_box_run_p)
20038 it->pixel_width += thick;
20039 if (it->end_of_box_run_p)
20040 it->pixel_width += thick;
20041 }
20042
20043 /* If face has an overline, add the height of the overline
20044 (1 pixel) and a 1 pixel margin to the character height. */
20045 if (face->overline_p)
20046 it->ascent += 2;
20047
20048 if (it->constrain_row_ascent_descent_p)
20049 {
20050 if (it->ascent > it->max_ascent)
20051 it->ascent = it->max_ascent;
20052 if (it->descent > it->max_descent)
20053 it->descent = it->max_descent;
20054 }
20055
20056 take_vertical_position_into_account (it);
20057
20058 /* If we have to actually produce glyphs, do it. */
20059 if (it->glyph_row)
20060 {
20061 if (stretched_p)
20062 {
20063 /* Translate a space with a `space-width' property
20064 into a stretch glyph. */
20065 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20066 / FONT_HEIGHT (font));
20067 append_stretch_glyph (it, it->object, it->pixel_width,
20068 it->ascent + it->descent, ascent);
20069 }
20070 else
20071 append_glyph (it);
20072
20073 /* If characters with lbearing or rbearing are displayed
20074 in this line, record that fact in a flag of the
20075 glyph row. This is used to optimize X output code. */
20076 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20077 it->glyph_row->contains_overlapping_glyphs_p = 1;
20078 }
20079 }
20080 else if (it->char_to_display == '\n')
20081 {
20082 /* A newline has no width but we need the height of the line.
20083 But if previous part of the line set a height, don't
20084 increase that height */
20085
20086 Lisp_Object height;
20087 Lisp_Object total_height = Qnil;
20088
20089 it->override_ascent = -1;
20090 it->pixel_width = 0;
20091 it->nglyphs = 0;
20092
20093 height = get_line_height_property(it, Qline_height);
20094 /* Split (line-height total-height) list */
20095 if (CONSP (height)
20096 && CONSP (XCDR (height))
20097 && NILP (XCDR (XCDR (height))))
20098 {
20099 total_height = XCAR (XCDR (height));
20100 height = XCAR (height);
20101 }
20102 height = calc_line_height_property(it, height, font, boff, 1);
20103
20104 if (it->override_ascent >= 0)
20105 {
20106 it->ascent = it->override_ascent;
20107 it->descent = it->override_descent;
20108 boff = it->override_boff;
20109 }
20110 else
20111 {
20112 it->ascent = FONT_BASE (font) + boff;
20113 it->descent = FONT_DESCENT (font) - boff;
20114 }
20115
20116 if (EQ (height, Qt))
20117 {
20118 if (it->descent > it->max_descent)
20119 {
20120 it->ascent += it->descent - it->max_descent;
20121 it->descent = it->max_descent;
20122 }
20123 if (it->ascent > it->max_ascent)
20124 {
20125 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20126 it->ascent = it->max_ascent;
20127 }
20128 it->phys_ascent = min (it->phys_ascent, it->ascent);
20129 it->phys_descent = min (it->phys_descent, it->descent);
20130 it->constrain_row_ascent_descent_p = 1;
20131 extra_line_spacing = 0;
20132 }
20133 else
20134 {
20135 Lisp_Object spacing;
20136
20137 it->phys_ascent = it->ascent;
20138 it->phys_descent = it->descent;
20139
20140 if ((it->max_ascent > 0 || it->max_descent > 0)
20141 && face->box != FACE_NO_BOX
20142 && face->box_line_width > 0)
20143 {
20144 it->ascent += face->box_line_width;
20145 it->descent += face->box_line_width;
20146 }
20147 if (!NILP (height)
20148 && XINT (height) > it->ascent + it->descent)
20149 it->ascent = XINT (height) - it->descent;
20150
20151 if (!NILP (total_height))
20152 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20153 else
20154 {
20155 spacing = get_line_height_property(it, Qline_spacing);
20156 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20157 }
20158 if (INTEGERP (spacing))
20159 {
20160 extra_line_spacing = XINT (spacing);
20161 if (!NILP (total_height))
20162 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20163 }
20164 }
20165 }
20166 else if (it->char_to_display == '\t')
20167 {
20168 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20169 int x = it->current_x + it->continuation_lines_width;
20170 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20171
20172 /* If the distance from the current position to the next tab
20173 stop is less than a space character width, use the
20174 tab stop after that. */
20175 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20176 next_tab_x += tab_width;
20177
20178 it->pixel_width = next_tab_x - x;
20179 it->nglyphs = 1;
20180 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20181 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20182
20183 if (it->glyph_row)
20184 {
20185 append_stretch_glyph (it, it->object, it->pixel_width,
20186 it->ascent + it->descent, it->ascent);
20187 }
20188 }
20189 else
20190 {
20191 /* A multi-byte character. Assume that the display width of the
20192 character is the width of the character multiplied by the
20193 width of the font. */
20194
20195 /* If we found a font, this font should give us the right
20196 metrics. If we didn't find a font, use the frame's
20197 default font and calculate the width of the character
20198 from the charset width; this is what old redisplay code
20199 did. */
20200
20201 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20202 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20203
20204 if (font_not_found_p || !pcm)
20205 {
20206 int charset = CHAR_CHARSET (it->char_to_display);
20207
20208 it->glyph_not_available_p = 1;
20209 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20210 * CHARSET_WIDTH (charset));
20211 it->phys_ascent = FONT_BASE (font) + boff;
20212 it->phys_descent = FONT_DESCENT (font) - boff;
20213 }
20214 else
20215 {
20216 it->pixel_width = pcm->width;
20217 it->phys_ascent = pcm->ascent + boff;
20218 it->phys_descent = pcm->descent - boff;
20219 if (it->glyph_row
20220 && (pcm->lbearing < 0
20221 || pcm->rbearing > pcm->width))
20222 it->glyph_row->contains_overlapping_glyphs_p = 1;
20223 }
20224 it->nglyphs = 1;
20225 it->ascent = FONT_BASE (font) + boff;
20226 it->descent = FONT_DESCENT (font) - boff;
20227 if (face->box != FACE_NO_BOX)
20228 {
20229 int thick = face->box_line_width;
20230
20231 if (thick > 0)
20232 {
20233 it->ascent += thick;
20234 it->descent += thick;
20235 }
20236 else
20237 thick = - thick;
20238
20239 if (it->start_of_box_run_p)
20240 it->pixel_width += thick;
20241 if (it->end_of_box_run_p)
20242 it->pixel_width += thick;
20243 }
20244
20245 /* If face has an overline, add the height of the overline
20246 (1 pixel) and a 1 pixel margin to the character height. */
20247 if (face->overline_p)
20248 it->ascent += 2;
20249
20250 take_vertical_position_into_account (it);
20251
20252 if (it->glyph_row)
20253 append_glyph (it);
20254 }
20255 it->multibyte_p = saved_multibyte_p;
20256 }
20257 else if (it->what == IT_COMPOSITION)
20258 {
20259 /* Note: A composition is represented as one glyph in the
20260 glyph matrix. There are no padding glyphs. */
20261 XChar2b char2b;
20262 XFontStruct *font;
20263 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20264 XCharStruct *pcm;
20265 int font_not_found_p;
20266 struct font_info *font_info;
20267 int boff; /* baseline offset */
20268 struct composition *cmp = composition_table[it->cmp_id];
20269
20270 /* Maybe translate single-byte characters to multibyte. */
20271 it->char_to_display = it->c;
20272 if (unibyte_display_via_language_environment
20273 && SINGLE_BYTE_CHAR_P (it->c)
20274 && (it->c >= 0240
20275 || (it->c >= 0200
20276 && !NILP (Vnonascii_translation_table))))
20277 {
20278 it->char_to_display = unibyte_char_to_multibyte (it->c);
20279 }
20280
20281 /* Get face and font to use. Encode IT->char_to_display. */
20282 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20283 face = FACE_FROM_ID (it->f, it->face_id);
20284 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20285 &char2b, it->multibyte_p, 0);
20286 font = face->font;
20287
20288 /* When no suitable font found, use the default font. */
20289 font_not_found_p = font == NULL;
20290 if (font_not_found_p)
20291 {
20292 font = FRAME_FONT (it->f);
20293 boff = FRAME_BASELINE_OFFSET (it->f);
20294 font_info = NULL;
20295 }
20296 else
20297 {
20298 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20299 boff = font_info->baseline_offset;
20300 if (font_info->vertical_centering)
20301 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20302 }
20303
20304 /* There are no padding glyphs, so there is only one glyph to
20305 produce for the composition. Important is that pixel_width,
20306 ascent and descent are the values of what is drawn by
20307 draw_glyphs (i.e. the values of the overall glyphs composed). */
20308 it->nglyphs = 1;
20309
20310 /* If we have not yet calculated pixel size data of glyphs of
20311 the composition for the current face font, calculate them
20312 now. Theoretically, we have to check all fonts for the
20313 glyphs, but that requires much time and memory space. So,
20314 here we check only the font of the first glyph. This leads
20315 to incorrect display very rarely, and C-l (recenter) can
20316 correct the display anyway. */
20317 if (cmp->font != (void *) font)
20318 {
20319 /* Ascent and descent of the font of the first character of
20320 this composition (adjusted by baseline offset). Ascent
20321 and descent of overall glyphs should not be less than
20322 them respectively. */
20323 int font_ascent = FONT_BASE (font) + boff;
20324 int font_descent = FONT_DESCENT (font) - boff;
20325 /* Bounding box of the overall glyphs. */
20326 int leftmost, rightmost, lowest, highest;
20327 int i, width, ascent, descent;
20328
20329 cmp->font = (void *) font;
20330
20331 /* Initialize the bounding box. */
20332 if (font_info
20333 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20334 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20335 {
20336 width = pcm->width;
20337 ascent = pcm->ascent;
20338 descent = pcm->descent;
20339 }
20340 else
20341 {
20342 width = FONT_WIDTH (font);
20343 ascent = FONT_BASE (font);
20344 descent = FONT_DESCENT (font);
20345 }
20346
20347 rightmost = width;
20348 lowest = - descent + boff;
20349 highest = ascent + boff;
20350 leftmost = 0;
20351
20352 if (font_info
20353 && font_info->default_ascent
20354 && CHAR_TABLE_P (Vuse_default_ascent)
20355 && !NILP (Faref (Vuse_default_ascent,
20356 make_number (it->char_to_display))))
20357 highest = font_info->default_ascent + boff;
20358
20359 /* Draw the first glyph at the normal position. It may be
20360 shifted to right later if some other glyphs are drawn at
20361 the left. */
20362 cmp->offsets[0] = 0;
20363 cmp->offsets[1] = boff;
20364
20365 /* Set cmp->offsets for the remaining glyphs. */
20366 for (i = 1; i < cmp->glyph_len; i++)
20367 {
20368 int left, right, btm, top;
20369 int ch = COMPOSITION_GLYPH (cmp, i);
20370 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20371
20372 face = FACE_FROM_ID (it->f, face_id);
20373 get_char_face_and_encoding (it->f, ch, face->id,
20374 &char2b, it->multibyte_p, 0);
20375 font = face->font;
20376 if (font == NULL)
20377 {
20378 font = FRAME_FONT (it->f);
20379 boff = FRAME_BASELINE_OFFSET (it->f);
20380 font_info = NULL;
20381 }
20382 else
20383 {
20384 font_info
20385 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20386 boff = font_info->baseline_offset;
20387 if (font_info->vertical_centering)
20388 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20389 }
20390
20391 if (font_info
20392 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20393 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20394 {
20395 width = pcm->width;
20396 ascent = pcm->ascent;
20397 descent = pcm->descent;
20398 }
20399 else
20400 {
20401 width = FONT_WIDTH (font);
20402 ascent = 1;
20403 descent = 0;
20404 }
20405
20406 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20407 {
20408 /* Relative composition with or without
20409 alternate chars. */
20410 left = (leftmost + rightmost - width) / 2;
20411 btm = - descent + boff;
20412 if (font_info && font_info->relative_compose
20413 && (! CHAR_TABLE_P (Vignore_relative_composition)
20414 || NILP (Faref (Vignore_relative_composition,
20415 make_number (ch)))))
20416 {
20417
20418 if (- descent >= font_info->relative_compose)
20419 /* One extra pixel between two glyphs. */
20420 btm = highest + 1;
20421 else if (ascent <= 0)
20422 /* One extra pixel between two glyphs. */
20423 btm = lowest - 1 - ascent - descent;
20424 }
20425 }
20426 else
20427 {
20428 /* A composition rule is specified by an integer
20429 value that encodes global and new reference
20430 points (GREF and NREF). GREF and NREF are
20431 specified by numbers as below:
20432
20433 0---1---2 -- ascent
20434 | |
20435 | |
20436 | |
20437 9--10--11 -- center
20438 | |
20439 ---3---4---5--- baseline
20440 | |
20441 6---7---8 -- descent
20442 */
20443 int rule = COMPOSITION_RULE (cmp, i);
20444 int gref, nref, grefx, grefy, nrefx, nrefy;
20445
20446 COMPOSITION_DECODE_RULE (rule, gref, nref);
20447 grefx = gref % 3, nrefx = nref % 3;
20448 grefy = gref / 3, nrefy = nref / 3;
20449
20450 left = (leftmost
20451 + grefx * (rightmost - leftmost) / 2
20452 - nrefx * width / 2);
20453 btm = ((grefy == 0 ? highest
20454 : grefy == 1 ? 0
20455 : grefy == 2 ? lowest
20456 : (highest + lowest) / 2)
20457 - (nrefy == 0 ? ascent + descent
20458 : nrefy == 1 ? descent - boff
20459 : nrefy == 2 ? 0
20460 : (ascent + descent) / 2));
20461 }
20462
20463 cmp->offsets[i * 2] = left;
20464 cmp->offsets[i * 2 + 1] = btm + descent;
20465
20466 /* Update the bounding box of the overall glyphs. */
20467 right = left + width;
20468 top = btm + descent + ascent;
20469 if (left < leftmost)
20470 leftmost = left;
20471 if (right > rightmost)
20472 rightmost = right;
20473 if (top > highest)
20474 highest = top;
20475 if (btm < lowest)
20476 lowest = btm;
20477 }
20478
20479 /* If there are glyphs whose x-offsets are negative,
20480 shift all glyphs to the right and make all x-offsets
20481 non-negative. */
20482 if (leftmost < 0)
20483 {
20484 for (i = 0; i < cmp->glyph_len; i++)
20485 cmp->offsets[i * 2] -= leftmost;
20486 rightmost -= leftmost;
20487 }
20488
20489 cmp->pixel_width = rightmost;
20490 cmp->ascent = highest;
20491 cmp->descent = - lowest;
20492 if (cmp->ascent < font_ascent)
20493 cmp->ascent = font_ascent;
20494 if (cmp->descent < font_descent)
20495 cmp->descent = font_descent;
20496 }
20497
20498 it->pixel_width = cmp->pixel_width;
20499 it->ascent = it->phys_ascent = cmp->ascent;
20500 it->descent = it->phys_descent = cmp->descent;
20501
20502 if (face->box != FACE_NO_BOX)
20503 {
20504 int thick = face->box_line_width;
20505
20506 if (thick > 0)
20507 {
20508 it->ascent += thick;
20509 it->descent += thick;
20510 }
20511 else
20512 thick = - thick;
20513
20514 if (it->start_of_box_run_p)
20515 it->pixel_width += thick;
20516 if (it->end_of_box_run_p)
20517 it->pixel_width += thick;
20518 }
20519
20520 /* If face has an overline, add the height of the overline
20521 (1 pixel) and a 1 pixel margin to the character height. */
20522 if (face->overline_p)
20523 it->ascent += 2;
20524
20525 take_vertical_position_into_account (it);
20526
20527 if (it->glyph_row)
20528 append_composite_glyph (it);
20529 }
20530 else if (it->what == IT_IMAGE)
20531 produce_image_glyph (it);
20532 else if (it->what == IT_STRETCH)
20533 produce_stretch_glyph (it);
20534
20535 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20536 because this isn't true for images with `:ascent 100'. */
20537 xassert (it->ascent >= 0 && it->descent >= 0);
20538 if (it->area == TEXT_AREA)
20539 it->current_x += it->pixel_width;
20540
20541 if (extra_line_spacing > 0)
20542 {
20543 it->descent += extra_line_spacing;
20544 if (extra_line_spacing > it->max_extra_line_spacing)
20545 it->max_extra_line_spacing = extra_line_spacing;
20546 }
20547
20548 it->max_ascent = max (it->max_ascent, it->ascent);
20549 it->max_descent = max (it->max_descent, it->descent);
20550 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20551 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20552 }
20553
20554 /* EXPORT for RIF:
20555 Output LEN glyphs starting at START at the nominal cursor position.
20556 Advance the nominal cursor over the text. The global variable
20557 updated_window contains the window being updated, updated_row is
20558 the glyph row being updated, and updated_area is the area of that
20559 row being updated. */
20560
20561 void
20562 x_write_glyphs (start, len)
20563 struct glyph *start;
20564 int len;
20565 {
20566 int x, hpos;
20567
20568 xassert (updated_window && updated_row);
20569 BLOCK_INPUT;
20570
20571 /* Write glyphs. */
20572
20573 hpos = start - updated_row->glyphs[updated_area];
20574 x = draw_glyphs (updated_window, output_cursor.x,
20575 updated_row, updated_area,
20576 hpos, hpos + len,
20577 DRAW_NORMAL_TEXT, 0);
20578
20579 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20580 if (updated_area == TEXT_AREA
20581 && updated_window->phys_cursor_on_p
20582 && updated_window->phys_cursor.vpos == output_cursor.vpos
20583 && updated_window->phys_cursor.hpos >= hpos
20584 && updated_window->phys_cursor.hpos < hpos + len)
20585 updated_window->phys_cursor_on_p = 0;
20586
20587 UNBLOCK_INPUT;
20588
20589 /* Advance the output cursor. */
20590 output_cursor.hpos += len;
20591 output_cursor.x = x;
20592 }
20593
20594
20595 /* EXPORT for RIF:
20596 Insert LEN glyphs from START at the nominal cursor position. */
20597
20598 void
20599 x_insert_glyphs (start, len)
20600 struct glyph *start;
20601 int len;
20602 {
20603 struct frame *f;
20604 struct window *w;
20605 int line_height, shift_by_width, shifted_region_width;
20606 struct glyph_row *row;
20607 struct glyph *glyph;
20608 int frame_x, frame_y, hpos;
20609
20610 xassert (updated_window && updated_row);
20611 BLOCK_INPUT;
20612 w = updated_window;
20613 f = XFRAME (WINDOW_FRAME (w));
20614
20615 /* Get the height of the line we are in. */
20616 row = updated_row;
20617 line_height = row->height;
20618
20619 /* Get the width of the glyphs to insert. */
20620 shift_by_width = 0;
20621 for (glyph = start; glyph < start + len; ++glyph)
20622 shift_by_width += glyph->pixel_width;
20623
20624 /* Get the width of the region to shift right. */
20625 shifted_region_width = (window_box_width (w, updated_area)
20626 - output_cursor.x
20627 - shift_by_width);
20628
20629 /* Shift right. */
20630 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20631 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20632
20633 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20634 line_height, shift_by_width);
20635
20636 /* Write the glyphs. */
20637 hpos = start - row->glyphs[updated_area];
20638 draw_glyphs (w, output_cursor.x, row, updated_area,
20639 hpos, hpos + len,
20640 DRAW_NORMAL_TEXT, 0);
20641
20642 /* Advance the output cursor. */
20643 output_cursor.hpos += len;
20644 output_cursor.x += shift_by_width;
20645 UNBLOCK_INPUT;
20646 }
20647
20648
20649 /* EXPORT for RIF:
20650 Erase the current text line from the nominal cursor position
20651 (inclusive) to pixel column TO_X (exclusive). The idea is that
20652 everything from TO_X onward is already erased.
20653
20654 TO_X is a pixel position relative to updated_area of
20655 updated_window. TO_X == -1 means clear to the end of this area. */
20656
20657 void
20658 x_clear_end_of_line (to_x)
20659 int to_x;
20660 {
20661 struct frame *f;
20662 struct window *w = updated_window;
20663 int max_x, min_y, max_y;
20664 int from_x, from_y, to_y;
20665
20666 xassert (updated_window && updated_row);
20667 f = XFRAME (w->frame);
20668
20669 if (updated_row->full_width_p)
20670 max_x = WINDOW_TOTAL_WIDTH (w);
20671 else
20672 max_x = window_box_width (w, updated_area);
20673 max_y = window_text_bottom_y (w);
20674
20675 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20676 of window. For TO_X > 0, truncate to end of drawing area. */
20677 if (to_x == 0)
20678 return;
20679 else if (to_x < 0)
20680 to_x = max_x;
20681 else
20682 to_x = min (to_x, max_x);
20683
20684 to_y = min (max_y, output_cursor.y + updated_row->height);
20685
20686 /* Notice if the cursor will be cleared by this operation. */
20687 if (!updated_row->full_width_p)
20688 notice_overwritten_cursor (w, updated_area,
20689 output_cursor.x, -1,
20690 updated_row->y,
20691 MATRIX_ROW_BOTTOM_Y (updated_row));
20692
20693 from_x = output_cursor.x;
20694
20695 /* Translate to frame coordinates. */
20696 if (updated_row->full_width_p)
20697 {
20698 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20699 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20700 }
20701 else
20702 {
20703 int area_left = window_box_left (w, updated_area);
20704 from_x += area_left;
20705 to_x += area_left;
20706 }
20707
20708 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20709 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20710 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20711
20712 /* Prevent inadvertently clearing to end of the X window. */
20713 if (to_x > from_x && to_y > from_y)
20714 {
20715 BLOCK_INPUT;
20716 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
20717 to_x - from_x, to_y - from_y);
20718 UNBLOCK_INPUT;
20719 }
20720 }
20721
20722 #endif /* HAVE_WINDOW_SYSTEM */
20723
20724
20725 \f
20726 /***********************************************************************
20727 Cursor types
20728 ***********************************************************************/
20729
20730 /* Value is the internal representation of the specified cursor type
20731 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20732 of the bar cursor. */
20733
20734 static enum text_cursor_kinds
20735 get_specified_cursor_type (arg, width)
20736 Lisp_Object arg;
20737 int *width;
20738 {
20739 enum text_cursor_kinds type;
20740
20741 if (NILP (arg))
20742 return NO_CURSOR;
20743
20744 if (EQ (arg, Qbox))
20745 return FILLED_BOX_CURSOR;
20746
20747 if (EQ (arg, Qhollow))
20748 return HOLLOW_BOX_CURSOR;
20749
20750 if (EQ (arg, Qbar))
20751 {
20752 *width = 2;
20753 return BAR_CURSOR;
20754 }
20755
20756 if (CONSP (arg)
20757 && EQ (XCAR (arg), Qbar)
20758 && INTEGERP (XCDR (arg))
20759 && XINT (XCDR (arg)) >= 0)
20760 {
20761 *width = XINT (XCDR (arg));
20762 return BAR_CURSOR;
20763 }
20764
20765 if (EQ (arg, Qhbar))
20766 {
20767 *width = 2;
20768 return HBAR_CURSOR;
20769 }
20770
20771 if (CONSP (arg)
20772 && EQ (XCAR (arg), Qhbar)
20773 && INTEGERP (XCDR (arg))
20774 && XINT (XCDR (arg)) >= 0)
20775 {
20776 *width = XINT (XCDR (arg));
20777 return HBAR_CURSOR;
20778 }
20779
20780 /* Treat anything unknown as "hollow box cursor".
20781 It was bad to signal an error; people have trouble fixing
20782 .Xdefaults with Emacs, when it has something bad in it. */
20783 type = HOLLOW_BOX_CURSOR;
20784
20785 return type;
20786 }
20787
20788 /* Set the default cursor types for specified frame. */
20789 void
20790 set_frame_cursor_types (f, arg)
20791 struct frame *f;
20792 Lisp_Object arg;
20793 {
20794 int width;
20795 Lisp_Object tem;
20796
20797 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20798 FRAME_CURSOR_WIDTH (f) = width;
20799
20800 /* By default, set up the blink-off state depending on the on-state. */
20801
20802 tem = Fassoc (arg, Vblink_cursor_alist);
20803 if (!NILP (tem))
20804 {
20805 FRAME_BLINK_OFF_CURSOR (f)
20806 = get_specified_cursor_type (XCDR (tem), &width);
20807 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20808 }
20809 else
20810 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20811 }
20812
20813
20814 /* Return the cursor we want to be displayed in window W. Return
20815 width of bar/hbar cursor through WIDTH arg. Return with
20816 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20817 (i.e. if the `system caret' should track this cursor).
20818
20819 In a mini-buffer window, we want the cursor only to appear if we
20820 are reading input from this window. For the selected window, we
20821 want the cursor type given by the frame parameter or buffer local
20822 setting of cursor-type. If explicitly marked off, draw no cursor.
20823 In all other cases, we want a hollow box cursor. */
20824
20825 static enum text_cursor_kinds
20826 get_window_cursor_type (w, glyph, width, active_cursor)
20827 struct window *w;
20828 struct glyph *glyph;
20829 int *width;
20830 int *active_cursor;
20831 {
20832 struct frame *f = XFRAME (w->frame);
20833 struct buffer *b = XBUFFER (w->buffer);
20834 int cursor_type = DEFAULT_CURSOR;
20835 Lisp_Object alt_cursor;
20836 int non_selected = 0;
20837
20838 *active_cursor = 1;
20839
20840 /* Echo area */
20841 if (cursor_in_echo_area
20842 && FRAME_HAS_MINIBUF_P (f)
20843 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20844 {
20845 if (w == XWINDOW (echo_area_window))
20846 {
20847 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20848 {
20849 *width = FRAME_CURSOR_WIDTH (f);
20850 return FRAME_DESIRED_CURSOR (f);
20851 }
20852 else
20853 return get_specified_cursor_type (b->cursor_type, width);
20854 }
20855
20856 *active_cursor = 0;
20857 non_selected = 1;
20858 }
20859
20860 /* Nonselected window or nonselected frame. */
20861 else if (w != XWINDOW (f->selected_window)
20862 #ifdef HAVE_WINDOW_SYSTEM
20863 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20864 #endif
20865 )
20866 {
20867 *active_cursor = 0;
20868
20869 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20870 return NO_CURSOR;
20871
20872 non_selected = 1;
20873 }
20874
20875 /* Never display a cursor in a window in which cursor-type is nil. */
20876 if (NILP (b->cursor_type))
20877 return NO_CURSOR;
20878
20879 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20880 if (non_selected)
20881 {
20882 alt_cursor = b->cursor_in_non_selected_windows;
20883 return get_specified_cursor_type (alt_cursor, width);
20884 }
20885
20886 /* Get the normal cursor type for this window. */
20887 if (EQ (b->cursor_type, Qt))
20888 {
20889 cursor_type = FRAME_DESIRED_CURSOR (f);
20890 *width = FRAME_CURSOR_WIDTH (f);
20891 }
20892 else
20893 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20894
20895 /* Use normal cursor if not blinked off. */
20896 if (!w->cursor_off_p)
20897 {
20898 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20899 if (cursor_type == FILLED_BOX_CURSOR)
20900 cursor_type = HOLLOW_BOX_CURSOR;
20901 }
20902 return cursor_type;
20903 }
20904
20905 /* Cursor is blinked off, so determine how to "toggle" it. */
20906
20907 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20908 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20909 return get_specified_cursor_type (XCDR (alt_cursor), width);
20910
20911 /* Then see if frame has specified a specific blink off cursor type. */
20912 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20913 {
20914 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20915 return FRAME_BLINK_OFF_CURSOR (f);
20916 }
20917
20918 #if 0
20919 /* Some people liked having a permanently visible blinking cursor,
20920 while others had very strong opinions against it. So it was
20921 decided to remove it. KFS 2003-09-03 */
20922
20923 /* Finally perform built-in cursor blinking:
20924 filled box <-> hollow box
20925 wide [h]bar <-> narrow [h]bar
20926 narrow [h]bar <-> no cursor
20927 other type <-> no cursor */
20928
20929 if (cursor_type == FILLED_BOX_CURSOR)
20930 return HOLLOW_BOX_CURSOR;
20931
20932 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20933 {
20934 *width = 1;
20935 return cursor_type;
20936 }
20937 #endif
20938
20939 return NO_CURSOR;
20940 }
20941
20942
20943 #ifdef HAVE_WINDOW_SYSTEM
20944
20945 /* Notice when the text cursor of window W has been completely
20946 overwritten by a drawing operation that outputs glyphs in AREA
20947 starting at X0 and ending at X1 in the line starting at Y0 and
20948 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20949 the rest of the line after X0 has been written. Y coordinates
20950 are window-relative. */
20951
20952 static void
20953 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20954 struct window *w;
20955 enum glyph_row_area area;
20956 int x0, y0, x1, y1;
20957 {
20958 int cx0, cx1, cy0, cy1;
20959 struct glyph_row *row;
20960
20961 if (!w->phys_cursor_on_p)
20962 return;
20963 if (area != TEXT_AREA)
20964 return;
20965
20966 if (w->phys_cursor.vpos < 0
20967 || w->phys_cursor.vpos >= w->current_matrix->nrows
20968 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20969 !(row->enabled_p && row->displays_text_p)))
20970 return;
20971
20972 if (row->cursor_in_fringe_p)
20973 {
20974 row->cursor_in_fringe_p = 0;
20975 draw_fringe_bitmap (w, row, 0);
20976 w->phys_cursor_on_p = 0;
20977 return;
20978 }
20979
20980 cx0 = w->phys_cursor.x;
20981 cx1 = cx0 + w->phys_cursor_width;
20982 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20983 return;
20984
20985 /* The cursor image will be completely removed from the
20986 screen if the output area intersects the cursor area in
20987 y-direction. When we draw in [y0 y1[, and some part of
20988 the cursor is at y < y0, that part must have been drawn
20989 before. When scrolling, the cursor is erased before
20990 actually scrolling, so we don't come here. When not
20991 scrolling, the rows above the old cursor row must have
20992 changed, and in this case these rows must have written
20993 over the cursor image.
20994
20995 Likewise if part of the cursor is below y1, with the
20996 exception of the cursor being in the first blank row at
20997 the buffer and window end because update_text_area
20998 doesn't draw that row. (Except when it does, but
20999 that's handled in update_text_area.) */
21000
21001 cy0 = w->phys_cursor.y;
21002 cy1 = cy0 + w->phys_cursor_height;
21003 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21004 return;
21005
21006 w->phys_cursor_on_p = 0;
21007 }
21008
21009 #endif /* HAVE_WINDOW_SYSTEM */
21010
21011 \f
21012 /************************************************************************
21013 Mouse Face
21014 ************************************************************************/
21015
21016 #ifdef HAVE_WINDOW_SYSTEM
21017
21018 /* EXPORT for RIF:
21019 Fix the display of area AREA of overlapping row ROW in window W
21020 with respect to the overlapping part OVERLAPS. */
21021
21022 void
21023 x_fix_overlapping_area (w, row, area, overlaps)
21024 struct window *w;
21025 struct glyph_row *row;
21026 enum glyph_row_area area;
21027 int overlaps;
21028 {
21029 int i, x;
21030
21031 BLOCK_INPUT;
21032
21033 x = 0;
21034 for (i = 0; i < row->used[area];)
21035 {
21036 if (row->glyphs[area][i].overlaps_vertically_p)
21037 {
21038 int start = i, start_x = x;
21039
21040 do
21041 {
21042 x += row->glyphs[area][i].pixel_width;
21043 ++i;
21044 }
21045 while (i < row->used[area]
21046 && row->glyphs[area][i].overlaps_vertically_p);
21047
21048 draw_glyphs (w, start_x, row, area,
21049 start, i,
21050 DRAW_NORMAL_TEXT, overlaps);
21051 }
21052 else
21053 {
21054 x += row->glyphs[area][i].pixel_width;
21055 ++i;
21056 }
21057 }
21058
21059 UNBLOCK_INPUT;
21060 }
21061
21062
21063 /* EXPORT:
21064 Draw the cursor glyph of window W in glyph row ROW. See the
21065 comment of draw_glyphs for the meaning of HL. */
21066
21067 void
21068 draw_phys_cursor_glyph (w, row, hl)
21069 struct window *w;
21070 struct glyph_row *row;
21071 enum draw_glyphs_face hl;
21072 {
21073 /* If cursor hpos is out of bounds, don't draw garbage. This can
21074 happen in mini-buffer windows when switching between echo area
21075 glyphs and mini-buffer. */
21076 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21077 {
21078 int on_p = w->phys_cursor_on_p;
21079 int x1;
21080 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21081 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21082 hl, 0);
21083 w->phys_cursor_on_p = on_p;
21084
21085 if (hl == DRAW_CURSOR)
21086 w->phys_cursor_width = x1 - w->phys_cursor.x;
21087 /* When we erase the cursor, and ROW is overlapped by other
21088 rows, make sure that these overlapping parts of other rows
21089 are redrawn. */
21090 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21091 {
21092 w->phys_cursor_width = x1 - w->phys_cursor.x;
21093
21094 if (row > w->current_matrix->rows
21095 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21096 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21097 OVERLAPS_ERASED_CURSOR);
21098
21099 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21100 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21101 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21102 OVERLAPS_ERASED_CURSOR);
21103 }
21104 }
21105 }
21106
21107
21108 /* EXPORT:
21109 Erase the image of a cursor of window W from the screen. */
21110
21111 void
21112 erase_phys_cursor (w)
21113 struct window *w;
21114 {
21115 struct frame *f = XFRAME (w->frame);
21116 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21117 int hpos = w->phys_cursor.hpos;
21118 int vpos = w->phys_cursor.vpos;
21119 int mouse_face_here_p = 0;
21120 struct glyph_matrix *active_glyphs = w->current_matrix;
21121 struct glyph_row *cursor_row;
21122 struct glyph *cursor_glyph;
21123 enum draw_glyphs_face hl;
21124
21125 /* No cursor displayed or row invalidated => nothing to do on the
21126 screen. */
21127 if (w->phys_cursor_type == NO_CURSOR)
21128 goto mark_cursor_off;
21129
21130 /* VPOS >= active_glyphs->nrows means that window has been resized.
21131 Don't bother to erase the cursor. */
21132 if (vpos >= active_glyphs->nrows)
21133 goto mark_cursor_off;
21134
21135 /* If row containing cursor is marked invalid, there is nothing we
21136 can do. */
21137 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21138 if (!cursor_row->enabled_p)
21139 goto mark_cursor_off;
21140
21141 /* If line spacing is > 0, old cursor may only be partially visible in
21142 window after split-window. So adjust visible height. */
21143 cursor_row->visible_height = min (cursor_row->visible_height,
21144 window_text_bottom_y (w) - cursor_row->y);
21145
21146 /* If row is completely invisible, don't attempt to delete a cursor which
21147 isn't there. This can happen if cursor is at top of a window, and
21148 we switch to a buffer with a header line in that window. */
21149 if (cursor_row->visible_height <= 0)
21150 goto mark_cursor_off;
21151
21152 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21153 if (cursor_row->cursor_in_fringe_p)
21154 {
21155 cursor_row->cursor_in_fringe_p = 0;
21156 draw_fringe_bitmap (w, cursor_row, 0);
21157 goto mark_cursor_off;
21158 }
21159
21160 /* This can happen when the new row is shorter than the old one.
21161 In this case, either draw_glyphs or clear_end_of_line
21162 should have cleared the cursor. Note that we wouldn't be
21163 able to erase the cursor in this case because we don't have a
21164 cursor glyph at hand. */
21165 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21166 goto mark_cursor_off;
21167
21168 /* If the cursor is in the mouse face area, redisplay that when
21169 we clear the cursor. */
21170 if (! NILP (dpyinfo->mouse_face_window)
21171 && w == XWINDOW (dpyinfo->mouse_face_window)
21172 && (vpos > dpyinfo->mouse_face_beg_row
21173 || (vpos == dpyinfo->mouse_face_beg_row
21174 && hpos >= dpyinfo->mouse_face_beg_col))
21175 && (vpos < dpyinfo->mouse_face_end_row
21176 || (vpos == dpyinfo->mouse_face_end_row
21177 && hpos < dpyinfo->mouse_face_end_col))
21178 /* Don't redraw the cursor's spot in mouse face if it is at the
21179 end of a line (on a newline). The cursor appears there, but
21180 mouse highlighting does not. */
21181 && cursor_row->used[TEXT_AREA] > hpos)
21182 mouse_face_here_p = 1;
21183
21184 /* Maybe clear the display under the cursor. */
21185 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21186 {
21187 int x, y;
21188 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21189 int width;
21190
21191 cursor_glyph = get_phys_cursor_glyph (w);
21192 if (cursor_glyph == NULL)
21193 goto mark_cursor_off;
21194
21195 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21196 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21197 width = min (cursor_glyph->pixel_width,
21198 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21199
21200 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21201 }
21202
21203 /* Erase the cursor by redrawing the character underneath it. */
21204 if (mouse_face_here_p)
21205 hl = DRAW_MOUSE_FACE;
21206 else
21207 hl = DRAW_NORMAL_TEXT;
21208 draw_phys_cursor_glyph (w, cursor_row, hl);
21209
21210 mark_cursor_off:
21211 w->phys_cursor_on_p = 0;
21212 w->phys_cursor_type = NO_CURSOR;
21213 }
21214
21215
21216 /* EXPORT:
21217 Display or clear cursor of window W. If ON is zero, clear the
21218 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21219 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21220
21221 void
21222 display_and_set_cursor (w, on, hpos, vpos, x, y)
21223 struct window *w;
21224 int on, hpos, vpos, x, y;
21225 {
21226 struct frame *f = XFRAME (w->frame);
21227 int new_cursor_type;
21228 int new_cursor_width;
21229 int active_cursor;
21230 struct glyph_row *glyph_row;
21231 struct glyph *glyph;
21232
21233 /* This is pointless on invisible frames, and dangerous on garbaged
21234 windows and frames; in the latter case, the frame or window may
21235 be in the midst of changing its size, and x and y may be off the
21236 window. */
21237 if (! FRAME_VISIBLE_P (f)
21238 || FRAME_GARBAGED_P (f)
21239 || vpos >= w->current_matrix->nrows
21240 || hpos >= w->current_matrix->matrix_w)
21241 return;
21242
21243 /* If cursor is off and we want it off, return quickly. */
21244 if (!on && !w->phys_cursor_on_p)
21245 return;
21246
21247 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21248 /* If cursor row is not enabled, we don't really know where to
21249 display the cursor. */
21250 if (!glyph_row->enabled_p)
21251 {
21252 w->phys_cursor_on_p = 0;
21253 return;
21254 }
21255
21256 glyph = NULL;
21257 if (!glyph_row->exact_window_width_line_p
21258 || hpos < glyph_row->used[TEXT_AREA])
21259 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21260
21261 xassert (interrupt_input_blocked);
21262
21263 /* Set new_cursor_type to the cursor we want to be displayed. */
21264 new_cursor_type = get_window_cursor_type (w, glyph,
21265 &new_cursor_width, &active_cursor);
21266
21267 /* If cursor is currently being shown and we don't want it to be or
21268 it is in the wrong place, or the cursor type is not what we want,
21269 erase it. */
21270 if (w->phys_cursor_on_p
21271 && (!on
21272 || w->phys_cursor.x != x
21273 || w->phys_cursor.y != y
21274 || new_cursor_type != w->phys_cursor_type
21275 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21276 && new_cursor_width != w->phys_cursor_width)))
21277 erase_phys_cursor (w);
21278
21279 /* Don't check phys_cursor_on_p here because that flag is only set
21280 to zero in some cases where we know that the cursor has been
21281 completely erased, to avoid the extra work of erasing the cursor
21282 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21283 still not be visible, or it has only been partly erased. */
21284 if (on)
21285 {
21286 w->phys_cursor_ascent = glyph_row->ascent;
21287 w->phys_cursor_height = glyph_row->height;
21288
21289 /* Set phys_cursor_.* before x_draw_.* is called because some
21290 of them may need the information. */
21291 w->phys_cursor.x = x;
21292 w->phys_cursor.y = glyph_row->y;
21293 w->phys_cursor.hpos = hpos;
21294 w->phys_cursor.vpos = vpos;
21295 }
21296
21297 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21298 new_cursor_type, new_cursor_width,
21299 on, active_cursor);
21300 }
21301
21302
21303 /* Switch the display of W's cursor on or off, according to the value
21304 of ON. */
21305
21306 static void
21307 update_window_cursor (w, on)
21308 struct window *w;
21309 int on;
21310 {
21311 /* Don't update cursor in windows whose frame is in the process
21312 of being deleted. */
21313 if (w->current_matrix)
21314 {
21315 BLOCK_INPUT;
21316 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21317 w->phys_cursor.x, w->phys_cursor.y);
21318 UNBLOCK_INPUT;
21319 }
21320 }
21321
21322
21323 /* Call update_window_cursor with parameter ON_P on all leaf windows
21324 in the window tree rooted at W. */
21325
21326 static void
21327 update_cursor_in_window_tree (w, on_p)
21328 struct window *w;
21329 int on_p;
21330 {
21331 while (w)
21332 {
21333 if (!NILP (w->hchild))
21334 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21335 else if (!NILP (w->vchild))
21336 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21337 else
21338 update_window_cursor (w, on_p);
21339
21340 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21341 }
21342 }
21343
21344
21345 /* EXPORT:
21346 Display the cursor on window W, or clear it, according to ON_P.
21347 Don't change the cursor's position. */
21348
21349 void
21350 x_update_cursor (f, on_p)
21351 struct frame *f;
21352 int on_p;
21353 {
21354 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21355 }
21356
21357
21358 /* EXPORT:
21359 Clear the cursor of window W to background color, and mark the
21360 cursor as not shown. This is used when the text where the cursor
21361 is is about to be rewritten. */
21362
21363 void
21364 x_clear_cursor (w)
21365 struct window *w;
21366 {
21367 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21368 update_window_cursor (w, 0);
21369 }
21370
21371
21372 /* EXPORT:
21373 Display the active region described by mouse_face_* according to DRAW. */
21374
21375 void
21376 show_mouse_face (dpyinfo, draw)
21377 Display_Info *dpyinfo;
21378 enum draw_glyphs_face draw;
21379 {
21380 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21381 struct frame *f = XFRAME (WINDOW_FRAME (w));
21382
21383 if (/* If window is in the process of being destroyed, don't bother
21384 to do anything. */
21385 w->current_matrix != NULL
21386 /* Don't update mouse highlight if hidden */
21387 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21388 /* Recognize when we are called to operate on rows that don't exist
21389 anymore. This can happen when a window is split. */
21390 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21391 {
21392 int phys_cursor_on_p = w->phys_cursor_on_p;
21393 struct glyph_row *row, *first, *last;
21394
21395 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21396 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21397
21398 for (row = first; row <= last && row->enabled_p; ++row)
21399 {
21400 int start_hpos, end_hpos, start_x;
21401
21402 /* For all but the first row, the highlight starts at column 0. */
21403 if (row == first)
21404 {
21405 start_hpos = dpyinfo->mouse_face_beg_col;
21406 start_x = dpyinfo->mouse_face_beg_x;
21407 }
21408 else
21409 {
21410 start_hpos = 0;
21411 start_x = 0;
21412 }
21413
21414 if (row == last)
21415 end_hpos = dpyinfo->mouse_face_end_col;
21416 else
21417 {
21418 end_hpos = row->used[TEXT_AREA];
21419 if (draw == DRAW_NORMAL_TEXT)
21420 row->fill_line_p = 1; /* Clear to end of line */
21421 }
21422
21423 if (end_hpos > start_hpos)
21424 {
21425 draw_glyphs (w, start_x, row, TEXT_AREA,
21426 start_hpos, end_hpos,
21427 draw, 0);
21428
21429 row->mouse_face_p
21430 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21431 }
21432 }
21433
21434 /* When we've written over the cursor, arrange for it to
21435 be displayed again. */
21436 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21437 {
21438 BLOCK_INPUT;
21439 display_and_set_cursor (w, 1,
21440 w->phys_cursor.hpos, w->phys_cursor.vpos,
21441 w->phys_cursor.x, w->phys_cursor.y);
21442 UNBLOCK_INPUT;
21443 }
21444 }
21445
21446 /* Change the mouse cursor. */
21447 if (draw == DRAW_NORMAL_TEXT)
21448 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21449 else if (draw == DRAW_MOUSE_FACE)
21450 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21451 else
21452 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21453 }
21454
21455 /* EXPORT:
21456 Clear out the mouse-highlighted active region.
21457 Redraw it un-highlighted first. Value is non-zero if mouse
21458 face was actually drawn unhighlighted. */
21459
21460 int
21461 clear_mouse_face (dpyinfo)
21462 Display_Info *dpyinfo;
21463 {
21464 int cleared = 0;
21465
21466 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21467 {
21468 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21469 cleared = 1;
21470 }
21471
21472 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21473 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21474 dpyinfo->mouse_face_window = Qnil;
21475 dpyinfo->mouse_face_overlay = Qnil;
21476 return cleared;
21477 }
21478
21479
21480 /* EXPORT:
21481 Non-zero if physical cursor of window W is within mouse face. */
21482
21483 int
21484 cursor_in_mouse_face_p (w)
21485 struct window *w;
21486 {
21487 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21488 int in_mouse_face = 0;
21489
21490 if (WINDOWP (dpyinfo->mouse_face_window)
21491 && XWINDOW (dpyinfo->mouse_face_window) == w)
21492 {
21493 int hpos = w->phys_cursor.hpos;
21494 int vpos = w->phys_cursor.vpos;
21495
21496 if (vpos >= dpyinfo->mouse_face_beg_row
21497 && vpos <= dpyinfo->mouse_face_end_row
21498 && (vpos > dpyinfo->mouse_face_beg_row
21499 || hpos >= dpyinfo->mouse_face_beg_col)
21500 && (vpos < dpyinfo->mouse_face_end_row
21501 || hpos < dpyinfo->mouse_face_end_col
21502 || dpyinfo->mouse_face_past_end))
21503 in_mouse_face = 1;
21504 }
21505
21506 return in_mouse_face;
21507 }
21508
21509
21510
21511 \f
21512 /* Find the glyph matrix position of buffer position CHARPOS in window
21513 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21514 current glyphs must be up to date. If CHARPOS is above window
21515 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21516 of last line in W. In the row containing CHARPOS, stop before glyphs
21517 having STOP as object. */
21518
21519 #if 1 /* This is a version of fast_find_position that's more correct
21520 in the presence of hscrolling, for example. I didn't install
21521 it right away because the problem fixed is minor, it failed
21522 in 20.x as well, and I think it's too risky to install
21523 so near the release of 21.1. 2001-09-25 gerd. */
21524
21525 static int
21526 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21527 struct window *w;
21528 int charpos;
21529 int *hpos, *vpos, *x, *y;
21530 Lisp_Object stop;
21531 {
21532 struct glyph_row *row, *first;
21533 struct glyph *glyph, *end;
21534 int past_end = 0;
21535
21536 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21537 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21538 {
21539 *x = first->x;
21540 *y = first->y;
21541 *hpos = 0;
21542 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21543 return 1;
21544 }
21545
21546 row = row_containing_pos (w, charpos, first, NULL, 0);
21547 if (row == NULL)
21548 {
21549 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21550 past_end = 1;
21551 }
21552
21553 /* If whole rows or last part of a row came from a display overlay,
21554 row_containing_pos will skip over such rows because their end pos
21555 equals the start pos of the overlay or interval.
21556
21557 Move back if we have a STOP object and previous row's
21558 end glyph came from STOP. */
21559 if (!NILP (stop))
21560 {
21561 struct glyph_row *prev;
21562 while ((prev = row - 1, prev >= first)
21563 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21564 && prev->used[TEXT_AREA] > 0)
21565 {
21566 struct glyph *beg = prev->glyphs[TEXT_AREA];
21567 glyph = beg + prev->used[TEXT_AREA];
21568 while (--glyph >= beg
21569 && INTEGERP (glyph->object));
21570 if (glyph < beg
21571 || !EQ (stop, glyph->object))
21572 break;
21573 row = prev;
21574 }
21575 }
21576
21577 *x = row->x;
21578 *y = row->y;
21579 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21580
21581 glyph = row->glyphs[TEXT_AREA];
21582 end = glyph + row->used[TEXT_AREA];
21583
21584 /* Skip over glyphs not having an object at the start of the row.
21585 These are special glyphs like truncation marks on terminal
21586 frames. */
21587 if (row->displays_text_p)
21588 while (glyph < end
21589 && INTEGERP (glyph->object)
21590 && !EQ (stop, glyph->object)
21591 && glyph->charpos < 0)
21592 {
21593 *x += glyph->pixel_width;
21594 ++glyph;
21595 }
21596
21597 while (glyph < end
21598 && !INTEGERP (glyph->object)
21599 && !EQ (stop, glyph->object)
21600 && (!BUFFERP (glyph->object)
21601 || glyph->charpos < charpos))
21602 {
21603 *x += glyph->pixel_width;
21604 ++glyph;
21605 }
21606
21607 *hpos = glyph - row->glyphs[TEXT_AREA];
21608 return !past_end;
21609 }
21610
21611 #else /* not 1 */
21612
21613 static int
21614 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21615 struct window *w;
21616 int pos;
21617 int *hpos, *vpos, *x, *y;
21618 Lisp_Object stop;
21619 {
21620 int i;
21621 int lastcol;
21622 int maybe_next_line_p = 0;
21623 int line_start_position;
21624 int yb = window_text_bottom_y (w);
21625 struct glyph_row *row, *best_row;
21626 int row_vpos, best_row_vpos;
21627 int current_x;
21628
21629 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21630 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21631
21632 while (row->y < yb)
21633 {
21634 if (row->used[TEXT_AREA])
21635 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21636 else
21637 line_start_position = 0;
21638
21639 if (line_start_position > pos)
21640 break;
21641 /* If the position sought is the end of the buffer,
21642 don't include the blank lines at the bottom of the window. */
21643 else if (line_start_position == pos
21644 && pos == BUF_ZV (XBUFFER (w->buffer)))
21645 {
21646 maybe_next_line_p = 1;
21647 break;
21648 }
21649 else if (line_start_position > 0)
21650 {
21651 best_row = row;
21652 best_row_vpos = row_vpos;
21653 }
21654
21655 if (row->y + row->height >= yb)
21656 break;
21657
21658 ++row;
21659 ++row_vpos;
21660 }
21661
21662 /* Find the right column within BEST_ROW. */
21663 lastcol = 0;
21664 current_x = best_row->x;
21665 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21666 {
21667 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21668 int charpos = glyph->charpos;
21669
21670 if (BUFFERP (glyph->object))
21671 {
21672 if (charpos == pos)
21673 {
21674 *hpos = i;
21675 *vpos = best_row_vpos;
21676 *x = current_x;
21677 *y = best_row->y;
21678 return 1;
21679 }
21680 else if (charpos > pos)
21681 break;
21682 }
21683 else if (EQ (glyph->object, stop))
21684 break;
21685
21686 if (charpos > 0)
21687 lastcol = i;
21688 current_x += glyph->pixel_width;
21689 }
21690
21691 /* If we're looking for the end of the buffer,
21692 and we didn't find it in the line we scanned,
21693 use the start of the following line. */
21694 if (maybe_next_line_p)
21695 {
21696 ++best_row;
21697 ++best_row_vpos;
21698 lastcol = 0;
21699 current_x = best_row->x;
21700 }
21701
21702 *vpos = best_row_vpos;
21703 *hpos = lastcol + 1;
21704 *x = current_x;
21705 *y = best_row->y;
21706 return 0;
21707 }
21708
21709 #endif /* not 1 */
21710
21711
21712 /* Find the position of the glyph for position POS in OBJECT in
21713 window W's current matrix, and return in *X, *Y the pixel
21714 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21715
21716 RIGHT_P non-zero means return the position of the right edge of the
21717 glyph, RIGHT_P zero means return the left edge position.
21718
21719 If no glyph for POS exists in the matrix, return the position of
21720 the glyph with the next smaller position that is in the matrix, if
21721 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21722 exists in the matrix, return the position of the glyph with the
21723 next larger position in OBJECT.
21724
21725 Value is non-zero if a glyph was found. */
21726
21727 static int
21728 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21729 struct window *w;
21730 int pos;
21731 Lisp_Object object;
21732 int *hpos, *vpos, *x, *y;
21733 int right_p;
21734 {
21735 int yb = window_text_bottom_y (w);
21736 struct glyph_row *r;
21737 struct glyph *best_glyph = NULL;
21738 struct glyph_row *best_row = NULL;
21739 int best_x = 0;
21740
21741 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21742 r->enabled_p && r->y < yb;
21743 ++r)
21744 {
21745 struct glyph *g = r->glyphs[TEXT_AREA];
21746 struct glyph *e = g + r->used[TEXT_AREA];
21747 int gx;
21748
21749 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21750 if (EQ (g->object, object))
21751 {
21752 if (g->charpos == pos)
21753 {
21754 best_glyph = g;
21755 best_x = gx;
21756 best_row = r;
21757 goto found;
21758 }
21759 else if (best_glyph == NULL
21760 || ((abs (g->charpos - pos)
21761 < abs (best_glyph->charpos - pos))
21762 && (right_p
21763 ? g->charpos < pos
21764 : g->charpos > pos)))
21765 {
21766 best_glyph = g;
21767 best_x = gx;
21768 best_row = r;
21769 }
21770 }
21771 }
21772
21773 found:
21774
21775 if (best_glyph)
21776 {
21777 *x = best_x;
21778 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21779
21780 if (right_p)
21781 {
21782 *x += best_glyph->pixel_width;
21783 ++*hpos;
21784 }
21785
21786 *y = best_row->y;
21787 *vpos = best_row - w->current_matrix->rows;
21788 }
21789
21790 return best_glyph != NULL;
21791 }
21792
21793
21794 /* See if position X, Y is within a hot-spot of an image. */
21795
21796 static int
21797 on_hot_spot_p (hot_spot, x, y)
21798 Lisp_Object hot_spot;
21799 int x, y;
21800 {
21801 if (!CONSP (hot_spot))
21802 return 0;
21803
21804 if (EQ (XCAR (hot_spot), Qrect))
21805 {
21806 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21807 Lisp_Object rect = XCDR (hot_spot);
21808 Lisp_Object tem;
21809 if (!CONSP (rect))
21810 return 0;
21811 if (!CONSP (XCAR (rect)))
21812 return 0;
21813 if (!CONSP (XCDR (rect)))
21814 return 0;
21815 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21816 return 0;
21817 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21818 return 0;
21819 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21820 return 0;
21821 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21822 return 0;
21823 return 1;
21824 }
21825 else if (EQ (XCAR (hot_spot), Qcircle))
21826 {
21827 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21828 Lisp_Object circ = XCDR (hot_spot);
21829 Lisp_Object lr, lx0, ly0;
21830 if (CONSP (circ)
21831 && CONSP (XCAR (circ))
21832 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21833 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21834 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21835 {
21836 double r = XFLOATINT (lr);
21837 double dx = XINT (lx0) - x;
21838 double dy = XINT (ly0) - y;
21839 return (dx * dx + dy * dy <= r * r);
21840 }
21841 }
21842 else if (EQ (XCAR (hot_spot), Qpoly))
21843 {
21844 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21845 if (VECTORP (XCDR (hot_spot)))
21846 {
21847 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21848 Lisp_Object *poly = v->contents;
21849 int n = v->size;
21850 int i;
21851 int inside = 0;
21852 Lisp_Object lx, ly;
21853 int x0, y0;
21854
21855 /* Need an even number of coordinates, and at least 3 edges. */
21856 if (n < 6 || n & 1)
21857 return 0;
21858
21859 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21860 If count is odd, we are inside polygon. Pixels on edges
21861 may or may not be included depending on actual geometry of the
21862 polygon. */
21863 if ((lx = poly[n-2], !INTEGERP (lx))
21864 || (ly = poly[n-1], !INTEGERP (lx)))
21865 return 0;
21866 x0 = XINT (lx), y0 = XINT (ly);
21867 for (i = 0; i < n; i += 2)
21868 {
21869 int x1 = x0, y1 = y0;
21870 if ((lx = poly[i], !INTEGERP (lx))
21871 || (ly = poly[i+1], !INTEGERP (ly)))
21872 return 0;
21873 x0 = XINT (lx), y0 = XINT (ly);
21874
21875 /* Does this segment cross the X line? */
21876 if (x0 >= x)
21877 {
21878 if (x1 >= x)
21879 continue;
21880 }
21881 else if (x1 < x)
21882 continue;
21883 if (y > y0 && y > y1)
21884 continue;
21885 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21886 inside = !inside;
21887 }
21888 return inside;
21889 }
21890 }
21891 return 0;
21892 }
21893
21894 Lisp_Object
21895 find_hot_spot (map, x, y)
21896 Lisp_Object map;
21897 int x, y;
21898 {
21899 while (CONSP (map))
21900 {
21901 if (CONSP (XCAR (map))
21902 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21903 return XCAR (map);
21904 map = XCDR (map);
21905 }
21906
21907 return Qnil;
21908 }
21909
21910 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21911 3, 3, 0,
21912 doc: /* Lookup in image map MAP coordinates X and Y.
21913 An image map is an alist where each element has the format (AREA ID PLIST).
21914 An AREA is specified as either a rectangle, a circle, or a polygon:
21915 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21916 pixel coordinates of the upper left and bottom right corners.
21917 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21918 and the radius of the circle; r may be a float or integer.
21919 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21920 vector describes one corner in the polygon.
21921 Returns the alist element for the first matching AREA in MAP. */)
21922 (map, x, y)
21923 Lisp_Object map;
21924 Lisp_Object x, y;
21925 {
21926 if (NILP (map))
21927 return Qnil;
21928
21929 CHECK_NUMBER (x);
21930 CHECK_NUMBER (y);
21931
21932 return find_hot_spot (map, XINT (x), XINT (y));
21933 }
21934
21935
21936 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21937 static void
21938 define_frame_cursor1 (f, cursor, pointer)
21939 struct frame *f;
21940 Cursor cursor;
21941 Lisp_Object pointer;
21942 {
21943 /* Do not change cursor shape while dragging mouse. */
21944 if (!NILP (do_mouse_tracking))
21945 return;
21946
21947 if (!NILP (pointer))
21948 {
21949 if (EQ (pointer, Qarrow))
21950 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21951 else if (EQ (pointer, Qhand))
21952 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21953 else if (EQ (pointer, Qtext))
21954 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21955 else if (EQ (pointer, intern ("hdrag")))
21956 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21957 #ifdef HAVE_X_WINDOWS
21958 else if (EQ (pointer, intern ("vdrag")))
21959 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21960 #endif
21961 else if (EQ (pointer, intern ("hourglass")))
21962 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21963 else if (EQ (pointer, Qmodeline))
21964 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21965 else
21966 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21967 }
21968
21969 if (cursor != No_Cursor)
21970 FRAME_RIF (f)->define_frame_cursor (f, cursor);
21971 }
21972
21973 /* Take proper action when mouse has moved to the mode or header line
21974 or marginal area AREA of window W, x-position X and y-position Y.
21975 X is relative to the start of the text display area of W, so the
21976 width of bitmap areas and scroll bars must be subtracted to get a
21977 position relative to the start of the mode line. */
21978
21979 static void
21980 note_mode_line_or_margin_highlight (window, x, y, area)
21981 Lisp_Object window;
21982 int x, y;
21983 enum window_part area;
21984 {
21985 struct window *w = XWINDOW (window);
21986 struct frame *f = XFRAME (w->frame);
21987 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21988 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21989 Lisp_Object pointer = Qnil;
21990 int charpos, dx, dy, width, height;
21991 Lisp_Object string, object = Qnil;
21992 Lisp_Object pos, help;
21993
21994 Lisp_Object mouse_face;
21995 int original_x_pixel = x;
21996 struct glyph * glyph = NULL;
21997 struct glyph_row *row;
21998
21999 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22000 {
22001 int x0;
22002 struct glyph *end;
22003
22004 string = mode_line_string (w, area, &x, &y, &charpos,
22005 &object, &dx, &dy, &width, &height);
22006
22007 row = (area == ON_MODE_LINE
22008 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22009 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22010
22011 /* Find glyph */
22012 if (row->mode_line_p && row->enabled_p)
22013 {
22014 glyph = row->glyphs[TEXT_AREA];
22015 end = glyph + row->used[TEXT_AREA];
22016
22017 for (x0 = original_x_pixel;
22018 glyph < end && x0 >= glyph->pixel_width;
22019 ++glyph)
22020 x0 -= glyph->pixel_width;
22021
22022 if (glyph >= end)
22023 glyph = NULL;
22024 }
22025 }
22026 else
22027 {
22028 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22029 string = marginal_area_string (w, area, &x, &y, &charpos,
22030 &object, &dx, &dy, &width, &height);
22031 }
22032
22033 help = Qnil;
22034
22035 if (IMAGEP (object))
22036 {
22037 Lisp_Object image_map, hotspot;
22038 if ((image_map = Fplist_get (XCDR (object), QCmap),
22039 !NILP (image_map))
22040 && (hotspot = find_hot_spot (image_map, dx, dy),
22041 CONSP (hotspot))
22042 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22043 {
22044 Lisp_Object area_id, plist;
22045
22046 area_id = XCAR (hotspot);
22047 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22048 If so, we could look for mouse-enter, mouse-leave
22049 properties in PLIST (and do something...). */
22050 hotspot = XCDR (hotspot);
22051 if (CONSP (hotspot)
22052 && (plist = XCAR (hotspot), CONSP (plist)))
22053 {
22054 pointer = Fplist_get (plist, Qpointer);
22055 if (NILP (pointer))
22056 pointer = Qhand;
22057 help = Fplist_get (plist, Qhelp_echo);
22058 if (!NILP (help))
22059 {
22060 help_echo_string = help;
22061 /* Is this correct? ++kfs */
22062 XSETWINDOW (help_echo_window, w);
22063 help_echo_object = w->buffer;
22064 help_echo_pos = charpos;
22065 }
22066 }
22067 }
22068 if (NILP (pointer))
22069 pointer = Fplist_get (XCDR (object), QCpointer);
22070 }
22071
22072 if (STRINGP (string))
22073 {
22074 pos = make_number (charpos);
22075 /* If we're on a string with `help-echo' text property, arrange
22076 for the help to be displayed. This is done by setting the
22077 global variable help_echo_string to the help string. */
22078 if (NILP (help))
22079 {
22080 help = Fget_text_property (pos, Qhelp_echo, string);
22081 if (!NILP (help))
22082 {
22083 help_echo_string = help;
22084 XSETWINDOW (help_echo_window, w);
22085 help_echo_object = string;
22086 help_echo_pos = charpos;
22087 }
22088 }
22089
22090 if (NILP (pointer))
22091 pointer = Fget_text_property (pos, Qpointer, string);
22092
22093 /* Change the mouse pointer according to what is under X/Y. */
22094 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22095 {
22096 Lisp_Object map;
22097 map = Fget_text_property (pos, Qlocal_map, string);
22098 if (!KEYMAPP (map))
22099 map = Fget_text_property (pos, Qkeymap, string);
22100 if (!KEYMAPP (map))
22101 cursor = dpyinfo->vertical_scroll_bar_cursor;
22102 }
22103
22104 /* Change the mouse face according to what is under X/Y. */
22105 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22106 if (!NILP (mouse_face)
22107 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22108 && glyph)
22109 {
22110 Lisp_Object b, e;
22111
22112 struct glyph * tmp_glyph;
22113
22114 int gpos;
22115 int gseq_length;
22116 int total_pixel_width;
22117 int ignore;
22118
22119 int vpos, hpos;
22120
22121 b = Fprevious_single_property_change (make_number (charpos + 1),
22122 Qmouse_face, string, Qnil);
22123 if (NILP (b))
22124 b = make_number (0);
22125
22126 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22127 if (NILP (e))
22128 e = make_number (SCHARS (string));
22129
22130 /* Calculate the position(glyph position: GPOS) of GLYPH in
22131 displayed string. GPOS is different from CHARPOS.
22132
22133 CHARPOS is the position of glyph in internal string
22134 object. A mode line string format has structures which
22135 is converted to a flatten by emacs lisp interpreter.
22136 The internal string is an element of the structures.
22137 The displayed string is the flatten string. */
22138 for (tmp_glyph = glyph - 1, gpos = 0;
22139 tmp_glyph->charpos >= XINT (b);
22140 tmp_glyph--, gpos++)
22141 {
22142 if (!EQ (tmp_glyph->object, glyph->object))
22143 break;
22144 }
22145
22146 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22147 displayed string holding GLYPH.
22148
22149 GSEQ_LENGTH is different from SCHARS (STRING).
22150 SCHARS (STRING) returns the length of the internal string. */
22151 for (tmp_glyph = glyph, gseq_length = gpos;
22152 tmp_glyph->charpos < XINT (e);
22153 tmp_glyph++, gseq_length++)
22154 {
22155 if (!EQ (tmp_glyph->object, glyph->object))
22156 break;
22157 }
22158
22159 total_pixel_width = 0;
22160 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22161 total_pixel_width += tmp_glyph->pixel_width;
22162
22163 /* Pre calculation of re-rendering position */
22164 vpos = (x - gpos);
22165 hpos = (area == ON_MODE_LINE
22166 ? (w->current_matrix)->nrows - 1
22167 : 0);
22168
22169 /* If the re-rendering position is included in the last
22170 re-rendering area, we should do nothing. */
22171 if ( EQ (window, dpyinfo->mouse_face_window)
22172 && dpyinfo->mouse_face_beg_col <= vpos
22173 && vpos < dpyinfo->mouse_face_end_col
22174 && dpyinfo->mouse_face_beg_row == hpos )
22175 return;
22176
22177 if (clear_mouse_face (dpyinfo))
22178 cursor = No_Cursor;
22179
22180 dpyinfo->mouse_face_beg_col = vpos;
22181 dpyinfo->mouse_face_beg_row = hpos;
22182
22183 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22184 dpyinfo->mouse_face_beg_y = 0;
22185
22186 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22187 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22188
22189 dpyinfo->mouse_face_end_x = 0;
22190 dpyinfo->mouse_face_end_y = 0;
22191
22192 dpyinfo->mouse_face_past_end = 0;
22193 dpyinfo->mouse_face_window = window;
22194
22195 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22196 charpos,
22197 0, 0, 0, &ignore,
22198 glyph->face_id, 1);
22199 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22200
22201 if (NILP (pointer))
22202 pointer = Qhand;
22203 }
22204 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22205 clear_mouse_face (dpyinfo);
22206 }
22207 define_frame_cursor1 (f, cursor, pointer);
22208 }
22209
22210
22211 /* EXPORT:
22212 Take proper action when the mouse has moved to position X, Y on
22213 frame F as regards highlighting characters that have mouse-face
22214 properties. Also de-highlighting chars where the mouse was before.
22215 X and Y can be negative or out of range. */
22216
22217 void
22218 note_mouse_highlight (f, x, y)
22219 struct frame *f;
22220 int x, y;
22221 {
22222 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22223 enum window_part part;
22224 Lisp_Object window;
22225 struct window *w;
22226 Cursor cursor = No_Cursor;
22227 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22228 struct buffer *b;
22229
22230 /* When a menu is active, don't highlight because this looks odd. */
22231 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22232 if (popup_activated ())
22233 return;
22234 #endif
22235
22236 if (NILP (Vmouse_highlight)
22237 || !f->glyphs_initialized_p)
22238 return;
22239
22240 dpyinfo->mouse_face_mouse_x = x;
22241 dpyinfo->mouse_face_mouse_y = y;
22242 dpyinfo->mouse_face_mouse_frame = f;
22243
22244 if (dpyinfo->mouse_face_defer)
22245 return;
22246
22247 if (gc_in_progress)
22248 {
22249 dpyinfo->mouse_face_deferred_gc = 1;
22250 return;
22251 }
22252
22253 /* Which window is that in? */
22254 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22255
22256 /* If we were displaying active text in another window, clear that.
22257 Also clear if we move out of text area in same window. */
22258 if (! EQ (window, dpyinfo->mouse_face_window)
22259 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22260 && !NILP (dpyinfo->mouse_face_window)))
22261 clear_mouse_face (dpyinfo);
22262
22263 /* Not on a window -> return. */
22264 if (!WINDOWP (window))
22265 return;
22266
22267 /* Reset help_echo_string. It will get recomputed below. */
22268 help_echo_string = Qnil;
22269
22270 /* Convert to window-relative pixel coordinates. */
22271 w = XWINDOW (window);
22272 frame_to_window_pixel_xy (w, &x, &y);
22273
22274 /* Handle tool-bar window differently since it doesn't display a
22275 buffer. */
22276 if (EQ (window, f->tool_bar_window))
22277 {
22278 note_tool_bar_highlight (f, x, y);
22279 return;
22280 }
22281
22282 /* Mouse is on the mode, header line or margin? */
22283 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22284 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22285 {
22286 note_mode_line_or_margin_highlight (window, x, y, part);
22287 return;
22288 }
22289
22290 if (part == ON_VERTICAL_BORDER)
22291 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22292 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22293 || part == ON_SCROLL_BAR)
22294 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22295 else
22296 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22297
22298 /* Are we in a window whose display is up to date?
22299 And verify the buffer's text has not changed. */
22300 b = XBUFFER (w->buffer);
22301 if (part == ON_TEXT
22302 && EQ (w->window_end_valid, w->buffer)
22303 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22304 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22305 {
22306 int hpos, vpos, pos, i, dx, dy, area;
22307 struct glyph *glyph;
22308 Lisp_Object object;
22309 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22310 Lisp_Object *overlay_vec = NULL;
22311 int noverlays;
22312 struct buffer *obuf;
22313 int obegv, ozv, same_region;
22314
22315 /* Find the glyph under X/Y. */
22316 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22317
22318 /* Look for :pointer property on image. */
22319 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22320 {
22321 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22322 if (img != NULL && IMAGEP (img->spec))
22323 {
22324 Lisp_Object image_map, hotspot;
22325 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22326 !NILP (image_map))
22327 && (hotspot = find_hot_spot (image_map,
22328 glyph->slice.x + dx,
22329 glyph->slice.y + dy),
22330 CONSP (hotspot))
22331 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22332 {
22333 Lisp_Object area_id, plist;
22334
22335 area_id = XCAR (hotspot);
22336 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22337 If so, we could look for mouse-enter, mouse-leave
22338 properties in PLIST (and do something...). */
22339 hotspot = XCDR (hotspot);
22340 if (CONSP (hotspot)
22341 && (plist = XCAR (hotspot), CONSP (plist)))
22342 {
22343 pointer = Fplist_get (plist, Qpointer);
22344 if (NILP (pointer))
22345 pointer = Qhand;
22346 help_echo_string = Fplist_get (plist, Qhelp_echo);
22347 if (!NILP (help_echo_string))
22348 {
22349 help_echo_window = window;
22350 help_echo_object = glyph->object;
22351 help_echo_pos = glyph->charpos;
22352 }
22353 }
22354 }
22355 if (NILP (pointer))
22356 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22357 }
22358 }
22359
22360 /* Clear mouse face if X/Y not over text. */
22361 if (glyph == NULL
22362 || area != TEXT_AREA
22363 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22364 {
22365 if (clear_mouse_face (dpyinfo))
22366 cursor = No_Cursor;
22367 if (NILP (pointer))
22368 {
22369 if (area != TEXT_AREA)
22370 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22371 else
22372 pointer = Vvoid_text_area_pointer;
22373 }
22374 goto set_cursor;
22375 }
22376
22377 pos = glyph->charpos;
22378 object = glyph->object;
22379 if (!STRINGP (object) && !BUFFERP (object))
22380 goto set_cursor;
22381
22382 /* If we get an out-of-range value, return now; avoid an error. */
22383 if (BUFFERP (object) && pos > BUF_Z (b))
22384 goto set_cursor;
22385
22386 /* Make the window's buffer temporarily current for
22387 overlays_at and compute_char_face. */
22388 obuf = current_buffer;
22389 current_buffer = b;
22390 obegv = BEGV;
22391 ozv = ZV;
22392 BEGV = BEG;
22393 ZV = Z;
22394
22395 /* Is this char mouse-active or does it have help-echo? */
22396 position = make_number (pos);
22397
22398 if (BUFFERP (object))
22399 {
22400 /* Put all the overlays we want in a vector in overlay_vec. */
22401 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22402 /* Sort overlays into increasing priority order. */
22403 noverlays = sort_overlays (overlay_vec, noverlays, w);
22404 }
22405 else
22406 noverlays = 0;
22407
22408 same_region = (EQ (window, dpyinfo->mouse_face_window)
22409 && vpos >= dpyinfo->mouse_face_beg_row
22410 && vpos <= dpyinfo->mouse_face_end_row
22411 && (vpos > dpyinfo->mouse_face_beg_row
22412 || hpos >= dpyinfo->mouse_face_beg_col)
22413 && (vpos < dpyinfo->mouse_face_end_row
22414 || hpos < dpyinfo->mouse_face_end_col
22415 || dpyinfo->mouse_face_past_end));
22416
22417 if (same_region)
22418 cursor = No_Cursor;
22419
22420 /* Check mouse-face highlighting. */
22421 if (! same_region
22422 /* If there exists an overlay with mouse-face overlapping
22423 the one we are currently highlighting, we have to
22424 check if we enter the overlapping overlay, and then
22425 highlight only that. */
22426 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22427 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22428 {
22429 /* Find the highest priority overlay that has a mouse-face
22430 property. */
22431 overlay = Qnil;
22432 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22433 {
22434 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22435 if (!NILP (mouse_face))
22436 overlay = overlay_vec[i];
22437 }
22438
22439 /* If we're actually highlighting the same overlay as
22440 before, there's no need to do that again. */
22441 if (!NILP (overlay)
22442 && EQ (overlay, dpyinfo->mouse_face_overlay))
22443 goto check_help_echo;
22444
22445 dpyinfo->mouse_face_overlay = overlay;
22446
22447 /* Clear the display of the old active region, if any. */
22448 if (clear_mouse_face (dpyinfo))
22449 cursor = No_Cursor;
22450
22451 /* If no overlay applies, get a text property. */
22452 if (NILP (overlay))
22453 mouse_face = Fget_text_property (position, Qmouse_face, object);
22454
22455 /* Handle the overlay case. */
22456 if (!NILP (overlay))
22457 {
22458 /* Find the range of text around this char that
22459 should be active. */
22460 Lisp_Object before, after;
22461 int ignore;
22462
22463 before = Foverlay_start (overlay);
22464 after = Foverlay_end (overlay);
22465 /* Record this as the current active region. */
22466 fast_find_position (w, XFASTINT (before),
22467 &dpyinfo->mouse_face_beg_col,
22468 &dpyinfo->mouse_face_beg_row,
22469 &dpyinfo->mouse_face_beg_x,
22470 &dpyinfo->mouse_face_beg_y, Qnil);
22471
22472 dpyinfo->mouse_face_past_end
22473 = !fast_find_position (w, XFASTINT (after),
22474 &dpyinfo->mouse_face_end_col,
22475 &dpyinfo->mouse_face_end_row,
22476 &dpyinfo->mouse_face_end_x,
22477 &dpyinfo->mouse_face_end_y, Qnil);
22478 dpyinfo->mouse_face_window = window;
22479
22480 dpyinfo->mouse_face_face_id
22481 = face_at_buffer_position (w, pos, 0, 0,
22482 &ignore, pos + 1,
22483 !dpyinfo->mouse_face_hidden);
22484
22485 /* Display it as active. */
22486 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22487 cursor = No_Cursor;
22488 }
22489 /* Handle the text property case. */
22490 else if (!NILP (mouse_face) && BUFFERP (object))
22491 {
22492 /* Find the range of text around this char that
22493 should be active. */
22494 Lisp_Object before, after, beginning, end;
22495 int ignore;
22496
22497 beginning = Fmarker_position (w->start);
22498 end = make_number (BUF_Z (XBUFFER (object))
22499 - XFASTINT (w->window_end_pos));
22500 before
22501 = Fprevious_single_property_change (make_number (pos + 1),
22502 Qmouse_face,
22503 object, beginning);
22504 after
22505 = Fnext_single_property_change (position, Qmouse_face,
22506 object, end);
22507
22508 /* Record this as the current active region. */
22509 fast_find_position (w, XFASTINT (before),
22510 &dpyinfo->mouse_face_beg_col,
22511 &dpyinfo->mouse_face_beg_row,
22512 &dpyinfo->mouse_face_beg_x,
22513 &dpyinfo->mouse_face_beg_y, Qnil);
22514 dpyinfo->mouse_face_past_end
22515 = !fast_find_position (w, XFASTINT (after),
22516 &dpyinfo->mouse_face_end_col,
22517 &dpyinfo->mouse_face_end_row,
22518 &dpyinfo->mouse_face_end_x,
22519 &dpyinfo->mouse_face_end_y, Qnil);
22520 dpyinfo->mouse_face_window = window;
22521
22522 if (BUFFERP (object))
22523 dpyinfo->mouse_face_face_id
22524 = face_at_buffer_position (w, pos, 0, 0,
22525 &ignore, pos + 1,
22526 !dpyinfo->mouse_face_hidden);
22527
22528 /* Display it as active. */
22529 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22530 cursor = No_Cursor;
22531 }
22532 else if (!NILP (mouse_face) && STRINGP (object))
22533 {
22534 Lisp_Object b, e;
22535 int ignore;
22536
22537 b = Fprevious_single_property_change (make_number (pos + 1),
22538 Qmouse_face,
22539 object, Qnil);
22540 e = Fnext_single_property_change (position, Qmouse_face,
22541 object, Qnil);
22542 if (NILP (b))
22543 b = make_number (0);
22544 if (NILP (e))
22545 e = make_number (SCHARS (object) - 1);
22546
22547 fast_find_string_pos (w, XINT (b), object,
22548 &dpyinfo->mouse_face_beg_col,
22549 &dpyinfo->mouse_face_beg_row,
22550 &dpyinfo->mouse_face_beg_x,
22551 &dpyinfo->mouse_face_beg_y, 0);
22552 fast_find_string_pos (w, XINT (e), object,
22553 &dpyinfo->mouse_face_end_col,
22554 &dpyinfo->mouse_face_end_row,
22555 &dpyinfo->mouse_face_end_x,
22556 &dpyinfo->mouse_face_end_y, 1);
22557 dpyinfo->mouse_face_past_end = 0;
22558 dpyinfo->mouse_face_window = window;
22559 dpyinfo->mouse_face_face_id
22560 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22561 glyph->face_id, 1);
22562 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22563 cursor = No_Cursor;
22564 }
22565 else if (STRINGP (object) && NILP (mouse_face))
22566 {
22567 /* A string which doesn't have mouse-face, but
22568 the text ``under'' it might have. */
22569 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22570 int start = MATRIX_ROW_START_CHARPOS (r);
22571
22572 pos = string_buffer_position (w, object, start);
22573 if (pos > 0)
22574 mouse_face = get_char_property_and_overlay (make_number (pos),
22575 Qmouse_face,
22576 w->buffer,
22577 &overlay);
22578 if (!NILP (mouse_face) && !NILP (overlay))
22579 {
22580 Lisp_Object before = Foverlay_start (overlay);
22581 Lisp_Object after = Foverlay_end (overlay);
22582 int ignore;
22583
22584 /* Note that we might not be able to find position
22585 BEFORE in the glyph matrix if the overlay is
22586 entirely covered by a `display' property. In
22587 this case, we overshoot. So let's stop in
22588 the glyph matrix before glyphs for OBJECT. */
22589 fast_find_position (w, XFASTINT (before),
22590 &dpyinfo->mouse_face_beg_col,
22591 &dpyinfo->mouse_face_beg_row,
22592 &dpyinfo->mouse_face_beg_x,
22593 &dpyinfo->mouse_face_beg_y,
22594 object);
22595
22596 dpyinfo->mouse_face_past_end
22597 = !fast_find_position (w, XFASTINT (after),
22598 &dpyinfo->mouse_face_end_col,
22599 &dpyinfo->mouse_face_end_row,
22600 &dpyinfo->mouse_face_end_x,
22601 &dpyinfo->mouse_face_end_y,
22602 Qnil);
22603 dpyinfo->mouse_face_window = window;
22604 dpyinfo->mouse_face_face_id
22605 = face_at_buffer_position (w, pos, 0, 0,
22606 &ignore, pos + 1,
22607 !dpyinfo->mouse_face_hidden);
22608
22609 /* Display it as active. */
22610 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22611 cursor = No_Cursor;
22612 }
22613 }
22614 }
22615
22616 check_help_echo:
22617
22618 /* Look for a `help-echo' property. */
22619 if (NILP (help_echo_string)) {
22620 Lisp_Object help, overlay;
22621
22622 /* Check overlays first. */
22623 help = overlay = Qnil;
22624 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22625 {
22626 overlay = overlay_vec[i];
22627 help = Foverlay_get (overlay, Qhelp_echo);
22628 }
22629
22630 if (!NILP (help))
22631 {
22632 help_echo_string = help;
22633 help_echo_window = window;
22634 help_echo_object = overlay;
22635 help_echo_pos = pos;
22636 }
22637 else
22638 {
22639 Lisp_Object object = glyph->object;
22640 int charpos = glyph->charpos;
22641
22642 /* Try text properties. */
22643 if (STRINGP (object)
22644 && charpos >= 0
22645 && charpos < SCHARS (object))
22646 {
22647 help = Fget_text_property (make_number (charpos),
22648 Qhelp_echo, object);
22649 if (NILP (help))
22650 {
22651 /* If the string itself doesn't specify a help-echo,
22652 see if the buffer text ``under'' it does. */
22653 struct glyph_row *r
22654 = MATRIX_ROW (w->current_matrix, vpos);
22655 int start = MATRIX_ROW_START_CHARPOS (r);
22656 int pos = string_buffer_position (w, object, start);
22657 if (pos > 0)
22658 {
22659 help = Fget_char_property (make_number (pos),
22660 Qhelp_echo, w->buffer);
22661 if (!NILP (help))
22662 {
22663 charpos = pos;
22664 object = w->buffer;
22665 }
22666 }
22667 }
22668 }
22669 else if (BUFFERP (object)
22670 && charpos >= BEGV
22671 && charpos < ZV)
22672 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22673 object);
22674
22675 if (!NILP (help))
22676 {
22677 help_echo_string = help;
22678 help_echo_window = window;
22679 help_echo_object = object;
22680 help_echo_pos = charpos;
22681 }
22682 }
22683 }
22684
22685 /* Look for a `pointer' property. */
22686 if (NILP (pointer))
22687 {
22688 /* Check overlays first. */
22689 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22690 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22691
22692 if (NILP (pointer))
22693 {
22694 Lisp_Object object = glyph->object;
22695 int charpos = glyph->charpos;
22696
22697 /* Try text properties. */
22698 if (STRINGP (object)
22699 && charpos >= 0
22700 && charpos < SCHARS (object))
22701 {
22702 pointer = Fget_text_property (make_number (charpos),
22703 Qpointer, object);
22704 if (NILP (pointer))
22705 {
22706 /* If the string itself doesn't specify a pointer,
22707 see if the buffer text ``under'' it does. */
22708 struct glyph_row *r
22709 = MATRIX_ROW (w->current_matrix, vpos);
22710 int start = MATRIX_ROW_START_CHARPOS (r);
22711 int pos = string_buffer_position (w, object, start);
22712 if (pos > 0)
22713 pointer = Fget_char_property (make_number (pos),
22714 Qpointer, w->buffer);
22715 }
22716 }
22717 else if (BUFFERP (object)
22718 && charpos >= BEGV
22719 && charpos < ZV)
22720 pointer = Fget_text_property (make_number (charpos),
22721 Qpointer, object);
22722 }
22723 }
22724
22725 BEGV = obegv;
22726 ZV = ozv;
22727 current_buffer = obuf;
22728 }
22729
22730 set_cursor:
22731
22732 define_frame_cursor1 (f, cursor, pointer);
22733 }
22734
22735
22736 /* EXPORT for RIF:
22737 Clear any mouse-face on window W. This function is part of the
22738 redisplay interface, and is called from try_window_id and similar
22739 functions to ensure the mouse-highlight is off. */
22740
22741 void
22742 x_clear_window_mouse_face (w)
22743 struct window *w;
22744 {
22745 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22746 Lisp_Object window;
22747
22748 BLOCK_INPUT;
22749 XSETWINDOW (window, w);
22750 if (EQ (window, dpyinfo->mouse_face_window))
22751 clear_mouse_face (dpyinfo);
22752 UNBLOCK_INPUT;
22753 }
22754
22755
22756 /* EXPORT:
22757 Just discard the mouse face information for frame F, if any.
22758 This is used when the size of F is changed. */
22759
22760 void
22761 cancel_mouse_face (f)
22762 struct frame *f;
22763 {
22764 Lisp_Object window;
22765 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22766
22767 window = dpyinfo->mouse_face_window;
22768 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22769 {
22770 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22771 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22772 dpyinfo->mouse_face_window = Qnil;
22773 }
22774 }
22775
22776
22777 #endif /* HAVE_WINDOW_SYSTEM */
22778
22779 \f
22780 /***********************************************************************
22781 Exposure Events
22782 ***********************************************************************/
22783
22784 #ifdef HAVE_WINDOW_SYSTEM
22785
22786 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22787 which intersects rectangle R. R is in window-relative coordinates. */
22788
22789 static void
22790 expose_area (w, row, r, area)
22791 struct window *w;
22792 struct glyph_row *row;
22793 XRectangle *r;
22794 enum glyph_row_area area;
22795 {
22796 struct glyph *first = row->glyphs[area];
22797 struct glyph *end = row->glyphs[area] + row->used[area];
22798 struct glyph *last;
22799 int first_x, start_x, x;
22800
22801 if (area == TEXT_AREA && row->fill_line_p)
22802 /* If row extends face to end of line write the whole line. */
22803 draw_glyphs (w, 0, row, area,
22804 0, row->used[area],
22805 DRAW_NORMAL_TEXT, 0);
22806 else
22807 {
22808 /* Set START_X to the window-relative start position for drawing glyphs of
22809 AREA. The first glyph of the text area can be partially visible.
22810 The first glyphs of other areas cannot. */
22811 start_x = window_box_left_offset (w, area);
22812 x = start_x;
22813 if (area == TEXT_AREA)
22814 x += row->x;
22815
22816 /* Find the first glyph that must be redrawn. */
22817 while (first < end
22818 && x + first->pixel_width < r->x)
22819 {
22820 x += first->pixel_width;
22821 ++first;
22822 }
22823
22824 /* Find the last one. */
22825 last = first;
22826 first_x = x;
22827 while (last < end
22828 && x < r->x + r->width)
22829 {
22830 x += last->pixel_width;
22831 ++last;
22832 }
22833
22834 /* Repaint. */
22835 if (last > first)
22836 draw_glyphs (w, first_x - start_x, row, area,
22837 first - row->glyphs[area], last - row->glyphs[area],
22838 DRAW_NORMAL_TEXT, 0);
22839 }
22840 }
22841
22842
22843 /* Redraw the parts of the glyph row ROW on window W intersecting
22844 rectangle R. R is in window-relative coordinates. Value is
22845 non-zero if mouse-face was overwritten. */
22846
22847 static int
22848 expose_line (w, row, r)
22849 struct window *w;
22850 struct glyph_row *row;
22851 XRectangle *r;
22852 {
22853 xassert (row->enabled_p);
22854
22855 if (row->mode_line_p || w->pseudo_window_p)
22856 draw_glyphs (w, 0, row, TEXT_AREA,
22857 0, row->used[TEXT_AREA],
22858 DRAW_NORMAL_TEXT, 0);
22859 else
22860 {
22861 if (row->used[LEFT_MARGIN_AREA])
22862 expose_area (w, row, r, LEFT_MARGIN_AREA);
22863 if (row->used[TEXT_AREA])
22864 expose_area (w, row, r, TEXT_AREA);
22865 if (row->used[RIGHT_MARGIN_AREA])
22866 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22867 draw_row_fringe_bitmaps (w, row);
22868 }
22869
22870 return row->mouse_face_p;
22871 }
22872
22873
22874 /* Redraw those parts of glyphs rows during expose event handling that
22875 overlap other rows. Redrawing of an exposed line writes over parts
22876 of lines overlapping that exposed line; this function fixes that.
22877
22878 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22879 row in W's current matrix that is exposed and overlaps other rows.
22880 LAST_OVERLAPPING_ROW is the last such row. */
22881
22882 static void
22883 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22884 struct window *w;
22885 struct glyph_row *first_overlapping_row;
22886 struct glyph_row *last_overlapping_row;
22887 {
22888 struct glyph_row *row;
22889
22890 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22891 if (row->overlapping_p)
22892 {
22893 xassert (row->enabled_p && !row->mode_line_p);
22894
22895 if (row->used[LEFT_MARGIN_AREA])
22896 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22897
22898 if (row->used[TEXT_AREA])
22899 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22900
22901 if (row->used[RIGHT_MARGIN_AREA])
22902 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22903 }
22904 }
22905
22906
22907 /* Return non-zero if W's cursor intersects rectangle R. */
22908
22909 static int
22910 phys_cursor_in_rect_p (w, r)
22911 struct window *w;
22912 XRectangle *r;
22913 {
22914 XRectangle cr, result;
22915 struct glyph *cursor_glyph;
22916
22917 cursor_glyph = get_phys_cursor_glyph (w);
22918 if (cursor_glyph)
22919 {
22920 /* r is relative to W's box, but w->phys_cursor.x is relative
22921 to left edge of W's TEXT area. Adjust it. */
22922 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22923 cr.y = w->phys_cursor.y;
22924 cr.width = cursor_glyph->pixel_width;
22925 cr.height = w->phys_cursor_height;
22926 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22927 I assume the effect is the same -- and this is portable. */
22928 return x_intersect_rectangles (&cr, r, &result);
22929 }
22930 /* If we don't understand the format, pretend we're not in the hot-spot. */
22931 return 0;
22932 }
22933
22934
22935 /* EXPORT:
22936 Draw a vertical window border to the right of window W if W doesn't
22937 have vertical scroll bars. */
22938
22939 void
22940 x_draw_vertical_border (w)
22941 struct window *w;
22942 {
22943 struct frame *f = XFRAME (WINDOW_FRAME (w));
22944
22945 /* We could do better, if we knew what type of scroll-bar the adjacent
22946 windows (on either side) have... But we don't :-(
22947 However, I think this works ok. ++KFS 2003-04-25 */
22948
22949 /* Redraw borders between horizontally adjacent windows. Don't
22950 do it for frames with vertical scroll bars because either the
22951 right scroll bar of a window, or the left scroll bar of its
22952 neighbor will suffice as a border. */
22953 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22954 return;
22955
22956 if (!WINDOW_RIGHTMOST_P (w)
22957 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22958 {
22959 int x0, x1, y0, y1;
22960
22961 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22962 y1 -= 1;
22963
22964 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22965 x1 -= 1;
22966
22967 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
22968 }
22969 else if (!WINDOW_LEFTMOST_P (w)
22970 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22971 {
22972 int x0, x1, y0, y1;
22973
22974 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22975 y1 -= 1;
22976
22977 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22978 x0 -= 1;
22979
22980 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
22981 }
22982 }
22983
22984
22985 /* Redraw the part of window W intersection rectangle FR. Pixel
22986 coordinates in FR are frame-relative. Call this function with
22987 input blocked. Value is non-zero if the exposure overwrites
22988 mouse-face. */
22989
22990 static int
22991 expose_window (w, fr)
22992 struct window *w;
22993 XRectangle *fr;
22994 {
22995 struct frame *f = XFRAME (w->frame);
22996 XRectangle wr, r;
22997 int mouse_face_overwritten_p = 0;
22998
22999 /* If window is not yet fully initialized, do nothing. This can
23000 happen when toolkit scroll bars are used and a window is split.
23001 Reconfiguring the scroll bar will generate an expose for a newly
23002 created window. */
23003 if (w->current_matrix == NULL)
23004 return 0;
23005
23006 /* When we're currently updating the window, display and current
23007 matrix usually don't agree. Arrange for a thorough display
23008 later. */
23009 if (w == updated_window)
23010 {
23011 SET_FRAME_GARBAGED (f);
23012 return 0;
23013 }
23014
23015 /* Frame-relative pixel rectangle of W. */
23016 wr.x = WINDOW_LEFT_EDGE_X (w);
23017 wr.y = WINDOW_TOP_EDGE_Y (w);
23018 wr.width = WINDOW_TOTAL_WIDTH (w);
23019 wr.height = WINDOW_TOTAL_HEIGHT (w);
23020
23021 if (x_intersect_rectangles (fr, &wr, &r))
23022 {
23023 int yb = window_text_bottom_y (w);
23024 struct glyph_row *row;
23025 int cursor_cleared_p;
23026 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23027
23028 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23029 r.x, r.y, r.width, r.height));
23030
23031 /* Convert to window coordinates. */
23032 r.x -= WINDOW_LEFT_EDGE_X (w);
23033 r.y -= WINDOW_TOP_EDGE_Y (w);
23034
23035 /* Turn off the cursor. */
23036 if (!w->pseudo_window_p
23037 && phys_cursor_in_rect_p (w, &r))
23038 {
23039 x_clear_cursor (w);
23040 cursor_cleared_p = 1;
23041 }
23042 else
23043 cursor_cleared_p = 0;
23044
23045 /* Update lines intersecting rectangle R. */
23046 first_overlapping_row = last_overlapping_row = NULL;
23047 for (row = w->current_matrix->rows;
23048 row->enabled_p;
23049 ++row)
23050 {
23051 int y0 = row->y;
23052 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23053
23054 if ((y0 >= r.y && y0 < r.y + r.height)
23055 || (y1 > r.y && y1 < r.y + r.height)
23056 || (r.y >= y0 && r.y < y1)
23057 || (r.y + r.height > y0 && r.y + r.height < y1))
23058 {
23059 /* A header line may be overlapping, but there is no need
23060 to fix overlapping areas for them. KFS 2005-02-12 */
23061 if (row->overlapping_p && !row->mode_line_p)
23062 {
23063 if (first_overlapping_row == NULL)
23064 first_overlapping_row = row;
23065 last_overlapping_row = row;
23066 }
23067
23068 if (expose_line (w, row, &r))
23069 mouse_face_overwritten_p = 1;
23070 }
23071
23072 if (y1 >= yb)
23073 break;
23074 }
23075
23076 /* Display the mode line if there is one. */
23077 if (WINDOW_WANTS_MODELINE_P (w)
23078 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23079 row->enabled_p)
23080 && row->y < r.y + r.height)
23081 {
23082 if (expose_line (w, row, &r))
23083 mouse_face_overwritten_p = 1;
23084 }
23085
23086 if (!w->pseudo_window_p)
23087 {
23088 /* Fix the display of overlapping rows. */
23089 if (first_overlapping_row)
23090 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23091
23092 /* Draw border between windows. */
23093 x_draw_vertical_border (w);
23094
23095 /* Turn the cursor on again. */
23096 if (cursor_cleared_p)
23097 update_window_cursor (w, 1);
23098 }
23099 }
23100
23101 return mouse_face_overwritten_p;
23102 }
23103
23104
23105
23106 /* Redraw (parts) of all windows in the window tree rooted at W that
23107 intersect R. R contains frame pixel coordinates. Value is
23108 non-zero if the exposure overwrites mouse-face. */
23109
23110 static int
23111 expose_window_tree (w, r)
23112 struct window *w;
23113 XRectangle *r;
23114 {
23115 struct frame *f = XFRAME (w->frame);
23116 int mouse_face_overwritten_p = 0;
23117
23118 while (w && !FRAME_GARBAGED_P (f))
23119 {
23120 if (!NILP (w->hchild))
23121 mouse_face_overwritten_p
23122 |= expose_window_tree (XWINDOW (w->hchild), r);
23123 else if (!NILP (w->vchild))
23124 mouse_face_overwritten_p
23125 |= expose_window_tree (XWINDOW (w->vchild), r);
23126 else
23127 mouse_face_overwritten_p |= expose_window (w, r);
23128
23129 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23130 }
23131
23132 return mouse_face_overwritten_p;
23133 }
23134
23135
23136 /* EXPORT:
23137 Redisplay an exposed area of frame F. X and Y are the upper-left
23138 corner of the exposed rectangle. W and H are width and height of
23139 the exposed area. All are pixel values. W or H zero means redraw
23140 the entire frame. */
23141
23142 void
23143 expose_frame (f, x, y, w, h)
23144 struct frame *f;
23145 int x, y, w, h;
23146 {
23147 XRectangle r;
23148 int mouse_face_overwritten_p = 0;
23149
23150 TRACE ((stderr, "expose_frame "));
23151
23152 /* No need to redraw if frame will be redrawn soon. */
23153 if (FRAME_GARBAGED_P (f))
23154 {
23155 TRACE ((stderr, " garbaged\n"));
23156 return;
23157 }
23158
23159 /* If basic faces haven't been realized yet, there is no point in
23160 trying to redraw anything. This can happen when we get an expose
23161 event while Emacs is starting, e.g. by moving another window. */
23162 if (FRAME_FACE_CACHE (f) == NULL
23163 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23164 {
23165 TRACE ((stderr, " no faces\n"));
23166 return;
23167 }
23168
23169 if (w == 0 || h == 0)
23170 {
23171 r.x = r.y = 0;
23172 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23173 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23174 }
23175 else
23176 {
23177 r.x = x;
23178 r.y = y;
23179 r.width = w;
23180 r.height = h;
23181 }
23182
23183 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23184 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23185
23186 if (WINDOWP (f->tool_bar_window))
23187 mouse_face_overwritten_p
23188 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23189
23190 #ifdef HAVE_X_WINDOWS
23191 #ifndef MSDOS
23192 #ifndef USE_X_TOOLKIT
23193 if (WINDOWP (f->menu_bar_window))
23194 mouse_face_overwritten_p
23195 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23196 #endif /* not USE_X_TOOLKIT */
23197 #endif
23198 #endif
23199
23200 /* Some window managers support a focus-follows-mouse style with
23201 delayed raising of frames. Imagine a partially obscured frame,
23202 and moving the mouse into partially obscured mouse-face on that
23203 frame. The visible part of the mouse-face will be highlighted,
23204 then the WM raises the obscured frame. With at least one WM, KDE
23205 2.1, Emacs is not getting any event for the raising of the frame
23206 (even tried with SubstructureRedirectMask), only Expose events.
23207 These expose events will draw text normally, i.e. not
23208 highlighted. Which means we must redo the highlight here.
23209 Subsume it under ``we love X''. --gerd 2001-08-15 */
23210 /* Included in Windows version because Windows most likely does not
23211 do the right thing if any third party tool offers
23212 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23213 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23214 {
23215 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23216 if (f == dpyinfo->mouse_face_mouse_frame)
23217 {
23218 int x = dpyinfo->mouse_face_mouse_x;
23219 int y = dpyinfo->mouse_face_mouse_y;
23220 clear_mouse_face (dpyinfo);
23221 note_mouse_highlight (f, x, y);
23222 }
23223 }
23224 }
23225
23226
23227 /* EXPORT:
23228 Determine the intersection of two rectangles R1 and R2. Return
23229 the intersection in *RESULT. Value is non-zero if RESULT is not
23230 empty. */
23231
23232 int
23233 x_intersect_rectangles (r1, r2, result)
23234 XRectangle *r1, *r2, *result;
23235 {
23236 XRectangle *left, *right;
23237 XRectangle *upper, *lower;
23238 int intersection_p = 0;
23239
23240 /* Rearrange so that R1 is the left-most rectangle. */
23241 if (r1->x < r2->x)
23242 left = r1, right = r2;
23243 else
23244 left = r2, right = r1;
23245
23246 /* X0 of the intersection is right.x0, if this is inside R1,
23247 otherwise there is no intersection. */
23248 if (right->x <= left->x + left->width)
23249 {
23250 result->x = right->x;
23251
23252 /* The right end of the intersection is the minimum of the
23253 the right ends of left and right. */
23254 result->width = (min (left->x + left->width, right->x + right->width)
23255 - result->x);
23256
23257 /* Same game for Y. */
23258 if (r1->y < r2->y)
23259 upper = r1, lower = r2;
23260 else
23261 upper = r2, lower = r1;
23262
23263 /* The upper end of the intersection is lower.y0, if this is inside
23264 of upper. Otherwise, there is no intersection. */
23265 if (lower->y <= upper->y + upper->height)
23266 {
23267 result->y = lower->y;
23268
23269 /* The lower end of the intersection is the minimum of the lower
23270 ends of upper and lower. */
23271 result->height = (min (lower->y + lower->height,
23272 upper->y + upper->height)
23273 - result->y);
23274 intersection_p = 1;
23275 }
23276 }
23277
23278 return intersection_p;
23279 }
23280
23281 #endif /* HAVE_WINDOW_SYSTEM */
23282
23283 \f
23284 /***********************************************************************
23285 Initialization
23286 ***********************************************************************/
23287
23288 void
23289 syms_of_xdisp ()
23290 {
23291 Vwith_echo_area_save_vector = Qnil;
23292 staticpro (&Vwith_echo_area_save_vector);
23293
23294 Vmessage_stack = Qnil;
23295 staticpro (&Vmessage_stack);
23296
23297 Qinhibit_redisplay = intern ("inhibit-redisplay");
23298 staticpro (&Qinhibit_redisplay);
23299
23300 message_dolog_marker1 = Fmake_marker ();
23301 staticpro (&message_dolog_marker1);
23302 message_dolog_marker2 = Fmake_marker ();
23303 staticpro (&message_dolog_marker2);
23304 message_dolog_marker3 = Fmake_marker ();
23305 staticpro (&message_dolog_marker3);
23306
23307 #if GLYPH_DEBUG
23308 defsubr (&Sdump_frame_glyph_matrix);
23309 defsubr (&Sdump_glyph_matrix);
23310 defsubr (&Sdump_glyph_row);
23311 defsubr (&Sdump_tool_bar_row);
23312 defsubr (&Strace_redisplay);
23313 defsubr (&Strace_to_stderr);
23314 #endif
23315 #ifdef HAVE_WINDOW_SYSTEM
23316 defsubr (&Stool_bar_lines_needed);
23317 defsubr (&Slookup_image_map);
23318 #endif
23319 defsubr (&Sformat_mode_line);
23320
23321 staticpro (&Qmenu_bar_update_hook);
23322 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23323
23324 staticpro (&Qoverriding_terminal_local_map);
23325 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23326
23327 staticpro (&Qoverriding_local_map);
23328 Qoverriding_local_map = intern ("overriding-local-map");
23329
23330 staticpro (&Qwindow_scroll_functions);
23331 Qwindow_scroll_functions = intern ("window-scroll-functions");
23332
23333 staticpro (&Qredisplay_end_trigger_functions);
23334 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23335
23336 staticpro (&Qinhibit_point_motion_hooks);
23337 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23338
23339 QCdata = intern (":data");
23340 staticpro (&QCdata);
23341 Qdisplay = intern ("display");
23342 staticpro (&Qdisplay);
23343 Qspace_width = intern ("space-width");
23344 staticpro (&Qspace_width);
23345 Qraise = intern ("raise");
23346 staticpro (&Qraise);
23347 Qslice = intern ("slice");
23348 staticpro (&Qslice);
23349 Qspace = intern ("space");
23350 staticpro (&Qspace);
23351 Qmargin = intern ("margin");
23352 staticpro (&Qmargin);
23353 Qpointer = intern ("pointer");
23354 staticpro (&Qpointer);
23355 Qleft_margin = intern ("left-margin");
23356 staticpro (&Qleft_margin);
23357 Qright_margin = intern ("right-margin");
23358 staticpro (&Qright_margin);
23359 Qcenter = intern ("center");
23360 staticpro (&Qcenter);
23361 Qline_height = intern ("line-height");
23362 staticpro (&Qline_height);
23363 QCalign_to = intern (":align-to");
23364 staticpro (&QCalign_to);
23365 QCrelative_width = intern (":relative-width");
23366 staticpro (&QCrelative_width);
23367 QCrelative_height = intern (":relative-height");
23368 staticpro (&QCrelative_height);
23369 QCeval = intern (":eval");
23370 staticpro (&QCeval);
23371 QCpropertize = intern (":propertize");
23372 staticpro (&QCpropertize);
23373 QCfile = intern (":file");
23374 staticpro (&QCfile);
23375 Qfontified = intern ("fontified");
23376 staticpro (&Qfontified);
23377 Qfontification_functions = intern ("fontification-functions");
23378 staticpro (&Qfontification_functions);
23379 Qtrailing_whitespace = intern ("trailing-whitespace");
23380 staticpro (&Qtrailing_whitespace);
23381 Qescape_glyph = intern ("escape-glyph");
23382 staticpro (&Qescape_glyph);
23383 Qnobreak_space = intern ("nobreak-space");
23384 staticpro (&Qnobreak_space);
23385 Qimage = intern ("image");
23386 staticpro (&Qimage);
23387 QCmap = intern (":map");
23388 staticpro (&QCmap);
23389 QCpointer = intern (":pointer");
23390 staticpro (&QCpointer);
23391 Qrect = intern ("rect");
23392 staticpro (&Qrect);
23393 Qcircle = intern ("circle");
23394 staticpro (&Qcircle);
23395 Qpoly = intern ("poly");
23396 staticpro (&Qpoly);
23397 Qmessage_truncate_lines = intern ("message-truncate-lines");
23398 staticpro (&Qmessage_truncate_lines);
23399 Qgrow_only = intern ("grow-only");
23400 staticpro (&Qgrow_only);
23401 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23402 staticpro (&Qinhibit_menubar_update);
23403 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23404 staticpro (&Qinhibit_eval_during_redisplay);
23405 Qposition = intern ("position");
23406 staticpro (&Qposition);
23407 Qbuffer_position = intern ("buffer-position");
23408 staticpro (&Qbuffer_position);
23409 Qobject = intern ("object");
23410 staticpro (&Qobject);
23411 Qbar = intern ("bar");
23412 staticpro (&Qbar);
23413 Qhbar = intern ("hbar");
23414 staticpro (&Qhbar);
23415 Qbox = intern ("box");
23416 staticpro (&Qbox);
23417 Qhollow = intern ("hollow");
23418 staticpro (&Qhollow);
23419 Qhand = intern ("hand");
23420 staticpro (&Qhand);
23421 Qarrow = intern ("arrow");
23422 staticpro (&Qarrow);
23423 Qtext = intern ("text");
23424 staticpro (&Qtext);
23425 Qrisky_local_variable = intern ("risky-local-variable");
23426 staticpro (&Qrisky_local_variable);
23427 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23428 staticpro (&Qinhibit_free_realized_faces);
23429
23430 list_of_error = Fcons (Fcons (intern ("error"),
23431 Fcons (intern ("void-variable"), Qnil)),
23432 Qnil);
23433 staticpro (&list_of_error);
23434
23435 Qlast_arrow_position = intern ("last-arrow-position");
23436 staticpro (&Qlast_arrow_position);
23437 Qlast_arrow_string = intern ("last-arrow-string");
23438 staticpro (&Qlast_arrow_string);
23439
23440 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23441 staticpro (&Qoverlay_arrow_string);
23442 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23443 staticpro (&Qoverlay_arrow_bitmap);
23444
23445 echo_buffer[0] = echo_buffer[1] = Qnil;
23446 staticpro (&echo_buffer[0]);
23447 staticpro (&echo_buffer[1]);
23448
23449 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23450 staticpro (&echo_area_buffer[0]);
23451 staticpro (&echo_area_buffer[1]);
23452
23453 Vmessages_buffer_name = build_string ("*Messages*");
23454 staticpro (&Vmessages_buffer_name);
23455
23456 mode_line_proptrans_alist = Qnil;
23457 staticpro (&mode_line_proptrans_alist);
23458 mode_line_string_list = Qnil;
23459 staticpro (&mode_line_string_list);
23460 mode_line_string_face = Qnil;
23461 staticpro (&mode_line_string_face);
23462 mode_line_string_face_prop = Qnil;
23463 staticpro (&mode_line_string_face_prop);
23464 Vmode_line_unwind_vector = Qnil;
23465 staticpro (&Vmode_line_unwind_vector);
23466
23467 help_echo_string = Qnil;
23468 staticpro (&help_echo_string);
23469 help_echo_object = Qnil;
23470 staticpro (&help_echo_object);
23471 help_echo_window = Qnil;
23472 staticpro (&help_echo_window);
23473 previous_help_echo_string = Qnil;
23474 staticpro (&previous_help_echo_string);
23475 help_echo_pos = -1;
23476
23477 #ifdef HAVE_WINDOW_SYSTEM
23478 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23479 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23480 For example, if a block cursor is over a tab, it will be drawn as
23481 wide as that tab on the display. */);
23482 x_stretch_cursor_p = 0;
23483 #endif
23484
23485 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23486 doc: /* *Non-nil means highlight trailing whitespace.
23487 The face used for trailing whitespace is `trailing-whitespace'. */);
23488 Vshow_trailing_whitespace = Qnil;
23489
23490 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23491 doc: /* *Control highlighting of nobreak space and soft hyphen.
23492 A value of t means highlight the character itself (for nobreak space,
23493 use face `nobreak-space').
23494 A value of nil means no highlighting.
23495 Other values mean display the escape glyph followed by an ordinary
23496 space or ordinary hyphen. */);
23497 Vnobreak_char_display = Qt;
23498
23499 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23500 doc: /* *The pointer shape to show in void text areas.
23501 A value of nil means to show the text pointer. Other options are `arrow',
23502 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23503 Vvoid_text_area_pointer = Qarrow;
23504
23505 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23506 doc: /* Non-nil means don't actually do any redisplay.
23507 This is used for internal purposes. */);
23508 Vinhibit_redisplay = Qnil;
23509
23510 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23511 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23512 Vglobal_mode_string = Qnil;
23513
23514 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23515 doc: /* Marker for where to display an arrow on top of the buffer text.
23516 This must be the beginning of a line in order to work.
23517 See also `overlay-arrow-string'. */);
23518 Voverlay_arrow_position = Qnil;
23519
23520 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23521 doc: /* String to display as an arrow in non-window frames.
23522 See also `overlay-arrow-position'. */);
23523 Voverlay_arrow_string = build_string ("=>");
23524
23525 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23526 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23527 The symbols on this list are examined during redisplay to determine
23528 where to display overlay arrows. */);
23529 Voverlay_arrow_variable_list
23530 = Fcons (intern ("overlay-arrow-position"), Qnil);
23531
23532 DEFVAR_INT ("scroll-step", &scroll_step,
23533 doc: /* *The number of lines to try scrolling a window by when point moves out.
23534 If that fails to bring point back on frame, point is centered instead.
23535 If this is zero, point is always centered after it moves off frame.
23536 If you want scrolling to always be a line at a time, you should set
23537 `scroll-conservatively' to a large value rather than set this to 1. */);
23538
23539 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23540 doc: /* *Scroll up to this many lines, to bring point back on screen.
23541 A value of zero means to scroll the text to center point vertically
23542 in the window. */);
23543 scroll_conservatively = 0;
23544
23545 DEFVAR_INT ("scroll-margin", &scroll_margin,
23546 doc: /* *Number of lines of margin at the top and bottom of a window.
23547 Recenter the window whenever point gets within this many lines
23548 of the top or bottom of the window. */);
23549 scroll_margin = 0;
23550
23551 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23552 doc: /* Pixels per inch value for non-window system displays.
23553 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23554 Vdisplay_pixels_per_inch = make_float (72.0);
23555
23556 #if GLYPH_DEBUG
23557 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23558 #endif
23559
23560 DEFVAR_BOOL ("truncate-partial-width-windows",
23561 &truncate_partial_width_windows,
23562 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23563 truncate_partial_width_windows = 1;
23564
23565 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23566 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23567 Any other value means to use the appropriate face, `mode-line',
23568 `header-line', or `menu' respectively. */);
23569 mode_line_inverse_video = 1;
23570
23571 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23572 doc: /* *Maximum buffer size for which line number should be displayed.
23573 If the buffer is bigger than this, the line number does not appear
23574 in the mode line. A value of nil means no limit. */);
23575 Vline_number_display_limit = Qnil;
23576
23577 DEFVAR_INT ("line-number-display-limit-width",
23578 &line_number_display_limit_width,
23579 doc: /* *Maximum line width (in characters) for line number display.
23580 If the average length of the lines near point is bigger than this, then the
23581 line number may be omitted from the mode line. */);
23582 line_number_display_limit_width = 200;
23583
23584 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23585 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23586 highlight_nonselected_windows = 0;
23587
23588 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23589 doc: /* Non-nil if more than one frame is visible on this display.
23590 Minibuffer-only frames don't count, but iconified frames do.
23591 This variable is not guaranteed to be accurate except while processing
23592 `frame-title-format' and `icon-title-format'. */);
23593
23594 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23595 doc: /* Template for displaying the title bar of visible frames.
23596 \(Assuming the window manager supports this feature.)
23597 This variable has the same structure as `mode-line-format' (which see),
23598 and is used only on frames for which no explicit name has been set
23599 \(see `modify-frame-parameters'). */);
23600
23601 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23602 doc: /* Template for displaying the title bar of an iconified frame.
23603 \(Assuming the window manager supports this feature.)
23604 This variable has the same structure as `mode-line-format' (which see),
23605 and is used only on frames for which no explicit name has been set
23606 \(see `modify-frame-parameters'). */);
23607 Vicon_title_format
23608 = Vframe_title_format
23609 = Fcons (intern ("multiple-frames"),
23610 Fcons (build_string ("%b"),
23611 Fcons (Fcons (empty_string,
23612 Fcons (intern ("invocation-name"),
23613 Fcons (build_string ("@"),
23614 Fcons (intern ("system-name"),
23615 Qnil)))),
23616 Qnil)));
23617
23618 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23619 doc: /* Maximum number of lines to keep in the message log buffer.
23620 If nil, disable message logging. If t, log messages but don't truncate
23621 the buffer when it becomes large. */);
23622 Vmessage_log_max = make_number (50);
23623
23624 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23625 doc: /* Functions called before redisplay, if window sizes have changed.
23626 The value should be a list of functions that take one argument.
23627 Just before redisplay, for each frame, if any of its windows have changed
23628 size since the last redisplay, or have been split or deleted,
23629 all the functions in the list are called, with the frame as argument. */);
23630 Vwindow_size_change_functions = Qnil;
23631
23632 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23633 doc: /* List of functions to call before redisplaying a window with scrolling.
23634 Each function is called with two arguments, the window
23635 and its new display-start position. Note that the value of `window-end'
23636 is not valid when these functions are called. */);
23637 Vwindow_scroll_functions = Qnil;
23638
23639 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23640 doc: /* Functions called when redisplay of a window reaches the end trigger.
23641 Each function is called with two arguments, the window and the end trigger value.
23642 See `set-window-redisplay-end-trigger'. */);
23643 Vredisplay_end_trigger_functions = Qnil;
23644
23645 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23646 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23647 mouse_autoselect_window = 0;
23648
23649 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23650 doc: /* *Non-nil means automatically resize tool-bars.
23651 This increases a tool-bar's height if not all tool-bar items are visible.
23652 It decreases a tool-bar's height when it would display blank lines
23653 otherwise. */);
23654 auto_resize_tool_bars_p = 1;
23655
23656 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23657 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23658 auto_raise_tool_bar_buttons_p = 1;
23659
23660 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23661 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23662 make_cursor_line_fully_visible_p = 1;
23663
23664 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23665 doc: /* *Margin around tool-bar buttons in pixels.
23666 If an integer, use that for both horizontal and vertical margins.
23667 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23668 HORZ specifying the horizontal margin, and VERT specifying the
23669 vertical margin. */);
23670 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23671
23672 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23673 doc: /* *Relief thickness of tool-bar buttons. */);
23674 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23675
23676 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23677 doc: /* List of functions to call to fontify regions of text.
23678 Each function is called with one argument POS. Functions must
23679 fontify a region starting at POS in the current buffer, and give
23680 fontified regions the property `fontified'. */);
23681 Vfontification_functions = Qnil;
23682 Fmake_variable_buffer_local (Qfontification_functions);
23683
23684 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23685 &unibyte_display_via_language_environment,
23686 doc: /* *Non-nil means display unibyte text according to language environment.
23687 Specifically this means that unibyte non-ASCII characters
23688 are displayed by converting them to the equivalent multibyte characters
23689 according to the current language environment. As a result, they are
23690 displayed according to the current fontset. */);
23691 unibyte_display_via_language_environment = 0;
23692
23693 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23694 doc: /* *Maximum height for resizing mini-windows.
23695 If a float, it specifies a fraction of the mini-window frame's height.
23696 If an integer, it specifies a number of lines. */);
23697 Vmax_mini_window_height = make_float (0.25);
23698
23699 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23700 doc: /* *How to resize mini-windows.
23701 A value of nil means don't automatically resize mini-windows.
23702 A value of t means resize them to fit the text displayed in them.
23703 A value of `grow-only', the default, means let mini-windows grow
23704 only, until their display becomes empty, at which point the windows
23705 go back to their normal size. */);
23706 Vresize_mini_windows = Qgrow_only;
23707
23708 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23709 doc: /* Alist specifying how to blink the cursor off.
23710 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23711 `cursor-type' frame-parameter or variable equals ON-STATE,
23712 comparing using `equal', Emacs uses OFF-STATE to specify
23713 how to blink it off. ON-STATE and OFF-STATE are values for
23714 the `cursor-type' frame parameter.
23715
23716 If a frame's ON-STATE has no entry in this list,
23717 the frame's other specifications determine how to blink the cursor off. */);
23718 Vblink_cursor_alist = Qnil;
23719
23720 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23721 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23722 automatic_hscrolling_p = 1;
23723
23724 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23725 doc: /* *How many columns away from the window edge point is allowed to get
23726 before automatic hscrolling will horizontally scroll the window. */);
23727 hscroll_margin = 5;
23728
23729 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23730 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23731 When point is less than `automatic-hscroll-margin' columns from the window
23732 edge, automatic hscrolling will scroll the window by the amount of columns
23733 determined by this variable. If its value is a positive integer, scroll that
23734 many columns. If it's a positive floating-point number, it specifies the
23735 fraction of the window's width to scroll. If it's nil or zero, point will be
23736 centered horizontally after the scroll. Any other value, including negative
23737 numbers, are treated as if the value were zero.
23738
23739 Automatic hscrolling always moves point outside the scroll margin, so if
23740 point was more than scroll step columns inside the margin, the window will
23741 scroll more than the value given by the scroll step.
23742
23743 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23744 and `scroll-right' overrides this variable's effect. */);
23745 Vhscroll_step = make_number (0);
23746
23747 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23748 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23749 Bind this around calls to `message' to let it take effect. */);
23750 message_truncate_lines = 0;
23751
23752 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23753 doc: /* Normal hook run to update the menu bar definitions.
23754 Redisplay runs this hook before it redisplays the menu bar.
23755 This is used to update submenus such as Buffers,
23756 whose contents depend on various data. */);
23757 Vmenu_bar_update_hook = Qnil;
23758
23759 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23760 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23761 inhibit_menubar_update = 0;
23762
23763 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23764 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23765 inhibit_eval_during_redisplay = 0;
23766
23767 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23768 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23769 inhibit_free_realized_faces = 0;
23770
23771 #if GLYPH_DEBUG
23772 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23773 doc: /* Inhibit try_window_id display optimization. */);
23774 inhibit_try_window_id = 0;
23775
23776 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23777 doc: /* Inhibit try_window_reusing display optimization. */);
23778 inhibit_try_window_reusing = 0;
23779
23780 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23781 doc: /* Inhibit try_cursor_movement display optimization. */);
23782 inhibit_try_cursor_movement = 0;
23783 #endif /* GLYPH_DEBUG */
23784 }
23785
23786
23787 /* Initialize this module when Emacs starts. */
23788
23789 void
23790 init_xdisp ()
23791 {
23792 Lisp_Object root_window;
23793 struct window *mini_w;
23794
23795 current_header_line_height = current_mode_line_height = -1;
23796
23797 CHARPOS (this_line_start_pos) = 0;
23798
23799 mini_w = XWINDOW (minibuf_window);
23800 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23801
23802 if (!noninteractive)
23803 {
23804 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23805 int i;
23806
23807 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23808 set_window_height (root_window,
23809 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23810 0);
23811 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23812 set_window_height (minibuf_window, 1, 0);
23813
23814 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23815 mini_w->total_cols = make_number (FRAME_COLS (f));
23816
23817 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23818 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23819 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23820
23821 /* The default ellipsis glyphs `...'. */
23822 for (i = 0; i < 3; ++i)
23823 default_invis_vector[i] = make_number ('.');
23824 }
23825
23826 {
23827 /* Allocate the buffer for frame titles.
23828 Also used for `format-mode-line'. */
23829 int size = 100;
23830 mode_line_noprop_buf = (char *) xmalloc (size);
23831 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23832 mode_line_noprop_ptr = mode_line_noprop_buf;
23833 mode_line_target = MODE_LINE_DISPLAY;
23834 }
23835
23836 help_echo_showing_p = 0;
23837 }
23838
23839
23840 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23841 (do not change this comment) */