]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Fix redisplay loop in last change.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern Lisp_Object do_mouse_tracking;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
268
269 int make_cursor_line_fully_visible_p;
270
271 /* Margin around tool bar buttons in pixels. */
272
273 Lisp_Object Vtool_bar_button_margin;
274
275 /* Thickness of shadow to draw around tool bar buttons. */
276
277 EMACS_INT tool_bar_button_relief;
278
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
281
282 int auto_resize_tool_bars_p;
283
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
287
288 int x_stretch_cursor_p;
289
290 /* Non-nil means don't actually do any redisplay. */
291
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
293
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
295
296 int inhibit_eval_during_redisplay;
297
298 /* Names of text properties relevant for redisplay. */
299
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
302
303 /* Symbols used in text property values. */
304
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
316
317 /* Non-nil means highlight trailing whitespace. */
318
319 Lisp_Object Vshow_trailing_whitespace;
320
321 /* Non-nil means escape non-break space and hyphens. */
322
323 Lisp_Object Vnobreak_char_display;
324
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern Lisp_Object Voverflow_newline_into_fringe;
327
328 /* Test if overflow newline into fringe. Called with iterator IT
329 at or past right window margin, and with IT->current_x set. */
330
331 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
332 (!NILP (Voverflow_newline_into_fringe) \
333 && FRAME_WINDOW_P (it->f) \
334 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
335 && it->current_x == it->last_visible_x)
336
337 #endif /* HAVE_WINDOW_SYSTEM */
338
339 /* Non-nil means show the text cursor in void text areas
340 i.e. in blank areas after eol and eob. This used to be
341 the default in 21.3. */
342
343 Lisp_Object Vvoid_text_area_pointer;
344
345 /* Name of the face used to highlight trailing whitespace. */
346
347 Lisp_Object Qtrailing_whitespace;
348
349 /* Name and number of the face used to highlight escape glyphs. */
350
351 Lisp_Object Qescape_glyph;
352
353 /* Name and number of the face used to highlight non-breaking spaces. */
354
355 Lisp_Object Qnobreak_space;
356
357 /* The symbol `image' which is the car of the lists used to represent
358 images in Lisp. */
359
360 Lisp_Object Qimage;
361
362 /* The image map types. */
363 Lisp_Object QCmap, QCpointer;
364 Lisp_Object Qrect, Qcircle, Qpoly;
365
366 /* Non-zero means print newline to stdout before next mini-buffer
367 message. */
368
369 int noninteractive_need_newline;
370
371 /* Non-zero means print newline to message log before next message. */
372
373 static int message_log_need_newline;
374
375 /* Three markers that message_dolog uses.
376 It could allocate them itself, but that causes trouble
377 in handling memory-full errors. */
378 static Lisp_Object message_dolog_marker1;
379 static Lisp_Object message_dolog_marker2;
380 static Lisp_Object message_dolog_marker3;
381 \f
382 /* The buffer position of the first character appearing entirely or
383 partially on the line of the selected window which contains the
384 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
385 redisplay optimization in redisplay_internal. */
386
387 static struct text_pos this_line_start_pos;
388
389 /* Number of characters past the end of the line above, including the
390 terminating newline. */
391
392 static struct text_pos this_line_end_pos;
393
394 /* The vertical positions and the height of this line. */
395
396 static int this_line_vpos;
397 static int this_line_y;
398 static int this_line_pixel_height;
399
400 /* X position at which this display line starts. Usually zero;
401 negative if first character is partially visible. */
402
403 static int this_line_start_x;
404
405 /* Buffer that this_line_.* variables are referring to. */
406
407 static struct buffer *this_line_buffer;
408
409 /* Nonzero means truncate lines in all windows less wide than the
410 frame. */
411
412 int truncate_partial_width_windows;
413
414 /* A flag to control how to display unibyte 8-bit character. */
415
416 int unibyte_display_via_language_environment;
417
418 /* Nonzero means we have more than one non-mini-buffer-only frame.
419 Not guaranteed to be accurate except while parsing
420 frame-title-format. */
421
422 int multiple_frames;
423
424 Lisp_Object Vglobal_mode_string;
425
426
427 /* List of variables (symbols) which hold markers for overlay arrows.
428 The symbols on this list are examined during redisplay to determine
429 where to display overlay arrows. */
430
431 Lisp_Object Voverlay_arrow_variable_list;
432
433 /* Marker for where to display an arrow on top of the buffer text. */
434
435 Lisp_Object Voverlay_arrow_position;
436
437 /* String to display for the arrow. Only used on terminal frames. */
438
439 Lisp_Object Voverlay_arrow_string;
440
441 /* Values of those variables at last redisplay are stored as
442 properties on `overlay-arrow-position' symbol. However, if
443 Voverlay_arrow_position is a marker, last-arrow-position is its
444 numerical position. */
445
446 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
447
448 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
449 properties on a symbol in overlay-arrow-variable-list. */
450
451 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
452
453 /* Like mode-line-format, but for the title bar on a visible frame. */
454
455 Lisp_Object Vframe_title_format;
456
457 /* Like mode-line-format, but for the title bar on an iconified frame. */
458
459 Lisp_Object Vicon_title_format;
460
461 /* List of functions to call when a window's size changes. These
462 functions get one arg, a frame on which one or more windows' sizes
463 have changed. */
464
465 static Lisp_Object Vwindow_size_change_functions;
466
467 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
468
469 /* Nonzero if an overlay arrow has been displayed in this window. */
470
471 static int overlay_arrow_seen;
472
473 /* Nonzero means highlight the region even in nonselected windows. */
474
475 int highlight_nonselected_windows;
476
477 /* If cursor motion alone moves point off frame, try scrolling this
478 many lines up or down if that will bring it back. */
479
480 static EMACS_INT scroll_step;
481
482 /* Nonzero means scroll just far enough to bring point back on the
483 screen, when appropriate. */
484
485 static EMACS_INT scroll_conservatively;
486
487 /* Recenter the window whenever point gets within this many lines of
488 the top or bottom of the window. This value is translated into a
489 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
490 that there is really a fixed pixel height scroll margin. */
491
492 EMACS_INT scroll_margin;
493
494 /* Number of windows showing the buffer of the selected window (or
495 another buffer with the same base buffer). keyboard.c refers to
496 this. */
497
498 int buffer_shared;
499
500 /* Vector containing glyphs for an ellipsis `...'. */
501
502 static Lisp_Object default_invis_vector[3];
503
504 /* Zero means display the mode-line/header-line/menu-bar in the default face
505 (this slightly odd definition is for compatibility with previous versions
506 of emacs), non-zero means display them using their respective faces.
507
508 This variable is deprecated. */
509
510 int mode_line_inverse_video;
511
512 /* Prompt to display in front of the mini-buffer contents. */
513
514 Lisp_Object minibuf_prompt;
515
516 /* Width of current mini-buffer prompt. Only set after display_line
517 of the line that contains the prompt. */
518
519 int minibuf_prompt_width;
520
521 /* This is the window where the echo area message was displayed. It
522 is always a mini-buffer window, but it may not be the same window
523 currently active as a mini-buffer. */
524
525 Lisp_Object echo_area_window;
526
527 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
528 pushes the current message and the value of
529 message_enable_multibyte on the stack, the function restore_message
530 pops the stack and displays MESSAGE again. */
531
532 Lisp_Object Vmessage_stack;
533
534 /* Nonzero means multibyte characters were enabled when the echo area
535 message was specified. */
536
537 int message_enable_multibyte;
538
539 /* Nonzero if we should redraw the mode lines on the next redisplay. */
540
541 int update_mode_lines;
542
543 /* Nonzero if window sizes or contents have changed since last
544 redisplay that finished. */
545
546 int windows_or_buffers_changed;
547
548 /* Nonzero means a frame's cursor type has been changed. */
549
550 int cursor_type_changed;
551
552 /* Nonzero after display_mode_line if %l was used and it displayed a
553 line number. */
554
555 int line_number_displayed;
556
557 /* Maximum buffer size for which to display line numbers. */
558
559 Lisp_Object Vline_number_display_limit;
560
561 /* Line width to consider when repositioning for line number display. */
562
563 static EMACS_INT line_number_display_limit_width;
564
565 /* Number of lines to keep in the message log buffer. t means
566 infinite. nil means don't log at all. */
567
568 Lisp_Object Vmessage_log_max;
569
570 /* The name of the *Messages* buffer, a string. */
571
572 static Lisp_Object Vmessages_buffer_name;
573
574 /* Index 0 is the buffer that holds the current (desired) echo area message,
575 or nil if none is desired right now.
576
577 Index 1 is the buffer that holds the previously displayed echo area message,
578 or nil to indicate no message. This is normally what's on the screen now.
579
580 These two can point to the same buffer. That happens when the last
581 message output by the user (or made by echoing) has been displayed. */
582
583 Lisp_Object echo_area_buffer[2];
584
585 /* Permanent pointers to the two buffers that are used for echo area
586 purposes. Once the two buffers are made, and their pointers are
587 placed here, these two slots remain unchanged unless those buffers
588 need to be created afresh. */
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 /* Maximum height for resizing mini-windows. Either a float
612 specifying a fraction of the available height, or an integer
613 specifying a number of lines. */
614
615 Lisp_Object Vmax_mini_window_height;
616
617 /* Non-zero means messages should be displayed with truncated
618 lines instead of being continued. */
619
620 int message_truncate_lines;
621 Lisp_Object Qmessage_truncate_lines;
622
623 /* Set to 1 in clear_message to make redisplay_internal aware
624 of an emptied echo area. */
625
626 static int message_cleared_p;
627
628 /* How to blink the default frame cursor off. */
629 Lisp_Object Vblink_cursor_alist;
630
631 /* A scratch glyph row with contents used for generating truncation
632 glyphs. Also used in direct_output_for_insert. */
633
634 #define MAX_SCRATCH_GLYPHS 100
635 struct glyph_row scratch_glyph_row;
636 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
637
638 /* Ascent and height of the last line processed by move_it_to. */
639
640 static int last_max_ascent, last_height;
641
642 /* Non-zero if there's a help-echo in the echo area. */
643
644 int help_echo_showing_p;
645
646 /* If >= 0, computed, exact values of mode-line and header-line height
647 to use in the macros CURRENT_MODE_LINE_HEIGHT and
648 CURRENT_HEADER_LINE_HEIGHT. */
649
650 int current_mode_line_height, current_header_line_height;
651
652 /* The maximum distance to look ahead for text properties. Values
653 that are too small let us call compute_char_face and similar
654 functions too often which is expensive. Values that are too large
655 let us call compute_char_face and alike too often because we
656 might not be interested in text properties that far away. */
657
658 #define TEXT_PROP_DISTANCE_LIMIT 100
659
660 #if GLYPH_DEBUG
661
662 /* Variables to turn off display optimizations from Lisp. */
663
664 int inhibit_try_window_id, inhibit_try_window_reusing;
665 int inhibit_try_cursor_movement;
666
667 /* Non-zero means print traces of redisplay if compiled with
668 GLYPH_DEBUG != 0. */
669
670 int trace_redisplay_p;
671
672 #endif /* GLYPH_DEBUG */
673
674 #ifdef DEBUG_TRACE_MOVE
675 /* Non-zero means trace with TRACE_MOVE to stderr. */
676 int trace_move;
677
678 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
679 #else
680 #define TRACE_MOVE(x) (void) 0
681 #endif
682
683 /* Non-zero means automatically scroll windows horizontally to make
684 point visible. */
685
686 int automatic_hscrolling_p;
687
688 /* How close to the margin can point get before the window is scrolled
689 horizontally. */
690 EMACS_INT hscroll_margin;
691
692 /* How much to scroll horizontally when point is inside the above margin. */
693 Lisp_Object Vhscroll_step;
694
695 /* The variable `resize-mini-windows'. If nil, don't resize
696 mini-windows. If t, always resize them to fit the text they
697 display. If `grow-only', let mini-windows grow only until they
698 become empty. */
699
700 Lisp_Object Vresize_mini_windows;
701
702 /* Buffer being redisplayed -- for redisplay_window_error. */
703
704 struct buffer *displayed_buffer;
705
706 /* Value returned from text property handlers (see below). */
707
708 enum prop_handled
709 {
710 HANDLED_NORMALLY,
711 HANDLED_RECOMPUTE_PROPS,
712 HANDLED_OVERLAY_STRING_CONSUMED,
713 HANDLED_RETURN
714 };
715
716 /* A description of text properties that redisplay is interested
717 in. */
718
719 struct props
720 {
721 /* The name of the property. */
722 Lisp_Object *name;
723
724 /* A unique index for the property. */
725 enum prop_idx idx;
726
727 /* A handler function called to set up iterator IT from the property
728 at IT's current position. Value is used to steer handle_stop. */
729 enum prop_handled (*handler) P_ ((struct it *it));
730 };
731
732 static enum prop_handled handle_face_prop P_ ((struct it *));
733 static enum prop_handled handle_invisible_prop P_ ((struct it *));
734 static enum prop_handled handle_display_prop P_ ((struct it *));
735 static enum prop_handled handle_composition_prop P_ ((struct it *));
736 static enum prop_handled handle_overlay_change P_ ((struct it *));
737 static enum prop_handled handle_fontified_prop P_ ((struct it *));
738
739 /* Properties handled by iterators. */
740
741 static struct props it_props[] =
742 {
743 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
744 /* Handle `face' before `display' because some sub-properties of
745 `display' need to know the face. */
746 {&Qface, FACE_PROP_IDX, handle_face_prop},
747 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
748 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
749 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
750 {NULL, 0, NULL}
751 };
752
753 /* Value is the position described by X. If X is a marker, value is
754 the marker_position of X. Otherwise, value is X. */
755
756 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
757
758 /* Enumeration returned by some move_it_.* functions internally. */
759
760 enum move_it_result
761 {
762 /* Not used. Undefined value. */
763 MOVE_UNDEFINED,
764
765 /* Move ended at the requested buffer position or ZV. */
766 MOVE_POS_MATCH_OR_ZV,
767
768 /* Move ended at the requested X pixel position. */
769 MOVE_X_REACHED,
770
771 /* Move within a line ended at the end of a line that must be
772 continued. */
773 MOVE_LINE_CONTINUED,
774
775 /* Move within a line ended at the end of a line that would
776 be displayed truncated. */
777 MOVE_LINE_TRUNCATED,
778
779 /* Move within a line ended at a line end. */
780 MOVE_NEWLINE_OR_CR
781 };
782
783 /* This counter is used to clear the face cache every once in a while
784 in redisplay_internal. It is incremented for each redisplay.
785 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
786 cleared. */
787
788 #define CLEAR_FACE_CACHE_COUNT 500
789 static int clear_face_cache_count;
790
791 /* Similarly for the image cache. */
792
793 #ifdef HAVE_WINDOW_SYSTEM
794 #define CLEAR_IMAGE_CACHE_COUNT 101
795 static int clear_image_cache_count;
796 #endif
797
798 /* Record the previous terminal frame we displayed. */
799
800 static struct frame *previous_terminal_frame;
801
802 /* Non-zero while redisplay_internal is in progress. */
803
804 int redisplaying_p;
805
806 /* Non-zero means don't free realized faces. Bound while freeing
807 realized faces is dangerous because glyph matrices might still
808 reference them. */
809
810 int inhibit_free_realized_faces;
811 Lisp_Object Qinhibit_free_realized_faces;
812
813 /* If a string, XTread_socket generates an event to display that string.
814 (The display is done in read_char.) */
815
816 Lisp_Object help_echo_string;
817 Lisp_Object help_echo_window;
818 Lisp_Object help_echo_object;
819 int help_echo_pos;
820
821 /* Temporary variable for XTread_socket. */
822
823 Lisp_Object previous_help_echo_string;
824
825 /* Null glyph slice */
826
827 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
828
829 \f
830 /* Function prototypes. */
831
832 static void setup_for_ellipsis P_ ((struct it *, int));
833 static void mark_window_display_accurate_1 P_ ((struct window *, int));
834 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
835 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
837 static int redisplay_mode_lines P_ ((Lisp_Object, int));
838 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
839
840 #if 0
841 static int invisible_text_between_p P_ ((struct it *, int, int));
842 #endif
843
844 static void pint2str P_ ((char *, int, int));
845 static void pint2hrstr P_ ((char *, int, int));
846 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
847 struct text_pos));
848 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
849 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
850 static void store_mode_line_noprop_char P_ ((char));
851 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
852 static void x_consider_frame_title P_ ((Lisp_Object));
853 static void handle_stop P_ ((struct it *));
854 static int tool_bar_lines_needed P_ ((struct frame *));
855 static int single_display_spec_intangible_p P_ ((Lisp_Object));
856 static void ensure_echo_area_buffers P_ ((void));
857 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
858 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
859 static int with_echo_area_buffer P_ ((struct window *, int,
860 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
861 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static void clear_garbaged_frames P_ ((void));
863 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int display_echo_area P_ ((struct window *));
867 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
870 static int string_char_and_length P_ ((const unsigned char *, int, int *));
871 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
872 struct text_pos));
873 static int compute_window_start_on_continuation_line P_ ((struct window *));
874 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
875 static void insert_left_trunc_glyphs P_ ((struct it *));
876 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
877 Lisp_Object));
878 static void extend_face_to_end_of_line P_ ((struct it *));
879 static int append_space_for_newline P_ ((struct it *, int));
880 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
881 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
882 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
883 static int trailing_whitespace_p P_ ((int));
884 static int message_log_check_duplicate P_ ((int, int, int, int));
885 static void push_it P_ ((struct it *));
886 static void pop_it P_ ((struct it *));
887 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
888 static void select_frame_for_redisplay P_ ((Lisp_Object));
889 static void redisplay_internal P_ ((int));
890 static int echo_area_display P_ ((int));
891 static void redisplay_windows P_ ((Lisp_Object));
892 static void redisplay_window P_ ((Lisp_Object, int));
893 static Lisp_Object redisplay_window_error ();
894 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
895 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
896 static void update_menu_bar P_ ((struct frame *, int));
897 static int try_window_reusing_current_matrix P_ ((struct window *));
898 static int try_window_id P_ ((struct window *));
899 static int display_line P_ ((struct it *));
900 static int display_mode_lines P_ ((struct window *));
901 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
902 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
903 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
904 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
905 static void display_menu_bar P_ ((struct window *));
906 static int display_count_lines P_ ((int, int, int, int, int *));
907 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
908 int, int, struct it *, int, int, int, int));
909 static void compute_line_metrics P_ ((struct it *));
910 static void run_redisplay_end_trigger_hook P_ ((struct it *));
911 static int get_overlay_strings P_ ((struct it *, int));
912 static void next_overlay_string P_ ((struct it *));
913 static void reseat P_ ((struct it *, struct text_pos, int));
914 static void reseat_1 P_ ((struct it *, struct text_pos, int));
915 static void back_to_previous_visible_line_start P_ ((struct it *));
916 void reseat_at_previous_visible_line_start P_ ((struct it *));
917 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
918 static int next_element_from_ellipsis P_ ((struct it *));
919 static int next_element_from_display_vector P_ ((struct it *));
920 static int next_element_from_string P_ ((struct it *));
921 static int next_element_from_c_string P_ ((struct it *));
922 static int next_element_from_buffer P_ ((struct it *));
923 static int next_element_from_composition P_ ((struct it *));
924 static int next_element_from_image P_ ((struct it *));
925 static int next_element_from_stretch P_ ((struct it *));
926 static void load_overlay_strings P_ ((struct it *, int));
927 static int init_from_display_pos P_ ((struct it *, struct window *,
928 struct display_pos *));
929 static void reseat_to_string P_ ((struct it *, unsigned char *,
930 Lisp_Object, int, int, int, int));
931 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
932 int, int, int));
933 void move_it_vertically_backward P_ ((struct it *, int));
934 static void init_to_row_start P_ ((struct it *, struct window *,
935 struct glyph_row *));
936 static int init_to_row_end P_ ((struct it *, struct window *,
937 struct glyph_row *));
938 static void back_to_previous_line_start P_ ((struct it *));
939 static int forward_to_next_line_start P_ ((struct it *, int *));
940 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
941 Lisp_Object, int));
942 static struct text_pos string_pos P_ ((int, Lisp_Object));
943 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
944 static int number_of_chars P_ ((unsigned char *, int));
945 static void compute_stop_pos P_ ((struct it *));
946 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
947 Lisp_Object));
948 static int face_before_or_after_it_pos P_ ((struct it *, int));
949 static int next_overlay_change P_ ((int));
950 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
951 Lisp_Object, struct text_pos *,
952 int));
953 static int underlying_face_id P_ ((struct it *));
954 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
955 struct window *));
956
957 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
958 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
959
960 #ifdef HAVE_WINDOW_SYSTEM
961
962 static void update_tool_bar P_ ((struct frame *, int));
963 static void build_desired_tool_bar_string P_ ((struct frame *f));
964 static int redisplay_tool_bar P_ ((struct frame *));
965 static void display_tool_bar_line P_ ((struct it *));
966 static void notice_overwritten_cursor P_ ((struct window *,
967 enum glyph_row_area,
968 int, int, int, int));
969
970
971
972 #endif /* HAVE_WINDOW_SYSTEM */
973
974 \f
975 /***********************************************************************
976 Window display dimensions
977 ***********************************************************************/
978
979 /* Return the bottom boundary y-position for text lines in window W.
980 This is the first y position at which a line cannot start.
981 It is relative to the top of the window.
982
983 This is the height of W minus the height of a mode line, if any. */
984
985 INLINE int
986 window_text_bottom_y (w)
987 struct window *w;
988 {
989 int height = WINDOW_TOTAL_HEIGHT (w);
990
991 if (WINDOW_WANTS_MODELINE_P (w))
992 height -= CURRENT_MODE_LINE_HEIGHT (w);
993 return height;
994 }
995
996 /* Return the pixel width of display area AREA of window W. AREA < 0
997 means return the total width of W, not including fringes to
998 the left and right of the window. */
999
1000 INLINE int
1001 window_box_width (w, area)
1002 struct window *w;
1003 int area;
1004 {
1005 int cols = XFASTINT (w->total_cols);
1006 int pixels = 0;
1007
1008 if (!w->pseudo_window_p)
1009 {
1010 cols -= WINDOW_SCROLL_BAR_COLS (w);
1011
1012 if (area == TEXT_AREA)
1013 {
1014 if (INTEGERP (w->left_margin_cols))
1015 cols -= XFASTINT (w->left_margin_cols);
1016 if (INTEGERP (w->right_margin_cols))
1017 cols -= XFASTINT (w->right_margin_cols);
1018 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1019 }
1020 else if (area == LEFT_MARGIN_AREA)
1021 {
1022 cols = (INTEGERP (w->left_margin_cols)
1023 ? XFASTINT (w->left_margin_cols) : 0);
1024 pixels = 0;
1025 }
1026 else if (area == RIGHT_MARGIN_AREA)
1027 {
1028 cols = (INTEGERP (w->right_margin_cols)
1029 ? XFASTINT (w->right_margin_cols) : 0);
1030 pixels = 0;
1031 }
1032 }
1033
1034 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1035 }
1036
1037
1038 /* Return the pixel height of the display area of window W, not
1039 including mode lines of W, if any. */
1040
1041 INLINE int
1042 window_box_height (w)
1043 struct window *w;
1044 {
1045 struct frame *f = XFRAME (w->frame);
1046 int height = WINDOW_TOTAL_HEIGHT (w);
1047
1048 xassert (height >= 0);
1049
1050 /* Note: the code below that determines the mode-line/header-line
1051 height is essentially the same as that contained in the macro
1052 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1053 the appropriate glyph row has its `mode_line_p' flag set,
1054 and if it doesn't, uses estimate_mode_line_height instead. */
1055
1056 if (WINDOW_WANTS_MODELINE_P (w))
1057 {
1058 struct glyph_row *ml_row
1059 = (w->current_matrix && w->current_matrix->rows
1060 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1061 : 0);
1062 if (ml_row && ml_row->mode_line_p)
1063 height -= ml_row->height;
1064 else
1065 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1066 }
1067
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1069 {
1070 struct glyph_row *hl_row
1071 = (w->current_matrix && w->current_matrix->rows
1072 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1073 : 0);
1074 if (hl_row && hl_row->mode_line_p)
1075 height -= hl_row->height;
1076 else
1077 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1078 }
1079
1080 /* With a very small font and a mode-line that's taller than
1081 default, we might end up with a negative height. */
1082 return max (0, height);
1083 }
1084
1085 /* Return the window-relative coordinate of the left edge of display
1086 area AREA of window W. AREA < 0 means return the left edge of the
1087 whole window, to the right of the left fringe of W. */
1088
1089 INLINE int
1090 window_box_left_offset (w, area)
1091 struct window *w;
1092 int area;
1093 {
1094 int x;
1095
1096 if (w->pseudo_window_p)
1097 return 0;
1098
1099 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1100
1101 if (area == TEXT_AREA)
1102 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1103 + window_box_width (w, LEFT_MARGIN_AREA));
1104 else if (area == RIGHT_MARGIN_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA)
1107 + window_box_width (w, TEXT_AREA)
1108 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1109 ? 0
1110 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1111 else if (area == LEFT_MARGIN_AREA
1112 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1113 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1114
1115 return x;
1116 }
1117
1118
1119 /* Return the window-relative coordinate of the right edge of display
1120 area AREA of window W. AREA < 0 means return the left edge of the
1121 whole window, to the left of the right fringe of W. */
1122
1123 INLINE int
1124 window_box_right_offset (w, area)
1125 struct window *w;
1126 int area;
1127 {
1128 return window_box_left_offset (w, area) + window_box_width (w, area);
1129 }
1130
1131 /* Return the frame-relative coordinate of the left edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the right of the left fringe of W. */
1134
1135 INLINE int
1136 window_box_left (w, area)
1137 struct window *w;
1138 int area;
1139 {
1140 struct frame *f = XFRAME (w->frame);
1141 int x;
1142
1143 if (w->pseudo_window_p)
1144 return FRAME_INTERNAL_BORDER_WIDTH (f);
1145
1146 x = (WINDOW_LEFT_EDGE_X (w)
1147 + window_box_left_offset (w, area));
1148
1149 return x;
1150 }
1151
1152
1153 /* Return the frame-relative coordinate of the right edge of display
1154 area AREA of window W. AREA < 0 means return the left edge of the
1155 whole window, to the left of the right fringe of W. */
1156
1157 INLINE int
1158 window_box_right (w, area)
1159 struct window *w;
1160 int area;
1161 {
1162 return window_box_left (w, area) + window_box_width (w, area);
1163 }
1164
1165 /* Get the bounding box of the display area AREA of window W, without
1166 mode lines, in frame-relative coordinates. AREA < 0 means the
1167 whole window, not including the left and right fringes of
1168 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1169 coordinates of the upper-left corner of the box. Return in
1170 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1171
1172 INLINE void
1173 window_box (w, area, box_x, box_y, box_width, box_height)
1174 struct window *w;
1175 int area;
1176 int *box_x, *box_y, *box_width, *box_height;
1177 {
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1185 {
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1189 }
1190 }
1191
1192
1193 /* Get the bounding box of the display area AREA of window W, without
1194 mode lines. AREA < 0 means the whole window, not including the
1195 left and right fringe of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1200
1201 INLINE void
1202 window_box_edges (w, area, top_left_x, top_left_y,
1203 bottom_right_x, bottom_right_y)
1204 struct window *w;
1205 int area;
1206 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1207 {
1208 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1209 bottom_right_y);
1210 *bottom_right_x += *top_left_x;
1211 *bottom_right_y += *top_left_y;
1212 }
1213
1214
1215 \f
1216 /***********************************************************************
1217 Utilities
1218 ***********************************************************************/
1219
1220 /* Return the bottom y-position of the line the iterator IT is in.
1221 This can modify IT's settings. */
1222
1223 int
1224 line_bottom_y (it)
1225 struct it *it;
1226 {
1227 int line_height = it->max_ascent + it->max_descent;
1228 int line_top_y = it->current_y;
1229
1230 if (line_height == 0)
1231 {
1232 if (last_height)
1233 line_height = last_height;
1234 else if (IT_CHARPOS (*it) < ZV)
1235 {
1236 move_it_by_lines (it, 1, 1);
1237 line_height = (it->max_ascent || it->max_descent
1238 ? it->max_ascent + it->max_descent
1239 : last_height);
1240 }
1241 else
1242 {
1243 struct glyph_row *row = it->glyph_row;
1244
1245 /* Use the default character height. */
1246 it->glyph_row = NULL;
1247 it->what = IT_CHARACTER;
1248 it->c = ' ';
1249 it->len = 1;
1250 PRODUCE_GLYPHS (it);
1251 line_height = it->ascent + it->descent;
1252 it->glyph_row = row;
1253 }
1254 }
1255
1256 return line_top_y + line_height;
1257 }
1258
1259
1260 /* Return 1 if position CHARPOS is visible in window W.
1261 If visible, set *X and *Y to pixel coordinates of top left corner.
1262 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1263 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1264 and header-lines heights. */
1265
1266 int
1267 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1268 struct window *w;
1269 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1270 {
1271 struct it it;
1272 struct text_pos top;
1273 int visible_p = 0;
1274 struct buffer *old_buffer = NULL;
1275
1276 if (noninteractive)
1277 return visible_p;
1278
1279 if (XBUFFER (w->buffer) != current_buffer)
1280 {
1281 old_buffer = current_buffer;
1282 set_buffer_internal_1 (XBUFFER (w->buffer));
1283 }
1284
1285 SET_TEXT_POS_FROM_MARKER (top, w->start);
1286
1287 /* Compute exact mode line heights, if requested. */
1288 if (exact_mode_line_heights_p)
1289 {
1290 if (WINDOW_WANTS_MODELINE_P (w))
1291 current_mode_line_height
1292 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1293 current_buffer->mode_line_format);
1294
1295 if (WINDOW_WANTS_HEADER_LINE_P (w))
1296 current_header_line_height
1297 = display_mode_line (w, HEADER_LINE_FACE_ID,
1298 current_buffer->header_line_format);
1299 }
1300
1301 start_display (&it, w, top);
1302 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1303 MOVE_TO_POS | MOVE_TO_Y);
1304
1305 /* Note that we may overshoot because of invisible text. */
1306 if (IT_CHARPOS (it) >= charpos)
1307 {
1308 int top_x = it.current_x;
1309 int top_y = it.current_y;
1310 int bottom_y = (last_height = 0, line_bottom_y (&it));
1311 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1312
1313 if (top_y < window_top_y)
1314 visible_p = bottom_y > window_top_y;
1315 else if (top_y < it.last_visible_y)
1316 visible_p = 1;
1317 if (visible_p)
1318 {
1319 *x = top_x;
1320 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1321 *rtop = max (0, window_top_y - top_y);
1322 *rbot = max (0, bottom_y - it.last_visible_y);
1323 }
1324 }
1325 else
1326 {
1327 struct it it2;
1328
1329 it2 = it;
1330 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1331 move_it_by_lines (&it, 1, 0);
1332 if (charpos < IT_CHARPOS (it))
1333 {
1334 visible_p = 1;
1335 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1336 *x = it2.current_x;
1337 *y = it2.current_y + it2.max_ascent - it2.ascent;
1338 *rtop = max (0, -it2.current_y);
1339 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1340 - it.last_visible_y));
1341 }
1342 }
1343
1344 if (old_buffer)
1345 set_buffer_internal_1 (old_buffer);
1346
1347 current_header_line_height = current_mode_line_height = -1;
1348
1349 return visible_p;
1350 }
1351
1352
1353 /* Return the next character from STR which is MAXLEN bytes long.
1354 Return in *LEN the length of the character. This is like
1355 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1356 we find one, we return a `?', but with the length of the invalid
1357 character. */
1358
1359 static INLINE int
1360 string_char_and_length (str, maxlen, len)
1361 const unsigned char *str;
1362 int maxlen, *len;
1363 {
1364 int c;
1365
1366 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1367 if (!CHAR_VALID_P (c, 1))
1368 /* We may not change the length here because other places in Emacs
1369 don't use this function, i.e. they silently accept invalid
1370 characters. */
1371 c = '?';
1372
1373 return c;
1374 }
1375
1376
1377
1378 /* Given a position POS containing a valid character and byte position
1379 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1380
1381 static struct text_pos
1382 string_pos_nchars_ahead (pos, string, nchars)
1383 struct text_pos pos;
1384 Lisp_Object string;
1385 int nchars;
1386 {
1387 xassert (STRINGP (string) && nchars >= 0);
1388
1389 if (STRING_MULTIBYTE (string))
1390 {
1391 int rest = SBYTES (string) - BYTEPOS (pos);
1392 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1393 int len;
1394
1395 while (nchars--)
1396 {
1397 string_char_and_length (p, rest, &len);
1398 p += len, rest -= len;
1399 xassert (rest >= 0);
1400 CHARPOS (pos) += 1;
1401 BYTEPOS (pos) += len;
1402 }
1403 }
1404 else
1405 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1406
1407 return pos;
1408 }
1409
1410
1411 /* Value is the text position, i.e. character and byte position,
1412 for character position CHARPOS in STRING. */
1413
1414 static INLINE struct text_pos
1415 string_pos (charpos, string)
1416 int charpos;
1417 Lisp_Object string;
1418 {
1419 struct text_pos pos;
1420 xassert (STRINGP (string));
1421 xassert (charpos >= 0);
1422 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1423 return pos;
1424 }
1425
1426
1427 /* Value is a text position, i.e. character and byte position, for
1428 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1429 means recognize multibyte characters. */
1430
1431 static struct text_pos
1432 c_string_pos (charpos, s, multibyte_p)
1433 int charpos;
1434 unsigned char *s;
1435 int multibyte_p;
1436 {
1437 struct text_pos pos;
1438
1439 xassert (s != NULL);
1440 xassert (charpos >= 0);
1441
1442 if (multibyte_p)
1443 {
1444 int rest = strlen (s), len;
1445
1446 SET_TEXT_POS (pos, 0, 0);
1447 while (charpos--)
1448 {
1449 string_char_and_length (s, rest, &len);
1450 s += len, rest -= len;
1451 xassert (rest >= 0);
1452 CHARPOS (pos) += 1;
1453 BYTEPOS (pos) += len;
1454 }
1455 }
1456 else
1457 SET_TEXT_POS (pos, charpos, charpos);
1458
1459 return pos;
1460 }
1461
1462
1463 /* Value is the number of characters in C string S. MULTIBYTE_P
1464 non-zero means recognize multibyte characters. */
1465
1466 static int
1467 number_of_chars (s, multibyte_p)
1468 unsigned char *s;
1469 int multibyte_p;
1470 {
1471 int nchars;
1472
1473 if (multibyte_p)
1474 {
1475 int rest = strlen (s), len;
1476 unsigned char *p = (unsigned char *) s;
1477
1478 for (nchars = 0; rest > 0; ++nchars)
1479 {
1480 string_char_and_length (p, rest, &len);
1481 rest -= len, p += len;
1482 }
1483 }
1484 else
1485 nchars = strlen (s);
1486
1487 return nchars;
1488 }
1489
1490
1491 /* Compute byte position NEWPOS->bytepos corresponding to
1492 NEWPOS->charpos. POS is a known position in string STRING.
1493 NEWPOS->charpos must be >= POS.charpos. */
1494
1495 static void
1496 compute_string_pos (newpos, pos, string)
1497 struct text_pos *newpos, pos;
1498 Lisp_Object string;
1499 {
1500 xassert (STRINGP (string));
1501 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1502
1503 if (STRING_MULTIBYTE (string))
1504 *newpos = string_pos_nchars_ahead (pos, string,
1505 CHARPOS (*newpos) - CHARPOS (pos));
1506 else
1507 BYTEPOS (*newpos) = CHARPOS (*newpos);
1508 }
1509
1510 /* EXPORT:
1511 Return an estimation of the pixel height of mode or top lines on
1512 frame F. FACE_ID specifies what line's height to estimate. */
1513
1514 int
1515 estimate_mode_line_height (f, face_id)
1516 struct frame *f;
1517 enum face_id face_id;
1518 {
1519 #ifdef HAVE_WINDOW_SYSTEM
1520 if (FRAME_WINDOW_P (f))
1521 {
1522 int height = FONT_HEIGHT (FRAME_FONT (f));
1523
1524 /* This function is called so early when Emacs starts that the face
1525 cache and mode line face are not yet initialized. */
1526 if (FRAME_FACE_CACHE (f))
1527 {
1528 struct face *face = FACE_FROM_ID (f, face_id);
1529 if (face)
1530 {
1531 if (face->font)
1532 height = FONT_HEIGHT (face->font);
1533 if (face->box_line_width > 0)
1534 height += 2 * face->box_line_width;
1535 }
1536 }
1537
1538 return height;
1539 }
1540 #endif
1541
1542 return 1;
1543 }
1544
1545 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1546 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1547 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1548 not force the value into range. */
1549
1550 void
1551 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1552 FRAME_PTR f;
1553 register int pix_x, pix_y;
1554 int *x, *y;
1555 NativeRectangle *bounds;
1556 int noclip;
1557 {
1558
1559 #ifdef HAVE_WINDOW_SYSTEM
1560 if (FRAME_WINDOW_P (f))
1561 {
1562 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1563 even for negative values. */
1564 if (pix_x < 0)
1565 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1566 if (pix_y < 0)
1567 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1568
1569 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1570 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1571
1572 if (bounds)
1573 STORE_NATIVE_RECT (*bounds,
1574 FRAME_COL_TO_PIXEL_X (f, pix_x),
1575 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1576 FRAME_COLUMN_WIDTH (f) - 1,
1577 FRAME_LINE_HEIGHT (f) - 1);
1578
1579 if (!noclip)
1580 {
1581 if (pix_x < 0)
1582 pix_x = 0;
1583 else if (pix_x > FRAME_TOTAL_COLS (f))
1584 pix_x = FRAME_TOTAL_COLS (f);
1585
1586 if (pix_y < 0)
1587 pix_y = 0;
1588 else if (pix_y > FRAME_LINES (f))
1589 pix_y = FRAME_LINES (f);
1590 }
1591 }
1592 #endif
1593
1594 *x = pix_x;
1595 *y = pix_y;
1596 }
1597
1598
1599 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1600 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1601 can't tell the positions because W's display is not up to date,
1602 return 0. */
1603
1604 int
1605 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1606 struct window *w;
1607 int hpos, vpos;
1608 int *frame_x, *frame_y;
1609 {
1610 #ifdef HAVE_WINDOW_SYSTEM
1611 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1612 {
1613 int success_p;
1614
1615 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1616 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1617
1618 if (display_completed)
1619 {
1620 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1621 struct glyph *glyph = row->glyphs[TEXT_AREA];
1622 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1623
1624 hpos = row->x;
1625 vpos = row->y;
1626 while (glyph < end)
1627 {
1628 hpos += glyph->pixel_width;
1629 ++glyph;
1630 }
1631
1632 /* If first glyph is partially visible, its first visible position is still 0. */
1633 if (hpos < 0)
1634 hpos = 0;
1635
1636 success_p = 1;
1637 }
1638 else
1639 {
1640 hpos = vpos = 0;
1641 success_p = 0;
1642 }
1643
1644 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1645 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1646 return success_p;
1647 }
1648 #endif
1649
1650 *frame_x = hpos;
1651 *frame_y = vpos;
1652 return 1;
1653 }
1654
1655
1656 #ifdef HAVE_WINDOW_SYSTEM
1657
1658 /* Find the glyph under window-relative coordinates X/Y in window W.
1659 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1660 strings. Return in *HPOS and *VPOS the row and column number of
1661 the glyph found. Return in *AREA the glyph area containing X.
1662 Value is a pointer to the glyph found or null if X/Y is not on
1663 text, or we can't tell because W's current matrix is not up to
1664 date. */
1665
1666 static struct glyph *
1667 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1668 struct window *w;
1669 int x, y;
1670 int *hpos, *vpos, *dx, *dy, *area;
1671 {
1672 struct glyph *glyph, *end;
1673 struct glyph_row *row = NULL;
1674 int x0, i;
1675
1676 /* Find row containing Y. Give up if some row is not enabled. */
1677 for (i = 0; i < w->current_matrix->nrows; ++i)
1678 {
1679 row = MATRIX_ROW (w->current_matrix, i);
1680 if (!row->enabled_p)
1681 return NULL;
1682 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1683 break;
1684 }
1685
1686 *vpos = i;
1687 *hpos = 0;
1688
1689 /* Give up if Y is not in the window. */
1690 if (i == w->current_matrix->nrows)
1691 return NULL;
1692
1693 /* Get the glyph area containing X. */
1694 if (w->pseudo_window_p)
1695 {
1696 *area = TEXT_AREA;
1697 x0 = 0;
1698 }
1699 else
1700 {
1701 if (x < window_box_left_offset (w, TEXT_AREA))
1702 {
1703 *area = LEFT_MARGIN_AREA;
1704 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1705 }
1706 else if (x < window_box_right_offset (w, TEXT_AREA))
1707 {
1708 *area = TEXT_AREA;
1709 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1710 }
1711 else
1712 {
1713 *area = RIGHT_MARGIN_AREA;
1714 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1715 }
1716 }
1717
1718 /* Find glyph containing X. */
1719 glyph = row->glyphs[*area];
1720 end = glyph + row->used[*area];
1721 x -= x0;
1722 while (glyph < end && x >= glyph->pixel_width)
1723 {
1724 x -= glyph->pixel_width;
1725 ++glyph;
1726 }
1727
1728 if (glyph == end)
1729 return NULL;
1730
1731 if (dx)
1732 {
1733 *dx = x;
1734 *dy = y - (row->y + row->ascent - glyph->ascent);
1735 }
1736
1737 *hpos = glyph - row->glyphs[*area];
1738 return glyph;
1739 }
1740
1741
1742 /* EXPORT:
1743 Convert frame-relative x/y to coordinates relative to window W.
1744 Takes pseudo-windows into account. */
1745
1746 void
1747 frame_to_window_pixel_xy (w, x, y)
1748 struct window *w;
1749 int *x, *y;
1750 {
1751 if (w->pseudo_window_p)
1752 {
1753 /* A pseudo-window is always full-width, and starts at the
1754 left edge of the frame, plus a frame border. */
1755 struct frame *f = XFRAME (w->frame);
1756 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1757 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1758 }
1759 else
1760 {
1761 *x -= WINDOW_LEFT_EDGE_X (w);
1762 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1763 }
1764 }
1765
1766 /* EXPORT:
1767 Return in *R the clipping rectangle for glyph string S. */
1768
1769 void
1770 get_glyph_string_clip_rect (s, nr)
1771 struct glyph_string *s;
1772 NativeRectangle *nr;
1773 {
1774 XRectangle r;
1775
1776 if (s->row->full_width_p)
1777 {
1778 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1779 r.x = WINDOW_LEFT_EDGE_X (s->w);
1780 r.width = WINDOW_TOTAL_WIDTH (s->w);
1781
1782 /* Unless displaying a mode or menu bar line, which are always
1783 fully visible, clip to the visible part of the row. */
1784 if (s->w->pseudo_window_p)
1785 r.height = s->row->visible_height;
1786 else
1787 r.height = s->height;
1788 }
1789 else
1790 {
1791 /* This is a text line that may be partially visible. */
1792 r.x = window_box_left (s->w, s->area);
1793 r.width = window_box_width (s->w, s->area);
1794 r.height = s->row->visible_height;
1795 }
1796
1797 if (s->clip_head)
1798 if (r.x < s->clip_head->x)
1799 {
1800 if (r.width >= s->clip_head->x - r.x)
1801 r.width -= s->clip_head->x - r.x;
1802 else
1803 r.width = 0;
1804 r.x = s->clip_head->x;
1805 }
1806 if (s->clip_tail)
1807 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1808 {
1809 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1810 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1811 else
1812 r.width = 0;
1813 }
1814
1815 /* If S draws overlapping rows, it's sufficient to use the top and
1816 bottom of the window for clipping because this glyph string
1817 intentionally draws over other lines. */
1818 if (s->for_overlaps_p)
1819 {
1820 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1821 r.height = window_text_bottom_y (s->w) - r.y;
1822 }
1823 else
1824 {
1825 /* Don't use S->y for clipping because it doesn't take partially
1826 visible lines into account. For example, it can be negative for
1827 partially visible lines at the top of a window. */
1828 if (!s->row->full_width_p
1829 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1830 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1831 else
1832 r.y = max (0, s->row->y);
1833
1834 /* If drawing a tool-bar window, draw it over the internal border
1835 at the top of the window. */
1836 if (WINDOWP (s->f->tool_bar_window)
1837 && s->w == XWINDOW (s->f->tool_bar_window))
1838 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1839 }
1840
1841 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1842
1843 /* If drawing the cursor, don't let glyph draw outside its
1844 advertised boundaries. Cleartype does this under some circumstances. */
1845 if (s->hl == DRAW_CURSOR)
1846 {
1847 struct glyph *glyph = s->first_glyph;
1848 int height, max_y;
1849
1850 if (s->x > r.x)
1851 {
1852 r.width -= s->x - r.x;
1853 r.x = s->x;
1854 }
1855 r.width = min (r.width, glyph->pixel_width);
1856
1857 /* If r.y is below window bottom, ensure that we still see a cursor. */
1858 height = min (glyph->ascent + glyph->descent,
1859 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1860 max_y = window_text_bottom_y (s->w) - height;
1861 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1862 if (s->ybase - glyph->ascent > max_y)
1863 {
1864 r.y = max_y;
1865 r.height = height;
1866 }
1867 else
1868 {
1869 /* Don't draw cursor glyph taller than our actual glyph. */
1870 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1871 if (height < r.height)
1872 {
1873 max_y = r.y + r.height;
1874 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1875 r.height = min (max_y - r.y, height);
1876 }
1877 }
1878 }
1879
1880 #ifdef CONVERT_FROM_XRECT
1881 CONVERT_FROM_XRECT (r, *nr);
1882 #else
1883 *nr = r;
1884 #endif
1885 }
1886
1887
1888 /* EXPORT:
1889 Return the position and height of the phys cursor in window W.
1890 Set w->phys_cursor_width to width of phys cursor.
1891 */
1892
1893 int
1894 get_phys_cursor_geometry (w, row, glyph, heightp)
1895 struct window *w;
1896 struct glyph_row *row;
1897 struct glyph *glyph;
1898 int *heightp;
1899 {
1900 struct frame *f = XFRAME (WINDOW_FRAME (w));
1901 int y, wd, h, h0, y0;
1902
1903 /* Compute the width of the rectangle to draw. If on a stretch
1904 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1905 rectangle as wide as the glyph, but use a canonical character
1906 width instead. */
1907 wd = glyph->pixel_width - 1;
1908 #ifdef HAVE_NTGUI
1909 wd++; /* Why? */
1910 #endif
1911 if (glyph->type == STRETCH_GLYPH
1912 && !x_stretch_cursor_p)
1913 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1914 w->phys_cursor_width = wd;
1915
1916 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1917
1918 /* If y is below window bottom, ensure that we still see a cursor. */
1919 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1920
1921 h = max (h0, glyph->ascent + glyph->descent);
1922 h0 = min (h0, glyph->ascent + glyph->descent);
1923
1924 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1925 if (y < y0)
1926 {
1927 h = max (h - (y0 - y) + 1, h0);
1928 y = y0 - 1;
1929 }
1930 else
1931 {
1932 y0 = window_text_bottom_y (w) - h0;
1933 if (y > y0)
1934 {
1935 h += y - y0;
1936 y = y0;
1937 }
1938 }
1939
1940 *heightp = h - 1;
1941 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1942 }
1943
1944
1945 #endif /* HAVE_WINDOW_SYSTEM */
1946
1947 \f
1948 /***********************************************************************
1949 Lisp form evaluation
1950 ***********************************************************************/
1951
1952 /* Error handler for safe_eval and safe_call. */
1953
1954 static Lisp_Object
1955 safe_eval_handler (arg)
1956 Lisp_Object arg;
1957 {
1958 add_to_log ("Error during redisplay: %s", arg, Qnil);
1959 return Qnil;
1960 }
1961
1962
1963 /* Evaluate SEXPR and return the result, or nil if something went
1964 wrong. Prevent redisplay during the evaluation. */
1965
1966 Lisp_Object
1967 safe_eval (sexpr)
1968 Lisp_Object sexpr;
1969 {
1970 Lisp_Object val;
1971
1972 if (inhibit_eval_during_redisplay)
1973 val = Qnil;
1974 else
1975 {
1976 int count = SPECPDL_INDEX ();
1977 struct gcpro gcpro1;
1978
1979 GCPRO1 (sexpr);
1980 specbind (Qinhibit_redisplay, Qt);
1981 /* Use Qt to ensure debugger does not run,
1982 so there is no possibility of wanting to redisplay. */
1983 val = internal_condition_case_1 (Feval, sexpr, Qt,
1984 safe_eval_handler);
1985 UNGCPRO;
1986 val = unbind_to (count, val);
1987 }
1988
1989 return val;
1990 }
1991
1992
1993 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1994 Return the result, or nil if something went wrong. Prevent
1995 redisplay during the evaluation. */
1996
1997 Lisp_Object
1998 safe_call (nargs, args)
1999 int nargs;
2000 Lisp_Object *args;
2001 {
2002 Lisp_Object val;
2003
2004 if (inhibit_eval_during_redisplay)
2005 val = Qnil;
2006 else
2007 {
2008 int count = SPECPDL_INDEX ();
2009 struct gcpro gcpro1;
2010
2011 GCPRO1 (args[0]);
2012 gcpro1.nvars = nargs;
2013 specbind (Qinhibit_redisplay, Qt);
2014 /* Use Qt to ensure debugger does not run,
2015 so there is no possibility of wanting to redisplay. */
2016 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2017 safe_eval_handler);
2018 UNGCPRO;
2019 val = unbind_to (count, val);
2020 }
2021
2022 return val;
2023 }
2024
2025
2026 /* Call function FN with one argument ARG.
2027 Return the result, or nil if something went wrong. */
2028
2029 Lisp_Object
2030 safe_call1 (fn, arg)
2031 Lisp_Object fn, arg;
2032 {
2033 Lisp_Object args[2];
2034 args[0] = fn;
2035 args[1] = arg;
2036 return safe_call (2, args);
2037 }
2038
2039
2040 \f
2041 /***********************************************************************
2042 Debugging
2043 ***********************************************************************/
2044
2045 #if 0
2046
2047 /* Define CHECK_IT to perform sanity checks on iterators.
2048 This is for debugging. It is too slow to do unconditionally. */
2049
2050 static void
2051 check_it (it)
2052 struct it *it;
2053 {
2054 if (it->method == GET_FROM_STRING)
2055 {
2056 xassert (STRINGP (it->string));
2057 xassert (IT_STRING_CHARPOS (*it) >= 0);
2058 }
2059 else
2060 {
2061 xassert (IT_STRING_CHARPOS (*it) < 0);
2062 if (it->method == GET_FROM_BUFFER)
2063 {
2064 /* Check that character and byte positions agree. */
2065 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2066 }
2067 }
2068
2069 if (it->dpvec)
2070 xassert (it->current.dpvec_index >= 0);
2071 else
2072 xassert (it->current.dpvec_index < 0);
2073 }
2074
2075 #define CHECK_IT(IT) check_it ((IT))
2076
2077 #else /* not 0 */
2078
2079 #define CHECK_IT(IT) (void) 0
2080
2081 #endif /* not 0 */
2082
2083
2084 #if GLYPH_DEBUG
2085
2086 /* Check that the window end of window W is what we expect it
2087 to be---the last row in the current matrix displaying text. */
2088
2089 static void
2090 check_window_end (w)
2091 struct window *w;
2092 {
2093 if (!MINI_WINDOW_P (w)
2094 && !NILP (w->window_end_valid))
2095 {
2096 struct glyph_row *row;
2097 xassert ((row = MATRIX_ROW (w->current_matrix,
2098 XFASTINT (w->window_end_vpos)),
2099 !row->enabled_p
2100 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2101 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2102 }
2103 }
2104
2105 #define CHECK_WINDOW_END(W) check_window_end ((W))
2106
2107 #else /* not GLYPH_DEBUG */
2108
2109 #define CHECK_WINDOW_END(W) (void) 0
2110
2111 #endif /* not GLYPH_DEBUG */
2112
2113
2114 \f
2115 /***********************************************************************
2116 Iterator initialization
2117 ***********************************************************************/
2118
2119 /* Initialize IT for displaying current_buffer in window W, starting
2120 at character position CHARPOS. CHARPOS < 0 means that no buffer
2121 position is specified which is useful when the iterator is assigned
2122 a position later. BYTEPOS is the byte position corresponding to
2123 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2124
2125 If ROW is not null, calls to produce_glyphs with IT as parameter
2126 will produce glyphs in that row.
2127
2128 BASE_FACE_ID is the id of a base face to use. It must be one of
2129 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2130 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2131 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2132
2133 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2134 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2135 will be initialized to use the corresponding mode line glyph row of
2136 the desired matrix of W. */
2137
2138 void
2139 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2140 struct it *it;
2141 struct window *w;
2142 int charpos, bytepos;
2143 struct glyph_row *row;
2144 enum face_id base_face_id;
2145 {
2146 int highlight_region_p;
2147
2148 /* Some precondition checks. */
2149 xassert (w != NULL && it != NULL);
2150 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2151 && charpos <= ZV));
2152
2153 /* If face attributes have been changed since the last redisplay,
2154 free realized faces now because they depend on face definitions
2155 that might have changed. Don't free faces while there might be
2156 desired matrices pending which reference these faces. */
2157 if (face_change_count && !inhibit_free_realized_faces)
2158 {
2159 face_change_count = 0;
2160 free_all_realized_faces (Qnil);
2161 }
2162
2163 /* Use one of the mode line rows of W's desired matrix if
2164 appropriate. */
2165 if (row == NULL)
2166 {
2167 if (base_face_id == MODE_LINE_FACE_ID
2168 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2169 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2170 else if (base_face_id == HEADER_LINE_FACE_ID)
2171 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2172 }
2173
2174 /* Clear IT. */
2175 bzero (it, sizeof *it);
2176 it->current.overlay_string_index = -1;
2177 it->current.dpvec_index = -1;
2178 it->base_face_id = base_face_id;
2179 it->string = Qnil;
2180 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2181
2182 /* The window in which we iterate over current_buffer: */
2183 XSETWINDOW (it->window, w);
2184 it->w = w;
2185 it->f = XFRAME (w->frame);
2186
2187 /* Extra space between lines (on window systems only). */
2188 if (base_face_id == DEFAULT_FACE_ID
2189 && FRAME_WINDOW_P (it->f))
2190 {
2191 if (NATNUMP (current_buffer->extra_line_spacing))
2192 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2193 else if (FLOATP (current_buffer->extra_line_spacing))
2194 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2195 * FRAME_LINE_HEIGHT (it->f));
2196 else if (it->f->extra_line_spacing > 0)
2197 it->extra_line_spacing = it->f->extra_line_spacing;
2198 it->max_extra_line_spacing = 0;
2199 }
2200
2201 /* If realized faces have been removed, e.g. because of face
2202 attribute changes of named faces, recompute them. When running
2203 in batch mode, the face cache of Vterminal_frame is null. If
2204 we happen to get called, make a dummy face cache. */
2205 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2206 init_frame_faces (it->f);
2207 if (FRAME_FACE_CACHE (it->f)->used == 0)
2208 recompute_basic_faces (it->f);
2209
2210 /* Current value of the `slice', `space-width', and 'height' properties. */
2211 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2212 it->space_width = Qnil;
2213 it->font_height = Qnil;
2214 it->override_ascent = -1;
2215
2216 /* Are control characters displayed as `^C'? */
2217 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2218
2219 /* -1 means everything between a CR and the following line end
2220 is invisible. >0 means lines indented more than this value are
2221 invisible. */
2222 it->selective = (INTEGERP (current_buffer->selective_display)
2223 ? XFASTINT (current_buffer->selective_display)
2224 : (!NILP (current_buffer->selective_display)
2225 ? -1 : 0));
2226 it->selective_display_ellipsis_p
2227 = !NILP (current_buffer->selective_display_ellipses);
2228
2229 /* Display table to use. */
2230 it->dp = window_display_table (w);
2231
2232 /* Are multibyte characters enabled in current_buffer? */
2233 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2234
2235 /* Non-zero if we should highlight the region. */
2236 highlight_region_p
2237 = (!NILP (Vtransient_mark_mode)
2238 && !NILP (current_buffer->mark_active)
2239 && XMARKER (current_buffer->mark)->buffer != 0);
2240
2241 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2242 start and end of a visible region in window IT->w. Set both to
2243 -1 to indicate no region. */
2244 if (highlight_region_p
2245 /* Maybe highlight only in selected window. */
2246 && (/* Either show region everywhere. */
2247 highlight_nonselected_windows
2248 /* Or show region in the selected window. */
2249 || w == XWINDOW (selected_window)
2250 /* Or show the region if we are in the mini-buffer and W is
2251 the window the mini-buffer refers to. */
2252 || (MINI_WINDOW_P (XWINDOW (selected_window))
2253 && WINDOWP (minibuf_selected_window)
2254 && w == XWINDOW (minibuf_selected_window))))
2255 {
2256 int charpos = marker_position (current_buffer->mark);
2257 it->region_beg_charpos = min (PT, charpos);
2258 it->region_end_charpos = max (PT, charpos);
2259 }
2260 else
2261 it->region_beg_charpos = it->region_end_charpos = -1;
2262
2263 /* Get the position at which the redisplay_end_trigger hook should
2264 be run, if it is to be run at all. */
2265 if (MARKERP (w->redisplay_end_trigger)
2266 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2267 it->redisplay_end_trigger_charpos
2268 = marker_position (w->redisplay_end_trigger);
2269 else if (INTEGERP (w->redisplay_end_trigger))
2270 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2271
2272 /* Correct bogus values of tab_width. */
2273 it->tab_width = XINT (current_buffer->tab_width);
2274 if (it->tab_width <= 0 || it->tab_width > 1000)
2275 it->tab_width = 8;
2276
2277 /* Are lines in the display truncated? */
2278 it->truncate_lines_p
2279 = (base_face_id != DEFAULT_FACE_ID
2280 || XINT (it->w->hscroll)
2281 || (truncate_partial_width_windows
2282 && !WINDOW_FULL_WIDTH_P (it->w))
2283 || !NILP (current_buffer->truncate_lines));
2284
2285 /* Get dimensions of truncation and continuation glyphs. These are
2286 displayed as fringe bitmaps under X, so we don't need them for such
2287 frames. */
2288 if (!FRAME_WINDOW_P (it->f))
2289 {
2290 if (it->truncate_lines_p)
2291 {
2292 /* We will need the truncation glyph. */
2293 xassert (it->glyph_row == NULL);
2294 produce_special_glyphs (it, IT_TRUNCATION);
2295 it->truncation_pixel_width = it->pixel_width;
2296 }
2297 else
2298 {
2299 /* We will need the continuation glyph. */
2300 xassert (it->glyph_row == NULL);
2301 produce_special_glyphs (it, IT_CONTINUATION);
2302 it->continuation_pixel_width = it->pixel_width;
2303 }
2304
2305 /* Reset these values to zero because the produce_special_glyphs
2306 above has changed them. */
2307 it->pixel_width = it->ascent = it->descent = 0;
2308 it->phys_ascent = it->phys_descent = 0;
2309 }
2310
2311 /* Set this after getting the dimensions of truncation and
2312 continuation glyphs, so that we don't produce glyphs when calling
2313 produce_special_glyphs, above. */
2314 it->glyph_row = row;
2315 it->area = TEXT_AREA;
2316
2317 /* Get the dimensions of the display area. The display area
2318 consists of the visible window area plus a horizontally scrolled
2319 part to the left of the window. All x-values are relative to the
2320 start of this total display area. */
2321 if (base_face_id != DEFAULT_FACE_ID)
2322 {
2323 /* Mode lines, menu bar in terminal frames. */
2324 it->first_visible_x = 0;
2325 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2326 }
2327 else
2328 {
2329 it->first_visible_x
2330 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2331 it->last_visible_x = (it->first_visible_x
2332 + window_box_width (w, TEXT_AREA));
2333
2334 /* If we truncate lines, leave room for the truncator glyph(s) at
2335 the right margin. Otherwise, leave room for the continuation
2336 glyph(s). Truncation and continuation glyphs are not inserted
2337 for window-based redisplay. */
2338 if (!FRAME_WINDOW_P (it->f))
2339 {
2340 if (it->truncate_lines_p)
2341 it->last_visible_x -= it->truncation_pixel_width;
2342 else
2343 it->last_visible_x -= it->continuation_pixel_width;
2344 }
2345
2346 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2347 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2348 }
2349
2350 /* Leave room for a border glyph. */
2351 if (!FRAME_WINDOW_P (it->f)
2352 && !WINDOW_RIGHTMOST_P (it->w))
2353 it->last_visible_x -= 1;
2354
2355 it->last_visible_y = window_text_bottom_y (w);
2356
2357 /* For mode lines and alike, arrange for the first glyph having a
2358 left box line if the face specifies a box. */
2359 if (base_face_id != DEFAULT_FACE_ID)
2360 {
2361 struct face *face;
2362
2363 it->face_id = base_face_id;
2364
2365 /* If we have a boxed mode line, make the first character appear
2366 with a left box line. */
2367 face = FACE_FROM_ID (it->f, base_face_id);
2368 if (face->box != FACE_NO_BOX)
2369 it->start_of_box_run_p = 1;
2370 }
2371
2372 /* If a buffer position was specified, set the iterator there,
2373 getting overlays and face properties from that position. */
2374 if (charpos >= BUF_BEG (current_buffer))
2375 {
2376 it->end_charpos = ZV;
2377 it->face_id = -1;
2378 IT_CHARPOS (*it) = charpos;
2379
2380 /* Compute byte position if not specified. */
2381 if (bytepos < charpos)
2382 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2383 else
2384 IT_BYTEPOS (*it) = bytepos;
2385
2386 it->start = it->current;
2387
2388 /* Compute faces etc. */
2389 reseat (it, it->current.pos, 1);
2390 }
2391
2392 CHECK_IT (it);
2393 }
2394
2395
2396 /* Initialize IT for the display of window W with window start POS. */
2397
2398 void
2399 start_display (it, w, pos)
2400 struct it *it;
2401 struct window *w;
2402 struct text_pos pos;
2403 {
2404 struct glyph_row *row;
2405 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2406
2407 row = w->desired_matrix->rows + first_vpos;
2408 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2409 it->first_vpos = first_vpos;
2410
2411 /* Don't reseat to previous visible line start if current start
2412 position is in a string or image. */
2413 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2414 {
2415 int start_at_line_beg_p;
2416 int first_y = it->current_y;
2417
2418 /* If window start is not at a line start, skip forward to POS to
2419 get the correct continuation lines width. */
2420 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2421 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2422 if (!start_at_line_beg_p)
2423 {
2424 int new_x;
2425
2426 reseat_at_previous_visible_line_start (it);
2427 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2428
2429 new_x = it->current_x + it->pixel_width;
2430
2431 /* If lines are continued, this line may end in the middle
2432 of a multi-glyph character (e.g. a control character
2433 displayed as \003, or in the middle of an overlay
2434 string). In this case move_it_to above will not have
2435 taken us to the start of the continuation line but to the
2436 end of the continued line. */
2437 if (it->current_x > 0
2438 && !it->truncate_lines_p /* Lines are continued. */
2439 && (/* And glyph doesn't fit on the line. */
2440 new_x > it->last_visible_x
2441 /* Or it fits exactly and we're on a window
2442 system frame. */
2443 || (new_x == it->last_visible_x
2444 && FRAME_WINDOW_P (it->f))))
2445 {
2446 if (it->current.dpvec_index >= 0
2447 || it->current.overlay_string_index >= 0)
2448 {
2449 set_iterator_to_next (it, 1);
2450 move_it_in_display_line_to (it, -1, -1, 0);
2451 }
2452
2453 it->continuation_lines_width += it->current_x;
2454 }
2455
2456 /* We're starting a new display line, not affected by the
2457 height of the continued line, so clear the appropriate
2458 fields in the iterator structure. */
2459 it->max_ascent = it->max_descent = 0;
2460 it->max_phys_ascent = it->max_phys_descent = 0;
2461
2462 it->current_y = first_y;
2463 it->vpos = 0;
2464 it->current_x = it->hpos = 0;
2465 }
2466 }
2467
2468 #if 0 /* Don't assert the following because start_display is sometimes
2469 called intentionally with a window start that is not at a
2470 line start. Please leave this code in as a comment. */
2471
2472 /* Window start should be on a line start, now. */
2473 xassert (it->continuation_lines_width
2474 || IT_CHARPOS (it) == BEGV
2475 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2476 #endif /* 0 */
2477 }
2478
2479
2480 /* Return 1 if POS is a position in ellipses displayed for invisible
2481 text. W is the window we display, for text property lookup. */
2482
2483 static int
2484 in_ellipses_for_invisible_text_p (pos, w)
2485 struct display_pos *pos;
2486 struct window *w;
2487 {
2488 Lisp_Object prop, window;
2489 int ellipses_p = 0;
2490 int charpos = CHARPOS (pos->pos);
2491
2492 /* If POS specifies a position in a display vector, this might
2493 be for an ellipsis displayed for invisible text. We won't
2494 get the iterator set up for delivering that ellipsis unless
2495 we make sure that it gets aware of the invisible text. */
2496 if (pos->dpvec_index >= 0
2497 && pos->overlay_string_index < 0
2498 && CHARPOS (pos->string_pos) < 0
2499 && charpos > BEGV
2500 && (XSETWINDOW (window, w),
2501 prop = Fget_char_property (make_number (charpos),
2502 Qinvisible, window),
2503 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2504 {
2505 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2506 window);
2507 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2508 }
2509
2510 return ellipses_p;
2511 }
2512
2513
2514 /* Initialize IT for stepping through current_buffer in window W,
2515 starting at position POS that includes overlay string and display
2516 vector/ control character translation position information. Value
2517 is zero if there are overlay strings with newlines at POS. */
2518
2519 static int
2520 init_from_display_pos (it, w, pos)
2521 struct it *it;
2522 struct window *w;
2523 struct display_pos *pos;
2524 {
2525 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2526 int i, overlay_strings_with_newlines = 0;
2527
2528 /* If POS specifies a position in a display vector, this might
2529 be for an ellipsis displayed for invisible text. We won't
2530 get the iterator set up for delivering that ellipsis unless
2531 we make sure that it gets aware of the invisible text. */
2532 if (in_ellipses_for_invisible_text_p (pos, w))
2533 {
2534 --charpos;
2535 bytepos = 0;
2536 }
2537
2538 /* Keep in mind: the call to reseat in init_iterator skips invisible
2539 text, so we might end up at a position different from POS. This
2540 is only a problem when POS is a row start after a newline and an
2541 overlay starts there with an after-string, and the overlay has an
2542 invisible property. Since we don't skip invisible text in
2543 display_line and elsewhere immediately after consuming the
2544 newline before the row start, such a POS will not be in a string,
2545 but the call to init_iterator below will move us to the
2546 after-string. */
2547 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2548
2549 /* This only scans the current chunk -- it should scan all chunks.
2550 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2551 to 16 in 22.1 to make this a lesser problem. */
2552 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2553 {
2554 const char *s = SDATA (it->overlay_strings[i]);
2555 const char *e = s + SBYTES (it->overlay_strings[i]);
2556
2557 while (s < e && *s != '\n')
2558 ++s;
2559
2560 if (s < e)
2561 {
2562 overlay_strings_with_newlines = 1;
2563 break;
2564 }
2565 }
2566
2567 /* If position is within an overlay string, set up IT to the right
2568 overlay string. */
2569 if (pos->overlay_string_index >= 0)
2570 {
2571 int relative_index;
2572
2573 /* If the first overlay string happens to have a `display'
2574 property for an image, the iterator will be set up for that
2575 image, and we have to undo that setup first before we can
2576 correct the overlay string index. */
2577 if (it->method == GET_FROM_IMAGE)
2578 pop_it (it);
2579
2580 /* We already have the first chunk of overlay strings in
2581 IT->overlay_strings. Load more until the one for
2582 pos->overlay_string_index is in IT->overlay_strings. */
2583 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2584 {
2585 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2586 it->current.overlay_string_index = 0;
2587 while (n--)
2588 {
2589 load_overlay_strings (it, 0);
2590 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2591 }
2592 }
2593
2594 it->current.overlay_string_index = pos->overlay_string_index;
2595 relative_index = (it->current.overlay_string_index
2596 % OVERLAY_STRING_CHUNK_SIZE);
2597 it->string = it->overlay_strings[relative_index];
2598 xassert (STRINGP (it->string));
2599 it->current.string_pos = pos->string_pos;
2600 it->method = GET_FROM_STRING;
2601 }
2602
2603 #if 0 /* This is bogus because POS not having an overlay string
2604 position does not mean it's after the string. Example: A
2605 line starting with a before-string and initialization of IT
2606 to the previous row's end position. */
2607 else if (it->current.overlay_string_index >= 0)
2608 {
2609 /* If POS says we're already after an overlay string ending at
2610 POS, make sure to pop the iterator because it will be in
2611 front of that overlay string. When POS is ZV, we've thereby
2612 also ``processed'' overlay strings at ZV. */
2613 while (it->sp)
2614 pop_it (it);
2615 it->current.overlay_string_index = -1;
2616 it->method = GET_FROM_BUFFER;
2617 if (CHARPOS (pos->pos) == ZV)
2618 it->overlay_strings_at_end_processed_p = 1;
2619 }
2620 #endif /* 0 */
2621
2622 if (CHARPOS (pos->string_pos) >= 0)
2623 {
2624 /* Recorded position is not in an overlay string, but in another
2625 string. This can only be a string from a `display' property.
2626 IT should already be filled with that string. */
2627 it->current.string_pos = pos->string_pos;
2628 xassert (STRINGP (it->string));
2629 }
2630
2631 /* Restore position in display vector translations, control
2632 character translations or ellipses. */
2633 if (pos->dpvec_index >= 0)
2634 {
2635 if (it->dpvec == NULL)
2636 get_next_display_element (it);
2637 xassert (it->dpvec && it->current.dpvec_index == 0);
2638 it->current.dpvec_index = pos->dpvec_index;
2639 }
2640
2641 CHECK_IT (it);
2642 return !overlay_strings_with_newlines;
2643 }
2644
2645
2646 /* Initialize IT for stepping through current_buffer in window W
2647 starting at ROW->start. */
2648
2649 static void
2650 init_to_row_start (it, w, row)
2651 struct it *it;
2652 struct window *w;
2653 struct glyph_row *row;
2654 {
2655 init_from_display_pos (it, w, &row->start);
2656 it->start = row->start;
2657 it->continuation_lines_width = row->continuation_lines_width;
2658 CHECK_IT (it);
2659 }
2660
2661
2662 /* Initialize IT for stepping through current_buffer in window W
2663 starting in the line following ROW, i.e. starting at ROW->end.
2664 Value is zero if there are overlay strings with newlines at ROW's
2665 end position. */
2666
2667 static int
2668 init_to_row_end (it, w, row)
2669 struct it *it;
2670 struct window *w;
2671 struct glyph_row *row;
2672 {
2673 int success = 0;
2674
2675 if (init_from_display_pos (it, w, &row->end))
2676 {
2677 if (row->continued_p)
2678 it->continuation_lines_width
2679 = row->continuation_lines_width + row->pixel_width;
2680 CHECK_IT (it);
2681 success = 1;
2682 }
2683
2684 return success;
2685 }
2686
2687
2688
2689 \f
2690 /***********************************************************************
2691 Text properties
2692 ***********************************************************************/
2693
2694 /* Called when IT reaches IT->stop_charpos. Handle text property and
2695 overlay changes. Set IT->stop_charpos to the next position where
2696 to stop. */
2697
2698 static void
2699 handle_stop (it)
2700 struct it *it;
2701 {
2702 enum prop_handled handled;
2703 int handle_overlay_change_p = 1;
2704 struct props *p;
2705
2706 it->dpvec = NULL;
2707 it->current.dpvec_index = -1;
2708
2709 /* Use face of preceding text for ellipsis (if invisible) */
2710 if (it->selective_display_ellipsis_p)
2711 it->saved_face_id = it->face_id;
2712
2713 do
2714 {
2715 handled = HANDLED_NORMALLY;
2716
2717 /* Call text property handlers. */
2718 for (p = it_props; p->handler; ++p)
2719 {
2720 handled = p->handler (it);
2721
2722 if (handled == HANDLED_RECOMPUTE_PROPS)
2723 break;
2724 else if (handled == HANDLED_RETURN)
2725 return;
2726 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2727 handle_overlay_change_p = 0;
2728 }
2729
2730 if (handled != HANDLED_RECOMPUTE_PROPS)
2731 {
2732 /* Don't check for overlay strings below when set to deliver
2733 characters from a display vector. */
2734 if (it->method == GET_FROM_DISPLAY_VECTOR)
2735 handle_overlay_change_p = 0;
2736
2737 /* Handle overlay changes. */
2738 if (handle_overlay_change_p)
2739 handled = handle_overlay_change (it);
2740
2741 /* Determine where to stop next. */
2742 if (handled == HANDLED_NORMALLY)
2743 compute_stop_pos (it);
2744 }
2745 }
2746 while (handled == HANDLED_RECOMPUTE_PROPS);
2747 }
2748
2749
2750 /* Compute IT->stop_charpos from text property and overlay change
2751 information for IT's current position. */
2752
2753 static void
2754 compute_stop_pos (it)
2755 struct it *it;
2756 {
2757 register INTERVAL iv, next_iv;
2758 Lisp_Object object, limit, position;
2759
2760 /* If nowhere else, stop at the end. */
2761 it->stop_charpos = it->end_charpos;
2762
2763 if (STRINGP (it->string))
2764 {
2765 /* Strings are usually short, so don't limit the search for
2766 properties. */
2767 object = it->string;
2768 limit = Qnil;
2769 position = make_number (IT_STRING_CHARPOS (*it));
2770 }
2771 else
2772 {
2773 int charpos;
2774
2775 /* If next overlay change is in front of the current stop pos
2776 (which is IT->end_charpos), stop there. Note: value of
2777 next_overlay_change is point-max if no overlay change
2778 follows. */
2779 charpos = next_overlay_change (IT_CHARPOS (*it));
2780 if (charpos < it->stop_charpos)
2781 it->stop_charpos = charpos;
2782
2783 /* If showing the region, we have to stop at the region
2784 start or end because the face might change there. */
2785 if (it->region_beg_charpos > 0)
2786 {
2787 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2788 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2789 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2790 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2791 }
2792
2793 /* Set up variables for computing the stop position from text
2794 property changes. */
2795 XSETBUFFER (object, current_buffer);
2796 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2797 position = make_number (IT_CHARPOS (*it));
2798
2799 }
2800
2801 /* Get the interval containing IT's position. Value is a null
2802 interval if there isn't such an interval. */
2803 iv = validate_interval_range (object, &position, &position, 0);
2804 if (!NULL_INTERVAL_P (iv))
2805 {
2806 Lisp_Object values_here[LAST_PROP_IDX];
2807 struct props *p;
2808
2809 /* Get properties here. */
2810 for (p = it_props; p->handler; ++p)
2811 values_here[p->idx] = textget (iv->plist, *p->name);
2812
2813 /* Look for an interval following iv that has different
2814 properties. */
2815 for (next_iv = next_interval (iv);
2816 (!NULL_INTERVAL_P (next_iv)
2817 && (NILP (limit)
2818 || XFASTINT (limit) > next_iv->position));
2819 next_iv = next_interval (next_iv))
2820 {
2821 for (p = it_props; p->handler; ++p)
2822 {
2823 Lisp_Object new_value;
2824
2825 new_value = textget (next_iv->plist, *p->name);
2826 if (!EQ (values_here[p->idx], new_value))
2827 break;
2828 }
2829
2830 if (p->handler)
2831 break;
2832 }
2833
2834 if (!NULL_INTERVAL_P (next_iv))
2835 {
2836 if (INTEGERP (limit)
2837 && next_iv->position >= XFASTINT (limit))
2838 /* No text property change up to limit. */
2839 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2840 else
2841 /* Text properties change in next_iv. */
2842 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2843 }
2844 }
2845
2846 xassert (STRINGP (it->string)
2847 || (it->stop_charpos >= BEGV
2848 && it->stop_charpos >= IT_CHARPOS (*it)));
2849 }
2850
2851
2852 /* Return the position of the next overlay change after POS in
2853 current_buffer. Value is point-max if no overlay change
2854 follows. This is like `next-overlay-change' but doesn't use
2855 xmalloc. */
2856
2857 static int
2858 next_overlay_change (pos)
2859 int pos;
2860 {
2861 int noverlays;
2862 int endpos;
2863 Lisp_Object *overlays;
2864 int i;
2865
2866 /* Get all overlays at the given position. */
2867 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2868
2869 /* If any of these overlays ends before endpos,
2870 use its ending point instead. */
2871 for (i = 0; i < noverlays; ++i)
2872 {
2873 Lisp_Object oend;
2874 int oendpos;
2875
2876 oend = OVERLAY_END (overlays[i]);
2877 oendpos = OVERLAY_POSITION (oend);
2878 endpos = min (endpos, oendpos);
2879 }
2880
2881 return endpos;
2882 }
2883
2884
2885 \f
2886 /***********************************************************************
2887 Fontification
2888 ***********************************************************************/
2889
2890 /* Handle changes in the `fontified' property of the current buffer by
2891 calling hook functions from Qfontification_functions to fontify
2892 regions of text. */
2893
2894 static enum prop_handled
2895 handle_fontified_prop (it)
2896 struct it *it;
2897 {
2898 Lisp_Object prop, pos;
2899 enum prop_handled handled = HANDLED_NORMALLY;
2900
2901 /* Get the value of the `fontified' property at IT's current buffer
2902 position. (The `fontified' property doesn't have a special
2903 meaning in strings.) If the value is nil, call functions from
2904 Qfontification_functions. */
2905 if (!STRINGP (it->string)
2906 && it->s == NULL
2907 && !NILP (Vfontification_functions)
2908 && !NILP (Vrun_hooks)
2909 && (pos = make_number (IT_CHARPOS (*it)),
2910 prop = Fget_char_property (pos, Qfontified, Qnil),
2911 NILP (prop)))
2912 {
2913 int count = SPECPDL_INDEX ();
2914 Lisp_Object val;
2915
2916 val = Vfontification_functions;
2917 specbind (Qfontification_functions, Qnil);
2918
2919 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2920 safe_call1 (val, pos);
2921 else
2922 {
2923 Lisp_Object globals, fn;
2924 struct gcpro gcpro1, gcpro2;
2925
2926 globals = Qnil;
2927 GCPRO2 (val, globals);
2928
2929 for (; CONSP (val); val = XCDR (val))
2930 {
2931 fn = XCAR (val);
2932
2933 if (EQ (fn, Qt))
2934 {
2935 /* A value of t indicates this hook has a local
2936 binding; it means to run the global binding too.
2937 In a global value, t should not occur. If it
2938 does, we must ignore it to avoid an endless
2939 loop. */
2940 for (globals = Fdefault_value (Qfontification_functions);
2941 CONSP (globals);
2942 globals = XCDR (globals))
2943 {
2944 fn = XCAR (globals);
2945 if (!EQ (fn, Qt))
2946 safe_call1 (fn, pos);
2947 }
2948 }
2949 else
2950 safe_call1 (fn, pos);
2951 }
2952
2953 UNGCPRO;
2954 }
2955
2956 unbind_to (count, Qnil);
2957
2958 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2959 something. This avoids an endless loop if they failed to
2960 fontify the text for which reason ever. */
2961 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2962 handled = HANDLED_RECOMPUTE_PROPS;
2963 }
2964
2965 return handled;
2966 }
2967
2968
2969 \f
2970 /***********************************************************************
2971 Faces
2972 ***********************************************************************/
2973
2974 /* Set up iterator IT from face properties at its current position.
2975 Called from handle_stop. */
2976
2977 static enum prop_handled
2978 handle_face_prop (it)
2979 struct it *it;
2980 {
2981 int new_face_id, next_stop;
2982
2983 if (!STRINGP (it->string))
2984 {
2985 new_face_id
2986 = face_at_buffer_position (it->w,
2987 IT_CHARPOS (*it),
2988 it->region_beg_charpos,
2989 it->region_end_charpos,
2990 &next_stop,
2991 (IT_CHARPOS (*it)
2992 + TEXT_PROP_DISTANCE_LIMIT),
2993 0);
2994
2995 /* Is this a start of a run of characters with box face?
2996 Caveat: this can be called for a freshly initialized
2997 iterator; face_id is -1 in this case. We know that the new
2998 face will not change until limit, i.e. if the new face has a
2999 box, all characters up to limit will have one. But, as
3000 usual, we don't know whether limit is really the end. */
3001 if (new_face_id != it->face_id)
3002 {
3003 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3004
3005 /* If new face has a box but old face has not, this is
3006 the start of a run of characters with box, i.e. it has
3007 a shadow on the left side. The value of face_id of the
3008 iterator will be -1 if this is the initial call that gets
3009 the face. In this case, we have to look in front of IT's
3010 position and see whether there is a face != new_face_id. */
3011 it->start_of_box_run_p
3012 = (new_face->box != FACE_NO_BOX
3013 && (it->face_id >= 0
3014 || IT_CHARPOS (*it) == BEG
3015 || new_face_id != face_before_it_pos (it)));
3016 it->face_box_p = new_face->box != FACE_NO_BOX;
3017 }
3018 }
3019 else
3020 {
3021 int base_face_id, bufpos;
3022
3023 if (it->current.overlay_string_index >= 0)
3024 bufpos = IT_CHARPOS (*it);
3025 else
3026 bufpos = 0;
3027
3028 /* For strings from a buffer, i.e. overlay strings or strings
3029 from a `display' property, use the face at IT's current
3030 buffer position as the base face to merge with, so that
3031 overlay strings appear in the same face as surrounding
3032 text, unless they specify their own faces. */
3033 base_face_id = underlying_face_id (it);
3034
3035 new_face_id = face_at_string_position (it->w,
3036 it->string,
3037 IT_STRING_CHARPOS (*it),
3038 bufpos,
3039 it->region_beg_charpos,
3040 it->region_end_charpos,
3041 &next_stop,
3042 base_face_id, 0);
3043
3044 #if 0 /* This shouldn't be neccessary. Let's check it. */
3045 /* If IT is used to display a mode line we would really like to
3046 use the mode line face instead of the frame's default face. */
3047 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3048 && new_face_id == DEFAULT_FACE_ID)
3049 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3050 #endif
3051
3052 /* Is this a start of a run of characters with box? Caveat:
3053 this can be called for a freshly allocated iterator; face_id
3054 is -1 is this case. We know that the new face will not
3055 change until the next check pos, i.e. if the new face has a
3056 box, all characters up to that position will have a
3057 box. But, as usual, we don't know whether that position
3058 is really the end. */
3059 if (new_face_id != it->face_id)
3060 {
3061 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3062 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3063
3064 /* If new face has a box but old face hasn't, this is the
3065 start of a run of characters with box, i.e. it has a
3066 shadow on the left side. */
3067 it->start_of_box_run_p
3068 = new_face->box && (old_face == NULL || !old_face->box);
3069 it->face_box_p = new_face->box != FACE_NO_BOX;
3070 }
3071 }
3072
3073 it->face_id = new_face_id;
3074 return HANDLED_NORMALLY;
3075 }
3076
3077
3078 /* Return the ID of the face ``underlying'' IT's current position,
3079 which is in a string. If the iterator is associated with a
3080 buffer, return the face at IT's current buffer position.
3081 Otherwise, use the iterator's base_face_id. */
3082
3083 static int
3084 underlying_face_id (it)
3085 struct it *it;
3086 {
3087 int face_id = it->base_face_id, i;
3088
3089 xassert (STRINGP (it->string));
3090
3091 for (i = it->sp - 1; i >= 0; --i)
3092 if (NILP (it->stack[i].string))
3093 face_id = it->stack[i].face_id;
3094
3095 return face_id;
3096 }
3097
3098
3099 /* Compute the face one character before or after the current position
3100 of IT. BEFORE_P non-zero means get the face in front of IT's
3101 position. Value is the id of the face. */
3102
3103 static int
3104 face_before_or_after_it_pos (it, before_p)
3105 struct it *it;
3106 int before_p;
3107 {
3108 int face_id, limit;
3109 int next_check_charpos;
3110 struct text_pos pos;
3111
3112 xassert (it->s == NULL);
3113
3114 if (STRINGP (it->string))
3115 {
3116 int bufpos, base_face_id;
3117
3118 /* No face change past the end of the string (for the case
3119 we are padding with spaces). No face change before the
3120 string start. */
3121 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3122 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3123 return it->face_id;
3124
3125 /* Set pos to the position before or after IT's current position. */
3126 if (before_p)
3127 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3128 else
3129 /* For composition, we must check the character after the
3130 composition. */
3131 pos = (it->what == IT_COMPOSITION
3132 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3133 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3134
3135 if (it->current.overlay_string_index >= 0)
3136 bufpos = IT_CHARPOS (*it);
3137 else
3138 bufpos = 0;
3139
3140 base_face_id = underlying_face_id (it);
3141
3142 /* Get the face for ASCII, or unibyte. */
3143 face_id = face_at_string_position (it->w,
3144 it->string,
3145 CHARPOS (pos),
3146 bufpos,
3147 it->region_beg_charpos,
3148 it->region_end_charpos,
3149 &next_check_charpos,
3150 base_face_id, 0);
3151
3152 /* Correct the face for charsets different from ASCII. Do it
3153 for the multibyte case only. The face returned above is
3154 suitable for unibyte text if IT->string is unibyte. */
3155 if (STRING_MULTIBYTE (it->string))
3156 {
3157 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3158 int rest = SBYTES (it->string) - BYTEPOS (pos);
3159 int c, len;
3160 struct face *face = FACE_FROM_ID (it->f, face_id);
3161
3162 c = string_char_and_length (p, rest, &len);
3163 face_id = FACE_FOR_CHAR (it->f, face, c);
3164 }
3165 }
3166 else
3167 {
3168 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3169 || (IT_CHARPOS (*it) <= BEGV && before_p))
3170 return it->face_id;
3171
3172 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3173 pos = it->current.pos;
3174
3175 if (before_p)
3176 DEC_TEXT_POS (pos, it->multibyte_p);
3177 else
3178 {
3179 if (it->what == IT_COMPOSITION)
3180 /* For composition, we must check the position after the
3181 composition. */
3182 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3183 else
3184 INC_TEXT_POS (pos, it->multibyte_p);
3185 }
3186
3187 /* Determine face for CHARSET_ASCII, or unibyte. */
3188 face_id = face_at_buffer_position (it->w,
3189 CHARPOS (pos),
3190 it->region_beg_charpos,
3191 it->region_end_charpos,
3192 &next_check_charpos,
3193 limit, 0);
3194
3195 /* Correct the face for charsets different from ASCII. Do it
3196 for the multibyte case only. The face returned above is
3197 suitable for unibyte text if current_buffer is unibyte. */
3198 if (it->multibyte_p)
3199 {
3200 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3201 struct face *face = FACE_FROM_ID (it->f, face_id);
3202 face_id = FACE_FOR_CHAR (it->f, face, c);
3203 }
3204 }
3205
3206 return face_id;
3207 }
3208
3209
3210 \f
3211 /***********************************************************************
3212 Invisible text
3213 ***********************************************************************/
3214
3215 /* Set up iterator IT from invisible properties at its current
3216 position. Called from handle_stop. */
3217
3218 static enum prop_handled
3219 handle_invisible_prop (it)
3220 struct it *it;
3221 {
3222 enum prop_handled handled = HANDLED_NORMALLY;
3223
3224 if (STRINGP (it->string))
3225 {
3226 extern Lisp_Object Qinvisible;
3227 Lisp_Object prop, end_charpos, limit, charpos;
3228
3229 /* Get the value of the invisible text property at the
3230 current position. Value will be nil if there is no such
3231 property. */
3232 charpos = make_number (IT_STRING_CHARPOS (*it));
3233 prop = Fget_text_property (charpos, Qinvisible, it->string);
3234
3235 if (!NILP (prop)
3236 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3237 {
3238 handled = HANDLED_RECOMPUTE_PROPS;
3239
3240 /* Get the position at which the next change of the
3241 invisible text property can be found in IT->string.
3242 Value will be nil if the property value is the same for
3243 all the rest of IT->string. */
3244 XSETINT (limit, SCHARS (it->string));
3245 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3246 it->string, limit);
3247
3248 /* Text at current position is invisible. The next
3249 change in the property is at position end_charpos.
3250 Move IT's current position to that position. */
3251 if (INTEGERP (end_charpos)
3252 && XFASTINT (end_charpos) < XFASTINT (limit))
3253 {
3254 struct text_pos old;
3255 old = it->current.string_pos;
3256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3257 compute_string_pos (&it->current.string_pos, old, it->string);
3258 }
3259 else
3260 {
3261 /* The rest of the string is invisible. If this is an
3262 overlay string, proceed with the next overlay string
3263 or whatever comes and return a character from there. */
3264 if (it->current.overlay_string_index >= 0)
3265 {
3266 next_overlay_string (it);
3267 /* Don't check for overlay strings when we just
3268 finished processing them. */
3269 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3270 }
3271 else
3272 {
3273 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3274 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3275 }
3276 }
3277 }
3278 }
3279 else
3280 {
3281 int invis_p, newpos, next_stop, start_charpos;
3282 Lisp_Object pos, prop, overlay;
3283
3284 /* First of all, is there invisible text at this position? */
3285 start_charpos = IT_CHARPOS (*it);
3286 pos = make_number (IT_CHARPOS (*it));
3287 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3288 &overlay);
3289 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3290
3291 /* If we are on invisible text, skip over it. */
3292 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3293 {
3294 /* Record whether we have to display an ellipsis for the
3295 invisible text. */
3296 int display_ellipsis_p = invis_p == 2;
3297
3298 handled = HANDLED_RECOMPUTE_PROPS;
3299
3300 /* Loop skipping over invisible text. The loop is left at
3301 ZV or with IT on the first char being visible again. */
3302 do
3303 {
3304 /* Try to skip some invisible text. Return value is the
3305 position reached which can be equal to IT's position
3306 if there is nothing invisible here. This skips both
3307 over invisible text properties and overlays with
3308 invisible property. */
3309 newpos = skip_invisible (IT_CHARPOS (*it),
3310 &next_stop, ZV, it->window);
3311
3312 /* If we skipped nothing at all we weren't at invisible
3313 text in the first place. If everything to the end of
3314 the buffer was skipped, end the loop. */
3315 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3316 invis_p = 0;
3317 else
3318 {
3319 /* We skipped some characters but not necessarily
3320 all there are. Check if we ended up on visible
3321 text. Fget_char_property returns the property of
3322 the char before the given position, i.e. if we
3323 get invis_p = 0, this means that the char at
3324 newpos is visible. */
3325 pos = make_number (newpos);
3326 prop = Fget_char_property (pos, Qinvisible, it->window);
3327 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3328 }
3329
3330 /* If we ended up on invisible text, proceed to
3331 skip starting with next_stop. */
3332 if (invis_p)
3333 IT_CHARPOS (*it) = next_stop;
3334 }
3335 while (invis_p);
3336
3337 /* The position newpos is now either ZV or on visible text. */
3338 IT_CHARPOS (*it) = newpos;
3339 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3340
3341 /* If there are before-strings at the start of invisible
3342 text, and the text is invisible because of a text
3343 property, arrange to show before-strings because 20.x did
3344 it that way. (If the text is invisible because of an
3345 overlay property instead of a text property, this is
3346 already handled in the overlay code.) */
3347 if (NILP (overlay)
3348 && get_overlay_strings (it, start_charpos))
3349 {
3350 handled = HANDLED_RECOMPUTE_PROPS;
3351 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3352 }
3353 else if (display_ellipsis_p)
3354 setup_for_ellipsis (it, 0);
3355 }
3356 }
3357
3358 return handled;
3359 }
3360
3361
3362 /* Make iterator IT return `...' next.
3363 Replaces LEN characters from buffer. */
3364
3365 static void
3366 setup_for_ellipsis (it, len)
3367 struct it *it;
3368 int len;
3369 {
3370 /* Use the display table definition for `...'. Invalid glyphs
3371 will be handled by the method returning elements from dpvec. */
3372 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3373 {
3374 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3375 it->dpvec = v->contents;
3376 it->dpend = v->contents + v->size;
3377 }
3378 else
3379 {
3380 /* Default `...'. */
3381 it->dpvec = default_invis_vector;
3382 it->dpend = default_invis_vector + 3;
3383 }
3384
3385 it->dpvec_char_len = len;
3386 it->current.dpvec_index = 0;
3387 it->dpvec_face_id = -1;
3388
3389 /* Remember the current face id in case glyphs specify faces.
3390 IT's face is restored in set_iterator_to_next.
3391 saved_face_id was set to preceding char's face in handle_stop. */
3392 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3393 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3394
3395 it->method = GET_FROM_DISPLAY_VECTOR;
3396 it->ellipsis_p = 1;
3397 }
3398
3399
3400 \f
3401 /***********************************************************************
3402 'display' property
3403 ***********************************************************************/
3404
3405 /* Set up iterator IT from `display' property at its current position.
3406 Called from handle_stop.
3407 We return HANDLED_RETURN if some part of the display property
3408 overrides the display of the buffer text itself.
3409 Otherwise we return HANDLED_NORMALLY. */
3410
3411 static enum prop_handled
3412 handle_display_prop (it)
3413 struct it *it;
3414 {
3415 Lisp_Object prop, object;
3416 struct text_pos *position;
3417 /* Nonzero if some property replaces the display of the text itself. */
3418 int display_replaced_p = 0;
3419
3420 if (STRINGP (it->string))
3421 {
3422 object = it->string;
3423 position = &it->current.string_pos;
3424 }
3425 else
3426 {
3427 object = it->w->buffer;
3428 position = &it->current.pos;
3429 }
3430
3431 /* Reset those iterator values set from display property values. */
3432 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3433 it->space_width = Qnil;
3434 it->font_height = Qnil;
3435 it->voffset = 0;
3436
3437 /* We don't support recursive `display' properties, i.e. string
3438 values that have a string `display' property, that have a string
3439 `display' property etc. */
3440 if (!it->string_from_display_prop_p)
3441 it->area = TEXT_AREA;
3442
3443 prop = Fget_char_property (make_number (position->charpos),
3444 Qdisplay, object);
3445 if (NILP (prop))
3446 return HANDLED_NORMALLY;
3447
3448 if (CONSP (prop)
3449 /* Simple properties. */
3450 && !EQ (XCAR (prop), Qimage)
3451 && !EQ (XCAR (prop), Qspace)
3452 && !EQ (XCAR (prop), Qwhen)
3453 && !EQ (XCAR (prop), Qslice)
3454 && !EQ (XCAR (prop), Qspace_width)
3455 && !EQ (XCAR (prop), Qheight)
3456 && !EQ (XCAR (prop), Qraise)
3457 /* Marginal area specifications. */
3458 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3459 && !EQ (XCAR (prop), Qleft_fringe)
3460 && !EQ (XCAR (prop), Qright_fringe)
3461 && !NILP (XCAR (prop)))
3462 {
3463 for (; CONSP (prop); prop = XCDR (prop))
3464 {
3465 if (handle_single_display_spec (it, XCAR (prop), object,
3466 position, display_replaced_p))
3467 display_replaced_p = 1;
3468 }
3469 }
3470 else if (VECTORP (prop))
3471 {
3472 int i;
3473 for (i = 0; i < ASIZE (prop); ++i)
3474 if (handle_single_display_spec (it, AREF (prop, i), object,
3475 position, display_replaced_p))
3476 display_replaced_p = 1;
3477 }
3478 else
3479 {
3480 int ret = handle_single_display_spec (it, prop, object, position, 0);
3481 if (ret < 0) /* Replaced by "", i.e. nothing. */
3482 return HANDLED_RECOMPUTE_PROPS;
3483 if (ret)
3484 display_replaced_p = 1;
3485 }
3486
3487 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3488 }
3489
3490
3491 /* Value is the position of the end of the `display' property starting
3492 at START_POS in OBJECT. */
3493
3494 static struct text_pos
3495 display_prop_end (it, object, start_pos)
3496 struct it *it;
3497 Lisp_Object object;
3498 struct text_pos start_pos;
3499 {
3500 Lisp_Object end;
3501 struct text_pos end_pos;
3502
3503 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3504 Qdisplay, object, Qnil);
3505 CHARPOS (end_pos) = XFASTINT (end);
3506 if (STRINGP (object))
3507 compute_string_pos (&end_pos, start_pos, it->string);
3508 else
3509 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3510
3511 return end_pos;
3512 }
3513
3514
3515 /* Set up IT from a single `display' specification PROP. OBJECT
3516 is the object in which the `display' property was found. *POSITION
3517 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3518 means that we previously saw a display specification which already
3519 replaced text display with something else, for example an image;
3520 we ignore such properties after the first one has been processed.
3521
3522 If PROP is a `space' or `image' specification, and in some other
3523 cases too, set *POSITION to the position where the `display'
3524 property ends.
3525
3526 Value is non-zero if something was found which replaces the display
3527 of buffer or string text. Specifically, the value is -1 if that
3528 "something" is "nothing". */
3529
3530 static int
3531 handle_single_display_spec (it, spec, object, position,
3532 display_replaced_before_p)
3533 struct it *it;
3534 Lisp_Object spec;
3535 Lisp_Object object;
3536 struct text_pos *position;
3537 int display_replaced_before_p;
3538 {
3539 Lisp_Object form;
3540 Lisp_Object location, value;
3541 struct text_pos start_pos;
3542 int valid_p;
3543
3544 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3545 If the result is non-nil, use VALUE instead of SPEC. */
3546 form = Qt;
3547 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3548 {
3549 spec = XCDR (spec);
3550 if (!CONSP (spec))
3551 return 0;
3552 form = XCAR (spec);
3553 spec = XCDR (spec);
3554 }
3555
3556 if (!NILP (form) && !EQ (form, Qt))
3557 {
3558 int count = SPECPDL_INDEX ();
3559 struct gcpro gcpro1;
3560
3561 /* Bind `object' to the object having the `display' property, a
3562 buffer or string. Bind `position' to the position in the
3563 object where the property was found, and `buffer-position'
3564 to the current position in the buffer. */
3565 specbind (Qobject, object);
3566 specbind (Qposition, make_number (CHARPOS (*position)));
3567 specbind (Qbuffer_position,
3568 make_number (STRINGP (object)
3569 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3570 GCPRO1 (form);
3571 form = safe_eval (form);
3572 UNGCPRO;
3573 unbind_to (count, Qnil);
3574 }
3575
3576 if (NILP (form))
3577 return 0;
3578
3579 /* Handle `(height HEIGHT)' specifications. */
3580 if (CONSP (spec)
3581 && EQ (XCAR (spec), Qheight)
3582 && CONSP (XCDR (spec)))
3583 {
3584 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3585 return 0;
3586
3587 it->font_height = XCAR (XCDR (spec));
3588 if (!NILP (it->font_height))
3589 {
3590 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3591 int new_height = -1;
3592
3593 if (CONSP (it->font_height)
3594 && (EQ (XCAR (it->font_height), Qplus)
3595 || EQ (XCAR (it->font_height), Qminus))
3596 && CONSP (XCDR (it->font_height))
3597 && INTEGERP (XCAR (XCDR (it->font_height))))
3598 {
3599 /* `(+ N)' or `(- N)' where N is an integer. */
3600 int steps = XINT (XCAR (XCDR (it->font_height)));
3601 if (EQ (XCAR (it->font_height), Qplus))
3602 steps = - steps;
3603 it->face_id = smaller_face (it->f, it->face_id, steps);
3604 }
3605 else if (FUNCTIONP (it->font_height))
3606 {
3607 /* Call function with current height as argument.
3608 Value is the new height. */
3609 Lisp_Object height;
3610 height = safe_call1 (it->font_height,
3611 face->lface[LFACE_HEIGHT_INDEX]);
3612 if (NUMBERP (height))
3613 new_height = XFLOATINT (height);
3614 }
3615 else if (NUMBERP (it->font_height))
3616 {
3617 /* Value is a multiple of the canonical char height. */
3618 struct face *face;
3619
3620 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3621 new_height = (XFLOATINT (it->font_height)
3622 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3623 }
3624 else
3625 {
3626 /* Evaluate IT->font_height with `height' bound to the
3627 current specified height to get the new height. */
3628 int count = SPECPDL_INDEX ();
3629
3630 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3631 value = safe_eval (it->font_height);
3632 unbind_to (count, Qnil);
3633
3634 if (NUMBERP (value))
3635 new_height = XFLOATINT (value);
3636 }
3637
3638 if (new_height > 0)
3639 it->face_id = face_with_height (it->f, it->face_id, new_height);
3640 }
3641
3642 return 0;
3643 }
3644
3645 /* Handle `(space_width WIDTH)'. */
3646 if (CONSP (spec)
3647 && EQ (XCAR (spec), Qspace_width)
3648 && CONSP (XCDR (spec)))
3649 {
3650 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3651 return 0;
3652
3653 value = XCAR (XCDR (spec));
3654 if (NUMBERP (value) && XFLOATINT (value) > 0)
3655 it->space_width = value;
3656
3657 return 0;
3658 }
3659
3660 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3661 if (CONSP (spec)
3662 && EQ (XCAR (spec), Qslice))
3663 {
3664 Lisp_Object tem;
3665
3666 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3667 return 0;
3668
3669 if (tem = XCDR (spec), CONSP (tem))
3670 {
3671 it->slice.x = XCAR (tem);
3672 if (tem = XCDR (tem), CONSP (tem))
3673 {
3674 it->slice.y = XCAR (tem);
3675 if (tem = XCDR (tem), CONSP (tem))
3676 {
3677 it->slice.width = XCAR (tem);
3678 if (tem = XCDR (tem), CONSP (tem))
3679 it->slice.height = XCAR (tem);
3680 }
3681 }
3682 }
3683
3684 return 0;
3685 }
3686
3687 /* Handle `(raise FACTOR)'. */
3688 if (CONSP (spec)
3689 && EQ (XCAR (spec), Qraise)
3690 && CONSP (XCDR (spec)))
3691 {
3692 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3693 return 0;
3694
3695 #ifdef HAVE_WINDOW_SYSTEM
3696 value = XCAR (XCDR (spec));
3697 if (NUMBERP (value))
3698 {
3699 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3700 it->voffset = - (XFLOATINT (value)
3701 * (FONT_HEIGHT (face->font)));
3702 }
3703 #endif /* HAVE_WINDOW_SYSTEM */
3704
3705 return 0;
3706 }
3707
3708 /* Don't handle the other kinds of display specifications
3709 inside a string that we got from a `display' property. */
3710 if (it->string_from_display_prop_p)
3711 return 0;
3712
3713 /* Characters having this form of property are not displayed, so
3714 we have to find the end of the property. */
3715 start_pos = *position;
3716 *position = display_prop_end (it, object, start_pos);
3717 value = Qnil;
3718
3719 /* Stop the scan at that end position--we assume that all
3720 text properties change there. */
3721 it->stop_charpos = position->charpos;
3722
3723 /* Handle `(left-fringe BITMAP [FACE])'
3724 and `(right-fringe BITMAP [FACE])'. */
3725 if (CONSP (spec)
3726 && (EQ (XCAR (spec), Qleft_fringe)
3727 || EQ (XCAR (spec), Qright_fringe))
3728 && CONSP (XCDR (spec)))
3729 {
3730 int face_id = DEFAULT_FACE_ID;
3731 int fringe_bitmap;
3732
3733 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3734 /* If we return here, POSITION has been advanced
3735 across the text with this property. */
3736 return 0;
3737
3738 #ifdef HAVE_WINDOW_SYSTEM
3739 value = XCAR (XCDR (spec));
3740 if (!SYMBOLP (value)
3741 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3742 /* If we return here, POSITION has been advanced
3743 across the text with this property. */
3744 return 0;
3745
3746 if (CONSP (XCDR (XCDR (spec))))
3747 {
3748 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3749 int face_id2 = lookup_derived_face (it->f, face_name,
3750 'A', FRINGE_FACE_ID, 0);
3751 if (face_id2 >= 0)
3752 face_id = face_id2;
3753 }
3754
3755 /* Save current settings of IT so that we can restore them
3756 when we are finished with the glyph property value. */
3757
3758 push_it (it);
3759
3760 it->area = TEXT_AREA;
3761 it->what = IT_IMAGE;
3762 it->image_id = -1; /* no image */
3763 it->position = start_pos;
3764 it->object = NILP (object) ? it->w->buffer : object;
3765 it->method = GET_FROM_IMAGE;
3766 it->face_id = face_id;
3767
3768 /* Say that we haven't consumed the characters with
3769 `display' property yet. The call to pop_it in
3770 set_iterator_to_next will clean this up. */
3771 *position = start_pos;
3772
3773 if (EQ (XCAR (spec), Qleft_fringe))
3774 {
3775 it->left_user_fringe_bitmap = fringe_bitmap;
3776 it->left_user_fringe_face_id = face_id;
3777 }
3778 else
3779 {
3780 it->right_user_fringe_bitmap = fringe_bitmap;
3781 it->right_user_fringe_face_id = face_id;
3782 }
3783 #endif /* HAVE_WINDOW_SYSTEM */
3784 return 1;
3785 }
3786
3787 /* Prepare to handle `((margin left-margin) ...)',
3788 `((margin right-margin) ...)' and `((margin nil) ...)'
3789 prefixes for display specifications. */
3790 location = Qunbound;
3791 if (CONSP (spec) && CONSP (XCAR (spec)))
3792 {
3793 Lisp_Object tem;
3794
3795 value = XCDR (spec);
3796 if (CONSP (value))
3797 value = XCAR (value);
3798
3799 tem = XCAR (spec);
3800 if (EQ (XCAR (tem), Qmargin)
3801 && (tem = XCDR (tem),
3802 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3803 (NILP (tem)
3804 || EQ (tem, Qleft_margin)
3805 || EQ (tem, Qright_margin))))
3806 location = tem;
3807 }
3808
3809 if (EQ (location, Qunbound))
3810 {
3811 location = Qnil;
3812 value = spec;
3813 }
3814
3815 /* After this point, VALUE is the property after any
3816 margin prefix has been stripped. It must be a string,
3817 an image specification, or `(space ...)'.
3818
3819 LOCATION specifies where to display: `left-margin',
3820 `right-margin' or nil. */
3821
3822 valid_p = (STRINGP (value)
3823 #ifdef HAVE_WINDOW_SYSTEM
3824 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3825 #endif /* not HAVE_WINDOW_SYSTEM */
3826 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3827
3828 if (valid_p && !display_replaced_before_p)
3829 {
3830 /* Save current settings of IT so that we can restore them
3831 when we are finished with the glyph property value. */
3832 push_it (it);
3833
3834 if (NILP (location))
3835 it->area = TEXT_AREA;
3836 else if (EQ (location, Qleft_margin))
3837 it->area = LEFT_MARGIN_AREA;
3838 else
3839 it->area = RIGHT_MARGIN_AREA;
3840
3841 if (STRINGP (value))
3842 {
3843 if (SCHARS (value) == 0)
3844 {
3845 pop_it (it);
3846 return -1; /* Replaced by "", i.e. nothing. */
3847 }
3848 it->string = value;
3849 it->multibyte_p = STRING_MULTIBYTE (it->string);
3850 it->current.overlay_string_index = -1;
3851 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3852 it->end_charpos = it->string_nchars = SCHARS (it->string);
3853 it->method = GET_FROM_STRING;
3854 it->stop_charpos = 0;
3855 it->string_from_display_prop_p = 1;
3856 /* Say that we haven't consumed the characters with
3857 `display' property yet. The call to pop_it in
3858 set_iterator_to_next will clean this up. */
3859 *position = start_pos;
3860 }
3861 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3862 {
3863 it->method = GET_FROM_STRETCH;
3864 it->object = value;
3865 it->current.pos = it->position = start_pos;
3866 }
3867 #ifdef HAVE_WINDOW_SYSTEM
3868 else
3869 {
3870 it->what = IT_IMAGE;
3871 it->image_id = lookup_image (it->f, value);
3872 it->position = start_pos;
3873 it->object = NILP (object) ? it->w->buffer : object;
3874 it->method = GET_FROM_IMAGE;
3875
3876 /* Say that we haven't consumed the characters with
3877 `display' property yet. The call to pop_it in
3878 set_iterator_to_next will clean this up. */
3879 *position = start_pos;
3880 }
3881 #endif /* HAVE_WINDOW_SYSTEM */
3882
3883 return 1;
3884 }
3885
3886 /* Invalid property or property not supported. Restore
3887 POSITION to what it was before. */
3888 *position = start_pos;
3889 return 0;
3890 }
3891
3892
3893 /* Check if SPEC is a display specification value whose text should be
3894 treated as intangible. */
3895
3896 static int
3897 single_display_spec_intangible_p (prop)
3898 Lisp_Object prop;
3899 {
3900 /* Skip over `when FORM'. */
3901 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3902 {
3903 prop = XCDR (prop);
3904 if (!CONSP (prop))
3905 return 0;
3906 prop = XCDR (prop);
3907 }
3908
3909 if (STRINGP (prop))
3910 return 1;
3911
3912 if (!CONSP (prop))
3913 return 0;
3914
3915 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3916 we don't need to treat text as intangible. */
3917 if (EQ (XCAR (prop), Qmargin))
3918 {
3919 prop = XCDR (prop);
3920 if (!CONSP (prop))
3921 return 0;
3922
3923 prop = XCDR (prop);
3924 if (!CONSP (prop)
3925 || EQ (XCAR (prop), Qleft_margin)
3926 || EQ (XCAR (prop), Qright_margin))
3927 return 0;
3928 }
3929
3930 return (CONSP (prop)
3931 && (EQ (XCAR (prop), Qimage)
3932 || EQ (XCAR (prop), Qspace)));
3933 }
3934
3935
3936 /* Check if PROP is a display property value whose text should be
3937 treated as intangible. */
3938
3939 int
3940 display_prop_intangible_p (prop)
3941 Lisp_Object prop;
3942 {
3943 if (CONSP (prop)
3944 && CONSP (XCAR (prop))
3945 && !EQ (Qmargin, XCAR (XCAR (prop))))
3946 {
3947 /* A list of sub-properties. */
3948 while (CONSP (prop))
3949 {
3950 if (single_display_spec_intangible_p (XCAR (prop)))
3951 return 1;
3952 prop = XCDR (prop);
3953 }
3954 }
3955 else if (VECTORP (prop))
3956 {
3957 /* A vector of sub-properties. */
3958 int i;
3959 for (i = 0; i < ASIZE (prop); ++i)
3960 if (single_display_spec_intangible_p (AREF (prop, i)))
3961 return 1;
3962 }
3963 else
3964 return single_display_spec_intangible_p (prop);
3965
3966 return 0;
3967 }
3968
3969
3970 /* Return 1 if PROP is a display sub-property value containing STRING. */
3971
3972 static int
3973 single_display_spec_string_p (prop, string)
3974 Lisp_Object prop, string;
3975 {
3976 if (EQ (string, prop))
3977 return 1;
3978
3979 /* Skip over `when FORM'. */
3980 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3981 {
3982 prop = XCDR (prop);
3983 if (!CONSP (prop))
3984 return 0;
3985 prop = XCDR (prop);
3986 }
3987
3988 if (CONSP (prop))
3989 /* Skip over `margin LOCATION'. */
3990 if (EQ (XCAR (prop), Qmargin))
3991 {
3992 prop = XCDR (prop);
3993 if (!CONSP (prop))
3994 return 0;
3995
3996 prop = XCDR (prop);
3997 if (!CONSP (prop))
3998 return 0;
3999 }
4000
4001 return CONSP (prop) && EQ (XCAR (prop), string);
4002 }
4003
4004
4005 /* Return 1 if STRING appears in the `display' property PROP. */
4006
4007 static int
4008 display_prop_string_p (prop, string)
4009 Lisp_Object prop, string;
4010 {
4011 if (CONSP (prop)
4012 && CONSP (XCAR (prop))
4013 && !EQ (Qmargin, XCAR (XCAR (prop))))
4014 {
4015 /* A list of sub-properties. */
4016 while (CONSP (prop))
4017 {
4018 if (single_display_spec_string_p (XCAR (prop), string))
4019 return 1;
4020 prop = XCDR (prop);
4021 }
4022 }
4023 else if (VECTORP (prop))
4024 {
4025 /* A vector of sub-properties. */
4026 int i;
4027 for (i = 0; i < ASIZE (prop); ++i)
4028 if (single_display_spec_string_p (AREF (prop, i), string))
4029 return 1;
4030 }
4031 else
4032 return single_display_spec_string_p (prop, string);
4033
4034 return 0;
4035 }
4036
4037
4038 /* Determine from which buffer position in W's buffer STRING comes
4039 from. AROUND_CHARPOS is an approximate position where it could
4040 be from. Value is the buffer position or 0 if it couldn't be
4041 determined.
4042
4043 W's buffer must be current.
4044
4045 This function is necessary because we don't record buffer positions
4046 in glyphs generated from strings (to keep struct glyph small).
4047 This function may only use code that doesn't eval because it is
4048 called asynchronously from note_mouse_highlight. */
4049
4050 int
4051 string_buffer_position (w, string, around_charpos)
4052 struct window *w;
4053 Lisp_Object string;
4054 int around_charpos;
4055 {
4056 Lisp_Object limit, prop, pos;
4057 const int MAX_DISTANCE = 1000;
4058 int found = 0;
4059
4060 pos = make_number (around_charpos);
4061 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4062 while (!found && !EQ (pos, limit))
4063 {
4064 prop = Fget_char_property (pos, Qdisplay, Qnil);
4065 if (!NILP (prop) && display_prop_string_p (prop, string))
4066 found = 1;
4067 else
4068 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4069 }
4070
4071 if (!found)
4072 {
4073 pos = make_number (around_charpos);
4074 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4075 while (!found && !EQ (pos, limit))
4076 {
4077 prop = Fget_char_property (pos, Qdisplay, Qnil);
4078 if (!NILP (prop) && display_prop_string_p (prop, string))
4079 found = 1;
4080 else
4081 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4082 limit);
4083 }
4084 }
4085
4086 return found ? XINT (pos) : 0;
4087 }
4088
4089
4090 \f
4091 /***********************************************************************
4092 `composition' property
4093 ***********************************************************************/
4094
4095 /* Set up iterator IT from `composition' property at its current
4096 position. Called from handle_stop. */
4097
4098 static enum prop_handled
4099 handle_composition_prop (it)
4100 struct it *it;
4101 {
4102 Lisp_Object prop, string;
4103 int pos, pos_byte, end;
4104 enum prop_handled handled = HANDLED_NORMALLY;
4105
4106 if (STRINGP (it->string))
4107 {
4108 pos = IT_STRING_CHARPOS (*it);
4109 pos_byte = IT_STRING_BYTEPOS (*it);
4110 string = it->string;
4111 }
4112 else
4113 {
4114 pos = IT_CHARPOS (*it);
4115 pos_byte = IT_BYTEPOS (*it);
4116 string = Qnil;
4117 }
4118
4119 /* If there's a valid composition and point is not inside of the
4120 composition (in the case that the composition is from the current
4121 buffer), draw a glyph composed from the composition components. */
4122 if (find_composition (pos, -1, &pos, &end, &prop, string)
4123 && COMPOSITION_VALID_P (pos, end, prop)
4124 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4125 {
4126 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4127
4128 if (id >= 0)
4129 {
4130 it->method = GET_FROM_COMPOSITION;
4131 it->cmp_id = id;
4132 it->cmp_len = COMPOSITION_LENGTH (prop);
4133 /* For a terminal, draw only the first character of the
4134 components. */
4135 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4136 it->len = (STRINGP (it->string)
4137 ? string_char_to_byte (it->string, end)
4138 : CHAR_TO_BYTE (end)) - pos_byte;
4139 it->stop_charpos = end;
4140 handled = HANDLED_RETURN;
4141 }
4142 }
4143
4144 return handled;
4145 }
4146
4147
4148 \f
4149 /***********************************************************************
4150 Overlay strings
4151 ***********************************************************************/
4152
4153 /* The following structure is used to record overlay strings for
4154 later sorting in load_overlay_strings. */
4155
4156 struct overlay_entry
4157 {
4158 Lisp_Object overlay;
4159 Lisp_Object string;
4160 int priority;
4161 int after_string_p;
4162 };
4163
4164
4165 /* Set up iterator IT from overlay strings at its current position.
4166 Called from handle_stop. */
4167
4168 static enum prop_handled
4169 handle_overlay_change (it)
4170 struct it *it;
4171 {
4172 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4173 return HANDLED_RECOMPUTE_PROPS;
4174 else
4175 return HANDLED_NORMALLY;
4176 }
4177
4178
4179 /* Set up the next overlay string for delivery by IT, if there is an
4180 overlay string to deliver. Called by set_iterator_to_next when the
4181 end of the current overlay string is reached. If there are more
4182 overlay strings to display, IT->string and
4183 IT->current.overlay_string_index are set appropriately here.
4184 Otherwise IT->string is set to nil. */
4185
4186 static void
4187 next_overlay_string (it)
4188 struct it *it;
4189 {
4190 ++it->current.overlay_string_index;
4191 if (it->current.overlay_string_index == it->n_overlay_strings)
4192 {
4193 /* No more overlay strings. Restore IT's settings to what
4194 they were before overlay strings were processed, and
4195 continue to deliver from current_buffer. */
4196 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4197
4198 pop_it (it);
4199 xassert (it->stop_charpos >= BEGV
4200 && it->stop_charpos <= it->end_charpos);
4201 it->string = Qnil;
4202 it->current.overlay_string_index = -1;
4203 SET_TEXT_POS (it->current.string_pos, -1, -1);
4204 it->n_overlay_strings = 0;
4205 it->method = GET_FROM_BUFFER;
4206
4207 /* If we're at the end of the buffer, record that we have
4208 processed the overlay strings there already, so that
4209 next_element_from_buffer doesn't try it again. */
4210 if (IT_CHARPOS (*it) >= it->end_charpos)
4211 it->overlay_strings_at_end_processed_p = 1;
4212
4213 /* If we have to display `...' for invisible text, set
4214 the iterator up for that. */
4215 if (display_ellipsis_p)
4216 setup_for_ellipsis (it, 0);
4217 }
4218 else
4219 {
4220 /* There are more overlay strings to process. If
4221 IT->current.overlay_string_index has advanced to a position
4222 where we must load IT->overlay_strings with more strings, do
4223 it. */
4224 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4225
4226 if (it->current.overlay_string_index && i == 0)
4227 load_overlay_strings (it, 0);
4228
4229 /* Initialize IT to deliver display elements from the overlay
4230 string. */
4231 it->string = it->overlay_strings[i];
4232 it->multibyte_p = STRING_MULTIBYTE (it->string);
4233 SET_TEXT_POS (it->current.string_pos, 0, 0);
4234 it->method = GET_FROM_STRING;
4235 it->stop_charpos = 0;
4236 }
4237
4238 CHECK_IT (it);
4239 }
4240
4241
4242 /* Compare two overlay_entry structures E1 and E2. Used as a
4243 comparison function for qsort in load_overlay_strings. Overlay
4244 strings for the same position are sorted so that
4245
4246 1. All after-strings come in front of before-strings, except
4247 when they come from the same overlay.
4248
4249 2. Within after-strings, strings are sorted so that overlay strings
4250 from overlays with higher priorities come first.
4251
4252 2. Within before-strings, strings are sorted so that overlay
4253 strings from overlays with higher priorities come last.
4254
4255 Value is analogous to strcmp. */
4256
4257
4258 static int
4259 compare_overlay_entries (e1, e2)
4260 void *e1, *e2;
4261 {
4262 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4263 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4264 int result;
4265
4266 if (entry1->after_string_p != entry2->after_string_p)
4267 {
4268 /* Let after-strings appear in front of before-strings if
4269 they come from different overlays. */
4270 if (EQ (entry1->overlay, entry2->overlay))
4271 result = entry1->after_string_p ? 1 : -1;
4272 else
4273 result = entry1->after_string_p ? -1 : 1;
4274 }
4275 else if (entry1->after_string_p)
4276 /* After-strings sorted in order of decreasing priority. */
4277 result = entry2->priority - entry1->priority;
4278 else
4279 /* Before-strings sorted in order of increasing priority. */
4280 result = entry1->priority - entry2->priority;
4281
4282 return result;
4283 }
4284
4285
4286 /* Load the vector IT->overlay_strings with overlay strings from IT's
4287 current buffer position, or from CHARPOS if that is > 0. Set
4288 IT->n_overlays to the total number of overlay strings found.
4289
4290 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4291 a time. On entry into load_overlay_strings,
4292 IT->current.overlay_string_index gives the number of overlay
4293 strings that have already been loaded by previous calls to this
4294 function.
4295
4296 IT->add_overlay_start contains an additional overlay start
4297 position to consider for taking overlay strings from, if non-zero.
4298 This position comes into play when the overlay has an `invisible'
4299 property, and both before and after-strings. When we've skipped to
4300 the end of the overlay, because of its `invisible' property, we
4301 nevertheless want its before-string to appear.
4302 IT->add_overlay_start will contain the overlay start position
4303 in this case.
4304
4305 Overlay strings are sorted so that after-string strings come in
4306 front of before-string strings. Within before and after-strings,
4307 strings are sorted by overlay priority. See also function
4308 compare_overlay_entries. */
4309
4310 static void
4311 load_overlay_strings (it, charpos)
4312 struct it *it;
4313 int charpos;
4314 {
4315 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4316 Lisp_Object overlay, window, str, invisible;
4317 struct Lisp_Overlay *ov;
4318 int start, end;
4319 int size = 20;
4320 int n = 0, i, j, invis_p;
4321 struct overlay_entry *entries
4322 = (struct overlay_entry *) alloca (size * sizeof *entries);
4323
4324 if (charpos <= 0)
4325 charpos = IT_CHARPOS (*it);
4326
4327 /* Append the overlay string STRING of overlay OVERLAY to vector
4328 `entries' which has size `size' and currently contains `n'
4329 elements. AFTER_P non-zero means STRING is an after-string of
4330 OVERLAY. */
4331 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4332 do \
4333 { \
4334 Lisp_Object priority; \
4335 \
4336 if (n == size) \
4337 { \
4338 int new_size = 2 * size; \
4339 struct overlay_entry *old = entries; \
4340 entries = \
4341 (struct overlay_entry *) alloca (new_size \
4342 * sizeof *entries); \
4343 bcopy (old, entries, size * sizeof *entries); \
4344 size = new_size; \
4345 } \
4346 \
4347 entries[n].string = (STRING); \
4348 entries[n].overlay = (OVERLAY); \
4349 priority = Foverlay_get ((OVERLAY), Qpriority); \
4350 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4351 entries[n].after_string_p = (AFTER_P); \
4352 ++n; \
4353 } \
4354 while (0)
4355
4356 /* Process overlay before the overlay center. */
4357 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4358 {
4359 XSETMISC (overlay, ov);
4360 xassert (OVERLAYP (overlay));
4361 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4362 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4363
4364 if (end < charpos)
4365 break;
4366
4367 /* Skip this overlay if it doesn't start or end at IT's current
4368 position. */
4369 if (end != charpos && start != charpos)
4370 continue;
4371
4372 /* Skip this overlay if it doesn't apply to IT->w. */
4373 window = Foverlay_get (overlay, Qwindow);
4374 if (WINDOWP (window) && XWINDOW (window) != it->w)
4375 continue;
4376
4377 /* If the text ``under'' the overlay is invisible, both before-
4378 and after-strings from this overlay are visible; start and
4379 end position are indistinguishable. */
4380 invisible = Foverlay_get (overlay, Qinvisible);
4381 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4382
4383 /* If overlay has a non-empty before-string, record it. */
4384 if ((start == charpos || (end == charpos && invis_p))
4385 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4386 && SCHARS (str))
4387 RECORD_OVERLAY_STRING (overlay, str, 0);
4388
4389 /* If overlay has a non-empty after-string, record it. */
4390 if ((end == charpos || (start == charpos && invis_p))
4391 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4392 && SCHARS (str))
4393 RECORD_OVERLAY_STRING (overlay, str, 1);
4394 }
4395
4396 /* Process overlays after the overlay center. */
4397 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4398 {
4399 XSETMISC (overlay, ov);
4400 xassert (OVERLAYP (overlay));
4401 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4402 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4403
4404 if (start > charpos)
4405 break;
4406
4407 /* Skip this overlay if it doesn't start or end at IT's current
4408 position. */
4409 if (end != charpos && start != charpos)
4410 continue;
4411
4412 /* Skip this overlay if it doesn't apply to IT->w. */
4413 window = Foverlay_get (overlay, Qwindow);
4414 if (WINDOWP (window) && XWINDOW (window) != it->w)
4415 continue;
4416
4417 /* If the text ``under'' the overlay is invisible, it has a zero
4418 dimension, and both before- and after-strings apply. */
4419 invisible = Foverlay_get (overlay, Qinvisible);
4420 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4421
4422 /* If overlay has a non-empty before-string, record it. */
4423 if ((start == charpos || (end == charpos && invis_p))
4424 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4425 && SCHARS (str))
4426 RECORD_OVERLAY_STRING (overlay, str, 0);
4427
4428 /* If overlay has a non-empty after-string, record it. */
4429 if ((end == charpos || (start == charpos && invis_p))
4430 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4431 && SCHARS (str))
4432 RECORD_OVERLAY_STRING (overlay, str, 1);
4433 }
4434
4435 #undef RECORD_OVERLAY_STRING
4436
4437 /* Sort entries. */
4438 if (n > 1)
4439 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4440
4441 /* Record the total number of strings to process. */
4442 it->n_overlay_strings = n;
4443
4444 /* IT->current.overlay_string_index is the number of overlay strings
4445 that have already been consumed by IT. Copy some of the
4446 remaining overlay strings to IT->overlay_strings. */
4447 i = 0;
4448 j = it->current.overlay_string_index;
4449 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4450 it->overlay_strings[i++] = entries[j++].string;
4451
4452 CHECK_IT (it);
4453 }
4454
4455
4456 /* Get the first chunk of overlay strings at IT's current buffer
4457 position, or at CHARPOS if that is > 0. Value is non-zero if at
4458 least one overlay string was found. */
4459
4460 static int
4461 get_overlay_strings (it, charpos)
4462 struct it *it;
4463 int charpos;
4464 {
4465 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4466 process. This fills IT->overlay_strings with strings, and sets
4467 IT->n_overlay_strings to the total number of strings to process.
4468 IT->pos.overlay_string_index has to be set temporarily to zero
4469 because load_overlay_strings needs this; it must be set to -1
4470 when no overlay strings are found because a zero value would
4471 indicate a position in the first overlay string. */
4472 it->current.overlay_string_index = 0;
4473 load_overlay_strings (it, charpos);
4474
4475 /* If we found overlay strings, set up IT to deliver display
4476 elements from the first one. Otherwise set up IT to deliver
4477 from current_buffer. */
4478 if (it->n_overlay_strings)
4479 {
4480 /* Make sure we know settings in current_buffer, so that we can
4481 restore meaningful values when we're done with the overlay
4482 strings. */
4483 compute_stop_pos (it);
4484 xassert (it->face_id >= 0);
4485
4486 /* Save IT's settings. They are restored after all overlay
4487 strings have been processed. */
4488 xassert (it->sp == 0);
4489 push_it (it);
4490
4491 /* Set up IT to deliver display elements from the first overlay
4492 string. */
4493 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4494 it->string = it->overlay_strings[0];
4495 it->stop_charpos = 0;
4496 xassert (STRINGP (it->string));
4497 it->end_charpos = SCHARS (it->string);
4498 it->multibyte_p = STRING_MULTIBYTE (it->string);
4499 it->method = GET_FROM_STRING;
4500 }
4501 else
4502 {
4503 it->string = Qnil;
4504 it->current.overlay_string_index = -1;
4505 it->method = GET_FROM_BUFFER;
4506 }
4507
4508 CHECK_IT (it);
4509
4510 /* Value is non-zero if we found at least one overlay string. */
4511 return STRINGP (it->string);
4512 }
4513
4514
4515 \f
4516 /***********************************************************************
4517 Saving and restoring state
4518 ***********************************************************************/
4519
4520 /* Save current settings of IT on IT->stack. Called, for example,
4521 before setting up IT for an overlay string, to be able to restore
4522 IT's settings to what they were after the overlay string has been
4523 processed. */
4524
4525 static void
4526 push_it (it)
4527 struct it *it;
4528 {
4529 struct iterator_stack_entry *p;
4530
4531 xassert (it->sp < 2);
4532 p = it->stack + it->sp;
4533
4534 p->stop_charpos = it->stop_charpos;
4535 xassert (it->face_id >= 0);
4536 p->face_id = it->face_id;
4537 p->string = it->string;
4538 p->pos = it->current;
4539 p->end_charpos = it->end_charpos;
4540 p->string_nchars = it->string_nchars;
4541 p->area = it->area;
4542 p->multibyte_p = it->multibyte_p;
4543 p->slice = it->slice;
4544 p->space_width = it->space_width;
4545 p->font_height = it->font_height;
4546 p->voffset = it->voffset;
4547 p->string_from_display_prop_p = it->string_from_display_prop_p;
4548 p->display_ellipsis_p = 0;
4549 ++it->sp;
4550 }
4551
4552
4553 /* Restore IT's settings from IT->stack. Called, for example, when no
4554 more overlay strings must be processed, and we return to delivering
4555 display elements from a buffer, or when the end of a string from a
4556 `display' property is reached and we return to delivering display
4557 elements from an overlay string, or from a buffer. */
4558
4559 static void
4560 pop_it (it)
4561 struct it *it;
4562 {
4563 struct iterator_stack_entry *p;
4564
4565 xassert (it->sp > 0);
4566 --it->sp;
4567 p = it->stack + it->sp;
4568 it->stop_charpos = p->stop_charpos;
4569 it->face_id = p->face_id;
4570 it->string = p->string;
4571 it->current = p->pos;
4572 it->end_charpos = p->end_charpos;
4573 it->string_nchars = p->string_nchars;
4574 it->area = p->area;
4575 it->multibyte_p = p->multibyte_p;
4576 it->slice = p->slice;
4577 it->space_width = p->space_width;
4578 it->font_height = p->font_height;
4579 it->voffset = p->voffset;
4580 it->string_from_display_prop_p = p->string_from_display_prop_p;
4581 }
4582
4583
4584 \f
4585 /***********************************************************************
4586 Moving over lines
4587 ***********************************************************************/
4588
4589 /* Set IT's current position to the previous line start. */
4590
4591 static void
4592 back_to_previous_line_start (it)
4593 struct it *it;
4594 {
4595 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4596 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4597 }
4598
4599
4600 /* Move IT to the next line start.
4601
4602 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4603 we skipped over part of the text (as opposed to moving the iterator
4604 continuously over the text). Otherwise, don't change the value
4605 of *SKIPPED_P.
4606
4607 Newlines may come from buffer text, overlay strings, or strings
4608 displayed via the `display' property. That's the reason we can't
4609 simply use find_next_newline_no_quit.
4610
4611 Note that this function may not skip over invisible text that is so
4612 because of text properties and immediately follows a newline. If
4613 it would, function reseat_at_next_visible_line_start, when called
4614 from set_iterator_to_next, would effectively make invisible
4615 characters following a newline part of the wrong glyph row, which
4616 leads to wrong cursor motion. */
4617
4618 static int
4619 forward_to_next_line_start (it, skipped_p)
4620 struct it *it;
4621 int *skipped_p;
4622 {
4623 int old_selective, newline_found_p, n;
4624 const int MAX_NEWLINE_DISTANCE = 500;
4625
4626 /* If already on a newline, just consume it to avoid unintended
4627 skipping over invisible text below. */
4628 if (it->what == IT_CHARACTER
4629 && it->c == '\n'
4630 && CHARPOS (it->position) == IT_CHARPOS (*it))
4631 {
4632 set_iterator_to_next (it, 0);
4633 it->c = 0;
4634 return 1;
4635 }
4636
4637 /* Don't handle selective display in the following. It's (a)
4638 unnecessary because it's done by the caller, and (b) leads to an
4639 infinite recursion because next_element_from_ellipsis indirectly
4640 calls this function. */
4641 old_selective = it->selective;
4642 it->selective = 0;
4643
4644 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4645 from buffer text. */
4646 for (n = newline_found_p = 0;
4647 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4648 n += STRINGP (it->string) ? 0 : 1)
4649 {
4650 if (!get_next_display_element (it))
4651 return 0;
4652 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4653 set_iterator_to_next (it, 0);
4654 }
4655
4656 /* If we didn't find a newline near enough, see if we can use a
4657 short-cut. */
4658 if (!newline_found_p)
4659 {
4660 int start = IT_CHARPOS (*it);
4661 int limit = find_next_newline_no_quit (start, 1);
4662 Lisp_Object pos;
4663
4664 xassert (!STRINGP (it->string));
4665
4666 /* If there isn't any `display' property in sight, and no
4667 overlays, we can just use the position of the newline in
4668 buffer text. */
4669 if (it->stop_charpos >= limit
4670 || ((pos = Fnext_single_property_change (make_number (start),
4671 Qdisplay,
4672 Qnil, make_number (limit)),
4673 NILP (pos))
4674 && next_overlay_change (start) == ZV))
4675 {
4676 IT_CHARPOS (*it) = limit;
4677 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4678 *skipped_p = newline_found_p = 1;
4679 }
4680 else
4681 {
4682 while (get_next_display_element (it)
4683 && !newline_found_p)
4684 {
4685 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4686 set_iterator_to_next (it, 0);
4687 }
4688 }
4689 }
4690
4691 it->selective = old_selective;
4692 return newline_found_p;
4693 }
4694
4695
4696 /* Set IT's current position to the previous visible line start. Skip
4697 invisible text that is so either due to text properties or due to
4698 selective display. Caution: this does not change IT->current_x and
4699 IT->hpos. */
4700
4701 static void
4702 back_to_previous_visible_line_start (it)
4703 struct it *it;
4704 {
4705 while (IT_CHARPOS (*it) > BEGV)
4706 {
4707 back_to_previous_line_start (it);
4708 if (IT_CHARPOS (*it) <= BEGV)
4709 break;
4710
4711 /* If selective > 0, then lines indented more than that values
4712 are invisible. */
4713 if (it->selective > 0
4714 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4715 (double) it->selective)) /* iftc */
4716 continue;
4717
4718 /* Check the newline before point for invisibility. */
4719 {
4720 Lisp_Object prop;
4721 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4722 Qinvisible, it->window);
4723 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4724 continue;
4725 }
4726
4727 /* If newline has a display property that replaces the newline with something
4728 else (image or text), find start of overlay or interval and continue search
4729 from that point. */
4730 if (IT_CHARPOS (*it) > BEGV)
4731 {
4732 struct it it2 = *it;
4733 int pos;
4734 int beg, end;
4735 Lisp_Object val, overlay;
4736
4737 pos = --IT_CHARPOS (it2);
4738 --IT_BYTEPOS (it2);
4739 it2.sp = 0;
4740 if (handle_display_prop (&it2) == HANDLED_RETURN
4741 && !NILP (val = get_char_property_and_overlay
4742 (make_number (pos), Qdisplay, Qnil, &overlay))
4743 && (OVERLAYP (overlay)
4744 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4745 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4746 {
4747 if (beg < BEGV)
4748 beg = BEGV;
4749 IT_CHARPOS (*it) = beg;
4750 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4751 continue;
4752 }
4753 }
4754
4755 break;
4756 }
4757
4758 xassert (IT_CHARPOS (*it) >= BEGV);
4759 xassert (IT_CHARPOS (*it) == BEGV
4760 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4761 CHECK_IT (it);
4762 }
4763
4764
4765 /* Reseat iterator IT at the previous visible line start. Skip
4766 invisible text that is so either due to text properties or due to
4767 selective display. At the end, update IT's overlay information,
4768 face information etc. */
4769
4770 void
4771 reseat_at_previous_visible_line_start (it)
4772 struct it *it;
4773 {
4774 back_to_previous_visible_line_start (it);
4775 reseat (it, it->current.pos, 1);
4776 CHECK_IT (it);
4777 }
4778
4779
4780 /* Reseat iterator IT on the next visible line start in the current
4781 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4782 preceding the line start. Skip over invisible text that is so
4783 because of selective display. Compute faces, overlays etc at the
4784 new position. Note that this function does not skip over text that
4785 is invisible because of text properties. */
4786
4787 static void
4788 reseat_at_next_visible_line_start (it, on_newline_p)
4789 struct it *it;
4790 int on_newline_p;
4791 {
4792 int newline_found_p, skipped_p = 0;
4793
4794 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4795
4796 /* Skip over lines that are invisible because they are indented
4797 more than the value of IT->selective. */
4798 if (it->selective > 0)
4799 while (IT_CHARPOS (*it) < ZV
4800 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4801 (double) it->selective)) /* iftc */
4802 {
4803 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4804 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4805 }
4806
4807 /* Position on the newline if that's what's requested. */
4808 if (on_newline_p && newline_found_p)
4809 {
4810 if (STRINGP (it->string))
4811 {
4812 if (IT_STRING_CHARPOS (*it) > 0)
4813 {
4814 --IT_STRING_CHARPOS (*it);
4815 --IT_STRING_BYTEPOS (*it);
4816 }
4817 }
4818 else if (IT_CHARPOS (*it) > BEGV)
4819 {
4820 --IT_CHARPOS (*it);
4821 --IT_BYTEPOS (*it);
4822 reseat (it, it->current.pos, 0);
4823 }
4824 }
4825 else if (skipped_p)
4826 reseat (it, it->current.pos, 0);
4827
4828 CHECK_IT (it);
4829 }
4830
4831
4832 \f
4833 /***********************************************************************
4834 Changing an iterator's position
4835 ***********************************************************************/
4836
4837 /* Change IT's current position to POS in current_buffer. If FORCE_P
4838 is non-zero, always check for text properties at the new position.
4839 Otherwise, text properties are only looked up if POS >=
4840 IT->check_charpos of a property. */
4841
4842 static void
4843 reseat (it, pos, force_p)
4844 struct it *it;
4845 struct text_pos pos;
4846 int force_p;
4847 {
4848 int original_pos = IT_CHARPOS (*it);
4849
4850 reseat_1 (it, pos, 0);
4851
4852 /* Determine where to check text properties. Avoid doing it
4853 where possible because text property lookup is very expensive. */
4854 if (force_p
4855 || CHARPOS (pos) > it->stop_charpos
4856 || CHARPOS (pos) < original_pos)
4857 handle_stop (it);
4858
4859 CHECK_IT (it);
4860 }
4861
4862
4863 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4864 IT->stop_pos to POS, also. */
4865
4866 static void
4867 reseat_1 (it, pos, set_stop_p)
4868 struct it *it;
4869 struct text_pos pos;
4870 int set_stop_p;
4871 {
4872 /* Don't call this function when scanning a C string. */
4873 xassert (it->s == NULL);
4874
4875 /* POS must be a reasonable value. */
4876 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4877
4878 it->current.pos = it->position = pos;
4879 XSETBUFFER (it->object, current_buffer);
4880 it->end_charpos = ZV;
4881 it->dpvec = NULL;
4882 it->current.dpvec_index = -1;
4883 it->current.overlay_string_index = -1;
4884 IT_STRING_CHARPOS (*it) = -1;
4885 IT_STRING_BYTEPOS (*it) = -1;
4886 it->string = Qnil;
4887 it->method = GET_FROM_BUFFER;
4888 /* RMS: I added this to fix a bug in move_it_vertically_backward
4889 where it->area continued to relate to the starting point
4890 for the backward motion. Bug report from
4891 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4892 However, I am not sure whether reseat still does the right thing
4893 in general after this change. */
4894 it->area = TEXT_AREA;
4895 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4896 it->sp = 0;
4897 it->face_before_selective_p = 0;
4898
4899 if (set_stop_p)
4900 it->stop_charpos = CHARPOS (pos);
4901 }
4902
4903
4904 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4905 If S is non-null, it is a C string to iterate over. Otherwise,
4906 STRING gives a Lisp string to iterate over.
4907
4908 If PRECISION > 0, don't return more then PRECISION number of
4909 characters from the string.
4910
4911 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4912 characters have been returned. FIELD_WIDTH < 0 means an infinite
4913 field width.
4914
4915 MULTIBYTE = 0 means disable processing of multibyte characters,
4916 MULTIBYTE > 0 means enable it,
4917 MULTIBYTE < 0 means use IT->multibyte_p.
4918
4919 IT must be initialized via a prior call to init_iterator before
4920 calling this function. */
4921
4922 static void
4923 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4924 struct it *it;
4925 unsigned char *s;
4926 Lisp_Object string;
4927 int charpos;
4928 int precision, field_width, multibyte;
4929 {
4930 /* No region in strings. */
4931 it->region_beg_charpos = it->region_end_charpos = -1;
4932
4933 /* No text property checks performed by default, but see below. */
4934 it->stop_charpos = -1;
4935
4936 /* Set iterator position and end position. */
4937 bzero (&it->current, sizeof it->current);
4938 it->current.overlay_string_index = -1;
4939 it->current.dpvec_index = -1;
4940 xassert (charpos >= 0);
4941
4942 /* If STRING is specified, use its multibyteness, otherwise use the
4943 setting of MULTIBYTE, if specified. */
4944 if (multibyte >= 0)
4945 it->multibyte_p = multibyte > 0;
4946
4947 if (s == NULL)
4948 {
4949 xassert (STRINGP (string));
4950 it->string = string;
4951 it->s = NULL;
4952 it->end_charpos = it->string_nchars = SCHARS (string);
4953 it->method = GET_FROM_STRING;
4954 it->current.string_pos = string_pos (charpos, string);
4955 }
4956 else
4957 {
4958 it->s = s;
4959 it->string = Qnil;
4960
4961 /* Note that we use IT->current.pos, not it->current.string_pos,
4962 for displaying C strings. */
4963 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4964 if (it->multibyte_p)
4965 {
4966 it->current.pos = c_string_pos (charpos, s, 1);
4967 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4968 }
4969 else
4970 {
4971 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4972 it->end_charpos = it->string_nchars = strlen (s);
4973 }
4974
4975 it->method = GET_FROM_C_STRING;
4976 }
4977
4978 /* PRECISION > 0 means don't return more than PRECISION characters
4979 from the string. */
4980 if (precision > 0 && it->end_charpos - charpos > precision)
4981 it->end_charpos = it->string_nchars = charpos + precision;
4982
4983 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4984 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4985 FIELD_WIDTH < 0 means infinite field width. This is useful for
4986 padding with `-' at the end of a mode line. */
4987 if (field_width < 0)
4988 field_width = INFINITY;
4989 if (field_width > it->end_charpos - charpos)
4990 it->end_charpos = charpos + field_width;
4991
4992 /* Use the standard display table for displaying strings. */
4993 if (DISP_TABLE_P (Vstandard_display_table))
4994 it->dp = XCHAR_TABLE (Vstandard_display_table);
4995
4996 it->stop_charpos = charpos;
4997 CHECK_IT (it);
4998 }
4999
5000
5001 \f
5002 /***********************************************************************
5003 Iteration
5004 ***********************************************************************/
5005
5006 /* Map enum it_method value to corresponding next_element_from_* function. */
5007
5008 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5009 {
5010 next_element_from_buffer,
5011 next_element_from_display_vector,
5012 next_element_from_composition,
5013 next_element_from_string,
5014 next_element_from_c_string,
5015 next_element_from_image,
5016 next_element_from_stretch
5017 };
5018
5019
5020 /* Load IT's display element fields with information about the next
5021 display element from the current position of IT. Value is zero if
5022 end of buffer (or C string) is reached. */
5023
5024 int
5025 get_next_display_element (it)
5026 struct it *it;
5027 {
5028 /* Non-zero means that we found a display element. Zero means that
5029 we hit the end of what we iterate over. Performance note: the
5030 function pointer `method' used here turns out to be faster than
5031 using a sequence of if-statements. */
5032 int success_p;
5033
5034 get_next:
5035 success_p = (*get_next_element[it->method]) (it);
5036
5037 if (it->what == IT_CHARACTER)
5038 {
5039 /* Map via display table or translate control characters.
5040 IT->c, IT->len etc. have been set to the next character by
5041 the function call above. If we have a display table, and it
5042 contains an entry for IT->c, translate it. Don't do this if
5043 IT->c itself comes from a display table, otherwise we could
5044 end up in an infinite recursion. (An alternative could be to
5045 count the recursion depth of this function and signal an
5046 error when a certain maximum depth is reached.) Is it worth
5047 it? */
5048 if (success_p && it->dpvec == NULL)
5049 {
5050 Lisp_Object dv;
5051
5052 if (it->dp
5053 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5054 VECTORP (dv)))
5055 {
5056 struct Lisp_Vector *v = XVECTOR (dv);
5057
5058 /* Return the first character from the display table
5059 entry, if not empty. If empty, don't display the
5060 current character. */
5061 if (v->size)
5062 {
5063 it->dpvec_char_len = it->len;
5064 it->dpvec = v->contents;
5065 it->dpend = v->contents + v->size;
5066 it->current.dpvec_index = 0;
5067 it->dpvec_face_id = -1;
5068 it->saved_face_id = it->face_id;
5069 it->method = GET_FROM_DISPLAY_VECTOR;
5070 it->ellipsis_p = 0;
5071 }
5072 else
5073 {
5074 set_iterator_to_next (it, 0);
5075 }
5076 goto get_next;
5077 }
5078
5079 /* Translate control characters into `\003' or `^C' form.
5080 Control characters coming from a display table entry are
5081 currently not translated because we use IT->dpvec to hold
5082 the translation. This could easily be changed but I
5083 don't believe that it is worth doing.
5084
5085 If it->multibyte_p is nonzero, eight-bit characters and
5086 non-printable multibyte characters are also translated to
5087 octal form.
5088
5089 If it->multibyte_p is zero, eight-bit characters that
5090 don't have corresponding multibyte char code are also
5091 translated to octal form. */
5092 else if ((it->c < ' '
5093 && (it->area != TEXT_AREA
5094 /* In mode line, treat \n like other crl chars. */
5095 || (it->c != '\t'
5096 && it->glyph_row && it->glyph_row->mode_line_p)
5097 || (it->c != '\n' && it->c != '\t')))
5098 || (it->multibyte_p
5099 ? ((it->c >= 127
5100 && it->len == 1)
5101 || !CHAR_PRINTABLE_P (it->c)
5102 || (!NILP (Vnobreak_char_display)
5103 && (it->c == 0x8a0 || it->c == 0x8ad
5104 || it->c == 0x920 || it->c == 0x92d
5105 || it->c == 0xe20 || it->c == 0xe2d
5106 || it->c == 0xf20 || it->c == 0xf2d)))
5107 : (it->c >= 127
5108 && (!unibyte_display_via_language_environment
5109 || it->c == unibyte_char_to_multibyte (it->c)))))
5110 {
5111 /* IT->c is a control character which must be displayed
5112 either as '\003' or as `^C' where the '\\' and '^'
5113 can be defined in the display table. Fill
5114 IT->ctl_chars with glyphs for what we have to
5115 display. Then, set IT->dpvec to these glyphs. */
5116 GLYPH g;
5117 int ctl_len;
5118 int face_id, lface_id = 0 ;
5119 GLYPH escape_glyph;
5120
5121 /* Handle control characters with ^. */
5122
5123 if (it->c < 128 && it->ctl_arrow_p)
5124 {
5125 g = '^'; /* default glyph for Control */
5126 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5127 if (it->dp
5128 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5129 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5130 {
5131 g = XINT (DISP_CTRL_GLYPH (it->dp));
5132 lface_id = FAST_GLYPH_FACE (g);
5133 }
5134 if (lface_id)
5135 {
5136 g = FAST_GLYPH_CHAR (g);
5137 face_id = merge_faces (it->f, Qt, lface_id,
5138 it->face_id);
5139 }
5140 else
5141 {
5142 /* Merge the escape-glyph face into the current face. */
5143 face_id = merge_faces (it->f, Qescape_glyph, 0,
5144 it->face_id);
5145 }
5146
5147 XSETINT (it->ctl_chars[0], g);
5148 g = it->c ^ 0100;
5149 XSETINT (it->ctl_chars[1], g);
5150 ctl_len = 2;
5151 goto display_control;
5152 }
5153
5154 /* Handle non-break space in the mode where it only gets
5155 highlighting. */
5156
5157 if (EQ (Vnobreak_char_display, Qt)
5158 && (it->c == 0x8a0 || it->c == 0x920
5159 || it->c == 0xe20 || it->c == 0xf20))
5160 {
5161 /* Merge the no-break-space face into the current face. */
5162 face_id = merge_faces (it->f, Qnobreak_space, 0,
5163 it->face_id);
5164
5165 g = it->c = ' ';
5166 XSETINT (it->ctl_chars[0], g);
5167 ctl_len = 1;
5168 goto display_control;
5169 }
5170
5171 /* Handle sequences that start with the "escape glyph". */
5172
5173 /* the default escape glyph is \. */
5174 escape_glyph = '\\';
5175
5176 if (it->dp
5177 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5178 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5179 {
5180 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5181 lface_id = FAST_GLYPH_FACE (escape_glyph);
5182 }
5183 if (lface_id)
5184 {
5185 /* The display table specified a face.
5186 Merge it into face_id and also into escape_glyph. */
5187 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5188 face_id = merge_faces (it->f, Qt, lface_id,
5189 it->face_id);
5190 }
5191 else
5192 {
5193 /* Merge the escape-glyph face into the current face. */
5194 face_id = merge_faces (it->f, Qescape_glyph, 0,
5195 it->face_id);
5196 }
5197
5198 /* Handle soft hyphens in the mode where they only get
5199 highlighting. */
5200
5201 if (EQ (Vnobreak_char_display, Qt)
5202 && (it->c == 0x8ad || it->c == 0x92d
5203 || it->c == 0xe2d || it->c == 0xf2d))
5204 {
5205 g = it->c = '-';
5206 XSETINT (it->ctl_chars[0], g);
5207 ctl_len = 1;
5208 goto display_control;
5209 }
5210
5211 /* Handle non-break space and soft hyphen
5212 with the escape glyph. */
5213
5214 if (it->c == 0x8a0 || it->c == 0x8ad
5215 || it->c == 0x920 || it->c == 0x92d
5216 || it->c == 0xe20 || it->c == 0xe2d
5217 || it->c == 0xf20 || it->c == 0xf2d)
5218 {
5219 XSETINT (it->ctl_chars[0], escape_glyph);
5220 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5221 XSETINT (it->ctl_chars[1], g);
5222 ctl_len = 2;
5223 goto display_control;
5224 }
5225
5226 {
5227 unsigned char str[MAX_MULTIBYTE_LENGTH];
5228 int len;
5229 int i;
5230
5231 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5232 if (SINGLE_BYTE_CHAR_P (it->c))
5233 str[0] = it->c, len = 1;
5234 else
5235 {
5236 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5237 if (len < 0)
5238 {
5239 /* It's an invalid character, which shouldn't
5240 happen actually, but due to bugs it may
5241 happen. Let's print the char as is, there's
5242 not much meaningful we can do with it. */
5243 str[0] = it->c;
5244 str[1] = it->c >> 8;
5245 str[2] = it->c >> 16;
5246 str[3] = it->c >> 24;
5247 len = 4;
5248 }
5249 }
5250
5251 for (i = 0; i < len; i++)
5252 {
5253 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5254 /* Insert three more glyphs into IT->ctl_chars for
5255 the octal display of the character. */
5256 g = ((str[i] >> 6) & 7) + '0';
5257 XSETINT (it->ctl_chars[i * 4 + 1], g);
5258 g = ((str[i] >> 3) & 7) + '0';
5259 XSETINT (it->ctl_chars[i * 4 + 2], g);
5260 g = (str[i] & 7) + '0';
5261 XSETINT (it->ctl_chars[i * 4 + 3], g);
5262 }
5263 ctl_len = len * 4;
5264 }
5265
5266 display_control:
5267 /* Set up IT->dpvec and return first character from it. */
5268 it->dpvec_char_len = it->len;
5269 it->dpvec = it->ctl_chars;
5270 it->dpend = it->dpvec + ctl_len;
5271 it->current.dpvec_index = 0;
5272 it->dpvec_face_id = face_id;
5273 it->saved_face_id = it->face_id;
5274 it->method = GET_FROM_DISPLAY_VECTOR;
5275 it->ellipsis_p = 0;
5276 goto get_next;
5277 }
5278 }
5279
5280 /* Adjust face id for a multibyte character. There are no
5281 multibyte character in unibyte text. */
5282 if (it->multibyte_p
5283 && success_p
5284 && FRAME_WINDOW_P (it->f))
5285 {
5286 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5287 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5288 }
5289 }
5290
5291 /* Is this character the last one of a run of characters with
5292 box? If yes, set IT->end_of_box_run_p to 1. */
5293 if (it->face_box_p
5294 && it->s == NULL)
5295 {
5296 int face_id;
5297 struct face *face;
5298
5299 it->end_of_box_run_p
5300 = ((face_id = face_after_it_pos (it),
5301 face_id != it->face_id)
5302 && (face = FACE_FROM_ID (it->f, face_id),
5303 face->box == FACE_NO_BOX));
5304 }
5305
5306 /* Value is 0 if end of buffer or string reached. */
5307 return success_p;
5308 }
5309
5310
5311 /* Move IT to the next display element.
5312
5313 RESEAT_P non-zero means if called on a newline in buffer text,
5314 skip to the next visible line start.
5315
5316 Functions get_next_display_element and set_iterator_to_next are
5317 separate because I find this arrangement easier to handle than a
5318 get_next_display_element function that also increments IT's
5319 position. The way it is we can first look at an iterator's current
5320 display element, decide whether it fits on a line, and if it does,
5321 increment the iterator position. The other way around we probably
5322 would either need a flag indicating whether the iterator has to be
5323 incremented the next time, or we would have to implement a
5324 decrement position function which would not be easy to write. */
5325
5326 void
5327 set_iterator_to_next (it, reseat_p)
5328 struct it *it;
5329 int reseat_p;
5330 {
5331 /* Reset flags indicating start and end of a sequence of characters
5332 with box. Reset them at the start of this function because
5333 moving the iterator to a new position might set them. */
5334 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5335
5336 switch (it->method)
5337 {
5338 case GET_FROM_BUFFER:
5339 /* The current display element of IT is a character from
5340 current_buffer. Advance in the buffer, and maybe skip over
5341 invisible lines that are so because of selective display. */
5342 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5343 reseat_at_next_visible_line_start (it, 0);
5344 else
5345 {
5346 xassert (it->len != 0);
5347 IT_BYTEPOS (*it) += it->len;
5348 IT_CHARPOS (*it) += 1;
5349 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5350 }
5351 break;
5352
5353 case GET_FROM_COMPOSITION:
5354 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5355 if (STRINGP (it->string))
5356 {
5357 IT_STRING_BYTEPOS (*it) += it->len;
5358 IT_STRING_CHARPOS (*it) += it->cmp_len;
5359 it->method = GET_FROM_STRING;
5360 goto consider_string_end;
5361 }
5362 else
5363 {
5364 IT_BYTEPOS (*it) += it->len;
5365 IT_CHARPOS (*it) += it->cmp_len;
5366 it->method = GET_FROM_BUFFER;
5367 }
5368 break;
5369
5370 case GET_FROM_C_STRING:
5371 /* Current display element of IT is from a C string. */
5372 IT_BYTEPOS (*it) += it->len;
5373 IT_CHARPOS (*it) += 1;
5374 break;
5375
5376 case GET_FROM_DISPLAY_VECTOR:
5377 /* Current display element of IT is from a display table entry.
5378 Advance in the display table definition. Reset it to null if
5379 end reached, and continue with characters from buffers/
5380 strings. */
5381 ++it->current.dpvec_index;
5382
5383 /* Restore face of the iterator to what they were before the
5384 display vector entry (these entries may contain faces). */
5385 it->face_id = it->saved_face_id;
5386
5387 if (it->dpvec + it->current.dpvec_index == it->dpend)
5388 {
5389 if (it->s)
5390 it->method = GET_FROM_C_STRING;
5391 else if (STRINGP (it->string))
5392 it->method = GET_FROM_STRING;
5393 else
5394 it->method = GET_FROM_BUFFER;
5395
5396 it->dpvec = NULL;
5397 it->current.dpvec_index = -1;
5398
5399 /* Skip over characters which were displayed via IT->dpvec. */
5400 if (it->dpvec_char_len < 0)
5401 reseat_at_next_visible_line_start (it, 1);
5402 else if (it->dpvec_char_len > 0)
5403 {
5404 it->len = it->dpvec_char_len;
5405 set_iterator_to_next (it, reseat_p);
5406 }
5407
5408 /* Recheck faces after display vector */
5409 it->stop_charpos = IT_CHARPOS (*it);
5410 }
5411 break;
5412
5413 case GET_FROM_STRING:
5414 /* Current display element is a character from a Lisp string. */
5415 xassert (it->s == NULL && STRINGP (it->string));
5416 IT_STRING_BYTEPOS (*it) += it->len;
5417 IT_STRING_CHARPOS (*it) += 1;
5418
5419 consider_string_end:
5420
5421 if (it->current.overlay_string_index >= 0)
5422 {
5423 /* IT->string is an overlay string. Advance to the
5424 next, if there is one. */
5425 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5426 next_overlay_string (it);
5427 }
5428 else
5429 {
5430 /* IT->string is not an overlay string. If we reached
5431 its end, and there is something on IT->stack, proceed
5432 with what is on the stack. This can be either another
5433 string, this time an overlay string, or a buffer. */
5434 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5435 && it->sp > 0)
5436 {
5437 pop_it (it);
5438 if (STRINGP (it->string))
5439 goto consider_string_end;
5440 it->method = GET_FROM_BUFFER;
5441 }
5442 }
5443 break;
5444
5445 case GET_FROM_IMAGE:
5446 case GET_FROM_STRETCH:
5447 /* The position etc with which we have to proceed are on
5448 the stack. The position may be at the end of a string,
5449 if the `display' property takes up the whole string. */
5450 xassert (it->sp > 0);
5451 pop_it (it);
5452 it->image_id = 0;
5453 if (STRINGP (it->string))
5454 {
5455 it->method = GET_FROM_STRING;
5456 goto consider_string_end;
5457 }
5458 it->method = GET_FROM_BUFFER;
5459 break;
5460
5461 default:
5462 /* There are no other methods defined, so this should be a bug. */
5463 abort ();
5464 }
5465
5466 xassert (it->method != GET_FROM_STRING
5467 || (STRINGP (it->string)
5468 && IT_STRING_CHARPOS (*it) >= 0));
5469 }
5470
5471 /* Load IT's display element fields with information about the next
5472 display element which comes from a display table entry or from the
5473 result of translating a control character to one of the forms `^C'
5474 or `\003'.
5475
5476 IT->dpvec holds the glyphs to return as characters.
5477 IT->saved_face_id holds the face id before the display vector--
5478 it is restored into IT->face_idin set_iterator_to_next. */
5479
5480 static int
5481 next_element_from_display_vector (it)
5482 struct it *it;
5483 {
5484 /* Precondition. */
5485 xassert (it->dpvec && it->current.dpvec_index >= 0);
5486
5487 it->face_id = it->saved_face_id;
5488
5489 if (INTEGERP (*it->dpvec)
5490 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5491 {
5492 GLYPH g;
5493
5494 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5495 it->c = FAST_GLYPH_CHAR (g);
5496 it->len = CHAR_BYTES (it->c);
5497
5498 /* The entry may contain a face id to use. Such a face id is
5499 the id of a Lisp face, not a realized face. A face id of
5500 zero means no face is specified. */
5501 if (it->dpvec_face_id >= 0)
5502 it->face_id = it->dpvec_face_id;
5503 else
5504 {
5505 int lface_id = FAST_GLYPH_FACE (g);
5506 if (lface_id > 0)
5507 it->face_id = merge_faces (it->f, Qt, lface_id,
5508 it->saved_face_id);
5509 }
5510 }
5511 else
5512 /* Display table entry is invalid. Return a space. */
5513 it->c = ' ', it->len = 1;
5514
5515 /* Don't change position and object of the iterator here. They are
5516 still the values of the character that had this display table
5517 entry or was translated, and that's what we want. */
5518 it->what = IT_CHARACTER;
5519 return 1;
5520 }
5521
5522
5523 /* Load IT with the next display element from Lisp string IT->string.
5524 IT->current.string_pos is the current position within the string.
5525 If IT->current.overlay_string_index >= 0, the Lisp string is an
5526 overlay string. */
5527
5528 static int
5529 next_element_from_string (it)
5530 struct it *it;
5531 {
5532 struct text_pos position;
5533
5534 xassert (STRINGP (it->string));
5535 xassert (IT_STRING_CHARPOS (*it) >= 0);
5536 position = it->current.string_pos;
5537
5538 /* Time to check for invisible text? */
5539 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5540 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5541 {
5542 handle_stop (it);
5543
5544 /* Since a handler may have changed IT->method, we must
5545 recurse here. */
5546 return get_next_display_element (it);
5547 }
5548
5549 if (it->current.overlay_string_index >= 0)
5550 {
5551 /* Get the next character from an overlay string. In overlay
5552 strings, There is no field width or padding with spaces to
5553 do. */
5554 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5555 {
5556 it->what = IT_EOB;
5557 return 0;
5558 }
5559 else if (STRING_MULTIBYTE (it->string))
5560 {
5561 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5562 const unsigned char *s = (SDATA (it->string)
5563 + IT_STRING_BYTEPOS (*it));
5564 it->c = string_char_and_length (s, remaining, &it->len);
5565 }
5566 else
5567 {
5568 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5569 it->len = 1;
5570 }
5571 }
5572 else
5573 {
5574 /* Get the next character from a Lisp string that is not an
5575 overlay string. Such strings come from the mode line, for
5576 example. We may have to pad with spaces, or truncate the
5577 string. See also next_element_from_c_string. */
5578 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5579 {
5580 it->what = IT_EOB;
5581 return 0;
5582 }
5583 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5584 {
5585 /* Pad with spaces. */
5586 it->c = ' ', it->len = 1;
5587 CHARPOS (position) = BYTEPOS (position) = -1;
5588 }
5589 else if (STRING_MULTIBYTE (it->string))
5590 {
5591 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5592 const unsigned char *s = (SDATA (it->string)
5593 + IT_STRING_BYTEPOS (*it));
5594 it->c = string_char_and_length (s, maxlen, &it->len);
5595 }
5596 else
5597 {
5598 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5599 it->len = 1;
5600 }
5601 }
5602
5603 /* Record what we have and where it came from. Note that we store a
5604 buffer position in IT->position although it could arguably be a
5605 string position. */
5606 it->what = IT_CHARACTER;
5607 it->object = it->string;
5608 it->position = position;
5609 return 1;
5610 }
5611
5612
5613 /* Load IT with next display element from C string IT->s.
5614 IT->string_nchars is the maximum number of characters to return
5615 from the string. IT->end_charpos may be greater than
5616 IT->string_nchars when this function is called, in which case we
5617 may have to return padding spaces. Value is zero if end of string
5618 reached, including padding spaces. */
5619
5620 static int
5621 next_element_from_c_string (it)
5622 struct it *it;
5623 {
5624 int success_p = 1;
5625
5626 xassert (it->s);
5627 it->what = IT_CHARACTER;
5628 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5629 it->object = Qnil;
5630
5631 /* IT's position can be greater IT->string_nchars in case a field
5632 width or precision has been specified when the iterator was
5633 initialized. */
5634 if (IT_CHARPOS (*it) >= it->end_charpos)
5635 {
5636 /* End of the game. */
5637 it->what = IT_EOB;
5638 success_p = 0;
5639 }
5640 else if (IT_CHARPOS (*it) >= it->string_nchars)
5641 {
5642 /* Pad with spaces. */
5643 it->c = ' ', it->len = 1;
5644 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5645 }
5646 else if (it->multibyte_p)
5647 {
5648 /* Implementation note: The calls to strlen apparently aren't a
5649 performance problem because there is no noticeable performance
5650 difference between Emacs running in unibyte or multibyte mode. */
5651 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5652 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5653 maxlen, &it->len);
5654 }
5655 else
5656 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5657
5658 return success_p;
5659 }
5660
5661
5662 /* Set up IT to return characters from an ellipsis, if appropriate.
5663 The definition of the ellipsis glyphs may come from a display table
5664 entry. This function Fills IT with the first glyph from the
5665 ellipsis if an ellipsis is to be displayed. */
5666
5667 static int
5668 next_element_from_ellipsis (it)
5669 struct it *it;
5670 {
5671 if (it->selective_display_ellipsis_p)
5672 setup_for_ellipsis (it, it->len);
5673 else
5674 {
5675 /* The face at the current position may be different from the
5676 face we find after the invisible text. Remember what it
5677 was in IT->saved_face_id, and signal that it's there by
5678 setting face_before_selective_p. */
5679 it->saved_face_id = it->face_id;
5680 it->method = GET_FROM_BUFFER;
5681 reseat_at_next_visible_line_start (it, 1);
5682 it->face_before_selective_p = 1;
5683 }
5684
5685 return get_next_display_element (it);
5686 }
5687
5688
5689 /* Deliver an image display element. The iterator IT is already
5690 filled with image information (done in handle_display_prop). Value
5691 is always 1. */
5692
5693
5694 static int
5695 next_element_from_image (it)
5696 struct it *it;
5697 {
5698 it->what = IT_IMAGE;
5699 return 1;
5700 }
5701
5702
5703 /* Fill iterator IT with next display element from a stretch glyph
5704 property. IT->object is the value of the text property. Value is
5705 always 1. */
5706
5707 static int
5708 next_element_from_stretch (it)
5709 struct it *it;
5710 {
5711 it->what = IT_STRETCH;
5712 return 1;
5713 }
5714
5715
5716 /* Load IT with the next display element from current_buffer. Value
5717 is zero if end of buffer reached. IT->stop_charpos is the next
5718 position at which to stop and check for text properties or buffer
5719 end. */
5720
5721 static int
5722 next_element_from_buffer (it)
5723 struct it *it;
5724 {
5725 int success_p = 1;
5726
5727 /* Check this assumption, otherwise, we would never enter the
5728 if-statement, below. */
5729 xassert (IT_CHARPOS (*it) >= BEGV
5730 && IT_CHARPOS (*it) <= it->stop_charpos);
5731
5732 if (IT_CHARPOS (*it) >= it->stop_charpos)
5733 {
5734 if (IT_CHARPOS (*it) >= it->end_charpos)
5735 {
5736 int overlay_strings_follow_p;
5737
5738 /* End of the game, except when overlay strings follow that
5739 haven't been returned yet. */
5740 if (it->overlay_strings_at_end_processed_p)
5741 overlay_strings_follow_p = 0;
5742 else
5743 {
5744 it->overlay_strings_at_end_processed_p = 1;
5745 overlay_strings_follow_p = get_overlay_strings (it, 0);
5746 }
5747
5748 if (overlay_strings_follow_p)
5749 success_p = get_next_display_element (it);
5750 else
5751 {
5752 it->what = IT_EOB;
5753 it->position = it->current.pos;
5754 success_p = 0;
5755 }
5756 }
5757 else
5758 {
5759 handle_stop (it);
5760 return get_next_display_element (it);
5761 }
5762 }
5763 else
5764 {
5765 /* No face changes, overlays etc. in sight, so just return a
5766 character from current_buffer. */
5767 unsigned char *p;
5768
5769 /* Maybe run the redisplay end trigger hook. Performance note:
5770 This doesn't seem to cost measurable time. */
5771 if (it->redisplay_end_trigger_charpos
5772 && it->glyph_row
5773 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5774 run_redisplay_end_trigger_hook (it);
5775
5776 /* Get the next character, maybe multibyte. */
5777 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5778 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5779 {
5780 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5781 - IT_BYTEPOS (*it));
5782 it->c = string_char_and_length (p, maxlen, &it->len);
5783 }
5784 else
5785 it->c = *p, it->len = 1;
5786
5787 /* Record what we have and where it came from. */
5788 it->what = IT_CHARACTER;;
5789 it->object = it->w->buffer;
5790 it->position = it->current.pos;
5791
5792 /* Normally we return the character found above, except when we
5793 really want to return an ellipsis for selective display. */
5794 if (it->selective)
5795 {
5796 if (it->c == '\n')
5797 {
5798 /* A value of selective > 0 means hide lines indented more
5799 than that number of columns. */
5800 if (it->selective > 0
5801 && IT_CHARPOS (*it) + 1 < ZV
5802 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5803 IT_BYTEPOS (*it) + 1,
5804 (double) it->selective)) /* iftc */
5805 {
5806 success_p = next_element_from_ellipsis (it);
5807 it->dpvec_char_len = -1;
5808 }
5809 }
5810 else if (it->c == '\r' && it->selective == -1)
5811 {
5812 /* A value of selective == -1 means that everything from the
5813 CR to the end of the line is invisible, with maybe an
5814 ellipsis displayed for it. */
5815 success_p = next_element_from_ellipsis (it);
5816 it->dpvec_char_len = -1;
5817 }
5818 }
5819 }
5820
5821 /* Value is zero if end of buffer reached. */
5822 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5823 return success_p;
5824 }
5825
5826
5827 /* Run the redisplay end trigger hook for IT. */
5828
5829 static void
5830 run_redisplay_end_trigger_hook (it)
5831 struct it *it;
5832 {
5833 Lisp_Object args[3];
5834
5835 /* IT->glyph_row should be non-null, i.e. we should be actually
5836 displaying something, or otherwise we should not run the hook. */
5837 xassert (it->glyph_row);
5838
5839 /* Set up hook arguments. */
5840 args[0] = Qredisplay_end_trigger_functions;
5841 args[1] = it->window;
5842 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5843 it->redisplay_end_trigger_charpos = 0;
5844
5845 /* Since we are *trying* to run these functions, don't try to run
5846 them again, even if they get an error. */
5847 it->w->redisplay_end_trigger = Qnil;
5848 Frun_hook_with_args (3, args);
5849
5850 /* Notice if it changed the face of the character we are on. */
5851 handle_face_prop (it);
5852 }
5853
5854
5855 /* Deliver a composition display element. The iterator IT is already
5856 filled with composition information (done in
5857 handle_composition_prop). Value is always 1. */
5858
5859 static int
5860 next_element_from_composition (it)
5861 struct it *it;
5862 {
5863 it->what = IT_COMPOSITION;
5864 it->position = (STRINGP (it->string)
5865 ? it->current.string_pos
5866 : it->current.pos);
5867 return 1;
5868 }
5869
5870
5871 \f
5872 /***********************************************************************
5873 Moving an iterator without producing glyphs
5874 ***********************************************************************/
5875
5876 /* Check if iterator is at a position corresponding to a valid buffer
5877 position after some move_it_ call. */
5878
5879 #define IT_POS_VALID_AFTER_MOVE_P(it) \
5880 ((it)->method == GET_FROM_STRING \
5881 ? IT_STRING_CHARPOS (*it) == 0 \
5882 : 1)
5883
5884
5885 /* Move iterator IT to a specified buffer or X position within one
5886 line on the display without producing glyphs.
5887
5888 OP should be a bit mask including some or all of these bits:
5889 MOVE_TO_X: Stop on reaching x-position TO_X.
5890 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5891 Regardless of OP's value, stop in reaching the end of the display line.
5892
5893 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5894 This means, in particular, that TO_X includes window's horizontal
5895 scroll amount.
5896
5897 The return value has several possible values that
5898 say what condition caused the scan to stop:
5899
5900 MOVE_POS_MATCH_OR_ZV
5901 - when TO_POS or ZV was reached.
5902
5903 MOVE_X_REACHED
5904 -when TO_X was reached before TO_POS or ZV were reached.
5905
5906 MOVE_LINE_CONTINUED
5907 - when we reached the end of the display area and the line must
5908 be continued.
5909
5910 MOVE_LINE_TRUNCATED
5911 - when we reached the end of the display area and the line is
5912 truncated.
5913
5914 MOVE_NEWLINE_OR_CR
5915 - when we stopped at a line end, i.e. a newline or a CR and selective
5916 display is on. */
5917
5918 static enum move_it_result
5919 move_it_in_display_line_to (it, to_charpos, to_x, op)
5920 struct it *it;
5921 int to_charpos, to_x, op;
5922 {
5923 enum move_it_result result = MOVE_UNDEFINED;
5924 struct glyph_row *saved_glyph_row;
5925
5926 /* Don't produce glyphs in produce_glyphs. */
5927 saved_glyph_row = it->glyph_row;
5928 it->glyph_row = NULL;
5929
5930 #define BUFFER_POS_REACHED_P() \
5931 ((op & MOVE_TO_POS) != 0 \
5932 && BUFFERP (it->object) \
5933 && IT_CHARPOS (*it) >= to_charpos \
5934 && (it->method == GET_FROM_BUFFER \
5935 || (it->method == GET_FROM_DISPLAY_VECTOR \
5936 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5937
5938
5939 while (1)
5940 {
5941 int x, i, ascent = 0, descent = 0;
5942
5943 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5944 if ((op & MOVE_TO_POS) != 0
5945 && BUFFERP (it->object)
5946 && it->method == GET_FROM_BUFFER
5947 && IT_CHARPOS (*it) > to_charpos)
5948 {
5949 result = MOVE_POS_MATCH_OR_ZV;
5950 break;
5951 }
5952
5953 /* Stop when ZV reached.
5954 We used to stop here when TO_CHARPOS reached as well, but that is
5955 too soon if this glyph does not fit on this line. So we handle it
5956 explicitly below. */
5957 if (!get_next_display_element (it)
5958 || (it->truncate_lines_p
5959 && BUFFER_POS_REACHED_P ()))
5960 {
5961 result = MOVE_POS_MATCH_OR_ZV;
5962 break;
5963 }
5964
5965 /* The call to produce_glyphs will get the metrics of the
5966 display element IT is loaded with. We record in x the
5967 x-position before this display element in case it does not
5968 fit on the line. */
5969 x = it->current_x;
5970
5971 /* Remember the line height so far in case the next element doesn't
5972 fit on the line. */
5973 if (!it->truncate_lines_p)
5974 {
5975 ascent = it->max_ascent;
5976 descent = it->max_descent;
5977 }
5978
5979 PRODUCE_GLYPHS (it);
5980
5981 if (it->area != TEXT_AREA)
5982 {
5983 set_iterator_to_next (it, 1);
5984 continue;
5985 }
5986
5987 /* The number of glyphs we get back in IT->nglyphs will normally
5988 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5989 character on a terminal frame, or (iii) a line end. For the
5990 second case, IT->nglyphs - 1 padding glyphs will be present
5991 (on X frames, there is only one glyph produced for a
5992 composite character.
5993
5994 The behavior implemented below means, for continuation lines,
5995 that as many spaces of a TAB as fit on the current line are
5996 displayed there. For terminal frames, as many glyphs of a
5997 multi-glyph character are displayed in the current line, too.
5998 This is what the old redisplay code did, and we keep it that
5999 way. Under X, the whole shape of a complex character must
6000 fit on the line or it will be completely displayed in the
6001 next line.
6002
6003 Note that both for tabs and padding glyphs, all glyphs have
6004 the same width. */
6005 if (it->nglyphs)
6006 {
6007 /* More than one glyph or glyph doesn't fit on line. All
6008 glyphs have the same width. */
6009 int single_glyph_width = it->pixel_width / it->nglyphs;
6010 int new_x;
6011
6012 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6013 {
6014 new_x = x + single_glyph_width;
6015
6016 /* We want to leave anything reaching TO_X to the caller. */
6017 if ((op & MOVE_TO_X) && new_x > to_x)
6018 {
6019 if (BUFFER_POS_REACHED_P ())
6020 goto buffer_pos_reached;
6021 it->current_x = x;
6022 result = MOVE_X_REACHED;
6023 break;
6024 }
6025 else if (/* Lines are continued. */
6026 !it->truncate_lines_p
6027 && (/* And glyph doesn't fit on the line. */
6028 new_x > it->last_visible_x
6029 /* Or it fits exactly and we're on a window
6030 system frame. */
6031 || (new_x == it->last_visible_x
6032 && FRAME_WINDOW_P (it->f))))
6033 {
6034 if (/* IT->hpos == 0 means the very first glyph
6035 doesn't fit on the line, e.g. a wide image. */
6036 it->hpos == 0
6037 || (new_x == it->last_visible_x
6038 && FRAME_WINDOW_P (it->f)))
6039 {
6040 ++it->hpos;
6041 it->current_x = new_x;
6042 if (i == it->nglyphs - 1)
6043 {
6044 set_iterator_to_next (it, 1);
6045 #ifdef HAVE_WINDOW_SYSTEM
6046 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6047 {
6048 if (!get_next_display_element (it))
6049 {
6050 result = MOVE_POS_MATCH_OR_ZV;
6051 break;
6052 }
6053 if (BUFFER_POS_REACHED_P ())
6054 {
6055 if (ITERATOR_AT_END_OF_LINE_P (it))
6056 result = MOVE_POS_MATCH_OR_ZV;
6057 else
6058 result = MOVE_LINE_CONTINUED;
6059 break;
6060 }
6061 if (ITERATOR_AT_END_OF_LINE_P (it))
6062 {
6063 result = MOVE_NEWLINE_OR_CR;
6064 break;
6065 }
6066 }
6067 #endif /* HAVE_WINDOW_SYSTEM */
6068 }
6069 }
6070 else
6071 {
6072 it->current_x = x;
6073 it->max_ascent = ascent;
6074 it->max_descent = descent;
6075 }
6076
6077 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6078 IT_CHARPOS (*it)));
6079 result = MOVE_LINE_CONTINUED;
6080 break;
6081 }
6082 else if (BUFFER_POS_REACHED_P ())
6083 goto buffer_pos_reached;
6084 else if (new_x > it->first_visible_x)
6085 {
6086 /* Glyph is visible. Increment number of glyphs that
6087 would be displayed. */
6088 ++it->hpos;
6089 }
6090 else
6091 {
6092 /* Glyph is completely off the left margin of the display
6093 area. Nothing to do. */
6094 }
6095 }
6096
6097 if (result != MOVE_UNDEFINED)
6098 break;
6099 }
6100 else if (BUFFER_POS_REACHED_P ())
6101 {
6102 buffer_pos_reached:
6103 it->current_x = x;
6104 it->max_ascent = ascent;
6105 it->max_descent = descent;
6106 result = MOVE_POS_MATCH_OR_ZV;
6107 break;
6108 }
6109 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6110 {
6111 /* Stop when TO_X specified and reached. This check is
6112 necessary here because of lines consisting of a line end,
6113 only. The line end will not produce any glyphs and we
6114 would never get MOVE_X_REACHED. */
6115 xassert (it->nglyphs == 0);
6116 result = MOVE_X_REACHED;
6117 break;
6118 }
6119
6120 /* Is this a line end? If yes, we're done. */
6121 if (ITERATOR_AT_END_OF_LINE_P (it))
6122 {
6123 result = MOVE_NEWLINE_OR_CR;
6124 break;
6125 }
6126
6127 /* The current display element has been consumed. Advance
6128 to the next. */
6129 set_iterator_to_next (it, 1);
6130
6131 /* Stop if lines are truncated and IT's current x-position is
6132 past the right edge of the window now. */
6133 if (it->truncate_lines_p
6134 && it->current_x >= it->last_visible_x)
6135 {
6136 #ifdef HAVE_WINDOW_SYSTEM
6137 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6138 {
6139 if (!get_next_display_element (it)
6140 || BUFFER_POS_REACHED_P ())
6141 {
6142 result = MOVE_POS_MATCH_OR_ZV;
6143 break;
6144 }
6145 if (ITERATOR_AT_END_OF_LINE_P (it))
6146 {
6147 result = MOVE_NEWLINE_OR_CR;
6148 break;
6149 }
6150 }
6151 #endif /* HAVE_WINDOW_SYSTEM */
6152 result = MOVE_LINE_TRUNCATED;
6153 break;
6154 }
6155 }
6156
6157 #undef BUFFER_POS_REACHED_P
6158
6159 /* Restore the iterator settings altered at the beginning of this
6160 function. */
6161 it->glyph_row = saved_glyph_row;
6162 return result;
6163 }
6164
6165
6166 /* Move IT forward until it satisfies one or more of the criteria in
6167 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6168
6169 OP is a bit-mask that specifies where to stop, and in particular,
6170 which of those four position arguments makes a difference. See the
6171 description of enum move_operation_enum.
6172
6173 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6174 screen line, this function will set IT to the next position >
6175 TO_CHARPOS. */
6176
6177 void
6178 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6179 struct it *it;
6180 int to_charpos, to_x, to_y, to_vpos;
6181 int op;
6182 {
6183 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6184 int line_height;
6185 int reached = 0;
6186
6187 for (;;)
6188 {
6189 if (op & MOVE_TO_VPOS)
6190 {
6191 /* If no TO_CHARPOS and no TO_X specified, stop at the
6192 start of the line TO_VPOS. */
6193 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6194 {
6195 if (it->vpos == to_vpos)
6196 {
6197 reached = 1;
6198 break;
6199 }
6200 else
6201 skip = move_it_in_display_line_to (it, -1, -1, 0);
6202 }
6203 else
6204 {
6205 /* TO_VPOS >= 0 means stop at TO_X in the line at
6206 TO_VPOS, or at TO_POS, whichever comes first. */
6207 if (it->vpos == to_vpos)
6208 {
6209 reached = 2;
6210 break;
6211 }
6212
6213 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6214
6215 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6216 {
6217 reached = 3;
6218 break;
6219 }
6220 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6221 {
6222 /* We have reached TO_X but not in the line we want. */
6223 skip = move_it_in_display_line_to (it, to_charpos,
6224 -1, MOVE_TO_POS);
6225 if (skip == MOVE_POS_MATCH_OR_ZV)
6226 {
6227 reached = 4;
6228 break;
6229 }
6230 }
6231 }
6232 }
6233 else if (op & MOVE_TO_Y)
6234 {
6235 struct it it_backup;
6236
6237 /* TO_Y specified means stop at TO_X in the line containing
6238 TO_Y---or at TO_CHARPOS if this is reached first. The
6239 problem is that we can't really tell whether the line
6240 contains TO_Y before we have completely scanned it, and
6241 this may skip past TO_X. What we do is to first scan to
6242 TO_X.
6243
6244 If TO_X is not specified, use a TO_X of zero. The reason
6245 is to make the outcome of this function more predictable.
6246 If we didn't use TO_X == 0, we would stop at the end of
6247 the line which is probably not what a caller would expect
6248 to happen. */
6249 skip = move_it_in_display_line_to (it, to_charpos,
6250 ((op & MOVE_TO_X)
6251 ? to_x : 0),
6252 (MOVE_TO_X
6253 | (op & MOVE_TO_POS)));
6254
6255 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6256 if (skip == MOVE_POS_MATCH_OR_ZV)
6257 {
6258 reached = 5;
6259 break;
6260 }
6261
6262 /* If TO_X was reached, we would like to know whether TO_Y
6263 is in the line. This can only be said if we know the
6264 total line height which requires us to scan the rest of
6265 the line. */
6266 if (skip == MOVE_X_REACHED)
6267 {
6268 it_backup = *it;
6269 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6270 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6271 op & MOVE_TO_POS);
6272 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6273 }
6274
6275 /* Now, decide whether TO_Y is in this line. */
6276 line_height = it->max_ascent + it->max_descent;
6277 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6278
6279 if (to_y >= it->current_y
6280 && to_y < it->current_y + line_height)
6281 {
6282 if (skip == MOVE_X_REACHED)
6283 /* If TO_Y is in this line and TO_X was reached above,
6284 we scanned too far. We have to restore IT's settings
6285 to the ones before skipping. */
6286 *it = it_backup;
6287 reached = 6;
6288 }
6289 else if (skip == MOVE_X_REACHED)
6290 {
6291 skip = skip2;
6292 if (skip == MOVE_POS_MATCH_OR_ZV)
6293 reached = 7;
6294 }
6295
6296 if (reached)
6297 break;
6298 }
6299 else
6300 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6301
6302 switch (skip)
6303 {
6304 case MOVE_POS_MATCH_OR_ZV:
6305 reached = 8;
6306 goto out;
6307
6308 case MOVE_NEWLINE_OR_CR:
6309 set_iterator_to_next (it, 1);
6310 it->continuation_lines_width = 0;
6311 break;
6312
6313 case MOVE_LINE_TRUNCATED:
6314 it->continuation_lines_width = 0;
6315 reseat_at_next_visible_line_start (it, 0);
6316 if ((op & MOVE_TO_POS) != 0
6317 && IT_CHARPOS (*it) > to_charpos)
6318 {
6319 reached = 9;
6320 goto out;
6321 }
6322 break;
6323
6324 case MOVE_LINE_CONTINUED:
6325 it->continuation_lines_width += it->current_x;
6326 break;
6327
6328 default:
6329 abort ();
6330 }
6331
6332 /* Reset/increment for the next run. */
6333 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6334 it->current_x = it->hpos = 0;
6335 it->current_y += it->max_ascent + it->max_descent;
6336 ++it->vpos;
6337 last_height = it->max_ascent + it->max_descent;
6338 last_max_ascent = it->max_ascent;
6339 it->max_ascent = it->max_descent = 0;
6340 }
6341
6342 out:
6343
6344 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6345 }
6346
6347
6348 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6349
6350 If DY > 0, move IT backward at least that many pixels. DY = 0
6351 means move IT backward to the preceding line start or BEGV. This
6352 function may move over more than DY pixels if IT->current_y - DY
6353 ends up in the middle of a line; in this case IT->current_y will be
6354 set to the top of the line moved to. */
6355
6356 void
6357 move_it_vertically_backward (it, dy)
6358 struct it *it;
6359 int dy;
6360 {
6361 int nlines, h;
6362 struct it it2, it3;
6363 int start_pos;
6364
6365 move_further_back:
6366 xassert (dy >= 0);
6367
6368 start_pos = IT_CHARPOS (*it);
6369
6370 /* Estimate how many newlines we must move back. */
6371 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6372
6373 /* Set the iterator's position that many lines back. */
6374 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6375 back_to_previous_visible_line_start (it);
6376
6377 /* Reseat the iterator here. When moving backward, we don't want
6378 reseat to skip forward over invisible text, set up the iterator
6379 to deliver from overlay strings at the new position etc. So,
6380 use reseat_1 here. */
6381 reseat_1 (it, it->current.pos, 1);
6382
6383 /* We are now surely at a line start. */
6384 it->current_x = it->hpos = 0;
6385 it->continuation_lines_width = 0;
6386
6387 /* Move forward and see what y-distance we moved. First move to the
6388 start of the next line so that we get its height. We need this
6389 height to be able to tell whether we reached the specified
6390 y-distance. */
6391 it2 = *it;
6392 it2.max_ascent = it2.max_descent = 0;
6393 do
6394 {
6395 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6396 MOVE_TO_POS | MOVE_TO_VPOS);
6397 }
6398 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6399 xassert (IT_CHARPOS (*it) >= BEGV);
6400 it3 = it2;
6401
6402 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6403 xassert (IT_CHARPOS (*it) >= BEGV);
6404 /* H is the actual vertical distance from the position in *IT
6405 and the starting position. */
6406 h = it2.current_y - it->current_y;
6407 /* NLINES is the distance in number of lines. */
6408 nlines = it2.vpos - it->vpos;
6409
6410 /* Correct IT's y and vpos position
6411 so that they are relative to the starting point. */
6412 it->vpos -= nlines;
6413 it->current_y -= h;
6414
6415 if (dy == 0)
6416 {
6417 /* DY == 0 means move to the start of the screen line. The
6418 value of nlines is > 0 if continuation lines were involved. */
6419 if (nlines > 0)
6420 move_it_by_lines (it, nlines, 1);
6421 #if 0
6422 /* I think this assert is bogus if buffer contains
6423 invisible text or images. KFS. */
6424 xassert (IT_CHARPOS (*it) <= start_pos);
6425 #endif
6426 }
6427 else
6428 {
6429 /* The y-position we try to reach, relative to *IT.
6430 Note that H has been subtracted in front of the if-statement. */
6431 int target_y = it->current_y + h - dy;
6432 int y0 = it3.current_y;
6433 int y1 = line_bottom_y (&it3);
6434 int line_height = y1 - y0;
6435
6436 /* If we did not reach target_y, try to move further backward if
6437 we can. If we moved too far backward, try to move forward. */
6438 if (target_y < it->current_y
6439 /* This is heuristic. In a window that's 3 lines high, with
6440 a line height of 13 pixels each, recentering with point
6441 on the bottom line will try to move -39/2 = 19 pixels
6442 backward. Try to avoid moving into the first line. */
6443 && (it->current_y - target_y
6444 > min (window_box_height (it->w), line_height * 2 / 3))
6445 && IT_CHARPOS (*it) > BEGV)
6446 {
6447 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6448 target_y - it->current_y));
6449 dy = it->current_y - target_y;
6450 goto move_further_back;
6451 }
6452 else if (target_y >= it->current_y + line_height
6453 && IT_CHARPOS (*it) < ZV)
6454 {
6455 /* Should move forward by at least one line, maybe more.
6456
6457 Note: Calling move_it_by_lines can be expensive on
6458 terminal frames, where compute_motion is used (via
6459 vmotion) to do the job, when there are very long lines
6460 and truncate-lines is nil. That's the reason for
6461 treating terminal frames specially here. */
6462
6463 if (!FRAME_WINDOW_P (it->f))
6464 move_it_vertically (it, target_y - (it->current_y + line_height));
6465 else
6466 {
6467 do
6468 {
6469 move_it_by_lines (it, 1, 1);
6470 }
6471 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6472 }
6473
6474 #if 0
6475 /* I think this assert is bogus if buffer contains
6476 invisible text or images. KFS. */
6477 xassert (IT_CHARPOS (*it) >= BEGV);
6478 #endif
6479 }
6480 }
6481 }
6482
6483
6484 /* Move IT by a specified amount of pixel lines DY. DY negative means
6485 move backwards. DY = 0 means move to start of screen line. At the
6486 end, IT will be on the start of a screen line. */
6487
6488 void
6489 move_it_vertically (it, dy)
6490 struct it *it;
6491 int dy;
6492 {
6493 if (dy <= 0)
6494 move_it_vertically_backward (it, -dy);
6495 else
6496 {
6497 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6498 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6499 MOVE_TO_POS | MOVE_TO_Y);
6500 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6501
6502 /* If buffer ends in ZV without a newline, move to the start of
6503 the line to satisfy the post-condition. */
6504 if (IT_CHARPOS (*it) == ZV
6505 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6506 move_it_by_lines (it, 0, 0);
6507 }
6508 }
6509
6510
6511 /* Move iterator IT past the end of the text line it is in. */
6512
6513 void
6514 move_it_past_eol (it)
6515 struct it *it;
6516 {
6517 enum move_it_result rc;
6518
6519 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6520 if (rc == MOVE_NEWLINE_OR_CR)
6521 set_iterator_to_next (it, 0);
6522 }
6523
6524
6525 #if 0 /* Currently not used. */
6526
6527 /* Return non-zero if some text between buffer positions START_CHARPOS
6528 and END_CHARPOS is invisible. IT->window is the window for text
6529 property lookup. */
6530
6531 static int
6532 invisible_text_between_p (it, start_charpos, end_charpos)
6533 struct it *it;
6534 int start_charpos, end_charpos;
6535 {
6536 Lisp_Object prop, limit;
6537 int invisible_found_p;
6538
6539 xassert (it != NULL && start_charpos <= end_charpos);
6540
6541 /* Is text at START invisible? */
6542 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6543 it->window);
6544 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6545 invisible_found_p = 1;
6546 else
6547 {
6548 limit = Fnext_single_char_property_change (make_number (start_charpos),
6549 Qinvisible, Qnil,
6550 make_number (end_charpos));
6551 invisible_found_p = XFASTINT (limit) < end_charpos;
6552 }
6553
6554 return invisible_found_p;
6555 }
6556
6557 #endif /* 0 */
6558
6559
6560 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6561 negative means move up. DVPOS == 0 means move to the start of the
6562 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6563 NEED_Y_P is zero, IT->current_y will be left unchanged.
6564
6565 Further optimization ideas: If we would know that IT->f doesn't use
6566 a face with proportional font, we could be faster for
6567 truncate-lines nil. */
6568
6569 void
6570 move_it_by_lines (it, dvpos, need_y_p)
6571 struct it *it;
6572 int dvpos, need_y_p;
6573 {
6574 struct position pos;
6575
6576 if (!FRAME_WINDOW_P (it->f))
6577 {
6578 struct text_pos textpos;
6579
6580 /* We can use vmotion on frames without proportional fonts. */
6581 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6582 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6583 reseat (it, textpos, 1);
6584 it->vpos += pos.vpos;
6585 it->current_y += pos.vpos;
6586 }
6587 else if (dvpos == 0)
6588 {
6589 /* DVPOS == 0 means move to the start of the screen line. */
6590 move_it_vertically_backward (it, 0);
6591 xassert (it->current_x == 0 && it->hpos == 0);
6592 /* Let next call to line_bottom_y calculate real line height */
6593 last_height = 0;
6594 }
6595 else if (dvpos > 0)
6596 {
6597 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6598 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6599 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6600 }
6601 else
6602 {
6603 struct it it2;
6604 int start_charpos, i;
6605
6606 /* Start at the beginning of the screen line containing IT's
6607 position. This may actually move vertically backwards,
6608 in case of overlays, so adjust dvpos accordingly. */
6609 dvpos += it->vpos;
6610 move_it_vertically_backward (it, 0);
6611 dvpos -= it->vpos;
6612
6613 /* Go back -DVPOS visible lines and reseat the iterator there. */
6614 start_charpos = IT_CHARPOS (*it);
6615 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6616 back_to_previous_visible_line_start (it);
6617 reseat (it, it->current.pos, 1);
6618
6619 /* Move further back if we end up in a string or an image. */
6620 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6621 {
6622 /* First try to move to start of display line. */
6623 dvpos += it->vpos;
6624 move_it_vertically_backward (it, 0);
6625 dvpos -= it->vpos;
6626 if (IT_POS_VALID_AFTER_MOVE_P (it))
6627 break;
6628 /* If start of line is still in string or image,
6629 move further back. */
6630 back_to_previous_visible_line_start (it);
6631 reseat (it, it->current.pos, 1);
6632 dvpos--;
6633 }
6634
6635 it->current_x = it->hpos = 0;
6636
6637 /* Above call may have moved too far if continuation lines
6638 are involved. Scan forward and see if it did. */
6639 it2 = *it;
6640 it2.vpos = it2.current_y = 0;
6641 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6642 it->vpos -= it2.vpos;
6643 it->current_y -= it2.current_y;
6644 it->current_x = it->hpos = 0;
6645
6646 /* If we moved too far back, move IT some lines forward. */
6647 if (it2.vpos > -dvpos)
6648 {
6649 int delta = it2.vpos + dvpos;
6650 it2 = *it;
6651 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6652 /* Move back again if we got too far ahead. */
6653 if (IT_CHARPOS (*it) >= start_charpos)
6654 *it = it2;
6655 }
6656 }
6657 }
6658
6659 /* Return 1 if IT points into the middle of a display vector. */
6660
6661 int
6662 in_display_vector_p (it)
6663 struct it *it;
6664 {
6665 return (it->method == GET_FROM_DISPLAY_VECTOR
6666 && it->current.dpvec_index > 0
6667 && it->dpvec + it->current.dpvec_index != it->dpend);
6668 }
6669
6670 \f
6671 /***********************************************************************
6672 Messages
6673 ***********************************************************************/
6674
6675
6676 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6677 to *Messages*. */
6678
6679 void
6680 add_to_log (format, arg1, arg2)
6681 char *format;
6682 Lisp_Object arg1, arg2;
6683 {
6684 Lisp_Object args[3];
6685 Lisp_Object msg, fmt;
6686 char *buffer;
6687 int len;
6688 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6689 USE_SAFE_ALLOCA;
6690
6691 /* Do nothing if called asynchronously. Inserting text into
6692 a buffer may call after-change-functions and alike and
6693 that would means running Lisp asynchronously. */
6694 if (handling_signal)
6695 return;
6696
6697 fmt = msg = Qnil;
6698 GCPRO4 (fmt, msg, arg1, arg2);
6699
6700 args[0] = fmt = build_string (format);
6701 args[1] = arg1;
6702 args[2] = arg2;
6703 msg = Fformat (3, args);
6704
6705 len = SBYTES (msg) + 1;
6706 SAFE_ALLOCA (buffer, char *, len);
6707 bcopy (SDATA (msg), buffer, len);
6708
6709 message_dolog (buffer, len - 1, 1, 0);
6710 SAFE_FREE ();
6711
6712 UNGCPRO;
6713 }
6714
6715
6716 /* Output a newline in the *Messages* buffer if "needs" one. */
6717
6718 void
6719 message_log_maybe_newline ()
6720 {
6721 if (message_log_need_newline)
6722 message_dolog ("", 0, 1, 0);
6723 }
6724
6725
6726 /* Add a string M of length NBYTES to the message log, optionally
6727 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6728 nonzero, means interpret the contents of M as multibyte. This
6729 function calls low-level routines in order to bypass text property
6730 hooks, etc. which might not be safe to run. */
6731
6732 void
6733 message_dolog (m, nbytes, nlflag, multibyte)
6734 const char *m;
6735 int nbytes, nlflag, multibyte;
6736 {
6737 if (!NILP (Vmemory_full))
6738 return;
6739
6740 if (!NILP (Vmessage_log_max))
6741 {
6742 struct buffer *oldbuf;
6743 Lisp_Object oldpoint, oldbegv, oldzv;
6744 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6745 int point_at_end = 0;
6746 int zv_at_end = 0;
6747 Lisp_Object old_deactivate_mark, tem;
6748 struct gcpro gcpro1;
6749
6750 old_deactivate_mark = Vdeactivate_mark;
6751 oldbuf = current_buffer;
6752 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6753 current_buffer->undo_list = Qt;
6754
6755 oldpoint = message_dolog_marker1;
6756 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6757 oldbegv = message_dolog_marker2;
6758 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6759 oldzv = message_dolog_marker3;
6760 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6761 GCPRO1 (old_deactivate_mark);
6762
6763 if (PT == Z)
6764 point_at_end = 1;
6765 if (ZV == Z)
6766 zv_at_end = 1;
6767
6768 BEGV = BEG;
6769 BEGV_BYTE = BEG_BYTE;
6770 ZV = Z;
6771 ZV_BYTE = Z_BYTE;
6772 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6773
6774 /* Insert the string--maybe converting multibyte to single byte
6775 or vice versa, so that all the text fits the buffer. */
6776 if (multibyte
6777 && NILP (current_buffer->enable_multibyte_characters))
6778 {
6779 int i, c, char_bytes;
6780 unsigned char work[1];
6781
6782 /* Convert a multibyte string to single-byte
6783 for the *Message* buffer. */
6784 for (i = 0; i < nbytes; i += char_bytes)
6785 {
6786 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6787 work[0] = (SINGLE_BYTE_CHAR_P (c)
6788 ? c
6789 : multibyte_char_to_unibyte (c, Qnil));
6790 insert_1_both (work, 1, 1, 1, 0, 0);
6791 }
6792 }
6793 else if (! multibyte
6794 && ! NILP (current_buffer->enable_multibyte_characters))
6795 {
6796 int i, c, char_bytes;
6797 unsigned char *msg = (unsigned char *) m;
6798 unsigned char str[MAX_MULTIBYTE_LENGTH];
6799 /* Convert a single-byte string to multibyte
6800 for the *Message* buffer. */
6801 for (i = 0; i < nbytes; i++)
6802 {
6803 c = unibyte_char_to_multibyte (msg[i]);
6804 char_bytes = CHAR_STRING (c, str);
6805 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6806 }
6807 }
6808 else if (nbytes)
6809 insert_1 (m, nbytes, 1, 0, 0);
6810
6811 if (nlflag)
6812 {
6813 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6814 insert_1 ("\n", 1, 1, 0, 0);
6815
6816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6817 this_bol = PT;
6818 this_bol_byte = PT_BYTE;
6819
6820 /* See if this line duplicates the previous one.
6821 If so, combine duplicates. */
6822 if (this_bol > BEG)
6823 {
6824 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6825 prev_bol = PT;
6826 prev_bol_byte = PT_BYTE;
6827
6828 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6829 this_bol, this_bol_byte);
6830 if (dup)
6831 {
6832 del_range_both (prev_bol, prev_bol_byte,
6833 this_bol, this_bol_byte, 0);
6834 if (dup > 1)
6835 {
6836 char dupstr[40];
6837 int duplen;
6838
6839 /* If you change this format, don't forget to also
6840 change message_log_check_duplicate. */
6841 sprintf (dupstr, " [%d times]", dup);
6842 duplen = strlen (dupstr);
6843 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6844 insert_1 (dupstr, duplen, 1, 0, 1);
6845 }
6846 }
6847 }
6848
6849 /* If we have more than the desired maximum number of lines
6850 in the *Messages* buffer now, delete the oldest ones.
6851 This is safe because we don't have undo in this buffer. */
6852
6853 if (NATNUMP (Vmessage_log_max))
6854 {
6855 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6856 -XFASTINT (Vmessage_log_max) - 1, 0);
6857 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6858 }
6859 }
6860 BEGV = XMARKER (oldbegv)->charpos;
6861 BEGV_BYTE = marker_byte_position (oldbegv);
6862
6863 if (zv_at_end)
6864 {
6865 ZV = Z;
6866 ZV_BYTE = Z_BYTE;
6867 }
6868 else
6869 {
6870 ZV = XMARKER (oldzv)->charpos;
6871 ZV_BYTE = marker_byte_position (oldzv);
6872 }
6873
6874 if (point_at_end)
6875 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6876 else
6877 /* We can't do Fgoto_char (oldpoint) because it will run some
6878 Lisp code. */
6879 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6880 XMARKER (oldpoint)->bytepos);
6881
6882 UNGCPRO;
6883 unchain_marker (XMARKER (oldpoint));
6884 unchain_marker (XMARKER (oldbegv));
6885 unchain_marker (XMARKER (oldzv));
6886
6887 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6888 set_buffer_internal (oldbuf);
6889 if (NILP (tem))
6890 windows_or_buffers_changed = old_windows_or_buffers_changed;
6891 message_log_need_newline = !nlflag;
6892 Vdeactivate_mark = old_deactivate_mark;
6893 }
6894 }
6895
6896
6897 /* We are at the end of the buffer after just having inserted a newline.
6898 (Note: We depend on the fact we won't be crossing the gap.)
6899 Check to see if the most recent message looks a lot like the previous one.
6900 Return 0 if different, 1 if the new one should just replace it, or a
6901 value N > 1 if we should also append " [N times]". */
6902
6903 static int
6904 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6905 int prev_bol, this_bol;
6906 int prev_bol_byte, this_bol_byte;
6907 {
6908 int i;
6909 int len = Z_BYTE - 1 - this_bol_byte;
6910 int seen_dots = 0;
6911 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6912 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6913
6914 for (i = 0; i < len; i++)
6915 {
6916 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6917 seen_dots = 1;
6918 if (p1[i] != p2[i])
6919 return seen_dots;
6920 }
6921 p1 += len;
6922 if (*p1 == '\n')
6923 return 2;
6924 if (*p1++ == ' ' && *p1++ == '[')
6925 {
6926 int n = 0;
6927 while (*p1 >= '0' && *p1 <= '9')
6928 n = n * 10 + *p1++ - '0';
6929 if (strncmp (p1, " times]\n", 8) == 0)
6930 return n+1;
6931 }
6932 return 0;
6933 }
6934 \f
6935
6936 /* Display an echo area message M with a specified length of NBYTES
6937 bytes. The string may include null characters. If M is 0, clear
6938 out any existing message, and let the mini-buffer text show
6939 through.
6940
6941 The buffer M must continue to exist until after the echo area gets
6942 cleared or some other message gets displayed there. This means do
6943 not pass text that is stored in a Lisp string; do not pass text in
6944 a buffer that was alloca'd. */
6945
6946 void
6947 message2 (m, nbytes, multibyte)
6948 const char *m;
6949 int nbytes;
6950 int multibyte;
6951 {
6952 /* First flush out any partial line written with print. */
6953 message_log_maybe_newline ();
6954 if (m)
6955 message_dolog (m, nbytes, 1, multibyte);
6956 message2_nolog (m, nbytes, multibyte);
6957 }
6958
6959
6960 /* The non-logging counterpart of message2. */
6961
6962 void
6963 message2_nolog (m, nbytes, multibyte)
6964 const char *m;
6965 int nbytes, multibyte;
6966 {
6967 struct frame *sf = SELECTED_FRAME ();
6968 message_enable_multibyte = multibyte;
6969
6970 if (noninteractive)
6971 {
6972 if (noninteractive_need_newline)
6973 putc ('\n', stderr);
6974 noninteractive_need_newline = 0;
6975 if (m)
6976 fwrite (m, nbytes, 1, stderr);
6977 if (cursor_in_echo_area == 0)
6978 fprintf (stderr, "\n");
6979 fflush (stderr);
6980 }
6981 /* A null message buffer means that the frame hasn't really been
6982 initialized yet. Error messages get reported properly by
6983 cmd_error, so this must be just an informative message; toss it. */
6984 else if (INTERACTIVE
6985 && sf->glyphs_initialized_p
6986 && FRAME_MESSAGE_BUF (sf))
6987 {
6988 Lisp_Object mini_window;
6989 struct frame *f;
6990
6991 /* Get the frame containing the mini-buffer
6992 that the selected frame is using. */
6993 mini_window = FRAME_MINIBUF_WINDOW (sf);
6994 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6995
6996 FRAME_SAMPLE_VISIBILITY (f);
6997 if (FRAME_VISIBLE_P (sf)
6998 && ! FRAME_VISIBLE_P (f))
6999 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7000
7001 if (m)
7002 {
7003 set_message (m, Qnil, nbytes, multibyte);
7004 if (minibuffer_auto_raise)
7005 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7006 }
7007 else
7008 clear_message (1, 1);
7009
7010 do_pending_window_change (0);
7011 echo_area_display (1);
7012 do_pending_window_change (0);
7013 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7014 (*frame_up_to_date_hook) (f);
7015 }
7016 }
7017
7018
7019 /* Display an echo area message M with a specified length of NBYTES
7020 bytes. The string may include null characters. If M is not a
7021 string, clear out any existing message, and let the mini-buffer
7022 text show through.
7023
7024 This function cancels echoing. */
7025
7026 void
7027 message3 (m, nbytes, multibyte)
7028 Lisp_Object m;
7029 int nbytes;
7030 int multibyte;
7031 {
7032 struct gcpro gcpro1;
7033
7034 GCPRO1 (m);
7035 clear_message (1,1);
7036 cancel_echoing ();
7037
7038 /* First flush out any partial line written with print. */
7039 message_log_maybe_newline ();
7040 if (STRINGP (m))
7041 message_dolog (SDATA (m), nbytes, 1, multibyte);
7042 message3_nolog (m, nbytes, multibyte);
7043
7044 UNGCPRO;
7045 }
7046
7047
7048 /* The non-logging version of message3.
7049 This does not cancel echoing, because it is used for echoing.
7050 Perhaps we need to make a separate function for echoing
7051 and make this cancel echoing. */
7052
7053 void
7054 message3_nolog (m, nbytes, multibyte)
7055 Lisp_Object m;
7056 int nbytes, multibyte;
7057 {
7058 struct frame *sf = SELECTED_FRAME ();
7059 message_enable_multibyte = multibyte;
7060
7061 if (noninteractive)
7062 {
7063 if (noninteractive_need_newline)
7064 putc ('\n', stderr);
7065 noninteractive_need_newline = 0;
7066 if (STRINGP (m))
7067 fwrite (SDATA (m), nbytes, 1, stderr);
7068 if (cursor_in_echo_area == 0)
7069 fprintf (stderr, "\n");
7070 fflush (stderr);
7071 }
7072 /* A null message buffer means that the frame hasn't really been
7073 initialized yet. Error messages get reported properly by
7074 cmd_error, so this must be just an informative message; toss it. */
7075 else if (INTERACTIVE
7076 && sf->glyphs_initialized_p
7077 && FRAME_MESSAGE_BUF (sf))
7078 {
7079 Lisp_Object mini_window;
7080 Lisp_Object frame;
7081 struct frame *f;
7082
7083 /* Get the frame containing the mini-buffer
7084 that the selected frame is using. */
7085 mini_window = FRAME_MINIBUF_WINDOW (sf);
7086 frame = XWINDOW (mini_window)->frame;
7087 f = XFRAME (frame);
7088
7089 FRAME_SAMPLE_VISIBILITY (f);
7090 if (FRAME_VISIBLE_P (sf)
7091 && !FRAME_VISIBLE_P (f))
7092 Fmake_frame_visible (frame);
7093
7094 if (STRINGP (m) && SCHARS (m) > 0)
7095 {
7096 set_message (NULL, m, nbytes, multibyte);
7097 if (minibuffer_auto_raise)
7098 Fraise_frame (frame);
7099 }
7100 else
7101 clear_message (1, 1);
7102
7103 do_pending_window_change (0);
7104 echo_area_display (1);
7105 do_pending_window_change (0);
7106 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7107 (*frame_up_to_date_hook) (f);
7108 }
7109 }
7110
7111
7112 /* Display a null-terminated echo area message M. If M is 0, clear
7113 out any existing message, and let the mini-buffer text show through.
7114
7115 The buffer M must continue to exist until after the echo area gets
7116 cleared or some other message gets displayed there. Do not pass
7117 text that is stored in a Lisp string. Do not pass text in a buffer
7118 that was alloca'd. */
7119
7120 void
7121 message1 (m)
7122 char *m;
7123 {
7124 message2 (m, (m ? strlen (m) : 0), 0);
7125 }
7126
7127
7128 /* The non-logging counterpart of message1. */
7129
7130 void
7131 message1_nolog (m)
7132 char *m;
7133 {
7134 message2_nolog (m, (m ? strlen (m) : 0), 0);
7135 }
7136
7137 /* Display a message M which contains a single %s
7138 which gets replaced with STRING. */
7139
7140 void
7141 message_with_string (m, string, log)
7142 char *m;
7143 Lisp_Object string;
7144 int log;
7145 {
7146 CHECK_STRING (string);
7147
7148 if (noninteractive)
7149 {
7150 if (m)
7151 {
7152 if (noninteractive_need_newline)
7153 putc ('\n', stderr);
7154 noninteractive_need_newline = 0;
7155 fprintf (stderr, m, SDATA (string));
7156 if (cursor_in_echo_area == 0)
7157 fprintf (stderr, "\n");
7158 fflush (stderr);
7159 }
7160 }
7161 else if (INTERACTIVE)
7162 {
7163 /* The frame whose minibuffer we're going to display the message on.
7164 It may be larger than the selected frame, so we need
7165 to use its buffer, not the selected frame's buffer. */
7166 Lisp_Object mini_window;
7167 struct frame *f, *sf = SELECTED_FRAME ();
7168
7169 /* Get the frame containing the minibuffer
7170 that the selected frame is using. */
7171 mini_window = FRAME_MINIBUF_WINDOW (sf);
7172 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7173
7174 /* A null message buffer means that the frame hasn't really been
7175 initialized yet. Error messages get reported properly by
7176 cmd_error, so this must be just an informative message; toss it. */
7177 if (FRAME_MESSAGE_BUF (f))
7178 {
7179 Lisp_Object args[2], message;
7180 struct gcpro gcpro1, gcpro2;
7181
7182 args[0] = build_string (m);
7183 args[1] = message = string;
7184 GCPRO2 (args[0], message);
7185 gcpro1.nvars = 2;
7186
7187 message = Fformat (2, args);
7188
7189 if (log)
7190 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7191 else
7192 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7193
7194 UNGCPRO;
7195
7196 /* Print should start at the beginning of the message
7197 buffer next time. */
7198 message_buf_print = 0;
7199 }
7200 }
7201 }
7202
7203
7204 /* Dump an informative message to the minibuf. If M is 0, clear out
7205 any existing message, and let the mini-buffer text show through. */
7206
7207 /* VARARGS 1 */
7208 void
7209 message (m, a1, a2, a3)
7210 char *m;
7211 EMACS_INT a1, a2, a3;
7212 {
7213 if (noninteractive)
7214 {
7215 if (m)
7216 {
7217 if (noninteractive_need_newline)
7218 putc ('\n', stderr);
7219 noninteractive_need_newline = 0;
7220 fprintf (stderr, m, a1, a2, a3);
7221 if (cursor_in_echo_area == 0)
7222 fprintf (stderr, "\n");
7223 fflush (stderr);
7224 }
7225 }
7226 else if (INTERACTIVE)
7227 {
7228 /* The frame whose mini-buffer we're going to display the message
7229 on. It may be larger than the selected frame, so we need to
7230 use its buffer, not the selected frame's buffer. */
7231 Lisp_Object mini_window;
7232 struct frame *f, *sf = SELECTED_FRAME ();
7233
7234 /* Get the frame containing the mini-buffer
7235 that the selected frame is using. */
7236 mini_window = FRAME_MINIBUF_WINDOW (sf);
7237 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7238
7239 /* A null message buffer means that the frame hasn't really been
7240 initialized yet. Error messages get reported properly by
7241 cmd_error, so this must be just an informative message; toss
7242 it. */
7243 if (FRAME_MESSAGE_BUF (f))
7244 {
7245 if (m)
7246 {
7247 int len;
7248 #ifdef NO_ARG_ARRAY
7249 char *a[3];
7250 a[0] = (char *) a1;
7251 a[1] = (char *) a2;
7252 a[2] = (char *) a3;
7253
7254 len = doprnt (FRAME_MESSAGE_BUF (f),
7255 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7256 #else
7257 len = doprnt (FRAME_MESSAGE_BUF (f),
7258 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7259 (char **) &a1);
7260 #endif /* NO_ARG_ARRAY */
7261
7262 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7263 }
7264 else
7265 message1 (0);
7266
7267 /* Print should start at the beginning of the message
7268 buffer next time. */
7269 message_buf_print = 0;
7270 }
7271 }
7272 }
7273
7274
7275 /* The non-logging version of message. */
7276
7277 void
7278 message_nolog (m, a1, a2, a3)
7279 char *m;
7280 EMACS_INT a1, a2, a3;
7281 {
7282 Lisp_Object old_log_max;
7283 old_log_max = Vmessage_log_max;
7284 Vmessage_log_max = Qnil;
7285 message (m, a1, a2, a3);
7286 Vmessage_log_max = old_log_max;
7287 }
7288
7289
7290 /* Display the current message in the current mini-buffer. This is
7291 only called from error handlers in process.c, and is not time
7292 critical. */
7293
7294 void
7295 update_echo_area ()
7296 {
7297 if (!NILP (echo_area_buffer[0]))
7298 {
7299 Lisp_Object string;
7300 string = Fcurrent_message ();
7301 message3 (string, SBYTES (string),
7302 !NILP (current_buffer->enable_multibyte_characters));
7303 }
7304 }
7305
7306
7307 /* Make sure echo area buffers in `echo_buffers' are live.
7308 If they aren't, make new ones. */
7309
7310 static void
7311 ensure_echo_area_buffers ()
7312 {
7313 int i;
7314
7315 for (i = 0; i < 2; ++i)
7316 if (!BUFFERP (echo_buffer[i])
7317 || NILP (XBUFFER (echo_buffer[i])->name))
7318 {
7319 char name[30];
7320 Lisp_Object old_buffer;
7321 int j;
7322
7323 old_buffer = echo_buffer[i];
7324 sprintf (name, " *Echo Area %d*", i);
7325 echo_buffer[i] = Fget_buffer_create (build_string (name));
7326 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7327
7328 for (j = 0; j < 2; ++j)
7329 if (EQ (old_buffer, echo_area_buffer[j]))
7330 echo_area_buffer[j] = echo_buffer[i];
7331 }
7332 }
7333
7334
7335 /* Call FN with args A1..A4 with either the current or last displayed
7336 echo_area_buffer as current buffer.
7337
7338 WHICH zero means use the current message buffer
7339 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7340 from echo_buffer[] and clear it.
7341
7342 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7343 suitable buffer from echo_buffer[] and clear it.
7344
7345 Value is what FN returns. */
7346
7347 static int
7348 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7349 struct window *w;
7350 int which;
7351 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7352 EMACS_INT a1;
7353 Lisp_Object a2;
7354 EMACS_INT a3, a4;
7355 {
7356 Lisp_Object buffer;
7357 int this_one, the_other, clear_buffer_p, rc;
7358 int count = SPECPDL_INDEX ();
7359
7360 /* If buffers aren't live, make new ones. */
7361 ensure_echo_area_buffers ();
7362
7363 clear_buffer_p = 0;
7364
7365 if (which == 0)
7366 this_one = 0, the_other = 1;
7367 else if (which > 0)
7368 this_one = 1, the_other = 0;
7369
7370 /* Choose a suitable buffer from echo_buffer[] is we don't
7371 have one. */
7372 if (NILP (echo_area_buffer[this_one]))
7373 {
7374 echo_area_buffer[this_one]
7375 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7376 ? echo_buffer[the_other]
7377 : echo_buffer[this_one]);
7378 clear_buffer_p = 1;
7379 }
7380
7381 buffer = echo_area_buffer[this_one];
7382
7383 /* Don't get confused by reusing the buffer used for echoing
7384 for a different purpose. */
7385 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7386 cancel_echoing ();
7387
7388 record_unwind_protect (unwind_with_echo_area_buffer,
7389 with_echo_area_buffer_unwind_data (w));
7390
7391 /* Make the echo area buffer current. Note that for display
7392 purposes, it is not necessary that the displayed window's buffer
7393 == current_buffer, except for text property lookup. So, let's
7394 only set that buffer temporarily here without doing a full
7395 Fset_window_buffer. We must also change w->pointm, though,
7396 because otherwise an assertions in unshow_buffer fails, and Emacs
7397 aborts. */
7398 set_buffer_internal_1 (XBUFFER (buffer));
7399 if (w)
7400 {
7401 w->buffer = buffer;
7402 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7403 }
7404
7405 current_buffer->undo_list = Qt;
7406 current_buffer->read_only = Qnil;
7407 specbind (Qinhibit_read_only, Qt);
7408 specbind (Qinhibit_modification_hooks, Qt);
7409
7410 if (clear_buffer_p && Z > BEG)
7411 del_range (BEG, Z);
7412
7413 xassert (BEGV >= BEG);
7414 xassert (ZV <= Z && ZV >= BEGV);
7415
7416 rc = fn (a1, a2, a3, a4);
7417
7418 xassert (BEGV >= BEG);
7419 xassert (ZV <= Z && ZV >= BEGV);
7420
7421 unbind_to (count, Qnil);
7422 return rc;
7423 }
7424
7425
7426 /* Save state that should be preserved around the call to the function
7427 FN called in with_echo_area_buffer. */
7428
7429 static Lisp_Object
7430 with_echo_area_buffer_unwind_data (w)
7431 struct window *w;
7432 {
7433 int i = 0;
7434 Lisp_Object vector;
7435
7436 /* Reduce consing by keeping one vector in
7437 Vwith_echo_area_save_vector. */
7438 vector = Vwith_echo_area_save_vector;
7439 Vwith_echo_area_save_vector = Qnil;
7440
7441 if (NILP (vector))
7442 vector = Fmake_vector (make_number (7), Qnil);
7443
7444 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7445 AREF (vector, i) = Vdeactivate_mark, ++i;
7446 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7447
7448 if (w)
7449 {
7450 XSETWINDOW (AREF (vector, i), w); ++i;
7451 AREF (vector, i) = w->buffer; ++i;
7452 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7453 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7454 }
7455 else
7456 {
7457 int end = i + 4;
7458 for (; i < end; ++i)
7459 AREF (vector, i) = Qnil;
7460 }
7461
7462 xassert (i == ASIZE (vector));
7463 return vector;
7464 }
7465
7466
7467 /* Restore global state from VECTOR which was created by
7468 with_echo_area_buffer_unwind_data. */
7469
7470 static Lisp_Object
7471 unwind_with_echo_area_buffer (vector)
7472 Lisp_Object vector;
7473 {
7474 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7475 Vdeactivate_mark = AREF (vector, 1);
7476 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7477
7478 if (WINDOWP (AREF (vector, 3)))
7479 {
7480 struct window *w;
7481 Lisp_Object buffer, charpos, bytepos;
7482
7483 w = XWINDOW (AREF (vector, 3));
7484 buffer = AREF (vector, 4);
7485 charpos = AREF (vector, 5);
7486 bytepos = AREF (vector, 6);
7487
7488 w->buffer = buffer;
7489 set_marker_both (w->pointm, buffer,
7490 XFASTINT (charpos), XFASTINT (bytepos));
7491 }
7492
7493 Vwith_echo_area_save_vector = vector;
7494 return Qnil;
7495 }
7496
7497
7498 /* Set up the echo area for use by print functions. MULTIBYTE_P
7499 non-zero means we will print multibyte. */
7500
7501 void
7502 setup_echo_area_for_printing (multibyte_p)
7503 int multibyte_p;
7504 {
7505 /* If we can't find an echo area any more, exit. */
7506 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7507 Fkill_emacs (Qnil);
7508
7509 ensure_echo_area_buffers ();
7510
7511 if (!message_buf_print)
7512 {
7513 /* A message has been output since the last time we printed.
7514 Choose a fresh echo area buffer. */
7515 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7516 echo_area_buffer[0] = echo_buffer[1];
7517 else
7518 echo_area_buffer[0] = echo_buffer[0];
7519
7520 /* Switch to that buffer and clear it. */
7521 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7522 current_buffer->truncate_lines = Qnil;
7523
7524 if (Z > BEG)
7525 {
7526 int count = SPECPDL_INDEX ();
7527 specbind (Qinhibit_read_only, Qt);
7528 /* Note that undo recording is always disabled. */
7529 del_range (BEG, Z);
7530 unbind_to (count, Qnil);
7531 }
7532 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7533
7534 /* Set up the buffer for the multibyteness we need. */
7535 if (multibyte_p
7536 != !NILP (current_buffer->enable_multibyte_characters))
7537 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7538
7539 /* Raise the frame containing the echo area. */
7540 if (minibuffer_auto_raise)
7541 {
7542 struct frame *sf = SELECTED_FRAME ();
7543 Lisp_Object mini_window;
7544 mini_window = FRAME_MINIBUF_WINDOW (sf);
7545 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7546 }
7547
7548 message_log_maybe_newline ();
7549 message_buf_print = 1;
7550 }
7551 else
7552 {
7553 if (NILP (echo_area_buffer[0]))
7554 {
7555 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7556 echo_area_buffer[0] = echo_buffer[1];
7557 else
7558 echo_area_buffer[0] = echo_buffer[0];
7559 }
7560
7561 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7562 {
7563 /* Someone switched buffers between print requests. */
7564 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7565 current_buffer->truncate_lines = Qnil;
7566 }
7567 }
7568 }
7569
7570
7571 /* Display an echo area message in window W. Value is non-zero if W's
7572 height is changed. If display_last_displayed_message_p is
7573 non-zero, display the message that was last displayed, otherwise
7574 display the current message. */
7575
7576 static int
7577 display_echo_area (w)
7578 struct window *w;
7579 {
7580 int i, no_message_p, window_height_changed_p, count;
7581
7582 /* Temporarily disable garbage collections while displaying the echo
7583 area. This is done because a GC can print a message itself.
7584 That message would modify the echo area buffer's contents while a
7585 redisplay of the buffer is going on, and seriously confuse
7586 redisplay. */
7587 count = inhibit_garbage_collection ();
7588
7589 /* If there is no message, we must call display_echo_area_1
7590 nevertheless because it resizes the window. But we will have to
7591 reset the echo_area_buffer in question to nil at the end because
7592 with_echo_area_buffer will sets it to an empty buffer. */
7593 i = display_last_displayed_message_p ? 1 : 0;
7594 no_message_p = NILP (echo_area_buffer[i]);
7595
7596 window_height_changed_p
7597 = with_echo_area_buffer (w, display_last_displayed_message_p,
7598 display_echo_area_1,
7599 (EMACS_INT) w, Qnil, 0, 0);
7600
7601 if (no_message_p)
7602 echo_area_buffer[i] = Qnil;
7603
7604 unbind_to (count, Qnil);
7605 return window_height_changed_p;
7606 }
7607
7608
7609 /* Helper for display_echo_area. Display the current buffer which
7610 contains the current echo area message in window W, a mini-window,
7611 a pointer to which is passed in A1. A2..A4 are currently not used.
7612 Change the height of W so that all of the message is displayed.
7613 Value is non-zero if height of W was changed. */
7614
7615 static int
7616 display_echo_area_1 (a1, a2, a3, a4)
7617 EMACS_INT a1;
7618 Lisp_Object a2;
7619 EMACS_INT a3, a4;
7620 {
7621 struct window *w = (struct window *) a1;
7622 Lisp_Object window;
7623 struct text_pos start;
7624 int window_height_changed_p = 0;
7625
7626 /* Do this before displaying, so that we have a large enough glyph
7627 matrix for the display. */
7628 window_height_changed_p = resize_mini_window (w, 0);
7629
7630 /* Display. */
7631 clear_glyph_matrix (w->desired_matrix);
7632 XSETWINDOW (window, w);
7633 SET_TEXT_POS (start, BEG, BEG_BYTE);
7634 try_window (window, start, 0);
7635
7636 return window_height_changed_p;
7637 }
7638
7639
7640 /* Resize the echo area window to exactly the size needed for the
7641 currently displayed message, if there is one. If a mini-buffer
7642 is active, don't shrink it. */
7643
7644 void
7645 resize_echo_area_exactly ()
7646 {
7647 if (BUFFERP (echo_area_buffer[0])
7648 && WINDOWP (echo_area_window))
7649 {
7650 struct window *w = XWINDOW (echo_area_window);
7651 int resized_p;
7652 Lisp_Object resize_exactly;
7653
7654 if (minibuf_level == 0)
7655 resize_exactly = Qt;
7656 else
7657 resize_exactly = Qnil;
7658
7659 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7660 (EMACS_INT) w, resize_exactly, 0, 0);
7661 if (resized_p)
7662 {
7663 ++windows_or_buffers_changed;
7664 ++update_mode_lines;
7665 redisplay_internal (0);
7666 }
7667 }
7668 }
7669
7670
7671 /* Callback function for with_echo_area_buffer, when used from
7672 resize_echo_area_exactly. A1 contains a pointer to the window to
7673 resize, EXACTLY non-nil means resize the mini-window exactly to the
7674 size of the text displayed. A3 and A4 are not used. Value is what
7675 resize_mini_window returns. */
7676
7677 static int
7678 resize_mini_window_1 (a1, exactly, a3, a4)
7679 EMACS_INT a1;
7680 Lisp_Object exactly;
7681 EMACS_INT a3, a4;
7682 {
7683 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7684 }
7685
7686
7687 /* Resize mini-window W to fit the size of its contents. EXACT:P
7688 means size the window exactly to the size needed. Otherwise, it's
7689 only enlarged until W's buffer is empty. Value is non-zero if
7690 the window height has been changed. */
7691
7692 int
7693 resize_mini_window (w, exact_p)
7694 struct window *w;
7695 int exact_p;
7696 {
7697 struct frame *f = XFRAME (w->frame);
7698 int window_height_changed_p = 0;
7699
7700 xassert (MINI_WINDOW_P (w));
7701
7702 /* Don't resize windows while redisplaying a window; it would
7703 confuse redisplay functions when the size of the window they are
7704 displaying changes from under them. Such a resizing can happen,
7705 for instance, when which-func prints a long message while
7706 we are running fontification-functions. We're running these
7707 functions with safe_call which binds inhibit-redisplay to t. */
7708 if (!NILP (Vinhibit_redisplay))
7709 return 0;
7710
7711 /* Nil means don't try to resize. */
7712 if (NILP (Vresize_mini_windows)
7713 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7714 return 0;
7715
7716 if (!FRAME_MINIBUF_ONLY_P (f))
7717 {
7718 struct it it;
7719 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7720 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7721 int height, max_height;
7722 int unit = FRAME_LINE_HEIGHT (f);
7723 struct text_pos start;
7724 struct buffer *old_current_buffer = NULL;
7725
7726 if (current_buffer != XBUFFER (w->buffer))
7727 {
7728 old_current_buffer = current_buffer;
7729 set_buffer_internal (XBUFFER (w->buffer));
7730 }
7731
7732 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7733
7734 /* Compute the max. number of lines specified by the user. */
7735 if (FLOATP (Vmax_mini_window_height))
7736 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7737 else if (INTEGERP (Vmax_mini_window_height))
7738 max_height = XINT (Vmax_mini_window_height);
7739 else
7740 max_height = total_height / 4;
7741
7742 /* Correct that max. height if it's bogus. */
7743 max_height = max (1, max_height);
7744 max_height = min (total_height, max_height);
7745
7746 /* Find out the height of the text in the window. */
7747 if (it.truncate_lines_p)
7748 height = 1;
7749 else
7750 {
7751 last_height = 0;
7752 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7753 if (it.max_ascent == 0 && it.max_descent == 0)
7754 height = it.current_y + last_height;
7755 else
7756 height = it.current_y + it.max_ascent + it.max_descent;
7757 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7758 height = (height + unit - 1) / unit;
7759 }
7760
7761 /* Compute a suitable window start. */
7762 if (height > max_height)
7763 {
7764 height = max_height;
7765 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7766 move_it_vertically_backward (&it, (height - 1) * unit);
7767 start = it.current.pos;
7768 }
7769 else
7770 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7771 SET_MARKER_FROM_TEXT_POS (w->start, start);
7772
7773 if (EQ (Vresize_mini_windows, Qgrow_only))
7774 {
7775 /* Let it grow only, until we display an empty message, in which
7776 case the window shrinks again. */
7777 if (height > WINDOW_TOTAL_LINES (w))
7778 {
7779 int old_height = WINDOW_TOTAL_LINES (w);
7780 freeze_window_starts (f, 1);
7781 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7782 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7783 }
7784 else if (height < WINDOW_TOTAL_LINES (w)
7785 && (exact_p || BEGV == ZV))
7786 {
7787 int old_height = WINDOW_TOTAL_LINES (w);
7788 freeze_window_starts (f, 0);
7789 shrink_mini_window (w);
7790 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7791 }
7792 }
7793 else
7794 {
7795 /* Always resize to exact size needed. */
7796 if (height > WINDOW_TOTAL_LINES (w))
7797 {
7798 int old_height = WINDOW_TOTAL_LINES (w);
7799 freeze_window_starts (f, 1);
7800 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7801 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7802 }
7803 else if (height < WINDOW_TOTAL_LINES (w))
7804 {
7805 int old_height = WINDOW_TOTAL_LINES (w);
7806 freeze_window_starts (f, 0);
7807 shrink_mini_window (w);
7808
7809 if (height)
7810 {
7811 freeze_window_starts (f, 1);
7812 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7813 }
7814
7815 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7816 }
7817 }
7818
7819 if (old_current_buffer)
7820 set_buffer_internal (old_current_buffer);
7821 }
7822
7823 return window_height_changed_p;
7824 }
7825
7826
7827 /* Value is the current message, a string, or nil if there is no
7828 current message. */
7829
7830 Lisp_Object
7831 current_message ()
7832 {
7833 Lisp_Object msg;
7834
7835 if (NILP (echo_area_buffer[0]))
7836 msg = Qnil;
7837 else
7838 {
7839 with_echo_area_buffer (0, 0, current_message_1,
7840 (EMACS_INT) &msg, Qnil, 0, 0);
7841 if (NILP (msg))
7842 echo_area_buffer[0] = Qnil;
7843 }
7844
7845 return msg;
7846 }
7847
7848
7849 static int
7850 current_message_1 (a1, a2, a3, a4)
7851 EMACS_INT a1;
7852 Lisp_Object a2;
7853 EMACS_INT a3, a4;
7854 {
7855 Lisp_Object *msg = (Lisp_Object *) a1;
7856
7857 if (Z > BEG)
7858 *msg = make_buffer_string (BEG, Z, 1);
7859 else
7860 *msg = Qnil;
7861 return 0;
7862 }
7863
7864
7865 /* Push the current message on Vmessage_stack for later restauration
7866 by restore_message. Value is non-zero if the current message isn't
7867 empty. This is a relatively infrequent operation, so it's not
7868 worth optimizing. */
7869
7870 int
7871 push_message ()
7872 {
7873 Lisp_Object msg;
7874 msg = current_message ();
7875 Vmessage_stack = Fcons (msg, Vmessage_stack);
7876 return STRINGP (msg);
7877 }
7878
7879
7880 /* Restore message display from the top of Vmessage_stack. */
7881
7882 void
7883 restore_message ()
7884 {
7885 Lisp_Object msg;
7886
7887 xassert (CONSP (Vmessage_stack));
7888 msg = XCAR (Vmessage_stack);
7889 if (STRINGP (msg))
7890 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7891 else
7892 message3_nolog (msg, 0, 0);
7893 }
7894
7895
7896 /* Handler for record_unwind_protect calling pop_message. */
7897
7898 Lisp_Object
7899 pop_message_unwind (dummy)
7900 Lisp_Object dummy;
7901 {
7902 pop_message ();
7903 return Qnil;
7904 }
7905
7906 /* Pop the top-most entry off Vmessage_stack. */
7907
7908 void
7909 pop_message ()
7910 {
7911 xassert (CONSP (Vmessage_stack));
7912 Vmessage_stack = XCDR (Vmessage_stack);
7913 }
7914
7915
7916 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7917 exits. If the stack is not empty, we have a missing pop_message
7918 somewhere. */
7919
7920 void
7921 check_message_stack ()
7922 {
7923 if (!NILP (Vmessage_stack))
7924 abort ();
7925 }
7926
7927
7928 /* Truncate to NCHARS what will be displayed in the echo area the next
7929 time we display it---but don't redisplay it now. */
7930
7931 void
7932 truncate_echo_area (nchars)
7933 int nchars;
7934 {
7935 if (nchars == 0)
7936 echo_area_buffer[0] = Qnil;
7937 /* A null message buffer means that the frame hasn't really been
7938 initialized yet. Error messages get reported properly by
7939 cmd_error, so this must be just an informative message; toss it. */
7940 else if (!noninteractive
7941 && INTERACTIVE
7942 && !NILP (echo_area_buffer[0]))
7943 {
7944 struct frame *sf = SELECTED_FRAME ();
7945 if (FRAME_MESSAGE_BUF (sf))
7946 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7947 }
7948 }
7949
7950
7951 /* Helper function for truncate_echo_area. Truncate the current
7952 message to at most NCHARS characters. */
7953
7954 static int
7955 truncate_message_1 (nchars, a2, a3, a4)
7956 EMACS_INT nchars;
7957 Lisp_Object a2;
7958 EMACS_INT a3, a4;
7959 {
7960 if (BEG + nchars < Z)
7961 del_range (BEG + nchars, Z);
7962 if (Z == BEG)
7963 echo_area_buffer[0] = Qnil;
7964 return 0;
7965 }
7966
7967
7968 /* Set the current message to a substring of S or STRING.
7969
7970 If STRING is a Lisp string, set the message to the first NBYTES
7971 bytes from STRING. NBYTES zero means use the whole string. If
7972 STRING is multibyte, the message will be displayed multibyte.
7973
7974 If S is not null, set the message to the first LEN bytes of S. LEN
7975 zero means use the whole string. MULTIBYTE_P non-zero means S is
7976 multibyte. Display the message multibyte in that case. */
7977
7978 void
7979 set_message (s, string, nbytes, multibyte_p)
7980 const char *s;
7981 Lisp_Object string;
7982 int nbytes, multibyte_p;
7983 {
7984 message_enable_multibyte
7985 = ((s && multibyte_p)
7986 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7987
7988 with_echo_area_buffer (0, 0, set_message_1,
7989 (EMACS_INT) s, string, nbytes, multibyte_p);
7990 message_buf_print = 0;
7991 help_echo_showing_p = 0;
7992 }
7993
7994
7995 /* Helper function for set_message. Arguments have the same meaning
7996 as there, with A1 corresponding to S and A2 corresponding to STRING
7997 This function is called with the echo area buffer being
7998 current. */
7999
8000 static int
8001 set_message_1 (a1, a2, nbytes, multibyte_p)
8002 EMACS_INT a1;
8003 Lisp_Object a2;
8004 EMACS_INT nbytes, multibyte_p;
8005 {
8006 const char *s = (const char *) a1;
8007 Lisp_Object string = a2;
8008
8009 /* Change multibyteness of the echo buffer appropriately. */
8010 if (message_enable_multibyte
8011 != !NILP (current_buffer->enable_multibyte_characters))
8012 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8013
8014 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8015
8016 /* Insert new message at BEG. */
8017 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8018 Ferase_buffer ();
8019
8020 if (STRINGP (string))
8021 {
8022 int nchars;
8023
8024 if (nbytes == 0)
8025 nbytes = SBYTES (string);
8026 nchars = string_byte_to_char (string, nbytes);
8027
8028 /* This function takes care of single/multibyte conversion. We
8029 just have to ensure that the echo area buffer has the right
8030 setting of enable_multibyte_characters. */
8031 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8032 }
8033 else if (s)
8034 {
8035 if (nbytes == 0)
8036 nbytes = strlen (s);
8037
8038 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8039 {
8040 /* Convert from multi-byte to single-byte. */
8041 int i, c, n;
8042 unsigned char work[1];
8043
8044 /* Convert a multibyte string to single-byte. */
8045 for (i = 0; i < nbytes; i += n)
8046 {
8047 c = string_char_and_length (s + i, nbytes - i, &n);
8048 work[0] = (SINGLE_BYTE_CHAR_P (c)
8049 ? c
8050 : multibyte_char_to_unibyte (c, Qnil));
8051 insert_1_both (work, 1, 1, 1, 0, 0);
8052 }
8053 }
8054 else if (!multibyte_p
8055 && !NILP (current_buffer->enable_multibyte_characters))
8056 {
8057 /* Convert from single-byte to multi-byte. */
8058 int i, c, n;
8059 const unsigned char *msg = (const unsigned char *) s;
8060 unsigned char str[MAX_MULTIBYTE_LENGTH];
8061
8062 /* Convert a single-byte string to multibyte. */
8063 for (i = 0; i < nbytes; i++)
8064 {
8065 c = unibyte_char_to_multibyte (msg[i]);
8066 n = CHAR_STRING (c, str);
8067 insert_1_both (str, 1, n, 1, 0, 0);
8068 }
8069 }
8070 else
8071 insert_1 (s, nbytes, 1, 0, 0);
8072 }
8073
8074 return 0;
8075 }
8076
8077
8078 /* Clear messages. CURRENT_P non-zero means clear the current
8079 message. LAST_DISPLAYED_P non-zero means clear the message
8080 last displayed. */
8081
8082 void
8083 clear_message (current_p, last_displayed_p)
8084 int current_p, last_displayed_p;
8085 {
8086 if (current_p)
8087 {
8088 echo_area_buffer[0] = Qnil;
8089 message_cleared_p = 1;
8090 }
8091
8092 if (last_displayed_p)
8093 echo_area_buffer[1] = Qnil;
8094
8095 message_buf_print = 0;
8096 }
8097
8098 /* Clear garbaged frames.
8099
8100 This function is used where the old redisplay called
8101 redraw_garbaged_frames which in turn called redraw_frame which in
8102 turn called clear_frame. The call to clear_frame was a source of
8103 flickering. I believe a clear_frame is not necessary. It should
8104 suffice in the new redisplay to invalidate all current matrices,
8105 and ensure a complete redisplay of all windows. */
8106
8107 static void
8108 clear_garbaged_frames ()
8109 {
8110 if (frame_garbaged)
8111 {
8112 Lisp_Object tail, frame;
8113 int changed_count = 0;
8114
8115 FOR_EACH_FRAME (tail, frame)
8116 {
8117 struct frame *f = XFRAME (frame);
8118
8119 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8120 {
8121 if (f->resized_p)
8122 {
8123 Fredraw_frame (frame);
8124 f->force_flush_display_p = 1;
8125 }
8126 clear_current_matrices (f);
8127 changed_count++;
8128 f->garbaged = 0;
8129 f->resized_p = 0;
8130 }
8131 }
8132
8133 frame_garbaged = 0;
8134 if (changed_count)
8135 ++windows_or_buffers_changed;
8136 }
8137 }
8138
8139
8140 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8141 is non-zero update selected_frame. Value is non-zero if the
8142 mini-windows height has been changed. */
8143
8144 static int
8145 echo_area_display (update_frame_p)
8146 int update_frame_p;
8147 {
8148 Lisp_Object mini_window;
8149 struct window *w;
8150 struct frame *f;
8151 int window_height_changed_p = 0;
8152 struct frame *sf = SELECTED_FRAME ();
8153
8154 mini_window = FRAME_MINIBUF_WINDOW (sf);
8155 w = XWINDOW (mini_window);
8156 f = XFRAME (WINDOW_FRAME (w));
8157
8158 /* Don't display if frame is invisible or not yet initialized. */
8159 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8160 return 0;
8161
8162 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8163 #ifndef MAC_OS8
8164 #ifdef HAVE_WINDOW_SYSTEM
8165 /* When Emacs starts, selected_frame may be a visible terminal
8166 frame, even if we run under a window system. If we let this
8167 through, a message would be displayed on the terminal. */
8168 if (EQ (selected_frame, Vterminal_frame)
8169 && !NILP (Vwindow_system))
8170 return 0;
8171 #endif /* HAVE_WINDOW_SYSTEM */
8172 #endif
8173
8174 /* Redraw garbaged frames. */
8175 if (frame_garbaged)
8176 clear_garbaged_frames ();
8177
8178 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8179 {
8180 echo_area_window = mini_window;
8181 window_height_changed_p = display_echo_area (w);
8182 w->must_be_updated_p = 1;
8183
8184 /* Update the display, unless called from redisplay_internal.
8185 Also don't update the screen during redisplay itself. The
8186 update will happen at the end of redisplay, and an update
8187 here could cause confusion. */
8188 if (update_frame_p && !redisplaying_p)
8189 {
8190 int n = 0;
8191
8192 /* If the display update has been interrupted by pending
8193 input, update mode lines in the frame. Due to the
8194 pending input, it might have been that redisplay hasn't
8195 been called, so that mode lines above the echo area are
8196 garbaged. This looks odd, so we prevent it here. */
8197 if (!display_completed)
8198 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8199
8200 if (window_height_changed_p
8201 /* Don't do this if Emacs is shutting down. Redisplay
8202 needs to run hooks. */
8203 && !NILP (Vrun_hooks))
8204 {
8205 /* Must update other windows. Likewise as in other
8206 cases, don't let this update be interrupted by
8207 pending input. */
8208 int count = SPECPDL_INDEX ();
8209 specbind (Qredisplay_dont_pause, Qt);
8210 windows_or_buffers_changed = 1;
8211 redisplay_internal (0);
8212 unbind_to (count, Qnil);
8213 }
8214 else if (FRAME_WINDOW_P (f) && n == 0)
8215 {
8216 /* Window configuration is the same as before.
8217 Can do with a display update of the echo area,
8218 unless we displayed some mode lines. */
8219 update_single_window (w, 1);
8220 rif->flush_display (f);
8221 }
8222 else
8223 update_frame (f, 1, 1);
8224
8225 /* If cursor is in the echo area, make sure that the next
8226 redisplay displays the minibuffer, so that the cursor will
8227 be replaced with what the minibuffer wants. */
8228 if (cursor_in_echo_area)
8229 ++windows_or_buffers_changed;
8230 }
8231 }
8232 else if (!EQ (mini_window, selected_window))
8233 windows_or_buffers_changed++;
8234
8235 /* The current message is now also the last one displayed. */
8236 echo_area_buffer[1] = echo_area_buffer[0];
8237
8238 /* Prevent redisplay optimization in redisplay_internal by resetting
8239 this_line_start_pos. This is done because the mini-buffer now
8240 displays the message instead of its buffer text. */
8241 if (EQ (mini_window, selected_window))
8242 CHARPOS (this_line_start_pos) = 0;
8243
8244 return window_height_changed_p;
8245 }
8246
8247
8248 \f
8249 /***********************************************************************
8250 Mode Lines and Frame Titles
8251 ***********************************************************************/
8252
8253 /* A buffer for constructing non-propertized mode-line strings and
8254 frame titles in it; allocated from the heap in init_xdisp and
8255 resized as needed in store_mode_line_noprop_char. */
8256
8257 static char *mode_line_noprop_buf;
8258
8259 /* The buffer's end, and a current output position in it. */
8260
8261 static char *mode_line_noprop_buf_end;
8262 static char *mode_line_noprop_ptr;
8263
8264 #define MODE_LINE_NOPROP_LEN(start) \
8265 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8266
8267 static enum {
8268 MODE_LINE_DISPLAY = 0,
8269 MODE_LINE_TITLE,
8270 MODE_LINE_NOPROP,
8271 MODE_LINE_STRING
8272 } mode_line_target;
8273
8274 /* Alist that caches the results of :propertize.
8275 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8276 static Lisp_Object mode_line_proptrans_alist;
8277
8278 /* List of strings making up the mode-line. */
8279 static Lisp_Object mode_line_string_list;
8280
8281 /* Base face property when building propertized mode line string. */
8282 static Lisp_Object mode_line_string_face;
8283 static Lisp_Object mode_line_string_face_prop;
8284
8285
8286 /* Unwind data for mode line strings */
8287
8288 static Lisp_Object Vmode_line_unwind_vector;
8289
8290 static Lisp_Object
8291 format_mode_line_unwind_data (obuf)
8292 struct buffer *obuf;
8293 {
8294 Lisp_Object vector;
8295
8296 /* Reduce consing by keeping one vector in
8297 Vwith_echo_area_save_vector. */
8298 vector = Vmode_line_unwind_vector;
8299 Vmode_line_unwind_vector = Qnil;
8300
8301 if (NILP (vector))
8302 vector = Fmake_vector (make_number (7), Qnil);
8303
8304 AREF (vector, 0) = make_number (mode_line_target);
8305 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8306 AREF (vector, 2) = mode_line_string_list;
8307 AREF (vector, 3) = mode_line_proptrans_alist;
8308 AREF (vector, 4) = mode_line_string_face;
8309 AREF (vector, 5) = mode_line_string_face_prop;
8310
8311 if (obuf)
8312 XSETBUFFER (AREF (vector, 6), obuf);
8313 else
8314 AREF (vector, 6) = Qnil;
8315
8316 return vector;
8317 }
8318
8319 static Lisp_Object
8320 unwind_format_mode_line (vector)
8321 Lisp_Object vector;
8322 {
8323 mode_line_target = XINT (AREF (vector, 0));
8324 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8325 mode_line_string_list = AREF (vector, 2);
8326 mode_line_proptrans_alist = AREF (vector, 3);
8327 mode_line_string_face = AREF (vector, 4);
8328 mode_line_string_face_prop = AREF (vector, 5);
8329
8330 if (!NILP (AREF (vector, 6)))
8331 {
8332 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8333 AREF (vector, 6) = Qnil;
8334 }
8335
8336 Vmode_line_unwind_vector = vector;
8337 return Qnil;
8338 }
8339
8340
8341 /* Store a single character C for the frame title in mode_line_noprop_buf.
8342 Re-allocate mode_line_noprop_buf if necessary. */
8343
8344 static void
8345 #ifdef PROTOTYPES
8346 store_mode_line_noprop_char (char c)
8347 #else
8348 store_mode_line_noprop_char (c)
8349 char c;
8350 #endif
8351 {
8352 /* If output position has reached the end of the allocated buffer,
8353 double the buffer's size. */
8354 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8355 {
8356 int len = MODE_LINE_NOPROP_LEN (0);
8357 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8358 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8359 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8360 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8361 }
8362
8363 *mode_line_noprop_ptr++ = c;
8364 }
8365
8366
8367 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8368 mode_line_noprop_ptr. STR is the string to store. Do not copy
8369 characters that yield more columns than PRECISION; PRECISION <= 0
8370 means copy the whole string. Pad with spaces until FIELD_WIDTH
8371 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8372 pad. Called from display_mode_element when it is used to build a
8373 frame title. */
8374
8375 static int
8376 store_mode_line_noprop (str, field_width, precision)
8377 const unsigned char *str;
8378 int field_width, precision;
8379 {
8380 int n = 0;
8381 int dummy, nbytes;
8382
8383 /* Copy at most PRECISION chars from STR. */
8384 nbytes = strlen (str);
8385 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8386 while (nbytes--)
8387 store_mode_line_noprop_char (*str++);
8388
8389 /* Fill up with spaces until FIELD_WIDTH reached. */
8390 while (field_width > 0
8391 && n < field_width)
8392 {
8393 store_mode_line_noprop_char (' ');
8394 ++n;
8395 }
8396
8397 return n;
8398 }
8399
8400 /***********************************************************************
8401 Frame Titles
8402 ***********************************************************************/
8403
8404 #ifdef HAVE_WINDOW_SYSTEM
8405
8406 /* Set the title of FRAME, if it has changed. The title format is
8407 Vicon_title_format if FRAME is iconified, otherwise it is
8408 frame_title_format. */
8409
8410 static void
8411 x_consider_frame_title (frame)
8412 Lisp_Object frame;
8413 {
8414 struct frame *f = XFRAME (frame);
8415
8416 if (FRAME_WINDOW_P (f)
8417 || FRAME_MINIBUF_ONLY_P (f)
8418 || f->explicit_name)
8419 {
8420 /* Do we have more than one visible frame on this X display? */
8421 Lisp_Object tail;
8422 Lisp_Object fmt;
8423 int title_start;
8424 char *title;
8425 int len;
8426 struct it it;
8427 int count = SPECPDL_INDEX ();
8428
8429 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8430 {
8431 Lisp_Object other_frame = XCAR (tail);
8432 struct frame *tf = XFRAME (other_frame);
8433
8434 if (tf != f
8435 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8436 && !FRAME_MINIBUF_ONLY_P (tf)
8437 && !EQ (other_frame, tip_frame)
8438 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8439 break;
8440 }
8441
8442 /* Set global variable indicating that multiple frames exist. */
8443 multiple_frames = CONSP (tail);
8444
8445 /* Switch to the buffer of selected window of the frame. Set up
8446 mode_line_target so that display_mode_element will output into
8447 mode_line_noprop_buf; then display the title. */
8448 record_unwind_protect (unwind_format_mode_line,
8449 format_mode_line_unwind_data (current_buffer));
8450
8451 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8452 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8453
8454 mode_line_target = MODE_LINE_TITLE;
8455 title_start = MODE_LINE_NOPROP_LEN (0);
8456 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8457 NULL, DEFAULT_FACE_ID);
8458 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8459 len = MODE_LINE_NOPROP_LEN (title_start);
8460 title = mode_line_noprop_buf + title_start;
8461 unbind_to (count, Qnil);
8462
8463 /* Set the title only if it's changed. This avoids consing in
8464 the common case where it hasn't. (If it turns out that we've
8465 already wasted too much time by walking through the list with
8466 display_mode_element, then we might need to optimize at a
8467 higher level than this.) */
8468 if (! STRINGP (f->name)
8469 || SBYTES (f->name) != len
8470 || bcmp (title, SDATA (f->name), len) != 0)
8471 x_implicitly_set_name (f, make_string (title, len), Qnil);
8472 }
8473 }
8474
8475 #endif /* not HAVE_WINDOW_SYSTEM */
8476
8477
8478
8479 \f
8480 /***********************************************************************
8481 Menu Bars
8482 ***********************************************************************/
8483
8484
8485 /* Prepare for redisplay by updating menu-bar item lists when
8486 appropriate. This can call eval. */
8487
8488 void
8489 prepare_menu_bars ()
8490 {
8491 int all_windows;
8492 struct gcpro gcpro1, gcpro2;
8493 struct frame *f;
8494 Lisp_Object tooltip_frame;
8495
8496 #ifdef HAVE_WINDOW_SYSTEM
8497 tooltip_frame = tip_frame;
8498 #else
8499 tooltip_frame = Qnil;
8500 #endif
8501
8502 /* Update all frame titles based on their buffer names, etc. We do
8503 this before the menu bars so that the buffer-menu will show the
8504 up-to-date frame titles. */
8505 #ifdef HAVE_WINDOW_SYSTEM
8506 if (windows_or_buffers_changed || update_mode_lines)
8507 {
8508 Lisp_Object tail, frame;
8509
8510 FOR_EACH_FRAME (tail, frame)
8511 {
8512 f = XFRAME (frame);
8513 if (!EQ (frame, tooltip_frame)
8514 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8515 x_consider_frame_title (frame);
8516 }
8517 }
8518 #endif /* HAVE_WINDOW_SYSTEM */
8519
8520 /* Update the menu bar item lists, if appropriate. This has to be
8521 done before any actual redisplay or generation of display lines. */
8522 all_windows = (update_mode_lines
8523 || buffer_shared > 1
8524 || windows_or_buffers_changed);
8525 if (all_windows)
8526 {
8527 Lisp_Object tail, frame;
8528 int count = SPECPDL_INDEX ();
8529
8530 record_unwind_save_match_data ();
8531
8532 FOR_EACH_FRAME (tail, frame)
8533 {
8534 f = XFRAME (frame);
8535
8536 /* Ignore tooltip frame. */
8537 if (EQ (frame, tooltip_frame))
8538 continue;
8539
8540 /* If a window on this frame changed size, report that to
8541 the user and clear the size-change flag. */
8542 if (FRAME_WINDOW_SIZES_CHANGED (f))
8543 {
8544 Lisp_Object functions;
8545
8546 /* Clear flag first in case we get an error below. */
8547 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8548 functions = Vwindow_size_change_functions;
8549 GCPRO2 (tail, functions);
8550
8551 while (CONSP (functions))
8552 {
8553 call1 (XCAR (functions), frame);
8554 functions = XCDR (functions);
8555 }
8556 UNGCPRO;
8557 }
8558
8559 GCPRO1 (tail);
8560 update_menu_bar (f, 0);
8561 #ifdef HAVE_WINDOW_SYSTEM
8562 update_tool_bar (f, 0);
8563 #endif
8564 UNGCPRO;
8565 }
8566
8567 unbind_to (count, Qnil);
8568 }
8569 else
8570 {
8571 struct frame *sf = SELECTED_FRAME ();
8572 update_menu_bar (sf, 1);
8573 #ifdef HAVE_WINDOW_SYSTEM
8574 update_tool_bar (sf, 1);
8575 #endif
8576 }
8577
8578 /* Motif needs this. See comment in xmenu.c. Turn it off when
8579 pending_menu_activation is not defined. */
8580 #ifdef USE_X_TOOLKIT
8581 pending_menu_activation = 0;
8582 #endif
8583 }
8584
8585
8586 /* Update the menu bar item list for frame F. This has to be done
8587 before we start to fill in any display lines, because it can call
8588 eval.
8589
8590 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8591
8592 static void
8593 update_menu_bar (f, save_match_data)
8594 struct frame *f;
8595 int save_match_data;
8596 {
8597 Lisp_Object window;
8598 register struct window *w;
8599
8600 /* If called recursively during a menu update, do nothing. This can
8601 happen when, for instance, an activate-menubar-hook causes a
8602 redisplay. */
8603 if (inhibit_menubar_update)
8604 return;
8605
8606 window = FRAME_SELECTED_WINDOW (f);
8607 w = XWINDOW (window);
8608
8609 #if 0 /* The if statement below this if statement used to include the
8610 condition !NILP (w->update_mode_line), rather than using
8611 update_mode_lines directly, and this if statement may have
8612 been added to make that condition work. Now the if
8613 statement below matches its comment, this isn't needed. */
8614 if (update_mode_lines)
8615 w->update_mode_line = Qt;
8616 #endif
8617
8618 if (FRAME_WINDOW_P (f)
8619 ?
8620 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8621 || defined (USE_GTK)
8622 FRAME_EXTERNAL_MENU_BAR (f)
8623 #else
8624 FRAME_MENU_BAR_LINES (f) > 0
8625 #endif
8626 : FRAME_MENU_BAR_LINES (f) > 0)
8627 {
8628 /* If the user has switched buffers or windows, we need to
8629 recompute to reflect the new bindings. But we'll
8630 recompute when update_mode_lines is set too; that means
8631 that people can use force-mode-line-update to request
8632 that the menu bar be recomputed. The adverse effect on
8633 the rest of the redisplay algorithm is about the same as
8634 windows_or_buffers_changed anyway. */
8635 if (windows_or_buffers_changed
8636 /* This used to test w->update_mode_line, but we believe
8637 there is no need to recompute the menu in that case. */
8638 || update_mode_lines
8639 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8640 < BUF_MODIFF (XBUFFER (w->buffer)))
8641 != !NILP (w->last_had_star))
8642 || ((!NILP (Vtransient_mark_mode)
8643 && !NILP (XBUFFER (w->buffer)->mark_active))
8644 != !NILP (w->region_showing)))
8645 {
8646 struct buffer *prev = current_buffer;
8647 int count = SPECPDL_INDEX ();
8648
8649 specbind (Qinhibit_menubar_update, Qt);
8650
8651 set_buffer_internal_1 (XBUFFER (w->buffer));
8652 if (save_match_data)
8653 record_unwind_save_match_data ();
8654 if (NILP (Voverriding_local_map_menu_flag))
8655 {
8656 specbind (Qoverriding_terminal_local_map, Qnil);
8657 specbind (Qoverriding_local_map, Qnil);
8658 }
8659
8660 /* Run the Lucid hook. */
8661 safe_run_hooks (Qactivate_menubar_hook);
8662
8663 /* If it has changed current-menubar from previous value,
8664 really recompute the menu-bar from the value. */
8665 if (! NILP (Vlucid_menu_bar_dirty_flag))
8666 call0 (Qrecompute_lucid_menubar);
8667
8668 safe_run_hooks (Qmenu_bar_update_hook);
8669 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8670
8671 /* Redisplay the menu bar in case we changed it. */
8672 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8673 || defined (USE_GTK)
8674 if (FRAME_WINDOW_P (f)
8675 #if defined (MAC_OS)
8676 /* All frames on Mac OS share the same menubar. So only the
8677 selected frame should be allowed to set it. */
8678 && f == SELECTED_FRAME ()
8679 #endif
8680 )
8681 set_frame_menubar (f, 0, 0);
8682 else
8683 /* On a terminal screen, the menu bar is an ordinary screen
8684 line, and this makes it get updated. */
8685 w->update_mode_line = Qt;
8686 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8687 /* In the non-toolkit version, the menu bar is an ordinary screen
8688 line, and this makes it get updated. */
8689 w->update_mode_line = Qt;
8690 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8691
8692 unbind_to (count, Qnil);
8693 set_buffer_internal_1 (prev);
8694 }
8695 }
8696 }
8697
8698
8699 \f
8700 /***********************************************************************
8701 Output Cursor
8702 ***********************************************************************/
8703
8704 #ifdef HAVE_WINDOW_SYSTEM
8705
8706 /* EXPORT:
8707 Nominal cursor position -- where to draw output.
8708 HPOS and VPOS are window relative glyph matrix coordinates.
8709 X and Y are window relative pixel coordinates. */
8710
8711 struct cursor_pos output_cursor;
8712
8713
8714 /* EXPORT:
8715 Set the global variable output_cursor to CURSOR. All cursor
8716 positions are relative to updated_window. */
8717
8718 void
8719 set_output_cursor (cursor)
8720 struct cursor_pos *cursor;
8721 {
8722 output_cursor.hpos = cursor->hpos;
8723 output_cursor.vpos = cursor->vpos;
8724 output_cursor.x = cursor->x;
8725 output_cursor.y = cursor->y;
8726 }
8727
8728
8729 /* EXPORT for RIF:
8730 Set a nominal cursor position.
8731
8732 HPOS and VPOS are column/row positions in a window glyph matrix. X
8733 and Y are window text area relative pixel positions.
8734
8735 If this is done during an update, updated_window will contain the
8736 window that is being updated and the position is the future output
8737 cursor position for that window. If updated_window is null, use
8738 selected_window and display the cursor at the given position. */
8739
8740 void
8741 x_cursor_to (vpos, hpos, y, x)
8742 int vpos, hpos, y, x;
8743 {
8744 struct window *w;
8745
8746 /* If updated_window is not set, work on selected_window. */
8747 if (updated_window)
8748 w = updated_window;
8749 else
8750 w = XWINDOW (selected_window);
8751
8752 /* Set the output cursor. */
8753 output_cursor.hpos = hpos;
8754 output_cursor.vpos = vpos;
8755 output_cursor.x = x;
8756 output_cursor.y = y;
8757
8758 /* If not called as part of an update, really display the cursor.
8759 This will also set the cursor position of W. */
8760 if (updated_window == NULL)
8761 {
8762 BLOCK_INPUT;
8763 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8764 if (rif->flush_display_optional)
8765 rif->flush_display_optional (SELECTED_FRAME ());
8766 UNBLOCK_INPUT;
8767 }
8768 }
8769
8770 #endif /* HAVE_WINDOW_SYSTEM */
8771
8772 \f
8773 /***********************************************************************
8774 Tool-bars
8775 ***********************************************************************/
8776
8777 #ifdef HAVE_WINDOW_SYSTEM
8778
8779 /* Where the mouse was last time we reported a mouse event. */
8780
8781 FRAME_PTR last_mouse_frame;
8782
8783 /* Tool-bar item index of the item on which a mouse button was pressed
8784 or -1. */
8785
8786 int last_tool_bar_item;
8787
8788
8789 /* Update the tool-bar item list for frame F. This has to be done
8790 before we start to fill in any display lines. Called from
8791 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8792 and restore it here. */
8793
8794 static void
8795 update_tool_bar (f, save_match_data)
8796 struct frame *f;
8797 int save_match_data;
8798 {
8799 #ifdef USE_GTK
8800 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8801 #else
8802 int do_update = WINDOWP (f->tool_bar_window)
8803 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8804 #endif
8805
8806 if (do_update)
8807 {
8808 Lisp_Object window;
8809 struct window *w;
8810
8811 window = FRAME_SELECTED_WINDOW (f);
8812 w = XWINDOW (window);
8813
8814 /* If the user has switched buffers or windows, we need to
8815 recompute to reflect the new bindings. But we'll
8816 recompute when update_mode_lines is set too; that means
8817 that people can use force-mode-line-update to request
8818 that the menu bar be recomputed. The adverse effect on
8819 the rest of the redisplay algorithm is about the same as
8820 windows_or_buffers_changed anyway. */
8821 if (windows_or_buffers_changed
8822 || !NILP (w->update_mode_line)
8823 || update_mode_lines
8824 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8825 < BUF_MODIFF (XBUFFER (w->buffer)))
8826 != !NILP (w->last_had_star))
8827 || ((!NILP (Vtransient_mark_mode)
8828 && !NILP (XBUFFER (w->buffer)->mark_active))
8829 != !NILP (w->region_showing)))
8830 {
8831 struct buffer *prev = current_buffer;
8832 int count = SPECPDL_INDEX ();
8833 Lisp_Object new_tool_bar;
8834 int new_n_tool_bar;
8835 struct gcpro gcpro1;
8836
8837 /* Set current_buffer to the buffer of the selected
8838 window of the frame, so that we get the right local
8839 keymaps. */
8840 set_buffer_internal_1 (XBUFFER (w->buffer));
8841
8842 /* Save match data, if we must. */
8843 if (save_match_data)
8844 record_unwind_save_match_data ();
8845
8846 /* Make sure that we don't accidentally use bogus keymaps. */
8847 if (NILP (Voverriding_local_map_menu_flag))
8848 {
8849 specbind (Qoverriding_terminal_local_map, Qnil);
8850 specbind (Qoverriding_local_map, Qnil);
8851 }
8852
8853 GCPRO1 (new_tool_bar);
8854
8855 /* Build desired tool-bar items from keymaps. */
8856 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8857 &new_n_tool_bar);
8858
8859 /* Redisplay the tool-bar if we changed it. */
8860 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8861 {
8862 /* Redisplay that happens asynchronously due to an expose event
8863 may access f->tool_bar_items. Make sure we update both
8864 variables within BLOCK_INPUT so no such event interrupts. */
8865 BLOCK_INPUT;
8866 f->tool_bar_items = new_tool_bar;
8867 f->n_tool_bar_items = new_n_tool_bar;
8868 w->update_mode_line = Qt;
8869 UNBLOCK_INPUT;
8870 }
8871
8872 UNGCPRO;
8873
8874 unbind_to (count, Qnil);
8875 set_buffer_internal_1 (prev);
8876 }
8877 }
8878 }
8879
8880
8881 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8882 F's desired tool-bar contents. F->tool_bar_items must have
8883 been set up previously by calling prepare_menu_bars. */
8884
8885 static void
8886 build_desired_tool_bar_string (f)
8887 struct frame *f;
8888 {
8889 int i, size, size_needed;
8890 struct gcpro gcpro1, gcpro2, gcpro3;
8891 Lisp_Object image, plist, props;
8892
8893 image = plist = props = Qnil;
8894 GCPRO3 (image, plist, props);
8895
8896 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8897 Otherwise, make a new string. */
8898
8899 /* The size of the string we might be able to reuse. */
8900 size = (STRINGP (f->desired_tool_bar_string)
8901 ? SCHARS (f->desired_tool_bar_string)
8902 : 0);
8903
8904 /* We need one space in the string for each image. */
8905 size_needed = f->n_tool_bar_items;
8906
8907 /* Reuse f->desired_tool_bar_string, if possible. */
8908 if (size < size_needed || NILP (f->desired_tool_bar_string))
8909 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8910 make_number (' '));
8911 else
8912 {
8913 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8914 Fremove_text_properties (make_number (0), make_number (size),
8915 props, f->desired_tool_bar_string);
8916 }
8917
8918 /* Put a `display' property on the string for the images to display,
8919 put a `menu_item' property on tool-bar items with a value that
8920 is the index of the item in F's tool-bar item vector. */
8921 for (i = 0; i < f->n_tool_bar_items; ++i)
8922 {
8923 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8924
8925 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8926 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8927 int hmargin, vmargin, relief, idx, end;
8928 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8929
8930 /* If image is a vector, choose the image according to the
8931 button state. */
8932 image = PROP (TOOL_BAR_ITEM_IMAGES);
8933 if (VECTORP (image))
8934 {
8935 if (enabled_p)
8936 idx = (selected_p
8937 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8938 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8939 else
8940 idx = (selected_p
8941 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8942 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8943
8944 xassert (ASIZE (image) >= idx);
8945 image = AREF (image, idx);
8946 }
8947 else
8948 idx = -1;
8949
8950 /* Ignore invalid image specifications. */
8951 if (!valid_image_p (image))
8952 continue;
8953
8954 /* Display the tool-bar button pressed, or depressed. */
8955 plist = Fcopy_sequence (XCDR (image));
8956
8957 /* Compute margin and relief to draw. */
8958 relief = (tool_bar_button_relief >= 0
8959 ? tool_bar_button_relief
8960 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8961 hmargin = vmargin = relief;
8962
8963 if (INTEGERP (Vtool_bar_button_margin)
8964 && XINT (Vtool_bar_button_margin) > 0)
8965 {
8966 hmargin += XFASTINT (Vtool_bar_button_margin);
8967 vmargin += XFASTINT (Vtool_bar_button_margin);
8968 }
8969 else if (CONSP (Vtool_bar_button_margin))
8970 {
8971 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8972 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8973 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8974
8975 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8976 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8977 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8978 }
8979
8980 if (auto_raise_tool_bar_buttons_p)
8981 {
8982 /* Add a `:relief' property to the image spec if the item is
8983 selected. */
8984 if (selected_p)
8985 {
8986 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8987 hmargin -= relief;
8988 vmargin -= relief;
8989 }
8990 }
8991 else
8992 {
8993 /* If image is selected, display it pressed, i.e. with a
8994 negative relief. If it's not selected, display it with a
8995 raised relief. */
8996 plist = Fplist_put (plist, QCrelief,
8997 (selected_p
8998 ? make_number (-relief)
8999 : make_number (relief)));
9000 hmargin -= relief;
9001 vmargin -= relief;
9002 }
9003
9004 /* Put a margin around the image. */
9005 if (hmargin || vmargin)
9006 {
9007 if (hmargin == vmargin)
9008 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9009 else
9010 plist = Fplist_put (plist, QCmargin,
9011 Fcons (make_number (hmargin),
9012 make_number (vmargin)));
9013 }
9014
9015 /* If button is not enabled, and we don't have special images
9016 for the disabled state, make the image appear disabled by
9017 applying an appropriate algorithm to it. */
9018 if (!enabled_p && idx < 0)
9019 plist = Fplist_put (plist, QCconversion, Qdisabled);
9020
9021 /* Put a `display' text property on the string for the image to
9022 display. Put a `menu-item' property on the string that gives
9023 the start of this item's properties in the tool-bar items
9024 vector. */
9025 image = Fcons (Qimage, plist);
9026 props = list4 (Qdisplay, image,
9027 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9028
9029 /* Let the last image hide all remaining spaces in the tool bar
9030 string. The string can be longer than needed when we reuse a
9031 previous string. */
9032 if (i + 1 == f->n_tool_bar_items)
9033 end = SCHARS (f->desired_tool_bar_string);
9034 else
9035 end = i + 1;
9036 Fadd_text_properties (make_number (i), make_number (end),
9037 props, f->desired_tool_bar_string);
9038 #undef PROP
9039 }
9040
9041 UNGCPRO;
9042 }
9043
9044
9045 /* Display one line of the tool-bar of frame IT->f. */
9046
9047 static void
9048 display_tool_bar_line (it)
9049 struct it *it;
9050 {
9051 struct glyph_row *row = it->glyph_row;
9052 int max_x = it->last_visible_x;
9053 struct glyph *last;
9054
9055 prepare_desired_row (row);
9056 row->y = it->current_y;
9057
9058 /* Note that this isn't made use of if the face hasn't a box,
9059 so there's no need to check the face here. */
9060 it->start_of_box_run_p = 1;
9061
9062 while (it->current_x < max_x)
9063 {
9064 int x_before, x, n_glyphs_before, i, nglyphs;
9065
9066 /* Get the next display element. */
9067 if (!get_next_display_element (it))
9068 break;
9069
9070 /* Produce glyphs. */
9071 x_before = it->current_x;
9072 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9073 PRODUCE_GLYPHS (it);
9074
9075 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9076 i = 0;
9077 x = x_before;
9078 while (i < nglyphs)
9079 {
9080 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9081
9082 if (x + glyph->pixel_width > max_x)
9083 {
9084 /* Glyph doesn't fit on line. */
9085 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9086 it->current_x = x;
9087 goto out;
9088 }
9089
9090 ++it->hpos;
9091 x += glyph->pixel_width;
9092 ++i;
9093 }
9094
9095 /* Stop at line ends. */
9096 if (ITERATOR_AT_END_OF_LINE_P (it))
9097 break;
9098
9099 set_iterator_to_next (it, 1);
9100 }
9101
9102 out:;
9103
9104 row->displays_text_p = row->used[TEXT_AREA] != 0;
9105 extend_face_to_end_of_line (it);
9106 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9107 last->right_box_line_p = 1;
9108 if (last == row->glyphs[TEXT_AREA])
9109 last->left_box_line_p = 1;
9110 compute_line_metrics (it);
9111
9112 /* If line is empty, make it occupy the rest of the tool-bar. */
9113 if (!row->displays_text_p)
9114 {
9115 row->height = row->phys_height = it->last_visible_y - row->y;
9116 row->ascent = row->phys_ascent = 0;
9117 row->extra_line_spacing = 0;
9118 }
9119
9120 row->full_width_p = 1;
9121 row->continued_p = 0;
9122 row->truncated_on_left_p = 0;
9123 row->truncated_on_right_p = 0;
9124
9125 it->current_x = it->hpos = 0;
9126 it->current_y += row->height;
9127 ++it->vpos;
9128 ++it->glyph_row;
9129 }
9130
9131
9132 /* Value is the number of screen lines needed to make all tool-bar
9133 items of frame F visible. */
9134
9135 static int
9136 tool_bar_lines_needed (f)
9137 struct frame *f;
9138 {
9139 struct window *w = XWINDOW (f->tool_bar_window);
9140 struct it it;
9141
9142 /* Initialize an iterator for iteration over
9143 F->desired_tool_bar_string in the tool-bar window of frame F. */
9144 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9145 it.first_visible_x = 0;
9146 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9147 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9148
9149 while (!ITERATOR_AT_END_P (&it))
9150 {
9151 it.glyph_row = w->desired_matrix->rows;
9152 clear_glyph_row (it.glyph_row);
9153 display_tool_bar_line (&it);
9154 }
9155
9156 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9157 }
9158
9159
9160 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9161 0, 1, 0,
9162 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9163 (frame)
9164 Lisp_Object frame;
9165 {
9166 struct frame *f;
9167 struct window *w;
9168 int nlines = 0;
9169
9170 if (NILP (frame))
9171 frame = selected_frame;
9172 else
9173 CHECK_FRAME (frame);
9174 f = XFRAME (frame);
9175
9176 if (WINDOWP (f->tool_bar_window)
9177 || (w = XWINDOW (f->tool_bar_window),
9178 WINDOW_TOTAL_LINES (w) > 0))
9179 {
9180 update_tool_bar (f, 1);
9181 if (f->n_tool_bar_items)
9182 {
9183 build_desired_tool_bar_string (f);
9184 nlines = tool_bar_lines_needed (f);
9185 }
9186 }
9187
9188 return make_number (nlines);
9189 }
9190
9191
9192 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9193 height should be changed. */
9194
9195 static int
9196 redisplay_tool_bar (f)
9197 struct frame *f;
9198 {
9199 struct window *w;
9200 struct it it;
9201 struct glyph_row *row;
9202 int change_height_p = 0;
9203
9204 #ifdef USE_GTK
9205 if (FRAME_EXTERNAL_TOOL_BAR (f))
9206 update_frame_tool_bar (f);
9207 return 0;
9208 #endif
9209
9210 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9211 do anything. This means you must start with tool-bar-lines
9212 non-zero to get the auto-sizing effect. Or in other words, you
9213 can turn off tool-bars by specifying tool-bar-lines zero. */
9214 if (!WINDOWP (f->tool_bar_window)
9215 || (w = XWINDOW (f->tool_bar_window),
9216 WINDOW_TOTAL_LINES (w) == 0))
9217 return 0;
9218
9219 /* Set up an iterator for the tool-bar window. */
9220 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9221 it.first_visible_x = 0;
9222 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9223 row = it.glyph_row;
9224
9225 /* Build a string that represents the contents of the tool-bar. */
9226 build_desired_tool_bar_string (f);
9227 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9228
9229 /* Display as many lines as needed to display all tool-bar items. */
9230 while (it.current_y < it.last_visible_y)
9231 display_tool_bar_line (&it);
9232
9233 /* It doesn't make much sense to try scrolling in the tool-bar
9234 window, so don't do it. */
9235 w->desired_matrix->no_scrolling_p = 1;
9236 w->must_be_updated_p = 1;
9237
9238 if (auto_resize_tool_bars_p)
9239 {
9240 int nlines;
9241
9242 /* If we couldn't display everything, change the tool-bar's
9243 height. */
9244 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9245 change_height_p = 1;
9246
9247 /* If there are blank lines at the end, except for a partially
9248 visible blank line at the end that is smaller than
9249 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9250 row = it.glyph_row - 1;
9251 if (!row->displays_text_p
9252 && row->height >= FRAME_LINE_HEIGHT (f))
9253 change_height_p = 1;
9254
9255 /* If row displays tool-bar items, but is partially visible,
9256 change the tool-bar's height. */
9257 if (row->displays_text_p
9258 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9259 change_height_p = 1;
9260
9261 /* Resize windows as needed by changing the `tool-bar-lines'
9262 frame parameter. */
9263 if (change_height_p
9264 && (nlines = tool_bar_lines_needed (f),
9265 nlines != WINDOW_TOTAL_LINES (w)))
9266 {
9267 extern Lisp_Object Qtool_bar_lines;
9268 Lisp_Object frame;
9269 int old_height = WINDOW_TOTAL_LINES (w);
9270
9271 XSETFRAME (frame, f);
9272 clear_glyph_matrix (w->desired_matrix);
9273 Fmodify_frame_parameters (frame,
9274 Fcons (Fcons (Qtool_bar_lines,
9275 make_number (nlines)),
9276 Qnil));
9277 if (WINDOW_TOTAL_LINES (w) != old_height)
9278 fonts_changed_p = 1;
9279 }
9280 }
9281
9282 return change_height_p;
9283 }
9284
9285
9286 /* Get information about the tool-bar item which is displayed in GLYPH
9287 on frame F. Return in *PROP_IDX the index where tool-bar item
9288 properties start in F->tool_bar_items. Value is zero if
9289 GLYPH doesn't display a tool-bar item. */
9290
9291 static int
9292 tool_bar_item_info (f, glyph, prop_idx)
9293 struct frame *f;
9294 struct glyph *glyph;
9295 int *prop_idx;
9296 {
9297 Lisp_Object prop;
9298 int success_p;
9299 int charpos;
9300
9301 /* This function can be called asynchronously, which means we must
9302 exclude any possibility that Fget_text_property signals an
9303 error. */
9304 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9305 charpos = max (0, charpos);
9306
9307 /* Get the text property `menu-item' at pos. The value of that
9308 property is the start index of this item's properties in
9309 F->tool_bar_items. */
9310 prop = Fget_text_property (make_number (charpos),
9311 Qmenu_item, f->current_tool_bar_string);
9312 if (INTEGERP (prop))
9313 {
9314 *prop_idx = XINT (prop);
9315 success_p = 1;
9316 }
9317 else
9318 success_p = 0;
9319
9320 return success_p;
9321 }
9322
9323 \f
9324 /* Get information about the tool-bar item at position X/Y on frame F.
9325 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9326 the current matrix of the tool-bar window of F, or NULL if not
9327 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9328 item in F->tool_bar_items. Value is
9329
9330 -1 if X/Y is not on a tool-bar item
9331 0 if X/Y is on the same item that was highlighted before.
9332 1 otherwise. */
9333
9334 static int
9335 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9336 struct frame *f;
9337 int x, y;
9338 struct glyph **glyph;
9339 int *hpos, *vpos, *prop_idx;
9340 {
9341 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9342 struct window *w = XWINDOW (f->tool_bar_window);
9343 int area;
9344
9345 /* Find the glyph under X/Y. */
9346 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9347 if (*glyph == NULL)
9348 return -1;
9349
9350 /* Get the start of this tool-bar item's properties in
9351 f->tool_bar_items. */
9352 if (!tool_bar_item_info (f, *glyph, prop_idx))
9353 return -1;
9354
9355 /* Is mouse on the highlighted item? */
9356 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9357 && *vpos >= dpyinfo->mouse_face_beg_row
9358 && *vpos <= dpyinfo->mouse_face_end_row
9359 && (*vpos > dpyinfo->mouse_face_beg_row
9360 || *hpos >= dpyinfo->mouse_face_beg_col)
9361 && (*vpos < dpyinfo->mouse_face_end_row
9362 || *hpos < dpyinfo->mouse_face_end_col
9363 || dpyinfo->mouse_face_past_end))
9364 return 0;
9365
9366 return 1;
9367 }
9368
9369
9370 /* EXPORT:
9371 Handle mouse button event on the tool-bar of frame F, at
9372 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9373 0 for button release. MODIFIERS is event modifiers for button
9374 release. */
9375
9376 void
9377 handle_tool_bar_click (f, x, y, down_p, modifiers)
9378 struct frame *f;
9379 int x, y, down_p;
9380 unsigned int modifiers;
9381 {
9382 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9383 struct window *w = XWINDOW (f->tool_bar_window);
9384 int hpos, vpos, prop_idx;
9385 struct glyph *glyph;
9386 Lisp_Object enabled_p;
9387
9388 /* If not on the highlighted tool-bar item, return. */
9389 frame_to_window_pixel_xy (w, &x, &y);
9390 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9391 return;
9392
9393 /* If item is disabled, do nothing. */
9394 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9395 if (NILP (enabled_p))
9396 return;
9397
9398 if (down_p)
9399 {
9400 /* Show item in pressed state. */
9401 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9402 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9403 last_tool_bar_item = prop_idx;
9404 }
9405 else
9406 {
9407 Lisp_Object key, frame;
9408 struct input_event event;
9409 EVENT_INIT (event);
9410
9411 /* Show item in released state. */
9412 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9413 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9414
9415 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9416
9417 XSETFRAME (frame, f);
9418 event.kind = TOOL_BAR_EVENT;
9419 event.frame_or_window = frame;
9420 event.arg = frame;
9421 kbd_buffer_store_event (&event);
9422
9423 event.kind = TOOL_BAR_EVENT;
9424 event.frame_or_window = frame;
9425 event.arg = key;
9426 event.modifiers = modifiers;
9427 kbd_buffer_store_event (&event);
9428 last_tool_bar_item = -1;
9429 }
9430 }
9431
9432
9433 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9434 tool-bar window-relative coordinates X/Y. Called from
9435 note_mouse_highlight. */
9436
9437 static void
9438 note_tool_bar_highlight (f, x, y)
9439 struct frame *f;
9440 int x, y;
9441 {
9442 Lisp_Object window = f->tool_bar_window;
9443 struct window *w = XWINDOW (window);
9444 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9445 int hpos, vpos;
9446 struct glyph *glyph;
9447 struct glyph_row *row;
9448 int i;
9449 Lisp_Object enabled_p;
9450 int prop_idx;
9451 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9452 int mouse_down_p, rc;
9453
9454 /* Function note_mouse_highlight is called with negative x(y
9455 values when mouse moves outside of the frame. */
9456 if (x <= 0 || y <= 0)
9457 {
9458 clear_mouse_face (dpyinfo);
9459 return;
9460 }
9461
9462 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9463 if (rc < 0)
9464 {
9465 /* Not on tool-bar item. */
9466 clear_mouse_face (dpyinfo);
9467 return;
9468 }
9469 else if (rc == 0)
9470 /* On same tool-bar item as before. */
9471 goto set_help_echo;
9472
9473 clear_mouse_face (dpyinfo);
9474
9475 /* Mouse is down, but on different tool-bar item? */
9476 mouse_down_p = (dpyinfo->grabbed
9477 && f == last_mouse_frame
9478 && FRAME_LIVE_P (f));
9479 if (mouse_down_p
9480 && last_tool_bar_item != prop_idx)
9481 return;
9482
9483 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9484 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9485
9486 /* If tool-bar item is not enabled, don't highlight it. */
9487 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9488 if (!NILP (enabled_p))
9489 {
9490 /* Compute the x-position of the glyph. In front and past the
9491 image is a space. We include this in the highlighted area. */
9492 row = MATRIX_ROW (w->current_matrix, vpos);
9493 for (i = x = 0; i < hpos; ++i)
9494 x += row->glyphs[TEXT_AREA][i].pixel_width;
9495
9496 /* Record this as the current active region. */
9497 dpyinfo->mouse_face_beg_col = hpos;
9498 dpyinfo->mouse_face_beg_row = vpos;
9499 dpyinfo->mouse_face_beg_x = x;
9500 dpyinfo->mouse_face_beg_y = row->y;
9501 dpyinfo->mouse_face_past_end = 0;
9502
9503 dpyinfo->mouse_face_end_col = hpos + 1;
9504 dpyinfo->mouse_face_end_row = vpos;
9505 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9506 dpyinfo->mouse_face_end_y = row->y;
9507 dpyinfo->mouse_face_window = window;
9508 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9509
9510 /* Display it as active. */
9511 show_mouse_face (dpyinfo, draw);
9512 dpyinfo->mouse_face_image_state = draw;
9513 }
9514
9515 set_help_echo:
9516
9517 /* Set help_echo_string to a help string to display for this tool-bar item.
9518 XTread_socket does the rest. */
9519 help_echo_object = help_echo_window = Qnil;
9520 help_echo_pos = -1;
9521 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9522 if (NILP (help_echo_string))
9523 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9524 }
9525
9526 #endif /* HAVE_WINDOW_SYSTEM */
9527
9528
9529 \f
9530 /************************************************************************
9531 Horizontal scrolling
9532 ************************************************************************/
9533
9534 static int hscroll_window_tree P_ ((Lisp_Object));
9535 static int hscroll_windows P_ ((Lisp_Object));
9536
9537 /* For all leaf windows in the window tree rooted at WINDOW, set their
9538 hscroll value so that PT is (i) visible in the window, and (ii) so
9539 that it is not within a certain margin at the window's left and
9540 right border. Value is non-zero if any window's hscroll has been
9541 changed. */
9542
9543 static int
9544 hscroll_window_tree (window)
9545 Lisp_Object window;
9546 {
9547 int hscrolled_p = 0;
9548 int hscroll_relative_p = FLOATP (Vhscroll_step);
9549 int hscroll_step_abs = 0;
9550 double hscroll_step_rel = 0;
9551
9552 if (hscroll_relative_p)
9553 {
9554 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9555 if (hscroll_step_rel < 0)
9556 {
9557 hscroll_relative_p = 0;
9558 hscroll_step_abs = 0;
9559 }
9560 }
9561 else if (INTEGERP (Vhscroll_step))
9562 {
9563 hscroll_step_abs = XINT (Vhscroll_step);
9564 if (hscroll_step_abs < 0)
9565 hscroll_step_abs = 0;
9566 }
9567 else
9568 hscroll_step_abs = 0;
9569
9570 while (WINDOWP (window))
9571 {
9572 struct window *w = XWINDOW (window);
9573
9574 if (WINDOWP (w->hchild))
9575 hscrolled_p |= hscroll_window_tree (w->hchild);
9576 else if (WINDOWP (w->vchild))
9577 hscrolled_p |= hscroll_window_tree (w->vchild);
9578 else if (w->cursor.vpos >= 0)
9579 {
9580 int h_margin;
9581 int text_area_width;
9582 struct glyph_row *current_cursor_row
9583 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9584 struct glyph_row *desired_cursor_row
9585 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9586 struct glyph_row *cursor_row
9587 = (desired_cursor_row->enabled_p
9588 ? desired_cursor_row
9589 : current_cursor_row);
9590
9591 text_area_width = window_box_width (w, TEXT_AREA);
9592
9593 /* Scroll when cursor is inside this scroll margin. */
9594 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9595
9596 if ((XFASTINT (w->hscroll)
9597 && w->cursor.x <= h_margin)
9598 || (cursor_row->enabled_p
9599 && cursor_row->truncated_on_right_p
9600 && (w->cursor.x >= text_area_width - h_margin)))
9601 {
9602 struct it it;
9603 int hscroll;
9604 struct buffer *saved_current_buffer;
9605 int pt;
9606 int wanted_x;
9607
9608 /* Find point in a display of infinite width. */
9609 saved_current_buffer = current_buffer;
9610 current_buffer = XBUFFER (w->buffer);
9611
9612 if (w == XWINDOW (selected_window))
9613 pt = BUF_PT (current_buffer);
9614 else
9615 {
9616 pt = marker_position (w->pointm);
9617 pt = max (BEGV, pt);
9618 pt = min (ZV, pt);
9619 }
9620
9621 /* Move iterator to pt starting at cursor_row->start in
9622 a line with infinite width. */
9623 init_to_row_start (&it, w, cursor_row);
9624 it.last_visible_x = INFINITY;
9625 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9626 current_buffer = saved_current_buffer;
9627
9628 /* Position cursor in window. */
9629 if (!hscroll_relative_p && hscroll_step_abs == 0)
9630 hscroll = max (0, (it.current_x
9631 - (ITERATOR_AT_END_OF_LINE_P (&it)
9632 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9633 : (text_area_width / 2))))
9634 / FRAME_COLUMN_WIDTH (it.f);
9635 else if (w->cursor.x >= text_area_width - h_margin)
9636 {
9637 if (hscroll_relative_p)
9638 wanted_x = text_area_width * (1 - hscroll_step_rel)
9639 - h_margin;
9640 else
9641 wanted_x = text_area_width
9642 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9643 - h_margin;
9644 hscroll
9645 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9646 }
9647 else
9648 {
9649 if (hscroll_relative_p)
9650 wanted_x = text_area_width * hscroll_step_rel
9651 + h_margin;
9652 else
9653 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9654 + h_margin;
9655 hscroll
9656 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9657 }
9658 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9659
9660 /* Don't call Fset_window_hscroll if value hasn't
9661 changed because it will prevent redisplay
9662 optimizations. */
9663 if (XFASTINT (w->hscroll) != hscroll)
9664 {
9665 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9666 w->hscroll = make_number (hscroll);
9667 hscrolled_p = 1;
9668 }
9669 }
9670 }
9671
9672 window = w->next;
9673 }
9674
9675 /* Value is non-zero if hscroll of any leaf window has been changed. */
9676 return hscrolled_p;
9677 }
9678
9679
9680 /* Set hscroll so that cursor is visible and not inside horizontal
9681 scroll margins for all windows in the tree rooted at WINDOW. See
9682 also hscroll_window_tree above. Value is non-zero if any window's
9683 hscroll has been changed. If it has, desired matrices on the frame
9684 of WINDOW are cleared. */
9685
9686 static int
9687 hscroll_windows (window)
9688 Lisp_Object window;
9689 {
9690 int hscrolled_p;
9691
9692 if (automatic_hscrolling_p)
9693 {
9694 hscrolled_p = hscroll_window_tree (window);
9695 if (hscrolled_p)
9696 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9697 }
9698 else
9699 hscrolled_p = 0;
9700 return hscrolled_p;
9701 }
9702
9703
9704 \f
9705 /************************************************************************
9706 Redisplay
9707 ************************************************************************/
9708
9709 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9710 to a non-zero value. This is sometimes handy to have in a debugger
9711 session. */
9712
9713 #if GLYPH_DEBUG
9714
9715 /* First and last unchanged row for try_window_id. */
9716
9717 int debug_first_unchanged_at_end_vpos;
9718 int debug_last_unchanged_at_beg_vpos;
9719
9720 /* Delta vpos and y. */
9721
9722 int debug_dvpos, debug_dy;
9723
9724 /* Delta in characters and bytes for try_window_id. */
9725
9726 int debug_delta, debug_delta_bytes;
9727
9728 /* Values of window_end_pos and window_end_vpos at the end of
9729 try_window_id. */
9730
9731 EMACS_INT debug_end_pos, debug_end_vpos;
9732
9733 /* Append a string to W->desired_matrix->method. FMT is a printf
9734 format string. A1...A9 are a supplement for a variable-length
9735 argument list. If trace_redisplay_p is non-zero also printf the
9736 resulting string to stderr. */
9737
9738 static void
9739 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9740 struct window *w;
9741 char *fmt;
9742 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9743 {
9744 char buffer[512];
9745 char *method = w->desired_matrix->method;
9746 int len = strlen (method);
9747 int size = sizeof w->desired_matrix->method;
9748 int remaining = size - len - 1;
9749
9750 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9751 if (len && remaining)
9752 {
9753 method[len] = '|';
9754 --remaining, ++len;
9755 }
9756
9757 strncpy (method + len, buffer, remaining);
9758
9759 if (trace_redisplay_p)
9760 fprintf (stderr, "%p (%s): %s\n",
9761 w,
9762 ((BUFFERP (w->buffer)
9763 && STRINGP (XBUFFER (w->buffer)->name))
9764 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9765 : "no buffer"),
9766 buffer);
9767 }
9768
9769 #endif /* GLYPH_DEBUG */
9770
9771
9772 /* Value is non-zero if all changes in window W, which displays
9773 current_buffer, are in the text between START and END. START is a
9774 buffer position, END is given as a distance from Z. Used in
9775 redisplay_internal for display optimization. */
9776
9777 static INLINE int
9778 text_outside_line_unchanged_p (w, start, end)
9779 struct window *w;
9780 int start, end;
9781 {
9782 int unchanged_p = 1;
9783
9784 /* If text or overlays have changed, see where. */
9785 if (XFASTINT (w->last_modified) < MODIFF
9786 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9787 {
9788 /* Gap in the line? */
9789 if (GPT < start || Z - GPT < end)
9790 unchanged_p = 0;
9791
9792 /* Changes start in front of the line, or end after it? */
9793 if (unchanged_p
9794 && (BEG_UNCHANGED < start - 1
9795 || END_UNCHANGED < end))
9796 unchanged_p = 0;
9797
9798 /* If selective display, can't optimize if changes start at the
9799 beginning of the line. */
9800 if (unchanged_p
9801 && INTEGERP (current_buffer->selective_display)
9802 && XINT (current_buffer->selective_display) > 0
9803 && (BEG_UNCHANGED < start || GPT <= start))
9804 unchanged_p = 0;
9805
9806 /* If there are overlays at the start or end of the line, these
9807 may have overlay strings with newlines in them. A change at
9808 START, for instance, may actually concern the display of such
9809 overlay strings as well, and they are displayed on different
9810 lines. So, quickly rule out this case. (For the future, it
9811 might be desirable to implement something more telling than
9812 just BEG/END_UNCHANGED.) */
9813 if (unchanged_p)
9814 {
9815 if (BEG + BEG_UNCHANGED == start
9816 && overlay_touches_p (start))
9817 unchanged_p = 0;
9818 if (END_UNCHANGED == end
9819 && overlay_touches_p (Z - end))
9820 unchanged_p = 0;
9821 }
9822 }
9823
9824 return unchanged_p;
9825 }
9826
9827
9828 /* Do a frame update, taking possible shortcuts into account. This is
9829 the main external entry point for redisplay.
9830
9831 If the last redisplay displayed an echo area message and that message
9832 is no longer requested, we clear the echo area or bring back the
9833 mini-buffer if that is in use. */
9834
9835 void
9836 redisplay ()
9837 {
9838 redisplay_internal (0);
9839 }
9840
9841
9842 static Lisp_Object
9843 overlay_arrow_string_or_property (var)
9844 Lisp_Object var;
9845 {
9846 Lisp_Object val;
9847
9848 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9849 return val;
9850
9851 return Voverlay_arrow_string;
9852 }
9853
9854 /* Return 1 if there are any overlay-arrows in current_buffer. */
9855 static int
9856 overlay_arrow_in_current_buffer_p ()
9857 {
9858 Lisp_Object vlist;
9859
9860 for (vlist = Voverlay_arrow_variable_list;
9861 CONSP (vlist);
9862 vlist = XCDR (vlist))
9863 {
9864 Lisp_Object var = XCAR (vlist);
9865 Lisp_Object val;
9866
9867 if (!SYMBOLP (var))
9868 continue;
9869 val = find_symbol_value (var);
9870 if (MARKERP (val)
9871 && current_buffer == XMARKER (val)->buffer)
9872 return 1;
9873 }
9874 return 0;
9875 }
9876
9877
9878 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9879 has changed. */
9880
9881 static int
9882 overlay_arrows_changed_p ()
9883 {
9884 Lisp_Object vlist;
9885
9886 for (vlist = Voverlay_arrow_variable_list;
9887 CONSP (vlist);
9888 vlist = XCDR (vlist))
9889 {
9890 Lisp_Object var = XCAR (vlist);
9891 Lisp_Object val, pstr;
9892
9893 if (!SYMBOLP (var))
9894 continue;
9895 val = find_symbol_value (var);
9896 if (!MARKERP (val))
9897 continue;
9898 if (! EQ (COERCE_MARKER (val),
9899 Fget (var, Qlast_arrow_position))
9900 || ! (pstr = overlay_arrow_string_or_property (var),
9901 EQ (pstr, Fget (var, Qlast_arrow_string))))
9902 return 1;
9903 }
9904 return 0;
9905 }
9906
9907 /* Mark overlay arrows to be updated on next redisplay. */
9908
9909 static void
9910 update_overlay_arrows (up_to_date)
9911 int up_to_date;
9912 {
9913 Lisp_Object vlist;
9914
9915 for (vlist = Voverlay_arrow_variable_list;
9916 CONSP (vlist);
9917 vlist = XCDR (vlist))
9918 {
9919 Lisp_Object var = XCAR (vlist);
9920
9921 if (!SYMBOLP (var))
9922 continue;
9923
9924 if (up_to_date > 0)
9925 {
9926 Lisp_Object val = find_symbol_value (var);
9927 Fput (var, Qlast_arrow_position,
9928 COERCE_MARKER (val));
9929 Fput (var, Qlast_arrow_string,
9930 overlay_arrow_string_or_property (var));
9931 }
9932 else if (up_to_date < 0
9933 || !NILP (Fget (var, Qlast_arrow_position)))
9934 {
9935 Fput (var, Qlast_arrow_position, Qt);
9936 Fput (var, Qlast_arrow_string, Qt);
9937 }
9938 }
9939 }
9940
9941
9942 /* Return overlay arrow string to display at row.
9943 Return integer (bitmap number) for arrow bitmap in left fringe.
9944 Return nil if no overlay arrow. */
9945
9946 static Lisp_Object
9947 overlay_arrow_at_row (it, row)
9948 struct it *it;
9949 struct glyph_row *row;
9950 {
9951 Lisp_Object vlist;
9952
9953 for (vlist = Voverlay_arrow_variable_list;
9954 CONSP (vlist);
9955 vlist = XCDR (vlist))
9956 {
9957 Lisp_Object var = XCAR (vlist);
9958 Lisp_Object val;
9959
9960 if (!SYMBOLP (var))
9961 continue;
9962
9963 val = find_symbol_value (var);
9964
9965 if (MARKERP (val)
9966 && current_buffer == XMARKER (val)->buffer
9967 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9968 {
9969 if (FRAME_WINDOW_P (it->f)
9970 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9971 {
9972 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9973 {
9974 int fringe_bitmap;
9975 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9976 return make_number (fringe_bitmap);
9977 }
9978 return make_number (-1); /* Use default arrow bitmap */
9979 }
9980 return overlay_arrow_string_or_property (var);
9981 }
9982 }
9983
9984 return Qnil;
9985 }
9986
9987 /* Return 1 if point moved out of or into a composition. Otherwise
9988 return 0. PREV_BUF and PREV_PT are the last point buffer and
9989 position. BUF and PT are the current point buffer and position. */
9990
9991 int
9992 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9993 struct buffer *prev_buf, *buf;
9994 int prev_pt, pt;
9995 {
9996 int start, end;
9997 Lisp_Object prop;
9998 Lisp_Object buffer;
9999
10000 XSETBUFFER (buffer, buf);
10001 /* Check a composition at the last point if point moved within the
10002 same buffer. */
10003 if (prev_buf == buf)
10004 {
10005 if (prev_pt == pt)
10006 /* Point didn't move. */
10007 return 0;
10008
10009 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10010 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10011 && COMPOSITION_VALID_P (start, end, prop)
10012 && start < prev_pt && end > prev_pt)
10013 /* The last point was within the composition. Return 1 iff
10014 point moved out of the composition. */
10015 return (pt <= start || pt >= end);
10016 }
10017
10018 /* Check a composition at the current point. */
10019 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10020 && find_composition (pt, -1, &start, &end, &prop, buffer)
10021 && COMPOSITION_VALID_P (start, end, prop)
10022 && start < pt && end > pt);
10023 }
10024
10025
10026 /* Reconsider the setting of B->clip_changed which is displayed
10027 in window W. */
10028
10029 static INLINE void
10030 reconsider_clip_changes (w, b)
10031 struct window *w;
10032 struct buffer *b;
10033 {
10034 if (b->clip_changed
10035 && !NILP (w->window_end_valid)
10036 && w->current_matrix->buffer == b
10037 && w->current_matrix->zv == BUF_ZV (b)
10038 && w->current_matrix->begv == BUF_BEGV (b))
10039 b->clip_changed = 0;
10040
10041 /* If display wasn't paused, and W is not a tool bar window, see if
10042 point has been moved into or out of a composition. In that case,
10043 we set b->clip_changed to 1 to force updating the screen. If
10044 b->clip_changed has already been set to 1, we can skip this
10045 check. */
10046 if (!b->clip_changed
10047 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10048 {
10049 int pt;
10050
10051 if (w == XWINDOW (selected_window))
10052 pt = BUF_PT (current_buffer);
10053 else
10054 pt = marker_position (w->pointm);
10055
10056 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10057 || pt != XINT (w->last_point))
10058 && check_point_in_composition (w->current_matrix->buffer,
10059 XINT (w->last_point),
10060 XBUFFER (w->buffer), pt))
10061 b->clip_changed = 1;
10062 }
10063 }
10064 \f
10065
10066 /* Select FRAME to forward the values of frame-local variables into C
10067 variables so that the redisplay routines can access those values
10068 directly. */
10069
10070 static void
10071 select_frame_for_redisplay (frame)
10072 Lisp_Object frame;
10073 {
10074 Lisp_Object tail, sym, val;
10075 Lisp_Object old = selected_frame;
10076
10077 selected_frame = frame;
10078
10079 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10080 if (CONSP (XCAR (tail))
10081 && (sym = XCAR (XCAR (tail)),
10082 SYMBOLP (sym))
10083 && (sym = indirect_variable (sym),
10084 val = SYMBOL_VALUE (sym),
10085 (BUFFER_LOCAL_VALUEP (val)
10086 || SOME_BUFFER_LOCAL_VALUEP (val)))
10087 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10088 Fsymbol_value (sym);
10089
10090 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10091 if (CONSP (XCAR (tail))
10092 && (sym = XCAR (XCAR (tail)),
10093 SYMBOLP (sym))
10094 && (sym = indirect_variable (sym),
10095 val = SYMBOL_VALUE (sym),
10096 (BUFFER_LOCAL_VALUEP (val)
10097 || SOME_BUFFER_LOCAL_VALUEP (val)))
10098 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10099 Fsymbol_value (sym);
10100 }
10101
10102
10103 #define STOP_POLLING \
10104 do { if (! polling_stopped_here) stop_polling (); \
10105 polling_stopped_here = 1; } while (0)
10106
10107 #define RESUME_POLLING \
10108 do { if (polling_stopped_here) start_polling (); \
10109 polling_stopped_here = 0; } while (0)
10110
10111
10112 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10113 response to any user action; therefore, we should preserve the echo
10114 area. (Actually, our caller does that job.) Perhaps in the future
10115 avoid recentering windows if it is not necessary; currently that
10116 causes some problems. */
10117
10118 static void
10119 redisplay_internal (preserve_echo_area)
10120 int preserve_echo_area;
10121 {
10122 struct window *w = XWINDOW (selected_window);
10123 struct frame *f = XFRAME (w->frame);
10124 int pause;
10125 int must_finish = 0;
10126 struct text_pos tlbufpos, tlendpos;
10127 int number_of_visible_frames;
10128 int count;
10129 struct frame *sf = SELECTED_FRAME ();
10130 int polling_stopped_here = 0;
10131
10132 /* Non-zero means redisplay has to consider all windows on all
10133 frames. Zero means, only selected_window is considered. */
10134 int consider_all_windows_p;
10135
10136 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10137
10138 /* No redisplay if running in batch mode or frame is not yet fully
10139 initialized, or redisplay is explicitly turned off by setting
10140 Vinhibit_redisplay. */
10141 if (noninteractive
10142 || !NILP (Vinhibit_redisplay)
10143 || !f->glyphs_initialized_p)
10144 return;
10145
10146 /* The flag redisplay_performed_directly_p is set by
10147 direct_output_for_insert when it already did the whole screen
10148 update necessary. */
10149 if (redisplay_performed_directly_p)
10150 {
10151 redisplay_performed_directly_p = 0;
10152 if (!hscroll_windows (selected_window))
10153 return;
10154 }
10155
10156 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10157 if (popup_activated ())
10158 return;
10159 #endif
10160
10161 /* I don't think this happens but let's be paranoid. */
10162 if (redisplaying_p)
10163 return;
10164
10165 /* Record a function that resets redisplaying_p to its old value
10166 when we leave this function. */
10167 count = SPECPDL_INDEX ();
10168 record_unwind_protect (unwind_redisplay,
10169 Fcons (make_number (redisplaying_p), selected_frame));
10170 ++redisplaying_p;
10171 specbind (Qinhibit_free_realized_faces, Qnil);
10172
10173 retry:
10174 pause = 0;
10175 reconsider_clip_changes (w, current_buffer);
10176
10177 /* If new fonts have been loaded that make a glyph matrix adjustment
10178 necessary, do it. */
10179 if (fonts_changed_p)
10180 {
10181 adjust_glyphs (NULL);
10182 ++windows_or_buffers_changed;
10183 fonts_changed_p = 0;
10184 }
10185
10186 /* If face_change_count is non-zero, init_iterator will free all
10187 realized faces, which includes the faces referenced from current
10188 matrices. So, we can't reuse current matrices in this case. */
10189 if (face_change_count)
10190 ++windows_or_buffers_changed;
10191
10192 if (! FRAME_WINDOW_P (sf)
10193 && previous_terminal_frame != sf)
10194 {
10195 /* Since frames on an ASCII terminal share the same display
10196 area, displaying a different frame means redisplay the whole
10197 thing. */
10198 windows_or_buffers_changed++;
10199 SET_FRAME_GARBAGED (sf);
10200 XSETFRAME (Vterminal_frame, sf);
10201 }
10202 previous_terminal_frame = sf;
10203
10204 /* Set the visible flags for all frames. Do this before checking
10205 for resized or garbaged frames; they want to know if their frames
10206 are visible. See the comment in frame.h for
10207 FRAME_SAMPLE_VISIBILITY. */
10208 {
10209 Lisp_Object tail, frame;
10210
10211 number_of_visible_frames = 0;
10212
10213 FOR_EACH_FRAME (tail, frame)
10214 {
10215 struct frame *f = XFRAME (frame);
10216
10217 FRAME_SAMPLE_VISIBILITY (f);
10218 if (FRAME_VISIBLE_P (f))
10219 ++number_of_visible_frames;
10220 clear_desired_matrices (f);
10221 }
10222 }
10223
10224 /* Notice any pending interrupt request to change frame size. */
10225 do_pending_window_change (1);
10226
10227 /* Clear frames marked as garbaged. */
10228 if (frame_garbaged)
10229 clear_garbaged_frames ();
10230
10231 /* Build menubar and tool-bar items. */
10232 prepare_menu_bars ();
10233
10234 if (windows_or_buffers_changed)
10235 update_mode_lines++;
10236
10237 /* Detect case that we need to write or remove a star in the mode line. */
10238 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10239 {
10240 w->update_mode_line = Qt;
10241 if (buffer_shared > 1)
10242 update_mode_lines++;
10243 }
10244
10245 /* If %c is in the mode line, update it if needed. */
10246 if (!NILP (w->column_number_displayed)
10247 /* This alternative quickly identifies a common case
10248 where no change is needed. */
10249 && !(PT == XFASTINT (w->last_point)
10250 && XFASTINT (w->last_modified) >= MODIFF
10251 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10252 && (XFASTINT (w->column_number_displayed)
10253 != (int) current_column ())) /* iftc */
10254 w->update_mode_line = Qt;
10255
10256 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10257
10258 /* The variable buffer_shared is set in redisplay_window and
10259 indicates that we redisplay a buffer in different windows. See
10260 there. */
10261 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10262 || cursor_type_changed);
10263
10264 /* If specs for an arrow have changed, do thorough redisplay
10265 to ensure we remove any arrow that should no longer exist. */
10266 if (overlay_arrows_changed_p ())
10267 consider_all_windows_p = windows_or_buffers_changed = 1;
10268
10269 /* Normally the message* functions will have already displayed and
10270 updated the echo area, but the frame may have been trashed, or
10271 the update may have been preempted, so display the echo area
10272 again here. Checking message_cleared_p captures the case that
10273 the echo area should be cleared. */
10274 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10275 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10276 || (message_cleared_p
10277 && minibuf_level == 0
10278 /* If the mini-window is currently selected, this means the
10279 echo-area doesn't show through. */
10280 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10281 {
10282 int window_height_changed_p = echo_area_display (0);
10283 must_finish = 1;
10284
10285 /* If we don't display the current message, don't clear the
10286 message_cleared_p flag, because, if we did, we wouldn't clear
10287 the echo area in the next redisplay which doesn't preserve
10288 the echo area. */
10289 if (!display_last_displayed_message_p)
10290 message_cleared_p = 0;
10291
10292 if (fonts_changed_p)
10293 goto retry;
10294 else if (window_height_changed_p)
10295 {
10296 consider_all_windows_p = 1;
10297 ++update_mode_lines;
10298 ++windows_or_buffers_changed;
10299
10300 /* If window configuration was changed, frames may have been
10301 marked garbaged. Clear them or we will experience
10302 surprises wrt scrolling. */
10303 if (frame_garbaged)
10304 clear_garbaged_frames ();
10305 }
10306 }
10307 else if (EQ (selected_window, minibuf_window)
10308 && (current_buffer->clip_changed
10309 || XFASTINT (w->last_modified) < MODIFF
10310 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10311 && resize_mini_window (w, 0))
10312 {
10313 /* Resized active mini-window to fit the size of what it is
10314 showing if its contents might have changed. */
10315 must_finish = 1;
10316 consider_all_windows_p = 1;
10317 ++windows_or_buffers_changed;
10318 ++update_mode_lines;
10319
10320 /* If window configuration was changed, frames may have been
10321 marked garbaged. Clear them or we will experience
10322 surprises wrt scrolling. */
10323 if (frame_garbaged)
10324 clear_garbaged_frames ();
10325 }
10326
10327
10328 /* If showing the region, and mark has changed, we must redisplay
10329 the whole window. The assignment to this_line_start_pos prevents
10330 the optimization directly below this if-statement. */
10331 if (((!NILP (Vtransient_mark_mode)
10332 && !NILP (XBUFFER (w->buffer)->mark_active))
10333 != !NILP (w->region_showing))
10334 || (!NILP (w->region_showing)
10335 && !EQ (w->region_showing,
10336 Fmarker_position (XBUFFER (w->buffer)->mark))))
10337 CHARPOS (this_line_start_pos) = 0;
10338
10339 /* Optimize the case that only the line containing the cursor in the
10340 selected window has changed. Variables starting with this_ are
10341 set in display_line and record information about the line
10342 containing the cursor. */
10343 tlbufpos = this_line_start_pos;
10344 tlendpos = this_line_end_pos;
10345 if (!consider_all_windows_p
10346 && CHARPOS (tlbufpos) > 0
10347 && NILP (w->update_mode_line)
10348 && !current_buffer->clip_changed
10349 && !current_buffer->prevent_redisplay_optimizations_p
10350 && FRAME_VISIBLE_P (XFRAME (w->frame))
10351 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10352 /* Make sure recorded data applies to current buffer, etc. */
10353 && this_line_buffer == current_buffer
10354 && current_buffer == XBUFFER (w->buffer)
10355 && NILP (w->force_start)
10356 && NILP (w->optional_new_start)
10357 /* Point must be on the line that we have info recorded about. */
10358 && PT >= CHARPOS (tlbufpos)
10359 && PT <= Z - CHARPOS (tlendpos)
10360 /* All text outside that line, including its final newline,
10361 must be unchanged */
10362 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10363 CHARPOS (tlendpos)))
10364 {
10365 if (CHARPOS (tlbufpos) > BEGV
10366 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10367 && (CHARPOS (tlbufpos) == ZV
10368 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10369 /* Former continuation line has disappeared by becoming empty */
10370 goto cancel;
10371 else if (XFASTINT (w->last_modified) < MODIFF
10372 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10373 || MINI_WINDOW_P (w))
10374 {
10375 /* We have to handle the case of continuation around a
10376 wide-column character (See the comment in indent.c around
10377 line 885).
10378
10379 For instance, in the following case:
10380
10381 -------- Insert --------
10382 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10383 J_I_ ==> J_I_ `^^' are cursors.
10384 ^^ ^^
10385 -------- --------
10386
10387 As we have to redraw the line above, we should goto cancel. */
10388
10389 struct it it;
10390 int line_height_before = this_line_pixel_height;
10391
10392 /* Note that start_display will handle the case that the
10393 line starting at tlbufpos is a continuation lines. */
10394 start_display (&it, w, tlbufpos);
10395
10396 /* Implementation note: It this still necessary? */
10397 if (it.current_x != this_line_start_x)
10398 goto cancel;
10399
10400 TRACE ((stderr, "trying display optimization 1\n"));
10401 w->cursor.vpos = -1;
10402 overlay_arrow_seen = 0;
10403 it.vpos = this_line_vpos;
10404 it.current_y = this_line_y;
10405 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10406 display_line (&it);
10407
10408 /* If line contains point, is not continued,
10409 and ends at same distance from eob as before, we win */
10410 if (w->cursor.vpos >= 0
10411 /* Line is not continued, otherwise this_line_start_pos
10412 would have been set to 0 in display_line. */
10413 && CHARPOS (this_line_start_pos)
10414 /* Line ends as before. */
10415 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10416 /* Line has same height as before. Otherwise other lines
10417 would have to be shifted up or down. */
10418 && this_line_pixel_height == line_height_before)
10419 {
10420 /* If this is not the window's last line, we must adjust
10421 the charstarts of the lines below. */
10422 if (it.current_y < it.last_visible_y)
10423 {
10424 struct glyph_row *row
10425 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10426 int delta, delta_bytes;
10427
10428 if (Z - CHARPOS (tlendpos) == ZV)
10429 {
10430 /* This line ends at end of (accessible part of)
10431 buffer. There is no newline to count. */
10432 delta = (Z
10433 - CHARPOS (tlendpos)
10434 - MATRIX_ROW_START_CHARPOS (row));
10435 delta_bytes = (Z_BYTE
10436 - BYTEPOS (tlendpos)
10437 - MATRIX_ROW_START_BYTEPOS (row));
10438 }
10439 else
10440 {
10441 /* This line ends in a newline. Must take
10442 account of the newline and the rest of the
10443 text that follows. */
10444 delta = (Z
10445 - CHARPOS (tlendpos)
10446 - MATRIX_ROW_START_CHARPOS (row));
10447 delta_bytes = (Z_BYTE
10448 - BYTEPOS (tlendpos)
10449 - MATRIX_ROW_START_BYTEPOS (row));
10450 }
10451
10452 increment_matrix_positions (w->current_matrix,
10453 this_line_vpos + 1,
10454 w->current_matrix->nrows,
10455 delta, delta_bytes);
10456 }
10457
10458 /* If this row displays text now but previously didn't,
10459 or vice versa, w->window_end_vpos may have to be
10460 adjusted. */
10461 if ((it.glyph_row - 1)->displays_text_p)
10462 {
10463 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10464 XSETINT (w->window_end_vpos, this_line_vpos);
10465 }
10466 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10467 && this_line_vpos > 0)
10468 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10469 w->window_end_valid = Qnil;
10470
10471 /* Update hint: No need to try to scroll in update_window. */
10472 w->desired_matrix->no_scrolling_p = 1;
10473
10474 #if GLYPH_DEBUG
10475 *w->desired_matrix->method = 0;
10476 debug_method_add (w, "optimization 1");
10477 #endif
10478 #ifdef HAVE_WINDOW_SYSTEM
10479 update_window_fringes (w, 0);
10480 #endif
10481 goto update;
10482 }
10483 else
10484 goto cancel;
10485 }
10486 else if (/* Cursor position hasn't changed. */
10487 PT == XFASTINT (w->last_point)
10488 /* Make sure the cursor was last displayed
10489 in this window. Otherwise we have to reposition it. */
10490 && 0 <= w->cursor.vpos
10491 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10492 {
10493 if (!must_finish)
10494 {
10495 do_pending_window_change (1);
10496
10497 /* We used to always goto end_of_redisplay here, but this
10498 isn't enough if we have a blinking cursor. */
10499 if (w->cursor_off_p == w->last_cursor_off_p)
10500 goto end_of_redisplay;
10501 }
10502 goto update;
10503 }
10504 /* If highlighting the region, or if the cursor is in the echo area,
10505 then we can't just move the cursor. */
10506 else if (! (!NILP (Vtransient_mark_mode)
10507 && !NILP (current_buffer->mark_active))
10508 && (EQ (selected_window, current_buffer->last_selected_window)
10509 || highlight_nonselected_windows)
10510 && NILP (w->region_showing)
10511 && NILP (Vshow_trailing_whitespace)
10512 && !cursor_in_echo_area)
10513 {
10514 struct it it;
10515 struct glyph_row *row;
10516
10517 /* Skip from tlbufpos to PT and see where it is. Note that
10518 PT may be in invisible text. If so, we will end at the
10519 next visible position. */
10520 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10521 NULL, DEFAULT_FACE_ID);
10522 it.current_x = this_line_start_x;
10523 it.current_y = this_line_y;
10524 it.vpos = this_line_vpos;
10525
10526 /* The call to move_it_to stops in front of PT, but
10527 moves over before-strings. */
10528 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10529
10530 if (it.vpos == this_line_vpos
10531 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10532 row->enabled_p))
10533 {
10534 xassert (this_line_vpos == it.vpos);
10535 xassert (this_line_y == it.current_y);
10536 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10537 #if GLYPH_DEBUG
10538 *w->desired_matrix->method = 0;
10539 debug_method_add (w, "optimization 3");
10540 #endif
10541 goto update;
10542 }
10543 else
10544 goto cancel;
10545 }
10546
10547 cancel:
10548 /* Text changed drastically or point moved off of line. */
10549 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10550 }
10551
10552 CHARPOS (this_line_start_pos) = 0;
10553 consider_all_windows_p |= buffer_shared > 1;
10554 ++clear_face_cache_count;
10555 #ifdef HAVE_WINDOW_SYSTEM
10556 ++clear_image_cache_count;
10557 #endif
10558
10559 /* Build desired matrices, and update the display. If
10560 consider_all_windows_p is non-zero, do it for all windows on all
10561 frames. Otherwise do it for selected_window, only. */
10562
10563 if (consider_all_windows_p)
10564 {
10565 Lisp_Object tail, frame;
10566 int i, n = 0, size = 50;
10567 struct frame **updated
10568 = (struct frame **) alloca (size * sizeof *updated);
10569
10570 /* Recompute # windows showing selected buffer. This will be
10571 incremented each time such a window is displayed. */
10572 buffer_shared = 0;
10573
10574 FOR_EACH_FRAME (tail, frame)
10575 {
10576 struct frame *f = XFRAME (frame);
10577
10578 if (FRAME_WINDOW_P (f) || f == sf)
10579 {
10580 if (! EQ (frame, selected_frame))
10581 /* Select the frame, for the sake of frame-local
10582 variables. */
10583 select_frame_for_redisplay (frame);
10584
10585 /* Mark all the scroll bars to be removed; we'll redeem
10586 the ones we want when we redisplay their windows. */
10587 if (condemn_scroll_bars_hook)
10588 condemn_scroll_bars_hook (f);
10589
10590 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10591 redisplay_windows (FRAME_ROOT_WINDOW (f));
10592
10593 /* Any scroll bars which redisplay_windows should have
10594 nuked should now go away. */
10595 if (judge_scroll_bars_hook)
10596 judge_scroll_bars_hook (f);
10597
10598 /* If fonts changed, display again. */
10599 /* ??? rms: I suspect it is a mistake to jump all the way
10600 back to retry here. It should just retry this frame. */
10601 if (fonts_changed_p)
10602 goto retry;
10603
10604 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10605 {
10606 /* See if we have to hscroll. */
10607 if (hscroll_windows (f->root_window))
10608 goto retry;
10609
10610 /* Prevent various kinds of signals during display
10611 update. stdio is not robust about handling
10612 signals, which can cause an apparent I/O
10613 error. */
10614 if (interrupt_input)
10615 unrequest_sigio ();
10616 STOP_POLLING;
10617
10618 /* Update the display. */
10619 set_window_update_flags (XWINDOW (f->root_window), 1);
10620 pause |= update_frame (f, 0, 0);
10621 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10622 if (pause)
10623 break;
10624 #endif
10625
10626 if (n == size)
10627 {
10628 int nbytes = size * sizeof *updated;
10629 struct frame **p = (struct frame **) alloca (2 * nbytes);
10630 bcopy (updated, p, nbytes);
10631 size *= 2;
10632 }
10633
10634 updated[n++] = f;
10635 }
10636 }
10637 }
10638
10639 if (!pause)
10640 {
10641 /* Do the mark_window_display_accurate after all windows have
10642 been redisplayed because this call resets flags in buffers
10643 which are needed for proper redisplay. */
10644 for (i = 0; i < n; ++i)
10645 {
10646 struct frame *f = updated[i];
10647 mark_window_display_accurate (f->root_window, 1);
10648 if (frame_up_to_date_hook)
10649 frame_up_to_date_hook (f);
10650 }
10651 }
10652 }
10653 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10654 {
10655 Lisp_Object mini_window;
10656 struct frame *mini_frame;
10657
10658 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10659 /* Use list_of_error, not Qerror, so that
10660 we catch only errors and don't run the debugger. */
10661 internal_condition_case_1 (redisplay_window_1, selected_window,
10662 list_of_error,
10663 redisplay_window_error);
10664
10665 /* Compare desired and current matrices, perform output. */
10666
10667 update:
10668 /* If fonts changed, display again. */
10669 if (fonts_changed_p)
10670 goto retry;
10671
10672 /* Prevent various kinds of signals during display update.
10673 stdio is not robust about handling signals,
10674 which can cause an apparent I/O error. */
10675 if (interrupt_input)
10676 unrequest_sigio ();
10677 STOP_POLLING;
10678
10679 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10680 {
10681 if (hscroll_windows (selected_window))
10682 goto retry;
10683
10684 XWINDOW (selected_window)->must_be_updated_p = 1;
10685 pause = update_frame (sf, 0, 0);
10686 }
10687
10688 /* We may have called echo_area_display at the top of this
10689 function. If the echo area is on another frame, that may
10690 have put text on a frame other than the selected one, so the
10691 above call to update_frame would not have caught it. Catch
10692 it here. */
10693 mini_window = FRAME_MINIBUF_WINDOW (sf);
10694 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10695
10696 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10697 {
10698 XWINDOW (mini_window)->must_be_updated_p = 1;
10699 pause |= update_frame (mini_frame, 0, 0);
10700 if (!pause && hscroll_windows (mini_window))
10701 goto retry;
10702 }
10703 }
10704
10705 /* If display was paused because of pending input, make sure we do a
10706 thorough update the next time. */
10707 if (pause)
10708 {
10709 /* Prevent the optimization at the beginning of
10710 redisplay_internal that tries a single-line update of the
10711 line containing the cursor in the selected window. */
10712 CHARPOS (this_line_start_pos) = 0;
10713
10714 /* Let the overlay arrow be updated the next time. */
10715 update_overlay_arrows (0);
10716
10717 /* If we pause after scrolling, some rows in the current
10718 matrices of some windows are not valid. */
10719 if (!WINDOW_FULL_WIDTH_P (w)
10720 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10721 update_mode_lines = 1;
10722 }
10723 else
10724 {
10725 if (!consider_all_windows_p)
10726 {
10727 /* This has already been done above if
10728 consider_all_windows_p is set. */
10729 mark_window_display_accurate_1 (w, 1);
10730
10731 /* Say overlay arrows are up to date. */
10732 update_overlay_arrows (1);
10733
10734 if (frame_up_to_date_hook != 0)
10735 frame_up_to_date_hook (sf);
10736 }
10737
10738 update_mode_lines = 0;
10739 windows_or_buffers_changed = 0;
10740 cursor_type_changed = 0;
10741 }
10742
10743 /* Start SIGIO interrupts coming again. Having them off during the
10744 code above makes it less likely one will discard output, but not
10745 impossible, since there might be stuff in the system buffer here.
10746 But it is much hairier to try to do anything about that. */
10747 if (interrupt_input)
10748 request_sigio ();
10749 RESUME_POLLING;
10750
10751 /* If a frame has become visible which was not before, redisplay
10752 again, so that we display it. Expose events for such a frame
10753 (which it gets when becoming visible) don't call the parts of
10754 redisplay constructing glyphs, so simply exposing a frame won't
10755 display anything in this case. So, we have to display these
10756 frames here explicitly. */
10757 if (!pause)
10758 {
10759 Lisp_Object tail, frame;
10760 int new_count = 0;
10761
10762 FOR_EACH_FRAME (tail, frame)
10763 {
10764 int this_is_visible = 0;
10765
10766 if (XFRAME (frame)->visible)
10767 this_is_visible = 1;
10768 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10769 if (XFRAME (frame)->visible)
10770 this_is_visible = 1;
10771
10772 if (this_is_visible)
10773 new_count++;
10774 }
10775
10776 if (new_count != number_of_visible_frames)
10777 windows_or_buffers_changed++;
10778 }
10779
10780 /* Change frame size now if a change is pending. */
10781 do_pending_window_change (1);
10782
10783 /* If we just did a pending size change, or have additional
10784 visible frames, redisplay again. */
10785 if (windows_or_buffers_changed && !pause)
10786 goto retry;
10787
10788 /* Clear the face cache eventually. */
10789 if (consider_all_windows_p)
10790 {
10791 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10792 {
10793 clear_face_cache (0);
10794 clear_face_cache_count = 0;
10795 }
10796 #ifdef HAVE_WINDOW_SYSTEM
10797 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10798 {
10799 Lisp_Object tail, frame;
10800 FOR_EACH_FRAME (tail, frame)
10801 {
10802 struct frame *f = XFRAME (frame);
10803 if (FRAME_WINDOW_P (f))
10804 clear_image_cache (f, 0);
10805 }
10806 clear_image_cache_count = 0;
10807 }
10808 #endif /* HAVE_WINDOW_SYSTEM */
10809 }
10810
10811 end_of_redisplay:
10812 unbind_to (count, Qnil);
10813 RESUME_POLLING;
10814 }
10815
10816
10817 /* Redisplay, but leave alone any recent echo area message unless
10818 another message has been requested in its place.
10819
10820 This is useful in situations where you need to redisplay but no
10821 user action has occurred, making it inappropriate for the message
10822 area to be cleared. See tracking_off and
10823 wait_reading_process_output for examples of these situations.
10824
10825 FROM_WHERE is an integer saying from where this function was
10826 called. This is useful for debugging. */
10827
10828 void
10829 redisplay_preserve_echo_area (from_where)
10830 int from_where;
10831 {
10832 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10833
10834 if (!NILP (echo_area_buffer[1]))
10835 {
10836 /* We have a previously displayed message, but no current
10837 message. Redisplay the previous message. */
10838 display_last_displayed_message_p = 1;
10839 redisplay_internal (1);
10840 display_last_displayed_message_p = 0;
10841 }
10842 else
10843 redisplay_internal (1);
10844
10845 if (rif != NULL && rif->flush_display_optional)
10846 rif->flush_display_optional (NULL);
10847 }
10848
10849
10850 /* Function registered with record_unwind_protect in
10851 redisplay_internal. Reset redisplaying_p to the value it had
10852 before redisplay_internal was called, and clear
10853 prevent_freeing_realized_faces_p. It also selects the previously
10854 selected frame. */
10855
10856 static Lisp_Object
10857 unwind_redisplay (val)
10858 Lisp_Object val;
10859 {
10860 Lisp_Object old_redisplaying_p, old_frame;
10861
10862 old_redisplaying_p = XCAR (val);
10863 redisplaying_p = XFASTINT (old_redisplaying_p);
10864 old_frame = XCDR (val);
10865 if (! EQ (old_frame, selected_frame))
10866 select_frame_for_redisplay (old_frame);
10867 return Qnil;
10868 }
10869
10870
10871 /* Mark the display of window W as accurate or inaccurate. If
10872 ACCURATE_P is non-zero mark display of W as accurate. If
10873 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10874 redisplay_internal is called. */
10875
10876 static void
10877 mark_window_display_accurate_1 (w, accurate_p)
10878 struct window *w;
10879 int accurate_p;
10880 {
10881 if (BUFFERP (w->buffer))
10882 {
10883 struct buffer *b = XBUFFER (w->buffer);
10884
10885 w->last_modified
10886 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10887 w->last_overlay_modified
10888 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10889 w->last_had_star
10890 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10891
10892 if (accurate_p)
10893 {
10894 b->clip_changed = 0;
10895 b->prevent_redisplay_optimizations_p = 0;
10896
10897 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10898 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10899 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10900 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10901
10902 w->current_matrix->buffer = b;
10903 w->current_matrix->begv = BUF_BEGV (b);
10904 w->current_matrix->zv = BUF_ZV (b);
10905
10906 w->last_cursor = w->cursor;
10907 w->last_cursor_off_p = w->cursor_off_p;
10908
10909 if (w == XWINDOW (selected_window))
10910 w->last_point = make_number (BUF_PT (b));
10911 else
10912 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10913 }
10914 }
10915
10916 if (accurate_p)
10917 {
10918 w->window_end_valid = w->buffer;
10919 #if 0 /* This is incorrect with variable-height lines. */
10920 xassert (XINT (w->window_end_vpos)
10921 < (WINDOW_TOTAL_LINES (w)
10922 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10923 #endif
10924 w->update_mode_line = Qnil;
10925 }
10926 }
10927
10928
10929 /* Mark the display of windows in the window tree rooted at WINDOW as
10930 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10931 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10932 be redisplayed the next time redisplay_internal is called. */
10933
10934 void
10935 mark_window_display_accurate (window, accurate_p)
10936 Lisp_Object window;
10937 int accurate_p;
10938 {
10939 struct window *w;
10940
10941 for (; !NILP (window); window = w->next)
10942 {
10943 w = XWINDOW (window);
10944 mark_window_display_accurate_1 (w, accurate_p);
10945
10946 if (!NILP (w->vchild))
10947 mark_window_display_accurate (w->vchild, accurate_p);
10948 if (!NILP (w->hchild))
10949 mark_window_display_accurate (w->hchild, accurate_p);
10950 }
10951
10952 if (accurate_p)
10953 {
10954 update_overlay_arrows (1);
10955 }
10956 else
10957 {
10958 /* Force a thorough redisplay the next time by setting
10959 last_arrow_position and last_arrow_string to t, which is
10960 unequal to any useful value of Voverlay_arrow_... */
10961 update_overlay_arrows (-1);
10962 }
10963 }
10964
10965
10966 /* Return value in display table DP (Lisp_Char_Table *) for character
10967 C. Since a display table doesn't have any parent, we don't have to
10968 follow parent. Do not call this function directly but use the
10969 macro DISP_CHAR_VECTOR. */
10970
10971 Lisp_Object
10972 disp_char_vector (dp, c)
10973 struct Lisp_Char_Table *dp;
10974 int c;
10975 {
10976 int code[4], i;
10977 Lisp_Object val;
10978
10979 if (SINGLE_BYTE_CHAR_P (c))
10980 return (dp->contents[c]);
10981
10982 SPLIT_CHAR (c, code[0], code[1], code[2]);
10983 if (code[1] < 32)
10984 code[1] = -1;
10985 else if (code[2] < 32)
10986 code[2] = -1;
10987
10988 /* Here, the possible range of code[0] (== charset ID) is
10989 128..max_charset. Since the top level char table contains data
10990 for multibyte characters after 256th element, we must increment
10991 code[0] by 128 to get a correct index. */
10992 code[0] += 128;
10993 code[3] = -1; /* anchor */
10994
10995 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10996 {
10997 val = dp->contents[code[i]];
10998 if (!SUB_CHAR_TABLE_P (val))
10999 return (NILP (val) ? dp->defalt : val);
11000 }
11001
11002 /* Here, val is a sub char table. We return the default value of
11003 it. */
11004 return (dp->defalt);
11005 }
11006
11007
11008 \f
11009 /***********************************************************************
11010 Window Redisplay
11011 ***********************************************************************/
11012
11013 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11014
11015 static void
11016 redisplay_windows (window)
11017 Lisp_Object window;
11018 {
11019 while (!NILP (window))
11020 {
11021 struct window *w = XWINDOW (window);
11022
11023 if (!NILP (w->hchild))
11024 redisplay_windows (w->hchild);
11025 else if (!NILP (w->vchild))
11026 redisplay_windows (w->vchild);
11027 else
11028 {
11029 displayed_buffer = XBUFFER (w->buffer);
11030 /* Use list_of_error, not Qerror, so that
11031 we catch only errors and don't run the debugger. */
11032 internal_condition_case_1 (redisplay_window_0, window,
11033 list_of_error,
11034 redisplay_window_error);
11035 }
11036
11037 window = w->next;
11038 }
11039 }
11040
11041 static Lisp_Object
11042 redisplay_window_error ()
11043 {
11044 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11045 return Qnil;
11046 }
11047
11048 static Lisp_Object
11049 redisplay_window_0 (window)
11050 Lisp_Object window;
11051 {
11052 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11053 redisplay_window (window, 0);
11054 return Qnil;
11055 }
11056
11057 static Lisp_Object
11058 redisplay_window_1 (window)
11059 Lisp_Object window;
11060 {
11061 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11062 redisplay_window (window, 1);
11063 return Qnil;
11064 }
11065 \f
11066
11067 /* Increment GLYPH until it reaches END or CONDITION fails while
11068 adding (GLYPH)->pixel_width to X. */
11069
11070 #define SKIP_GLYPHS(glyph, end, x, condition) \
11071 do \
11072 { \
11073 (x) += (glyph)->pixel_width; \
11074 ++(glyph); \
11075 } \
11076 while ((glyph) < (end) && (condition))
11077
11078
11079 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11080 DELTA is the number of bytes by which positions recorded in ROW
11081 differ from current buffer positions. */
11082
11083 void
11084 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11085 struct window *w;
11086 struct glyph_row *row;
11087 struct glyph_matrix *matrix;
11088 int delta, delta_bytes, dy, dvpos;
11089 {
11090 struct glyph *glyph = row->glyphs[TEXT_AREA];
11091 struct glyph *end = glyph + row->used[TEXT_AREA];
11092 struct glyph *cursor = NULL;
11093 /* The first glyph that starts a sequence of glyphs from string. */
11094 struct glyph *string_start;
11095 /* The X coordinate of string_start. */
11096 int string_start_x;
11097 /* The last known character position. */
11098 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11099 /* The last known character position before string_start. */
11100 int string_before_pos;
11101 int x = row->x;
11102 int cursor_x = x;
11103 int cursor_from_overlay_pos = 0;
11104 int pt_old = PT - delta;
11105
11106 /* Skip over glyphs not having an object at the start of the row.
11107 These are special glyphs like truncation marks on terminal
11108 frames. */
11109 if (row->displays_text_p)
11110 while (glyph < end
11111 && INTEGERP (glyph->object)
11112 && glyph->charpos < 0)
11113 {
11114 x += glyph->pixel_width;
11115 ++glyph;
11116 }
11117
11118 string_start = NULL;
11119 while (glyph < end
11120 && !INTEGERP (glyph->object)
11121 && (!BUFFERP (glyph->object)
11122 || (last_pos = glyph->charpos) < pt_old))
11123 {
11124 if (! STRINGP (glyph->object))
11125 {
11126 string_start = NULL;
11127 x += glyph->pixel_width;
11128 ++glyph;
11129 if (cursor_from_overlay_pos
11130 && last_pos > cursor_from_overlay_pos)
11131 {
11132 cursor_from_overlay_pos = 0;
11133 cursor = 0;
11134 }
11135 }
11136 else
11137 {
11138 string_before_pos = last_pos;
11139 string_start = glyph;
11140 string_start_x = x;
11141 /* Skip all glyphs from string. */
11142 do
11143 {
11144 int pos;
11145 if ((cursor == NULL || glyph > cursor)
11146 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11147 Qcursor, (glyph)->object))
11148 && (pos = string_buffer_position (w, glyph->object,
11149 string_before_pos),
11150 (pos == 0 /* From overlay */
11151 || pos == pt_old)))
11152 {
11153 /* Estimate overlay buffer position from the buffer
11154 positions of the glyphs before and after the overlay.
11155 Add 1 to last_pos so that if point corresponds to the
11156 glyph right after the overlay, we still use a 'cursor'
11157 property found in that overlay. */
11158 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11159 cursor = glyph;
11160 cursor_x = x;
11161 }
11162 x += glyph->pixel_width;
11163 ++glyph;
11164 }
11165 while (glyph < end && STRINGP (glyph->object));
11166 }
11167 }
11168
11169 if (cursor != NULL)
11170 {
11171 glyph = cursor;
11172 x = cursor_x;
11173 }
11174 else if (row->ends_in_ellipsis_p && glyph == end)
11175 {
11176 /* Scan back over the ellipsis glyphs, decrementing positions. */
11177 while (glyph > row->glyphs[TEXT_AREA]
11178 && (glyph - 1)->charpos == last_pos)
11179 glyph--, x -= glyph->pixel_width;
11180 /* That loop always goes one position too far,
11181 including the glyph before the ellipsis.
11182 So scan forward over that one. */
11183 x += glyph->pixel_width;
11184 glyph++;
11185 }
11186 else if (string_start
11187 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11188 {
11189 /* We may have skipped over point because the previous glyphs
11190 are from string. As there's no easy way to know the
11191 character position of the current glyph, find the correct
11192 glyph on point by scanning from string_start again. */
11193 Lisp_Object limit;
11194 Lisp_Object string;
11195 int pos;
11196
11197 limit = make_number (pt_old + 1);
11198 end = glyph;
11199 glyph = string_start;
11200 x = string_start_x;
11201 string = glyph->object;
11202 pos = string_buffer_position (w, string, string_before_pos);
11203 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11204 because we always put cursor after overlay strings. */
11205 while (pos == 0 && glyph < end)
11206 {
11207 string = glyph->object;
11208 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11209 if (glyph < end)
11210 pos = string_buffer_position (w, glyph->object, string_before_pos);
11211 }
11212
11213 while (glyph < end)
11214 {
11215 pos = XINT (Fnext_single_char_property_change
11216 (make_number (pos), Qdisplay, Qnil, limit));
11217 if (pos > pt_old)
11218 break;
11219 /* Skip glyphs from the same string. */
11220 string = glyph->object;
11221 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11222 /* Skip glyphs from an overlay. */
11223 while (glyph < end
11224 && ! string_buffer_position (w, glyph->object, pos))
11225 {
11226 string = glyph->object;
11227 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11228 }
11229 }
11230 }
11231
11232 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11233 w->cursor.x = x;
11234 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11235 w->cursor.y = row->y + dy;
11236
11237 if (w == XWINDOW (selected_window))
11238 {
11239 if (!row->continued_p
11240 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11241 && row->x == 0)
11242 {
11243 this_line_buffer = XBUFFER (w->buffer);
11244
11245 CHARPOS (this_line_start_pos)
11246 = MATRIX_ROW_START_CHARPOS (row) + delta;
11247 BYTEPOS (this_line_start_pos)
11248 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11249
11250 CHARPOS (this_line_end_pos)
11251 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11252 BYTEPOS (this_line_end_pos)
11253 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11254
11255 this_line_y = w->cursor.y;
11256 this_line_pixel_height = row->height;
11257 this_line_vpos = w->cursor.vpos;
11258 this_line_start_x = row->x;
11259 }
11260 else
11261 CHARPOS (this_line_start_pos) = 0;
11262 }
11263 }
11264
11265
11266 /* Run window scroll functions, if any, for WINDOW with new window
11267 start STARTP. Sets the window start of WINDOW to that position.
11268
11269 We assume that the window's buffer is really current. */
11270
11271 static INLINE struct text_pos
11272 run_window_scroll_functions (window, startp)
11273 Lisp_Object window;
11274 struct text_pos startp;
11275 {
11276 struct window *w = XWINDOW (window);
11277 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11278
11279 if (current_buffer != XBUFFER (w->buffer))
11280 abort ();
11281
11282 if (!NILP (Vwindow_scroll_functions))
11283 {
11284 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11285 make_number (CHARPOS (startp)));
11286 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11287 /* In case the hook functions switch buffers. */
11288 if (current_buffer != XBUFFER (w->buffer))
11289 set_buffer_internal_1 (XBUFFER (w->buffer));
11290 }
11291
11292 return startp;
11293 }
11294
11295
11296 /* Make sure the line containing the cursor is fully visible.
11297 A value of 1 means there is nothing to be done.
11298 (Either the line is fully visible, or it cannot be made so,
11299 or we cannot tell.)
11300
11301 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11302 is higher than window.
11303
11304 A value of 0 means the caller should do scrolling
11305 as if point had gone off the screen. */
11306
11307 static int
11308 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11309 struct window *w;
11310 int force_p;
11311 {
11312 struct glyph_matrix *matrix;
11313 struct glyph_row *row;
11314 int window_height;
11315
11316 if (!make_cursor_line_fully_visible_p)
11317 return 1;
11318
11319 /* It's not always possible to find the cursor, e.g, when a window
11320 is full of overlay strings. Don't do anything in that case. */
11321 if (w->cursor.vpos < 0)
11322 return 1;
11323
11324 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11325 row = MATRIX_ROW (matrix, w->cursor.vpos);
11326
11327 /* If the cursor row is not partially visible, there's nothing to do. */
11328 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11329 return 1;
11330
11331 /* If the row the cursor is in is taller than the window's height,
11332 it's not clear what to do, so do nothing. */
11333 window_height = window_box_height (w);
11334 if (row->height >= window_height)
11335 {
11336 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11337 return 1;
11338 }
11339 return 0;
11340
11341 #if 0
11342 /* This code used to try to scroll the window just enough to make
11343 the line visible. It returned 0 to say that the caller should
11344 allocate larger glyph matrices. */
11345
11346 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11347 {
11348 int dy = row->height - row->visible_height;
11349 w->vscroll = 0;
11350 w->cursor.y += dy;
11351 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11352 }
11353 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11354 {
11355 int dy = - (row->height - row->visible_height);
11356 w->vscroll = dy;
11357 w->cursor.y += dy;
11358 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11359 }
11360
11361 /* When we change the cursor y-position of the selected window,
11362 change this_line_y as well so that the display optimization for
11363 the cursor line of the selected window in redisplay_internal uses
11364 the correct y-position. */
11365 if (w == XWINDOW (selected_window))
11366 this_line_y = w->cursor.y;
11367
11368 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11369 redisplay with larger matrices. */
11370 if (matrix->nrows < required_matrix_height (w))
11371 {
11372 fonts_changed_p = 1;
11373 return 0;
11374 }
11375
11376 return 1;
11377 #endif /* 0 */
11378 }
11379
11380
11381 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11382 non-zero means only WINDOW is redisplayed in redisplay_internal.
11383 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11384 in redisplay_window to bring a partially visible line into view in
11385 the case that only the cursor has moved.
11386
11387 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11388 last screen line's vertical height extends past the end of the screen.
11389
11390 Value is
11391
11392 1 if scrolling succeeded
11393
11394 0 if scrolling didn't find point.
11395
11396 -1 if new fonts have been loaded so that we must interrupt
11397 redisplay, adjust glyph matrices, and try again. */
11398
11399 enum
11400 {
11401 SCROLLING_SUCCESS,
11402 SCROLLING_FAILED,
11403 SCROLLING_NEED_LARGER_MATRICES
11404 };
11405
11406 static int
11407 try_scrolling (window, just_this_one_p, scroll_conservatively,
11408 scroll_step, temp_scroll_step, last_line_misfit)
11409 Lisp_Object window;
11410 int just_this_one_p;
11411 EMACS_INT scroll_conservatively, scroll_step;
11412 int temp_scroll_step;
11413 int last_line_misfit;
11414 {
11415 struct window *w = XWINDOW (window);
11416 struct frame *f = XFRAME (w->frame);
11417 struct text_pos scroll_margin_pos;
11418 struct text_pos pos;
11419 struct text_pos startp;
11420 struct it it;
11421 Lisp_Object window_end;
11422 int this_scroll_margin;
11423 int dy = 0;
11424 int scroll_max;
11425 int rc;
11426 int amount_to_scroll = 0;
11427 Lisp_Object aggressive;
11428 int height;
11429 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11430
11431 #if GLYPH_DEBUG
11432 debug_method_add (w, "try_scrolling");
11433 #endif
11434
11435 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11436
11437 /* Compute scroll margin height in pixels. We scroll when point is
11438 within this distance from the top or bottom of the window. */
11439 if (scroll_margin > 0)
11440 {
11441 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11442 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11443 }
11444 else
11445 this_scroll_margin = 0;
11446
11447 /* Force scroll_conservatively to have a reasonable value so it doesn't
11448 cause an overflow while computing how much to scroll. */
11449 if (scroll_conservatively)
11450 scroll_conservatively = min (scroll_conservatively,
11451 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11452
11453 /* Compute how much we should try to scroll maximally to bring point
11454 into view. */
11455 if (scroll_step || scroll_conservatively || temp_scroll_step)
11456 scroll_max = max (scroll_step,
11457 max (scroll_conservatively, temp_scroll_step));
11458 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11459 || NUMBERP (current_buffer->scroll_up_aggressively))
11460 /* We're trying to scroll because of aggressive scrolling
11461 but no scroll_step is set. Choose an arbitrary one. Maybe
11462 there should be a variable for this. */
11463 scroll_max = 10;
11464 else
11465 scroll_max = 0;
11466 scroll_max *= FRAME_LINE_HEIGHT (f);
11467
11468 /* Decide whether we have to scroll down. Start at the window end
11469 and move this_scroll_margin up to find the position of the scroll
11470 margin. */
11471 window_end = Fwindow_end (window, Qt);
11472
11473 too_near_end:
11474
11475 CHARPOS (scroll_margin_pos) = XINT (window_end);
11476 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11477
11478 if (this_scroll_margin || extra_scroll_margin_lines)
11479 {
11480 start_display (&it, w, scroll_margin_pos);
11481 if (this_scroll_margin)
11482 move_it_vertically_backward (&it, this_scroll_margin);
11483 if (extra_scroll_margin_lines)
11484 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11485 scroll_margin_pos = it.current.pos;
11486 }
11487
11488 if (PT >= CHARPOS (scroll_margin_pos))
11489 {
11490 int y0;
11491
11492 /* Point is in the scroll margin at the bottom of the window, or
11493 below. Compute a new window start that makes point visible. */
11494
11495 /* Compute the distance from the scroll margin to PT.
11496 Give up if the distance is greater than scroll_max. */
11497 start_display (&it, w, scroll_margin_pos);
11498 y0 = it.current_y;
11499 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11500 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11501
11502 /* To make point visible, we have to move the window start
11503 down so that the line the cursor is in is visible, which
11504 means we have to add in the height of the cursor line. */
11505 dy = line_bottom_y (&it) - y0;
11506
11507 if (dy > scroll_max)
11508 return SCROLLING_FAILED;
11509
11510 /* Move the window start down. If scrolling conservatively,
11511 move it just enough down to make point visible. If
11512 scroll_step is set, move it down by scroll_step. */
11513 start_display (&it, w, startp);
11514
11515 if (scroll_conservatively)
11516 /* Set AMOUNT_TO_SCROLL to at least one line,
11517 and at most scroll_conservatively lines. */
11518 amount_to_scroll
11519 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11520 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11521 else if (scroll_step || temp_scroll_step)
11522 amount_to_scroll = scroll_max;
11523 else
11524 {
11525 aggressive = current_buffer->scroll_up_aggressively;
11526 height = WINDOW_BOX_TEXT_HEIGHT (w);
11527 if (NUMBERP (aggressive))
11528 {
11529 double float_amount = XFLOATINT (aggressive) * height;
11530 amount_to_scroll = float_amount;
11531 if (amount_to_scroll == 0 && float_amount > 0)
11532 amount_to_scroll = 1;
11533 }
11534 }
11535
11536 if (amount_to_scroll <= 0)
11537 return SCROLLING_FAILED;
11538
11539 /* If moving by amount_to_scroll leaves STARTP unchanged,
11540 move it down one screen line. */
11541
11542 move_it_vertically (&it, amount_to_scroll);
11543 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11544 move_it_by_lines (&it, 1, 1);
11545 startp = it.current.pos;
11546 }
11547 else
11548 {
11549 /* See if point is inside the scroll margin at the top of the
11550 window. */
11551 scroll_margin_pos = startp;
11552 if (this_scroll_margin)
11553 {
11554 start_display (&it, w, startp);
11555 move_it_vertically (&it, this_scroll_margin);
11556 scroll_margin_pos = it.current.pos;
11557 }
11558
11559 if (PT < CHARPOS (scroll_margin_pos))
11560 {
11561 /* Point is in the scroll margin at the top of the window or
11562 above what is displayed in the window. */
11563 int y0;
11564
11565 /* Compute the vertical distance from PT to the scroll
11566 margin position. Give up if distance is greater than
11567 scroll_max. */
11568 SET_TEXT_POS (pos, PT, PT_BYTE);
11569 start_display (&it, w, pos);
11570 y0 = it.current_y;
11571 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11572 it.last_visible_y, -1,
11573 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11574 dy = it.current_y - y0;
11575 if (dy > scroll_max)
11576 return SCROLLING_FAILED;
11577
11578 /* Compute new window start. */
11579 start_display (&it, w, startp);
11580
11581 if (scroll_conservatively)
11582 amount_to_scroll
11583 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11584 else if (scroll_step || temp_scroll_step)
11585 amount_to_scroll = scroll_max;
11586 else
11587 {
11588 aggressive = current_buffer->scroll_down_aggressively;
11589 height = WINDOW_BOX_TEXT_HEIGHT (w);
11590 if (NUMBERP (aggressive))
11591 {
11592 double float_amount = XFLOATINT (aggressive) * height;
11593 amount_to_scroll = float_amount;
11594 if (amount_to_scroll == 0 && float_amount > 0)
11595 amount_to_scroll = 1;
11596 }
11597 }
11598
11599 if (amount_to_scroll <= 0)
11600 return SCROLLING_FAILED;
11601
11602 move_it_vertically_backward (&it, amount_to_scroll);
11603 startp = it.current.pos;
11604 }
11605 }
11606
11607 /* Run window scroll functions. */
11608 startp = run_window_scroll_functions (window, startp);
11609
11610 /* Display the window. Give up if new fonts are loaded, or if point
11611 doesn't appear. */
11612 if (!try_window (window, startp, 0))
11613 rc = SCROLLING_NEED_LARGER_MATRICES;
11614 else if (w->cursor.vpos < 0)
11615 {
11616 clear_glyph_matrix (w->desired_matrix);
11617 rc = SCROLLING_FAILED;
11618 }
11619 else
11620 {
11621 /* Maybe forget recorded base line for line number display. */
11622 if (!just_this_one_p
11623 || current_buffer->clip_changed
11624 || BEG_UNCHANGED < CHARPOS (startp))
11625 w->base_line_number = Qnil;
11626
11627 /* If cursor ends up on a partially visible line,
11628 treat that as being off the bottom of the screen. */
11629 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11630 {
11631 clear_glyph_matrix (w->desired_matrix);
11632 ++extra_scroll_margin_lines;
11633 goto too_near_end;
11634 }
11635 rc = SCROLLING_SUCCESS;
11636 }
11637
11638 return rc;
11639 }
11640
11641
11642 /* Compute a suitable window start for window W if display of W starts
11643 on a continuation line. Value is non-zero if a new window start
11644 was computed.
11645
11646 The new window start will be computed, based on W's width, starting
11647 from the start of the continued line. It is the start of the
11648 screen line with the minimum distance from the old start W->start. */
11649
11650 static int
11651 compute_window_start_on_continuation_line (w)
11652 struct window *w;
11653 {
11654 struct text_pos pos, start_pos;
11655 int window_start_changed_p = 0;
11656
11657 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11658
11659 /* If window start is on a continuation line... Window start may be
11660 < BEGV in case there's invisible text at the start of the
11661 buffer (M-x rmail, for example). */
11662 if (CHARPOS (start_pos) > BEGV
11663 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11664 {
11665 struct it it;
11666 struct glyph_row *row;
11667
11668 /* Handle the case that the window start is out of range. */
11669 if (CHARPOS (start_pos) < BEGV)
11670 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11671 else if (CHARPOS (start_pos) > ZV)
11672 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11673
11674 /* Find the start of the continued line. This should be fast
11675 because scan_buffer is fast (newline cache). */
11676 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11677 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11678 row, DEFAULT_FACE_ID);
11679 reseat_at_previous_visible_line_start (&it);
11680
11681 /* If the line start is "too far" away from the window start,
11682 say it takes too much time to compute a new window start. */
11683 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11684 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11685 {
11686 int min_distance, distance;
11687
11688 /* Move forward by display lines to find the new window
11689 start. If window width was enlarged, the new start can
11690 be expected to be > the old start. If window width was
11691 decreased, the new window start will be < the old start.
11692 So, we're looking for the display line start with the
11693 minimum distance from the old window start. */
11694 pos = it.current.pos;
11695 min_distance = INFINITY;
11696 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11697 distance < min_distance)
11698 {
11699 min_distance = distance;
11700 pos = it.current.pos;
11701 move_it_by_lines (&it, 1, 0);
11702 }
11703
11704 /* Set the window start there. */
11705 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11706 window_start_changed_p = 1;
11707 }
11708 }
11709
11710 return window_start_changed_p;
11711 }
11712
11713
11714 /* Try cursor movement in case text has not changed in window WINDOW,
11715 with window start STARTP. Value is
11716
11717 CURSOR_MOVEMENT_SUCCESS if successful
11718
11719 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11720
11721 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11722 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11723 we want to scroll as if scroll-step were set to 1. See the code.
11724
11725 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11726 which case we have to abort this redisplay, and adjust matrices
11727 first. */
11728
11729 enum
11730 {
11731 CURSOR_MOVEMENT_SUCCESS,
11732 CURSOR_MOVEMENT_CANNOT_BE_USED,
11733 CURSOR_MOVEMENT_MUST_SCROLL,
11734 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11735 };
11736
11737 static int
11738 try_cursor_movement (window, startp, scroll_step)
11739 Lisp_Object window;
11740 struct text_pos startp;
11741 int *scroll_step;
11742 {
11743 struct window *w = XWINDOW (window);
11744 struct frame *f = XFRAME (w->frame);
11745 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11746
11747 #if GLYPH_DEBUG
11748 if (inhibit_try_cursor_movement)
11749 return rc;
11750 #endif
11751
11752 /* Handle case where text has not changed, only point, and it has
11753 not moved off the frame. */
11754 if (/* Point may be in this window. */
11755 PT >= CHARPOS (startp)
11756 /* Selective display hasn't changed. */
11757 && !current_buffer->clip_changed
11758 /* Function force-mode-line-update is used to force a thorough
11759 redisplay. It sets either windows_or_buffers_changed or
11760 update_mode_lines. So don't take a shortcut here for these
11761 cases. */
11762 && !update_mode_lines
11763 && !windows_or_buffers_changed
11764 && !cursor_type_changed
11765 /* Can't use this case if highlighting a region. When a
11766 region exists, cursor movement has to do more than just
11767 set the cursor. */
11768 && !(!NILP (Vtransient_mark_mode)
11769 && !NILP (current_buffer->mark_active))
11770 && NILP (w->region_showing)
11771 && NILP (Vshow_trailing_whitespace)
11772 /* Right after splitting windows, last_point may be nil. */
11773 && INTEGERP (w->last_point)
11774 /* This code is not used for mini-buffer for the sake of the case
11775 of redisplaying to replace an echo area message; since in
11776 that case the mini-buffer contents per se are usually
11777 unchanged. This code is of no real use in the mini-buffer
11778 since the handling of this_line_start_pos, etc., in redisplay
11779 handles the same cases. */
11780 && !EQ (window, minibuf_window)
11781 /* When splitting windows or for new windows, it happens that
11782 redisplay is called with a nil window_end_vpos or one being
11783 larger than the window. This should really be fixed in
11784 window.c. I don't have this on my list, now, so we do
11785 approximately the same as the old redisplay code. --gerd. */
11786 && INTEGERP (w->window_end_vpos)
11787 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11788 && (FRAME_WINDOW_P (f)
11789 || !overlay_arrow_in_current_buffer_p ()))
11790 {
11791 int this_scroll_margin, top_scroll_margin;
11792 struct glyph_row *row = NULL;
11793
11794 #if GLYPH_DEBUG
11795 debug_method_add (w, "cursor movement");
11796 #endif
11797
11798 /* Scroll if point within this distance from the top or bottom
11799 of the window. This is a pixel value. */
11800 this_scroll_margin = max (0, scroll_margin);
11801 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11802 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11803
11804 top_scroll_margin = this_scroll_margin;
11805 if (WINDOW_WANTS_HEADER_LINE_P (w))
11806 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11807
11808 /* Start with the row the cursor was displayed during the last
11809 not paused redisplay. Give up if that row is not valid. */
11810 if (w->last_cursor.vpos < 0
11811 || w->last_cursor.vpos >= w->current_matrix->nrows)
11812 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11813 else
11814 {
11815 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11816 if (row->mode_line_p)
11817 ++row;
11818 if (!row->enabled_p)
11819 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11820 }
11821
11822 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11823 {
11824 int scroll_p = 0;
11825 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11826
11827 if (PT > XFASTINT (w->last_point))
11828 {
11829 /* Point has moved forward. */
11830 while (MATRIX_ROW_END_CHARPOS (row) < PT
11831 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11832 {
11833 xassert (row->enabled_p);
11834 ++row;
11835 }
11836
11837 /* The end position of a row equals the start position
11838 of the next row. If PT is there, we would rather
11839 display it in the next line. */
11840 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11841 && MATRIX_ROW_END_CHARPOS (row) == PT
11842 && !cursor_row_p (w, row))
11843 ++row;
11844
11845 /* If within the scroll margin, scroll. Note that
11846 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11847 the next line would be drawn, and that
11848 this_scroll_margin can be zero. */
11849 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11850 || PT > MATRIX_ROW_END_CHARPOS (row)
11851 /* Line is completely visible last line in window
11852 and PT is to be set in the next line. */
11853 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11854 && PT == MATRIX_ROW_END_CHARPOS (row)
11855 && !row->ends_at_zv_p
11856 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11857 scroll_p = 1;
11858 }
11859 else if (PT < XFASTINT (w->last_point))
11860 {
11861 /* Cursor has to be moved backward. Note that PT >=
11862 CHARPOS (startp) because of the outer if-statement. */
11863 while (!row->mode_line_p
11864 && (MATRIX_ROW_START_CHARPOS (row) > PT
11865 || (MATRIX_ROW_START_CHARPOS (row) == PT
11866 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11867 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11868 row > w->current_matrix->rows
11869 && (row-1)->ends_in_newline_from_string_p))))
11870 && (row->y > top_scroll_margin
11871 || CHARPOS (startp) == BEGV))
11872 {
11873 xassert (row->enabled_p);
11874 --row;
11875 }
11876
11877 /* Consider the following case: Window starts at BEGV,
11878 there is invisible, intangible text at BEGV, so that
11879 display starts at some point START > BEGV. It can
11880 happen that we are called with PT somewhere between
11881 BEGV and START. Try to handle that case. */
11882 if (row < w->current_matrix->rows
11883 || row->mode_line_p)
11884 {
11885 row = w->current_matrix->rows;
11886 if (row->mode_line_p)
11887 ++row;
11888 }
11889
11890 /* Due to newlines in overlay strings, we may have to
11891 skip forward over overlay strings. */
11892 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11893 && MATRIX_ROW_END_CHARPOS (row) == PT
11894 && !cursor_row_p (w, row))
11895 ++row;
11896
11897 /* If within the scroll margin, scroll. */
11898 if (row->y < top_scroll_margin
11899 && CHARPOS (startp) != BEGV)
11900 scroll_p = 1;
11901 }
11902 else
11903 {
11904 /* Cursor did not move. So don't scroll even if cursor line
11905 is partially visible, as it was so before. */
11906 rc = CURSOR_MOVEMENT_SUCCESS;
11907 }
11908
11909 if (PT < MATRIX_ROW_START_CHARPOS (row)
11910 || PT > MATRIX_ROW_END_CHARPOS (row))
11911 {
11912 /* if PT is not in the glyph row, give up. */
11913 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11914 }
11915 else if (rc != CURSOR_MOVEMENT_SUCCESS
11916 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11917 && make_cursor_line_fully_visible_p)
11918 {
11919 if (PT == MATRIX_ROW_END_CHARPOS (row)
11920 && !row->ends_at_zv_p
11921 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11922 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11923 else if (row->height > window_box_height (w))
11924 {
11925 /* If we end up in a partially visible line, let's
11926 make it fully visible, except when it's taller
11927 than the window, in which case we can't do much
11928 about it. */
11929 *scroll_step = 1;
11930 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11931 }
11932 else
11933 {
11934 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11935 if (!cursor_row_fully_visible_p (w, 0, 1))
11936 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11937 else
11938 rc = CURSOR_MOVEMENT_SUCCESS;
11939 }
11940 }
11941 else if (scroll_p)
11942 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11943 else
11944 {
11945 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11946 rc = CURSOR_MOVEMENT_SUCCESS;
11947 }
11948 }
11949 }
11950
11951 return rc;
11952 }
11953
11954 void
11955 set_vertical_scroll_bar (w)
11956 struct window *w;
11957 {
11958 int start, end, whole;
11959
11960 /* Calculate the start and end positions for the current window.
11961 At some point, it would be nice to choose between scrollbars
11962 which reflect the whole buffer size, with special markers
11963 indicating narrowing, and scrollbars which reflect only the
11964 visible region.
11965
11966 Note that mini-buffers sometimes aren't displaying any text. */
11967 if (!MINI_WINDOW_P (w)
11968 || (w == XWINDOW (minibuf_window)
11969 && NILP (echo_area_buffer[0])))
11970 {
11971 struct buffer *buf = XBUFFER (w->buffer);
11972 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11973 start = marker_position (w->start) - BUF_BEGV (buf);
11974 /* I don't think this is guaranteed to be right. For the
11975 moment, we'll pretend it is. */
11976 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11977
11978 if (end < start)
11979 end = start;
11980 if (whole < (end - start))
11981 whole = end - start;
11982 }
11983 else
11984 start = end = whole = 0;
11985
11986 /* Indicate what this scroll bar ought to be displaying now. */
11987 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11988 }
11989
11990
11991 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11992 selected_window is redisplayed.
11993
11994 We can return without actually redisplaying the window if
11995 fonts_changed_p is nonzero. In that case, redisplay_internal will
11996 retry. */
11997
11998 static void
11999 redisplay_window (window, just_this_one_p)
12000 Lisp_Object window;
12001 int just_this_one_p;
12002 {
12003 struct window *w = XWINDOW (window);
12004 struct frame *f = XFRAME (w->frame);
12005 struct buffer *buffer = XBUFFER (w->buffer);
12006 struct buffer *old = current_buffer;
12007 struct text_pos lpoint, opoint, startp;
12008 int update_mode_line;
12009 int tem;
12010 struct it it;
12011 /* Record it now because it's overwritten. */
12012 int current_matrix_up_to_date_p = 0;
12013 int used_current_matrix_p = 0;
12014 /* This is less strict than current_matrix_up_to_date_p.
12015 It indictes that the buffer contents and narrowing are unchanged. */
12016 int buffer_unchanged_p = 0;
12017 int temp_scroll_step = 0;
12018 int count = SPECPDL_INDEX ();
12019 int rc;
12020 int centering_position = -1;
12021 int last_line_misfit = 0;
12022
12023 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12024 opoint = lpoint;
12025
12026 /* W must be a leaf window here. */
12027 xassert (!NILP (w->buffer));
12028 #if GLYPH_DEBUG
12029 *w->desired_matrix->method = 0;
12030 #endif
12031
12032 specbind (Qinhibit_point_motion_hooks, Qt);
12033
12034 reconsider_clip_changes (w, buffer);
12035
12036 /* Has the mode line to be updated? */
12037 update_mode_line = (!NILP (w->update_mode_line)
12038 || update_mode_lines
12039 || buffer->clip_changed
12040 || buffer->prevent_redisplay_optimizations_p);
12041
12042 if (MINI_WINDOW_P (w))
12043 {
12044 if (w == XWINDOW (echo_area_window)
12045 && !NILP (echo_area_buffer[0]))
12046 {
12047 if (update_mode_line)
12048 /* We may have to update a tty frame's menu bar or a
12049 tool-bar. Example `M-x C-h C-h C-g'. */
12050 goto finish_menu_bars;
12051 else
12052 /* We've already displayed the echo area glyphs in this window. */
12053 goto finish_scroll_bars;
12054 }
12055 else if ((w != XWINDOW (minibuf_window)
12056 || minibuf_level == 0)
12057 /* When buffer is nonempty, redisplay window normally. */
12058 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12059 /* Quail displays non-mini buffers in minibuffer window.
12060 In that case, redisplay the window normally. */
12061 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12062 {
12063 /* W is a mini-buffer window, but it's not active, so clear
12064 it. */
12065 int yb = window_text_bottom_y (w);
12066 struct glyph_row *row;
12067 int y;
12068
12069 for (y = 0, row = w->desired_matrix->rows;
12070 y < yb;
12071 y += row->height, ++row)
12072 blank_row (w, row, y);
12073 goto finish_scroll_bars;
12074 }
12075
12076 clear_glyph_matrix (w->desired_matrix);
12077 }
12078
12079 /* Otherwise set up data on this window; select its buffer and point
12080 value. */
12081 /* Really select the buffer, for the sake of buffer-local
12082 variables. */
12083 set_buffer_internal_1 (XBUFFER (w->buffer));
12084 SET_TEXT_POS (opoint, PT, PT_BYTE);
12085
12086 current_matrix_up_to_date_p
12087 = (!NILP (w->window_end_valid)
12088 && !current_buffer->clip_changed
12089 && !current_buffer->prevent_redisplay_optimizations_p
12090 && XFASTINT (w->last_modified) >= MODIFF
12091 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12092
12093 buffer_unchanged_p
12094 = (!NILP (w->window_end_valid)
12095 && !current_buffer->clip_changed
12096 && XFASTINT (w->last_modified) >= MODIFF
12097 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12098
12099 /* When windows_or_buffers_changed is non-zero, we can't rely on
12100 the window end being valid, so set it to nil there. */
12101 if (windows_or_buffers_changed)
12102 {
12103 /* If window starts on a continuation line, maybe adjust the
12104 window start in case the window's width changed. */
12105 if (XMARKER (w->start)->buffer == current_buffer)
12106 compute_window_start_on_continuation_line (w);
12107
12108 w->window_end_valid = Qnil;
12109 }
12110
12111 /* Some sanity checks. */
12112 CHECK_WINDOW_END (w);
12113 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12114 abort ();
12115 if (BYTEPOS (opoint) < CHARPOS (opoint))
12116 abort ();
12117
12118 /* If %c is in mode line, update it if needed. */
12119 if (!NILP (w->column_number_displayed)
12120 /* This alternative quickly identifies a common case
12121 where no change is needed. */
12122 && !(PT == XFASTINT (w->last_point)
12123 && XFASTINT (w->last_modified) >= MODIFF
12124 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12125 && (XFASTINT (w->column_number_displayed)
12126 != (int) current_column ())) /* iftc */
12127 update_mode_line = 1;
12128
12129 /* Count number of windows showing the selected buffer. An indirect
12130 buffer counts as its base buffer. */
12131 if (!just_this_one_p)
12132 {
12133 struct buffer *current_base, *window_base;
12134 current_base = current_buffer;
12135 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12136 if (current_base->base_buffer)
12137 current_base = current_base->base_buffer;
12138 if (window_base->base_buffer)
12139 window_base = window_base->base_buffer;
12140 if (current_base == window_base)
12141 buffer_shared++;
12142 }
12143
12144 /* Point refers normally to the selected window. For any other
12145 window, set up appropriate value. */
12146 if (!EQ (window, selected_window))
12147 {
12148 int new_pt = XMARKER (w->pointm)->charpos;
12149 int new_pt_byte = marker_byte_position (w->pointm);
12150 if (new_pt < BEGV)
12151 {
12152 new_pt = BEGV;
12153 new_pt_byte = BEGV_BYTE;
12154 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12155 }
12156 else if (new_pt > (ZV - 1))
12157 {
12158 new_pt = ZV;
12159 new_pt_byte = ZV_BYTE;
12160 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12161 }
12162
12163 /* We don't use SET_PT so that the point-motion hooks don't run. */
12164 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12165 }
12166
12167 /* If any of the character widths specified in the display table
12168 have changed, invalidate the width run cache. It's true that
12169 this may be a bit late to catch such changes, but the rest of
12170 redisplay goes (non-fatally) haywire when the display table is
12171 changed, so why should we worry about doing any better? */
12172 if (current_buffer->width_run_cache)
12173 {
12174 struct Lisp_Char_Table *disptab = buffer_display_table ();
12175
12176 if (! disptab_matches_widthtab (disptab,
12177 XVECTOR (current_buffer->width_table)))
12178 {
12179 invalidate_region_cache (current_buffer,
12180 current_buffer->width_run_cache,
12181 BEG, Z);
12182 recompute_width_table (current_buffer, disptab);
12183 }
12184 }
12185
12186 /* If window-start is screwed up, choose a new one. */
12187 if (XMARKER (w->start)->buffer != current_buffer)
12188 goto recenter;
12189
12190 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12191
12192 /* If someone specified a new starting point but did not insist,
12193 check whether it can be used. */
12194 if (!NILP (w->optional_new_start)
12195 && CHARPOS (startp) >= BEGV
12196 && CHARPOS (startp) <= ZV)
12197 {
12198 w->optional_new_start = Qnil;
12199 start_display (&it, w, startp);
12200 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12201 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12202 if (IT_CHARPOS (it) == PT)
12203 w->force_start = Qt;
12204 /* IT may overshoot PT if text at PT is invisible. */
12205 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12206 w->force_start = Qt;
12207
12208
12209 }
12210
12211 /* Handle case where place to start displaying has been specified,
12212 unless the specified location is outside the accessible range. */
12213 if (!NILP (w->force_start)
12214 || w->frozen_window_start_p)
12215 {
12216 /* We set this later on if we have to adjust point. */
12217 int new_vpos = -1;
12218 int val;
12219
12220 w->force_start = Qnil;
12221 w->vscroll = 0;
12222 w->window_end_valid = Qnil;
12223
12224 /* Forget any recorded base line for line number display. */
12225 if (!buffer_unchanged_p)
12226 w->base_line_number = Qnil;
12227
12228 /* Redisplay the mode line. Select the buffer properly for that.
12229 Also, run the hook window-scroll-functions
12230 because we have scrolled. */
12231 /* Note, we do this after clearing force_start because
12232 if there's an error, it is better to forget about force_start
12233 than to get into an infinite loop calling the hook functions
12234 and having them get more errors. */
12235 if (!update_mode_line
12236 || ! NILP (Vwindow_scroll_functions))
12237 {
12238 update_mode_line = 1;
12239 w->update_mode_line = Qt;
12240 startp = run_window_scroll_functions (window, startp);
12241 }
12242
12243 w->last_modified = make_number (0);
12244 w->last_overlay_modified = make_number (0);
12245 if (CHARPOS (startp) < BEGV)
12246 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12247 else if (CHARPOS (startp) > ZV)
12248 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12249
12250 /* Redisplay, then check if cursor has been set during the
12251 redisplay. Give up if new fonts were loaded. */
12252 val = try_window (window, startp, 1);
12253 if (!val)
12254 {
12255 w->force_start = Qt;
12256 clear_glyph_matrix (w->desired_matrix);
12257 goto need_larger_matrices;
12258 }
12259 /* Point was outside the scroll margins. */
12260 if (val < 0)
12261 new_vpos = window_box_height (w) / 2;
12262
12263 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12264 {
12265 /* If point does not appear, try to move point so it does
12266 appear. The desired matrix has been built above, so we
12267 can use it here. */
12268 new_vpos = window_box_height (w) / 2;
12269 }
12270
12271 if (!cursor_row_fully_visible_p (w, 0, 0))
12272 {
12273 /* Point does appear, but on a line partly visible at end of window.
12274 Move it back to a fully-visible line. */
12275 new_vpos = window_box_height (w);
12276 }
12277
12278 /* If we need to move point for either of the above reasons,
12279 now actually do it. */
12280 if (new_vpos >= 0)
12281 {
12282 struct glyph_row *row;
12283
12284 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12285 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12286 ++row;
12287
12288 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12289 MATRIX_ROW_START_BYTEPOS (row));
12290
12291 if (w != XWINDOW (selected_window))
12292 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12293 else if (current_buffer == old)
12294 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12295
12296 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12297
12298 /* If we are highlighting the region, then we just changed
12299 the region, so redisplay to show it. */
12300 if (!NILP (Vtransient_mark_mode)
12301 && !NILP (current_buffer->mark_active))
12302 {
12303 clear_glyph_matrix (w->desired_matrix);
12304 if (!try_window (window, startp, 0))
12305 goto need_larger_matrices;
12306 }
12307 }
12308
12309 #if GLYPH_DEBUG
12310 debug_method_add (w, "forced window start");
12311 #endif
12312 goto done;
12313 }
12314
12315 /* Handle case where text has not changed, only point, and it has
12316 not moved off the frame, and we are not retrying after hscroll.
12317 (current_matrix_up_to_date_p is nonzero when retrying.) */
12318 if (current_matrix_up_to_date_p
12319 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12320 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12321 {
12322 switch (rc)
12323 {
12324 case CURSOR_MOVEMENT_SUCCESS:
12325 used_current_matrix_p = 1;
12326 goto done;
12327
12328 #if 0 /* try_cursor_movement never returns this value. */
12329 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12330 goto need_larger_matrices;
12331 #endif
12332
12333 case CURSOR_MOVEMENT_MUST_SCROLL:
12334 goto try_to_scroll;
12335
12336 default:
12337 abort ();
12338 }
12339 }
12340 /* If current starting point was originally the beginning of a line
12341 but no longer is, find a new starting point. */
12342 else if (!NILP (w->start_at_line_beg)
12343 && !(CHARPOS (startp) <= BEGV
12344 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12345 {
12346 #if GLYPH_DEBUG
12347 debug_method_add (w, "recenter 1");
12348 #endif
12349 goto recenter;
12350 }
12351
12352 /* Try scrolling with try_window_id. Value is > 0 if update has
12353 been done, it is -1 if we know that the same window start will
12354 not work. It is 0 if unsuccessful for some other reason. */
12355 else if ((tem = try_window_id (w)) != 0)
12356 {
12357 #if GLYPH_DEBUG
12358 debug_method_add (w, "try_window_id %d", tem);
12359 #endif
12360
12361 if (fonts_changed_p)
12362 goto need_larger_matrices;
12363 if (tem > 0)
12364 goto done;
12365
12366 /* Otherwise try_window_id has returned -1 which means that we
12367 don't want the alternative below this comment to execute. */
12368 }
12369 else if (CHARPOS (startp) >= BEGV
12370 && CHARPOS (startp) <= ZV
12371 && PT >= CHARPOS (startp)
12372 && (CHARPOS (startp) < ZV
12373 /* Avoid starting at end of buffer. */
12374 || CHARPOS (startp) == BEGV
12375 || (XFASTINT (w->last_modified) >= MODIFF
12376 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12377 {
12378 #if GLYPH_DEBUG
12379 debug_method_add (w, "same window start");
12380 #endif
12381
12382 /* Try to redisplay starting at same place as before.
12383 If point has not moved off frame, accept the results. */
12384 if (!current_matrix_up_to_date_p
12385 /* Don't use try_window_reusing_current_matrix in this case
12386 because a window scroll function can have changed the
12387 buffer. */
12388 || !NILP (Vwindow_scroll_functions)
12389 || MINI_WINDOW_P (w)
12390 || !(used_current_matrix_p
12391 = try_window_reusing_current_matrix (w)))
12392 {
12393 IF_DEBUG (debug_method_add (w, "1"));
12394 if (try_window (window, startp, 1) < 0)
12395 /* -1 means we need to scroll.
12396 0 means we need new matrices, but fonts_changed_p
12397 is set in that case, so we will detect it below. */
12398 goto try_to_scroll;
12399 }
12400
12401 if (fonts_changed_p)
12402 goto need_larger_matrices;
12403
12404 if (w->cursor.vpos >= 0)
12405 {
12406 if (!just_this_one_p
12407 || current_buffer->clip_changed
12408 || BEG_UNCHANGED < CHARPOS (startp))
12409 /* Forget any recorded base line for line number display. */
12410 w->base_line_number = Qnil;
12411
12412 if (!cursor_row_fully_visible_p (w, 1, 0))
12413 {
12414 clear_glyph_matrix (w->desired_matrix);
12415 last_line_misfit = 1;
12416 }
12417 /* Drop through and scroll. */
12418 else
12419 goto done;
12420 }
12421 else
12422 clear_glyph_matrix (w->desired_matrix);
12423 }
12424
12425 try_to_scroll:
12426
12427 w->last_modified = make_number (0);
12428 w->last_overlay_modified = make_number (0);
12429
12430 /* Redisplay the mode line. Select the buffer properly for that. */
12431 if (!update_mode_line)
12432 {
12433 update_mode_line = 1;
12434 w->update_mode_line = Qt;
12435 }
12436
12437 /* Try to scroll by specified few lines. */
12438 if ((scroll_conservatively
12439 || scroll_step
12440 || temp_scroll_step
12441 || NUMBERP (current_buffer->scroll_up_aggressively)
12442 || NUMBERP (current_buffer->scroll_down_aggressively))
12443 && !current_buffer->clip_changed
12444 && CHARPOS (startp) >= BEGV
12445 && CHARPOS (startp) <= ZV)
12446 {
12447 /* The function returns -1 if new fonts were loaded, 1 if
12448 successful, 0 if not successful. */
12449 int rc = try_scrolling (window, just_this_one_p,
12450 scroll_conservatively,
12451 scroll_step,
12452 temp_scroll_step, last_line_misfit);
12453 switch (rc)
12454 {
12455 case SCROLLING_SUCCESS:
12456 goto done;
12457
12458 case SCROLLING_NEED_LARGER_MATRICES:
12459 goto need_larger_matrices;
12460
12461 case SCROLLING_FAILED:
12462 break;
12463
12464 default:
12465 abort ();
12466 }
12467 }
12468
12469 /* Finally, just choose place to start which centers point */
12470
12471 recenter:
12472 if (centering_position < 0)
12473 centering_position = window_box_height (w) / 2;
12474
12475 #if GLYPH_DEBUG
12476 debug_method_add (w, "recenter");
12477 #endif
12478
12479 /* w->vscroll = 0; */
12480
12481 /* Forget any previously recorded base line for line number display. */
12482 if (!buffer_unchanged_p)
12483 w->base_line_number = Qnil;
12484
12485 /* Move backward half the height of the window. */
12486 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12487 it.current_y = it.last_visible_y;
12488 move_it_vertically_backward (&it, centering_position);
12489 xassert (IT_CHARPOS (it) >= BEGV);
12490
12491 /* The function move_it_vertically_backward may move over more
12492 than the specified y-distance. If it->w is small, e.g. a
12493 mini-buffer window, we may end up in front of the window's
12494 display area. Start displaying at the start of the line
12495 containing PT in this case. */
12496 if (it.current_y <= 0)
12497 {
12498 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12499 move_it_vertically_backward (&it, 0);
12500 #if 0
12501 /* I think this assert is bogus if buffer contains
12502 invisible text or images. KFS. */
12503 xassert (IT_CHARPOS (it) <= PT);
12504 #endif
12505 it.current_y = 0;
12506 }
12507
12508 it.current_x = it.hpos = 0;
12509
12510 /* Set startp here explicitly in case that helps avoid an infinite loop
12511 in case the window-scroll-functions functions get errors. */
12512 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12513
12514 /* Run scroll hooks. */
12515 startp = run_window_scroll_functions (window, it.current.pos);
12516
12517 /* Redisplay the window. */
12518 if (!current_matrix_up_to_date_p
12519 || windows_or_buffers_changed
12520 || cursor_type_changed
12521 /* Don't use try_window_reusing_current_matrix in this case
12522 because it can have changed the buffer. */
12523 || !NILP (Vwindow_scroll_functions)
12524 || !just_this_one_p
12525 || MINI_WINDOW_P (w)
12526 || !(used_current_matrix_p
12527 = try_window_reusing_current_matrix (w)))
12528 try_window (window, startp, 0);
12529
12530 /* If new fonts have been loaded (due to fontsets), give up. We
12531 have to start a new redisplay since we need to re-adjust glyph
12532 matrices. */
12533 if (fonts_changed_p)
12534 goto need_larger_matrices;
12535
12536 /* If cursor did not appear assume that the middle of the window is
12537 in the first line of the window. Do it again with the next line.
12538 (Imagine a window of height 100, displaying two lines of height
12539 60. Moving back 50 from it->last_visible_y will end in the first
12540 line.) */
12541 if (w->cursor.vpos < 0)
12542 {
12543 if (!NILP (w->window_end_valid)
12544 && PT >= Z - XFASTINT (w->window_end_pos))
12545 {
12546 clear_glyph_matrix (w->desired_matrix);
12547 move_it_by_lines (&it, 1, 0);
12548 try_window (window, it.current.pos, 0);
12549 }
12550 else if (PT < IT_CHARPOS (it))
12551 {
12552 clear_glyph_matrix (w->desired_matrix);
12553 move_it_by_lines (&it, -1, 0);
12554 try_window (window, it.current.pos, 0);
12555 }
12556 else
12557 {
12558 /* Not much we can do about it. */
12559 }
12560 }
12561
12562 /* Consider the following case: Window starts at BEGV, there is
12563 invisible, intangible text at BEGV, so that display starts at
12564 some point START > BEGV. It can happen that we are called with
12565 PT somewhere between BEGV and START. Try to handle that case. */
12566 if (w->cursor.vpos < 0)
12567 {
12568 struct glyph_row *row = w->current_matrix->rows;
12569 if (row->mode_line_p)
12570 ++row;
12571 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12572 }
12573
12574 if (!cursor_row_fully_visible_p (w, 0, 0))
12575 {
12576 /* If vscroll is enabled, disable it and try again. */
12577 if (w->vscroll)
12578 {
12579 w->vscroll = 0;
12580 clear_glyph_matrix (w->desired_matrix);
12581 goto recenter;
12582 }
12583
12584 /* If centering point failed to make the whole line visible,
12585 put point at the top instead. That has to make the whole line
12586 visible, if it can be done. */
12587 if (centering_position == 0)
12588 goto done;
12589
12590 clear_glyph_matrix (w->desired_matrix);
12591 centering_position = 0;
12592 goto recenter;
12593 }
12594
12595 done:
12596
12597 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12598 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12599 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12600 ? Qt : Qnil);
12601
12602 /* Display the mode line, if we must. */
12603 if ((update_mode_line
12604 /* If window not full width, must redo its mode line
12605 if (a) the window to its side is being redone and
12606 (b) we do a frame-based redisplay. This is a consequence
12607 of how inverted lines are drawn in frame-based redisplay. */
12608 || (!just_this_one_p
12609 && !FRAME_WINDOW_P (f)
12610 && !WINDOW_FULL_WIDTH_P (w))
12611 /* Line number to display. */
12612 || INTEGERP (w->base_line_pos)
12613 /* Column number is displayed and different from the one displayed. */
12614 || (!NILP (w->column_number_displayed)
12615 && (XFASTINT (w->column_number_displayed)
12616 != (int) current_column ()))) /* iftc */
12617 /* This means that the window has a mode line. */
12618 && (WINDOW_WANTS_MODELINE_P (w)
12619 || WINDOW_WANTS_HEADER_LINE_P (w)))
12620 {
12621 display_mode_lines (w);
12622
12623 /* If mode line height has changed, arrange for a thorough
12624 immediate redisplay using the correct mode line height. */
12625 if (WINDOW_WANTS_MODELINE_P (w)
12626 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12627 {
12628 fonts_changed_p = 1;
12629 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12630 = DESIRED_MODE_LINE_HEIGHT (w);
12631 }
12632
12633 /* If top line height has changed, arrange for a thorough
12634 immediate redisplay using the correct mode line height. */
12635 if (WINDOW_WANTS_HEADER_LINE_P (w)
12636 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12637 {
12638 fonts_changed_p = 1;
12639 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12640 = DESIRED_HEADER_LINE_HEIGHT (w);
12641 }
12642
12643 if (fonts_changed_p)
12644 goto need_larger_matrices;
12645 }
12646
12647 if (!line_number_displayed
12648 && !BUFFERP (w->base_line_pos))
12649 {
12650 w->base_line_pos = Qnil;
12651 w->base_line_number = Qnil;
12652 }
12653
12654 finish_menu_bars:
12655
12656 /* When we reach a frame's selected window, redo the frame's menu bar. */
12657 if (update_mode_line
12658 && EQ (FRAME_SELECTED_WINDOW (f), window))
12659 {
12660 int redisplay_menu_p = 0;
12661 int redisplay_tool_bar_p = 0;
12662
12663 if (FRAME_WINDOW_P (f))
12664 {
12665 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12666 || defined (USE_GTK)
12667 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12668 #else
12669 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12670 #endif
12671 }
12672 else
12673 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12674
12675 if (redisplay_menu_p)
12676 display_menu_bar (w);
12677
12678 #ifdef HAVE_WINDOW_SYSTEM
12679 #ifdef USE_GTK
12680 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12681 #else
12682 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12683 && (FRAME_TOOL_BAR_LINES (f) > 0
12684 || auto_resize_tool_bars_p);
12685
12686 #endif
12687
12688 if (redisplay_tool_bar_p)
12689 redisplay_tool_bar (f);
12690 #endif
12691 }
12692
12693 #ifdef HAVE_WINDOW_SYSTEM
12694 if (FRAME_WINDOW_P (f)
12695 && update_window_fringes (w, 0)
12696 && !just_this_one_p
12697 && (used_current_matrix_p || overlay_arrow_seen)
12698 && !w->pseudo_window_p)
12699 {
12700 update_begin (f);
12701 BLOCK_INPUT;
12702 if (draw_window_fringes (w, 1))
12703 x_draw_vertical_border (w);
12704 UNBLOCK_INPUT;
12705 update_end (f);
12706 }
12707 #endif /* HAVE_WINDOW_SYSTEM */
12708
12709 /* We go to this label, with fonts_changed_p nonzero,
12710 if it is necessary to try again using larger glyph matrices.
12711 We have to redeem the scroll bar even in this case,
12712 because the loop in redisplay_internal expects that. */
12713 need_larger_matrices:
12714 ;
12715 finish_scroll_bars:
12716
12717 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12718 {
12719 /* Set the thumb's position and size. */
12720 set_vertical_scroll_bar (w);
12721
12722 /* Note that we actually used the scroll bar attached to this
12723 window, so it shouldn't be deleted at the end of redisplay. */
12724 redeem_scroll_bar_hook (w);
12725 }
12726
12727 /* Restore current_buffer and value of point in it. */
12728 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12729 set_buffer_internal_1 (old);
12730 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12731
12732 unbind_to (count, Qnil);
12733 }
12734
12735
12736 /* Build the complete desired matrix of WINDOW with a window start
12737 buffer position POS.
12738
12739 Value is 1 if successful. It is zero if fonts were loaded during
12740 redisplay which makes re-adjusting glyph matrices necessary, and -1
12741 if point would appear in the scroll margins.
12742 (We check that only if CHECK_MARGINS is nonzero. */
12743
12744 int
12745 try_window (window, pos, check_margins)
12746 Lisp_Object window;
12747 struct text_pos pos;
12748 int check_margins;
12749 {
12750 struct window *w = XWINDOW (window);
12751 struct it it;
12752 struct glyph_row *last_text_row = NULL;
12753
12754 /* Make POS the new window start. */
12755 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12756
12757 /* Mark cursor position as unknown. No overlay arrow seen. */
12758 w->cursor.vpos = -1;
12759 overlay_arrow_seen = 0;
12760
12761 /* Initialize iterator and info to start at POS. */
12762 start_display (&it, w, pos);
12763
12764 /* Display all lines of W. */
12765 while (it.current_y < it.last_visible_y)
12766 {
12767 if (display_line (&it))
12768 last_text_row = it.glyph_row - 1;
12769 if (fonts_changed_p)
12770 return 0;
12771 }
12772
12773 /* Don't let the cursor end in the scroll margins. */
12774 if (check_margins
12775 && !MINI_WINDOW_P (w))
12776 {
12777 int this_scroll_margin, cursor_height;
12778
12779 this_scroll_margin = max (0, scroll_margin);
12780 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12781 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12782 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12783
12784 if ((w->cursor.y < this_scroll_margin
12785 && CHARPOS (pos) > BEGV)
12786 /* rms: considering make_cursor_line_fully_visible_p here
12787 seems to give wrong results. We don't want to recenter
12788 when the last line is partly visible, we want to allow
12789 that case to be handled in the usual way. */
12790 || (w->cursor.y + 1) > it.last_visible_y)
12791 {
12792 w->cursor.vpos = -1;
12793 clear_glyph_matrix (w->desired_matrix);
12794 return -1;
12795 }
12796 }
12797
12798 /* If bottom moved off end of frame, change mode line percentage. */
12799 if (XFASTINT (w->window_end_pos) <= 0
12800 && Z != IT_CHARPOS (it))
12801 w->update_mode_line = Qt;
12802
12803 /* Set window_end_pos to the offset of the last character displayed
12804 on the window from the end of current_buffer. Set
12805 window_end_vpos to its row number. */
12806 if (last_text_row)
12807 {
12808 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12809 w->window_end_bytepos
12810 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12811 w->window_end_pos
12812 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12813 w->window_end_vpos
12814 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12815 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12816 ->displays_text_p);
12817 }
12818 else
12819 {
12820 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12821 w->window_end_pos = make_number (Z - ZV);
12822 w->window_end_vpos = make_number (0);
12823 }
12824
12825 /* But that is not valid info until redisplay finishes. */
12826 w->window_end_valid = Qnil;
12827 return 1;
12828 }
12829
12830
12831 \f
12832 /************************************************************************
12833 Window redisplay reusing current matrix when buffer has not changed
12834 ************************************************************************/
12835
12836 /* Try redisplay of window W showing an unchanged buffer with a
12837 different window start than the last time it was displayed by
12838 reusing its current matrix. Value is non-zero if successful.
12839 W->start is the new window start. */
12840
12841 static int
12842 try_window_reusing_current_matrix (w)
12843 struct window *w;
12844 {
12845 struct frame *f = XFRAME (w->frame);
12846 struct glyph_row *row, *bottom_row;
12847 struct it it;
12848 struct run run;
12849 struct text_pos start, new_start;
12850 int nrows_scrolled, i;
12851 struct glyph_row *last_text_row;
12852 struct glyph_row *last_reused_text_row;
12853 struct glyph_row *start_row;
12854 int start_vpos, min_y, max_y;
12855
12856 #if GLYPH_DEBUG
12857 if (inhibit_try_window_reusing)
12858 return 0;
12859 #endif
12860
12861 if (/* This function doesn't handle terminal frames. */
12862 !FRAME_WINDOW_P (f)
12863 /* Don't try to reuse the display if windows have been split
12864 or such. */
12865 || windows_or_buffers_changed
12866 || cursor_type_changed)
12867 return 0;
12868
12869 /* Can't do this if region may have changed. */
12870 if ((!NILP (Vtransient_mark_mode)
12871 && !NILP (current_buffer->mark_active))
12872 || !NILP (w->region_showing)
12873 || !NILP (Vshow_trailing_whitespace))
12874 return 0;
12875
12876 /* If top-line visibility has changed, give up. */
12877 if (WINDOW_WANTS_HEADER_LINE_P (w)
12878 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12879 return 0;
12880
12881 /* Give up if old or new display is scrolled vertically. We could
12882 make this function handle this, but right now it doesn't. */
12883 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12884 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12885 return 0;
12886
12887 /* The variable new_start now holds the new window start. The old
12888 start `start' can be determined from the current matrix. */
12889 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12890 start = start_row->start.pos;
12891 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12892
12893 /* Clear the desired matrix for the display below. */
12894 clear_glyph_matrix (w->desired_matrix);
12895
12896 if (CHARPOS (new_start) <= CHARPOS (start))
12897 {
12898 int first_row_y;
12899
12900 /* Don't use this method if the display starts with an ellipsis
12901 displayed for invisible text. It's not easy to handle that case
12902 below, and it's certainly not worth the effort since this is
12903 not a frequent case. */
12904 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12905 return 0;
12906
12907 IF_DEBUG (debug_method_add (w, "twu1"));
12908
12909 /* Display up to a row that can be reused. The variable
12910 last_text_row is set to the last row displayed that displays
12911 text. Note that it.vpos == 0 if or if not there is a
12912 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12913 start_display (&it, w, new_start);
12914 first_row_y = it.current_y;
12915 w->cursor.vpos = -1;
12916 last_text_row = last_reused_text_row = NULL;
12917
12918 while (it.current_y < it.last_visible_y
12919 && !fonts_changed_p)
12920 {
12921 /* If we have reached into the characters in the START row,
12922 that means the line boundaries have changed. So we
12923 can't start copying with the row START. Maybe it will
12924 work to start copying with the following row. */
12925 while (IT_CHARPOS (it) > CHARPOS (start))
12926 {
12927 /* Advance to the next row as the "start". */
12928 start_row++;
12929 start = start_row->start.pos;
12930 /* If there are no more rows to try, or just one, give up. */
12931 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12932 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12933 || CHARPOS (start) == ZV)
12934 {
12935 clear_glyph_matrix (w->desired_matrix);
12936 return 0;
12937 }
12938
12939 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12940 }
12941 /* If we have reached alignment,
12942 we can copy the rest of the rows. */
12943 if (IT_CHARPOS (it) == CHARPOS (start))
12944 break;
12945
12946 if (display_line (&it))
12947 last_text_row = it.glyph_row - 1;
12948 }
12949
12950 /* A value of current_y < last_visible_y means that we stopped
12951 at the previous window start, which in turn means that we
12952 have at least one reusable row. */
12953 if (it.current_y < it.last_visible_y)
12954 {
12955 /* IT.vpos always starts from 0; it counts text lines. */
12956 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12957
12958 /* Find PT if not already found in the lines displayed. */
12959 if (w->cursor.vpos < 0)
12960 {
12961 int dy = it.current_y - start_row->y;
12962
12963 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12964 row = row_containing_pos (w, PT, row, NULL, dy);
12965 if (row)
12966 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12967 dy, nrows_scrolled);
12968 else
12969 {
12970 clear_glyph_matrix (w->desired_matrix);
12971 return 0;
12972 }
12973 }
12974
12975 /* Scroll the display. Do it before the current matrix is
12976 changed. The problem here is that update has not yet
12977 run, i.e. part of the current matrix is not up to date.
12978 scroll_run_hook will clear the cursor, and use the
12979 current matrix to get the height of the row the cursor is
12980 in. */
12981 run.current_y = start_row->y;
12982 run.desired_y = it.current_y;
12983 run.height = it.last_visible_y - it.current_y;
12984
12985 if (run.height > 0 && run.current_y != run.desired_y)
12986 {
12987 update_begin (f);
12988 rif->update_window_begin_hook (w);
12989 rif->clear_window_mouse_face (w);
12990 rif->scroll_run_hook (w, &run);
12991 rif->update_window_end_hook (w, 0, 0);
12992 update_end (f);
12993 }
12994
12995 /* Shift current matrix down by nrows_scrolled lines. */
12996 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12997 rotate_matrix (w->current_matrix,
12998 start_vpos,
12999 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13000 nrows_scrolled);
13001
13002 /* Disable lines that must be updated. */
13003 for (i = 0; i < it.vpos; ++i)
13004 (start_row + i)->enabled_p = 0;
13005
13006 /* Re-compute Y positions. */
13007 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13008 max_y = it.last_visible_y;
13009 for (row = start_row + nrows_scrolled;
13010 row < bottom_row;
13011 ++row)
13012 {
13013 row->y = it.current_y;
13014 row->visible_height = row->height;
13015
13016 if (row->y < min_y)
13017 row->visible_height -= min_y - row->y;
13018 if (row->y + row->height > max_y)
13019 row->visible_height -= row->y + row->height - max_y;
13020 row->redraw_fringe_bitmaps_p = 1;
13021
13022 it.current_y += row->height;
13023
13024 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13025 last_reused_text_row = row;
13026 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13027 break;
13028 }
13029
13030 /* Disable lines in the current matrix which are now
13031 below the window. */
13032 for (++row; row < bottom_row; ++row)
13033 row->enabled_p = 0;
13034 }
13035
13036 /* Update window_end_pos etc.; last_reused_text_row is the last
13037 reused row from the current matrix containing text, if any.
13038 The value of last_text_row is the last displayed line
13039 containing text. */
13040 if (last_reused_text_row)
13041 {
13042 w->window_end_bytepos
13043 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13044 w->window_end_pos
13045 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13046 w->window_end_vpos
13047 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13048 w->current_matrix));
13049 }
13050 else if (last_text_row)
13051 {
13052 w->window_end_bytepos
13053 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13054 w->window_end_pos
13055 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13056 w->window_end_vpos
13057 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13058 }
13059 else
13060 {
13061 /* This window must be completely empty. */
13062 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13063 w->window_end_pos = make_number (Z - ZV);
13064 w->window_end_vpos = make_number (0);
13065 }
13066 w->window_end_valid = Qnil;
13067
13068 /* Update hint: don't try scrolling again in update_window. */
13069 w->desired_matrix->no_scrolling_p = 1;
13070
13071 #if GLYPH_DEBUG
13072 debug_method_add (w, "try_window_reusing_current_matrix 1");
13073 #endif
13074 return 1;
13075 }
13076 else if (CHARPOS (new_start) > CHARPOS (start))
13077 {
13078 struct glyph_row *pt_row, *row;
13079 struct glyph_row *first_reusable_row;
13080 struct glyph_row *first_row_to_display;
13081 int dy;
13082 int yb = window_text_bottom_y (w);
13083
13084 /* Find the row starting at new_start, if there is one. Don't
13085 reuse a partially visible line at the end. */
13086 first_reusable_row = start_row;
13087 while (first_reusable_row->enabled_p
13088 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13089 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13090 < CHARPOS (new_start)))
13091 ++first_reusable_row;
13092
13093 /* Give up if there is no row to reuse. */
13094 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13095 || !first_reusable_row->enabled_p
13096 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13097 != CHARPOS (new_start)))
13098 return 0;
13099
13100 /* We can reuse fully visible rows beginning with
13101 first_reusable_row to the end of the window. Set
13102 first_row_to_display to the first row that cannot be reused.
13103 Set pt_row to the row containing point, if there is any. */
13104 pt_row = NULL;
13105 for (first_row_to_display = first_reusable_row;
13106 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13107 ++first_row_to_display)
13108 {
13109 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13110 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13111 pt_row = first_row_to_display;
13112 }
13113
13114 /* Start displaying at the start of first_row_to_display. */
13115 xassert (first_row_to_display->y < yb);
13116 init_to_row_start (&it, w, first_row_to_display);
13117
13118 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13119 - start_vpos);
13120 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13121 - nrows_scrolled);
13122 it.current_y = (first_row_to_display->y - first_reusable_row->y
13123 + WINDOW_HEADER_LINE_HEIGHT (w));
13124
13125 /* Display lines beginning with first_row_to_display in the
13126 desired matrix. Set last_text_row to the last row displayed
13127 that displays text. */
13128 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13129 if (pt_row == NULL)
13130 w->cursor.vpos = -1;
13131 last_text_row = NULL;
13132 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13133 if (display_line (&it))
13134 last_text_row = it.glyph_row - 1;
13135
13136 /* Give up If point isn't in a row displayed or reused. */
13137 if (w->cursor.vpos < 0)
13138 {
13139 clear_glyph_matrix (w->desired_matrix);
13140 return 0;
13141 }
13142
13143 /* If point is in a reused row, adjust y and vpos of the cursor
13144 position. */
13145 if (pt_row)
13146 {
13147 w->cursor.vpos -= nrows_scrolled;
13148 w->cursor.y -= first_reusable_row->y - start_row->y;
13149 }
13150
13151 /* Scroll the display. */
13152 run.current_y = first_reusable_row->y;
13153 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13154 run.height = it.last_visible_y - run.current_y;
13155 dy = run.current_y - run.desired_y;
13156
13157 if (run.height)
13158 {
13159 update_begin (f);
13160 rif->update_window_begin_hook (w);
13161 rif->clear_window_mouse_face (w);
13162 rif->scroll_run_hook (w, &run);
13163 rif->update_window_end_hook (w, 0, 0);
13164 update_end (f);
13165 }
13166
13167 /* Adjust Y positions of reused rows. */
13168 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13169 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13170 max_y = it.last_visible_y;
13171 for (row = first_reusable_row; row < first_row_to_display; ++row)
13172 {
13173 row->y -= dy;
13174 row->visible_height = row->height;
13175 if (row->y < min_y)
13176 row->visible_height -= min_y - row->y;
13177 if (row->y + row->height > max_y)
13178 row->visible_height -= row->y + row->height - max_y;
13179 row->redraw_fringe_bitmaps_p = 1;
13180 }
13181
13182 /* Scroll the current matrix. */
13183 xassert (nrows_scrolled > 0);
13184 rotate_matrix (w->current_matrix,
13185 start_vpos,
13186 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13187 -nrows_scrolled);
13188
13189 /* Disable rows not reused. */
13190 for (row -= nrows_scrolled; row < bottom_row; ++row)
13191 row->enabled_p = 0;
13192
13193 /* Point may have moved to a different line, so we cannot assume that
13194 the previous cursor position is valid; locate the correct row. */
13195 if (pt_row)
13196 {
13197 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13198 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13199 row++)
13200 {
13201 w->cursor.vpos++;
13202 w->cursor.y = row->y;
13203 }
13204 if (row < bottom_row)
13205 {
13206 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13207 while (glyph->charpos < PT)
13208 {
13209 w->cursor.hpos++;
13210 w->cursor.x += glyph->pixel_width;
13211 glyph++;
13212 }
13213 }
13214 }
13215
13216 /* Adjust window end. A null value of last_text_row means that
13217 the window end is in reused rows which in turn means that
13218 only its vpos can have changed. */
13219 if (last_text_row)
13220 {
13221 w->window_end_bytepos
13222 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13223 w->window_end_pos
13224 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13225 w->window_end_vpos
13226 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13227 }
13228 else
13229 {
13230 w->window_end_vpos
13231 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13232 }
13233
13234 w->window_end_valid = Qnil;
13235 w->desired_matrix->no_scrolling_p = 1;
13236
13237 #if GLYPH_DEBUG
13238 debug_method_add (w, "try_window_reusing_current_matrix 2");
13239 #endif
13240 return 1;
13241 }
13242
13243 return 0;
13244 }
13245
13246
13247 \f
13248 /************************************************************************
13249 Window redisplay reusing current matrix when buffer has changed
13250 ************************************************************************/
13251
13252 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13253 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13254 int *, int *));
13255 static struct glyph_row *
13256 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13257 struct glyph_row *));
13258
13259
13260 /* Return the last row in MATRIX displaying text. If row START is
13261 non-null, start searching with that row. IT gives the dimensions
13262 of the display. Value is null if matrix is empty; otherwise it is
13263 a pointer to the row found. */
13264
13265 static struct glyph_row *
13266 find_last_row_displaying_text (matrix, it, start)
13267 struct glyph_matrix *matrix;
13268 struct it *it;
13269 struct glyph_row *start;
13270 {
13271 struct glyph_row *row, *row_found;
13272
13273 /* Set row_found to the last row in IT->w's current matrix
13274 displaying text. The loop looks funny but think of partially
13275 visible lines. */
13276 row_found = NULL;
13277 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13278 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13279 {
13280 xassert (row->enabled_p);
13281 row_found = row;
13282 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13283 break;
13284 ++row;
13285 }
13286
13287 return row_found;
13288 }
13289
13290
13291 /* Return the last row in the current matrix of W that is not affected
13292 by changes at the start of current_buffer that occurred since W's
13293 current matrix was built. Value is null if no such row exists.
13294
13295 BEG_UNCHANGED us the number of characters unchanged at the start of
13296 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13297 first changed character in current_buffer. Characters at positions <
13298 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13299 when the current matrix was built. */
13300
13301 static struct glyph_row *
13302 find_last_unchanged_at_beg_row (w)
13303 struct window *w;
13304 {
13305 int first_changed_pos = BEG + BEG_UNCHANGED;
13306 struct glyph_row *row;
13307 struct glyph_row *row_found = NULL;
13308 int yb = window_text_bottom_y (w);
13309
13310 /* Find the last row displaying unchanged text. */
13311 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13312 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13313 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13314 {
13315 if (/* If row ends before first_changed_pos, it is unchanged,
13316 except in some case. */
13317 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13318 /* When row ends in ZV and we write at ZV it is not
13319 unchanged. */
13320 && !row->ends_at_zv_p
13321 /* When first_changed_pos is the end of a continued line,
13322 row is not unchanged because it may be no longer
13323 continued. */
13324 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13325 && (row->continued_p
13326 || row->exact_window_width_line_p)))
13327 row_found = row;
13328
13329 /* Stop if last visible row. */
13330 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13331 break;
13332
13333 ++row;
13334 }
13335
13336 return row_found;
13337 }
13338
13339
13340 /* Find the first glyph row in the current matrix of W that is not
13341 affected by changes at the end of current_buffer since the
13342 time W's current matrix was built.
13343
13344 Return in *DELTA the number of chars by which buffer positions in
13345 unchanged text at the end of current_buffer must be adjusted.
13346
13347 Return in *DELTA_BYTES the corresponding number of bytes.
13348
13349 Value is null if no such row exists, i.e. all rows are affected by
13350 changes. */
13351
13352 static struct glyph_row *
13353 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13354 struct window *w;
13355 int *delta, *delta_bytes;
13356 {
13357 struct glyph_row *row;
13358 struct glyph_row *row_found = NULL;
13359
13360 *delta = *delta_bytes = 0;
13361
13362 /* Display must not have been paused, otherwise the current matrix
13363 is not up to date. */
13364 if (NILP (w->window_end_valid))
13365 abort ();
13366
13367 /* A value of window_end_pos >= END_UNCHANGED means that the window
13368 end is in the range of changed text. If so, there is no
13369 unchanged row at the end of W's current matrix. */
13370 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13371 return NULL;
13372
13373 /* Set row to the last row in W's current matrix displaying text. */
13374 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13375
13376 /* If matrix is entirely empty, no unchanged row exists. */
13377 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13378 {
13379 /* The value of row is the last glyph row in the matrix having a
13380 meaningful buffer position in it. The end position of row
13381 corresponds to window_end_pos. This allows us to translate
13382 buffer positions in the current matrix to current buffer
13383 positions for characters not in changed text. */
13384 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13385 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13386 int last_unchanged_pos, last_unchanged_pos_old;
13387 struct glyph_row *first_text_row
13388 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13389
13390 *delta = Z - Z_old;
13391 *delta_bytes = Z_BYTE - Z_BYTE_old;
13392
13393 /* Set last_unchanged_pos to the buffer position of the last
13394 character in the buffer that has not been changed. Z is the
13395 index + 1 of the last character in current_buffer, i.e. by
13396 subtracting END_UNCHANGED we get the index of the last
13397 unchanged character, and we have to add BEG to get its buffer
13398 position. */
13399 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13400 last_unchanged_pos_old = last_unchanged_pos - *delta;
13401
13402 /* Search backward from ROW for a row displaying a line that
13403 starts at a minimum position >= last_unchanged_pos_old. */
13404 for (; row > first_text_row; --row)
13405 {
13406 /* This used to abort, but it can happen.
13407 It is ok to just stop the search instead here. KFS. */
13408 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13409 break;
13410
13411 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13412 row_found = row;
13413 }
13414 }
13415
13416 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13417 abort ();
13418
13419 return row_found;
13420 }
13421
13422
13423 /* Make sure that glyph rows in the current matrix of window W
13424 reference the same glyph memory as corresponding rows in the
13425 frame's frame matrix. This function is called after scrolling W's
13426 current matrix on a terminal frame in try_window_id and
13427 try_window_reusing_current_matrix. */
13428
13429 static void
13430 sync_frame_with_window_matrix_rows (w)
13431 struct window *w;
13432 {
13433 struct frame *f = XFRAME (w->frame);
13434 struct glyph_row *window_row, *window_row_end, *frame_row;
13435
13436 /* Preconditions: W must be a leaf window and full-width. Its frame
13437 must have a frame matrix. */
13438 xassert (NILP (w->hchild) && NILP (w->vchild));
13439 xassert (WINDOW_FULL_WIDTH_P (w));
13440 xassert (!FRAME_WINDOW_P (f));
13441
13442 /* If W is a full-width window, glyph pointers in W's current matrix
13443 have, by definition, to be the same as glyph pointers in the
13444 corresponding frame matrix. Note that frame matrices have no
13445 marginal areas (see build_frame_matrix). */
13446 window_row = w->current_matrix->rows;
13447 window_row_end = window_row + w->current_matrix->nrows;
13448 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13449 while (window_row < window_row_end)
13450 {
13451 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13452 struct glyph *end = window_row->glyphs[LAST_AREA];
13453
13454 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13455 frame_row->glyphs[TEXT_AREA] = start;
13456 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13457 frame_row->glyphs[LAST_AREA] = end;
13458
13459 /* Disable frame rows whose corresponding window rows have
13460 been disabled in try_window_id. */
13461 if (!window_row->enabled_p)
13462 frame_row->enabled_p = 0;
13463
13464 ++window_row, ++frame_row;
13465 }
13466 }
13467
13468
13469 /* Find the glyph row in window W containing CHARPOS. Consider all
13470 rows between START and END (not inclusive). END null means search
13471 all rows to the end of the display area of W. Value is the row
13472 containing CHARPOS or null. */
13473
13474 struct glyph_row *
13475 row_containing_pos (w, charpos, start, end, dy)
13476 struct window *w;
13477 int charpos;
13478 struct glyph_row *start, *end;
13479 int dy;
13480 {
13481 struct glyph_row *row = start;
13482 int last_y;
13483
13484 /* If we happen to start on a header-line, skip that. */
13485 if (row->mode_line_p)
13486 ++row;
13487
13488 if ((end && row >= end) || !row->enabled_p)
13489 return NULL;
13490
13491 last_y = window_text_bottom_y (w) - dy;
13492
13493 while (1)
13494 {
13495 /* Give up if we have gone too far. */
13496 if (end && row >= end)
13497 return NULL;
13498 /* This formerly returned if they were equal.
13499 I think that both quantities are of a "last plus one" type;
13500 if so, when they are equal, the row is within the screen. -- rms. */
13501 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13502 return NULL;
13503
13504 /* If it is in this row, return this row. */
13505 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13506 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13507 /* The end position of a row equals the start
13508 position of the next row. If CHARPOS is there, we
13509 would rather display it in the next line, except
13510 when this line ends in ZV. */
13511 && !row->ends_at_zv_p
13512 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13513 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13514 return row;
13515 ++row;
13516 }
13517 }
13518
13519
13520 /* Try to redisplay window W by reusing its existing display. W's
13521 current matrix must be up to date when this function is called,
13522 i.e. window_end_valid must not be nil.
13523
13524 Value is
13525
13526 1 if display has been updated
13527 0 if otherwise unsuccessful
13528 -1 if redisplay with same window start is known not to succeed
13529
13530 The following steps are performed:
13531
13532 1. Find the last row in the current matrix of W that is not
13533 affected by changes at the start of current_buffer. If no such row
13534 is found, give up.
13535
13536 2. Find the first row in W's current matrix that is not affected by
13537 changes at the end of current_buffer. Maybe there is no such row.
13538
13539 3. Display lines beginning with the row + 1 found in step 1 to the
13540 row found in step 2 or, if step 2 didn't find a row, to the end of
13541 the window.
13542
13543 4. If cursor is not known to appear on the window, give up.
13544
13545 5. If display stopped at the row found in step 2, scroll the
13546 display and current matrix as needed.
13547
13548 6. Maybe display some lines at the end of W, if we must. This can
13549 happen under various circumstances, like a partially visible line
13550 becoming fully visible, or because newly displayed lines are displayed
13551 in smaller font sizes.
13552
13553 7. Update W's window end information. */
13554
13555 static int
13556 try_window_id (w)
13557 struct window *w;
13558 {
13559 struct frame *f = XFRAME (w->frame);
13560 struct glyph_matrix *current_matrix = w->current_matrix;
13561 struct glyph_matrix *desired_matrix = w->desired_matrix;
13562 struct glyph_row *last_unchanged_at_beg_row;
13563 struct glyph_row *first_unchanged_at_end_row;
13564 struct glyph_row *row;
13565 struct glyph_row *bottom_row;
13566 int bottom_vpos;
13567 struct it it;
13568 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13569 struct text_pos start_pos;
13570 struct run run;
13571 int first_unchanged_at_end_vpos = 0;
13572 struct glyph_row *last_text_row, *last_text_row_at_end;
13573 struct text_pos start;
13574 int first_changed_charpos, last_changed_charpos;
13575
13576 #if GLYPH_DEBUG
13577 if (inhibit_try_window_id)
13578 return 0;
13579 #endif
13580
13581 /* This is handy for debugging. */
13582 #if 0
13583 #define GIVE_UP(X) \
13584 do { \
13585 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13586 return 0; \
13587 } while (0)
13588 #else
13589 #define GIVE_UP(X) return 0
13590 #endif
13591
13592 SET_TEXT_POS_FROM_MARKER (start, w->start);
13593
13594 /* Don't use this for mini-windows because these can show
13595 messages and mini-buffers, and we don't handle that here. */
13596 if (MINI_WINDOW_P (w))
13597 GIVE_UP (1);
13598
13599 /* This flag is used to prevent redisplay optimizations. */
13600 if (windows_or_buffers_changed || cursor_type_changed)
13601 GIVE_UP (2);
13602
13603 /* Verify that narrowing has not changed.
13604 Also verify that we were not told to prevent redisplay optimizations.
13605 It would be nice to further
13606 reduce the number of cases where this prevents try_window_id. */
13607 if (current_buffer->clip_changed
13608 || current_buffer->prevent_redisplay_optimizations_p)
13609 GIVE_UP (3);
13610
13611 /* Window must either use window-based redisplay or be full width. */
13612 if (!FRAME_WINDOW_P (f)
13613 && (!line_ins_del_ok
13614 || !WINDOW_FULL_WIDTH_P (w)))
13615 GIVE_UP (4);
13616
13617 /* Give up if point is not known NOT to appear in W. */
13618 if (PT < CHARPOS (start))
13619 GIVE_UP (5);
13620
13621 /* Another way to prevent redisplay optimizations. */
13622 if (XFASTINT (w->last_modified) == 0)
13623 GIVE_UP (6);
13624
13625 /* Verify that window is not hscrolled. */
13626 if (XFASTINT (w->hscroll) != 0)
13627 GIVE_UP (7);
13628
13629 /* Verify that display wasn't paused. */
13630 if (NILP (w->window_end_valid))
13631 GIVE_UP (8);
13632
13633 /* Can't use this if highlighting a region because a cursor movement
13634 will do more than just set the cursor. */
13635 if (!NILP (Vtransient_mark_mode)
13636 && !NILP (current_buffer->mark_active))
13637 GIVE_UP (9);
13638
13639 /* Likewise if highlighting trailing whitespace. */
13640 if (!NILP (Vshow_trailing_whitespace))
13641 GIVE_UP (11);
13642
13643 /* Likewise if showing a region. */
13644 if (!NILP (w->region_showing))
13645 GIVE_UP (10);
13646
13647 /* Can use this if overlay arrow position and or string have changed. */
13648 if (overlay_arrows_changed_p ())
13649 GIVE_UP (12);
13650
13651
13652 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13653 only if buffer has really changed. The reason is that the gap is
13654 initially at Z for freshly visited files. The code below would
13655 set end_unchanged to 0 in that case. */
13656 if (MODIFF > SAVE_MODIFF
13657 /* This seems to happen sometimes after saving a buffer. */
13658 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13659 {
13660 if (GPT - BEG < BEG_UNCHANGED)
13661 BEG_UNCHANGED = GPT - BEG;
13662 if (Z - GPT < END_UNCHANGED)
13663 END_UNCHANGED = Z - GPT;
13664 }
13665
13666 /* The position of the first and last character that has been changed. */
13667 first_changed_charpos = BEG + BEG_UNCHANGED;
13668 last_changed_charpos = Z - END_UNCHANGED;
13669
13670 /* If window starts after a line end, and the last change is in
13671 front of that newline, then changes don't affect the display.
13672 This case happens with stealth-fontification. Note that although
13673 the display is unchanged, glyph positions in the matrix have to
13674 be adjusted, of course. */
13675 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13676 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13677 && ((last_changed_charpos < CHARPOS (start)
13678 && CHARPOS (start) == BEGV)
13679 || (last_changed_charpos < CHARPOS (start) - 1
13680 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13681 {
13682 int Z_old, delta, Z_BYTE_old, delta_bytes;
13683 struct glyph_row *r0;
13684
13685 /* Compute how many chars/bytes have been added to or removed
13686 from the buffer. */
13687 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13688 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13689 delta = Z - Z_old;
13690 delta_bytes = Z_BYTE - Z_BYTE_old;
13691
13692 /* Give up if PT is not in the window. Note that it already has
13693 been checked at the start of try_window_id that PT is not in
13694 front of the window start. */
13695 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13696 GIVE_UP (13);
13697
13698 /* If window start is unchanged, we can reuse the whole matrix
13699 as is, after adjusting glyph positions. No need to compute
13700 the window end again, since its offset from Z hasn't changed. */
13701 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13702 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13703 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13704 /* PT must not be in a partially visible line. */
13705 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13706 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13707 {
13708 /* Adjust positions in the glyph matrix. */
13709 if (delta || delta_bytes)
13710 {
13711 struct glyph_row *r1
13712 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13713 increment_matrix_positions (w->current_matrix,
13714 MATRIX_ROW_VPOS (r0, current_matrix),
13715 MATRIX_ROW_VPOS (r1, current_matrix),
13716 delta, delta_bytes);
13717 }
13718
13719 /* Set the cursor. */
13720 row = row_containing_pos (w, PT, r0, NULL, 0);
13721 if (row)
13722 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13723 else
13724 abort ();
13725 return 1;
13726 }
13727 }
13728
13729 /* Handle the case that changes are all below what is displayed in
13730 the window, and that PT is in the window. This shortcut cannot
13731 be taken if ZV is visible in the window, and text has been added
13732 there that is visible in the window. */
13733 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13734 /* ZV is not visible in the window, or there are no
13735 changes at ZV, actually. */
13736 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13737 || first_changed_charpos == last_changed_charpos))
13738 {
13739 struct glyph_row *r0;
13740
13741 /* Give up if PT is not in the window. Note that it already has
13742 been checked at the start of try_window_id that PT is not in
13743 front of the window start. */
13744 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13745 GIVE_UP (14);
13746
13747 /* If window start is unchanged, we can reuse the whole matrix
13748 as is, without changing glyph positions since no text has
13749 been added/removed in front of the window end. */
13750 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13751 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13752 /* PT must not be in a partially visible line. */
13753 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13754 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13755 {
13756 /* We have to compute the window end anew since text
13757 can have been added/removed after it. */
13758 w->window_end_pos
13759 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13760 w->window_end_bytepos
13761 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13762
13763 /* Set the cursor. */
13764 row = row_containing_pos (w, PT, r0, NULL, 0);
13765 if (row)
13766 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13767 else
13768 abort ();
13769 return 2;
13770 }
13771 }
13772
13773 /* Give up if window start is in the changed area.
13774
13775 The condition used to read
13776
13777 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13778
13779 but why that was tested escapes me at the moment. */
13780 if (CHARPOS (start) >= first_changed_charpos
13781 && CHARPOS (start) <= last_changed_charpos)
13782 GIVE_UP (15);
13783
13784 /* Check that window start agrees with the start of the first glyph
13785 row in its current matrix. Check this after we know the window
13786 start is not in changed text, otherwise positions would not be
13787 comparable. */
13788 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13789 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13790 GIVE_UP (16);
13791
13792 /* Give up if the window ends in strings. Overlay strings
13793 at the end are difficult to handle, so don't try. */
13794 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13795 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13796 GIVE_UP (20);
13797
13798 /* Compute the position at which we have to start displaying new
13799 lines. Some of the lines at the top of the window might be
13800 reusable because they are not displaying changed text. Find the
13801 last row in W's current matrix not affected by changes at the
13802 start of current_buffer. Value is null if changes start in the
13803 first line of window. */
13804 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13805 if (last_unchanged_at_beg_row)
13806 {
13807 /* Avoid starting to display in the moddle of a character, a TAB
13808 for instance. This is easier than to set up the iterator
13809 exactly, and it's not a frequent case, so the additional
13810 effort wouldn't really pay off. */
13811 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13812 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13813 && last_unchanged_at_beg_row > w->current_matrix->rows)
13814 --last_unchanged_at_beg_row;
13815
13816 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13817 GIVE_UP (17);
13818
13819 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13820 GIVE_UP (18);
13821 start_pos = it.current.pos;
13822
13823 /* Start displaying new lines in the desired matrix at the same
13824 vpos we would use in the current matrix, i.e. below
13825 last_unchanged_at_beg_row. */
13826 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13827 current_matrix);
13828 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13829 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13830
13831 xassert (it.hpos == 0 && it.current_x == 0);
13832 }
13833 else
13834 {
13835 /* There are no reusable lines at the start of the window.
13836 Start displaying in the first text line. */
13837 start_display (&it, w, start);
13838 it.vpos = it.first_vpos;
13839 start_pos = it.current.pos;
13840 }
13841
13842 /* Find the first row that is not affected by changes at the end of
13843 the buffer. Value will be null if there is no unchanged row, in
13844 which case we must redisplay to the end of the window. delta
13845 will be set to the value by which buffer positions beginning with
13846 first_unchanged_at_end_row have to be adjusted due to text
13847 changes. */
13848 first_unchanged_at_end_row
13849 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13850 IF_DEBUG (debug_delta = delta);
13851 IF_DEBUG (debug_delta_bytes = delta_bytes);
13852
13853 /* Set stop_pos to the buffer position up to which we will have to
13854 display new lines. If first_unchanged_at_end_row != NULL, this
13855 is the buffer position of the start of the line displayed in that
13856 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13857 that we don't stop at a buffer position. */
13858 stop_pos = 0;
13859 if (first_unchanged_at_end_row)
13860 {
13861 xassert (last_unchanged_at_beg_row == NULL
13862 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13863
13864 /* If this is a continuation line, move forward to the next one
13865 that isn't. Changes in lines above affect this line.
13866 Caution: this may move first_unchanged_at_end_row to a row
13867 not displaying text. */
13868 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13869 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13870 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13871 < it.last_visible_y))
13872 ++first_unchanged_at_end_row;
13873
13874 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13875 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13876 >= it.last_visible_y))
13877 first_unchanged_at_end_row = NULL;
13878 else
13879 {
13880 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13881 + delta);
13882 first_unchanged_at_end_vpos
13883 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13884 xassert (stop_pos >= Z - END_UNCHANGED);
13885 }
13886 }
13887 else if (last_unchanged_at_beg_row == NULL)
13888 GIVE_UP (19);
13889
13890
13891 #if GLYPH_DEBUG
13892
13893 /* Either there is no unchanged row at the end, or the one we have
13894 now displays text. This is a necessary condition for the window
13895 end pos calculation at the end of this function. */
13896 xassert (first_unchanged_at_end_row == NULL
13897 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13898
13899 debug_last_unchanged_at_beg_vpos
13900 = (last_unchanged_at_beg_row
13901 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13902 : -1);
13903 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13904
13905 #endif /* GLYPH_DEBUG != 0 */
13906
13907
13908 /* Display new lines. Set last_text_row to the last new line
13909 displayed which has text on it, i.e. might end up as being the
13910 line where the window_end_vpos is. */
13911 w->cursor.vpos = -1;
13912 last_text_row = NULL;
13913 overlay_arrow_seen = 0;
13914 while (it.current_y < it.last_visible_y
13915 && !fonts_changed_p
13916 && (first_unchanged_at_end_row == NULL
13917 || IT_CHARPOS (it) < stop_pos))
13918 {
13919 if (display_line (&it))
13920 last_text_row = it.glyph_row - 1;
13921 }
13922
13923 if (fonts_changed_p)
13924 return -1;
13925
13926
13927 /* Compute differences in buffer positions, y-positions etc. for
13928 lines reused at the bottom of the window. Compute what we can
13929 scroll. */
13930 if (first_unchanged_at_end_row
13931 /* No lines reused because we displayed everything up to the
13932 bottom of the window. */
13933 && it.current_y < it.last_visible_y)
13934 {
13935 dvpos = (it.vpos
13936 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13937 current_matrix));
13938 dy = it.current_y - first_unchanged_at_end_row->y;
13939 run.current_y = first_unchanged_at_end_row->y;
13940 run.desired_y = run.current_y + dy;
13941 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13942 }
13943 else
13944 {
13945 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13946 first_unchanged_at_end_row = NULL;
13947 }
13948 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13949
13950
13951 /* Find the cursor if not already found. We have to decide whether
13952 PT will appear on this window (it sometimes doesn't, but this is
13953 not a very frequent case.) This decision has to be made before
13954 the current matrix is altered. A value of cursor.vpos < 0 means
13955 that PT is either in one of the lines beginning at
13956 first_unchanged_at_end_row or below the window. Don't care for
13957 lines that might be displayed later at the window end; as
13958 mentioned, this is not a frequent case. */
13959 if (w->cursor.vpos < 0)
13960 {
13961 /* Cursor in unchanged rows at the top? */
13962 if (PT < CHARPOS (start_pos)
13963 && last_unchanged_at_beg_row)
13964 {
13965 row = row_containing_pos (w, PT,
13966 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13967 last_unchanged_at_beg_row + 1, 0);
13968 if (row)
13969 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13970 }
13971
13972 /* Start from first_unchanged_at_end_row looking for PT. */
13973 else if (first_unchanged_at_end_row)
13974 {
13975 row = row_containing_pos (w, PT - delta,
13976 first_unchanged_at_end_row, NULL, 0);
13977 if (row)
13978 set_cursor_from_row (w, row, w->current_matrix, delta,
13979 delta_bytes, dy, dvpos);
13980 }
13981
13982 /* Give up if cursor was not found. */
13983 if (w->cursor.vpos < 0)
13984 {
13985 clear_glyph_matrix (w->desired_matrix);
13986 return -1;
13987 }
13988 }
13989
13990 /* Don't let the cursor end in the scroll margins. */
13991 {
13992 int this_scroll_margin, cursor_height;
13993
13994 this_scroll_margin = max (0, scroll_margin);
13995 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13996 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13997 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13998
13999 if ((w->cursor.y < this_scroll_margin
14000 && CHARPOS (start) > BEGV)
14001 /* Old redisplay didn't take scroll margin into account at the bottom,
14002 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14003 || (w->cursor.y + (make_cursor_line_fully_visible_p
14004 ? cursor_height + this_scroll_margin
14005 : 1)) > it.last_visible_y)
14006 {
14007 w->cursor.vpos = -1;
14008 clear_glyph_matrix (w->desired_matrix);
14009 return -1;
14010 }
14011 }
14012
14013 /* Scroll the display. Do it before changing the current matrix so
14014 that xterm.c doesn't get confused about where the cursor glyph is
14015 found. */
14016 if (dy && run.height)
14017 {
14018 update_begin (f);
14019
14020 if (FRAME_WINDOW_P (f))
14021 {
14022 rif->update_window_begin_hook (w);
14023 rif->clear_window_mouse_face (w);
14024 rif->scroll_run_hook (w, &run);
14025 rif->update_window_end_hook (w, 0, 0);
14026 }
14027 else
14028 {
14029 /* Terminal frame. In this case, dvpos gives the number of
14030 lines to scroll by; dvpos < 0 means scroll up. */
14031 int first_unchanged_at_end_vpos
14032 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14033 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14034 int end = (WINDOW_TOP_EDGE_LINE (w)
14035 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14036 + window_internal_height (w));
14037
14038 /* Perform the operation on the screen. */
14039 if (dvpos > 0)
14040 {
14041 /* Scroll last_unchanged_at_beg_row to the end of the
14042 window down dvpos lines. */
14043 set_terminal_window (end);
14044
14045 /* On dumb terminals delete dvpos lines at the end
14046 before inserting dvpos empty lines. */
14047 if (!scroll_region_ok)
14048 ins_del_lines (end - dvpos, -dvpos);
14049
14050 /* Insert dvpos empty lines in front of
14051 last_unchanged_at_beg_row. */
14052 ins_del_lines (from, dvpos);
14053 }
14054 else if (dvpos < 0)
14055 {
14056 /* Scroll up last_unchanged_at_beg_vpos to the end of
14057 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14058 set_terminal_window (end);
14059
14060 /* Delete dvpos lines in front of
14061 last_unchanged_at_beg_vpos. ins_del_lines will set
14062 the cursor to the given vpos and emit |dvpos| delete
14063 line sequences. */
14064 ins_del_lines (from + dvpos, dvpos);
14065
14066 /* On a dumb terminal insert dvpos empty lines at the
14067 end. */
14068 if (!scroll_region_ok)
14069 ins_del_lines (end + dvpos, -dvpos);
14070 }
14071
14072 set_terminal_window (0);
14073 }
14074
14075 update_end (f);
14076 }
14077
14078 /* Shift reused rows of the current matrix to the right position.
14079 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14080 text. */
14081 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14082 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14083 if (dvpos < 0)
14084 {
14085 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14086 bottom_vpos, dvpos);
14087 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14088 bottom_vpos, 0);
14089 }
14090 else if (dvpos > 0)
14091 {
14092 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14093 bottom_vpos, dvpos);
14094 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14095 first_unchanged_at_end_vpos + dvpos, 0);
14096 }
14097
14098 /* For frame-based redisplay, make sure that current frame and window
14099 matrix are in sync with respect to glyph memory. */
14100 if (!FRAME_WINDOW_P (f))
14101 sync_frame_with_window_matrix_rows (w);
14102
14103 /* Adjust buffer positions in reused rows. */
14104 if (delta)
14105 increment_matrix_positions (current_matrix,
14106 first_unchanged_at_end_vpos + dvpos,
14107 bottom_vpos, delta, delta_bytes);
14108
14109 /* Adjust Y positions. */
14110 if (dy)
14111 shift_glyph_matrix (w, current_matrix,
14112 first_unchanged_at_end_vpos + dvpos,
14113 bottom_vpos, dy);
14114
14115 if (first_unchanged_at_end_row)
14116 {
14117 first_unchanged_at_end_row += dvpos;
14118 if (first_unchanged_at_end_row->y >= it.last_visible_y
14119 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14120 first_unchanged_at_end_row = NULL;
14121 }
14122
14123 /* If scrolling up, there may be some lines to display at the end of
14124 the window. */
14125 last_text_row_at_end = NULL;
14126 if (dy < 0)
14127 {
14128 /* Scrolling up can leave for example a partially visible line
14129 at the end of the window to be redisplayed. */
14130 /* Set last_row to the glyph row in the current matrix where the
14131 window end line is found. It has been moved up or down in
14132 the matrix by dvpos. */
14133 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14134 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14135
14136 /* If last_row is the window end line, it should display text. */
14137 xassert (last_row->displays_text_p);
14138
14139 /* If window end line was partially visible before, begin
14140 displaying at that line. Otherwise begin displaying with the
14141 line following it. */
14142 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14143 {
14144 init_to_row_start (&it, w, last_row);
14145 it.vpos = last_vpos;
14146 it.current_y = last_row->y;
14147 }
14148 else
14149 {
14150 init_to_row_end (&it, w, last_row);
14151 it.vpos = 1 + last_vpos;
14152 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14153 ++last_row;
14154 }
14155
14156 /* We may start in a continuation line. If so, we have to
14157 get the right continuation_lines_width and current_x. */
14158 it.continuation_lines_width = last_row->continuation_lines_width;
14159 it.hpos = it.current_x = 0;
14160
14161 /* Display the rest of the lines at the window end. */
14162 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14163 while (it.current_y < it.last_visible_y
14164 && !fonts_changed_p)
14165 {
14166 /* Is it always sure that the display agrees with lines in
14167 the current matrix? I don't think so, so we mark rows
14168 displayed invalid in the current matrix by setting their
14169 enabled_p flag to zero. */
14170 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14171 if (display_line (&it))
14172 last_text_row_at_end = it.glyph_row - 1;
14173 }
14174 }
14175
14176 /* Update window_end_pos and window_end_vpos. */
14177 if (first_unchanged_at_end_row
14178 && !last_text_row_at_end)
14179 {
14180 /* Window end line if one of the preserved rows from the current
14181 matrix. Set row to the last row displaying text in current
14182 matrix starting at first_unchanged_at_end_row, after
14183 scrolling. */
14184 xassert (first_unchanged_at_end_row->displays_text_p);
14185 row = find_last_row_displaying_text (w->current_matrix, &it,
14186 first_unchanged_at_end_row);
14187 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14188
14189 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14190 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14191 w->window_end_vpos
14192 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14193 xassert (w->window_end_bytepos >= 0);
14194 IF_DEBUG (debug_method_add (w, "A"));
14195 }
14196 else if (last_text_row_at_end)
14197 {
14198 w->window_end_pos
14199 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14200 w->window_end_bytepos
14201 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14202 w->window_end_vpos
14203 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14204 xassert (w->window_end_bytepos >= 0);
14205 IF_DEBUG (debug_method_add (w, "B"));
14206 }
14207 else if (last_text_row)
14208 {
14209 /* We have displayed either to the end of the window or at the
14210 end of the window, i.e. the last row with text is to be found
14211 in the desired matrix. */
14212 w->window_end_pos
14213 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14214 w->window_end_bytepos
14215 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14216 w->window_end_vpos
14217 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14218 xassert (w->window_end_bytepos >= 0);
14219 }
14220 else if (first_unchanged_at_end_row == NULL
14221 && last_text_row == NULL
14222 && last_text_row_at_end == NULL)
14223 {
14224 /* Displayed to end of window, but no line containing text was
14225 displayed. Lines were deleted at the end of the window. */
14226 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14227 int vpos = XFASTINT (w->window_end_vpos);
14228 struct glyph_row *current_row = current_matrix->rows + vpos;
14229 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14230
14231 for (row = NULL;
14232 row == NULL && vpos >= first_vpos;
14233 --vpos, --current_row, --desired_row)
14234 {
14235 if (desired_row->enabled_p)
14236 {
14237 if (desired_row->displays_text_p)
14238 row = desired_row;
14239 }
14240 else if (current_row->displays_text_p)
14241 row = current_row;
14242 }
14243
14244 xassert (row != NULL);
14245 w->window_end_vpos = make_number (vpos + 1);
14246 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14247 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14248 xassert (w->window_end_bytepos >= 0);
14249 IF_DEBUG (debug_method_add (w, "C"));
14250 }
14251 else
14252 abort ();
14253
14254 #if 0 /* This leads to problems, for instance when the cursor is
14255 at ZV, and the cursor line displays no text. */
14256 /* Disable rows below what's displayed in the window. This makes
14257 debugging easier. */
14258 enable_glyph_matrix_rows (current_matrix,
14259 XFASTINT (w->window_end_vpos) + 1,
14260 bottom_vpos, 0);
14261 #endif
14262
14263 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14264 debug_end_vpos = XFASTINT (w->window_end_vpos));
14265
14266 /* Record that display has not been completed. */
14267 w->window_end_valid = Qnil;
14268 w->desired_matrix->no_scrolling_p = 1;
14269 return 3;
14270
14271 #undef GIVE_UP
14272 }
14273
14274
14275 \f
14276 /***********************************************************************
14277 More debugging support
14278 ***********************************************************************/
14279
14280 #if GLYPH_DEBUG
14281
14282 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14283 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14284 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14285
14286
14287 /* Dump the contents of glyph matrix MATRIX on stderr.
14288
14289 GLYPHS 0 means don't show glyph contents.
14290 GLYPHS 1 means show glyphs in short form
14291 GLYPHS > 1 means show glyphs in long form. */
14292
14293 void
14294 dump_glyph_matrix (matrix, glyphs)
14295 struct glyph_matrix *matrix;
14296 int glyphs;
14297 {
14298 int i;
14299 for (i = 0; i < matrix->nrows; ++i)
14300 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14301 }
14302
14303
14304 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14305 the glyph row and area where the glyph comes from. */
14306
14307 void
14308 dump_glyph (row, glyph, area)
14309 struct glyph_row *row;
14310 struct glyph *glyph;
14311 int area;
14312 {
14313 if (glyph->type == CHAR_GLYPH)
14314 {
14315 fprintf (stderr,
14316 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14317 glyph - row->glyphs[TEXT_AREA],
14318 'C',
14319 glyph->charpos,
14320 (BUFFERP (glyph->object)
14321 ? 'B'
14322 : (STRINGP (glyph->object)
14323 ? 'S'
14324 : '-')),
14325 glyph->pixel_width,
14326 glyph->u.ch,
14327 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14328 ? glyph->u.ch
14329 : '.'),
14330 glyph->face_id,
14331 glyph->left_box_line_p,
14332 glyph->right_box_line_p);
14333 }
14334 else if (glyph->type == STRETCH_GLYPH)
14335 {
14336 fprintf (stderr,
14337 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14338 glyph - row->glyphs[TEXT_AREA],
14339 'S',
14340 glyph->charpos,
14341 (BUFFERP (glyph->object)
14342 ? 'B'
14343 : (STRINGP (glyph->object)
14344 ? 'S'
14345 : '-')),
14346 glyph->pixel_width,
14347 0,
14348 '.',
14349 glyph->face_id,
14350 glyph->left_box_line_p,
14351 glyph->right_box_line_p);
14352 }
14353 else if (glyph->type == IMAGE_GLYPH)
14354 {
14355 fprintf (stderr,
14356 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14357 glyph - row->glyphs[TEXT_AREA],
14358 'I',
14359 glyph->charpos,
14360 (BUFFERP (glyph->object)
14361 ? 'B'
14362 : (STRINGP (glyph->object)
14363 ? 'S'
14364 : '-')),
14365 glyph->pixel_width,
14366 glyph->u.img_id,
14367 '.',
14368 glyph->face_id,
14369 glyph->left_box_line_p,
14370 glyph->right_box_line_p);
14371 }
14372 }
14373
14374
14375 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14376 GLYPHS 0 means don't show glyph contents.
14377 GLYPHS 1 means show glyphs in short form
14378 GLYPHS > 1 means show glyphs in long form. */
14379
14380 void
14381 dump_glyph_row (row, vpos, glyphs)
14382 struct glyph_row *row;
14383 int vpos, glyphs;
14384 {
14385 if (glyphs != 1)
14386 {
14387 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14388 fprintf (stderr, "======================================================================\n");
14389
14390 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14391 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14392 vpos,
14393 MATRIX_ROW_START_CHARPOS (row),
14394 MATRIX_ROW_END_CHARPOS (row),
14395 row->used[TEXT_AREA],
14396 row->contains_overlapping_glyphs_p,
14397 row->enabled_p,
14398 row->truncated_on_left_p,
14399 row->truncated_on_right_p,
14400 row->continued_p,
14401 MATRIX_ROW_CONTINUATION_LINE_P (row),
14402 row->displays_text_p,
14403 row->ends_at_zv_p,
14404 row->fill_line_p,
14405 row->ends_in_middle_of_char_p,
14406 row->starts_in_middle_of_char_p,
14407 row->mouse_face_p,
14408 row->x,
14409 row->y,
14410 row->pixel_width,
14411 row->height,
14412 row->visible_height,
14413 row->ascent,
14414 row->phys_ascent);
14415 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14416 row->end.overlay_string_index,
14417 row->continuation_lines_width);
14418 fprintf (stderr, "%9d %5d\n",
14419 CHARPOS (row->start.string_pos),
14420 CHARPOS (row->end.string_pos));
14421 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14422 row->end.dpvec_index);
14423 }
14424
14425 if (glyphs > 1)
14426 {
14427 int area;
14428
14429 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14430 {
14431 struct glyph *glyph = row->glyphs[area];
14432 struct glyph *glyph_end = glyph + row->used[area];
14433
14434 /* Glyph for a line end in text. */
14435 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14436 ++glyph_end;
14437
14438 if (glyph < glyph_end)
14439 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14440
14441 for (; glyph < glyph_end; ++glyph)
14442 dump_glyph (row, glyph, area);
14443 }
14444 }
14445 else if (glyphs == 1)
14446 {
14447 int area;
14448
14449 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14450 {
14451 char *s = (char *) alloca (row->used[area] + 1);
14452 int i;
14453
14454 for (i = 0; i < row->used[area]; ++i)
14455 {
14456 struct glyph *glyph = row->glyphs[area] + i;
14457 if (glyph->type == CHAR_GLYPH
14458 && glyph->u.ch < 0x80
14459 && glyph->u.ch >= ' ')
14460 s[i] = glyph->u.ch;
14461 else
14462 s[i] = '.';
14463 }
14464
14465 s[i] = '\0';
14466 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14467 }
14468 }
14469 }
14470
14471
14472 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14473 Sdump_glyph_matrix, 0, 1, "p",
14474 doc: /* Dump the current matrix of the selected window to stderr.
14475 Shows contents of glyph row structures. With non-nil
14476 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14477 glyphs in short form, otherwise show glyphs in long form. */)
14478 (glyphs)
14479 Lisp_Object glyphs;
14480 {
14481 struct window *w = XWINDOW (selected_window);
14482 struct buffer *buffer = XBUFFER (w->buffer);
14483
14484 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14485 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14486 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14487 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14488 fprintf (stderr, "=============================================\n");
14489 dump_glyph_matrix (w->current_matrix,
14490 NILP (glyphs) ? 0 : XINT (glyphs));
14491 return Qnil;
14492 }
14493
14494
14495 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14496 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14497 ()
14498 {
14499 struct frame *f = XFRAME (selected_frame);
14500 dump_glyph_matrix (f->current_matrix, 1);
14501 return Qnil;
14502 }
14503
14504
14505 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14506 doc: /* Dump glyph row ROW to stderr.
14507 GLYPH 0 means don't dump glyphs.
14508 GLYPH 1 means dump glyphs in short form.
14509 GLYPH > 1 or omitted means dump glyphs in long form. */)
14510 (row, glyphs)
14511 Lisp_Object row, glyphs;
14512 {
14513 struct glyph_matrix *matrix;
14514 int vpos;
14515
14516 CHECK_NUMBER (row);
14517 matrix = XWINDOW (selected_window)->current_matrix;
14518 vpos = XINT (row);
14519 if (vpos >= 0 && vpos < matrix->nrows)
14520 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14521 vpos,
14522 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14523 return Qnil;
14524 }
14525
14526
14527 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14528 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14529 GLYPH 0 means don't dump glyphs.
14530 GLYPH 1 means dump glyphs in short form.
14531 GLYPH > 1 or omitted means dump glyphs in long form. */)
14532 (row, glyphs)
14533 Lisp_Object row, glyphs;
14534 {
14535 struct frame *sf = SELECTED_FRAME ();
14536 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14537 int vpos;
14538
14539 CHECK_NUMBER (row);
14540 vpos = XINT (row);
14541 if (vpos >= 0 && vpos < m->nrows)
14542 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14543 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14544 return Qnil;
14545 }
14546
14547
14548 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14549 doc: /* Toggle tracing of redisplay.
14550 With ARG, turn tracing on if and only if ARG is positive. */)
14551 (arg)
14552 Lisp_Object arg;
14553 {
14554 if (NILP (arg))
14555 trace_redisplay_p = !trace_redisplay_p;
14556 else
14557 {
14558 arg = Fprefix_numeric_value (arg);
14559 trace_redisplay_p = XINT (arg) > 0;
14560 }
14561
14562 return Qnil;
14563 }
14564
14565
14566 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14567 doc: /* Like `format', but print result to stderr.
14568 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14569 (nargs, args)
14570 int nargs;
14571 Lisp_Object *args;
14572 {
14573 Lisp_Object s = Fformat (nargs, args);
14574 fprintf (stderr, "%s", SDATA (s));
14575 return Qnil;
14576 }
14577
14578 #endif /* GLYPH_DEBUG */
14579
14580
14581 \f
14582 /***********************************************************************
14583 Building Desired Matrix Rows
14584 ***********************************************************************/
14585
14586 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14587 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14588
14589 static struct glyph_row *
14590 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14591 struct window *w;
14592 Lisp_Object overlay_arrow_string;
14593 {
14594 struct frame *f = XFRAME (WINDOW_FRAME (w));
14595 struct buffer *buffer = XBUFFER (w->buffer);
14596 struct buffer *old = current_buffer;
14597 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14598 int arrow_len = SCHARS (overlay_arrow_string);
14599 const unsigned char *arrow_end = arrow_string + arrow_len;
14600 const unsigned char *p;
14601 struct it it;
14602 int multibyte_p;
14603 int n_glyphs_before;
14604
14605 set_buffer_temp (buffer);
14606 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14607 it.glyph_row->used[TEXT_AREA] = 0;
14608 SET_TEXT_POS (it.position, 0, 0);
14609
14610 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14611 p = arrow_string;
14612 while (p < arrow_end)
14613 {
14614 Lisp_Object face, ilisp;
14615
14616 /* Get the next character. */
14617 if (multibyte_p)
14618 it.c = string_char_and_length (p, arrow_len, &it.len);
14619 else
14620 it.c = *p, it.len = 1;
14621 p += it.len;
14622
14623 /* Get its face. */
14624 ilisp = make_number (p - arrow_string);
14625 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14626 it.face_id = compute_char_face (f, it.c, face);
14627
14628 /* Compute its width, get its glyphs. */
14629 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14630 SET_TEXT_POS (it.position, -1, -1);
14631 PRODUCE_GLYPHS (&it);
14632
14633 /* If this character doesn't fit any more in the line, we have
14634 to remove some glyphs. */
14635 if (it.current_x > it.last_visible_x)
14636 {
14637 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14638 break;
14639 }
14640 }
14641
14642 set_buffer_temp (old);
14643 return it.glyph_row;
14644 }
14645
14646
14647 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14648 glyphs are only inserted for terminal frames since we can't really
14649 win with truncation glyphs when partially visible glyphs are
14650 involved. Which glyphs to insert is determined by
14651 produce_special_glyphs. */
14652
14653 static void
14654 insert_left_trunc_glyphs (it)
14655 struct it *it;
14656 {
14657 struct it truncate_it;
14658 struct glyph *from, *end, *to, *toend;
14659
14660 xassert (!FRAME_WINDOW_P (it->f));
14661
14662 /* Get the truncation glyphs. */
14663 truncate_it = *it;
14664 truncate_it.current_x = 0;
14665 truncate_it.face_id = DEFAULT_FACE_ID;
14666 truncate_it.glyph_row = &scratch_glyph_row;
14667 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14668 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14669 truncate_it.object = make_number (0);
14670 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14671
14672 /* Overwrite glyphs from IT with truncation glyphs. */
14673 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14674 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14675 to = it->glyph_row->glyphs[TEXT_AREA];
14676 toend = to + it->glyph_row->used[TEXT_AREA];
14677
14678 while (from < end)
14679 *to++ = *from++;
14680
14681 /* There may be padding glyphs left over. Overwrite them too. */
14682 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14683 {
14684 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14685 while (from < end)
14686 *to++ = *from++;
14687 }
14688
14689 if (to > toend)
14690 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14691 }
14692
14693
14694 /* Compute the pixel height and width of IT->glyph_row.
14695
14696 Most of the time, ascent and height of a display line will be equal
14697 to the max_ascent and max_height values of the display iterator
14698 structure. This is not the case if
14699
14700 1. We hit ZV without displaying anything. In this case, max_ascent
14701 and max_height will be zero.
14702
14703 2. We have some glyphs that don't contribute to the line height.
14704 (The glyph row flag contributes_to_line_height_p is for future
14705 pixmap extensions).
14706
14707 The first case is easily covered by using default values because in
14708 these cases, the line height does not really matter, except that it
14709 must not be zero. */
14710
14711 static void
14712 compute_line_metrics (it)
14713 struct it *it;
14714 {
14715 struct glyph_row *row = it->glyph_row;
14716 int area, i;
14717
14718 if (FRAME_WINDOW_P (it->f))
14719 {
14720 int i, min_y, max_y;
14721
14722 /* The line may consist of one space only, that was added to
14723 place the cursor on it. If so, the row's height hasn't been
14724 computed yet. */
14725 if (row->height == 0)
14726 {
14727 if (it->max_ascent + it->max_descent == 0)
14728 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14729 row->ascent = it->max_ascent;
14730 row->height = it->max_ascent + it->max_descent;
14731 row->phys_ascent = it->max_phys_ascent;
14732 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14733 row->extra_line_spacing = it->max_extra_line_spacing;
14734 }
14735
14736 /* Compute the width of this line. */
14737 row->pixel_width = row->x;
14738 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14739 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14740
14741 xassert (row->pixel_width >= 0);
14742 xassert (row->ascent >= 0 && row->height > 0);
14743
14744 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14745 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14746
14747 /* If first line's physical ascent is larger than its logical
14748 ascent, use the physical ascent, and make the row taller.
14749 This makes accented characters fully visible. */
14750 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14751 && row->phys_ascent > row->ascent)
14752 {
14753 row->height += row->phys_ascent - row->ascent;
14754 row->ascent = row->phys_ascent;
14755 }
14756
14757 /* Compute how much of the line is visible. */
14758 row->visible_height = row->height;
14759
14760 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14761 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14762
14763 if (row->y < min_y)
14764 row->visible_height -= min_y - row->y;
14765 if (row->y + row->height > max_y)
14766 row->visible_height -= row->y + row->height - max_y;
14767 }
14768 else
14769 {
14770 row->pixel_width = row->used[TEXT_AREA];
14771 if (row->continued_p)
14772 row->pixel_width -= it->continuation_pixel_width;
14773 else if (row->truncated_on_right_p)
14774 row->pixel_width -= it->truncation_pixel_width;
14775 row->ascent = row->phys_ascent = 0;
14776 row->height = row->phys_height = row->visible_height = 1;
14777 row->extra_line_spacing = 0;
14778 }
14779
14780 /* Compute a hash code for this row. */
14781 row->hash = 0;
14782 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14783 for (i = 0; i < row->used[area]; ++i)
14784 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14785 + row->glyphs[area][i].u.val
14786 + row->glyphs[area][i].face_id
14787 + row->glyphs[area][i].padding_p
14788 + (row->glyphs[area][i].type << 2));
14789
14790 it->max_ascent = it->max_descent = 0;
14791 it->max_phys_ascent = it->max_phys_descent = 0;
14792 }
14793
14794
14795 /* Append one space to the glyph row of iterator IT if doing a
14796 window-based redisplay. The space has the same face as
14797 IT->face_id. Value is non-zero if a space was added.
14798
14799 This function is called to make sure that there is always one glyph
14800 at the end of a glyph row that the cursor can be set on under
14801 window-systems. (If there weren't such a glyph we would not know
14802 how wide and tall a box cursor should be displayed).
14803
14804 At the same time this space let's a nicely handle clearing to the
14805 end of the line if the row ends in italic text. */
14806
14807 static int
14808 append_space_for_newline (it, default_face_p)
14809 struct it *it;
14810 int default_face_p;
14811 {
14812 if (FRAME_WINDOW_P (it->f))
14813 {
14814 int n = it->glyph_row->used[TEXT_AREA];
14815
14816 if (it->glyph_row->glyphs[TEXT_AREA] + n
14817 < it->glyph_row->glyphs[1 + TEXT_AREA])
14818 {
14819 /* Save some values that must not be changed.
14820 Must save IT->c and IT->len because otherwise
14821 ITERATOR_AT_END_P wouldn't work anymore after
14822 append_space_for_newline has been called. */
14823 enum display_element_type saved_what = it->what;
14824 int saved_c = it->c, saved_len = it->len;
14825 int saved_x = it->current_x;
14826 int saved_face_id = it->face_id;
14827 struct text_pos saved_pos;
14828 Lisp_Object saved_object;
14829 struct face *face;
14830
14831 saved_object = it->object;
14832 saved_pos = it->position;
14833
14834 it->what = IT_CHARACTER;
14835 bzero (&it->position, sizeof it->position);
14836 it->object = make_number (0);
14837 it->c = ' ';
14838 it->len = 1;
14839
14840 if (default_face_p)
14841 it->face_id = DEFAULT_FACE_ID;
14842 else if (it->face_before_selective_p)
14843 it->face_id = it->saved_face_id;
14844 face = FACE_FROM_ID (it->f, it->face_id);
14845 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14846
14847 PRODUCE_GLYPHS (it);
14848
14849 it->override_ascent = -1;
14850 it->constrain_row_ascent_descent_p = 0;
14851 it->current_x = saved_x;
14852 it->object = saved_object;
14853 it->position = saved_pos;
14854 it->what = saved_what;
14855 it->face_id = saved_face_id;
14856 it->len = saved_len;
14857 it->c = saved_c;
14858 return 1;
14859 }
14860 }
14861
14862 return 0;
14863 }
14864
14865
14866 /* Extend the face of the last glyph in the text area of IT->glyph_row
14867 to the end of the display line. Called from display_line.
14868 If the glyph row is empty, add a space glyph to it so that we
14869 know the face to draw. Set the glyph row flag fill_line_p. */
14870
14871 static void
14872 extend_face_to_end_of_line (it)
14873 struct it *it;
14874 {
14875 struct face *face;
14876 struct frame *f = it->f;
14877
14878 /* If line is already filled, do nothing. */
14879 if (it->current_x >= it->last_visible_x)
14880 return;
14881
14882 /* Face extension extends the background and box of IT->face_id
14883 to the end of the line. If the background equals the background
14884 of the frame, we don't have to do anything. */
14885 if (it->face_before_selective_p)
14886 face = FACE_FROM_ID (it->f, it->saved_face_id);
14887 else
14888 face = FACE_FROM_ID (f, it->face_id);
14889
14890 if (FRAME_WINDOW_P (f)
14891 && face->box == FACE_NO_BOX
14892 && face->background == FRAME_BACKGROUND_PIXEL (f)
14893 && !face->stipple)
14894 return;
14895
14896 /* Set the glyph row flag indicating that the face of the last glyph
14897 in the text area has to be drawn to the end of the text area. */
14898 it->glyph_row->fill_line_p = 1;
14899
14900 /* If current character of IT is not ASCII, make sure we have the
14901 ASCII face. This will be automatically undone the next time
14902 get_next_display_element returns a multibyte character. Note
14903 that the character will always be single byte in unibyte text. */
14904 if (!SINGLE_BYTE_CHAR_P (it->c))
14905 {
14906 it->face_id = FACE_FOR_CHAR (f, face, 0);
14907 }
14908
14909 if (FRAME_WINDOW_P (f))
14910 {
14911 /* If the row is empty, add a space with the current face of IT,
14912 so that we know which face to draw. */
14913 if (it->glyph_row->used[TEXT_AREA] == 0)
14914 {
14915 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14916 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14917 it->glyph_row->used[TEXT_AREA] = 1;
14918 }
14919 }
14920 else
14921 {
14922 /* Save some values that must not be changed. */
14923 int saved_x = it->current_x;
14924 struct text_pos saved_pos;
14925 Lisp_Object saved_object;
14926 enum display_element_type saved_what = it->what;
14927 int saved_face_id = it->face_id;
14928
14929 saved_object = it->object;
14930 saved_pos = it->position;
14931
14932 it->what = IT_CHARACTER;
14933 bzero (&it->position, sizeof it->position);
14934 it->object = make_number (0);
14935 it->c = ' ';
14936 it->len = 1;
14937 it->face_id = face->id;
14938
14939 PRODUCE_GLYPHS (it);
14940
14941 while (it->current_x <= it->last_visible_x)
14942 PRODUCE_GLYPHS (it);
14943
14944 /* Don't count these blanks really. It would let us insert a left
14945 truncation glyph below and make us set the cursor on them, maybe. */
14946 it->current_x = saved_x;
14947 it->object = saved_object;
14948 it->position = saved_pos;
14949 it->what = saved_what;
14950 it->face_id = saved_face_id;
14951 }
14952 }
14953
14954
14955 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14956 trailing whitespace. */
14957
14958 static int
14959 trailing_whitespace_p (charpos)
14960 int charpos;
14961 {
14962 int bytepos = CHAR_TO_BYTE (charpos);
14963 int c = 0;
14964
14965 while (bytepos < ZV_BYTE
14966 && (c = FETCH_CHAR (bytepos),
14967 c == ' ' || c == '\t'))
14968 ++bytepos;
14969
14970 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14971 {
14972 if (bytepos != PT_BYTE)
14973 return 1;
14974 }
14975 return 0;
14976 }
14977
14978
14979 /* Highlight trailing whitespace, if any, in ROW. */
14980
14981 void
14982 highlight_trailing_whitespace (f, row)
14983 struct frame *f;
14984 struct glyph_row *row;
14985 {
14986 int used = row->used[TEXT_AREA];
14987
14988 if (used)
14989 {
14990 struct glyph *start = row->glyphs[TEXT_AREA];
14991 struct glyph *glyph = start + used - 1;
14992
14993 /* Skip over glyphs inserted to display the cursor at the
14994 end of a line, for extending the face of the last glyph
14995 to the end of the line on terminals, and for truncation
14996 and continuation glyphs. */
14997 while (glyph >= start
14998 && glyph->type == CHAR_GLYPH
14999 && INTEGERP (glyph->object))
15000 --glyph;
15001
15002 /* If last glyph is a space or stretch, and it's trailing
15003 whitespace, set the face of all trailing whitespace glyphs in
15004 IT->glyph_row to `trailing-whitespace'. */
15005 if (glyph >= start
15006 && BUFFERP (glyph->object)
15007 && (glyph->type == STRETCH_GLYPH
15008 || (glyph->type == CHAR_GLYPH
15009 && glyph->u.ch == ' '))
15010 && trailing_whitespace_p (glyph->charpos))
15011 {
15012 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15013 if (face_id < 0)
15014 return;
15015
15016 while (glyph >= start
15017 && BUFFERP (glyph->object)
15018 && (glyph->type == STRETCH_GLYPH
15019 || (glyph->type == CHAR_GLYPH
15020 && glyph->u.ch == ' ')))
15021 (glyph--)->face_id = face_id;
15022 }
15023 }
15024 }
15025
15026
15027 /* Value is non-zero if glyph row ROW in window W should be
15028 used to hold the cursor. */
15029
15030 static int
15031 cursor_row_p (w, row)
15032 struct window *w;
15033 struct glyph_row *row;
15034 {
15035 int cursor_row_p = 1;
15036
15037 if (PT == MATRIX_ROW_END_CHARPOS (row))
15038 {
15039 /* If the row ends with a newline from a string, we don't want
15040 the cursor there, but we still want it at the start of the
15041 string if the string starts in this row.
15042 If the row is continued it doesn't end in a newline. */
15043 if (CHARPOS (row->end.string_pos) >= 0)
15044 cursor_row_p = (row->continued_p
15045 || PT >= MATRIX_ROW_START_CHARPOS (row));
15046 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15047 {
15048 /* If the row ends in middle of a real character,
15049 and the line is continued, we want the cursor here.
15050 That's because MATRIX_ROW_END_CHARPOS would equal
15051 PT if PT is before the character. */
15052 if (!row->ends_in_ellipsis_p)
15053 cursor_row_p = row->continued_p;
15054 else
15055 /* If the row ends in an ellipsis, then
15056 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15057 We want that position to be displayed after the ellipsis. */
15058 cursor_row_p = 0;
15059 }
15060 /* If the row ends at ZV, display the cursor at the end of that
15061 row instead of at the start of the row below. */
15062 else if (row->ends_at_zv_p)
15063 cursor_row_p = 1;
15064 else
15065 cursor_row_p = 0;
15066 }
15067
15068 return cursor_row_p;
15069 }
15070
15071
15072 /* Construct the glyph row IT->glyph_row in the desired matrix of
15073 IT->w from text at the current position of IT. See dispextern.h
15074 for an overview of struct it. Value is non-zero if
15075 IT->glyph_row displays text, as opposed to a line displaying ZV
15076 only. */
15077
15078 static int
15079 display_line (it)
15080 struct it *it;
15081 {
15082 struct glyph_row *row = it->glyph_row;
15083 Lisp_Object overlay_arrow_string;
15084
15085 /* We always start displaying at hpos zero even if hscrolled. */
15086 xassert (it->hpos == 0 && it->current_x == 0);
15087
15088 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15089 >= it->w->desired_matrix->nrows)
15090 {
15091 it->w->nrows_scale_factor++;
15092 fonts_changed_p = 1;
15093 return 0;
15094 }
15095
15096 /* Is IT->w showing the region? */
15097 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15098
15099 /* Clear the result glyph row and enable it. */
15100 prepare_desired_row (row);
15101
15102 row->y = it->current_y;
15103 row->start = it->start;
15104 row->continuation_lines_width = it->continuation_lines_width;
15105 row->displays_text_p = 1;
15106 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15107 it->starts_in_middle_of_char_p = 0;
15108
15109 /* Arrange the overlays nicely for our purposes. Usually, we call
15110 display_line on only one line at a time, in which case this
15111 can't really hurt too much, or we call it on lines which appear
15112 one after another in the buffer, in which case all calls to
15113 recenter_overlay_lists but the first will be pretty cheap. */
15114 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15115
15116 /* Move over display elements that are not visible because we are
15117 hscrolled. This may stop at an x-position < IT->first_visible_x
15118 if the first glyph is partially visible or if we hit a line end. */
15119 if (it->current_x < it->first_visible_x)
15120 {
15121 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15122 MOVE_TO_POS | MOVE_TO_X);
15123 }
15124
15125 /* Get the initial row height. This is either the height of the
15126 text hscrolled, if there is any, or zero. */
15127 row->ascent = it->max_ascent;
15128 row->height = it->max_ascent + it->max_descent;
15129 row->phys_ascent = it->max_phys_ascent;
15130 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15131 row->extra_line_spacing = it->max_extra_line_spacing;
15132
15133 /* Loop generating characters. The loop is left with IT on the next
15134 character to display. */
15135 while (1)
15136 {
15137 int n_glyphs_before, hpos_before, x_before;
15138 int x, i, nglyphs;
15139 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15140
15141 /* Retrieve the next thing to display. Value is zero if end of
15142 buffer reached. */
15143 if (!get_next_display_element (it))
15144 {
15145 /* Maybe add a space at the end of this line that is used to
15146 display the cursor there under X. Set the charpos of the
15147 first glyph of blank lines not corresponding to any text
15148 to -1. */
15149 #ifdef HAVE_WINDOW_SYSTEM
15150 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15151 row->exact_window_width_line_p = 1;
15152 else
15153 #endif /* HAVE_WINDOW_SYSTEM */
15154 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15155 || row->used[TEXT_AREA] == 0)
15156 {
15157 row->glyphs[TEXT_AREA]->charpos = -1;
15158 row->displays_text_p = 0;
15159
15160 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15161 && (!MINI_WINDOW_P (it->w)
15162 || (minibuf_level && EQ (it->window, minibuf_window))))
15163 row->indicate_empty_line_p = 1;
15164 }
15165
15166 it->continuation_lines_width = 0;
15167 row->ends_at_zv_p = 1;
15168 break;
15169 }
15170
15171 /* Now, get the metrics of what we want to display. This also
15172 generates glyphs in `row' (which is IT->glyph_row). */
15173 n_glyphs_before = row->used[TEXT_AREA];
15174 x = it->current_x;
15175
15176 /* Remember the line height so far in case the next element doesn't
15177 fit on the line. */
15178 if (!it->truncate_lines_p)
15179 {
15180 ascent = it->max_ascent;
15181 descent = it->max_descent;
15182 phys_ascent = it->max_phys_ascent;
15183 phys_descent = it->max_phys_descent;
15184 }
15185
15186 PRODUCE_GLYPHS (it);
15187
15188 /* If this display element was in marginal areas, continue with
15189 the next one. */
15190 if (it->area != TEXT_AREA)
15191 {
15192 row->ascent = max (row->ascent, it->max_ascent);
15193 row->height = max (row->height, it->max_ascent + it->max_descent);
15194 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15195 row->phys_height = max (row->phys_height,
15196 it->max_phys_ascent + it->max_phys_descent);
15197 row->extra_line_spacing = max (row->extra_line_spacing,
15198 it->max_extra_line_spacing);
15199 set_iterator_to_next (it, 1);
15200 continue;
15201 }
15202
15203 /* Does the display element fit on the line? If we truncate
15204 lines, we should draw past the right edge of the window. If
15205 we don't truncate, we want to stop so that we can display the
15206 continuation glyph before the right margin. If lines are
15207 continued, there are two possible strategies for characters
15208 resulting in more than 1 glyph (e.g. tabs): Display as many
15209 glyphs as possible in this line and leave the rest for the
15210 continuation line, or display the whole element in the next
15211 line. Original redisplay did the former, so we do it also. */
15212 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15213 hpos_before = it->hpos;
15214 x_before = x;
15215
15216 if (/* Not a newline. */
15217 nglyphs > 0
15218 /* Glyphs produced fit entirely in the line. */
15219 && it->current_x < it->last_visible_x)
15220 {
15221 it->hpos += nglyphs;
15222 row->ascent = max (row->ascent, it->max_ascent);
15223 row->height = max (row->height, it->max_ascent + it->max_descent);
15224 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15225 row->phys_height = max (row->phys_height,
15226 it->max_phys_ascent + it->max_phys_descent);
15227 row->extra_line_spacing = max (row->extra_line_spacing,
15228 it->max_extra_line_spacing);
15229 if (it->current_x - it->pixel_width < it->first_visible_x)
15230 row->x = x - it->first_visible_x;
15231 }
15232 else
15233 {
15234 int new_x;
15235 struct glyph *glyph;
15236
15237 for (i = 0; i < nglyphs; ++i, x = new_x)
15238 {
15239 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15240 new_x = x + glyph->pixel_width;
15241
15242 if (/* Lines are continued. */
15243 !it->truncate_lines_p
15244 && (/* Glyph doesn't fit on the line. */
15245 new_x > it->last_visible_x
15246 /* Or it fits exactly on a window system frame. */
15247 || (new_x == it->last_visible_x
15248 && FRAME_WINDOW_P (it->f))))
15249 {
15250 /* End of a continued line. */
15251
15252 if (it->hpos == 0
15253 || (new_x == it->last_visible_x
15254 && FRAME_WINDOW_P (it->f)))
15255 {
15256 /* Current glyph is the only one on the line or
15257 fits exactly on the line. We must continue
15258 the line because we can't draw the cursor
15259 after the glyph. */
15260 row->continued_p = 1;
15261 it->current_x = new_x;
15262 it->continuation_lines_width += new_x;
15263 ++it->hpos;
15264 if (i == nglyphs - 1)
15265 {
15266 set_iterator_to_next (it, 1);
15267 #ifdef HAVE_WINDOW_SYSTEM
15268 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15269 {
15270 if (!get_next_display_element (it))
15271 {
15272 row->exact_window_width_line_p = 1;
15273 it->continuation_lines_width = 0;
15274 row->continued_p = 0;
15275 row->ends_at_zv_p = 1;
15276 }
15277 else if (ITERATOR_AT_END_OF_LINE_P (it))
15278 {
15279 row->continued_p = 0;
15280 row->exact_window_width_line_p = 1;
15281 }
15282 }
15283 #endif /* HAVE_WINDOW_SYSTEM */
15284 }
15285 }
15286 else if (CHAR_GLYPH_PADDING_P (*glyph)
15287 && !FRAME_WINDOW_P (it->f))
15288 {
15289 /* A padding glyph that doesn't fit on this line.
15290 This means the whole character doesn't fit
15291 on the line. */
15292 row->used[TEXT_AREA] = n_glyphs_before;
15293
15294 /* Fill the rest of the row with continuation
15295 glyphs like in 20.x. */
15296 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15297 < row->glyphs[1 + TEXT_AREA])
15298 produce_special_glyphs (it, IT_CONTINUATION);
15299
15300 row->continued_p = 1;
15301 it->current_x = x_before;
15302 it->continuation_lines_width += x_before;
15303
15304 /* Restore the height to what it was before the
15305 element not fitting on the line. */
15306 it->max_ascent = ascent;
15307 it->max_descent = descent;
15308 it->max_phys_ascent = phys_ascent;
15309 it->max_phys_descent = phys_descent;
15310 }
15311 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15312 {
15313 /* A TAB that extends past the right edge of the
15314 window. This produces a single glyph on
15315 window system frames. We leave the glyph in
15316 this row and let it fill the row, but don't
15317 consume the TAB. */
15318 it->continuation_lines_width += it->last_visible_x;
15319 row->ends_in_middle_of_char_p = 1;
15320 row->continued_p = 1;
15321 glyph->pixel_width = it->last_visible_x - x;
15322 it->starts_in_middle_of_char_p = 1;
15323 }
15324 else
15325 {
15326 /* Something other than a TAB that draws past
15327 the right edge of the window. Restore
15328 positions to values before the element. */
15329 row->used[TEXT_AREA] = n_glyphs_before + i;
15330
15331 /* Display continuation glyphs. */
15332 if (!FRAME_WINDOW_P (it->f))
15333 produce_special_glyphs (it, IT_CONTINUATION);
15334 row->continued_p = 1;
15335
15336 it->continuation_lines_width += x;
15337
15338 if (nglyphs > 1 && i > 0)
15339 {
15340 row->ends_in_middle_of_char_p = 1;
15341 it->starts_in_middle_of_char_p = 1;
15342 }
15343
15344 /* Restore the height to what it was before the
15345 element not fitting on the line. */
15346 it->max_ascent = ascent;
15347 it->max_descent = descent;
15348 it->max_phys_ascent = phys_ascent;
15349 it->max_phys_descent = phys_descent;
15350 }
15351
15352 break;
15353 }
15354 else if (new_x > it->first_visible_x)
15355 {
15356 /* Increment number of glyphs actually displayed. */
15357 ++it->hpos;
15358
15359 if (x < it->first_visible_x)
15360 /* Glyph is partially visible, i.e. row starts at
15361 negative X position. */
15362 row->x = x - it->first_visible_x;
15363 }
15364 else
15365 {
15366 /* Glyph is completely off the left margin of the
15367 window. This should not happen because of the
15368 move_it_in_display_line at the start of this
15369 function, unless the text display area of the
15370 window is empty. */
15371 xassert (it->first_visible_x <= it->last_visible_x);
15372 }
15373 }
15374
15375 row->ascent = max (row->ascent, it->max_ascent);
15376 row->height = max (row->height, it->max_ascent + it->max_descent);
15377 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15378 row->phys_height = max (row->phys_height,
15379 it->max_phys_ascent + it->max_phys_descent);
15380 row->extra_line_spacing = max (row->extra_line_spacing,
15381 it->max_extra_line_spacing);
15382
15383 /* End of this display line if row is continued. */
15384 if (row->continued_p || row->ends_at_zv_p)
15385 break;
15386 }
15387
15388 at_end_of_line:
15389 /* Is this a line end? If yes, we're also done, after making
15390 sure that a non-default face is extended up to the right
15391 margin of the window. */
15392 if (ITERATOR_AT_END_OF_LINE_P (it))
15393 {
15394 int used_before = row->used[TEXT_AREA];
15395
15396 row->ends_in_newline_from_string_p = STRINGP (it->object);
15397
15398 #ifdef HAVE_WINDOW_SYSTEM
15399 /* Add a space at the end of the line that is used to
15400 display the cursor there. */
15401 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15402 append_space_for_newline (it, 0);
15403 #endif /* HAVE_WINDOW_SYSTEM */
15404
15405 /* Extend the face to the end of the line. */
15406 extend_face_to_end_of_line (it);
15407
15408 /* Make sure we have the position. */
15409 if (used_before == 0)
15410 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15411
15412 /* Consume the line end. This skips over invisible lines. */
15413 set_iterator_to_next (it, 1);
15414 it->continuation_lines_width = 0;
15415 break;
15416 }
15417
15418 /* Proceed with next display element. Note that this skips
15419 over lines invisible because of selective display. */
15420 set_iterator_to_next (it, 1);
15421
15422 /* If we truncate lines, we are done when the last displayed
15423 glyphs reach past the right margin of the window. */
15424 if (it->truncate_lines_p
15425 && (FRAME_WINDOW_P (it->f)
15426 ? (it->current_x >= it->last_visible_x)
15427 : (it->current_x > it->last_visible_x)))
15428 {
15429 /* Maybe add truncation glyphs. */
15430 if (!FRAME_WINDOW_P (it->f))
15431 {
15432 int i, n;
15433
15434 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15435 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15436 break;
15437
15438 for (n = row->used[TEXT_AREA]; i < n; ++i)
15439 {
15440 row->used[TEXT_AREA] = i;
15441 produce_special_glyphs (it, IT_TRUNCATION);
15442 }
15443 }
15444 #ifdef HAVE_WINDOW_SYSTEM
15445 else
15446 {
15447 /* Don't truncate if we can overflow newline into fringe. */
15448 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15449 {
15450 if (!get_next_display_element (it))
15451 {
15452 it->continuation_lines_width = 0;
15453 row->ends_at_zv_p = 1;
15454 row->exact_window_width_line_p = 1;
15455 break;
15456 }
15457 if (ITERATOR_AT_END_OF_LINE_P (it))
15458 {
15459 row->exact_window_width_line_p = 1;
15460 goto at_end_of_line;
15461 }
15462 }
15463 }
15464 #endif /* HAVE_WINDOW_SYSTEM */
15465
15466 row->truncated_on_right_p = 1;
15467 it->continuation_lines_width = 0;
15468 reseat_at_next_visible_line_start (it, 0);
15469 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15470 it->hpos = hpos_before;
15471 it->current_x = x_before;
15472 break;
15473 }
15474 }
15475
15476 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15477 at the left window margin. */
15478 if (it->first_visible_x
15479 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15480 {
15481 if (!FRAME_WINDOW_P (it->f))
15482 insert_left_trunc_glyphs (it);
15483 row->truncated_on_left_p = 1;
15484 }
15485
15486 /* If the start of this line is the overlay arrow-position, then
15487 mark this glyph row as the one containing the overlay arrow.
15488 This is clearly a mess with variable size fonts. It would be
15489 better to let it be displayed like cursors under X. */
15490 if ((row->displays_text_p || !overlay_arrow_seen)
15491 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15492 !NILP (overlay_arrow_string)))
15493 {
15494 /* Overlay arrow in window redisplay is a fringe bitmap. */
15495 if (STRINGP (overlay_arrow_string))
15496 {
15497 struct glyph_row *arrow_row
15498 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15499 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15500 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15501 struct glyph *p = row->glyphs[TEXT_AREA];
15502 struct glyph *p2, *end;
15503
15504 /* Copy the arrow glyphs. */
15505 while (glyph < arrow_end)
15506 *p++ = *glyph++;
15507
15508 /* Throw away padding glyphs. */
15509 p2 = p;
15510 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15511 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15512 ++p2;
15513 if (p2 > p)
15514 {
15515 while (p2 < end)
15516 *p++ = *p2++;
15517 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15518 }
15519 }
15520 else
15521 {
15522 xassert (INTEGERP (overlay_arrow_string));
15523 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15524 }
15525 overlay_arrow_seen = 1;
15526 }
15527
15528 /* Compute pixel dimensions of this line. */
15529 compute_line_metrics (it);
15530
15531 /* Remember the position at which this line ends. */
15532 row->end = it->current;
15533
15534 /* Record whether this row ends inside an ellipsis. */
15535 row->ends_in_ellipsis_p
15536 = (it->method == GET_FROM_DISPLAY_VECTOR
15537 && it->ellipsis_p);
15538
15539 /* Save fringe bitmaps in this row. */
15540 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15541 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15542 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15543 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15544
15545 it->left_user_fringe_bitmap = 0;
15546 it->left_user_fringe_face_id = 0;
15547 it->right_user_fringe_bitmap = 0;
15548 it->right_user_fringe_face_id = 0;
15549
15550 /* Maybe set the cursor. */
15551 if (it->w->cursor.vpos < 0
15552 && PT >= MATRIX_ROW_START_CHARPOS (row)
15553 && PT <= MATRIX_ROW_END_CHARPOS (row)
15554 && cursor_row_p (it->w, row))
15555 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15556
15557 /* Highlight trailing whitespace. */
15558 if (!NILP (Vshow_trailing_whitespace))
15559 highlight_trailing_whitespace (it->f, it->glyph_row);
15560
15561 /* Prepare for the next line. This line starts horizontally at (X
15562 HPOS) = (0 0). Vertical positions are incremented. As a
15563 convenience for the caller, IT->glyph_row is set to the next
15564 row to be used. */
15565 it->current_x = it->hpos = 0;
15566 it->current_y += row->height;
15567 ++it->vpos;
15568 ++it->glyph_row;
15569 it->start = it->current;
15570 return row->displays_text_p;
15571 }
15572
15573
15574 \f
15575 /***********************************************************************
15576 Menu Bar
15577 ***********************************************************************/
15578
15579 /* Redisplay the menu bar in the frame for window W.
15580
15581 The menu bar of X frames that don't have X toolkit support is
15582 displayed in a special window W->frame->menu_bar_window.
15583
15584 The menu bar of terminal frames is treated specially as far as
15585 glyph matrices are concerned. Menu bar lines are not part of
15586 windows, so the update is done directly on the frame matrix rows
15587 for the menu bar. */
15588
15589 static void
15590 display_menu_bar (w)
15591 struct window *w;
15592 {
15593 struct frame *f = XFRAME (WINDOW_FRAME (w));
15594 struct it it;
15595 Lisp_Object items;
15596 int i;
15597
15598 /* Don't do all this for graphical frames. */
15599 #ifdef HAVE_NTGUI
15600 if (!NILP (Vwindow_system))
15601 return;
15602 #endif
15603 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15604 if (FRAME_X_P (f))
15605 return;
15606 #endif
15607 #ifdef MAC_OS
15608 if (FRAME_MAC_P (f))
15609 return;
15610 #endif
15611
15612 #ifdef USE_X_TOOLKIT
15613 xassert (!FRAME_WINDOW_P (f));
15614 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15615 it.first_visible_x = 0;
15616 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15617 #else /* not USE_X_TOOLKIT */
15618 if (FRAME_WINDOW_P (f))
15619 {
15620 /* Menu bar lines are displayed in the desired matrix of the
15621 dummy window menu_bar_window. */
15622 struct window *menu_w;
15623 xassert (WINDOWP (f->menu_bar_window));
15624 menu_w = XWINDOW (f->menu_bar_window);
15625 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15626 MENU_FACE_ID);
15627 it.first_visible_x = 0;
15628 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15629 }
15630 else
15631 {
15632 /* This is a TTY frame, i.e. character hpos/vpos are used as
15633 pixel x/y. */
15634 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15635 MENU_FACE_ID);
15636 it.first_visible_x = 0;
15637 it.last_visible_x = FRAME_COLS (f);
15638 }
15639 #endif /* not USE_X_TOOLKIT */
15640
15641 if (! mode_line_inverse_video)
15642 /* Force the menu-bar to be displayed in the default face. */
15643 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15644
15645 /* Clear all rows of the menu bar. */
15646 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15647 {
15648 struct glyph_row *row = it.glyph_row + i;
15649 clear_glyph_row (row);
15650 row->enabled_p = 1;
15651 row->full_width_p = 1;
15652 }
15653
15654 /* Display all items of the menu bar. */
15655 items = FRAME_MENU_BAR_ITEMS (it.f);
15656 for (i = 0; i < XVECTOR (items)->size; i += 4)
15657 {
15658 Lisp_Object string;
15659
15660 /* Stop at nil string. */
15661 string = AREF (items, i + 1);
15662 if (NILP (string))
15663 break;
15664
15665 /* Remember where item was displayed. */
15666 AREF (items, i + 3) = make_number (it.hpos);
15667
15668 /* Display the item, pad with one space. */
15669 if (it.current_x < it.last_visible_x)
15670 display_string (NULL, string, Qnil, 0, 0, &it,
15671 SCHARS (string) + 1, 0, 0, -1);
15672 }
15673
15674 /* Fill out the line with spaces. */
15675 if (it.current_x < it.last_visible_x)
15676 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15677
15678 /* Compute the total height of the lines. */
15679 compute_line_metrics (&it);
15680 }
15681
15682
15683 \f
15684 /***********************************************************************
15685 Mode Line
15686 ***********************************************************************/
15687
15688 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15689 FORCE is non-zero, redisplay mode lines unconditionally.
15690 Otherwise, redisplay only mode lines that are garbaged. Value is
15691 the number of windows whose mode lines were redisplayed. */
15692
15693 static int
15694 redisplay_mode_lines (window, force)
15695 Lisp_Object window;
15696 int force;
15697 {
15698 int nwindows = 0;
15699
15700 while (!NILP (window))
15701 {
15702 struct window *w = XWINDOW (window);
15703
15704 if (WINDOWP (w->hchild))
15705 nwindows += redisplay_mode_lines (w->hchild, force);
15706 else if (WINDOWP (w->vchild))
15707 nwindows += redisplay_mode_lines (w->vchild, force);
15708 else if (force
15709 || FRAME_GARBAGED_P (XFRAME (w->frame))
15710 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15711 {
15712 struct text_pos lpoint;
15713 struct buffer *old = current_buffer;
15714
15715 /* Set the window's buffer for the mode line display. */
15716 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15717 set_buffer_internal_1 (XBUFFER (w->buffer));
15718
15719 /* Point refers normally to the selected window. For any
15720 other window, set up appropriate value. */
15721 if (!EQ (window, selected_window))
15722 {
15723 struct text_pos pt;
15724
15725 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15726 if (CHARPOS (pt) < BEGV)
15727 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15728 else if (CHARPOS (pt) > (ZV - 1))
15729 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15730 else
15731 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15732 }
15733
15734 /* Display mode lines. */
15735 clear_glyph_matrix (w->desired_matrix);
15736 if (display_mode_lines (w))
15737 {
15738 ++nwindows;
15739 w->must_be_updated_p = 1;
15740 }
15741
15742 /* Restore old settings. */
15743 set_buffer_internal_1 (old);
15744 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15745 }
15746
15747 window = w->next;
15748 }
15749
15750 return nwindows;
15751 }
15752
15753
15754 /* Display the mode and/or top line of window W. Value is the number
15755 of mode lines displayed. */
15756
15757 static int
15758 display_mode_lines (w)
15759 struct window *w;
15760 {
15761 Lisp_Object old_selected_window, old_selected_frame;
15762 int n = 0;
15763
15764 old_selected_frame = selected_frame;
15765 selected_frame = w->frame;
15766 old_selected_window = selected_window;
15767 XSETWINDOW (selected_window, w);
15768
15769 /* These will be set while the mode line specs are processed. */
15770 line_number_displayed = 0;
15771 w->column_number_displayed = Qnil;
15772
15773 if (WINDOW_WANTS_MODELINE_P (w))
15774 {
15775 struct window *sel_w = XWINDOW (old_selected_window);
15776
15777 /* Select mode line face based on the real selected window. */
15778 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15779 current_buffer->mode_line_format);
15780 ++n;
15781 }
15782
15783 if (WINDOW_WANTS_HEADER_LINE_P (w))
15784 {
15785 display_mode_line (w, HEADER_LINE_FACE_ID,
15786 current_buffer->header_line_format);
15787 ++n;
15788 }
15789
15790 selected_frame = old_selected_frame;
15791 selected_window = old_selected_window;
15792 return n;
15793 }
15794
15795
15796 /* Display mode or top line of window W. FACE_ID specifies which line
15797 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15798 FORMAT is the mode line format to display. Value is the pixel
15799 height of the mode line displayed. */
15800
15801 static int
15802 display_mode_line (w, face_id, format)
15803 struct window *w;
15804 enum face_id face_id;
15805 Lisp_Object format;
15806 {
15807 struct it it;
15808 struct face *face;
15809 int count = SPECPDL_INDEX ();
15810
15811 init_iterator (&it, w, -1, -1, NULL, face_id);
15812 prepare_desired_row (it.glyph_row);
15813
15814 it.glyph_row->mode_line_p = 1;
15815
15816 if (! mode_line_inverse_video)
15817 /* Force the mode-line to be displayed in the default face. */
15818 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15819
15820 record_unwind_protect (unwind_format_mode_line,
15821 format_mode_line_unwind_data (NULL));
15822
15823 mode_line_target = MODE_LINE_DISPLAY;
15824
15825 /* Temporarily make frame's keyboard the current kboard so that
15826 kboard-local variables in the mode_line_format will get the right
15827 values. */
15828 push_frame_kboard (it.f);
15829 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15830 pop_frame_kboard ();
15831
15832 unbind_to (count, Qnil);
15833
15834 /* Fill up with spaces. */
15835 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15836
15837 compute_line_metrics (&it);
15838 it.glyph_row->full_width_p = 1;
15839 it.glyph_row->continued_p = 0;
15840 it.glyph_row->truncated_on_left_p = 0;
15841 it.glyph_row->truncated_on_right_p = 0;
15842
15843 /* Make a 3D mode-line have a shadow at its right end. */
15844 face = FACE_FROM_ID (it.f, face_id);
15845 extend_face_to_end_of_line (&it);
15846 if (face->box != FACE_NO_BOX)
15847 {
15848 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15849 + it.glyph_row->used[TEXT_AREA] - 1);
15850 last->right_box_line_p = 1;
15851 }
15852
15853 return it.glyph_row->height;
15854 }
15855
15856 /* Contribute ELT to the mode line for window IT->w. How it
15857 translates into text depends on its data type.
15858
15859 IT describes the display environment in which we display, as usual.
15860
15861 DEPTH is the depth in recursion. It is used to prevent
15862 infinite recursion here.
15863
15864 FIELD_WIDTH is the number of characters the display of ELT should
15865 occupy in the mode line, and PRECISION is the maximum number of
15866 characters to display from ELT's representation. See
15867 display_string for details.
15868
15869 Returns the hpos of the end of the text generated by ELT.
15870
15871 PROPS is a property list to add to any string we encounter.
15872
15873 If RISKY is nonzero, remove (disregard) any properties in any string
15874 we encounter, and ignore :eval and :propertize.
15875
15876 The global variable `mode_line_target' determines whether the
15877 output is passed to `store_mode_line_noprop',
15878 `store_mode_line_string', or `display_string'. */
15879
15880 static int
15881 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15882 struct it *it;
15883 int depth;
15884 int field_width, precision;
15885 Lisp_Object elt, props;
15886 int risky;
15887 {
15888 int n = 0, field, prec;
15889 int literal = 0;
15890
15891 tail_recurse:
15892 if (depth > 100)
15893 elt = build_string ("*too-deep*");
15894
15895 depth++;
15896
15897 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15898 {
15899 case Lisp_String:
15900 {
15901 /* A string: output it and check for %-constructs within it. */
15902 unsigned char c;
15903 const unsigned char *this, *lisp_string;
15904
15905 if (!NILP (props) || risky)
15906 {
15907 Lisp_Object oprops, aelt;
15908 oprops = Ftext_properties_at (make_number (0), elt);
15909
15910 /* If the starting string's properties are not what
15911 we want, translate the string. Also, if the string
15912 is risky, do that anyway. */
15913
15914 if (NILP (Fequal (props, oprops)) || risky)
15915 {
15916 /* If the starting string has properties,
15917 merge the specified ones onto the existing ones. */
15918 if (! NILP (oprops) && !risky)
15919 {
15920 Lisp_Object tem;
15921
15922 oprops = Fcopy_sequence (oprops);
15923 tem = props;
15924 while (CONSP (tem))
15925 {
15926 oprops = Fplist_put (oprops, XCAR (tem),
15927 XCAR (XCDR (tem)));
15928 tem = XCDR (XCDR (tem));
15929 }
15930 props = oprops;
15931 }
15932
15933 aelt = Fassoc (elt, mode_line_proptrans_alist);
15934 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15935 {
15936 mode_line_proptrans_alist
15937 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15938 elt = XCAR (aelt);
15939 }
15940 else
15941 {
15942 Lisp_Object tem;
15943
15944 elt = Fcopy_sequence (elt);
15945 Fset_text_properties (make_number (0), Flength (elt),
15946 props, elt);
15947 /* Add this item to mode_line_proptrans_alist. */
15948 mode_line_proptrans_alist
15949 = Fcons (Fcons (elt, props),
15950 mode_line_proptrans_alist);
15951 /* Truncate mode_line_proptrans_alist
15952 to at most 50 elements. */
15953 tem = Fnthcdr (make_number (50),
15954 mode_line_proptrans_alist);
15955 if (! NILP (tem))
15956 XSETCDR (tem, Qnil);
15957 }
15958 }
15959 }
15960
15961 this = SDATA (elt);
15962 lisp_string = this;
15963
15964 if (literal)
15965 {
15966 prec = precision - n;
15967 switch (mode_line_target)
15968 {
15969 case MODE_LINE_NOPROP:
15970 case MODE_LINE_TITLE:
15971 n += store_mode_line_noprop (SDATA (elt), -1, prec);
15972 break;
15973 case MODE_LINE_STRING:
15974 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15975 break;
15976 case MODE_LINE_DISPLAY:
15977 n += display_string (NULL, elt, Qnil, 0, 0, it,
15978 0, prec, 0, STRING_MULTIBYTE (elt));
15979 break;
15980 }
15981
15982 break;
15983 }
15984
15985 while ((precision <= 0 || n < precision)
15986 && *this
15987 && (mode_line_target != MODE_LINE_DISPLAY
15988 || it->current_x < it->last_visible_x))
15989 {
15990 const unsigned char *last = this;
15991
15992 /* Advance to end of string or next format specifier. */
15993 while ((c = *this++) != '\0' && c != '%')
15994 ;
15995
15996 if (this - 1 != last)
15997 {
15998 int nchars, nbytes;
15999
16000 /* Output to end of string or up to '%'. Field width
16001 is length of string. Don't output more than
16002 PRECISION allows us. */
16003 --this;
16004
16005 prec = c_string_width (last, this - last, precision - n,
16006 &nchars, &nbytes);
16007
16008 switch (mode_line_target)
16009 {
16010 case MODE_LINE_NOPROP:
16011 case MODE_LINE_TITLE:
16012 n += store_mode_line_noprop (last, 0, prec);
16013 break;
16014 case MODE_LINE_STRING:
16015 {
16016 int bytepos = last - lisp_string;
16017 int charpos = string_byte_to_char (elt, bytepos);
16018 int endpos = (precision <= 0
16019 ? string_byte_to_char (elt,
16020 this - lisp_string)
16021 : charpos + nchars);
16022
16023 n += store_mode_line_string (NULL,
16024 Fsubstring (elt, make_number (charpos),
16025 make_number (endpos)),
16026 0, 0, 0, Qnil);
16027 }
16028 break;
16029 case MODE_LINE_DISPLAY:
16030 {
16031 int bytepos = last - lisp_string;
16032 int charpos = string_byte_to_char (elt, bytepos);
16033 n += display_string (NULL, elt, Qnil, 0, charpos,
16034 it, 0, prec, 0,
16035 STRING_MULTIBYTE (elt));
16036 }
16037 break;
16038 }
16039 }
16040 else /* c == '%' */
16041 {
16042 const unsigned char *percent_position = this;
16043
16044 /* Get the specified minimum width. Zero means
16045 don't pad. */
16046 field = 0;
16047 while ((c = *this++) >= '0' && c <= '9')
16048 field = field * 10 + c - '0';
16049
16050 /* Don't pad beyond the total padding allowed. */
16051 if (field_width - n > 0 && field > field_width - n)
16052 field = field_width - n;
16053
16054 /* Note that either PRECISION <= 0 or N < PRECISION. */
16055 prec = precision - n;
16056
16057 if (c == 'M')
16058 n += display_mode_element (it, depth, field, prec,
16059 Vglobal_mode_string, props,
16060 risky);
16061 else if (c != 0)
16062 {
16063 int multibyte;
16064 int bytepos, charpos;
16065 unsigned char *spec;
16066
16067 bytepos = percent_position - lisp_string;
16068 charpos = (STRING_MULTIBYTE (elt)
16069 ? string_byte_to_char (elt, bytepos)
16070 : bytepos);
16071
16072 spec
16073 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16074
16075 switch (mode_line_target)
16076 {
16077 case MODE_LINE_NOPROP:
16078 case MODE_LINE_TITLE:
16079 n += store_mode_line_noprop (spec, field, prec);
16080 break;
16081 case MODE_LINE_STRING:
16082 {
16083 int len = strlen (spec);
16084 Lisp_Object tem = make_string (spec, len);
16085 props = Ftext_properties_at (make_number (charpos), elt);
16086 /* Should only keep face property in props */
16087 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16088 }
16089 break;
16090 case MODE_LINE_DISPLAY:
16091 {
16092 int nglyphs_before, nwritten;
16093
16094 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16095 nwritten = display_string (spec, Qnil, elt,
16096 charpos, 0, it,
16097 field, prec, 0,
16098 multibyte);
16099
16100 /* Assign to the glyphs written above the
16101 string where the `%x' came from, position
16102 of the `%'. */
16103 if (nwritten > 0)
16104 {
16105 struct glyph *glyph
16106 = (it->glyph_row->glyphs[TEXT_AREA]
16107 + nglyphs_before);
16108 int i;
16109
16110 for (i = 0; i < nwritten; ++i)
16111 {
16112 glyph[i].object = elt;
16113 glyph[i].charpos = charpos;
16114 }
16115
16116 n += nwritten;
16117 }
16118 }
16119 break;
16120 }
16121 }
16122 else /* c == 0 */
16123 break;
16124 }
16125 }
16126 }
16127 break;
16128
16129 case Lisp_Symbol:
16130 /* A symbol: process the value of the symbol recursively
16131 as if it appeared here directly. Avoid error if symbol void.
16132 Special case: if value of symbol is a string, output the string
16133 literally. */
16134 {
16135 register Lisp_Object tem;
16136
16137 /* If the variable is not marked as risky to set
16138 then its contents are risky to use. */
16139 if (NILP (Fget (elt, Qrisky_local_variable)))
16140 risky = 1;
16141
16142 tem = Fboundp (elt);
16143 if (!NILP (tem))
16144 {
16145 tem = Fsymbol_value (elt);
16146 /* If value is a string, output that string literally:
16147 don't check for % within it. */
16148 if (STRINGP (tem))
16149 literal = 1;
16150
16151 if (!EQ (tem, elt))
16152 {
16153 /* Give up right away for nil or t. */
16154 elt = tem;
16155 goto tail_recurse;
16156 }
16157 }
16158 }
16159 break;
16160
16161 case Lisp_Cons:
16162 {
16163 register Lisp_Object car, tem;
16164
16165 /* A cons cell: five distinct cases.
16166 If first element is :eval or :propertize, do something special.
16167 If first element is a string or a cons, process all the elements
16168 and effectively concatenate them.
16169 If first element is a negative number, truncate displaying cdr to
16170 at most that many characters. If positive, pad (with spaces)
16171 to at least that many characters.
16172 If first element is a symbol, process the cadr or caddr recursively
16173 according to whether the symbol's value is non-nil or nil. */
16174 car = XCAR (elt);
16175 if (EQ (car, QCeval))
16176 {
16177 /* An element of the form (:eval FORM) means evaluate FORM
16178 and use the result as mode line elements. */
16179
16180 if (risky)
16181 break;
16182
16183 if (CONSP (XCDR (elt)))
16184 {
16185 Lisp_Object spec;
16186 spec = safe_eval (XCAR (XCDR (elt)));
16187 n += display_mode_element (it, depth, field_width - n,
16188 precision - n, spec, props,
16189 risky);
16190 }
16191 }
16192 else if (EQ (car, QCpropertize))
16193 {
16194 /* An element of the form (:propertize ELT PROPS...)
16195 means display ELT but applying properties PROPS. */
16196
16197 if (risky)
16198 break;
16199
16200 if (CONSP (XCDR (elt)))
16201 n += display_mode_element (it, depth, field_width - n,
16202 precision - n, XCAR (XCDR (elt)),
16203 XCDR (XCDR (elt)), risky);
16204 }
16205 else if (SYMBOLP (car))
16206 {
16207 tem = Fboundp (car);
16208 elt = XCDR (elt);
16209 if (!CONSP (elt))
16210 goto invalid;
16211 /* elt is now the cdr, and we know it is a cons cell.
16212 Use its car if CAR has a non-nil value. */
16213 if (!NILP (tem))
16214 {
16215 tem = Fsymbol_value (car);
16216 if (!NILP (tem))
16217 {
16218 elt = XCAR (elt);
16219 goto tail_recurse;
16220 }
16221 }
16222 /* Symbol's value is nil (or symbol is unbound)
16223 Get the cddr of the original list
16224 and if possible find the caddr and use that. */
16225 elt = XCDR (elt);
16226 if (NILP (elt))
16227 break;
16228 else if (!CONSP (elt))
16229 goto invalid;
16230 elt = XCAR (elt);
16231 goto tail_recurse;
16232 }
16233 else if (INTEGERP (car))
16234 {
16235 register int lim = XINT (car);
16236 elt = XCDR (elt);
16237 if (lim < 0)
16238 {
16239 /* Negative int means reduce maximum width. */
16240 if (precision <= 0)
16241 precision = -lim;
16242 else
16243 precision = min (precision, -lim);
16244 }
16245 else if (lim > 0)
16246 {
16247 /* Padding specified. Don't let it be more than
16248 current maximum. */
16249 if (precision > 0)
16250 lim = min (precision, lim);
16251
16252 /* If that's more padding than already wanted, queue it.
16253 But don't reduce padding already specified even if
16254 that is beyond the current truncation point. */
16255 field_width = max (lim, field_width);
16256 }
16257 goto tail_recurse;
16258 }
16259 else if (STRINGP (car) || CONSP (car))
16260 {
16261 register int limit = 50;
16262 /* Limit is to protect against circular lists. */
16263 while (CONSP (elt)
16264 && --limit > 0
16265 && (precision <= 0 || n < precision))
16266 {
16267 n += display_mode_element (it, depth,
16268 /* Do padding only after the last
16269 element in the list. */
16270 (! CONSP (XCDR (elt))
16271 ? field_width - n
16272 : 0),
16273 precision - n, XCAR (elt),
16274 props, risky);
16275 elt = XCDR (elt);
16276 }
16277 }
16278 }
16279 break;
16280
16281 default:
16282 invalid:
16283 elt = build_string ("*invalid*");
16284 goto tail_recurse;
16285 }
16286
16287 /* Pad to FIELD_WIDTH. */
16288 if (field_width > 0 && n < field_width)
16289 {
16290 switch (mode_line_target)
16291 {
16292 case MODE_LINE_NOPROP:
16293 case MODE_LINE_TITLE:
16294 n += store_mode_line_noprop ("", field_width - n, 0);
16295 break;
16296 case MODE_LINE_STRING:
16297 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16298 break;
16299 case MODE_LINE_DISPLAY:
16300 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16301 0, 0, 0);
16302 break;
16303 }
16304 }
16305
16306 return n;
16307 }
16308
16309 /* Store a mode-line string element in mode_line_string_list.
16310
16311 If STRING is non-null, display that C string. Otherwise, the Lisp
16312 string LISP_STRING is displayed.
16313
16314 FIELD_WIDTH is the minimum number of output glyphs to produce.
16315 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16316 with spaces. FIELD_WIDTH <= 0 means don't pad.
16317
16318 PRECISION is the maximum number of characters to output from
16319 STRING. PRECISION <= 0 means don't truncate the string.
16320
16321 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16322 properties to the string.
16323
16324 PROPS are the properties to add to the string.
16325 The mode_line_string_face face property is always added to the string.
16326 */
16327
16328 static int
16329 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16330 char *string;
16331 Lisp_Object lisp_string;
16332 int copy_string;
16333 int field_width;
16334 int precision;
16335 Lisp_Object props;
16336 {
16337 int len;
16338 int n = 0;
16339
16340 if (string != NULL)
16341 {
16342 len = strlen (string);
16343 if (precision > 0 && len > precision)
16344 len = precision;
16345 lisp_string = make_string (string, len);
16346 if (NILP (props))
16347 props = mode_line_string_face_prop;
16348 else if (!NILP (mode_line_string_face))
16349 {
16350 Lisp_Object face = Fplist_get (props, Qface);
16351 props = Fcopy_sequence (props);
16352 if (NILP (face))
16353 face = mode_line_string_face;
16354 else
16355 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16356 props = Fplist_put (props, Qface, face);
16357 }
16358 Fadd_text_properties (make_number (0), make_number (len),
16359 props, lisp_string);
16360 }
16361 else
16362 {
16363 len = XFASTINT (Flength (lisp_string));
16364 if (precision > 0 && len > precision)
16365 {
16366 len = precision;
16367 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16368 precision = -1;
16369 }
16370 if (!NILP (mode_line_string_face))
16371 {
16372 Lisp_Object face;
16373 if (NILP (props))
16374 props = Ftext_properties_at (make_number (0), lisp_string);
16375 face = Fplist_get (props, Qface);
16376 if (NILP (face))
16377 face = mode_line_string_face;
16378 else
16379 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16380 props = Fcons (Qface, Fcons (face, Qnil));
16381 if (copy_string)
16382 lisp_string = Fcopy_sequence (lisp_string);
16383 }
16384 if (!NILP (props))
16385 Fadd_text_properties (make_number (0), make_number (len),
16386 props, lisp_string);
16387 }
16388
16389 if (len > 0)
16390 {
16391 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16392 n += len;
16393 }
16394
16395 if (field_width > len)
16396 {
16397 field_width -= len;
16398 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16399 if (!NILP (props))
16400 Fadd_text_properties (make_number (0), make_number (field_width),
16401 props, lisp_string);
16402 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16403 n += field_width;
16404 }
16405
16406 return n;
16407 }
16408
16409
16410 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16411 1, 4, 0,
16412 doc: /* Format a string out of a mode line format specification.
16413 First arg FORMAT specifies the mode line format (see `mode-line-format'
16414 for details) to use.
16415
16416 Optional second arg FACE specifies the face property to put
16417 on all characters for which no face is specified.
16418 t means whatever face the window's mode line currently uses
16419 \(either `mode-line' or `mode-line-inactive', depending).
16420 nil means the default is no face property.
16421 If FACE is an integer, the value string has no text properties.
16422
16423 Optional third and fourth args WINDOW and BUFFER specify the window
16424 and buffer to use as the context for the formatting (defaults
16425 are the selected window and the window's buffer). */)
16426 (format, face, window, buffer)
16427 Lisp_Object format, face, window, buffer;
16428 {
16429 struct it it;
16430 int len;
16431 struct window *w;
16432 struct buffer *old_buffer = NULL;
16433 int face_id = -1;
16434 int no_props = INTEGERP (face);
16435 int count = SPECPDL_INDEX ();
16436 Lisp_Object str;
16437 int string_start = 0;
16438
16439 if (NILP (window))
16440 window = selected_window;
16441 CHECK_WINDOW (window);
16442 w = XWINDOW (window);
16443
16444 if (NILP (buffer))
16445 buffer = w->buffer;
16446 CHECK_BUFFER (buffer);
16447
16448 if (NILP (format))
16449 return build_string ("");
16450
16451 if (no_props)
16452 face = Qnil;
16453
16454 if (!NILP (face))
16455 {
16456 if (EQ (face, Qt))
16457 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16458 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16459 }
16460
16461 if (face_id < 0)
16462 face_id = DEFAULT_FACE_ID;
16463
16464 if (XBUFFER (buffer) != current_buffer)
16465 old_buffer = current_buffer;
16466
16467 record_unwind_protect (unwind_format_mode_line,
16468 format_mode_line_unwind_data (old_buffer));
16469
16470 if (old_buffer)
16471 set_buffer_internal_1 (XBUFFER (buffer));
16472
16473 init_iterator (&it, w, -1, -1, NULL, face_id);
16474
16475 if (no_props)
16476 {
16477 mode_line_target = MODE_LINE_NOPROP;
16478 mode_line_string_face_prop = Qnil;
16479 mode_line_string_list = Qnil;
16480 string_start = MODE_LINE_NOPROP_LEN (0);
16481 }
16482 else
16483 {
16484 mode_line_target = MODE_LINE_STRING;
16485 mode_line_string_list = Qnil;
16486 mode_line_string_face = face;
16487 mode_line_string_face_prop
16488 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16489 }
16490
16491 push_frame_kboard (it.f);
16492 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16493 pop_frame_kboard ();
16494
16495 if (no_props)
16496 {
16497 len = MODE_LINE_NOPROP_LEN (string_start);
16498 str = make_string (mode_line_noprop_buf + string_start, len);
16499 }
16500 else
16501 {
16502 mode_line_string_list = Fnreverse (mode_line_string_list);
16503 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16504 make_string ("", 0));
16505 }
16506
16507 unbind_to (count, Qnil);
16508 return str;
16509 }
16510
16511 /* Write a null-terminated, right justified decimal representation of
16512 the positive integer D to BUF using a minimal field width WIDTH. */
16513
16514 static void
16515 pint2str (buf, width, d)
16516 register char *buf;
16517 register int width;
16518 register int d;
16519 {
16520 register char *p = buf;
16521
16522 if (d <= 0)
16523 *p++ = '0';
16524 else
16525 {
16526 while (d > 0)
16527 {
16528 *p++ = d % 10 + '0';
16529 d /= 10;
16530 }
16531 }
16532
16533 for (width -= (int) (p - buf); width > 0; --width)
16534 *p++ = ' ';
16535 *p-- = '\0';
16536 while (p > buf)
16537 {
16538 d = *buf;
16539 *buf++ = *p;
16540 *p-- = d;
16541 }
16542 }
16543
16544 /* Write a null-terminated, right justified decimal and "human
16545 readable" representation of the nonnegative integer D to BUF using
16546 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16547
16548 static const char power_letter[] =
16549 {
16550 0, /* not used */
16551 'k', /* kilo */
16552 'M', /* mega */
16553 'G', /* giga */
16554 'T', /* tera */
16555 'P', /* peta */
16556 'E', /* exa */
16557 'Z', /* zetta */
16558 'Y' /* yotta */
16559 };
16560
16561 static void
16562 pint2hrstr (buf, width, d)
16563 char *buf;
16564 int width;
16565 int d;
16566 {
16567 /* We aim to represent the nonnegative integer D as
16568 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16569 int quotient = d;
16570 int remainder = 0;
16571 /* -1 means: do not use TENTHS. */
16572 int tenths = -1;
16573 int exponent = 0;
16574
16575 /* Length of QUOTIENT.TENTHS as a string. */
16576 int length;
16577
16578 char * psuffix;
16579 char * p;
16580
16581 if (1000 <= quotient)
16582 {
16583 /* Scale to the appropriate EXPONENT. */
16584 do
16585 {
16586 remainder = quotient % 1000;
16587 quotient /= 1000;
16588 exponent++;
16589 }
16590 while (1000 <= quotient);
16591
16592 /* Round to nearest and decide whether to use TENTHS or not. */
16593 if (quotient <= 9)
16594 {
16595 tenths = remainder / 100;
16596 if (50 <= remainder % 100)
16597 {
16598 if (tenths < 9)
16599 tenths++;
16600 else
16601 {
16602 quotient++;
16603 if (quotient == 10)
16604 tenths = -1;
16605 else
16606 tenths = 0;
16607 }
16608 }
16609 }
16610 else
16611 if (500 <= remainder)
16612 {
16613 if (quotient < 999)
16614 quotient++;
16615 else
16616 {
16617 quotient = 1;
16618 exponent++;
16619 tenths = 0;
16620 }
16621 }
16622 }
16623
16624 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16625 if (tenths == -1 && quotient <= 99)
16626 if (quotient <= 9)
16627 length = 1;
16628 else
16629 length = 2;
16630 else
16631 length = 3;
16632 p = psuffix = buf + max (width, length);
16633
16634 /* Print EXPONENT. */
16635 if (exponent)
16636 *psuffix++ = power_letter[exponent];
16637 *psuffix = '\0';
16638
16639 /* Print TENTHS. */
16640 if (tenths >= 0)
16641 {
16642 *--p = '0' + tenths;
16643 *--p = '.';
16644 }
16645
16646 /* Print QUOTIENT. */
16647 do
16648 {
16649 int digit = quotient % 10;
16650 *--p = '0' + digit;
16651 }
16652 while ((quotient /= 10) != 0);
16653
16654 /* Print leading spaces. */
16655 while (buf < p)
16656 *--p = ' ';
16657 }
16658
16659 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16660 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16661 type of CODING_SYSTEM. Return updated pointer into BUF. */
16662
16663 static unsigned char invalid_eol_type[] = "(*invalid*)";
16664
16665 static char *
16666 decode_mode_spec_coding (coding_system, buf, eol_flag)
16667 Lisp_Object coding_system;
16668 register char *buf;
16669 int eol_flag;
16670 {
16671 Lisp_Object val;
16672 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16673 const unsigned char *eol_str;
16674 int eol_str_len;
16675 /* The EOL conversion we are using. */
16676 Lisp_Object eoltype;
16677
16678 val = Fget (coding_system, Qcoding_system);
16679 eoltype = Qnil;
16680
16681 if (!VECTORP (val)) /* Not yet decided. */
16682 {
16683 if (multibyte)
16684 *buf++ = '-';
16685 if (eol_flag)
16686 eoltype = eol_mnemonic_undecided;
16687 /* Don't mention EOL conversion if it isn't decided. */
16688 }
16689 else
16690 {
16691 Lisp_Object eolvalue;
16692
16693 eolvalue = Fget (coding_system, Qeol_type);
16694
16695 if (multibyte)
16696 *buf++ = XFASTINT (AREF (val, 1));
16697
16698 if (eol_flag)
16699 {
16700 /* The EOL conversion that is normal on this system. */
16701
16702 if (NILP (eolvalue)) /* Not yet decided. */
16703 eoltype = eol_mnemonic_undecided;
16704 else if (VECTORP (eolvalue)) /* Not yet decided. */
16705 eoltype = eol_mnemonic_undecided;
16706 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16707 eoltype = (XFASTINT (eolvalue) == 0
16708 ? eol_mnemonic_unix
16709 : (XFASTINT (eolvalue) == 1
16710 ? eol_mnemonic_dos : eol_mnemonic_mac));
16711 }
16712 }
16713
16714 if (eol_flag)
16715 {
16716 /* Mention the EOL conversion if it is not the usual one. */
16717 if (STRINGP (eoltype))
16718 {
16719 eol_str = SDATA (eoltype);
16720 eol_str_len = SBYTES (eoltype);
16721 }
16722 else if (INTEGERP (eoltype)
16723 && CHAR_VALID_P (XINT (eoltype), 0))
16724 {
16725 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16726 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16727 eol_str = tmp;
16728 }
16729 else
16730 {
16731 eol_str = invalid_eol_type;
16732 eol_str_len = sizeof (invalid_eol_type) - 1;
16733 }
16734 bcopy (eol_str, buf, eol_str_len);
16735 buf += eol_str_len;
16736 }
16737
16738 return buf;
16739 }
16740
16741 /* Return a string for the output of a mode line %-spec for window W,
16742 generated by character C. PRECISION >= 0 means don't return a
16743 string longer than that value. FIELD_WIDTH > 0 means pad the
16744 string returned with spaces to that value. Return 1 in *MULTIBYTE
16745 if the result is multibyte text.
16746
16747 Note we operate on the current buffer for most purposes,
16748 the exception being w->base_line_pos. */
16749
16750 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16751
16752 static char *
16753 decode_mode_spec (w, c, field_width, precision, multibyte)
16754 struct window *w;
16755 register int c;
16756 int field_width, precision;
16757 int *multibyte;
16758 {
16759 Lisp_Object obj;
16760 struct frame *f = XFRAME (WINDOW_FRAME (w));
16761 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16762 struct buffer *b = current_buffer;
16763
16764 obj = Qnil;
16765 *multibyte = 0;
16766
16767 switch (c)
16768 {
16769 case '*':
16770 if (!NILP (b->read_only))
16771 return "%";
16772 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16773 return "*";
16774 return "-";
16775
16776 case '+':
16777 /* This differs from %* only for a modified read-only buffer. */
16778 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16779 return "*";
16780 if (!NILP (b->read_only))
16781 return "%";
16782 return "-";
16783
16784 case '&':
16785 /* This differs from %* in ignoring read-only-ness. */
16786 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16787 return "*";
16788 return "-";
16789
16790 case '%':
16791 return "%";
16792
16793 case '[':
16794 {
16795 int i;
16796 char *p;
16797
16798 if (command_loop_level > 5)
16799 return "[[[... ";
16800 p = decode_mode_spec_buf;
16801 for (i = 0; i < command_loop_level; i++)
16802 *p++ = '[';
16803 *p = 0;
16804 return decode_mode_spec_buf;
16805 }
16806
16807 case ']':
16808 {
16809 int i;
16810 char *p;
16811
16812 if (command_loop_level > 5)
16813 return " ...]]]";
16814 p = decode_mode_spec_buf;
16815 for (i = 0; i < command_loop_level; i++)
16816 *p++ = ']';
16817 *p = 0;
16818 return decode_mode_spec_buf;
16819 }
16820
16821 case '-':
16822 {
16823 register int i;
16824
16825 /* Let lots_of_dashes be a string of infinite length. */
16826 if (mode_line_target == MODE_LINE_NOPROP ||
16827 mode_line_target == MODE_LINE_STRING)
16828 return "--";
16829 if (field_width <= 0
16830 || field_width > sizeof (lots_of_dashes))
16831 {
16832 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16833 decode_mode_spec_buf[i] = '-';
16834 decode_mode_spec_buf[i] = '\0';
16835 return decode_mode_spec_buf;
16836 }
16837 else
16838 return lots_of_dashes;
16839 }
16840
16841 case 'b':
16842 obj = b->name;
16843 break;
16844
16845 case 'c':
16846 {
16847 int col = (int) current_column (); /* iftc */
16848 w->column_number_displayed = make_number (col);
16849 pint2str (decode_mode_spec_buf, field_width, col);
16850 return decode_mode_spec_buf;
16851 }
16852
16853 case 'F':
16854 /* %F displays the frame name. */
16855 if (!NILP (f->title))
16856 return (char *) SDATA (f->title);
16857 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16858 return (char *) SDATA (f->name);
16859 return "Emacs";
16860
16861 case 'f':
16862 obj = b->filename;
16863 break;
16864
16865 case 'i':
16866 {
16867 int size = ZV - BEGV;
16868 pint2str (decode_mode_spec_buf, field_width, size);
16869 return decode_mode_spec_buf;
16870 }
16871
16872 case 'I':
16873 {
16874 int size = ZV - BEGV;
16875 pint2hrstr (decode_mode_spec_buf, field_width, size);
16876 return decode_mode_spec_buf;
16877 }
16878
16879 case 'l':
16880 {
16881 int startpos = XMARKER (w->start)->charpos;
16882 int startpos_byte = marker_byte_position (w->start);
16883 int line, linepos, linepos_byte, topline;
16884 int nlines, junk;
16885 int height = WINDOW_TOTAL_LINES (w);
16886
16887 /* If we decided that this buffer isn't suitable for line numbers,
16888 don't forget that too fast. */
16889 if (EQ (w->base_line_pos, w->buffer))
16890 goto no_value;
16891 /* But do forget it, if the window shows a different buffer now. */
16892 else if (BUFFERP (w->base_line_pos))
16893 w->base_line_pos = Qnil;
16894
16895 /* If the buffer is very big, don't waste time. */
16896 if (INTEGERP (Vline_number_display_limit)
16897 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16898 {
16899 w->base_line_pos = Qnil;
16900 w->base_line_number = Qnil;
16901 goto no_value;
16902 }
16903
16904 if (!NILP (w->base_line_number)
16905 && !NILP (w->base_line_pos)
16906 && XFASTINT (w->base_line_pos) <= startpos)
16907 {
16908 line = XFASTINT (w->base_line_number);
16909 linepos = XFASTINT (w->base_line_pos);
16910 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16911 }
16912 else
16913 {
16914 line = 1;
16915 linepos = BUF_BEGV (b);
16916 linepos_byte = BUF_BEGV_BYTE (b);
16917 }
16918
16919 /* Count lines from base line to window start position. */
16920 nlines = display_count_lines (linepos, linepos_byte,
16921 startpos_byte,
16922 startpos, &junk);
16923
16924 topline = nlines + line;
16925
16926 /* Determine a new base line, if the old one is too close
16927 or too far away, or if we did not have one.
16928 "Too close" means it's plausible a scroll-down would
16929 go back past it. */
16930 if (startpos == BUF_BEGV (b))
16931 {
16932 w->base_line_number = make_number (topline);
16933 w->base_line_pos = make_number (BUF_BEGV (b));
16934 }
16935 else if (nlines < height + 25 || nlines > height * 3 + 50
16936 || linepos == BUF_BEGV (b))
16937 {
16938 int limit = BUF_BEGV (b);
16939 int limit_byte = BUF_BEGV_BYTE (b);
16940 int position;
16941 int distance = (height * 2 + 30) * line_number_display_limit_width;
16942
16943 if (startpos - distance > limit)
16944 {
16945 limit = startpos - distance;
16946 limit_byte = CHAR_TO_BYTE (limit);
16947 }
16948
16949 nlines = display_count_lines (startpos, startpos_byte,
16950 limit_byte,
16951 - (height * 2 + 30),
16952 &position);
16953 /* If we couldn't find the lines we wanted within
16954 line_number_display_limit_width chars per line,
16955 give up on line numbers for this window. */
16956 if (position == limit_byte && limit == startpos - distance)
16957 {
16958 w->base_line_pos = w->buffer;
16959 w->base_line_number = Qnil;
16960 goto no_value;
16961 }
16962
16963 w->base_line_number = make_number (topline - nlines);
16964 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16965 }
16966
16967 /* Now count lines from the start pos to point. */
16968 nlines = display_count_lines (startpos, startpos_byte,
16969 PT_BYTE, PT, &junk);
16970
16971 /* Record that we did display the line number. */
16972 line_number_displayed = 1;
16973
16974 /* Make the string to show. */
16975 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16976 return decode_mode_spec_buf;
16977 no_value:
16978 {
16979 char* p = decode_mode_spec_buf;
16980 int pad = field_width - 2;
16981 while (pad-- > 0)
16982 *p++ = ' ';
16983 *p++ = '?';
16984 *p++ = '?';
16985 *p = '\0';
16986 return decode_mode_spec_buf;
16987 }
16988 }
16989 break;
16990
16991 case 'm':
16992 obj = b->mode_name;
16993 break;
16994
16995 case 'n':
16996 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16997 return " Narrow";
16998 break;
16999
17000 case 'p':
17001 {
17002 int pos = marker_position (w->start);
17003 int total = BUF_ZV (b) - BUF_BEGV (b);
17004
17005 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17006 {
17007 if (pos <= BUF_BEGV (b))
17008 return "All";
17009 else
17010 return "Bottom";
17011 }
17012 else if (pos <= BUF_BEGV (b))
17013 return "Top";
17014 else
17015 {
17016 if (total > 1000000)
17017 /* Do it differently for a large value, to avoid overflow. */
17018 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17019 else
17020 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17021 /* We can't normally display a 3-digit number,
17022 so get us a 2-digit number that is close. */
17023 if (total == 100)
17024 total = 99;
17025 sprintf (decode_mode_spec_buf, "%2d%%", total);
17026 return decode_mode_spec_buf;
17027 }
17028 }
17029
17030 /* Display percentage of size above the bottom of the screen. */
17031 case 'P':
17032 {
17033 int toppos = marker_position (w->start);
17034 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17035 int total = BUF_ZV (b) - BUF_BEGV (b);
17036
17037 if (botpos >= BUF_ZV (b))
17038 {
17039 if (toppos <= BUF_BEGV (b))
17040 return "All";
17041 else
17042 return "Bottom";
17043 }
17044 else
17045 {
17046 if (total > 1000000)
17047 /* Do it differently for a large value, to avoid overflow. */
17048 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17049 else
17050 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17051 /* We can't normally display a 3-digit number,
17052 so get us a 2-digit number that is close. */
17053 if (total == 100)
17054 total = 99;
17055 if (toppos <= BUF_BEGV (b))
17056 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17057 else
17058 sprintf (decode_mode_spec_buf, "%2d%%", total);
17059 return decode_mode_spec_buf;
17060 }
17061 }
17062
17063 case 's':
17064 /* status of process */
17065 obj = Fget_buffer_process (Fcurrent_buffer ());
17066 if (NILP (obj))
17067 return "no process";
17068 #ifdef subprocesses
17069 obj = Fsymbol_name (Fprocess_status (obj));
17070 #endif
17071 break;
17072
17073 case 't': /* indicate TEXT or BINARY */
17074 #ifdef MODE_LINE_BINARY_TEXT
17075 return MODE_LINE_BINARY_TEXT (b);
17076 #else
17077 return "T";
17078 #endif
17079
17080 case 'z':
17081 /* coding-system (not including end-of-line format) */
17082 case 'Z':
17083 /* coding-system (including end-of-line type) */
17084 {
17085 int eol_flag = (c == 'Z');
17086 char *p = decode_mode_spec_buf;
17087
17088 if (! FRAME_WINDOW_P (f))
17089 {
17090 /* No need to mention EOL here--the terminal never needs
17091 to do EOL conversion. */
17092 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17093 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17094 }
17095 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17096 p, eol_flag);
17097
17098 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17099 #ifdef subprocesses
17100 obj = Fget_buffer_process (Fcurrent_buffer ());
17101 if (PROCESSP (obj))
17102 {
17103 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17104 p, eol_flag);
17105 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17106 p, eol_flag);
17107 }
17108 #endif /* subprocesses */
17109 #endif /* 0 */
17110 *p = 0;
17111 return decode_mode_spec_buf;
17112 }
17113 }
17114
17115 if (STRINGP (obj))
17116 {
17117 *multibyte = STRING_MULTIBYTE (obj);
17118 return (char *) SDATA (obj);
17119 }
17120 else
17121 return "";
17122 }
17123
17124
17125 /* Count up to COUNT lines starting from START / START_BYTE.
17126 But don't go beyond LIMIT_BYTE.
17127 Return the number of lines thus found (always nonnegative).
17128
17129 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17130
17131 static int
17132 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17133 int start, start_byte, limit_byte, count;
17134 int *byte_pos_ptr;
17135 {
17136 register unsigned char *cursor;
17137 unsigned char *base;
17138
17139 register int ceiling;
17140 register unsigned char *ceiling_addr;
17141 int orig_count = count;
17142
17143 /* If we are not in selective display mode,
17144 check only for newlines. */
17145 int selective_display = (!NILP (current_buffer->selective_display)
17146 && !INTEGERP (current_buffer->selective_display));
17147
17148 if (count > 0)
17149 {
17150 while (start_byte < limit_byte)
17151 {
17152 ceiling = BUFFER_CEILING_OF (start_byte);
17153 ceiling = min (limit_byte - 1, ceiling);
17154 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17155 base = (cursor = BYTE_POS_ADDR (start_byte));
17156 while (1)
17157 {
17158 if (selective_display)
17159 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17160 ;
17161 else
17162 while (*cursor != '\n' && ++cursor != ceiling_addr)
17163 ;
17164
17165 if (cursor != ceiling_addr)
17166 {
17167 if (--count == 0)
17168 {
17169 start_byte += cursor - base + 1;
17170 *byte_pos_ptr = start_byte;
17171 return orig_count;
17172 }
17173 else
17174 if (++cursor == ceiling_addr)
17175 break;
17176 }
17177 else
17178 break;
17179 }
17180 start_byte += cursor - base;
17181 }
17182 }
17183 else
17184 {
17185 while (start_byte > limit_byte)
17186 {
17187 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17188 ceiling = max (limit_byte, ceiling);
17189 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17190 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17191 while (1)
17192 {
17193 if (selective_display)
17194 while (--cursor != ceiling_addr
17195 && *cursor != '\n' && *cursor != 015)
17196 ;
17197 else
17198 while (--cursor != ceiling_addr && *cursor != '\n')
17199 ;
17200
17201 if (cursor != ceiling_addr)
17202 {
17203 if (++count == 0)
17204 {
17205 start_byte += cursor - base + 1;
17206 *byte_pos_ptr = start_byte;
17207 /* When scanning backwards, we should
17208 not count the newline posterior to which we stop. */
17209 return - orig_count - 1;
17210 }
17211 }
17212 else
17213 break;
17214 }
17215 /* Here we add 1 to compensate for the last decrement
17216 of CURSOR, which took it past the valid range. */
17217 start_byte += cursor - base + 1;
17218 }
17219 }
17220
17221 *byte_pos_ptr = limit_byte;
17222
17223 if (count < 0)
17224 return - orig_count + count;
17225 return orig_count - count;
17226
17227 }
17228
17229
17230 \f
17231 /***********************************************************************
17232 Displaying strings
17233 ***********************************************************************/
17234
17235 /* Display a NUL-terminated string, starting with index START.
17236
17237 If STRING is non-null, display that C string. Otherwise, the Lisp
17238 string LISP_STRING is displayed.
17239
17240 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17241 FACE_STRING. Display STRING or LISP_STRING with the face at
17242 FACE_STRING_POS in FACE_STRING:
17243
17244 Display the string in the environment given by IT, but use the
17245 standard display table, temporarily.
17246
17247 FIELD_WIDTH is the minimum number of output glyphs to produce.
17248 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17249 with spaces. If STRING has more characters, more than FIELD_WIDTH
17250 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17251
17252 PRECISION is the maximum number of characters to output from
17253 STRING. PRECISION < 0 means don't truncate the string.
17254
17255 This is roughly equivalent to printf format specifiers:
17256
17257 FIELD_WIDTH PRECISION PRINTF
17258 ----------------------------------------
17259 -1 -1 %s
17260 -1 10 %.10s
17261 10 -1 %10s
17262 20 10 %20.10s
17263
17264 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17265 display them, and < 0 means obey the current buffer's value of
17266 enable_multibyte_characters.
17267
17268 Value is the number of glyphs produced. */
17269
17270 static int
17271 display_string (string, lisp_string, face_string, face_string_pos,
17272 start, it, field_width, precision, max_x, multibyte)
17273 unsigned char *string;
17274 Lisp_Object lisp_string;
17275 Lisp_Object face_string;
17276 int face_string_pos;
17277 int start;
17278 struct it *it;
17279 int field_width, precision, max_x;
17280 int multibyte;
17281 {
17282 int hpos_at_start = it->hpos;
17283 int saved_face_id = it->face_id;
17284 struct glyph_row *row = it->glyph_row;
17285
17286 /* Initialize the iterator IT for iteration over STRING beginning
17287 with index START. */
17288 reseat_to_string (it, string, lisp_string, start,
17289 precision, field_width, multibyte);
17290
17291 /* If displaying STRING, set up the face of the iterator
17292 from LISP_STRING, if that's given. */
17293 if (STRINGP (face_string))
17294 {
17295 int endptr;
17296 struct face *face;
17297
17298 it->face_id
17299 = face_at_string_position (it->w, face_string, face_string_pos,
17300 0, it->region_beg_charpos,
17301 it->region_end_charpos,
17302 &endptr, it->base_face_id, 0);
17303 face = FACE_FROM_ID (it->f, it->face_id);
17304 it->face_box_p = face->box != FACE_NO_BOX;
17305 }
17306
17307 /* Set max_x to the maximum allowed X position. Don't let it go
17308 beyond the right edge of the window. */
17309 if (max_x <= 0)
17310 max_x = it->last_visible_x;
17311 else
17312 max_x = min (max_x, it->last_visible_x);
17313
17314 /* Skip over display elements that are not visible. because IT->w is
17315 hscrolled. */
17316 if (it->current_x < it->first_visible_x)
17317 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17318 MOVE_TO_POS | MOVE_TO_X);
17319
17320 row->ascent = it->max_ascent;
17321 row->height = it->max_ascent + it->max_descent;
17322 row->phys_ascent = it->max_phys_ascent;
17323 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17324 row->extra_line_spacing = it->max_extra_line_spacing;
17325
17326 /* This condition is for the case that we are called with current_x
17327 past last_visible_x. */
17328 while (it->current_x < max_x)
17329 {
17330 int x_before, x, n_glyphs_before, i, nglyphs;
17331
17332 /* Get the next display element. */
17333 if (!get_next_display_element (it))
17334 break;
17335
17336 /* Produce glyphs. */
17337 x_before = it->current_x;
17338 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17339 PRODUCE_GLYPHS (it);
17340
17341 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17342 i = 0;
17343 x = x_before;
17344 while (i < nglyphs)
17345 {
17346 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17347
17348 if (!it->truncate_lines_p
17349 && x + glyph->pixel_width > max_x)
17350 {
17351 /* End of continued line or max_x reached. */
17352 if (CHAR_GLYPH_PADDING_P (*glyph))
17353 {
17354 /* A wide character is unbreakable. */
17355 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17356 it->current_x = x_before;
17357 }
17358 else
17359 {
17360 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17361 it->current_x = x;
17362 }
17363 break;
17364 }
17365 else if (x + glyph->pixel_width > it->first_visible_x)
17366 {
17367 /* Glyph is at least partially visible. */
17368 ++it->hpos;
17369 if (x < it->first_visible_x)
17370 it->glyph_row->x = x - it->first_visible_x;
17371 }
17372 else
17373 {
17374 /* Glyph is off the left margin of the display area.
17375 Should not happen. */
17376 abort ();
17377 }
17378
17379 row->ascent = max (row->ascent, it->max_ascent);
17380 row->height = max (row->height, it->max_ascent + it->max_descent);
17381 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17382 row->phys_height = max (row->phys_height,
17383 it->max_phys_ascent + it->max_phys_descent);
17384 row->extra_line_spacing = max (row->extra_line_spacing,
17385 it->max_extra_line_spacing);
17386 x += glyph->pixel_width;
17387 ++i;
17388 }
17389
17390 /* Stop if max_x reached. */
17391 if (i < nglyphs)
17392 break;
17393
17394 /* Stop at line ends. */
17395 if (ITERATOR_AT_END_OF_LINE_P (it))
17396 {
17397 it->continuation_lines_width = 0;
17398 break;
17399 }
17400
17401 set_iterator_to_next (it, 1);
17402
17403 /* Stop if truncating at the right edge. */
17404 if (it->truncate_lines_p
17405 && it->current_x >= it->last_visible_x)
17406 {
17407 /* Add truncation mark, but don't do it if the line is
17408 truncated at a padding space. */
17409 if (IT_CHARPOS (*it) < it->string_nchars)
17410 {
17411 if (!FRAME_WINDOW_P (it->f))
17412 {
17413 int i, n;
17414
17415 if (it->current_x > it->last_visible_x)
17416 {
17417 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17418 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17419 break;
17420 for (n = row->used[TEXT_AREA]; i < n; ++i)
17421 {
17422 row->used[TEXT_AREA] = i;
17423 produce_special_glyphs (it, IT_TRUNCATION);
17424 }
17425 }
17426 produce_special_glyphs (it, IT_TRUNCATION);
17427 }
17428 it->glyph_row->truncated_on_right_p = 1;
17429 }
17430 break;
17431 }
17432 }
17433
17434 /* Maybe insert a truncation at the left. */
17435 if (it->first_visible_x
17436 && IT_CHARPOS (*it) > 0)
17437 {
17438 if (!FRAME_WINDOW_P (it->f))
17439 insert_left_trunc_glyphs (it);
17440 it->glyph_row->truncated_on_left_p = 1;
17441 }
17442
17443 it->face_id = saved_face_id;
17444
17445 /* Value is number of columns displayed. */
17446 return it->hpos - hpos_at_start;
17447 }
17448
17449
17450 \f
17451 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17452 appears as an element of LIST or as the car of an element of LIST.
17453 If PROPVAL is a list, compare each element against LIST in that
17454 way, and return 1/2 if any element of PROPVAL is found in LIST.
17455 Otherwise return 0. This function cannot quit.
17456 The return value is 2 if the text is invisible but with an ellipsis
17457 and 1 if it's invisible and without an ellipsis. */
17458
17459 int
17460 invisible_p (propval, list)
17461 register Lisp_Object propval;
17462 Lisp_Object list;
17463 {
17464 register Lisp_Object tail, proptail;
17465
17466 for (tail = list; CONSP (tail); tail = XCDR (tail))
17467 {
17468 register Lisp_Object tem;
17469 tem = XCAR (tail);
17470 if (EQ (propval, tem))
17471 return 1;
17472 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17473 return NILP (XCDR (tem)) ? 1 : 2;
17474 }
17475
17476 if (CONSP (propval))
17477 {
17478 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17479 {
17480 Lisp_Object propelt;
17481 propelt = XCAR (proptail);
17482 for (tail = list; CONSP (tail); tail = XCDR (tail))
17483 {
17484 register Lisp_Object tem;
17485 tem = XCAR (tail);
17486 if (EQ (propelt, tem))
17487 return 1;
17488 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17489 return NILP (XCDR (tem)) ? 1 : 2;
17490 }
17491 }
17492 }
17493
17494 return 0;
17495 }
17496
17497 /* Calculate a width or height in pixels from a specification using
17498 the following elements:
17499
17500 SPEC ::=
17501 NUM - a (fractional) multiple of the default font width/height
17502 (NUM) - specifies exactly NUM pixels
17503 UNIT - a fixed number of pixels, see below.
17504 ELEMENT - size of a display element in pixels, see below.
17505 (NUM . SPEC) - equals NUM * SPEC
17506 (+ SPEC SPEC ...) - add pixel values
17507 (- SPEC SPEC ...) - subtract pixel values
17508 (- SPEC) - negate pixel value
17509
17510 NUM ::=
17511 INT or FLOAT - a number constant
17512 SYMBOL - use symbol's (buffer local) variable binding.
17513
17514 UNIT ::=
17515 in - pixels per inch *)
17516 mm - pixels per 1/1000 meter *)
17517 cm - pixels per 1/100 meter *)
17518 width - width of current font in pixels.
17519 height - height of current font in pixels.
17520
17521 *) using the ratio(s) defined in display-pixels-per-inch.
17522
17523 ELEMENT ::=
17524
17525 left-fringe - left fringe width in pixels
17526 right-fringe - right fringe width in pixels
17527
17528 left-margin - left margin width in pixels
17529 right-margin - right margin width in pixels
17530
17531 scroll-bar - scroll-bar area width in pixels
17532
17533 Examples:
17534
17535 Pixels corresponding to 5 inches:
17536 (5 . in)
17537
17538 Total width of non-text areas on left side of window (if scroll-bar is on left):
17539 '(space :width (+ left-fringe left-margin scroll-bar))
17540
17541 Align to first text column (in header line):
17542 '(space :align-to 0)
17543
17544 Align to middle of text area minus half the width of variable `my-image'
17545 containing a loaded image:
17546 '(space :align-to (0.5 . (- text my-image)))
17547
17548 Width of left margin minus width of 1 character in the default font:
17549 '(space :width (- left-margin 1))
17550
17551 Width of left margin minus width of 2 characters in the current font:
17552 '(space :width (- left-margin (2 . width)))
17553
17554 Center 1 character over left-margin (in header line):
17555 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17556
17557 Different ways to express width of left fringe plus left margin minus one pixel:
17558 '(space :width (- (+ left-fringe left-margin) (1)))
17559 '(space :width (+ left-fringe left-margin (- (1))))
17560 '(space :width (+ left-fringe left-margin (-1)))
17561
17562 */
17563
17564 #define NUMVAL(X) \
17565 ((INTEGERP (X) || FLOATP (X)) \
17566 ? XFLOATINT (X) \
17567 : - 1)
17568
17569 int
17570 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17571 double *res;
17572 struct it *it;
17573 Lisp_Object prop;
17574 void *font;
17575 int width_p, *align_to;
17576 {
17577 double pixels;
17578
17579 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17580 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17581
17582 if (NILP (prop))
17583 return OK_PIXELS (0);
17584
17585 if (SYMBOLP (prop))
17586 {
17587 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17588 {
17589 char *unit = SDATA (SYMBOL_NAME (prop));
17590
17591 if (unit[0] == 'i' && unit[1] == 'n')
17592 pixels = 1.0;
17593 else if (unit[0] == 'm' && unit[1] == 'm')
17594 pixels = 25.4;
17595 else if (unit[0] == 'c' && unit[1] == 'm')
17596 pixels = 2.54;
17597 else
17598 pixels = 0;
17599 if (pixels > 0)
17600 {
17601 double ppi;
17602 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17603 || (CONSP (Vdisplay_pixels_per_inch)
17604 && (ppi = (width_p
17605 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17606 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17607 ppi > 0)))
17608 return OK_PIXELS (ppi / pixels);
17609
17610 return 0;
17611 }
17612 }
17613
17614 #ifdef HAVE_WINDOW_SYSTEM
17615 if (EQ (prop, Qheight))
17616 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17617 if (EQ (prop, Qwidth))
17618 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17619 #else
17620 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17621 return OK_PIXELS (1);
17622 #endif
17623
17624 if (EQ (prop, Qtext))
17625 return OK_PIXELS (width_p
17626 ? window_box_width (it->w, TEXT_AREA)
17627 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17628
17629 if (align_to && *align_to < 0)
17630 {
17631 *res = 0;
17632 if (EQ (prop, Qleft))
17633 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17634 if (EQ (prop, Qright))
17635 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17636 if (EQ (prop, Qcenter))
17637 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17638 + window_box_width (it->w, TEXT_AREA) / 2);
17639 if (EQ (prop, Qleft_fringe))
17640 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17641 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17642 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17643 if (EQ (prop, Qright_fringe))
17644 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17645 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17646 : window_box_right_offset (it->w, TEXT_AREA));
17647 if (EQ (prop, Qleft_margin))
17648 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17649 if (EQ (prop, Qright_margin))
17650 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17651 if (EQ (prop, Qscroll_bar))
17652 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17653 ? 0
17654 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17655 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17656 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17657 : 0)));
17658 }
17659 else
17660 {
17661 if (EQ (prop, Qleft_fringe))
17662 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17663 if (EQ (prop, Qright_fringe))
17664 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17665 if (EQ (prop, Qleft_margin))
17666 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17667 if (EQ (prop, Qright_margin))
17668 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17669 if (EQ (prop, Qscroll_bar))
17670 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17671 }
17672
17673 prop = Fbuffer_local_value (prop, it->w->buffer);
17674 }
17675
17676 if (INTEGERP (prop) || FLOATP (prop))
17677 {
17678 int base_unit = (width_p
17679 ? FRAME_COLUMN_WIDTH (it->f)
17680 : FRAME_LINE_HEIGHT (it->f));
17681 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17682 }
17683
17684 if (CONSP (prop))
17685 {
17686 Lisp_Object car = XCAR (prop);
17687 Lisp_Object cdr = XCDR (prop);
17688
17689 if (SYMBOLP (car))
17690 {
17691 #ifdef HAVE_WINDOW_SYSTEM
17692 if (valid_image_p (prop))
17693 {
17694 int id = lookup_image (it->f, prop);
17695 struct image *img = IMAGE_FROM_ID (it->f, id);
17696
17697 return OK_PIXELS (width_p ? img->width : img->height);
17698 }
17699 #endif
17700 if (EQ (car, Qplus) || EQ (car, Qminus))
17701 {
17702 int first = 1;
17703 double px;
17704
17705 pixels = 0;
17706 while (CONSP (cdr))
17707 {
17708 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17709 font, width_p, align_to))
17710 return 0;
17711 if (first)
17712 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17713 else
17714 pixels += px;
17715 cdr = XCDR (cdr);
17716 }
17717 if (EQ (car, Qminus))
17718 pixels = -pixels;
17719 return OK_PIXELS (pixels);
17720 }
17721
17722 car = Fbuffer_local_value (car, it->w->buffer);
17723 }
17724
17725 if (INTEGERP (car) || FLOATP (car))
17726 {
17727 double fact;
17728 pixels = XFLOATINT (car);
17729 if (NILP (cdr))
17730 return OK_PIXELS (pixels);
17731 if (calc_pixel_width_or_height (&fact, it, cdr,
17732 font, width_p, align_to))
17733 return OK_PIXELS (pixels * fact);
17734 return 0;
17735 }
17736
17737 return 0;
17738 }
17739
17740 return 0;
17741 }
17742
17743 \f
17744 /***********************************************************************
17745 Glyph Display
17746 ***********************************************************************/
17747
17748 #ifdef HAVE_WINDOW_SYSTEM
17749
17750 #if GLYPH_DEBUG
17751
17752 void
17753 dump_glyph_string (s)
17754 struct glyph_string *s;
17755 {
17756 fprintf (stderr, "glyph string\n");
17757 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17758 s->x, s->y, s->width, s->height);
17759 fprintf (stderr, " ybase = %d\n", s->ybase);
17760 fprintf (stderr, " hl = %d\n", s->hl);
17761 fprintf (stderr, " left overhang = %d, right = %d\n",
17762 s->left_overhang, s->right_overhang);
17763 fprintf (stderr, " nchars = %d\n", s->nchars);
17764 fprintf (stderr, " extends to end of line = %d\n",
17765 s->extends_to_end_of_line_p);
17766 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17767 fprintf (stderr, " bg width = %d\n", s->background_width);
17768 }
17769
17770 #endif /* GLYPH_DEBUG */
17771
17772 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17773 of XChar2b structures for S; it can't be allocated in
17774 init_glyph_string because it must be allocated via `alloca'. W
17775 is the window on which S is drawn. ROW and AREA are the glyph row
17776 and area within the row from which S is constructed. START is the
17777 index of the first glyph structure covered by S. HL is a
17778 face-override for drawing S. */
17779
17780 #ifdef HAVE_NTGUI
17781 #define OPTIONAL_HDC(hdc) hdc,
17782 #define DECLARE_HDC(hdc) HDC hdc;
17783 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17784 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17785 #endif
17786
17787 #ifndef OPTIONAL_HDC
17788 #define OPTIONAL_HDC(hdc)
17789 #define DECLARE_HDC(hdc)
17790 #define ALLOCATE_HDC(hdc, f)
17791 #define RELEASE_HDC(hdc, f)
17792 #endif
17793
17794 static void
17795 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17796 struct glyph_string *s;
17797 DECLARE_HDC (hdc)
17798 XChar2b *char2b;
17799 struct window *w;
17800 struct glyph_row *row;
17801 enum glyph_row_area area;
17802 int start;
17803 enum draw_glyphs_face hl;
17804 {
17805 bzero (s, sizeof *s);
17806 s->w = w;
17807 s->f = XFRAME (w->frame);
17808 #ifdef HAVE_NTGUI
17809 s->hdc = hdc;
17810 #endif
17811 s->display = FRAME_X_DISPLAY (s->f);
17812 s->window = FRAME_X_WINDOW (s->f);
17813 s->char2b = char2b;
17814 s->hl = hl;
17815 s->row = row;
17816 s->area = area;
17817 s->first_glyph = row->glyphs[area] + start;
17818 s->height = row->height;
17819 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17820
17821 /* Display the internal border below the tool-bar window. */
17822 if (WINDOWP (s->f->tool_bar_window)
17823 && s->w == XWINDOW (s->f->tool_bar_window))
17824 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17825
17826 s->ybase = s->y + row->ascent;
17827 }
17828
17829
17830 /* Append the list of glyph strings with head H and tail T to the list
17831 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17832
17833 static INLINE void
17834 append_glyph_string_lists (head, tail, h, t)
17835 struct glyph_string **head, **tail;
17836 struct glyph_string *h, *t;
17837 {
17838 if (h)
17839 {
17840 if (*head)
17841 (*tail)->next = h;
17842 else
17843 *head = h;
17844 h->prev = *tail;
17845 *tail = t;
17846 }
17847 }
17848
17849
17850 /* Prepend the list of glyph strings with head H and tail T to the
17851 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17852 result. */
17853
17854 static INLINE void
17855 prepend_glyph_string_lists (head, tail, h, t)
17856 struct glyph_string **head, **tail;
17857 struct glyph_string *h, *t;
17858 {
17859 if (h)
17860 {
17861 if (*head)
17862 (*head)->prev = t;
17863 else
17864 *tail = t;
17865 t->next = *head;
17866 *head = h;
17867 }
17868 }
17869
17870
17871 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17872 Set *HEAD and *TAIL to the resulting list. */
17873
17874 static INLINE void
17875 append_glyph_string (head, tail, s)
17876 struct glyph_string **head, **tail;
17877 struct glyph_string *s;
17878 {
17879 s->next = s->prev = NULL;
17880 append_glyph_string_lists (head, tail, s, s);
17881 }
17882
17883
17884 /* Get face and two-byte form of character glyph GLYPH on frame F.
17885 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17886 a pointer to a realized face that is ready for display. */
17887
17888 static INLINE struct face *
17889 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17890 struct frame *f;
17891 struct glyph *glyph;
17892 XChar2b *char2b;
17893 int *two_byte_p;
17894 {
17895 struct face *face;
17896
17897 xassert (glyph->type == CHAR_GLYPH);
17898 face = FACE_FROM_ID (f, glyph->face_id);
17899
17900 if (two_byte_p)
17901 *two_byte_p = 0;
17902
17903 if (!glyph->multibyte_p)
17904 {
17905 /* Unibyte case. We don't have to encode, but we have to make
17906 sure to use a face suitable for unibyte. */
17907 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17908 }
17909 else if (glyph->u.ch < 128
17910 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17911 {
17912 /* Case of ASCII in a face known to fit ASCII. */
17913 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17914 }
17915 else
17916 {
17917 int c1, c2, charset;
17918
17919 /* Split characters into bytes. If c2 is -1 afterwards, C is
17920 really a one-byte character so that byte1 is zero. */
17921 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17922 if (c2 > 0)
17923 STORE_XCHAR2B (char2b, c1, c2);
17924 else
17925 STORE_XCHAR2B (char2b, 0, c1);
17926
17927 /* Maybe encode the character in *CHAR2B. */
17928 if (charset != CHARSET_ASCII)
17929 {
17930 struct font_info *font_info
17931 = FONT_INFO_FROM_ID (f, face->font_info_id);
17932 if (font_info)
17933 glyph->font_type
17934 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17935 }
17936 }
17937
17938 /* Make sure X resources of the face are allocated. */
17939 xassert (face != NULL);
17940 PREPARE_FACE_FOR_DISPLAY (f, face);
17941 return face;
17942 }
17943
17944
17945 /* Fill glyph string S with composition components specified by S->cmp.
17946
17947 FACES is an array of faces for all components of this composition.
17948 S->gidx is the index of the first component for S.
17949 OVERLAPS_P non-zero means S should draw the foreground only, and
17950 use its physical height for clipping.
17951
17952 Value is the index of a component not in S. */
17953
17954 static int
17955 fill_composite_glyph_string (s, faces, overlaps_p)
17956 struct glyph_string *s;
17957 struct face **faces;
17958 int overlaps_p;
17959 {
17960 int i;
17961
17962 xassert (s);
17963
17964 s->for_overlaps_p = overlaps_p;
17965
17966 s->face = faces[s->gidx];
17967 s->font = s->face->font;
17968 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17969
17970 /* For all glyphs of this composition, starting at the offset
17971 S->gidx, until we reach the end of the definition or encounter a
17972 glyph that requires the different face, add it to S. */
17973 ++s->nchars;
17974 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17975 ++s->nchars;
17976
17977 /* All glyph strings for the same composition has the same width,
17978 i.e. the width set for the first component of the composition. */
17979
17980 s->width = s->first_glyph->pixel_width;
17981
17982 /* If the specified font could not be loaded, use the frame's
17983 default font, but record the fact that we couldn't load it in
17984 the glyph string so that we can draw rectangles for the
17985 characters of the glyph string. */
17986 if (s->font == NULL)
17987 {
17988 s->font_not_found_p = 1;
17989 s->font = FRAME_FONT (s->f);
17990 }
17991
17992 /* Adjust base line for subscript/superscript text. */
17993 s->ybase += s->first_glyph->voffset;
17994
17995 xassert (s->face && s->face->gc);
17996
17997 /* This glyph string must always be drawn with 16-bit functions. */
17998 s->two_byte_p = 1;
17999
18000 return s->gidx + s->nchars;
18001 }
18002
18003
18004 /* Fill glyph string S from a sequence of character glyphs.
18005
18006 FACE_ID is the face id of the string. START is the index of the
18007 first glyph to consider, END is the index of the last + 1.
18008 OVERLAPS_P non-zero means S should draw the foreground only, and
18009 use its physical height for clipping.
18010
18011 Value is the index of the first glyph not in S. */
18012
18013 static int
18014 fill_glyph_string (s, face_id, start, end, overlaps_p)
18015 struct glyph_string *s;
18016 int face_id;
18017 int start, end, overlaps_p;
18018 {
18019 struct glyph *glyph, *last;
18020 int voffset;
18021 int glyph_not_available_p;
18022
18023 xassert (s->f == XFRAME (s->w->frame));
18024 xassert (s->nchars == 0);
18025 xassert (start >= 0 && end > start);
18026
18027 s->for_overlaps_p = overlaps_p,
18028 glyph = s->row->glyphs[s->area] + start;
18029 last = s->row->glyphs[s->area] + end;
18030 voffset = glyph->voffset;
18031
18032 glyph_not_available_p = glyph->glyph_not_available_p;
18033
18034 while (glyph < last
18035 && glyph->type == CHAR_GLYPH
18036 && glyph->voffset == voffset
18037 /* Same face id implies same font, nowadays. */
18038 && glyph->face_id == face_id
18039 && glyph->glyph_not_available_p == glyph_not_available_p)
18040 {
18041 int two_byte_p;
18042
18043 s->face = get_glyph_face_and_encoding (s->f, glyph,
18044 s->char2b + s->nchars,
18045 &two_byte_p);
18046 s->two_byte_p = two_byte_p;
18047 ++s->nchars;
18048 xassert (s->nchars <= end - start);
18049 s->width += glyph->pixel_width;
18050 ++glyph;
18051 }
18052
18053 s->font = s->face->font;
18054 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18055
18056 /* If the specified font could not be loaded, use the frame's font,
18057 but record the fact that we couldn't load it in
18058 S->font_not_found_p so that we can draw rectangles for the
18059 characters of the glyph string. */
18060 if (s->font == NULL || glyph_not_available_p)
18061 {
18062 s->font_not_found_p = 1;
18063 s->font = FRAME_FONT (s->f);
18064 }
18065
18066 /* Adjust base line for subscript/superscript text. */
18067 s->ybase += voffset;
18068
18069 xassert (s->face && s->face->gc);
18070 return glyph - s->row->glyphs[s->area];
18071 }
18072
18073
18074 /* Fill glyph string S from image glyph S->first_glyph. */
18075
18076 static void
18077 fill_image_glyph_string (s)
18078 struct glyph_string *s;
18079 {
18080 xassert (s->first_glyph->type == IMAGE_GLYPH);
18081 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18082 xassert (s->img);
18083 s->slice = s->first_glyph->slice;
18084 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18085 s->font = s->face->font;
18086 s->width = s->first_glyph->pixel_width;
18087
18088 /* Adjust base line for subscript/superscript text. */
18089 s->ybase += s->first_glyph->voffset;
18090 }
18091
18092
18093 /* Fill glyph string S from a sequence of stretch glyphs.
18094
18095 ROW is the glyph row in which the glyphs are found, AREA is the
18096 area within the row. START is the index of the first glyph to
18097 consider, END is the index of the last + 1.
18098
18099 Value is the index of the first glyph not in S. */
18100
18101 static int
18102 fill_stretch_glyph_string (s, row, area, start, end)
18103 struct glyph_string *s;
18104 struct glyph_row *row;
18105 enum glyph_row_area area;
18106 int start, end;
18107 {
18108 struct glyph *glyph, *last;
18109 int voffset, face_id;
18110
18111 xassert (s->first_glyph->type == STRETCH_GLYPH);
18112
18113 glyph = s->row->glyphs[s->area] + start;
18114 last = s->row->glyphs[s->area] + end;
18115 face_id = glyph->face_id;
18116 s->face = FACE_FROM_ID (s->f, face_id);
18117 s->font = s->face->font;
18118 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18119 s->width = glyph->pixel_width;
18120 voffset = glyph->voffset;
18121
18122 for (++glyph;
18123 (glyph < last
18124 && glyph->type == STRETCH_GLYPH
18125 && glyph->voffset == voffset
18126 && glyph->face_id == face_id);
18127 ++glyph)
18128 s->width += glyph->pixel_width;
18129
18130 /* Adjust base line for subscript/superscript text. */
18131 s->ybase += voffset;
18132
18133 /* The case that face->gc == 0 is handled when drawing the glyph
18134 string by calling PREPARE_FACE_FOR_DISPLAY. */
18135 xassert (s->face);
18136 return glyph - s->row->glyphs[s->area];
18137 }
18138
18139
18140 /* EXPORT for RIF:
18141 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18142 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18143 assumed to be zero. */
18144
18145 void
18146 x_get_glyph_overhangs (glyph, f, left, right)
18147 struct glyph *glyph;
18148 struct frame *f;
18149 int *left, *right;
18150 {
18151 *left = *right = 0;
18152
18153 if (glyph->type == CHAR_GLYPH)
18154 {
18155 XFontStruct *font;
18156 struct face *face;
18157 struct font_info *font_info;
18158 XChar2b char2b;
18159 XCharStruct *pcm;
18160
18161 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18162 font = face->font;
18163 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18164 if (font /* ++KFS: Should this be font_info ? */
18165 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18166 {
18167 if (pcm->rbearing > pcm->width)
18168 *right = pcm->rbearing - pcm->width;
18169 if (pcm->lbearing < 0)
18170 *left = -pcm->lbearing;
18171 }
18172 }
18173 }
18174
18175
18176 /* Return the index of the first glyph preceding glyph string S that
18177 is overwritten by S because of S's left overhang. Value is -1
18178 if no glyphs are overwritten. */
18179
18180 static int
18181 left_overwritten (s)
18182 struct glyph_string *s;
18183 {
18184 int k;
18185
18186 if (s->left_overhang)
18187 {
18188 int x = 0, i;
18189 struct glyph *glyphs = s->row->glyphs[s->area];
18190 int first = s->first_glyph - glyphs;
18191
18192 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18193 x -= glyphs[i].pixel_width;
18194
18195 k = i + 1;
18196 }
18197 else
18198 k = -1;
18199
18200 return k;
18201 }
18202
18203
18204 /* Return the index of the first glyph preceding glyph string S that
18205 is overwriting S because of its right overhang. Value is -1 if no
18206 glyph in front of S overwrites S. */
18207
18208 static int
18209 left_overwriting (s)
18210 struct glyph_string *s;
18211 {
18212 int i, k, x;
18213 struct glyph *glyphs = s->row->glyphs[s->area];
18214 int first = s->first_glyph - glyphs;
18215
18216 k = -1;
18217 x = 0;
18218 for (i = first - 1; i >= 0; --i)
18219 {
18220 int left, right;
18221 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18222 if (x + right > 0)
18223 k = i;
18224 x -= glyphs[i].pixel_width;
18225 }
18226
18227 return k;
18228 }
18229
18230
18231 /* Return the index of the last glyph following glyph string S that is
18232 not overwritten by S because of S's right overhang. Value is -1 if
18233 no such glyph is found. */
18234
18235 static int
18236 right_overwritten (s)
18237 struct glyph_string *s;
18238 {
18239 int k = -1;
18240
18241 if (s->right_overhang)
18242 {
18243 int x = 0, i;
18244 struct glyph *glyphs = s->row->glyphs[s->area];
18245 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18246 int end = s->row->used[s->area];
18247
18248 for (i = first; i < end && s->right_overhang > x; ++i)
18249 x += glyphs[i].pixel_width;
18250
18251 k = i;
18252 }
18253
18254 return k;
18255 }
18256
18257
18258 /* Return the index of the last glyph following glyph string S that
18259 overwrites S because of its left overhang. Value is negative
18260 if no such glyph is found. */
18261
18262 static int
18263 right_overwriting (s)
18264 struct glyph_string *s;
18265 {
18266 int i, k, x;
18267 int end = s->row->used[s->area];
18268 struct glyph *glyphs = s->row->glyphs[s->area];
18269 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18270
18271 k = -1;
18272 x = 0;
18273 for (i = first; i < end; ++i)
18274 {
18275 int left, right;
18276 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18277 if (x - left < 0)
18278 k = i;
18279 x += glyphs[i].pixel_width;
18280 }
18281
18282 return k;
18283 }
18284
18285
18286 /* Get face and two-byte form of character C in face FACE_ID on frame
18287 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18288 means we want to display multibyte text. DISPLAY_P non-zero means
18289 make sure that X resources for the face returned are allocated.
18290 Value is a pointer to a realized face that is ready for display if
18291 DISPLAY_P is non-zero. */
18292
18293 static INLINE struct face *
18294 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18295 struct frame *f;
18296 int c, face_id;
18297 XChar2b *char2b;
18298 int multibyte_p, display_p;
18299 {
18300 struct face *face = FACE_FROM_ID (f, face_id);
18301
18302 if (!multibyte_p)
18303 {
18304 /* Unibyte case. We don't have to encode, but we have to make
18305 sure to use a face suitable for unibyte. */
18306 STORE_XCHAR2B (char2b, 0, c);
18307 face_id = FACE_FOR_CHAR (f, face, c);
18308 face = FACE_FROM_ID (f, face_id);
18309 }
18310 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18311 {
18312 /* Case of ASCII in a face known to fit ASCII. */
18313 STORE_XCHAR2B (char2b, 0, c);
18314 }
18315 else
18316 {
18317 int c1, c2, charset;
18318
18319 /* Split characters into bytes. If c2 is -1 afterwards, C is
18320 really a one-byte character so that byte1 is zero. */
18321 SPLIT_CHAR (c, charset, c1, c2);
18322 if (c2 > 0)
18323 STORE_XCHAR2B (char2b, c1, c2);
18324 else
18325 STORE_XCHAR2B (char2b, 0, c1);
18326
18327 /* Maybe encode the character in *CHAR2B. */
18328 if (face->font != NULL)
18329 {
18330 struct font_info *font_info
18331 = FONT_INFO_FROM_ID (f, face->font_info_id);
18332 if (font_info)
18333 rif->encode_char (c, char2b, font_info, 0);
18334 }
18335 }
18336
18337 /* Make sure X resources of the face are allocated. */
18338 #ifdef HAVE_X_WINDOWS
18339 if (display_p)
18340 #endif
18341 {
18342 xassert (face != NULL);
18343 PREPARE_FACE_FOR_DISPLAY (f, face);
18344 }
18345
18346 return face;
18347 }
18348
18349
18350 /* Set background width of glyph string S. START is the index of the
18351 first glyph following S. LAST_X is the right-most x-position + 1
18352 in the drawing area. */
18353
18354 static INLINE void
18355 set_glyph_string_background_width (s, start, last_x)
18356 struct glyph_string *s;
18357 int start;
18358 int last_x;
18359 {
18360 /* If the face of this glyph string has to be drawn to the end of
18361 the drawing area, set S->extends_to_end_of_line_p. */
18362 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18363
18364 if (start == s->row->used[s->area]
18365 && s->area == TEXT_AREA
18366 && ((s->hl == DRAW_NORMAL_TEXT
18367 && (s->row->fill_line_p
18368 || s->face->background != default_face->background
18369 || s->face->stipple != default_face->stipple
18370 || s->row->mouse_face_p))
18371 || s->hl == DRAW_MOUSE_FACE
18372 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18373 && s->row->fill_line_p)))
18374 s->extends_to_end_of_line_p = 1;
18375
18376 /* If S extends its face to the end of the line, set its
18377 background_width to the distance to the right edge of the drawing
18378 area. */
18379 if (s->extends_to_end_of_line_p)
18380 s->background_width = last_x - s->x + 1;
18381 else
18382 s->background_width = s->width;
18383 }
18384
18385
18386 /* Compute overhangs and x-positions for glyph string S and its
18387 predecessors, or successors. X is the starting x-position for S.
18388 BACKWARD_P non-zero means process predecessors. */
18389
18390 static void
18391 compute_overhangs_and_x (s, x, backward_p)
18392 struct glyph_string *s;
18393 int x;
18394 int backward_p;
18395 {
18396 if (backward_p)
18397 {
18398 while (s)
18399 {
18400 if (rif->compute_glyph_string_overhangs)
18401 rif->compute_glyph_string_overhangs (s);
18402 x -= s->width;
18403 s->x = x;
18404 s = s->prev;
18405 }
18406 }
18407 else
18408 {
18409 while (s)
18410 {
18411 if (rif->compute_glyph_string_overhangs)
18412 rif->compute_glyph_string_overhangs (s);
18413 s->x = x;
18414 x += s->width;
18415 s = s->next;
18416 }
18417 }
18418 }
18419
18420
18421
18422 /* The following macros are only called from draw_glyphs below.
18423 They reference the following parameters of that function directly:
18424 `w', `row', `area', and `overlap_p'
18425 as well as the following local variables:
18426 `s', `f', and `hdc' (in W32) */
18427
18428 #ifdef HAVE_NTGUI
18429 /* On W32, silently add local `hdc' variable to argument list of
18430 init_glyph_string. */
18431 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18432 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18433 #else
18434 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18435 init_glyph_string (s, char2b, w, row, area, start, hl)
18436 #endif
18437
18438 /* Add a glyph string for a stretch glyph to the list of strings
18439 between HEAD and TAIL. START is the index of the stretch glyph in
18440 row area AREA of glyph row ROW. END is the index of the last glyph
18441 in that glyph row area. X is the current output position assigned
18442 to the new glyph string constructed. HL overrides that face of the
18443 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18444 is the right-most x-position of the drawing area. */
18445
18446 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18447 and below -- keep them on one line. */
18448 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18449 do \
18450 { \
18451 s = (struct glyph_string *) alloca (sizeof *s); \
18452 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18453 START = fill_stretch_glyph_string (s, row, area, START, END); \
18454 append_glyph_string (&HEAD, &TAIL, s); \
18455 s->x = (X); \
18456 } \
18457 while (0)
18458
18459
18460 /* Add a glyph string for an image glyph to the list of strings
18461 between HEAD and TAIL. START is the index of the image glyph in
18462 row area AREA of glyph row ROW. END is the index of the last glyph
18463 in that glyph row area. X is the current output position assigned
18464 to the new glyph string constructed. HL overrides that face of the
18465 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18466 is the right-most x-position of the drawing area. */
18467
18468 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18469 do \
18470 { \
18471 s = (struct glyph_string *) alloca (sizeof *s); \
18472 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18473 fill_image_glyph_string (s); \
18474 append_glyph_string (&HEAD, &TAIL, s); \
18475 ++START; \
18476 s->x = (X); \
18477 } \
18478 while (0)
18479
18480
18481 /* Add a glyph string for a sequence of character glyphs to the list
18482 of strings between HEAD and TAIL. START is the index of the first
18483 glyph in row area AREA of glyph row ROW that is part of the new
18484 glyph string. END is the index of the last glyph in that glyph row
18485 area. X is the current output position assigned to the new glyph
18486 string constructed. HL overrides that face of the glyph; e.g. it
18487 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18488 right-most x-position of the drawing area. */
18489
18490 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18491 do \
18492 { \
18493 int c, face_id; \
18494 XChar2b *char2b; \
18495 \
18496 c = (row)->glyphs[area][START].u.ch; \
18497 face_id = (row)->glyphs[area][START].face_id; \
18498 \
18499 s = (struct glyph_string *) alloca (sizeof *s); \
18500 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18501 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18502 append_glyph_string (&HEAD, &TAIL, s); \
18503 s->x = (X); \
18504 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18505 } \
18506 while (0)
18507
18508
18509 /* Add a glyph string for a composite sequence to the list of strings
18510 between HEAD and TAIL. START is the index of the first glyph in
18511 row area AREA of glyph row ROW that is part of the new glyph
18512 string. END is the index of the last glyph in that glyph row area.
18513 X is the current output position assigned to the new glyph string
18514 constructed. HL overrides that face of the glyph; e.g. it is
18515 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18516 x-position of the drawing area. */
18517
18518 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18519 do { \
18520 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18521 int face_id = (row)->glyphs[area][START].face_id; \
18522 struct face *base_face = FACE_FROM_ID (f, face_id); \
18523 struct composition *cmp = composition_table[cmp_id]; \
18524 int glyph_len = cmp->glyph_len; \
18525 XChar2b *char2b; \
18526 struct face **faces; \
18527 struct glyph_string *first_s = NULL; \
18528 int n; \
18529 \
18530 base_face = base_face->ascii_face; \
18531 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18532 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18533 /* At first, fill in `char2b' and `faces'. */ \
18534 for (n = 0; n < glyph_len; n++) \
18535 { \
18536 int c = COMPOSITION_GLYPH (cmp, n); \
18537 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18538 faces[n] = FACE_FROM_ID (f, this_face_id); \
18539 get_char_face_and_encoding (f, c, this_face_id, \
18540 char2b + n, 1, 1); \
18541 } \
18542 \
18543 /* Make glyph_strings for each glyph sequence that is drawable by \
18544 the same face, and append them to HEAD/TAIL. */ \
18545 for (n = 0; n < cmp->glyph_len;) \
18546 { \
18547 s = (struct glyph_string *) alloca (sizeof *s); \
18548 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18549 append_glyph_string (&(HEAD), &(TAIL), s); \
18550 s->cmp = cmp; \
18551 s->gidx = n; \
18552 s->x = (X); \
18553 \
18554 if (n == 0) \
18555 first_s = s; \
18556 \
18557 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18558 } \
18559 \
18560 ++START; \
18561 s = first_s; \
18562 } while (0)
18563
18564
18565 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18566 of AREA of glyph row ROW on window W between indices START and END.
18567 HL overrides the face for drawing glyph strings, e.g. it is
18568 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18569 x-positions of the drawing area.
18570
18571 This is an ugly monster macro construct because we must use alloca
18572 to allocate glyph strings (because draw_glyphs can be called
18573 asynchronously). */
18574
18575 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18576 do \
18577 { \
18578 HEAD = TAIL = NULL; \
18579 while (START < END) \
18580 { \
18581 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18582 switch (first_glyph->type) \
18583 { \
18584 case CHAR_GLYPH: \
18585 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18586 HL, X, LAST_X); \
18587 break; \
18588 \
18589 case COMPOSITE_GLYPH: \
18590 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18591 HL, X, LAST_X); \
18592 break; \
18593 \
18594 case STRETCH_GLYPH: \
18595 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18596 HL, X, LAST_X); \
18597 break; \
18598 \
18599 case IMAGE_GLYPH: \
18600 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18601 HL, X, LAST_X); \
18602 break; \
18603 \
18604 default: \
18605 abort (); \
18606 } \
18607 \
18608 set_glyph_string_background_width (s, START, LAST_X); \
18609 (X) += s->width; \
18610 } \
18611 } \
18612 while (0)
18613
18614
18615 /* Draw glyphs between START and END in AREA of ROW on window W,
18616 starting at x-position X. X is relative to AREA in W. HL is a
18617 face-override with the following meaning:
18618
18619 DRAW_NORMAL_TEXT draw normally
18620 DRAW_CURSOR draw in cursor face
18621 DRAW_MOUSE_FACE draw in mouse face.
18622 DRAW_INVERSE_VIDEO draw in mode line face
18623 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18624 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18625
18626 If OVERLAPS_P is non-zero, draw only the foreground of characters
18627 and clip to the physical height of ROW.
18628
18629 Value is the x-position reached, relative to AREA of W. */
18630
18631 static int
18632 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18633 struct window *w;
18634 int x;
18635 struct glyph_row *row;
18636 enum glyph_row_area area;
18637 int start, end;
18638 enum draw_glyphs_face hl;
18639 int overlaps_p;
18640 {
18641 struct glyph_string *head, *tail;
18642 struct glyph_string *s;
18643 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18644 int last_x, area_width;
18645 int x_reached;
18646 int i, j;
18647 struct frame *f = XFRAME (WINDOW_FRAME (w));
18648 DECLARE_HDC (hdc);
18649
18650 ALLOCATE_HDC (hdc, f);
18651
18652 /* Let's rather be paranoid than getting a SEGV. */
18653 end = min (end, row->used[area]);
18654 start = max (0, start);
18655 start = min (end, start);
18656
18657 /* Translate X to frame coordinates. Set last_x to the right
18658 end of the drawing area. */
18659 if (row->full_width_p)
18660 {
18661 /* X is relative to the left edge of W, without scroll bars
18662 or fringes. */
18663 x += WINDOW_LEFT_EDGE_X (w);
18664 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18665 }
18666 else
18667 {
18668 int area_left = window_box_left (w, area);
18669 x += area_left;
18670 area_width = window_box_width (w, area);
18671 last_x = area_left + area_width;
18672 }
18673
18674 /* Build a doubly-linked list of glyph_string structures between
18675 head and tail from what we have to draw. Note that the macro
18676 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18677 the reason we use a separate variable `i'. */
18678 i = start;
18679 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18680 if (tail)
18681 x_reached = tail->x + tail->background_width;
18682 else
18683 x_reached = x;
18684
18685 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18686 the row, redraw some glyphs in front or following the glyph
18687 strings built above. */
18688 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18689 {
18690 int dummy_x = 0;
18691 struct glyph_string *h, *t;
18692
18693 /* Compute overhangs for all glyph strings. */
18694 if (rif->compute_glyph_string_overhangs)
18695 for (s = head; s; s = s->next)
18696 rif->compute_glyph_string_overhangs (s);
18697
18698 /* Prepend glyph strings for glyphs in front of the first glyph
18699 string that are overwritten because of the first glyph
18700 string's left overhang. The background of all strings
18701 prepended must be drawn because the first glyph string
18702 draws over it. */
18703 i = left_overwritten (head);
18704 if (i >= 0)
18705 {
18706 j = i;
18707 BUILD_GLYPH_STRINGS (j, start, h, t,
18708 DRAW_NORMAL_TEXT, dummy_x, last_x);
18709 start = i;
18710 compute_overhangs_and_x (t, head->x, 1);
18711 prepend_glyph_string_lists (&head, &tail, h, t);
18712 clip_head = head;
18713 }
18714
18715 /* Prepend glyph strings for glyphs in front of the first glyph
18716 string that overwrite that glyph string because of their
18717 right overhang. For these strings, only the foreground must
18718 be drawn, because it draws over the glyph string at `head'.
18719 The background must not be drawn because this would overwrite
18720 right overhangs of preceding glyphs for which no glyph
18721 strings exist. */
18722 i = left_overwriting (head);
18723 if (i >= 0)
18724 {
18725 clip_head = head;
18726 BUILD_GLYPH_STRINGS (i, start, h, t,
18727 DRAW_NORMAL_TEXT, dummy_x, last_x);
18728 for (s = h; s; s = s->next)
18729 s->background_filled_p = 1;
18730 compute_overhangs_and_x (t, head->x, 1);
18731 prepend_glyph_string_lists (&head, &tail, h, t);
18732 }
18733
18734 /* Append glyphs strings for glyphs following the last glyph
18735 string tail that are overwritten by tail. The background of
18736 these strings has to be drawn because tail's foreground draws
18737 over it. */
18738 i = right_overwritten (tail);
18739 if (i >= 0)
18740 {
18741 BUILD_GLYPH_STRINGS (end, i, h, t,
18742 DRAW_NORMAL_TEXT, x, last_x);
18743 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18744 append_glyph_string_lists (&head, &tail, h, t);
18745 clip_tail = tail;
18746 }
18747
18748 /* Append glyph strings for glyphs following the last glyph
18749 string tail that overwrite tail. The foreground of such
18750 glyphs has to be drawn because it writes into the background
18751 of tail. The background must not be drawn because it could
18752 paint over the foreground of following glyphs. */
18753 i = right_overwriting (tail);
18754 if (i >= 0)
18755 {
18756 clip_tail = tail;
18757 BUILD_GLYPH_STRINGS (end, i, h, t,
18758 DRAW_NORMAL_TEXT, x, last_x);
18759 for (s = h; s; s = s->next)
18760 s->background_filled_p = 1;
18761 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18762 append_glyph_string_lists (&head, &tail, h, t);
18763 }
18764 if (clip_head || clip_tail)
18765 for (s = head; s; s = s->next)
18766 {
18767 s->clip_head = clip_head;
18768 s->clip_tail = clip_tail;
18769 }
18770 }
18771
18772 /* Draw all strings. */
18773 for (s = head; s; s = s->next)
18774 rif->draw_glyph_string (s);
18775
18776 if (area == TEXT_AREA
18777 && !row->full_width_p
18778 /* When drawing overlapping rows, only the glyph strings'
18779 foreground is drawn, which doesn't erase a cursor
18780 completely. */
18781 && !overlaps_p)
18782 {
18783 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18784 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18785 : (tail ? tail->x + tail->background_width : x));
18786
18787 int text_left = window_box_left (w, TEXT_AREA);
18788 x0 -= text_left;
18789 x1 -= text_left;
18790
18791 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18792 row->y, MATRIX_ROW_BOTTOM_Y (row));
18793 }
18794
18795 /* Value is the x-position up to which drawn, relative to AREA of W.
18796 This doesn't include parts drawn because of overhangs. */
18797 if (row->full_width_p)
18798 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18799 else
18800 x_reached -= window_box_left (w, area);
18801
18802 RELEASE_HDC (hdc, f);
18803
18804 return x_reached;
18805 }
18806
18807 /* Expand row matrix if too narrow. Don't expand if area
18808 is not present. */
18809
18810 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18811 { \
18812 if (!fonts_changed_p \
18813 && (it->glyph_row->glyphs[area] \
18814 < it->glyph_row->glyphs[area + 1])) \
18815 { \
18816 it->w->ncols_scale_factor++; \
18817 fonts_changed_p = 1; \
18818 } \
18819 }
18820
18821 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18822 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18823
18824 static INLINE void
18825 append_glyph (it)
18826 struct it *it;
18827 {
18828 struct glyph *glyph;
18829 enum glyph_row_area area = it->area;
18830
18831 xassert (it->glyph_row);
18832 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18833
18834 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18835 if (glyph < it->glyph_row->glyphs[area + 1])
18836 {
18837 glyph->charpos = CHARPOS (it->position);
18838 glyph->object = it->object;
18839 glyph->pixel_width = it->pixel_width;
18840 glyph->ascent = it->ascent;
18841 glyph->descent = it->descent;
18842 glyph->voffset = it->voffset;
18843 glyph->type = CHAR_GLYPH;
18844 glyph->multibyte_p = it->multibyte_p;
18845 glyph->left_box_line_p = it->start_of_box_run_p;
18846 glyph->right_box_line_p = it->end_of_box_run_p;
18847 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18848 || it->phys_descent > it->descent);
18849 glyph->padding_p = 0;
18850 glyph->glyph_not_available_p = it->glyph_not_available_p;
18851 glyph->face_id = it->face_id;
18852 glyph->u.ch = it->char_to_display;
18853 glyph->slice = null_glyph_slice;
18854 glyph->font_type = FONT_TYPE_UNKNOWN;
18855 ++it->glyph_row->used[area];
18856 }
18857 else
18858 IT_EXPAND_MATRIX_WIDTH (it, area);
18859 }
18860
18861 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18862 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18863
18864 static INLINE void
18865 append_composite_glyph (it)
18866 struct it *it;
18867 {
18868 struct glyph *glyph;
18869 enum glyph_row_area area = it->area;
18870
18871 xassert (it->glyph_row);
18872
18873 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18874 if (glyph < it->glyph_row->glyphs[area + 1])
18875 {
18876 glyph->charpos = CHARPOS (it->position);
18877 glyph->object = it->object;
18878 glyph->pixel_width = it->pixel_width;
18879 glyph->ascent = it->ascent;
18880 glyph->descent = it->descent;
18881 glyph->voffset = it->voffset;
18882 glyph->type = COMPOSITE_GLYPH;
18883 glyph->multibyte_p = it->multibyte_p;
18884 glyph->left_box_line_p = it->start_of_box_run_p;
18885 glyph->right_box_line_p = it->end_of_box_run_p;
18886 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18887 || it->phys_descent > it->descent);
18888 glyph->padding_p = 0;
18889 glyph->glyph_not_available_p = 0;
18890 glyph->face_id = it->face_id;
18891 glyph->u.cmp_id = it->cmp_id;
18892 glyph->slice = null_glyph_slice;
18893 glyph->font_type = FONT_TYPE_UNKNOWN;
18894 ++it->glyph_row->used[area];
18895 }
18896 else
18897 IT_EXPAND_MATRIX_WIDTH (it, area);
18898 }
18899
18900
18901 /* Change IT->ascent and IT->height according to the setting of
18902 IT->voffset. */
18903
18904 static INLINE void
18905 take_vertical_position_into_account (it)
18906 struct it *it;
18907 {
18908 if (it->voffset)
18909 {
18910 if (it->voffset < 0)
18911 /* Increase the ascent so that we can display the text higher
18912 in the line. */
18913 it->ascent -= it->voffset;
18914 else
18915 /* Increase the descent so that we can display the text lower
18916 in the line. */
18917 it->descent += it->voffset;
18918 }
18919 }
18920
18921
18922 /* Produce glyphs/get display metrics for the image IT is loaded with.
18923 See the description of struct display_iterator in dispextern.h for
18924 an overview of struct display_iterator. */
18925
18926 static void
18927 produce_image_glyph (it)
18928 struct it *it;
18929 {
18930 struct image *img;
18931 struct face *face;
18932 int glyph_ascent;
18933 struct glyph_slice slice;
18934
18935 xassert (it->what == IT_IMAGE);
18936
18937 face = FACE_FROM_ID (it->f, it->face_id);
18938 xassert (face);
18939 /* Make sure X resources of the face is loaded. */
18940 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18941
18942 if (it->image_id < 0)
18943 {
18944 /* Fringe bitmap. */
18945 it->ascent = it->phys_ascent = 0;
18946 it->descent = it->phys_descent = 0;
18947 it->pixel_width = 0;
18948 it->nglyphs = 0;
18949 return;
18950 }
18951
18952 img = IMAGE_FROM_ID (it->f, it->image_id);
18953 xassert (img);
18954 /* Make sure X resources of the image is loaded. */
18955 prepare_image_for_display (it->f, img);
18956
18957 slice.x = slice.y = 0;
18958 slice.width = img->width;
18959 slice.height = img->height;
18960
18961 if (INTEGERP (it->slice.x))
18962 slice.x = XINT (it->slice.x);
18963 else if (FLOATP (it->slice.x))
18964 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18965
18966 if (INTEGERP (it->slice.y))
18967 slice.y = XINT (it->slice.y);
18968 else if (FLOATP (it->slice.y))
18969 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18970
18971 if (INTEGERP (it->slice.width))
18972 slice.width = XINT (it->slice.width);
18973 else if (FLOATP (it->slice.width))
18974 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18975
18976 if (INTEGERP (it->slice.height))
18977 slice.height = XINT (it->slice.height);
18978 else if (FLOATP (it->slice.height))
18979 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18980
18981 if (slice.x >= img->width)
18982 slice.x = img->width;
18983 if (slice.y >= img->height)
18984 slice.y = img->height;
18985 if (slice.x + slice.width >= img->width)
18986 slice.width = img->width - slice.x;
18987 if (slice.y + slice.height > img->height)
18988 slice.height = img->height - slice.y;
18989
18990 if (slice.width == 0 || slice.height == 0)
18991 return;
18992
18993 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18994
18995 it->descent = slice.height - glyph_ascent;
18996 if (slice.y == 0)
18997 it->descent += img->vmargin;
18998 if (slice.y + slice.height == img->height)
18999 it->descent += img->vmargin;
19000 it->phys_descent = it->descent;
19001
19002 it->pixel_width = slice.width;
19003 if (slice.x == 0)
19004 it->pixel_width += img->hmargin;
19005 if (slice.x + slice.width == img->width)
19006 it->pixel_width += img->hmargin;
19007
19008 /* It's quite possible for images to have an ascent greater than
19009 their height, so don't get confused in that case. */
19010 if (it->descent < 0)
19011 it->descent = 0;
19012
19013 #if 0 /* this breaks image tiling */
19014 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19015 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19016 if (face_ascent > it->ascent)
19017 it->ascent = it->phys_ascent = face_ascent;
19018 #endif
19019
19020 it->nglyphs = 1;
19021
19022 if (face->box != FACE_NO_BOX)
19023 {
19024 if (face->box_line_width > 0)
19025 {
19026 if (slice.y == 0)
19027 it->ascent += face->box_line_width;
19028 if (slice.y + slice.height == img->height)
19029 it->descent += face->box_line_width;
19030 }
19031
19032 if (it->start_of_box_run_p && slice.x == 0)
19033 it->pixel_width += abs (face->box_line_width);
19034 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19035 it->pixel_width += abs (face->box_line_width);
19036 }
19037
19038 take_vertical_position_into_account (it);
19039
19040 if (it->glyph_row)
19041 {
19042 struct glyph *glyph;
19043 enum glyph_row_area area = it->area;
19044
19045 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19046 if (glyph < it->glyph_row->glyphs[area + 1])
19047 {
19048 glyph->charpos = CHARPOS (it->position);
19049 glyph->object = it->object;
19050 glyph->pixel_width = it->pixel_width;
19051 glyph->ascent = glyph_ascent;
19052 glyph->descent = it->descent;
19053 glyph->voffset = it->voffset;
19054 glyph->type = IMAGE_GLYPH;
19055 glyph->multibyte_p = it->multibyte_p;
19056 glyph->left_box_line_p = it->start_of_box_run_p;
19057 glyph->right_box_line_p = it->end_of_box_run_p;
19058 glyph->overlaps_vertically_p = 0;
19059 glyph->padding_p = 0;
19060 glyph->glyph_not_available_p = 0;
19061 glyph->face_id = it->face_id;
19062 glyph->u.img_id = img->id;
19063 glyph->slice = slice;
19064 glyph->font_type = FONT_TYPE_UNKNOWN;
19065 ++it->glyph_row->used[area];
19066 }
19067 else
19068 IT_EXPAND_MATRIX_WIDTH (it, area);
19069 }
19070 }
19071
19072
19073 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19074 of the glyph, WIDTH and HEIGHT are the width and height of the
19075 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19076
19077 static void
19078 append_stretch_glyph (it, object, width, height, ascent)
19079 struct it *it;
19080 Lisp_Object object;
19081 int width, height;
19082 int ascent;
19083 {
19084 struct glyph *glyph;
19085 enum glyph_row_area area = it->area;
19086
19087 xassert (ascent >= 0 && ascent <= height);
19088
19089 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19090 if (glyph < it->glyph_row->glyphs[area + 1])
19091 {
19092 glyph->charpos = CHARPOS (it->position);
19093 glyph->object = object;
19094 glyph->pixel_width = width;
19095 glyph->ascent = ascent;
19096 glyph->descent = height - ascent;
19097 glyph->voffset = it->voffset;
19098 glyph->type = STRETCH_GLYPH;
19099 glyph->multibyte_p = it->multibyte_p;
19100 glyph->left_box_line_p = it->start_of_box_run_p;
19101 glyph->right_box_line_p = it->end_of_box_run_p;
19102 glyph->overlaps_vertically_p = 0;
19103 glyph->padding_p = 0;
19104 glyph->glyph_not_available_p = 0;
19105 glyph->face_id = it->face_id;
19106 glyph->u.stretch.ascent = ascent;
19107 glyph->u.stretch.height = height;
19108 glyph->slice = null_glyph_slice;
19109 glyph->font_type = FONT_TYPE_UNKNOWN;
19110 ++it->glyph_row->used[area];
19111 }
19112 else
19113 IT_EXPAND_MATRIX_WIDTH (it, area);
19114 }
19115
19116
19117 /* Produce a stretch glyph for iterator IT. IT->object is the value
19118 of the glyph property displayed. The value must be a list
19119 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19120 being recognized:
19121
19122 1. `:width WIDTH' specifies that the space should be WIDTH *
19123 canonical char width wide. WIDTH may be an integer or floating
19124 point number.
19125
19126 2. `:relative-width FACTOR' specifies that the width of the stretch
19127 should be computed from the width of the first character having the
19128 `glyph' property, and should be FACTOR times that width.
19129
19130 3. `:align-to HPOS' specifies that the space should be wide enough
19131 to reach HPOS, a value in canonical character units.
19132
19133 Exactly one of the above pairs must be present.
19134
19135 4. `:height HEIGHT' specifies that the height of the stretch produced
19136 should be HEIGHT, measured in canonical character units.
19137
19138 5. `:relative-height FACTOR' specifies that the height of the
19139 stretch should be FACTOR times the height of the characters having
19140 the glyph property.
19141
19142 Either none or exactly one of 4 or 5 must be present.
19143
19144 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19145 of the stretch should be used for the ascent of the stretch.
19146 ASCENT must be in the range 0 <= ASCENT <= 100. */
19147
19148 static void
19149 produce_stretch_glyph (it)
19150 struct it *it;
19151 {
19152 /* (space :width WIDTH :height HEIGHT ...) */
19153 Lisp_Object prop, plist;
19154 int width = 0, height = 0, align_to = -1;
19155 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19156 int ascent = 0;
19157 double tem;
19158 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19159 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19160
19161 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19162
19163 /* List should start with `space'. */
19164 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19165 plist = XCDR (it->object);
19166
19167 /* Compute the width of the stretch. */
19168 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19169 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19170 {
19171 /* Absolute width `:width WIDTH' specified and valid. */
19172 zero_width_ok_p = 1;
19173 width = (int)tem;
19174 }
19175 else if (prop = Fplist_get (plist, QCrelative_width),
19176 NUMVAL (prop) > 0)
19177 {
19178 /* Relative width `:relative-width FACTOR' specified and valid.
19179 Compute the width of the characters having the `glyph'
19180 property. */
19181 struct it it2;
19182 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19183
19184 it2 = *it;
19185 if (it->multibyte_p)
19186 {
19187 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19188 - IT_BYTEPOS (*it));
19189 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19190 }
19191 else
19192 it2.c = *p, it2.len = 1;
19193
19194 it2.glyph_row = NULL;
19195 it2.what = IT_CHARACTER;
19196 x_produce_glyphs (&it2);
19197 width = NUMVAL (prop) * it2.pixel_width;
19198 }
19199 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19200 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19201 {
19202 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19203 align_to = (align_to < 0
19204 ? 0
19205 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19206 else if (align_to < 0)
19207 align_to = window_box_left_offset (it->w, TEXT_AREA);
19208 width = max (0, (int)tem + align_to - it->current_x);
19209 zero_width_ok_p = 1;
19210 }
19211 else
19212 /* Nothing specified -> width defaults to canonical char width. */
19213 width = FRAME_COLUMN_WIDTH (it->f);
19214
19215 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19216 width = 1;
19217
19218 /* Compute height. */
19219 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19220 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19221 {
19222 height = (int)tem;
19223 zero_height_ok_p = 1;
19224 }
19225 else if (prop = Fplist_get (plist, QCrelative_height),
19226 NUMVAL (prop) > 0)
19227 height = FONT_HEIGHT (font) * NUMVAL (prop);
19228 else
19229 height = FONT_HEIGHT (font);
19230
19231 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19232 height = 1;
19233
19234 /* Compute percentage of height used for ascent. If
19235 `:ascent ASCENT' is present and valid, use that. Otherwise,
19236 derive the ascent from the font in use. */
19237 if (prop = Fplist_get (plist, QCascent),
19238 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19239 ascent = height * NUMVAL (prop) / 100.0;
19240 else if (!NILP (prop)
19241 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19242 ascent = min (max (0, (int)tem), height);
19243 else
19244 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19245
19246 if (width > 0 && height > 0 && it->glyph_row)
19247 {
19248 Lisp_Object object = it->stack[it->sp - 1].string;
19249 if (!STRINGP (object))
19250 object = it->w->buffer;
19251 append_stretch_glyph (it, object, width, height, ascent);
19252 }
19253
19254 it->pixel_width = width;
19255 it->ascent = it->phys_ascent = ascent;
19256 it->descent = it->phys_descent = height - it->ascent;
19257 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19258
19259 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19260 {
19261 if (face->box_line_width > 0)
19262 {
19263 it->ascent += face->box_line_width;
19264 it->descent += face->box_line_width;
19265 }
19266
19267 if (it->start_of_box_run_p)
19268 it->pixel_width += abs (face->box_line_width);
19269 if (it->end_of_box_run_p)
19270 it->pixel_width += abs (face->box_line_width);
19271 }
19272
19273 take_vertical_position_into_account (it);
19274 }
19275
19276 /* Get line-height and line-spacing property at point.
19277 If line-height has format (HEIGHT TOTAL), return TOTAL
19278 in TOTAL_HEIGHT. */
19279
19280 static Lisp_Object
19281 get_line_height_property (it, prop)
19282 struct it *it;
19283 Lisp_Object prop;
19284 {
19285 Lisp_Object position;
19286
19287 if (STRINGP (it->object))
19288 position = make_number (IT_STRING_CHARPOS (*it));
19289 else if (BUFFERP (it->object))
19290 position = make_number (IT_CHARPOS (*it));
19291 else
19292 return Qnil;
19293
19294 return Fget_char_property (position, prop, it->object);
19295 }
19296
19297 /* Calculate line-height and line-spacing properties.
19298 An integer value specifies explicit pixel value.
19299 A float value specifies relative value to current face height.
19300 A cons (float . face-name) specifies relative value to
19301 height of specified face font.
19302
19303 Returns height in pixels, or nil. */
19304
19305
19306 static Lisp_Object
19307 calc_line_height_property (it, val, font, boff, override)
19308 struct it *it;
19309 Lisp_Object val;
19310 XFontStruct *font;
19311 int boff, override;
19312 {
19313 Lisp_Object face_name = Qnil;
19314 int ascent, descent, height;
19315
19316 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19317 return val;
19318
19319 if (CONSP (val))
19320 {
19321 face_name = XCAR (val);
19322 val = XCDR (val);
19323 if (!NUMBERP (val))
19324 val = make_number (1);
19325 if (NILP (face_name))
19326 {
19327 height = it->ascent + it->descent;
19328 goto scale;
19329 }
19330 }
19331
19332 if (NILP (face_name))
19333 {
19334 font = FRAME_FONT (it->f);
19335 boff = FRAME_BASELINE_OFFSET (it->f);
19336 }
19337 else if (EQ (face_name, Qt))
19338 {
19339 override = 0;
19340 }
19341 else
19342 {
19343 int face_id;
19344 struct face *face;
19345 struct font_info *font_info;
19346
19347 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19348 if (face_id < 0)
19349 return make_number (-1);
19350
19351 face = FACE_FROM_ID (it->f, face_id);
19352 font = face->font;
19353 if (font == NULL)
19354 return make_number (-1);
19355
19356 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19357 boff = font_info->baseline_offset;
19358 if (font_info->vertical_centering)
19359 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19360 }
19361
19362 ascent = FONT_BASE (font) + boff;
19363 descent = FONT_DESCENT (font) - boff;
19364
19365 if (override)
19366 {
19367 it->override_ascent = ascent;
19368 it->override_descent = descent;
19369 it->override_boff = boff;
19370 }
19371
19372 height = ascent + descent;
19373
19374 scale:
19375 if (FLOATP (val))
19376 height = (int)(XFLOAT_DATA (val) * height);
19377 else if (INTEGERP (val))
19378 height *= XINT (val);
19379
19380 return make_number (height);
19381 }
19382
19383
19384 /* RIF:
19385 Produce glyphs/get display metrics for the display element IT is
19386 loaded with. See the description of struct display_iterator in
19387 dispextern.h for an overview of struct display_iterator. */
19388
19389 void
19390 x_produce_glyphs (it)
19391 struct it *it;
19392 {
19393 int extra_line_spacing = it->extra_line_spacing;
19394
19395 it->glyph_not_available_p = 0;
19396
19397 if (it->what == IT_CHARACTER)
19398 {
19399 XChar2b char2b;
19400 XFontStruct *font;
19401 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19402 XCharStruct *pcm;
19403 int font_not_found_p;
19404 struct font_info *font_info;
19405 int boff; /* baseline offset */
19406 /* We may change it->multibyte_p upon unibyte<->multibyte
19407 conversion. So, save the current value now and restore it
19408 later.
19409
19410 Note: It seems that we don't have to record multibyte_p in
19411 struct glyph because the character code itself tells if or
19412 not the character is multibyte. Thus, in the future, we must
19413 consider eliminating the field `multibyte_p' in the struct
19414 glyph. */
19415 int saved_multibyte_p = it->multibyte_p;
19416
19417 /* Maybe translate single-byte characters to multibyte, or the
19418 other way. */
19419 it->char_to_display = it->c;
19420 if (!ASCII_BYTE_P (it->c))
19421 {
19422 if (unibyte_display_via_language_environment
19423 && SINGLE_BYTE_CHAR_P (it->c)
19424 && (it->c >= 0240
19425 || !NILP (Vnonascii_translation_table)))
19426 {
19427 it->char_to_display = unibyte_char_to_multibyte (it->c);
19428 it->multibyte_p = 1;
19429 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19430 face = FACE_FROM_ID (it->f, it->face_id);
19431 }
19432 else if (!SINGLE_BYTE_CHAR_P (it->c)
19433 && !it->multibyte_p)
19434 {
19435 it->multibyte_p = 1;
19436 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19437 face = FACE_FROM_ID (it->f, it->face_id);
19438 }
19439 }
19440
19441 /* Get font to use. Encode IT->char_to_display. */
19442 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19443 &char2b, it->multibyte_p, 0);
19444 font = face->font;
19445
19446 /* When no suitable font found, use the default font. */
19447 font_not_found_p = font == NULL;
19448 if (font_not_found_p)
19449 {
19450 font = FRAME_FONT (it->f);
19451 boff = FRAME_BASELINE_OFFSET (it->f);
19452 font_info = NULL;
19453 }
19454 else
19455 {
19456 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19457 boff = font_info->baseline_offset;
19458 if (font_info->vertical_centering)
19459 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19460 }
19461
19462 if (it->char_to_display >= ' '
19463 && (!it->multibyte_p || it->char_to_display < 128))
19464 {
19465 /* Either unibyte or ASCII. */
19466 int stretched_p;
19467
19468 it->nglyphs = 1;
19469
19470 pcm = rif->per_char_metric (font, &char2b,
19471 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19472
19473 if (it->override_ascent >= 0)
19474 {
19475 it->ascent = it->override_ascent;
19476 it->descent = it->override_descent;
19477 boff = it->override_boff;
19478 }
19479 else
19480 {
19481 it->ascent = FONT_BASE (font) + boff;
19482 it->descent = FONT_DESCENT (font) - boff;
19483 }
19484
19485 if (pcm)
19486 {
19487 it->phys_ascent = pcm->ascent + boff;
19488 it->phys_descent = pcm->descent - boff;
19489 it->pixel_width = pcm->width;
19490 }
19491 else
19492 {
19493 it->glyph_not_available_p = 1;
19494 it->phys_ascent = it->ascent;
19495 it->phys_descent = it->descent;
19496 it->pixel_width = FONT_WIDTH (font);
19497 }
19498
19499 if (it->constrain_row_ascent_descent_p)
19500 {
19501 if (it->descent > it->max_descent)
19502 {
19503 it->ascent += it->descent - it->max_descent;
19504 it->descent = it->max_descent;
19505 }
19506 if (it->ascent > it->max_ascent)
19507 {
19508 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19509 it->ascent = it->max_ascent;
19510 }
19511 it->phys_ascent = min (it->phys_ascent, it->ascent);
19512 it->phys_descent = min (it->phys_descent, it->descent);
19513 extra_line_spacing = 0;
19514 }
19515
19516 /* If this is a space inside a region of text with
19517 `space-width' property, change its width. */
19518 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19519 if (stretched_p)
19520 it->pixel_width *= XFLOATINT (it->space_width);
19521
19522 /* If face has a box, add the box thickness to the character
19523 height. If character has a box line to the left and/or
19524 right, add the box line width to the character's width. */
19525 if (face->box != FACE_NO_BOX)
19526 {
19527 int thick = face->box_line_width;
19528
19529 if (thick > 0)
19530 {
19531 it->ascent += thick;
19532 it->descent += thick;
19533 }
19534 else
19535 thick = -thick;
19536
19537 if (it->start_of_box_run_p)
19538 it->pixel_width += thick;
19539 if (it->end_of_box_run_p)
19540 it->pixel_width += thick;
19541 }
19542
19543 /* If face has an overline, add the height of the overline
19544 (1 pixel) and a 1 pixel margin to the character height. */
19545 if (face->overline_p)
19546 it->ascent += 2;
19547
19548 if (it->constrain_row_ascent_descent_p)
19549 {
19550 if (it->ascent > it->max_ascent)
19551 it->ascent = it->max_ascent;
19552 if (it->descent > it->max_descent)
19553 it->descent = it->max_descent;
19554 }
19555
19556 take_vertical_position_into_account (it);
19557
19558 /* If we have to actually produce glyphs, do it. */
19559 if (it->glyph_row)
19560 {
19561 if (stretched_p)
19562 {
19563 /* Translate a space with a `space-width' property
19564 into a stretch glyph. */
19565 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19566 / FONT_HEIGHT (font));
19567 append_stretch_glyph (it, it->object, it->pixel_width,
19568 it->ascent + it->descent, ascent);
19569 }
19570 else
19571 append_glyph (it);
19572
19573 /* If characters with lbearing or rbearing are displayed
19574 in this line, record that fact in a flag of the
19575 glyph row. This is used to optimize X output code. */
19576 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19577 it->glyph_row->contains_overlapping_glyphs_p = 1;
19578 }
19579 }
19580 else if (it->char_to_display == '\n')
19581 {
19582 /* A newline has no width but we need the height of the line.
19583 But if previous part of the line set a height, don't
19584 increase that height */
19585
19586 Lisp_Object height;
19587 Lisp_Object total_height = Qnil;
19588
19589 it->override_ascent = -1;
19590 it->pixel_width = 0;
19591 it->nglyphs = 0;
19592
19593 height = get_line_height_property(it, Qline_height);
19594 /* Split (line-height total-height) list */
19595 if (CONSP (height)
19596 && CONSP (XCDR (height))
19597 && NILP (XCDR (XCDR (height))))
19598 {
19599 total_height = XCAR (XCDR (height));
19600 height = XCAR (height);
19601 }
19602 height = calc_line_height_property(it, height, font, boff, 1);
19603
19604 if (it->override_ascent >= 0)
19605 {
19606 it->ascent = it->override_ascent;
19607 it->descent = it->override_descent;
19608 boff = it->override_boff;
19609 }
19610 else
19611 {
19612 it->ascent = FONT_BASE (font) + boff;
19613 it->descent = FONT_DESCENT (font) - boff;
19614 }
19615
19616 if (EQ (height, Qt))
19617 {
19618 if (it->descent > it->max_descent)
19619 {
19620 it->ascent += it->descent - it->max_descent;
19621 it->descent = it->max_descent;
19622 }
19623 if (it->ascent > it->max_ascent)
19624 {
19625 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19626 it->ascent = it->max_ascent;
19627 }
19628 it->phys_ascent = min (it->phys_ascent, it->ascent);
19629 it->phys_descent = min (it->phys_descent, it->descent);
19630 it->constrain_row_ascent_descent_p = 1;
19631 extra_line_spacing = 0;
19632 }
19633 else
19634 {
19635 Lisp_Object spacing;
19636
19637 it->phys_ascent = it->ascent;
19638 it->phys_descent = it->descent;
19639
19640 if ((it->max_ascent > 0 || it->max_descent > 0)
19641 && face->box != FACE_NO_BOX
19642 && face->box_line_width > 0)
19643 {
19644 it->ascent += face->box_line_width;
19645 it->descent += face->box_line_width;
19646 }
19647 if (!NILP (height)
19648 && XINT (height) > it->ascent + it->descent)
19649 it->ascent = XINT (height) - it->descent;
19650
19651 if (!NILP (total_height))
19652 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19653 else
19654 {
19655 spacing = get_line_height_property(it, Qline_spacing);
19656 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19657 }
19658 if (INTEGERP (spacing))
19659 {
19660 extra_line_spacing = XINT (spacing);
19661 if (!NILP (total_height))
19662 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19663 }
19664 }
19665 }
19666 else if (it->char_to_display == '\t')
19667 {
19668 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19669 int x = it->current_x + it->continuation_lines_width;
19670 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19671
19672 /* If the distance from the current position to the next tab
19673 stop is less than a space character width, use the
19674 tab stop after that. */
19675 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19676 next_tab_x += tab_width;
19677
19678 it->pixel_width = next_tab_x - x;
19679 it->nglyphs = 1;
19680 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19681 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19682
19683 if (it->glyph_row)
19684 {
19685 append_stretch_glyph (it, it->object, it->pixel_width,
19686 it->ascent + it->descent, it->ascent);
19687 }
19688 }
19689 else
19690 {
19691 /* A multi-byte character. Assume that the display width of the
19692 character is the width of the character multiplied by the
19693 width of the font. */
19694
19695 /* If we found a font, this font should give us the right
19696 metrics. If we didn't find a font, use the frame's
19697 default font and calculate the width of the character
19698 from the charset width; this is what old redisplay code
19699 did. */
19700
19701 pcm = rif->per_char_metric (font, &char2b,
19702 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19703
19704 if (font_not_found_p || !pcm)
19705 {
19706 int charset = CHAR_CHARSET (it->char_to_display);
19707
19708 it->glyph_not_available_p = 1;
19709 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19710 * CHARSET_WIDTH (charset));
19711 it->phys_ascent = FONT_BASE (font) + boff;
19712 it->phys_descent = FONT_DESCENT (font) - boff;
19713 }
19714 else
19715 {
19716 it->pixel_width = pcm->width;
19717 it->phys_ascent = pcm->ascent + boff;
19718 it->phys_descent = pcm->descent - boff;
19719 if (it->glyph_row
19720 && (pcm->lbearing < 0
19721 || pcm->rbearing > pcm->width))
19722 it->glyph_row->contains_overlapping_glyphs_p = 1;
19723 }
19724 it->nglyphs = 1;
19725 it->ascent = FONT_BASE (font) + boff;
19726 it->descent = FONT_DESCENT (font) - boff;
19727 if (face->box != FACE_NO_BOX)
19728 {
19729 int thick = face->box_line_width;
19730
19731 if (thick > 0)
19732 {
19733 it->ascent += thick;
19734 it->descent += thick;
19735 }
19736 else
19737 thick = - thick;
19738
19739 if (it->start_of_box_run_p)
19740 it->pixel_width += thick;
19741 if (it->end_of_box_run_p)
19742 it->pixel_width += thick;
19743 }
19744
19745 /* If face has an overline, add the height of the overline
19746 (1 pixel) and a 1 pixel margin to the character height. */
19747 if (face->overline_p)
19748 it->ascent += 2;
19749
19750 take_vertical_position_into_account (it);
19751
19752 if (it->glyph_row)
19753 append_glyph (it);
19754 }
19755 it->multibyte_p = saved_multibyte_p;
19756 }
19757 else if (it->what == IT_COMPOSITION)
19758 {
19759 /* Note: A composition is represented as one glyph in the
19760 glyph matrix. There are no padding glyphs. */
19761 XChar2b char2b;
19762 XFontStruct *font;
19763 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19764 XCharStruct *pcm;
19765 int font_not_found_p;
19766 struct font_info *font_info;
19767 int boff; /* baseline offset */
19768 struct composition *cmp = composition_table[it->cmp_id];
19769
19770 /* Maybe translate single-byte characters to multibyte. */
19771 it->char_to_display = it->c;
19772 if (unibyte_display_via_language_environment
19773 && SINGLE_BYTE_CHAR_P (it->c)
19774 && (it->c >= 0240
19775 || (it->c >= 0200
19776 && !NILP (Vnonascii_translation_table))))
19777 {
19778 it->char_to_display = unibyte_char_to_multibyte (it->c);
19779 }
19780
19781 /* Get face and font to use. Encode IT->char_to_display. */
19782 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19783 face = FACE_FROM_ID (it->f, it->face_id);
19784 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19785 &char2b, it->multibyte_p, 0);
19786 font = face->font;
19787
19788 /* When no suitable font found, use the default font. */
19789 font_not_found_p = font == NULL;
19790 if (font_not_found_p)
19791 {
19792 font = FRAME_FONT (it->f);
19793 boff = FRAME_BASELINE_OFFSET (it->f);
19794 font_info = NULL;
19795 }
19796 else
19797 {
19798 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19799 boff = font_info->baseline_offset;
19800 if (font_info->vertical_centering)
19801 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19802 }
19803
19804 /* There are no padding glyphs, so there is only one glyph to
19805 produce for the composition. Important is that pixel_width,
19806 ascent and descent are the values of what is drawn by
19807 draw_glyphs (i.e. the values of the overall glyphs composed). */
19808 it->nglyphs = 1;
19809
19810 /* If we have not yet calculated pixel size data of glyphs of
19811 the composition for the current face font, calculate them
19812 now. Theoretically, we have to check all fonts for the
19813 glyphs, but that requires much time and memory space. So,
19814 here we check only the font of the first glyph. This leads
19815 to incorrect display very rarely, and C-l (recenter) can
19816 correct the display anyway. */
19817 if (cmp->font != (void *) font)
19818 {
19819 /* Ascent and descent of the font of the first character of
19820 this composition (adjusted by baseline offset). Ascent
19821 and descent of overall glyphs should not be less than
19822 them respectively. */
19823 int font_ascent = FONT_BASE (font) + boff;
19824 int font_descent = FONT_DESCENT (font) - boff;
19825 /* Bounding box of the overall glyphs. */
19826 int leftmost, rightmost, lowest, highest;
19827 int i, width, ascent, descent;
19828
19829 cmp->font = (void *) font;
19830
19831 /* Initialize the bounding box. */
19832 if (font_info
19833 && (pcm = rif->per_char_metric (font, &char2b,
19834 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19835 {
19836 width = pcm->width;
19837 ascent = pcm->ascent;
19838 descent = pcm->descent;
19839 }
19840 else
19841 {
19842 width = FONT_WIDTH (font);
19843 ascent = FONT_BASE (font);
19844 descent = FONT_DESCENT (font);
19845 }
19846
19847 rightmost = width;
19848 lowest = - descent + boff;
19849 highest = ascent + boff;
19850 leftmost = 0;
19851
19852 if (font_info
19853 && font_info->default_ascent
19854 && CHAR_TABLE_P (Vuse_default_ascent)
19855 && !NILP (Faref (Vuse_default_ascent,
19856 make_number (it->char_to_display))))
19857 highest = font_info->default_ascent + boff;
19858
19859 /* Draw the first glyph at the normal position. It may be
19860 shifted to right later if some other glyphs are drawn at
19861 the left. */
19862 cmp->offsets[0] = 0;
19863 cmp->offsets[1] = boff;
19864
19865 /* Set cmp->offsets for the remaining glyphs. */
19866 for (i = 1; i < cmp->glyph_len; i++)
19867 {
19868 int left, right, btm, top;
19869 int ch = COMPOSITION_GLYPH (cmp, i);
19870 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19871
19872 face = FACE_FROM_ID (it->f, face_id);
19873 get_char_face_and_encoding (it->f, ch, face->id,
19874 &char2b, it->multibyte_p, 0);
19875 font = face->font;
19876 if (font == NULL)
19877 {
19878 font = FRAME_FONT (it->f);
19879 boff = FRAME_BASELINE_OFFSET (it->f);
19880 font_info = NULL;
19881 }
19882 else
19883 {
19884 font_info
19885 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19886 boff = font_info->baseline_offset;
19887 if (font_info->vertical_centering)
19888 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19889 }
19890
19891 if (font_info
19892 && (pcm = rif->per_char_metric (font, &char2b,
19893 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19894 {
19895 width = pcm->width;
19896 ascent = pcm->ascent;
19897 descent = pcm->descent;
19898 }
19899 else
19900 {
19901 width = FONT_WIDTH (font);
19902 ascent = 1;
19903 descent = 0;
19904 }
19905
19906 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19907 {
19908 /* Relative composition with or without
19909 alternate chars. */
19910 left = (leftmost + rightmost - width) / 2;
19911 btm = - descent + boff;
19912 if (font_info && font_info->relative_compose
19913 && (! CHAR_TABLE_P (Vignore_relative_composition)
19914 || NILP (Faref (Vignore_relative_composition,
19915 make_number (ch)))))
19916 {
19917
19918 if (- descent >= font_info->relative_compose)
19919 /* One extra pixel between two glyphs. */
19920 btm = highest + 1;
19921 else if (ascent <= 0)
19922 /* One extra pixel between two glyphs. */
19923 btm = lowest - 1 - ascent - descent;
19924 }
19925 }
19926 else
19927 {
19928 /* A composition rule is specified by an integer
19929 value that encodes global and new reference
19930 points (GREF and NREF). GREF and NREF are
19931 specified by numbers as below:
19932
19933 0---1---2 -- ascent
19934 | |
19935 | |
19936 | |
19937 9--10--11 -- center
19938 | |
19939 ---3---4---5--- baseline
19940 | |
19941 6---7---8 -- descent
19942 */
19943 int rule = COMPOSITION_RULE (cmp, i);
19944 int gref, nref, grefx, grefy, nrefx, nrefy;
19945
19946 COMPOSITION_DECODE_RULE (rule, gref, nref);
19947 grefx = gref % 3, nrefx = nref % 3;
19948 grefy = gref / 3, nrefy = nref / 3;
19949
19950 left = (leftmost
19951 + grefx * (rightmost - leftmost) / 2
19952 - nrefx * width / 2);
19953 btm = ((grefy == 0 ? highest
19954 : grefy == 1 ? 0
19955 : grefy == 2 ? lowest
19956 : (highest + lowest) / 2)
19957 - (nrefy == 0 ? ascent + descent
19958 : nrefy == 1 ? descent - boff
19959 : nrefy == 2 ? 0
19960 : (ascent + descent) / 2));
19961 }
19962
19963 cmp->offsets[i * 2] = left;
19964 cmp->offsets[i * 2 + 1] = btm + descent;
19965
19966 /* Update the bounding box of the overall glyphs. */
19967 right = left + width;
19968 top = btm + descent + ascent;
19969 if (left < leftmost)
19970 leftmost = left;
19971 if (right > rightmost)
19972 rightmost = right;
19973 if (top > highest)
19974 highest = top;
19975 if (btm < lowest)
19976 lowest = btm;
19977 }
19978
19979 /* If there are glyphs whose x-offsets are negative,
19980 shift all glyphs to the right and make all x-offsets
19981 non-negative. */
19982 if (leftmost < 0)
19983 {
19984 for (i = 0; i < cmp->glyph_len; i++)
19985 cmp->offsets[i * 2] -= leftmost;
19986 rightmost -= leftmost;
19987 }
19988
19989 cmp->pixel_width = rightmost;
19990 cmp->ascent = highest;
19991 cmp->descent = - lowest;
19992 if (cmp->ascent < font_ascent)
19993 cmp->ascent = font_ascent;
19994 if (cmp->descent < font_descent)
19995 cmp->descent = font_descent;
19996 }
19997
19998 it->pixel_width = cmp->pixel_width;
19999 it->ascent = it->phys_ascent = cmp->ascent;
20000 it->descent = it->phys_descent = cmp->descent;
20001
20002 if (face->box != FACE_NO_BOX)
20003 {
20004 int thick = face->box_line_width;
20005
20006 if (thick > 0)
20007 {
20008 it->ascent += thick;
20009 it->descent += thick;
20010 }
20011 else
20012 thick = - thick;
20013
20014 if (it->start_of_box_run_p)
20015 it->pixel_width += thick;
20016 if (it->end_of_box_run_p)
20017 it->pixel_width += thick;
20018 }
20019
20020 /* If face has an overline, add the height of the overline
20021 (1 pixel) and a 1 pixel margin to the character height. */
20022 if (face->overline_p)
20023 it->ascent += 2;
20024
20025 take_vertical_position_into_account (it);
20026
20027 if (it->glyph_row)
20028 append_composite_glyph (it);
20029 }
20030 else if (it->what == IT_IMAGE)
20031 produce_image_glyph (it);
20032 else if (it->what == IT_STRETCH)
20033 produce_stretch_glyph (it);
20034
20035 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20036 because this isn't true for images with `:ascent 100'. */
20037 xassert (it->ascent >= 0 && it->descent >= 0);
20038 if (it->area == TEXT_AREA)
20039 it->current_x += it->pixel_width;
20040
20041 if (extra_line_spacing > 0)
20042 {
20043 it->descent += extra_line_spacing;
20044 if (extra_line_spacing > it->max_extra_line_spacing)
20045 it->max_extra_line_spacing = extra_line_spacing;
20046 }
20047
20048 it->max_ascent = max (it->max_ascent, it->ascent);
20049 it->max_descent = max (it->max_descent, it->descent);
20050 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20051 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20052 }
20053
20054 /* EXPORT for RIF:
20055 Output LEN glyphs starting at START at the nominal cursor position.
20056 Advance the nominal cursor over the text. The global variable
20057 updated_window contains the window being updated, updated_row is
20058 the glyph row being updated, and updated_area is the area of that
20059 row being updated. */
20060
20061 void
20062 x_write_glyphs (start, len)
20063 struct glyph *start;
20064 int len;
20065 {
20066 int x, hpos;
20067
20068 xassert (updated_window && updated_row);
20069 BLOCK_INPUT;
20070
20071 /* Write glyphs. */
20072
20073 hpos = start - updated_row->glyphs[updated_area];
20074 x = draw_glyphs (updated_window, output_cursor.x,
20075 updated_row, updated_area,
20076 hpos, hpos + len,
20077 DRAW_NORMAL_TEXT, 0);
20078
20079 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20080 if (updated_area == TEXT_AREA
20081 && updated_window->phys_cursor_on_p
20082 && updated_window->phys_cursor.vpos == output_cursor.vpos
20083 && updated_window->phys_cursor.hpos >= hpos
20084 && updated_window->phys_cursor.hpos < hpos + len)
20085 updated_window->phys_cursor_on_p = 0;
20086
20087 UNBLOCK_INPUT;
20088
20089 /* Advance the output cursor. */
20090 output_cursor.hpos += len;
20091 output_cursor.x = x;
20092 }
20093
20094
20095 /* EXPORT for RIF:
20096 Insert LEN glyphs from START at the nominal cursor position. */
20097
20098 void
20099 x_insert_glyphs (start, len)
20100 struct glyph *start;
20101 int len;
20102 {
20103 struct frame *f;
20104 struct window *w;
20105 int line_height, shift_by_width, shifted_region_width;
20106 struct glyph_row *row;
20107 struct glyph *glyph;
20108 int frame_x, frame_y, hpos;
20109
20110 xassert (updated_window && updated_row);
20111 BLOCK_INPUT;
20112 w = updated_window;
20113 f = XFRAME (WINDOW_FRAME (w));
20114
20115 /* Get the height of the line we are in. */
20116 row = updated_row;
20117 line_height = row->height;
20118
20119 /* Get the width of the glyphs to insert. */
20120 shift_by_width = 0;
20121 for (glyph = start; glyph < start + len; ++glyph)
20122 shift_by_width += glyph->pixel_width;
20123
20124 /* Get the width of the region to shift right. */
20125 shifted_region_width = (window_box_width (w, updated_area)
20126 - output_cursor.x
20127 - shift_by_width);
20128
20129 /* Shift right. */
20130 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20131 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20132
20133 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20134 line_height, shift_by_width);
20135
20136 /* Write the glyphs. */
20137 hpos = start - row->glyphs[updated_area];
20138 draw_glyphs (w, output_cursor.x, row, updated_area,
20139 hpos, hpos + len,
20140 DRAW_NORMAL_TEXT, 0);
20141
20142 /* Advance the output cursor. */
20143 output_cursor.hpos += len;
20144 output_cursor.x += shift_by_width;
20145 UNBLOCK_INPUT;
20146 }
20147
20148
20149 /* EXPORT for RIF:
20150 Erase the current text line from the nominal cursor position
20151 (inclusive) to pixel column TO_X (exclusive). The idea is that
20152 everything from TO_X onward is already erased.
20153
20154 TO_X is a pixel position relative to updated_area of
20155 updated_window. TO_X == -1 means clear to the end of this area. */
20156
20157 void
20158 x_clear_end_of_line (to_x)
20159 int to_x;
20160 {
20161 struct frame *f;
20162 struct window *w = updated_window;
20163 int max_x, min_y, max_y;
20164 int from_x, from_y, to_y;
20165
20166 xassert (updated_window && updated_row);
20167 f = XFRAME (w->frame);
20168
20169 if (updated_row->full_width_p)
20170 max_x = WINDOW_TOTAL_WIDTH (w);
20171 else
20172 max_x = window_box_width (w, updated_area);
20173 max_y = window_text_bottom_y (w);
20174
20175 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20176 of window. For TO_X > 0, truncate to end of drawing area. */
20177 if (to_x == 0)
20178 return;
20179 else if (to_x < 0)
20180 to_x = max_x;
20181 else
20182 to_x = min (to_x, max_x);
20183
20184 to_y = min (max_y, output_cursor.y + updated_row->height);
20185
20186 /* Notice if the cursor will be cleared by this operation. */
20187 if (!updated_row->full_width_p)
20188 notice_overwritten_cursor (w, updated_area,
20189 output_cursor.x, -1,
20190 updated_row->y,
20191 MATRIX_ROW_BOTTOM_Y (updated_row));
20192
20193 from_x = output_cursor.x;
20194
20195 /* Translate to frame coordinates. */
20196 if (updated_row->full_width_p)
20197 {
20198 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20199 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20200 }
20201 else
20202 {
20203 int area_left = window_box_left (w, updated_area);
20204 from_x += area_left;
20205 to_x += area_left;
20206 }
20207
20208 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20209 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20210 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20211
20212 /* Prevent inadvertently clearing to end of the X window. */
20213 if (to_x > from_x && to_y > from_y)
20214 {
20215 BLOCK_INPUT;
20216 rif->clear_frame_area (f, from_x, from_y,
20217 to_x - from_x, to_y - from_y);
20218 UNBLOCK_INPUT;
20219 }
20220 }
20221
20222 #endif /* HAVE_WINDOW_SYSTEM */
20223
20224
20225 \f
20226 /***********************************************************************
20227 Cursor types
20228 ***********************************************************************/
20229
20230 /* Value is the internal representation of the specified cursor type
20231 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20232 of the bar cursor. */
20233
20234 static enum text_cursor_kinds
20235 get_specified_cursor_type (arg, width)
20236 Lisp_Object arg;
20237 int *width;
20238 {
20239 enum text_cursor_kinds type;
20240
20241 if (NILP (arg))
20242 return NO_CURSOR;
20243
20244 if (EQ (arg, Qbox))
20245 return FILLED_BOX_CURSOR;
20246
20247 if (EQ (arg, Qhollow))
20248 return HOLLOW_BOX_CURSOR;
20249
20250 if (EQ (arg, Qbar))
20251 {
20252 *width = 2;
20253 return BAR_CURSOR;
20254 }
20255
20256 if (CONSP (arg)
20257 && EQ (XCAR (arg), Qbar)
20258 && INTEGERP (XCDR (arg))
20259 && XINT (XCDR (arg)) >= 0)
20260 {
20261 *width = XINT (XCDR (arg));
20262 return BAR_CURSOR;
20263 }
20264
20265 if (EQ (arg, Qhbar))
20266 {
20267 *width = 2;
20268 return HBAR_CURSOR;
20269 }
20270
20271 if (CONSP (arg)
20272 && EQ (XCAR (arg), Qhbar)
20273 && INTEGERP (XCDR (arg))
20274 && XINT (XCDR (arg)) >= 0)
20275 {
20276 *width = XINT (XCDR (arg));
20277 return HBAR_CURSOR;
20278 }
20279
20280 /* Treat anything unknown as "hollow box cursor".
20281 It was bad to signal an error; people have trouble fixing
20282 .Xdefaults with Emacs, when it has something bad in it. */
20283 type = HOLLOW_BOX_CURSOR;
20284
20285 return type;
20286 }
20287
20288 /* Set the default cursor types for specified frame. */
20289 void
20290 set_frame_cursor_types (f, arg)
20291 struct frame *f;
20292 Lisp_Object arg;
20293 {
20294 int width;
20295 Lisp_Object tem;
20296
20297 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20298 FRAME_CURSOR_WIDTH (f) = width;
20299
20300 /* By default, set up the blink-off state depending on the on-state. */
20301
20302 tem = Fassoc (arg, Vblink_cursor_alist);
20303 if (!NILP (tem))
20304 {
20305 FRAME_BLINK_OFF_CURSOR (f)
20306 = get_specified_cursor_type (XCDR (tem), &width);
20307 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20308 }
20309 else
20310 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20311 }
20312
20313
20314 /* Return the cursor we want to be displayed in window W. Return
20315 width of bar/hbar cursor through WIDTH arg. Return with
20316 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20317 (i.e. if the `system caret' should track this cursor).
20318
20319 In a mini-buffer window, we want the cursor only to appear if we
20320 are reading input from this window. For the selected window, we
20321 want the cursor type given by the frame parameter or buffer local
20322 setting of cursor-type. If explicitly marked off, draw no cursor.
20323 In all other cases, we want a hollow box cursor. */
20324
20325 static enum text_cursor_kinds
20326 get_window_cursor_type (w, glyph, width, active_cursor)
20327 struct window *w;
20328 struct glyph *glyph;
20329 int *width;
20330 int *active_cursor;
20331 {
20332 struct frame *f = XFRAME (w->frame);
20333 struct buffer *b = XBUFFER (w->buffer);
20334 int cursor_type = DEFAULT_CURSOR;
20335 Lisp_Object alt_cursor;
20336 int non_selected = 0;
20337
20338 *active_cursor = 1;
20339
20340 /* Echo area */
20341 if (cursor_in_echo_area
20342 && FRAME_HAS_MINIBUF_P (f)
20343 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20344 {
20345 if (w == XWINDOW (echo_area_window))
20346 {
20347 *width = FRAME_CURSOR_WIDTH (f);
20348 return FRAME_DESIRED_CURSOR (f);
20349 }
20350
20351 *active_cursor = 0;
20352 non_selected = 1;
20353 }
20354
20355 /* Nonselected window or nonselected frame. */
20356 else if (w != XWINDOW (f->selected_window)
20357 #ifdef HAVE_WINDOW_SYSTEM
20358 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20359 #endif
20360 )
20361 {
20362 *active_cursor = 0;
20363
20364 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20365 return NO_CURSOR;
20366
20367 non_selected = 1;
20368 }
20369
20370 /* Never display a cursor in a window in which cursor-type is nil. */
20371 if (NILP (b->cursor_type))
20372 return NO_CURSOR;
20373
20374 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20375 if (non_selected)
20376 {
20377 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20378 return get_specified_cursor_type (alt_cursor, width);
20379 }
20380
20381 /* Get the normal cursor type for this window. */
20382 if (EQ (b->cursor_type, Qt))
20383 {
20384 cursor_type = FRAME_DESIRED_CURSOR (f);
20385 *width = FRAME_CURSOR_WIDTH (f);
20386 }
20387 else
20388 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20389
20390 /* Use normal cursor if not blinked off. */
20391 if (!w->cursor_off_p)
20392 {
20393 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20394 if (cursor_type == FILLED_BOX_CURSOR)
20395 cursor_type = HOLLOW_BOX_CURSOR;
20396 }
20397 return cursor_type;
20398 }
20399
20400 /* Cursor is blinked off, so determine how to "toggle" it. */
20401
20402 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20403 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20404 return get_specified_cursor_type (XCDR (alt_cursor), width);
20405
20406 /* Then see if frame has specified a specific blink off cursor type. */
20407 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20408 {
20409 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20410 return FRAME_BLINK_OFF_CURSOR (f);
20411 }
20412
20413 #if 0
20414 /* Some people liked having a permanently visible blinking cursor,
20415 while others had very strong opinions against it. So it was
20416 decided to remove it. KFS 2003-09-03 */
20417
20418 /* Finally perform built-in cursor blinking:
20419 filled box <-> hollow box
20420 wide [h]bar <-> narrow [h]bar
20421 narrow [h]bar <-> no cursor
20422 other type <-> no cursor */
20423
20424 if (cursor_type == FILLED_BOX_CURSOR)
20425 return HOLLOW_BOX_CURSOR;
20426
20427 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20428 {
20429 *width = 1;
20430 return cursor_type;
20431 }
20432 #endif
20433
20434 return NO_CURSOR;
20435 }
20436
20437
20438 #ifdef HAVE_WINDOW_SYSTEM
20439
20440 /* Notice when the text cursor of window W has been completely
20441 overwritten by a drawing operation that outputs glyphs in AREA
20442 starting at X0 and ending at X1 in the line starting at Y0 and
20443 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20444 the rest of the line after X0 has been written. Y coordinates
20445 are window-relative. */
20446
20447 static void
20448 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20449 struct window *w;
20450 enum glyph_row_area area;
20451 int x0, y0, x1, y1;
20452 {
20453 int cx0, cx1, cy0, cy1;
20454 struct glyph_row *row;
20455
20456 if (!w->phys_cursor_on_p)
20457 return;
20458 if (area != TEXT_AREA)
20459 return;
20460
20461 if (w->phys_cursor.vpos < 0
20462 || w->phys_cursor.vpos >= w->current_matrix->nrows
20463 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20464 !(row->enabled_p && row->displays_text_p)))
20465 return;
20466
20467 if (row->cursor_in_fringe_p)
20468 {
20469 row->cursor_in_fringe_p = 0;
20470 draw_fringe_bitmap (w, row, 0);
20471 w->phys_cursor_on_p = 0;
20472 return;
20473 }
20474
20475 cx0 = w->phys_cursor.x;
20476 cx1 = cx0 + w->phys_cursor_width;
20477 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20478 return;
20479
20480 /* The cursor image will be completely removed from the
20481 screen if the output area intersects the cursor area in
20482 y-direction. When we draw in [y0 y1[, and some part of
20483 the cursor is at y < y0, that part must have been drawn
20484 before. When scrolling, the cursor is erased before
20485 actually scrolling, so we don't come here. When not
20486 scrolling, the rows above the old cursor row must have
20487 changed, and in this case these rows must have written
20488 over the cursor image.
20489
20490 Likewise if part of the cursor is below y1, with the
20491 exception of the cursor being in the first blank row at
20492 the buffer and window end because update_text_area
20493 doesn't draw that row. (Except when it does, but
20494 that's handled in update_text_area.) */
20495
20496 cy0 = w->phys_cursor.y;
20497 cy1 = cy0 + w->phys_cursor_height;
20498 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20499 return;
20500
20501 w->phys_cursor_on_p = 0;
20502 }
20503
20504 #endif /* HAVE_WINDOW_SYSTEM */
20505
20506 \f
20507 /************************************************************************
20508 Mouse Face
20509 ************************************************************************/
20510
20511 #ifdef HAVE_WINDOW_SYSTEM
20512
20513 /* EXPORT for RIF:
20514 Fix the display of area AREA of overlapping row ROW in window W. */
20515
20516 void
20517 x_fix_overlapping_area (w, row, area)
20518 struct window *w;
20519 struct glyph_row *row;
20520 enum glyph_row_area area;
20521 {
20522 int i, x;
20523
20524 BLOCK_INPUT;
20525
20526 x = 0;
20527 for (i = 0; i < row->used[area];)
20528 {
20529 if (row->glyphs[area][i].overlaps_vertically_p)
20530 {
20531 int start = i, start_x = x;
20532
20533 do
20534 {
20535 x += row->glyphs[area][i].pixel_width;
20536 ++i;
20537 }
20538 while (i < row->used[area]
20539 && row->glyphs[area][i].overlaps_vertically_p);
20540
20541 draw_glyphs (w, start_x, row, area,
20542 start, i,
20543 DRAW_NORMAL_TEXT, 1);
20544 }
20545 else
20546 {
20547 x += row->glyphs[area][i].pixel_width;
20548 ++i;
20549 }
20550 }
20551
20552 UNBLOCK_INPUT;
20553 }
20554
20555
20556 /* EXPORT:
20557 Draw the cursor glyph of window W in glyph row ROW. See the
20558 comment of draw_glyphs for the meaning of HL. */
20559
20560 void
20561 draw_phys_cursor_glyph (w, row, hl)
20562 struct window *w;
20563 struct glyph_row *row;
20564 enum draw_glyphs_face hl;
20565 {
20566 /* If cursor hpos is out of bounds, don't draw garbage. This can
20567 happen in mini-buffer windows when switching between echo area
20568 glyphs and mini-buffer. */
20569 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20570 {
20571 int on_p = w->phys_cursor_on_p;
20572 int x1;
20573 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20574 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20575 hl, 0);
20576 w->phys_cursor_on_p = on_p;
20577
20578 if (hl == DRAW_CURSOR)
20579 w->phys_cursor_width = x1 - w->phys_cursor.x;
20580 /* When we erase the cursor, and ROW is overlapped by other
20581 rows, make sure that these overlapping parts of other rows
20582 are redrawn. */
20583 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20584 {
20585 if (row > w->current_matrix->rows
20586 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20587 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20588
20589 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20590 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20591 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20592 }
20593 }
20594 }
20595
20596
20597 /* EXPORT:
20598 Erase the image of a cursor of window W from the screen. */
20599
20600 void
20601 erase_phys_cursor (w)
20602 struct window *w;
20603 {
20604 struct frame *f = XFRAME (w->frame);
20605 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20606 int hpos = w->phys_cursor.hpos;
20607 int vpos = w->phys_cursor.vpos;
20608 int mouse_face_here_p = 0;
20609 struct glyph_matrix *active_glyphs = w->current_matrix;
20610 struct glyph_row *cursor_row;
20611 struct glyph *cursor_glyph;
20612 enum draw_glyphs_face hl;
20613
20614 /* No cursor displayed or row invalidated => nothing to do on the
20615 screen. */
20616 if (w->phys_cursor_type == NO_CURSOR)
20617 goto mark_cursor_off;
20618
20619 /* VPOS >= active_glyphs->nrows means that window has been resized.
20620 Don't bother to erase the cursor. */
20621 if (vpos >= active_glyphs->nrows)
20622 goto mark_cursor_off;
20623
20624 /* If row containing cursor is marked invalid, there is nothing we
20625 can do. */
20626 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20627 if (!cursor_row->enabled_p)
20628 goto mark_cursor_off;
20629
20630 /* If line spacing is > 0, old cursor may only be partially visible in
20631 window after split-window. So adjust visible height. */
20632 cursor_row->visible_height = min (cursor_row->visible_height,
20633 window_text_bottom_y (w) - cursor_row->y);
20634
20635 /* If row is completely invisible, don't attempt to delete a cursor which
20636 isn't there. This can happen if cursor is at top of a window, and
20637 we switch to a buffer with a header line in that window. */
20638 if (cursor_row->visible_height <= 0)
20639 goto mark_cursor_off;
20640
20641 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20642 if (cursor_row->cursor_in_fringe_p)
20643 {
20644 cursor_row->cursor_in_fringe_p = 0;
20645 draw_fringe_bitmap (w, cursor_row, 0);
20646 goto mark_cursor_off;
20647 }
20648
20649 /* This can happen when the new row is shorter than the old one.
20650 In this case, either draw_glyphs or clear_end_of_line
20651 should have cleared the cursor. Note that we wouldn't be
20652 able to erase the cursor in this case because we don't have a
20653 cursor glyph at hand. */
20654 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20655 goto mark_cursor_off;
20656
20657 /* If the cursor is in the mouse face area, redisplay that when
20658 we clear the cursor. */
20659 if (! NILP (dpyinfo->mouse_face_window)
20660 && w == XWINDOW (dpyinfo->mouse_face_window)
20661 && (vpos > dpyinfo->mouse_face_beg_row
20662 || (vpos == dpyinfo->mouse_face_beg_row
20663 && hpos >= dpyinfo->mouse_face_beg_col))
20664 && (vpos < dpyinfo->mouse_face_end_row
20665 || (vpos == dpyinfo->mouse_face_end_row
20666 && hpos < dpyinfo->mouse_face_end_col))
20667 /* Don't redraw the cursor's spot in mouse face if it is at the
20668 end of a line (on a newline). The cursor appears there, but
20669 mouse highlighting does not. */
20670 && cursor_row->used[TEXT_AREA] > hpos)
20671 mouse_face_here_p = 1;
20672
20673 /* Maybe clear the display under the cursor. */
20674 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20675 {
20676 int x, y;
20677 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20678 int width;
20679
20680 cursor_glyph = get_phys_cursor_glyph (w);
20681 if (cursor_glyph == NULL)
20682 goto mark_cursor_off;
20683
20684 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20685 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20686 width = min (cursor_glyph->pixel_width,
20687 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20688
20689 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20690 }
20691
20692 /* Erase the cursor by redrawing the character underneath it. */
20693 if (mouse_face_here_p)
20694 hl = DRAW_MOUSE_FACE;
20695 else
20696 hl = DRAW_NORMAL_TEXT;
20697 draw_phys_cursor_glyph (w, cursor_row, hl);
20698
20699 mark_cursor_off:
20700 w->phys_cursor_on_p = 0;
20701 w->phys_cursor_type = NO_CURSOR;
20702 }
20703
20704
20705 /* EXPORT:
20706 Display or clear cursor of window W. If ON is zero, clear the
20707 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20708 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20709
20710 void
20711 display_and_set_cursor (w, on, hpos, vpos, x, y)
20712 struct window *w;
20713 int on, hpos, vpos, x, y;
20714 {
20715 struct frame *f = XFRAME (w->frame);
20716 int new_cursor_type;
20717 int new_cursor_width;
20718 int active_cursor;
20719 struct glyph_row *glyph_row;
20720 struct glyph *glyph;
20721
20722 /* This is pointless on invisible frames, and dangerous on garbaged
20723 windows and frames; in the latter case, the frame or window may
20724 be in the midst of changing its size, and x and y may be off the
20725 window. */
20726 if (! FRAME_VISIBLE_P (f)
20727 || FRAME_GARBAGED_P (f)
20728 || vpos >= w->current_matrix->nrows
20729 || hpos >= w->current_matrix->matrix_w)
20730 return;
20731
20732 /* If cursor is off and we want it off, return quickly. */
20733 if (!on && !w->phys_cursor_on_p)
20734 return;
20735
20736 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20737 /* If cursor row is not enabled, we don't really know where to
20738 display the cursor. */
20739 if (!glyph_row->enabled_p)
20740 {
20741 w->phys_cursor_on_p = 0;
20742 return;
20743 }
20744
20745 glyph = NULL;
20746 if (!glyph_row->exact_window_width_line_p
20747 || hpos < glyph_row->used[TEXT_AREA])
20748 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20749
20750 xassert (interrupt_input_blocked);
20751
20752 /* Set new_cursor_type to the cursor we want to be displayed. */
20753 new_cursor_type = get_window_cursor_type (w, glyph,
20754 &new_cursor_width, &active_cursor);
20755
20756 /* If cursor is currently being shown and we don't want it to be or
20757 it is in the wrong place, or the cursor type is not what we want,
20758 erase it. */
20759 if (w->phys_cursor_on_p
20760 && (!on
20761 || w->phys_cursor.x != x
20762 || w->phys_cursor.y != y
20763 || new_cursor_type != w->phys_cursor_type
20764 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20765 && new_cursor_width != w->phys_cursor_width)))
20766 erase_phys_cursor (w);
20767
20768 /* Don't check phys_cursor_on_p here because that flag is only set
20769 to zero in some cases where we know that the cursor has been
20770 completely erased, to avoid the extra work of erasing the cursor
20771 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20772 still not be visible, or it has only been partly erased. */
20773 if (on)
20774 {
20775 w->phys_cursor_ascent = glyph_row->ascent;
20776 w->phys_cursor_height = glyph_row->height;
20777
20778 /* Set phys_cursor_.* before x_draw_.* is called because some
20779 of them may need the information. */
20780 w->phys_cursor.x = x;
20781 w->phys_cursor.y = glyph_row->y;
20782 w->phys_cursor.hpos = hpos;
20783 w->phys_cursor.vpos = vpos;
20784 }
20785
20786 rif->draw_window_cursor (w, glyph_row, x, y,
20787 new_cursor_type, new_cursor_width,
20788 on, active_cursor);
20789 }
20790
20791
20792 /* Switch the display of W's cursor on or off, according to the value
20793 of ON. */
20794
20795 static void
20796 update_window_cursor (w, on)
20797 struct window *w;
20798 int on;
20799 {
20800 /* Don't update cursor in windows whose frame is in the process
20801 of being deleted. */
20802 if (w->current_matrix)
20803 {
20804 BLOCK_INPUT;
20805 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20806 w->phys_cursor.x, w->phys_cursor.y);
20807 UNBLOCK_INPUT;
20808 }
20809 }
20810
20811
20812 /* Call update_window_cursor with parameter ON_P on all leaf windows
20813 in the window tree rooted at W. */
20814
20815 static void
20816 update_cursor_in_window_tree (w, on_p)
20817 struct window *w;
20818 int on_p;
20819 {
20820 while (w)
20821 {
20822 if (!NILP (w->hchild))
20823 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20824 else if (!NILP (w->vchild))
20825 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20826 else
20827 update_window_cursor (w, on_p);
20828
20829 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20830 }
20831 }
20832
20833
20834 /* EXPORT:
20835 Display the cursor on window W, or clear it, according to ON_P.
20836 Don't change the cursor's position. */
20837
20838 void
20839 x_update_cursor (f, on_p)
20840 struct frame *f;
20841 int on_p;
20842 {
20843 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20844 }
20845
20846
20847 /* EXPORT:
20848 Clear the cursor of window W to background color, and mark the
20849 cursor as not shown. This is used when the text where the cursor
20850 is is about to be rewritten. */
20851
20852 void
20853 x_clear_cursor (w)
20854 struct window *w;
20855 {
20856 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20857 update_window_cursor (w, 0);
20858 }
20859
20860
20861 /* EXPORT:
20862 Display the active region described by mouse_face_* according to DRAW. */
20863
20864 void
20865 show_mouse_face (dpyinfo, draw)
20866 Display_Info *dpyinfo;
20867 enum draw_glyphs_face draw;
20868 {
20869 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20870 struct frame *f = XFRAME (WINDOW_FRAME (w));
20871
20872 if (/* If window is in the process of being destroyed, don't bother
20873 to do anything. */
20874 w->current_matrix != NULL
20875 /* Don't update mouse highlight if hidden */
20876 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20877 /* Recognize when we are called to operate on rows that don't exist
20878 anymore. This can happen when a window is split. */
20879 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20880 {
20881 int phys_cursor_on_p = w->phys_cursor_on_p;
20882 struct glyph_row *row, *first, *last;
20883
20884 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20885 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20886
20887 for (row = first; row <= last && row->enabled_p; ++row)
20888 {
20889 int start_hpos, end_hpos, start_x;
20890
20891 /* For all but the first row, the highlight starts at column 0. */
20892 if (row == first)
20893 {
20894 start_hpos = dpyinfo->mouse_face_beg_col;
20895 start_x = dpyinfo->mouse_face_beg_x;
20896 }
20897 else
20898 {
20899 start_hpos = 0;
20900 start_x = 0;
20901 }
20902
20903 if (row == last)
20904 end_hpos = dpyinfo->mouse_face_end_col;
20905 else
20906 end_hpos = row->used[TEXT_AREA];
20907
20908 if (end_hpos > start_hpos)
20909 {
20910 draw_glyphs (w, start_x, row, TEXT_AREA,
20911 start_hpos, end_hpos,
20912 draw, 0);
20913
20914 row->mouse_face_p
20915 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20916 }
20917 }
20918
20919 /* When we've written over the cursor, arrange for it to
20920 be displayed again. */
20921 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20922 {
20923 BLOCK_INPUT;
20924 display_and_set_cursor (w, 1,
20925 w->phys_cursor.hpos, w->phys_cursor.vpos,
20926 w->phys_cursor.x, w->phys_cursor.y);
20927 UNBLOCK_INPUT;
20928 }
20929 }
20930
20931 /* Change the mouse cursor. */
20932 if (draw == DRAW_NORMAL_TEXT)
20933 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20934 else if (draw == DRAW_MOUSE_FACE)
20935 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20936 else
20937 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20938 }
20939
20940 /* EXPORT:
20941 Clear out the mouse-highlighted active region.
20942 Redraw it un-highlighted first. Value is non-zero if mouse
20943 face was actually drawn unhighlighted. */
20944
20945 int
20946 clear_mouse_face (dpyinfo)
20947 Display_Info *dpyinfo;
20948 {
20949 int cleared = 0;
20950
20951 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20952 {
20953 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20954 cleared = 1;
20955 }
20956
20957 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20958 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20959 dpyinfo->mouse_face_window = Qnil;
20960 dpyinfo->mouse_face_overlay = Qnil;
20961 return cleared;
20962 }
20963
20964
20965 /* EXPORT:
20966 Non-zero if physical cursor of window W is within mouse face. */
20967
20968 int
20969 cursor_in_mouse_face_p (w)
20970 struct window *w;
20971 {
20972 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20973 int in_mouse_face = 0;
20974
20975 if (WINDOWP (dpyinfo->mouse_face_window)
20976 && XWINDOW (dpyinfo->mouse_face_window) == w)
20977 {
20978 int hpos = w->phys_cursor.hpos;
20979 int vpos = w->phys_cursor.vpos;
20980
20981 if (vpos >= dpyinfo->mouse_face_beg_row
20982 && vpos <= dpyinfo->mouse_face_end_row
20983 && (vpos > dpyinfo->mouse_face_beg_row
20984 || hpos >= dpyinfo->mouse_face_beg_col)
20985 && (vpos < dpyinfo->mouse_face_end_row
20986 || hpos < dpyinfo->mouse_face_end_col
20987 || dpyinfo->mouse_face_past_end))
20988 in_mouse_face = 1;
20989 }
20990
20991 return in_mouse_face;
20992 }
20993
20994
20995
20996 \f
20997 /* Find the glyph matrix position of buffer position CHARPOS in window
20998 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20999 current glyphs must be up to date. If CHARPOS is above window
21000 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21001 of last line in W. In the row containing CHARPOS, stop before glyphs
21002 having STOP as object. */
21003
21004 #if 1 /* This is a version of fast_find_position that's more correct
21005 in the presence of hscrolling, for example. I didn't install
21006 it right away because the problem fixed is minor, it failed
21007 in 20.x as well, and I think it's too risky to install
21008 so near the release of 21.1. 2001-09-25 gerd. */
21009
21010 static int
21011 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21012 struct window *w;
21013 int charpos;
21014 int *hpos, *vpos, *x, *y;
21015 Lisp_Object stop;
21016 {
21017 struct glyph_row *row, *first;
21018 struct glyph *glyph, *end;
21019 int past_end = 0;
21020
21021 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21022 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21023 {
21024 *x = first->x;
21025 *y = first->y;
21026 *hpos = 0;
21027 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21028 return 1;
21029 }
21030
21031 row = row_containing_pos (w, charpos, first, NULL, 0);
21032 if (row == NULL)
21033 {
21034 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21035 past_end = 1;
21036 }
21037
21038 /* If whole rows or last part of a row came from a display overlay,
21039 row_containing_pos will skip over such rows because their end pos
21040 equals the start pos of the overlay or interval.
21041
21042 Move back if we have a STOP object and previous row's
21043 end glyph came from STOP. */
21044 if (!NILP (stop))
21045 {
21046 struct glyph_row *prev;
21047 while ((prev = row - 1, prev >= first)
21048 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21049 && prev->used[TEXT_AREA] > 0)
21050 {
21051 struct glyph *beg = prev->glyphs[TEXT_AREA];
21052 glyph = beg + prev->used[TEXT_AREA];
21053 while (--glyph >= beg
21054 && INTEGERP (glyph->object));
21055 if (glyph < beg
21056 || !EQ (stop, glyph->object))
21057 break;
21058 row = prev;
21059 }
21060 }
21061
21062 *x = row->x;
21063 *y = row->y;
21064 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21065
21066 glyph = row->glyphs[TEXT_AREA];
21067 end = glyph + row->used[TEXT_AREA];
21068
21069 /* Skip over glyphs not having an object at the start of the row.
21070 These are special glyphs like truncation marks on terminal
21071 frames. */
21072 if (row->displays_text_p)
21073 while (glyph < end
21074 && INTEGERP (glyph->object)
21075 && !EQ (stop, glyph->object)
21076 && glyph->charpos < 0)
21077 {
21078 *x += glyph->pixel_width;
21079 ++glyph;
21080 }
21081
21082 while (glyph < end
21083 && !INTEGERP (glyph->object)
21084 && !EQ (stop, glyph->object)
21085 && (!BUFFERP (glyph->object)
21086 || glyph->charpos < charpos))
21087 {
21088 *x += glyph->pixel_width;
21089 ++glyph;
21090 }
21091
21092 *hpos = glyph - row->glyphs[TEXT_AREA];
21093 return !past_end;
21094 }
21095
21096 #else /* not 1 */
21097
21098 static int
21099 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21100 struct window *w;
21101 int pos;
21102 int *hpos, *vpos, *x, *y;
21103 Lisp_Object stop;
21104 {
21105 int i;
21106 int lastcol;
21107 int maybe_next_line_p = 0;
21108 int line_start_position;
21109 int yb = window_text_bottom_y (w);
21110 struct glyph_row *row, *best_row;
21111 int row_vpos, best_row_vpos;
21112 int current_x;
21113
21114 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21115 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21116
21117 while (row->y < yb)
21118 {
21119 if (row->used[TEXT_AREA])
21120 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21121 else
21122 line_start_position = 0;
21123
21124 if (line_start_position > pos)
21125 break;
21126 /* If the position sought is the end of the buffer,
21127 don't include the blank lines at the bottom of the window. */
21128 else if (line_start_position == pos
21129 && pos == BUF_ZV (XBUFFER (w->buffer)))
21130 {
21131 maybe_next_line_p = 1;
21132 break;
21133 }
21134 else if (line_start_position > 0)
21135 {
21136 best_row = row;
21137 best_row_vpos = row_vpos;
21138 }
21139
21140 if (row->y + row->height >= yb)
21141 break;
21142
21143 ++row;
21144 ++row_vpos;
21145 }
21146
21147 /* Find the right column within BEST_ROW. */
21148 lastcol = 0;
21149 current_x = best_row->x;
21150 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21151 {
21152 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21153 int charpos = glyph->charpos;
21154
21155 if (BUFFERP (glyph->object))
21156 {
21157 if (charpos == pos)
21158 {
21159 *hpos = i;
21160 *vpos = best_row_vpos;
21161 *x = current_x;
21162 *y = best_row->y;
21163 return 1;
21164 }
21165 else if (charpos > pos)
21166 break;
21167 }
21168 else if (EQ (glyph->object, stop))
21169 break;
21170
21171 if (charpos > 0)
21172 lastcol = i;
21173 current_x += glyph->pixel_width;
21174 }
21175
21176 /* If we're looking for the end of the buffer,
21177 and we didn't find it in the line we scanned,
21178 use the start of the following line. */
21179 if (maybe_next_line_p)
21180 {
21181 ++best_row;
21182 ++best_row_vpos;
21183 lastcol = 0;
21184 current_x = best_row->x;
21185 }
21186
21187 *vpos = best_row_vpos;
21188 *hpos = lastcol + 1;
21189 *x = current_x;
21190 *y = best_row->y;
21191 return 0;
21192 }
21193
21194 #endif /* not 1 */
21195
21196
21197 /* Find the position of the glyph for position POS in OBJECT in
21198 window W's current matrix, and return in *X, *Y the pixel
21199 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21200
21201 RIGHT_P non-zero means return the position of the right edge of the
21202 glyph, RIGHT_P zero means return the left edge position.
21203
21204 If no glyph for POS exists in the matrix, return the position of
21205 the glyph with the next smaller position that is in the matrix, if
21206 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21207 exists in the matrix, return the position of the glyph with the
21208 next larger position in OBJECT.
21209
21210 Value is non-zero if a glyph was found. */
21211
21212 static int
21213 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21214 struct window *w;
21215 int pos;
21216 Lisp_Object object;
21217 int *hpos, *vpos, *x, *y;
21218 int right_p;
21219 {
21220 int yb = window_text_bottom_y (w);
21221 struct glyph_row *r;
21222 struct glyph *best_glyph = NULL;
21223 struct glyph_row *best_row = NULL;
21224 int best_x = 0;
21225
21226 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21227 r->enabled_p && r->y < yb;
21228 ++r)
21229 {
21230 struct glyph *g = r->glyphs[TEXT_AREA];
21231 struct glyph *e = g + r->used[TEXT_AREA];
21232 int gx;
21233
21234 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21235 if (EQ (g->object, object))
21236 {
21237 if (g->charpos == pos)
21238 {
21239 best_glyph = g;
21240 best_x = gx;
21241 best_row = r;
21242 goto found;
21243 }
21244 else if (best_glyph == NULL
21245 || ((abs (g->charpos - pos)
21246 < abs (best_glyph->charpos - pos))
21247 && (right_p
21248 ? g->charpos < pos
21249 : g->charpos > pos)))
21250 {
21251 best_glyph = g;
21252 best_x = gx;
21253 best_row = r;
21254 }
21255 }
21256 }
21257
21258 found:
21259
21260 if (best_glyph)
21261 {
21262 *x = best_x;
21263 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21264
21265 if (right_p)
21266 {
21267 *x += best_glyph->pixel_width;
21268 ++*hpos;
21269 }
21270
21271 *y = best_row->y;
21272 *vpos = best_row - w->current_matrix->rows;
21273 }
21274
21275 return best_glyph != NULL;
21276 }
21277
21278
21279 /* See if position X, Y is within a hot-spot of an image. */
21280
21281 static int
21282 on_hot_spot_p (hot_spot, x, y)
21283 Lisp_Object hot_spot;
21284 int x, y;
21285 {
21286 if (!CONSP (hot_spot))
21287 return 0;
21288
21289 if (EQ (XCAR (hot_spot), Qrect))
21290 {
21291 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21292 Lisp_Object rect = XCDR (hot_spot);
21293 Lisp_Object tem;
21294 if (!CONSP (rect))
21295 return 0;
21296 if (!CONSP (XCAR (rect)))
21297 return 0;
21298 if (!CONSP (XCDR (rect)))
21299 return 0;
21300 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21301 return 0;
21302 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21303 return 0;
21304 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21305 return 0;
21306 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21307 return 0;
21308 return 1;
21309 }
21310 else if (EQ (XCAR (hot_spot), Qcircle))
21311 {
21312 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21313 Lisp_Object circ = XCDR (hot_spot);
21314 Lisp_Object lr, lx0, ly0;
21315 if (CONSP (circ)
21316 && CONSP (XCAR (circ))
21317 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21318 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21319 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21320 {
21321 double r = XFLOATINT (lr);
21322 double dx = XINT (lx0) - x;
21323 double dy = XINT (ly0) - y;
21324 return (dx * dx + dy * dy <= r * r);
21325 }
21326 }
21327 else if (EQ (XCAR (hot_spot), Qpoly))
21328 {
21329 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21330 if (VECTORP (XCDR (hot_spot)))
21331 {
21332 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21333 Lisp_Object *poly = v->contents;
21334 int n = v->size;
21335 int i;
21336 int inside = 0;
21337 Lisp_Object lx, ly;
21338 int x0, y0;
21339
21340 /* Need an even number of coordinates, and at least 3 edges. */
21341 if (n < 6 || n & 1)
21342 return 0;
21343
21344 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21345 If count is odd, we are inside polygon. Pixels on edges
21346 may or may not be included depending on actual geometry of the
21347 polygon. */
21348 if ((lx = poly[n-2], !INTEGERP (lx))
21349 || (ly = poly[n-1], !INTEGERP (lx)))
21350 return 0;
21351 x0 = XINT (lx), y0 = XINT (ly);
21352 for (i = 0; i < n; i += 2)
21353 {
21354 int x1 = x0, y1 = y0;
21355 if ((lx = poly[i], !INTEGERP (lx))
21356 || (ly = poly[i+1], !INTEGERP (ly)))
21357 return 0;
21358 x0 = XINT (lx), y0 = XINT (ly);
21359
21360 /* Does this segment cross the X line? */
21361 if (x0 >= x)
21362 {
21363 if (x1 >= x)
21364 continue;
21365 }
21366 else if (x1 < x)
21367 continue;
21368 if (y > y0 && y > y1)
21369 continue;
21370 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21371 inside = !inside;
21372 }
21373 return inside;
21374 }
21375 }
21376 /* If we don't understand the format, pretend we're not in the hot-spot. */
21377 return 0;
21378 }
21379
21380 Lisp_Object
21381 find_hot_spot (map, x, y)
21382 Lisp_Object map;
21383 int x, y;
21384 {
21385 while (CONSP (map))
21386 {
21387 if (CONSP (XCAR (map))
21388 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21389 return XCAR (map);
21390 map = XCDR (map);
21391 }
21392
21393 return Qnil;
21394 }
21395
21396 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21397 3, 3, 0,
21398 doc: /* Lookup in image map MAP coordinates X and Y.
21399 An image map is an alist where each element has the format (AREA ID PLIST).
21400 An AREA is specified as either a rectangle, a circle, or a polygon:
21401 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21402 pixel coordinates of the upper left and bottom right corners.
21403 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21404 and the radius of the circle; r may be a float or integer.
21405 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21406 vector describes one corner in the polygon.
21407 Returns the alist element for the first matching AREA in MAP. */)
21408 (map, x, y)
21409 Lisp_Object map;
21410 Lisp_Object x, y;
21411 {
21412 if (NILP (map))
21413 return Qnil;
21414
21415 CHECK_NUMBER (x);
21416 CHECK_NUMBER (y);
21417
21418 return find_hot_spot (map, XINT (x), XINT (y));
21419 }
21420
21421
21422 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21423 static void
21424 define_frame_cursor1 (f, cursor, pointer)
21425 struct frame *f;
21426 Cursor cursor;
21427 Lisp_Object pointer;
21428 {
21429 /* Do not change cursor shape while dragging mouse. */
21430 if (!NILP (do_mouse_tracking))
21431 return;
21432
21433 if (!NILP (pointer))
21434 {
21435 if (EQ (pointer, Qarrow))
21436 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21437 else if (EQ (pointer, Qhand))
21438 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21439 else if (EQ (pointer, Qtext))
21440 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21441 else if (EQ (pointer, intern ("hdrag")))
21442 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21443 #ifdef HAVE_X_WINDOWS
21444 else if (EQ (pointer, intern ("vdrag")))
21445 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21446 #endif
21447 else if (EQ (pointer, intern ("hourglass")))
21448 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21449 else if (EQ (pointer, Qmodeline))
21450 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21451 else
21452 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21453 }
21454
21455 if (cursor != No_Cursor)
21456 rif->define_frame_cursor (f, cursor);
21457 }
21458
21459 /* Take proper action when mouse has moved to the mode or header line
21460 or marginal area AREA of window W, x-position X and y-position Y.
21461 X is relative to the start of the text display area of W, so the
21462 width of bitmap areas and scroll bars must be subtracted to get a
21463 position relative to the start of the mode line. */
21464
21465 static void
21466 note_mode_line_or_margin_highlight (window, x, y, area)
21467 Lisp_Object window;
21468 int x, y;
21469 enum window_part area;
21470 {
21471 struct window *w = XWINDOW (window);
21472 struct frame *f = XFRAME (w->frame);
21473 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21474 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21475 Lisp_Object pointer = Qnil;
21476 int charpos, dx, dy, width, height;
21477 Lisp_Object string, object = Qnil;
21478 Lisp_Object pos, help;
21479
21480 Lisp_Object mouse_face;
21481 int original_x_pixel = x;
21482 struct glyph * glyph = NULL;
21483 struct glyph_row *row;
21484
21485 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21486 {
21487 int x0;
21488 struct glyph *end;
21489
21490 string = mode_line_string (w, area, &x, &y, &charpos,
21491 &object, &dx, &dy, &width, &height);
21492
21493 row = (area == ON_MODE_LINE
21494 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21495 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21496
21497 /* Find glyph */
21498 if (row->mode_line_p && row->enabled_p)
21499 {
21500 glyph = row->glyphs[TEXT_AREA];
21501 end = glyph + row->used[TEXT_AREA];
21502
21503 for (x0 = original_x_pixel;
21504 glyph < end && x0 >= glyph->pixel_width;
21505 ++glyph)
21506 x0 -= glyph->pixel_width;
21507
21508 if (glyph >= end)
21509 glyph = NULL;
21510 }
21511 }
21512 else
21513 {
21514 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21515 string = marginal_area_string (w, area, &x, &y, &charpos,
21516 &object, &dx, &dy, &width, &height);
21517 }
21518
21519 help = Qnil;
21520
21521 if (IMAGEP (object))
21522 {
21523 Lisp_Object image_map, hotspot;
21524 if ((image_map = Fplist_get (XCDR (object), QCmap),
21525 !NILP (image_map))
21526 && (hotspot = find_hot_spot (image_map, dx, dy),
21527 CONSP (hotspot))
21528 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21529 {
21530 Lisp_Object area_id, plist;
21531
21532 area_id = XCAR (hotspot);
21533 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21534 If so, we could look for mouse-enter, mouse-leave
21535 properties in PLIST (and do something...). */
21536 hotspot = XCDR (hotspot);
21537 if (CONSP (hotspot)
21538 && (plist = XCAR (hotspot), CONSP (plist)))
21539 {
21540 pointer = Fplist_get (plist, Qpointer);
21541 if (NILP (pointer))
21542 pointer = Qhand;
21543 help = Fplist_get (plist, Qhelp_echo);
21544 if (!NILP (help))
21545 {
21546 help_echo_string = help;
21547 /* Is this correct? ++kfs */
21548 XSETWINDOW (help_echo_window, w);
21549 help_echo_object = w->buffer;
21550 help_echo_pos = charpos;
21551 }
21552 }
21553 }
21554 if (NILP (pointer))
21555 pointer = Fplist_get (XCDR (object), QCpointer);
21556 }
21557
21558 if (STRINGP (string))
21559 {
21560 pos = make_number (charpos);
21561 /* If we're on a string with `help-echo' text property, arrange
21562 for the help to be displayed. This is done by setting the
21563 global variable help_echo_string to the help string. */
21564 if (NILP (help))
21565 {
21566 help = Fget_text_property (pos, Qhelp_echo, string);
21567 if (!NILP (help))
21568 {
21569 help_echo_string = help;
21570 XSETWINDOW (help_echo_window, w);
21571 help_echo_object = string;
21572 help_echo_pos = charpos;
21573 }
21574 }
21575
21576 if (NILP (pointer))
21577 pointer = Fget_text_property (pos, Qpointer, string);
21578
21579 /* Change the mouse pointer according to what is under X/Y. */
21580 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21581 {
21582 Lisp_Object map;
21583 map = Fget_text_property (pos, Qlocal_map, string);
21584 if (!KEYMAPP (map))
21585 map = Fget_text_property (pos, Qkeymap, string);
21586 if (!KEYMAPP (map))
21587 cursor = dpyinfo->vertical_scroll_bar_cursor;
21588 }
21589
21590 /* Change the mouse face according to what is under X/Y. */
21591 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21592 if (!NILP (mouse_face)
21593 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21594 && glyph)
21595 {
21596 Lisp_Object b, e;
21597
21598 struct glyph * tmp_glyph;
21599
21600 int gpos;
21601 int gseq_length;
21602 int total_pixel_width;
21603 int ignore;
21604
21605 int vpos, hpos;
21606
21607 b = Fprevious_single_property_change (make_number (charpos + 1),
21608 Qmouse_face, string, Qnil);
21609 if (NILP (b))
21610 b = make_number (0);
21611
21612 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21613 if (NILP (e))
21614 e = make_number (SCHARS (string));
21615
21616 /* Calculate the position(glyph position: GPOS) of GLYPH in
21617 displayed string. GPOS is different from CHARPOS.
21618
21619 CHARPOS is the position of glyph in internal string
21620 object. A mode line string format has structures which
21621 is converted to a flatten by emacs lisp interpreter.
21622 The internal string is an element of the structures.
21623 The displayed string is the flatten string. */
21624 for (tmp_glyph = glyph - 1, gpos = 0;
21625 tmp_glyph->charpos >= XINT (b);
21626 tmp_glyph--, gpos++)
21627 {
21628 if (!EQ (tmp_glyph->object, glyph->object))
21629 break;
21630 }
21631
21632 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21633 displayed string holding GLYPH.
21634
21635 GSEQ_LENGTH is different from SCHARS (STRING).
21636 SCHARS (STRING) returns the length of the internal string. */
21637 for (tmp_glyph = glyph, gseq_length = gpos;
21638 tmp_glyph->charpos < XINT (e);
21639 tmp_glyph++, gseq_length++)
21640 {
21641 if (!EQ (tmp_glyph->object, glyph->object))
21642 break;
21643 }
21644
21645 total_pixel_width = 0;
21646 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21647 total_pixel_width += tmp_glyph->pixel_width;
21648
21649 /* Pre calculation of re-rendering position */
21650 vpos = (x - gpos);
21651 hpos = (area == ON_MODE_LINE
21652 ? (w->current_matrix)->nrows - 1
21653 : 0);
21654
21655 /* If the re-rendering position is included in the last
21656 re-rendering area, we should do nothing. */
21657 if ( EQ (window, dpyinfo->mouse_face_window)
21658 && dpyinfo->mouse_face_beg_col <= vpos
21659 && vpos < dpyinfo->mouse_face_end_col
21660 && dpyinfo->mouse_face_beg_row == hpos )
21661 return;
21662
21663 if (clear_mouse_face (dpyinfo))
21664 cursor = No_Cursor;
21665
21666 dpyinfo->mouse_face_beg_col = vpos;
21667 dpyinfo->mouse_face_beg_row = hpos;
21668
21669 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21670 dpyinfo->mouse_face_beg_y = 0;
21671
21672 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21673 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21674
21675 dpyinfo->mouse_face_end_x = 0;
21676 dpyinfo->mouse_face_end_y = 0;
21677
21678 dpyinfo->mouse_face_past_end = 0;
21679 dpyinfo->mouse_face_window = window;
21680
21681 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21682 charpos,
21683 0, 0, 0, &ignore,
21684 glyph->face_id, 1);
21685 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21686
21687 if (NILP (pointer))
21688 pointer = Qhand;
21689 }
21690 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21691 clear_mouse_face (dpyinfo);
21692 }
21693 define_frame_cursor1 (f, cursor, pointer);
21694 }
21695
21696
21697 /* EXPORT:
21698 Take proper action when the mouse has moved to position X, Y on
21699 frame F as regards highlighting characters that have mouse-face
21700 properties. Also de-highlighting chars where the mouse was before.
21701 X and Y can be negative or out of range. */
21702
21703 void
21704 note_mouse_highlight (f, x, y)
21705 struct frame *f;
21706 int x, y;
21707 {
21708 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21709 enum window_part part;
21710 Lisp_Object window;
21711 struct window *w;
21712 Cursor cursor = No_Cursor;
21713 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21714 struct buffer *b;
21715
21716 /* When a menu is active, don't highlight because this looks odd. */
21717 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21718 if (popup_activated ())
21719 return;
21720 #endif
21721
21722 if (NILP (Vmouse_highlight)
21723 || !f->glyphs_initialized_p)
21724 return;
21725
21726 dpyinfo->mouse_face_mouse_x = x;
21727 dpyinfo->mouse_face_mouse_y = y;
21728 dpyinfo->mouse_face_mouse_frame = f;
21729
21730 if (dpyinfo->mouse_face_defer)
21731 return;
21732
21733 if (gc_in_progress)
21734 {
21735 dpyinfo->mouse_face_deferred_gc = 1;
21736 return;
21737 }
21738
21739 /* Which window is that in? */
21740 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21741
21742 /* If we were displaying active text in another window, clear that.
21743 Also clear if we move out of text area in same window. */
21744 if (! EQ (window, dpyinfo->mouse_face_window)
21745 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21746 && !NILP (dpyinfo->mouse_face_window)))
21747 clear_mouse_face (dpyinfo);
21748
21749 /* Not on a window -> return. */
21750 if (!WINDOWP (window))
21751 return;
21752
21753 /* Reset help_echo_string. It will get recomputed below. */
21754 help_echo_string = Qnil;
21755
21756 /* Convert to window-relative pixel coordinates. */
21757 w = XWINDOW (window);
21758 frame_to_window_pixel_xy (w, &x, &y);
21759
21760 /* Handle tool-bar window differently since it doesn't display a
21761 buffer. */
21762 if (EQ (window, f->tool_bar_window))
21763 {
21764 note_tool_bar_highlight (f, x, y);
21765 return;
21766 }
21767
21768 /* Mouse is on the mode, header line or margin? */
21769 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21770 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21771 {
21772 note_mode_line_or_margin_highlight (window, x, y, part);
21773 return;
21774 }
21775
21776 if (part == ON_VERTICAL_BORDER)
21777 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21778 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21779 || part == ON_SCROLL_BAR)
21780 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21781 else
21782 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21783
21784 /* Are we in a window whose display is up to date?
21785 And verify the buffer's text has not changed. */
21786 b = XBUFFER (w->buffer);
21787 if (part == ON_TEXT
21788 && EQ (w->window_end_valid, w->buffer)
21789 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21790 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21791 {
21792 int hpos, vpos, pos, i, dx, dy, area;
21793 struct glyph *glyph;
21794 Lisp_Object object;
21795 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21796 Lisp_Object *overlay_vec = NULL;
21797 int noverlays;
21798 struct buffer *obuf;
21799 int obegv, ozv, same_region;
21800
21801 /* Find the glyph under X/Y. */
21802 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21803
21804 /* Look for :pointer property on image. */
21805 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21806 {
21807 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21808 if (img != NULL && IMAGEP (img->spec))
21809 {
21810 Lisp_Object image_map, hotspot;
21811 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21812 !NILP (image_map))
21813 && (hotspot = find_hot_spot (image_map,
21814 glyph->slice.x + dx,
21815 glyph->slice.y + dy),
21816 CONSP (hotspot))
21817 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21818 {
21819 Lisp_Object area_id, plist;
21820
21821 area_id = XCAR (hotspot);
21822 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21823 If so, we could look for mouse-enter, mouse-leave
21824 properties in PLIST (and do something...). */
21825 hotspot = XCDR (hotspot);
21826 if (CONSP (hotspot)
21827 && (plist = XCAR (hotspot), CONSP (plist)))
21828 {
21829 pointer = Fplist_get (plist, Qpointer);
21830 if (NILP (pointer))
21831 pointer = Qhand;
21832 help_echo_string = Fplist_get (plist, Qhelp_echo);
21833 if (!NILP (help_echo_string))
21834 {
21835 help_echo_window = window;
21836 help_echo_object = glyph->object;
21837 help_echo_pos = glyph->charpos;
21838 }
21839 }
21840 }
21841 if (NILP (pointer))
21842 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21843 }
21844 }
21845
21846 /* Clear mouse face if X/Y not over text. */
21847 if (glyph == NULL
21848 || area != TEXT_AREA
21849 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21850 {
21851 if (clear_mouse_face (dpyinfo))
21852 cursor = No_Cursor;
21853 if (NILP (pointer))
21854 {
21855 if (area != TEXT_AREA)
21856 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21857 else
21858 pointer = Vvoid_text_area_pointer;
21859 }
21860 goto set_cursor;
21861 }
21862
21863 pos = glyph->charpos;
21864 object = glyph->object;
21865 if (!STRINGP (object) && !BUFFERP (object))
21866 goto set_cursor;
21867
21868 /* If we get an out-of-range value, return now; avoid an error. */
21869 if (BUFFERP (object) && pos > BUF_Z (b))
21870 goto set_cursor;
21871
21872 /* Make the window's buffer temporarily current for
21873 overlays_at and compute_char_face. */
21874 obuf = current_buffer;
21875 current_buffer = b;
21876 obegv = BEGV;
21877 ozv = ZV;
21878 BEGV = BEG;
21879 ZV = Z;
21880
21881 /* Is this char mouse-active or does it have help-echo? */
21882 position = make_number (pos);
21883
21884 if (BUFFERP (object))
21885 {
21886 /* Put all the overlays we want in a vector in overlay_vec. */
21887 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21888 /* Sort overlays into increasing priority order. */
21889 noverlays = sort_overlays (overlay_vec, noverlays, w);
21890 }
21891 else
21892 noverlays = 0;
21893
21894 same_region = (EQ (window, dpyinfo->mouse_face_window)
21895 && vpos >= dpyinfo->mouse_face_beg_row
21896 && vpos <= dpyinfo->mouse_face_end_row
21897 && (vpos > dpyinfo->mouse_face_beg_row
21898 || hpos >= dpyinfo->mouse_face_beg_col)
21899 && (vpos < dpyinfo->mouse_face_end_row
21900 || hpos < dpyinfo->mouse_face_end_col
21901 || dpyinfo->mouse_face_past_end));
21902
21903 if (same_region)
21904 cursor = No_Cursor;
21905
21906 /* Check mouse-face highlighting. */
21907 if (! same_region
21908 /* If there exists an overlay with mouse-face overlapping
21909 the one we are currently highlighting, we have to
21910 check if we enter the overlapping overlay, and then
21911 highlight only that. */
21912 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21913 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21914 {
21915 /* Find the highest priority overlay that has a mouse-face
21916 property. */
21917 overlay = Qnil;
21918 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21919 {
21920 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21921 if (!NILP (mouse_face))
21922 overlay = overlay_vec[i];
21923 }
21924
21925 /* If we're actually highlighting the same overlay as
21926 before, there's no need to do that again. */
21927 if (!NILP (overlay)
21928 && EQ (overlay, dpyinfo->mouse_face_overlay))
21929 goto check_help_echo;
21930
21931 dpyinfo->mouse_face_overlay = overlay;
21932
21933 /* Clear the display of the old active region, if any. */
21934 if (clear_mouse_face (dpyinfo))
21935 cursor = No_Cursor;
21936
21937 /* If no overlay applies, get a text property. */
21938 if (NILP (overlay))
21939 mouse_face = Fget_text_property (position, Qmouse_face, object);
21940
21941 /* Handle the overlay case. */
21942 if (!NILP (overlay))
21943 {
21944 /* Find the range of text around this char that
21945 should be active. */
21946 Lisp_Object before, after;
21947 int ignore;
21948
21949 before = Foverlay_start (overlay);
21950 after = Foverlay_end (overlay);
21951 /* Record this as the current active region. */
21952 fast_find_position (w, XFASTINT (before),
21953 &dpyinfo->mouse_face_beg_col,
21954 &dpyinfo->mouse_face_beg_row,
21955 &dpyinfo->mouse_face_beg_x,
21956 &dpyinfo->mouse_face_beg_y, Qnil);
21957
21958 dpyinfo->mouse_face_past_end
21959 = !fast_find_position (w, XFASTINT (after),
21960 &dpyinfo->mouse_face_end_col,
21961 &dpyinfo->mouse_face_end_row,
21962 &dpyinfo->mouse_face_end_x,
21963 &dpyinfo->mouse_face_end_y, Qnil);
21964 dpyinfo->mouse_face_window = window;
21965
21966 dpyinfo->mouse_face_face_id
21967 = face_at_buffer_position (w, pos, 0, 0,
21968 &ignore, pos + 1,
21969 !dpyinfo->mouse_face_hidden);
21970
21971 /* Display it as active. */
21972 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21973 cursor = No_Cursor;
21974 }
21975 /* Handle the text property case. */
21976 else if (!NILP (mouse_face) && BUFFERP (object))
21977 {
21978 /* Find the range of text around this char that
21979 should be active. */
21980 Lisp_Object before, after, beginning, end;
21981 int ignore;
21982
21983 beginning = Fmarker_position (w->start);
21984 end = make_number (BUF_Z (XBUFFER (object))
21985 - XFASTINT (w->window_end_pos));
21986 before
21987 = Fprevious_single_property_change (make_number (pos + 1),
21988 Qmouse_face,
21989 object, beginning);
21990 after
21991 = Fnext_single_property_change (position, Qmouse_face,
21992 object, end);
21993
21994 /* Record this as the current active region. */
21995 fast_find_position (w, XFASTINT (before),
21996 &dpyinfo->mouse_face_beg_col,
21997 &dpyinfo->mouse_face_beg_row,
21998 &dpyinfo->mouse_face_beg_x,
21999 &dpyinfo->mouse_face_beg_y, Qnil);
22000 dpyinfo->mouse_face_past_end
22001 = !fast_find_position (w, XFASTINT (after),
22002 &dpyinfo->mouse_face_end_col,
22003 &dpyinfo->mouse_face_end_row,
22004 &dpyinfo->mouse_face_end_x,
22005 &dpyinfo->mouse_face_end_y, Qnil);
22006 dpyinfo->mouse_face_window = window;
22007
22008 if (BUFFERP (object))
22009 dpyinfo->mouse_face_face_id
22010 = face_at_buffer_position (w, pos, 0, 0,
22011 &ignore, pos + 1,
22012 !dpyinfo->mouse_face_hidden);
22013
22014 /* Display it as active. */
22015 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22016 cursor = No_Cursor;
22017 }
22018 else if (!NILP (mouse_face) && STRINGP (object))
22019 {
22020 Lisp_Object b, e;
22021 int ignore;
22022
22023 b = Fprevious_single_property_change (make_number (pos + 1),
22024 Qmouse_face,
22025 object, Qnil);
22026 e = Fnext_single_property_change (position, Qmouse_face,
22027 object, Qnil);
22028 if (NILP (b))
22029 b = make_number (0);
22030 if (NILP (e))
22031 e = make_number (SCHARS (object) - 1);
22032
22033 fast_find_string_pos (w, XINT (b), object,
22034 &dpyinfo->mouse_face_beg_col,
22035 &dpyinfo->mouse_face_beg_row,
22036 &dpyinfo->mouse_face_beg_x,
22037 &dpyinfo->mouse_face_beg_y, 0);
22038 fast_find_string_pos (w, XINT (e), object,
22039 &dpyinfo->mouse_face_end_col,
22040 &dpyinfo->mouse_face_end_row,
22041 &dpyinfo->mouse_face_end_x,
22042 &dpyinfo->mouse_face_end_y, 1);
22043 dpyinfo->mouse_face_past_end = 0;
22044 dpyinfo->mouse_face_window = window;
22045 dpyinfo->mouse_face_face_id
22046 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22047 glyph->face_id, 1);
22048 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22049 cursor = No_Cursor;
22050 }
22051 else if (STRINGP (object) && NILP (mouse_face))
22052 {
22053 /* A string which doesn't have mouse-face, but
22054 the text ``under'' it might have. */
22055 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22056 int start = MATRIX_ROW_START_CHARPOS (r);
22057
22058 pos = string_buffer_position (w, object, start);
22059 if (pos > 0)
22060 mouse_face = get_char_property_and_overlay (make_number (pos),
22061 Qmouse_face,
22062 w->buffer,
22063 &overlay);
22064 if (!NILP (mouse_face) && !NILP (overlay))
22065 {
22066 Lisp_Object before = Foverlay_start (overlay);
22067 Lisp_Object after = Foverlay_end (overlay);
22068 int ignore;
22069
22070 /* Note that we might not be able to find position
22071 BEFORE in the glyph matrix if the overlay is
22072 entirely covered by a `display' property. In
22073 this case, we overshoot. So let's stop in
22074 the glyph matrix before glyphs for OBJECT. */
22075 fast_find_position (w, XFASTINT (before),
22076 &dpyinfo->mouse_face_beg_col,
22077 &dpyinfo->mouse_face_beg_row,
22078 &dpyinfo->mouse_face_beg_x,
22079 &dpyinfo->mouse_face_beg_y,
22080 object);
22081
22082 dpyinfo->mouse_face_past_end
22083 = !fast_find_position (w, XFASTINT (after),
22084 &dpyinfo->mouse_face_end_col,
22085 &dpyinfo->mouse_face_end_row,
22086 &dpyinfo->mouse_face_end_x,
22087 &dpyinfo->mouse_face_end_y,
22088 Qnil);
22089 dpyinfo->mouse_face_window = window;
22090 dpyinfo->mouse_face_face_id
22091 = face_at_buffer_position (w, pos, 0, 0,
22092 &ignore, pos + 1,
22093 !dpyinfo->mouse_face_hidden);
22094
22095 /* Display it as active. */
22096 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22097 cursor = No_Cursor;
22098 }
22099 }
22100 }
22101
22102 check_help_echo:
22103
22104 /* Look for a `help-echo' property. */
22105 if (NILP (help_echo_string)) {
22106 Lisp_Object help, overlay;
22107
22108 /* Check overlays first. */
22109 help = overlay = Qnil;
22110 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22111 {
22112 overlay = overlay_vec[i];
22113 help = Foverlay_get (overlay, Qhelp_echo);
22114 }
22115
22116 if (!NILP (help))
22117 {
22118 help_echo_string = help;
22119 help_echo_window = window;
22120 help_echo_object = overlay;
22121 help_echo_pos = pos;
22122 }
22123 else
22124 {
22125 Lisp_Object object = glyph->object;
22126 int charpos = glyph->charpos;
22127
22128 /* Try text properties. */
22129 if (STRINGP (object)
22130 && charpos >= 0
22131 && charpos < SCHARS (object))
22132 {
22133 help = Fget_text_property (make_number (charpos),
22134 Qhelp_echo, object);
22135 if (NILP (help))
22136 {
22137 /* If the string itself doesn't specify a help-echo,
22138 see if the buffer text ``under'' it does. */
22139 struct glyph_row *r
22140 = MATRIX_ROW (w->current_matrix, vpos);
22141 int start = MATRIX_ROW_START_CHARPOS (r);
22142 int pos = string_buffer_position (w, object, start);
22143 if (pos > 0)
22144 {
22145 help = Fget_char_property (make_number (pos),
22146 Qhelp_echo, w->buffer);
22147 if (!NILP (help))
22148 {
22149 charpos = pos;
22150 object = w->buffer;
22151 }
22152 }
22153 }
22154 }
22155 else if (BUFFERP (object)
22156 && charpos >= BEGV
22157 && charpos < ZV)
22158 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22159 object);
22160
22161 if (!NILP (help))
22162 {
22163 help_echo_string = help;
22164 help_echo_window = window;
22165 help_echo_object = object;
22166 help_echo_pos = charpos;
22167 }
22168 }
22169 }
22170
22171 /* Look for a `pointer' property. */
22172 if (NILP (pointer))
22173 {
22174 /* Check overlays first. */
22175 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22176 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22177
22178 if (NILP (pointer))
22179 {
22180 Lisp_Object object = glyph->object;
22181 int charpos = glyph->charpos;
22182
22183 /* Try text properties. */
22184 if (STRINGP (object)
22185 && charpos >= 0
22186 && charpos < SCHARS (object))
22187 {
22188 pointer = Fget_text_property (make_number (charpos),
22189 Qpointer, object);
22190 if (NILP (pointer))
22191 {
22192 /* If the string itself doesn't specify a pointer,
22193 see if the buffer text ``under'' it does. */
22194 struct glyph_row *r
22195 = MATRIX_ROW (w->current_matrix, vpos);
22196 int start = MATRIX_ROW_START_CHARPOS (r);
22197 int pos = string_buffer_position (w, object, start);
22198 if (pos > 0)
22199 pointer = Fget_char_property (make_number (pos),
22200 Qpointer, w->buffer);
22201 }
22202 }
22203 else if (BUFFERP (object)
22204 && charpos >= BEGV
22205 && charpos < ZV)
22206 pointer = Fget_text_property (make_number (charpos),
22207 Qpointer, object);
22208 }
22209 }
22210
22211 BEGV = obegv;
22212 ZV = ozv;
22213 current_buffer = obuf;
22214 }
22215
22216 set_cursor:
22217
22218 define_frame_cursor1 (f, cursor, pointer);
22219 }
22220
22221
22222 /* EXPORT for RIF:
22223 Clear any mouse-face on window W. This function is part of the
22224 redisplay interface, and is called from try_window_id and similar
22225 functions to ensure the mouse-highlight is off. */
22226
22227 void
22228 x_clear_window_mouse_face (w)
22229 struct window *w;
22230 {
22231 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22232 Lisp_Object window;
22233
22234 BLOCK_INPUT;
22235 XSETWINDOW (window, w);
22236 if (EQ (window, dpyinfo->mouse_face_window))
22237 clear_mouse_face (dpyinfo);
22238 UNBLOCK_INPUT;
22239 }
22240
22241
22242 /* EXPORT:
22243 Just discard the mouse face information for frame F, if any.
22244 This is used when the size of F is changed. */
22245
22246 void
22247 cancel_mouse_face (f)
22248 struct frame *f;
22249 {
22250 Lisp_Object window;
22251 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22252
22253 window = dpyinfo->mouse_face_window;
22254 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22255 {
22256 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22257 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22258 dpyinfo->mouse_face_window = Qnil;
22259 }
22260 }
22261
22262
22263 #endif /* HAVE_WINDOW_SYSTEM */
22264
22265 \f
22266 /***********************************************************************
22267 Exposure Events
22268 ***********************************************************************/
22269
22270 #ifdef HAVE_WINDOW_SYSTEM
22271
22272 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22273 which intersects rectangle R. R is in window-relative coordinates. */
22274
22275 static void
22276 expose_area (w, row, r, area)
22277 struct window *w;
22278 struct glyph_row *row;
22279 XRectangle *r;
22280 enum glyph_row_area area;
22281 {
22282 struct glyph *first = row->glyphs[area];
22283 struct glyph *end = row->glyphs[area] + row->used[area];
22284 struct glyph *last;
22285 int first_x, start_x, x;
22286
22287 if (area == TEXT_AREA && row->fill_line_p)
22288 /* If row extends face to end of line write the whole line. */
22289 draw_glyphs (w, 0, row, area,
22290 0, row->used[area],
22291 DRAW_NORMAL_TEXT, 0);
22292 else
22293 {
22294 /* Set START_X to the window-relative start position for drawing glyphs of
22295 AREA. The first glyph of the text area can be partially visible.
22296 The first glyphs of other areas cannot. */
22297 start_x = window_box_left_offset (w, area);
22298 x = start_x;
22299 if (area == TEXT_AREA)
22300 x += row->x;
22301
22302 /* Find the first glyph that must be redrawn. */
22303 while (first < end
22304 && x + first->pixel_width < r->x)
22305 {
22306 x += first->pixel_width;
22307 ++first;
22308 }
22309
22310 /* Find the last one. */
22311 last = first;
22312 first_x = x;
22313 while (last < end
22314 && x < r->x + r->width)
22315 {
22316 x += last->pixel_width;
22317 ++last;
22318 }
22319
22320 /* Repaint. */
22321 if (last > first)
22322 draw_glyphs (w, first_x - start_x, row, area,
22323 first - row->glyphs[area], last - row->glyphs[area],
22324 DRAW_NORMAL_TEXT, 0);
22325 }
22326 }
22327
22328
22329 /* Redraw the parts of the glyph row ROW on window W intersecting
22330 rectangle R. R is in window-relative coordinates. Value is
22331 non-zero if mouse-face was overwritten. */
22332
22333 static int
22334 expose_line (w, row, r)
22335 struct window *w;
22336 struct glyph_row *row;
22337 XRectangle *r;
22338 {
22339 xassert (row->enabled_p);
22340
22341 if (row->mode_line_p || w->pseudo_window_p)
22342 draw_glyphs (w, 0, row, TEXT_AREA,
22343 0, row->used[TEXT_AREA],
22344 DRAW_NORMAL_TEXT, 0);
22345 else
22346 {
22347 if (row->used[LEFT_MARGIN_AREA])
22348 expose_area (w, row, r, LEFT_MARGIN_AREA);
22349 if (row->used[TEXT_AREA])
22350 expose_area (w, row, r, TEXT_AREA);
22351 if (row->used[RIGHT_MARGIN_AREA])
22352 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22353 draw_row_fringe_bitmaps (w, row);
22354 }
22355
22356 return row->mouse_face_p;
22357 }
22358
22359
22360 /* Redraw those parts of glyphs rows during expose event handling that
22361 overlap other rows. Redrawing of an exposed line writes over parts
22362 of lines overlapping that exposed line; this function fixes that.
22363
22364 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22365 row in W's current matrix that is exposed and overlaps other rows.
22366 LAST_OVERLAPPING_ROW is the last such row. */
22367
22368 static void
22369 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22370 struct window *w;
22371 struct glyph_row *first_overlapping_row;
22372 struct glyph_row *last_overlapping_row;
22373 {
22374 struct glyph_row *row;
22375
22376 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22377 if (row->overlapping_p)
22378 {
22379 xassert (row->enabled_p && !row->mode_line_p);
22380
22381 if (row->used[LEFT_MARGIN_AREA])
22382 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22383
22384 if (row->used[TEXT_AREA])
22385 x_fix_overlapping_area (w, row, TEXT_AREA);
22386
22387 if (row->used[RIGHT_MARGIN_AREA])
22388 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22389 }
22390 }
22391
22392
22393 /* Return non-zero if W's cursor intersects rectangle R. */
22394
22395 static int
22396 phys_cursor_in_rect_p (w, r)
22397 struct window *w;
22398 XRectangle *r;
22399 {
22400 XRectangle cr, result;
22401 struct glyph *cursor_glyph;
22402
22403 cursor_glyph = get_phys_cursor_glyph (w);
22404 if (cursor_glyph)
22405 {
22406 /* r is relative to W's box, but w->phys_cursor.x is relative
22407 to left edge of W's TEXT area. Adjust it. */
22408 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22409 cr.y = w->phys_cursor.y;
22410 cr.width = cursor_glyph->pixel_width;
22411 cr.height = w->phys_cursor_height;
22412 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22413 I assume the effect is the same -- and this is portable. */
22414 return x_intersect_rectangles (&cr, r, &result);
22415 }
22416 else
22417 return 0;
22418 }
22419
22420
22421 /* EXPORT:
22422 Draw a vertical window border to the right of window W if W doesn't
22423 have vertical scroll bars. */
22424
22425 void
22426 x_draw_vertical_border (w)
22427 struct window *w;
22428 {
22429 /* We could do better, if we knew what type of scroll-bar the adjacent
22430 windows (on either side) have... But we don't :-(
22431 However, I think this works ok. ++KFS 2003-04-25 */
22432
22433 /* Redraw borders between horizontally adjacent windows. Don't
22434 do it for frames with vertical scroll bars because either the
22435 right scroll bar of a window, or the left scroll bar of its
22436 neighbor will suffice as a border. */
22437 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22438 return;
22439
22440 if (!WINDOW_RIGHTMOST_P (w)
22441 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22442 {
22443 int x0, x1, y0, y1;
22444
22445 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22446 y1 -= 1;
22447
22448 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22449 x1 -= 1;
22450
22451 rif->draw_vertical_window_border (w, x1, y0, y1);
22452 }
22453 else if (!WINDOW_LEFTMOST_P (w)
22454 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22455 {
22456 int x0, x1, y0, y1;
22457
22458 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22459 y1 -= 1;
22460
22461 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22462 x0 -= 1;
22463
22464 rif->draw_vertical_window_border (w, x0, y0, y1);
22465 }
22466 }
22467
22468
22469 /* Redraw the part of window W intersection rectangle FR. Pixel
22470 coordinates in FR are frame-relative. Call this function with
22471 input blocked. Value is non-zero if the exposure overwrites
22472 mouse-face. */
22473
22474 static int
22475 expose_window (w, fr)
22476 struct window *w;
22477 XRectangle *fr;
22478 {
22479 struct frame *f = XFRAME (w->frame);
22480 XRectangle wr, r;
22481 int mouse_face_overwritten_p = 0;
22482
22483 /* If window is not yet fully initialized, do nothing. This can
22484 happen when toolkit scroll bars are used and a window is split.
22485 Reconfiguring the scroll bar will generate an expose for a newly
22486 created window. */
22487 if (w->current_matrix == NULL)
22488 return 0;
22489
22490 /* When we're currently updating the window, display and current
22491 matrix usually don't agree. Arrange for a thorough display
22492 later. */
22493 if (w == updated_window)
22494 {
22495 SET_FRAME_GARBAGED (f);
22496 return 0;
22497 }
22498
22499 /* Frame-relative pixel rectangle of W. */
22500 wr.x = WINDOW_LEFT_EDGE_X (w);
22501 wr.y = WINDOW_TOP_EDGE_Y (w);
22502 wr.width = WINDOW_TOTAL_WIDTH (w);
22503 wr.height = WINDOW_TOTAL_HEIGHT (w);
22504
22505 if (x_intersect_rectangles (fr, &wr, &r))
22506 {
22507 int yb = window_text_bottom_y (w);
22508 struct glyph_row *row;
22509 int cursor_cleared_p;
22510 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22511
22512 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22513 r.x, r.y, r.width, r.height));
22514
22515 /* Convert to window coordinates. */
22516 r.x -= WINDOW_LEFT_EDGE_X (w);
22517 r.y -= WINDOW_TOP_EDGE_Y (w);
22518
22519 /* Turn off the cursor. */
22520 if (!w->pseudo_window_p
22521 && phys_cursor_in_rect_p (w, &r))
22522 {
22523 x_clear_cursor (w);
22524 cursor_cleared_p = 1;
22525 }
22526 else
22527 cursor_cleared_p = 0;
22528
22529 /* Update lines intersecting rectangle R. */
22530 first_overlapping_row = last_overlapping_row = NULL;
22531 for (row = w->current_matrix->rows;
22532 row->enabled_p;
22533 ++row)
22534 {
22535 int y0 = row->y;
22536 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22537
22538 if ((y0 >= r.y && y0 < r.y + r.height)
22539 || (y1 > r.y && y1 < r.y + r.height)
22540 || (r.y >= y0 && r.y < y1)
22541 || (r.y + r.height > y0 && r.y + r.height < y1))
22542 {
22543 /* A header line may be overlapping, but there is no need
22544 to fix overlapping areas for them. KFS 2005-02-12 */
22545 if (row->overlapping_p && !row->mode_line_p)
22546 {
22547 if (first_overlapping_row == NULL)
22548 first_overlapping_row = row;
22549 last_overlapping_row = row;
22550 }
22551
22552 if (expose_line (w, row, &r))
22553 mouse_face_overwritten_p = 1;
22554 }
22555
22556 if (y1 >= yb)
22557 break;
22558 }
22559
22560 /* Display the mode line if there is one. */
22561 if (WINDOW_WANTS_MODELINE_P (w)
22562 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22563 row->enabled_p)
22564 && row->y < r.y + r.height)
22565 {
22566 if (expose_line (w, row, &r))
22567 mouse_face_overwritten_p = 1;
22568 }
22569
22570 if (!w->pseudo_window_p)
22571 {
22572 /* Fix the display of overlapping rows. */
22573 if (first_overlapping_row)
22574 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22575
22576 /* Draw border between windows. */
22577 x_draw_vertical_border (w);
22578
22579 /* Turn the cursor on again. */
22580 if (cursor_cleared_p)
22581 update_window_cursor (w, 1);
22582 }
22583 }
22584
22585 return mouse_face_overwritten_p;
22586 }
22587
22588
22589
22590 /* Redraw (parts) of all windows in the window tree rooted at W that
22591 intersect R. R contains frame pixel coordinates. Value is
22592 non-zero if the exposure overwrites mouse-face. */
22593
22594 static int
22595 expose_window_tree (w, r)
22596 struct window *w;
22597 XRectangle *r;
22598 {
22599 struct frame *f = XFRAME (w->frame);
22600 int mouse_face_overwritten_p = 0;
22601
22602 while (w && !FRAME_GARBAGED_P (f))
22603 {
22604 if (!NILP (w->hchild))
22605 mouse_face_overwritten_p
22606 |= expose_window_tree (XWINDOW (w->hchild), r);
22607 else if (!NILP (w->vchild))
22608 mouse_face_overwritten_p
22609 |= expose_window_tree (XWINDOW (w->vchild), r);
22610 else
22611 mouse_face_overwritten_p |= expose_window (w, r);
22612
22613 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22614 }
22615
22616 return mouse_face_overwritten_p;
22617 }
22618
22619
22620 /* EXPORT:
22621 Redisplay an exposed area of frame F. X and Y are the upper-left
22622 corner of the exposed rectangle. W and H are width and height of
22623 the exposed area. All are pixel values. W or H zero means redraw
22624 the entire frame. */
22625
22626 void
22627 expose_frame (f, x, y, w, h)
22628 struct frame *f;
22629 int x, y, w, h;
22630 {
22631 XRectangle r;
22632 int mouse_face_overwritten_p = 0;
22633
22634 TRACE ((stderr, "expose_frame "));
22635
22636 /* No need to redraw if frame will be redrawn soon. */
22637 if (FRAME_GARBAGED_P (f))
22638 {
22639 TRACE ((stderr, " garbaged\n"));
22640 return;
22641 }
22642
22643 /* If basic faces haven't been realized yet, there is no point in
22644 trying to redraw anything. This can happen when we get an expose
22645 event while Emacs is starting, e.g. by moving another window. */
22646 if (FRAME_FACE_CACHE (f) == NULL
22647 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22648 {
22649 TRACE ((stderr, " no faces\n"));
22650 return;
22651 }
22652
22653 if (w == 0 || h == 0)
22654 {
22655 r.x = r.y = 0;
22656 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22657 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22658 }
22659 else
22660 {
22661 r.x = x;
22662 r.y = y;
22663 r.width = w;
22664 r.height = h;
22665 }
22666
22667 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22668 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22669
22670 if (WINDOWP (f->tool_bar_window))
22671 mouse_face_overwritten_p
22672 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22673
22674 #ifdef HAVE_X_WINDOWS
22675 #ifndef MSDOS
22676 #ifndef USE_X_TOOLKIT
22677 if (WINDOWP (f->menu_bar_window))
22678 mouse_face_overwritten_p
22679 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22680 #endif /* not USE_X_TOOLKIT */
22681 #endif
22682 #endif
22683
22684 /* Some window managers support a focus-follows-mouse style with
22685 delayed raising of frames. Imagine a partially obscured frame,
22686 and moving the mouse into partially obscured mouse-face on that
22687 frame. The visible part of the mouse-face will be highlighted,
22688 then the WM raises the obscured frame. With at least one WM, KDE
22689 2.1, Emacs is not getting any event for the raising of the frame
22690 (even tried with SubstructureRedirectMask), only Expose events.
22691 These expose events will draw text normally, i.e. not
22692 highlighted. Which means we must redo the highlight here.
22693 Subsume it under ``we love X''. --gerd 2001-08-15 */
22694 /* Included in Windows version because Windows most likely does not
22695 do the right thing if any third party tool offers
22696 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22697 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22698 {
22699 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22700 if (f == dpyinfo->mouse_face_mouse_frame)
22701 {
22702 int x = dpyinfo->mouse_face_mouse_x;
22703 int y = dpyinfo->mouse_face_mouse_y;
22704 clear_mouse_face (dpyinfo);
22705 note_mouse_highlight (f, x, y);
22706 }
22707 }
22708 }
22709
22710
22711 /* EXPORT:
22712 Determine the intersection of two rectangles R1 and R2. Return
22713 the intersection in *RESULT. Value is non-zero if RESULT is not
22714 empty. */
22715
22716 int
22717 x_intersect_rectangles (r1, r2, result)
22718 XRectangle *r1, *r2, *result;
22719 {
22720 XRectangle *left, *right;
22721 XRectangle *upper, *lower;
22722 int intersection_p = 0;
22723
22724 /* Rearrange so that R1 is the left-most rectangle. */
22725 if (r1->x < r2->x)
22726 left = r1, right = r2;
22727 else
22728 left = r2, right = r1;
22729
22730 /* X0 of the intersection is right.x0, if this is inside R1,
22731 otherwise there is no intersection. */
22732 if (right->x <= left->x + left->width)
22733 {
22734 result->x = right->x;
22735
22736 /* The right end of the intersection is the minimum of the
22737 the right ends of left and right. */
22738 result->width = (min (left->x + left->width, right->x + right->width)
22739 - result->x);
22740
22741 /* Same game for Y. */
22742 if (r1->y < r2->y)
22743 upper = r1, lower = r2;
22744 else
22745 upper = r2, lower = r1;
22746
22747 /* The upper end of the intersection is lower.y0, if this is inside
22748 of upper. Otherwise, there is no intersection. */
22749 if (lower->y <= upper->y + upper->height)
22750 {
22751 result->y = lower->y;
22752
22753 /* The lower end of the intersection is the minimum of the lower
22754 ends of upper and lower. */
22755 result->height = (min (lower->y + lower->height,
22756 upper->y + upper->height)
22757 - result->y);
22758 intersection_p = 1;
22759 }
22760 }
22761
22762 return intersection_p;
22763 }
22764
22765 #endif /* HAVE_WINDOW_SYSTEM */
22766
22767 \f
22768 /***********************************************************************
22769 Initialization
22770 ***********************************************************************/
22771
22772 void
22773 syms_of_xdisp ()
22774 {
22775 Vwith_echo_area_save_vector = Qnil;
22776 staticpro (&Vwith_echo_area_save_vector);
22777
22778 Vmessage_stack = Qnil;
22779 staticpro (&Vmessage_stack);
22780
22781 Qinhibit_redisplay = intern ("inhibit-redisplay");
22782 staticpro (&Qinhibit_redisplay);
22783
22784 message_dolog_marker1 = Fmake_marker ();
22785 staticpro (&message_dolog_marker1);
22786 message_dolog_marker2 = Fmake_marker ();
22787 staticpro (&message_dolog_marker2);
22788 message_dolog_marker3 = Fmake_marker ();
22789 staticpro (&message_dolog_marker3);
22790
22791 #if GLYPH_DEBUG
22792 defsubr (&Sdump_frame_glyph_matrix);
22793 defsubr (&Sdump_glyph_matrix);
22794 defsubr (&Sdump_glyph_row);
22795 defsubr (&Sdump_tool_bar_row);
22796 defsubr (&Strace_redisplay);
22797 defsubr (&Strace_to_stderr);
22798 #endif
22799 #ifdef HAVE_WINDOW_SYSTEM
22800 defsubr (&Stool_bar_lines_needed);
22801 defsubr (&Slookup_image_map);
22802 #endif
22803 defsubr (&Sformat_mode_line);
22804
22805 staticpro (&Qmenu_bar_update_hook);
22806 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22807
22808 staticpro (&Qoverriding_terminal_local_map);
22809 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22810
22811 staticpro (&Qoverriding_local_map);
22812 Qoverriding_local_map = intern ("overriding-local-map");
22813
22814 staticpro (&Qwindow_scroll_functions);
22815 Qwindow_scroll_functions = intern ("window-scroll-functions");
22816
22817 staticpro (&Qredisplay_end_trigger_functions);
22818 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22819
22820 staticpro (&Qinhibit_point_motion_hooks);
22821 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22822
22823 QCdata = intern (":data");
22824 staticpro (&QCdata);
22825 Qdisplay = intern ("display");
22826 staticpro (&Qdisplay);
22827 Qspace_width = intern ("space-width");
22828 staticpro (&Qspace_width);
22829 Qraise = intern ("raise");
22830 staticpro (&Qraise);
22831 Qslice = intern ("slice");
22832 staticpro (&Qslice);
22833 Qspace = intern ("space");
22834 staticpro (&Qspace);
22835 Qmargin = intern ("margin");
22836 staticpro (&Qmargin);
22837 Qpointer = intern ("pointer");
22838 staticpro (&Qpointer);
22839 Qleft_margin = intern ("left-margin");
22840 staticpro (&Qleft_margin);
22841 Qright_margin = intern ("right-margin");
22842 staticpro (&Qright_margin);
22843 Qcenter = intern ("center");
22844 staticpro (&Qcenter);
22845 Qline_height = intern ("line-height");
22846 staticpro (&Qline_height);
22847 QCalign_to = intern (":align-to");
22848 staticpro (&QCalign_to);
22849 QCrelative_width = intern (":relative-width");
22850 staticpro (&QCrelative_width);
22851 QCrelative_height = intern (":relative-height");
22852 staticpro (&QCrelative_height);
22853 QCeval = intern (":eval");
22854 staticpro (&QCeval);
22855 QCpropertize = intern (":propertize");
22856 staticpro (&QCpropertize);
22857 QCfile = intern (":file");
22858 staticpro (&QCfile);
22859 Qfontified = intern ("fontified");
22860 staticpro (&Qfontified);
22861 Qfontification_functions = intern ("fontification-functions");
22862 staticpro (&Qfontification_functions);
22863 Qtrailing_whitespace = intern ("trailing-whitespace");
22864 staticpro (&Qtrailing_whitespace);
22865 Qescape_glyph = intern ("escape-glyph");
22866 staticpro (&Qescape_glyph);
22867 Qnobreak_space = intern ("nobreak-space");
22868 staticpro (&Qnobreak_space);
22869 Qimage = intern ("image");
22870 staticpro (&Qimage);
22871 QCmap = intern (":map");
22872 staticpro (&QCmap);
22873 QCpointer = intern (":pointer");
22874 staticpro (&QCpointer);
22875 Qrect = intern ("rect");
22876 staticpro (&Qrect);
22877 Qcircle = intern ("circle");
22878 staticpro (&Qcircle);
22879 Qpoly = intern ("poly");
22880 staticpro (&Qpoly);
22881 Qmessage_truncate_lines = intern ("message-truncate-lines");
22882 staticpro (&Qmessage_truncate_lines);
22883 Qgrow_only = intern ("grow-only");
22884 staticpro (&Qgrow_only);
22885 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22886 staticpro (&Qinhibit_menubar_update);
22887 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22888 staticpro (&Qinhibit_eval_during_redisplay);
22889 Qposition = intern ("position");
22890 staticpro (&Qposition);
22891 Qbuffer_position = intern ("buffer-position");
22892 staticpro (&Qbuffer_position);
22893 Qobject = intern ("object");
22894 staticpro (&Qobject);
22895 Qbar = intern ("bar");
22896 staticpro (&Qbar);
22897 Qhbar = intern ("hbar");
22898 staticpro (&Qhbar);
22899 Qbox = intern ("box");
22900 staticpro (&Qbox);
22901 Qhollow = intern ("hollow");
22902 staticpro (&Qhollow);
22903 Qhand = intern ("hand");
22904 staticpro (&Qhand);
22905 Qarrow = intern ("arrow");
22906 staticpro (&Qarrow);
22907 Qtext = intern ("text");
22908 staticpro (&Qtext);
22909 Qrisky_local_variable = intern ("risky-local-variable");
22910 staticpro (&Qrisky_local_variable);
22911 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22912 staticpro (&Qinhibit_free_realized_faces);
22913
22914 list_of_error = Fcons (Fcons (intern ("error"),
22915 Fcons (intern ("void-variable"), Qnil)),
22916 Qnil);
22917 staticpro (&list_of_error);
22918
22919 Qlast_arrow_position = intern ("last-arrow-position");
22920 staticpro (&Qlast_arrow_position);
22921 Qlast_arrow_string = intern ("last-arrow-string");
22922 staticpro (&Qlast_arrow_string);
22923
22924 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22925 staticpro (&Qoverlay_arrow_string);
22926 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22927 staticpro (&Qoverlay_arrow_bitmap);
22928
22929 echo_buffer[0] = echo_buffer[1] = Qnil;
22930 staticpro (&echo_buffer[0]);
22931 staticpro (&echo_buffer[1]);
22932
22933 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22934 staticpro (&echo_area_buffer[0]);
22935 staticpro (&echo_area_buffer[1]);
22936
22937 Vmessages_buffer_name = build_string ("*Messages*");
22938 staticpro (&Vmessages_buffer_name);
22939
22940 mode_line_proptrans_alist = Qnil;
22941 staticpro (&mode_line_proptrans_alist);
22942 mode_line_string_list = Qnil;
22943 staticpro (&mode_line_string_list);
22944 mode_line_string_face = Qnil;
22945 staticpro (&mode_line_string_face);
22946 mode_line_string_face_prop = Qnil;
22947 staticpro (&mode_line_string_face_prop);
22948 Vmode_line_unwind_vector = Qnil;
22949 staticpro (&Vmode_line_unwind_vector);
22950
22951 help_echo_string = Qnil;
22952 staticpro (&help_echo_string);
22953 help_echo_object = Qnil;
22954 staticpro (&help_echo_object);
22955 help_echo_window = Qnil;
22956 staticpro (&help_echo_window);
22957 previous_help_echo_string = Qnil;
22958 staticpro (&previous_help_echo_string);
22959 help_echo_pos = -1;
22960
22961 #ifdef HAVE_WINDOW_SYSTEM
22962 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22963 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22964 For example, if a block cursor is over a tab, it will be drawn as
22965 wide as that tab on the display. */);
22966 x_stretch_cursor_p = 0;
22967 #endif
22968
22969 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22970 doc: /* *Non-nil means highlight trailing whitespace.
22971 The face used for trailing whitespace is `trailing-whitespace'. */);
22972 Vshow_trailing_whitespace = Qnil;
22973
22974 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
22975 doc: /* *Control highlighting of nobreak space and soft hyphen.
22976 A value of t means highlight the character itself (for nobreak space,
22977 use face `nobreak-space').
22978 A value of nil means no highlighting.
22979 Other values mean display the escape glyph followed by an ordinary
22980 space or ordinary hyphen. */);
22981 Vnobreak_char_display = Qt;
22982
22983 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22984 doc: /* *The pointer shape to show in void text areas.
22985 A value of nil means to show the text pointer. Other options are `arrow',
22986 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22987 Vvoid_text_area_pointer = Qarrow;
22988
22989 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22990 doc: /* Non-nil means don't actually do any redisplay.
22991 This is used for internal purposes. */);
22992 Vinhibit_redisplay = Qnil;
22993
22994 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22995 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22996 Vglobal_mode_string = Qnil;
22997
22998 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22999 doc: /* Marker for where to display an arrow on top of the buffer text.
23000 This must be the beginning of a line in order to work.
23001 See also `overlay-arrow-string'. */);
23002 Voverlay_arrow_position = Qnil;
23003
23004 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23005 doc: /* String to display as an arrow in non-window frames.
23006 See also `overlay-arrow-position'. */);
23007 Voverlay_arrow_string = build_string ("=>");
23008
23009 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23010 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23011 The symbols on this list are examined during redisplay to determine
23012 where to display overlay arrows. */);
23013 Voverlay_arrow_variable_list
23014 = Fcons (intern ("overlay-arrow-position"), Qnil);
23015
23016 DEFVAR_INT ("scroll-step", &scroll_step,
23017 doc: /* *The number of lines to try scrolling a window by when point moves out.
23018 If that fails to bring point back on frame, point is centered instead.
23019 If this is zero, point is always centered after it moves off frame.
23020 If you want scrolling to always be a line at a time, you should set
23021 `scroll-conservatively' to a large value rather than set this to 1. */);
23022
23023 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23024 doc: /* *Scroll up to this many lines, to bring point back on screen.
23025 A value of zero means to scroll the text to center point vertically
23026 in the window. */);
23027 scroll_conservatively = 0;
23028
23029 DEFVAR_INT ("scroll-margin", &scroll_margin,
23030 doc: /* *Number of lines of margin at the top and bottom of a window.
23031 Recenter the window whenever point gets within this many lines
23032 of the top or bottom of the window. */);
23033 scroll_margin = 0;
23034
23035 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23036 doc: /* Pixels per inch on current display.
23037 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23038 Vdisplay_pixels_per_inch = make_float (72.0);
23039
23040 #if GLYPH_DEBUG
23041 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23042 #endif
23043
23044 DEFVAR_BOOL ("truncate-partial-width-windows",
23045 &truncate_partial_width_windows,
23046 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23047 truncate_partial_width_windows = 1;
23048
23049 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23050 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23051 Any other value means to use the appropriate face, `mode-line',
23052 `header-line', or `menu' respectively. */);
23053 mode_line_inverse_video = 1;
23054
23055 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23056 doc: /* *Maximum buffer size for which line number should be displayed.
23057 If the buffer is bigger than this, the line number does not appear
23058 in the mode line. A value of nil means no limit. */);
23059 Vline_number_display_limit = Qnil;
23060
23061 DEFVAR_INT ("line-number-display-limit-width",
23062 &line_number_display_limit_width,
23063 doc: /* *Maximum line width (in characters) for line number display.
23064 If the average length of the lines near point is bigger than this, then the
23065 line number may be omitted from the mode line. */);
23066 line_number_display_limit_width = 200;
23067
23068 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23069 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23070 highlight_nonselected_windows = 0;
23071
23072 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23073 doc: /* Non-nil if more than one frame is visible on this display.
23074 Minibuffer-only frames don't count, but iconified frames do.
23075 This variable is not guaranteed to be accurate except while processing
23076 `frame-title-format' and `icon-title-format'. */);
23077
23078 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23079 doc: /* Template for displaying the title bar of visible frames.
23080 \(Assuming the window manager supports this feature.)
23081 This variable has the same structure as `mode-line-format' (which see),
23082 and is used only on frames for which no explicit name has been set
23083 \(see `modify-frame-parameters'). */);
23084
23085 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23086 doc: /* Template for displaying the title bar of an iconified frame.
23087 \(Assuming the window manager supports this feature.)
23088 This variable has the same structure as `mode-line-format' (which see),
23089 and is used only on frames for which no explicit name has been set
23090 \(see `modify-frame-parameters'). */);
23091 Vicon_title_format
23092 = Vframe_title_format
23093 = Fcons (intern ("multiple-frames"),
23094 Fcons (build_string ("%b"),
23095 Fcons (Fcons (empty_string,
23096 Fcons (intern ("invocation-name"),
23097 Fcons (build_string ("@"),
23098 Fcons (intern ("system-name"),
23099 Qnil)))),
23100 Qnil)));
23101
23102 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23103 doc: /* Maximum number of lines to keep in the message log buffer.
23104 If nil, disable message logging. If t, log messages but don't truncate
23105 the buffer when it becomes large. */);
23106 Vmessage_log_max = make_number (50);
23107
23108 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23109 doc: /* Functions called before redisplay, if window sizes have changed.
23110 The value should be a list of functions that take one argument.
23111 Just before redisplay, for each frame, if any of its windows have changed
23112 size since the last redisplay, or have been split or deleted,
23113 all the functions in the list are called, with the frame as argument. */);
23114 Vwindow_size_change_functions = Qnil;
23115
23116 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23117 doc: /* List of functions to call before redisplaying a window with scrolling.
23118 Each function is called with two arguments, the window
23119 and its new display-start position. Note that the value of `window-end'
23120 is not valid when these functions are called. */);
23121 Vwindow_scroll_functions = Qnil;
23122
23123 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23124 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23125 mouse_autoselect_window = 0;
23126
23127 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23128 doc: /* *Non-nil means automatically resize tool-bars.
23129 This increases a tool-bar's height if not all tool-bar items are visible.
23130 It decreases a tool-bar's height when it would display blank lines
23131 otherwise. */);
23132 auto_resize_tool_bars_p = 1;
23133
23134 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23135 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23136 auto_raise_tool_bar_buttons_p = 1;
23137
23138 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23139 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23140 make_cursor_line_fully_visible_p = 1;
23141
23142 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23143 doc: /* *Margin around tool-bar buttons in pixels.
23144 If an integer, use that for both horizontal and vertical margins.
23145 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23146 HORZ specifying the horizontal margin, and VERT specifying the
23147 vertical margin. */);
23148 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23149
23150 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23151 doc: /* *Relief thickness of tool-bar buttons. */);
23152 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23153
23154 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23155 doc: /* List of functions to call to fontify regions of text.
23156 Each function is called with one argument POS. Functions must
23157 fontify a region starting at POS in the current buffer, and give
23158 fontified regions the property `fontified'. */);
23159 Vfontification_functions = Qnil;
23160 Fmake_variable_buffer_local (Qfontification_functions);
23161
23162 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23163 &unibyte_display_via_language_environment,
23164 doc: /* *Non-nil means display unibyte text according to language environment.
23165 Specifically this means that unibyte non-ASCII characters
23166 are displayed by converting them to the equivalent multibyte characters
23167 according to the current language environment. As a result, they are
23168 displayed according to the current fontset. */);
23169 unibyte_display_via_language_environment = 0;
23170
23171 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23172 doc: /* *Maximum height for resizing mini-windows.
23173 If a float, it specifies a fraction of the mini-window frame's height.
23174 If an integer, it specifies a number of lines. */);
23175 Vmax_mini_window_height = make_float (0.25);
23176
23177 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23178 doc: /* *How to resize mini-windows.
23179 A value of nil means don't automatically resize mini-windows.
23180 A value of t means resize them to fit the text displayed in them.
23181 A value of `grow-only', the default, means let mini-windows grow
23182 only, until their display becomes empty, at which point the windows
23183 go back to their normal size. */);
23184 Vresize_mini_windows = Qgrow_only;
23185
23186 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23187 doc: /* Alist specifying how to blink the cursor off.
23188 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23189 `cursor-type' frame-parameter or variable equals ON-STATE,
23190 comparing using `equal', Emacs uses OFF-STATE to specify
23191 how to blink it off. */);
23192 Vblink_cursor_alist = Qnil;
23193
23194 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23195 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23196 automatic_hscrolling_p = 1;
23197
23198 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23199 doc: /* *How many columns away from the window edge point is allowed to get
23200 before automatic hscrolling will horizontally scroll the window. */);
23201 hscroll_margin = 5;
23202
23203 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23204 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23205 When point is less than `automatic-hscroll-margin' columns from the window
23206 edge, automatic hscrolling will scroll the window by the amount of columns
23207 determined by this variable. If its value is a positive integer, scroll that
23208 many columns. If it's a positive floating-point number, it specifies the
23209 fraction of the window's width to scroll. If it's nil or zero, point will be
23210 centered horizontally after the scroll. Any other value, including negative
23211 numbers, are treated as if the value were zero.
23212
23213 Automatic hscrolling always moves point outside the scroll margin, so if
23214 point was more than scroll step columns inside the margin, the window will
23215 scroll more than the value given by the scroll step.
23216
23217 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23218 and `scroll-right' overrides this variable's effect. */);
23219 Vhscroll_step = make_number (0);
23220
23221 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23222 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23223 Bind this around calls to `message' to let it take effect. */);
23224 message_truncate_lines = 0;
23225
23226 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23227 doc: /* Normal hook run to update the menu bar definitions.
23228 Redisplay runs this hook before it redisplays the menu bar.
23229 This is used to update submenus such as Buffers,
23230 whose contents depend on various data. */);
23231 Vmenu_bar_update_hook = Qnil;
23232
23233 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23234 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23235 inhibit_menubar_update = 0;
23236
23237 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23238 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23239 inhibit_eval_during_redisplay = 0;
23240
23241 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23242 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23243 inhibit_free_realized_faces = 0;
23244
23245 #if GLYPH_DEBUG
23246 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23247 doc: /* Inhibit try_window_id display optimization. */);
23248 inhibit_try_window_id = 0;
23249
23250 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23251 doc: /* Inhibit try_window_reusing display optimization. */);
23252 inhibit_try_window_reusing = 0;
23253
23254 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23255 doc: /* Inhibit try_cursor_movement display optimization. */);
23256 inhibit_try_cursor_movement = 0;
23257 #endif /* GLYPH_DEBUG */
23258 }
23259
23260
23261 /* Initialize this module when Emacs starts. */
23262
23263 void
23264 init_xdisp ()
23265 {
23266 Lisp_Object root_window;
23267 struct window *mini_w;
23268
23269 current_header_line_height = current_mode_line_height = -1;
23270
23271 CHARPOS (this_line_start_pos) = 0;
23272
23273 mini_w = XWINDOW (minibuf_window);
23274 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23275
23276 if (!noninteractive)
23277 {
23278 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23279 int i;
23280
23281 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23282 set_window_height (root_window,
23283 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23284 0);
23285 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23286 set_window_height (minibuf_window, 1, 0);
23287
23288 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23289 mini_w->total_cols = make_number (FRAME_COLS (f));
23290
23291 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23292 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23293 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23294
23295 /* The default ellipsis glyphs `...'. */
23296 for (i = 0; i < 3; ++i)
23297 default_invis_vector[i] = make_number ('.');
23298 }
23299
23300 {
23301 /* Allocate the buffer for frame titles.
23302 Also used for `format-mode-line'. */
23303 int size = 100;
23304 mode_line_noprop_buf = (char *) xmalloc (size);
23305 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23306 mode_line_noprop_ptr = mode_line_noprop_buf;
23307 mode_line_target = MODE_LINE_DISPLAY;
23308 }
23309
23310 help_echo_showing_p = 0;
23311 }
23312
23313
23314 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23315 (do not change this comment) */