* movemail.c:
[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
693 /* How close to the margin can point get before the window is scrolled
694 horizontally. */
695 EMACS_INT hscroll_margin;
696
697 /* How much to scroll horizontally when point is inside the above margin. */
698 Lisp_Object Vhscroll_step;
699
700 /* The variable `resize-mini-windows'. If nil, don't resize
701 mini-windows. If t, always resize them to fit the text they
702 display. If `grow-only', let mini-windows grow only until they
703 become empty. */
704
705 Lisp_Object Vresize_mini_windows;
706
707 /* Buffer being redisplayed -- for redisplay_window_error. */
708
709 struct buffer *displayed_buffer;
710
711 /* Space between overline and text. */
712
713 EMACS_INT overline_margin;
714
715 /* Value returned from text property handlers (see below). */
716
717 enum prop_handled
718 {
719 HANDLED_NORMALLY,
720 HANDLED_RECOMPUTE_PROPS,
721 HANDLED_OVERLAY_STRING_CONSUMED,
722 HANDLED_RETURN
723 };
724
725 /* A description of text properties that redisplay is interested
726 in. */
727
728 struct props
729 {
730 /* The name of the property. */
731 Lisp_Object *name;
732
733 /* A unique index for the property. */
734 enum prop_idx idx;
735
736 /* A handler function called to set up iterator IT from the property
737 at IT's current position. Value is used to steer handle_stop. */
738 enum prop_handled (*handler) P_ ((struct it *it));
739 };
740
741 static enum prop_handled handle_face_prop P_ ((struct it *));
742 static enum prop_handled handle_invisible_prop P_ ((struct it *));
743 static enum prop_handled handle_display_prop P_ ((struct it *));
744 static enum prop_handled handle_composition_prop P_ ((struct it *));
745 static enum prop_handled handle_overlay_change P_ ((struct it *));
746 static enum prop_handled handle_fontified_prop P_ ((struct it *));
747
748 /* Properties handled by iterators. */
749
750 static struct props it_props[] =
751 {
752 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
753 /* Handle `face' before `display' because some sub-properties of
754 `display' need to know the face. */
755 {&Qface, FACE_PROP_IDX, handle_face_prop},
756 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
757 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
758 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
759 {NULL, 0, NULL}
760 };
761
762 /* Value is the position described by X. If X is a marker, value is
763 the marker_position of X. Otherwise, value is X. */
764
765 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
766
767 /* Enumeration returned by some move_it_.* functions internally. */
768
769 enum move_it_result
770 {
771 /* Not used. Undefined value. */
772 MOVE_UNDEFINED,
773
774 /* Move ended at the requested buffer position or ZV. */
775 MOVE_POS_MATCH_OR_ZV,
776
777 /* Move ended at the requested X pixel position. */
778 MOVE_X_REACHED,
779
780 /* Move within a line ended at the end of a line that must be
781 continued. */
782 MOVE_LINE_CONTINUED,
783
784 /* Move within a line ended at the end of a line that would
785 be displayed truncated. */
786 MOVE_LINE_TRUNCATED,
787
788 /* Move within a line ended at a line end. */
789 MOVE_NEWLINE_OR_CR
790 };
791
792 /* This counter is used to clear the face cache every once in a while
793 in redisplay_internal. It is incremented for each redisplay.
794 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
795 cleared. */
796
797 #define CLEAR_FACE_CACHE_COUNT 500
798 static int clear_face_cache_count;
799
800 /* Similarly for the image cache. */
801
802 #ifdef HAVE_WINDOW_SYSTEM
803 #define CLEAR_IMAGE_CACHE_COUNT 101
804 static int clear_image_cache_count;
805 #endif
806
807 /* Non-zero while redisplay_internal is in progress. */
808
809 int redisplaying_p;
810
811 /* Non-zero means don't free realized faces. Bound while freeing
812 realized faces is dangerous because glyph matrices might still
813 reference them. */
814
815 int inhibit_free_realized_faces;
816 Lisp_Object Qinhibit_free_realized_faces;
817
818 /* If a string, XTread_socket generates an event to display that string.
819 (The display is done in read_char.) */
820
821 Lisp_Object help_echo_string;
822 Lisp_Object help_echo_window;
823 Lisp_Object help_echo_object;
824 int help_echo_pos;
825
826 /* Temporary variable for XTread_socket. */
827
828 Lisp_Object previous_help_echo_string;
829
830 /* Null glyph slice */
831
832 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
833
834 \f
835 /* Function prototypes. */
836
837 static void setup_for_ellipsis P_ ((struct it *, int));
838 static void mark_window_display_accurate_1 P_ ((struct window *, int));
839 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
840 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
841 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
842 static int redisplay_mode_lines P_ ((Lisp_Object, int));
843 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
844
845 #if 0
846 static int invisible_text_between_p P_ ((struct it *, int, int));
847 #endif
848
849 static void pint2str P_ ((char *, int, int));
850 static void pint2hrstr P_ ((char *, int, int));
851 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
852 struct text_pos));
853 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
854 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
855 static void store_mode_line_noprop_char P_ ((char));
856 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
857 static void x_consider_frame_title P_ ((Lisp_Object));
858 static void handle_stop P_ ((struct it *));
859 static int tool_bar_lines_needed P_ ((struct frame *, int *));
860 static int single_display_spec_intangible_p P_ ((Lisp_Object));
861 static void ensure_echo_area_buffers P_ ((void));
862 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
863 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
864 static int with_echo_area_buffer P_ ((struct window *, int,
865 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
866 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static void clear_garbaged_frames P_ ((void));
868 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int display_echo_area P_ ((struct window *));
872 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
874 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
875 static int string_char_and_length P_ ((const unsigned char *, int, int *));
876 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
877 struct text_pos));
878 static int compute_window_start_on_continuation_line P_ ((struct window *));
879 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
880 static void insert_left_trunc_glyphs P_ ((struct it *));
881 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
882 Lisp_Object));
883 static void extend_face_to_end_of_line P_ ((struct it *));
884 static int append_space_for_newline P_ ((struct it *, int));
885 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
886 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
887 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
888 static int trailing_whitespace_p P_ ((int));
889 static int message_log_check_duplicate P_ ((int, int, int, int));
890 static void push_it P_ ((struct it *));
891 static void pop_it P_ ((struct it *));
892 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
893 static void select_frame_for_redisplay P_ ((Lisp_Object));
894 static void redisplay_internal P_ ((int));
895 static int echo_area_display P_ ((int));
896 static void redisplay_windows P_ ((Lisp_Object));
897 static void redisplay_window P_ ((Lisp_Object, int));
898 static Lisp_Object redisplay_window_error ();
899 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
900 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
901 static int update_menu_bar P_ ((struct frame *, int, int));
902 static int try_window_reusing_current_matrix P_ ((struct window *));
903 static int try_window_id P_ ((struct window *));
904 static int display_line P_ ((struct it *));
905 static int display_mode_lines P_ ((struct window *));
906 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
907 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
908 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
909 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
910 static void display_menu_bar P_ ((struct window *));
911 static int display_count_lines P_ ((int, int, int, int, int *));
912 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
913 int, int, struct it *, int, int, int, int));
914 static void compute_line_metrics P_ ((struct it *));
915 static void run_redisplay_end_trigger_hook P_ ((struct it *));
916 static int get_overlay_strings P_ ((struct it *, int));
917 static int get_overlay_strings_1 P_ ((struct it *, int, int));
918 static void next_overlay_string P_ ((struct it *));
919 static void reseat P_ ((struct it *, struct text_pos, int));
920 static void reseat_1 P_ ((struct it *, struct text_pos, int));
921 static void back_to_previous_visible_line_start P_ ((struct it *));
922 void reseat_at_previous_visible_line_start P_ ((struct it *));
923 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
924 static int next_element_from_ellipsis P_ ((struct it *));
925 static int next_element_from_display_vector P_ ((struct it *));
926 static int next_element_from_string P_ ((struct it *));
927 static int next_element_from_c_string P_ ((struct it *));
928 static int next_element_from_buffer P_ ((struct it *));
929 static int next_element_from_composition P_ ((struct it *));
930 static int next_element_from_image P_ ((struct it *));
931 static int next_element_from_stretch P_ ((struct it *));
932 static void load_overlay_strings P_ ((struct it *, int));
933 static int init_from_display_pos P_ ((struct it *, struct window *,
934 struct display_pos *));
935 static void reseat_to_string P_ ((struct it *, unsigned char *,
936 Lisp_Object, int, int, int, int));
937 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
938 int, int, int));
939 void move_it_vertically_backward P_ ((struct it *, int));
940 static void init_to_row_start P_ ((struct it *, struct window *,
941 struct glyph_row *));
942 static int init_to_row_end P_ ((struct it *, struct window *,
943 struct glyph_row *));
944 static void back_to_previous_line_start P_ ((struct it *));
945 static int forward_to_next_line_start P_ ((struct it *, int *));
946 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
947 Lisp_Object, int));
948 static struct text_pos string_pos P_ ((int, Lisp_Object));
949 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
950 static int number_of_chars P_ ((unsigned char *, int));
951 static void compute_stop_pos P_ ((struct it *));
952 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
953 Lisp_Object));
954 static int face_before_or_after_it_pos P_ ((struct it *, int));
955 static int next_overlay_change P_ ((int));
956 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
957 Lisp_Object, Lisp_Object,
958 struct text_pos *, int));
959 static int underlying_face_id P_ ((struct it *));
960 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
961 struct window *));
962
963 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
964 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
965
966 #ifdef HAVE_WINDOW_SYSTEM
967
968 static void update_tool_bar P_ ((struct frame *, int));
969 static void build_desired_tool_bar_string P_ ((struct frame *f));
970 static int redisplay_tool_bar P_ ((struct frame *));
971 static void display_tool_bar_line P_ ((struct it *, int));
972 static void notice_overwritten_cursor P_ ((struct window *,
973 enum glyph_row_area,
974 int, int, int, int));
975
976
977
978 #endif /* HAVE_WINDOW_SYSTEM */
979
980 \f
981 /***********************************************************************
982 Window display dimensions
983 ***********************************************************************/
984
985 /* Return the bottom boundary y-position for text lines in window W.
986 This is the first y position at which a line cannot start.
987 It is relative to the top of the window.
988
989 This is the height of W minus the height of a mode line, if any. */
990
991 INLINE int
992 window_text_bottom_y (w)
993 struct window *w;
994 {
995 int height = WINDOW_TOTAL_HEIGHT (w);
996
997 if (WINDOW_WANTS_MODELINE_P (w))
998 height -= CURRENT_MODE_LINE_HEIGHT (w);
999 return height;
1000 }
1001
1002 /* Return the pixel width of display area AREA of window W. AREA < 0
1003 means return the total width of W, not including fringes to
1004 the left and right of the window. */
1005
1006 INLINE int
1007 window_box_width (w, area)
1008 struct window *w;
1009 int area;
1010 {
1011 int cols = XFASTINT (w->total_cols);
1012 int pixels = 0;
1013
1014 if (!w->pseudo_window_p)
1015 {
1016 cols -= WINDOW_SCROLL_BAR_COLS (w);
1017
1018 if (area == TEXT_AREA)
1019 {
1020 if (INTEGERP (w->left_margin_cols))
1021 cols -= XFASTINT (w->left_margin_cols);
1022 if (INTEGERP (w->right_margin_cols))
1023 cols -= XFASTINT (w->right_margin_cols);
1024 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1025 }
1026 else if (area == LEFT_MARGIN_AREA)
1027 {
1028 cols = (INTEGERP (w->left_margin_cols)
1029 ? XFASTINT (w->left_margin_cols) : 0);
1030 pixels = 0;
1031 }
1032 else if (area == RIGHT_MARGIN_AREA)
1033 {
1034 cols = (INTEGERP (w->right_margin_cols)
1035 ? XFASTINT (w->right_margin_cols) : 0);
1036 pixels = 0;
1037 }
1038 }
1039
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1041 }
1042
1043
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1046
1047 INLINE int
1048 window_box_height (w)
1049 struct window *w;
1050 {
1051 struct frame *f = XFRAME (w->frame);
1052 int height = WINDOW_TOTAL_HEIGHT (w);
1053
1054 xassert (height >= 0);
1055
1056 /* Note: the code below that determines the mode-line/header-line
1057 height is essentially the same as that contained in the macro
1058 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1059 the appropriate glyph row has its `mode_line_p' flag set,
1060 and if it doesn't, uses estimate_mode_line_height instead. */
1061
1062 if (WINDOW_WANTS_MODELINE_P (w))
1063 {
1064 struct glyph_row *ml_row
1065 = (w->current_matrix && w->current_matrix->rows
1066 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1067 : 0);
1068 if (ml_row && ml_row->mode_line_p)
1069 height -= ml_row->height;
1070 else
1071 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1072 }
1073
1074 if (WINDOW_WANTS_HEADER_LINE_P (w))
1075 {
1076 struct glyph_row *hl_row
1077 = (w->current_matrix && w->current_matrix->rows
1078 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1079 : 0);
1080 if (hl_row && hl_row->mode_line_p)
1081 height -= hl_row->height;
1082 else
1083 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1084 }
1085
1086 /* With a very small font and a mode-line that's taller than
1087 default, we might end up with a negative height. */
1088 return max (0, height);
1089 }
1090
1091 /* Return the window-relative coordinate of the left edge of display
1092 area AREA of window W. AREA < 0 means return the left edge of the
1093 whole window, to the right of the left fringe of W. */
1094
1095 INLINE int
1096 window_box_left_offset (w, area)
1097 struct window *w;
1098 int area;
1099 {
1100 int x;
1101
1102 if (w->pseudo_window_p)
1103 return 0;
1104
1105 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1106
1107 if (area == TEXT_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA));
1110 else if (area == RIGHT_MARGIN_AREA)
1111 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1112 + window_box_width (w, LEFT_MARGIN_AREA)
1113 + window_box_width (w, TEXT_AREA)
1114 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1115 ? 0
1116 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1117 else if (area == LEFT_MARGIN_AREA
1118 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1119 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1120
1121 return x;
1122 }
1123
1124
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the left of the right fringe of W. */
1128
1129 INLINE int
1130 window_box_right_offset (w, area)
1131 struct window *w;
1132 int area;
1133 {
1134 return window_box_left_offset (w, area) + window_box_width (w, area);
1135 }
1136
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. AREA < 0 means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1140
1141 INLINE int
1142 window_box_left (w, area)
1143 struct window *w;
1144 int area;
1145 {
1146 struct frame *f = XFRAME (w->frame);
1147 int x;
1148
1149 if (w->pseudo_window_p)
1150 return FRAME_INTERNAL_BORDER_WIDTH (f);
1151
1152 x = (WINDOW_LEFT_EDGE_X (w)
1153 + window_box_left_offset (w, area));
1154
1155 return x;
1156 }
1157
1158
1159 /* Return the frame-relative coordinate of the right edge of display
1160 area AREA of window W. AREA < 0 means return the left edge of the
1161 whole window, to the left of the right fringe of W. */
1162
1163 INLINE int
1164 window_box_right (w, area)
1165 struct window *w;
1166 int area;
1167 {
1168 return window_box_left (w, area) + window_box_width (w, area);
1169 }
1170
1171 /* Get the bounding box of the display area AREA of window W, without
1172 mode lines, in frame-relative coordinates. AREA < 0 means the
1173 whole window, not including the left and right fringes of
1174 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1175 coordinates of the upper-left corner of the box. Return in
1176 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1177
1178 INLINE void
1179 window_box (w, area, box_x, box_y, box_width, box_height)
1180 struct window *w;
1181 int area;
1182 int *box_x, *box_y, *box_width, *box_height;
1183 {
1184 if (box_width)
1185 *box_width = window_box_width (w, area);
1186 if (box_height)
1187 *box_height = window_box_height (w);
1188 if (box_x)
1189 *box_x = window_box_left (w, area);
1190 if (box_y)
1191 {
1192 *box_y = WINDOW_TOP_EDGE_Y (w);
1193 if (WINDOW_WANTS_HEADER_LINE_P (w))
1194 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1195 }
1196 }
1197
1198
1199 /* Get the bounding box of the display area AREA of window W, without
1200 mode lines. AREA < 0 means the whole window, not including the
1201 left and right fringe of the window. Return in *TOP_LEFT_X
1202 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1203 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1204 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1205 box. */
1206
1207 INLINE void
1208 window_box_edges (w, area, top_left_x, top_left_y,
1209 bottom_right_x, bottom_right_y)
1210 struct window *w;
1211 int area;
1212 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1213 {
1214 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1215 bottom_right_y);
1216 *bottom_right_x += *top_left_x;
1217 *bottom_right_y += *top_left_y;
1218 }
1219
1220
1221 \f
1222 /***********************************************************************
1223 Utilities
1224 ***********************************************************************/
1225
1226 /* Return the bottom y-position of the line the iterator IT is in.
1227 This can modify IT's settings. */
1228
1229 int
1230 line_bottom_y (it)
1231 struct it *it;
1232 {
1233 int line_height = it->max_ascent + it->max_descent;
1234 int line_top_y = it->current_y;
1235
1236 if (line_height == 0)
1237 {
1238 if (last_height)
1239 line_height = last_height;
1240 else if (IT_CHARPOS (*it) < ZV)
1241 {
1242 move_it_by_lines (it, 1, 1);
1243 line_height = (it->max_ascent || it->max_descent
1244 ? it->max_ascent + it->max_descent
1245 : last_height);
1246 }
1247 else
1248 {
1249 struct glyph_row *row = it->glyph_row;
1250
1251 /* Use the default character height. */
1252 it->glyph_row = NULL;
1253 it->what = IT_CHARACTER;
1254 it->c = ' ';
1255 it->len = 1;
1256 PRODUCE_GLYPHS (it);
1257 line_height = it->ascent + it->descent;
1258 it->glyph_row = row;
1259 }
1260 }
1261
1262 return line_top_y + line_height;
1263 }
1264
1265
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1271
1272 int
1273 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1274 struct window *w;
1275 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1276 {
1277 struct it it;
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1281
1282 if (noninteractive)
1283 return visible_p;
1284
1285 if (XBUFFER (w->buffer) != current_buffer)
1286 {
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->buffer));
1289 }
1290
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1292
1293 /* Compute exact mode line heights. */
1294 if (WINDOW_WANTS_MODELINE_P (w))
1295 current_mode_line_height
1296 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1297 current_buffer->mode_line_format);
1298
1299 if (WINDOW_WANTS_HEADER_LINE_P (w))
1300 current_header_line_height
1301 = display_mode_line (w, HEADER_LINE_FACE_ID,
1302 current_buffer->header_line_format);
1303
1304 start_display (&it, w, top);
1305 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1306 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1307
1308 /* Note that we may overshoot because of invisible text. */
1309 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1310 {
1311 int top_x = it.current_x;
1312 int top_y = it.current_y;
1313 int bottom_y = (last_height = 0, line_bottom_y (&it));
1314 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1315
1316 if (top_y < window_top_y)
1317 visible_p = bottom_y > window_top_y;
1318 else if (top_y < it.last_visible_y)
1319 visible_p = 1;
1320 if (visible_p)
1321 {
1322 *x = top_x;
1323 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1324 *rtop = max (0, window_top_y - top_y);
1325 *rbot = max (0, bottom_y - it.last_visible_y);
1326 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1327 - max (top_y, window_top_y)));
1328 *vpos = it.vpos;
1329 }
1330 }
1331 else
1332 {
1333 struct it it2;
1334
1335 it2 = it;
1336 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1337 move_it_by_lines (&it, 1, 0);
1338 if (charpos < IT_CHARPOS (it)
1339 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1340 {
1341 visible_p = 1;
1342 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1343 *x = it2.current_x;
1344 *y = it2.current_y + it2.max_ascent - it2.ascent;
1345 *rtop = max (0, -it2.current_y);
1346 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1347 - it.last_visible_y));
1348 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1349 it.last_visible_y)
1350 - max (it2.current_y,
1351 WINDOW_HEADER_LINE_HEIGHT (w))));
1352 *vpos = it2.vpos;
1353 }
1354 }
1355
1356 if (old_buffer)
1357 set_buffer_internal_1 (old_buffer);
1358
1359 current_header_line_height = current_mode_line_height = -1;
1360
1361 if (visible_p && XFASTINT (w->hscroll) > 0)
1362 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1363
1364 #if 0
1365 /* Debugging code. */
1366 if (visible_p)
1367 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1368 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1369 else
1370 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1371 #endif
1372
1373 return visible_p;
1374 }
1375
1376
1377 /* Return the next character from STR which is MAXLEN bytes long.
1378 Return in *LEN the length of the character. This is like
1379 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1380 we find one, we return a `?', but with the length of the invalid
1381 character. */
1382
1383 static INLINE int
1384 string_char_and_length (str, maxlen, len)
1385 const unsigned char *str;
1386 int maxlen, *len;
1387 {
1388 int c;
1389
1390 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1391 if (!CHAR_VALID_P (c, 1))
1392 /* We may not change the length here because other places in Emacs
1393 don't use this function, i.e. they silently accept invalid
1394 characters. */
1395 c = '?';
1396
1397 return c;
1398 }
1399
1400
1401
1402 /* Given a position POS containing a valid character and byte position
1403 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1404
1405 static struct text_pos
1406 string_pos_nchars_ahead (pos, string, nchars)
1407 struct text_pos pos;
1408 Lisp_Object string;
1409 int nchars;
1410 {
1411 xassert (STRINGP (string) && nchars >= 0);
1412
1413 if (STRING_MULTIBYTE (string))
1414 {
1415 int rest = SBYTES (string) - BYTEPOS (pos);
1416 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1417 int len;
1418
1419 while (nchars--)
1420 {
1421 string_char_and_length (p, rest, &len);
1422 p += len, rest -= len;
1423 xassert (rest >= 0);
1424 CHARPOS (pos) += 1;
1425 BYTEPOS (pos) += len;
1426 }
1427 }
1428 else
1429 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1430
1431 return pos;
1432 }
1433
1434
1435 /* Value is the text position, i.e. character and byte position,
1436 for character position CHARPOS in STRING. */
1437
1438 static INLINE struct text_pos
1439 string_pos (charpos, string)
1440 int charpos;
1441 Lisp_Object string;
1442 {
1443 struct text_pos pos;
1444 xassert (STRINGP (string));
1445 xassert (charpos >= 0);
1446 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1447 return pos;
1448 }
1449
1450
1451 /* Value is a text position, i.e. character and byte position, for
1452 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1453 means recognize multibyte characters. */
1454
1455 static struct text_pos
1456 c_string_pos (charpos, s, multibyte_p)
1457 int charpos;
1458 unsigned char *s;
1459 int multibyte_p;
1460 {
1461 struct text_pos pos;
1462
1463 xassert (s != NULL);
1464 xassert (charpos >= 0);
1465
1466 if (multibyte_p)
1467 {
1468 int rest = strlen (s), len;
1469
1470 SET_TEXT_POS (pos, 0, 0);
1471 while (charpos--)
1472 {
1473 string_char_and_length (s, rest, &len);
1474 s += len, rest -= len;
1475 xassert (rest >= 0);
1476 CHARPOS (pos) += 1;
1477 BYTEPOS (pos) += len;
1478 }
1479 }
1480 else
1481 SET_TEXT_POS (pos, charpos, charpos);
1482
1483 return pos;
1484 }
1485
1486
1487 /* Value is the number of characters in C string S. MULTIBYTE_P
1488 non-zero means recognize multibyte characters. */
1489
1490 static int
1491 number_of_chars (s, multibyte_p)
1492 unsigned char *s;
1493 int multibyte_p;
1494 {
1495 int nchars;
1496
1497 if (multibyte_p)
1498 {
1499 int rest = strlen (s), len;
1500 unsigned char *p = (unsigned char *) s;
1501
1502 for (nchars = 0; rest > 0; ++nchars)
1503 {
1504 string_char_and_length (p, rest, &len);
1505 rest -= len, p += len;
1506 }
1507 }
1508 else
1509 nchars = strlen (s);
1510
1511 return nchars;
1512 }
1513
1514
1515 /* Compute byte position NEWPOS->bytepos corresponding to
1516 NEWPOS->charpos. POS is a known position in string STRING.
1517 NEWPOS->charpos must be >= POS.charpos. */
1518
1519 static void
1520 compute_string_pos (newpos, pos, string)
1521 struct text_pos *newpos, pos;
1522 Lisp_Object string;
1523 {
1524 xassert (STRINGP (string));
1525 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1526
1527 if (STRING_MULTIBYTE (string))
1528 *newpos = string_pos_nchars_ahead (pos, string,
1529 CHARPOS (*newpos) - CHARPOS (pos));
1530 else
1531 BYTEPOS (*newpos) = CHARPOS (*newpos);
1532 }
1533
1534 /* EXPORT:
1535 Return an estimation of the pixel height of mode or top lines on
1536 frame F. FACE_ID specifies what line's height to estimate. */
1537
1538 int
1539 estimate_mode_line_height (f, face_id)
1540 struct frame *f;
1541 enum face_id face_id;
1542 {
1543 #ifdef HAVE_WINDOW_SYSTEM
1544 if (FRAME_WINDOW_P (f))
1545 {
1546 int height = FONT_HEIGHT (FRAME_FONT (f));
1547
1548 /* This function is called so early when Emacs starts that the face
1549 cache and mode line face are not yet initialized. */
1550 if (FRAME_FACE_CACHE (f))
1551 {
1552 struct face *face = FACE_FROM_ID (f, face_id);
1553 if (face)
1554 {
1555 if (face->font)
1556 height = FONT_HEIGHT (face->font);
1557 if (face->box_line_width > 0)
1558 height += 2 * face->box_line_width;
1559 }
1560 }
1561
1562 return height;
1563 }
1564 #endif
1565
1566 return 1;
1567 }
1568
1569 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1570 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1571 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1572 not force the value into range. */
1573
1574 void
1575 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1576 FRAME_PTR f;
1577 register int pix_x, pix_y;
1578 int *x, *y;
1579 NativeRectangle *bounds;
1580 int noclip;
1581 {
1582
1583 #ifdef HAVE_WINDOW_SYSTEM
1584 if (FRAME_WINDOW_P (f))
1585 {
1586 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1587 even for negative values. */
1588 if (pix_x < 0)
1589 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1590 if (pix_y < 0)
1591 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1592
1593 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1594 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1595
1596 if (bounds)
1597 STORE_NATIVE_RECT (*bounds,
1598 FRAME_COL_TO_PIXEL_X (f, pix_x),
1599 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1600 FRAME_COLUMN_WIDTH (f) - 1,
1601 FRAME_LINE_HEIGHT (f) - 1);
1602
1603 if (!noclip)
1604 {
1605 if (pix_x < 0)
1606 pix_x = 0;
1607 else if (pix_x > FRAME_TOTAL_COLS (f))
1608 pix_x = FRAME_TOTAL_COLS (f);
1609
1610 if (pix_y < 0)
1611 pix_y = 0;
1612 else if (pix_y > FRAME_LINES (f))
1613 pix_y = FRAME_LINES (f);
1614 }
1615 }
1616 #endif
1617
1618 *x = pix_x;
1619 *y = pix_y;
1620 }
1621
1622
1623 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1624 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1625 can't tell the positions because W's display is not up to date,
1626 return 0. */
1627
1628 int
1629 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1630 struct window *w;
1631 int hpos, vpos;
1632 int *frame_x, *frame_y;
1633 {
1634 #ifdef HAVE_WINDOW_SYSTEM
1635 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1636 {
1637 int success_p;
1638
1639 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1640 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1641
1642 if (display_completed)
1643 {
1644 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1645 struct glyph *glyph = row->glyphs[TEXT_AREA];
1646 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1647
1648 hpos = row->x;
1649 vpos = row->y;
1650 while (glyph < end)
1651 {
1652 hpos += glyph->pixel_width;
1653 ++glyph;
1654 }
1655
1656 /* If first glyph is partially visible, its first visible position is still 0. */
1657 if (hpos < 0)
1658 hpos = 0;
1659
1660 success_p = 1;
1661 }
1662 else
1663 {
1664 hpos = vpos = 0;
1665 success_p = 0;
1666 }
1667
1668 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1669 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1670 return success_p;
1671 }
1672 #endif
1673
1674 *frame_x = hpos;
1675 *frame_y = vpos;
1676 return 1;
1677 }
1678
1679
1680 #ifdef HAVE_WINDOW_SYSTEM
1681
1682 /* Find the glyph under window-relative coordinates X/Y in window W.
1683 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1684 strings. Return in *HPOS and *VPOS the row and column number of
1685 the glyph found. Return in *AREA the glyph area containing X.
1686 Value is a pointer to the glyph found or null if X/Y is not on
1687 text, or we can't tell because W's current matrix is not up to
1688 date. */
1689
1690 static struct glyph *
1691 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1692 struct window *w;
1693 int x, y;
1694 int *hpos, *vpos, *dx, *dy, *area;
1695 {
1696 struct glyph *glyph, *end;
1697 struct glyph_row *row = NULL;
1698 int x0, i;
1699
1700 /* Find row containing Y. Give up if some row is not enabled. */
1701 for (i = 0; i < w->current_matrix->nrows; ++i)
1702 {
1703 row = MATRIX_ROW (w->current_matrix, i);
1704 if (!row->enabled_p)
1705 return NULL;
1706 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1707 break;
1708 }
1709
1710 *vpos = i;
1711 *hpos = 0;
1712
1713 /* Give up if Y is not in the window. */
1714 if (i == w->current_matrix->nrows)
1715 return NULL;
1716
1717 /* Get the glyph area containing X. */
1718 if (w->pseudo_window_p)
1719 {
1720 *area = TEXT_AREA;
1721 x0 = 0;
1722 }
1723 else
1724 {
1725 if (x < window_box_left_offset (w, TEXT_AREA))
1726 {
1727 *area = LEFT_MARGIN_AREA;
1728 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1729 }
1730 else if (x < window_box_right_offset (w, TEXT_AREA))
1731 {
1732 *area = TEXT_AREA;
1733 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1734 }
1735 else
1736 {
1737 *area = RIGHT_MARGIN_AREA;
1738 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1739 }
1740 }
1741
1742 /* Find glyph containing X. */
1743 glyph = row->glyphs[*area];
1744 end = glyph + row->used[*area];
1745 x -= x0;
1746 while (glyph < end && x >= glyph->pixel_width)
1747 {
1748 x -= glyph->pixel_width;
1749 ++glyph;
1750 }
1751
1752 if (glyph == end)
1753 return NULL;
1754
1755 if (dx)
1756 {
1757 *dx = x;
1758 *dy = y - (row->y + row->ascent - glyph->ascent);
1759 }
1760
1761 *hpos = glyph - row->glyphs[*area];
1762 return glyph;
1763 }
1764
1765
1766 /* EXPORT:
1767 Convert frame-relative x/y to coordinates relative to window W.
1768 Takes pseudo-windows into account. */
1769
1770 void
1771 frame_to_window_pixel_xy (w, x, y)
1772 struct window *w;
1773 int *x, *y;
1774 {
1775 if (w->pseudo_window_p)
1776 {
1777 /* A pseudo-window is always full-width, and starts at the
1778 left edge of the frame, plus a frame border. */
1779 struct frame *f = XFRAME (w->frame);
1780 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1781 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1782 }
1783 else
1784 {
1785 *x -= WINDOW_LEFT_EDGE_X (w);
1786 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1787 }
1788 }
1789
1790 /* EXPORT:
1791 Return in RECTS[] at most N clipping rectangles for glyph string S.
1792 Return the number of stored rectangles. */
1793
1794 int
1795 get_glyph_string_clip_rects (s, rects, n)
1796 struct glyph_string *s;
1797 NativeRectangle *rects;
1798 int n;
1799 {
1800 XRectangle r;
1801
1802 if (n <= 0)
1803 return 0;
1804
1805 if (s->row->full_width_p)
1806 {
1807 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1808 r.x = WINDOW_LEFT_EDGE_X (s->w);
1809 r.width = WINDOW_TOTAL_WIDTH (s->w);
1810
1811 /* Unless displaying a mode or menu bar line, which are always
1812 fully visible, clip to the visible part of the row. */
1813 if (s->w->pseudo_window_p)
1814 r.height = s->row->visible_height;
1815 else
1816 r.height = s->height;
1817 }
1818 else
1819 {
1820 /* This is a text line that may be partially visible. */
1821 r.x = window_box_left (s->w, s->area);
1822 r.width = window_box_width (s->w, s->area);
1823 r.height = s->row->visible_height;
1824 }
1825
1826 if (s->clip_head)
1827 if (r.x < s->clip_head->x)
1828 {
1829 if (r.width >= s->clip_head->x - r.x)
1830 r.width -= s->clip_head->x - r.x;
1831 else
1832 r.width = 0;
1833 r.x = s->clip_head->x;
1834 }
1835 if (s->clip_tail)
1836 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1837 {
1838 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1839 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1840 else
1841 r.width = 0;
1842 }
1843
1844 /* If S draws overlapping rows, it's sufficient to use the top and
1845 bottom of the window for clipping because this glyph string
1846 intentionally draws over other lines. */
1847 if (s->for_overlaps)
1848 {
1849 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1850 r.height = window_text_bottom_y (s->w) - r.y;
1851
1852 /* Alas, the above simple strategy does not work for the
1853 environments with anti-aliased text: if the same text is
1854 drawn onto the same place multiple times, it gets thicker.
1855 If the overlap we are processing is for the erased cursor, we
1856 take the intersection with the rectagle of the cursor. */
1857 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1858 {
1859 XRectangle rc, r_save = r;
1860
1861 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1862 rc.y = s->w->phys_cursor.y;
1863 rc.width = s->w->phys_cursor_width;
1864 rc.height = s->w->phys_cursor_height;
1865
1866 x_intersect_rectangles (&r_save, &rc, &r);
1867 }
1868 }
1869 else
1870 {
1871 /* Don't use S->y for clipping because it doesn't take partially
1872 visible lines into account. For example, it can be negative for
1873 partially visible lines at the top of a window. */
1874 if (!s->row->full_width_p
1875 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1876 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1877 else
1878 r.y = max (0, s->row->y);
1879
1880 /* If drawing a tool-bar window, draw it over the internal border
1881 at the top of the window. */
1882 if (WINDOWP (s->f->tool_bar_window)
1883 && s->w == XWINDOW (s->f->tool_bar_window))
1884 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1885 }
1886
1887 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1888
1889 /* If drawing the cursor, don't let glyph draw outside its
1890 advertised boundaries. Cleartype does this under some circumstances. */
1891 if (s->hl == DRAW_CURSOR)
1892 {
1893 struct glyph *glyph = s->first_glyph;
1894 int height, max_y;
1895
1896 if (s->x > r.x)
1897 {
1898 r.width -= s->x - r.x;
1899 r.x = s->x;
1900 }
1901 r.width = min (r.width, glyph->pixel_width);
1902
1903 /* If r.y is below window bottom, ensure that we still see a cursor. */
1904 height = min (glyph->ascent + glyph->descent,
1905 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1906 max_y = window_text_bottom_y (s->w) - height;
1907 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1908 if (s->ybase - glyph->ascent > max_y)
1909 {
1910 r.y = max_y;
1911 r.height = height;
1912 }
1913 else
1914 {
1915 /* Don't draw cursor glyph taller than our actual glyph. */
1916 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1917 if (height < r.height)
1918 {
1919 max_y = r.y + r.height;
1920 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1921 r.height = min (max_y - r.y, height);
1922 }
1923 }
1924 }
1925
1926 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1927 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1928 {
1929 #ifdef CONVERT_FROM_XRECT
1930 CONVERT_FROM_XRECT (r, *rects);
1931 #else
1932 *rects = r;
1933 #endif
1934 return 1;
1935 }
1936 else
1937 {
1938 /* If we are processing overlapping and allowed to return
1939 multiple clipping rectangles, we exclude the row of the glyph
1940 string from the clipping rectangle. This is to avoid drawing
1941 the same text on the environment with anti-aliasing. */
1942 #ifdef CONVERT_FROM_XRECT
1943 XRectangle rs[2];
1944 #else
1945 XRectangle *rs = rects;
1946 #endif
1947 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1948
1949 if (s->for_overlaps & OVERLAPS_PRED)
1950 {
1951 rs[i] = r;
1952 if (r.y + r.height > row_y)
1953 {
1954 if (r.y < row_y)
1955 rs[i].height = row_y - r.y;
1956 else
1957 rs[i].height = 0;
1958 }
1959 i++;
1960 }
1961 if (s->for_overlaps & OVERLAPS_SUCC)
1962 {
1963 rs[i] = r;
1964 if (r.y < row_y + s->row->visible_height)
1965 {
1966 if (r.y + r.height > row_y + s->row->visible_height)
1967 {
1968 rs[i].y = row_y + s->row->visible_height;
1969 rs[i].height = r.y + r.height - rs[i].y;
1970 }
1971 else
1972 rs[i].height = 0;
1973 }
1974 i++;
1975 }
1976
1977 n = i;
1978 #ifdef CONVERT_FROM_XRECT
1979 for (i = 0; i < n; i++)
1980 CONVERT_FROM_XRECT (rs[i], rects[i]);
1981 #endif
1982 return n;
1983 }
1984 }
1985
1986 /* EXPORT:
1987 Return in *NR the clipping rectangle for glyph string S. */
1988
1989 void
1990 get_glyph_string_clip_rect (s, nr)
1991 struct glyph_string *s;
1992 NativeRectangle *nr;
1993 {
1994 get_glyph_string_clip_rects (s, nr, 1);
1995 }
1996
1997
1998 /* EXPORT:
1999 Return the position and height of the phys cursor in window W.
2000 Set w->phys_cursor_width to width of phys cursor.
2001 */
2002
2003 void
2004 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2005 struct window *w;
2006 struct glyph_row *row;
2007 struct glyph *glyph;
2008 int *xp, *yp, *heightp;
2009 {
2010 struct frame *f = XFRAME (WINDOW_FRAME (w));
2011 int x, y, wd, h, h0, y0;
2012
2013 /* Compute the width of the rectangle to draw. If on a stretch
2014 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2015 rectangle as wide as the glyph, but use a canonical character
2016 width instead. */
2017 wd = glyph->pixel_width - 1;
2018 #ifdef HAVE_NTGUI
2019 wd++; /* Why? */
2020 #endif
2021
2022 x = w->phys_cursor.x;
2023 if (x < 0)
2024 {
2025 wd += x;
2026 x = 0;
2027 }
2028
2029 if (glyph->type == STRETCH_GLYPH
2030 && !x_stretch_cursor_p)
2031 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2032 w->phys_cursor_width = wd;
2033
2034 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2035
2036 /* If y is below window bottom, ensure that we still see a cursor. */
2037 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2038
2039 h = max (h0, glyph->ascent + glyph->descent);
2040 h0 = min (h0, glyph->ascent + glyph->descent);
2041
2042 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2043 if (y < y0)
2044 {
2045 h = max (h - (y0 - y) + 1, h0);
2046 y = y0 - 1;
2047 }
2048 else
2049 {
2050 y0 = window_text_bottom_y (w) - h0;
2051 if (y > y0)
2052 {
2053 h += y - y0;
2054 y = y0;
2055 }
2056 }
2057
2058 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2059 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2060 *heightp = h;
2061 }
2062
2063 /*
2064 * Remember which glyph the mouse is over.
2065 */
2066
2067 void
2068 remember_mouse_glyph (f, gx, gy, rect)
2069 struct frame *f;
2070 int gx, gy;
2071 NativeRectangle *rect;
2072 {
2073 Lisp_Object window;
2074 struct window *w;
2075 struct glyph_row *r, *gr, *end_row;
2076 enum window_part part;
2077 enum glyph_row_area area;
2078 int x, y, width, height;
2079
2080 /* Try to determine frame pixel position and size of the glyph under
2081 frame pixel coordinates X/Y on frame F. */
2082
2083 if (!f->glyphs_initialized_p
2084 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2085 NILP (window)))
2086 {
2087 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2088 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2089 goto virtual_glyph;
2090 }
2091
2092 w = XWINDOW (window);
2093 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2094 height = WINDOW_FRAME_LINE_HEIGHT (w);
2095
2096 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2097 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2098
2099 if (w->pseudo_window_p)
2100 {
2101 area = TEXT_AREA;
2102 part = ON_MODE_LINE; /* Don't adjust margin. */
2103 goto text_glyph;
2104 }
2105
2106 switch (part)
2107 {
2108 case ON_LEFT_MARGIN:
2109 area = LEFT_MARGIN_AREA;
2110 goto text_glyph;
2111
2112 case ON_RIGHT_MARGIN:
2113 area = RIGHT_MARGIN_AREA;
2114 goto text_glyph;
2115
2116 case ON_HEADER_LINE:
2117 case ON_MODE_LINE:
2118 gr = (part == ON_HEADER_LINE
2119 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2120 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2121 gy = gr->y;
2122 area = TEXT_AREA;
2123 goto text_glyph_row_found;
2124
2125 case ON_TEXT:
2126 area = TEXT_AREA;
2127
2128 text_glyph:
2129 gr = 0; gy = 0;
2130 for (; r <= end_row && r->enabled_p; ++r)
2131 if (r->y + r->height > y)
2132 {
2133 gr = r; gy = r->y;
2134 break;
2135 }
2136
2137 text_glyph_row_found:
2138 if (gr && gy <= y)
2139 {
2140 struct glyph *g = gr->glyphs[area];
2141 struct glyph *end = g + gr->used[area];
2142
2143 height = gr->height;
2144 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2145 if (gx + g->pixel_width > x)
2146 break;
2147
2148 if (g < end)
2149 {
2150 if (g->type == IMAGE_GLYPH)
2151 {
2152 /* Don't remember when mouse is over image, as
2153 image may have hot-spots. */
2154 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2155 return;
2156 }
2157 width = g->pixel_width;
2158 }
2159 else
2160 {
2161 /* Use nominal char spacing at end of line. */
2162 x -= gx;
2163 gx += (x / width) * width;
2164 }
2165
2166 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2167 gx += window_box_left_offset (w, area);
2168 }
2169 else
2170 {
2171 /* Use nominal line height at end of window. */
2172 gx = (x / width) * width;
2173 y -= gy;
2174 gy += (y / height) * height;
2175 }
2176 break;
2177
2178 case ON_LEFT_FRINGE:
2179 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2180 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2181 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2182 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2183 goto row_glyph;
2184
2185 case ON_RIGHT_FRINGE:
2186 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2187 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2188 : window_box_right_offset (w, TEXT_AREA));
2189 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2190 goto row_glyph;
2191
2192 case ON_SCROLL_BAR:
2193 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2194 ? 0
2195 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2196 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2197 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2198 : 0)));
2199 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2200
2201 row_glyph:
2202 gr = 0, gy = 0;
2203 for (; r <= end_row && r->enabled_p; ++r)
2204 if (r->y + r->height > y)
2205 {
2206 gr = r; gy = r->y;
2207 break;
2208 }
2209
2210 if (gr && gy <= y)
2211 height = gr->height;
2212 else
2213 {
2214 /* Use nominal line height at end of window. */
2215 y -= gy;
2216 gy += (y / height) * height;
2217 }
2218 break;
2219
2220 default:
2221 ;
2222 virtual_glyph:
2223 /* If there is no glyph under the mouse, then we divide the screen
2224 into a grid of the smallest glyph in the frame, and use that
2225 as our "glyph". */
2226
2227 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2228 round down even for negative values. */
2229 if (gx < 0)
2230 gx -= width - 1;
2231 if (gy < 0)
2232 gy -= height - 1;
2233
2234 gx = (gx / width) * width;
2235 gy = (gy / height) * height;
2236
2237 goto store_rect;
2238 }
2239
2240 gx += WINDOW_LEFT_EDGE_X (w);
2241 gy += WINDOW_TOP_EDGE_Y (w);
2242
2243 store_rect:
2244 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2245
2246 /* Visible feedback for debugging. */
2247 #if 0
2248 #if HAVE_X_WINDOWS
2249 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2250 f->output_data.x->normal_gc,
2251 gx, gy, width, height);
2252 #endif
2253 #endif
2254 }
2255
2256
2257 #endif /* HAVE_WINDOW_SYSTEM */
2258
2259 \f
2260 /***********************************************************************
2261 Lisp form evaluation
2262 ***********************************************************************/
2263
2264 /* Error handler for safe_eval and safe_call. */
2265
2266 static Lisp_Object
2267 safe_eval_handler (arg)
2268 Lisp_Object arg;
2269 {
2270 add_to_log ("Error during redisplay: %s", arg, Qnil);
2271 return Qnil;
2272 }
2273
2274
2275 /* Evaluate SEXPR and return the result, or nil if something went
2276 wrong. Prevent redisplay during the evaluation. */
2277
2278 Lisp_Object
2279 safe_eval (sexpr)
2280 Lisp_Object sexpr;
2281 {
2282 Lisp_Object val;
2283
2284 if (inhibit_eval_during_redisplay)
2285 val = Qnil;
2286 else
2287 {
2288 int count = SPECPDL_INDEX ();
2289 struct gcpro gcpro1;
2290
2291 GCPRO1 (sexpr);
2292 specbind (Qinhibit_redisplay, Qt);
2293 /* Use Qt to ensure debugger does not run,
2294 so there is no possibility of wanting to redisplay. */
2295 val = internal_condition_case_1 (Feval, sexpr, Qt,
2296 safe_eval_handler);
2297 UNGCPRO;
2298 val = unbind_to (count, val);
2299 }
2300
2301 return val;
2302 }
2303
2304
2305 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2306 Return the result, or nil if something went wrong. Prevent
2307 redisplay during the evaluation. */
2308
2309 Lisp_Object
2310 safe_call (nargs, args)
2311 int nargs;
2312 Lisp_Object *args;
2313 {
2314 Lisp_Object val;
2315
2316 if (inhibit_eval_during_redisplay)
2317 val = Qnil;
2318 else
2319 {
2320 int count = SPECPDL_INDEX ();
2321 struct gcpro gcpro1;
2322
2323 GCPRO1 (args[0]);
2324 gcpro1.nvars = nargs;
2325 specbind (Qinhibit_redisplay, Qt);
2326 /* Use Qt to ensure debugger does not run,
2327 so there is no possibility of wanting to redisplay. */
2328 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2329 safe_eval_handler);
2330 UNGCPRO;
2331 val = unbind_to (count, val);
2332 }
2333
2334 return val;
2335 }
2336
2337
2338 /* Call function FN with one argument ARG.
2339 Return the result, or nil if something went wrong. */
2340
2341 Lisp_Object
2342 safe_call1 (fn, arg)
2343 Lisp_Object fn, arg;
2344 {
2345 Lisp_Object args[2];
2346 args[0] = fn;
2347 args[1] = arg;
2348 return safe_call (2, args);
2349 }
2350
2351
2352 \f
2353 /***********************************************************************
2354 Debugging
2355 ***********************************************************************/
2356
2357 #if 0
2358
2359 /* Define CHECK_IT to perform sanity checks on iterators.
2360 This is for debugging. It is too slow to do unconditionally. */
2361
2362 static void
2363 check_it (it)
2364 struct it *it;
2365 {
2366 if (it->method == GET_FROM_STRING)
2367 {
2368 xassert (STRINGP (it->string));
2369 xassert (IT_STRING_CHARPOS (*it) >= 0);
2370 }
2371 else
2372 {
2373 xassert (IT_STRING_CHARPOS (*it) < 0);
2374 if (it->method == GET_FROM_BUFFER)
2375 {
2376 /* Check that character and byte positions agree. */
2377 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2378 }
2379 }
2380
2381 if (it->dpvec)
2382 xassert (it->current.dpvec_index >= 0);
2383 else
2384 xassert (it->current.dpvec_index < 0);
2385 }
2386
2387 #define CHECK_IT(IT) check_it ((IT))
2388
2389 #else /* not 0 */
2390
2391 #define CHECK_IT(IT) (void) 0
2392
2393 #endif /* not 0 */
2394
2395
2396 #if GLYPH_DEBUG
2397
2398 /* Check that the window end of window W is what we expect it
2399 to be---the last row in the current matrix displaying text. */
2400
2401 static void
2402 check_window_end (w)
2403 struct window *w;
2404 {
2405 if (!MINI_WINDOW_P (w)
2406 && !NILP (w->window_end_valid))
2407 {
2408 struct glyph_row *row;
2409 xassert ((row = MATRIX_ROW (w->current_matrix,
2410 XFASTINT (w->window_end_vpos)),
2411 !row->enabled_p
2412 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2413 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2414 }
2415 }
2416
2417 #define CHECK_WINDOW_END(W) check_window_end ((W))
2418
2419 #else /* not GLYPH_DEBUG */
2420
2421 #define CHECK_WINDOW_END(W) (void) 0
2422
2423 #endif /* not GLYPH_DEBUG */
2424
2425
2426 \f
2427 /***********************************************************************
2428 Iterator initialization
2429 ***********************************************************************/
2430
2431 /* Initialize IT for displaying current_buffer in window W, starting
2432 at character position CHARPOS. CHARPOS < 0 means that no buffer
2433 position is specified which is useful when the iterator is assigned
2434 a position later. BYTEPOS is the byte position corresponding to
2435 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2436
2437 If ROW is not null, calls to produce_glyphs with IT as parameter
2438 will produce glyphs in that row.
2439
2440 BASE_FACE_ID is the id of a base face to use. It must be one of
2441 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2442 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2443 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2444
2445 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2446 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2447 will be initialized to use the corresponding mode line glyph row of
2448 the desired matrix of W. */
2449
2450 void
2451 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2452 struct it *it;
2453 struct window *w;
2454 int charpos, bytepos;
2455 struct glyph_row *row;
2456 enum face_id base_face_id;
2457 {
2458 int highlight_region_p;
2459
2460 /* Some precondition checks. */
2461 xassert (w != NULL && it != NULL);
2462 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2463 && charpos <= ZV));
2464
2465 /* If face attributes have been changed since the last redisplay,
2466 free realized faces now because they depend on face definitions
2467 that might have changed. Don't free faces while there might be
2468 desired matrices pending which reference these faces. */
2469 if (face_change_count && !inhibit_free_realized_faces)
2470 {
2471 face_change_count = 0;
2472 free_all_realized_faces (Qnil);
2473 }
2474
2475 /* Use one of the mode line rows of W's desired matrix if
2476 appropriate. */
2477 if (row == NULL)
2478 {
2479 if (base_face_id == MODE_LINE_FACE_ID
2480 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2481 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2482 else if (base_face_id == HEADER_LINE_FACE_ID)
2483 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2484 }
2485
2486 /* Clear IT. */
2487 bzero (it, sizeof *it);
2488 it->current.overlay_string_index = -1;
2489 it->current.dpvec_index = -1;
2490 it->base_face_id = base_face_id;
2491 it->string = Qnil;
2492 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2493
2494 /* The window in which we iterate over current_buffer: */
2495 XSETWINDOW (it->window, w);
2496 it->w = w;
2497 it->f = XFRAME (w->frame);
2498
2499 /* Extra space between lines (on window systems only). */
2500 if (base_face_id == DEFAULT_FACE_ID
2501 && FRAME_WINDOW_P (it->f))
2502 {
2503 if (NATNUMP (current_buffer->extra_line_spacing))
2504 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2505 else if (FLOATP (current_buffer->extra_line_spacing))
2506 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2507 * FRAME_LINE_HEIGHT (it->f));
2508 else if (it->f->extra_line_spacing > 0)
2509 it->extra_line_spacing = it->f->extra_line_spacing;
2510 it->max_extra_line_spacing = 0;
2511 }
2512
2513 /* If realized faces have been removed, e.g. because of face
2514 attribute changes of named faces, recompute them. When running
2515 in batch mode, the face cache of the initial frame is null. If
2516 we happen to get called, make a dummy face cache. */
2517 if (FRAME_FACE_CACHE (it->f) == NULL)
2518 init_frame_faces (it->f);
2519 if (FRAME_FACE_CACHE (it->f)->used == 0)
2520 recompute_basic_faces (it->f);
2521
2522 /* Current value of the `slice', `space-width', and 'height' properties. */
2523 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2524 it->space_width = Qnil;
2525 it->font_height = Qnil;
2526 it->override_ascent = -1;
2527
2528 /* Are control characters displayed as `^C'? */
2529 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2530
2531 /* -1 means everything between a CR and the following line end
2532 is invisible. >0 means lines indented more than this value are
2533 invisible. */
2534 it->selective = (INTEGERP (current_buffer->selective_display)
2535 ? XFASTINT (current_buffer->selective_display)
2536 : (!NILP (current_buffer->selective_display)
2537 ? -1 : 0));
2538 it->selective_display_ellipsis_p
2539 = !NILP (current_buffer->selective_display_ellipses);
2540
2541 /* Display table to use. */
2542 it->dp = window_display_table (w);
2543
2544 /* Are multibyte characters enabled in current_buffer? */
2545 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2546
2547 /* Non-zero if we should highlight the region. */
2548 highlight_region_p
2549 = (!NILP (Vtransient_mark_mode)
2550 && !NILP (current_buffer->mark_active)
2551 && XMARKER (current_buffer->mark)->buffer != 0);
2552
2553 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2554 start and end of a visible region in window IT->w. Set both to
2555 -1 to indicate no region. */
2556 if (highlight_region_p
2557 /* Maybe highlight only in selected window. */
2558 && (/* Either show region everywhere. */
2559 highlight_nonselected_windows
2560 /* Or show region in the selected window. */
2561 || w == XWINDOW (selected_window)
2562 /* Or show the region if we are in the mini-buffer and W is
2563 the window the mini-buffer refers to. */
2564 || (MINI_WINDOW_P (XWINDOW (selected_window))
2565 && WINDOWP (minibuf_selected_window)
2566 && w == XWINDOW (minibuf_selected_window))))
2567 {
2568 int charpos = marker_position (current_buffer->mark);
2569 it->region_beg_charpos = min (PT, charpos);
2570 it->region_end_charpos = max (PT, charpos);
2571 }
2572 else
2573 it->region_beg_charpos = it->region_end_charpos = -1;
2574
2575 /* Get the position at which the redisplay_end_trigger hook should
2576 be run, if it is to be run at all. */
2577 if (MARKERP (w->redisplay_end_trigger)
2578 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2579 it->redisplay_end_trigger_charpos
2580 = marker_position (w->redisplay_end_trigger);
2581 else if (INTEGERP (w->redisplay_end_trigger))
2582 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2583
2584 /* Correct bogus values of tab_width. */
2585 it->tab_width = XINT (current_buffer->tab_width);
2586 if (it->tab_width <= 0 || it->tab_width > 1000)
2587 it->tab_width = 8;
2588
2589 /* Are lines in the display truncated? */
2590 it->truncate_lines_p
2591 = (base_face_id != DEFAULT_FACE_ID
2592 || XINT (it->w->hscroll)
2593 || (truncate_partial_width_windows
2594 && !WINDOW_FULL_WIDTH_P (it->w))
2595 || !NILP (current_buffer->truncate_lines));
2596
2597 /* Get dimensions of truncation and continuation glyphs. These are
2598 displayed as fringe bitmaps under X, so we don't need them for such
2599 frames. */
2600 if (!FRAME_WINDOW_P (it->f))
2601 {
2602 if (it->truncate_lines_p)
2603 {
2604 /* We will need the truncation glyph. */
2605 xassert (it->glyph_row == NULL);
2606 produce_special_glyphs (it, IT_TRUNCATION);
2607 it->truncation_pixel_width = it->pixel_width;
2608 }
2609 else
2610 {
2611 /* We will need the continuation glyph. */
2612 xassert (it->glyph_row == NULL);
2613 produce_special_glyphs (it, IT_CONTINUATION);
2614 it->continuation_pixel_width = it->pixel_width;
2615 }
2616
2617 /* Reset these values to zero because the produce_special_glyphs
2618 above has changed them. */
2619 it->pixel_width = it->ascent = it->descent = 0;
2620 it->phys_ascent = it->phys_descent = 0;
2621 }
2622
2623 /* Set this after getting the dimensions of truncation and
2624 continuation glyphs, so that we don't produce glyphs when calling
2625 produce_special_glyphs, above. */
2626 it->glyph_row = row;
2627 it->area = TEXT_AREA;
2628
2629 /* Get the dimensions of the display area. The display area
2630 consists of the visible window area plus a horizontally scrolled
2631 part to the left of the window. All x-values are relative to the
2632 start of this total display area. */
2633 if (base_face_id != DEFAULT_FACE_ID)
2634 {
2635 /* Mode lines, menu bar in terminal frames. */
2636 it->first_visible_x = 0;
2637 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2638 }
2639 else
2640 {
2641 it->first_visible_x
2642 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2643 it->last_visible_x = (it->first_visible_x
2644 + window_box_width (w, TEXT_AREA));
2645
2646 /* If we truncate lines, leave room for the truncator glyph(s) at
2647 the right margin. Otherwise, leave room for the continuation
2648 glyph(s). Truncation and continuation glyphs are not inserted
2649 for window-based redisplay. */
2650 if (!FRAME_WINDOW_P (it->f))
2651 {
2652 if (it->truncate_lines_p)
2653 it->last_visible_x -= it->truncation_pixel_width;
2654 else
2655 it->last_visible_x -= it->continuation_pixel_width;
2656 }
2657
2658 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2659 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2660 }
2661
2662 /* Leave room for a border glyph. */
2663 if (!FRAME_WINDOW_P (it->f)
2664 && !WINDOW_RIGHTMOST_P (it->w))
2665 it->last_visible_x -= 1;
2666
2667 it->last_visible_y = window_text_bottom_y (w);
2668
2669 /* For mode lines and alike, arrange for the first glyph having a
2670 left box line if the face specifies a box. */
2671 if (base_face_id != DEFAULT_FACE_ID)
2672 {
2673 struct face *face;
2674
2675 it->face_id = base_face_id;
2676
2677 /* If we have a boxed mode line, make the first character appear
2678 with a left box line. */
2679 face = FACE_FROM_ID (it->f, base_face_id);
2680 if (face->box != FACE_NO_BOX)
2681 it->start_of_box_run_p = 1;
2682 }
2683
2684 /* If a buffer position was specified, set the iterator there,
2685 getting overlays and face properties from that position. */
2686 if (charpos >= BUF_BEG (current_buffer))
2687 {
2688 it->end_charpos = ZV;
2689 it->face_id = -1;
2690 IT_CHARPOS (*it) = charpos;
2691
2692 /* Compute byte position if not specified. */
2693 if (bytepos < charpos)
2694 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2695 else
2696 IT_BYTEPOS (*it) = bytepos;
2697
2698 it->start = it->current;
2699
2700 /* Compute faces etc. */
2701 reseat (it, it->current.pos, 1);
2702 }
2703
2704 CHECK_IT (it);
2705 }
2706
2707
2708 /* Initialize IT for the display of window W with window start POS. */
2709
2710 void
2711 start_display (it, w, pos)
2712 struct it *it;
2713 struct window *w;
2714 struct text_pos pos;
2715 {
2716 struct glyph_row *row;
2717 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2718
2719 row = w->desired_matrix->rows + first_vpos;
2720 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2721 it->first_vpos = first_vpos;
2722
2723 /* Don't reseat to previous visible line start if current start
2724 position is in a string or image. */
2725 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2726 {
2727 int start_at_line_beg_p;
2728 int first_y = it->current_y;
2729
2730 /* If window start is not at a line start, skip forward to POS to
2731 get the correct continuation lines width. */
2732 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2733 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2734 if (!start_at_line_beg_p)
2735 {
2736 int new_x;
2737
2738 reseat_at_previous_visible_line_start (it);
2739 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2740
2741 new_x = it->current_x + it->pixel_width;
2742
2743 /* If lines are continued, this line may end in the middle
2744 of a multi-glyph character (e.g. a control character
2745 displayed as \003, or in the middle of an overlay
2746 string). In this case move_it_to above will not have
2747 taken us to the start of the continuation line but to the
2748 end of the continued line. */
2749 if (it->current_x > 0
2750 && !it->truncate_lines_p /* Lines are continued. */
2751 && (/* And glyph doesn't fit on the line. */
2752 new_x > it->last_visible_x
2753 /* Or it fits exactly and we're on a window
2754 system frame. */
2755 || (new_x == it->last_visible_x
2756 && FRAME_WINDOW_P (it->f))))
2757 {
2758 if (it->current.dpvec_index >= 0
2759 || it->current.overlay_string_index >= 0)
2760 {
2761 set_iterator_to_next (it, 1);
2762 move_it_in_display_line_to (it, -1, -1, 0);
2763 }
2764
2765 it->continuation_lines_width += it->current_x;
2766 }
2767
2768 /* We're starting a new display line, not affected by the
2769 height of the continued line, so clear the appropriate
2770 fields in the iterator structure. */
2771 it->max_ascent = it->max_descent = 0;
2772 it->max_phys_ascent = it->max_phys_descent = 0;
2773
2774 it->current_y = first_y;
2775 it->vpos = 0;
2776 it->current_x = it->hpos = 0;
2777 }
2778 }
2779
2780 #if 0 /* Don't assert the following because start_display is sometimes
2781 called intentionally with a window start that is not at a
2782 line start. Please leave this code in as a comment. */
2783
2784 /* Window start should be on a line start, now. */
2785 xassert (it->continuation_lines_width
2786 || IT_CHARPOS (it) == BEGV
2787 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2788 #endif /* 0 */
2789 }
2790
2791
2792 /* Return 1 if POS is a position in ellipses displayed for invisible
2793 text. W is the window we display, for text property lookup. */
2794
2795 static int
2796 in_ellipses_for_invisible_text_p (pos, w)
2797 struct display_pos *pos;
2798 struct window *w;
2799 {
2800 Lisp_Object prop, window;
2801 int ellipses_p = 0;
2802 int charpos = CHARPOS (pos->pos);
2803
2804 /* If POS specifies a position in a display vector, this might
2805 be for an ellipsis displayed for invisible text. We won't
2806 get the iterator set up for delivering that ellipsis unless
2807 we make sure that it gets aware of the invisible text. */
2808 if (pos->dpvec_index >= 0
2809 && pos->overlay_string_index < 0
2810 && CHARPOS (pos->string_pos) < 0
2811 && charpos > BEGV
2812 && (XSETWINDOW (window, w),
2813 prop = Fget_char_property (make_number (charpos),
2814 Qinvisible, window),
2815 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2816 {
2817 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2818 window);
2819 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2820 }
2821
2822 return ellipses_p;
2823 }
2824
2825
2826 /* Initialize IT for stepping through current_buffer in window W,
2827 starting at position POS that includes overlay string and display
2828 vector/ control character translation position information. Value
2829 is zero if there are overlay strings with newlines at POS. */
2830
2831 static int
2832 init_from_display_pos (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct display_pos *pos;
2836 {
2837 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2838 int i, overlay_strings_with_newlines = 0;
2839
2840 /* If POS specifies a position in a display vector, this might
2841 be for an ellipsis displayed for invisible text. We won't
2842 get the iterator set up for delivering that ellipsis unless
2843 we make sure that it gets aware of the invisible text. */
2844 if (in_ellipses_for_invisible_text_p (pos, w))
2845 {
2846 --charpos;
2847 bytepos = 0;
2848 }
2849
2850 /* Keep in mind: the call to reseat in init_iterator skips invisible
2851 text, so we might end up at a position different from POS. This
2852 is only a problem when POS is a row start after a newline and an
2853 overlay starts there with an after-string, and the overlay has an
2854 invisible property. Since we don't skip invisible text in
2855 display_line and elsewhere immediately after consuming the
2856 newline before the row start, such a POS will not be in a string,
2857 but the call to init_iterator below will move us to the
2858 after-string. */
2859 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2860
2861 /* This only scans the current chunk -- it should scan all chunks.
2862 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2863 to 16 in 22.1 to make this a lesser problem. */
2864 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2865 {
2866 const char *s = SDATA (it->overlay_strings[i]);
2867 const char *e = s + SBYTES (it->overlay_strings[i]);
2868
2869 while (s < e && *s != '\n')
2870 ++s;
2871
2872 if (s < e)
2873 {
2874 overlay_strings_with_newlines = 1;
2875 break;
2876 }
2877 }
2878
2879 /* If position is within an overlay string, set up IT to the right
2880 overlay string. */
2881 if (pos->overlay_string_index >= 0)
2882 {
2883 int relative_index;
2884
2885 /* If the first overlay string happens to have a `display'
2886 property for an image, the iterator will be set up for that
2887 image, and we have to undo that setup first before we can
2888 correct the overlay string index. */
2889 if (it->method == GET_FROM_IMAGE)
2890 pop_it (it);
2891
2892 /* We already have the first chunk of overlay strings in
2893 IT->overlay_strings. Load more until the one for
2894 pos->overlay_string_index is in IT->overlay_strings. */
2895 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2896 {
2897 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2898 it->current.overlay_string_index = 0;
2899 while (n--)
2900 {
2901 load_overlay_strings (it, 0);
2902 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2903 }
2904 }
2905
2906 it->current.overlay_string_index = pos->overlay_string_index;
2907 relative_index = (it->current.overlay_string_index
2908 % OVERLAY_STRING_CHUNK_SIZE);
2909 it->string = it->overlay_strings[relative_index];
2910 xassert (STRINGP (it->string));
2911 it->current.string_pos = pos->string_pos;
2912 it->method = GET_FROM_STRING;
2913 }
2914
2915 #if 0 /* This is bogus because POS not having an overlay string
2916 position does not mean it's after the string. Example: A
2917 line starting with a before-string and initialization of IT
2918 to the previous row's end position. */
2919 else if (it->current.overlay_string_index >= 0)
2920 {
2921 /* If POS says we're already after an overlay string ending at
2922 POS, make sure to pop the iterator because it will be in
2923 front of that overlay string. When POS is ZV, we've thereby
2924 also ``processed'' overlay strings at ZV. */
2925 while (it->sp)
2926 pop_it (it);
2927 xassert (it->current.overlay_string_index == -1);
2928 xassert (it->method == GET_FROM_BUFFER);
2929 if (CHARPOS (pos->pos) == ZV)
2930 it->overlay_strings_at_end_processed_p = 1;
2931 }
2932 #endif /* 0 */
2933
2934 if (CHARPOS (pos->string_pos) >= 0)
2935 {
2936 /* Recorded position is not in an overlay string, but in another
2937 string. This can only be a string from a `display' property.
2938 IT should already be filled with that string. */
2939 it->current.string_pos = pos->string_pos;
2940 xassert (STRINGP (it->string));
2941 }
2942
2943 /* Restore position in display vector translations, control
2944 character translations or ellipses. */
2945 if (pos->dpvec_index >= 0)
2946 {
2947 if (it->dpvec == NULL)
2948 get_next_display_element (it);
2949 xassert (it->dpvec && it->current.dpvec_index == 0);
2950 it->current.dpvec_index = pos->dpvec_index;
2951 }
2952
2953 CHECK_IT (it);
2954 return !overlay_strings_with_newlines;
2955 }
2956
2957
2958 /* Initialize IT for stepping through current_buffer in window W
2959 starting at ROW->start. */
2960
2961 static void
2962 init_to_row_start (it, w, row)
2963 struct it *it;
2964 struct window *w;
2965 struct glyph_row *row;
2966 {
2967 init_from_display_pos (it, w, &row->start);
2968 it->start = row->start;
2969 it->continuation_lines_width = row->continuation_lines_width;
2970 CHECK_IT (it);
2971 }
2972
2973
2974 /* Initialize IT for stepping through current_buffer in window W
2975 starting in the line following ROW, i.e. starting at ROW->end.
2976 Value is zero if there are overlay strings with newlines at ROW's
2977 end position. */
2978
2979 static int
2980 init_to_row_end (it, w, row)
2981 struct it *it;
2982 struct window *w;
2983 struct glyph_row *row;
2984 {
2985 int success = 0;
2986
2987 if (init_from_display_pos (it, w, &row->end))
2988 {
2989 if (row->continued_p)
2990 it->continuation_lines_width
2991 = row->continuation_lines_width + row->pixel_width;
2992 CHECK_IT (it);
2993 success = 1;
2994 }
2995
2996 return success;
2997 }
2998
2999
3000
3001 \f
3002 /***********************************************************************
3003 Text properties
3004 ***********************************************************************/
3005
3006 /* Called when IT reaches IT->stop_charpos. Handle text property and
3007 overlay changes. Set IT->stop_charpos to the next position where
3008 to stop. */
3009
3010 static void
3011 handle_stop (it)
3012 struct it *it;
3013 {
3014 enum prop_handled handled;
3015 int handle_overlay_change_p;
3016 struct props *p;
3017
3018 it->dpvec = NULL;
3019 it->current.dpvec_index = -1;
3020 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3021 it->ignore_overlay_strings_at_pos_p = 0;
3022
3023 /* Use face of preceding text for ellipsis (if invisible) */
3024 if (it->selective_display_ellipsis_p)
3025 it->saved_face_id = it->face_id;
3026
3027 do
3028 {
3029 handled = HANDLED_NORMALLY;
3030
3031 /* Call text property handlers. */
3032 for (p = it_props; p->handler; ++p)
3033 {
3034 handled = p->handler (it);
3035
3036 if (handled == HANDLED_RECOMPUTE_PROPS)
3037 break;
3038 else if (handled == HANDLED_RETURN)
3039 {
3040 /* We still want to show before and after strings from
3041 overlays even if the actual buffer text is replaced. */
3042 if (!handle_overlay_change_p || it->sp > 1)
3043 return;
3044 if (!get_overlay_strings_1 (it, 0, 0))
3045 return;
3046 it->ignore_overlay_strings_at_pos_p = 1;
3047 it->string_from_display_prop_p = 0;
3048 handle_overlay_change_p = 0;
3049 handled = HANDLED_RECOMPUTE_PROPS;
3050 break;
3051 }
3052 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3053 handle_overlay_change_p = 0;
3054 }
3055
3056 if (handled != HANDLED_RECOMPUTE_PROPS)
3057 {
3058 /* Don't check for overlay strings below when set to deliver
3059 characters from a display vector. */
3060 if (it->method == GET_FROM_DISPLAY_VECTOR)
3061 handle_overlay_change_p = 0;
3062
3063 /* Handle overlay changes.
3064 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3065 if it finds overlays. */
3066 if (handle_overlay_change_p)
3067 handled = handle_overlay_change (it);
3068 }
3069 }
3070 while (handled == HANDLED_RECOMPUTE_PROPS);
3071
3072 /* Determine where to stop next. */
3073 if (handled == HANDLED_NORMALLY)
3074 compute_stop_pos (it);
3075 }
3076
3077
3078 /* Compute IT->stop_charpos from text property and overlay change
3079 information for IT's current position. */
3080
3081 static void
3082 compute_stop_pos (it)
3083 struct it *it;
3084 {
3085 register INTERVAL iv, next_iv;
3086 Lisp_Object object, limit, position;
3087
3088 /* If nowhere else, stop at the end. */
3089 it->stop_charpos = it->end_charpos;
3090
3091 if (STRINGP (it->string))
3092 {
3093 /* Strings are usually short, so don't limit the search for
3094 properties. */
3095 object = it->string;
3096 limit = Qnil;
3097 position = make_number (IT_STRING_CHARPOS (*it));
3098 }
3099 else
3100 {
3101 int charpos;
3102
3103 /* If next overlay change is in front of the current stop pos
3104 (which is IT->end_charpos), stop there. Note: value of
3105 next_overlay_change is point-max if no overlay change
3106 follows. */
3107 charpos = next_overlay_change (IT_CHARPOS (*it));
3108 if (charpos < it->stop_charpos)
3109 it->stop_charpos = charpos;
3110
3111 /* If showing the region, we have to stop at the region
3112 start or end because the face might change there. */
3113 if (it->region_beg_charpos > 0)
3114 {
3115 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3116 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3117 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3118 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3119 }
3120
3121 /* Set up variables for computing the stop position from text
3122 property changes. */
3123 XSETBUFFER (object, current_buffer);
3124 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3125 position = make_number (IT_CHARPOS (*it));
3126
3127 }
3128
3129 /* Get the interval containing IT's position. Value is a null
3130 interval if there isn't such an interval. */
3131 iv = validate_interval_range (object, &position, &position, 0);
3132 if (!NULL_INTERVAL_P (iv))
3133 {
3134 Lisp_Object values_here[LAST_PROP_IDX];
3135 struct props *p;
3136
3137 /* Get properties here. */
3138 for (p = it_props; p->handler; ++p)
3139 values_here[p->idx] = textget (iv->plist, *p->name);
3140
3141 /* Look for an interval following iv that has different
3142 properties. */
3143 for (next_iv = next_interval (iv);
3144 (!NULL_INTERVAL_P (next_iv)
3145 && (NILP (limit)
3146 || XFASTINT (limit) > next_iv->position));
3147 next_iv = next_interval (next_iv))
3148 {
3149 for (p = it_props; p->handler; ++p)
3150 {
3151 Lisp_Object new_value;
3152
3153 new_value = textget (next_iv->plist, *p->name);
3154 if (!EQ (values_here[p->idx], new_value))
3155 break;
3156 }
3157
3158 if (p->handler)
3159 break;
3160 }
3161
3162 if (!NULL_INTERVAL_P (next_iv))
3163 {
3164 if (INTEGERP (limit)
3165 && next_iv->position >= XFASTINT (limit))
3166 /* No text property change up to limit. */
3167 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3168 else
3169 /* Text properties change in next_iv. */
3170 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3171 }
3172 }
3173
3174 xassert (STRINGP (it->string)
3175 || (it->stop_charpos >= BEGV
3176 && it->stop_charpos >= IT_CHARPOS (*it)));
3177 }
3178
3179
3180 /* Return the position of the next overlay change after POS in
3181 current_buffer. Value is point-max if no overlay change
3182 follows. This is like `next-overlay-change' but doesn't use
3183 xmalloc. */
3184
3185 static int
3186 next_overlay_change (pos)
3187 int pos;
3188 {
3189 int noverlays;
3190 int endpos;
3191 Lisp_Object *overlays;
3192 int i;
3193
3194 /* Get all overlays at the given position. */
3195 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3196
3197 /* If any of these overlays ends before endpos,
3198 use its ending point instead. */
3199 for (i = 0; i < noverlays; ++i)
3200 {
3201 Lisp_Object oend;
3202 int oendpos;
3203
3204 oend = OVERLAY_END (overlays[i]);
3205 oendpos = OVERLAY_POSITION (oend);
3206 endpos = min (endpos, oendpos);
3207 }
3208
3209 return endpos;
3210 }
3211
3212
3213 \f
3214 /***********************************************************************
3215 Fontification
3216 ***********************************************************************/
3217
3218 /* Handle changes in the `fontified' property of the current buffer by
3219 calling hook functions from Qfontification_functions to fontify
3220 regions of text. */
3221
3222 static enum prop_handled
3223 handle_fontified_prop (it)
3224 struct it *it;
3225 {
3226 Lisp_Object prop, pos;
3227 enum prop_handled handled = HANDLED_NORMALLY;
3228
3229 if (!NILP (Vmemory_full))
3230 return handled;
3231
3232 /* Get the value of the `fontified' property at IT's current buffer
3233 position. (The `fontified' property doesn't have a special
3234 meaning in strings.) If the value is nil, call functions from
3235 Qfontification_functions. */
3236 if (!STRINGP (it->string)
3237 && it->s == NULL
3238 && !NILP (Vfontification_functions)
3239 && !NILP (Vrun_hooks)
3240 && (pos = make_number (IT_CHARPOS (*it)),
3241 prop = Fget_char_property (pos, Qfontified, Qnil),
3242 /* Ignore the special cased nil value always present at EOB since
3243 no amount of fontifying will be able to change it. */
3244 NILP (prop) && IT_CHARPOS (*it) < Z))
3245 {
3246 int count = SPECPDL_INDEX ();
3247 Lisp_Object val;
3248
3249 val = Vfontification_functions;
3250 specbind (Qfontification_functions, Qnil);
3251
3252 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3253 safe_call1 (val, pos);
3254 else
3255 {
3256 Lisp_Object globals, fn;
3257 struct gcpro gcpro1, gcpro2;
3258
3259 globals = Qnil;
3260 GCPRO2 (val, globals);
3261
3262 for (; CONSP (val); val = XCDR (val))
3263 {
3264 fn = XCAR (val);
3265
3266 if (EQ (fn, Qt))
3267 {
3268 /* A value of t indicates this hook has a local
3269 binding; it means to run the global binding too.
3270 In a global value, t should not occur. If it
3271 does, we must ignore it to avoid an endless
3272 loop. */
3273 for (globals = Fdefault_value (Qfontification_functions);
3274 CONSP (globals);
3275 globals = XCDR (globals))
3276 {
3277 fn = XCAR (globals);
3278 if (!EQ (fn, Qt))
3279 safe_call1 (fn, pos);
3280 }
3281 }
3282 else
3283 safe_call1 (fn, pos);
3284 }
3285
3286 UNGCPRO;
3287 }
3288
3289 unbind_to (count, Qnil);
3290
3291 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3292 something. This avoids an endless loop if they failed to
3293 fontify the text for which reason ever. */
3294 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3295 handled = HANDLED_RECOMPUTE_PROPS;
3296 }
3297
3298 return handled;
3299 }
3300
3301
3302 \f
3303 /***********************************************************************
3304 Faces
3305 ***********************************************************************/
3306
3307 /* Set up iterator IT from face properties at its current position.
3308 Called from handle_stop. */
3309
3310 static enum prop_handled
3311 handle_face_prop (it)
3312 struct it *it;
3313 {
3314 int new_face_id, next_stop;
3315
3316 if (!STRINGP (it->string))
3317 {
3318 new_face_id
3319 = face_at_buffer_position (it->w,
3320 IT_CHARPOS (*it),
3321 it->region_beg_charpos,
3322 it->region_end_charpos,
3323 &next_stop,
3324 (IT_CHARPOS (*it)
3325 + TEXT_PROP_DISTANCE_LIMIT),
3326 0);
3327
3328 /* Is this a start of a run of characters with box face?
3329 Caveat: this can be called for a freshly initialized
3330 iterator; face_id is -1 in this case. We know that the new
3331 face will not change until limit, i.e. if the new face has a
3332 box, all characters up to limit will have one. But, as
3333 usual, we don't know whether limit is really the end. */
3334 if (new_face_id != it->face_id)
3335 {
3336 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3337
3338 /* If new face has a box but old face has not, this is
3339 the start of a run of characters with box, i.e. it has
3340 a shadow on the left side. The value of face_id of the
3341 iterator will be -1 if this is the initial call that gets
3342 the face. In this case, we have to look in front of IT's
3343 position and see whether there is a face != new_face_id. */
3344 it->start_of_box_run_p
3345 = (new_face->box != FACE_NO_BOX
3346 && (it->face_id >= 0
3347 || IT_CHARPOS (*it) == BEG
3348 || new_face_id != face_before_it_pos (it)));
3349 it->face_box_p = new_face->box != FACE_NO_BOX;
3350 }
3351 }
3352 else
3353 {
3354 int base_face_id, bufpos;
3355 int i;
3356 Lisp_Object from_overlay
3357 = (it->current.overlay_string_index >= 0
3358 ? it->string_overlays[it->current.overlay_string_index]
3359 : Qnil);
3360
3361 /* See if we got to this string directly or indirectly from
3362 an overlay property. That includes the before-string or
3363 after-string of an overlay, strings in display properties
3364 provided by an overlay, their text properties, etc.
3365
3366 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3367 if (! NILP (from_overlay))
3368 for (i = it->sp - 1; i >= 0; i--)
3369 {
3370 if (it->stack[i].current.overlay_string_index >= 0)
3371 from_overlay
3372 = it->string_overlays[it->stack[i].current.overlay_string_index];
3373 else if (! NILP (it->stack[i].from_overlay))
3374 from_overlay = it->stack[i].from_overlay;
3375
3376 if (!NILP (from_overlay))
3377 break;
3378 }
3379
3380 if (! NILP (from_overlay))
3381 {
3382 bufpos = IT_CHARPOS (*it);
3383 /* For a string from an overlay, the base face depends
3384 only on text properties and ignores overlays. */
3385 base_face_id
3386 = face_for_overlay_string (it->w,
3387 IT_CHARPOS (*it),
3388 it->region_beg_charpos,
3389 it->region_end_charpos,
3390 &next_stop,
3391 (IT_CHARPOS (*it)
3392 + TEXT_PROP_DISTANCE_LIMIT),
3393 0,
3394 from_overlay);
3395 }
3396 else
3397 {
3398 bufpos = 0;
3399
3400 /* For strings from a `display' property, use the face at
3401 IT's current buffer position as the base face to merge
3402 with, so that overlay strings appear in the same face as
3403 surrounding text, unless they specify their own
3404 faces. */
3405 base_face_id = underlying_face_id (it);
3406 }
3407
3408 new_face_id = face_at_string_position (it->w,
3409 it->string,
3410 IT_STRING_CHARPOS (*it),
3411 bufpos,
3412 it->region_beg_charpos,
3413 it->region_end_charpos,
3414 &next_stop,
3415 base_face_id, 0);
3416
3417 #if 0 /* This shouldn't be neccessary. Let's check it. */
3418 /* If IT is used to display a mode line we would really like to
3419 use the mode line face instead of the frame's default face. */
3420 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3421 && new_face_id == DEFAULT_FACE_ID)
3422 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3423 #endif
3424
3425 /* Is this a start of a run of characters with box? Caveat:
3426 this can be called for a freshly allocated iterator; face_id
3427 is -1 is this case. We know that the new face will not
3428 change until the next check pos, i.e. if the new face has a
3429 box, all characters up to that position will have a
3430 box. But, as usual, we don't know whether that position
3431 is really the end. */
3432 if (new_face_id != it->face_id)
3433 {
3434 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3435 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3436
3437 /* If new face has a box but old face hasn't, this is the
3438 start of a run of characters with box, i.e. it has a
3439 shadow on the left side. */
3440 it->start_of_box_run_p
3441 = new_face->box && (old_face == NULL || !old_face->box);
3442 it->face_box_p = new_face->box != FACE_NO_BOX;
3443 }
3444 }
3445
3446 it->face_id = new_face_id;
3447 return HANDLED_NORMALLY;
3448 }
3449
3450
3451 /* Return the ID of the face ``underlying'' IT's current position,
3452 which is in a string. If the iterator is associated with a
3453 buffer, return the face at IT's current buffer position.
3454 Otherwise, use the iterator's base_face_id. */
3455
3456 static int
3457 underlying_face_id (it)
3458 struct it *it;
3459 {
3460 int face_id = it->base_face_id, i;
3461
3462 xassert (STRINGP (it->string));
3463
3464 for (i = it->sp - 1; i >= 0; --i)
3465 if (NILP (it->stack[i].string))
3466 face_id = it->stack[i].face_id;
3467
3468 return face_id;
3469 }
3470
3471
3472 /* Compute the face one character before or after the current position
3473 of IT. BEFORE_P non-zero means get the face in front of IT's
3474 position. Value is the id of the face. */
3475
3476 static int
3477 face_before_or_after_it_pos (it, before_p)
3478 struct it *it;
3479 int before_p;
3480 {
3481 int face_id, limit;
3482 int next_check_charpos;
3483 struct text_pos pos;
3484
3485 xassert (it->s == NULL);
3486
3487 if (STRINGP (it->string))
3488 {
3489 int bufpos, base_face_id;
3490
3491 /* No face change past the end of the string (for the case
3492 we are padding with spaces). No face change before the
3493 string start. */
3494 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3495 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3496 return it->face_id;
3497
3498 /* Set pos to the position before or after IT's current position. */
3499 if (before_p)
3500 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3501 else
3502 /* For composition, we must check the character after the
3503 composition. */
3504 pos = (it->what == IT_COMPOSITION
3505 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3506 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3507
3508 if (it->current.overlay_string_index >= 0)
3509 bufpos = IT_CHARPOS (*it);
3510 else
3511 bufpos = 0;
3512
3513 base_face_id = underlying_face_id (it);
3514
3515 /* Get the face for ASCII, or unibyte. */
3516 face_id = face_at_string_position (it->w,
3517 it->string,
3518 CHARPOS (pos),
3519 bufpos,
3520 it->region_beg_charpos,
3521 it->region_end_charpos,
3522 &next_check_charpos,
3523 base_face_id, 0);
3524
3525 /* Correct the face for charsets different from ASCII. Do it
3526 for the multibyte case only. The face returned above is
3527 suitable for unibyte text if IT->string is unibyte. */
3528 if (STRING_MULTIBYTE (it->string))
3529 {
3530 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3531 int rest = SBYTES (it->string) - BYTEPOS (pos);
3532 int c, len;
3533 struct face *face = FACE_FROM_ID (it->f, face_id);
3534
3535 c = string_char_and_length (p, rest, &len);
3536 face_id = FACE_FOR_CHAR (it->f, face, c);
3537 }
3538 }
3539 else
3540 {
3541 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3542 || (IT_CHARPOS (*it) <= BEGV && before_p))
3543 return it->face_id;
3544
3545 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3546 pos = it->current.pos;
3547
3548 if (before_p)
3549 DEC_TEXT_POS (pos, it->multibyte_p);
3550 else
3551 {
3552 if (it->what == IT_COMPOSITION)
3553 /* For composition, we must check the position after the
3554 composition. */
3555 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3556 else
3557 INC_TEXT_POS (pos, it->multibyte_p);
3558 }
3559
3560 /* Determine face for CHARSET_ASCII, or unibyte. */
3561 face_id = face_at_buffer_position (it->w,
3562 CHARPOS (pos),
3563 it->region_beg_charpos,
3564 it->region_end_charpos,
3565 &next_check_charpos,
3566 limit, 0);
3567
3568 /* Correct the face for charsets different from ASCII. Do it
3569 for the multibyte case only. The face returned above is
3570 suitable for unibyte text if current_buffer is unibyte. */
3571 if (it->multibyte_p)
3572 {
3573 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3574 struct face *face = FACE_FROM_ID (it->f, face_id);
3575 face_id = FACE_FOR_CHAR (it->f, face, c);
3576 }
3577 }
3578
3579 return face_id;
3580 }
3581
3582
3583 \f
3584 /***********************************************************************
3585 Invisible text
3586 ***********************************************************************/
3587
3588 /* Set up iterator IT from invisible properties at its current
3589 position. Called from handle_stop. */
3590
3591 static enum prop_handled
3592 handle_invisible_prop (it)
3593 struct it *it;
3594 {
3595 enum prop_handled handled = HANDLED_NORMALLY;
3596
3597 if (STRINGP (it->string))
3598 {
3599 extern Lisp_Object Qinvisible;
3600 Lisp_Object prop, end_charpos, limit, charpos;
3601
3602 /* Get the value of the invisible text property at the
3603 current position. Value will be nil if there is no such
3604 property. */
3605 charpos = make_number (IT_STRING_CHARPOS (*it));
3606 prop = Fget_text_property (charpos, Qinvisible, it->string);
3607
3608 if (!NILP (prop)
3609 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3610 {
3611 handled = HANDLED_RECOMPUTE_PROPS;
3612
3613 /* Get the position at which the next change of the
3614 invisible text property can be found in IT->string.
3615 Value will be nil if the property value is the same for
3616 all the rest of IT->string. */
3617 XSETINT (limit, SCHARS (it->string));
3618 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3619 it->string, limit);
3620
3621 /* Text at current position is invisible. The next
3622 change in the property is at position end_charpos.
3623 Move IT's current position to that position. */
3624 if (INTEGERP (end_charpos)
3625 && XFASTINT (end_charpos) < XFASTINT (limit))
3626 {
3627 struct text_pos old;
3628 old = it->current.string_pos;
3629 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3630 compute_string_pos (&it->current.string_pos, old, it->string);
3631 }
3632 else
3633 {
3634 /* The rest of the string is invisible. If this is an
3635 overlay string, proceed with the next overlay string
3636 or whatever comes and return a character from there. */
3637 if (it->current.overlay_string_index >= 0)
3638 {
3639 next_overlay_string (it);
3640 /* Don't check for overlay strings when we just
3641 finished processing them. */
3642 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3643 }
3644 else
3645 {
3646 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3647 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3648 }
3649 }
3650 }
3651 }
3652 else
3653 {
3654 int invis_p;
3655 EMACS_INT newpos, next_stop, start_charpos;
3656 Lisp_Object pos, prop, overlay;
3657
3658 /* First of all, is there invisible text at this position? */
3659 start_charpos = IT_CHARPOS (*it);
3660 pos = make_number (IT_CHARPOS (*it));
3661 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3662 &overlay);
3663 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3664
3665 /* If we are on invisible text, skip over it. */
3666 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3667 {
3668 /* Record whether we have to display an ellipsis for the
3669 invisible text. */
3670 int display_ellipsis_p = invis_p == 2;
3671
3672 handled = HANDLED_RECOMPUTE_PROPS;
3673
3674 /* Loop skipping over invisible text. The loop is left at
3675 ZV or with IT on the first char being visible again. */
3676 do
3677 {
3678 /* Try to skip some invisible text. Return value is the
3679 position reached which can be equal to IT's position
3680 if there is nothing invisible here. This skips both
3681 over invisible text properties and overlays with
3682 invisible property. */
3683 newpos = skip_invisible (IT_CHARPOS (*it),
3684 &next_stop, ZV, it->window);
3685
3686 /* If we skipped nothing at all we weren't at invisible
3687 text in the first place. If everything to the end of
3688 the buffer was skipped, end the loop. */
3689 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3690 invis_p = 0;
3691 else
3692 {
3693 /* We skipped some characters but not necessarily
3694 all there are. Check if we ended up on visible
3695 text. Fget_char_property returns the property of
3696 the char before the given position, i.e. if we
3697 get invis_p = 0, this means that the char at
3698 newpos is visible. */
3699 pos = make_number (newpos);
3700 prop = Fget_char_property (pos, Qinvisible, it->window);
3701 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3702 }
3703
3704 /* If we ended up on invisible text, proceed to
3705 skip starting with next_stop. */
3706 if (invis_p)
3707 IT_CHARPOS (*it) = next_stop;
3708
3709 /* If there are adjacent invisible texts, don't lose the
3710 second one's ellipsis. */
3711 if (invis_p == 2)
3712 display_ellipsis_p = 1;
3713 }
3714 while (invis_p);
3715
3716 /* The position newpos is now either ZV or on visible text. */
3717 IT_CHARPOS (*it) = newpos;
3718 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3719
3720 /* If there are before-strings at the start of invisible
3721 text, and the text is invisible because of a text
3722 property, arrange to show before-strings because 20.x did
3723 it that way. (If the text is invisible because of an
3724 overlay property instead of a text property, this is
3725 already handled in the overlay code.) */
3726 if (NILP (overlay)
3727 && get_overlay_strings (it, start_charpos))
3728 {
3729 handled = HANDLED_RECOMPUTE_PROPS;
3730 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3731 }
3732 else if (display_ellipsis_p)
3733 {
3734 /* Make sure that the glyphs of the ellipsis will get
3735 correct `charpos' values. If we would not update
3736 it->position here, the glyphs would belong to the
3737 last visible character _before_ the invisible
3738 text, which confuses `set_cursor_from_row'.
3739
3740 We use the last invisible position instead of the
3741 first because this way the cursor is always drawn on
3742 the first "." of the ellipsis, whenever PT is inside
3743 the invisible text. Otherwise the cursor would be
3744 placed _after_ the ellipsis when the point is after the
3745 first invisible character. */
3746 if (!STRINGP (it->object))
3747 {
3748 it->position.charpos = IT_CHARPOS (*it) - 1;
3749 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3750 }
3751 setup_for_ellipsis (it, 0);
3752 /* Let the ellipsis display before
3753 considering any properties of the following char.
3754 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3755 handled = HANDLED_RETURN;
3756 }
3757 }
3758 }
3759
3760 return handled;
3761 }
3762
3763
3764 /* Make iterator IT return `...' next.
3765 Replaces LEN characters from buffer. */
3766
3767 static void
3768 setup_for_ellipsis (it, len)
3769 struct it *it;
3770 int len;
3771 {
3772 /* Use the display table definition for `...'. Invalid glyphs
3773 will be handled by the method returning elements from dpvec. */
3774 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3775 {
3776 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3777 it->dpvec = v->contents;
3778 it->dpend = v->contents + v->size;
3779 }
3780 else
3781 {
3782 /* Default `...'. */
3783 it->dpvec = default_invis_vector;
3784 it->dpend = default_invis_vector + 3;
3785 }
3786
3787 it->dpvec_char_len = len;
3788 it->current.dpvec_index = 0;
3789 it->dpvec_face_id = -1;
3790
3791 /* Remember the current face id in case glyphs specify faces.
3792 IT's face is restored in set_iterator_to_next.
3793 saved_face_id was set to preceding char's face in handle_stop. */
3794 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3795 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3796
3797 it->method = GET_FROM_DISPLAY_VECTOR;
3798 it->ellipsis_p = 1;
3799 }
3800
3801
3802 \f
3803 /***********************************************************************
3804 'display' property
3805 ***********************************************************************/
3806
3807 /* Set up iterator IT from `display' property at its current position.
3808 Called from handle_stop.
3809 We return HANDLED_RETURN if some part of the display property
3810 overrides the display of the buffer text itself.
3811 Otherwise we return HANDLED_NORMALLY. */
3812
3813 static enum prop_handled
3814 handle_display_prop (it)
3815 struct it *it;
3816 {
3817 Lisp_Object prop, object, overlay;
3818 struct text_pos *position;
3819 /* Nonzero if some property replaces the display of the text itself. */
3820 int display_replaced_p = 0;
3821
3822 if (STRINGP (it->string))
3823 {
3824 object = it->string;
3825 position = &it->current.string_pos;
3826 }
3827 else
3828 {
3829 XSETWINDOW (object, it->w);
3830 position = &it->current.pos;
3831 }
3832
3833 /* Reset those iterator values set from display property values. */
3834 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3835 it->space_width = Qnil;
3836 it->font_height = Qnil;
3837 it->voffset = 0;
3838
3839 /* We don't support recursive `display' properties, i.e. string
3840 values that have a string `display' property, that have a string
3841 `display' property etc. */
3842 if (!it->string_from_display_prop_p)
3843 it->area = TEXT_AREA;
3844
3845 prop = get_char_property_and_overlay (make_number (position->charpos),
3846 Qdisplay, object, &overlay);
3847 if (NILP (prop))
3848 return HANDLED_NORMALLY;
3849 /* Now OVERLAY is the overlay that gave us this property, or nil
3850 if it was a text property. */
3851
3852 if (!STRINGP (it->string))
3853 object = it->w->buffer;
3854
3855 if (CONSP (prop)
3856 /* Simple properties. */
3857 && !EQ (XCAR (prop), Qimage)
3858 && !EQ (XCAR (prop), Qspace)
3859 && !EQ (XCAR (prop), Qwhen)
3860 && !EQ (XCAR (prop), Qslice)
3861 && !EQ (XCAR (prop), Qspace_width)
3862 && !EQ (XCAR (prop), Qheight)
3863 && !EQ (XCAR (prop), Qraise)
3864 /* Marginal area specifications. */
3865 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3866 && !EQ (XCAR (prop), Qleft_fringe)
3867 && !EQ (XCAR (prop), Qright_fringe)
3868 && !NILP (XCAR (prop)))
3869 {
3870 for (; CONSP (prop); prop = XCDR (prop))
3871 {
3872 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3873 position, display_replaced_p))
3874 {
3875 display_replaced_p = 1;
3876 /* If some text in a string is replaced, `position' no
3877 longer points to the position of `object'. */
3878 if (STRINGP (object))
3879 break;
3880 }
3881 }
3882 }
3883 else if (VECTORP (prop))
3884 {
3885 int i;
3886 for (i = 0; i < ASIZE (prop); ++i)
3887 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3888 position, display_replaced_p))
3889 {
3890 display_replaced_p = 1;
3891 /* If some text in a string is replaced, `position' no
3892 longer points to the position of `object'. */
3893 if (STRINGP (object))
3894 break;
3895 }
3896 }
3897 else
3898 {
3899 int ret = handle_single_display_spec (it, prop, object, overlay,
3900 position, 0);
3901 if (ret < 0) /* Replaced by "", i.e. nothing. */
3902 return HANDLED_RECOMPUTE_PROPS;
3903 if (ret)
3904 display_replaced_p = 1;
3905 }
3906
3907 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3908 }
3909
3910
3911 /* Value is the position of the end of the `display' property starting
3912 at START_POS in OBJECT. */
3913
3914 static struct text_pos
3915 display_prop_end (it, object, start_pos)
3916 struct it *it;
3917 Lisp_Object object;
3918 struct text_pos start_pos;
3919 {
3920 Lisp_Object end;
3921 struct text_pos end_pos;
3922
3923 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3924 Qdisplay, object, Qnil);
3925 CHARPOS (end_pos) = XFASTINT (end);
3926 if (STRINGP (object))
3927 compute_string_pos (&end_pos, start_pos, it->string);
3928 else
3929 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3930
3931 return end_pos;
3932 }
3933
3934
3935 /* Set up IT from a single `display' specification PROP. OBJECT
3936 is the object in which the `display' property was found. *POSITION
3937 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3938 means that we previously saw a display specification which already
3939 replaced text display with something else, for example an image;
3940 we ignore such properties after the first one has been processed.
3941
3942 OVERLAY is the overlay this `display' property came from,
3943 or nil if it was a text property.
3944
3945 If PROP is a `space' or `image' specification, and in some other
3946 cases too, set *POSITION to the position where the `display'
3947 property ends.
3948
3949 Value is non-zero if something was found which replaces the display
3950 of buffer or string text. Specifically, the value is -1 if that
3951 "something" is "nothing". */
3952
3953 static int
3954 handle_single_display_spec (it, spec, object, overlay, position,
3955 display_replaced_before_p)
3956 struct it *it;
3957 Lisp_Object spec;
3958 Lisp_Object object;
3959 Lisp_Object overlay;
3960 struct text_pos *position;
3961 int display_replaced_before_p;
3962 {
3963 Lisp_Object form;
3964 Lisp_Object location, value;
3965 struct text_pos start_pos, save_pos;
3966 int valid_p;
3967
3968 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3969 If the result is non-nil, use VALUE instead of SPEC. */
3970 form = Qt;
3971 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3972 {
3973 spec = XCDR (spec);
3974 if (!CONSP (spec))
3975 return 0;
3976 form = XCAR (spec);
3977 spec = XCDR (spec);
3978 }
3979
3980 if (!NILP (form) && !EQ (form, Qt))
3981 {
3982 int count = SPECPDL_INDEX ();
3983 struct gcpro gcpro1;
3984
3985 /* Bind `object' to the object having the `display' property, a
3986 buffer or string. Bind `position' to the position in the
3987 object where the property was found, and `buffer-position'
3988 to the current position in the buffer. */
3989 specbind (Qobject, object);
3990 specbind (Qposition, make_number (CHARPOS (*position)));
3991 specbind (Qbuffer_position,
3992 make_number (STRINGP (object)
3993 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3994 GCPRO1 (form);
3995 form = safe_eval (form);
3996 UNGCPRO;
3997 unbind_to (count, Qnil);
3998 }
3999
4000 if (NILP (form))
4001 return 0;
4002
4003 /* Handle `(height HEIGHT)' specifications. */
4004 if (CONSP (spec)
4005 && EQ (XCAR (spec), Qheight)
4006 && CONSP (XCDR (spec)))
4007 {
4008 if (!FRAME_WINDOW_P (it->f))
4009 return 0;
4010
4011 it->font_height = XCAR (XCDR (spec));
4012 if (!NILP (it->font_height))
4013 {
4014 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4015 int new_height = -1;
4016
4017 if (CONSP (it->font_height)
4018 && (EQ (XCAR (it->font_height), Qplus)
4019 || EQ (XCAR (it->font_height), Qminus))
4020 && CONSP (XCDR (it->font_height))
4021 && INTEGERP (XCAR (XCDR (it->font_height))))
4022 {
4023 /* `(+ N)' or `(- N)' where N is an integer. */
4024 int steps = XINT (XCAR (XCDR (it->font_height)));
4025 if (EQ (XCAR (it->font_height), Qplus))
4026 steps = - steps;
4027 it->face_id = smaller_face (it->f, it->face_id, steps);
4028 }
4029 else if (FUNCTIONP (it->font_height))
4030 {
4031 /* Call function with current height as argument.
4032 Value is the new height. */
4033 Lisp_Object height;
4034 height = safe_call1 (it->font_height,
4035 face->lface[LFACE_HEIGHT_INDEX]);
4036 if (NUMBERP (height))
4037 new_height = XFLOATINT (height);
4038 }
4039 else if (NUMBERP (it->font_height))
4040 {
4041 /* Value is a multiple of the canonical char height. */
4042 struct face *face;
4043
4044 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4045 new_height = (XFLOATINT (it->font_height)
4046 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4047 }
4048 else
4049 {
4050 /* Evaluate IT->font_height with `height' bound to the
4051 current specified height to get the new height. */
4052 int count = SPECPDL_INDEX ();
4053
4054 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4055 value = safe_eval (it->font_height);
4056 unbind_to (count, Qnil);
4057
4058 if (NUMBERP (value))
4059 new_height = XFLOATINT (value);
4060 }
4061
4062 if (new_height > 0)
4063 it->face_id = face_with_height (it->f, it->face_id, new_height);
4064 }
4065
4066 return 0;
4067 }
4068
4069 /* Handle `(space-width WIDTH)'. */
4070 if (CONSP (spec)
4071 && EQ (XCAR (spec), Qspace_width)
4072 && CONSP (XCDR (spec)))
4073 {
4074 if (!FRAME_WINDOW_P (it->f))
4075 return 0;
4076
4077 value = XCAR (XCDR (spec));
4078 if (NUMBERP (value) && XFLOATINT (value) > 0)
4079 it->space_width = value;
4080
4081 return 0;
4082 }
4083
4084 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4085 if (CONSP (spec)
4086 && EQ (XCAR (spec), Qslice))
4087 {
4088 Lisp_Object tem;
4089
4090 if (!FRAME_WINDOW_P (it->f))
4091 return 0;
4092
4093 if (tem = XCDR (spec), CONSP (tem))
4094 {
4095 it->slice.x = XCAR (tem);
4096 if (tem = XCDR (tem), CONSP (tem))
4097 {
4098 it->slice.y = XCAR (tem);
4099 if (tem = XCDR (tem), CONSP (tem))
4100 {
4101 it->slice.width = XCAR (tem);
4102 if (tem = XCDR (tem), CONSP (tem))
4103 it->slice.height = XCAR (tem);
4104 }
4105 }
4106 }
4107
4108 return 0;
4109 }
4110
4111 /* Handle `(raise FACTOR)'. */
4112 if (CONSP (spec)
4113 && EQ (XCAR (spec), Qraise)
4114 && CONSP (XCDR (spec)))
4115 {
4116 if (!FRAME_WINDOW_P (it->f))
4117 return 0;
4118
4119 #ifdef HAVE_WINDOW_SYSTEM
4120 value = XCAR (XCDR (spec));
4121 if (NUMBERP (value))
4122 {
4123 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4124 it->voffset = - (XFLOATINT (value)
4125 * (FONT_HEIGHT (face->font)));
4126 }
4127 #endif /* HAVE_WINDOW_SYSTEM */
4128
4129 return 0;
4130 }
4131
4132 /* Don't handle the other kinds of display specifications
4133 inside a string that we got from a `display' property. */
4134 if (it->string_from_display_prop_p)
4135 return 0;
4136
4137 /* Characters having this form of property are not displayed, so
4138 we have to find the end of the property. */
4139 start_pos = *position;
4140 *position = display_prop_end (it, object, start_pos);
4141 value = Qnil;
4142
4143 /* Stop the scan at that end position--we assume that all
4144 text properties change there. */
4145 it->stop_charpos = position->charpos;
4146
4147 /* Handle `(left-fringe BITMAP [FACE])'
4148 and `(right-fringe BITMAP [FACE])'. */
4149 if (CONSP (spec)
4150 && (EQ (XCAR (spec), Qleft_fringe)
4151 || EQ (XCAR (spec), Qright_fringe))
4152 && CONSP (XCDR (spec)))
4153 {
4154 int face_id = DEFAULT_FACE_ID;
4155 int fringe_bitmap;
4156
4157 if (!FRAME_WINDOW_P (it->f))
4158 /* If we return here, POSITION has been advanced
4159 across the text with this property. */
4160 return 0;
4161
4162 #ifdef HAVE_WINDOW_SYSTEM
4163 value = XCAR (XCDR (spec));
4164 if (!SYMBOLP (value)
4165 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4166 /* If we return here, POSITION has been advanced
4167 across the text with this property. */
4168 return 0;
4169
4170 if (CONSP (XCDR (XCDR (spec))))
4171 {
4172 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4173 int face_id2 = lookup_derived_face (it->f, face_name,
4174 'A', FRINGE_FACE_ID, 0);
4175 if (face_id2 >= 0)
4176 face_id = face_id2;
4177 }
4178
4179 /* Save current settings of IT so that we can restore them
4180 when we are finished with the glyph property value. */
4181
4182 save_pos = it->position;
4183 it->position = *position;
4184 push_it (it);
4185 it->position = save_pos;
4186
4187 it->area = TEXT_AREA;
4188 it->what = IT_IMAGE;
4189 it->image_id = -1; /* no image */
4190 it->position = start_pos;
4191 it->object = NILP (object) ? it->w->buffer : object;
4192 it->method = GET_FROM_IMAGE;
4193 it->from_overlay = Qnil;
4194 it->face_id = face_id;
4195
4196 /* Say that we haven't consumed the characters with
4197 `display' property yet. The call to pop_it in
4198 set_iterator_to_next will clean this up. */
4199 *position = start_pos;
4200
4201 if (EQ (XCAR (spec), Qleft_fringe))
4202 {
4203 it->left_user_fringe_bitmap = fringe_bitmap;
4204 it->left_user_fringe_face_id = face_id;
4205 }
4206 else
4207 {
4208 it->right_user_fringe_bitmap = fringe_bitmap;
4209 it->right_user_fringe_face_id = face_id;
4210 }
4211 #endif /* HAVE_WINDOW_SYSTEM */
4212 return 1;
4213 }
4214
4215 /* Prepare to handle `((margin left-margin) ...)',
4216 `((margin right-margin) ...)' and `((margin nil) ...)'
4217 prefixes for display specifications. */
4218 location = Qunbound;
4219 if (CONSP (spec) && CONSP (XCAR (spec)))
4220 {
4221 Lisp_Object tem;
4222
4223 value = XCDR (spec);
4224 if (CONSP (value))
4225 value = XCAR (value);
4226
4227 tem = XCAR (spec);
4228 if (EQ (XCAR (tem), Qmargin)
4229 && (tem = XCDR (tem),
4230 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4231 (NILP (tem)
4232 || EQ (tem, Qleft_margin)
4233 || EQ (tem, Qright_margin))))
4234 location = tem;
4235 }
4236
4237 if (EQ (location, Qunbound))
4238 {
4239 location = Qnil;
4240 value = spec;
4241 }
4242
4243 /* After this point, VALUE is the property after any
4244 margin prefix has been stripped. It must be a string,
4245 an image specification, or `(space ...)'.
4246
4247 LOCATION specifies where to display: `left-margin',
4248 `right-margin' or nil. */
4249
4250 valid_p = (STRINGP (value)
4251 #ifdef HAVE_WINDOW_SYSTEM
4252 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4253 #endif /* not HAVE_WINDOW_SYSTEM */
4254 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4255
4256 if (valid_p && !display_replaced_before_p)
4257 {
4258 /* Save current settings of IT so that we can restore them
4259 when we are finished with the glyph property value. */
4260 save_pos = it->position;
4261 it->position = *position;
4262 push_it (it);
4263 it->position = save_pos;
4264 it->from_overlay = overlay;
4265
4266 if (NILP (location))
4267 it->area = TEXT_AREA;
4268 else if (EQ (location, Qleft_margin))
4269 it->area = LEFT_MARGIN_AREA;
4270 else
4271 it->area = RIGHT_MARGIN_AREA;
4272
4273 if (STRINGP (value))
4274 {
4275 if (SCHARS (value) == 0)
4276 {
4277 pop_it (it);
4278 return -1; /* Replaced by "", i.e. nothing. */
4279 }
4280 it->string = value;
4281 it->multibyte_p = STRING_MULTIBYTE (it->string);
4282 it->current.overlay_string_index = -1;
4283 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4284 it->end_charpos = it->string_nchars = SCHARS (it->string);
4285 it->method = GET_FROM_STRING;
4286 it->stop_charpos = 0;
4287 it->string_from_display_prop_p = 1;
4288 /* Say that we haven't consumed the characters with
4289 `display' property yet. The call to pop_it in
4290 set_iterator_to_next will clean this up. */
4291 if (BUFFERP (object))
4292 it->current.pos = start_pos;
4293 }
4294 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4295 {
4296 it->method = GET_FROM_STRETCH;
4297 it->object = value;
4298 it->position = start_pos;
4299 if (BUFFERP (object))
4300 it->current.pos = start_pos;
4301 }
4302 #ifdef HAVE_WINDOW_SYSTEM
4303 else
4304 {
4305 it->what = IT_IMAGE;
4306 it->image_id = lookup_image (it->f, value);
4307 it->position = start_pos;
4308 it->object = NILP (object) ? it->w->buffer : object;
4309 it->method = GET_FROM_IMAGE;
4310
4311 /* Say that we haven't consumed the characters with
4312 `display' property yet. The call to pop_it in
4313 set_iterator_to_next will clean this up. */
4314 if (BUFFERP (object))
4315 it->current.pos = start_pos;
4316 }
4317 #endif /* HAVE_WINDOW_SYSTEM */
4318
4319 return 1;
4320 }
4321
4322 /* Invalid property or property not supported. Restore
4323 POSITION to what it was before. */
4324 *position = start_pos;
4325 return 0;
4326 }
4327
4328
4329 /* Check if SPEC is a display sub-property value whose text should be
4330 treated as intangible. */
4331
4332 static int
4333 single_display_spec_intangible_p (prop)
4334 Lisp_Object prop;
4335 {
4336 /* Skip over `when FORM'. */
4337 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4338 {
4339 prop = XCDR (prop);
4340 if (!CONSP (prop))
4341 return 0;
4342 prop = XCDR (prop);
4343 }
4344
4345 if (STRINGP (prop))
4346 return 1;
4347
4348 if (!CONSP (prop))
4349 return 0;
4350
4351 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4352 we don't need to treat text as intangible. */
4353 if (EQ (XCAR (prop), Qmargin))
4354 {
4355 prop = XCDR (prop);
4356 if (!CONSP (prop))
4357 return 0;
4358
4359 prop = XCDR (prop);
4360 if (!CONSP (prop)
4361 || EQ (XCAR (prop), Qleft_margin)
4362 || EQ (XCAR (prop), Qright_margin))
4363 return 0;
4364 }
4365
4366 return (CONSP (prop)
4367 && (EQ (XCAR (prop), Qimage)
4368 || EQ (XCAR (prop), Qspace)));
4369 }
4370
4371
4372 /* Check if PROP is a display property value whose text should be
4373 treated as intangible. */
4374
4375 int
4376 display_prop_intangible_p (prop)
4377 Lisp_Object prop;
4378 {
4379 if (CONSP (prop)
4380 && CONSP (XCAR (prop))
4381 && !EQ (Qmargin, XCAR (XCAR (prop))))
4382 {
4383 /* A list of sub-properties. */
4384 while (CONSP (prop))
4385 {
4386 if (single_display_spec_intangible_p (XCAR (prop)))
4387 return 1;
4388 prop = XCDR (prop);
4389 }
4390 }
4391 else if (VECTORP (prop))
4392 {
4393 /* A vector of sub-properties. */
4394 int i;
4395 for (i = 0; i < ASIZE (prop); ++i)
4396 if (single_display_spec_intangible_p (AREF (prop, i)))
4397 return 1;
4398 }
4399 else
4400 return single_display_spec_intangible_p (prop);
4401
4402 return 0;
4403 }
4404
4405
4406 /* Return 1 if PROP is a display sub-property value containing STRING. */
4407
4408 static int
4409 single_display_spec_string_p (prop, string)
4410 Lisp_Object prop, string;
4411 {
4412 if (EQ (string, prop))
4413 return 1;
4414
4415 /* Skip over `when FORM'. */
4416 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4417 {
4418 prop = XCDR (prop);
4419 if (!CONSP (prop))
4420 return 0;
4421 prop = XCDR (prop);
4422 }
4423
4424 if (CONSP (prop))
4425 /* Skip over `margin LOCATION'. */
4426 if (EQ (XCAR (prop), Qmargin))
4427 {
4428 prop = XCDR (prop);
4429 if (!CONSP (prop))
4430 return 0;
4431
4432 prop = XCDR (prop);
4433 if (!CONSP (prop))
4434 return 0;
4435 }
4436
4437 return CONSP (prop) && EQ (XCAR (prop), string);
4438 }
4439
4440
4441 /* Return 1 if STRING appears in the `display' property PROP. */
4442
4443 static int
4444 display_prop_string_p (prop, string)
4445 Lisp_Object prop, string;
4446 {
4447 if (CONSP (prop)
4448 && CONSP (XCAR (prop))
4449 && !EQ (Qmargin, XCAR (XCAR (prop))))
4450 {
4451 /* A list of sub-properties. */
4452 while (CONSP (prop))
4453 {
4454 if (single_display_spec_string_p (XCAR (prop), string))
4455 return 1;
4456 prop = XCDR (prop);
4457 }
4458 }
4459 else if (VECTORP (prop))
4460 {
4461 /* A vector of sub-properties. */
4462 int i;
4463 for (i = 0; i < ASIZE (prop); ++i)
4464 if (single_display_spec_string_p (AREF (prop, i), string))
4465 return 1;
4466 }
4467 else
4468 return single_display_spec_string_p (prop, string);
4469
4470 return 0;
4471 }
4472
4473
4474 /* Determine from which buffer position in W's buffer STRING comes
4475 from. AROUND_CHARPOS is an approximate position where it could
4476 be from. Value is the buffer position or 0 if it couldn't be
4477 determined.
4478
4479 W's buffer must be current.
4480
4481 This function is necessary because we don't record buffer positions
4482 in glyphs generated from strings (to keep struct glyph small).
4483 This function may only use code that doesn't eval because it is
4484 called asynchronously from note_mouse_highlight. */
4485
4486 int
4487 string_buffer_position (w, string, around_charpos)
4488 struct window *w;
4489 Lisp_Object string;
4490 int around_charpos;
4491 {
4492 Lisp_Object limit, prop, pos;
4493 const int MAX_DISTANCE = 1000;
4494 int found = 0;
4495
4496 pos = make_number (around_charpos);
4497 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4498 while (!found && !EQ (pos, limit))
4499 {
4500 prop = Fget_char_property (pos, Qdisplay, Qnil);
4501 if (!NILP (prop) && display_prop_string_p (prop, string))
4502 found = 1;
4503 else
4504 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4505 }
4506
4507 if (!found)
4508 {
4509 pos = make_number (around_charpos);
4510 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4511 while (!found && !EQ (pos, limit))
4512 {
4513 prop = Fget_char_property (pos, Qdisplay, Qnil);
4514 if (!NILP (prop) && display_prop_string_p (prop, string))
4515 found = 1;
4516 else
4517 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4518 limit);
4519 }
4520 }
4521
4522 return found ? XINT (pos) : 0;
4523 }
4524
4525
4526 \f
4527 /***********************************************************************
4528 `composition' property
4529 ***********************************************************************/
4530
4531 /* Set up iterator IT from `composition' property at its current
4532 position. Called from handle_stop. */
4533
4534 static enum prop_handled
4535 handle_composition_prop (it)
4536 struct it *it;
4537 {
4538 Lisp_Object prop, string;
4539 int pos, pos_byte, end;
4540 enum prop_handled handled = HANDLED_NORMALLY;
4541
4542 if (STRINGP (it->string))
4543 {
4544 pos = IT_STRING_CHARPOS (*it);
4545 pos_byte = IT_STRING_BYTEPOS (*it);
4546 string = it->string;
4547 }
4548 else
4549 {
4550 pos = IT_CHARPOS (*it);
4551 pos_byte = IT_BYTEPOS (*it);
4552 string = Qnil;
4553 }
4554
4555 /* If there's a valid composition and point is not inside of the
4556 composition (in the case that the composition is from the current
4557 buffer), draw a glyph composed from the composition components. */
4558 if (find_composition (pos, -1, &pos, &end, &prop, string)
4559 && COMPOSITION_VALID_P (pos, end, prop)
4560 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4561 {
4562 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4563
4564 if (id >= 0)
4565 {
4566 struct composition *cmp = composition_table[id];
4567
4568 if (cmp->glyph_len == 0)
4569 {
4570 /* No glyph. */
4571 if (STRINGP (it->string))
4572 {
4573 IT_STRING_CHARPOS (*it) = end;
4574 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4575 end);
4576 }
4577 else
4578 {
4579 IT_CHARPOS (*it) = end;
4580 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4581 }
4582 return HANDLED_RECOMPUTE_PROPS;
4583 }
4584
4585 it->stop_charpos = end;
4586 push_it (it);
4587
4588 it->method = GET_FROM_COMPOSITION;
4589 it->cmp_id = id;
4590 it->cmp_len = COMPOSITION_LENGTH (prop);
4591 /* For a terminal, draw only the first character of the
4592 components. */
4593 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4594 it->len = (STRINGP (it->string)
4595 ? string_char_to_byte (it->string, end)
4596 : CHAR_TO_BYTE (end)) - pos_byte;
4597 handled = HANDLED_RETURN;
4598 }
4599 }
4600
4601 return handled;
4602 }
4603
4604
4605 \f
4606 /***********************************************************************
4607 Overlay strings
4608 ***********************************************************************/
4609
4610 /* The following structure is used to record overlay strings for
4611 later sorting in load_overlay_strings. */
4612
4613 struct overlay_entry
4614 {
4615 Lisp_Object overlay;
4616 Lisp_Object string;
4617 int priority;
4618 int after_string_p;
4619 };
4620
4621
4622 /* Set up iterator IT from overlay strings at its current position.
4623 Called from handle_stop. */
4624
4625 static enum prop_handled
4626 handle_overlay_change (it)
4627 struct it *it;
4628 {
4629 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4630 return HANDLED_RECOMPUTE_PROPS;
4631 else
4632 return HANDLED_NORMALLY;
4633 }
4634
4635
4636 /* Set up the next overlay string for delivery by IT, if there is an
4637 overlay string to deliver. Called by set_iterator_to_next when the
4638 end of the current overlay string is reached. If there are more
4639 overlay strings to display, IT->string and
4640 IT->current.overlay_string_index are set appropriately here.
4641 Otherwise IT->string is set to nil. */
4642
4643 static void
4644 next_overlay_string (it)
4645 struct it *it;
4646 {
4647 ++it->current.overlay_string_index;
4648 if (it->current.overlay_string_index == it->n_overlay_strings)
4649 {
4650 /* No more overlay strings. Restore IT's settings to what
4651 they were before overlay strings were processed, and
4652 continue to deliver from current_buffer. */
4653 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4654
4655 pop_it (it);
4656 xassert (it->sp > 0
4657 || it->method == GET_FROM_COMPOSITION
4658 || (NILP (it->string)
4659 && it->method == GET_FROM_BUFFER
4660 && it->stop_charpos >= BEGV
4661 && it->stop_charpos <= it->end_charpos));
4662 it->current.overlay_string_index = -1;
4663 it->n_overlay_strings = 0;
4664
4665 /* If we're at the end of the buffer, record that we have
4666 processed the overlay strings there already, so that
4667 next_element_from_buffer doesn't try it again. */
4668 if (IT_CHARPOS (*it) >= it->end_charpos)
4669 it->overlay_strings_at_end_processed_p = 1;
4670
4671 /* If we have to display `...' for invisible text, set
4672 the iterator up for that. */
4673 if (display_ellipsis_p)
4674 setup_for_ellipsis (it, 0);
4675 }
4676 else
4677 {
4678 /* There are more overlay strings to process. If
4679 IT->current.overlay_string_index has advanced to a position
4680 where we must load IT->overlay_strings with more strings, do
4681 it. */
4682 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4683
4684 if (it->current.overlay_string_index && i == 0)
4685 load_overlay_strings (it, 0);
4686
4687 /* Initialize IT to deliver display elements from the overlay
4688 string. */
4689 it->string = it->overlay_strings[i];
4690 it->multibyte_p = STRING_MULTIBYTE (it->string);
4691 SET_TEXT_POS (it->current.string_pos, 0, 0);
4692 it->method = GET_FROM_STRING;
4693 it->stop_charpos = 0;
4694 }
4695
4696 CHECK_IT (it);
4697 }
4698
4699
4700 /* Compare two overlay_entry structures E1 and E2. Used as a
4701 comparison function for qsort in load_overlay_strings. Overlay
4702 strings for the same position are sorted so that
4703
4704 1. All after-strings come in front of before-strings, except
4705 when they come from the same overlay.
4706
4707 2. Within after-strings, strings are sorted so that overlay strings
4708 from overlays with higher priorities come first.
4709
4710 2. Within before-strings, strings are sorted so that overlay
4711 strings from overlays with higher priorities come last.
4712
4713 Value is analogous to strcmp. */
4714
4715
4716 static int
4717 compare_overlay_entries (e1, e2)
4718 void *e1, *e2;
4719 {
4720 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4721 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4722 int result;
4723
4724 if (entry1->after_string_p != entry2->after_string_p)
4725 {
4726 /* Let after-strings appear in front of before-strings if
4727 they come from different overlays. */
4728 if (EQ (entry1->overlay, entry2->overlay))
4729 result = entry1->after_string_p ? 1 : -1;
4730 else
4731 result = entry1->after_string_p ? -1 : 1;
4732 }
4733 else if (entry1->after_string_p)
4734 /* After-strings sorted in order of decreasing priority. */
4735 result = entry2->priority - entry1->priority;
4736 else
4737 /* Before-strings sorted in order of increasing priority. */
4738 result = entry1->priority - entry2->priority;
4739
4740 return result;
4741 }
4742
4743
4744 /* Load the vector IT->overlay_strings with overlay strings from IT's
4745 current buffer position, or from CHARPOS if that is > 0. Set
4746 IT->n_overlays to the total number of overlay strings found.
4747
4748 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4749 a time. On entry into load_overlay_strings,
4750 IT->current.overlay_string_index gives the number of overlay
4751 strings that have already been loaded by previous calls to this
4752 function.
4753
4754 IT->add_overlay_start contains an additional overlay start
4755 position to consider for taking overlay strings from, if non-zero.
4756 This position comes into play when the overlay has an `invisible'
4757 property, and both before and after-strings. When we've skipped to
4758 the end of the overlay, because of its `invisible' property, we
4759 nevertheless want its before-string to appear.
4760 IT->add_overlay_start will contain the overlay start position
4761 in this case.
4762
4763 Overlay strings are sorted so that after-string strings come in
4764 front of before-string strings. Within before and after-strings,
4765 strings are sorted by overlay priority. See also function
4766 compare_overlay_entries. */
4767
4768 static void
4769 load_overlay_strings (it, charpos)
4770 struct it *it;
4771 int charpos;
4772 {
4773 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4774 Lisp_Object overlay, window, str, invisible;
4775 struct Lisp_Overlay *ov;
4776 int start, end;
4777 int size = 20;
4778 int n = 0, i, j, invis_p;
4779 struct overlay_entry *entries
4780 = (struct overlay_entry *) alloca (size * sizeof *entries);
4781
4782 if (charpos <= 0)
4783 charpos = IT_CHARPOS (*it);
4784
4785 /* Append the overlay string STRING of overlay OVERLAY to vector
4786 `entries' which has size `size' and currently contains `n'
4787 elements. AFTER_P non-zero means STRING is an after-string of
4788 OVERLAY. */
4789 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4790 do \
4791 { \
4792 Lisp_Object priority; \
4793 \
4794 if (n == size) \
4795 { \
4796 int new_size = 2 * size; \
4797 struct overlay_entry *old = entries; \
4798 entries = \
4799 (struct overlay_entry *) alloca (new_size \
4800 * sizeof *entries); \
4801 bcopy (old, entries, size * sizeof *entries); \
4802 size = new_size; \
4803 } \
4804 \
4805 entries[n].string = (STRING); \
4806 entries[n].overlay = (OVERLAY); \
4807 priority = Foverlay_get ((OVERLAY), Qpriority); \
4808 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4809 entries[n].after_string_p = (AFTER_P); \
4810 ++n; \
4811 } \
4812 while (0)
4813
4814 /* Process overlay before the overlay center. */
4815 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4816 {
4817 XSETMISC (overlay, ov);
4818 xassert (OVERLAYP (overlay));
4819 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4820 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4821
4822 if (end < charpos)
4823 break;
4824
4825 /* Skip this overlay if it doesn't start or end at IT's current
4826 position. */
4827 if (end != charpos && start != charpos)
4828 continue;
4829
4830 /* Skip this overlay if it doesn't apply to IT->w. */
4831 window = Foverlay_get (overlay, Qwindow);
4832 if (WINDOWP (window) && XWINDOW (window) != it->w)
4833 continue;
4834
4835 /* If the text ``under'' the overlay is invisible, both before-
4836 and after-strings from this overlay are visible; start and
4837 end position are indistinguishable. */
4838 invisible = Foverlay_get (overlay, Qinvisible);
4839 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4840
4841 /* If overlay has a non-empty before-string, record it. */
4842 if ((start == charpos || (end == charpos && invis_p))
4843 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4844 && SCHARS (str))
4845 RECORD_OVERLAY_STRING (overlay, str, 0);
4846
4847 /* If overlay has a non-empty after-string, record it. */
4848 if ((end == charpos || (start == charpos && invis_p))
4849 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4850 && SCHARS (str))
4851 RECORD_OVERLAY_STRING (overlay, str, 1);
4852 }
4853
4854 /* Process overlays after the overlay center. */
4855 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4856 {
4857 XSETMISC (overlay, ov);
4858 xassert (OVERLAYP (overlay));
4859 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4860 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4861
4862 if (start > charpos)
4863 break;
4864
4865 /* Skip this overlay if it doesn't start or end at IT's current
4866 position. */
4867 if (end != charpos && start != charpos)
4868 continue;
4869
4870 /* Skip this overlay if it doesn't apply to IT->w. */
4871 window = Foverlay_get (overlay, Qwindow);
4872 if (WINDOWP (window) && XWINDOW (window) != it->w)
4873 continue;
4874
4875 /* If the text ``under'' the overlay is invisible, it has a zero
4876 dimension, and both before- and after-strings apply. */
4877 invisible = Foverlay_get (overlay, Qinvisible);
4878 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4879
4880 /* If overlay has a non-empty before-string, record it. */
4881 if ((start == charpos || (end == charpos && invis_p))
4882 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4883 && SCHARS (str))
4884 RECORD_OVERLAY_STRING (overlay, str, 0);
4885
4886 /* If overlay has a non-empty after-string, record it. */
4887 if ((end == charpos || (start == charpos && invis_p))
4888 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4889 && SCHARS (str))
4890 RECORD_OVERLAY_STRING (overlay, str, 1);
4891 }
4892
4893 #undef RECORD_OVERLAY_STRING
4894
4895 /* Sort entries. */
4896 if (n > 1)
4897 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4898
4899 /* Record the total number of strings to process. */
4900 it->n_overlay_strings = n;
4901
4902 /* IT->current.overlay_string_index is the number of overlay strings
4903 that have already been consumed by IT. Copy some of the
4904 remaining overlay strings to IT->overlay_strings. */
4905 i = 0;
4906 j = it->current.overlay_string_index;
4907 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4908 {
4909 it->overlay_strings[i] = entries[j].string;
4910 it->string_overlays[i++] = entries[j++].overlay;
4911 }
4912
4913 CHECK_IT (it);
4914 }
4915
4916
4917 /* Get the first chunk of overlay strings at IT's current buffer
4918 position, or at CHARPOS if that is > 0. Value is non-zero if at
4919 least one overlay string was found. */
4920
4921 static int
4922 get_overlay_strings_1 (it, charpos, compute_stop_p)
4923 struct it *it;
4924 int charpos;
4925 {
4926 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4927 process. This fills IT->overlay_strings with strings, and sets
4928 IT->n_overlay_strings to the total number of strings to process.
4929 IT->pos.overlay_string_index has to be set temporarily to zero
4930 because load_overlay_strings needs this; it must be set to -1
4931 when no overlay strings are found because a zero value would
4932 indicate a position in the first overlay string. */
4933 it->current.overlay_string_index = 0;
4934 load_overlay_strings (it, charpos);
4935
4936 /* If we found overlay strings, set up IT to deliver display
4937 elements from the first one. Otherwise set up IT to deliver
4938 from current_buffer. */
4939 if (it->n_overlay_strings)
4940 {
4941 /* Make sure we know settings in current_buffer, so that we can
4942 restore meaningful values when we're done with the overlay
4943 strings. */
4944 if (compute_stop_p)
4945 compute_stop_pos (it);
4946 xassert (it->face_id >= 0);
4947
4948 /* Save IT's settings. They are restored after all overlay
4949 strings have been processed. */
4950 xassert (!compute_stop_p || it->sp == 0);
4951 push_it (it);
4952
4953 /* Set up IT to deliver display elements from the first overlay
4954 string. */
4955 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4956 it->string = it->overlay_strings[0];
4957 it->from_overlay = Qnil;
4958 it->stop_charpos = 0;
4959 xassert (STRINGP (it->string));
4960 it->end_charpos = SCHARS (it->string);
4961 it->multibyte_p = STRING_MULTIBYTE (it->string);
4962 it->method = GET_FROM_STRING;
4963 return 1;
4964 }
4965
4966 it->current.overlay_string_index = -1;
4967 return 0;
4968 }
4969
4970 static int
4971 get_overlay_strings (it, charpos)
4972 struct it *it;
4973 int charpos;
4974 {
4975 it->string = Qnil;
4976 it->method = GET_FROM_BUFFER;
4977
4978 (void) get_overlay_strings_1 (it, charpos, 1);
4979
4980 CHECK_IT (it);
4981
4982 /* Value is non-zero if we found at least one overlay string. */
4983 return STRINGP (it->string);
4984 }
4985
4986
4987 \f
4988 /***********************************************************************
4989 Saving and restoring state
4990 ***********************************************************************/
4991
4992 /* Save current settings of IT on IT->stack. Called, for example,
4993 before setting up IT for an overlay string, to be able to restore
4994 IT's settings to what they were after the overlay string has been
4995 processed. */
4996
4997 static void
4998 push_it (it)
4999 struct it *it;
5000 {
5001 struct iterator_stack_entry *p;
5002
5003 xassert (it->sp < IT_STACK_SIZE);
5004 p = it->stack + it->sp;
5005
5006 p->stop_charpos = it->stop_charpos;
5007 xassert (it->face_id >= 0);
5008 p->face_id = it->face_id;
5009 p->string = it->string;
5010 p->method = it->method;
5011 p->from_overlay = it->from_overlay;
5012 switch (p->method)
5013 {
5014 case GET_FROM_IMAGE:
5015 p->u.image.object = it->object;
5016 p->u.image.image_id = it->image_id;
5017 p->u.image.slice = it->slice;
5018 break;
5019 case GET_FROM_COMPOSITION:
5020 p->u.comp.object = it->object;
5021 p->u.comp.c = it->c;
5022 p->u.comp.len = it->len;
5023 p->u.comp.cmp_id = it->cmp_id;
5024 p->u.comp.cmp_len = it->cmp_len;
5025 break;
5026 case GET_FROM_STRETCH:
5027 p->u.stretch.object = it->object;
5028 break;
5029 }
5030 p->position = it->position;
5031 p->current = it->current;
5032 p->end_charpos = it->end_charpos;
5033 p->string_nchars = it->string_nchars;
5034 p->area = it->area;
5035 p->multibyte_p = it->multibyte_p;
5036 p->space_width = it->space_width;
5037 p->font_height = it->font_height;
5038 p->voffset = it->voffset;
5039 p->string_from_display_prop_p = it->string_from_display_prop_p;
5040 p->display_ellipsis_p = 0;
5041 ++it->sp;
5042 }
5043
5044
5045 /* Restore IT's settings from IT->stack. Called, for example, when no
5046 more overlay strings must be processed, and we return to delivering
5047 display elements from a buffer, or when the end of a string from a
5048 `display' property is reached and we return to delivering display
5049 elements from an overlay string, or from a buffer. */
5050
5051 static void
5052 pop_it (it)
5053 struct it *it;
5054 {
5055 struct iterator_stack_entry *p;
5056
5057 xassert (it->sp > 0);
5058 --it->sp;
5059 p = it->stack + it->sp;
5060 it->stop_charpos = p->stop_charpos;
5061 it->face_id = p->face_id;
5062 it->current = p->current;
5063 it->position = p->position;
5064 it->string = p->string;
5065 it->from_overlay = p->from_overlay;
5066 if (NILP (it->string))
5067 SET_TEXT_POS (it->current.string_pos, -1, -1);
5068 it->method = p->method;
5069 switch (it->method)
5070 {
5071 case GET_FROM_IMAGE:
5072 it->image_id = p->u.image.image_id;
5073 it->object = p->u.image.object;
5074 it->slice = p->u.image.slice;
5075 break;
5076 case GET_FROM_COMPOSITION:
5077 it->object = p->u.comp.object;
5078 it->c = p->u.comp.c;
5079 it->len = p->u.comp.len;
5080 it->cmp_id = p->u.comp.cmp_id;
5081 it->cmp_len = p->u.comp.cmp_len;
5082 break;
5083 case GET_FROM_STRETCH:
5084 it->object = p->u.comp.object;
5085 break;
5086 case GET_FROM_BUFFER:
5087 it->object = it->w->buffer;
5088 break;
5089 case GET_FROM_STRING:
5090 it->object = it->string;
5091 break;
5092 }
5093 it->end_charpos = p->end_charpos;
5094 it->string_nchars = p->string_nchars;
5095 it->area = p->area;
5096 it->multibyte_p = p->multibyte_p;
5097 it->space_width = p->space_width;
5098 it->font_height = p->font_height;
5099 it->voffset = p->voffset;
5100 it->string_from_display_prop_p = p->string_from_display_prop_p;
5101 }
5102
5103
5104 \f
5105 /***********************************************************************
5106 Moving over lines
5107 ***********************************************************************/
5108
5109 /* Set IT's current position to the previous line start. */
5110
5111 static void
5112 back_to_previous_line_start (it)
5113 struct it *it;
5114 {
5115 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5116 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5117 }
5118
5119
5120 /* Move IT to the next line start.
5121
5122 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5123 we skipped over part of the text (as opposed to moving the iterator
5124 continuously over the text). Otherwise, don't change the value
5125 of *SKIPPED_P.
5126
5127 Newlines may come from buffer text, overlay strings, or strings
5128 displayed via the `display' property. That's the reason we can't
5129 simply use find_next_newline_no_quit.
5130
5131 Note that this function may not skip over invisible text that is so
5132 because of text properties and immediately follows a newline. If
5133 it would, function reseat_at_next_visible_line_start, when called
5134 from set_iterator_to_next, would effectively make invisible
5135 characters following a newline part of the wrong glyph row, which
5136 leads to wrong cursor motion. */
5137
5138 static int
5139 forward_to_next_line_start (it, skipped_p)
5140 struct it *it;
5141 int *skipped_p;
5142 {
5143 int old_selective, newline_found_p, n;
5144 const int MAX_NEWLINE_DISTANCE = 500;
5145
5146 /* If already on a newline, just consume it to avoid unintended
5147 skipping over invisible text below. */
5148 if (it->what == IT_CHARACTER
5149 && it->c == '\n'
5150 && CHARPOS (it->position) == IT_CHARPOS (*it))
5151 {
5152 set_iterator_to_next (it, 0);
5153 it->c = 0;
5154 return 1;
5155 }
5156
5157 /* Don't handle selective display in the following. It's (a)
5158 unnecessary because it's done by the caller, and (b) leads to an
5159 infinite recursion because next_element_from_ellipsis indirectly
5160 calls this function. */
5161 old_selective = it->selective;
5162 it->selective = 0;
5163
5164 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5165 from buffer text. */
5166 for (n = newline_found_p = 0;
5167 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5168 n += STRINGP (it->string) ? 0 : 1)
5169 {
5170 if (!get_next_display_element (it))
5171 return 0;
5172 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5173 set_iterator_to_next (it, 0);
5174 }
5175
5176 /* If we didn't find a newline near enough, see if we can use a
5177 short-cut. */
5178 if (!newline_found_p)
5179 {
5180 int start = IT_CHARPOS (*it);
5181 int limit = find_next_newline_no_quit (start, 1);
5182 Lisp_Object pos;
5183
5184 xassert (!STRINGP (it->string));
5185
5186 /* If there isn't any `display' property in sight, and no
5187 overlays, we can just use the position of the newline in
5188 buffer text. */
5189 if (it->stop_charpos >= limit
5190 || ((pos = Fnext_single_property_change (make_number (start),
5191 Qdisplay,
5192 Qnil, make_number (limit)),
5193 NILP (pos))
5194 && next_overlay_change (start) == ZV))
5195 {
5196 IT_CHARPOS (*it) = limit;
5197 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5198 *skipped_p = newline_found_p = 1;
5199 }
5200 else
5201 {
5202 while (get_next_display_element (it)
5203 && !newline_found_p)
5204 {
5205 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5206 set_iterator_to_next (it, 0);
5207 }
5208 }
5209 }
5210
5211 it->selective = old_selective;
5212 return newline_found_p;
5213 }
5214
5215
5216 /* Set IT's current position to the previous visible line start. Skip
5217 invisible text that is so either due to text properties or due to
5218 selective display. Caution: this does not change IT->current_x and
5219 IT->hpos. */
5220
5221 static void
5222 back_to_previous_visible_line_start (it)
5223 struct it *it;
5224 {
5225 while (IT_CHARPOS (*it) > BEGV)
5226 {
5227 back_to_previous_line_start (it);
5228
5229 if (IT_CHARPOS (*it) <= BEGV)
5230 break;
5231
5232 /* If selective > 0, then lines indented more than that values
5233 are invisible. */
5234 if (it->selective > 0
5235 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5236 (double) it->selective)) /* iftc */
5237 continue;
5238
5239 /* Check the newline before point for invisibility. */
5240 {
5241 Lisp_Object prop;
5242 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5243 Qinvisible, it->window);
5244 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5245 continue;
5246 }
5247
5248 if (IT_CHARPOS (*it) <= BEGV)
5249 break;
5250
5251 {
5252 struct it it2;
5253 int pos;
5254 int beg, end;
5255 Lisp_Object val, overlay;
5256
5257 /* If newline is part of a composition, continue from start of composition */
5258 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5259 && beg < IT_CHARPOS (*it))
5260 goto replaced;
5261
5262 /* If newline is replaced by a display property, find start of overlay
5263 or interval and continue search from that point. */
5264 it2 = *it;
5265 pos = --IT_CHARPOS (it2);
5266 --IT_BYTEPOS (it2);
5267 it2.sp = 0;
5268 if (handle_display_prop (&it2) == HANDLED_RETURN
5269 && !NILP (val = get_char_property_and_overlay
5270 (make_number (pos), Qdisplay, Qnil, &overlay))
5271 && (OVERLAYP (overlay)
5272 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5273 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5274 goto replaced;
5275
5276 /* Newline is not replaced by anything -- so we are done. */
5277 break;
5278
5279 replaced:
5280 if (beg < BEGV)
5281 beg = BEGV;
5282 IT_CHARPOS (*it) = beg;
5283 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5284 }
5285 }
5286
5287 it->continuation_lines_width = 0;
5288
5289 xassert (IT_CHARPOS (*it) >= BEGV);
5290 xassert (IT_CHARPOS (*it) == BEGV
5291 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5292 CHECK_IT (it);
5293 }
5294
5295
5296 /* Reseat iterator IT at the previous visible line start. Skip
5297 invisible text that is so either due to text properties or due to
5298 selective display. At the end, update IT's overlay information,
5299 face information etc. */
5300
5301 void
5302 reseat_at_previous_visible_line_start (it)
5303 struct it *it;
5304 {
5305 back_to_previous_visible_line_start (it);
5306 reseat (it, it->current.pos, 1);
5307 CHECK_IT (it);
5308 }
5309
5310
5311 /* Reseat iterator IT on the next visible line start in the current
5312 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5313 preceding the line start. Skip over invisible text that is so
5314 because of selective display. Compute faces, overlays etc at the
5315 new position. Note that this function does not skip over text that
5316 is invisible because of text properties. */
5317
5318 static void
5319 reseat_at_next_visible_line_start (it, on_newline_p)
5320 struct it *it;
5321 int on_newline_p;
5322 {
5323 int newline_found_p, skipped_p = 0;
5324
5325 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5326
5327 /* Skip over lines that are invisible because they are indented
5328 more than the value of IT->selective. */
5329 if (it->selective > 0)
5330 while (IT_CHARPOS (*it) < ZV
5331 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5332 (double) it->selective)) /* iftc */
5333 {
5334 xassert (IT_BYTEPOS (*it) == BEGV
5335 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5336 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5337 }
5338
5339 /* Position on the newline if that's what's requested. */
5340 if (on_newline_p && newline_found_p)
5341 {
5342 if (STRINGP (it->string))
5343 {
5344 if (IT_STRING_CHARPOS (*it) > 0)
5345 {
5346 --IT_STRING_CHARPOS (*it);
5347 --IT_STRING_BYTEPOS (*it);
5348 }
5349 }
5350 else if (IT_CHARPOS (*it) > BEGV)
5351 {
5352 --IT_CHARPOS (*it);
5353 --IT_BYTEPOS (*it);
5354 reseat (it, it->current.pos, 0);
5355 }
5356 }
5357 else if (skipped_p)
5358 reseat (it, it->current.pos, 0);
5359
5360 CHECK_IT (it);
5361 }
5362
5363
5364 \f
5365 /***********************************************************************
5366 Changing an iterator's position
5367 ***********************************************************************/
5368
5369 /* Change IT's current position to POS in current_buffer. If FORCE_P
5370 is non-zero, always check for text properties at the new position.
5371 Otherwise, text properties are only looked up if POS >=
5372 IT->check_charpos of a property. */
5373
5374 static void
5375 reseat (it, pos, force_p)
5376 struct it *it;
5377 struct text_pos pos;
5378 int force_p;
5379 {
5380 int original_pos = IT_CHARPOS (*it);
5381
5382 reseat_1 (it, pos, 0);
5383
5384 /* Determine where to check text properties. Avoid doing it
5385 where possible because text property lookup is very expensive. */
5386 if (force_p
5387 || CHARPOS (pos) > it->stop_charpos
5388 || CHARPOS (pos) < original_pos)
5389 handle_stop (it);
5390
5391 CHECK_IT (it);
5392 }
5393
5394
5395 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5396 IT->stop_pos to POS, also. */
5397
5398 static void
5399 reseat_1 (it, pos, set_stop_p)
5400 struct it *it;
5401 struct text_pos pos;
5402 int set_stop_p;
5403 {
5404 /* Don't call this function when scanning a C string. */
5405 xassert (it->s == NULL);
5406
5407 /* POS must be a reasonable value. */
5408 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5409
5410 it->current.pos = it->position = pos;
5411 it->end_charpos = ZV;
5412 it->dpvec = NULL;
5413 it->current.dpvec_index = -1;
5414 it->current.overlay_string_index = -1;
5415 IT_STRING_CHARPOS (*it) = -1;
5416 IT_STRING_BYTEPOS (*it) = -1;
5417 it->string = Qnil;
5418 it->method = GET_FROM_BUFFER;
5419 it->object = it->w->buffer;
5420 it->area = TEXT_AREA;
5421 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5422 it->sp = 0;
5423 it->string_from_display_prop_p = 0;
5424 it->face_before_selective_p = 0;
5425
5426 if (set_stop_p)
5427 it->stop_charpos = CHARPOS (pos);
5428 }
5429
5430
5431 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5432 If S is non-null, it is a C string to iterate over. Otherwise,
5433 STRING gives a Lisp string to iterate over.
5434
5435 If PRECISION > 0, don't return more then PRECISION number of
5436 characters from the string.
5437
5438 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5439 characters have been returned. FIELD_WIDTH < 0 means an infinite
5440 field width.
5441
5442 MULTIBYTE = 0 means disable processing of multibyte characters,
5443 MULTIBYTE > 0 means enable it,
5444 MULTIBYTE < 0 means use IT->multibyte_p.
5445
5446 IT must be initialized via a prior call to init_iterator before
5447 calling this function. */
5448
5449 static void
5450 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5451 struct it *it;
5452 unsigned char *s;
5453 Lisp_Object string;
5454 int charpos;
5455 int precision, field_width, multibyte;
5456 {
5457 /* No region in strings. */
5458 it->region_beg_charpos = it->region_end_charpos = -1;
5459
5460 /* No text property checks performed by default, but see below. */
5461 it->stop_charpos = -1;
5462
5463 /* Set iterator position and end position. */
5464 bzero (&it->current, sizeof it->current);
5465 it->current.overlay_string_index = -1;
5466 it->current.dpvec_index = -1;
5467 xassert (charpos >= 0);
5468
5469 /* If STRING is specified, use its multibyteness, otherwise use the
5470 setting of MULTIBYTE, if specified. */
5471 if (multibyte >= 0)
5472 it->multibyte_p = multibyte > 0;
5473
5474 if (s == NULL)
5475 {
5476 xassert (STRINGP (string));
5477 it->string = string;
5478 it->s = NULL;
5479 it->end_charpos = it->string_nchars = SCHARS (string);
5480 it->method = GET_FROM_STRING;
5481 it->current.string_pos = string_pos (charpos, string);
5482 }
5483 else
5484 {
5485 it->s = s;
5486 it->string = Qnil;
5487
5488 /* Note that we use IT->current.pos, not it->current.string_pos,
5489 for displaying C strings. */
5490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5491 if (it->multibyte_p)
5492 {
5493 it->current.pos = c_string_pos (charpos, s, 1);
5494 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5495 }
5496 else
5497 {
5498 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5499 it->end_charpos = it->string_nchars = strlen (s);
5500 }
5501
5502 it->method = GET_FROM_C_STRING;
5503 }
5504
5505 /* PRECISION > 0 means don't return more than PRECISION characters
5506 from the string. */
5507 if (precision > 0 && it->end_charpos - charpos > precision)
5508 it->end_charpos = it->string_nchars = charpos + precision;
5509
5510 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5511 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5512 FIELD_WIDTH < 0 means infinite field width. This is useful for
5513 padding with `-' at the end of a mode line. */
5514 if (field_width < 0)
5515 field_width = INFINITY;
5516 if (field_width > it->end_charpos - charpos)
5517 it->end_charpos = charpos + field_width;
5518
5519 /* Use the standard display table for displaying strings. */
5520 if (DISP_TABLE_P (Vstandard_display_table))
5521 it->dp = XCHAR_TABLE (Vstandard_display_table);
5522
5523 it->stop_charpos = charpos;
5524 CHECK_IT (it);
5525 }
5526
5527
5528 \f
5529 /***********************************************************************
5530 Iteration
5531 ***********************************************************************/
5532
5533 /* Map enum it_method value to corresponding next_element_from_* function. */
5534
5535 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5536 {
5537 next_element_from_buffer,
5538 next_element_from_display_vector,
5539 next_element_from_composition,
5540 next_element_from_string,
5541 next_element_from_c_string,
5542 next_element_from_image,
5543 next_element_from_stretch
5544 };
5545
5546
5547 /* Load IT's display element fields with information about the next
5548 display element from the current position of IT. Value is zero if
5549 end of buffer (or C string) is reached. */
5550
5551 static struct frame *last_escape_glyph_frame = NULL;
5552 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5553 static int last_escape_glyph_merged_face_id = 0;
5554
5555 int
5556 get_next_display_element (it)
5557 struct it *it;
5558 {
5559 /* Non-zero means that we found a display element. Zero means that
5560 we hit the end of what we iterate over. Performance note: the
5561 function pointer `method' used here turns out to be faster than
5562 using a sequence of if-statements. */
5563 int success_p;
5564
5565 get_next:
5566 success_p = (*get_next_element[it->method]) (it);
5567
5568 if (it->what == IT_CHARACTER)
5569 {
5570 /* Map via display table or translate control characters.
5571 IT->c, IT->len etc. have been set to the next character by
5572 the function call above. If we have a display table, and it
5573 contains an entry for IT->c, translate it. Don't do this if
5574 IT->c itself comes from a display table, otherwise we could
5575 end up in an infinite recursion. (An alternative could be to
5576 count the recursion depth of this function and signal an
5577 error when a certain maximum depth is reached.) Is it worth
5578 it? */
5579 if (success_p && it->dpvec == NULL)
5580 {
5581 Lisp_Object dv;
5582
5583 if (it->dp
5584 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5585 VECTORP (dv)))
5586 {
5587 struct Lisp_Vector *v = XVECTOR (dv);
5588
5589 /* Return the first character from the display table
5590 entry, if not empty. If empty, don't display the
5591 current character. */
5592 if (v->size)
5593 {
5594 it->dpvec_char_len = it->len;
5595 it->dpvec = v->contents;
5596 it->dpend = v->contents + v->size;
5597 it->current.dpvec_index = 0;
5598 it->dpvec_face_id = -1;
5599 it->saved_face_id = it->face_id;
5600 it->method = GET_FROM_DISPLAY_VECTOR;
5601 it->ellipsis_p = 0;
5602 }
5603 else
5604 {
5605 set_iterator_to_next (it, 0);
5606 }
5607 goto get_next;
5608 }
5609
5610 /* Translate control characters into `\003' or `^C' form.
5611 Control characters coming from a display table entry are
5612 currently not translated because we use IT->dpvec to hold
5613 the translation. This could easily be changed but I
5614 don't believe that it is worth doing.
5615
5616 If it->multibyte_p is nonzero, eight-bit characters and
5617 non-printable multibyte characters are also translated to
5618 octal form.
5619
5620 If it->multibyte_p is zero, eight-bit characters that
5621 don't have corresponding multibyte char code are also
5622 translated to octal form. */
5623 else if ((it->c < ' '
5624 && (it->area != TEXT_AREA
5625 /* In mode line, treat \n like other crl chars. */
5626 || (it->c != '\t'
5627 && it->glyph_row && it->glyph_row->mode_line_p)
5628 || (it->c != '\n' && it->c != '\t')))
5629 || (it->multibyte_p
5630 ? ((it->c >= 127
5631 && it->len == 1)
5632 || !CHAR_PRINTABLE_P (it->c)
5633 || (!NILP (Vnobreak_char_display)
5634 && (it->c == 0x8a0 || it->c == 0x8ad
5635 || it->c == 0x920 || it->c == 0x92d
5636 || it->c == 0xe20 || it->c == 0xe2d
5637 || it->c == 0xf20 || it->c == 0xf2d)))
5638 : (it->c >= 127
5639 && (!unibyte_display_via_language_environment
5640 || it->c == unibyte_char_to_multibyte (it->c)))))
5641 {
5642 /* IT->c is a control character which must be displayed
5643 either as '\003' or as `^C' where the '\\' and '^'
5644 can be defined in the display table. Fill
5645 IT->ctl_chars with glyphs for what we have to
5646 display. Then, set IT->dpvec to these glyphs. */
5647 GLYPH g;
5648 int ctl_len;
5649 int face_id, lface_id = 0 ;
5650 GLYPH escape_glyph;
5651
5652 /* Handle control characters with ^. */
5653
5654 if (it->c < 128 && it->ctl_arrow_p)
5655 {
5656 g = '^'; /* default glyph for Control */
5657 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5658 if (it->dp
5659 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5660 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5661 {
5662 g = XINT (DISP_CTRL_GLYPH (it->dp));
5663 lface_id = FAST_GLYPH_FACE (g);
5664 }
5665 if (lface_id)
5666 {
5667 g = FAST_GLYPH_CHAR (g);
5668 face_id = merge_faces (it->f, Qt, lface_id,
5669 it->face_id);
5670 }
5671 else if (it->f == last_escape_glyph_frame
5672 && it->face_id == last_escape_glyph_face_id)
5673 {
5674 face_id = last_escape_glyph_merged_face_id;
5675 }
5676 else
5677 {
5678 /* Merge the escape-glyph face into the current face. */
5679 face_id = merge_faces (it->f, Qescape_glyph, 0,
5680 it->face_id);
5681 last_escape_glyph_frame = it->f;
5682 last_escape_glyph_face_id = it->face_id;
5683 last_escape_glyph_merged_face_id = face_id;
5684 }
5685
5686 XSETINT (it->ctl_chars[0], g);
5687 g = it->c ^ 0100;
5688 XSETINT (it->ctl_chars[1], g);
5689 ctl_len = 2;
5690 goto display_control;
5691 }
5692
5693 /* Handle non-break space in the mode where it only gets
5694 highlighting. */
5695
5696 if (EQ (Vnobreak_char_display, Qt)
5697 && (it->c == 0x8a0 || it->c == 0x920
5698 || it->c == 0xe20 || it->c == 0xf20))
5699 {
5700 /* Merge the no-break-space face into the current face. */
5701 face_id = merge_faces (it->f, Qnobreak_space, 0,
5702 it->face_id);
5703
5704 g = it->c = ' ';
5705 XSETINT (it->ctl_chars[0], g);
5706 ctl_len = 1;
5707 goto display_control;
5708 }
5709
5710 /* Handle sequences that start with the "escape glyph". */
5711
5712 /* the default escape glyph is \. */
5713 escape_glyph = '\\';
5714
5715 if (it->dp
5716 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5717 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5718 {
5719 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5720 lface_id = FAST_GLYPH_FACE (escape_glyph);
5721 }
5722 if (lface_id)
5723 {
5724 /* The display table specified a face.
5725 Merge it into face_id and also into escape_glyph. */
5726 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5727 face_id = merge_faces (it->f, Qt, lface_id,
5728 it->face_id);
5729 }
5730 else if (it->f == last_escape_glyph_frame
5731 && it->face_id == last_escape_glyph_face_id)
5732 {
5733 face_id = last_escape_glyph_merged_face_id;
5734 }
5735 else
5736 {
5737 /* Merge the escape-glyph face into the current face. */
5738 face_id = merge_faces (it->f, Qescape_glyph, 0,
5739 it->face_id);
5740 last_escape_glyph_frame = it->f;
5741 last_escape_glyph_face_id = it->face_id;
5742 last_escape_glyph_merged_face_id = face_id;
5743 }
5744
5745 /* Handle soft hyphens in the mode where they only get
5746 highlighting. */
5747
5748 if (EQ (Vnobreak_char_display, Qt)
5749 && (it->c == 0x8ad || it->c == 0x92d
5750 || it->c == 0xe2d || it->c == 0xf2d))
5751 {
5752 g = it->c = '-';
5753 XSETINT (it->ctl_chars[0], g);
5754 ctl_len = 1;
5755 goto display_control;
5756 }
5757
5758 /* Handle non-break space and soft hyphen
5759 with the escape glyph. */
5760
5761 if (it->c == 0x8a0 || it->c == 0x8ad
5762 || it->c == 0x920 || it->c == 0x92d
5763 || it->c == 0xe20 || it->c == 0xe2d
5764 || it->c == 0xf20 || it->c == 0xf2d)
5765 {
5766 XSETINT (it->ctl_chars[0], escape_glyph);
5767 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5768 XSETINT (it->ctl_chars[1], g);
5769 ctl_len = 2;
5770 goto display_control;
5771 }
5772
5773 {
5774 unsigned char str[MAX_MULTIBYTE_LENGTH];
5775 int len;
5776 int i;
5777
5778 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5779 if (SINGLE_BYTE_CHAR_P (it->c))
5780 str[0] = it->c, len = 1;
5781 else
5782 {
5783 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5784 if (len < 0)
5785 {
5786 /* It's an invalid character, which shouldn't
5787 happen actually, but due to bugs it may
5788 happen. Let's print the char as is, there's
5789 not much meaningful we can do with it. */
5790 str[0] = it->c;
5791 str[1] = it->c >> 8;
5792 str[2] = it->c >> 16;
5793 str[3] = it->c >> 24;
5794 len = 4;
5795 }
5796 }
5797
5798 for (i = 0; i < len; i++)
5799 {
5800 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5801 /* Insert three more glyphs into IT->ctl_chars for
5802 the octal display of the character. */
5803 g = ((str[i] >> 6) & 7) + '0';
5804 XSETINT (it->ctl_chars[i * 4 + 1], g);
5805 g = ((str[i] >> 3) & 7) + '0';
5806 XSETINT (it->ctl_chars[i * 4 + 2], g);
5807 g = (str[i] & 7) + '0';
5808 XSETINT (it->ctl_chars[i * 4 + 3], g);
5809 }
5810 ctl_len = len * 4;
5811 }
5812
5813 display_control:
5814 /* Set up IT->dpvec and return first character from it. */
5815 it->dpvec_char_len = it->len;
5816 it->dpvec = it->ctl_chars;
5817 it->dpend = it->dpvec + ctl_len;
5818 it->current.dpvec_index = 0;
5819 it->dpvec_face_id = face_id;
5820 it->saved_face_id = it->face_id;
5821 it->method = GET_FROM_DISPLAY_VECTOR;
5822 it->ellipsis_p = 0;
5823 goto get_next;
5824 }
5825 }
5826
5827 /* Adjust face id for a multibyte character. There are no
5828 multibyte character in unibyte text. */
5829 if (it->multibyte_p
5830 && success_p
5831 && FRAME_WINDOW_P (it->f))
5832 {
5833 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5834 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5835 }
5836 }
5837
5838 /* Is this character the last one of a run of characters with
5839 box? If yes, set IT->end_of_box_run_p to 1. */
5840 if (it->face_box_p
5841 && it->s == NULL)
5842 {
5843 int face_id;
5844 struct face *face;
5845
5846 it->end_of_box_run_p
5847 = ((face_id = face_after_it_pos (it),
5848 face_id != it->face_id)
5849 && (face = FACE_FROM_ID (it->f, face_id),
5850 face->box == FACE_NO_BOX));
5851 }
5852
5853 /* Value is 0 if end of buffer or string reached. */
5854 return success_p;
5855 }
5856
5857
5858 /* Move IT to the next display element.
5859
5860 RESEAT_P non-zero means if called on a newline in buffer text,
5861 skip to the next visible line start.
5862
5863 Functions get_next_display_element and set_iterator_to_next are
5864 separate because I find this arrangement easier to handle than a
5865 get_next_display_element function that also increments IT's
5866 position. The way it is we can first look at an iterator's current
5867 display element, decide whether it fits on a line, and if it does,
5868 increment the iterator position. The other way around we probably
5869 would either need a flag indicating whether the iterator has to be
5870 incremented the next time, or we would have to implement a
5871 decrement position function which would not be easy to write. */
5872
5873 void
5874 set_iterator_to_next (it, reseat_p)
5875 struct it *it;
5876 int reseat_p;
5877 {
5878 /* Reset flags indicating start and end of a sequence of characters
5879 with box. Reset them at the start of this function because
5880 moving the iterator to a new position might set them. */
5881 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5882
5883 switch (it->method)
5884 {
5885 case GET_FROM_BUFFER:
5886 /* The current display element of IT is a character from
5887 current_buffer. Advance in the buffer, and maybe skip over
5888 invisible lines that are so because of selective display. */
5889 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5890 reseat_at_next_visible_line_start (it, 0);
5891 else
5892 {
5893 xassert (it->len != 0);
5894 IT_BYTEPOS (*it) += it->len;
5895 IT_CHARPOS (*it) += 1;
5896 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5897 }
5898 break;
5899
5900 case GET_FROM_COMPOSITION:
5901 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5902 xassert (it->sp > 0);
5903 pop_it (it);
5904 if (it->method == GET_FROM_STRING)
5905 {
5906 IT_STRING_BYTEPOS (*it) += it->len;
5907 IT_STRING_CHARPOS (*it) += it->cmp_len;
5908 goto consider_string_end;
5909 }
5910 else if (it->method == GET_FROM_BUFFER)
5911 {
5912 IT_BYTEPOS (*it) += it->len;
5913 IT_CHARPOS (*it) += it->cmp_len;
5914 }
5915 break;
5916
5917 case GET_FROM_C_STRING:
5918 /* Current display element of IT is from a C string. */
5919 IT_BYTEPOS (*it) += it->len;
5920 IT_CHARPOS (*it) += 1;
5921 break;
5922
5923 case GET_FROM_DISPLAY_VECTOR:
5924 /* Current display element of IT is from a display table entry.
5925 Advance in the display table definition. Reset it to null if
5926 end reached, and continue with characters from buffers/
5927 strings. */
5928 ++it->current.dpvec_index;
5929
5930 /* Restore face of the iterator to what they were before the
5931 display vector entry (these entries may contain faces). */
5932 it->face_id = it->saved_face_id;
5933
5934 if (it->dpvec + it->current.dpvec_index == it->dpend)
5935 {
5936 int recheck_faces = it->ellipsis_p;
5937
5938 if (it->s)
5939 it->method = GET_FROM_C_STRING;
5940 else if (STRINGP (it->string))
5941 it->method = GET_FROM_STRING;
5942 else
5943 {
5944 it->method = GET_FROM_BUFFER;
5945 it->object = it->w->buffer;
5946 }
5947
5948 it->dpvec = NULL;
5949 it->current.dpvec_index = -1;
5950
5951 /* Skip over characters which were displayed via IT->dpvec. */
5952 if (it->dpvec_char_len < 0)
5953 reseat_at_next_visible_line_start (it, 1);
5954 else if (it->dpvec_char_len > 0)
5955 {
5956 if (it->method == GET_FROM_STRING
5957 && it->n_overlay_strings > 0)
5958 it->ignore_overlay_strings_at_pos_p = 1;
5959 it->len = it->dpvec_char_len;
5960 set_iterator_to_next (it, reseat_p);
5961 }
5962
5963 /* Maybe recheck faces after display vector */
5964 if (recheck_faces)
5965 it->stop_charpos = IT_CHARPOS (*it);
5966 }
5967 break;
5968
5969 case GET_FROM_STRING:
5970 /* Current display element is a character from a Lisp string. */
5971 xassert (it->s == NULL && STRINGP (it->string));
5972 IT_STRING_BYTEPOS (*it) += it->len;
5973 IT_STRING_CHARPOS (*it) += 1;
5974
5975 consider_string_end:
5976
5977 if (it->current.overlay_string_index >= 0)
5978 {
5979 /* IT->string is an overlay string. Advance to the
5980 next, if there is one. */
5981 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5982 next_overlay_string (it);
5983 }
5984 else
5985 {
5986 /* IT->string is not an overlay string. If we reached
5987 its end, and there is something on IT->stack, proceed
5988 with what is on the stack. This can be either another
5989 string, this time an overlay string, or a buffer. */
5990 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5991 && it->sp > 0)
5992 {
5993 pop_it (it);
5994 if (it->method == GET_FROM_STRING)
5995 goto consider_string_end;
5996 }
5997 }
5998 break;
5999
6000 case GET_FROM_IMAGE:
6001 case GET_FROM_STRETCH:
6002 /* The position etc with which we have to proceed are on
6003 the stack. The position may be at the end of a string,
6004 if the `display' property takes up the whole string. */
6005 xassert (it->sp > 0);
6006 pop_it (it);
6007 if (it->method == GET_FROM_STRING)
6008 goto consider_string_end;
6009 break;
6010
6011 default:
6012 /* There are no other methods defined, so this should be a bug. */
6013 abort ();
6014 }
6015
6016 xassert (it->method != GET_FROM_STRING
6017 || (STRINGP (it->string)
6018 && IT_STRING_CHARPOS (*it) >= 0));
6019 }
6020
6021 /* Load IT's display element fields with information about the next
6022 display element which comes from a display table entry or from the
6023 result of translating a control character to one of the forms `^C'
6024 or `\003'.
6025
6026 IT->dpvec holds the glyphs to return as characters.
6027 IT->saved_face_id holds the face id before the display vector--
6028 it is restored into IT->face_idin set_iterator_to_next. */
6029
6030 static int
6031 next_element_from_display_vector (it)
6032 struct it *it;
6033 {
6034 /* Precondition. */
6035 xassert (it->dpvec && it->current.dpvec_index >= 0);
6036
6037 it->face_id = it->saved_face_id;
6038
6039 if (INTEGERP (*it->dpvec)
6040 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6041 {
6042 GLYPH g;
6043
6044 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6045 it->c = FAST_GLYPH_CHAR (g);
6046 it->len = CHAR_BYTES (it->c);
6047
6048 /* The entry may contain a face id to use. Such a face id is
6049 the id of a Lisp face, not a realized face. A face id of
6050 zero means no face is specified. */
6051 if (it->dpvec_face_id >= 0)
6052 it->face_id = it->dpvec_face_id;
6053 else
6054 {
6055 int lface_id = FAST_GLYPH_FACE (g);
6056 if (lface_id > 0)
6057 it->face_id = merge_faces (it->f, Qt, lface_id,
6058 it->saved_face_id);
6059 }
6060 }
6061 else
6062 /* Display table entry is invalid. Return a space. */
6063 it->c = ' ', it->len = 1;
6064
6065 /* Don't change position and object of the iterator here. They are
6066 still the values of the character that had this display table
6067 entry or was translated, and that's what we want. */
6068 it->what = IT_CHARACTER;
6069 return 1;
6070 }
6071
6072
6073 /* Load IT with the next display element from Lisp string IT->string.
6074 IT->current.string_pos is the current position within the string.
6075 If IT->current.overlay_string_index >= 0, the Lisp string is an
6076 overlay string. */
6077
6078 static int
6079 next_element_from_string (it)
6080 struct it *it;
6081 {
6082 struct text_pos position;
6083
6084 xassert (STRINGP (it->string));
6085 xassert (IT_STRING_CHARPOS (*it) >= 0);
6086 position = it->current.string_pos;
6087
6088 /* Time to check for invisible text? */
6089 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6090 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6091 {
6092 handle_stop (it);
6093
6094 /* Since a handler may have changed IT->method, we must
6095 recurse here. */
6096 return get_next_display_element (it);
6097 }
6098
6099 if (it->current.overlay_string_index >= 0)
6100 {
6101 /* Get the next character from an overlay string. In overlay
6102 strings, There is no field width or padding with spaces to
6103 do. */
6104 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6105 {
6106 it->what = IT_EOB;
6107 return 0;
6108 }
6109 else if (STRING_MULTIBYTE (it->string))
6110 {
6111 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6112 const unsigned char *s = (SDATA (it->string)
6113 + IT_STRING_BYTEPOS (*it));
6114 it->c = string_char_and_length (s, remaining, &it->len);
6115 }
6116 else
6117 {
6118 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6119 it->len = 1;
6120 }
6121 }
6122 else
6123 {
6124 /* Get the next character from a Lisp string that is not an
6125 overlay string. Such strings come from the mode line, for
6126 example. We may have to pad with spaces, or truncate the
6127 string. See also next_element_from_c_string. */
6128 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6129 {
6130 it->what = IT_EOB;
6131 return 0;
6132 }
6133 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6134 {
6135 /* Pad with spaces. */
6136 it->c = ' ', it->len = 1;
6137 CHARPOS (position) = BYTEPOS (position) = -1;
6138 }
6139 else if (STRING_MULTIBYTE (it->string))
6140 {
6141 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6142 const unsigned char *s = (SDATA (it->string)
6143 + IT_STRING_BYTEPOS (*it));
6144 it->c = string_char_and_length (s, maxlen, &it->len);
6145 }
6146 else
6147 {
6148 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6149 it->len = 1;
6150 }
6151 }
6152
6153 /* Record what we have and where it came from. */
6154 it->what = IT_CHARACTER;
6155 it->object = it->string;
6156 it->position = position;
6157 return 1;
6158 }
6159
6160
6161 /* Load IT with next display element from C string IT->s.
6162 IT->string_nchars is the maximum number of characters to return
6163 from the string. IT->end_charpos may be greater than
6164 IT->string_nchars when this function is called, in which case we
6165 may have to return padding spaces. Value is zero if end of string
6166 reached, including padding spaces. */
6167
6168 static int
6169 next_element_from_c_string (it)
6170 struct it *it;
6171 {
6172 int success_p = 1;
6173
6174 xassert (it->s);
6175 it->what = IT_CHARACTER;
6176 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6177 it->object = Qnil;
6178
6179 /* IT's position can be greater IT->string_nchars in case a field
6180 width or precision has been specified when the iterator was
6181 initialized. */
6182 if (IT_CHARPOS (*it) >= it->end_charpos)
6183 {
6184 /* End of the game. */
6185 it->what = IT_EOB;
6186 success_p = 0;
6187 }
6188 else if (IT_CHARPOS (*it) >= it->string_nchars)
6189 {
6190 /* Pad with spaces. */
6191 it->c = ' ', it->len = 1;
6192 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6193 }
6194 else if (it->multibyte_p)
6195 {
6196 /* Implementation note: The calls to strlen apparently aren't a
6197 performance problem because there is no noticeable performance
6198 difference between Emacs running in unibyte or multibyte mode. */
6199 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6200 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6201 maxlen, &it->len);
6202 }
6203 else
6204 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6205
6206 return success_p;
6207 }
6208
6209
6210 /* Set up IT to return characters from an ellipsis, if appropriate.
6211 The definition of the ellipsis glyphs may come from a display table
6212 entry. This function Fills IT with the first glyph from the
6213 ellipsis if an ellipsis is to be displayed. */
6214
6215 static int
6216 next_element_from_ellipsis (it)
6217 struct it *it;
6218 {
6219 if (it->selective_display_ellipsis_p)
6220 setup_for_ellipsis (it, it->len);
6221 else
6222 {
6223 /* The face at the current position may be different from the
6224 face we find after the invisible text. Remember what it
6225 was in IT->saved_face_id, and signal that it's there by
6226 setting face_before_selective_p. */
6227 it->saved_face_id = it->face_id;
6228 it->method = GET_FROM_BUFFER;
6229 it->object = it->w->buffer;
6230 reseat_at_next_visible_line_start (it, 1);
6231 it->face_before_selective_p = 1;
6232 }
6233
6234 return get_next_display_element (it);
6235 }
6236
6237
6238 /* Deliver an image display element. The iterator IT is already
6239 filled with image information (done in handle_display_prop). Value
6240 is always 1. */
6241
6242
6243 static int
6244 next_element_from_image (it)
6245 struct it *it;
6246 {
6247 it->what = IT_IMAGE;
6248 return 1;
6249 }
6250
6251
6252 /* Fill iterator IT with next display element from a stretch glyph
6253 property. IT->object is the value of the text property. Value is
6254 always 1. */
6255
6256 static int
6257 next_element_from_stretch (it)
6258 struct it *it;
6259 {
6260 it->what = IT_STRETCH;
6261 return 1;
6262 }
6263
6264
6265 /* Load IT with the next display element from current_buffer. Value
6266 is zero if end of buffer reached. IT->stop_charpos is the next
6267 position at which to stop and check for text properties or buffer
6268 end. */
6269
6270 static int
6271 next_element_from_buffer (it)
6272 struct it *it;
6273 {
6274 int success_p = 1;
6275
6276 /* Check this assumption, otherwise, we would never enter the
6277 if-statement, below. */
6278 xassert (IT_CHARPOS (*it) >= BEGV
6279 && IT_CHARPOS (*it) <= it->stop_charpos);
6280
6281 if (IT_CHARPOS (*it) >= it->stop_charpos)
6282 {
6283 if (IT_CHARPOS (*it) >= it->end_charpos)
6284 {
6285 int overlay_strings_follow_p;
6286
6287 /* End of the game, except when overlay strings follow that
6288 haven't been returned yet. */
6289 if (it->overlay_strings_at_end_processed_p)
6290 overlay_strings_follow_p = 0;
6291 else
6292 {
6293 it->overlay_strings_at_end_processed_p = 1;
6294 overlay_strings_follow_p = get_overlay_strings (it, 0);
6295 }
6296
6297 if (overlay_strings_follow_p)
6298 success_p = get_next_display_element (it);
6299 else
6300 {
6301 it->what = IT_EOB;
6302 it->position = it->current.pos;
6303 success_p = 0;
6304 }
6305 }
6306 else
6307 {
6308 handle_stop (it);
6309 return get_next_display_element (it);
6310 }
6311 }
6312 else
6313 {
6314 /* No face changes, overlays etc. in sight, so just return a
6315 character from current_buffer. */
6316 unsigned char *p;
6317
6318 /* Maybe run the redisplay end trigger hook. Performance note:
6319 This doesn't seem to cost measurable time. */
6320 if (it->redisplay_end_trigger_charpos
6321 && it->glyph_row
6322 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6323 run_redisplay_end_trigger_hook (it);
6324
6325 /* Get the next character, maybe multibyte. */
6326 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6327 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6328 {
6329 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6330 - IT_BYTEPOS (*it));
6331 it->c = string_char_and_length (p, maxlen, &it->len);
6332 }
6333 else
6334 it->c = *p, it->len = 1;
6335
6336 /* Record what we have and where it came from. */
6337 it->what = IT_CHARACTER;
6338 it->object = it->w->buffer;
6339 it->position = it->current.pos;
6340
6341 /* Normally we return the character found above, except when we
6342 really want to return an ellipsis for selective display. */
6343 if (it->selective)
6344 {
6345 if (it->c == '\n')
6346 {
6347 /* A value of selective > 0 means hide lines indented more
6348 than that number of columns. */
6349 if (it->selective > 0
6350 && IT_CHARPOS (*it) + 1 < ZV
6351 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6352 IT_BYTEPOS (*it) + 1,
6353 (double) it->selective)) /* iftc */
6354 {
6355 success_p = next_element_from_ellipsis (it);
6356 it->dpvec_char_len = -1;
6357 }
6358 }
6359 else if (it->c == '\r' && it->selective == -1)
6360 {
6361 /* A value of selective == -1 means that everything from the
6362 CR to the end of the line is invisible, with maybe an
6363 ellipsis displayed for it. */
6364 success_p = next_element_from_ellipsis (it);
6365 it->dpvec_char_len = -1;
6366 }
6367 }
6368 }
6369
6370 /* Value is zero if end of buffer reached. */
6371 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6372 return success_p;
6373 }
6374
6375
6376 /* Run the redisplay end trigger hook for IT. */
6377
6378 static void
6379 run_redisplay_end_trigger_hook (it)
6380 struct it *it;
6381 {
6382 Lisp_Object args[3];
6383
6384 /* IT->glyph_row should be non-null, i.e. we should be actually
6385 displaying something, or otherwise we should not run the hook. */
6386 xassert (it->glyph_row);
6387
6388 /* Set up hook arguments. */
6389 args[0] = Qredisplay_end_trigger_functions;
6390 args[1] = it->window;
6391 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6392 it->redisplay_end_trigger_charpos = 0;
6393
6394 /* Since we are *trying* to run these functions, don't try to run
6395 them again, even if they get an error. */
6396 it->w->redisplay_end_trigger = Qnil;
6397 Frun_hook_with_args (3, args);
6398
6399 /* Notice if it changed the face of the character we are on. */
6400 handle_face_prop (it);
6401 }
6402
6403
6404 /* Deliver a composition display element. The iterator IT is already
6405 filled with composition information (done in
6406 handle_composition_prop). Value is always 1. */
6407
6408 static int
6409 next_element_from_composition (it)
6410 struct it *it;
6411 {
6412 it->what = IT_COMPOSITION;
6413 it->position = (STRINGP (it->string)
6414 ? it->current.string_pos
6415 : it->current.pos);
6416 if (STRINGP (it->string))
6417 it->object = it->string;
6418 else
6419 it->object = it->w->buffer;
6420 return 1;
6421 }
6422
6423
6424 \f
6425 /***********************************************************************
6426 Moving an iterator without producing glyphs
6427 ***********************************************************************/
6428
6429 /* Check if iterator is at a position corresponding to a valid buffer
6430 position after some move_it_ call. */
6431
6432 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6433 ((it)->method == GET_FROM_STRING \
6434 ? IT_STRING_CHARPOS (*it) == 0 \
6435 : 1)
6436
6437
6438 /* Move iterator IT to a specified buffer or X position within one
6439 line on the display without producing glyphs.
6440
6441 OP should be a bit mask including some or all of these bits:
6442 MOVE_TO_X: Stop on reaching x-position TO_X.
6443 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6444 Regardless of OP's value, stop in reaching the end of the display line.
6445
6446 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6447 This means, in particular, that TO_X includes window's horizontal
6448 scroll amount.
6449
6450 The return value has several possible values that
6451 say what condition caused the scan to stop:
6452
6453 MOVE_POS_MATCH_OR_ZV
6454 - when TO_POS or ZV was reached.
6455
6456 MOVE_X_REACHED
6457 -when TO_X was reached before TO_POS or ZV were reached.
6458
6459 MOVE_LINE_CONTINUED
6460 - when we reached the end of the display area and the line must
6461 be continued.
6462
6463 MOVE_LINE_TRUNCATED
6464 - when we reached the end of the display area and the line is
6465 truncated.
6466
6467 MOVE_NEWLINE_OR_CR
6468 - when we stopped at a line end, i.e. a newline or a CR and selective
6469 display is on. */
6470
6471 static enum move_it_result
6472 move_it_in_display_line_to (it, to_charpos, to_x, op)
6473 struct it *it;
6474 int to_charpos, to_x, op;
6475 {
6476 enum move_it_result result = MOVE_UNDEFINED;
6477 struct glyph_row *saved_glyph_row;
6478
6479 /* Don't produce glyphs in produce_glyphs. */
6480 saved_glyph_row = it->glyph_row;
6481 it->glyph_row = NULL;
6482
6483 #define BUFFER_POS_REACHED_P() \
6484 ((op & MOVE_TO_POS) != 0 \
6485 && BUFFERP (it->object) \
6486 && IT_CHARPOS (*it) >= to_charpos \
6487 && (it->method == GET_FROM_BUFFER \
6488 || (it->method == GET_FROM_DISPLAY_VECTOR \
6489 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6490
6491
6492 while (1)
6493 {
6494 int x, i, ascent = 0, descent = 0;
6495
6496 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6497 if ((op & MOVE_TO_POS) != 0
6498 && BUFFERP (it->object)
6499 && it->method == GET_FROM_BUFFER
6500 && IT_CHARPOS (*it) > to_charpos)
6501 {
6502 result = MOVE_POS_MATCH_OR_ZV;
6503 break;
6504 }
6505
6506 /* Stop when ZV reached.
6507 We used to stop here when TO_CHARPOS reached as well, but that is
6508 too soon if this glyph does not fit on this line. So we handle it
6509 explicitly below. */
6510 if (!get_next_display_element (it)
6511 || (it->truncate_lines_p
6512 && BUFFER_POS_REACHED_P ()))
6513 {
6514 result = MOVE_POS_MATCH_OR_ZV;
6515 break;
6516 }
6517
6518 /* The call to produce_glyphs will get the metrics of the
6519 display element IT is loaded with. We record in x the
6520 x-position before this display element in case it does not
6521 fit on the line. */
6522 x = it->current_x;
6523
6524 /* Remember the line height so far in case the next element doesn't
6525 fit on the line. */
6526 if (!it->truncate_lines_p)
6527 {
6528 ascent = it->max_ascent;
6529 descent = it->max_descent;
6530 }
6531
6532 PRODUCE_GLYPHS (it);
6533
6534 if (it->area != TEXT_AREA)
6535 {
6536 set_iterator_to_next (it, 1);
6537 continue;
6538 }
6539
6540 /* The number of glyphs we get back in IT->nglyphs will normally
6541 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6542 character on a terminal frame, or (iii) a line end. For the
6543 second case, IT->nglyphs - 1 padding glyphs will be present
6544 (on X frames, there is only one glyph produced for a
6545 composite character.
6546
6547 The behavior implemented below means, for continuation lines,
6548 that as many spaces of a TAB as fit on the current line are
6549 displayed there. For terminal frames, as many glyphs of a
6550 multi-glyph character are displayed in the current line, too.
6551 This is what the old redisplay code did, and we keep it that
6552 way. Under X, the whole shape of a complex character must
6553 fit on the line or it will be completely displayed in the
6554 next line.
6555
6556 Note that both for tabs and padding glyphs, all glyphs have
6557 the same width. */
6558 if (it->nglyphs)
6559 {
6560 /* More than one glyph or glyph doesn't fit on line. All
6561 glyphs have the same width. */
6562 int single_glyph_width = it->pixel_width / it->nglyphs;
6563 int new_x;
6564 int x_before_this_char = x;
6565 int hpos_before_this_char = it->hpos;
6566
6567 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6568 {
6569 new_x = x + single_glyph_width;
6570
6571 /* We want to leave anything reaching TO_X to the caller. */
6572 if ((op & MOVE_TO_X) && new_x > to_x)
6573 {
6574 if (BUFFER_POS_REACHED_P ())
6575 goto buffer_pos_reached;
6576 it->current_x = x;
6577 result = MOVE_X_REACHED;
6578 break;
6579 }
6580 else if (/* Lines are continued. */
6581 !it->truncate_lines_p
6582 && (/* And glyph doesn't fit on the line. */
6583 new_x > it->last_visible_x
6584 /* Or it fits exactly and we're on a window
6585 system frame. */
6586 || (new_x == it->last_visible_x
6587 && FRAME_WINDOW_P (it->f))))
6588 {
6589 if (/* IT->hpos == 0 means the very first glyph
6590 doesn't fit on the line, e.g. a wide image. */
6591 it->hpos == 0
6592 || (new_x == it->last_visible_x
6593 && FRAME_WINDOW_P (it->f)))
6594 {
6595 ++it->hpos;
6596 it->current_x = new_x;
6597
6598 /* The character's last glyph just barely fits
6599 in this row. */
6600 if (i == it->nglyphs - 1)
6601 {
6602 /* If this is the destination position,
6603 return a position *before* it in this row,
6604 now that we know it fits in this row. */
6605 if (BUFFER_POS_REACHED_P ())
6606 {
6607 it->hpos = hpos_before_this_char;
6608 it->current_x = x_before_this_char;
6609 result = MOVE_POS_MATCH_OR_ZV;
6610 break;
6611 }
6612
6613 set_iterator_to_next (it, 1);
6614 #ifdef HAVE_WINDOW_SYSTEM
6615 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6616 {
6617 if (!get_next_display_element (it))
6618 {
6619 result = MOVE_POS_MATCH_OR_ZV;
6620 break;
6621 }
6622 if (BUFFER_POS_REACHED_P ())
6623 {
6624 if (ITERATOR_AT_END_OF_LINE_P (it))
6625 result = MOVE_POS_MATCH_OR_ZV;
6626 else
6627 result = MOVE_LINE_CONTINUED;
6628 break;
6629 }
6630 if (ITERATOR_AT_END_OF_LINE_P (it))
6631 {
6632 result = MOVE_NEWLINE_OR_CR;
6633 break;
6634 }
6635 }
6636 #endif /* HAVE_WINDOW_SYSTEM */
6637 }
6638 }
6639 else
6640 {
6641 it->current_x = x;
6642 it->max_ascent = ascent;
6643 it->max_descent = descent;
6644 }
6645
6646 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6647 IT_CHARPOS (*it)));
6648 result = MOVE_LINE_CONTINUED;
6649 break;
6650 }
6651 else if (BUFFER_POS_REACHED_P ())
6652 goto buffer_pos_reached;
6653 else if (new_x > it->first_visible_x)
6654 {
6655 /* Glyph is visible. Increment number of glyphs that
6656 would be displayed. */
6657 ++it->hpos;
6658 }
6659 else
6660 {
6661 /* Glyph is completely off the left margin of the display
6662 area. Nothing to do. */
6663 }
6664 }
6665
6666 if (result != MOVE_UNDEFINED)
6667 break;
6668 }
6669 else if (BUFFER_POS_REACHED_P ())
6670 {
6671 buffer_pos_reached:
6672 it->current_x = x;
6673 it->max_ascent = ascent;
6674 it->max_descent = descent;
6675 result = MOVE_POS_MATCH_OR_ZV;
6676 break;
6677 }
6678 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6679 {
6680 /* Stop when TO_X specified and reached. This check is
6681 necessary here because of lines consisting of a line end,
6682 only. The line end will not produce any glyphs and we
6683 would never get MOVE_X_REACHED. */
6684 xassert (it->nglyphs == 0);
6685 result = MOVE_X_REACHED;
6686 break;
6687 }
6688
6689 /* Is this a line end? If yes, we're done. */
6690 if (ITERATOR_AT_END_OF_LINE_P (it))
6691 {
6692 result = MOVE_NEWLINE_OR_CR;
6693 break;
6694 }
6695
6696 /* The current display element has been consumed. Advance
6697 to the next. */
6698 set_iterator_to_next (it, 1);
6699
6700 /* Stop if lines are truncated and IT's current x-position is
6701 past the right edge of the window now. */
6702 if (it->truncate_lines_p
6703 && it->current_x >= it->last_visible_x)
6704 {
6705 #ifdef HAVE_WINDOW_SYSTEM
6706 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6707 {
6708 if (!get_next_display_element (it)
6709 || BUFFER_POS_REACHED_P ())
6710 {
6711 result = MOVE_POS_MATCH_OR_ZV;
6712 break;
6713 }
6714 if (ITERATOR_AT_END_OF_LINE_P (it))
6715 {
6716 result = MOVE_NEWLINE_OR_CR;
6717 break;
6718 }
6719 }
6720 #endif /* HAVE_WINDOW_SYSTEM */
6721 result = MOVE_LINE_TRUNCATED;
6722 break;
6723 }
6724 }
6725
6726 #undef BUFFER_POS_REACHED_P
6727
6728 /* Restore the iterator settings altered at the beginning of this
6729 function. */
6730 it->glyph_row = saved_glyph_row;
6731 return result;
6732 }
6733
6734
6735 /* Move IT forward until it satisfies one or more of the criteria in
6736 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6737
6738 OP is a bit-mask that specifies where to stop, and in particular,
6739 which of those four position arguments makes a difference. See the
6740 description of enum move_operation_enum.
6741
6742 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6743 screen line, this function will set IT to the next position >
6744 TO_CHARPOS. */
6745
6746 void
6747 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6748 struct it *it;
6749 int to_charpos, to_x, to_y, to_vpos;
6750 int op;
6751 {
6752 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6753 int line_height;
6754 int reached = 0;
6755
6756 for (;;)
6757 {
6758 if (op & MOVE_TO_VPOS)
6759 {
6760 /* If no TO_CHARPOS and no TO_X specified, stop at the
6761 start of the line TO_VPOS. */
6762 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6763 {
6764 if (it->vpos == to_vpos)
6765 {
6766 reached = 1;
6767 break;
6768 }
6769 else
6770 skip = move_it_in_display_line_to (it, -1, -1, 0);
6771 }
6772 else
6773 {
6774 /* TO_VPOS >= 0 means stop at TO_X in the line at
6775 TO_VPOS, or at TO_POS, whichever comes first. */
6776 if (it->vpos == to_vpos)
6777 {
6778 reached = 2;
6779 break;
6780 }
6781
6782 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6783
6784 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6785 {
6786 reached = 3;
6787 break;
6788 }
6789 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6790 {
6791 /* We have reached TO_X but not in the line we want. */
6792 skip = move_it_in_display_line_to (it, to_charpos,
6793 -1, MOVE_TO_POS);
6794 if (skip == MOVE_POS_MATCH_OR_ZV)
6795 {
6796 reached = 4;
6797 break;
6798 }
6799 }
6800 }
6801 }
6802 else if (op & MOVE_TO_Y)
6803 {
6804 struct it it_backup;
6805
6806 /* TO_Y specified means stop at TO_X in the line containing
6807 TO_Y---or at TO_CHARPOS if this is reached first. The
6808 problem is that we can't really tell whether the line
6809 contains TO_Y before we have completely scanned it, and
6810 this may skip past TO_X. What we do is to first scan to
6811 TO_X.
6812
6813 If TO_X is not specified, use a TO_X of zero. The reason
6814 is to make the outcome of this function more predictable.
6815 If we didn't use TO_X == 0, we would stop at the end of
6816 the line which is probably not what a caller would expect
6817 to happen. */
6818 skip = move_it_in_display_line_to (it, to_charpos,
6819 ((op & MOVE_TO_X)
6820 ? to_x : 0),
6821 (MOVE_TO_X
6822 | (op & MOVE_TO_POS)));
6823
6824 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6825 if (skip == MOVE_POS_MATCH_OR_ZV)
6826 {
6827 reached = 5;
6828 break;
6829 }
6830
6831 /* If TO_X was reached, we would like to know whether TO_Y
6832 is in the line. This can only be said if we know the
6833 total line height which requires us to scan the rest of
6834 the line. */
6835 if (skip == MOVE_X_REACHED)
6836 {
6837 it_backup = *it;
6838 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6839 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6840 op & MOVE_TO_POS);
6841 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6842 }
6843
6844 /* Now, decide whether TO_Y is in this line. */
6845 line_height = it->max_ascent + it->max_descent;
6846 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6847
6848 if (to_y >= it->current_y
6849 && to_y < it->current_y + line_height)
6850 {
6851 if (skip == MOVE_X_REACHED)
6852 /* If TO_Y is in this line and TO_X was reached above,
6853 we scanned too far. We have to restore IT's settings
6854 to the ones before skipping. */
6855 *it = it_backup;
6856 reached = 6;
6857 }
6858 else if (skip == MOVE_X_REACHED)
6859 {
6860 skip = skip2;
6861 if (skip == MOVE_POS_MATCH_OR_ZV)
6862 reached = 7;
6863 }
6864
6865 if (reached)
6866 break;
6867 }
6868 else if (BUFFERP (it->object)
6869 && it->method == GET_FROM_BUFFER
6870 && IT_CHARPOS (*it) >= to_charpos)
6871 skip = MOVE_POS_MATCH_OR_ZV;
6872 else
6873 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6874
6875 switch (skip)
6876 {
6877 case MOVE_POS_MATCH_OR_ZV:
6878 reached = 8;
6879 goto out;
6880
6881 case MOVE_NEWLINE_OR_CR:
6882 set_iterator_to_next (it, 1);
6883 it->continuation_lines_width = 0;
6884 break;
6885
6886 case MOVE_LINE_TRUNCATED:
6887 it->continuation_lines_width = 0;
6888 reseat_at_next_visible_line_start (it, 0);
6889 if ((op & MOVE_TO_POS) != 0
6890 && IT_CHARPOS (*it) > to_charpos)
6891 {
6892 reached = 9;
6893 goto out;
6894 }
6895 break;
6896
6897 case MOVE_LINE_CONTINUED:
6898 /* For continued lines ending in a tab, some of the glyphs
6899 associated with the tab are displayed on the current
6900 line. Since it->current_x does not include these glyphs,
6901 we use it->last_visible_x instead. */
6902 it->continuation_lines_width +=
6903 (it->c == '\t') ? it->last_visible_x : it->current_x;
6904 break;
6905
6906 default:
6907 abort ();
6908 }
6909
6910 /* Reset/increment for the next run. */
6911 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6912 it->current_x = it->hpos = 0;
6913 it->current_y += it->max_ascent + it->max_descent;
6914 ++it->vpos;
6915 last_height = it->max_ascent + it->max_descent;
6916 last_max_ascent = it->max_ascent;
6917 it->max_ascent = it->max_descent = 0;
6918 }
6919
6920 out:
6921
6922 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6923 }
6924
6925
6926 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6927
6928 If DY > 0, move IT backward at least that many pixels. DY = 0
6929 means move IT backward to the preceding line start or BEGV. This
6930 function may move over more than DY pixels if IT->current_y - DY
6931 ends up in the middle of a line; in this case IT->current_y will be
6932 set to the top of the line moved to. */
6933
6934 void
6935 move_it_vertically_backward (it, dy)
6936 struct it *it;
6937 int dy;
6938 {
6939 int nlines, h;
6940 struct it it2, it3;
6941 int start_pos;
6942
6943 move_further_back:
6944 xassert (dy >= 0);
6945
6946 start_pos = IT_CHARPOS (*it);
6947
6948 /* Estimate how many newlines we must move back. */
6949 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6950
6951 /* Set the iterator's position that many lines back. */
6952 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6953 back_to_previous_visible_line_start (it);
6954
6955 /* Reseat the iterator here. When moving backward, we don't want
6956 reseat to skip forward over invisible text, set up the iterator
6957 to deliver from overlay strings at the new position etc. So,
6958 use reseat_1 here. */
6959 reseat_1 (it, it->current.pos, 1);
6960
6961 /* We are now surely at a line start. */
6962 it->current_x = it->hpos = 0;
6963 it->continuation_lines_width = 0;
6964
6965 /* Move forward and see what y-distance we moved. First move to the
6966 start of the next line so that we get its height. We need this
6967 height to be able to tell whether we reached the specified
6968 y-distance. */
6969 it2 = *it;
6970 it2.max_ascent = it2.max_descent = 0;
6971 do
6972 {
6973 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6974 MOVE_TO_POS | MOVE_TO_VPOS);
6975 }
6976 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6977 xassert (IT_CHARPOS (*it) >= BEGV);
6978 it3 = it2;
6979
6980 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6981 xassert (IT_CHARPOS (*it) >= BEGV);
6982 /* H is the actual vertical distance from the position in *IT
6983 and the starting position. */
6984 h = it2.current_y - it->current_y;
6985 /* NLINES is the distance in number of lines. */
6986 nlines = it2.vpos - it->vpos;
6987
6988 /* Correct IT's y and vpos position
6989 so that they are relative to the starting point. */
6990 it->vpos -= nlines;
6991 it->current_y -= h;
6992
6993 if (dy == 0)
6994 {
6995 /* DY == 0 means move to the start of the screen line. The
6996 value of nlines is > 0 if continuation lines were involved. */
6997 if (nlines > 0)
6998 move_it_by_lines (it, nlines, 1);
6999 #if 0
7000 /* I think this assert is bogus if buffer contains
7001 invisible text or images. KFS. */
7002 xassert (IT_CHARPOS (*it) <= start_pos);
7003 #endif
7004 }
7005 else
7006 {
7007 /* The y-position we try to reach, relative to *IT.
7008 Note that H has been subtracted in front of the if-statement. */
7009 int target_y = it->current_y + h - dy;
7010 int y0 = it3.current_y;
7011 int y1 = line_bottom_y (&it3);
7012 int line_height = y1 - y0;
7013
7014 /* If we did not reach target_y, try to move further backward if
7015 we can. If we moved too far backward, try to move forward. */
7016 if (target_y < it->current_y
7017 /* This is heuristic. In a window that's 3 lines high, with
7018 a line height of 13 pixels each, recentering with point
7019 on the bottom line will try to move -39/2 = 19 pixels
7020 backward. Try to avoid moving into the first line. */
7021 && (it->current_y - target_y
7022 > min (window_box_height (it->w), line_height * 2 / 3))
7023 && IT_CHARPOS (*it) > BEGV)
7024 {
7025 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7026 target_y - it->current_y));
7027 dy = it->current_y - target_y;
7028 goto move_further_back;
7029 }
7030 else if (target_y >= it->current_y + line_height
7031 && IT_CHARPOS (*it) < ZV)
7032 {
7033 /* Should move forward by at least one line, maybe more.
7034
7035 Note: Calling move_it_by_lines can be expensive on
7036 terminal frames, where compute_motion is used (via
7037 vmotion) to do the job, when there are very long lines
7038 and truncate-lines is nil. That's the reason for
7039 treating terminal frames specially here. */
7040
7041 if (!FRAME_WINDOW_P (it->f))
7042 move_it_vertically (it, target_y - (it->current_y + line_height));
7043 else
7044 {
7045 do
7046 {
7047 move_it_by_lines (it, 1, 1);
7048 }
7049 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7050 }
7051
7052 #if 0
7053 /* I think this assert is bogus if buffer contains
7054 invisible text or images. KFS. */
7055 xassert (IT_CHARPOS (*it) >= BEGV);
7056 #endif
7057 }
7058 }
7059 }
7060
7061
7062 /* Move IT by a specified amount of pixel lines DY. DY negative means
7063 move backwards. DY = 0 means move to start of screen line. At the
7064 end, IT will be on the start of a screen line. */
7065
7066 void
7067 move_it_vertically (it, dy)
7068 struct it *it;
7069 int dy;
7070 {
7071 if (dy <= 0)
7072 move_it_vertically_backward (it, -dy);
7073 else
7074 {
7075 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7076 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7077 MOVE_TO_POS | MOVE_TO_Y);
7078 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7079
7080 /* If buffer ends in ZV without a newline, move to the start of
7081 the line to satisfy the post-condition. */
7082 if (IT_CHARPOS (*it) == ZV
7083 && ZV > BEGV
7084 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7085 move_it_by_lines (it, 0, 0);
7086 }
7087 }
7088
7089
7090 /* Move iterator IT past the end of the text line it is in. */
7091
7092 void
7093 move_it_past_eol (it)
7094 struct it *it;
7095 {
7096 enum move_it_result rc;
7097
7098 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7099 if (rc == MOVE_NEWLINE_OR_CR)
7100 set_iterator_to_next (it, 0);
7101 }
7102
7103
7104 #if 0 /* Currently not used. */
7105
7106 /* Return non-zero if some text between buffer positions START_CHARPOS
7107 and END_CHARPOS is invisible. IT->window is the window for text
7108 property lookup. */
7109
7110 static int
7111 invisible_text_between_p (it, start_charpos, end_charpos)
7112 struct it *it;
7113 int start_charpos, end_charpos;
7114 {
7115 Lisp_Object prop, limit;
7116 int invisible_found_p;
7117
7118 xassert (it != NULL && start_charpos <= end_charpos);
7119
7120 /* Is text at START invisible? */
7121 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7122 it->window);
7123 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7124 invisible_found_p = 1;
7125 else
7126 {
7127 limit = Fnext_single_char_property_change (make_number (start_charpos),
7128 Qinvisible, Qnil,
7129 make_number (end_charpos));
7130 invisible_found_p = XFASTINT (limit) < end_charpos;
7131 }
7132
7133 return invisible_found_p;
7134 }
7135
7136 #endif /* 0 */
7137
7138
7139 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7140 negative means move up. DVPOS == 0 means move to the start of the
7141 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7142 NEED_Y_P is zero, IT->current_y will be left unchanged.
7143
7144 Further optimization ideas: If we would know that IT->f doesn't use
7145 a face with proportional font, we could be faster for
7146 truncate-lines nil. */
7147
7148 void
7149 move_it_by_lines (it, dvpos, need_y_p)
7150 struct it *it;
7151 int dvpos, need_y_p;
7152 {
7153 struct position pos;
7154
7155 /* The commented-out optimization uses vmotion on terminals. This
7156 gives bad results, because elements like it->what, on which
7157 callers such as pos_visible_p rely, aren't updated. */
7158 /* if (!FRAME_WINDOW_P (it->f))
7159 {
7160 struct text_pos textpos;
7161
7162 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7163 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7164 reseat (it, textpos, 1);
7165 it->vpos += pos.vpos;
7166 it->current_y += pos.vpos;
7167 }
7168 else */
7169
7170 if (dvpos == 0)
7171 {
7172 /* DVPOS == 0 means move to the start of the screen line. */
7173 move_it_vertically_backward (it, 0);
7174 xassert (it->current_x == 0 && it->hpos == 0);
7175 /* Let next call to line_bottom_y calculate real line height */
7176 last_height = 0;
7177 }
7178 else if (dvpos > 0)
7179 {
7180 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7181 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7182 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7183 }
7184 else
7185 {
7186 struct it it2;
7187 int start_charpos, i;
7188
7189 /* Start at the beginning of the screen line containing IT's
7190 position. This may actually move vertically backwards,
7191 in case of overlays, so adjust dvpos accordingly. */
7192 dvpos += it->vpos;
7193 move_it_vertically_backward (it, 0);
7194 dvpos -= it->vpos;
7195
7196 /* Go back -DVPOS visible lines and reseat the iterator there. */
7197 start_charpos = IT_CHARPOS (*it);
7198 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7199 back_to_previous_visible_line_start (it);
7200 reseat (it, it->current.pos, 1);
7201
7202 /* Move further back if we end up in a string or an image. */
7203 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7204 {
7205 /* First try to move to start of display line. */
7206 dvpos += it->vpos;
7207 move_it_vertically_backward (it, 0);
7208 dvpos -= it->vpos;
7209 if (IT_POS_VALID_AFTER_MOVE_P (it))
7210 break;
7211 /* If start of line is still in string or image,
7212 move further back. */
7213 back_to_previous_visible_line_start (it);
7214 reseat (it, it->current.pos, 1);
7215 dvpos--;
7216 }
7217
7218 it->current_x = it->hpos = 0;
7219
7220 /* Above call may have moved too far if continuation lines
7221 are involved. Scan forward and see if it did. */
7222 it2 = *it;
7223 it2.vpos = it2.current_y = 0;
7224 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7225 it->vpos -= it2.vpos;
7226 it->current_y -= it2.current_y;
7227 it->current_x = it->hpos = 0;
7228
7229 /* If we moved too far back, move IT some lines forward. */
7230 if (it2.vpos > -dvpos)
7231 {
7232 int delta = it2.vpos + dvpos;
7233 it2 = *it;
7234 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7235 /* Move back again if we got too far ahead. */
7236 if (IT_CHARPOS (*it) >= start_charpos)
7237 *it = it2;
7238 }
7239 }
7240 }
7241
7242 /* Return 1 if IT points into the middle of a display vector. */
7243
7244 int
7245 in_display_vector_p (it)
7246 struct it *it;
7247 {
7248 return (it->method == GET_FROM_DISPLAY_VECTOR
7249 && it->current.dpvec_index > 0
7250 && it->dpvec + it->current.dpvec_index != it->dpend);
7251 }
7252
7253 \f
7254 /***********************************************************************
7255 Messages
7256 ***********************************************************************/
7257
7258
7259 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7260 to *Messages*. */
7261
7262 void
7263 add_to_log (format, arg1, arg2)
7264 char *format;
7265 Lisp_Object arg1, arg2;
7266 {
7267 Lisp_Object args[3];
7268 Lisp_Object msg, fmt;
7269 char *buffer;
7270 int len;
7271 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7272 USE_SAFE_ALLOCA;
7273
7274 /* Do nothing if called asynchronously. Inserting text into
7275 a buffer may call after-change-functions and alike and
7276 that would means running Lisp asynchronously. */
7277 if (handling_signal)
7278 return;
7279
7280 fmt = msg = Qnil;
7281 GCPRO4 (fmt, msg, arg1, arg2);
7282
7283 args[0] = fmt = build_string (format);
7284 args[1] = arg1;
7285 args[2] = arg2;
7286 msg = Fformat (3, args);
7287
7288 len = SBYTES (msg) + 1;
7289 SAFE_ALLOCA (buffer, char *, len);
7290 bcopy (SDATA (msg), buffer, len);
7291
7292 message_dolog (buffer, len - 1, 1, 0);
7293 SAFE_FREE ();
7294
7295 UNGCPRO;
7296 }
7297
7298
7299 /* Output a newline in the *Messages* buffer if "needs" one. */
7300
7301 void
7302 message_log_maybe_newline ()
7303 {
7304 if (message_log_need_newline)
7305 message_dolog ("", 0, 1, 0);
7306 }
7307
7308
7309 /* Add a string M of length NBYTES to the message log, optionally
7310 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7311 nonzero, means interpret the contents of M as multibyte. This
7312 function calls low-level routines in order to bypass text property
7313 hooks, etc. which might not be safe to run.
7314
7315 This may GC (insert may run before/after change hooks),
7316 so the buffer M must NOT point to a Lisp string. */
7317
7318 void
7319 message_dolog (m, nbytes, nlflag, multibyte)
7320 const char *m;
7321 int nbytes, nlflag, multibyte;
7322 {
7323 if (!NILP (Vmemory_full))
7324 return;
7325
7326 if (!NILP (Vmessage_log_max))
7327 {
7328 struct buffer *oldbuf;
7329 Lisp_Object oldpoint, oldbegv, oldzv;
7330 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7331 int point_at_end = 0;
7332 int zv_at_end = 0;
7333 Lisp_Object old_deactivate_mark, tem;
7334 struct gcpro gcpro1;
7335
7336 old_deactivate_mark = Vdeactivate_mark;
7337 oldbuf = current_buffer;
7338 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7339 current_buffer->undo_list = Qt;
7340
7341 oldpoint = message_dolog_marker1;
7342 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7343 oldbegv = message_dolog_marker2;
7344 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7345 oldzv = message_dolog_marker3;
7346 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7347 GCPRO1 (old_deactivate_mark);
7348
7349 if (PT == Z)
7350 point_at_end = 1;
7351 if (ZV == Z)
7352 zv_at_end = 1;
7353
7354 BEGV = BEG;
7355 BEGV_BYTE = BEG_BYTE;
7356 ZV = Z;
7357 ZV_BYTE = Z_BYTE;
7358 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7359
7360 /* Insert the string--maybe converting multibyte to single byte
7361 or vice versa, so that all the text fits the buffer. */
7362 if (multibyte
7363 && NILP (current_buffer->enable_multibyte_characters))
7364 {
7365 int i, c, char_bytes;
7366 unsigned char work[1];
7367
7368 /* Convert a multibyte string to single-byte
7369 for the *Message* buffer. */
7370 for (i = 0; i < nbytes; i += char_bytes)
7371 {
7372 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7373 work[0] = (SINGLE_BYTE_CHAR_P (c)
7374 ? c
7375 : multibyte_char_to_unibyte (c, Qnil));
7376 insert_1_both (work, 1, 1, 1, 0, 0);
7377 }
7378 }
7379 else if (! multibyte
7380 && ! NILP (current_buffer->enable_multibyte_characters))
7381 {
7382 int i, c, char_bytes;
7383 unsigned char *msg = (unsigned char *) m;
7384 unsigned char str[MAX_MULTIBYTE_LENGTH];
7385 /* Convert a single-byte string to multibyte
7386 for the *Message* buffer. */
7387 for (i = 0; i < nbytes; i++)
7388 {
7389 c = unibyte_char_to_multibyte (msg[i]);
7390 char_bytes = CHAR_STRING (c, str);
7391 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7392 }
7393 }
7394 else if (nbytes)
7395 insert_1 (m, nbytes, 1, 0, 0);
7396
7397 if (nlflag)
7398 {
7399 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7400 insert_1 ("\n", 1, 1, 0, 0);
7401
7402 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7403 this_bol = PT;
7404 this_bol_byte = PT_BYTE;
7405
7406 /* See if this line duplicates the previous one.
7407 If so, combine duplicates. */
7408 if (this_bol > BEG)
7409 {
7410 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7411 prev_bol = PT;
7412 prev_bol_byte = PT_BYTE;
7413
7414 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7415 this_bol, this_bol_byte);
7416 if (dup)
7417 {
7418 del_range_both (prev_bol, prev_bol_byte,
7419 this_bol, this_bol_byte, 0);
7420 if (dup > 1)
7421 {
7422 char dupstr[40];
7423 int duplen;
7424
7425 /* If you change this format, don't forget to also
7426 change message_log_check_duplicate. */
7427 sprintf (dupstr, " [%d times]", dup);
7428 duplen = strlen (dupstr);
7429 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7430 insert_1 (dupstr, duplen, 1, 0, 1);
7431 }
7432 }
7433 }
7434
7435 /* If we have more than the desired maximum number of lines
7436 in the *Messages* buffer now, delete the oldest ones.
7437 This is safe because we don't have undo in this buffer. */
7438
7439 if (NATNUMP (Vmessage_log_max))
7440 {
7441 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7442 -XFASTINT (Vmessage_log_max) - 1, 0);
7443 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7444 }
7445 }
7446 BEGV = XMARKER (oldbegv)->charpos;
7447 BEGV_BYTE = marker_byte_position (oldbegv);
7448
7449 if (zv_at_end)
7450 {
7451 ZV = Z;
7452 ZV_BYTE = Z_BYTE;
7453 }
7454 else
7455 {
7456 ZV = XMARKER (oldzv)->charpos;
7457 ZV_BYTE = marker_byte_position (oldzv);
7458 }
7459
7460 if (point_at_end)
7461 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7462 else
7463 /* We can't do Fgoto_char (oldpoint) because it will run some
7464 Lisp code. */
7465 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7466 XMARKER (oldpoint)->bytepos);
7467
7468 UNGCPRO;
7469 unchain_marker (XMARKER (oldpoint));
7470 unchain_marker (XMARKER (oldbegv));
7471 unchain_marker (XMARKER (oldzv));
7472
7473 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7474 set_buffer_internal (oldbuf);
7475 if (NILP (tem))
7476 windows_or_buffers_changed = old_windows_or_buffers_changed;
7477 message_log_need_newline = !nlflag;
7478 Vdeactivate_mark = old_deactivate_mark;
7479 }
7480 }
7481
7482
7483 /* We are at the end of the buffer after just having inserted a newline.
7484 (Note: We depend on the fact we won't be crossing the gap.)
7485 Check to see if the most recent message looks a lot like the previous one.
7486 Return 0 if different, 1 if the new one should just replace it, or a
7487 value N > 1 if we should also append " [N times]". */
7488
7489 static int
7490 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7491 int prev_bol, this_bol;
7492 int prev_bol_byte, this_bol_byte;
7493 {
7494 int i;
7495 int len = Z_BYTE - 1 - this_bol_byte;
7496 int seen_dots = 0;
7497 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7498 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7499
7500 for (i = 0; i < len; i++)
7501 {
7502 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7503 seen_dots = 1;
7504 if (p1[i] != p2[i])
7505 return seen_dots;
7506 }
7507 p1 += len;
7508 if (*p1 == '\n')
7509 return 2;
7510 if (*p1++ == ' ' && *p1++ == '[')
7511 {
7512 int n = 0;
7513 while (*p1 >= '0' && *p1 <= '9')
7514 n = n * 10 + *p1++ - '0';
7515 if (strncmp (p1, " times]\n", 8) == 0)
7516 return n+1;
7517 }
7518 return 0;
7519 }
7520 \f
7521
7522 /* Display an echo area message M with a specified length of NBYTES
7523 bytes. The string may include null characters. If M is 0, clear
7524 out any existing message, and let the mini-buffer text show
7525 through.
7526
7527 This may GC, so the buffer M must NOT point to a Lisp string. */
7528
7529 void
7530 message2 (m, nbytes, multibyte)
7531 const char *m;
7532 int nbytes;
7533 int multibyte;
7534 {
7535 /* First flush out any partial line written with print. */
7536 message_log_maybe_newline ();
7537 if (m)
7538 message_dolog (m, nbytes, 1, multibyte);
7539 message2_nolog (m, nbytes, multibyte);
7540 }
7541
7542
7543 /* The non-logging counterpart of message2. */
7544
7545 void
7546 message2_nolog (m, nbytes, multibyte)
7547 const char *m;
7548 int nbytes, multibyte;
7549 {
7550 struct frame *sf = SELECTED_FRAME ();
7551 message_enable_multibyte = multibyte;
7552
7553 if (noninteractive)
7554 {
7555 if (noninteractive_need_newline)
7556 putc ('\n', stderr);
7557 noninteractive_need_newline = 0;
7558 if (m)
7559 fwrite (m, nbytes, 1, stderr);
7560 if (cursor_in_echo_area == 0)
7561 fprintf (stderr, "\n");
7562 fflush (stderr);
7563 }
7564 /* A null message buffer means that the frame hasn't really been
7565 initialized yet. Error messages get reported properly by
7566 cmd_error, so this must be just an informative message; toss it. */
7567 else if (INTERACTIVE
7568 && sf->glyphs_initialized_p
7569 && FRAME_MESSAGE_BUF (sf))
7570 {
7571 Lisp_Object mini_window;
7572 struct frame *f;
7573
7574 /* Get the frame containing the mini-buffer
7575 that the selected frame is using. */
7576 mini_window = FRAME_MINIBUF_WINDOW (sf);
7577 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7578
7579 FRAME_SAMPLE_VISIBILITY (f);
7580 if (FRAME_VISIBLE_P (sf)
7581 && ! FRAME_VISIBLE_P (f))
7582 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7583
7584 if (m)
7585 {
7586 set_message (m, Qnil, nbytes, multibyte);
7587 if (minibuffer_auto_raise)
7588 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7589 }
7590 else
7591 clear_message (1, 1);
7592
7593 do_pending_window_change (0);
7594 echo_area_display (1);
7595 do_pending_window_change (0);
7596 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7597 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7598 }
7599 }
7600
7601
7602 /* Display an echo area message M with a specified length of NBYTES
7603 bytes. The string may include null characters. If M is not a
7604 string, clear out any existing message, and let the mini-buffer
7605 text show through.
7606
7607 This function cancels echoing. */
7608
7609 void
7610 message3 (m, nbytes, multibyte)
7611 Lisp_Object m;
7612 int nbytes;
7613 int multibyte;
7614 {
7615 struct gcpro gcpro1;
7616
7617 GCPRO1 (m);
7618 clear_message (1,1);
7619 cancel_echoing ();
7620
7621 /* First flush out any partial line written with print. */
7622 message_log_maybe_newline ();
7623 if (STRINGP (m))
7624 {
7625 char *buffer;
7626 USE_SAFE_ALLOCA;
7627
7628 SAFE_ALLOCA (buffer, char *, nbytes);
7629 bcopy (SDATA (m), buffer, nbytes);
7630 message_dolog (buffer, nbytes, 1, multibyte);
7631 SAFE_FREE ();
7632 }
7633 message3_nolog (m, nbytes, multibyte);
7634
7635 UNGCPRO;
7636 }
7637
7638
7639 /* The non-logging version of message3.
7640 This does not cancel echoing, because it is used for echoing.
7641 Perhaps we need to make a separate function for echoing
7642 and make this cancel echoing. */
7643
7644 void
7645 message3_nolog (m, nbytes, multibyte)
7646 Lisp_Object m;
7647 int nbytes, multibyte;
7648 {
7649 struct frame *sf = SELECTED_FRAME ();
7650 message_enable_multibyte = multibyte;
7651
7652 if (noninteractive)
7653 {
7654 if (noninteractive_need_newline)
7655 putc ('\n', stderr);
7656 noninteractive_need_newline = 0;
7657 if (STRINGP (m))
7658 fwrite (SDATA (m), nbytes, 1, stderr);
7659 if (cursor_in_echo_area == 0)
7660 fprintf (stderr, "\n");
7661 fflush (stderr);
7662 }
7663 /* A null message buffer means that the frame hasn't really been
7664 initialized yet. Error messages get reported properly by
7665 cmd_error, so this must be just an informative message; toss it. */
7666 else if (INTERACTIVE
7667 && sf->glyphs_initialized_p
7668 && FRAME_MESSAGE_BUF (sf))
7669 {
7670 Lisp_Object mini_window;
7671 Lisp_Object frame;
7672 struct frame *f;
7673
7674 /* Get the frame containing the mini-buffer
7675 that the selected frame is using. */
7676 mini_window = FRAME_MINIBUF_WINDOW (sf);
7677 frame = XWINDOW (mini_window)->frame;
7678 f = XFRAME (frame);
7679
7680 FRAME_SAMPLE_VISIBILITY (f);
7681 if (FRAME_VISIBLE_P (sf)
7682 && !FRAME_VISIBLE_P (f))
7683 Fmake_frame_visible (frame);
7684
7685 if (STRINGP (m) && SCHARS (m) > 0)
7686 {
7687 set_message (NULL, m, nbytes, multibyte);
7688 if (minibuffer_auto_raise)
7689 Fraise_frame (frame);
7690 /* Assume we are not echoing.
7691 (If we are, echo_now will override this.) */
7692 echo_message_buffer = Qnil;
7693 }
7694 else
7695 clear_message (1, 1);
7696
7697 do_pending_window_change (0);
7698 echo_area_display (1);
7699 do_pending_window_change (0);
7700 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7701 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7702 }
7703 }
7704
7705
7706 /* Display a null-terminated echo area message M. If M is 0, clear
7707 out any existing message, and let the mini-buffer text show through.
7708
7709 The buffer M must continue to exist until after the echo area gets
7710 cleared or some other message gets displayed there. Do not pass
7711 text that is stored in a Lisp string. Do not pass text in a buffer
7712 that was alloca'd. */
7713
7714 void
7715 message1 (m)
7716 char *m;
7717 {
7718 message2 (m, (m ? strlen (m) : 0), 0);
7719 }
7720
7721
7722 /* The non-logging counterpart of message1. */
7723
7724 void
7725 message1_nolog (m)
7726 char *m;
7727 {
7728 message2_nolog (m, (m ? strlen (m) : 0), 0);
7729 }
7730
7731 /* Display a message M which contains a single %s
7732 which gets replaced with STRING. */
7733
7734 void
7735 message_with_string (m, string, log)
7736 char *m;
7737 Lisp_Object string;
7738 int log;
7739 {
7740 CHECK_STRING (string);
7741
7742 if (noninteractive)
7743 {
7744 if (m)
7745 {
7746 if (noninteractive_need_newline)
7747 putc ('\n', stderr);
7748 noninteractive_need_newline = 0;
7749 fprintf (stderr, m, SDATA (string));
7750 if (cursor_in_echo_area == 0)
7751 fprintf (stderr, "\n");
7752 fflush (stderr);
7753 }
7754 }
7755 else if (INTERACTIVE)
7756 {
7757 /* The frame whose minibuffer we're going to display the message on.
7758 It may be larger than the selected frame, so we need
7759 to use its buffer, not the selected frame's buffer. */
7760 Lisp_Object mini_window;
7761 struct frame *f, *sf = SELECTED_FRAME ();
7762
7763 /* Get the frame containing the minibuffer
7764 that the selected frame is using. */
7765 mini_window = FRAME_MINIBUF_WINDOW (sf);
7766 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7767
7768 /* A null message buffer means that the frame hasn't really been
7769 initialized yet. Error messages get reported properly by
7770 cmd_error, so this must be just an informative message; toss it. */
7771 if (FRAME_MESSAGE_BUF (f))
7772 {
7773 Lisp_Object args[2], message;
7774 struct gcpro gcpro1, gcpro2;
7775
7776 args[0] = build_string (m);
7777 args[1] = message = string;
7778 GCPRO2 (args[0], message);
7779 gcpro1.nvars = 2;
7780
7781 message = Fformat (2, args);
7782
7783 if (log)
7784 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7785 else
7786 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7787
7788 UNGCPRO;
7789
7790 /* Print should start at the beginning of the message
7791 buffer next time. */
7792 message_buf_print = 0;
7793 }
7794 }
7795 }
7796
7797
7798 /* Dump an informative message to the minibuf. If M is 0, clear out
7799 any existing message, and let the mini-buffer text show through. */
7800
7801 /* VARARGS 1 */
7802 void
7803 message (m, a1, a2, a3)
7804 char *m;
7805 EMACS_INT a1, a2, a3;
7806 {
7807 if (noninteractive)
7808 {
7809 if (m)
7810 {
7811 if (noninteractive_need_newline)
7812 putc ('\n', stderr);
7813 noninteractive_need_newline = 0;
7814 fprintf (stderr, m, a1, a2, a3);
7815 if (cursor_in_echo_area == 0)
7816 fprintf (stderr, "\n");
7817 fflush (stderr);
7818 }
7819 }
7820 else if (INTERACTIVE)
7821 {
7822 /* The frame whose mini-buffer we're going to display the message
7823 on. It may be larger than the selected frame, so we need to
7824 use its buffer, not the selected frame's buffer. */
7825 Lisp_Object mini_window;
7826 struct frame *f, *sf = SELECTED_FRAME ();
7827
7828 /* Get the frame containing the mini-buffer
7829 that the selected frame is using. */
7830 mini_window = FRAME_MINIBUF_WINDOW (sf);
7831 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7832
7833 /* A null message buffer means that the frame hasn't really been
7834 initialized yet. Error messages get reported properly by
7835 cmd_error, so this must be just an informative message; toss
7836 it. */
7837 if (FRAME_MESSAGE_BUF (f))
7838 {
7839 if (m)
7840 {
7841 int len;
7842 #ifdef NO_ARG_ARRAY
7843 char *a[3];
7844 a[0] = (char *) a1;
7845 a[1] = (char *) a2;
7846 a[2] = (char *) a3;
7847
7848 len = doprnt (FRAME_MESSAGE_BUF (f),
7849 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7850 #else
7851 len = doprnt (FRAME_MESSAGE_BUF (f),
7852 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7853 (char **) &a1);
7854 #endif /* NO_ARG_ARRAY */
7855
7856 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7857 }
7858 else
7859 message1 (0);
7860
7861 /* Print should start at the beginning of the message
7862 buffer next time. */
7863 message_buf_print = 0;
7864 }
7865 }
7866 }
7867
7868
7869 /* The non-logging version of message. */
7870
7871 void
7872 message_nolog (m, a1, a2, a3)
7873 char *m;
7874 EMACS_INT a1, a2, a3;
7875 {
7876 Lisp_Object old_log_max;
7877 old_log_max = Vmessage_log_max;
7878 Vmessage_log_max = Qnil;
7879 message (m, a1, a2, a3);
7880 Vmessage_log_max = old_log_max;
7881 }
7882
7883
7884 /* Display the current message in the current mini-buffer. This is
7885 only called from error handlers in process.c, and is not time
7886 critical. */
7887
7888 void
7889 update_echo_area ()
7890 {
7891 if (!NILP (echo_area_buffer[0]))
7892 {
7893 Lisp_Object string;
7894 string = Fcurrent_message ();
7895 message3 (string, SBYTES (string),
7896 !NILP (current_buffer->enable_multibyte_characters));
7897 }
7898 }
7899
7900
7901 /* Make sure echo area buffers in `echo_buffers' are live.
7902 If they aren't, make new ones. */
7903
7904 static void
7905 ensure_echo_area_buffers ()
7906 {
7907 int i;
7908
7909 for (i = 0; i < 2; ++i)
7910 if (!BUFFERP (echo_buffer[i])
7911 || NILP (XBUFFER (echo_buffer[i])->name))
7912 {
7913 char name[30];
7914 Lisp_Object old_buffer;
7915 int j;
7916
7917 old_buffer = echo_buffer[i];
7918 sprintf (name, " *Echo Area %d*", i);
7919 echo_buffer[i] = Fget_buffer_create (build_string (name));
7920 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7921
7922 for (j = 0; j < 2; ++j)
7923 if (EQ (old_buffer, echo_area_buffer[j]))
7924 echo_area_buffer[j] = echo_buffer[i];
7925 }
7926 }
7927
7928
7929 /* Call FN with args A1..A4 with either the current or last displayed
7930 echo_area_buffer as current buffer.
7931
7932 WHICH zero means use the current message buffer
7933 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7934 from echo_buffer[] and clear it.
7935
7936 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7937 suitable buffer from echo_buffer[] and clear it.
7938
7939 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7940 that the current message becomes the last displayed one, make
7941 choose a suitable buffer for echo_area_buffer[0], and clear it.
7942
7943 Value is what FN returns. */
7944
7945 static int
7946 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7947 struct window *w;
7948 int which;
7949 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7950 EMACS_INT a1;
7951 Lisp_Object a2;
7952 EMACS_INT a3, a4;
7953 {
7954 Lisp_Object buffer;
7955 int this_one, the_other, clear_buffer_p, rc;
7956 int count = SPECPDL_INDEX ();
7957
7958 /* If buffers aren't live, make new ones. */
7959 ensure_echo_area_buffers ();
7960
7961 clear_buffer_p = 0;
7962
7963 if (which == 0)
7964 this_one = 0, the_other = 1;
7965 else if (which > 0)
7966 this_one = 1, the_other = 0;
7967 else
7968 {
7969 this_one = 0, the_other = 1;
7970 clear_buffer_p = 1;
7971
7972 /* We need a fresh one in case the current echo buffer equals
7973 the one containing the last displayed echo area message. */
7974 if (!NILP (echo_area_buffer[this_one])
7975 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7976 echo_area_buffer[this_one] = Qnil;
7977 }
7978
7979 /* Choose a suitable buffer from echo_buffer[] is we don't
7980 have one. */
7981 if (NILP (echo_area_buffer[this_one]))
7982 {
7983 echo_area_buffer[this_one]
7984 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7985 ? echo_buffer[the_other]
7986 : echo_buffer[this_one]);
7987 clear_buffer_p = 1;
7988 }
7989
7990 buffer = echo_area_buffer[this_one];
7991
7992 /* Don't get confused by reusing the buffer used for echoing
7993 for a different purpose. */
7994 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7995 cancel_echoing ();
7996
7997 record_unwind_protect (unwind_with_echo_area_buffer,
7998 with_echo_area_buffer_unwind_data (w));
7999
8000 /* Make the echo area buffer current. Note that for display
8001 purposes, it is not necessary that the displayed window's buffer
8002 == current_buffer, except for text property lookup. So, let's
8003 only set that buffer temporarily here without doing a full
8004 Fset_window_buffer. We must also change w->pointm, though,
8005 because otherwise an assertions in unshow_buffer fails, and Emacs
8006 aborts. */
8007 set_buffer_internal_1 (XBUFFER (buffer));
8008 if (w)
8009 {
8010 w->buffer = buffer;
8011 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8012 }
8013
8014 current_buffer->undo_list = Qt;
8015 current_buffer->read_only = Qnil;
8016 specbind (Qinhibit_read_only, Qt);
8017 specbind (Qinhibit_modification_hooks, Qt);
8018
8019 if (clear_buffer_p && Z > BEG)
8020 del_range (BEG, Z);
8021
8022 xassert (BEGV >= BEG);
8023 xassert (ZV <= Z && ZV >= BEGV);
8024
8025 rc = fn (a1, a2, a3, a4);
8026
8027 xassert (BEGV >= BEG);
8028 xassert (ZV <= Z && ZV >= BEGV);
8029
8030 unbind_to (count, Qnil);
8031 return rc;
8032 }
8033
8034
8035 /* Save state that should be preserved around the call to the function
8036 FN called in with_echo_area_buffer. */
8037
8038 static Lisp_Object
8039 with_echo_area_buffer_unwind_data (w)
8040 struct window *w;
8041 {
8042 int i = 0;
8043 Lisp_Object vector;
8044
8045 /* Reduce consing by keeping one vector in
8046 Vwith_echo_area_save_vector. */
8047 vector = Vwith_echo_area_save_vector;
8048 Vwith_echo_area_save_vector = Qnil;
8049
8050 if (NILP (vector))
8051 vector = Fmake_vector (make_number (7), Qnil);
8052
8053 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8054 AREF (vector, i) = Vdeactivate_mark, ++i;
8055 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8056
8057 if (w)
8058 {
8059 XSETWINDOW (AREF (vector, i), w); ++i;
8060 AREF (vector, i) = w->buffer; ++i;
8061 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8062 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8063 }
8064 else
8065 {
8066 int end = i + 4;
8067 for (; i < end; ++i)
8068 AREF (vector, i) = Qnil;
8069 }
8070
8071 xassert (i == ASIZE (vector));
8072 return vector;
8073 }
8074
8075
8076 /* Restore global state from VECTOR which was created by
8077 with_echo_area_buffer_unwind_data. */
8078
8079 static Lisp_Object
8080 unwind_with_echo_area_buffer (vector)
8081 Lisp_Object vector;
8082 {
8083 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8084 Vdeactivate_mark = AREF (vector, 1);
8085 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8086
8087 if (WINDOWP (AREF (vector, 3)))
8088 {
8089 struct window *w;
8090 Lisp_Object buffer, charpos, bytepos;
8091
8092 w = XWINDOW (AREF (vector, 3));
8093 buffer = AREF (vector, 4);
8094 charpos = AREF (vector, 5);
8095 bytepos = AREF (vector, 6);
8096
8097 w->buffer = buffer;
8098 set_marker_both (w->pointm, buffer,
8099 XFASTINT (charpos), XFASTINT (bytepos));
8100 }
8101
8102 Vwith_echo_area_save_vector = vector;
8103 return Qnil;
8104 }
8105
8106
8107 /* Set up the echo area for use by print functions. MULTIBYTE_P
8108 non-zero means we will print multibyte. */
8109
8110 void
8111 setup_echo_area_for_printing (multibyte_p)
8112 int multibyte_p;
8113 {
8114 /* If we can't find an echo area any more, exit. */
8115 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8116 Fkill_emacs (Qnil);
8117
8118 ensure_echo_area_buffers ();
8119
8120 if (!message_buf_print)
8121 {
8122 /* A message has been output since the last time we printed.
8123 Choose a fresh echo area buffer. */
8124 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8125 echo_area_buffer[0] = echo_buffer[1];
8126 else
8127 echo_area_buffer[0] = echo_buffer[0];
8128
8129 /* Switch to that buffer and clear it. */
8130 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8131 current_buffer->truncate_lines = Qnil;
8132
8133 if (Z > BEG)
8134 {
8135 int count = SPECPDL_INDEX ();
8136 specbind (Qinhibit_read_only, Qt);
8137 /* Note that undo recording is always disabled. */
8138 del_range (BEG, Z);
8139 unbind_to (count, Qnil);
8140 }
8141 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8142
8143 /* Set up the buffer for the multibyteness we need. */
8144 if (multibyte_p
8145 != !NILP (current_buffer->enable_multibyte_characters))
8146 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8147
8148 /* Raise the frame containing the echo area. */
8149 if (minibuffer_auto_raise)
8150 {
8151 struct frame *sf = SELECTED_FRAME ();
8152 Lisp_Object mini_window;
8153 mini_window = FRAME_MINIBUF_WINDOW (sf);
8154 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8155 }
8156
8157 message_log_maybe_newline ();
8158 message_buf_print = 1;
8159 }
8160 else
8161 {
8162 if (NILP (echo_area_buffer[0]))
8163 {
8164 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8165 echo_area_buffer[0] = echo_buffer[1];
8166 else
8167 echo_area_buffer[0] = echo_buffer[0];
8168 }
8169
8170 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8171 {
8172 /* Someone switched buffers between print requests. */
8173 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8174 current_buffer->truncate_lines = Qnil;
8175 }
8176 }
8177 }
8178
8179
8180 /* Display an echo area message in window W. Value is non-zero if W's
8181 height is changed. If display_last_displayed_message_p is
8182 non-zero, display the message that was last displayed, otherwise
8183 display the current message. */
8184
8185 static int
8186 display_echo_area (w)
8187 struct window *w;
8188 {
8189 int i, no_message_p, window_height_changed_p, count;
8190
8191 /* Temporarily disable garbage collections while displaying the echo
8192 area. This is done because a GC can print a message itself.
8193 That message would modify the echo area buffer's contents while a
8194 redisplay of the buffer is going on, and seriously confuse
8195 redisplay. */
8196 count = inhibit_garbage_collection ();
8197
8198 /* If there is no message, we must call display_echo_area_1
8199 nevertheless because it resizes the window. But we will have to
8200 reset the echo_area_buffer in question to nil at the end because
8201 with_echo_area_buffer will sets it to an empty buffer. */
8202 i = display_last_displayed_message_p ? 1 : 0;
8203 no_message_p = NILP (echo_area_buffer[i]);
8204
8205 window_height_changed_p
8206 = with_echo_area_buffer (w, display_last_displayed_message_p,
8207 display_echo_area_1,
8208 (EMACS_INT) w, Qnil, 0, 0);
8209
8210 if (no_message_p)
8211 echo_area_buffer[i] = Qnil;
8212
8213 unbind_to (count, Qnil);
8214 return window_height_changed_p;
8215 }
8216
8217
8218 /* Helper for display_echo_area. Display the current buffer which
8219 contains the current echo area message in window W, a mini-window,
8220 a pointer to which is passed in A1. A2..A4 are currently not used.
8221 Change the height of W so that all of the message is displayed.
8222 Value is non-zero if height of W was changed. */
8223
8224 static int
8225 display_echo_area_1 (a1, a2, a3, a4)
8226 EMACS_INT a1;
8227 Lisp_Object a2;
8228 EMACS_INT a3, a4;
8229 {
8230 struct window *w = (struct window *) a1;
8231 Lisp_Object window;
8232 struct text_pos start;
8233 int window_height_changed_p = 0;
8234
8235 /* Do this before displaying, so that we have a large enough glyph
8236 matrix for the display. If we can't get enough space for the
8237 whole text, display the last N lines. That works by setting w->start. */
8238 window_height_changed_p = resize_mini_window (w, 0);
8239
8240 /* Use the starting position chosen by resize_mini_window. */
8241 SET_TEXT_POS_FROM_MARKER (start, w->start);
8242
8243 /* Display. */
8244 clear_glyph_matrix (w->desired_matrix);
8245 XSETWINDOW (window, w);
8246 try_window (window, start, 0);
8247
8248 return window_height_changed_p;
8249 }
8250
8251
8252 /* Resize the echo area window to exactly the size needed for the
8253 currently displayed message, if there is one. If a mini-buffer
8254 is active, don't shrink it. */
8255
8256 void
8257 resize_echo_area_exactly ()
8258 {
8259 if (BUFFERP (echo_area_buffer[0])
8260 && WINDOWP (echo_area_window))
8261 {
8262 struct window *w = XWINDOW (echo_area_window);
8263 int resized_p;
8264 Lisp_Object resize_exactly;
8265
8266 if (minibuf_level == 0)
8267 resize_exactly = Qt;
8268 else
8269 resize_exactly = Qnil;
8270
8271 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8272 (EMACS_INT) w, resize_exactly, 0, 0);
8273 if (resized_p)
8274 {
8275 ++windows_or_buffers_changed;
8276 ++update_mode_lines;
8277 redisplay_internal (0);
8278 }
8279 }
8280 }
8281
8282
8283 /* Callback function for with_echo_area_buffer, when used from
8284 resize_echo_area_exactly. A1 contains a pointer to the window to
8285 resize, EXACTLY non-nil means resize the mini-window exactly to the
8286 size of the text displayed. A3 and A4 are not used. Value is what
8287 resize_mini_window returns. */
8288
8289 static int
8290 resize_mini_window_1 (a1, exactly, a3, a4)
8291 EMACS_INT a1;
8292 Lisp_Object exactly;
8293 EMACS_INT a3, a4;
8294 {
8295 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8296 }
8297
8298
8299 /* Resize mini-window W to fit the size of its contents. EXACT:P
8300 means size the window exactly to the size needed. Otherwise, it's
8301 only enlarged until W's buffer is empty.
8302
8303 Set W->start to the right place to begin display. If the whole
8304 contents fit, start at the beginning. Otherwise, start so as
8305 to make the end of the contents appear. This is particularly
8306 important for y-or-n-p, but seems desirable generally.
8307
8308 Value is non-zero if the window height has been changed. */
8309
8310 int
8311 resize_mini_window (w, exact_p)
8312 struct window *w;
8313 int exact_p;
8314 {
8315 struct frame *f = XFRAME (w->frame);
8316 int window_height_changed_p = 0;
8317
8318 xassert (MINI_WINDOW_P (w));
8319
8320 /* By default, start display at the beginning. */
8321 set_marker_both (w->start, w->buffer,
8322 BUF_BEGV (XBUFFER (w->buffer)),
8323 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8324
8325 /* Don't resize windows while redisplaying a window; it would
8326 confuse redisplay functions when the size of the window they are
8327 displaying changes from under them. Such a resizing can happen,
8328 for instance, when which-func prints a long message while
8329 we are running fontification-functions. We're running these
8330 functions with safe_call which binds inhibit-redisplay to t. */
8331 if (!NILP (Vinhibit_redisplay))
8332 return 0;
8333
8334 /* Nil means don't try to resize. */
8335 if (NILP (Vresize_mini_windows)
8336 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8337 return 0;
8338
8339 if (!FRAME_MINIBUF_ONLY_P (f))
8340 {
8341 struct it it;
8342 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8343 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8344 int height, max_height;
8345 int unit = FRAME_LINE_HEIGHT (f);
8346 struct text_pos start;
8347 struct buffer *old_current_buffer = NULL;
8348
8349 if (current_buffer != XBUFFER (w->buffer))
8350 {
8351 old_current_buffer = current_buffer;
8352 set_buffer_internal (XBUFFER (w->buffer));
8353 }
8354
8355 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8356
8357 /* Compute the max. number of lines specified by the user. */
8358 if (FLOATP (Vmax_mini_window_height))
8359 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8360 else if (INTEGERP (Vmax_mini_window_height))
8361 max_height = XINT (Vmax_mini_window_height);
8362 else
8363 max_height = total_height / 4;
8364
8365 /* Correct that max. height if it's bogus. */
8366 max_height = max (1, max_height);
8367 max_height = min (total_height, max_height);
8368
8369 /* Find out the height of the text in the window. */
8370 if (it.truncate_lines_p)
8371 height = 1;
8372 else
8373 {
8374 last_height = 0;
8375 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8376 if (it.max_ascent == 0 && it.max_descent == 0)
8377 height = it.current_y + last_height;
8378 else
8379 height = it.current_y + it.max_ascent + it.max_descent;
8380 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8381 height = (height + unit - 1) / unit;
8382 }
8383
8384 /* Compute a suitable window start. */
8385 if (height > max_height)
8386 {
8387 height = max_height;
8388 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8389 move_it_vertically_backward (&it, (height - 1) * unit);
8390 start = it.current.pos;
8391 }
8392 else
8393 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8394 SET_MARKER_FROM_TEXT_POS (w->start, start);
8395
8396 if (EQ (Vresize_mini_windows, Qgrow_only))
8397 {
8398 /* Let it grow only, until we display an empty message, in which
8399 case the window shrinks again. */
8400 if (height > WINDOW_TOTAL_LINES (w))
8401 {
8402 int old_height = WINDOW_TOTAL_LINES (w);
8403 freeze_window_starts (f, 1);
8404 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8405 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8406 }
8407 else if (height < WINDOW_TOTAL_LINES (w)
8408 && (exact_p || BEGV == ZV))
8409 {
8410 int old_height = WINDOW_TOTAL_LINES (w);
8411 freeze_window_starts (f, 0);
8412 shrink_mini_window (w);
8413 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8414 }
8415 }
8416 else
8417 {
8418 /* Always resize to exact size needed. */
8419 if (height > WINDOW_TOTAL_LINES (w))
8420 {
8421 int old_height = WINDOW_TOTAL_LINES (w);
8422 freeze_window_starts (f, 1);
8423 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8424 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8425 }
8426 else if (height < WINDOW_TOTAL_LINES (w))
8427 {
8428 int old_height = WINDOW_TOTAL_LINES (w);
8429 freeze_window_starts (f, 0);
8430 shrink_mini_window (w);
8431
8432 if (height)
8433 {
8434 freeze_window_starts (f, 1);
8435 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8436 }
8437
8438 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8439 }
8440 }
8441
8442 if (old_current_buffer)
8443 set_buffer_internal (old_current_buffer);
8444 }
8445
8446 return window_height_changed_p;
8447 }
8448
8449
8450 /* Value is the current message, a string, or nil if there is no
8451 current message. */
8452
8453 Lisp_Object
8454 current_message ()
8455 {
8456 Lisp_Object msg;
8457
8458 if (NILP (echo_area_buffer[0]))
8459 msg = Qnil;
8460 else
8461 {
8462 with_echo_area_buffer (0, 0, current_message_1,
8463 (EMACS_INT) &msg, Qnil, 0, 0);
8464 if (NILP (msg))
8465 echo_area_buffer[0] = Qnil;
8466 }
8467
8468 return msg;
8469 }
8470
8471
8472 static int
8473 current_message_1 (a1, a2, a3, a4)
8474 EMACS_INT a1;
8475 Lisp_Object a2;
8476 EMACS_INT a3, a4;
8477 {
8478 Lisp_Object *msg = (Lisp_Object *) a1;
8479
8480 if (Z > BEG)
8481 *msg = make_buffer_string (BEG, Z, 1);
8482 else
8483 *msg = Qnil;
8484 return 0;
8485 }
8486
8487
8488 /* Push the current message on Vmessage_stack for later restauration
8489 by restore_message. Value is non-zero if the current message isn't
8490 empty. This is a relatively infrequent operation, so it's not
8491 worth optimizing. */
8492
8493 int
8494 push_message ()
8495 {
8496 Lisp_Object msg;
8497 msg = current_message ();
8498 Vmessage_stack = Fcons (msg, Vmessage_stack);
8499 return STRINGP (msg);
8500 }
8501
8502
8503 /* Restore message display from the top of Vmessage_stack. */
8504
8505 void
8506 restore_message ()
8507 {
8508 Lisp_Object msg;
8509
8510 xassert (CONSP (Vmessage_stack));
8511 msg = XCAR (Vmessage_stack);
8512 if (STRINGP (msg))
8513 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8514 else
8515 message3_nolog (msg, 0, 0);
8516 }
8517
8518
8519 /* Handler for record_unwind_protect calling pop_message. */
8520
8521 Lisp_Object
8522 pop_message_unwind (dummy)
8523 Lisp_Object dummy;
8524 {
8525 pop_message ();
8526 return Qnil;
8527 }
8528
8529 /* Pop the top-most entry off Vmessage_stack. */
8530
8531 void
8532 pop_message ()
8533 {
8534 xassert (CONSP (Vmessage_stack));
8535 Vmessage_stack = XCDR (Vmessage_stack);
8536 }
8537
8538
8539 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8540 exits. If the stack is not empty, we have a missing pop_message
8541 somewhere. */
8542
8543 void
8544 check_message_stack ()
8545 {
8546 if (!NILP (Vmessage_stack))
8547 abort ();
8548 }
8549
8550
8551 /* Truncate to NCHARS what will be displayed in the echo area the next
8552 time we display it---but don't redisplay it now. */
8553
8554 void
8555 truncate_echo_area (nchars)
8556 int nchars;
8557 {
8558 if (nchars == 0)
8559 echo_area_buffer[0] = Qnil;
8560 /* A null message buffer means that the frame hasn't really been
8561 initialized yet. Error messages get reported properly by
8562 cmd_error, so this must be just an informative message; toss it. */
8563 else if (!noninteractive
8564 && INTERACTIVE
8565 && !NILP (echo_area_buffer[0]))
8566 {
8567 struct frame *sf = SELECTED_FRAME ();
8568 if (FRAME_MESSAGE_BUF (sf))
8569 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8570 }
8571 }
8572
8573
8574 /* Helper function for truncate_echo_area. Truncate the current
8575 message to at most NCHARS characters. */
8576
8577 static int
8578 truncate_message_1 (nchars, a2, a3, a4)
8579 EMACS_INT nchars;
8580 Lisp_Object a2;
8581 EMACS_INT a3, a4;
8582 {
8583 if (BEG + nchars < Z)
8584 del_range (BEG + nchars, Z);
8585 if (Z == BEG)
8586 echo_area_buffer[0] = Qnil;
8587 return 0;
8588 }
8589
8590
8591 /* Set the current message to a substring of S or STRING.
8592
8593 If STRING is a Lisp string, set the message to the first NBYTES
8594 bytes from STRING. NBYTES zero means use the whole string. If
8595 STRING is multibyte, the message will be displayed multibyte.
8596
8597 If S is not null, set the message to the first LEN bytes of S. LEN
8598 zero means use the whole string. MULTIBYTE_P non-zero means S is
8599 multibyte. Display the message multibyte in that case.
8600
8601 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8602 to t before calling set_message_1 (which calls insert).
8603 */
8604
8605 void
8606 set_message (s, string, nbytes, multibyte_p)
8607 const char *s;
8608 Lisp_Object string;
8609 int nbytes, multibyte_p;
8610 {
8611 message_enable_multibyte
8612 = ((s && multibyte_p)
8613 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8614
8615 with_echo_area_buffer (0, -1, set_message_1,
8616 (EMACS_INT) s, string, nbytes, multibyte_p);
8617 message_buf_print = 0;
8618 help_echo_showing_p = 0;
8619 }
8620
8621
8622 /* Helper function for set_message. Arguments have the same meaning
8623 as there, with A1 corresponding to S and A2 corresponding to STRING
8624 This function is called with the echo area buffer being
8625 current. */
8626
8627 static int
8628 set_message_1 (a1, a2, nbytes, multibyte_p)
8629 EMACS_INT a1;
8630 Lisp_Object a2;
8631 EMACS_INT nbytes, multibyte_p;
8632 {
8633 const char *s = (const char *) a1;
8634 Lisp_Object string = a2;
8635
8636 /* Change multibyteness of the echo buffer appropriately. */
8637 if (message_enable_multibyte
8638 != !NILP (current_buffer->enable_multibyte_characters))
8639 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8640
8641 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8642
8643 /* Insert new message at BEG. */
8644 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8645
8646 if (STRINGP (string))
8647 {
8648 int nchars;
8649
8650 if (nbytes == 0)
8651 nbytes = SBYTES (string);
8652 nchars = string_byte_to_char (string, nbytes);
8653
8654 /* This function takes care of single/multibyte conversion. We
8655 just have to ensure that the echo area buffer has the right
8656 setting of enable_multibyte_characters. */
8657 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8658 }
8659 else if (s)
8660 {
8661 if (nbytes == 0)
8662 nbytes = strlen (s);
8663
8664 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8665 {
8666 /* Convert from multi-byte to single-byte. */
8667 int i, c, n;
8668 unsigned char work[1];
8669
8670 /* Convert a multibyte string to single-byte. */
8671 for (i = 0; i < nbytes; i += n)
8672 {
8673 c = string_char_and_length (s + i, nbytes - i, &n);
8674 work[0] = (SINGLE_BYTE_CHAR_P (c)
8675 ? c
8676 : multibyte_char_to_unibyte (c, Qnil));
8677 insert_1_both (work, 1, 1, 1, 0, 0);
8678 }
8679 }
8680 else if (!multibyte_p
8681 && !NILP (current_buffer->enable_multibyte_characters))
8682 {
8683 /* Convert from single-byte to multi-byte. */
8684 int i, c, n;
8685 const unsigned char *msg = (const unsigned char *) s;
8686 unsigned char str[MAX_MULTIBYTE_LENGTH];
8687
8688 /* Convert a single-byte string to multibyte. */
8689 for (i = 0; i < nbytes; i++)
8690 {
8691 c = unibyte_char_to_multibyte (msg[i]);
8692 n = CHAR_STRING (c, str);
8693 insert_1_both (str, 1, n, 1, 0, 0);
8694 }
8695 }
8696 else
8697 insert_1 (s, nbytes, 1, 0, 0);
8698 }
8699
8700 return 0;
8701 }
8702
8703
8704 /* Clear messages. CURRENT_P non-zero means clear the current
8705 message. LAST_DISPLAYED_P non-zero means clear the message
8706 last displayed. */
8707
8708 void
8709 clear_message (current_p, last_displayed_p)
8710 int current_p, last_displayed_p;
8711 {
8712 if (current_p)
8713 {
8714 echo_area_buffer[0] = Qnil;
8715 message_cleared_p = 1;
8716 }
8717
8718 if (last_displayed_p)
8719 echo_area_buffer[1] = Qnil;
8720
8721 message_buf_print = 0;
8722 }
8723
8724 /* Clear garbaged frames.
8725
8726 This function is used where the old redisplay called
8727 redraw_garbaged_frames which in turn called redraw_frame which in
8728 turn called clear_frame. The call to clear_frame was a source of
8729 flickering. I believe a clear_frame is not necessary. It should
8730 suffice in the new redisplay to invalidate all current matrices,
8731 and ensure a complete redisplay of all windows. */
8732
8733 static void
8734 clear_garbaged_frames ()
8735 {
8736 if (frame_garbaged)
8737 {
8738 Lisp_Object tail, frame;
8739 int changed_count = 0;
8740
8741 FOR_EACH_FRAME (tail, frame)
8742 {
8743 struct frame *f = XFRAME (frame);
8744
8745 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8746 {
8747 if (f->resized_p)
8748 {
8749 Fredraw_frame (frame);
8750 f->force_flush_display_p = 1;
8751 }
8752 clear_current_matrices (f);
8753 changed_count++;
8754 f->garbaged = 0;
8755 f->resized_p = 0;
8756 }
8757 }
8758
8759 frame_garbaged = 0;
8760 if (changed_count)
8761 ++windows_or_buffers_changed;
8762 }
8763 }
8764
8765
8766 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8767 is non-zero update selected_frame. Value is non-zero if the
8768 mini-windows height has been changed. */
8769
8770 static int
8771 echo_area_display (update_frame_p)
8772 int update_frame_p;
8773 {
8774 Lisp_Object mini_window;
8775 struct window *w;
8776 struct frame *f;
8777 int window_height_changed_p = 0;
8778 struct frame *sf = SELECTED_FRAME ();
8779
8780 mini_window = FRAME_MINIBUF_WINDOW (sf);
8781 w = XWINDOW (mini_window);
8782 f = XFRAME (WINDOW_FRAME (w));
8783
8784 /* Don't display if frame is invisible or not yet initialized. */
8785 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8786 return 0;
8787
8788 #ifdef HAVE_WINDOW_SYSTEM
8789 /* When Emacs starts, selected_frame may be the initial terminal
8790 frame. If we let this through, a message would be displayed on
8791 the terminal. */
8792 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8793 return 0;
8794 #endif /* HAVE_WINDOW_SYSTEM */
8795
8796 /* Redraw garbaged frames. */
8797 if (frame_garbaged)
8798 clear_garbaged_frames ();
8799
8800 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8801 {
8802 echo_area_window = mini_window;
8803 window_height_changed_p = display_echo_area (w);
8804 w->must_be_updated_p = 1;
8805
8806 /* Update the display, unless called from redisplay_internal.
8807 Also don't update the screen during redisplay itself. The
8808 update will happen at the end of redisplay, and an update
8809 here could cause confusion. */
8810 if (update_frame_p && !redisplaying_p)
8811 {
8812 int n = 0;
8813
8814 /* If the display update has been interrupted by pending
8815 input, update mode lines in the frame. Due to the
8816 pending input, it might have been that redisplay hasn't
8817 been called, so that mode lines above the echo area are
8818 garbaged. This looks odd, so we prevent it here. */
8819 if (!display_completed)
8820 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8821
8822 if (window_height_changed_p
8823 /* Don't do this if Emacs is shutting down. Redisplay
8824 needs to run hooks. */
8825 && !NILP (Vrun_hooks))
8826 {
8827 /* Must update other windows. Likewise as in other
8828 cases, don't let this update be interrupted by
8829 pending input. */
8830 int count = SPECPDL_INDEX ();
8831 specbind (Qredisplay_dont_pause, Qt);
8832 windows_or_buffers_changed = 1;
8833 redisplay_internal (0);
8834 unbind_to (count, Qnil);
8835 }
8836 else if (FRAME_WINDOW_P (f) && n == 0)
8837 {
8838 /* Window configuration is the same as before.
8839 Can do with a display update of the echo area,
8840 unless we displayed some mode lines. */
8841 update_single_window (w, 1);
8842 FRAME_RIF (f)->flush_display (f);
8843 }
8844 else
8845 update_frame (f, 1, 1);
8846
8847 /* If cursor is in the echo area, make sure that the next
8848 redisplay displays the minibuffer, so that the cursor will
8849 be replaced with what the minibuffer wants. */
8850 if (cursor_in_echo_area)
8851 ++windows_or_buffers_changed;
8852 }
8853 }
8854 else if (!EQ (mini_window, selected_window))
8855 windows_or_buffers_changed++;
8856
8857 /* Last displayed message is now the current message. */
8858 echo_area_buffer[1] = echo_area_buffer[0];
8859 /* Inform read_char that we're not echoing. */
8860 echo_message_buffer = Qnil;
8861
8862 /* Prevent redisplay optimization in redisplay_internal by resetting
8863 this_line_start_pos. This is done because the mini-buffer now
8864 displays the message instead of its buffer text. */
8865 if (EQ (mini_window, selected_window))
8866 CHARPOS (this_line_start_pos) = 0;
8867
8868 return window_height_changed_p;
8869 }
8870
8871
8872 \f
8873 /***********************************************************************
8874 Mode Lines and Frame Titles
8875 ***********************************************************************/
8876
8877 /* A buffer for constructing non-propertized mode-line strings and
8878 frame titles in it; allocated from the heap in init_xdisp and
8879 resized as needed in store_mode_line_noprop_char. */
8880
8881 static char *mode_line_noprop_buf;
8882
8883 /* The buffer's end, and a current output position in it. */
8884
8885 static char *mode_line_noprop_buf_end;
8886 static char *mode_line_noprop_ptr;
8887
8888 #define MODE_LINE_NOPROP_LEN(start) \
8889 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8890
8891 static enum {
8892 MODE_LINE_DISPLAY = 0,
8893 MODE_LINE_TITLE,
8894 MODE_LINE_NOPROP,
8895 MODE_LINE_STRING
8896 } mode_line_target;
8897
8898 /* Alist that caches the results of :propertize.
8899 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8900 static Lisp_Object mode_line_proptrans_alist;
8901
8902 /* List of strings making up the mode-line. */
8903 static Lisp_Object mode_line_string_list;
8904
8905 /* Base face property when building propertized mode line string. */
8906 static Lisp_Object mode_line_string_face;
8907 static Lisp_Object mode_line_string_face_prop;
8908
8909
8910 /* Unwind data for mode line strings */
8911
8912 static Lisp_Object Vmode_line_unwind_vector;
8913
8914 static Lisp_Object
8915 format_mode_line_unwind_data (obuf, save_proptrans)
8916 struct buffer *obuf;
8917 {
8918 Lisp_Object vector;
8919
8920 /* Reduce consing by keeping one vector in
8921 Vwith_echo_area_save_vector. */
8922 vector = Vmode_line_unwind_vector;
8923 Vmode_line_unwind_vector = Qnil;
8924
8925 if (NILP (vector))
8926 vector = Fmake_vector (make_number (7), Qnil);
8927
8928 AREF (vector, 0) = make_number (mode_line_target);
8929 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8930 AREF (vector, 2) = mode_line_string_list;
8931 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8932 AREF (vector, 4) = mode_line_string_face;
8933 AREF (vector, 5) = mode_line_string_face_prop;
8934
8935 if (obuf)
8936 XSETBUFFER (AREF (vector, 6), obuf);
8937 else
8938 AREF (vector, 6) = Qnil;
8939
8940 return vector;
8941 }
8942
8943 static Lisp_Object
8944 unwind_format_mode_line (vector)
8945 Lisp_Object vector;
8946 {
8947 mode_line_target = XINT (AREF (vector, 0));
8948 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8949 mode_line_string_list = AREF (vector, 2);
8950 if (! EQ (AREF (vector, 3), Qt))
8951 mode_line_proptrans_alist = AREF (vector, 3);
8952 mode_line_string_face = AREF (vector, 4);
8953 mode_line_string_face_prop = AREF (vector, 5);
8954
8955 if (!NILP (AREF (vector, 6)))
8956 {
8957 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8958 AREF (vector, 6) = Qnil;
8959 }
8960
8961 Vmode_line_unwind_vector = vector;
8962 return Qnil;
8963 }
8964
8965
8966 /* Store a single character C for the frame title in mode_line_noprop_buf.
8967 Re-allocate mode_line_noprop_buf if necessary. */
8968
8969 static void
8970 #ifdef PROTOTYPES
8971 store_mode_line_noprop_char (char c)
8972 #else
8973 store_mode_line_noprop_char (c)
8974 char c;
8975 #endif
8976 {
8977 /* If output position has reached the end of the allocated buffer,
8978 double the buffer's size. */
8979 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8980 {
8981 int len = MODE_LINE_NOPROP_LEN (0);
8982 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8983 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8984 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8985 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8986 }
8987
8988 *mode_line_noprop_ptr++ = c;
8989 }
8990
8991
8992 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8993 mode_line_noprop_ptr. STR is the string to store. Do not copy
8994 characters that yield more columns than PRECISION; PRECISION <= 0
8995 means copy the whole string. Pad with spaces until FIELD_WIDTH
8996 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8997 pad. Called from display_mode_element when it is used to build a
8998 frame title. */
8999
9000 static int
9001 store_mode_line_noprop (str, field_width, precision)
9002 const unsigned char *str;
9003 int field_width, precision;
9004 {
9005 int n = 0;
9006 int dummy, nbytes;
9007
9008 /* Copy at most PRECISION chars from STR. */
9009 nbytes = strlen (str);
9010 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9011 while (nbytes--)
9012 store_mode_line_noprop_char (*str++);
9013
9014 /* Fill up with spaces until FIELD_WIDTH reached. */
9015 while (field_width > 0
9016 && n < field_width)
9017 {
9018 store_mode_line_noprop_char (' ');
9019 ++n;
9020 }
9021
9022 return n;
9023 }
9024
9025 /***********************************************************************
9026 Frame Titles
9027 ***********************************************************************/
9028
9029 #ifdef HAVE_WINDOW_SYSTEM
9030
9031 /* Set the title of FRAME, if it has changed. The title format is
9032 Vicon_title_format if FRAME is iconified, otherwise it is
9033 frame_title_format. */
9034
9035 static void
9036 x_consider_frame_title (frame)
9037 Lisp_Object frame;
9038 {
9039 struct frame *f = XFRAME (frame);
9040
9041 if (FRAME_WINDOW_P (f)
9042 || FRAME_MINIBUF_ONLY_P (f)
9043 || f->explicit_name)
9044 {
9045 /* Do we have more than one visible frame on this X display? */
9046 Lisp_Object tail;
9047 Lisp_Object fmt;
9048 int title_start;
9049 char *title;
9050 int len;
9051 struct it it;
9052 int count = SPECPDL_INDEX ();
9053
9054 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9055 {
9056 Lisp_Object other_frame = XCAR (tail);
9057 struct frame *tf = XFRAME (other_frame);
9058
9059 if (tf != f
9060 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9061 && !FRAME_MINIBUF_ONLY_P (tf)
9062 && !EQ (other_frame, tip_frame)
9063 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9064 break;
9065 }
9066
9067 /* Set global variable indicating that multiple frames exist. */
9068 multiple_frames = CONSP (tail);
9069
9070 /* Switch to the buffer of selected window of the frame. Set up
9071 mode_line_target so that display_mode_element will output into
9072 mode_line_noprop_buf; then display the title. */
9073 record_unwind_protect (unwind_format_mode_line,
9074 format_mode_line_unwind_data (current_buffer, 0));
9075
9076 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9077 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9078
9079 mode_line_target = MODE_LINE_TITLE;
9080 title_start = MODE_LINE_NOPROP_LEN (0);
9081 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9082 NULL, DEFAULT_FACE_ID);
9083 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9084 len = MODE_LINE_NOPROP_LEN (title_start);
9085 title = mode_line_noprop_buf + title_start;
9086 unbind_to (count, Qnil);
9087
9088 /* Set the title only if it's changed. This avoids consing in
9089 the common case where it hasn't. (If it turns out that we've
9090 already wasted too much time by walking through the list with
9091 display_mode_element, then we might need to optimize at a
9092 higher level than this.) */
9093 if (! STRINGP (f->name)
9094 || SBYTES (f->name) != len
9095 || bcmp (title, SDATA (f->name), len) != 0)
9096 x_implicitly_set_name (f, make_string (title, len), Qnil);
9097 }
9098 }
9099
9100 #endif /* not HAVE_WINDOW_SYSTEM */
9101
9102
9103
9104 \f
9105 /***********************************************************************
9106 Menu Bars
9107 ***********************************************************************/
9108
9109
9110 /* Prepare for redisplay by updating menu-bar item lists when
9111 appropriate. This can call eval. */
9112
9113 void
9114 prepare_menu_bars ()
9115 {
9116 int all_windows;
9117 struct gcpro gcpro1, gcpro2;
9118 struct frame *f;
9119 Lisp_Object tooltip_frame;
9120
9121 #ifdef HAVE_WINDOW_SYSTEM
9122 tooltip_frame = tip_frame;
9123 #else
9124 tooltip_frame = Qnil;
9125 #endif
9126
9127 /* Update all frame titles based on their buffer names, etc. We do
9128 this before the menu bars so that the buffer-menu will show the
9129 up-to-date frame titles. */
9130 #ifdef HAVE_WINDOW_SYSTEM
9131 if (windows_or_buffers_changed || update_mode_lines)
9132 {
9133 Lisp_Object tail, frame;
9134
9135 FOR_EACH_FRAME (tail, frame)
9136 {
9137 f = XFRAME (frame);
9138 if (!EQ (frame, tooltip_frame)
9139 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9140 x_consider_frame_title (frame);
9141 }
9142 }
9143 #endif /* HAVE_WINDOW_SYSTEM */
9144
9145 /* Update the menu bar item lists, if appropriate. This has to be
9146 done before any actual redisplay or generation of display lines. */
9147 all_windows = (update_mode_lines
9148 || buffer_shared > 1
9149 || windows_or_buffers_changed);
9150 if (all_windows)
9151 {
9152 Lisp_Object tail, frame;
9153 int count = SPECPDL_INDEX ();
9154 /* 1 means that update_menu_bar has run its hooks
9155 so any further calls to update_menu_bar shouldn't do so again. */
9156 int menu_bar_hooks_run = 0;
9157
9158 record_unwind_save_match_data ();
9159
9160 FOR_EACH_FRAME (tail, frame)
9161 {
9162 f = XFRAME (frame);
9163
9164 /* Ignore tooltip frame. */
9165 if (EQ (frame, tooltip_frame))
9166 continue;
9167
9168 /* If a window on this frame changed size, report that to
9169 the user and clear the size-change flag. */
9170 if (FRAME_WINDOW_SIZES_CHANGED (f))
9171 {
9172 Lisp_Object functions;
9173
9174 /* Clear flag first in case we get an error below. */
9175 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9176 functions = Vwindow_size_change_functions;
9177 GCPRO2 (tail, functions);
9178
9179 while (CONSP (functions))
9180 {
9181 call1 (XCAR (functions), frame);
9182 functions = XCDR (functions);
9183 }
9184 UNGCPRO;
9185 }
9186
9187 GCPRO1 (tail);
9188 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9189 #ifdef HAVE_WINDOW_SYSTEM
9190 update_tool_bar (f, 0);
9191 #ifdef MAC_OS
9192 mac_update_title_bar (f, 0);
9193 #endif
9194 #endif
9195 UNGCPRO;
9196 }
9197
9198 unbind_to (count, Qnil);
9199 }
9200 else
9201 {
9202 struct frame *sf = SELECTED_FRAME ();
9203 update_menu_bar (sf, 1, 0);
9204 #ifdef HAVE_WINDOW_SYSTEM
9205 update_tool_bar (sf, 1);
9206 #ifdef MAC_OS
9207 mac_update_title_bar (sf, 1);
9208 #endif
9209 #endif
9210 }
9211
9212 /* Motif needs this. See comment in xmenu.c. Turn it off when
9213 pending_menu_activation is not defined. */
9214 #ifdef USE_X_TOOLKIT
9215 pending_menu_activation = 0;
9216 #endif
9217 }
9218
9219
9220 /* Update the menu bar item list for frame F. This has to be done
9221 before we start to fill in any display lines, because it can call
9222 eval.
9223
9224 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9225
9226 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9227 already ran the menu bar hooks for this redisplay, so there
9228 is no need to run them again. The return value is the
9229 updated value of this flag, to pass to the next call. */
9230
9231 static int
9232 update_menu_bar (f, save_match_data, hooks_run)
9233 struct frame *f;
9234 int save_match_data;
9235 int hooks_run;
9236 {
9237 Lisp_Object window;
9238 register struct window *w;
9239
9240 /* If called recursively during a menu update, do nothing. This can
9241 happen when, for instance, an activate-menubar-hook causes a
9242 redisplay. */
9243 if (inhibit_menubar_update)
9244 return hooks_run;
9245
9246 window = FRAME_SELECTED_WINDOW (f);
9247 w = XWINDOW (window);
9248
9249 #if 0 /* The if statement below this if statement used to include the
9250 condition !NILP (w->update_mode_line), rather than using
9251 update_mode_lines directly, and this if statement may have
9252 been added to make that condition work. Now the if
9253 statement below matches its comment, this isn't needed. */
9254 if (update_mode_lines)
9255 w->update_mode_line = Qt;
9256 #endif
9257
9258 if (FRAME_WINDOW_P (f)
9259 ?
9260 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9261 || defined (USE_GTK)
9262 FRAME_EXTERNAL_MENU_BAR (f)
9263 #else
9264 FRAME_MENU_BAR_LINES (f) > 0
9265 #endif
9266 : FRAME_MENU_BAR_LINES (f) > 0)
9267 {
9268 /* If the user has switched buffers or windows, we need to
9269 recompute to reflect the new bindings. But we'll
9270 recompute when update_mode_lines is set too; that means
9271 that people can use force-mode-line-update to request
9272 that the menu bar be recomputed. The adverse effect on
9273 the rest of the redisplay algorithm is about the same as
9274 windows_or_buffers_changed anyway. */
9275 if (windows_or_buffers_changed
9276 /* This used to test w->update_mode_line, but we believe
9277 there is no need to recompute the menu in that case. */
9278 || update_mode_lines
9279 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9280 < BUF_MODIFF (XBUFFER (w->buffer)))
9281 != !NILP (w->last_had_star))
9282 || ((!NILP (Vtransient_mark_mode)
9283 && !NILP (XBUFFER (w->buffer)->mark_active))
9284 != !NILP (w->region_showing)))
9285 {
9286 struct buffer *prev = current_buffer;
9287 int count = SPECPDL_INDEX ();
9288
9289 specbind (Qinhibit_menubar_update, Qt);
9290
9291 set_buffer_internal_1 (XBUFFER (w->buffer));
9292 if (save_match_data)
9293 record_unwind_save_match_data ();
9294 if (NILP (Voverriding_local_map_menu_flag))
9295 {
9296 specbind (Qoverriding_terminal_local_map, Qnil);
9297 specbind (Qoverriding_local_map, Qnil);
9298 }
9299
9300 if (!hooks_run)
9301 {
9302 /* Run the Lucid hook. */
9303 safe_run_hooks (Qactivate_menubar_hook);
9304
9305 /* If it has changed current-menubar from previous value,
9306 really recompute the menu-bar from the value. */
9307 if (! NILP (Vlucid_menu_bar_dirty_flag))
9308 call0 (Qrecompute_lucid_menubar);
9309
9310 safe_run_hooks (Qmenu_bar_update_hook);
9311
9312 hooks_run = 1;
9313 }
9314
9315 XSETFRAME (Vmenu_updating_frame, f);
9316 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9317
9318 /* Redisplay the menu bar in case we changed it. */
9319 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9320 || defined (USE_GTK)
9321 if (FRAME_WINDOW_P (f))
9322 {
9323 #ifdef MAC_OS
9324 /* All frames on Mac OS share the same menubar. So only
9325 the selected frame should be allowed to set it. */
9326 if (f == SELECTED_FRAME ())
9327 #endif
9328 set_frame_menubar (f, 0, 0);
9329 }
9330 else
9331 /* On a terminal screen, the menu bar is an ordinary screen
9332 line, and this makes it get updated. */
9333 w->update_mode_line = Qt;
9334 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9335 /* In the non-toolkit version, the menu bar is an ordinary screen
9336 line, and this makes it get updated. */
9337 w->update_mode_line = Qt;
9338 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9339
9340 unbind_to (count, Qnil);
9341 set_buffer_internal_1 (prev);
9342 }
9343 }
9344
9345 return hooks_run;
9346 }
9347
9348
9349 \f
9350 /***********************************************************************
9351 Output Cursor
9352 ***********************************************************************/
9353
9354 #ifdef HAVE_WINDOW_SYSTEM
9355
9356 /* EXPORT:
9357 Nominal cursor position -- where to draw output.
9358 HPOS and VPOS are window relative glyph matrix coordinates.
9359 X and Y are window relative pixel coordinates. */
9360
9361 struct cursor_pos output_cursor;
9362
9363
9364 /* EXPORT:
9365 Set the global variable output_cursor to CURSOR. All cursor
9366 positions are relative to updated_window. */
9367
9368 void
9369 set_output_cursor (cursor)
9370 struct cursor_pos *cursor;
9371 {
9372 output_cursor.hpos = cursor->hpos;
9373 output_cursor.vpos = cursor->vpos;
9374 output_cursor.x = cursor->x;
9375 output_cursor.y = cursor->y;
9376 }
9377
9378
9379 /* EXPORT for RIF:
9380 Set a nominal cursor position.
9381
9382 HPOS and VPOS are column/row positions in a window glyph matrix. X
9383 and Y are window text area relative pixel positions.
9384
9385 If this is done during an update, updated_window will contain the
9386 window that is being updated and the position is the future output
9387 cursor position for that window. If updated_window is null, use
9388 selected_window and display the cursor at the given position. */
9389
9390 void
9391 x_cursor_to (vpos, hpos, y, x)
9392 int vpos, hpos, y, x;
9393 {
9394 struct window *w;
9395
9396 /* If updated_window is not set, work on selected_window. */
9397 if (updated_window)
9398 w = updated_window;
9399 else
9400 w = XWINDOW (selected_window);
9401
9402 /* Set the output cursor. */
9403 output_cursor.hpos = hpos;
9404 output_cursor.vpos = vpos;
9405 output_cursor.x = x;
9406 output_cursor.y = y;
9407
9408 /* If not called as part of an update, really display the cursor.
9409 This will also set the cursor position of W. */
9410 if (updated_window == NULL)
9411 {
9412 BLOCK_INPUT;
9413 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9414 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9415 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9416 UNBLOCK_INPUT;
9417 }
9418 }
9419
9420 #endif /* HAVE_WINDOW_SYSTEM */
9421
9422 \f
9423 /***********************************************************************
9424 Tool-bars
9425 ***********************************************************************/
9426
9427 #ifdef HAVE_WINDOW_SYSTEM
9428
9429 /* Where the mouse was last time we reported a mouse event. */
9430
9431 FRAME_PTR last_mouse_frame;
9432
9433 /* Tool-bar item index of the item on which a mouse button was pressed
9434 or -1. */
9435
9436 int last_tool_bar_item;
9437
9438
9439 /* Update the tool-bar item list for frame F. This has to be done
9440 before we start to fill in any display lines. Called from
9441 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9442 and restore it here. */
9443
9444 static void
9445 update_tool_bar (f, save_match_data)
9446 struct frame *f;
9447 int save_match_data;
9448 {
9449 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9450 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9451 #else
9452 int do_update = WINDOWP (f->tool_bar_window)
9453 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9454 #endif
9455
9456 if (do_update)
9457 {
9458 Lisp_Object window;
9459 struct window *w;
9460
9461 window = FRAME_SELECTED_WINDOW (f);
9462 w = XWINDOW (window);
9463
9464 /* If the user has switched buffers or windows, we need to
9465 recompute to reflect the new bindings. But we'll
9466 recompute when update_mode_lines is set too; that means
9467 that people can use force-mode-line-update to request
9468 that the menu bar be recomputed. The adverse effect on
9469 the rest of the redisplay algorithm is about the same as
9470 windows_or_buffers_changed anyway. */
9471 if (windows_or_buffers_changed
9472 || !NILP (w->update_mode_line)
9473 || update_mode_lines
9474 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9475 < BUF_MODIFF (XBUFFER (w->buffer)))
9476 != !NILP (w->last_had_star))
9477 || ((!NILP (Vtransient_mark_mode)
9478 && !NILP (XBUFFER (w->buffer)->mark_active))
9479 != !NILP (w->region_showing)))
9480 {
9481 struct buffer *prev = current_buffer;
9482 int count = SPECPDL_INDEX ();
9483 Lisp_Object new_tool_bar;
9484 int new_n_tool_bar;
9485 struct gcpro gcpro1;
9486
9487 /* Set current_buffer to the buffer of the selected
9488 window of the frame, so that we get the right local
9489 keymaps. */
9490 set_buffer_internal_1 (XBUFFER (w->buffer));
9491
9492 /* Save match data, if we must. */
9493 if (save_match_data)
9494 record_unwind_save_match_data ();
9495
9496 /* Make sure that we don't accidentally use bogus keymaps. */
9497 if (NILP (Voverriding_local_map_menu_flag))
9498 {
9499 specbind (Qoverriding_terminal_local_map, Qnil);
9500 specbind (Qoverriding_local_map, Qnil);
9501 }
9502
9503 GCPRO1 (new_tool_bar);
9504
9505 /* Build desired tool-bar items from keymaps. */
9506 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9507 &new_n_tool_bar);
9508
9509 /* Redisplay the tool-bar if we changed it. */
9510 if (new_n_tool_bar != f->n_tool_bar_items
9511 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9512 {
9513 /* Redisplay that happens asynchronously due to an expose event
9514 may access f->tool_bar_items. Make sure we update both
9515 variables within BLOCK_INPUT so no such event interrupts. */
9516 BLOCK_INPUT;
9517 f->tool_bar_items = new_tool_bar;
9518 f->n_tool_bar_items = new_n_tool_bar;
9519 w->update_mode_line = Qt;
9520 UNBLOCK_INPUT;
9521 }
9522
9523 UNGCPRO;
9524
9525 unbind_to (count, Qnil);
9526 set_buffer_internal_1 (prev);
9527 }
9528 }
9529 }
9530
9531
9532 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9533 F's desired tool-bar contents. F->tool_bar_items must have
9534 been set up previously by calling prepare_menu_bars. */
9535
9536 static void
9537 build_desired_tool_bar_string (f)
9538 struct frame *f;
9539 {
9540 int i, size, size_needed;
9541 struct gcpro gcpro1, gcpro2, gcpro3;
9542 Lisp_Object image, plist, props;
9543
9544 image = plist = props = Qnil;
9545 GCPRO3 (image, plist, props);
9546
9547 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9548 Otherwise, make a new string. */
9549
9550 /* The size of the string we might be able to reuse. */
9551 size = (STRINGP (f->desired_tool_bar_string)
9552 ? SCHARS (f->desired_tool_bar_string)
9553 : 0);
9554
9555 /* We need one space in the string for each image. */
9556 size_needed = f->n_tool_bar_items;
9557
9558 /* Reuse f->desired_tool_bar_string, if possible. */
9559 if (size < size_needed || NILP (f->desired_tool_bar_string))
9560 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9561 make_number (' '));
9562 else
9563 {
9564 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9565 Fremove_text_properties (make_number (0), make_number (size),
9566 props, f->desired_tool_bar_string);
9567 }
9568
9569 /* Put a `display' property on the string for the images to display,
9570 put a `menu_item' property on tool-bar items with a value that
9571 is the index of the item in F's tool-bar item vector. */
9572 for (i = 0; i < f->n_tool_bar_items; ++i)
9573 {
9574 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9575
9576 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9577 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9578 int hmargin, vmargin, relief, idx, end;
9579 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9580
9581 /* If image is a vector, choose the image according to the
9582 button state. */
9583 image = PROP (TOOL_BAR_ITEM_IMAGES);
9584 if (VECTORP (image))
9585 {
9586 if (enabled_p)
9587 idx = (selected_p
9588 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9589 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9590 else
9591 idx = (selected_p
9592 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9593 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9594
9595 xassert (ASIZE (image) >= idx);
9596 image = AREF (image, idx);
9597 }
9598 else
9599 idx = -1;
9600
9601 /* Ignore invalid image specifications. */
9602 if (!valid_image_p (image))
9603 continue;
9604
9605 /* Display the tool-bar button pressed, or depressed. */
9606 plist = Fcopy_sequence (XCDR (image));
9607
9608 /* Compute margin and relief to draw. */
9609 relief = (tool_bar_button_relief >= 0
9610 ? tool_bar_button_relief
9611 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9612 hmargin = vmargin = relief;
9613
9614 if (INTEGERP (Vtool_bar_button_margin)
9615 && XINT (Vtool_bar_button_margin) > 0)
9616 {
9617 hmargin += XFASTINT (Vtool_bar_button_margin);
9618 vmargin += XFASTINT (Vtool_bar_button_margin);
9619 }
9620 else if (CONSP (Vtool_bar_button_margin))
9621 {
9622 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9623 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9624 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9625
9626 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9627 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9628 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9629 }
9630
9631 if (auto_raise_tool_bar_buttons_p)
9632 {
9633 /* Add a `:relief' property to the image spec if the item is
9634 selected. */
9635 if (selected_p)
9636 {
9637 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9638 hmargin -= relief;
9639 vmargin -= relief;
9640 }
9641 }
9642 else
9643 {
9644 /* If image is selected, display it pressed, i.e. with a
9645 negative relief. If it's not selected, display it with a
9646 raised relief. */
9647 plist = Fplist_put (plist, QCrelief,
9648 (selected_p
9649 ? make_number (-relief)
9650 : make_number (relief)));
9651 hmargin -= relief;
9652 vmargin -= relief;
9653 }
9654
9655 /* Put a margin around the image. */
9656 if (hmargin || vmargin)
9657 {
9658 if (hmargin == vmargin)
9659 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9660 else
9661 plist = Fplist_put (plist, QCmargin,
9662 Fcons (make_number (hmargin),
9663 make_number (vmargin)));
9664 }
9665
9666 /* If button is not enabled, and we don't have special images
9667 for the disabled state, make the image appear disabled by
9668 applying an appropriate algorithm to it. */
9669 if (!enabled_p && idx < 0)
9670 plist = Fplist_put (plist, QCconversion, Qdisabled);
9671
9672 /* Put a `display' text property on the string for the image to
9673 display. Put a `menu-item' property on the string that gives
9674 the start of this item's properties in the tool-bar items
9675 vector. */
9676 image = Fcons (Qimage, plist);
9677 props = list4 (Qdisplay, image,
9678 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9679
9680 /* Let the last image hide all remaining spaces in the tool bar
9681 string. The string can be longer than needed when we reuse a
9682 previous string. */
9683 if (i + 1 == f->n_tool_bar_items)
9684 end = SCHARS (f->desired_tool_bar_string);
9685 else
9686 end = i + 1;
9687 Fadd_text_properties (make_number (i), make_number (end),
9688 props, f->desired_tool_bar_string);
9689 #undef PROP
9690 }
9691
9692 UNGCPRO;
9693 }
9694
9695
9696 /* Display one line of the tool-bar of frame IT->f.
9697
9698 HEIGHT specifies the desired height of the tool-bar line.
9699 If the actual height of the glyph row is less than HEIGHT, the
9700 row's height is increased to HEIGHT, and the icons are centered
9701 vertically in the new height.
9702
9703 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9704 count a final empty row in case the tool-bar width exactly matches
9705 the window width.
9706 */
9707
9708 static void
9709 display_tool_bar_line (it, height)
9710 struct it *it;
9711 int height;
9712 {
9713 struct glyph_row *row = it->glyph_row;
9714 int max_x = it->last_visible_x;
9715 struct glyph *last;
9716
9717 prepare_desired_row (row);
9718 row->y = it->current_y;
9719
9720 /* Note that this isn't made use of if the face hasn't a box,
9721 so there's no need to check the face here. */
9722 it->start_of_box_run_p = 1;
9723
9724 while (it->current_x < max_x)
9725 {
9726 int x, n_glyphs_before, i, nglyphs;
9727 struct it it_before;
9728
9729 /* Get the next display element. */
9730 if (!get_next_display_element (it))
9731 {
9732 /* Don't count empty row if we are counting needed tool-bar lines. */
9733 if (height < 0 && !it->hpos)
9734 return;
9735 break;
9736 }
9737
9738 /* Produce glyphs. */
9739 n_glyphs_before = row->used[TEXT_AREA];
9740 it_before = *it;
9741
9742 PRODUCE_GLYPHS (it);
9743
9744 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9745 i = 0;
9746 x = it_before.current_x;
9747 while (i < nglyphs)
9748 {
9749 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9750
9751 if (x + glyph->pixel_width > max_x)
9752 {
9753 /* Glyph doesn't fit on line. Backtrack. */
9754 row->used[TEXT_AREA] = n_glyphs_before;
9755 *it = it_before;
9756 /* If this is the only glyph on this line, it will never fit on the
9757 toolbar, so skip it. But ensure there is at least one glyph,
9758 so we don't accidentally disable the tool-bar. */
9759 if (n_glyphs_before == 0
9760 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9761 break;
9762 goto out;
9763 }
9764
9765 ++it->hpos;
9766 x += glyph->pixel_width;
9767 ++i;
9768 }
9769
9770 /* Stop at line ends. */
9771 if (ITERATOR_AT_END_OF_LINE_P (it))
9772 break;
9773
9774 set_iterator_to_next (it, 1);
9775 }
9776
9777 out:;
9778
9779 row->displays_text_p = row->used[TEXT_AREA] != 0;
9780
9781 /* Use default face for the border below the tool bar.
9782
9783 FIXME: When auto-resize-tool-bars is grow-only, there is
9784 no additional border below the possibly empty tool-bar lines.
9785 So to make the extra empty lines look "normal", we have to
9786 use the tool-bar face for the border too. */
9787 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9788 it->face_id = DEFAULT_FACE_ID;
9789
9790 extend_face_to_end_of_line (it);
9791 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9792 last->right_box_line_p = 1;
9793 if (last == row->glyphs[TEXT_AREA])
9794 last->left_box_line_p = 1;
9795
9796 /* Make line the desired height and center it vertically. */
9797 if ((height -= it->max_ascent + it->max_descent) > 0)
9798 {
9799 /* Don't add more than one line height. */
9800 height %= FRAME_LINE_HEIGHT (it->f);
9801 it->max_ascent += height / 2;
9802 it->max_descent += (height + 1) / 2;
9803 }
9804
9805 compute_line_metrics (it);
9806
9807 /* If line is empty, make it occupy the rest of the tool-bar. */
9808 if (!row->displays_text_p)
9809 {
9810 row->height = row->phys_height = it->last_visible_y - row->y;
9811 row->visible_height = row->height;
9812 row->ascent = row->phys_ascent = 0;
9813 row->extra_line_spacing = 0;
9814 }
9815
9816 row->full_width_p = 1;
9817 row->continued_p = 0;
9818 row->truncated_on_left_p = 0;
9819 row->truncated_on_right_p = 0;
9820
9821 it->current_x = it->hpos = 0;
9822 it->current_y += row->height;
9823 ++it->vpos;
9824 ++it->glyph_row;
9825 }
9826
9827
9828 /* Max tool-bar height. */
9829
9830 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9831 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9832
9833 /* Value is the number of screen lines needed to make all tool-bar
9834 items of frame F visible. The number of actual rows needed is
9835 returned in *N_ROWS if non-NULL. */
9836
9837 static int
9838 tool_bar_lines_needed (f, n_rows)
9839 struct frame *f;
9840 int *n_rows;
9841 {
9842 struct window *w = XWINDOW (f->tool_bar_window);
9843 struct it it;
9844 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9845 the desired matrix, so use (unused) mode-line row as temporary row to
9846 avoid destroying the first tool-bar row. */
9847 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9848
9849 /* Initialize an iterator for iteration over
9850 F->desired_tool_bar_string in the tool-bar window of frame F. */
9851 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9852 it.first_visible_x = 0;
9853 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9854 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9855
9856 while (!ITERATOR_AT_END_P (&it))
9857 {
9858 clear_glyph_row (temp_row);
9859 it.glyph_row = temp_row;
9860 display_tool_bar_line (&it, -1);
9861 }
9862 clear_glyph_row (temp_row);
9863
9864 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9865 if (n_rows)
9866 *n_rows = it.vpos > 0 ? it.vpos : -1;
9867
9868 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9869 }
9870
9871
9872 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9873 0, 1, 0,
9874 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9875 (frame)
9876 Lisp_Object frame;
9877 {
9878 struct frame *f;
9879 struct window *w;
9880 int nlines = 0;
9881
9882 if (NILP (frame))
9883 frame = selected_frame;
9884 else
9885 CHECK_FRAME (frame);
9886 f = XFRAME (frame);
9887
9888 if (WINDOWP (f->tool_bar_window)
9889 || (w = XWINDOW (f->tool_bar_window),
9890 WINDOW_TOTAL_LINES (w) > 0))
9891 {
9892 update_tool_bar (f, 1);
9893 if (f->n_tool_bar_items)
9894 {
9895 build_desired_tool_bar_string (f);
9896 nlines = tool_bar_lines_needed (f, NULL);
9897 }
9898 }
9899
9900 return make_number (nlines);
9901 }
9902
9903
9904 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9905 height should be changed. */
9906
9907 static int
9908 redisplay_tool_bar (f)
9909 struct frame *f;
9910 {
9911 struct window *w;
9912 struct it it;
9913 struct glyph_row *row;
9914
9915 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9916 if (FRAME_EXTERNAL_TOOL_BAR (f))
9917 update_frame_tool_bar (f);
9918 return 0;
9919 #endif
9920
9921 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9922 do anything. This means you must start with tool-bar-lines
9923 non-zero to get the auto-sizing effect. Or in other words, you
9924 can turn off tool-bars by specifying tool-bar-lines zero. */
9925 if (!WINDOWP (f->tool_bar_window)
9926 || (w = XWINDOW (f->tool_bar_window),
9927 WINDOW_TOTAL_LINES (w) == 0))
9928 return 0;
9929
9930 /* Set up an iterator for the tool-bar window. */
9931 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9932 it.first_visible_x = 0;
9933 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9934 row = it.glyph_row;
9935
9936 /* Build a string that represents the contents of the tool-bar. */
9937 build_desired_tool_bar_string (f);
9938 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9939
9940 if (f->n_tool_bar_rows == 0)
9941 {
9942 int nlines;
9943
9944 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9945 nlines != WINDOW_TOTAL_LINES (w)))
9946 {
9947 extern Lisp_Object Qtool_bar_lines;
9948 Lisp_Object frame;
9949 int old_height = WINDOW_TOTAL_LINES (w);
9950
9951 XSETFRAME (frame, f);
9952 Fmodify_frame_parameters (frame,
9953 Fcons (Fcons (Qtool_bar_lines,
9954 make_number (nlines)),
9955 Qnil));
9956 if (WINDOW_TOTAL_LINES (w) != old_height)
9957 {
9958 clear_glyph_matrix (w->desired_matrix);
9959 fonts_changed_p = 1;
9960 return 1;
9961 }
9962 }
9963 }
9964
9965 /* Display as many lines as needed to display all tool-bar items. */
9966
9967 if (f->n_tool_bar_rows > 0)
9968 {
9969 int border, rows, height, extra;
9970
9971 if (INTEGERP (Vtool_bar_border))
9972 border = XINT (Vtool_bar_border);
9973 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9974 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9975 else if (EQ (Vtool_bar_border, Qborder_width))
9976 border = f->border_width;
9977 else
9978 border = 0;
9979 if (border < 0)
9980 border = 0;
9981
9982 rows = f->n_tool_bar_rows;
9983 height = max (1, (it.last_visible_y - border) / rows);
9984 extra = it.last_visible_y - border - height * rows;
9985
9986 while (it.current_y < it.last_visible_y)
9987 {
9988 int h = 0;
9989 if (extra > 0 && rows-- > 0)
9990 {
9991 h = (extra + rows - 1) / rows;
9992 extra -= h;
9993 }
9994 display_tool_bar_line (&it, height + h);
9995 }
9996 }
9997 else
9998 {
9999 while (it.current_y < it.last_visible_y)
10000 display_tool_bar_line (&it, 0);
10001 }
10002
10003 /* It doesn't make much sense to try scrolling in the tool-bar
10004 window, so don't do it. */
10005 w->desired_matrix->no_scrolling_p = 1;
10006 w->must_be_updated_p = 1;
10007
10008 if (!NILP (Vauto_resize_tool_bars))
10009 {
10010 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10011 int change_height_p = 0;
10012
10013 /* If we couldn't display everything, change the tool-bar's
10014 height if there is room for more. */
10015 if (IT_STRING_CHARPOS (it) < it.end_charpos
10016 && it.current_y < max_tool_bar_height)
10017 change_height_p = 1;
10018
10019 row = it.glyph_row - 1;
10020
10021 /* If there are blank lines at the end, except for a partially
10022 visible blank line at the end that is smaller than
10023 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10024 if (!row->displays_text_p
10025 && row->height >= FRAME_LINE_HEIGHT (f))
10026 change_height_p = 1;
10027
10028 /* If row displays tool-bar items, but is partially visible,
10029 change the tool-bar's height. */
10030 if (row->displays_text_p
10031 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10032 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10033 change_height_p = 1;
10034
10035 /* Resize windows as needed by changing the `tool-bar-lines'
10036 frame parameter. */
10037 if (change_height_p)
10038 {
10039 extern Lisp_Object Qtool_bar_lines;
10040 Lisp_Object frame;
10041 int old_height = WINDOW_TOTAL_LINES (w);
10042 int nrows;
10043 int nlines = tool_bar_lines_needed (f, &nrows);
10044
10045 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10046 && !f->minimize_tool_bar_window_p)
10047 ? (nlines > old_height)
10048 : (nlines != old_height));
10049 f->minimize_tool_bar_window_p = 0;
10050
10051 if (change_height_p)
10052 {
10053 XSETFRAME (frame, f);
10054 Fmodify_frame_parameters (frame,
10055 Fcons (Fcons (Qtool_bar_lines,
10056 make_number (nlines)),
10057 Qnil));
10058 if (WINDOW_TOTAL_LINES (w) != old_height)
10059 {
10060 clear_glyph_matrix (w->desired_matrix);
10061 f->n_tool_bar_rows = nrows;
10062 fonts_changed_p = 1;
10063 return 1;
10064 }
10065 }
10066 }
10067 }
10068
10069 f->minimize_tool_bar_window_p = 0;
10070 return 0;
10071 }
10072
10073
10074 /* Get information about the tool-bar item which is displayed in GLYPH
10075 on frame F. Return in *PROP_IDX the index where tool-bar item
10076 properties start in F->tool_bar_items. Value is zero if
10077 GLYPH doesn't display a tool-bar item. */
10078
10079 static int
10080 tool_bar_item_info (f, glyph, prop_idx)
10081 struct frame *f;
10082 struct glyph *glyph;
10083 int *prop_idx;
10084 {
10085 Lisp_Object prop;
10086 int success_p;
10087 int charpos;
10088
10089 /* This function can be called asynchronously, which means we must
10090 exclude any possibility that Fget_text_property signals an
10091 error. */
10092 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10093 charpos = max (0, charpos);
10094
10095 /* Get the text property `menu-item' at pos. The value of that
10096 property is the start index of this item's properties in
10097 F->tool_bar_items. */
10098 prop = Fget_text_property (make_number (charpos),
10099 Qmenu_item, f->current_tool_bar_string);
10100 if (INTEGERP (prop))
10101 {
10102 *prop_idx = XINT (prop);
10103 success_p = 1;
10104 }
10105 else
10106 success_p = 0;
10107
10108 return success_p;
10109 }
10110
10111 \f
10112 /* Get information about the tool-bar item at position X/Y on frame F.
10113 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10114 the current matrix of the tool-bar window of F, or NULL if not
10115 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10116 item in F->tool_bar_items. Value is
10117
10118 -1 if X/Y is not on a tool-bar item
10119 0 if X/Y is on the same item that was highlighted before.
10120 1 otherwise. */
10121
10122 static int
10123 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10124 struct frame *f;
10125 int x, y;
10126 struct glyph **glyph;
10127 int *hpos, *vpos, *prop_idx;
10128 {
10129 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10130 struct window *w = XWINDOW (f->tool_bar_window);
10131 int area;
10132
10133 /* Find the glyph under X/Y. */
10134 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10135 if (*glyph == NULL)
10136 return -1;
10137
10138 /* Get the start of this tool-bar item's properties in
10139 f->tool_bar_items. */
10140 if (!tool_bar_item_info (f, *glyph, prop_idx))
10141 return -1;
10142
10143 /* Is mouse on the highlighted item? */
10144 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10145 && *vpos >= dpyinfo->mouse_face_beg_row
10146 && *vpos <= dpyinfo->mouse_face_end_row
10147 && (*vpos > dpyinfo->mouse_face_beg_row
10148 || *hpos >= dpyinfo->mouse_face_beg_col)
10149 && (*vpos < dpyinfo->mouse_face_end_row
10150 || *hpos < dpyinfo->mouse_face_end_col
10151 || dpyinfo->mouse_face_past_end))
10152 return 0;
10153
10154 return 1;
10155 }
10156
10157
10158 /* EXPORT:
10159 Handle mouse button event on the tool-bar of frame F, at
10160 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10161 0 for button release. MODIFIERS is event modifiers for button
10162 release. */
10163
10164 void
10165 handle_tool_bar_click (f, x, y, down_p, modifiers)
10166 struct frame *f;
10167 int x, y, down_p;
10168 unsigned int modifiers;
10169 {
10170 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10171 struct window *w = XWINDOW (f->tool_bar_window);
10172 int hpos, vpos, prop_idx;
10173 struct glyph *glyph;
10174 Lisp_Object enabled_p;
10175
10176 /* If not on the highlighted tool-bar item, return. */
10177 frame_to_window_pixel_xy (w, &x, &y);
10178 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10179 return;
10180
10181 /* If item is disabled, do nothing. */
10182 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10183 if (NILP (enabled_p))
10184 return;
10185
10186 if (down_p)
10187 {
10188 /* Show item in pressed state. */
10189 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10190 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10191 last_tool_bar_item = prop_idx;
10192 }
10193 else
10194 {
10195 Lisp_Object key, frame;
10196 struct input_event event;
10197 EVENT_INIT (event);
10198
10199 /* Show item in released state. */
10200 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10201 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10202
10203 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10204
10205 XSETFRAME (frame, f);
10206 event.kind = TOOL_BAR_EVENT;
10207 event.frame_or_window = frame;
10208 event.arg = frame;
10209 kbd_buffer_store_event (&event);
10210
10211 event.kind = TOOL_BAR_EVENT;
10212 event.frame_or_window = frame;
10213 event.arg = key;
10214 event.modifiers = modifiers;
10215 kbd_buffer_store_event (&event);
10216 last_tool_bar_item = -1;
10217 }
10218 }
10219
10220
10221 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10222 tool-bar window-relative coordinates X/Y. Called from
10223 note_mouse_highlight. */
10224
10225 static void
10226 note_tool_bar_highlight (f, x, y)
10227 struct frame *f;
10228 int x, y;
10229 {
10230 Lisp_Object window = f->tool_bar_window;
10231 struct window *w = XWINDOW (window);
10232 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10233 int hpos, vpos;
10234 struct glyph *glyph;
10235 struct glyph_row *row;
10236 int i;
10237 Lisp_Object enabled_p;
10238 int prop_idx;
10239 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10240 int mouse_down_p, rc;
10241
10242 /* Function note_mouse_highlight is called with negative x(y
10243 values when mouse moves outside of the frame. */
10244 if (x <= 0 || y <= 0)
10245 {
10246 clear_mouse_face (dpyinfo);
10247 return;
10248 }
10249
10250 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10251 if (rc < 0)
10252 {
10253 /* Not on tool-bar item. */
10254 clear_mouse_face (dpyinfo);
10255 return;
10256 }
10257 else if (rc == 0)
10258 /* On same tool-bar item as before. */
10259 goto set_help_echo;
10260
10261 clear_mouse_face (dpyinfo);
10262
10263 /* Mouse is down, but on different tool-bar item? */
10264 mouse_down_p = (dpyinfo->grabbed
10265 && f == last_mouse_frame
10266 && FRAME_LIVE_P (f));
10267 if (mouse_down_p
10268 && last_tool_bar_item != prop_idx)
10269 return;
10270
10271 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10272 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10273
10274 /* If tool-bar item is not enabled, don't highlight it. */
10275 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10276 if (!NILP (enabled_p))
10277 {
10278 /* Compute the x-position of the glyph. In front and past the
10279 image is a space. We include this in the highlighted area. */
10280 row = MATRIX_ROW (w->current_matrix, vpos);
10281 for (i = x = 0; i < hpos; ++i)
10282 x += row->glyphs[TEXT_AREA][i].pixel_width;
10283
10284 /* Record this as the current active region. */
10285 dpyinfo->mouse_face_beg_col = hpos;
10286 dpyinfo->mouse_face_beg_row = vpos;
10287 dpyinfo->mouse_face_beg_x = x;
10288 dpyinfo->mouse_face_beg_y = row->y;
10289 dpyinfo->mouse_face_past_end = 0;
10290
10291 dpyinfo->mouse_face_end_col = hpos + 1;
10292 dpyinfo->mouse_face_end_row = vpos;
10293 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10294 dpyinfo->mouse_face_end_y = row->y;
10295 dpyinfo->mouse_face_window = window;
10296 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10297
10298 /* Display it as active. */
10299 show_mouse_face (dpyinfo, draw);
10300 dpyinfo->mouse_face_image_state = draw;
10301 }
10302
10303 set_help_echo:
10304
10305 /* Set help_echo_string to a help string to display for this tool-bar item.
10306 XTread_socket does the rest. */
10307 help_echo_object = help_echo_window = Qnil;
10308 help_echo_pos = -1;
10309 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10310 if (NILP (help_echo_string))
10311 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10312 }
10313
10314 #endif /* HAVE_WINDOW_SYSTEM */
10315
10316
10317 \f
10318 /************************************************************************
10319 Horizontal scrolling
10320 ************************************************************************/
10321
10322 static int hscroll_window_tree P_ ((Lisp_Object));
10323 static int hscroll_windows P_ ((Lisp_Object));
10324
10325 /* For all leaf windows in the window tree rooted at WINDOW, set their
10326 hscroll value so that PT is (i) visible in the window, and (ii) so
10327 that it is not within a certain margin at the window's left and
10328 right border. Value is non-zero if any window's hscroll has been
10329 changed. */
10330
10331 static int
10332 hscroll_window_tree (window)
10333 Lisp_Object window;
10334 {
10335 int hscrolled_p = 0;
10336 int hscroll_relative_p = FLOATP (Vhscroll_step);
10337 int hscroll_step_abs = 0;
10338 double hscroll_step_rel = 0;
10339
10340 if (hscroll_relative_p)
10341 {
10342 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10343 if (hscroll_step_rel < 0)
10344 {
10345 hscroll_relative_p = 0;
10346 hscroll_step_abs = 0;
10347 }
10348 }
10349 else if (INTEGERP (Vhscroll_step))
10350 {
10351 hscroll_step_abs = XINT (Vhscroll_step);
10352 if (hscroll_step_abs < 0)
10353 hscroll_step_abs = 0;
10354 }
10355 else
10356 hscroll_step_abs = 0;
10357
10358 while (WINDOWP (window))
10359 {
10360 struct window *w = XWINDOW (window);
10361
10362 if (WINDOWP (w->hchild))
10363 hscrolled_p |= hscroll_window_tree (w->hchild);
10364 else if (WINDOWP (w->vchild))
10365 hscrolled_p |= hscroll_window_tree (w->vchild);
10366 else if (w->cursor.vpos >= 0)
10367 {
10368 int h_margin;
10369 int text_area_width;
10370 struct glyph_row *current_cursor_row
10371 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10372 struct glyph_row *desired_cursor_row
10373 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10374 struct glyph_row *cursor_row
10375 = (desired_cursor_row->enabled_p
10376 ? desired_cursor_row
10377 : current_cursor_row);
10378
10379 text_area_width = window_box_width (w, TEXT_AREA);
10380
10381 /* Scroll when cursor is inside this scroll margin. */
10382 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10383
10384 if ((XFASTINT (w->hscroll)
10385 && w->cursor.x <= h_margin)
10386 || (cursor_row->enabled_p
10387 && cursor_row->truncated_on_right_p
10388 && (w->cursor.x >= text_area_width - h_margin)))
10389 {
10390 struct it it;
10391 int hscroll;
10392 struct buffer *saved_current_buffer;
10393 int pt;
10394 int wanted_x;
10395
10396 /* Find point in a display of infinite width. */
10397 saved_current_buffer = current_buffer;
10398 current_buffer = XBUFFER (w->buffer);
10399
10400 if (w == XWINDOW (selected_window))
10401 pt = BUF_PT (current_buffer);
10402 else
10403 {
10404 pt = marker_position (w->pointm);
10405 pt = max (BEGV, pt);
10406 pt = min (ZV, pt);
10407 }
10408
10409 /* Move iterator to pt starting at cursor_row->start in
10410 a line with infinite width. */
10411 init_to_row_start (&it, w, cursor_row);
10412 it.last_visible_x = INFINITY;
10413 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10414 current_buffer = saved_current_buffer;
10415
10416 /* Position cursor in window. */
10417 if (!hscroll_relative_p && hscroll_step_abs == 0)
10418 hscroll = max (0, (it.current_x
10419 - (ITERATOR_AT_END_OF_LINE_P (&it)
10420 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10421 : (text_area_width / 2))))
10422 / FRAME_COLUMN_WIDTH (it.f);
10423 else if (w->cursor.x >= text_area_width - h_margin)
10424 {
10425 if (hscroll_relative_p)
10426 wanted_x = text_area_width * (1 - hscroll_step_rel)
10427 - h_margin;
10428 else
10429 wanted_x = text_area_width
10430 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10431 - h_margin;
10432 hscroll
10433 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10434 }
10435 else
10436 {
10437 if (hscroll_relative_p)
10438 wanted_x = text_area_width * hscroll_step_rel
10439 + h_margin;
10440 else
10441 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10442 + h_margin;
10443 hscroll
10444 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10445 }
10446 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10447
10448 /* Don't call Fset_window_hscroll if value hasn't
10449 changed because it will prevent redisplay
10450 optimizations. */
10451 if (XFASTINT (w->hscroll) != hscroll)
10452 {
10453 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10454 w->hscroll = make_number (hscroll);
10455 hscrolled_p = 1;
10456 }
10457 }
10458 }
10459
10460 window = w->next;
10461 }
10462
10463 /* Value is non-zero if hscroll of any leaf window has been changed. */
10464 return hscrolled_p;
10465 }
10466
10467
10468 /* Set hscroll so that cursor is visible and not inside horizontal
10469 scroll margins for all windows in the tree rooted at WINDOW. See
10470 also hscroll_window_tree above. Value is non-zero if any window's
10471 hscroll has been changed. If it has, desired matrices on the frame
10472 of WINDOW are cleared. */
10473
10474 static int
10475 hscroll_windows (window)
10476 Lisp_Object window;
10477 {
10478 int hscrolled_p;
10479
10480 if (automatic_hscrolling_p)
10481 {
10482 hscrolled_p = hscroll_window_tree (window);
10483 if (hscrolled_p)
10484 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10485 }
10486 else
10487 hscrolled_p = 0;
10488 return hscrolled_p;
10489 }
10490
10491
10492 \f
10493 /************************************************************************
10494 Redisplay
10495 ************************************************************************/
10496
10497 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10498 to a non-zero value. This is sometimes handy to have in a debugger
10499 session. */
10500
10501 #if GLYPH_DEBUG
10502
10503 /* First and last unchanged row for try_window_id. */
10504
10505 int debug_first_unchanged_at_end_vpos;
10506 int debug_last_unchanged_at_beg_vpos;
10507
10508 /* Delta vpos and y. */
10509
10510 int debug_dvpos, debug_dy;
10511
10512 /* Delta in characters and bytes for try_window_id. */
10513
10514 int debug_delta, debug_delta_bytes;
10515
10516 /* Values of window_end_pos and window_end_vpos at the end of
10517 try_window_id. */
10518
10519 EMACS_INT debug_end_pos, debug_end_vpos;
10520
10521 /* Append a string to W->desired_matrix->method. FMT is a printf
10522 format string. A1...A9 are a supplement for a variable-length
10523 argument list. If trace_redisplay_p is non-zero also printf the
10524 resulting string to stderr. */
10525
10526 static void
10527 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10528 struct window *w;
10529 char *fmt;
10530 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10531 {
10532 char buffer[512];
10533 char *method = w->desired_matrix->method;
10534 int len = strlen (method);
10535 int size = sizeof w->desired_matrix->method;
10536 int remaining = size - len - 1;
10537
10538 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10539 if (len && remaining)
10540 {
10541 method[len] = '|';
10542 --remaining, ++len;
10543 }
10544
10545 strncpy (method + len, buffer, remaining);
10546
10547 if (trace_redisplay_p)
10548 fprintf (stderr, "%p (%s): %s\n",
10549 w,
10550 ((BUFFERP (w->buffer)
10551 && STRINGP (XBUFFER (w->buffer)->name))
10552 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10553 : "no buffer"),
10554 buffer);
10555 }
10556
10557 #endif /* GLYPH_DEBUG */
10558
10559
10560 /* Value is non-zero if all changes in window W, which displays
10561 current_buffer, are in the text between START and END. START is a
10562 buffer position, END is given as a distance from Z. Used in
10563 redisplay_internal for display optimization. */
10564
10565 static INLINE int
10566 text_outside_line_unchanged_p (w, start, end)
10567 struct window *w;
10568 int start, end;
10569 {
10570 int unchanged_p = 1;
10571
10572 /* If text or overlays have changed, see where. */
10573 if (XFASTINT (w->last_modified) < MODIFF
10574 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10575 {
10576 /* Gap in the line? */
10577 if (GPT < start || Z - GPT < end)
10578 unchanged_p = 0;
10579
10580 /* Changes start in front of the line, or end after it? */
10581 if (unchanged_p
10582 && (BEG_UNCHANGED < start - 1
10583 || END_UNCHANGED < end))
10584 unchanged_p = 0;
10585
10586 /* If selective display, can't optimize if changes start at the
10587 beginning of the line. */
10588 if (unchanged_p
10589 && INTEGERP (current_buffer->selective_display)
10590 && XINT (current_buffer->selective_display) > 0
10591 && (BEG_UNCHANGED < start || GPT <= start))
10592 unchanged_p = 0;
10593
10594 /* If there are overlays at the start or end of the line, these
10595 may have overlay strings with newlines in them. A change at
10596 START, for instance, may actually concern the display of such
10597 overlay strings as well, and they are displayed on different
10598 lines. So, quickly rule out this case. (For the future, it
10599 might be desirable to implement something more telling than
10600 just BEG/END_UNCHANGED.) */
10601 if (unchanged_p)
10602 {
10603 if (BEG + BEG_UNCHANGED == start
10604 && overlay_touches_p (start))
10605 unchanged_p = 0;
10606 if (END_UNCHANGED == end
10607 && overlay_touches_p (Z - end))
10608 unchanged_p = 0;
10609 }
10610 }
10611
10612 return unchanged_p;
10613 }
10614
10615
10616 /* Do a frame update, taking possible shortcuts into account. This is
10617 the main external entry point for redisplay.
10618
10619 If the last redisplay displayed an echo area message and that message
10620 is no longer requested, we clear the echo area or bring back the
10621 mini-buffer if that is in use. */
10622
10623 void
10624 redisplay ()
10625 {
10626 redisplay_internal (0);
10627 }
10628
10629
10630 static Lisp_Object
10631 overlay_arrow_string_or_property (var)
10632 Lisp_Object var;
10633 {
10634 Lisp_Object val;
10635
10636 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10637 return val;
10638
10639 return Voverlay_arrow_string;
10640 }
10641
10642 /* Return 1 if there are any overlay-arrows in current_buffer. */
10643 static int
10644 overlay_arrow_in_current_buffer_p ()
10645 {
10646 Lisp_Object vlist;
10647
10648 for (vlist = Voverlay_arrow_variable_list;
10649 CONSP (vlist);
10650 vlist = XCDR (vlist))
10651 {
10652 Lisp_Object var = XCAR (vlist);
10653 Lisp_Object val;
10654
10655 if (!SYMBOLP (var))
10656 continue;
10657 val = find_symbol_value (var);
10658 if (MARKERP (val)
10659 && current_buffer == XMARKER (val)->buffer)
10660 return 1;
10661 }
10662 return 0;
10663 }
10664
10665
10666 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10667 has changed. */
10668
10669 static int
10670 overlay_arrows_changed_p ()
10671 {
10672 Lisp_Object vlist;
10673
10674 for (vlist = Voverlay_arrow_variable_list;
10675 CONSP (vlist);
10676 vlist = XCDR (vlist))
10677 {
10678 Lisp_Object var = XCAR (vlist);
10679 Lisp_Object val, pstr;
10680
10681 if (!SYMBOLP (var))
10682 continue;
10683 val = find_symbol_value (var);
10684 if (!MARKERP (val))
10685 continue;
10686 if (! EQ (COERCE_MARKER (val),
10687 Fget (var, Qlast_arrow_position))
10688 || ! (pstr = overlay_arrow_string_or_property (var),
10689 EQ (pstr, Fget (var, Qlast_arrow_string))))
10690 return 1;
10691 }
10692 return 0;
10693 }
10694
10695 /* Mark overlay arrows to be updated on next redisplay. */
10696
10697 static void
10698 update_overlay_arrows (up_to_date)
10699 int up_to_date;
10700 {
10701 Lisp_Object vlist;
10702
10703 for (vlist = Voverlay_arrow_variable_list;
10704 CONSP (vlist);
10705 vlist = XCDR (vlist))
10706 {
10707 Lisp_Object var = XCAR (vlist);
10708
10709 if (!SYMBOLP (var))
10710 continue;
10711
10712 if (up_to_date > 0)
10713 {
10714 Lisp_Object val = find_symbol_value (var);
10715 Fput (var, Qlast_arrow_position,
10716 COERCE_MARKER (val));
10717 Fput (var, Qlast_arrow_string,
10718 overlay_arrow_string_or_property (var));
10719 }
10720 else if (up_to_date < 0
10721 || !NILP (Fget (var, Qlast_arrow_position)))
10722 {
10723 Fput (var, Qlast_arrow_position, Qt);
10724 Fput (var, Qlast_arrow_string, Qt);
10725 }
10726 }
10727 }
10728
10729
10730 /* Return overlay arrow string to display at row.
10731 Return integer (bitmap number) for arrow bitmap in left fringe.
10732 Return nil if no overlay arrow. */
10733
10734 static Lisp_Object
10735 overlay_arrow_at_row (it, row)
10736 struct it *it;
10737 struct glyph_row *row;
10738 {
10739 Lisp_Object vlist;
10740
10741 for (vlist = Voverlay_arrow_variable_list;
10742 CONSP (vlist);
10743 vlist = XCDR (vlist))
10744 {
10745 Lisp_Object var = XCAR (vlist);
10746 Lisp_Object val;
10747
10748 if (!SYMBOLP (var))
10749 continue;
10750
10751 val = find_symbol_value (var);
10752
10753 if (MARKERP (val)
10754 && current_buffer == XMARKER (val)->buffer
10755 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10756 {
10757 if (FRAME_WINDOW_P (it->f)
10758 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10759 {
10760 #ifdef HAVE_WINDOW_SYSTEM
10761 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10762 {
10763 int fringe_bitmap;
10764 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10765 return make_number (fringe_bitmap);
10766 }
10767 #endif
10768 return make_number (-1); /* Use default arrow bitmap */
10769 }
10770 return overlay_arrow_string_or_property (var);
10771 }
10772 }
10773
10774 return Qnil;
10775 }
10776
10777 /* Return 1 if point moved out of or into a composition. Otherwise
10778 return 0. PREV_BUF and PREV_PT are the last point buffer and
10779 position. BUF and PT are the current point buffer and position. */
10780
10781 int
10782 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10783 struct buffer *prev_buf, *buf;
10784 int prev_pt, pt;
10785 {
10786 int start, end;
10787 Lisp_Object prop;
10788 Lisp_Object buffer;
10789
10790 XSETBUFFER (buffer, buf);
10791 /* Check a composition at the last point if point moved within the
10792 same buffer. */
10793 if (prev_buf == buf)
10794 {
10795 if (prev_pt == pt)
10796 /* Point didn't move. */
10797 return 0;
10798
10799 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10800 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10801 && COMPOSITION_VALID_P (start, end, prop)
10802 && start < prev_pt && end > prev_pt)
10803 /* The last point was within the composition. Return 1 iff
10804 point moved out of the composition. */
10805 return (pt <= start || pt >= end);
10806 }
10807
10808 /* Check a composition at the current point. */
10809 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10810 && find_composition (pt, -1, &start, &end, &prop, buffer)
10811 && COMPOSITION_VALID_P (start, end, prop)
10812 && start < pt && end > pt);
10813 }
10814
10815
10816 /* Reconsider the setting of B->clip_changed which is displayed
10817 in window W. */
10818
10819 static INLINE void
10820 reconsider_clip_changes (w, b)
10821 struct window *w;
10822 struct buffer *b;
10823 {
10824 if (b->clip_changed
10825 && !NILP (w->window_end_valid)
10826 && w->current_matrix->buffer == b
10827 && w->current_matrix->zv == BUF_ZV (b)
10828 && w->current_matrix->begv == BUF_BEGV (b))
10829 b->clip_changed = 0;
10830
10831 /* If display wasn't paused, and W is not a tool bar window, see if
10832 point has been moved into or out of a composition. In that case,
10833 we set b->clip_changed to 1 to force updating the screen. If
10834 b->clip_changed has already been set to 1, we can skip this
10835 check. */
10836 if (!b->clip_changed
10837 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10838 {
10839 int pt;
10840
10841 if (w == XWINDOW (selected_window))
10842 pt = BUF_PT (current_buffer);
10843 else
10844 pt = marker_position (w->pointm);
10845
10846 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10847 || pt != XINT (w->last_point))
10848 && check_point_in_composition (w->current_matrix->buffer,
10849 XINT (w->last_point),
10850 XBUFFER (w->buffer), pt))
10851 b->clip_changed = 1;
10852 }
10853 }
10854 \f
10855
10856 /* Select FRAME to forward the values of frame-local variables into C
10857 variables so that the redisplay routines can access those values
10858 directly. */
10859
10860 static void
10861 select_frame_for_redisplay (frame)
10862 Lisp_Object frame;
10863 {
10864 Lisp_Object tail, sym, val;
10865 Lisp_Object old = selected_frame;
10866
10867 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10868
10869 selected_frame = frame;
10870
10871 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10872 if (CONSP (XCAR (tail))
10873 && (sym = XCAR (XCAR (tail)),
10874 SYMBOLP (sym))
10875 && (sym = indirect_variable (sym),
10876 val = SYMBOL_VALUE (sym),
10877 (BUFFER_LOCAL_VALUEP (val)))
10878 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10879 /* Use find_symbol_value rather than Fsymbol_value
10880 to avoid an error if it is void. */
10881 find_symbol_value (sym);
10882
10883 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10884 if (CONSP (XCAR (tail))
10885 && (sym = XCAR (XCAR (tail)),
10886 SYMBOLP (sym))
10887 && (sym = indirect_variable (sym),
10888 val = SYMBOL_VALUE (sym),
10889 (BUFFER_LOCAL_VALUEP (val)))
10890 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10891 find_symbol_value (sym);
10892 }
10893
10894
10895 #define STOP_POLLING \
10896 do { if (! polling_stopped_here) stop_polling (); \
10897 polling_stopped_here = 1; } while (0)
10898
10899 #define RESUME_POLLING \
10900 do { if (polling_stopped_here) start_polling (); \
10901 polling_stopped_here = 0; } while (0)
10902
10903
10904 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10905 response to any user action; therefore, we should preserve the echo
10906 area. (Actually, our caller does that job.) Perhaps in the future
10907 avoid recentering windows if it is not necessary; currently that
10908 causes some problems. */
10909
10910 static void
10911 redisplay_internal (preserve_echo_area)
10912 int preserve_echo_area;
10913 {
10914 struct window *w = XWINDOW (selected_window);
10915 struct frame *f;
10916 int pause;
10917 int must_finish = 0;
10918 struct text_pos tlbufpos, tlendpos;
10919 int number_of_visible_frames;
10920 int count, count1;
10921 struct frame *sf;
10922 int polling_stopped_here = 0;
10923 Lisp_Object old_frame = selected_frame;
10924
10925 /* Non-zero means redisplay has to consider all windows on all
10926 frames. Zero means, only selected_window is considered. */
10927 int consider_all_windows_p;
10928
10929 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10930
10931 /* No redisplay if running in batch mode or frame is not yet fully
10932 initialized, or redisplay is explicitly turned off by setting
10933 Vinhibit_redisplay. */
10934 if (noninteractive
10935 || !NILP (Vinhibit_redisplay))
10936 return;
10937
10938 /* Don't examine these until after testing Vinhibit_redisplay.
10939 When Emacs is shutting down, perhaps because its connection to
10940 X has dropped, we should not look at them at all. */
10941 f = XFRAME (w->frame);
10942 sf = SELECTED_FRAME ();
10943
10944 if (!f->glyphs_initialized_p)
10945 return;
10946
10947 /* The flag redisplay_performed_directly_p is set by
10948 direct_output_for_insert when it already did the whole screen
10949 update necessary. */
10950 if (redisplay_performed_directly_p)
10951 {
10952 redisplay_performed_directly_p = 0;
10953 if (!hscroll_windows (selected_window))
10954 return;
10955 }
10956
10957 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10958 if (popup_activated ())
10959 return;
10960 #endif
10961
10962 /* I don't think this happens but let's be paranoid. */
10963 if (redisplaying_p)
10964 return;
10965
10966 /* Record a function that resets redisplaying_p to its old value
10967 when we leave this function. */
10968 count = SPECPDL_INDEX ();
10969 record_unwind_protect (unwind_redisplay,
10970 Fcons (make_number (redisplaying_p), selected_frame));
10971 ++redisplaying_p;
10972 specbind (Qinhibit_free_realized_faces, Qnil);
10973
10974 {
10975 Lisp_Object tail, frame;
10976
10977 FOR_EACH_FRAME (tail, frame)
10978 {
10979 struct frame *f = XFRAME (frame);
10980 f->already_hscrolled_p = 0;
10981 }
10982 }
10983
10984 retry:
10985 if (!EQ (old_frame, selected_frame)
10986 && FRAME_LIVE_P (XFRAME (old_frame)))
10987 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
10988 selected_frame and selected_window to be temporarily out-of-sync so
10989 when we come back here via `goto retry', we need to resync because we
10990 may need to run Elisp code (via prepare_menu_bars). */
10991 select_frame_for_redisplay (old_frame);
10992
10993 pause = 0;
10994 reconsider_clip_changes (w, current_buffer);
10995 last_escape_glyph_frame = NULL;
10996 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10997
10998 /* If new fonts have been loaded that make a glyph matrix adjustment
10999 necessary, do it. */
11000 if (fonts_changed_p)
11001 {
11002 adjust_glyphs (NULL);
11003 ++windows_or_buffers_changed;
11004 fonts_changed_p = 0;
11005 }
11006
11007 /* If face_change_count is non-zero, init_iterator will free all
11008 realized faces, which includes the faces referenced from current
11009 matrices. So, we can't reuse current matrices in this case. */
11010 if (face_change_count)
11011 ++windows_or_buffers_changed;
11012
11013 if (FRAME_TERMCAP_P (sf)
11014 && FRAME_TTY (sf)->previous_frame != sf)
11015 {
11016 /* Since frames on a single ASCII terminal share the same
11017 display area, displaying a different frame means redisplay
11018 the whole thing. */
11019 windows_or_buffers_changed++;
11020 SET_FRAME_GARBAGED (sf);
11021 FRAME_TTY (sf)->previous_frame = sf;
11022 }
11023
11024 /* Set the visible flags for all frames. Do this before checking
11025 for resized or garbaged frames; they want to know if their frames
11026 are visible. See the comment in frame.h for
11027 FRAME_SAMPLE_VISIBILITY. */
11028 {
11029 Lisp_Object tail, frame;
11030
11031 number_of_visible_frames = 0;
11032
11033 FOR_EACH_FRAME (tail, frame)
11034 {
11035 struct frame *f = XFRAME (frame);
11036
11037 FRAME_SAMPLE_VISIBILITY (f);
11038 if (FRAME_VISIBLE_P (f))
11039 ++number_of_visible_frames;
11040 clear_desired_matrices (f);
11041 }
11042 }
11043
11044
11045 /* Notice any pending interrupt request to change frame size. */
11046 do_pending_window_change (1);
11047
11048 /* Clear frames marked as garbaged. */
11049 if (frame_garbaged)
11050 clear_garbaged_frames ();
11051
11052 /* Build menubar and tool-bar items. */
11053 if (NILP (Vmemory_full))
11054 prepare_menu_bars ();
11055
11056 if (windows_or_buffers_changed)
11057 update_mode_lines++;
11058
11059 /* Detect case that we need to write or remove a star in the mode line. */
11060 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11061 {
11062 w->update_mode_line = Qt;
11063 if (buffer_shared > 1)
11064 update_mode_lines++;
11065 }
11066
11067 /* Avoid invocation of point motion hooks by `current_column' below. */
11068 count1 = SPECPDL_INDEX ();
11069 specbind (Qinhibit_point_motion_hooks, Qt);
11070
11071 /* If %c is in the mode line, update it if needed. */
11072 if (!NILP (w->column_number_displayed)
11073 /* This alternative quickly identifies a common case
11074 where no change is needed. */
11075 && !(PT == XFASTINT (w->last_point)
11076 && XFASTINT (w->last_modified) >= MODIFF
11077 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11078 && (XFASTINT (w->column_number_displayed)
11079 != (int) current_column ())) /* iftc */
11080 w->update_mode_line = Qt;
11081
11082 unbind_to (count1, Qnil);
11083
11084 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11085
11086 /* The variable buffer_shared is set in redisplay_window and
11087 indicates that we redisplay a buffer in different windows. See
11088 there. */
11089 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11090 || cursor_type_changed);
11091
11092 /* If specs for an arrow have changed, do thorough redisplay
11093 to ensure we remove any arrow that should no longer exist. */
11094 if (overlay_arrows_changed_p ())
11095 consider_all_windows_p = windows_or_buffers_changed = 1;
11096
11097 /* Normally the message* functions will have already displayed and
11098 updated the echo area, but the frame may have been trashed, or
11099 the update may have been preempted, so display the echo area
11100 again here. Checking message_cleared_p captures the case that
11101 the echo area should be cleared. */
11102 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11103 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11104 || (message_cleared_p
11105 && minibuf_level == 0
11106 /* If the mini-window is currently selected, this means the
11107 echo-area doesn't show through. */
11108 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11109 {
11110 int window_height_changed_p = echo_area_display (0);
11111 must_finish = 1;
11112
11113 /* If we don't display the current message, don't clear the
11114 message_cleared_p flag, because, if we did, we wouldn't clear
11115 the echo area in the next redisplay which doesn't preserve
11116 the echo area. */
11117 if (!display_last_displayed_message_p)
11118 message_cleared_p = 0;
11119
11120 if (fonts_changed_p)
11121 goto retry;
11122 else if (window_height_changed_p)
11123 {
11124 consider_all_windows_p = 1;
11125 ++update_mode_lines;
11126 ++windows_or_buffers_changed;
11127
11128 /* If window configuration was changed, frames may have been
11129 marked garbaged. Clear them or we will experience
11130 surprises wrt scrolling. */
11131 if (frame_garbaged)
11132 clear_garbaged_frames ();
11133 }
11134 }
11135 else if (EQ (selected_window, minibuf_window)
11136 && (current_buffer->clip_changed
11137 || XFASTINT (w->last_modified) < MODIFF
11138 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11139 && resize_mini_window (w, 0))
11140 {
11141 /* Resized active mini-window to fit the size of what it is
11142 showing if its contents might have changed. */
11143 must_finish = 1;
11144 consider_all_windows_p = 1;
11145 ++windows_or_buffers_changed;
11146 ++update_mode_lines;
11147
11148 /* If window configuration was changed, frames may have been
11149 marked garbaged. Clear them or we will experience
11150 surprises wrt scrolling. */
11151 if (frame_garbaged)
11152 clear_garbaged_frames ();
11153 }
11154
11155
11156 /* If showing the region, and mark has changed, we must redisplay
11157 the whole window. The assignment to this_line_start_pos prevents
11158 the optimization directly below this if-statement. */
11159 if (((!NILP (Vtransient_mark_mode)
11160 && !NILP (XBUFFER (w->buffer)->mark_active))
11161 != !NILP (w->region_showing))
11162 || (!NILP (w->region_showing)
11163 && !EQ (w->region_showing,
11164 Fmarker_position (XBUFFER (w->buffer)->mark))))
11165 CHARPOS (this_line_start_pos) = 0;
11166
11167 /* Optimize the case that only the line containing the cursor in the
11168 selected window has changed. Variables starting with this_ are
11169 set in display_line and record information about the line
11170 containing the cursor. */
11171 tlbufpos = this_line_start_pos;
11172 tlendpos = this_line_end_pos;
11173 if (!consider_all_windows_p
11174 && CHARPOS (tlbufpos) > 0
11175 && NILP (w->update_mode_line)
11176 && !current_buffer->clip_changed
11177 && !current_buffer->prevent_redisplay_optimizations_p
11178 && FRAME_VISIBLE_P (XFRAME (w->frame))
11179 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11180 /* Make sure recorded data applies to current buffer, etc. */
11181 && this_line_buffer == current_buffer
11182 && current_buffer == XBUFFER (w->buffer)
11183 && NILP (w->force_start)
11184 && NILP (w->optional_new_start)
11185 /* Point must be on the line that we have info recorded about. */
11186 && PT >= CHARPOS (tlbufpos)
11187 && PT <= Z - CHARPOS (tlendpos)
11188 /* All text outside that line, including its final newline,
11189 must be unchanged */
11190 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11191 CHARPOS (tlendpos)))
11192 {
11193 if (CHARPOS (tlbufpos) > BEGV
11194 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11195 && (CHARPOS (tlbufpos) == ZV
11196 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11197 /* Former continuation line has disappeared by becoming empty */
11198 goto cancel;
11199 else if (XFASTINT (w->last_modified) < MODIFF
11200 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11201 || MINI_WINDOW_P (w))
11202 {
11203 /* We have to handle the case of continuation around a
11204 wide-column character (See the comment in indent.c around
11205 line 885).
11206
11207 For instance, in the following case:
11208
11209 -------- Insert --------
11210 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11211 J_I_ ==> J_I_ `^^' are cursors.
11212 ^^ ^^
11213 -------- --------
11214
11215 As we have to redraw the line above, we should goto cancel. */
11216
11217 struct it it;
11218 int line_height_before = this_line_pixel_height;
11219
11220 /* Note that start_display will handle the case that the
11221 line starting at tlbufpos is a continuation lines. */
11222 start_display (&it, w, tlbufpos);
11223
11224 /* Implementation note: It this still necessary? */
11225 if (it.current_x != this_line_start_x)
11226 goto cancel;
11227
11228 TRACE ((stderr, "trying display optimization 1\n"));
11229 w->cursor.vpos = -1;
11230 overlay_arrow_seen = 0;
11231 it.vpos = this_line_vpos;
11232 it.current_y = this_line_y;
11233 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11234 display_line (&it);
11235
11236 /* If line contains point, is not continued,
11237 and ends at same distance from eob as before, we win */
11238 if (w->cursor.vpos >= 0
11239 /* Line is not continued, otherwise this_line_start_pos
11240 would have been set to 0 in display_line. */
11241 && CHARPOS (this_line_start_pos)
11242 /* Line ends as before. */
11243 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11244 /* Line has same height as before. Otherwise other lines
11245 would have to be shifted up or down. */
11246 && this_line_pixel_height == line_height_before)
11247 {
11248 /* If this is not the window's last line, we must adjust
11249 the charstarts of the lines below. */
11250 if (it.current_y < it.last_visible_y)
11251 {
11252 struct glyph_row *row
11253 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11254 int delta, delta_bytes;
11255
11256 if (Z - CHARPOS (tlendpos) == ZV)
11257 {
11258 /* This line ends at end of (accessible part of)
11259 buffer. There is no newline to count. */
11260 delta = (Z
11261 - CHARPOS (tlendpos)
11262 - MATRIX_ROW_START_CHARPOS (row));
11263 delta_bytes = (Z_BYTE
11264 - BYTEPOS (tlendpos)
11265 - MATRIX_ROW_START_BYTEPOS (row));
11266 }
11267 else
11268 {
11269 /* This line ends in a newline. Must take
11270 account of the newline and the rest of the
11271 text that follows. */
11272 delta = (Z
11273 - CHARPOS (tlendpos)
11274 - MATRIX_ROW_START_CHARPOS (row));
11275 delta_bytes = (Z_BYTE
11276 - BYTEPOS (tlendpos)
11277 - MATRIX_ROW_START_BYTEPOS (row));
11278 }
11279
11280 increment_matrix_positions (w->current_matrix,
11281 this_line_vpos + 1,
11282 w->current_matrix->nrows,
11283 delta, delta_bytes);
11284 }
11285
11286 /* If this row displays text now but previously didn't,
11287 or vice versa, w->window_end_vpos may have to be
11288 adjusted. */
11289 if ((it.glyph_row - 1)->displays_text_p)
11290 {
11291 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11292 XSETINT (w->window_end_vpos, this_line_vpos);
11293 }
11294 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11295 && this_line_vpos > 0)
11296 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11297 w->window_end_valid = Qnil;
11298
11299 /* Update hint: No need to try to scroll in update_window. */
11300 w->desired_matrix->no_scrolling_p = 1;
11301
11302 #if GLYPH_DEBUG
11303 *w->desired_matrix->method = 0;
11304 debug_method_add (w, "optimization 1");
11305 #endif
11306 #ifdef HAVE_WINDOW_SYSTEM
11307 update_window_fringes (w, 0);
11308 #endif
11309 goto update;
11310 }
11311 else
11312 goto cancel;
11313 }
11314 else if (/* Cursor position hasn't changed. */
11315 PT == XFASTINT (w->last_point)
11316 /* Make sure the cursor was last displayed
11317 in this window. Otherwise we have to reposition it. */
11318 && 0 <= w->cursor.vpos
11319 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11320 {
11321 if (!must_finish)
11322 {
11323 do_pending_window_change (1);
11324
11325 /* We used to always goto end_of_redisplay here, but this
11326 isn't enough if we have a blinking cursor. */
11327 if (w->cursor_off_p == w->last_cursor_off_p)
11328 goto end_of_redisplay;
11329 }
11330 goto update;
11331 }
11332 /* If highlighting the region, or if the cursor is in the echo area,
11333 then we can't just move the cursor. */
11334 else if (! (!NILP (Vtransient_mark_mode)
11335 && !NILP (current_buffer->mark_active))
11336 && (EQ (selected_window, current_buffer->last_selected_window)
11337 || highlight_nonselected_windows)
11338 && NILP (w->region_showing)
11339 && NILP (Vshow_trailing_whitespace)
11340 && !cursor_in_echo_area)
11341 {
11342 struct it it;
11343 struct glyph_row *row;
11344
11345 /* Skip from tlbufpos to PT and see where it is. Note that
11346 PT may be in invisible text. If so, we will end at the
11347 next visible position. */
11348 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11349 NULL, DEFAULT_FACE_ID);
11350 it.current_x = this_line_start_x;
11351 it.current_y = this_line_y;
11352 it.vpos = this_line_vpos;
11353
11354 /* The call to move_it_to stops in front of PT, but
11355 moves over before-strings. */
11356 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11357
11358 if (it.vpos == this_line_vpos
11359 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11360 row->enabled_p))
11361 {
11362 xassert (this_line_vpos == it.vpos);
11363 xassert (this_line_y == it.current_y);
11364 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11365 #if GLYPH_DEBUG
11366 *w->desired_matrix->method = 0;
11367 debug_method_add (w, "optimization 3");
11368 #endif
11369 goto update;
11370 }
11371 else
11372 goto cancel;
11373 }
11374
11375 cancel:
11376 /* Text changed drastically or point moved off of line. */
11377 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11378 }
11379
11380 CHARPOS (this_line_start_pos) = 0;
11381 consider_all_windows_p |= buffer_shared > 1;
11382 ++clear_face_cache_count;
11383 #ifdef HAVE_WINDOW_SYSTEM
11384 ++clear_image_cache_count;
11385 #endif
11386
11387 /* Build desired matrices, and update the display. If
11388 consider_all_windows_p is non-zero, do it for all windows on all
11389 frames. Otherwise do it for selected_window, only. */
11390
11391 if (consider_all_windows_p)
11392 {
11393 Lisp_Object tail, frame;
11394
11395 FOR_EACH_FRAME (tail, frame)
11396 XFRAME (frame)->updated_p = 0;
11397
11398 /* Recompute # windows showing selected buffer. This will be
11399 incremented each time such a window is displayed. */
11400 buffer_shared = 0;
11401
11402 FOR_EACH_FRAME (tail, frame)
11403 {
11404 struct frame *f = XFRAME (frame);
11405
11406 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11407 {
11408 if (! EQ (frame, selected_frame))
11409 /* Select the frame, for the sake of frame-local
11410 variables. */
11411 select_frame_for_redisplay (frame);
11412
11413 /* Mark all the scroll bars to be removed; we'll redeem
11414 the ones we want when we redisplay their windows. */
11415 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11416 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11417
11418 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11419 redisplay_windows (FRAME_ROOT_WINDOW (f));
11420
11421 /* Any scroll bars which redisplay_windows should have
11422 nuked should now go away. */
11423 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11424 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11425
11426 /* If fonts changed, display again. */
11427 /* ??? rms: I suspect it is a mistake to jump all the way
11428 back to retry here. It should just retry this frame. */
11429 if (fonts_changed_p)
11430 goto retry;
11431
11432 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11433 {
11434 /* See if we have to hscroll. */
11435 if (!f->already_hscrolled_p)
11436 {
11437 f->already_hscrolled_p = 1;
11438 if (hscroll_windows (f->root_window))
11439 goto retry;
11440 }
11441
11442 /* Prevent various kinds of signals during display
11443 update. stdio is not robust about handling
11444 signals, which can cause an apparent I/O
11445 error. */
11446 if (interrupt_input)
11447 unrequest_sigio ();
11448 STOP_POLLING;
11449
11450 /* Update the display. */
11451 set_window_update_flags (XWINDOW (f->root_window), 1);
11452 pause |= update_frame (f, 0, 0);
11453 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11454 if (pause)
11455 break;
11456 #endif
11457
11458 f->updated_p = 1;
11459 }
11460 }
11461 }
11462
11463 if (!pause)
11464 {
11465 /* Do the mark_window_display_accurate after all windows have
11466 been redisplayed because this call resets flags in buffers
11467 which are needed for proper redisplay. */
11468 FOR_EACH_FRAME (tail, frame)
11469 {
11470 struct frame *f = XFRAME (frame);
11471 if (f->updated_p)
11472 {
11473 mark_window_display_accurate (f->root_window, 1);
11474 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11475 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11476 }
11477 }
11478 }
11479 }
11480 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11481 {
11482 Lisp_Object mini_window;
11483 struct frame *mini_frame;
11484
11485 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11486 /* Use list_of_error, not Qerror, so that
11487 we catch only errors and don't run the debugger. */
11488 internal_condition_case_1 (redisplay_window_1, selected_window,
11489 list_of_error,
11490 redisplay_window_error);
11491
11492 /* Compare desired and current matrices, perform output. */
11493
11494 update:
11495 /* If fonts changed, display again. */
11496 if (fonts_changed_p)
11497 goto retry;
11498
11499 /* Prevent various kinds of signals during display update.
11500 stdio is not robust about handling signals,
11501 which can cause an apparent I/O error. */
11502 if (interrupt_input)
11503 unrequest_sigio ();
11504 STOP_POLLING;
11505
11506 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11507 {
11508 if (hscroll_windows (selected_window))
11509 goto retry;
11510
11511 XWINDOW (selected_window)->must_be_updated_p = 1;
11512 pause = update_frame (sf, 0, 0);
11513 }
11514
11515 /* We may have called echo_area_display at the top of this
11516 function. If the echo area is on another frame, that may
11517 have put text on a frame other than the selected one, so the
11518 above call to update_frame would not have caught it. Catch
11519 it here. */
11520 mini_window = FRAME_MINIBUF_WINDOW (sf);
11521 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11522
11523 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11524 {
11525 XWINDOW (mini_window)->must_be_updated_p = 1;
11526 pause |= update_frame (mini_frame, 0, 0);
11527 if (!pause && hscroll_windows (mini_window))
11528 goto retry;
11529 }
11530 }
11531
11532 /* If display was paused because of pending input, make sure we do a
11533 thorough update the next time. */
11534 if (pause)
11535 {
11536 /* Prevent the optimization at the beginning of
11537 redisplay_internal that tries a single-line update of the
11538 line containing the cursor in the selected window. */
11539 CHARPOS (this_line_start_pos) = 0;
11540
11541 /* Let the overlay arrow be updated the next time. */
11542 update_overlay_arrows (0);
11543
11544 /* If we pause after scrolling, some rows in the current
11545 matrices of some windows are not valid. */
11546 if (!WINDOW_FULL_WIDTH_P (w)
11547 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11548 update_mode_lines = 1;
11549 }
11550 else
11551 {
11552 if (!consider_all_windows_p)
11553 {
11554 /* This has already been done above if
11555 consider_all_windows_p is set. */
11556 mark_window_display_accurate_1 (w, 1);
11557
11558 /* Say overlay arrows are up to date. */
11559 update_overlay_arrows (1);
11560
11561 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11562 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11563 }
11564
11565 update_mode_lines = 0;
11566 windows_or_buffers_changed = 0;
11567 cursor_type_changed = 0;
11568 }
11569
11570 /* Start SIGIO interrupts coming again. Having them off during the
11571 code above makes it less likely one will discard output, but not
11572 impossible, since there might be stuff in the system buffer here.
11573 But it is much hairier to try to do anything about that. */
11574 if (interrupt_input)
11575 request_sigio ();
11576 RESUME_POLLING;
11577
11578 /* If a frame has become visible which was not before, redisplay
11579 again, so that we display it. Expose events for such a frame
11580 (which it gets when becoming visible) don't call the parts of
11581 redisplay constructing glyphs, so simply exposing a frame won't
11582 display anything in this case. So, we have to display these
11583 frames here explicitly. */
11584 if (!pause)
11585 {
11586 Lisp_Object tail, frame;
11587 int new_count = 0;
11588
11589 FOR_EACH_FRAME (tail, frame)
11590 {
11591 int this_is_visible = 0;
11592
11593 if (XFRAME (frame)->visible)
11594 this_is_visible = 1;
11595 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11596 if (XFRAME (frame)->visible)
11597 this_is_visible = 1;
11598
11599 if (this_is_visible)
11600 new_count++;
11601 }
11602
11603 if (new_count != number_of_visible_frames)
11604 windows_or_buffers_changed++;
11605 }
11606
11607 /* Change frame size now if a change is pending. */
11608 do_pending_window_change (1);
11609
11610 /* If we just did a pending size change, or have additional
11611 visible frames, redisplay again. */
11612 if (windows_or_buffers_changed && !pause)
11613 goto retry;
11614
11615 /* Clear the face cache eventually. */
11616 if (consider_all_windows_p)
11617 {
11618 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11619 {
11620 clear_face_cache (0);
11621 clear_face_cache_count = 0;
11622 }
11623 #ifdef HAVE_WINDOW_SYSTEM
11624 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11625 {
11626 Lisp_Object tail, frame;
11627 FOR_EACH_FRAME (tail, frame)
11628 {
11629 struct frame *f = XFRAME (frame);
11630 if (FRAME_WINDOW_P (f))
11631 clear_image_cache (f, 0);
11632 }
11633 clear_image_cache_count = 0;
11634 }
11635 #endif /* HAVE_WINDOW_SYSTEM */
11636 }
11637
11638 end_of_redisplay:
11639 unbind_to (count, Qnil);
11640 RESUME_POLLING;
11641 }
11642
11643
11644 /* Redisplay, but leave alone any recent echo area message unless
11645 another message has been requested in its place.
11646
11647 This is useful in situations where you need to redisplay but no
11648 user action has occurred, making it inappropriate for the message
11649 area to be cleared. See tracking_off and
11650 wait_reading_process_output for examples of these situations.
11651
11652 FROM_WHERE is an integer saying from where this function was
11653 called. This is useful for debugging. */
11654
11655 void
11656 redisplay_preserve_echo_area (from_where)
11657 int from_where;
11658 {
11659 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11660
11661 if (!NILP (echo_area_buffer[1]))
11662 {
11663 /* We have a previously displayed message, but no current
11664 message. Redisplay the previous message. */
11665 display_last_displayed_message_p = 1;
11666 redisplay_internal (1);
11667 display_last_displayed_message_p = 0;
11668 }
11669 else
11670 redisplay_internal (1);
11671
11672 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11673 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11674 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11675 }
11676
11677
11678 /* Function registered with record_unwind_protect in
11679 redisplay_internal. Reset redisplaying_p to the value it had
11680 before redisplay_internal was called, and clear
11681 prevent_freeing_realized_faces_p. It also selects the previously
11682 selected frame, unless it has been deleted (by an X connection
11683 failure during redisplay, for example). */
11684
11685 static Lisp_Object
11686 unwind_redisplay (val)
11687 Lisp_Object val;
11688 {
11689 Lisp_Object old_redisplaying_p, old_frame;
11690
11691 old_redisplaying_p = XCAR (val);
11692 redisplaying_p = XFASTINT (old_redisplaying_p);
11693 old_frame = XCDR (val);
11694 if (! EQ (old_frame, selected_frame)
11695 && FRAME_LIVE_P (XFRAME (old_frame)))
11696 select_frame_for_redisplay (old_frame);
11697 return Qnil;
11698 }
11699
11700
11701 /* Mark the display of window W as accurate or inaccurate. If
11702 ACCURATE_P is non-zero mark display of W as accurate. If
11703 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11704 redisplay_internal is called. */
11705
11706 static void
11707 mark_window_display_accurate_1 (w, accurate_p)
11708 struct window *w;
11709 int accurate_p;
11710 {
11711 if (BUFFERP (w->buffer))
11712 {
11713 struct buffer *b = XBUFFER (w->buffer);
11714
11715 w->last_modified
11716 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11717 w->last_overlay_modified
11718 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11719 w->last_had_star
11720 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11721
11722 if (accurate_p)
11723 {
11724 b->clip_changed = 0;
11725 b->prevent_redisplay_optimizations_p = 0;
11726
11727 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11728 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11729 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11730 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11731
11732 w->current_matrix->buffer = b;
11733 w->current_matrix->begv = BUF_BEGV (b);
11734 w->current_matrix->zv = BUF_ZV (b);
11735
11736 w->last_cursor = w->cursor;
11737 w->last_cursor_off_p = w->cursor_off_p;
11738
11739 if (w == XWINDOW (selected_window))
11740 w->last_point = make_number (BUF_PT (b));
11741 else
11742 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11743 }
11744 }
11745
11746 if (accurate_p)
11747 {
11748 w->window_end_valid = w->buffer;
11749 #if 0 /* This is incorrect with variable-height lines. */
11750 xassert (XINT (w->window_end_vpos)
11751 < (WINDOW_TOTAL_LINES (w)
11752 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11753 #endif
11754 w->update_mode_line = Qnil;
11755 }
11756 }
11757
11758
11759 /* Mark the display of windows in the window tree rooted at WINDOW as
11760 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11761 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11762 be redisplayed the next time redisplay_internal is called. */
11763
11764 void
11765 mark_window_display_accurate (window, accurate_p)
11766 Lisp_Object window;
11767 int accurate_p;
11768 {
11769 struct window *w;
11770
11771 for (; !NILP (window); window = w->next)
11772 {
11773 w = XWINDOW (window);
11774 mark_window_display_accurate_1 (w, accurate_p);
11775
11776 if (!NILP (w->vchild))
11777 mark_window_display_accurate (w->vchild, accurate_p);
11778 if (!NILP (w->hchild))
11779 mark_window_display_accurate (w->hchild, accurate_p);
11780 }
11781
11782 if (accurate_p)
11783 {
11784 update_overlay_arrows (1);
11785 }
11786 else
11787 {
11788 /* Force a thorough redisplay the next time by setting
11789 last_arrow_position and last_arrow_string to t, which is
11790 unequal to any useful value of Voverlay_arrow_... */
11791 update_overlay_arrows (-1);
11792 }
11793 }
11794
11795
11796 /* Return value in display table DP (Lisp_Char_Table *) for character
11797 C. Since a display table doesn't have any parent, we don't have to
11798 follow parent. Do not call this function directly but use the
11799 macro DISP_CHAR_VECTOR. */
11800
11801 Lisp_Object
11802 disp_char_vector (dp, c)
11803 struct Lisp_Char_Table *dp;
11804 int c;
11805 {
11806 int code[4], i;
11807 Lisp_Object val;
11808
11809 if (SINGLE_BYTE_CHAR_P (c))
11810 return (dp->contents[c]);
11811
11812 SPLIT_CHAR (c, code[0], code[1], code[2]);
11813 if (code[1] < 32)
11814 code[1] = -1;
11815 else if (code[2] < 32)
11816 code[2] = -1;
11817
11818 /* Here, the possible range of code[0] (== charset ID) is
11819 128..max_charset. Since the top level char table contains data
11820 for multibyte characters after 256th element, we must increment
11821 code[0] by 128 to get a correct index. */
11822 code[0] += 128;
11823 code[3] = -1; /* anchor */
11824
11825 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11826 {
11827 val = dp->contents[code[i]];
11828 if (!SUB_CHAR_TABLE_P (val))
11829 return (NILP (val) ? dp->defalt : val);
11830 }
11831
11832 /* Here, val is a sub char table. We return the default value of
11833 it. */
11834 return (dp->defalt);
11835 }
11836
11837
11838 \f
11839 /***********************************************************************
11840 Window Redisplay
11841 ***********************************************************************/
11842
11843 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11844
11845 static void
11846 redisplay_windows (window)
11847 Lisp_Object window;
11848 {
11849 while (!NILP (window))
11850 {
11851 struct window *w = XWINDOW (window);
11852
11853 if (!NILP (w->hchild))
11854 redisplay_windows (w->hchild);
11855 else if (!NILP (w->vchild))
11856 redisplay_windows (w->vchild);
11857 else
11858 {
11859 displayed_buffer = XBUFFER (w->buffer);
11860 /* Use list_of_error, not Qerror, so that
11861 we catch only errors and don't run the debugger. */
11862 internal_condition_case_1 (redisplay_window_0, window,
11863 list_of_error,
11864 redisplay_window_error);
11865 }
11866
11867 window = w->next;
11868 }
11869 }
11870
11871 static Lisp_Object
11872 redisplay_window_error ()
11873 {
11874 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11875 return Qnil;
11876 }
11877
11878 static Lisp_Object
11879 redisplay_window_0 (window)
11880 Lisp_Object window;
11881 {
11882 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11883 redisplay_window (window, 0);
11884 return Qnil;
11885 }
11886
11887 static Lisp_Object
11888 redisplay_window_1 (window)
11889 Lisp_Object window;
11890 {
11891 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11892 redisplay_window (window, 1);
11893 return Qnil;
11894 }
11895 \f
11896
11897 /* Increment GLYPH until it reaches END or CONDITION fails while
11898 adding (GLYPH)->pixel_width to X. */
11899
11900 #define SKIP_GLYPHS(glyph, end, x, condition) \
11901 do \
11902 { \
11903 (x) += (glyph)->pixel_width; \
11904 ++(glyph); \
11905 } \
11906 while ((glyph) < (end) && (condition))
11907
11908
11909 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11910 DELTA is the number of bytes by which positions recorded in ROW
11911 differ from current buffer positions.
11912
11913 Return 0 if cursor is not on this row. 1 otherwise. */
11914
11915 int
11916 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11917 struct window *w;
11918 struct glyph_row *row;
11919 struct glyph_matrix *matrix;
11920 int delta, delta_bytes, dy, dvpos;
11921 {
11922 struct glyph *glyph = row->glyphs[TEXT_AREA];
11923 struct glyph *end = glyph + row->used[TEXT_AREA];
11924 struct glyph *cursor = NULL;
11925 /* The first glyph that starts a sequence of glyphs from string. */
11926 struct glyph *string_start;
11927 /* The X coordinate of string_start. */
11928 int string_start_x;
11929 /* The last known character position. */
11930 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11931 /* The last known character position before string_start. */
11932 int string_before_pos;
11933 int x = row->x;
11934 int cursor_x = x;
11935 int cursor_from_overlay_pos = 0;
11936 int pt_old = PT - delta;
11937
11938 /* Skip over glyphs not having an object at the start of the row.
11939 These are special glyphs like truncation marks on terminal
11940 frames. */
11941 if (row->displays_text_p)
11942 while (glyph < end
11943 && INTEGERP (glyph->object)
11944 && glyph->charpos < 0)
11945 {
11946 x += glyph->pixel_width;
11947 ++glyph;
11948 }
11949
11950 string_start = NULL;
11951 while (glyph < end
11952 && !INTEGERP (glyph->object)
11953 && (!BUFFERP (glyph->object)
11954 || (last_pos = glyph->charpos) < pt_old))
11955 {
11956 if (! STRINGP (glyph->object))
11957 {
11958 string_start = NULL;
11959 x += glyph->pixel_width;
11960 ++glyph;
11961 if (cursor_from_overlay_pos
11962 && last_pos >= cursor_from_overlay_pos)
11963 {
11964 cursor_from_overlay_pos = 0;
11965 cursor = 0;
11966 }
11967 }
11968 else
11969 {
11970 if (string_start == NULL)
11971 {
11972 string_before_pos = last_pos;
11973 string_start = glyph;
11974 string_start_x = x;
11975 }
11976 /* Skip all glyphs from string. */
11977 do
11978 {
11979 Lisp_Object cprop;
11980 int pos;
11981 if ((cursor == NULL || glyph > cursor)
11982 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11983 Qcursor, (glyph)->object),
11984 !NILP (cprop))
11985 && (pos = string_buffer_position (w, glyph->object,
11986 string_before_pos),
11987 (pos == 0 /* From overlay */
11988 || pos == pt_old)))
11989 {
11990 /* Estimate overlay buffer position from the buffer
11991 positions of the glyphs before and after the overlay.
11992 Add 1 to last_pos so that if point corresponds to the
11993 glyph right after the overlay, we still use a 'cursor'
11994 property found in that overlay. */
11995 cursor_from_overlay_pos = (pos ? 0 : last_pos
11996 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11997 cursor = glyph;
11998 cursor_x = x;
11999 }
12000 x += glyph->pixel_width;
12001 ++glyph;
12002 }
12003 while (glyph < end && EQ (glyph->object, string_start->object));
12004 }
12005 }
12006
12007 if (cursor != NULL)
12008 {
12009 glyph = cursor;
12010 x = cursor_x;
12011 }
12012 else if (row->ends_in_ellipsis_p && glyph == end)
12013 {
12014 /* Scan back over the ellipsis glyphs, decrementing positions. */
12015 while (glyph > row->glyphs[TEXT_AREA]
12016 && (glyph - 1)->charpos == last_pos)
12017 glyph--, x -= glyph->pixel_width;
12018 /* That loop always goes one position too far,
12019 including the glyph before the ellipsis.
12020 So scan forward over that one. */
12021 x += glyph->pixel_width;
12022 glyph++;
12023 }
12024 else if (string_start
12025 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12026 {
12027 /* We may have skipped over point because the previous glyphs
12028 are from string. As there's no easy way to know the
12029 character position of the current glyph, find the correct
12030 glyph on point by scanning from string_start again. */
12031 Lisp_Object limit;
12032 Lisp_Object string;
12033 struct glyph *stop = glyph;
12034 int pos;
12035
12036 limit = make_number (pt_old + 1);
12037 glyph = string_start;
12038 x = string_start_x;
12039 string = glyph->object;
12040 pos = string_buffer_position (w, string, string_before_pos);
12041 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12042 because we always put cursor after overlay strings. */
12043 while (pos == 0 && glyph < stop)
12044 {
12045 string = glyph->object;
12046 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12047 if (glyph < stop)
12048 pos = string_buffer_position (w, glyph->object, string_before_pos);
12049 }
12050
12051 while (glyph < stop)
12052 {
12053 pos = XINT (Fnext_single_char_property_change
12054 (make_number (pos), Qdisplay, Qnil, limit));
12055 if (pos > pt_old)
12056 break;
12057 /* Skip glyphs from the same string. */
12058 string = glyph->object;
12059 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12060 /* Skip glyphs from an overlay. */
12061 while (glyph < stop
12062 && ! string_buffer_position (w, glyph->object, pos))
12063 {
12064 string = glyph->object;
12065 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12066 }
12067 }
12068
12069 /* If we reached the end of the line, and end was from a string,
12070 cursor is not on this line. */
12071 if (glyph == end && row->continued_p)
12072 return 0;
12073 }
12074
12075 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12076 w->cursor.x = x;
12077 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12078 w->cursor.y = row->y + dy;
12079
12080 if (w == XWINDOW (selected_window))
12081 {
12082 if (!row->continued_p
12083 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12084 && row->x == 0)
12085 {
12086 this_line_buffer = XBUFFER (w->buffer);
12087
12088 CHARPOS (this_line_start_pos)
12089 = MATRIX_ROW_START_CHARPOS (row) + delta;
12090 BYTEPOS (this_line_start_pos)
12091 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12092
12093 CHARPOS (this_line_end_pos)
12094 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12095 BYTEPOS (this_line_end_pos)
12096 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12097
12098 this_line_y = w->cursor.y;
12099 this_line_pixel_height = row->height;
12100 this_line_vpos = w->cursor.vpos;
12101 this_line_start_x = row->x;
12102 }
12103 else
12104 CHARPOS (this_line_start_pos) = 0;
12105 }
12106
12107 return 1;
12108 }
12109
12110
12111 /* Run window scroll functions, if any, for WINDOW with new window
12112 start STARTP. Sets the window start of WINDOW to that position.
12113
12114 We assume that the window's buffer is really current. */
12115
12116 static INLINE struct text_pos
12117 run_window_scroll_functions (window, startp)
12118 Lisp_Object window;
12119 struct text_pos startp;
12120 {
12121 struct window *w = XWINDOW (window);
12122 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12123
12124 if (current_buffer != XBUFFER (w->buffer))
12125 abort ();
12126
12127 if (!NILP (Vwindow_scroll_functions))
12128 {
12129 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12130 make_number (CHARPOS (startp)));
12131 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12132 /* In case the hook functions switch buffers. */
12133 if (current_buffer != XBUFFER (w->buffer))
12134 set_buffer_internal_1 (XBUFFER (w->buffer));
12135 }
12136
12137 return startp;
12138 }
12139
12140
12141 /* Make sure the line containing the cursor is fully visible.
12142 A value of 1 means there is nothing to be done.
12143 (Either the line is fully visible, or it cannot be made so,
12144 or we cannot tell.)
12145
12146 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12147 is higher than window.
12148
12149 A value of 0 means the caller should do scrolling
12150 as if point had gone off the screen. */
12151
12152 static int
12153 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12154 struct window *w;
12155 int force_p;
12156 int current_matrix_p;
12157 {
12158 struct glyph_matrix *matrix;
12159 struct glyph_row *row;
12160 int window_height;
12161
12162 if (!make_cursor_line_fully_visible_p)
12163 return 1;
12164
12165 /* It's not always possible to find the cursor, e.g, when a window
12166 is full of overlay strings. Don't do anything in that case. */
12167 if (w->cursor.vpos < 0)
12168 return 1;
12169
12170 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12171 row = MATRIX_ROW (matrix, w->cursor.vpos);
12172
12173 /* If the cursor row is not partially visible, there's nothing to do. */
12174 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12175 return 1;
12176
12177 /* If the row the cursor is in is taller than the window's height,
12178 it's not clear what to do, so do nothing. */
12179 window_height = window_box_height (w);
12180 if (row->height >= window_height)
12181 {
12182 if (!force_p || MINI_WINDOW_P (w)
12183 || w->vscroll || w->cursor.vpos == 0)
12184 return 1;
12185 }
12186 return 0;
12187
12188 #if 0
12189 /* This code used to try to scroll the window just enough to make
12190 the line visible. It returned 0 to say that the caller should
12191 allocate larger glyph matrices. */
12192
12193 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12194 {
12195 int dy = row->height - row->visible_height;
12196 w->vscroll = 0;
12197 w->cursor.y += dy;
12198 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12199 }
12200 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12201 {
12202 int dy = - (row->height - row->visible_height);
12203 w->vscroll = dy;
12204 w->cursor.y += dy;
12205 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12206 }
12207
12208 /* When we change the cursor y-position of the selected window,
12209 change this_line_y as well so that the display optimization for
12210 the cursor line of the selected window in redisplay_internal uses
12211 the correct y-position. */
12212 if (w == XWINDOW (selected_window))
12213 this_line_y = w->cursor.y;
12214
12215 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12216 redisplay with larger matrices. */
12217 if (matrix->nrows < required_matrix_height (w))
12218 {
12219 fonts_changed_p = 1;
12220 return 0;
12221 }
12222
12223 return 1;
12224 #endif /* 0 */
12225 }
12226
12227
12228 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12229 non-zero means only WINDOW is redisplayed in redisplay_internal.
12230 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12231 in redisplay_window to bring a partially visible line into view in
12232 the case that only the cursor has moved.
12233
12234 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12235 last screen line's vertical height extends past the end of the screen.
12236
12237 Value is
12238
12239 1 if scrolling succeeded
12240
12241 0 if scrolling didn't find point.
12242
12243 -1 if new fonts have been loaded so that we must interrupt
12244 redisplay, adjust glyph matrices, and try again. */
12245
12246 enum
12247 {
12248 SCROLLING_SUCCESS,
12249 SCROLLING_FAILED,
12250 SCROLLING_NEED_LARGER_MATRICES
12251 };
12252
12253 static int
12254 try_scrolling (window, just_this_one_p, scroll_conservatively,
12255 scroll_step, temp_scroll_step, last_line_misfit)
12256 Lisp_Object window;
12257 int just_this_one_p;
12258 EMACS_INT scroll_conservatively, scroll_step;
12259 int temp_scroll_step;
12260 int last_line_misfit;
12261 {
12262 struct window *w = XWINDOW (window);
12263 struct frame *f = XFRAME (w->frame);
12264 struct text_pos scroll_margin_pos;
12265 struct text_pos pos;
12266 struct text_pos startp;
12267 struct it it;
12268 Lisp_Object window_end;
12269 int this_scroll_margin;
12270 int dy = 0;
12271 int scroll_max;
12272 int rc;
12273 int amount_to_scroll = 0;
12274 Lisp_Object aggressive;
12275 int height;
12276 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12277
12278 #if GLYPH_DEBUG
12279 debug_method_add (w, "try_scrolling");
12280 #endif
12281
12282 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12283
12284 /* Compute scroll margin height in pixels. We scroll when point is
12285 within this distance from the top or bottom of the window. */
12286 if (scroll_margin > 0)
12287 {
12288 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12289 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12290 }
12291 else
12292 this_scroll_margin = 0;
12293
12294 /* Force scroll_conservatively to have a reasonable value so it doesn't
12295 cause an overflow while computing how much to scroll. */
12296 if (scroll_conservatively)
12297 scroll_conservatively = min (scroll_conservatively,
12298 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12299
12300 /* Compute how much we should try to scroll maximally to bring point
12301 into view. */
12302 if (scroll_step || scroll_conservatively || temp_scroll_step)
12303 scroll_max = max (scroll_step,
12304 max (scroll_conservatively, temp_scroll_step));
12305 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12306 || NUMBERP (current_buffer->scroll_up_aggressively))
12307 /* We're trying to scroll because of aggressive scrolling
12308 but no scroll_step is set. Choose an arbitrary one. Maybe
12309 there should be a variable for this. */
12310 scroll_max = 10;
12311 else
12312 scroll_max = 0;
12313 scroll_max *= FRAME_LINE_HEIGHT (f);
12314
12315 /* Decide whether we have to scroll down. Start at the window end
12316 and move this_scroll_margin up to find the position of the scroll
12317 margin. */
12318 window_end = Fwindow_end (window, Qt);
12319
12320 too_near_end:
12321
12322 CHARPOS (scroll_margin_pos) = XINT (window_end);
12323 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12324
12325 if (this_scroll_margin || extra_scroll_margin_lines)
12326 {
12327 start_display (&it, w, scroll_margin_pos);
12328 if (this_scroll_margin)
12329 move_it_vertically_backward (&it, this_scroll_margin);
12330 if (extra_scroll_margin_lines)
12331 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12332 scroll_margin_pos = it.current.pos;
12333 }
12334
12335 if (PT >= CHARPOS (scroll_margin_pos))
12336 {
12337 int y0;
12338
12339 /* Point is in the scroll margin at the bottom of the window, or
12340 below. Compute a new window start that makes point visible. */
12341
12342 /* Compute the distance from the scroll margin to PT.
12343 Give up if the distance is greater than scroll_max. */
12344 start_display (&it, w, scroll_margin_pos);
12345 y0 = it.current_y;
12346 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12347 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12348
12349 /* To make point visible, we have to move the window start
12350 down so that the line the cursor is in is visible, which
12351 means we have to add in the height of the cursor line. */
12352 dy = line_bottom_y (&it) - y0;
12353
12354 if (dy > scroll_max)
12355 return SCROLLING_FAILED;
12356
12357 /* Move the window start down. If scrolling conservatively,
12358 move it just enough down to make point visible. If
12359 scroll_step is set, move it down by scroll_step. */
12360 start_display (&it, w, startp);
12361
12362 if (scroll_conservatively)
12363 /* Set AMOUNT_TO_SCROLL to at least one line,
12364 and at most scroll_conservatively lines. */
12365 amount_to_scroll
12366 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12367 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12368 else if (scroll_step || temp_scroll_step)
12369 amount_to_scroll = scroll_max;
12370 else
12371 {
12372 aggressive = current_buffer->scroll_up_aggressively;
12373 height = WINDOW_BOX_TEXT_HEIGHT (w);
12374 if (NUMBERP (aggressive))
12375 {
12376 double float_amount = XFLOATINT (aggressive) * height;
12377 amount_to_scroll = float_amount;
12378 if (amount_to_scroll == 0 && float_amount > 0)
12379 amount_to_scroll = 1;
12380 }
12381 }
12382
12383 if (amount_to_scroll <= 0)
12384 return SCROLLING_FAILED;
12385
12386 /* If moving by amount_to_scroll leaves STARTP unchanged,
12387 move it down one screen line. */
12388
12389 move_it_vertically (&it, amount_to_scroll);
12390 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12391 move_it_by_lines (&it, 1, 1);
12392 startp = it.current.pos;
12393 }
12394 else
12395 {
12396 /* See if point is inside the scroll margin at the top of the
12397 window. */
12398 scroll_margin_pos = startp;
12399 if (this_scroll_margin)
12400 {
12401 start_display (&it, w, startp);
12402 move_it_vertically (&it, this_scroll_margin);
12403 scroll_margin_pos = it.current.pos;
12404 }
12405
12406 if (PT < CHARPOS (scroll_margin_pos))
12407 {
12408 /* Point is in the scroll margin at the top of the window or
12409 above what is displayed in the window. */
12410 int y0;
12411
12412 /* Compute the vertical distance from PT to the scroll
12413 margin position. Give up if distance is greater than
12414 scroll_max. */
12415 SET_TEXT_POS (pos, PT, PT_BYTE);
12416 start_display (&it, w, pos);
12417 y0 = it.current_y;
12418 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12419 it.last_visible_y, -1,
12420 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12421 dy = it.current_y - y0;
12422 if (dy > scroll_max)
12423 return SCROLLING_FAILED;
12424
12425 /* Compute new window start. */
12426 start_display (&it, w, startp);
12427
12428 if (scroll_conservatively)
12429 amount_to_scroll
12430 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12431 else if (scroll_step || temp_scroll_step)
12432 amount_to_scroll = scroll_max;
12433 else
12434 {
12435 aggressive = current_buffer->scroll_down_aggressively;
12436 height = WINDOW_BOX_TEXT_HEIGHT (w);
12437 if (NUMBERP (aggressive))
12438 {
12439 double float_amount = XFLOATINT (aggressive) * height;
12440 amount_to_scroll = float_amount;
12441 if (amount_to_scroll == 0 && float_amount > 0)
12442 amount_to_scroll = 1;
12443 }
12444 }
12445
12446 if (amount_to_scroll <= 0)
12447 return SCROLLING_FAILED;
12448
12449 move_it_vertically_backward (&it, amount_to_scroll);
12450 startp = it.current.pos;
12451 }
12452 }
12453
12454 /* Run window scroll functions. */
12455 startp = run_window_scroll_functions (window, startp);
12456
12457 /* Display the window. Give up if new fonts are loaded, or if point
12458 doesn't appear. */
12459 if (!try_window (window, startp, 0))
12460 rc = SCROLLING_NEED_LARGER_MATRICES;
12461 else if (w->cursor.vpos < 0)
12462 {
12463 clear_glyph_matrix (w->desired_matrix);
12464 rc = SCROLLING_FAILED;
12465 }
12466 else
12467 {
12468 /* Maybe forget recorded base line for line number display. */
12469 if (!just_this_one_p
12470 || current_buffer->clip_changed
12471 || BEG_UNCHANGED < CHARPOS (startp))
12472 w->base_line_number = Qnil;
12473
12474 /* If cursor ends up on a partially visible line,
12475 treat that as being off the bottom of the screen. */
12476 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12477 {
12478 clear_glyph_matrix (w->desired_matrix);
12479 ++extra_scroll_margin_lines;
12480 goto too_near_end;
12481 }
12482 rc = SCROLLING_SUCCESS;
12483 }
12484
12485 return rc;
12486 }
12487
12488
12489 /* Compute a suitable window start for window W if display of W starts
12490 on a continuation line. Value is non-zero if a new window start
12491 was computed.
12492
12493 The new window start will be computed, based on W's width, starting
12494 from the start of the continued line. It is the start of the
12495 screen line with the minimum distance from the old start W->start. */
12496
12497 static int
12498 compute_window_start_on_continuation_line (w)
12499 struct window *w;
12500 {
12501 struct text_pos pos, start_pos;
12502 int window_start_changed_p = 0;
12503
12504 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12505
12506 /* If window start is on a continuation line... Window start may be
12507 < BEGV in case there's invisible text at the start of the
12508 buffer (M-x rmail, for example). */
12509 if (CHARPOS (start_pos) > BEGV
12510 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12511 {
12512 struct it it;
12513 struct glyph_row *row;
12514
12515 /* Handle the case that the window start is out of range. */
12516 if (CHARPOS (start_pos) < BEGV)
12517 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12518 else if (CHARPOS (start_pos) > ZV)
12519 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12520
12521 /* Find the start of the continued line. This should be fast
12522 because scan_buffer is fast (newline cache). */
12523 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12524 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12525 row, DEFAULT_FACE_ID);
12526 reseat_at_previous_visible_line_start (&it);
12527
12528 /* If the line start is "too far" away from the window start,
12529 say it takes too much time to compute a new window start. */
12530 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12531 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12532 {
12533 int min_distance, distance;
12534
12535 /* Move forward by display lines to find the new window
12536 start. If window width was enlarged, the new start can
12537 be expected to be > the old start. If window width was
12538 decreased, the new window start will be < the old start.
12539 So, we're looking for the display line start with the
12540 minimum distance from the old window start. */
12541 pos = it.current.pos;
12542 min_distance = INFINITY;
12543 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12544 distance < min_distance)
12545 {
12546 min_distance = distance;
12547 pos = it.current.pos;
12548 move_it_by_lines (&it, 1, 0);
12549 }
12550
12551 /* Set the window start there. */
12552 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12553 window_start_changed_p = 1;
12554 }
12555 }
12556
12557 return window_start_changed_p;
12558 }
12559
12560
12561 /* Try cursor movement in case text has not changed in window WINDOW,
12562 with window start STARTP. Value is
12563
12564 CURSOR_MOVEMENT_SUCCESS if successful
12565
12566 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12567
12568 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12569 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12570 we want to scroll as if scroll-step were set to 1. See the code.
12571
12572 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12573 which case we have to abort this redisplay, and adjust matrices
12574 first. */
12575
12576 enum
12577 {
12578 CURSOR_MOVEMENT_SUCCESS,
12579 CURSOR_MOVEMENT_CANNOT_BE_USED,
12580 CURSOR_MOVEMENT_MUST_SCROLL,
12581 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12582 };
12583
12584 static int
12585 try_cursor_movement (window, startp, scroll_step)
12586 Lisp_Object window;
12587 struct text_pos startp;
12588 int *scroll_step;
12589 {
12590 struct window *w = XWINDOW (window);
12591 struct frame *f = XFRAME (w->frame);
12592 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12593
12594 #if GLYPH_DEBUG
12595 if (inhibit_try_cursor_movement)
12596 return rc;
12597 #endif
12598
12599 /* Handle case where text has not changed, only point, and it has
12600 not moved off the frame. */
12601 if (/* Point may be in this window. */
12602 PT >= CHARPOS (startp)
12603 /* Selective display hasn't changed. */
12604 && !current_buffer->clip_changed
12605 /* Function force-mode-line-update is used to force a thorough
12606 redisplay. It sets either windows_or_buffers_changed or
12607 update_mode_lines. So don't take a shortcut here for these
12608 cases. */
12609 && !update_mode_lines
12610 && !windows_or_buffers_changed
12611 && !cursor_type_changed
12612 /* Can't use this case if highlighting a region. When a
12613 region exists, cursor movement has to do more than just
12614 set the cursor. */
12615 && !(!NILP (Vtransient_mark_mode)
12616 && !NILP (current_buffer->mark_active))
12617 && NILP (w->region_showing)
12618 && NILP (Vshow_trailing_whitespace)
12619 /* Right after splitting windows, last_point may be nil. */
12620 && INTEGERP (w->last_point)
12621 /* This code is not used for mini-buffer for the sake of the case
12622 of redisplaying to replace an echo area message; since in
12623 that case the mini-buffer contents per se are usually
12624 unchanged. This code is of no real use in the mini-buffer
12625 since the handling of this_line_start_pos, etc., in redisplay
12626 handles the same cases. */
12627 && !EQ (window, minibuf_window)
12628 /* When splitting windows or for new windows, it happens that
12629 redisplay is called with a nil window_end_vpos or one being
12630 larger than the window. This should really be fixed in
12631 window.c. I don't have this on my list, now, so we do
12632 approximately the same as the old redisplay code. --gerd. */
12633 && INTEGERP (w->window_end_vpos)
12634 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12635 && (FRAME_WINDOW_P (f)
12636 || !overlay_arrow_in_current_buffer_p ()))
12637 {
12638 int this_scroll_margin, top_scroll_margin;
12639 struct glyph_row *row = NULL;
12640
12641 #if GLYPH_DEBUG
12642 debug_method_add (w, "cursor movement");
12643 #endif
12644
12645 /* Scroll if point within this distance from the top or bottom
12646 of the window. This is a pixel value. */
12647 this_scroll_margin = max (0, scroll_margin);
12648 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12649 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12650
12651 top_scroll_margin = this_scroll_margin;
12652 if (WINDOW_WANTS_HEADER_LINE_P (w))
12653 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12654
12655 /* Start with the row the cursor was displayed during the last
12656 not paused redisplay. Give up if that row is not valid. */
12657 if (w->last_cursor.vpos < 0
12658 || w->last_cursor.vpos >= w->current_matrix->nrows)
12659 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12660 else
12661 {
12662 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12663 if (row->mode_line_p)
12664 ++row;
12665 if (!row->enabled_p)
12666 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12667 }
12668
12669 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12670 {
12671 int scroll_p = 0;
12672 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12673
12674 if (PT > XFASTINT (w->last_point))
12675 {
12676 /* Point has moved forward. */
12677 while (MATRIX_ROW_END_CHARPOS (row) < PT
12678 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12679 {
12680 xassert (row->enabled_p);
12681 ++row;
12682 }
12683
12684 /* The end position of a row equals the start position
12685 of the next row. If PT is there, we would rather
12686 display it in the next line. */
12687 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12688 && MATRIX_ROW_END_CHARPOS (row) == PT
12689 && !cursor_row_p (w, row))
12690 ++row;
12691
12692 /* If within the scroll margin, scroll. Note that
12693 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12694 the next line would be drawn, and that
12695 this_scroll_margin can be zero. */
12696 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12697 || PT > MATRIX_ROW_END_CHARPOS (row)
12698 /* Line is completely visible last line in window
12699 and PT is to be set in the next line. */
12700 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12701 && PT == MATRIX_ROW_END_CHARPOS (row)
12702 && !row->ends_at_zv_p
12703 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12704 scroll_p = 1;
12705 }
12706 else if (PT < XFASTINT (w->last_point))
12707 {
12708 /* Cursor has to be moved backward. Note that PT >=
12709 CHARPOS (startp) because of the outer if-statement. */
12710 while (!row->mode_line_p
12711 && (MATRIX_ROW_START_CHARPOS (row) > PT
12712 || (MATRIX_ROW_START_CHARPOS (row) == PT
12713 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12714 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12715 row > w->current_matrix->rows
12716 && (row-1)->ends_in_newline_from_string_p))))
12717 && (row->y > top_scroll_margin
12718 || CHARPOS (startp) == BEGV))
12719 {
12720 xassert (row->enabled_p);
12721 --row;
12722 }
12723
12724 /* Consider the following case: Window starts at BEGV,
12725 there is invisible, intangible text at BEGV, so that
12726 display starts at some point START > BEGV. It can
12727 happen that we are called with PT somewhere between
12728 BEGV and START. Try to handle that case. */
12729 if (row < w->current_matrix->rows
12730 || row->mode_line_p)
12731 {
12732 row = w->current_matrix->rows;
12733 if (row->mode_line_p)
12734 ++row;
12735 }
12736
12737 /* Due to newlines in overlay strings, we may have to
12738 skip forward over overlay strings. */
12739 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12740 && MATRIX_ROW_END_CHARPOS (row) == PT
12741 && !cursor_row_p (w, row))
12742 ++row;
12743
12744 /* If within the scroll margin, scroll. */
12745 if (row->y < top_scroll_margin
12746 && CHARPOS (startp) != BEGV)
12747 scroll_p = 1;
12748 }
12749 else
12750 {
12751 /* Cursor did not move. So don't scroll even if cursor line
12752 is partially visible, as it was so before. */
12753 rc = CURSOR_MOVEMENT_SUCCESS;
12754 }
12755
12756 if (PT < MATRIX_ROW_START_CHARPOS (row)
12757 || PT > MATRIX_ROW_END_CHARPOS (row))
12758 {
12759 /* if PT is not in the glyph row, give up. */
12760 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12761 }
12762 else if (rc != CURSOR_MOVEMENT_SUCCESS
12763 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12764 && make_cursor_line_fully_visible_p)
12765 {
12766 if (PT == MATRIX_ROW_END_CHARPOS (row)
12767 && !row->ends_at_zv_p
12768 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12769 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12770 else if (row->height > window_box_height (w))
12771 {
12772 /* If we end up in a partially visible line, let's
12773 make it fully visible, except when it's taller
12774 than the window, in which case we can't do much
12775 about it. */
12776 *scroll_step = 1;
12777 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12778 }
12779 else
12780 {
12781 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12782 if (!cursor_row_fully_visible_p (w, 0, 1))
12783 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12784 else
12785 rc = CURSOR_MOVEMENT_SUCCESS;
12786 }
12787 }
12788 else if (scroll_p)
12789 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12790 else
12791 {
12792 do
12793 {
12794 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12795 {
12796 rc = CURSOR_MOVEMENT_SUCCESS;
12797 break;
12798 }
12799 ++row;
12800 }
12801 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12802 && MATRIX_ROW_START_CHARPOS (row) == PT
12803 && cursor_row_p (w, row));
12804 }
12805 }
12806 }
12807
12808 return rc;
12809 }
12810
12811 void
12812 set_vertical_scroll_bar (w)
12813 struct window *w;
12814 {
12815 int start, end, whole;
12816
12817 /* Calculate the start and end positions for the current window.
12818 At some point, it would be nice to choose between scrollbars
12819 which reflect the whole buffer size, with special markers
12820 indicating narrowing, and scrollbars which reflect only the
12821 visible region.
12822
12823 Note that mini-buffers sometimes aren't displaying any text. */
12824 if (!MINI_WINDOW_P (w)
12825 || (w == XWINDOW (minibuf_window)
12826 && NILP (echo_area_buffer[0])))
12827 {
12828 struct buffer *buf = XBUFFER (w->buffer);
12829 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12830 start = marker_position (w->start) - BUF_BEGV (buf);
12831 /* I don't think this is guaranteed to be right. For the
12832 moment, we'll pretend it is. */
12833 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12834
12835 if (end < start)
12836 end = start;
12837 if (whole < (end - start))
12838 whole = end - start;
12839 }
12840 else
12841 start = end = whole = 0;
12842
12843 /* Indicate what this scroll bar ought to be displaying now. */
12844 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12845 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12846 (w, end - start, whole, start);
12847 }
12848
12849
12850 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12851 selected_window is redisplayed.
12852
12853 We can return without actually redisplaying the window if
12854 fonts_changed_p is nonzero. In that case, redisplay_internal will
12855 retry. */
12856
12857 static void
12858 redisplay_window (window, just_this_one_p)
12859 Lisp_Object window;
12860 int just_this_one_p;
12861 {
12862 struct window *w = XWINDOW (window);
12863 struct frame *f = XFRAME (w->frame);
12864 struct buffer *buffer = XBUFFER (w->buffer);
12865 struct buffer *old = current_buffer;
12866 struct text_pos lpoint, opoint, startp;
12867 int update_mode_line;
12868 int tem;
12869 struct it it;
12870 /* Record it now because it's overwritten. */
12871 int current_matrix_up_to_date_p = 0;
12872 int used_current_matrix_p = 0;
12873 /* This is less strict than current_matrix_up_to_date_p.
12874 It indictes that the buffer contents and narrowing are unchanged. */
12875 int buffer_unchanged_p = 0;
12876 int temp_scroll_step = 0;
12877 int count = SPECPDL_INDEX ();
12878 int rc;
12879 int centering_position = -1;
12880 int last_line_misfit = 0;
12881 int beg_unchanged, end_unchanged;
12882
12883 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12884 opoint = lpoint;
12885
12886 /* W must be a leaf window here. */
12887 xassert (!NILP (w->buffer));
12888 #if GLYPH_DEBUG
12889 *w->desired_matrix->method = 0;
12890 #endif
12891
12892 specbind (Qinhibit_point_motion_hooks, Qt);
12893
12894 reconsider_clip_changes (w, buffer);
12895
12896 /* Has the mode line to be updated? */
12897 update_mode_line = (!NILP (w->update_mode_line)
12898 || update_mode_lines
12899 || buffer->clip_changed
12900 || buffer->prevent_redisplay_optimizations_p);
12901
12902 if (MINI_WINDOW_P (w))
12903 {
12904 if (w == XWINDOW (echo_area_window)
12905 && !NILP (echo_area_buffer[0]))
12906 {
12907 if (update_mode_line)
12908 /* We may have to update a tty frame's menu bar or a
12909 tool-bar. Example `M-x C-h C-h C-g'. */
12910 goto finish_menu_bars;
12911 else
12912 /* We've already displayed the echo area glyphs in this window. */
12913 goto finish_scroll_bars;
12914 }
12915 else if ((w != XWINDOW (minibuf_window)
12916 || minibuf_level == 0)
12917 /* When buffer is nonempty, redisplay window normally. */
12918 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12919 /* Quail displays non-mini buffers in minibuffer window.
12920 In that case, redisplay the window normally. */
12921 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12922 {
12923 /* W is a mini-buffer window, but it's not active, so clear
12924 it. */
12925 int yb = window_text_bottom_y (w);
12926 struct glyph_row *row;
12927 int y;
12928
12929 for (y = 0, row = w->desired_matrix->rows;
12930 y < yb;
12931 y += row->height, ++row)
12932 blank_row (w, row, y);
12933 goto finish_scroll_bars;
12934 }
12935
12936 clear_glyph_matrix (w->desired_matrix);
12937 }
12938
12939 /* Otherwise set up data on this window; select its buffer and point
12940 value. */
12941 /* Really select the buffer, for the sake of buffer-local
12942 variables. */
12943 set_buffer_internal_1 (XBUFFER (w->buffer));
12944 SET_TEXT_POS (opoint, PT, PT_BYTE);
12945
12946 beg_unchanged = BEG_UNCHANGED;
12947 end_unchanged = END_UNCHANGED;
12948
12949 current_matrix_up_to_date_p
12950 = (!NILP (w->window_end_valid)
12951 && !current_buffer->clip_changed
12952 && !current_buffer->prevent_redisplay_optimizations_p
12953 && XFASTINT (w->last_modified) >= MODIFF
12954 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12955
12956 buffer_unchanged_p
12957 = (!NILP (w->window_end_valid)
12958 && !current_buffer->clip_changed
12959 && XFASTINT (w->last_modified) >= MODIFF
12960 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12961
12962 /* When windows_or_buffers_changed is non-zero, we can't rely on
12963 the window end being valid, so set it to nil there. */
12964 if (windows_or_buffers_changed)
12965 {
12966 /* If window starts on a continuation line, maybe adjust the
12967 window start in case the window's width changed. */
12968 if (XMARKER (w->start)->buffer == current_buffer)
12969 compute_window_start_on_continuation_line (w);
12970
12971 w->window_end_valid = Qnil;
12972 }
12973
12974 /* Some sanity checks. */
12975 CHECK_WINDOW_END (w);
12976 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12977 abort ();
12978 if (BYTEPOS (opoint) < CHARPOS (opoint))
12979 abort ();
12980
12981 /* If %c is in mode line, update it if needed. */
12982 if (!NILP (w->column_number_displayed)
12983 /* This alternative quickly identifies a common case
12984 where no change is needed. */
12985 && !(PT == XFASTINT (w->last_point)
12986 && XFASTINT (w->last_modified) >= MODIFF
12987 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12988 && (XFASTINT (w->column_number_displayed)
12989 != (int) current_column ())) /* iftc */
12990 update_mode_line = 1;
12991
12992 /* Count number of windows showing the selected buffer. An indirect
12993 buffer counts as its base buffer. */
12994 if (!just_this_one_p)
12995 {
12996 struct buffer *current_base, *window_base;
12997 current_base = current_buffer;
12998 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12999 if (current_base->base_buffer)
13000 current_base = current_base->base_buffer;
13001 if (window_base->base_buffer)
13002 window_base = window_base->base_buffer;
13003 if (current_base == window_base)
13004 buffer_shared++;
13005 }
13006
13007 /* Point refers normally to the selected window. For any other
13008 window, set up appropriate value. */
13009 if (!EQ (window, selected_window))
13010 {
13011 int new_pt = XMARKER (w->pointm)->charpos;
13012 int new_pt_byte = marker_byte_position (w->pointm);
13013 if (new_pt < BEGV)
13014 {
13015 new_pt = BEGV;
13016 new_pt_byte = BEGV_BYTE;
13017 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13018 }
13019 else if (new_pt > (ZV - 1))
13020 {
13021 new_pt = ZV;
13022 new_pt_byte = ZV_BYTE;
13023 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13024 }
13025
13026 /* We don't use SET_PT so that the point-motion hooks don't run. */
13027 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13028 }
13029
13030 /* If any of the character widths specified in the display table
13031 have changed, invalidate the width run cache. It's true that
13032 this may be a bit late to catch such changes, but the rest of
13033 redisplay goes (non-fatally) haywire when the display table is
13034 changed, so why should we worry about doing any better? */
13035 if (current_buffer->width_run_cache)
13036 {
13037 struct Lisp_Char_Table *disptab = buffer_display_table ();
13038
13039 if (! disptab_matches_widthtab (disptab,
13040 XVECTOR (current_buffer->width_table)))
13041 {
13042 invalidate_region_cache (current_buffer,
13043 current_buffer->width_run_cache,
13044 BEG, Z);
13045 recompute_width_table (current_buffer, disptab);
13046 }
13047 }
13048
13049 /* If window-start is screwed up, choose a new one. */
13050 if (XMARKER (w->start)->buffer != current_buffer)
13051 goto recenter;
13052
13053 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13054
13055 /* If someone specified a new starting point but did not insist,
13056 check whether it can be used. */
13057 if (!NILP (w->optional_new_start)
13058 && CHARPOS (startp) >= BEGV
13059 && CHARPOS (startp) <= ZV)
13060 {
13061 w->optional_new_start = Qnil;
13062 start_display (&it, w, startp);
13063 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13064 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13065 if (IT_CHARPOS (it) == PT)
13066 w->force_start = Qt;
13067 /* IT may overshoot PT if text at PT is invisible. */
13068 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13069 w->force_start = Qt;
13070 }
13071
13072 force_start:
13073
13074 /* Handle case where place to start displaying has been specified,
13075 unless the specified location is outside the accessible range. */
13076 if (!NILP (w->force_start)
13077 || w->frozen_window_start_p)
13078 {
13079 /* We set this later on if we have to adjust point. */
13080 int new_vpos = -1;
13081 int val;
13082
13083 w->force_start = Qnil;
13084 w->vscroll = 0;
13085 w->window_end_valid = Qnil;
13086
13087 /* Forget any recorded base line for line number display. */
13088 if (!buffer_unchanged_p)
13089 w->base_line_number = Qnil;
13090
13091 /* Redisplay the mode line. Select the buffer properly for that.
13092 Also, run the hook window-scroll-functions
13093 because we have scrolled. */
13094 /* Note, we do this after clearing force_start because
13095 if there's an error, it is better to forget about force_start
13096 than to get into an infinite loop calling the hook functions
13097 and having them get more errors. */
13098 if (!update_mode_line
13099 || ! NILP (Vwindow_scroll_functions))
13100 {
13101 update_mode_line = 1;
13102 w->update_mode_line = Qt;
13103 startp = run_window_scroll_functions (window, startp);
13104 }
13105
13106 w->last_modified = make_number (0);
13107 w->last_overlay_modified = make_number (0);
13108 if (CHARPOS (startp) < BEGV)
13109 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13110 else if (CHARPOS (startp) > ZV)
13111 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13112
13113 /* Redisplay, then check if cursor has been set during the
13114 redisplay. Give up if new fonts were loaded. */
13115 val = try_window (window, startp, 1);
13116 if (!val)
13117 {
13118 w->force_start = Qt;
13119 clear_glyph_matrix (w->desired_matrix);
13120 goto need_larger_matrices;
13121 }
13122 /* Point was outside the scroll margins. */
13123 if (val < 0)
13124 new_vpos = window_box_height (w) / 2;
13125
13126 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13127 {
13128 /* If point does not appear, try to move point so it does
13129 appear. The desired matrix has been built above, so we
13130 can use it here. */
13131 new_vpos = window_box_height (w) / 2;
13132 }
13133
13134 if (!cursor_row_fully_visible_p (w, 0, 0))
13135 {
13136 /* Point does appear, but on a line partly visible at end of window.
13137 Move it back to a fully-visible line. */
13138 new_vpos = window_box_height (w);
13139 }
13140
13141 /* If we need to move point for either of the above reasons,
13142 now actually do it. */
13143 if (new_vpos >= 0)
13144 {
13145 struct glyph_row *row;
13146
13147 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13148 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13149 ++row;
13150
13151 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13152 MATRIX_ROW_START_BYTEPOS (row));
13153
13154 if (w != XWINDOW (selected_window))
13155 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13156 else if (current_buffer == old)
13157 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13158
13159 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13160
13161 /* If we are highlighting the region, then we just changed
13162 the region, so redisplay to show it. */
13163 if (!NILP (Vtransient_mark_mode)
13164 && !NILP (current_buffer->mark_active))
13165 {
13166 clear_glyph_matrix (w->desired_matrix);
13167 if (!try_window (window, startp, 0))
13168 goto need_larger_matrices;
13169 }
13170 }
13171
13172 #if GLYPH_DEBUG
13173 debug_method_add (w, "forced window start");
13174 #endif
13175 goto done;
13176 }
13177
13178 /* Handle case where text has not changed, only point, and it has
13179 not moved off the frame, and we are not retrying after hscroll.
13180 (current_matrix_up_to_date_p is nonzero when retrying.) */
13181 if (current_matrix_up_to_date_p
13182 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13183 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13184 {
13185 switch (rc)
13186 {
13187 case CURSOR_MOVEMENT_SUCCESS:
13188 used_current_matrix_p = 1;
13189 goto done;
13190
13191 #if 0 /* try_cursor_movement never returns this value. */
13192 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13193 goto need_larger_matrices;
13194 #endif
13195
13196 case CURSOR_MOVEMENT_MUST_SCROLL:
13197 goto try_to_scroll;
13198
13199 default:
13200 abort ();
13201 }
13202 }
13203 /* If current starting point was originally the beginning of a line
13204 but no longer is, find a new starting point. */
13205 else if (!NILP (w->start_at_line_beg)
13206 && !(CHARPOS (startp) <= BEGV
13207 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13208 {
13209 #if GLYPH_DEBUG
13210 debug_method_add (w, "recenter 1");
13211 #endif
13212 goto recenter;
13213 }
13214
13215 /* Try scrolling with try_window_id. Value is > 0 if update has
13216 been done, it is -1 if we know that the same window start will
13217 not work. It is 0 if unsuccessful for some other reason. */
13218 else if ((tem = try_window_id (w)) != 0)
13219 {
13220 #if GLYPH_DEBUG
13221 debug_method_add (w, "try_window_id %d", tem);
13222 #endif
13223
13224 if (fonts_changed_p)
13225 goto need_larger_matrices;
13226 if (tem > 0)
13227 goto done;
13228
13229 /* Otherwise try_window_id has returned -1 which means that we
13230 don't want the alternative below this comment to execute. */
13231 }
13232 else if (CHARPOS (startp) >= BEGV
13233 && CHARPOS (startp) <= ZV
13234 && PT >= CHARPOS (startp)
13235 && (CHARPOS (startp) < ZV
13236 /* Avoid starting at end of buffer. */
13237 || CHARPOS (startp) == BEGV
13238 || (XFASTINT (w->last_modified) >= MODIFF
13239 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13240 {
13241
13242 /* If first window line is a continuation line, and window start
13243 is inside the modified region, but the first change is before
13244 current window start, we must select a new window start.
13245
13246 However, if this is the result of a down-mouse event (e.g. by
13247 extending the mouse-drag-overlay), we don't want to select a
13248 new window start, since that would change the position under
13249 the mouse, resulting in an unwanted mouse-movement rather
13250 than a simple mouse-click. */
13251 if (NILP (w->start_at_line_beg)
13252 && NILP (do_mouse_tracking)
13253 && CHARPOS (startp) > BEGV
13254 && CHARPOS (startp) > BEG + beg_unchanged
13255 && CHARPOS (startp) <= Z - end_unchanged)
13256 {
13257 w->force_start = Qt;
13258 if (XMARKER (w->start)->buffer == current_buffer)
13259 compute_window_start_on_continuation_line (w);
13260 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13261 goto force_start;
13262 }
13263
13264 #if GLYPH_DEBUG
13265 debug_method_add (w, "same window start");
13266 #endif
13267
13268 /* Try to redisplay starting at same place as before.
13269 If point has not moved off frame, accept the results. */
13270 if (!current_matrix_up_to_date_p
13271 /* Don't use try_window_reusing_current_matrix in this case
13272 because a window scroll function can have changed the
13273 buffer. */
13274 || !NILP (Vwindow_scroll_functions)
13275 || MINI_WINDOW_P (w)
13276 || !(used_current_matrix_p
13277 = try_window_reusing_current_matrix (w)))
13278 {
13279 IF_DEBUG (debug_method_add (w, "1"));
13280 if (try_window (window, startp, 1) < 0)
13281 /* -1 means we need to scroll.
13282 0 means we need new matrices, but fonts_changed_p
13283 is set in that case, so we will detect it below. */
13284 goto try_to_scroll;
13285 }
13286
13287 if (fonts_changed_p)
13288 goto need_larger_matrices;
13289
13290 if (w->cursor.vpos >= 0)
13291 {
13292 if (!just_this_one_p
13293 || current_buffer->clip_changed
13294 || BEG_UNCHANGED < CHARPOS (startp))
13295 /* Forget any recorded base line for line number display. */
13296 w->base_line_number = Qnil;
13297
13298 if (!cursor_row_fully_visible_p (w, 1, 0))
13299 {
13300 clear_glyph_matrix (w->desired_matrix);
13301 last_line_misfit = 1;
13302 }
13303 /* Drop through and scroll. */
13304 else
13305 goto done;
13306 }
13307 else
13308 clear_glyph_matrix (w->desired_matrix);
13309 }
13310
13311 try_to_scroll:
13312
13313 w->last_modified = make_number (0);
13314 w->last_overlay_modified = make_number (0);
13315
13316 /* Redisplay the mode line. Select the buffer properly for that. */
13317 if (!update_mode_line)
13318 {
13319 update_mode_line = 1;
13320 w->update_mode_line = Qt;
13321 }
13322
13323 /* Try to scroll by specified few lines. */
13324 if ((scroll_conservatively
13325 || scroll_step
13326 || temp_scroll_step
13327 || NUMBERP (current_buffer->scroll_up_aggressively)
13328 || NUMBERP (current_buffer->scroll_down_aggressively))
13329 && !current_buffer->clip_changed
13330 && CHARPOS (startp) >= BEGV
13331 && CHARPOS (startp) <= ZV)
13332 {
13333 /* The function returns -1 if new fonts were loaded, 1 if
13334 successful, 0 if not successful. */
13335 int rc = try_scrolling (window, just_this_one_p,
13336 scroll_conservatively,
13337 scroll_step,
13338 temp_scroll_step, last_line_misfit);
13339 switch (rc)
13340 {
13341 case SCROLLING_SUCCESS:
13342 goto done;
13343
13344 case SCROLLING_NEED_LARGER_MATRICES:
13345 goto need_larger_matrices;
13346
13347 case SCROLLING_FAILED:
13348 break;
13349
13350 default:
13351 abort ();
13352 }
13353 }
13354
13355 /* Finally, just choose place to start which centers point */
13356
13357 recenter:
13358 if (centering_position < 0)
13359 centering_position = window_box_height (w) / 2;
13360
13361 #if GLYPH_DEBUG
13362 debug_method_add (w, "recenter");
13363 #endif
13364
13365 /* w->vscroll = 0; */
13366
13367 /* Forget any previously recorded base line for line number display. */
13368 if (!buffer_unchanged_p)
13369 w->base_line_number = Qnil;
13370
13371 /* Move backward half the height of the window. */
13372 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13373 it.current_y = it.last_visible_y;
13374 move_it_vertically_backward (&it, centering_position);
13375 xassert (IT_CHARPOS (it) >= BEGV);
13376
13377 /* The function move_it_vertically_backward may move over more
13378 than the specified y-distance. If it->w is small, e.g. a
13379 mini-buffer window, we may end up in front of the window's
13380 display area. Start displaying at the start of the line
13381 containing PT in this case. */
13382 if (it.current_y <= 0)
13383 {
13384 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13385 move_it_vertically_backward (&it, 0);
13386 #if 0
13387 /* I think this assert is bogus if buffer contains
13388 invisible text or images. KFS. */
13389 xassert (IT_CHARPOS (it) <= PT);
13390 #endif
13391 it.current_y = 0;
13392 }
13393
13394 it.current_x = it.hpos = 0;
13395
13396 /* Set startp here explicitly in case that helps avoid an infinite loop
13397 in case the window-scroll-functions functions get errors. */
13398 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13399
13400 /* Run scroll hooks. */
13401 startp = run_window_scroll_functions (window, it.current.pos);
13402
13403 /* Redisplay the window. */
13404 if (!current_matrix_up_to_date_p
13405 || windows_or_buffers_changed
13406 || cursor_type_changed
13407 /* Don't use try_window_reusing_current_matrix in this case
13408 because it can have changed the buffer. */
13409 || !NILP (Vwindow_scroll_functions)
13410 || !just_this_one_p
13411 || MINI_WINDOW_P (w)
13412 || !(used_current_matrix_p
13413 = try_window_reusing_current_matrix (w)))
13414 try_window (window, startp, 0);
13415
13416 /* If new fonts have been loaded (due to fontsets), give up. We
13417 have to start a new redisplay since we need to re-adjust glyph
13418 matrices. */
13419 if (fonts_changed_p)
13420 goto need_larger_matrices;
13421
13422 /* If cursor did not appear assume that the middle of the window is
13423 in the first line of the window. Do it again with the next line.
13424 (Imagine a window of height 100, displaying two lines of height
13425 60. Moving back 50 from it->last_visible_y will end in the first
13426 line.) */
13427 if (w->cursor.vpos < 0)
13428 {
13429 if (!NILP (w->window_end_valid)
13430 && PT >= Z - XFASTINT (w->window_end_pos))
13431 {
13432 clear_glyph_matrix (w->desired_matrix);
13433 move_it_by_lines (&it, 1, 0);
13434 try_window (window, it.current.pos, 0);
13435 }
13436 else if (PT < IT_CHARPOS (it))
13437 {
13438 clear_glyph_matrix (w->desired_matrix);
13439 move_it_by_lines (&it, -1, 0);
13440 try_window (window, it.current.pos, 0);
13441 }
13442 else
13443 {
13444 /* Not much we can do about it. */
13445 }
13446 }
13447
13448 /* Consider the following case: Window starts at BEGV, there is
13449 invisible, intangible text at BEGV, so that display starts at
13450 some point START > BEGV. It can happen that we are called with
13451 PT somewhere between BEGV and START. Try to handle that case. */
13452 if (w->cursor.vpos < 0)
13453 {
13454 struct glyph_row *row = w->current_matrix->rows;
13455 if (row->mode_line_p)
13456 ++row;
13457 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13458 }
13459
13460 if (!cursor_row_fully_visible_p (w, 0, 0))
13461 {
13462 /* If vscroll is enabled, disable it and try again. */
13463 if (w->vscroll)
13464 {
13465 w->vscroll = 0;
13466 clear_glyph_matrix (w->desired_matrix);
13467 goto recenter;
13468 }
13469
13470 /* If centering point failed to make the whole line visible,
13471 put point at the top instead. That has to make the whole line
13472 visible, if it can be done. */
13473 if (centering_position == 0)
13474 goto done;
13475
13476 clear_glyph_matrix (w->desired_matrix);
13477 centering_position = 0;
13478 goto recenter;
13479 }
13480
13481 done:
13482
13483 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13484 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13485 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13486 ? Qt : Qnil);
13487
13488 /* Display the mode line, if we must. */
13489 if ((update_mode_line
13490 /* If window not full width, must redo its mode line
13491 if (a) the window to its side is being redone and
13492 (b) we do a frame-based redisplay. This is a consequence
13493 of how inverted lines are drawn in frame-based redisplay. */
13494 || (!just_this_one_p
13495 && !FRAME_WINDOW_P (f)
13496 && !WINDOW_FULL_WIDTH_P (w))
13497 /* Line number to display. */
13498 || INTEGERP (w->base_line_pos)
13499 /* Column number is displayed and different from the one displayed. */
13500 || (!NILP (w->column_number_displayed)
13501 && (XFASTINT (w->column_number_displayed)
13502 != (int) current_column ()))) /* iftc */
13503 /* This means that the window has a mode line. */
13504 && (WINDOW_WANTS_MODELINE_P (w)
13505 || WINDOW_WANTS_HEADER_LINE_P (w)))
13506 {
13507 display_mode_lines (w);
13508
13509 /* If mode line height has changed, arrange for a thorough
13510 immediate redisplay using the correct mode line height. */
13511 if (WINDOW_WANTS_MODELINE_P (w)
13512 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13513 {
13514 fonts_changed_p = 1;
13515 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13516 = DESIRED_MODE_LINE_HEIGHT (w);
13517 }
13518
13519 /* If top line height has changed, arrange for a thorough
13520 immediate redisplay using the correct mode line height. */
13521 if (WINDOW_WANTS_HEADER_LINE_P (w)
13522 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13523 {
13524 fonts_changed_p = 1;
13525 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13526 = DESIRED_HEADER_LINE_HEIGHT (w);
13527 }
13528
13529 if (fonts_changed_p)
13530 goto need_larger_matrices;
13531 }
13532
13533 if (!line_number_displayed
13534 && !BUFFERP (w->base_line_pos))
13535 {
13536 w->base_line_pos = Qnil;
13537 w->base_line_number = Qnil;
13538 }
13539
13540 finish_menu_bars:
13541
13542 /* When we reach a frame's selected window, redo the frame's menu bar. */
13543 if (update_mode_line
13544 && EQ (FRAME_SELECTED_WINDOW (f), window))
13545 {
13546 int redisplay_menu_p = 0;
13547 int redisplay_tool_bar_p = 0;
13548
13549 if (FRAME_WINDOW_P (f))
13550 {
13551 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13552 || defined (USE_GTK)
13553 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13554 #else
13555 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13556 #endif
13557 }
13558 else
13559 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13560
13561 if (redisplay_menu_p)
13562 display_menu_bar (w);
13563
13564 #ifdef HAVE_WINDOW_SYSTEM
13565 if (FRAME_WINDOW_P (f))
13566 {
13567 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13568 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13569 #else
13570 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13571 && (FRAME_TOOL_BAR_LINES (f) > 0
13572 || !NILP (Vauto_resize_tool_bars));
13573 #endif
13574
13575 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13576 {
13577 extern int ignore_mouse_drag_p;
13578 ignore_mouse_drag_p = 1;
13579 }
13580 }
13581 #endif
13582 }
13583
13584 #ifdef HAVE_WINDOW_SYSTEM
13585 if (FRAME_WINDOW_P (f)
13586 && update_window_fringes (w, (just_this_one_p
13587 || (!used_current_matrix_p && !overlay_arrow_seen)
13588 || w->pseudo_window_p)))
13589 {
13590 update_begin (f);
13591 BLOCK_INPUT;
13592 if (draw_window_fringes (w, 1))
13593 x_draw_vertical_border (w);
13594 UNBLOCK_INPUT;
13595 update_end (f);
13596 }
13597 #endif /* HAVE_WINDOW_SYSTEM */
13598
13599 /* We go to this label, with fonts_changed_p nonzero,
13600 if it is necessary to try again using larger glyph matrices.
13601 We have to redeem the scroll bar even in this case,
13602 because the loop in redisplay_internal expects that. */
13603 need_larger_matrices:
13604 ;
13605 finish_scroll_bars:
13606
13607 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13608 {
13609 /* Set the thumb's position and size. */
13610 set_vertical_scroll_bar (w);
13611
13612 /* Note that we actually used the scroll bar attached to this
13613 window, so it shouldn't be deleted at the end of redisplay. */
13614 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13615 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13616 }
13617
13618 /* Restore current_buffer and value of point in it. */
13619 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13620 set_buffer_internal_1 (old);
13621 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13622 shorter. This can be caused by log truncation in *Messages*. */
13623 if (CHARPOS (lpoint) <= ZV)
13624 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13625
13626 unbind_to (count, Qnil);
13627 }
13628
13629
13630 /* Build the complete desired matrix of WINDOW with a window start
13631 buffer position POS.
13632
13633 Value is 1 if successful. It is zero if fonts were loaded during
13634 redisplay which makes re-adjusting glyph matrices necessary, and -1
13635 if point would appear in the scroll margins.
13636 (We check that only if CHECK_MARGINS is nonzero. */
13637
13638 int
13639 try_window (window, pos, check_margins)
13640 Lisp_Object window;
13641 struct text_pos pos;
13642 int check_margins;
13643 {
13644 struct window *w = XWINDOW (window);
13645 struct it it;
13646 struct glyph_row *last_text_row = NULL;
13647 struct frame *f = XFRAME (w->frame);
13648
13649 /* Make POS the new window start. */
13650 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13651
13652 /* Mark cursor position as unknown. No overlay arrow seen. */
13653 w->cursor.vpos = -1;
13654 overlay_arrow_seen = 0;
13655
13656 /* Initialize iterator and info to start at POS. */
13657 start_display (&it, w, pos);
13658
13659 /* Display all lines of W. */
13660 while (it.current_y < it.last_visible_y)
13661 {
13662 if (display_line (&it))
13663 last_text_row = it.glyph_row - 1;
13664 if (fonts_changed_p)
13665 return 0;
13666 }
13667
13668 /* Don't let the cursor end in the scroll margins. */
13669 if (check_margins
13670 && !MINI_WINDOW_P (w))
13671 {
13672 int this_scroll_margin;
13673
13674 this_scroll_margin = max (0, scroll_margin);
13675 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13676 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13677
13678 if ((w->cursor.y >= 0 /* not vscrolled */
13679 && w->cursor.y < this_scroll_margin
13680 && CHARPOS (pos) > BEGV
13681 && IT_CHARPOS (it) < ZV)
13682 /* rms: considering make_cursor_line_fully_visible_p here
13683 seems to give wrong results. We don't want to recenter
13684 when the last line is partly visible, we want to allow
13685 that case to be handled in the usual way. */
13686 || (w->cursor.y + 1) > it.last_visible_y)
13687 {
13688 w->cursor.vpos = -1;
13689 clear_glyph_matrix (w->desired_matrix);
13690 return -1;
13691 }
13692 }
13693
13694 /* If bottom moved off end of frame, change mode line percentage. */
13695 if (XFASTINT (w->window_end_pos) <= 0
13696 && Z != IT_CHARPOS (it))
13697 w->update_mode_line = Qt;
13698
13699 /* Set window_end_pos to the offset of the last character displayed
13700 on the window from the end of current_buffer. Set
13701 window_end_vpos to its row number. */
13702 if (last_text_row)
13703 {
13704 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13705 w->window_end_bytepos
13706 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13707 w->window_end_pos
13708 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13709 w->window_end_vpos
13710 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13711 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13712 ->displays_text_p);
13713 }
13714 else
13715 {
13716 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13717 w->window_end_pos = make_number (Z - ZV);
13718 w->window_end_vpos = make_number (0);
13719 }
13720
13721 /* But that is not valid info until redisplay finishes. */
13722 w->window_end_valid = Qnil;
13723 return 1;
13724 }
13725
13726
13727 \f
13728 /************************************************************************
13729 Window redisplay reusing current matrix when buffer has not changed
13730 ************************************************************************/
13731
13732 /* Try redisplay of window W showing an unchanged buffer with a
13733 different window start than the last time it was displayed by
13734 reusing its current matrix. Value is non-zero if successful.
13735 W->start is the new window start. */
13736
13737 static int
13738 try_window_reusing_current_matrix (w)
13739 struct window *w;
13740 {
13741 struct frame *f = XFRAME (w->frame);
13742 struct glyph_row *row, *bottom_row;
13743 struct it it;
13744 struct run run;
13745 struct text_pos start, new_start;
13746 int nrows_scrolled, i;
13747 struct glyph_row *last_text_row;
13748 struct glyph_row *last_reused_text_row;
13749 struct glyph_row *start_row;
13750 int start_vpos, min_y, max_y;
13751
13752 #if GLYPH_DEBUG
13753 if (inhibit_try_window_reusing)
13754 return 0;
13755 #endif
13756
13757 if (/* This function doesn't handle terminal frames. */
13758 !FRAME_WINDOW_P (f)
13759 /* Don't try to reuse the display if windows have been split
13760 or such. */
13761 || windows_or_buffers_changed
13762 || cursor_type_changed)
13763 return 0;
13764
13765 /* Can't do this if region may have changed. */
13766 if ((!NILP (Vtransient_mark_mode)
13767 && !NILP (current_buffer->mark_active))
13768 || !NILP (w->region_showing)
13769 || !NILP (Vshow_trailing_whitespace))
13770 return 0;
13771
13772 /* If top-line visibility has changed, give up. */
13773 if (WINDOW_WANTS_HEADER_LINE_P (w)
13774 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13775 return 0;
13776
13777 /* Give up if old or new display is scrolled vertically. We could
13778 make this function handle this, but right now it doesn't. */
13779 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13780 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13781 return 0;
13782
13783 /* The variable new_start now holds the new window start. The old
13784 start `start' can be determined from the current matrix. */
13785 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13786 start = start_row->start.pos;
13787 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13788
13789 /* Clear the desired matrix for the display below. */
13790 clear_glyph_matrix (w->desired_matrix);
13791
13792 if (CHARPOS (new_start) <= CHARPOS (start))
13793 {
13794 int first_row_y;
13795
13796 /* Don't use this method if the display starts with an ellipsis
13797 displayed for invisible text. It's not easy to handle that case
13798 below, and it's certainly not worth the effort since this is
13799 not a frequent case. */
13800 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13801 return 0;
13802
13803 IF_DEBUG (debug_method_add (w, "twu1"));
13804
13805 /* Display up to a row that can be reused. The variable
13806 last_text_row is set to the last row displayed that displays
13807 text. Note that it.vpos == 0 if or if not there is a
13808 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13809 start_display (&it, w, new_start);
13810 first_row_y = it.current_y;
13811 w->cursor.vpos = -1;
13812 last_text_row = last_reused_text_row = NULL;
13813
13814 while (it.current_y < it.last_visible_y
13815 && !fonts_changed_p)
13816 {
13817 /* If we have reached into the characters in the START row,
13818 that means the line boundaries have changed. So we
13819 can't start copying with the row START. Maybe it will
13820 work to start copying with the following row. */
13821 while (IT_CHARPOS (it) > CHARPOS (start))
13822 {
13823 /* Advance to the next row as the "start". */
13824 start_row++;
13825 start = start_row->start.pos;
13826 /* If there are no more rows to try, or just one, give up. */
13827 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13828 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13829 || CHARPOS (start) == ZV)
13830 {
13831 clear_glyph_matrix (w->desired_matrix);
13832 return 0;
13833 }
13834
13835 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13836 }
13837 /* If we have reached alignment,
13838 we can copy the rest of the rows. */
13839 if (IT_CHARPOS (it) == CHARPOS (start))
13840 break;
13841
13842 if (display_line (&it))
13843 last_text_row = it.glyph_row - 1;
13844 }
13845
13846 /* A value of current_y < last_visible_y means that we stopped
13847 at the previous window start, which in turn means that we
13848 have at least one reusable row. */
13849 if (it.current_y < it.last_visible_y)
13850 {
13851 /* IT.vpos always starts from 0; it counts text lines. */
13852 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13853
13854 /* Find PT if not already found in the lines displayed. */
13855 if (w->cursor.vpos < 0)
13856 {
13857 int dy = it.current_y - start_row->y;
13858
13859 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13860 row = row_containing_pos (w, PT, row, NULL, dy);
13861 if (row)
13862 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13863 dy, nrows_scrolled);
13864 else
13865 {
13866 clear_glyph_matrix (w->desired_matrix);
13867 return 0;
13868 }
13869 }
13870
13871 /* Scroll the display. Do it before the current matrix is
13872 changed. The problem here is that update has not yet
13873 run, i.e. part of the current matrix is not up to date.
13874 scroll_run_hook will clear the cursor, and use the
13875 current matrix to get the height of the row the cursor is
13876 in. */
13877 run.current_y = start_row->y;
13878 run.desired_y = it.current_y;
13879 run.height = it.last_visible_y - it.current_y;
13880
13881 if (run.height > 0 && run.current_y != run.desired_y)
13882 {
13883 update_begin (f);
13884 FRAME_RIF (f)->update_window_begin_hook (w);
13885 FRAME_RIF (f)->clear_window_mouse_face (w);
13886 FRAME_RIF (f)->scroll_run_hook (w, &run);
13887 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13888 update_end (f);
13889 }
13890
13891 /* Shift current matrix down by nrows_scrolled lines. */
13892 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13893 rotate_matrix (w->current_matrix,
13894 start_vpos,
13895 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13896 nrows_scrolled);
13897
13898 /* Disable lines that must be updated. */
13899 for (i = 0; i < nrows_scrolled; ++i)
13900 (start_row + i)->enabled_p = 0;
13901
13902 /* Re-compute Y positions. */
13903 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13904 max_y = it.last_visible_y;
13905 for (row = start_row + nrows_scrolled;
13906 row < bottom_row;
13907 ++row)
13908 {
13909 row->y = it.current_y;
13910 row->visible_height = row->height;
13911
13912 if (row->y < min_y)
13913 row->visible_height -= min_y - row->y;
13914 if (row->y + row->height > max_y)
13915 row->visible_height -= row->y + row->height - max_y;
13916 row->redraw_fringe_bitmaps_p = 1;
13917
13918 it.current_y += row->height;
13919
13920 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13921 last_reused_text_row = row;
13922 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13923 break;
13924 }
13925
13926 /* Disable lines in the current matrix which are now
13927 below the window. */
13928 for (++row; row < bottom_row; ++row)
13929 row->enabled_p = row->mode_line_p = 0;
13930 }
13931
13932 /* Update window_end_pos etc.; last_reused_text_row is the last
13933 reused row from the current matrix containing text, if any.
13934 The value of last_text_row is the last displayed line
13935 containing text. */
13936 if (last_reused_text_row)
13937 {
13938 w->window_end_bytepos
13939 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13940 w->window_end_pos
13941 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13942 w->window_end_vpos
13943 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13944 w->current_matrix));
13945 }
13946 else if (last_text_row)
13947 {
13948 w->window_end_bytepos
13949 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13950 w->window_end_pos
13951 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13952 w->window_end_vpos
13953 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13954 }
13955 else
13956 {
13957 /* This window must be completely empty. */
13958 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13959 w->window_end_pos = make_number (Z - ZV);
13960 w->window_end_vpos = make_number (0);
13961 }
13962 w->window_end_valid = Qnil;
13963
13964 /* Update hint: don't try scrolling again in update_window. */
13965 w->desired_matrix->no_scrolling_p = 1;
13966
13967 #if GLYPH_DEBUG
13968 debug_method_add (w, "try_window_reusing_current_matrix 1");
13969 #endif
13970 return 1;
13971 }
13972 else if (CHARPOS (new_start) > CHARPOS (start))
13973 {
13974 struct glyph_row *pt_row, *row;
13975 struct glyph_row *first_reusable_row;
13976 struct glyph_row *first_row_to_display;
13977 int dy;
13978 int yb = window_text_bottom_y (w);
13979
13980 /* Find the row starting at new_start, if there is one. Don't
13981 reuse a partially visible line at the end. */
13982 first_reusable_row = start_row;
13983 while (first_reusable_row->enabled_p
13984 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13985 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13986 < CHARPOS (new_start)))
13987 ++first_reusable_row;
13988
13989 /* Give up if there is no row to reuse. */
13990 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13991 || !first_reusable_row->enabled_p
13992 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13993 != CHARPOS (new_start)))
13994 return 0;
13995
13996 /* We can reuse fully visible rows beginning with
13997 first_reusable_row to the end of the window. Set
13998 first_row_to_display to the first row that cannot be reused.
13999 Set pt_row to the row containing point, if there is any. */
14000 pt_row = NULL;
14001 for (first_row_to_display = first_reusable_row;
14002 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14003 ++first_row_to_display)
14004 {
14005 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14006 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14007 pt_row = first_row_to_display;
14008 }
14009
14010 /* Start displaying at the start of first_row_to_display. */
14011 xassert (first_row_to_display->y < yb);
14012 init_to_row_start (&it, w, first_row_to_display);
14013
14014 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14015 - start_vpos);
14016 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14017 - nrows_scrolled);
14018 it.current_y = (first_row_to_display->y - first_reusable_row->y
14019 + WINDOW_HEADER_LINE_HEIGHT (w));
14020
14021 /* Display lines beginning with first_row_to_display in the
14022 desired matrix. Set last_text_row to the last row displayed
14023 that displays text. */
14024 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14025 if (pt_row == NULL)
14026 w->cursor.vpos = -1;
14027 last_text_row = NULL;
14028 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14029 if (display_line (&it))
14030 last_text_row = it.glyph_row - 1;
14031
14032 /* Give up If point isn't in a row displayed or reused. */
14033 if (w->cursor.vpos < 0)
14034 {
14035 clear_glyph_matrix (w->desired_matrix);
14036 return 0;
14037 }
14038
14039 /* If point is in a reused row, adjust y and vpos of the cursor
14040 position. */
14041 if (pt_row)
14042 {
14043 w->cursor.vpos -= nrows_scrolled;
14044 w->cursor.y -= first_reusable_row->y - start_row->y;
14045 }
14046
14047 /* Scroll the display. */
14048 run.current_y = first_reusable_row->y;
14049 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14050 run.height = it.last_visible_y - run.current_y;
14051 dy = run.current_y - run.desired_y;
14052
14053 if (run.height)
14054 {
14055 update_begin (f);
14056 FRAME_RIF (f)->update_window_begin_hook (w);
14057 FRAME_RIF (f)->clear_window_mouse_face (w);
14058 FRAME_RIF (f)->scroll_run_hook (w, &run);
14059 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14060 update_end (f);
14061 }
14062
14063 /* Adjust Y positions of reused rows. */
14064 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14065 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14066 max_y = it.last_visible_y;
14067 for (row = first_reusable_row; row < first_row_to_display; ++row)
14068 {
14069 row->y -= dy;
14070 row->visible_height = row->height;
14071 if (row->y < min_y)
14072 row->visible_height -= min_y - row->y;
14073 if (row->y + row->height > max_y)
14074 row->visible_height -= row->y + row->height - max_y;
14075 row->redraw_fringe_bitmaps_p = 1;
14076 }
14077
14078 /* Scroll the current matrix. */
14079 xassert (nrows_scrolled > 0);
14080 rotate_matrix (w->current_matrix,
14081 start_vpos,
14082 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14083 -nrows_scrolled);
14084
14085 /* Disable rows not reused. */
14086 for (row -= nrows_scrolled; row < bottom_row; ++row)
14087 row->enabled_p = 0;
14088
14089 /* Point may have moved to a different line, so we cannot assume that
14090 the previous cursor position is valid; locate the correct row. */
14091 if (pt_row)
14092 {
14093 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14094 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14095 row++)
14096 {
14097 w->cursor.vpos++;
14098 w->cursor.y = row->y;
14099 }
14100 if (row < bottom_row)
14101 {
14102 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14103 while (glyph->charpos < PT)
14104 {
14105 w->cursor.hpos++;
14106 w->cursor.x += glyph->pixel_width;
14107 glyph++;
14108 }
14109 }
14110 }
14111
14112 /* Adjust window end. A null value of last_text_row means that
14113 the window end is in reused rows which in turn means that
14114 only its vpos can have changed. */
14115 if (last_text_row)
14116 {
14117 w->window_end_bytepos
14118 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14119 w->window_end_pos
14120 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14121 w->window_end_vpos
14122 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14123 }
14124 else
14125 {
14126 w->window_end_vpos
14127 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14128 }
14129
14130 w->window_end_valid = Qnil;
14131 w->desired_matrix->no_scrolling_p = 1;
14132
14133 #if GLYPH_DEBUG
14134 debug_method_add (w, "try_window_reusing_current_matrix 2");
14135 #endif
14136 return 1;
14137 }
14138
14139 return 0;
14140 }
14141
14142
14143 \f
14144 /************************************************************************
14145 Window redisplay reusing current matrix when buffer has changed
14146 ************************************************************************/
14147
14148 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14149 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14150 int *, int *));
14151 static struct glyph_row *
14152 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14153 struct glyph_row *));
14154
14155
14156 /* Return the last row in MATRIX displaying text. If row START is
14157 non-null, start searching with that row. IT gives the dimensions
14158 of the display. Value is null if matrix is empty; otherwise it is
14159 a pointer to the row found. */
14160
14161 static struct glyph_row *
14162 find_last_row_displaying_text (matrix, it, start)
14163 struct glyph_matrix *matrix;
14164 struct it *it;
14165 struct glyph_row *start;
14166 {
14167 struct glyph_row *row, *row_found;
14168
14169 /* Set row_found to the last row in IT->w's current matrix
14170 displaying text. The loop looks funny but think of partially
14171 visible lines. */
14172 row_found = NULL;
14173 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14174 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14175 {
14176 xassert (row->enabled_p);
14177 row_found = row;
14178 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14179 break;
14180 ++row;
14181 }
14182
14183 return row_found;
14184 }
14185
14186
14187 /* Return the last row in the current matrix of W that is not affected
14188 by changes at the start of current_buffer that occurred since W's
14189 current matrix was built. Value is null if no such row exists.
14190
14191 BEG_UNCHANGED us the number of characters unchanged at the start of
14192 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14193 first changed character in current_buffer. Characters at positions <
14194 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14195 when the current matrix was built. */
14196
14197 static struct glyph_row *
14198 find_last_unchanged_at_beg_row (w)
14199 struct window *w;
14200 {
14201 int first_changed_pos = BEG + BEG_UNCHANGED;
14202 struct glyph_row *row;
14203 struct glyph_row *row_found = NULL;
14204 int yb = window_text_bottom_y (w);
14205
14206 /* Find the last row displaying unchanged text. */
14207 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14208 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14209 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14210 {
14211 if (/* If row ends before first_changed_pos, it is unchanged,
14212 except in some case. */
14213 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14214 /* When row ends in ZV and we write at ZV it is not
14215 unchanged. */
14216 && !row->ends_at_zv_p
14217 /* When first_changed_pos is the end of a continued line,
14218 row is not unchanged because it may be no longer
14219 continued. */
14220 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14221 && (row->continued_p
14222 || row->exact_window_width_line_p)))
14223 row_found = row;
14224
14225 /* Stop if last visible row. */
14226 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14227 break;
14228
14229 ++row;
14230 }
14231
14232 return row_found;
14233 }
14234
14235
14236 /* Find the first glyph row in the current matrix of W that is not
14237 affected by changes at the end of current_buffer since the
14238 time W's current matrix was built.
14239
14240 Return in *DELTA the number of chars by which buffer positions in
14241 unchanged text at the end of current_buffer must be adjusted.
14242
14243 Return in *DELTA_BYTES the corresponding number of bytes.
14244
14245 Value is null if no such row exists, i.e. all rows are affected by
14246 changes. */
14247
14248 static struct glyph_row *
14249 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14250 struct window *w;
14251 int *delta, *delta_bytes;
14252 {
14253 struct glyph_row *row;
14254 struct glyph_row *row_found = NULL;
14255
14256 *delta = *delta_bytes = 0;
14257
14258 /* Display must not have been paused, otherwise the current matrix
14259 is not up to date. */
14260 if (NILP (w->window_end_valid))
14261 abort ();
14262
14263 /* A value of window_end_pos >= END_UNCHANGED means that the window
14264 end is in the range of changed text. If so, there is no
14265 unchanged row at the end of W's current matrix. */
14266 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14267 return NULL;
14268
14269 /* Set row to the last row in W's current matrix displaying text. */
14270 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14271
14272 /* If matrix is entirely empty, no unchanged row exists. */
14273 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14274 {
14275 /* The value of row is the last glyph row in the matrix having a
14276 meaningful buffer position in it. The end position of row
14277 corresponds to window_end_pos. This allows us to translate
14278 buffer positions in the current matrix to current buffer
14279 positions for characters not in changed text. */
14280 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14281 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14282 int last_unchanged_pos, last_unchanged_pos_old;
14283 struct glyph_row *first_text_row
14284 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14285
14286 *delta = Z - Z_old;
14287 *delta_bytes = Z_BYTE - Z_BYTE_old;
14288
14289 /* Set last_unchanged_pos to the buffer position of the last
14290 character in the buffer that has not been changed. Z is the
14291 index + 1 of the last character in current_buffer, i.e. by
14292 subtracting END_UNCHANGED we get the index of the last
14293 unchanged character, and we have to add BEG to get its buffer
14294 position. */
14295 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14296 last_unchanged_pos_old = last_unchanged_pos - *delta;
14297
14298 /* Search backward from ROW for a row displaying a line that
14299 starts at a minimum position >= last_unchanged_pos_old. */
14300 for (; row > first_text_row; --row)
14301 {
14302 /* This used to abort, but it can happen.
14303 It is ok to just stop the search instead here. KFS. */
14304 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14305 break;
14306
14307 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14308 row_found = row;
14309 }
14310 }
14311
14312 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14313 abort ();
14314
14315 return row_found;
14316 }
14317
14318
14319 /* Make sure that glyph rows in the current matrix of window W
14320 reference the same glyph memory as corresponding rows in the
14321 frame's frame matrix. This function is called after scrolling W's
14322 current matrix on a terminal frame in try_window_id and
14323 try_window_reusing_current_matrix. */
14324
14325 static void
14326 sync_frame_with_window_matrix_rows (w)
14327 struct window *w;
14328 {
14329 struct frame *f = XFRAME (w->frame);
14330 struct glyph_row *window_row, *window_row_end, *frame_row;
14331
14332 /* Preconditions: W must be a leaf window and full-width. Its frame
14333 must have a frame matrix. */
14334 xassert (NILP (w->hchild) && NILP (w->vchild));
14335 xassert (WINDOW_FULL_WIDTH_P (w));
14336 xassert (!FRAME_WINDOW_P (f));
14337
14338 /* If W is a full-width window, glyph pointers in W's current matrix
14339 have, by definition, to be the same as glyph pointers in the
14340 corresponding frame matrix. Note that frame matrices have no
14341 marginal areas (see build_frame_matrix). */
14342 window_row = w->current_matrix->rows;
14343 window_row_end = window_row + w->current_matrix->nrows;
14344 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14345 while (window_row < window_row_end)
14346 {
14347 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14348 struct glyph *end = window_row->glyphs[LAST_AREA];
14349
14350 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14351 frame_row->glyphs[TEXT_AREA] = start;
14352 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14353 frame_row->glyphs[LAST_AREA] = end;
14354
14355 /* Disable frame rows whose corresponding window rows have
14356 been disabled in try_window_id. */
14357 if (!window_row->enabled_p)
14358 frame_row->enabled_p = 0;
14359
14360 ++window_row, ++frame_row;
14361 }
14362 }
14363
14364
14365 /* Find the glyph row in window W containing CHARPOS. Consider all
14366 rows between START and END (not inclusive). END null means search
14367 all rows to the end of the display area of W. Value is the row
14368 containing CHARPOS or null. */
14369
14370 struct glyph_row *
14371 row_containing_pos (w, charpos, start, end, dy)
14372 struct window *w;
14373 int charpos;
14374 struct glyph_row *start, *end;
14375 int dy;
14376 {
14377 struct glyph_row *row = start;
14378 int last_y;
14379
14380 /* If we happen to start on a header-line, skip that. */
14381 if (row->mode_line_p)
14382 ++row;
14383
14384 if ((end && row >= end) || !row->enabled_p)
14385 return NULL;
14386
14387 last_y = window_text_bottom_y (w) - dy;
14388
14389 while (1)
14390 {
14391 /* Give up if we have gone too far. */
14392 if (end && row >= end)
14393 return NULL;
14394 /* This formerly returned if they were equal.
14395 I think that both quantities are of a "last plus one" type;
14396 if so, when they are equal, the row is within the screen. -- rms. */
14397 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14398 return NULL;
14399
14400 /* If it is in this row, return this row. */
14401 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14402 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14403 /* The end position of a row equals the start
14404 position of the next row. If CHARPOS is there, we
14405 would rather display it in the next line, except
14406 when this line ends in ZV. */
14407 && !row->ends_at_zv_p
14408 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14409 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14410 return row;
14411 ++row;
14412 }
14413 }
14414
14415
14416 /* Try to redisplay window W by reusing its existing display. W's
14417 current matrix must be up to date when this function is called,
14418 i.e. window_end_valid must not be nil.
14419
14420 Value is
14421
14422 1 if display has been updated
14423 0 if otherwise unsuccessful
14424 -1 if redisplay with same window start is known not to succeed
14425
14426 The following steps are performed:
14427
14428 1. Find the last row in the current matrix of W that is not
14429 affected by changes at the start of current_buffer. If no such row
14430 is found, give up.
14431
14432 2. Find the first row in W's current matrix that is not affected by
14433 changes at the end of current_buffer. Maybe there is no such row.
14434
14435 3. Display lines beginning with the row + 1 found in step 1 to the
14436 row found in step 2 or, if step 2 didn't find a row, to the end of
14437 the window.
14438
14439 4. If cursor is not known to appear on the window, give up.
14440
14441 5. If display stopped at the row found in step 2, scroll the
14442 display and current matrix as needed.
14443
14444 6. Maybe display some lines at the end of W, if we must. This can
14445 happen under various circumstances, like a partially visible line
14446 becoming fully visible, or because newly displayed lines are displayed
14447 in smaller font sizes.
14448
14449 7. Update W's window end information. */
14450
14451 static int
14452 try_window_id (w)
14453 struct window *w;
14454 {
14455 struct frame *f = XFRAME (w->frame);
14456 struct glyph_matrix *current_matrix = w->current_matrix;
14457 struct glyph_matrix *desired_matrix = w->desired_matrix;
14458 struct glyph_row *last_unchanged_at_beg_row;
14459 struct glyph_row *first_unchanged_at_end_row;
14460 struct glyph_row *row;
14461 struct glyph_row *bottom_row;
14462 int bottom_vpos;
14463 struct it it;
14464 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14465 struct text_pos start_pos;
14466 struct run run;
14467 int first_unchanged_at_end_vpos = 0;
14468 struct glyph_row *last_text_row, *last_text_row_at_end;
14469 struct text_pos start;
14470 int first_changed_charpos, last_changed_charpos;
14471
14472 #if GLYPH_DEBUG
14473 if (inhibit_try_window_id)
14474 return 0;
14475 #endif
14476
14477 /* This is handy for debugging. */
14478 #if 0
14479 #define GIVE_UP(X) \
14480 do { \
14481 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14482 return 0; \
14483 } while (0)
14484 #else
14485 #define GIVE_UP(X) return 0
14486 #endif
14487
14488 SET_TEXT_POS_FROM_MARKER (start, w->start);
14489
14490 /* Don't use this for mini-windows because these can show
14491 messages and mini-buffers, and we don't handle that here. */
14492 if (MINI_WINDOW_P (w))
14493 GIVE_UP (1);
14494
14495 /* This flag is used to prevent redisplay optimizations. */
14496 if (windows_or_buffers_changed || cursor_type_changed)
14497 GIVE_UP (2);
14498
14499 /* Verify that narrowing has not changed.
14500 Also verify that we were not told to prevent redisplay optimizations.
14501 It would be nice to further
14502 reduce the number of cases where this prevents try_window_id. */
14503 if (current_buffer->clip_changed
14504 || current_buffer->prevent_redisplay_optimizations_p)
14505 GIVE_UP (3);
14506
14507 /* Window must either use window-based redisplay or be full width. */
14508 if (!FRAME_WINDOW_P (f)
14509 && (!FRAME_LINE_INS_DEL_OK (f)
14510 || !WINDOW_FULL_WIDTH_P (w)))
14511 GIVE_UP (4);
14512
14513 /* Give up if point is not known NOT to appear in W. */
14514 if (PT < CHARPOS (start))
14515 GIVE_UP (5);
14516
14517 /* Another way to prevent redisplay optimizations. */
14518 if (XFASTINT (w->last_modified) == 0)
14519 GIVE_UP (6);
14520
14521 /* Verify that window is not hscrolled. */
14522 if (XFASTINT (w->hscroll) != 0)
14523 GIVE_UP (7);
14524
14525 /* Verify that display wasn't paused. */
14526 if (NILP (w->window_end_valid))
14527 GIVE_UP (8);
14528
14529 /* Can't use this if highlighting a region because a cursor movement
14530 will do more than just set the cursor. */
14531 if (!NILP (Vtransient_mark_mode)
14532 && !NILP (current_buffer->mark_active))
14533 GIVE_UP (9);
14534
14535 /* Likewise if highlighting trailing whitespace. */
14536 if (!NILP (Vshow_trailing_whitespace))
14537 GIVE_UP (11);
14538
14539 /* Likewise if showing a region. */
14540 if (!NILP (w->region_showing))
14541 GIVE_UP (10);
14542
14543 /* Can use this if overlay arrow position and or string have changed. */
14544 if (overlay_arrows_changed_p ())
14545 GIVE_UP (12);
14546
14547
14548 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14549 only if buffer has really changed. The reason is that the gap is
14550 initially at Z for freshly visited files. The code below would
14551 set end_unchanged to 0 in that case. */
14552 if (MODIFF > SAVE_MODIFF
14553 /* This seems to happen sometimes after saving a buffer. */
14554 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14555 {
14556 if (GPT - BEG < BEG_UNCHANGED)
14557 BEG_UNCHANGED = GPT - BEG;
14558 if (Z - GPT < END_UNCHANGED)
14559 END_UNCHANGED = Z - GPT;
14560 }
14561
14562 /* The position of the first and last character that has been changed. */
14563 first_changed_charpos = BEG + BEG_UNCHANGED;
14564 last_changed_charpos = Z - END_UNCHANGED;
14565
14566 /* If window starts after a line end, and the last change is in
14567 front of that newline, then changes don't affect the display.
14568 This case happens with stealth-fontification. Note that although
14569 the display is unchanged, glyph positions in the matrix have to
14570 be adjusted, of course. */
14571 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14572 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14573 && ((last_changed_charpos < CHARPOS (start)
14574 && CHARPOS (start) == BEGV)
14575 || (last_changed_charpos < CHARPOS (start) - 1
14576 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14577 {
14578 int Z_old, delta, Z_BYTE_old, delta_bytes;
14579 struct glyph_row *r0;
14580
14581 /* Compute how many chars/bytes have been added to or removed
14582 from the buffer. */
14583 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14584 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14585 delta = Z - Z_old;
14586 delta_bytes = Z_BYTE - Z_BYTE_old;
14587
14588 /* Give up if PT is not in the window. Note that it already has
14589 been checked at the start of try_window_id that PT is not in
14590 front of the window start. */
14591 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14592 GIVE_UP (13);
14593
14594 /* If window start is unchanged, we can reuse the whole matrix
14595 as is, after adjusting glyph positions. No need to compute
14596 the window end again, since its offset from Z hasn't changed. */
14597 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14598 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14599 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14600 /* PT must not be in a partially visible line. */
14601 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14602 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14603 {
14604 /* Adjust positions in the glyph matrix. */
14605 if (delta || delta_bytes)
14606 {
14607 struct glyph_row *r1
14608 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14609 increment_matrix_positions (w->current_matrix,
14610 MATRIX_ROW_VPOS (r0, current_matrix),
14611 MATRIX_ROW_VPOS (r1, current_matrix),
14612 delta, delta_bytes);
14613 }
14614
14615 /* Set the cursor. */
14616 row = row_containing_pos (w, PT, r0, NULL, 0);
14617 if (row)
14618 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14619 else
14620 abort ();
14621 return 1;
14622 }
14623 }
14624
14625 /* Handle the case that changes are all below what is displayed in
14626 the window, and that PT is in the window. This shortcut cannot
14627 be taken if ZV is visible in the window, and text has been added
14628 there that is visible in the window. */
14629 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14630 /* ZV is not visible in the window, or there are no
14631 changes at ZV, actually. */
14632 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14633 || first_changed_charpos == last_changed_charpos))
14634 {
14635 struct glyph_row *r0;
14636
14637 /* Give up if PT is not in the window. Note that it already has
14638 been checked at the start of try_window_id that PT is not in
14639 front of the window start. */
14640 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14641 GIVE_UP (14);
14642
14643 /* If window start is unchanged, we can reuse the whole matrix
14644 as is, without changing glyph positions since no text has
14645 been added/removed in front of the window end. */
14646 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14647 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14648 /* PT must not be in a partially visible line. */
14649 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14650 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14651 {
14652 /* We have to compute the window end anew since text
14653 can have been added/removed after it. */
14654 w->window_end_pos
14655 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14656 w->window_end_bytepos
14657 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14658
14659 /* Set the cursor. */
14660 row = row_containing_pos (w, PT, r0, NULL, 0);
14661 if (row)
14662 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14663 else
14664 abort ();
14665 return 2;
14666 }
14667 }
14668
14669 /* Give up if window start is in the changed area.
14670
14671 The condition used to read
14672
14673 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14674
14675 but why that was tested escapes me at the moment. */
14676 if (CHARPOS (start) >= first_changed_charpos
14677 && CHARPOS (start) <= last_changed_charpos)
14678 GIVE_UP (15);
14679
14680 /* Check that window start agrees with the start of the first glyph
14681 row in its current matrix. Check this after we know the window
14682 start is not in changed text, otherwise positions would not be
14683 comparable. */
14684 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14685 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14686 GIVE_UP (16);
14687
14688 /* Give up if the window ends in strings. Overlay strings
14689 at the end are difficult to handle, so don't try. */
14690 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14691 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14692 GIVE_UP (20);
14693
14694 /* Compute the position at which we have to start displaying new
14695 lines. Some of the lines at the top of the window might be
14696 reusable because they are not displaying changed text. Find the
14697 last row in W's current matrix not affected by changes at the
14698 start of current_buffer. Value is null if changes start in the
14699 first line of window. */
14700 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14701 if (last_unchanged_at_beg_row)
14702 {
14703 /* Avoid starting to display in the moddle of a character, a TAB
14704 for instance. This is easier than to set up the iterator
14705 exactly, and it's not a frequent case, so the additional
14706 effort wouldn't really pay off. */
14707 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14708 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14709 && last_unchanged_at_beg_row > w->current_matrix->rows)
14710 --last_unchanged_at_beg_row;
14711
14712 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14713 GIVE_UP (17);
14714
14715 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14716 GIVE_UP (18);
14717 start_pos = it.current.pos;
14718
14719 /* Start displaying new lines in the desired matrix at the same
14720 vpos we would use in the current matrix, i.e. below
14721 last_unchanged_at_beg_row. */
14722 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14723 current_matrix);
14724 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14725 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14726
14727 xassert (it.hpos == 0 && it.current_x == 0);
14728 }
14729 else
14730 {
14731 /* There are no reusable lines at the start of the window.
14732 Start displaying in the first text line. */
14733 start_display (&it, w, start);
14734 it.vpos = it.first_vpos;
14735 start_pos = it.current.pos;
14736 }
14737
14738 /* Find the first row that is not affected by changes at the end of
14739 the buffer. Value will be null if there is no unchanged row, in
14740 which case we must redisplay to the end of the window. delta
14741 will be set to the value by which buffer positions beginning with
14742 first_unchanged_at_end_row have to be adjusted due to text
14743 changes. */
14744 first_unchanged_at_end_row
14745 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14746 IF_DEBUG (debug_delta = delta);
14747 IF_DEBUG (debug_delta_bytes = delta_bytes);
14748
14749 /* Set stop_pos to the buffer position up to which we will have to
14750 display new lines. If first_unchanged_at_end_row != NULL, this
14751 is the buffer position of the start of the line displayed in that
14752 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14753 that we don't stop at a buffer position. */
14754 stop_pos = 0;
14755 if (first_unchanged_at_end_row)
14756 {
14757 xassert (last_unchanged_at_beg_row == NULL
14758 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14759
14760 /* If this is a continuation line, move forward to the next one
14761 that isn't. Changes in lines above affect this line.
14762 Caution: this may move first_unchanged_at_end_row to a row
14763 not displaying text. */
14764 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14765 && 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;
14769
14770 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14771 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14772 >= it.last_visible_y))
14773 first_unchanged_at_end_row = NULL;
14774 else
14775 {
14776 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14777 + delta);
14778 first_unchanged_at_end_vpos
14779 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14780 xassert (stop_pos >= Z - END_UNCHANGED);
14781 }
14782 }
14783 else if (last_unchanged_at_beg_row == NULL)
14784 GIVE_UP (19);
14785
14786
14787 #if GLYPH_DEBUG
14788
14789 /* Either there is no unchanged row at the end, or the one we have
14790 now displays text. This is a necessary condition for the window
14791 end pos calculation at the end of this function. */
14792 xassert (first_unchanged_at_end_row == NULL
14793 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14794
14795 debug_last_unchanged_at_beg_vpos
14796 = (last_unchanged_at_beg_row
14797 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14798 : -1);
14799 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14800
14801 #endif /* GLYPH_DEBUG != 0 */
14802
14803
14804 /* Display new lines. Set last_text_row to the last new line
14805 displayed which has text on it, i.e. might end up as being the
14806 line where the window_end_vpos is. */
14807 w->cursor.vpos = -1;
14808 last_text_row = NULL;
14809 overlay_arrow_seen = 0;
14810 while (it.current_y < it.last_visible_y
14811 && !fonts_changed_p
14812 && (first_unchanged_at_end_row == NULL
14813 || IT_CHARPOS (it) < stop_pos))
14814 {
14815 if (display_line (&it))
14816 last_text_row = it.glyph_row - 1;
14817 }
14818
14819 if (fonts_changed_p)
14820 return -1;
14821
14822
14823 /* Compute differences in buffer positions, y-positions etc. for
14824 lines reused at the bottom of the window. Compute what we can
14825 scroll. */
14826 if (first_unchanged_at_end_row
14827 /* No lines reused because we displayed everything up to the
14828 bottom of the window. */
14829 && it.current_y < it.last_visible_y)
14830 {
14831 dvpos = (it.vpos
14832 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14833 current_matrix));
14834 dy = it.current_y - first_unchanged_at_end_row->y;
14835 run.current_y = first_unchanged_at_end_row->y;
14836 run.desired_y = run.current_y + dy;
14837 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14838 }
14839 else
14840 {
14841 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14842 first_unchanged_at_end_row = NULL;
14843 }
14844 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14845
14846
14847 /* Find the cursor if not already found. We have to decide whether
14848 PT will appear on this window (it sometimes doesn't, but this is
14849 not a very frequent case.) This decision has to be made before
14850 the current matrix is altered. A value of cursor.vpos < 0 means
14851 that PT is either in one of the lines beginning at
14852 first_unchanged_at_end_row or below the window. Don't care for
14853 lines that might be displayed later at the window end; as
14854 mentioned, this is not a frequent case. */
14855 if (w->cursor.vpos < 0)
14856 {
14857 /* Cursor in unchanged rows at the top? */
14858 if (PT < CHARPOS (start_pos)
14859 && last_unchanged_at_beg_row)
14860 {
14861 row = row_containing_pos (w, PT,
14862 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14863 last_unchanged_at_beg_row + 1, 0);
14864 if (row)
14865 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14866 }
14867
14868 /* Start from first_unchanged_at_end_row looking for PT. */
14869 else if (first_unchanged_at_end_row)
14870 {
14871 row = row_containing_pos (w, PT - delta,
14872 first_unchanged_at_end_row, NULL, 0);
14873 if (row)
14874 set_cursor_from_row (w, row, w->current_matrix, delta,
14875 delta_bytes, dy, dvpos);
14876 }
14877
14878 /* Give up if cursor was not found. */
14879 if (w->cursor.vpos < 0)
14880 {
14881 clear_glyph_matrix (w->desired_matrix);
14882 return -1;
14883 }
14884 }
14885
14886 /* Don't let the cursor end in the scroll margins. */
14887 {
14888 int this_scroll_margin, cursor_height;
14889
14890 this_scroll_margin = max (0, scroll_margin);
14891 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14892 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14893 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14894
14895 if ((w->cursor.y < this_scroll_margin
14896 && CHARPOS (start) > BEGV)
14897 /* Old redisplay didn't take scroll margin into account at the bottom,
14898 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14899 || (w->cursor.y + (make_cursor_line_fully_visible_p
14900 ? cursor_height + this_scroll_margin
14901 : 1)) > it.last_visible_y)
14902 {
14903 w->cursor.vpos = -1;
14904 clear_glyph_matrix (w->desired_matrix);
14905 return -1;
14906 }
14907 }
14908
14909 /* Scroll the display. Do it before changing the current matrix so
14910 that xterm.c doesn't get confused about where the cursor glyph is
14911 found. */
14912 if (dy && run.height)
14913 {
14914 update_begin (f);
14915
14916 if (FRAME_WINDOW_P (f))
14917 {
14918 FRAME_RIF (f)->update_window_begin_hook (w);
14919 FRAME_RIF (f)->clear_window_mouse_face (w);
14920 FRAME_RIF (f)->scroll_run_hook (w, &run);
14921 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14922 }
14923 else
14924 {
14925 /* Terminal frame. In this case, dvpos gives the number of
14926 lines to scroll by; dvpos < 0 means scroll up. */
14927 int first_unchanged_at_end_vpos
14928 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14929 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14930 int end = (WINDOW_TOP_EDGE_LINE (w)
14931 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14932 + window_internal_height (w));
14933
14934 /* Perform the operation on the screen. */
14935 if (dvpos > 0)
14936 {
14937 /* Scroll last_unchanged_at_beg_row to the end of the
14938 window down dvpos lines. */
14939 set_terminal_window (f, end);
14940
14941 /* On dumb terminals delete dvpos lines at the end
14942 before inserting dvpos empty lines. */
14943 if (!FRAME_SCROLL_REGION_OK (f))
14944 ins_del_lines (f, end - dvpos, -dvpos);
14945
14946 /* Insert dvpos empty lines in front of
14947 last_unchanged_at_beg_row. */
14948 ins_del_lines (f, from, dvpos);
14949 }
14950 else if (dvpos < 0)
14951 {
14952 /* Scroll up last_unchanged_at_beg_vpos to the end of
14953 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14954 set_terminal_window (f, end);
14955
14956 /* Delete dvpos lines in front of
14957 last_unchanged_at_beg_vpos. ins_del_lines will set
14958 the cursor to the given vpos and emit |dvpos| delete
14959 line sequences. */
14960 ins_del_lines (f, from + dvpos, dvpos);
14961
14962 /* On a dumb terminal insert dvpos empty lines at the
14963 end. */
14964 if (!FRAME_SCROLL_REGION_OK (f))
14965 ins_del_lines (f, end + dvpos, -dvpos);
14966 }
14967
14968 set_terminal_window (f, 0);
14969 }
14970
14971 update_end (f);
14972 }
14973
14974 /* Shift reused rows of the current matrix to the right position.
14975 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14976 text. */
14977 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14978 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14979 if (dvpos < 0)
14980 {
14981 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14982 bottom_vpos, dvpos);
14983 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14984 bottom_vpos, 0);
14985 }
14986 else if (dvpos > 0)
14987 {
14988 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14989 bottom_vpos, dvpos);
14990 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14991 first_unchanged_at_end_vpos + dvpos, 0);
14992 }
14993
14994 /* For frame-based redisplay, make sure that current frame and window
14995 matrix are in sync with respect to glyph memory. */
14996 if (!FRAME_WINDOW_P (f))
14997 sync_frame_with_window_matrix_rows (w);
14998
14999 /* Adjust buffer positions in reused rows. */
15000 if (delta || delta_bytes)
15001 increment_matrix_positions (current_matrix,
15002 first_unchanged_at_end_vpos + dvpos,
15003 bottom_vpos, delta, delta_bytes);
15004
15005 /* Adjust Y positions. */
15006 if (dy)
15007 shift_glyph_matrix (w, current_matrix,
15008 first_unchanged_at_end_vpos + dvpos,
15009 bottom_vpos, dy);
15010
15011 if (first_unchanged_at_end_row)
15012 {
15013 first_unchanged_at_end_row += dvpos;
15014 if (first_unchanged_at_end_row->y >= it.last_visible_y
15015 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15016 first_unchanged_at_end_row = NULL;
15017 }
15018
15019 /* If scrolling up, there may be some lines to display at the end of
15020 the window. */
15021 last_text_row_at_end = NULL;
15022 if (dy < 0)
15023 {
15024 /* Scrolling up can leave for example a partially visible line
15025 at the end of the window to be redisplayed. */
15026 /* Set last_row to the glyph row in the current matrix where the
15027 window end line is found. It has been moved up or down in
15028 the matrix by dvpos. */
15029 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15030 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15031
15032 /* If last_row is the window end line, it should display text. */
15033 xassert (last_row->displays_text_p);
15034
15035 /* If window end line was partially visible before, begin
15036 displaying at that line. Otherwise begin displaying with the
15037 line following it. */
15038 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15039 {
15040 init_to_row_start (&it, w, last_row);
15041 it.vpos = last_vpos;
15042 it.current_y = last_row->y;
15043 }
15044 else
15045 {
15046 init_to_row_end (&it, w, last_row);
15047 it.vpos = 1 + last_vpos;
15048 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15049 ++last_row;
15050 }
15051
15052 /* We may start in a continuation line. If so, we have to
15053 get the right continuation_lines_width and current_x. */
15054 it.continuation_lines_width = last_row->continuation_lines_width;
15055 it.hpos = it.current_x = 0;
15056
15057 /* Display the rest of the lines at the window end. */
15058 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15059 while (it.current_y < it.last_visible_y
15060 && !fonts_changed_p)
15061 {
15062 /* Is it always sure that the display agrees with lines in
15063 the current matrix? I don't think so, so we mark rows
15064 displayed invalid in the current matrix by setting their
15065 enabled_p flag to zero. */
15066 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15067 if (display_line (&it))
15068 last_text_row_at_end = it.glyph_row - 1;
15069 }
15070 }
15071
15072 /* Update window_end_pos and window_end_vpos. */
15073 if (first_unchanged_at_end_row
15074 && !last_text_row_at_end)
15075 {
15076 /* Window end line if one of the preserved rows from the current
15077 matrix. Set row to the last row displaying text in current
15078 matrix starting at first_unchanged_at_end_row, after
15079 scrolling. */
15080 xassert (first_unchanged_at_end_row->displays_text_p);
15081 row = find_last_row_displaying_text (w->current_matrix, &it,
15082 first_unchanged_at_end_row);
15083 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15084
15085 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15086 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15087 w->window_end_vpos
15088 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15089 xassert (w->window_end_bytepos >= 0);
15090 IF_DEBUG (debug_method_add (w, "A"));
15091 }
15092 else if (last_text_row_at_end)
15093 {
15094 w->window_end_pos
15095 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15096 w->window_end_bytepos
15097 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15098 w->window_end_vpos
15099 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15100 xassert (w->window_end_bytepos >= 0);
15101 IF_DEBUG (debug_method_add (w, "B"));
15102 }
15103 else if (last_text_row)
15104 {
15105 /* We have displayed either to the end of the window or at the
15106 end of the window, i.e. the last row with text is to be found
15107 in the desired matrix. */
15108 w->window_end_pos
15109 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15110 w->window_end_bytepos
15111 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15112 w->window_end_vpos
15113 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15114 xassert (w->window_end_bytepos >= 0);
15115 }
15116 else if (first_unchanged_at_end_row == NULL
15117 && last_text_row == NULL
15118 && last_text_row_at_end == NULL)
15119 {
15120 /* Displayed to end of window, but no line containing text was
15121 displayed. Lines were deleted at the end of the window. */
15122 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15123 int vpos = XFASTINT (w->window_end_vpos);
15124 struct glyph_row *current_row = current_matrix->rows + vpos;
15125 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15126
15127 for (row = NULL;
15128 row == NULL && vpos >= first_vpos;
15129 --vpos, --current_row, --desired_row)
15130 {
15131 if (desired_row->enabled_p)
15132 {
15133 if (desired_row->displays_text_p)
15134 row = desired_row;
15135 }
15136 else if (current_row->displays_text_p)
15137 row = current_row;
15138 }
15139
15140 xassert (row != NULL);
15141 w->window_end_vpos = make_number (vpos + 1);
15142 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15143 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15144 xassert (w->window_end_bytepos >= 0);
15145 IF_DEBUG (debug_method_add (w, "C"));
15146 }
15147 else
15148 abort ();
15149
15150 #if 0 /* This leads to problems, for instance when the cursor is
15151 at ZV, and the cursor line displays no text. */
15152 /* Disable rows below what's displayed in the window. This makes
15153 debugging easier. */
15154 enable_glyph_matrix_rows (current_matrix,
15155 XFASTINT (w->window_end_vpos) + 1,
15156 bottom_vpos, 0);
15157 #endif
15158
15159 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15160 debug_end_vpos = XFASTINT (w->window_end_vpos));
15161
15162 /* Record that display has not been completed. */
15163 w->window_end_valid = Qnil;
15164 w->desired_matrix->no_scrolling_p = 1;
15165 return 3;
15166
15167 #undef GIVE_UP
15168 }
15169
15170
15171 \f
15172 /***********************************************************************
15173 More debugging support
15174 ***********************************************************************/
15175
15176 #if GLYPH_DEBUG
15177
15178 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15179 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15180 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15181
15182
15183 /* Dump the contents of glyph matrix MATRIX on stderr.
15184
15185 GLYPHS 0 means don't show glyph contents.
15186 GLYPHS 1 means show glyphs in short form
15187 GLYPHS > 1 means show glyphs in long form. */
15188
15189 void
15190 dump_glyph_matrix (matrix, glyphs)
15191 struct glyph_matrix *matrix;
15192 int glyphs;
15193 {
15194 int i;
15195 for (i = 0; i < matrix->nrows; ++i)
15196 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15197 }
15198
15199
15200 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15201 the glyph row and area where the glyph comes from. */
15202
15203 void
15204 dump_glyph (row, glyph, area)
15205 struct glyph_row *row;
15206 struct glyph *glyph;
15207 int area;
15208 {
15209 if (glyph->type == CHAR_GLYPH)
15210 {
15211 fprintf (stderr,
15212 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15213 glyph - row->glyphs[TEXT_AREA],
15214 'C',
15215 glyph->charpos,
15216 (BUFFERP (glyph->object)
15217 ? 'B'
15218 : (STRINGP (glyph->object)
15219 ? 'S'
15220 : '-')),
15221 glyph->pixel_width,
15222 glyph->u.ch,
15223 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15224 ? glyph->u.ch
15225 : '.'),
15226 glyph->face_id,
15227 glyph->left_box_line_p,
15228 glyph->right_box_line_p);
15229 }
15230 else if (glyph->type == STRETCH_GLYPH)
15231 {
15232 fprintf (stderr,
15233 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15234 glyph - row->glyphs[TEXT_AREA],
15235 'S',
15236 glyph->charpos,
15237 (BUFFERP (glyph->object)
15238 ? 'B'
15239 : (STRINGP (glyph->object)
15240 ? 'S'
15241 : '-')),
15242 glyph->pixel_width,
15243 0,
15244 '.',
15245 glyph->face_id,
15246 glyph->left_box_line_p,
15247 glyph->right_box_line_p);
15248 }
15249 else if (glyph->type == IMAGE_GLYPH)
15250 {
15251 fprintf (stderr,
15252 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15253 glyph - row->glyphs[TEXT_AREA],
15254 'I',
15255 glyph->charpos,
15256 (BUFFERP (glyph->object)
15257 ? 'B'
15258 : (STRINGP (glyph->object)
15259 ? 'S'
15260 : '-')),
15261 glyph->pixel_width,
15262 glyph->u.img_id,
15263 '.',
15264 glyph->face_id,
15265 glyph->left_box_line_p,
15266 glyph->right_box_line_p);
15267 }
15268 else if (glyph->type == COMPOSITE_GLYPH)
15269 {
15270 fprintf (stderr,
15271 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15272 glyph - row->glyphs[TEXT_AREA],
15273 '+',
15274 glyph->charpos,
15275 (BUFFERP (glyph->object)
15276 ? 'B'
15277 : (STRINGP (glyph->object)
15278 ? 'S'
15279 : '-')),
15280 glyph->pixel_width,
15281 glyph->u.cmp_id,
15282 '.',
15283 glyph->face_id,
15284 glyph->left_box_line_p,
15285 glyph->right_box_line_p);
15286 }
15287 }
15288
15289
15290 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15291 GLYPHS 0 means don't show glyph contents.
15292 GLYPHS 1 means show glyphs in short form
15293 GLYPHS > 1 means show glyphs in long form. */
15294
15295 void
15296 dump_glyph_row (row, vpos, glyphs)
15297 struct glyph_row *row;
15298 int vpos, glyphs;
15299 {
15300 if (glyphs != 1)
15301 {
15302 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15303 fprintf (stderr, "======================================================================\n");
15304
15305 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15306 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15307 vpos,
15308 MATRIX_ROW_START_CHARPOS (row),
15309 MATRIX_ROW_END_CHARPOS (row),
15310 row->used[TEXT_AREA],
15311 row->contains_overlapping_glyphs_p,
15312 row->enabled_p,
15313 row->truncated_on_left_p,
15314 row->truncated_on_right_p,
15315 row->continued_p,
15316 MATRIX_ROW_CONTINUATION_LINE_P (row),
15317 row->displays_text_p,
15318 row->ends_at_zv_p,
15319 row->fill_line_p,
15320 row->ends_in_middle_of_char_p,
15321 row->starts_in_middle_of_char_p,
15322 row->mouse_face_p,
15323 row->x,
15324 row->y,
15325 row->pixel_width,
15326 row->height,
15327 row->visible_height,
15328 row->ascent,
15329 row->phys_ascent);
15330 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15331 row->end.overlay_string_index,
15332 row->continuation_lines_width);
15333 fprintf (stderr, "%9d %5d\n",
15334 CHARPOS (row->start.string_pos),
15335 CHARPOS (row->end.string_pos));
15336 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15337 row->end.dpvec_index);
15338 }
15339
15340 if (glyphs > 1)
15341 {
15342 int area;
15343
15344 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15345 {
15346 struct glyph *glyph = row->glyphs[area];
15347 struct glyph *glyph_end = glyph + row->used[area];
15348
15349 /* Glyph for a line end in text. */
15350 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15351 ++glyph_end;
15352
15353 if (glyph < glyph_end)
15354 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15355
15356 for (; glyph < glyph_end; ++glyph)
15357 dump_glyph (row, glyph, area);
15358 }
15359 }
15360 else if (glyphs == 1)
15361 {
15362 int area;
15363
15364 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15365 {
15366 char *s = (char *) alloca (row->used[area] + 1);
15367 int i;
15368
15369 for (i = 0; i < row->used[area]; ++i)
15370 {
15371 struct glyph *glyph = row->glyphs[area] + i;
15372 if (glyph->type == CHAR_GLYPH
15373 && glyph->u.ch < 0x80
15374 && glyph->u.ch >= ' ')
15375 s[i] = glyph->u.ch;
15376 else
15377 s[i] = '.';
15378 }
15379
15380 s[i] = '\0';
15381 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15382 }
15383 }
15384 }
15385
15386
15387 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15388 Sdump_glyph_matrix, 0, 1, "p",
15389 doc: /* Dump the current matrix of the selected window to stderr.
15390 Shows contents of glyph row structures. With non-nil
15391 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15392 glyphs in short form, otherwise show glyphs in long form. */)
15393 (glyphs)
15394 Lisp_Object glyphs;
15395 {
15396 struct window *w = XWINDOW (selected_window);
15397 struct buffer *buffer = XBUFFER (w->buffer);
15398
15399 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15400 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15401 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15402 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15403 fprintf (stderr, "=============================================\n");
15404 dump_glyph_matrix (w->current_matrix,
15405 NILP (glyphs) ? 0 : XINT (glyphs));
15406 return Qnil;
15407 }
15408
15409
15410 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15411 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15412 ()
15413 {
15414 struct frame *f = XFRAME (selected_frame);
15415 dump_glyph_matrix (f->current_matrix, 1);
15416 return Qnil;
15417 }
15418
15419
15420 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15421 doc: /* Dump glyph row ROW to stderr.
15422 GLYPH 0 means don't dump glyphs.
15423 GLYPH 1 means dump glyphs in short form.
15424 GLYPH > 1 or omitted means dump glyphs in long form. */)
15425 (row, glyphs)
15426 Lisp_Object row, glyphs;
15427 {
15428 struct glyph_matrix *matrix;
15429 int vpos;
15430
15431 CHECK_NUMBER (row);
15432 matrix = XWINDOW (selected_window)->current_matrix;
15433 vpos = XINT (row);
15434 if (vpos >= 0 && vpos < matrix->nrows)
15435 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15436 vpos,
15437 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15438 return Qnil;
15439 }
15440
15441
15442 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15443 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15444 GLYPH 0 means don't dump glyphs.
15445 GLYPH 1 means dump glyphs in short form.
15446 GLYPH > 1 or omitted means dump glyphs in long form. */)
15447 (row, glyphs)
15448 Lisp_Object row, glyphs;
15449 {
15450 struct frame *sf = SELECTED_FRAME ();
15451 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15452 int vpos;
15453
15454 CHECK_NUMBER (row);
15455 vpos = XINT (row);
15456 if (vpos >= 0 && vpos < m->nrows)
15457 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15458 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15459 return Qnil;
15460 }
15461
15462
15463 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15464 doc: /* Toggle tracing of redisplay.
15465 With ARG, turn tracing on if and only if ARG is positive. */)
15466 (arg)
15467 Lisp_Object arg;
15468 {
15469 if (NILP (arg))
15470 trace_redisplay_p = !trace_redisplay_p;
15471 else
15472 {
15473 arg = Fprefix_numeric_value (arg);
15474 trace_redisplay_p = XINT (arg) > 0;
15475 }
15476
15477 return Qnil;
15478 }
15479
15480
15481 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15482 doc: /* Like `format', but print result to stderr.
15483 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15484 (nargs, args)
15485 int nargs;
15486 Lisp_Object *args;
15487 {
15488 Lisp_Object s = Fformat (nargs, args);
15489 fprintf (stderr, "%s", SDATA (s));
15490 return Qnil;
15491 }
15492
15493 #endif /* GLYPH_DEBUG */
15494
15495
15496 \f
15497 /***********************************************************************
15498 Building Desired Matrix Rows
15499 ***********************************************************************/
15500
15501 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15502 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15503
15504 static struct glyph_row *
15505 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15506 struct window *w;
15507 Lisp_Object overlay_arrow_string;
15508 {
15509 struct frame *f = XFRAME (WINDOW_FRAME (w));
15510 struct buffer *buffer = XBUFFER (w->buffer);
15511 struct buffer *old = current_buffer;
15512 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15513 int arrow_len = SCHARS (overlay_arrow_string);
15514 const unsigned char *arrow_end = arrow_string + arrow_len;
15515 const unsigned char *p;
15516 struct it it;
15517 int multibyte_p;
15518 int n_glyphs_before;
15519
15520 set_buffer_temp (buffer);
15521 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15522 it.glyph_row->used[TEXT_AREA] = 0;
15523 SET_TEXT_POS (it.position, 0, 0);
15524
15525 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15526 p = arrow_string;
15527 while (p < arrow_end)
15528 {
15529 Lisp_Object face, ilisp;
15530
15531 /* Get the next character. */
15532 if (multibyte_p)
15533 it.c = string_char_and_length (p, arrow_len, &it.len);
15534 else
15535 it.c = *p, it.len = 1;
15536 p += it.len;
15537
15538 /* Get its face. */
15539 ilisp = make_number (p - arrow_string);
15540 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15541 it.face_id = compute_char_face (f, it.c, face);
15542
15543 /* Compute its width, get its glyphs. */
15544 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15545 SET_TEXT_POS (it.position, -1, -1);
15546 PRODUCE_GLYPHS (&it);
15547
15548 /* If this character doesn't fit any more in the line, we have
15549 to remove some glyphs. */
15550 if (it.current_x > it.last_visible_x)
15551 {
15552 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15553 break;
15554 }
15555 }
15556
15557 set_buffer_temp (old);
15558 return it.glyph_row;
15559 }
15560
15561
15562 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15563 glyphs are only inserted for terminal frames since we can't really
15564 win with truncation glyphs when partially visible glyphs are
15565 involved. Which glyphs to insert is determined by
15566 produce_special_glyphs. */
15567
15568 static void
15569 insert_left_trunc_glyphs (it)
15570 struct it *it;
15571 {
15572 struct it truncate_it;
15573 struct glyph *from, *end, *to, *toend;
15574
15575 xassert (!FRAME_WINDOW_P (it->f));
15576
15577 /* Get the truncation glyphs. */
15578 truncate_it = *it;
15579 truncate_it.current_x = 0;
15580 truncate_it.face_id = DEFAULT_FACE_ID;
15581 truncate_it.glyph_row = &scratch_glyph_row;
15582 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15583 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15584 truncate_it.object = make_number (0);
15585 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15586
15587 /* Overwrite glyphs from IT with truncation glyphs. */
15588 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15589 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15590 to = it->glyph_row->glyphs[TEXT_AREA];
15591 toend = to + it->glyph_row->used[TEXT_AREA];
15592
15593 while (from < end)
15594 *to++ = *from++;
15595
15596 /* There may be padding glyphs left over. Overwrite them too. */
15597 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15598 {
15599 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15600 while (from < end)
15601 *to++ = *from++;
15602 }
15603
15604 if (to > toend)
15605 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15606 }
15607
15608
15609 /* Compute the pixel height and width of IT->glyph_row.
15610
15611 Most of the time, ascent and height of a display line will be equal
15612 to the max_ascent and max_height values of the display iterator
15613 structure. This is not the case if
15614
15615 1. We hit ZV without displaying anything. In this case, max_ascent
15616 and max_height will be zero.
15617
15618 2. We have some glyphs that don't contribute to the line height.
15619 (The glyph row flag contributes_to_line_height_p is for future
15620 pixmap extensions).
15621
15622 The first case is easily covered by using default values because in
15623 these cases, the line height does not really matter, except that it
15624 must not be zero. */
15625
15626 static void
15627 compute_line_metrics (it)
15628 struct it *it;
15629 {
15630 struct glyph_row *row = it->glyph_row;
15631 int area, i;
15632
15633 if (FRAME_WINDOW_P (it->f))
15634 {
15635 int i, min_y, max_y;
15636
15637 /* The line may consist of one space only, that was added to
15638 place the cursor on it. If so, the row's height hasn't been
15639 computed yet. */
15640 if (row->height == 0)
15641 {
15642 if (it->max_ascent + it->max_descent == 0)
15643 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15644 row->ascent = it->max_ascent;
15645 row->height = it->max_ascent + it->max_descent;
15646 row->phys_ascent = it->max_phys_ascent;
15647 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15648 row->extra_line_spacing = it->max_extra_line_spacing;
15649 }
15650
15651 /* Compute the width of this line. */
15652 row->pixel_width = row->x;
15653 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15654 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15655
15656 xassert (row->pixel_width >= 0);
15657 xassert (row->ascent >= 0 && row->height > 0);
15658
15659 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15660 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15661
15662 /* If first line's physical ascent is larger than its logical
15663 ascent, use the physical ascent, and make the row taller.
15664 This makes accented characters fully visible. */
15665 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15666 && row->phys_ascent > row->ascent)
15667 {
15668 row->height += row->phys_ascent - row->ascent;
15669 row->ascent = row->phys_ascent;
15670 }
15671
15672 /* Compute how much of the line is visible. */
15673 row->visible_height = row->height;
15674
15675 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15676 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15677
15678 if (row->y < min_y)
15679 row->visible_height -= min_y - row->y;
15680 if (row->y + row->height > max_y)
15681 row->visible_height -= row->y + row->height - max_y;
15682 }
15683 else
15684 {
15685 row->pixel_width = row->used[TEXT_AREA];
15686 if (row->continued_p)
15687 row->pixel_width -= it->continuation_pixel_width;
15688 else if (row->truncated_on_right_p)
15689 row->pixel_width -= it->truncation_pixel_width;
15690 row->ascent = row->phys_ascent = 0;
15691 row->height = row->phys_height = row->visible_height = 1;
15692 row->extra_line_spacing = 0;
15693 }
15694
15695 /* Compute a hash code for this row. */
15696 row->hash = 0;
15697 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15698 for (i = 0; i < row->used[area]; ++i)
15699 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15700 + row->glyphs[area][i].u.val
15701 + row->glyphs[area][i].face_id
15702 + row->glyphs[area][i].padding_p
15703 + (row->glyphs[area][i].type << 2));
15704
15705 it->max_ascent = it->max_descent = 0;
15706 it->max_phys_ascent = it->max_phys_descent = 0;
15707 }
15708
15709
15710 /* Append one space to the glyph row of iterator IT if doing a
15711 window-based redisplay. The space has the same face as
15712 IT->face_id. Value is non-zero if a space was added.
15713
15714 This function is called to make sure that there is always one glyph
15715 at the end of a glyph row that the cursor can be set on under
15716 window-systems. (If there weren't such a glyph we would not know
15717 how wide and tall a box cursor should be displayed).
15718
15719 At the same time this space let's a nicely handle clearing to the
15720 end of the line if the row ends in italic text. */
15721
15722 static int
15723 append_space_for_newline (it, default_face_p)
15724 struct it *it;
15725 int default_face_p;
15726 {
15727 if (FRAME_WINDOW_P (it->f))
15728 {
15729 int n = it->glyph_row->used[TEXT_AREA];
15730
15731 if (it->glyph_row->glyphs[TEXT_AREA] + n
15732 < it->glyph_row->glyphs[1 + TEXT_AREA])
15733 {
15734 /* Save some values that must not be changed.
15735 Must save IT->c and IT->len because otherwise
15736 ITERATOR_AT_END_P wouldn't work anymore after
15737 append_space_for_newline has been called. */
15738 enum display_element_type saved_what = it->what;
15739 int saved_c = it->c, saved_len = it->len;
15740 int saved_x = it->current_x;
15741 int saved_face_id = it->face_id;
15742 struct text_pos saved_pos;
15743 Lisp_Object saved_object;
15744 struct face *face;
15745
15746 saved_object = it->object;
15747 saved_pos = it->position;
15748
15749 it->what = IT_CHARACTER;
15750 bzero (&it->position, sizeof it->position);
15751 it->object = make_number (0);
15752 it->c = ' ';
15753 it->len = 1;
15754
15755 if (default_face_p)
15756 it->face_id = DEFAULT_FACE_ID;
15757 else if (it->face_before_selective_p)
15758 it->face_id = it->saved_face_id;
15759 face = FACE_FROM_ID (it->f, it->face_id);
15760 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15761
15762 PRODUCE_GLYPHS (it);
15763
15764 it->override_ascent = -1;
15765 it->constrain_row_ascent_descent_p = 0;
15766 it->current_x = saved_x;
15767 it->object = saved_object;
15768 it->position = saved_pos;
15769 it->what = saved_what;
15770 it->face_id = saved_face_id;
15771 it->len = saved_len;
15772 it->c = saved_c;
15773 return 1;
15774 }
15775 }
15776
15777 return 0;
15778 }
15779
15780
15781 /* Extend the face of the last glyph in the text area of IT->glyph_row
15782 to the end of the display line. Called from display_line.
15783 If the glyph row is empty, add a space glyph to it so that we
15784 know the face to draw. Set the glyph row flag fill_line_p. */
15785
15786 static void
15787 extend_face_to_end_of_line (it)
15788 struct it *it;
15789 {
15790 struct face *face;
15791 struct frame *f = it->f;
15792
15793 /* If line is already filled, do nothing. */
15794 if (it->current_x >= it->last_visible_x)
15795 return;
15796
15797 /* Face extension extends the background and box of IT->face_id
15798 to the end of the line. If the background equals the background
15799 of the frame, we don't have to do anything. */
15800 if (it->face_before_selective_p)
15801 face = FACE_FROM_ID (it->f, it->saved_face_id);
15802 else
15803 face = FACE_FROM_ID (f, it->face_id);
15804
15805 if (FRAME_WINDOW_P (f)
15806 && it->glyph_row->displays_text_p
15807 && face->box == FACE_NO_BOX
15808 && face->background == FRAME_BACKGROUND_PIXEL (f)
15809 && !face->stipple)
15810 return;
15811
15812 /* Set the glyph row flag indicating that the face of the last glyph
15813 in the text area has to be drawn to the end of the text area. */
15814 it->glyph_row->fill_line_p = 1;
15815
15816 /* If current character of IT is not ASCII, make sure we have the
15817 ASCII face. This will be automatically undone the next time
15818 get_next_display_element returns a multibyte character. Note
15819 that the character will always be single byte in unibyte text. */
15820 if (!SINGLE_BYTE_CHAR_P (it->c))
15821 {
15822 it->face_id = FACE_FOR_CHAR (f, face, 0);
15823 }
15824
15825 if (FRAME_WINDOW_P (f))
15826 {
15827 /* If the row is empty, add a space with the current face of IT,
15828 so that we know which face to draw. */
15829 if (it->glyph_row->used[TEXT_AREA] == 0)
15830 {
15831 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15832 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15833 it->glyph_row->used[TEXT_AREA] = 1;
15834 }
15835 }
15836 else
15837 {
15838 /* Save some values that must not be changed. */
15839 int saved_x = it->current_x;
15840 struct text_pos saved_pos;
15841 Lisp_Object saved_object;
15842 enum display_element_type saved_what = it->what;
15843 int saved_face_id = it->face_id;
15844
15845 saved_object = it->object;
15846 saved_pos = it->position;
15847
15848 it->what = IT_CHARACTER;
15849 bzero (&it->position, sizeof it->position);
15850 it->object = make_number (0);
15851 it->c = ' ';
15852 it->len = 1;
15853 it->face_id = face->id;
15854
15855 PRODUCE_GLYPHS (it);
15856
15857 while (it->current_x <= it->last_visible_x)
15858 PRODUCE_GLYPHS (it);
15859
15860 /* Don't count these blanks really. It would let us insert a left
15861 truncation glyph below and make us set the cursor on them, maybe. */
15862 it->current_x = saved_x;
15863 it->object = saved_object;
15864 it->position = saved_pos;
15865 it->what = saved_what;
15866 it->face_id = saved_face_id;
15867 }
15868 }
15869
15870
15871 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15872 trailing whitespace. */
15873
15874 static int
15875 trailing_whitespace_p (charpos)
15876 int charpos;
15877 {
15878 int bytepos = CHAR_TO_BYTE (charpos);
15879 int c = 0;
15880
15881 while (bytepos < ZV_BYTE
15882 && (c = FETCH_CHAR (bytepos),
15883 c == ' ' || c == '\t'))
15884 ++bytepos;
15885
15886 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15887 {
15888 if (bytepos != PT_BYTE)
15889 return 1;
15890 }
15891 return 0;
15892 }
15893
15894
15895 /* Highlight trailing whitespace, if any, in ROW. */
15896
15897 void
15898 highlight_trailing_whitespace (f, row)
15899 struct frame *f;
15900 struct glyph_row *row;
15901 {
15902 int used = row->used[TEXT_AREA];
15903
15904 if (used)
15905 {
15906 struct glyph *start = row->glyphs[TEXT_AREA];
15907 struct glyph *glyph = start + used - 1;
15908
15909 /* Skip over glyphs inserted to display the cursor at the
15910 end of a line, for extending the face of the last glyph
15911 to the end of the line on terminals, and for truncation
15912 and continuation glyphs. */
15913 while (glyph >= start
15914 && glyph->type == CHAR_GLYPH
15915 && INTEGERP (glyph->object))
15916 --glyph;
15917
15918 /* If last glyph is a space or stretch, and it's trailing
15919 whitespace, set the face of all trailing whitespace glyphs in
15920 IT->glyph_row to `trailing-whitespace'. */
15921 if (glyph >= start
15922 && BUFFERP (glyph->object)
15923 && (glyph->type == STRETCH_GLYPH
15924 || (glyph->type == CHAR_GLYPH
15925 && glyph->u.ch == ' '))
15926 && trailing_whitespace_p (glyph->charpos))
15927 {
15928 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15929 if (face_id < 0)
15930 return;
15931
15932 while (glyph >= start
15933 && BUFFERP (glyph->object)
15934 && (glyph->type == STRETCH_GLYPH
15935 || (glyph->type == CHAR_GLYPH
15936 && glyph->u.ch == ' ')))
15937 (glyph--)->face_id = face_id;
15938 }
15939 }
15940 }
15941
15942
15943 /* Value is non-zero if glyph row ROW in window W should be
15944 used to hold the cursor. */
15945
15946 static int
15947 cursor_row_p (w, row)
15948 struct window *w;
15949 struct glyph_row *row;
15950 {
15951 int cursor_row_p = 1;
15952
15953 if (PT == MATRIX_ROW_END_CHARPOS (row))
15954 {
15955 /* Suppose the row ends on a string.
15956 Unless the row is continued, that means it ends on a newline
15957 in the string. If it's anything other than a display string
15958 (e.g. a before-string from an overlay), we don't want the
15959 cursor there. (This heuristic seems to give the optimal
15960 behavior for the various types of multi-line strings.) */
15961 if (CHARPOS (row->end.string_pos) >= 0)
15962 {
15963 if (row->continued_p)
15964 cursor_row_p = 1;
15965 else
15966 {
15967 /* Check for `display' property. */
15968 struct glyph *beg = row->glyphs[TEXT_AREA];
15969 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15970 struct glyph *glyph;
15971
15972 cursor_row_p = 0;
15973 for (glyph = end; glyph >= beg; --glyph)
15974 if (STRINGP (glyph->object))
15975 {
15976 Lisp_Object prop
15977 = Fget_char_property (make_number (PT),
15978 Qdisplay, Qnil);
15979 cursor_row_p =
15980 (!NILP (prop)
15981 && display_prop_string_p (prop, glyph->object));
15982 break;
15983 }
15984 }
15985 }
15986 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15987 {
15988 /* If the row ends in middle of a real character,
15989 and the line is continued, we want the cursor here.
15990 That's because MATRIX_ROW_END_CHARPOS would equal
15991 PT if PT is before the character. */
15992 if (!row->ends_in_ellipsis_p)
15993 cursor_row_p = row->continued_p;
15994 else
15995 /* If the row ends in an ellipsis, then
15996 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15997 We want that position to be displayed after the ellipsis. */
15998 cursor_row_p = 0;
15999 }
16000 /* If the row ends at ZV, display the cursor at the end of that
16001 row instead of at the start of the row below. */
16002 else if (row->ends_at_zv_p)
16003 cursor_row_p = 1;
16004 else
16005 cursor_row_p = 0;
16006 }
16007
16008 return cursor_row_p;
16009 }
16010
16011
16012 /* Construct the glyph row IT->glyph_row in the desired matrix of
16013 IT->w from text at the current position of IT. See dispextern.h
16014 for an overview of struct it. Value is non-zero if
16015 IT->glyph_row displays text, as opposed to a line displaying ZV
16016 only. */
16017
16018 static int
16019 display_line (it)
16020 struct it *it;
16021 {
16022 struct glyph_row *row = it->glyph_row;
16023 Lisp_Object overlay_arrow_string;
16024
16025 /* We always start displaying at hpos zero even if hscrolled. */
16026 xassert (it->hpos == 0 && it->current_x == 0);
16027
16028 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16029 >= it->w->desired_matrix->nrows)
16030 {
16031 it->w->nrows_scale_factor++;
16032 fonts_changed_p = 1;
16033 return 0;
16034 }
16035
16036 /* Is IT->w showing the region? */
16037 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16038
16039 /* Clear the result glyph row and enable it. */
16040 prepare_desired_row (row);
16041
16042 row->y = it->current_y;
16043 row->start = it->start;
16044 row->continuation_lines_width = it->continuation_lines_width;
16045 row->displays_text_p = 1;
16046 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16047 it->starts_in_middle_of_char_p = 0;
16048
16049 /* Arrange the overlays nicely for our purposes. Usually, we call
16050 display_line on only one line at a time, in which case this
16051 can't really hurt too much, or we call it on lines which appear
16052 one after another in the buffer, in which case all calls to
16053 recenter_overlay_lists but the first will be pretty cheap. */
16054 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16055
16056 /* Move over display elements that are not visible because we are
16057 hscrolled. This may stop at an x-position < IT->first_visible_x
16058 if the first glyph is partially visible or if we hit a line end. */
16059 if (it->current_x < it->first_visible_x)
16060 {
16061 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16062 MOVE_TO_POS | MOVE_TO_X);
16063 }
16064
16065 /* Get the initial row height. This is either the height of the
16066 text hscrolled, if there is any, or zero. */
16067 row->ascent = it->max_ascent;
16068 row->height = it->max_ascent + it->max_descent;
16069 row->phys_ascent = it->max_phys_ascent;
16070 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16071 row->extra_line_spacing = it->max_extra_line_spacing;
16072
16073 /* Loop generating characters. The loop is left with IT on the next
16074 character to display. */
16075 while (1)
16076 {
16077 int n_glyphs_before, hpos_before, x_before;
16078 int x, i, nglyphs;
16079 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16080
16081 /* Retrieve the next thing to display. Value is zero if end of
16082 buffer reached. */
16083 if (!get_next_display_element (it))
16084 {
16085 /* Maybe add a space at the end of this line that is used to
16086 display the cursor there under X. Set the charpos of the
16087 first glyph of blank lines not corresponding to any text
16088 to -1. */
16089 #ifdef HAVE_WINDOW_SYSTEM
16090 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16091 row->exact_window_width_line_p = 1;
16092 else
16093 #endif /* HAVE_WINDOW_SYSTEM */
16094 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16095 || row->used[TEXT_AREA] == 0)
16096 {
16097 row->glyphs[TEXT_AREA]->charpos = -1;
16098 row->displays_text_p = 0;
16099
16100 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16101 && (!MINI_WINDOW_P (it->w)
16102 || (minibuf_level && EQ (it->window, minibuf_window))))
16103 row->indicate_empty_line_p = 1;
16104 }
16105
16106 it->continuation_lines_width = 0;
16107 row->ends_at_zv_p = 1;
16108 break;
16109 }
16110
16111 /* Now, get the metrics of what we want to display. This also
16112 generates glyphs in `row' (which is IT->glyph_row). */
16113 n_glyphs_before = row->used[TEXT_AREA];
16114 x = it->current_x;
16115
16116 /* Remember the line height so far in case the next element doesn't
16117 fit on the line. */
16118 if (!it->truncate_lines_p)
16119 {
16120 ascent = it->max_ascent;
16121 descent = it->max_descent;
16122 phys_ascent = it->max_phys_ascent;
16123 phys_descent = it->max_phys_descent;
16124 }
16125
16126 PRODUCE_GLYPHS (it);
16127
16128 /* If this display element was in marginal areas, continue with
16129 the next one. */
16130 if (it->area != TEXT_AREA)
16131 {
16132 row->ascent = max (row->ascent, it->max_ascent);
16133 row->height = max (row->height, it->max_ascent + it->max_descent);
16134 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16135 row->phys_height = max (row->phys_height,
16136 it->max_phys_ascent + it->max_phys_descent);
16137 row->extra_line_spacing = max (row->extra_line_spacing,
16138 it->max_extra_line_spacing);
16139 set_iterator_to_next (it, 1);
16140 continue;
16141 }
16142
16143 /* Does the display element fit on the line? If we truncate
16144 lines, we should draw past the right edge of the window. If
16145 we don't truncate, we want to stop so that we can display the
16146 continuation glyph before the right margin. If lines are
16147 continued, there are two possible strategies for characters
16148 resulting in more than 1 glyph (e.g. tabs): Display as many
16149 glyphs as possible in this line and leave the rest for the
16150 continuation line, or display the whole element in the next
16151 line. Original redisplay did the former, so we do it also. */
16152 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16153 hpos_before = it->hpos;
16154 x_before = x;
16155
16156 if (/* Not a newline. */
16157 nglyphs > 0
16158 /* Glyphs produced fit entirely in the line. */
16159 && it->current_x < it->last_visible_x)
16160 {
16161 it->hpos += nglyphs;
16162 row->ascent = max (row->ascent, it->max_ascent);
16163 row->height = max (row->height, it->max_ascent + it->max_descent);
16164 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16165 row->phys_height = max (row->phys_height,
16166 it->max_phys_ascent + it->max_phys_descent);
16167 row->extra_line_spacing = max (row->extra_line_spacing,
16168 it->max_extra_line_spacing);
16169 if (it->current_x - it->pixel_width < it->first_visible_x)
16170 row->x = x - it->first_visible_x;
16171 }
16172 else
16173 {
16174 int new_x;
16175 struct glyph *glyph;
16176
16177 for (i = 0; i < nglyphs; ++i, x = new_x)
16178 {
16179 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16180 new_x = x + glyph->pixel_width;
16181
16182 if (/* Lines are continued. */
16183 !it->truncate_lines_p
16184 && (/* Glyph doesn't fit on the line. */
16185 new_x > it->last_visible_x
16186 /* Or it fits exactly on a window system frame. */
16187 || (new_x == it->last_visible_x
16188 && FRAME_WINDOW_P (it->f))))
16189 {
16190 /* End of a continued line. */
16191
16192 if (it->hpos == 0
16193 || (new_x == it->last_visible_x
16194 && FRAME_WINDOW_P (it->f)))
16195 {
16196 /* Current glyph is the only one on the line or
16197 fits exactly on the line. We must continue
16198 the line because we can't draw the cursor
16199 after the glyph. */
16200 row->continued_p = 1;
16201 it->current_x = new_x;
16202 it->continuation_lines_width += new_x;
16203 ++it->hpos;
16204 if (i == nglyphs - 1)
16205 {
16206 set_iterator_to_next (it, 1);
16207 #ifdef HAVE_WINDOW_SYSTEM
16208 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16209 {
16210 if (!get_next_display_element (it))
16211 {
16212 row->exact_window_width_line_p = 1;
16213 it->continuation_lines_width = 0;
16214 row->continued_p = 0;
16215 row->ends_at_zv_p = 1;
16216 }
16217 else if (ITERATOR_AT_END_OF_LINE_P (it))
16218 {
16219 row->continued_p = 0;
16220 row->exact_window_width_line_p = 1;
16221 }
16222 }
16223 #endif /* HAVE_WINDOW_SYSTEM */
16224 }
16225 }
16226 else if (CHAR_GLYPH_PADDING_P (*glyph)
16227 && !FRAME_WINDOW_P (it->f))
16228 {
16229 /* A padding glyph that doesn't fit on this line.
16230 This means the whole character doesn't fit
16231 on the line. */
16232 row->used[TEXT_AREA] = n_glyphs_before;
16233
16234 /* Fill the rest of the row with continuation
16235 glyphs like in 20.x. */
16236 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16237 < row->glyphs[1 + TEXT_AREA])
16238 produce_special_glyphs (it, IT_CONTINUATION);
16239
16240 row->continued_p = 1;
16241 it->current_x = x_before;
16242 it->continuation_lines_width += x_before;
16243
16244 /* Restore the height to what it was before the
16245 element not fitting on the line. */
16246 it->max_ascent = ascent;
16247 it->max_descent = descent;
16248 it->max_phys_ascent = phys_ascent;
16249 it->max_phys_descent = phys_descent;
16250 }
16251 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16252 {
16253 /* A TAB that extends past the right edge of the
16254 window. This produces a single glyph on
16255 window system frames. We leave the glyph in
16256 this row and let it fill the row, but don't
16257 consume the TAB. */
16258 it->continuation_lines_width += it->last_visible_x;
16259 row->ends_in_middle_of_char_p = 1;
16260 row->continued_p = 1;
16261 glyph->pixel_width = it->last_visible_x - x;
16262 it->starts_in_middle_of_char_p = 1;
16263 }
16264 else
16265 {
16266 /* Something other than a TAB that draws past
16267 the right edge of the window. Restore
16268 positions to values before the element. */
16269 row->used[TEXT_AREA] = n_glyphs_before + i;
16270
16271 /* Display continuation glyphs. */
16272 if (!FRAME_WINDOW_P (it->f))
16273 produce_special_glyphs (it, IT_CONTINUATION);
16274 row->continued_p = 1;
16275
16276 it->current_x = x_before;
16277 it->continuation_lines_width += x;
16278 extend_face_to_end_of_line (it);
16279
16280 if (nglyphs > 1 && i > 0)
16281 {
16282 row->ends_in_middle_of_char_p = 1;
16283 it->starts_in_middle_of_char_p = 1;
16284 }
16285
16286 /* Restore the height to what it was before the
16287 element not fitting on the line. */
16288 it->max_ascent = ascent;
16289 it->max_descent = descent;
16290 it->max_phys_ascent = phys_ascent;
16291 it->max_phys_descent = phys_descent;
16292 }
16293
16294 break;
16295 }
16296 else if (new_x > it->first_visible_x)
16297 {
16298 /* Increment number of glyphs actually displayed. */
16299 ++it->hpos;
16300
16301 if (x < it->first_visible_x)
16302 /* Glyph is partially visible, i.e. row starts at
16303 negative X position. */
16304 row->x = x - it->first_visible_x;
16305 }
16306 else
16307 {
16308 /* Glyph is completely off the left margin of the
16309 window. This should not happen because of the
16310 move_it_in_display_line at the start of this
16311 function, unless the text display area of the
16312 window is empty. */
16313 xassert (it->first_visible_x <= it->last_visible_x);
16314 }
16315 }
16316
16317 row->ascent = max (row->ascent, it->max_ascent);
16318 row->height = max (row->height, it->max_ascent + it->max_descent);
16319 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16320 row->phys_height = max (row->phys_height,
16321 it->max_phys_ascent + it->max_phys_descent);
16322 row->extra_line_spacing = max (row->extra_line_spacing,
16323 it->max_extra_line_spacing);
16324
16325 /* End of this display line if row is continued. */
16326 if (row->continued_p || row->ends_at_zv_p)
16327 break;
16328 }
16329
16330 at_end_of_line:
16331 /* Is this a line end? If yes, we're also done, after making
16332 sure that a non-default face is extended up to the right
16333 margin of the window. */
16334 if (ITERATOR_AT_END_OF_LINE_P (it))
16335 {
16336 int used_before = row->used[TEXT_AREA];
16337
16338 row->ends_in_newline_from_string_p = STRINGP (it->object);
16339
16340 #ifdef HAVE_WINDOW_SYSTEM
16341 /* Add a space at the end of the line that is used to
16342 display the cursor there. */
16343 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16344 append_space_for_newline (it, 0);
16345 #endif /* HAVE_WINDOW_SYSTEM */
16346
16347 /* Extend the face to the end of the line. */
16348 extend_face_to_end_of_line (it);
16349
16350 /* Make sure we have the position. */
16351 if (used_before == 0)
16352 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16353
16354 /* Consume the line end. This skips over invisible lines. */
16355 set_iterator_to_next (it, 1);
16356 it->continuation_lines_width = 0;
16357 break;
16358 }
16359
16360 /* Proceed with next display element. Note that this skips
16361 over lines invisible because of selective display. */
16362 set_iterator_to_next (it, 1);
16363
16364 /* If we truncate lines, we are done when the last displayed
16365 glyphs reach past the right margin of the window. */
16366 if (it->truncate_lines_p
16367 && (FRAME_WINDOW_P (it->f)
16368 ? (it->current_x >= it->last_visible_x)
16369 : (it->current_x > it->last_visible_x)))
16370 {
16371 /* Maybe add truncation glyphs. */
16372 if (!FRAME_WINDOW_P (it->f))
16373 {
16374 int i, n;
16375
16376 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16377 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16378 break;
16379
16380 for (n = row->used[TEXT_AREA]; i < n; ++i)
16381 {
16382 row->used[TEXT_AREA] = i;
16383 produce_special_glyphs (it, IT_TRUNCATION);
16384 }
16385 }
16386 #ifdef HAVE_WINDOW_SYSTEM
16387 else
16388 {
16389 /* Don't truncate if we can overflow newline into fringe. */
16390 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16391 {
16392 if (!get_next_display_element (it))
16393 {
16394 it->continuation_lines_width = 0;
16395 row->ends_at_zv_p = 1;
16396 row->exact_window_width_line_p = 1;
16397 break;
16398 }
16399 if (ITERATOR_AT_END_OF_LINE_P (it))
16400 {
16401 row->exact_window_width_line_p = 1;
16402 goto at_end_of_line;
16403 }
16404 }
16405 }
16406 #endif /* HAVE_WINDOW_SYSTEM */
16407
16408 row->truncated_on_right_p = 1;
16409 it->continuation_lines_width = 0;
16410 reseat_at_next_visible_line_start (it, 0);
16411 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16412 it->hpos = hpos_before;
16413 it->current_x = x_before;
16414 break;
16415 }
16416 }
16417
16418 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16419 at the left window margin. */
16420 if (it->first_visible_x
16421 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16422 {
16423 if (!FRAME_WINDOW_P (it->f))
16424 insert_left_trunc_glyphs (it);
16425 row->truncated_on_left_p = 1;
16426 }
16427
16428 /* If the start of this line is the overlay arrow-position, then
16429 mark this glyph row as the one containing the overlay arrow.
16430 This is clearly a mess with variable size fonts. It would be
16431 better to let it be displayed like cursors under X. */
16432 if ((row->displays_text_p || !overlay_arrow_seen)
16433 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16434 !NILP (overlay_arrow_string)))
16435 {
16436 /* Overlay arrow in window redisplay is a fringe bitmap. */
16437 if (STRINGP (overlay_arrow_string))
16438 {
16439 struct glyph_row *arrow_row
16440 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16441 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16442 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16443 struct glyph *p = row->glyphs[TEXT_AREA];
16444 struct glyph *p2, *end;
16445
16446 /* Copy the arrow glyphs. */
16447 while (glyph < arrow_end)
16448 *p++ = *glyph++;
16449
16450 /* Throw away padding glyphs. */
16451 p2 = p;
16452 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16453 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16454 ++p2;
16455 if (p2 > p)
16456 {
16457 while (p2 < end)
16458 *p++ = *p2++;
16459 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16460 }
16461 }
16462 else
16463 {
16464 xassert (INTEGERP (overlay_arrow_string));
16465 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16466 }
16467 overlay_arrow_seen = 1;
16468 }
16469
16470 /* Compute pixel dimensions of this line. */
16471 compute_line_metrics (it);
16472
16473 /* Remember the position at which this line ends. */
16474 row->end = it->current;
16475
16476 /* Record whether this row ends inside an ellipsis. */
16477 row->ends_in_ellipsis_p
16478 = (it->method == GET_FROM_DISPLAY_VECTOR
16479 && it->ellipsis_p);
16480
16481 /* Save fringe bitmaps in this row. */
16482 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16483 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16484 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16485 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16486
16487 it->left_user_fringe_bitmap = 0;
16488 it->left_user_fringe_face_id = 0;
16489 it->right_user_fringe_bitmap = 0;
16490 it->right_user_fringe_face_id = 0;
16491
16492 /* Maybe set the cursor. */
16493 if (it->w->cursor.vpos < 0
16494 && PT >= MATRIX_ROW_START_CHARPOS (row)
16495 && PT <= MATRIX_ROW_END_CHARPOS (row)
16496 && cursor_row_p (it->w, row))
16497 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16498
16499 /* Highlight trailing whitespace. */
16500 if (!NILP (Vshow_trailing_whitespace))
16501 highlight_trailing_whitespace (it->f, it->glyph_row);
16502
16503 /* Prepare for the next line. This line starts horizontally at (X
16504 HPOS) = (0 0). Vertical positions are incremented. As a
16505 convenience for the caller, IT->glyph_row is set to the next
16506 row to be used. */
16507 it->current_x = it->hpos = 0;
16508 it->current_y += row->height;
16509 ++it->vpos;
16510 ++it->glyph_row;
16511 it->start = it->current;
16512 return row->displays_text_p;
16513 }
16514
16515
16516 \f
16517 /***********************************************************************
16518 Menu Bar
16519 ***********************************************************************/
16520
16521 /* Redisplay the menu bar in the frame for window W.
16522
16523 The menu bar of X frames that don't have X toolkit support is
16524 displayed in a special window W->frame->menu_bar_window.
16525
16526 The menu bar of terminal frames is treated specially as far as
16527 glyph matrices are concerned. Menu bar lines are not part of
16528 windows, so the update is done directly on the frame matrix rows
16529 for the menu bar. */
16530
16531 static void
16532 display_menu_bar (w)
16533 struct window *w;
16534 {
16535 struct frame *f = XFRAME (WINDOW_FRAME (w));
16536 struct it it;
16537 Lisp_Object items;
16538 int i;
16539
16540 /* Don't do all this for graphical frames. */
16541 #ifdef HAVE_NTGUI
16542 if (FRAME_W32_P (f))
16543 return;
16544 #endif
16545 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16546 if (FRAME_X_P (f))
16547 return;
16548 #endif
16549 #ifdef MAC_OS
16550 if (FRAME_MAC_P (f))
16551 return;
16552 #endif
16553
16554 #ifdef USE_X_TOOLKIT
16555 xassert (!FRAME_WINDOW_P (f));
16556 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16557 it.first_visible_x = 0;
16558 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16559 #else /* not USE_X_TOOLKIT */
16560 if (FRAME_WINDOW_P (f))
16561 {
16562 /* Menu bar lines are displayed in the desired matrix of the
16563 dummy window menu_bar_window. */
16564 struct window *menu_w;
16565 xassert (WINDOWP (f->menu_bar_window));
16566 menu_w = XWINDOW (f->menu_bar_window);
16567 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16568 MENU_FACE_ID);
16569 it.first_visible_x = 0;
16570 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16571 }
16572 else
16573 {
16574 /* This is a TTY frame, i.e. character hpos/vpos are used as
16575 pixel x/y. */
16576 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16577 MENU_FACE_ID);
16578 it.first_visible_x = 0;
16579 it.last_visible_x = FRAME_COLS (f);
16580 }
16581 #endif /* not USE_X_TOOLKIT */
16582
16583 if (! mode_line_inverse_video)
16584 /* Force the menu-bar to be displayed in the default face. */
16585 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16586
16587 /* Clear all rows of the menu bar. */
16588 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16589 {
16590 struct glyph_row *row = it.glyph_row + i;
16591 clear_glyph_row (row);
16592 row->enabled_p = 1;
16593 row->full_width_p = 1;
16594 }
16595
16596 /* Display all items of the menu bar. */
16597 items = FRAME_MENU_BAR_ITEMS (it.f);
16598 for (i = 0; i < XVECTOR (items)->size; i += 4)
16599 {
16600 Lisp_Object string;
16601
16602 /* Stop at nil string. */
16603 string = AREF (items, i + 1);
16604 if (NILP (string))
16605 break;
16606
16607 /* Remember where item was displayed. */
16608 AREF (items, i + 3) = make_number (it.hpos);
16609
16610 /* Display the item, pad with one space. */
16611 if (it.current_x < it.last_visible_x)
16612 display_string (NULL, string, Qnil, 0, 0, &it,
16613 SCHARS (string) + 1, 0, 0, -1);
16614 }
16615
16616 /* Fill out the line with spaces. */
16617 if (it.current_x < it.last_visible_x)
16618 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16619
16620 /* Compute the total height of the lines. */
16621 compute_line_metrics (&it);
16622 }
16623
16624
16625 \f
16626 /***********************************************************************
16627 Mode Line
16628 ***********************************************************************/
16629
16630 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16631 FORCE is non-zero, redisplay mode lines unconditionally.
16632 Otherwise, redisplay only mode lines that are garbaged. Value is
16633 the number of windows whose mode lines were redisplayed. */
16634
16635 static int
16636 redisplay_mode_lines (window, force)
16637 Lisp_Object window;
16638 int force;
16639 {
16640 int nwindows = 0;
16641
16642 while (!NILP (window))
16643 {
16644 struct window *w = XWINDOW (window);
16645
16646 if (WINDOWP (w->hchild))
16647 nwindows += redisplay_mode_lines (w->hchild, force);
16648 else if (WINDOWP (w->vchild))
16649 nwindows += redisplay_mode_lines (w->vchild, force);
16650 else if (force
16651 || FRAME_GARBAGED_P (XFRAME (w->frame))
16652 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16653 {
16654 struct text_pos lpoint;
16655 struct buffer *old = current_buffer;
16656
16657 /* Set the window's buffer for the mode line display. */
16658 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16659 set_buffer_internal_1 (XBUFFER (w->buffer));
16660
16661 /* Point refers normally to the selected window. For any
16662 other window, set up appropriate value. */
16663 if (!EQ (window, selected_window))
16664 {
16665 struct text_pos pt;
16666
16667 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16668 if (CHARPOS (pt) < BEGV)
16669 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16670 else if (CHARPOS (pt) > (ZV - 1))
16671 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16672 else
16673 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16674 }
16675
16676 /* Display mode lines. */
16677 clear_glyph_matrix (w->desired_matrix);
16678 if (display_mode_lines (w))
16679 {
16680 ++nwindows;
16681 w->must_be_updated_p = 1;
16682 }
16683
16684 /* Restore old settings. */
16685 set_buffer_internal_1 (old);
16686 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16687 }
16688
16689 window = w->next;
16690 }
16691
16692 return nwindows;
16693 }
16694
16695
16696 /* Display the mode and/or top line of window W. Value is the number
16697 of mode lines displayed. */
16698
16699 static int
16700 display_mode_lines (w)
16701 struct window *w;
16702 {
16703 Lisp_Object old_selected_window, old_selected_frame;
16704 int n = 0;
16705
16706 old_selected_frame = selected_frame;
16707 selected_frame = w->frame;
16708 old_selected_window = selected_window;
16709 XSETWINDOW (selected_window, w);
16710
16711 /* These will be set while the mode line specs are processed. */
16712 line_number_displayed = 0;
16713 w->column_number_displayed = Qnil;
16714
16715 if (WINDOW_WANTS_MODELINE_P (w))
16716 {
16717 struct window *sel_w = XWINDOW (old_selected_window);
16718
16719 /* Select mode line face based on the real selected window. */
16720 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16721 current_buffer->mode_line_format);
16722 ++n;
16723 }
16724
16725 if (WINDOW_WANTS_HEADER_LINE_P (w))
16726 {
16727 display_mode_line (w, HEADER_LINE_FACE_ID,
16728 current_buffer->header_line_format);
16729 ++n;
16730 }
16731
16732 selected_frame = old_selected_frame;
16733 selected_window = old_selected_window;
16734 return n;
16735 }
16736
16737
16738 /* Display mode or top line of window W. FACE_ID specifies which line
16739 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16740 FORMAT is the mode line format to display. Value is the pixel
16741 height of the mode line displayed. */
16742
16743 static int
16744 display_mode_line (w, face_id, format)
16745 struct window *w;
16746 enum face_id face_id;
16747 Lisp_Object format;
16748 {
16749 struct it it;
16750 struct face *face;
16751 int count = SPECPDL_INDEX ();
16752
16753 init_iterator (&it, w, -1, -1, NULL, face_id);
16754 /* Don't extend on a previously drawn mode-line.
16755 This may happen if called from pos_visible_p. */
16756 it.glyph_row->enabled_p = 0;
16757 prepare_desired_row (it.glyph_row);
16758
16759 it.glyph_row->mode_line_p = 1;
16760
16761 if (! mode_line_inverse_video)
16762 /* Force the mode-line to be displayed in the default face. */
16763 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16764
16765 record_unwind_protect (unwind_format_mode_line,
16766 format_mode_line_unwind_data (NULL, 0));
16767
16768 mode_line_target = MODE_LINE_DISPLAY;
16769
16770 /* Temporarily make frame's keyboard the current kboard so that
16771 kboard-local variables in the mode_line_format will get the right
16772 values. */
16773 push_kboard (FRAME_KBOARD (it.f));
16774 record_unwind_save_match_data ();
16775 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16776 pop_kboard ();
16777
16778 unbind_to (count, Qnil);
16779
16780 /* Fill up with spaces. */
16781 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16782
16783 compute_line_metrics (&it);
16784 it.glyph_row->full_width_p = 1;
16785 it.glyph_row->continued_p = 0;
16786 it.glyph_row->truncated_on_left_p = 0;
16787 it.glyph_row->truncated_on_right_p = 0;
16788
16789 /* Make a 3D mode-line have a shadow at its right end. */
16790 face = FACE_FROM_ID (it.f, face_id);
16791 extend_face_to_end_of_line (&it);
16792 if (face->box != FACE_NO_BOX)
16793 {
16794 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16795 + it.glyph_row->used[TEXT_AREA] - 1);
16796 last->right_box_line_p = 1;
16797 }
16798
16799 return it.glyph_row->height;
16800 }
16801
16802 /* Move element ELT in LIST to the front of LIST.
16803 Return the updated list. */
16804
16805 static Lisp_Object
16806 move_elt_to_front (elt, list)
16807 Lisp_Object elt, list;
16808 {
16809 register Lisp_Object tail, prev;
16810 register Lisp_Object tem;
16811
16812 tail = list;
16813 prev = Qnil;
16814 while (CONSP (tail))
16815 {
16816 tem = XCAR (tail);
16817
16818 if (EQ (elt, tem))
16819 {
16820 /* Splice out the link TAIL. */
16821 if (NILP (prev))
16822 list = XCDR (tail);
16823 else
16824 Fsetcdr (prev, XCDR (tail));
16825
16826 /* Now make it the first. */
16827 Fsetcdr (tail, list);
16828 return tail;
16829 }
16830 else
16831 prev = tail;
16832 tail = XCDR (tail);
16833 QUIT;
16834 }
16835
16836 /* Not found--return unchanged LIST. */
16837 return list;
16838 }
16839
16840 /* Contribute ELT to the mode line for window IT->w. How it
16841 translates into text depends on its data type.
16842
16843 IT describes the display environment in which we display, as usual.
16844
16845 DEPTH is the depth in recursion. It is used to prevent
16846 infinite recursion here.
16847
16848 FIELD_WIDTH is the number of characters the display of ELT should
16849 occupy in the mode line, and PRECISION is the maximum number of
16850 characters to display from ELT's representation. See
16851 display_string for details.
16852
16853 Returns the hpos of the end of the text generated by ELT.
16854
16855 PROPS is a property list to add to any string we encounter.
16856
16857 If RISKY is nonzero, remove (disregard) any properties in any string
16858 we encounter, and ignore :eval and :propertize.
16859
16860 The global variable `mode_line_target' determines whether the
16861 output is passed to `store_mode_line_noprop',
16862 `store_mode_line_string', or `display_string'. */
16863
16864 static int
16865 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16866 struct it *it;
16867 int depth;
16868 int field_width, precision;
16869 Lisp_Object elt, props;
16870 int risky;
16871 {
16872 int n = 0, field, prec;
16873 int literal = 0;
16874
16875 tail_recurse:
16876 if (depth > 100)
16877 elt = build_string ("*too-deep*");
16878
16879 depth++;
16880
16881 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16882 {
16883 case Lisp_String:
16884 {
16885 /* A string: output it and check for %-constructs within it. */
16886 unsigned char c;
16887 int offset = 0;
16888
16889 if (SCHARS (elt) > 0
16890 && (!NILP (props) || risky))
16891 {
16892 Lisp_Object oprops, aelt;
16893 oprops = Ftext_properties_at (make_number (0), elt);
16894
16895 /* If the starting string's properties are not what
16896 we want, translate the string. Also, if the string
16897 is risky, do that anyway. */
16898
16899 if (NILP (Fequal (props, oprops)) || risky)
16900 {
16901 /* If the starting string has properties,
16902 merge the specified ones onto the existing ones. */
16903 if (! NILP (oprops) && !risky)
16904 {
16905 Lisp_Object tem;
16906
16907 oprops = Fcopy_sequence (oprops);
16908 tem = props;
16909 while (CONSP (tem))
16910 {
16911 oprops = Fplist_put (oprops, XCAR (tem),
16912 XCAR (XCDR (tem)));
16913 tem = XCDR (XCDR (tem));
16914 }
16915 props = oprops;
16916 }
16917
16918 aelt = Fassoc (elt, mode_line_proptrans_alist);
16919 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16920 {
16921 /* AELT is what we want. Move it to the front
16922 without consing. */
16923 elt = XCAR (aelt);
16924 mode_line_proptrans_alist
16925 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16926 }
16927 else
16928 {
16929 Lisp_Object tem;
16930
16931 /* If AELT has the wrong props, it is useless.
16932 so get rid of it. */
16933 if (! NILP (aelt))
16934 mode_line_proptrans_alist
16935 = Fdelq (aelt, mode_line_proptrans_alist);
16936
16937 elt = Fcopy_sequence (elt);
16938 Fset_text_properties (make_number (0), Flength (elt),
16939 props, elt);
16940 /* Add this item to mode_line_proptrans_alist. */
16941 mode_line_proptrans_alist
16942 = Fcons (Fcons (elt, props),
16943 mode_line_proptrans_alist);
16944 /* Truncate mode_line_proptrans_alist
16945 to at most 50 elements. */
16946 tem = Fnthcdr (make_number (50),
16947 mode_line_proptrans_alist);
16948 if (! NILP (tem))
16949 XSETCDR (tem, Qnil);
16950 }
16951 }
16952 }
16953
16954 offset = 0;
16955
16956 if (literal)
16957 {
16958 prec = precision - n;
16959 switch (mode_line_target)
16960 {
16961 case MODE_LINE_NOPROP:
16962 case MODE_LINE_TITLE:
16963 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16964 break;
16965 case MODE_LINE_STRING:
16966 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16967 break;
16968 case MODE_LINE_DISPLAY:
16969 n += display_string (NULL, elt, Qnil, 0, 0, it,
16970 0, prec, 0, STRING_MULTIBYTE (elt));
16971 break;
16972 }
16973
16974 break;
16975 }
16976
16977 /* Handle the non-literal case. */
16978
16979 while ((precision <= 0 || n < precision)
16980 && SREF (elt, offset) != 0
16981 && (mode_line_target != MODE_LINE_DISPLAY
16982 || it->current_x < it->last_visible_x))
16983 {
16984 int last_offset = offset;
16985
16986 /* Advance to end of string or next format specifier. */
16987 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16988 ;
16989
16990 if (offset - 1 != last_offset)
16991 {
16992 int nchars, nbytes;
16993
16994 /* Output to end of string or up to '%'. Field width
16995 is length of string. Don't output more than
16996 PRECISION allows us. */
16997 offset--;
16998
16999 prec = c_string_width (SDATA (elt) + last_offset,
17000 offset - last_offset, precision - n,
17001 &nchars, &nbytes);
17002
17003 switch (mode_line_target)
17004 {
17005 case MODE_LINE_NOPROP:
17006 case MODE_LINE_TITLE:
17007 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17008 break;
17009 case MODE_LINE_STRING:
17010 {
17011 int bytepos = last_offset;
17012 int charpos = string_byte_to_char (elt, bytepos);
17013 int endpos = (precision <= 0
17014 ? string_byte_to_char (elt, offset)
17015 : charpos + nchars);
17016
17017 n += store_mode_line_string (NULL,
17018 Fsubstring (elt, make_number (charpos),
17019 make_number (endpos)),
17020 0, 0, 0, Qnil);
17021 }
17022 break;
17023 case MODE_LINE_DISPLAY:
17024 {
17025 int bytepos = last_offset;
17026 int charpos = string_byte_to_char (elt, bytepos);
17027
17028 if (precision <= 0)
17029 nchars = string_byte_to_char (elt, offset) - charpos;
17030 n += display_string (NULL, elt, Qnil, 0, charpos,
17031 it, 0, nchars, 0,
17032 STRING_MULTIBYTE (elt));
17033 }
17034 break;
17035 }
17036 }
17037 else /* c == '%' */
17038 {
17039 int percent_position = offset;
17040
17041 /* Get the specified minimum width. Zero means
17042 don't pad. */
17043 field = 0;
17044 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17045 field = field * 10 + c - '0';
17046
17047 /* Don't pad beyond the total padding allowed. */
17048 if (field_width - n > 0 && field > field_width - n)
17049 field = field_width - n;
17050
17051 /* Note that either PRECISION <= 0 or N < PRECISION. */
17052 prec = precision - n;
17053
17054 if (c == 'M')
17055 n += display_mode_element (it, depth, field, prec,
17056 Vglobal_mode_string, props,
17057 risky);
17058 else if (c != 0)
17059 {
17060 int multibyte;
17061 int bytepos, charpos;
17062 unsigned char *spec;
17063
17064 bytepos = percent_position;
17065 charpos = (STRING_MULTIBYTE (elt)
17066 ? string_byte_to_char (elt, bytepos)
17067 : bytepos);
17068
17069 spec
17070 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17071
17072 switch (mode_line_target)
17073 {
17074 case MODE_LINE_NOPROP:
17075 case MODE_LINE_TITLE:
17076 n += store_mode_line_noprop (spec, field, prec);
17077 break;
17078 case MODE_LINE_STRING:
17079 {
17080 int len = strlen (spec);
17081 Lisp_Object tem = make_string (spec, len);
17082 props = Ftext_properties_at (make_number (charpos), elt);
17083 /* Should only keep face property in props */
17084 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17085 }
17086 break;
17087 case MODE_LINE_DISPLAY:
17088 {
17089 int nglyphs_before, nwritten;
17090
17091 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17092 nwritten = display_string (spec, Qnil, elt,
17093 charpos, 0, it,
17094 field, prec, 0,
17095 multibyte);
17096
17097 /* Assign to the glyphs written above the
17098 string where the `%x' came from, position
17099 of the `%'. */
17100 if (nwritten > 0)
17101 {
17102 struct glyph *glyph
17103 = (it->glyph_row->glyphs[TEXT_AREA]
17104 + nglyphs_before);
17105 int i;
17106
17107 for (i = 0; i < nwritten; ++i)
17108 {
17109 glyph[i].object = elt;
17110 glyph[i].charpos = charpos;
17111 }
17112
17113 n += nwritten;
17114 }
17115 }
17116 break;
17117 }
17118 }
17119 else /* c == 0 */
17120 break;
17121 }
17122 }
17123 }
17124 break;
17125
17126 case Lisp_Symbol:
17127 /* A symbol: process the value of the symbol recursively
17128 as if it appeared here directly. Avoid error if symbol void.
17129 Special case: if value of symbol is a string, output the string
17130 literally. */
17131 {
17132 register Lisp_Object tem;
17133
17134 /* If the variable is not marked as risky to set
17135 then its contents are risky to use. */
17136 if (NILP (Fget (elt, Qrisky_local_variable)))
17137 risky = 1;
17138
17139 tem = Fboundp (elt);
17140 if (!NILP (tem))
17141 {
17142 tem = Fsymbol_value (elt);
17143 /* If value is a string, output that string literally:
17144 don't check for % within it. */
17145 if (STRINGP (tem))
17146 literal = 1;
17147
17148 if (!EQ (tem, elt))
17149 {
17150 /* Give up right away for nil or t. */
17151 elt = tem;
17152 goto tail_recurse;
17153 }
17154 }
17155 }
17156 break;
17157
17158 case Lisp_Cons:
17159 {
17160 register Lisp_Object car, tem;
17161
17162 /* A cons cell: five distinct cases.
17163 If first element is :eval or :propertize, do something special.
17164 If first element is a string or a cons, process all the elements
17165 and effectively concatenate them.
17166 If first element is a negative number, truncate displaying cdr to
17167 at most that many characters. If positive, pad (with spaces)
17168 to at least that many characters.
17169 If first element is a symbol, process the cadr or caddr recursively
17170 according to whether the symbol's value is non-nil or nil. */
17171 car = XCAR (elt);
17172 if (EQ (car, QCeval))
17173 {
17174 /* An element of the form (:eval FORM) means evaluate FORM
17175 and use the result as mode line elements. */
17176
17177 if (risky)
17178 break;
17179
17180 if (CONSP (XCDR (elt)))
17181 {
17182 Lisp_Object spec;
17183 spec = safe_eval (XCAR (XCDR (elt)));
17184 n += display_mode_element (it, depth, field_width - n,
17185 precision - n, spec, props,
17186 risky);
17187 }
17188 }
17189 else if (EQ (car, QCpropertize))
17190 {
17191 /* An element of the form (:propertize ELT PROPS...)
17192 means display ELT but applying properties PROPS. */
17193
17194 if (risky)
17195 break;
17196
17197 if (CONSP (XCDR (elt)))
17198 n += display_mode_element (it, depth, field_width - n,
17199 precision - n, XCAR (XCDR (elt)),
17200 XCDR (XCDR (elt)), risky);
17201 }
17202 else if (SYMBOLP (car))
17203 {
17204 tem = Fboundp (car);
17205 elt = XCDR (elt);
17206 if (!CONSP (elt))
17207 goto invalid;
17208 /* elt is now the cdr, and we know it is a cons cell.
17209 Use its car if CAR has a non-nil value. */
17210 if (!NILP (tem))
17211 {
17212 tem = Fsymbol_value (car);
17213 if (!NILP (tem))
17214 {
17215 elt = XCAR (elt);
17216 goto tail_recurse;
17217 }
17218 }
17219 /* Symbol's value is nil (or symbol is unbound)
17220 Get the cddr of the original list
17221 and if possible find the caddr and use that. */
17222 elt = XCDR (elt);
17223 if (NILP (elt))
17224 break;
17225 else if (!CONSP (elt))
17226 goto invalid;
17227 elt = XCAR (elt);
17228 goto tail_recurse;
17229 }
17230 else if (INTEGERP (car))
17231 {
17232 register int lim = XINT (car);
17233 elt = XCDR (elt);
17234 if (lim < 0)
17235 {
17236 /* Negative int means reduce maximum width. */
17237 if (precision <= 0)
17238 precision = -lim;
17239 else
17240 precision = min (precision, -lim);
17241 }
17242 else if (lim > 0)
17243 {
17244 /* Padding specified. Don't let it be more than
17245 current maximum. */
17246 if (precision > 0)
17247 lim = min (precision, lim);
17248
17249 /* If that's more padding than already wanted, queue it.
17250 But don't reduce padding already specified even if
17251 that is beyond the current truncation point. */
17252 field_width = max (lim, field_width);
17253 }
17254 goto tail_recurse;
17255 }
17256 else if (STRINGP (car) || CONSP (car))
17257 {
17258 register int limit = 50;
17259 /* Limit is to protect against circular lists. */
17260 while (CONSP (elt)
17261 && --limit > 0
17262 && (precision <= 0 || n < precision))
17263 {
17264 n += display_mode_element (it, depth,
17265 /* Do padding only after the last
17266 element in the list. */
17267 (! CONSP (XCDR (elt))
17268 ? field_width - n
17269 : 0),
17270 precision - n, XCAR (elt),
17271 props, risky);
17272 elt = XCDR (elt);
17273 }
17274 }
17275 }
17276 break;
17277
17278 default:
17279 invalid:
17280 elt = build_string ("*invalid*");
17281 goto tail_recurse;
17282 }
17283
17284 /* Pad to FIELD_WIDTH. */
17285 if (field_width > 0 && n < field_width)
17286 {
17287 switch (mode_line_target)
17288 {
17289 case MODE_LINE_NOPROP:
17290 case MODE_LINE_TITLE:
17291 n += store_mode_line_noprop ("", field_width - n, 0);
17292 break;
17293 case MODE_LINE_STRING:
17294 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17295 break;
17296 case MODE_LINE_DISPLAY:
17297 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17298 0, 0, 0);
17299 break;
17300 }
17301 }
17302
17303 return n;
17304 }
17305
17306 /* Store a mode-line string element in mode_line_string_list.
17307
17308 If STRING is non-null, display that C string. Otherwise, the Lisp
17309 string LISP_STRING is displayed.
17310
17311 FIELD_WIDTH is the minimum number of output glyphs to produce.
17312 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17313 with spaces. FIELD_WIDTH <= 0 means don't pad.
17314
17315 PRECISION is the maximum number of characters to output from
17316 STRING. PRECISION <= 0 means don't truncate the string.
17317
17318 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17319 properties to the string.
17320
17321 PROPS are the properties to add to the string.
17322 The mode_line_string_face face property is always added to the string.
17323 */
17324
17325 static int
17326 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17327 char *string;
17328 Lisp_Object lisp_string;
17329 int copy_string;
17330 int field_width;
17331 int precision;
17332 Lisp_Object props;
17333 {
17334 int len;
17335 int n = 0;
17336
17337 if (string != NULL)
17338 {
17339 len = strlen (string);
17340 if (precision > 0 && len > precision)
17341 len = precision;
17342 lisp_string = make_string (string, len);
17343 if (NILP (props))
17344 props = mode_line_string_face_prop;
17345 else if (!NILP (mode_line_string_face))
17346 {
17347 Lisp_Object face = Fplist_get (props, Qface);
17348 props = Fcopy_sequence (props);
17349 if (NILP (face))
17350 face = mode_line_string_face;
17351 else
17352 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17353 props = Fplist_put (props, Qface, face);
17354 }
17355 Fadd_text_properties (make_number (0), make_number (len),
17356 props, lisp_string);
17357 }
17358 else
17359 {
17360 len = XFASTINT (Flength (lisp_string));
17361 if (precision > 0 && len > precision)
17362 {
17363 len = precision;
17364 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17365 precision = -1;
17366 }
17367 if (!NILP (mode_line_string_face))
17368 {
17369 Lisp_Object face;
17370 if (NILP (props))
17371 props = Ftext_properties_at (make_number (0), lisp_string);
17372 face = Fplist_get (props, Qface);
17373 if (NILP (face))
17374 face = mode_line_string_face;
17375 else
17376 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17377 props = Fcons (Qface, Fcons (face, Qnil));
17378 if (copy_string)
17379 lisp_string = Fcopy_sequence (lisp_string);
17380 }
17381 if (!NILP (props))
17382 Fadd_text_properties (make_number (0), make_number (len),
17383 props, lisp_string);
17384 }
17385
17386 if (len > 0)
17387 {
17388 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17389 n += len;
17390 }
17391
17392 if (field_width > len)
17393 {
17394 field_width -= len;
17395 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17396 if (!NILP (props))
17397 Fadd_text_properties (make_number (0), make_number (field_width),
17398 props, lisp_string);
17399 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17400 n += field_width;
17401 }
17402
17403 return n;
17404 }
17405
17406
17407 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17408 1, 4, 0,
17409 doc: /* Format a string out of a mode line format specification.
17410 First arg FORMAT specifies the mode line format (see `mode-line-format'
17411 for details) to use.
17412
17413 Optional second arg FACE specifies the face property to put
17414 on all characters for which no face is specified.
17415 The value t means whatever face the window's mode line currently uses
17416 \(either `mode-line' or `mode-line-inactive', depending).
17417 A value of nil means the default is no face property.
17418 If FACE is an integer, the value string has no text properties.
17419
17420 Optional third and fourth args WINDOW and BUFFER specify the window
17421 and buffer to use as the context for the formatting (defaults
17422 are the selected window and the window's buffer). */)
17423 (format, face, window, buffer)
17424 Lisp_Object format, face, window, buffer;
17425 {
17426 struct it it;
17427 int len;
17428 struct window *w;
17429 struct buffer *old_buffer = NULL;
17430 int face_id = -1;
17431 int no_props = INTEGERP (face);
17432 int count = SPECPDL_INDEX ();
17433 Lisp_Object str;
17434 int string_start = 0;
17435
17436 if (NILP (window))
17437 window = selected_window;
17438 CHECK_WINDOW (window);
17439 w = XWINDOW (window);
17440
17441 if (NILP (buffer))
17442 buffer = w->buffer;
17443 CHECK_BUFFER (buffer);
17444
17445 /* Make formatting the modeline a non-op when noninteractive, otherwise
17446 there will be problems later caused by a partially initialized frame. */
17447 if (NILP (format) || noninteractive)
17448 return empty_unibyte_string;
17449
17450 if (no_props)
17451 face = Qnil;
17452
17453 if (!NILP (face))
17454 {
17455 if (EQ (face, Qt))
17456 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17457 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17458 }
17459
17460 if (face_id < 0)
17461 face_id = DEFAULT_FACE_ID;
17462
17463 if (XBUFFER (buffer) != current_buffer)
17464 old_buffer = current_buffer;
17465
17466 /* Save things including mode_line_proptrans_alist,
17467 and set that to nil so that we don't alter the outer value. */
17468 record_unwind_protect (unwind_format_mode_line,
17469 format_mode_line_unwind_data (old_buffer, 1));
17470 mode_line_proptrans_alist = Qnil;
17471
17472 if (old_buffer)
17473 set_buffer_internal_1 (XBUFFER (buffer));
17474
17475 init_iterator (&it, w, -1, -1, NULL, face_id);
17476
17477 if (no_props)
17478 {
17479 mode_line_target = MODE_LINE_NOPROP;
17480 mode_line_string_face_prop = Qnil;
17481 mode_line_string_list = Qnil;
17482 string_start = MODE_LINE_NOPROP_LEN (0);
17483 }
17484 else
17485 {
17486 mode_line_target = MODE_LINE_STRING;
17487 mode_line_string_list = Qnil;
17488 mode_line_string_face = face;
17489 mode_line_string_face_prop
17490 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17491 }
17492
17493 push_kboard (FRAME_KBOARD (it.f));
17494 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17495 pop_kboard ();
17496
17497 if (no_props)
17498 {
17499 len = MODE_LINE_NOPROP_LEN (string_start);
17500 str = make_string (mode_line_noprop_buf + string_start, len);
17501 }
17502 else
17503 {
17504 mode_line_string_list = Fnreverse (mode_line_string_list);
17505 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17506 empty_unibyte_string);
17507 }
17508
17509 unbind_to (count, Qnil);
17510 return str;
17511 }
17512
17513 /* Write a null-terminated, right justified decimal representation of
17514 the positive integer D to BUF using a minimal field width WIDTH. */
17515
17516 static void
17517 pint2str (buf, width, d)
17518 register char *buf;
17519 register int width;
17520 register int d;
17521 {
17522 register char *p = buf;
17523
17524 if (d <= 0)
17525 *p++ = '0';
17526 else
17527 {
17528 while (d > 0)
17529 {
17530 *p++ = d % 10 + '0';
17531 d /= 10;
17532 }
17533 }
17534
17535 for (width -= (int) (p - buf); width > 0; --width)
17536 *p++ = ' ';
17537 *p-- = '\0';
17538 while (p > buf)
17539 {
17540 d = *buf;
17541 *buf++ = *p;
17542 *p-- = d;
17543 }
17544 }
17545
17546 /* Write a null-terminated, right justified decimal and "human
17547 readable" representation of the nonnegative integer D to BUF using
17548 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17549
17550 static const char power_letter[] =
17551 {
17552 0, /* not used */
17553 'k', /* kilo */
17554 'M', /* mega */
17555 'G', /* giga */
17556 'T', /* tera */
17557 'P', /* peta */
17558 'E', /* exa */
17559 'Z', /* zetta */
17560 'Y' /* yotta */
17561 };
17562
17563 static void
17564 pint2hrstr (buf, width, d)
17565 char *buf;
17566 int width;
17567 int d;
17568 {
17569 /* We aim to represent the nonnegative integer D as
17570 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17571 int quotient = d;
17572 int remainder = 0;
17573 /* -1 means: do not use TENTHS. */
17574 int tenths = -1;
17575 int exponent = 0;
17576
17577 /* Length of QUOTIENT.TENTHS as a string. */
17578 int length;
17579
17580 char * psuffix;
17581 char * p;
17582
17583 if (1000 <= quotient)
17584 {
17585 /* Scale to the appropriate EXPONENT. */
17586 do
17587 {
17588 remainder = quotient % 1000;
17589 quotient /= 1000;
17590 exponent++;
17591 }
17592 while (1000 <= quotient);
17593
17594 /* Round to nearest and decide whether to use TENTHS or not. */
17595 if (quotient <= 9)
17596 {
17597 tenths = remainder / 100;
17598 if (50 <= remainder % 100)
17599 {
17600 if (tenths < 9)
17601 tenths++;
17602 else
17603 {
17604 quotient++;
17605 if (quotient == 10)
17606 tenths = -1;
17607 else
17608 tenths = 0;
17609 }
17610 }
17611 }
17612 else
17613 if (500 <= remainder)
17614 {
17615 if (quotient < 999)
17616 quotient++;
17617 else
17618 {
17619 quotient = 1;
17620 exponent++;
17621 tenths = 0;
17622 }
17623 }
17624 }
17625
17626 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17627 if (tenths == -1 && quotient <= 99)
17628 if (quotient <= 9)
17629 length = 1;
17630 else
17631 length = 2;
17632 else
17633 length = 3;
17634 p = psuffix = buf + max (width, length);
17635
17636 /* Print EXPONENT. */
17637 if (exponent)
17638 *psuffix++ = power_letter[exponent];
17639 *psuffix = '\0';
17640
17641 /* Print TENTHS. */
17642 if (tenths >= 0)
17643 {
17644 *--p = '0' + tenths;
17645 *--p = '.';
17646 }
17647
17648 /* Print QUOTIENT. */
17649 do
17650 {
17651 int digit = quotient % 10;
17652 *--p = '0' + digit;
17653 }
17654 while ((quotient /= 10) != 0);
17655
17656 /* Print leading spaces. */
17657 while (buf < p)
17658 *--p = ' ';
17659 }
17660
17661 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17662 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17663 type of CODING_SYSTEM. Return updated pointer into BUF. */
17664
17665 static unsigned char invalid_eol_type[] = "(*invalid*)";
17666
17667 static char *
17668 decode_mode_spec_coding (coding_system, buf, eol_flag)
17669 Lisp_Object coding_system;
17670 register char *buf;
17671 int eol_flag;
17672 {
17673 Lisp_Object val;
17674 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17675 const unsigned char *eol_str;
17676 int eol_str_len;
17677 /* The EOL conversion we are using. */
17678 Lisp_Object eoltype;
17679
17680 val = Fget (coding_system, Qcoding_system);
17681 eoltype = Qnil;
17682
17683 if (!VECTORP (val)) /* Not yet decided. */
17684 {
17685 if (multibyte)
17686 *buf++ = '-';
17687 if (eol_flag)
17688 eoltype = eol_mnemonic_undecided;
17689 /* Don't mention EOL conversion if it isn't decided. */
17690 }
17691 else
17692 {
17693 Lisp_Object eolvalue;
17694
17695 eolvalue = Fget (coding_system, Qeol_type);
17696
17697 if (multibyte)
17698 *buf++ = XFASTINT (AREF (val, 1));
17699
17700 if (eol_flag)
17701 {
17702 /* The EOL conversion that is normal on this system. */
17703
17704 if (NILP (eolvalue)) /* Not yet decided. */
17705 eoltype = eol_mnemonic_undecided;
17706 else if (VECTORP (eolvalue)) /* Not yet decided. */
17707 eoltype = eol_mnemonic_undecided;
17708 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17709 eoltype = (XFASTINT (eolvalue) == 0
17710 ? eol_mnemonic_unix
17711 : (XFASTINT (eolvalue) == 1
17712 ? eol_mnemonic_dos : eol_mnemonic_mac));
17713 }
17714 }
17715
17716 if (eol_flag)
17717 {
17718 /* Mention the EOL conversion if it is not the usual one. */
17719 if (STRINGP (eoltype))
17720 {
17721 eol_str = SDATA (eoltype);
17722 eol_str_len = SBYTES (eoltype);
17723 }
17724 else if (INTEGERP (eoltype)
17725 && CHAR_VALID_P (XINT (eoltype), 0))
17726 {
17727 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17728 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17729 eol_str = tmp;
17730 }
17731 else
17732 {
17733 eol_str = invalid_eol_type;
17734 eol_str_len = sizeof (invalid_eol_type) - 1;
17735 }
17736 bcopy (eol_str, buf, eol_str_len);
17737 buf += eol_str_len;
17738 }
17739
17740 return buf;
17741 }
17742
17743 /* Return a string for the output of a mode line %-spec for window W,
17744 generated by character C. PRECISION >= 0 means don't return a
17745 string longer than that value. FIELD_WIDTH > 0 means pad the
17746 string returned with spaces to that value. Return 1 in *MULTIBYTE
17747 if the result is multibyte text.
17748
17749 Note we operate on the current buffer for most purposes,
17750 the exception being w->base_line_pos. */
17751
17752 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17753
17754 static char *
17755 decode_mode_spec (w, c, field_width, precision, multibyte)
17756 struct window *w;
17757 register int c;
17758 int field_width, precision;
17759 int *multibyte;
17760 {
17761 Lisp_Object obj;
17762 struct frame *f = XFRAME (WINDOW_FRAME (w));
17763 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17764 struct buffer *b = current_buffer;
17765
17766 obj = Qnil;
17767 *multibyte = 0;
17768
17769 switch (c)
17770 {
17771 case '*':
17772 if (!NILP (b->read_only))
17773 return "%";
17774 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17775 return "*";
17776 return "-";
17777
17778 case '+':
17779 /* This differs from %* only for a modified read-only buffer. */
17780 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17781 return "*";
17782 if (!NILP (b->read_only))
17783 return "%";
17784 return "-";
17785
17786 case '&':
17787 /* This differs from %* in ignoring read-only-ness. */
17788 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17789 return "*";
17790 return "-";
17791
17792 case '%':
17793 return "%";
17794
17795 case '[':
17796 {
17797 int i;
17798 char *p;
17799
17800 if (command_loop_level > 5)
17801 return "[[[... ";
17802 p = decode_mode_spec_buf;
17803 for (i = 0; i < command_loop_level; i++)
17804 *p++ = '[';
17805 *p = 0;
17806 return decode_mode_spec_buf;
17807 }
17808
17809 case ']':
17810 {
17811 int i;
17812 char *p;
17813
17814 if (command_loop_level > 5)
17815 return " ...]]]";
17816 p = decode_mode_spec_buf;
17817 for (i = 0; i < command_loop_level; i++)
17818 *p++ = ']';
17819 *p = 0;
17820 return decode_mode_spec_buf;
17821 }
17822
17823 case '-':
17824 {
17825 register int i;
17826
17827 /* Let lots_of_dashes be a string of infinite length. */
17828 if (mode_line_target == MODE_LINE_NOPROP ||
17829 mode_line_target == MODE_LINE_STRING)
17830 return "--";
17831 if (field_width <= 0
17832 || field_width > sizeof (lots_of_dashes))
17833 {
17834 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17835 decode_mode_spec_buf[i] = '-';
17836 decode_mode_spec_buf[i] = '\0';
17837 return decode_mode_spec_buf;
17838 }
17839 else
17840 return lots_of_dashes;
17841 }
17842
17843 case 'b':
17844 obj = b->name;
17845 break;
17846
17847 case 'c':
17848 /* %c and %l are ignored in `frame-title-format'.
17849 (In redisplay_internal, the frame title is drawn _before_ the
17850 windows are updated, so the stuff which depends on actual
17851 window contents (such as %l) may fail to render properly, or
17852 even crash emacs.) */
17853 if (mode_line_target == MODE_LINE_TITLE)
17854 return "";
17855 else
17856 {
17857 int col = (int) current_column (); /* iftc */
17858 w->column_number_displayed = make_number (col);
17859 pint2str (decode_mode_spec_buf, field_width, col);
17860 return decode_mode_spec_buf;
17861 }
17862
17863 case 'e':
17864 #ifndef SYSTEM_MALLOC
17865 {
17866 if (NILP (Vmemory_full))
17867 return "";
17868 else
17869 return "!MEM FULL! ";
17870 }
17871 #else
17872 return "";
17873 #endif
17874
17875 case 'F':
17876 /* %F displays the frame name. */
17877 if (!NILP (f->title))
17878 return (char *) SDATA (f->title);
17879 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17880 return (char *) SDATA (f->name);
17881 return "Emacs";
17882
17883 case 'f':
17884 obj = b->filename;
17885 break;
17886
17887 case 'i':
17888 {
17889 int size = ZV - BEGV;
17890 pint2str (decode_mode_spec_buf, field_width, size);
17891 return decode_mode_spec_buf;
17892 }
17893
17894 case 'I':
17895 {
17896 int size = ZV - BEGV;
17897 pint2hrstr (decode_mode_spec_buf, field_width, size);
17898 return decode_mode_spec_buf;
17899 }
17900
17901 case 'l':
17902 {
17903 int startpos, startpos_byte, line, linepos, linepos_byte;
17904 int topline, nlines, junk, height;
17905
17906 /* %c and %l are ignored in `frame-title-format'. */
17907 if (mode_line_target == MODE_LINE_TITLE)
17908 return "";
17909
17910 startpos = XMARKER (w->start)->charpos;
17911 startpos_byte = marker_byte_position (w->start);
17912 height = WINDOW_TOTAL_LINES (w);
17913
17914 /* If we decided that this buffer isn't suitable for line numbers,
17915 don't forget that too fast. */
17916 if (EQ (w->base_line_pos, w->buffer))
17917 goto no_value;
17918 /* But do forget it, if the window shows a different buffer now. */
17919 else if (BUFFERP (w->base_line_pos))
17920 w->base_line_pos = Qnil;
17921
17922 /* If the buffer is very big, don't waste time. */
17923 if (INTEGERP (Vline_number_display_limit)
17924 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17925 {
17926 w->base_line_pos = Qnil;
17927 w->base_line_number = Qnil;
17928 goto no_value;
17929 }
17930
17931 if (!NILP (w->base_line_number)
17932 && !NILP (w->base_line_pos)
17933 && XFASTINT (w->base_line_pos) <= startpos)
17934 {
17935 line = XFASTINT (w->base_line_number);
17936 linepos = XFASTINT (w->base_line_pos);
17937 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17938 }
17939 else
17940 {
17941 line = 1;
17942 linepos = BUF_BEGV (b);
17943 linepos_byte = BUF_BEGV_BYTE (b);
17944 }
17945
17946 /* Count lines from base line to window start position. */
17947 nlines = display_count_lines (linepos, linepos_byte,
17948 startpos_byte,
17949 startpos, &junk);
17950
17951 topline = nlines + line;
17952
17953 /* Determine a new base line, if the old one is too close
17954 or too far away, or if we did not have one.
17955 "Too close" means it's plausible a scroll-down would
17956 go back past it. */
17957 if (startpos == BUF_BEGV (b))
17958 {
17959 w->base_line_number = make_number (topline);
17960 w->base_line_pos = make_number (BUF_BEGV (b));
17961 }
17962 else if (nlines < height + 25 || nlines > height * 3 + 50
17963 || linepos == BUF_BEGV (b))
17964 {
17965 int limit = BUF_BEGV (b);
17966 int limit_byte = BUF_BEGV_BYTE (b);
17967 int position;
17968 int distance = (height * 2 + 30) * line_number_display_limit_width;
17969
17970 if (startpos - distance > limit)
17971 {
17972 limit = startpos - distance;
17973 limit_byte = CHAR_TO_BYTE (limit);
17974 }
17975
17976 nlines = display_count_lines (startpos, startpos_byte,
17977 limit_byte,
17978 - (height * 2 + 30),
17979 &position);
17980 /* If we couldn't find the lines we wanted within
17981 line_number_display_limit_width chars per line,
17982 give up on line numbers for this window. */
17983 if (position == limit_byte && limit == startpos - distance)
17984 {
17985 w->base_line_pos = w->buffer;
17986 w->base_line_number = Qnil;
17987 goto no_value;
17988 }
17989
17990 w->base_line_number = make_number (topline - nlines);
17991 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17992 }
17993
17994 /* Now count lines from the start pos to point. */
17995 nlines = display_count_lines (startpos, startpos_byte,
17996 PT_BYTE, PT, &junk);
17997
17998 /* Record that we did display the line number. */
17999 line_number_displayed = 1;
18000
18001 /* Make the string to show. */
18002 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18003 return decode_mode_spec_buf;
18004 no_value:
18005 {
18006 char* p = decode_mode_spec_buf;
18007 int pad = field_width - 2;
18008 while (pad-- > 0)
18009 *p++ = ' ';
18010 *p++ = '?';
18011 *p++ = '?';
18012 *p = '\0';
18013 return decode_mode_spec_buf;
18014 }
18015 }
18016 break;
18017
18018 case 'm':
18019 obj = b->mode_name;
18020 break;
18021
18022 case 'n':
18023 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18024 return " Narrow";
18025 break;
18026
18027 case 'p':
18028 {
18029 int pos = marker_position (w->start);
18030 int total = BUF_ZV (b) - BUF_BEGV (b);
18031
18032 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18033 {
18034 if (pos <= BUF_BEGV (b))
18035 return "All";
18036 else
18037 return "Bottom";
18038 }
18039 else if (pos <= BUF_BEGV (b))
18040 return "Top";
18041 else
18042 {
18043 if (total > 1000000)
18044 /* Do it differently for a large value, to avoid overflow. */
18045 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18046 else
18047 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18048 /* We can't normally display a 3-digit number,
18049 so get us a 2-digit number that is close. */
18050 if (total == 100)
18051 total = 99;
18052 sprintf (decode_mode_spec_buf, "%2d%%", total);
18053 return decode_mode_spec_buf;
18054 }
18055 }
18056
18057 /* Display percentage of size above the bottom of the screen. */
18058 case 'P':
18059 {
18060 int toppos = marker_position (w->start);
18061 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18062 int total = BUF_ZV (b) - BUF_BEGV (b);
18063
18064 if (botpos >= BUF_ZV (b))
18065 {
18066 if (toppos <= BUF_BEGV (b))
18067 return "All";
18068 else
18069 return "Bottom";
18070 }
18071 else
18072 {
18073 if (total > 1000000)
18074 /* Do it differently for a large value, to avoid overflow. */
18075 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18076 else
18077 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18078 /* We can't normally display a 3-digit number,
18079 so get us a 2-digit number that is close. */
18080 if (total == 100)
18081 total = 99;
18082 if (toppos <= BUF_BEGV (b))
18083 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18084 else
18085 sprintf (decode_mode_spec_buf, "%2d%%", total);
18086 return decode_mode_spec_buf;
18087 }
18088 }
18089
18090 case 's':
18091 /* status of process */
18092 obj = Fget_buffer_process (Fcurrent_buffer ());
18093 if (NILP (obj))
18094 return "no process";
18095 #ifdef subprocesses
18096 obj = Fsymbol_name (Fprocess_status (obj));
18097 #endif
18098 break;
18099
18100 case '@':
18101 {
18102 Lisp_Object val;
18103 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18104 if (NILP (val))
18105 return "-";
18106 else
18107 return "@";
18108 }
18109
18110 case 't': /* indicate TEXT or BINARY */
18111 #ifdef MODE_LINE_BINARY_TEXT
18112 return MODE_LINE_BINARY_TEXT (b);
18113 #else
18114 return "T";
18115 #endif
18116
18117 case 'z':
18118 /* coding-system (not including end-of-line format) */
18119 case 'Z':
18120 /* coding-system (including end-of-line type) */
18121 {
18122 int eol_flag = (c == 'Z');
18123 char *p = decode_mode_spec_buf;
18124
18125 if (! FRAME_WINDOW_P (f))
18126 {
18127 /* No need to mention EOL here--the terminal never needs
18128 to do EOL conversion. */
18129 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
18130 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
18131 }
18132 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18133 p, eol_flag);
18134
18135 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18136 #ifdef subprocesses
18137 obj = Fget_buffer_process (Fcurrent_buffer ());
18138 if (PROCESSP (obj))
18139 {
18140 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18141 p, eol_flag);
18142 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18143 p, eol_flag);
18144 }
18145 #endif /* subprocesses */
18146 #endif /* 0 */
18147 *p = 0;
18148 return decode_mode_spec_buf;
18149 }
18150 }
18151
18152 if (STRINGP (obj))
18153 {
18154 *multibyte = STRING_MULTIBYTE (obj);
18155 return (char *) SDATA (obj);
18156 }
18157 else
18158 return "";
18159 }
18160
18161
18162 /* Count up to COUNT lines starting from START / START_BYTE.
18163 But don't go beyond LIMIT_BYTE.
18164 Return the number of lines thus found (always nonnegative).
18165
18166 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18167
18168 static int
18169 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18170 int start, start_byte, limit_byte, count;
18171 int *byte_pos_ptr;
18172 {
18173 register unsigned char *cursor;
18174 unsigned char *base;
18175
18176 register int ceiling;
18177 register unsigned char *ceiling_addr;
18178 int orig_count = count;
18179
18180 /* If we are not in selective display mode,
18181 check only for newlines. */
18182 int selective_display = (!NILP (current_buffer->selective_display)
18183 && !INTEGERP (current_buffer->selective_display));
18184
18185 if (count > 0)
18186 {
18187 while (start_byte < limit_byte)
18188 {
18189 ceiling = BUFFER_CEILING_OF (start_byte);
18190 ceiling = min (limit_byte - 1, ceiling);
18191 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18192 base = (cursor = BYTE_POS_ADDR (start_byte));
18193 while (1)
18194 {
18195 if (selective_display)
18196 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18197 ;
18198 else
18199 while (*cursor != '\n' && ++cursor != ceiling_addr)
18200 ;
18201
18202 if (cursor != ceiling_addr)
18203 {
18204 if (--count == 0)
18205 {
18206 start_byte += cursor - base + 1;
18207 *byte_pos_ptr = start_byte;
18208 return orig_count;
18209 }
18210 else
18211 if (++cursor == ceiling_addr)
18212 break;
18213 }
18214 else
18215 break;
18216 }
18217 start_byte += cursor - base;
18218 }
18219 }
18220 else
18221 {
18222 while (start_byte > limit_byte)
18223 {
18224 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18225 ceiling = max (limit_byte, ceiling);
18226 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18227 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18228 while (1)
18229 {
18230 if (selective_display)
18231 while (--cursor != ceiling_addr
18232 && *cursor != '\n' && *cursor != 015)
18233 ;
18234 else
18235 while (--cursor != ceiling_addr && *cursor != '\n')
18236 ;
18237
18238 if (cursor != ceiling_addr)
18239 {
18240 if (++count == 0)
18241 {
18242 start_byte += cursor - base + 1;
18243 *byte_pos_ptr = start_byte;
18244 /* When scanning backwards, we should
18245 not count the newline posterior to which we stop. */
18246 return - orig_count - 1;
18247 }
18248 }
18249 else
18250 break;
18251 }
18252 /* Here we add 1 to compensate for the last decrement
18253 of CURSOR, which took it past the valid range. */
18254 start_byte += cursor - base + 1;
18255 }
18256 }
18257
18258 *byte_pos_ptr = limit_byte;
18259
18260 if (count < 0)
18261 return - orig_count + count;
18262 return orig_count - count;
18263
18264 }
18265
18266
18267 \f
18268 /***********************************************************************
18269 Displaying strings
18270 ***********************************************************************/
18271
18272 /* Display a NUL-terminated string, starting with index START.
18273
18274 If STRING is non-null, display that C string. Otherwise, the Lisp
18275 string LISP_STRING is displayed.
18276
18277 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18278 FACE_STRING. Display STRING or LISP_STRING with the face at
18279 FACE_STRING_POS in FACE_STRING:
18280
18281 Display the string in the environment given by IT, but use the
18282 standard display table, temporarily.
18283
18284 FIELD_WIDTH is the minimum number of output glyphs to produce.
18285 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18286 with spaces. If STRING has more characters, more than FIELD_WIDTH
18287 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18288
18289 PRECISION is the maximum number of characters to output from
18290 STRING. PRECISION < 0 means don't truncate the string.
18291
18292 This is roughly equivalent to printf format specifiers:
18293
18294 FIELD_WIDTH PRECISION PRINTF
18295 ----------------------------------------
18296 -1 -1 %s
18297 -1 10 %.10s
18298 10 -1 %10s
18299 20 10 %20.10s
18300
18301 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18302 display them, and < 0 means obey the current buffer's value of
18303 enable_multibyte_characters.
18304
18305 Value is the number of columns displayed. */
18306
18307 static int
18308 display_string (string, lisp_string, face_string, face_string_pos,
18309 start, it, field_width, precision, max_x, multibyte)
18310 unsigned char *string;
18311 Lisp_Object lisp_string;
18312 Lisp_Object face_string;
18313 int face_string_pos;
18314 int start;
18315 struct it *it;
18316 int field_width, precision, max_x;
18317 int multibyte;
18318 {
18319 int hpos_at_start = it->hpos;
18320 int saved_face_id = it->face_id;
18321 struct glyph_row *row = it->glyph_row;
18322
18323 /* Initialize the iterator IT for iteration over STRING beginning
18324 with index START. */
18325 reseat_to_string (it, string, lisp_string, start,
18326 precision, field_width, multibyte);
18327
18328 /* If displaying STRING, set up the face of the iterator
18329 from LISP_STRING, if that's given. */
18330 if (STRINGP (face_string))
18331 {
18332 int endptr;
18333 struct face *face;
18334
18335 it->face_id
18336 = face_at_string_position (it->w, face_string, face_string_pos,
18337 0, it->region_beg_charpos,
18338 it->region_end_charpos,
18339 &endptr, it->base_face_id, 0);
18340 face = FACE_FROM_ID (it->f, it->face_id);
18341 it->face_box_p = face->box != FACE_NO_BOX;
18342 }
18343
18344 /* Set max_x to the maximum allowed X position. Don't let it go
18345 beyond the right edge of the window. */
18346 if (max_x <= 0)
18347 max_x = it->last_visible_x;
18348 else
18349 max_x = min (max_x, it->last_visible_x);
18350
18351 /* Skip over display elements that are not visible. because IT->w is
18352 hscrolled. */
18353 if (it->current_x < it->first_visible_x)
18354 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18355 MOVE_TO_POS | MOVE_TO_X);
18356
18357 row->ascent = it->max_ascent;
18358 row->height = it->max_ascent + it->max_descent;
18359 row->phys_ascent = it->max_phys_ascent;
18360 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18361 row->extra_line_spacing = it->max_extra_line_spacing;
18362
18363 /* This condition is for the case that we are called with current_x
18364 past last_visible_x. */
18365 while (it->current_x < max_x)
18366 {
18367 int x_before, x, n_glyphs_before, i, nglyphs;
18368
18369 /* Get the next display element. */
18370 if (!get_next_display_element (it))
18371 break;
18372
18373 /* Produce glyphs. */
18374 x_before = it->current_x;
18375 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18376 PRODUCE_GLYPHS (it);
18377
18378 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18379 i = 0;
18380 x = x_before;
18381 while (i < nglyphs)
18382 {
18383 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18384
18385 if (!it->truncate_lines_p
18386 && x + glyph->pixel_width > max_x)
18387 {
18388 /* End of continued line or max_x reached. */
18389 if (CHAR_GLYPH_PADDING_P (*glyph))
18390 {
18391 /* A wide character is unbreakable. */
18392 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18393 it->current_x = x_before;
18394 }
18395 else
18396 {
18397 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18398 it->current_x = x;
18399 }
18400 break;
18401 }
18402 else if (x + glyph->pixel_width > it->first_visible_x)
18403 {
18404 /* Glyph is at least partially visible. */
18405 ++it->hpos;
18406 if (x < it->first_visible_x)
18407 it->glyph_row->x = x - it->first_visible_x;
18408 }
18409 else
18410 {
18411 /* Glyph is off the left margin of the display area.
18412 Should not happen. */
18413 abort ();
18414 }
18415
18416 row->ascent = max (row->ascent, it->max_ascent);
18417 row->height = max (row->height, it->max_ascent + it->max_descent);
18418 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18419 row->phys_height = max (row->phys_height,
18420 it->max_phys_ascent + it->max_phys_descent);
18421 row->extra_line_spacing = max (row->extra_line_spacing,
18422 it->max_extra_line_spacing);
18423 x += glyph->pixel_width;
18424 ++i;
18425 }
18426
18427 /* Stop if max_x reached. */
18428 if (i < nglyphs)
18429 break;
18430
18431 /* Stop at line ends. */
18432 if (ITERATOR_AT_END_OF_LINE_P (it))
18433 {
18434 it->continuation_lines_width = 0;
18435 break;
18436 }
18437
18438 set_iterator_to_next (it, 1);
18439
18440 /* Stop if truncating at the right edge. */
18441 if (it->truncate_lines_p
18442 && it->current_x >= it->last_visible_x)
18443 {
18444 /* Add truncation mark, but don't do it if the line is
18445 truncated at a padding space. */
18446 if (IT_CHARPOS (*it) < it->string_nchars)
18447 {
18448 if (!FRAME_WINDOW_P (it->f))
18449 {
18450 int i, n;
18451
18452 if (it->current_x > it->last_visible_x)
18453 {
18454 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18455 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18456 break;
18457 for (n = row->used[TEXT_AREA]; i < n; ++i)
18458 {
18459 row->used[TEXT_AREA] = i;
18460 produce_special_glyphs (it, IT_TRUNCATION);
18461 }
18462 }
18463 produce_special_glyphs (it, IT_TRUNCATION);
18464 }
18465 it->glyph_row->truncated_on_right_p = 1;
18466 }
18467 break;
18468 }
18469 }
18470
18471 /* Maybe insert a truncation at the left. */
18472 if (it->first_visible_x
18473 && IT_CHARPOS (*it) > 0)
18474 {
18475 if (!FRAME_WINDOW_P (it->f))
18476 insert_left_trunc_glyphs (it);
18477 it->glyph_row->truncated_on_left_p = 1;
18478 }
18479
18480 it->face_id = saved_face_id;
18481
18482 /* Value is number of columns displayed. */
18483 return it->hpos - hpos_at_start;
18484 }
18485
18486
18487 \f
18488 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18489 appears as an element of LIST or as the car of an element of LIST.
18490 If PROPVAL is a list, compare each element against LIST in that
18491 way, and return 1/2 if any element of PROPVAL is found in LIST.
18492 Otherwise return 0. This function cannot quit.
18493 The return value is 2 if the text is invisible but with an ellipsis
18494 and 1 if it's invisible and without an ellipsis. */
18495
18496 int
18497 invisible_p (propval, list)
18498 register Lisp_Object propval;
18499 Lisp_Object list;
18500 {
18501 register Lisp_Object tail, proptail;
18502
18503 for (tail = list; CONSP (tail); tail = XCDR (tail))
18504 {
18505 register Lisp_Object tem;
18506 tem = XCAR (tail);
18507 if (EQ (propval, tem))
18508 return 1;
18509 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18510 return NILP (XCDR (tem)) ? 1 : 2;
18511 }
18512
18513 if (CONSP (propval))
18514 {
18515 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18516 {
18517 Lisp_Object propelt;
18518 propelt = XCAR (proptail);
18519 for (tail = list; CONSP (tail); tail = XCDR (tail))
18520 {
18521 register Lisp_Object tem;
18522 tem = XCAR (tail);
18523 if (EQ (propelt, tem))
18524 return 1;
18525 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18526 return NILP (XCDR (tem)) ? 1 : 2;
18527 }
18528 }
18529 }
18530
18531 return 0;
18532 }
18533
18534 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18535 doc: /* Non-nil if the property makes the text invisible.
18536 POS-OR-PROP can be a marker or number, in which case it is taken to be
18537 a position in the current buffer and the value of the `invisible' property
18538 is checked; or it can be some other value, which is then presumed to be the
18539 value of the `invisible' property of the text of interest.
18540 The non-nil value returned can be t for truly invisible text or something
18541 else if the text is replaced by an ellipsis. */)
18542 (pos_or_prop)
18543 Lisp_Object pos_or_prop;
18544 {
18545 Lisp_Object prop
18546 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18547 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18548 : pos_or_prop);
18549 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18550 return (invis == 0 ? Qnil
18551 : invis == 1 ? Qt
18552 : make_number (invis));
18553 }
18554
18555 /* Calculate a width or height in pixels from a specification using
18556 the following elements:
18557
18558 SPEC ::=
18559 NUM - a (fractional) multiple of the default font width/height
18560 (NUM) - specifies exactly NUM pixels
18561 UNIT - a fixed number of pixels, see below.
18562 ELEMENT - size of a display element in pixels, see below.
18563 (NUM . SPEC) - equals NUM * SPEC
18564 (+ SPEC SPEC ...) - add pixel values
18565 (- SPEC SPEC ...) - subtract pixel values
18566 (- SPEC) - negate pixel value
18567
18568 NUM ::=
18569 INT or FLOAT - a number constant
18570 SYMBOL - use symbol's (buffer local) variable binding.
18571
18572 UNIT ::=
18573 in - pixels per inch *)
18574 mm - pixels per 1/1000 meter *)
18575 cm - pixels per 1/100 meter *)
18576 width - width of current font in pixels.
18577 height - height of current font in pixels.
18578
18579 *) using the ratio(s) defined in display-pixels-per-inch.
18580
18581 ELEMENT ::=
18582
18583 left-fringe - left fringe width in pixels
18584 right-fringe - right fringe width in pixels
18585
18586 left-margin - left margin width in pixels
18587 right-margin - right margin width in pixels
18588
18589 scroll-bar - scroll-bar area width in pixels
18590
18591 Examples:
18592
18593 Pixels corresponding to 5 inches:
18594 (5 . in)
18595
18596 Total width of non-text areas on left side of window (if scroll-bar is on left):
18597 '(space :width (+ left-fringe left-margin scroll-bar))
18598
18599 Align to first text column (in header line):
18600 '(space :align-to 0)
18601
18602 Align to middle of text area minus half the width of variable `my-image'
18603 containing a loaded image:
18604 '(space :align-to (0.5 . (- text my-image)))
18605
18606 Width of left margin minus width of 1 character in the default font:
18607 '(space :width (- left-margin 1))
18608
18609 Width of left margin minus width of 2 characters in the current font:
18610 '(space :width (- left-margin (2 . width)))
18611
18612 Center 1 character over left-margin (in header line):
18613 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18614
18615 Different ways to express width of left fringe plus left margin minus one pixel:
18616 '(space :width (- (+ left-fringe left-margin) (1)))
18617 '(space :width (+ left-fringe left-margin (- (1))))
18618 '(space :width (+ left-fringe left-margin (-1)))
18619
18620 */
18621
18622 #define NUMVAL(X) \
18623 ((INTEGERP (X) || FLOATP (X)) \
18624 ? XFLOATINT (X) \
18625 : - 1)
18626
18627 int
18628 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18629 double *res;
18630 struct it *it;
18631 Lisp_Object prop;
18632 void *font;
18633 int width_p, *align_to;
18634 {
18635 double pixels;
18636
18637 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18638 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18639
18640 if (NILP (prop))
18641 return OK_PIXELS (0);
18642
18643 xassert (FRAME_LIVE_P (it->f));
18644
18645 if (SYMBOLP (prop))
18646 {
18647 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18648 {
18649 char *unit = SDATA (SYMBOL_NAME (prop));
18650
18651 if (unit[0] == 'i' && unit[1] == 'n')
18652 pixels = 1.0;
18653 else if (unit[0] == 'm' && unit[1] == 'm')
18654 pixels = 25.4;
18655 else if (unit[0] == 'c' && unit[1] == 'm')
18656 pixels = 2.54;
18657 else
18658 pixels = 0;
18659 if (pixels > 0)
18660 {
18661 double ppi;
18662 #ifdef HAVE_WINDOW_SYSTEM
18663 if (FRAME_WINDOW_P (it->f)
18664 && (ppi = (width_p
18665 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18666 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18667 ppi > 0))
18668 return OK_PIXELS (ppi / pixels);
18669 #endif
18670
18671 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18672 || (CONSP (Vdisplay_pixels_per_inch)
18673 && (ppi = (width_p
18674 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18675 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18676 ppi > 0)))
18677 return OK_PIXELS (ppi / pixels);
18678
18679 return 0;
18680 }
18681 }
18682
18683 #ifdef HAVE_WINDOW_SYSTEM
18684 if (EQ (prop, Qheight))
18685 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18686 if (EQ (prop, Qwidth))
18687 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18688 #else
18689 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18690 return OK_PIXELS (1);
18691 #endif
18692
18693 if (EQ (prop, Qtext))
18694 return OK_PIXELS (width_p
18695 ? window_box_width (it->w, TEXT_AREA)
18696 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18697
18698 if (align_to && *align_to < 0)
18699 {
18700 *res = 0;
18701 if (EQ (prop, Qleft))
18702 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18703 if (EQ (prop, Qright))
18704 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18705 if (EQ (prop, Qcenter))
18706 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18707 + window_box_width (it->w, TEXT_AREA) / 2);
18708 if (EQ (prop, Qleft_fringe))
18709 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18710 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18711 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18712 if (EQ (prop, Qright_fringe))
18713 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18714 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18715 : window_box_right_offset (it->w, TEXT_AREA));
18716 if (EQ (prop, Qleft_margin))
18717 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18718 if (EQ (prop, Qright_margin))
18719 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18720 if (EQ (prop, Qscroll_bar))
18721 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18722 ? 0
18723 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18724 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18725 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18726 : 0)));
18727 }
18728 else
18729 {
18730 if (EQ (prop, Qleft_fringe))
18731 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18732 if (EQ (prop, Qright_fringe))
18733 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18734 if (EQ (prop, Qleft_margin))
18735 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18736 if (EQ (prop, Qright_margin))
18737 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18738 if (EQ (prop, Qscroll_bar))
18739 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18740 }
18741
18742 prop = Fbuffer_local_value (prop, it->w->buffer);
18743 }
18744
18745 if (INTEGERP (prop) || FLOATP (prop))
18746 {
18747 int base_unit = (width_p
18748 ? FRAME_COLUMN_WIDTH (it->f)
18749 : FRAME_LINE_HEIGHT (it->f));
18750 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18751 }
18752
18753 if (CONSP (prop))
18754 {
18755 Lisp_Object car = XCAR (prop);
18756 Lisp_Object cdr = XCDR (prop);
18757
18758 if (SYMBOLP (car))
18759 {
18760 #ifdef HAVE_WINDOW_SYSTEM
18761 if (FRAME_WINDOW_P (it->f)
18762 && valid_image_p (prop))
18763 {
18764 int id = lookup_image (it->f, prop);
18765 struct image *img = IMAGE_FROM_ID (it->f, id);
18766
18767 return OK_PIXELS (width_p ? img->width : img->height);
18768 }
18769 #endif
18770 if (EQ (car, Qplus) || EQ (car, Qminus))
18771 {
18772 int first = 1;
18773 double px;
18774
18775 pixels = 0;
18776 while (CONSP (cdr))
18777 {
18778 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18779 font, width_p, align_to))
18780 return 0;
18781 if (first)
18782 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18783 else
18784 pixels += px;
18785 cdr = XCDR (cdr);
18786 }
18787 if (EQ (car, Qminus))
18788 pixels = -pixels;
18789 return OK_PIXELS (pixels);
18790 }
18791
18792 car = Fbuffer_local_value (car, it->w->buffer);
18793 }
18794
18795 if (INTEGERP (car) || FLOATP (car))
18796 {
18797 double fact;
18798 pixels = XFLOATINT (car);
18799 if (NILP (cdr))
18800 return OK_PIXELS (pixels);
18801 if (calc_pixel_width_or_height (&fact, it, cdr,
18802 font, width_p, align_to))
18803 return OK_PIXELS (pixels * fact);
18804 return 0;
18805 }
18806
18807 return 0;
18808 }
18809
18810 return 0;
18811 }
18812
18813 \f
18814 /***********************************************************************
18815 Glyph Display
18816 ***********************************************************************/
18817
18818 #ifdef HAVE_WINDOW_SYSTEM
18819
18820 #if GLYPH_DEBUG
18821
18822 void
18823 dump_glyph_string (s)
18824 struct glyph_string *s;
18825 {
18826 fprintf (stderr, "glyph string\n");
18827 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18828 s->x, s->y, s->width, s->height);
18829 fprintf (stderr, " ybase = %d\n", s->ybase);
18830 fprintf (stderr, " hl = %d\n", s->hl);
18831 fprintf (stderr, " left overhang = %d, right = %d\n",
18832 s->left_overhang, s->right_overhang);
18833 fprintf (stderr, " nchars = %d\n", s->nchars);
18834 fprintf (stderr, " extends to end of line = %d\n",
18835 s->extends_to_end_of_line_p);
18836 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18837 fprintf (stderr, " bg width = %d\n", s->background_width);
18838 }
18839
18840 #endif /* GLYPH_DEBUG */
18841
18842 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18843 of XChar2b structures for S; it can't be allocated in
18844 init_glyph_string because it must be allocated via `alloca'. W
18845 is the window on which S is drawn. ROW and AREA are the glyph row
18846 and area within the row from which S is constructed. START is the
18847 index of the first glyph structure covered by S. HL is a
18848 face-override for drawing S. */
18849
18850 #ifdef HAVE_NTGUI
18851 #define OPTIONAL_HDC(hdc) hdc,
18852 #define DECLARE_HDC(hdc) HDC hdc;
18853 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18854 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18855 #endif
18856
18857 #ifndef OPTIONAL_HDC
18858 #define OPTIONAL_HDC(hdc)
18859 #define DECLARE_HDC(hdc)
18860 #define ALLOCATE_HDC(hdc, f)
18861 #define RELEASE_HDC(hdc, f)
18862 #endif
18863
18864 static void
18865 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18866 struct glyph_string *s;
18867 DECLARE_HDC (hdc)
18868 XChar2b *char2b;
18869 struct window *w;
18870 struct glyph_row *row;
18871 enum glyph_row_area area;
18872 int start;
18873 enum draw_glyphs_face hl;
18874 {
18875 bzero (s, sizeof *s);
18876 s->w = w;
18877 s->f = XFRAME (w->frame);
18878 #ifdef HAVE_NTGUI
18879 s->hdc = hdc;
18880 #endif
18881 s->display = FRAME_X_DISPLAY (s->f);
18882 s->window = FRAME_X_WINDOW (s->f);
18883 s->char2b = char2b;
18884 s->hl = hl;
18885 s->row = row;
18886 s->area = area;
18887 s->first_glyph = row->glyphs[area] + start;
18888 s->height = row->height;
18889 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18890
18891 /* Display the internal border below the tool-bar window. */
18892 if (WINDOWP (s->f->tool_bar_window)
18893 && s->w == XWINDOW (s->f->tool_bar_window))
18894 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18895
18896 s->ybase = s->y + row->ascent;
18897 }
18898
18899
18900 /* Append the list of glyph strings with head H and tail T to the list
18901 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18902
18903 static INLINE void
18904 append_glyph_string_lists (head, tail, h, t)
18905 struct glyph_string **head, **tail;
18906 struct glyph_string *h, *t;
18907 {
18908 if (h)
18909 {
18910 if (*head)
18911 (*tail)->next = h;
18912 else
18913 *head = h;
18914 h->prev = *tail;
18915 *tail = t;
18916 }
18917 }
18918
18919
18920 /* Prepend the list of glyph strings with head H and tail T to the
18921 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18922 result. */
18923
18924 static INLINE void
18925 prepend_glyph_string_lists (head, tail, h, t)
18926 struct glyph_string **head, **tail;
18927 struct glyph_string *h, *t;
18928 {
18929 if (h)
18930 {
18931 if (*head)
18932 (*head)->prev = t;
18933 else
18934 *tail = t;
18935 t->next = *head;
18936 *head = h;
18937 }
18938 }
18939
18940
18941 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18942 Set *HEAD and *TAIL to the resulting list. */
18943
18944 static INLINE void
18945 append_glyph_string (head, tail, s)
18946 struct glyph_string **head, **tail;
18947 struct glyph_string *s;
18948 {
18949 s->next = s->prev = NULL;
18950 append_glyph_string_lists (head, tail, s, s);
18951 }
18952
18953
18954 /* Get face and two-byte form of character glyph GLYPH on frame F.
18955 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18956 a pointer to a realized face that is ready for display. */
18957
18958 static INLINE struct face *
18959 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18960 struct frame *f;
18961 struct glyph *glyph;
18962 XChar2b *char2b;
18963 int *two_byte_p;
18964 {
18965 struct face *face;
18966
18967 xassert (glyph->type == CHAR_GLYPH);
18968 face = FACE_FROM_ID (f, glyph->face_id);
18969
18970 if (two_byte_p)
18971 *two_byte_p = 0;
18972
18973 if (!glyph->multibyte_p)
18974 {
18975 /* Unibyte case. We don't have to encode, but we have to make
18976 sure to use a face suitable for unibyte. */
18977 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18978 }
18979 else if (glyph->u.ch < 128)
18980 {
18981 /* Case of ASCII in a face known to fit ASCII. */
18982 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18983 }
18984 else
18985 {
18986 int c1, c2, charset;
18987
18988 /* Split characters into bytes. If c2 is -1 afterwards, C is
18989 really a one-byte character so that byte1 is zero. */
18990 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18991 if (c2 > 0)
18992 STORE_XCHAR2B (char2b, c1, c2);
18993 else
18994 STORE_XCHAR2B (char2b, 0, c1);
18995
18996 /* Maybe encode the character in *CHAR2B. */
18997 if (charset != CHARSET_ASCII)
18998 {
18999 struct font_info *font_info
19000 = FONT_INFO_FROM_ID (f, face->font_info_id);
19001 if (font_info)
19002 glyph->font_type
19003 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
19004 }
19005 }
19006
19007 /* Make sure X resources of the face are allocated. */
19008 xassert (face != NULL);
19009 PREPARE_FACE_FOR_DISPLAY (f, face);
19010 return face;
19011 }
19012
19013
19014 /* Fill glyph string S with composition components specified by S->cmp.
19015
19016 FACES is an array of faces for all components of this composition.
19017 S->gidx is the index of the first component for S.
19018
19019 OVERLAPS non-zero means S should draw the foreground only, and use
19020 its physical height for clipping. See also draw_glyphs.
19021
19022 Value is the index of a component not in S. */
19023
19024 static int
19025 fill_composite_glyph_string (s, faces, overlaps)
19026 struct glyph_string *s;
19027 struct face **faces;
19028 int overlaps;
19029 {
19030 int i;
19031
19032 xassert (s);
19033
19034 s->for_overlaps = overlaps;
19035
19036 s->face = faces[s->gidx];
19037 s->font = s->face->font;
19038 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19039
19040 /* For all glyphs of this composition, starting at the offset
19041 S->gidx, until we reach the end of the definition or encounter a
19042 glyph that requires the different face, add it to S. */
19043 ++s->nchars;
19044 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
19045 ++s->nchars;
19046
19047 /* All glyph strings for the same composition has the same width,
19048 i.e. the width set for the first component of the composition. */
19049
19050 s->width = s->first_glyph->pixel_width;
19051
19052 /* If the specified font could not be loaded, use the frame's
19053 default font, but record the fact that we couldn't load it in
19054 the glyph string so that we can draw rectangles for the
19055 characters of the glyph string. */
19056 if (s->font == NULL)
19057 {
19058 s->font_not_found_p = 1;
19059 s->font = FRAME_FONT (s->f);
19060 }
19061
19062 /* Adjust base line for subscript/superscript text. */
19063 s->ybase += s->first_glyph->voffset;
19064
19065 xassert (s->face && s->face->gc);
19066
19067 /* This glyph string must always be drawn with 16-bit functions. */
19068 s->two_byte_p = 1;
19069
19070 return s->gidx + s->nchars;
19071 }
19072
19073
19074 /* Fill glyph string S from a sequence of character glyphs.
19075
19076 FACE_ID is the face id of the string. START is the index of the
19077 first glyph to consider, END is the index of the last + 1.
19078 OVERLAPS non-zero means S should draw the foreground only, and use
19079 its physical height for clipping. See also draw_glyphs.
19080
19081 Value is the index of the first glyph not in S. */
19082
19083 static int
19084 fill_glyph_string (s, face_id, start, end, overlaps)
19085 struct glyph_string *s;
19086 int face_id;
19087 int start, end, overlaps;
19088 {
19089 struct glyph *glyph, *last;
19090 int voffset;
19091 int glyph_not_available_p;
19092
19093 xassert (s->f == XFRAME (s->w->frame));
19094 xassert (s->nchars == 0);
19095 xassert (start >= 0 && end > start);
19096
19097 s->for_overlaps = overlaps,
19098 glyph = s->row->glyphs[s->area] + start;
19099 last = s->row->glyphs[s->area] + end;
19100 voffset = glyph->voffset;
19101
19102 glyph_not_available_p = glyph->glyph_not_available_p;
19103
19104 while (glyph < last
19105 && glyph->type == CHAR_GLYPH
19106 && glyph->voffset == voffset
19107 /* Same face id implies same font, nowadays. */
19108 && glyph->face_id == face_id
19109 && glyph->glyph_not_available_p == glyph_not_available_p)
19110 {
19111 int two_byte_p;
19112
19113 s->face = get_glyph_face_and_encoding (s->f, glyph,
19114 s->char2b + s->nchars,
19115 &two_byte_p);
19116 s->two_byte_p = two_byte_p;
19117 ++s->nchars;
19118 xassert (s->nchars <= end - start);
19119 s->width += glyph->pixel_width;
19120 ++glyph;
19121 }
19122
19123 s->font = s->face->font;
19124 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19125
19126 /* If the specified font could not be loaded, use the frame's font,
19127 but record the fact that we couldn't load it in
19128 S->font_not_found_p so that we can draw rectangles for the
19129 characters of the glyph string. */
19130 if (s->font == NULL || glyph_not_available_p)
19131 {
19132 s->font_not_found_p = 1;
19133 s->font = FRAME_FONT (s->f);
19134 }
19135
19136 /* Adjust base line for subscript/superscript text. */
19137 s->ybase += voffset;
19138
19139 xassert (s->face && s->face->gc);
19140 return glyph - s->row->glyphs[s->area];
19141 }
19142
19143
19144 /* Fill glyph string S from image glyph S->first_glyph. */
19145
19146 static void
19147 fill_image_glyph_string (s)
19148 struct glyph_string *s;
19149 {
19150 xassert (s->first_glyph->type == IMAGE_GLYPH);
19151 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19152 xassert (s->img);
19153 s->slice = s->first_glyph->slice;
19154 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19155 s->font = s->face->font;
19156 s->width = s->first_glyph->pixel_width;
19157
19158 /* Adjust base line for subscript/superscript text. */
19159 s->ybase += s->first_glyph->voffset;
19160 }
19161
19162
19163 /* Fill glyph string S from a sequence of stretch glyphs.
19164
19165 ROW is the glyph row in which the glyphs are found, AREA is the
19166 area within the row. START is the index of the first glyph to
19167 consider, END is the index of the last + 1.
19168
19169 Value is the index of the first glyph not in S. */
19170
19171 static int
19172 fill_stretch_glyph_string (s, row, area, start, end)
19173 struct glyph_string *s;
19174 struct glyph_row *row;
19175 enum glyph_row_area area;
19176 int start, end;
19177 {
19178 struct glyph *glyph, *last;
19179 int voffset, face_id;
19180
19181 xassert (s->first_glyph->type == STRETCH_GLYPH);
19182
19183 glyph = s->row->glyphs[s->area] + start;
19184 last = s->row->glyphs[s->area] + end;
19185 face_id = glyph->face_id;
19186 s->face = FACE_FROM_ID (s->f, face_id);
19187 s->font = s->face->font;
19188 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19189 s->width = glyph->pixel_width;
19190 s->nchars = 1;
19191 voffset = glyph->voffset;
19192
19193 for (++glyph;
19194 (glyph < last
19195 && glyph->type == STRETCH_GLYPH
19196 && glyph->voffset == voffset
19197 && glyph->face_id == face_id);
19198 ++glyph)
19199 s->width += glyph->pixel_width;
19200
19201 /* Adjust base line for subscript/superscript text. */
19202 s->ybase += voffset;
19203
19204 /* The case that face->gc == 0 is handled when drawing the glyph
19205 string by calling PREPARE_FACE_FOR_DISPLAY. */
19206 xassert (s->face);
19207 return glyph - s->row->glyphs[s->area];
19208 }
19209
19210
19211 /* EXPORT for RIF:
19212 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19213 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19214 assumed to be zero. */
19215
19216 void
19217 x_get_glyph_overhangs (glyph, f, left, right)
19218 struct glyph *glyph;
19219 struct frame *f;
19220 int *left, *right;
19221 {
19222 *left = *right = 0;
19223
19224 if (glyph->type == CHAR_GLYPH)
19225 {
19226 XFontStruct *font;
19227 struct face *face;
19228 struct font_info *font_info;
19229 XChar2b char2b;
19230 XCharStruct *pcm;
19231
19232 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19233 font = face->font;
19234 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19235 if (font /* ++KFS: Should this be font_info ? */
19236 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
19237 {
19238 if (pcm->rbearing > pcm->width)
19239 *right = pcm->rbearing - pcm->width;
19240 if (pcm->lbearing < 0)
19241 *left = -pcm->lbearing;
19242 }
19243 }
19244 }
19245
19246
19247 /* Return the index of the first glyph preceding glyph string S that
19248 is overwritten by S because of S's left overhang. Value is -1
19249 if no glyphs are overwritten. */
19250
19251 static int
19252 left_overwritten (s)
19253 struct glyph_string *s;
19254 {
19255 int k;
19256
19257 if (s->left_overhang)
19258 {
19259 int x = 0, i;
19260 struct glyph *glyphs = s->row->glyphs[s->area];
19261 int first = s->first_glyph - glyphs;
19262
19263 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19264 x -= glyphs[i].pixel_width;
19265
19266 k = i + 1;
19267 }
19268 else
19269 k = -1;
19270
19271 return k;
19272 }
19273
19274
19275 /* Return the index of the first glyph preceding glyph string S that
19276 is overwriting S because of its right overhang. Value is -1 if no
19277 glyph in front of S overwrites S. */
19278
19279 static int
19280 left_overwriting (s)
19281 struct glyph_string *s;
19282 {
19283 int i, k, x;
19284 struct glyph *glyphs = s->row->glyphs[s->area];
19285 int first = s->first_glyph - glyphs;
19286
19287 k = -1;
19288 x = 0;
19289 for (i = first - 1; i >= 0; --i)
19290 {
19291 int left, right;
19292 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19293 if (x + right > 0)
19294 k = i;
19295 x -= glyphs[i].pixel_width;
19296 }
19297
19298 return k;
19299 }
19300
19301
19302 /* Return the index of the last glyph following glyph string S that is
19303 not overwritten by S because of S's right overhang. Value is -1 if
19304 no such glyph is found. */
19305
19306 static int
19307 right_overwritten (s)
19308 struct glyph_string *s;
19309 {
19310 int k = -1;
19311
19312 if (s->right_overhang)
19313 {
19314 int x = 0, i;
19315 struct glyph *glyphs = s->row->glyphs[s->area];
19316 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19317 int end = s->row->used[s->area];
19318
19319 for (i = first; i < end && s->right_overhang > x; ++i)
19320 x += glyphs[i].pixel_width;
19321
19322 k = i;
19323 }
19324
19325 return k;
19326 }
19327
19328
19329 /* Return the index of the last glyph following glyph string S that
19330 overwrites S because of its left overhang. Value is negative
19331 if no such glyph is found. */
19332
19333 static int
19334 right_overwriting (s)
19335 struct glyph_string *s;
19336 {
19337 int i, k, x;
19338 int end = s->row->used[s->area];
19339 struct glyph *glyphs = s->row->glyphs[s->area];
19340 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19341
19342 k = -1;
19343 x = 0;
19344 for (i = first; i < end; ++i)
19345 {
19346 int left, right;
19347 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19348 if (x - left < 0)
19349 k = i;
19350 x += glyphs[i].pixel_width;
19351 }
19352
19353 return k;
19354 }
19355
19356
19357 /* Get face and two-byte form of character C in face FACE_ID on frame
19358 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19359 means we want to display multibyte text. DISPLAY_P non-zero means
19360 make sure that X resources for the face returned are allocated.
19361 Value is a pointer to a realized face that is ready for display if
19362 DISPLAY_P is non-zero. */
19363
19364 static INLINE struct face *
19365 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19366 struct frame *f;
19367 int c, face_id;
19368 XChar2b *char2b;
19369 int multibyte_p, display_p;
19370 {
19371 struct face *face = FACE_FROM_ID (f, face_id);
19372
19373 if (!multibyte_p)
19374 {
19375 /* Unibyte case. We don't have to encode, but we have to make
19376 sure to use a face suitable for unibyte. */
19377 STORE_XCHAR2B (char2b, 0, c);
19378 face_id = FACE_FOR_CHAR (f, face, c);
19379 face = FACE_FROM_ID (f, face_id);
19380 }
19381 else if (c < 128)
19382 {
19383 /* Case of ASCII in a face known to fit ASCII. */
19384 STORE_XCHAR2B (char2b, 0, c);
19385 }
19386 else
19387 {
19388 int c1, c2, charset;
19389
19390 /* Split characters into bytes. If c2 is -1 afterwards, C is
19391 really a one-byte character so that byte1 is zero. */
19392 SPLIT_CHAR (c, charset, c1, c2);
19393 if (c2 > 0)
19394 STORE_XCHAR2B (char2b, c1, c2);
19395 else
19396 STORE_XCHAR2B (char2b, 0, c1);
19397
19398 /* Maybe encode the character in *CHAR2B. */
19399 if (face->font != NULL)
19400 {
19401 struct font_info *font_info
19402 = FONT_INFO_FROM_ID (f, face->font_info_id);
19403 if (font_info)
19404 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
19405 }
19406 }
19407
19408 /* Make sure X resources of the face are allocated. */
19409 #ifdef HAVE_X_WINDOWS
19410 if (display_p)
19411 #endif
19412 {
19413 xassert (face != NULL);
19414 PREPARE_FACE_FOR_DISPLAY (f, face);
19415 }
19416
19417 return face;
19418 }
19419
19420
19421 /* Set background width of glyph string S. START is the index of the
19422 first glyph following S. LAST_X is the right-most x-position + 1
19423 in the drawing area. */
19424
19425 static INLINE void
19426 set_glyph_string_background_width (s, start, last_x)
19427 struct glyph_string *s;
19428 int start;
19429 int last_x;
19430 {
19431 /* If the face of this glyph string has to be drawn to the end of
19432 the drawing area, set S->extends_to_end_of_line_p. */
19433
19434 if (start == s->row->used[s->area]
19435 && s->area == TEXT_AREA
19436 && ((s->row->fill_line_p
19437 && (s->hl == DRAW_NORMAL_TEXT
19438 || s->hl == DRAW_IMAGE_RAISED
19439 || s->hl == DRAW_IMAGE_SUNKEN))
19440 || s->hl == DRAW_MOUSE_FACE))
19441 s->extends_to_end_of_line_p = 1;
19442
19443 /* If S extends its face to the end of the line, set its
19444 background_width to the distance to the right edge of the drawing
19445 area. */
19446 if (s->extends_to_end_of_line_p)
19447 s->background_width = last_x - s->x + 1;
19448 else
19449 s->background_width = s->width;
19450 }
19451
19452
19453 /* Compute overhangs and x-positions for glyph string S and its
19454 predecessors, or successors. X is the starting x-position for S.
19455 BACKWARD_P non-zero means process predecessors. */
19456
19457 static void
19458 compute_overhangs_and_x (s, x, backward_p)
19459 struct glyph_string *s;
19460 int x;
19461 int backward_p;
19462 {
19463 if (backward_p)
19464 {
19465 while (s)
19466 {
19467 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19468 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19469 x -= s->width;
19470 s->x = x;
19471 s = s->prev;
19472 }
19473 }
19474 else
19475 {
19476 while (s)
19477 {
19478 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19479 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19480 s->x = x;
19481 x += s->width;
19482 s = s->next;
19483 }
19484 }
19485 }
19486
19487
19488
19489 /* The following macros are only called from draw_glyphs below.
19490 They reference the following parameters of that function directly:
19491 `w', `row', `area', and `overlap_p'
19492 as well as the following local variables:
19493 `s', `f', and `hdc' (in W32) */
19494
19495 #ifdef HAVE_NTGUI
19496 /* On W32, silently add local `hdc' variable to argument list of
19497 init_glyph_string. */
19498 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19499 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19500 #else
19501 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19502 init_glyph_string (s, char2b, w, row, area, start, hl)
19503 #endif
19504
19505 /* Add a glyph string for a stretch glyph to the list of strings
19506 between HEAD and TAIL. START is the index of the stretch glyph in
19507 row area AREA of glyph row ROW. END is the index of the last glyph
19508 in that glyph row area. X is the current output position assigned
19509 to the new glyph string constructed. HL overrides that face of the
19510 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19511 is the right-most x-position of the drawing area. */
19512
19513 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19514 and below -- keep them on one line. */
19515 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19516 do \
19517 { \
19518 s = (struct glyph_string *) alloca (sizeof *s); \
19519 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19520 START = fill_stretch_glyph_string (s, row, area, START, END); \
19521 append_glyph_string (&HEAD, &TAIL, s); \
19522 s->x = (X); \
19523 } \
19524 while (0)
19525
19526
19527 /* Add a glyph string for an image glyph to the list of strings
19528 between HEAD and TAIL. START is the index of the image glyph in
19529 row area AREA of glyph row ROW. END is the index of the last glyph
19530 in that glyph row area. X is the current output position assigned
19531 to the new glyph string constructed. HL overrides that face of the
19532 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19533 is the right-most x-position of the drawing area. */
19534
19535 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19536 do \
19537 { \
19538 s = (struct glyph_string *) alloca (sizeof *s); \
19539 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19540 fill_image_glyph_string (s); \
19541 append_glyph_string (&HEAD, &TAIL, s); \
19542 ++START; \
19543 s->x = (X); \
19544 } \
19545 while (0)
19546
19547
19548 /* Add a glyph string for a sequence of character glyphs to the list
19549 of strings between HEAD and TAIL. START is the index of the first
19550 glyph in row area AREA of glyph row ROW that is part of the new
19551 glyph string. END is the index of the last glyph in that glyph row
19552 area. X is the current output position assigned to the new glyph
19553 string constructed. HL overrides that face of the glyph; e.g. it
19554 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19555 right-most x-position of the drawing area. */
19556
19557 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19558 do \
19559 { \
19560 int c, face_id; \
19561 XChar2b *char2b; \
19562 \
19563 c = (row)->glyphs[area][START].u.ch; \
19564 face_id = (row)->glyphs[area][START].face_id; \
19565 \
19566 s = (struct glyph_string *) alloca (sizeof *s); \
19567 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19568 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19569 append_glyph_string (&HEAD, &TAIL, s); \
19570 s->x = (X); \
19571 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19572 } \
19573 while (0)
19574
19575
19576 /* Add a glyph string for a composite sequence to the list of strings
19577 between HEAD and TAIL. START is the index of the first glyph in
19578 row area AREA of glyph row ROW that is part of the new glyph
19579 string. END is the index of the last glyph in that glyph row area.
19580 X is the current output position assigned to the new glyph string
19581 constructed. HL overrides that face of the glyph; e.g. it is
19582 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19583 x-position of the drawing area. */
19584
19585 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19586 do { \
19587 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19588 int face_id = (row)->glyphs[area][START].face_id; \
19589 struct face *base_face = FACE_FROM_ID (f, face_id); \
19590 struct composition *cmp = composition_table[cmp_id]; \
19591 int glyph_len = cmp->glyph_len; \
19592 XChar2b *char2b; \
19593 struct face **faces; \
19594 struct glyph_string *first_s = NULL; \
19595 int n; \
19596 \
19597 base_face = base_face->ascii_face; \
19598 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19599 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19600 /* At first, fill in `char2b' and `faces'. */ \
19601 for (n = 0; n < glyph_len; n++) \
19602 { \
19603 int c = COMPOSITION_GLYPH (cmp, n); \
19604 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19605 faces[n] = FACE_FROM_ID (f, this_face_id); \
19606 get_char_face_and_encoding (f, c, this_face_id, \
19607 char2b + n, 1, 1); \
19608 } \
19609 \
19610 /* Make glyph_strings for each glyph sequence that is drawable by \
19611 the same face, and append them to HEAD/TAIL. */ \
19612 for (n = 0; n < cmp->glyph_len;) \
19613 { \
19614 s = (struct glyph_string *) alloca (sizeof *s); \
19615 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19616 append_glyph_string (&(HEAD), &(TAIL), s); \
19617 s->cmp = cmp; \
19618 s->gidx = n; \
19619 s->x = (X); \
19620 \
19621 if (n == 0) \
19622 first_s = s; \
19623 \
19624 n = fill_composite_glyph_string (s, faces, overlaps); \
19625 } \
19626 \
19627 ++START; \
19628 s = first_s; \
19629 } while (0)
19630
19631
19632 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19633 of AREA of glyph row ROW on window W between indices START and END.
19634 HL overrides the face for drawing glyph strings, e.g. it is
19635 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19636 x-positions of the drawing area.
19637
19638 This is an ugly monster macro construct because we must use alloca
19639 to allocate glyph strings (because draw_glyphs can be called
19640 asynchronously). */
19641
19642 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19643 do \
19644 { \
19645 HEAD = TAIL = NULL; \
19646 while (START < END) \
19647 { \
19648 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19649 switch (first_glyph->type) \
19650 { \
19651 case CHAR_GLYPH: \
19652 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19653 HL, X, LAST_X); \
19654 break; \
19655 \
19656 case COMPOSITE_GLYPH: \
19657 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19658 HL, X, LAST_X); \
19659 break; \
19660 \
19661 case STRETCH_GLYPH: \
19662 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19663 HL, X, LAST_X); \
19664 break; \
19665 \
19666 case IMAGE_GLYPH: \
19667 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19668 HL, X, LAST_X); \
19669 break; \
19670 \
19671 default: \
19672 abort (); \
19673 } \
19674 \
19675 set_glyph_string_background_width (s, START, LAST_X); \
19676 (X) += s->width; \
19677 } \
19678 } \
19679 while (0)
19680
19681
19682 /* Draw glyphs between START and END in AREA of ROW on window W,
19683 starting at x-position X. X is relative to AREA in W. HL is a
19684 face-override with the following meaning:
19685
19686 DRAW_NORMAL_TEXT draw normally
19687 DRAW_CURSOR draw in cursor face
19688 DRAW_MOUSE_FACE draw in mouse face.
19689 DRAW_INVERSE_VIDEO draw in mode line face
19690 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19691 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19692
19693 If OVERLAPS is non-zero, draw only the foreground of characters and
19694 clip to the physical height of ROW. Non-zero value also defines
19695 the overlapping part to be drawn:
19696
19697 OVERLAPS_PRED overlap with preceding rows
19698 OVERLAPS_SUCC overlap with succeeding rows
19699 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19700 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19701
19702 Value is the x-position reached, relative to AREA of W. */
19703
19704 static int
19705 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19706 struct window *w;
19707 int x;
19708 struct glyph_row *row;
19709 enum glyph_row_area area;
19710 int start, end;
19711 enum draw_glyphs_face hl;
19712 int overlaps;
19713 {
19714 struct glyph_string *head, *tail;
19715 struct glyph_string *s;
19716 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19717 int last_x, area_width;
19718 int x_reached;
19719 int i, j;
19720 struct frame *f = XFRAME (WINDOW_FRAME (w));
19721 DECLARE_HDC (hdc);
19722
19723 ALLOCATE_HDC (hdc, f);
19724
19725 /* Let's rather be paranoid than getting a SEGV. */
19726 end = min (end, row->used[area]);
19727 start = max (0, start);
19728 start = min (end, start);
19729
19730 /* Translate X to frame coordinates. Set last_x to the right
19731 end of the drawing area. */
19732 if (row->full_width_p)
19733 {
19734 /* X is relative to the left edge of W, without scroll bars
19735 or fringes. */
19736 x += WINDOW_LEFT_EDGE_X (w);
19737 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19738 }
19739 else
19740 {
19741 int area_left = window_box_left (w, area);
19742 x += area_left;
19743 area_width = window_box_width (w, area);
19744 last_x = area_left + area_width;
19745 }
19746
19747 /* Build a doubly-linked list of glyph_string structures between
19748 head and tail from what we have to draw. Note that the macro
19749 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19750 the reason we use a separate variable `i'. */
19751 i = start;
19752 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19753 if (tail)
19754 x_reached = tail->x + tail->background_width;
19755 else
19756 x_reached = x;
19757
19758 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19759 the row, redraw some glyphs in front or following the glyph
19760 strings built above. */
19761 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19762 {
19763 int dummy_x = 0;
19764 struct glyph_string *h, *t;
19765
19766 /* Compute overhangs for all glyph strings. */
19767 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19768 for (s = head; s; s = s->next)
19769 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19770
19771 /* Prepend glyph strings for glyphs in front of the first glyph
19772 string that are overwritten because of the first glyph
19773 string's left overhang. The background of all strings
19774 prepended must be drawn because the first glyph string
19775 draws over it. */
19776 i = left_overwritten (head);
19777 if (i >= 0)
19778 {
19779 j = i;
19780 BUILD_GLYPH_STRINGS (j, start, h, t,
19781 DRAW_NORMAL_TEXT, dummy_x, last_x);
19782 start = i;
19783 compute_overhangs_and_x (t, head->x, 1);
19784 prepend_glyph_string_lists (&head, &tail, h, t);
19785 clip_head = head;
19786 }
19787
19788 /* Prepend glyph strings for glyphs in front of the first glyph
19789 string that overwrite that glyph string because of their
19790 right overhang. For these strings, only the foreground must
19791 be drawn, because it draws over the glyph string at `head'.
19792 The background must not be drawn because this would overwrite
19793 right overhangs of preceding glyphs for which no glyph
19794 strings exist. */
19795 i = left_overwriting (head);
19796 if (i >= 0)
19797 {
19798 clip_head = head;
19799 BUILD_GLYPH_STRINGS (i, start, h, t,
19800 DRAW_NORMAL_TEXT, dummy_x, last_x);
19801 for (s = h; s; s = s->next)
19802 s->background_filled_p = 1;
19803 compute_overhangs_and_x (t, head->x, 1);
19804 prepend_glyph_string_lists (&head, &tail, h, t);
19805 }
19806
19807 /* Append glyphs strings for glyphs following the last glyph
19808 string tail that are overwritten by tail. The background of
19809 these strings has to be drawn because tail's foreground draws
19810 over it. */
19811 i = right_overwritten (tail);
19812 if (i >= 0)
19813 {
19814 BUILD_GLYPH_STRINGS (end, i, h, t,
19815 DRAW_NORMAL_TEXT, x, last_x);
19816 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19817 append_glyph_string_lists (&head, &tail, h, t);
19818 clip_tail = tail;
19819 }
19820
19821 /* Append glyph strings for glyphs following the last glyph
19822 string tail that overwrite tail. The foreground of such
19823 glyphs has to be drawn because it writes into the background
19824 of tail. The background must not be drawn because it could
19825 paint over the foreground of following glyphs. */
19826 i = right_overwriting (tail);
19827 if (i >= 0)
19828 {
19829 clip_tail = tail;
19830 BUILD_GLYPH_STRINGS (end, i, h, t,
19831 DRAW_NORMAL_TEXT, x, last_x);
19832 for (s = h; s; s = s->next)
19833 s->background_filled_p = 1;
19834 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19835 append_glyph_string_lists (&head, &tail, h, t);
19836 }
19837 if (clip_head || clip_tail)
19838 for (s = head; s; s = s->next)
19839 {
19840 s->clip_head = clip_head;
19841 s->clip_tail = clip_tail;
19842 }
19843 }
19844
19845 /* Draw all strings. */
19846 for (s = head; s; s = s->next)
19847 FRAME_RIF (f)->draw_glyph_string (s);
19848
19849 if (area == TEXT_AREA
19850 && !row->full_width_p
19851 /* When drawing overlapping rows, only the glyph strings'
19852 foreground is drawn, which doesn't erase a cursor
19853 completely. */
19854 && !overlaps)
19855 {
19856 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19857 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19858 : (tail ? tail->x + tail->background_width : x));
19859
19860 int text_left = window_box_left (w, TEXT_AREA);
19861 x0 -= text_left;
19862 x1 -= text_left;
19863
19864 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19865 row->y, MATRIX_ROW_BOTTOM_Y (row));
19866 }
19867
19868 /* Value is the x-position up to which drawn, relative to AREA of W.
19869 This doesn't include parts drawn because of overhangs. */
19870 if (row->full_width_p)
19871 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19872 else
19873 x_reached -= window_box_left (w, area);
19874
19875 RELEASE_HDC (hdc, f);
19876
19877 return x_reached;
19878 }
19879
19880 /* Expand row matrix if too narrow. Don't expand if area
19881 is not present. */
19882
19883 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19884 { \
19885 if (!fonts_changed_p \
19886 && (it->glyph_row->glyphs[area] \
19887 < it->glyph_row->glyphs[area + 1])) \
19888 { \
19889 it->w->ncols_scale_factor++; \
19890 fonts_changed_p = 1; \
19891 } \
19892 }
19893
19894 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19895 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19896
19897 static INLINE void
19898 append_glyph (it)
19899 struct it *it;
19900 {
19901 struct glyph *glyph;
19902 enum glyph_row_area area = it->area;
19903
19904 xassert (it->glyph_row);
19905 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19906
19907 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19908 if (glyph < it->glyph_row->glyphs[area + 1])
19909 {
19910 glyph->charpos = CHARPOS (it->position);
19911 glyph->object = it->object;
19912 glyph->pixel_width = it->pixel_width;
19913 glyph->ascent = it->ascent;
19914 glyph->descent = it->descent;
19915 glyph->voffset = it->voffset;
19916 glyph->type = CHAR_GLYPH;
19917 glyph->multibyte_p = it->multibyte_p;
19918 glyph->left_box_line_p = it->start_of_box_run_p;
19919 glyph->right_box_line_p = it->end_of_box_run_p;
19920 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19921 || it->phys_descent > it->descent);
19922 glyph->padding_p = 0;
19923 glyph->glyph_not_available_p = it->glyph_not_available_p;
19924 glyph->face_id = it->face_id;
19925 glyph->u.ch = it->char_to_display;
19926 glyph->slice = null_glyph_slice;
19927 glyph->font_type = FONT_TYPE_UNKNOWN;
19928 ++it->glyph_row->used[area];
19929 }
19930 else
19931 IT_EXPAND_MATRIX_WIDTH (it, area);
19932 }
19933
19934 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19935 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19936
19937 static INLINE void
19938 append_composite_glyph (it)
19939 struct it *it;
19940 {
19941 struct glyph *glyph;
19942 enum glyph_row_area area = it->area;
19943
19944 xassert (it->glyph_row);
19945
19946 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19947 if (glyph < it->glyph_row->glyphs[area + 1])
19948 {
19949 glyph->charpos = CHARPOS (it->position);
19950 glyph->object = it->object;
19951 glyph->pixel_width = it->pixel_width;
19952 glyph->ascent = it->ascent;
19953 glyph->descent = it->descent;
19954 glyph->voffset = it->voffset;
19955 glyph->type = COMPOSITE_GLYPH;
19956 glyph->multibyte_p = it->multibyte_p;
19957 glyph->left_box_line_p = it->start_of_box_run_p;
19958 glyph->right_box_line_p = it->end_of_box_run_p;
19959 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19960 || it->phys_descent > it->descent);
19961 glyph->padding_p = 0;
19962 glyph->glyph_not_available_p = 0;
19963 glyph->face_id = it->face_id;
19964 glyph->u.cmp_id = it->cmp_id;
19965 glyph->slice = null_glyph_slice;
19966 glyph->font_type = FONT_TYPE_UNKNOWN;
19967 ++it->glyph_row->used[area];
19968 }
19969 else
19970 IT_EXPAND_MATRIX_WIDTH (it, area);
19971 }
19972
19973
19974 /* Change IT->ascent and IT->height according to the setting of
19975 IT->voffset. */
19976
19977 static INLINE void
19978 take_vertical_position_into_account (it)
19979 struct it *it;
19980 {
19981 if (it->voffset)
19982 {
19983 if (it->voffset < 0)
19984 /* Increase the ascent so that we can display the text higher
19985 in the line. */
19986 it->ascent -= it->voffset;
19987 else
19988 /* Increase the descent so that we can display the text lower
19989 in the line. */
19990 it->descent += it->voffset;
19991 }
19992 }
19993
19994
19995 /* Produce glyphs/get display metrics for the image IT is loaded with.
19996 See the description of struct display_iterator in dispextern.h for
19997 an overview of struct display_iterator. */
19998
19999 static void
20000 produce_image_glyph (it)
20001 struct it *it;
20002 {
20003 struct image *img;
20004 struct face *face;
20005 int glyph_ascent, crop;
20006 struct glyph_slice slice;
20007
20008 xassert (it->what == IT_IMAGE);
20009
20010 face = FACE_FROM_ID (it->f, it->face_id);
20011 xassert (face);
20012 /* Make sure X resources of the face is loaded. */
20013 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20014
20015 if (it->image_id < 0)
20016 {
20017 /* Fringe bitmap. */
20018 it->ascent = it->phys_ascent = 0;
20019 it->descent = it->phys_descent = 0;
20020 it->pixel_width = 0;
20021 it->nglyphs = 0;
20022 return;
20023 }
20024
20025 img = IMAGE_FROM_ID (it->f, it->image_id);
20026 xassert (img);
20027 /* Make sure X resources of the image is loaded. */
20028 prepare_image_for_display (it->f, img);
20029
20030 slice.x = slice.y = 0;
20031 slice.width = img->width;
20032 slice.height = img->height;
20033
20034 if (INTEGERP (it->slice.x))
20035 slice.x = XINT (it->slice.x);
20036 else if (FLOATP (it->slice.x))
20037 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20038
20039 if (INTEGERP (it->slice.y))
20040 slice.y = XINT (it->slice.y);
20041 else if (FLOATP (it->slice.y))
20042 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20043
20044 if (INTEGERP (it->slice.width))
20045 slice.width = XINT (it->slice.width);
20046 else if (FLOATP (it->slice.width))
20047 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20048
20049 if (INTEGERP (it->slice.height))
20050 slice.height = XINT (it->slice.height);
20051 else if (FLOATP (it->slice.height))
20052 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20053
20054 if (slice.x >= img->width)
20055 slice.x = img->width;
20056 if (slice.y >= img->height)
20057 slice.y = img->height;
20058 if (slice.x + slice.width >= img->width)
20059 slice.width = img->width - slice.x;
20060 if (slice.y + slice.height > img->height)
20061 slice.height = img->height - slice.y;
20062
20063 if (slice.width == 0 || slice.height == 0)
20064 return;
20065
20066 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20067
20068 it->descent = slice.height - glyph_ascent;
20069 if (slice.y == 0)
20070 it->descent += img->vmargin;
20071 if (slice.y + slice.height == img->height)
20072 it->descent += img->vmargin;
20073 it->phys_descent = it->descent;
20074
20075 it->pixel_width = slice.width;
20076 if (slice.x == 0)
20077 it->pixel_width += img->hmargin;
20078 if (slice.x + slice.width == img->width)
20079 it->pixel_width += img->hmargin;
20080
20081 /* It's quite possible for images to have an ascent greater than
20082 their height, so don't get confused in that case. */
20083 if (it->descent < 0)
20084 it->descent = 0;
20085
20086 #if 0 /* this breaks image tiling */
20087 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20088 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20089 if (face_ascent > it->ascent)
20090 it->ascent = it->phys_ascent = face_ascent;
20091 #endif
20092
20093 it->nglyphs = 1;
20094
20095 if (face->box != FACE_NO_BOX)
20096 {
20097 if (face->box_line_width > 0)
20098 {
20099 if (slice.y == 0)
20100 it->ascent += face->box_line_width;
20101 if (slice.y + slice.height == img->height)
20102 it->descent += face->box_line_width;
20103 }
20104
20105 if (it->start_of_box_run_p && slice.x == 0)
20106 it->pixel_width += eabs (face->box_line_width);
20107 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20108 it->pixel_width += eabs (face->box_line_width);
20109 }
20110
20111 take_vertical_position_into_account (it);
20112
20113 /* Automatically crop wide image glyphs at right edge so we can
20114 draw the cursor on same display row. */
20115 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20116 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20117 {
20118 it->pixel_width -= crop;
20119 slice.width -= crop;
20120 }
20121
20122 if (it->glyph_row)
20123 {
20124 struct glyph *glyph;
20125 enum glyph_row_area area = it->area;
20126
20127 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20128 if (glyph < it->glyph_row->glyphs[area + 1])
20129 {
20130 glyph->charpos = CHARPOS (it->position);
20131 glyph->object = it->object;
20132 glyph->pixel_width = it->pixel_width;
20133 glyph->ascent = glyph_ascent;
20134 glyph->descent = it->descent;
20135 glyph->voffset = it->voffset;
20136 glyph->type = IMAGE_GLYPH;
20137 glyph->multibyte_p = it->multibyte_p;
20138 glyph->left_box_line_p = it->start_of_box_run_p;
20139 glyph->right_box_line_p = it->end_of_box_run_p;
20140 glyph->overlaps_vertically_p = 0;
20141 glyph->padding_p = 0;
20142 glyph->glyph_not_available_p = 0;
20143 glyph->face_id = it->face_id;
20144 glyph->u.img_id = img->id;
20145 glyph->slice = slice;
20146 glyph->font_type = FONT_TYPE_UNKNOWN;
20147 ++it->glyph_row->used[area];
20148 }
20149 else
20150 IT_EXPAND_MATRIX_WIDTH (it, area);
20151 }
20152 }
20153
20154
20155 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20156 of the glyph, WIDTH and HEIGHT are the width and height of the
20157 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20158
20159 static void
20160 append_stretch_glyph (it, object, width, height, ascent)
20161 struct it *it;
20162 Lisp_Object object;
20163 int width, height;
20164 int ascent;
20165 {
20166 struct glyph *glyph;
20167 enum glyph_row_area area = it->area;
20168
20169 xassert (ascent >= 0 && ascent <= height);
20170
20171 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20172 if (glyph < it->glyph_row->glyphs[area + 1])
20173 {
20174 glyph->charpos = CHARPOS (it->position);
20175 glyph->object = object;
20176 glyph->pixel_width = width;
20177 glyph->ascent = ascent;
20178 glyph->descent = height - ascent;
20179 glyph->voffset = it->voffset;
20180 glyph->type = STRETCH_GLYPH;
20181 glyph->multibyte_p = it->multibyte_p;
20182 glyph->left_box_line_p = it->start_of_box_run_p;
20183 glyph->right_box_line_p = it->end_of_box_run_p;
20184 glyph->overlaps_vertically_p = 0;
20185 glyph->padding_p = 0;
20186 glyph->glyph_not_available_p = 0;
20187 glyph->face_id = it->face_id;
20188 glyph->u.stretch.ascent = ascent;
20189 glyph->u.stretch.height = height;
20190 glyph->slice = null_glyph_slice;
20191 glyph->font_type = FONT_TYPE_UNKNOWN;
20192 ++it->glyph_row->used[area];
20193 }
20194 else
20195 IT_EXPAND_MATRIX_WIDTH (it, area);
20196 }
20197
20198
20199 /* Produce a stretch glyph for iterator IT. IT->object is the value
20200 of the glyph property displayed. The value must be a list
20201 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20202 being recognized:
20203
20204 1. `:width WIDTH' specifies that the space should be WIDTH *
20205 canonical char width wide. WIDTH may be an integer or floating
20206 point number.
20207
20208 2. `:relative-width FACTOR' specifies that the width of the stretch
20209 should be computed from the width of the first character having the
20210 `glyph' property, and should be FACTOR times that width.
20211
20212 3. `:align-to HPOS' specifies that the space should be wide enough
20213 to reach HPOS, a value in canonical character units.
20214
20215 Exactly one of the above pairs must be present.
20216
20217 4. `:height HEIGHT' specifies that the height of the stretch produced
20218 should be HEIGHT, measured in canonical character units.
20219
20220 5. `:relative-height FACTOR' specifies that the height of the
20221 stretch should be FACTOR times the height of the characters having
20222 the glyph property.
20223
20224 Either none or exactly one of 4 or 5 must be present.
20225
20226 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20227 of the stretch should be used for the ascent of the stretch.
20228 ASCENT must be in the range 0 <= ASCENT <= 100. */
20229
20230 static void
20231 produce_stretch_glyph (it)
20232 struct it *it;
20233 {
20234 /* (space :width WIDTH :height HEIGHT ...) */
20235 Lisp_Object prop, plist;
20236 int width = 0, height = 0, align_to = -1;
20237 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20238 int ascent = 0;
20239 double tem;
20240 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20241 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20242
20243 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20244
20245 /* List should start with `space'. */
20246 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20247 plist = XCDR (it->object);
20248
20249 /* Compute the width of the stretch. */
20250 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20251 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20252 {
20253 /* Absolute width `:width WIDTH' specified and valid. */
20254 zero_width_ok_p = 1;
20255 width = (int)tem;
20256 }
20257 else if (prop = Fplist_get (plist, QCrelative_width),
20258 NUMVAL (prop) > 0)
20259 {
20260 /* Relative width `:relative-width FACTOR' specified and valid.
20261 Compute the width of the characters having the `glyph'
20262 property. */
20263 struct it it2;
20264 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20265
20266 it2 = *it;
20267 if (it->multibyte_p)
20268 {
20269 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20270 - IT_BYTEPOS (*it));
20271 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20272 }
20273 else
20274 it2.c = *p, it2.len = 1;
20275
20276 it2.glyph_row = NULL;
20277 it2.what = IT_CHARACTER;
20278 x_produce_glyphs (&it2);
20279 width = NUMVAL (prop) * it2.pixel_width;
20280 }
20281 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20282 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20283 {
20284 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20285 align_to = (align_to < 0
20286 ? 0
20287 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20288 else if (align_to < 0)
20289 align_to = window_box_left_offset (it->w, TEXT_AREA);
20290 width = max (0, (int)tem + align_to - it->current_x);
20291 zero_width_ok_p = 1;
20292 }
20293 else
20294 /* Nothing specified -> width defaults to canonical char width. */
20295 width = FRAME_COLUMN_WIDTH (it->f);
20296
20297 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20298 width = 1;
20299
20300 /* Compute height. */
20301 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20302 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20303 {
20304 height = (int)tem;
20305 zero_height_ok_p = 1;
20306 }
20307 else if (prop = Fplist_get (plist, QCrelative_height),
20308 NUMVAL (prop) > 0)
20309 height = FONT_HEIGHT (font) * NUMVAL (prop);
20310 else
20311 height = FONT_HEIGHT (font);
20312
20313 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20314 height = 1;
20315
20316 /* Compute percentage of height used for ascent. If
20317 `:ascent ASCENT' is present and valid, use that. Otherwise,
20318 derive the ascent from the font in use. */
20319 if (prop = Fplist_get (plist, QCascent),
20320 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20321 ascent = height * NUMVAL (prop) / 100.0;
20322 else if (!NILP (prop)
20323 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20324 ascent = min (max (0, (int)tem), height);
20325 else
20326 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20327
20328 if (width > 0 && !it->truncate_lines_p
20329 && it->current_x + width > it->last_visible_x)
20330 width = it->last_visible_x - it->current_x - 1;
20331
20332 if (width > 0 && height > 0 && it->glyph_row)
20333 {
20334 Lisp_Object object = it->stack[it->sp - 1].string;
20335 if (!STRINGP (object))
20336 object = it->w->buffer;
20337 append_stretch_glyph (it, object, width, height, ascent);
20338 }
20339
20340 it->pixel_width = width;
20341 it->ascent = it->phys_ascent = ascent;
20342 it->descent = it->phys_descent = height - it->ascent;
20343 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20344
20345 take_vertical_position_into_account (it);
20346 }
20347
20348 /* Get line-height and line-spacing property at point.
20349 If line-height has format (HEIGHT TOTAL), return TOTAL
20350 in TOTAL_HEIGHT. */
20351
20352 static Lisp_Object
20353 get_line_height_property (it, prop)
20354 struct it *it;
20355 Lisp_Object prop;
20356 {
20357 Lisp_Object position;
20358
20359 if (STRINGP (it->object))
20360 position = make_number (IT_STRING_CHARPOS (*it));
20361 else if (BUFFERP (it->object))
20362 position = make_number (IT_CHARPOS (*it));
20363 else
20364 return Qnil;
20365
20366 return Fget_char_property (position, prop, it->object);
20367 }
20368
20369 /* Calculate line-height and line-spacing properties.
20370 An integer value specifies explicit pixel value.
20371 A float value specifies relative value to current face height.
20372 A cons (float . face-name) specifies relative value to
20373 height of specified face font.
20374
20375 Returns height in pixels, or nil. */
20376
20377
20378 static Lisp_Object
20379 calc_line_height_property (it, val, font, boff, override)
20380 struct it *it;
20381 Lisp_Object val;
20382 XFontStruct *font;
20383 int boff, override;
20384 {
20385 Lisp_Object face_name = Qnil;
20386 int ascent, descent, height;
20387
20388 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20389 return val;
20390
20391 if (CONSP (val))
20392 {
20393 face_name = XCAR (val);
20394 val = XCDR (val);
20395 if (!NUMBERP (val))
20396 val = make_number (1);
20397 if (NILP (face_name))
20398 {
20399 height = it->ascent + it->descent;
20400 goto scale;
20401 }
20402 }
20403
20404 if (NILP (face_name))
20405 {
20406 font = FRAME_FONT (it->f);
20407 boff = FRAME_BASELINE_OFFSET (it->f);
20408 }
20409 else if (EQ (face_name, Qt))
20410 {
20411 override = 0;
20412 }
20413 else
20414 {
20415 int face_id;
20416 struct face *face;
20417 struct font_info *font_info;
20418
20419 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20420 if (face_id < 0)
20421 return make_number (-1);
20422
20423 face = FACE_FROM_ID (it->f, face_id);
20424 font = face->font;
20425 if (font == NULL)
20426 return make_number (-1);
20427
20428 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20429 boff = font_info->baseline_offset;
20430 if (font_info->vertical_centering)
20431 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20432 }
20433
20434 ascent = FONT_BASE (font) + boff;
20435 descent = FONT_DESCENT (font) - boff;
20436
20437 if (override)
20438 {
20439 it->override_ascent = ascent;
20440 it->override_descent = descent;
20441 it->override_boff = boff;
20442 }
20443
20444 height = ascent + descent;
20445
20446 scale:
20447 if (FLOATP (val))
20448 height = (int)(XFLOAT_DATA (val) * height);
20449 else if (INTEGERP (val))
20450 height *= XINT (val);
20451
20452 return make_number (height);
20453 }
20454
20455
20456 /* RIF:
20457 Produce glyphs/get display metrics for the display element IT is
20458 loaded with. See the description of struct it in dispextern.h
20459 for an overview of struct it. */
20460
20461 void
20462 x_produce_glyphs (it)
20463 struct it *it;
20464 {
20465 int extra_line_spacing = it->extra_line_spacing;
20466
20467 it->glyph_not_available_p = 0;
20468
20469 if (it->what == IT_CHARACTER)
20470 {
20471 XChar2b char2b;
20472 XFontStruct *font;
20473 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20474 XCharStruct *pcm;
20475 int font_not_found_p;
20476 struct font_info *font_info;
20477 int boff; /* baseline offset */
20478 /* We may change it->multibyte_p upon unibyte<->multibyte
20479 conversion. So, save the current value now and restore it
20480 later.
20481
20482 Note: It seems that we don't have to record multibyte_p in
20483 struct glyph because the character code itself tells if or
20484 not the character is multibyte. Thus, in the future, we must
20485 consider eliminating the field `multibyte_p' in the struct
20486 glyph. */
20487 int saved_multibyte_p = it->multibyte_p;
20488
20489 /* Maybe translate single-byte characters to multibyte, or the
20490 other way. */
20491 it->char_to_display = it->c;
20492 if (!ASCII_BYTE_P (it->c))
20493 {
20494 if (unibyte_display_via_language_environment
20495 && SINGLE_BYTE_CHAR_P (it->c)
20496 && (it->c >= 0240
20497 || !NILP (Vnonascii_translation_table)))
20498 {
20499 it->char_to_display = unibyte_char_to_multibyte (it->c);
20500 it->multibyte_p = 1;
20501 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20502 face = FACE_FROM_ID (it->f, it->face_id);
20503 }
20504 else if (!SINGLE_BYTE_CHAR_P (it->c)
20505 && !it->multibyte_p)
20506 {
20507 it->multibyte_p = 1;
20508 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20509 face = FACE_FROM_ID (it->f, it->face_id);
20510 }
20511 }
20512
20513 /* Get font to use. Encode IT->char_to_display. */
20514 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20515 &char2b, it->multibyte_p, 0);
20516 font = face->font;
20517
20518 /* When no suitable font found, use the default font. */
20519 font_not_found_p = font == NULL;
20520 if (font_not_found_p)
20521 {
20522 font = FRAME_FONT (it->f);
20523 boff = FRAME_BASELINE_OFFSET (it->f);
20524 font_info = NULL;
20525 }
20526 else
20527 {
20528 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20529 boff = font_info->baseline_offset;
20530 if (font_info->vertical_centering)
20531 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20532 }
20533
20534 if (it->char_to_display >= ' '
20535 && (!it->multibyte_p || it->char_to_display < 128))
20536 {
20537 /* Either unibyte or ASCII. */
20538 int stretched_p;
20539
20540 it->nglyphs = 1;
20541
20542 pcm = FRAME_RIF (it->f)->per_char_metric
20543 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20544
20545 if (it->override_ascent >= 0)
20546 {
20547 it->ascent = it->override_ascent;
20548 it->descent = it->override_descent;
20549 boff = it->override_boff;
20550 }
20551 else
20552 {
20553 it->ascent = FONT_BASE (font) + boff;
20554 it->descent = FONT_DESCENT (font) - boff;
20555 }
20556
20557 if (pcm)
20558 {
20559 it->phys_ascent = pcm->ascent + boff;
20560 it->phys_descent = pcm->descent - boff;
20561 it->pixel_width = pcm->width;
20562 }
20563 else
20564 {
20565 it->glyph_not_available_p = 1;
20566 it->phys_ascent = it->ascent;
20567 it->phys_descent = it->descent;
20568 it->pixel_width = FONT_WIDTH (font);
20569 }
20570
20571 if (it->constrain_row_ascent_descent_p)
20572 {
20573 if (it->descent > it->max_descent)
20574 {
20575 it->ascent += it->descent - it->max_descent;
20576 it->descent = it->max_descent;
20577 }
20578 if (it->ascent > it->max_ascent)
20579 {
20580 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20581 it->ascent = it->max_ascent;
20582 }
20583 it->phys_ascent = min (it->phys_ascent, it->ascent);
20584 it->phys_descent = min (it->phys_descent, it->descent);
20585 extra_line_spacing = 0;
20586 }
20587
20588 /* If this is a space inside a region of text with
20589 `space-width' property, change its width. */
20590 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20591 if (stretched_p)
20592 it->pixel_width *= XFLOATINT (it->space_width);
20593
20594 /* If face has a box, add the box thickness to the character
20595 height. If character has a box line to the left and/or
20596 right, add the box line width to the character's width. */
20597 if (face->box != FACE_NO_BOX)
20598 {
20599 int thick = face->box_line_width;
20600
20601 if (thick > 0)
20602 {
20603 it->ascent += thick;
20604 it->descent += thick;
20605 }
20606 else
20607 thick = -thick;
20608
20609 if (it->start_of_box_run_p)
20610 it->pixel_width += thick;
20611 if (it->end_of_box_run_p)
20612 it->pixel_width += thick;
20613 }
20614
20615 /* If face has an overline, add the height of the overline
20616 (1 pixel) and a 1 pixel margin to the character height. */
20617 if (face->overline_p)
20618 it->ascent += overline_margin;
20619
20620 if (it->constrain_row_ascent_descent_p)
20621 {
20622 if (it->ascent > it->max_ascent)
20623 it->ascent = it->max_ascent;
20624 if (it->descent > it->max_descent)
20625 it->descent = it->max_descent;
20626 }
20627
20628 take_vertical_position_into_account (it);
20629
20630 /* If we have to actually produce glyphs, do it. */
20631 if (it->glyph_row)
20632 {
20633 if (stretched_p)
20634 {
20635 /* Translate a space with a `space-width' property
20636 into a stretch glyph. */
20637 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20638 / FONT_HEIGHT (font));
20639 append_stretch_glyph (it, it->object, it->pixel_width,
20640 it->ascent + it->descent, ascent);
20641 }
20642 else
20643 append_glyph (it);
20644
20645 /* If characters with lbearing or rbearing are displayed
20646 in this line, record that fact in a flag of the
20647 glyph row. This is used to optimize X output code. */
20648 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20649 it->glyph_row->contains_overlapping_glyphs_p = 1;
20650 }
20651 }
20652 else if (it->char_to_display == '\n')
20653 {
20654 /* A newline has no width but we need the height of the line.
20655 But if previous part of the line set a height, don't
20656 increase that height */
20657
20658 Lisp_Object height;
20659 Lisp_Object total_height = Qnil;
20660
20661 it->override_ascent = -1;
20662 it->pixel_width = 0;
20663 it->nglyphs = 0;
20664
20665 height = get_line_height_property(it, Qline_height);
20666 /* Split (line-height total-height) list */
20667 if (CONSP (height)
20668 && CONSP (XCDR (height))
20669 && NILP (XCDR (XCDR (height))))
20670 {
20671 total_height = XCAR (XCDR (height));
20672 height = XCAR (height);
20673 }
20674 height = calc_line_height_property(it, height, font, boff, 1);
20675
20676 if (it->override_ascent >= 0)
20677 {
20678 it->ascent = it->override_ascent;
20679 it->descent = it->override_descent;
20680 boff = it->override_boff;
20681 }
20682 else
20683 {
20684 it->ascent = FONT_BASE (font) + boff;
20685 it->descent = FONT_DESCENT (font) - boff;
20686 }
20687
20688 if (EQ (height, Qt))
20689 {
20690 if (it->descent > it->max_descent)
20691 {
20692 it->ascent += it->descent - it->max_descent;
20693 it->descent = it->max_descent;
20694 }
20695 if (it->ascent > it->max_ascent)
20696 {
20697 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20698 it->ascent = it->max_ascent;
20699 }
20700 it->phys_ascent = min (it->phys_ascent, it->ascent);
20701 it->phys_descent = min (it->phys_descent, it->descent);
20702 it->constrain_row_ascent_descent_p = 1;
20703 extra_line_spacing = 0;
20704 }
20705 else
20706 {
20707 Lisp_Object spacing;
20708
20709 it->phys_ascent = it->ascent;
20710 it->phys_descent = it->descent;
20711
20712 if ((it->max_ascent > 0 || it->max_descent > 0)
20713 && face->box != FACE_NO_BOX
20714 && face->box_line_width > 0)
20715 {
20716 it->ascent += face->box_line_width;
20717 it->descent += face->box_line_width;
20718 }
20719 if (!NILP (height)
20720 && XINT (height) > it->ascent + it->descent)
20721 it->ascent = XINT (height) - it->descent;
20722
20723 if (!NILP (total_height))
20724 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20725 else
20726 {
20727 spacing = get_line_height_property(it, Qline_spacing);
20728 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20729 }
20730 if (INTEGERP (spacing))
20731 {
20732 extra_line_spacing = XINT (spacing);
20733 if (!NILP (total_height))
20734 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20735 }
20736 }
20737 }
20738 else if (it->char_to_display == '\t')
20739 {
20740 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20741 int x = it->current_x + it->continuation_lines_width;
20742 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20743
20744 /* If the distance from the current position to the next tab
20745 stop is less than a space character width, use the
20746 tab stop after that. */
20747 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20748 next_tab_x += tab_width;
20749
20750 it->pixel_width = next_tab_x - x;
20751 it->nglyphs = 1;
20752 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20753 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20754
20755 if (it->glyph_row)
20756 {
20757 append_stretch_glyph (it, it->object, it->pixel_width,
20758 it->ascent + it->descent, it->ascent);
20759 }
20760 }
20761 else
20762 {
20763 /* A multi-byte character. Assume that the display width of the
20764 character is the width of the character multiplied by the
20765 width of the font. */
20766
20767 /* If we found a font, this font should give us the right
20768 metrics. If we didn't find a font, use the frame's
20769 default font and calculate the width of the character
20770 from the charset width; this is what old redisplay code
20771 did. */
20772
20773 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20774 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20775
20776 if (font_not_found_p || !pcm)
20777 {
20778 int charset = CHAR_CHARSET (it->char_to_display);
20779
20780 it->glyph_not_available_p = 1;
20781 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20782 * CHARSET_WIDTH (charset));
20783 it->phys_ascent = FONT_BASE (font) + boff;
20784 it->phys_descent = FONT_DESCENT (font) - boff;
20785 }
20786 else
20787 {
20788 it->pixel_width = pcm->width;
20789 it->phys_ascent = pcm->ascent + boff;
20790 it->phys_descent = pcm->descent - boff;
20791 if (it->glyph_row
20792 && (pcm->lbearing < 0
20793 || pcm->rbearing > pcm->width))
20794 it->glyph_row->contains_overlapping_glyphs_p = 1;
20795 }
20796 it->nglyphs = 1;
20797 it->ascent = FONT_BASE (font) + boff;
20798 it->descent = FONT_DESCENT (font) - boff;
20799 if (face->box != FACE_NO_BOX)
20800 {
20801 int thick = face->box_line_width;
20802
20803 if (thick > 0)
20804 {
20805 it->ascent += thick;
20806 it->descent += thick;
20807 }
20808 else
20809 thick = - thick;
20810
20811 if (it->start_of_box_run_p)
20812 it->pixel_width += thick;
20813 if (it->end_of_box_run_p)
20814 it->pixel_width += thick;
20815 }
20816
20817 /* If face has an overline, add the height of the overline
20818 (1 pixel) and a 1 pixel margin to the character height. */
20819 if (face->overline_p)
20820 it->ascent += overline_margin;
20821
20822 take_vertical_position_into_account (it);
20823
20824 if (it->glyph_row)
20825 append_glyph (it);
20826 }
20827 it->multibyte_p = saved_multibyte_p;
20828 }
20829 else if (it->what == IT_COMPOSITION)
20830 {
20831 /* Note: A composition is represented as one glyph in the
20832 glyph matrix. There are no padding glyphs. */
20833 XChar2b char2b;
20834 XFontStruct *font;
20835 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20836 XCharStruct *pcm;
20837 int font_not_found_p;
20838 struct font_info *font_info;
20839 int boff; /* baseline offset */
20840 struct composition *cmp = composition_table[it->cmp_id];
20841
20842 /* Maybe translate single-byte characters to multibyte. */
20843 it->char_to_display = it->c;
20844 if (unibyte_display_via_language_environment
20845 && SINGLE_BYTE_CHAR_P (it->c)
20846 && (it->c >= 0240
20847 || (it->c >= 0200
20848 && !NILP (Vnonascii_translation_table))))
20849 {
20850 it->char_to_display = unibyte_char_to_multibyte (it->c);
20851 }
20852
20853 /* Get face and font to use. Encode IT->char_to_display. */
20854 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20855 face = FACE_FROM_ID (it->f, it->face_id);
20856 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20857 &char2b, it->multibyte_p, 0);
20858 font = face->font;
20859
20860 /* When no suitable font found, use the default font. */
20861 font_not_found_p = font == NULL;
20862 if (font_not_found_p)
20863 {
20864 font = FRAME_FONT (it->f);
20865 boff = FRAME_BASELINE_OFFSET (it->f);
20866 font_info = NULL;
20867 }
20868 else
20869 {
20870 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20871 boff = font_info->baseline_offset;
20872 if (font_info->vertical_centering)
20873 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20874 }
20875
20876 /* There are no padding glyphs, so there is only one glyph to
20877 produce for the composition. Important is that pixel_width,
20878 ascent and descent are the values of what is drawn by
20879 draw_glyphs (i.e. the values of the overall glyphs composed). */
20880 it->nglyphs = 1;
20881
20882 /* If we have not yet calculated pixel size data of glyphs of
20883 the composition for the current face font, calculate them
20884 now. Theoretically, we have to check all fonts for the
20885 glyphs, but that requires much time and memory space. So,
20886 here we check only the font of the first glyph. This leads
20887 to incorrect display very rarely, and C-l (recenter) can
20888 correct the display anyway. */
20889 if (cmp->font != (void *) font)
20890 {
20891 /* Ascent and descent of the font of the first character of
20892 this composition (adjusted by baseline offset). Ascent
20893 and descent of overall glyphs should not be less than
20894 them respectively. */
20895 int font_ascent = FONT_BASE (font) + boff;
20896 int font_descent = FONT_DESCENT (font) - boff;
20897 /* Bounding box of the overall glyphs. */
20898 int leftmost, rightmost, lowest, highest;
20899 int i, width, ascent, descent;
20900
20901 cmp->font = (void *) font;
20902
20903 /* Initialize the bounding box. */
20904 if (font_info
20905 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20906 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20907 {
20908 width = pcm->width;
20909 ascent = pcm->ascent;
20910 descent = pcm->descent;
20911 }
20912 else
20913 {
20914 width = FONT_WIDTH (font);
20915 ascent = FONT_BASE (font);
20916 descent = FONT_DESCENT (font);
20917 }
20918
20919 rightmost = width;
20920 lowest = - descent + boff;
20921 highest = ascent + boff;
20922 leftmost = 0;
20923
20924 if (font_info
20925 && font_info->default_ascent
20926 && CHAR_TABLE_P (Vuse_default_ascent)
20927 && !NILP (Faref (Vuse_default_ascent,
20928 make_number (it->char_to_display))))
20929 highest = font_info->default_ascent + boff;
20930
20931 /* Draw the first glyph at the normal position. It may be
20932 shifted to right later if some other glyphs are drawn at
20933 the left. */
20934 cmp->offsets[0] = 0;
20935 cmp->offsets[1] = boff;
20936
20937 /* Set cmp->offsets for the remaining glyphs. */
20938 for (i = 1; i < cmp->glyph_len; i++)
20939 {
20940 int left, right, btm, top;
20941 int ch = COMPOSITION_GLYPH (cmp, i);
20942 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20943
20944 face = FACE_FROM_ID (it->f, face_id);
20945 get_char_face_and_encoding (it->f, ch, face->id,
20946 &char2b, it->multibyte_p, 0);
20947 font = face->font;
20948 if (font == NULL)
20949 {
20950 font = FRAME_FONT (it->f);
20951 boff = FRAME_BASELINE_OFFSET (it->f);
20952 font_info = NULL;
20953 }
20954 else
20955 {
20956 font_info
20957 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20958 boff = font_info->baseline_offset;
20959 if (font_info->vertical_centering)
20960 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20961 }
20962
20963 if (font_info
20964 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20965 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20966 {
20967 width = pcm->width;
20968 ascent = pcm->ascent;
20969 descent = pcm->descent;
20970 }
20971 else
20972 {
20973 width = FONT_WIDTH (font);
20974 ascent = 1;
20975 descent = 0;
20976 }
20977
20978 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20979 {
20980 /* Relative composition with or without
20981 alternate chars. */
20982 left = (leftmost + rightmost - width) / 2;
20983 btm = - descent + boff;
20984 if (font_info && font_info->relative_compose
20985 && (! CHAR_TABLE_P (Vignore_relative_composition)
20986 || NILP (Faref (Vignore_relative_composition,
20987 make_number (ch)))))
20988 {
20989
20990 if (- descent >= font_info->relative_compose)
20991 /* One extra pixel between two glyphs. */
20992 btm = highest + 1;
20993 else if (ascent <= 0)
20994 /* One extra pixel between two glyphs. */
20995 btm = lowest - 1 - ascent - descent;
20996 }
20997 }
20998 else
20999 {
21000 /* A composition rule is specified by an integer
21001 value that encodes global and new reference
21002 points (GREF and NREF). GREF and NREF are
21003 specified by numbers as below:
21004
21005 0---1---2 -- ascent
21006 | |
21007 | |
21008 | |
21009 9--10--11 -- center
21010 | |
21011 ---3---4---5--- baseline
21012 | |
21013 6---7---8 -- descent
21014 */
21015 int rule = COMPOSITION_RULE (cmp, i);
21016 int gref, nref, grefx, grefy, nrefx, nrefy;
21017
21018 COMPOSITION_DECODE_RULE (rule, gref, nref);
21019 grefx = gref % 3, nrefx = nref % 3;
21020 grefy = gref / 3, nrefy = nref / 3;
21021
21022 left = (leftmost
21023 + grefx * (rightmost - leftmost) / 2
21024 - nrefx * width / 2);
21025 btm = ((grefy == 0 ? highest
21026 : grefy == 1 ? 0
21027 : grefy == 2 ? lowest
21028 : (highest + lowest) / 2)
21029 - (nrefy == 0 ? ascent + descent
21030 : nrefy == 1 ? descent - boff
21031 : nrefy == 2 ? 0
21032 : (ascent + descent) / 2));
21033 }
21034
21035 cmp->offsets[i * 2] = left;
21036 cmp->offsets[i * 2 + 1] = btm + descent;
21037
21038 /* Update the bounding box of the overall glyphs. */
21039 right = left + width;
21040 top = btm + descent + ascent;
21041 if (left < leftmost)
21042 leftmost = left;
21043 if (right > rightmost)
21044 rightmost = right;
21045 if (top > highest)
21046 highest = top;
21047 if (btm < lowest)
21048 lowest = btm;
21049 }
21050
21051 /* If there are glyphs whose x-offsets are negative,
21052 shift all glyphs to the right and make all x-offsets
21053 non-negative. */
21054 if (leftmost < 0)
21055 {
21056 for (i = 0; i < cmp->glyph_len; i++)
21057 cmp->offsets[i * 2] -= leftmost;
21058 rightmost -= leftmost;
21059 }
21060
21061 cmp->pixel_width = rightmost;
21062 cmp->ascent = highest;
21063 cmp->descent = - lowest;
21064 if (cmp->ascent < font_ascent)
21065 cmp->ascent = font_ascent;
21066 if (cmp->descent < font_descent)
21067 cmp->descent = font_descent;
21068 }
21069
21070 it->pixel_width = cmp->pixel_width;
21071 it->ascent = it->phys_ascent = cmp->ascent;
21072 it->descent = it->phys_descent = cmp->descent;
21073
21074 if (face->box != FACE_NO_BOX)
21075 {
21076 int thick = face->box_line_width;
21077
21078 if (thick > 0)
21079 {
21080 it->ascent += thick;
21081 it->descent += thick;
21082 }
21083 else
21084 thick = - thick;
21085
21086 if (it->start_of_box_run_p)
21087 it->pixel_width += thick;
21088 if (it->end_of_box_run_p)
21089 it->pixel_width += thick;
21090 }
21091
21092 /* If face has an overline, add the height of the overline
21093 (1 pixel) and a 1 pixel margin to the character height. */
21094 if (face->overline_p)
21095 it->ascent += overline_margin;
21096
21097 take_vertical_position_into_account (it);
21098
21099 if (it->glyph_row)
21100 append_composite_glyph (it);
21101 }
21102 else if (it->what == IT_IMAGE)
21103 produce_image_glyph (it);
21104 else if (it->what == IT_STRETCH)
21105 produce_stretch_glyph (it);
21106
21107 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21108 because this isn't true for images with `:ascent 100'. */
21109 xassert (it->ascent >= 0 && it->descent >= 0);
21110 if (it->area == TEXT_AREA)
21111 it->current_x += it->pixel_width;
21112
21113 if (extra_line_spacing > 0)
21114 {
21115 it->descent += extra_line_spacing;
21116 if (extra_line_spacing > it->max_extra_line_spacing)
21117 it->max_extra_line_spacing = extra_line_spacing;
21118 }
21119
21120 it->max_ascent = max (it->max_ascent, it->ascent);
21121 it->max_descent = max (it->max_descent, it->descent);
21122 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21123 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21124 }
21125
21126 /* EXPORT for RIF:
21127 Output LEN glyphs starting at START at the nominal cursor position.
21128 Advance the nominal cursor over the text. The global variable
21129 updated_window contains the window being updated, updated_row is
21130 the glyph row being updated, and updated_area is the area of that
21131 row being updated. */
21132
21133 void
21134 x_write_glyphs (start, len)
21135 struct glyph *start;
21136 int len;
21137 {
21138 int x, hpos;
21139
21140 xassert (updated_window && updated_row);
21141 BLOCK_INPUT;
21142
21143 /* Write glyphs. */
21144
21145 hpos = start - updated_row->glyphs[updated_area];
21146 x = draw_glyphs (updated_window, output_cursor.x,
21147 updated_row, updated_area,
21148 hpos, hpos + len,
21149 DRAW_NORMAL_TEXT, 0);
21150
21151 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21152 if (updated_area == TEXT_AREA
21153 && updated_window->phys_cursor_on_p
21154 && updated_window->phys_cursor.vpos == output_cursor.vpos
21155 && updated_window->phys_cursor.hpos >= hpos
21156 && updated_window->phys_cursor.hpos < hpos + len)
21157 updated_window->phys_cursor_on_p = 0;
21158
21159 UNBLOCK_INPUT;
21160
21161 /* Advance the output cursor. */
21162 output_cursor.hpos += len;
21163 output_cursor.x = x;
21164 }
21165
21166
21167 /* EXPORT for RIF:
21168 Insert LEN glyphs from START at the nominal cursor position. */
21169
21170 void
21171 x_insert_glyphs (start, len)
21172 struct glyph *start;
21173 int len;
21174 {
21175 struct frame *f;
21176 struct window *w;
21177 int line_height, shift_by_width, shifted_region_width;
21178 struct glyph_row *row;
21179 struct glyph *glyph;
21180 int frame_x, frame_y, hpos;
21181
21182 xassert (updated_window && updated_row);
21183 BLOCK_INPUT;
21184 w = updated_window;
21185 f = XFRAME (WINDOW_FRAME (w));
21186
21187 /* Get the height of the line we are in. */
21188 row = updated_row;
21189 line_height = row->height;
21190
21191 /* Get the width of the glyphs to insert. */
21192 shift_by_width = 0;
21193 for (glyph = start; glyph < start + len; ++glyph)
21194 shift_by_width += glyph->pixel_width;
21195
21196 /* Get the width of the region to shift right. */
21197 shifted_region_width = (window_box_width (w, updated_area)
21198 - output_cursor.x
21199 - shift_by_width);
21200
21201 /* Shift right. */
21202 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21203 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21204
21205 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21206 line_height, shift_by_width);
21207
21208 /* Write the glyphs. */
21209 hpos = start - row->glyphs[updated_area];
21210 draw_glyphs (w, output_cursor.x, row, updated_area,
21211 hpos, hpos + len,
21212 DRAW_NORMAL_TEXT, 0);
21213
21214 /* Advance the output cursor. */
21215 output_cursor.hpos += len;
21216 output_cursor.x += shift_by_width;
21217 UNBLOCK_INPUT;
21218 }
21219
21220
21221 /* EXPORT for RIF:
21222 Erase the current text line from the nominal cursor position
21223 (inclusive) to pixel column TO_X (exclusive). The idea is that
21224 everything from TO_X onward is already erased.
21225
21226 TO_X is a pixel position relative to updated_area of
21227 updated_window. TO_X == -1 means clear to the end of this area. */
21228
21229 void
21230 x_clear_end_of_line (to_x)
21231 int to_x;
21232 {
21233 struct frame *f;
21234 struct window *w = updated_window;
21235 int max_x, min_y, max_y;
21236 int from_x, from_y, to_y;
21237
21238 xassert (updated_window && updated_row);
21239 f = XFRAME (w->frame);
21240
21241 if (updated_row->full_width_p)
21242 max_x = WINDOW_TOTAL_WIDTH (w);
21243 else
21244 max_x = window_box_width (w, updated_area);
21245 max_y = window_text_bottom_y (w);
21246
21247 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21248 of window. For TO_X > 0, truncate to end of drawing area. */
21249 if (to_x == 0)
21250 return;
21251 else if (to_x < 0)
21252 to_x = max_x;
21253 else
21254 to_x = min (to_x, max_x);
21255
21256 to_y = min (max_y, output_cursor.y + updated_row->height);
21257
21258 /* Notice if the cursor will be cleared by this operation. */
21259 if (!updated_row->full_width_p)
21260 notice_overwritten_cursor (w, updated_area,
21261 output_cursor.x, -1,
21262 updated_row->y,
21263 MATRIX_ROW_BOTTOM_Y (updated_row));
21264
21265 from_x = output_cursor.x;
21266
21267 /* Translate to frame coordinates. */
21268 if (updated_row->full_width_p)
21269 {
21270 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21271 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21272 }
21273 else
21274 {
21275 int area_left = window_box_left (w, updated_area);
21276 from_x += area_left;
21277 to_x += area_left;
21278 }
21279
21280 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21281 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21282 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21283
21284 /* Prevent inadvertently clearing to end of the X window. */
21285 if (to_x > from_x && to_y > from_y)
21286 {
21287 BLOCK_INPUT;
21288 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21289 to_x - from_x, to_y - from_y);
21290 UNBLOCK_INPUT;
21291 }
21292 }
21293
21294 #endif /* HAVE_WINDOW_SYSTEM */
21295
21296
21297 \f
21298 /***********************************************************************
21299 Cursor types
21300 ***********************************************************************/
21301
21302 /* Value is the internal representation of the specified cursor type
21303 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21304 of the bar cursor. */
21305
21306 static enum text_cursor_kinds
21307 get_specified_cursor_type (arg, width)
21308 Lisp_Object arg;
21309 int *width;
21310 {
21311 enum text_cursor_kinds type;
21312
21313 if (NILP (arg))
21314 return NO_CURSOR;
21315
21316 if (EQ (arg, Qbox))
21317 return FILLED_BOX_CURSOR;
21318
21319 if (EQ (arg, Qhollow))
21320 return HOLLOW_BOX_CURSOR;
21321
21322 if (EQ (arg, Qbar))
21323 {
21324 *width = 2;
21325 return BAR_CURSOR;
21326 }
21327
21328 if (CONSP (arg)
21329 && EQ (XCAR (arg), Qbar)
21330 && INTEGERP (XCDR (arg))
21331 && XINT (XCDR (arg)) >= 0)
21332 {
21333 *width = XINT (XCDR (arg));
21334 return BAR_CURSOR;
21335 }
21336
21337 if (EQ (arg, Qhbar))
21338 {
21339 *width = 2;
21340 return HBAR_CURSOR;
21341 }
21342
21343 if (CONSP (arg)
21344 && EQ (XCAR (arg), Qhbar)
21345 && INTEGERP (XCDR (arg))
21346 && XINT (XCDR (arg)) >= 0)
21347 {
21348 *width = XINT (XCDR (arg));
21349 return HBAR_CURSOR;
21350 }
21351
21352 /* Treat anything unknown as "hollow box cursor".
21353 It was bad to signal an error; people have trouble fixing
21354 .Xdefaults with Emacs, when it has something bad in it. */
21355 type = HOLLOW_BOX_CURSOR;
21356
21357 return type;
21358 }
21359
21360 /* Set the default cursor types for specified frame. */
21361 void
21362 set_frame_cursor_types (f, arg)
21363 struct frame *f;
21364 Lisp_Object arg;
21365 {
21366 int width;
21367 Lisp_Object tem;
21368
21369 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21370 FRAME_CURSOR_WIDTH (f) = width;
21371
21372 /* By default, set up the blink-off state depending on the on-state. */
21373
21374 tem = Fassoc (arg, Vblink_cursor_alist);
21375 if (!NILP (tem))
21376 {
21377 FRAME_BLINK_OFF_CURSOR (f)
21378 = get_specified_cursor_type (XCDR (tem), &width);
21379 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21380 }
21381 else
21382 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21383 }
21384
21385
21386 /* Return the cursor we want to be displayed in window W. Return
21387 width of bar/hbar cursor through WIDTH arg. Return with
21388 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21389 (i.e. if the `system caret' should track this cursor).
21390
21391 In a mini-buffer window, we want the cursor only to appear if we
21392 are reading input from this window. For the selected window, we
21393 want the cursor type given by the frame parameter or buffer local
21394 setting of cursor-type. If explicitly marked off, draw no cursor.
21395 In all other cases, we want a hollow box cursor. */
21396
21397 static enum text_cursor_kinds
21398 get_window_cursor_type (w, glyph, width, active_cursor)
21399 struct window *w;
21400 struct glyph *glyph;
21401 int *width;
21402 int *active_cursor;
21403 {
21404 struct frame *f = XFRAME (w->frame);
21405 struct buffer *b = XBUFFER (w->buffer);
21406 int cursor_type = DEFAULT_CURSOR;
21407 Lisp_Object alt_cursor;
21408 int non_selected = 0;
21409
21410 *active_cursor = 1;
21411
21412 /* Echo area */
21413 if (cursor_in_echo_area
21414 && FRAME_HAS_MINIBUF_P (f)
21415 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21416 {
21417 if (w == XWINDOW (echo_area_window))
21418 {
21419 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21420 {
21421 *width = FRAME_CURSOR_WIDTH (f);
21422 return FRAME_DESIRED_CURSOR (f);
21423 }
21424 else
21425 return get_specified_cursor_type (b->cursor_type, width);
21426 }
21427
21428 *active_cursor = 0;
21429 non_selected = 1;
21430 }
21431
21432 /* Detect a nonselected window or nonselected frame. */
21433 else if (w != XWINDOW (f->selected_window)
21434 #ifdef HAVE_WINDOW_SYSTEM
21435 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21436 #endif
21437 )
21438 {
21439 *active_cursor = 0;
21440
21441 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21442 return NO_CURSOR;
21443
21444 non_selected = 1;
21445 }
21446
21447 /* Never display a cursor in a window in which cursor-type is nil. */
21448 if (NILP (b->cursor_type))
21449 return NO_CURSOR;
21450
21451 /* Get the normal cursor type for this window. */
21452 if (EQ (b->cursor_type, Qt))
21453 {
21454 cursor_type = FRAME_DESIRED_CURSOR (f);
21455 *width = FRAME_CURSOR_WIDTH (f);
21456 }
21457 else
21458 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21459
21460 /* Use cursor-in-non-selected-windows instead
21461 for non-selected window or frame. */
21462 if (non_selected)
21463 {
21464 alt_cursor = b->cursor_in_non_selected_windows;
21465 if (!EQ (Qt, alt_cursor))
21466 return get_specified_cursor_type (alt_cursor, width);
21467 /* t means modify the normal cursor type. */
21468 if (cursor_type == FILLED_BOX_CURSOR)
21469 cursor_type = HOLLOW_BOX_CURSOR;
21470 else if (cursor_type == BAR_CURSOR && *width > 1)
21471 --*width;
21472 return cursor_type;
21473 }
21474
21475 /* Use normal cursor if not blinked off. */
21476 if (!w->cursor_off_p)
21477 {
21478 #ifdef HAVE_WINDOW_SYSTEM
21479 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21480 {
21481 if (cursor_type == FILLED_BOX_CURSOR)
21482 {
21483 /* Using a block cursor on large images can be very annoying.
21484 So use a hollow cursor for "large" images.
21485 If image is not transparent (no mask), also use hollow cursor. */
21486 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21487 if (img != NULL && IMAGEP (img->spec))
21488 {
21489 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21490 where N = size of default frame font size.
21491 This should cover most of the "tiny" icons people may use. */
21492 if (!img->mask
21493 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21494 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21495 cursor_type = HOLLOW_BOX_CURSOR;
21496 }
21497 }
21498 else if (cursor_type != NO_CURSOR)
21499 {
21500 /* Display current only supports BOX and HOLLOW cursors for images.
21501 So for now, unconditionally use a HOLLOW cursor when cursor is
21502 not a solid box cursor. */
21503 cursor_type = HOLLOW_BOX_CURSOR;
21504 }
21505 }
21506 #endif
21507 return cursor_type;
21508 }
21509
21510 /* Cursor is blinked off, so determine how to "toggle" it. */
21511
21512 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21513 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21514 return get_specified_cursor_type (XCDR (alt_cursor), width);
21515
21516 /* Then see if frame has specified a specific blink off cursor type. */
21517 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21518 {
21519 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21520 return FRAME_BLINK_OFF_CURSOR (f);
21521 }
21522
21523 #if 0
21524 /* Some people liked having a permanently visible blinking cursor,
21525 while others had very strong opinions against it. So it was
21526 decided to remove it. KFS 2003-09-03 */
21527
21528 /* Finally perform built-in cursor blinking:
21529 filled box <-> hollow box
21530 wide [h]bar <-> narrow [h]bar
21531 narrow [h]bar <-> no cursor
21532 other type <-> no cursor */
21533
21534 if (cursor_type == FILLED_BOX_CURSOR)
21535 return HOLLOW_BOX_CURSOR;
21536
21537 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21538 {
21539 *width = 1;
21540 return cursor_type;
21541 }
21542 #endif
21543
21544 return NO_CURSOR;
21545 }
21546
21547
21548 #ifdef HAVE_WINDOW_SYSTEM
21549
21550 /* Notice when the text cursor of window W has been completely
21551 overwritten by a drawing operation that outputs glyphs in AREA
21552 starting at X0 and ending at X1 in the line starting at Y0 and
21553 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21554 the rest of the line after X0 has been written. Y coordinates
21555 are window-relative. */
21556
21557 static void
21558 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21559 struct window *w;
21560 enum glyph_row_area area;
21561 int x0, y0, x1, y1;
21562 {
21563 int cx0, cx1, cy0, cy1;
21564 struct glyph_row *row;
21565
21566 if (!w->phys_cursor_on_p)
21567 return;
21568 if (area != TEXT_AREA)
21569 return;
21570
21571 if (w->phys_cursor.vpos < 0
21572 || w->phys_cursor.vpos >= w->current_matrix->nrows
21573 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21574 !(row->enabled_p && row->displays_text_p)))
21575 return;
21576
21577 if (row->cursor_in_fringe_p)
21578 {
21579 row->cursor_in_fringe_p = 0;
21580 draw_fringe_bitmap (w, row, 0);
21581 w->phys_cursor_on_p = 0;
21582 return;
21583 }
21584
21585 cx0 = w->phys_cursor.x;
21586 cx1 = cx0 + w->phys_cursor_width;
21587 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21588 return;
21589
21590 /* The cursor image will be completely removed from the
21591 screen if the output area intersects the cursor area in
21592 y-direction. When we draw in [y0 y1[, and some part of
21593 the cursor is at y < y0, that part must have been drawn
21594 before. When scrolling, the cursor is erased before
21595 actually scrolling, so we don't come here. When not
21596 scrolling, the rows above the old cursor row must have
21597 changed, and in this case these rows must have written
21598 over the cursor image.
21599
21600 Likewise if part of the cursor is below y1, with the
21601 exception of the cursor being in the first blank row at
21602 the buffer and window end because update_text_area
21603 doesn't draw that row. (Except when it does, but
21604 that's handled in update_text_area.) */
21605
21606 cy0 = w->phys_cursor.y;
21607 cy1 = cy0 + w->phys_cursor_height;
21608 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21609 return;
21610
21611 w->phys_cursor_on_p = 0;
21612 }
21613
21614 #endif /* HAVE_WINDOW_SYSTEM */
21615
21616 \f
21617 /************************************************************************
21618 Mouse Face
21619 ************************************************************************/
21620
21621 #ifdef HAVE_WINDOW_SYSTEM
21622
21623 /* EXPORT for RIF:
21624 Fix the display of area AREA of overlapping row ROW in window W
21625 with respect to the overlapping part OVERLAPS. */
21626
21627 void
21628 x_fix_overlapping_area (w, row, area, overlaps)
21629 struct window *w;
21630 struct glyph_row *row;
21631 enum glyph_row_area area;
21632 int overlaps;
21633 {
21634 int i, x;
21635
21636 BLOCK_INPUT;
21637
21638 x = 0;
21639 for (i = 0; i < row->used[area];)
21640 {
21641 if (row->glyphs[area][i].overlaps_vertically_p)
21642 {
21643 int start = i, start_x = x;
21644
21645 do
21646 {
21647 x += row->glyphs[area][i].pixel_width;
21648 ++i;
21649 }
21650 while (i < row->used[area]
21651 && row->glyphs[area][i].overlaps_vertically_p);
21652
21653 draw_glyphs (w, start_x, row, area,
21654 start, i,
21655 DRAW_NORMAL_TEXT, overlaps);
21656 }
21657 else
21658 {
21659 x += row->glyphs[area][i].pixel_width;
21660 ++i;
21661 }
21662 }
21663
21664 UNBLOCK_INPUT;
21665 }
21666
21667
21668 /* EXPORT:
21669 Draw the cursor glyph of window W in glyph row ROW. See the
21670 comment of draw_glyphs for the meaning of HL. */
21671
21672 void
21673 draw_phys_cursor_glyph (w, row, hl)
21674 struct window *w;
21675 struct glyph_row *row;
21676 enum draw_glyphs_face hl;
21677 {
21678 /* If cursor hpos is out of bounds, don't draw garbage. This can
21679 happen in mini-buffer windows when switching between echo area
21680 glyphs and mini-buffer. */
21681 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21682 {
21683 int on_p = w->phys_cursor_on_p;
21684 int x1;
21685 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21686 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21687 hl, 0);
21688 w->phys_cursor_on_p = on_p;
21689
21690 if (hl == DRAW_CURSOR)
21691 w->phys_cursor_width = x1 - w->phys_cursor.x;
21692 /* When we erase the cursor, and ROW is overlapped by other
21693 rows, make sure that these overlapping parts of other rows
21694 are redrawn. */
21695 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21696 {
21697 w->phys_cursor_width = x1 - w->phys_cursor.x;
21698
21699 if (row > w->current_matrix->rows
21700 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21701 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21702 OVERLAPS_ERASED_CURSOR);
21703
21704 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21705 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21706 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21707 OVERLAPS_ERASED_CURSOR);
21708 }
21709 }
21710 }
21711
21712
21713 /* EXPORT:
21714 Erase the image of a cursor of window W from the screen. */
21715
21716 void
21717 erase_phys_cursor (w)
21718 struct window *w;
21719 {
21720 struct frame *f = XFRAME (w->frame);
21721 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21722 int hpos = w->phys_cursor.hpos;
21723 int vpos = w->phys_cursor.vpos;
21724 int mouse_face_here_p = 0;
21725 struct glyph_matrix *active_glyphs = w->current_matrix;
21726 struct glyph_row *cursor_row;
21727 struct glyph *cursor_glyph;
21728 enum draw_glyphs_face hl;
21729
21730 /* No cursor displayed or row invalidated => nothing to do on the
21731 screen. */
21732 if (w->phys_cursor_type == NO_CURSOR)
21733 goto mark_cursor_off;
21734
21735 /* VPOS >= active_glyphs->nrows means that window has been resized.
21736 Don't bother to erase the cursor. */
21737 if (vpos >= active_glyphs->nrows)
21738 goto mark_cursor_off;
21739
21740 /* If row containing cursor is marked invalid, there is nothing we
21741 can do. */
21742 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21743 if (!cursor_row->enabled_p)
21744 goto mark_cursor_off;
21745
21746 /* If line spacing is > 0, old cursor may only be partially visible in
21747 window after split-window. So adjust visible height. */
21748 cursor_row->visible_height = min (cursor_row->visible_height,
21749 window_text_bottom_y (w) - cursor_row->y);
21750
21751 /* If row is completely invisible, don't attempt to delete a cursor which
21752 isn't there. This can happen if cursor is at top of a window, and
21753 we switch to a buffer with a header line in that window. */
21754 if (cursor_row->visible_height <= 0)
21755 goto mark_cursor_off;
21756
21757 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21758 if (cursor_row->cursor_in_fringe_p)
21759 {
21760 cursor_row->cursor_in_fringe_p = 0;
21761 draw_fringe_bitmap (w, cursor_row, 0);
21762 goto mark_cursor_off;
21763 }
21764
21765 /* This can happen when the new row is shorter than the old one.
21766 In this case, either draw_glyphs or clear_end_of_line
21767 should have cleared the cursor. Note that we wouldn't be
21768 able to erase the cursor in this case because we don't have a
21769 cursor glyph at hand. */
21770 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21771 goto mark_cursor_off;
21772
21773 /* If the cursor is in the mouse face area, redisplay that when
21774 we clear the cursor. */
21775 if (! NILP (dpyinfo->mouse_face_window)
21776 && w == XWINDOW (dpyinfo->mouse_face_window)
21777 && (vpos > dpyinfo->mouse_face_beg_row
21778 || (vpos == dpyinfo->mouse_face_beg_row
21779 && hpos >= dpyinfo->mouse_face_beg_col))
21780 && (vpos < dpyinfo->mouse_face_end_row
21781 || (vpos == dpyinfo->mouse_face_end_row
21782 && hpos < dpyinfo->mouse_face_end_col))
21783 /* Don't redraw the cursor's spot in mouse face if it is at the
21784 end of a line (on a newline). The cursor appears there, but
21785 mouse highlighting does not. */
21786 && cursor_row->used[TEXT_AREA] > hpos)
21787 mouse_face_here_p = 1;
21788
21789 /* Maybe clear the display under the cursor. */
21790 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21791 {
21792 int x, y, left_x;
21793 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21794 int width;
21795
21796 cursor_glyph = get_phys_cursor_glyph (w);
21797 if (cursor_glyph == NULL)
21798 goto mark_cursor_off;
21799
21800 width = cursor_glyph->pixel_width;
21801 left_x = window_box_left_offset (w, TEXT_AREA);
21802 x = w->phys_cursor.x;
21803 if (x < left_x)
21804 width -= left_x - x;
21805 width = min (width, window_box_width (w, TEXT_AREA) - x);
21806 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21807 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21808
21809 if (width > 0)
21810 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21811 }
21812
21813 /* Erase the cursor by redrawing the character underneath it. */
21814 if (mouse_face_here_p)
21815 hl = DRAW_MOUSE_FACE;
21816 else
21817 hl = DRAW_NORMAL_TEXT;
21818 draw_phys_cursor_glyph (w, cursor_row, hl);
21819
21820 mark_cursor_off:
21821 w->phys_cursor_on_p = 0;
21822 w->phys_cursor_type = NO_CURSOR;
21823 }
21824
21825
21826 /* EXPORT:
21827 Display or clear cursor of window W. If ON is zero, clear the
21828 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21829 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21830
21831 void
21832 display_and_set_cursor (w, on, hpos, vpos, x, y)
21833 struct window *w;
21834 int on, hpos, vpos, x, y;
21835 {
21836 struct frame *f = XFRAME (w->frame);
21837 int new_cursor_type;
21838 int new_cursor_width;
21839 int active_cursor;
21840 struct glyph_row *glyph_row;
21841 struct glyph *glyph;
21842
21843 /* This is pointless on invisible frames, and dangerous on garbaged
21844 windows and frames; in the latter case, the frame or window may
21845 be in the midst of changing its size, and x and y may be off the
21846 window. */
21847 if (! FRAME_VISIBLE_P (f)
21848 || FRAME_GARBAGED_P (f)
21849 || vpos >= w->current_matrix->nrows
21850 || hpos >= w->current_matrix->matrix_w)
21851 return;
21852
21853 /* If cursor is off and we want it off, return quickly. */
21854 if (!on && !w->phys_cursor_on_p)
21855 return;
21856
21857 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21858 /* If cursor row is not enabled, we don't really know where to
21859 display the cursor. */
21860 if (!glyph_row->enabled_p)
21861 {
21862 w->phys_cursor_on_p = 0;
21863 return;
21864 }
21865
21866 glyph = NULL;
21867 if (!glyph_row->exact_window_width_line_p
21868 || hpos < glyph_row->used[TEXT_AREA])
21869 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21870
21871 xassert (interrupt_input_blocked);
21872
21873 /* Set new_cursor_type to the cursor we want to be displayed. */
21874 new_cursor_type = get_window_cursor_type (w, glyph,
21875 &new_cursor_width, &active_cursor);
21876
21877 /* If cursor is currently being shown and we don't want it to be or
21878 it is in the wrong place, or the cursor type is not what we want,
21879 erase it. */
21880 if (w->phys_cursor_on_p
21881 && (!on
21882 || w->phys_cursor.x != x
21883 || w->phys_cursor.y != y
21884 || new_cursor_type != w->phys_cursor_type
21885 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21886 && new_cursor_width != w->phys_cursor_width)))
21887 erase_phys_cursor (w);
21888
21889 /* Don't check phys_cursor_on_p here because that flag is only set
21890 to zero in some cases where we know that the cursor has been
21891 completely erased, to avoid the extra work of erasing the cursor
21892 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21893 still not be visible, or it has only been partly erased. */
21894 if (on)
21895 {
21896 w->phys_cursor_ascent = glyph_row->ascent;
21897 w->phys_cursor_height = glyph_row->height;
21898
21899 /* Set phys_cursor_.* before x_draw_.* is called because some
21900 of them may need the information. */
21901 w->phys_cursor.x = x;
21902 w->phys_cursor.y = glyph_row->y;
21903 w->phys_cursor.hpos = hpos;
21904 w->phys_cursor.vpos = vpos;
21905 }
21906
21907 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21908 new_cursor_type, new_cursor_width,
21909 on, active_cursor);
21910 }
21911
21912
21913 /* Switch the display of W's cursor on or off, according to the value
21914 of ON. */
21915
21916 static void
21917 update_window_cursor (w, on)
21918 struct window *w;
21919 int on;
21920 {
21921 /* Don't update cursor in windows whose frame is in the process
21922 of being deleted. */
21923 if (w->current_matrix)
21924 {
21925 BLOCK_INPUT;
21926 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21927 w->phys_cursor.x, w->phys_cursor.y);
21928 UNBLOCK_INPUT;
21929 }
21930 }
21931
21932
21933 /* Call update_window_cursor with parameter ON_P on all leaf windows
21934 in the window tree rooted at W. */
21935
21936 static void
21937 update_cursor_in_window_tree (w, on_p)
21938 struct window *w;
21939 int on_p;
21940 {
21941 while (w)
21942 {
21943 if (!NILP (w->hchild))
21944 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21945 else if (!NILP (w->vchild))
21946 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21947 else
21948 update_window_cursor (w, on_p);
21949
21950 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21951 }
21952 }
21953
21954
21955 /* EXPORT:
21956 Display the cursor on window W, or clear it, according to ON_P.
21957 Don't change the cursor's position. */
21958
21959 void
21960 x_update_cursor (f, on_p)
21961 struct frame *f;
21962 int on_p;
21963 {
21964 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21965 }
21966
21967
21968 /* EXPORT:
21969 Clear the cursor of window W to background color, and mark the
21970 cursor as not shown. This is used when the text where the cursor
21971 is is about to be rewritten. */
21972
21973 void
21974 x_clear_cursor (w)
21975 struct window *w;
21976 {
21977 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21978 update_window_cursor (w, 0);
21979 }
21980
21981
21982 /* EXPORT:
21983 Display the active region described by mouse_face_* according to DRAW. */
21984
21985 void
21986 show_mouse_face (dpyinfo, draw)
21987 Display_Info *dpyinfo;
21988 enum draw_glyphs_face draw;
21989 {
21990 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21991 struct frame *f = XFRAME (WINDOW_FRAME (w));
21992
21993 if (/* If window is in the process of being destroyed, don't bother
21994 to do anything. */
21995 w->current_matrix != NULL
21996 /* Don't update mouse highlight if hidden */
21997 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21998 /* Recognize when we are called to operate on rows that don't exist
21999 anymore. This can happen when a window is split. */
22000 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22001 {
22002 int phys_cursor_on_p = w->phys_cursor_on_p;
22003 struct glyph_row *row, *first, *last;
22004
22005 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22006 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22007
22008 for (row = first; row <= last && row->enabled_p; ++row)
22009 {
22010 int start_hpos, end_hpos, start_x;
22011
22012 /* For all but the first row, the highlight starts at column 0. */
22013 if (row == first)
22014 {
22015 start_hpos = dpyinfo->mouse_face_beg_col;
22016 start_x = dpyinfo->mouse_face_beg_x;
22017 }
22018 else
22019 {
22020 start_hpos = 0;
22021 start_x = 0;
22022 }
22023
22024 if (row == last)
22025 end_hpos = dpyinfo->mouse_face_end_col;
22026 else
22027 {
22028 end_hpos = row->used[TEXT_AREA];
22029 if (draw == DRAW_NORMAL_TEXT)
22030 row->fill_line_p = 1; /* Clear to end of line */
22031 }
22032
22033 if (end_hpos > start_hpos)
22034 {
22035 draw_glyphs (w, start_x, row, TEXT_AREA,
22036 start_hpos, end_hpos,
22037 draw, 0);
22038
22039 row->mouse_face_p
22040 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22041 }
22042 }
22043
22044 /* When we've written over the cursor, arrange for it to
22045 be displayed again. */
22046 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22047 {
22048 BLOCK_INPUT;
22049 display_and_set_cursor (w, 1,
22050 w->phys_cursor.hpos, w->phys_cursor.vpos,
22051 w->phys_cursor.x, w->phys_cursor.y);
22052 UNBLOCK_INPUT;
22053 }
22054 }
22055
22056 /* Change the mouse cursor. */
22057 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22058 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22059 else if (draw == DRAW_MOUSE_FACE)
22060 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22061 else
22062 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22063 }
22064
22065 /* EXPORT:
22066 Clear out the mouse-highlighted active region.
22067 Redraw it un-highlighted first. Value is non-zero if mouse
22068 face was actually drawn unhighlighted. */
22069
22070 int
22071 clear_mouse_face (dpyinfo)
22072 Display_Info *dpyinfo;
22073 {
22074 int cleared = 0;
22075
22076 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22077 {
22078 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22079 cleared = 1;
22080 }
22081
22082 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22083 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22084 dpyinfo->mouse_face_window = Qnil;
22085 dpyinfo->mouse_face_overlay = Qnil;
22086 return cleared;
22087 }
22088
22089
22090 /* EXPORT:
22091 Non-zero if physical cursor of window W is within mouse face. */
22092
22093 int
22094 cursor_in_mouse_face_p (w)
22095 struct window *w;
22096 {
22097 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22098 int in_mouse_face = 0;
22099
22100 if (WINDOWP (dpyinfo->mouse_face_window)
22101 && XWINDOW (dpyinfo->mouse_face_window) == w)
22102 {
22103 int hpos = w->phys_cursor.hpos;
22104 int vpos = w->phys_cursor.vpos;
22105
22106 if (vpos >= dpyinfo->mouse_face_beg_row
22107 && vpos <= dpyinfo->mouse_face_end_row
22108 && (vpos > dpyinfo->mouse_face_beg_row
22109 || hpos >= dpyinfo->mouse_face_beg_col)
22110 && (vpos < dpyinfo->mouse_face_end_row
22111 || hpos < dpyinfo->mouse_face_end_col
22112 || dpyinfo->mouse_face_past_end))
22113 in_mouse_face = 1;
22114 }
22115
22116 return in_mouse_face;
22117 }
22118
22119
22120
22121 \f
22122 /* Find the glyph matrix position of buffer position CHARPOS in window
22123 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22124 current glyphs must be up to date. If CHARPOS is above window
22125 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22126 of last line in W. In the row containing CHARPOS, stop before glyphs
22127 having STOP as object. */
22128
22129 #if 1 /* This is a version of fast_find_position that's more correct
22130 in the presence of hscrolling, for example. I didn't install
22131 it right away because the problem fixed is minor, it failed
22132 in 20.x as well, and I think it's too risky to install
22133 so near the release of 21.1. 2001-09-25 gerd. */
22134
22135 static int
22136 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22137 struct window *w;
22138 int charpos;
22139 int *hpos, *vpos, *x, *y;
22140 Lisp_Object stop;
22141 {
22142 struct glyph_row *row, *first;
22143 struct glyph *glyph, *end;
22144 int past_end = 0;
22145
22146 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22147 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22148 {
22149 *x = first->x;
22150 *y = first->y;
22151 *hpos = 0;
22152 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22153 return 1;
22154 }
22155
22156 row = row_containing_pos (w, charpos, first, NULL, 0);
22157 if (row == NULL)
22158 {
22159 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22160 past_end = 1;
22161 }
22162
22163 /* If whole rows or last part of a row came from a display overlay,
22164 row_containing_pos will skip over such rows because their end pos
22165 equals the start pos of the overlay or interval.
22166
22167 Move back if we have a STOP object and previous row's
22168 end glyph came from STOP. */
22169 if (!NILP (stop))
22170 {
22171 struct glyph_row *prev;
22172 while ((prev = row - 1, prev >= first)
22173 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22174 && prev->used[TEXT_AREA] > 0)
22175 {
22176 struct glyph *beg = prev->glyphs[TEXT_AREA];
22177 glyph = beg + prev->used[TEXT_AREA];
22178 while (--glyph >= beg
22179 && INTEGERP (glyph->object));
22180 if (glyph < beg
22181 || !EQ (stop, glyph->object))
22182 break;
22183 row = prev;
22184 }
22185 }
22186
22187 *x = row->x;
22188 *y = row->y;
22189 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22190
22191 glyph = row->glyphs[TEXT_AREA];
22192 end = glyph + row->used[TEXT_AREA];
22193
22194 /* Skip over glyphs not having an object at the start of the row.
22195 These are special glyphs like truncation marks on terminal
22196 frames. */
22197 if (row->displays_text_p)
22198 while (glyph < end
22199 && INTEGERP (glyph->object)
22200 && !EQ (stop, glyph->object)
22201 && glyph->charpos < 0)
22202 {
22203 *x += glyph->pixel_width;
22204 ++glyph;
22205 }
22206
22207 while (glyph < end
22208 && !INTEGERP (glyph->object)
22209 && !EQ (stop, glyph->object)
22210 && (!BUFFERP (glyph->object)
22211 || glyph->charpos < charpos))
22212 {
22213 *x += glyph->pixel_width;
22214 ++glyph;
22215 }
22216
22217 *hpos = glyph - row->glyphs[TEXT_AREA];
22218 return !past_end;
22219 }
22220
22221 #else /* not 1 */
22222
22223 static int
22224 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22225 struct window *w;
22226 int pos;
22227 int *hpos, *vpos, *x, *y;
22228 Lisp_Object stop;
22229 {
22230 int i;
22231 int lastcol;
22232 int maybe_next_line_p = 0;
22233 int line_start_position;
22234 int yb = window_text_bottom_y (w);
22235 struct glyph_row *row, *best_row;
22236 int row_vpos, best_row_vpos;
22237 int current_x;
22238
22239 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22240 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22241
22242 while (row->y < yb)
22243 {
22244 if (row->used[TEXT_AREA])
22245 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22246 else
22247 line_start_position = 0;
22248
22249 if (line_start_position > pos)
22250 break;
22251 /* If the position sought is the end of the buffer,
22252 don't include the blank lines at the bottom of the window. */
22253 else if (line_start_position == pos
22254 && pos == BUF_ZV (XBUFFER (w->buffer)))
22255 {
22256 maybe_next_line_p = 1;
22257 break;
22258 }
22259 else if (line_start_position > 0)
22260 {
22261 best_row = row;
22262 best_row_vpos = row_vpos;
22263 }
22264
22265 if (row->y + row->height >= yb)
22266 break;
22267
22268 ++row;
22269 ++row_vpos;
22270 }
22271
22272 /* Find the right column within BEST_ROW. */
22273 lastcol = 0;
22274 current_x = best_row->x;
22275 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22276 {
22277 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22278 int charpos = glyph->charpos;
22279
22280 if (BUFFERP (glyph->object))
22281 {
22282 if (charpos == pos)
22283 {
22284 *hpos = i;
22285 *vpos = best_row_vpos;
22286 *x = current_x;
22287 *y = best_row->y;
22288 return 1;
22289 }
22290 else if (charpos > pos)
22291 break;
22292 }
22293 else if (EQ (glyph->object, stop))
22294 break;
22295
22296 if (charpos > 0)
22297 lastcol = i;
22298 current_x += glyph->pixel_width;
22299 }
22300
22301 /* If we're looking for the end of the buffer,
22302 and we didn't find it in the line we scanned,
22303 use the start of the following line. */
22304 if (maybe_next_line_p)
22305 {
22306 ++best_row;
22307 ++best_row_vpos;
22308 lastcol = 0;
22309 current_x = best_row->x;
22310 }
22311
22312 *vpos = best_row_vpos;
22313 *hpos = lastcol + 1;
22314 *x = current_x;
22315 *y = best_row->y;
22316 return 0;
22317 }
22318
22319 #endif /* not 1 */
22320
22321
22322 /* Find the position of the glyph for position POS in OBJECT in
22323 window W's current matrix, and return in *X, *Y the pixel
22324 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22325
22326 RIGHT_P non-zero means return the position of the right edge of the
22327 glyph, RIGHT_P zero means return the left edge position.
22328
22329 If no glyph for POS exists in the matrix, return the position of
22330 the glyph with the next smaller position that is in the matrix, if
22331 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22332 exists in the matrix, return the position of the glyph with the
22333 next larger position in OBJECT.
22334
22335 Value is non-zero if a glyph was found. */
22336
22337 static int
22338 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22339 struct window *w;
22340 int pos;
22341 Lisp_Object object;
22342 int *hpos, *vpos, *x, *y;
22343 int right_p;
22344 {
22345 int yb = window_text_bottom_y (w);
22346 struct glyph_row *r;
22347 struct glyph *best_glyph = NULL;
22348 struct glyph_row *best_row = NULL;
22349 int best_x = 0;
22350
22351 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22352 r->enabled_p && r->y < yb;
22353 ++r)
22354 {
22355 struct glyph *g = r->glyphs[TEXT_AREA];
22356 struct glyph *e = g + r->used[TEXT_AREA];
22357 int gx;
22358
22359 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22360 if (EQ (g->object, object))
22361 {
22362 if (g->charpos == pos)
22363 {
22364 best_glyph = g;
22365 best_x = gx;
22366 best_row = r;
22367 goto found;
22368 }
22369 else if (best_glyph == NULL
22370 || ((eabs (g->charpos - pos)
22371 < eabs (best_glyph->charpos - pos))
22372 && (right_p
22373 ? g->charpos < pos
22374 : g->charpos > pos)))
22375 {
22376 best_glyph = g;
22377 best_x = gx;
22378 best_row = r;
22379 }
22380 }
22381 }
22382
22383 found:
22384
22385 if (best_glyph)
22386 {
22387 *x = best_x;
22388 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22389
22390 if (right_p)
22391 {
22392 *x += best_glyph->pixel_width;
22393 ++*hpos;
22394 }
22395
22396 *y = best_row->y;
22397 *vpos = best_row - w->current_matrix->rows;
22398 }
22399
22400 return best_glyph != NULL;
22401 }
22402
22403
22404 /* See if position X, Y is within a hot-spot of an image. */
22405
22406 static int
22407 on_hot_spot_p (hot_spot, x, y)
22408 Lisp_Object hot_spot;
22409 int x, y;
22410 {
22411 if (!CONSP (hot_spot))
22412 return 0;
22413
22414 if (EQ (XCAR (hot_spot), Qrect))
22415 {
22416 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22417 Lisp_Object rect = XCDR (hot_spot);
22418 Lisp_Object tem;
22419 if (!CONSP (rect))
22420 return 0;
22421 if (!CONSP (XCAR (rect)))
22422 return 0;
22423 if (!CONSP (XCDR (rect)))
22424 return 0;
22425 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22426 return 0;
22427 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22428 return 0;
22429 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22430 return 0;
22431 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22432 return 0;
22433 return 1;
22434 }
22435 else if (EQ (XCAR (hot_spot), Qcircle))
22436 {
22437 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22438 Lisp_Object circ = XCDR (hot_spot);
22439 Lisp_Object lr, lx0, ly0;
22440 if (CONSP (circ)
22441 && CONSP (XCAR (circ))
22442 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22443 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22444 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22445 {
22446 double r = XFLOATINT (lr);
22447 double dx = XINT (lx0) - x;
22448 double dy = XINT (ly0) - y;
22449 return (dx * dx + dy * dy <= r * r);
22450 }
22451 }
22452 else if (EQ (XCAR (hot_spot), Qpoly))
22453 {
22454 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22455 if (VECTORP (XCDR (hot_spot)))
22456 {
22457 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22458 Lisp_Object *poly = v->contents;
22459 int n = v->size;
22460 int i;
22461 int inside = 0;
22462 Lisp_Object lx, ly;
22463 int x0, y0;
22464
22465 /* Need an even number of coordinates, and at least 3 edges. */
22466 if (n < 6 || n & 1)
22467 return 0;
22468
22469 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22470 If count is odd, we are inside polygon. Pixels on edges
22471 may or may not be included depending on actual geometry of the
22472 polygon. */
22473 if ((lx = poly[n-2], !INTEGERP (lx))
22474 || (ly = poly[n-1], !INTEGERP (lx)))
22475 return 0;
22476 x0 = XINT (lx), y0 = XINT (ly);
22477 for (i = 0; i < n; i += 2)
22478 {
22479 int x1 = x0, y1 = y0;
22480 if ((lx = poly[i], !INTEGERP (lx))
22481 || (ly = poly[i+1], !INTEGERP (ly)))
22482 return 0;
22483 x0 = XINT (lx), y0 = XINT (ly);
22484
22485 /* Does this segment cross the X line? */
22486 if (x0 >= x)
22487 {
22488 if (x1 >= x)
22489 continue;
22490 }
22491 else if (x1 < x)
22492 continue;
22493 if (y > y0 && y > y1)
22494 continue;
22495 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22496 inside = !inside;
22497 }
22498 return inside;
22499 }
22500 }
22501 return 0;
22502 }
22503
22504 Lisp_Object
22505 find_hot_spot (map, x, y)
22506 Lisp_Object map;
22507 int x, y;
22508 {
22509 while (CONSP (map))
22510 {
22511 if (CONSP (XCAR (map))
22512 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22513 return XCAR (map);
22514 map = XCDR (map);
22515 }
22516
22517 return Qnil;
22518 }
22519
22520 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22521 3, 3, 0,
22522 doc: /* Lookup in image map MAP coordinates X and Y.
22523 An image map is an alist where each element has the format (AREA ID PLIST).
22524 An AREA is specified as either a rectangle, a circle, or a polygon:
22525 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22526 pixel coordinates of the upper left and bottom right corners.
22527 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22528 and the radius of the circle; r may be a float or integer.
22529 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22530 vector describes one corner in the polygon.
22531 Returns the alist element for the first matching AREA in MAP. */)
22532 (map, x, y)
22533 Lisp_Object map;
22534 Lisp_Object x, y;
22535 {
22536 if (NILP (map))
22537 return Qnil;
22538
22539 CHECK_NUMBER (x);
22540 CHECK_NUMBER (y);
22541
22542 return find_hot_spot (map, XINT (x), XINT (y));
22543 }
22544
22545
22546 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22547 static void
22548 define_frame_cursor1 (f, cursor, pointer)
22549 struct frame *f;
22550 Cursor cursor;
22551 Lisp_Object pointer;
22552 {
22553 /* Do not change cursor shape while dragging mouse. */
22554 if (!NILP (do_mouse_tracking))
22555 return;
22556
22557 if (!NILP (pointer))
22558 {
22559 if (EQ (pointer, Qarrow))
22560 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22561 else if (EQ (pointer, Qhand))
22562 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22563 else if (EQ (pointer, Qtext))
22564 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22565 else if (EQ (pointer, intern ("hdrag")))
22566 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22567 #ifdef HAVE_X_WINDOWS
22568 else if (EQ (pointer, intern ("vdrag")))
22569 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22570 #endif
22571 else if (EQ (pointer, intern ("hourglass")))
22572 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22573 else if (EQ (pointer, Qmodeline))
22574 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22575 else
22576 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22577 }
22578
22579 if (cursor != No_Cursor)
22580 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22581 }
22582
22583 /* Take proper action when mouse has moved to the mode or header line
22584 or marginal area AREA of window W, x-position X and y-position Y.
22585 X is relative to the start of the text display area of W, so the
22586 width of bitmap areas and scroll bars must be subtracted to get a
22587 position relative to the start of the mode line. */
22588
22589 static void
22590 note_mode_line_or_margin_highlight (window, x, y, area)
22591 Lisp_Object window;
22592 int x, y;
22593 enum window_part area;
22594 {
22595 struct window *w = XWINDOW (window);
22596 struct frame *f = XFRAME (w->frame);
22597 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22598 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22599 Lisp_Object pointer = Qnil;
22600 int charpos, dx, dy, width, height;
22601 Lisp_Object string, object = Qnil;
22602 Lisp_Object pos, help;
22603
22604 Lisp_Object mouse_face;
22605 int original_x_pixel = x;
22606 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22607 struct glyph_row *row;
22608
22609 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22610 {
22611 int x0;
22612 struct glyph *end;
22613
22614 string = mode_line_string (w, area, &x, &y, &charpos,
22615 &object, &dx, &dy, &width, &height);
22616
22617 row = (area == ON_MODE_LINE
22618 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22619 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22620
22621 /* Find glyph */
22622 if (row->mode_line_p && row->enabled_p)
22623 {
22624 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22625 end = glyph + row->used[TEXT_AREA];
22626
22627 for (x0 = original_x_pixel;
22628 glyph < end && x0 >= glyph->pixel_width;
22629 ++glyph)
22630 x0 -= glyph->pixel_width;
22631
22632 if (glyph >= end)
22633 glyph = NULL;
22634 }
22635 }
22636 else
22637 {
22638 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22639 string = marginal_area_string (w, area, &x, &y, &charpos,
22640 &object, &dx, &dy, &width, &height);
22641 }
22642
22643 help = Qnil;
22644
22645 if (IMAGEP (object))
22646 {
22647 Lisp_Object image_map, hotspot;
22648 if ((image_map = Fplist_get (XCDR (object), QCmap),
22649 !NILP (image_map))
22650 && (hotspot = find_hot_spot (image_map, dx, dy),
22651 CONSP (hotspot))
22652 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22653 {
22654 Lisp_Object area_id, plist;
22655
22656 area_id = XCAR (hotspot);
22657 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22658 If so, we could look for mouse-enter, mouse-leave
22659 properties in PLIST (and do something...). */
22660 hotspot = XCDR (hotspot);
22661 if (CONSP (hotspot)
22662 && (plist = XCAR (hotspot), CONSP (plist)))
22663 {
22664 pointer = Fplist_get (plist, Qpointer);
22665 if (NILP (pointer))
22666 pointer = Qhand;
22667 help = Fplist_get (plist, Qhelp_echo);
22668 if (!NILP (help))
22669 {
22670 help_echo_string = help;
22671 /* Is this correct? ++kfs */
22672 XSETWINDOW (help_echo_window, w);
22673 help_echo_object = w->buffer;
22674 help_echo_pos = charpos;
22675 }
22676 }
22677 }
22678 if (NILP (pointer))
22679 pointer = Fplist_get (XCDR (object), QCpointer);
22680 }
22681
22682 if (STRINGP (string))
22683 {
22684 pos = make_number (charpos);
22685 /* If we're on a string with `help-echo' text property, arrange
22686 for the help to be displayed. This is done by setting the
22687 global variable help_echo_string to the help string. */
22688 if (NILP (help))
22689 {
22690 help = Fget_text_property (pos, Qhelp_echo, string);
22691 if (!NILP (help))
22692 {
22693 help_echo_string = help;
22694 XSETWINDOW (help_echo_window, w);
22695 help_echo_object = string;
22696 help_echo_pos = charpos;
22697 }
22698 }
22699
22700 if (NILP (pointer))
22701 pointer = Fget_text_property (pos, Qpointer, string);
22702
22703 /* Change the mouse pointer according to what is under X/Y. */
22704 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22705 {
22706 Lisp_Object map;
22707 map = Fget_text_property (pos, Qlocal_map, string);
22708 if (!KEYMAPP (map))
22709 map = Fget_text_property (pos, Qkeymap, string);
22710 if (!KEYMAPP (map))
22711 cursor = dpyinfo->vertical_scroll_bar_cursor;
22712 }
22713
22714 /* Change the mouse face according to what is under X/Y. */
22715 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22716 if (!NILP (mouse_face)
22717 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22718 && glyph)
22719 {
22720 Lisp_Object b, e;
22721
22722 struct glyph * tmp_glyph;
22723
22724 int gpos;
22725 int gseq_length;
22726 int total_pixel_width;
22727 int ignore;
22728
22729 int vpos, hpos;
22730
22731 b = Fprevious_single_property_change (make_number (charpos + 1),
22732 Qmouse_face, string, Qnil);
22733 if (NILP (b))
22734 b = make_number (0);
22735
22736 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22737 if (NILP (e))
22738 e = make_number (SCHARS (string));
22739
22740 /* Calculate the position(glyph position: GPOS) of GLYPH in
22741 displayed string. GPOS is different from CHARPOS.
22742
22743 CHARPOS is the position of glyph in internal string
22744 object. A mode line string format has structures which
22745 is converted to a flatten by emacs lisp interpreter.
22746 The internal string is an element of the structures.
22747 The displayed string is the flatten string. */
22748 gpos = 0;
22749 if (glyph > row_start_glyph)
22750 {
22751 tmp_glyph = glyph - 1;
22752 while (tmp_glyph >= row_start_glyph
22753 && tmp_glyph->charpos >= XINT (b)
22754 && EQ (tmp_glyph->object, glyph->object))
22755 {
22756 tmp_glyph--;
22757 gpos++;
22758 }
22759 }
22760
22761 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22762 displayed string holding GLYPH.
22763
22764 GSEQ_LENGTH is different from SCHARS (STRING).
22765 SCHARS (STRING) returns the length of the internal string. */
22766 for (tmp_glyph = glyph, gseq_length = gpos;
22767 tmp_glyph->charpos < XINT (e);
22768 tmp_glyph++, gseq_length++)
22769 {
22770 if (!EQ (tmp_glyph->object, glyph->object))
22771 break;
22772 }
22773
22774 total_pixel_width = 0;
22775 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22776 total_pixel_width += tmp_glyph->pixel_width;
22777
22778 /* Pre calculation of re-rendering position */
22779 vpos = (x - gpos);
22780 hpos = (area == ON_MODE_LINE
22781 ? (w->current_matrix)->nrows - 1
22782 : 0);
22783
22784 /* If the re-rendering position is included in the last
22785 re-rendering area, we should do nothing. */
22786 if ( EQ (window, dpyinfo->mouse_face_window)
22787 && dpyinfo->mouse_face_beg_col <= vpos
22788 && vpos < dpyinfo->mouse_face_end_col
22789 && dpyinfo->mouse_face_beg_row == hpos )
22790 return;
22791
22792 if (clear_mouse_face (dpyinfo))
22793 cursor = No_Cursor;
22794
22795 dpyinfo->mouse_face_beg_col = vpos;
22796 dpyinfo->mouse_face_beg_row = hpos;
22797
22798 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22799 dpyinfo->mouse_face_beg_y = 0;
22800
22801 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22802 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22803
22804 dpyinfo->mouse_face_end_x = 0;
22805 dpyinfo->mouse_face_end_y = 0;
22806
22807 dpyinfo->mouse_face_past_end = 0;
22808 dpyinfo->mouse_face_window = window;
22809
22810 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22811 charpos,
22812 0, 0, 0, &ignore,
22813 glyph->face_id, 1);
22814 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22815
22816 if (NILP (pointer))
22817 pointer = Qhand;
22818 }
22819 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22820 clear_mouse_face (dpyinfo);
22821 }
22822 define_frame_cursor1 (f, cursor, pointer);
22823 }
22824
22825
22826 /* EXPORT:
22827 Take proper action when the mouse has moved to position X, Y on
22828 frame F as regards highlighting characters that have mouse-face
22829 properties. Also de-highlighting chars where the mouse was before.
22830 X and Y can be negative or out of range. */
22831
22832 void
22833 note_mouse_highlight (f, x, y)
22834 struct frame *f;
22835 int x, y;
22836 {
22837 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22838 enum window_part part;
22839 Lisp_Object window;
22840 struct window *w;
22841 Cursor cursor = No_Cursor;
22842 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22843 struct buffer *b;
22844
22845 /* When a menu is active, don't highlight because this looks odd. */
22846 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22847 if (popup_activated ())
22848 return;
22849 #endif
22850
22851 if (NILP (Vmouse_highlight)
22852 || !f->glyphs_initialized_p)
22853 return;
22854
22855 dpyinfo->mouse_face_mouse_x = x;
22856 dpyinfo->mouse_face_mouse_y = y;
22857 dpyinfo->mouse_face_mouse_frame = f;
22858
22859 if (dpyinfo->mouse_face_defer)
22860 return;
22861
22862 if (gc_in_progress)
22863 {
22864 dpyinfo->mouse_face_deferred_gc = 1;
22865 return;
22866 }
22867
22868 /* Which window is that in? */
22869 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22870
22871 /* If we were displaying active text in another window, clear that.
22872 Also clear if we move out of text area in same window. */
22873 if (! EQ (window, dpyinfo->mouse_face_window)
22874 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22875 && !NILP (dpyinfo->mouse_face_window)))
22876 clear_mouse_face (dpyinfo);
22877
22878 /* Not on a window -> return. */
22879 if (!WINDOWP (window))
22880 return;
22881
22882 /* Reset help_echo_string. It will get recomputed below. */
22883 help_echo_string = Qnil;
22884
22885 /* Convert to window-relative pixel coordinates. */
22886 w = XWINDOW (window);
22887 frame_to_window_pixel_xy (w, &x, &y);
22888
22889 /* Handle tool-bar window differently since it doesn't display a
22890 buffer. */
22891 if (EQ (window, f->tool_bar_window))
22892 {
22893 note_tool_bar_highlight (f, x, y);
22894 return;
22895 }
22896
22897 /* Mouse is on the mode, header line or margin? */
22898 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22899 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22900 {
22901 note_mode_line_or_margin_highlight (window, x, y, part);
22902 return;
22903 }
22904
22905 if (part == ON_VERTICAL_BORDER)
22906 {
22907 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22908 help_echo_string = build_string ("drag-mouse-1: resize");
22909 }
22910 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22911 || part == ON_SCROLL_BAR)
22912 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22913 else
22914 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22915
22916 /* Are we in a window whose display is up to date?
22917 And verify the buffer's text has not changed. */
22918 b = XBUFFER (w->buffer);
22919 if (part == ON_TEXT
22920 && EQ (w->window_end_valid, w->buffer)
22921 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22922 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22923 {
22924 int hpos, vpos, pos, i, dx, dy, area;
22925 struct glyph *glyph;
22926 Lisp_Object object;
22927 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22928 Lisp_Object *overlay_vec = NULL;
22929 int noverlays;
22930 struct buffer *obuf;
22931 int obegv, ozv, same_region;
22932
22933 /* Find the glyph under X/Y. */
22934 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22935
22936 /* Look for :pointer property on image. */
22937 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22938 {
22939 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22940 if (img != NULL && IMAGEP (img->spec))
22941 {
22942 Lisp_Object image_map, hotspot;
22943 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22944 !NILP (image_map))
22945 && (hotspot = find_hot_spot (image_map,
22946 glyph->slice.x + dx,
22947 glyph->slice.y + dy),
22948 CONSP (hotspot))
22949 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22950 {
22951 Lisp_Object area_id, plist;
22952
22953 area_id = XCAR (hotspot);
22954 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22955 If so, we could look for mouse-enter, mouse-leave
22956 properties in PLIST (and do something...). */
22957 hotspot = XCDR (hotspot);
22958 if (CONSP (hotspot)
22959 && (plist = XCAR (hotspot), CONSP (plist)))
22960 {
22961 pointer = Fplist_get (plist, Qpointer);
22962 if (NILP (pointer))
22963 pointer = Qhand;
22964 help_echo_string = Fplist_get (plist, Qhelp_echo);
22965 if (!NILP (help_echo_string))
22966 {
22967 help_echo_window = window;
22968 help_echo_object = glyph->object;
22969 help_echo_pos = glyph->charpos;
22970 }
22971 }
22972 }
22973 if (NILP (pointer))
22974 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22975 }
22976 }
22977
22978 /* Clear mouse face if X/Y not over text. */
22979 if (glyph == NULL
22980 || area != TEXT_AREA
22981 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22982 {
22983 if (clear_mouse_face (dpyinfo))
22984 cursor = No_Cursor;
22985 if (NILP (pointer))
22986 {
22987 if (area != TEXT_AREA)
22988 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22989 else
22990 pointer = Vvoid_text_area_pointer;
22991 }
22992 goto set_cursor;
22993 }
22994
22995 pos = glyph->charpos;
22996 object = glyph->object;
22997 if (!STRINGP (object) && !BUFFERP (object))
22998 goto set_cursor;
22999
23000 /* If we get an out-of-range value, return now; avoid an error. */
23001 if (BUFFERP (object) && pos > BUF_Z (b))
23002 goto set_cursor;
23003
23004 /* Make the window's buffer temporarily current for
23005 overlays_at and compute_char_face. */
23006 obuf = current_buffer;
23007 current_buffer = b;
23008 obegv = BEGV;
23009 ozv = ZV;
23010 BEGV = BEG;
23011 ZV = Z;
23012
23013 /* Is this char mouse-active or does it have help-echo? */
23014 position = make_number (pos);
23015
23016 if (BUFFERP (object))
23017 {
23018 /* Put all the overlays we want in a vector in overlay_vec. */
23019 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23020 /* Sort overlays into increasing priority order. */
23021 noverlays = sort_overlays (overlay_vec, noverlays, w);
23022 }
23023 else
23024 noverlays = 0;
23025
23026 same_region = (EQ (window, dpyinfo->mouse_face_window)
23027 && vpos >= dpyinfo->mouse_face_beg_row
23028 && vpos <= dpyinfo->mouse_face_end_row
23029 && (vpos > dpyinfo->mouse_face_beg_row
23030 || hpos >= dpyinfo->mouse_face_beg_col)
23031 && (vpos < dpyinfo->mouse_face_end_row
23032 || hpos < dpyinfo->mouse_face_end_col
23033 || dpyinfo->mouse_face_past_end));
23034
23035 if (same_region)
23036 cursor = No_Cursor;
23037
23038 /* Check mouse-face highlighting. */
23039 if (! same_region
23040 /* If there exists an overlay with mouse-face overlapping
23041 the one we are currently highlighting, we have to
23042 check if we enter the overlapping overlay, and then
23043 highlight only that. */
23044 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23045 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23046 {
23047 /* Find the highest priority overlay that has a mouse-face
23048 property. */
23049 overlay = Qnil;
23050 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23051 {
23052 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23053 if (!NILP (mouse_face))
23054 overlay = overlay_vec[i];
23055 }
23056
23057 /* If we're actually highlighting the same overlay as
23058 before, there's no need to do that again. */
23059 if (!NILP (overlay)
23060 && EQ (overlay, dpyinfo->mouse_face_overlay))
23061 goto check_help_echo;
23062
23063 dpyinfo->mouse_face_overlay = overlay;
23064
23065 /* Clear the display of the old active region, if any. */
23066 if (clear_mouse_face (dpyinfo))
23067 cursor = No_Cursor;
23068
23069 /* If no overlay applies, get a text property. */
23070 if (NILP (overlay))
23071 mouse_face = Fget_text_property (position, Qmouse_face, object);
23072
23073 /* Handle the overlay case. */
23074 if (!NILP (overlay))
23075 {
23076 /* Find the range of text around this char that
23077 should be active. */
23078 Lisp_Object before, after;
23079 int ignore;
23080
23081 before = Foverlay_start (overlay);
23082 after = Foverlay_end (overlay);
23083 /* Record this as the current active region. */
23084 fast_find_position (w, XFASTINT (before),
23085 &dpyinfo->mouse_face_beg_col,
23086 &dpyinfo->mouse_face_beg_row,
23087 &dpyinfo->mouse_face_beg_x,
23088 &dpyinfo->mouse_face_beg_y, Qnil);
23089
23090 dpyinfo->mouse_face_past_end
23091 = !fast_find_position (w, XFASTINT (after),
23092 &dpyinfo->mouse_face_end_col,
23093 &dpyinfo->mouse_face_end_row,
23094 &dpyinfo->mouse_face_end_x,
23095 &dpyinfo->mouse_face_end_y, Qnil);
23096 dpyinfo->mouse_face_window = window;
23097
23098 dpyinfo->mouse_face_face_id
23099 = face_at_buffer_position (w, pos, 0, 0,
23100 &ignore, pos + 1,
23101 !dpyinfo->mouse_face_hidden);
23102
23103 /* Display it as active. */
23104 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23105 cursor = No_Cursor;
23106 }
23107 /* Handle the text property case. */
23108 else if (!NILP (mouse_face) && BUFFERP (object))
23109 {
23110 /* Find the range of text around this char that
23111 should be active. */
23112 Lisp_Object before, after, beginning, end;
23113 int ignore;
23114
23115 beginning = Fmarker_position (w->start);
23116 end = make_number (BUF_Z (XBUFFER (object))
23117 - XFASTINT (w->window_end_pos));
23118 before
23119 = Fprevious_single_property_change (make_number (pos + 1),
23120 Qmouse_face,
23121 object, beginning);
23122 after
23123 = Fnext_single_property_change (position, Qmouse_face,
23124 object, end);
23125
23126 /* Record this as the current active region. */
23127 fast_find_position (w, XFASTINT (before),
23128 &dpyinfo->mouse_face_beg_col,
23129 &dpyinfo->mouse_face_beg_row,
23130 &dpyinfo->mouse_face_beg_x,
23131 &dpyinfo->mouse_face_beg_y, Qnil);
23132 dpyinfo->mouse_face_past_end
23133 = !fast_find_position (w, XFASTINT (after),
23134 &dpyinfo->mouse_face_end_col,
23135 &dpyinfo->mouse_face_end_row,
23136 &dpyinfo->mouse_face_end_x,
23137 &dpyinfo->mouse_face_end_y, Qnil);
23138 dpyinfo->mouse_face_window = window;
23139
23140 if (BUFFERP (object))
23141 dpyinfo->mouse_face_face_id
23142 = face_at_buffer_position (w, pos, 0, 0,
23143 &ignore, pos + 1,
23144 !dpyinfo->mouse_face_hidden);
23145
23146 /* Display it as active. */
23147 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23148 cursor = No_Cursor;
23149 }
23150 else if (!NILP (mouse_face) && STRINGP (object))
23151 {
23152 Lisp_Object b, e;
23153 int ignore;
23154
23155 b = Fprevious_single_property_change (make_number (pos + 1),
23156 Qmouse_face,
23157 object, Qnil);
23158 e = Fnext_single_property_change (position, Qmouse_face,
23159 object, Qnil);
23160 if (NILP (b))
23161 b = make_number (0);
23162 if (NILP (e))
23163 e = make_number (SCHARS (object) - 1);
23164
23165 fast_find_string_pos (w, XINT (b), object,
23166 &dpyinfo->mouse_face_beg_col,
23167 &dpyinfo->mouse_face_beg_row,
23168 &dpyinfo->mouse_face_beg_x,
23169 &dpyinfo->mouse_face_beg_y, 0);
23170 fast_find_string_pos (w, XINT (e), object,
23171 &dpyinfo->mouse_face_end_col,
23172 &dpyinfo->mouse_face_end_row,
23173 &dpyinfo->mouse_face_end_x,
23174 &dpyinfo->mouse_face_end_y, 1);
23175 dpyinfo->mouse_face_past_end = 0;
23176 dpyinfo->mouse_face_window = window;
23177 dpyinfo->mouse_face_face_id
23178 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23179 glyph->face_id, 1);
23180 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23181 cursor = No_Cursor;
23182 }
23183 else if (STRINGP (object) && NILP (mouse_face))
23184 {
23185 /* A string which doesn't have mouse-face, but
23186 the text ``under'' it might have. */
23187 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23188 int start = MATRIX_ROW_START_CHARPOS (r);
23189
23190 pos = string_buffer_position (w, object, start);
23191 if (pos > 0)
23192 mouse_face = get_char_property_and_overlay (make_number (pos),
23193 Qmouse_face,
23194 w->buffer,
23195 &overlay);
23196 if (!NILP (mouse_face) && !NILP (overlay))
23197 {
23198 Lisp_Object before = Foverlay_start (overlay);
23199 Lisp_Object after = Foverlay_end (overlay);
23200 int ignore;
23201
23202 /* Note that we might not be able to find position
23203 BEFORE in the glyph matrix if the overlay is
23204 entirely covered by a `display' property. In
23205 this case, we overshoot. So let's stop in
23206 the glyph matrix before glyphs for OBJECT. */
23207 fast_find_position (w, XFASTINT (before),
23208 &dpyinfo->mouse_face_beg_col,
23209 &dpyinfo->mouse_face_beg_row,
23210 &dpyinfo->mouse_face_beg_x,
23211 &dpyinfo->mouse_face_beg_y,
23212 object);
23213
23214 dpyinfo->mouse_face_past_end
23215 = !fast_find_position (w, XFASTINT (after),
23216 &dpyinfo->mouse_face_end_col,
23217 &dpyinfo->mouse_face_end_row,
23218 &dpyinfo->mouse_face_end_x,
23219 &dpyinfo->mouse_face_end_y,
23220 Qnil);
23221 dpyinfo->mouse_face_window = window;
23222 dpyinfo->mouse_face_face_id
23223 = face_at_buffer_position (w, pos, 0, 0,
23224 &ignore, pos + 1,
23225 !dpyinfo->mouse_face_hidden);
23226
23227 /* Display it as active. */
23228 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23229 cursor = No_Cursor;
23230 }
23231 }
23232 }
23233
23234 check_help_echo:
23235
23236 /* Look for a `help-echo' property. */
23237 if (NILP (help_echo_string)) {
23238 Lisp_Object help, overlay;
23239
23240 /* Check overlays first. */
23241 help = overlay = Qnil;
23242 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23243 {
23244 overlay = overlay_vec[i];
23245 help = Foverlay_get (overlay, Qhelp_echo);
23246 }
23247
23248 if (!NILP (help))
23249 {
23250 help_echo_string = help;
23251 help_echo_window = window;
23252 help_echo_object = overlay;
23253 help_echo_pos = pos;
23254 }
23255 else
23256 {
23257 Lisp_Object object = glyph->object;
23258 int charpos = glyph->charpos;
23259
23260 /* Try text properties. */
23261 if (STRINGP (object)
23262 && charpos >= 0
23263 && charpos < SCHARS (object))
23264 {
23265 help = Fget_text_property (make_number (charpos),
23266 Qhelp_echo, object);
23267 if (NILP (help))
23268 {
23269 /* If the string itself doesn't specify a help-echo,
23270 see if the buffer text ``under'' it does. */
23271 struct glyph_row *r
23272 = MATRIX_ROW (w->current_matrix, vpos);
23273 int start = MATRIX_ROW_START_CHARPOS (r);
23274 int pos = string_buffer_position (w, object, start);
23275 if (pos > 0)
23276 {
23277 help = Fget_char_property (make_number (pos),
23278 Qhelp_echo, w->buffer);
23279 if (!NILP (help))
23280 {
23281 charpos = pos;
23282 object = w->buffer;
23283 }
23284 }
23285 }
23286 }
23287 else if (BUFFERP (object)
23288 && charpos >= BEGV
23289 && charpos < ZV)
23290 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23291 object);
23292
23293 if (!NILP (help))
23294 {
23295 help_echo_string = help;
23296 help_echo_window = window;
23297 help_echo_object = object;
23298 help_echo_pos = charpos;
23299 }
23300 }
23301 }
23302
23303 /* Look for a `pointer' property. */
23304 if (NILP (pointer))
23305 {
23306 /* Check overlays first. */
23307 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23308 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23309
23310 if (NILP (pointer))
23311 {
23312 Lisp_Object object = glyph->object;
23313 int charpos = glyph->charpos;
23314
23315 /* Try text properties. */
23316 if (STRINGP (object)
23317 && charpos >= 0
23318 && charpos < SCHARS (object))
23319 {
23320 pointer = Fget_text_property (make_number (charpos),
23321 Qpointer, object);
23322 if (NILP (pointer))
23323 {
23324 /* If the string itself doesn't specify a pointer,
23325 see if the buffer text ``under'' it does. */
23326 struct glyph_row *r
23327 = MATRIX_ROW (w->current_matrix, vpos);
23328 int start = MATRIX_ROW_START_CHARPOS (r);
23329 int pos = string_buffer_position (w, object, start);
23330 if (pos > 0)
23331 pointer = Fget_char_property (make_number (pos),
23332 Qpointer, w->buffer);
23333 }
23334 }
23335 else if (BUFFERP (object)
23336 && charpos >= BEGV
23337 && charpos < ZV)
23338 pointer = Fget_text_property (make_number (charpos),
23339 Qpointer, object);
23340 }
23341 }
23342
23343 BEGV = obegv;
23344 ZV = ozv;
23345 current_buffer = obuf;
23346 }
23347
23348 set_cursor:
23349
23350 define_frame_cursor1 (f, cursor, pointer);
23351 }
23352
23353
23354 /* EXPORT for RIF:
23355 Clear any mouse-face on window W. This function is part of the
23356 redisplay interface, and is called from try_window_id and similar
23357 functions to ensure the mouse-highlight is off. */
23358
23359 void
23360 x_clear_window_mouse_face (w)
23361 struct window *w;
23362 {
23363 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23364 Lisp_Object window;
23365
23366 BLOCK_INPUT;
23367 XSETWINDOW (window, w);
23368 if (EQ (window, dpyinfo->mouse_face_window))
23369 clear_mouse_face (dpyinfo);
23370 UNBLOCK_INPUT;
23371 }
23372
23373
23374 /* EXPORT:
23375 Just discard the mouse face information for frame F, if any.
23376 This is used when the size of F is changed. */
23377
23378 void
23379 cancel_mouse_face (f)
23380 struct frame *f;
23381 {
23382 Lisp_Object window;
23383 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23384
23385 window = dpyinfo->mouse_face_window;
23386 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23387 {
23388 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23389 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23390 dpyinfo->mouse_face_window = Qnil;
23391 }
23392 }
23393
23394
23395 #endif /* HAVE_WINDOW_SYSTEM */
23396
23397 \f
23398 /***********************************************************************
23399 Exposure Events
23400 ***********************************************************************/
23401
23402 #ifdef HAVE_WINDOW_SYSTEM
23403
23404 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23405 which intersects rectangle R. R is in window-relative coordinates. */
23406
23407 static void
23408 expose_area (w, row, r, area)
23409 struct window *w;
23410 struct glyph_row *row;
23411 XRectangle *r;
23412 enum glyph_row_area area;
23413 {
23414 struct glyph *first = row->glyphs[area];
23415 struct glyph *end = row->glyphs[area] + row->used[area];
23416 struct glyph *last;
23417 int first_x, start_x, x;
23418
23419 if (area == TEXT_AREA && row->fill_line_p)
23420 /* If row extends face to end of line write the whole line. */
23421 draw_glyphs (w, 0, row, area,
23422 0, row->used[area],
23423 DRAW_NORMAL_TEXT, 0);
23424 else
23425 {
23426 /* Set START_X to the window-relative start position for drawing glyphs of
23427 AREA. The first glyph of the text area can be partially visible.
23428 The first glyphs of other areas cannot. */
23429 start_x = window_box_left_offset (w, area);
23430 x = start_x;
23431 if (area == TEXT_AREA)
23432 x += row->x;
23433
23434 /* Find the first glyph that must be redrawn. */
23435 while (first < end
23436 && x + first->pixel_width < r->x)
23437 {
23438 x += first->pixel_width;
23439 ++first;
23440 }
23441
23442 /* Find the last one. */
23443 last = first;
23444 first_x = x;
23445 while (last < end
23446 && x < r->x + r->width)
23447 {
23448 x += last->pixel_width;
23449 ++last;
23450 }
23451
23452 /* Repaint. */
23453 if (last > first)
23454 draw_glyphs (w, first_x - start_x, row, area,
23455 first - row->glyphs[area], last - row->glyphs[area],
23456 DRAW_NORMAL_TEXT, 0);
23457 }
23458 }
23459
23460
23461 /* Redraw the parts of the glyph row ROW on window W intersecting
23462 rectangle R. R is in window-relative coordinates. Value is
23463 non-zero if mouse-face was overwritten. */
23464
23465 static int
23466 expose_line (w, row, r)
23467 struct window *w;
23468 struct glyph_row *row;
23469 XRectangle *r;
23470 {
23471 xassert (row->enabled_p);
23472
23473 if (row->mode_line_p || w->pseudo_window_p)
23474 draw_glyphs (w, 0, row, TEXT_AREA,
23475 0, row->used[TEXT_AREA],
23476 DRAW_NORMAL_TEXT, 0);
23477 else
23478 {
23479 if (row->used[LEFT_MARGIN_AREA])
23480 expose_area (w, row, r, LEFT_MARGIN_AREA);
23481 if (row->used[TEXT_AREA])
23482 expose_area (w, row, r, TEXT_AREA);
23483 if (row->used[RIGHT_MARGIN_AREA])
23484 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23485 draw_row_fringe_bitmaps (w, row);
23486 }
23487
23488 return row->mouse_face_p;
23489 }
23490
23491
23492 /* Redraw those parts of glyphs rows during expose event handling that
23493 overlap other rows. Redrawing of an exposed line writes over parts
23494 of lines overlapping that exposed line; this function fixes that.
23495
23496 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23497 row in W's current matrix that is exposed and overlaps other rows.
23498 LAST_OVERLAPPING_ROW is the last such row. */
23499
23500 static void
23501 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23502 struct window *w;
23503 struct glyph_row *first_overlapping_row;
23504 struct glyph_row *last_overlapping_row;
23505 {
23506 struct glyph_row *row;
23507
23508 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23509 if (row->overlapping_p)
23510 {
23511 xassert (row->enabled_p && !row->mode_line_p);
23512
23513 if (row->used[LEFT_MARGIN_AREA])
23514 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23515
23516 if (row->used[TEXT_AREA])
23517 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23518
23519 if (row->used[RIGHT_MARGIN_AREA])
23520 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23521 }
23522 }
23523
23524
23525 /* Return non-zero if W's cursor intersects rectangle R. */
23526
23527 static int
23528 phys_cursor_in_rect_p (w, r)
23529 struct window *w;
23530 XRectangle *r;
23531 {
23532 XRectangle cr, result;
23533 struct glyph *cursor_glyph;
23534 struct glyph_row *row;
23535
23536 if (w->phys_cursor.vpos >= 0
23537 && w->phys_cursor.vpos < w->current_matrix->nrows
23538 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23539 row->enabled_p)
23540 && row->cursor_in_fringe_p)
23541 {
23542 /* Cursor is in the fringe. */
23543 cr.x = window_box_right_offset (w,
23544 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23545 ? RIGHT_MARGIN_AREA
23546 : TEXT_AREA));
23547 cr.y = row->y;
23548 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23549 cr.height = row->height;
23550 return x_intersect_rectangles (&cr, r, &result);
23551 }
23552
23553 cursor_glyph = get_phys_cursor_glyph (w);
23554 if (cursor_glyph)
23555 {
23556 /* r is relative to W's box, but w->phys_cursor.x is relative
23557 to left edge of W's TEXT area. Adjust it. */
23558 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23559 cr.y = w->phys_cursor.y;
23560 cr.width = cursor_glyph->pixel_width;
23561 cr.height = w->phys_cursor_height;
23562 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23563 I assume the effect is the same -- and this is portable. */
23564 return x_intersect_rectangles (&cr, r, &result);
23565 }
23566 /* If we don't understand the format, pretend we're not in the hot-spot. */
23567 return 0;
23568 }
23569
23570
23571 /* EXPORT:
23572 Draw a vertical window border to the right of window W if W doesn't
23573 have vertical scroll bars. */
23574
23575 void
23576 x_draw_vertical_border (w)
23577 struct window *w;
23578 {
23579 struct frame *f = XFRAME (WINDOW_FRAME (w));
23580
23581 /* We could do better, if we knew what type of scroll-bar the adjacent
23582 windows (on either side) have... But we don't :-(
23583 However, I think this works ok. ++KFS 2003-04-25 */
23584
23585 /* Redraw borders between horizontally adjacent windows. Don't
23586 do it for frames with vertical scroll bars because either the
23587 right scroll bar of a window, or the left scroll bar of its
23588 neighbor will suffice as a border. */
23589 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23590 return;
23591
23592 if (!WINDOW_RIGHTMOST_P (w)
23593 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23594 {
23595 int x0, x1, y0, y1;
23596
23597 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23598 y1 -= 1;
23599
23600 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23601 x1 -= 1;
23602
23603 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23604 }
23605 else if (!WINDOW_LEFTMOST_P (w)
23606 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23607 {
23608 int x0, x1, y0, y1;
23609
23610 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23611 y1 -= 1;
23612
23613 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23614 x0 -= 1;
23615
23616 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23617 }
23618 }
23619
23620
23621 /* Redraw the part of window W intersection rectangle FR. Pixel
23622 coordinates in FR are frame-relative. Call this function with
23623 input blocked. Value is non-zero if the exposure overwrites
23624 mouse-face. */
23625
23626 static int
23627 expose_window (w, fr)
23628 struct window *w;
23629 XRectangle *fr;
23630 {
23631 struct frame *f = XFRAME (w->frame);
23632 XRectangle wr, r;
23633 int mouse_face_overwritten_p = 0;
23634
23635 /* If window is not yet fully initialized, do nothing. This can
23636 happen when toolkit scroll bars are used and a window is split.
23637 Reconfiguring the scroll bar will generate an expose for a newly
23638 created window. */
23639 if (w->current_matrix == NULL)
23640 return 0;
23641
23642 /* When we're currently updating the window, display and current
23643 matrix usually don't agree. Arrange for a thorough display
23644 later. */
23645 if (w == updated_window)
23646 {
23647 SET_FRAME_GARBAGED (f);
23648 return 0;
23649 }
23650
23651 /* Frame-relative pixel rectangle of W. */
23652 wr.x = WINDOW_LEFT_EDGE_X (w);
23653 wr.y = WINDOW_TOP_EDGE_Y (w);
23654 wr.width = WINDOW_TOTAL_WIDTH (w);
23655 wr.height = WINDOW_TOTAL_HEIGHT (w);
23656
23657 if (x_intersect_rectangles (fr, &wr, &r))
23658 {
23659 int yb = window_text_bottom_y (w);
23660 struct glyph_row *row;
23661 int cursor_cleared_p;
23662 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23663
23664 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23665 r.x, r.y, r.width, r.height));
23666
23667 /* Convert to window coordinates. */
23668 r.x -= WINDOW_LEFT_EDGE_X (w);
23669 r.y -= WINDOW_TOP_EDGE_Y (w);
23670
23671 /* Turn off the cursor. */
23672 if (!w->pseudo_window_p
23673 && phys_cursor_in_rect_p (w, &r))
23674 {
23675 x_clear_cursor (w);
23676 cursor_cleared_p = 1;
23677 }
23678 else
23679 cursor_cleared_p = 0;
23680
23681 /* Update lines intersecting rectangle R. */
23682 first_overlapping_row = last_overlapping_row = NULL;
23683 for (row = w->current_matrix->rows;
23684 row->enabled_p;
23685 ++row)
23686 {
23687 int y0 = row->y;
23688 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23689
23690 if ((y0 >= r.y && y0 < r.y + r.height)
23691 || (y1 > r.y && y1 < r.y + r.height)
23692 || (r.y >= y0 && r.y < y1)
23693 || (r.y + r.height > y0 && r.y + r.height < y1))
23694 {
23695 /* A header line may be overlapping, but there is no need
23696 to fix overlapping areas for them. KFS 2005-02-12 */
23697 if (row->overlapping_p && !row->mode_line_p)
23698 {
23699 if (first_overlapping_row == NULL)
23700 first_overlapping_row = row;
23701 last_overlapping_row = row;
23702 }
23703
23704 if (expose_line (w, row, &r))
23705 mouse_face_overwritten_p = 1;
23706 }
23707
23708 if (y1 >= yb)
23709 break;
23710 }
23711
23712 /* Display the mode line if there is one. */
23713 if (WINDOW_WANTS_MODELINE_P (w)
23714 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23715 row->enabled_p)
23716 && row->y < r.y + r.height)
23717 {
23718 if (expose_line (w, row, &r))
23719 mouse_face_overwritten_p = 1;
23720 }
23721
23722 if (!w->pseudo_window_p)
23723 {
23724 /* Fix the display of overlapping rows. */
23725 if (first_overlapping_row)
23726 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23727
23728 /* Draw border between windows. */
23729 x_draw_vertical_border (w);
23730
23731 /* Turn the cursor on again. */
23732 if (cursor_cleared_p)
23733 update_window_cursor (w, 1);
23734 }
23735 }
23736
23737 return mouse_face_overwritten_p;
23738 }
23739
23740
23741
23742 /* Redraw (parts) of all windows in the window tree rooted at W that
23743 intersect R. R contains frame pixel coordinates. Value is
23744 non-zero if the exposure overwrites mouse-face. */
23745
23746 static int
23747 expose_window_tree (w, r)
23748 struct window *w;
23749 XRectangle *r;
23750 {
23751 struct frame *f = XFRAME (w->frame);
23752 int mouse_face_overwritten_p = 0;
23753
23754 while (w && !FRAME_GARBAGED_P (f))
23755 {
23756 if (!NILP (w->hchild))
23757 mouse_face_overwritten_p
23758 |= expose_window_tree (XWINDOW (w->hchild), r);
23759 else if (!NILP (w->vchild))
23760 mouse_face_overwritten_p
23761 |= expose_window_tree (XWINDOW (w->vchild), r);
23762 else
23763 mouse_face_overwritten_p |= expose_window (w, r);
23764
23765 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23766 }
23767
23768 return mouse_face_overwritten_p;
23769 }
23770
23771
23772 /* EXPORT:
23773 Redisplay an exposed area of frame F. X and Y are the upper-left
23774 corner of the exposed rectangle. W and H are width and height of
23775 the exposed area. All are pixel values. W or H zero means redraw
23776 the entire frame. */
23777
23778 void
23779 expose_frame (f, x, y, w, h)
23780 struct frame *f;
23781 int x, y, w, h;
23782 {
23783 XRectangle r;
23784 int mouse_face_overwritten_p = 0;
23785
23786 TRACE ((stderr, "expose_frame "));
23787
23788 /* No need to redraw if frame will be redrawn soon. */
23789 if (FRAME_GARBAGED_P (f))
23790 {
23791 TRACE ((stderr, " garbaged\n"));
23792 return;
23793 }
23794
23795 /* If basic faces haven't been realized yet, there is no point in
23796 trying to redraw anything. This can happen when we get an expose
23797 event while Emacs is starting, e.g. by moving another window. */
23798 if (FRAME_FACE_CACHE (f) == NULL
23799 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23800 {
23801 TRACE ((stderr, " no faces\n"));
23802 return;
23803 }
23804
23805 if (w == 0 || h == 0)
23806 {
23807 r.x = r.y = 0;
23808 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23809 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23810 }
23811 else
23812 {
23813 r.x = x;
23814 r.y = y;
23815 r.width = w;
23816 r.height = h;
23817 }
23818
23819 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23820 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23821
23822 if (WINDOWP (f->tool_bar_window))
23823 mouse_face_overwritten_p
23824 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23825
23826 #ifdef HAVE_X_WINDOWS
23827 #ifndef MSDOS
23828 #ifndef USE_X_TOOLKIT
23829 if (WINDOWP (f->menu_bar_window))
23830 mouse_face_overwritten_p
23831 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23832 #endif /* not USE_X_TOOLKIT */
23833 #endif
23834 #endif
23835
23836 /* Some window managers support a focus-follows-mouse style with
23837 delayed raising of frames. Imagine a partially obscured frame,
23838 and moving the mouse into partially obscured mouse-face on that
23839 frame. The visible part of the mouse-face will be highlighted,
23840 then the WM raises the obscured frame. With at least one WM, KDE
23841 2.1, Emacs is not getting any event for the raising of the frame
23842 (even tried with SubstructureRedirectMask), only Expose events.
23843 These expose events will draw text normally, i.e. not
23844 highlighted. Which means we must redo the highlight here.
23845 Subsume it under ``we love X''. --gerd 2001-08-15 */
23846 /* Included in Windows version because Windows most likely does not
23847 do the right thing if any third party tool offers
23848 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23849 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23850 {
23851 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23852 if (f == dpyinfo->mouse_face_mouse_frame)
23853 {
23854 int x = dpyinfo->mouse_face_mouse_x;
23855 int y = dpyinfo->mouse_face_mouse_y;
23856 clear_mouse_face (dpyinfo);
23857 note_mouse_highlight (f, x, y);
23858 }
23859 }
23860 }
23861
23862
23863 /* EXPORT:
23864 Determine the intersection of two rectangles R1 and R2. Return
23865 the intersection in *RESULT. Value is non-zero if RESULT is not
23866 empty. */
23867
23868 int
23869 x_intersect_rectangles (r1, r2, result)
23870 XRectangle *r1, *r2, *result;
23871 {
23872 XRectangle *left, *right;
23873 XRectangle *upper, *lower;
23874 int intersection_p = 0;
23875
23876 /* Rearrange so that R1 is the left-most rectangle. */
23877 if (r1->x < r2->x)
23878 left = r1, right = r2;
23879 else
23880 left = r2, right = r1;
23881
23882 /* X0 of the intersection is right.x0, if this is inside R1,
23883 otherwise there is no intersection. */
23884 if (right->x <= left->x + left->width)
23885 {
23886 result->x = right->x;
23887
23888 /* The right end of the intersection is the minimum of the
23889 the right ends of left and right. */
23890 result->width = (min (left->x + left->width, right->x + right->width)
23891 - result->x);
23892
23893 /* Same game for Y. */
23894 if (r1->y < r2->y)
23895 upper = r1, lower = r2;
23896 else
23897 upper = r2, lower = r1;
23898
23899 /* The upper end of the intersection is lower.y0, if this is inside
23900 of upper. Otherwise, there is no intersection. */
23901 if (lower->y <= upper->y + upper->height)
23902 {
23903 result->y = lower->y;
23904
23905 /* The lower end of the intersection is the minimum of the lower
23906 ends of upper and lower. */
23907 result->height = (min (lower->y + lower->height,
23908 upper->y + upper->height)
23909 - result->y);
23910 intersection_p = 1;
23911 }
23912 }
23913
23914 return intersection_p;
23915 }
23916
23917 #endif /* HAVE_WINDOW_SYSTEM */
23918
23919 \f
23920 /***********************************************************************
23921 Initialization
23922 ***********************************************************************/
23923
23924 void
23925 syms_of_xdisp ()
23926 {
23927 Vwith_echo_area_save_vector = Qnil;
23928 staticpro (&Vwith_echo_area_save_vector);
23929
23930 Vmessage_stack = Qnil;
23931 staticpro (&Vmessage_stack);
23932
23933 Qinhibit_redisplay = intern ("inhibit-redisplay");
23934 staticpro (&Qinhibit_redisplay);
23935
23936 message_dolog_marker1 = Fmake_marker ();
23937 staticpro (&message_dolog_marker1);
23938 message_dolog_marker2 = Fmake_marker ();
23939 staticpro (&message_dolog_marker2);
23940 message_dolog_marker3 = Fmake_marker ();
23941 staticpro (&message_dolog_marker3);
23942
23943 #if GLYPH_DEBUG
23944 defsubr (&Sdump_frame_glyph_matrix);
23945 defsubr (&Sdump_glyph_matrix);
23946 defsubr (&Sdump_glyph_row);
23947 defsubr (&Sdump_tool_bar_row);
23948 defsubr (&Strace_redisplay);
23949 defsubr (&Strace_to_stderr);
23950 #endif
23951 #ifdef HAVE_WINDOW_SYSTEM
23952 defsubr (&Stool_bar_lines_needed);
23953 defsubr (&Slookup_image_map);
23954 #endif
23955 defsubr (&Sformat_mode_line);
23956 defsubr (&Sinvisible_p);
23957
23958 staticpro (&Qmenu_bar_update_hook);
23959 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23960
23961 staticpro (&Qoverriding_terminal_local_map);
23962 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23963
23964 staticpro (&Qoverriding_local_map);
23965 Qoverriding_local_map = intern ("overriding-local-map");
23966
23967 staticpro (&Qwindow_scroll_functions);
23968 Qwindow_scroll_functions = intern ("window-scroll-functions");
23969
23970 staticpro (&Qredisplay_end_trigger_functions);
23971 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23972
23973 staticpro (&Qinhibit_point_motion_hooks);
23974 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23975
23976 QCdata = intern (":data");
23977 staticpro (&QCdata);
23978 Qdisplay = intern ("display");
23979 staticpro (&Qdisplay);
23980 Qspace_width = intern ("space-width");
23981 staticpro (&Qspace_width);
23982 Qraise = intern ("raise");
23983 staticpro (&Qraise);
23984 Qslice = intern ("slice");
23985 staticpro (&Qslice);
23986 Qspace = intern ("space");
23987 staticpro (&Qspace);
23988 Qmargin = intern ("margin");
23989 staticpro (&Qmargin);
23990 Qpointer = intern ("pointer");
23991 staticpro (&Qpointer);
23992 Qleft_margin = intern ("left-margin");
23993 staticpro (&Qleft_margin);
23994 Qright_margin = intern ("right-margin");
23995 staticpro (&Qright_margin);
23996 Qcenter = intern ("center");
23997 staticpro (&Qcenter);
23998 Qline_height = intern ("line-height");
23999 staticpro (&Qline_height);
24000 QCalign_to = intern (":align-to");
24001 staticpro (&QCalign_to);
24002 QCrelative_width = intern (":relative-width");
24003 staticpro (&QCrelative_width);
24004 QCrelative_height = intern (":relative-height");
24005 staticpro (&QCrelative_height);
24006 QCeval = intern (":eval");
24007 staticpro (&QCeval);
24008 QCpropertize = intern (":propertize");
24009 staticpro (&QCpropertize);
24010 QCfile = intern (":file");
24011 staticpro (&QCfile);
24012 Qfontified = intern ("fontified");
24013 staticpro (&Qfontified);
24014 Qfontification_functions = intern ("fontification-functions");
24015 staticpro (&Qfontification_functions);
24016 Qtrailing_whitespace = intern ("trailing-whitespace");
24017 staticpro (&Qtrailing_whitespace);
24018 Qescape_glyph = intern ("escape-glyph");
24019 staticpro (&Qescape_glyph);
24020 Qnobreak_space = intern ("nobreak-space");
24021 staticpro (&Qnobreak_space);
24022 Qimage = intern ("image");
24023 staticpro (&Qimage);
24024 QCmap = intern (":map");
24025 staticpro (&QCmap);
24026 QCpointer = intern (":pointer");
24027 staticpro (&QCpointer);
24028 Qrect = intern ("rect");
24029 staticpro (&Qrect);
24030 Qcircle = intern ("circle");
24031 staticpro (&Qcircle);
24032 Qpoly = intern ("poly");
24033 staticpro (&Qpoly);
24034 Qmessage_truncate_lines = intern ("message-truncate-lines");
24035 staticpro (&Qmessage_truncate_lines);
24036 Qgrow_only = intern ("grow-only");
24037 staticpro (&Qgrow_only);
24038 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24039 staticpro (&Qinhibit_menubar_update);
24040 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24041 staticpro (&Qinhibit_eval_during_redisplay);
24042 Qposition = intern ("position");
24043 staticpro (&Qposition);
24044 Qbuffer_position = intern ("buffer-position");
24045 staticpro (&Qbuffer_position);
24046 Qobject = intern ("object");
24047 staticpro (&Qobject);
24048 Qbar = intern ("bar");
24049 staticpro (&Qbar);
24050 Qhbar = intern ("hbar");
24051 staticpro (&Qhbar);
24052 Qbox = intern ("box");
24053 staticpro (&Qbox);
24054 Qhollow = intern ("hollow");
24055 staticpro (&Qhollow);
24056 Qhand = intern ("hand");
24057 staticpro (&Qhand);
24058 Qarrow = intern ("arrow");
24059 staticpro (&Qarrow);
24060 Qtext = intern ("text");
24061 staticpro (&Qtext);
24062 Qrisky_local_variable = intern ("risky-local-variable");
24063 staticpro (&Qrisky_local_variable);
24064 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24065 staticpro (&Qinhibit_free_realized_faces);
24066
24067 list_of_error = Fcons (Fcons (intern ("error"),
24068 Fcons (intern ("void-variable"), Qnil)),
24069 Qnil);
24070 staticpro (&list_of_error);
24071
24072 Qlast_arrow_position = intern ("last-arrow-position");
24073 staticpro (&Qlast_arrow_position);
24074 Qlast_arrow_string = intern ("last-arrow-string");
24075 staticpro (&Qlast_arrow_string);
24076
24077 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24078 staticpro (&Qoverlay_arrow_string);
24079 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24080 staticpro (&Qoverlay_arrow_bitmap);
24081
24082 echo_buffer[0] = echo_buffer[1] = Qnil;
24083 staticpro (&echo_buffer[0]);
24084 staticpro (&echo_buffer[1]);
24085
24086 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24087 staticpro (&echo_area_buffer[0]);
24088 staticpro (&echo_area_buffer[1]);
24089
24090 Vmessages_buffer_name = build_string ("*Messages*");
24091 staticpro (&Vmessages_buffer_name);
24092
24093 mode_line_proptrans_alist = Qnil;
24094 staticpro (&mode_line_proptrans_alist);
24095 mode_line_string_list = Qnil;
24096 staticpro (&mode_line_string_list);
24097 mode_line_string_face = Qnil;
24098 staticpro (&mode_line_string_face);
24099 mode_line_string_face_prop = Qnil;
24100 staticpro (&mode_line_string_face_prop);
24101 Vmode_line_unwind_vector = Qnil;
24102 staticpro (&Vmode_line_unwind_vector);
24103
24104 help_echo_string = Qnil;
24105 staticpro (&help_echo_string);
24106 help_echo_object = Qnil;
24107 staticpro (&help_echo_object);
24108 help_echo_window = Qnil;
24109 staticpro (&help_echo_window);
24110 previous_help_echo_string = Qnil;
24111 staticpro (&previous_help_echo_string);
24112 help_echo_pos = -1;
24113
24114 #ifdef HAVE_WINDOW_SYSTEM
24115 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24116 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24117 For example, if a block cursor is over a tab, it will be drawn as
24118 wide as that tab on the display. */);
24119 x_stretch_cursor_p = 0;
24120 #endif
24121
24122 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24123 doc: /* *Non-nil means highlight trailing whitespace.
24124 The face used for trailing whitespace is `trailing-whitespace'. */);
24125 Vshow_trailing_whitespace = Qnil;
24126
24127 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24128 doc: /* *Control highlighting of nobreak space and soft hyphen.
24129 A value of t means highlight the character itself (for nobreak space,
24130 use face `nobreak-space').
24131 A value of nil means no highlighting.
24132 Other values mean display the escape glyph followed by an ordinary
24133 space or ordinary hyphen. */);
24134 Vnobreak_char_display = Qt;
24135
24136 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24137 doc: /* *The pointer shape to show in void text areas.
24138 A value of nil means to show the text pointer. Other options are `arrow',
24139 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24140 Vvoid_text_area_pointer = Qarrow;
24141
24142 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24143 doc: /* Non-nil means don't actually do any redisplay.
24144 This is used for internal purposes. */);
24145 Vinhibit_redisplay = Qnil;
24146
24147 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24148 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24149 Vglobal_mode_string = Qnil;
24150
24151 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24152 doc: /* Marker for where to display an arrow on top of the buffer text.
24153 This must be the beginning of a line in order to work.
24154 See also `overlay-arrow-string'. */);
24155 Voverlay_arrow_position = Qnil;
24156
24157 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24158 doc: /* String to display as an arrow in non-window frames.
24159 See also `overlay-arrow-position'. */);
24160 Voverlay_arrow_string = build_string ("=>");
24161
24162 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24163 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24164 The symbols on this list are examined during redisplay to determine
24165 where to display overlay arrows. */);
24166 Voverlay_arrow_variable_list
24167 = Fcons (intern ("overlay-arrow-position"), Qnil);
24168
24169 DEFVAR_INT ("scroll-step", &scroll_step,
24170 doc: /* *The number of lines to try scrolling a window by when point moves out.
24171 If that fails to bring point back on frame, point is centered instead.
24172 If this is zero, point is always centered after it moves off frame.
24173 If you want scrolling to always be a line at a time, you should set
24174 `scroll-conservatively' to a large value rather than set this to 1. */);
24175
24176 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24177 doc: /* *Scroll up to this many lines, to bring point back on screen.
24178 If point moves off-screen, redisplay will scroll by up to
24179 `scroll-conservatively' lines in order to bring point just barely
24180 onto the screen again. If that cannot be done, then redisplay
24181 recenters point as usual.
24182
24183 A value of zero means always recenter point if it moves off screen. */);
24184 scroll_conservatively = 0;
24185
24186 DEFVAR_INT ("scroll-margin", &scroll_margin,
24187 doc: /* *Number of lines of margin at the top and bottom of a window.
24188 Recenter the window whenever point gets within this many lines
24189 of the top or bottom of the window. */);
24190 scroll_margin = 0;
24191
24192 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24193 doc: /* Pixels per inch value for non-window system displays.
24194 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24195 Vdisplay_pixels_per_inch = make_float (72.0);
24196
24197 #if GLYPH_DEBUG
24198 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24199 #endif
24200
24201 DEFVAR_BOOL ("truncate-partial-width-windows",
24202 &truncate_partial_width_windows,
24203 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24204 truncate_partial_width_windows = 1;
24205
24206 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24207 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24208 Any other value means to use the appropriate face, `mode-line',
24209 `header-line', or `menu' respectively. */);
24210 mode_line_inverse_video = 1;
24211
24212 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24213 doc: /* *Maximum buffer size for which line number should be displayed.
24214 If the buffer is bigger than this, the line number does not appear
24215 in the mode line. A value of nil means no limit. */);
24216 Vline_number_display_limit = Qnil;
24217
24218 DEFVAR_INT ("line-number-display-limit-width",
24219 &line_number_display_limit_width,
24220 doc: /* *Maximum line width (in characters) for line number display.
24221 If the average length of the lines near point is bigger than this, then the
24222 line number may be omitted from the mode line. */);
24223 line_number_display_limit_width = 200;
24224
24225 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24226 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24227 highlight_nonselected_windows = 0;
24228
24229 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24230 doc: /* Non-nil if more than one frame is visible on this display.
24231 Minibuffer-only frames don't count, but iconified frames do.
24232 This variable is not guaranteed to be accurate except while processing
24233 `frame-title-format' and `icon-title-format'. */);
24234
24235 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24236 doc: /* Template for displaying the title bar of visible frames.
24237 \(Assuming the window manager supports this feature.)
24238
24239 This variable has the same structure as `mode-line-format', except that
24240 the %c and %l constructs are ignored. It is used only on frames for
24241 which no explicit name has been set \(see `modify-frame-parameters'). */);
24242
24243 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24244 doc: /* Template for displaying the title bar of an iconified frame.
24245 \(Assuming the window manager supports this feature.)
24246 This variable has the same structure as `mode-line-format' (which see),
24247 and is used only on frames for which no explicit name has been set
24248 \(see `modify-frame-parameters'). */);
24249 Vicon_title_format
24250 = Vframe_title_format
24251 = Fcons (intern ("multiple-frames"),
24252 Fcons (build_string ("%b"),
24253 Fcons (Fcons (empty_unibyte_string,
24254 Fcons (intern ("invocation-name"),
24255 Fcons (build_string ("@"),
24256 Fcons (intern ("system-name"),
24257 Qnil)))),
24258 Qnil)));
24259
24260 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24261 doc: /* Maximum number of lines to keep in the message log buffer.
24262 If nil, disable message logging. If t, log messages but don't truncate
24263 the buffer when it becomes large. */);
24264 Vmessage_log_max = make_number (100);
24265
24266 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24267 doc: /* Functions called before redisplay, if window sizes have changed.
24268 The value should be a list of functions that take one argument.
24269 Just before redisplay, for each frame, if any of its windows have changed
24270 size since the last redisplay, or have been split or deleted,
24271 all the functions in the list are called, with the frame as argument. */);
24272 Vwindow_size_change_functions = Qnil;
24273
24274 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24275 doc: /* List of functions to call before redisplaying a window with scrolling.
24276 Each function is called with two arguments, the window
24277 and its new display-start position. Note that the value of `window-end'
24278 is not valid when these functions are called. */);
24279 Vwindow_scroll_functions = Qnil;
24280
24281 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24282 doc: /* Functions called when redisplay of a window reaches the end trigger.
24283 Each function is called with two arguments, the window and the end trigger value.
24284 See `set-window-redisplay-end-trigger'. */);
24285 Vredisplay_end_trigger_functions = Qnil;
24286
24287 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24288 doc: /* *Non-nil means autoselect window with mouse pointer.
24289 If nil, do not autoselect windows.
24290 A positive number means delay autoselection by that many seconds: a
24291 window is autoselected only after the mouse has remained in that
24292 window for the duration of the delay.
24293 A negative number has a similar effect, but causes windows to be
24294 autoselected only after the mouse has stopped moving. \(Because of
24295 the way Emacs compares mouse events, you will occasionally wait twice
24296 that time before the window gets selected.\)
24297 Any other value means to autoselect window instantaneously when the
24298 mouse pointer enters it.
24299
24300 Autoselection selects the minibuffer only if it is active, and never
24301 unselects the minibuffer if it is active.
24302
24303 When customizing this variable make sure that the actual value of
24304 `focus-follows-mouse' matches the behavior of your window manager. */);
24305 Vmouse_autoselect_window = Qnil;
24306
24307 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24308 doc: /* *Non-nil means automatically resize tool-bars.
24309 This dynamically changes the tool-bar's height to the minimum height
24310 that is needed to make all tool-bar items visible.
24311 If value is `grow-only', the tool-bar's height is only increased
24312 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24313 Vauto_resize_tool_bars = Qt;
24314
24315 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24316 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24317 auto_raise_tool_bar_buttons_p = 1;
24318
24319 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24320 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24321 make_cursor_line_fully_visible_p = 1;
24322
24323 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24324 doc: /* *Border below tool-bar in pixels.
24325 If an integer, use it as the height of the border.
24326 If it is one of `internal-border-width' or `border-width', use the
24327 value of the corresponding frame parameter.
24328 Otherwise, no border is added below the tool-bar. */);
24329 Vtool_bar_border = Qinternal_border_width;
24330
24331 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24332 doc: /* *Margin around tool-bar buttons in pixels.
24333 If an integer, use that for both horizontal and vertical margins.
24334 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24335 HORZ specifying the horizontal margin, and VERT specifying the
24336 vertical margin. */);
24337 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24338
24339 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24340 doc: /* *Relief thickness of tool-bar buttons. */);
24341 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24342
24343 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24344 doc: /* List of functions to call to fontify regions of text.
24345 Each function is called with one argument POS. Functions must
24346 fontify a region starting at POS in the current buffer, and give
24347 fontified regions the property `fontified'. */);
24348 Vfontification_functions = Qnil;
24349 Fmake_variable_buffer_local (Qfontification_functions);
24350
24351 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24352 &unibyte_display_via_language_environment,
24353 doc: /* *Non-nil means display unibyte text according to language environment.
24354 Specifically this means that unibyte non-ASCII characters
24355 are displayed by converting them to the equivalent multibyte characters
24356 according to the current language environment. As a result, they are
24357 displayed according to the current fontset. */);
24358 unibyte_display_via_language_environment = 0;
24359
24360 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24361 doc: /* *Maximum height for resizing mini-windows.
24362 If a float, it specifies a fraction of the mini-window frame's height.
24363 If an integer, it specifies a number of lines. */);
24364 Vmax_mini_window_height = make_float (0.25);
24365
24366 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24367 doc: /* *How to resize mini-windows.
24368 A value of nil means don't automatically resize mini-windows.
24369 A value of t means resize them to fit the text displayed in them.
24370 A value of `grow-only', the default, means let mini-windows grow
24371 only, until their display becomes empty, at which point the windows
24372 go back to their normal size. */);
24373 Vresize_mini_windows = Qgrow_only;
24374
24375 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24376 doc: /* Alist specifying how to blink the cursor off.
24377 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24378 `cursor-type' frame-parameter or variable equals ON-STATE,
24379 comparing using `equal', Emacs uses OFF-STATE to specify
24380 how to blink it off. ON-STATE and OFF-STATE are values for
24381 the `cursor-type' frame parameter.
24382
24383 If a frame's ON-STATE has no entry in this list,
24384 the frame's other specifications determine how to blink the cursor off. */);
24385 Vblink_cursor_alist = Qnil;
24386
24387 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24388 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24389 automatic_hscrolling_p = 1;
24390
24391 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24392 doc: /* *How many columns away from the window edge point is allowed to get
24393 before automatic hscrolling will horizontally scroll the window. */);
24394 hscroll_margin = 5;
24395
24396 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24397 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24398 When point is less than `hscroll-margin' columns from the window
24399 edge, automatic hscrolling will scroll the window by the amount of columns
24400 determined by this variable. If its value is a positive integer, scroll that
24401 many columns. If it's a positive floating-point number, it specifies the
24402 fraction of the window's width to scroll. If it's nil or zero, point will be
24403 centered horizontally after the scroll. Any other value, including negative
24404 numbers, are treated as if the value were zero.
24405
24406 Automatic hscrolling always moves point outside the scroll margin, so if
24407 point was more than scroll step columns inside the margin, the window will
24408 scroll more than the value given by the scroll step.
24409
24410 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24411 and `scroll-right' overrides this variable's effect. */);
24412 Vhscroll_step = make_number (0);
24413
24414 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24415 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24416 Bind this around calls to `message' to let it take effect. */);
24417 message_truncate_lines = 0;
24418
24419 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24420 doc: /* Normal hook run to update the menu bar definitions.
24421 Redisplay runs this hook before it redisplays the menu bar.
24422 This is used to update submenus such as Buffers,
24423 whose contents depend on various data. */);
24424 Vmenu_bar_update_hook = Qnil;
24425
24426 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24427 doc: /* Frame for which we are updating a menu.
24428 The enable predicate for a menu binding should check this variable. */);
24429 Vmenu_updating_frame = Qnil;
24430
24431 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24432 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24433 inhibit_menubar_update = 0;
24434
24435 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24436 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24437 inhibit_eval_during_redisplay = 0;
24438
24439 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24440 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24441 inhibit_free_realized_faces = 0;
24442
24443 #if GLYPH_DEBUG
24444 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24445 doc: /* Inhibit try_window_id display optimization. */);
24446 inhibit_try_window_id = 0;
24447
24448 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24449 doc: /* Inhibit try_window_reusing display optimization. */);
24450 inhibit_try_window_reusing = 0;
24451
24452 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24453 doc: /* Inhibit try_cursor_movement display optimization. */);
24454 inhibit_try_cursor_movement = 0;
24455 #endif /* GLYPH_DEBUG */
24456
24457 DEFVAR_INT ("overline-margin", &overline_margin,
24458 doc: /* *Space between overline and text, in pixels.
24459 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24460 margin to the caracter height. */);
24461 overline_margin = 2;
24462 }
24463
24464
24465 /* Initialize this module when Emacs starts. */
24466
24467 void
24468 init_xdisp ()
24469 {
24470 Lisp_Object root_window;
24471 struct window *mini_w;
24472
24473 current_header_line_height = current_mode_line_height = -1;
24474
24475 CHARPOS (this_line_start_pos) = 0;
24476
24477 mini_w = XWINDOW (minibuf_window);
24478 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24479
24480 if (!noninteractive)
24481 {
24482 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24483 int i;
24484
24485 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24486 set_window_height (root_window,
24487 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24488 0);
24489 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24490 set_window_height (minibuf_window, 1, 0);
24491
24492 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24493 mini_w->total_cols = make_number (FRAME_COLS (f));
24494
24495 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24496 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24497 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24498
24499 /* The default ellipsis glyphs `...'. */
24500 for (i = 0; i < 3; ++i)
24501 default_invis_vector[i] = make_number ('.');
24502 }
24503
24504 {
24505 /* Allocate the buffer for frame titles.
24506 Also used for `format-mode-line'. */
24507 int size = 100;
24508 mode_line_noprop_buf = (char *) xmalloc (size);
24509 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24510 mode_line_noprop_ptr = mode_line_noprop_buf;
24511 mode_line_target = MODE_LINE_DISPLAY;
24512 }
24513
24514 help_echo_showing_p = 0;
24515 }
24516
24517
24518 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24519 (do not change this comment) */