(Fclrhash): Return TABLE.
[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, 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "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-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_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 below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
275
276 Lisp_Object Vtool_bar_border;
277
278 /* Margin around tool bar buttons in pixels. */
279
280 Lisp_Object Vtool_bar_button_margin;
281
282 /* Thickness of shadow to draw around tool bar buttons. */
283
284 EMACS_INT tool_bar_button_relief;
285
286 /* Non-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
288
289 If value is `grow-only', only make tool-bar bigger. */
290
291 Lisp_Object Vauto_resize_tool_bars;
292
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
296
297 int x_stretch_cursor_p;
298
299 /* Non-nil means don't actually do any redisplay. */
300
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
302
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
304
305 int inhibit_eval_during_redisplay;
306
307 /* Names of text properties relevant for redisplay. */
308
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
311
312 /* Symbols used in text property values. */
313
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
325
326 /* Non-nil means highlight trailing whitespace. */
327
328 Lisp_Object Vshow_trailing_whitespace;
329
330 /* Non-nil means escape non-break space and hyphens. */
331
332 Lisp_Object Vnobreak_char_display;
333
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
336
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
339
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
345
346 #endif /* HAVE_WINDOW_SYSTEM */
347
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
351
352 Lisp_Object Vvoid_text_area_pointer;
353
354 /* Name of the face used to highlight trailing whitespace. */
355
356 Lisp_Object Qtrailing_whitespace;
357
358 /* Name and number of the face used to highlight escape glyphs. */
359
360 Lisp_Object Qescape_glyph;
361
362 /* Name and number of the face used to highlight non-breaking spaces. */
363
364 Lisp_Object Qnobreak_space;
365
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
368
369 Lisp_Object Qimage;
370
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
374
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
377
378 int noninteractive_need_newline;
379
380 /* Non-zero means print newline to message log before next message. */
381
382 static int message_log_need_newline;
383
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
390 \f
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
395
396 static struct text_pos this_line_start_pos;
397
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
400
401 static struct text_pos this_line_end_pos;
402
403 /* The vertical positions and the height of this line. */
404
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
408
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
411
412 static int this_line_start_x;
413
414 /* Buffer that this_line_.* variables are referring to. */
415
416 static struct buffer *this_line_buffer;
417
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
420
421 int truncate_partial_width_windows;
422
423 /* A flag to control how to display unibyte 8-bit character. */
424
425 int unibyte_display_via_language_environment;
426
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
430
431 int multiple_frames;
432
433 Lisp_Object Vglobal_mode_string;
434
435
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
439
440 Lisp_Object Voverlay_arrow_variable_list;
441
442 /* Marker for where to display an arrow on top of the buffer text. */
443
444 Lisp_Object Voverlay_arrow_position;
445
446 /* String to display for the arrow. Only used on terminal frames. */
447
448 Lisp_Object Voverlay_arrow_string;
449
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
454
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
456
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
459
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
461
462 /* Like mode-line-format, but for the title bar on a visible frame. */
463
464 Lisp_Object Vframe_title_format;
465
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
467
468 Lisp_Object Vicon_title_format;
469
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
473
474 static Lisp_Object Vwindow_size_change_functions;
475
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
477
478 /* Nonzero if an overlay arrow has been displayed in this window. */
479
480 static int overlay_arrow_seen;
481
482 /* Nonzero means highlight the region even in nonselected windows. */
483
484 int highlight_nonselected_windows;
485
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
488
489 static EMACS_INT scroll_step;
490
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
493
494 static EMACS_INT scroll_conservatively;
495
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
500
501 EMACS_INT scroll_margin;
502
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
506
507 int buffer_shared;
508
509 /* Vector containing glyphs for an ellipsis `...'. */
510
511 static Lisp_Object default_invis_vector[3];
512
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
516
517 This variable is deprecated. */
518
519 int mode_line_inverse_video;
520
521 /* Prompt to display in front of the mini-buffer contents. */
522
523 Lisp_Object minibuf_prompt;
524
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
527
528 int minibuf_prompt_width;
529
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
533
534 Lisp_Object echo_area_window;
535
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
540
541 Lisp_Object Vmessage_stack;
542
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
545
546 int message_enable_multibyte;
547
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
549
550 int update_mode_lines;
551
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
554
555 int windows_or_buffers_changed;
556
557 /* Nonzero means a frame's cursor type has been changed. */
558
559 int cursor_type_changed;
560
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
563
564 int line_number_displayed;
565
566 /* Maximum buffer size for which to display line numbers. */
567
568 Lisp_Object Vline_number_display_limit;
569
570 /* Line width to consider when repositioning for line number display. */
571
572 static EMACS_INT line_number_display_limit_width;
573
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
576
577 Lisp_Object Vmessage_log_max;
578
579 /* The name of the *Messages* buffer, a string. */
580
581 static Lisp_Object Vmessages_buffer_name;
582
583 /* Current, index 0, and last displayed echo area message. Either
584 buffers from echo_buffers, or nil to indicate no message. */
585
586 Lisp_Object echo_area_buffer[2];
587
588 /* The buffers referenced from echo_area_buffer. */
589
590 static Lisp_Object echo_buffer[2];
591
592 /* A vector saved used in with_area_buffer to reduce consing. */
593
594 static Lisp_Object Vwith_echo_area_save_vector;
595
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
598
599 static int display_last_displayed_message_p;
600
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
603
604 int message_buf_print;
605
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
607
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
610
611 /* When evaluating expressions from menu bar items (enable conditions,
612 for instance), this is the frame they are being processed for. */
613
614 Lisp_Object Vmenu_updating_frame;
615
616 /* Maximum height for resizing mini-windows. Either a float
617 specifying a fraction of the available height, or an integer
618 specifying a number of lines. */
619
620 Lisp_Object Vmax_mini_window_height;
621
622 /* Non-zero means messages should be displayed with truncated
623 lines instead of being continued. */
624
625 int message_truncate_lines;
626 Lisp_Object Qmessage_truncate_lines;
627
628 /* Set to 1 in clear_message to make redisplay_internal aware
629 of an emptied echo area. */
630
631 static int message_cleared_p;
632
633 /* How to blink the default frame cursor off. */
634 Lisp_Object Vblink_cursor_alist;
635
636 /* A scratch glyph row with contents used for generating truncation
637 glyphs. Also used in direct_output_for_insert. */
638
639 #define MAX_SCRATCH_GLYPHS 100
640 struct glyph_row scratch_glyph_row;
641 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
642
643 /* Ascent and height of the last line processed by move_it_to. */
644
645 static int last_max_ascent, last_height;
646
647 /* Non-zero if there's a help-echo in the echo area. */
648
649 int help_echo_showing_p;
650
651 /* If >= 0, computed, exact values of mode-line and header-line height
652 to use in the macros CURRENT_MODE_LINE_HEIGHT and
653 CURRENT_HEADER_LINE_HEIGHT. */
654
655 int current_mode_line_height, current_header_line_height;
656
657 /* The maximum distance to look ahead for text properties. Values
658 that are too small let us call compute_char_face and similar
659 functions too often which is expensive. Values that are too large
660 let us call compute_char_face and alike too often because we
661 might not be interested in text properties that far away. */
662
663 #define TEXT_PROP_DISTANCE_LIMIT 100
664
665 #if GLYPH_DEBUG
666
667 /* Variables to turn off display optimizations from Lisp. */
668
669 int inhibit_try_window_id, inhibit_try_window_reusing;
670 int inhibit_try_cursor_movement;
671
672 /* Non-zero means print traces of redisplay if compiled with
673 GLYPH_DEBUG != 0. */
674
675 int trace_redisplay_p;
676
677 #endif /* GLYPH_DEBUG */
678
679 #ifdef DEBUG_TRACE_MOVE
680 /* Non-zero means trace with TRACE_MOVE to stderr. */
681 int trace_move;
682
683 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
684 #else
685 #define TRACE_MOVE(x) (void) 0
686 #endif
687
688 /* Non-zero means automatically scroll windows horizontally to make
689 point visible. */
690
691 int automatic_hscrolling_p;
692 Lisp_Object Qauto_hscroll_mode;
693
694 /* How close to the margin can point get before the window is scrolled
695 horizontally. */
696 EMACS_INT hscroll_margin;
697
698 /* How much to scroll horizontally when point is inside the above margin. */
699 Lisp_Object Vhscroll_step;
700
701 /* The variable `resize-mini-windows'. If nil, don't resize
702 mini-windows. If t, always resize them to fit the text they
703 display. If `grow-only', let mini-windows grow only until they
704 become empty. */
705
706 Lisp_Object Vresize_mini_windows;
707
708 /* Buffer being redisplayed -- for redisplay_window_error. */
709
710 struct buffer *displayed_buffer;
711
712 /* Space between overline and text. */
713
714 EMACS_INT overline_margin;
715
716 /* Value returned from text property handlers (see below). */
717
718 enum prop_handled
719 {
720 HANDLED_NORMALLY,
721 HANDLED_RECOMPUTE_PROPS,
722 HANDLED_OVERLAY_STRING_CONSUMED,
723 HANDLED_RETURN
724 };
725
726 /* A description of text properties that redisplay is interested
727 in. */
728
729 struct props
730 {
731 /* The name of the property. */
732 Lisp_Object *name;
733
734 /* A unique index for the property. */
735 enum prop_idx idx;
736
737 /* A handler function called to set up iterator IT from the property
738 at IT's current position. Value is used to steer handle_stop. */
739 enum prop_handled (*handler) P_ ((struct it *it));
740 };
741
742 static enum prop_handled handle_face_prop P_ ((struct it *));
743 static enum prop_handled handle_invisible_prop P_ ((struct it *));
744 static enum prop_handled handle_display_prop P_ ((struct it *));
745 static enum prop_handled handle_composition_prop P_ ((struct it *));
746 static enum prop_handled handle_overlay_change P_ ((struct it *));
747 static enum prop_handled handle_fontified_prop P_ ((struct it *));
748
749 /* Properties handled by iterators. */
750
751 static struct props it_props[] =
752 {
753 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
754 /* Handle `face' before `display' because some sub-properties of
755 `display' need to know the face. */
756 {&Qface, FACE_PROP_IDX, handle_face_prop},
757 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
758 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
759 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
760 {NULL, 0, NULL}
761 };
762
763 /* Value is the position described by X. If X is a marker, value is
764 the marker_position of X. Otherwise, value is X. */
765
766 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
767
768 /* Enumeration returned by some move_it_.* functions internally. */
769
770 enum move_it_result
771 {
772 /* Not used. Undefined value. */
773 MOVE_UNDEFINED,
774
775 /* Move ended at the requested buffer position or ZV. */
776 MOVE_POS_MATCH_OR_ZV,
777
778 /* Move ended at the requested X pixel position. */
779 MOVE_X_REACHED,
780
781 /* Move within a line ended at the end of a line that must be
782 continued. */
783 MOVE_LINE_CONTINUED,
784
785 /* Move within a line ended at the end of a line that would
786 be displayed truncated. */
787 MOVE_LINE_TRUNCATED,
788
789 /* Move within a line ended at a line end. */
790 MOVE_NEWLINE_OR_CR
791 };
792
793 /* This counter is used to clear the face cache every once in a while
794 in redisplay_internal. It is incremented for each redisplay.
795 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
796 cleared. */
797
798 #define CLEAR_FACE_CACHE_COUNT 500
799 static int clear_face_cache_count;
800
801 /* Similarly for the image cache. */
802
803 #ifdef HAVE_WINDOW_SYSTEM
804 #define CLEAR_IMAGE_CACHE_COUNT 101
805 static int clear_image_cache_count;
806 #endif
807
808 /* Non-zero while redisplay_internal is in progress. */
809
810 int redisplaying_p;
811
812 /* Non-zero means don't free realized faces. Bound while freeing
813 realized faces is dangerous because glyph matrices might still
814 reference them. */
815
816 int inhibit_free_realized_faces;
817 Lisp_Object Qinhibit_free_realized_faces;
818
819 /* If a string, XTread_socket generates an event to display that string.
820 (The display is done in read_char.) */
821
822 Lisp_Object help_echo_string;
823 Lisp_Object help_echo_window;
824 Lisp_Object help_echo_object;
825 int help_echo_pos;
826
827 /* Temporary variable for XTread_socket. */
828
829 Lisp_Object previous_help_echo_string;
830
831 /* Null glyph slice */
832
833 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
834
835 \f
836 /* Function prototypes. */
837
838 static void setup_for_ellipsis P_ ((struct it *, int));
839 static void mark_window_display_accurate_1 P_ ((struct window *, int));
840 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
841 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
842 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
843 static int redisplay_mode_lines P_ ((Lisp_Object, int));
844 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
845
846 #if 0
847 static int invisible_text_between_p P_ ((struct it *, int, int));
848 #endif
849
850 static void pint2str P_ ((char *, int, int));
851 static void pint2hrstr P_ ((char *, int, int));
852 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
853 struct text_pos));
854 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
855 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
856 static void store_mode_line_noprop_char P_ ((char));
857 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
858 static void x_consider_frame_title P_ ((Lisp_Object));
859 static void handle_stop P_ ((struct it *));
860 static int tool_bar_lines_needed P_ ((struct frame *, int *));
861 static int single_display_spec_intangible_p P_ ((Lisp_Object));
862 static void ensure_echo_area_buffers P_ ((void));
863 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
864 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
865 static int with_echo_area_buffer P_ ((struct window *, int,
866 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
867 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static void clear_garbaged_frames P_ ((void));
869 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static int display_echo_area P_ ((struct window *));
873 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
874 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
876 static int string_char_and_length P_ ((const unsigned char *, int, int *));
877 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
878 struct text_pos));
879 static int compute_window_start_on_continuation_line P_ ((struct window *));
880 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
881 static void insert_left_trunc_glyphs P_ ((struct it *));
882 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
883 Lisp_Object));
884 static void extend_face_to_end_of_line P_ ((struct it *));
885 static int append_space_for_newline P_ ((struct it *, int));
886 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
887 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
888 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
889 static int trailing_whitespace_p P_ ((int));
890 static int message_log_check_duplicate P_ ((int, int, int, int));
891 static void push_it P_ ((struct it *));
892 static void pop_it P_ ((struct it *));
893 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
894 static void select_frame_for_redisplay P_ ((Lisp_Object));
895 static void redisplay_internal P_ ((int));
896 static int echo_area_display P_ ((int));
897 static void redisplay_windows P_ ((Lisp_Object));
898 static void redisplay_window P_ ((Lisp_Object, int));
899 static Lisp_Object redisplay_window_error ();
900 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
901 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
902 static int update_menu_bar P_ ((struct frame *, int, int));
903 static int try_window_reusing_current_matrix P_ ((struct window *));
904 static int try_window_id P_ ((struct window *));
905 static int display_line P_ ((struct it *));
906 static int display_mode_lines P_ ((struct window *));
907 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
908 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
909 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
910 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
911 static void display_menu_bar P_ ((struct window *));
912 static int display_count_lines P_ ((int, int, int, int, int *));
913 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
914 int, int, struct it *, int, int, int, int));
915 static void compute_line_metrics P_ ((struct it *));
916 static void run_redisplay_end_trigger_hook P_ ((struct it *));
917 static int get_overlay_strings P_ ((struct it *, int));
918 static int get_overlay_strings_1 P_ ((struct it *, int, int));
919 static void next_overlay_string P_ ((struct it *));
920 static void reseat P_ ((struct it *, struct text_pos, int));
921 static void reseat_1 P_ ((struct it *, struct text_pos, int));
922 static void back_to_previous_visible_line_start P_ ((struct it *));
923 void reseat_at_previous_visible_line_start P_ ((struct it *));
924 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
925 static int next_element_from_ellipsis P_ ((struct it *));
926 static int next_element_from_display_vector P_ ((struct it *));
927 static int next_element_from_string P_ ((struct it *));
928 static int next_element_from_c_string P_ ((struct it *));
929 static int next_element_from_buffer P_ ((struct it *));
930 static int next_element_from_composition P_ ((struct it *));
931 static int next_element_from_image P_ ((struct it *));
932 static int next_element_from_stretch P_ ((struct it *));
933 static void load_overlay_strings P_ ((struct it *, int));
934 static int init_from_display_pos P_ ((struct it *, struct window *,
935 struct display_pos *));
936 static void reseat_to_string P_ ((struct it *, unsigned char *,
937 Lisp_Object, int, int, int, int));
938 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
939 int, int, int));
940 void move_it_vertically_backward P_ ((struct it *, int));
941 static void init_to_row_start P_ ((struct it *, struct window *,
942 struct glyph_row *));
943 static int init_to_row_end P_ ((struct it *, struct window *,
944 struct glyph_row *));
945 static void back_to_previous_line_start P_ ((struct it *));
946 static int forward_to_next_line_start P_ ((struct it *, int *));
947 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
948 Lisp_Object, int));
949 static struct text_pos string_pos P_ ((int, Lisp_Object));
950 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
951 static int number_of_chars P_ ((unsigned char *, int));
952 static void compute_stop_pos P_ ((struct it *));
953 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
954 Lisp_Object));
955 static int face_before_or_after_it_pos P_ ((struct it *, int));
956 static int next_overlay_change P_ ((int));
957 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
958 Lisp_Object, Lisp_Object,
959 struct text_pos *, int));
960 static int underlying_face_id P_ ((struct it *));
961 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
962 struct window *));
963
964 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
965 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
966
967 #ifdef HAVE_WINDOW_SYSTEM
968
969 static void update_tool_bar P_ ((struct frame *, int));
970 static void build_desired_tool_bar_string P_ ((struct frame *f));
971 static int redisplay_tool_bar P_ ((struct frame *));
972 static void display_tool_bar_line P_ ((struct it *, int));
973 static void notice_overwritten_cursor P_ ((struct window *,
974 enum glyph_row_area,
975 int, int, int, int));
976
977
978
979 #endif /* HAVE_WINDOW_SYSTEM */
980
981 \f
982 /***********************************************************************
983 Window display dimensions
984 ***********************************************************************/
985
986 /* Return the bottom boundary y-position for text lines in window W.
987 This is the first y position at which a line cannot start.
988 It is relative to the top of the window.
989
990 This is the height of W minus the height of a mode line, if any. */
991
992 INLINE int
993 window_text_bottom_y (w)
994 struct window *w;
995 {
996 int height = WINDOW_TOTAL_HEIGHT (w);
997
998 if (WINDOW_WANTS_MODELINE_P (w))
999 height -= CURRENT_MODE_LINE_HEIGHT (w);
1000 return height;
1001 }
1002
1003 /* Return the pixel width of display area AREA of window W. AREA < 0
1004 means return the total width of W, not including fringes to
1005 the left and right of the window. */
1006
1007 INLINE int
1008 window_box_width (w, area)
1009 struct window *w;
1010 int area;
1011 {
1012 int cols = XFASTINT (w->total_cols);
1013 int pixels = 0;
1014
1015 if (!w->pseudo_window_p)
1016 {
1017 cols -= WINDOW_SCROLL_BAR_COLS (w);
1018
1019 if (area == TEXT_AREA)
1020 {
1021 if (INTEGERP (w->left_margin_cols))
1022 cols -= XFASTINT (w->left_margin_cols);
1023 if (INTEGERP (w->right_margin_cols))
1024 cols -= XFASTINT (w->right_margin_cols);
1025 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1026 }
1027 else if (area == LEFT_MARGIN_AREA)
1028 {
1029 cols = (INTEGERP (w->left_margin_cols)
1030 ? XFASTINT (w->left_margin_cols) : 0);
1031 pixels = 0;
1032 }
1033 else if (area == RIGHT_MARGIN_AREA)
1034 {
1035 cols = (INTEGERP (w->right_margin_cols)
1036 ? XFASTINT (w->right_margin_cols) : 0);
1037 pixels = 0;
1038 }
1039 }
1040
1041 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1042 }
1043
1044
1045 /* Return the pixel height of the display area of window W, not
1046 including mode lines of W, if any. */
1047
1048 INLINE int
1049 window_box_height (w)
1050 struct window *w;
1051 {
1052 struct frame *f = XFRAME (w->frame);
1053 int height = WINDOW_TOTAL_HEIGHT (w);
1054
1055 xassert (height >= 0);
1056
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1062
1063 if (WINDOW_WANTS_MODELINE_P (w))
1064 {
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 }
1074
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1076 {
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 }
1086
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1090 }
1091
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. AREA < 0 means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1095
1096 INLINE int
1097 window_box_left_offset (w, area)
1098 struct window *w;
1099 int area;
1100 {
1101 int x;
1102
1103 if (w->pseudo_window_p)
1104 return 0;
1105
1106 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1107
1108 if (area == TEXT_AREA)
1109 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1110 + window_box_width (w, LEFT_MARGIN_AREA));
1111 else if (area == RIGHT_MARGIN_AREA)
1112 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1113 + window_box_width (w, LEFT_MARGIN_AREA)
1114 + window_box_width (w, TEXT_AREA)
1115 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1116 ? 0
1117 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1118 else if (area == LEFT_MARGIN_AREA
1119 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1120 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1121
1122 return x;
1123 }
1124
1125
1126 /* Return the window-relative coordinate of the right edge of display
1127 area AREA of window W. AREA < 0 means return the left edge of the
1128 whole window, to the left of the right fringe of W. */
1129
1130 INLINE int
1131 window_box_right_offset (w, area)
1132 struct window *w;
1133 int area;
1134 {
1135 return window_box_left_offset (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Return the frame-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1141
1142 INLINE int
1143 window_box_left (w, area)
1144 struct window *w;
1145 int area;
1146 {
1147 struct frame *f = XFRAME (w->frame);
1148 int x;
1149
1150 if (w->pseudo_window_p)
1151 return FRAME_INTERNAL_BORDER_WIDTH (f);
1152
1153 x = (WINDOW_LEFT_EDGE_X (w)
1154 + window_box_left_offset (w, area));
1155
1156 return x;
1157 }
1158
1159
1160 /* Return the frame-relative coordinate of the right edge of display
1161 area AREA of window W. AREA < 0 means return the left edge of the
1162 whole window, to the left of the right fringe of W. */
1163
1164 INLINE int
1165 window_box_right (w, area)
1166 struct window *w;
1167 int area;
1168 {
1169 return window_box_left (w, area) + window_box_width (w, area);
1170 }
1171
1172 /* Get the bounding box of the display area AREA of window W, without
1173 mode lines, in frame-relative coordinates. AREA < 0 means the
1174 whole window, not including the left and right fringes of
1175 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1176 coordinates of the upper-left corner of the box. Return in
1177 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1178
1179 INLINE void
1180 window_box (w, area, box_x, box_y, box_width, box_height)
1181 struct window *w;
1182 int area;
1183 int *box_x, *box_y, *box_width, *box_height;
1184 {
1185 if (box_width)
1186 *box_width = window_box_width (w, area);
1187 if (box_height)
1188 *box_height = window_box_height (w);
1189 if (box_x)
1190 *box_x = window_box_left (w, area);
1191 if (box_y)
1192 {
1193 *box_y = WINDOW_TOP_EDGE_Y (w);
1194 if (WINDOW_WANTS_HEADER_LINE_P (w))
1195 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1196 }
1197 }
1198
1199
1200 /* Get the bounding box of the display area AREA of window W, without
1201 mode lines. AREA < 0 means the whole window, not including the
1202 left and right fringe of the window. Return in *TOP_LEFT_X
1203 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1204 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1205 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1206 box. */
1207
1208 INLINE void
1209 window_box_edges (w, area, top_left_x, top_left_y,
1210 bottom_right_x, bottom_right_y)
1211 struct window *w;
1212 int area;
1213 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1214 {
1215 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1216 bottom_right_y);
1217 *bottom_right_x += *top_left_x;
1218 *bottom_right_y += *top_left_y;
1219 }
1220
1221
1222 \f
1223 /***********************************************************************
1224 Utilities
1225 ***********************************************************************/
1226
1227 /* Return the bottom y-position of the line the iterator IT is in.
1228 This can modify IT's settings. */
1229
1230 int
1231 line_bottom_y (it)
1232 struct it *it;
1233 {
1234 int line_height = it->max_ascent + it->max_descent;
1235 int line_top_y = it->current_y;
1236
1237 if (line_height == 0)
1238 {
1239 if (last_height)
1240 line_height = last_height;
1241 else if (IT_CHARPOS (*it) < ZV)
1242 {
1243 move_it_by_lines (it, 1, 1);
1244 line_height = (it->max_ascent || it->max_descent
1245 ? it->max_ascent + it->max_descent
1246 : last_height);
1247 }
1248 else
1249 {
1250 struct glyph_row *row = it->glyph_row;
1251
1252 /* Use the default character height. */
1253 it->glyph_row = NULL;
1254 it->what = IT_CHARACTER;
1255 it->c = ' ';
1256 it->len = 1;
1257 PRODUCE_GLYPHS (it);
1258 line_height = it->ascent + it->descent;
1259 it->glyph_row = row;
1260 }
1261 }
1262
1263 return line_top_y + line_height;
1264 }
1265
1266
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 CHARPOS < 0 means return info about WINDOW_END position.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1272
1273 int
1274 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1275 struct window *w;
1276 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1277 {
1278 struct it it;
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1282
1283 if (noninteractive)
1284 return visible_p;
1285
1286 if (XBUFFER (w->buffer) != current_buffer)
1287 {
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->buffer));
1290 }
1291
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293
1294 /* Compute exact mode line heights. */
1295 if (WINDOW_WANTS_MODELINE_P (w))
1296 current_mode_line_height
1297 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1298 current_buffer->mode_line_format);
1299
1300 if (WINDOW_WANTS_HEADER_LINE_P (w))
1301 current_header_line_height
1302 = display_mode_line (w, HEADER_LINE_FACE_ID,
1303 current_buffer->header_line_format);
1304
1305 start_display (&it, w, top);
1306 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1307 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1308
1309 /* Note that we may overshoot because of invisible text. */
1310 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1311 {
1312 int top_x = it.current_x;
1313 int top_y = it.current_y;
1314 int bottom_y = (last_height = 0, line_bottom_y (&it));
1315 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1316
1317 if (top_y < window_top_y)
1318 visible_p = bottom_y > window_top_y;
1319 else if (top_y < it.last_visible_y)
1320 visible_p = 1;
1321 if (visible_p)
1322 {
1323 *x = top_x;
1324 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1325 *rtop = max (0, window_top_y - top_y);
1326 *rbot = max (0, bottom_y - it.last_visible_y);
1327 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1328 - max (top_y, window_top_y)));
1329 *vpos = it.vpos;
1330 }
1331 }
1332 else
1333 {
1334 struct it it2;
1335
1336 it2 = it;
1337 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1338 move_it_by_lines (&it, 1, 0);
1339 if (charpos < IT_CHARPOS (it)
1340 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1341 {
1342 visible_p = 1;
1343 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1344 *x = it2.current_x;
1345 *y = it2.current_y + it2.max_ascent - it2.ascent;
1346 *rtop = max (0, -it2.current_y);
1347 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1348 - it.last_visible_y));
1349 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1350 it.last_visible_y)
1351 - max (it2.current_y,
1352 WINDOW_HEADER_LINE_HEIGHT (w))));
1353 *vpos = it2.vpos;
1354 }
1355 }
1356
1357 if (old_buffer)
1358 set_buffer_internal_1 (old_buffer);
1359
1360 current_header_line_height = current_mode_line_height = -1;
1361
1362 if (visible_p && XFASTINT (w->hscroll) > 0)
1363 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1364
1365 #if 0
1366 /* Debugging code. */
1367 if (visible_p)
1368 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1369 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1370 else
1371 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1372 #endif
1373
1374 return visible_p;
1375 }
1376
1377
1378 /* Return the next character from STR which is MAXLEN bytes long.
1379 Return in *LEN the length of the character. This is like
1380 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1381 we find one, we return a `?', but with the length of the invalid
1382 character. */
1383
1384 static INLINE int
1385 string_char_and_length (str, maxlen, len)
1386 const unsigned char *str;
1387 int maxlen, *len;
1388 {
1389 int c;
1390
1391 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1392 if (!CHAR_VALID_P (c, 1))
1393 /* We may not change the length here because other places in Emacs
1394 don't use this function, i.e. they silently accept invalid
1395 characters. */
1396 c = '?';
1397
1398 return c;
1399 }
1400
1401
1402
1403 /* Given a position POS containing a valid character and byte position
1404 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1405
1406 static struct text_pos
1407 string_pos_nchars_ahead (pos, string, nchars)
1408 struct text_pos pos;
1409 Lisp_Object string;
1410 int nchars;
1411 {
1412 xassert (STRINGP (string) && nchars >= 0);
1413
1414 if (STRING_MULTIBYTE (string))
1415 {
1416 int rest = SBYTES (string) - BYTEPOS (pos);
1417 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1418 int len;
1419
1420 while (nchars--)
1421 {
1422 string_char_and_length (p, rest, &len);
1423 p += len, rest -= len;
1424 xassert (rest >= 0);
1425 CHARPOS (pos) += 1;
1426 BYTEPOS (pos) += len;
1427 }
1428 }
1429 else
1430 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1431
1432 return pos;
1433 }
1434
1435
1436 /* Value is the text position, i.e. character and byte position,
1437 for character position CHARPOS in STRING. */
1438
1439 static INLINE struct text_pos
1440 string_pos (charpos, string)
1441 int charpos;
1442 Lisp_Object string;
1443 {
1444 struct text_pos pos;
1445 xassert (STRINGP (string));
1446 xassert (charpos >= 0);
1447 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1448 return pos;
1449 }
1450
1451
1452 /* Value is a text position, i.e. character and byte position, for
1453 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1454 means recognize multibyte characters. */
1455
1456 static struct text_pos
1457 c_string_pos (charpos, s, multibyte_p)
1458 int charpos;
1459 unsigned char *s;
1460 int multibyte_p;
1461 {
1462 struct text_pos pos;
1463
1464 xassert (s != NULL);
1465 xassert (charpos >= 0);
1466
1467 if (multibyte_p)
1468 {
1469 int rest = strlen (s), len;
1470
1471 SET_TEXT_POS (pos, 0, 0);
1472 while (charpos--)
1473 {
1474 string_char_and_length (s, rest, &len);
1475 s += len, rest -= len;
1476 xassert (rest >= 0);
1477 CHARPOS (pos) += 1;
1478 BYTEPOS (pos) += len;
1479 }
1480 }
1481 else
1482 SET_TEXT_POS (pos, charpos, charpos);
1483
1484 return pos;
1485 }
1486
1487
1488 /* Value is the number of characters in C string S. MULTIBYTE_P
1489 non-zero means recognize multibyte characters. */
1490
1491 static int
1492 number_of_chars (s, multibyte_p)
1493 unsigned char *s;
1494 int multibyte_p;
1495 {
1496 int nchars;
1497
1498 if (multibyte_p)
1499 {
1500 int rest = strlen (s), len;
1501 unsigned char *p = (unsigned char *) s;
1502
1503 for (nchars = 0; rest > 0; ++nchars)
1504 {
1505 string_char_and_length (p, rest, &len);
1506 rest -= len, p += len;
1507 }
1508 }
1509 else
1510 nchars = strlen (s);
1511
1512 return nchars;
1513 }
1514
1515
1516 /* Compute byte position NEWPOS->bytepos corresponding to
1517 NEWPOS->charpos. POS is a known position in string STRING.
1518 NEWPOS->charpos must be >= POS.charpos. */
1519
1520 static void
1521 compute_string_pos (newpos, pos, string)
1522 struct text_pos *newpos, pos;
1523 Lisp_Object string;
1524 {
1525 xassert (STRINGP (string));
1526 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1527
1528 if (STRING_MULTIBYTE (string))
1529 *newpos = string_pos_nchars_ahead (pos, string,
1530 CHARPOS (*newpos) - CHARPOS (pos));
1531 else
1532 BYTEPOS (*newpos) = CHARPOS (*newpos);
1533 }
1534
1535 /* EXPORT:
1536 Return an estimation of the pixel height of mode or top lines on
1537 frame F. FACE_ID specifies what line's height to estimate. */
1538
1539 int
1540 estimate_mode_line_height (f, face_id)
1541 struct frame *f;
1542 enum face_id face_id;
1543 {
1544 #ifdef HAVE_WINDOW_SYSTEM
1545 if (FRAME_WINDOW_P (f))
1546 {
1547 int height = FONT_HEIGHT (FRAME_FONT (f));
1548
1549 /* This function is called so early when Emacs starts that the face
1550 cache and mode line face are not yet initialized. */
1551 if (FRAME_FACE_CACHE (f))
1552 {
1553 struct face *face = FACE_FROM_ID (f, face_id);
1554 if (face)
1555 {
1556 if (face->font)
1557 height = FONT_HEIGHT (face->font);
1558 if (face->box_line_width > 0)
1559 height += 2 * face->box_line_width;
1560 }
1561 }
1562
1563 return height;
1564 }
1565 #endif
1566
1567 return 1;
1568 }
1569
1570 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1571 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1572 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1573 not force the value into range. */
1574
1575 void
1576 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1577 FRAME_PTR f;
1578 register int pix_x, pix_y;
1579 int *x, *y;
1580 NativeRectangle *bounds;
1581 int noclip;
1582 {
1583
1584 #ifdef HAVE_WINDOW_SYSTEM
1585 if (FRAME_WINDOW_P (f))
1586 {
1587 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1588 even for negative values. */
1589 if (pix_x < 0)
1590 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1591 if (pix_y < 0)
1592 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1593
1594 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1595 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1596
1597 if (bounds)
1598 STORE_NATIVE_RECT (*bounds,
1599 FRAME_COL_TO_PIXEL_X (f, pix_x),
1600 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1601 FRAME_COLUMN_WIDTH (f) - 1,
1602 FRAME_LINE_HEIGHT (f) - 1);
1603
1604 if (!noclip)
1605 {
1606 if (pix_x < 0)
1607 pix_x = 0;
1608 else if (pix_x > FRAME_TOTAL_COLS (f))
1609 pix_x = FRAME_TOTAL_COLS (f);
1610
1611 if (pix_y < 0)
1612 pix_y = 0;
1613 else if (pix_y > FRAME_LINES (f))
1614 pix_y = FRAME_LINES (f);
1615 }
1616 }
1617 #endif
1618
1619 *x = pix_x;
1620 *y = pix_y;
1621 }
1622
1623
1624 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1625 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1626 can't tell the positions because W's display is not up to date,
1627 return 0. */
1628
1629 int
1630 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1631 struct window *w;
1632 int hpos, vpos;
1633 int *frame_x, *frame_y;
1634 {
1635 #ifdef HAVE_WINDOW_SYSTEM
1636 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1637 {
1638 int success_p;
1639
1640 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1641 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1642
1643 if (display_completed)
1644 {
1645 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1646 struct glyph *glyph = row->glyphs[TEXT_AREA];
1647 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1648
1649 hpos = row->x;
1650 vpos = row->y;
1651 while (glyph < end)
1652 {
1653 hpos += glyph->pixel_width;
1654 ++glyph;
1655 }
1656
1657 /* If first glyph is partially visible, its first visible position is still 0. */
1658 if (hpos < 0)
1659 hpos = 0;
1660
1661 success_p = 1;
1662 }
1663 else
1664 {
1665 hpos = vpos = 0;
1666 success_p = 0;
1667 }
1668
1669 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1670 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1671 return success_p;
1672 }
1673 #endif
1674
1675 *frame_x = hpos;
1676 *frame_y = vpos;
1677 return 1;
1678 }
1679
1680
1681 #ifdef HAVE_WINDOW_SYSTEM
1682
1683 /* Find the glyph under window-relative coordinates X/Y in window W.
1684 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1685 strings. Return in *HPOS and *VPOS the row and column number of
1686 the glyph found. Return in *AREA the glyph area containing X.
1687 Value is a pointer to the glyph found or null if X/Y is not on
1688 text, or we can't tell because W's current matrix is not up to
1689 date. */
1690
1691 static struct glyph *
1692 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1693 struct window *w;
1694 int x, y;
1695 int *hpos, *vpos, *dx, *dy, *area;
1696 {
1697 struct glyph *glyph, *end;
1698 struct glyph_row *row = NULL;
1699 int x0, i;
1700
1701 /* Find row containing Y. Give up if some row is not enabled. */
1702 for (i = 0; i < w->current_matrix->nrows; ++i)
1703 {
1704 row = MATRIX_ROW (w->current_matrix, i);
1705 if (!row->enabled_p)
1706 return NULL;
1707 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1708 break;
1709 }
1710
1711 *vpos = i;
1712 *hpos = 0;
1713
1714 /* Give up if Y is not in the window. */
1715 if (i == w->current_matrix->nrows)
1716 return NULL;
1717
1718 /* Get the glyph area containing X. */
1719 if (w->pseudo_window_p)
1720 {
1721 *area = TEXT_AREA;
1722 x0 = 0;
1723 }
1724 else
1725 {
1726 if (x < window_box_left_offset (w, TEXT_AREA))
1727 {
1728 *area = LEFT_MARGIN_AREA;
1729 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1730 }
1731 else if (x < window_box_right_offset (w, TEXT_AREA))
1732 {
1733 *area = TEXT_AREA;
1734 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1735 }
1736 else
1737 {
1738 *area = RIGHT_MARGIN_AREA;
1739 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1740 }
1741 }
1742
1743 /* Find glyph containing X. */
1744 glyph = row->glyphs[*area];
1745 end = glyph + row->used[*area];
1746 x -= x0;
1747 while (glyph < end && x >= glyph->pixel_width)
1748 {
1749 x -= glyph->pixel_width;
1750 ++glyph;
1751 }
1752
1753 if (glyph == end)
1754 return NULL;
1755
1756 if (dx)
1757 {
1758 *dx = x;
1759 *dy = y - (row->y + row->ascent - glyph->ascent);
1760 }
1761
1762 *hpos = glyph - row->glyphs[*area];
1763 return glyph;
1764 }
1765
1766
1767 /* EXPORT:
1768 Convert frame-relative x/y to coordinates relative to window W.
1769 Takes pseudo-windows into account. */
1770
1771 void
1772 frame_to_window_pixel_xy (w, x, y)
1773 struct window *w;
1774 int *x, *y;
1775 {
1776 if (w->pseudo_window_p)
1777 {
1778 /* A pseudo-window is always full-width, and starts at the
1779 left edge of the frame, plus a frame border. */
1780 struct frame *f = XFRAME (w->frame);
1781 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1782 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1783 }
1784 else
1785 {
1786 *x -= WINDOW_LEFT_EDGE_X (w);
1787 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1788 }
1789 }
1790
1791 /* EXPORT:
1792 Return in RECTS[] at most N clipping rectangles for glyph string S.
1793 Return the number of stored rectangles. */
1794
1795 int
1796 get_glyph_string_clip_rects (s, rects, n)
1797 struct glyph_string *s;
1798 NativeRectangle *rects;
1799 int n;
1800 {
1801 XRectangle r;
1802
1803 if (n <= 0)
1804 return 0;
1805
1806 if (s->row->full_width_p)
1807 {
1808 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1809 r.x = WINDOW_LEFT_EDGE_X (s->w);
1810 r.width = WINDOW_TOTAL_WIDTH (s->w);
1811
1812 /* Unless displaying a mode or menu bar line, which are always
1813 fully visible, clip to the visible part of the row. */
1814 if (s->w->pseudo_window_p)
1815 r.height = s->row->visible_height;
1816 else
1817 r.height = s->height;
1818 }
1819 else
1820 {
1821 /* This is a text line that may be partially visible. */
1822 r.x = window_box_left (s->w, s->area);
1823 r.width = window_box_width (s->w, s->area);
1824 r.height = s->row->visible_height;
1825 }
1826
1827 if (s->clip_head)
1828 if (r.x < s->clip_head->x)
1829 {
1830 if (r.width >= s->clip_head->x - r.x)
1831 r.width -= s->clip_head->x - r.x;
1832 else
1833 r.width = 0;
1834 r.x = s->clip_head->x;
1835 }
1836 if (s->clip_tail)
1837 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1838 {
1839 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1840 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1841 else
1842 r.width = 0;
1843 }
1844
1845 /* If S draws overlapping rows, it's sufficient to use the top and
1846 bottom of the window for clipping because this glyph string
1847 intentionally draws over other lines. */
1848 if (s->for_overlaps)
1849 {
1850 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1851 r.height = window_text_bottom_y (s->w) - r.y;
1852
1853 /* Alas, the above simple strategy does not work for the
1854 environments with anti-aliased text: if the same text is
1855 drawn onto the same place multiple times, it gets thicker.
1856 If the overlap we are processing is for the erased cursor, we
1857 take the intersection with the rectagle of the cursor. */
1858 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1859 {
1860 XRectangle rc, r_save = r;
1861
1862 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1863 rc.y = s->w->phys_cursor.y;
1864 rc.width = s->w->phys_cursor_width;
1865 rc.height = s->w->phys_cursor_height;
1866
1867 x_intersect_rectangles (&r_save, &rc, &r);
1868 }
1869 }
1870 else
1871 {
1872 /* Don't use S->y for clipping because it doesn't take partially
1873 visible lines into account. For example, it can be negative for
1874 partially visible lines at the top of a window. */
1875 if (!s->row->full_width_p
1876 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1877 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1878 else
1879 r.y = max (0, s->row->y);
1880
1881 /* If drawing a tool-bar window, draw it over the internal border
1882 at the top of the window. */
1883 if (WINDOWP (s->f->tool_bar_window)
1884 && s->w == XWINDOW (s->f->tool_bar_window))
1885 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1886 }
1887
1888 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1889
1890 /* If drawing the cursor, don't let glyph draw outside its
1891 advertised boundaries. Cleartype does this under some circumstances. */
1892 if (s->hl == DRAW_CURSOR)
1893 {
1894 struct glyph *glyph = s->first_glyph;
1895 int height, max_y;
1896
1897 if (s->x > r.x)
1898 {
1899 r.width -= s->x - r.x;
1900 r.x = s->x;
1901 }
1902 r.width = min (r.width, glyph->pixel_width);
1903
1904 /* If r.y is below window bottom, ensure that we still see a cursor. */
1905 height = min (glyph->ascent + glyph->descent,
1906 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1907 max_y = window_text_bottom_y (s->w) - height;
1908 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1909 if (s->ybase - glyph->ascent > max_y)
1910 {
1911 r.y = max_y;
1912 r.height = height;
1913 }
1914 else
1915 {
1916 /* Don't draw cursor glyph taller than our actual glyph. */
1917 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1918 if (height < r.height)
1919 {
1920 max_y = r.y + r.height;
1921 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1922 r.height = min (max_y - r.y, height);
1923 }
1924 }
1925 }
1926
1927 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1928 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1929 {
1930 #ifdef CONVERT_FROM_XRECT
1931 CONVERT_FROM_XRECT (r, *rects);
1932 #else
1933 *rects = r;
1934 #endif
1935 return 1;
1936 }
1937 else
1938 {
1939 /* If we are processing overlapping and allowed to return
1940 multiple clipping rectangles, we exclude the row of the glyph
1941 string from the clipping rectangle. This is to avoid drawing
1942 the same text on the environment with anti-aliasing. */
1943 #ifdef CONVERT_FROM_XRECT
1944 XRectangle rs[2];
1945 #else
1946 XRectangle *rs = rects;
1947 #endif
1948 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1949
1950 if (s->for_overlaps & OVERLAPS_PRED)
1951 {
1952 rs[i] = r;
1953 if (r.y + r.height > row_y)
1954 {
1955 if (r.y < row_y)
1956 rs[i].height = row_y - r.y;
1957 else
1958 rs[i].height = 0;
1959 }
1960 i++;
1961 }
1962 if (s->for_overlaps & OVERLAPS_SUCC)
1963 {
1964 rs[i] = r;
1965 if (r.y < row_y + s->row->visible_height)
1966 {
1967 if (r.y + r.height > row_y + s->row->visible_height)
1968 {
1969 rs[i].y = row_y + s->row->visible_height;
1970 rs[i].height = r.y + r.height - rs[i].y;
1971 }
1972 else
1973 rs[i].height = 0;
1974 }
1975 i++;
1976 }
1977
1978 n = i;
1979 #ifdef CONVERT_FROM_XRECT
1980 for (i = 0; i < n; i++)
1981 CONVERT_FROM_XRECT (rs[i], rects[i]);
1982 #endif
1983 return n;
1984 }
1985 }
1986
1987 /* EXPORT:
1988 Return in *NR the clipping rectangle for glyph string S. */
1989
1990 void
1991 get_glyph_string_clip_rect (s, nr)
1992 struct glyph_string *s;
1993 NativeRectangle *nr;
1994 {
1995 get_glyph_string_clip_rects (s, nr, 1);
1996 }
1997
1998
1999 /* EXPORT:
2000 Return the position and height of the phys cursor in window W.
2001 Set w->phys_cursor_width to width of phys cursor.
2002 */
2003
2004 void
2005 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2006 struct window *w;
2007 struct glyph_row *row;
2008 struct glyph *glyph;
2009 int *xp, *yp, *heightp;
2010 {
2011 struct frame *f = XFRAME (WINDOW_FRAME (w));
2012 int x, y, wd, h, h0, y0;
2013
2014 /* Compute the width of the rectangle to draw. If on a stretch
2015 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2016 rectangle as wide as the glyph, but use a canonical character
2017 width instead. */
2018 wd = glyph->pixel_width - 1;
2019 #ifdef HAVE_NTGUI
2020 wd++; /* Why? */
2021 #endif
2022
2023 x = w->phys_cursor.x;
2024 if (x < 0)
2025 {
2026 wd += x;
2027 x = 0;
2028 }
2029
2030 if (glyph->type == STRETCH_GLYPH
2031 && !x_stretch_cursor_p)
2032 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2033 w->phys_cursor_width = wd;
2034
2035 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2036
2037 /* If y is below window bottom, ensure that we still see a cursor. */
2038 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2039
2040 h = max (h0, glyph->ascent + glyph->descent);
2041 h0 = min (h0, glyph->ascent + glyph->descent);
2042
2043 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2044 if (y < y0)
2045 {
2046 h = max (h - (y0 - y) + 1, h0);
2047 y = y0 - 1;
2048 }
2049 else
2050 {
2051 y0 = window_text_bottom_y (w) - h0;
2052 if (y > y0)
2053 {
2054 h += y - y0;
2055 y = y0;
2056 }
2057 }
2058
2059 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2060 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2061 *heightp = h;
2062 }
2063
2064 /*
2065 * Remember which glyph the mouse is over.
2066 */
2067
2068 void
2069 remember_mouse_glyph (f, gx, gy, rect)
2070 struct frame *f;
2071 int gx, gy;
2072 NativeRectangle *rect;
2073 {
2074 Lisp_Object window;
2075 struct window *w;
2076 struct glyph_row *r, *gr, *end_row;
2077 enum window_part part;
2078 enum glyph_row_area area;
2079 int x, y, width, height;
2080
2081 /* Try to determine frame pixel position and size of the glyph under
2082 frame pixel coordinates X/Y on frame F. */
2083
2084 if (!f->glyphs_initialized_p
2085 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2086 NILP (window)))
2087 {
2088 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2089 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2090 goto virtual_glyph;
2091 }
2092
2093 w = XWINDOW (window);
2094 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2095 height = WINDOW_FRAME_LINE_HEIGHT (w);
2096
2097 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2098 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2099
2100 if (w->pseudo_window_p)
2101 {
2102 area = TEXT_AREA;
2103 part = ON_MODE_LINE; /* Don't adjust margin. */
2104 goto text_glyph;
2105 }
2106
2107 switch (part)
2108 {
2109 case ON_LEFT_MARGIN:
2110 area = LEFT_MARGIN_AREA;
2111 goto text_glyph;
2112
2113 case ON_RIGHT_MARGIN:
2114 area = RIGHT_MARGIN_AREA;
2115 goto text_glyph;
2116
2117 case ON_HEADER_LINE:
2118 case ON_MODE_LINE:
2119 gr = (part == ON_HEADER_LINE
2120 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2121 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2122 gy = gr->y;
2123 area = TEXT_AREA;
2124 goto text_glyph_row_found;
2125
2126 case ON_TEXT:
2127 area = TEXT_AREA;
2128
2129 text_glyph:
2130 gr = 0; gy = 0;
2131 for (; r <= end_row && r->enabled_p; ++r)
2132 if (r->y + r->height > y)
2133 {
2134 gr = r; gy = r->y;
2135 break;
2136 }
2137
2138 text_glyph_row_found:
2139 if (gr && gy <= y)
2140 {
2141 struct glyph *g = gr->glyphs[area];
2142 struct glyph *end = g + gr->used[area];
2143
2144 height = gr->height;
2145 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2146 if (gx + g->pixel_width > x)
2147 break;
2148
2149 if (g < end)
2150 {
2151 if (g->type == IMAGE_GLYPH)
2152 {
2153 /* Don't remember when mouse is over image, as
2154 image may have hot-spots. */
2155 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2156 return;
2157 }
2158 width = g->pixel_width;
2159 }
2160 else
2161 {
2162 /* Use nominal char spacing at end of line. */
2163 x -= gx;
2164 gx += (x / width) * width;
2165 }
2166
2167 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2168 gx += window_box_left_offset (w, area);
2169 }
2170 else
2171 {
2172 /* Use nominal line height at end of window. */
2173 gx = (x / width) * width;
2174 y -= gy;
2175 gy += (y / height) * height;
2176 }
2177 break;
2178
2179 case ON_LEFT_FRINGE:
2180 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2181 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2182 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2183 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2184 goto row_glyph;
2185
2186 case ON_RIGHT_FRINGE:
2187 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2188 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2189 : window_box_right_offset (w, TEXT_AREA));
2190 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2191 goto row_glyph;
2192
2193 case ON_SCROLL_BAR:
2194 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2195 ? 0
2196 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2197 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2198 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2199 : 0)));
2200 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2201
2202 row_glyph:
2203 gr = 0, gy = 0;
2204 for (; r <= end_row && r->enabled_p; ++r)
2205 if (r->y + r->height > y)
2206 {
2207 gr = r; gy = r->y;
2208 break;
2209 }
2210
2211 if (gr && gy <= y)
2212 height = gr->height;
2213 else
2214 {
2215 /* Use nominal line height at end of window. */
2216 y -= gy;
2217 gy += (y / height) * height;
2218 }
2219 break;
2220
2221 default:
2222 ;
2223 virtual_glyph:
2224 /* If there is no glyph under the mouse, then we divide the screen
2225 into a grid of the smallest glyph in the frame, and use that
2226 as our "glyph". */
2227
2228 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2229 round down even for negative values. */
2230 if (gx < 0)
2231 gx -= width - 1;
2232 if (gy < 0)
2233 gy -= height - 1;
2234
2235 gx = (gx / width) * width;
2236 gy = (gy / height) * height;
2237
2238 goto store_rect;
2239 }
2240
2241 gx += WINDOW_LEFT_EDGE_X (w);
2242 gy += WINDOW_TOP_EDGE_Y (w);
2243
2244 store_rect:
2245 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2246
2247 /* Visible feedback for debugging. */
2248 #if 0
2249 #if HAVE_X_WINDOWS
2250 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2251 f->output_data.x->normal_gc,
2252 gx, gy, width, height);
2253 #endif
2254 #endif
2255 }
2256
2257
2258 #endif /* HAVE_WINDOW_SYSTEM */
2259
2260 \f
2261 /***********************************************************************
2262 Lisp form evaluation
2263 ***********************************************************************/
2264
2265 /* Error handler for safe_eval and safe_call. */
2266
2267 static Lisp_Object
2268 safe_eval_handler (arg)
2269 Lisp_Object arg;
2270 {
2271 add_to_log ("Error during redisplay: %s", arg, Qnil);
2272 return Qnil;
2273 }
2274
2275
2276 /* Evaluate SEXPR and return the result, or nil if something went
2277 wrong. Prevent redisplay during the evaluation. */
2278
2279 Lisp_Object
2280 safe_eval (sexpr)
2281 Lisp_Object sexpr;
2282 {
2283 Lisp_Object val;
2284
2285 if (inhibit_eval_during_redisplay)
2286 val = Qnil;
2287 else
2288 {
2289 int count = SPECPDL_INDEX ();
2290 struct gcpro gcpro1;
2291
2292 GCPRO1 (sexpr);
2293 specbind (Qinhibit_redisplay, Qt);
2294 /* Use Qt to ensure debugger does not run,
2295 so there is no possibility of wanting to redisplay. */
2296 val = internal_condition_case_1 (Feval, sexpr, Qt,
2297 safe_eval_handler);
2298 UNGCPRO;
2299 val = unbind_to (count, val);
2300 }
2301
2302 return val;
2303 }
2304
2305
2306 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2307 Return the result, or nil if something went wrong. Prevent
2308 redisplay during the evaluation. */
2309
2310 Lisp_Object
2311 safe_call (nargs, args)
2312 int nargs;
2313 Lisp_Object *args;
2314 {
2315 Lisp_Object val;
2316
2317 if (inhibit_eval_during_redisplay)
2318 val = Qnil;
2319 else
2320 {
2321 int count = SPECPDL_INDEX ();
2322 struct gcpro gcpro1;
2323
2324 GCPRO1 (args[0]);
2325 gcpro1.nvars = nargs;
2326 specbind (Qinhibit_redisplay, Qt);
2327 /* Use Qt to ensure debugger does not run,
2328 so there is no possibility of wanting to redisplay. */
2329 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2330 safe_eval_handler);
2331 UNGCPRO;
2332 val = unbind_to (count, val);
2333 }
2334
2335 return val;
2336 }
2337
2338
2339 /* Call function FN with one argument ARG.
2340 Return the result, or nil if something went wrong. */
2341
2342 Lisp_Object
2343 safe_call1 (fn, arg)
2344 Lisp_Object fn, arg;
2345 {
2346 Lisp_Object args[2];
2347 args[0] = fn;
2348 args[1] = arg;
2349 return safe_call (2, args);
2350 }
2351
2352
2353 \f
2354 /***********************************************************************
2355 Debugging
2356 ***********************************************************************/
2357
2358 #if 0
2359
2360 /* Define CHECK_IT to perform sanity checks on iterators.
2361 This is for debugging. It is too slow to do unconditionally. */
2362
2363 static void
2364 check_it (it)
2365 struct it *it;
2366 {
2367 if (it->method == GET_FROM_STRING)
2368 {
2369 xassert (STRINGP (it->string));
2370 xassert (IT_STRING_CHARPOS (*it) >= 0);
2371 }
2372 else
2373 {
2374 xassert (IT_STRING_CHARPOS (*it) < 0);
2375 if (it->method == GET_FROM_BUFFER)
2376 {
2377 /* Check that character and byte positions agree. */
2378 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2379 }
2380 }
2381
2382 if (it->dpvec)
2383 xassert (it->current.dpvec_index >= 0);
2384 else
2385 xassert (it->current.dpvec_index < 0);
2386 }
2387
2388 #define CHECK_IT(IT) check_it ((IT))
2389
2390 #else /* not 0 */
2391
2392 #define CHECK_IT(IT) (void) 0
2393
2394 #endif /* not 0 */
2395
2396
2397 #if GLYPH_DEBUG
2398
2399 /* Check that the window end of window W is what we expect it
2400 to be---the last row in the current matrix displaying text. */
2401
2402 static void
2403 check_window_end (w)
2404 struct window *w;
2405 {
2406 if (!MINI_WINDOW_P (w)
2407 && !NILP (w->window_end_valid))
2408 {
2409 struct glyph_row *row;
2410 xassert ((row = MATRIX_ROW (w->current_matrix,
2411 XFASTINT (w->window_end_vpos)),
2412 !row->enabled_p
2413 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2414 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2415 }
2416 }
2417
2418 #define CHECK_WINDOW_END(W) check_window_end ((W))
2419
2420 #else /* not GLYPH_DEBUG */
2421
2422 #define CHECK_WINDOW_END(W) (void) 0
2423
2424 #endif /* not GLYPH_DEBUG */
2425
2426
2427 \f
2428 /***********************************************************************
2429 Iterator initialization
2430 ***********************************************************************/
2431
2432 /* Initialize IT for displaying current_buffer in window W, starting
2433 at character position CHARPOS. CHARPOS < 0 means that no buffer
2434 position is specified which is useful when the iterator is assigned
2435 a position later. BYTEPOS is the byte position corresponding to
2436 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2437
2438 If ROW is not null, calls to produce_glyphs with IT as parameter
2439 will produce glyphs in that row.
2440
2441 BASE_FACE_ID is the id of a base face to use. It must be one of
2442 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2443 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2444 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2445
2446 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2447 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2448 will be initialized to use the corresponding mode line glyph row of
2449 the desired matrix of W. */
2450
2451 void
2452 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2453 struct it *it;
2454 struct window *w;
2455 int charpos, bytepos;
2456 struct glyph_row *row;
2457 enum face_id base_face_id;
2458 {
2459 int highlight_region_p;
2460
2461 /* Some precondition checks. */
2462 xassert (w != NULL && it != NULL);
2463 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2464 && charpos <= ZV));
2465
2466 /* If face attributes have been changed since the last redisplay,
2467 free realized faces now because they depend on face definitions
2468 that might have changed. Don't free faces while there might be
2469 desired matrices pending which reference these faces. */
2470 if (face_change_count && !inhibit_free_realized_faces)
2471 {
2472 face_change_count = 0;
2473 free_all_realized_faces (Qnil);
2474 }
2475
2476 /* Use one of the mode line rows of W's desired matrix if
2477 appropriate. */
2478 if (row == NULL)
2479 {
2480 if (base_face_id == MODE_LINE_FACE_ID
2481 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2482 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2483 else if (base_face_id == HEADER_LINE_FACE_ID)
2484 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2485 }
2486
2487 /* Clear IT. */
2488 bzero (it, sizeof *it);
2489 it->current.overlay_string_index = -1;
2490 it->current.dpvec_index = -1;
2491 it->base_face_id = base_face_id;
2492 it->string = Qnil;
2493 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2494
2495 /* The window in which we iterate over current_buffer: */
2496 XSETWINDOW (it->window, w);
2497 it->w = w;
2498 it->f = XFRAME (w->frame);
2499
2500 /* Extra space between lines (on window systems only). */
2501 if (base_face_id == DEFAULT_FACE_ID
2502 && FRAME_WINDOW_P (it->f))
2503 {
2504 if (NATNUMP (current_buffer->extra_line_spacing))
2505 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2506 else if (FLOATP (current_buffer->extra_line_spacing))
2507 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2508 * FRAME_LINE_HEIGHT (it->f));
2509 else if (it->f->extra_line_spacing > 0)
2510 it->extra_line_spacing = it->f->extra_line_spacing;
2511 it->max_extra_line_spacing = 0;
2512 }
2513
2514 /* If realized faces have been removed, e.g. because of face
2515 attribute changes of named faces, recompute them. When running
2516 in batch mode, the face cache of the initial frame is null. If
2517 we happen to get called, make a dummy face cache. */
2518 if (FRAME_FACE_CACHE (it->f) == NULL)
2519 init_frame_faces (it->f);
2520 if (FRAME_FACE_CACHE (it->f)->used == 0)
2521 recompute_basic_faces (it->f);
2522
2523 /* Current value of the `slice', `space-width', and 'height' properties. */
2524 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2525 it->space_width = Qnil;
2526 it->font_height = Qnil;
2527 it->override_ascent = -1;
2528
2529 /* Are control characters displayed as `^C'? */
2530 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2531
2532 /* -1 means everything between a CR and the following line end
2533 is invisible. >0 means lines indented more than this value are
2534 invisible. */
2535 it->selective = (INTEGERP (current_buffer->selective_display)
2536 ? XFASTINT (current_buffer->selective_display)
2537 : (!NILP (current_buffer->selective_display)
2538 ? -1 : 0));
2539 it->selective_display_ellipsis_p
2540 = !NILP (current_buffer->selective_display_ellipses);
2541
2542 /* Display table to use. */
2543 it->dp = window_display_table (w);
2544
2545 /* Are multibyte characters enabled in current_buffer? */
2546 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2547
2548 /* Non-zero if we should highlight the region. */
2549 highlight_region_p
2550 = (!NILP (Vtransient_mark_mode)
2551 && !NILP (current_buffer->mark_active)
2552 && XMARKER (current_buffer->mark)->buffer != 0);
2553
2554 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2555 start and end of a visible region in window IT->w. Set both to
2556 -1 to indicate no region. */
2557 if (highlight_region_p
2558 /* Maybe highlight only in selected window. */
2559 && (/* Either show region everywhere. */
2560 highlight_nonselected_windows
2561 /* Or show region in the selected window. */
2562 || w == XWINDOW (selected_window)
2563 /* Or show the region if we are in the mini-buffer and W is
2564 the window the mini-buffer refers to. */
2565 || (MINI_WINDOW_P (XWINDOW (selected_window))
2566 && WINDOWP (minibuf_selected_window)
2567 && w == XWINDOW (minibuf_selected_window))))
2568 {
2569 int charpos = marker_position (current_buffer->mark);
2570 it->region_beg_charpos = min (PT, charpos);
2571 it->region_end_charpos = max (PT, charpos);
2572 }
2573 else
2574 it->region_beg_charpos = it->region_end_charpos = -1;
2575
2576 /* Get the position at which the redisplay_end_trigger hook should
2577 be run, if it is to be run at all. */
2578 if (MARKERP (w->redisplay_end_trigger)
2579 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2580 it->redisplay_end_trigger_charpos
2581 = marker_position (w->redisplay_end_trigger);
2582 else if (INTEGERP (w->redisplay_end_trigger))
2583 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2584
2585 /* Correct bogus values of tab_width. */
2586 it->tab_width = XINT (current_buffer->tab_width);
2587 if (it->tab_width <= 0 || it->tab_width > 1000)
2588 it->tab_width = 8;
2589
2590 /* Are lines in the display truncated? */
2591 it->truncate_lines_p
2592 = (base_face_id != DEFAULT_FACE_ID
2593 || XINT (it->w->hscroll)
2594 || (truncate_partial_width_windows
2595 && !WINDOW_FULL_WIDTH_P (it->w))
2596 || !NILP (current_buffer->truncate_lines));
2597
2598 /* Get dimensions of truncation and continuation glyphs. These are
2599 displayed as fringe bitmaps under X, so we don't need them for such
2600 frames. */
2601 if (!FRAME_WINDOW_P (it->f))
2602 {
2603 if (it->truncate_lines_p)
2604 {
2605 /* We will need the truncation glyph. */
2606 xassert (it->glyph_row == NULL);
2607 produce_special_glyphs (it, IT_TRUNCATION);
2608 it->truncation_pixel_width = it->pixel_width;
2609 }
2610 else
2611 {
2612 /* We will need the continuation glyph. */
2613 xassert (it->glyph_row == NULL);
2614 produce_special_glyphs (it, IT_CONTINUATION);
2615 it->continuation_pixel_width = it->pixel_width;
2616 }
2617
2618 /* Reset these values to zero because the produce_special_glyphs
2619 above has changed them. */
2620 it->pixel_width = it->ascent = it->descent = 0;
2621 it->phys_ascent = it->phys_descent = 0;
2622 }
2623
2624 /* Set this after getting the dimensions of truncation and
2625 continuation glyphs, so that we don't produce glyphs when calling
2626 produce_special_glyphs, above. */
2627 it->glyph_row = row;
2628 it->area = TEXT_AREA;
2629
2630 /* Get the dimensions of the display area. The display area
2631 consists of the visible window area plus a horizontally scrolled
2632 part to the left of the window. All x-values are relative to the
2633 start of this total display area. */
2634 if (base_face_id != DEFAULT_FACE_ID)
2635 {
2636 /* Mode lines, menu bar in terminal frames. */
2637 it->first_visible_x = 0;
2638 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2639 }
2640 else
2641 {
2642 it->first_visible_x
2643 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2644 it->last_visible_x = (it->first_visible_x
2645 + window_box_width (w, TEXT_AREA));
2646
2647 /* If we truncate lines, leave room for the truncator glyph(s) at
2648 the right margin. Otherwise, leave room for the continuation
2649 glyph(s). Truncation and continuation glyphs are not inserted
2650 for window-based redisplay. */
2651 if (!FRAME_WINDOW_P (it->f))
2652 {
2653 if (it->truncate_lines_p)
2654 it->last_visible_x -= it->truncation_pixel_width;
2655 else
2656 it->last_visible_x -= it->continuation_pixel_width;
2657 }
2658
2659 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2660 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2661 }
2662
2663 /* Leave room for a border glyph. */
2664 if (!FRAME_WINDOW_P (it->f)
2665 && !WINDOW_RIGHTMOST_P (it->w))
2666 it->last_visible_x -= 1;
2667
2668 it->last_visible_y = window_text_bottom_y (w);
2669
2670 /* For mode lines and alike, arrange for the first glyph having a
2671 left box line if the face specifies a box. */
2672 if (base_face_id != DEFAULT_FACE_ID)
2673 {
2674 struct face *face;
2675
2676 it->face_id = base_face_id;
2677
2678 /* If we have a boxed mode line, make the first character appear
2679 with a left box line. */
2680 face = FACE_FROM_ID (it->f, base_face_id);
2681 if (face->box != FACE_NO_BOX)
2682 it->start_of_box_run_p = 1;
2683 }
2684
2685 /* If a buffer position was specified, set the iterator there,
2686 getting overlays and face properties from that position. */
2687 if (charpos >= BUF_BEG (current_buffer))
2688 {
2689 it->end_charpos = ZV;
2690 it->face_id = -1;
2691 IT_CHARPOS (*it) = charpos;
2692
2693 /* Compute byte position if not specified. */
2694 if (bytepos < charpos)
2695 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2696 else
2697 IT_BYTEPOS (*it) = bytepos;
2698
2699 it->start = it->current;
2700
2701 /* Compute faces etc. */
2702 reseat (it, it->current.pos, 1);
2703 }
2704
2705 CHECK_IT (it);
2706 }
2707
2708
2709 /* Initialize IT for the display of window W with window start POS. */
2710
2711 void
2712 start_display (it, w, pos)
2713 struct it *it;
2714 struct window *w;
2715 struct text_pos pos;
2716 {
2717 struct glyph_row *row;
2718 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2719
2720 row = w->desired_matrix->rows + first_vpos;
2721 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2722 it->first_vpos = first_vpos;
2723
2724 /* Don't reseat to previous visible line start if current start
2725 position is in a string or image. */
2726 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2727 {
2728 int start_at_line_beg_p;
2729 int first_y = it->current_y;
2730
2731 /* If window start is not at a line start, skip forward to POS to
2732 get the correct continuation lines width. */
2733 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2734 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2735 if (!start_at_line_beg_p)
2736 {
2737 int new_x;
2738
2739 reseat_at_previous_visible_line_start (it);
2740 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2741
2742 new_x = it->current_x + it->pixel_width;
2743
2744 /* If lines are continued, this line may end in the middle
2745 of a multi-glyph character (e.g. a control character
2746 displayed as \003, or in the middle of an overlay
2747 string). In this case move_it_to above will not have
2748 taken us to the start of the continuation line but to the
2749 end of the continued line. */
2750 if (it->current_x > 0
2751 && !it->truncate_lines_p /* Lines are continued. */
2752 && (/* And glyph doesn't fit on the line. */
2753 new_x > it->last_visible_x
2754 /* Or it fits exactly and we're on a window
2755 system frame. */
2756 || (new_x == it->last_visible_x
2757 && FRAME_WINDOW_P (it->f))))
2758 {
2759 if (it->current.dpvec_index >= 0
2760 || it->current.overlay_string_index >= 0)
2761 {
2762 set_iterator_to_next (it, 1);
2763 move_it_in_display_line_to (it, -1, -1, 0);
2764 }
2765
2766 it->continuation_lines_width += it->current_x;
2767 }
2768
2769 /* We're starting a new display line, not affected by the
2770 height of the continued line, so clear the appropriate
2771 fields in the iterator structure. */
2772 it->max_ascent = it->max_descent = 0;
2773 it->max_phys_ascent = it->max_phys_descent = 0;
2774
2775 it->current_y = first_y;
2776 it->vpos = 0;
2777 it->current_x = it->hpos = 0;
2778 }
2779 }
2780
2781 #if 0 /* Don't assert the following because start_display is sometimes
2782 called intentionally with a window start that is not at a
2783 line start. Please leave this code in as a comment. */
2784
2785 /* Window start should be on a line start, now. */
2786 xassert (it->continuation_lines_width
2787 || IT_CHARPOS (it) == BEGV
2788 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2789 #endif /* 0 */
2790 }
2791
2792
2793 /* Return 1 if POS is a position in ellipses displayed for invisible
2794 text. W is the window we display, for text property lookup. */
2795
2796 static int
2797 in_ellipses_for_invisible_text_p (pos, w)
2798 struct display_pos *pos;
2799 struct window *w;
2800 {
2801 Lisp_Object prop, window;
2802 int ellipses_p = 0;
2803 int charpos = CHARPOS (pos->pos);
2804
2805 /* If POS specifies a position in a display vector, this might
2806 be for an ellipsis displayed for invisible text. We won't
2807 get the iterator set up for delivering that ellipsis unless
2808 we make sure that it gets aware of the invisible text. */
2809 if (pos->dpvec_index >= 0
2810 && pos->overlay_string_index < 0
2811 && CHARPOS (pos->string_pos) < 0
2812 && charpos > BEGV
2813 && (XSETWINDOW (window, w),
2814 prop = Fget_char_property (make_number (charpos),
2815 Qinvisible, window),
2816 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2817 {
2818 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2819 window);
2820 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2821 }
2822
2823 return ellipses_p;
2824 }
2825
2826
2827 /* Initialize IT for stepping through current_buffer in window W,
2828 starting at position POS that includes overlay string and display
2829 vector/ control character translation position information. Value
2830 is zero if there are overlay strings with newlines at POS. */
2831
2832 static int
2833 init_from_display_pos (it, w, pos)
2834 struct it *it;
2835 struct window *w;
2836 struct display_pos *pos;
2837 {
2838 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2839 int i, overlay_strings_with_newlines = 0;
2840
2841 /* If POS specifies a position in a display vector, this might
2842 be for an ellipsis displayed for invisible text. We won't
2843 get the iterator set up for delivering that ellipsis unless
2844 we make sure that it gets aware of the invisible text. */
2845 if (in_ellipses_for_invisible_text_p (pos, w))
2846 {
2847 --charpos;
2848 bytepos = 0;
2849 }
2850
2851 /* Keep in mind: the call to reseat in init_iterator skips invisible
2852 text, so we might end up at a position different from POS. This
2853 is only a problem when POS is a row start after a newline and an
2854 overlay starts there with an after-string, and the overlay has an
2855 invisible property. Since we don't skip invisible text in
2856 display_line and elsewhere immediately after consuming the
2857 newline before the row start, such a POS will not be in a string,
2858 but the call to init_iterator below will move us to the
2859 after-string. */
2860 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2861
2862 /* This only scans the current chunk -- it should scan all chunks.
2863 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2864 to 16 in 22.1 to make this a lesser problem. */
2865 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2866 {
2867 const char *s = SDATA (it->overlay_strings[i]);
2868 const char *e = s + SBYTES (it->overlay_strings[i]);
2869
2870 while (s < e && *s != '\n')
2871 ++s;
2872
2873 if (s < e)
2874 {
2875 overlay_strings_with_newlines = 1;
2876 break;
2877 }
2878 }
2879
2880 /* If position is within an overlay string, set up IT to the right
2881 overlay string. */
2882 if (pos->overlay_string_index >= 0)
2883 {
2884 int relative_index;
2885
2886 /* If the first overlay string happens to have a `display'
2887 property for an image, the iterator will be set up for that
2888 image, and we have to undo that setup first before we can
2889 correct the overlay string index. */
2890 if (it->method == GET_FROM_IMAGE)
2891 pop_it (it);
2892
2893 /* We already have the first chunk of overlay strings in
2894 IT->overlay_strings. Load more until the one for
2895 pos->overlay_string_index is in IT->overlay_strings. */
2896 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2897 {
2898 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2899 it->current.overlay_string_index = 0;
2900 while (n--)
2901 {
2902 load_overlay_strings (it, 0);
2903 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2904 }
2905 }
2906
2907 it->current.overlay_string_index = pos->overlay_string_index;
2908 relative_index = (it->current.overlay_string_index
2909 % OVERLAY_STRING_CHUNK_SIZE);
2910 it->string = it->overlay_strings[relative_index];
2911 xassert (STRINGP (it->string));
2912 it->current.string_pos = pos->string_pos;
2913 it->method = GET_FROM_STRING;
2914 }
2915
2916 #if 0 /* This is bogus because POS not having an overlay string
2917 position does not mean it's after the string. Example: A
2918 line starting with a before-string and initialization of IT
2919 to the previous row's end position. */
2920 else if (it->current.overlay_string_index >= 0)
2921 {
2922 /* If POS says we're already after an overlay string ending at
2923 POS, make sure to pop the iterator because it will be in
2924 front of that overlay string. When POS is ZV, we've thereby
2925 also ``processed'' overlay strings at ZV. */
2926 while (it->sp)
2927 pop_it (it);
2928 xassert (it->current.overlay_string_index == -1);
2929 xassert (it->method == GET_FROM_BUFFER);
2930 if (CHARPOS (pos->pos) == ZV)
2931 it->overlay_strings_at_end_processed_p = 1;
2932 }
2933 #endif /* 0 */
2934
2935 if (CHARPOS (pos->string_pos) >= 0)
2936 {
2937 /* Recorded position is not in an overlay string, but in another
2938 string. This can only be a string from a `display' property.
2939 IT should already be filled with that string. */
2940 it->current.string_pos = pos->string_pos;
2941 xassert (STRINGP (it->string));
2942 }
2943
2944 /* Restore position in display vector translations, control
2945 character translations or ellipses. */
2946 if (pos->dpvec_index >= 0)
2947 {
2948 if (it->dpvec == NULL)
2949 get_next_display_element (it);
2950 xassert (it->dpvec && it->current.dpvec_index == 0);
2951 it->current.dpvec_index = pos->dpvec_index;
2952 }
2953
2954 CHECK_IT (it);
2955 return !overlay_strings_with_newlines;
2956 }
2957
2958
2959 /* Initialize IT for stepping through current_buffer in window W
2960 starting at ROW->start. */
2961
2962 static void
2963 init_to_row_start (it, w, row)
2964 struct it *it;
2965 struct window *w;
2966 struct glyph_row *row;
2967 {
2968 init_from_display_pos (it, w, &row->start);
2969 it->start = row->start;
2970 it->continuation_lines_width = row->continuation_lines_width;
2971 CHECK_IT (it);
2972 }
2973
2974
2975 /* Initialize IT for stepping through current_buffer in window W
2976 starting in the line following ROW, i.e. starting at ROW->end.
2977 Value is zero if there are overlay strings with newlines at ROW's
2978 end position. */
2979
2980 static int
2981 init_to_row_end (it, w, row)
2982 struct it *it;
2983 struct window *w;
2984 struct glyph_row *row;
2985 {
2986 int success = 0;
2987
2988 if (init_from_display_pos (it, w, &row->end))
2989 {
2990 if (row->continued_p)
2991 it->continuation_lines_width
2992 = row->continuation_lines_width + row->pixel_width;
2993 CHECK_IT (it);
2994 success = 1;
2995 }
2996
2997 return success;
2998 }
2999
3000
3001
3002 \f
3003 /***********************************************************************
3004 Text properties
3005 ***********************************************************************/
3006
3007 /* Called when IT reaches IT->stop_charpos. Handle text property and
3008 overlay changes. Set IT->stop_charpos to the next position where
3009 to stop. */
3010
3011 static void
3012 handle_stop (it)
3013 struct it *it;
3014 {
3015 enum prop_handled handled;
3016 int handle_overlay_change_p;
3017 struct props *p;
3018
3019 it->dpvec = NULL;
3020 it->current.dpvec_index = -1;
3021 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3022 it->ignore_overlay_strings_at_pos_p = 0;
3023
3024 /* Use face of preceding text for ellipsis (if invisible) */
3025 if (it->selective_display_ellipsis_p)
3026 it->saved_face_id = it->face_id;
3027
3028 do
3029 {
3030 handled = HANDLED_NORMALLY;
3031
3032 /* Call text property handlers. */
3033 for (p = it_props; p->handler; ++p)
3034 {
3035 handled = p->handler (it);
3036
3037 if (handled == HANDLED_RECOMPUTE_PROPS)
3038 break;
3039 else if (handled == HANDLED_RETURN)
3040 {
3041 /* We still want to show before and after strings from
3042 overlays even if the actual buffer text is replaced. */
3043 if (!handle_overlay_change_p || it->sp > 1)
3044 return;
3045 if (!get_overlay_strings_1 (it, 0, 0))
3046 return;
3047 it->ignore_overlay_strings_at_pos_p = 1;
3048 it->string_from_display_prop_p = 0;
3049 handle_overlay_change_p = 0;
3050 handled = HANDLED_RECOMPUTE_PROPS;
3051 break;
3052 }
3053 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3054 handle_overlay_change_p = 0;
3055 }
3056
3057 if (handled != HANDLED_RECOMPUTE_PROPS)
3058 {
3059 /* Don't check for overlay strings below when set to deliver
3060 characters from a display vector. */
3061 if (it->method == GET_FROM_DISPLAY_VECTOR)
3062 handle_overlay_change_p = 0;
3063
3064 /* Handle overlay changes.
3065 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3066 if it finds overlays. */
3067 if (handle_overlay_change_p)
3068 handled = handle_overlay_change (it);
3069 }
3070 }
3071 while (handled == HANDLED_RECOMPUTE_PROPS);
3072
3073 /* Determine where to stop next. */
3074 if (handled == HANDLED_NORMALLY)
3075 compute_stop_pos (it);
3076 }
3077
3078
3079 /* Compute IT->stop_charpos from text property and overlay change
3080 information for IT's current position. */
3081
3082 static void
3083 compute_stop_pos (it)
3084 struct it *it;
3085 {
3086 register INTERVAL iv, next_iv;
3087 Lisp_Object object, limit, position;
3088
3089 /* If nowhere else, stop at the end. */
3090 it->stop_charpos = it->end_charpos;
3091
3092 if (STRINGP (it->string))
3093 {
3094 /* Strings are usually short, so don't limit the search for
3095 properties. */
3096 object = it->string;
3097 limit = Qnil;
3098 position = make_number (IT_STRING_CHARPOS (*it));
3099 }
3100 else
3101 {
3102 int charpos;
3103
3104 /* If next overlay change is in front of the current stop pos
3105 (which is IT->end_charpos), stop there. Note: value of
3106 next_overlay_change is point-max if no overlay change
3107 follows. */
3108 charpos = next_overlay_change (IT_CHARPOS (*it));
3109 if (charpos < it->stop_charpos)
3110 it->stop_charpos = charpos;
3111
3112 /* If showing the region, we have to stop at the region
3113 start or end because the face might change there. */
3114 if (it->region_beg_charpos > 0)
3115 {
3116 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3117 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3118 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3119 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3120 }
3121
3122 /* Set up variables for computing the stop position from text
3123 property changes. */
3124 XSETBUFFER (object, current_buffer);
3125 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3126 position = make_number (IT_CHARPOS (*it));
3127
3128 }
3129
3130 /* Get the interval containing IT's position. Value is a null
3131 interval if there isn't such an interval. */
3132 iv = validate_interval_range (object, &position, &position, 0);
3133 if (!NULL_INTERVAL_P (iv))
3134 {
3135 Lisp_Object values_here[LAST_PROP_IDX];
3136 struct props *p;
3137
3138 /* Get properties here. */
3139 for (p = it_props; p->handler; ++p)
3140 values_here[p->idx] = textget (iv->plist, *p->name);
3141
3142 /* Look for an interval following iv that has different
3143 properties. */
3144 for (next_iv = next_interval (iv);
3145 (!NULL_INTERVAL_P (next_iv)
3146 && (NILP (limit)
3147 || XFASTINT (limit) > next_iv->position));
3148 next_iv = next_interval (next_iv))
3149 {
3150 for (p = it_props; p->handler; ++p)
3151 {
3152 Lisp_Object new_value;
3153
3154 new_value = textget (next_iv->plist, *p->name);
3155 if (!EQ (values_here[p->idx], new_value))
3156 break;
3157 }
3158
3159 if (p->handler)
3160 break;
3161 }
3162
3163 if (!NULL_INTERVAL_P (next_iv))
3164 {
3165 if (INTEGERP (limit)
3166 && next_iv->position >= XFASTINT (limit))
3167 /* No text property change up to limit. */
3168 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3169 else
3170 /* Text properties change in next_iv. */
3171 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3172 }
3173 }
3174
3175 xassert (STRINGP (it->string)
3176 || (it->stop_charpos >= BEGV
3177 && it->stop_charpos >= IT_CHARPOS (*it)));
3178 }
3179
3180
3181 /* Return the position of the next overlay change after POS in
3182 current_buffer. Value is point-max if no overlay change
3183 follows. This is like `next-overlay-change' but doesn't use
3184 xmalloc. */
3185
3186 static int
3187 next_overlay_change (pos)
3188 int pos;
3189 {
3190 int noverlays;
3191 int endpos;
3192 Lisp_Object *overlays;
3193 int i;
3194
3195 /* Get all overlays at the given position. */
3196 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3197
3198 /* If any of these overlays ends before endpos,
3199 use its ending point instead. */
3200 for (i = 0; i < noverlays; ++i)
3201 {
3202 Lisp_Object oend;
3203 int oendpos;
3204
3205 oend = OVERLAY_END (overlays[i]);
3206 oendpos = OVERLAY_POSITION (oend);
3207 endpos = min (endpos, oendpos);
3208 }
3209
3210 return endpos;
3211 }
3212
3213
3214 \f
3215 /***********************************************************************
3216 Fontification
3217 ***********************************************************************/
3218
3219 /* Handle changes in the `fontified' property of the current buffer by
3220 calling hook functions from Qfontification_functions to fontify
3221 regions of text. */
3222
3223 static enum prop_handled
3224 handle_fontified_prop (it)
3225 struct it *it;
3226 {
3227 Lisp_Object prop, pos;
3228 enum prop_handled handled = HANDLED_NORMALLY;
3229
3230 if (!NILP (Vmemory_full))
3231 return handled;
3232
3233 /* Get the value of the `fontified' property at IT's current buffer
3234 position. (The `fontified' property doesn't have a special
3235 meaning in strings.) If the value is nil, call functions from
3236 Qfontification_functions. */
3237 if (!STRINGP (it->string)
3238 && it->s == NULL
3239 && !NILP (Vfontification_functions)
3240 && !NILP (Vrun_hooks)
3241 && (pos = make_number (IT_CHARPOS (*it)),
3242 prop = Fget_char_property (pos, Qfontified, Qnil),
3243 /* Ignore the special cased nil value always present at EOB since
3244 no amount of fontifying will be able to change it. */
3245 NILP (prop) && IT_CHARPOS (*it) < Z))
3246 {
3247 int count = SPECPDL_INDEX ();
3248 Lisp_Object val;
3249
3250 val = Vfontification_functions;
3251 specbind (Qfontification_functions, Qnil);
3252
3253 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3254 safe_call1 (val, pos);
3255 else
3256 {
3257 Lisp_Object globals, fn;
3258 struct gcpro gcpro1, gcpro2;
3259
3260 globals = Qnil;
3261 GCPRO2 (val, globals);
3262
3263 for (; CONSP (val); val = XCDR (val))
3264 {
3265 fn = XCAR (val);
3266
3267 if (EQ (fn, Qt))
3268 {
3269 /* A value of t indicates this hook has a local
3270 binding; it means to run the global binding too.
3271 In a global value, t should not occur. If it
3272 does, we must ignore it to avoid an endless
3273 loop. */
3274 for (globals = Fdefault_value (Qfontification_functions);
3275 CONSP (globals);
3276 globals = XCDR (globals))
3277 {
3278 fn = XCAR (globals);
3279 if (!EQ (fn, Qt))
3280 safe_call1 (fn, pos);
3281 }
3282 }
3283 else
3284 safe_call1 (fn, pos);
3285 }
3286
3287 UNGCPRO;
3288 }
3289
3290 unbind_to (count, Qnil);
3291
3292 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3293 something. This avoids an endless loop if they failed to
3294 fontify the text for which reason ever. */
3295 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3296 handled = HANDLED_RECOMPUTE_PROPS;
3297 }
3298
3299 return handled;
3300 }
3301
3302
3303 \f
3304 /***********************************************************************
3305 Faces
3306 ***********************************************************************/
3307
3308 /* Set up iterator IT from face properties at its current position.
3309 Called from handle_stop. */
3310
3311 static enum prop_handled
3312 handle_face_prop (it)
3313 struct it *it;
3314 {
3315 int new_face_id, next_stop;
3316
3317 if (!STRINGP (it->string))
3318 {
3319 new_face_id
3320 = face_at_buffer_position (it->w,
3321 IT_CHARPOS (*it),
3322 it->region_beg_charpos,
3323 it->region_end_charpos,
3324 &next_stop,
3325 (IT_CHARPOS (*it)
3326 + TEXT_PROP_DISTANCE_LIMIT),
3327 0);
3328
3329 /* Is this a start of a run of characters with box face?
3330 Caveat: this can be called for a freshly initialized
3331 iterator; face_id is -1 in this case. We know that the new
3332 face will not change until limit, i.e. if the new face has a
3333 box, all characters up to limit will have one. But, as
3334 usual, we don't know whether limit is really the end. */
3335 if (new_face_id != it->face_id)
3336 {
3337 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3338
3339 /* If new face has a box but old face has not, this is
3340 the start of a run of characters with box, i.e. it has
3341 a shadow on the left side. The value of face_id of the
3342 iterator will be -1 if this is the initial call that gets
3343 the face. In this case, we have to look in front of IT's
3344 position and see whether there is a face != new_face_id. */
3345 it->start_of_box_run_p
3346 = (new_face->box != FACE_NO_BOX
3347 && (it->face_id >= 0
3348 || IT_CHARPOS (*it) == BEG
3349 || new_face_id != face_before_it_pos (it)));
3350 it->face_box_p = new_face->box != FACE_NO_BOX;
3351 }
3352 }
3353 else
3354 {
3355 int base_face_id, bufpos;
3356 int i;
3357 Lisp_Object from_overlay
3358 = (it->current.overlay_string_index >= 0
3359 ? it->string_overlays[it->current.overlay_string_index]
3360 : Qnil);
3361
3362 /* See if we got to this string directly or indirectly from
3363 an overlay property. That includes the before-string or
3364 after-string of an overlay, strings in display properties
3365 provided by an overlay, their text properties, etc.
3366
3367 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3368 if (! NILP (from_overlay))
3369 for (i = it->sp - 1; i >= 0; i--)
3370 {
3371 if (it->stack[i].current.overlay_string_index >= 0)
3372 from_overlay
3373 = it->string_overlays[it->stack[i].current.overlay_string_index];
3374 else if (! NILP (it->stack[i].from_overlay))
3375 from_overlay = it->stack[i].from_overlay;
3376
3377 if (!NILP (from_overlay))
3378 break;
3379 }
3380
3381 if (! NILP (from_overlay))
3382 {
3383 bufpos = IT_CHARPOS (*it);
3384 /* For a string from an overlay, the base face depends
3385 only on text properties and ignores overlays. */
3386 base_face_id
3387 = face_for_overlay_string (it->w,
3388 IT_CHARPOS (*it),
3389 it->region_beg_charpos,
3390 it->region_end_charpos,
3391 &next_stop,
3392 (IT_CHARPOS (*it)
3393 + TEXT_PROP_DISTANCE_LIMIT),
3394 0,
3395 from_overlay);
3396 }
3397 else
3398 {
3399 bufpos = 0;
3400
3401 /* For strings from a `display' property, use the face at
3402 IT's current buffer position as the base face to merge
3403 with, so that overlay strings appear in the same face as
3404 surrounding text, unless they specify their own
3405 faces. */
3406 base_face_id = underlying_face_id (it);
3407 }
3408
3409 new_face_id = face_at_string_position (it->w,
3410 it->string,
3411 IT_STRING_CHARPOS (*it),
3412 bufpos,
3413 it->region_beg_charpos,
3414 it->region_end_charpos,
3415 &next_stop,
3416 base_face_id, 0);
3417
3418 #if 0 /* This shouldn't be neccessary. Let's check it. */
3419 /* If IT is used to display a mode line we would really like to
3420 use the mode line face instead of the frame's default face. */
3421 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3422 && new_face_id == DEFAULT_FACE_ID)
3423 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3424 #endif
3425
3426 /* Is this a start of a run of characters with box? Caveat:
3427 this can be called for a freshly allocated iterator; face_id
3428 is -1 is this case. We know that the new face will not
3429 change until the next check pos, i.e. if the new face has a
3430 box, all characters up to that position will have a
3431 box. But, as usual, we don't know whether that position
3432 is really the end. */
3433 if (new_face_id != it->face_id)
3434 {
3435 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3436 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3437
3438 /* If new face has a box but old face hasn't, this is the
3439 start of a run of characters with box, i.e. it has a
3440 shadow on the left side. */
3441 it->start_of_box_run_p
3442 = new_face->box && (old_face == NULL || !old_face->box);
3443 it->face_box_p = new_face->box != FACE_NO_BOX;
3444 }
3445 }
3446
3447 it->face_id = new_face_id;
3448 return HANDLED_NORMALLY;
3449 }
3450
3451
3452 /* Return the ID of the face ``underlying'' IT's current position,
3453 which is in a string. If the iterator is associated with a
3454 buffer, return the face at IT's current buffer position.
3455 Otherwise, use the iterator's base_face_id. */
3456
3457 static int
3458 underlying_face_id (it)
3459 struct it *it;
3460 {
3461 int face_id = it->base_face_id, i;
3462
3463 xassert (STRINGP (it->string));
3464
3465 for (i = it->sp - 1; i >= 0; --i)
3466 if (NILP (it->stack[i].string))
3467 face_id = it->stack[i].face_id;
3468
3469 return face_id;
3470 }
3471
3472
3473 /* Compute the face one character before or after the current position
3474 of IT. BEFORE_P non-zero means get the face in front of IT's
3475 position. Value is the id of the face. */
3476
3477 static int
3478 face_before_or_after_it_pos (it, before_p)
3479 struct it *it;
3480 int before_p;
3481 {
3482 int face_id, limit;
3483 int next_check_charpos;
3484 struct text_pos pos;
3485
3486 xassert (it->s == NULL);
3487
3488 if (STRINGP (it->string))
3489 {
3490 int bufpos, base_face_id;
3491
3492 /* No face change past the end of the string (for the case
3493 we are padding with spaces). No face change before the
3494 string start. */
3495 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3496 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3497 return it->face_id;
3498
3499 /* Set pos to the position before or after IT's current position. */
3500 if (before_p)
3501 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3502 else
3503 /* For composition, we must check the character after the
3504 composition. */
3505 pos = (it->what == IT_COMPOSITION
3506 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3507 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3508
3509 if (it->current.overlay_string_index >= 0)
3510 bufpos = IT_CHARPOS (*it);
3511 else
3512 bufpos = 0;
3513
3514 base_face_id = underlying_face_id (it);
3515
3516 /* Get the face for ASCII, or unibyte. */
3517 face_id = face_at_string_position (it->w,
3518 it->string,
3519 CHARPOS (pos),
3520 bufpos,
3521 it->region_beg_charpos,
3522 it->region_end_charpos,
3523 &next_check_charpos,
3524 base_face_id, 0);
3525
3526 /* Correct the face for charsets different from ASCII. Do it
3527 for the multibyte case only. The face returned above is
3528 suitable for unibyte text if IT->string is unibyte. */
3529 if (STRING_MULTIBYTE (it->string))
3530 {
3531 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3532 int rest = SBYTES (it->string) - BYTEPOS (pos);
3533 int c, len;
3534 struct face *face = FACE_FROM_ID (it->f, face_id);
3535
3536 c = string_char_and_length (p, rest, &len);
3537 face_id = FACE_FOR_CHAR (it->f, face, c);
3538 }
3539 }
3540 else
3541 {
3542 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3543 || (IT_CHARPOS (*it) <= BEGV && before_p))
3544 return it->face_id;
3545
3546 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3547 pos = it->current.pos;
3548
3549 if (before_p)
3550 DEC_TEXT_POS (pos, it->multibyte_p);
3551 else
3552 {
3553 if (it->what == IT_COMPOSITION)
3554 /* For composition, we must check the position after the
3555 composition. */
3556 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3557 else
3558 INC_TEXT_POS (pos, it->multibyte_p);
3559 }
3560
3561 /* Determine face for CHARSET_ASCII, or unibyte. */
3562 face_id = face_at_buffer_position (it->w,
3563 CHARPOS (pos),
3564 it->region_beg_charpos,
3565 it->region_end_charpos,
3566 &next_check_charpos,
3567 limit, 0);
3568
3569 /* Correct the face for charsets different from ASCII. Do it
3570 for the multibyte case only. The face returned above is
3571 suitable for unibyte text if current_buffer is unibyte. */
3572 if (it->multibyte_p)
3573 {
3574 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3575 struct face *face = FACE_FROM_ID (it->f, face_id);
3576 face_id = FACE_FOR_CHAR (it->f, face, c);
3577 }
3578 }
3579
3580 return face_id;
3581 }
3582
3583
3584 \f
3585 /***********************************************************************
3586 Invisible text
3587 ***********************************************************************/
3588
3589 /* Set up iterator IT from invisible properties at its current
3590 position. Called from handle_stop. */
3591
3592 static enum prop_handled
3593 handle_invisible_prop (it)
3594 struct it *it;
3595 {
3596 enum prop_handled handled = HANDLED_NORMALLY;
3597
3598 if (STRINGP (it->string))
3599 {
3600 extern Lisp_Object Qinvisible;
3601 Lisp_Object prop, end_charpos, limit, charpos;
3602
3603 /* Get the value of the invisible text property at the
3604 current position. Value will be nil if there is no such
3605 property. */
3606 charpos = make_number (IT_STRING_CHARPOS (*it));
3607 prop = Fget_text_property (charpos, Qinvisible, it->string);
3608
3609 if (!NILP (prop)
3610 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3611 {
3612 handled = HANDLED_RECOMPUTE_PROPS;
3613
3614 /* Get the position at which the next change of the
3615 invisible text property can be found in IT->string.
3616 Value will be nil if the property value is the same for
3617 all the rest of IT->string. */
3618 XSETINT (limit, SCHARS (it->string));
3619 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3620 it->string, limit);
3621
3622 /* Text at current position is invisible. The next
3623 change in the property is at position end_charpos.
3624 Move IT's current position to that position. */
3625 if (INTEGERP (end_charpos)
3626 && XFASTINT (end_charpos) < XFASTINT (limit))
3627 {
3628 struct text_pos old;
3629 old = it->current.string_pos;
3630 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3631 compute_string_pos (&it->current.string_pos, old, it->string);
3632 }
3633 else
3634 {
3635 /* The rest of the string is invisible. If this is an
3636 overlay string, proceed with the next overlay string
3637 or whatever comes and return a character from there. */
3638 if (it->current.overlay_string_index >= 0)
3639 {
3640 next_overlay_string (it);
3641 /* Don't check for overlay strings when we just
3642 finished processing them. */
3643 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3644 }
3645 else
3646 {
3647 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3648 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3649 }
3650 }
3651 }
3652 }
3653 else
3654 {
3655 int invis_p;
3656 EMACS_INT newpos, next_stop, start_charpos;
3657 Lisp_Object pos, prop, overlay;
3658
3659 /* First of all, is there invisible text at this position? */
3660 start_charpos = IT_CHARPOS (*it);
3661 pos = make_number (IT_CHARPOS (*it));
3662 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3663 &overlay);
3664 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3665
3666 /* If we are on invisible text, skip over it. */
3667 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3668 {
3669 /* Record whether we have to display an ellipsis for the
3670 invisible text. */
3671 int display_ellipsis_p = invis_p == 2;
3672
3673 handled = HANDLED_RECOMPUTE_PROPS;
3674
3675 /* Loop skipping over invisible text. The loop is left at
3676 ZV or with IT on the first char being visible again. */
3677 do
3678 {
3679 /* Try to skip some invisible text. Return value is the
3680 position reached which can be equal to IT's position
3681 if there is nothing invisible here. This skips both
3682 over invisible text properties and overlays with
3683 invisible property. */
3684 newpos = skip_invisible (IT_CHARPOS (*it),
3685 &next_stop, ZV, it->window);
3686
3687 /* If we skipped nothing at all we weren't at invisible
3688 text in the first place. If everything to the end of
3689 the buffer was skipped, end the loop. */
3690 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3691 invis_p = 0;
3692 else
3693 {
3694 /* We skipped some characters but not necessarily
3695 all there are. Check if we ended up on visible
3696 text. Fget_char_property returns the property of
3697 the char before the given position, i.e. if we
3698 get invis_p = 0, this means that the char at
3699 newpos is visible. */
3700 pos = make_number (newpos);
3701 prop = Fget_char_property (pos, Qinvisible, it->window);
3702 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3703 }
3704
3705 /* If we ended up on invisible text, proceed to
3706 skip starting with next_stop. */
3707 if (invis_p)
3708 IT_CHARPOS (*it) = next_stop;
3709
3710 /* If there are adjacent invisible texts, don't lose the
3711 second one's ellipsis. */
3712 if (invis_p == 2)
3713 display_ellipsis_p = 1;
3714 }
3715 while (invis_p);
3716
3717 /* The position newpos is now either ZV or on visible text. */
3718 IT_CHARPOS (*it) = newpos;
3719 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3720
3721 /* If there are before-strings at the start of invisible
3722 text, and the text is invisible because of a text
3723 property, arrange to show before-strings because 20.x did
3724 it that way. (If the text is invisible because of an
3725 overlay property instead of a text property, this is
3726 already handled in the overlay code.) */
3727 if (NILP (overlay)
3728 && get_overlay_strings (it, start_charpos))
3729 {
3730 handled = HANDLED_RECOMPUTE_PROPS;
3731 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3732 }
3733 else if (display_ellipsis_p)
3734 {
3735 /* Make sure that the glyphs of the ellipsis will get
3736 correct `charpos' values. If we would not update
3737 it->position here, the glyphs would belong to the
3738 last visible character _before_ the invisible
3739 text, which confuses `set_cursor_from_row'.
3740
3741 We use the last invisible position instead of the
3742 first because this way the cursor is always drawn on
3743 the first "." of the ellipsis, whenever PT is inside
3744 the invisible text. Otherwise the cursor would be
3745 placed _after_ the ellipsis when the point is after the
3746 first invisible character. */
3747 if (!STRINGP (it->object))
3748 {
3749 it->position.charpos = IT_CHARPOS (*it) - 1;
3750 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3751 }
3752 setup_for_ellipsis (it, 0);
3753 /* Let the ellipsis display before
3754 considering any properties of the following char.
3755 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3756 handled = HANDLED_RETURN;
3757 }
3758 }
3759 }
3760
3761 return handled;
3762 }
3763
3764
3765 /* Make iterator IT return `...' next.
3766 Replaces LEN characters from buffer. */
3767
3768 static void
3769 setup_for_ellipsis (it, len)
3770 struct it *it;
3771 int len;
3772 {
3773 /* Use the display table definition for `...'. Invalid glyphs
3774 will be handled by the method returning elements from dpvec. */
3775 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3776 {
3777 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3778 it->dpvec = v->contents;
3779 it->dpend = v->contents + v->size;
3780 }
3781 else
3782 {
3783 /* Default `...'. */
3784 it->dpvec = default_invis_vector;
3785 it->dpend = default_invis_vector + 3;
3786 }
3787
3788 it->dpvec_char_len = len;
3789 it->current.dpvec_index = 0;
3790 it->dpvec_face_id = -1;
3791
3792 /* Remember the current face id in case glyphs specify faces.
3793 IT's face is restored in set_iterator_to_next.
3794 saved_face_id was set to preceding char's face in handle_stop. */
3795 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3796 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3797
3798 it->method = GET_FROM_DISPLAY_VECTOR;
3799 it->ellipsis_p = 1;
3800 }
3801
3802
3803 \f
3804 /***********************************************************************
3805 'display' property
3806 ***********************************************************************/
3807
3808 /* Set up iterator IT from `display' property at its current position.
3809 Called from handle_stop.
3810 We return HANDLED_RETURN if some part of the display property
3811 overrides the display of the buffer text itself.
3812 Otherwise we return HANDLED_NORMALLY. */
3813
3814 static enum prop_handled
3815 handle_display_prop (it)
3816 struct it *it;
3817 {
3818 Lisp_Object prop, object, overlay;
3819 struct text_pos *position;
3820 /* Nonzero if some property replaces the display of the text itself. */
3821 int display_replaced_p = 0;
3822
3823 if (STRINGP (it->string))
3824 {
3825 object = it->string;
3826 position = &it->current.string_pos;
3827 }
3828 else
3829 {
3830 XSETWINDOW (object, it->w);
3831 position = &it->current.pos;
3832 }
3833
3834 /* Reset those iterator values set from display property values. */
3835 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3836 it->space_width = Qnil;
3837 it->font_height = Qnil;
3838 it->voffset = 0;
3839
3840 /* We don't support recursive `display' properties, i.e. string
3841 values that have a string `display' property, that have a string
3842 `display' property etc. */
3843 if (!it->string_from_display_prop_p)
3844 it->area = TEXT_AREA;
3845
3846 prop = get_char_property_and_overlay (make_number (position->charpos),
3847 Qdisplay, object, &overlay);
3848 if (NILP (prop))
3849 return HANDLED_NORMALLY;
3850 /* Now OVERLAY is the overlay that gave us this property, or nil
3851 if it was a text property. */
3852
3853 if (!STRINGP (it->string))
3854 object = it->w->buffer;
3855
3856 if (CONSP (prop)
3857 /* Simple properties. */
3858 && !EQ (XCAR (prop), Qimage)
3859 && !EQ (XCAR (prop), Qspace)
3860 && !EQ (XCAR (prop), Qwhen)
3861 && !EQ (XCAR (prop), Qslice)
3862 && !EQ (XCAR (prop), Qspace_width)
3863 && !EQ (XCAR (prop), Qheight)
3864 && !EQ (XCAR (prop), Qraise)
3865 /* Marginal area specifications. */
3866 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3867 && !EQ (XCAR (prop), Qleft_fringe)
3868 && !EQ (XCAR (prop), Qright_fringe)
3869 && !NILP (XCAR (prop)))
3870 {
3871 for (; CONSP (prop); prop = XCDR (prop))
3872 {
3873 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3874 position, display_replaced_p))
3875 {
3876 display_replaced_p = 1;
3877 /* If some text in a string is replaced, `position' no
3878 longer points to the position of `object'. */
3879 if (STRINGP (object))
3880 break;
3881 }
3882 }
3883 }
3884 else if (VECTORP (prop))
3885 {
3886 int i;
3887 for (i = 0; i < ASIZE (prop); ++i)
3888 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3889 position, display_replaced_p))
3890 {
3891 display_replaced_p = 1;
3892 /* If some text in a string is replaced, `position' no
3893 longer points to the position of `object'. */
3894 if (STRINGP (object))
3895 break;
3896 }
3897 }
3898 else
3899 {
3900 int ret = handle_single_display_spec (it, prop, object, overlay,
3901 position, 0);
3902 if (ret < 0) /* Replaced by "", i.e. nothing. */
3903 return HANDLED_RECOMPUTE_PROPS;
3904 if (ret)
3905 display_replaced_p = 1;
3906 }
3907
3908 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3909 }
3910
3911
3912 /* Value is the position of the end of the `display' property starting
3913 at START_POS in OBJECT. */
3914
3915 static struct text_pos
3916 display_prop_end (it, object, start_pos)
3917 struct it *it;
3918 Lisp_Object object;
3919 struct text_pos start_pos;
3920 {
3921 Lisp_Object end;
3922 struct text_pos end_pos;
3923
3924 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3925 Qdisplay, object, Qnil);
3926 CHARPOS (end_pos) = XFASTINT (end);
3927 if (STRINGP (object))
3928 compute_string_pos (&end_pos, start_pos, it->string);
3929 else
3930 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3931
3932 return end_pos;
3933 }
3934
3935
3936 /* Set up IT from a single `display' specification PROP. OBJECT
3937 is the object in which the `display' property was found. *POSITION
3938 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3939 means that we previously saw a display specification which already
3940 replaced text display with something else, for example an image;
3941 we ignore such properties after the first one has been processed.
3942
3943 OVERLAY is the overlay this `display' property came from,
3944 or nil if it was a text property.
3945
3946 If PROP is a `space' or `image' specification, and in some other
3947 cases too, set *POSITION to the position where the `display'
3948 property ends.
3949
3950 Value is non-zero if something was found which replaces the display
3951 of buffer or string text. Specifically, the value is -1 if that
3952 "something" is "nothing". */
3953
3954 static int
3955 handle_single_display_spec (it, spec, object, overlay, position,
3956 display_replaced_before_p)
3957 struct it *it;
3958 Lisp_Object spec;
3959 Lisp_Object object;
3960 Lisp_Object overlay;
3961 struct text_pos *position;
3962 int display_replaced_before_p;
3963 {
3964 Lisp_Object form;
3965 Lisp_Object location, value;
3966 struct text_pos start_pos, save_pos;
3967 int valid_p;
3968
3969 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3970 If the result is non-nil, use VALUE instead of SPEC. */
3971 form = Qt;
3972 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3973 {
3974 spec = XCDR (spec);
3975 if (!CONSP (spec))
3976 return 0;
3977 form = XCAR (spec);
3978 spec = XCDR (spec);
3979 }
3980
3981 if (!NILP (form) && !EQ (form, Qt))
3982 {
3983 int count = SPECPDL_INDEX ();
3984 struct gcpro gcpro1;
3985
3986 /* Bind `object' to the object having the `display' property, a
3987 buffer or string. Bind `position' to the position in the
3988 object where the property was found, and `buffer-position'
3989 to the current position in the buffer. */
3990 specbind (Qobject, object);
3991 specbind (Qposition, make_number (CHARPOS (*position)));
3992 specbind (Qbuffer_position,
3993 make_number (STRINGP (object)
3994 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3995 GCPRO1 (form);
3996 form = safe_eval (form);
3997 UNGCPRO;
3998 unbind_to (count, Qnil);
3999 }
4000
4001 if (NILP (form))
4002 return 0;
4003
4004 /* Handle `(height HEIGHT)' specifications. */
4005 if (CONSP (spec)
4006 && EQ (XCAR (spec), Qheight)
4007 && CONSP (XCDR (spec)))
4008 {
4009 if (!FRAME_WINDOW_P (it->f))
4010 return 0;
4011
4012 it->font_height = XCAR (XCDR (spec));
4013 if (!NILP (it->font_height))
4014 {
4015 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4016 int new_height = -1;
4017
4018 if (CONSP (it->font_height)
4019 && (EQ (XCAR (it->font_height), Qplus)
4020 || EQ (XCAR (it->font_height), Qminus))
4021 && CONSP (XCDR (it->font_height))
4022 && INTEGERP (XCAR (XCDR (it->font_height))))
4023 {
4024 /* `(+ N)' or `(- N)' where N is an integer. */
4025 int steps = XINT (XCAR (XCDR (it->font_height)));
4026 if (EQ (XCAR (it->font_height), Qplus))
4027 steps = - steps;
4028 it->face_id = smaller_face (it->f, it->face_id, steps);
4029 }
4030 else if (FUNCTIONP (it->font_height))
4031 {
4032 /* Call function with current height as argument.
4033 Value is the new height. */
4034 Lisp_Object height;
4035 height = safe_call1 (it->font_height,
4036 face->lface[LFACE_HEIGHT_INDEX]);
4037 if (NUMBERP (height))
4038 new_height = XFLOATINT (height);
4039 }
4040 else if (NUMBERP (it->font_height))
4041 {
4042 /* Value is a multiple of the canonical char height. */
4043 struct face *face;
4044
4045 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4046 new_height = (XFLOATINT (it->font_height)
4047 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4048 }
4049 else
4050 {
4051 /* Evaluate IT->font_height with `height' bound to the
4052 current specified height to get the new height. */
4053 int count = SPECPDL_INDEX ();
4054
4055 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4056 value = safe_eval (it->font_height);
4057 unbind_to (count, Qnil);
4058
4059 if (NUMBERP (value))
4060 new_height = XFLOATINT (value);
4061 }
4062
4063 if (new_height > 0)
4064 it->face_id = face_with_height (it->f, it->face_id, new_height);
4065 }
4066
4067 return 0;
4068 }
4069
4070 /* Handle `(space-width WIDTH)'. */
4071 if (CONSP (spec)
4072 && EQ (XCAR (spec), Qspace_width)
4073 && CONSP (XCDR (spec)))
4074 {
4075 if (!FRAME_WINDOW_P (it->f))
4076 return 0;
4077
4078 value = XCAR (XCDR (spec));
4079 if (NUMBERP (value) && XFLOATINT (value) > 0)
4080 it->space_width = value;
4081
4082 return 0;
4083 }
4084
4085 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4086 if (CONSP (spec)
4087 && EQ (XCAR (spec), Qslice))
4088 {
4089 Lisp_Object tem;
4090
4091 if (!FRAME_WINDOW_P (it->f))
4092 return 0;
4093
4094 if (tem = XCDR (spec), CONSP (tem))
4095 {
4096 it->slice.x = XCAR (tem);
4097 if (tem = XCDR (tem), CONSP (tem))
4098 {
4099 it->slice.y = XCAR (tem);
4100 if (tem = XCDR (tem), CONSP (tem))
4101 {
4102 it->slice.width = XCAR (tem);
4103 if (tem = XCDR (tem), CONSP (tem))
4104 it->slice.height = XCAR (tem);
4105 }
4106 }
4107 }
4108
4109 return 0;
4110 }
4111
4112 /* Handle `(raise FACTOR)'. */
4113 if (CONSP (spec)
4114 && EQ (XCAR (spec), Qraise)
4115 && CONSP (XCDR (spec)))
4116 {
4117 if (!FRAME_WINDOW_P (it->f))
4118 return 0;
4119
4120 #ifdef HAVE_WINDOW_SYSTEM
4121 value = XCAR (XCDR (spec));
4122 if (NUMBERP (value))
4123 {
4124 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4125 it->voffset = - (XFLOATINT (value)
4126 * (FONT_HEIGHT (face->font)));
4127 }
4128 #endif /* HAVE_WINDOW_SYSTEM */
4129
4130 return 0;
4131 }
4132
4133 /* Don't handle the other kinds of display specifications
4134 inside a string that we got from a `display' property. */
4135 if (it->string_from_display_prop_p)
4136 return 0;
4137
4138 /* Characters having this form of property are not displayed, so
4139 we have to find the end of the property. */
4140 start_pos = *position;
4141 *position = display_prop_end (it, object, start_pos);
4142 value = Qnil;
4143
4144 /* Stop the scan at that end position--we assume that all
4145 text properties change there. */
4146 it->stop_charpos = position->charpos;
4147
4148 /* Handle `(left-fringe BITMAP [FACE])'
4149 and `(right-fringe BITMAP [FACE])'. */
4150 if (CONSP (spec)
4151 && (EQ (XCAR (spec), Qleft_fringe)
4152 || EQ (XCAR (spec), Qright_fringe))
4153 && CONSP (XCDR (spec)))
4154 {
4155 int face_id = DEFAULT_FACE_ID;
4156 int fringe_bitmap;
4157
4158 if (!FRAME_WINDOW_P (it->f))
4159 /* If we return here, POSITION has been advanced
4160 across the text with this property. */
4161 return 0;
4162
4163 #ifdef HAVE_WINDOW_SYSTEM
4164 value = XCAR (XCDR (spec));
4165 if (!SYMBOLP (value)
4166 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4167 /* If we return here, POSITION has been advanced
4168 across the text with this property. */
4169 return 0;
4170
4171 if (CONSP (XCDR (XCDR (spec))))
4172 {
4173 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4174 int face_id2 = lookup_derived_face (it->f, face_name,
4175 'A', FRINGE_FACE_ID, 0);
4176 if (face_id2 >= 0)
4177 face_id = face_id2;
4178 }
4179
4180 /* Save current settings of IT so that we can restore them
4181 when we are finished with the glyph property value. */
4182
4183 save_pos = it->position;
4184 it->position = *position;
4185 push_it (it);
4186 it->position = save_pos;
4187
4188 it->area = TEXT_AREA;
4189 it->what = IT_IMAGE;
4190 it->image_id = -1; /* no image */
4191 it->position = start_pos;
4192 it->object = NILP (object) ? it->w->buffer : object;
4193 it->method = GET_FROM_IMAGE;
4194 it->from_overlay = Qnil;
4195 it->face_id = face_id;
4196
4197 /* Say that we haven't consumed the characters with
4198 `display' property yet. The call to pop_it in
4199 set_iterator_to_next will clean this up. */
4200 *position = start_pos;
4201
4202 if (EQ (XCAR (spec), Qleft_fringe))
4203 {
4204 it->left_user_fringe_bitmap = fringe_bitmap;
4205 it->left_user_fringe_face_id = face_id;
4206 }
4207 else
4208 {
4209 it->right_user_fringe_bitmap = fringe_bitmap;
4210 it->right_user_fringe_face_id = face_id;
4211 }
4212 #endif /* HAVE_WINDOW_SYSTEM */
4213 return 1;
4214 }
4215
4216 /* Prepare to handle `((margin left-margin) ...)',
4217 `((margin right-margin) ...)' and `((margin nil) ...)'
4218 prefixes for display specifications. */
4219 location = Qunbound;
4220 if (CONSP (spec) && CONSP (XCAR (spec)))
4221 {
4222 Lisp_Object tem;
4223
4224 value = XCDR (spec);
4225 if (CONSP (value))
4226 value = XCAR (value);
4227
4228 tem = XCAR (spec);
4229 if (EQ (XCAR (tem), Qmargin)
4230 && (tem = XCDR (tem),
4231 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4232 (NILP (tem)
4233 || EQ (tem, Qleft_margin)
4234 || EQ (tem, Qright_margin))))
4235 location = tem;
4236 }
4237
4238 if (EQ (location, Qunbound))
4239 {
4240 location = Qnil;
4241 value = spec;
4242 }
4243
4244 /* After this point, VALUE is the property after any
4245 margin prefix has been stripped. It must be a string,
4246 an image specification, or `(space ...)'.
4247
4248 LOCATION specifies where to display: `left-margin',
4249 `right-margin' or nil. */
4250
4251 valid_p = (STRINGP (value)
4252 #ifdef HAVE_WINDOW_SYSTEM
4253 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4254 #endif /* not HAVE_WINDOW_SYSTEM */
4255 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4256
4257 if (valid_p && !display_replaced_before_p)
4258 {
4259 /* Save current settings of IT so that we can restore them
4260 when we are finished with the glyph property value. */
4261 save_pos = it->position;
4262 it->position = *position;
4263 push_it (it);
4264 it->position = save_pos;
4265 it->from_overlay = overlay;
4266
4267 if (NILP (location))
4268 it->area = TEXT_AREA;
4269 else if (EQ (location, Qleft_margin))
4270 it->area = LEFT_MARGIN_AREA;
4271 else
4272 it->area = RIGHT_MARGIN_AREA;
4273
4274 if (STRINGP (value))
4275 {
4276 if (SCHARS (value) == 0)
4277 {
4278 pop_it (it);
4279 return -1; /* Replaced by "", i.e. nothing. */
4280 }
4281 it->string = value;
4282 it->multibyte_p = STRING_MULTIBYTE (it->string);
4283 it->current.overlay_string_index = -1;
4284 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4285 it->end_charpos = it->string_nchars = SCHARS (it->string);
4286 it->method = GET_FROM_STRING;
4287 it->stop_charpos = 0;
4288 it->string_from_display_prop_p = 1;
4289 /* Say that we haven't consumed the characters with
4290 `display' property yet. The call to pop_it in
4291 set_iterator_to_next will clean this up. */
4292 if (BUFFERP (object))
4293 it->current.pos = start_pos;
4294 }
4295 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4296 {
4297 it->method = GET_FROM_STRETCH;
4298 it->object = value;
4299 it->position = start_pos;
4300 if (BUFFERP (object))
4301 it->current.pos = start_pos;
4302 }
4303 #ifdef HAVE_WINDOW_SYSTEM
4304 else
4305 {
4306 it->what = IT_IMAGE;
4307 it->image_id = lookup_image (it->f, value);
4308 it->position = start_pos;
4309 it->object = NILP (object) ? it->w->buffer : object;
4310 it->method = GET_FROM_IMAGE;
4311
4312 /* Say that we haven't consumed the characters with
4313 `display' property yet. The call to pop_it in
4314 set_iterator_to_next will clean this up. */
4315 if (BUFFERP (object))
4316 it->current.pos = start_pos;
4317 }
4318 #endif /* HAVE_WINDOW_SYSTEM */
4319
4320 return 1;
4321 }
4322
4323 /* Invalid property or property not supported. Restore
4324 POSITION to what it was before. */
4325 *position = start_pos;
4326 return 0;
4327 }
4328
4329
4330 /* Check if SPEC is a display sub-property value whose text should be
4331 treated as intangible. */
4332
4333 static int
4334 single_display_spec_intangible_p (prop)
4335 Lisp_Object prop;
4336 {
4337 /* Skip over `when FORM'. */
4338 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4339 {
4340 prop = XCDR (prop);
4341 if (!CONSP (prop))
4342 return 0;
4343 prop = XCDR (prop);
4344 }
4345
4346 if (STRINGP (prop))
4347 return 1;
4348
4349 if (!CONSP (prop))
4350 return 0;
4351
4352 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4353 we don't need to treat text as intangible. */
4354 if (EQ (XCAR (prop), Qmargin))
4355 {
4356 prop = XCDR (prop);
4357 if (!CONSP (prop))
4358 return 0;
4359
4360 prop = XCDR (prop);
4361 if (!CONSP (prop)
4362 || EQ (XCAR (prop), Qleft_margin)
4363 || EQ (XCAR (prop), Qright_margin))
4364 return 0;
4365 }
4366
4367 return (CONSP (prop)
4368 && (EQ (XCAR (prop), Qimage)
4369 || EQ (XCAR (prop), Qspace)));
4370 }
4371
4372
4373 /* Check if PROP is a display property value whose text should be
4374 treated as intangible. */
4375
4376 int
4377 display_prop_intangible_p (prop)
4378 Lisp_Object prop;
4379 {
4380 if (CONSP (prop)
4381 && CONSP (XCAR (prop))
4382 && !EQ (Qmargin, XCAR (XCAR (prop))))
4383 {
4384 /* A list of sub-properties. */
4385 while (CONSP (prop))
4386 {
4387 if (single_display_spec_intangible_p (XCAR (prop)))
4388 return 1;
4389 prop = XCDR (prop);
4390 }
4391 }
4392 else if (VECTORP (prop))
4393 {
4394 /* A vector of sub-properties. */
4395 int i;
4396 for (i = 0; i < ASIZE (prop); ++i)
4397 if (single_display_spec_intangible_p (AREF (prop, i)))
4398 return 1;
4399 }
4400 else
4401 return single_display_spec_intangible_p (prop);
4402
4403 return 0;
4404 }
4405
4406
4407 /* Return 1 if PROP is a display sub-property value containing STRING. */
4408
4409 static int
4410 single_display_spec_string_p (prop, string)
4411 Lisp_Object prop, string;
4412 {
4413 if (EQ (string, prop))
4414 return 1;
4415
4416 /* Skip over `when FORM'. */
4417 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4418 {
4419 prop = XCDR (prop);
4420 if (!CONSP (prop))
4421 return 0;
4422 prop = XCDR (prop);
4423 }
4424
4425 if (CONSP (prop))
4426 /* Skip over `margin LOCATION'. */
4427 if (EQ (XCAR (prop), Qmargin))
4428 {
4429 prop = XCDR (prop);
4430 if (!CONSP (prop))
4431 return 0;
4432
4433 prop = XCDR (prop);
4434 if (!CONSP (prop))
4435 return 0;
4436 }
4437
4438 return CONSP (prop) && EQ (XCAR (prop), string);
4439 }
4440
4441
4442 /* Return 1 if STRING appears in the `display' property PROP. */
4443
4444 static int
4445 display_prop_string_p (prop, string)
4446 Lisp_Object prop, string;
4447 {
4448 if (CONSP (prop)
4449 && CONSP (XCAR (prop))
4450 && !EQ (Qmargin, XCAR (XCAR (prop))))
4451 {
4452 /* A list of sub-properties. */
4453 while (CONSP (prop))
4454 {
4455 if (single_display_spec_string_p (XCAR (prop), string))
4456 return 1;
4457 prop = XCDR (prop);
4458 }
4459 }
4460 else if (VECTORP (prop))
4461 {
4462 /* A vector of sub-properties. */
4463 int i;
4464 for (i = 0; i < ASIZE (prop); ++i)
4465 if (single_display_spec_string_p (AREF (prop, i), string))
4466 return 1;
4467 }
4468 else
4469 return single_display_spec_string_p (prop, string);
4470
4471 return 0;
4472 }
4473
4474
4475 /* Determine from which buffer position in W's buffer STRING comes
4476 from. AROUND_CHARPOS is an approximate position where it could
4477 be from. Value is the buffer position or 0 if it couldn't be
4478 determined.
4479
4480 W's buffer must be current.
4481
4482 This function is necessary because we don't record buffer positions
4483 in glyphs generated from strings (to keep struct glyph small).
4484 This function may only use code that doesn't eval because it is
4485 called asynchronously from note_mouse_highlight. */
4486
4487 int
4488 string_buffer_position (w, string, around_charpos)
4489 struct window *w;
4490 Lisp_Object string;
4491 int around_charpos;
4492 {
4493 Lisp_Object limit, prop, pos;
4494 const int MAX_DISTANCE = 1000;
4495 int found = 0;
4496
4497 pos = make_number (around_charpos);
4498 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4499 while (!found && !EQ (pos, limit))
4500 {
4501 prop = Fget_char_property (pos, Qdisplay, Qnil);
4502 if (!NILP (prop) && display_prop_string_p (prop, string))
4503 found = 1;
4504 else
4505 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4506 }
4507
4508 if (!found)
4509 {
4510 pos = make_number (around_charpos);
4511 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4512 while (!found && !EQ (pos, limit))
4513 {
4514 prop = Fget_char_property (pos, Qdisplay, Qnil);
4515 if (!NILP (prop) && display_prop_string_p (prop, string))
4516 found = 1;
4517 else
4518 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4519 limit);
4520 }
4521 }
4522
4523 return found ? XINT (pos) : 0;
4524 }
4525
4526
4527 \f
4528 /***********************************************************************
4529 `composition' property
4530 ***********************************************************************/
4531
4532 /* Set up iterator IT from `composition' property at its current
4533 position. Called from handle_stop. */
4534
4535 static enum prop_handled
4536 handle_composition_prop (it)
4537 struct it *it;
4538 {
4539 Lisp_Object prop, string;
4540 int pos, pos_byte, end;
4541 enum prop_handled handled = HANDLED_NORMALLY;
4542
4543 if (STRINGP (it->string))
4544 {
4545 pos = IT_STRING_CHARPOS (*it);
4546 pos_byte = IT_STRING_BYTEPOS (*it);
4547 string = it->string;
4548 }
4549 else
4550 {
4551 pos = IT_CHARPOS (*it);
4552 pos_byte = IT_BYTEPOS (*it);
4553 string = Qnil;
4554 }
4555
4556 /* If there's a valid composition and point is not inside of the
4557 composition (in the case that the composition is from the current
4558 buffer), draw a glyph composed from the composition components. */
4559 if (find_composition (pos, -1, &pos, &end, &prop, string)
4560 && COMPOSITION_VALID_P (pos, end, prop)
4561 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4562 {
4563 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4564
4565 if (id >= 0)
4566 {
4567 struct composition *cmp = composition_table[id];
4568
4569 if (cmp->glyph_len == 0)
4570 {
4571 /* No glyph. */
4572 if (STRINGP (it->string))
4573 {
4574 IT_STRING_CHARPOS (*it) = end;
4575 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4576 end);
4577 }
4578 else
4579 {
4580 IT_CHARPOS (*it) = end;
4581 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4582 }
4583 return HANDLED_RECOMPUTE_PROPS;
4584 }
4585
4586 it->stop_charpos = end;
4587 push_it (it);
4588
4589 it->method = GET_FROM_COMPOSITION;
4590 it->cmp_id = id;
4591 it->cmp_len = COMPOSITION_LENGTH (prop);
4592 /* For a terminal, draw only the first character of the
4593 components. */
4594 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4595 it->len = (STRINGP (it->string)
4596 ? string_char_to_byte (it->string, end)
4597 : CHAR_TO_BYTE (end)) - pos_byte;
4598 handled = HANDLED_RETURN;
4599 }
4600 }
4601
4602 return handled;
4603 }
4604
4605
4606 \f
4607 /***********************************************************************
4608 Overlay strings
4609 ***********************************************************************/
4610
4611 /* The following structure is used to record overlay strings for
4612 later sorting in load_overlay_strings. */
4613
4614 struct overlay_entry
4615 {
4616 Lisp_Object overlay;
4617 Lisp_Object string;
4618 int priority;
4619 int after_string_p;
4620 };
4621
4622
4623 /* Set up iterator IT from overlay strings at its current position.
4624 Called from handle_stop. */
4625
4626 static enum prop_handled
4627 handle_overlay_change (it)
4628 struct it *it;
4629 {
4630 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4631 return HANDLED_RECOMPUTE_PROPS;
4632 else
4633 return HANDLED_NORMALLY;
4634 }
4635
4636
4637 /* Set up the next overlay string for delivery by IT, if there is an
4638 overlay string to deliver. Called by set_iterator_to_next when the
4639 end of the current overlay string is reached. If there are more
4640 overlay strings to display, IT->string and
4641 IT->current.overlay_string_index are set appropriately here.
4642 Otherwise IT->string is set to nil. */
4643
4644 static void
4645 next_overlay_string (it)
4646 struct it *it;
4647 {
4648 ++it->current.overlay_string_index;
4649 if (it->current.overlay_string_index == it->n_overlay_strings)
4650 {
4651 /* No more overlay strings. Restore IT's settings to what
4652 they were before overlay strings were processed, and
4653 continue to deliver from current_buffer. */
4654 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4655
4656 pop_it (it);
4657 xassert (it->sp > 0
4658 || it->method == GET_FROM_COMPOSITION
4659 || (NILP (it->string)
4660 && it->method == GET_FROM_BUFFER
4661 && it->stop_charpos >= BEGV
4662 && it->stop_charpos <= it->end_charpos));
4663 it->current.overlay_string_index = -1;
4664 it->n_overlay_strings = 0;
4665
4666 /* If we're at the end of the buffer, record that we have
4667 processed the overlay strings there already, so that
4668 next_element_from_buffer doesn't try it again. */
4669 if (IT_CHARPOS (*it) >= it->end_charpos)
4670 it->overlay_strings_at_end_processed_p = 1;
4671
4672 /* If we have to display `...' for invisible text, set
4673 the iterator up for that. */
4674 if (display_ellipsis_p)
4675 setup_for_ellipsis (it, 0);
4676 }
4677 else
4678 {
4679 /* There are more overlay strings to process. If
4680 IT->current.overlay_string_index has advanced to a position
4681 where we must load IT->overlay_strings with more strings, do
4682 it. */
4683 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4684
4685 if (it->current.overlay_string_index && i == 0)
4686 load_overlay_strings (it, 0);
4687
4688 /* Initialize IT to deliver display elements from the overlay
4689 string. */
4690 it->string = it->overlay_strings[i];
4691 it->multibyte_p = STRING_MULTIBYTE (it->string);
4692 SET_TEXT_POS (it->current.string_pos, 0, 0);
4693 it->method = GET_FROM_STRING;
4694 it->stop_charpos = 0;
4695 }
4696
4697 CHECK_IT (it);
4698 }
4699
4700
4701 /* Compare two overlay_entry structures E1 and E2. Used as a
4702 comparison function for qsort in load_overlay_strings. Overlay
4703 strings for the same position are sorted so that
4704
4705 1. All after-strings come in front of before-strings, except
4706 when they come from the same overlay.
4707
4708 2. Within after-strings, strings are sorted so that overlay strings
4709 from overlays with higher priorities come first.
4710
4711 2. Within before-strings, strings are sorted so that overlay
4712 strings from overlays with higher priorities come last.
4713
4714 Value is analogous to strcmp. */
4715
4716
4717 static int
4718 compare_overlay_entries (e1, e2)
4719 void *e1, *e2;
4720 {
4721 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4722 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4723 int result;
4724
4725 if (entry1->after_string_p != entry2->after_string_p)
4726 {
4727 /* Let after-strings appear in front of before-strings if
4728 they come from different overlays. */
4729 if (EQ (entry1->overlay, entry2->overlay))
4730 result = entry1->after_string_p ? 1 : -1;
4731 else
4732 result = entry1->after_string_p ? -1 : 1;
4733 }
4734 else if (entry1->after_string_p)
4735 /* After-strings sorted in order of decreasing priority. */
4736 result = entry2->priority - entry1->priority;
4737 else
4738 /* Before-strings sorted in order of increasing priority. */
4739 result = entry1->priority - entry2->priority;
4740
4741 return result;
4742 }
4743
4744
4745 /* Load the vector IT->overlay_strings with overlay strings from IT's
4746 current buffer position, or from CHARPOS if that is > 0. Set
4747 IT->n_overlays to the total number of overlay strings found.
4748
4749 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4750 a time. On entry into load_overlay_strings,
4751 IT->current.overlay_string_index gives the number of overlay
4752 strings that have already been loaded by previous calls to this
4753 function.
4754
4755 IT->add_overlay_start contains an additional overlay start
4756 position to consider for taking overlay strings from, if non-zero.
4757 This position comes into play when the overlay has an `invisible'
4758 property, and both before and after-strings. When we've skipped to
4759 the end of the overlay, because of its `invisible' property, we
4760 nevertheless want its before-string to appear.
4761 IT->add_overlay_start will contain the overlay start position
4762 in this case.
4763
4764 Overlay strings are sorted so that after-string strings come in
4765 front of before-string strings. Within before and after-strings,
4766 strings are sorted by overlay priority. See also function
4767 compare_overlay_entries. */
4768
4769 static void
4770 load_overlay_strings (it, charpos)
4771 struct it *it;
4772 int charpos;
4773 {
4774 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4775 Lisp_Object overlay, window, str, invisible;
4776 struct Lisp_Overlay *ov;
4777 int start, end;
4778 int size = 20;
4779 int n = 0, i, j, invis_p;
4780 struct overlay_entry *entries
4781 = (struct overlay_entry *) alloca (size * sizeof *entries);
4782
4783 if (charpos <= 0)
4784 charpos = IT_CHARPOS (*it);
4785
4786 /* Append the overlay string STRING of overlay OVERLAY to vector
4787 `entries' which has size `size' and currently contains `n'
4788 elements. AFTER_P non-zero means STRING is an after-string of
4789 OVERLAY. */
4790 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4791 do \
4792 { \
4793 Lisp_Object priority; \
4794 \
4795 if (n == size) \
4796 { \
4797 int new_size = 2 * size; \
4798 struct overlay_entry *old = entries; \
4799 entries = \
4800 (struct overlay_entry *) alloca (new_size \
4801 * sizeof *entries); \
4802 bcopy (old, entries, size * sizeof *entries); \
4803 size = new_size; \
4804 } \
4805 \
4806 entries[n].string = (STRING); \
4807 entries[n].overlay = (OVERLAY); \
4808 priority = Foverlay_get ((OVERLAY), Qpriority); \
4809 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4810 entries[n].after_string_p = (AFTER_P); \
4811 ++n; \
4812 } \
4813 while (0)
4814
4815 /* Process overlay before the overlay center. */
4816 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4817 {
4818 XSETMISC (overlay, ov);
4819 xassert (OVERLAYP (overlay));
4820 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4821 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4822
4823 if (end < charpos)
4824 break;
4825
4826 /* Skip this overlay if it doesn't start or end at IT's current
4827 position. */
4828 if (end != charpos && start != charpos)
4829 continue;
4830
4831 /* Skip this overlay if it doesn't apply to IT->w. */
4832 window = Foverlay_get (overlay, Qwindow);
4833 if (WINDOWP (window) && XWINDOW (window) != it->w)
4834 continue;
4835
4836 /* If the text ``under'' the overlay is invisible, both before-
4837 and after-strings from this overlay are visible; start and
4838 end position are indistinguishable. */
4839 invisible = Foverlay_get (overlay, Qinvisible);
4840 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4841
4842 /* If overlay has a non-empty before-string, record it. */
4843 if ((start == charpos || (end == charpos && invis_p))
4844 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4845 && SCHARS (str))
4846 RECORD_OVERLAY_STRING (overlay, str, 0);
4847
4848 /* If overlay has a non-empty after-string, record it. */
4849 if ((end == charpos || (start == charpos && invis_p))
4850 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4851 && SCHARS (str))
4852 RECORD_OVERLAY_STRING (overlay, str, 1);
4853 }
4854
4855 /* Process overlays after the overlay center. */
4856 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4857 {
4858 XSETMISC (overlay, ov);
4859 xassert (OVERLAYP (overlay));
4860 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4861 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4862
4863 if (start > charpos)
4864 break;
4865
4866 /* Skip this overlay if it doesn't start or end at IT's current
4867 position. */
4868 if (end != charpos && start != charpos)
4869 continue;
4870
4871 /* Skip this overlay if it doesn't apply to IT->w. */
4872 window = Foverlay_get (overlay, Qwindow);
4873 if (WINDOWP (window) && XWINDOW (window) != it->w)
4874 continue;
4875
4876 /* If the text ``under'' the overlay is invisible, it has a zero
4877 dimension, and both before- and after-strings apply. */
4878 invisible = Foverlay_get (overlay, Qinvisible);
4879 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4880
4881 /* If overlay has a non-empty before-string, record it. */
4882 if ((start == charpos || (end == charpos && invis_p))
4883 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4884 && SCHARS (str))
4885 RECORD_OVERLAY_STRING (overlay, str, 0);
4886
4887 /* If overlay has a non-empty after-string, record it. */
4888 if ((end == charpos || (start == charpos && invis_p))
4889 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4890 && SCHARS (str))
4891 RECORD_OVERLAY_STRING (overlay, str, 1);
4892 }
4893
4894 #undef RECORD_OVERLAY_STRING
4895
4896 /* Sort entries. */
4897 if (n > 1)
4898 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4899
4900 /* Record the total number of strings to process. */
4901 it->n_overlay_strings = n;
4902
4903 /* IT->current.overlay_string_index is the number of overlay strings
4904 that have already been consumed by IT. Copy some of the
4905 remaining overlay strings to IT->overlay_strings. */
4906 i = 0;
4907 j = it->current.overlay_string_index;
4908 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4909 {
4910 it->overlay_strings[i] = entries[j].string;
4911 it->string_overlays[i++] = entries[j++].overlay;
4912 }
4913
4914 CHECK_IT (it);
4915 }
4916
4917
4918 /* Get the first chunk of overlay strings at IT's current buffer
4919 position, or at CHARPOS if that is > 0. Value is non-zero if at
4920 least one overlay string was found. */
4921
4922 static int
4923 get_overlay_strings_1 (it, charpos, compute_stop_p)
4924 struct it *it;
4925 int charpos;
4926 {
4927 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4928 process. This fills IT->overlay_strings with strings, and sets
4929 IT->n_overlay_strings to the total number of strings to process.
4930 IT->pos.overlay_string_index has to be set temporarily to zero
4931 because load_overlay_strings needs this; it must be set to -1
4932 when no overlay strings are found because a zero value would
4933 indicate a position in the first overlay string. */
4934 it->current.overlay_string_index = 0;
4935 load_overlay_strings (it, charpos);
4936
4937 /* If we found overlay strings, set up IT to deliver display
4938 elements from the first one. Otherwise set up IT to deliver
4939 from current_buffer. */
4940 if (it->n_overlay_strings)
4941 {
4942 /* Make sure we know settings in current_buffer, so that we can
4943 restore meaningful values when we're done with the overlay
4944 strings. */
4945 if (compute_stop_p)
4946 compute_stop_pos (it);
4947 xassert (it->face_id >= 0);
4948
4949 /* Save IT's settings. They are restored after all overlay
4950 strings have been processed. */
4951 xassert (!compute_stop_p || it->sp == 0);
4952 push_it (it);
4953
4954 /* Set up IT to deliver display elements from the first overlay
4955 string. */
4956 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4957 it->string = it->overlay_strings[0];
4958 it->from_overlay = Qnil;
4959 it->stop_charpos = 0;
4960 xassert (STRINGP (it->string));
4961 it->end_charpos = SCHARS (it->string);
4962 it->multibyte_p = STRING_MULTIBYTE (it->string);
4963 it->method = GET_FROM_STRING;
4964 return 1;
4965 }
4966
4967 it->current.overlay_string_index = -1;
4968 return 0;
4969 }
4970
4971 static int
4972 get_overlay_strings (it, charpos)
4973 struct it *it;
4974 int charpos;
4975 {
4976 it->string = Qnil;
4977 it->method = GET_FROM_BUFFER;
4978
4979 (void) get_overlay_strings_1 (it, charpos, 1);
4980
4981 CHECK_IT (it);
4982
4983 /* Value is non-zero if we found at least one overlay string. */
4984 return STRINGP (it->string);
4985 }
4986
4987
4988 \f
4989 /***********************************************************************
4990 Saving and restoring state
4991 ***********************************************************************/
4992
4993 /* Save current settings of IT on IT->stack. Called, for example,
4994 before setting up IT for an overlay string, to be able to restore
4995 IT's settings to what they were after the overlay string has been
4996 processed. */
4997
4998 static void
4999 push_it (it)
5000 struct it *it;
5001 {
5002 struct iterator_stack_entry *p;
5003
5004 xassert (it->sp < IT_STACK_SIZE);
5005 p = it->stack + it->sp;
5006
5007 p->stop_charpos = it->stop_charpos;
5008 xassert (it->face_id >= 0);
5009 p->face_id = it->face_id;
5010 p->string = it->string;
5011 p->method = it->method;
5012 p->from_overlay = it->from_overlay;
5013 switch (p->method)
5014 {
5015 case GET_FROM_IMAGE:
5016 p->u.image.object = it->object;
5017 p->u.image.image_id = it->image_id;
5018 p->u.image.slice = it->slice;
5019 break;
5020 case GET_FROM_COMPOSITION:
5021 p->u.comp.object = it->object;
5022 p->u.comp.c = it->c;
5023 p->u.comp.len = it->len;
5024 p->u.comp.cmp_id = it->cmp_id;
5025 p->u.comp.cmp_len = it->cmp_len;
5026 break;
5027 case GET_FROM_STRETCH:
5028 p->u.stretch.object = it->object;
5029 break;
5030 }
5031 p->position = it->position;
5032 p->current = it->current;
5033 p->end_charpos = it->end_charpos;
5034 p->string_nchars = it->string_nchars;
5035 p->area = it->area;
5036 p->multibyte_p = it->multibyte_p;
5037 p->space_width = it->space_width;
5038 p->font_height = it->font_height;
5039 p->voffset = it->voffset;
5040 p->string_from_display_prop_p = it->string_from_display_prop_p;
5041 p->display_ellipsis_p = 0;
5042 ++it->sp;
5043 }
5044
5045
5046 /* Restore IT's settings from IT->stack. Called, for example, when no
5047 more overlay strings must be processed, and we return to delivering
5048 display elements from a buffer, or when the end of a string from a
5049 `display' property is reached and we return to delivering display
5050 elements from an overlay string, or from a buffer. */
5051
5052 static void
5053 pop_it (it)
5054 struct it *it;
5055 {
5056 struct iterator_stack_entry *p;
5057
5058 xassert (it->sp > 0);
5059 --it->sp;
5060 p = it->stack + it->sp;
5061 it->stop_charpos = p->stop_charpos;
5062 it->face_id = p->face_id;
5063 it->current = p->current;
5064 it->position = p->position;
5065 it->string = p->string;
5066 it->from_overlay = p->from_overlay;
5067 if (NILP (it->string))
5068 SET_TEXT_POS (it->current.string_pos, -1, -1);
5069 it->method = p->method;
5070 switch (it->method)
5071 {
5072 case GET_FROM_IMAGE:
5073 it->image_id = p->u.image.image_id;
5074 it->object = p->u.image.object;
5075 it->slice = p->u.image.slice;
5076 break;
5077 case GET_FROM_COMPOSITION:
5078 it->object = p->u.comp.object;
5079 it->c = p->u.comp.c;
5080 it->len = p->u.comp.len;
5081 it->cmp_id = p->u.comp.cmp_id;
5082 it->cmp_len = p->u.comp.cmp_len;
5083 break;
5084 case GET_FROM_STRETCH:
5085 it->object = p->u.comp.object;
5086 break;
5087 case GET_FROM_BUFFER:
5088 it->object = it->w->buffer;
5089 break;
5090 case GET_FROM_STRING:
5091 it->object = it->string;
5092 break;
5093 }
5094 it->end_charpos = p->end_charpos;
5095 it->string_nchars = p->string_nchars;
5096 it->area = p->area;
5097 it->multibyte_p = p->multibyte_p;
5098 it->space_width = p->space_width;
5099 it->font_height = p->font_height;
5100 it->voffset = p->voffset;
5101 it->string_from_display_prop_p = p->string_from_display_prop_p;
5102 }
5103
5104
5105 \f
5106 /***********************************************************************
5107 Moving over lines
5108 ***********************************************************************/
5109
5110 /* Set IT's current position to the previous line start. */
5111
5112 static void
5113 back_to_previous_line_start (it)
5114 struct it *it;
5115 {
5116 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5117 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5118 }
5119
5120
5121 /* Move IT to the next line start.
5122
5123 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5124 we skipped over part of the text (as opposed to moving the iterator
5125 continuously over the text). Otherwise, don't change the value
5126 of *SKIPPED_P.
5127
5128 Newlines may come from buffer text, overlay strings, or strings
5129 displayed via the `display' property. That's the reason we can't
5130 simply use find_next_newline_no_quit.
5131
5132 Note that this function may not skip over invisible text that is so
5133 because of text properties and immediately follows a newline. If
5134 it would, function reseat_at_next_visible_line_start, when called
5135 from set_iterator_to_next, would effectively make invisible
5136 characters following a newline part of the wrong glyph row, which
5137 leads to wrong cursor motion. */
5138
5139 static int
5140 forward_to_next_line_start (it, skipped_p)
5141 struct it *it;
5142 int *skipped_p;
5143 {
5144 int old_selective, newline_found_p, n;
5145 const int MAX_NEWLINE_DISTANCE = 500;
5146
5147 /* If already on a newline, just consume it to avoid unintended
5148 skipping over invisible text below. */
5149 if (it->what == IT_CHARACTER
5150 && it->c == '\n'
5151 && CHARPOS (it->position) == IT_CHARPOS (*it))
5152 {
5153 set_iterator_to_next (it, 0);
5154 it->c = 0;
5155 return 1;
5156 }
5157
5158 /* Don't handle selective display in the following. It's (a)
5159 unnecessary because it's done by the caller, and (b) leads to an
5160 infinite recursion because next_element_from_ellipsis indirectly
5161 calls this function. */
5162 old_selective = it->selective;
5163 it->selective = 0;
5164
5165 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5166 from buffer text. */
5167 for (n = newline_found_p = 0;
5168 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5169 n += STRINGP (it->string) ? 0 : 1)
5170 {
5171 if (!get_next_display_element (it))
5172 return 0;
5173 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5174 set_iterator_to_next (it, 0);
5175 }
5176
5177 /* If we didn't find a newline near enough, see if we can use a
5178 short-cut. */
5179 if (!newline_found_p)
5180 {
5181 int start = IT_CHARPOS (*it);
5182 int limit = find_next_newline_no_quit (start, 1);
5183 Lisp_Object pos;
5184
5185 xassert (!STRINGP (it->string));
5186
5187 /* If there isn't any `display' property in sight, and no
5188 overlays, we can just use the position of the newline in
5189 buffer text. */
5190 if (it->stop_charpos >= limit
5191 || ((pos = Fnext_single_property_change (make_number (start),
5192 Qdisplay,
5193 Qnil, make_number (limit)),
5194 NILP (pos))
5195 && next_overlay_change (start) == ZV))
5196 {
5197 IT_CHARPOS (*it) = limit;
5198 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5199 *skipped_p = newline_found_p = 1;
5200 }
5201 else
5202 {
5203 while (get_next_display_element (it)
5204 && !newline_found_p)
5205 {
5206 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5207 set_iterator_to_next (it, 0);
5208 }
5209 }
5210 }
5211
5212 it->selective = old_selective;
5213 return newline_found_p;
5214 }
5215
5216
5217 /* Set IT's current position to the previous visible line start. Skip
5218 invisible text that is so either due to text properties or due to
5219 selective display. Caution: this does not change IT->current_x and
5220 IT->hpos. */
5221
5222 static void
5223 back_to_previous_visible_line_start (it)
5224 struct it *it;
5225 {
5226 while (IT_CHARPOS (*it) > BEGV)
5227 {
5228 back_to_previous_line_start (it);
5229
5230 if (IT_CHARPOS (*it) <= BEGV)
5231 break;
5232
5233 /* If selective > 0, then lines indented more than that values
5234 are invisible. */
5235 if (it->selective > 0
5236 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5237 (double) it->selective)) /* iftc */
5238 continue;
5239
5240 /* Check the newline before point for invisibility. */
5241 {
5242 Lisp_Object prop;
5243 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5244 Qinvisible, it->window);
5245 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5246 continue;
5247 }
5248
5249 if (IT_CHARPOS (*it) <= BEGV)
5250 break;
5251
5252 {
5253 struct it it2;
5254 int pos;
5255 int beg, end;
5256 Lisp_Object val, overlay;
5257
5258 /* If newline is part of a composition, continue from start of composition */
5259 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5260 && beg < IT_CHARPOS (*it))
5261 goto replaced;
5262
5263 /* If newline is replaced by a display property, find start of overlay
5264 or interval and continue search from that point. */
5265 it2 = *it;
5266 pos = --IT_CHARPOS (it2);
5267 --IT_BYTEPOS (it2);
5268 it2.sp = 0;
5269 if (handle_display_prop (&it2) == HANDLED_RETURN
5270 && !NILP (val = get_char_property_and_overlay
5271 (make_number (pos), Qdisplay, Qnil, &overlay))
5272 && (OVERLAYP (overlay)
5273 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5274 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5275 goto replaced;
5276
5277 /* Newline is not replaced by anything -- so we are done. */
5278 break;
5279
5280 replaced:
5281 if (beg < BEGV)
5282 beg = BEGV;
5283 IT_CHARPOS (*it) = beg;
5284 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5285 }
5286 }
5287
5288 it->continuation_lines_width = 0;
5289
5290 xassert (IT_CHARPOS (*it) >= BEGV);
5291 xassert (IT_CHARPOS (*it) == BEGV
5292 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5293 CHECK_IT (it);
5294 }
5295
5296
5297 /* Reseat iterator IT at the previous visible line start. Skip
5298 invisible text that is so either due to text properties or due to
5299 selective display. At the end, update IT's overlay information,
5300 face information etc. */
5301
5302 void
5303 reseat_at_previous_visible_line_start (it)
5304 struct it *it;
5305 {
5306 back_to_previous_visible_line_start (it);
5307 reseat (it, it->current.pos, 1);
5308 CHECK_IT (it);
5309 }
5310
5311
5312 /* Reseat iterator IT on the next visible line start in the current
5313 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5314 preceding the line start. Skip over invisible text that is so
5315 because of selective display. Compute faces, overlays etc at the
5316 new position. Note that this function does not skip over text that
5317 is invisible because of text properties. */
5318
5319 static void
5320 reseat_at_next_visible_line_start (it, on_newline_p)
5321 struct it *it;
5322 int on_newline_p;
5323 {
5324 int newline_found_p, skipped_p = 0;
5325
5326 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5327
5328 /* Skip over lines that are invisible because they are indented
5329 more than the value of IT->selective. */
5330 if (it->selective > 0)
5331 while (IT_CHARPOS (*it) < ZV
5332 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5333 (double) it->selective)) /* iftc */
5334 {
5335 xassert (IT_BYTEPOS (*it) == BEGV
5336 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5337 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5338 }
5339
5340 /* Position on the newline if that's what's requested. */
5341 if (on_newline_p && newline_found_p)
5342 {
5343 if (STRINGP (it->string))
5344 {
5345 if (IT_STRING_CHARPOS (*it) > 0)
5346 {
5347 --IT_STRING_CHARPOS (*it);
5348 --IT_STRING_BYTEPOS (*it);
5349 }
5350 }
5351 else if (IT_CHARPOS (*it) > BEGV)
5352 {
5353 --IT_CHARPOS (*it);
5354 --IT_BYTEPOS (*it);
5355 reseat (it, it->current.pos, 0);
5356 }
5357 }
5358 else if (skipped_p)
5359 reseat (it, it->current.pos, 0);
5360
5361 CHECK_IT (it);
5362 }
5363
5364
5365 \f
5366 /***********************************************************************
5367 Changing an iterator's position
5368 ***********************************************************************/
5369
5370 /* Change IT's current position to POS in current_buffer. If FORCE_P
5371 is non-zero, always check for text properties at the new position.
5372 Otherwise, text properties are only looked up if POS >=
5373 IT->check_charpos of a property. */
5374
5375 static void
5376 reseat (it, pos, force_p)
5377 struct it *it;
5378 struct text_pos pos;
5379 int force_p;
5380 {
5381 int original_pos = IT_CHARPOS (*it);
5382
5383 reseat_1 (it, pos, 0);
5384
5385 /* Determine where to check text properties. Avoid doing it
5386 where possible because text property lookup is very expensive. */
5387 if (force_p
5388 || CHARPOS (pos) > it->stop_charpos
5389 || CHARPOS (pos) < original_pos)
5390 handle_stop (it);
5391
5392 CHECK_IT (it);
5393 }
5394
5395
5396 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5397 IT->stop_pos to POS, also. */
5398
5399 static void
5400 reseat_1 (it, pos, set_stop_p)
5401 struct it *it;
5402 struct text_pos pos;
5403 int set_stop_p;
5404 {
5405 /* Don't call this function when scanning a C string. */
5406 xassert (it->s == NULL);
5407
5408 /* POS must be a reasonable value. */
5409 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5410
5411 it->current.pos = it->position = pos;
5412 it->end_charpos = ZV;
5413 it->dpvec = NULL;
5414 it->current.dpvec_index = -1;
5415 it->current.overlay_string_index = -1;
5416 IT_STRING_CHARPOS (*it) = -1;
5417 IT_STRING_BYTEPOS (*it) = -1;
5418 it->string = Qnil;
5419 it->method = GET_FROM_BUFFER;
5420 it->object = it->w->buffer;
5421 it->area = TEXT_AREA;
5422 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5423 it->sp = 0;
5424 it->string_from_display_prop_p = 0;
5425 it->face_before_selective_p = 0;
5426
5427 if (set_stop_p)
5428 it->stop_charpos = CHARPOS (pos);
5429 }
5430
5431
5432 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5433 If S is non-null, it is a C string to iterate over. Otherwise,
5434 STRING gives a Lisp string to iterate over.
5435
5436 If PRECISION > 0, don't return more then PRECISION number of
5437 characters from the string.
5438
5439 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5440 characters have been returned. FIELD_WIDTH < 0 means an infinite
5441 field width.
5442
5443 MULTIBYTE = 0 means disable processing of multibyte characters,
5444 MULTIBYTE > 0 means enable it,
5445 MULTIBYTE < 0 means use IT->multibyte_p.
5446
5447 IT must be initialized via a prior call to init_iterator before
5448 calling this function. */
5449
5450 static void
5451 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5452 struct it *it;
5453 unsigned char *s;
5454 Lisp_Object string;
5455 int charpos;
5456 int precision, field_width, multibyte;
5457 {
5458 /* No region in strings. */
5459 it->region_beg_charpos = it->region_end_charpos = -1;
5460
5461 /* No text property checks performed by default, but see below. */
5462 it->stop_charpos = -1;
5463
5464 /* Set iterator position and end position. */
5465 bzero (&it->current, sizeof it->current);
5466 it->current.overlay_string_index = -1;
5467 it->current.dpvec_index = -1;
5468 xassert (charpos >= 0);
5469
5470 /* If STRING is specified, use its multibyteness, otherwise use the
5471 setting of MULTIBYTE, if specified. */
5472 if (multibyte >= 0)
5473 it->multibyte_p = multibyte > 0;
5474
5475 if (s == NULL)
5476 {
5477 xassert (STRINGP (string));
5478 it->string = string;
5479 it->s = NULL;
5480 it->end_charpos = it->string_nchars = SCHARS (string);
5481 it->method = GET_FROM_STRING;
5482 it->current.string_pos = string_pos (charpos, string);
5483 }
5484 else
5485 {
5486 it->s = s;
5487 it->string = Qnil;
5488
5489 /* Note that we use IT->current.pos, not it->current.string_pos,
5490 for displaying C strings. */
5491 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5492 if (it->multibyte_p)
5493 {
5494 it->current.pos = c_string_pos (charpos, s, 1);
5495 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5496 }
5497 else
5498 {
5499 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5500 it->end_charpos = it->string_nchars = strlen (s);
5501 }
5502
5503 it->method = GET_FROM_C_STRING;
5504 }
5505
5506 /* PRECISION > 0 means don't return more than PRECISION characters
5507 from the string. */
5508 if (precision > 0 && it->end_charpos - charpos > precision)
5509 it->end_charpos = it->string_nchars = charpos + precision;
5510
5511 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5512 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5513 FIELD_WIDTH < 0 means infinite field width. This is useful for
5514 padding with `-' at the end of a mode line. */
5515 if (field_width < 0)
5516 field_width = INFINITY;
5517 if (field_width > it->end_charpos - charpos)
5518 it->end_charpos = charpos + field_width;
5519
5520 /* Use the standard display table for displaying strings. */
5521 if (DISP_TABLE_P (Vstandard_display_table))
5522 it->dp = XCHAR_TABLE (Vstandard_display_table);
5523
5524 it->stop_charpos = charpos;
5525 CHECK_IT (it);
5526 }
5527
5528
5529 \f
5530 /***********************************************************************
5531 Iteration
5532 ***********************************************************************/
5533
5534 /* Map enum it_method value to corresponding next_element_from_* function. */
5535
5536 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5537 {
5538 next_element_from_buffer,
5539 next_element_from_display_vector,
5540 next_element_from_composition,
5541 next_element_from_string,
5542 next_element_from_c_string,
5543 next_element_from_image,
5544 next_element_from_stretch
5545 };
5546
5547
5548 /* Load IT's display element fields with information about the next
5549 display element from the current position of IT. Value is zero if
5550 end of buffer (or C string) is reached. */
5551
5552 static struct frame *last_escape_glyph_frame = NULL;
5553 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5554 static int last_escape_glyph_merged_face_id = 0;
5555
5556 int
5557 get_next_display_element (it)
5558 struct it *it;
5559 {
5560 /* Non-zero means that we found a display element. Zero means that
5561 we hit the end of what we iterate over. Performance note: the
5562 function pointer `method' used here turns out to be faster than
5563 using a sequence of if-statements. */
5564 int success_p;
5565
5566 get_next:
5567 success_p = (*get_next_element[it->method]) (it);
5568
5569 if (it->what == IT_CHARACTER)
5570 {
5571 /* Map via display table or translate control characters.
5572 IT->c, IT->len etc. have been set to the next character by
5573 the function call above. If we have a display table, and it
5574 contains an entry for IT->c, translate it. Don't do this if
5575 IT->c itself comes from a display table, otherwise we could
5576 end up in an infinite recursion. (An alternative could be to
5577 count the recursion depth of this function and signal an
5578 error when a certain maximum depth is reached.) Is it worth
5579 it? */
5580 if (success_p && it->dpvec == NULL)
5581 {
5582 Lisp_Object dv;
5583
5584 if (it->dp
5585 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5586 VECTORP (dv)))
5587 {
5588 struct Lisp_Vector *v = XVECTOR (dv);
5589
5590 /* Return the first character from the display table
5591 entry, if not empty. If empty, don't display the
5592 current character. */
5593 if (v->size)
5594 {
5595 it->dpvec_char_len = it->len;
5596 it->dpvec = v->contents;
5597 it->dpend = v->contents + v->size;
5598 it->current.dpvec_index = 0;
5599 it->dpvec_face_id = -1;
5600 it->saved_face_id = it->face_id;
5601 it->method = GET_FROM_DISPLAY_VECTOR;
5602 it->ellipsis_p = 0;
5603 }
5604 else
5605 {
5606 set_iterator_to_next (it, 0);
5607 }
5608 goto get_next;
5609 }
5610
5611 /* Translate control characters into `\003' or `^C' form.
5612 Control characters coming from a display table entry are
5613 currently not translated because we use IT->dpvec to hold
5614 the translation. This could easily be changed but I
5615 don't believe that it is worth doing.
5616
5617 If it->multibyte_p is nonzero, eight-bit characters and
5618 non-printable multibyte characters are also translated to
5619 octal form.
5620
5621 If it->multibyte_p is zero, eight-bit characters that
5622 don't have corresponding multibyte char code are also
5623 translated to octal form. */
5624 else if ((it->c < ' '
5625 && (it->area != TEXT_AREA
5626 /* In mode line, treat \n like other crl chars. */
5627 || (it->c != '\t'
5628 && it->glyph_row && it->glyph_row->mode_line_p)
5629 || (it->c != '\n' && it->c != '\t')))
5630 || (it->multibyte_p
5631 ? ((it->c >= 127
5632 && it->len == 1)
5633 || !CHAR_PRINTABLE_P (it->c)
5634 || (!NILP (Vnobreak_char_display)
5635 && (it->c == 0x8a0 || it->c == 0x8ad
5636 || it->c == 0x920 || it->c == 0x92d
5637 || it->c == 0xe20 || it->c == 0xe2d
5638 || it->c == 0xf20 || it->c == 0xf2d)))
5639 : (it->c >= 127
5640 && (!unibyte_display_via_language_environment
5641 || it->c == unibyte_char_to_multibyte (it->c)))))
5642 {
5643 /* IT->c is a control character which must be displayed
5644 either as '\003' or as `^C' where the '\\' and '^'
5645 can be defined in the display table. Fill
5646 IT->ctl_chars with glyphs for what we have to
5647 display. Then, set IT->dpvec to these glyphs. */
5648 GLYPH g;
5649 int ctl_len;
5650 int face_id, lface_id = 0 ;
5651 GLYPH escape_glyph;
5652
5653 /* Handle control characters with ^. */
5654
5655 if (it->c < 128 && it->ctl_arrow_p)
5656 {
5657 g = '^'; /* default glyph for Control */
5658 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5659 if (it->dp
5660 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5661 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5662 {
5663 g = XINT (DISP_CTRL_GLYPH (it->dp));
5664 lface_id = FAST_GLYPH_FACE (g);
5665 }
5666 if (lface_id)
5667 {
5668 g = FAST_GLYPH_CHAR (g);
5669 face_id = merge_faces (it->f, Qt, lface_id,
5670 it->face_id);
5671 }
5672 else if (it->f == last_escape_glyph_frame
5673 && it->face_id == last_escape_glyph_face_id)
5674 {
5675 face_id = last_escape_glyph_merged_face_id;
5676 }
5677 else
5678 {
5679 /* Merge the escape-glyph face into the current face. */
5680 face_id = merge_faces (it->f, Qescape_glyph, 0,
5681 it->face_id);
5682 last_escape_glyph_frame = it->f;
5683 last_escape_glyph_face_id = it->face_id;
5684 last_escape_glyph_merged_face_id = face_id;
5685 }
5686
5687 XSETINT (it->ctl_chars[0], g);
5688 g = it->c ^ 0100;
5689 XSETINT (it->ctl_chars[1], g);
5690 ctl_len = 2;
5691 goto display_control;
5692 }
5693
5694 /* Handle non-break space in the mode where it only gets
5695 highlighting. */
5696
5697 if (EQ (Vnobreak_char_display, Qt)
5698 && (it->c == 0x8a0 || it->c == 0x920
5699 || it->c == 0xe20 || it->c == 0xf20))
5700 {
5701 /* Merge the no-break-space face into the current face. */
5702 face_id = merge_faces (it->f, Qnobreak_space, 0,
5703 it->face_id);
5704
5705 g = it->c = ' ';
5706 XSETINT (it->ctl_chars[0], g);
5707 ctl_len = 1;
5708 goto display_control;
5709 }
5710
5711 /* Handle sequences that start with the "escape glyph". */
5712
5713 /* the default escape glyph is \. */
5714 escape_glyph = '\\';
5715
5716 if (it->dp
5717 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5718 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5719 {
5720 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5721 lface_id = FAST_GLYPH_FACE (escape_glyph);
5722 }
5723 if (lface_id)
5724 {
5725 /* The display table specified a face.
5726 Merge it into face_id and also into escape_glyph. */
5727 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5728 face_id = merge_faces (it->f, Qt, lface_id,
5729 it->face_id);
5730 }
5731 else if (it->f == last_escape_glyph_frame
5732 && it->face_id == last_escape_glyph_face_id)
5733 {
5734 face_id = last_escape_glyph_merged_face_id;
5735 }
5736 else
5737 {
5738 /* Merge the escape-glyph face into the current face. */
5739 face_id = merge_faces (it->f, Qescape_glyph, 0,
5740 it->face_id);
5741 last_escape_glyph_frame = it->f;
5742 last_escape_glyph_face_id = it->face_id;
5743 last_escape_glyph_merged_face_id = face_id;
5744 }
5745
5746 /* Handle soft hyphens in the mode where they only get
5747 highlighting. */
5748
5749 if (EQ (Vnobreak_char_display, Qt)
5750 && (it->c == 0x8ad || it->c == 0x92d
5751 || it->c == 0xe2d || it->c == 0xf2d))
5752 {
5753 g = it->c = '-';
5754 XSETINT (it->ctl_chars[0], g);
5755 ctl_len = 1;
5756 goto display_control;
5757 }
5758
5759 /* Handle non-break space and soft hyphen
5760 with the escape glyph. */
5761
5762 if (it->c == 0x8a0 || it->c == 0x8ad
5763 || it->c == 0x920 || it->c == 0x92d
5764 || it->c == 0xe20 || it->c == 0xe2d
5765 || it->c == 0xf20 || it->c == 0xf2d)
5766 {
5767 XSETINT (it->ctl_chars[0], escape_glyph);
5768 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5769 XSETINT (it->ctl_chars[1], g);
5770 ctl_len = 2;
5771 goto display_control;
5772 }
5773
5774 {
5775 unsigned char str[MAX_MULTIBYTE_LENGTH];
5776 int len;
5777 int i;
5778
5779 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5780 if (SINGLE_BYTE_CHAR_P (it->c))
5781 str[0] = it->c, len = 1;
5782 else
5783 {
5784 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5785 if (len < 0)
5786 {
5787 /* It's an invalid character, which shouldn't
5788 happen actually, but due to bugs it may
5789 happen. Let's print the char as is, there's
5790 not much meaningful we can do with it. */
5791 str[0] = it->c;
5792 str[1] = it->c >> 8;
5793 str[2] = it->c >> 16;
5794 str[3] = it->c >> 24;
5795 len = 4;
5796 }
5797 }
5798
5799 for (i = 0; i < len; i++)
5800 {
5801 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5802 /* Insert three more glyphs into IT->ctl_chars for
5803 the octal display of the character. */
5804 g = ((str[i] >> 6) & 7) + '0';
5805 XSETINT (it->ctl_chars[i * 4 + 1], g);
5806 g = ((str[i] >> 3) & 7) + '0';
5807 XSETINT (it->ctl_chars[i * 4 + 2], g);
5808 g = (str[i] & 7) + '0';
5809 XSETINT (it->ctl_chars[i * 4 + 3], g);
5810 }
5811 ctl_len = len * 4;
5812 }
5813
5814 display_control:
5815 /* Set up IT->dpvec and return first character from it. */
5816 it->dpvec_char_len = it->len;
5817 it->dpvec = it->ctl_chars;
5818 it->dpend = it->dpvec + ctl_len;
5819 it->current.dpvec_index = 0;
5820 it->dpvec_face_id = face_id;
5821 it->saved_face_id = it->face_id;
5822 it->method = GET_FROM_DISPLAY_VECTOR;
5823 it->ellipsis_p = 0;
5824 goto get_next;
5825 }
5826 }
5827
5828 /* Adjust face id for a multibyte character. There are no
5829 multibyte character in unibyte text. */
5830 if (it->multibyte_p
5831 && success_p
5832 && FRAME_WINDOW_P (it->f))
5833 {
5834 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5835 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5836 }
5837 }
5838
5839 /* Is this character the last one of a run of characters with
5840 box? If yes, set IT->end_of_box_run_p to 1. */
5841 if (it->face_box_p
5842 && it->s == NULL)
5843 {
5844 int face_id;
5845 struct face *face;
5846
5847 it->end_of_box_run_p
5848 = ((face_id = face_after_it_pos (it),
5849 face_id != it->face_id)
5850 && (face = FACE_FROM_ID (it->f, face_id),
5851 face->box == FACE_NO_BOX));
5852 }
5853
5854 /* Value is 0 if end of buffer or string reached. */
5855 return success_p;
5856 }
5857
5858
5859 /* Move IT to the next display element.
5860
5861 RESEAT_P non-zero means if called on a newline in buffer text,
5862 skip to the next visible line start.
5863
5864 Functions get_next_display_element and set_iterator_to_next are
5865 separate because I find this arrangement easier to handle than a
5866 get_next_display_element function that also increments IT's
5867 position. The way it is we can first look at an iterator's current
5868 display element, decide whether it fits on a line, and if it does,
5869 increment the iterator position. The other way around we probably
5870 would either need a flag indicating whether the iterator has to be
5871 incremented the next time, or we would have to implement a
5872 decrement position function which would not be easy to write. */
5873
5874 void
5875 set_iterator_to_next (it, reseat_p)
5876 struct it *it;
5877 int reseat_p;
5878 {
5879 /* Reset flags indicating start and end of a sequence of characters
5880 with box. Reset them at the start of this function because
5881 moving the iterator to a new position might set them. */
5882 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5883
5884 switch (it->method)
5885 {
5886 case GET_FROM_BUFFER:
5887 /* The current display element of IT is a character from
5888 current_buffer. Advance in the buffer, and maybe skip over
5889 invisible lines that are so because of selective display. */
5890 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5891 reseat_at_next_visible_line_start (it, 0);
5892 else
5893 {
5894 xassert (it->len != 0);
5895 IT_BYTEPOS (*it) += it->len;
5896 IT_CHARPOS (*it) += 1;
5897 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5898 }
5899 break;
5900
5901 case GET_FROM_COMPOSITION:
5902 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5903 xassert (it->sp > 0);
5904 pop_it (it);
5905 if (it->method == GET_FROM_STRING)
5906 {
5907 IT_STRING_BYTEPOS (*it) += it->len;
5908 IT_STRING_CHARPOS (*it) += it->cmp_len;
5909 goto consider_string_end;
5910 }
5911 else if (it->method == GET_FROM_BUFFER)
5912 {
5913 IT_BYTEPOS (*it) += it->len;
5914 IT_CHARPOS (*it) += it->cmp_len;
5915 }
5916 break;
5917
5918 case GET_FROM_C_STRING:
5919 /* Current display element of IT is from a C string. */
5920 IT_BYTEPOS (*it) += it->len;
5921 IT_CHARPOS (*it) += 1;
5922 break;
5923
5924 case GET_FROM_DISPLAY_VECTOR:
5925 /* Current display element of IT is from a display table entry.
5926 Advance in the display table definition. Reset it to null if
5927 end reached, and continue with characters from buffers/
5928 strings. */
5929 ++it->current.dpvec_index;
5930
5931 /* Restore face of the iterator to what they were before the
5932 display vector entry (these entries may contain faces). */
5933 it->face_id = it->saved_face_id;
5934
5935 if (it->dpvec + it->current.dpvec_index == it->dpend)
5936 {
5937 int recheck_faces = it->ellipsis_p;
5938
5939 if (it->s)
5940 it->method = GET_FROM_C_STRING;
5941 else if (STRINGP (it->string))
5942 it->method = GET_FROM_STRING;
5943 else
5944 {
5945 it->method = GET_FROM_BUFFER;
5946 it->object = it->w->buffer;
5947 }
5948
5949 it->dpvec = NULL;
5950 it->current.dpvec_index = -1;
5951
5952 /* Skip over characters which were displayed via IT->dpvec. */
5953 if (it->dpvec_char_len < 0)
5954 reseat_at_next_visible_line_start (it, 1);
5955 else if (it->dpvec_char_len > 0)
5956 {
5957 if (it->method == GET_FROM_STRING
5958 && it->n_overlay_strings > 0)
5959 it->ignore_overlay_strings_at_pos_p = 1;
5960 it->len = it->dpvec_char_len;
5961 set_iterator_to_next (it, reseat_p);
5962 }
5963
5964 /* Maybe recheck faces after display vector */
5965 if (recheck_faces)
5966 it->stop_charpos = IT_CHARPOS (*it);
5967 }
5968 break;
5969
5970 case GET_FROM_STRING:
5971 /* Current display element is a character from a Lisp string. */
5972 xassert (it->s == NULL && STRINGP (it->string));
5973 IT_STRING_BYTEPOS (*it) += it->len;
5974 IT_STRING_CHARPOS (*it) += 1;
5975
5976 consider_string_end:
5977
5978 if (it->current.overlay_string_index >= 0)
5979 {
5980 /* IT->string is an overlay string. Advance to the
5981 next, if there is one. */
5982 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5983 next_overlay_string (it);
5984 }
5985 else
5986 {
5987 /* IT->string is not an overlay string. If we reached
5988 its end, and there is something on IT->stack, proceed
5989 with what is on the stack. This can be either another
5990 string, this time an overlay string, or a buffer. */
5991 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5992 && it->sp > 0)
5993 {
5994 pop_it (it);
5995 if (it->method == GET_FROM_STRING)
5996 goto consider_string_end;
5997 }
5998 }
5999 break;
6000
6001 case GET_FROM_IMAGE:
6002 case GET_FROM_STRETCH:
6003 /* The position etc with which we have to proceed are on
6004 the stack. The position may be at the end of a string,
6005 if the `display' property takes up the whole string. */
6006 xassert (it->sp > 0);
6007 pop_it (it);
6008 if (it->method == GET_FROM_STRING)
6009 goto consider_string_end;
6010 break;
6011
6012 default:
6013 /* There are no other methods defined, so this should be a bug. */
6014 abort ();
6015 }
6016
6017 xassert (it->method != GET_FROM_STRING
6018 || (STRINGP (it->string)
6019 && IT_STRING_CHARPOS (*it) >= 0));
6020 }
6021
6022 /* Load IT's display element fields with information about the next
6023 display element which comes from a display table entry or from the
6024 result of translating a control character to one of the forms `^C'
6025 or `\003'.
6026
6027 IT->dpvec holds the glyphs to return as characters.
6028 IT->saved_face_id holds the face id before the display vector--
6029 it is restored into IT->face_idin set_iterator_to_next. */
6030
6031 static int
6032 next_element_from_display_vector (it)
6033 struct it *it;
6034 {
6035 /* Precondition. */
6036 xassert (it->dpvec && it->current.dpvec_index >= 0);
6037
6038 it->face_id = it->saved_face_id;
6039
6040 if (INTEGERP (*it->dpvec)
6041 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6042 {
6043 GLYPH g;
6044
6045 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6046 it->c = FAST_GLYPH_CHAR (g);
6047 it->len = CHAR_BYTES (it->c);
6048
6049 /* The entry may contain a face id to use. Such a face id is
6050 the id of a Lisp face, not a realized face. A face id of
6051 zero means no face is specified. */
6052 if (it->dpvec_face_id >= 0)
6053 it->face_id = it->dpvec_face_id;
6054 else
6055 {
6056 int lface_id = FAST_GLYPH_FACE (g);
6057 if (lface_id > 0)
6058 it->face_id = merge_faces (it->f, Qt, lface_id,
6059 it->saved_face_id);
6060 }
6061 }
6062 else
6063 /* Display table entry is invalid. Return a space. */
6064 it->c = ' ', it->len = 1;
6065
6066 /* Don't change position and object of the iterator here. They are
6067 still the values of the character that had this display table
6068 entry or was translated, and that's what we want. */
6069 it->what = IT_CHARACTER;
6070 return 1;
6071 }
6072
6073
6074 /* Load IT with the next display element from Lisp string IT->string.
6075 IT->current.string_pos is the current position within the string.
6076 If IT->current.overlay_string_index >= 0, the Lisp string is an
6077 overlay string. */
6078
6079 static int
6080 next_element_from_string (it)
6081 struct it *it;
6082 {
6083 struct text_pos position;
6084
6085 xassert (STRINGP (it->string));
6086 xassert (IT_STRING_CHARPOS (*it) >= 0);
6087 position = it->current.string_pos;
6088
6089 /* Time to check for invisible text? */
6090 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6091 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6092 {
6093 handle_stop (it);
6094
6095 /* Since a handler may have changed IT->method, we must
6096 recurse here. */
6097 return get_next_display_element (it);
6098 }
6099
6100 if (it->current.overlay_string_index >= 0)
6101 {
6102 /* Get the next character from an overlay string. In overlay
6103 strings, There is no field width or padding with spaces to
6104 do. */
6105 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6106 {
6107 it->what = IT_EOB;
6108 return 0;
6109 }
6110 else if (STRING_MULTIBYTE (it->string))
6111 {
6112 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6113 const unsigned char *s = (SDATA (it->string)
6114 + IT_STRING_BYTEPOS (*it));
6115 it->c = string_char_and_length (s, remaining, &it->len);
6116 }
6117 else
6118 {
6119 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6120 it->len = 1;
6121 }
6122 }
6123 else
6124 {
6125 /* Get the next character from a Lisp string that is not an
6126 overlay string. Such strings come from the mode line, for
6127 example. We may have to pad with spaces, or truncate the
6128 string. See also next_element_from_c_string. */
6129 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6130 {
6131 it->what = IT_EOB;
6132 return 0;
6133 }
6134 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6135 {
6136 /* Pad with spaces. */
6137 it->c = ' ', it->len = 1;
6138 CHARPOS (position) = BYTEPOS (position) = -1;
6139 }
6140 else if (STRING_MULTIBYTE (it->string))
6141 {
6142 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6143 const unsigned char *s = (SDATA (it->string)
6144 + IT_STRING_BYTEPOS (*it));
6145 it->c = string_char_and_length (s, maxlen, &it->len);
6146 }
6147 else
6148 {
6149 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6150 it->len = 1;
6151 }
6152 }
6153
6154 /* Record what we have and where it came from. */
6155 it->what = IT_CHARACTER;
6156 it->object = it->string;
6157 it->position = position;
6158 return 1;
6159 }
6160
6161
6162 /* Load IT with next display element from C string IT->s.
6163 IT->string_nchars is the maximum number of characters to return
6164 from the string. IT->end_charpos may be greater than
6165 IT->string_nchars when this function is called, in which case we
6166 may have to return padding spaces. Value is zero if end of string
6167 reached, including padding spaces. */
6168
6169 static int
6170 next_element_from_c_string (it)
6171 struct it *it;
6172 {
6173 int success_p = 1;
6174
6175 xassert (it->s);
6176 it->what = IT_CHARACTER;
6177 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6178 it->object = Qnil;
6179
6180 /* IT's position can be greater IT->string_nchars in case a field
6181 width or precision has been specified when the iterator was
6182 initialized. */
6183 if (IT_CHARPOS (*it) >= it->end_charpos)
6184 {
6185 /* End of the game. */
6186 it->what = IT_EOB;
6187 success_p = 0;
6188 }
6189 else if (IT_CHARPOS (*it) >= it->string_nchars)
6190 {
6191 /* Pad with spaces. */
6192 it->c = ' ', it->len = 1;
6193 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6194 }
6195 else if (it->multibyte_p)
6196 {
6197 /* Implementation note: The calls to strlen apparently aren't a
6198 performance problem because there is no noticeable performance
6199 difference between Emacs running in unibyte or multibyte mode. */
6200 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6201 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6202 maxlen, &it->len);
6203 }
6204 else
6205 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6206
6207 return success_p;
6208 }
6209
6210
6211 /* Set up IT to return characters from an ellipsis, if appropriate.
6212 The definition of the ellipsis glyphs may come from a display table
6213 entry. This function Fills IT with the first glyph from the
6214 ellipsis if an ellipsis is to be displayed. */
6215
6216 static int
6217 next_element_from_ellipsis (it)
6218 struct it *it;
6219 {
6220 if (it->selective_display_ellipsis_p)
6221 setup_for_ellipsis (it, it->len);
6222 else
6223 {
6224 /* The face at the current position may be different from the
6225 face we find after the invisible text. Remember what it
6226 was in IT->saved_face_id, and signal that it's there by
6227 setting face_before_selective_p. */
6228 it->saved_face_id = it->face_id;
6229 it->method = GET_FROM_BUFFER;
6230 it->object = it->w->buffer;
6231 reseat_at_next_visible_line_start (it, 1);
6232 it->face_before_selective_p = 1;
6233 }
6234
6235 return get_next_display_element (it);
6236 }
6237
6238
6239 /* Deliver an image display element. The iterator IT is already
6240 filled with image information (done in handle_display_prop). Value
6241 is always 1. */
6242
6243
6244 static int
6245 next_element_from_image (it)
6246 struct it *it;
6247 {
6248 it->what = IT_IMAGE;
6249 return 1;
6250 }
6251
6252
6253 /* Fill iterator IT with next display element from a stretch glyph
6254 property. IT->object is the value of the text property. Value is
6255 always 1. */
6256
6257 static int
6258 next_element_from_stretch (it)
6259 struct it *it;
6260 {
6261 it->what = IT_STRETCH;
6262 return 1;
6263 }
6264
6265
6266 /* Load IT with the next display element from current_buffer. Value
6267 is zero if end of buffer reached. IT->stop_charpos is the next
6268 position at which to stop and check for text properties or buffer
6269 end. */
6270
6271 static int
6272 next_element_from_buffer (it)
6273 struct it *it;
6274 {
6275 int success_p = 1;
6276
6277 /* Check this assumption, otherwise, we would never enter the
6278 if-statement, below. */
6279 xassert (IT_CHARPOS (*it) >= BEGV
6280 && IT_CHARPOS (*it) <= it->stop_charpos);
6281
6282 if (IT_CHARPOS (*it) >= it->stop_charpos)
6283 {
6284 if (IT_CHARPOS (*it) >= it->end_charpos)
6285 {
6286 int overlay_strings_follow_p;
6287
6288 /* End of the game, except when overlay strings follow that
6289 haven't been returned yet. */
6290 if (it->overlay_strings_at_end_processed_p)
6291 overlay_strings_follow_p = 0;
6292 else
6293 {
6294 it->overlay_strings_at_end_processed_p = 1;
6295 overlay_strings_follow_p = get_overlay_strings (it, 0);
6296 }
6297
6298 if (overlay_strings_follow_p)
6299 success_p = get_next_display_element (it);
6300 else
6301 {
6302 it->what = IT_EOB;
6303 it->position = it->current.pos;
6304 success_p = 0;
6305 }
6306 }
6307 else
6308 {
6309 handle_stop (it);
6310 return get_next_display_element (it);
6311 }
6312 }
6313 else
6314 {
6315 /* No face changes, overlays etc. in sight, so just return a
6316 character from current_buffer. */
6317 unsigned char *p;
6318
6319 /* Maybe run the redisplay end trigger hook. Performance note:
6320 This doesn't seem to cost measurable time. */
6321 if (it->redisplay_end_trigger_charpos
6322 && it->glyph_row
6323 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6324 run_redisplay_end_trigger_hook (it);
6325
6326 /* Get the next character, maybe multibyte. */
6327 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6328 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6329 {
6330 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6331 - IT_BYTEPOS (*it));
6332 it->c = string_char_and_length (p, maxlen, &it->len);
6333 }
6334 else
6335 it->c = *p, it->len = 1;
6336
6337 /* Record what we have and where it came from. */
6338 it->what = IT_CHARACTER;
6339 it->object = it->w->buffer;
6340 it->position = it->current.pos;
6341
6342 /* Normally we return the character found above, except when we
6343 really want to return an ellipsis for selective display. */
6344 if (it->selective)
6345 {
6346 if (it->c == '\n')
6347 {
6348 /* A value of selective > 0 means hide lines indented more
6349 than that number of columns. */
6350 if (it->selective > 0
6351 && IT_CHARPOS (*it) + 1 < ZV
6352 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6353 IT_BYTEPOS (*it) + 1,
6354 (double) it->selective)) /* iftc */
6355 {
6356 success_p = next_element_from_ellipsis (it);
6357 it->dpvec_char_len = -1;
6358 }
6359 }
6360 else if (it->c == '\r' && it->selective == -1)
6361 {
6362 /* A value of selective == -1 means that everything from the
6363 CR to the end of the line is invisible, with maybe an
6364 ellipsis displayed for it. */
6365 success_p = next_element_from_ellipsis (it);
6366 it->dpvec_char_len = -1;
6367 }
6368 }
6369 }
6370
6371 /* Value is zero if end of buffer reached. */
6372 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6373 return success_p;
6374 }
6375
6376
6377 /* Run the redisplay end trigger hook for IT. */
6378
6379 static void
6380 run_redisplay_end_trigger_hook (it)
6381 struct it *it;
6382 {
6383 Lisp_Object args[3];
6384
6385 /* IT->glyph_row should be non-null, i.e. we should be actually
6386 displaying something, or otherwise we should not run the hook. */
6387 xassert (it->glyph_row);
6388
6389 /* Set up hook arguments. */
6390 args[0] = Qredisplay_end_trigger_functions;
6391 args[1] = it->window;
6392 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6393 it->redisplay_end_trigger_charpos = 0;
6394
6395 /* Since we are *trying* to run these functions, don't try to run
6396 them again, even if they get an error. */
6397 it->w->redisplay_end_trigger = Qnil;
6398 Frun_hook_with_args (3, args);
6399
6400 /* Notice if it changed the face of the character we are on. */
6401 handle_face_prop (it);
6402 }
6403
6404
6405 /* Deliver a composition display element. The iterator IT is already
6406 filled with composition information (done in
6407 handle_composition_prop). Value is always 1. */
6408
6409 static int
6410 next_element_from_composition (it)
6411 struct it *it;
6412 {
6413 it->what = IT_COMPOSITION;
6414 it->position = (STRINGP (it->string)
6415 ? it->current.string_pos
6416 : it->current.pos);
6417 if (STRINGP (it->string))
6418 it->object = it->string;
6419 else
6420 it->object = it->w->buffer;
6421 return 1;
6422 }
6423
6424
6425 \f
6426 /***********************************************************************
6427 Moving an iterator without producing glyphs
6428 ***********************************************************************/
6429
6430 /* Check if iterator is at a position corresponding to a valid buffer
6431 position after some move_it_ call. */
6432
6433 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6434 ((it)->method == GET_FROM_STRING \
6435 ? IT_STRING_CHARPOS (*it) == 0 \
6436 : 1)
6437
6438
6439 /* Move iterator IT to a specified buffer or X position within one
6440 line on the display without producing glyphs.
6441
6442 OP should be a bit mask including some or all of these bits:
6443 MOVE_TO_X: Stop on reaching x-position TO_X.
6444 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6445 Regardless of OP's value, stop in reaching the end of the display line.
6446
6447 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6448 This means, in particular, that TO_X includes window's horizontal
6449 scroll amount.
6450
6451 The return value has several possible values that
6452 say what condition caused the scan to stop:
6453
6454 MOVE_POS_MATCH_OR_ZV
6455 - when TO_POS or ZV was reached.
6456
6457 MOVE_X_REACHED
6458 -when TO_X was reached before TO_POS or ZV were reached.
6459
6460 MOVE_LINE_CONTINUED
6461 - when we reached the end of the display area and the line must
6462 be continued.
6463
6464 MOVE_LINE_TRUNCATED
6465 - when we reached the end of the display area and the line is
6466 truncated.
6467
6468 MOVE_NEWLINE_OR_CR
6469 - when we stopped at a line end, i.e. a newline or a CR and selective
6470 display is on. */
6471
6472 static enum move_it_result
6473 move_it_in_display_line_to (it, to_charpos, to_x, op)
6474 struct it *it;
6475 int to_charpos, to_x, op;
6476 {
6477 enum move_it_result result = MOVE_UNDEFINED;
6478 struct glyph_row *saved_glyph_row;
6479
6480 /* Don't produce glyphs in produce_glyphs. */
6481 saved_glyph_row = it->glyph_row;
6482 it->glyph_row = NULL;
6483
6484 #define BUFFER_POS_REACHED_P() \
6485 ((op & MOVE_TO_POS) != 0 \
6486 && BUFFERP (it->object) \
6487 && IT_CHARPOS (*it) >= to_charpos \
6488 && (it->method == GET_FROM_BUFFER \
6489 || (it->method == GET_FROM_DISPLAY_VECTOR \
6490 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6491
6492
6493 while (1)
6494 {
6495 int x, i, ascent = 0, descent = 0;
6496
6497 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6498 if ((op & MOVE_TO_POS) != 0
6499 && BUFFERP (it->object)
6500 && it->method == GET_FROM_BUFFER
6501 && IT_CHARPOS (*it) > to_charpos)
6502 {
6503 result = MOVE_POS_MATCH_OR_ZV;
6504 break;
6505 }
6506
6507 /* Stop when ZV reached.
6508 We used to stop here when TO_CHARPOS reached as well, but that is
6509 too soon if this glyph does not fit on this line. So we handle it
6510 explicitly below. */
6511 if (!get_next_display_element (it)
6512 || (it->truncate_lines_p
6513 && BUFFER_POS_REACHED_P ()))
6514 {
6515 result = MOVE_POS_MATCH_OR_ZV;
6516 break;
6517 }
6518
6519 /* The call to produce_glyphs will get the metrics of the
6520 display element IT is loaded with. We record in x the
6521 x-position before this display element in case it does not
6522 fit on the line. */
6523 x = it->current_x;
6524
6525 /* Remember the line height so far in case the next element doesn't
6526 fit on the line. */
6527 if (!it->truncate_lines_p)
6528 {
6529 ascent = it->max_ascent;
6530 descent = it->max_descent;
6531 }
6532
6533 PRODUCE_GLYPHS (it);
6534
6535 if (it->area != TEXT_AREA)
6536 {
6537 set_iterator_to_next (it, 1);
6538 continue;
6539 }
6540
6541 /* The number of glyphs we get back in IT->nglyphs will normally
6542 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6543 character on a terminal frame, or (iii) a line end. For the
6544 second case, IT->nglyphs - 1 padding glyphs will be present
6545 (on X frames, there is only one glyph produced for a
6546 composite character.
6547
6548 The behavior implemented below means, for continuation lines,
6549 that as many spaces of a TAB as fit on the current line are
6550 displayed there. For terminal frames, as many glyphs of a
6551 multi-glyph character are displayed in the current line, too.
6552 This is what the old redisplay code did, and we keep it that
6553 way. Under X, the whole shape of a complex character must
6554 fit on the line or it will be completely displayed in the
6555 next line.
6556
6557 Note that both for tabs and padding glyphs, all glyphs have
6558 the same width. */
6559 if (it->nglyphs)
6560 {
6561 /* More than one glyph or glyph doesn't fit on line. All
6562 glyphs have the same width. */
6563 int single_glyph_width = it->pixel_width / it->nglyphs;
6564 int new_x;
6565 int x_before_this_char = x;
6566 int hpos_before_this_char = it->hpos;
6567
6568 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6569 {
6570 new_x = x + single_glyph_width;
6571
6572 /* We want to leave anything reaching TO_X to the caller. */
6573 if ((op & MOVE_TO_X) && new_x > to_x)
6574 {
6575 if (BUFFER_POS_REACHED_P ())
6576 goto buffer_pos_reached;
6577 it->current_x = x;
6578 result = MOVE_X_REACHED;
6579 break;
6580 }
6581 else if (/* Lines are continued. */
6582 !it->truncate_lines_p
6583 && (/* And glyph doesn't fit on the line. */
6584 new_x > it->last_visible_x
6585 /* Or it fits exactly and we're on a window
6586 system frame. */
6587 || (new_x == it->last_visible_x
6588 && FRAME_WINDOW_P (it->f))))
6589 {
6590 if (/* IT->hpos == 0 means the very first glyph
6591 doesn't fit on the line, e.g. a wide image. */
6592 it->hpos == 0
6593 || (new_x == it->last_visible_x
6594 && FRAME_WINDOW_P (it->f)))
6595 {
6596 ++it->hpos;
6597 it->current_x = new_x;
6598
6599 /* The character's last glyph just barely fits
6600 in this row. */
6601 if (i == it->nglyphs - 1)
6602 {
6603 /* If this is the destination position,
6604 return a position *before* it in this row,
6605 now that we know it fits in this row. */
6606 if (BUFFER_POS_REACHED_P ())
6607 {
6608 it->hpos = hpos_before_this_char;
6609 it->current_x = x_before_this_char;
6610 result = MOVE_POS_MATCH_OR_ZV;
6611 break;
6612 }
6613
6614 set_iterator_to_next (it, 1);
6615 #ifdef HAVE_WINDOW_SYSTEM
6616 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6617 {
6618 if (!get_next_display_element (it))
6619 {
6620 result = MOVE_POS_MATCH_OR_ZV;
6621 break;
6622 }
6623 if (BUFFER_POS_REACHED_P ())
6624 {
6625 if (ITERATOR_AT_END_OF_LINE_P (it))
6626 result = MOVE_POS_MATCH_OR_ZV;
6627 else
6628 result = MOVE_LINE_CONTINUED;
6629 break;
6630 }
6631 if (ITERATOR_AT_END_OF_LINE_P (it))
6632 {
6633 result = MOVE_NEWLINE_OR_CR;
6634 break;
6635 }
6636 }
6637 #endif /* HAVE_WINDOW_SYSTEM */
6638 }
6639 }
6640 else
6641 {
6642 it->current_x = x;
6643 it->max_ascent = ascent;
6644 it->max_descent = descent;
6645 }
6646
6647 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6648 IT_CHARPOS (*it)));
6649 result = MOVE_LINE_CONTINUED;
6650 break;
6651 }
6652 else if (BUFFER_POS_REACHED_P ())
6653 goto buffer_pos_reached;
6654 else if (new_x > it->first_visible_x)
6655 {
6656 /* Glyph is visible. Increment number of glyphs that
6657 would be displayed. */
6658 ++it->hpos;
6659 }
6660 else
6661 {
6662 /* Glyph is completely off the left margin of the display
6663 area. Nothing to do. */
6664 }
6665 }
6666
6667 if (result != MOVE_UNDEFINED)
6668 break;
6669 }
6670 else if (BUFFER_POS_REACHED_P ())
6671 {
6672 buffer_pos_reached:
6673 it->current_x = x;
6674 it->max_ascent = ascent;
6675 it->max_descent = descent;
6676 result = MOVE_POS_MATCH_OR_ZV;
6677 break;
6678 }
6679 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6680 {
6681 /* Stop when TO_X specified and reached. This check is
6682 necessary here because of lines consisting of a line end,
6683 only. The line end will not produce any glyphs and we
6684 would never get MOVE_X_REACHED. */
6685 xassert (it->nglyphs == 0);
6686 result = MOVE_X_REACHED;
6687 break;
6688 }
6689
6690 /* Is this a line end? If yes, we're done. */
6691 if (ITERATOR_AT_END_OF_LINE_P (it))
6692 {
6693 result = MOVE_NEWLINE_OR_CR;
6694 break;
6695 }
6696
6697 /* The current display element has been consumed. Advance
6698 to the next. */
6699 set_iterator_to_next (it, 1);
6700
6701 /* Stop if lines are truncated and IT's current x-position is
6702 past the right edge of the window now. */
6703 if (it->truncate_lines_p
6704 && it->current_x >= it->last_visible_x)
6705 {
6706 #ifdef HAVE_WINDOW_SYSTEM
6707 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6708 {
6709 if (!get_next_display_element (it)
6710 || BUFFER_POS_REACHED_P ())
6711 {
6712 result = MOVE_POS_MATCH_OR_ZV;
6713 break;
6714 }
6715 if (ITERATOR_AT_END_OF_LINE_P (it))
6716 {
6717 result = MOVE_NEWLINE_OR_CR;
6718 break;
6719 }
6720 }
6721 #endif /* HAVE_WINDOW_SYSTEM */
6722 result = MOVE_LINE_TRUNCATED;
6723 break;
6724 }
6725 }
6726
6727 #undef BUFFER_POS_REACHED_P
6728
6729 /* Restore the iterator settings altered at the beginning of this
6730 function. */
6731 it->glyph_row = saved_glyph_row;
6732 return result;
6733 }
6734
6735
6736 /* Move IT forward until it satisfies one or more of the criteria in
6737 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6738
6739 OP is a bit-mask that specifies where to stop, and in particular,
6740 which of those four position arguments makes a difference. See the
6741 description of enum move_operation_enum.
6742
6743 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6744 screen line, this function will set IT to the next position >
6745 TO_CHARPOS. */
6746
6747 void
6748 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6749 struct it *it;
6750 int to_charpos, to_x, to_y, to_vpos;
6751 int op;
6752 {
6753 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6754 int line_height;
6755 int reached = 0;
6756
6757 for (;;)
6758 {
6759 if (op & MOVE_TO_VPOS)
6760 {
6761 /* If no TO_CHARPOS and no TO_X specified, stop at the
6762 start of the line TO_VPOS. */
6763 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6764 {
6765 if (it->vpos == to_vpos)
6766 {
6767 reached = 1;
6768 break;
6769 }
6770 else
6771 skip = move_it_in_display_line_to (it, -1, -1, 0);
6772 }
6773 else
6774 {
6775 /* TO_VPOS >= 0 means stop at TO_X in the line at
6776 TO_VPOS, or at TO_POS, whichever comes first. */
6777 if (it->vpos == to_vpos)
6778 {
6779 reached = 2;
6780 break;
6781 }
6782
6783 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6784
6785 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6786 {
6787 reached = 3;
6788 break;
6789 }
6790 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6791 {
6792 /* We have reached TO_X but not in the line we want. */
6793 skip = move_it_in_display_line_to (it, to_charpos,
6794 -1, MOVE_TO_POS);
6795 if (skip == MOVE_POS_MATCH_OR_ZV)
6796 {
6797 reached = 4;
6798 break;
6799 }
6800 }
6801 }
6802 }
6803 else if (op & MOVE_TO_Y)
6804 {
6805 struct it it_backup;
6806
6807 /* TO_Y specified means stop at TO_X in the line containing
6808 TO_Y---or at TO_CHARPOS if this is reached first. The
6809 problem is that we can't really tell whether the line
6810 contains TO_Y before we have completely scanned it, and
6811 this may skip past TO_X. What we do is to first scan to
6812 TO_X.
6813
6814 If TO_X is not specified, use a TO_X of zero. The reason
6815 is to make the outcome of this function more predictable.
6816 If we didn't use TO_X == 0, we would stop at the end of
6817 the line which is probably not what a caller would expect
6818 to happen. */
6819 skip = move_it_in_display_line_to (it, to_charpos,
6820 ((op & MOVE_TO_X)
6821 ? to_x : 0),
6822 (MOVE_TO_X
6823 | (op & MOVE_TO_POS)));
6824
6825 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6826 if (skip == MOVE_POS_MATCH_OR_ZV)
6827 {
6828 reached = 5;
6829 break;
6830 }
6831
6832 /* If TO_X was reached, we would like to know whether TO_Y
6833 is in the line. This can only be said if we know the
6834 total line height which requires us to scan the rest of
6835 the line. */
6836 if (skip == MOVE_X_REACHED)
6837 {
6838 it_backup = *it;
6839 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6840 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6841 op & MOVE_TO_POS);
6842 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6843 }
6844
6845 /* Now, decide whether TO_Y is in this line. */
6846 line_height = it->max_ascent + it->max_descent;
6847 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6848
6849 if (to_y >= it->current_y
6850 && to_y < it->current_y + line_height)
6851 {
6852 if (skip == MOVE_X_REACHED)
6853 /* If TO_Y is in this line and TO_X was reached above,
6854 we scanned too far. We have to restore IT's settings
6855 to the ones before skipping. */
6856 *it = it_backup;
6857 reached = 6;
6858 }
6859 else if (skip == MOVE_X_REACHED)
6860 {
6861 skip = skip2;
6862 if (skip == MOVE_POS_MATCH_OR_ZV)
6863 reached = 7;
6864 }
6865
6866 if (reached)
6867 break;
6868 }
6869 else if (BUFFERP (it->object)
6870 && it->method == GET_FROM_BUFFER
6871 && IT_CHARPOS (*it) >= to_charpos)
6872 skip = MOVE_POS_MATCH_OR_ZV;
6873 else
6874 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6875
6876 switch (skip)
6877 {
6878 case MOVE_POS_MATCH_OR_ZV:
6879 reached = 8;
6880 goto out;
6881
6882 case MOVE_NEWLINE_OR_CR:
6883 set_iterator_to_next (it, 1);
6884 it->continuation_lines_width = 0;
6885 break;
6886
6887 case MOVE_LINE_TRUNCATED:
6888 it->continuation_lines_width = 0;
6889 reseat_at_next_visible_line_start (it, 0);
6890 if ((op & MOVE_TO_POS) != 0
6891 && IT_CHARPOS (*it) > to_charpos)
6892 {
6893 reached = 9;
6894 goto out;
6895 }
6896 break;
6897
6898 case MOVE_LINE_CONTINUED:
6899 /* For continued lines ending in a tab, some of the glyphs
6900 associated with the tab are displayed on the current
6901 line. Since it->current_x does not include these glyphs,
6902 we use it->last_visible_x instead. */
6903 it->continuation_lines_width +=
6904 (it->c == '\t') ? it->last_visible_x : it->current_x;
6905 break;
6906
6907 default:
6908 abort ();
6909 }
6910
6911 /* Reset/increment for the next run. */
6912 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6913 it->current_x = it->hpos = 0;
6914 it->current_y += it->max_ascent + it->max_descent;
6915 ++it->vpos;
6916 last_height = it->max_ascent + it->max_descent;
6917 last_max_ascent = it->max_ascent;
6918 it->max_ascent = it->max_descent = 0;
6919 }
6920
6921 out:
6922
6923 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6924 }
6925
6926
6927 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6928
6929 If DY > 0, move IT backward at least that many pixels. DY = 0
6930 means move IT backward to the preceding line start or BEGV. This
6931 function may move over more than DY pixels if IT->current_y - DY
6932 ends up in the middle of a line; in this case IT->current_y will be
6933 set to the top of the line moved to. */
6934
6935 void
6936 move_it_vertically_backward (it, dy)
6937 struct it *it;
6938 int dy;
6939 {
6940 int nlines, h;
6941 struct it it2, it3;
6942 int start_pos;
6943
6944 move_further_back:
6945 xassert (dy >= 0);
6946
6947 start_pos = IT_CHARPOS (*it);
6948
6949 /* Estimate how many newlines we must move back. */
6950 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6951
6952 /* Set the iterator's position that many lines back. */
6953 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6954 back_to_previous_visible_line_start (it);
6955
6956 /* Reseat the iterator here. When moving backward, we don't want
6957 reseat to skip forward over invisible text, set up the iterator
6958 to deliver from overlay strings at the new position etc. So,
6959 use reseat_1 here. */
6960 reseat_1 (it, it->current.pos, 1);
6961
6962 /* We are now surely at a line start. */
6963 it->current_x = it->hpos = 0;
6964 it->continuation_lines_width = 0;
6965
6966 /* Move forward and see what y-distance we moved. First move to the
6967 start of the next line so that we get its height. We need this
6968 height to be able to tell whether we reached the specified
6969 y-distance. */
6970 it2 = *it;
6971 it2.max_ascent = it2.max_descent = 0;
6972 do
6973 {
6974 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6975 MOVE_TO_POS | MOVE_TO_VPOS);
6976 }
6977 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6978 xassert (IT_CHARPOS (*it) >= BEGV);
6979 it3 = it2;
6980
6981 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6982 xassert (IT_CHARPOS (*it) >= BEGV);
6983 /* H is the actual vertical distance from the position in *IT
6984 and the starting position. */
6985 h = it2.current_y - it->current_y;
6986 /* NLINES is the distance in number of lines. */
6987 nlines = it2.vpos - it->vpos;
6988
6989 /* Correct IT's y and vpos position
6990 so that they are relative to the starting point. */
6991 it->vpos -= nlines;
6992 it->current_y -= h;
6993
6994 if (dy == 0)
6995 {
6996 /* DY == 0 means move to the start of the screen line. The
6997 value of nlines is > 0 if continuation lines were involved. */
6998 if (nlines > 0)
6999 move_it_by_lines (it, nlines, 1);
7000 #if 0
7001 /* I think this assert is bogus if buffer contains
7002 invisible text or images. KFS. */
7003 xassert (IT_CHARPOS (*it) <= start_pos);
7004 #endif
7005 }
7006 else
7007 {
7008 /* The y-position we try to reach, relative to *IT.
7009 Note that H has been subtracted in front of the if-statement. */
7010 int target_y = it->current_y + h - dy;
7011 int y0 = it3.current_y;
7012 int y1 = line_bottom_y (&it3);
7013 int line_height = y1 - y0;
7014
7015 /* If we did not reach target_y, try to move further backward if
7016 we can. If we moved too far backward, try to move forward. */
7017 if (target_y < it->current_y
7018 /* This is heuristic. In a window that's 3 lines high, with
7019 a line height of 13 pixels each, recentering with point
7020 on the bottom line will try to move -39/2 = 19 pixels
7021 backward. Try to avoid moving into the first line. */
7022 && (it->current_y - target_y
7023 > min (window_box_height (it->w), line_height * 2 / 3))
7024 && IT_CHARPOS (*it) > BEGV)
7025 {
7026 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7027 target_y - it->current_y));
7028 dy = it->current_y - target_y;
7029 goto move_further_back;
7030 }
7031 else if (target_y >= it->current_y + line_height
7032 && IT_CHARPOS (*it) < ZV)
7033 {
7034 /* Should move forward by at least one line, maybe more.
7035
7036 Note: Calling move_it_by_lines can be expensive on
7037 terminal frames, where compute_motion is used (via
7038 vmotion) to do the job, when there are very long lines
7039 and truncate-lines is nil. That's the reason for
7040 treating terminal frames specially here. */
7041
7042 if (!FRAME_WINDOW_P (it->f))
7043 move_it_vertically (it, target_y - (it->current_y + line_height));
7044 else
7045 {
7046 do
7047 {
7048 move_it_by_lines (it, 1, 1);
7049 }
7050 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7051 }
7052
7053 #if 0
7054 /* I think this assert is bogus if buffer contains
7055 invisible text or images. KFS. */
7056 xassert (IT_CHARPOS (*it) >= BEGV);
7057 #endif
7058 }
7059 }
7060 }
7061
7062
7063 /* Move IT by a specified amount of pixel lines DY. DY negative means
7064 move backwards. DY = 0 means move to start of screen line. At the
7065 end, IT will be on the start of a screen line. */
7066
7067 void
7068 move_it_vertically (it, dy)
7069 struct it *it;
7070 int dy;
7071 {
7072 if (dy <= 0)
7073 move_it_vertically_backward (it, -dy);
7074 else
7075 {
7076 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7077 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7078 MOVE_TO_POS | MOVE_TO_Y);
7079 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7080
7081 /* If buffer ends in ZV without a newline, move to the start of
7082 the line to satisfy the post-condition. */
7083 if (IT_CHARPOS (*it) == ZV
7084 && ZV > BEGV
7085 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7086 move_it_by_lines (it, 0, 0);
7087 }
7088 }
7089
7090
7091 /* Move iterator IT past the end of the text line it is in. */
7092
7093 void
7094 move_it_past_eol (it)
7095 struct it *it;
7096 {
7097 enum move_it_result rc;
7098
7099 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7100 if (rc == MOVE_NEWLINE_OR_CR)
7101 set_iterator_to_next (it, 0);
7102 }
7103
7104
7105 #if 0 /* Currently not used. */
7106
7107 /* Return non-zero if some text between buffer positions START_CHARPOS
7108 and END_CHARPOS is invisible. IT->window is the window for text
7109 property lookup. */
7110
7111 static int
7112 invisible_text_between_p (it, start_charpos, end_charpos)
7113 struct it *it;
7114 int start_charpos, end_charpos;
7115 {
7116 Lisp_Object prop, limit;
7117 int invisible_found_p;
7118
7119 xassert (it != NULL && start_charpos <= end_charpos);
7120
7121 /* Is text at START invisible? */
7122 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7123 it->window);
7124 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7125 invisible_found_p = 1;
7126 else
7127 {
7128 limit = Fnext_single_char_property_change (make_number (start_charpos),
7129 Qinvisible, Qnil,
7130 make_number (end_charpos));
7131 invisible_found_p = XFASTINT (limit) < end_charpos;
7132 }
7133
7134 return invisible_found_p;
7135 }
7136
7137 #endif /* 0 */
7138
7139
7140 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7141 negative means move up. DVPOS == 0 means move to the start of the
7142 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7143 NEED_Y_P is zero, IT->current_y will be left unchanged.
7144
7145 Further optimization ideas: If we would know that IT->f doesn't use
7146 a face with proportional font, we could be faster for
7147 truncate-lines nil. */
7148
7149 void
7150 move_it_by_lines (it, dvpos, need_y_p)
7151 struct it *it;
7152 int dvpos, need_y_p;
7153 {
7154 struct position pos;
7155
7156 /* The commented-out optimization uses vmotion on terminals. This
7157 gives bad results, because elements like it->what, on which
7158 callers such as pos_visible_p rely, aren't updated. */
7159 /* if (!FRAME_WINDOW_P (it->f))
7160 {
7161 struct text_pos textpos;
7162
7163 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7164 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7165 reseat (it, textpos, 1);
7166 it->vpos += pos.vpos;
7167 it->current_y += pos.vpos;
7168 }
7169 else */
7170
7171 if (dvpos == 0)
7172 {
7173 /* DVPOS == 0 means move to the start of the screen line. */
7174 move_it_vertically_backward (it, 0);
7175 xassert (it->current_x == 0 && it->hpos == 0);
7176 /* Let next call to line_bottom_y calculate real line height */
7177 last_height = 0;
7178 }
7179 else if (dvpos > 0)
7180 {
7181 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7182 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7183 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7184 }
7185 else
7186 {
7187 struct it it2;
7188 int start_charpos, i;
7189
7190 /* Start at the beginning of the screen line containing IT's
7191 position. This may actually move vertically backwards,
7192 in case of overlays, so adjust dvpos accordingly. */
7193 dvpos += it->vpos;
7194 move_it_vertically_backward (it, 0);
7195 dvpos -= it->vpos;
7196
7197 /* Go back -DVPOS visible lines and reseat the iterator there. */
7198 start_charpos = IT_CHARPOS (*it);
7199 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7200 back_to_previous_visible_line_start (it);
7201 reseat (it, it->current.pos, 1);
7202
7203 /* Move further back if we end up in a string or an image. */
7204 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7205 {
7206 /* First try to move to start of display line. */
7207 dvpos += it->vpos;
7208 move_it_vertically_backward (it, 0);
7209 dvpos -= it->vpos;
7210 if (IT_POS_VALID_AFTER_MOVE_P (it))
7211 break;
7212 /* If start of line is still in string or image,
7213 move further back. */
7214 back_to_previous_visible_line_start (it);
7215 reseat (it, it->current.pos, 1);
7216 dvpos--;
7217 }
7218
7219 it->current_x = it->hpos = 0;
7220
7221 /* Above call may have moved too far if continuation lines
7222 are involved. Scan forward and see if it did. */
7223 it2 = *it;
7224 it2.vpos = it2.current_y = 0;
7225 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7226 it->vpos -= it2.vpos;
7227 it->current_y -= it2.current_y;
7228 it->current_x = it->hpos = 0;
7229
7230 /* If we moved too far back, move IT some lines forward. */
7231 if (it2.vpos > -dvpos)
7232 {
7233 int delta = it2.vpos + dvpos;
7234 it2 = *it;
7235 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7236 /* Move back again if we got too far ahead. */
7237 if (IT_CHARPOS (*it) >= start_charpos)
7238 *it = it2;
7239 }
7240 }
7241 }
7242
7243 /* Return 1 if IT points into the middle of a display vector. */
7244
7245 int
7246 in_display_vector_p (it)
7247 struct it *it;
7248 {
7249 return (it->method == GET_FROM_DISPLAY_VECTOR
7250 && it->current.dpvec_index > 0
7251 && it->dpvec + it->current.dpvec_index != it->dpend);
7252 }
7253
7254 \f
7255 /***********************************************************************
7256 Messages
7257 ***********************************************************************/
7258
7259
7260 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7261 to *Messages*. */
7262
7263 void
7264 add_to_log (format, arg1, arg2)
7265 char *format;
7266 Lisp_Object arg1, arg2;
7267 {
7268 Lisp_Object args[3];
7269 Lisp_Object msg, fmt;
7270 char *buffer;
7271 int len;
7272 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7273 USE_SAFE_ALLOCA;
7274
7275 /* Do nothing if called asynchronously. Inserting text into
7276 a buffer may call after-change-functions and alike and
7277 that would means running Lisp asynchronously. */
7278 if (handling_signal)
7279 return;
7280
7281 fmt = msg = Qnil;
7282 GCPRO4 (fmt, msg, arg1, arg2);
7283
7284 args[0] = fmt = build_string (format);
7285 args[1] = arg1;
7286 args[2] = arg2;
7287 msg = Fformat (3, args);
7288
7289 len = SBYTES (msg) + 1;
7290 SAFE_ALLOCA (buffer, char *, len);
7291 bcopy (SDATA (msg), buffer, len);
7292
7293 message_dolog (buffer, len - 1, 1, 0);
7294 SAFE_FREE ();
7295
7296 UNGCPRO;
7297 }
7298
7299
7300 /* Output a newline in the *Messages* buffer if "needs" one. */
7301
7302 void
7303 message_log_maybe_newline ()
7304 {
7305 if (message_log_need_newline)
7306 message_dolog ("", 0, 1, 0);
7307 }
7308
7309
7310 /* Add a string M of length NBYTES to the message log, optionally
7311 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7312 nonzero, means interpret the contents of M as multibyte. This
7313 function calls low-level routines in order to bypass text property
7314 hooks, etc. which might not be safe to run.
7315
7316 This may GC (insert may run before/after change hooks),
7317 so the buffer M must NOT point to a Lisp string. */
7318
7319 void
7320 message_dolog (m, nbytes, nlflag, multibyte)
7321 const char *m;
7322 int nbytes, nlflag, multibyte;
7323 {
7324 if (!NILP (Vmemory_full))
7325 return;
7326
7327 if (!NILP (Vmessage_log_max))
7328 {
7329 struct buffer *oldbuf;
7330 Lisp_Object oldpoint, oldbegv, oldzv;
7331 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7332 int point_at_end = 0;
7333 int zv_at_end = 0;
7334 Lisp_Object old_deactivate_mark, tem;
7335 struct gcpro gcpro1;
7336
7337 old_deactivate_mark = Vdeactivate_mark;
7338 oldbuf = current_buffer;
7339 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7340 current_buffer->undo_list = Qt;
7341
7342 oldpoint = message_dolog_marker1;
7343 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7344 oldbegv = message_dolog_marker2;
7345 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7346 oldzv = message_dolog_marker3;
7347 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7348 GCPRO1 (old_deactivate_mark);
7349
7350 if (PT == Z)
7351 point_at_end = 1;
7352 if (ZV == Z)
7353 zv_at_end = 1;
7354
7355 BEGV = BEG;
7356 BEGV_BYTE = BEG_BYTE;
7357 ZV = Z;
7358 ZV_BYTE = Z_BYTE;
7359 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7360
7361 /* Insert the string--maybe converting multibyte to single byte
7362 or vice versa, so that all the text fits the buffer. */
7363 if (multibyte
7364 && NILP (current_buffer->enable_multibyte_characters))
7365 {
7366 int i, c, char_bytes;
7367 unsigned char work[1];
7368
7369 /* Convert a multibyte string to single-byte
7370 for the *Message* buffer. */
7371 for (i = 0; i < nbytes; i += char_bytes)
7372 {
7373 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7374 work[0] = (SINGLE_BYTE_CHAR_P (c)
7375 ? c
7376 : multibyte_char_to_unibyte (c, Qnil));
7377 insert_1_both (work, 1, 1, 1, 0, 0);
7378 }
7379 }
7380 else if (! multibyte
7381 && ! NILP (current_buffer->enable_multibyte_characters))
7382 {
7383 int i, c, char_bytes;
7384 unsigned char *msg = (unsigned char *) m;
7385 unsigned char str[MAX_MULTIBYTE_LENGTH];
7386 /* Convert a single-byte string to multibyte
7387 for the *Message* buffer. */
7388 for (i = 0; i < nbytes; i++)
7389 {
7390 c = unibyte_char_to_multibyte (msg[i]);
7391 char_bytes = CHAR_STRING (c, str);
7392 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7393 }
7394 }
7395 else if (nbytes)
7396 insert_1 (m, nbytes, 1, 0, 0);
7397
7398 if (nlflag)
7399 {
7400 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7401 insert_1 ("\n", 1, 1, 0, 0);
7402
7403 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7404 this_bol = PT;
7405 this_bol_byte = PT_BYTE;
7406
7407 /* See if this line duplicates the previous one.
7408 If so, combine duplicates. */
7409 if (this_bol > BEG)
7410 {
7411 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7412 prev_bol = PT;
7413 prev_bol_byte = PT_BYTE;
7414
7415 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7416 this_bol, this_bol_byte);
7417 if (dup)
7418 {
7419 del_range_both (prev_bol, prev_bol_byte,
7420 this_bol, this_bol_byte, 0);
7421 if (dup > 1)
7422 {
7423 char dupstr[40];
7424 int duplen;
7425
7426 /* If you change this format, don't forget to also
7427 change message_log_check_duplicate. */
7428 sprintf (dupstr, " [%d times]", dup);
7429 duplen = strlen (dupstr);
7430 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7431 insert_1 (dupstr, duplen, 1, 0, 1);
7432 }
7433 }
7434 }
7435
7436 /* If we have more than the desired maximum number of lines
7437 in the *Messages* buffer now, delete the oldest ones.
7438 This is safe because we don't have undo in this buffer. */
7439
7440 if (NATNUMP (Vmessage_log_max))
7441 {
7442 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7443 -XFASTINT (Vmessage_log_max) - 1, 0);
7444 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7445 }
7446 }
7447 BEGV = XMARKER (oldbegv)->charpos;
7448 BEGV_BYTE = marker_byte_position (oldbegv);
7449
7450 if (zv_at_end)
7451 {
7452 ZV = Z;
7453 ZV_BYTE = Z_BYTE;
7454 }
7455 else
7456 {
7457 ZV = XMARKER (oldzv)->charpos;
7458 ZV_BYTE = marker_byte_position (oldzv);
7459 }
7460
7461 if (point_at_end)
7462 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7463 else
7464 /* We can't do Fgoto_char (oldpoint) because it will run some
7465 Lisp code. */
7466 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7467 XMARKER (oldpoint)->bytepos);
7468
7469 UNGCPRO;
7470 unchain_marker (XMARKER (oldpoint));
7471 unchain_marker (XMARKER (oldbegv));
7472 unchain_marker (XMARKER (oldzv));
7473
7474 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7475 set_buffer_internal (oldbuf);
7476 if (NILP (tem))
7477 windows_or_buffers_changed = old_windows_or_buffers_changed;
7478 message_log_need_newline = !nlflag;
7479 Vdeactivate_mark = old_deactivate_mark;
7480 }
7481 }
7482
7483
7484 /* We are at the end of the buffer after just having inserted a newline.
7485 (Note: We depend on the fact we won't be crossing the gap.)
7486 Check to see if the most recent message looks a lot like the previous one.
7487 Return 0 if different, 1 if the new one should just replace it, or a
7488 value N > 1 if we should also append " [N times]". */
7489
7490 static int
7491 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7492 int prev_bol, this_bol;
7493 int prev_bol_byte, this_bol_byte;
7494 {
7495 int i;
7496 int len = Z_BYTE - 1 - this_bol_byte;
7497 int seen_dots = 0;
7498 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7499 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7500
7501 for (i = 0; i < len; i++)
7502 {
7503 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7504 seen_dots = 1;
7505 if (p1[i] != p2[i])
7506 return seen_dots;
7507 }
7508 p1 += len;
7509 if (*p1 == '\n')
7510 return 2;
7511 if (*p1++ == ' ' && *p1++ == '[')
7512 {
7513 int n = 0;
7514 while (*p1 >= '0' && *p1 <= '9')
7515 n = n * 10 + *p1++ - '0';
7516 if (strncmp (p1, " times]\n", 8) == 0)
7517 return n+1;
7518 }
7519 return 0;
7520 }
7521 \f
7522
7523 /* Display an echo area message M with a specified length of NBYTES
7524 bytes. The string may include null characters. If M is 0, clear
7525 out any existing message, and let the mini-buffer text show
7526 through.
7527
7528 This may GC, so the buffer M must NOT point to a Lisp string. */
7529
7530 void
7531 message2 (m, nbytes, multibyte)
7532 const char *m;
7533 int nbytes;
7534 int multibyte;
7535 {
7536 /* First flush out any partial line written with print. */
7537 message_log_maybe_newline ();
7538 if (m)
7539 message_dolog (m, nbytes, 1, multibyte);
7540 message2_nolog (m, nbytes, multibyte);
7541 }
7542
7543
7544 /* The non-logging counterpart of message2. */
7545
7546 void
7547 message2_nolog (m, nbytes, multibyte)
7548 const char *m;
7549 int nbytes, multibyte;
7550 {
7551 struct frame *sf = SELECTED_FRAME ();
7552 message_enable_multibyte = multibyte;
7553
7554 if (noninteractive)
7555 {
7556 if (noninteractive_need_newline)
7557 putc ('\n', stderr);
7558 noninteractive_need_newline = 0;
7559 if (m)
7560 fwrite (m, nbytes, 1, stderr);
7561 if (cursor_in_echo_area == 0)
7562 fprintf (stderr, "\n");
7563 fflush (stderr);
7564 }
7565 /* A null message buffer means that the frame hasn't really been
7566 initialized yet. Error messages get reported properly by
7567 cmd_error, so this must be just an informative message; toss it. */
7568 else if (INTERACTIVE
7569 && sf->glyphs_initialized_p
7570 && FRAME_MESSAGE_BUF (sf))
7571 {
7572 Lisp_Object mini_window;
7573 struct frame *f;
7574
7575 /* Get the frame containing the mini-buffer
7576 that the selected frame is using. */
7577 mini_window = FRAME_MINIBUF_WINDOW (sf);
7578 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7579
7580 FRAME_SAMPLE_VISIBILITY (f);
7581 if (FRAME_VISIBLE_P (sf)
7582 && ! FRAME_VISIBLE_P (f))
7583 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7584
7585 if (m)
7586 {
7587 set_message (m, Qnil, nbytes, multibyte);
7588 if (minibuffer_auto_raise)
7589 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7590 }
7591 else
7592 clear_message (1, 1);
7593
7594 do_pending_window_change (0);
7595 echo_area_display (1);
7596 do_pending_window_change (0);
7597 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7598 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7599 }
7600 }
7601
7602
7603 /* Display an echo area message M with a specified length of NBYTES
7604 bytes. The string may include null characters. If M is not a
7605 string, clear out any existing message, and let the mini-buffer
7606 text show through.
7607
7608 This function cancels echoing. */
7609
7610 void
7611 message3 (m, nbytes, multibyte)
7612 Lisp_Object m;
7613 int nbytes;
7614 int multibyte;
7615 {
7616 struct gcpro gcpro1;
7617
7618 GCPRO1 (m);
7619 clear_message (1,1);
7620 cancel_echoing ();
7621
7622 /* First flush out any partial line written with print. */
7623 message_log_maybe_newline ();
7624 if (STRINGP (m))
7625 {
7626 char *buffer;
7627 USE_SAFE_ALLOCA;
7628
7629 SAFE_ALLOCA (buffer, char *, nbytes);
7630 bcopy (SDATA (m), buffer, nbytes);
7631 message_dolog (buffer, nbytes, 1, multibyte);
7632 SAFE_FREE ();
7633 }
7634 message3_nolog (m, nbytes, multibyte);
7635
7636 UNGCPRO;
7637 }
7638
7639
7640 /* The non-logging version of message3.
7641 This does not cancel echoing, because it is used for echoing.
7642 Perhaps we need to make a separate function for echoing
7643 and make this cancel echoing. */
7644
7645 void
7646 message3_nolog (m, nbytes, multibyte)
7647 Lisp_Object m;
7648 int nbytes, multibyte;
7649 {
7650 struct frame *sf = SELECTED_FRAME ();
7651 message_enable_multibyte = multibyte;
7652
7653 if (noninteractive)
7654 {
7655 if (noninteractive_need_newline)
7656 putc ('\n', stderr);
7657 noninteractive_need_newline = 0;
7658 if (STRINGP (m))
7659 fwrite (SDATA (m), nbytes, 1, stderr);
7660 if (cursor_in_echo_area == 0)
7661 fprintf (stderr, "\n");
7662 fflush (stderr);
7663 }
7664 /* A null message buffer means that the frame hasn't really been
7665 initialized yet. Error messages get reported properly by
7666 cmd_error, so this must be just an informative message; toss it. */
7667 else if (INTERACTIVE
7668 && sf->glyphs_initialized_p
7669 && FRAME_MESSAGE_BUF (sf))
7670 {
7671 Lisp_Object mini_window;
7672 Lisp_Object frame;
7673 struct frame *f;
7674
7675 /* Get the frame containing the mini-buffer
7676 that the selected frame is using. */
7677 mini_window = FRAME_MINIBUF_WINDOW (sf);
7678 frame = XWINDOW (mini_window)->frame;
7679 f = XFRAME (frame);
7680
7681 FRAME_SAMPLE_VISIBILITY (f);
7682 if (FRAME_VISIBLE_P (sf)
7683 && !FRAME_VISIBLE_P (f))
7684 Fmake_frame_visible (frame);
7685
7686 if (STRINGP (m) && SCHARS (m) > 0)
7687 {
7688 set_message (NULL, m, nbytes, multibyte);
7689 if (minibuffer_auto_raise)
7690 Fraise_frame (frame);
7691 /* Assume we are not echoing.
7692 (If we are, echo_now will override this.) */
7693 echo_message_buffer = Qnil;
7694 }
7695 else
7696 clear_message (1, 1);
7697
7698 do_pending_window_change (0);
7699 echo_area_display (1);
7700 do_pending_window_change (0);
7701 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7702 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7703 }
7704 }
7705
7706
7707 /* Display a null-terminated echo area message M. If M is 0, clear
7708 out any existing message, and let the mini-buffer text show through.
7709
7710 The buffer M must continue to exist until after the echo area gets
7711 cleared or some other message gets displayed there. Do not pass
7712 text that is stored in a Lisp string. Do not pass text in a buffer
7713 that was alloca'd. */
7714
7715 void
7716 message1 (m)
7717 char *m;
7718 {
7719 message2 (m, (m ? strlen (m) : 0), 0);
7720 }
7721
7722
7723 /* The non-logging counterpart of message1. */
7724
7725 void
7726 message1_nolog (m)
7727 char *m;
7728 {
7729 message2_nolog (m, (m ? strlen (m) : 0), 0);
7730 }
7731
7732 /* Display a message M which contains a single %s
7733 which gets replaced with STRING. */
7734
7735 void
7736 message_with_string (m, string, log)
7737 char *m;
7738 Lisp_Object string;
7739 int log;
7740 {
7741 CHECK_STRING (string);
7742
7743 if (noninteractive)
7744 {
7745 if (m)
7746 {
7747 if (noninteractive_need_newline)
7748 putc ('\n', stderr);
7749 noninteractive_need_newline = 0;
7750 fprintf (stderr, m, SDATA (string));
7751 if (cursor_in_echo_area == 0)
7752 fprintf (stderr, "\n");
7753 fflush (stderr);
7754 }
7755 }
7756 else if (INTERACTIVE)
7757 {
7758 /* The frame whose minibuffer we're going to display the message on.
7759 It may be larger than the selected frame, so we need
7760 to use its buffer, not the selected frame's buffer. */
7761 Lisp_Object mini_window;
7762 struct frame *f, *sf = SELECTED_FRAME ();
7763
7764 /* Get the frame containing the minibuffer
7765 that the selected frame is using. */
7766 mini_window = FRAME_MINIBUF_WINDOW (sf);
7767 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7768
7769 /* A null message buffer means that the frame hasn't really been
7770 initialized yet. Error messages get reported properly by
7771 cmd_error, so this must be just an informative message; toss it. */
7772 if (FRAME_MESSAGE_BUF (f))
7773 {
7774 Lisp_Object args[2], message;
7775 struct gcpro gcpro1, gcpro2;
7776
7777 args[0] = build_string (m);
7778 args[1] = message = string;
7779 GCPRO2 (args[0], message);
7780 gcpro1.nvars = 2;
7781
7782 message = Fformat (2, args);
7783
7784 if (log)
7785 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7786 else
7787 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7788
7789 UNGCPRO;
7790
7791 /* Print should start at the beginning of the message
7792 buffer next time. */
7793 message_buf_print = 0;
7794 }
7795 }
7796 }
7797
7798
7799 /* Dump an informative message to the minibuf. If M is 0, clear out
7800 any existing message, and let the mini-buffer text show through. */
7801
7802 /* VARARGS 1 */
7803 void
7804 message (m, a1, a2, a3)
7805 char *m;
7806 EMACS_INT a1, a2, a3;
7807 {
7808 if (noninteractive)
7809 {
7810 if (m)
7811 {
7812 if (noninteractive_need_newline)
7813 putc ('\n', stderr);
7814 noninteractive_need_newline = 0;
7815 fprintf (stderr, m, a1, a2, a3);
7816 if (cursor_in_echo_area == 0)
7817 fprintf (stderr, "\n");
7818 fflush (stderr);
7819 }
7820 }
7821 else if (INTERACTIVE)
7822 {
7823 /* The frame whose mini-buffer we're going to display the message
7824 on. It may be larger than the selected frame, so we need to
7825 use its buffer, not the selected frame's buffer. */
7826 Lisp_Object mini_window;
7827 struct frame *f, *sf = SELECTED_FRAME ();
7828
7829 /* Get the frame containing the mini-buffer
7830 that the selected frame is using. */
7831 mini_window = FRAME_MINIBUF_WINDOW (sf);
7832 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7833
7834 /* A null message buffer means that the frame hasn't really been
7835 initialized yet. Error messages get reported properly by
7836 cmd_error, so this must be just an informative message; toss
7837 it. */
7838 if (FRAME_MESSAGE_BUF (f))
7839 {
7840 if (m)
7841 {
7842 int len;
7843 #ifdef NO_ARG_ARRAY
7844 char *a[3];
7845 a[0] = (char *) a1;
7846 a[1] = (char *) a2;
7847 a[2] = (char *) a3;
7848
7849 len = doprnt (FRAME_MESSAGE_BUF (f),
7850 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7851 #else
7852 len = doprnt (FRAME_MESSAGE_BUF (f),
7853 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7854 (char **) &a1);
7855 #endif /* NO_ARG_ARRAY */
7856
7857 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7858 }
7859 else
7860 message1 (0);
7861
7862 /* Print should start at the beginning of the message
7863 buffer next time. */
7864 message_buf_print = 0;
7865 }
7866 }
7867 }
7868
7869
7870 /* The non-logging version of message. */
7871
7872 void
7873 message_nolog (m, a1, a2, a3)
7874 char *m;
7875 EMACS_INT a1, a2, a3;
7876 {
7877 Lisp_Object old_log_max;
7878 old_log_max = Vmessage_log_max;
7879 Vmessage_log_max = Qnil;
7880 message (m, a1, a2, a3);
7881 Vmessage_log_max = old_log_max;
7882 }
7883
7884
7885 /* Display the current message in the current mini-buffer. This is
7886 only called from error handlers in process.c, and is not time
7887 critical. */
7888
7889 void
7890 update_echo_area ()
7891 {
7892 if (!NILP (echo_area_buffer[0]))
7893 {
7894 Lisp_Object string;
7895 string = Fcurrent_message ();
7896 message3 (string, SBYTES (string),
7897 !NILP (current_buffer->enable_multibyte_characters));
7898 }
7899 }
7900
7901
7902 /* Make sure echo area buffers in `echo_buffers' are live.
7903 If they aren't, make new ones. */
7904
7905 static void
7906 ensure_echo_area_buffers ()
7907 {
7908 int i;
7909
7910 for (i = 0; i < 2; ++i)
7911 if (!BUFFERP (echo_buffer[i])
7912 || NILP (XBUFFER (echo_buffer[i])->name))
7913 {
7914 char name[30];
7915 Lisp_Object old_buffer;
7916 int j;
7917
7918 old_buffer = echo_buffer[i];
7919 sprintf (name, " *Echo Area %d*", i);
7920 echo_buffer[i] = Fget_buffer_create (build_string (name));
7921 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7922
7923 for (j = 0; j < 2; ++j)
7924 if (EQ (old_buffer, echo_area_buffer[j]))
7925 echo_area_buffer[j] = echo_buffer[i];
7926 }
7927 }
7928
7929
7930 /* Call FN with args A1..A4 with either the current or last displayed
7931 echo_area_buffer as current buffer.
7932
7933 WHICH zero means use the current message buffer
7934 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7935 from echo_buffer[] and clear it.
7936
7937 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7938 suitable buffer from echo_buffer[] and clear it.
7939
7940 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7941 that the current message becomes the last displayed one, make
7942 choose a suitable buffer for echo_area_buffer[0], and clear it.
7943
7944 Value is what FN returns. */
7945
7946 static int
7947 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7948 struct window *w;
7949 int which;
7950 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7951 EMACS_INT a1;
7952 Lisp_Object a2;
7953 EMACS_INT a3, a4;
7954 {
7955 Lisp_Object buffer;
7956 int this_one, the_other, clear_buffer_p, rc;
7957 int count = SPECPDL_INDEX ();
7958
7959 /* If buffers aren't live, make new ones. */
7960 ensure_echo_area_buffers ();
7961
7962 clear_buffer_p = 0;
7963
7964 if (which == 0)
7965 this_one = 0, the_other = 1;
7966 else if (which > 0)
7967 this_one = 1, the_other = 0;
7968 else
7969 {
7970 this_one = 0, the_other = 1;
7971 clear_buffer_p = 1;
7972
7973 /* We need a fresh one in case the current echo buffer equals
7974 the one containing the last displayed echo area message. */
7975 if (!NILP (echo_area_buffer[this_one])
7976 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7977 echo_area_buffer[this_one] = Qnil;
7978 }
7979
7980 /* Choose a suitable buffer from echo_buffer[] is we don't
7981 have one. */
7982 if (NILP (echo_area_buffer[this_one]))
7983 {
7984 echo_area_buffer[this_one]
7985 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7986 ? echo_buffer[the_other]
7987 : echo_buffer[this_one]);
7988 clear_buffer_p = 1;
7989 }
7990
7991 buffer = echo_area_buffer[this_one];
7992
7993 /* Don't get confused by reusing the buffer used for echoing
7994 for a different purpose. */
7995 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7996 cancel_echoing ();
7997
7998 record_unwind_protect (unwind_with_echo_area_buffer,
7999 with_echo_area_buffer_unwind_data (w));
8000
8001 /* Make the echo area buffer current. Note that for display
8002 purposes, it is not necessary that the displayed window's buffer
8003 == current_buffer, except for text property lookup. So, let's
8004 only set that buffer temporarily here without doing a full
8005 Fset_window_buffer. We must also change w->pointm, though,
8006 because otherwise an assertions in unshow_buffer fails, and Emacs
8007 aborts. */
8008 set_buffer_internal_1 (XBUFFER (buffer));
8009 if (w)
8010 {
8011 w->buffer = buffer;
8012 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8013 }
8014
8015 current_buffer->undo_list = Qt;
8016 current_buffer->read_only = Qnil;
8017 specbind (Qinhibit_read_only, Qt);
8018 specbind (Qinhibit_modification_hooks, Qt);
8019
8020 if (clear_buffer_p && Z > BEG)
8021 del_range (BEG, Z);
8022
8023 xassert (BEGV >= BEG);
8024 xassert (ZV <= Z && ZV >= BEGV);
8025
8026 rc = fn (a1, a2, a3, a4);
8027
8028 xassert (BEGV >= BEG);
8029 xassert (ZV <= Z && ZV >= BEGV);
8030
8031 unbind_to (count, Qnil);
8032 return rc;
8033 }
8034
8035
8036 /* Save state that should be preserved around the call to the function
8037 FN called in with_echo_area_buffer. */
8038
8039 static Lisp_Object
8040 with_echo_area_buffer_unwind_data (w)
8041 struct window *w;
8042 {
8043 int i = 0;
8044 Lisp_Object vector;
8045
8046 /* Reduce consing by keeping one vector in
8047 Vwith_echo_area_save_vector. */
8048 vector = Vwith_echo_area_save_vector;
8049 Vwith_echo_area_save_vector = Qnil;
8050
8051 if (NILP (vector))
8052 vector = Fmake_vector (make_number (7), Qnil);
8053
8054 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8055 AREF (vector, i) = Vdeactivate_mark, ++i;
8056 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8057
8058 if (w)
8059 {
8060 XSETWINDOW (AREF (vector, i), w); ++i;
8061 AREF (vector, i) = w->buffer; ++i;
8062 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8063 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8064 }
8065 else
8066 {
8067 int end = i + 4;
8068 for (; i < end; ++i)
8069 AREF (vector, i) = Qnil;
8070 }
8071
8072 xassert (i == ASIZE (vector));
8073 return vector;
8074 }
8075
8076
8077 /* Restore global state from VECTOR which was created by
8078 with_echo_area_buffer_unwind_data. */
8079
8080 static Lisp_Object
8081 unwind_with_echo_area_buffer (vector)
8082 Lisp_Object vector;
8083 {
8084 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8085 Vdeactivate_mark = AREF (vector, 1);
8086 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8087
8088 if (WINDOWP (AREF (vector, 3)))
8089 {
8090 struct window *w;
8091 Lisp_Object buffer, charpos, bytepos;
8092
8093 w = XWINDOW (AREF (vector, 3));
8094 buffer = AREF (vector, 4);
8095 charpos = AREF (vector, 5);
8096 bytepos = AREF (vector, 6);
8097
8098 w->buffer = buffer;
8099 set_marker_both (w->pointm, buffer,
8100 XFASTINT (charpos), XFASTINT (bytepos));
8101 }
8102
8103 Vwith_echo_area_save_vector = vector;
8104 return Qnil;
8105 }
8106
8107
8108 /* Set up the echo area for use by print functions. MULTIBYTE_P
8109 non-zero means we will print multibyte. */
8110
8111 void
8112 setup_echo_area_for_printing (multibyte_p)
8113 int multibyte_p;
8114 {
8115 /* If we can't find an echo area any more, exit. */
8116 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8117 Fkill_emacs (Qnil);
8118
8119 ensure_echo_area_buffers ();
8120
8121 if (!message_buf_print)
8122 {
8123 /* A message has been output since the last time we printed.
8124 Choose a fresh echo area buffer. */
8125 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8126 echo_area_buffer[0] = echo_buffer[1];
8127 else
8128 echo_area_buffer[0] = echo_buffer[0];
8129
8130 /* Switch to that buffer and clear it. */
8131 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8132 current_buffer->truncate_lines = Qnil;
8133
8134 if (Z > BEG)
8135 {
8136 int count = SPECPDL_INDEX ();
8137 specbind (Qinhibit_read_only, Qt);
8138 /* Note that undo recording is always disabled. */
8139 del_range (BEG, Z);
8140 unbind_to (count, Qnil);
8141 }
8142 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8143
8144 /* Set up the buffer for the multibyteness we need. */
8145 if (multibyte_p
8146 != !NILP (current_buffer->enable_multibyte_characters))
8147 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8148
8149 /* Raise the frame containing the echo area. */
8150 if (minibuffer_auto_raise)
8151 {
8152 struct frame *sf = SELECTED_FRAME ();
8153 Lisp_Object mini_window;
8154 mini_window = FRAME_MINIBUF_WINDOW (sf);
8155 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8156 }
8157
8158 message_log_maybe_newline ();
8159 message_buf_print = 1;
8160 }
8161 else
8162 {
8163 if (NILP (echo_area_buffer[0]))
8164 {
8165 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8166 echo_area_buffer[0] = echo_buffer[1];
8167 else
8168 echo_area_buffer[0] = echo_buffer[0];
8169 }
8170
8171 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8172 {
8173 /* Someone switched buffers between print requests. */
8174 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8175 current_buffer->truncate_lines = Qnil;
8176 }
8177 }
8178 }
8179
8180
8181 /* Display an echo area message in window W. Value is non-zero if W's
8182 height is changed. If display_last_displayed_message_p is
8183 non-zero, display the message that was last displayed, otherwise
8184 display the current message. */
8185
8186 static int
8187 display_echo_area (w)
8188 struct window *w;
8189 {
8190 int i, no_message_p, window_height_changed_p, count;
8191
8192 /* Temporarily disable garbage collections while displaying the echo
8193 area. This is done because a GC can print a message itself.
8194 That message would modify the echo area buffer's contents while a
8195 redisplay of the buffer is going on, and seriously confuse
8196 redisplay. */
8197 count = inhibit_garbage_collection ();
8198
8199 /* If there is no message, we must call display_echo_area_1
8200 nevertheless because it resizes the window. But we will have to
8201 reset the echo_area_buffer in question to nil at the end because
8202 with_echo_area_buffer will sets it to an empty buffer. */
8203 i = display_last_displayed_message_p ? 1 : 0;
8204 no_message_p = NILP (echo_area_buffer[i]);
8205
8206 window_height_changed_p
8207 = with_echo_area_buffer (w, display_last_displayed_message_p,
8208 display_echo_area_1,
8209 (EMACS_INT) w, Qnil, 0, 0);
8210
8211 if (no_message_p)
8212 echo_area_buffer[i] = Qnil;
8213
8214 unbind_to (count, Qnil);
8215 return window_height_changed_p;
8216 }
8217
8218
8219 /* Helper for display_echo_area. Display the current buffer which
8220 contains the current echo area message in window W, a mini-window,
8221 a pointer to which is passed in A1. A2..A4 are currently not used.
8222 Change the height of W so that all of the message is displayed.
8223 Value is non-zero if height of W was changed. */
8224
8225 static int
8226 display_echo_area_1 (a1, a2, a3, a4)
8227 EMACS_INT a1;
8228 Lisp_Object a2;
8229 EMACS_INT a3, a4;
8230 {
8231 struct window *w = (struct window *) a1;
8232 Lisp_Object window;
8233 struct text_pos start;
8234 int window_height_changed_p = 0;
8235
8236 /* Do this before displaying, so that we have a large enough glyph
8237 matrix for the display. If we can't get enough space for the
8238 whole text, display the last N lines. That works by setting w->start. */
8239 window_height_changed_p = resize_mini_window (w, 0);
8240
8241 /* Use the starting position chosen by resize_mini_window. */
8242 SET_TEXT_POS_FROM_MARKER (start, w->start);
8243
8244 /* Display. */
8245 clear_glyph_matrix (w->desired_matrix);
8246 XSETWINDOW (window, w);
8247 try_window (window, start, 0);
8248
8249 return window_height_changed_p;
8250 }
8251
8252
8253 /* Resize the echo area window to exactly the size needed for the
8254 currently displayed message, if there is one. If a mini-buffer
8255 is active, don't shrink it. */
8256
8257 void
8258 resize_echo_area_exactly ()
8259 {
8260 if (BUFFERP (echo_area_buffer[0])
8261 && WINDOWP (echo_area_window))
8262 {
8263 struct window *w = XWINDOW (echo_area_window);
8264 int resized_p;
8265 Lisp_Object resize_exactly;
8266
8267 if (minibuf_level == 0)
8268 resize_exactly = Qt;
8269 else
8270 resize_exactly = Qnil;
8271
8272 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8273 (EMACS_INT) w, resize_exactly, 0, 0);
8274 if (resized_p)
8275 {
8276 ++windows_or_buffers_changed;
8277 ++update_mode_lines;
8278 redisplay_internal (0);
8279 }
8280 }
8281 }
8282
8283
8284 /* Callback function for with_echo_area_buffer, when used from
8285 resize_echo_area_exactly. A1 contains a pointer to the window to
8286 resize, EXACTLY non-nil means resize the mini-window exactly to the
8287 size of the text displayed. A3 and A4 are not used. Value is what
8288 resize_mini_window returns. */
8289
8290 static int
8291 resize_mini_window_1 (a1, exactly, a3, a4)
8292 EMACS_INT a1;
8293 Lisp_Object exactly;
8294 EMACS_INT a3, a4;
8295 {
8296 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8297 }
8298
8299
8300 /* Resize mini-window W to fit the size of its contents. EXACT:P
8301 means size the window exactly to the size needed. Otherwise, it's
8302 only enlarged until W's buffer is empty.
8303
8304 Set W->start to the right place to begin display. If the whole
8305 contents fit, start at the beginning. Otherwise, start so as
8306 to make the end of the contents appear. This is particularly
8307 important for y-or-n-p, but seems desirable generally.
8308
8309 Value is non-zero if the window height has been changed. */
8310
8311 int
8312 resize_mini_window (w, exact_p)
8313 struct window *w;
8314 int exact_p;
8315 {
8316 struct frame *f = XFRAME (w->frame);
8317 int window_height_changed_p = 0;
8318
8319 xassert (MINI_WINDOW_P (w));
8320
8321 /* By default, start display at the beginning. */
8322 set_marker_both (w->start, w->buffer,
8323 BUF_BEGV (XBUFFER (w->buffer)),
8324 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8325
8326 /* Don't resize windows while redisplaying a window; it would
8327 confuse redisplay functions when the size of the window they are
8328 displaying changes from under them. Such a resizing can happen,
8329 for instance, when which-func prints a long message while
8330 we are running fontification-functions. We're running these
8331 functions with safe_call which binds inhibit-redisplay to t. */
8332 if (!NILP (Vinhibit_redisplay))
8333 return 0;
8334
8335 /* Nil means don't try to resize. */
8336 if (NILP (Vresize_mini_windows)
8337 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8338 return 0;
8339
8340 if (!FRAME_MINIBUF_ONLY_P (f))
8341 {
8342 struct it it;
8343 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8344 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8345 int height, max_height;
8346 int unit = FRAME_LINE_HEIGHT (f);
8347 struct text_pos start;
8348 struct buffer *old_current_buffer = NULL;
8349
8350 if (current_buffer != XBUFFER (w->buffer))
8351 {
8352 old_current_buffer = current_buffer;
8353 set_buffer_internal (XBUFFER (w->buffer));
8354 }
8355
8356 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8357
8358 /* Compute the max. number of lines specified by the user. */
8359 if (FLOATP (Vmax_mini_window_height))
8360 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8361 else if (INTEGERP (Vmax_mini_window_height))
8362 max_height = XINT (Vmax_mini_window_height);
8363 else
8364 max_height = total_height / 4;
8365
8366 /* Correct that max. height if it's bogus. */
8367 max_height = max (1, max_height);
8368 max_height = min (total_height, max_height);
8369
8370 /* Find out the height of the text in the window. */
8371 if (it.truncate_lines_p)
8372 height = 1;
8373 else
8374 {
8375 last_height = 0;
8376 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8377 if (it.max_ascent == 0 && it.max_descent == 0)
8378 height = it.current_y + last_height;
8379 else
8380 height = it.current_y + it.max_ascent + it.max_descent;
8381 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8382 height = (height + unit - 1) / unit;
8383 }
8384
8385 /* Compute a suitable window start. */
8386 if (height > max_height)
8387 {
8388 height = max_height;
8389 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8390 move_it_vertically_backward (&it, (height - 1) * unit);
8391 start = it.current.pos;
8392 }
8393 else
8394 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8395 SET_MARKER_FROM_TEXT_POS (w->start, start);
8396
8397 if (EQ (Vresize_mini_windows, Qgrow_only))
8398 {
8399 /* Let it grow only, until we display an empty message, in which
8400 case the window shrinks again. */
8401 if (height > WINDOW_TOTAL_LINES (w))
8402 {
8403 int old_height = WINDOW_TOTAL_LINES (w);
8404 freeze_window_starts (f, 1);
8405 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8406 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8407 }
8408 else if (height < WINDOW_TOTAL_LINES (w)
8409 && (exact_p || BEGV == ZV))
8410 {
8411 int old_height = WINDOW_TOTAL_LINES (w);
8412 freeze_window_starts (f, 0);
8413 shrink_mini_window (w);
8414 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8415 }
8416 }
8417 else
8418 {
8419 /* Always resize to exact size needed. */
8420 if (height > WINDOW_TOTAL_LINES (w))
8421 {
8422 int old_height = WINDOW_TOTAL_LINES (w);
8423 freeze_window_starts (f, 1);
8424 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8425 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8426 }
8427 else if (height < WINDOW_TOTAL_LINES (w))
8428 {
8429 int old_height = WINDOW_TOTAL_LINES (w);
8430 freeze_window_starts (f, 0);
8431 shrink_mini_window (w);
8432
8433 if (height)
8434 {
8435 freeze_window_starts (f, 1);
8436 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8437 }
8438
8439 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8440 }
8441 }
8442
8443 if (old_current_buffer)
8444 set_buffer_internal (old_current_buffer);
8445 }
8446
8447 return window_height_changed_p;
8448 }
8449
8450
8451 /* Value is the current message, a string, or nil if there is no
8452 current message. */
8453
8454 Lisp_Object
8455 current_message ()
8456 {
8457 Lisp_Object msg;
8458
8459 if (NILP (echo_area_buffer[0]))
8460 msg = Qnil;
8461 else
8462 {
8463 with_echo_area_buffer (0, 0, current_message_1,
8464 (EMACS_INT) &msg, Qnil, 0, 0);
8465 if (NILP (msg))
8466 echo_area_buffer[0] = Qnil;
8467 }
8468
8469 return msg;
8470 }
8471
8472
8473 static int
8474 current_message_1 (a1, a2, a3, a4)
8475 EMACS_INT a1;
8476 Lisp_Object a2;
8477 EMACS_INT a3, a4;
8478 {
8479 Lisp_Object *msg = (Lisp_Object *) a1;
8480
8481 if (Z > BEG)
8482 *msg = make_buffer_string (BEG, Z, 1);
8483 else
8484 *msg = Qnil;
8485 return 0;
8486 }
8487
8488
8489 /* Push the current message on Vmessage_stack for later restauration
8490 by restore_message. Value is non-zero if the current message isn't
8491 empty. This is a relatively infrequent operation, so it's not
8492 worth optimizing. */
8493
8494 int
8495 push_message ()
8496 {
8497 Lisp_Object msg;
8498 msg = current_message ();
8499 Vmessage_stack = Fcons (msg, Vmessage_stack);
8500 return STRINGP (msg);
8501 }
8502
8503
8504 /* Restore message display from the top of Vmessage_stack. */
8505
8506 void
8507 restore_message ()
8508 {
8509 Lisp_Object msg;
8510
8511 xassert (CONSP (Vmessage_stack));
8512 msg = XCAR (Vmessage_stack);
8513 if (STRINGP (msg))
8514 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8515 else
8516 message3_nolog (msg, 0, 0);
8517 }
8518
8519
8520 /* Handler for record_unwind_protect calling pop_message. */
8521
8522 Lisp_Object
8523 pop_message_unwind (dummy)
8524 Lisp_Object dummy;
8525 {
8526 pop_message ();
8527 return Qnil;
8528 }
8529
8530 /* Pop the top-most entry off Vmessage_stack. */
8531
8532 void
8533 pop_message ()
8534 {
8535 xassert (CONSP (Vmessage_stack));
8536 Vmessage_stack = XCDR (Vmessage_stack);
8537 }
8538
8539
8540 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8541 exits. If the stack is not empty, we have a missing pop_message
8542 somewhere. */
8543
8544 void
8545 check_message_stack ()
8546 {
8547 if (!NILP (Vmessage_stack))
8548 abort ();
8549 }
8550
8551
8552 /* Truncate to NCHARS what will be displayed in the echo area the next
8553 time we display it---but don't redisplay it now. */
8554
8555 void
8556 truncate_echo_area (nchars)
8557 int nchars;
8558 {
8559 if (nchars == 0)
8560 echo_area_buffer[0] = Qnil;
8561 /* A null message buffer means that the frame hasn't really been
8562 initialized yet. Error messages get reported properly by
8563 cmd_error, so this must be just an informative message; toss it. */
8564 else if (!noninteractive
8565 && INTERACTIVE
8566 && !NILP (echo_area_buffer[0]))
8567 {
8568 struct frame *sf = SELECTED_FRAME ();
8569 if (FRAME_MESSAGE_BUF (sf))
8570 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8571 }
8572 }
8573
8574
8575 /* Helper function for truncate_echo_area. Truncate the current
8576 message to at most NCHARS characters. */
8577
8578 static int
8579 truncate_message_1 (nchars, a2, a3, a4)
8580 EMACS_INT nchars;
8581 Lisp_Object a2;
8582 EMACS_INT a3, a4;
8583 {
8584 if (BEG + nchars < Z)
8585 del_range (BEG + nchars, Z);
8586 if (Z == BEG)
8587 echo_area_buffer[0] = Qnil;
8588 return 0;
8589 }
8590
8591
8592 /* Set the current message to a substring of S or STRING.
8593
8594 If STRING is a Lisp string, set the message to the first NBYTES
8595 bytes from STRING. NBYTES zero means use the whole string. If
8596 STRING is multibyte, the message will be displayed multibyte.
8597
8598 If S is not null, set the message to the first LEN bytes of S. LEN
8599 zero means use the whole string. MULTIBYTE_P non-zero means S is
8600 multibyte. Display the message multibyte in that case.
8601
8602 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8603 to t before calling set_message_1 (which calls insert).
8604 */
8605
8606 void
8607 set_message (s, string, nbytes, multibyte_p)
8608 const char *s;
8609 Lisp_Object string;
8610 int nbytes, multibyte_p;
8611 {
8612 message_enable_multibyte
8613 = ((s && multibyte_p)
8614 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8615
8616 with_echo_area_buffer (0, -1, set_message_1,
8617 (EMACS_INT) s, string, nbytes, multibyte_p);
8618 message_buf_print = 0;
8619 help_echo_showing_p = 0;
8620 }
8621
8622
8623 /* Helper function for set_message. Arguments have the same meaning
8624 as there, with A1 corresponding to S and A2 corresponding to STRING
8625 This function is called with the echo area buffer being
8626 current. */
8627
8628 static int
8629 set_message_1 (a1, a2, nbytes, multibyte_p)
8630 EMACS_INT a1;
8631 Lisp_Object a2;
8632 EMACS_INT nbytes, multibyte_p;
8633 {
8634 const char *s = (const char *) a1;
8635 Lisp_Object string = a2;
8636
8637 /* Change multibyteness of the echo buffer appropriately. */
8638 if (message_enable_multibyte
8639 != !NILP (current_buffer->enable_multibyte_characters))
8640 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8641
8642 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8643
8644 /* Insert new message at BEG. */
8645 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8646
8647 if (STRINGP (string))
8648 {
8649 int nchars;
8650
8651 if (nbytes == 0)
8652 nbytes = SBYTES (string);
8653 nchars = string_byte_to_char (string, nbytes);
8654
8655 /* This function takes care of single/multibyte conversion. We
8656 just have to ensure that the echo area buffer has the right
8657 setting of enable_multibyte_characters. */
8658 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8659 }
8660 else if (s)
8661 {
8662 if (nbytes == 0)
8663 nbytes = strlen (s);
8664
8665 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8666 {
8667 /* Convert from multi-byte to single-byte. */
8668 int i, c, n;
8669 unsigned char work[1];
8670
8671 /* Convert a multibyte string to single-byte. */
8672 for (i = 0; i < nbytes; i += n)
8673 {
8674 c = string_char_and_length (s + i, nbytes - i, &n);
8675 work[0] = (SINGLE_BYTE_CHAR_P (c)
8676 ? c
8677 : multibyte_char_to_unibyte (c, Qnil));
8678 insert_1_both (work, 1, 1, 1, 0, 0);
8679 }
8680 }
8681 else if (!multibyte_p
8682 && !NILP (current_buffer->enable_multibyte_characters))
8683 {
8684 /* Convert from single-byte to multi-byte. */
8685 int i, c, n;
8686 const unsigned char *msg = (const unsigned char *) s;
8687 unsigned char str[MAX_MULTIBYTE_LENGTH];
8688
8689 /* Convert a single-byte string to multibyte. */
8690 for (i = 0; i < nbytes; i++)
8691 {
8692 c = unibyte_char_to_multibyte (msg[i]);
8693 n = CHAR_STRING (c, str);
8694 insert_1_both (str, 1, n, 1, 0, 0);
8695 }
8696 }
8697 else
8698 insert_1 (s, nbytes, 1, 0, 0);
8699 }
8700
8701 return 0;
8702 }
8703
8704
8705 /* Clear messages. CURRENT_P non-zero means clear the current
8706 message. LAST_DISPLAYED_P non-zero means clear the message
8707 last displayed. */
8708
8709 void
8710 clear_message (current_p, last_displayed_p)
8711 int current_p, last_displayed_p;
8712 {
8713 if (current_p)
8714 {
8715 echo_area_buffer[0] = Qnil;
8716 message_cleared_p = 1;
8717 }
8718
8719 if (last_displayed_p)
8720 echo_area_buffer[1] = Qnil;
8721
8722 message_buf_print = 0;
8723 }
8724
8725 /* Clear garbaged frames.
8726
8727 This function is used where the old redisplay called
8728 redraw_garbaged_frames which in turn called redraw_frame which in
8729 turn called clear_frame. The call to clear_frame was a source of
8730 flickering. I believe a clear_frame is not necessary. It should
8731 suffice in the new redisplay to invalidate all current matrices,
8732 and ensure a complete redisplay of all windows. */
8733
8734 static void
8735 clear_garbaged_frames ()
8736 {
8737 if (frame_garbaged)
8738 {
8739 Lisp_Object tail, frame;
8740 int changed_count = 0;
8741
8742 FOR_EACH_FRAME (tail, frame)
8743 {
8744 struct frame *f = XFRAME (frame);
8745
8746 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8747 {
8748 if (f->resized_p)
8749 {
8750 Fredraw_frame (frame);
8751 f->force_flush_display_p = 1;
8752 }
8753 clear_current_matrices (f);
8754 changed_count++;
8755 f->garbaged = 0;
8756 f->resized_p = 0;
8757 }
8758 }
8759
8760 frame_garbaged = 0;
8761 if (changed_count)
8762 ++windows_or_buffers_changed;
8763 }
8764 }
8765
8766
8767 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8768 is non-zero update selected_frame. Value is non-zero if the
8769 mini-windows height has been changed. */
8770
8771 static int
8772 echo_area_display (update_frame_p)
8773 int update_frame_p;
8774 {
8775 Lisp_Object mini_window;
8776 struct window *w;
8777 struct frame *f;
8778 int window_height_changed_p = 0;
8779 struct frame *sf = SELECTED_FRAME ();
8780
8781 mini_window = FRAME_MINIBUF_WINDOW (sf);
8782 w = XWINDOW (mini_window);
8783 f = XFRAME (WINDOW_FRAME (w));
8784
8785 /* Don't display if frame is invisible or not yet initialized. */
8786 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8787 return 0;
8788
8789 #ifdef HAVE_WINDOW_SYSTEM
8790 /* When Emacs starts, selected_frame may be the initial terminal
8791 frame. If we let this through, a message would be displayed on
8792 the terminal. */
8793 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8794 return 0;
8795 #endif /* HAVE_WINDOW_SYSTEM */
8796
8797 /* Redraw garbaged frames. */
8798 if (frame_garbaged)
8799 clear_garbaged_frames ();
8800
8801 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8802 {
8803 echo_area_window = mini_window;
8804 window_height_changed_p = display_echo_area (w);
8805 w->must_be_updated_p = 1;
8806
8807 /* Update the display, unless called from redisplay_internal.
8808 Also don't update the screen during redisplay itself. The
8809 update will happen at the end of redisplay, and an update
8810 here could cause confusion. */
8811 if (update_frame_p && !redisplaying_p)
8812 {
8813 int n = 0;
8814
8815 /* If the display update has been interrupted by pending
8816 input, update mode lines in the frame. Due to the
8817 pending input, it might have been that redisplay hasn't
8818 been called, so that mode lines above the echo area are
8819 garbaged. This looks odd, so we prevent it here. */
8820 if (!display_completed)
8821 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8822
8823 if (window_height_changed_p
8824 /* Don't do this if Emacs is shutting down. Redisplay
8825 needs to run hooks. */
8826 && !NILP (Vrun_hooks))
8827 {
8828 /* Must update other windows. Likewise as in other
8829 cases, don't let this update be interrupted by
8830 pending input. */
8831 int count = SPECPDL_INDEX ();
8832 specbind (Qredisplay_dont_pause, Qt);
8833 windows_or_buffers_changed = 1;
8834 redisplay_internal (0);
8835 unbind_to (count, Qnil);
8836 }
8837 else if (FRAME_WINDOW_P (f) && n == 0)
8838 {
8839 /* Window configuration is the same as before.
8840 Can do with a display update of the echo area,
8841 unless we displayed some mode lines. */
8842 update_single_window (w, 1);
8843 FRAME_RIF (f)->flush_display (f);
8844 }
8845 else
8846 update_frame (f, 1, 1);
8847
8848 /* If cursor is in the echo area, make sure that the next
8849 redisplay displays the minibuffer, so that the cursor will
8850 be replaced with what the minibuffer wants. */
8851 if (cursor_in_echo_area)
8852 ++windows_or_buffers_changed;
8853 }
8854 }
8855 else if (!EQ (mini_window, selected_window))
8856 windows_or_buffers_changed++;
8857
8858 /* Last displayed message is now the current message. */
8859 echo_area_buffer[1] = echo_area_buffer[0];
8860 /* Inform read_char that we're not echoing. */
8861 echo_message_buffer = Qnil;
8862
8863 /* Prevent redisplay optimization in redisplay_internal by resetting
8864 this_line_start_pos. This is done because the mini-buffer now
8865 displays the message instead of its buffer text. */
8866 if (EQ (mini_window, selected_window))
8867 CHARPOS (this_line_start_pos) = 0;
8868
8869 return window_height_changed_p;
8870 }
8871
8872
8873 \f
8874 /***********************************************************************
8875 Mode Lines and Frame Titles
8876 ***********************************************************************/
8877
8878 /* A buffer for constructing non-propertized mode-line strings and
8879 frame titles in it; allocated from the heap in init_xdisp and
8880 resized as needed in store_mode_line_noprop_char. */
8881
8882 static char *mode_line_noprop_buf;
8883
8884 /* The buffer's end, and a current output position in it. */
8885
8886 static char *mode_line_noprop_buf_end;
8887 static char *mode_line_noprop_ptr;
8888
8889 #define MODE_LINE_NOPROP_LEN(start) \
8890 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8891
8892 static enum {
8893 MODE_LINE_DISPLAY = 0,
8894 MODE_LINE_TITLE,
8895 MODE_LINE_NOPROP,
8896 MODE_LINE_STRING
8897 } mode_line_target;
8898
8899 /* Alist that caches the results of :propertize.
8900 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8901 static Lisp_Object mode_line_proptrans_alist;
8902
8903 /* List of strings making up the mode-line. */
8904 static Lisp_Object mode_line_string_list;
8905
8906 /* Base face property when building propertized mode line string. */
8907 static Lisp_Object mode_line_string_face;
8908 static Lisp_Object mode_line_string_face_prop;
8909
8910
8911 /* Unwind data for mode line strings */
8912
8913 static Lisp_Object Vmode_line_unwind_vector;
8914
8915 static Lisp_Object
8916 format_mode_line_unwind_data (obuf, save_proptrans)
8917 struct buffer *obuf;
8918 {
8919 Lisp_Object vector;
8920
8921 /* Reduce consing by keeping one vector in
8922 Vwith_echo_area_save_vector. */
8923 vector = Vmode_line_unwind_vector;
8924 Vmode_line_unwind_vector = Qnil;
8925
8926 if (NILP (vector))
8927 vector = Fmake_vector (make_number (7), Qnil);
8928
8929 AREF (vector, 0) = make_number (mode_line_target);
8930 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8931 AREF (vector, 2) = mode_line_string_list;
8932 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8933 AREF (vector, 4) = mode_line_string_face;
8934 AREF (vector, 5) = mode_line_string_face_prop;
8935
8936 if (obuf)
8937 XSETBUFFER (AREF (vector, 6), obuf);
8938 else
8939 AREF (vector, 6) = Qnil;
8940
8941 return vector;
8942 }
8943
8944 static Lisp_Object
8945 unwind_format_mode_line (vector)
8946 Lisp_Object vector;
8947 {
8948 mode_line_target = XINT (AREF (vector, 0));
8949 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8950 mode_line_string_list = AREF (vector, 2);
8951 if (! EQ (AREF (vector, 3), Qt))
8952 mode_line_proptrans_alist = AREF (vector, 3);
8953 mode_line_string_face = AREF (vector, 4);
8954 mode_line_string_face_prop = AREF (vector, 5);
8955
8956 if (!NILP (AREF (vector, 6)))
8957 {
8958 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8959 AREF (vector, 6) = Qnil;
8960 }
8961
8962 Vmode_line_unwind_vector = vector;
8963 return Qnil;
8964 }
8965
8966
8967 /* Store a single character C for the frame title in mode_line_noprop_buf.
8968 Re-allocate mode_line_noprop_buf if necessary. */
8969
8970 static void
8971 #ifdef PROTOTYPES
8972 store_mode_line_noprop_char (char c)
8973 #else
8974 store_mode_line_noprop_char (c)
8975 char c;
8976 #endif
8977 {
8978 /* If output position has reached the end of the allocated buffer,
8979 double the buffer's size. */
8980 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8981 {
8982 int len = MODE_LINE_NOPROP_LEN (0);
8983 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8984 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8985 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8986 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8987 }
8988
8989 *mode_line_noprop_ptr++ = c;
8990 }
8991
8992
8993 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8994 mode_line_noprop_ptr. STR is the string to store. Do not copy
8995 characters that yield more columns than PRECISION; PRECISION <= 0
8996 means copy the whole string. Pad with spaces until FIELD_WIDTH
8997 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8998 pad. Called from display_mode_element when it is used to build a
8999 frame title. */
9000
9001 static int
9002 store_mode_line_noprop (str, field_width, precision)
9003 const unsigned char *str;
9004 int field_width, precision;
9005 {
9006 int n = 0;
9007 int dummy, nbytes;
9008
9009 /* Copy at most PRECISION chars from STR. */
9010 nbytes = strlen (str);
9011 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9012 while (nbytes--)
9013 store_mode_line_noprop_char (*str++);
9014
9015 /* Fill up with spaces until FIELD_WIDTH reached. */
9016 while (field_width > 0
9017 && n < field_width)
9018 {
9019 store_mode_line_noprop_char (' ');
9020 ++n;
9021 }
9022
9023 return n;
9024 }
9025
9026 /***********************************************************************
9027 Frame Titles
9028 ***********************************************************************/
9029
9030 #ifdef HAVE_WINDOW_SYSTEM
9031
9032 /* Set the title of FRAME, if it has changed. The title format is
9033 Vicon_title_format if FRAME is iconified, otherwise it is
9034 frame_title_format. */
9035
9036 static void
9037 x_consider_frame_title (frame)
9038 Lisp_Object frame;
9039 {
9040 struct frame *f = XFRAME (frame);
9041
9042 if (FRAME_WINDOW_P (f)
9043 || FRAME_MINIBUF_ONLY_P (f)
9044 || f->explicit_name)
9045 {
9046 /* Do we have more than one visible frame on this X display? */
9047 Lisp_Object tail;
9048 Lisp_Object fmt;
9049 int title_start;
9050 char *title;
9051 int len;
9052 struct it it;
9053 int count = SPECPDL_INDEX ();
9054
9055 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9056 {
9057 Lisp_Object other_frame = XCAR (tail);
9058 struct frame *tf = XFRAME (other_frame);
9059
9060 if (tf != f
9061 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9062 && !FRAME_MINIBUF_ONLY_P (tf)
9063 && !EQ (other_frame, tip_frame)
9064 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9065 break;
9066 }
9067
9068 /* Set global variable indicating that multiple frames exist. */
9069 multiple_frames = CONSP (tail);
9070
9071 /* Switch to the buffer of selected window of the frame. Set up
9072 mode_line_target so that display_mode_element will output into
9073 mode_line_noprop_buf; then display the title. */
9074 record_unwind_protect (unwind_format_mode_line,
9075 format_mode_line_unwind_data (current_buffer, 0));
9076
9077 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9078 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9079
9080 mode_line_target = MODE_LINE_TITLE;
9081 title_start = MODE_LINE_NOPROP_LEN (0);
9082 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9083 NULL, DEFAULT_FACE_ID);
9084 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9085 len = MODE_LINE_NOPROP_LEN (title_start);
9086 title = mode_line_noprop_buf + title_start;
9087 unbind_to (count, Qnil);
9088
9089 /* Set the title only if it's changed. This avoids consing in
9090 the common case where it hasn't. (If it turns out that we've
9091 already wasted too much time by walking through the list with
9092 display_mode_element, then we might need to optimize at a
9093 higher level than this.) */
9094 if (! STRINGP (f->name)
9095 || SBYTES (f->name) != len
9096 || bcmp (title, SDATA (f->name), len) != 0)
9097 x_implicitly_set_name (f, make_string (title, len), Qnil);
9098 }
9099 }
9100
9101 #endif /* not HAVE_WINDOW_SYSTEM */
9102
9103
9104
9105 \f
9106 /***********************************************************************
9107 Menu Bars
9108 ***********************************************************************/
9109
9110
9111 /* Prepare for redisplay by updating menu-bar item lists when
9112 appropriate. This can call eval. */
9113
9114 void
9115 prepare_menu_bars ()
9116 {
9117 int all_windows;
9118 struct gcpro gcpro1, gcpro2;
9119 struct frame *f;
9120 Lisp_Object tooltip_frame;
9121
9122 #ifdef HAVE_WINDOW_SYSTEM
9123 tooltip_frame = tip_frame;
9124 #else
9125 tooltip_frame = Qnil;
9126 #endif
9127
9128 /* Update all frame titles based on their buffer names, etc. We do
9129 this before the menu bars so that the buffer-menu will show the
9130 up-to-date frame titles. */
9131 #ifdef HAVE_WINDOW_SYSTEM
9132 if (windows_or_buffers_changed || update_mode_lines)
9133 {
9134 Lisp_Object tail, frame;
9135
9136 FOR_EACH_FRAME (tail, frame)
9137 {
9138 f = XFRAME (frame);
9139 if (!EQ (frame, tooltip_frame)
9140 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9141 x_consider_frame_title (frame);
9142 }
9143 }
9144 #endif /* HAVE_WINDOW_SYSTEM */
9145
9146 /* Update the menu bar item lists, if appropriate. This has to be
9147 done before any actual redisplay or generation of display lines. */
9148 all_windows = (update_mode_lines
9149 || buffer_shared > 1
9150 || windows_or_buffers_changed);
9151 if (all_windows)
9152 {
9153 Lisp_Object tail, frame;
9154 int count = SPECPDL_INDEX ();
9155 /* 1 means that update_menu_bar has run its hooks
9156 so any further calls to update_menu_bar shouldn't do so again. */
9157 int menu_bar_hooks_run = 0;
9158
9159 record_unwind_save_match_data ();
9160
9161 FOR_EACH_FRAME (tail, frame)
9162 {
9163 f = XFRAME (frame);
9164
9165 /* Ignore tooltip frame. */
9166 if (EQ (frame, tooltip_frame))
9167 continue;
9168
9169 /* If a window on this frame changed size, report that to
9170 the user and clear the size-change flag. */
9171 if (FRAME_WINDOW_SIZES_CHANGED (f))
9172 {
9173 Lisp_Object functions;
9174
9175 /* Clear flag first in case we get an error below. */
9176 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9177 functions = Vwindow_size_change_functions;
9178 GCPRO2 (tail, functions);
9179
9180 while (CONSP (functions))
9181 {
9182 call1 (XCAR (functions), frame);
9183 functions = XCDR (functions);
9184 }
9185 UNGCPRO;
9186 }
9187
9188 GCPRO1 (tail);
9189 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9190 #ifdef HAVE_WINDOW_SYSTEM
9191 update_tool_bar (f, 0);
9192 #ifdef MAC_OS
9193 mac_update_title_bar (f, 0);
9194 #endif
9195 #endif
9196 UNGCPRO;
9197 }
9198
9199 unbind_to (count, Qnil);
9200 }
9201 else
9202 {
9203 struct frame *sf = SELECTED_FRAME ();
9204 update_menu_bar (sf, 1, 0);
9205 #ifdef HAVE_WINDOW_SYSTEM
9206 update_tool_bar (sf, 1);
9207 #ifdef MAC_OS
9208 mac_update_title_bar (sf, 1);
9209 #endif
9210 #endif
9211 }
9212
9213 /* Motif needs this. See comment in xmenu.c. Turn it off when
9214 pending_menu_activation is not defined. */
9215 #ifdef USE_X_TOOLKIT
9216 pending_menu_activation = 0;
9217 #endif
9218 }
9219
9220
9221 /* Update the menu bar item list for frame F. This has to be done
9222 before we start to fill in any display lines, because it can call
9223 eval.
9224
9225 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9226
9227 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9228 already ran the menu bar hooks for this redisplay, so there
9229 is no need to run them again. The return value is the
9230 updated value of this flag, to pass to the next call. */
9231
9232 static int
9233 update_menu_bar (f, save_match_data, hooks_run)
9234 struct frame *f;
9235 int save_match_data;
9236 int hooks_run;
9237 {
9238 Lisp_Object window;
9239 register struct window *w;
9240
9241 /* If called recursively during a menu update, do nothing. This can
9242 happen when, for instance, an activate-menubar-hook causes a
9243 redisplay. */
9244 if (inhibit_menubar_update)
9245 return hooks_run;
9246
9247 window = FRAME_SELECTED_WINDOW (f);
9248 w = XWINDOW (window);
9249
9250 #if 0 /* The if statement below this if statement used to include the
9251 condition !NILP (w->update_mode_line), rather than using
9252 update_mode_lines directly, and this if statement may have
9253 been added to make that condition work. Now the if
9254 statement below matches its comment, this isn't needed. */
9255 if (update_mode_lines)
9256 w->update_mode_line = Qt;
9257 #endif
9258
9259 if (FRAME_WINDOW_P (f)
9260 ?
9261 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9262 || defined (USE_GTK)
9263 FRAME_EXTERNAL_MENU_BAR (f)
9264 #else
9265 FRAME_MENU_BAR_LINES (f) > 0
9266 #endif
9267 : FRAME_MENU_BAR_LINES (f) > 0)
9268 {
9269 /* If the user has switched buffers or windows, we need to
9270 recompute to reflect the new bindings. But we'll
9271 recompute when update_mode_lines is set too; that means
9272 that people can use force-mode-line-update to request
9273 that the menu bar be recomputed. The adverse effect on
9274 the rest of the redisplay algorithm is about the same as
9275 windows_or_buffers_changed anyway. */
9276 if (windows_or_buffers_changed
9277 /* This used to test w->update_mode_line, but we believe
9278 there is no need to recompute the menu in that case. */
9279 || update_mode_lines
9280 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9281 < BUF_MODIFF (XBUFFER (w->buffer)))
9282 != !NILP (w->last_had_star))
9283 || ((!NILP (Vtransient_mark_mode)
9284 && !NILP (XBUFFER (w->buffer)->mark_active))
9285 != !NILP (w->region_showing)))
9286 {
9287 struct buffer *prev = current_buffer;
9288 int count = SPECPDL_INDEX ();
9289
9290 specbind (Qinhibit_menubar_update, Qt);
9291
9292 set_buffer_internal_1 (XBUFFER (w->buffer));
9293 if (save_match_data)
9294 record_unwind_save_match_data ();
9295 if (NILP (Voverriding_local_map_menu_flag))
9296 {
9297 specbind (Qoverriding_terminal_local_map, Qnil);
9298 specbind (Qoverriding_local_map, Qnil);
9299 }
9300
9301 if (!hooks_run)
9302 {
9303 /* Run the Lucid hook. */
9304 safe_run_hooks (Qactivate_menubar_hook);
9305
9306 /* If it has changed current-menubar from previous value,
9307 really recompute the menu-bar from the value. */
9308 if (! NILP (Vlucid_menu_bar_dirty_flag))
9309 call0 (Qrecompute_lucid_menubar);
9310
9311 safe_run_hooks (Qmenu_bar_update_hook);
9312
9313 hooks_run = 1;
9314 }
9315
9316 XSETFRAME (Vmenu_updating_frame, f);
9317 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9318
9319 /* Redisplay the menu bar in case we changed it. */
9320 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9321 || defined (USE_GTK)
9322 if (FRAME_WINDOW_P (f))
9323 {
9324 #ifdef MAC_OS
9325 /* All frames on Mac OS share the same menubar. So only
9326 the selected frame should be allowed to set it. */
9327 if (f == SELECTED_FRAME ())
9328 #endif
9329 set_frame_menubar (f, 0, 0);
9330 }
9331 else
9332 /* On a terminal screen, the menu bar is an ordinary screen
9333 line, and this makes it get updated. */
9334 w->update_mode_line = Qt;
9335 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9336 /* In the non-toolkit version, the menu bar is an ordinary screen
9337 line, and this makes it get updated. */
9338 w->update_mode_line = Qt;
9339 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9340
9341 unbind_to (count, Qnil);
9342 set_buffer_internal_1 (prev);
9343 }
9344 }
9345
9346 return hooks_run;
9347 }
9348
9349
9350 \f
9351 /***********************************************************************
9352 Output Cursor
9353 ***********************************************************************/
9354
9355 #ifdef HAVE_WINDOW_SYSTEM
9356
9357 /* EXPORT:
9358 Nominal cursor position -- where to draw output.
9359 HPOS and VPOS are window relative glyph matrix coordinates.
9360 X and Y are window relative pixel coordinates. */
9361
9362 struct cursor_pos output_cursor;
9363
9364
9365 /* EXPORT:
9366 Set the global variable output_cursor to CURSOR. All cursor
9367 positions are relative to updated_window. */
9368
9369 void
9370 set_output_cursor (cursor)
9371 struct cursor_pos *cursor;
9372 {
9373 output_cursor.hpos = cursor->hpos;
9374 output_cursor.vpos = cursor->vpos;
9375 output_cursor.x = cursor->x;
9376 output_cursor.y = cursor->y;
9377 }
9378
9379
9380 /* EXPORT for RIF:
9381 Set a nominal cursor position.
9382
9383 HPOS and VPOS are column/row positions in a window glyph matrix. X
9384 and Y are window text area relative pixel positions.
9385
9386 If this is done during an update, updated_window will contain the
9387 window that is being updated and the position is the future output
9388 cursor position for that window. If updated_window is null, use
9389 selected_window and display the cursor at the given position. */
9390
9391 void
9392 x_cursor_to (vpos, hpos, y, x)
9393 int vpos, hpos, y, x;
9394 {
9395 struct window *w;
9396
9397 /* If updated_window is not set, work on selected_window. */
9398 if (updated_window)
9399 w = updated_window;
9400 else
9401 w = XWINDOW (selected_window);
9402
9403 /* Set the output cursor. */
9404 output_cursor.hpos = hpos;
9405 output_cursor.vpos = vpos;
9406 output_cursor.x = x;
9407 output_cursor.y = y;
9408
9409 /* If not called as part of an update, really display the cursor.
9410 This will also set the cursor position of W. */
9411 if (updated_window == NULL)
9412 {
9413 BLOCK_INPUT;
9414 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9415 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9416 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9417 UNBLOCK_INPUT;
9418 }
9419 }
9420
9421 #endif /* HAVE_WINDOW_SYSTEM */
9422
9423 \f
9424 /***********************************************************************
9425 Tool-bars
9426 ***********************************************************************/
9427
9428 #ifdef HAVE_WINDOW_SYSTEM
9429
9430 /* Where the mouse was last time we reported a mouse event. */
9431
9432 FRAME_PTR last_mouse_frame;
9433
9434 /* Tool-bar item index of the item on which a mouse button was pressed
9435 or -1. */
9436
9437 int last_tool_bar_item;
9438
9439
9440 /* Update the tool-bar item list for frame F. This has to be done
9441 before we start to fill in any display lines. Called from
9442 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9443 and restore it here. */
9444
9445 static void
9446 update_tool_bar (f, save_match_data)
9447 struct frame *f;
9448 int save_match_data;
9449 {
9450 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9451 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9452 #else
9453 int do_update = WINDOWP (f->tool_bar_window)
9454 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9455 #endif
9456
9457 if (do_update)
9458 {
9459 Lisp_Object window;
9460 struct window *w;
9461
9462 window = FRAME_SELECTED_WINDOW (f);
9463 w = XWINDOW (window);
9464
9465 /* If the user has switched buffers or windows, we need to
9466 recompute to reflect the new bindings. But we'll
9467 recompute when update_mode_lines is set too; that means
9468 that people can use force-mode-line-update to request
9469 that the menu bar be recomputed. The adverse effect on
9470 the rest of the redisplay algorithm is about the same as
9471 windows_or_buffers_changed anyway. */
9472 if (windows_or_buffers_changed
9473 || !NILP (w->update_mode_line)
9474 || update_mode_lines
9475 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9476 < BUF_MODIFF (XBUFFER (w->buffer)))
9477 != !NILP (w->last_had_star))
9478 || ((!NILP (Vtransient_mark_mode)
9479 && !NILP (XBUFFER (w->buffer)->mark_active))
9480 != !NILP (w->region_showing)))
9481 {
9482 struct buffer *prev = current_buffer;
9483 int count = SPECPDL_INDEX ();
9484 Lisp_Object new_tool_bar;
9485 int new_n_tool_bar;
9486 struct gcpro gcpro1;
9487
9488 /* Set current_buffer to the buffer of the selected
9489 window of the frame, so that we get the right local
9490 keymaps. */
9491 set_buffer_internal_1 (XBUFFER (w->buffer));
9492
9493 /* Save match data, if we must. */
9494 if (save_match_data)
9495 record_unwind_save_match_data ();
9496
9497 /* Make sure that we don't accidentally use bogus keymaps. */
9498 if (NILP (Voverriding_local_map_menu_flag))
9499 {
9500 specbind (Qoverriding_terminal_local_map, Qnil);
9501 specbind (Qoverriding_local_map, Qnil);
9502 }
9503
9504 GCPRO1 (new_tool_bar);
9505
9506 /* Build desired tool-bar items from keymaps. */
9507 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9508 &new_n_tool_bar);
9509
9510 /* Redisplay the tool-bar if we changed it. */
9511 if (new_n_tool_bar != f->n_tool_bar_items
9512 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9513 {
9514 /* Redisplay that happens asynchronously due to an expose event
9515 may access f->tool_bar_items. Make sure we update both
9516 variables within BLOCK_INPUT so no such event interrupts. */
9517 BLOCK_INPUT;
9518 f->tool_bar_items = new_tool_bar;
9519 f->n_tool_bar_items = new_n_tool_bar;
9520 w->update_mode_line = Qt;
9521 UNBLOCK_INPUT;
9522 }
9523
9524 UNGCPRO;
9525
9526 unbind_to (count, Qnil);
9527 set_buffer_internal_1 (prev);
9528 }
9529 }
9530 }
9531
9532
9533 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9534 F's desired tool-bar contents. F->tool_bar_items must have
9535 been set up previously by calling prepare_menu_bars. */
9536
9537 static void
9538 build_desired_tool_bar_string (f)
9539 struct frame *f;
9540 {
9541 int i, size, size_needed;
9542 struct gcpro gcpro1, gcpro2, gcpro3;
9543 Lisp_Object image, plist, props;
9544
9545 image = plist = props = Qnil;
9546 GCPRO3 (image, plist, props);
9547
9548 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9549 Otherwise, make a new string. */
9550
9551 /* The size of the string we might be able to reuse. */
9552 size = (STRINGP (f->desired_tool_bar_string)
9553 ? SCHARS (f->desired_tool_bar_string)
9554 : 0);
9555
9556 /* We need one space in the string for each image. */
9557 size_needed = f->n_tool_bar_items;
9558
9559 /* Reuse f->desired_tool_bar_string, if possible. */
9560 if (size < size_needed || NILP (f->desired_tool_bar_string))
9561 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9562 make_number (' '));
9563 else
9564 {
9565 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9566 Fremove_text_properties (make_number (0), make_number (size),
9567 props, f->desired_tool_bar_string);
9568 }
9569
9570 /* Put a `display' property on the string for the images to display,
9571 put a `menu_item' property on tool-bar items with a value that
9572 is the index of the item in F's tool-bar item vector. */
9573 for (i = 0; i < f->n_tool_bar_items; ++i)
9574 {
9575 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9576
9577 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9578 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9579 int hmargin, vmargin, relief, idx, end;
9580 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9581
9582 /* If image is a vector, choose the image according to the
9583 button state. */
9584 image = PROP (TOOL_BAR_ITEM_IMAGES);
9585 if (VECTORP (image))
9586 {
9587 if (enabled_p)
9588 idx = (selected_p
9589 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9590 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9591 else
9592 idx = (selected_p
9593 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9594 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9595
9596 xassert (ASIZE (image) >= idx);
9597 image = AREF (image, idx);
9598 }
9599 else
9600 idx = -1;
9601
9602 /* Ignore invalid image specifications. */
9603 if (!valid_image_p (image))
9604 continue;
9605
9606 /* Display the tool-bar button pressed, or depressed. */
9607 plist = Fcopy_sequence (XCDR (image));
9608
9609 /* Compute margin and relief to draw. */
9610 relief = (tool_bar_button_relief >= 0
9611 ? tool_bar_button_relief
9612 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9613 hmargin = vmargin = relief;
9614
9615 if (INTEGERP (Vtool_bar_button_margin)
9616 && XINT (Vtool_bar_button_margin) > 0)
9617 {
9618 hmargin += XFASTINT (Vtool_bar_button_margin);
9619 vmargin += XFASTINT (Vtool_bar_button_margin);
9620 }
9621 else if (CONSP (Vtool_bar_button_margin))
9622 {
9623 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9624 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9625 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9626
9627 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9628 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9629 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9630 }
9631
9632 if (auto_raise_tool_bar_buttons_p)
9633 {
9634 /* Add a `:relief' property to the image spec if the item is
9635 selected. */
9636 if (selected_p)
9637 {
9638 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9639 hmargin -= relief;
9640 vmargin -= relief;
9641 }
9642 }
9643 else
9644 {
9645 /* If image is selected, display it pressed, i.e. with a
9646 negative relief. If it's not selected, display it with a
9647 raised relief. */
9648 plist = Fplist_put (plist, QCrelief,
9649 (selected_p
9650 ? make_number (-relief)
9651 : make_number (relief)));
9652 hmargin -= relief;
9653 vmargin -= relief;
9654 }
9655
9656 /* Put a margin around the image. */
9657 if (hmargin || vmargin)
9658 {
9659 if (hmargin == vmargin)
9660 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9661 else
9662 plist = Fplist_put (plist, QCmargin,
9663 Fcons (make_number (hmargin),
9664 make_number (vmargin)));
9665 }
9666
9667 /* If button is not enabled, and we don't have special images
9668 for the disabled state, make the image appear disabled by
9669 applying an appropriate algorithm to it. */
9670 if (!enabled_p && idx < 0)
9671 plist = Fplist_put (plist, QCconversion, Qdisabled);
9672
9673 /* Put a `display' text property on the string for the image to
9674 display. Put a `menu-item' property on the string that gives
9675 the start of this item's properties in the tool-bar items
9676 vector. */
9677 image = Fcons (Qimage, plist);
9678 props = list4 (Qdisplay, image,
9679 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9680
9681 /* Let the last image hide all remaining spaces in the tool bar
9682 string. The string can be longer than needed when we reuse a
9683 previous string. */
9684 if (i + 1 == f->n_tool_bar_items)
9685 end = SCHARS (f->desired_tool_bar_string);
9686 else
9687 end = i + 1;
9688 Fadd_text_properties (make_number (i), make_number (end),
9689 props, f->desired_tool_bar_string);
9690 #undef PROP
9691 }
9692
9693 UNGCPRO;
9694 }
9695
9696
9697 /* Display one line of the tool-bar of frame IT->f.
9698
9699 HEIGHT specifies the desired height of the tool-bar line.
9700 If the actual height of the glyph row is less than HEIGHT, the
9701 row's height is increased to HEIGHT, and the icons are centered
9702 vertically in the new height.
9703
9704 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9705 count a final empty row in case the tool-bar width exactly matches
9706 the window width.
9707 */
9708
9709 static void
9710 display_tool_bar_line (it, height)
9711 struct it *it;
9712 int height;
9713 {
9714 struct glyph_row *row = it->glyph_row;
9715 int max_x = it->last_visible_x;
9716 struct glyph *last;
9717
9718 prepare_desired_row (row);
9719 row->y = it->current_y;
9720
9721 /* Note that this isn't made use of if the face hasn't a box,
9722 so there's no need to check the face here. */
9723 it->start_of_box_run_p = 1;
9724
9725 while (it->current_x < max_x)
9726 {
9727 int x, n_glyphs_before, i, nglyphs;
9728 struct it it_before;
9729
9730 /* Get the next display element. */
9731 if (!get_next_display_element (it))
9732 {
9733 /* Don't count empty row if we are counting needed tool-bar lines. */
9734 if (height < 0 && !it->hpos)
9735 return;
9736 break;
9737 }
9738
9739 /* Produce glyphs. */
9740 n_glyphs_before = row->used[TEXT_AREA];
9741 it_before = *it;
9742
9743 PRODUCE_GLYPHS (it);
9744
9745 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9746 i = 0;
9747 x = it_before.current_x;
9748 while (i < nglyphs)
9749 {
9750 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9751
9752 if (x + glyph->pixel_width > max_x)
9753 {
9754 /* Glyph doesn't fit on line. Backtrack. */
9755 row->used[TEXT_AREA] = n_glyphs_before;
9756 *it = it_before;
9757 /* If this is the only glyph on this line, it will never fit on the
9758 toolbar, so skip it. But ensure there is at least one glyph,
9759 so we don't accidentally disable the tool-bar. */
9760 if (n_glyphs_before == 0
9761 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9762 break;
9763 goto out;
9764 }
9765
9766 ++it->hpos;
9767 x += glyph->pixel_width;
9768 ++i;
9769 }
9770
9771 /* Stop at line ends. */
9772 if (ITERATOR_AT_END_OF_LINE_P (it))
9773 break;
9774
9775 set_iterator_to_next (it, 1);
9776 }
9777
9778 out:;
9779
9780 row->displays_text_p = row->used[TEXT_AREA] != 0;
9781
9782 /* Use default face for the border below the tool bar.
9783
9784 FIXME: When auto-resize-tool-bars is grow-only, there is
9785 no additional border below the possibly empty tool-bar lines.
9786 So to make the extra empty lines look "normal", we have to
9787 use the tool-bar face for the border too. */
9788 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9789 it->face_id = DEFAULT_FACE_ID;
9790
9791 extend_face_to_end_of_line (it);
9792 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9793 last->right_box_line_p = 1;
9794 if (last == row->glyphs[TEXT_AREA])
9795 last->left_box_line_p = 1;
9796
9797 /* Make line the desired height and center it vertically. */
9798 if ((height -= it->max_ascent + it->max_descent) > 0)
9799 {
9800 /* Don't add more than one line height. */
9801 height %= FRAME_LINE_HEIGHT (it->f);
9802 it->max_ascent += height / 2;
9803 it->max_descent += (height + 1) / 2;
9804 }
9805
9806 compute_line_metrics (it);
9807
9808 /* If line is empty, make it occupy the rest of the tool-bar. */
9809 if (!row->displays_text_p)
9810 {
9811 row->height = row->phys_height = it->last_visible_y - row->y;
9812 row->visible_height = row->height;
9813 row->ascent = row->phys_ascent = 0;
9814 row->extra_line_spacing = 0;
9815 }
9816
9817 row->full_width_p = 1;
9818 row->continued_p = 0;
9819 row->truncated_on_left_p = 0;
9820 row->truncated_on_right_p = 0;
9821
9822 it->current_x = it->hpos = 0;
9823 it->current_y += row->height;
9824 ++it->vpos;
9825 ++it->glyph_row;
9826 }
9827
9828
9829 /* Max tool-bar height. */
9830
9831 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9832 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9833
9834 /* Value is the number of screen lines needed to make all tool-bar
9835 items of frame F visible. The number of actual rows needed is
9836 returned in *N_ROWS if non-NULL. */
9837
9838 static int
9839 tool_bar_lines_needed (f, n_rows)
9840 struct frame *f;
9841 int *n_rows;
9842 {
9843 struct window *w = XWINDOW (f->tool_bar_window);
9844 struct it it;
9845 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9846 the desired matrix, so use (unused) mode-line row as temporary row to
9847 avoid destroying the first tool-bar row. */
9848 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9849
9850 /* Initialize an iterator for iteration over
9851 F->desired_tool_bar_string in the tool-bar window of frame F. */
9852 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9853 it.first_visible_x = 0;
9854 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9855 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9856
9857 while (!ITERATOR_AT_END_P (&it))
9858 {
9859 clear_glyph_row (temp_row);
9860 it.glyph_row = temp_row;
9861 display_tool_bar_line (&it, -1);
9862 }
9863 clear_glyph_row (temp_row);
9864
9865 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9866 if (n_rows)
9867 *n_rows = it.vpos > 0 ? it.vpos : -1;
9868
9869 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9870 }
9871
9872
9873 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9874 0, 1, 0,
9875 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9876 (frame)
9877 Lisp_Object frame;
9878 {
9879 struct frame *f;
9880 struct window *w;
9881 int nlines = 0;
9882
9883 if (NILP (frame))
9884 frame = selected_frame;
9885 else
9886 CHECK_FRAME (frame);
9887 f = XFRAME (frame);
9888
9889 if (WINDOWP (f->tool_bar_window)
9890 || (w = XWINDOW (f->tool_bar_window),
9891 WINDOW_TOTAL_LINES (w) > 0))
9892 {
9893 update_tool_bar (f, 1);
9894 if (f->n_tool_bar_items)
9895 {
9896 build_desired_tool_bar_string (f);
9897 nlines = tool_bar_lines_needed (f, NULL);
9898 }
9899 }
9900
9901 return make_number (nlines);
9902 }
9903
9904
9905 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9906 height should be changed. */
9907
9908 static int
9909 redisplay_tool_bar (f)
9910 struct frame *f;
9911 {
9912 struct window *w;
9913 struct it it;
9914 struct glyph_row *row;
9915
9916 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9917 if (FRAME_EXTERNAL_TOOL_BAR (f))
9918 update_frame_tool_bar (f);
9919 return 0;
9920 #endif
9921
9922 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9923 do anything. This means you must start with tool-bar-lines
9924 non-zero to get the auto-sizing effect. Or in other words, you
9925 can turn off tool-bars by specifying tool-bar-lines zero. */
9926 if (!WINDOWP (f->tool_bar_window)
9927 || (w = XWINDOW (f->tool_bar_window),
9928 WINDOW_TOTAL_LINES (w) == 0))
9929 return 0;
9930
9931 /* Set up an iterator for the tool-bar window. */
9932 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9933 it.first_visible_x = 0;
9934 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9935 row = it.glyph_row;
9936
9937 /* Build a string that represents the contents of the tool-bar. */
9938 build_desired_tool_bar_string (f);
9939 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9940
9941 if (f->n_tool_bar_rows == 0)
9942 {
9943 int nlines;
9944
9945 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9946 nlines != WINDOW_TOTAL_LINES (w)))
9947 {
9948 extern Lisp_Object Qtool_bar_lines;
9949 Lisp_Object frame;
9950 int old_height = WINDOW_TOTAL_LINES (w);
9951
9952 XSETFRAME (frame, f);
9953 Fmodify_frame_parameters (frame,
9954 Fcons (Fcons (Qtool_bar_lines,
9955 make_number (nlines)),
9956 Qnil));
9957 if (WINDOW_TOTAL_LINES (w) != old_height)
9958 {
9959 clear_glyph_matrix (w->desired_matrix);
9960 fonts_changed_p = 1;
9961 return 1;
9962 }
9963 }
9964 }
9965
9966 /* Display as many lines as needed to display all tool-bar items. */
9967
9968 if (f->n_tool_bar_rows > 0)
9969 {
9970 int border, rows, height, extra;
9971
9972 if (INTEGERP (Vtool_bar_border))
9973 border = XINT (Vtool_bar_border);
9974 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9975 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9976 else if (EQ (Vtool_bar_border, Qborder_width))
9977 border = f->border_width;
9978 else
9979 border = 0;
9980 if (border < 0)
9981 border = 0;
9982
9983 rows = f->n_tool_bar_rows;
9984 height = max (1, (it.last_visible_y - border) / rows);
9985 extra = it.last_visible_y - border - height * rows;
9986
9987 while (it.current_y < it.last_visible_y)
9988 {
9989 int h = 0;
9990 if (extra > 0 && rows-- > 0)
9991 {
9992 h = (extra + rows - 1) / rows;
9993 extra -= h;
9994 }
9995 display_tool_bar_line (&it, height + h);
9996 }
9997 }
9998 else
9999 {
10000 while (it.current_y < it.last_visible_y)
10001 display_tool_bar_line (&it, 0);
10002 }
10003
10004 /* It doesn't make much sense to try scrolling in the tool-bar
10005 window, so don't do it. */
10006 w->desired_matrix->no_scrolling_p = 1;
10007 w->must_be_updated_p = 1;
10008
10009 if (!NILP (Vauto_resize_tool_bars))
10010 {
10011 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10012 int change_height_p = 0;
10013
10014 /* If we couldn't display everything, change the tool-bar's
10015 height if there is room for more. */
10016 if (IT_STRING_CHARPOS (it) < it.end_charpos
10017 && it.current_y < max_tool_bar_height)
10018 change_height_p = 1;
10019
10020 row = it.glyph_row - 1;
10021
10022 /* If there are blank lines at the end, except for a partially
10023 visible blank line at the end that is smaller than
10024 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10025 if (!row->displays_text_p
10026 && row->height >= FRAME_LINE_HEIGHT (f))
10027 change_height_p = 1;
10028
10029 /* If row displays tool-bar items, but is partially visible,
10030 change the tool-bar's height. */
10031 if (row->displays_text_p
10032 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10033 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10034 change_height_p = 1;
10035
10036 /* Resize windows as needed by changing the `tool-bar-lines'
10037 frame parameter. */
10038 if (change_height_p)
10039 {
10040 extern Lisp_Object Qtool_bar_lines;
10041 Lisp_Object frame;
10042 int old_height = WINDOW_TOTAL_LINES (w);
10043 int nrows;
10044 int nlines = tool_bar_lines_needed (f, &nrows);
10045
10046 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10047 && !f->minimize_tool_bar_window_p)
10048 ? (nlines > old_height)
10049 : (nlines != old_height));
10050 f->minimize_tool_bar_window_p = 0;
10051
10052 if (change_height_p)
10053 {
10054 XSETFRAME (frame, f);
10055 Fmodify_frame_parameters (frame,
10056 Fcons (Fcons (Qtool_bar_lines,
10057 make_number (nlines)),
10058 Qnil));
10059 if (WINDOW_TOTAL_LINES (w) != old_height)
10060 {
10061 clear_glyph_matrix (w->desired_matrix);
10062 f->n_tool_bar_rows = nrows;
10063 fonts_changed_p = 1;
10064 return 1;
10065 }
10066 }
10067 }
10068 }
10069
10070 f->minimize_tool_bar_window_p = 0;
10071 return 0;
10072 }
10073
10074
10075 /* Get information about the tool-bar item which is displayed in GLYPH
10076 on frame F. Return in *PROP_IDX the index where tool-bar item
10077 properties start in F->tool_bar_items. Value is zero if
10078 GLYPH doesn't display a tool-bar item. */
10079
10080 static int
10081 tool_bar_item_info (f, glyph, prop_idx)
10082 struct frame *f;
10083 struct glyph *glyph;
10084 int *prop_idx;
10085 {
10086 Lisp_Object prop;
10087 int success_p;
10088 int charpos;
10089
10090 /* This function can be called asynchronously, which means we must
10091 exclude any possibility that Fget_text_property signals an
10092 error. */
10093 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10094 charpos = max (0, charpos);
10095
10096 /* Get the text property `menu-item' at pos. The value of that
10097 property is the start index of this item's properties in
10098 F->tool_bar_items. */
10099 prop = Fget_text_property (make_number (charpos),
10100 Qmenu_item, f->current_tool_bar_string);
10101 if (INTEGERP (prop))
10102 {
10103 *prop_idx = XINT (prop);
10104 success_p = 1;
10105 }
10106 else
10107 success_p = 0;
10108
10109 return success_p;
10110 }
10111
10112 \f
10113 /* Get information about the tool-bar item at position X/Y on frame F.
10114 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10115 the current matrix of the tool-bar window of F, or NULL if not
10116 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10117 item in F->tool_bar_items. Value is
10118
10119 -1 if X/Y is not on a tool-bar item
10120 0 if X/Y is on the same item that was highlighted before.
10121 1 otherwise. */
10122
10123 static int
10124 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10125 struct frame *f;
10126 int x, y;
10127 struct glyph **glyph;
10128 int *hpos, *vpos, *prop_idx;
10129 {
10130 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10131 struct window *w = XWINDOW (f->tool_bar_window);
10132 int area;
10133
10134 /* Find the glyph under X/Y. */
10135 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10136 if (*glyph == NULL)
10137 return -1;
10138
10139 /* Get the start of this tool-bar item's properties in
10140 f->tool_bar_items. */
10141 if (!tool_bar_item_info (f, *glyph, prop_idx))
10142 return -1;
10143
10144 /* Is mouse on the highlighted item? */
10145 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10146 && *vpos >= dpyinfo->mouse_face_beg_row
10147 && *vpos <= dpyinfo->mouse_face_end_row
10148 && (*vpos > dpyinfo->mouse_face_beg_row
10149 || *hpos >= dpyinfo->mouse_face_beg_col)
10150 && (*vpos < dpyinfo->mouse_face_end_row
10151 || *hpos < dpyinfo->mouse_face_end_col
10152 || dpyinfo->mouse_face_past_end))
10153 return 0;
10154
10155 return 1;
10156 }
10157
10158
10159 /* EXPORT:
10160 Handle mouse button event on the tool-bar of frame F, at
10161 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10162 0 for button release. MODIFIERS is event modifiers for button
10163 release. */
10164
10165 void
10166 handle_tool_bar_click (f, x, y, down_p, modifiers)
10167 struct frame *f;
10168 int x, y, down_p;
10169 unsigned int modifiers;
10170 {
10171 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10172 struct window *w = XWINDOW (f->tool_bar_window);
10173 int hpos, vpos, prop_idx;
10174 struct glyph *glyph;
10175 Lisp_Object enabled_p;
10176
10177 /* If not on the highlighted tool-bar item, return. */
10178 frame_to_window_pixel_xy (w, &x, &y);
10179 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10180 return;
10181
10182 /* If item is disabled, do nothing. */
10183 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10184 if (NILP (enabled_p))
10185 return;
10186
10187 if (down_p)
10188 {
10189 /* Show item in pressed state. */
10190 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10191 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10192 last_tool_bar_item = prop_idx;
10193 }
10194 else
10195 {
10196 Lisp_Object key, frame;
10197 struct input_event event;
10198 EVENT_INIT (event);
10199
10200 /* Show item in released state. */
10201 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10202 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10203
10204 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10205
10206 XSETFRAME (frame, f);
10207 event.kind = TOOL_BAR_EVENT;
10208 event.frame_or_window = frame;
10209 event.arg = frame;
10210 kbd_buffer_store_event (&event);
10211
10212 event.kind = TOOL_BAR_EVENT;
10213 event.frame_or_window = frame;
10214 event.arg = key;
10215 event.modifiers = modifiers;
10216 kbd_buffer_store_event (&event);
10217 last_tool_bar_item = -1;
10218 }
10219 }
10220
10221
10222 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10223 tool-bar window-relative coordinates X/Y. Called from
10224 note_mouse_highlight. */
10225
10226 static void
10227 note_tool_bar_highlight (f, x, y)
10228 struct frame *f;
10229 int x, y;
10230 {
10231 Lisp_Object window = f->tool_bar_window;
10232 struct window *w = XWINDOW (window);
10233 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10234 int hpos, vpos;
10235 struct glyph *glyph;
10236 struct glyph_row *row;
10237 int i;
10238 Lisp_Object enabled_p;
10239 int prop_idx;
10240 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10241 int mouse_down_p, rc;
10242
10243 /* Function note_mouse_highlight is called with negative x(y
10244 values when mouse moves outside of the frame. */
10245 if (x <= 0 || y <= 0)
10246 {
10247 clear_mouse_face (dpyinfo);
10248 return;
10249 }
10250
10251 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10252 if (rc < 0)
10253 {
10254 /* Not on tool-bar item. */
10255 clear_mouse_face (dpyinfo);
10256 return;
10257 }
10258 else if (rc == 0)
10259 /* On same tool-bar item as before. */
10260 goto set_help_echo;
10261
10262 clear_mouse_face (dpyinfo);
10263
10264 /* Mouse is down, but on different tool-bar item? */
10265 mouse_down_p = (dpyinfo->grabbed
10266 && f == last_mouse_frame
10267 && FRAME_LIVE_P (f));
10268 if (mouse_down_p
10269 && last_tool_bar_item != prop_idx)
10270 return;
10271
10272 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10273 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10274
10275 /* If tool-bar item is not enabled, don't highlight it. */
10276 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10277 if (!NILP (enabled_p))
10278 {
10279 /* Compute the x-position of the glyph. In front and past the
10280 image is a space. We include this in the highlighted area. */
10281 row = MATRIX_ROW (w->current_matrix, vpos);
10282 for (i = x = 0; i < hpos; ++i)
10283 x += row->glyphs[TEXT_AREA][i].pixel_width;
10284
10285 /* Record this as the current active region. */
10286 dpyinfo->mouse_face_beg_col = hpos;
10287 dpyinfo->mouse_face_beg_row = vpos;
10288 dpyinfo->mouse_face_beg_x = x;
10289 dpyinfo->mouse_face_beg_y = row->y;
10290 dpyinfo->mouse_face_past_end = 0;
10291
10292 dpyinfo->mouse_face_end_col = hpos + 1;
10293 dpyinfo->mouse_face_end_row = vpos;
10294 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10295 dpyinfo->mouse_face_end_y = row->y;
10296 dpyinfo->mouse_face_window = window;
10297 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10298
10299 /* Display it as active. */
10300 show_mouse_face (dpyinfo, draw);
10301 dpyinfo->mouse_face_image_state = draw;
10302 }
10303
10304 set_help_echo:
10305
10306 /* Set help_echo_string to a help string to display for this tool-bar item.
10307 XTread_socket does the rest. */
10308 help_echo_object = help_echo_window = Qnil;
10309 help_echo_pos = -1;
10310 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10311 if (NILP (help_echo_string))
10312 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10313 }
10314
10315 #endif /* HAVE_WINDOW_SYSTEM */
10316
10317
10318 \f
10319 /************************************************************************
10320 Horizontal scrolling
10321 ************************************************************************/
10322
10323 static int hscroll_window_tree P_ ((Lisp_Object));
10324 static int hscroll_windows P_ ((Lisp_Object));
10325
10326 /* For all leaf windows in the window tree rooted at WINDOW, set their
10327 hscroll value so that PT is (i) visible in the window, and (ii) so
10328 that it is not within a certain margin at the window's left and
10329 right border. Value is non-zero if any window's hscroll has been
10330 changed. */
10331
10332 static int
10333 hscroll_window_tree (window)
10334 Lisp_Object window;
10335 {
10336 int hscrolled_p = 0;
10337 int hscroll_relative_p = FLOATP (Vhscroll_step);
10338 int hscroll_step_abs = 0;
10339 double hscroll_step_rel = 0;
10340
10341 if (hscroll_relative_p)
10342 {
10343 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10344 if (hscroll_step_rel < 0)
10345 {
10346 hscroll_relative_p = 0;
10347 hscroll_step_abs = 0;
10348 }
10349 }
10350 else if (INTEGERP (Vhscroll_step))
10351 {
10352 hscroll_step_abs = XINT (Vhscroll_step);
10353 if (hscroll_step_abs < 0)
10354 hscroll_step_abs = 0;
10355 }
10356 else
10357 hscroll_step_abs = 0;
10358
10359 while (WINDOWP (window))
10360 {
10361 struct window *w = XWINDOW (window);
10362
10363 if (WINDOWP (w->hchild))
10364 hscrolled_p |= hscroll_window_tree (w->hchild);
10365 else if (WINDOWP (w->vchild))
10366 hscrolled_p |= hscroll_window_tree (w->vchild);
10367 else if (w->cursor.vpos >= 0)
10368 {
10369 int h_margin;
10370 int text_area_width;
10371 struct glyph_row *current_cursor_row
10372 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10373 struct glyph_row *desired_cursor_row
10374 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10375 struct glyph_row *cursor_row
10376 = (desired_cursor_row->enabled_p
10377 ? desired_cursor_row
10378 : current_cursor_row);
10379
10380 text_area_width = window_box_width (w, TEXT_AREA);
10381
10382 /* Scroll when cursor is inside this scroll margin. */
10383 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10384
10385 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10386 && ((XFASTINT (w->hscroll)
10387 && w->cursor.x <= h_margin)
10388 || (cursor_row->enabled_p
10389 && cursor_row->truncated_on_right_p
10390 && (w->cursor.x >= text_area_width - h_margin))))
10391 {
10392 struct it it;
10393 int hscroll;
10394 struct buffer *saved_current_buffer;
10395 int pt;
10396 int wanted_x;
10397
10398 /* Find point in a display of infinite width. */
10399 saved_current_buffer = current_buffer;
10400 current_buffer = XBUFFER (w->buffer);
10401
10402 if (w == XWINDOW (selected_window))
10403 pt = BUF_PT (current_buffer);
10404 else
10405 {
10406 pt = marker_position (w->pointm);
10407 pt = max (BEGV, pt);
10408 pt = min (ZV, pt);
10409 }
10410
10411 /* Move iterator to pt starting at cursor_row->start in
10412 a line with infinite width. */
10413 init_to_row_start (&it, w, cursor_row);
10414 it.last_visible_x = INFINITY;
10415 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10416 current_buffer = saved_current_buffer;
10417
10418 /* Position cursor in window. */
10419 if (!hscroll_relative_p && hscroll_step_abs == 0)
10420 hscroll = max (0, (it.current_x
10421 - (ITERATOR_AT_END_OF_LINE_P (&it)
10422 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10423 : (text_area_width / 2))))
10424 / FRAME_COLUMN_WIDTH (it.f);
10425 else if (w->cursor.x >= text_area_width - h_margin)
10426 {
10427 if (hscroll_relative_p)
10428 wanted_x = text_area_width * (1 - hscroll_step_rel)
10429 - h_margin;
10430 else
10431 wanted_x = text_area_width
10432 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10433 - h_margin;
10434 hscroll
10435 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10436 }
10437 else
10438 {
10439 if (hscroll_relative_p)
10440 wanted_x = text_area_width * hscroll_step_rel
10441 + h_margin;
10442 else
10443 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10444 + h_margin;
10445 hscroll
10446 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10447 }
10448 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10449
10450 /* Don't call Fset_window_hscroll if value hasn't
10451 changed because it will prevent redisplay
10452 optimizations. */
10453 if (XFASTINT (w->hscroll) != hscroll)
10454 {
10455 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10456 w->hscroll = make_number (hscroll);
10457 hscrolled_p = 1;
10458 }
10459 }
10460 }
10461
10462 window = w->next;
10463 }
10464
10465 /* Value is non-zero if hscroll of any leaf window has been changed. */
10466 return hscrolled_p;
10467 }
10468
10469
10470 /* Set hscroll so that cursor is visible and not inside horizontal
10471 scroll margins for all windows in the tree rooted at WINDOW. See
10472 also hscroll_window_tree above. Value is non-zero if any window's
10473 hscroll has been changed. If it has, desired matrices on the frame
10474 of WINDOW are cleared. */
10475
10476 static int
10477 hscroll_windows (window)
10478 Lisp_Object window;
10479 {
10480 int hscrolled_p = hscroll_window_tree (window);
10481 if (hscrolled_p)
10482 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10483 return hscrolled_p;
10484 }
10485
10486
10487 \f
10488 /************************************************************************
10489 Redisplay
10490 ************************************************************************/
10491
10492 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10493 to a non-zero value. This is sometimes handy to have in a debugger
10494 session. */
10495
10496 #if GLYPH_DEBUG
10497
10498 /* First and last unchanged row for try_window_id. */
10499
10500 int debug_first_unchanged_at_end_vpos;
10501 int debug_last_unchanged_at_beg_vpos;
10502
10503 /* Delta vpos and y. */
10504
10505 int debug_dvpos, debug_dy;
10506
10507 /* Delta in characters and bytes for try_window_id. */
10508
10509 int debug_delta, debug_delta_bytes;
10510
10511 /* Values of window_end_pos and window_end_vpos at the end of
10512 try_window_id. */
10513
10514 EMACS_INT debug_end_pos, debug_end_vpos;
10515
10516 /* Append a string to W->desired_matrix->method. FMT is a printf
10517 format string. A1...A9 are a supplement for a variable-length
10518 argument list. If trace_redisplay_p is non-zero also printf the
10519 resulting string to stderr. */
10520
10521 static void
10522 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10523 struct window *w;
10524 char *fmt;
10525 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10526 {
10527 char buffer[512];
10528 char *method = w->desired_matrix->method;
10529 int len = strlen (method);
10530 int size = sizeof w->desired_matrix->method;
10531 int remaining = size - len - 1;
10532
10533 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10534 if (len && remaining)
10535 {
10536 method[len] = '|';
10537 --remaining, ++len;
10538 }
10539
10540 strncpy (method + len, buffer, remaining);
10541
10542 if (trace_redisplay_p)
10543 fprintf (stderr, "%p (%s): %s\n",
10544 w,
10545 ((BUFFERP (w->buffer)
10546 && STRINGP (XBUFFER (w->buffer)->name))
10547 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10548 : "no buffer"),
10549 buffer);
10550 }
10551
10552 #endif /* GLYPH_DEBUG */
10553
10554
10555 /* Value is non-zero if all changes in window W, which displays
10556 current_buffer, are in the text between START and END. START is a
10557 buffer position, END is given as a distance from Z. Used in
10558 redisplay_internal for display optimization. */
10559
10560 static INLINE int
10561 text_outside_line_unchanged_p (w, start, end)
10562 struct window *w;
10563 int start, end;
10564 {
10565 int unchanged_p = 1;
10566
10567 /* If text or overlays have changed, see where. */
10568 if (XFASTINT (w->last_modified) < MODIFF
10569 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10570 {
10571 /* Gap in the line? */
10572 if (GPT < start || Z - GPT < end)
10573 unchanged_p = 0;
10574
10575 /* Changes start in front of the line, or end after it? */
10576 if (unchanged_p
10577 && (BEG_UNCHANGED < start - 1
10578 || END_UNCHANGED < end))
10579 unchanged_p = 0;
10580
10581 /* If selective display, can't optimize if changes start at the
10582 beginning of the line. */
10583 if (unchanged_p
10584 && INTEGERP (current_buffer->selective_display)
10585 && XINT (current_buffer->selective_display) > 0
10586 && (BEG_UNCHANGED < start || GPT <= start))
10587 unchanged_p = 0;
10588
10589 /* If there are overlays at the start or end of the line, these
10590 may have overlay strings with newlines in them. A change at
10591 START, for instance, may actually concern the display of such
10592 overlay strings as well, and they are displayed on different
10593 lines. So, quickly rule out this case. (For the future, it
10594 might be desirable to implement something more telling than
10595 just BEG/END_UNCHANGED.) */
10596 if (unchanged_p)
10597 {
10598 if (BEG + BEG_UNCHANGED == start
10599 && overlay_touches_p (start))
10600 unchanged_p = 0;
10601 if (END_UNCHANGED == end
10602 && overlay_touches_p (Z - end))
10603 unchanged_p = 0;
10604 }
10605 }
10606
10607 return unchanged_p;
10608 }
10609
10610
10611 /* Do a frame update, taking possible shortcuts into account. This is
10612 the main external entry point for redisplay.
10613
10614 If the last redisplay displayed an echo area message and that message
10615 is no longer requested, we clear the echo area or bring back the
10616 mini-buffer if that is in use. */
10617
10618 void
10619 redisplay ()
10620 {
10621 redisplay_internal (0);
10622 }
10623
10624
10625 static Lisp_Object
10626 overlay_arrow_string_or_property (var)
10627 Lisp_Object var;
10628 {
10629 Lisp_Object val;
10630
10631 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10632 return val;
10633
10634 return Voverlay_arrow_string;
10635 }
10636
10637 /* Return 1 if there are any overlay-arrows in current_buffer. */
10638 static int
10639 overlay_arrow_in_current_buffer_p ()
10640 {
10641 Lisp_Object vlist;
10642
10643 for (vlist = Voverlay_arrow_variable_list;
10644 CONSP (vlist);
10645 vlist = XCDR (vlist))
10646 {
10647 Lisp_Object var = XCAR (vlist);
10648 Lisp_Object val;
10649
10650 if (!SYMBOLP (var))
10651 continue;
10652 val = find_symbol_value (var);
10653 if (MARKERP (val)
10654 && current_buffer == XMARKER (val)->buffer)
10655 return 1;
10656 }
10657 return 0;
10658 }
10659
10660
10661 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10662 has changed. */
10663
10664 static int
10665 overlay_arrows_changed_p ()
10666 {
10667 Lisp_Object vlist;
10668
10669 for (vlist = Voverlay_arrow_variable_list;
10670 CONSP (vlist);
10671 vlist = XCDR (vlist))
10672 {
10673 Lisp_Object var = XCAR (vlist);
10674 Lisp_Object val, pstr;
10675
10676 if (!SYMBOLP (var))
10677 continue;
10678 val = find_symbol_value (var);
10679 if (!MARKERP (val))
10680 continue;
10681 if (! EQ (COERCE_MARKER (val),
10682 Fget (var, Qlast_arrow_position))
10683 || ! (pstr = overlay_arrow_string_or_property (var),
10684 EQ (pstr, Fget (var, Qlast_arrow_string))))
10685 return 1;
10686 }
10687 return 0;
10688 }
10689
10690 /* Mark overlay arrows to be updated on next redisplay. */
10691
10692 static void
10693 update_overlay_arrows (up_to_date)
10694 int up_to_date;
10695 {
10696 Lisp_Object vlist;
10697
10698 for (vlist = Voverlay_arrow_variable_list;
10699 CONSP (vlist);
10700 vlist = XCDR (vlist))
10701 {
10702 Lisp_Object var = XCAR (vlist);
10703
10704 if (!SYMBOLP (var))
10705 continue;
10706
10707 if (up_to_date > 0)
10708 {
10709 Lisp_Object val = find_symbol_value (var);
10710 Fput (var, Qlast_arrow_position,
10711 COERCE_MARKER (val));
10712 Fput (var, Qlast_arrow_string,
10713 overlay_arrow_string_or_property (var));
10714 }
10715 else if (up_to_date < 0
10716 || !NILP (Fget (var, Qlast_arrow_position)))
10717 {
10718 Fput (var, Qlast_arrow_position, Qt);
10719 Fput (var, Qlast_arrow_string, Qt);
10720 }
10721 }
10722 }
10723
10724
10725 /* Return overlay arrow string to display at row.
10726 Return integer (bitmap number) for arrow bitmap in left fringe.
10727 Return nil if no overlay arrow. */
10728
10729 static Lisp_Object
10730 overlay_arrow_at_row (it, row)
10731 struct it *it;
10732 struct glyph_row *row;
10733 {
10734 Lisp_Object vlist;
10735
10736 for (vlist = Voverlay_arrow_variable_list;
10737 CONSP (vlist);
10738 vlist = XCDR (vlist))
10739 {
10740 Lisp_Object var = XCAR (vlist);
10741 Lisp_Object val;
10742
10743 if (!SYMBOLP (var))
10744 continue;
10745
10746 val = find_symbol_value (var);
10747
10748 if (MARKERP (val)
10749 && current_buffer == XMARKER (val)->buffer
10750 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10751 {
10752 if (FRAME_WINDOW_P (it->f)
10753 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10754 {
10755 #ifdef HAVE_WINDOW_SYSTEM
10756 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10757 {
10758 int fringe_bitmap;
10759 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10760 return make_number (fringe_bitmap);
10761 }
10762 #endif
10763 return make_number (-1); /* Use default arrow bitmap */
10764 }
10765 return overlay_arrow_string_or_property (var);
10766 }
10767 }
10768
10769 return Qnil;
10770 }
10771
10772 /* Return 1 if point moved out of or into a composition. Otherwise
10773 return 0. PREV_BUF and PREV_PT are the last point buffer and
10774 position. BUF and PT are the current point buffer and position. */
10775
10776 int
10777 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10778 struct buffer *prev_buf, *buf;
10779 int prev_pt, pt;
10780 {
10781 int start, end;
10782 Lisp_Object prop;
10783 Lisp_Object buffer;
10784
10785 XSETBUFFER (buffer, buf);
10786 /* Check a composition at the last point if point moved within the
10787 same buffer. */
10788 if (prev_buf == buf)
10789 {
10790 if (prev_pt == pt)
10791 /* Point didn't move. */
10792 return 0;
10793
10794 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10795 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10796 && COMPOSITION_VALID_P (start, end, prop)
10797 && start < prev_pt && end > prev_pt)
10798 /* The last point was within the composition. Return 1 iff
10799 point moved out of the composition. */
10800 return (pt <= start || pt >= end);
10801 }
10802
10803 /* Check a composition at the current point. */
10804 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10805 && find_composition (pt, -1, &start, &end, &prop, buffer)
10806 && COMPOSITION_VALID_P (start, end, prop)
10807 && start < pt && end > pt);
10808 }
10809
10810
10811 /* Reconsider the setting of B->clip_changed which is displayed
10812 in window W. */
10813
10814 static INLINE void
10815 reconsider_clip_changes (w, b)
10816 struct window *w;
10817 struct buffer *b;
10818 {
10819 if (b->clip_changed
10820 && !NILP (w->window_end_valid)
10821 && w->current_matrix->buffer == b
10822 && w->current_matrix->zv == BUF_ZV (b)
10823 && w->current_matrix->begv == BUF_BEGV (b))
10824 b->clip_changed = 0;
10825
10826 /* If display wasn't paused, and W is not a tool bar window, see if
10827 point has been moved into or out of a composition. In that case,
10828 we set b->clip_changed to 1 to force updating the screen. If
10829 b->clip_changed has already been set to 1, we can skip this
10830 check. */
10831 if (!b->clip_changed
10832 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10833 {
10834 int pt;
10835
10836 if (w == XWINDOW (selected_window))
10837 pt = BUF_PT (current_buffer);
10838 else
10839 pt = marker_position (w->pointm);
10840
10841 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10842 || pt != XINT (w->last_point))
10843 && check_point_in_composition (w->current_matrix->buffer,
10844 XINT (w->last_point),
10845 XBUFFER (w->buffer), pt))
10846 b->clip_changed = 1;
10847 }
10848 }
10849 \f
10850
10851 /* Select FRAME to forward the values of frame-local variables into C
10852 variables so that the redisplay routines can access those values
10853 directly. */
10854
10855 static void
10856 select_frame_for_redisplay (frame)
10857 Lisp_Object frame;
10858 {
10859 Lisp_Object tail, sym, val;
10860 Lisp_Object old = selected_frame;
10861
10862 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10863
10864 selected_frame = frame;
10865
10866 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10867 if (CONSP (XCAR (tail))
10868 && (sym = XCAR (XCAR (tail)),
10869 SYMBOLP (sym))
10870 && (sym = indirect_variable (sym),
10871 val = SYMBOL_VALUE (sym),
10872 (BUFFER_LOCAL_VALUEP (val)))
10873 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10874 /* Use find_symbol_value rather than Fsymbol_value
10875 to avoid an error if it is void. */
10876 find_symbol_value (sym);
10877
10878 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10879 if (CONSP (XCAR (tail))
10880 && (sym = XCAR (XCAR (tail)),
10881 SYMBOLP (sym))
10882 && (sym = indirect_variable (sym),
10883 val = SYMBOL_VALUE (sym),
10884 (BUFFER_LOCAL_VALUEP (val)))
10885 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10886 find_symbol_value (sym);
10887 }
10888
10889
10890 #define STOP_POLLING \
10891 do { if (! polling_stopped_here) stop_polling (); \
10892 polling_stopped_here = 1; } while (0)
10893
10894 #define RESUME_POLLING \
10895 do { if (polling_stopped_here) start_polling (); \
10896 polling_stopped_here = 0; } while (0)
10897
10898
10899 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10900 response to any user action; therefore, we should preserve the echo
10901 area. (Actually, our caller does that job.) Perhaps in the future
10902 avoid recentering windows if it is not necessary; currently that
10903 causes some problems. */
10904
10905 static void
10906 redisplay_internal (preserve_echo_area)
10907 int preserve_echo_area;
10908 {
10909 struct window *w = XWINDOW (selected_window);
10910 struct frame *f;
10911 int pause;
10912 int must_finish = 0;
10913 struct text_pos tlbufpos, tlendpos;
10914 int number_of_visible_frames;
10915 int count, count1;
10916 struct frame *sf;
10917 int polling_stopped_here = 0;
10918 Lisp_Object old_frame = selected_frame;
10919
10920 /* Non-zero means redisplay has to consider all windows on all
10921 frames. Zero means, only selected_window is considered. */
10922 int consider_all_windows_p;
10923
10924 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10925
10926 /* No redisplay if running in batch mode or frame is not yet fully
10927 initialized, or redisplay is explicitly turned off by setting
10928 Vinhibit_redisplay. */
10929 if (noninteractive
10930 || !NILP (Vinhibit_redisplay))
10931 return;
10932
10933 /* Don't examine these until after testing Vinhibit_redisplay.
10934 When Emacs is shutting down, perhaps because its connection to
10935 X has dropped, we should not look at them at all. */
10936 f = XFRAME (w->frame);
10937 sf = SELECTED_FRAME ();
10938
10939 if (!f->glyphs_initialized_p)
10940 return;
10941
10942 /* The flag redisplay_performed_directly_p is set by
10943 direct_output_for_insert when it already did the whole screen
10944 update necessary. */
10945 if (redisplay_performed_directly_p)
10946 {
10947 redisplay_performed_directly_p = 0;
10948 if (!hscroll_windows (selected_window))
10949 return;
10950 }
10951
10952 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10953 if (popup_activated ())
10954 return;
10955 #endif
10956
10957 /* I don't think this happens but let's be paranoid. */
10958 if (redisplaying_p)
10959 return;
10960
10961 /* Record a function that resets redisplaying_p to its old value
10962 when we leave this function. */
10963 count = SPECPDL_INDEX ();
10964 record_unwind_protect (unwind_redisplay,
10965 Fcons (make_number (redisplaying_p), selected_frame));
10966 ++redisplaying_p;
10967 specbind (Qinhibit_free_realized_faces, Qnil);
10968
10969 {
10970 Lisp_Object tail, frame;
10971
10972 FOR_EACH_FRAME (tail, frame)
10973 {
10974 struct frame *f = XFRAME (frame);
10975 f->already_hscrolled_p = 0;
10976 }
10977 }
10978
10979 retry:
10980 if (!EQ (old_frame, selected_frame)
10981 && FRAME_LIVE_P (XFRAME (old_frame)))
10982 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
10983 selected_frame and selected_window to be temporarily out-of-sync so
10984 when we come back here via `goto retry', we need to resync because we
10985 may need to run Elisp code (via prepare_menu_bars). */
10986 select_frame_for_redisplay (old_frame);
10987
10988 pause = 0;
10989 reconsider_clip_changes (w, current_buffer);
10990 last_escape_glyph_frame = NULL;
10991 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10992
10993 /* If new fonts have been loaded that make a glyph matrix adjustment
10994 necessary, do it. */
10995 if (fonts_changed_p)
10996 {
10997 adjust_glyphs (NULL);
10998 ++windows_or_buffers_changed;
10999 fonts_changed_p = 0;
11000 }
11001
11002 /* If face_change_count is non-zero, init_iterator will free all
11003 realized faces, which includes the faces referenced from current
11004 matrices. So, we can't reuse current matrices in this case. */
11005 if (face_change_count)
11006 ++windows_or_buffers_changed;
11007
11008 if (FRAME_TERMCAP_P (sf)
11009 && FRAME_TTY (sf)->previous_frame != sf)
11010 {
11011 /* Since frames on a single ASCII terminal share the same
11012 display area, displaying a different frame means redisplay
11013 the whole thing. */
11014 windows_or_buffers_changed++;
11015 SET_FRAME_GARBAGED (sf);
11016 FRAME_TTY (sf)->previous_frame = sf;
11017 }
11018
11019 /* Set the visible flags for all frames. Do this before checking
11020 for resized or garbaged frames; they want to know if their frames
11021 are visible. See the comment in frame.h for
11022 FRAME_SAMPLE_VISIBILITY. */
11023 {
11024 Lisp_Object tail, frame;
11025
11026 number_of_visible_frames = 0;
11027
11028 FOR_EACH_FRAME (tail, frame)
11029 {
11030 struct frame *f = XFRAME (frame);
11031
11032 FRAME_SAMPLE_VISIBILITY (f);
11033 if (FRAME_VISIBLE_P (f))
11034 ++number_of_visible_frames;
11035 clear_desired_matrices (f);
11036 }
11037 }
11038
11039
11040 /* Notice any pending interrupt request to change frame size. */
11041 do_pending_window_change (1);
11042
11043 /* Clear frames marked as garbaged. */
11044 if (frame_garbaged)
11045 clear_garbaged_frames ();
11046
11047 /* Build menubar and tool-bar items. */
11048 if (NILP (Vmemory_full))
11049 prepare_menu_bars ();
11050
11051 if (windows_or_buffers_changed)
11052 update_mode_lines++;
11053
11054 /* Detect case that we need to write or remove a star in the mode line. */
11055 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11056 {
11057 w->update_mode_line = Qt;
11058 if (buffer_shared > 1)
11059 update_mode_lines++;
11060 }
11061
11062 /* Avoid invocation of point motion hooks by `current_column' below. */
11063 count1 = SPECPDL_INDEX ();
11064 specbind (Qinhibit_point_motion_hooks, Qt);
11065
11066 /* If %c is in the mode line, update it if needed. */
11067 if (!NILP (w->column_number_displayed)
11068 /* This alternative quickly identifies a common case
11069 where no change is needed. */
11070 && !(PT == XFASTINT (w->last_point)
11071 && XFASTINT (w->last_modified) >= MODIFF
11072 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11073 && (XFASTINT (w->column_number_displayed)
11074 != (int) current_column ())) /* iftc */
11075 w->update_mode_line = Qt;
11076
11077 unbind_to (count1, Qnil);
11078
11079 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11080
11081 /* The variable buffer_shared is set in redisplay_window and
11082 indicates that we redisplay a buffer in different windows. See
11083 there. */
11084 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11085 || cursor_type_changed);
11086
11087 /* If specs for an arrow have changed, do thorough redisplay
11088 to ensure we remove any arrow that should no longer exist. */
11089 if (overlay_arrows_changed_p ())
11090 consider_all_windows_p = windows_or_buffers_changed = 1;
11091
11092 /* Normally the message* functions will have already displayed and
11093 updated the echo area, but the frame may have been trashed, or
11094 the update may have been preempted, so display the echo area
11095 again here. Checking message_cleared_p captures the case that
11096 the echo area should be cleared. */
11097 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11098 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11099 || (message_cleared_p
11100 && minibuf_level == 0
11101 /* If the mini-window is currently selected, this means the
11102 echo-area doesn't show through. */
11103 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11104 {
11105 int window_height_changed_p = echo_area_display (0);
11106 must_finish = 1;
11107
11108 /* If we don't display the current message, don't clear the
11109 message_cleared_p flag, because, if we did, we wouldn't clear
11110 the echo area in the next redisplay which doesn't preserve
11111 the echo area. */
11112 if (!display_last_displayed_message_p)
11113 message_cleared_p = 0;
11114
11115 if (fonts_changed_p)
11116 goto retry;
11117 else if (window_height_changed_p)
11118 {
11119 consider_all_windows_p = 1;
11120 ++update_mode_lines;
11121 ++windows_or_buffers_changed;
11122
11123 /* If window configuration was changed, frames may have been
11124 marked garbaged. Clear them or we will experience
11125 surprises wrt scrolling. */
11126 if (frame_garbaged)
11127 clear_garbaged_frames ();
11128 }
11129 }
11130 else if (EQ (selected_window, minibuf_window)
11131 && (current_buffer->clip_changed
11132 || XFASTINT (w->last_modified) < MODIFF
11133 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11134 && resize_mini_window (w, 0))
11135 {
11136 /* Resized active mini-window to fit the size of what it is
11137 showing if its contents might have changed. */
11138 must_finish = 1;
11139 consider_all_windows_p = 1;
11140 ++windows_or_buffers_changed;
11141 ++update_mode_lines;
11142
11143 /* If window configuration was changed, frames may have been
11144 marked garbaged. Clear them or we will experience
11145 surprises wrt scrolling. */
11146 if (frame_garbaged)
11147 clear_garbaged_frames ();
11148 }
11149
11150
11151 /* If showing the region, and mark has changed, we must redisplay
11152 the whole window. The assignment to this_line_start_pos prevents
11153 the optimization directly below this if-statement. */
11154 if (((!NILP (Vtransient_mark_mode)
11155 && !NILP (XBUFFER (w->buffer)->mark_active))
11156 != !NILP (w->region_showing))
11157 || (!NILP (w->region_showing)
11158 && !EQ (w->region_showing,
11159 Fmarker_position (XBUFFER (w->buffer)->mark))))
11160 CHARPOS (this_line_start_pos) = 0;
11161
11162 /* Optimize the case that only the line containing the cursor in the
11163 selected window has changed. Variables starting with this_ are
11164 set in display_line and record information about the line
11165 containing the cursor. */
11166 tlbufpos = this_line_start_pos;
11167 tlendpos = this_line_end_pos;
11168 if (!consider_all_windows_p
11169 && CHARPOS (tlbufpos) > 0
11170 && NILP (w->update_mode_line)
11171 && !current_buffer->clip_changed
11172 && !current_buffer->prevent_redisplay_optimizations_p
11173 && FRAME_VISIBLE_P (XFRAME (w->frame))
11174 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11175 /* Make sure recorded data applies to current buffer, etc. */
11176 && this_line_buffer == current_buffer
11177 && current_buffer == XBUFFER (w->buffer)
11178 && NILP (w->force_start)
11179 && NILP (w->optional_new_start)
11180 /* Point must be on the line that we have info recorded about. */
11181 && PT >= CHARPOS (tlbufpos)
11182 && PT <= Z - CHARPOS (tlendpos)
11183 /* All text outside that line, including its final newline,
11184 must be unchanged */
11185 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11186 CHARPOS (tlendpos)))
11187 {
11188 if (CHARPOS (tlbufpos) > BEGV
11189 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11190 && (CHARPOS (tlbufpos) == ZV
11191 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11192 /* Former continuation line has disappeared by becoming empty */
11193 goto cancel;
11194 else if (XFASTINT (w->last_modified) < MODIFF
11195 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11196 || MINI_WINDOW_P (w))
11197 {
11198 /* We have to handle the case of continuation around a
11199 wide-column character (See the comment in indent.c around
11200 line 885).
11201
11202 For instance, in the following case:
11203
11204 -------- Insert --------
11205 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11206 J_I_ ==> J_I_ `^^' are cursors.
11207 ^^ ^^
11208 -------- --------
11209
11210 As we have to redraw the line above, we should goto cancel. */
11211
11212 struct it it;
11213 int line_height_before = this_line_pixel_height;
11214
11215 /* Note that start_display will handle the case that the
11216 line starting at tlbufpos is a continuation lines. */
11217 start_display (&it, w, tlbufpos);
11218
11219 /* Implementation note: It this still necessary? */
11220 if (it.current_x != this_line_start_x)
11221 goto cancel;
11222
11223 TRACE ((stderr, "trying display optimization 1\n"));
11224 w->cursor.vpos = -1;
11225 overlay_arrow_seen = 0;
11226 it.vpos = this_line_vpos;
11227 it.current_y = this_line_y;
11228 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11229 display_line (&it);
11230
11231 /* If line contains point, is not continued,
11232 and ends at same distance from eob as before, we win */
11233 if (w->cursor.vpos >= 0
11234 /* Line is not continued, otherwise this_line_start_pos
11235 would have been set to 0 in display_line. */
11236 && CHARPOS (this_line_start_pos)
11237 /* Line ends as before. */
11238 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11239 /* Line has same height as before. Otherwise other lines
11240 would have to be shifted up or down. */
11241 && this_line_pixel_height == line_height_before)
11242 {
11243 /* If this is not the window's last line, we must adjust
11244 the charstarts of the lines below. */
11245 if (it.current_y < it.last_visible_y)
11246 {
11247 struct glyph_row *row
11248 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11249 int delta, delta_bytes;
11250
11251 if (Z - CHARPOS (tlendpos) == ZV)
11252 {
11253 /* This line ends at end of (accessible part of)
11254 buffer. There is no newline to count. */
11255 delta = (Z
11256 - CHARPOS (tlendpos)
11257 - MATRIX_ROW_START_CHARPOS (row));
11258 delta_bytes = (Z_BYTE
11259 - BYTEPOS (tlendpos)
11260 - MATRIX_ROW_START_BYTEPOS (row));
11261 }
11262 else
11263 {
11264 /* This line ends in a newline. Must take
11265 account of the newline and the rest of the
11266 text that follows. */
11267 delta = (Z
11268 - CHARPOS (tlendpos)
11269 - MATRIX_ROW_START_CHARPOS (row));
11270 delta_bytes = (Z_BYTE
11271 - BYTEPOS (tlendpos)
11272 - MATRIX_ROW_START_BYTEPOS (row));
11273 }
11274
11275 increment_matrix_positions (w->current_matrix,
11276 this_line_vpos + 1,
11277 w->current_matrix->nrows,
11278 delta, delta_bytes);
11279 }
11280
11281 /* If this row displays text now but previously didn't,
11282 or vice versa, w->window_end_vpos may have to be
11283 adjusted. */
11284 if ((it.glyph_row - 1)->displays_text_p)
11285 {
11286 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11287 XSETINT (w->window_end_vpos, this_line_vpos);
11288 }
11289 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11290 && this_line_vpos > 0)
11291 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11292 w->window_end_valid = Qnil;
11293
11294 /* Update hint: No need to try to scroll in update_window. */
11295 w->desired_matrix->no_scrolling_p = 1;
11296
11297 #if GLYPH_DEBUG
11298 *w->desired_matrix->method = 0;
11299 debug_method_add (w, "optimization 1");
11300 #endif
11301 #ifdef HAVE_WINDOW_SYSTEM
11302 update_window_fringes (w, 0);
11303 #endif
11304 goto update;
11305 }
11306 else
11307 goto cancel;
11308 }
11309 else if (/* Cursor position hasn't changed. */
11310 PT == XFASTINT (w->last_point)
11311 /* Make sure the cursor was last displayed
11312 in this window. Otherwise we have to reposition it. */
11313 && 0 <= w->cursor.vpos
11314 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11315 {
11316 if (!must_finish)
11317 {
11318 do_pending_window_change (1);
11319
11320 /* We used to always goto end_of_redisplay here, but this
11321 isn't enough if we have a blinking cursor. */
11322 if (w->cursor_off_p == w->last_cursor_off_p)
11323 goto end_of_redisplay;
11324 }
11325 goto update;
11326 }
11327 /* If highlighting the region, or if the cursor is in the echo area,
11328 then we can't just move the cursor. */
11329 else if (! (!NILP (Vtransient_mark_mode)
11330 && !NILP (current_buffer->mark_active))
11331 && (EQ (selected_window, current_buffer->last_selected_window)
11332 || highlight_nonselected_windows)
11333 && NILP (w->region_showing)
11334 && NILP (Vshow_trailing_whitespace)
11335 && !cursor_in_echo_area)
11336 {
11337 struct it it;
11338 struct glyph_row *row;
11339
11340 /* Skip from tlbufpos to PT and see where it is. Note that
11341 PT may be in invisible text. If so, we will end at the
11342 next visible position. */
11343 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11344 NULL, DEFAULT_FACE_ID);
11345 it.current_x = this_line_start_x;
11346 it.current_y = this_line_y;
11347 it.vpos = this_line_vpos;
11348
11349 /* The call to move_it_to stops in front of PT, but
11350 moves over before-strings. */
11351 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11352
11353 if (it.vpos == this_line_vpos
11354 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11355 row->enabled_p))
11356 {
11357 xassert (this_line_vpos == it.vpos);
11358 xassert (this_line_y == it.current_y);
11359 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11360 #if GLYPH_DEBUG
11361 *w->desired_matrix->method = 0;
11362 debug_method_add (w, "optimization 3");
11363 #endif
11364 goto update;
11365 }
11366 else
11367 goto cancel;
11368 }
11369
11370 cancel:
11371 /* Text changed drastically or point moved off of line. */
11372 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11373 }
11374
11375 CHARPOS (this_line_start_pos) = 0;
11376 consider_all_windows_p |= buffer_shared > 1;
11377 ++clear_face_cache_count;
11378 #ifdef HAVE_WINDOW_SYSTEM
11379 ++clear_image_cache_count;
11380 #endif
11381
11382 /* Build desired matrices, and update the display. If
11383 consider_all_windows_p is non-zero, do it for all windows on all
11384 frames. Otherwise do it for selected_window, only. */
11385
11386 if (consider_all_windows_p)
11387 {
11388 Lisp_Object tail, frame;
11389
11390 FOR_EACH_FRAME (tail, frame)
11391 XFRAME (frame)->updated_p = 0;
11392
11393 /* Recompute # windows showing selected buffer. This will be
11394 incremented each time such a window is displayed. */
11395 buffer_shared = 0;
11396
11397 FOR_EACH_FRAME (tail, frame)
11398 {
11399 struct frame *f = XFRAME (frame);
11400
11401 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11402 {
11403 if (! EQ (frame, selected_frame))
11404 /* Select the frame, for the sake of frame-local
11405 variables. */
11406 select_frame_for_redisplay (frame);
11407
11408 /* Mark all the scroll bars to be removed; we'll redeem
11409 the ones we want when we redisplay their windows. */
11410 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11411 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11412
11413 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11414 redisplay_windows (FRAME_ROOT_WINDOW (f));
11415
11416 /* Any scroll bars which redisplay_windows should have
11417 nuked should now go away. */
11418 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11419 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11420
11421 /* If fonts changed, display again. */
11422 /* ??? rms: I suspect it is a mistake to jump all the way
11423 back to retry here. It should just retry this frame. */
11424 if (fonts_changed_p)
11425 goto retry;
11426
11427 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11428 {
11429 /* See if we have to hscroll. */
11430 if (!f->already_hscrolled_p)
11431 {
11432 f->already_hscrolled_p = 1;
11433 if (hscroll_windows (f->root_window))
11434 goto retry;
11435 }
11436
11437 /* Prevent various kinds of signals during display
11438 update. stdio is not robust about handling
11439 signals, which can cause an apparent I/O
11440 error. */
11441 if (interrupt_input)
11442 unrequest_sigio ();
11443 STOP_POLLING;
11444
11445 /* Update the display. */
11446 set_window_update_flags (XWINDOW (f->root_window), 1);
11447 pause |= update_frame (f, 0, 0);
11448 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11449 if (pause)
11450 break;
11451 #endif
11452
11453 f->updated_p = 1;
11454 }
11455 }
11456 }
11457
11458 if (!pause)
11459 {
11460 /* Do the mark_window_display_accurate after all windows have
11461 been redisplayed because this call resets flags in buffers
11462 which are needed for proper redisplay. */
11463 FOR_EACH_FRAME (tail, frame)
11464 {
11465 struct frame *f = XFRAME (frame);
11466 if (f->updated_p)
11467 {
11468 mark_window_display_accurate (f->root_window, 1);
11469 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11470 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11471 }
11472 }
11473 }
11474 }
11475 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11476 {
11477 Lisp_Object mini_window;
11478 struct frame *mini_frame;
11479
11480 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11481 /* Use list_of_error, not Qerror, so that
11482 we catch only errors and don't run the debugger. */
11483 internal_condition_case_1 (redisplay_window_1, selected_window,
11484 list_of_error,
11485 redisplay_window_error);
11486
11487 /* Compare desired and current matrices, perform output. */
11488
11489 update:
11490 /* If fonts changed, display again. */
11491 if (fonts_changed_p)
11492 goto retry;
11493
11494 /* Prevent various kinds of signals during display update.
11495 stdio is not robust about handling signals,
11496 which can cause an apparent I/O error. */
11497 if (interrupt_input)
11498 unrequest_sigio ();
11499 STOP_POLLING;
11500
11501 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11502 {
11503 if (hscroll_windows (selected_window))
11504 goto retry;
11505
11506 XWINDOW (selected_window)->must_be_updated_p = 1;
11507 pause = update_frame (sf, 0, 0);
11508 }
11509
11510 /* We may have called echo_area_display at the top of this
11511 function. If the echo area is on another frame, that may
11512 have put text on a frame other than the selected one, so the
11513 above call to update_frame would not have caught it. Catch
11514 it here. */
11515 mini_window = FRAME_MINIBUF_WINDOW (sf);
11516 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11517
11518 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11519 {
11520 XWINDOW (mini_window)->must_be_updated_p = 1;
11521 pause |= update_frame (mini_frame, 0, 0);
11522 if (!pause && hscroll_windows (mini_window))
11523 goto retry;
11524 }
11525 }
11526
11527 /* If display was paused because of pending input, make sure we do a
11528 thorough update the next time. */
11529 if (pause)
11530 {
11531 /* Prevent the optimization at the beginning of
11532 redisplay_internal that tries a single-line update of the
11533 line containing the cursor in the selected window. */
11534 CHARPOS (this_line_start_pos) = 0;
11535
11536 /* Let the overlay arrow be updated the next time. */
11537 update_overlay_arrows (0);
11538
11539 /* If we pause after scrolling, some rows in the current
11540 matrices of some windows are not valid. */
11541 if (!WINDOW_FULL_WIDTH_P (w)
11542 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11543 update_mode_lines = 1;
11544 }
11545 else
11546 {
11547 if (!consider_all_windows_p)
11548 {
11549 /* This has already been done above if
11550 consider_all_windows_p is set. */
11551 mark_window_display_accurate_1 (w, 1);
11552
11553 /* Say overlay arrows are up to date. */
11554 update_overlay_arrows (1);
11555
11556 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11557 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11558 }
11559
11560 update_mode_lines = 0;
11561 windows_or_buffers_changed = 0;
11562 cursor_type_changed = 0;
11563 }
11564
11565 /* Start SIGIO interrupts coming again. Having them off during the
11566 code above makes it less likely one will discard output, but not
11567 impossible, since there might be stuff in the system buffer here.
11568 But it is much hairier to try to do anything about that. */
11569 if (interrupt_input)
11570 request_sigio ();
11571 RESUME_POLLING;
11572
11573 /* If a frame has become visible which was not before, redisplay
11574 again, so that we display it. Expose events for such a frame
11575 (which it gets when becoming visible) don't call the parts of
11576 redisplay constructing glyphs, so simply exposing a frame won't
11577 display anything in this case. So, we have to display these
11578 frames here explicitly. */
11579 if (!pause)
11580 {
11581 Lisp_Object tail, frame;
11582 int new_count = 0;
11583
11584 FOR_EACH_FRAME (tail, frame)
11585 {
11586 int this_is_visible = 0;
11587
11588 if (XFRAME (frame)->visible)
11589 this_is_visible = 1;
11590 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11591 if (XFRAME (frame)->visible)
11592 this_is_visible = 1;
11593
11594 if (this_is_visible)
11595 new_count++;
11596 }
11597
11598 if (new_count != number_of_visible_frames)
11599 windows_or_buffers_changed++;
11600 }
11601
11602 /* Change frame size now if a change is pending. */
11603 do_pending_window_change (1);
11604
11605 /* If we just did a pending size change, or have additional
11606 visible frames, redisplay again. */
11607 if (windows_or_buffers_changed && !pause)
11608 goto retry;
11609
11610 /* Clear the face cache eventually. */
11611 if (consider_all_windows_p)
11612 {
11613 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11614 {
11615 clear_face_cache (0);
11616 clear_face_cache_count = 0;
11617 }
11618 #ifdef HAVE_WINDOW_SYSTEM
11619 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11620 {
11621 Lisp_Object tail, frame;
11622 FOR_EACH_FRAME (tail, frame)
11623 {
11624 struct frame *f = XFRAME (frame);
11625 if (FRAME_WINDOW_P (f))
11626 clear_image_cache (f, 0);
11627 }
11628 clear_image_cache_count = 0;
11629 }
11630 #endif /* HAVE_WINDOW_SYSTEM */
11631 }
11632
11633 end_of_redisplay:
11634 unbind_to (count, Qnil);
11635 RESUME_POLLING;
11636 }
11637
11638
11639 /* Redisplay, but leave alone any recent echo area message unless
11640 another message has been requested in its place.
11641
11642 This is useful in situations where you need to redisplay but no
11643 user action has occurred, making it inappropriate for the message
11644 area to be cleared. See tracking_off and
11645 wait_reading_process_output for examples of these situations.
11646
11647 FROM_WHERE is an integer saying from where this function was
11648 called. This is useful for debugging. */
11649
11650 void
11651 redisplay_preserve_echo_area (from_where)
11652 int from_where;
11653 {
11654 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11655
11656 if (!NILP (echo_area_buffer[1]))
11657 {
11658 /* We have a previously displayed message, but no current
11659 message. Redisplay the previous message. */
11660 display_last_displayed_message_p = 1;
11661 redisplay_internal (1);
11662 display_last_displayed_message_p = 0;
11663 }
11664 else
11665 redisplay_internal (1);
11666
11667 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11668 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11669 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11670 }
11671
11672
11673 /* Function registered with record_unwind_protect in
11674 redisplay_internal. Reset redisplaying_p to the value it had
11675 before redisplay_internal was called, and clear
11676 prevent_freeing_realized_faces_p. It also selects the previously
11677 selected frame, unless it has been deleted (by an X connection
11678 failure during redisplay, for example). */
11679
11680 static Lisp_Object
11681 unwind_redisplay (val)
11682 Lisp_Object val;
11683 {
11684 Lisp_Object old_redisplaying_p, old_frame;
11685
11686 old_redisplaying_p = XCAR (val);
11687 redisplaying_p = XFASTINT (old_redisplaying_p);
11688 old_frame = XCDR (val);
11689 if (! EQ (old_frame, selected_frame)
11690 && FRAME_LIVE_P (XFRAME (old_frame)))
11691 select_frame_for_redisplay (old_frame);
11692 return Qnil;
11693 }
11694
11695
11696 /* Mark the display of window W as accurate or inaccurate. If
11697 ACCURATE_P is non-zero mark display of W as accurate. If
11698 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11699 redisplay_internal is called. */
11700
11701 static void
11702 mark_window_display_accurate_1 (w, accurate_p)
11703 struct window *w;
11704 int accurate_p;
11705 {
11706 if (BUFFERP (w->buffer))
11707 {
11708 struct buffer *b = XBUFFER (w->buffer);
11709
11710 w->last_modified
11711 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11712 w->last_overlay_modified
11713 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11714 w->last_had_star
11715 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11716
11717 if (accurate_p)
11718 {
11719 b->clip_changed = 0;
11720 b->prevent_redisplay_optimizations_p = 0;
11721
11722 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11723 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11724 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11725 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11726
11727 w->current_matrix->buffer = b;
11728 w->current_matrix->begv = BUF_BEGV (b);
11729 w->current_matrix->zv = BUF_ZV (b);
11730
11731 w->last_cursor = w->cursor;
11732 w->last_cursor_off_p = w->cursor_off_p;
11733
11734 if (w == XWINDOW (selected_window))
11735 w->last_point = make_number (BUF_PT (b));
11736 else
11737 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11738 }
11739 }
11740
11741 if (accurate_p)
11742 {
11743 w->window_end_valid = w->buffer;
11744 #if 0 /* This is incorrect with variable-height lines. */
11745 xassert (XINT (w->window_end_vpos)
11746 < (WINDOW_TOTAL_LINES (w)
11747 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11748 #endif
11749 w->update_mode_line = Qnil;
11750 }
11751 }
11752
11753
11754 /* Mark the display of windows in the window tree rooted at WINDOW as
11755 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11756 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11757 be redisplayed the next time redisplay_internal is called. */
11758
11759 void
11760 mark_window_display_accurate (window, accurate_p)
11761 Lisp_Object window;
11762 int accurate_p;
11763 {
11764 struct window *w;
11765
11766 for (; !NILP (window); window = w->next)
11767 {
11768 w = XWINDOW (window);
11769 mark_window_display_accurate_1 (w, accurate_p);
11770
11771 if (!NILP (w->vchild))
11772 mark_window_display_accurate (w->vchild, accurate_p);
11773 if (!NILP (w->hchild))
11774 mark_window_display_accurate (w->hchild, accurate_p);
11775 }
11776
11777 if (accurate_p)
11778 {
11779 update_overlay_arrows (1);
11780 }
11781 else
11782 {
11783 /* Force a thorough redisplay the next time by setting
11784 last_arrow_position and last_arrow_string to t, which is
11785 unequal to any useful value of Voverlay_arrow_... */
11786 update_overlay_arrows (-1);
11787 }
11788 }
11789
11790
11791 /* Return value in display table DP (Lisp_Char_Table *) for character
11792 C. Since a display table doesn't have any parent, we don't have to
11793 follow parent. Do not call this function directly but use the
11794 macro DISP_CHAR_VECTOR. */
11795
11796 Lisp_Object
11797 disp_char_vector (dp, c)
11798 struct Lisp_Char_Table *dp;
11799 int c;
11800 {
11801 int code[4], i;
11802 Lisp_Object val;
11803
11804 if (SINGLE_BYTE_CHAR_P (c))
11805 return (dp->contents[c]);
11806
11807 SPLIT_CHAR (c, code[0], code[1], code[2]);
11808 if (code[1] < 32)
11809 code[1] = -1;
11810 else if (code[2] < 32)
11811 code[2] = -1;
11812
11813 /* Here, the possible range of code[0] (== charset ID) is
11814 128..max_charset. Since the top level char table contains data
11815 for multibyte characters after 256th element, we must increment
11816 code[0] by 128 to get a correct index. */
11817 code[0] += 128;
11818 code[3] = -1; /* anchor */
11819
11820 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11821 {
11822 val = dp->contents[code[i]];
11823 if (!SUB_CHAR_TABLE_P (val))
11824 return (NILP (val) ? dp->defalt : val);
11825 }
11826
11827 /* Here, val is a sub char table. We return the default value of
11828 it. */
11829 return (dp->defalt);
11830 }
11831
11832
11833 \f
11834 /***********************************************************************
11835 Window Redisplay
11836 ***********************************************************************/
11837
11838 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11839
11840 static void
11841 redisplay_windows (window)
11842 Lisp_Object window;
11843 {
11844 while (!NILP (window))
11845 {
11846 struct window *w = XWINDOW (window);
11847
11848 if (!NILP (w->hchild))
11849 redisplay_windows (w->hchild);
11850 else if (!NILP (w->vchild))
11851 redisplay_windows (w->vchild);
11852 else
11853 {
11854 displayed_buffer = XBUFFER (w->buffer);
11855 /* Use list_of_error, not Qerror, so that
11856 we catch only errors and don't run the debugger. */
11857 internal_condition_case_1 (redisplay_window_0, window,
11858 list_of_error,
11859 redisplay_window_error);
11860 }
11861
11862 window = w->next;
11863 }
11864 }
11865
11866 static Lisp_Object
11867 redisplay_window_error ()
11868 {
11869 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11870 return Qnil;
11871 }
11872
11873 static Lisp_Object
11874 redisplay_window_0 (window)
11875 Lisp_Object window;
11876 {
11877 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11878 redisplay_window (window, 0);
11879 return Qnil;
11880 }
11881
11882 static Lisp_Object
11883 redisplay_window_1 (window)
11884 Lisp_Object window;
11885 {
11886 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11887 redisplay_window (window, 1);
11888 return Qnil;
11889 }
11890 \f
11891
11892 /* Increment GLYPH until it reaches END or CONDITION fails while
11893 adding (GLYPH)->pixel_width to X. */
11894
11895 #define SKIP_GLYPHS(glyph, end, x, condition) \
11896 do \
11897 { \
11898 (x) += (glyph)->pixel_width; \
11899 ++(glyph); \
11900 } \
11901 while ((glyph) < (end) && (condition))
11902
11903
11904 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11905 DELTA is the number of bytes by which positions recorded in ROW
11906 differ from current buffer positions.
11907
11908 Return 0 if cursor is not on this row. 1 otherwise. */
11909
11910 int
11911 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11912 struct window *w;
11913 struct glyph_row *row;
11914 struct glyph_matrix *matrix;
11915 int delta, delta_bytes, dy, dvpos;
11916 {
11917 struct glyph *glyph = row->glyphs[TEXT_AREA];
11918 struct glyph *end = glyph + row->used[TEXT_AREA];
11919 struct glyph *cursor = NULL;
11920 /* The first glyph that starts a sequence of glyphs from string. */
11921 struct glyph *string_start;
11922 /* The X coordinate of string_start. */
11923 int string_start_x;
11924 /* The last known character position. */
11925 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11926 /* The last known character position before string_start. */
11927 int string_before_pos;
11928 int x = row->x;
11929 int cursor_x = x;
11930 int cursor_from_overlay_pos = 0;
11931 int pt_old = PT - delta;
11932
11933 /* Skip over glyphs not having an object at the start of the row.
11934 These are special glyphs like truncation marks on terminal
11935 frames. */
11936 if (row->displays_text_p)
11937 while (glyph < end
11938 && INTEGERP (glyph->object)
11939 && glyph->charpos < 0)
11940 {
11941 x += glyph->pixel_width;
11942 ++glyph;
11943 }
11944
11945 string_start = NULL;
11946 while (glyph < end
11947 && !INTEGERP (glyph->object)
11948 && (!BUFFERP (glyph->object)
11949 || (last_pos = glyph->charpos) < pt_old))
11950 {
11951 if (! STRINGP (glyph->object))
11952 {
11953 string_start = NULL;
11954 x += glyph->pixel_width;
11955 ++glyph;
11956 if (cursor_from_overlay_pos
11957 && last_pos >= cursor_from_overlay_pos)
11958 {
11959 cursor_from_overlay_pos = 0;
11960 cursor = 0;
11961 }
11962 }
11963 else
11964 {
11965 if (string_start == NULL)
11966 {
11967 string_before_pos = last_pos;
11968 string_start = glyph;
11969 string_start_x = x;
11970 }
11971 /* Skip all glyphs from string. */
11972 do
11973 {
11974 Lisp_Object cprop;
11975 int pos;
11976 if ((cursor == NULL || glyph > cursor)
11977 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11978 Qcursor, (glyph)->object),
11979 !NILP (cprop))
11980 && (pos = string_buffer_position (w, glyph->object,
11981 string_before_pos),
11982 (pos == 0 /* From overlay */
11983 || pos == pt_old)))
11984 {
11985 /* Estimate overlay buffer position from the buffer
11986 positions of the glyphs before and after the overlay.
11987 Add 1 to last_pos so that if point corresponds to the
11988 glyph right after the overlay, we still use a 'cursor'
11989 property found in that overlay. */
11990 cursor_from_overlay_pos = (pos ? 0 : last_pos
11991 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11992 cursor = glyph;
11993 cursor_x = x;
11994 }
11995 x += glyph->pixel_width;
11996 ++glyph;
11997 }
11998 while (glyph < end && EQ (glyph->object, string_start->object));
11999 }
12000 }
12001
12002 if (cursor != NULL)
12003 {
12004 glyph = cursor;
12005 x = cursor_x;
12006 }
12007 else if (row->ends_in_ellipsis_p && glyph == end)
12008 {
12009 /* Scan back over the ellipsis glyphs, decrementing positions. */
12010 while (glyph > row->glyphs[TEXT_AREA]
12011 && (glyph - 1)->charpos == last_pos)
12012 glyph--, x -= glyph->pixel_width;
12013 /* That loop always goes one position too far,
12014 including the glyph before the ellipsis.
12015 So scan forward over that one. */
12016 x += glyph->pixel_width;
12017 glyph++;
12018 }
12019 else if (string_start
12020 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12021 {
12022 /* We may have skipped over point because the previous glyphs
12023 are from string. As there's no easy way to know the
12024 character position of the current glyph, find the correct
12025 glyph on point by scanning from string_start again. */
12026 Lisp_Object limit;
12027 Lisp_Object string;
12028 struct glyph *stop = glyph;
12029 int pos;
12030
12031 limit = make_number (pt_old + 1);
12032 glyph = string_start;
12033 x = string_start_x;
12034 string = glyph->object;
12035 pos = string_buffer_position (w, string, string_before_pos);
12036 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12037 because we always put cursor after overlay strings. */
12038 while (pos == 0 && glyph < stop)
12039 {
12040 string = glyph->object;
12041 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12042 if (glyph < stop)
12043 pos = string_buffer_position (w, glyph->object, string_before_pos);
12044 }
12045
12046 while (glyph < stop)
12047 {
12048 pos = XINT (Fnext_single_char_property_change
12049 (make_number (pos), Qdisplay, Qnil, limit));
12050 if (pos > pt_old)
12051 break;
12052 /* Skip glyphs from the same string. */
12053 string = glyph->object;
12054 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12055 /* Skip glyphs from an overlay. */
12056 while (glyph < stop
12057 && ! string_buffer_position (w, glyph->object, pos))
12058 {
12059 string = glyph->object;
12060 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12061 }
12062 }
12063
12064 /* If we reached the end of the line, and end was from a string,
12065 cursor is not on this line. */
12066 if (glyph == end && row->continued_p)
12067 return 0;
12068 }
12069
12070 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12071 w->cursor.x = x;
12072 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12073 w->cursor.y = row->y + dy;
12074
12075 if (w == XWINDOW (selected_window))
12076 {
12077 if (!row->continued_p
12078 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12079 && row->x == 0)
12080 {
12081 this_line_buffer = XBUFFER (w->buffer);
12082
12083 CHARPOS (this_line_start_pos)
12084 = MATRIX_ROW_START_CHARPOS (row) + delta;
12085 BYTEPOS (this_line_start_pos)
12086 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12087
12088 CHARPOS (this_line_end_pos)
12089 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12090 BYTEPOS (this_line_end_pos)
12091 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12092
12093 this_line_y = w->cursor.y;
12094 this_line_pixel_height = row->height;
12095 this_line_vpos = w->cursor.vpos;
12096 this_line_start_x = row->x;
12097 }
12098 else
12099 CHARPOS (this_line_start_pos) = 0;
12100 }
12101
12102 return 1;
12103 }
12104
12105
12106 /* Run window scroll functions, if any, for WINDOW with new window
12107 start STARTP. Sets the window start of WINDOW to that position.
12108
12109 We assume that the window's buffer is really current. */
12110
12111 static INLINE struct text_pos
12112 run_window_scroll_functions (window, startp)
12113 Lisp_Object window;
12114 struct text_pos startp;
12115 {
12116 struct window *w = XWINDOW (window);
12117 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12118
12119 if (current_buffer != XBUFFER (w->buffer))
12120 abort ();
12121
12122 if (!NILP (Vwindow_scroll_functions))
12123 {
12124 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12125 make_number (CHARPOS (startp)));
12126 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12127 /* In case the hook functions switch buffers. */
12128 if (current_buffer != XBUFFER (w->buffer))
12129 set_buffer_internal_1 (XBUFFER (w->buffer));
12130 }
12131
12132 return startp;
12133 }
12134
12135
12136 /* Make sure the line containing the cursor is fully visible.
12137 A value of 1 means there is nothing to be done.
12138 (Either the line is fully visible, or it cannot be made so,
12139 or we cannot tell.)
12140
12141 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12142 is higher than window.
12143
12144 A value of 0 means the caller should do scrolling
12145 as if point had gone off the screen. */
12146
12147 static int
12148 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12149 struct window *w;
12150 int force_p;
12151 int current_matrix_p;
12152 {
12153 struct glyph_matrix *matrix;
12154 struct glyph_row *row;
12155 int window_height;
12156
12157 if (!make_cursor_line_fully_visible_p)
12158 return 1;
12159
12160 /* It's not always possible to find the cursor, e.g, when a window
12161 is full of overlay strings. Don't do anything in that case. */
12162 if (w->cursor.vpos < 0)
12163 return 1;
12164
12165 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12166 row = MATRIX_ROW (matrix, w->cursor.vpos);
12167
12168 /* If the cursor row is not partially visible, there's nothing to do. */
12169 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12170 return 1;
12171
12172 /* If the row the cursor is in is taller than the window's height,
12173 it's not clear what to do, so do nothing. */
12174 window_height = window_box_height (w);
12175 if (row->height >= window_height)
12176 {
12177 if (!force_p || MINI_WINDOW_P (w)
12178 || w->vscroll || w->cursor.vpos == 0)
12179 return 1;
12180 }
12181 return 0;
12182
12183 #if 0
12184 /* This code used to try to scroll the window just enough to make
12185 the line visible. It returned 0 to say that the caller should
12186 allocate larger glyph matrices. */
12187
12188 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12189 {
12190 int dy = row->height - row->visible_height;
12191 w->vscroll = 0;
12192 w->cursor.y += dy;
12193 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12194 }
12195 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12196 {
12197 int dy = - (row->height - row->visible_height);
12198 w->vscroll = dy;
12199 w->cursor.y += dy;
12200 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12201 }
12202
12203 /* When we change the cursor y-position of the selected window,
12204 change this_line_y as well so that the display optimization for
12205 the cursor line of the selected window in redisplay_internal uses
12206 the correct y-position. */
12207 if (w == XWINDOW (selected_window))
12208 this_line_y = w->cursor.y;
12209
12210 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12211 redisplay with larger matrices. */
12212 if (matrix->nrows < required_matrix_height (w))
12213 {
12214 fonts_changed_p = 1;
12215 return 0;
12216 }
12217
12218 return 1;
12219 #endif /* 0 */
12220 }
12221
12222
12223 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12224 non-zero means only WINDOW is redisplayed in redisplay_internal.
12225 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12226 in redisplay_window to bring a partially visible line into view in
12227 the case that only the cursor has moved.
12228
12229 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12230 last screen line's vertical height extends past the end of the screen.
12231
12232 Value is
12233
12234 1 if scrolling succeeded
12235
12236 0 if scrolling didn't find point.
12237
12238 -1 if new fonts have been loaded so that we must interrupt
12239 redisplay, adjust glyph matrices, and try again. */
12240
12241 enum
12242 {
12243 SCROLLING_SUCCESS,
12244 SCROLLING_FAILED,
12245 SCROLLING_NEED_LARGER_MATRICES
12246 };
12247
12248 static int
12249 try_scrolling (window, just_this_one_p, scroll_conservatively,
12250 scroll_step, temp_scroll_step, last_line_misfit)
12251 Lisp_Object window;
12252 int just_this_one_p;
12253 EMACS_INT scroll_conservatively, scroll_step;
12254 int temp_scroll_step;
12255 int last_line_misfit;
12256 {
12257 struct window *w = XWINDOW (window);
12258 struct frame *f = XFRAME (w->frame);
12259 struct text_pos scroll_margin_pos;
12260 struct text_pos pos;
12261 struct text_pos startp;
12262 struct it it;
12263 Lisp_Object window_end;
12264 int this_scroll_margin;
12265 int dy = 0;
12266 int scroll_max;
12267 int rc;
12268 int amount_to_scroll = 0;
12269 Lisp_Object aggressive;
12270 int height;
12271 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12272
12273 #if GLYPH_DEBUG
12274 debug_method_add (w, "try_scrolling");
12275 #endif
12276
12277 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12278
12279 /* Compute scroll margin height in pixels. We scroll when point is
12280 within this distance from the top or bottom of the window. */
12281 if (scroll_margin > 0)
12282 {
12283 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12284 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12285 }
12286 else
12287 this_scroll_margin = 0;
12288
12289 /* Force scroll_conservatively to have a reasonable value so it doesn't
12290 cause an overflow while computing how much to scroll. */
12291 if (scroll_conservatively)
12292 scroll_conservatively = min (scroll_conservatively,
12293 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12294
12295 /* Compute how much we should try to scroll maximally to bring point
12296 into view. */
12297 if (scroll_step || scroll_conservatively || temp_scroll_step)
12298 scroll_max = max (scroll_step,
12299 max (scroll_conservatively, temp_scroll_step));
12300 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12301 || NUMBERP (current_buffer->scroll_up_aggressively))
12302 /* We're trying to scroll because of aggressive scrolling
12303 but no scroll_step is set. Choose an arbitrary one. Maybe
12304 there should be a variable for this. */
12305 scroll_max = 10;
12306 else
12307 scroll_max = 0;
12308 scroll_max *= FRAME_LINE_HEIGHT (f);
12309
12310 /* Decide whether we have to scroll down. Start at the window end
12311 and move this_scroll_margin up to find the position of the scroll
12312 margin. */
12313 window_end = Fwindow_end (window, Qt);
12314
12315 too_near_end:
12316
12317 CHARPOS (scroll_margin_pos) = XINT (window_end);
12318 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12319
12320 if (this_scroll_margin || extra_scroll_margin_lines)
12321 {
12322 start_display (&it, w, scroll_margin_pos);
12323 if (this_scroll_margin)
12324 move_it_vertically_backward (&it, this_scroll_margin);
12325 if (extra_scroll_margin_lines)
12326 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12327 scroll_margin_pos = it.current.pos;
12328 }
12329
12330 if (PT >= CHARPOS (scroll_margin_pos))
12331 {
12332 int y0;
12333
12334 /* Point is in the scroll margin at the bottom of the window, or
12335 below. Compute a new window start that makes point visible. */
12336
12337 /* Compute the distance from the scroll margin to PT.
12338 Give up if the distance is greater than scroll_max. */
12339 start_display (&it, w, scroll_margin_pos);
12340 y0 = it.current_y;
12341 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12342 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12343
12344 /* To make point visible, we have to move the window start
12345 down so that the line the cursor is in is visible, which
12346 means we have to add in the height of the cursor line. */
12347 dy = line_bottom_y (&it) - y0;
12348
12349 if (dy > scroll_max)
12350 return SCROLLING_FAILED;
12351
12352 /* Move the window start down. If scrolling conservatively,
12353 move it just enough down to make point visible. If
12354 scroll_step is set, move it down by scroll_step. */
12355 start_display (&it, w, startp);
12356
12357 if (scroll_conservatively)
12358 /* Set AMOUNT_TO_SCROLL to at least one line,
12359 and at most scroll_conservatively lines. */
12360 amount_to_scroll
12361 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12362 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12363 else if (scroll_step || temp_scroll_step)
12364 amount_to_scroll = scroll_max;
12365 else
12366 {
12367 aggressive = current_buffer->scroll_up_aggressively;
12368 height = WINDOW_BOX_TEXT_HEIGHT (w);
12369 if (NUMBERP (aggressive))
12370 {
12371 double float_amount = XFLOATINT (aggressive) * height;
12372 amount_to_scroll = float_amount;
12373 if (amount_to_scroll == 0 && float_amount > 0)
12374 amount_to_scroll = 1;
12375 }
12376 }
12377
12378 if (amount_to_scroll <= 0)
12379 return SCROLLING_FAILED;
12380
12381 /* If moving by amount_to_scroll leaves STARTP unchanged,
12382 move it down one screen line. */
12383
12384 move_it_vertically (&it, amount_to_scroll);
12385 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12386 move_it_by_lines (&it, 1, 1);
12387 startp = it.current.pos;
12388 }
12389 else
12390 {
12391 /* See if point is inside the scroll margin at the top of the
12392 window. */
12393 scroll_margin_pos = startp;
12394 if (this_scroll_margin)
12395 {
12396 start_display (&it, w, startp);
12397 move_it_vertically (&it, this_scroll_margin);
12398 scroll_margin_pos = it.current.pos;
12399 }
12400
12401 if (PT < CHARPOS (scroll_margin_pos))
12402 {
12403 /* Point is in the scroll margin at the top of the window or
12404 above what is displayed in the window. */
12405 int y0;
12406
12407 /* Compute the vertical distance from PT to the scroll
12408 margin position. Give up if distance is greater than
12409 scroll_max. */
12410 SET_TEXT_POS (pos, PT, PT_BYTE);
12411 start_display (&it, w, pos);
12412 y0 = it.current_y;
12413 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12414 it.last_visible_y, -1,
12415 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12416 dy = it.current_y - y0;
12417 if (dy > scroll_max)
12418 return SCROLLING_FAILED;
12419
12420 /* Compute new window start. */
12421 start_display (&it, w, startp);
12422
12423 if (scroll_conservatively)
12424 amount_to_scroll
12425 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12426 else if (scroll_step || temp_scroll_step)
12427 amount_to_scroll = scroll_max;
12428 else
12429 {
12430 aggressive = current_buffer->scroll_down_aggressively;
12431 height = WINDOW_BOX_TEXT_HEIGHT (w);
12432 if (NUMBERP (aggressive))
12433 {
12434 double float_amount = XFLOATINT (aggressive) * height;
12435 amount_to_scroll = float_amount;
12436 if (amount_to_scroll == 0 && float_amount > 0)
12437 amount_to_scroll = 1;
12438 }
12439 }
12440
12441 if (amount_to_scroll <= 0)
12442 return SCROLLING_FAILED;
12443
12444 move_it_vertically_backward (&it, amount_to_scroll);
12445 startp = it.current.pos;
12446 }
12447 }
12448
12449 /* Run window scroll functions. */
12450 startp = run_window_scroll_functions (window, startp);
12451
12452 /* Display the window. Give up if new fonts are loaded, or if point
12453 doesn't appear. */
12454 if (!try_window (window, startp, 0))
12455 rc = SCROLLING_NEED_LARGER_MATRICES;
12456 else if (w->cursor.vpos < 0)
12457 {
12458 clear_glyph_matrix (w->desired_matrix);
12459 rc = SCROLLING_FAILED;
12460 }
12461 else
12462 {
12463 /* Maybe forget recorded base line for line number display. */
12464 if (!just_this_one_p
12465 || current_buffer->clip_changed
12466 || BEG_UNCHANGED < CHARPOS (startp))
12467 w->base_line_number = Qnil;
12468
12469 /* If cursor ends up on a partially visible line,
12470 treat that as being off the bottom of the screen. */
12471 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12472 {
12473 clear_glyph_matrix (w->desired_matrix);
12474 ++extra_scroll_margin_lines;
12475 goto too_near_end;
12476 }
12477 rc = SCROLLING_SUCCESS;
12478 }
12479
12480 return rc;
12481 }
12482
12483
12484 /* Compute a suitable window start for window W if display of W starts
12485 on a continuation line. Value is non-zero if a new window start
12486 was computed.
12487
12488 The new window start will be computed, based on W's width, starting
12489 from the start of the continued line. It is the start of the
12490 screen line with the minimum distance from the old start W->start. */
12491
12492 static int
12493 compute_window_start_on_continuation_line (w)
12494 struct window *w;
12495 {
12496 struct text_pos pos, start_pos;
12497 int window_start_changed_p = 0;
12498
12499 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12500
12501 /* If window start is on a continuation line... Window start may be
12502 < BEGV in case there's invisible text at the start of the
12503 buffer (M-x rmail, for example). */
12504 if (CHARPOS (start_pos) > BEGV
12505 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12506 {
12507 struct it it;
12508 struct glyph_row *row;
12509
12510 /* Handle the case that the window start is out of range. */
12511 if (CHARPOS (start_pos) < BEGV)
12512 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12513 else if (CHARPOS (start_pos) > ZV)
12514 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12515
12516 /* Find the start of the continued line. This should be fast
12517 because scan_buffer is fast (newline cache). */
12518 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12519 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12520 row, DEFAULT_FACE_ID);
12521 reseat_at_previous_visible_line_start (&it);
12522
12523 /* If the line start is "too far" away from the window start,
12524 say it takes too much time to compute a new window start. */
12525 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12526 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12527 {
12528 int min_distance, distance;
12529
12530 /* Move forward by display lines to find the new window
12531 start. If window width was enlarged, the new start can
12532 be expected to be > the old start. If window width was
12533 decreased, the new window start will be < the old start.
12534 So, we're looking for the display line start with the
12535 minimum distance from the old window start. */
12536 pos = it.current.pos;
12537 min_distance = INFINITY;
12538 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12539 distance < min_distance)
12540 {
12541 min_distance = distance;
12542 pos = it.current.pos;
12543 move_it_by_lines (&it, 1, 0);
12544 }
12545
12546 /* Set the window start there. */
12547 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12548 window_start_changed_p = 1;
12549 }
12550 }
12551
12552 return window_start_changed_p;
12553 }
12554
12555
12556 /* Try cursor movement in case text has not changed in window WINDOW,
12557 with window start STARTP. Value is
12558
12559 CURSOR_MOVEMENT_SUCCESS if successful
12560
12561 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12562
12563 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12564 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12565 we want to scroll as if scroll-step were set to 1. See the code.
12566
12567 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12568 which case we have to abort this redisplay, and adjust matrices
12569 first. */
12570
12571 enum
12572 {
12573 CURSOR_MOVEMENT_SUCCESS,
12574 CURSOR_MOVEMENT_CANNOT_BE_USED,
12575 CURSOR_MOVEMENT_MUST_SCROLL,
12576 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12577 };
12578
12579 static int
12580 try_cursor_movement (window, startp, scroll_step)
12581 Lisp_Object window;
12582 struct text_pos startp;
12583 int *scroll_step;
12584 {
12585 struct window *w = XWINDOW (window);
12586 struct frame *f = XFRAME (w->frame);
12587 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12588
12589 #if GLYPH_DEBUG
12590 if (inhibit_try_cursor_movement)
12591 return rc;
12592 #endif
12593
12594 /* Handle case where text has not changed, only point, and it has
12595 not moved off the frame. */
12596 if (/* Point may be in this window. */
12597 PT >= CHARPOS (startp)
12598 /* Selective display hasn't changed. */
12599 && !current_buffer->clip_changed
12600 /* Function force-mode-line-update is used to force a thorough
12601 redisplay. It sets either windows_or_buffers_changed or
12602 update_mode_lines. So don't take a shortcut here for these
12603 cases. */
12604 && !update_mode_lines
12605 && !windows_or_buffers_changed
12606 && !cursor_type_changed
12607 /* Can't use this case if highlighting a region. When a
12608 region exists, cursor movement has to do more than just
12609 set the cursor. */
12610 && !(!NILP (Vtransient_mark_mode)
12611 && !NILP (current_buffer->mark_active))
12612 && NILP (w->region_showing)
12613 && NILP (Vshow_trailing_whitespace)
12614 /* Right after splitting windows, last_point may be nil. */
12615 && INTEGERP (w->last_point)
12616 /* This code is not used for mini-buffer for the sake of the case
12617 of redisplaying to replace an echo area message; since in
12618 that case the mini-buffer contents per se are usually
12619 unchanged. This code is of no real use in the mini-buffer
12620 since the handling of this_line_start_pos, etc., in redisplay
12621 handles the same cases. */
12622 && !EQ (window, minibuf_window)
12623 /* When splitting windows or for new windows, it happens that
12624 redisplay is called with a nil window_end_vpos or one being
12625 larger than the window. This should really be fixed in
12626 window.c. I don't have this on my list, now, so we do
12627 approximately the same as the old redisplay code. --gerd. */
12628 && INTEGERP (w->window_end_vpos)
12629 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12630 && (FRAME_WINDOW_P (f)
12631 || !overlay_arrow_in_current_buffer_p ()))
12632 {
12633 int this_scroll_margin, top_scroll_margin;
12634 struct glyph_row *row = NULL;
12635
12636 #if GLYPH_DEBUG
12637 debug_method_add (w, "cursor movement");
12638 #endif
12639
12640 /* Scroll if point within this distance from the top or bottom
12641 of the window. This is a pixel value. */
12642 this_scroll_margin = max (0, scroll_margin);
12643 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12644 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12645
12646 top_scroll_margin = this_scroll_margin;
12647 if (WINDOW_WANTS_HEADER_LINE_P (w))
12648 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12649
12650 /* Start with the row the cursor was displayed during the last
12651 not paused redisplay. Give up if that row is not valid. */
12652 if (w->last_cursor.vpos < 0
12653 || w->last_cursor.vpos >= w->current_matrix->nrows)
12654 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12655 else
12656 {
12657 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12658 if (row->mode_line_p)
12659 ++row;
12660 if (!row->enabled_p)
12661 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12662 }
12663
12664 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12665 {
12666 int scroll_p = 0;
12667 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12668
12669 if (PT > XFASTINT (w->last_point))
12670 {
12671 /* Point has moved forward. */
12672 while (MATRIX_ROW_END_CHARPOS (row) < PT
12673 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12674 {
12675 xassert (row->enabled_p);
12676 ++row;
12677 }
12678
12679 /* The end position of a row equals the start position
12680 of the next row. If PT is there, we would rather
12681 display it in the next line. */
12682 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12683 && MATRIX_ROW_END_CHARPOS (row) == PT
12684 && !cursor_row_p (w, row))
12685 ++row;
12686
12687 /* If within the scroll margin, scroll. Note that
12688 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12689 the next line would be drawn, and that
12690 this_scroll_margin can be zero. */
12691 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12692 || PT > MATRIX_ROW_END_CHARPOS (row)
12693 /* Line is completely visible last line in window
12694 and PT is to be set in the next line. */
12695 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12696 && PT == MATRIX_ROW_END_CHARPOS (row)
12697 && !row->ends_at_zv_p
12698 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12699 scroll_p = 1;
12700 }
12701 else if (PT < XFASTINT (w->last_point))
12702 {
12703 /* Cursor has to be moved backward. Note that PT >=
12704 CHARPOS (startp) because of the outer if-statement. */
12705 while (!row->mode_line_p
12706 && (MATRIX_ROW_START_CHARPOS (row) > PT
12707 || (MATRIX_ROW_START_CHARPOS (row) == PT
12708 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12709 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12710 row > w->current_matrix->rows
12711 && (row-1)->ends_in_newline_from_string_p))))
12712 && (row->y > top_scroll_margin
12713 || CHARPOS (startp) == BEGV))
12714 {
12715 xassert (row->enabled_p);
12716 --row;
12717 }
12718
12719 /* Consider the following case: Window starts at BEGV,
12720 there is invisible, intangible text at BEGV, so that
12721 display starts at some point START > BEGV. It can
12722 happen that we are called with PT somewhere between
12723 BEGV and START. Try to handle that case. */
12724 if (row < w->current_matrix->rows
12725 || row->mode_line_p)
12726 {
12727 row = w->current_matrix->rows;
12728 if (row->mode_line_p)
12729 ++row;
12730 }
12731
12732 /* Due to newlines in overlay strings, we may have to
12733 skip forward over overlay strings. */
12734 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12735 && MATRIX_ROW_END_CHARPOS (row) == PT
12736 && !cursor_row_p (w, row))
12737 ++row;
12738
12739 /* If within the scroll margin, scroll. */
12740 if (row->y < top_scroll_margin
12741 && CHARPOS (startp) != BEGV)
12742 scroll_p = 1;
12743 }
12744 else
12745 {
12746 /* Cursor did not move. So don't scroll even if cursor line
12747 is partially visible, as it was so before. */
12748 rc = CURSOR_MOVEMENT_SUCCESS;
12749 }
12750
12751 if (PT < MATRIX_ROW_START_CHARPOS (row)
12752 || PT > MATRIX_ROW_END_CHARPOS (row))
12753 {
12754 /* if PT is not in the glyph row, give up. */
12755 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12756 }
12757 else if (rc != CURSOR_MOVEMENT_SUCCESS
12758 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12759 && make_cursor_line_fully_visible_p)
12760 {
12761 if (PT == MATRIX_ROW_END_CHARPOS (row)
12762 && !row->ends_at_zv_p
12763 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12764 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12765 else if (row->height > window_box_height (w))
12766 {
12767 /* If we end up in a partially visible line, let's
12768 make it fully visible, except when it's taller
12769 than the window, in which case we can't do much
12770 about it. */
12771 *scroll_step = 1;
12772 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12773 }
12774 else
12775 {
12776 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12777 if (!cursor_row_fully_visible_p (w, 0, 1))
12778 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12779 else
12780 rc = CURSOR_MOVEMENT_SUCCESS;
12781 }
12782 }
12783 else if (scroll_p)
12784 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12785 else
12786 {
12787 do
12788 {
12789 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12790 {
12791 rc = CURSOR_MOVEMENT_SUCCESS;
12792 break;
12793 }
12794 ++row;
12795 }
12796 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12797 && MATRIX_ROW_START_CHARPOS (row) == PT
12798 && cursor_row_p (w, row));
12799 }
12800 }
12801 }
12802
12803 return rc;
12804 }
12805
12806 void
12807 set_vertical_scroll_bar (w)
12808 struct window *w;
12809 {
12810 int start, end, whole;
12811
12812 /* Calculate the start and end positions for the current window.
12813 At some point, it would be nice to choose between scrollbars
12814 which reflect the whole buffer size, with special markers
12815 indicating narrowing, and scrollbars which reflect only the
12816 visible region.
12817
12818 Note that mini-buffers sometimes aren't displaying any text. */
12819 if (!MINI_WINDOW_P (w)
12820 || (w == XWINDOW (minibuf_window)
12821 && NILP (echo_area_buffer[0])))
12822 {
12823 struct buffer *buf = XBUFFER (w->buffer);
12824 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12825 start = marker_position (w->start) - BUF_BEGV (buf);
12826 /* I don't think this is guaranteed to be right. For the
12827 moment, we'll pretend it is. */
12828 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12829
12830 if (end < start)
12831 end = start;
12832 if (whole < (end - start))
12833 whole = end - start;
12834 }
12835 else
12836 start = end = whole = 0;
12837
12838 /* Indicate what this scroll bar ought to be displaying now. */
12839 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12840 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12841 (w, end - start, whole, start);
12842 }
12843
12844
12845 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12846 selected_window is redisplayed.
12847
12848 We can return without actually redisplaying the window if
12849 fonts_changed_p is nonzero. In that case, redisplay_internal will
12850 retry. */
12851
12852 static void
12853 redisplay_window (window, just_this_one_p)
12854 Lisp_Object window;
12855 int just_this_one_p;
12856 {
12857 struct window *w = XWINDOW (window);
12858 struct frame *f = XFRAME (w->frame);
12859 struct buffer *buffer = XBUFFER (w->buffer);
12860 struct buffer *old = current_buffer;
12861 struct text_pos lpoint, opoint, startp;
12862 int update_mode_line;
12863 int tem;
12864 struct it it;
12865 /* Record it now because it's overwritten. */
12866 int current_matrix_up_to_date_p = 0;
12867 int used_current_matrix_p = 0;
12868 /* This is less strict than current_matrix_up_to_date_p.
12869 It indictes that the buffer contents and narrowing are unchanged. */
12870 int buffer_unchanged_p = 0;
12871 int temp_scroll_step = 0;
12872 int count = SPECPDL_INDEX ();
12873 int rc;
12874 int centering_position = -1;
12875 int last_line_misfit = 0;
12876 int beg_unchanged, end_unchanged;
12877
12878 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12879 opoint = lpoint;
12880
12881 /* W must be a leaf window here. */
12882 xassert (!NILP (w->buffer));
12883 #if GLYPH_DEBUG
12884 *w->desired_matrix->method = 0;
12885 #endif
12886
12887 specbind (Qinhibit_point_motion_hooks, Qt);
12888
12889 reconsider_clip_changes (w, buffer);
12890
12891 /* Has the mode line to be updated? */
12892 update_mode_line = (!NILP (w->update_mode_line)
12893 || update_mode_lines
12894 || buffer->clip_changed
12895 || buffer->prevent_redisplay_optimizations_p);
12896
12897 if (MINI_WINDOW_P (w))
12898 {
12899 if (w == XWINDOW (echo_area_window)
12900 && !NILP (echo_area_buffer[0]))
12901 {
12902 if (update_mode_line)
12903 /* We may have to update a tty frame's menu bar or a
12904 tool-bar. Example `M-x C-h C-h C-g'. */
12905 goto finish_menu_bars;
12906 else
12907 /* We've already displayed the echo area glyphs in this window. */
12908 goto finish_scroll_bars;
12909 }
12910 else if ((w != XWINDOW (minibuf_window)
12911 || minibuf_level == 0)
12912 /* When buffer is nonempty, redisplay window normally. */
12913 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12914 /* Quail displays non-mini buffers in minibuffer window.
12915 In that case, redisplay the window normally. */
12916 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12917 {
12918 /* W is a mini-buffer window, but it's not active, so clear
12919 it. */
12920 int yb = window_text_bottom_y (w);
12921 struct glyph_row *row;
12922 int y;
12923
12924 for (y = 0, row = w->desired_matrix->rows;
12925 y < yb;
12926 y += row->height, ++row)
12927 blank_row (w, row, y);
12928 goto finish_scroll_bars;
12929 }
12930
12931 clear_glyph_matrix (w->desired_matrix);
12932 }
12933
12934 /* Otherwise set up data on this window; select its buffer and point
12935 value. */
12936 /* Really select the buffer, for the sake of buffer-local
12937 variables. */
12938 set_buffer_internal_1 (XBUFFER (w->buffer));
12939 SET_TEXT_POS (opoint, PT, PT_BYTE);
12940
12941 beg_unchanged = BEG_UNCHANGED;
12942 end_unchanged = END_UNCHANGED;
12943
12944 current_matrix_up_to_date_p
12945 = (!NILP (w->window_end_valid)
12946 && !current_buffer->clip_changed
12947 && !current_buffer->prevent_redisplay_optimizations_p
12948 && XFASTINT (w->last_modified) >= MODIFF
12949 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12950
12951 buffer_unchanged_p
12952 = (!NILP (w->window_end_valid)
12953 && !current_buffer->clip_changed
12954 && XFASTINT (w->last_modified) >= MODIFF
12955 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12956
12957 /* When windows_or_buffers_changed is non-zero, we can't rely on
12958 the window end being valid, so set it to nil there. */
12959 if (windows_or_buffers_changed)
12960 {
12961 /* If window starts on a continuation line, maybe adjust the
12962 window start in case the window's width changed. */
12963 if (XMARKER (w->start)->buffer == current_buffer)
12964 compute_window_start_on_continuation_line (w);
12965
12966 w->window_end_valid = Qnil;
12967 }
12968
12969 /* Some sanity checks. */
12970 CHECK_WINDOW_END (w);
12971 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12972 abort ();
12973 if (BYTEPOS (opoint) < CHARPOS (opoint))
12974 abort ();
12975
12976 /* If %c is in mode line, update it if needed. */
12977 if (!NILP (w->column_number_displayed)
12978 /* This alternative quickly identifies a common case
12979 where no change is needed. */
12980 && !(PT == XFASTINT (w->last_point)
12981 && XFASTINT (w->last_modified) >= MODIFF
12982 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12983 && (XFASTINT (w->column_number_displayed)
12984 != (int) current_column ())) /* iftc */
12985 update_mode_line = 1;
12986
12987 /* Count number of windows showing the selected buffer. An indirect
12988 buffer counts as its base buffer. */
12989 if (!just_this_one_p)
12990 {
12991 struct buffer *current_base, *window_base;
12992 current_base = current_buffer;
12993 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12994 if (current_base->base_buffer)
12995 current_base = current_base->base_buffer;
12996 if (window_base->base_buffer)
12997 window_base = window_base->base_buffer;
12998 if (current_base == window_base)
12999 buffer_shared++;
13000 }
13001
13002 /* Point refers normally to the selected window. For any other
13003 window, set up appropriate value. */
13004 if (!EQ (window, selected_window))
13005 {
13006 int new_pt = XMARKER (w->pointm)->charpos;
13007 int new_pt_byte = marker_byte_position (w->pointm);
13008 if (new_pt < BEGV)
13009 {
13010 new_pt = BEGV;
13011 new_pt_byte = BEGV_BYTE;
13012 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13013 }
13014 else if (new_pt > (ZV - 1))
13015 {
13016 new_pt = ZV;
13017 new_pt_byte = ZV_BYTE;
13018 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13019 }
13020
13021 /* We don't use SET_PT so that the point-motion hooks don't run. */
13022 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13023 }
13024
13025 /* If any of the character widths specified in the display table
13026 have changed, invalidate the width run cache. It's true that
13027 this may be a bit late to catch such changes, but the rest of
13028 redisplay goes (non-fatally) haywire when the display table is
13029 changed, so why should we worry about doing any better? */
13030 if (current_buffer->width_run_cache)
13031 {
13032 struct Lisp_Char_Table *disptab = buffer_display_table ();
13033
13034 if (! disptab_matches_widthtab (disptab,
13035 XVECTOR (current_buffer->width_table)))
13036 {
13037 invalidate_region_cache (current_buffer,
13038 current_buffer->width_run_cache,
13039 BEG, Z);
13040 recompute_width_table (current_buffer, disptab);
13041 }
13042 }
13043
13044 /* If window-start is screwed up, choose a new one. */
13045 if (XMARKER (w->start)->buffer != current_buffer)
13046 goto recenter;
13047
13048 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13049
13050 /* If someone specified a new starting point but did not insist,
13051 check whether it can be used. */
13052 if (!NILP (w->optional_new_start)
13053 && CHARPOS (startp) >= BEGV
13054 && CHARPOS (startp) <= ZV)
13055 {
13056 w->optional_new_start = Qnil;
13057 start_display (&it, w, startp);
13058 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13059 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13060 if (IT_CHARPOS (it) == PT)
13061 w->force_start = Qt;
13062 /* IT may overshoot PT if text at PT is invisible. */
13063 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13064 w->force_start = Qt;
13065 }
13066
13067 force_start:
13068
13069 /* Handle case where place to start displaying has been specified,
13070 unless the specified location is outside the accessible range. */
13071 if (!NILP (w->force_start)
13072 || w->frozen_window_start_p)
13073 {
13074 /* We set this later on if we have to adjust point. */
13075 int new_vpos = -1;
13076 int val;
13077
13078 w->force_start = Qnil;
13079 w->vscroll = 0;
13080 w->window_end_valid = Qnil;
13081
13082 /* Forget any recorded base line for line number display. */
13083 if (!buffer_unchanged_p)
13084 w->base_line_number = Qnil;
13085
13086 /* Redisplay the mode line. Select the buffer properly for that.
13087 Also, run the hook window-scroll-functions
13088 because we have scrolled. */
13089 /* Note, we do this after clearing force_start because
13090 if there's an error, it is better to forget about force_start
13091 than to get into an infinite loop calling the hook functions
13092 and having them get more errors. */
13093 if (!update_mode_line
13094 || ! NILP (Vwindow_scroll_functions))
13095 {
13096 update_mode_line = 1;
13097 w->update_mode_line = Qt;
13098 startp = run_window_scroll_functions (window, startp);
13099 }
13100
13101 w->last_modified = make_number (0);
13102 w->last_overlay_modified = make_number (0);
13103 if (CHARPOS (startp) < BEGV)
13104 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13105 else if (CHARPOS (startp) > ZV)
13106 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13107
13108 /* Redisplay, then check if cursor has been set during the
13109 redisplay. Give up if new fonts were loaded. */
13110 val = try_window (window, startp, 1);
13111 if (!val)
13112 {
13113 w->force_start = Qt;
13114 clear_glyph_matrix (w->desired_matrix);
13115 goto need_larger_matrices;
13116 }
13117 /* Point was outside the scroll margins. */
13118 if (val < 0)
13119 new_vpos = window_box_height (w) / 2;
13120
13121 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13122 {
13123 /* If point does not appear, try to move point so it does
13124 appear. The desired matrix has been built above, so we
13125 can use it here. */
13126 new_vpos = window_box_height (w) / 2;
13127 }
13128
13129 if (!cursor_row_fully_visible_p (w, 0, 0))
13130 {
13131 /* Point does appear, but on a line partly visible at end of window.
13132 Move it back to a fully-visible line. */
13133 new_vpos = window_box_height (w);
13134 }
13135
13136 /* If we need to move point for either of the above reasons,
13137 now actually do it. */
13138 if (new_vpos >= 0)
13139 {
13140 struct glyph_row *row;
13141
13142 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13143 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13144 ++row;
13145
13146 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13147 MATRIX_ROW_START_BYTEPOS (row));
13148
13149 if (w != XWINDOW (selected_window))
13150 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13151 else if (current_buffer == old)
13152 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13153
13154 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13155
13156 /* If we are highlighting the region, then we just changed
13157 the region, so redisplay to show it. */
13158 if (!NILP (Vtransient_mark_mode)
13159 && !NILP (current_buffer->mark_active))
13160 {
13161 clear_glyph_matrix (w->desired_matrix);
13162 if (!try_window (window, startp, 0))
13163 goto need_larger_matrices;
13164 }
13165 }
13166
13167 #if GLYPH_DEBUG
13168 debug_method_add (w, "forced window start");
13169 #endif
13170 goto done;
13171 }
13172
13173 /* Handle case where text has not changed, only point, and it has
13174 not moved off the frame, and we are not retrying after hscroll.
13175 (current_matrix_up_to_date_p is nonzero when retrying.) */
13176 if (current_matrix_up_to_date_p
13177 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13178 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13179 {
13180 switch (rc)
13181 {
13182 case CURSOR_MOVEMENT_SUCCESS:
13183 used_current_matrix_p = 1;
13184 goto done;
13185
13186 #if 0 /* try_cursor_movement never returns this value. */
13187 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13188 goto need_larger_matrices;
13189 #endif
13190
13191 case CURSOR_MOVEMENT_MUST_SCROLL:
13192 goto try_to_scroll;
13193
13194 default:
13195 abort ();
13196 }
13197 }
13198 /* If current starting point was originally the beginning of a line
13199 but no longer is, find a new starting point. */
13200 else if (!NILP (w->start_at_line_beg)
13201 && !(CHARPOS (startp) <= BEGV
13202 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13203 {
13204 #if GLYPH_DEBUG
13205 debug_method_add (w, "recenter 1");
13206 #endif
13207 goto recenter;
13208 }
13209
13210 /* Try scrolling with try_window_id. Value is > 0 if update has
13211 been done, it is -1 if we know that the same window start will
13212 not work. It is 0 if unsuccessful for some other reason. */
13213 else if ((tem = try_window_id (w)) != 0)
13214 {
13215 #if GLYPH_DEBUG
13216 debug_method_add (w, "try_window_id %d", tem);
13217 #endif
13218
13219 if (fonts_changed_p)
13220 goto need_larger_matrices;
13221 if (tem > 0)
13222 goto done;
13223
13224 /* Otherwise try_window_id has returned -1 which means that we
13225 don't want the alternative below this comment to execute. */
13226 }
13227 else if (CHARPOS (startp) >= BEGV
13228 && CHARPOS (startp) <= ZV
13229 && PT >= CHARPOS (startp)
13230 && (CHARPOS (startp) < ZV
13231 /* Avoid starting at end of buffer. */
13232 || CHARPOS (startp) == BEGV
13233 || (XFASTINT (w->last_modified) >= MODIFF
13234 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13235 {
13236
13237 /* If first window line is a continuation line, and window start
13238 is inside the modified region, but the first change is before
13239 current window start, we must select a new window start.
13240
13241 However, if this is the result of a down-mouse event (e.g. by
13242 extending the mouse-drag-overlay), we don't want to select a
13243 new window start, since that would change the position under
13244 the mouse, resulting in an unwanted mouse-movement rather
13245 than a simple mouse-click. */
13246 if (NILP (w->start_at_line_beg)
13247 && NILP (do_mouse_tracking)
13248 && CHARPOS (startp) > BEGV
13249 && CHARPOS (startp) > BEG + beg_unchanged
13250 && CHARPOS (startp) <= Z - end_unchanged)
13251 {
13252 w->force_start = Qt;
13253 if (XMARKER (w->start)->buffer == current_buffer)
13254 compute_window_start_on_continuation_line (w);
13255 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13256 goto force_start;
13257 }
13258
13259 #if GLYPH_DEBUG
13260 debug_method_add (w, "same window start");
13261 #endif
13262
13263 /* Try to redisplay starting at same place as before.
13264 If point has not moved off frame, accept the results. */
13265 if (!current_matrix_up_to_date_p
13266 /* Don't use try_window_reusing_current_matrix in this case
13267 because a window scroll function can have changed the
13268 buffer. */
13269 || !NILP (Vwindow_scroll_functions)
13270 || MINI_WINDOW_P (w)
13271 || !(used_current_matrix_p
13272 = try_window_reusing_current_matrix (w)))
13273 {
13274 IF_DEBUG (debug_method_add (w, "1"));
13275 if (try_window (window, startp, 1) < 0)
13276 /* -1 means we need to scroll.
13277 0 means we need new matrices, but fonts_changed_p
13278 is set in that case, so we will detect it below. */
13279 goto try_to_scroll;
13280 }
13281
13282 if (fonts_changed_p)
13283 goto need_larger_matrices;
13284
13285 if (w->cursor.vpos >= 0)
13286 {
13287 if (!just_this_one_p
13288 || current_buffer->clip_changed
13289 || BEG_UNCHANGED < CHARPOS (startp))
13290 /* Forget any recorded base line for line number display. */
13291 w->base_line_number = Qnil;
13292
13293 if (!cursor_row_fully_visible_p (w, 1, 0))
13294 {
13295 clear_glyph_matrix (w->desired_matrix);
13296 last_line_misfit = 1;
13297 }
13298 /* Drop through and scroll. */
13299 else
13300 goto done;
13301 }
13302 else
13303 clear_glyph_matrix (w->desired_matrix);
13304 }
13305
13306 try_to_scroll:
13307
13308 w->last_modified = make_number (0);
13309 w->last_overlay_modified = make_number (0);
13310
13311 /* Redisplay the mode line. Select the buffer properly for that. */
13312 if (!update_mode_line)
13313 {
13314 update_mode_line = 1;
13315 w->update_mode_line = Qt;
13316 }
13317
13318 /* Try to scroll by specified few lines. */
13319 if ((scroll_conservatively
13320 || scroll_step
13321 || temp_scroll_step
13322 || NUMBERP (current_buffer->scroll_up_aggressively)
13323 || NUMBERP (current_buffer->scroll_down_aggressively))
13324 && !current_buffer->clip_changed
13325 && CHARPOS (startp) >= BEGV
13326 && CHARPOS (startp) <= ZV)
13327 {
13328 /* The function returns -1 if new fonts were loaded, 1 if
13329 successful, 0 if not successful. */
13330 int rc = try_scrolling (window, just_this_one_p,
13331 scroll_conservatively,
13332 scroll_step,
13333 temp_scroll_step, last_line_misfit);
13334 switch (rc)
13335 {
13336 case SCROLLING_SUCCESS:
13337 goto done;
13338
13339 case SCROLLING_NEED_LARGER_MATRICES:
13340 goto need_larger_matrices;
13341
13342 case SCROLLING_FAILED:
13343 break;
13344
13345 default:
13346 abort ();
13347 }
13348 }
13349
13350 /* Finally, just choose place to start which centers point */
13351
13352 recenter:
13353 if (centering_position < 0)
13354 centering_position = window_box_height (w) / 2;
13355
13356 #if GLYPH_DEBUG
13357 debug_method_add (w, "recenter");
13358 #endif
13359
13360 /* w->vscroll = 0; */
13361
13362 /* Forget any previously recorded base line for line number display. */
13363 if (!buffer_unchanged_p)
13364 w->base_line_number = Qnil;
13365
13366 /* Move backward half the height of the window. */
13367 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13368 it.current_y = it.last_visible_y;
13369 move_it_vertically_backward (&it, centering_position);
13370 xassert (IT_CHARPOS (it) >= BEGV);
13371
13372 /* The function move_it_vertically_backward may move over more
13373 than the specified y-distance. If it->w is small, e.g. a
13374 mini-buffer window, we may end up in front of the window's
13375 display area. Start displaying at the start of the line
13376 containing PT in this case. */
13377 if (it.current_y <= 0)
13378 {
13379 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13380 move_it_vertically_backward (&it, 0);
13381 #if 0
13382 /* I think this assert is bogus if buffer contains
13383 invisible text or images. KFS. */
13384 xassert (IT_CHARPOS (it) <= PT);
13385 #endif
13386 it.current_y = 0;
13387 }
13388
13389 it.current_x = it.hpos = 0;
13390
13391 /* Set startp here explicitly in case that helps avoid an infinite loop
13392 in case the window-scroll-functions functions get errors. */
13393 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13394
13395 /* Run scroll hooks. */
13396 startp = run_window_scroll_functions (window, it.current.pos);
13397
13398 /* Redisplay the window. */
13399 if (!current_matrix_up_to_date_p
13400 || windows_or_buffers_changed
13401 || cursor_type_changed
13402 /* Don't use try_window_reusing_current_matrix in this case
13403 because it can have changed the buffer. */
13404 || !NILP (Vwindow_scroll_functions)
13405 || !just_this_one_p
13406 || MINI_WINDOW_P (w)
13407 || !(used_current_matrix_p
13408 = try_window_reusing_current_matrix (w)))
13409 try_window (window, startp, 0);
13410
13411 /* If new fonts have been loaded (due to fontsets), give up. We
13412 have to start a new redisplay since we need to re-adjust glyph
13413 matrices. */
13414 if (fonts_changed_p)
13415 goto need_larger_matrices;
13416
13417 /* If cursor did not appear assume that the middle of the window is
13418 in the first line of the window. Do it again with the next line.
13419 (Imagine a window of height 100, displaying two lines of height
13420 60. Moving back 50 from it->last_visible_y will end in the first
13421 line.) */
13422 if (w->cursor.vpos < 0)
13423 {
13424 if (!NILP (w->window_end_valid)
13425 && PT >= Z - XFASTINT (w->window_end_pos))
13426 {
13427 clear_glyph_matrix (w->desired_matrix);
13428 move_it_by_lines (&it, 1, 0);
13429 try_window (window, it.current.pos, 0);
13430 }
13431 else if (PT < IT_CHARPOS (it))
13432 {
13433 clear_glyph_matrix (w->desired_matrix);
13434 move_it_by_lines (&it, -1, 0);
13435 try_window (window, it.current.pos, 0);
13436 }
13437 else
13438 {
13439 /* Not much we can do about it. */
13440 }
13441 }
13442
13443 /* Consider the following case: Window starts at BEGV, there is
13444 invisible, intangible text at BEGV, so that display starts at
13445 some point START > BEGV. It can happen that we are called with
13446 PT somewhere between BEGV and START. Try to handle that case. */
13447 if (w->cursor.vpos < 0)
13448 {
13449 struct glyph_row *row = w->current_matrix->rows;
13450 if (row->mode_line_p)
13451 ++row;
13452 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13453 }
13454
13455 if (!cursor_row_fully_visible_p (w, 0, 0))
13456 {
13457 /* If vscroll is enabled, disable it and try again. */
13458 if (w->vscroll)
13459 {
13460 w->vscroll = 0;
13461 clear_glyph_matrix (w->desired_matrix);
13462 goto recenter;
13463 }
13464
13465 /* If centering point failed to make the whole line visible,
13466 put point at the top instead. That has to make the whole line
13467 visible, if it can be done. */
13468 if (centering_position == 0)
13469 goto done;
13470
13471 clear_glyph_matrix (w->desired_matrix);
13472 centering_position = 0;
13473 goto recenter;
13474 }
13475
13476 done:
13477
13478 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13479 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13480 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13481 ? Qt : Qnil);
13482
13483 /* Display the mode line, if we must. */
13484 if ((update_mode_line
13485 /* If window not full width, must redo its mode line
13486 if (a) the window to its side is being redone and
13487 (b) we do a frame-based redisplay. This is a consequence
13488 of how inverted lines are drawn in frame-based redisplay. */
13489 || (!just_this_one_p
13490 && !FRAME_WINDOW_P (f)
13491 && !WINDOW_FULL_WIDTH_P (w))
13492 /* Line number to display. */
13493 || INTEGERP (w->base_line_pos)
13494 /* Column number is displayed and different from the one displayed. */
13495 || (!NILP (w->column_number_displayed)
13496 && (XFASTINT (w->column_number_displayed)
13497 != (int) current_column ()))) /* iftc */
13498 /* This means that the window has a mode line. */
13499 && (WINDOW_WANTS_MODELINE_P (w)
13500 || WINDOW_WANTS_HEADER_LINE_P (w)))
13501 {
13502 display_mode_lines (w);
13503
13504 /* If mode line height has changed, arrange for a thorough
13505 immediate redisplay using the correct mode line height. */
13506 if (WINDOW_WANTS_MODELINE_P (w)
13507 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13508 {
13509 fonts_changed_p = 1;
13510 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13511 = DESIRED_MODE_LINE_HEIGHT (w);
13512 }
13513
13514 /* If top line height has changed, arrange for a thorough
13515 immediate redisplay using the correct mode line height. */
13516 if (WINDOW_WANTS_HEADER_LINE_P (w)
13517 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13518 {
13519 fonts_changed_p = 1;
13520 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13521 = DESIRED_HEADER_LINE_HEIGHT (w);
13522 }
13523
13524 if (fonts_changed_p)
13525 goto need_larger_matrices;
13526 }
13527
13528 if (!line_number_displayed
13529 && !BUFFERP (w->base_line_pos))
13530 {
13531 w->base_line_pos = Qnil;
13532 w->base_line_number = Qnil;
13533 }
13534
13535 finish_menu_bars:
13536
13537 /* When we reach a frame's selected window, redo the frame's menu bar. */
13538 if (update_mode_line
13539 && EQ (FRAME_SELECTED_WINDOW (f), window))
13540 {
13541 int redisplay_menu_p = 0;
13542 int redisplay_tool_bar_p = 0;
13543
13544 if (FRAME_WINDOW_P (f))
13545 {
13546 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13547 || defined (USE_GTK)
13548 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13549 #else
13550 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13551 #endif
13552 }
13553 else
13554 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13555
13556 if (redisplay_menu_p)
13557 display_menu_bar (w);
13558
13559 #ifdef HAVE_WINDOW_SYSTEM
13560 if (FRAME_WINDOW_P (f))
13561 {
13562 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13563 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13564 #else
13565 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13566 && (FRAME_TOOL_BAR_LINES (f) > 0
13567 || !NILP (Vauto_resize_tool_bars));
13568 #endif
13569
13570 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13571 {
13572 extern int ignore_mouse_drag_p;
13573 ignore_mouse_drag_p = 1;
13574 }
13575 }
13576 #endif
13577 }
13578
13579 #ifdef HAVE_WINDOW_SYSTEM
13580 if (FRAME_WINDOW_P (f)
13581 && update_window_fringes (w, (just_this_one_p
13582 || (!used_current_matrix_p && !overlay_arrow_seen)
13583 || w->pseudo_window_p)))
13584 {
13585 update_begin (f);
13586 BLOCK_INPUT;
13587 if (draw_window_fringes (w, 1))
13588 x_draw_vertical_border (w);
13589 UNBLOCK_INPUT;
13590 update_end (f);
13591 }
13592 #endif /* HAVE_WINDOW_SYSTEM */
13593
13594 /* We go to this label, with fonts_changed_p nonzero,
13595 if it is necessary to try again using larger glyph matrices.
13596 We have to redeem the scroll bar even in this case,
13597 because the loop in redisplay_internal expects that. */
13598 need_larger_matrices:
13599 ;
13600 finish_scroll_bars:
13601
13602 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13603 {
13604 /* Set the thumb's position and size. */
13605 set_vertical_scroll_bar (w);
13606
13607 /* Note that we actually used the scroll bar attached to this
13608 window, so it shouldn't be deleted at the end of redisplay. */
13609 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13610 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13611 }
13612
13613 /* Restore current_buffer and value of point in it. */
13614 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13615 set_buffer_internal_1 (old);
13616 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13617 shorter. This can be caused by log truncation in *Messages*. */
13618 if (CHARPOS (lpoint) <= ZV)
13619 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13620
13621 unbind_to (count, Qnil);
13622 }
13623
13624
13625 /* Build the complete desired matrix of WINDOW with a window start
13626 buffer position POS.
13627
13628 Value is 1 if successful. It is zero if fonts were loaded during
13629 redisplay which makes re-adjusting glyph matrices necessary, and -1
13630 if point would appear in the scroll margins.
13631 (We check that only if CHECK_MARGINS is nonzero. */
13632
13633 int
13634 try_window (window, pos, check_margins)
13635 Lisp_Object window;
13636 struct text_pos pos;
13637 int check_margins;
13638 {
13639 struct window *w = XWINDOW (window);
13640 struct it it;
13641 struct glyph_row *last_text_row = NULL;
13642 struct frame *f = XFRAME (w->frame);
13643
13644 /* Make POS the new window start. */
13645 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13646
13647 /* Mark cursor position as unknown. No overlay arrow seen. */
13648 w->cursor.vpos = -1;
13649 overlay_arrow_seen = 0;
13650
13651 /* Initialize iterator and info to start at POS. */
13652 start_display (&it, w, pos);
13653
13654 /* Display all lines of W. */
13655 while (it.current_y < it.last_visible_y)
13656 {
13657 if (display_line (&it))
13658 last_text_row = it.glyph_row - 1;
13659 if (fonts_changed_p)
13660 return 0;
13661 }
13662
13663 /* Don't let the cursor end in the scroll margins. */
13664 if (check_margins
13665 && !MINI_WINDOW_P (w))
13666 {
13667 int this_scroll_margin;
13668
13669 this_scroll_margin = max (0, scroll_margin);
13670 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13671 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13672
13673 if ((w->cursor.y >= 0 /* not vscrolled */
13674 && w->cursor.y < this_scroll_margin
13675 && CHARPOS (pos) > BEGV
13676 && IT_CHARPOS (it) < ZV)
13677 /* rms: considering make_cursor_line_fully_visible_p here
13678 seems to give wrong results. We don't want to recenter
13679 when the last line is partly visible, we want to allow
13680 that case to be handled in the usual way. */
13681 || (w->cursor.y + 1) > it.last_visible_y)
13682 {
13683 w->cursor.vpos = -1;
13684 clear_glyph_matrix (w->desired_matrix);
13685 return -1;
13686 }
13687 }
13688
13689 /* If bottom moved off end of frame, change mode line percentage. */
13690 if (XFASTINT (w->window_end_pos) <= 0
13691 && Z != IT_CHARPOS (it))
13692 w->update_mode_line = Qt;
13693
13694 /* Set window_end_pos to the offset of the last character displayed
13695 on the window from the end of current_buffer. Set
13696 window_end_vpos to its row number. */
13697 if (last_text_row)
13698 {
13699 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13700 w->window_end_bytepos
13701 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13702 w->window_end_pos
13703 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13704 w->window_end_vpos
13705 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13706 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13707 ->displays_text_p);
13708 }
13709 else
13710 {
13711 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13712 w->window_end_pos = make_number (Z - ZV);
13713 w->window_end_vpos = make_number (0);
13714 }
13715
13716 /* But that is not valid info until redisplay finishes. */
13717 w->window_end_valid = Qnil;
13718 return 1;
13719 }
13720
13721
13722 \f
13723 /************************************************************************
13724 Window redisplay reusing current matrix when buffer has not changed
13725 ************************************************************************/
13726
13727 /* Try redisplay of window W showing an unchanged buffer with a
13728 different window start than the last time it was displayed by
13729 reusing its current matrix. Value is non-zero if successful.
13730 W->start is the new window start. */
13731
13732 static int
13733 try_window_reusing_current_matrix (w)
13734 struct window *w;
13735 {
13736 struct frame *f = XFRAME (w->frame);
13737 struct glyph_row *row, *bottom_row;
13738 struct it it;
13739 struct run run;
13740 struct text_pos start, new_start;
13741 int nrows_scrolled, i;
13742 struct glyph_row *last_text_row;
13743 struct glyph_row *last_reused_text_row;
13744 struct glyph_row *start_row;
13745 int start_vpos, min_y, max_y;
13746
13747 #if GLYPH_DEBUG
13748 if (inhibit_try_window_reusing)
13749 return 0;
13750 #endif
13751
13752 if (/* This function doesn't handle terminal frames. */
13753 !FRAME_WINDOW_P (f)
13754 /* Don't try to reuse the display if windows have been split
13755 or such. */
13756 || windows_or_buffers_changed
13757 || cursor_type_changed)
13758 return 0;
13759
13760 /* Can't do this if region may have changed. */
13761 if ((!NILP (Vtransient_mark_mode)
13762 && !NILP (current_buffer->mark_active))
13763 || !NILP (w->region_showing)
13764 || !NILP (Vshow_trailing_whitespace))
13765 return 0;
13766
13767 /* If top-line visibility has changed, give up. */
13768 if (WINDOW_WANTS_HEADER_LINE_P (w)
13769 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13770 return 0;
13771
13772 /* Give up if old or new display is scrolled vertically. We could
13773 make this function handle this, but right now it doesn't. */
13774 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13775 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13776 return 0;
13777
13778 /* The variable new_start now holds the new window start. The old
13779 start `start' can be determined from the current matrix. */
13780 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13781 start = start_row->start.pos;
13782 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13783
13784 /* Clear the desired matrix for the display below. */
13785 clear_glyph_matrix (w->desired_matrix);
13786
13787 if (CHARPOS (new_start) <= CHARPOS (start))
13788 {
13789 int first_row_y;
13790
13791 /* Don't use this method if the display starts with an ellipsis
13792 displayed for invisible text. It's not easy to handle that case
13793 below, and it's certainly not worth the effort since this is
13794 not a frequent case. */
13795 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13796 return 0;
13797
13798 IF_DEBUG (debug_method_add (w, "twu1"));
13799
13800 /* Display up to a row that can be reused. The variable
13801 last_text_row is set to the last row displayed that displays
13802 text. Note that it.vpos == 0 if or if not there is a
13803 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13804 start_display (&it, w, new_start);
13805 first_row_y = it.current_y;
13806 w->cursor.vpos = -1;
13807 last_text_row = last_reused_text_row = NULL;
13808
13809 while (it.current_y < it.last_visible_y
13810 && !fonts_changed_p)
13811 {
13812 /* If we have reached into the characters in the START row,
13813 that means the line boundaries have changed. So we
13814 can't start copying with the row START. Maybe it will
13815 work to start copying with the following row. */
13816 while (IT_CHARPOS (it) > CHARPOS (start))
13817 {
13818 /* Advance to the next row as the "start". */
13819 start_row++;
13820 start = start_row->start.pos;
13821 /* If there are no more rows to try, or just one, give up. */
13822 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13823 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13824 || CHARPOS (start) == ZV)
13825 {
13826 clear_glyph_matrix (w->desired_matrix);
13827 return 0;
13828 }
13829
13830 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13831 }
13832 /* If we have reached alignment,
13833 we can copy the rest of the rows. */
13834 if (IT_CHARPOS (it) == CHARPOS (start))
13835 break;
13836
13837 if (display_line (&it))
13838 last_text_row = it.glyph_row - 1;
13839 }
13840
13841 /* A value of current_y < last_visible_y means that we stopped
13842 at the previous window start, which in turn means that we
13843 have at least one reusable row. */
13844 if (it.current_y < it.last_visible_y)
13845 {
13846 /* IT.vpos always starts from 0; it counts text lines. */
13847 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13848
13849 /* Find PT if not already found in the lines displayed. */
13850 if (w->cursor.vpos < 0)
13851 {
13852 int dy = it.current_y - start_row->y;
13853
13854 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13855 row = row_containing_pos (w, PT, row, NULL, dy);
13856 if (row)
13857 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13858 dy, nrows_scrolled);
13859 else
13860 {
13861 clear_glyph_matrix (w->desired_matrix);
13862 return 0;
13863 }
13864 }
13865
13866 /* Scroll the display. Do it before the current matrix is
13867 changed. The problem here is that update has not yet
13868 run, i.e. part of the current matrix is not up to date.
13869 scroll_run_hook will clear the cursor, and use the
13870 current matrix to get the height of the row the cursor is
13871 in. */
13872 run.current_y = start_row->y;
13873 run.desired_y = it.current_y;
13874 run.height = it.last_visible_y - it.current_y;
13875
13876 if (run.height > 0 && run.current_y != run.desired_y)
13877 {
13878 update_begin (f);
13879 FRAME_RIF (f)->update_window_begin_hook (w);
13880 FRAME_RIF (f)->clear_window_mouse_face (w);
13881 FRAME_RIF (f)->scroll_run_hook (w, &run);
13882 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13883 update_end (f);
13884 }
13885
13886 /* Shift current matrix down by nrows_scrolled lines. */
13887 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13888 rotate_matrix (w->current_matrix,
13889 start_vpos,
13890 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13891 nrows_scrolled);
13892
13893 /* Disable lines that must be updated. */
13894 for (i = 0; i < nrows_scrolled; ++i)
13895 (start_row + i)->enabled_p = 0;
13896
13897 /* Re-compute Y positions. */
13898 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13899 max_y = it.last_visible_y;
13900 for (row = start_row + nrows_scrolled;
13901 row < bottom_row;
13902 ++row)
13903 {
13904 row->y = it.current_y;
13905 row->visible_height = row->height;
13906
13907 if (row->y < min_y)
13908 row->visible_height -= min_y - row->y;
13909 if (row->y + row->height > max_y)
13910 row->visible_height -= row->y + row->height - max_y;
13911 row->redraw_fringe_bitmaps_p = 1;
13912
13913 it.current_y += row->height;
13914
13915 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13916 last_reused_text_row = row;
13917 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13918 break;
13919 }
13920
13921 /* Disable lines in the current matrix which are now
13922 below the window. */
13923 for (++row; row < bottom_row; ++row)
13924 row->enabled_p = row->mode_line_p = 0;
13925 }
13926
13927 /* Update window_end_pos etc.; last_reused_text_row is the last
13928 reused row from the current matrix containing text, if any.
13929 The value of last_text_row is the last displayed line
13930 containing text. */
13931 if (last_reused_text_row)
13932 {
13933 w->window_end_bytepos
13934 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13935 w->window_end_pos
13936 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13937 w->window_end_vpos
13938 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13939 w->current_matrix));
13940 }
13941 else if (last_text_row)
13942 {
13943 w->window_end_bytepos
13944 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13945 w->window_end_pos
13946 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13947 w->window_end_vpos
13948 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13949 }
13950 else
13951 {
13952 /* This window must be completely empty. */
13953 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13954 w->window_end_pos = make_number (Z - ZV);
13955 w->window_end_vpos = make_number (0);
13956 }
13957 w->window_end_valid = Qnil;
13958
13959 /* Update hint: don't try scrolling again in update_window. */
13960 w->desired_matrix->no_scrolling_p = 1;
13961
13962 #if GLYPH_DEBUG
13963 debug_method_add (w, "try_window_reusing_current_matrix 1");
13964 #endif
13965 return 1;
13966 }
13967 else if (CHARPOS (new_start) > CHARPOS (start))
13968 {
13969 struct glyph_row *pt_row, *row;
13970 struct glyph_row *first_reusable_row;
13971 struct glyph_row *first_row_to_display;
13972 int dy;
13973 int yb = window_text_bottom_y (w);
13974
13975 /* Find the row starting at new_start, if there is one. Don't
13976 reuse a partially visible line at the end. */
13977 first_reusable_row = start_row;
13978 while (first_reusable_row->enabled_p
13979 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13980 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13981 < CHARPOS (new_start)))
13982 ++first_reusable_row;
13983
13984 /* Give up if there is no row to reuse. */
13985 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13986 || !first_reusable_row->enabled_p
13987 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13988 != CHARPOS (new_start)))
13989 return 0;
13990
13991 /* We can reuse fully visible rows beginning with
13992 first_reusable_row to the end of the window. Set
13993 first_row_to_display to the first row that cannot be reused.
13994 Set pt_row to the row containing point, if there is any. */
13995 pt_row = NULL;
13996 for (first_row_to_display = first_reusable_row;
13997 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13998 ++first_row_to_display)
13999 {
14000 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14001 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14002 pt_row = first_row_to_display;
14003 }
14004
14005 /* Start displaying at the start of first_row_to_display. */
14006 xassert (first_row_to_display->y < yb);
14007 init_to_row_start (&it, w, first_row_to_display);
14008
14009 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14010 - start_vpos);
14011 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14012 - nrows_scrolled);
14013 it.current_y = (first_row_to_display->y - first_reusable_row->y
14014 + WINDOW_HEADER_LINE_HEIGHT (w));
14015
14016 /* Display lines beginning with first_row_to_display in the
14017 desired matrix. Set last_text_row to the last row displayed
14018 that displays text. */
14019 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14020 if (pt_row == NULL)
14021 w->cursor.vpos = -1;
14022 last_text_row = NULL;
14023 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14024 if (display_line (&it))
14025 last_text_row = it.glyph_row - 1;
14026
14027 /* Give up If point isn't in a row displayed or reused. */
14028 if (w->cursor.vpos < 0)
14029 {
14030 clear_glyph_matrix (w->desired_matrix);
14031 return 0;
14032 }
14033
14034 /* If point is in a reused row, adjust y and vpos of the cursor
14035 position. */
14036 if (pt_row)
14037 {
14038 w->cursor.vpos -= nrows_scrolled;
14039 w->cursor.y -= first_reusable_row->y - start_row->y;
14040 }
14041
14042 /* Scroll the display. */
14043 run.current_y = first_reusable_row->y;
14044 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14045 run.height = it.last_visible_y - run.current_y;
14046 dy = run.current_y - run.desired_y;
14047
14048 if (run.height)
14049 {
14050 update_begin (f);
14051 FRAME_RIF (f)->update_window_begin_hook (w);
14052 FRAME_RIF (f)->clear_window_mouse_face (w);
14053 FRAME_RIF (f)->scroll_run_hook (w, &run);
14054 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14055 update_end (f);
14056 }
14057
14058 /* Adjust Y positions of reused rows. */
14059 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14060 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14061 max_y = it.last_visible_y;
14062 for (row = first_reusable_row; row < first_row_to_display; ++row)
14063 {
14064 row->y -= dy;
14065 row->visible_height = row->height;
14066 if (row->y < min_y)
14067 row->visible_height -= min_y - row->y;
14068 if (row->y + row->height > max_y)
14069 row->visible_height -= row->y + row->height - max_y;
14070 row->redraw_fringe_bitmaps_p = 1;
14071 }
14072
14073 /* Scroll the current matrix. */
14074 xassert (nrows_scrolled > 0);
14075 rotate_matrix (w->current_matrix,
14076 start_vpos,
14077 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14078 -nrows_scrolled);
14079
14080 /* Disable rows not reused. */
14081 for (row -= nrows_scrolled; row < bottom_row; ++row)
14082 row->enabled_p = 0;
14083
14084 /* Point may have moved to a different line, so we cannot assume that
14085 the previous cursor position is valid; locate the correct row. */
14086 if (pt_row)
14087 {
14088 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14089 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14090 row++)
14091 {
14092 w->cursor.vpos++;
14093 w->cursor.y = row->y;
14094 }
14095 if (row < bottom_row)
14096 {
14097 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14098 while (glyph->charpos < PT)
14099 {
14100 w->cursor.hpos++;
14101 w->cursor.x += glyph->pixel_width;
14102 glyph++;
14103 }
14104 }
14105 }
14106
14107 /* Adjust window end. A null value of last_text_row means that
14108 the window end is in reused rows which in turn means that
14109 only its vpos can have changed. */
14110 if (last_text_row)
14111 {
14112 w->window_end_bytepos
14113 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14114 w->window_end_pos
14115 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14116 w->window_end_vpos
14117 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14118 }
14119 else
14120 {
14121 w->window_end_vpos
14122 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14123 }
14124
14125 w->window_end_valid = Qnil;
14126 w->desired_matrix->no_scrolling_p = 1;
14127
14128 #if GLYPH_DEBUG
14129 debug_method_add (w, "try_window_reusing_current_matrix 2");
14130 #endif
14131 return 1;
14132 }
14133
14134 return 0;
14135 }
14136
14137
14138 \f
14139 /************************************************************************
14140 Window redisplay reusing current matrix when buffer has changed
14141 ************************************************************************/
14142
14143 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14144 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14145 int *, int *));
14146 static struct glyph_row *
14147 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14148 struct glyph_row *));
14149
14150
14151 /* Return the last row in MATRIX displaying text. If row START is
14152 non-null, start searching with that row. IT gives the dimensions
14153 of the display. Value is null if matrix is empty; otherwise it is
14154 a pointer to the row found. */
14155
14156 static struct glyph_row *
14157 find_last_row_displaying_text (matrix, it, start)
14158 struct glyph_matrix *matrix;
14159 struct it *it;
14160 struct glyph_row *start;
14161 {
14162 struct glyph_row *row, *row_found;
14163
14164 /* Set row_found to the last row in IT->w's current matrix
14165 displaying text. The loop looks funny but think of partially
14166 visible lines. */
14167 row_found = NULL;
14168 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14169 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14170 {
14171 xassert (row->enabled_p);
14172 row_found = row;
14173 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14174 break;
14175 ++row;
14176 }
14177
14178 return row_found;
14179 }
14180
14181
14182 /* Return the last row in the current matrix of W that is not affected
14183 by changes at the start of current_buffer that occurred since W's
14184 current matrix was built. Value is null if no such row exists.
14185
14186 BEG_UNCHANGED us the number of characters unchanged at the start of
14187 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14188 first changed character in current_buffer. Characters at positions <
14189 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14190 when the current matrix was built. */
14191
14192 static struct glyph_row *
14193 find_last_unchanged_at_beg_row (w)
14194 struct window *w;
14195 {
14196 int first_changed_pos = BEG + BEG_UNCHANGED;
14197 struct glyph_row *row;
14198 struct glyph_row *row_found = NULL;
14199 int yb = window_text_bottom_y (w);
14200
14201 /* Find the last row displaying unchanged text. */
14202 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14203 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14204 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14205 {
14206 if (/* If row ends before first_changed_pos, it is unchanged,
14207 except in some case. */
14208 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14209 /* When row ends in ZV and we write at ZV it is not
14210 unchanged. */
14211 && !row->ends_at_zv_p
14212 /* When first_changed_pos is the end of a continued line,
14213 row is not unchanged because it may be no longer
14214 continued. */
14215 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14216 && (row->continued_p
14217 || row->exact_window_width_line_p)))
14218 row_found = row;
14219
14220 /* Stop if last visible row. */
14221 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14222 break;
14223
14224 ++row;
14225 }
14226
14227 return row_found;
14228 }
14229
14230
14231 /* Find the first glyph row in the current matrix of W that is not
14232 affected by changes at the end of current_buffer since the
14233 time W's current matrix was built.
14234
14235 Return in *DELTA the number of chars by which buffer positions in
14236 unchanged text at the end of current_buffer must be adjusted.
14237
14238 Return in *DELTA_BYTES the corresponding number of bytes.
14239
14240 Value is null if no such row exists, i.e. all rows are affected by
14241 changes. */
14242
14243 static struct glyph_row *
14244 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14245 struct window *w;
14246 int *delta, *delta_bytes;
14247 {
14248 struct glyph_row *row;
14249 struct glyph_row *row_found = NULL;
14250
14251 *delta = *delta_bytes = 0;
14252
14253 /* Display must not have been paused, otherwise the current matrix
14254 is not up to date. */
14255 if (NILP (w->window_end_valid))
14256 abort ();
14257
14258 /* A value of window_end_pos >= END_UNCHANGED means that the window
14259 end is in the range of changed text. If so, there is no
14260 unchanged row at the end of W's current matrix. */
14261 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14262 return NULL;
14263
14264 /* Set row to the last row in W's current matrix displaying text. */
14265 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14266
14267 /* If matrix is entirely empty, no unchanged row exists. */
14268 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14269 {
14270 /* The value of row is the last glyph row in the matrix having a
14271 meaningful buffer position in it. The end position of row
14272 corresponds to window_end_pos. This allows us to translate
14273 buffer positions in the current matrix to current buffer
14274 positions for characters not in changed text. */
14275 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14276 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14277 int last_unchanged_pos, last_unchanged_pos_old;
14278 struct glyph_row *first_text_row
14279 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14280
14281 *delta = Z - Z_old;
14282 *delta_bytes = Z_BYTE - Z_BYTE_old;
14283
14284 /* Set last_unchanged_pos to the buffer position of the last
14285 character in the buffer that has not been changed. Z is the
14286 index + 1 of the last character in current_buffer, i.e. by
14287 subtracting END_UNCHANGED we get the index of the last
14288 unchanged character, and we have to add BEG to get its buffer
14289 position. */
14290 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14291 last_unchanged_pos_old = last_unchanged_pos - *delta;
14292
14293 /* Search backward from ROW for a row displaying a line that
14294 starts at a minimum position >= last_unchanged_pos_old. */
14295 for (; row > first_text_row; --row)
14296 {
14297 /* This used to abort, but it can happen.
14298 It is ok to just stop the search instead here. KFS. */
14299 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14300 break;
14301
14302 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14303 row_found = row;
14304 }
14305 }
14306
14307 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14308 abort ();
14309
14310 return row_found;
14311 }
14312
14313
14314 /* Make sure that glyph rows in the current matrix of window W
14315 reference the same glyph memory as corresponding rows in the
14316 frame's frame matrix. This function is called after scrolling W's
14317 current matrix on a terminal frame in try_window_id and
14318 try_window_reusing_current_matrix. */
14319
14320 static void
14321 sync_frame_with_window_matrix_rows (w)
14322 struct window *w;
14323 {
14324 struct frame *f = XFRAME (w->frame);
14325 struct glyph_row *window_row, *window_row_end, *frame_row;
14326
14327 /* Preconditions: W must be a leaf window and full-width. Its frame
14328 must have a frame matrix. */
14329 xassert (NILP (w->hchild) && NILP (w->vchild));
14330 xassert (WINDOW_FULL_WIDTH_P (w));
14331 xassert (!FRAME_WINDOW_P (f));
14332
14333 /* If W is a full-width window, glyph pointers in W's current matrix
14334 have, by definition, to be the same as glyph pointers in the
14335 corresponding frame matrix. Note that frame matrices have no
14336 marginal areas (see build_frame_matrix). */
14337 window_row = w->current_matrix->rows;
14338 window_row_end = window_row + w->current_matrix->nrows;
14339 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14340 while (window_row < window_row_end)
14341 {
14342 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14343 struct glyph *end = window_row->glyphs[LAST_AREA];
14344
14345 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14346 frame_row->glyphs[TEXT_AREA] = start;
14347 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14348 frame_row->glyphs[LAST_AREA] = end;
14349
14350 /* Disable frame rows whose corresponding window rows have
14351 been disabled in try_window_id. */
14352 if (!window_row->enabled_p)
14353 frame_row->enabled_p = 0;
14354
14355 ++window_row, ++frame_row;
14356 }
14357 }
14358
14359
14360 /* Find the glyph row in window W containing CHARPOS. Consider all
14361 rows between START and END (not inclusive). END null means search
14362 all rows to the end of the display area of W. Value is the row
14363 containing CHARPOS or null. */
14364
14365 struct glyph_row *
14366 row_containing_pos (w, charpos, start, end, dy)
14367 struct window *w;
14368 int charpos;
14369 struct glyph_row *start, *end;
14370 int dy;
14371 {
14372 struct glyph_row *row = start;
14373 int last_y;
14374
14375 /* If we happen to start on a header-line, skip that. */
14376 if (row->mode_line_p)
14377 ++row;
14378
14379 if ((end && row >= end) || !row->enabled_p)
14380 return NULL;
14381
14382 last_y = window_text_bottom_y (w) - dy;
14383
14384 while (1)
14385 {
14386 /* Give up if we have gone too far. */
14387 if (end && row >= end)
14388 return NULL;
14389 /* This formerly returned if they were equal.
14390 I think that both quantities are of a "last plus one" type;
14391 if so, when they are equal, the row is within the screen. -- rms. */
14392 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14393 return NULL;
14394
14395 /* If it is in this row, return this row. */
14396 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14397 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14398 /* The end position of a row equals the start
14399 position of the next row. If CHARPOS is there, we
14400 would rather display it in the next line, except
14401 when this line ends in ZV. */
14402 && !row->ends_at_zv_p
14403 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14404 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14405 return row;
14406 ++row;
14407 }
14408 }
14409
14410
14411 /* Try to redisplay window W by reusing its existing display. W's
14412 current matrix must be up to date when this function is called,
14413 i.e. window_end_valid must not be nil.
14414
14415 Value is
14416
14417 1 if display has been updated
14418 0 if otherwise unsuccessful
14419 -1 if redisplay with same window start is known not to succeed
14420
14421 The following steps are performed:
14422
14423 1. Find the last row in the current matrix of W that is not
14424 affected by changes at the start of current_buffer. If no such row
14425 is found, give up.
14426
14427 2. Find the first row in W's current matrix that is not affected by
14428 changes at the end of current_buffer. Maybe there is no such row.
14429
14430 3. Display lines beginning with the row + 1 found in step 1 to the
14431 row found in step 2 or, if step 2 didn't find a row, to the end of
14432 the window.
14433
14434 4. If cursor is not known to appear on the window, give up.
14435
14436 5. If display stopped at the row found in step 2, scroll the
14437 display and current matrix as needed.
14438
14439 6. Maybe display some lines at the end of W, if we must. This can
14440 happen under various circumstances, like a partially visible line
14441 becoming fully visible, or because newly displayed lines are displayed
14442 in smaller font sizes.
14443
14444 7. Update W's window end information. */
14445
14446 static int
14447 try_window_id (w)
14448 struct window *w;
14449 {
14450 struct frame *f = XFRAME (w->frame);
14451 struct glyph_matrix *current_matrix = w->current_matrix;
14452 struct glyph_matrix *desired_matrix = w->desired_matrix;
14453 struct glyph_row *last_unchanged_at_beg_row;
14454 struct glyph_row *first_unchanged_at_end_row;
14455 struct glyph_row *row;
14456 struct glyph_row *bottom_row;
14457 int bottom_vpos;
14458 struct it it;
14459 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14460 struct text_pos start_pos;
14461 struct run run;
14462 int first_unchanged_at_end_vpos = 0;
14463 struct glyph_row *last_text_row, *last_text_row_at_end;
14464 struct text_pos start;
14465 int first_changed_charpos, last_changed_charpos;
14466
14467 #if GLYPH_DEBUG
14468 if (inhibit_try_window_id)
14469 return 0;
14470 #endif
14471
14472 /* This is handy for debugging. */
14473 #if 0
14474 #define GIVE_UP(X) \
14475 do { \
14476 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14477 return 0; \
14478 } while (0)
14479 #else
14480 #define GIVE_UP(X) return 0
14481 #endif
14482
14483 SET_TEXT_POS_FROM_MARKER (start, w->start);
14484
14485 /* Don't use this for mini-windows because these can show
14486 messages and mini-buffers, and we don't handle that here. */
14487 if (MINI_WINDOW_P (w))
14488 GIVE_UP (1);
14489
14490 /* This flag is used to prevent redisplay optimizations. */
14491 if (windows_or_buffers_changed || cursor_type_changed)
14492 GIVE_UP (2);
14493
14494 /* Verify that narrowing has not changed.
14495 Also verify that we were not told to prevent redisplay optimizations.
14496 It would be nice to further
14497 reduce the number of cases where this prevents try_window_id. */
14498 if (current_buffer->clip_changed
14499 || current_buffer->prevent_redisplay_optimizations_p)
14500 GIVE_UP (3);
14501
14502 /* Window must either use window-based redisplay or be full width. */
14503 if (!FRAME_WINDOW_P (f)
14504 && (!FRAME_LINE_INS_DEL_OK (f)
14505 || !WINDOW_FULL_WIDTH_P (w)))
14506 GIVE_UP (4);
14507
14508 /* Give up if point is not known NOT to appear in W. */
14509 if (PT < CHARPOS (start))
14510 GIVE_UP (5);
14511
14512 /* Another way to prevent redisplay optimizations. */
14513 if (XFASTINT (w->last_modified) == 0)
14514 GIVE_UP (6);
14515
14516 /* Verify that window is not hscrolled. */
14517 if (XFASTINT (w->hscroll) != 0)
14518 GIVE_UP (7);
14519
14520 /* Verify that display wasn't paused. */
14521 if (NILP (w->window_end_valid))
14522 GIVE_UP (8);
14523
14524 /* Can't use this if highlighting a region because a cursor movement
14525 will do more than just set the cursor. */
14526 if (!NILP (Vtransient_mark_mode)
14527 && !NILP (current_buffer->mark_active))
14528 GIVE_UP (9);
14529
14530 /* Likewise if highlighting trailing whitespace. */
14531 if (!NILP (Vshow_trailing_whitespace))
14532 GIVE_UP (11);
14533
14534 /* Likewise if showing a region. */
14535 if (!NILP (w->region_showing))
14536 GIVE_UP (10);
14537
14538 /* Can use this if overlay arrow position and or string have changed. */
14539 if (overlay_arrows_changed_p ())
14540 GIVE_UP (12);
14541
14542
14543 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14544 only if buffer has really changed. The reason is that the gap is
14545 initially at Z for freshly visited files. The code below would
14546 set end_unchanged to 0 in that case. */
14547 if (MODIFF > SAVE_MODIFF
14548 /* This seems to happen sometimes after saving a buffer. */
14549 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14550 {
14551 if (GPT - BEG < BEG_UNCHANGED)
14552 BEG_UNCHANGED = GPT - BEG;
14553 if (Z - GPT < END_UNCHANGED)
14554 END_UNCHANGED = Z - GPT;
14555 }
14556
14557 /* The position of the first and last character that has been changed. */
14558 first_changed_charpos = BEG + BEG_UNCHANGED;
14559 last_changed_charpos = Z - END_UNCHANGED;
14560
14561 /* If window starts after a line end, and the last change is in
14562 front of that newline, then changes don't affect the display.
14563 This case happens with stealth-fontification. Note that although
14564 the display is unchanged, glyph positions in the matrix have to
14565 be adjusted, of course. */
14566 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14567 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14568 && ((last_changed_charpos < CHARPOS (start)
14569 && CHARPOS (start) == BEGV)
14570 || (last_changed_charpos < CHARPOS (start) - 1
14571 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14572 {
14573 int Z_old, delta, Z_BYTE_old, delta_bytes;
14574 struct glyph_row *r0;
14575
14576 /* Compute how many chars/bytes have been added to or removed
14577 from the buffer. */
14578 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14579 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14580 delta = Z - Z_old;
14581 delta_bytes = Z_BYTE - Z_BYTE_old;
14582
14583 /* Give up if PT is not in the window. Note that it already has
14584 been checked at the start of try_window_id that PT is not in
14585 front of the window start. */
14586 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14587 GIVE_UP (13);
14588
14589 /* If window start is unchanged, we can reuse the whole matrix
14590 as is, after adjusting glyph positions. No need to compute
14591 the window end again, since its offset from Z hasn't changed. */
14592 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14593 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14594 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14595 /* PT must not be in a partially visible line. */
14596 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14597 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14598 {
14599 /* Adjust positions in the glyph matrix. */
14600 if (delta || delta_bytes)
14601 {
14602 struct glyph_row *r1
14603 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14604 increment_matrix_positions (w->current_matrix,
14605 MATRIX_ROW_VPOS (r0, current_matrix),
14606 MATRIX_ROW_VPOS (r1, current_matrix),
14607 delta, delta_bytes);
14608 }
14609
14610 /* Set the cursor. */
14611 row = row_containing_pos (w, PT, r0, NULL, 0);
14612 if (row)
14613 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14614 else
14615 abort ();
14616 return 1;
14617 }
14618 }
14619
14620 /* Handle the case that changes are all below what is displayed in
14621 the window, and that PT is in the window. This shortcut cannot
14622 be taken if ZV is visible in the window, and text has been added
14623 there that is visible in the window. */
14624 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14625 /* ZV is not visible in the window, or there are no
14626 changes at ZV, actually. */
14627 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14628 || first_changed_charpos == last_changed_charpos))
14629 {
14630 struct glyph_row *r0;
14631
14632 /* Give up if PT is not in the window. Note that it already has
14633 been checked at the start of try_window_id that PT is not in
14634 front of the window start. */
14635 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14636 GIVE_UP (14);
14637
14638 /* If window start is unchanged, we can reuse the whole matrix
14639 as is, without changing glyph positions since no text has
14640 been added/removed in front of the window end. */
14641 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14642 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14643 /* PT must not be in a partially visible line. */
14644 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14645 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14646 {
14647 /* We have to compute the window end anew since text
14648 can have been added/removed after it. */
14649 w->window_end_pos
14650 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14651 w->window_end_bytepos
14652 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14653
14654 /* Set the cursor. */
14655 row = row_containing_pos (w, PT, r0, NULL, 0);
14656 if (row)
14657 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14658 else
14659 abort ();
14660 return 2;
14661 }
14662 }
14663
14664 /* Give up if window start is in the changed area.
14665
14666 The condition used to read
14667
14668 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14669
14670 but why that was tested escapes me at the moment. */
14671 if (CHARPOS (start) >= first_changed_charpos
14672 && CHARPOS (start) <= last_changed_charpos)
14673 GIVE_UP (15);
14674
14675 /* Check that window start agrees with the start of the first glyph
14676 row in its current matrix. Check this after we know the window
14677 start is not in changed text, otherwise positions would not be
14678 comparable. */
14679 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14680 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14681 GIVE_UP (16);
14682
14683 /* Give up if the window ends in strings. Overlay strings
14684 at the end are difficult to handle, so don't try. */
14685 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14686 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14687 GIVE_UP (20);
14688
14689 /* Compute the position at which we have to start displaying new
14690 lines. Some of the lines at the top of the window might be
14691 reusable because they are not displaying changed text. Find the
14692 last row in W's current matrix not affected by changes at the
14693 start of current_buffer. Value is null if changes start in the
14694 first line of window. */
14695 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14696 if (last_unchanged_at_beg_row)
14697 {
14698 /* Avoid starting to display in the moddle of a character, a TAB
14699 for instance. This is easier than to set up the iterator
14700 exactly, and it's not a frequent case, so the additional
14701 effort wouldn't really pay off. */
14702 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14703 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14704 && last_unchanged_at_beg_row > w->current_matrix->rows)
14705 --last_unchanged_at_beg_row;
14706
14707 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14708 GIVE_UP (17);
14709
14710 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14711 GIVE_UP (18);
14712 start_pos = it.current.pos;
14713
14714 /* Start displaying new lines in the desired matrix at the same
14715 vpos we would use in the current matrix, i.e. below
14716 last_unchanged_at_beg_row. */
14717 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14718 current_matrix);
14719 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14720 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14721
14722 xassert (it.hpos == 0 && it.current_x == 0);
14723 }
14724 else
14725 {
14726 /* There are no reusable lines at the start of the window.
14727 Start displaying in the first text line. */
14728 start_display (&it, w, start);
14729 it.vpos = it.first_vpos;
14730 start_pos = it.current.pos;
14731 }
14732
14733 /* Find the first row that is not affected by changes at the end of
14734 the buffer. Value will be null if there is no unchanged row, in
14735 which case we must redisplay to the end of the window. delta
14736 will be set to the value by which buffer positions beginning with
14737 first_unchanged_at_end_row have to be adjusted due to text
14738 changes. */
14739 first_unchanged_at_end_row
14740 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14741 IF_DEBUG (debug_delta = delta);
14742 IF_DEBUG (debug_delta_bytes = delta_bytes);
14743
14744 /* Set stop_pos to the buffer position up to which we will have to
14745 display new lines. If first_unchanged_at_end_row != NULL, this
14746 is the buffer position of the start of the line displayed in that
14747 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14748 that we don't stop at a buffer position. */
14749 stop_pos = 0;
14750 if (first_unchanged_at_end_row)
14751 {
14752 xassert (last_unchanged_at_beg_row == NULL
14753 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14754
14755 /* If this is a continuation line, move forward to the next one
14756 that isn't. Changes in lines above affect this line.
14757 Caution: this may move first_unchanged_at_end_row to a row
14758 not displaying text. */
14759 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14760 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14761 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14762 < it.last_visible_y))
14763 ++first_unchanged_at_end_row;
14764
14765 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14766 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14767 >= it.last_visible_y))
14768 first_unchanged_at_end_row = NULL;
14769 else
14770 {
14771 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14772 + delta);
14773 first_unchanged_at_end_vpos
14774 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14775 xassert (stop_pos >= Z - END_UNCHANGED);
14776 }
14777 }
14778 else if (last_unchanged_at_beg_row == NULL)
14779 GIVE_UP (19);
14780
14781
14782 #if GLYPH_DEBUG
14783
14784 /* Either there is no unchanged row at the end, or the one we have
14785 now displays text. This is a necessary condition for the window
14786 end pos calculation at the end of this function. */
14787 xassert (first_unchanged_at_end_row == NULL
14788 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14789
14790 debug_last_unchanged_at_beg_vpos
14791 = (last_unchanged_at_beg_row
14792 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14793 : -1);
14794 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14795
14796 #endif /* GLYPH_DEBUG != 0 */
14797
14798
14799 /* Display new lines. Set last_text_row to the last new line
14800 displayed which has text on it, i.e. might end up as being the
14801 line where the window_end_vpos is. */
14802 w->cursor.vpos = -1;
14803 last_text_row = NULL;
14804 overlay_arrow_seen = 0;
14805 while (it.current_y < it.last_visible_y
14806 && !fonts_changed_p
14807 && (first_unchanged_at_end_row == NULL
14808 || IT_CHARPOS (it) < stop_pos))
14809 {
14810 if (display_line (&it))
14811 last_text_row = it.glyph_row - 1;
14812 }
14813
14814 if (fonts_changed_p)
14815 return -1;
14816
14817
14818 /* Compute differences in buffer positions, y-positions etc. for
14819 lines reused at the bottom of the window. Compute what we can
14820 scroll. */
14821 if (first_unchanged_at_end_row
14822 /* No lines reused because we displayed everything up to the
14823 bottom of the window. */
14824 && it.current_y < it.last_visible_y)
14825 {
14826 dvpos = (it.vpos
14827 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14828 current_matrix));
14829 dy = it.current_y - first_unchanged_at_end_row->y;
14830 run.current_y = first_unchanged_at_end_row->y;
14831 run.desired_y = run.current_y + dy;
14832 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14833 }
14834 else
14835 {
14836 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14837 first_unchanged_at_end_row = NULL;
14838 }
14839 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14840
14841
14842 /* Find the cursor if not already found. We have to decide whether
14843 PT will appear on this window (it sometimes doesn't, but this is
14844 not a very frequent case.) This decision has to be made before
14845 the current matrix is altered. A value of cursor.vpos < 0 means
14846 that PT is either in one of the lines beginning at
14847 first_unchanged_at_end_row or below the window. Don't care for
14848 lines that might be displayed later at the window end; as
14849 mentioned, this is not a frequent case. */
14850 if (w->cursor.vpos < 0)
14851 {
14852 /* Cursor in unchanged rows at the top? */
14853 if (PT < CHARPOS (start_pos)
14854 && last_unchanged_at_beg_row)
14855 {
14856 row = row_containing_pos (w, PT,
14857 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14858 last_unchanged_at_beg_row + 1, 0);
14859 if (row)
14860 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14861 }
14862
14863 /* Start from first_unchanged_at_end_row looking for PT. */
14864 else if (first_unchanged_at_end_row)
14865 {
14866 row = row_containing_pos (w, PT - delta,
14867 first_unchanged_at_end_row, NULL, 0);
14868 if (row)
14869 set_cursor_from_row (w, row, w->current_matrix, delta,
14870 delta_bytes, dy, dvpos);
14871 }
14872
14873 /* Give up if cursor was not found. */
14874 if (w->cursor.vpos < 0)
14875 {
14876 clear_glyph_matrix (w->desired_matrix);
14877 return -1;
14878 }
14879 }
14880
14881 /* Don't let the cursor end in the scroll margins. */
14882 {
14883 int this_scroll_margin, cursor_height;
14884
14885 this_scroll_margin = max (0, scroll_margin);
14886 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14887 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14888 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14889
14890 if ((w->cursor.y < this_scroll_margin
14891 && CHARPOS (start) > BEGV)
14892 /* Old redisplay didn't take scroll margin into account at the bottom,
14893 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14894 || (w->cursor.y + (make_cursor_line_fully_visible_p
14895 ? cursor_height + this_scroll_margin
14896 : 1)) > it.last_visible_y)
14897 {
14898 w->cursor.vpos = -1;
14899 clear_glyph_matrix (w->desired_matrix);
14900 return -1;
14901 }
14902 }
14903
14904 /* Scroll the display. Do it before changing the current matrix so
14905 that xterm.c doesn't get confused about where the cursor glyph is
14906 found. */
14907 if (dy && run.height)
14908 {
14909 update_begin (f);
14910
14911 if (FRAME_WINDOW_P (f))
14912 {
14913 FRAME_RIF (f)->update_window_begin_hook (w);
14914 FRAME_RIF (f)->clear_window_mouse_face (w);
14915 FRAME_RIF (f)->scroll_run_hook (w, &run);
14916 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14917 }
14918 else
14919 {
14920 /* Terminal frame. In this case, dvpos gives the number of
14921 lines to scroll by; dvpos < 0 means scroll up. */
14922 int first_unchanged_at_end_vpos
14923 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14924 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14925 int end = (WINDOW_TOP_EDGE_LINE (w)
14926 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14927 + window_internal_height (w));
14928
14929 /* Perform the operation on the screen. */
14930 if (dvpos > 0)
14931 {
14932 /* Scroll last_unchanged_at_beg_row to the end of the
14933 window down dvpos lines. */
14934 set_terminal_window (f, end);
14935
14936 /* On dumb terminals delete dvpos lines at the end
14937 before inserting dvpos empty lines. */
14938 if (!FRAME_SCROLL_REGION_OK (f))
14939 ins_del_lines (f, end - dvpos, -dvpos);
14940
14941 /* Insert dvpos empty lines in front of
14942 last_unchanged_at_beg_row. */
14943 ins_del_lines (f, from, dvpos);
14944 }
14945 else if (dvpos < 0)
14946 {
14947 /* Scroll up last_unchanged_at_beg_vpos to the end of
14948 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14949 set_terminal_window (f, end);
14950
14951 /* Delete dvpos lines in front of
14952 last_unchanged_at_beg_vpos. ins_del_lines will set
14953 the cursor to the given vpos and emit |dvpos| delete
14954 line sequences. */
14955 ins_del_lines (f, from + dvpos, dvpos);
14956
14957 /* On a dumb terminal insert dvpos empty lines at the
14958 end. */
14959 if (!FRAME_SCROLL_REGION_OK (f))
14960 ins_del_lines (f, end + dvpos, -dvpos);
14961 }
14962
14963 set_terminal_window (f, 0);
14964 }
14965
14966 update_end (f);
14967 }
14968
14969 /* Shift reused rows of the current matrix to the right position.
14970 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14971 text. */
14972 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14973 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14974 if (dvpos < 0)
14975 {
14976 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14977 bottom_vpos, dvpos);
14978 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14979 bottom_vpos, 0);
14980 }
14981 else if (dvpos > 0)
14982 {
14983 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14984 bottom_vpos, dvpos);
14985 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14986 first_unchanged_at_end_vpos + dvpos, 0);
14987 }
14988
14989 /* For frame-based redisplay, make sure that current frame and window
14990 matrix are in sync with respect to glyph memory. */
14991 if (!FRAME_WINDOW_P (f))
14992 sync_frame_with_window_matrix_rows (w);
14993
14994 /* Adjust buffer positions in reused rows. */
14995 if (delta || delta_bytes)
14996 increment_matrix_positions (current_matrix,
14997 first_unchanged_at_end_vpos + dvpos,
14998 bottom_vpos, delta, delta_bytes);
14999
15000 /* Adjust Y positions. */
15001 if (dy)
15002 shift_glyph_matrix (w, current_matrix,
15003 first_unchanged_at_end_vpos + dvpos,
15004 bottom_vpos, dy);
15005
15006 if (first_unchanged_at_end_row)
15007 {
15008 first_unchanged_at_end_row += dvpos;
15009 if (first_unchanged_at_end_row->y >= it.last_visible_y
15010 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15011 first_unchanged_at_end_row = NULL;
15012 }
15013
15014 /* If scrolling up, there may be some lines to display at the end of
15015 the window. */
15016 last_text_row_at_end = NULL;
15017 if (dy < 0)
15018 {
15019 /* Scrolling up can leave for example a partially visible line
15020 at the end of the window to be redisplayed. */
15021 /* Set last_row to the glyph row in the current matrix where the
15022 window end line is found. It has been moved up or down in
15023 the matrix by dvpos. */
15024 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15025 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15026
15027 /* If last_row is the window end line, it should display text. */
15028 xassert (last_row->displays_text_p);
15029
15030 /* If window end line was partially visible before, begin
15031 displaying at that line. Otherwise begin displaying with the
15032 line following it. */
15033 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15034 {
15035 init_to_row_start (&it, w, last_row);
15036 it.vpos = last_vpos;
15037 it.current_y = last_row->y;
15038 }
15039 else
15040 {
15041 init_to_row_end (&it, w, last_row);
15042 it.vpos = 1 + last_vpos;
15043 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15044 ++last_row;
15045 }
15046
15047 /* We may start in a continuation line. If so, we have to
15048 get the right continuation_lines_width and current_x. */
15049 it.continuation_lines_width = last_row->continuation_lines_width;
15050 it.hpos = it.current_x = 0;
15051
15052 /* Display the rest of the lines at the window end. */
15053 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15054 while (it.current_y < it.last_visible_y
15055 && !fonts_changed_p)
15056 {
15057 /* Is it always sure that the display agrees with lines in
15058 the current matrix? I don't think so, so we mark rows
15059 displayed invalid in the current matrix by setting their
15060 enabled_p flag to zero. */
15061 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15062 if (display_line (&it))
15063 last_text_row_at_end = it.glyph_row - 1;
15064 }
15065 }
15066
15067 /* Update window_end_pos and window_end_vpos. */
15068 if (first_unchanged_at_end_row
15069 && !last_text_row_at_end)
15070 {
15071 /* Window end line if one of the preserved rows from the current
15072 matrix. Set row to the last row displaying text in current
15073 matrix starting at first_unchanged_at_end_row, after
15074 scrolling. */
15075 xassert (first_unchanged_at_end_row->displays_text_p);
15076 row = find_last_row_displaying_text (w->current_matrix, &it,
15077 first_unchanged_at_end_row);
15078 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15079
15080 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15081 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15082 w->window_end_vpos
15083 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15084 xassert (w->window_end_bytepos >= 0);
15085 IF_DEBUG (debug_method_add (w, "A"));
15086 }
15087 else if (last_text_row_at_end)
15088 {
15089 w->window_end_pos
15090 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15091 w->window_end_bytepos
15092 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15093 w->window_end_vpos
15094 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15095 xassert (w->window_end_bytepos >= 0);
15096 IF_DEBUG (debug_method_add (w, "B"));
15097 }
15098 else if (last_text_row)
15099 {
15100 /* We have displayed either to the end of the window or at the
15101 end of the window, i.e. the last row with text is to be found
15102 in the desired matrix. */
15103 w->window_end_pos
15104 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15105 w->window_end_bytepos
15106 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15107 w->window_end_vpos
15108 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15109 xassert (w->window_end_bytepos >= 0);
15110 }
15111 else if (first_unchanged_at_end_row == NULL
15112 && last_text_row == NULL
15113 && last_text_row_at_end == NULL)
15114 {
15115 /* Displayed to end of window, but no line containing text was
15116 displayed. Lines were deleted at the end of the window. */
15117 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15118 int vpos = XFASTINT (w->window_end_vpos);
15119 struct glyph_row *current_row = current_matrix->rows + vpos;
15120 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15121
15122 for (row = NULL;
15123 row == NULL && vpos >= first_vpos;
15124 --vpos, --current_row, --desired_row)
15125 {
15126 if (desired_row->enabled_p)
15127 {
15128 if (desired_row->displays_text_p)
15129 row = desired_row;
15130 }
15131 else if (current_row->displays_text_p)
15132 row = current_row;
15133 }
15134
15135 xassert (row != NULL);
15136 w->window_end_vpos = make_number (vpos + 1);
15137 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15138 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15139 xassert (w->window_end_bytepos >= 0);
15140 IF_DEBUG (debug_method_add (w, "C"));
15141 }
15142 else
15143 abort ();
15144
15145 #if 0 /* This leads to problems, for instance when the cursor is
15146 at ZV, and the cursor line displays no text. */
15147 /* Disable rows below what's displayed in the window. This makes
15148 debugging easier. */
15149 enable_glyph_matrix_rows (current_matrix,
15150 XFASTINT (w->window_end_vpos) + 1,
15151 bottom_vpos, 0);
15152 #endif
15153
15154 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15155 debug_end_vpos = XFASTINT (w->window_end_vpos));
15156
15157 /* Record that display has not been completed. */
15158 w->window_end_valid = Qnil;
15159 w->desired_matrix->no_scrolling_p = 1;
15160 return 3;
15161
15162 #undef GIVE_UP
15163 }
15164
15165
15166 \f
15167 /***********************************************************************
15168 More debugging support
15169 ***********************************************************************/
15170
15171 #if GLYPH_DEBUG
15172
15173 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15174 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15175 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15176
15177
15178 /* Dump the contents of glyph matrix MATRIX on stderr.
15179
15180 GLYPHS 0 means don't show glyph contents.
15181 GLYPHS 1 means show glyphs in short form
15182 GLYPHS > 1 means show glyphs in long form. */
15183
15184 void
15185 dump_glyph_matrix (matrix, glyphs)
15186 struct glyph_matrix *matrix;
15187 int glyphs;
15188 {
15189 int i;
15190 for (i = 0; i < matrix->nrows; ++i)
15191 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15192 }
15193
15194
15195 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15196 the glyph row and area where the glyph comes from. */
15197
15198 void
15199 dump_glyph (row, glyph, area)
15200 struct glyph_row *row;
15201 struct glyph *glyph;
15202 int area;
15203 {
15204 if (glyph->type == CHAR_GLYPH)
15205 {
15206 fprintf (stderr,
15207 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15208 glyph - row->glyphs[TEXT_AREA],
15209 'C',
15210 glyph->charpos,
15211 (BUFFERP (glyph->object)
15212 ? 'B'
15213 : (STRINGP (glyph->object)
15214 ? 'S'
15215 : '-')),
15216 glyph->pixel_width,
15217 glyph->u.ch,
15218 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15219 ? glyph->u.ch
15220 : '.'),
15221 glyph->face_id,
15222 glyph->left_box_line_p,
15223 glyph->right_box_line_p);
15224 }
15225 else if (glyph->type == STRETCH_GLYPH)
15226 {
15227 fprintf (stderr,
15228 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15229 glyph - row->glyphs[TEXT_AREA],
15230 'S',
15231 glyph->charpos,
15232 (BUFFERP (glyph->object)
15233 ? 'B'
15234 : (STRINGP (glyph->object)
15235 ? 'S'
15236 : '-')),
15237 glyph->pixel_width,
15238 0,
15239 '.',
15240 glyph->face_id,
15241 glyph->left_box_line_p,
15242 glyph->right_box_line_p);
15243 }
15244 else if (glyph->type == IMAGE_GLYPH)
15245 {
15246 fprintf (stderr,
15247 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15248 glyph - row->glyphs[TEXT_AREA],
15249 'I',
15250 glyph->charpos,
15251 (BUFFERP (glyph->object)
15252 ? 'B'
15253 : (STRINGP (glyph->object)
15254 ? 'S'
15255 : '-')),
15256 glyph->pixel_width,
15257 glyph->u.img_id,
15258 '.',
15259 glyph->face_id,
15260 glyph->left_box_line_p,
15261 glyph->right_box_line_p);
15262 }
15263 else if (glyph->type == COMPOSITE_GLYPH)
15264 {
15265 fprintf (stderr,
15266 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15267 glyph - row->glyphs[TEXT_AREA],
15268 '+',
15269 glyph->charpos,
15270 (BUFFERP (glyph->object)
15271 ? 'B'
15272 : (STRINGP (glyph->object)
15273 ? 'S'
15274 : '-')),
15275 glyph->pixel_width,
15276 glyph->u.cmp_id,
15277 '.',
15278 glyph->face_id,
15279 glyph->left_box_line_p,
15280 glyph->right_box_line_p);
15281 }
15282 }
15283
15284
15285 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15286 GLYPHS 0 means don't show glyph contents.
15287 GLYPHS 1 means show glyphs in short form
15288 GLYPHS > 1 means show glyphs in long form. */
15289
15290 void
15291 dump_glyph_row (row, vpos, glyphs)
15292 struct glyph_row *row;
15293 int vpos, glyphs;
15294 {
15295 if (glyphs != 1)
15296 {
15297 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15298 fprintf (stderr, "======================================================================\n");
15299
15300 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15301 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15302 vpos,
15303 MATRIX_ROW_START_CHARPOS (row),
15304 MATRIX_ROW_END_CHARPOS (row),
15305 row->used[TEXT_AREA],
15306 row->contains_overlapping_glyphs_p,
15307 row->enabled_p,
15308 row->truncated_on_left_p,
15309 row->truncated_on_right_p,
15310 row->continued_p,
15311 MATRIX_ROW_CONTINUATION_LINE_P (row),
15312 row->displays_text_p,
15313 row->ends_at_zv_p,
15314 row->fill_line_p,
15315 row->ends_in_middle_of_char_p,
15316 row->starts_in_middle_of_char_p,
15317 row->mouse_face_p,
15318 row->x,
15319 row->y,
15320 row->pixel_width,
15321 row->height,
15322 row->visible_height,
15323 row->ascent,
15324 row->phys_ascent);
15325 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15326 row->end.overlay_string_index,
15327 row->continuation_lines_width);
15328 fprintf (stderr, "%9d %5d\n",
15329 CHARPOS (row->start.string_pos),
15330 CHARPOS (row->end.string_pos));
15331 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15332 row->end.dpvec_index);
15333 }
15334
15335 if (glyphs > 1)
15336 {
15337 int area;
15338
15339 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15340 {
15341 struct glyph *glyph = row->glyphs[area];
15342 struct glyph *glyph_end = glyph + row->used[area];
15343
15344 /* Glyph for a line end in text. */
15345 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15346 ++glyph_end;
15347
15348 if (glyph < glyph_end)
15349 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15350
15351 for (; glyph < glyph_end; ++glyph)
15352 dump_glyph (row, glyph, area);
15353 }
15354 }
15355 else if (glyphs == 1)
15356 {
15357 int area;
15358
15359 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15360 {
15361 char *s = (char *) alloca (row->used[area] + 1);
15362 int i;
15363
15364 for (i = 0; i < row->used[area]; ++i)
15365 {
15366 struct glyph *glyph = row->glyphs[area] + i;
15367 if (glyph->type == CHAR_GLYPH
15368 && glyph->u.ch < 0x80
15369 && glyph->u.ch >= ' ')
15370 s[i] = glyph->u.ch;
15371 else
15372 s[i] = '.';
15373 }
15374
15375 s[i] = '\0';
15376 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15377 }
15378 }
15379 }
15380
15381
15382 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15383 Sdump_glyph_matrix, 0, 1, "p",
15384 doc: /* Dump the current matrix of the selected window to stderr.
15385 Shows contents of glyph row structures. With non-nil
15386 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15387 glyphs in short form, otherwise show glyphs in long form. */)
15388 (glyphs)
15389 Lisp_Object glyphs;
15390 {
15391 struct window *w = XWINDOW (selected_window);
15392 struct buffer *buffer = XBUFFER (w->buffer);
15393
15394 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15395 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15396 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15397 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15398 fprintf (stderr, "=============================================\n");
15399 dump_glyph_matrix (w->current_matrix,
15400 NILP (glyphs) ? 0 : XINT (glyphs));
15401 return Qnil;
15402 }
15403
15404
15405 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15406 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15407 ()
15408 {
15409 struct frame *f = XFRAME (selected_frame);
15410 dump_glyph_matrix (f->current_matrix, 1);
15411 return Qnil;
15412 }
15413
15414
15415 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15416 doc: /* Dump glyph row ROW to stderr.
15417 GLYPH 0 means don't dump glyphs.
15418 GLYPH 1 means dump glyphs in short form.
15419 GLYPH > 1 or omitted means dump glyphs in long form. */)
15420 (row, glyphs)
15421 Lisp_Object row, glyphs;
15422 {
15423 struct glyph_matrix *matrix;
15424 int vpos;
15425
15426 CHECK_NUMBER (row);
15427 matrix = XWINDOW (selected_window)->current_matrix;
15428 vpos = XINT (row);
15429 if (vpos >= 0 && vpos < matrix->nrows)
15430 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15431 vpos,
15432 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15433 return Qnil;
15434 }
15435
15436
15437 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15438 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15439 GLYPH 0 means don't dump glyphs.
15440 GLYPH 1 means dump glyphs in short form.
15441 GLYPH > 1 or omitted means dump glyphs in long form. */)
15442 (row, glyphs)
15443 Lisp_Object row, glyphs;
15444 {
15445 struct frame *sf = SELECTED_FRAME ();
15446 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15447 int vpos;
15448
15449 CHECK_NUMBER (row);
15450 vpos = XINT (row);
15451 if (vpos >= 0 && vpos < m->nrows)
15452 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15453 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15454 return Qnil;
15455 }
15456
15457
15458 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15459 doc: /* Toggle tracing of redisplay.
15460 With ARG, turn tracing on if and only if ARG is positive. */)
15461 (arg)
15462 Lisp_Object arg;
15463 {
15464 if (NILP (arg))
15465 trace_redisplay_p = !trace_redisplay_p;
15466 else
15467 {
15468 arg = Fprefix_numeric_value (arg);
15469 trace_redisplay_p = XINT (arg) > 0;
15470 }
15471
15472 return Qnil;
15473 }
15474
15475
15476 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15477 doc: /* Like `format', but print result to stderr.
15478 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15479 (nargs, args)
15480 int nargs;
15481 Lisp_Object *args;
15482 {
15483 Lisp_Object s = Fformat (nargs, args);
15484 fprintf (stderr, "%s", SDATA (s));
15485 return Qnil;
15486 }
15487
15488 #endif /* GLYPH_DEBUG */
15489
15490
15491 \f
15492 /***********************************************************************
15493 Building Desired Matrix Rows
15494 ***********************************************************************/
15495
15496 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15497 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15498
15499 static struct glyph_row *
15500 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15501 struct window *w;
15502 Lisp_Object overlay_arrow_string;
15503 {
15504 struct frame *f = XFRAME (WINDOW_FRAME (w));
15505 struct buffer *buffer = XBUFFER (w->buffer);
15506 struct buffer *old = current_buffer;
15507 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15508 int arrow_len = SCHARS (overlay_arrow_string);
15509 const unsigned char *arrow_end = arrow_string + arrow_len;
15510 const unsigned char *p;
15511 struct it it;
15512 int multibyte_p;
15513 int n_glyphs_before;
15514
15515 set_buffer_temp (buffer);
15516 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15517 it.glyph_row->used[TEXT_AREA] = 0;
15518 SET_TEXT_POS (it.position, 0, 0);
15519
15520 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15521 p = arrow_string;
15522 while (p < arrow_end)
15523 {
15524 Lisp_Object face, ilisp;
15525
15526 /* Get the next character. */
15527 if (multibyte_p)
15528 it.c = string_char_and_length (p, arrow_len, &it.len);
15529 else
15530 it.c = *p, it.len = 1;
15531 p += it.len;
15532
15533 /* Get its face. */
15534 ilisp = make_number (p - arrow_string);
15535 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15536 it.face_id = compute_char_face (f, it.c, face);
15537
15538 /* Compute its width, get its glyphs. */
15539 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15540 SET_TEXT_POS (it.position, -1, -1);
15541 PRODUCE_GLYPHS (&it);
15542
15543 /* If this character doesn't fit any more in the line, we have
15544 to remove some glyphs. */
15545 if (it.current_x > it.last_visible_x)
15546 {
15547 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15548 break;
15549 }
15550 }
15551
15552 set_buffer_temp (old);
15553 return it.glyph_row;
15554 }
15555
15556
15557 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15558 glyphs are only inserted for terminal frames since we can't really
15559 win with truncation glyphs when partially visible glyphs are
15560 involved. Which glyphs to insert is determined by
15561 produce_special_glyphs. */
15562
15563 static void
15564 insert_left_trunc_glyphs (it)
15565 struct it *it;
15566 {
15567 struct it truncate_it;
15568 struct glyph *from, *end, *to, *toend;
15569
15570 xassert (!FRAME_WINDOW_P (it->f));
15571
15572 /* Get the truncation glyphs. */
15573 truncate_it = *it;
15574 truncate_it.current_x = 0;
15575 truncate_it.face_id = DEFAULT_FACE_ID;
15576 truncate_it.glyph_row = &scratch_glyph_row;
15577 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15578 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15579 truncate_it.object = make_number (0);
15580 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15581
15582 /* Overwrite glyphs from IT with truncation glyphs. */
15583 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15584 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15585 to = it->glyph_row->glyphs[TEXT_AREA];
15586 toend = to + it->glyph_row->used[TEXT_AREA];
15587
15588 while (from < end)
15589 *to++ = *from++;
15590
15591 /* There may be padding glyphs left over. Overwrite them too. */
15592 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15593 {
15594 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15595 while (from < end)
15596 *to++ = *from++;
15597 }
15598
15599 if (to > toend)
15600 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15601 }
15602
15603
15604 /* Compute the pixel height and width of IT->glyph_row.
15605
15606 Most of the time, ascent and height of a display line will be equal
15607 to the max_ascent and max_height values of the display iterator
15608 structure. This is not the case if
15609
15610 1. We hit ZV without displaying anything. In this case, max_ascent
15611 and max_height will be zero.
15612
15613 2. We have some glyphs that don't contribute to the line height.
15614 (The glyph row flag contributes_to_line_height_p is for future
15615 pixmap extensions).
15616
15617 The first case is easily covered by using default values because in
15618 these cases, the line height does not really matter, except that it
15619 must not be zero. */
15620
15621 static void
15622 compute_line_metrics (it)
15623 struct it *it;
15624 {
15625 struct glyph_row *row = it->glyph_row;
15626 int area, i;
15627
15628 if (FRAME_WINDOW_P (it->f))
15629 {
15630 int i, min_y, max_y;
15631
15632 /* The line may consist of one space only, that was added to
15633 place the cursor on it. If so, the row's height hasn't been
15634 computed yet. */
15635 if (row->height == 0)
15636 {
15637 if (it->max_ascent + it->max_descent == 0)
15638 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15639 row->ascent = it->max_ascent;
15640 row->height = it->max_ascent + it->max_descent;
15641 row->phys_ascent = it->max_phys_ascent;
15642 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15643 row->extra_line_spacing = it->max_extra_line_spacing;
15644 }
15645
15646 /* Compute the width of this line. */
15647 row->pixel_width = row->x;
15648 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15649 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15650
15651 xassert (row->pixel_width >= 0);
15652 xassert (row->ascent >= 0 && row->height > 0);
15653
15654 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15655 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15656
15657 /* If first line's physical ascent is larger than its logical
15658 ascent, use the physical ascent, and make the row taller.
15659 This makes accented characters fully visible. */
15660 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15661 && row->phys_ascent > row->ascent)
15662 {
15663 row->height += row->phys_ascent - row->ascent;
15664 row->ascent = row->phys_ascent;
15665 }
15666
15667 /* Compute how much of the line is visible. */
15668 row->visible_height = row->height;
15669
15670 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15671 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15672
15673 if (row->y < min_y)
15674 row->visible_height -= min_y - row->y;
15675 if (row->y + row->height > max_y)
15676 row->visible_height -= row->y + row->height - max_y;
15677 }
15678 else
15679 {
15680 row->pixel_width = row->used[TEXT_AREA];
15681 if (row->continued_p)
15682 row->pixel_width -= it->continuation_pixel_width;
15683 else if (row->truncated_on_right_p)
15684 row->pixel_width -= it->truncation_pixel_width;
15685 row->ascent = row->phys_ascent = 0;
15686 row->height = row->phys_height = row->visible_height = 1;
15687 row->extra_line_spacing = 0;
15688 }
15689
15690 /* Compute a hash code for this row. */
15691 row->hash = 0;
15692 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15693 for (i = 0; i < row->used[area]; ++i)
15694 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15695 + row->glyphs[area][i].u.val
15696 + row->glyphs[area][i].face_id
15697 + row->glyphs[area][i].padding_p
15698 + (row->glyphs[area][i].type << 2));
15699
15700 it->max_ascent = it->max_descent = 0;
15701 it->max_phys_ascent = it->max_phys_descent = 0;
15702 }
15703
15704
15705 /* Append one space to the glyph row of iterator IT if doing a
15706 window-based redisplay. The space has the same face as
15707 IT->face_id. Value is non-zero if a space was added.
15708
15709 This function is called to make sure that there is always one glyph
15710 at the end of a glyph row that the cursor can be set on under
15711 window-systems. (If there weren't such a glyph we would not know
15712 how wide and tall a box cursor should be displayed).
15713
15714 At the same time this space let's a nicely handle clearing to the
15715 end of the line if the row ends in italic text. */
15716
15717 static int
15718 append_space_for_newline (it, default_face_p)
15719 struct it *it;
15720 int default_face_p;
15721 {
15722 if (FRAME_WINDOW_P (it->f))
15723 {
15724 int n = it->glyph_row->used[TEXT_AREA];
15725
15726 if (it->glyph_row->glyphs[TEXT_AREA] + n
15727 < it->glyph_row->glyphs[1 + TEXT_AREA])
15728 {
15729 /* Save some values that must not be changed.
15730 Must save IT->c and IT->len because otherwise
15731 ITERATOR_AT_END_P wouldn't work anymore after
15732 append_space_for_newline has been called. */
15733 enum display_element_type saved_what = it->what;
15734 int saved_c = it->c, saved_len = it->len;
15735 int saved_x = it->current_x;
15736 int saved_face_id = it->face_id;
15737 struct text_pos saved_pos;
15738 Lisp_Object saved_object;
15739 struct face *face;
15740
15741 saved_object = it->object;
15742 saved_pos = it->position;
15743
15744 it->what = IT_CHARACTER;
15745 bzero (&it->position, sizeof it->position);
15746 it->object = make_number (0);
15747 it->c = ' ';
15748 it->len = 1;
15749
15750 if (default_face_p)
15751 it->face_id = DEFAULT_FACE_ID;
15752 else if (it->face_before_selective_p)
15753 it->face_id = it->saved_face_id;
15754 face = FACE_FROM_ID (it->f, it->face_id);
15755 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15756
15757 PRODUCE_GLYPHS (it);
15758
15759 it->override_ascent = -1;
15760 it->constrain_row_ascent_descent_p = 0;
15761 it->current_x = saved_x;
15762 it->object = saved_object;
15763 it->position = saved_pos;
15764 it->what = saved_what;
15765 it->face_id = saved_face_id;
15766 it->len = saved_len;
15767 it->c = saved_c;
15768 return 1;
15769 }
15770 }
15771
15772 return 0;
15773 }
15774
15775
15776 /* Extend the face of the last glyph in the text area of IT->glyph_row
15777 to the end of the display line. Called from display_line.
15778 If the glyph row is empty, add a space glyph to it so that we
15779 know the face to draw. Set the glyph row flag fill_line_p. */
15780
15781 static void
15782 extend_face_to_end_of_line (it)
15783 struct it *it;
15784 {
15785 struct face *face;
15786 struct frame *f = it->f;
15787
15788 /* If line is already filled, do nothing. */
15789 if (it->current_x >= it->last_visible_x)
15790 return;
15791
15792 /* Face extension extends the background and box of IT->face_id
15793 to the end of the line. If the background equals the background
15794 of the frame, we don't have to do anything. */
15795 if (it->face_before_selective_p)
15796 face = FACE_FROM_ID (it->f, it->saved_face_id);
15797 else
15798 face = FACE_FROM_ID (f, it->face_id);
15799
15800 if (FRAME_WINDOW_P (f)
15801 && it->glyph_row->displays_text_p
15802 && face->box == FACE_NO_BOX
15803 && face->background == FRAME_BACKGROUND_PIXEL (f)
15804 && !face->stipple)
15805 return;
15806
15807 /* Set the glyph row flag indicating that the face of the last glyph
15808 in the text area has to be drawn to the end of the text area. */
15809 it->glyph_row->fill_line_p = 1;
15810
15811 /* If current character of IT is not ASCII, make sure we have the
15812 ASCII face. This will be automatically undone the next time
15813 get_next_display_element returns a multibyte character. Note
15814 that the character will always be single byte in unibyte text. */
15815 if (!SINGLE_BYTE_CHAR_P (it->c))
15816 {
15817 it->face_id = FACE_FOR_CHAR (f, face, 0);
15818 }
15819
15820 if (FRAME_WINDOW_P (f))
15821 {
15822 /* If the row is empty, add a space with the current face of IT,
15823 so that we know which face to draw. */
15824 if (it->glyph_row->used[TEXT_AREA] == 0)
15825 {
15826 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15827 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15828 it->glyph_row->used[TEXT_AREA] = 1;
15829 }
15830 }
15831 else
15832 {
15833 /* Save some values that must not be changed. */
15834 int saved_x = it->current_x;
15835 struct text_pos saved_pos;
15836 Lisp_Object saved_object;
15837 enum display_element_type saved_what = it->what;
15838 int saved_face_id = it->face_id;
15839
15840 saved_object = it->object;
15841 saved_pos = it->position;
15842
15843 it->what = IT_CHARACTER;
15844 bzero (&it->position, sizeof it->position);
15845 it->object = make_number (0);
15846 it->c = ' ';
15847 it->len = 1;
15848 it->face_id = face->id;
15849
15850 PRODUCE_GLYPHS (it);
15851
15852 while (it->current_x <= it->last_visible_x)
15853 PRODUCE_GLYPHS (it);
15854
15855 /* Don't count these blanks really. It would let us insert a left
15856 truncation glyph below and make us set the cursor on them, maybe. */
15857 it->current_x = saved_x;
15858 it->object = saved_object;
15859 it->position = saved_pos;
15860 it->what = saved_what;
15861 it->face_id = saved_face_id;
15862 }
15863 }
15864
15865
15866 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15867 trailing whitespace. */
15868
15869 static int
15870 trailing_whitespace_p (charpos)
15871 int charpos;
15872 {
15873 int bytepos = CHAR_TO_BYTE (charpos);
15874 int c = 0;
15875
15876 while (bytepos < ZV_BYTE
15877 && (c = FETCH_CHAR (bytepos),
15878 c == ' ' || c == '\t'))
15879 ++bytepos;
15880
15881 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15882 {
15883 if (bytepos != PT_BYTE)
15884 return 1;
15885 }
15886 return 0;
15887 }
15888
15889
15890 /* Highlight trailing whitespace, if any, in ROW. */
15891
15892 void
15893 highlight_trailing_whitespace (f, row)
15894 struct frame *f;
15895 struct glyph_row *row;
15896 {
15897 int used = row->used[TEXT_AREA];
15898
15899 if (used)
15900 {
15901 struct glyph *start = row->glyphs[TEXT_AREA];
15902 struct glyph *glyph = start + used - 1;
15903
15904 /* Skip over glyphs inserted to display the cursor at the
15905 end of a line, for extending the face of the last glyph
15906 to the end of the line on terminals, and for truncation
15907 and continuation glyphs. */
15908 while (glyph >= start
15909 && glyph->type == CHAR_GLYPH
15910 && INTEGERP (glyph->object))
15911 --glyph;
15912
15913 /* If last glyph is a space or stretch, and it's trailing
15914 whitespace, set the face of all trailing whitespace glyphs in
15915 IT->glyph_row to `trailing-whitespace'. */
15916 if (glyph >= start
15917 && BUFFERP (glyph->object)
15918 && (glyph->type == STRETCH_GLYPH
15919 || (glyph->type == CHAR_GLYPH
15920 && glyph->u.ch == ' '))
15921 && trailing_whitespace_p (glyph->charpos))
15922 {
15923 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15924 if (face_id < 0)
15925 return;
15926
15927 while (glyph >= start
15928 && BUFFERP (glyph->object)
15929 && (glyph->type == STRETCH_GLYPH
15930 || (glyph->type == CHAR_GLYPH
15931 && glyph->u.ch == ' ')))
15932 (glyph--)->face_id = face_id;
15933 }
15934 }
15935 }
15936
15937
15938 /* Value is non-zero if glyph row ROW in window W should be
15939 used to hold the cursor. */
15940
15941 static int
15942 cursor_row_p (w, row)
15943 struct window *w;
15944 struct glyph_row *row;
15945 {
15946 int cursor_row_p = 1;
15947
15948 if (PT == MATRIX_ROW_END_CHARPOS (row))
15949 {
15950 /* Suppose the row ends on a string.
15951 Unless the row is continued, that means it ends on a newline
15952 in the string. If it's anything other than a display string
15953 (e.g. a before-string from an overlay), we don't want the
15954 cursor there. (This heuristic seems to give the optimal
15955 behavior for the various types of multi-line strings.) */
15956 if (CHARPOS (row->end.string_pos) >= 0)
15957 {
15958 if (row->continued_p)
15959 cursor_row_p = 1;
15960 else
15961 {
15962 /* Check for `display' property. */
15963 struct glyph *beg = row->glyphs[TEXT_AREA];
15964 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15965 struct glyph *glyph;
15966
15967 cursor_row_p = 0;
15968 for (glyph = end; glyph >= beg; --glyph)
15969 if (STRINGP (glyph->object))
15970 {
15971 Lisp_Object prop
15972 = Fget_char_property (make_number (PT),
15973 Qdisplay, Qnil);
15974 cursor_row_p =
15975 (!NILP (prop)
15976 && display_prop_string_p (prop, glyph->object));
15977 break;
15978 }
15979 }
15980 }
15981 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15982 {
15983 /* If the row ends in middle of a real character,
15984 and the line is continued, we want the cursor here.
15985 That's because MATRIX_ROW_END_CHARPOS would equal
15986 PT if PT is before the character. */
15987 if (!row->ends_in_ellipsis_p)
15988 cursor_row_p = row->continued_p;
15989 else
15990 /* If the row ends in an ellipsis, then
15991 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15992 We want that position to be displayed after the ellipsis. */
15993 cursor_row_p = 0;
15994 }
15995 /* If the row ends at ZV, display the cursor at the end of that
15996 row instead of at the start of the row below. */
15997 else if (row->ends_at_zv_p)
15998 cursor_row_p = 1;
15999 else
16000 cursor_row_p = 0;
16001 }
16002
16003 return cursor_row_p;
16004 }
16005
16006
16007 /* Construct the glyph row IT->glyph_row in the desired matrix of
16008 IT->w from text at the current position of IT. See dispextern.h
16009 for an overview of struct it. Value is non-zero if
16010 IT->glyph_row displays text, as opposed to a line displaying ZV
16011 only. */
16012
16013 static int
16014 display_line (it)
16015 struct it *it;
16016 {
16017 struct glyph_row *row = it->glyph_row;
16018 Lisp_Object overlay_arrow_string;
16019
16020 /* We always start displaying at hpos zero even if hscrolled. */
16021 xassert (it->hpos == 0 && it->current_x == 0);
16022
16023 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16024 >= it->w->desired_matrix->nrows)
16025 {
16026 it->w->nrows_scale_factor++;
16027 fonts_changed_p = 1;
16028 return 0;
16029 }
16030
16031 /* Is IT->w showing the region? */
16032 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16033
16034 /* Clear the result glyph row and enable it. */
16035 prepare_desired_row (row);
16036
16037 row->y = it->current_y;
16038 row->start = it->start;
16039 row->continuation_lines_width = it->continuation_lines_width;
16040 row->displays_text_p = 1;
16041 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16042 it->starts_in_middle_of_char_p = 0;
16043
16044 /* Arrange the overlays nicely for our purposes. Usually, we call
16045 display_line on only one line at a time, in which case this
16046 can't really hurt too much, or we call it on lines which appear
16047 one after another in the buffer, in which case all calls to
16048 recenter_overlay_lists but the first will be pretty cheap. */
16049 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16050
16051 /* Move over display elements that are not visible because we are
16052 hscrolled. This may stop at an x-position < IT->first_visible_x
16053 if the first glyph is partially visible or if we hit a line end. */
16054 if (it->current_x < it->first_visible_x)
16055 {
16056 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16057 MOVE_TO_POS | MOVE_TO_X);
16058 }
16059
16060 /* Get the initial row height. This is either the height of the
16061 text hscrolled, if there is any, or zero. */
16062 row->ascent = it->max_ascent;
16063 row->height = it->max_ascent + it->max_descent;
16064 row->phys_ascent = it->max_phys_ascent;
16065 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16066 row->extra_line_spacing = it->max_extra_line_spacing;
16067
16068 /* Loop generating characters. The loop is left with IT on the next
16069 character to display. */
16070 while (1)
16071 {
16072 int n_glyphs_before, hpos_before, x_before;
16073 int x, i, nglyphs;
16074 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16075
16076 /* Retrieve the next thing to display. Value is zero if end of
16077 buffer reached. */
16078 if (!get_next_display_element (it))
16079 {
16080 /* Maybe add a space at the end of this line that is used to
16081 display the cursor there under X. Set the charpos of the
16082 first glyph of blank lines not corresponding to any text
16083 to -1. */
16084 #ifdef HAVE_WINDOW_SYSTEM
16085 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16086 row->exact_window_width_line_p = 1;
16087 else
16088 #endif /* HAVE_WINDOW_SYSTEM */
16089 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16090 || row->used[TEXT_AREA] == 0)
16091 {
16092 row->glyphs[TEXT_AREA]->charpos = -1;
16093 row->displays_text_p = 0;
16094
16095 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16096 && (!MINI_WINDOW_P (it->w)
16097 || (minibuf_level && EQ (it->window, minibuf_window))))
16098 row->indicate_empty_line_p = 1;
16099 }
16100
16101 it->continuation_lines_width = 0;
16102 row->ends_at_zv_p = 1;
16103 break;
16104 }
16105
16106 /* Now, get the metrics of what we want to display. This also
16107 generates glyphs in `row' (which is IT->glyph_row). */
16108 n_glyphs_before = row->used[TEXT_AREA];
16109 x = it->current_x;
16110
16111 /* Remember the line height so far in case the next element doesn't
16112 fit on the line. */
16113 if (!it->truncate_lines_p)
16114 {
16115 ascent = it->max_ascent;
16116 descent = it->max_descent;
16117 phys_ascent = it->max_phys_ascent;
16118 phys_descent = it->max_phys_descent;
16119 }
16120
16121 PRODUCE_GLYPHS (it);
16122
16123 /* If this display element was in marginal areas, continue with
16124 the next one. */
16125 if (it->area != TEXT_AREA)
16126 {
16127 row->ascent = max (row->ascent, it->max_ascent);
16128 row->height = max (row->height, it->max_ascent + it->max_descent);
16129 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16130 row->phys_height = max (row->phys_height,
16131 it->max_phys_ascent + it->max_phys_descent);
16132 row->extra_line_spacing = max (row->extra_line_spacing,
16133 it->max_extra_line_spacing);
16134 set_iterator_to_next (it, 1);
16135 continue;
16136 }
16137
16138 /* Does the display element fit on the line? If we truncate
16139 lines, we should draw past the right edge of the window. If
16140 we don't truncate, we want to stop so that we can display the
16141 continuation glyph before the right margin. If lines are
16142 continued, there are two possible strategies for characters
16143 resulting in more than 1 glyph (e.g. tabs): Display as many
16144 glyphs as possible in this line and leave the rest for the
16145 continuation line, or display the whole element in the next
16146 line. Original redisplay did the former, so we do it also. */
16147 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16148 hpos_before = it->hpos;
16149 x_before = x;
16150
16151 if (/* Not a newline. */
16152 nglyphs > 0
16153 /* Glyphs produced fit entirely in the line. */
16154 && it->current_x < it->last_visible_x)
16155 {
16156 it->hpos += nglyphs;
16157 row->ascent = max (row->ascent, it->max_ascent);
16158 row->height = max (row->height, it->max_ascent + it->max_descent);
16159 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16160 row->phys_height = max (row->phys_height,
16161 it->max_phys_ascent + it->max_phys_descent);
16162 row->extra_line_spacing = max (row->extra_line_spacing,
16163 it->max_extra_line_spacing);
16164 if (it->current_x - it->pixel_width < it->first_visible_x)
16165 row->x = x - it->first_visible_x;
16166 }
16167 else
16168 {
16169 int new_x;
16170 struct glyph *glyph;
16171
16172 for (i = 0; i < nglyphs; ++i, x = new_x)
16173 {
16174 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16175 new_x = x + glyph->pixel_width;
16176
16177 if (/* Lines are continued. */
16178 !it->truncate_lines_p
16179 && (/* Glyph doesn't fit on the line. */
16180 new_x > it->last_visible_x
16181 /* Or it fits exactly on a window system frame. */
16182 || (new_x == it->last_visible_x
16183 && FRAME_WINDOW_P (it->f))))
16184 {
16185 /* End of a continued line. */
16186
16187 if (it->hpos == 0
16188 || (new_x == it->last_visible_x
16189 && FRAME_WINDOW_P (it->f)))
16190 {
16191 /* Current glyph is the only one on the line or
16192 fits exactly on the line. We must continue
16193 the line because we can't draw the cursor
16194 after the glyph. */
16195 row->continued_p = 1;
16196 it->current_x = new_x;
16197 it->continuation_lines_width += new_x;
16198 ++it->hpos;
16199 if (i == nglyphs - 1)
16200 {
16201 set_iterator_to_next (it, 1);
16202 #ifdef HAVE_WINDOW_SYSTEM
16203 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16204 {
16205 if (!get_next_display_element (it))
16206 {
16207 row->exact_window_width_line_p = 1;
16208 it->continuation_lines_width = 0;
16209 row->continued_p = 0;
16210 row->ends_at_zv_p = 1;
16211 }
16212 else if (ITERATOR_AT_END_OF_LINE_P (it))
16213 {
16214 row->continued_p = 0;
16215 row->exact_window_width_line_p = 1;
16216 }
16217 }
16218 #endif /* HAVE_WINDOW_SYSTEM */
16219 }
16220 }
16221 else if (CHAR_GLYPH_PADDING_P (*glyph)
16222 && !FRAME_WINDOW_P (it->f))
16223 {
16224 /* A padding glyph that doesn't fit on this line.
16225 This means the whole character doesn't fit
16226 on the line. */
16227 row->used[TEXT_AREA] = n_glyphs_before;
16228
16229 /* Fill the rest of the row with continuation
16230 glyphs like in 20.x. */
16231 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16232 < row->glyphs[1 + TEXT_AREA])
16233 produce_special_glyphs (it, IT_CONTINUATION);
16234
16235 row->continued_p = 1;
16236 it->current_x = x_before;
16237 it->continuation_lines_width += x_before;
16238
16239 /* Restore the height to what it was before the
16240 element not fitting on the line. */
16241 it->max_ascent = ascent;
16242 it->max_descent = descent;
16243 it->max_phys_ascent = phys_ascent;
16244 it->max_phys_descent = phys_descent;
16245 }
16246 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16247 {
16248 /* A TAB that extends past the right edge of the
16249 window. This produces a single glyph on
16250 window system frames. We leave the glyph in
16251 this row and let it fill the row, but don't
16252 consume the TAB. */
16253 it->continuation_lines_width += it->last_visible_x;
16254 row->ends_in_middle_of_char_p = 1;
16255 row->continued_p = 1;
16256 glyph->pixel_width = it->last_visible_x - x;
16257 it->starts_in_middle_of_char_p = 1;
16258 }
16259 else
16260 {
16261 /* Something other than a TAB that draws past
16262 the right edge of the window. Restore
16263 positions to values before the element. */
16264 row->used[TEXT_AREA] = n_glyphs_before + i;
16265
16266 /* Display continuation glyphs. */
16267 if (!FRAME_WINDOW_P (it->f))
16268 produce_special_glyphs (it, IT_CONTINUATION);
16269 row->continued_p = 1;
16270
16271 it->current_x = x_before;
16272 it->continuation_lines_width += x;
16273 extend_face_to_end_of_line (it);
16274
16275 if (nglyphs > 1 && i > 0)
16276 {
16277 row->ends_in_middle_of_char_p = 1;
16278 it->starts_in_middle_of_char_p = 1;
16279 }
16280
16281 /* Restore the height to what it was before the
16282 element not fitting on the line. */
16283 it->max_ascent = ascent;
16284 it->max_descent = descent;
16285 it->max_phys_ascent = phys_ascent;
16286 it->max_phys_descent = phys_descent;
16287 }
16288
16289 break;
16290 }
16291 else if (new_x > it->first_visible_x)
16292 {
16293 /* Increment number of glyphs actually displayed. */
16294 ++it->hpos;
16295
16296 if (x < it->first_visible_x)
16297 /* Glyph is partially visible, i.e. row starts at
16298 negative X position. */
16299 row->x = x - it->first_visible_x;
16300 }
16301 else
16302 {
16303 /* Glyph is completely off the left margin of the
16304 window. This should not happen because of the
16305 move_it_in_display_line at the start of this
16306 function, unless the text display area of the
16307 window is empty. */
16308 xassert (it->first_visible_x <= it->last_visible_x);
16309 }
16310 }
16311
16312 row->ascent = max (row->ascent, it->max_ascent);
16313 row->height = max (row->height, it->max_ascent + it->max_descent);
16314 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16315 row->phys_height = max (row->phys_height,
16316 it->max_phys_ascent + it->max_phys_descent);
16317 row->extra_line_spacing = max (row->extra_line_spacing,
16318 it->max_extra_line_spacing);
16319
16320 /* End of this display line if row is continued. */
16321 if (row->continued_p || row->ends_at_zv_p)
16322 break;
16323 }
16324
16325 at_end_of_line:
16326 /* Is this a line end? If yes, we're also done, after making
16327 sure that a non-default face is extended up to the right
16328 margin of the window. */
16329 if (ITERATOR_AT_END_OF_LINE_P (it))
16330 {
16331 int used_before = row->used[TEXT_AREA];
16332
16333 row->ends_in_newline_from_string_p = STRINGP (it->object);
16334
16335 #ifdef HAVE_WINDOW_SYSTEM
16336 /* Add a space at the end of the line that is used to
16337 display the cursor there. */
16338 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16339 append_space_for_newline (it, 0);
16340 #endif /* HAVE_WINDOW_SYSTEM */
16341
16342 /* Extend the face to the end of the line. */
16343 extend_face_to_end_of_line (it);
16344
16345 /* Make sure we have the position. */
16346 if (used_before == 0)
16347 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16348
16349 /* Consume the line end. This skips over invisible lines. */
16350 set_iterator_to_next (it, 1);
16351 it->continuation_lines_width = 0;
16352 break;
16353 }
16354
16355 /* Proceed with next display element. Note that this skips
16356 over lines invisible because of selective display. */
16357 set_iterator_to_next (it, 1);
16358
16359 /* If we truncate lines, we are done when the last displayed
16360 glyphs reach past the right margin of the window. */
16361 if (it->truncate_lines_p
16362 && (FRAME_WINDOW_P (it->f)
16363 ? (it->current_x >= it->last_visible_x)
16364 : (it->current_x > it->last_visible_x)))
16365 {
16366 /* Maybe add truncation glyphs. */
16367 if (!FRAME_WINDOW_P (it->f))
16368 {
16369 int i, n;
16370
16371 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16372 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16373 break;
16374
16375 for (n = row->used[TEXT_AREA]; i < n; ++i)
16376 {
16377 row->used[TEXT_AREA] = i;
16378 produce_special_glyphs (it, IT_TRUNCATION);
16379 }
16380 }
16381 #ifdef HAVE_WINDOW_SYSTEM
16382 else
16383 {
16384 /* Don't truncate if we can overflow newline into fringe. */
16385 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16386 {
16387 if (!get_next_display_element (it))
16388 {
16389 it->continuation_lines_width = 0;
16390 row->ends_at_zv_p = 1;
16391 row->exact_window_width_line_p = 1;
16392 break;
16393 }
16394 if (ITERATOR_AT_END_OF_LINE_P (it))
16395 {
16396 row->exact_window_width_line_p = 1;
16397 goto at_end_of_line;
16398 }
16399 }
16400 }
16401 #endif /* HAVE_WINDOW_SYSTEM */
16402
16403 row->truncated_on_right_p = 1;
16404 it->continuation_lines_width = 0;
16405 reseat_at_next_visible_line_start (it, 0);
16406 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16407 it->hpos = hpos_before;
16408 it->current_x = x_before;
16409 break;
16410 }
16411 }
16412
16413 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16414 at the left window margin. */
16415 if (it->first_visible_x
16416 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16417 {
16418 if (!FRAME_WINDOW_P (it->f))
16419 insert_left_trunc_glyphs (it);
16420 row->truncated_on_left_p = 1;
16421 }
16422
16423 /* If the start of this line is the overlay arrow-position, then
16424 mark this glyph row as the one containing the overlay arrow.
16425 This is clearly a mess with variable size fonts. It would be
16426 better to let it be displayed like cursors under X. */
16427 if ((row->displays_text_p || !overlay_arrow_seen)
16428 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16429 !NILP (overlay_arrow_string)))
16430 {
16431 /* Overlay arrow in window redisplay is a fringe bitmap. */
16432 if (STRINGP (overlay_arrow_string))
16433 {
16434 struct glyph_row *arrow_row
16435 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16436 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16437 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16438 struct glyph *p = row->glyphs[TEXT_AREA];
16439 struct glyph *p2, *end;
16440
16441 /* Copy the arrow glyphs. */
16442 while (glyph < arrow_end)
16443 *p++ = *glyph++;
16444
16445 /* Throw away padding glyphs. */
16446 p2 = p;
16447 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16448 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16449 ++p2;
16450 if (p2 > p)
16451 {
16452 while (p2 < end)
16453 *p++ = *p2++;
16454 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16455 }
16456 }
16457 else
16458 {
16459 xassert (INTEGERP (overlay_arrow_string));
16460 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16461 }
16462 overlay_arrow_seen = 1;
16463 }
16464
16465 /* Compute pixel dimensions of this line. */
16466 compute_line_metrics (it);
16467
16468 /* Remember the position at which this line ends. */
16469 row->end = it->current;
16470
16471 /* Record whether this row ends inside an ellipsis. */
16472 row->ends_in_ellipsis_p
16473 = (it->method == GET_FROM_DISPLAY_VECTOR
16474 && it->ellipsis_p);
16475
16476 /* Save fringe bitmaps in this row. */
16477 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16478 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16479 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16480 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16481
16482 it->left_user_fringe_bitmap = 0;
16483 it->left_user_fringe_face_id = 0;
16484 it->right_user_fringe_bitmap = 0;
16485 it->right_user_fringe_face_id = 0;
16486
16487 /* Maybe set the cursor. */
16488 if (it->w->cursor.vpos < 0
16489 && PT >= MATRIX_ROW_START_CHARPOS (row)
16490 && PT <= MATRIX_ROW_END_CHARPOS (row)
16491 && cursor_row_p (it->w, row))
16492 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16493
16494 /* Highlight trailing whitespace. */
16495 if (!NILP (Vshow_trailing_whitespace))
16496 highlight_trailing_whitespace (it->f, it->glyph_row);
16497
16498 /* Prepare for the next line. This line starts horizontally at (X
16499 HPOS) = (0 0). Vertical positions are incremented. As a
16500 convenience for the caller, IT->glyph_row is set to the next
16501 row to be used. */
16502 it->current_x = it->hpos = 0;
16503 it->current_y += row->height;
16504 ++it->vpos;
16505 ++it->glyph_row;
16506 it->start = it->current;
16507 return row->displays_text_p;
16508 }
16509
16510
16511 \f
16512 /***********************************************************************
16513 Menu Bar
16514 ***********************************************************************/
16515
16516 /* Redisplay the menu bar in the frame for window W.
16517
16518 The menu bar of X frames that don't have X toolkit support is
16519 displayed in a special window W->frame->menu_bar_window.
16520
16521 The menu bar of terminal frames is treated specially as far as
16522 glyph matrices are concerned. Menu bar lines are not part of
16523 windows, so the update is done directly on the frame matrix rows
16524 for the menu bar. */
16525
16526 static void
16527 display_menu_bar (w)
16528 struct window *w;
16529 {
16530 struct frame *f = XFRAME (WINDOW_FRAME (w));
16531 struct it it;
16532 Lisp_Object items;
16533 int i;
16534
16535 /* Don't do all this for graphical frames. */
16536 #ifdef HAVE_NTGUI
16537 if (FRAME_W32_P (f))
16538 return;
16539 #endif
16540 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16541 if (FRAME_X_P (f))
16542 return;
16543 #endif
16544 #ifdef MAC_OS
16545 if (FRAME_MAC_P (f))
16546 return;
16547 #endif
16548
16549 #ifdef USE_X_TOOLKIT
16550 xassert (!FRAME_WINDOW_P (f));
16551 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16552 it.first_visible_x = 0;
16553 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16554 #else /* not USE_X_TOOLKIT */
16555 if (FRAME_WINDOW_P (f))
16556 {
16557 /* Menu bar lines are displayed in the desired matrix of the
16558 dummy window menu_bar_window. */
16559 struct window *menu_w;
16560 xassert (WINDOWP (f->menu_bar_window));
16561 menu_w = XWINDOW (f->menu_bar_window);
16562 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16563 MENU_FACE_ID);
16564 it.first_visible_x = 0;
16565 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16566 }
16567 else
16568 {
16569 /* This is a TTY frame, i.e. character hpos/vpos are used as
16570 pixel x/y. */
16571 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16572 MENU_FACE_ID);
16573 it.first_visible_x = 0;
16574 it.last_visible_x = FRAME_COLS (f);
16575 }
16576 #endif /* not USE_X_TOOLKIT */
16577
16578 if (! mode_line_inverse_video)
16579 /* Force the menu-bar to be displayed in the default face. */
16580 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16581
16582 /* Clear all rows of the menu bar. */
16583 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16584 {
16585 struct glyph_row *row = it.glyph_row + i;
16586 clear_glyph_row (row);
16587 row->enabled_p = 1;
16588 row->full_width_p = 1;
16589 }
16590
16591 /* Display all items of the menu bar. */
16592 items = FRAME_MENU_BAR_ITEMS (it.f);
16593 for (i = 0; i < XVECTOR (items)->size; i += 4)
16594 {
16595 Lisp_Object string;
16596
16597 /* Stop at nil string. */
16598 string = AREF (items, i + 1);
16599 if (NILP (string))
16600 break;
16601
16602 /* Remember where item was displayed. */
16603 AREF (items, i + 3) = make_number (it.hpos);
16604
16605 /* Display the item, pad with one space. */
16606 if (it.current_x < it.last_visible_x)
16607 display_string (NULL, string, Qnil, 0, 0, &it,
16608 SCHARS (string) + 1, 0, 0, -1);
16609 }
16610
16611 /* Fill out the line with spaces. */
16612 if (it.current_x < it.last_visible_x)
16613 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16614
16615 /* Compute the total height of the lines. */
16616 compute_line_metrics (&it);
16617 }
16618
16619
16620 \f
16621 /***********************************************************************
16622 Mode Line
16623 ***********************************************************************/
16624
16625 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16626 FORCE is non-zero, redisplay mode lines unconditionally.
16627 Otherwise, redisplay only mode lines that are garbaged. Value is
16628 the number of windows whose mode lines were redisplayed. */
16629
16630 static int
16631 redisplay_mode_lines (window, force)
16632 Lisp_Object window;
16633 int force;
16634 {
16635 int nwindows = 0;
16636
16637 while (!NILP (window))
16638 {
16639 struct window *w = XWINDOW (window);
16640
16641 if (WINDOWP (w->hchild))
16642 nwindows += redisplay_mode_lines (w->hchild, force);
16643 else if (WINDOWP (w->vchild))
16644 nwindows += redisplay_mode_lines (w->vchild, force);
16645 else if (force
16646 || FRAME_GARBAGED_P (XFRAME (w->frame))
16647 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16648 {
16649 struct text_pos lpoint;
16650 struct buffer *old = current_buffer;
16651
16652 /* Set the window's buffer for the mode line display. */
16653 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16654 set_buffer_internal_1 (XBUFFER (w->buffer));
16655
16656 /* Point refers normally to the selected window. For any
16657 other window, set up appropriate value. */
16658 if (!EQ (window, selected_window))
16659 {
16660 struct text_pos pt;
16661
16662 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16663 if (CHARPOS (pt) < BEGV)
16664 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16665 else if (CHARPOS (pt) > (ZV - 1))
16666 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16667 else
16668 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16669 }
16670
16671 /* Display mode lines. */
16672 clear_glyph_matrix (w->desired_matrix);
16673 if (display_mode_lines (w))
16674 {
16675 ++nwindows;
16676 w->must_be_updated_p = 1;
16677 }
16678
16679 /* Restore old settings. */
16680 set_buffer_internal_1 (old);
16681 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16682 }
16683
16684 window = w->next;
16685 }
16686
16687 return nwindows;
16688 }
16689
16690
16691 /* Display the mode and/or top line of window W. Value is the number
16692 of mode lines displayed. */
16693
16694 static int
16695 display_mode_lines (w)
16696 struct window *w;
16697 {
16698 Lisp_Object old_selected_window, old_selected_frame;
16699 int n = 0;
16700
16701 old_selected_frame = selected_frame;
16702 selected_frame = w->frame;
16703 old_selected_window = selected_window;
16704 XSETWINDOW (selected_window, w);
16705
16706 /* These will be set while the mode line specs are processed. */
16707 line_number_displayed = 0;
16708 w->column_number_displayed = Qnil;
16709
16710 if (WINDOW_WANTS_MODELINE_P (w))
16711 {
16712 struct window *sel_w = XWINDOW (old_selected_window);
16713
16714 /* Select mode line face based on the real selected window. */
16715 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16716 current_buffer->mode_line_format);
16717 ++n;
16718 }
16719
16720 if (WINDOW_WANTS_HEADER_LINE_P (w))
16721 {
16722 display_mode_line (w, HEADER_LINE_FACE_ID,
16723 current_buffer->header_line_format);
16724 ++n;
16725 }
16726
16727 selected_frame = old_selected_frame;
16728 selected_window = old_selected_window;
16729 return n;
16730 }
16731
16732
16733 /* Display mode or top line of window W. FACE_ID specifies which line
16734 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16735 FORMAT is the mode line format to display. Value is the pixel
16736 height of the mode line displayed. */
16737
16738 static int
16739 display_mode_line (w, face_id, format)
16740 struct window *w;
16741 enum face_id face_id;
16742 Lisp_Object format;
16743 {
16744 struct it it;
16745 struct face *face;
16746 int count = SPECPDL_INDEX ();
16747
16748 init_iterator (&it, w, -1, -1, NULL, face_id);
16749 /* Don't extend on a previously drawn mode-line.
16750 This may happen if called from pos_visible_p. */
16751 it.glyph_row->enabled_p = 0;
16752 prepare_desired_row (it.glyph_row);
16753
16754 it.glyph_row->mode_line_p = 1;
16755
16756 if (! mode_line_inverse_video)
16757 /* Force the mode-line to be displayed in the default face. */
16758 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16759
16760 record_unwind_protect (unwind_format_mode_line,
16761 format_mode_line_unwind_data (NULL, 0));
16762
16763 mode_line_target = MODE_LINE_DISPLAY;
16764
16765 /* Temporarily make frame's keyboard the current kboard so that
16766 kboard-local variables in the mode_line_format will get the right
16767 values. */
16768 push_kboard (FRAME_KBOARD (it.f));
16769 record_unwind_save_match_data ();
16770 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16771 pop_kboard ();
16772
16773 unbind_to (count, Qnil);
16774
16775 /* Fill up with spaces. */
16776 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16777
16778 compute_line_metrics (&it);
16779 it.glyph_row->full_width_p = 1;
16780 it.glyph_row->continued_p = 0;
16781 it.glyph_row->truncated_on_left_p = 0;
16782 it.glyph_row->truncated_on_right_p = 0;
16783
16784 /* Make a 3D mode-line have a shadow at its right end. */
16785 face = FACE_FROM_ID (it.f, face_id);
16786 extend_face_to_end_of_line (&it);
16787 if (face->box != FACE_NO_BOX)
16788 {
16789 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16790 + it.glyph_row->used[TEXT_AREA] - 1);
16791 last->right_box_line_p = 1;
16792 }
16793
16794 return it.glyph_row->height;
16795 }
16796
16797 /* Move element ELT in LIST to the front of LIST.
16798 Return the updated list. */
16799
16800 static Lisp_Object
16801 move_elt_to_front (elt, list)
16802 Lisp_Object elt, list;
16803 {
16804 register Lisp_Object tail, prev;
16805 register Lisp_Object tem;
16806
16807 tail = list;
16808 prev = Qnil;
16809 while (CONSP (tail))
16810 {
16811 tem = XCAR (tail);
16812
16813 if (EQ (elt, tem))
16814 {
16815 /* Splice out the link TAIL. */
16816 if (NILP (prev))
16817 list = XCDR (tail);
16818 else
16819 Fsetcdr (prev, XCDR (tail));
16820
16821 /* Now make it the first. */
16822 Fsetcdr (tail, list);
16823 return tail;
16824 }
16825 else
16826 prev = tail;
16827 tail = XCDR (tail);
16828 QUIT;
16829 }
16830
16831 /* Not found--return unchanged LIST. */
16832 return list;
16833 }
16834
16835 /* Contribute ELT to the mode line for window IT->w. How it
16836 translates into text depends on its data type.
16837
16838 IT describes the display environment in which we display, as usual.
16839
16840 DEPTH is the depth in recursion. It is used to prevent
16841 infinite recursion here.
16842
16843 FIELD_WIDTH is the number of characters the display of ELT should
16844 occupy in the mode line, and PRECISION is the maximum number of
16845 characters to display from ELT's representation. See
16846 display_string for details.
16847
16848 Returns the hpos of the end of the text generated by ELT.
16849
16850 PROPS is a property list to add to any string we encounter.
16851
16852 If RISKY is nonzero, remove (disregard) any properties in any string
16853 we encounter, and ignore :eval and :propertize.
16854
16855 The global variable `mode_line_target' determines whether the
16856 output is passed to `store_mode_line_noprop',
16857 `store_mode_line_string', or `display_string'. */
16858
16859 static int
16860 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16861 struct it *it;
16862 int depth;
16863 int field_width, precision;
16864 Lisp_Object elt, props;
16865 int risky;
16866 {
16867 int n = 0, field, prec;
16868 int literal = 0;
16869
16870 tail_recurse:
16871 if (depth > 100)
16872 elt = build_string ("*too-deep*");
16873
16874 depth++;
16875
16876 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16877 {
16878 case Lisp_String:
16879 {
16880 /* A string: output it and check for %-constructs within it. */
16881 unsigned char c;
16882 int offset = 0;
16883
16884 if (SCHARS (elt) > 0
16885 && (!NILP (props) || risky))
16886 {
16887 Lisp_Object oprops, aelt;
16888 oprops = Ftext_properties_at (make_number (0), elt);
16889
16890 /* If the starting string's properties are not what
16891 we want, translate the string. Also, if the string
16892 is risky, do that anyway. */
16893
16894 if (NILP (Fequal (props, oprops)) || risky)
16895 {
16896 /* If the starting string has properties,
16897 merge the specified ones onto the existing ones. */
16898 if (! NILP (oprops) && !risky)
16899 {
16900 Lisp_Object tem;
16901
16902 oprops = Fcopy_sequence (oprops);
16903 tem = props;
16904 while (CONSP (tem))
16905 {
16906 oprops = Fplist_put (oprops, XCAR (tem),
16907 XCAR (XCDR (tem)));
16908 tem = XCDR (XCDR (tem));
16909 }
16910 props = oprops;
16911 }
16912
16913 aelt = Fassoc (elt, mode_line_proptrans_alist);
16914 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16915 {
16916 /* AELT is what we want. Move it to the front
16917 without consing. */
16918 elt = XCAR (aelt);
16919 mode_line_proptrans_alist
16920 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16921 }
16922 else
16923 {
16924 Lisp_Object tem;
16925
16926 /* If AELT has the wrong props, it is useless.
16927 so get rid of it. */
16928 if (! NILP (aelt))
16929 mode_line_proptrans_alist
16930 = Fdelq (aelt, mode_line_proptrans_alist);
16931
16932 elt = Fcopy_sequence (elt);
16933 Fset_text_properties (make_number (0), Flength (elt),
16934 props, elt);
16935 /* Add this item to mode_line_proptrans_alist. */
16936 mode_line_proptrans_alist
16937 = Fcons (Fcons (elt, props),
16938 mode_line_proptrans_alist);
16939 /* Truncate mode_line_proptrans_alist
16940 to at most 50 elements. */
16941 tem = Fnthcdr (make_number (50),
16942 mode_line_proptrans_alist);
16943 if (! NILP (tem))
16944 XSETCDR (tem, Qnil);
16945 }
16946 }
16947 }
16948
16949 offset = 0;
16950
16951 if (literal)
16952 {
16953 prec = precision - n;
16954 switch (mode_line_target)
16955 {
16956 case MODE_LINE_NOPROP:
16957 case MODE_LINE_TITLE:
16958 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16959 break;
16960 case MODE_LINE_STRING:
16961 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16962 break;
16963 case MODE_LINE_DISPLAY:
16964 n += display_string (NULL, elt, Qnil, 0, 0, it,
16965 0, prec, 0, STRING_MULTIBYTE (elt));
16966 break;
16967 }
16968
16969 break;
16970 }
16971
16972 /* Handle the non-literal case. */
16973
16974 while ((precision <= 0 || n < precision)
16975 && SREF (elt, offset) != 0
16976 && (mode_line_target != MODE_LINE_DISPLAY
16977 || it->current_x < it->last_visible_x))
16978 {
16979 int last_offset = offset;
16980
16981 /* Advance to end of string or next format specifier. */
16982 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16983 ;
16984
16985 if (offset - 1 != last_offset)
16986 {
16987 int nchars, nbytes;
16988
16989 /* Output to end of string or up to '%'. Field width
16990 is length of string. Don't output more than
16991 PRECISION allows us. */
16992 offset--;
16993
16994 prec = c_string_width (SDATA (elt) + last_offset,
16995 offset - last_offset, precision - n,
16996 &nchars, &nbytes);
16997
16998 switch (mode_line_target)
16999 {
17000 case MODE_LINE_NOPROP:
17001 case MODE_LINE_TITLE:
17002 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17003 break;
17004 case MODE_LINE_STRING:
17005 {
17006 int bytepos = last_offset;
17007 int charpos = string_byte_to_char (elt, bytepos);
17008 int endpos = (precision <= 0
17009 ? string_byte_to_char (elt, offset)
17010 : charpos + nchars);
17011
17012 n += store_mode_line_string (NULL,
17013 Fsubstring (elt, make_number (charpos),
17014 make_number (endpos)),
17015 0, 0, 0, Qnil);
17016 }
17017 break;
17018 case MODE_LINE_DISPLAY:
17019 {
17020 int bytepos = last_offset;
17021 int charpos = string_byte_to_char (elt, bytepos);
17022
17023 if (precision <= 0)
17024 nchars = string_byte_to_char (elt, offset) - charpos;
17025 n += display_string (NULL, elt, Qnil, 0, charpos,
17026 it, 0, nchars, 0,
17027 STRING_MULTIBYTE (elt));
17028 }
17029 break;
17030 }
17031 }
17032 else /* c == '%' */
17033 {
17034 int percent_position = offset;
17035
17036 /* Get the specified minimum width. Zero means
17037 don't pad. */
17038 field = 0;
17039 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17040 field = field * 10 + c - '0';
17041
17042 /* Don't pad beyond the total padding allowed. */
17043 if (field_width - n > 0 && field > field_width - n)
17044 field = field_width - n;
17045
17046 /* Note that either PRECISION <= 0 or N < PRECISION. */
17047 prec = precision - n;
17048
17049 if (c == 'M')
17050 n += display_mode_element (it, depth, field, prec,
17051 Vglobal_mode_string, props,
17052 risky);
17053 else if (c != 0)
17054 {
17055 int multibyte;
17056 int bytepos, charpos;
17057 unsigned char *spec;
17058
17059 bytepos = percent_position;
17060 charpos = (STRING_MULTIBYTE (elt)
17061 ? string_byte_to_char (elt, bytepos)
17062 : bytepos);
17063
17064 spec
17065 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17066
17067 switch (mode_line_target)
17068 {
17069 case MODE_LINE_NOPROP:
17070 case MODE_LINE_TITLE:
17071 n += store_mode_line_noprop (spec, field, prec);
17072 break;
17073 case MODE_LINE_STRING:
17074 {
17075 int len = strlen (spec);
17076 Lisp_Object tem = make_string (spec, len);
17077 props = Ftext_properties_at (make_number (charpos), elt);
17078 /* Should only keep face property in props */
17079 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17080 }
17081 break;
17082 case MODE_LINE_DISPLAY:
17083 {
17084 int nglyphs_before, nwritten;
17085
17086 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17087 nwritten = display_string (spec, Qnil, elt,
17088 charpos, 0, it,
17089 field, prec, 0,
17090 multibyte);
17091
17092 /* Assign to the glyphs written above the
17093 string where the `%x' came from, position
17094 of the `%'. */
17095 if (nwritten > 0)
17096 {
17097 struct glyph *glyph
17098 = (it->glyph_row->glyphs[TEXT_AREA]
17099 + nglyphs_before);
17100 int i;
17101
17102 for (i = 0; i < nwritten; ++i)
17103 {
17104 glyph[i].object = elt;
17105 glyph[i].charpos = charpos;
17106 }
17107
17108 n += nwritten;
17109 }
17110 }
17111 break;
17112 }
17113 }
17114 else /* c == 0 */
17115 break;
17116 }
17117 }
17118 }
17119 break;
17120
17121 case Lisp_Symbol:
17122 /* A symbol: process the value of the symbol recursively
17123 as if it appeared here directly. Avoid error if symbol void.
17124 Special case: if value of symbol is a string, output the string
17125 literally. */
17126 {
17127 register Lisp_Object tem;
17128
17129 /* If the variable is not marked as risky to set
17130 then its contents are risky to use. */
17131 if (NILP (Fget (elt, Qrisky_local_variable)))
17132 risky = 1;
17133
17134 tem = Fboundp (elt);
17135 if (!NILP (tem))
17136 {
17137 tem = Fsymbol_value (elt);
17138 /* If value is a string, output that string literally:
17139 don't check for % within it. */
17140 if (STRINGP (tem))
17141 literal = 1;
17142
17143 if (!EQ (tem, elt))
17144 {
17145 /* Give up right away for nil or t. */
17146 elt = tem;
17147 goto tail_recurse;
17148 }
17149 }
17150 }
17151 break;
17152
17153 case Lisp_Cons:
17154 {
17155 register Lisp_Object car, tem;
17156
17157 /* A cons cell: five distinct cases.
17158 If first element is :eval or :propertize, do something special.
17159 If first element is a string or a cons, process all the elements
17160 and effectively concatenate them.
17161 If first element is a negative number, truncate displaying cdr to
17162 at most that many characters. If positive, pad (with spaces)
17163 to at least that many characters.
17164 If first element is a symbol, process the cadr or caddr recursively
17165 according to whether the symbol's value is non-nil or nil. */
17166 car = XCAR (elt);
17167 if (EQ (car, QCeval))
17168 {
17169 /* An element of the form (:eval FORM) means evaluate FORM
17170 and use the result as mode line elements. */
17171
17172 if (risky)
17173 break;
17174
17175 if (CONSP (XCDR (elt)))
17176 {
17177 Lisp_Object spec;
17178 spec = safe_eval (XCAR (XCDR (elt)));
17179 n += display_mode_element (it, depth, field_width - n,
17180 precision - n, spec, props,
17181 risky);
17182 }
17183 }
17184 else if (EQ (car, QCpropertize))
17185 {
17186 /* An element of the form (:propertize ELT PROPS...)
17187 means display ELT but applying properties PROPS. */
17188
17189 if (risky)
17190 break;
17191
17192 if (CONSP (XCDR (elt)))
17193 n += display_mode_element (it, depth, field_width - n,
17194 precision - n, XCAR (XCDR (elt)),
17195 XCDR (XCDR (elt)), risky);
17196 }
17197 else if (SYMBOLP (car))
17198 {
17199 tem = Fboundp (car);
17200 elt = XCDR (elt);
17201 if (!CONSP (elt))
17202 goto invalid;
17203 /* elt is now the cdr, and we know it is a cons cell.
17204 Use its car if CAR has a non-nil value. */
17205 if (!NILP (tem))
17206 {
17207 tem = Fsymbol_value (car);
17208 if (!NILP (tem))
17209 {
17210 elt = XCAR (elt);
17211 goto tail_recurse;
17212 }
17213 }
17214 /* Symbol's value is nil (or symbol is unbound)
17215 Get the cddr of the original list
17216 and if possible find the caddr and use that. */
17217 elt = XCDR (elt);
17218 if (NILP (elt))
17219 break;
17220 else if (!CONSP (elt))
17221 goto invalid;
17222 elt = XCAR (elt);
17223 goto tail_recurse;
17224 }
17225 else if (INTEGERP (car))
17226 {
17227 register int lim = XINT (car);
17228 elt = XCDR (elt);
17229 if (lim < 0)
17230 {
17231 /* Negative int means reduce maximum width. */
17232 if (precision <= 0)
17233 precision = -lim;
17234 else
17235 precision = min (precision, -lim);
17236 }
17237 else if (lim > 0)
17238 {
17239 /* Padding specified. Don't let it be more than
17240 current maximum. */
17241 if (precision > 0)
17242 lim = min (precision, lim);
17243
17244 /* If that's more padding than already wanted, queue it.
17245 But don't reduce padding already specified even if
17246 that is beyond the current truncation point. */
17247 field_width = max (lim, field_width);
17248 }
17249 goto tail_recurse;
17250 }
17251 else if (STRINGP (car) || CONSP (car))
17252 {
17253 register int limit = 50;
17254 /* Limit is to protect against circular lists. */
17255 while (CONSP (elt)
17256 && --limit > 0
17257 && (precision <= 0 || n < precision))
17258 {
17259 n += display_mode_element (it, depth,
17260 /* Do padding only after the last
17261 element in the list. */
17262 (! CONSP (XCDR (elt))
17263 ? field_width - n
17264 : 0),
17265 precision - n, XCAR (elt),
17266 props, risky);
17267 elt = XCDR (elt);
17268 }
17269 }
17270 }
17271 break;
17272
17273 default:
17274 invalid:
17275 elt = build_string ("*invalid*");
17276 goto tail_recurse;
17277 }
17278
17279 /* Pad to FIELD_WIDTH. */
17280 if (field_width > 0 && n < field_width)
17281 {
17282 switch (mode_line_target)
17283 {
17284 case MODE_LINE_NOPROP:
17285 case MODE_LINE_TITLE:
17286 n += store_mode_line_noprop ("", field_width - n, 0);
17287 break;
17288 case MODE_LINE_STRING:
17289 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17290 break;
17291 case MODE_LINE_DISPLAY:
17292 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17293 0, 0, 0);
17294 break;
17295 }
17296 }
17297
17298 return n;
17299 }
17300
17301 /* Store a mode-line string element in mode_line_string_list.
17302
17303 If STRING is non-null, display that C string. Otherwise, the Lisp
17304 string LISP_STRING is displayed.
17305
17306 FIELD_WIDTH is the minimum number of output glyphs to produce.
17307 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17308 with spaces. FIELD_WIDTH <= 0 means don't pad.
17309
17310 PRECISION is the maximum number of characters to output from
17311 STRING. PRECISION <= 0 means don't truncate the string.
17312
17313 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17314 properties to the string.
17315
17316 PROPS are the properties to add to the string.
17317 The mode_line_string_face face property is always added to the string.
17318 */
17319
17320 static int
17321 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17322 char *string;
17323 Lisp_Object lisp_string;
17324 int copy_string;
17325 int field_width;
17326 int precision;
17327 Lisp_Object props;
17328 {
17329 int len;
17330 int n = 0;
17331
17332 if (string != NULL)
17333 {
17334 len = strlen (string);
17335 if (precision > 0 && len > precision)
17336 len = precision;
17337 lisp_string = make_string (string, len);
17338 if (NILP (props))
17339 props = mode_line_string_face_prop;
17340 else if (!NILP (mode_line_string_face))
17341 {
17342 Lisp_Object face = Fplist_get (props, Qface);
17343 props = Fcopy_sequence (props);
17344 if (NILP (face))
17345 face = mode_line_string_face;
17346 else
17347 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17348 props = Fplist_put (props, Qface, face);
17349 }
17350 Fadd_text_properties (make_number (0), make_number (len),
17351 props, lisp_string);
17352 }
17353 else
17354 {
17355 len = XFASTINT (Flength (lisp_string));
17356 if (precision > 0 && len > precision)
17357 {
17358 len = precision;
17359 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17360 precision = -1;
17361 }
17362 if (!NILP (mode_line_string_face))
17363 {
17364 Lisp_Object face;
17365 if (NILP (props))
17366 props = Ftext_properties_at (make_number (0), lisp_string);
17367 face = Fplist_get (props, Qface);
17368 if (NILP (face))
17369 face = mode_line_string_face;
17370 else
17371 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17372 props = Fcons (Qface, Fcons (face, Qnil));
17373 if (copy_string)
17374 lisp_string = Fcopy_sequence (lisp_string);
17375 }
17376 if (!NILP (props))
17377 Fadd_text_properties (make_number (0), make_number (len),
17378 props, lisp_string);
17379 }
17380
17381 if (len > 0)
17382 {
17383 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17384 n += len;
17385 }
17386
17387 if (field_width > len)
17388 {
17389 field_width -= len;
17390 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17391 if (!NILP (props))
17392 Fadd_text_properties (make_number (0), make_number (field_width),
17393 props, lisp_string);
17394 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17395 n += field_width;
17396 }
17397
17398 return n;
17399 }
17400
17401
17402 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17403 1, 4, 0,
17404 doc: /* Format a string out of a mode line format specification.
17405 First arg FORMAT specifies the mode line format (see `mode-line-format'
17406 for details) to use.
17407
17408 Optional second arg FACE specifies the face property to put
17409 on all characters for which no face is specified.
17410 The value t means whatever face the window's mode line currently uses
17411 \(either `mode-line' or `mode-line-inactive', depending).
17412 A value of nil means the default is no face property.
17413 If FACE is an integer, the value string has no text properties.
17414
17415 Optional third and fourth args WINDOW and BUFFER specify the window
17416 and buffer to use as the context for the formatting (defaults
17417 are the selected window and the window's buffer). */)
17418 (format, face, window, buffer)
17419 Lisp_Object format, face, window, buffer;
17420 {
17421 struct it it;
17422 int len;
17423 struct window *w;
17424 struct buffer *old_buffer = NULL;
17425 int face_id = -1;
17426 int no_props = INTEGERP (face);
17427 int count = SPECPDL_INDEX ();
17428 Lisp_Object str;
17429 int string_start = 0;
17430
17431 if (NILP (window))
17432 window = selected_window;
17433 CHECK_WINDOW (window);
17434 w = XWINDOW (window);
17435
17436 if (NILP (buffer))
17437 buffer = w->buffer;
17438 CHECK_BUFFER (buffer);
17439
17440 /* Make formatting the modeline a non-op when noninteractive, otherwise
17441 there will be problems later caused by a partially initialized frame. */
17442 if (NILP (format) || noninteractive)
17443 return empty_unibyte_string;
17444
17445 if (no_props)
17446 face = Qnil;
17447
17448 if (!NILP (face))
17449 {
17450 if (EQ (face, Qt))
17451 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17452 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17453 }
17454
17455 if (face_id < 0)
17456 face_id = DEFAULT_FACE_ID;
17457
17458 if (XBUFFER (buffer) != current_buffer)
17459 old_buffer = current_buffer;
17460
17461 /* Save things including mode_line_proptrans_alist,
17462 and set that to nil so that we don't alter the outer value. */
17463 record_unwind_protect (unwind_format_mode_line,
17464 format_mode_line_unwind_data (old_buffer, 1));
17465 mode_line_proptrans_alist = Qnil;
17466
17467 if (old_buffer)
17468 set_buffer_internal_1 (XBUFFER (buffer));
17469
17470 init_iterator (&it, w, -1, -1, NULL, face_id);
17471
17472 if (no_props)
17473 {
17474 mode_line_target = MODE_LINE_NOPROP;
17475 mode_line_string_face_prop = Qnil;
17476 mode_line_string_list = Qnil;
17477 string_start = MODE_LINE_NOPROP_LEN (0);
17478 }
17479 else
17480 {
17481 mode_line_target = MODE_LINE_STRING;
17482 mode_line_string_list = Qnil;
17483 mode_line_string_face = face;
17484 mode_line_string_face_prop
17485 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17486 }
17487
17488 push_kboard (FRAME_KBOARD (it.f));
17489 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17490 pop_kboard ();
17491
17492 if (no_props)
17493 {
17494 len = MODE_LINE_NOPROP_LEN (string_start);
17495 str = make_string (mode_line_noprop_buf + string_start, len);
17496 }
17497 else
17498 {
17499 mode_line_string_list = Fnreverse (mode_line_string_list);
17500 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17501 empty_unibyte_string);
17502 }
17503
17504 unbind_to (count, Qnil);
17505 return str;
17506 }
17507
17508 /* Write a null-terminated, right justified decimal representation of
17509 the positive integer D to BUF using a minimal field width WIDTH. */
17510
17511 static void
17512 pint2str (buf, width, d)
17513 register char *buf;
17514 register int width;
17515 register int d;
17516 {
17517 register char *p = buf;
17518
17519 if (d <= 0)
17520 *p++ = '0';
17521 else
17522 {
17523 while (d > 0)
17524 {
17525 *p++ = d % 10 + '0';
17526 d /= 10;
17527 }
17528 }
17529
17530 for (width -= (int) (p - buf); width > 0; --width)
17531 *p++ = ' ';
17532 *p-- = '\0';
17533 while (p > buf)
17534 {
17535 d = *buf;
17536 *buf++ = *p;
17537 *p-- = d;
17538 }
17539 }
17540
17541 /* Write a null-terminated, right justified decimal and "human
17542 readable" representation of the nonnegative integer D to BUF using
17543 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17544
17545 static const char power_letter[] =
17546 {
17547 0, /* not used */
17548 'k', /* kilo */
17549 'M', /* mega */
17550 'G', /* giga */
17551 'T', /* tera */
17552 'P', /* peta */
17553 'E', /* exa */
17554 'Z', /* zetta */
17555 'Y' /* yotta */
17556 };
17557
17558 static void
17559 pint2hrstr (buf, width, d)
17560 char *buf;
17561 int width;
17562 int d;
17563 {
17564 /* We aim to represent the nonnegative integer D as
17565 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17566 int quotient = d;
17567 int remainder = 0;
17568 /* -1 means: do not use TENTHS. */
17569 int tenths = -1;
17570 int exponent = 0;
17571
17572 /* Length of QUOTIENT.TENTHS as a string. */
17573 int length;
17574
17575 char * psuffix;
17576 char * p;
17577
17578 if (1000 <= quotient)
17579 {
17580 /* Scale to the appropriate EXPONENT. */
17581 do
17582 {
17583 remainder = quotient % 1000;
17584 quotient /= 1000;
17585 exponent++;
17586 }
17587 while (1000 <= quotient);
17588
17589 /* Round to nearest and decide whether to use TENTHS or not. */
17590 if (quotient <= 9)
17591 {
17592 tenths = remainder / 100;
17593 if (50 <= remainder % 100)
17594 {
17595 if (tenths < 9)
17596 tenths++;
17597 else
17598 {
17599 quotient++;
17600 if (quotient == 10)
17601 tenths = -1;
17602 else
17603 tenths = 0;
17604 }
17605 }
17606 }
17607 else
17608 if (500 <= remainder)
17609 {
17610 if (quotient < 999)
17611 quotient++;
17612 else
17613 {
17614 quotient = 1;
17615 exponent++;
17616 tenths = 0;
17617 }
17618 }
17619 }
17620
17621 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17622 if (tenths == -1 && quotient <= 99)
17623 if (quotient <= 9)
17624 length = 1;
17625 else
17626 length = 2;
17627 else
17628 length = 3;
17629 p = psuffix = buf + max (width, length);
17630
17631 /* Print EXPONENT. */
17632 if (exponent)
17633 *psuffix++ = power_letter[exponent];
17634 *psuffix = '\0';
17635
17636 /* Print TENTHS. */
17637 if (tenths >= 0)
17638 {
17639 *--p = '0' + tenths;
17640 *--p = '.';
17641 }
17642
17643 /* Print QUOTIENT. */
17644 do
17645 {
17646 int digit = quotient % 10;
17647 *--p = '0' + digit;
17648 }
17649 while ((quotient /= 10) != 0);
17650
17651 /* Print leading spaces. */
17652 while (buf < p)
17653 *--p = ' ';
17654 }
17655
17656 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17657 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17658 type of CODING_SYSTEM. Return updated pointer into BUF. */
17659
17660 static unsigned char invalid_eol_type[] = "(*invalid*)";
17661
17662 static char *
17663 decode_mode_spec_coding (coding_system, buf, eol_flag)
17664 Lisp_Object coding_system;
17665 register char *buf;
17666 int eol_flag;
17667 {
17668 Lisp_Object val;
17669 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17670 const unsigned char *eol_str;
17671 int eol_str_len;
17672 /* The EOL conversion we are using. */
17673 Lisp_Object eoltype;
17674
17675 val = Fget (coding_system, Qcoding_system);
17676 eoltype = Qnil;
17677
17678 if (!VECTORP (val)) /* Not yet decided. */
17679 {
17680 if (multibyte)
17681 *buf++ = '-';
17682 if (eol_flag)
17683 eoltype = eol_mnemonic_undecided;
17684 /* Don't mention EOL conversion if it isn't decided. */
17685 }
17686 else
17687 {
17688 Lisp_Object eolvalue;
17689
17690 eolvalue = Fget (coding_system, Qeol_type);
17691
17692 if (multibyte)
17693 *buf++ = XFASTINT (AREF (val, 1));
17694
17695 if (eol_flag)
17696 {
17697 /* The EOL conversion that is normal on this system. */
17698
17699 if (NILP (eolvalue)) /* Not yet decided. */
17700 eoltype = eol_mnemonic_undecided;
17701 else if (VECTORP (eolvalue)) /* Not yet decided. */
17702 eoltype = eol_mnemonic_undecided;
17703 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17704 eoltype = (XFASTINT (eolvalue) == 0
17705 ? eol_mnemonic_unix
17706 : (XFASTINT (eolvalue) == 1
17707 ? eol_mnemonic_dos : eol_mnemonic_mac));
17708 }
17709 }
17710
17711 if (eol_flag)
17712 {
17713 /* Mention the EOL conversion if it is not the usual one. */
17714 if (STRINGP (eoltype))
17715 {
17716 eol_str = SDATA (eoltype);
17717 eol_str_len = SBYTES (eoltype);
17718 }
17719 else if (INTEGERP (eoltype)
17720 && CHAR_VALID_P (XINT (eoltype), 0))
17721 {
17722 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17723 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17724 eol_str = tmp;
17725 }
17726 else
17727 {
17728 eol_str = invalid_eol_type;
17729 eol_str_len = sizeof (invalid_eol_type) - 1;
17730 }
17731 bcopy (eol_str, buf, eol_str_len);
17732 buf += eol_str_len;
17733 }
17734
17735 return buf;
17736 }
17737
17738 /* Return a string for the output of a mode line %-spec for window W,
17739 generated by character C. PRECISION >= 0 means don't return a
17740 string longer than that value. FIELD_WIDTH > 0 means pad the
17741 string returned with spaces to that value. Return 1 in *MULTIBYTE
17742 if the result is multibyte text.
17743
17744 Note we operate on the current buffer for most purposes,
17745 the exception being w->base_line_pos. */
17746
17747 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17748
17749 static char *
17750 decode_mode_spec (w, c, field_width, precision, multibyte)
17751 struct window *w;
17752 register int c;
17753 int field_width, precision;
17754 int *multibyte;
17755 {
17756 Lisp_Object obj;
17757 struct frame *f = XFRAME (WINDOW_FRAME (w));
17758 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17759 struct buffer *b = current_buffer;
17760
17761 obj = Qnil;
17762 *multibyte = 0;
17763
17764 switch (c)
17765 {
17766 case '*':
17767 if (!NILP (b->read_only))
17768 return "%";
17769 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17770 return "*";
17771 return "-";
17772
17773 case '+':
17774 /* This differs from %* only for a modified read-only buffer. */
17775 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17776 return "*";
17777 if (!NILP (b->read_only))
17778 return "%";
17779 return "-";
17780
17781 case '&':
17782 /* This differs from %* in ignoring read-only-ness. */
17783 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17784 return "*";
17785 return "-";
17786
17787 case '%':
17788 return "%";
17789
17790 case '[':
17791 {
17792 int i;
17793 char *p;
17794
17795 if (command_loop_level > 5)
17796 return "[[[... ";
17797 p = decode_mode_spec_buf;
17798 for (i = 0; i < command_loop_level; i++)
17799 *p++ = '[';
17800 *p = 0;
17801 return decode_mode_spec_buf;
17802 }
17803
17804 case ']':
17805 {
17806 int i;
17807 char *p;
17808
17809 if (command_loop_level > 5)
17810 return " ...]]]";
17811 p = decode_mode_spec_buf;
17812 for (i = 0; i < command_loop_level; i++)
17813 *p++ = ']';
17814 *p = 0;
17815 return decode_mode_spec_buf;
17816 }
17817
17818 case '-':
17819 {
17820 register int i;
17821
17822 /* Let lots_of_dashes be a string of infinite length. */
17823 if (mode_line_target == MODE_LINE_NOPROP ||
17824 mode_line_target == MODE_LINE_STRING)
17825 return "--";
17826 if (field_width <= 0
17827 || field_width > sizeof (lots_of_dashes))
17828 {
17829 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17830 decode_mode_spec_buf[i] = '-';
17831 decode_mode_spec_buf[i] = '\0';
17832 return decode_mode_spec_buf;
17833 }
17834 else
17835 return lots_of_dashes;
17836 }
17837
17838 case 'b':
17839 obj = b->name;
17840 break;
17841
17842 case 'c':
17843 /* %c and %l are ignored in `frame-title-format'.
17844 (In redisplay_internal, the frame title is drawn _before_ the
17845 windows are updated, so the stuff which depends on actual
17846 window contents (such as %l) may fail to render properly, or
17847 even crash emacs.) */
17848 if (mode_line_target == MODE_LINE_TITLE)
17849 return "";
17850 else
17851 {
17852 int col = (int) current_column (); /* iftc */
17853 w->column_number_displayed = make_number (col);
17854 pint2str (decode_mode_spec_buf, field_width, col);
17855 return decode_mode_spec_buf;
17856 }
17857
17858 case 'e':
17859 #ifndef SYSTEM_MALLOC
17860 {
17861 if (NILP (Vmemory_full))
17862 return "";
17863 else
17864 return "!MEM FULL! ";
17865 }
17866 #else
17867 return "";
17868 #endif
17869
17870 case 'F':
17871 /* %F displays the frame name. */
17872 if (!NILP (f->title))
17873 return (char *) SDATA (f->title);
17874 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17875 return (char *) SDATA (f->name);
17876 return "Emacs";
17877
17878 case 'f':
17879 obj = b->filename;
17880 break;
17881
17882 case 'i':
17883 {
17884 int size = ZV - BEGV;
17885 pint2str (decode_mode_spec_buf, field_width, size);
17886 return decode_mode_spec_buf;
17887 }
17888
17889 case 'I':
17890 {
17891 int size = ZV - BEGV;
17892 pint2hrstr (decode_mode_spec_buf, field_width, size);
17893 return decode_mode_spec_buf;
17894 }
17895
17896 case 'l':
17897 {
17898 int startpos, startpos_byte, line, linepos, linepos_byte;
17899 int topline, nlines, junk, height;
17900
17901 /* %c and %l are ignored in `frame-title-format'. */
17902 if (mode_line_target == MODE_LINE_TITLE)
17903 return "";
17904
17905 startpos = XMARKER (w->start)->charpos;
17906 startpos_byte = marker_byte_position (w->start);
17907 height = WINDOW_TOTAL_LINES (w);
17908
17909 /* If we decided that this buffer isn't suitable for line numbers,
17910 don't forget that too fast. */
17911 if (EQ (w->base_line_pos, w->buffer))
17912 goto no_value;
17913 /* But do forget it, if the window shows a different buffer now. */
17914 else if (BUFFERP (w->base_line_pos))
17915 w->base_line_pos = Qnil;
17916
17917 /* If the buffer is very big, don't waste time. */
17918 if (INTEGERP (Vline_number_display_limit)
17919 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17920 {
17921 w->base_line_pos = Qnil;
17922 w->base_line_number = Qnil;
17923 goto no_value;
17924 }
17925
17926 if (!NILP (w->base_line_number)
17927 && !NILP (w->base_line_pos)
17928 && XFASTINT (w->base_line_pos) <= startpos)
17929 {
17930 line = XFASTINT (w->base_line_number);
17931 linepos = XFASTINT (w->base_line_pos);
17932 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17933 }
17934 else
17935 {
17936 line = 1;
17937 linepos = BUF_BEGV (b);
17938 linepos_byte = BUF_BEGV_BYTE (b);
17939 }
17940
17941 /* Count lines from base line to window start position. */
17942 nlines = display_count_lines (linepos, linepos_byte,
17943 startpos_byte,
17944 startpos, &junk);
17945
17946 topline = nlines + line;
17947
17948 /* Determine a new base line, if the old one is too close
17949 or too far away, or if we did not have one.
17950 "Too close" means it's plausible a scroll-down would
17951 go back past it. */
17952 if (startpos == BUF_BEGV (b))
17953 {
17954 w->base_line_number = make_number (topline);
17955 w->base_line_pos = make_number (BUF_BEGV (b));
17956 }
17957 else if (nlines < height + 25 || nlines > height * 3 + 50
17958 || linepos == BUF_BEGV (b))
17959 {
17960 int limit = BUF_BEGV (b);
17961 int limit_byte = BUF_BEGV_BYTE (b);
17962 int position;
17963 int distance = (height * 2 + 30) * line_number_display_limit_width;
17964
17965 if (startpos - distance > limit)
17966 {
17967 limit = startpos - distance;
17968 limit_byte = CHAR_TO_BYTE (limit);
17969 }
17970
17971 nlines = display_count_lines (startpos, startpos_byte,
17972 limit_byte,
17973 - (height * 2 + 30),
17974 &position);
17975 /* If we couldn't find the lines we wanted within
17976 line_number_display_limit_width chars per line,
17977 give up on line numbers for this window. */
17978 if (position == limit_byte && limit == startpos - distance)
17979 {
17980 w->base_line_pos = w->buffer;
17981 w->base_line_number = Qnil;
17982 goto no_value;
17983 }
17984
17985 w->base_line_number = make_number (topline - nlines);
17986 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17987 }
17988
17989 /* Now count lines from the start pos to point. */
17990 nlines = display_count_lines (startpos, startpos_byte,
17991 PT_BYTE, PT, &junk);
17992
17993 /* Record that we did display the line number. */
17994 line_number_displayed = 1;
17995
17996 /* Make the string to show. */
17997 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17998 return decode_mode_spec_buf;
17999 no_value:
18000 {
18001 char* p = decode_mode_spec_buf;
18002 int pad = field_width - 2;
18003 while (pad-- > 0)
18004 *p++ = ' ';
18005 *p++ = '?';
18006 *p++ = '?';
18007 *p = '\0';
18008 return decode_mode_spec_buf;
18009 }
18010 }
18011 break;
18012
18013 case 'm':
18014 obj = b->mode_name;
18015 break;
18016
18017 case 'n':
18018 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18019 return " Narrow";
18020 break;
18021
18022 case 'p':
18023 {
18024 int pos = marker_position (w->start);
18025 int total = BUF_ZV (b) - BUF_BEGV (b);
18026
18027 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18028 {
18029 if (pos <= BUF_BEGV (b))
18030 return "All";
18031 else
18032 return "Bottom";
18033 }
18034 else if (pos <= BUF_BEGV (b))
18035 return "Top";
18036 else
18037 {
18038 if (total > 1000000)
18039 /* Do it differently for a large value, to avoid overflow. */
18040 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18041 else
18042 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18043 /* We can't normally display a 3-digit number,
18044 so get us a 2-digit number that is close. */
18045 if (total == 100)
18046 total = 99;
18047 sprintf (decode_mode_spec_buf, "%2d%%", total);
18048 return decode_mode_spec_buf;
18049 }
18050 }
18051
18052 /* Display percentage of size above the bottom of the screen. */
18053 case 'P':
18054 {
18055 int toppos = marker_position (w->start);
18056 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18057 int total = BUF_ZV (b) - BUF_BEGV (b);
18058
18059 if (botpos >= BUF_ZV (b))
18060 {
18061 if (toppos <= BUF_BEGV (b))
18062 return "All";
18063 else
18064 return "Bottom";
18065 }
18066 else
18067 {
18068 if (total > 1000000)
18069 /* Do it differently for a large value, to avoid overflow. */
18070 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18071 else
18072 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18073 /* We can't normally display a 3-digit number,
18074 so get us a 2-digit number that is close. */
18075 if (total == 100)
18076 total = 99;
18077 if (toppos <= BUF_BEGV (b))
18078 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18079 else
18080 sprintf (decode_mode_spec_buf, "%2d%%", total);
18081 return decode_mode_spec_buf;
18082 }
18083 }
18084
18085 case 's':
18086 /* status of process */
18087 obj = Fget_buffer_process (Fcurrent_buffer ());
18088 if (NILP (obj))
18089 return "no process";
18090 #ifdef subprocesses
18091 obj = Fsymbol_name (Fprocess_status (obj));
18092 #endif
18093 break;
18094
18095 case '@':
18096 {
18097 Lisp_Object val;
18098 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18099 if (NILP (val))
18100 return "-";
18101 else
18102 return "@";
18103 }
18104
18105 case 't': /* indicate TEXT or BINARY */
18106 #ifdef MODE_LINE_BINARY_TEXT
18107 return MODE_LINE_BINARY_TEXT (b);
18108 #else
18109 return "T";
18110 #endif
18111
18112 case 'z':
18113 /* coding-system (not including end-of-line format) */
18114 case 'Z':
18115 /* coding-system (including end-of-line type) */
18116 {
18117 int eol_flag = (c == 'Z');
18118 char *p = decode_mode_spec_buf;
18119
18120 if (! FRAME_WINDOW_P (f))
18121 {
18122 /* No need to mention EOL here--the terminal never needs
18123 to do EOL conversion. */
18124 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
18125 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
18126 }
18127 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18128 p, eol_flag);
18129
18130 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18131 #ifdef subprocesses
18132 obj = Fget_buffer_process (Fcurrent_buffer ());
18133 if (PROCESSP (obj))
18134 {
18135 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18136 p, eol_flag);
18137 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18138 p, eol_flag);
18139 }
18140 #endif /* subprocesses */
18141 #endif /* 0 */
18142 *p = 0;
18143 return decode_mode_spec_buf;
18144 }
18145 }
18146
18147 if (STRINGP (obj))
18148 {
18149 *multibyte = STRING_MULTIBYTE (obj);
18150 return (char *) SDATA (obj);
18151 }
18152 else
18153 return "";
18154 }
18155
18156
18157 /* Count up to COUNT lines starting from START / START_BYTE.
18158 But don't go beyond LIMIT_BYTE.
18159 Return the number of lines thus found (always nonnegative).
18160
18161 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18162
18163 static int
18164 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18165 int start, start_byte, limit_byte, count;
18166 int *byte_pos_ptr;
18167 {
18168 register unsigned char *cursor;
18169 unsigned char *base;
18170
18171 register int ceiling;
18172 register unsigned char *ceiling_addr;
18173 int orig_count = count;
18174
18175 /* If we are not in selective display mode,
18176 check only for newlines. */
18177 int selective_display = (!NILP (current_buffer->selective_display)
18178 && !INTEGERP (current_buffer->selective_display));
18179
18180 if (count > 0)
18181 {
18182 while (start_byte < limit_byte)
18183 {
18184 ceiling = BUFFER_CEILING_OF (start_byte);
18185 ceiling = min (limit_byte - 1, ceiling);
18186 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18187 base = (cursor = BYTE_POS_ADDR (start_byte));
18188 while (1)
18189 {
18190 if (selective_display)
18191 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18192 ;
18193 else
18194 while (*cursor != '\n' && ++cursor != ceiling_addr)
18195 ;
18196
18197 if (cursor != ceiling_addr)
18198 {
18199 if (--count == 0)
18200 {
18201 start_byte += cursor - base + 1;
18202 *byte_pos_ptr = start_byte;
18203 return orig_count;
18204 }
18205 else
18206 if (++cursor == ceiling_addr)
18207 break;
18208 }
18209 else
18210 break;
18211 }
18212 start_byte += cursor - base;
18213 }
18214 }
18215 else
18216 {
18217 while (start_byte > limit_byte)
18218 {
18219 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18220 ceiling = max (limit_byte, ceiling);
18221 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18222 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18223 while (1)
18224 {
18225 if (selective_display)
18226 while (--cursor != ceiling_addr
18227 && *cursor != '\n' && *cursor != 015)
18228 ;
18229 else
18230 while (--cursor != ceiling_addr && *cursor != '\n')
18231 ;
18232
18233 if (cursor != ceiling_addr)
18234 {
18235 if (++count == 0)
18236 {
18237 start_byte += cursor - base + 1;
18238 *byte_pos_ptr = start_byte;
18239 /* When scanning backwards, we should
18240 not count the newline posterior to which we stop. */
18241 return - orig_count - 1;
18242 }
18243 }
18244 else
18245 break;
18246 }
18247 /* Here we add 1 to compensate for the last decrement
18248 of CURSOR, which took it past the valid range. */
18249 start_byte += cursor - base + 1;
18250 }
18251 }
18252
18253 *byte_pos_ptr = limit_byte;
18254
18255 if (count < 0)
18256 return - orig_count + count;
18257 return orig_count - count;
18258
18259 }
18260
18261
18262 \f
18263 /***********************************************************************
18264 Displaying strings
18265 ***********************************************************************/
18266
18267 /* Display a NUL-terminated string, starting with index START.
18268
18269 If STRING is non-null, display that C string. Otherwise, the Lisp
18270 string LISP_STRING is displayed.
18271
18272 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18273 FACE_STRING. Display STRING or LISP_STRING with the face at
18274 FACE_STRING_POS in FACE_STRING:
18275
18276 Display the string in the environment given by IT, but use the
18277 standard display table, temporarily.
18278
18279 FIELD_WIDTH is the minimum number of output glyphs to produce.
18280 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18281 with spaces. If STRING has more characters, more than FIELD_WIDTH
18282 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18283
18284 PRECISION is the maximum number of characters to output from
18285 STRING. PRECISION < 0 means don't truncate the string.
18286
18287 This is roughly equivalent to printf format specifiers:
18288
18289 FIELD_WIDTH PRECISION PRINTF
18290 ----------------------------------------
18291 -1 -1 %s
18292 -1 10 %.10s
18293 10 -1 %10s
18294 20 10 %20.10s
18295
18296 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18297 display them, and < 0 means obey the current buffer's value of
18298 enable_multibyte_characters.
18299
18300 Value is the number of columns displayed. */
18301
18302 static int
18303 display_string (string, lisp_string, face_string, face_string_pos,
18304 start, it, field_width, precision, max_x, multibyte)
18305 unsigned char *string;
18306 Lisp_Object lisp_string;
18307 Lisp_Object face_string;
18308 int face_string_pos;
18309 int start;
18310 struct it *it;
18311 int field_width, precision, max_x;
18312 int multibyte;
18313 {
18314 int hpos_at_start = it->hpos;
18315 int saved_face_id = it->face_id;
18316 struct glyph_row *row = it->glyph_row;
18317
18318 /* Initialize the iterator IT for iteration over STRING beginning
18319 with index START. */
18320 reseat_to_string (it, string, lisp_string, start,
18321 precision, field_width, multibyte);
18322
18323 /* If displaying STRING, set up the face of the iterator
18324 from LISP_STRING, if that's given. */
18325 if (STRINGP (face_string))
18326 {
18327 int endptr;
18328 struct face *face;
18329
18330 it->face_id
18331 = face_at_string_position (it->w, face_string, face_string_pos,
18332 0, it->region_beg_charpos,
18333 it->region_end_charpos,
18334 &endptr, it->base_face_id, 0);
18335 face = FACE_FROM_ID (it->f, it->face_id);
18336 it->face_box_p = face->box != FACE_NO_BOX;
18337 }
18338
18339 /* Set max_x to the maximum allowed X position. Don't let it go
18340 beyond the right edge of the window. */
18341 if (max_x <= 0)
18342 max_x = it->last_visible_x;
18343 else
18344 max_x = min (max_x, it->last_visible_x);
18345
18346 /* Skip over display elements that are not visible. because IT->w is
18347 hscrolled. */
18348 if (it->current_x < it->first_visible_x)
18349 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18350 MOVE_TO_POS | MOVE_TO_X);
18351
18352 row->ascent = it->max_ascent;
18353 row->height = it->max_ascent + it->max_descent;
18354 row->phys_ascent = it->max_phys_ascent;
18355 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18356 row->extra_line_spacing = it->max_extra_line_spacing;
18357
18358 /* This condition is for the case that we are called with current_x
18359 past last_visible_x. */
18360 while (it->current_x < max_x)
18361 {
18362 int x_before, x, n_glyphs_before, i, nglyphs;
18363
18364 /* Get the next display element. */
18365 if (!get_next_display_element (it))
18366 break;
18367
18368 /* Produce glyphs. */
18369 x_before = it->current_x;
18370 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18371 PRODUCE_GLYPHS (it);
18372
18373 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18374 i = 0;
18375 x = x_before;
18376 while (i < nglyphs)
18377 {
18378 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18379
18380 if (!it->truncate_lines_p
18381 && x + glyph->pixel_width > max_x)
18382 {
18383 /* End of continued line or max_x reached. */
18384 if (CHAR_GLYPH_PADDING_P (*glyph))
18385 {
18386 /* A wide character is unbreakable. */
18387 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18388 it->current_x = x_before;
18389 }
18390 else
18391 {
18392 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18393 it->current_x = x;
18394 }
18395 break;
18396 }
18397 else if (x + glyph->pixel_width > it->first_visible_x)
18398 {
18399 /* Glyph is at least partially visible. */
18400 ++it->hpos;
18401 if (x < it->first_visible_x)
18402 it->glyph_row->x = x - it->first_visible_x;
18403 }
18404 else
18405 {
18406 /* Glyph is off the left margin of the display area.
18407 Should not happen. */
18408 abort ();
18409 }
18410
18411 row->ascent = max (row->ascent, it->max_ascent);
18412 row->height = max (row->height, it->max_ascent + it->max_descent);
18413 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18414 row->phys_height = max (row->phys_height,
18415 it->max_phys_ascent + it->max_phys_descent);
18416 row->extra_line_spacing = max (row->extra_line_spacing,
18417 it->max_extra_line_spacing);
18418 x += glyph->pixel_width;
18419 ++i;
18420 }
18421
18422 /* Stop if max_x reached. */
18423 if (i < nglyphs)
18424 break;
18425
18426 /* Stop at line ends. */
18427 if (ITERATOR_AT_END_OF_LINE_P (it))
18428 {
18429 it->continuation_lines_width = 0;
18430 break;
18431 }
18432
18433 set_iterator_to_next (it, 1);
18434
18435 /* Stop if truncating at the right edge. */
18436 if (it->truncate_lines_p
18437 && it->current_x >= it->last_visible_x)
18438 {
18439 /* Add truncation mark, but don't do it if the line is
18440 truncated at a padding space. */
18441 if (IT_CHARPOS (*it) < it->string_nchars)
18442 {
18443 if (!FRAME_WINDOW_P (it->f))
18444 {
18445 int i, n;
18446
18447 if (it->current_x > it->last_visible_x)
18448 {
18449 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18450 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18451 break;
18452 for (n = row->used[TEXT_AREA]; i < n; ++i)
18453 {
18454 row->used[TEXT_AREA] = i;
18455 produce_special_glyphs (it, IT_TRUNCATION);
18456 }
18457 }
18458 produce_special_glyphs (it, IT_TRUNCATION);
18459 }
18460 it->glyph_row->truncated_on_right_p = 1;
18461 }
18462 break;
18463 }
18464 }
18465
18466 /* Maybe insert a truncation at the left. */
18467 if (it->first_visible_x
18468 && IT_CHARPOS (*it) > 0)
18469 {
18470 if (!FRAME_WINDOW_P (it->f))
18471 insert_left_trunc_glyphs (it);
18472 it->glyph_row->truncated_on_left_p = 1;
18473 }
18474
18475 it->face_id = saved_face_id;
18476
18477 /* Value is number of columns displayed. */
18478 return it->hpos - hpos_at_start;
18479 }
18480
18481
18482 \f
18483 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18484 appears as an element of LIST or as the car of an element of LIST.
18485 If PROPVAL is a list, compare each element against LIST in that
18486 way, and return 1/2 if any element of PROPVAL is found in LIST.
18487 Otherwise return 0. This function cannot quit.
18488 The return value is 2 if the text is invisible but with an ellipsis
18489 and 1 if it's invisible and without an ellipsis. */
18490
18491 int
18492 invisible_p (propval, list)
18493 register Lisp_Object propval;
18494 Lisp_Object list;
18495 {
18496 register Lisp_Object tail, proptail;
18497
18498 for (tail = list; CONSP (tail); tail = XCDR (tail))
18499 {
18500 register Lisp_Object tem;
18501 tem = XCAR (tail);
18502 if (EQ (propval, tem))
18503 return 1;
18504 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18505 return NILP (XCDR (tem)) ? 1 : 2;
18506 }
18507
18508 if (CONSP (propval))
18509 {
18510 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18511 {
18512 Lisp_Object propelt;
18513 propelt = XCAR (proptail);
18514 for (tail = list; CONSP (tail); tail = XCDR (tail))
18515 {
18516 register Lisp_Object tem;
18517 tem = XCAR (tail);
18518 if (EQ (propelt, tem))
18519 return 1;
18520 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18521 return NILP (XCDR (tem)) ? 1 : 2;
18522 }
18523 }
18524 }
18525
18526 return 0;
18527 }
18528
18529 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18530 doc: /* Non-nil if the property makes the text invisible.
18531 POS-OR-PROP can be a marker or number, in which case it is taken to be
18532 a position in the current buffer and the value of the `invisible' property
18533 is checked; or it can be some other value, which is then presumed to be the
18534 value of the `invisible' property of the text of interest.
18535 The non-nil value returned can be t for truly invisible text or something
18536 else if the text is replaced by an ellipsis. */)
18537 (pos_or_prop)
18538 Lisp_Object pos_or_prop;
18539 {
18540 Lisp_Object prop
18541 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18542 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18543 : pos_or_prop);
18544 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18545 return (invis == 0 ? Qnil
18546 : invis == 1 ? Qt
18547 : make_number (invis));
18548 }
18549
18550 /* Calculate a width or height in pixels from a specification using
18551 the following elements:
18552
18553 SPEC ::=
18554 NUM - a (fractional) multiple of the default font width/height
18555 (NUM) - specifies exactly NUM pixels
18556 UNIT - a fixed number of pixels, see below.
18557 ELEMENT - size of a display element in pixels, see below.
18558 (NUM . SPEC) - equals NUM * SPEC
18559 (+ SPEC SPEC ...) - add pixel values
18560 (- SPEC SPEC ...) - subtract pixel values
18561 (- SPEC) - negate pixel value
18562
18563 NUM ::=
18564 INT or FLOAT - a number constant
18565 SYMBOL - use symbol's (buffer local) variable binding.
18566
18567 UNIT ::=
18568 in - pixels per inch *)
18569 mm - pixels per 1/1000 meter *)
18570 cm - pixels per 1/100 meter *)
18571 width - width of current font in pixels.
18572 height - height of current font in pixels.
18573
18574 *) using the ratio(s) defined in display-pixels-per-inch.
18575
18576 ELEMENT ::=
18577
18578 left-fringe - left fringe width in pixels
18579 right-fringe - right fringe width in pixels
18580
18581 left-margin - left margin width in pixels
18582 right-margin - right margin width in pixels
18583
18584 scroll-bar - scroll-bar area width in pixels
18585
18586 Examples:
18587
18588 Pixels corresponding to 5 inches:
18589 (5 . in)
18590
18591 Total width of non-text areas on left side of window (if scroll-bar is on left):
18592 '(space :width (+ left-fringe left-margin scroll-bar))
18593
18594 Align to first text column (in header line):
18595 '(space :align-to 0)
18596
18597 Align to middle of text area minus half the width of variable `my-image'
18598 containing a loaded image:
18599 '(space :align-to (0.5 . (- text my-image)))
18600
18601 Width of left margin minus width of 1 character in the default font:
18602 '(space :width (- left-margin 1))
18603
18604 Width of left margin minus width of 2 characters in the current font:
18605 '(space :width (- left-margin (2 . width)))
18606
18607 Center 1 character over left-margin (in header line):
18608 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18609
18610 Different ways to express width of left fringe plus left margin minus one pixel:
18611 '(space :width (- (+ left-fringe left-margin) (1)))
18612 '(space :width (+ left-fringe left-margin (- (1))))
18613 '(space :width (+ left-fringe left-margin (-1)))
18614
18615 */
18616
18617 #define NUMVAL(X) \
18618 ((INTEGERP (X) || FLOATP (X)) \
18619 ? XFLOATINT (X) \
18620 : - 1)
18621
18622 int
18623 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18624 double *res;
18625 struct it *it;
18626 Lisp_Object prop;
18627 void *font;
18628 int width_p, *align_to;
18629 {
18630 double pixels;
18631
18632 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18633 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18634
18635 if (NILP (prop))
18636 return OK_PIXELS (0);
18637
18638 xassert (FRAME_LIVE_P (it->f));
18639
18640 if (SYMBOLP (prop))
18641 {
18642 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18643 {
18644 char *unit = SDATA (SYMBOL_NAME (prop));
18645
18646 if (unit[0] == 'i' && unit[1] == 'n')
18647 pixels = 1.0;
18648 else if (unit[0] == 'm' && unit[1] == 'm')
18649 pixels = 25.4;
18650 else if (unit[0] == 'c' && unit[1] == 'm')
18651 pixels = 2.54;
18652 else
18653 pixels = 0;
18654 if (pixels > 0)
18655 {
18656 double ppi;
18657 #ifdef HAVE_WINDOW_SYSTEM
18658 if (FRAME_WINDOW_P (it->f)
18659 && (ppi = (width_p
18660 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18661 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18662 ppi > 0))
18663 return OK_PIXELS (ppi / pixels);
18664 #endif
18665
18666 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18667 || (CONSP (Vdisplay_pixels_per_inch)
18668 && (ppi = (width_p
18669 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18670 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18671 ppi > 0)))
18672 return OK_PIXELS (ppi / pixels);
18673
18674 return 0;
18675 }
18676 }
18677
18678 #ifdef HAVE_WINDOW_SYSTEM
18679 if (EQ (prop, Qheight))
18680 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18681 if (EQ (prop, Qwidth))
18682 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18683 #else
18684 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18685 return OK_PIXELS (1);
18686 #endif
18687
18688 if (EQ (prop, Qtext))
18689 return OK_PIXELS (width_p
18690 ? window_box_width (it->w, TEXT_AREA)
18691 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18692
18693 if (align_to && *align_to < 0)
18694 {
18695 *res = 0;
18696 if (EQ (prop, Qleft))
18697 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18698 if (EQ (prop, Qright))
18699 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18700 if (EQ (prop, Qcenter))
18701 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18702 + window_box_width (it->w, TEXT_AREA) / 2);
18703 if (EQ (prop, Qleft_fringe))
18704 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18705 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18706 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18707 if (EQ (prop, Qright_fringe))
18708 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18709 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18710 : window_box_right_offset (it->w, TEXT_AREA));
18711 if (EQ (prop, Qleft_margin))
18712 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18713 if (EQ (prop, Qright_margin))
18714 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18715 if (EQ (prop, Qscroll_bar))
18716 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18717 ? 0
18718 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18719 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18720 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18721 : 0)));
18722 }
18723 else
18724 {
18725 if (EQ (prop, Qleft_fringe))
18726 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18727 if (EQ (prop, Qright_fringe))
18728 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18729 if (EQ (prop, Qleft_margin))
18730 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18731 if (EQ (prop, Qright_margin))
18732 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18733 if (EQ (prop, Qscroll_bar))
18734 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18735 }
18736
18737 prop = Fbuffer_local_value (prop, it->w->buffer);
18738 }
18739
18740 if (INTEGERP (prop) || FLOATP (prop))
18741 {
18742 int base_unit = (width_p
18743 ? FRAME_COLUMN_WIDTH (it->f)
18744 : FRAME_LINE_HEIGHT (it->f));
18745 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18746 }
18747
18748 if (CONSP (prop))
18749 {
18750 Lisp_Object car = XCAR (prop);
18751 Lisp_Object cdr = XCDR (prop);
18752
18753 if (SYMBOLP (car))
18754 {
18755 #ifdef HAVE_WINDOW_SYSTEM
18756 if (FRAME_WINDOW_P (it->f)
18757 && valid_image_p (prop))
18758 {
18759 int id = lookup_image (it->f, prop);
18760 struct image *img = IMAGE_FROM_ID (it->f, id);
18761
18762 return OK_PIXELS (width_p ? img->width : img->height);
18763 }
18764 #endif
18765 if (EQ (car, Qplus) || EQ (car, Qminus))
18766 {
18767 int first = 1;
18768 double px;
18769
18770 pixels = 0;
18771 while (CONSP (cdr))
18772 {
18773 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18774 font, width_p, align_to))
18775 return 0;
18776 if (first)
18777 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18778 else
18779 pixels += px;
18780 cdr = XCDR (cdr);
18781 }
18782 if (EQ (car, Qminus))
18783 pixels = -pixels;
18784 return OK_PIXELS (pixels);
18785 }
18786
18787 car = Fbuffer_local_value (car, it->w->buffer);
18788 }
18789
18790 if (INTEGERP (car) || FLOATP (car))
18791 {
18792 double fact;
18793 pixels = XFLOATINT (car);
18794 if (NILP (cdr))
18795 return OK_PIXELS (pixels);
18796 if (calc_pixel_width_or_height (&fact, it, cdr,
18797 font, width_p, align_to))
18798 return OK_PIXELS (pixels * fact);
18799 return 0;
18800 }
18801
18802 return 0;
18803 }
18804
18805 return 0;
18806 }
18807
18808 \f
18809 /***********************************************************************
18810 Glyph Display
18811 ***********************************************************************/
18812
18813 #ifdef HAVE_WINDOW_SYSTEM
18814
18815 #if GLYPH_DEBUG
18816
18817 void
18818 dump_glyph_string (s)
18819 struct glyph_string *s;
18820 {
18821 fprintf (stderr, "glyph string\n");
18822 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18823 s->x, s->y, s->width, s->height);
18824 fprintf (stderr, " ybase = %d\n", s->ybase);
18825 fprintf (stderr, " hl = %d\n", s->hl);
18826 fprintf (stderr, " left overhang = %d, right = %d\n",
18827 s->left_overhang, s->right_overhang);
18828 fprintf (stderr, " nchars = %d\n", s->nchars);
18829 fprintf (stderr, " extends to end of line = %d\n",
18830 s->extends_to_end_of_line_p);
18831 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18832 fprintf (stderr, " bg width = %d\n", s->background_width);
18833 }
18834
18835 #endif /* GLYPH_DEBUG */
18836
18837 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18838 of XChar2b structures for S; it can't be allocated in
18839 init_glyph_string because it must be allocated via `alloca'. W
18840 is the window on which S is drawn. ROW and AREA are the glyph row
18841 and area within the row from which S is constructed. START is the
18842 index of the first glyph structure covered by S. HL is a
18843 face-override for drawing S. */
18844
18845 #ifdef HAVE_NTGUI
18846 #define OPTIONAL_HDC(hdc) hdc,
18847 #define DECLARE_HDC(hdc) HDC hdc;
18848 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18849 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18850 #endif
18851
18852 #ifndef OPTIONAL_HDC
18853 #define OPTIONAL_HDC(hdc)
18854 #define DECLARE_HDC(hdc)
18855 #define ALLOCATE_HDC(hdc, f)
18856 #define RELEASE_HDC(hdc, f)
18857 #endif
18858
18859 static void
18860 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18861 struct glyph_string *s;
18862 DECLARE_HDC (hdc)
18863 XChar2b *char2b;
18864 struct window *w;
18865 struct glyph_row *row;
18866 enum glyph_row_area area;
18867 int start;
18868 enum draw_glyphs_face hl;
18869 {
18870 bzero (s, sizeof *s);
18871 s->w = w;
18872 s->f = XFRAME (w->frame);
18873 #ifdef HAVE_NTGUI
18874 s->hdc = hdc;
18875 #endif
18876 s->display = FRAME_X_DISPLAY (s->f);
18877 s->window = FRAME_X_WINDOW (s->f);
18878 s->char2b = char2b;
18879 s->hl = hl;
18880 s->row = row;
18881 s->area = area;
18882 s->first_glyph = row->glyphs[area] + start;
18883 s->height = row->height;
18884 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18885
18886 /* Display the internal border below the tool-bar window. */
18887 if (WINDOWP (s->f->tool_bar_window)
18888 && s->w == XWINDOW (s->f->tool_bar_window))
18889 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18890
18891 s->ybase = s->y + row->ascent;
18892 }
18893
18894
18895 /* Append the list of glyph strings with head H and tail T to the list
18896 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18897
18898 static INLINE void
18899 append_glyph_string_lists (head, tail, h, t)
18900 struct glyph_string **head, **tail;
18901 struct glyph_string *h, *t;
18902 {
18903 if (h)
18904 {
18905 if (*head)
18906 (*tail)->next = h;
18907 else
18908 *head = h;
18909 h->prev = *tail;
18910 *tail = t;
18911 }
18912 }
18913
18914
18915 /* Prepend the list of glyph strings with head H and tail T to the
18916 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18917 result. */
18918
18919 static INLINE void
18920 prepend_glyph_string_lists (head, tail, h, t)
18921 struct glyph_string **head, **tail;
18922 struct glyph_string *h, *t;
18923 {
18924 if (h)
18925 {
18926 if (*head)
18927 (*head)->prev = t;
18928 else
18929 *tail = t;
18930 t->next = *head;
18931 *head = h;
18932 }
18933 }
18934
18935
18936 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18937 Set *HEAD and *TAIL to the resulting list. */
18938
18939 static INLINE void
18940 append_glyph_string (head, tail, s)
18941 struct glyph_string **head, **tail;
18942 struct glyph_string *s;
18943 {
18944 s->next = s->prev = NULL;
18945 append_glyph_string_lists (head, tail, s, s);
18946 }
18947
18948
18949 /* Get face and two-byte form of character glyph GLYPH on frame F.
18950 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18951 a pointer to a realized face that is ready for display. */
18952
18953 static INLINE struct face *
18954 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18955 struct frame *f;
18956 struct glyph *glyph;
18957 XChar2b *char2b;
18958 int *two_byte_p;
18959 {
18960 struct face *face;
18961
18962 xassert (glyph->type == CHAR_GLYPH);
18963 face = FACE_FROM_ID (f, glyph->face_id);
18964
18965 if (two_byte_p)
18966 *two_byte_p = 0;
18967
18968 if (!glyph->multibyte_p)
18969 {
18970 /* Unibyte case. We don't have to encode, but we have to make
18971 sure to use a face suitable for unibyte. */
18972 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18973 }
18974 else if (glyph->u.ch < 128)
18975 {
18976 /* Case of ASCII in a face known to fit ASCII. */
18977 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18978 }
18979 else
18980 {
18981 int c1, c2, charset;
18982
18983 /* Split characters into bytes. If c2 is -1 afterwards, C is
18984 really a one-byte character so that byte1 is zero. */
18985 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18986 if (c2 > 0)
18987 STORE_XCHAR2B (char2b, c1, c2);
18988 else
18989 STORE_XCHAR2B (char2b, 0, c1);
18990
18991 /* Maybe encode the character in *CHAR2B. */
18992 if (charset != CHARSET_ASCII)
18993 {
18994 struct font_info *font_info
18995 = FONT_INFO_FROM_ID (f, face->font_info_id);
18996 if (font_info)
18997 glyph->font_type
18998 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18999 }
19000 }
19001
19002 /* Make sure X resources of the face are allocated. */
19003 xassert (face != NULL);
19004 PREPARE_FACE_FOR_DISPLAY (f, face);
19005 return face;
19006 }
19007
19008
19009 /* Fill glyph string S with composition components specified by S->cmp.
19010
19011 FACES is an array of faces for all components of this composition.
19012 S->gidx is the index of the first component for S.
19013
19014 OVERLAPS non-zero means S should draw the foreground only, and use
19015 its physical height for clipping. See also draw_glyphs.
19016
19017 Value is the index of a component not in S. */
19018
19019 static int
19020 fill_composite_glyph_string (s, faces, overlaps)
19021 struct glyph_string *s;
19022 struct face **faces;
19023 int overlaps;
19024 {
19025 int i;
19026
19027 xassert (s);
19028
19029 s->for_overlaps = overlaps;
19030
19031 s->face = faces[s->gidx];
19032 s->font = s->face->font;
19033 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19034
19035 /* For all glyphs of this composition, starting at the offset
19036 S->gidx, until we reach the end of the definition or encounter a
19037 glyph that requires the different face, add it to S. */
19038 ++s->nchars;
19039 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
19040 ++s->nchars;
19041
19042 /* All glyph strings for the same composition has the same width,
19043 i.e. the width set for the first component of the composition. */
19044
19045 s->width = s->first_glyph->pixel_width;
19046
19047 /* If the specified font could not be loaded, use the frame's
19048 default font, but record the fact that we couldn't load it in
19049 the glyph string so that we can draw rectangles for the
19050 characters of the glyph string. */
19051 if (s->font == NULL)
19052 {
19053 s->font_not_found_p = 1;
19054 s->font = FRAME_FONT (s->f);
19055 }
19056
19057 /* Adjust base line for subscript/superscript text. */
19058 s->ybase += s->first_glyph->voffset;
19059
19060 xassert (s->face && s->face->gc);
19061
19062 /* This glyph string must always be drawn with 16-bit functions. */
19063 s->two_byte_p = 1;
19064
19065 return s->gidx + s->nchars;
19066 }
19067
19068
19069 /* Fill glyph string S from a sequence of character glyphs.
19070
19071 FACE_ID is the face id of the string. START is the index of the
19072 first glyph to consider, END is the index of the last + 1.
19073 OVERLAPS non-zero means S should draw the foreground only, and use
19074 its physical height for clipping. See also draw_glyphs.
19075
19076 Value is the index of the first glyph not in S. */
19077
19078 static int
19079 fill_glyph_string (s, face_id, start, end, overlaps)
19080 struct glyph_string *s;
19081 int face_id;
19082 int start, end, overlaps;
19083 {
19084 struct glyph *glyph, *last;
19085 int voffset;
19086 int glyph_not_available_p;
19087
19088 xassert (s->f == XFRAME (s->w->frame));
19089 xassert (s->nchars == 0);
19090 xassert (start >= 0 && end > start);
19091
19092 s->for_overlaps = overlaps,
19093 glyph = s->row->glyphs[s->area] + start;
19094 last = s->row->glyphs[s->area] + end;
19095 voffset = glyph->voffset;
19096
19097 glyph_not_available_p = glyph->glyph_not_available_p;
19098
19099 while (glyph < last
19100 && glyph->type == CHAR_GLYPH
19101 && glyph->voffset == voffset
19102 /* Same face id implies same font, nowadays. */
19103 && glyph->face_id == face_id
19104 && glyph->glyph_not_available_p == glyph_not_available_p)
19105 {
19106 int two_byte_p;
19107
19108 s->face = get_glyph_face_and_encoding (s->f, glyph,
19109 s->char2b + s->nchars,
19110 &two_byte_p);
19111 s->two_byte_p = two_byte_p;
19112 ++s->nchars;
19113 xassert (s->nchars <= end - start);
19114 s->width += glyph->pixel_width;
19115 ++glyph;
19116 }
19117
19118 s->font = s->face->font;
19119 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19120
19121 /* If the specified font could not be loaded, use the frame's font,
19122 but record the fact that we couldn't load it in
19123 S->font_not_found_p so that we can draw rectangles for the
19124 characters of the glyph string. */
19125 if (s->font == NULL || glyph_not_available_p)
19126 {
19127 s->font_not_found_p = 1;
19128 s->font = FRAME_FONT (s->f);
19129 }
19130
19131 /* Adjust base line for subscript/superscript text. */
19132 s->ybase += voffset;
19133
19134 xassert (s->face && s->face->gc);
19135 return glyph - s->row->glyphs[s->area];
19136 }
19137
19138
19139 /* Fill glyph string S from image glyph S->first_glyph. */
19140
19141 static void
19142 fill_image_glyph_string (s)
19143 struct glyph_string *s;
19144 {
19145 xassert (s->first_glyph->type == IMAGE_GLYPH);
19146 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19147 xassert (s->img);
19148 s->slice = s->first_glyph->slice;
19149 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19150 s->font = s->face->font;
19151 s->width = s->first_glyph->pixel_width;
19152
19153 /* Adjust base line for subscript/superscript text. */
19154 s->ybase += s->first_glyph->voffset;
19155 }
19156
19157
19158 /* Fill glyph string S from a sequence of stretch glyphs.
19159
19160 ROW is the glyph row in which the glyphs are found, AREA is the
19161 area within the row. START is the index of the first glyph to
19162 consider, END is the index of the last + 1.
19163
19164 Value is the index of the first glyph not in S. */
19165
19166 static int
19167 fill_stretch_glyph_string (s, row, area, start, end)
19168 struct glyph_string *s;
19169 struct glyph_row *row;
19170 enum glyph_row_area area;
19171 int start, end;
19172 {
19173 struct glyph *glyph, *last;
19174 int voffset, face_id;
19175
19176 xassert (s->first_glyph->type == STRETCH_GLYPH);
19177
19178 glyph = s->row->glyphs[s->area] + start;
19179 last = s->row->glyphs[s->area] + end;
19180 face_id = glyph->face_id;
19181 s->face = FACE_FROM_ID (s->f, face_id);
19182 s->font = s->face->font;
19183 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19184 s->width = glyph->pixel_width;
19185 s->nchars = 1;
19186 voffset = glyph->voffset;
19187
19188 for (++glyph;
19189 (glyph < last
19190 && glyph->type == STRETCH_GLYPH
19191 && glyph->voffset == voffset
19192 && glyph->face_id == face_id);
19193 ++glyph)
19194 s->width += glyph->pixel_width;
19195
19196 /* Adjust base line for subscript/superscript text. */
19197 s->ybase += voffset;
19198
19199 /* The case that face->gc == 0 is handled when drawing the glyph
19200 string by calling PREPARE_FACE_FOR_DISPLAY. */
19201 xassert (s->face);
19202 return glyph - s->row->glyphs[s->area];
19203 }
19204
19205
19206 /* EXPORT for RIF:
19207 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19208 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19209 assumed to be zero. */
19210
19211 void
19212 x_get_glyph_overhangs (glyph, f, left, right)
19213 struct glyph *glyph;
19214 struct frame *f;
19215 int *left, *right;
19216 {
19217 *left = *right = 0;
19218
19219 if (glyph->type == CHAR_GLYPH)
19220 {
19221 XFontStruct *font;
19222 struct face *face;
19223 struct font_info *font_info;
19224 XChar2b char2b;
19225 XCharStruct *pcm;
19226
19227 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19228 font = face->font;
19229 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19230 if (font /* ++KFS: Should this be font_info ? */
19231 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
19232 {
19233 if (pcm->rbearing > pcm->width)
19234 *right = pcm->rbearing - pcm->width;
19235 if (pcm->lbearing < 0)
19236 *left = -pcm->lbearing;
19237 }
19238 }
19239 }
19240
19241
19242 /* Return the index of the first glyph preceding glyph string S that
19243 is overwritten by S because of S's left overhang. Value is -1
19244 if no glyphs are overwritten. */
19245
19246 static int
19247 left_overwritten (s)
19248 struct glyph_string *s;
19249 {
19250 int k;
19251
19252 if (s->left_overhang)
19253 {
19254 int x = 0, i;
19255 struct glyph *glyphs = s->row->glyphs[s->area];
19256 int first = s->first_glyph - glyphs;
19257
19258 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19259 x -= glyphs[i].pixel_width;
19260
19261 k = i + 1;
19262 }
19263 else
19264 k = -1;
19265
19266 return k;
19267 }
19268
19269
19270 /* Return the index of the first glyph preceding glyph string S that
19271 is overwriting S because of its right overhang. Value is -1 if no
19272 glyph in front of S overwrites S. */
19273
19274 static int
19275 left_overwriting (s)
19276 struct glyph_string *s;
19277 {
19278 int i, k, x;
19279 struct glyph *glyphs = s->row->glyphs[s->area];
19280 int first = s->first_glyph - glyphs;
19281
19282 k = -1;
19283 x = 0;
19284 for (i = first - 1; i >= 0; --i)
19285 {
19286 int left, right;
19287 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19288 if (x + right > 0)
19289 k = i;
19290 x -= glyphs[i].pixel_width;
19291 }
19292
19293 return k;
19294 }
19295
19296
19297 /* Return the index of the last glyph following glyph string S that is
19298 not overwritten by S because of S's right overhang. Value is -1 if
19299 no such glyph is found. */
19300
19301 static int
19302 right_overwritten (s)
19303 struct glyph_string *s;
19304 {
19305 int k = -1;
19306
19307 if (s->right_overhang)
19308 {
19309 int x = 0, i;
19310 struct glyph *glyphs = s->row->glyphs[s->area];
19311 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19312 int end = s->row->used[s->area];
19313
19314 for (i = first; i < end && s->right_overhang > x; ++i)
19315 x += glyphs[i].pixel_width;
19316
19317 k = i;
19318 }
19319
19320 return k;
19321 }
19322
19323
19324 /* Return the index of the last glyph following glyph string S that
19325 overwrites S because of its left overhang. Value is negative
19326 if no such glyph is found. */
19327
19328 static int
19329 right_overwriting (s)
19330 struct glyph_string *s;
19331 {
19332 int i, k, x;
19333 int end = s->row->used[s->area];
19334 struct glyph *glyphs = s->row->glyphs[s->area];
19335 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19336
19337 k = -1;
19338 x = 0;
19339 for (i = first; i < end; ++i)
19340 {
19341 int left, right;
19342 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19343 if (x - left < 0)
19344 k = i;
19345 x += glyphs[i].pixel_width;
19346 }
19347
19348 return k;
19349 }
19350
19351
19352 /* Get face and two-byte form of character C in face FACE_ID on frame
19353 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19354 means we want to display multibyte text. DISPLAY_P non-zero means
19355 make sure that X resources for the face returned are allocated.
19356 Value is a pointer to a realized face that is ready for display if
19357 DISPLAY_P is non-zero. */
19358
19359 static INLINE struct face *
19360 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19361 struct frame *f;
19362 int c, face_id;
19363 XChar2b *char2b;
19364 int multibyte_p, display_p;
19365 {
19366 struct face *face = FACE_FROM_ID (f, face_id);
19367
19368 if (!multibyte_p)
19369 {
19370 /* Unibyte case. We don't have to encode, but we have to make
19371 sure to use a face suitable for unibyte. */
19372 STORE_XCHAR2B (char2b, 0, c);
19373 face_id = FACE_FOR_CHAR (f, face, c);
19374 face = FACE_FROM_ID (f, face_id);
19375 }
19376 else if (c < 128)
19377 {
19378 /* Case of ASCII in a face known to fit ASCII. */
19379 STORE_XCHAR2B (char2b, 0, c);
19380 }
19381 else
19382 {
19383 int c1, c2, charset;
19384
19385 /* Split characters into bytes. If c2 is -1 afterwards, C is
19386 really a one-byte character so that byte1 is zero. */
19387 SPLIT_CHAR (c, charset, c1, c2);
19388 if (c2 > 0)
19389 STORE_XCHAR2B (char2b, c1, c2);
19390 else
19391 STORE_XCHAR2B (char2b, 0, c1);
19392
19393 /* Maybe encode the character in *CHAR2B. */
19394 if (face->font != NULL)
19395 {
19396 struct font_info *font_info
19397 = FONT_INFO_FROM_ID (f, face->font_info_id);
19398 if (font_info)
19399 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
19400 }
19401 }
19402
19403 /* Make sure X resources of the face are allocated. */
19404 #ifdef HAVE_X_WINDOWS
19405 if (display_p)
19406 #endif
19407 {
19408 xassert (face != NULL);
19409 PREPARE_FACE_FOR_DISPLAY (f, face);
19410 }
19411
19412 return face;
19413 }
19414
19415
19416 /* Set background width of glyph string S. START is the index of the
19417 first glyph following S. LAST_X is the right-most x-position + 1
19418 in the drawing area. */
19419
19420 static INLINE void
19421 set_glyph_string_background_width (s, start, last_x)
19422 struct glyph_string *s;
19423 int start;
19424 int last_x;
19425 {
19426 /* If the face of this glyph string has to be drawn to the end of
19427 the drawing area, set S->extends_to_end_of_line_p. */
19428
19429 if (start == s->row->used[s->area]
19430 && s->area == TEXT_AREA
19431 && ((s->row->fill_line_p
19432 && (s->hl == DRAW_NORMAL_TEXT
19433 || s->hl == DRAW_IMAGE_RAISED
19434 || s->hl == DRAW_IMAGE_SUNKEN))
19435 || s->hl == DRAW_MOUSE_FACE))
19436 s->extends_to_end_of_line_p = 1;
19437
19438 /* If S extends its face to the end of the line, set its
19439 background_width to the distance to the right edge of the drawing
19440 area. */
19441 if (s->extends_to_end_of_line_p)
19442 s->background_width = last_x - s->x + 1;
19443 else
19444 s->background_width = s->width;
19445 }
19446
19447
19448 /* Compute overhangs and x-positions for glyph string S and its
19449 predecessors, or successors. X is the starting x-position for S.
19450 BACKWARD_P non-zero means process predecessors. */
19451
19452 static void
19453 compute_overhangs_and_x (s, x, backward_p)
19454 struct glyph_string *s;
19455 int x;
19456 int backward_p;
19457 {
19458 if (backward_p)
19459 {
19460 while (s)
19461 {
19462 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19463 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19464 x -= s->width;
19465 s->x = x;
19466 s = s->prev;
19467 }
19468 }
19469 else
19470 {
19471 while (s)
19472 {
19473 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19474 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19475 s->x = x;
19476 x += s->width;
19477 s = s->next;
19478 }
19479 }
19480 }
19481
19482
19483
19484 /* The following macros are only called from draw_glyphs below.
19485 They reference the following parameters of that function directly:
19486 `w', `row', `area', and `overlap_p'
19487 as well as the following local variables:
19488 `s', `f', and `hdc' (in W32) */
19489
19490 #ifdef HAVE_NTGUI
19491 /* On W32, silently add local `hdc' variable to argument list of
19492 init_glyph_string. */
19493 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19494 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19495 #else
19496 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19497 init_glyph_string (s, char2b, w, row, area, start, hl)
19498 #endif
19499
19500 /* Add a glyph string for a stretch glyph to the list of strings
19501 between HEAD and TAIL. START is the index of the stretch glyph in
19502 row area AREA of glyph row ROW. END is the index of the last glyph
19503 in that glyph row area. X is the current output position assigned
19504 to the new glyph string constructed. HL overrides that face of the
19505 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19506 is the right-most x-position of the drawing area. */
19507
19508 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19509 and below -- keep them on one line. */
19510 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19511 do \
19512 { \
19513 s = (struct glyph_string *) alloca (sizeof *s); \
19514 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19515 START = fill_stretch_glyph_string (s, row, area, START, END); \
19516 append_glyph_string (&HEAD, &TAIL, s); \
19517 s->x = (X); \
19518 } \
19519 while (0)
19520
19521
19522 /* Add a glyph string for an image glyph to the list of strings
19523 between HEAD and TAIL. START is the index of the image glyph in
19524 row area AREA of glyph row ROW. END is the index of the last glyph
19525 in that glyph row area. X is the current output position assigned
19526 to the new glyph string constructed. HL overrides that face of the
19527 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19528 is the right-most x-position of the drawing area. */
19529
19530 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19531 do \
19532 { \
19533 s = (struct glyph_string *) alloca (sizeof *s); \
19534 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19535 fill_image_glyph_string (s); \
19536 append_glyph_string (&HEAD, &TAIL, s); \
19537 ++START; \
19538 s->x = (X); \
19539 } \
19540 while (0)
19541
19542
19543 /* Add a glyph string for a sequence of character glyphs to the list
19544 of strings between HEAD and TAIL. START is the index of the first
19545 glyph in row area AREA of glyph row ROW that is part of the new
19546 glyph string. END is the index of the last glyph in that glyph row
19547 area. X is the current output position assigned to the new glyph
19548 string constructed. HL overrides that face of the glyph; e.g. it
19549 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19550 right-most x-position of the drawing area. */
19551
19552 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19553 do \
19554 { \
19555 int c, face_id; \
19556 XChar2b *char2b; \
19557 \
19558 c = (row)->glyphs[area][START].u.ch; \
19559 face_id = (row)->glyphs[area][START].face_id; \
19560 \
19561 s = (struct glyph_string *) alloca (sizeof *s); \
19562 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19563 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19564 append_glyph_string (&HEAD, &TAIL, s); \
19565 s->x = (X); \
19566 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19567 } \
19568 while (0)
19569
19570
19571 /* Add a glyph string for a composite sequence to the list of strings
19572 between HEAD and TAIL. START is the index of the first glyph in
19573 row area AREA of glyph row ROW that is part of the new glyph
19574 string. END is the index of the last glyph in that glyph row area.
19575 X is the current output position assigned to the new glyph string
19576 constructed. HL overrides that face of the glyph; e.g. it is
19577 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19578 x-position of the drawing area. */
19579
19580 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19581 do { \
19582 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19583 int face_id = (row)->glyphs[area][START].face_id; \
19584 struct face *base_face = FACE_FROM_ID (f, face_id); \
19585 struct composition *cmp = composition_table[cmp_id]; \
19586 int glyph_len = cmp->glyph_len; \
19587 XChar2b *char2b; \
19588 struct face **faces; \
19589 struct glyph_string *first_s = NULL; \
19590 int n; \
19591 \
19592 base_face = base_face->ascii_face; \
19593 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19594 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19595 /* At first, fill in `char2b' and `faces'. */ \
19596 for (n = 0; n < glyph_len; n++) \
19597 { \
19598 int c = COMPOSITION_GLYPH (cmp, n); \
19599 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19600 faces[n] = FACE_FROM_ID (f, this_face_id); \
19601 get_char_face_and_encoding (f, c, this_face_id, \
19602 char2b + n, 1, 1); \
19603 } \
19604 \
19605 /* Make glyph_strings for each glyph sequence that is drawable by \
19606 the same face, and append them to HEAD/TAIL. */ \
19607 for (n = 0; n < cmp->glyph_len;) \
19608 { \
19609 s = (struct glyph_string *) alloca (sizeof *s); \
19610 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19611 append_glyph_string (&(HEAD), &(TAIL), s); \
19612 s->cmp = cmp; \
19613 s->gidx = n; \
19614 s->x = (X); \
19615 \
19616 if (n == 0) \
19617 first_s = s; \
19618 \
19619 n = fill_composite_glyph_string (s, faces, overlaps); \
19620 } \
19621 \
19622 ++START; \
19623 s = first_s; \
19624 } while (0)
19625
19626
19627 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19628 of AREA of glyph row ROW on window W between indices START and END.
19629 HL overrides the face for drawing glyph strings, e.g. it is
19630 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19631 x-positions of the drawing area.
19632
19633 This is an ugly monster macro construct because we must use alloca
19634 to allocate glyph strings (because draw_glyphs can be called
19635 asynchronously). */
19636
19637 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19638 do \
19639 { \
19640 HEAD = TAIL = NULL; \
19641 while (START < END) \
19642 { \
19643 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19644 switch (first_glyph->type) \
19645 { \
19646 case CHAR_GLYPH: \
19647 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19648 HL, X, LAST_X); \
19649 break; \
19650 \
19651 case COMPOSITE_GLYPH: \
19652 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19653 HL, X, LAST_X); \
19654 break; \
19655 \
19656 case STRETCH_GLYPH: \
19657 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19658 HL, X, LAST_X); \
19659 break; \
19660 \
19661 case IMAGE_GLYPH: \
19662 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19663 HL, X, LAST_X); \
19664 break; \
19665 \
19666 default: \
19667 abort (); \
19668 } \
19669 \
19670 set_glyph_string_background_width (s, START, LAST_X); \
19671 (X) += s->width; \
19672 } \
19673 } \
19674 while (0)
19675
19676
19677 /* Draw glyphs between START and END in AREA of ROW on window W,
19678 starting at x-position X. X is relative to AREA in W. HL is a
19679 face-override with the following meaning:
19680
19681 DRAW_NORMAL_TEXT draw normally
19682 DRAW_CURSOR draw in cursor face
19683 DRAW_MOUSE_FACE draw in mouse face.
19684 DRAW_INVERSE_VIDEO draw in mode line face
19685 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19686 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19687
19688 If OVERLAPS is non-zero, draw only the foreground of characters and
19689 clip to the physical height of ROW. Non-zero value also defines
19690 the overlapping part to be drawn:
19691
19692 OVERLAPS_PRED overlap with preceding rows
19693 OVERLAPS_SUCC overlap with succeeding rows
19694 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19695 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19696
19697 Value is the x-position reached, relative to AREA of W. */
19698
19699 static int
19700 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19701 struct window *w;
19702 int x;
19703 struct glyph_row *row;
19704 enum glyph_row_area area;
19705 int start, end;
19706 enum draw_glyphs_face hl;
19707 int overlaps;
19708 {
19709 struct glyph_string *head, *tail;
19710 struct glyph_string *s;
19711 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19712 int last_x, area_width;
19713 int x_reached;
19714 int i, j;
19715 struct frame *f = XFRAME (WINDOW_FRAME (w));
19716 DECLARE_HDC (hdc);
19717
19718 ALLOCATE_HDC (hdc, f);
19719
19720 /* Let's rather be paranoid than getting a SEGV. */
19721 end = min (end, row->used[area]);
19722 start = max (0, start);
19723 start = min (end, start);
19724
19725 /* Translate X to frame coordinates. Set last_x to the right
19726 end of the drawing area. */
19727 if (row->full_width_p)
19728 {
19729 /* X is relative to the left edge of W, without scroll bars
19730 or fringes. */
19731 x += WINDOW_LEFT_EDGE_X (w);
19732 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19733 }
19734 else
19735 {
19736 int area_left = window_box_left (w, area);
19737 x += area_left;
19738 area_width = window_box_width (w, area);
19739 last_x = area_left + area_width;
19740 }
19741
19742 /* Build a doubly-linked list of glyph_string structures between
19743 head and tail from what we have to draw. Note that the macro
19744 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19745 the reason we use a separate variable `i'. */
19746 i = start;
19747 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19748 if (tail)
19749 x_reached = tail->x + tail->background_width;
19750 else
19751 x_reached = x;
19752
19753 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19754 the row, redraw some glyphs in front or following the glyph
19755 strings built above. */
19756 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19757 {
19758 int dummy_x = 0;
19759 struct glyph_string *h, *t;
19760
19761 /* Compute overhangs for all glyph strings. */
19762 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19763 for (s = head; s; s = s->next)
19764 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19765
19766 /* Prepend glyph strings for glyphs in front of the first glyph
19767 string that are overwritten because of the first glyph
19768 string's left overhang. The background of all strings
19769 prepended must be drawn because the first glyph string
19770 draws over it. */
19771 i = left_overwritten (head);
19772 if (i >= 0)
19773 {
19774 j = i;
19775 BUILD_GLYPH_STRINGS (j, start, h, t,
19776 DRAW_NORMAL_TEXT, dummy_x, last_x);
19777 start = i;
19778 compute_overhangs_and_x (t, head->x, 1);
19779 prepend_glyph_string_lists (&head, &tail, h, t);
19780 clip_head = head;
19781 }
19782
19783 /* Prepend glyph strings for glyphs in front of the first glyph
19784 string that overwrite that glyph string because of their
19785 right overhang. For these strings, only the foreground must
19786 be drawn, because it draws over the glyph string at `head'.
19787 The background must not be drawn because this would overwrite
19788 right overhangs of preceding glyphs for which no glyph
19789 strings exist. */
19790 i = left_overwriting (head);
19791 if (i >= 0)
19792 {
19793 clip_head = head;
19794 BUILD_GLYPH_STRINGS (i, start, h, t,
19795 DRAW_NORMAL_TEXT, dummy_x, last_x);
19796 for (s = h; s; s = s->next)
19797 s->background_filled_p = 1;
19798 compute_overhangs_and_x (t, head->x, 1);
19799 prepend_glyph_string_lists (&head, &tail, h, t);
19800 }
19801
19802 /* Append glyphs strings for glyphs following the last glyph
19803 string tail that are overwritten by tail. The background of
19804 these strings has to be drawn because tail's foreground draws
19805 over it. */
19806 i = right_overwritten (tail);
19807 if (i >= 0)
19808 {
19809 BUILD_GLYPH_STRINGS (end, i, h, t,
19810 DRAW_NORMAL_TEXT, x, last_x);
19811 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19812 append_glyph_string_lists (&head, &tail, h, t);
19813 clip_tail = tail;
19814 }
19815
19816 /* Append glyph strings for glyphs following the last glyph
19817 string tail that overwrite tail. The foreground of such
19818 glyphs has to be drawn because it writes into the background
19819 of tail. The background must not be drawn because it could
19820 paint over the foreground of following glyphs. */
19821 i = right_overwriting (tail);
19822 if (i >= 0)
19823 {
19824 clip_tail = tail;
19825 BUILD_GLYPH_STRINGS (end, i, h, t,
19826 DRAW_NORMAL_TEXT, x, last_x);
19827 for (s = h; s; s = s->next)
19828 s->background_filled_p = 1;
19829 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19830 append_glyph_string_lists (&head, &tail, h, t);
19831 }
19832 if (clip_head || clip_tail)
19833 for (s = head; s; s = s->next)
19834 {
19835 s->clip_head = clip_head;
19836 s->clip_tail = clip_tail;
19837 }
19838 }
19839
19840 /* Draw all strings. */
19841 for (s = head; s; s = s->next)
19842 FRAME_RIF (f)->draw_glyph_string (s);
19843
19844 if (area == TEXT_AREA
19845 && !row->full_width_p
19846 /* When drawing overlapping rows, only the glyph strings'
19847 foreground is drawn, which doesn't erase a cursor
19848 completely. */
19849 && !overlaps)
19850 {
19851 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19852 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19853 : (tail ? tail->x + tail->background_width : x));
19854
19855 int text_left = window_box_left (w, TEXT_AREA);
19856 x0 -= text_left;
19857 x1 -= text_left;
19858
19859 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19860 row->y, MATRIX_ROW_BOTTOM_Y (row));
19861 }
19862
19863 /* Value is the x-position up to which drawn, relative to AREA of W.
19864 This doesn't include parts drawn because of overhangs. */
19865 if (row->full_width_p)
19866 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19867 else
19868 x_reached -= window_box_left (w, area);
19869
19870 RELEASE_HDC (hdc, f);
19871
19872 return x_reached;
19873 }
19874
19875 /* Expand row matrix if too narrow. Don't expand if area
19876 is not present. */
19877
19878 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19879 { \
19880 if (!fonts_changed_p \
19881 && (it->glyph_row->glyphs[area] \
19882 < it->glyph_row->glyphs[area + 1])) \
19883 { \
19884 it->w->ncols_scale_factor++; \
19885 fonts_changed_p = 1; \
19886 } \
19887 }
19888
19889 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19890 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19891
19892 static INLINE void
19893 append_glyph (it)
19894 struct it *it;
19895 {
19896 struct glyph *glyph;
19897 enum glyph_row_area area = it->area;
19898
19899 xassert (it->glyph_row);
19900 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19901
19902 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19903 if (glyph < it->glyph_row->glyphs[area + 1])
19904 {
19905 glyph->charpos = CHARPOS (it->position);
19906 glyph->object = it->object;
19907 glyph->pixel_width = it->pixel_width;
19908 glyph->ascent = it->ascent;
19909 glyph->descent = it->descent;
19910 glyph->voffset = it->voffset;
19911 glyph->type = CHAR_GLYPH;
19912 glyph->multibyte_p = it->multibyte_p;
19913 glyph->left_box_line_p = it->start_of_box_run_p;
19914 glyph->right_box_line_p = it->end_of_box_run_p;
19915 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19916 || it->phys_descent > it->descent);
19917 glyph->padding_p = 0;
19918 glyph->glyph_not_available_p = it->glyph_not_available_p;
19919 glyph->face_id = it->face_id;
19920 glyph->u.ch = it->char_to_display;
19921 glyph->slice = null_glyph_slice;
19922 glyph->font_type = FONT_TYPE_UNKNOWN;
19923 ++it->glyph_row->used[area];
19924 }
19925 else
19926 IT_EXPAND_MATRIX_WIDTH (it, area);
19927 }
19928
19929 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19930 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19931
19932 static INLINE void
19933 append_composite_glyph (it)
19934 struct it *it;
19935 {
19936 struct glyph *glyph;
19937 enum glyph_row_area area = it->area;
19938
19939 xassert (it->glyph_row);
19940
19941 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19942 if (glyph < it->glyph_row->glyphs[area + 1])
19943 {
19944 glyph->charpos = CHARPOS (it->position);
19945 glyph->object = it->object;
19946 glyph->pixel_width = it->pixel_width;
19947 glyph->ascent = it->ascent;
19948 glyph->descent = it->descent;
19949 glyph->voffset = it->voffset;
19950 glyph->type = COMPOSITE_GLYPH;
19951 glyph->multibyte_p = it->multibyte_p;
19952 glyph->left_box_line_p = it->start_of_box_run_p;
19953 glyph->right_box_line_p = it->end_of_box_run_p;
19954 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19955 || it->phys_descent > it->descent);
19956 glyph->padding_p = 0;
19957 glyph->glyph_not_available_p = 0;
19958 glyph->face_id = it->face_id;
19959 glyph->u.cmp_id = it->cmp_id;
19960 glyph->slice = null_glyph_slice;
19961 glyph->font_type = FONT_TYPE_UNKNOWN;
19962 ++it->glyph_row->used[area];
19963 }
19964 else
19965 IT_EXPAND_MATRIX_WIDTH (it, area);
19966 }
19967
19968
19969 /* Change IT->ascent and IT->height according to the setting of
19970 IT->voffset. */
19971
19972 static INLINE void
19973 take_vertical_position_into_account (it)
19974 struct it *it;
19975 {
19976 if (it->voffset)
19977 {
19978 if (it->voffset < 0)
19979 /* Increase the ascent so that we can display the text higher
19980 in the line. */
19981 it->ascent -= it->voffset;
19982 else
19983 /* Increase the descent so that we can display the text lower
19984 in the line. */
19985 it->descent += it->voffset;
19986 }
19987 }
19988
19989
19990 /* Produce glyphs/get display metrics for the image IT is loaded with.
19991 See the description of struct display_iterator in dispextern.h for
19992 an overview of struct display_iterator. */
19993
19994 static void
19995 produce_image_glyph (it)
19996 struct it *it;
19997 {
19998 struct image *img;
19999 struct face *face;
20000 int glyph_ascent, crop;
20001 struct glyph_slice slice;
20002
20003 xassert (it->what == IT_IMAGE);
20004
20005 face = FACE_FROM_ID (it->f, it->face_id);
20006 xassert (face);
20007 /* Make sure X resources of the face is loaded. */
20008 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20009
20010 if (it->image_id < 0)
20011 {
20012 /* Fringe bitmap. */
20013 it->ascent = it->phys_ascent = 0;
20014 it->descent = it->phys_descent = 0;
20015 it->pixel_width = 0;
20016 it->nglyphs = 0;
20017 return;
20018 }
20019
20020 img = IMAGE_FROM_ID (it->f, it->image_id);
20021 xassert (img);
20022 /* Make sure X resources of the image is loaded. */
20023 prepare_image_for_display (it->f, img);
20024
20025 slice.x = slice.y = 0;
20026 slice.width = img->width;
20027 slice.height = img->height;
20028
20029 if (INTEGERP (it->slice.x))
20030 slice.x = XINT (it->slice.x);
20031 else if (FLOATP (it->slice.x))
20032 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20033
20034 if (INTEGERP (it->slice.y))
20035 slice.y = XINT (it->slice.y);
20036 else if (FLOATP (it->slice.y))
20037 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20038
20039 if (INTEGERP (it->slice.width))
20040 slice.width = XINT (it->slice.width);
20041 else if (FLOATP (it->slice.width))
20042 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20043
20044 if (INTEGERP (it->slice.height))
20045 slice.height = XINT (it->slice.height);
20046 else if (FLOATP (it->slice.height))
20047 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20048
20049 if (slice.x >= img->width)
20050 slice.x = img->width;
20051 if (slice.y >= img->height)
20052 slice.y = img->height;
20053 if (slice.x + slice.width >= img->width)
20054 slice.width = img->width - slice.x;
20055 if (slice.y + slice.height > img->height)
20056 slice.height = img->height - slice.y;
20057
20058 if (slice.width == 0 || slice.height == 0)
20059 return;
20060
20061 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20062
20063 it->descent = slice.height - glyph_ascent;
20064 if (slice.y == 0)
20065 it->descent += img->vmargin;
20066 if (slice.y + slice.height == img->height)
20067 it->descent += img->vmargin;
20068 it->phys_descent = it->descent;
20069
20070 it->pixel_width = slice.width;
20071 if (slice.x == 0)
20072 it->pixel_width += img->hmargin;
20073 if (slice.x + slice.width == img->width)
20074 it->pixel_width += img->hmargin;
20075
20076 /* It's quite possible for images to have an ascent greater than
20077 their height, so don't get confused in that case. */
20078 if (it->descent < 0)
20079 it->descent = 0;
20080
20081 #if 0 /* this breaks image tiling */
20082 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20083 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20084 if (face_ascent > it->ascent)
20085 it->ascent = it->phys_ascent = face_ascent;
20086 #endif
20087
20088 it->nglyphs = 1;
20089
20090 if (face->box != FACE_NO_BOX)
20091 {
20092 if (face->box_line_width > 0)
20093 {
20094 if (slice.y == 0)
20095 it->ascent += face->box_line_width;
20096 if (slice.y + slice.height == img->height)
20097 it->descent += face->box_line_width;
20098 }
20099
20100 if (it->start_of_box_run_p && slice.x == 0)
20101 it->pixel_width += eabs (face->box_line_width);
20102 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20103 it->pixel_width += eabs (face->box_line_width);
20104 }
20105
20106 take_vertical_position_into_account (it);
20107
20108 /* Automatically crop wide image glyphs at right edge so we can
20109 draw the cursor on same display row. */
20110 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20111 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20112 {
20113 it->pixel_width -= crop;
20114 slice.width -= crop;
20115 }
20116
20117 if (it->glyph_row)
20118 {
20119 struct glyph *glyph;
20120 enum glyph_row_area area = it->area;
20121
20122 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20123 if (glyph < it->glyph_row->glyphs[area + 1])
20124 {
20125 glyph->charpos = CHARPOS (it->position);
20126 glyph->object = it->object;
20127 glyph->pixel_width = it->pixel_width;
20128 glyph->ascent = glyph_ascent;
20129 glyph->descent = it->descent;
20130 glyph->voffset = it->voffset;
20131 glyph->type = IMAGE_GLYPH;
20132 glyph->multibyte_p = it->multibyte_p;
20133 glyph->left_box_line_p = it->start_of_box_run_p;
20134 glyph->right_box_line_p = it->end_of_box_run_p;
20135 glyph->overlaps_vertically_p = 0;
20136 glyph->padding_p = 0;
20137 glyph->glyph_not_available_p = 0;
20138 glyph->face_id = it->face_id;
20139 glyph->u.img_id = img->id;
20140 glyph->slice = slice;
20141 glyph->font_type = FONT_TYPE_UNKNOWN;
20142 ++it->glyph_row->used[area];
20143 }
20144 else
20145 IT_EXPAND_MATRIX_WIDTH (it, area);
20146 }
20147 }
20148
20149
20150 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20151 of the glyph, WIDTH and HEIGHT are the width and height of the
20152 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20153
20154 static void
20155 append_stretch_glyph (it, object, width, height, ascent)
20156 struct it *it;
20157 Lisp_Object object;
20158 int width, height;
20159 int ascent;
20160 {
20161 struct glyph *glyph;
20162 enum glyph_row_area area = it->area;
20163
20164 xassert (ascent >= 0 && ascent <= height);
20165
20166 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20167 if (glyph < it->glyph_row->glyphs[area + 1])
20168 {
20169 glyph->charpos = CHARPOS (it->position);
20170 glyph->object = object;
20171 glyph->pixel_width = width;
20172 glyph->ascent = ascent;
20173 glyph->descent = height - ascent;
20174 glyph->voffset = it->voffset;
20175 glyph->type = STRETCH_GLYPH;
20176 glyph->multibyte_p = it->multibyte_p;
20177 glyph->left_box_line_p = it->start_of_box_run_p;
20178 glyph->right_box_line_p = it->end_of_box_run_p;
20179 glyph->overlaps_vertically_p = 0;
20180 glyph->padding_p = 0;
20181 glyph->glyph_not_available_p = 0;
20182 glyph->face_id = it->face_id;
20183 glyph->u.stretch.ascent = ascent;
20184 glyph->u.stretch.height = height;
20185 glyph->slice = null_glyph_slice;
20186 glyph->font_type = FONT_TYPE_UNKNOWN;
20187 ++it->glyph_row->used[area];
20188 }
20189 else
20190 IT_EXPAND_MATRIX_WIDTH (it, area);
20191 }
20192
20193
20194 /* Produce a stretch glyph for iterator IT. IT->object is the value
20195 of the glyph property displayed. The value must be a list
20196 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20197 being recognized:
20198
20199 1. `:width WIDTH' specifies that the space should be WIDTH *
20200 canonical char width wide. WIDTH may be an integer or floating
20201 point number.
20202
20203 2. `:relative-width FACTOR' specifies that the width of the stretch
20204 should be computed from the width of the first character having the
20205 `glyph' property, and should be FACTOR times that width.
20206
20207 3. `:align-to HPOS' specifies that the space should be wide enough
20208 to reach HPOS, a value in canonical character units.
20209
20210 Exactly one of the above pairs must be present.
20211
20212 4. `:height HEIGHT' specifies that the height of the stretch produced
20213 should be HEIGHT, measured in canonical character units.
20214
20215 5. `:relative-height FACTOR' specifies that the height of the
20216 stretch should be FACTOR times the height of the characters having
20217 the glyph property.
20218
20219 Either none or exactly one of 4 or 5 must be present.
20220
20221 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20222 of the stretch should be used for the ascent of the stretch.
20223 ASCENT must be in the range 0 <= ASCENT <= 100. */
20224
20225 static void
20226 produce_stretch_glyph (it)
20227 struct it *it;
20228 {
20229 /* (space :width WIDTH :height HEIGHT ...) */
20230 Lisp_Object prop, plist;
20231 int width = 0, height = 0, align_to = -1;
20232 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20233 int ascent = 0;
20234 double tem;
20235 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20236 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20237
20238 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20239
20240 /* List should start with `space'. */
20241 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20242 plist = XCDR (it->object);
20243
20244 /* Compute the width of the stretch. */
20245 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20246 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20247 {
20248 /* Absolute width `:width WIDTH' specified and valid. */
20249 zero_width_ok_p = 1;
20250 width = (int)tem;
20251 }
20252 else if (prop = Fplist_get (plist, QCrelative_width),
20253 NUMVAL (prop) > 0)
20254 {
20255 /* Relative width `:relative-width FACTOR' specified and valid.
20256 Compute the width of the characters having the `glyph'
20257 property. */
20258 struct it it2;
20259 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20260
20261 it2 = *it;
20262 if (it->multibyte_p)
20263 {
20264 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20265 - IT_BYTEPOS (*it));
20266 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20267 }
20268 else
20269 it2.c = *p, it2.len = 1;
20270
20271 it2.glyph_row = NULL;
20272 it2.what = IT_CHARACTER;
20273 x_produce_glyphs (&it2);
20274 width = NUMVAL (prop) * it2.pixel_width;
20275 }
20276 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20277 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20278 {
20279 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20280 align_to = (align_to < 0
20281 ? 0
20282 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20283 else if (align_to < 0)
20284 align_to = window_box_left_offset (it->w, TEXT_AREA);
20285 width = max (0, (int)tem + align_to - it->current_x);
20286 zero_width_ok_p = 1;
20287 }
20288 else
20289 /* Nothing specified -> width defaults to canonical char width. */
20290 width = FRAME_COLUMN_WIDTH (it->f);
20291
20292 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20293 width = 1;
20294
20295 /* Compute height. */
20296 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20297 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20298 {
20299 height = (int)tem;
20300 zero_height_ok_p = 1;
20301 }
20302 else if (prop = Fplist_get (plist, QCrelative_height),
20303 NUMVAL (prop) > 0)
20304 height = FONT_HEIGHT (font) * NUMVAL (prop);
20305 else
20306 height = FONT_HEIGHT (font);
20307
20308 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20309 height = 1;
20310
20311 /* Compute percentage of height used for ascent. If
20312 `:ascent ASCENT' is present and valid, use that. Otherwise,
20313 derive the ascent from the font in use. */
20314 if (prop = Fplist_get (plist, QCascent),
20315 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20316 ascent = height * NUMVAL (prop) / 100.0;
20317 else if (!NILP (prop)
20318 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20319 ascent = min (max (0, (int)tem), height);
20320 else
20321 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20322
20323 if (width > 0 && !it->truncate_lines_p
20324 && it->current_x + width > it->last_visible_x)
20325 width = it->last_visible_x - it->current_x - 1;
20326
20327 if (width > 0 && height > 0 && it->glyph_row)
20328 {
20329 Lisp_Object object = it->stack[it->sp - 1].string;
20330 if (!STRINGP (object))
20331 object = it->w->buffer;
20332 append_stretch_glyph (it, object, width, height, ascent);
20333 }
20334
20335 it->pixel_width = width;
20336 it->ascent = it->phys_ascent = ascent;
20337 it->descent = it->phys_descent = height - it->ascent;
20338 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20339
20340 take_vertical_position_into_account (it);
20341 }
20342
20343 /* Get line-height and line-spacing property at point.
20344 If line-height has format (HEIGHT TOTAL), return TOTAL
20345 in TOTAL_HEIGHT. */
20346
20347 static Lisp_Object
20348 get_line_height_property (it, prop)
20349 struct it *it;
20350 Lisp_Object prop;
20351 {
20352 Lisp_Object position;
20353
20354 if (STRINGP (it->object))
20355 position = make_number (IT_STRING_CHARPOS (*it));
20356 else if (BUFFERP (it->object))
20357 position = make_number (IT_CHARPOS (*it));
20358 else
20359 return Qnil;
20360
20361 return Fget_char_property (position, prop, it->object);
20362 }
20363
20364 /* Calculate line-height and line-spacing properties.
20365 An integer value specifies explicit pixel value.
20366 A float value specifies relative value to current face height.
20367 A cons (float . face-name) specifies relative value to
20368 height of specified face font.
20369
20370 Returns height in pixels, or nil. */
20371
20372
20373 static Lisp_Object
20374 calc_line_height_property (it, val, font, boff, override)
20375 struct it *it;
20376 Lisp_Object val;
20377 XFontStruct *font;
20378 int boff, override;
20379 {
20380 Lisp_Object face_name = Qnil;
20381 int ascent, descent, height;
20382
20383 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20384 return val;
20385
20386 if (CONSP (val))
20387 {
20388 face_name = XCAR (val);
20389 val = XCDR (val);
20390 if (!NUMBERP (val))
20391 val = make_number (1);
20392 if (NILP (face_name))
20393 {
20394 height = it->ascent + it->descent;
20395 goto scale;
20396 }
20397 }
20398
20399 if (NILP (face_name))
20400 {
20401 font = FRAME_FONT (it->f);
20402 boff = FRAME_BASELINE_OFFSET (it->f);
20403 }
20404 else if (EQ (face_name, Qt))
20405 {
20406 override = 0;
20407 }
20408 else
20409 {
20410 int face_id;
20411 struct face *face;
20412 struct font_info *font_info;
20413
20414 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20415 if (face_id < 0)
20416 return make_number (-1);
20417
20418 face = FACE_FROM_ID (it->f, face_id);
20419 font = face->font;
20420 if (font == NULL)
20421 return make_number (-1);
20422
20423 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20424 boff = font_info->baseline_offset;
20425 if (font_info->vertical_centering)
20426 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20427 }
20428
20429 ascent = FONT_BASE (font) + boff;
20430 descent = FONT_DESCENT (font) - boff;
20431
20432 if (override)
20433 {
20434 it->override_ascent = ascent;
20435 it->override_descent = descent;
20436 it->override_boff = boff;
20437 }
20438
20439 height = ascent + descent;
20440
20441 scale:
20442 if (FLOATP (val))
20443 height = (int)(XFLOAT_DATA (val) * height);
20444 else if (INTEGERP (val))
20445 height *= XINT (val);
20446
20447 return make_number (height);
20448 }
20449
20450
20451 /* RIF:
20452 Produce glyphs/get display metrics for the display element IT is
20453 loaded with. See the description of struct it in dispextern.h
20454 for an overview of struct it. */
20455
20456 void
20457 x_produce_glyphs (it)
20458 struct it *it;
20459 {
20460 int extra_line_spacing = it->extra_line_spacing;
20461
20462 it->glyph_not_available_p = 0;
20463
20464 if (it->what == IT_CHARACTER)
20465 {
20466 XChar2b char2b;
20467 XFontStruct *font;
20468 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20469 XCharStruct *pcm;
20470 int font_not_found_p;
20471 struct font_info *font_info;
20472 int boff; /* baseline offset */
20473 /* We may change it->multibyte_p upon unibyte<->multibyte
20474 conversion. So, save the current value now and restore it
20475 later.
20476
20477 Note: It seems that we don't have to record multibyte_p in
20478 struct glyph because the character code itself tells if or
20479 not the character is multibyte. Thus, in the future, we must
20480 consider eliminating the field `multibyte_p' in the struct
20481 glyph. */
20482 int saved_multibyte_p = it->multibyte_p;
20483
20484 /* Maybe translate single-byte characters to multibyte, or the
20485 other way. */
20486 it->char_to_display = it->c;
20487 if (!ASCII_BYTE_P (it->c))
20488 {
20489 if (unibyte_display_via_language_environment
20490 && SINGLE_BYTE_CHAR_P (it->c)
20491 && (it->c >= 0240
20492 || !NILP (Vnonascii_translation_table)))
20493 {
20494 it->char_to_display = unibyte_char_to_multibyte (it->c);
20495 it->multibyte_p = 1;
20496 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20497 face = FACE_FROM_ID (it->f, it->face_id);
20498 }
20499 else if (!SINGLE_BYTE_CHAR_P (it->c)
20500 && !it->multibyte_p)
20501 {
20502 it->multibyte_p = 1;
20503 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20504 face = FACE_FROM_ID (it->f, it->face_id);
20505 }
20506 }
20507
20508 /* Get font to use. Encode IT->char_to_display. */
20509 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20510 &char2b, it->multibyte_p, 0);
20511 font = face->font;
20512
20513 /* When no suitable font found, use the default font. */
20514 font_not_found_p = font == NULL;
20515 if (font_not_found_p)
20516 {
20517 font = FRAME_FONT (it->f);
20518 boff = FRAME_BASELINE_OFFSET (it->f);
20519 font_info = NULL;
20520 }
20521 else
20522 {
20523 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20524 boff = font_info->baseline_offset;
20525 if (font_info->vertical_centering)
20526 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20527 }
20528
20529 if (it->char_to_display >= ' '
20530 && (!it->multibyte_p || it->char_to_display < 128))
20531 {
20532 /* Either unibyte or ASCII. */
20533 int stretched_p;
20534
20535 it->nglyphs = 1;
20536
20537 pcm = FRAME_RIF (it->f)->per_char_metric
20538 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20539
20540 if (it->override_ascent >= 0)
20541 {
20542 it->ascent = it->override_ascent;
20543 it->descent = it->override_descent;
20544 boff = it->override_boff;
20545 }
20546 else
20547 {
20548 it->ascent = FONT_BASE (font) + boff;
20549 it->descent = FONT_DESCENT (font) - boff;
20550 }
20551
20552 if (pcm)
20553 {
20554 it->phys_ascent = pcm->ascent + boff;
20555 it->phys_descent = pcm->descent - boff;
20556 it->pixel_width = pcm->width;
20557 }
20558 else
20559 {
20560 it->glyph_not_available_p = 1;
20561 it->phys_ascent = it->ascent;
20562 it->phys_descent = it->descent;
20563 it->pixel_width = FONT_WIDTH (font);
20564 }
20565
20566 if (it->constrain_row_ascent_descent_p)
20567 {
20568 if (it->descent > it->max_descent)
20569 {
20570 it->ascent += it->descent - it->max_descent;
20571 it->descent = it->max_descent;
20572 }
20573 if (it->ascent > it->max_ascent)
20574 {
20575 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20576 it->ascent = it->max_ascent;
20577 }
20578 it->phys_ascent = min (it->phys_ascent, it->ascent);
20579 it->phys_descent = min (it->phys_descent, it->descent);
20580 extra_line_spacing = 0;
20581 }
20582
20583 /* If this is a space inside a region of text with
20584 `space-width' property, change its width. */
20585 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20586 if (stretched_p)
20587 it->pixel_width *= XFLOATINT (it->space_width);
20588
20589 /* If face has a box, add the box thickness to the character
20590 height. If character has a box line to the left and/or
20591 right, add the box line width to the character's width. */
20592 if (face->box != FACE_NO_BOX)
20593 {
20594 int thick = face->box_line_width;
20595
20596 if (thick > 0)
20597 {
20598 it->ascent += thick;
20599 it->descent += thick;
20600 }
20601 else
20602 thick = -thick;
20603
20604 if (it->start_of_box_run_p)
20605 it->pixel_width += thick;
20606 if (it->end_of_box_run_p)
20607 it->pixel_width += thick;
20608 }
20609
20610 /* If face has an overline, add the height of the overline
20611 (1 pixel) and a 1 pixel margin to the character height. */
20612 if (face->overline_p)
20613 it->ascent += overline_margin;
20614
20615 if (it->constrain_row_ascent_descent_p)
20616 {
20617 if (it->ascent > it->max_ascent)
20618 it->ascent = it->max_ascent;
20619 if (it->descent > it->max_descent)
20620 it->descent = it->max_descent;
20621 }
20622
20623 take_vertical_position_into_account (it);
20624
20625 /* If we have to actually produce glyphs, do it. */
20626 if (it->glyph_row)
20627 {
20628 if (stretched_p)
20629 {
20630 /* Translate a space with a `space-width' property
20631 into a stretch glyph. */
20632 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20633 / FONT_HEIGHT (font));
20634 append_stretch_glyph (it, it->object, it->pixel_width,
20635 it->ascent + it->descent, ascent);
20636 }
20637 else
20638 append_glyph (it);
20639
20640 /* If characters with lbearing or rbearing are displayed
20641 in this line, record that fact in a flag of the
20642 glyph row. This is used to optimize X output code. */
20643 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20644 it->glyph_row->contains_overlapping_glyphs_p = 1;
20645 }
20646 }
20647 else if (it->char_to_display == '\n')
20648 {
20649 /* A newline has no width but we need the height of the line.
20650 But if previous part of the line set a height, don't
20651 increase that height */
20652
20653 Lisp_Object height;
20654 Lisp_Object total_height = Qnil;
20655
20656 it->override_ascent = -1;
20657 it->pixel_width = 0;
20658 it->nglyphs = 0;
20659
20660 height = get_line_height_property(it, Qline_height);
20661 /* Split (line-height total-height) list */
20662 if (CONSP (height)
20663 && CONSP (XCDR (height))
20664 && NILP (XCDR (XCDR (height))))
20665 {
20666 total_height = XCAR (XCDR (height));
20667 height = XCAR (height);
20668 }
20669 height = calc_line_height_property(it, height, font, boff, 1);
20670
20671 if (it->override_ascent >= 0)
20672 {
20673 it->ascent = it->override_ascent;
20674 it->descent = it->override_descent;
20675 boff = it->override_boff;
20676 }
20677 else
20678 {
20679 it->ascent = FONT_BASE (font) + boff;
20680 it->descent = FONT_DESCENT (font) - boff;
20681 }
20682
20683 if (EQ (height, Qt))
20684 {
20685 if (it->descent > it->max_descent)
20686 {
20687 it->ascent += it->descent - it->max_descent;
20688 it->descent = it->max_descent;
20689 }
20690 if (it->ascent > it->max_ascent)
20691 {
20692 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20693 it->ascent = it->max_ascent;
20694 }
20695 it->phys_ascent = min (it->phys_ascent, it->ascent);
20696 it->phys_descent = min (it->phys_descent, it->descent);
20697 it->constrain_row_ascent_descent_p = 1;
20698 extra_line_spacing = 0;
20699 }
20700 else
20701 {
20702 Lisp_Object spacing;
20703
20704 it->phys_ascent = it->ascent;
20705 it->phys_descent = it->descent;
20706
20707 if ((it->max_ascent > 0 || it->max_descent > 0)
20708 && face->box != FACE_NO_BOX
20709 && face->box_line_width > 0)
20710 {
20711 it->ascent += face->box_line_width;
20712 it->descent += face->box_line_width;
20713 }
20714 if (!NILP (height)
20715 && XINT (height) > it->ascent + it->descent)
20716 it->ascent = XINT (height) - it->descent;
20717
20718 if (!NILP (total_height))
20719 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20720 else
20721 {
20722 spacing = get_line_height_property(it, Qline_spacing);
20723 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20724 }
20725 if (INTEGERP (spacing))
20726 {
20727 extra_line_spacing = XINT (spacing);
20728 if (!NILP (total_height))
20729 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20730 }
20731 }
20732 }
20733 else if (it->char_to_display == '\t')
20734 {
20735 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20736 int x = it->current_x + it->continuation_lines_width;
20737 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20738
20739 /* If the distance from the current position to the next tab
20740 stop is less than a space character width, use the
20741 tab stop after that. */
20742 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20743 next_tab_x += tab_width;
20744
20745 it->pixel_width = next_tab_x - x;
20746 it->nglyphs = 1;
20747 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20748 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20749
20750 if (it->glyph_row)
20751 {
20752 append_stretch_glyph (it, it->object, it->pixel_width,
20753 it->ascent + it->descent, it->ascent);
20754 }
20755 }
20756 else
20757 {
20758 /* A multi-byte character. Assume that the display width of the
20759 character is the width of the character multiplied by the
20760 width of the font. */
20761
20762 /* If we found a font, this font should give us the right
20763 metrics. If we didn't find a font, use the frame's
20764 default font and calculate the width of the character
20765 from the charset width; this is what old redisplay code
20766 did. */
20767
20768 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20769 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20770
20771 if (font_not_found_p || !pcm)
20772 {
20773 int charset = CHAR_CHARSET (it->char_to_display);
20774
20775 it->glyph_not_available_p = 1;
20776 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20777 * CHARSET_WIDTH (charset));
20778 it->phys_ascent = FONT_BASE (font) + boff;
20779 it->phys_descent = FONT_DESCENT (font) - boff;
20780 }
20781 else
20782 {
20783 it->pixel_width = pcm->width;
20784 it->phys_ascent = pcm->ascent + boff;
20785 it->phys_descent = pcm->descent - boff;
20786 if (it->glyph_row
20787 && (pcm->lbearing < 0
20788 || pcm->rbearing > pcm->width))
20789 it->glyph_row->contains_overlapping_glyphs_p = 1;
20790 }
20791 it->nglyphs = 1;
20792 it->ascent = FONT_BASE (font) + boff;
20793 it->descent = FONT_DESCENT (font) - boff;
20794 if (face->box != FACE_NO_BOX)
20795 {
20796 int thick = face->box_line_width;
20797
20798 if (thick > 0)
20799 {
20800 it->ascent += thick;
20801 it->descent += thick;
20802 }
20803 else
20804 thick = - thick;
20805
20806 if (it->start_of_box_run_p)
20807 it->pixel_width += thick;
20808 if (it->end_of_box_run_p)
20809 it->pixel_width += thick;
20810 }
20811
20812 /* If face has an overline, add the height of the overline
20813 (1 pixel) and a 1 pixel margin to the character height. */
20814 if (face->overline_p)
20815 it->ascent += overline_margin;
20816
20817 take_vertical_position_into_account (it);
20818
20819 if (it->glyph_row)
20820 append_glyph (it);
20821 }
20822 it->multibyte_p = saved_multibyte_p;
20823 }
20824 else if (it->what == IT_COMPOSITION)
20825 {
20826 /* Note: A composition is represented as one glyph in the
20827 glyph matrix. There are no padding glyphs. */
20828 XChar2b char2b;
20829 XFontStruct *font;
20830 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20831 XCharStruct *pcm;
20832 int font_not_found_p;
20833 struct font_info *font_info;
20834 int boff; /* baseline offset */
20835 struct composition *cmp = composition_table[it->cmp_id];
20836
20837 /* Maybe translate single-byte characters to multibyte. */
20838 it->char_to_display = it->c;
20839 if (unibyte_display_via_language_environment
20840 && SINGLE_BYTE_CHAR_P (it->c)
20841 && (it->c >= 0240
20842 || (it->c >= 0200
20843 && !NILP (Vnonascii_translation_table))))
20844 {
20845 it->char_to_display = unibyte_char_to_multibyte (it->c);
20846 }
20847
20848 /* Get face and font to use. Encode IT->char_to_display. */
20849 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20850 face = FACE_FROM_ID (it->f, it->face_id);
20851 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20852 &char2b, it->multibyte_p, 0);
20853 font = face->font;
20854
20855 /* When no suitable font found, use the default font. */
20856 font_not_found_p = font == NULL;
20857 if (font_not_found_p)
20858 {
20859 font = FRAME_FONT (it->f);
20860 boff = FRAME_BASELINE_OFFSET (it->f);
20861 font_info = NULL;
20862 }
20863 else
20864 {
20865 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20866 boff = font_info->baseline_offset;
20867 if (font_info->vertical_centering)
20868 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20869 }
20870
20871 /* There are no padding glyphs, so there is only one glyph to
20872 produce for the composition. Important is that pixel_width,
20873 ascent and descent are the values of what is drawn by
20874 draw_glyphs (i.e. the values of the overall glyphs composed). */
20875 it->nglyphs = 1;
20876
20877 /* If we have not yet calculated pixel size data of glyphs of
20878 the composition for the current face font, calculate them
20879 now. Theoretically, we have to check all fonts for the
20880 glyphs, but that requires much time and memory space. So,
20881 here we check only the font of the first glyph. This leads
20882 to incorrect display very rarely, and C-l (recenter) can
20883 correct the display anyway. */
20884 if (cmp->font != (void *) font)
20885 {
20886 /* Ascent and descent of the font of the first character of
20887 this composition (adjusted by baseline offset). Ascent
20888 and descent of overall glyphs should not be less than
20889 them respectively. */
20890 int font_ascent = FONT_BASE (font) + boff;
20891 int font_descent = FONT_DESCENT (font) - boff;
20892 /* Bounding box of the overall glyphs. */
20893 int leftmost, rightmost, lowest, highest;
20894 int i, width, ascent, descent;
20895
20896 cmp->font = (void *) font;
20897
20898 /* Initialize the bounding box. */
20899 if (font_info
20900 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20901 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20902 {
20903 width = pcm->width;
20904 ascent = pcm->ascent;
20905 descent = pcm->descent;
20906 }
20907 else
20908 {
20909 width = FONT_WIDTH (font);
20910 ascent = FONT_BASE (font);
20911 descent = FONT_DESCENT (font);
20912 }
20913
20914 rightmost = width;
20915 lowest = - descent + boff;
20916 highest = ascent + boff;
20917 leftmost = 0;
20918
20919 if (font_info
20920 && font_info->default_ascent
20921 && CHAR_TABLE_P (Vuse_default_ascent)
20922 && !NILP (Faref (Vuse_default_ascent,
20923 make_number (it->char_to_display))))
20924 highest = font_info->default_ascent + boff;
20925
20926 /* Draw the first glyph at the normal position. It may be
20927 shifted to right later if some other glyphs are drawn at
20928 the left. */
20929 cmp->offsets[0] = 0;
20930 cmp->offsets[1] = boff;
20931
20932 /* Set cmp->offsets for the remaining glyphs. */
20933 for (i = 1; i < cmp->glyph_len; i++)
20934 {
20935 int left, right, btm, top;
20936 int ch = COMPOSITION_GLYPH (cmp, i);
20937 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20938
20939 face = FACE_FROM_ID (it->f, face_id);
20940 get_char_face_and_encoding (it->f, ch, face->id,
20941 &char2b, it->multibyte_p, 0);
20942 font = face->font;
20943 if (font == NULL)
20944 {
20945 font = FRAME_FONT (it->f);
20946 boff = FRAME_BASELINE_OFFSET (it->f);
20947 font_info = NULL;
20948 }
20949 else
20950 {
20951 font_info
20952 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20953 boff = font_info->baseline_offset;
20954 if (font_info->vertical_centering)
20955 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20956 }
20957
20958 if (font_info
20959 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20960 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20961 {
20962 width = pcm->width;
20963 ascent = pcm->ascent;
20964 descent = pcm->descent;
20965 }
20966 else
20967 {
20968 width = FONT_WIDTH (font);
20969 ascent = 1;
20970 descent = 0;
20971 }
20972
20973 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20974 {
20975 /* Relative composition with or without
20976 alternate chars. */
20977 left = (leftmost + rightmost - width) / 2;
20978 btm = - descent + boff;
20979 if (font_info && font_info->relative_compose
20980 && (! CHAR_TABLE_P (Vignore_relative_composition)
20981 || NILP (Faref (Vignore_relative_composition,
20982 make_number (ch)))))
20983 {
20984
20985 if (- descent >= font_info->relative_compose)
20986 /* One extra pixel between two glyphs. */
20987 btm = highest + 1;
20988 else if (ascent <= 0)
20989 /* One extra pixel between two glyphs. */
20990 btm = lowest - 1 - ascent - descent;
20991 }
20992 }
20993 else
20994 {
20995 /* A composition rule is specified by an integer
20996 value that encodes global and new reference
20997 points (GREF and NREF). GREF and NREF are
20998 specified by numbers as below:
20999
21000 0---1---2 -- ascent
21001 | |
21002 | |
21003 | |
21004 9--10--11 -- center
21005 | |
21006 ---3---4---5--- baseline
21007 | |
21008 6---7---8 -- descent
21009 */
21010 int rule = COMPOSITION_RULE (cmp, i);
21011 int gref, nref, grefx, grefy, nrefx, nrefy;
21012
21013 COMPOSITION_DECODE_RULE (rule, gref, nref);
21014 grefx = gref % 3, nrefx = nref % 3;
21015 grefy = gref / 3, nrefy = nref / 3;
21016
21017 left = (leftmost
21018 + grefx * (rightmost - leftmost) / 2
21019 - nrefx * width / 2);
21020 btm = ((grefy == 0 ? highest
21021 : grefy == 1 ? 0
21022 : grefy == 2 ? lowest
21023 : (highest + lowest) / 2)
21024 - (nrefy == 0 ? ascent + descent
21025 : nrefy == 1 ? descent - boff
21026 : nrefy == 2 ? 0
21027 : (ascent + descent) / 2));
21028 }
21029
21030 cmp->offsets[i * 2] = left;
21031 cmp->offsets[i * 2 + 1] = btm + descent;
21032
21033 /* Update the bounding box of the overall glyphs. */
21034 right = left + width;
21035 top = btm + descent + ascent;
21036 if (left < leftmost)
21037 leftmost = left;
21038 if (right > rightmost)
21039 rightmost = right;
21040 if (top > highest)
21041 highest = top;
21042 if (btm < lowest)
21043 lowest = btm;
21044 }
21045
21046 /* If there are glyphs whose x-offsets are negative,
21047 shift all glyphs to the right and make all x-offsets
21048 non-negative. */
21049 if (leftmost < 0)
21050 {
21051 for (i = 0; i < cmp->glyph_len; i++)
21052 cmp->offsets[i * 2] -= leftmost;
21053 rightmost -= leftmost;
21054 }
21055
21056 cmp->pixel_width = rightmost;
21057 cmp->ascent = highest;
21058 cmp->descent = - lowest;
21059 if (cmp->ascent < font_ascent)
21060 cmp->ascent = font_ascent;
21061 if (cmp->descent < font_descent)
21062 cmp->descent = font_descent;
21063 }
21064
21065 it->pixel_width = cmp->pixel_width;
21066 it->ascent = it->phys_ascent = cmp->ascent;
21067 it->descent = it->phys_descent = cmp->descent;
21068
21069 if (face->box != FACE_NO_BOX)
21070 {
21071 int thick = face->box_line_width;
21072
21073 if (thick > 0)
21074 {
21075 it->ascent += thick;
21076 it->descent += thick;
21077 }
21078 else
21079 thick = - thick;
21080
21081 if (it->start_of_box_run_p)
21082 it->pixel_width += thick;
21083 if (it->end_of_box_run_p)
21084 it->pixel_width += thick;
21085 }
21086
21087 /* If face has an overline, add the height of the overline
21088 (1 pixel) and a 1 pixel margin to the character height. */
21089 if (face->overline_p)
21090 it->ascent += overline_margin;
21091
21092 take_vertical_position_into_account (it);
21093
21094 if (it->glyph_row)
21095 append_composite_glyph (it);
21096 }
21097 else if (it->what == IT_IMAGE)
21098 produce_image_glyph (it);
21099 else if (it->what == IT_STRETCH)
21100 produce_stretch_glyph (it);
21101
21102 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21103 because this isn't true for images with `:ascent 100'. */
21104 xassert (it->ascent >= 0 && it->descent >= 0);
21105 if (it->area == TEXT_AREA)
21106 it->current_x += it->pixel_width;
21107
21108 if (extra_line_spacing > 0)
21109 {
21110 it->descent += extra_line_spacing;
21111 if (extra_line_spacing > it->max_extra_line_spacing)
21112 it->max_extra_line_spacing = extra_line_spacing;
21113 }
21114
21115 it->max_ascent = max (it->max_ascent, it->ascent);
21116 it->max_descent = max (it->max_descent, it->descent);
21117 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21118 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21119 }
21120
21121 /* EXPORT for RIF:
21122 Output LEN glyphs starting at START at the nominal cursor position.
21123 Advance the nominal cursor over the text. The global variable
21124 updated_window contains the window being updated, updated_row is
21125 the glyph row being updated, and updated_area is the area of that
21126 row being updated. */
21127
21128 void
21129 x_write_glyphs (start, len)
21130 struct glyph *start;
21131 int len;
21132 {
21133 int x, hpos;
21134
21135 xassert (updated_window && updated_row);
21136 BLOCK_INPUT;
21137
21138 /* Write glyphs. */
21139
21140 hpos = start - updated_row->glyphs[updated_area];
21141 x = draw_glyphs (updated_window, output_cursor.x,
21142 updated_row, updated_area,
21143 hpos, hpos + len,
21144 DRAW_NORMAL_TEXT, 0);
21145
21146 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21147 if (updated_area == TEXT_AREA
21148 && updated_window->phys_cursor_on_p
21149 && updated_window->phys_cursor.vpos == output_cursor.vpos
21150 && updated_window->phys_cursor.hpos >= hpos
21151 && updated_window->phys_cursor.hpos < hpos + len)
21152 updated_window->phys_cursor_on_p = 0;
21153
21154 UNBLOCK_INPUT;
21155
21156 /* Advance the output cursor. */
21157 output_cursor.hpos += len;
21158 output_cursor.x = x;
21159 }
21160
21161
21162 /* EXPORT for RIF:
21163 Insert LEN glyphs from START at the nominal cursor position. */
21164
21165 void
21166 x_insert_glyphs (start, len)
21167 struct glyph *start;
21168 int len;
21169 {
21170 struct frame *f;
21171 struct window *w;
21172 int line_height, shift_by_width, shifted_region_width;
21173 struct glyph_row *row;
21174 struct glyph *glyph;
21175 int frame_x, frame_y, hpos;
21176
21177 xassert (updated_window && updated_row);
21178 BLOCK_INPUT;
21179 w = updated_window;
21180 f = XFRAME (WINDOW_FRAME (w));
21181
21182 /* Get the height of the line we are in. */
21183 row = updated_row;
21184 line_height = row->height;
21185
21186 /* Get the width of the glyphs to insert. */
21187 shift_by_width = 0;
21188 for (glyph = start; glyph < start + len; ++glyph)
21189 shift_by_width += glyph->pixel_width;
21190
21191 /* Get the width of the region to shift right. */
21192 shifted_region_width = (window_box_width (w, updated_area)
21193 - output_cursor.x
21194 - shift_by_width);
21195
21196 /* Shift right. */
21197 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21198 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21199
21200 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21201 line_height, shift_by_width);
21202
21203 /* Write the glyphs. */
21204 hpos = start - row->glyphs[updated_area];
21205 draw_glyphs (w, output_cursor.x, row, updated_area,
21206 hpos, hpos + len,
21207 DRAW_NORMAL_TEXT, 0);
21208
21209 /* Advance the output cursor. */
21210 output_cursor.hpos += len;
21211 output_cursor.x += shift_by_width;
21212 UNBLOCK_INPUT;
21213 }
21214
21215
21216 /* EXPORT for RIF:
21217 Erase the current text line from the nominal cursor position
21218 (inclusive) to pixel column TO_X (exclusive). The idea is that
21219 everything from TO_X onward is already erased.
21220
21221 TO_X is a pixel position relative to updated_area of
21222 updated_window. TO_X == -1 means clear to the end of this area. */
21223
21224 void
21225 x_clear_end_of_line (to_x)
21226 int to_x;
21227 {
21228 struct frame *f;
21229 struct window *w = updated_window;
21230 int max_x, min_y, max_y;
21231 int from_x, from_y, to_y;
21232
21233 xassert (updated_window && updated_row);
21234 f = XFRAME (w->frame);
21235
21236 if (updated_row->full_width_p)
21237 max_x = WINDOW_TOTAL_WIDTH (w);
21238 else
21239 max_x = window_box_width (w, updated_area);
21240 max_y = window_text_bottom_y (w);
21241
21242 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21243 of window. For TO_X > 0, truncate to end of drawing area. */
21244 if (to_x == 0)
21245 return;
21246 else if (to_x < 0)
21247 to_x = max_x;
21248 else
21249 to_x = min (to_x, max_x);
21250
21251 to_y = min (max_y, output_cursor.y + updated_row->height);
21252
21253 /* Notice if the cursor will be cleared by this operation. */
21254 if (!updated_row->full_width_p)
21255 notice_overwritten_cursor (w, updated_area,
21256 output_cursor.x, -1,
21257 updated_row->y,
21258 MATRIX_ROW_BOTTOM_Y (updated_row));
21259
21260 from_x = output_cursor.x;
21261
21262 /* Translate to frame coordinates. */
21263 if (updated_row->full_width_p)
21264 {
21265 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21266 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21267 }
21268 else
21269 {
21270 int area_left = window_box_left (w, updated_area);
21271 from_x += area_left;
21272 to_x += area_left;
21273 }
21274
21275 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21276 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21277 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21278
21279 /* Prevent inadvertently clearing to end of the X window. */
21280 if (to_x > from_x && to_y > from_y)
21281 {
21282 BLOCK_INPUT;
21283 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21284 to_x - from_x, to_y - from_y);
21285 UNBLOCK_INPUT;
21286 }
21287 }
21288
21289 #endif /* HAVE_WINDOW_SYSTEM */
21290
21291
21292 \f
21293 /***********************************************************************
21294 Cursor types
21295 ***********************************************************************/
21296
21297 /* Value is the internal representation of the specified cursor type
21298 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21299 of the bar cursor. */
21300
21301 static enum text_cursor_kinds
21302 get_specified_cursor_type (arg, width)
21303 Lisp_Object arg;
21304 int *width;
21305 {
21306 enum text_cursor_kinds type;
21307
21308 if (NILP (arg))
21309 return NO_CURSOR;
21310
21311 if (EQ (arg, Qbox))
21312 return FILLED_BOX_CURSOR;
21313
21314 if (EQ (arg, Qhollow))
21315 return HOLLOW_BOX_CURSOR;
21316
21317 if (EQ (arg, Qbar))
21318 {
21319 *width = 2;
21320 return BAR_CURSOR;
21321 }
21322
21323 if (CONSP (arg)
21324 && EQ (XCAR (arg), Qbar)
21325 && INTEGERP (XCDR (arg))
21326 && XINT (XCDR (arg)) >= 0)
21327 {
21328 *width = XINT (XCDR (arg));
21329 return BAR_CURSOR;
21330 }
21331
21332 if (EQ (arg, Qhbar))
21333 {
21334 *width = 2;
21335 return HBAR_CURSOR;
21336 }
21337
21338 if (CONSP (arg)
21339 && EQ (XCAR (arg), Qhbar)
21340 && INTEGERP (XCDR (arg))
21341 && XINT (XCDR (arg)) >= 0)
21342 {
21343 *width = XINT (XCDR (arg));
21344 return HBAR_CURSOR;
21345 }
21346
21347 /* Treat anything unknown as "hollow box cursor".
21348 It was bad to signal an error; people have trouble fixing
21349 .Xdefaults with Emacs, when it has something bad in it. */
21350 type = HOLLOW_BOX_CURSOR;
21351
21352 return type;
21353 }
21354
21355 /* Set the default cursor types for specified frame. */
21356 void
21357 set_frame_cursor_types (f, arg)
21358 struct frame *f;
21359 Lisp_Object arg;
21360 {
21361 int width;
21362 Lisp_Object tem;
21363
21364 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21365 FRAME_CURSOR_WIDTH (f) = width;
21366
21367 /* By default, set up the blink-off state depending on the on-state. */
21368
21369 tem = Fassoc (arg, Vblink_cursor_alist);
21370 if (!NILP (tem))
21371 {
21372 FRAME_BLINK_OFF_CURSOR (f)
21373 = get_specified_cursor_type (XCDR (tem), &width);
21374 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21375 }
21376 else
21377 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21378 }
21379
21380
21381 /* Return the cursor we want to be displayed in window W. Return
21382 width of bar/hbar cursor through WIDTH arg. Return with
21383 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21384 (i.e. if the `system caret' should track this cursor).
21385
21386 In a mini-buffer window, we want the cursor only to appear if we
21387 are reading input from this window. For the selected window, we
21388 want the cursor type given by the frame parameter or buffer local
21389 setting of cursor-type. If explicitly marked off, draw no cursor.
21390 In all other cases, we want a hollow box cursor. */
21391
21392 static enum text_cursor_kinds
21393 get_window_cursor_type (w, glyph, width, active_cursor)
21394 struct window *w;
21395 struct glyph *glyph;
21396 int *width;
21397 int *active_cursor;
21398 {
21399 struct frame *f = XFRAME (w->frame);
21400 struct buffer *b = XBUFFER (w->buffer);
21401 int cursor_type = DEFAULT_CURSOR;
21402 Lisp_Object alt_cursor;
21403 int non_selected = 0;
21404
21405 *active_cursor = 1;
21406
21407 /* Echo area */
21408 if (cursor_in_echo_area
21409 && FRAME_HAS_MINIBUF_P (f)
21410 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21411 {
21412 if (w == XWINDOW (echo_area_window))
21413 {
21414 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21415 {
21416 *width = FRAME_CURSOR_WIDTH (f);
21417 return FRAME_DESIRED_CURSOR (f);
21418 }
21419 else
21420 return get_specified_cursor_type (b->cursor_type, width);
21421 }
21422
21423 *active_cursor = 0;
21424 non_selected = 1;
21425 }
21426
21427 /* Detect a nonselected window or nonselected frame. */
21428 else if (w != XWINDOW (f->selected_window)
21429 #ifdef HAVE_WINDOW_SYSTEM
21430 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21431 #endif
21432 )
21433 {
21434 *active_cursor = 0;
21435
21436 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21437 return NO_CURSOR;
21438
21439 non_selected = 1;
21440 }
21441
21442 /* Never display a cursor in a window in which cursor-type is nil. */
21443 if (NILP (b->cursor_type))
21444 return NO_CURSOR;
21445
21446 /* Get the normal cursor type for this window. */
21447 if (EQ (b->cursor_type, Qt))
21448 {
21449 cursor_type = FRAME_DESIRED_CURSOR (f);
21450 *width = FRAME_CURSOR_WIDTH (f);
21451 }
21452 else
21453 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21454
21455 /* Use cursor-in-non-selected-windows instead
21456 for non-selected window or frame. */
21457 if (non_selected)
21458 {
21459 alt_cursor = b->cursor_in_non_selected_windows;
21460 if (!EQ (Qt, alt_cursor))
21461 return get_specified_cursor_type (alt_cursor, width);
21462 /* t means modify the normal cursor type. */
21463 if (cursor_type == FILLED_BOX_CURSOR)
21464 cursor_type = HOLLOW_BOX_CURSOR;
21465 else if (cursor_type == BAR_CURSOR && *width > 1)
21466 --*width;
21467 return cursor_type;
21468 }
21469
21470 /* Use normal cursor if not blinked off. */
21471 if (!w->cursor_off_p)
21472 {
21473 #ifdef HAVE_WINDOW_SYSTEM
21474 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21475 {
21476 if (cursor_type == FILLED_BOX_CURSOR)
21477 {
21478 /* Using a block cursor on large images can be very annoying.
21479 So use a hollow cursor for "large" images.
21480 If image is not transparent (no mask), also use hollow cursor. */
21481 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21482 if (img != NULL && IMAGEP (img->spec))
21483 {
21484 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21485 where N = size of default frame font size.
21486 This should cover most of the "tiny" icons people may use. */
21487 if (!img->mask
21488 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21489 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21490 cursor_type = HOLLOW_BOX_CURSOR;
21491 }
21492 }
21493 else if (cursor_type != NO_CURSOR)
21494 {
21495 /* Display current only supports BOX and HOLLOW cursors for images.
21496 So for now, unconditionally use a HOLLOW cursor when cursor is
21497 not a solid box cursor. */
21498 cursor_type = HOLLOW_BOX_CURSOR;
21499 }
21500 }
21501 #endif
21502 return cursor_type;
21503 }
21504
21505 /* Cursor is blinked off, so determine how to "toggle" it. */
21506
21507 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21508 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21509 return get_specified_cursor_type (XCDR (alt_cursor), width);
21510
21511 /* Then see if frame has specified a specific blink off cursor type. */
21512 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21513 {
21514 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21515 return FRAME_BLINK_OFF_CURSOR (f);
21516 }
21517
21518 #if 0
21519 /* Some people liked having a permanently visible blinking cursor,
21520 while others had very strong opinions against it. So it was
21521 decided to remove it. KFS 2003-09-03 */
21522
21523 /* Finally perform built-in cursor blinking:
21524 filled box <-> hollow box
21525 wide [h]bar <-> narrow [h]bar
21526 narrow [h]bar <-> no cursor
21527 other type <-> no cursor */
21528
21529 if (cursor_type == FILLED_BOX_CURSOR)
21530 return HOLLOW_BOX_CURSOR;
21531
21532 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21533 {
21534 *width = 1;
21535 return cursor_type;
21536 }
21537 #endif
21538
21539 return NO_CURSOR;
21540 }
21541
21542
21543 #ifdef HAVE_WINDOW_SYSTEM
21544
21545 /* Notice when the text cursor of window W has been completely
21546 overwritten by a drawing operation that outputs glyphs in AREA
21547 starting at X0 and ending at X1 in the line starting at Y0 and
21548 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21549 the rest of the line after X0 has been written. Y coordinates
21550 are window-relative. */
21551
21552 static void
21553 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21554 struct window *w;
21555 enum glyph_row_area area;
21556 int x0, y0, x1, y1;
21557 {
21558 int cx0, cx1, cy0, cy1;
21559 struct glyph_row *row;
21560
21561 if (!w->phys_cursor_on_p)
21562 return;
21563 if (area != TEXT_AREA)
21564 return;
21565
21566 if (w->phys_cursor.vpos < 0
21567 || w->phys_cursor.vpos >= w->current_matrix->nrows
21568 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21569 !(row->enabled_p && row->displays_text_p)))
21570 return;
21571
21572 if (row->cursor_in_fringe_p)
21573 {
21574 row->cursor_in_fringe_p = 0;
21575 draw_fringe_bitmap (w, row, 0);
21576 w->phys_cursor_on_p = 0;
21577 return;
21578 }
21579
21580 cx0 = w->phys_cursor.x;
21581 cx1 = cx0 + w->phys_cursor_width;
21582 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21583 return;
21584
21585 /* The cursor image will be completely removed from the
21586 screen if the output area intersects the cursor area in
21587 y-direction. When we draw in [y0 y1[, and some part of
21588 the cursor is at y < y0, that part must have been drawn
21589 before. When scrolling, the cursor is erased before
21590 actually scrolling, so we don't come here. When not
21591 scrolling, the rows above the old cursor row must have
21592 changed, and in this case these rows must have written
21593 over the cursor image.
21594
21595 Likewise if part of the cursor is below y1, with the
21596 exception of the cursor being in the first blank row at
21597 the buffer and window end because update_text_area
21598 doesn't draw that row. (Except when it does, but
21599 that's handled in update_text_area.) */
21600
21601 cy0 = w->phys_cursor.y;
21602 cy1 = cy0 + w->phys_cursor_height;
21603 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21604 return;
21605
21606 w->phys_cursor_on_p = 0;
21607 }
21608
21609 #endif /* HAVE_WINDOW_SYSTEM */
21610
21611 \f
21612 /************************************************************************
21613 Mouse Face
21614 ************************************************************************/
21615
21616 #ifdef HAVE_WINDOW_SYSTEM
21617
21618 /* EXPORT for RIF:
21619 Fix the display of area AREA of overlapping row ROW in window W
21620 with respect to the overlapping part OVERLAPS. */
21621
21622 void
21623 x_fix_overlapping_area (w, row, area, overlaps)
21624 struct window *w;
21625 struct glyph_row *row;
21626 enum glyph_row_area area;
21627 int overlaps;
21628 {
21629 int i, x;
21630
21631 BLOCK_INPUT;
21632
21633 x = 0;
21634 for (i = 0; i < row->used[area];)
21635 {
21636 if (row->glyphs[area][i].overlaps_vertically_p)
21637 {
21638 int start = i, start_x = x;
21639
21640 do
21641 {
21642 x += row->glyphs[area][i].pixel_width;
21643 ++i;
21644 }
21645 while (i < row->used[area]
21646 && row->glyphs[area][i].overlaps_vertically_p);
21647
21648 draw_glyphs (w, start_x, row, area,
21649 start, i,
21650 DRAW_NORMAL_TEXT, overlaps);
21651 }
21652 else
21653 {
21654 x += row->glyphs[area][i].pixel_width;
21655 ++i;
21656 }
21657 }
21658
21659 UNBLOCK_INPUT;
21660 }
21661
21662
21663 /* EXPORT:
21664 Draw the cursor glyph of window W in glyph row ROW. See the
21665 comment of draw_glyphs for the meaning of HL. */
21666
21667 void
21668 draw_phys_cursor_glyph (w, row, hl)
21669 struct window *w;
21670 struct glyph_row *row;
21671 enum draw_glyphs_face hl;
21672 {
21673 /* If cursor hpos is out of bounds, don't draw garbage. This can
21674 happen in mini-buffer windows when switching between echo area
21675 glyphs and mini-buffer. */
21676 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21677 {
21678 int on_p = w->phys_cursor_on_p;
21679 int x1;
21680 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21681 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21682 hl, 0);
21683 w->phys_cursor_on_p = on_p;
21684
21685 if (hl == DRAW_CURSOR)
21686 w->phys_cursor_width = x1 - w->phys_cursor.x;
21687 /* When we erase the cursor, and ROW is overlapped by other
21688 rows, make sure that these overlapping parts of other rows
21689 are redrawn. */
21690 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21691 {
21692 w->phys_cursor_width = x1 - w->phys_cursor.x;
21693
21694 if (row > w->current_matrix->rows
21695 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21696 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21697 OVERLAPS_ERASED_CURSOR);
21698
21699 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21700 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21701 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21702 OVERLAPS_ERASED_CURSOR);
21703 }
21704 }
21705 }
21706
21707
21708 /* EXPORT:
21709 Erase the image of a cursor of window W from the screen. */
21710
21711 void
21712 erase_phys_cursor (w)
21713 struct window *w;
21714 {
21715 struct frame *f = XFRAME (w->frame);
21716 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21717 int hpos = w->phys_cursor.hpos;
21718 int vpos = w->phys_cursor.vpos;
21719 int mouse_face_here_p = 0;
21720 struct glyph_matrix *active_glyphs = w->current_matrix;
21721 struct glyph_row *cursor_row;
21722 struct glyph *cursor_glyph;
21723 enum draw_glyphs_face hl;
21724
21725 /* No cursor displayed or row invalidated => nothing to do on the
21726 screen. */
21727 if (w->phys_cursor_type == NO_CURSOR)
21728 goto mark_cursor_off;
21729
21730 /* VPOS >= active_glyphs->nrows means that window has been resized.
21731 Don't bother to erase the cursor. */
21732 if (vpos >= active_glyphs->nrows)
21733 goto mark_cursor_off;
21734
21735 /* If row containing cursor is marked invalid, there is nothing we
21736 can do. */
21737 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21738 if (!cursor_row->enabled_p)
21739 goto mark_cursor_off;
21740
21741 /* If line spacing is > 0, old cursor may only be partially visible in
21742 window after split-window. So adjust visible height. */
21743 cursor_row->visible_height = min (cursor_row->visible_height,
21744 window_text_bottom_y (w) - cursor_row->y);
21745
21746 /* If row is completely invisible, don't attempt to delete a cursor which
21747 isn't there. This can happen if cursor is at top of a window, and
21748 we switch to a buffer with a header line in that window. */
21749 if (cursor_row->visible_height <= 0)
21750 goto mark_cursor_off;
21751
21752 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21753 if (cursor_row->cursor_in_fringe_p)
21754 {
21755 cursor_row->cursor_in_fringe_p = 0;
21756 draw_fringe_bitmap (w, cursor_row, 0);
21757 goto mark_cursor_off;
21758 }
21759
21760 /* This can happen when the new row is shorter than the old one.
21761 In this case, either draw_glyphs or clear_end_of_line
21762 should have cleared the cursor. Note that we wouldn't be
21763 able to erase the cursor in this case because we don't have a
21764 cursor glyph at hand. */
21765 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21766 goto mark_cursor_off;
21767
21768 /* If the cursor is in the mouse face area, redisplay that when
21769 we clear the cursor. */
21770 if (! NILP (dpyinfo->mouse_face_window)
21771 && w == XWINDOW (dpyinfo->mouse_face_window)
21772 && (vpos > dpyinfo->mouse_face_beg_row
21773 || (vpos == dpyinfo->mouse_face_beg_row
21774 && hpos >= dpyinfo->mouse_face_beg_col))
21775 && (vpos < dpyinfo->mouse_face_end_row
21776 || (vpos == dpyinfo->mouse_face_end_row
21777 && hpos < dpyinfo->mouse_face_end_col))
21778 /* Don't redraw the cursor's spot in mouse face if it is at the
21779 end of a line (on a newline). The cursor appears there, but
21780 mouse highlighting does not. */
21781 && cursor_row->used[TEXT_AREA] > hpos)
21782 mouse_face_here_p = 1;
21783
21784 /* Maybe clear the display under the cursor. */
21785 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21786 {
21787 int x, y, left_x;
21788 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21789 int width;
21790
21791 cursor_glyph = get_phys_cursor_glyph (w);
21792 if (cursor_glyph == NULL)
21793 goto mark_cursor_off;
21794
21795 width = cursor_glyph->pixel_width;
21796 left_x = window_box_left_offset (w, TEXT_AREA);
21797 x = w->phys_cursor.x;
21798 if (x < left_x)
21799 width -= left_x - x;
21800 width = min (width, window_box_width (w, TEXT_AREA) - x);
21801 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21802 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21803
21804 if (width > 0)
21805 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21806 }
21807
21808 /* Erase the cursor by redrawing the character underneath it. */
21809 if (mouse_face_here_p)
21810 hl = DRAW_MOUSE_FACE;
21811 else
21812 hl = DRAW_NORMAL_TEXT;
21813 draw_phys_cursor_glyph (w, cursor_row, hl);
21814
21815 mark_cursor_off:
21816 w->phys_cursor_on_p = 0;
21817 w->phys_cursor_type = NO_CURSOR;
21818 }
21819
21820
21821 /* EXPORT:
21822 Display or clear cursor of window W. If ON is zero, clear the
21823 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21824 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21825
21826 void
21827 display_and_set_cursor (w, on, hpos, vpos, x, y)
21828 struct window *w;
21829 int on, hpos, vpos, x, y;
21830 {
21831 struct frame *f = XFRAME (w->frame);
21832 int new_cursor_type;
21833 int new_cursor_width;
21834 int active_cursor;
21835 struct glyph_row *glyph_row;
21836 struct glyph *glyph;
21837
21838 /* This is pointless on invisible frames, and dangerous on garbaged
21839 windows and frames; in the latter case, the frame or window may
21840 be in the midst of changing its size, and x and y may be off the
21841 window. */
21842 if (! FRAME_VISIBLE_P (f)
21843 || FRAME_GARBAGED_P (f)
21844 || vpos >= w->current_matrix->nrows
21845 || hpos >= w->current_matrix->matrix_w)
21846 return;
21847
21848 /* If cursor is off and we want it off, return quickly. */
21849 if (!on && !w->phys_cursor_on_p)
21850 return;
21851
21852 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21853 /* If cursor row is not enabled, we don't really know where to
21854 display the cursor. */
21855 if (!glyph_row->enabled_p)
21856 {
21857 w->phys_cursor_on_p = 0;
21858 return;
21859 }
21860
21861 glyph = NULL;
21862 if (!glyph_row->exact_window_width_line_p
21863 || hpos < glyph_row->used[TEXT_AREA])
21864 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21865
21866 xassert (interrupt_input_blocked);
21867
21868 /* Set new_cursor_type to the cursor we want to be displayed. */
21869 new_cursor_type = get_window_cursor_type (w, glyph,
21870 &new_cursor_width, &active_cursor);
21871
21872 /* If cursor is currently being shown and we don't want it to be or
21873 it is in the wrong place, or the cursor type is not what we want,
21874 erase it. */
21875 if (w->phys_cursor_on_p
21876 && (!on
21877 || w->phys_cursor.x != x
21878 || w->phys_cursor.y != y
21879 || new_cursor_type != w->phys_cursor_type
21880 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21881 && new_cursor_width != w->phys_cursor_width)))
21882 erase_phys_cursor (w);
21883
21884 /* Don't check phys_cursor_on_p here because that flag is only set
21885 to zero in some cases where we know that the cursor has been
21886 completely erased, to avoid the extra work of erasing the cursor
21887 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21888 still not be visible, or it has only been partly erased. */
21889 if (on)
21890 {
21891 w->phys_cursor_ascent = glyph_row->ascent;
21892 w->phys_cursor_height = glyph_row->height;
21893
21894 /* Set phys_cursor_.* before x_draw_.* is called because some
21895 of them may need the information. */
21896 w->phys_cursor.x = x;
21897 w->phys_cursor.y = glyph_row->y;
21898 w->phys_cursor.hpos = hpos;
21899 w->phys_cursor.vpos = vpos;
21900 }
21901
21902 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21903 new_cursor_type, new_cursor_width,
21904 on, active_cursor);
21905 }
21906
21907
21908 /* Switch the display of W's cursor on or off, according to the value
21909 of ON. */
21910
21911 static void
21912 update_window_cursor (w, on)
21913 struct window *w;
21914 int on;
21915 {
21916 /* Don't update cursor in windows whose frame is in the process
21917 of being deleted. */
21918 if (w->current_matrix)
21919 {
21920 BLOCK_INPUT;
21921 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21922 w->phys_cursor.x, w->phys_cursor.y);
21923 UNBLOCK_INPUT;
21924 }
21925 }
21926
21927
21928 /* Call update_window_cursor with parameter ON_P on all leaf windows
21929 in the window tree rooted at W. */
21930
21931 static void
21932 update_cursor_in_window_tree (w, on_p)
21933 struct window *w;
21934 int on_p;
21935 {
21936 while (w)
21937 {
21938 if (!NILP (w->hchild))
21939 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21940 else if (!NILP (w->vchild))
21941 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21942 else
21943 update_window_cursor (w, on_p);
21944
21945 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21946 }
21947 }
21948
21949
21950 /* EXPORT:
21951 Display the cursor on window W, or clear it, according to ON_P.
21952 Don't change the cursor's position. */
21953
21954 void
21955 x_update_cursor (f, on_p)
21956 struct frame *f;
21957 int on_p;
21958 {
21959 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21960 }
21961
21962
21963 /* EXPORT:
21964 Clear the cursor of window W to background color, and mark the
21965 cursor as not shown. This is used when the text where the cursor
21966 is is about to be rewritten. */
21967
21968 void
21969 x_clear_cursor (w)
21970 struct window *w;
21971 {
21972 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21973 update_window_cursor (w, 0);
21974 }
21975
21976
21977 /* EXPORT:
21978 Display the active region described by mouse_face_* according to DRAW. */
21979
21980 void
21981 show_mouse_face (dpyinfo, draw)
21982 Display_Info *dpyinfo;
21983 enum draw_glyphs_face draw;
21984 {
21985 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21986 struct frame *f = XFRAME (WINDOW_FRAME (w));
21987
21988 if (/* If window is in the process of being destroyed, don't bother
21989 to do anything. */
21990 w->current_matrix != NULL
21991 /* Don't update mouse highlight if hidden */
21992 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21993 /* Recognize when we are called to operate on rows that don't exist
21994 anymore. This can happen when a window is split. */
21995 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21996 {
21997 int phys_cursor_on_p = w->phys_cursor_on_p;
21998 struct glyph_row *row, *first, *last;
21999
22000 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22001 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22002
22003 for (row = first; row <= last && row->enabled_p; ++row)
22004 {
22005 int start_hpos, end_hpos, start_x;
22006
22007 /* For all but the first row, the highlight starts at column 0. */
22008 if (row == first)
22009 {
22010 start_hpos = dpyinfo->mouse_face_beg_col;
22011 start_x = dpyinfo->mouse_face_beg_x;
22012 }
22013 else
22014 {
22015 start_hpos = 0;
22016 start_x = 0;
22017 }
22018
22019 if (row == last)
22020 end_hpos = dpyinfo->mouse_face_end_col;
22021 else
22022 {
22023 end_hpos = row->used[TEXT_AREA];
22024 if (draw == DRAW_NORMAL_TEXT)
22025 row->fill_line_p = 1; /* Clear to end of line */
22026 }
22027
22028 if (end_hpos > start_hpos)
22029 {
22030 draw_glyphs (w, start_x, row, TEXT_AREA,
22031 start_hpos, end_hpos,
22032 draw, 0);
22033
22034 row->mouse_face_p
22035 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22036 }
22037 }
22038
22039 /* When we've written over the cursor, arrange for it to
22040 be displayed again. */
22041 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22042 {
22043 BLOCK_INPUT;
22044 display_and_set_cursor (w, 1,
22045 w->phys_cursor.hpos, w->phys_cursor.vpos,
22046 w->phys_cursor.x, w->phys_cursor.y);
22047 UNBLOCK_INPUT;
22048 }
22049 }
22050
22051 /* Change the mouse cursor. */
22052 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22053 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22054 else if (draw == DRAW_MOUSE_FACE)
22055 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22056 else
22057 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22058 }
22059
22060 /* EXPORT:
22061 Clear out the mouse-highlighted active region.
22062 Redraw it un-highlighted first. Value is non-zero if mouse
22063 face was actually drawn unhighlighted. */
22064
22065 int
22066 clear_mouse_face (dpyinfo)
22067 Display_Info *dpyinfo;
22068 {
22069 int cleared = 0;
22070
22071 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22072 {
22073 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22074 cleared = 1;
22075 }
22076
22077 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22078 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22079 dpyinfo->mouse_face_window = Qnil;
22080 dpyinfo->mouse_face_overlay = Qnil;
22081 return cleared;
22082 }
22083
22084
22085 /* EXPORT:
22086 Non-zero if physical cursor of window W is within mouse face. */
22087
22088 int
22089 cursor_in_mouse_face_p (w)
22090 struct window *w;
22091 {
22092 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22093 int in_mouse_face = 0;
22094
22095 if (WINDOWP (dpyinfo->mouse_face_window)
22096 && XWINDOW (dpyinfo->mouse_face_window) == w)
22097 {
22098 int hpos = w->phys_cursor.hpos;
22099 int vpos = w->phys_cursor.vpos;
22100
22101 if (vpos >= dpyinfo->mouse_face_beg_row
22102 && vpos <= dpyinfo->mouse_face_end_row
22103 && (vpos > dpyinfo->mouse_face_beg_row
22104 || hpos >= dpyinfo->mouse_face_beg_col)
22105 && (vpos < dpyinfo->mouse_face_end_row
22106 || hpos < dpyinfo->mouse_face_end_col
22107 || dpyinfo->mouse_face_past_end))
22108 in_mouse_face = 1;
22109 }
22110
22111 return in_mouse_face;
22112 }
22113
22114
22115
22116 \f
22117 /* Find the glyph matrix position of buffer position CHARPOS in window
22118 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22119 current glyphs must be up to date. If CHARPOS is above window
22120 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22121 of last line in W. In the row containing CHARPOS, stop before glyphs
22122 having STOP as object. */
22123
22124 #if 1 /* This is a version of fast_find_position that's more correct
22125 in the presence of hscrolling, for example. I didn't install
22126 it right away because the problem fixed is minor, it failed
22127 in 20.x as well, and I think it's too risky to install
22128 so near the release of 21.1. 2001-09-25 gerd. */
22129
22130 static int
22131 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22132 struct window *w;
22133 int charpos;
22134 int *hpos, *vpos, *x, *y;
22135 Lisp_Object stop;
22136 {
22137 struct glyph_row *row, *first;
22138 struct glyph *glyph, *end;
22139 int past_end = 0;
22140
22141 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22142 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22143 {
22144 *x = first->x;
22145 *y = first->y;
22146 *hpos = 0;
22147 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22148 return 1;
22149 }
22150
22151 row = row_containing_pos (w, charpos, first, NULL, 0);
22152 if (row == NULL)
22153 {
22154 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22155 past_end = 1;
22156 }
22157
22158 /* If whole rows or last part of a row came from a display overlay,
22159 row_containing_pos will skip over such rows because their end pos
22160 equals the start pos of the overlay or interval.
22161
22162 Move back if we have a STOP object and previous row's
22163 end glyph came from STOP. */
22164 if (!NILP (stop))
22165 {
22166 struct glyph_row *prev;
22167 while ((prev = row - 1, prev >= first)
22168 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22169 && prev->used[TEXT_AREA] > 0)
22170 {
22171 struct glyph *beg = prev->glyphs[TEXT_AREA];
22172 glyph = beg + prev->used[TEXT_AREA];
22173 while (--glyph >= beg
22174 && INTEGERP (glyph->object));
22175 if (glyph < beg
22176 || !EQ (stop, glyph->object))
22177 break;
22178 row = prev;
22179 }
22180 }
22181
22182 *x = row->x;
22183 *y = row->y;
22184 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22185
22186 glyph = row->glyphs[TEXT_AREA];
22187 end = glyph + row->used[TEXT_AREA];
22188
22189 /* Skip over glyphs not having an object at the start of the row.
22190 These are special glyphs like truncation marks on terminal
22191 frames. */
22192 if (row->displays_text_p)
22193 while (glyph < end
22194 && INTEGERP (glyph->object)
22195 && !EQ (stop, glyph->object)
22196 && glyph->charpos < 0)
22197 {
22198 *x += glyph->pixel_width;
22199 ++glyph;
22200 }
22201
22202 while (glyph < end
22203 && !INTEGERP (glyph->object)
22204 && !EQ (stop, glyph->object)
22205 && (!BUFFERP (glyph->object)
22206 || glyph->charpos < charpos))
22207 {
22208 *x += glyph->pixel_width;
22209 ++glyph;
22210 }
22211
22212 *hpos = glyph - row->glyphs[TEXT_AREA];
22213 return !past_end;
22214 }
22215
22216 #else /* not 1 */
22217
22218 static int
22219 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22220 struct window *w;
22221 int pos;
22222 int *hpos, *vpos, *x, *y;
22223 Lisp_Object stop;
22224 {
22225 int i;
22226 int lastcol;
22227 int maybe_next_line_p = 0;
22228 int line_start_position;
22229 int yb = window_text_bottom_y (w);
22230 struct glyph_row *row, *best_row;
22231 int row_vpos, best_row_vpos;
22232 int current_x;
22233
22234 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22235 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22236
22237 while (row->y < yb)
22238 {
22239 if (row->used[TEXT_AREA])
22240 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22241 else
22242 line_start_position = 0;
22243
22244 if (line_start_position > pos)
22245 break;
22246 /* If the position sought is the end of the buffer,
22247 don't include the blank lines at the bottom of the window. */
22248 else if (line_start_position == pos
22249 && pos == BUF_ZV (XBUFFER (w->buffer)))
22250 {
22251 maybe_next_line_p = 1;
22252 break;
22253 }
22254 else if (line_start_position > 0)
22255 {
22256 best_row = row;
22257 best_row_vpos = row_vpos;
22258 }
22259
22260 if (row->y + row->height >= yb)
22261 break;
22262
22263 ++row;
22264 ++row_vpos;
22265 }
22266
22267 /* Find the right column within BEST_ROW. */
22268 lastcol = 0;
22269 current_x = best_row->x;
22270 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22271 {
22272 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22273 int charpos = glyph->charpos;
22274
22275 if (BUFFERP (glyph->object))
22276 {
22277 if (charpos == pos)
22278 {
22279 *hpos = i;
22280 *vpos = best_row_vpos;
22281 *x = current_x;
22282 *y = best_row->y;
22283 return 1;
22284 }
22285 else if (charpos > pos)
22286 break;
22287 }
22288 else if (EQ (glyph->object, stop))
22289 break;
22290
22291 if (charpos > 0)
22292 lastcol = i;
22293 current_x += glyph->pixel_width;
22294 }
22295
22296 /* If we're looking for the end of the buffer,
22297 and we didn't find it in the line we scanned,
22298 use the start of the following line. */
22299 if (maybe_next_line_p)
22300 {
22301 ++best_row;
22302 ++best_row_vpos;
22303 lastcol = 0;
22304 current_x = best_row->x;
22305 }
22306
22307 *vpos = best_row_vpos;
22308 *hpos = lastcol + 1;
22309 *x = current_x;
22310 *y = best_row->y;
22311 return 0;
22312 }
22313
22314 #endif /* not 1 */
22315
22316
22317 /* Find the position of the glyph for position POS in OBJECT in
22318 window W's current matrix, and return in *X, *Y the pixel
22319 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22320
22321 RIGHT_P non-zero means return the position of the right edge of the
22322 glyph, RIGHT_P zero means return the left edge position.
22323
22324 If no glyph for POS exists in the matrix, return the position of
22325 the glyph with the next smaller position that is in the matrix, if
22326 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22327 exists in the matrix, return the position of the glyph with the
22328 next larger position in OBJECT.
22329
22330 Value is non-zero if a glyph was found. */
22331
22332 static int
22333 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22334 struct window *w;
22335 int pos;
22336 Lisp_Object object;
22337 int *hpos, *vpos, *x, *y;
22338 int right_p;
22339 {
22340 int yb = window_text_bottom_y (w);
22341 struct glyph_row *r;
22342 struct glyph *best_glyph = NULL;
22343 struct glyph_row *best_row = NULL;
22344 int best_x = 0;
22345
22346 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22347 r->enabled_p && r->y < yb;
22348 ++r)
22349 {
22350 struct glyph *g = r->glyphs[TEXT_AREA];
22351 struct glyph *e = g + r->used[TEXT_AREA];
22352 int gx;
22353
22354 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22355 if (EQ (g->object, object))
22356 {
22357 if (g->charpos == pos)
22358 {
22359 best_glyph = g;
22360 best_x = gx;
22361 best_row = r;
22362 goto found;
22363 }
22364 else if (best_glyph == NULL
22365 || ((eabs (g->charpos - pos)
22366 < eabs (best_glyph->charpos - pos))
22367 && (right_p
22368 ? g->charpos < pos
22369 : g->charpos > pos)))
22370 {
22371 best_glyph = g;
22372 best_x = gx;
22373 best_row = r;
22374 }
22375 }
22376 }
22377
22378 found:
22379
22380 if (best_glyph)
22381 {
22382 *x = best_x;
22383 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22384
22385 if (right_p)
22386 {
22387 *x += best_glyph->pixel_width;
22388 ++*hpos;
22389 }
22390
22391 *y = best_row->y;
22392 *vpos = best_row - w->current_matrix->rows;
22393 }
22394
22395 return best_glyph != NULL;
22396 }
22397
22398
22399 /* See if position X, Y is within a hot-spot of an image. */
22400
22401 static int
22402 on_hot_spot_p (hot_spot, x, y)
22403 Lisp_Object hot_spot;
22404 int x, y;
22405 {
22406 if (!CONSP (hot_spot))
22407 return 0;
22408
22409 if (EQ (XCAR (hot_spot), Qrect))
22410 {
22411 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22412 Lisp_Object rect = XCDR (hot_spot);
22413 Lisp_Object tem;
22414 if (!CONSP (rect))
22415 return 0;
22416 if (!CONSP (XCAR (rect)))
22417 return 0;
22418 if (!CONSP (XCDR (rect)))
22419 return 0;
22420 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22421 return 0;
22422 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22423 return 0;
22424 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22425 return 0;
22426 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22427 return 0;
22428 return 1;
22429 }
22430 else if (EQ (XCAR (hot_spot), Qcircle))
22431 {
22432 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22433 Lisp_Object circ = XCDR (hot_spot);
22434 Lisp_Object lr, lx0, ly0;
22435 if (CONSP (circ)
22436 && CONSP (XCAR (circ))
22437 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22438 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22439 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22440 {
22441 double r = XFLOATINT (lr);
22442 double dx = XINT (lx0) - x;
22443 double dy = XINT (ly0) - y;
22444 return (dx * dx + dy * dy <= r * r);
22445 }
22446 }
22447 else if (EQ (XCAR (hot_spot), Qpoly))
22448 {
22449 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22450 if (VECTORP (XCDR (hot_spot)))
22451 {
22452 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22453 Lisp_Object *poly = v->contents;
22454 int n = v->size;
22455 int i;
22456 int inside = 0;
22457 Lisp_Object lx, ly;
22458 int x0, y0;
22459
22460 /* Need an even number of coordinates, and at least 3 edges. */
22461 if (n < 6 || n & 1)
22462 return 0;
22463
22464 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22465 If count is odd, we are inside polygon. Pixels on edges
22466 may or may not be included depending on actual geometry of the
22467 polygon. */
22468 if ((lx = poly[n-2], !INTEGERP (lx))
22469 || (ly = poly[n-1], !INTEGERP (lx)))
22470 return 0;
22471 x0 = XINT (lx), y0 = XINT (ly);
22472 for (i = 0; i < n; i += 2)
22473 {
22474 int x1 = x0, y1 = y0;
22475 if ((lx = poly[i], !INTEGERP (lx))
22476 || (ly = poly[i+1], !INTEGERP (ly)))
22477 return 0;
22478 x0 = XINT (lx), y0 = XINT (ly);
22479
22480 /* Does this segment cross the X line? */
22481 if (x0 >= x)
22482 {
22483 if (x1 >= x)
22484 continue;
22485 }
22486 else if (x1 < x)
22487 continue;
22488 if (y > y0 && y > y1)
22489 continue;
22490 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22491 inside = !inside;
22492 }
22493 return inside;
22494 }
22495 }
22496 return 0;
22497 }
22498
22499 Lisp_Object
22500 find_hot_spot (map, x, y)
22501 Lisp_Object map;
22502 int x, y;
22503 {
22504 while (CONSP (map))
22505 {
22506 if (CONSP (XCAR (map))
22507 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22508 return XCAR (map);
22509 map = XCDR (map);
22510 }
22511
22512 return Qnil;
22513 }
22514
22515 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22516 3, 3, 0,
22517 doc: /* Lookup in image map MAP coordinates X and Y.
22518 An image map is an alist where each element has the format (AREA ID PLIST).
22519 An AREA is specified as either a rectangle, a circle, or a polygon:
22520 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22521 pixel coordinates of the upper left and bottom right corners.
22522 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22523 and the radius of the circle; r may be a float or integer.
22524 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22525 vector describes one corner in the polygon.
22526 Returns the alist element for the first matching AREA in MAP. */)
22527 (map, x, y)
22528 Lisp_Object map;
22529 Lisp_Object x, y;
22530 {
22531 if (NILP (map))
22532 return Qnil;
22533
22534 CHECK_NUMBER (x);
22535 CHECK_NUMBER (y);
22536
22537 return find_hot_spot (map, XINT (x), XINT (y));
22538 }
22539
22540
22541 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22542 static void
22543 define_frame_cursor1 (f, cursor, pointer)
22544 struct frame *f;
22545 Cursor cursor;
22546 Lisp_Object pointer;
22547 {
22548 /* Do not change cursor shape while dragging mouse. */
22549 if (!NILP (do_mouse_tracking))
22550 return;
22551
22552 if (!NILP (pointer))
22553 {
22554 if (EQ (pointer, Qarrow))
22555 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22556 else if (EQ (pointer, Qhand))
22557 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22558 else if (EQ (pointer, Qtext))
22559 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22560 else if (EQ (pointer, intern ("hdrag")))
22561 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22562 #ifdef HAVE_X_WINDOWS
22563 else if (EQ (pointer, intern ("vdrag")))
22564 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22565 #endif
22566 else if (EQ (pointer, intern ("hourglass")))
22567 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22568 else if (EQ (pointer, Qmodeline))
22569 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22570 else
22571 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22572 }
22573
22574 if (cursor != No_Cursor)
22575 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22576 }
22577
22578 /* Take proper action when mouse has moved to the mode or header line
22579 or marginal area AREA of window W, x-position X and y-position Y.
22580 X is relative to the start of the text display area of W, so the
22581 width of bitmap areas and scroll bars must be subtracted to get a
22582 position relative to the start of the mode line. */
22583
22584 static void
22585 note_mode_line_or_margin_highlight (window, x, y, area)
22586 Lisp_Object window;
22587 int x, y;
22588 enum window_part area;
22589 {
22590 struct window *w = XWINDOW (window);
22591 struct frame *f = XFRAME (w->frame);
22592 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22593 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22594 Lisp_Object pointer = Qnil;
22595 int charpos, dx, dy, width, height;
22596 Lisp_Object string, object = Qnil;
22597 Lisp_Object pos, help;
22598
22599 Lisp_Object mouse_face;
22600 int original_x_pixel = x;
22601 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22602 struct glyph_row *row;
22603
22604 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22605 {
22606 int x0;
22607 struct glyph *end;
22608
22609 string = mode_line_string (w, area, &x, &y, &charpos,
22610 &object, &dx, &dy, &width, &height);
22611
22612 row = (area == ON_MODE_LINE
22613 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22614 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22615
22616 /* Find glyph */
22617 if (row->mode_line_p && row->enabled_p)
22618 {
22619 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22620 end = glyph + row->used[TEXT_AREA];
22621
22622 for (x0 = original_x_pixel;
22623 glyph < end && x0 >= glyph->pixel_width;
22624 ++glyph)
22625 x0 -= glyph->pixel_width;
22626
22627 if (glyph >= end)
22628 glyph = NULL;
22629 }
22630 }
22631 else
22632 {
22633 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22634 string = marginal_area_string (w, area, &x, &y, &charpos,
22635 &object, &dx, &dy, &width, &height);
22636 }
22637
22638 help = Qnil;
22639
22640 if (IMAGEP (object))
22641 {
22642 Lisp_Object image_map, hotspot;
22643 if ((image_map = Fplist_get (XCDR (object), QCmap),
22644 !NILP (image_map))
22645 && (hotspot = find_hot_spot (image_map, dx, dy),
22646 CONSP (hotspot))
22647 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22648 {
22649 Lisp_Object area_id, plist;
22650
22651 area_id = XCAR (hotspot);
22652 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22653 If so, we could look for mouse-enter, mouse-leave
22654 properties in PLIST (and do something...). */
22655 hotspot = XCDR (hotspot);
22656 if (CONSP (hotspot)
22657 && (plist = XCAR (hotspot), CONSP (plist)))
22658 {
22659 pointer = Fplist_get (plist, Qpointer);
22660 if (NILP (pointer))
22661 pointer = Qhand;
22662 help = Fplist_get (plist, Qhelp_echo);
22663 if (!NILP (help))
22664 {
22665 help_echo_string = help;
22666 /* Is this correct? ++kfs */
22667 XSETWINDOW (help_echo_window, w);
22668 help_echo_object = w->buffer;
22669 help_echo_pos = charpos;
22670 }
22671 }
22672 }
22673 if (NILP (pointer))
22674 pointer = Fplist_get (XCDR (object), QCpointer);
22675 }
22676
22677 if (STRINGP (string))
22678 {
22679 pos = make_number (charpos);
22680 /* If we're on a string with `help-echo' text property, arrange
22681 for the help to be displayed. This is done by setting the
22682 global variable help_echo_string to the help string. */
22683 if (NILP (help))
22684 {
22685 help = Fget_text_property (pos, Qhelp_echo, string);
22686 if (!NILP (help))
22687 {
22688 help_echo_string = help;
22689 XSETWINDOW (help_echo_window, w);
22690 help_echo_object = string;
22691 help_echo_pos = charpos;
22692 }
22693 }
22694
22695 if (NILP (pointer))
22696 pointer = Fget_text_property (pos, Qpointer, string);
22697
22698 /* Change the mouse pointer according to what is under X/Y. */
22699 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22700 {
22701 Lisp_Object map;
22702 map = Fget_text_property (pos, Qlocal_map, string);
22703 if (!KEYMAPP (map))
22704 map = Fget_text_property (pos, Qkeymap, string);
22705 if (!KEYMAPP (map))
22706 cursor = dpyinfo->vertical_scroll_bar_cursor;
22707 }
22708
22709 /* Change the mouse face according to what is under X/Y. */
22710 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22711 if (!NILP (mouse_face)
22712 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22713 && glyph)
22714 {
22715 Lisp_Object b, e;
22716
22717 struct glyph * tmp_glyph;
22718
22719 int gpos;
22720 int gseq_length;
22721 int total_pixel_width;
22722 int ignore;
22723
22724 int vpos, hpos;
22725
22726 b = Fprevious_single_property_change (make_number (charpos + 1),
22727 Qmouse_face, string, Qnil);
22728 if (NILP (b))
22729 b = make_number (0);
22730
22731 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22732 if (NILP (e))
22733 e = make_number (SCHARS (string));
22734
22735 /* Calculate the position(glyph position: GPOS) of GLYPH in
22736 displayed string. GPOS is different from CHARPOS.
22737
22738 CHARPOS is the position of glyph in internal string
22739 object. A mode line string format has structures which
22740 is converted to a flatten by emacs lisp interpreter.
22741 The internal string is an element of the structures.
22742 The displayed string is the flatten string. */
22743 gpos = 0;
22744 if (glyph > row_start_glyph)
22745 {
22746 tmp_glyph = glyph - 1;
22747 while (tmp_glyph >= row_start_glyph
22748 && tmp_glyph->charpos >= XINT (b)
22749 && EQ (tmp_glyph->object, glyph->object))
22750 {
22751 tmp_glyph--;
22752 gpos++;
22753 }
22754 }
22755
22756 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22757 displayed string holding GLYPH.
22758
22759 GSEQ_LENGTH is different from SCHARS (STRING).
22760 SCHARS (STRING) returns the length of the internal string. */
22761 for (tmp_glyph = glyph, gseq_length = gpos;
22762 tmp_glyph->charpos < XINT (e);
22763 tmp_glyph++, gseq_length++)
22764 {
22765 if (!EQ (tmp_glyph->object, glyph->object))
22766 break;
22767 }
22768
22769 total_pixel_width = 0;
22770 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22771 total_pixel_width += tmp_glyph->pixel_width;
22772
22773 /* Pre calculation of re-rendering position */
22774 vpos = (x - gpos);
22775 hpos = (area == ON_MODE_LINE
22776 ? (w->current_matrix)->nrows - 1
22777 : 0);
22778
22779 /* If the re-rendering position is included in the last
22780 re-rendering area, we should do nothing. */
22781 if ( EQ (window, dpyinfo->mouse_face_window)
22782 && dpyinfo->mouse_face_beg_col <= vpos
22783 && vpos < dpyinfo->mouse_face_end_col
22784 && dpyinfo->mouse_face_beg_row == hpos )
22785 return;
22786
22787 if (clear_mouse_face (dpyinfo))
22788 cursor = No_Cursor;
22789
22790 dpyinfo->mouse_face_beg_col = vpos;
22791 dpyinfo->mouse_face_beg_row = hpos;
22792
22793 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22794 dpyinfo->mouse_face_beg_y = 0;
22795
22796 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22797 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22798
22799 dpyinfo->mouse_face_end_x = 0;
22800 dpyinfo->mouse_face_end_y = 0;
22801
22802 dpyinfo->mouse_face_past_end = 0;
22803 dpyinfo->mouse_face_window = window;
22804
22805 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22806 charpos,
22807 0, 0, 0, &ignore,
22808 glyph->face_id, 1);
22809 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22810
22811 if (NILP (pointer))
22812 pointer = Qhand;
22813 }
22814 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22815 clear_mouse_face (dpyinfo);
22816 }
22817 define_frame_cursor1 (f, cursor, pointer);
22818 }
22819
22820
22821 /* EXPORT:
22822 Take proper action when the mouse has moved to position X, Y on
22823 frame F as regards highlighting characters that have mouse-face
22824 properties. Also de-highlighting chars where the mouse was before.
22825 X and Y can be negative or out of range. */
22826
22827 void
22828 note_mouse_highlight (f, x, y)
22829 struct frame *f;
22830 int x, y;
22831 {
22832 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22833 enum window_part part;
22834 Lisp_Object window;
22835 struct window *w;
22836 Cursor cursor = No_Cursor;
22837 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22838 struct buffer *b;
22839
22840 /* When a menu is active, don't highlight because this looks odd. */
22841 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22842 if (popup_activated ())
22843 return;
22844 #endif
22845
22846 if (NILP (Vmouse_highlight)
22847 || !f->glyphs_initialized_p)
22848 return;
22849
22850 dpyinfo->mouse_face_mouse_x = x;
22851 dpyinfo->mouse_face_mouse_y = y;
22852 dpyinfo->mouse_face_mouse_frame = f;
22853
22854 if (dpyinfo->mouse_face_defer)
22855 return;
22856
22857 if (gc_in_progress)
22858 {
22859 dpyinfo->mouse_face_deferred_gc = 1;
22860 return;
22861 }
22862
22863 /* Which window is that in? */
22864 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22865
22866 /* If we were displaying active text in another window, clear that.
22867 Also clear if we move out of text area in same window. */
22868 if (! EQ (window, dpyinfo->mouse_face_window)
22869 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22870 && !NILP (dpyinfo->mouse_face_window)))
22871 clear_mouse_face (dpyinfo);
22872
22873 /* Not on a window -> return. */
22874 if (!WINDOWP (window))
22875 return;
22876
22877 /* Reset help_echo_string. It will get recomputed below. */
22878 help_echo_string = Qnil;
22879
22880 /* Convert to window-relative pixel coordinates. */
22881 w = XWINDOW (window);
22882 frame_to_window_pixel_xy (w, &x, &y);
22883
22884 /* Handle tool-bar window differently since it doesn't display a
22885 buffer. */
22886 if (EQ (window, f->tool_bar_window))
22887 {
22888 note_tool_bar_highlight (f, x, y);
22889 return;
22890 }
22891
22892 /* Mouse is on the mode, header line or margin? */
22893 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22894 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22895 {
22896 note_mode_line_or_margin_highlight (window, x, y, part);
22897 return;
22898 }
22899
22900 if (part == ON_VERTICAL_BORDER)
22901 {
22902 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22903 help_echo_string = build_string ("drag-mouse-1: resize");
22904 }
22905 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22906 || part == ON_SCROLL_BAR)
22907 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22908 else
22909 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22910
22911 /* Are we in a window whose display is up to date?
22912 And verify the buffer's text has not changed. */
22913 b = XBUFFER (w->buffer);
22914 if (part == ON_TEXT
22915 && EQ (w->window_end_valid, w->buffer)
22916 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22917 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22918 {
22919 int hpos, vpos, pos, i, dx, dy, area;
22920 struct glyph *glyph;
22921 Lisp_Object object;
22922 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22923 Lisp_Object *overlay_vec = NULL;
22924 int noverlays;
22925 struct buffer *obuf;
22926 int obegv, ozv, same_region;
22927
22928 /* Find the glyph under X/Y. */
22929 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22930
22931 /* Look for :pointer property on image. */
22932 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22933 {
22934 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22935 if (img != NULL && IMAGEP (img->spec))
22936 {
22937 Lisp_Object image_map, hotspot;
22938 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22939 !NILP (image_map))
22940 && (hotspot = find_hot_spot (image_map,
22941 glyph->slice.x + dx,
22942 glyph->slice.y + dy),
22943 CONSP (hotspot))
22944 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22945 {
22946 Lisp_Object area_id, plist;
22947
22948 area_id = XCAR (hotspot);
22949 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22950 If so, we could look for mouse-enter, mouse-leave
22951 properties in PLIST (and do something...). */
22952 hotspot = XCDR (hotspot);
22953 if (CONSP (hotspot)
22954 && (plist = XCAR (hotspot), CONSP (plist)))
22955 {
22956 pointer = Fplist_get (plist, Qpointer);
22957 if (NILP (pointer))
22958 pointer = Qhand;
22959 help_echo_string = Fplist_get (plist, Qhelp_echo);
22960 if (!NILP (help_echo_string))
22961 {
22962 help_echo_window = window;
22963 help_echo_object = glyph->object;
22964 help_echo_pos = glyph->charpos;
22965 }
22966 }
22967 }
22968 if (NILP (pointer))
22969 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22970 }
22971 }
22972
22973 /* Clear mouse face if X/Y not over text. */
22974 if (glyph == NULL
22975 || area != TEXT_AREA
22976 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22977 {
22978 if (clear_mouse_face (dpyinfo))
22979 cursor = No_Cursor;
22980 if (NILP (pointer))
22981 {
22982 if (area != TEXT_AREA)
22983 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22984 else
22985 pointer = Vvoid_text_area_pointer;
22986 }
22987 goto set_cursor;
22988 }
22989
22990 pos = glyph->charpos;
22991 object = glyph->object;
22992 if (!STRINGP (object) && !BUFFERP (object))
22993 goto set_cursor;
22994
22995 /* If we get an out-of-range value, return now; avoid an error. */
22996 if (BUFFERP (object) && pos > BUF_Z (b))
22997 goto set_cursor;
22998
22999 /* Make the window's buffer temporarily current for
23000 overlays_at and compute_char_face. */
23001 obuf = current_buffer;
23002 current_buffer = b;
23003 obegv = BEGV;
23004 ozv = ZV;
23005 BEGV = BEG;
23006 ZV = Z;
23007
23008 /* Is this char mouse-active or does it have help-echo? */
23009 position = make_number (pos);
23010
23011 if (BUFFERP (object))
23012 {
23013 /* Put all the overlays we want in a vector in overlay_vec. */
23014 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23015 /* Sort overlays into increasing priority order. */
23016 noverlays = sort_overlays (overlay_vec, noverlays, w);
23017 }
23018 else
23019 noverlays = 0;
23020
23021 same_region = (EQ (window, dpyinfo->mouse_face_window)
23022 && vpos >= dpyinfo->mouse_face_beg_row
23023 && vpos <= dpyinfo->mouse_face_end_row
23024 && (vpos > dpyinfo->mouse_face_beg_row
23025 || hpos >= dpyinfo->mouse_face_beg_col)
23026 && (vpos < dpyinfo->mouse_face_end_row
23027 || hpos < dpyinfo->mouse_face_end_col
23028 || dpyinfo->mouse_face_past_end));
23029
23030 if (same_region)
23031 cursor = No_Cursor;
23032
23033 /* Check mouse-face highlighting. */
23034 if (! same_region
23035 /* If there exists an overlay with mouse-face overlapping
23036 the one we are currently highlighting, we have to
23037 check if we enter the overlapping overlay, and then
23038 highlight only that. */
23039 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23040 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23041 {
23042 /* Find the highest priority overlay that has a mouse-face
23043 property. */
23044 overlay = Qnil;
23045 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23046 {
23047 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23048 if (!NILP (mouse_face))
23049 overlay = overlay_vec[i];
23050 }
23051
23052 /* If we're actually highlighting the same overlay as
23053 before, there's no need to do that again. */
23054 if (!NILP (overlay)
23055 && EQ (overlay, dpyinfo->mouse_face_overlay))
23056 goto check_help_echo;
23057
23058 dpyinfo->mouse_face_overlay = overlay;
23059
23060 /* Clear the display of the old active region, if any. */
23061 if (clear_mouse_face (dpyinfo))
23062 cursor = No_Cursor;
23063
23064 /* If no overlay applies, get a text property. */
23065 if (NILP (overlay))
23066 mouse_face = Fget_text_property (position, Qmouse_face, object);
23067
23068 /* Handle the overlay case. */
23069 if (!NILP (overlay))
23070 {
23071 /* Find the range of text around this char that
23072 should be active. */
23073 Lisp_Object before, after;
23074 int ignore;
23075
23076 before = Foverlay_start (overlay);
23077 after = Foverlay_end (overlay);
23078 /* Record this as the current active region. */
23079 fast_find_position (w, XFASTINT (before),
23080 &dpyinfo->mouse_face_beg_col,
23081 &dpyinfo->mouse_face_beg_row,
23082 &dpyinfo->mouse_face_beg_x,
23083 &dpyinfo->mouse_face_beg_y, Qnil);
23084
23085 dpyinfo->mouse_face_past_end
23086 = !fast_find_position (w, XFASTINT (after),
23087 &dpyinfo->mouse_face_end_col,
23088 &dpyinfo->mouse_face_end_row,
23089 &dpyinfo->mouse_face_end_x,
23090 &dpyinfo->mouse_face_end_y, Qnil);
23091 dpyinfo->mouse_face_window = window;
23092
23093 dpyinfo->mouse_face_face_id
23094 = face_at_buffer_position (w, pos, 0, 0,
23095 &ignore, pos + 1,
23096 !dpyinfo->mouse_face_hidden);
23097
23098 /* Display it as active. */
23099 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23100 cursor = No_Cursor;
23101 }
23102 /* Handle the text property case. */
23103 else if (!NILP (mouse_face) && BUFFERP (object))
23104 {
23105 /* Find the range of text around this char that
23106 should be active. */
23107 Lisp_Object before, after, beginning, end;
23108 int ignore;
23109
23110 beginning = Fmarker_position (w->start);
23111 end = make_number (BUF_Z (XBUFFER (object))
23112 - XFASTINT (w->window_end_pos));
23113 before
23114 = Fprevious_single_property_change (make_number (pos + 1),
23115 Qmouse_face,
23116 object, beginning);
23117 after
23118 = Fnext_single_property_change (position, Qmouse_face,
23119 object, end);
23120
23121 /* Record this as the current active region. */
23122 fast_find_position (w, XFASTINT (before),
23123 &dpyinfo->mouse_face_beg_col,
23124 &dpyinfo->mouse_face_beg_row,
23125 &dpyinfo->mouse_face_beg_x,
23126 &dpyinfo->mouse_face_beg_y, Qnil);
23127 dpyinfo->mouse_face_past_end
23128 = !fast_find_position (w, XFASTINT (after),
23129 &dpyinfo->mouse_face_end_col,
23130 &dpyinfo->mouse_face_end_row,
23131 &dpyinfo->mouse_face_end_x,
23132 &dpyinfo->mouse_face_end_y, Qnil);
23133 dpyinfo->mouse_face_window = window;
23134
23135 if (BUFFERP (object))
23136 dpyinfo->mouse_face_face_id
23137 = face_at_buffer_position (w, pos, 0, 0,
23138 &ignore, pos + 1,
23139 !dpyinfo->mouse_face_hidden);
23140
23141 /* Display it as active. */
23142 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23143 cursor = No_Cursor;
23144 }
23145 else if (!NILP (mouse_face) && STRINGP (object))
23146 {
23147 Lisp_Object b, e;
23148 int ignore;
23149
23150 b = Fprevious_single_property_change (make_number (pos + 1),
23151 Qmouse_face,
23152 object, Qnil);
23153 e = Fnext_single_property_change (position, Qmouse_face,
23154 object, Qnil);
23155 if (NILP (b))
23156 b = make_number (0);
23157 if (NILP (e))
23158 e = make_number (SCHARS (object) - 1);
23159
23160 fast_find_string_pos (w, XINT (b), object,
23161 &dpyinfo->mouse_face_beg_col,
23162 &dpyinfo->mouse_face_beg_row,
23163 &dpyinfo->mouse_face_beg_x,
23164 &dpyinfo->mouse_face_beg_y, 0);
23165 fast_find_string_pos (w, XINT (e), object,
23166 &dpyinfo->mouse_face_end_col,
23167 &dpyinfo->mouse_face_end_row,
23168 &dpyinfo->mouse_face_end_x,
23169 &dpyinfo->mouse_face_end_y, 1);
23170 dpyinfo->mouse_face_past_end = 0;
23171 dpyinfo->mouse_face_window = window;
23172 dpyinfo->mouse_face_face_id
23173 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23174 glyph->face_id, 1);
23175 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23176 cursor = No_Cursor;
23177 }
23178 else if (STRINGP (object) && NILP (mouse_face))
23179 {
23180 /* A string which doesn't have mouse-face, but
23181 the text ``under'' it might have. */
23182 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23183 int start = MATRIX_ROW_START_CHARPOS (r);
23184
23185 pos = string_buffer_position (w, object, start);
23186 if (pos > 0)
23187 mouse_face = get_char_property_and_overlay (make_number (pos),
23188 Qmouse_face,
23189 w->buffer,
23190 &overlay);
23191 if (!NILP (mouse_face) && !NILP (overlay))
23192 {
23193 Lisp_Object before = Foverlay_start (overlay);
23194 Lisp_Object after = Foverlay_end (overlay);
23195 int ignore;
23196
23197 /* Note that we might not be able to find position
23198 BEFORE in the glyph matrix if the overlay is
23199 entirely covered by a `display' property. In
23200 this case, we overshoot. So let's stop in
23201 the glyph matrix before glyphs for OBJECT. */
23202 fast_find_position (w, XFASTINT (before),
23203 &dpyinfo->mouse_face_beg_col,
23204 &dpyinfo->mouse_face_beg_row,
23205 &dpyinfo->mouse_face_beg_x,
23206 &dpyinfo->mouse_face_beg_y,
23207 object);
23208
23209 dpyinfo->mouse_face_past_end
23210 = !fast_find_position (w, XFASTINT (after),
23211 &dpyinfo->mouse_face_end_col,
23212 &dpyinfo->mouse_face_end_row,
23213 &dpyinfo->mouse_face_end_x,
23214 &dpyinfo->mouse_face_end_y,
23215 Qnil);
23216 dpyinfo->mouse_face_window = window;
23217 dpyinfo->mouse_face_face_id
23218 = face_at_buffer_position (w, pos, 0, 0,
23219 &ignore, pos + 1,
23220 !dpyinfo->mouse_face_hidden);
23221
23222 /* Display it as active. */
23223 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23224 cursor = No_Cursor;
23225 }
23226 }
23227 }
23228
23229 check_help_echo:
23230
23231 /* Look for a `help-echo' property. */
23232 if (NILP (help_echo_string)) {
23233 Lisp_Object help, overlay;
23234
23235 /* Check overlays first. */
23236 help = overlay = Qnil;
23237 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23238 {
23239 overlay = overlay_vec[i];
23240 help = Foverlay_get (overlay, Qhelp_echo);
23241 }
23242
23243 if (!NILP (help))
23244 {
23245 help_echo_string = help;
23246 help_echo_window = window;
23247 help_echo_object = overlay;
23248 help_echo_pos = pos;
23249 }
23250 else
23251 {
23252 Lisp_Object object = glyph->object;
23253 int charpos = glyph->charpos;
23254
23255 /* Try text properties. */
23256 if (STRINGP (object)
23257 && charpos >= 0
23258 && charpos < SCHARS (object))
23259 {
23260 help = Fget_text_property (make_number (charpos),
23261 Qhelp_echo, object);
23262 if (NILP (help))
23263 {
23264 /* If the string itself doesn't specify a help-echo,
23265 see if the buffer text ``under'' it does. */
23266 struct glyph_row *r
23267 = MATRIX_ROW (w->current_matrix, vpos);
23268 int start = MATRIX_ROW_START_CHARPOS (r);
23269 int pos = string_buffer_position (w, object, start);
23270 if (pos > 0)
23271 {
23272 help = Fget_char_property (make_number (pos),
23273 Qhelp_echo, w->buffer);
23274 if (!NILP (help))
23275 {
23276 charpos = pos;
23277 object = w->buffer;
23278 }
23279 }
23280 }
23281 }
23282 else if (BUFFERP (object)
23283 && charpos >= BEGV
23284 && charpos < ZV)
23285 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23286 object);
23287
23288 if (!NILP (help))
23289 {
23290 help_echo_string = help;
23291 help_echo_window = window;
23292 help_echo_object = object;
23293 help_echo_pos = charpos;
23294 }
23295 }
23296 }
23297
23298 /* Look for a `pointer' property. */
23299 if (NILP (pointer))
23300 {
23301 /* Check overlays first. */
23302 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23303 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23304
23305 if (NILP (pointer))
23306 {
23307 Lisp_Object object = glyph->object;
23308 int charpos = glyph->charpos;
23309
23310 /* Try text properties. */
23311 if (STRINGP (object)
23312 && charpos >= 0
23313 && charpos < SCHARS (object))
23314 {
23315 pointer = Fget_text_property (make_number (charpos),
23316 Qpointer, object);
23317 if (NILP (pointer))
23318 {
23319 /* If the string itself doesn't specify a pointer,
23320 see if the buffer text ``under'' it does. */
23321 struct glyph_row *r
23322 = MATRIX_ROW (w->current_matrix, vpos);
23323 int start = MATRIX_ROW_START_CHARPOS (r);
23324 int pos = string_buffer_position (w, object, start);
23325 if (pos > 0)
23326 pointer = Fget_char_property (make_number (pos),
23327 Qpointer, w->buffer);
23328 }
23329 }
23330 else if (BUFFERP (object)
23331 && charpos >= BEGV
23332 && charpos < ZV)
23333 pointer = Fget_text_property (make_number (charpos),
23334 Qpointer, object);
23335 }
23336 }
23337
23338 BEGV = obegv;
23339 ZV = ozv;
23340 current_buffer = obuf;
23341 }
23342
23343 set_cursor:
23344
23345 define_frame_cursor1 (f, cursor, pointer);
23346 }
23347
23348
23349 /* EXPORT for RIF:
23350 Clear any mouse-face on window W. This function is part of the
23351 redisplay interface, and is called from try_window_id and similar
23352 functions to ensure the mouse-highlight is off. */
23353
23354 void
23355 x_clear_window_mouse_face (w)
23356 struct window *w;
23357 {
23358 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23359 Lisp_Object window;
23360
23361 BLOCK_INPUT;
23362 XSETWINDOW (window, w);
23363 if (EQ (window, dpyinfo->mouse_face_window))
23364 clear_mouse_face (dpyinfo);
23365 UNBLOCK_INPUT;
23366 }
23367
23368
23369 /* EXPORT:
23370 Just discard the mouse face information for frame F, if any.
23371 This is used when the size of F is changed. */
23372
23373 void
23374 cancel_mouse_face (f)
23375 struct frame *f;
23376 {
23377 Lisp_Object window;
23378 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23379
23380 window = dpyinfo->mouse_face_window;
23381 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23382 {
23383 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23384 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23385 dpyinfo->mouse_face_window = Qnil;
23386 }
23387 }
23388
23389
23390 #endif /* HAVE_WINDOW_SYSTEM */
23391
23392 \f
23393 /***********************************************************************
23394 Exposure Events
23395 ***********************************************************************/
23396
23397 #ifdef HAVE_WINDOW_SYSTEM
23398
23399 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23400 which intersects rectangle R. R is in window-relative coordinates. */
23401
23402 static void
23403 expose_area (w, row, r, area)
23404 struct window *w;
23405 struct glyph_row *row;
23406 XRectangle *r;
23407 enum glyph_row_area area;
23408 {
23409 struct glyph *first = row->glyphs[area];
23410 struct glyph *end = row->glyphs[area] + row->used[area];
23411 struct glyph *last;
23412 int first_x, start_x, x;
23413
23414 if (area == TEXT_AREA && row->fill_line_p)
23415 /* If row extends face to end of line write the whole line. */
23416 draw_glyphs (w, 0, row, area,
23417 0, row->used[area],
23418 DRAW_NORMAL_TEXT, 0);
23419 else
23420 {
23421 /* Set START_X to the window-relative start position for drawing glyphs of
23422 AREA. The first glyph of the text area can be partially visible.
23423 The first glyphs of other areas cannot. */
23424 start_x = window_box_left_offset (w, area);
23425 x = start_x;
23426 if (area == TEXT_AREA)
23427 x += row->x;
23428
23429 /* Find the first glyph that must be redrawn. */
23430 while (first < end
23431 && x + first->pixel_width < r->x)
23432 {
23433 x += first->pixel_width;
23434 ++first;
23435 }
23436
23437 /* Find the last one. */
23438 last = first;
23439 first_x = x;
23440 while (last < end
23441 && x < r->x + r->width)
23442 {
23443 x += last->pixel_width;
23444 ++last;
23445 }
23446
23447 /* Repaint. */
23448 if (last > first)
23449 draw_glyphs (w, first_x - start_x, row, area,
23450 first - row->glyphs[area], last - row->glyphs[area],
23451 DRAW_NORMAL_TEXT, 0);
23452 }
23453 }
23454
23455
23456 /* Redraw the parts of the glyph row ROW on window W intersecting
23457 rectangle R. R is in window-relative coordinates. Value is
23458 non-zero if mouse-face was overwritten. */
23459
23460 static int
23461 expose_line (w, row, r)
23462 struct window *w;
23463 struct glyph_row *row;
23464 XRectangle *r;
23465 {
23466 xassert (row->enabled_p);
23467
23468 if (row->mode_line_p || w->pseudo_window_p)
23469 draw_glyphs (w, 0, row, TEXT_AREA,
23470 0, row->used[TEXT_AREA],
23471 DRAW_NORMAL_TEXT, 0);
23472 else
23473 {
23474 if (row->used[LEFT_MARGIN_AREA])
23475 expose_area (w, row, r, LEFT_MARGIN_AREA);
23476 if (row->used[TEXT_AREA])
23477 expose_area (w, row, r, TEXT_AREA);
23478 if (row->used[RIGHT_MARGIN_AREA])
23479 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23480 draw_row_fringe_bitmaps (w, row);
23481 }
23482
23483 return row->mouse_face_p;
23484 }
23485
23486
23487 /* Redraw those parts of glyphs rows during expose event handling that
23488 overlap other rows. Redrawing of an exposed line writes over parts
23489 of lines overlapping that exposed line; this function fixes that.
23490
23491 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23492 row in W's current matrix that is exposed and overlaps other rows.
23493 LAST_OVERLAPPING_ROW is the last such row. */
23494
23495 static void
23496 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23497 struct window *w;
23498 struct glyph_row *first_overlapping_row;
23499 struct glyph_row *last_overlapping_row;
23500 {
23501 struct glyph_row *row;
23502
23503 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23504 if (row->overlapping_p)
23505 {
23506 xassert (row->enabled_p && !row->mode_line_p);
23507
23508 if (row->used[LEFT_MARGIN_AREA])
23509 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23510
23511 if (row->used[TEXT_AREA])
23512 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23513
23514 if (row->used[RIGHT_MARGIN_AREA])
23515 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23516 }
23517 }
23518
23519
23520 /* Return non-zero if W's cursor intersects rectangle R. */
23521
23522 static int
23523 phys_cursor_in_rect_p (w, r)
23524 struct window *w;
23525 XRectangle *r;
23526 {
23527 XRectangle cr, result;
23528 struct glyph *cursor_glyph;
23529 struct glyph_row *row;
23530
23531 if (w->phys_cursor.vpos >= 0
23532 && w->phys_cursor.vpos < w->current_matrix->nrows
23533 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23534 row->enabled_p)
23535 && row->cursor_in_fringe_p)
23536 {
23537 /* Cursor is in the fringe. */
23538 cr.x = window_box_right_offset (w,
23539 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23540 ? RIGHT_MARGIN_AREA
23541 : TEXT_AREA));
23542 cr.y = row->y;
23543 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23544 cr.height = row->height;
23545 return x_intersect_rectangles (&cr, r, &result);
23546 }
23547
23548 cursor_glyph = get_phys_cursor_glyph (w);
23549 if (cursor_glyph)
23550 {
23551 /* r is relative to W's box, but w->phys_cursor.x is relative
23552 to left edge of W's TEXT area. Adjust it. */
23553 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23554 cr.y = w->phys_cursor.y;
23555 cr.width = cursor_glyph->pixel_width;
23556 cr.height = w->phys_cursor_height;
23557 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23558 I assume the effect is the same -- and this is portable. */
23559 return x_intersect_rectangles (&cr, r, &result);
23560 }
23561 /* If we don't understand the format, pretend we're not in the hot-spot. */
23562 return 0;
23563 }
23564
23565
23566 /* EXPORT:
23567 Draw a vertical window border to the right of window W if W doesn't
23568 have vertical scroll bars. */
23569
23570 void
23571 x_draw_vertical_border (w)
23572 struct window *w;
23573 {
23574 struct frame *f = XFRAME (WINDOW_FRAME (w));
23575
23576 /* We could do better, if we knew what type of scroll-bar the adjacent
23577 windows (on either side) have... But we don't :-(
23578 However, I think this works ok. ++KFS 2003-04-25 */
23579
23580 /* Redraw borders between horizontally adjacent windows. Don't
23581 do it for frames with vertical scroll bars because either the
23582 right scroll bar of a window, or the left scroll bar of its
23583 neighbor will suffice as a border. */
23584 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23585 return;
23586
23587 if (!WINDOW_RIGHTMOST_P (w)
23588 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23589 {
23590 int x0, x1, y0, y1;
23591
23592 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23593 y1 -= 1;
23594
23595 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23596 x1 -= 1;
23597
23598 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23599 }
23600 else if (!WINDOW_LEFTMOST_P (w)
23601 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23602 {
23603 int x0, x1, y0, y1;
23604
23605 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23606 y1 -= 1;
23607
23608 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23609 x0 -= 1;
23610
23611 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23612 }
23613 }
23614
23615
23616 /* Redraw the part of window W intersection rectangle FR. Pixel
23617 coordinates in FR are frame-relative. Call this function with
23618 input blocked. Value is non-zero if the exposure overwrites
23619 mouse-face. */
23620
23621 static int
23622 expose_window (w, fr)
23623 struct window *w;
23624 XRectangle *fr;
23625 {
23626 struct frame *f = XFRAME (w->frame);
23627 XRectangle wr, r;
23628 int mouse_face_overwritten_p = 0;
23629
23630 /* If window is not yet fully initialized, do nothing. This can
23631 happen when toolkit scroll bars are used and a window is split.
23632 Reconfiguring the scroll bar will generate an expose for a newly
23633 created window. */
23634 if (w->current_matrix == NULL)
23635 return 0;
23636
23637 /* When we're currently updating the window, display and current
23638 matrix usually don't agree. Arrange for a thorough display
23639 later. */
23640 if (w == updated_window)
23641 {
23642 SET_FRAME_GARBAGED (f);
23643 return 0;
23644 }
23645
23646 /* Frame-relative pixel rectangle of W. */
23647 wr.x = WINDOW_LEFT_EDGE_X (w);
23648 wr.y = WINDOW_TOP_EDGE_Y (w);
23649 wr.width = WINDOW_TOTAL_WIDTH (w);
23650 wr.height = WINDOW_TOTAL_HEIGHT (w);
23651
23652 if (x_intersect_rectangles (fr, &wr, &r))
23653 {
23654 int yb = window_text_bottom_y (w);
23655 struct glyph_row *row;
23656 int cursor_cleared_p;
23657 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23658
23659 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23660 r.x, r.y, r.width, r.height));
23661
23662 /* Convert to window coordinates. */
23663 r.x -= WINDOW_LEFT_EDGE_X (w);
23664 r.y -= WINDOW_TOP_EDGE_Y (w);
23665
23666 /* Turn off the cursor. */
23667 if (!w->pseudo_window_p
23668 && phys_cursor_in_rect_p (w, &r))
23669 {
23670 x_clear_cursor (w);
23671 cursor_cleared_p = 1;
23672 }
23673 else
23674 cursor_cleared_p = 0;
23675
23676 /* Update lines intersecting rectangle R. */
23677 first_overlapping_row = last_overlapping_row = NULL;
23678 for (row = w->current_matrix->rows;
23679 row->enabled_p;
23680 ++row)
23681 {
23682 int y0 = row->y;
23683 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23684
23685 if ((y0 >= r.y && y0 < r.y + r.height)
23686 || (y1 > r.y && y1 < r.y + r.height)
23687 || (r.y >= y0 && r.y < y1)
23688 || (r.y + r.height > y0 && r.y + r.height < y1))
23689 {
23690 /* A header line may be overlapping, but there is no need
23691 to fix overlapping areas for them. KFS 2005-02-12 */
23692 if (row->overlapping_p && !row->mode_line_p)
23693 {
23694 if (first_overlapping_row == NULL)
23695 first_overlapping_row = row;
23696 last_overlapping_row = row;
23697 }
23698
23699 if (expose_line (w, row, &r))
23700 mouse_face_overwritten_p = 1;
23701 }
23702
23703 if (y1 >= yb)
23704 break;
23705 }
23706
23707 /* Display the mode line if there is one. */
23708 if (WINDOW_WANTS_MODELINE_P (w)
23709 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23710 row->enabled_p)
23711 && row->y < r.y + r.height)
23712 {
23713 if (expose_line (w, row, &r))
23714 mouse_face_overwritten_p = 1;
23715 }
23716
23717 if (!w->pseudo_window_p)
23718 {
23719 /* Fix the display of overlapping rows. */
23720 if (first_overlapping_row)
23721 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23722
23723 /* Draw border between windows. */
23724 x_draw_vertical_border (w);
23725
23726 /* Turn the cursor on again. */
23727 if (cursor_cleared_p)
23728 update_window_cursor (w, 1);
23729 }
23730 }
23731
23732 return mouse_face_overwritten_p;
23733 }
23734
23735
23736
23737 /* Redraw (parts) of all windows in the window tree rooted at W that
23738 intersect R. R contains frame pixel coordinates. Value is
23739 non-zero if the exposure overwrites mouse-face. */
23740
23741 static int
23742 expose_window_tree (w, r)
23743 struct window *w;
23744 XRectangle *r;
23745 {
23746 struct frame *f = XFRAME (w->frame);
23747 int mouse_face_overwritten_p = 0;
23748
23749 while (w && !FRAME_GARBAGED_P (f))
23750 {
23751 if (!NILP (w->hchild))
23752 mouse_face_overwritten_p
23753 |= expose_window_tree (XWINDOW (w->hchild), r);
23754 else if (!NILP (w->vchild))
23755 mouse_face_overwritten_p
23756 |= expose_window_tree (XWINDOW (w->vchild), r);
23757 else
23758 mouse_face_overwritten_p |= expose_window (w, r);
23759
23760 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23761 }
23762
23763 return mouse_face_overwritten_p;
23764 }
23765
23766
23767 /* EXPORT:
23768 Redisplay an exposed area of frame F. X and Y are the upper-left
23769 corner of the exposed rectangle. W and H are width and height of
23770 the exposed area. All are pixel values. W or H zero means redraw
23771 the entire frame. */
23772
23773 void
23774 expose_frame (f, x, y, w, h)
23775 struct frame *f;
23776 int x, y, w, h;
23777 {
23778 XRectangle r;
23779 int mouse_face_overwritten_p = 0;
23780
23781 TRACE ((stderr, "expose_frame "));
23782
23783 /* No need to redraw if frame will be redrawn soon. */
23784 if (FRAME_GARBAGED_P (f))
23785 {
23786 TRACE ((stderr, " garbaged\n"));
23787 return;
23788 }
23789
23790 /* If basic faces haven't been realized yet, there is no point in
23791 trying to redraw anything. This can happen when we get an expose
23792 event while Emacs is starting, e.g. by moving another window. */
23793 if (FRAME_FACE_CACHE (f) == NULL
23794 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23795 {
23796 TRACE ((stderr, " no faces\n"));
23797 return;
23798 }
23799
23800 if (w == 0 || h == 0)
23801 {
23802 r.x = r.y = 0;
23803 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23804 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23805 }
23806 else
23807 {
23808 r.x = x;
23809 r.y = y;
23810 r.width = w;
23811 r.height = h;
23812 }
23813
23814 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23815 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23816
23817 if (WINDOWP (f->tool_bar_window))
23818 mouse_face_overwritten_p
23819 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23820
23821 #ifdef HAVE_X_WINDOWS
23822 #ifndef MSDOS
23823 #ifndef USE_X_TOOLKIT
23824 if (WINDOWP (f->menu_bar_window))
23825 mouse_face_overwritten_p
23826 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23827 #endif /* not USE_X_TOOLKIT */
23828 #endif
23829 #endif
23830
23831 /* Some window managers support a focus-follows-mouse style with
23832 delayed raising of frames. Imagine a partially obscured frame,
23833 and moving the mouse into partially obscured mouse-face on that
23834 frame. The visible part of the mouse-face will be highlighted,
23835 then the WM raises the obscured frame. With at least one WM, KDE
23836 2.1, Emacs is not getting any event for the raising of the frame
23837 (even tried with SubstructureRedirectMask), only Expose events.
23838 These expose events will draw text normally, i.e. not
23839 highlighted. Which means we must redo the highlight here.
23840 Subsume it under ``we love X''. --gerd 2001-08-15 */
23841 /* Included in Windows version because Windows most likely does not
23842 do the right thing if any third party tool offers
23843 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23844 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23845 {
23846 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23847 if (f == dpyinfo->mouse_face_mouse_frame)
23848 {
23849 int x = dpyinfo->mouse_face_mouse_x;
23850 int y = dpyinfo->mouse_face_mouse_y;
23851 clear_mouse_face (dpyinfo);
23852 note_mouse_highlight (f, x, y);
23853 }
23854 }
23855 }
23856
23857
23858 /* EXPORT:
23859 Determine the intersection of two rectangles R1 and R2. Return
23860 the intersection in *RESULT. Value is non-zero if RESULT is not
23861 empty. */
23862
23863 int
23864 x_intersect_rectangles (r1, r2, result)
23865 XRectangle *r1, *r2, *result;
23866 {
23867 XRectangle *left, *right;
23868 XRectangle *upper, *lower;
23869 int intersection_p = 0;
23870
23871 /* Rearrange so that R1 is the left-most rectangle. */
23872 if (r1->x < r2->x)
23873 left = r1, right = r2;
23874 else
23875 left = r2, right = r1;
23876
23877 /* X0 of the intersection is right.x0, if this is inside R1,
23878 otherwise there is no intersection. */
23879 if (right->x <= left->x + left->width)
23880 {
23881 result->x = right->x;
23882
23883 /* The right end of the intersection is the minimum of the
23884 the right ends of left and right. */
23885 result->width = (min (left->x + left->width, right->x + right->width)
23886 - result->x);
23887
23888 /* Same game for Y. */
23889 if (r1->y < r2->y)
23890 upper = r1, lower = r2;
23891 else
23892 upper = r2, lower = r1;
23893
23894 /* The upper end of the intersection is lower.y0, if this is inside
23895 of upper. Otherwise, there is no intersection. */
23896 if (lower->y <= upper->y + upper->height)
23897 {
23898 result->y = lower->y;
23899
23900 /* The lower end of the intersection is the minimum of the lower
23901 ends of upper and lower. */
23902 result->height = (min (lower->y + lower->height,
23903 upper->y + upper->height)
23904 - result->y);
23905 intersection_p = 1;
23906 }
23907 }
23908
23909 return intersection_p;
23910 }
23911
23912 #endif /* HAVE_WINDOW_SYSTEM */
23913
23914 \f
23915 /***********************************************************************
23916 Initialization
23917 ***********************************************************************/
23918
23919 void
23920 syms_of_xdisp ()
23921 {
23922 Vwith_echo_area_save_vector = Qnil;
23923 staticpro (&Vwith_echo_area_save_vector);
23924
23925 Vmessage_stack = Qnil;
23926 staticpro (&Vmessage_stack);
23927
23928 Qinhibit_redisplay = intern ("inhibit-redisplay");
23929 staticpro (&Qinhibit_redisplay);
23930
23931 message_dolog_marker1 = Fmake_marker ();
23932 staticpro (&message_dolog_marker1);
23933 message_dolog_marker2 = Fmake_marker ();
23934 staticpro (&message_dolog_marker2);
23935 message_dolog_marker3 = Fmake_marker ();
23936 staticpro (&message_dolog_marker3);
23937
23938 #if GLYPH_DEBUG
23939 defsubr (&Sdump_frame_glyph_matrix);
23940 defsubr (&Sdump_glyph_matrix);
23941 defsubr (&Sdump_glyph_row);
23942 defsubr (&Sdump_tool_bar_row);
23943 defsubr (&Strace_redisplay);
23944 defsubr (&Strace_to_stderr);
23945 #endif
23946 #ifdef HAVE_WINDOW_SYSTEM
23947 defsubr (&Stool_bar_lines_needed);
23948 defsubr (&Slookup_image_map);
23949 #endif
23950 defsubr (&Sformat_mode_line);
23951 defsubr (&Sinvisible_p);
23952
23953 staticpro (&Qmenu_bar_update_hook);
23954 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23955
23956 staticpro (&Qoverriding_terminal_local_map);
23957 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23958
23959 staticpro (&Qoverriding_local_map);
23960 Qoverriding_local_map = intern ("overriding-local-map");
23961
23962 staticpro (&Qwindow_scroll_functions);
23963 Qwindow_scroll_functions = intern ("window-scroll-functions");
23964
23965 staticpro (&Qredisplay_end_trigger_functions);
23966 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23967
23968 staticpro (&Qinhibit_point_motion_hooks);
23969 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23970
23971 QCdata = intern (":data");
23972 staticpro (&QCdata);
23973 Qdisplay = intern ("display");
23974 staticpro (&Qdisplay);
23975 Qspace_width = intern ("space-width");
23976 staticpro (&Qspace_width);
23977 Qraise = intern ("raise");
23978 staticpro (&Qraise);
23979 Qslice = intern ("slice");
23980 staticpro (&Qslice);
23981 Qspace = intern ("space");
23982 staticpro (&Qspace);
23983 Qmargin = intern ("margin");
23984 staticpro (&Qmargin);
23985 Qpointer = intern ("pointer");
23986 staticpro (&Qpointer);
23987 Qleft_margin = intern ("left-margin");
23988 staticpro (&Qleft_margin);
23989 Qright_margin = intern ("right-margin");
23990 staticpro (&Qright_margin);
23991 Qcenter = intern ("center");
23992 staticpro (&Qcenter);
23993 Qline_height = intern ("line-height");
23994 staticpro (&Qline_height);
23995 QCalign_to = intern (":align-to");
23996 staticpro (&QCalign_to);
23997 QCrelative_width = intern (":relative-width");
23998 staticpro (&QCrelative_width);
23999 QCrelative_height = intern (":relative-height");
24000 staticpro (&QCrelative_height);
24001 QCeval = intern (":eval");
24002 staticpro (&QCeval);
24003 QCpropertize = intern (":propertize");
24004 staticpro (&QCpropertize);
24005 QCfile = intern (":file");
24006 staticpro (&QCfile);
24007 Qfontified = intern ("fontified");
24008 staticpro (&Qfontified);
24009 Qfontification_functions = intern ("fontification-functions");
24010 staticpro (&Qfontification_functions);
24011 Qtrailing_whitespace = intern ("trailing-whitespace");
24012 staticpro (&Qtrailing_whitespace);
24013 Qescape_glyph = intern ("escape-glyph");
24014 staticpro (&Qescape_glyph);
24015 Qnobreak_space = intern ("nobreak-space");
24016 staticpro (&Qnobreak_space);
24017 Qimage = intern ("image");
24018 staticpro (&Qimage);
24019 QCmap = intern (":map");
24020 staticpro (&QCmap);
24021 QCpointer = intern (":pointer");
24022 staticpro (&QCpointer);
24023 Qrect = intern ("rect");
24024 staticpro (&Qrect);
24025 Qcircle = intern ("circle");
24026 staticpro (&Qcircle);
24027 Qpoly = intern ("poly");
24028 staticpro (&Qpoly);
24029 Qmessage_truncate_lines = intern ("message-truncate-lines");
24030 staticpro (&Qmessage_truncate_lines);
24031 Qgrow_only = intern ("grow-only");
24032 staticpro (&Qgrow_only);
24033 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24034 staticpro (&Qinhibit_menubar_update);
24035 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24036 staticpro (&Qinhibit_eval_during_redisplay);
24037 Qposition = intern ("position");
24038 staticpro (&Qposition);
24039 Qbuffer_position = intern ("buffer-position");
24040 staticpro (&Qbuffer_position);
24041 Qobject = intern ("object");
24042 staticpro (&Qobject);
24043 Qbar = intern ("bar");
24044 staticpro (&Qbar);
24045 Qhbar = intern ("hbar");
24046 staticpro (&Qhbar);
24047 Qbox = intern ("box");
24048 staticpro (&Qbox);
24049 Qhollow = intern ("hollow");
24050 staticpro (&Qhollow);
24051 Qhand = intern ("hand");
24052 staticpro (&Qhand);
24053 Qarrow = intern ("arrow");
24054 staticpro (&Qarrow);
24055 Qtext = intern ("text");
24056 staticpro (&Qtext);
24057 Qrisky_local_variable = intern ("risky-local-variable");
24058 staticpro (&Qrisky_local_variable);
24059 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24060 staticpro (&Qinhibit_free_realized_faces);
24061
24062 list_of_error = Fcons (Fcons (intern ("error"),
24063 Fcons (intern ("void-variable"), Qnil)),
24064 Qnil);
24065 staticpro (&list_of_error);
24066
24067 Qlast_arrow_position = intern ("last-arrow-position");
24068 staticpro (&Qlast_arrow_position);
24069 Qlast_arrow_string = intern ("last-arrow-string");
24070 staticpro (&Qlast_arrow_string);
24071
24072 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24073 staticpro (&Qoverlay_arrow_string);
24074 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24075 staticpro (&Qoverlay_arrow_bitmap);
24076
24077 echo_buffer[0] = echo_buffer[1] = Qnil;
24078 staticpro (&echo_buffer[0]);
24079 staticpro (&echo_buffer[1]);
24080
24081 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24082 staticpro (&echo_area_buffer[0]);
24083 staticpro (&echo_area_buffer[1]);
24084
24085 Vmessages_buffer_name = build_string ("*Messages*");
24086 staticpro (&Vmessages_buffer_name);
24087
24088 mode_line_proptrans_alist = Qnil;
24089 staticpro (&mode_line_proptrans_alist);
24090 mode_line_string_list = Qnil;
24091 staticpro (&mode_line_string_list);
24092 mode_line_string_face = Qnil;
24093 staticpro (&mode_line_string_face);
24094 mode_line_string_face_prop = Qnil;
24095 staticpro (&mode_line_string_face_prop);
24096 Vmode_line_unwind_vector = Qnil;
24097 staticpro (&Vmode_line_unwind_vector);
24098
24099 help_echo_string = Qnil;
24100 staticpro (&help_echo_string);
24101 help_echo_object = Qnil;
24102 staticpro (&help_echo_object);
24103 help_echo_window = Qnil;
24104 staticpro (&help_echo_window);
24105 previous_help_echo_string = Qnil;
24106 staticpro (&previous_help_echo_string);
24107 help_echo_pos = -1;
24108
24109 #ifdef HAVE_WINDOW_SYSTEM
24110 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24111 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24112 For example, if a block cursor is over a tab, it will be drawn as
24113 wide as that tab on the display. */);
24114 x_stretch_cursor_p = 0;
24115 #endif
24116
24117 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24118 doc: /* *Non-nil means highlight trailing whitespace.
24119 The face used for trailing whitespace is `trailing-whitespace'. */);
24120 Vshow_trailing_whitespace = Qnil;
24121
24122 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24123 doc: /* *Control highlighting of nobreak space and soft hyphen.
24124 A value of t means highlight the character itself (for nobreak space,
24125 use face `nobreak-space').
24126 A value of nil means no highlighting.
24127 Other values mean display the escape glyph followed by an ordinary
24128 space or ordinary hyphen. */);
24129 Vnobreak_char_display = Qt;
24130
24131 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24132 doc: /* *The pointer shape to show in void text areas.
24133 A value of nil means to show the text pointer. Other options are `arrow',
24134 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24135 Vvoid_text_area_pointer = Qarrow;
24136
24137 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24138 doc: /* Non-nil means don't actually do any redisplay.
24139 This is used for internal purposes. */);
24140 Vinhibit_redisplay = Qnil;
24141
24142 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24143 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24144 Vglobal_mode_string = Qnil;
24145
24146 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24147 doc: /* Marker for where to display an arrow on top of the buffer text.
24148 This must be the beginning of a line in order to work.
24149 See also `overlay-arrow-string'. */);
24150 Voverlay_arrow_position = Qnil;
24151
24152 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24153 doc: /* String to display as an arrow in non-window frames.
24154 See also `overlay-arrow-position'. */);
24155 Voverlay_arrow_string = build_string ("=>");
24156
24157 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24158 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24159 The symbols on this list are examined during redisplay to determine
24160 where to display overlay arrows. */);
24161 Voverlay_arrow_variable_list
24162 = Fcons (intern ("overlay-arrow-position"), Qnil);
24163
24164 DEFVAR_INT ("scroll-step", &scroll_step,
24165 doc: /* *The number of lines to try scrolling a window by when point moves out.
24166 If that fails to bring point back on frame, point is centered instead.
24167 If this is zero, point is always centered after it moves off frame.
24168 If you want scrolling to always be a line at a time, you should set
24169 `scroll-conservatively' to a large value rather than set this to 1. */);
24170
24171 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24172 doc: /* *Scroll up to this many lines, to bring point back on screen.
24173 If point moves off-screen, redisplay will scroll by up to
24174 `scroll-conservatively' lines in order to bring point just barely
24175 onto the screen again. If that cannot be done, then redisplay
24176 recenters point as usual.
24177
24178 A value of zero means always recenter point if it moves off screen. */);
24179 scroll_conservatively = 0;
24180
24181 DEFVAR_INT ("scroll-margin", &scroll_margin,
24182 doc: /* *Number of lines of margin at the top and bottom of a window.
24183 Recenter the window whenever point gets within this many lines
24184 of the top or bottom of the window. */);
24185 scroll_margin = 0;
24186
24187 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24188 doc: /* Pixels per inch value for non-window system displays.
24189 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24190 Vdisplay_pixels_per_inch = make_float (72.0);
24191
24192 #if GLYPH_DEBUG
24193 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24194 #endif
24195
24196 DEFVAR_BOOL ("truncate-partial-width-windows",
24197 &truncate_partial_width_windows,
24198 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24199 truncate_partial_width_windows = 1;
24200
24201 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24202 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24203 Any other value means to use the appropriate face, `mode-line',
24204 `header-line', or `menu' respectively. */);
24205 mode_line_inverse_video = 1;
24206
24207 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24208 doc: /* *Maximum buffer size for which line number should be displayed.
24209 If the buffer is bigger than this, the line number does not appear
24210 in the mode line. A value of nil means no limit. */);
24211 Vline_number_display_limit = Qnil;
24212
24213 DEFVAR_INT ("line-number-display-limit-width",
24214 &line_number_display_limit_width,
24215 doc: /* *Maximum line width (in characters) for line number display.
24216 If the average length of the lines near point is bigger than this, then the
24217 line number may be omitted from the mode line. */);
24218 line_number_display_limit_width = 200;
24219
24220 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24221 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24222 highlight_nonselected_windows = 0;
24223
24224 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24225 doc: /* Non-nil if more than one frame is visible on this display.
24226 Minibuffer-only frames don't count, but iconified frames do.
24227 This variable is not guaranteed to be accurate except while processing
24228 `frame-title-format' and `icon-title-format'. */);
24229
24230 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24231 doc: /* Template for displaying the title bar of visible frames.
24232 \(Assuming the window manager supports this feature.)
24233
24234 This variable has the same structure as `mode-line-format', except that
24235 the %c and %l constructs are ignored. It is used only on frames for
24236 which no explicit name has been set \(see `modify-frame-parameters'). */);
24237
24238 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24239 doc: /* Template for displaying the title bar of an iconified frame.
24240 \(Assuming the window manager supports this feature.)
24241 This variable has the same structure as `mode-line-format' (which see),
24242 and is used only on frames for which no explicit name has been set
24243 \(see `modify-frame-parameters'). */);
24244 Vicon_title_format
24245 = Vframe_title_format
24246 = Fcons (intern ("multiple-frames"),
24247 Fcons (build_string ("%b"),
24248 Fcons (Fcons (empty_unibyte_string,
24249 Fcons (intern ("invocation-name"),
24250 Fcons (build_string ("@"),
24251 Fcons (intern ("system-name"),
24252 Qnil)))),
24253 Qnil)));
24254
24255 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24256 doc: /* Maximum number of lines to keep in the message log buffer.
24257 If nil, disable message logging. If t, log messages but don't truncate
24258 the buffer when it becomes large. */);
24259 Vmessage_log_max = make_number (100);
24260
24261 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24262 doc: /* Functions called before redisplay, if window sizes have changed.
24263 The value should be a list of functions that take one argument.
24264 Just before redisplay, for each frame, if any of its windows have changed
24265 size since the last redisplay, or have been split or deleted,
24266 all the functions in the list are called, with the frame as argument. */);
24267 Vwindow_size_change_functions = Qnil;
24268
24269 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24270 doc: /* List of functions to call before redisplaying a window with scrolling.
24271 Each function is called with two arguments, the window
24272 and its new display-start position. Note that the value of `window-end'
24273 is not valid when these functions are called. */);
24274 Vwindow_scroll_functions = Qnil;
24275
24276 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24277 doc: /* Functions called when redisplay of a window reaches the end trigger.
24278 Each function is called with two arguments, the window and the end trigger value.
24279 See `set-window-redisplay-end-trigger'. */);
24280 Vredisplay_end_trigger_functions = Qnil;
24281
24282 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24283 doc: /* *Non-nil means autoselect window with mouse pointer.
24284 If nil, do not autoselect windows.
24285 A positive number means delay autoselection by that many seconds: a
24286 window is autoselected only after the mouse has remained in that
24287 window for the duration of the delay.
24288 A negative number has a similar effect, but causes windows to be
24289 autoselected only after the mouse has stopped moving. \(Because of
24290 the way Emacs compares mouse events, you will occasionally wait twice
24291 that time before the window gets selected.\)
24292 Any other value means to autoselect window instantaneously when the
24293 mouse pointer enters it.
24294
24295 Autoselection selects the minibuffer only if it is active, and never
24296 unselects the minibuffer if it is active.
24297
24298 When customizing this variable make sure that the actual value of
24299 `focus-follows-mouse' matches the behavior of your window manager. */);
24300 Vmouse_autoselect_window = Qnil;
24301
24302 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24303 doc: /* *Non-nil means automatically resize tool-bars.
24304 This dynamically changes the tool-bar's height to the minimum height
24305 that is needed to make all tool-bar items visible.
24306 If value is `grow-only', the tool-bar's height is only increased
24307 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24308 Vauto_resize_tool_bars = Qt;
24309
24310 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24311 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24312 auto_raise_tool_bar_buttons_p = 1;
24313
24314 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24315 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24316 make_cursor_line_fully_visible_p = 1;
24317
24318 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24319 doc: /* *Border below tool-bar in pixels.
24320 If an integer, use it as the height of the border.
24321 If it is one of `internal-border-width' or `border-width', use the
24322 value of the corresponding frame parameter.
24323 Otherwise, no border is added below the tool-bar. */);
24324 Vtool_bar_border = Qinternal_border_width;
24325
24326 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24327 doc: /* *Margin around tool-bar buttons in pixels.
24328 If an integer, use that for both horizontal and vertical margins.
24329 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24330 HORZ specifying the horizontal margin, and VERT specifying the
24331 vertical margin. */);
24332 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24333
24334 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24335 doc: /* *Relief thickness of tool-bar buttons. */);
24336 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24337
24338 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24339 doc: /* List of functions to call to fontify regions of text.
24340 Each function is called with one argument POS. Functions must
24341 fontify a region starting at POS in the current buffer, and give
24342 fontified regions the property `fontified'. */);
24343 Vfontification_functions = Qnil;
24344 Fmake_variable_buffer_local (Qfontification_functions);
24345
24346 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24347 &unibyte_display_via_language_environment,
24348 doc: /* *Non-nil means display unibyte text according to language environment.
24349 Specifically this means that unibyte non-ASCII characters
24350 are displayed by converting them to the equivalent multibyte characters
24351 according to the current language environment. As a result, they are
24352 displayed according to the current fontset. */);
24353 unibyte_display_via_language_environment = 0;
24354
24355 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24356 doc: /* *Maximum height for resizing mini-windows.
24357 If a float, it specifies a fraction of the mini-window frame's height.
24358 If an integer, it specifies a number of lines. */);
24359 Vmax_mini_window_height = make_float (0.25);
24360
24361 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24362 doc: /* *How to resize mini-windows.
24363 A value of nil means don't automatically resize mini-windows.
24364 A value of t means resize them to fit the text displayed in them.
24365 A value of `grow-only', the default, means let mini-windows grow
24366 only, until their display becomes empty, at which point the windows
24367 go back to their normal size. */);
24368 Vresize_mini_windows = Qgrow_only;
24369
24370 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24371 doc: /* Alist specifying how to blink the cursor off.
24372 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24373 `cursor-type' frame-parameter or variable equals ON-STATE,
24374 comparing using `equal', Emacs uses OFF-STATE to specify
24375 how to blink it off. ON-STATE and OFF-STATE are values for
24376 the `cursor-type' frame parameter.
24377
24378 If a frame's ON-STATE has no entry in this list,
24379 the frame's other specifications determine how to blink the cursor off. */);
24380 Vblink_cursor_alist = Qnil;
24381
24382 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24383 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24384 automatic_hscrolling_p = 1;
24385 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
24386 staticpro (&Qauto_hscroll_mode);
24387
24388 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24389 doc: /* *How many columns away from the window edge point is allowed to get
24390 before automatic hscrolling will horizontally scroll the window. */);
24391 hscroll_margin = 5;
24392
24393 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24394 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24395 When point is less than `hscroll-margin' columns from the window
24396 edge, automatic hscrolling will scroll the window by the amount of columns
24397 determined by this variable. If its value is a positive integer, scroll that
24398 many columns. If it's a positive floating-point number, it specifies the
24399 fraction of the window's width to scroll. If it's nil or zero, point will be
24400 centered horizontally after the scroll. Any other value, including negative
24401 numbers, are treated as if the value were zero.
24402
24403 Automatic hscrolling always moves point outside the scroll margin, so if
24404 point was more than scroll step columns inside the margin, the window will
24405 scroll more than the value given by the scroll step.
24406
24407 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24408 and `scroll-right' overrides this variable's effect. */);
24409 Vhscroll_step = make_number (0);
24410
24411 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24412 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24413 Bind this around calls to `message' to let it take effect. */);
24414 message_truncate_lines = 0;
24415
24416 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24417 doc: /* Normal hook run to update the menu bar definitions.
24418 Redisplay runs this hook before it redisplays the menu bar.
24419 This is used to update submenus such as Buffers,
24420 whose contents depend on various data. */);
24421 Vmenu_bar_update_hook = Qnil;
24422
24423 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24424 doc: /* Frame for which we are updating a menu.
24425 The enable predicate for a menu binding should check this variable. */);
24426 Vmenu_updating_frame = Qnil;
24427
24428 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24429 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24430 inhibit_menubar_update = 0;
24431
24432 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24433 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24434 inhibit_eval_during_redisplay = 0;
24435
24436 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24437 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24438 inhibit_free_realized_faces = 0;
24439
24440 #if GLYPH_DEBUG
24441 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24442 doc: /* Inhibit try_window_id display optimization. */);
24443 inhibit_try_window_id = 0;
24444
24445 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24446 doc: /* Inhibit try_window_reusing display optimization. */);
24447 inhibit_try_window_reusing = 0;
24448
24449 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24450 doc: /* Inhibit try_cursor_movement display optimization. */);
24451 inhibit_try_cursor_movement = 0;
24452 #endif /* GLYPH_DEBUG */
24453
24454 DEFVAR_INT ("overline-margin", &overline_margin,
24455 doc: /* *Space between overline and text, in pixels.
24456 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24457 margin to the caracter height. */);
24458 overline_margin = 2;
24459 }
24460
24461
24462 /* Initialize this module when Emacs starts. */
24463
24464 void
24465 init_xdisp ()
24466 {
24467 Lisp_Object root_window;
24468 struct window *mini_w;
24469
24470 current_header_line_height = current_mode_line_height = -1;
24471
24472 CHARPOS (this_line_start_pos) = 0;
24473
24474 mini_w = XWINDOW (minibuf_window);
24475 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24476
24477 if (!noninteractive)
24478 {
24479 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24480 int i;
24481
24482 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24483 set_window_height (root_window,
24484 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24485 0);
24486 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24487 set_window_height (minibuf_window, 1, 0);
24488
24489 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24490 mini_w->total_cols = make_number (FRAME_COLS (f));
24491
24492 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24493 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24494 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24495
24496 /* The default ellipsis glyphs `...'. */
24497 for (i = 0; i < 3; ++i)
24498 default_invis_vector[i] = make_number ('.');
24499 }
24500
24501 {
24502 /* Allocate the buffer for frame titles.
24503 Also used for `format-mode-line'. */
24504 int size = 100;
24505 mode_line_noprop_buf = (char *) xmalloc (size);
24506 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24507 mode_line_noprop_ptr = mode_line_noprop_buf;
24508 mode_line_target = MODE_LINE_DISPLAY;
24509 }
24510
24511 help_echo_showing_p = 0;
24512 }
24513
24514
24515 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24516 (do not change this comment) */