]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(update_window_fringes): Provide sensible fall-back
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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, Qtotal;
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 #ifdef HAVE_WINDOW_SYSTEM
322 extern Lisp_Object Voverflow_newline_into_fringe;
323
324 /* Test if overflow newline into fringe. Called with iterator IT
325 at or past right window margin, and with IT->current_x set. */
326
327 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
328 (!NILP (Voverflow_newline_into_fringe) \
329 && FRAME_WINDOW_P (it->f) \
330 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
331 && it->current_x == it->last_visible_x)
332
333 #endif /* HAVE_WINDOW_SYSTEM */
334
335 /* Non-nil means show the text cursor in void text areas
336 i.e. in blank areas after eol and eob. This used to be
337 the default in 21.3. */
338
339 Lisp_Object Vvoid_text_area_pointer;
340
341 /* Name of the face used to highlight trailing whitespace. */
342
343 Lisp_Object Qtrailing_whitespace;
344
345 /* The symbol `image' which is the car of the lists used to represent
346 images in Lisp. */
347
348 Lisp_Object Qimage;
349
350 /* The image map types. */
351 Lisp_Object QCmap, QCpointer;
352 Lisp_Object Qrect, Qcircle, Qpoly;
353
354 /* Non-zero means print newline to stdout before next mini-buffer
355 message. */
356
357 int noninteractive_need_newline;
358
359 /* Non-zero means print newline to message log before next message. */
360
361 static int message_log_need_newline;
362
363 /* Three markers that message_dolog uses.
364 It could allocate them itself, but that causes trouble
365 in handling memory-full errors. */
366 static Lisp_Object message_dolog_marker1;
367 static Lisp_Object message_dolog_marker2;
368 static Lisp_Object message_dolog_marker3;
369 \f
370 /* The buffer position of the first character appearing entirely or
371 partially on the line of the selected window which contains the
372 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
373 redisplay optimization in redisplay_internal. */
374
375 static struct text_pos this_line_start_pos;
376
377 /* Number of characters past the end of the line above, including the
378 terminating newline. */
379
380 static struct text_pos this_line_end_pos;
381
382 /* The vertical positions and the height of this line. */
383
384 static int this_line_vpos;
385 static int this_line_y;
386 static int this_line_pixel_height;
387
388 /* X position at which this display line starts. Usually zero;
389 negative if first character is partially visible. */
390
391 static int this_line_start_x;
392
393 /* Buffer that this_line_.* variables are referring to. */
394
395 static struct buffer *this_line_buffer;
396
397 /* Nonzero means truncate lines in all windows less wide than the
398 frame. */
399
400 int truncate_partial_width_windows;
401
402 /* A flag to control how to display unibyte 8-bit character. */
403
404 int unibyte_display_via_language_environment;
405
406 /* Nonzero means we have more than one non-mini-buffer-only frame.
407 Not guaranteed to be accurate except while parsing
408 frame-title-format. */
409
410 int multiple_frames;
411
412 Lisp_Object Vglobal_mode_string;
413
414
415 /* List of variables (symbols) which hold markers for overlay arrows.
416 The symbols on this list are examined during redisplay to determine
417 where to display overlay arrows. */
418
419 Lisp_Object Voverlay_arrow_variable_list;
420
421 /* Marker for where to display an arrow on top of the buffer text. */
422
423 Lisp_Object Voverlay_arrow_position;
424
425 /* String to display for the arrow. Only used on terminal frames. */
426
427 Lisp_Object Voverlay_arrow_string;
428
429 /* Values of those variables at last redisplay are stored as
430 properties on `overlay-arrow-position' symbol. However, if
431 Voverlay_arrow_position is a marker, last-arrow-position is its
432 numerical position. */
433
434 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
435
436 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
437 properties on a symbol in overlay-arrow-variable-list. */
438
439 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
440
441 /* Like mode-line-format, but for the title bar on a visible frame. */
442
443 Lisp_Object Vframe_title_format;
444
445 /* Like mode-line-format, but for the title bar on an iconified frame. */
446
447 Lisp_Object Vicon_title_format;
448
449 /* List of functions to call when a window's size changes. These
450 functions get one arg, a frame on which one or more windows' sizes
451 have changed. */
452
453 static Lisp_Object Vwindow_size_change_functions;
454
455 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
456
457 /* Nonzero if overlay arrow has been displayed once in this window. */
458
459 static int overlay_arrow_seen;
460
461 /* Nonzero means highlight the region even in nonselected windows. */
462
463 int highlight_nonselected_windows;
464
465 /* If cursor motion alone moves point off frame, try scrolling this
466 many lines up or down if that will bring it back. */
467
468 static EMACS_INT scroll_step;
469
470 /* Nonzero means scroll just far enough to bring point back on the
471 screen, when appropriate. */
472
473 static EMACS_INT scroll_conservatively;
474
475 /* Recenter the window whenever point gets within this many lines of
476 the top or bottom of the window. This value is translated into a
477 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
478 that there is really a fixed pixel height scroll margin. */
479
480 EMACS_INT scroll_margin;
481
482 /* Number of windows showing the buffer of the selected window (or
483 another buffer with the same base buffer). keyboard.c refers to
484 this. */
485
486 int buffer_shared;
487
488 /* Vector containing glyphs for an ellipsis `...'. */
489
490 static Lisp_Object default_invis_vector[3];
491
492 /* Zero means display the mode-line/header-line/menu-bar in the default face
493 (this slightly odd definition is for compatibility with previous versions
494 of emacs), non-zero means display them using their respective faces.
495
496 This variable is deprecated. */
497
498 int mode_line_inverse_video;
499
500 /* Prompt to display in front of the mini-buffer contents. */
501
502 Lisp_Object minibuf_prompt;
503
504 /* Width of current mini-buffer prompt. Only set after display_line
505 of the line that contains the prompt. */
506
507 int minibuf_prompt_width;
508
509 /* This is the window where the echo area message was displayed. It
510 is always a mini-buffer window, but it may not be the same window
511 currently active as a mini-buffer. */
512
513 Lisp_Object echo_area_window;
514
515 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
516 pushes the current message and the value of
517 message_enable_multibyte on the stack, the function restore_message
518 pops the stack and displays MESSAGE again. */
519
520 Lisp_Object Vmessage_stack;
521
522 /* Nonzero means multibyte characters were enabled when the echo area
523 message was specified. */
524
525 int message_enable_multibyte;
526
527 /* Nonzero if we should redraw the mode lines on the next redisplay. */
528
529 int update_mode_lines;
530
531 /* Nonzero if window sizes or contents have changed since last
532 redisplay that finished. */
533
534 int windows_or_buffers_changed;
535
536 /* Nonzero means a frame's cursor type has been changed. */
537
538 int cursor_type_changed;
539
540 /* Nonzero after display_mode_line if %l was used and it displayed a
541 line number. */
542
543 int line_number_displayed;
544
545 /* Maximum buffer size for which to display line numbers. */
546
547 Lisp_Object Vline_number_display_limit;
548
549 /* Line width to consider when repositioning for line number display. */
550
551 static EMACS_INT line_number_display_limit_width;
552
553 /* Number of lines to keep in the message log buffer. t means
554 infinite. nil means don't log at all. */
555
556 Lisp_Object Vmessage_log_max;
557
558 /* The name of the *Messages* buffer, a string. */
559
560 static Lisp_Object Vmessages_buffer_name;
561
562 /* Current, index 0, and last displayed echo area message. Either
563 buffers from echo_buffers, or nil to indicate no message. */
564
565 Lisp_Object echo_area_buffer[2];
566
567 /* The buffers referenced from echo_area_buffer. */
568
569 static Lisp_Object echo_buffer[2];
570
571 /* A vector saved used in with_area_buffer to reduce consing. */
572
573 static Lisp_Object Vwith_echo_area_save_vector;
574
575 /* Non-zero means display_echo_area should display the last echo area
576 message again. Set by redisplay_preserve_echo_area. */
577
578 static int display_last_displayed_message_p;
579
580 /* Nonzero if echo area is being used by print; zero if being used by
581 message. */
582
583 int message_buf_print;
584
585 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
586
587 Lisp_Object Qinhibit_menubar_update;
588 int inhibit_menubar_update;
589
590 /* Maximum height for resizing mini-windows. Either a float
591 specifying a fraction of the available height, or an integer
592 specifying a number of lines. */
593
594 Lisp_Object Vmax_mini_window_height;
595
596 /* Non-zero means messages should be displayed with truncated
597 lines instead of being continued. */
598
599 int message_truncate_lines;
600 Lisp_Object Qmessage_truncate_lines;
601
602 /* Set to 1 in clear_message to make redisplay_internal aware
603 of an emptied echo area. */
604
605 static int message_cleared_p;
606
607 /* Non-zero means we want a hollow cursor in windows that are not
608 selected. Zero means there's no cursor in such windows. */
609
610 Lisp_Object Vcursor_in_non_selected_windows;
611 Lisp_Object Qcursor_in_non_selected_windows;
612
613 /* How to blink the default frame cursor off. */
614 Lisp_Object Vblink_cursor_alist;
615
616 /* A scratch glyph row with contents used for generating truncation
617 glyphs. Also used in direct_output_for_insert. */
618
619 #define MAX_SCRATCH_GLYPHS 100
620 struct glyph_row scratch_glyph_row;
621 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
622
623 /* Ascent and height of the last line processed by move_it_to. */
624
625 static int last_max_ascent, last_height;
626
627 /* Non-zero if there's a help-echo in the echo area. */
628
629 int help_echo_showing_p;
630
631 /* If >= 0, computed, exact values of mode-line and header-line height
632 to use in the macros CURRENT_MODE_LINE_HEIGHT and
633 CURRENT_HEADER_LINE_HEIGHT. */
634
635 int current_mode_line_height, current_header_line_height;
636
637 /* The maximum distance to look ahead for text properties. Values
638 that are too small let us call compute_char_face and similar
639 functions too often which is expensive. Values that are too large
640 let us call compute_char_face and alike too often because we
641 might not be interested in text properties that far away. */
642
643 #define TEXT_PROP_DISTANCE_LIMIT 100
644
645 #if GLYPH_DEBUG
646
647 /* Variables to turn off display optimizations from Lisp. */
648
649 int inhibit_try_window_id, inhibit_try_window_reusing;
650 int inhibit_try_cursor_movement;
651
652 /* Non-zero means print traces of redisplay if compiled with
653 GLYPH_DEBUG != 0. */
654
655 int trace_redisplay_p;
656
657 #endif /* GLYPH_DEBUG */
658
659 #ifdef DEBUG_TRACE_MOVE
660 /* Non-zero means trace with TRACE_MOVE to stderr. */
661 int trace_move;
662
663 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
664 #else
665 #define TRACE_MOVE(x) (void) 0
666 #endif
667
668 /* Non-zero means automatically scroll windows horizontally to make
669 point visible. */
670
671 int automatic_hscrolling_p;
672
673 /* How close to the margin can point get before the window is scrolled
674 horizontally. */
675 EMACS_INT hscroll_margin;
676
677 /* How much to scroll horizontally when point is inside the above margin. */
678 Lisp_Object Vhscroll_step;
679
680 /* The variable `resize-mini-windows'. If nil, don't resize
681 mini-windows. If t, always resize them to fit the text they
682 display. If `grow-only', let mini-windows grow only until they
683 become empty. */
684
685 Lisp_Object Vresize_mini_windows;
686
687 /* Buffer being redisplayed -- for redisplay_window_error. */
688
689 struct buffer *displayed_buffer;
690
691 /* Value returned from text property handlers (see below). */
692
693 enum prop_handled
694 {
695 HANDLED_NORMALLY,
696 HANDLED_RECOMPUTE_PROPS,
697 HANDLED_OVERLAY_STRING_CONSUMED,
698 HANDLED_RETURN
699 };
700
701 /* A description of text properties that redisplay is interested
702 in. */
703
704 struct props
705 {
706 /* The name of the property. */
707 Lisp_Object *name;
708
709 /* A unique index for the property. */
710 enum prop_idx idx;
711
712 /* A handler function called to set up iterator IT from the property
713 at IT's current position. Value is used to steer handle_stop. */
714 enum prop_handled (*handler) P_ ((struct it *it));
715 };
716
717 static enum prop_handled handle_face_prop P_ ((struct it *));
718 static enum prop_handled handle_invisible_prop P_ ((struct it *));
719 static enum prop_handled handle_display_prop P_ ((struct it *));
720 static enum prop_handled handle_composition_prop P_ ((struct it *));
721 static enum prop_handled handle_overlay_change P_ ((struct it *));
722 static enum prop_handled handle_fontified_prop P_ ((struct it *));
723
724 /* Properties handled by iterators. */
725
726 static struct props it_props[] =
727 {
728 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
729 /* Handle `face' before `display' because some sub-properties of
730 `display' need to know the face. */
731 {&Qface, FACE_PROP_IDX, handle_face_prop},
732 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
733 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
734 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
735 {NULL, 0, NULL}
736 };
737
738 /* Value is the position described by X. If X is a marker, value is
739 the marker_position of X. Otherwise, value is X. */
740
741 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
742
743 /* Enumeration returned by some move_it_.* functions internally. */
744
745 enum move_it_result
746 {
747 /* Not used. Undefined value. */
748 MOVE_UNDEFINED,
749
750 /* Move ended at the requested buffer position or ZV. */
751 MOVE_POS_MATCH_OR_ZV,
752
753 /* Move ended at the requested X pixel position. */
754 MOVE_X_REACHED,
755
756 /* Move within a line ended at the end of a line that must be
757 continued. */
758 MOVE_LINE_CONTINUED,
759
760 /* Move within a line ended at the end of a line that would
761 be displayed truncated. */
762 MOVE_LINE_TRUNCATED,
763
764 /* Move within a line ended at a line end. */
765 MOVE_NEWLINE_OR_CR
766 };
767
768 /* This counter is used to clear the face cache every once in a while
769 in redisplay_internal. It is incremented for each redisplay.
770 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
771 cleared. */
772
773 #define CLEAR_FACE_CACHE_COUNT 500
774 static int clear_face_cache_count;
775
776 /* Record the previous terminal frame we displayed. */
777
778 static struct frame *previous_terminal_frame;
779
780 /* Non-zero while redisplay_internal is in progress. */
781
782 int redisplaying_p;
783
784 /* Non-zero means don't free realized faces. Bound while freeing
785 realized faces is dangerous because glyph matrices might still
786 reference them. */
787
788 int inhibit_free_realized_faces;
789 Lisp_Object Qinhibit_free_realized_faces;
790
791 /* If a string, XTread_socket generates an event to display that string.
792 (The display is done in read_char.) */
793
794 Lisp_Object help_echo_string;
795 Lisp_Object help_echo_window;
796 Lisp_Object help_echo_object;
797 int help_echo_pos;
798
799 /* Temporary variable for XTread_socket. */
800
801 Lisp_Object previous_help_echo_string;
802
803 /* Null glyph slice */
804
805 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
806
807 \f
808 /* Function prototypes. */
809
810 static void setup_for_ellipsis P_ ((struct it *));
811 static void mark_window_display_accurate_1 P_ ((struct window *, int));
812 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
813 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
814 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
815 static int redisplay_mode_lines P_ ((Lisp_Object, int));
816 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
817
818 #if 0
819 static int invisible_text_between_p P_ ((struct it *, int, int));
820 #endif
821
822 static int next_element_from_ellipsis P_ ((struct it *));
823 static void pint2str P_ ((char *, int, int));
824 static void pint2hrstr P_ ((char *, int, int));
825 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
826 struct text_pos));
827 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
828 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
829 static void store_frame_title_char P_ ((char));
830 static int store_frame_title P_ ((const unsigned char *, int, int));
831 static void x_consider_frame_title P_ ((Lisp_Object));
832 static void handle_stop P_ ((struct it *));
833 static int tool_bar_lines_needed P_ ((struct frame *));
834 static int single_display_prop_intangible_p P_ ((Lisp_Object));
835 static void ensure_echo_area_buffers P_ ((void));
836 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
837 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
838 static int with_echo_area_buffer P_ ((struct window *, int,
839 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
840 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static void clear_garbaged_frames P_ ((void));
842 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
843 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
845 static int display_echo_area P_ ((struct window *));
846 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
847 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
848 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
849 static int string_char_and_length P_ ((const unsigned char *, int, int *));
850 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
851 struct text_pos));
852 static int compute_window_start_on_continuation_line P_ ((struct window *));
853 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
854 static void insert_left_trunc_glyphs P_ ((struct it *));
855 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
856 Lisp_Object));
857 static void extend_face_to_end_of_line P_ ((struct it *));
858 static int append_space_for_newline P_ ((struct it *, int));
859 static int make_cursor_line_fully_visible P_ ((struct window *, int));
860 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
861 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
862 static int trailing_whitespace_p P_ ((int));
863 static int message_log_check_duplicate P_ ((int, int, int, int));
864 static void push_it P_ ((struct it *));
865 static void pop_it P_ ((struct it *));
866 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
867 static void select_frame_for_redisplay P_ ((Lisp_Object));
868 static void redisplay_internal P_ ((int));
869 static int echo_area_display P_ ((int));
870 static void redisplay_windows P_ ((Lisp_Object));
871 static void redisplay_window P_ ((Lisp_Object, int));
872 static Lisp_Object redisplay_window_error ();
873 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
874 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
875 static void update_menu_bar P_ ((struct frame *, int));
876 static int try_window_reusing_current_matrix P_ ((struct window *));
877 static int try_window_id P_ ((struct window *));
878 static int display_line P_ ((struct it *));
879 static int display_mode_lines P_ ((struct window *));
880 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
881 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
882 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
883 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
884 static void display_menu_bar P_ ((struct window *));
885 static int display_count_lines P_ ((int, int, int, int, int *));
886 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
887 int, int, struct it *, int, int, int, int));
888 static void compute_line_metrics P_ ((struct it *));
889 static void run_redisplay_end_trigger_hook P_ ((struct it *));
890 static int get_overlay_strings P_ ((struct it *, int));
891 static void next_overlay_string P_ ((struct it *));
892 static void reseat P_ ((struct it *, struct text_pos, int));
893 static void reseat_1 P_ ((struct it *, struct text_pos, int));
894 static void back_to_previous_visible_line_start P_ ((struct it *));
895 void reseat_at_previous_visible_line_start P_ ((struct it *));
896 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
897 static int next_element_from_display_vector P_ ((struct it *));
898 static int next_element_from_string P_ ((struct it *));
899 static int next_element_from_c_string P_ ((struct it *));
900 static int next_element_from_buffer P_ ((struct it *));
901 static int next_element_from_composition P_ ((struct it *));
902 static int next_element_from_image P_ ((struct it *));
903 static int next_element_from_stretch P_ ((struct it *));
904 static void load_overlay_strings P_ ((struct it *, int));
905 static int init_from_display_pos P_ ((struct it *, struct window *,
906 struct display_pos *));
907 static void reseat_to_string P_ ((struct it *, unsigned char *,
908 Lisp_Object, int, int, int, int));
909 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
910 int, int, int));
911 void move_it_vertically_backward P_ ((struct it *, int));
912 static void init_to_row_start P_ ((struct it *, struct window *,
913 struct glyph_row *));
914 static int init_to_row_end P_ ((struct it *, struct window *,
915 struct glyph_row *));
916 static void back_to_previous_line_start P_ ((struct it *));
917 static int forward_to_next_line_start P_ ((struct it *, int *));
918 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
919 Lisp_Object, int));
920 static struct text_pos string_pos P_ ((int, Lisp_Object));
921 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
922 static int number_of_chars P_ ((unsigned char *, int));
923 static void compute_stop_pos P_ ((struct it *));
924 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
925 Lisp_Object));
926 static int face_before_or_after_it_pos P_ ((struct it *, int));
927 static int next_overlay_change P_ ((int));
928 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
929 Lisp_Object, struct text_pos *,
930 int));
931 static int underlying_face_id P_ ((struct it *));
932 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
933 struct window *));
934
935 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
936 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
937
938 #ifdef HAVE_WINDOW_SYSTEM
939
940 static void update_tool_bar P_ ((struct frame *, int));
941 static void build_desired_tool_bar_string P_ ((struct frame *f));
942 static int redisplay_tool_bar P_ ((struct frame *));
943 static void display_tool_bar_line P_ ((struct it *));
944 static void notice_overwritten_cursor P_ ((struct window *,
945 enum glyph_row_area,
946 int, int, int, int));
947
948
949
950 #endif /* HAVE_WINDOW_SYSTEM */
951
952 \f
953 /***********************************************************************
954 Window display dimensions
955 ***********************************************************************/
956
957 /* Return the bottom boundary y-position for text lines in window W.
958 This is the first y position at which a line cannot start.
959 It is relative to the top of the window.
960
961 This is the height of W minus the height of a mode line, if any. */
962
963 INLINE int
964 window_text_bottom_y (w)
965 struct window *w;
966 {
967 int height = WINDOW_TOTAL_HEIGHT (w);
968
969 if (WINDOW_WANTS_MODELINE_P (w))
970 height -= CURRENT_MODE_LINE_HEIGHT (w);
971 return height;
972 }
973
974 /* Return the pixel width of display area AREA of window W. AREA < 0
975 means return the total width of W, not including fringes to
976 the left and right of the window. */
977
978 INLINE int
979 window_box_width (w, area)
980 struct window *w;
981 int area;
982 {
983 int cols = XFASTINT (w->total_cols);
984 int pixels = 0;
985
986 if (!w->pseudo_window_p)
987 {
988 cols -= WINDOW_SCROLL_BAR_COLS (w);
989
990 if (area == TEXT_AREA)
991 {
992 if (INTEGERP (w->left_margin_cols))
993 cols -= XFASTINT (w->left_margin_cols);
994 if (INTEGERP (w->right_margin_cols))
995 cols -= XFASTINT (w->right_margin_cols);
996 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
997 }
998 else if (area == LEFT_MARGIN_AREA)
999 {
1000 cols = (INTEGERP (w->left_margin_cols)
1001 ? XFASTINT (w->left_margin_cols) : 0);
1002 pixels = 0;
1003 }
1004 else if (area == RIGHT_MARGIN_AREA)
1005 {
1006 cols = (INTEGERP (w->right_margin_cols)
1007 ? XFASTINT (w->right_margin_cols) : 0);
1008 pixels = 0;
1009 }
1010 }
1011
1012 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1013 }
1014
1015
1016 /* Return the pixel height of the display area of window W, not
1017 including mode lines of W, if any. */
1018
1019 INLINE int
1020 window_box_height (w)
1021 struct window *w;
1022 {
1023 struct frame *f = XFRAME (w->frame);
1024 int height = WINDOW_TOTAL_HEIGHT (w);
1025
1026 xassert (height >= 0);
1027
1028 /* Note: the code below that determines the mode-line/header-line
1029 height is essentially the same as that contained in the macro
1030 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1031 the appropriate glyph row has its `mode_line_p' flag set,
1032 and if it doesn't, uses estimate_mode_line_height instead. */
1033
1034 if (WINDOW_WANTS_MODELINE_P (w))
1035 {
1036 struct glyph_row *ml_row
1037 = (w->current_matrix && w->current_matrix->rows
1038 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1039 : 0);
1040 if (ml_row && ml_row->mode_line_p)
1041 height -= ml_row->height;
1042 else
1043 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1044 }
1045
1046 if (WINDOW_WANTS_HEADER_LINE_P (w))
1047 {
1048 struct glyph_row *hl_row
1049 = (w->current_matrix && w->current_matrix->rows
1050 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1051 : 0);
1052 if (hl_row && hl_row->mode_line_p)
1053 height -= hl_row->height;
1054 else
1055 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1056 }
1057
1058 /* With a very small font and a mode-line that's taller than
1059 default, we might end up with a negative height. */
1060 return max (0, height);
1061 }
1062
1063 /* Return the window-relative coordinate of the left edge of display
1064 area AREA of window W. AREA < 0 means return the left edge of the
1065 whole window, to the right of the left fringe of W. */
1066
1067 INLINE int
1068 window_box_left_offset (w, area)
1069 struct window *w;
1070 int area;
1071 {
1072 int x;
1073
1074 if (w->pseudo_window_p)
1075 return 0;
1076
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1078
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1087 ? 0
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1092
1093 return x;
1094 }
1095
1096
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the left edge of the
1099 whole window, to the left of the right fringe of W. */
1100
1101 INLINE int
1102 window_box_right_offset (w, area)
1103 struct window *w;
1104 int area;
1105 {
1106 return window_box_left_offset (w, area) + window_box_width (w, area);
1107 }
1108
1109 /* Return the frame-relative coordinate of the left edge of display
1110 area AREA of window W. AREA < 0 means return the left edge of the
1111 whole window, to the right of the left fringe of W. */
1112
1113 INLINE int
1114 window_box_left (w, area)
1115 struct window *w;
1116 int area;
1117 {
1118 struct frame *f = XFRAME (w->frame);
1119 int x;
1120
1121 if (w->pseudo_window_p)
1122 return FRAME_INTERNAL_BORDER_WIDTH (f);
1123
1124 x = (WINDOW_LEFT_EDGE_X (w)
1125 + window_box_left_offset (w, area));
1126
1127 return x;
1128 }
1129
1130
1131 /* Return the frame-relative coordinate of the right edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the left of the right fringe of W. */
1134
1135 INLINE int
1136 window_box_right (w, area)
1137 struct window *w;
1138 int area;
1139 {
1140 return window_box_left (w, area) + window_box_width (w, area);
1141 }
1142
1143 /* Get the bounding box of the display area AREA of window W, without
1144 mode lines, in frame-relative coordinates. AREA < 0 means the
1145 whole window, not including the left and right fringes of
1146 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1147 coordinates of the upper-left corner of the box. Return in
1148 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1149
1150 INLINE void
1151 window_box (w, area, box_x, box_y, box_width, box_height)
1152 struct window *w;
1153 int area;
1154 int *box_x, *box_y, *box_width, *box_height;
1155 {
1156 if (box_width)
1157 *box_width = window_box_width (w, area);
1158 if (box_height)
1159 *box_height = window_box_height (w);
1160 if (box_x)
1161 *box_x = window_box_left (w, area);
1162 if (box_y)
1163 {
1164 *box_y = WINDOW_TOP_EDGE_Y (w);
1165 if (WINDOW_WANTS_HEADER_LINE_P (w))
1166 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1167 }
1168 }
1169
1170
1171 /* Get the bounding box of the display area AREA of window W, without
1172 mode lines. AREA < 0 means the whole window, not including the
1173 left and right fringe of the window. Return in *TOP_LEFT_X
1174 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1175 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1176 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1177 box. */
1178
1179 INLINE void
1180 window_box_edges (w, area, top_left_x, top_left_y,
1181 bottom_right_x, bottom_right_y)
1182 struct window *w;
1183 int area;
1184 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1185 {
1186 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1187 bottom_right_y);
1188 *bottom_right_x += *top_left_x;
1189 *bottom_right_y += *top_left_y;
1190 }
1191
1192
1193 \f
1194 /***********************************************************************
1195 Utilities
1196 ***********************************************************************/
1197
1198 /* Return the bottom y-position of the line the iterator IT is in.
1199 This can modify IT's settings. */
1200
1201 int
1202 line_bottom_y (it)
1203 struct it *it;
1204 {
1205 int line_height = it->max_ascent + it->max_descent;
1206 int line_top_y = it->current_y;
1207
1208 if (line_height == 0)
1209 {
1210 if (last_height)
1211 line_height = last_height;
1212 else if (IT_CHARPOS (*it) < ZV)
1213 {
1214 move_it_by_lines (it, 1, 1);
1215 line_height = (it->max_ascent || it->max_descent
1216 ? it->max_ascent + it->max_descent
1217 : last_height);
1218 }
1219 else
1220 {
1221 struct glyph_row *row = it->glyph_row;
1222
1223 /* Use the default character height. */
1224 it->glyph_row = NULL;
1225 it->what = IT_CHARACTER;
1226 it->c = ' ';
1227 it->len = 1;
1228 PRODUCE_GLYPHS (it);
1229 line_height = it->ascent + it->descent;
1230 it->glyph_row = row;
1231 }
1232 }
1233
1234 return line_top_y + line_height;
1235 }
1236
1237
1238 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1239 1 if POS is visible and the line containing POS is fully visible.
1240 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1241 and header-lines heights. */
1242
1243 int
1244 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1245 struct window *w;
1246 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1247 {
1248 struct it it;
1249 struct text_pos top;
1250 int visible_p;
1251 struct buffer *old_buffer = NULL;
1252
1253 if (XBUFFER (w->buffer) != current_buffer)
1254 {
1255 old_buffer = current_buffer;
1256 set_buffer_internal_1 (XBUFFER (w->buffer));
1257 }
1258
1259 *fully = visible_p = 0;
1260 SET_TEXT_POS_FROM_MARKER (top, w->start);
1261
1262 /* Compute exact mode line heights, if requested. */
1263 if (exact_mode_line_heights_p)
1264 {
1265 if (WINDOW_WANTS_MODELINE_P (w))
1266 current_mode_line_height
1267 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1268 current_buffer->mode_line_format);
1269
1270 if (WINDOW_WANTS_HEADER_LINE_P (w))
1271 current_header_line_height
1272 = display_mode_line (w, HEADER_LINE_FACE_ID,
1273 current_buffer->header_line_format);
1274 }
1275
1276 start_display (&it, w, top);
1277 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1278 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1279
1280 /* Note that we may overshoot because of invisible text. */
1281 if (IT_CHARPOS (it) >= charpos)
1282 {
1283 int top_y = it.current_y;
1284 int bottom_y = line_bottom_y (&it);
1285 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1286
1287 if (top_y < window_top_y)
1288 visible_p = bottom_y > window_top_y;
1289 else if (top_y < it.last_visible_y)
1290 {
1291 visible_p = 1;
1292 *fully = bottom_y <= it.last_visible_y;
1293 }
1294 if (visible_p && x)
1295 {
1296 *x = it.current_x;
1297 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1298 }
1299 }
1300 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1301 {
1302 struct it it2;
1303
1304 it2 = it;
1305 move_it_by_lines (&it, 1, 0);
1306 if (charpos < IT_CHARPOS (it))
1307 {
1308 visible_p = 1;
1309 if (x)
1310 {
1311 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1312 *x = it2.current_x;
1313 *y = it2.current_y + it2.max_ascent - it2.ascent;
1314 }
1315 }
1316 }
1317
1318 if (old_buffer)
1319 set_buffer_internal_1 (old_buffer);
1320
1321 current_header_line_height = current_mode_line_height = -1;
1322
1323 return visible_p;
1324 }
1325
1326
1327 /* Return the next character from STR which is MAXLEN bytes long.
1328 Return in *LEN the length of the character. This is like
1329 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1330 we find one, we return a `?', but with the length of the invalid
1331 character. */
1332
1333 static INLINE int
1334 string_char_and_length (str, maxlen, len)
1335 const unsigned char *str;
1336 int maxlen, *len;
1337 {
1338 int c;
1339
1340 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1341 if (!CHAR_VALID_P (c, 1))
1342 /* We may not change the length here because other places in Emacs
1343 don't use this function, i.e. they silently accept invalid
1344 characters. */
1345 c = '?';
1346
1347 return c;
1348 }
1349
1350
1351
1352 /* Given a position POS containing a valid character and byte position
1353 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1354
1355 static struct text_pos
1356 string_pos_nchars_ahead (pos, string, nchars)
1357 struct text_pos pos;
1358 Lisp_Object string;
1359 int nchars;
1360 {
1361 xassert (STRINGP (string) && nchars >= 0);
1362
1363 if (STRING_MULTIBYTE (string))
1364 {
1365 int rest = SBYTES (string) - BYTEPOS (pos);
1366 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1367 int len;
1368
1369 while (nchars--)
1370 {
1371 string_char_and_length (p, rest, &len);
1372 p += len, rest -= len;
1373 xassert (rest >= 0);
1374 CHARPOS (pos) += 1;
1375 BYTEPOS (pos) += len;
1376 }
1377 }
1378 else
1379 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1380
1381 return pos;
1382 }
1383
1384
1385 /* Value is the text position, i.e. character and byte position,
1386 for character position CHARPOS in STRING. */
1387
1388 static INLINE struct text_pos
1389 string_pos (charpos, string)
1390 int charpos;
1391 Lisp_Object string;
1392 {
1393 struct text_pos pos;
1394 xassert (STRINGP (string));
1395 xassert (charpos >= 0);
1396 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1397 return pos;
1398 }
1399
1400
1401 /* Value is a text position, i.e. character and byte position, for
1402 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1403 means recognize multibyte characters. */
1404
1405 static struct text_pos
1406 c_string_pos (charpos, s, multibyte_p)
1407 int charpos;
1408 unsigned char *s;
1409 int multibyte_p;
1410 {
1411 struct text_pos pos;
1412
1413 xassert (s != NULL);
1414 xassert (charpos >= 0);
1415
1416 if (multibyte_p)
1417 {
1418 int rest = strlen (s), len;
1419
1420 SET_TEXT_POS (pos, 0, 0);
1421 while (charpos--)
1422 {
1423 string_char_and_length (s, rest, &len);
1424 s += len, rest -= len;
1425 xassert (rest >= 0);
1426 CHARPOS (pos) += 1;
1427 BYTEPOS (pos) += len;
1428 }
1429 }
1430 else
1431 SET_TEXT_POS (pos, charpos, charpos);
1432
1433 return pos;
1434 }
1435
1436
1437 /* Value is the number of characters in C string S. MULTIBYTE_P
1438 non-zero means recognize multibyte characters. */
1439
1440 static int
1441 number_of_chars (s, multibyte_p)
1442 unsigned char *s;
1443 int multibyte_p;
1444 {
1445 int nchars;
1446
1447 if (multibyte_p)
1448 {
1449 int rest = strlen (s), len;
1450 unsigned char *p = (unsigned char *) s;
1451
1452 for (nchars = 0; rest > 0; ++nchars)
1453 {
1454 string_char_and_length (p, rest, &len);
1455 rest -= len, p += len;
1456 }
1457 }
1458 else
1459 nchars = strlen (s);
1460
1461 return nchars;
1462 }
1463
1464
1465 /* Compute byte position NEWPOS->bytepos corresponding to
1466 NEWPOS->charpos. POS is a known position in string STRING.
1467 NEWPOS->charpos must be >= POS.charpos. */
1468
1469 static void
1470 compute_string_pos (newpos, pos, string)
1471 struct text_pos *newpos, pos;
1472 Lisp_Object string;
1473 {
1474 xassert (STRINGP (string));
1475 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1476
1477 if (STRING_MULTIBYTE (string))
1478 *newpos = string_pos_nchars_ahead (pos, string,
1479 CHARPOS (*newpos) - CHARPOS (pos));
1480 else
1481 BYTEPOS (*newpos) = CHARPOS (*newpos);
1482 }
1483
1484 /* EXPORT:
1485 Return an estimation of the pixel height of mode or top lines on
1486 frame F. FACE_ID specifies what line's height to estimate. */
1487
1488 int
1489 estimate_mode_line_height (f, face_id)
1490 struct frame *f;
1491 enum face_id face_id;
1492 {
1493 #ifdef HAVE_WINDOW_SYSTEM
1494 if (FRAME_WINDOW_P (f))
1495 {
1496 int height = FONT_HEIGHT (FRAME_FONT (f));
1497
1498 /* This function is called so early when Emacs starts that the face
1499 cache and mode line face are not yet initialized. */
1500 if (FRAME_FACE_CACHE (f))
1501 {
1502 struct face *face = FACE_FROM_ID (f, face_id);
1503 if (face)
1504 {
1505 if (face->font)
1506 height = FONT_HEIGHT (face->font);
1507 if (face->box_line_width > 0)
1508 height += 2 * face->box_line_width;
1509 }
1510 }
1511
1512 return height;
1513 }
1514 #endif
1515
1516 return 1;
1517 }
1518
1519 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1520 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1521 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1522 not force the value into range. */
1523
1524 void
1525 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1526 FRAME_PTR f;
1527 register int pix_x, pix_y;
1528 int *x, *y;
1529 NativeRectangle *bounds;
1530 int noclip;
1531 {
1532
1533 #ifdef HAVE_WINDOW_SYSTEM
1534 if (FRAME_WINDOW_P (f))
1535 {
1536 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1537 even for negative values. */
1538 if (pix_x < 0)
1539 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1540 if (pix_y < 0)
1541 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1542
1543 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1544 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1545
1546 if (bounds)
1547 STORE_NATIVE_RECT (*bounds,
1548 FRAME_COL_TO_PIXEL_X (f, pix_x),
1549 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1550 FRAME_COLUMN_WIDTH (f) - 1,
1551 FRAME_LINE_HEIGHT (f) - 1);
1552
1553 if (!noclip)
1554 {
1555 if (pix_x < 0)
1556 pix_x = 0;
1557 else if (pix_x > FRAME_TOTAL_COLS (f))
1558 pix_x = FRAME_TOTAL_COLS (f);
1559
1560 if (pix_y < 0)
1561 pix_y = 0;
1562 else if (pix_y > FRAME_LINES (f))
1563 pix_y = FRAME_LINES (f);
1564 }
1565 }
1566 #endif
1567
1568 *x = pix_x;
1569 *y = pix_y;
1570 }
1571
1572
1573 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1574 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1575 can't tell the positions because W's display is not up to date,
1576 return 0. */
1577
1578 int
1579 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1580 struct window *w;
1581 int hpos, vpos;
1582 int *frame_x, *frame_y;
1583 {
1584 #ifdef HAVE_WINDOW_SYSTEM
1585 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1586 {
1587 int success_p;
1588
1589 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1590 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1591
1592 if (display_completed)
1593 {
1594 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1595 struct glyph *glyph = row->glyphs[TEXT_AREA];
1596 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1597
1598 hpos = row->x;
1599 vpos = row->y;
1600 while (glyph < end)
1601 {
1602 hpos += glyph->pixel_width;
1603 ++glyph;
1604 }
1605
1606 /* If first glyph is partially visible, its first visible position is still 0. */
1607 if (hpos < 0)
1608 hpos = 0;
1609
1610 success_p = 1;
1611 }
1612 else
1613 {
1614 hpos = vpos = 0;
1615 success_p = 0;
1616 }
1617
1618 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1619 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1620 return success_p;
1621 }
1622 #endif
1623
1624 *frame_x = hpos;
1625 *frame_y = vpos;
1626 return 1;
1627 }
1628
1629
1630 #ifdef HAVE_WINDOW_SYSTEM
1631
1632 /* Find the glyph under window-relative coordinates X/Y in window W.
1633 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1634 strings. Return in *HPOS and *VPOS the row and column number of
1635 the glyph found. Return in *AREA the glyph area containing X.
1636 Value is a pointer to the glyph found or null if X/Y is not on
1637 text, or we can't tell because W's current matrix is not up to
1638 date. */
1639
1640 static struct glyph *
1641 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1642 struct window *w;
1643 int x, y;
1644 int *hpos, *vpos, *dx, *dy, *area;
1645 {
1646 struct glyph *glyph, *end;
1647 struct glyph_row *row = NULL;
1648 int x0, i;
1649
1650 /* Find row containing Y. Give up if some row is not enabled. */
1651 for (i = 0; i < w->current_matrix->nrows; ++i)
1652 {
1653 row = MATRIX_ROW (w->current_matrix, i);
1654 if (!row->enabled_p)
1655 return NULL;
1656 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1657 break;
1658 }
1659
1660 *vpos = i;
1661 *hpos = 0;
1662
1663 /* Give up if Y is not in the window. */
1664 if (i == w->current_matrix->nrows)
1665 return NULL;
1666
1667 /* Get the glyph area containing X. */
1668 if (w->pseudo_window_p)
1669 {
1670 *area = TEXT_AREA;
1671 x0 = 0;
1672 }
1673 else
1674 {
1675 if (x < window_box_left_offset (w, TEXT_AREA))
1676 {
1677 *area = LEFT_MARGIN_AREA;
1678 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1679 }
1680 else if (x < window_box_right_offset (w, TEXT_AREA))
1681 {
1682 *area = TEXT_AREA;
1683 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1684 }
1685 else
1686 {
1687 *area = RIGHT_MARGIN_AREA;
1688 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1689 }
1690 }
1691
1692 /* Find glyph containing X. */
1693 glyph = row->glyphs[*area];
1694 end = glyph + row->used[*area];
1695 x -= x0;
1696 while (glyph < end && x >= glyph->pixel_width)
1697 {
1698 x -= glyph->pixel_width;
1699 ++glyph;
1700 }
1701
1702 if (glyph == end)
1703 return NULL;
1704
1705 if (dx)
1706 {
1707 *dx = x;
1708 *dy = y - (row->y + row->ascent - glyph->ascent);
1709 }
1710
1711 *hpos = glyph - row->glyphs[*area];
1712 return glyph;
1713 }
1714
1715
1716 /* EXPORT:
1717 Convert frame-relative x/y to coordinates relative to window W.
1718 Takes pseudo-windows into account. */
1719
1720 void
1721 frame_to_window_pixel_xy (w, x, y)
1722 struct window *w;
1723 int *x, *y;
1724 {
1725 if (w->pseudo_window_p)
1726 {
1727 /* A pseudo-window is always full-width, and starts at the
1728 left edge of the frame, plus a frame border. */
1729 struct frame *f = XFRAME (w->frame);
1730 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1731 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1732 }
1733 else
1734 {
1735 *x -= WINDOW_LEFT_EDGE_X (w);
1736 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1737 }
1738 }
1739
1740 /* EXPORT:
1741 Return in *R the clipping rectangle for glyph string S. */
1742
1743 void
1744 get_glyph_string_clip_rect (s, nr)
1745 struct glyph_string *s;
1746 NativeRectangle *nr;
1747 {
1748 XRectangle r;
1749
1750 if (s->row->full_width_p)
1751 {
1752 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1753 r.x = WINDOW_LEFT_EDGE_X (s->w);
1754 r.width = WINDOW_TOTAL_WIDTH (s->w);
1755
1756 /* Unless displaying a mode or menu bar line, which are always
1757 fully visible, clip to the visible part of the row. */
1758 if (s->w->pseudo_window_p)
1759 r.height = s->row->visible_height;
1760 else
1761 r.height = s->height;
1762 }
1763 else
1764 {
1765 /* This is a text line that may be partially visible. */
1766 r.x = window_box_left (s->w, s->area);
1767 r.width = window_box_width (s->w, s->area);
1768 r.height = s->row->visible_height;
1769 }
1770
1771 /* If S draws overlapping rows, it's sufficient to use the top and
1772 bottom of the window for clipping because this glyph string
1773 intentionally draws over other lines. */
1774 if (s->for_overlaps_p)
1775 {
1776 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1777 r.height = window_text_bottom_y (s->w) - r.y;
1778 }
1779 else
1780 {
1781 /* Don't use S->y for clipping because it doesn't take partially
1782 visible lines into account. For example, it can be negative for
1783 partially visible lines at the top of a window. */
1784 if (!s->row->full_width_p
1785 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1786 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1787 else
1788 r.y = max (0, s->row->y);
1789
1790 /* If drawing a tool-bar window, draw it over the internal border
1791 at the top of the window. */
1792 if (WINDOWP (s->f->tool_bar_window)
1793 && s->w == XWINDOW (s->f->tool_bar_window))
1794 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1795 }
1796
1797 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1798
1799 /* If drawing the cursor, don't let glyph draw outside its
1800 advertised boundaries. Cleartype does this under some circumstances. */
1801 if (s->hl == DRAW_CURSOR)
1802 {
1803 struct glyph *glyph = s->first_glyph;
1804 int height;
1805
1806 if (s->x > r.x)
1807 {
1808 r.width -= s->x - r.x;
1809 r.x = s->x;
1810 }
1811 r.width = min (r.width, glyph->pixel_width);
1812
1813 /* Don't draw cursor glyph taller than our actual glyph. */
1814 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1815 if (height < r.height)
1816 {
1817 int max_y = r.y + r.height;
1818 r.y = min (max_y, s->ybase + glyph->descent - height);
1819 r.height = min (max_y - r.y, height);
1820 }
1821 }
1822
1823 #ifdef CONVERT_FROM_XRECT
1824 CONVERT_FROM_XRECT (r, *nr);
1825 #else
1826 *nr = r;
1827 #endif
1828 }
1829
1830 #endif /* HAVE_WINDOW_SYSTEM */
1831
1832 \f
1833 /***********************************************************************
1834 Lisp form evaluation
1835 ***********************************************************************/
1836
1837 /* Error handler for safe_eval and safe_call. */
1838
1839 static Lisp_Object
1840 safe_eval_handler (arg)
1841 Lisp_Object arg;
1842 {
1843 add_to_log ("Error during redisplay: %s", arg, Qnil);
1844 return Qnil;
1845 }
1846
1847
1848 /* Evaluate SEXPR and return the result, or nil if something went
1849 wrong. Prevent redisplay during the evaluation. */
1850
1851 Lisp_Object
1852 safe_eval (sexpr)
1853 Lisp_Object sexpr;
1854 {
1855 Lisp_Object val;
1856
1857 if (inhibit_eval_during_redisplay)
1858 val = Qnil;
1859 else
1860 {
1861 int count = SPECPDL_INDEX ();
1862 struct gcpro gcpro1;
1863
1864 GCPRO1 (sexpr);
1865 specbind (Qinhibit_redisplay, Qt);
1866 /* Use Qt to ensure debugger does not run,
1867 so there is no possibility of wanting to redisplay. */
1868 val = internal_condition_case_1 (Feval, sexpr, Qt,
1869 safe_eval_handler);
1870 UNGCPRO;
1871 val = unbind_to (count, val);
1872 }
1873
1874 return val;
1875 }
1876
1877
1878 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1879 Return the result, or nil if something went wrong. Prevent
1880 redisplay during the evaluation. */
1881
1882 Lisp_Object
1883 safe_call (nargs, args)
1884 int nargs;
1885 Lisp_Object *args;
1886 {
1887 Lisp_Object val;
1888
1889 if (inhibit_eval_during_redisplay)
1890 val = Qnil;
1891 else
1892 {
1893 int count = SPECPDL_INDEX ();
1894 struct gcpro gcpro1;
1895
1896 GCPRO1 (args[0]);
1897 gcpro1.nvars = nargs;
1898 specbind (Qinhibit_redisplay, Qt);
1899 /* Use Qt to ensure debugger does not run,
1900 so there is no possibility of wanting to redisplay. */
1901 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1902 safe_eval_handler);
1903 UNGCPRO;
1904 val = unbind_to (count, val);
1905 }
1906
1907 return val;
1908 }
1909
1910
1911 /* Call function FN with one argument ARG.
1912 Return the result, or nil if something went wrong. */
1913
1914 Lisp_Object
1915 safe_call1 (fn, arg)
1916 Lisp_Object fn, arg;
1917 {
1918 Lisp_Object args[2];
1919 args[0] = fn;
1920 args[1] = arg;
1921 return safe_call (2, args);
1922 }
1923
1924
1925 \f
1926 /***********************************************************************
1927 Debugging
1928 ***********************************************************************/
1929
1930 #if 0
1931
1932 /* Define CHECK_IT to perform sanity checks on iterators.
1933 This is for debugging. It is too slow to do unconditionally. */
1934
1935 static void
1936 check_it (it)
1937 struct it *it;
1938 {
1939 if (it->method == next_element_from_string)
1940 {
1941 xassert (STRINGP (it->string));
1942 xassert (IT_STRING_CHARPOS (*it) >= 0);
1943 }
1944 else
1945 {
1946 xassert (IT_STRING_CHARPOS (*it) < 0);
1947 if (it->method == next_element_from_buffer)
1948 {
1949 /* Check that character and byte positions agree. */
1950 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1951 }
1952 }
1953
1954 if (it->dpvec)
1955 xassert (it->current.dpvec_index >= 0);
1956 else
1957 xassert (it->current.dpvec_index < 0);
1958 }
1959
1960 #define CHECK_IT(IT) check_it ((IT))
1961
1962 #else /* not 0 */
1963
1964 #define CHECK_IT(IT) (void) 0
1965
1966 #endif /* not 0 */
1967
1968
1969 #if GLYPH_DEBUG
1970
1971 /* Check that the window end of window W is what we expect it
1972 to be---the last row in the current matrix displaying text. */
1973
1974 static void
1975 check_window_end (w)
1976 struct window *w;
1977 {
1978 if (!MINI_WINDOW_P (w)
1979 && !NILP (w->window_end_valid))
1980 {
1981 struct glyph_row *row;
1982 xassert ((row = MATRIX_ROW (w->current_matrix,
1983 XFASTINT (w->window_end_vpos)),
1984 !row->enabled_p
1985 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1986 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1987 }
1988 }
1989
1990 #define CHECK_WINDOW_END(W) check_window_end ((W))
1991
1992 #else /* not GLYPH_DEBUG */
1993
1994 #define CHECK_WINDOW_END(W) (void) 0
1995
1996 #endif /* not GLYPH_DEBUG */
1997
1998
1999 \f
2000 /***********************************************************************
2001 Iterator initialization
2002 ***********************************************************************/
2003
2004 /* Initialize IT for displaying current_buffer in window W, starting
2005 at character position CHARPOS. CHARPOS < 0 means that no buffer
2006 position is specified which is useful when the iterator is assigned
2007 a position later. BYTEPOS is the byte position corresponding to
2008 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2009
2010 If ROW is not null, calls to produce_glyphs with IT as parameter
2011 will produce glyphs in that row.
2012
2013 BASE_FACE_ID is the id of a base face to use. It must be one of
2014 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2015 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2016 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2017
2018 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2019 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2020 will be initialized to use the corresponding mode line glyph row of
2021 the desired matrix of W. */
2022
2023 void
2024 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2025 struct it *it;
2026 struct window *w;
2027 int charpos, bytepos;
2028 struct glyph_row *row;
2029 enum face_id base_face_id;
2030 {
2031 int highlight_region_p;
2032
2033 /* Some precondition checks. */
2034 xassert (w != NULL && it != NULL);
2035 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2036 && charpos <= ZV));
2037
2038 /* If face attributes have been changed since the last redisplay,
2039 free realized faces now because they depend on face definitions
2040 that might have changed. Don't free faces while there might be
2041 desired matrices pending which reference these faces. */
2042 if (face_change_count && !inhibit_free_realized_faces)
2043 {
2044 face_change_count = 0;
2045 free_all_realized_faces (Qnil);
2046 }
2047
2048 /* Use one of the mode line rows of W's desired matrix if
2049 appropriate. */
2050 if (row == NULL)
2051 {
2052 if (base_face_id == MODE_LINE_FACE_ID
2053 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2054 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2055 else if (base_face_id == HEADER_LINE_FACE_ID)
2056 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2057 }
2058
2059 /* Clear IT. */
2060 bzero (it, sizeof *it);
2061 it->current.overlay_string_index = -1;
2062 it->current.dpvec_index = -1;
2063 it->base_face_id = base_face_id;
2064 it->string = Qnil;
2065 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2066
2067 /* The window in which we iterate over current_buffer: */
2068 XSETWINDOW (it->window, w);
2069 it->w = w;
2070 it->f = XFRAME (w->frame);
2071
2072 /* Extra space between lines (on window systems only). */
2073 if (base_face_id == DEFAULT_FACE_ID
2074 && FRAME_WINDOW_P (it->f))
2075 {
2076 if (NATNUMP (current_buffer->extra_line_spacing))
2077 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2078 else if (FLOATP (current_buffer->extra_line_spacing))
2079 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2080 * FRAME_LINE_HEIGHT (it->f));
2081 else if (it->f->extra_line_spacing > 0)
2082 it->extra_line_spacing = it->f->extra_line_spacing;
2083 it->max_extra_line_spacing = 0;
2084 }
2085
2086 /* If realized faces have been removed, e.g. because of face
2087 attribute changes of named faces, recompute them. When running
2088 in batch mode, the face cache of Vterminal_frame is null. If
2089 we happen to get called, make a dummy face cache. */
2090 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2091 init_frame_faces (it->f);
2092 if (FRAME_FACE_CACHE (it->f)->used == 0)
2093 recompute_basic_faces (it->f);
2094
2095 /* Current value of the `slice', `space-width', and 'height' properties. */
2096 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2097 it->space_width = Qnil;
2098 it->font_height = Qnil;
2099 it->override_ascent = -1;
2100
2101 /* Are control characters displayed as `^C'? */
2102 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2103
2104 /* -1 means everything between a CR and the following line end
2105 is invisible. >0 means lines indented more than this value are
2106 invisible. */
2107 it->selective = (INTEGERP (current_buffer->selective_display)
2108 ? XFASTINT (current_buffer->selective_display)
2109 : (!NILP (current_buffer->selective_display)
2110 ? -1 : 0));
2111 it->selective_display_ellipsis_p
2112 = !NILP (current_buffer->selective_display_ellipses);
2113
2114 /* Display table to use. */
2115 it->dp = window_display_table (w);
2116
2117 /* Are multibyte characters enabled in current_buffer? */
2118 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2119
2120 /* Non-zero if we should highlight the region. */
2121 highlight_region_p
2122 = (!NILP (Vtransient_mark_mode)
2123 && !NILP (current_buffer->mark_active)
2124 && XMARKER (current_buffer->mark)->buffer != 0);
2125
2126 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2127 start and end of a visible region in window IT->w. Set both to
2128 -1 to indicate no region. */
2129 if (highlight_region_p
2130 /* Maybe highlight only in selected window. */
2131 && (/* Either show region everywhere. */
2132 highlight_nonselected_windows
2133 /* Or show region in the selected window. */
2134 || w == XWINDOW (selected_window)
2135 /* Or show the region if we are in the mini-buffer and W is
2136 the window the mini-buffer refers to. */
2137 || (MINI_WINDOW_P (XWINDOW (selected_window))
2138 && WINDOWP (minibuf_selected_window)
2139 && w == XWINDOW (minibuf_selected_window))))
2140 {
2141 int charpos = marker_position (current_buffer->mark);
2142 it->region_beg_charpos = min (PT, charpos);
2143 it->region_end_charpos = max (PT, charpos);
2144 }
2145 else
2146 it->region_beg_charpos = it->region_end_charpos = -1;
2147
2148 /* Get the position at which the redisplay_end_trigger hook should
2149 be run, if it is to be run at all. */
2150 if (MARKERP (w->redisplay_end_trigger)
2151 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2152 it->redisplay_end_trigger_charpos
2153 = marker_position (w->redisplay_end_trigger);
2154 else if (INTEGERP (w->redisplay_end_trigger))
2155 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2156
2157 /* Correct bogus values of tab_width. */
2158 it->tab_width = XINT (current_buffer->tab_width);
2159 if (it->tab_width <= 0 || it->tab_width > 1000)
2160 it->tab_width = 8;
2161
2162 /* Are lines in the display truncated? */
2163 it->truncate_lines_p
2164 = (base_face_id != DEFAULT_FACE_ID
2165 || XINT (it->w->hscroll)
2166 || (truncate_partial_width_windows
2167 && !WINDOW_FULL_WIDTH_P (it->w))
2168 || !NILP (current_buffer->truncate_lines));
2169
2170 /* Get dimensions of truncation and continuation glyphs. These are
2171 displayed as fringe bitmaps under X, so we don't need them for such
2172 frames. */
2173 if (!FRAME_WINDOW_P (it->f))
2174 {
2175 if (it->truncate_lines_p)
2176 {
2177 /* We will need the truncation glyph. */
2178 xassert (it->glyph_row == NULL);
2179 produce_special_glyphs (it, IT_TRUNCATION);
2180 it->truncation_pixel_width = it->pixel_width;
2181 }
2182 else
2183 {
2184 /* We will need the continuation glyph. */
2185 xassert (it->glyph_row == NULL);
2186 produce_special_glyphs (it, IT_CONTINUATION);
2187 it->continuation_pixel_width = it->pixel_width;
2188 }
2189
2190 /* Reset these values to zero because the produce_special_glyphs
2191 above has changed them. */
2192 it->pixel_width = it->ascent = it->descent = 0;
2193 it->phys_ascent = it->phys_descent = 0;
2194 }
2195
2196 /* Set this after getting the dimensions of truncation and
2197 continuation glyphs, so that we don't produce glyphs when calling
2198 produce_special_glyphs, above. */
2199 it->glyph_row = row;
2200 it->area = TEXT_AREA;
2201
2202 /* Get the dimensions of the display area. The display area
2203 consists of the visible window area plus a horizontally scrolled
2204 part to the left of the window. All x-values are relative to the
2205 start of this total display area. */
2206 if (base_face_id != DEFAULT_FACE_ID)
2207 {
2208 /* Mode lines, menu bar in terminal frames. */
2209 it->first_visible_x = 0;
2210 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2211 }
2212 else
2213 {
2214 it->first_visible_x
2215 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2216 it->last_visible_x = (it->first_visible_x
2217 + window_box_width (w, TEXT_AREA));
2218
2219 /* If we truncate lines, leave room for the truncator glyph(s) at
2220 the right margin. Otherwise, leave room for the continuation
2221 glyph(s). Truncation and continuation glyphs are not inserted
2222 for window-based redisplay. */
2223 if (!FRAME_WINDOW_P (it->f))
2224 {
2225 if (it->truncate_lines_p)
2226 it->last_visible_x -= it->truncation_pixel_width;
2227 else
2228 it->last_visible_x -= it->continuation_pixel_width;
2229 }
2230
2231 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2232 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2233 }
2234
2235 /* Leave room for a border glyph. */
2236 if (!FRAME_WINDOW_P (it->f)
2237 && !WINDOW_RIGHTMOST_P (it->w))
2238 it->last_visible_x -= 1;
2239
2240 it->last_visible_y = window_text_bottom_y (w);
2241
2242 /* For mode lines and alike, arrange for the first glyph having a
2243 left box line if the face specifies a box. */
2244 if (base_face_id != DEFAULT_FACE_ID)
2245 {
2246 struct face *face;
2247
2248 it->face_id = base_face_id;
2249
2250 /* If we have a boxed mode line, make the first character appear
2251 with a left box line. */
2252 face = FACE_FROM_ID (it->f, base_face_id);
2253 if (face->box != FACE_NO_BOX)
2254 it->start_of_box_run_p = 1;
2255 }
2256
2257 /* If a buffer position was specified, set the iterator there,
2258 getting overlays and face properties from that position. */
2259 if (charpos >= BUF_BEG (current_buffer))
2260 {
2261 it->end_charpos = ZV;
2262 it->face_id = -1;
2263 IT_CHARPOS (*it) = charpos;
2264
2265 /* Compute byte position if not specified. */
2266 if (bytepos < charpos)
2267 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2268 else
2269 IT_BYTEPOS (*it) = bytepos;
2270
2271 it->start = it->current;
2272
2273 /* Compute faces etc. */
2274 reseat (it, it->current.pos, 1);
2275 }
2276
2277 CHECK_IT (it);
2278 }
2279
2280
2281 /* Initialize IT for the display of window W with window start POS. */
2282
2283 void
2284 start_display (it, w, pos)
2285 struct it *it;
2286 struct window *w;
2287 struct text_pos pos;
2288 {
2289 struct glyph_row *row;
2290 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2291
2292 row = w->desired_matrix->rows + first_vpos;
2293 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2294 it->first_vpos = first_vpos;
2295
2296 if (!it->truncate_lines_p)
2297 {
2298 int start_at_line_beg_p;
2299 int first_y = it->current_y;
2300
2301 /* If window start is not at a line start, skip forward to POS to
2302 get the correct continuation lines width. */
2303 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2304 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2305 if (!start_at_line_beg_p)
2306 {
2307 int new_x;
2308
2309 reseat_at_previous_visible_line_start (it);
2310 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2311
2312 new_x = it->current_x + it->pixel_width;
2313
2314 /* If lines are continued, this line may end in the middle
2315 of a multi-glyph character (e.g. a control character
2316 displayed as \003, or in the middle of an overlay
2317 string). In this case move_it_to above will not have
2318 taken us to the start of the continuation line but to the
2319 end of the continued line. */
2320 if (it->current_x > 0
2321 && !it->truncate_lines_p /* Lines are continued. */
2322 && (/* And glyph doesn't fit on the line. */
2323 new_x > it->last_visible_x
2324 /* Or it fits exactly and we're on a window
2325 system frame. */
2326 || (new_x == it->last_visible_x
2327 && FRAME_WINDOW_P (it->f))))
2328 {
2329 if (it->current.dpvec_index >= 0
2330 || it->current.overlay_string_index >= 0)
2331 {
2332 set_iterator_to_next (it, 1);
2333 move_it_in_display_line_to (it, -1, -1, 0);
2334 }
2335
2336 it->continuation_lines_width += it->current_x;
2337 }
2338
2339 /* We're starting a new display line, not affected by the
2340 height of the continued line, so clear the appropriate
2341 fields in the iterator structure. */
2342 it->max_ascent = it->max_descent = 0;
2343 it->max_phys_ascent = it->max_phys_descent = 0;
2344
2345 it->current_y = first_y;
2346 it->vpos = 0;
2347 it->current_x = it->hpos = 0;
2348 }
2349 }
2350
2351 #if 0 /* Don't assert the following because start_display is sometimes
2352 called intentionally with a window start that is not at a
2353 line start. Please leave this code in as a comment. */
2354
2355 /* Window start should be on a line start, now. */
2356 xassert (it->continuation_lines_width
2357 || IT_CHARPOS (it) == BEGV
2358 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2359 #endif /* 0 */
2360 }
2361
2362
2363 /* Return 1 if POS is a position in ellipses displayed for invisible
2364 text. W is the window we display, for text property lookup. */
2365
2366 static int
2367 in_ellipses_for_invisible_text_p (pos, w)
2368 struct display_pos *pos;
2369 struct window *w;
2370 {
2371 Lisp_Object prop, window;
2372 int ellipses_p = 0;
2373 int charpos = CHARPOS (pos->pos);
2374
2375 /* If POS specifies a position in a display vector, this might
2376 be for an ellipsis displayed for invisible text. We won't
2377 get the iterator set up for delivering that ellipsis unless
2378 we make sure that it gets aware of the invisible text. */
2379 if (pos->dpvec_index >= 0
2380 && pos->overlay_string_index < 0
2381 && CHARPOS (pos->string_pos) < 0
2382 && charpos > BEGV
2383 && (XSETWINDOW (window, w),
2384 prop = Fget_char_property (make_number (charpos),
2385 Qinvisible, window),
2386 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2387 {
2388 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2389 window);
2390 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2391 }
2392
2393 return ellipses_p;
2394 }
2395
2396
2397 /* Initialize IT for stepping through current_buffer in window W,
2398 starting at position POS that includes overlay string and display
2399 vector/ control character translation position information. Value
2400 is zero if there are overlay strings with newlines at POS. */
2401
2402 static int
2403 init_from_display_pos (it, w, pos)
2404 struct it *it;
2405 struct window *w;
2406 struct display_pos *pos;
2407 {
2408 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2409 int i, overlay_strings_with_newlines = 0;
2410
2411 /* If POS specifies a position in a display vector, this might
2412 be for an ellipsis displayed for invisible text. We won't
2413 get the iterator set up for delivering that ellipsis unless
2414 we make sure that it gets aware of the invisible text. */
2415 if (in_ellipses_for_invisible_text_p (pos, w))
2416 {
2417 --charpos;
2418 bytepos = 0;
2419 }
2420
2421 /* Keep in mind: the call to reseat in init_iterator skips invisible
2422 text, so we might end up at a position different from POS. This
2423 is only a problem when POS is a row start after a newline and an
2424 overlay starts there with an after-string, and the overlay has an
2425 invisible property. Since we don't skip invisible text in
2426 display_line and elsewhere immediately after consuming the
2427 newline before the row start, such a POS will not be in a string,
2428 but the call to init_iterator below will move us to the
2429 after-string. */
2430 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2431
2432 for (i = 0; i < it->n_overlay_strings; ++i)
2433 {
2434 const char *s = SDATA (it->overlay_strings[i]);
2435 const char *e = s + SBYTES (it->overlay_strings[i]);
2436
2437 while (s < e && *s != '\n')
2438 ++s;
2439
2440 if (s < e)
2441 {
2442 overlay_strings_with_newlines = 1;
2443 break;
2444 }
2445 }
2446
2447 /* If position is within an overlay string, set up IT to the right
2448 overlay string. */
2449 if (pos->overlay_string_index >= 0)
2450 {
2451 int relative_index;
2452
2453 /* If the first overlay string happens to have a `display'
2454 property for an image, the iterator will be set up for that
2455 image, and we have to undo that setup first before we can
2456 correct the overlay string index. */
2457 if (it->method == next_element_from_image)
2458 pop_it (it);
2459
2460 /* We already have the first chunk of overlay strings in
2461 IT->overlay_strings. Load more until the one for
2462 pos->overlay_string_index is in IT->overlay_strings. */
2463 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2464 {
2465 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2466 it->current.overlay_string_index = 0;
2467 while (n--)
2468 {
2469 load_overlay_strings (it, 0);
2470 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2471 }
2472 }
2473
2474 it->current.overlay_string_index = pos->overlay_string_index;
2475 relative_index = (it->current.overlay_string_index
2476 % OVERLAY_STRING_CHUNK_SIZE);
2477 it->string = it->overlay_strings[relative_index];
2478 xassert (STRINGP (it->string));
2479 it->current.string_pos = pos->string_pos;
2480 it->method = next_element_from_string;
2481 }
2482
2483 #if 0 /* This is bogus because POS not having an overlay string
2484 position does not mean it's after the string. Example: A
2485 line starting with a before-string and initialization of IT
2486 to the previous row's end position. */
2487 else if (it->current.overlay_string_index >= 0)
2488 {
2489 /* If POS says we're already after an overlay string ending at
2490 POS, make sure to pop the iterator because it will be in
2491 front of that overlay string. When POS is ZV, we've thereby
2492 also ``processed'' overlay strings at ZV. */
2493 while (it->sp)
2494 pop_it (it);
2495 it->current.overlay_string_index = -1;
2496 it->method = next_element_from_buffer;
2497 if (CHARPOS (pos->pos) == ZV)
2498 it->overlay_strings_at_end_processed_p = 1;
2499 }
2500 #endif /* 0 */
2501
2502 if (CHARPOS (pos->string_pos) >= 0)
2503 {
2504 /* Recorded position is not in an overlay string, but in another
2505 string. This can only be a string from a `display' property.
2506 IT should already be filled with that string. */
2507 it->current.string_pos = pos->string_pos;
2508 xassert (STRINGP (it->string));
2509 }
2510
2511 /* Restore position in display vector translations, control
2512 character translations or ellipses. */
2513 if (pos->dpvec_index >= 0)
2514 {
2515 if (it->dpvec == NULL)
2516 get_next_display_element (it);
2517 xassert (it->dpvec && it->current.dpvec_index == 0);
2518 it->current.dpvec_index = pos->dpvec_index;
2519 }
2520
2521 CHECK_IT (it);
2522 return !overlay_strings_with_newlines;
2523 }
2524
2525
2526 /* Initialize IT for stepping through current_buffer in window W
2527 starting at ROW->start. */
2528
2529 static void
2530 init_to_row_start (it, w, row)
2531 struct it *it;
2532 struct window *w;
2533 struct glyph_row *row;
2534 {
2535 init_from_display_pos (it, w, &row->start);
2536 it->start = row->start;
2537 it->continuation_lines_width = row->continuation_lines_width;
2538 CHECK_IT (it);
2539 }
2540
2541
2542 /* Initialize IT for stepping through current_buffer in window W
2543 starting in the line following ROW, i.e. starting at ROW->end.
2544 Value is zero if there are overlay strings with newlines at ROW's
2545 end position. */
2546
2547 static int
2548 init_to_row_end (it, w, row)
2549 struct it *it;
2550 struct window *w;
2551 struct glyph_row *row;
2552 {
2553 int success = 0;
2554
2555 if (init_from_display_pos (it, w, &row->end))
2556 {
2557 if (row->continued_p)
2558 it->continuation_lines_width
2559 = row->continuation_lines_width + row->pixel_width;
2560 CHECK_IT (it);
2561 success = 1;
2562 }
2563
2564 return success;
2565 }
2566
2567
2568
2569 \f
2570 /***********************************************************************
2571 Text properties
2572 ***********************************************************************/
2573
2574 /* Called when IT reaches IT->stop_charpos. Handle text property and
2575 overlay changes. Set IT->stop_charpos to the next position where
2576 to stop. */
2577
2578 static void
2579 handle_stop (it)
2580 struct it *it;
2581 {
2582 enum prop_handled handled;
2583 int handle_overlay_change_p = 1;
2584 struct props *p;
2585
2586 it->dpvec = NULL;
2587 it->current.dpvec_index = -1;
2588
2589 do
2590 {
2591 handled = HANDLED_NORMALLY;
2592
2593 /* Call text property handlers. */
2594 for (p = it_props; p->handler; ++p)
2595 {
2596 handled = p->handler (it);
2597
2598 if (handled == HANDLED_RECOMPUTE_PROPS)
2599 break;
2600 else if (handled == HANDLED_RETURN)
2601 return;
2602 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2603 handle_overlay_change_p = 0;
2604 }
2605
2606 if (handled != HANDLED_RECOMPUTE_PROPS)
2607 {
2608 /* Don't check for overlay strings below when set to deliver
2609 characters from a display vector. */
2610 if (it->method == next_element_from_display_vector)
2611 handle_overlay_change_p = 0;
2612
2613 /* Handle overlay changes. */
2614 if (handle_overlay_change_p)
2615 handled = handle_overlay_change (it);
2616
2617 /* Determine where to stop next. */
2618 if (handled == HANDLED_NORMALLY)
2619 compute_stop_pos (it);
2620 }
2621 }
2622 while (handled == HANDLED_RECOMPUTE_PROPS);
2623 }
2624
2625
2626 /* Compute IT->stop_charpos from text property and overlay change
2627 information for IT's current position. */
2628
2629 static void
2630 compute_stop_pos (it)
2631 struct it *it;
2632 {
2633 register INTERVAL iv, next_iv;
2634 Lisp_Object object, limit, position;
2635
2636 /* If nowhere else, stop at the end. */
2637 it->stop_charpos = it->end_charpos;
2638
2639 if (STRINGP (it->string))
2640 {
2641 /* Strings are usually short, so don't limit the search for
2642 properties. */
2643 object = it->string;
2644 limit = Qnil;
2645 position = make_number (IT_STRING_CHARPOS (*it));
2646 }
2647 else
2648 {
2649 int charpos;
2650
2651 /* If next overlay change is in front of the current stop pos
2652 (which is IT->end_charpos), stop there. Note: value of
2653 next_overlay_change is point-max if no overlay change
2654 follows. */
2655 charpos = next_overlay_change (IT_CHARPOS (*it));
2656 if (charpos < it->stop_charpos)
2657 it->stop_charpos = charpos;
2658
2659 /* If showing the region, we have to stop at the region
2660 start or end because the face might change there. */
2661 if (it->region_beg_charpos > 0)
2662 {
2663 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2664 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2665 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2666 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2667 }
2668
2669 /* Set up variables for computing the stop position from text
2670 property changes. */
2671 XSETBUFFER (object, current_buffer);
2672 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2673 position = make_number (IT_CHARPOS (*it));
2674
2675 }
2676
2677 /* Get the interval containing IT's position. Value is a null
2678 interval if there isn't such an interval. */
2679 iv = validate_interval_range (object, &position, &position, 0);
2680 if (!NULL_INTERVAL_P (iv))
2681 {
2682 Lisp_Object values_here[LAST_PROP_IDX];
2683 struct props *p;
2684
2685 /* Get properties here. */
2686 for (p = it_props; p->handler; ++p)
2687 values_here[p->idx] = textget (iv->plist, *p->name);
2688
2689 /* Look for an interval following iv that has different
2690 properties. */
2691 for (next_iv = next_interval (iv);
2692 (!NULL_INTERVAL_P (next_iv)
2693 && (NILP (limit)
2694 || XFASTINT (limit) > next_iv->position));
2695 next_iv = next_interval (next_iv))
2696 {
2697 for (p = it_props; p->handler; ++p)
2698 {
2699 Lisp_Object new_value;
2700
2701 new_value = textget (next_iv->plist, *p->name);
2702 if (!EQ (values_here[p->idx], new_value))
2703 break;
2704 }
2705
2706 if (p->handler)
2707 break;
2708 }
2709
2710 if (!NULL_INTERVAL_P (next_iv))
2711 {
2712 if (INTEGERP (limit)
2713 && next_iv->position >= XFASTINT (limit))
2714 /* No text property change up to limit. */
2715 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2716 else
2717 /* Text properties change in next_iv. */
2718 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2719 }
2720 }
2721
2722 xassert (STRINGP (it->string)
2723 || (it->stop_charpos >= BEGV
2724 && it->stop_charpos >= IT_CHARPOS (*it)));
2725 }
2726
2727
2728 /* Return the position of the next overlay change after POS in
2729 current_buffer. Value is point-max if no overlay change
2730 follows. This is like `next-overlay-change' but doesn't use
2731 xmalloc. */
2732
2733 static int
2734 next_overlay_change (pos)
2735 int pos;
2736 {
2737 int noverlays;
2738 int endpos;
2739 Lisp_Object *overlays;
2740 int i;
2741
2742 /* Get all overlays at the given position. */
2743 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2744
2745 /* If any of these overlays ends before endpos,
2746 use its ending point instead. */
2747 for (i = 0; i < noverlays; ++i)
2748 {
2749 Lisp_Object oend;
2750 int oendpos;
2751
2752 oend = OVERLAY_END (overlays[i]);
2753 oendpos = OVERLAY_POSITION (oend);
2754 endpos = min (endpos, oendpos);
2755 }
2756
2757 return endpos;
2758 }
2759
2760
2761 \f
2762 /***********************************************************************
2763 Fontification
2764 ***********************************************************************/
2765
2766 /* Handle changes in the `fontified' property of the current buffer by
2767 calling hook functions from Qfontification_functions to fontify
2768 regions of text. */
2769
2770 static enum prop_handled
2771 handle_fontified_prop (it)
2772 struct it *it;
2773 {
2774 Lisp_Object prop, pos;
2775 enum prop_handled handled = HANDLED_NORMALLY;
2776
2777 /* Get the value of the `fontified' property at IT's current buffer
2778 position. (The `fontified' property doesn't have a special
2779 meaning in strings.) If the value is nil, call functions from
2780 Qfontification_functions. */
2781 if (!STRINGP (it->string)
2782 && it->s == NULL
2783 && !NILP (Vfontification_functions)
2784 && !NILP (Vrun_hooks)
2785 && (pos = make_number (IT_CHARPOS (*it)),
2786 prop = Fget_char_property (pos, Qfontified, Qnil),
2787 NILP (prop)))
2788 {
2789 int count = SPECPDL_INDEX ();
2790 Lisp_Object val;
2791
2792 val = Vfontification_functions;
2793 specbind (Qfontification_functions, Qnil);
2794
2795 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2796 safe_call1 (val, pos);
2797 else
2798 {
2799 Lisp_Object globals, fn;
2800 struct gcpro gcpro1, gcpro2;
2801
2802 globals = Qnil;
2803 GCPRO2 (val, globals);
2804
2805 for (; CONSP (val); val = XCDR (val))
2806 {
2807 fn = XCAR (val);
2808
2809 if (EQ (fn, Qt))
2810 {
2811 /* A value of t indicates this hook has a local
2812 binding; it means to run the global binding too.
2813 In a global value, t should not occur. If it
2814 does, we must ignore it to avoid an endless
2815 loop. */
2816 for (globals = Fdefault_value (Qfontification_functions);
2817 CONSP (globals);
2818 globals = XCDR (globals))
2819 {
2820 fn = XCAR (globals);
2821 if (!EQ (fn, Qt))
2822 safe_call1 (fn, pos);
2823 }
2824 }
2825 else
2826 safe_call1 (fn, pos);
2827 }
2828
2829 UNGCPRO;
2830 }
2831
2832 unbind_to (count, Qnil);
2833
2834 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2835 something. This avoids an endless loop if they failed to
2836 fontify the text for which reason ever. */
2837 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2838 handled = HANDLED_RECOMPUTE_PROPS;
2839 }
2840
2841 return handled;
2842 }
2843
2844
2845 \f
2846 /***********************************************************************
2847 Faces
2848 ***********************************************************************/
2849
2850 /* Set up iterator IT from face properties at its current position.
2851 Called from handle_stop. */
2852
2853 static enum prop_handled
2854 handle_face_prop (it)
2855 struct it *it;
2856 {
2857 int new_face_id, next_stop;
2858
2859 if (!STRINGP (it->string))
2860 {
2861 new_face_id
2862 = face_at_buffer_position (it->w,
2863 IT_CHARPOS (*it),
2864 it->region_beg_charpos,
2865 it->region_end_charpos,
2866 &next_stop,
2867 (IT_CHARPOS (*it)
2868 + TEXT_PROP_DISTANCE_LIMIT),
2869 0);
2870
2871 /* Is this a start of a run of characters with box face?
2872 Caveat: this can be called for a freshly initialized
2873 iterator; face_id is -1 in this case. We know that the new
2874 face will not change until limit, i.e. if the new face has a
2875 box, all characters up to limit will have one. But, as
2876 usual, we don't know whether limit is really the end. */
2877 if (new_face_id != it->face_id)
2878 {
2879 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2880
2881 /* If new face has a box but old face has not, this is
2882 the start of a run of characters with box, i.e. it has
2883 a shadow on the left side. The value of face_id of the
2884 iterator will be -1 if this is the initial call that gets
2885 the face. In this case, we have to look in front of IT's
2886 position and see whether there is a face != new_face_id. */
2887 it->start_of_box_run_p
2888 = (new_face->box != FACE_NO_BOX
2889 && (it->face_id >= 0
2890 || IT_CHARPOS (*it) == BEG
2891 || new_face_id != face_before_it_pos (it)));
2892 it->face_box_p = new_face->box != FACE_NO_BOX;
2893 }
2894 }
2895 else
2896 {
2897 int base_face_id, bufpos;
2898
2899 if (it->current.overlay_string_index >= 0)
2900 bufpos = IT_CHARPOS (*it);
2901 else
2902 bufpos = 0;
2903
2904 /* For strings from a buffer, i.e. overlay strings or strings
2905 from a `display' property, use the face at IT's current
2906 buffer position as the base face to merge with, so that
2907 overlay strings appear in the same face as surrounding
2908 text, unless they specify their own faces. */
2909 base_face_id = underlying_face_id (it);
2910
2911 new_face_id = face_at_string_position (it->w,
2912 it->string,
2913 IT_STRING_CHARPOS (*it),
2914 bufpos,
2915 it->region_beg_charpos,
2916 it->region_end_charpos,
2917 &next_stop,
2918 base_face_id, 0);
2919
2920 #if 0 /* This shouldn't be neccessary. Let's check it. */
2921 /* If IT is used to display a mode line we would really like to
2922 use the mode line face instead of the frame's default face. */
2923 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2924 && new_face_id == DEFAULT_FACE_ID)
2925 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2926 #endif
2927
2928 /* Is this a start of a run of characters with box? Caveat:
2929 this can be called for a freshly allocated iterator; face_id
2930 is -1 is this case. We know that the new face will not
2931 change until the next check pos, i.e. if the new face has a
2932 box, all characters up to that position will have a
2933 box. But, as usual, we don't know whether that position
2934 is really the end. */
2935 if (new_face_id != it->face_id)
2936 {
2937 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2938 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2939
2940 /* If new face has a box but old face hasn't, this is the
2941 start of a run of characters with box, i.e. it has a
2942 shadow on the left side. */
2943 it->start_of_box_run_p
2944 = new_face->box && (old_face == NULL || !old_face->box);
2945 it->face_box_p = new_face->box != FACE_NO_BOX;
2946 }
2947 }
2948
2949 it->face_id = new_face_id;
2950 return HANDLED_NORMALLY;
2951 }
2952
2953
2954 /* Return the ID of the face ``underlying'' IT's current position,
2955 which is in a string. If the iterator is associated with a
2956 buffer, return the face at IT's current buffer position.
2957 Otherwise, use the iterator's base_face_id. */
2958
2959 static int
2960 underlying_face_id (it)
2961 struct it *it;
2962 {
2963 int face_id = it->base_face_id, i;
2964
2965 xassert (STRINGP (it->string));
2966
2967 for (i = it->sp - 1; i >= 0; --i)
2968 if (NILP (it->stack[i].string))
2969 face_id = it->stack[i].face_id;
2970
2971 return face_id;
2972 }
2973
2974
2975 /* Compute the face one character before or after the current position
2976 of IT. BEFORE_P non-zero means get the face in front of IT's
2977 position. Value is the id of the face. */
2978
2979 static int
2980 face_before_or_after_it_pos (it, before_p)
2981 struct it *it;
2982 int before_p;
2983 {
2984 int face_id, limit;
2985 int next_check_charpos;
2986 struct text_pos pos;
2987
2988 xassert (it->s == NULL);
2989
2990 if (STRINGP (it->string))
2991 {
2992 int bufpos, base_face_id;
2993
2994 /* No face change past the end of the string (for the case
2995 we are padding with spaces). No face change before the
2996 string start. */
2997 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2998 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2999 return it->face_id;
3000
3001 /* Set pos to the position before or after IT's current position. */
3002 if (before_p)
3003 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3004 else
3005 /* For composition, we must check the character after the
3006 composition. */
3007 pos = (it->what == IT_COMPOSITION
3008 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3009 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3010
3011 if (it->current.overlay_string_index >= 0)
3012 bufpos = IT_CHARPOS (*it);
3013 else
3014 bufpos = 0;
3015
3016 base_face_id = underlying_face_id (it);
3017
3018 /* Get the face for ASCII, or unibyte. */
3019 face_id = face_at_string_position (it->w,
3020 it->string,
3021 CHARPOS (pos),
3022 bufpos,
3023 it->region_beg_charpos,
3024 it->region_end_charpos,
3025 &next_check_charpos,
3026 base_face_id, 0);
3027
3028 /* Correct the face for charsets different from ASCII. Do it
3029 for the multibyte case only. The face returned above is
3030 suitable for unibyte text if IT->string is unibyte. */
3031 if (STRING_MULTIBYTE (it->string))
3032 {
3033 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3034 int rest = SBYTES (it->string) - BYTEPOS (pos);
3035 int c, len;
3036 struct face *face = FACE_FROM_ID (it->f, face_id);
3037
3038 c = string_char_and_length (p, rest, &len);
3039 face_id = FACE_FOR_CHAR (it->f, face, c);
3040 }
3041 }
3042 else
3043 {
3044 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3045 || (IT_CHARPOS (*it) <= BEGV && before_p))
3046 return it->face_id;
3047
3048 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3049 pos = it->current.pos;
3050
3051 if (before_p)
3052 DEC_TEXT_POS (pos, it->multibyte_p);
3053 else
3054 {
3055 if (it->what == IT_COMPOSITION)
3056 /* For composition, we must check the position after the
3057 composition. */
3058 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3059 else
3060 INC_TEXT_POS (pos, it->multibyte_p);
3061 }
3062
3063 /* Determine face for CHARSET_ASCII, or unibyte. */
3064 face_id = face_at_buffer_position (it->w,
3065 CHARPOS (pos),
3066 it->region_beg_charpos,
3067 it->region_end_charpos,
3068 &next_check_charpos,
3069 limit, 0);
3070
3071 /* Correct the face for charsets different from ASCII. Do it
3072 for the multibyte case only. The face returned above is
3073 suitable for unibyte text if current_buffer is unibyte. */
3074 if (it->multibyte_p)
3075 {
3076 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3077 struct face *face = FACE_FROM_ID (it->f, face_id);
3078 face_id = FACE_FOR_CHAR (it->f, face, c);
3079 }
3080 }
3081
3082 return face_id;
3083 }
3084
3085
3086 \f
3087 /***********************************************************************
3088 Invisible text
3089 ***********************************************************************/
3090
3091 /* Set up iterator IT from invisible properties at its current
3092 position. Called from handle_stop. */
3093
3094 static enum prop_handled
3095 handle_invisible_prop (it)
3096 struct it *it;
3097 {
3098 enum prop_handled handled = HANDLED_NORMALLY;
3099
3100 if (STRINGP (it->string))
3101 {
3102 extern Lisp_Object Qinvisible;
3103 Lisp_Object prop, end_charpos, limit, charpos;
3104
3105 /* Get the value of the invisible text property at the
3106 current position. Value will be nil if there is no such
3107 property. */
3108 charpos = make_number (IT_STRING_CHARPOS (*it));
3109 prop = Fget_text_property (charpos, Qinvisible, it->string);
3110
3111 if (!NILP (prop)
3112 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3113 {
3114 handled = HANDLED_RECOMPUTE_PROPS;
3115
3116 /* Get the position at which the next change of the
3117 invisible text property can be found in IT->string.
3118 Value will be nil if the property value is the same for
3119 all the rest of IT->string. */
3120 XSETINT (limit, SCHARS (it->string));
3121 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3122 it->string, limit);
3123
3124 /* Text at current position is invisible. The next
3125 change in the property is at position end_charpos.
3126 Move IT's current position to that position. */
3127 if (INTEGERP (end_charpos)
3128 && XFASTINT (end_charpos) < XFASTINT (limit))
3129 {
3130 struct text_pos old;
3131 old = it->current.string_pos;
3132 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3133 compute_string_pos (&it->current.string_pos, old, it->string);
3134 }
3135 else
3136 {
3137 /* The rest of the string is invisible. If this is an
3138 overlay string, proceed with the next overlay string
3139 or whatever comes and return a character from there. */
3140 if (it->current.overlay_string_index >= 0)
3141 {
3142 next_overlay_string (it);
3143 /* Don't check for overlay strings when we just
3144 finished processing them. */
3145 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3146 }
3147 else
3148 {
3149 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3150 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3151 }
3152 }
3153 }
3154 }
3155 else
3156 {
3157 int invis_p, newpos, next_stop, start_charpos;
3158 Lisp_Object pos, prop, overlay;
3159
3160 /* First of all, is there invisible text at this position? */
3161 start_charpos = IT_CHARPOS (*it);
3162 pos = make_number (IT_CHARPOS (*it));
3163 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3164 &overlay);
3165 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3166
3167 /* If we are on invisible text, skip over it. */
3168 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3169 {
3170 /* Record whether we have to display an ellipsis for the
3171 invisible text. */
3172 int display_ellipsis_p = invis_p == 2;
3173
3174 handled = HANDLED_RECOMPUTE_PROPS;
3175
3176 /* Loop skipping over invisible text. The loop is left at
3177 ZV or with IT on the first char being visible again. */
3178 do
3179 {
3180 /* Try to skip some invisible text. Return value is the
3181 position reached which can be equal to IT's position
3182 if there is nothing invisible here. This skips both
3183 over invisible text properties and overlays with
3184 invisible property. */
3185 newpos = skip_invisible (IT_CHARPOS (*it),
3186 &next_stop, ZV, it->window);
3187
3188 /* If we skipped nothing at all we weren't at invisible
3189 text in the first place. If everything to the end of
3190 the buffer was skipped, end the loop. */
3191 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3192 invis_p = 0;
3193 else
3194 {
3195 /* We skipped some characters but not necessarily
3196 all there are. Check if we ended up on visible
3197 text. Fget_char_property returns the property of
3198 the char before the given position, i.e. if we
3199 get invis_p = 0, this means that the char at
3200 newpos is visible. */
3201 pos = make_number (newpos);
3202 prop = Fget_char_property (pos, Qinvisible, it->window);
3203 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3204 }
3205
3206 /* If we ended up on invisible text, proceed to
3207 skip starting with next_stop. */
3208 if (invis_p)
3209 IT_CHARPOS (*it) = next_stop;
3210 }
3211 while (invis_p);
3212
3213 /* The position newpos is now either ZV or on visible text. */
3214 IT_CHARPOS (*it) = newpos;
3215 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3216
3217 /* If there are before-strings at the start of invisible
3218 text, and the text is invisible because of a text
3219 property, arrange to show before-strings because 20.x did
3220 it that way. (If the text is invisible because of an
3221 overlay property instead of a text property, this is
3222 already handled in the overlay code.) */
3223 if (NILP (overlay)
3224 && get_overlay_strings (it, start_charpos))
3225 {
3226 handled = HANDLED_RECOMPUTE_PROPS;
3227 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3228 }
3229 else if (display_ellipsis_p)
3230 setup_for_ellipsis (it);
3231 }
3232 }
3233
3234 return handled;
3235 }
3236
3237
3238 /* Make iterator IT return `...' next. */
3239
3240 static void
3241 setup_for_ellipsis (it)
3242 struct it *it;
3243 {
3244 if (it->dp
3245 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3246 {
3247 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3248 it->dpvec = v->contents;
3249 it->dpend = v->contents + v->size;
3250 }
3251 else
3252 {
3253 /* Default `...'. */
3254 it->dpvec = default_invis_vector;
3255 it->dpend = default_invis_vector + 3;
3256 }
3257
3258 /* The ellipsis display does not replace the display of the
3259 character at the new position. Indicate this by setting
3260 IT->dpvec_char_len to zero. */
3261 it->dpvec_char_len = 0;
3262
3263 it->current.dpvec_index = 0;
3264 it->method = next_element_from_display_vector;
3265 }
3266
3267
3268 \f
3269 /***********************************************************************
3270 'display' property
3271 ***********************************************************************/
3272
3273 /* Set up iterator IT from `display' property at its current position.
3274 Called from handle_stop. */
3275
3276 static enum prop_handled
3277 handle_display_prop (it)
3278 struct it *it;
3279 {
3280 Lisp_Object prop, object;
3281 struct text_pos *position;
3282 int display_replaced_p = 0;
3283
3284 if (STRINGP (it->string))
3285 {
3286 object = it->string;
3287 position = &it->current.string_pos;
3288 }
3289 else
3290 {
3291 object = it->w->buffer;
3292 position = &it->current.pos;
3293 }
3294
3295 /* Reset those iterator values set from display property values. */
3296 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3297 it->space_width = Qnil;
3298 it->font_height = Qnil;
3299 it->voffset = 0;
3300
3301 /* We don't support recursive `display' properties, i.e. string
3302 values that have a string `display' property, that have a string
3303 `display' property etc. */
3304 if (!it->string_from_display_prop_p)
3305 it->area = TEXT_AREA;
3306
3307 prop = Fget_char_property (make_number (position->charpos),
3308 Qdisplay, object);
3309 if (NILP (prop))
3310 return HANDLED_NORMALLY;
3311
3312 if (CONSP (prop)
3313 /* Simple properties. */
3314 && !EQ (XCAR (prop), Qimage)
3315 && !EQ (XCAR (prop), Qspace)
3316 && !EQ (XCAR (prop), Qwhen)
3317 && !EQ (XCAR (prop), Qslice)
3318 && !EQ (XCAR (prop), Qspace_width)
3319 && !EQ (XCAR (prop), Qheight)
3320 && !EQ (XCAR (prop), Qraise)
3321 /* Marginal area specifications. */
3322 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3323 && !EQ (XCAR (prop), Qleft_fringe)
3324 && !EQ (XCAR (prop), Qright_fringe)
3325 && !NILP (XCAR (prop)))
3326 {
3327 for (; CONSP (prop); prop = XCDR (prop))
3328 {
3329 if (handle_single_display_prop (it, XCAR (prop), object,
3330 position, display_replaced_p))
3331 display_replaced_p = 1;
3332 }
3333 }
3334 else if (VECTORP (prop))
3335 {
3336 int i;
3337 for (i = 0; i < ASIZE (prop); ++i)
3338 if (handle_single_display_prop (it, AREF (prop, i), object,
3339 position, display_replaced_p))
3340 display_replaced_p = 1;
3341 }
3342 else
3343 {
3344 if (handle_single_display_prop (it, prop, object, position, 0))
3345 display_replaced_p = 1;
3346 }
3347
3348 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3349 }
3350
3351
3352 /* Value is the position of the end of the `display' property starting
3353 at START_POS in OBJECT. */
3354
3355 static struct text_pos
3356 display_prop_end (it, object, start_pos)
3357 struct it *it;
3358 Lisp_Object object;
3359 struct text_pos start_pos;
3360 {
3361 Lisp_Object end;
3362 struct text_pos end_pos;
3363
3364 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3365 Qdisplay, object, Qnil);
3366 CHARPOS (end_pos) = XFASTINT (end);
3367 if (STRINGP (object))
3368 compute_string_pos (&end_pos, start_pos, it->string);
3369 else
3370 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3371
3372 return end_pos;
3373 }
3374
3375
3376 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3377 is the object in which the `display' property was found. *POSITION
3378 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3379 means that we previously saw a display sub-property which already
3380 replaced text display with something else, for example an image;
3381 ignore such properties after the first one has been processed.
3382
3383 If PROP is a `space' or `image' sub-property, set *POSITION to the
3384 end position of the `display' property.
3385
3386 Value is non-zero if something was found which replaces the display
3387 of buffer or string text. */
3388
3389 static int
3390 handle_single_display_prop (it, prop, object, position,
3391 display_replaced_before_p)
3392 struct it *it;
3393 Lisp_Object prop;
3394 Lisp_Object object;
3395 struct text_pos *position;
3396 int display_replaced_before_p;
3397 {
3398 Lisp_Object value;
3399 int replaces_text_display_p = 0;
3400 Lisp_Object form;
3401
3402 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3403 evaluated. If the result is nil, VALUE is ignored. */
3404 form = Qt;
3405 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3406 {
3407 prop = XCDR (prop);
3408 if (!CONSP (prop))
3409 return 0;
3410 form = XCAR (prop);
3411 prop = XCDR (prop);
3412 }
3413
3414 if (!NILP (form) && !EQ (form, Qt))
3415 {
3416 int count = SPECPDL_INDEX ();
3417 struct gcpro gcpro1;
3418
3419 /* Bind `object' to the object having the `display' property, a
3420 buffer or string. Bind `position' to the position in the
3421 object where the property was found, and `buffer-position'
3422 to the current position in the buffer. */
3423 specbind (Qobject, object);
3424 specbind (Qposition, make_number (CHARPOS (*position)));
3425 specbind (Qbuffer_position,
3426 make_number (STRINGP (object)
3427 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3428 GCPRO1 (form);
3429 form = safe_eval (form);
3430 UNGCPRO;
3431 unbind_to (count, Qnil);
3432 }
3433
3434 if (NILP (form))
3435 return 0;
3436
3437 if (CONSP (prop)
3438 && EQ (XCAR (prop), Qheight)
3439 && CONSP (XCDR (prop)))
3440 {
3441 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3442 return 0;
3443
3444 /* `(height HEIGHT)'. */
3445 it->font_height = XCAR (XCDR (prop));
3446 if (!NILP (it->font_height))
3447 {
3448 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3449 int new_height = -1;
3450
3451 if (CONSP (it->font_height)
3452 && (EQ (XCAR (it->font_height), Qplus)
3453 || EQ (XCAR (it->font_height), Qminus))
3454 && CONSP (XCDR (it->font_height))
3455 && INTEGERP (XCAR (XCDR (it->font_height))))
3456 {
3457 /* `(+ N)' or `(- N)' where N is an integer. */
3458 int steps = XINT (XCAR (XCDR (it->font_height)));
3459 if (EQ (XCAR (it->font_height), Qplus))
3460 steps = - steps;
3461 it->face_id = smaller_face (it->f, it->face_id, steps);
3462 }
3463 else if (FUNCTIONP (it->font_height))
3464 {
3465 /* Call function with current height as argument.
3466 Value is the new height. */
3467 Lisp_Object height;
3468 height = safe_call1 (it->font_height,
3469 face->lface[LFACE_HEIGHT_INDEX]);
3470 if (NUMBERP (height))
3471 new_height = XFLOATINT (height);
3472 }
3473 else if (NUMBERP (it->font_height))
3474 {
3475 /* Value is a multiple of the canonical char height. */
3476 struct face *face;
3477
3478 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3479 new_height = (XFLOATINT (it->font_height)
3480 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3481 }
3482 else
3483 {
3484 /* Evaluate IT->font_height with `height' bound to the
3485 current specified height to get the new height. */
3486 Lisp_Object value;
3487 int count = SPECPDL_INDEX ();
3488
3489 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3490 value = safe_eval (it->font_height);
3491 unbind_to (count, Qnil);
3492
3493 if (NUMBERP (value))
3494 new_height = XFLOATINT (value);
3495 }
3496
3497 if (new_height > 0)
3498 it->face_id = face_with_height (it->f, it->face_id, new_height);
3499 }
3500 }
3501 else if (CONSP (prop)
3502 && EQ (XCAR (prop), Qspace_width)
3503 && CONSP (XCDR (prop)))
3504 {
3505 /* `(space_width WIDTH)'. */
3506 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3507 return 0;
3508
3509 value = XCAR (XCDR (prop));
3510 if (NUMBERP (value) && XFLOATINT (value) > 0)
3511 it->space_width = value;
3512 }
3513 else if (CONSP (prop)
3514 && EQ (XCAR (prop), Qslice))
3515 {
3516 /* `(slice X Y WIDTH HEIGHT)'. */
3517 Lisp_Object tem;
3518
3519 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3520 return 0;
3521
3522 if (tem = XCDR (prop), CONSP (tem))
3523 {
3524 it->slice.x = XCAR (tem);
3525 if (tem = XCDR (tem), CONSP (tem))
3526 {
3527 it->slice.y = XCAR (tem);
3528 if (tem = XCDR (tem), CONSP (tem))
3529 {
3530 it->slice.width = XCAR (tem);
3531 if (tem = XCDR (tem), CONSP (tem))
3532 it->slice.height = XCAR (tem);
3533 }
3534 }
3535 }
3536 }
3537 else if (CONSP (prop)
3538 && EQ (XCAR (prop), Qraise)
3539 && CONSP (XCDR (prop)))
3540 {
3541 /* `(raise FACTOR)'. */
3542 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3543 return 0;
3544
3545 #ifdef HAVE_WINDOW_SYSTEM
3546 value = XCAR (XCDR (prop));
3547 if (NUMBERP (value))
3548 {
3549 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3550 it->voffset = - (XFLOATINT (value)
3551 * (FONT_HEIGHT (face->font)));
3552 }
3553 #endif /* HAVE_WINDOW_SYSTEM */
3554 }
3555 else if (!it->string_from_display_prop_p)
3556 {
3557 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3558 VALUE) or `((margin nil) VALUE)' or VALUE. */
3559 Lisp_Object location, value;
3560 struct text_pos start_pos;
3561 int valid_p;
3562
3563 /* Characters having this form of property are not displayed, so
3564 we have to find the end of the property. */
3565 start_pos = *position;
3566 *position = display_prop_end (it, object, start_pos);
3567 value = Qnil;
3568
3569 /* Let's stop at the new position and assume that all
3570 text properties change there. */
3571 it->stop_charpos = position->charpos;
3572
3573 if (CONSP (prop)
3574 && (EQ (XCAR (prop), Qleft_fringe)
3575 || EQ (XCAR (prop), Qright_fringe))
3576 && CONSP (XCDR (prop)))
3577 {
3578 unsigned face_id = DEFAULT_FACE_ID;
3579 int fringe_bitmap;
3580
3581 /* Save current settings of IT so that we can restore them
3582 when we are finished with the glyph property value. */
3583
3584 /* `(left-fringe BITMAP FACE)'. */
3585 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3586 return 0;
3587
3588 #ifdef HAVE_WINDOW_SYSTEM
3589 value = XCAR (XCDR (prop));
3590 if (!SYMBOLP (value)
3591 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3592 return 0;
3593
3594 if (CONSP (XCDR (XCDR (prop))))
3595 {
3596 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3597
3598 face_id = lookup_named_face (it->f, face_name, 'A');
3599 if (face_id < 0)
3600 return 0;
3601 }
3602
3603 push_it (it);
3604
3605 it->area = TEXT_AREA;
3606 it->what = IT_IMAGE;
3607 it->image_id = -1; /* no image */
3608 it->position = start_pos;
3609 it->object = NILP (object) ? it->w->buffer : object;
3610 it->method = next_element_from_image;
3611 it->face_id = face_id;
3612
3613 /* Say that we haven't consumed the characters with
3614 `display' property yet. The call to pop_it in
3615 set_iterator_to_next will clean this up. */
3616 *position = start_pos;
3617
3618 if (EQ (XCAR (prop), Qleft_fringe))
3619 {
3620 it->left_user_fringe_bitmap = fringe_bitmap;
3621 it->left_user_fringe_face_id = face_id;
3622 }
3623 else
3624 {
3625 it->right_user_fringe_bitmap = fringe_bitmap;
3626 it->right_user_fringe_face_id = face_id;
3627 }
3628 #endif /* HAVE_WINDOW_SYSTEM */
3629 return 1;
3630 }
3631
3632 location = Qunbound;
3633 if (CONSP (prop) && CONSP (XCAR (prop)))
3634 {
3635 Lisp_Object tem;
3636
3637 value = XCDR (prop);
3638 if (CONSP (value))
3639 value = XCAR (value);
3640
3641 tem = XCAR (prop);
3642 if (EQ (XCAR (tem), Qmargin)
3643 && (tem = XCDR (tem),
3644 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3645 (NILP (tem)
3646 || EQ (tem, Qleft_margin)
3647 || EQ (tem, Qright_margin))))
3648 location = tem;
3649 }
3650
3651 if (EQ (location, Qunbound))
3652 {
3653 location = Qnil;
3654 value = prop;
3655 }
3656
3657 valid_p = (STRINGP (value)
3658 #ifdef HAVE_WINDOW_SYSTEM
3659 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3660 #endif /* not HAVE_WINDOW_SYSTEM */
3661 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3662
3663 if ((EQ (location, Qleft_margin)
3664 || EQ (location, Qright_margin)
3665 || NILP (location))
3666 && valid_p
3667 && !display_replaced_before_p)
3668 {
3669 replaces_text_display_p = 1;
3670
3671 /* Save current settings of IT so that we can restore them
3672 when we are finished with the glyph property value. */
3673 push_it (it);
3674
3675 if (NILP (location))
3676 it->area = TEXT_AREA;
3677 else if (EQ (location, Qleft_margin))
3678 it->area = LEFT_MARGIN_AREA;
3679 else
3680 it->area = RIGHT_MARGIN_AREA;
3681
3682 if (STRINGP (value))
3683 {
3684 it->string = value;
3685 it->multibyte_p = STRING_MULTIBYTE (it->string);
3686 it->current.overlay_string_index = -1;
3687 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3688 it->end_charpos = it->string_nchars = SCHARS (it->string);
3689 it->method = next_element_from_string;
3690 it->stop_charpos = 0;
3691 it->string_from_display_prop_p = 1;
3692 /* Say that we haven't consumed the characters with
3693 `display' property yet. The call to pop_it in
3694 set_iterator_to_next will clean this up. */
3695 *position = start_pos;
3696 }
3697 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3698 {
3699 it->method = next_element_from_stretch;
3700 it->object = value;
3701 it->current.pos = it->position = start_pos;
3702 }
3703 #ifdef HAVE_WINDOW_SYSTEM
3704 else
3705 {
3706 it->what = IT_IMAGE;
3707 it->image_id = lookup_image (it->f, value);
3708 it->position = start_pos;
3709 it->object = NILP (object) ? it->w->buffer : object;
3710 it->method = next_element_from_image;
3711
3712 /* Say that we haven't consumed the characters with
3713 `display' property yet. The call to pop_it in
3714 set_iterator_to_next will clean this up. */
3715 *position = start_pos;
3716 }
3717 #endif /* HAVE_WINDOW_SYSTEM */
3718 }
3719 else
3720 /* Invalid property or property not supported. Restore
3721 the position to what it was before. */
3722 *position = start_pos;
3723 }
3724
3725 return replaces_text_display_p;
3726 }
3727
3728
3729 /* Check if PROP is a display sub-property value whose text should be
3730 treated as intangible. */
3731
3732 static int
3733 single_display_prop_intangible_p (prop)
3734 Lisp_Object prop;
3735 {
3736 /* Skip over `when FORM'. */
3737 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3738 {
3739 prop = XCDR (prop);
3740 if (!CONSP (prop))
3741 return 0;
3742 prop = XCDR (prop);
3743 }
3744
3745 if (STRINGP (prop))
3746 return 1;
3747
3748 if (!CONSP (prop))
3749 return 0;
3750
3751 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3752 we don't need to treat text as intangible. */
3753 if (EQ (XCAR (prop), Qmargin))
3754 {
3755 prop = XCDR (prop);
3756 if (!CONSP (prop))
3757 return 0;
3758
3759 prop = XCDR (prop);
3760 if (!CONSP (prop)
3761 || EQ (XCAR (prop), Qleft_margin)
3762 || EQ (XCAR (prop), Qright_margin))
3763 return 0;
3764 }
3765
3766 return (CONSP (prop)
3767 && (EQ (XCAR (prop), Qimage)
3768 || EQ (XCAR (prop), Qspace)));
3769 }
3770
3771
3772 /* Check if PROP is a display property value whose text should be
3773 treated as intangible. */
3774
3775 int
3776 display_prop_intangible_p (prop)
3777 Lisp_Object prop;
3778 {
3779 if (CONSP (prop)
3780 && CONSP (XCAR (prop))
3781 && !EQ (Qmargin, XCAR (XCAR (prop))))
3782 {
3783 /* A list of sub-properties. */
3784 while (CONSP (prop))
3785 {
3786 if (single_display_prop_intangible_p (XCAR (prop)))
3787 return 1;
3788 prop = XCDR (prop);
3789 }
3790 }
3791 else if (VECTORP (prop))
3792 {
3793 /* A vector of sub-properties. */
3794 int i;
3795 for (i = 0; i < ASIZE (prop); ++i)
3796 if (single_display_prop_intangible_p (AREF (prop, i)))
3797 return 1;
3798 }
3799 else
3800 return single_display_prop_intangible_p (prop);
3801
3802 return 0;
3803 }
3804
3805
3806 /* Return 1 if PROP is a display sub-property value containing STRING. */
3807
3808 static int
3809 single_display_prop_string_p (prop, string)
3810 Lisp_Object prop, string;
3811 {
3812 if (EQ (string, prop))
3813 return 1;
3814
3815 /* Skip over `when FORM'. */
3816 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3817 {
3818 prop = XCDR (prop);
3819 if (!CONSP (prop))
3820 return 0;
3821 prop = XCDR (prop);
3822 }
3823
3824 if (CONSP (prop))
3825 /* Skip over `margin LOCATION'. */
3826 if (EQ (XCAR (prop), Qmargin))
3827 {
3828 prop = XCDR (prop);
3829 if (!CONSP (prop))
3830 return 0;
3831
3832 prop = XCDR (prop);
3833 if (!CONSP (prop))
3834 return 0;
3835 }
3836
3837 return CONSP (prop) && EQ (XCAR (prop), string);
3838 }
3839
3840
3841 /* Return 1 if STRING appears in the `display' property PROP. */
3842
3843 static int
3844 display_prop_string_p (prop, string)
3845 Lisp_Object prop, string;
3846 {
3847 if (CONSP (prop)
3848 && CONSP (XCAR (prop))
3849 && !EQ (Qmargin, XCAR (XCAR (prop))))
3850 {
3851 /* A list of sub-properties. */
3852 while (CONSP (prop))
3853 {
3854 if (single_display_prop_string_p (XCAR (prop), string))
3855 return 1;
3856 prop = XCDR (prop);
3857 }
3858 }
3859 else if (VECTORP (prop))
3860 {
3861 /* A vector of sub-properties. */
3862 int i;
3863 for (i = 0; i < ASIZE (prop); ++i)
3864 if (single_display_prop_string_p (AREF (prop, i), string))
3865 return 1;
3866 }
3867 else
3868 return single_display_prop_string_p (prop, string);
3869
3870 return 0;
3871 }
3872
3873
3874 /* Determine from which buffer position in W's buffer STRING comes
3875 from. AROUND_CHARPOS is an approximate position where it could
3876 be from. Value is the buffer position or 0 if it couldn't be
3877 determined.
3878
3879 W's buffer must be current.
3880
3881 This function is necessary because we don't record buffer positions
3882 in glyphs generated from strings (to keep struct glyph small).
3883 This function may only use code that doesn't eval because it is
3884 called asynchronously from note_mouse_highlight. */
3885
3886 int
3887 string_buffer_position (w, string, around_charpos)
3888 struct window *w;
3889 Lisp_Object string;
3890 int around_charpos;
3891 {
3892 Lisp_Object limit, prop, pos;
3893 const int MAX_DISTANCE = 1000;
3894 int found = 0;
3895
3896 pos = make_number (around_charpos);
3897 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3898 while (!found && !EQ (pos, limit))
3899 {
3900 prop = Fget_char_property (pos, Qdisplay, Qnil);
3901 if (!NILP (prop) && display_prop_string_p (prop, string))
3902 found = 1;
3903 else
3904 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3905 }
3906
3907 if (!found)
3908 {
3909 pos = make_number (around_charpos);
3910 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3911 while (!found && !EQ (pos, limit))
3912 {
3913 prop = Fget_char_property (pos, Qdisplay, Qnil);
3914 if (!NILP (prop) && display_prop_string_p (prop, string))
3915 found = 1;
3916 else
3917 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3918 limit);
3919 }
3920 }
3921
3922 return found ? XINT (pos) : 0;
3923 }
3924
3925
3926 \f
3927 /***********************************************************************
3928 `composition' property
3929 ***********************************************************************/
3930
3931 /* Set up iterator IT from `composition' property at its current
3932 position. Called from handle_stop. */
3933
3934 static enum prop_handled
3935 handle_composition_prop (it)
3936 struct it *it;
3937 {
3938 Lisp_Object prop, string;
3939 int pos, pos_byte, end;
3940 enum prop_handled handled = HANDLED_NORMALLY;
3941
3942 if (STRINGP (it->string))
3943 {
3944 pos = IT_STRING_CHARPOS (*it);
3945 pos_byte = IT_STRING_BYTEPOS (*it);
3946 string = it->string;
3947 }
3948 else
3949 {
3950 pos = IT_CHARPOS (*it);
3951 pos_byte = IT_BYTEPOS (*it);
3952 string = Qnil;
3953 }
3954
3955 /* If there's a valid composition and point is not inside of the
3956 composition (in the case that the composition is from the current
3957 buffer), draw a glyph composed from the composition components. */
3958 if (find_composition (pos, -1, &pos, &end, &prop, string)
3959 && COMPOSITION_VALID_P (pos, end, prop)
3960 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3961 {
3962 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3963
3964 if (id >= 0)
3965 {
3966 it->method = next_element_from_composition;
3967 it->cmp_id = id;
3968 it->cmp_len = COMPOSITION_LENGTH (prop);
3969 /* For a terminal, draw only the first character of the
3970 components. */
3971 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3972 it->len = (STRINGP (it->string)
3973 ? string_char_to_byte (it->string, end)
3974 : CHAR_TO_BYTE (end)) - pos_byte;
3975 it->stop_charpos = end;
3976 handled = HANDLED_RETURN;
3977 }
3978 }
3979
3980 return handled;
3981 }
3982
3983
3984 \f
3985 /***********************************************************************
3986 Overlay strings
3987 ***********************************************************************/
3988
3989 /* The following structure is used to record overlay strings for
3990 later sorting in load_overlay_strings. */
3991
3992 struct overlay_entry
3993 {
3994 Lisp_Object overlay;
3995 Lisp_Object string;
3996 int priority;
3997 int after_string_p;
3998 };
3999
4000
4001 /* Set up iterator IT from overlay strings at its current position.
4002 Called from handle_stop. */
4003
4004 static enum prop_handled
4005 handle_overlay_change (it)
4006 struct it *it;
4007 {
4008 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4009 return HANDLED_RECOMPUTE_PROPS;
4010 else
4011 return HANDLED_NORMALLY;
4012 }
4013
4014
4015 /* Set up the next overlay string for delivery by IT, if there is an
4016 overlay string to deliver. Called by set_iterator_to_next when the
4017 end of the current overlay string is reached. If there are more
4018 overlay strings to display, IT->string and
4019 IT->current.overlay_string_index are set appropriately here.
4020 Otherwise IT->string is set to nil. */
4021
4022 static void
4023 next_overlay_string (it)
4024 struct it *it;
4025 {
4026 ++it->current.overlay_string_index;
4027 if (it->current.overlay_string_index == it->n_overlay_strings)
4028 {
4029 /* No more overlay strings. Restore IT's settings to what
4030 they were before overlay strings were processed, and
4031 continue to deliver from current_buffer. */
4032 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4033
4034 pop_it (it);
4035 xassert (it->stop_charpos >= BEGV
4036 && it->stop_charpos <= it->end_charpos);
4037 it->string = Qnil;
4038 it->current.overlay_string_index = -1;
4039 SET_TEXT_POS (it->current.string_pos, -1, -1);
4040 it->n_overlay_strings = 0;
4041 it->method = next_element_from_buffer;
4042
4043 /* If we're at the end of the buffer, record that we have
4044 processed the overlay strings there already, so that
4045 next_element_from_buffer doesn't try it again. */
4046 if (IT_CHARPOS (*it) >= it->end_charpos)
4047 it->overlay_strings_at_end_processed_p = 1;
4048
4049 /* If we have to display `...' for invisible text, set
4050 the iterator up for that. */
4051 if (display_ellipsis_p)
4052 setup_for_ellipsis (it);
4053 }
4054 else
4055 {
4056 /* There are more overlay strings to process. If
4057 IT->current.overlay_string_index has advanced to a position
4058 where we must load IT->overlay_strings with more strings, do
4059 it. */
4060 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4061
4062 if (it->current.overlay_string_index && i == 0)
4063 load_overlay_strings (it, 0);
4064
4065 /* Initialize IT to deliver display elements from the overlay
4066 string. */
4067 it->string = it->overlay_strings[i];
4068 it->multibyte_p = STRING_MULTIBYTE (it->string);
4069 SET_TEXT_POS (it->current.string_pos, 0, 0);
4070 it->method = next_element_from_string;
4071 it->stop_charpos = 0;
4072 }
4073
4074 CHECK_IT (it);
4075 }
4076
4077
4078 /* Compare two overlay_entry structures E1 and E2. Used as a
4079 comparison function for qsort in load_overlay_strings. Overlay
4080 strings for the same position are sorted so that
4081
4082 1. All after-strings come in front of before-strings, except
4083 when they come from the same overlay.
4084
4085 2. Within after-strings, strings are sorted so that overlay strings
4086 from overlays with higher priorities come first.
4087
4088 2. Within before-strings, strings are sorted so that overlay
4089 strings from overlays with higher priorities come last.
4090
4091 Value is analogous to strcmp. */
4092
4093
4094 static int
4095 compare_overlay_entries (e1, e2)
4096 void *e1, *e2;
4097 {
4098 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4099 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4100 int result;
4101
4102 if (entry1->after_string_p != entry2->after_string_p)
4103 {
4104 /* Let after-strings appear in front of before-strings if
4105 they come from different overlays. */
4106 if (EQ (entry1->overlay, entry2->overlay))
4107 result = entry1->after_string_p ? 1 : -1;
4108 else
4109 result = entry1->after_string_p ? -1 : 1;
4110 }
4111 else if (entry1->after_string_p)
4112 /* After-strings sorted in order of decreasing priority. */
4113 result = entry2->priority - entry1->priority;
4114 else
4115 /* Before-strings sorted in order of increasing priority. */
4116 result = entry1->priority - entry2->priority;
4117
4118 return result;
4119 }
4120
4121
4122 /* Load the vector IT->overlay_strings with overlay strings from IT's
4123 current buffer position, or from CHARPOS if that is > 0. Set
4124 IT->n_overlays to the total number of overlay strings found.
4125
4126 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4127 a time. On entry into load_overlay_strings,
4128 IT->current.overlay_string_index gives the number of overlay
4129 strings that have already been loaded by previous calls to this
4130 function.
4131
4132 IT->add_overlay_start contains an additional overlay start
4133 position to consider for taking overlay strings from, if non-zero.
4134 This position comes into play when the overlay has an `invisible'
4135 property, and both before and after-strings. When we've skipped to
4136 the end of the overlay, because of its `invisible' property, we
4137 nevertheless want its before-string to appear.
4138 IT->add_overlay_start will contain the overlay start position
4139 in this case.
4140
4141 Overlay strings are sorted so that after-string strings come in
4142 front of before-string strings. Within before and after-strings,
4143 strings are sorted by overlay priority. See also function
4144 compare_overlay_entries. */
4145
4146 static void
4147 load_overlay_strings (it, charpos)
4148 struct it *it;
4149 int charpos;
4150 {
4151 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4152 Lisp_Object overlay, window, str, invisible;
4153 struct Lisp_Overlay *ov;
4154 int start, end;
4155 int size = 20;
4156 int n = 0, i, j, invis_p;
4157 struct overlay_entry *entries
4158 = (struct overlay_entry *) alloca (size * sizeof *entries);
4159
4160 if (charpos <= 0)
4161 charpos = IT_CHARPOS (*it);
4162
4163 /* Append the overlay string STRING of overlay OVERLAY to vector
4164 `entries' which has size `size' and currently contains `n'
4165 elements. AFTER_P non-zero means STRING is an after-string of
4166 OVERLAY. */
4167 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4168 do \
4169 { \
4170 Lisp_Object priority; \
4171 \
4172 if (n == size) \
4173 { \
4174 int new_size = 2 * size; \
4175 struct overlay_entry *old = entries; \
4176 entries = \
4177 (struct overlay_entry *) alloca (new_size \
4178 * sizeof *entries); \
4179 bcopy (old, entries, size * sizeof *entries); \
4180 size = new_size; \
4181 } \
4182 \
4183 entries[n].string = (STRING); \
4184 entries[n].overlay = (OVERLAY); \
4185 priority = Foverlay_get ((OVERLAY), Qpriority); \
4186 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4187 entries[n].after_string_p = (AFTER_P); \
4188 ++n; \
4189 } \
4190 while (0)
4191
4192 /* Process overlay before the overlay center. */
4193 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4194 {
4195 XSETMISC (overlay, ov);
4196 xassert (OVERLAYP (overlay));
4197 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4198 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4199
4200 if (end < charpos)
4201 break;
4202
4203 /* Skip this overlay if it doesn't start or end at IT's current
4204 position. */
4205 if (end != charpos && start != charpos)
4206 continue;
4207
4208 /* Skip this overlay if it doesn't apply to IT->w. */
4209 window = Foverlay_get (overlay, Qwindow);
4210 if (WINDOWP (window) && XWINDOW (window) != it->w)
4211 continue;
4212
4213 /* If the text ``under'' the overlay is invisible, both before-
4214 and after-strings from this overlay are visible; start and
4215 end position are indistinguishable. */
4216 invisible = Foverlay_get (overlay, Qinvisible);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4218
4219 /* If overlay has a non-empty before-string, record it. */
4220 if ((start == charpos || (end == charpos && invis_p))
4221 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4222 && SCHARS (str))
4223 RECORD_OVERLAY_STRING (overlay, str, 0);
4224
4225 /* If overlay has a non-empty after-string, record it. */
4226 if ((end == charpos || (start == charpos && invis_p))
4227 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4228 && SCHARS (str))
4229 RECORD_OVERLAY_STRING (overlay, str, 1);
4230 }
4231
4232 /* Process overlays after the overlay center. */
4233 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4234 {
4235 XSETMISC (overlay, ov);
4236 xassert (OVERLAYP (overlay));
4237 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4238 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4239
4240 if (start > charpos)
4241 break;
4242
4243 /* Skip this overlay if it doesn't start or end at IT's current
4244 position. */
4245 if (end != charpos && start != charpos)
4246 continue;
4247
4248 /* Skip this overlay if it doesn't apply to IT->w. */
4249 window = Foverlay_get (overlay, Qwindow);
4250 if (WINDOWP (window) && XWINDOW (window) != it->w)
4251 continue;
4252
4253 /* If the text ``under'' the overlay is invisible, it has a zero
4254 dimension, and both before- and after-strings apply. */
4255 invisible = Foverlay_get (overlay, Qinvisible);
4256 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4257
4258 /* If overlay has a non-empty before-string, record it. */
4259 if ((start == charpos || (end == charpos && invis_p))
4260 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4261 && SCHARS (str))
4262 RECORD_OVERLAY_STRING (overlay, str, 0);
4263
4264 /* If overlay has a non-empty after-string, record it. */
4265 if ((end == charpos || (start == charpos && invis_p))
4266 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4267 && SCHARS (str))
4268 RECORD_OVERLAY_STRING (overlay, str, 1);
4269 }
4270
4271 #undef RECORD_OVERLAY_STRING
4272
4273 /* Sort entries. */
4274 if (n > 1)
4275 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4276
4277 /* Record the total number of strings to process. */
4278 it->n_overlay_strings = n;
4279
4280 /* IT->current.overlay_string_index is the number of overlay strings
4281 that have already been consumed by IT. Copy some of the
4282 remaining overlay strings to IT->overlay_strings. */
4283 i = 0;
4284 j = it->current.overlay_string_index;
4285 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4286 it->overlay_strings[i++] = entries[j++].string;
4287
4288 CHECK_IT (it);
4289 }
4290
4291
4292 /* Get the first chunk of overlay strings at IT's current buffer
4293 position, or at CHARPOS if that is > 0. Value is non-zero if at
4294 least one overlay string was found. */
4295
4296 static int
4297 get_overlay_strings (it, charpos)
4298 struct it *it;
4299 int charpos;
4300 {
4301 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4302 process. This fills IT->overlay_strings with strings, and sets
4303 IT->n_overlay_strings to the total number of strings to process.
4304 IT->pos.overlay_string_index has to be set temporarily to zero
4305 because load_overlay_strings needs this; it must be set to -1
4306 when no overlay strings are found because a zero value would
4307 indicate a position in the first overlay string. */
4308 it->current.overlay_string_index = 0;
4309 load_overlay_strings (it, charpos);
4310
4311 /* If we found overlay strings, set up IT to deliver display
4312 elements from the first one. Otherwise set up IT to deliver
4313 from current_buffer. */
4314 if (it->n_overlay_strings)
4315 {
4316 /* Make sure we know settings in current_buffer, so that we can
4317 restore meaningful values when we're done with the overlay
4318 strings. */
4319 compute_stop_pos (it);
4320 xassert (it->face_id >= 0);
4321
4322 /* Save IT's settings. They are restored after all overlay
4323 strings have been processed. */
4324 xassert (it->sp == 0);
4325 push_it (it);
4326
4327 /* Set up IT to deliver display elements from the first overlay
4328 string. */
4329 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4330 it->string = it->overlay_strings[0];
4331 it->stop_charpos = 0;
4332 xassert (STRINGP (it->string));
4333 it->end_charpos = SCHARS (it->string);
4334 it->multibyte_p = STRING_MULTIBYTE (it->string);
4335 it->method = next_element_from_string;
4336 }
4337 else
4338 {
4339 it->string = Qnil;
4340 it->current.overlay_string_index = -1;
4341 it->method = next_element_from_buffer;
4342 }
4343
4344 CHECK_IT (it);
4345
4346 /* Value is non-zero if we found at least one overlay string. */
4347 return STRINGP (it->string);
4348 }
4349
4350
4351 \f
4352 /***********************************************************************
4353 Saving and restoring state
4354 ***********************************************************************/
4355
4356 /* Save current settings of IT on IT->stack. Called, for example,
4357 before setting up IT for an overlay string, to be able to restore
4358 IT's settings to what they were after the overlay string has been
4359 processed. */
4360
4361 static void
4362 push_it (it)
4363 struct it *it;
4364 {
4365 struct iterator_stack_entry *p;
4366
4367 xassert (it->sp < 2);
4368 p = it->stack + it->sp;
4369
4370 p->stop_charpos = it->stop_charpos;
4371 xassert (it->face_id >= 0);
4372 p->face_id = it->face_id;
4373 p->string = it->string;
4374 p->pos = it->current;
4375 p->end_charpos = it->end_charpos;
4376 p->string_nchars = it->string_nchars;
4377 p->area = it->area;
4378 p->multibyte_p = it->multibyte_p;
4379 p->slice = it->slice;
4380 p->space_width = it->space_width;
4381 p->font_height = it->font_height;
4382 p->voffset = it->voffset;
4383 p->string_from_display_prop_p = it->string_from_display_prop_p;
4384 p->display_ellipsis_p = 0;
4385 ++it->sp;
4386 }
4387
4388
4389 /* Restore IT's settings from IT->stack. Called, for example, when no
4390 more overlay strings must be processed, and we return to delivering
4391 display elements from a buffer, or when the end of a string from a
4392 `display' property is reached and we return to delivering display
4393 elements from an overlay string, or from a buffer. */
4394
4395 static void
4396 pop_it (it)
4397 struct it *it;
4398 {
4399 struct iterator_stack_entry *p;
4400
4401 xassert (it->sp > 0);
4402 --it->sp;
4403 p = it->stack + it->sp;
4404 it->stop_charpos = p->stop_charpos;
4405 it->face_id = p->face_id;
4406 it->string = p->string;
4407 it->current = p->pos;
4408 it->end_charpos = p->end_charpos;
4409 it->string_nchars = p->string_nchars;
4410 it->area = p->area;
4411 it->multibyte_p = p->multibyte_p;
4412 it->slice = p->slice;
4413 it->space_width = p->space_width;
4414 it->font_height = p->font_height;
4415 it->voffset = p->voffset;
4416 it->string_from_display_prop_p = p->string_from_display_prop_p;
4417 }
4418
4419
4420 \f
4421 /***********************************************************************
4422 Moving over lines
4423 ***********************************************************************/
4424
4425 /* Set IT's current position to the previous line start. */
4426
4427 static void
4428 back_to_previous_line_start (it)
4429 struct it *it;
4430 {
4431 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4432 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4433 }
4434
4435
4436 /* Move IT to the next line start.
4437
4438 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4439 we skipped over part of the text (as opposed to moving the iterator
4440 continuously over the text). Otherwise, don't change the value
4441 of *SKIPPED_P.
4442
4443 Newlines may come from buffer text, overlay strings, or strings
4444 displayed via the `display' property. That's the reason we can't
4445 simply use find_next_newline_no_quit.
4446
4447 Note that this function may not skip over invisible text that is so
4448 because of text properties and immediately follows a newline. If
4449 it would, function reseat_at_next_visible_line_start, when called
4450 from set_iterator_to_next, would effectively make invisible
4451 characters following a newline part of the wrong glyph row, which
4452 leads to wrong cursor motion. */
4453
4454 static int
4455 forward_to_next_line_start (it, skipped_p)
4456 struct it *it;
4457 int *skipped_p;
4458 {
4459 int old_selective, newline_found_p, n;
4460 const int MAX_NEWLINE_DISTANCE = 500;
4461
4462 /* If already on a newline, just consume it to avoid unintended
4463 skipping over invisible text below. */
4464 if (it->what == IT_CHARACTER
4465 && it->c == '\n'
4466 && CHARPOS (it->position) == IT_CHARPOS (*it))
4467 {
4468 set_iterator_to_next (it, 0);
4469 it->c = 0;
4470 return 1;
4471 }
4472
4473 /* Don't handle selective display in the following. It's (a)
4474 unnecessary because it's done by the caller, and (b) leads to an
4475 infinite recursion because next_element_from_ellipsis indirectly
4476 calls this function. */
4477 old_selective = it->selective;
4478 it->selective = 0;
4479
4480 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4481 from buffer text. */
4482 for (n = newline_found_p = 0;
4483 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4484 n += STRINGP (it->string) ? 0 : 1)
4485 {
4486 if (!get_next_display_element (it))
4487 return 0;
4488 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4489 set_iterator_to_next (it, 0);
4490 }
4491
4492 /* If we didn't find a newline near enough, see if we can use a
4493 short-cut. */
4494 if (!newline_found_p)
4495 {
4496 int start = IT_CHARPOS (*it);
4497 int limit = find_next_newline_no_quit (start, 1);
4498 Lisp_Object pos;
4499
4500 xassert (!STRINGP (it->string));
4501
4502 /* If there isn't any `display' property in sight, and no
4503 overlays, we can just use the position of the newline in
4504 buffer text. */
4505 if (it->stop_charpos >= limit
4506 || ((pos = Fnext_single_property_change (make_number (start),
4507 Qdisplay,
4508 Qnil, make_number (limit)),
4509 NILP (pos))
4510 && next_overlay_change (start) == ZV))
4511 {
4512 IT_CHARPOS (*it) = limit;
4513 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4514 *skipped_p = newline_found_p = 1;
4515 }
4516 else
4517 {
4518 while (get_next_display_element (it)
4519 && !newline_found_p)
4520 {
4521 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4522 set_iterator_to_next (it, 0);
4523 }
4524 }
4525 }
4526
4527 it->selective = old_selective;
4528 return newline_found_p;
4529 }
4530
4531
4532 /* Set IT's current position to the previous visible line start. Skip
4533 invisible text that is so either due to text properties or due to
4534 selective display. Caution: this does not change IT->current_x and
4535 IT->hpos. */
4536
4537 static void
4538 back_to_previous_visible_line_start (it)
4539 struct it *it;
4540 {
4541 int visible_p = 0;
4542
4543 /* Go back one newline if not on BEGV already. */
4544 if (IT_CHARPOS (*it) > BEGV)
4545 back_to_previous_line_start (it);
4546
4547 /* Move over lines that are invisible because of selective display
4548 or text properties. */
4549 while (IT_CHARPOS (*it) > BEGV
4550 && !visible_p)
4551 {
4552 visible_p = 1;
4553
4554 /* If selective > 0, then lines indented more than that values
4555 are invisible. */
4556 if (it->selective > 0
4557 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4558 (double) it->selective)) /* iftc */
4559 visible_p = 0;
4560 else
4561 {
4562 Lisp_Object prop;
4563
4564 /* Check the newline before point for invisibility. */
4565 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4566 Qinvisible, it->window);
4567 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4568 visible_p = 0;
4569 }
4570
4571 if (visible_p)
4572 {
4573 struct it it2 = *it;
4574
4575 if (handle_display_prop (&it2) == HANDLED_RETURN)
4576 visible_p = 0;
4577 }
4578
4579 /* Back one more newline if the current one is invisible. */
4580 if (!visible_p)
4581 back_to_previous_line_start (it);
4582 }
4583
4584 xassert (IT_CHARPOS (*it) >= BEGV);
4585 xassert (IT_CHARPOS (*it) == BEGV
4586 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4587 CHECK_IT (it);
4588 }
4589
4590
4591 /* Reseat iterator IT at the previous visible line start. Skip
4592 invisible text that is so either due to text properties or due to
4593 selective display. At the end, update IT's overlay information,
4594 face information etc. */
4595
4596 void
4597 reseat_at_previous_visible_line_start (it)
4598 struct it *it;
4599 {
4600 back_to_previous_visible_line_start (it);
4601 reseat (it, it->current.pos, 1);
4602 CHECK_IT (it);
4603 }
4604
4605
4606 /* Reseat iterator IT on the next visible line start in the current
4607 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4608 preceding the line start. Skip over invisible text that is so
4609 because of selective display. Compute faces, overlays etc at the
4610 new position. Note that this function does not skip over text that
4611 is invisible because of text properties. */
4612
4613 static void
4614 reseat_at_next_visible_line_start (it, on_newline_p)
4615 struct it *it;
4616 int on_newline_p;
4617 {
4618 int newline_found_p, skipped_p = 0;
4619
4620 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4621
4622 /* Skip over lines that are invisible because they are indented
4623 more than the value of IT->selective. */
4624 if (it->selective > 0)
4625 while (IT_CHARPOS (*it) < ZV
4626 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4627 (double) it->selective)) /* iftc */
4628 {
4629 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4630 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4631 }
4632
4633 /* Position on the newline if that's what's requested. */
4634 if (on_newline_p && newline_found_p)
4635 {
4636 if (STRINGP (it->string))
4637 {
4638 if (IT_STRING_CHARPOS (*it) > 0)
4639 {
4640 --IT_STRING_CHARPOS (*it);
4641 --IT_STRING_BYTEPOS (*it);
4642 }
4643 }
4644 else if (IT_CHARPOS (*it) > BEGV)
4645 {
4646 --IT_CHARPOS (*it);
4647 --IT_BYTEPOS (*it);
4648 reseat (it, it->current.pos, 0);
4649 }
4650 }
4651 else if (skipped_p)
4652 reseat (it, it->current.pos, 0);
4653
4654 CHECK_IT (it);
4655 }
4656
4657
4658 \f
4659 /***********************************************************************
4660 Changing an iterator's position
4661 ***********************************************************************/
4662
4663 /* Change IT's current position to POS in current_buffer. If FORCE_P
4664 is non-zero, always check for text properties at the new position.
4665 Otherwise, text properties are only looked up if POS >=
4666 IT->check_charpos of a property. */
4667
4668 static void
4669 reseat (it, pos, force_p)
4670 struct it *it;
4671 struct text_pos pos;
4672 int force_p;
4673 {
4674 int original_pos = IT_CHARPOS (*it);
4675
4676 reseat_1 (it, pos, 0);
4677
4678 /* Determine where to check text properties. Avoid doing it
4679 where possible because text property lookup is very expensive. */
4680 if (force_p
4681 || CHARPOS (pos) > it->stop_charpos
4682 || CHARPOS (pos) < original_pos)
4683 handle_stop (it);
4684
4685 CHECK_IT (it);
4686 }
4687
4688
4689 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4690 IT->stop_pos to POS, also. */
4691
4692 static void
4693 reseat_1 (it, pos, set_stop_p)
4694 struct it *it;
4695 struct text_pos pos;
4696 int set_stop_p;
4697 {
4698 /* Don't call this function when scanning a C string. */
4699 xassert (it->s == NULL);
4700
4701 /* POS must be a reasonable value. */
4702 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4703
4704 it->current.pos = it->position = pos;
4705 XSETBUFFER (it->object, current_buffer);
4706 it->end_charpos = ZV;
4707 it->dpvec = NULL;
4708 it->current.dpvec_index = -1;
4709 it->current.overlay_string_index = -1;
4710 IT_STRING_CHARPOS (*it) = -1;
4711 IT_STRING_BYTEPOS (*it) = -1;
4712 it->string = Qnil;
4713 it->method = next_element_from_buffer;
4714 /* RMS: I added this to fix a bug in move_it_vertically_backward
4715 where it->area continued to relate to the starting point
4716 for the backward motion. Bug report from
4717 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4718 However, I am not sure whether reseat still does the right thing
4719 in general after this change. */
4720 it->area = TEXT_AREA;
4721 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4722 it->sp = 0;
4723 it->face_before_selective_p = 0;
4724
4725 if (set_stop_p)
4726 it->stop_charpos = CHARPOS (pos);
4727 }
4728
4729
4730 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4731 If S is non-null, it is a C string to iterate over. Otherwise,
4732 STRING gives a Lisp string to iterate over.
4733
4734 If PRECISION > 0, don't return more then PRECISION number of
4735 characters from the string.
4736
4737 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4738 characters have been returned. FIELD_WIDTH < 0 means an infinite
4739 field width.
4740
4741 MULTIBYTE = 0 means disable processing of multibyte characters,
4742 MULTIBYTE > 0 means enable it,
4743 MULTIBYTE < 0 means use IT->multibyte_p.
4744
4745 IT must be initialized via a prior call to init_iterator before
4746 calling this function. */
4747
4748 static void
4749 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4750 struct it *it;
4751 unsigned char *s;
4752 Lisp_Object string;
4753 int charpos;
4754 int precision, field_width, multibyte;
4755 {
4756 /* No region in strings. */
4757 it->region_beg_charpos = it->region_end_charpos = -1;
4758
4759 /* No text property checks performed by default, but see below. */
4760 it->stop_charpos = -1;
4761
4762 /* Set iterator position and end position. */
4763 bzero (&it->current, sizeof it->current);
4764 it->current.overlay_string_index = -1;
4765 it->current.dpvec_index = -1;
4766 xassert (charpos >= 0);
4767
4768 /* If STRING is specified, use its multibyteness, otherwise use the
4769 setting of MULTIBYTE, if specified. */
4770 if (multibyte >= 0)
4771 it->multibyte_p = multibyte > 0;
4772
4773 if (s == NULL)
4774 {
4775 xassert (STRINGP (string));
4776 it->string = string;
4777 it->s = NULL;
4778 it->end_charpos = it->string_nchars = SCHARS (string);
4779 it->method = next_element_from_string;
4780 it->current.string_pos = string_pos (charpos, string);
4781 }
4782 else
4783 {
4784 it->s = s;
4785 it->string = Qnil;
4786
4787 /* Note that we use IT->current.pos, not it->current.string_pos,
4788 for displaying C strings. */
4789 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4790 if (it->multibyte_p)
4791 {
4792 it->current.pos = c_string_pos (charpos, s, 1);
4793 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4794 }
4795 else
4796 {
4797 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4798 it->end_charpos = it->string_nchars = strlen (s);
4799 }
4800
4801 it->method = next_element_from_c_string;
4802 }
4803
4804 /* PRECISION > 0 means don't return more than PRECISION characters
4805 from the string. */
4806 if (precision > 0 && it->end_charpos - charpos > precision)
4807 it->end_charpos = it->string_nchars = charpos + precision;
4808
4809 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4810 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4811 FIELD_WIDTH < 0 means infinite field width. This is useful for
4812 padding with `-' at the end of a mode line. */
4813 if (field_width < 0)
4814 field_width = INFINITY;
4815 if (field_width > it->end_charpos - charpos)
4816 it->end_charpos = charpos + field_width;
4817
4818 /* Use the standard display table for displaying strings. */
4819 if (DISP_TABLE_P (Vstandard_display_table))
4820 it->dp = XCHAR_TABLE (Vstandard_display_table);
4821
4822 it->stop_charpos = charpos;
4823 CHECK_IT (it);
4824 }
4825
4826
4827 \f
4828 /***********************************************************************
4829 Iteration
4830 ***********************************************************************/
4831
4832 /* Load IT's display element fields with information about the next
4833 display element from the current position of IT. Value is zero if
4834 end of buffer (or C string) is reached. */
4835
4836 int
4837 get_next_display_element (it)
4838 struct it *it;
4839 {
4840 /* Non-zero means that we found a display element. Zero means that
4841 we hit the end of what we iterate over. Performance note: the
4842 function pointer `method' used here turns out to be faster than
4843 using a sequence of if-statements. */
4844 int success_p = (*it->method) (it);
4845
4846 if (it->what == IT_CHARACTER)
4847 {
4848 /* Map via display table or translate control characters.
4849 IT->c, IT->len etc. have been set to the next character by
4850 the function call above. If we have a display table, and it
4851 contains an entry for IT->c, translate it. Don't do this if
4852 IT->c itself comes from a display table, otherwise we could
4853 end up in an infinite recursion. (An alternative could be to
4854 count the recursion depth of this function and signal an
4855 error when a certain maximum depth is reached.) Is it worth
4856 it? */
4857 if (success_p && it->dpvec == NULL)
4858 {
4859 Lisp_Object dv;
4860
4861 if (it->dp
4862 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4863 VECTORP (dv)))
4864 {
4865 struct Lisp_Vector *v = XVECTOR (dv);
4866
4867 /* Return the first character from the display table
4868 entry, if not empty. If empty, don't display the
4869 current character. */
4870 if (v->size)
4871 {
4872 it->dpvec_char_len = it->len;
4873 it->dpvec = v->contents;
4874 it->dpend = v->contents + v->size;
4875 it->current.dpvec_index = 0;
4876 it->method = next_element_from_display_vector;
4877 success_p = get_next_display_element (it);
4878 }
4879 else
4880 {
4881 set_iterator_to_next (it, 0);
4882 success_p = get_next_display_element (it);
4883 }
4884 }
4885
4886 /* Translate control characters into `\003' or `^C' form.
4887 Control characters coming from a display table entry are
4888 currently not translated because we use IT->dpvec to hold
4889 the translation. This could easily be changed but I
4890 don't believe that it is worth doing.
4891
4892 If it->multibyte_p is nonzero, eight-bit characters and
4893 non-printable multibyte characters are also translated to
4894 octal form.
4895
4896 If it->multibyte_p is zero, eight-bit characters that
4897 don't have corresponding multibyte char code are also
4898 translated to octal form. */
4899 else if ((it->c < ' '
4900 && (it->area != TEXT_AREA
4901 /* In mode line, treat \n like other crl chars. */
4902 || (it->c != '\n'
4903 && it->glyph_row && it->glyph_row->mode_line_p)
4904 || (it->c != '\n' && it->c != '\t')))
4905 || (it->multibyte_p
4906 ? ((it->c >= 127
4907 && it->len == 1)
4908 || !CHAR_PRINTABLE_P (it->c))
4909 : (it->c >= 127
4910 && (!unibyte_display_via_language_environment
4911 || it->c == unibyte_char_to_multibyte (it->c)))))
4912 {
4913 /* IT->c is a control character which must be displayed
4914 either as '\003' or as `^C' where the '\\' and '^'
4915 can be defined in the display table. Fill
4916 IT->ctl_chars with glyphs for what we have to
4917 display. Then, set IT->dpvec to these glyphs. */
4918 GLYPH g;
4919
4920 if (it->c < 128 && it->ctl_arrow_p)
4921 {
4922 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4923 if (it->dp
4924 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4925 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4926 g = XINT (DISP_CTRL_GLYPH (it->dp));
4927 else
4928 g = FAST_MAKE_GLYPH ('^', 0);
4929 XSETINT (it->ctl_chars[0], g);
4930
4931 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4932 XSETINT (it->ctl_chars[1], g);
4933
4934 /* Set up IT->dpvec and return first character from it. */
4935 it->dpvec_char_len = it->len;
4936 it->dpvec = it->ctl_chars;
4937 it->dpend = it->dpvec + 2;
4938 it->current.dpvec_index = 0;
4939 it->method = next_element_from_display_vector;
4940 get_next_display_element (it);
4941 }
4942 else
4943 {
4944 unsigned char str[MAX_MULTIBYTE_LENGTH];
4945 int len;
4946 int i;
4947 GLYPH escape_glyph;
4948
4949 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4950 if (it->dp
4951 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4952 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4953 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4954 else
4955 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4956
4957 if (SINGLE_BYTE_CHAR_P (it->c))
4958 str[0] = it->c, len = 1;
4959 else
4960 {
4961 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4962 if (len < 0)
4963 {
4964 /* It's an invalid character, which
4965 shouldn't happen actually, but due to
4966 bugs it may happen. Let's print the char
4967 as is, there's not much meaningful we can
4968 do with it. */
4969 str[0] = it->c;
4970 str[1] = it->c >> 8;
4971 str[2] = it->c >> 16;
4972 str[3] = it->c >> 24;
4973 len = 4;
4974 }
4975 }
4976
4977 for (i = 0; i < len; i++)
4978 {
4979 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4980 /* Insert three more glyphs into IT->ctl_chars for
4981 the octal display of the character. */
4982 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4983 XSETINT (it->ctl_chars[i * 4 + 1], g);
4984 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4985 XSETINT (it->ctl_chars[i * 4 + 2], g);
4986 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4987 XSETINT (it->ctl_chars[i * 4 + 3], g);
4988 }
4989
4990 /* Set up IT->dpvec and return the first character
4991 from it. */
4992 it->dpvec_char_len = it->len;
4993 it->dpvec = it->ctl_chars;
4994 it->dpend = it->dpvec + len * 4;
4995 it->current.dpvec_index = 0;
4996 it->method = next_element_from_display_vector;
4997 get_next_display_element (it);
4998 }
4999 }
5000 }
5001
5002 /* Adjust face id for a multibyte character. There are no
5003 multibyte character in unibyte text. */
5004 if (it->multibyte_p
5005 && success_p
5006 && FRAME_WINDOW_P (it->f))
5007 {
5008 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5009 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5010 }
5011 }
5012
5013 /* Is this character the last one of a run of characters with
5014 box? If yes, set IT->end_of_box_run_p to 1. */
5015 if (it->face_box_p
5016 && it->s == NULL)
5017 {
5018 int face_id;
5019 struct face *face;
5020
5021 it->end_of_box_run_p
5022 = ((face_id = face_after_it_pos (it),
5023 face_id != it->face_id)
5024 && (face = FACE_FROM_ID (it->f, face_id),
5025 face->box == FACE_NO_BOX));
5026 }
5027
5028 /* Value is 0 if end of buffer or string reached. */
5029 return success_p;
5030 }
5031
5032
5033 /* Move IT to the next display element.
5034
5035 RESEAT_P non-zero means if called on a newline in buffer text,
5036 skip to the next visible line start.
5037
5038 Functions get_next_display_element and set_iterator_to_next are
5039 separate because I find this arrangement easier to handle than a
5040 get_next_display_element function that also increments IT's
5041 position. The way it is we can first look at an iterator's current
5042 display element, decide whether it fits on a line, and if it does,
5043 increment the iterator position. The other way around we probably
5044 would either need a flag indicating whether the iterator has to be
5045 incremented the next time, or we would have to implement a
5046 decrement position function which would not be easy to write. */
5047
5048 void
5049 set_iterator_to_next (it, reseat_p)
5050 struct it *it;
5051 int reseat_p;
5052 {
5053 /* Reset flags indicating start and end of a sequence of characters
5054 with box. Reset them at the start of this function because
5055 moving the iterator to a new position might set them. */
5056 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5057
5058 if (it->method == next_element_from_buffer)
5059 {
5060 /* The current display element of IT is a character from
5061 current_buffer. Advance in the buffer, and maybe skip over
5062 invisible lines that are so because of selective display. */
5063 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5064 reseat_at_next_visible_line_start (it, 0);
5065 else
5066 {
5067 xassert (it->len != 0);
5068 IT_BYTEPOS (*it) += it->len;
5069 IT_CHARPOS (*it) += 1;
5070 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5071 }
5072 }
5073 else if (it->method == next_element_from_composition)
5074 {
5075 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5076 if (STRINGP (it->string))
5077 {
5078 IT_STRING_BYTEPOS (*it) += it->len;
5079 IT_STRING_CHARPOS (*it) += it->cmp_len;
5080 it->method = next_element_from_string;
5081 goto consider_string_end;
5082 }
5083 else
5084 {
5085 IT_BYTEPOS (*it) += it->len;
5086 IT_CHARPOS (*it) += it->cmp_len;
5087 it->method = next_element_from_buffer;
5088 }
5089 }
5090 else if (it->method == next_element_from_c_string)
5091 {
5092 /* Current display element of IT is from a C string. */
5093 IT_BYTEPOS (*it) += it->len;
5094 IT_CHARPOS (*it) += 1;
5095 }
5096 else if (it->method == next_element_from_display_vector)
5097 {
5098 /* Current display element of IT is from a display table entry.
5099 Advance in the display table definition. Reset it to null if
5100 end reached, and continue with characters from buffers/
5101 strings. */
5102 ++it->current.dpvec_index;
5103
5104 /* Restore face of the iterator to what they were before the
5105 display vector entry (these entries may contain faces). */
5106 it->face_id = it->saved_face_id;
5107
5108 if (it->dpvec + it->current.dpvec_index == it->dpend)
5109 {
5110 if (it->s)
5111 it->method = next_element_from_c_string;
5112 else if (STRINGP (it->string))
5113 it->method = next_element_from_string;
5114 else
5115 it->method = next_element_from_buffer;
5116
5117 it->dpvec = NULL;
5118 it->current.dpvec_index = -1;
5119
5120 /* Skip over characters which were displayed via IT->dpvec. */
5121 if (it->dpvec_char_len < 0)
5122 reseat_at_next_visible_line_start (it, 1);
5123 else if (it->dpvec_char_len > 0)
5124 {
5125 it->len = it->dpvec_char_len;
5126 set_iterator_to_next (it, reseat_p);
5127 }
5128 }
5129 }
5130 else if (it->method == next_element_from_string)
5131 {
5132 /* Current display element is a character from a Lisp string. */
5133 xassert (it->s == NULL && STRINGP (it->string));
5134 IT_STRING_BYTEPOS (*it) += it->len;
5135 IT_STRING_CHARPOS (*it) += 1;
5136
5137 consider_string_end:
5138
5139 if (it->current.overlay_string_index >= 0)
5140 {
5141 /* IT->string is an overlay string. Advance to the
5142 next, if there is one. */
5143 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5144 next_overlay_string (it);
5145 }
5146 else
5147 {
5148 /* IT->string is not an overlay string. If we reached
5149 its end, and there is something on IT->stack, proceed
5150 with what is on the stack. This can be either another
5151 string, this time an overlay string, or a buffer. */
5152 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5153 && it->sp > 0)
5154 {
5155 pop_it (it);
5156 if (!STRINGP (it->string))
5157 it->method = next_element_from_buffer;
5158 else
5159 goto consider_string_end;
5160 }
5161 }
5162 }
5163 else if (it->method == next_element_from_image
5164 || it->method == next_element_from_stretch)
5165 {
5166 /* The position etc with which we have to proceed are on
5167 the stack. The position may be at the end of a string,
5168 if the `display' property takes up the whole string. */
5169 pop_it (it);
5170 it->image_id = 0;
5171 if (STRINGP (it->string))
5172 {
5173 it->method = next_element_from_string;
5174 goto consider_string_end;
5175 }
5176 else
5177 it->method = next_element_from_buffer;
5178 }
5179 else
5180 /* There are no other methods defined, so this should be a bug. */
5181 abort ();
5182
5183 xassert (it->method != next_element_from_string
5184 || (STRINGP (it->string)
5185 && IT_STRING_CHARPOS (*it) >= 0));
5186 }
5187
5188
5189 /* Load IT's display element fields with information about the next
5190 display element which comes from a display table entry or from the
5191 result of translating a control character to one of the forms `^C'
5192 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5193
5194 static int
5195 next_element_from_display_vector (it)
5196 struct it *it;
5197 {
5198 /* Precondition. */
5199 xassert (it->dpvec && it->current.dpvec_index >= 0);
5200
5201 /* Remember the current face id in case glyphs specify faces.
5202 IT's face is restored in set_iterator_to_next. */
5203 it->saved_face_id = it->face_id;
5204
5205 if (INTEGERP (*it->dpvec)
5206 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5207 {
5208 int lface_id;
5209 GLYPH g;
5210
5211 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5212 it->c = FAST_GLYPH_CHAR (g);
5213 it->len = CHAR_BYTES (it->c);
5214
5215 /* The entry may contain a face id to use. Such a face id is
5216 the id of a Lisp face, not a realized face. A face id of
5217 zero means no face is specified. */
5218 lface_id = FAST_GLYPH_FACE (g);
5219 if (lface_id)
5220 {
5221 /* The function returns -1 if lface_id is invalid. */
5222 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5223 if (face_id >= 0)
5224 it->face_id = face_id;
5225 }
5226 }
5227 else
5228 /* Display table entry is invalid. Return a space. */
5229 it->c = ' ', it->len = 1;
5230
5231 /* Don't change position and object of the iterator here. They are
5232 still the values of the character that had this display table
5233 entry or was translated, and that's what we want. */
5234 it->what = IT_CHARACTER;
5235 return 1;
5236 }
5237
5238
5239 /* Load IT with the next display element from Lisp string IT->string.
5240 IT->current.string_pos is the current position within the string.
5241 If IT->current.overlay_string_index >= 0, the Lisp string is an
5242 overlay string. */
5243
5244 static int
5245 next_element_from_string (it)
5246 struct it *it;
5247 {
5248 struct text_pos position;
5249
5250 xassert (STRINGP (it->string));
5251 xassert (IT_STRING_CHARPOS (*it) >= 0);
5252 position = it->current.string_pos;
5253
5254 /* Time to check for invisible text? */
5255 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5256 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5257 {
5258 handle_stop (it);
5259
5260 /* Since a handler may have changed IT->method, we must
5261 recurse here. */
5262 return get_next_display_element (it);
5263 }
5264
5265 if (it->current.overlay_string_index >= 0)
5266 {
5267 /* Get the next character from an overlay string. In overlay
5268 strings, There is no field width or padding with spaces to
5269 do. */
5270 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5271 {
5272 it->what = IT_EOB;
5273 return 0;
5274 }
5275 else if (STRING_MULTIBYTE (it->string))
5276 {
5277 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5278 const unsigned char *s = (SDATA (it->string)
5279 + IT_STRING_BYTEPOS (*it));
5280 it->c = string_char_and_length (s, remaining, &it->len);
5281 }
5282 else
5283 {
5284 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5285 it->len = 1;
5286 }
5287 }
5288 else
5289 {
5290 /* Get the next character from a Lisp string that is not an
5291 overlay string. Such strings come from the mode line, for
5292 example. We may have to pad with spaces, or truncate the
5293 string. See also next_element_from_c_string. */
5294 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5295 {
5296 it->what = IT_EOB;
5297 return 0;
5298 }
5299 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5300 {
5301 /* Pad with spaces. */
5302 it->c = ' ', it->len = 1;
5303 CHARPOS (position) = BYTEPOS (position) = -1;
5304 }
5305 else if (STRING_MULTIBYTE (it->string))
5306 {
5307 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5308 const unsigned char *s = (SDATA (it->string)
5309 + IT_STRING_BYTEPOS (*it));
5310 it->c = string_char_and_length (s, maxlen, &it->len);
5311 }
5312 else
5313 {
5314 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5315 it->len = 1;
5316 }
5317 }
5318
5319 /* Record what we have and where it came from. Note that we store a
5320 buffer position in IT->position although it could arguably be a
5321 string position. */
5322 it->what = IT_CHARACTER;
5323 it->object = it->string;
5324 it->position = position;
5325 return 1;
5326 }
5327
5328
5329 /* Load IT with next display element from C string IT->s.
5330 IT->string_nchars is the maximum number of characters to return
5331 from the string. IT->end_charpos may be greater than
5332 IT->string_nchars when this function is called, in which case we
5333 may have to return padding spaces. Value is zero if end of string
5334 reached, including padding spaces. */
5335
5336 static int
5337 next_element_from_c_string (it)
5338 struct it *it;
5339 {
5340 int success_p = 1;
5341
5342 xassert (it->s);
5343 it->what = IT_CHARACTER;
5344 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5345 it->object = Qnil;
5346
5347 /* IT's position can be greater IT->string_nchars in case a field
5348 width or precision has been specified when the iterator was
5349 initialized. */
5350 if (IT_CHARPOS (*it) >= it->end_charpos)
5351 {
5352 /* End of the game. */
5353 it->what = IT_EOB;
5354 success_p = 0;
5355 }
5356 else if (IT_CHARPOS (*it) >= it->string_nchars)
5357 {
5358 /* Pad with spaces. */
5359 it->c = ' ', it->len = 1;
5360 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5361 }
5362 else if (it->multibyte_p)
5363 {
5364 /* Implementation note: The calls to strlen apparently aren't a
5365 performance problem because there is no noticeable performance
5366 difference between Emacs running in unibyte or multibyte mode. */
5367 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5368 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5369 maxlen, &it->len);
5370 }
5371 else
5372 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5373
5374 return success_p;
5375 }
5376
5377
5378 /* Set up IT to return characters from an ellipsis, if appropriate.
5379 The definition of the ellipsis glyphs may come from a display table
5380 entry. This function Fills IT with the first glyph from the
5381 ellipsis if an ellipsis is to be displayed. */
5382
5383 static int
5384 next_element_from_ellipsis (it)
5385 struct it *it;
5386 {
5387 if (it->selective_display_ellipsis_p)
5388 {
5389 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5390 {
5391 /* Use the display table definition for `...'. Invalid glyphs
5392 will be handled by the method returning elements from dpvec. */
5393 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5394 it->dpvec_char_len = it->len;
5395 it->dpvec = v->contents;
5396 it->dpend = v->contents + v->size;
5397 it->current.dpvec_index = 0;
5398 it->method = next_element_from_display_vector;
5399 }
5400 else
5401 {
5402 /* Use default `...' which is stored in default_invis_vector. */
5403 it->dpvec_char_len = it->len;
5404 it->dpvec = default_invis_vector;
5405 it->dpend = default_invis_vector + 3;
5406 it->current.dpvec_index = 0;
5407 it->method = next_element_from_display_vector;
5408 }
5409 }
5410 else
5411 {
5412 /* The face at the current position may be different from the
5413 face we find after the invisible text. Remember what it
5414 was in IT->saved_face_id, and signal that it's there by
5415 setting face_before_selective_p. */
5416 it->saved_face_id = it->face_id;
5417 it->method = next_element_from_buffer;
5418 reseat_at_next_visible_line_start (it, 1);
5419 it->face_before_selective_p = 1;
5420 }
5421
5422 return get_next_display_element (it);
5423 }
5424
5425
5426 /* Deliver an image display element. The iterator IT is already
5427 filled with image information (done in handle_display_prop). Value
5428 is always 1. */
5429
5430
5431 static int
5432 next_element_from_image (it)
5433 struct it *it;
5434 {
5435 it->what = IT_IMAGE;
5436 return 1;
5437 }
5438
5439
5440 /* Fill iterator IT with next display element from a stretch glyph
5441 property. IT->object is the value of the text property. Value is
5442 always 1. */
5443
5444 static int
5445 next_element_from_stretch (it)
5446 struct it *it;
5447 {
5448 it->what = IT_STRETCH;
5449 return 1;
5450 }
5451
5452
5453 /* Load IT with the next display element from current_buffer. Value
5454 is zero if end of buffer reached. IT->stop_charpos is the next
5455 position at which to stop and check for text properties or buffer
5456 end. */
5457
5458 static int
5459 next_element_from_buffer (it)
5460 struct it *it;
5461 {
5462 int success_p = 1;
5463
5464 /* Check this assumption, otherwise, we would never enter the
5465 if-statement, below. */
5466 xassert (IT_CHARPOS (*it) >= BEGV
5467 && IT_CHARPOS (*it) <= it->stop_charpos);
5468
5469 if (IT_CHARPOS (*it) >= it->stop_charpos)
5470 {
5471 if (IT_CHARPOS (*it) >= it->end_charpos)
5472 {
5473 int overlay_strings_follow_p;
5474
5475 /* End of the game, except when overlay strings follow that
5476 haven't been returned yet. */
5477 if (it->overlay_strings_at_end_processed_p)
5478 overlay_strings_follow_p = 0;
5479 else
5480 {
5481 it->overlay_strings_at_end_processed_p = 1;
5482 overlay_strings_follow_p = get_overlay_strings (it, 0);
5483 }
5484
5485 if (overlay_strings_follow_p)
5486 success_p = get_next_display_element (it);
5487 else
5488 {
5489 it->what = IT_EOB;
5490 it->position = it->current.pos;
5491 success_p = 0;
5492 }
5493 }
5494 else
5495 {
5496 handle_stop (it);
5497 return get_next_display_element (it);
5498 }
5499 }
5500 else
5501 {
5502 /* No face changes, overlays etc. in sight, so just return a
5503 character from current_buffer. */
5504 unsigned char *p;
5505
5506 /* Maybe run the redisplay end trigger hook. Performance note:
5507 This doesn't seem to cost measurable time. */
5508 if (it->redisplay_end_trigger_charpos
5509 && it->glyph_row
5510 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5511 run_redisplay_end_trigger_hook (it);
5512
5513 /* Get the next character, maybe multibyte. */
5514 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5515 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5516 {
5517 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5518 - IT_BYTEPOS (*it));
5519 it->c = string_char_and_length (p, maxlen, &it->len);
5520 }
5521 else
5522 it->c = *p, it->len = 1;
5523
5524 /* Record what we have and where it came from. */
5525 it->what = IT_CHARACTER;;
5526 it->object = it->w->buffer;
5527 it->position = it->current.pos;
5528
5529 /* Normally we return the character found above, except when we
5530 really want to return an ellipsis for selective display. */
5531 if (it->selective)
5532 {
5533 if (it->c == '\n')
5534 {
5535 /* A value of selective > 0 means hide lines indented more
5536 than that number of columns. */
5537 if (it->selective > 0
5538 && IT_CHARPOS (*it) + 1 < ZV
5539 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5540 IT_BYTEPOS (*it) + 1,
5541 (double) it->selective)) /* iftc */
5542 {
5543 success_p = next_element_from_ellipsis (it);
5544 it->dpvec_char_len = -1;
5545 }
5546 }
5547 else if (it->c == '\r' && it->selective == -1)
5548 {
5549 /* A value of selective == -1 means that everything from the
5550 CR to the end of the line is invisible, with maybe an
5551 ellipsis displayed for it. */
5552 success_p = next_element_from_ellipsis (it);
5553 it->dpvec_char_len = -1;
5554 }
5555 }
5556 }
5557
5558 /* Value is zero if end of buffer reached. */
5559 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5560 return success_p;
5561 }
5562
5563
5564 /* Run the redisplay end trigger hook for IT. */
5565
5566 static void
5567 run_redisplay_end_trigger_hook (it)
5568 struct it *it;
5569 {
5570 Lisp_Object args[3];
5571
5572 /* IT->glyph_row should be non-null, i.e. we should be actually
5573 displaying something, or otherwise we should not run the hook. */
5574 xassert (it->glyph_row);
5575
5576 /* Set up hook arguments. */
5577 args[0] = Qredisplay_end_trigger_functions;
5578 args[1] = it->window;
5579 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5580 it->redisplay_end_trigger_charpos = 0;
5581
5582 /* Since we are *trying* to run these functions, don't try to run
5583 them again, even if they get an error. */
5584 it->w->redisplay_end_trigger = Qnil;
5585 Frun_hook_with_args (3, args);
5586
5587 /* Notice if it changed the face of the character we are on. */
5588 handle_face_prop (it);
5589 }
5590
5591
5592 /* Deliver a composition display element. The iterator IT is already
5593 filled with composition information (done in
5594 handle_composition_prop). Value is always 1. */
5595
5596 static int
5597 next_element_from_composition (it)
5598 struct it *it;
5599 {
5600 it->what = IT_COMPOSITION;
5601 it->position = (STRINGP (it->string)
5602 ? it->current.string_pos
5603 : it->current.pos);
5604 return 1;
5605 }
5606
5607
5608 \f
5609 /***********************************************************************
5610 Moving an iterator without producing glyphs
5611 ***********************************************************************/
5612
5613 /* Move iterator IT to a specified buffer or X position within one
5614 line on the display without producing glyphs.
5615
5616 OP should be a bit mask including some or all of these bits:
5617 MOVE_TO_X: Stop on reaching x-position TO_X.
5618 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5619 Regardless of OP's value, stop in reaching the end of the display line.
5620
5621 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5622 This means, in particular, that TO_X includes window's horizontal
5623 scroll amount.
5624
5625 The return value has several possible values that
5626 say what condition caused the scan to stop:
5627
5628 MOVE_POS_MATCH_OR_ZV
5629 - when TO_POS or ZV was reached.
5630
5631 MOVE_X_REACHED
5632 -when TO_X was reached before TO_POS or ZV were reached.
5633
5634 MOVE_LINE_CONTINUED
5635 - when we reached the end of the display area and the line must
5636 be continued.
5637
5638 MOVE_LINE_TRUNCATED
5639 - when we reached the end of the display area and the line is
5640 truncated.
5641
5642 MOVE_NEWLINE_OR_CR
5643 - when we stopped at a line end, i.e. a newline or a CR and selective
5644 display is on. */
5645
5646 static enum move_it_result
5647 move_it_in_display_line_to (it, to_charpos, to_x, op)
5648 struct it *it;
5649 int to_charpos, to_x, op;
5650 {
5651 enum move_it_result result = MOVE_UNDEFINED;
5652 struct glyph_row *saved_glyph_row;
5653
5654 /* Don't produce glyphs in produce_glyphs. */
5655 saved_glyph_row = it->glyph_row;
5656 it->glyph_row = NULL;
5657
5658 #define BUFFER_POS_REACHED_P() \
5659 ((op & MOVE_TO_POS) != 0 \
5660 && BUFFERP (it->object) \
5661 && IT_CHARPOS (*it) >= to_charpos)
5662
5663 while (1)
5664 {
5665 int x, i, ascent = 0, descent = 0;
5666
5667 /* Stop when ZV reached.
5668 We used to stop here when TO_CHARPOS reached as well, but that is
5669 too soon if this glyph does not fit on this line. So we handle it
5670 explicitly below. */
5671 if (!get_next_display_element (it)
5672 || (it->truncate_lines_p
5673 && BUFFER_POS_REACHED_P ()))
5674 {
5675 result = MOVE_POS_MATCH_OR_ZV;
5676 break;
5677 }
5678
5679 /* The call to produce_glyphs will get the metrics of the
5680 display element IT is loaded with. We record in x the
5681 x-position before this display element in case it does not
5682 fit on the line. */
5683 x = it->current_x;
5684
5685 /* Remember the line height so far in case the next element doesn't
5686 fit on the line. */
5687 if (!it->truncate_lines_p)
5688 {
5689 ascent = it->max_ascent;
5690 descent = it->max_descent;
5691 }
5692
5693 PRODUCE_GLYPHS (it);
5694
5695 if (it->area != TEXT_AREA)
5696 {
5697 set_iterator_to_next (it, 1);
5698 continue;
5699 }
5700
5701 /* The number of glyphs we get back in IT->nglyphs will normally
5702 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5703 character on a terminal frame, or (iii) a line end. For the
5704 second case, IT->nglyphs - 1 padding glyphs will be present
5705 (on X frames, there is only one glyph produced for a
5706 composite character.
5707
5708 The behavior implemented below means, for continuation lines,
5709 that as many spaces of a TAB as fit on the current line are
5710 displayed there. For terminal frames, as many glyphs of a
5711 multi-glyph character are displayed in the current line, too.
5712 This is what the old redisplay code did, and we keep it that
5713 way. Under X, the whole shape of a complex character must
5714 fit on the line or it will be completely displayed in the
5715 next line.
5716
5717 Note that both for tabs and padding glyphs, all glyphs have
5718 the same width. */
5719 if (it->nglyphs)
5720 {
5721 /* More than one glyph or glyph doesn't fit on line. All
5722 glyphs have the same width. */
5723 int single_glyph_width = it->pixel_width / it->nglyphs;
5724 int new_x;
5725
5726 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5727 {
5728 new_x = x + single_glyph_width;
5729
5730 /* We want to leave anything reaching TO_X to the caller. */
5731 if ((op & MOVE_TO_X) && new_x > to_x)
5732 {
5733 if (BUFFER_POS_REACHED_P ())
5734 goto buffer_pos_reached;
5735 it->current_x = x;
5736 result = MOVE_X_REACHED;
5737 break;
5738 }
5739 else if (/* Lines are continued. */
5740 !it->truncate_lines_p
5741 && (/* And glyph doesn't fit on the line. */
5742 new_x > it->last_visible_x
5743 /* Or it fits exactly and we're on a window
5744 system frame. */
5745 || (new_x == it->last_visible_x
5746 && FRAME_WINDOW_P (it->f))))
5747 {
5748 if (/* IT->hpos == 0 means the very first glyph
5749 doesn't fit on the line, e.g. a wide image. */
5750 it->hpos == 0
5751 || (new_x == it->last_visible_x
5752 && FRAME_WINDOW_P (it->f)))
5753 {
5754 ++it->hpos;
5755 it->current_x = new_x;
5756 if (i == it->nglyphs - 1)
5757 {
5758 set_iterator_to_next (it, 1);
5759 #ifdef HAVE_WINDOW_SYSTEM
5760 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5761 {
5762 if (!get_next_display_element (it))
5763 {
5764 result = MOVE_POS_MATCH_OR_ZV;
5765 break;
5766 }
5767 if (BUFFER_POS_REACHED_P ())
5768 {
5769 if (ITERATOR_AT_END_OF_LINE_P (it))
5770 result = MOVE_POS_MATCH_OR_ZV;
5771 else
5772 result = MOVE_LINE_CONTINUED;
5773 break;
5774 }
5775 if (ITERATOR_AT_END_OF_LINE_P (it))
5776 {
5777 result = MOVE_NEWLINE_OR_CR;
5778 break;
5779 }
5780 if (it->method == next_element_from_display_vector)
5781 it->face_id = it->saved_face_id;
5782 }
5783 #endif /* HAVE_WINDOW_SYSTEM */
5784 }
5785 }
5786 else
5787 {
5788 it->current_x = x;
5789 it->max_ascent = ascent;
5790 it->max_descent = descent;
5791 }
5792
5793 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5794 IT_CHARPOS (*it)));
5795 result = MOVE_LINE_CONTINUED;
5796 break;
5797 }
5798 else if (BUFFER_POS_REACHED_P ())
5799 goto buffer_pos_reached;
5800 else if (new_x > it->first_visible_x)
5801 {
5802 /* Glyph is visible. Increment number of glyphs that
5803 would be displayed. */
5804 ++it->hpos;
5805 }
5806 else
5807 {
5808 /* Glyph is completely off the left margin of the display
5809 area. Nothing to do. */
5810 }
5811 }
5812
5813 if (result != MOVE_UNDEFINED)
5814 break;
5815 }
5816 else if (BUFFER_POS_REACHED_P ())
5817 {
5818 buffer_pos_reached:
5819 it->current_x = x;
5820 it->max_ascent = ascent;
5821 it->max_descent = descent;
5822 result = MOVE_POS_MATCH_OR_ZV;
5823 break;
5824 }
5825 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5826 {
5827 /* Stop when TO_X specified and reached. This check is
5828 necessary here because of lines consisting of a line end,
5829 only. The line end will not produce any glyphs and we
5830 would never get MOVE_X_REACHED. */
5831 xassert (it->nglyphs == 0);
5832 result = MOVE_X_REACHED;
5833 break;
5834 }
5835
5836 /* Is this a line end? If yes, we're done. */
5837 if (ITERATOR_AT_END_OF_LINE_P (it))
5838 {
5839 result = MOVE_NEWLINE_OR_CR;
5840 break;
5841 }
5842
5843 /* The current display element has been consumed. Advance
5844 to the next. */
5845 set_iterator_to_next (it, 1);
5846
5847 /* Stop if lines are truncated and IT's current x-position is
5848 past the right edge of the window now. */
5849 if (it->truncate_lines_p
5850 && it->current_x >= it->last_visible_x)
5851 {
5852 #ifdef HAVE_WINDOW_SYSTEM
5853 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5854 {
5855 if (!get_next_display_element (it)
5856 || BUFFER_POS_REACHED_P ())
5857 {
5858 result = MOVE_POS_MATCH_OR_ZV;
5859 break;
5860 }
5861 if (ITERATOR_AT_END_OF_LINE_P (it))
5862 {
5863 result = MOVE_NEWLINE_OR_CR;
5864 break;
5865 }
5866 }
5867 #endif /* HAVE_WINDOW_SYSTEM */
5868 result = MOVE_LINE_TRUNCATED;
5869 break;
5870 }
5871 }
5872
5873 #undef BUFFER_POS_REACHED_P
5874
5875 /* Restore the iterator settings altered at the beginning of this
5876 function. */
5877 it->glyph_row = saved_glyph_row;
5878 return result;
5879 }
5880
5881
5882 /* Move IT forward until it satisfies one or more of the criteria in
5883 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5884
5885 OP is a bit-mask that specifies where to stop, and in particular,
5886 which of those four position arguments makes a difference. See the
5887 description of enum move_operation_enum.
5888
5889 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5890 screen line, this function will set IT to the next position >
5891 TO_CHARPOS. */
5892
5893 void
5894 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5895 struct it *it;
5896 int to_charpos, to_x, to_y, to_vpos;
5897 int op;
5898 {
5899 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5900 int line_height;
5901 int reached = 0;
5902
5903 for (;;)
5904 {
5905 if (op & MOVE_TO_VPOS)
5906 {
5907 /* If no TO_CHARPOS and no TO_X specified, stop at the
5908 start of the line TO_VPOS. */
5909 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5910 {
5911 if (it->vpos == to_vpos)
5912 {
5913 reached = 1;
5914 break;
5915 }
5916 else
5917 skip = move_it_in_display_line_to (it, -1, -1, 0);
5918 }
5919 else
5920 {
5921 /* TO_VPOS >= 0 means stop at TO_X in the line at
5922 TO_VPOS, or at TO_POS, whichever comes first. */
5923 if (it->vpos == to_vpos)
5924 {
5925 reached = 2;
5926 break;
5927 }
5928
5929 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5930
5931 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5932 {
5933 reached = 3;
5934 break;
5935 }
5936 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5937 {
5938 /* We have reached TO_X but not in the line we want. */
5939 skip = move_it_in_display_line_to (it, to_charpos,
5940 -1, MOVE_TO_POS);
5941 if (skip == MOVE_POS_MATCH_OR_ZV)
5942 {
5943 reached = 4;
5944 break;
5945 }
5946 }
5947 }
5948 }
5949 else if (op & MOVE_TO_Y)
5950 {
5951 struct it it_backup;
5952
5953 /* TO_Y specified means stop at TO_X in the line containing
5954 TO_Y---or at TO_CHARPOS if this is reached first. The
5955 problem is that we can't really tell whether the line
5956 contains TO_Y before we have completely scanned it, and
5957 this may skip past TO_X. What we do is to first scan to
5958 TO_X.
5959
5960 If TO_X is not specified, use a TO_X of zero. The reason
5961 is to make the outcome of this function more predictable.
5962 If we didn't use TO_X == 0, we would stop at the end of
5963 the line which is probably not what a caller would expect
5964 to happen. */
5965 skip = move_it_in_display_line_to (it, to_charpos,
5966 ((op & MOVE_TO_X)
5967 ? to_x : 0),
5968 (MOVE_TO_X
5969 | (op & MOVE_TO_POS)));
5970
5971 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5972 if (skip == MOVE_POS_MATCH_OR_ZV)
5973 {
5974 reached = 5;
5975 break;
5976 }
5977
5978 /* If TO_X was reached, we would like to know whether TO_Y
5979 is in the line. This can only be said if we know the
5980 total line height which requires us to scan the rest of
5981 the line. */
5982 if (skip == MOVE_X_REACHED)
5983 {
5984 it_backup = *it;
5985 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5986 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5987 op & MOVE_TO_POS);
5988 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5989 }
5990
5991 /* Now, decide whether TO_Y is in this line. */
5992 line_height = it->max_ascent + it->max_descent;
5993 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5994
5995 if (to_y >= it->current_y
5996 && to_y < it->current_y + line_height)
5997 {
5998 if (skip == MOVE_X_REACHED)
5999 /* If TO_Y is in this line and TO_X was reached above,
6000 we scanned too far. We have to restore IT's settings
6001 to the ones before skipping. */
6002 *it = it_backup;
6003 reached = 6;
6004 }
6005 else if (skip == MOVE_X_REACHED)
6006 {
6007 skip = skip2;
6008 if (skip == MOVE_POS_MATCH_OR_ZV)
6009 reached = 7;
6010 }
6011
6012 if (reached)
6013 break;
6014 }
6015 else
6016 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6017
6018 switch (skip)
6019 {
6020 case MOVE_POS_MATCH_OR_ZV:
6021 reached = 8;
6022 goto out;
6023
6024 case MOVE_NEWLINE_OR_CR:
6025 set_iterator_to_next (it, 1);
6026 it->continuation_lines_width = 0;
6027 break;
6028
6029 case MOVE_LINE_TRUNCATED:
6030 it->continuation_lines_width = 0;
6031 reseat_at_next_visible_line_start (it, 0);
6032 if ((op & MOVE_TO_POS) != 0
6033 && IT_CHARPOS (*it) > to_charpos)
6034 {
6035 reached = 9;
6036 goto out;
6037 }
6038 break;
6039
6040 case MOVE_LINE_CONTINUED:
6041 it->continuation_lines_width += it->current_x;
6042 break;
6043
6044 default:
6045 abort ();
6046 }
6047
6048 /* Reset/increment for the next run. */
6049 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6050 it->current_x = it->hpos = 0;
6051 it->current_y += it->max_ascent + it->max_descent;
6052 ++it->vpos;
6053 last_height = it->max_ascent + it->max_descent;
6054 last_max_ascent = it->max_ascent;
6055 it->max_ascent = it->max_descent = 0;
6056 }
6057
6058 out:
6059
6060 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6061 }
6062
6063
6064 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6065
6066 If DY > 0, move IT backward at least that many pixels. DY = 0
6067 means move IT backward to the preceding line start or BEGV. This
6068 function may move over more than DY pixels if IT->current_y - DY
6069 ends up in the middle of a line; in this case IT->current_y will be
6070 set to the top of the line moved to. */
6071
6072 void
6073 move_it_vertically_backward (it, dy)
6074 struct it *it;
6075 int dy;
6076 {
6077 int nlines, h;
6078 struct it it2, it3;
6079 int start_pos;
6080
6081 move_further_back:
6082 xassert (dy >= 0);
6083
6084 start_pos = IT_CHARPOS (*it);
6085
6086 /* Estimate how many newlines we must move back. */
6087 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6088
6089 /* Set the iterator's position that many lines back. */
6090 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6091 back_to_previous_visible_line_start (it);
6092
6093 /* Reseat the iterator here. When moving backward, we don't want
6094 reseat to skip forward over invisible text, set up the iterator
6095 to deliver from overlay strings at the new position etc. So,
6096 use reseat_1 here. */
6097 reseat_1 (it, it->current.pos, 1);
6098
6099 /* We are now surely at a line start. */
6100 it->current_x = it->hpos = 0;
6101 it->continuation_lines_width = 0;
6102
6103 /* Move forward and see what y-distance we moved. First move to the
6104 start of the next line so that we get its height. We need this
6105 height to be able to tell whether we reached the specified
6106 y-distance. */
6107 it2 = *it;
6108 it2.max_ascent = it2.max_descent = 0;
6109 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6110 MOVE_TO_POS | MOVE_TO_VPOS);
6111 xassert (IT_CHARPOS (*it) >= BEGV);
6112 it3 = it2;
6113
6114 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6115 xassert (IT_CHARPOS (*it) >= BEGV);
6116 /* H is the actual vertical distance from the position in *IT
6117 and the starting position. */
6118 h = it2.current_y - it->current_y;
6119 /* NLINES is the distance in number of lines. */
6120 nlines = it2.vpos - it->vpos;
6121
6122 /* Correct IT's y and vpos position
6123 so that they are relative to the starting point. */
6124 it->vpos -= nlines;
6125 it->current_y -= h;
6126
6127 if (dy == 0)
6128 {
6129 /* DY == 0 means move to the start of the screen line. The
6130 value of nlines is > 0 if continuation lines were involved. */
6131 if (nlines > 0)
6132 move_it_by_lines (it, nlines, 1);
6133 xassert (IT_CHARPOS (*it) <= start_pos);
6134 }
6135 else
6136 {
6137 /* The y-position we try to reach, relative to *IT.
6138 Note that H has been subtracted in front of the if-statement. */
6139 int target_y = it->current_y + h - dy;
6140 int y0 = it3.current_y;
6141 int y1 = line_bottom_y (&it3);
6142 int line_height = y1 - y0;
6143
6144 /* If we did not reach target_y, try to move further backward if
6145 we can. If we moved too far backward, try to move forward. */
6146 if (target_y < it->current_y
6147 /* This is heuristic. In a window that's 3 lines high, with
6148 a line height of 13 pixels each, recentering with point
6149 on the bottom line will try to move -39/2 = 19 pixels
6150 backward. Try to avoid moving into the first line. */
6151 && it->current_y - target_y > line_height * 2 / 3
6152 && IT_CHARPOS (*it) > BEGV)
6153 {
6154 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6155 target_y - it->current_y));
6156 dy = it->current_y - target_y;
6157 goto move_further_back;
6158 }
6159 else if (target_y >= it->current_y + line_height
6160 && IT_CHARPOS (*it) < ZV)
6161 {
6162 /* Should move forward by at least one line, maybe more.
6163
6164 Note: Calling move_it_by_lines can be expensive on
6165 terminal frames, where compute_motion is used (via
6166 vmotion) to do the job, when there are very long lines
6167 and truncate-lines is nil. That's the reason for
6168 treating terminal frames specially here. */
6169
6170 if (!FRAME_WINDOW_P (it->f))
6171 move_it_vertically (it, target_y - (it->current_y + line_height));
6172 else
6173 {
6174 do
6175 {
6176 move_it_by_lines (it, 1, 1);
6177 }
6178 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6179 }
6180
6181 xassert (IT_CHARPOS (*it) >= BEGV);
6182 }
6183 }
6184 }
6185
6186
6187 /* Move IT by a specified amount of pixel lines DY. DY negative means
6188 move backwards. DY = 0 means move to start of screen line. At the
6189 end, IT will be on the start of a screen line. */
6190
6191 void
6192 move_it_vertically (it, dy)
6193 struct it *it;
6194 int dy;
6195 {
6196 if (dy <= 0)
6197 move_it_vertically_backward (it, -dy);
6198 else
6199 {
6200 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6201 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6202 MOVE_TO_POS | MOVE_TO_Y);
6203 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6204
6205 /* If buffer ends in ZV without a newline, move to the start of
6206 the line to satisfy the post-condition. */
6207 if (IT_CHARPOS (*it) == ZV
6208 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6209 move_it_by_lines (it, 0, 0);
6210 }
6211 }
6212
6213
6214 /* Move iterator IT past the end of the text line it is in. */
6215
6216 void
6217 move_it_past_eol (it)
6218 struct it *it;
6219 {
6220 enum move_it_result rc;
6221
6222 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6223 if (rc == MOVE_NEWLINE_OR_CR)
6224 set_iterator_to_next (it, 0);
6225 }
6226
6227
6228 #if 0 /* Currently not used. */
6229
6230 /* Return non-zero if some text between buffer positions START_CHARPOS
6231 and END_CHARPOS is invisible. IT->window is the window for text
6232 property lookup. */
6233
6234 static int
6235 invisible_text_between_p (it, start_charpos, end_charpos)
6236 struct it *it;
6237 int start_charpos, end_charpos;
6238 {
6239 Lisp_Object prop, limit;
6240 int invisible_found_p;
6241
6242 xassert (it != NULL && start_charpos <= end_charpos);
6243
6244 /* Is text at START invisible? */
6245 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6246 it->window);
6247 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6248 invisible_found_p = 1;
6249 else
6250 {
6251 limit = Fnext_single_char_property_change (make_number (start_charpos),
6252 Qinvisible, Qnil,
6253 make_number (end_charpos));
6254 invisible_found_p = XFASTINT (limit) < end_charpos;
6255 }
6256
6257 return invisible_found_p;
6258 }
6259
6260 #endif /* 0 */
6261
6262
6263 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6264 negative means move up. DVPOS == 0 means move to the start of the
6265 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6266 NEED_Y_P is zero, IT->current_y will be left unchanged.
6267
6268 Further optimization ideas: If we would know that IT->f doesn't use
6269 a face with proportional font, we could be faster for
6270 truncate-lines nil. */
6271
6272 void
6273 move_it_by_lines (it, dvpos, need_y_p)
6274 struct it *it;
6275 int dvpos, need_y_p;
6276 {
6277 struct position pos;
6278
6279 if (!FRAME_WINDOW_P (it->f))
6280 {
6281 struct text_pos textpos;
6282
6283 /* We can use vmotion on frames without proportional fonts. */
6284 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6285 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6286 reseat (it, textpos, 1);
6287 it->vpos += pos.vpos;
6288 it->current_y += pos.vpos;
6289 }
6290 else if (dvpos == 0)
6291 {
6292 /* DVPOS == 0 means move to the start of the screen line. */
6293 move_it_vertically_backward (it, 0);
6294 xassert (it->current_x == 0 && it->hpos == 0);
6295 /* Let next call to line_bottom_y calculate real line height */
6296 last_height = 0;
6297 }
6298 else if (dvpos > 0)
6299 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6300 else
6301 {
6302 struct it it2;
6303 int start_charpos, i;
6304
6305 /* Start at the beginning of the screen line containing IT's
6306 position. */
6307 move_it_vertically_backward (it, 0);
6308
6309 /* Go back -DVPOS visible lines and reseat the iterator there. */
6310 start_charpos = IT_CHARPOS (*it);
6311 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6312 back_to_previous_visible_line_start (it);
6313 reseat (it, it->current.pos, 1);
6314 it->current_x = it->hpos = 0;
6315
6316 /* Above call may have moved too far if continuation lines
6317 are involved. Scan forward and see if it did. */
6318 it2 = *it;
6319 it2.vpos = it2.current_y = 0;
6320 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6321 it->vpos -= it2.vpos;
6322 it->current_y -= it2.current_y;
6323 it->current_x = it->hpos = 0;
6324
6325 /* If we moved too far, move IT some lines forward. */
6326 if (it2.vpos > -dvpos)
6327 {
6328 int delta = it2.vpos + dvpos;
6329 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6330 }
6331 }
6332 }
6333
6334 /* Return 1 if IT points into the middle of a display vector. */
6335
6336 int
6337 in_display_vector_p (it)
6338 struct it *it;
6339 {
6340 return (it->method == next_element_from_display_vector
6341 && it->current.dpvec_index > 0
6342 && it->dpvec + it->current.dpvec_index != it->dpend);
6343 }
6344
6345 \f
6346 /***********************************************************************
6347 Messages
6348 ***********************************************************************/
6349
6350
6351 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6352 to *Messages*. */
6353
6354 void
6355 add_to_log (format, arg1, arg2)
6356 char *format;
6357 Lisp_Object arg1, arg2;
6358 {
6359 Lisp_Object args[3];
6360 Lisp_Object msg, fmt;
6361 char *buffer;
6362 int len;
6363 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6364 USE_SAFE_ALLOCA;
6365
6366 /* Do nothing if called asynchronously. Inserting text into
6367 a buffer may call after-change-functions and alike and
6368 that would means running Lisp asynchronously. */
6369 if (handling_signal)
6370 return;
6371
6372 fmt = msg = Qnil;
6373 GCPRO4 (fmt, msg, arg1, arg2);
6374
6375 args[0] = fmt = build_string (format);
6376 args[1] = arg1;
6377 args[2] = arg2;
6378 msg = Fformat (3, args);
6379
6380 len = SBYTES (msg) + 1;
6381 SAFE_ALLOCA (buffer, char *, len);
6382 bcopy (SDATA (msg), buffer, len);
6383
6384 message_dolog (buffer, len - 1, 1, 0);
6385 SAFE_FREE ();
6386
6387 UNGCPRO;
6388 }
6389
6390
6391 /* Output a newline in the *Messages* buffer if "needs" one. */
6392
6393 void
6394 message_log_maybe_newline ()
6395 {
6396 if (message_log_need_newline)
6397 message_dolog ("", 0, 1, 0);
6398 }
6399
6400
6401 /* Add a string M of length NBYTES to the message log, optionally
6402 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6403 nonzero, means interpret the contents of M as multibyte. This
6404 function calls low-level routines in order to bypass text property
6405 hooks, etc. which might not be safe to run. */
6406
6407 void
6408 message_dolog (m, nbytes, nlflag, multibyte)
6409 const char *m;
6410 int nbytes, nlflag, multibyte;
6411 {
6412 if (!NILP (Vmemory_full))
6413 return;
6414
6415 if (!NILP (Vmessage_log_max))
6416 {
6417 struct buffer *oldbuf;
6418 Lisp_Object oldpoint, oldbegv, oldzv;
6419 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6420 int point_at_end = 0;
6421 int zv_at_end = 0;
6422 Lisp_Object old_deactivate_mark, tem;
6423 struct gcpro gcpro1;
6424
6425 old_deactivate_mark = Vdeactivate_mark;
6426 oldbuf = current_buffer;
6427 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6428 current_buffer->undo_list = Qt;
6429
6430 oldpoint = message_dolog_marker1;
6431 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6432 oldbegv = message_dolog_marker2;
6433 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6434 oldzv = message_dolog_marker3;
6435 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6436 GCPRO1 (old_deactivate_mark);
6437
6438 if (PT == Z)
6439 point_at_end = 1;
6440 if (ZV == Z)
6441 zv_at_end = 1;
6442
6443 BEGV = BEG;
6444 BEGV_BYTE = BEG_BYTE;
6445 ZV = Z;
6446 ZV_BYTE = Z_BYTE;
6447 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6448
6449 /* Insert the string--maybe converting multibyte to single byte
6450 or vice versa, so that all the text fits the buffer. */
6451 if (multibyte
6452 && NILP (current_buffer->enable_multibyte_characters))
6453 {
6454 int i, c, char_bytes;
6455 unsigned char work[1];
6456
6457 /* Convert a multibyte string to single-byte
6458 for the *Message* buffer. */
6459 for (i = 0; i < nbytes; i += char_bytes)
6460 {
6461 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6462 work[0] = (SINGLE_BYTE_CHAR_P (c)
6463 ? c
6464 : multibyte_char_to_unibyte (c, Qnil));
6465 insert_1_both (work, 1, 1, 1, 0, 0);
6466 }
6467 }
6468 else if (! multibyte
6469 && ! NILP (current_buffer->enable_multibyte_characters))
6470 {
6471 int i, c, char_bytes;
6472 unsigned char *msg = (unsigned char *) m;
6473 unsigned char str[MAX_MULTIBYTE_LENGTH];
6474 /* Convert a single-byte string to multibyte
6475 for the *Message* buffer. */
6476 for (i = 0; i < nbytes; i++)
6477 {
6478 c = unibyte_char_to_multibyte (msg[i]);
6479 char_bytes = CHAR_STRING (c, str);
6480 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6481 }
6482 }
6483 else if (nbytes)
6484 insert_1 (m, nbytes, 1, 0, 0);
6485
6486 if (nlflag)
6487 {
6488 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6489 insert_1 ("\n", 1, 1, 0, 0);
6490
6491 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6492 this_bol = PT;
6493 this_bol_byte = PT_BYTE;
6494
6495 /* See if this line duplicates the previous one.
6496 If so, combine duplicates. */
6497 if (this_bol > BEG)
6498 {
6499 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6500 prev_bol = PT;
6501 prev_bol_byte = PT_BYTE;
6502
6503 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6504 this_bol, this_bol_byte);
6505 if (dup)
6506 {
6507 del_range_both (prev_bol, prev_bol_byte,
6508 this_bol, this_bol_byte, 0);
6509 if (dup > 1)
6510 {
6511 char dupstr[40];
6512 int duplen;
6513
6514 /* If you change this format, don't forget to also
6515 change message_log_check_duplicate. */
6516 sprintf (dupstr, " [%d times]", dup);
6517 duplen = strlen (dupstr);
6518 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6519 insert_1 (dupstr, duplen, 1, 0, 1);
6520 }
6521 }
6522 }
6523
6524 /* If we have more than the desired maximum number of lines
6525 in the *Messages* buffer now, delete the oldest ones.
6526 This is safe because we don't have undo in this buffer. */
6527
6528 if (NATNUMP (Vmessage_log_max))
6529 {
6530 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6531 -XFASTINT (Vmessage_log_max) - 1, 0);
6532 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6533 }
6534 }
6535 BEGV = XMARKER (oldbegv)->charpos;
6536 BEGV_BYTE = marker_byte_position (oldbegv);
6537
6538 if (zv_at_end)
6539 {
6540 ZV = Z;
6541 ZV_BYTE = Z_BYTE;
6542 }
6543 else
6544 {
6545 ZV = XMARKER (oldzv)->charpos;
6546 ZV_BYTE = marker_byte_position (oldzv);
6547 }
6548
6549 if (point_at_end)
6550 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6551 else
6552 /* We can't do Fgoto_char (oldpoint) because it will run some
6553 Lisp code. */
6554 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6555 XMARKER (oldpoint)->bytepos);
6556
6557 UNGCPRO;
6558 unchain_marker (XMARKER (oldpoint));
6559 unchain_marker (XMARKER (oldbegv));
6560 unchain_marker (XMARKER (oldzv));
6561
6562 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6563 set_buffer_internal (oldbuf);
6564 if (NILP (tem))
6565 windows_or_buffers_changed = old_windows_or_buffers_changed;
6566 message_log_need_newline = !nlflag;
6567 Vdeactivate_mark = old_deactivate_mark;
6568 }
6569 }
6570
6571
6572 /* We are at the end of the buffer after just having inserted a newline.
6573 (Note: We depend on the fact we won't be crossing the gap.)
6574 Check to see if the most recent message looks a lot like the previous one.
6575 Return 0 if different, 1 if the new one should just replace it, or a
6576 value N > 1 if we should also append " [N times]". */
6577
6578 static int
6579 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6580 int prev_bol, this_bol;
6581 int prev_bol_byte, this_bol_byte;
6582 {
6583 int i;
6584 int len = Z_BYTE - 1 - this_bol_byte;
6585 int seen_dots = 0;
6586 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6587 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6588
6589 for (i = 0; i < len; i++)
6590 {
6591 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6592 seen_dots = 1;
6593 if (p1[i] != p2[i])
6594 return seen_dots;
6595 }
6596 p1 += len;
6597 if (*p1 == '\n')
6598 return 2;
6599 if (*p1++ == ' ' && *p1++ == '[')
6600 {
6601 int n = 0;
6602 while (*p1 >= '0' && *p1 <= '9')
6603 n = n * 10 + *p1++ - '0';
6604 if (strncmp (p1, " times]\n", 8) == 0)
6605 return n+1;
6606 }
6607 return 0;
6608 }
6609
6610
6611 /* Display an echo area message M with a specified length of NBYTES
6612 bytes. The string may include null characters. If M is 0, clear
6613 out any existing message, and let the mini-buffer text show
6614 through.
6615
6616 The buffer M must continue to exist until after the echo area gets
6617 cleared or some other message gets displayed there. This means do
6618 not pass text that is stored in a Lisp string; do not pass text in
6619 a buffer that was alloca'd. */
6620
6621 void
6622 message2 (m, nbytes, multibyte)
6623 const char *m;
6624 int nbytes;
6625 int multibyte;
6626 {
6627 /* First flush out any partial line written with print. */
6628 message_log_maybe_newline ();
6629 if (m)
6630 message_dolog (m, nbytes, 1, multibyte);
6631 message2_nolog (m, nbytes, multibyte);
6632 }
6633
6634
6635 /* The non-logging counterpart of message2. */
6636
6637 void
6638 message2_nolog (m, nbytes, multibyte)
6639 const char *m;
6640 int nbytes, multibyte;
6641 {
6642 struct frame *sf = SELECTED_FRAME ();
6643 message_enable_multibyte = multibyte;
6644
6645 if (noninteractive)
6646 {
6647 if (noninteractive_need_newline)
6648 putc ('\n', stderr);
6649 noninteractive_need_newline = 0;
6650 if (m)
6651 fwrite (m, nbytes, 1, stderr);
6652 if (cursor_in_echo_area == 0)
6653 fprintf (stderr, "\n");
6654 fflush (stderr);
6655 }
6656 /* A null message buffer means that the frame hasn't really been
6657 initialized yet. Error messages get reported properly by
6658 cmd_error, so this must be just an informative message; toss it. */
6659 else if (INTERACTIVE
6660 && sf->glyphs_initialized_p
6661 && FRAME_MESSAGE_BUF (sf))
6662 {
6663 Lisp_Object mini_window;
6664 struct frame *f;
6665
6666 /* Get the frame containing the mini-buffer
6667 that the selected frame is using. */
6668 mini_window = FRAME_MINIBUF_WINDOW (sf);
6669 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6670
6671 FRAME_SAMPLE_VISIBILITY (f);
6672 if (FRAME_VISIBLE_P (sf)
6673 && ! FRAME_VISIBLE_P (f))
6674 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6675
6676 if (m)
6677 {
6678 set_message (m, Qnil, nbytes, multibyte);
6679 if (minibuffer_auto_raise)
6680 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6681 }
6682 else
6683 clear_message (1, 1);
6684
6685 do_pending_window_change (0);
6686 echo_area_display (1);
6687 do_pending_window_change (0);
6688 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6689 (*frame_up_to_date_hook) (f);
6690 }
6691 }
6692
6693
6694 /* Display an echo area message M with a specified length of NBYTES
6695 bytes. The string may include null characters. If M is not a
6696 string, clear out any existing message, and let the mini-buffer
6697 text show through. */
6698
6699 void
6700 message3 (m, nbytes, multibyte)
6701 Lisp_Object m;
6702 int nbytes;
6703 int multibyte;
6704 {
6705 struct gcpro gcpro1;
6706
6707 GCPRO1 (m);
6708 clear_message (1,1);
6709
6710 /* First flush out any partial line written with print. */
6711 message_log_maybe_newline ();
6712 if (STRINGP (m))
6713 message_dolog (SDATA (m), nbytes, 1, multibyte);
6714 message3_nolog (m, nbytes, multibyte);
6715
6716 UNGCPRO;
6717 }
6718
6719
6720 /* The non-logging version of message3. */
6721
6722 void
6723 message3_nolog (m, nbytes, multibyte)
6724 Lisp_Object m;
6725 int nbytes, multibyte;
6726 {
6727 struct frame *sf = SELECTED_FRAME ();
6728 message_enable_multibyte = multibyte;
6729
6730 if (noninteractive)
6731 {
6732 if (noninteractive_need_newline)
6733 putc ('\n', stderr);
6734 noninteractive_need_newline = 0;
6735 if (STRINGP (m))
6736 fwrite (SDATA (m), nbytes, 1, stderr);
6737 if (cursor_in_echo_area == 0)
6738 fprintf (stderr, "\n");
6739 fflush (stderr);
6740 }
6741 /* A null message buffer means that the frame hasn't really been
6742 initialized yet. Error messages get reported properly by
6743 cmd_error, so this must be just an informative message; toss it. */
6744 else if (INTERACTIVE
6745 && sf->glyphs_initialized_p
6746 && FRAME_MESSAGE_BUF (sf))
6747 {
6748 Lisp_Object mini_window;
6749 Lisp_Object frame;
6750 struct frame *f;
6751
6752 /* Get the frame containing the mini-buffer
6753 that the selected frame is using. */
6754 mini_window = FRAME_MINIBUF_WINDOW (sf);
6755 frame = XWINDOW (mini_window)->frame;
6756 f = XFRAME (frame);
6757
6758 FRAME_SAMPLE_VISIBILITY (f);
6759 if (FRAME_VISIBLE_P (sf)
6760 && !FRAME_VISIBLE_P (f))
6761 Fmake_frame_visible (frame);
6762
6763 if (STRINGP (m) && SCHARS (m) > 0)
6764 {
6765 set_message (NULL, m, nbytes, multibyte);
6766 if (minibuffer_auto_raise)
6767 Fraise_frame (frame);
6768 }
6769 else
6770 clear_message (1, 1);
6771
6772 do_pending_window_change (0);
6773 echo_area_display (1);
6774 do_pending_window_change (0);
6775 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6776 (*frame_up_to_date_hook) (f);
6777 }
6778 }
6779
6780
6781 /* Display a null-terminated echo area message M. If M is 0, clear
6782 out any existing message, and let the mini-buffer text show through.
6783
6784 The buffer M must continue to exist until after the echo area gets
6785 cleared or some other message gets displayed there. Do not pass
6786 text that is stored in a Lisp string. Do not pass text in a buffer
6787 that was alloca'd. */
6788
6789 void
6790 message1 (m)
6791 char *m;
6792 {
6793 message2 (m, (m ? strlen (m) : 0), 0);
6794 }
6795
6796
6797 /* The non-logging counterpart of message1. */
6798
6799 void
6800 message1_nolog (m)
6801 char *m;
6802 {
6803 message2_nolog (m, (m ? strlen (m) : 0), 0);
6804 }
6805
6806 /* Display a message M which contains a single %s
6807 which gets replaced with STRING. */
6808
6809 void
6810 message_with_string (m, string, log)
6811 char *m;
6812 Lisp_Object string;
6813 int log;
6814 {
6815 CHECK_STRING (string);
6816
6817 if (noninteractive)
6818 {
6819 if (m)
6820 {
6821 if (noninteractive_need_newline)
6822 putc ('\n', stderr);
6823 noninteractive_need_newline = 0;
6824 fprintf (stderr, m, SDATA (string));
6825 if (cursor_in_echo_area == 0)
6826 fprintf (stderr, "\n");
6827 fflush (stderr);
6828 }
6829 }
6830 else if (INTERACTIVE)
6831 {
6832 /* The frame whose minibuffer we're going to display the message on.
6833 It may be larger than the selected frame, so we need
6834 to use its buffer, not the selected frame's buffer. */
6835 Lisp_Object mini_window;
6836 struct frame *f, *sf = SELECTED_FRAME ();
6837
6838 /* Get the frame containing the minibuffer
6839 that the selected frame is using. */
6840 mini_window = FRAME_MINIBUF_WINDOW (sf);
6841 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6842
6843 /* A null message buffer means that the frame hasn't really been
6844 initialized yet. Error messages get reported properly by
6845 cmd_error, so this must be just an informative message; toss it. */
6846 if (FRAME_MESSAGE_BUF (f))
6847 {
6848 Lisp_Object args[2], message;
6849 struct gcpro gcpro1, gcpro2;
6850
6851 args[0] = build_string (m);
6852 args[1] = message = string;
6853 GCPRO2 (args[0], message);
6854 gcpro1.nvars = 2;
6855
6856 message = Fformat (2, args);
6857
6858 if (log)
6859 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6860 else
6861 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6862
6863 UNGCPRO;
6864
6865 /* Print should start at the beginning of the message
6866 buffer next time. */
6867 message_buf_print = 0;
6868 }
6869 }
6870 }
6871
6872
6873 /* Dump an informative message to the minibuf. If M is 0, clear out
6874 any existing message, and let the mini-buffer text show through. */
6875
6876 /* VARARGS 1 */
6877 void
6878 message (m, a1, a2, a3)
6879 char *m;
6880 EMACS_INT a1, a2, a3;
6881 {
6882 if (noninteractive)
6883 {
6884 if (m)
6885 {
6886 if (noninteractive_need_newline)
6887 putc ('\n', stderr);
6888 noninteractive_need_newline = 0;
6889 fprintf (stderr, m, a1, a2, a3);
6890 if (cursor_in_echo_area == 0)
6891 fprintf (stderr, "\n");
6892 fflush (stderr);
6893 }
6894 }
6895 else if (INTERACTIVE)
6896 {
6897 /* The frame whose mini-buffer we're going to display the message
6898 on. It may be larger than the selected frame, so we need to
6899 use its buffer, not the selected frame's buffer. */
6900 Lisp_Object mini_window;
6901 struct frame *f, *sf = SELECTED_FRAME ();
6902
6903 /* Get the frame containing the mini-buffer
6904 that the selected frame is using. */
6905 mini_window = FRAME_MINIBUF_WINDOW (sf);
6906 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6907
6908 /* A null message buffer means that the frame hasn't really been
6909 initialized yet. Error messages get reported properly by
6910 cmd_error, so this must be just an informative message; toss
6911 it. */
6912 if (FRAME_MESSAGE_BUF (f))
6913 {
6914 if (m)
6915 {
6916 int len;
6917 #ifdef NO_ARG_ARRAY
6918 char *a[3];
6919 a[0] = (char *) a1;
6920 a[1] = (char *) a2;
6921 a[2] = (char *) a3;
6922
6923 len = doprnt (FRAME_MESSAGE_BUF (f),
6924 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6925 #else
6926 len = doprnt (FRAME_MESSAGE_BUF (f),
6927 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6928 (char **) &a1);
6929 #endif /* NO_ARG_ARRAY */
6930
6931 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6932 }
6933 else
6934 message1 (0);
6935
6936 /* Print should start at the beginning of the message
6937 buffer next time. */
6938 message_buf_print = 0;
6939 }
6940 }
6941 }
6942
6943
6944 /* The non-logging version of message. */
6945
6946 void
6947 message_nolog (m, a1, a2, a3)
6948 char *m;
6949 EMACS_INT a1, a2, a3;
6950 {
6951 Lisp_Object old_log_max;
6952 old_log_max = Vmessage_log_max;
6953 Vmessage_log_max = Qnil;
6954 message (m, a1, a2, a3);
6955 Vmessage_log_max = old_log_max;
6956 }
6957
6958
6959 /* Display the current message in the current mini-buffer. This is
6960 only called from error handlers in process.c, and is not time
6961 critical. */
6962
6963 void
6964 update_echo_area ()
6965 {
6966 if (!NILP (echo_area_buffer[0]))
6967 {
6968 Lisp_Object string;
6969 string = Fcurrent_message ();
6970 message3 (string, SBYTES (string),
6971 !NILP (current_buffer->enable_multibyte_characters));
6972 }
6973 }
6974
6975
6976 /* Make sure echo area buffers in `echo_buffers' are live.
6977 If they aren't, make new ones. */
6978
6979 static void
6980 ensure_echo_area_buffers ()
6981 {
6982 int i;
6983
6984 for (i = 0; i < 2; ++i)
6985 if (!BUFFERP (echo_buffer[i])
6986 || NILP (XBUFFER (echo_buffer[i])->name))
6987 {
6988 char name[30];
6989 Lisp_Object old_buffer;
6990 int j;
6991
6992 old_buffer = echo_buffer[i];
6993 sprintf (name, " *Echo Area %d*", i);
6994 echo_buffer[i] = Fget_buffer_create (build_string (name));
6995 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6996
6997 for (j = 0; j < 2; ++j)
6998 if (EQ (old_buffer, echo_area_buffer[j]))
6999 echo_area_buffer[j] = echo_buffer[i];
7000 }
7001 }
7002
7003
7004 /* Call FN with args A1..A4 with either the current or last displayed
7005 echo_area_buffer as current buffer.
7006
7007 WHICH zero means use the current message buffer
7008 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7009 from echo_buffer[] and clear it.
7010
7011 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7012 suitable buffer from echo_buffer[] and clear it.
7013
7014 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7015 that the current message becomes the last displayed one, make
7016 choose a suitable buffer for echo_area_buffer[0], and clear it.
7017
7018 Value is what FN returns. */
7019
7020 static int
7021 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7022 struct window *w;
7023 int which;
7024 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7025 EMACS_INT a1;
7026 Lisp_Object a2;
7027 EMACS_INT a3, a4;
7028 {
7029 Lisp_Object buffer;
7030 int this_one, the_other, clear_buffer_p, rc;
7031 int count = SPECPDL_INDEX ();
7032
7033 /* If buffers aren't live, make new ones. */
7034 ensure_echo_area_buffers ();
7035
7036 clear_buffer_p = 0;
7037
7038 if (which == 0)
7039 this_one = 0, the_other = 1;
7040 else if (which > 0)
7041 this_one = 1, the_other = 0;
7042 else
7043 {
7044 this_one = 0, the_other = 1;
7045 clear_buffer_p = 1;
7046
7047 /* We need a fresh one in case the current echo buffer equals
7048 the one containing the last displayed echo area message. */
7049 if (!NILP (echo_area_buffer[this_one])
7050 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7051 echo_area_buffer[this_one] = Qnil;
7052 }
7053
7054 /* Choose a suitable buffer from echo_buffer[] is we don't
7055 have one. */
7056 if (NILP (echo_area_buffer[this_one]))
7057 {
7058 echo_area_buffer[this_one]
7059 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7060 ? echo_buffer[the_other]
7061 : echo_buffer[this_one]);
7062 clear_buffer_p = 1;
7063 }
7064
7065 buffer = echo_area_buffer[this_one];
7066
7067 /* Don't get confused by reusing the buffer used for echoing
7068 for a different purpose. */
7069 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7070 cancel_echoing ();
7071
7072 record_unwind_protect (unwind_with_echo_area_buffer,
7073 with_echo_area_buffer_unwind_data (w));
7074
7075 /* Make the echo area buffer current. Note that for display
7076 purposes, it is not necessary that the displayed window's buffer
7077 == current_buffer, except for text property lookup. So, let's
7078 only set that buffer temporarily here without doing a full
7079 Fset_window_buffer. We must also change w->pointm, though,
7080 because otherwise an assertions in unshow_buffer fails, and Emacs
7081 aborts. */
7082 set_buffer_internal_1 (XBUFFER (buffer));
7083 if (w)
7084 {
7085 w->buffer = buffer;
7086 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7087 }
7088
7089 current_buffer->undo_list = Qt;
7090 current_buffer->read_only = Qnil;
7091 specbind (Qinhibit_read_only, Qt);
7092 specbind (Qinhibit_modification_hooks, Qt);
7093
7094 if (clear_buffer_p && Z > BEG)
7095 del_range (BEG, Z);
7096
7097 xassert (BEGV >= BEG);
7098 xassert (ZV <= Z && ZV >= BEGV);
7099
7100 rc = fn (a1, a2, a3, a4);
7101
7102 xassert (BEGV >= BEG);
7103 xassert (ZV <= Z && ZV >= BEGV);
7104
7105 unbind_to (count, Qnil);
7106 return rc;
7107 }
7108
7109
7110 /* Save state that should be preserved around the call to the function
7111 FN called in with_echo_area_buffer. */
7112
7113 static Lisp_Object
7114 with_echo_area_buffer_unwind_data (w)
7115 struct window *w;
7116 {
7117 int i = 0;
7118 Lisp_Object vector;
7119
7120 /* Reduce consing by keeping one vector in
7121 Vwith_echo_area_save_vector. */
7122 vector = Vwith_echo_area_save_vector;
7123 Vwith_echo_area_save_vector = Qnil;
7124
7125 if (NILP (vector))
7126 vector = Fmake_vector (make_number (7), Qnil);
7127
7128 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7129 AREF (vector, i) = Vdeactivate_mark, ++i;
7130 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7131
7132 if (w)
7133 {
7134 XSETWINDOW (AREF (vector, i), w); ++i;
7135 AREF (vector, i) = w->buffer; ++i;
7136 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7137 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7138 }
7139 else
7140 {
7141 int end = i + 4;
7142 for (; i < end; ++i)
7143 AREF (vector, i) = Qnil;
7144 }
7145
7146 xassert (i == ASIZE (vector));
7147 return vector;
7148 }
7149
7150
7151 /* Restore global state from VECTOR which was created by
7152 with_echo_area_buffer_unwind_data. */
7153
7154 static Lisp_Object
7155 unwind_with_echo_area_buffer (vector)
7156 Lisp_Object vector;
7157 {
7158 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7159 Vdeactivate_mark = AREF (vector, 1);
7160 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7161
7162 if (WINDOWP (AREF (vector, 3)))
7163 {
7164 struct window *w;
7165 Lisp_Object buffer, charpos, bytepos;
7166
7167 w = XWINDOW (AREF (vector, 3));
7168 buffer = AREF (vector, 4);
7169 charpos = AREF (vector, 5);
7170 bytepos = AREF (vector, 6);
7171
7172 w->buffer = buffer;
7173 set_marker_both (w->pointm, buffer,
7174 XFASTINT (charpos), XFASTINT (bytepos));
7175 }
7176
7177 Vwith_echo_area_save_vector = vector;
7178 return Qnil;
7179 }
7180
7181
7182 /* Set up the echo area for use by print functions. MULTIBYTE_P
7183 non-zero means we will print multibyte. */
7184
7185 void
7186 setup_echo_area_for_printing (multibyte_p)
7187 int multibyte_p;
7188 {
7189 /* If we can't find an echo area any more, exit. */
7190 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7191 Fkill_emacs (Qnil);
7192
7193 ensure_echo_area_buffers ();
7194
7195 if (!message_buf_print)
7196 {
7197 /* A message has been output since the last time we printed.
7198 Choose a fresh echo area buffer. */
7199 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7200 echo_area_buffer[0] = echo_buffer[1];
7201 else
7202 echo_area_buffer[0] = echo_buffer[0];
7203
7204 /* Switch to that buffer and clear it. */
7205 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7206 current_buffer->truncate_lines = Qnil;
7207
7208 if (Z > BEG)
7209 {
7210 int count = SPECPDL_INDEX ();
7211 specbind (Qinhibit_read_only, Qt);
7212 /* Note that undo recording is always disabled. */
7213 del_range (BEG, Z);
7214 unbind_to (count, Qnil);
7215 }
7216 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7217
7218 /* Set up the buffer for the multibyteness we need. */
7219 if (multibyte_p
7220 != !NILP (current_buffer->enable_multibyte_characters))
7221 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7222
7223 /* Raise the frame containing the echo area. */
7224 if (minibuffer_auto_raise)
7225 {
7226 struct frame *sf = SELECTED_FRAME ();
7227 Lisp_Object mini_window;
7228 mini_window = FRAME_MINIBUF_WINDOW (sf);
7229 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7230 }
7231
7232 message_log_maybe_newline ();
7233 message_buf_print = 1;
7234 }
7235 else
7236 {
7237 if (NILP (echo_area_buffer[0]))
7238 {
7239 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7240 echo_area_buffer[0] = echo_buffer[1];
7241 else
7242 echo_area_buffer[0] = echo_buffer[0];
7243 }
7244
7245 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7246 {
7247 /* Someone switched buffers between print requests. */
7248 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7249 current_buffer->truncate_lines = Qnil;
7250 }
7251 }
7252 }
7253
7254
7255 /* Display an echo area message in window W. Value is non-zero if W's
7256 height is changed. If display_last_displayed_message_p is
7257 non-zero, display the message that was last displayed, otherwise
7258 display the current message. */
7259
7260 static int
7261 display_echo_area (w)
7262 struct window *w;
7263 {
7264 int i, no_message_p, window_height_changed_p, count;
7265
7266 /* Temporarily disable garbage collections while displaying the echo
7267 area. This is done because a GC can print a message itself.
7268 That message would modify the echo area buffer's contents while a
7269 redisplay of the buffer is going on, and seriously confuse
7270 redisplay. */
7271 count = inhibit_garbage_collection ();
7272
7273 /* If there is no message, we must call display_echo_area_1
7274 nevertheless because it resizes the window. But we will have to
7275 reset the echo_area_buffer in question to nil at the end because
7276 with_echo_area_buffer will sets it to an empty buffer. */
7277 i = display_last_displayed_message_p ? 1 : 0;
7278 no_message_p = NILP (echo_area_buffer[i]);
7279
7280 window_height_changed_p
7281 = with_echo_area_buffer (w, display_last_displayed_message_p,
7282 display_echo_area_1,
7283 (EMACS_INT) w, Qnil, 0, 0);
7284
7285 if (no_message_p)
7286 echo_area_buffer[i] = Qnil;
7287
7288 unbind_to (count, Qnil);
7289 return window_height_changed_p;
7290 }
7291
7292
7293 /* Helper for display_echo_area. Display the current buffer which
7294 contains the current echo area message in window W, a mini-window,
7295 a pointer to which is passed in A1. A2..A4 are currently not used.
7296 Change the height of W so that all of the message is displayed.
7297 Value is non-zero if height of W was changed. */
7298
7299 static int
7300 display_echo_area_1 (a1, a2, a3, a4)
7301 EMACS_INT a1;
7302 Lisp_Object a2;
7303 EMACS_INT a3, a4;
7304 {
7305 struct window *w = (struct window *) a1;
7306 Lisp_Object window;
7307 struct text_pos start;
7308 int window_height_changed_p = 0;
7309
7310 /* Do this before displaying, so that we have a large enough glyph
7311 matrix for the display. */
7312 window_height_changed_p = resize_mini_window (w, 0);
7313
7314 /* Display. */
7315 clear_glyph_matrix (w->desired_matrix);
7316 XSETWINDOW (window, w);
7317 SET_TEXT_POS (start, BEG, BEG_BYTE);
7318 try_window (window, start);
7319
7320 return window_height_changed_p;
7321 }
7322
7323
7324 /* Resize the echo area window to exactly the size needed for the
7325 currently displayed message, if there is one. If a mini-buffer
7326 is active, don't shrink it. */
7327
7328 void
7329 resize_echo_area_exactly ()
7330 {
7331 if (BUFFERP (echo_area_buffer[0])
7332 && WINDOWP (echo_area_window))
7333 {
7334 struct window *w = XWINDOW (echo_area_window);
7335 int resized_p;
7336 Lisp_Object resize_exactly;
7337
7338 if (minibuf_level == 0)
7339 resize_exactly = Qt;
7340 else
7341 resize_exactly = Qnil;
7342
7343 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7344 (EMACS_INT) w, resize_exactly, 0, 0);
7345 if (resized_p)
7346 {
7347 ++windows_or_buffers_changed;
7348 ++update_mode_lines;
7349 redisplay_internal (0);
7350 }
7351 }
7352 }
7353
7354
7355 /* Callback function for with_echo_area_buffer, when used from
7356 resize_echo_area_exactly. A1 contains a pointer to the window to
7357 resize, EXACTLY non-nil means resize the mini-window exactly to the
7358 size of the text displayed. A3 and A4 are not used. Value is what
7359 resize_mini_window returns. */
7360
7361 static int
7362 resize_mini_window_1 (a1, exactly, a3, a4)
7363 EMACS_INT a1;
7364 Lisp_Object exactly;
7365 EMACS_INT a3, a4;
7366 {
7367 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7368 }
7369
7370
7371 /* Resize mini-window W to fit the size of its contents. EXACT:P
7372 means size the window exactly to the size needed. Otherwise, it's
7373 only enlarged until W's buffer is empty. Value is non-zero if
7374 the window height has been changed. */
7375
7376 int
7377 resize_mini_window (w, exact_p)
7378 struct window *w;
7379 int exact_p;
7380 {
7381 struct frame *f = XFRAME (w->frame);
7382 int window_height_changed_p = 0;
7383
7384 xassert (MINI_WINDOW_P (w));
7385
7386 /* Don't resize windows while redisplaying a window; it would
7387 confuse redisplay functions when the size of the window they are
7388 displaying changes from under them. Such a resizing can happen,
7389 for instance, when which-func prints a long message while
7390 we are running fontification-functions. We're running these
7391 functions with safe_call which binds inhibit-redisplay to t. */
7392 if (!NILP (Vinhibit_redisplay))
7393 return 0;
7394
7395 /* Nil means don't try to resize. */
7396 if (NILP (Vresize_mini_windows)
7397 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7398 return 0;
7399
7400 if (!FRAME_MINIBUF_ONLY_P (f))
7401 {
7402 struct it it;
7403 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7404 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7405 int height, max_height;
7406 int unit = FRAME_LINE_HEIGHT (f);
7407 struct text_pos start;
7408 struct buffer *old_current_buffer = NULL;
7409
7410 if (current_buffer != XBUFFER (w->buffer))
7411 {
7412 old_current_buffer = current_buffer;
7413 set_buffer_internal (XBUFFER (w->buffer));
7414 }
7415
7416 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7417
7418 /* Compute the max. number of lines specified by the user. */
7419 if (FLOATP (Vmax_mini_window_height))
7420 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7421 else if (INTEGERP (Vmax_mini_window_height))
7422 max_height = XINT (Vmax_mini_window_height);
7423 else
7424 max_height = total_height / 4;
7425
7426 /* Correct that max. height if it's bogus. */
7427 max_height = max (1, max_height);
7428 max_height = min (total_height, max_height);
7429
7430 /* Find out the height of the text in the window. */
7431 if (it.truncate_lines_p)
7432 height = 1;
7433 else
7434 {
7435 last_height = 0;
7436 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7437 if (it.max_ascent == 0 && it.max_descent == 0)
7438 height = it.current_y + last_height;
7439 else
7440 height = it.current_y + it.max_ascent + it.max_descent;
7441 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7442 height = (height + unit - 1) / unit;
7443 }
7444
7445 /* Compute a suitable window start. */
7446 if (height > max_height)
7447 {
7448 height = max_height;
7449 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7450 move_it_vertically_backward (&it, (height - 1) * unit);
7451 start = it.current.pos;
7452 }
7453 else
7454 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7455 SET_MARKER_FROM_TEXT_POS (w->start, start);
7456
7457 if (EQ (Vresize_mini_windows, Qgrow_only))
7458 {
7459 /* Let it grow only, until we display an empty message, in which
7460 case the window shrinks again. */
7461 if (height > WINDOW_TOTAL_LINES (w))
7462 {
7463 int old_height = WINDOW_TOTAL_LINES (w);
7464 freeze_window_starts (f, 1);
7465 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7466 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7467 }
7468 else if (height < WINDOW_TOTAL_LINES (w)
7469 && (exact_p || BEGV == ZV))
7470 {
7471 int old_height = WINDOW_TOTAL_LINES (w);
7472 freeze_window_starts (f, 0);
7473 shrink_mini_window (w);
7474 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7475 }
7476 }
7477 else
7478 {
7479 /* Always resize to exact size needed. */
7480 if (height > WINDOW_TOTAL_LINES (w))
7481 {
7482 int old_height = WINDOW_TOTAL_LINES (w);
7483 freeze_window_starts (f, 1);
7484 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7485 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7486 }
7487 else if (height < WINDOW_TOTAL_LINES (w))
7488 {
7489 int old_height = WINDOW_TOTAL_LINES (w);
7490 freeze_window_starts (f, 0);
7491 shrink_mini_window (w);
7492
7493 if (height)
7494 {
7495 freeze_window_starts (f, 1);
7496 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7497 }
7498
7499 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7500 }
7501 }
7502
7503 if (old_current_buffer)
7504 set_buffer_internal (old_current_buffer);
7505 }
7506
7507 return window_height_changed_p;
7508 }
7509
7510
7511 /* Value is the current message, a string, or nil if there is no
7512 current message. */
7513
7514 Lisp_Object
7515 current_message ()
7516 {
7517 Lisp_Object msg;
7518
7519 if (NILP (echo_area_buffer[0]))
7520 msg = Qnil;
7521 else
7522 {
7523 with_echo_area_buffer (0, 0, current_message_1,
7524 (EMACS_INT) &msg, Qnil, 0, 0);
7525 if (NILP (msg))
7526 echo_area_buffer[0] = Qnil;
7527 }
7528
7529 return msg;
7530 }
7531
7532
7533 static int
7534 current_message_1 (a1, a2, a3, a4)
7535 EMACS_INT a1;
7536 Lisp_Object a2;
7537 EMACS_INT a3, a4;
7538 {
7539 Lisp_Object *msg = (Lisp_Object *) a1;
7540
7541 if (Z > BEG)
7542 *msg = make_buffer_string (BEG, Z, 1);
7543 else
7544 *msg = Qnil;
7545 return 0;
7546 }
7547
7548
7549 /* Push the current message on Vmessage_stack for later restauration
7550 by restore_message. Value is non-zero if the current message isn't
7551 empty. This is a relatively infrequent operation, so it's not
7552 worth optimizing. */
7553
7554 int
7555 push_message ()
7556 {
7557 Lisp_Object msg;
7558 msg = current_message ();
7559 Vmessage_stack = Fcons (msg, Vmessage_stack);
7560 return STRINGP (msg);
7561 }
7562
7563
7564 /* Restore message display from the top of Vmessage_stack. */
7565
7566 void
7567 restore_message ()
7568 {
7569 Lisp_Object msg;
7570
7571 xassert (CONSP (Vmessage_stack));
7572 msg = XCAR (Vmessage_stack);
7573 if (STRINGP (msg))
7574 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7575 else
7576 message3_nolog (msg, 0, 0);
7577 }
7578
7579
7580 /* Handler for record_unwind_protect calling pop_message. */
7581
7582 Lisp_Object
7583 pop_message_unwind (dummy)
7584 Lisp_Object dummy;
7585 {
7586 pop_message ();
7587 return Qnil;
7588 }
7589
7590 /* Pop the top-most entry off Vmessage_stack. */
7591
7592 void
7593 pop_message ()
7594 {
7595 xassert (CONSP (Vmessage_stack));
7596 Vmessage_stack = XCDR (Vmessage_stack);
7597 }
7598
7599
7600 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7601 exits. If the stack is not empty, we have a missing pop_message
7602 somewhere. */
7603
7604 void
7605 check_message_stack ()
7606 {
7607 if (!NILP (Vmessage_stack))
7608 abort ();
7609 }
7610
7611
7612 /* Truncate to NCHARS what will be displayed in the echo area the next
7613 time we display it---but don't redisplay it now. */
7614
7615 void
7616 truncate_echo_area (nchars)
7617 int nchars;
7618 {
7619 if (nchars == 0)
7620 echo_area_buffer[0] = Qnil;
7621 /* A null message buffer means that the frame hasn't really been
7622 initialized yet. Error messages get reported properly by
7623 cmd_error, so this must be just an informative message; toss it. */
7624 else if (!noninteractive
7625 && INTERACTIVE
7626 && !NILP (echo_area_buffer[0]))
7627 {
7628 struct frame *sf = SELECTED_FRAME ();
7629 if (FRAME_MESSAGE_BUF (sf))
7630 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7631 }
7632 }
7633
7634
7635 /* Helper function for truncate_echo_area. Truncate the current
7636 message to at most NCHARS characters. */
7637
7638 static int
7639 truncate_message_1 (nchars, a2, a3, a4)
7640 EMACS_INT nchars;
7641 Lisp_Object a2;
7642 EMACS_INT a3, a4;
7643 {
7644 if (BEG + nchars < Z)
7645 del_range (BEG + nchars, Z);
7646 if (Z == BEG)
7647 echo_area_buffer[0] = Qnil;
7648 return 0;
7649 }
7650
7651
7652 /* Set the current message to a substring of S or STRING.
7653
7654 If STRING is a Lisp string, set the message to the first NBYTES
7655 bytes from STRING. NBYTES zero means use the whole string. If
7656 STRING is multibyte, the message will be displayed multibyte.
7657
7658 If S is not null, set the message to the first LEN bytes of S. LEN
7659 zero means use the whole string. MULTIBYTE_P non-zero means S is
7660 multibyte. Display the message multibyte in that case. */
7661
7662 void
7663 set_message (s, string, nbytes, multibyte_p)
7664 const char *s;
7665 Lisp_Object string;
7666 int nbytes, multibyte_p;
7667 {
7668 message_enable_multibyte
7669 = ((s && multibyte_p)
7670 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7671
7672 with_echo_area_buffer (0, -1, set_message_1,
7673 (EMACS_INT) s, string, nbytes, multibyte_p);
7674 message_buf_print = 0;
7675 help_echo_showing_p = 0;
7676 }
7677
7678
7679 /* Helper function for set_message. Arguments have the same meaning
7680 as there, with A1 corresponding to S and A2 corresponding to STRING
7681 This function is called with the echo area buffer being
7682 current. */
7683
7684 static int
7685 set_message_1 (a1, a2, nbytes, multibyte_p)
7686 EMACS_INT a1;
7687 Lisp_Object a2;
7688 EMACS_INT nbytes, multibyte_p;
7689 {
7690 const char *s = (const char *) a1;
7691 Lisp_Object string = a2;
7692
7693 xassert (BEG == Z);
7694
7695 /* Change multibyteness of the echo buffer appropriately. */
7696 if (message_enable_multibyte
7697 != !NILP (current_buffer->enable_multibyte_characters))
7698 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7699
7700 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7701
7702 /* Insert new message at BEG. */
7703 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7704
7705 if (STRINGP (string))
7706 {
7707 int nchars;
7708
7709 if (nbytes == 0)
7710 nbytes = SBYTES (string);
7711 nchars = string_byte_to_char (string, nbytes);
7712
7713 /* This function takes care of single/multibyte conversion. We
7714 just have to ensure that the echo area buffer has the right
7715 setting of enable_multibyte_characters. */
7716 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7717 }
7718 else if (s)
7719 {
7720 if (nbytes == 0)
7721 nbytes = strlen (s);
7722
7723 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7724 {
7725 /* Convert from multi-byte to single-byte. */
7726 int i, c, n;
7727 unsigned char work[1];
7728
7729 /* Convert a multibyte string to single-byte. */
7730 for (i = 0; i < nbytes; i += n)
7731 {
7732 c = string_char_and_length (s + i, nbytes - i, &n);
7733 work[0] = (SINGLE_BYTE_CHAR_P (c)
7734 ? c
7735 : multibyte_char_to_unibyte (c, Qnil));
7736 insert_1_both (work, 1, 1, 1, 0, 0);
7737 }
7738 }
7739 else if (!multibyte_p
7740 && !NILP (current_buffer->enable_multibyte_characters))
7741 {
7742 /* Convert from single-byte to multi-byte. */
7743 int i, c, n;
7744 const unsigned char *msg = (const unsigned char *) s;
7745 unsigned char str[MAX_MULTIBYTE_LENGTH];
7746
7747 /* Convert a single-byte string to multibyte. */
7748 for (i = 0; i < nbytes; i++)
7749 {
7750 c = unibyte_char_to_multibyte (msg[i]);
7751 n = CHAR_STRING (c, str);
7752 insert_1_both (str, 1, n, 1, 0, 0);
7753 }
7754 }
7755 else
7756 insert_1 (s, nbytes, 1, 0, 0);
7757 }
7758
7759 return 0;
7760 }
7761
7762
7763 /* Clear messages. CURRENT_P non-zero means clear the current
7764 message. LAST_DISPLAYED_P non-zero means clear the message
7765 last displayed. */
7766
7767 void
7768 clear_message (current_p, last_displayed_p)
7769 int current_p, last_displayed_p;
7770 {
7771 if (current_p)
7772 {
7773 echo_area_buffer[0] = Qnil;
7774 message_cleared_p = 1;
7775 }
7776
7777 if (last_displayed_p)
7778 echo_area_buffer[1] = Qnil;
7779
7780 message_buf_print = 0;
7781 }
7782
7783 /* Clear garbaged frames.
7784
7785 This function is used where the old redisplay called
7786 redraw_garbaged_frames which in turn called redraw_frame which in
7787 turn called clear_frame. The call to clear_frame was a source of
7788 flickering. I believe a clear_frame is not necessary. It should
7789 suffice in the new redisplay to invalidate all current matrices,
7790 and ensure a complete redisplay of all windows. */
7791
7792 static void
7793 clear_garbaged_frames ()
7794 {
7795 if (frame_garbaged)
7796 {
7797 Lisp_Object tail, frame;
7798 int changed_count = 0;
7799
7800 FOR_EACH_FRAME (tail, frame)
7801 {
7802 struct frame *f = XFRAME (frame);
7803
7804 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7805 {
7806 if (f->resized_p)
7807 {
7808 Fredraw_frame (frame);
7809 f->force_flush_display_p = 1;
7810 }
7811 clear_current_matrices (f);
7812 changed_count++;
7813 f->garbaged = 0;
7814 f->resized_p = 0;
7815 }
7816 }
7817
7818 frame_garbaged = 0;
7819 if (changed_count)
7820 ++windows_or_buffers_changed;
7821 }
7822 }
7823
7824
7825 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7826 is non-zero update selected_frame. Value is non-zero if the
7827 mini-windows height has been changed. */
7828
7829 static int
7830 echo_area_display (update_frame_p)
7831 int update_frame_p;
7832 {
7833 Lisp_Object mini_window;
7834 struct window *w;
7835 struct frame *f;
7836 int window_height_changed_p = 0;
7837 struct frame *sf = SELECTED_FRAME ();
7838
7839 mini_window = FRAME_MINIBUF_WINDOW (sf);
7840 w = XWINDOW (mini_window);
7841 f = XFRAME (WINDOW_FRAME (w));
7842
7843 /* Don't display if frame is invisible or not yet initialized. */
7844 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7845 return 0;
7846
7847 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7848 #ifndef MAC_OS8
7849 #ifdef HAVE_WINDOW_SYSTEM
7850 /* When Emacs starts, selected_frame may be a visible terminal
7851 frame, even if we run under a window system. If we let this
7852 through, a message would be displayed on the terminal. */
7853 if (EQ (selected_frame, Vterminal_frame)
7854 && !NILP (Vwindow_system))
7855 return 0;
7856 #endif /* HAVE_WINDOW_SYSTEM */
7857 #endif
7858
7859 /* Redraw garbaged frames. */
7860 if (frame_garbaged)
7861 clear_garbaged_frames ();
7862
7863 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7864 {
7865 echo_area_window = mini_window;
7866 window_height_changed_p = display_echo_area (w);
7867 w->must_be_updated_p = 1;
7868
7869 /* Update the display, unless called from redisplay_internal.
7870 Also don't update the screen during redisplay itself. The
7871 update will happen at the end of redisplay, and an update
7872 here could cause confusion. */
7873 if (update_frame_p && !redisplaying_p)
7874 {
7875 int n = 0;
7876
7877 /* If the display update has been interrupted by pending
7878 input, update mode lines in the frame. Due to the
7879 pending input, it might have been that redisplay hasn't
7880 been called, so that mode lines above the echo area are
7881 garbaged. This looks odd, so we prevent it here. */
7882 if (!display_completed)
7883 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7884
7885 if (window_height_changed_p
7886 /* Don't do this if Emacs is shutting down. Redisplay
7887 needs to run hooks. */
7888 && !NILP (Vrun_hooks))
7889 {
7890 /* Must update other windows. Likewise as in other
7891 cases, don't let this update be interrupted by
7892 pending input. */
7893 int count = SPECPDL_INDEX ();
7894 specbind (Qredisplay_dont_pause, Qt);
7895 windows_or_buffers_changed = 1;
7896 redisplay_internal (0);
7897 unbind_to (count, Qnil);
7898 }
7899 else if (FRAME_WINDOW_P (f) && n == 0)
7900 {
7901 /* Window configuration is the same as before.
7902 Can do with a display update of the echo area,
7903 unless we displayed some mode lines. */
7904 update_single_window (w, 1);
7905 rif->flush_display (f);
7906 }
7907 else
7908 update_frame (f, 1, 1);
7909
7910 /* If cursor is in the echo area, make sure that the next
7911 redisplay displays the minibuffer, so that the cursor will
7912 be replaced with what the minibuffer wants. */
7913 if (cursor_in_echo_area)
7914 ++windows_or_buffers_changed;
7915 }
7916 }
7917 else if (!EQ (mini_window, selected_window))
7918 windows_or_buffers_changed++;
7919
7920 /* Last displayed message is now the current message. */
7921 echo_area_buffer[1] = echo_area_buffer[0];
7922
7923 /* Prevent redisplay optimization in redisplay_internal by resetting
7924 this_line_start_pos. This is done because the mini-buffer now
7925 displays the message instead of its buffer text. */
7926 if (EQ (mini_window, selected_window))
7927 CHARPOS (this_line_start_pos) = 0;
7928
7929 return window_height_changed_p;
7930 }
7931
7932
7933 \f
7934 /***********************************************************************
7935 Frame Titles
7936 ***********************************************************************/
7937
7938
7939 /* The frame title buffering code is also used by Fformat_mode_line.
7940 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7941
7942 /* A buffer for constructing frame titles in it; allocated from the
7943 heap in init_xdisp and resized as needed in store_frame_title_char. */
7944
7945 static char *frame_title_buf;
7946
7947 /* The buffer's end, and a current output position in it. */
7948
7949 static char *frame_title_buf_end;
7950 static char *frame_title_ptr;
7951
7952
7953 /* Store a single character C for the frame title in frame_title_buf.
7954 Re-allocate frame_title_buf if necessary. */
7955
7956 static void
7957 #ifdef PROTOTYPES
7958 store_frame_title_char (char c)
7959 #else
7960 store_frame_title_char (c)
7961 char c;
7962 #endif
7963 {
7964 /* If output position has reached the end of the allocated buffer,
7965 double the buffer's size. */
7966 if (frame_title_ptr == frame_title_buf_end)
7967 {
7968 int len = frame_title_ptr - frame_title_buf;
7969 int new_size = 2 * len * sizeof *frame_title_buf;
7970 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7971 frame_title_buf_end = frame_title_buf + new_size;
7972 frame_title_ptr = frame_title_buf + len;
7973 }
7974
7975 *frame_title_ptr++ = c;
7976 }
7977
7978
7979 /* Store part of a frame title in frame_title_buf, beginning at
7980 frame_title_ptr. STR is the string to store. Do not copy
7981 characters that yield more columns than PRECISION; PRECISION <= 0
7982 means copy the whole string. Pad with spaces until FIELD_WIDTH
7983 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7984 pad. Called from display_mode_element when it is used to build a
7985 frame title. */
7986
7987 static int
7988 store_frame_title (str, field_width, precision)
7989 const unsigned char *str;
7990 int field_width, precision;
7991 {
7992 int n = 0;
7993 int dummy, nbytes;
7994
7995 /* Copy at most PRECISION chars from STR. */
7996 nbytes = strlen (str);
7997 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
7998 while (nbytes--)
7999 store_frame_title_char (*str++);
8000
8001 /* Fill up with spaces until FIELD_WIDTH reached. */
8002 while (field_width > 0
8003 && n < field_width)
8004 {
8005 store_frame_title_char (' ');
8006 ++n;
8007 }
8008
8009 return n;
8010 }
8011
8012 #ifdef HAVE_WINDOW_SYSTEM
8013
8014 /* Set the title of FRAME, if it has changed. The title format is
8015 Vicon_title_format if FRAME is iconified, otherwise it is
8016 frame_title_format. */
8017
8018 static void
8019 x_consider_frame_title (frame)
8020 Lisp_Object frame;
8021 {
8022 struct frame *f = XFRAME (frame);
8023
8024 if (FRAME_WINDOW_P (f)
8025 || FRAME_MINIBUF_ONLY_P (f)
8026 || f->explicit_name)
8027 {
8028 /* Do we have more than one visible frame on this X display? */
8029 Lisp_Object tail;
8030 Lisp_Object fmt;
8031 struct buffer *obuf;
8032 int len;
8033 struct it it;
8034
8035 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8036 {
8037 Lisp_Object other_frame = XCAR (tail);
8038 struct frame *tf = XFRAME (other_frame);
8039
8040 if (tf != f
8041 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8042 && !FRAME_MINIBUF_ONLY_P (tf)
8043 && !EQ (other_frame, tip_frame)
8044 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8045 break;
8046 }
8047
8048 /* Set global variable indicating that multiple frames exist. */
8049 multiple_frames = CONSP (tail);
8050
8051 /* Switch to the buffer of selected window of the frame. Set up
8052 frame_title_ptr so that display_mode_element will output into it;
8053 then display the title. */
8054 obuf = current_buffer;
8055 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8056 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8057 frame_title_ptr = frame_title_buf;
8058 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8059 NULL, DEFAULT_FACE_ID);
8060 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8061 len = frame_title_ptr - frame_title_buf;
8062 frame_title_ptr = NULL;
8063 set_buffer_internal_1 (obuf);
8064
8065 /* Set the title only if it's changed. This avoids consing in
8066 the common case where it hasn't. (If it turns out that we've
8067 already wasted too much time by walking through the list with
8068 display_mode_element, then we might need to optimize at a
8069 higher level than this.) */
8070 if (! STRINGP (f->name)
8071 || SBYTES (f->name) != len
8072 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8073 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8074 }
8075 }
8076
8077 #endif /* not HAVE_WINDOW_SYSTEM */
8078
8079
8080
8081 \f
8082 /***********************************************************************
8083 Menu Bars
8084 ***********************************************************************/
8085
8086
8087 /* Prepare for redisplay by updating menu-bar item lists when
8088 appropriate. This can call eval. */
8089
8090 void
8091 prepare_menu_bars ()
8092 {
8093 int all_windows;
8094 struct gcpro gcpro1, gcpro2;
8095 struct frame *f;
8096 Lisp_Object tooltip_frame;
8097
8098 #ifdef HAVE_WINDOW_SYSTEM
8099 tooltip_frame = tip_frame;
8100 #else
8101 tooltip_frame = Qnil;
8102 #endif
8103
8104 /* Update all frame titles based on their buffer names, etc. We do
8105 this before the menu bars so that the buffer-menu will show the
8106 up-to-date frame titles. */
8107 #ifdef HAVE_WINDOW_SYSTEM
8108 if (windows_or_buffers_changed || update_mode_lines)
8109 {
8110 Lisp_Object tail, frame;
8111
8112 FOR_EACH_FRAME (tail, frame)
8113 {
8114 f = XFRAME (frame);
8115 if (!EQ (frame, tooltip_frame)
8116 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8117 x_consider_frame_title (frame);
8118 }
8119 }
8120 #endif /* HAVE_WINDOW_SYSTEM */
8121
8122 /* Update the menu bar item lists, if appropriate. This has to be
8123 done before any actual redisplay or generation of display lines. */
8124 all_windows = (update_mode_lines
8125 || buffer_shared > 1
8126 || windows_or_buffers_changed);
8127 if (all_windows)
8128 {
8129 Lisp_Object tail, frame;
8130 int count = SPECPDL_INDEX ();
8131
8132 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8133
8134 FOR_EACH_FRAME (tail, frame)
8135 {
8136 f = XFRAME (frame);
8137
8138 /* Ignore tooltip frame. */
8139 if (EQ (frame, tooltip_frame))
8140 continue;
8141
8142 /* If a window on this frame changed size, report that to
8143 the user and clear the size-change flag. */
8144 if (FRAME_WINDOW_SIZES_CHANGED (f))
8145 {
8146 Lisp_Object functions;
8147
8148 /* Clear flag first in case we get an error below. */
8149 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8150 functions = Vwindow_size_change_functions;
8151 GCPRO2 (tail, functions);
8152
8153 while (CONSP (functions))
8154 {
8155 call1 (XCAR (functions), frame);
8156 functions = XCDR (functions);
8157 }
8158 UNGCPRO;
8159 }
8160
8161 GCPRO1 (tail);
8162 update_menu_bar (f, 0);
8163 #ifdef HAVE_WINDOW_SYSTEM
8164 update_tool_bar (f, 0);
8165 #endif
8166 UNGCPRO;
8167 }
8168
8169 unbind_to (count, Qnil);
8170 }
8171 else
8172 {
8173 struct frame *sf = SELECTED_FRAME ();
8174 update_menu_bar (sf, 1);
8175 #ifdef HAVE_WINDOW_SYSTEM
8176 update_tool_bar (sf, 1);
8177 #endif
8178 }
8179
8180 /* Motif needs this. See comment in xmenu.c. Turn it off when
8181 pending_menu_activation is not defined. */
8182 #ifdef USE_X_TOOLKIT
8183 pending_menu_activation = 0;
8184 #endif
8185 }
8186
8187
8188 /* Update the menu bar item list for frame F. This has to be done
8189 before we start to fill in any display lines, because it can call
8190 eval.
8191
8192 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8193
8194 static void
8195 update_menu_bar (f, save_match_data)
8196 struct frame *f;
8197 int save_match_data;
8198 {
8199 Lisp_Object window;
8200 register struct window *w;
8201
8202 /* If called recursively during a menu update, do nothing. This can
8203 happen when, for instance, an activate-menubar-hook causes a
8204 redisplay. */
8205 if (inhibit_menubar_update)
8206 return;
8207
8208 window = FRAME_SELECTED_WINDOW (f);
8209 w = XWINDOW (window);
8210
8211 #if 0 /* The if statement below this if statement used to include the
8212 condition !NILP (w->update_mode_line), rather than using
8213 update_mode_lines directly, and this if statement may have
8214 been added to make that condition work. Now the if
8215 statement below matches its comment, this isn't needed. */
8216 if (update_mode_lines)
8217 w->update_mode_line = Qt;
8218 #endif
8219
8220 if (FRAME_WINDOW_P (f)
8221 ?
8222 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8223 || defined (USE_GTK)
8224 FRAME_EXTERNAL_MENU_BAR (f)
8225 #else
8226 FRAME_MENU_BAR_LINES (f) > 0
8227 #endif
8228 : FRAME_MENU_BAR_LINES (f) > 0)
8229 {
8230 /* If the user has switched buffers or windows, we need to
8231 recompute to reflect the new bindings. But we'll
8232 recompute when update_mode_lines is set too; that means
8233 that people can use force-mode-line-update to request
8234 that the menu bar be recomputed. The adverse effect on
8235 the rest of the redisplay algorithm is about the same as
8236 windows_or_buffers_changed anyway. */
8237 if (windows_or_buffers_changed
8238 /* This used to test w->update_mode_line, but we believe
8239 there is no need to recompute the menu in that case. */
8240 || update_mode_lines
8241 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8242 < BUF_MODIFF (XBUFFER (w->buffer)))
8243 != !NILP (w->last_had_star))
8244 || ((!NILP (Vtransient_mark_mode)
8245 && !NILP (XBUFFER (w->buffer)->mark_active))
8246 != !NILP (w->region_showing)))
8247 {
8248 struct buffer *prev = current_buffer;
8249 int count = SPECPDL_INDEX ();
8250
8251 specbind (Qinhibit_menubar_update, Qt);
8252
8253 set_buffer_internal_1 (XBUFFER (w->buffer));
8254 if (save_match_data)
8255 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8256 if (NILP (Voverriding_local_map_menu_flag))
8257 {
8258 specbind (Qoverriding_terminal_local_map, Qnil);
8259 specbind (Qoverriding_local_map, Qnil);
8260 }
8261
8262 /* Run the Lucid hook. */
8263 safe_run_hooks (Qactivate_menubar_hook);
8264
8265 /* If it has changed current-menubar from previous value,
8266 really recompute the menu-bar from the value. */
8267 if (! NILP (Vlucid_menu_bar_dirty_flag))
8268 call0 (Qrecompute_lucid_menubar);
8269
8270 safe_run_hooks (Qmenu_bar_update_hook);
8271 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8272
8273 /* Redisplay the menu bar in case we changed it. */
8274 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8275 || defined (USE_GTK)
8276 if (FRAME_WINDOW_P (f)
8277 #if defined (MAC_OS)
8278 /* All frames on Mac OS share the same menubar. So only the
8279 selected frame should be allowed to set it. */
8280 && f == SELECTED_FRAME ()
8281 #endif
8282 )
8283 set_frame_menubar (f, 0, 0);
8284 else
8285 /* On a terminal screen, the menu bar is an ordinary screen
8286 line, and this makes it get updated. */
8287 w->update_mode_line = Qt;
8288 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8289 /* In the non-toolkit version, the menu bar is an ordinary screen
8290 line, and this makes it get updated. */
8291 w->update_mode_line = Qt;
8292 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8293
8294 unbind_to (count, Qnil);
8295 set_buffer_internal_1 (prev);
8296 }
8297 }
8298 }
8299
8300
8301 \f
8302 /***********************************************************************
8303 Output Cursor
8304 ***********************************************************************/
8305
8306 #ifdef HAVE_WINDOW_SYSTEM
8307
8308 /* EXPORT:
8309 Nominal cursor position -- where to draw output.
8310 HPOS and VPOS are window relative glyph matrix coordinates.
8311 X and Y are window relative pixel coordinates. */
8312
8313 struct cursor_pos output_cursor;
8314
8315
8316 /* EXPORT:
8317 Set the global variable output_cursor to CURSOR. All cursor
8318 positions are relative to updated_window. */
8319
8320 void
8321 set_output_cursor (cursor)
8322 struct cursor_pos *cursor;
8323 {
8324 output_cursor.hpos = cursor->hpos;
8325 output_cursor.vpos = cursor->vpos;
8326 output_cursor.x = cursor->x;
8327 output_cursor.y = cursor->y;
8328 }
8329
8330
8331 /* EXPORT for RIF:
8332 Set a nominal cursor position.
8333
8334 HPOS and VPOS are column/row positions in a window glyph matrix. X
8335 and Y are window text area relative pixel positions.
8336
8337 If this is done during an update, updated_window will contain the
8338 window that is being updated and the position is the future output
8339 cursor position for that window. If updated_window is null, use
8340 selected_window and display the cursor at the given position. */
8341
8342 void
8343 x_cursor_to (vpos, hpos, y, x)
8344 int vpos, hpos, y, x;
8345 {
8346 struct window *w;
8347
8348 /* If updated_window is not set, work on selected_window. */
8349 if (updated_window)
8350 w = updated_window;
8351 else
8352 w = XWINDOW (selected_window);
8353
8354 /* Set the output cursor. */
8355 output_cursor.hpos = hpos;
8356 output_cursor.vpos = vpos;
8357 output_cursor.x = x;
8358 output_cursor.y = y;
8359
8360 /* If not called as part of an update, really display the cursor.
8361 This will also set the cursor position of W. */
8362 if (updated_window == NULL)
8363 {
8364 BLOCK_INPUT;
8365 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8366 if (rif->flush_display_optional)
8367 rif->flush_display_optional (SELECTED_FRAME ());
8368 UNBLOCK_INPUT;
8369 }
8370 }
8371
8372 #endif /* HAVE_WINDOW_SYSTEM */
8373
8374 \f
8375 /***********************************************************************
8376 Tool-bars
8377 ***********************************************************************/
8378
8379 #ifdef HAVE_WINDOW_SYSTEM
8380
8381 /* Where the mouse was last time we reported a mouse event. */
8382
8383 FRAME_PTR last_mouse_frame;
8384
8385 /* Tool-bar item index of the item on which a mouse button was pressed
8386 or -1. */
8387
8388 int last_tool_bar_item;
8389
8390
8391 /* Update the tool-bar item list for frame F. This has to be done
8392 before we start to fill in any display lines. Called from
8393 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8394 and restore it here. */
8395
8396 static void
8397 update_tool_bar (f, save_match_data)
8398 struct frame *f;
8399 int save_match_data;
8400 {
8401 #ifdef USE_GTK
8402 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8403 #else
8404 int do_update = WINDOWP (f->tool_bar_window)
8405 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8406 #endif
8407
8408 if (do_update)
8409 {
8410 Lisp_Object window;
8411 struct window *w;
8412
8413 window = FRAME_SELECTED_WINDOW (f);
8414 w = XWINDOW (window);
8415
8416 /* If the user has switched buffers or windows, we need to
8417 recompute to reflect the new bindings. But we'll
8418 recompute when update_mode_lines is set too; that means
8419 that people can use force-mode-line-update to request
8420 that the menu bar be recomputed. The adverse effect on
8421 the rest of the redisplay algorithm is about the same as
8422 windows_or_buffers_changed anyway. */
8423 if (windows_or_buffers_changed
8424 || !NILP (w->update_mode_line)
8425 || update_mode_lines
8426 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8427 < BUF_MODIFF (XBUFFER (w->buffer)))
8428 != !NILP (w->last_had_star))
8429 || ((!NILP (Vtransient_mark_mode)
8430 && !NILP (XBUFFER (w->buffer)->mark_active))
8431 != !NILP (w->region_showing)))
8432 {
8433 struct buffer *prev = current_buffer;
8434 int count = SPECPDL_INDEX ();
8435 Lisp_Object new_tool_bar;
8436 int new_n_tool_bar;
8437 struct gcpro gcpro1;
8438
8439 /* Set current_buffer to the buffer of the selected
8440 window of the frame, so that we get the right local
8441 keymaps. */
8442 set_buffer_internal_1 (XBUFFER (w->buffer));
8443
8444 /* Save match data, if we must. */
8445 if (save_match_data)
8446 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8447
8448 /* Make sure that we don't accidentally use bogus keymaps. */
8449 if (NILP (Voverriding_local_map_menu_flag))
8450 {
8451 specbind (Qoverriding_terminal_local_map, Qnil);
8452 specbind (Qoverriding_local_map, Qnil);
8453 }
8454
8455 GCPRO1 (new_tool_bar);
8456
8457 /* Build desired tool-bar items from keymaps. */
8458 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8459 &new_n_tool_bar);
8460
8461 /* Redisplay the tool-bar if we changed it. */
8462 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8463 {
8464 /* Redisplay that happens asynchronously due to an expose event
8465 may access f->tool_bar_items. Make sure we update both
8466 variables within BLOCK_INPUT so no such event interrupts. */
8467 BLOCK_INPUT;
8468 f->tool_bar_items = new_tool_bar;
8469 f->n_tool_bar_items = new_n_tool_bar;
8470 w->update_mode_line = Qt;
8471 UNBLOCK_INPUT;
8472 }
8473
8474 UNGCPRO;
8475
8476 unbind_to (count, Qnil);
8477 set_buffer_internal_1 (prev);
8478 }
8479 }
8480 }
8481
8482
8483 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8484 F's desired tool-bar contents. F->tool_bar_items must have
8485 been set up previously by calling prepare_menu_bars. */
8486
8487 static void
8488 build_desired_tool_bar_string (f)
8489 struct frame *f;
8490 {
8491 int i, size, size_needed;
8492 struct gcpro gcpro1, gcpro2, gcpro3;
8493 Lisp_Object image, plist, props;
8494
8495 image = plist = props = Qnil;
8496 GCPRO3 (image, plist, props);
8497
8498 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8499 Otherwise, make a new string. */
8500
8501 /* The size of the string we might be able to reuse. */
8502 size = (STRINGP (f->desired_tool_bar_string)
8503 ? SCHARS (f->desired_tool_bar_string)
8504 : 0);
8505
8506 /* We need one space in the string for each image. */
8507 size_needed = f->n_tool_bar_items;
8508
8509 /* Reuse f->desired_tool_bar_string, if possible. */
8510 if (size < size_needed || NILP (f->desired_tool_bar_string))
8511 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8512 make_number (' '));
8513 else
8514 {
8515 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8516 Fremove_text_properties (make_number (0), make_number (size),
8517 props, f->desired_tool_bar_string);
8518 }
8519
8520 /* Put a `display' property on the string for the images to display,
8521 put a `menu_item' property on tool-bar items with a value that
8522 is the index of the item in F's tool-bar item vector. */
8523 for (i = 0; i < f->n_tool_bar_items; ++i)
8524 {
8525 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8526
8527 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8528 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8529 int hmargin, vmargin, relief, idx, end;
8530 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8531
8532 /* If image is a vector, choose the image according to the
8533 button state. */
8534 image = PROP (TOOL_BAR_ITEM_IMAGES);
8535 if (VECTORP (image))
8536 {
8537 if (enabled_p)
8538 idx = (selected_p
8539 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8540 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8541 else
8542 idx = (selected_p
8543 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8544 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8545
8546 xassert (ASIZE (image) >= idx);
8547 image = AREF (image, idx);
8548 }
8549 else
8550 idx = -1;
8551
8552 /* Ignore invalid image specifications. */
8553 if (!valid_image_p (image))
8554 continue;
8555
8556 /* Display the tool-bar button pressed, or depressed. */
8557 plist = Fcopy_sequence (XCDR (image));
8558
8559 /* Compute margin and relief to draw. */
8560 relief = (tool_bar_button_relief >= 0
8561 ? tool_bar_button_relief
8562 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8563 hmargin = vmargin = relief;
8564
8565 if (INTEGERP (Vtool_bar_button_margin)
8566 && XINT (Vtool_bar_button_margin) > 0)
8567 {
8568 hmargin += XFASTINT (Vtool_bar_button_margin);
8569 vmargin += XFASTINT (Vtool_bar_button_margin);
8570 }
8571 else if (CONSP (Vtool_bar_button_margin))
8572 {
8573 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8574 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8575 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8576
8577 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8578 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8579 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8580 }
8581
8582 if (auto_raise_tool_bar_buttons_p)
8583 {
8584 /* Add a `:relief' property to the image spec if the item is
8585 selected. */
8586 if (selected_p)
8587 {
8588 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8589 hmargin -= relief;
8590 vmargin -= relief;
8591 }
8592 }
8593 else
8594 {
8595 /* If image is selected, display it pressed, i.e. with a
8596 negative relief. If it's not selected, display it with a
8597 raised relief. */
8598 plist = Fplist_put (plist, QCrelief,
8599 (selected_p
8600 ? make_number (-relief)
8601 : make_number (relief)));
8602 hmargin -= relief;
8603 vmargin -= relief;
8604 }
8605
8606 /* Put a margin around the image. */
8607 if (hmargin || vmargin)
8608 {
8609 if (hmargin == vmargin)
8610 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8611 else
8612 plist = Fplist_put (plist, QCmargin,
8613 Fcons (make_number (hmargin),
8614 make_number (vmargin)));
8615 }
8616
8617 /* If button is not enabled, and we don't have special images
8618 for the disabled state, make the image appear disabled by
8619 applying an appropriate algorithm to it. */
8620 if (!enabled_p && idx < 0)
8621 plist = Fplist_put (plist, QCconversion, Qdisabled);
8622
8623 /* Put a `display' text property on the string for the image to
8624 display. Put a `menu-item' property on the string that gives
8625 the start of this item's properties in the tool-bar items
8626 vector. */
8627 image = Fcons (Qimage, plist);
8628 props = list4 (Qdisplay, image,
8629 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8630
8631 /* Let the last image hide all remaining spaces in the tool bar
8632 string. The string can be longer than needed when we reuse a
8633 previous string. */
8634 if (i + 1 == f->n_tool_bar_items)
8635 end = SCHARS (f->desired_tool_bar_string);
8636 else
8637 end = i + 1;
8638 Fadd_text_properties (make_number (i), make_number (end),
8639 props, f->desired_tool_bar_string);
8640 #undef PROP
8641 }
8642
8643 UNGCPRO;
8644 }
8645
8646
8647 /* Display one line of the tool-bar of frame IT->f. */
8648
8649 static void
8650 display_tool_bar_line (it)
8651 struct it *it;
8652 {
8653 struct glyph_row *row = it->glyph_row;
8654 int max_x = it->last_visible_x;
8655 struct glyph *last;
8656
8657 prepare_desired_row (row);
8658 row->y = it->current_y;
8659
8660 /* Note that this isn't made use of if the face hasn't a box,
8661 so there's no need to check the face here. */
8662 it->start_of_box_run_p = 1;
8663
8664 while (it->current_x < max_x)
8665 {
8666 int x_before, x, n_glyphs_before, i, nglyphs;
8667
8668 /* Get the next display element. */
8669 if (!get_next_display_element (it))
8670 break;
8671
8672 /* Produce glyphs. */
8673 x_before = it->current_x;
8674 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8675 PRODUCE_GLYPHS (it);
8676
8677 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8678 i = 0;
8679 x = x_before;
8680 while (i < nglyphs)
8681 {
8682 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8683
8684 if (x + glyph->pixel_width > max_x)
8685 {
8686 /* Glyph doesn't fit on line. */
8687 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8688 it->current_x = x;
8689 goto out;
8690 }
8691
8692 ++it->hpos;
8693 x += glyph->pixel_width;
8694 ++i;
8695 }
8696
8697 /* Stop at line ends. */
8698 if (ITERATOR_AT_END_OF_LINE_P (it))
8699 break;
8700
8701 set_iterator_to_next (it, 1);
8702 }
8703
8704 out:;
8705
8706 row->displays_text_p = row->used[TEXT_AREA] != 0;
8707 extend_face_to_end_of_line (it);
8708 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8709 last->right_box_line_p = 1;
8710 if (last == row->glyphs[TEXT_AREA])
8711 last->left_box_line_p = 1;
8712 compute_line_metrics (it);
8713
8714 /* If line is empty, make it occupy the rest of the tool-bar. */
8715 if (!row->displays_text_p)
8716 {
8717 row->height = row->phys_height = it->last_visible_y - row->y;
8718 row->ascent = row->phys_ascent = 0;
8719 row->extra_line_spacing = 0;
8720 }
8721
8722 row->full_width_p = 1;
8723 row->continued_p = 0;
8724 row->truncated_on_left_p = 0;
8725 row->truncated_on_right_p = 0;
8726
8727 it->current_x = it->hpos = 0;
8728 it->current_y += row->height;
8729 ++it->vpos;
8730 ++it->glyph_row;
8731 }
8732
8733
8734 /* Value is the number of screen lines needed to make all tool-bar
8735 items of frame F visible. */
8736
8737 static int
8738 tool_bar_lines_needed (f)
8739 struct frame *f;
8740 {
8741 struct window *w = XWINDOW (f->tool_bar_window);
8742 struct it it;
8743
8744 /* Initialize an iterator for iteration over
8745 F->desired_tool_bar_string in the tool-bar window of frame F. */
8746 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8747 it.first_visible_x = 0;
8748 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8749 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8750
8751 while (!ITERATOR_AT_END_P (&it))
8752 {
8753 it.glyph_row = w->desired_matrix->rows;
8754 clear_glyph_row (it.glyph_row);
8755 display_tool_bar_line (&it);
8756 }
8757
8758 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8759 }
8760
8761
8762 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8763 0, 1, 0,
8764 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8765 (frame)
8766 Lisp_Object frame;
8767 {
8768 struct frame *f;
8769 struct window *w;
8770 int nlines = 0;
8771
8772 if (NILP (frame))
8773 frame = selected_frame;
8774 else
8775 CHECK_FRAME (frame);
8776 f = XFRAME (frame);
8777
8778 if (WINDOWP (f->tool_bar_window)
8779 || (w = XWINDOW (f->tool_bar_window),
8780 WINDOW_TOTAL_LINES (w) > 0))
8781 {
8782 update_tool_bar (f, 1);
8783 if (f->n_tool_bar_items)
8784 {
8785 build_desired_tool_bar_string (f);
8786 nlines = tool_bar_lines_needed (f);
8787 }
8788 }
8789
8790 return make_number (nlines);
8791 }
8792
8793
8794 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8795 height should be changed. */
8796
8797 static int
8798 redisplay_tool_bar (f)
8799 struct frame *f;
8800 {
8801 struct window *w;
8802 struct it it;
8803 struct glyph_row *row;
8804 int change_height_p = 0;
8805
8806 #ifdef USE_GTK
8807 if (FRAME_EXTERNAL_TOOL_BAR (f))
8808 update_frame_tool_bar (f);
8809 return 0;
8810 #endif
8811
8812 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8813 do anything. This means you must start with tool-bar-lines
8814 non-zero to get the auto-sizing effect. Or in other words, you
8815 can turn off tool-bars by specifying tool-bar-lines zero. */
8816 if (!WINDOWP (f->tool_bar_window)
8817 || (w = XWINDOW (f->tool_bar_window),
8818 WINDOW_TOTAL_LINES (w) == 0))
8819 return 0;
8820
8821 /* Set up an iterator for the tool-bar window. */
8822 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8823 it.first_visible_x = 0;
8824 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8825 row = it.glyph_row;
8826
8827 /* Build a string that represents the contents of the tool-bar. */
8828 build_desired_tool_bar_string (f);
8829 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8830
8831 /* Display as many lines as needed to display all tool-bar items. */
8832 while (it.current_y < it.last_visible_y)
8833 display_tool_bar_line (&it);
8834
8835 /* It doesn't make much sense to try scrolling in the tool-bar
8836 window, so don't do it. */
8837 w->desired_matrix->no_scrolling_p = 1;
8838 w->must_be_updated_p = 1;
8839
8840 if (auto_resize_tool_bars_p)
8841 {
8842 int nlines;
8843
8844 /* If we couldn't display everything, change the tool-bar's
8845 height. */
8846 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8847 change_height_p = 1;
8848
8849 /* If there are blank lines at the end, except for a partially
8850 visible blank line at the end that is smaller than
8851 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8852 row = it.glyph_row - 1;
8853 if (!row->displays_text_p
8854 && row->height >= FRAME_LINE_HEIGHT (f))
8855 change_height_p = 1;
8856
8857 /* If row displays tool-bar items, but is partially visible,
8858 change the tool-bar's height. */
8859 if (row->displays_text_p
8860 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8861 change_height_p = 1;
8862
8863 /* Resize windows as needed by changing the `tool-bar-lines'
8864 frame parameter. */
8865 if (change_height_p
8866 && (nlines = tool_bar_lines_needed (f),
8867 nlines != WINDOW_TOTAL_LINES (w)))
8868 {
8869 extern Lisp_Object Qtool_bar_lines;
8870 Lisp_Object frame;
8871 int old_height = WINDOW_TOTAL_LINES (w);
8872
8873 XSETFRAME (frame, f);
8874 clear_glyph_matrix (w->desired_matrix);
8875 Fmodify_frame_parameters (frame,
8876 Fcons (Fcons (Qtool_bar_lines,
8877 make_number (nlines)),
8878 Qnil));
8879 if (WINDOW_TOTAL_LINES (w) != old_height)
8880 fonts_changed_p = 1;
8881 }
8882 }
8883
8884 return change_height_p;
8885 }
8886
8887
8888 /* Get information about the tool-bar item which is displayed in GLYPH
8889 on frame F. Return in *PROP_IDX the index where tool-bar item
8890 properties start in F->tool_bar_items. Value is zero if
8891 GLYPH doesn't display a tool-bar item. */
8892
8893 static int
8894 tool_bar_item_info (f, glyph, prop_idx)
8895 struct frame *f;
8896 struct glyph *glyph;
8897 int *prop_idx;
8898 {
8899 Lisp_Object prop;
8900 int success_p;
8901 int charpos;
8902
8903 /* This function can be called asynchronously, which means we must
8904 exclude any possibility that Fget_text_property signals an
8905 error. */
8906 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8907 charpos = max (0, charpos);
8908
8909 /* Get the text property `menu-item' at pos. The value of that
8910 property is the start index of this item's properties in
8911 F->tool_bar_items. */
8912 prop = Fget_text_property (make_number (charpos),
8913 Qmenu_item, f->current_tool_bar_string);
8914 if (INTEGERP (prop))
8915 {
8916 *prop_idx = XINT (prop);
8917 success_p = 1;
8918 }
8919 else
8920 success_p = 0;
8921
8922 return success_p;
8923 }
8924
8925 \f
8926 /* Get information about the tool-bar item at position X/Y on frame F.
8927 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8928 the current matrix of the tool-bar window of F, or NULL if not
8929 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8930 item in F->tool_bar_items. Value is
8931
8932 -1 if X/Y is not on a tool-bar item
8933 0 if X/Y is on the same item that was highlighted before.
8934 1 otherwise. */
8935
8936 static int
8937 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8938 struct frame *f;
8939 int x, y;
8940 struct glyph **glyph;
8941 int *hpos, *vpos, *prop_idx;
8942 {
8943 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8944 struct window *w = XWINDOW (f->tool_bar_window);
8945 int area;
8946
8947 /* Find the glyph under X/Y. */
8948 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8949 if (*glyph == NULL)
8950 return -1;
8951
8952 /* Get the start of this tool-bar item's properties in
8953 f->tool_bar_items. */
8954 if (!tool_bar_item_info (f, *glyph, prop_idx))
8955 return -1;
8956
8957 /* Is mouse on the highlighted item? */
8958 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8959 && *vpos >= dpyinfo->mouse_face_beg_row
8960 && *vpos <= dpyinfo->mouse_face_end_row
8961 && (*vpos > dpyinfo->mouse_face_beg_row
8962 || *hpos >= dpyinfo->mouse_face_beg_col)
8963 && (*vpos < dpyinfo->mouse_face_end_row
8964 || *hpos < dpyinfo->mouse_face_end_col
8965 || dpyinfo->mouse_face_past_end))
8966 return 0;
8967
8968 return 1;
8969 }
8970
8971
8972 /* EXPORT:
8973 Handle mouse button event on the tool-bar of frame F, at
8974 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8975 0 for button release. MODIFIERS is event modifiers for button
8976 release. */
8977
8978 void
8979 handle_tool_bar_click (f, x, y, down_p, modifiers)
8980 struct frame *f;
8981 int x, y, down_p;
8982 unsigned int modifiers;
8983 {
8984 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8985 struct window *w = XWINDOW (f->tool_bar_window);
8986 int hpos, vpos, prop_idx;
8987 struct glyph *glyph;
8988 Lisp_Object enabled_p;
8989
8990 /* If not on the highlighted tool-bar item, return. */
8991 frame_to_window_pixel_xy (w, &x, &y);
8992 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8993 return;
8994
8995 /* If item is disabled, do nothing. */
8996 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8997 if (NILP (enabled_p))
8998 return;
8999
9000 if (down_p)
9001 {
9002 /* Show item in pressed state. */
9003 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9004 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9005 last_tool_bar_item = prop_idx;
9006 }
9007 else
9008 {
9009 Lisp_Object key, frame;
9010 struct input_event event;
9011 EVENT_INIT (event);
9012
9013 /* Show item in released state. */
9014 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9015 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9016
9017 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9018
9019 XSETFRAME (frame, f);
9020 event.kind = TOOL_BAR_EVENT;
9021 event.frame_or_window = frame;
9022 event.arg = frame;
9023 kbd_buffer_store_event (&event);
9024
9025 event.kind = TOOL_BAR_EVENT;
9026 event.frame_or_window = frame;
9027 event.arg = key;
9028 event.modifiers = modifiers;
9029 kbd_buffer_store_event (&event);
9030 last_tool_bar_item = -1;
9031 }
9032 }
9033
9034
9035 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9036 tool-bar window-relative coordinates X/Y. Called from
9037 note_mouse_highlight. */
9038
9039 static void
9040 note_tool_bar_highlight (f, x, y)
9041 struct frame *f;
9042 int x, y;
9043 {
9044 Lisp_Object window = f->tool_bar_window;
9045 struct window *w = XWINDOW (window);
9046 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9047 int hpos, vpos;
9048 struct glyph *glyph;
9049 struct glyph_row *row;
9050 int i;
9051 Lisp_Object enabled_p;
9052 int prop_idx;
9053 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9054 int mouse_down_p, rc;
9055
9056 /* Function note_mouse_highlight is called with negative x(y
9057 values when mouse moves outside of the frame. */
9058 if (x <= 0 || y <= 0)
9059 {
9060 clear_mouse_face (dpyinfo);
9061 return;
9062 }
9063
9064 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9065 if (rc < 0)
9066 {
9067 /* Not on tool-bar item. */
9068 clear_mouse_face (dpyinfo);
9069 return;
9070 }
9071 else if (rc == 0)
9072 /* On same tool-bar item as before. */
9073 goto set_help_echo;
9074
9075 clear_mouse_face (dpyinfo);
9076
9077 /* Mouse is down, but on different tool-bar item? */
9078 mouse_down_p = (dpyinfo->grabbed
9079 && f == last_mouse_frame
9080 && FRAME_LIVE_P (f));
9081 if (mouse_down_p
9082 && last_tool_bar_item != prop_idx)
9083 return;
9084
9085 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9086 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9087
9088 /* If tool-bar item is not enabled, don't highlight it. */
9089 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9090 if (!NILP (enabled_p))
9091 {
9092 /* Compute the x-position of the glyph. In front and past the
9093 image is a space. We include this in the highlighted area. */
9094 row = MATRIX_ROW (w->current_matrix, vpos);
9095 for (i = x = 0; i < hpos; ++i)
9096 x += row->glyphs[TEXT_AREA][i].pixel_width;
9097
9098 /* Record this as the current active region. */
9099 dpyinfo->mouse_face_beg_col = hpos;
9100 dpyinfo->mouse_face_beg_row = vpos;
9101 dpyinfo->mouse_face_beg_x = x;
9102 dpyinfo->mouse_face_beg_y = row->y;
9103 dpyinfo->mouse_face_past_end = 0;
9104
9105 dpyinfo->mouse_face_end_col = hpos + 1;
9106 dpyinfo->mouse_face_end_row = vpos;
9107 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9108 dpyinfo->mouse_face_end_y = row->y;
9109 dpyinfo->mouse_face_window = window;
9110 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9111
9112 /* Display it as active. */
9113 show_mouse_face (dpyinfo, draw);
9114 dpyinfo->mouse_face_image_state = draw;
9115 }
9116
9117 set_help_echo:
9118
9119 /* Set help_echo_string to a help string to display for this tool-bar item.
9120 XTread_socket does the rest. */
9121 help_echo_object = help_echo_window = Qnil;
9122 help_echo_pos = -1;
9123 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9124 if (NILP (help_echo_string))
9125 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9126 }
9127
9128 #endif /* HAVE_WINDOW_SYSTEM */
9129
9130
9131 \f
9132 /************************************************************************
9133 Horizontal scrolling
9134 ************************************************************************/
9135
9136 static int hscroll_window_tree P_ ((Lisp_Object));
9137 static int hscroll_windows P_ ((Lisp_Object));
9138
9139 /* For all leaf windows in the window tree rooted at WINDOW, set their
9140 hscroll value so that PT is (i) visible in the window, and (ii) so
9141 that it is not within a certain margin at the window's left and
9142 right border. Value is non-zero if any window's hscroll has been
9143 changed. */
9144
9145 static int
9146 hscroll_window_tree (window)
9147 Lisp_Object window;
9148 {
9149 int hscrolled_p = 0;
9150 int hscroll_relative_p = FLOATP (Vhscroll_step);
9151 int hscroll_step_abs = 0;
9152 double hscroll_step_rel = 0;
9153
9154 if (hscroll_relative_p)
9155 {
9156 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9157 if (hscroll_step_rel < 0)
9158 {
9159 hscroll_relative_p = 0;
9160 hscroll_step_abs = 0;
9161 }
9162 }
9163 else if (INTEGERP (Vhscroll_step))
9164 {
9165 hscroll_step_abs = XINT (Vhscroll_step);
9166 if (hscroll_step_abs < 0)
9167 hscroll_step_abs = 0;
9168 }
9169 else
9170 hscroll_step_abs = 0;
9171
9172 while (WINDOWP (window))
9173 {
9174 struct window *w = XWINDOW (window);
9175
9176 if (WINDOWP (w->hchild))
9177 hscrolled_p |= hscroll_window_tree (w->hchild);
9178 else if (WINDOWP (w->vchild))
9179 hscrolled_p |= hscroll_window_tree (w->vchild);
9180 else if (w->cursor.vpos >= 0)
9181 {
9182 int h_margin;
9183 int text_area_width;
9184 struct glyph_row *current_cursor_row
9185 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9186 struct glyph_row *desired_cursor_row
9187 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9188 struct glyph_row *cursor_row
9189 = (desired_cursor_row->enabled_p
9190 ? desired_cursor_row
9191 : current_cursor_row);
9192
9193 text_area_width = window_box_width (w, TEXT_AREA);
9194
9195 /* Scroll when cursor is inside this scroll margin. */
9196 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9197
9198 if ((XFASTINT (w->hscroll)
9199 && w->cursor.x <= h_margin)
9200 || (cursor_row->enabled_p
9201 && cursor_row->truncated_on_right_p
9202 && (w->cursor.x >= text_area_width - h_margin)))
9203 {
9204 struct it it;
9205 int hscroll;
9206 struct buffer *saved_current_buffer;
9207 int pt;
9208 int wanted_x;
9209
9210 /* Find point in a display of infinite width. */
9211 saved_current_buffer = current_buffer;
9212 current_buffer = XBUFFER (w->buffer);
9213
9214 if (w == XWINDOW (selected_window))
9215 pt = BUF_PT (current_buffer);
9216 else
9217 {
9218 pt = marker_position (w->pointm);
9219 pt = max (BEGV, pt);
9220 pt = min (ZV, pt);
9221 }
9222
9223 /* Move iterator to pt starting at cursor_row->start in
9224 a line with infinite width. */
9225 init_to_row_start (&it, w, cursor_row);
9226 it.last_visible_x = INFINITY;
9227 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9228 current_buffer = saved_current_buffer;
9229
9230 /* Position cursor in window. */
9231 if (!hscroll_relative_p && hscroll_step_abs == 0)
9232 hscroll = max (0, (it.current_x
9233 - (ITERATOR_AT_END_OF_LINE_P (&it)
9234 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9235 : (text_area_width / 2))))
9236 / FRAME_COLUMN_WIDTH (it.f);
9237 else if (w->cursor.x >= text_area_width - h_margin)
9238 {
9239 if (hscroll_relative_p)
9240 wanted_x = text_area_width * (1 - hscroll_step_rel)
9241 - h_margin;
9242 else
9243 wanted_x = text_area_width
9244 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9245 - h_margin;
9246 hscroll
9247 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9248 }
9249 else
9250 {
9251 if (hscroll_relative_p)
9252 wanted_x = text_area_width * hscroll_step_rel
9253 + h_margin;
9254 else
9255 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9256 + h_margin;
9257 hscroll
9258 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9259 }
9260 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9261
9262 /* Don't call Fset_window_hscroll if value hasn't
9263 changed because it will prevent redisplay
9264 optimizations. */
9265 if (XFASTINT (w->hscroll) != hscroll)
9266 {
9267 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9268 w->hscroll = make_number (hscroll);
9269 hscrolled_p = 1;
9270 }
9271 }
9272 }
9273
9274 window = w->next;
9275 }
9276
9277 /* Value is non-zero if hscroll of any leaf window has been changed. */
9278 return hscrolled_p;
9279 }
9280
9281
9282 /* Set hscroll so that cursor is visible and not inside horizontal
9283 scroll margins for all windows in the tree rooted at WINDOW. See
9284 also hscroll_window_tree above. Value is non-zero if any window's
9285 hscroll has been changed. If it has, desired matrices on the frame
9286 of WINDOW are cleared. */
9287
9288 static int
9289 hscroll_windows (window)
9290 Lisp_Object window;
9291 {
9292 int hscrolled_p;
9293
9294 if (automatic_hscrolling_p)
9295 {
9296 hscrolled_p = hscroll_window_tree (window);
9297 if (hscrolled_p)
9298 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9299 }
9300 else
9301 hscrolled_p = 0;
9302 return hscrolled_p;
9303 }
9304
9305
9306 \f
9307 /************************************************************************
9308 Redisplay
9309 ************************************************************************/
9310
9311 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9312 to a non-zero value. This is sometimes handy to have in a debugger
9313 session. */
9314
9315 #if GLYPH_DEBUG
9316
9317 /* First and last unchanged row for try_window_id. */
9318
9319 int debug_first_unchanged_at_end_vpos;
9320 int debug_last_unchanged_at_beg_vpos;
9321
9322 /* Delta vpos and y. */
9323
9324 int debug_dvpos, debug_dy;
9325
9326 /* Delta in characters and bytes for try_window_id. */
9327
9328 int debug_delta, debug_delta_bytes;
9329
9330 /* Values of window_end_pos and window_end_vpos at the end of
9331 try_window_id. */
9332
9333 EMACS_INT debug_end_pos, debug_end_vpos;
9334
9335 /* Append a string to W->desired_matrix->method. FMT is a printf
9336 format string. A1...A9 are a supplement for a variable-length
9337 argument list. If trace_redisplay_p is non-zero also printf the
9338 resulting string to stderr. */
9339
9340 static void
9341 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9342 struct window *w;
9343 char *fmt;
9344 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9345 {
9346 char buffer[512];
9347 char *method = w->desired_matrix->method;
9348 int len = strlen (method);
9349 int size = sizeof w->desired_matrix->method;
9350 int remaining = size - len - 1;
9351
9352 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9353 if (len && remaining)
9354 {
9355 method[len] = '|';
9356 --remaining, ++len;
9357 }
9358
9359 strncpy (method + len, buffer, remaining);
9360
9361 if (trace_redisplay_p)
9362 fprintf (stderr, "%p (%s): %s\n",
9363 w,
9364 ((BUFFERP (w->buffer)
9365 && STRINGP (XBUFFER (w->buffer)->name))
9366 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9367 : "no buffer"),
9368 buffer);
9369 }
9370
9371 #endif /* GLYPH_DEBUG */
9372
9373
9374 /* Value is non-zero if all changes in window W, which displays
9375 current_buffer, are in the text between START and END. START is a
9376 buffer position, END is given as a distance from Z. Used in
9377 redisplay_internal for display optimization. */
9378
9379 static INLINE int
9380 text_outside_line_unchanged_p (w, start, end)
9381 struct window *w;
9382 int start, end;
9383 {
9384 int unchanged_p = 1;
9385
9386 /* If text or overlays have changed, see where. */
9387 if (XFASTINT (w->last_modified) < MODIFF
9388 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9389 {
9390 /* Gap in the line? */
9391 if (GPT < start || Z - GPT < end)
9392 unchanged_p = 0;
9393
9394 /* Changes start in front of the line, or end after it? */
9395 if (unchanged_p
9396 && (BEG_UNCHANGED < start - 1
9397 || END_UNCHANGED < end))
9398 unchanged_p = 0;
9399
9400 /* If selective display, can't optimize if changes start at the
9401 beginning of the line. */
9402 if (unchanged_p
9403 && INTEGERP (current_buffer->selective_display)
9404 && XINT (current_buffer->selective_display) > 0
9405 && (BEG_UNCHANGED < start || GPT <= start))
9406 unchanged_p = 0;
9407
9408 /* If there are overlays at the start or end of the line, these
9409 may have overlay strings with newlines in them. A change at
9410 START, for instance, may actually concern the display of such
9411 overlay strings as well, and they are displayed on different
9412 lines. So, quickly rule out this case. (For the future, it
9413 might be desirable to implement something more telling than
9414 just BEG/END_UNCHANGED.) */
9415 if (unchanged_p)
9416 {
9417 if (BEG + BEG_UNCHANGED == start
9418 && overlay_touches_p (start))
9419 unchanged_p = 0;
9420 if (END_UNCHANGED == end
9421 && overlay_touches_p (Z - end))
9422 unchanged_p = 0;
9423 }
9424 }
9425
9426 return unchanged_p;
9427 }
9428
9429
9430 /* Do a frame update, taking possible shortcuts into account. This is
9431 the main external entry point for redisplay.
9432
9433 If the last redisplay displayed an echo area message and that message
9434 is no longer requested, we clear the echo area or bring back the
9435 mini-buffer if that is in use. */
9436
9437 void
9438 redisplay ()
9439 {
9440 redisplay_internal (0);
9441 }
9442
9443
9444 static Lisp_Object
9445 overlay_arrow_string_or_property (var, pbitmap)
9446 Lisp_Object var;
9447 int *pbitmap;
9448 {
9449 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9450 Lisp_Object bitmap;
9451
9452 if (pbitmap)
9453 {
9454 *pbitmap = 0;
9455 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9456 *pbitmap = XINT (bitmap);
9457 }
9458
9459 if (!NILP (pstr))
9460 return pstr;
9461 return Voverlay_arrow_string;
9462 }
9463
9464 /* Return 1 if there are any overlay-arrows in current_buffer. */
9465 static int
9466 overlay_arrow_in_current_buffer_p ()
9467 {
9468 Lisp_Object vlist;
9469
9470 for (vlist = Voverlay_arrow_variable_list;
9471 CONSP (vlist);
9472 vlist = XCDR (vlist))
9473 {
9474 Lisp_Object var = XCAR (vlist);
9475 Lisp_Object val;
9476
9477 if (!SYMBOLP (var))
9478 continue;
9479 val = find_symbol_value (var);
9480 if (MARKERP (val)
9481 && current_buffer == XMARKER (val)->buffer)
9482 return 1;
9483 }
9484 return 0;
9485 }
9486
9487
9488 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9489 has changed. */
9490
9491 static int
9492 overlay_arrows_changed_p ()
9493 {
9494 Lisp_Object vlist;
9495
9496 for (vlist = Voverlay_arrow_variable_list;
9497 CONSP (vlist);
9498 vlist = XCDR (vlist))
9499 {
9500 Lisp_Object var = XCAR (vlist);
9501 Lisp_Object val, pstr;
9502
9503 if (!SYMBOLP (var))
9504 continue;
9505 val = find_symbol_value (var);
9506 if (!MARKERP (val))
9507 continue;
9508 if (! EQ (COERCE_MARKER (val),
9509 Fget (var, Qlast_arrow_position))
9510 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9511 EQ (pstr, Fget (var, Qlast_arrow_string))))
9512 return 1;
9513 }
9514 return 0;
9515 }
9516
9517 /* Mark overlay arrows to be updated on next redisplay. */
9518
9519 static void
9520 update_overlay_arrows (up_to_date)
9521 int up_to_date;
9522 {
9523 Lisp_Object vlist;
9524
9525 for (vlist = Voverlay_arrow_variable_list;
9526 CONSP (vlist);
9527 vlist = XCDR (vlist))
9528 {
9529 Lisp_Object var = XCAR (vlist);
9530
9531 if (!SYMBOLP (var))
9532 continue;
9533
9534 if (up_to_date > 0)
9535 {
9536 Lisp_Object val = find_symbol_value (var);
9537 Fput (var, Qlast_arrow_position,
9538 COERCE_MARKER (val));
9539 Fput (var, Qlast_arrow_string,
9540 overlay_arrow_string_or_property (var, 0));
9541 }
9542 else if (up_to_date < 0
9543 || !NILP (Fget (var, Qlast_arrow_position)))
9544 {
9545 Fput (var, Qlast_arrow_position, Qt);
9546 Fput (var, Qlast_arrow_string, Qt);
9547 }
9548 }
9549 }
9550
9551
9552 /* Return overlay arrow string to display at row.
9553 Return t if display as bitmap in left fringe.
9554 Return nil if no overlay arrow. */
9555
9556 static Lisp_Object
9557 overlay_arrow_at_row (it, row, pbitmap)
9558 struct it *it;
9559 struct glyph_row *row;
9560 int *pbitmap;
9561 {
9562 Lisp_Object vlist;
9563
9564 for (vlist = Voverlay_arrow_variable_list;
9565 CONSP (vlist);
9566 vlist = XCDR (vlist))
9567 {
9568 Lisp_Object var = XCAR (vlist);
9569 Lisp_Object val;
9570
9571 if (!SYMBOLP (var))
9572 continue;
9573
9574 val = find_symbol_value (var);
9575
9576 if (MARKERP (val)
9577 && current_buffer == XMARKER (val)->buffer
9578 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9579 {
9580 val = overlay_arrow_string_or_property (var, pbitmap);
9581 if (FRAME_WINDOW_P (it->f)
9582 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9583 return Qt;
9584 if (STRINGP (val))
9585 return val;
9586 break;
9587 }
9588 }
9589
9590 *pbitmap = 0;
9591 return Qnil;
9592 }
9593
9594 /* Return 1 if point moved out of or into a composition. Otherwise
9595 return 0. PREV_BUF and PREV_PT are the last point buffer and
9596 position. BUF and PT are the current point buffer and position. */
9597
9598 int
9599 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9600 struct buffer *prev_buf, *buf;
9601 int prev_pt, pt;
9602 {
9603 int start, end;
9604 Lisp_Object prop;
9605 Lisp_Object buffer;
9606
9607 XSETBUFFER (buffer, buf);
9608 /* Check a composition at the last point if point moved within the
9609 same buffer. */
9610 if (prev_buf == buf)
9611 {
9612 if (prev_pt == pt)
9613 /* Point didn't move. */
9614 return 0;
9615
9616 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9617 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9618 && COMPOSITION_VALID_P (start, end, prop)
9619 && start < prev_pt && end > prev_pt)
9620 /* The last point was within the composition. Return 1 iff
9621 point moved out of the composition. */
9622 return (pt <= start || pt >= end);
9623 }
9624
9625 /* Check a composition at the current point. */
9626 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9627 && find_composition (pt, -1, &start, &end, &prop, buffer)
9628 && COMPOSITION_VALID_P (start, end, prop)
9629 && start < pt && end > pt);
9630 }
9631
9632
9633 /* Reconsider the setting of B->clip_changed which is displayed
9634 in window W. */
9635
9636 static INLINE void
9637 reconsider_clip_changes (w, b)
9638 struct window *w;
9639 struct buffer *b;
9640 {
9641 if (b->clip_changed
9642 && !NILP (w->window_end_valid)
9643 && w->current_matrix->buffer == b
9644 && w->current_matrix->zv == BUF_ZV (b)
9645 && w->current_matrix->begv == BUF_BEGV (b))
9646 b->clip_changed = 0;
9647
9648 /* If display wasn't paused, and W is not a tool bar window, see if
9649 point has been moved into or out of a composition. In that case,
9650 we set b->clip_changed to 1 to force updating the screen. If
9651 b->clip_changed has already been set to 1, we can skip this
9652 check. */
9653 if (!b->clip_changed
9654 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9655 {
9656 int pt;
9657
9658 if (w == XWINDOW (selected_window))
9659 pt = BUF_PT (current_buffer);
9660 else
9661 pt = marker_position (w->pointm);
9662
9663 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9664 || pt != XINT (w->last_point))
9665 && check_point_in_composition (w->current_matrix->buffer,
9666 XINT (w->last_point),
9667 XBUFFER (w->buffer), pt))
9668 b->clip_changed = 1;
9669 }
9670 }
9671 \f
9672
9673 /* Select FRAME to forward the values of frame-local variables into C
9674 variables so that the redisplay routines can access those values
9675 directly. */
9676
9677 static void
9678 select_frame_for_redisplay (frame)
9679 Lisp_Object frame;
9680 {
9681 Lisp_Object tail, sym, val;
9682 Lisp_Object old = selected_frame;
9683
9684 selected_frame = frame;
9685
9686 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9687 if (CONSP (XCAR (tail))
9688 && (sym = XCAR (XCAR (tail)),
9689 SYMBOLP (sym))
9690 && (sym = indirect_variable (sym),
9691 val = SYMBOL_VALUE (sym),
9692 (BUFFER_LOCAL_VALUEP (val)
9693 || SOME_BUFFER_LOCAL_VALUEP (val)))
9694 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9695 Fsymbol_value (sym);
9696
9697 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9698 if (CONSP (XCAR (tail))
9699 && (sym = XCAR (XCAR (tail)),
9700 SYMBOLP (sym))
9701 && (sym = indirect_variable (sym),
9702 val = SYMBOL_VALUE (sym),
9703 (BUFFER_LOCAL_VALUEP (val)
9704 || SOME_BUFFER_LOCAL_VALUEP (val)))
9705 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9706 Fsymbol_value (sym);
9707 }
9708
9709
9710 #define STOP_POLLING \
9711 do { if (! polling_stopped_here) stop_polling (); \
9712 polling_stopped_here = 1; } while (0)
9713
9714 #define RESUME_POLLING \
9715 do { if (polling_stopped_here) start_polling (); \
9716 polling_stopped_here = 0; } while (0)
9717
9718
9719 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9720 response to any user action; therefore, we should preserve the echo
9721 area. (Actually, our caller does that job.) Perhaps in the future
9722 avoid recentering windows if it is not necessary; currently that
9723 causes some problems. */
9724
9725 static void
9726 redisplay_internal (preserve_echo_area)
9727 int preserve_echo_area;
9728 {
9729 struct window *w = XWINDOW (selected_window);
9730 struct frame *f = XFRAME (w->frame);
9731 int pause;
9732 int must_finish = 0;
9733 struct text_pos tlbufpos, tlendpos;
9734 int number_of_visible_frames;
9735 int count;
9736 struct frame *sf = SELECTED_FRAME ();
9737 int polling_stopped_here = 0;
9738
9739 /* Non-zero means redisplay has to consider all windows on all
9740 frames. Zero means, only selected_window is considered. */
9741 int consider_all_windows_p;
9742
9743 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9744
9745 /* No redisplay if running in batch mode or frame is not yet fully
9746 initialized, or redisplay is explicitly turned off by setting
9747 Vinhibit_redisplay. */
9748 if (noninteractive
9749 || !NILP (Vinhibit_redisplay)
9750 || !f->glyphs_initialized_p)
9751 return;
9752
9753 /* The flag redisplay_performed_directly_p is set by
9754 direct_output_for_insert when it already did the whole screen
9755 update necessary. */
9756 if (redisplay_performed_directly_p)
9757 {
9758 redisplay_performed_directly_p = 0;
9759 if (!hscroll_windows (selected_window))
9760 return;
9761 }
9762
9763 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9764 if (popup_activated ())
9765 return;
9766 #endif
9767
9768 /* I don't think this happens but let's be paranoid. */
9769 if (redisplaying_p)
9770 return;
9771
9772 /* Record a function that resets redisplaying_p to its old value
9773 when we leave this function. */
9774 count = SPECPDL_INDEX ();
9775 record_unwind_protect (unwind_redisplay,
9776 Fcons (make_number (redisplaying_p), selected_frame));
9777 ++redisplaying_p;
9778 specbind (Qinhibit_free_realized_faces, Qnil);
9779
9780 retry:
9781 pause = 0;
9782 reconsider_clip_changes (w, current_buffer);
9783
9784 /* If new fonts have been loaded that make a glyph matrix adjustment
9785 necessary, do it. */
9786 if (fonts_changed_p)
9787 {
9788 adjust_glyphs (NULL);
9789 ++windows_or_buffers_changed;
9790 fonts_changed_p = 0;
9791 }
9792
9793 /* If face_change_count is non-zero, init_iterator will free all
9794 realized faces, which includes the faces referenced from current
9795 matrices. So, we can't reuse current matrices in this case. */
9796 if (face_change_count)
9797 ++windows_or_buffers_changed;
9798
9799 if (! FRAME_WINDOW_P (sf)
9800 && previous_terminal_frame != sf)
9801 {
9802 /* Since frames on an ASCII terminal share the same display
9803 area, displaying a different frame means redisplay the whole
9804 thing. */
9805 windows_or_buffers_changed++;
9806 SET_FRAME_GARBAGED (sf);
9807 XSETFRAME (Vterminal_frame, sf);
9808 }
9809 previous_terminal_frame = sf;
9810
9811 /* Set the visible flags for all frames. Do this before checking
9812 for resized or garbaged frames; they want to know if their frames
9813 are visible. See the comment in frame.h for
9814 FRAME_SAMPLE_VISIBILITY. */
9815 {
9816 Lisp_Object tail, frame;
9817
9818 number_of_visible_frames = 0;
9819
9820 FOR_EACH_FRAME (tail, frame)
9821 {
9822 struct frame *f = XFRAME (frame);
9823
9824 FRAME_SAMPLE_VISIBILITY (f);
9825 if (FRAME_VISIBLE_P (f))
9826 ++number_of_visible_frames;
9827 clear_desired_matrices (f);
9828 }
9829 }
9830
9831 /* Notice any pending interrupt request to change frame size. */
9832 do_pending_window_change (1);
9833
9834 /* Clear frames marked as garbaged. */
9835 if (frame_garbaged)
9836 clear_garbaged_frames ();
9837
9838 /* Build menubar and tool-bar items. */
9839 prepare_menu_bars ();
9840
9841 if (windows_or_buffers_changed)
9842 update_mode_lines++;
9843
9844 /* Detect case that we need to write or remove a star in the mode line. */
9845 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9846 {
9847 w->update_mode_line = Qt;
9848 if (buffer_shared > 1)
9849 update_mode_lines++;
9850 }
9851
9852 /* If %c is in the mode line, update it if needed. */
9853 if (!NILP (w->column_number_displayed)
9854 /* This alternative quickly identifies a common case
9855 where no change is needed. */
9856 && !(PT == XFASTINT (w->last_point)
9857 && XFASTINT (w->last_modified) >= MODIFF
9858 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9859 && (XFASTINT (w->column_number_displayed)
9860 != (int) current_column ())) /* iftc */
9861 w->update_mode_line = Qt;
9862
9863 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9864
9865 /* The variable buffer_shared is set in redisplay_window and
9866 indicates that we redisplay a buffer in different windows. See
9867 there. */
9868 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9869 || cursor_type_changed);
9870
9871 /* If specs for an arrow have changed, do thorough redisplay
9872 to ensure we remove any arrow that should no longer exist. */
9873 if (overlay_arrows_changed_p ())
9874 consider_all_windows_p = windows_or_buffers_changed = 1;
9875
9876 /* Normally the message* functions will have already displayed and
9877 updated the echo area, but the frame may have been trashed, or
9878 the update may have been preempted, so display the echo area
9879 again here. Checking message_cleared_p captures the case that
9880 the echo area should be cleared. */
9881 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9882 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9883 || (message_cleared_p
9884 && minibuf_level == 0
9885 /* If the mini-window is currently selected, this means the
9886 echo-area doesn't show through. */
9887 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9888 {
9889 int window_height_changed_p = echo_area_display (0);
9890 must_finish = 1;
9891
9892 /* If we don't display the current message, don't clear the
9893 message_cleared_p flag, because, if we did, we wouldn't clear
9894 the echo area in the next redisplay which doesn't preserve
9895 the echo area. */
9896 if (!display_last_displayed_message_p)
9897 message_cleared_p = 0;
9898
9899 if (fonts_changed_p)
9900 goto retry;
9901 else if (window_height_changed_p)
9902 {
9903 consider_all_windows_p = 1;
9904 ++update_mode_lines;
9905 ++windows_or_buffers_changed;
9906
9907 /* If window configuration was changed, frames may have been
9908 marked garbaged. Clear them or we will experience
9909 surprises wrt scrolling. */
9910 if (frame_garbaged)
9911 clear_garbaged_frames ();
9912 }
9913 }
9914 else if (EQ (selected_window, minibuf_window)
9915 && (current_buffer->clip_changed
9916 || XFASTINT (w->last_modified) < MODIFF
9917 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9918 && resize_mini_window (w, 0))
9919 {
9920 /* Resized active mini-window to fit the size of what it is
9921 showing if its contents might have changed. */
9922 must_finish = 1;
9923 consider_all_windows_p = 1;
9924 ++windows_or_buffers_changed;
9925 ++update_mode_lines;
9926
9927 /* If window configuration was changed, frames may have been
9928 marked garbaged. Clear them or we will experience
9929 surprises wrt scrolling. */
9930 if (frame_garbaged)
9931 clear_garbaged_frames ();
9932 }
9933
9934
9935 /* If showing the region, and mark has changed, we must redisplay
9936 the whole window. The assignment to this_line_start_pos prevents
9937 the optimization directly below this if-statement. */
9938 if (((!NILP (Vtransient_mark_mode)
9939 && !NILP (XBUFFER (w->buffer)->mark_active))
9940 != !NILP (w->region_showing))
9941 || (!NILP (w->region_showing)
9942 && !EQ (w->region_showing,
9943 Fmarker_position (XBUFFER (w->buffer)->mark))))
9944 CHARPOS (this_line_start_pos) = 0;
9945
9946 /* Optimize the case that only the line containing the cursor in the
9947 selected window has changed. Variables starting with this_ are
9948 set in display_line and record information about the line
9949 containing the cursor. */
9950 tlbufpos = this_line_start_pos;
9951 tlendpos = this_line_end_pos;
9952 if (!consider_all_windows_p
9953 && CHARPOS (tlbufpos) > 0
9954 && NILP (w->update_mode_line)
9955 && !current_buffer->clip_changed
9956 && !current_buffer->prevent_redisplay_optimizations_p
9957 && FRAME_VISIBLE_P (XFRAME (w->frame))
9958 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9959 /* Make sure recorded data applies to current buffer, etc. */
9960 && this_line_buffer == current_buffer
9961 && current_buffer == XBUFFER (w->buffer)
9962 && NILP (w->force_start)
9963 && NILP (w->optional_new_start)
9964 /* Point must be on the line that we have info recorded about. */
9965 && PT >= CHARPOS (tlbufpos)
9966 && PT <= Z - CHARPOS (tlendpos)
9967 /* All text outside that line, including its final newline,
9968 must be unchanged */
9969 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9970 CHARPOS (tlendpos)))
9971 {
9972 if (CHARPOS (tlbufpos) > BEGV
9973 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9974 && (CHARPOS (tlbufpos) == ZV
9975 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9976 /* Former continuation line has disappeared by becoming empty */
9977 goto cancel;
9978 else if (XFASTINT (w->last_modified) < MODIFF
9979 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9980 || MINI_WINDOW_P (w))
9981 {
9982 /* We have to handle the case of continuation around a
9983 wide-column character (See the comment in indent.c around
9984 line 885).
9985
9986 For instance, in the following case:
9987
9988 -------- Insert --------
9989 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9990 J_I_ ==> J_I_ `^^' are cursors.
9991 ^^ ^^
9992 -------- --------
9993
9994 As we have to redraw the line above, we should goto cancel. */
9995
9996 struct it it;
9997 int line_height_before = this_line_pixel_height;
9998
9999 /* Note that start_display will handle the case that the
10000 line starting at tlbufpos is a continuation lines. */
10001 start_display (&it, w, tlbufpos);
10002
10003 /* Implementation note: It this still necessary? */
10004 if (it.current_x != this_line_start_x)
10005 goto cancel;
10006
10007 TRACE ((stderr, "trying display optimization 1\n"));
10008 w->cursor.vpos = -1;
10009 overlay_arrow_seen = 0;
10010 it.vpos = this_line_vpos;
10011 it.current_y = this_line_y;
10012 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10013 display_line (&it);
10014
10015 /* If line contains point, is not continued,
10016 and ends at same distance from eob as before, we win */
10017 if (w->cursor.vpos >= 0
10018 /* Line is not continued, otherwise this_line_start_pos
10019 would have been set to 0 in display_line. */
10020 && CHARPOS (this_line_start_pos)
10021 /* Line ends as before. */
10022 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10023 /* Line has same height as before. Otherwise other lines
10024 would have to be shifted up or down. */
10025 && this_line_pixel_height == line_height_before)
10026 {
10027 /* If this is not the window's last line, we must adjust
10028 the charstarts of the lines below. */
10029 if (it.current_y < it.last_visible_y)
10030 {
10031 struct glyph_row *row
10032 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10033 int delta, delta_bytes;
10034
10035 if (Z - CHARPOS (tlendpos) == ZV)
10036 {
10037 /* This line ends at end of (accessible part of)
10038 buffer. There is no newline to count. */
10039 delta = (Z
10040 - CHARPOS (tlendpos)
10041 - MATRIX_ROW_START_CHARPOS (row));
10042 delta_bytes = (Z_BYTE
10043 - BYTEPOS (tlendpos)
10044 - MATRIX_ROW_START_BYTEPOS (row));
10045 }
10046 else
10047 {
10048 /* This line ends in a newline. Must take
10049 account of the newline and the rest of the
10050 text that follows. */
10051 delta = (Z
10052 - CHARPOS (tlendpos)
10053 - MATRIX_ROW_START_CHARPOS (row));
10054 delta_bytes = (Z_BYTE
10055 - BYTEPOS (tlendpos)
10056 - MATRIX_ROW_START_BYTEPOS (row));
10057 }
10058
10059 increment_matrix_positions (w->current_matrix,
10060 this_line_vpos + 1,
10061 w->current_matrix->nrows,
10062 delta, delta_bytes);
10063 }
10064
10065 /* If this row displays text now but previously didn't,
10066 or vice versa, w->window_end_vpos may have to be
10067 adjusted. */
10068 if ((it.glyph_row - 1)->displays_text_p)
10069 {
10070 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10071 XSETINT (w->window_end_vpos, this_line_vpos);
10072 }
10073 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10074 && this_line_vpos > 0)
10075 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10076 w->window_end_valid = Qnil;
10077
10078 /* Update hint: No need to try to scroll in update_window. */
10079 w->desired_matrix->no_scrolling_p = 1;
10080
10081 #if GLYPH_DEBUG
10082 *w->desired_matrix->method = 0;
10083 debug_method_add (w, "optimization 1");
10084 #endif
10085 #ifdef HAVE_WINDOW_SYSTEM
10086 update_window_fringes (w, 0);
10087 #endif
10088 goto update;
10089 }
10090 else
10091 goto cancel;
10092 }
10093 else if (/* Cursor position hasn't changed. */
10094 PT == XFASTINT (w->last_point)
10095 /* Make sure the cursor was last displayed
10096 in this window. Otherwise we have to reposition it. */
10097 && 0 <= w->cursor.vpos
10098 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10099 {
10100 if (!must_finish)
10101 {
10102 do_pending_window_change (1);
10103
10104 /* We used to always goto end_of_redisplay here, but this
10105 isn't enough if we have a blinking cursor. */
10106 if (w->cursor_off_p == w->last_cursor_off_p)
10107 goto end_of_redisplay;
10108 }
10109 goto update;
10110 }
10111 /* If highlighting the region, or if the cursor is in the echo area,
10112 then we can't just move the cursor. */
10113 else if (! (!NILP (Vtransient_mark_mode)
10114 && !NILP (current_buffer->mark_active))
10115 && (EQ (selected_window, current_buffer->last_selected_window)
10116 || highlight_nonselected_windows)
10117 && NILP (w->region_showing)
10118 && NILP (Vshow_trailing_whitespace)
10119 && !cursor_in_echo_area)
10120 {
10121 struct it it;
10122 struct glyph_row *row;
10123
10124 /* Skip from tlbufpos to PT and see where it is. Note that
10125 PT may be in invisible text. If so, we will end at the
10126 next visible position. */
10127 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10128 NULL, DEFAULT_FACE_ID);
10129 it.current_x = this_line_start_x;
10130 it.current_y = this_line_y;
10131 it.vpos = this_line_vpos;
10132
10133 /* The call to move_it_to stops in front of PT, but
10134 moves over before-strings. */
10135 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10136
10137 if (it.vpos == this_line_vpos
10138 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10139 row->enabled_p))
10140 {
10141 xassert (this_line_vpos == it.vpos);
10142 xassert (this_line_y == it.current_y);
10143 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10144 #if GLYPH_DEBUG
10145 *w->desired_matrix->method = 0;
10146 debug_method_add (w, "optimization 3");
10147 #endif
10148 goto update;
10149 }
10150 else
10151 goto cancel;
10152 }
10153
10154 cancel:
10155 /* Text changed drastically or point moved off of line. */
10156 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10157 }
10158
10159 CHARPOS (this_line_start_pos) = 0;
10160 consider_all_windows_p |= buffer_shared > 1;
10161 ++clear_face_cache_count;
10162
10163
10164 /* Build desired matrices, and update the display. If
10165 consider_all_windows_p is non-zero, do it for all windows on all
10166 frames. Otherwise do it for selected_window, only. */
10167
10168 if (consider_all_windows_p)
10169 {
10170 Lisp_Object tail, frame;
10171 int i, n = 0, size = 50;
10172 struct frame **updated
10173 = (struct frame **) alloca (size * sizeof *updated);
10174
10175 /* Clear the face cache eventually. */
10176 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10177 {
10178 clear_face_cache (0);
10179 clear_face_cache_count = 0;
10180 }
10181
10182 /* Recompute # windows showing selected buffer. This will be
10183 incremented each time such a window is displayed. */
10184 buffer_shared = 0;
10185
10186 FOR_EACH_FRAME (tail, frame)
10187 {
10188 struct frame *f = XFRAME (frame);
10189
10190 if (FRAME_WINDOW_P (f) || f == sf)
10191 {
10192 if (! EQ (frame, selected_frame))
10193 /* Select the frame, for the sake of frame-local
10194 variables. */
10195 select_frame_for_redisplay (frame);
10196
10197 #ifdef HAVE_WINDOW_SYSTEM
10198 if (clear_face_cache_count % 50 == 0
10199 && FRAME_WINDOW_P (f))
10200 clear_image_cache (f, 0);
10201 #endif /* HAVE_WINDOW_SYSTEM */
10202
10203 /* Mark all the scroll bars to be removed; we'll redeem
10204 the ones we want when we redisplay their windows. */
10205 if (condemn_scroll_bars_hook)
10206 condemn_scroll_bars_hook (f);
10207
10208 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10209 redisplay_windows (FRAME_ROOT_WINDOW (f));
10210
10211 /* Any scroll bars which redisplay_windows should have
10212 nuked should now go away. */
10213 if (judge_scroll_bars_hook)
10214 judge_scroll_bars_hook (f);
10215
10216 /* If fonts changed, display again. */
10217 /* ??? rms: I suspect it is a mistake to jump all the way
10218 back to retry here. It should just retry this frame. */
10219 if (fonts_changed_p)
10220 goto retry;
10221
10222 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10223 {
10224 /* See if we have to hscroll. */
10225 if (hscroll_windows (f->root_window))
10226 goto retry;
10227
10228 /* Prevent various kinds of signals during display
10229 update. stdio is not robust about handling
10230 signals, which can cause an apparent I/O
10231 error. */
10232 if (interrupt_input)
10233 unrequest_sigio ();
10234 STOP_POLLING;
10235
10236 /* Update the display. */
10237 set_window_update_flags (XWINDOW (f->root_window), 1);
10238 pause |= update_frame (f, 0, 0);
10239 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10240 if (pause)
10241 break;
10242 #endif
10243
10244 if (n == size)
10245 {
10246 int nbytes = size * sizeof *updated;
10247 struct frame **p = (struct frame **) alloca (2 * nbytes);
10248 bcopy (updated, p, nbytes);
10249 size *= 2;
10250 }
10251
10252 updated[n++] = f;
10253 }
10254 }
10255 }
10256
10257 if (!pause)
10258 {
10259 /* Do the mark_window_display_accurate after all windows have
10260 been redisplayed because this call resets flags in buffers
10261 which are needed for proper redisplay. */
10262 for (i = 0; i < n; ++i)
10263 {
10264 struct frame *f = updated[i];
10265 mark_window_display_accurate (f->root_window, 1);
10266 if (frame_up_to_date_hook)
10267 frame_up_to_date_hook (f);
10268 }
10269 }
10270 }
10271 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10272 {
10273 Lisp_Object mini_window;
10274 struct frame *mini_frame;
10275
10276 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10277 /* Use list_of_error, not Qerror, so that
10278 we catch only errors and don't run the debugger. */
10279 internal_condition_case_1 (redisplay_window_1, selected_window,
10280 list_of_error,
10281 redisplay_window_error);
10282
10283 /* Compare desired and current matrices, perform output. */
10284
10285 update:
10286 /* If fonts changed, display again. */
10287 if (fonts_changed_p)
10288 goto retry;
10289
10290 /* Prevent various kinds of signals during display update.
10291 stdio is not robust about handling signals,
10292 which can cause an apparent I/O error. */
10293 if (interrupt_input)
10294 unrequest_sigio ();
10295 STOP_POLLING;
10296
10297 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10298 {
10299 if (hscroll_windows (selected_window))
10300 goto retry;
10301
10302 XWINDOW (selected_window)->must_be_updated_p = 1;
10303 pause = update_frame (sf, 0, 0);
10304 }
10305
10306 /* We may have called echo_area_display at the top of this
10307 function. If the echo area is on another frame, that may
10308 have put text on a frame other than the selected one, so the
10309 above call to update_frame would not have caught it. Catch
10310 it here. */
10311 mini_window = FRAME_MINIBUF_WINDOW (sf);
10312 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10313
10314 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10315 {
10316 XWINDOW (mini_window)->must_be_updated_p = 1;
10317 pause |= update_frame (mini_frame, 0, 0);
10318 if (!pause && hscroll_windows (mini_window))
10319 goto retry;
10320 }
10321 }
10322
10323 /* If display was paused because of pending input, make sure we do a
10324 thorough update the next time. */
10325 if (pause)
10326 {
10327 /* Prevent the optimization at the beginning of
10328 redisplay_internal that tries a single-line update of the
10329 line containing the cursor in the selected window. */
10330 CHARPOS (this_line_start_pos) = 0;
10331
10332 /* Let the overlay arrow be updated the next time. */
10333 update_overlay_arrows (0);
10334
10335 /* If we pause after scrolling, some rows in the current
10336 matrices of some windows are not valid. */
10337 if (!WINDOW_FULL_WIDTH_P (w)
10338 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10339 update_mode_lines = 1;
10340 }
10341 else
10342 {
10343 if (!consider_all_windows_p)
10344 {
10345 /* This has already been done above if
10346 consider_all_windows_p is set. */
10347 mark_window_display_accurate_1 (w, 1);
10348
10349 /* Say overlay arrows are up to date. */
10350 update_overlay_arrows (1);
10351
10352 if (frame_up_to_date_hook != 0)
10353 frame_up_to_date_hook (sf);
10354 }
10355
10356 update_mode_lines = 0;
10357 windows_or_buffers_changed = 0;
10358 cursor_type_changed = 0;
10359 }
10360
10361 /* Start SIGIO interrupts coming again. Having them off during the
10362 code above makes it less likely one will discard output, but not
10363 impossible, since there might be stuff in the system buffer here.
10364 But it is much hairier to try to do anything about that. */
10365 if (interrupt_input)
10366 request_sigio ();
10367 RESUME_POLLING;
10368
10369 /* If a frame has become visible which was not before, redisplay
10370 again, so that we display it. Expose events for such a frame
10371 (which it gets when becoming visible) don't call the parts of
10372 redisplay constructing glyphs, so simply exposing a frame won't
10373 display anything in this case. So, we have to display these
10374 frames here explicitly. */
10375 if (!pause)
10376 {
10377 Lisp_Object tail, frame;
10378 int new_count = 0;
10379
10380 FOR_EACH_FRAME (tail, frame)
10381 {
10382 int this_is_visible = 0;
10383
10384 if (XFRAME (frame)->visible)
10385 this_is_visible = 1;
10386 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10387 if (XFRAME (frame)->visible)
10388 this_is_visible = 1;
10389
10390 if (this_is_visible)
10391 new_count++;
10392 }
10393
10394 if (new_count != number_of_visible_frames)
10395 windows_or_buffers_changed++;
10396 }
10397
10398 /* Change frame size now if a change is pending. */
10399 do_pending_window_change (1);
10400
10401 /* If we just did a pending size change, or have additional
10402 visible frames, redisplay again. */
10403 if (windows_or_buffers_changed && !pause)
10404 goto retry;
10405
10406 end_of_redisplay:
10407 unbind_to (count, Qnil);
10408 RESUME_POLLING;
10409 }
10410
10411
10412 /* Redisplay, but leave alone any recent echo area message unless
10413 another message has been requested in its place.
10414
10415 This is useful in situations where you need to redisplay but no
10416 user action has occurred, making it inappropriate for the message
10417 area to be cleared. See tracking_off and
10418 wait_reading_process_output for examples of these situations.
10419
10420 FROM_WHERE is an integer saying from where this function was
10421 called. This is useful for debugging. */
10422
10423 void
10424 redisplay_preserve_echo_area (from_where)
10425 int from_where;
10426 {
10427 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10428
10429 if (!NILP (echo_area_buffer[1]))
10430 {
10431 /* We have a previously displayed message, but no current
10432 message. Redisplay the previous message. */
10433 display_last_displayed_message_p = 1;
10434 redisplay_internal (1);
10435 display_last_displayed_message_p = 0;
10436 }
10437 else
10438 redisplay_internal (1);
10439
10440 if (rif != NULL && rif->flush_display_optional)
10441 rif->flush_display_optional (NULL);
10442 }
10443
10444
10445 /* Function registered with record_unwind_protect in
10446 redisplay_internal. Reset redisplaying_p to the value it had
10447 before redisplay_internal was called, and clear
10448 prevent_freeing_realized_faces_p. It also selects the previously
10449 selected frame. */
10450
10451 static Lisp_Object
10452 unwind_redisplay (val)
10453 Lisp_Object val;
10454 {
10455 Lisp_Object old_redisplaying_p, old_frame;
10456
10457 old_redisplaying_p = XCAR (val);
10458 redisplaying_p = XFASTINT (old_redisplaying_p);
10459 old_frame = XCDR (val);
10460 if (! EQ (old_frame, selected_frame))
10461 select_frame_for_redisplay (old_frame);
10462 return Qnil;
10463 }
10464
10465
10466 /* Mark the display of window W as accurate or inaccurate. If
10467 ACCURATE_P is non-zero mark display of W as accurate. If
10468 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10469 redisplay_internal is called. */
10470
10471 static void
10472 mark_window_display_accurate_1 (w, accurate_p)
10473 struct window *w;
10474 int accurate_p;
10475 {
10476 if (BUFFERP (w->buffer))
10477 {
10478 struct buffer *b = XBUFFER (w->buffer);
10479
10480 w->last_modified
10481 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10482 w->last_overlay_modified
10483 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10484 w->last_had_star
10485 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10486
10487 if (accurate_p)
10488 {
10489 b->clip_changed = 0;
10490 b->prevent_redisplay_optimizations_p = 0;
10491
10492 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10493 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10494 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10495 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10496
10497 w->current_matrix->buffer = b;
10498 w->current_matrix->begv = BUF_BEGV (b);
10499 w->current_matrix->zv = BUF_ZV (b);
10500
10501 w->last_cursor = w->cursor;
10502 w->last_cursor_off_p = w->cursor_off_p;
10503
10504 if (w == XWINDOW (selected_window))
10505 w->last_point = make_number (BUF_PT (b));
10506 else
10507 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10508 }
10509 }
10510
10511 if (accurate_p)
10512 {
10513 w->window_end_valid = w->buffer;
10514 #if 0 /* This is incorrect with variable-height lines. */
10515 xassert (XINT (w->window_end_vpos)
10516 < (WINDOW_TOTAL_LINES (w)
10517 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10518 #endif
10519 w->update_mode_line = Qnil;
10520 }
10521 }
10522
10523
10524 /* Mark the display of windows in the window tree rooted at WINDOW as
10525 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10526 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10527 be redisplayed the next time redisplay_internal is called. */
10528
10529 void
10530 mark_window_display_accurate (window, accurate_p)
10531 Lisp_Object window;
10532 int accurate_p;
10533 {
10534 struct window *w;
10535
10536 for (; !NILP (window); window = w->next)
10537 {
10538 w = XWINDOW (window);
10539 mark_window_display_accurate_1 (w, accurate_p);
10540
10541 if (!NILP (w->vchild))
10542 mark_window_display_accurate (w->vchild, accurate_p);
10543 if (!NILP (w->hchild))
10544 mark_window_display_accurate (w->hchild, accurate_p);
10545 }
10546
10547 if (accurate_p)
10548 {
10549 update_overlay_arrows (1);
10550 }
10551 else
10552 {
10553 /* Force a thorough redisplay the next time by setting
10554 last_arrow_position and last_arrow_string to t, which is
10555 unequal to any useful value of Voverlay_arrow_... */
10556 update_overlay_arrows (-1);
10557 }
10558 }
10559
10560
10561 /* Return value in display table DP (Lisp_Char_Table *) for character
10562 C. Since a display table doesn't have any parent, we don't have to
10563 follow parent. Do not call this function directly but use the
10564 macro DISP_CHAR_VECTOR. */
10565
10566 Lisp_Object
10567 disp_char_vector (dp, c)
10568 struct Lisp_Char_Table *dp;
10569 int c;
10570 {
10571 int code[4], i;
10572 Lisp_Object val;
10573
10574 if (SINGLE_BYTE_CHAR_P (c))
10575 return (dp->contents[c]);
10576
10577 SPLIT_CHAR (c, code[0], code[1], code[2]);
10578 if (code[1] < 32)
10579 code[1] = -1;
10580 else if (code[2] < 32)
10581 code[2] = -1;
10582
10583 /* Here, the possible range of code[0] (== charset ID) is
10584 128..max_charset. Since the top level char table contains data
10585 for multibyte characters after 256th element, we must increment
10586 code[0] by 128 to get a correct index. */
10587 code[0] += 128;
10588 code[3] = -1; /* anchor */
10589
10590 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10591 {
10592 val = dp->contents[code[i]];
10593 if (!SUB_CHAR_TABLE_P (val))
10594 return (NILP (val) ? dp->defalt : val);
10595 }
10596
10597 /* Here, val is a sub char table. We return the default value of
10598 it. */
10599 return (dp->defalt);
10600 }
10601
10602
10603 \f
10604 /***********************************************************************
10605 Window Redisplay
10606 ***********************************************************************/
10607
10608 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10609
10610 static void
10611 redisplay_windows (window)
10612 Lisp_Object window;
10613 {
10614 while (!NILP (window))
10615 {
10616 struct window *w = XWINDOW (window);
10617
10618 if (!NILP (w->hchild))
10619 redisplay_windows (w->hchild);
10620 else if (!NILP (w->vchild))
10621 redisplay_windows (w->vchild);
10622 else
10623 {
10624 displayed_buffer = XBUFFER (w->buffer);
10625 /* Use list_of_error, not Qerror, so that
10626 we catch only errors and don't run the debugger. */
10627 internal_condition_case_1 (redisplay_window_0, window,
10628 list_of_error,
10629 redisplay_window_error);
10630 }
10631
10632 window = w->next;
10633 }
10634 }
10635
10636 static Lisp_Object
10637 redisplay_window_error ()
10638 {
10639 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10640 return Qnil;
10641 }
10642
10643 static Lisp_Object
10644 redisplay_window_0 (window)
10645 Lisp_Object window;
10646 {
10647 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10648 redisplay_window (window, 0);
10649 return Qnil;
10650 }
10651
10652 static Lisp_Object
10653 redisplay_window_1 (window)
10654 Lisp_Object window;
10655 {
10656 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10657 redisplay_window (window, 1);
10658 return Qnil;
10659 }
10660 \f
10661
10662 /* Increment GLYPH until it reaches END or CONDITION fails while
10663 adding (GLYPH)->pixel_width to X. */
10664
10665 #define SKIP_GLYPHS(glyph, end, x, condition) \
10666 do \
10667 { \
10668 (x) += (glyph)->pixel_width; \
10669 ++(glyph); \
10670 } \
10671 while ((glyph) < (end) && (condition))
10672
10673
10674 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10675 DELTA is the number of bytes by which positions recorded in ROW
10676 differ from current buffer positions. */
10677
10678 void
10679 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10680 struct window *w;
10681 struct glyph_row *row;
10682 struct glyph_matrix *matrix;
10683 int delta, delta_bytes, dy, dvpos;
10684 {
10685 struct glyph *glyph = row->glyphs[TEXT_AREA];
10686 struct glyph *end = glyph + row->used[TEXT_AREA];
10687 struct glyph *cursor = NULL;
10688 /* The first glyph that starts a sequence of glyphs from string. */
10689 struct glyph *string_start;
10690 /* The X coordinate of string_start. */
10691 int string_start_x;
10692 /* The last known character position. */
10693 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10694 /* The last known character position before string_start. */
10695 int string_before_pos;
10696 int x = row->x;
10697 int cursor_x = x;
10698 int cursor_from_overlay_pos = 0;
10699 int pt_old = PT - delta;
10700
10701 /* Skip over glyphs not having an object at the start of the row.
10702 These are special glyphs like truncation marks on terminal
10703 frames. */
10704 if (row->displays_text_p)
10705 while (glyph < end
10706 && INTEGERP (glyph->object)
10707 && glyph->charpos < 0)
10708 {
10709 x += glyph->pixel_width;
10710 ++glyph;
10711 }
10712
10713 string_start = NULL;
10714 while (glyph < end
10715 && !INTEGERP (glyph->object)
10716 && (!BUFFERP (glyph->object)
10717 || (last_pos = glyph->charpos) < pt_old))
10718 {
10719 if (! STRINGP (glyph->object))
10720 {
10721 string_start = NULL;
10722 x += glyph->pixel_width;
10723 ++glyph;
10724 if (cursor_from_overlay_pos
10725 && last_pos > cursor_from_overlay_pos)
10726 {
10727 cursor_from_overlay_pos = 0;
10728 cursor = 0;
10729 }
10730 }
10731 else
10732 {
10733 string_before_pos = last_pos;
10734 string_start = glyph;
10735 string_start_x = x;
10736 /* Skip all glyphs from string. */
10737 do
10738 {
10739 int pos;
10740 if ((cursor == NULL || glyph > cursor)
10741 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10742 Qcursor, (glyph)->object))
10743 && (pos = string_buffer_position (w, glyph->object,
10744 string_before_pos),
10745 (pos == 0 /* From overlay */
10746 || pos == pt_old)))
10747 {
10748 /* Estimate overlay buffer position from the buffer
10749 positions of the glyphs before and after the overlay.
10750 Add 1 to last_pos so that if point corresponds to the
10751 glyph right after the overlay, we still use a 'cursor'
10752 property found in that overlay. */
10753 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10754 cursor = glyph;
10755 cursor_x = x;
10756 }
10757 x += glyph->pixel_width;
10758 ++glyph;
10759 }
10760 while (glyph < end && STRINGP (glyph->object));
10761 }
10762 }
10763
10764 if (cursor != NULL)
10765 {
10766 glyph = cursor;
10767 x = cursor_x;
10768 }
10769 else if (string_start
10770 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10771 {
10772 /* We may have skipped over point because the previous glyphs
10773 are from string. As there's no easy way to know the
10774 character position of the current glyph, find the correct
10775 glyph on point by scanning from string_start again. */
10776 Lisp_Object limit;
10777 Lisp_Object string;
10778 int pos;
10779
10780 limit = make_number (pt_old + 1);
10781 end = glyph;
10782 glyph = string_start;
10783 x = string_start_x;
10784 string = glyph->object;
10785 pos = string_buffer_position (w, string, string_before_pos);
10786 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10787 because we always put cursor after overlay strings. */
10788 while (pos == 0 && glyph < end)
10789 {
10790 string = glyph->object;
10791 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10792 if (glyph < end)
10793 pos = string_buffer_position (w, glyph->object, string_before_pos);
10794 }
10795
10796 while (glyph < end)
10797 {
10798 pos = XINT (Fnext_single_char_property_change
10799 (make_number (pos), Qdisplay, Qnil, limit));
10800 if (pos > pt_old)
10801 break;
10802 /* Skip glyphs from the same string. */
10803 string = glyph->object;
10804 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10805 /* Skip glyphs from an overlay. */
10806 while (glyph < end
10807 && ! string_buffer_position (w, glyph->object, pos))
10808 {
10809 string = glyph->object;
10810 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10811 }
10812 }
10813 }
10814
10815 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10816 w->cursor.x = x;
10817 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10818 w->cursor.y = row->y + dy;
10819
10820 if (w == XWINDOW (selected_window))
10821 {
10822 if (!row->continued_p
10823 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10824 && row->x == 0)
10825 {
10826 this_line_buffer = XBUFFER (w->buffer);
10827
10828 CHARPOS (this_line_start_pos)
10829 = MATRIX_ROW_START_CHARPOS (row) + delta;
10830 BYTEPOS (this_line_start_pos)
10831 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10832
10833 CHARPOS (this_line_end_pos)
10834 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10835 BYTEPOS (this_line_end_pos)
10836 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10837
10838 this_line_y = w->cursor.y;
10839 this_line_pixel_height = row->height;
10840 this_line_vpos = w->cursor.vpos;
10841 this_line_start_x = row->x;
10842 }
10843 else
10844 CHARPOS (this_line_start_pos) = 0;
10845 }
10846 }
10847
10848
10849 /* Run window scroll functions, if any, for WINDOW with new window
10850 start STARTP. Sets the window start of WINDOW to that position.
10851
10852 We assume that the window's buffer is really current. */
10853
10854 static INLINE struct text_pos
10855 run_window_scroll_functions (window, startp)
10856 Lisp_Object window;
10857 struct text_pos startp;
10858 {
10859 struct window *w = XWINDOW (window);
10860 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10861
10862 if (current_buffer != XBUFFER (w->buffer))
10863 abort ();
10864
10865 if (!NILP (Vwindow_scroll_functions))
10866 {
10867 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10868 make_number (CHARPOS (startp)));
10869 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10870 /* In case the hook functions switch buffers. */
10871 if (current_buffer != XBUFFER (w->buffer))
10872 set_buffer_internal_1 (XBUFFER (w->buffer));
10873 }
10874
10875 return startp;
10876 }
10877
10878
10879 /* Make sure the line containing the cursor is fully visible.
10880 A value of 1 means there is nothing to be done.
10881 (Either the line is fully visible, or it cannot be made so,
10882 or we cannot tell.)
10883
10884 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10885 is higher than window.
10886
10887 A value of 0 means the caller should do scrolling
10888 as if point had gone off the screen. */
10889
10890 static int
10891 make_cursor_line_fully_visible (w, force_p)
10892 struct window *w;
10893 int force_p;
10894 {
10895 struct glyph_matrix *matrix;
10896 struct glyph_row *row;
10897 int window_height;
10898
10899 if (!make_cursor_line_fully_visible_p)
10900 return 1;
10901
10902 /* It's not always possible to find the cursor, e.g, when a window
10903 is full of overlay strings. Don't do anything in that case. */
10904 if (w->cursor.vpos < 0)
10905 return 1;
10906
10907 matrix = w->desired_matrix;
10908 row = MATRIX_ROW (matrix, w->cursor.vpos);
10909
10910 /* If the cursor row is not partially visible, there's nothing to do. */
10911 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10912 return 1;
10913
10914 /* If the row the cursor is in is taller than the window's height,
10915 it's not clear what to do, so do nothing. */
10916 window_height = window_box_height (w);
10917 if (row->height >= window_height)
10918 {
10919 if (!force_p || w->vscroll)
10920 return 1;
10921 }
10922 return 0;
10923
10924 #if 0
10925 /* This code used to try to scroll the window just enough to make
10926 the line visible. It returned 0 to say that the caller should
10927 allocate larger glyph matrices. */
10928
10929 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10930 {
10931 int dy = row->height - row->visible_height;
10932 w->vscroll = 0;
10933 w->cursor.y += dy;
10934 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10935 }
10936 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10937 {
10938 int dy = - (row->height - row->visible_height);
10939 w->vscroll = dy;
10940 w->cursor.y += dy;
10941 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10942 }
10943
10944 /* When we change the cursor y-position of the selected window,
10945 change this_line_y as well so that the display optimization for
10946 the cursor line of the selected window in redisplay_internal uses
10947 the correct y-position. */
10948 if (w == XWINDOW (selected_window))
10949 this_line_y = w->cursor.y;
10950
10951 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10952 redisplay with larger matrices. */
10953 if (matrix->nrows < required_matrix_height (w))
10954 {
10955 fonts_changed_p = 1;
10956 return 0;
10957 }
10958
10959 return 1;
10960 #endif /* 0 */
10961 }
10962
10963
10964 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10965 non-zero means only WINDOW is redisplayed in redisplay_internal.
10966 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10967 in redisplay_window to bring a partially visible line into view in
10968 the case that only the cursor has moved.
10969
10970 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10971 last screen line's vertical height extends past the end of the screen.
10972
10973 Value is
10974
10975 1 if scrolling succeeded
10976
10977 0 if scrolling didn't find point.
10978
10979 -1 if new fonts have been loaded so that we must interrupt
10980 redisplay, adjust glyph matrices, and try again. */
10981
10982 enum
10983 {
10984 SCROLLING_SUCCESS,
10985 SCROLLING_FAILED,
10986 SCROLLING_NEED_LARGER_MATRICES
10987 };
10988
10989 static int
10990 try_scrolling (window, just_this_one_p, scroll_conservatively,
10991 scroll_step, temp_scroll_step, last_line_misfit)
10992 Lisp_Object window;
10993 int just_this_one_p;
10994 EMACS_INT scroll_conservatively, scroll_step;
10995 int temp_scroll_step;
10996 int last_line_misfit;
10997 {
10998 struct window *w = XWINDOW (window);
10999 struct frame *f = XFRAME (w->frame);
11000 struct text_pos scroll_margin_pos;
11001 struct text_pos pos;
11002 struct text_pos startp;
11003 struct it it;
11004 Lisp_Object window_end;
11005 int this_scroll_margin;
11006 int dy = 0;
11007 int scroll_max;
11008 int rc;
11009 int amount_to_scroll = 0;
11010 Lisp_Object aggressive;
11011 int height;
11012 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11013
11014 #if GLYPH_DEBUG
11015 debug_method_add (w, "try_scrolling");
11016 #endif
11017
11018 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11019
11020 /* Compute scroll margin height in pixels. We scroll when point is
11021 within this distance from the top or bottom of the window. */
11022 if (scroll_margin > 0)
11023 {
11024 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11025 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11026 }
11027 else
11028 this_scroll_margin = 0;
11029
11030 /* Force scroll_conservatively to have a reasonable value so it doesn't
11031 cause an overflow while computing how much to scroll. */
11032 if (scroll_conservatively)
11033 scroll_conservatively = min (scroll_conservatively,
11034 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11035
11036 /* Compute how much we should try to scroll maximally to bring point
11037 into view. */
11038 if (scroll_step || scroll_conservatively || temp_scroll_step)
11039 scroll_max = max (scroll_step,
11040 max (scroll_conservatively, temp_scroll_step));
11041 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11042 || NUMBERP (current_buffer->scroll_up_aggressively))
11043 /* We're trying to scroll because of aggressive scrolling
11044 but no scroll_step is set. Choose an arbitrary one. Maybe
11045 there should be a variable for this. */
11046 scroll_max = 10;
11047 else
11048 scroll_max = 0;
11049 scroll_max *= FRAME_LINE_HEIGHT (f);
11050
11051 /* Decide whether we have to scroll down. Start at the window end
11052 and move this_scroll_margin up to find the position of the scroll
11053 margin. */
11054 window_end = Fwindow_end (window, Qt);
11055
11056 too_near_end:
11057
11058 CHARPOS (scroll_margin_pos) = XINT (window_end);
11059 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11060
11061 if (this_scroll_margin || extra_scroll_margin_lines)
11062 {
11063 start_display (&it, w, scroll_margin_pos);
11064 if (this_scroll_margin)
11065 move_it_vertically_backward (&it, this_scroll_margin);
11066 if (extra_scroll_margin_lines)
11067 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11068 scroll_margin_pos = it.current.pos;
11069 }
11070
11071 if (PT >= CHARPOS (scroll_margin_pos))
11072 {
11073 int y0;
11074
11075 /* Point is in the scroll margin at the bottom of the window, or
11076 below. Compute a new window start that makes point visible. */
11077
11078 /* Compute the distance from the scroll margin to PT.
11079 Give up if the distance is greater than scroll_max. */
11080 start_display (&it, w, scroll_margin_pos);
11081 y0 = it.current_y;
11082 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11083 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11084
11085 /* To make point visible, we have to move the window start
11086 down so that the line the cursor is in is visible, which
11087 means we have to add in the height of the cursor line. */
11088 dy = line_bottom_y (&it) - y0;
11089
11090 if (dy > scroll_max)
11091 return SCROLLING_FAILED;
11092
11093 /* Move the window start down. If scrolling conservatively,
11094 move it just enough down to make point visible. If
11095 scroll_step is set, move it down by scroll_step. */
11096 start_display (&it, w, startp);
11097
11098 if (scroll_conservatively)
11099 /* Set AMOUNT_TO_SCROLL to at least one line,
11100 and at most scroll_conservatively lines. */
11101 amount_to_scroll
11102 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11103 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11104 else if (scroll_step || temp_scroll_step)
11105 amount_to_scroll = scroll_max;
11106 else
11107 {
11108 aggressive = current_buffer->scroll_up_aggressively;
11109 height = WINDOW_BOX_TEXT_HEIGHT (w);
11110 if (NUMBERP (aggressive))
11111 {
11112 double float_amount = XFLOATINT (aggressive) * height;
11113 amount_to_scroll = float_amount;
11114 if (amount_to_scroll == 0 && float_amount > 0)
11115 amount_to_scroll = 1;
11116 }
11117 }
11118
11119 if (amount_to_scroll <= 0)
11120 return SCROLLING_FAILED;
11121
11122 /* If moving by amount_to_scroll leaves STARTP unchanged,
11123 move it down one screen line. */
11124
11125 move_it_vertically (&it, amount_to_scroll);
11126 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11127 move_it_by_lines (&it, 1, 1);
11128 startp = it.current.pos;
11129 }
11130 else
11131 {
11132 /* See if point is inside the scroll margin at the top of the
11133 window. */
11134 scroll_margin_pos = startp;
11135 if (this_scroll_margin)
11136 {
11137 start_display (&it, w, startp);
11138 move_it_vertically (&it, this_scroll_margin);
11139 scroll_margin_pos = it.current.pos;
11140 }
11141
11142 if (PT < CHARPOS (scroll_margin_pos))
11143 {
11144 /* Point is in the scroll margin at the top of the window or
11145 above what is displayed in the window. */
11146 int y0;
11147
11148 /* Compute the vertical distance from PT to the scroll
11149 margin position. Give up if distance is greater than
11150 scroll_max. */
11151 SET_TEXT_POS (pos, PT, PT_BYTE);
11152 start_display (&it, w, pos);
11153 y0 = it.current_y;
11154 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11155 it.last_visible_y, -1,
11156 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11157 dy = it.current_y - y0;
11158 if (dy > scroll_max)
11159 return SCROLLING_FAILED;
11160
11161 /* Compute new window start. */
11162 start_display (&it, w, startp);
11163
11164 if (scroll_conservatively)
11165 amount_to_scroll
11166 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11167 else if (scroll_step || temp_scroll_step)
11168 amount_to_scroll = scroll_max;
11169 else
11170 {
11171 aggressive = current_buffer->scroll_down_aggressively;
11172 height = WINDOW_BOX_TEXT_HEIGHT (w);
11173 if (NUMBERP (aggressive))
11174 {
11175 double float_amount = XFLOATINT (aggressive) * height;
11176 amount_to_scroll = float_amount;
11177 if (amount_to_scroll == 0 && float_amount > 0)
11178 amount_to_scroll = 1;
11179 }
11180 }
11181
11182 if (amount_to_scroll <= 0)
11183 return SCROLLING_FAILED;
11184
11185 move_it_vertically_backward (&it, amount_to_scroll);
11186 startp = it.current.pos;
11187 }
11188 }
11189
11190 /* Run window scroll functions. */
11191 startp = run_window_scroll_functions (window, startp);
11192
11193 /* Display the window. Give up if new fonts are loaded, or if point
11194 doesn't appear. */
11195 if (!try_window (window, startp))
11196 rc = SCROLLING_NEED_LARGER_MATRICES;
11197 else if (w->cursor.vpos < 0)
11198 {
11199 clear_glyph_matrix (w->desired_matrix);
11200 rc = SCROLLING_FAILED;
11201 }
11202 else
11203 {
11204 /* Maybe forget recorded base line for line number display. */
11205 if (!just_this_one_p
11206 || current_buffer->clip_changed
11207 || BEG_UNCHANGED < CHARPOS (startp))
11208 w->base_line_number = Qnil;
11209
11210 /* If cursor ends up on a partially visible line,
11211 treat that as being off the bottom of the screen. */
11212 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11213 {
11214 clear_glyph_matrix (w->desired_matrix);
11215 ++extra_scroll_margin_lines;
11216 goto too_near_end;
11217 }
11218 rc = SCROLLING_SUCCESS;
11219 }
11220
11221 return rc;
11222 }
11223
11224
11225 /* Compute a suitable window start for window W if display of W starts
11226 on a continuation line. Value is non-zero if a new window start
11227 was computed.
11228
11229 The new window start will be computed, based on W's width, starting
11230 from the start of the continued line. It is the start of the
11231 screen line with the minimum distance from the old start W->start. */
11232
11233 static int
11234 compute_window_start_on_continuation_line (w)
11235 struct window *w;
11236 {
11237 struct text_pos pos, start_pos;
11238 int window_start_changed_p = 0;
11239
11240 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11241
11242 /* If window start is on a continuation line... Window start may be
11243 < BEGV in case there's invisible text at the start of the
11244 buffer (M-x rmail, for example). */
11245 if (CHARPOS (start_pos) > BEGV
11246 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11247 {
11248 struct it it;
11249 struct glyph_row *row;
11250
11251 /* Handle the case that the window start is out of range. */
11252 if (CHARPOS (start_pos) < BEGV)
11253 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11254 else if (CHARPOS (start_pos) > ZV)
11255 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11256
11257 /* Find the start of the continued line. This should be fast
11258 because scan_buffer is fast (newline cache). */
11259 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11260 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11261 row, DEFAULT_FACE_ID);
11262 reseat_at_previous_visible_line_start (&it);
11263
11264 /* If the line start is "too far" away from the window start,
11265 say it takes too much time to compute a new window start. */
11266 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11267 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11268 {
11269 int min_distance, distance;
11270
11271 /* Move forward by display lines to find the new window
11272 start. If window width was enlarged, the new start can
11273 be expected to be > the old start. If window width was
11274 decreased, the new window start will be < the old start.
11275 So, we're looking for the display line start with the
11276 minimum distance from the old window start. */
11277 pos = it.current.pos;
11278 min_distance = INFINITY;
11279 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11280 distance < min_distance)
11281 {
11282 min_distance = distance;
11283 pos = it.current.pos;
11284 move_it_by_lines (&it, 1, 0);
11285 }
11286
11287 /* Set the window start there. */
11288 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11289 window_start_changed_p = 1;
11290 }
11291 }
11292
11293 return window_start_changed_p;
11294 }
11295
11296
11297 /* Try cursor movement in case text has not changed in window WINDOW,
11298 with window start STARTP. Value is
11299
11300 CURSOR_MOVEMENT_SUCCESS if successful
11301
11302 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11303
11304 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11305 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11306 we want to scroll as if scroll-step were set to 1. See the code.
11307
11308 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11309 which case we have to abort this redisplay, and adjust matrices
11310 first. */
11311
11312 enum
11313 {
11314 CURSOR_MOVEMENT_SUCCESS,
11315 CURSOR_MOVEMENT_CANNOT_BE_USED,
11316 CURSOR_MOVEMENT_MUST_SCROLL,
11317 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11318 };
11319
11320 static int
11321 try_cursor_movement (window, startp, scroll_step)
11322 Lisp_Object window;
11323 struct text_pos startp;
11324 int *scroll_step;
11325 {
11326 struct window *w = XWINDOW (window);
11327 struct frame *f = XFRAME (w->frame);
11328 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11329
11330 #if GLYPH_DEBUG
11331 if (inhibit_try_cursor_movement)
11332 return rc;
11333 #endif
11334
11335 /* Handle case where text has not changed, only point, and it has
11336 not moved off the frame. */
11337 if (/* Point may be in this window. */
11338 PT >= CHARPOS (startp)
11339 /* Selective display hasn't changed. */
11340 && !current_buffer->clip_changed
11341 /* Function force-mode-line-update is used to force a thorough
11342 redisplay. It sets either windows_or_buffers_changed or
11343 update_mode_lines. So don't take a shortcut here for these
11344 cases. */
11345 && !update_mode_lines
11346 && !windows_or_buffers_changed
11347 && !cursor_type_changed
11348 /* Can't use this case if highlighting a region. When a
11349 region exists, cursor movement has to do more than just
11350 set the cursor. */
11351 && !(!NILP (Vtransient_mark_mode)
11352 && !NILP (current_buffer->mark_active))
11353 && NILP (w->region_showing)
11354 && NILP (Vshow_trailing_whitespace)
11355 /* Right after splitting windows, last_point may be nil. */
11356 && INTEGERP (w->last_point)
11357 /* This code is not used for mini-buffer for the sake of the case
11358 of redisplaying to replace an echo area message; since in
11359 that case the mini-buffer contents per se are usually
11360 unchanged. This code is of no real use in the mini-buffer
11361 since the handling of this_line_start_pos, etc., in redisplay
11362 handles the same cases. */
11363 && !EQ (window, minibuf_window)
11364 /* When splitting windows or for new windows, it happens that
11365 redisplay is called with a nil window_end_vpos or one being
11366 larger than the window. This should really be fixed in
11367 window.c. I don't have this on my list, now, so we do
11368 approximately the same as the old redisplay code. --gerd. */
11369 && INTEGERP (w->window_end_vpos)
11370 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11371 && (FRAME_WINDOW_P (f)
11372 || !overlay_arrow_in_current_buffer_p ()))
11373 {
11374 int this_scroll_margin, top_scroll_margin;
11375 struct glyph_row *row = NULL;
11376
11377 #if GLYPH_DEBUG
11378 debug_method_add (w, "cursor movement");
11379 #endif
11380
11381 /* Scroll if point within this distance from the top or bottom
11382 of the window. This is a pixel value. */
11383 this_scroll_margin = max (0, scroll_margin);
11384 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11385 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11386
11387 top_scroll_margin = this_scroll_margin;
11388 if (WINDOW_WANTS_HEADER_LINE_P (w))
11389 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11390
11391 /* Start with the row the cursor was displayed during the last
11392 not paused redisplay. Give up if that row is not valid. */
11393 if (w->last_cursor.vpos < 0
11394 || w->last_cursor.vpos >= w->current_matrix->nrows)
11395 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11396 else
11397 {
11398 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11399 if (row->mode_line_p)
11400 ++row;
11401 if (!row->enabled_p)
11402 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11403 }
11404
11405 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11406 {
11407 int scroll_p = 0;
11408 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11409
11410 if (PT > XFASTINT (w->last_point))
11411 {
11412 /* Point has moved forward. */
11413 while (MATRIX_ROW_END_CHARPOS (row) < PT
11414 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11415 {
11416 xassert (row->enabled_p);
11417 ++row;
11418 }
11419
11420 /* The end position of a row equals the start position
11421 of the next row. If PT is there, we would rather
11422 display it in the next line. */
11423 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11424 && MATRIX_ROW_END_CHARPOS (row) == PT
11425 && !cursor_row_p (w, row))
11426 ++row;
11427
11428 /* If within the scroll margin, scroll. Note that
11429 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11430 the next line would be drawn, and that
11431 this_scroll_margin can be zero. */
11432 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11433 || PT > MATRIX_ROW_END_CHARPOS (row)
11434 /* Line is completely visible last line in window
11435 and PT is to be set in the next line. */
11436 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11437 && PT == MATRIX_ROW_END_CHARPOS (row)
11438 && !row->ends_at_zv_p
11439 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11440 scroll_p = 1;
11441 }
11442 else if (PT < XFASTINT (w->last_point))
11443 {
11444 /* Cursor has to be moved backward. Note that PT >=
11445 CHARPOS (startp) because of the outer if-statement. */
11446 while (!row->mode_line_p
11447 && (MATRIX_ROW_START_CHARPOS (row) > PT
11448 || (MATRIX_ROW_START_CHARPOS (row) == PT
11449 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11450 && (row->y > top_scroll_margin
11451 || CHARPOS (startp) == BEGV))
11452 {
11453 xassert (row->enabled_p);
11454 --row;
11455 }
11456
11457 /* Consider the following case: Window starts at BEGV,
11458 there is invisible, intangible text at BEGV, so that
11459 display starts at some point START > BEGV. It can
11460 happen that we are called with PT somewhere between
11461 BEGV and START. Try to handle that case. */
11462 if (row < w->current_matrix->rows
11463 || row->mode_line_p)
11464 {
11465 row = w->current_matrix->rows;
11466 if (row->mode_line_p)
11467 ++row;
11468 }
11469
11470 /* Due to newlines in overlay strings, we may have to
11471 skip forward over overlay strings. */
11472 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11473 && MATRIX_ROW_END_CHARPOS (row) == PT
11474 && !cursor_row_p (w, row))
11475 ++row;
11476
11477 /* If within the scroll margin, scroll. */
11478 if (row->y < top_scroll_margin
11479 && CHARPOS (startp) != BEGV)
11480 scroll_p = 1;
11481 }
11482
11483 if (PT < MATRIX_ROW_START_CHARPOS (row)
11484 || PT > MATRIX_ROW_END_CHARPOS (row))
11485 {
11486 /* if PT is not in the glyph row, give up. */
11487 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11488 }
11489 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11490 && make_cursor_line_fully_visible_p)
11491 {
11492 if (PT == MATRIX_ROW_END_CHARPOS (row)
11493 && !row->ends_at_zv_p
11494 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11495 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11496 else if (row->height > window_box_height (w))
11497 {
11498 /* If we end up in a partially visible line, let's
11499 make it fully visible, except when it's taller
11500 than the window, in which case we can't do much
11501 about it. */
11502 *scroll_step = 1;
11503 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11504 }
11505 else
11506 {
11507 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11508 if (!make_cursor_line_fully_visible (w, 0))
11509 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11510 else
11511 rc = CURSOR_MOVEMENT_SUCCESS;
11512 }
11513 }
11514 else if (scroll_p)
11515 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11516 else
11517 {
11518 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11519 rc = CURSOR_MOVEMENT_SUCCESS;
11520 }
11521 }
11522 }
11523
11524 return rc;
11525 }
11526
11527 void
11528 set_vertical_scroll_bar (w)
11529 struct window *w;
11530 {
11531 int start, end, whole;
11532
11533 /* Calculate the start and end positions for the current window.
11534 At some point, it would be nice to choose between scrollbars
11535 which reflect the whole buffer size, with special markers
11536 indicating narrowing, and scrollbars which reflect only the
11537 visible region.
11538
11539 Note that mini-buffers sometimes aren't displaying any text. */
11540 if (!MINI_WINDOW_P (w)
11541 || (w == XWINDOW (minibuf_window)
11542 && NILP (echo_area_buffer[0])))
11543 {
11544 struct buffer *buf = XBUFFER (w->buffer);
11545 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11546 start = marker_position (w->start) - BUF_BEGV (buf);
11547 /* I don't think this is guaranteed to be right. For the
11548 moment, we'll pretend it is. */
11549 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11550
11551 if (end < start)
11552 end = start;
11553 if (whole < (end - start))
11554 whole = end - start;
11555 }
11556 else
11557 start = end = whole = 0;
11558
11559 /* Indicate what this scroll bar ought to be displaying now. */
11560 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11561 }
11562
11563
11564 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11565 selected_window is redisplayed.
11566
11567 We can return without actually redisplaying the window if
11568 fonts_changed_p is nonzero. In that case, redisplay_internal will
11569 retry. */
11570
11571 static void
11572 redisplay_window (window, just_this_one_p)
11573 Lisp_Object window;
11574 int just_this_one_p;
11575 {
11576 struct window *w = XWINDOW (window);
11577 struct frame *f = XFRAME (w->frame);
11578 struct buffer *buffer = XBUFFER (w->buffer);
11579 struct buffer *old = current_buffer;
11580 struct text_pos lpoint, opoint, startp;
11581 int update_mode_line;
11582 int tem;
11583 struct it it;
11584 /* Record it now because it's overwritten. */
11585 int current_matrix_up_to_date_p = 0;
11586 int used_current_matrix_p = 0;
11587 /* This is less strict than current_matrix_up_to_date_p.
11588 It indictes that the buffer contents and narrowing are unchanged. */
11589 int buffer_unchanged_p = 0;
11590 int temp_scroll_step = 0;
11591 int count = SPECPDL_INDEX ();
11592 int rc;
11593 int centering_position;
11594 int last_line_misfit = 0;
11595
11596 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11597 opoint = lpoint;
11598
11599 /* W must be a leaf window here. */
11600 xassert (!NILP (w->buffer));
11601 #if GLYPH_DEBUG
11602 *w->desired_matrix->method = 0;
11603 #endif
11604
11605 specbind (Qinhibit_point_motion_hooks, Qt);
11606
11607 reconsider_clip_changes (w, buffer);
11608
11609 /* Has the mode line to be updated? */
11610 update_mode_line = (!NILP (w->update_mode_line)
11611 || update_mode_lines
11612 || buffer->clip_changed
11613 || buffer->prevent_redisplay_optimizations_p);
11614
11615 if (MINI_WINDOW_P (w))
11616 {
11617 if (w == XWINDOW (echo_area_window)
11618 && !NILP (echo_area_buffer[0]))
11619 {
11620 if (update_mode_line)
11621 /* We may have to update a tty frame's menu bar or a
11622 tool-bar. Example `M-x C-h C-h C-g'. */
11623 goto finish_menu_bars;
11624 else
11625 /* We've already displayed the echo area glyphs in this window. */
11626 goto finish_scroll_bars;
11627 }
11628 else if ((w != XWINDOW (minibuf_window)
11629 || minibuf_level == 0)
11630 /* When buffer is nonempty, redisplay window normally. */
11631 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11632 /* Quail displays non-mini buffers in minibuffer window.
11633 In that case, redisplay the window normally. */
11634 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11635 {
11636 /* W is a mini-buffer window, but it's not active, so clear
11637 it. */
11638 int yb = window_text_bottom_y (w);
11639 struct glyph_row *row;
11640 int y;
11641
11642 for (y = 0, row = w->desired_matrix->rows;
11643 y < yb;
11644 y += row->height, ++row)
11645 blank_row (w, row, y);
11646 goto finish_scroll_bars;
11647 }
11648
11649 clear_glyph_matrix (w->desired_matrix);
11650 }
11651
11652 /* Otherwise set up data on this window; select its buffer and point
11653 value. */
11654 /* Really select the buffer, for the sake of buffer-local
11655 variables. */
11656 set_buffer_internal_1 (XBUFFER (w->buffer));
11657 SET_TEXT_POS (opoint, PT, PT_BYTE);
11658
11659 current_matrix_up_to_date_p
11660 = (!NILP (w->window_end_valid)
11661 && !current_buffer->clip_changed
11662 && !current_buffer->prevent_redisplay_optimizations_p
11663 && XFASTINT (w->last_modified) >= MODIFF
11664 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11665
11666 buffer_unchanged_p
11667 = (!NILP (w->window_end_valid)
11668 && !current_buffer->clip_changed
11669 && XFASTINT (w->last_modified) >= MODIFF
11670 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11671
11672 /* When windows_or_buffers_changed is non-zero, we can't rely on
11673 the window end being valid, so set it to nil there. */
11674 if (windows_or_buffers_changed)
11675 {
11676 /* If window starts on a continuation line, maybe adjust the
11677 window start in case the window's width changed. */
11678 if (XMARKER (w->start)->buffer == current_buffer)
11679 compute_window_start_on_continuation_line (w);
11680
11681 w->window_end_valid = Qnil;
11682 }
11683
11684 /* Some sanity checks. */
11685 CHECK_WINDOW_END (w);
11686 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11687 abort ();
11688 if (BYTEPOS (opoint) < CHARPOS (opoint))
11689 abort ();
11690
11691 /* If %c is in mode line, update it if needed. */
11692 if (!NILP (w->column_number_displayed)
11693 /* This alternative quickly identifies a common case
11694 where no change is needed. */
11695 && !(PT == XFASTINT (w->last_point)
11696 && XFASTINT (w->last_modified) >= MODIFF
11697 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11698 && (XFASTINT (w->column_number_displayed)
11699 != (int) current_column ())) /* iftc */
11700 update_mode_line = 1;
11701
11702 /* Count number of windows showing the selected buffer. An indirect
11703 buffer counts as its base buffer. */
11704 if (!just_this_one_p)
11705 {
11706 struct buffer *current_base, *window_base;
11707 current_base = current_buffer;
11708 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11709 if (current_base->base_buffer)
11710 current_base = current_base->base_buffer;
11711 if (window_base->base_buffer)
11712 window_base = window_base->base_buffer;
11713 if (current_base == window_base)
11714 buffer_shared++;
11715 }
11716
11717 /* Point refers normally to the selected window. For any other
11718 window, set up appropriate value. */
11719 if (!EQ (window, selected_window))
11720 {
11721 int new_pt = XMARKER (w->pointm)->charpos;
11722 int new_pt_byte = marker_byte_position (w->pointm);
11723 if (new_pt < BEGV)
11724 {
11725 new_pt = BEGV;
11726 new_pt_byte = BEGV_BYTE;
11727 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11728 }
11729 else if (new_pt > (ZV - 1))
11730 {
11731 new_pt = ZV;
11732 new_pt_byte = ZV_BYTE;
11733 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11734 }
11735
11736 /* We don't use SET_PT so that the point-motion hooks don't run. */
11737 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11738 }
11739
11740 /* If any of the character widths specified in the display table
11741 have changed, invalidate the width run cache. It's true that
11742 this may be a bit late to catch such changes, but the rest of
11743 redisplay goes (non-fatally) haywire when the display table is
11744 changed, so why should we worry about doing any better? */
11745 if (current_buffer->width_run_cache)
11746 {
11747 struct Lisp_Char_Table *disptab = buffer_display_table ();
11748
11749 if (! disptab_matches_widthtab (disptab,
11750 XVECTOR (current_buffer->width_table)))
11751 {
11752 invalidate_region_cache (current_buffer,
11753 current_buffer->width_run_cache,
11754 BEG, Z);
11755 recompute_width_table (current_buffer, disptab);
11756 }
11757 }
11758
11759 /* If window-start is screwed up, choose a new one. */
11760 if (XMARKER (w->start)->buffer != current_buffer)
11761 goto recenter;
11762
11763 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11764
11765 /* If someone specified a new starting point but did not insist,
11766 check whether it can be used. */
11767 if (!NILP (w->optional_new_start)
11768 && CHARPOS (startp) >= BEGV
11769 && CHARPOS (startp) <= ZV)
11770 {
11771 w->optional_new_start = Qnil;
11772 start_display (&it, w, startp);
11773 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11774 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11775 if (IT_CHARPOS (it) == PT)
11776 w->force_start = Qt;
11777 /* IT may overshoot PT if text at PT is invisible. */
11778 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11779 w->force_start = Qt;
11780
11781
11782 }
11783
11784 /* Handle case where place to start displaying has been specified,
11785 unless the specified location is outside the accessible range. */
11786 if (!NILP (w->force_start)
11787 || w->frozen_window_start_p)
11788 {
11789 /* We set this later on if we have to adjust point. */
11790 int new_vpos = -1;
11791
11792 w->force_start = Qnil;
11793 w->vscroll = 0;
11794 w->window_end_valid = Qnil;
11795
11796 /* Forget any recorded base line for line number display. */
11797 if (!buffer_unchanged_p)
11798 w->base_line_number = Qnil;
11799
11800 /* Redisplay the mode line. Select the buffer properly for that.
11801 Also, run the hook window-scroll-functions
11802 because we have scrolled. */
11803 /* Note, we do this after clearing force_start because
11804 if there's an error, it is better to forget about force_start
11805 than to get into an infinite loop calling the hook functions
11806 and having them get more errors. */
11807 if (!update_mode_line
11808 || ! NILP (Vwindow_scroll_functions))
11809 {
11810 update_mode_line = 1;
11811 w->update_mode_line = Qt;
11812 startp = run_window_scroll_functions (window, startp);
11813 }
11814
11815 w->last_modified = make_number (0);
11816 w->last_overlay_modified = make_number (0);
11817 if (CHARPOS (startp) < BEGV)
11818 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11819 else if (CHARPOS (startp) > ZV)
11820 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11821
11822 /* Redisplay, then check if cursor has been set during the
11823 redisplay. Give up if new fonts were loaded. */
11824 if (!try_window (window, startp))
11825 {
11826 w->force_start = Qt;
11827 clear_glyph_matrix (w->desired_matrix);
11828 goto need_larger_matrices;
11829 }
11830
11831 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11832 {
11833 /* If point does not appear, try to move point so it does
11834 appear. The desired matrix has been built above, so we
11835 can use it here. */
11836 new_vpos = window_box_height (w) / 2;
11837 }
11838
11839 if (!make_cursor_line_fully_visible (w, 0))
11840 {
11841 /* Point does appear, but on a line partly visible at end of window.
11842 Move it back to a fully-visible line. */
11843 new_vpos = window_box_height (w);
11844 }
11845
11846 /* If we need to move point for either of the above reasons,
11847 now actually do it. */
11848 if (new_vpos >= 0)
11849 {
11850 struct glyph_row *row;
11851
11852 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11853 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11854 ++row;
11855
11856 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11857 MATRIX_ROW_START_BYTEPOS (row));
11858
11859 if (w != XWINDOW (selected_window))
11860 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11861 else if (current_buffer == old)
11862 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11863
11864 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11865
11866 /* If we are highlighting the region, then we just changed
11867 the region, so redisplay to show it. */
11868 if (!NILP (Vtransient_mark_mode)
11869 && !NILP (current_buffer->mark_active))
11870 {
11871 clear_glyph_matrix (w->desired_matrix);
11872 if (!try_window (window, startp))
11873 goto need_larger_matrices;
11874 }
11875 }
11876
11877 #if GLYPH_DEBUG
11878 debug_method_add (w, "forced window start");
11879 #endif
11880 goto done;
11881 }
11882
11883 /* Handle case where text has not changed, only point, and it has
11884 not moved off the frame, and we are not retrying after hscroll.
11885 (current_matrix_up_to_date_p is nonzero when retrying.) */
11886 if (current_matrix_up_to_date_p
11887 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11888 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11889 {
11890 switch (rc)
11891 {
11892 case CURSOR_MOVEMENT_SUCCESS:
11893 used_current_matrix_p = 1;
11894 goto done;
11895
11896 #if 0 /* try_cursor_movement never returns this value. */
11897 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11898 goto need_larger_matrices;
11899 #endif
11900
11901 case CURSOR_MOVEMENT_MUST_SCROLL:
11902 goto try_to_scroll;
11903
11904 default:
11905 abort ();
11906 }
11907 }
11908 /* If current starting point was originally the beginning of a line
11909 but no longer is, find a new starting point. */
11910 else if (!NILP (w->start_at_line_beg)
11911 && !(CHARPOS (startp) <= BEGV
11912 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11913 {
11914 #if GLYPH_DEBUG
11915 debug_method_add (w, "recenter 1");
11916 #endif
11917 goto recenter;
11918 }
11919
11920 /* Try scrolling with try_window_id. Value is > 0 if update has
11921 been done, it is -1 if we know that the same window start will
11922 not work. It is 0 if unsuccessful for some other reason. */
11923 else if ((tem = try_window_id (w)) != 0)
11924 {
11925 #if GLYPH_DEBUG
11926 debug_method_add (w, "try_window_id %d", tem);
11927 #endif
11928
11929 if (fonts_changed_p)
11930 goto need_larger_matrices;
11931 if (tem > 0)
11932 goto done;
11933
11934 /* Otherwise try_window_id has returned -1 which means that we
11935 don't want the alternative below this comment to execute. */
11936 }
11937 else if (CHARPOS (startp) >= BEGV
11938 && CHARPOS (startp) <= ZV
11939 && PT >= CHARPOS (startp)
11940 && (CHARPOS (startp) < ZV
11941 /* Avoid starting at end of buffer. */
11942 || CHARPOS (startp) == BEGV
11943 || (XFASTINT (w->last_modified) >= MODIFF
11944 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11945 {
11946 #if GLYPH_DEBUG
11947 debug_method_add (w, "same window start");
11948 #endif
11949
11950 /* Try to redisplay starting at same place as before.
11951 If point has not moved off frame, accept the results. */
11952 if (!current_matrix_up_to_date_p
11953 /* Don't use try_window_reusing_current_matrix in this case
11954 because a window scroll function can have changed the
11955 buffer. */
11956 || !NILP (Vwindow_scroll_functions)
11957 || MINI_WINDOW_P (w)
11958 || !(used_current_matrix_p
11959 = try_window_reusing_current_matrix (w)))
11960 {
11961 IF_DEBUG (debug_method_add (w, "1"));
11962 try_window (window, startp);
11963 }
11964
11965 if (fonts_changed_p)
11966 goto need_larger_matrices;
11967
11968 if (w->cursor.vpos >= 0)
11969 {
11970 if (!just_this_one_p
11971 || current_buffer->clip_changed
11972 || BEG_UNCHANGED < CHARPOS (startp))
11973 /* Forget any recorded base line for line number display. */
11974 w->base_line_number = Qnil;
11975
11976 if (!make_cursor_line_fully_visible (w, 1))
11977 {
11978 clear_glyph_matrix (w->desired_matrix);
11979 last_line_misfit = 1;
11980 }
11981 /* Drop through and scroll. */
11982 else
11983 goto done;
11984 }
11985 else
11986 clear_glyph_matrix (w->desired_matrix);
11987 }
11988
11989 try_to_scroll:
11990
11991 w->last_modified = make_number (0);
11992 w->last_overlay_modified = make_number (0);
11993
11994 /* Redisplay the mode line. Select the buffer properly for that. */
11995 if (!update_mode_line)
11996 {
11997 update_mode_line = 1;
11998 w->update_mode_line = Qt;
11999 }
12000
12001 /* Try to scroll by specified few lines. */
12002 if ((scroll_conservatively
12003 || scroll_step
12004 || temp_scroll_step
12005 || NUMBERP (current_buffer->scroll_up_aggressively)
12006 || NUMBERP (current_buffer->scroll_down_aggressively))
12007 && !current_buffer->clip_changed
12008 && CHARPOS (startp) >= BEGV
12009 && CHARPOS (startp) <= ZV)
12010 {
12011 /* The function returns -1 if new fonts were loaded, 1 if
12012 successful, 0 if not successful. */
12013 int rc = try_scrolling (window, just_this_one_p,
12014 scroll_conservatively,
12015 scroll_step,
12016 temp_scroll_step, last_line_misfit);
12017 switch (rc)
12018 {
12019 case SCROLLING_SUCCESS:
12020 goto done;
12021
12022 case SCROLLING_NEED_LARGER_MATRICES:
12023 goto need_larger_matrices;
12024
12025 case SCROLLING_FAILED:
12026 break;
12027
12028 default:
12029 abort ();
12030 }
12031 }
12032
12033 /* Finally, just choose place to start which centers point */
12034
12035 recenter:
12036 centering_position = window_box_height (w) / 2;
12037
12038 point_at_top:
12039 /* Jump here with centering_position already set to 0. */
12040
12041 #if GLYPH_DEBUG
12042 debug_method_add (w, "recenter");
12043 #endif
12044
12045 /* w->vscroll = 0; */
12046
12047 /* Forget any previously recorded base line for line number display. */
12048 if (!buffer_unchanged_p)
12049 w->base_line_number = Qnil;
12050
12051 /* Move backward half the height of the window. */
12052 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12053 it.current_y = it.last_visible_y;
12054 move_it_vertically_backward (&it, centering_position);
12055 xassert (IT_CHARPOS (it) >= BEGV);
12056
12057 /* The function move_it_vertically_backward may move over more
12058 than the specified y-distance. If it->w is small, e.g. a
12059 mini-buffer window, we may end up in front of the window's
12060 display area. Start displaying at the start of the line
12061 containing PT in this case. */
12062 if (it.current_y <= 0)
12063 {
12064 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12065 move_it_vertically_backward (&it, 0);
12066 xassert (IT_CHARPOS (it) <= PT);
12067 it.current_y = 0;
12068 }
12069
12070 it.current_x = it.hpos = 0;
12071
12072 /* Set startp here explicitly in case that helps avoid an infinite loop
12073 in case the window-scroll-functions functions get errors. */
12074 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12075
12076 /* Run scroll hooks. */
12077 startp = run_window_scroll_functions (window, it.current.pos);
12078
12079 /* Redisplay the window. */
12080 if (!current_matrix_up_to_date_p
12081 || windows_or_buffers_changed
12082 || cursor_type_changed
12083 /* Don't use try_window_reusing_current_matrix in this case
12084 because it can have changed the buffer. */
12085 || !NILP (Vwindow_scroll_functions)
12086 || !just_this_one_p
12087 || MINI_WINDOW_P (w)
12088 || !(used_current_matrix_p
12089 = try_window_reusing_current_matrix (w)))
12090 try_window (window, startp);
12091
12092 /* If new fonts have been loaded (due to fontsets), give up. We
12093 have to start a new redisplay since we need to re-adjust glyph
12094 matrices. */
12095 if (fonts_changed_p)
12096 goto need_larger_matrices;
12097
12098 /* If cursor did not appear assume that the middle of the window is
12099 in the first line of the window. Do it again with the next line.
12100 (Imagine a window of height 100, displaying two lines of height
12101 60. Moving back 50 from it->last_visible_y will end in the first
12102 line.) */
12103 if (w->cursor.vpos < 0)
12104 {
12105 if (!NILP (w->window_end_valid)
12106 && PT >= Z - XFASTINT (w->window_end_pos))
12107 {
12108 clear_glyph_matrix (w->desired_matrix);
12109 move_it_by_lines (&it, 1, 0);
12110 try_window (window, it.current.pos);
12111 }
12112 else if (PT < IT_CHARPOS (it))
12113 {
12114 clear_glyph_matrix (w->desired_matrix);
12115 move_it_by_lines (&it, -1, 0);
12116 try_window (window, it.current.pos);
12117 }
12118 else
12119 {
12120 /* Not much we can do about it. */
12121 }
12122 }
12123
12124 /* Consider the following case: Window starts at BEGV, there is
12125 invisible, intangible text at BEGV, so that display starts at
12126 some point START > BEGV. It can happen that we are called with
12127 PT somewhere between BEGV and START. Try to handle that case. */
12128 if (w->cursor.vpos < 0)
12129 {
12130 struct glyph_row *row = w->current_matrix->rows;
12131 if (row->mode_line_p)
12132 ++row;
12133 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12134 }
12135
12136 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12137 {
12138 /* If vscroll is enabled, disable it and try again. */
12139 if (w->vscroll)
12140 {
12141 w->vscroll = 0;
12142 clear_glyph_matrix (w->desired_matrix);
12143 goto recenter;
12144 }
12145
12146 /* If centering point failed to make the whole line visible,
12147 put point at the top instead. That has to make the whole line
12148 visible, if it can be done. */
12149 clear_glyph_matrix (w->desired_matrix);
12150 centering_position = 0;
12151 goto point_at_top;
12152 }
12153
12154 done:
12155
12156 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12157 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12158 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12159 ? Qt : Qnil);
12160
12161 /* Display the mode line, if we must. */
12162 if ((update_mode_line
12163 /* If window not full width, must redo its mode line
12164 if (a) the window to its side is being redone and
12165 (b) we do a frame-based redisplay. This is a consequence
12166 of how inverted lines are drawn in frame-based redisplay. */
12167 || (!just_this_one_p
12168 && !FRAME_WINDOW_P (f)
12169 && !WINDOW_FULL_WIDTH_P (w))
12170 /* Line number to display. */
12171 || INTEGERP (w->base_line_pos)
12172 /* Column number is displayed and different from the one displayed. */
12173 || (!NILP (w->column_number_displayed)
12174 && (XFASTINT (w->column_number_displayed)
12175 != (int) current_column ()))) /* iftc */
12176 /* This means that the window has a mode line. */
12177 && (WINDOW_WANTS_MODELINE_P (w)
12178 || WINDOW_WANTS_HEADER_LINE_P (w)))
12179 {
12180 display_mode_lines (w);
12181
12182 /* If mode line height has changed, arrange for a thorough
12183 immediate redisplay using the correct mode line height. */
12184 if (WINDOW_WANTS_MODELINE_P (w)
12185 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12186 {
12187 fonts_changed_p = 1;
12188 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12189 = DESIRED_MODE_LINE_HEIGHT (w);
12190 }
12191
12192 /* If top line height has changed, arrange for a thorough
12193 immediate redisplay using the correct mode line height. */
12194 if (WINDOW_WANTS_HEADER_LINE_P (w)
12195 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12196 {
12197 fonts_changed_p = 1;
12198 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12199 = DESIRED_HEADER_LINE_HEIGHT (w);
12200 }
12201
12202 if (fonts_changed_p)
12203 goto need_larger_matrices;
12204 }
12205
12206 if (!line_number_displayed
12207 && !BUFFERP (w->base_line_pos))
12208 {
12209 w->base_line_pos = Qnil;
12210 w->base_line_number = Qnil;
12211 }
12212
12213 finish_menu_bars:
12214
12215 /* When we reach a frame's selected window, redo the frame's menu bar. */
12216 if (update_mode_line
12217 && EQ (FRAME_SELECTED_WINDOW (f), window))
12218 {
12219 int redisplay_menu_p = 0;
12220 int redisplay_tool_bar_p = 0;
12221
12222 if (FRAME_WINDOW_P (f))
12223 {
12224 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12225 || defined (USE_GTK)
12226 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12227 #else
12228 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12229 #endif
12230 }
12231 else
12232 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12233
12234 if (redisplay_menu_p)
12235 display_menu_bar (w);
12236
12237 #ifdef HAVE_WINDOW_SYSTEM
12238 #ifdef USE_GTK
12239 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12240 #else
12241 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12242 && (FRAME_TOOL_BAR_LINES (f) > 0
12243 || auto_resize_tool_bars_p);
12244
12245 #endif
12246
12247 if (redisplay_tool_bar_p)
12248 redisplay_tool_bar (f);
12249 #endif
12250 }
12251
12252 #ifdef HAVE_WINDOW_SYSTEM
12253 if (FRAME_WINDOW_P (f)
12254 && update_window_fringes (w, 0)
12255 && !just_this_one_p
12256 && (used_current_matrix_p || overlay_arrow_seen)
12257 && !w->pseudo_window_p)
12258 {
12259 update_begin (f);
12260 BLOCK_INPUT;
12261 if (draw_window_fringes (w, 1))
12262 x_draw_vertical_border (w);
12263 UNBLOCK_INPUT;
12264 update_end (f);
12265 }
12266 #endif /* HAVE_WINDOW_SYSTEM */
12267
12268 /* We go to this label, with fonts_changed_p nonzero,
12269 if it is necessary to try again using larger glyph matrices.
12270 We have to redeem the scroll bar even in this case,
12271 because the loop in redisplay_internal expects that. */
12272 need_larger_matrices:
12273 ;
12274 finish_scroll_bars:
12275
12276 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12277 {
12278 /* Set the thumb's position and size. */
12279 set_vertical_scroll_bar (w);
12280
12281 /* Note that we actually used the scroll bar attached to this
12282 window, so it shouldn't be deleted at the end of redisplay. */
12283 redeem_scroll_bar_hook (w);
12284 }
12285
12286 /* Restore current_buffer and value of point in it. */
12287 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12288 set_buffer_internal_1 (old);
12289 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12290
12291 unbind_to (count, Qnil);
12292 }
12293
12294
12295 /* Build the complete desired matrix of WINDOW with a window start
12296 buffer position POS. Value is non-zero if successful. It is zero
12297 if fonts were loaded during redisplay which makes re-adjusting
12298 glyph matrices necessary. */
12299
12300 int
12301 try_window (window, pos)
12302 Lisp_Object window;
12303 struct text_pos pos;
12304 {
12305 struct window *w = XWINDOW (window);
12306 struct it it;
12307 struct glyph_row *last_text_row = NULL;
12308
12309 /* Make POS the new window start. */
12310 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12311
12312 /* Mark cursor position as unknown. No overlay arrow seen. */
12313 w->cursor.vpos = -1;
12314 overlay_arrow_seen = 0;
12315
12316 /* Initialize iterator and info to start at POS. */
12317 start_display (&it, w, pos);
12318
12319 /* Display all lines of W. */
12320 while (it.current_y < it.last_visible_y)
12321 {
12322 if (display_line (&it))
12323 last_text_row = it.glyph_row - 1;
12324 if (fonts_changed_p)
12325 return 0;
12326 }
12327
12328 /* If bottom moved off end of frame, change mode line percentage. */
12329 if (XFASTINT (w->window_end_pos) <= 0
12330 && Z != IT_CHARPOS (it))
12331 w->update_mode_line = Qt;
12332
12333 /* Set window_end_pos to the offset of the last character displayed
12334 on the window from the end of current_buffer. Set
12335 window_end_vpos to its row number. */
12336 if (last_text_row)
12337 {
12338 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12339 w->window_end_bytepos
12340 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12341 w->window_end_pos
12342 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12343 w->window_end_vpos
12344 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12345 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12346 ->displays_text_p);
12347 }
12348 else
12349 {
12350 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12351 w->window_end_pos = make_number (Z - ZV);
12352 w->window_end_vpos = make_number (0);
12353 }
12354
12355 /* But that is not valid info until redisplay finishes. */
12356 w->window_end_valid = Qnil;
12357 return 1;
12358 }
12359
12360
12361 \f
12362 /************************************************************************
12363 Window redisplay reusing current matrix when buffer has not changed
12364 ************************************************************************/
12365
12366 /* Try redisplay of window W showing an unchanged buffer with a
12367 different window start than the last time it was displayed by
12368 reusing its current matrix. Value is non-zero if successful.
12369 W->start is the new window start. */
12370
12371 static int
12372 try_window_reusing_current_matrix (w)
12373 struct window *w;
12374 {
12375 struct frame *f = XFRAME (w->frame);
12376 struct glyph_row *row, *bottom_row;
12377 struct it it;
12378 struct run run;
12379 struct text_pos start, new_start;
12380 int nrows_scrolled, i;
12381 struct glyph_row *last_text_row;
12382 struct glyph_row *last_reused_text_row;
12383 struct glyph_row *start_row;
12384 int start_vpos, min_y, max_y;
12385
12386 #if GLYPH_DEBUG
12387 if (inhibit_try_window_reusing)
12388 return 0;
12389 #endif
12390
12391 if (/* This function doesn't handle terminal frames. */
12392 !FRAME_WINDOW_P (f)
12393 /* Don't try to reuse the display if windows have been split
12394 or such. */
12395 || windows_or_buffers_changed
12396 || cursor_type_changed)
12397 return 0;
12398
12399 /* Can't do this if region may have changed. */
12400 if ((!NILP (Vtransient_mark_mode)
12401 && !NILP (current_buffer->mark_active))
12402 || !NILP (w->region_showing)
12403 || !NILP (Vshow_trailing_whitespace))
12404 return 0;
12405
12406 /* If top-line visibility has changed, give up. */
12407 if (WINDOW_WANTS_HEADER_LINE_P (w)
12408 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12409 return 0;
12410
12411 /* Give up if old or new display is scrolled vertically. We could
12412 make this function handle this, but right now it doesn't. */
12413 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12414 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12415 return 0;
12416
12417 /* The variable new_start now holds the new window start. The old
12418 start `start' can be determined from the current matrix. */
12419 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12420 start = start_row->start.pos;
12421 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12422
12423 /* Clear the desired matrix for the display below. */
12424 clear_glyph_matrix (w->desired_matrix);
12425
12426 if (CHARPOS (new_start) <= CHARPOS (start))
12427 {
12428 int first_row_y;
12429
12430 /* Don't use this method if the display starts with an ellipsis
12431 displayed for invisible text. It's not easy to handle that case
12432 below, and it's certainly not worth the effort since this is
12433 not a frequent case. */
12434 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12435 return 0;
12436
12437 IF_DEBUG (debug_method_add (w, "twu1"));
12438
12439 /* Display up to a row that can be reused. The variable
12440 last_text_row is set to the last row displayed that displays
12441 text. Note that it.vpos == 0 if or if not there is a
12442 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12443 start_display (&it, w, new_start);
12444 first_row_y = it.current_y;
12445 w->cursor.vpos = -1;
12446 last_text_row = last_reused_text_row = NULL;
12447
12448 while (it.current_y < it.last_visible_y
12449 && !fonts_changed_p)
12450 {
12451 /* If we have reached into the characters in the START row,
12452 that means the line boundaries have changed. So we
12453 can't start copying with the row START. Maybe it will
12454 work to start copying with the following row. */
12455 while (IT_CHARPOS (it) > CHARPOS (start))
12456 {
12457 /* Advance to the next row as the "start". */
12458 start_row++;
12459 start = start_row->start.pos;
12460 /* If there are no more rows to try, or just one, give up. */
12461 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12462 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12463 || CHARPOS (start) == ZV)
12464 {
12465 clear_glyph_matrix (w->desired_matrix);
12466 return 0;
12467 }
12468
12469 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12470 }
12471 /* If we have reached alignment,
12472 we can copy the rest of the rows. */
12473 if (IT_CHARPOS (it) == CHARPOS (start))
12474 break;
12475
12476 if (display_line (&it))
12477 last_text_row = it.glyph_row - 1;
12478 }
12479
12480 /* A value of current_y < last_visible_y means that we stopped
12481 at the previous window start, which in turn means that we
12482 have at least one reusable row. */
12483 if (it.current_y < it.last_visible_y)
12484 {
12485 /* IT.vpos always starts from 0; it counts text lines. */
12486 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12487
12488 /* Find PT if not already found in the lines displayed. */
12489 if (w->cursor.vpos < 0)
12490 {
12491 int dy = it.current_y - start_row->y;
12492
12493 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12494 row = row_containing_pos (w, PT, row, NULL, dy);
12495 if (row)
12496 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12497 dy, nrows_scrolled);
12498 else
12499 {
12500 clear_glyph_matrix (w->desired_matrix);
12501 return 0;
12502 }
12503 }
12504
12505 /* Scroll the display. Do it before the current matrix is
12506 changed. The problem here is that update has not yet
12507 run, i.e. part of the current matrix is not up to date.
12508 scroll_run_hook will clear the cursor, and use the
12509 current matrix to get the height of the row the cursor is
12510 in. */
12511 run.current_y = start_row->y;
12512 run.desired_y = it.current_y;
12513 run.height = it.last_visible_y - it.current_y;
12514
12515 if (run.height > 0 && run.current_y != run.desired_y)
12516 {
12517 update_begin (f);
12518 rif->update_window_begin_hook (w);
12519 rif->clear_window_mouse_face (w);
12520 rif->scroll_run_hook (w, &run);
12521 rif->update_window_end_hook (w, 0, 0);
12522 update_end (f);
12523 }
12524
12525 /* Shift current matrix down by nrows_scrolled lines. */
12526 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12527 rotate_matrix (w->current_matrix,
12528 start_vpos,
12529 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12530 nrows_scrolled);
12531
12532 /* Disable lines that must be updated. */
12533 for (i = 0; i < it.vpos; ++i)
12534 (start_row + i)->enabled_p = 0;
12535
12536 /* Re-compute Y positions. */
12537 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12538 max_y = it.last_visible_y;
12539 for (row = start_row + nrows_scrolled;
12540 row < bottom_row;
12541 ++row)
12542 {
12543 row->y = it.current_y;
12544 row->visible_height = row->height;
12545
12546 if (row->y < min_y)
12547 row->visible_height -= min_y - row->y;
12548 if (row->y + row->height > max_y)
12549 row->visible_height -= row->y + row->height - max_y;
12550 row->redraw_fringe_bitmaps_p = 1;
12551
12552 it.current_y += row->height;
12553
12554 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12555 last_reused_text_row = row;
12556 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12557 break;
12558 }
12559
12560 /* Disable lines in the current matrix which are now
12561 below the window. */
12562 for (++row; row < bottom_row; ++row)
12563 row->enabled_p = 0;
12564 }
12565
12566 /* Update window_end_pos etc.; last_reused_text_row is the last
12567 reused row from the current matrix containing text, if any.
12568 The value of last_text_row is the last displayed line
12569 containing text. */
12570 if (last_reused_text_row)
12571 {
12572 w->window_end_bytepos
12573 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12574 w->window_end_pos
12575 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12576 w->window_end_vpos
12577 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12578 w->current_matrix));
12579 }
12580 else if (last_text_row)
12581 {
12582 w->window_end_bytepos
12583 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12584 w->window_end_pos
12585 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12586 w->window_end_vpos
12587 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12588 }
12589 else
12590 {
12591 /* This window must be completely empty. */
12592 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12593 w->window_end_pos = make_number (Z - ZV);
12594 w->window_end_vpos = make_number (0);
12595 }
12596 w->window_end_valid = Qnil;
12597
12598 /* Update hint: don't try scrolling again in update_window. */
12599 w->desired_matrix->no_scrolling_p = 1;
12600
12601 #if GLYPH_DEBUG
12602 debug_method_add (w, "try_window_reusing_current_matrix 1");
12603 #endif
12604 return 1;
12605 }
12606 else if (CHARPOS (new_start) > CHARPOS (start))
12607 {
12608 struct glyph_row *pt_row, *row;
12609 struct glyph_row *first_reusable_row;
12610 struct glyph_row *first_row_to_display;
12611 int dy;
12612 int yb = window_text_bottom_y (w);
12613
12614 /* Find the row starting at new_start, if there is one. Don't
12615 reuse a partially visible line at the end. */
12616 first_reusable_row = start_row;
12617 while (first_reusable_row->enabled_p
12618 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12619 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12620 < CHARPOS (new_start)))
12621 ++first_reusable_row;
12622
12623 /* Give up if there is no row to reuse. */
12624 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12625 || !first_reusable_row->enabled_p
12626 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12627 != CHARPOS (new_start)))
12628 return 0;
12629
12630 /* We can reuse fully visible rows beginning with
12631 first_reusable_row to the end of the window. Set
12632 first_row_to_display to the first row that cannot be reused.
12633 Set pt_row to the row containing point, if there is any. */
12634 pt_row = NULL;
12635 for (first_row_to_display = first_reusable_row;
12636 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12637 ++first_row_to_display)
12638 {
12639 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12640 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12641 pt_row = first_row_to_display;
12642 }
12643
12644 /* Start displaying at the start of first_row_to_display. */
12645 xassert (first_row_to_display->y < yb);
12646 init_to_row_start (&it, w, first_row_to_display);
12647
12648 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12649 - start_vpos);
12650 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12651 - nrows_scrolled);
12652 it.current_y = (first_row_to_display->y - first_reusable_row->y
12653 + WINDOW_HEADER_LINE_HEIGHT (w));
12654
12655 /* Display lines beginning with first_row_to_display in the
12656 desired matrix. Set last_text_row to the last row displayed
12657 that displays text. */
12658 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12659 if (pt_row == NULL)
12660 w->cursor.vpos = -1;
12661 last_text_row = NULL;
12662 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12663 if (display_line (&it))
12664 last_text_row = it.glyph_row - 1;
12665
12666 /* Give up If point isn't in a row displayed or reused. */
12667 if (w->cursor.vpos < 0)
12668 {
12669 clear_glyph_matrix (w->desired_matrix);
12670 return 0;
12671 }
12672
12673 /* If point is in a reused row, adjust y and vpos of the cursor
12674 position. */
12675 if (pt_row)
12676 {
12677 w->cursor.vpos -= nrows_scrolled;
12678 w->cursor.y -= first_reusable_row->y - start_row->y;
12679 }
12680
12681 /* Scroll the display. */
12682 run.current_y = first_reusable_row->y;
12683 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12684 run.height = it.last_visible_y - run.current_y;
12685 dy = run.current_y - run.desired_y;
12686
12687 if (run.height)
12688 {
12689 update_begin (f);
12690 rif->update_window_begin_hook (w);
12691 rif->clear_window_mouse_face (w);
12692 rif->scroll_run_hook (w, &run);
12693 rif->update_window_end_hook (w, 0, 0);
12694 update_end (f);
12695 }
12696
12697 /* Adjust Y positions of reused rows. */
12698 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12699 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12700 max_y = it.last_visible_y;
12701 for (row = first_reusable_row; row < first_row_to_display; ++row)
12702 {
12703 row->y -= dy;
12704 row->visible_height = row->height;
12705 if (row->y < min_y)
12706 row->visible_height -= min_y - row->y;
12707 if (row->y + row->height > max_y)
12708 row->visible_height -= row->y + row->height - max_y;
12709 row->redraw_fringe_bitmaps_p = 1;
12710 }
12711
12712 /* Scroll the current matrix. */
12713 xassert (nrows_scrolled > 0);
12714 rotate_matrix (w->current_matrix,
12715 start_vpos,
12716 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12717 -nrows_scrolled);
12718
12719 /* Disable rows not reused. */
12720 for (row -= nrows_scrolled; row < bottom_row; ++row)
12721 row->enabled_p = 0;
12722
12723 /* Point may have moved to a different line, so we cannot assume that
12724 the previous cursor position is valid; locate the correct row. */
12725 if (pt_row)
12726 {
12727 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12728 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12729 row++)
12730 {
12731 w->cursor.vpos++;
12732 w->cursor.y = row->y;
12733 }
12734 if (row < bottom_row)
12735 {
12736 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12737 while (glyph->charpos < PT)
12738 {
12739 w->cursor.hpos++;
12740 w->cursor.x += glyph->pixel_width;
12741 glyph++;
12742 }
12743 }
12744 }
12745
12746 /* Adjust window end. A null value of last_text_row means that
12747 the window end is in reused rows which in turn means that
12748 only its vpos can have changed. */
12749 if (last_text_row)
12750 {
12751 w->window_end_bytepos
12752 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12753 w->window_end_pos
12754 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12755 w->window_end_vpos
12756 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12757 }
12758 else
12759 {
12760 w->window_end_vpos
12761 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12762 }
12763
12764 w->window_end_valid = Qnil;
12765 w->desired_matrix->no_scrolling_p = 1;
12766
12767 #if GLYPH_DEBUG
12768 debug_method_add (w, "try_window_reusing_current_matrix 2");
12769 #endif
12770 return 1;
12771 }
12772
12773 return 0;
12774 }
12775
12776
12777 \f
12778 /************************************************************************
12779 Window redisplay reusing current matrix when buffer has changed
12780 ************************************************************************/
12781
12782 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12783 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12784 int *, int *));
12785 static struct glyph_row *
12786 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12787 struct glyph_row *));
12788
12789
12790 /* Return the last row in MATRIX displaying text. If row START is
12791 non-null, start searching with that row. IT gives the dimensions
12792 of the display. Value is null if matrix is empty; otherwise it is
12793 a pointer to the row found. */
12794
12795 static struct glyph_row *
12796 find_last_row_displaying_text (matrix, it, start)
12797 struct glyph_matrix *matrix;
12798 struct it *it;
12799 struct glyph_row *start;
12800 {
12801 struct glyph_row *row, *row_found;
12802
12803 /* Set row_found to the last row in IT->w's current matrix
12804 displaying text. The loop looks funny but think of partially
12805 visible lines. */
12806 row_found = NULL;
12807 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12808 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12809 {
12810 xassert (row->enabled_p);
12811 row_found = row;
12812 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12813 break;
12814 ++row;
12815 }
12816
12817 return row_found;
12818 }
12819
12820
12821 /* Return the last row in the current matrix of W that is not affected
12822 by changes at the start of current_buffer that occurred since W's
12823 current matrix was built. Value is null if no such row exists.
12824
12825 BEG_UNCHANGED us the number of characters unchanged at the start of
12826 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12827 first changed character in current_buffer. Characters at positions <
12828 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12829 when the current matrix was built. */
12830
12831 static struct glyph_row *
12832 find_last_unchanged_at_beg_row (w)
12833 struct window *w;
12834 {
12835 int first_changed_pos = BEG + BEG_UNCHANGED;
12836 struct glyph_row *row;
12837 struct glyph_row *row_found = NULL;
12838 int yb = window_text_bottom_y (w);
12839
12840 /* Find the last row displaying unchanged text. */
12841 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12842 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12843 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12844 {
12845 if (/* If row ends before first_changed_pos, it is unchanged,
12846 except in some case. */
12847 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12848 /* When row ends in ZV and we write at ZV it is not
12849 unchanged. */
12850 && !row->ends_at_zv_p
12851 /* When first_changed_pos is the end of a continued line,
12852 row is not unchanged because it may be no longer
12853 continued. */
12854 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12855 && (row->continued_p
12856 || row->exact_window_width_line_p)))
12857 row_found = row;
12858
12859 /* Stop if last visible row. */
12860 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12861 break;
12862
12863 ++row;
12864 }
12865
12866 return row_found;
12867 }
12868
12869
12870 /* Find the first glyph row in the current matrix of W that is not
12871 affected by changes at the end of current_buffer since the
12872 time W's current matrix was built.
12873
12874 Return in *DELTA the number of chars by which buffer positions in
12875 unchanged text at the end of current_buffer must be adjusted.
12876
12877 Return in *DELTA_BYTES the corresponding number of bytes.
12878
12879 Value is null if no such row exists, i.e. all rows are affected by
12880 changes. */
12881
12882 static struct glyph_row *
12883 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12884 struct window *w;
12885 int *delta, *delta_bytes;
12886 {
12887 struct glyph_row *row;
12888 struct glyph_row *row_found = NULL;
12889
12890 *delta = *delta_bytes = 0;
12891
12892 /* Display must not have been paused, otherwise the current matrix
12893 is not up to date. */
12894 if (NILP (w->window_end_valid))
12895 abort ();
12896
12897 /* A value of window_end_pos >= END_UNCHANGED means that the window
12898 end is in the range of changed text. If so, there is no
12899 unchanged row at the end of W's current matrix. */
12900 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12901 return NULL;
12902
12903 /* Set row to the last row in W's current matrix displaying text. */
12904 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12905
12906 /* If matrix is entirely empty, no unchanged row exists. */
12907 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12908 {
12909 /* The value of row is the last glyph row in the matrix having a
12910 meaningful buffer position in it. The end position of row
12911 corresponds to window_end_pos. This allows us to translate
12912 buffer positions in the current matrix to current buffer
12913 positions for characters not in changed text. */
12914 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12915 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12916 int last_unchanged_pos, last_unchanged_pos_old;
12917 struct glyph_row *first_text_row
12918 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12919
12920 *delta = Z - Z_old;
12921 *delta_bytes = Z_BYTE - Z_BYTE_old;
12922
12923 /* Set last_unchanged_pos to the buffer position of the last
12924 character in the buffer that has not been changed. Z is the
12925 index + 1 of the last character in current_buffer, i.e. by
12926 subtracting END_UNCHANGED we get the index of the last
12927 unchanged character, and we have to add BEG to get its buffer
12928 position. */
12929 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12930 last_unchanged_pos_old = last_unchanged_pos - *delta;
12931
12932 /* Search backward from ROW for a row displaying a line that
12933 starts at a minimum position >= last_unchanged_pos_old. */
12934 for (; row > first_text_row; --row)
12935 {
12936 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12937 abort ();
12938
12939 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12940 row_found = row;
12941 }
12942 }
12943
12944 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12945 abort ();
12946
12947 return row_found;
12948 }
12949
12950
12951 /* Make sure that glyph rows in the current matrix of window W
12952 reference the same glyph memory as corresponding rows in the
12953 frame's frame matrix. This function is called after scrolling W's
12954 current matrix on a terminal frame in try_window_id and
12955 try_window_reusing_current_matrix. */
12956
12957 static void
12958 sync_frame_with_window_matrix_rows (w)
12959 struct window *w;
12960 {
12961 struct frame *f = XFRAME (w->frame);
12962 struct glyph_row *window_row, *window_row_end, *frame_row;
12963
12964 /* Preconditions: W must be a leaf window and full-width. Its frame
12965 must have a frame matrix. */
12966 xassert (NILP (w->hchild) && NILP (w->vchild));
12967 xassert (WINDOW_FULL_WIDTH_P (w));
12968 xassert (!FRAME_WINDOW_P (f));
12969
12970 /* If W is a full-width window, glyph pointers in W's current matrix
12971 have, by definition, to be the same as glyph pointers in the
12972 corresponding frame matrix. Note that frame matrices have no
12973 marginal areas (see build_frame_matrix). */
12974 window_row = w->current_matrix->rows;
12975 window_row_end = window_row + w->current_matrix->nrows;
12976 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12977 while (window_row < window_row_end)
12978 {
12979 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12980 struct glyph *end = window_row->glyphs[LAST_AREA];
12981
12982 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12983 frame_row->glyphs[TEXT_AREA] = start;
12984 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12985 frame_row->glyphs[LAST_AREA] = end;
12986
12987 /* Disable frame rows whose corresponding window rows have
12988 been disabled in try_window_id. */
12989 if (!window_row->enabled_p)
12990 frame_row->enabled_p = 0;
12991
12992 ++window_row, ++frame_row;
12993 }
12994 }
12995
12996
12997 /* Find the glyph row in window W containing CHARPOS. Consider all
12998 rows between START and END (not inclusive). END null means search
12999 all rows to the end of the display area of W. Value is the row
13000 containing CHARPOS or null. */
13001
13002 struct glyph_row *
13003 row_containing_pos (w, charpos, start, end, dy)
13004 struct window *w;
13005 int charpos;
13006 struct glyph_row *start, *end;
13007 int dy;
13008 {
13009 struct glyph_row *row = start;
13010 int last_y;
13011
13012 /* If we happen to start on a header-line, skip that. */
13013 if (row->mode_line_p)
13014 ++row;
13015
13016 if ((end && row >= end) || !row->enabled_p)
13017 return NULL;
13018
13019 last_y = window_text_bottom_y (w) - dy;
13020
13021 while (1)
13022 {
13023 /* Give up if we have gone too far. */
13024 if (end && row >= end)
13025 return NULL;
13026 /* This formerly returned if they were equal.
13027 I think that both quantities are of a "last plus one" type;
13028 if so, when they are equal, the row is within the screen. -- rms. */
13029 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13030 return NULL;
13031
13032 /* If it is in this row, return this row. */
13033 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13034 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13035 /* The end position of a row equals the start
13036 position of the next row. If CHARPOS is there, we
13037 would rather display it in the next line, except
13038 when this line ends in ZV. */
13039 && !row->ends_at_zv_p
13040 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13041 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13042 return row;
13043 ++row;
13044 }
13045 }
13046
13047
13048 /* Try to redisplay window W by reusing its existing display. W's
13049 current matrix must be up to date when this function is called,
13050 i.e. window_end_valid must not be nil.
13051
13052 Value is
13053
13054 1 if display has been updated
13055 0 if otherwise unsuccessful
13056 -1 if redisplay with same window start is known not to succeed
13057
13058 The following steps are performed:
13059
13060 1. Find the last row in the current matrix of W that is not
13061 affected by changes at the start of current_buffer. If no such row
13062 is found, give up.
13063
13064 2. Find the first row in W's current matrix that is not affected by
13065 changes at the end of current_buffer. Maybe there is no such row.
13066
13067 3. Display lines beginning with the row + 1 found in step 1 to the
13068 row found in step 2 or, if step 2 didn't find a row, to the end of
13069 the window.
13070
13071 4. If cursor is not known to appear on the window, give up.
13072
13073 5. If display stopped at the row found in step 2, scroll the
13074 display and current matrix as needed.
13075
13076 6. Maybe display some lines at the end of W, if we must. This can
13077 happen under various circumstances, like a partially visible line
13078 becoming fully visible, or because newly displayed lines are displayed
13079 in smaller font sizes.
13080
13081 7. Update W's window end information. */
13082
13083 static int
13084 try_window_id (w)
13085 struct window *w;
13086 {
13087 struct frame *f = XFRAME (w->frame);
13088 struct glyph_matrix *current_matrix = w->current_matrix;
13089 struct glyph_matrix *desired_matrix = w->desired_matrix;
13090 struct glyph_row *last_unchanged_at_beg_row;
13091 struct glyph_row *first_unchanged_at_end_row;
13092 struct glyph_row *row;
13093 struct glyph_row *bottom_row;
13094 int bottom_vpos;
13095 struct it it;
13096 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13097 struct text_pos start_pos;
13098 struct run run;
13099 int first_unchanged_at_end_vpos = 0;
13100 struct glyph_row *last_text_row, *last_text_row_at_end;
13101 struct text_pos start;
13102 int first_changed_charpos, last_changed_charpos;
13103
13104 #if GLYPH_DEBUG
13105 if (inhibit_try_window_id)
13106 return 0;
13107 #endif
13108
13109 /* This is handy for debugging. */
13110 #if 0
13111 #define GIVE_UP(X) \
13112 do { \
13113 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13114 return 0; \
13115 } while (0)
13116 #else
13117 #define GIVE_UP(X) return 0
13118 #endif
13119
13120 SET_TEXT_POS_FROM_MARKER (start, w->start);
13121
13122 /* Don't use this for mini-windows because these can show
13123 messages and mini-buffers, and we don't handle that here. */
13124 if (MINI_WINDOW_P (w))
13125 GIVE_UP (1);
13126
13127 /* This flag is used to prevent redisplay optimizations. */
13128 if (windows_or_buffers_changed || cursor_type_changed)
13129 GIVE_UP (2);
13130
13131 /* Verify that narrowing has not changed.
13132 Also verify that we were not told to prevent redisplay optimizations.
13133 It would be nice to further
13134 reduce the number of cases where this prevents try_window_id. */
13135 if (current_buffer->clip_changed
13136 || current_buffer->prevent_redisplay_optimizations_p)
13137 GIVE_UP (3);
13138
13139 /* Window must either use window-based redisplay or be full width. */
13140 if (!FRAME_WINDOW_P (f)
13141 && (!line_ins_del_ok
13142 || !WINDOW_FULL_WIDTH_P (w)))
13143 GIVE_UP (4);
13144
13145 /* Give up if point is not known NOT to appear in W. */
13146 if (PT < CHARPOS (start))
13147 GIVE_UP (5);
13148
13149 /* Another way to prevent redisplay optimizations. */
13150 if (XFASTINT (w->last_modified) == 0)
13151 GIVE_UP (6);
13152
13153 /* Verify that window is not hscrolled. */
13154 if (XFASTINT (w->hscroll) != 0)
13155 GIVE_UP (7);
13156
13157 /* Verify that display wasn't paused. */
13158 if (NILP (w->window_end_valid))
13159 GIVE_UP (8);
13160
13161 /* Can't use this if highlighting a region because a cursor movement
13162 will do more than just set the cursor. */
13163 if (!NILP (Vtransient_mark_mode)
13164 && !NILP (current_buffer->mark_active))
13165 GIVE_UP (9);
13166
13167 /* Likewise if highlighting trailing whitespace. */
13168 if (!NILP (Vshow_trailing_whitespace))
13169 GIVE_UP (11);
13170
13171 /* Likewise if showing a region. */
13172 if (!NILP (w->region_showing))
13173 GIVE_UP (10);
13174
13175 /* Can use this if overlay arrow position and or string have changed. */
13176 if (overlay_arrows_changed_p ())
13177 GIVE_UP (12);
13178
13179
13180 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13181 only if buffer has really changed. The reason is that the gap is
13182 initially at Z for freshly visited files. The code below would
13183 set end_unchanged to 0 in that case. */
13184 if (MODIFF > SAVE_MODIFF
13185 /* This seems to happen sometimes after saving a buffer. */
13186 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13187 {
13188 if (GPT - BEG < BEG_UNCHANGED)
13189 BEG_UNCHANGED = GPT - BEG;
13190 if (Z - GPT < END_UNCHANGED)
13191 END_UNCHANGED = Z - GPT;
13192 }
13193
13194 /* The position of the first and last character that has been changed. */
13195 first_changed_charpos = BEG + BEG_UNCHANGED;
13196 last_changed_charpos = Z - END_UNCHANGED;
13197
13198 /* If window starts after a line end, and the last change is in
13199 front of that newline, then changes don't affect the display.
13200 This case happens with stealth-fontification. Note that although
13201 the display is unchanged, glyph positions in the matrix have to
13202 be adjusted, of course. */
13203 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13204 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13205 && ((last_changed_charpos < CHARPOS (start)
13206 && CHARPOS (start) == BEGV)
13207 || (last_changed_charpos < CHARPOS (start) - 1
13208 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13209 {
13210 int Z_old, delta, Z_BYTE_old, delta_bytes;
13211 struct glyph_row *r0;
13212
13213 /* Compute how many chars/bytes have been added to or removed
13214 from the buffer. */
13215 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13216 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13217 delta = Z - Z_old;
13218 delta_bytes = Z_BYTE - Z_BYTE_old;
13219
13220 /* Give up if PT is not in the window. Note that it already has
13221 been checked at the start of try_window_id that PT is not in
13222 front of the window start. */
13223 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13224 GIVE_UP (13);
13225
13226 /* If window start is unchanged, we can reuse the whole matrix
13227 as is, after adjusting glyph positions. No need to compute
13228 the window end again, since its offset from Z hasn't changed. */
13229 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13230 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13231 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13232 /* PT must not be in a partially visible line. */
13233 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13234 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13235 {
13236 /* Adjust positions in the glyph matrix. */
13237 if (delta || delta_bytes)
13238 {
13239 struct glyph_row *r1
13240 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13241 increment_matrix_positions (w->current_matrix,
13242 MATRIX_ROW_VPOS (r0, current_matrix),
13243 MATRIX_ROW_VPOS (r1, current_matrix),
13244 delta, delta_bytes);
13245 }
13246
13247 /* Set the cursor. */
13248 row = row_containing_pos (w, PT, r0, NULL, 0);
13249 if (row)
13250 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13251 else
13252 abort ();
13253 return 1;
13254 }
13255 }
13256
13257 /* Handle the case that changes are all below what is displayed in
13258 the window, and that PT is in the window. This shortcut cannot
13259 be taken if ZV is visible in the window, and text has been added
13260 there that is visible in the window. */
13261 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13262 /* ZV is not visible in the window, or there are no
13263 changes at ZV, actually. */
13264 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13265 || first_changed_charpos == last_changed_charpos))
13266 {
13267 struct glyph_row *r0;
13268
13269 /* Give up if PT is not in the window. Note that it already has
13270 been checked at the start of try_window_id that PT is not in
13271 front of the window start. */
13272 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13273 GIVE_UP (14);
13274
13275 /* If window start is unchanged, we can reuse the whole matrix
13276 as is, without changing glyph positions since no text has
13277 been added/removed in front of the window end. */
13278 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13279 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13280 /* PT must not be in a partially visible line. */
13281 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13282 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13283 {
13284 /* We have to compute the window end anew since text
13285 can have been added/removed after it. */
13286 w->window_end_pos
13287 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13288 w->window_end_bytepos
13289 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13290
13291 /* Set the cursor. */
13292 row = row_containing_pos (w, PT, r0, NULL, 0);
13293 if (row)
13294 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13295 else
13296 abort ();
13297 return 2;
13298 }
13299 }
13300
13301 /* Give up if window start is in the changed area.
13302
13303 The condition used to read
13304
13305 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13306
13307 but why that was tested escapes me at the moment. */
13308 if (CHARPOS (start) >= first_changed_charpos
13309 && CHARPOS (start) <= last_changed_charpos)
13310 GIVE_UP (15);
13311
13312 /* Check that window start agrees with the start of the first glyph
13313 row in its current matrix. Check this after we know the window
13314 start is not in changed text, otherwise positions would not be
13315 comparable. */
13316 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13317 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13318 GIVE_UP (16);
13319
13320 /* Give up if the window ends in strings. Overlay strings
13321 at the end are difficult to handle, so don't try. */
13322 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13323 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13324 GIVE_UP (20);
13325
13326 /* Compute the position at which we have to start displaying new
13327 lines. Some of the lines at the top of the window might be
13328 reusable because they are not displaying changed text. Find the
13329 last row in W's current matrix not affected by changes at the
13330 start of current_buffer. Value is null if changes start in the
13331 first line of window. */
13332 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13333 if (last_unchanged_at_beg_row)
13334 {
13335 /* Avoid starting to display in the moddle of a character, a TAB
13336 for instance. This is easier than to set up the iterator
13337 exactly, and it's not a frequent case, so the additional
13338 effort wouldn't really pay off. */
13339 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13340 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13341 && last_unchanged_at_beg_row > w->current_matrix->rows)
13342 --last_unchanged_at_beg_row;
13343
13344 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13345 GIVE_UP (17);
13346
13347 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13348 GIVE_UP (18);
13349 start_pos = it.current.pos;
13350
13351 /* Start displaying new lines in the desired matrix at the same
13352 vpos we would use in the current matrix, i.e. below
13353 last_unchanged_at_beg_row. */
13354 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13355 current_matrix);
13356 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13357 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13358
13359 xassert (it.hpos == 0 && it.current_x == 0);
13360 }
13361 else
13362 {
13363 /* There are no reusable lines at the start of the window.
13364 Start displaying in the first text line. */
13365 start_display (&it, w, start);
13366 it.vpos = it.first_vpos;
13367 start_pos = it.current.pos;
13368 }
13369
13370 /* Find the first row that is not affected by changes at the end of
13371 the buffer. Value will be null if there is no unchanged row, in
13372 which case we must redisplay to the end of the window. delta
13373 will be set to the value by which buffer positions beginning with
13374 first_unchanged_at_end_row have to be adjusted due to text
13375 changes. */
13376 first_unchanged_at_end_row
13377 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13378 IF_DEBUG (debug_delta = delta);
13379 IF_DEBUG (debug_delta_bytes = delta_bytes);
13380
13381 /* Set stop_pos to the buffer position up to which we will have to
13382 display new lines. If first_unchanged_at_end_row != NULL, this
13383 is the buffer position of the start of the line displayed in that
13384 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13385 that we don't stop at a buffer position. */
13386 stop_pos = 0;
13387 if (first_unchanged_at_end_row)
13388 {
13389 xassert (last_unchanged_at_beg_row == NULL
13390 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13391
13392 /* If this is a continuation line, move forward to the next one
13393 that isn't. Changes in lines above affect this line.
13394 Caution: this may move first_unchanged_at_end_row to a row
13395 not displaying text. */
13396 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13397 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13398 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13399 < it.last_visible_y))
13400 ++first_unchanged_at_end_row;
13401
13402 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13403 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13404 >= it.last_visible_y))
13405 first_unchanged_at_end_row = NULL;
13406 else
13407 {
13408 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13409 + delta);
13410 first_unchanged_at_end_vpos
13411 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13412 xassert (stop_pos >= Z - END_UNCHANGED);
13413 }
13414 }
13415 else if (last_unchanged_at_beg_row == NULL)
13416 GIVE_UP (19);
13417
13418
13419 #if GLYPH_DEBUG
13420
13421 /* Either there is no unchanged row at the end, or the one we have
13422 now displays text. This is a necessary condition for the window
13423 end pos calculation at the end of this function. */
13424 xassert (first_unchanged_at_end_row == NULL
13425 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13426
13427 debug_last_unchanged_at_beg_vpos
13428 = (last_unchanged_at_beg_row
13429 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13430 : -1);
13431 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13432
13433 #endif /* GLYPH_DEBUG != 0 */
13434
13435
13436 /* Display new lines. Set last_text_row to the last new line
13437 displayed which has text on it, i.e. might end up as being the
13438 line where the window_end_vpos is. */
13439 w->cursor.vpos = -1;
13440 last_text_row = NULL;
13441 overlay_arrow_seen = 0;
13442 while (it.current_y < it.last_visible_y
13443 && !fonts_changed_p
13444 && (first_unchanged_at_end_row == NULL
13445 || IT_CHARPOS (it) < stop_pos))
13446 {
13447 if (display_line (&it))
13448 last_text_row = it.glyph_row - 1;
13449 }
13450
13451 if (fonts_changed_p)
13452 return -1;
13453
13454
13455 /* Compute differences in buffer positions, y-positions etc. for
13456 lines reused at the bottom of the window. Compute what we can
13457 scroll. */
13458 if (first_unchanged_at_end_row
13459 /* No lines reused because we displayed everything up to the
13460 bottom of the window. */
13461 && it.current_y < it.last_visible_y)
13462 {
13463 dvpos = (it.vpos
13464 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13465 current_matrix));
13466 dy = it.current_y - first_unchanged_at_end_row->y;
13467 run.current_y = first_unchanged_at_end_row->y;
13468 run.desired_y = run.current_y + dy;
13469 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13470 }
13471 else
13472 {
13473 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13474 first_unchanged_at_end_row = NULL;
13475 }
13476 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13477
13478
13479 /* Find the cursor if not already found. We have to decide whether
13480 PT will appear on this window (it sometimes doesn't, but this is
13481 not a very frequent case.) This decision has to be made before
13482 the current matrix is altered. A value of cursor.vpos < 0 means
13483 that PT is either in one of the lines beginning at
13484 first_unchanged_at_end_row or below the window. Don't care for
13485 lines that might be displayed later at the window end; as
13486 mentioned, this is not a frequent case. */
13487 if (w->cursor.vpos < 0)
13488 {
13489 /* Cursor in unchanged rows at the top? */
13490 if (PT < CHARPOS (start_pos)
13491 && last_unchanged_at_beg_row)
13492 {
13493 row = row_containing_pos (w, PT,
13494 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13495 last_unchanged_at_beg_row + 1, 0);
13496 if (row)
13497 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13498 }
13499
13500 /* Start from first_unchanged_at_end_row looking for PT. */
13501 else if (first_unchanged_at_end_row)
13502 {
13503 row = row_containing_pos (w, PT - delta,
13504 first_unchanged_at_end_row, NULL, 0);
13505 if (row)
13506 set_cursor_from_row (w, row, w->current_matrix, delta,
13507 delta_bytes, dy, dvpos);
13508 }
13509
13510 /* Give up if cursor was not found. */
13511 if (w->cursor.vpos < 0)
13512 {
13513 clear_glyph_matrix (w->desired_matrix);
13514 return -1;
13515 }
13516 }
13517
13518 /* Don't let the cursor end in the scroll margins. */
13519 {
13520 int this_scroll_margin, cursor_height;
13521
13522 this_scroll_margin = max (0, scroll_margin);
13523 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13524 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13525 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13526
13527 if ((w->cursor.y < this_scroll_margin
13528 && CHARPOS (start) > BEGV)
13529 /* Old redisplay didn't take scroll margin into account at the bottom,
13530 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13531 || (w->cursor.y + (make_cursor_line_fully_visible_p
13532 ? cursor_height + this_scroll_margin
13533 : 1)) > it.last_visible_y)
13534 {
13535 w->cursor.vpos = -1;
13536 clear_glyph_matrix (w->desired_matrix);
13537 return -1;
13538 }
13539 }
13540
13541 /* Scroll the display. Do it before changing the current matrix so
13542 that xterm.c doesn't get confused about where the cursor glyph is
13543 found. */
13544 if (dy && run.height)
13545 {
13546 update_begin (f);
13547
13548 if (FRAME_WINDOW_P (f))
13549 {
13550 rif->update_window_begin_hook (w);
13551 rif->clear_window_mouse_face (w);
13552 rif->scroll_run_hook (w, &run);
13553 rif->update_window_end_hook (w, 0, 0);
13554 }
13555 else
13556 {
13557 /* Terminal frame. In this case, dvpos gives the number of
13558 lines to scroll by; dvpos < 0 means scroll up. */
13559 int first_unchanged_at_end_vpos
13560 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13561 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13562 int end = (WINDOW_TOP_EDGE_LINE (w)
13563 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13564 + window_internal_height (w));
13565
13566 /* Perform the operation on the screen. */
13567 if (dvpos > 0)
13568 {
13569 /* Scroll last_unchanged_at_beg_row to the end of the
13570 window down dvpos lines. */
13571 set_terminal_window (end);
13572
13573 /* On dumb terminals delete dvpos lines at the end
13574 before inserting dvpos empty lines. */
13575 if (!scroll_region_ok)
13576 ins_del_lines (end - dvpos, -dvpos);
13577
13578 /* Insert dvpos empty lines in front of
13579 last_unchanged_at_beg_row. */
13580 ins_del_lines (from, dvpos);
13581 }
13582 else if (dvpos < 0)
13583 {
13584 /* Scroll up last_unchanged_at_beg_vpos to the end of
13585 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13586 set_terminal_window (end);
13587
13588 /* Delete dvpos lines in front of
13589 last_unchanged_at_beg_vpos. ins_del_lines will set
13590 the cursor to the given vpos and emit |dvpos| delete
13591 line sequences. */
13592 ins_del_lines (from + dvpos, dvpos);
13593
13594 /* On a dumb terminal insert dvpos empty lines at the
13595 end. */
13596 if (!scroll_region_ok)
13597 ins_del_lines (end + dvpos, -dvpos);
13598 }
13599
13600 set_terminal_window (0);
13601 }
13602
13603 update_end (f);
13604 }
13605
13606 /* Shift reused rows of the current matrix to the right position.
13607 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13608 text. */
13609 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13610 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13611 if (dvpos < 0)
13612 {
13613 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13614 bottom_vpos, dvpos);
13615 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13616 bottom_vpos, 0);
13617 }
13618 else if (dvpos > 0)
13619 {
13620 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13621 bottom_vpos, dvpos);
13622 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13623 first_unchanged_at_end_vpos + dvpos, 0);
13624 }
13625
13626 /* For frame-based redisplay, make sure that current frame and window
13627 matrix are in sync with respect to glyph memory. */
13628 if (!FRAME_WINDOW_P (f))
13629 sync_frame_with_window_matrix_rows (w);
13630
13631 /* Adjust buffer positions in reused rows. */
13632 if (delta)
13633 increment_matrix_positions (current_matrix,
13634 first_unchanged_at_end_vpos + dvpos,
13635 bottom_vpos, delta, delta_bytes);
13636
13637 /* Adjust Y positions. */
13638 if (dy)
13639 shift_glyph_matrix (w, current_matrix,
13640 first_unchanged_at_end_vpos + dvpos,
13641 bottom_vpos, dy);
13642
13643 if (first_unchanged_at_end_row)
13644 first_unchanged_at_end_row += dvpos;
13645
13646 /* If scrolling up, there may be some lines to display at the end of
13647 the window. */
13648 last_text_row_at_end = NULL;
13649 if (dy < 0)
13650 {
13651 /* Scrolling up can leave for example a partially visible line
13652 at the end of the window to be redisplayed. */
13653 /* Set last_row to the glyph row in the current matrix where the
13654 window end line is found. It has been moved up or down in
13655 the matrix by dvpos. */
13656 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13657 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13658
13659 /* If last_row is the window end line, it should display text. */
13660 xassert (last_row->displays_text_p);
13661
13662 /* If window end line was partially visible before, begin
13663 displaying at that line. Otherwise begin displaying with the
13664 line following it. */
13665 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13666 {
13667 init_to_row_start (&it, w, last_row);
13668 it.vpos = last_vpos;
13669 it.current_y = last_row->y;
13670 }
13671 else
13672 {
13673 init_to_row_end (&it, w, last_row);
13674 it.vpos = 1 + last_vpos;
13675 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13676 ++last_row;
13677 }
13678
13679 /* We may start in a continuation line. If so, we have to
13680 get the right continuation_lines_width and current_x. */
13681 it.continuation_lines_width = last_row->continuation_lines_width;
13682 it.hpos = it.current_x = 0;
13683
13684 /* Display the rest of the lines at the window end. */
13685 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13686 while (it.current_y < it.last_visible_y
13687 && !fonts_changed_p)
13688 {
13689 /* Is it always sure that the display agrees with lines in
13690 the current matrix? I don't think so, so we mark rows
13691 displayed invalid in the current matrix by setting their
13692 enabled_p flag to zero. */
13693 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13694 if (display_line (&it))
13695 last_text_row_at_end = it.glyph_row - 1;
13696 }
13697 }
13698
13699 /* Update window_end_pos and window_end_vpos. */
13700 if (first_unchanged_at_end_row
13701 && first_unchanged_at_end_row->y < it.last_visible_y
13702 && !last_text_row_at_end)
13703 {
13704 /* Window end line if one of the preserved rows from the current
13705 matrix. Set row to the last row displaying text in current
13706 matrix starting at first_unchanged_at_end_row, after
13707 scrolling. */
13708 xassert (first_unchanged_at_end_row->displays_text_p);
13709 row = find_last_row_displaying_text (w->current_matrix, &it,
13710 first_unchanged_at_end_row);
13711 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13712
13713 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13714 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13715 w->window_end_vpos
13716 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13717 xassert (w->window_end_bytepos >= 0);
13718 IF_DEBUG (debug_method_add (w, "A"));
13719 }
13720 else if (last_text_row_at_end)
13721 {
13722 w->window_end_pos
13723 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13724 w->window_end_bytepos
13725 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13726 w->window_end_vpos
13727 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13728 xassert (w->window_end_bytepos >= 0);
13729 IF_DEBUG (debug_method_add (w, "B"));
13730 }
13731 else if (last_text_row)
13732 {
13733 /* We have displayed either to the end of the window or at the
13734 end of the window, i.e. the last row with text is to be found
13735 in the desired matrix. */
13736 w->window_end_pos
13737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13738 w->window_end_bytepos
13739 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13740 w->window_end_vpos
13741 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13742 xassert (w->window_end_bytepos >= 0);
13743 }
13744 else if (first_unchanged_at_end_row == NULL
13745 && last_text_row == NULL
13746 && last_text_row_at_end == NULL)
13747 {
13748 /* Displayed to end of window, but no line containing text was
13749 displayed. Lines were deleted at the end of the window. */
13750 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13751 int vpos = XFASTINT (w->window_end_vpos);
13752 struct glyph_row *current_row = current_matrix->rows + vpos;
13753 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13754
13755 for (row = NULL;
13756 row == NULL && vpos >= first_vpos;
13757 --vpos, --current_row, --desired_row)
13758 {
13759 if (desired_row->enabled_p)
13760 {
13761 if (desired_row->displays_text_p)
13762 row = desired_row;
13763 }
13764 else if (current_row->displays_text_p)
13765 row = current_row;
13766 }
13767
13768 xassert (row != NULL);
13769 w->window_end_vpos = make_number (vpos + 1);
13770 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13771 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13772 xassert (w->window_end_bytepos >= 0);
13773 IF_DEBUG (debug_method_add (w, "C"));
13774 }
13775 else
13776 abort ();
13777
13778 #if 0 /* This leads to problems, for instance when the cursor is
13779 at ZV, and the cursor line displays no text. */
13780 /* Disable rows below what's displayed in the window. This makes
13781 debugging easier. */
13782 enable_glyph_matrix_rows (current_matrix,
13783 XFASTINT (w->window_end_vpos) + 1,
13784 bottom_vpos, 0);
13785 #endif
13786
13787 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13788 debug_end_vpos = XFASTINT (w->window_end_vpos));
13789
13790 /* Record that display has not been completed. */
13791 w->window_end_valid = Qnil;
13792 w->desired_matrix->no_scrolling_p = 1;
13793 return 3;
13794
13795 #undef GIVE_UP
13796 }
13797
13798
13799 \f
13800 /***********************************************************************
13801 More debugging support
13802 ***********************************************************************/
13803
13804 #if GLYPH_DEBUG
13805
13806 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13807 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13808 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13809
13810
13811 /* Dump the contents of glyph matrix MATRIX on stderr.
13812
13813 GLYPHS 0 means don't show glyph contents.
13814 GLYPHS 1 means show glyphs in short form
13815 GLYPHS > 1 means show glyphs in long form. */
13816
13817 void
13818 dump_glyph_matrix (matrix, glyphs)
13819 struct glyph_matrix *matrix;
13820 int glyphs;
13821 {
13822 int i;
13823 for (i = 0; i < matrix->nrows; ++i)
13824 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13825 }
13826
13827
13828 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13829 the glyph row and area where the glyph comes from. */
13830
13831 void
13832 dump_glyph (row, glyph, area)
13833 struct glyph_row *row;
13834 struct glyph *glyph;
13835 int area;
13836 {
13837 if (glyph->type == CHAR_GLYPH)
13838 {
13839 fprintf (stderr,
13840 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13841 glyph - row->glyphs[TEXT_AREA],
13842 'C',
13843 glyph->charpos,
13844 (BUFFERP (glyph->object)
13845 ? 'B'
13846 : (STRINGP (glyph->object)
13847 ? 'S'
13848 : '-')),
13849 glyph->pixel_width,
13850 glyph->u.ch,
13851 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13852 ? glyph->u.ch
13853 : '.'),
13854 glyph->face_id,
13855 glyph->left_box_line_p,
13856 glyph->right_box_line_p);
13857 }
13858 else if (glyph->type == STRETCH_GLYPH)
13859 {
13860 fprintf (stderr,
13861 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13862 glyph - row->glyphs[TEXT_AREA],
13863 'S',
13864 glyph->charpos,
13865 (BUFFERP (glyph->object)
13866 ? 'B'
13867 : (STRINGP (glyph->object)
13868 ? 'S'
13869 : '-')),
13870 glyph->pixel_width,
13871 0,
13872 '.',
13873 glyph->face_id,
13874 glyph->left_box_line_p,
13875 glyph->right_box_line_p);
13876 }
13877 else if (glyph->type == IMAGE_GLYPH)
13878 {
13879 fprintf (stderr,
13880 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13881 glyph - row->glyphs[TEXT_AREA],
13882 'I',
13883 glyph->charpos,
13884 (BUFFERP (glyph->object)
13885 ? 'B'
13886 : (STRINGP (glyph->object)
13887 ? 'S'
13888 : '-')),
13889 glyph->pixel_width,
13890 glyph->u.img_id,
13891 '.',
13892 glyph->face_id,
13893 glyph->left_box_line_p,
13894 glyph->right_box_line_p);
13895 }
13896 }
13897
13898
13899 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13900 GLYPHS 0 means don't show glyph contents.
13901 GLYPHS 1 means show glyphs in short form
13902 GLYPHS > 1 means show glyphs in long form. */
13903
13904 void
13905 dump_glyph_row (row, vpos, glyphs)
13906 struct glyph_row *row;
13907 int vpos, glyphs;
13908 {
13909 if (glyphs != 1)
13910 {
13911 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13912 fprintf (stderr, "=======================================================================\n");
13913
13914 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13915 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13916 vpos,
13917 MATRIX_ROW_START_CHARPOS (row),
13918 MATRIX_ROW_END_CHARPOS (row),
13919 row->used[TEXT_AREA],
13920 row->contains_overlapping_glyphs_p,
13921 row->enabled_p,
13922 row->truncated_on_left_p,
13923 row->truncated_on_right_p,
13924 row->overlay_arrow_p,
13925 row->continued_p,
13926 MATRIX_ROW_CONTINUATION_LINE_P (row),
13927 row->displays_text_p,
13928 row->ends_at_zv_p,
13929 row->fill_line_p,
13930 row->ends_in_middle_of_char_p,
13931 row->starts_in_middle_of_char_p,
13932 row->mouse_face_p,
13933 row->x,
13934 row->y,
13935 row->pixel_width,
13936 row->height,
13937 row->visible_height,
13938 row->ascent,
13939 row->phys_ascent);
13940 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13941 row->end.overlay_string_index,
13942 row->continuation_lines_width);
13943 fprintf (stderr, "%9d %5d\n",
13944 CHARPOS (row->start.string_pos),
13945 CHARPOS (row->end.string_pos));
13946 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13947 row->end.dpvec_index);
13948 }
13949
13950 if (glyphs > 1)
13951 {
13952 int area;
13953
13954 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13955 {
13956 struct glyph *glyph = row->glyphs[area];
13957 struct glyph *glyph_end = glyph + row->used[area];
13958
13959 /* Glyph for a line end in text. */
13960 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13961 ++glyph_end;
13962
13963 if (glyph < glyph_end)
13964 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13965
13966 for (; glyph < glyph_end; ++glyph)
13967 dump_glyph (row, glyph, area);
13968 }
13969 }
13970 else if (glyphs == 1)
13971 {
13972 int area;
13973
13974 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13975 {
13976 char *s = (char *) alloca (row->used[area] + 1);
13977 int i;
13978
13979 for (i = 0; i < row->used[area]; ++i)
13980 {
13981 struct glyph *glyph = row->glyphs[area] + i;
13982 if (glyph->type == CHAR_GLYPH
13983 && glyph->u.ch < 0x80
13984 && glyph->u.ch >= ' ')
13985 s[i] = glyph->u.ch;
13986 else
13987 s[i] = '.';
13988 }
13989
13990 s[i] = '\0';
13991 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13992 }
13993 }
13994 }
13995
13996
13997 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13998 Sdump_glyph_matrix, 0, 1, "p",
13999 doc: /* Dump the current matrix of the selected window to stderr.
14000 Shows contents of glyph row structures. With non-nil
14001 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14002 glyphs in short form, otherwise show glyphs in long form. */)
14003 (glyphs)
14004 Lisp_Object glyphs;
14005 {
14006 struct window *w = XWINDOW (selected_window);
14007 struct buffer *buffer = XBUFFER (w->buffer);
14008
14009 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14010 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14011 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14012 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14013 fprintf (stderr, "=============================================\n");
14014 dump_glyph_matrix (w->current_matrix,
14015 NILP (glyphs) ? 0 : XINT (glyphs));
14016 return Qnil;
14017 }
14018
14019
14020 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14021 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14022 ()
14023 {
14024 struct frame *f = XFRAME (selected_frame);
14025 dump_glyph_matrix (f->current_matrix, 1);
14026 return Qnil;
14027 }
14028
14029
14030 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14031 doc: /* Dump glyph row ROW to stderr.
14032 GLYPH 0 means don't dump glyphs.
14033 GLYPH 1 means dump glyphs in short form.
14034 GLYPH > 1 or omitted means dump glyphs in long form. */)
14035 (row, glyphs)
14036 Lisp_Object row, glyphs;
14037 {
14038 struct glyph_matrix *matrix;
14039 int vpos;
14040
14041 CHECK_NUMBER (row);
14042 matrix = XWINDOW (selected_window)->current_matrix;
14043 vpos = XINT (row);
14044 if (vpos >= 0 && vpos < matrix->nrows)
14045 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14046 vpos,
14047 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14048 return Qnil;
14049 }
14050
14051
14052 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14053 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14054 GLYPH 0 means don't dump glyphs.
14055 GLYPH 1 means dump glyphs in short form.
14056 GLYPH > 1 or omitted means dump glyphs in long form. */)
14057 (row, glyphs)
14058 Lisp_Object row, glyphs;
14059 {
14060 struct frame *sf = SELECTED_FRAME ();
14061 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14062 int vpos;
14063
14064 CHECK_NUMBER (row);
14065 vpos = XINT (row);
14066 if (vpos >= 0 && vpos < m->nrows)
14067 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14068 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14069 return Qnil;
14070 }
14071
14072
14073 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14074 doc: /* Toggle tracing of redisplay.
14075 With ARG, turn tracing on if and only if ARG is positive. */)
14076 (arg)
14077 Lisp_Object arg;
14078 {
14079 if (NILP (arg))
14080 trace_redisplay_p = !trace_redisplay_p;
14081 else
14082 {
14083 arg = Fprefix_numeric_value (arg);
14084 trace_redisplay_p = XINT (arg) > 0;
14085 }
14086
14087 return Qnil;
14088 }
14089
14090
14091 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14092 doc: /* Like `format', but print result to stderr.
14093 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14094 (nargs, args)
14095 int nargs;
14096 Lisp_Object *args;
14097 {
14098 Lisp_Object s = Fformat (nargs, args);
14099 fprintf (stderr, "%s", SDATA (s));
14100 return Qnil;
14101 }
14102
14103 #endif /* GLYPH_DEBUG */
14104
14105
14106 \f
14107 /***********************************************************************
14108 Building Desired Matrix Rows
14109 ***********************************************************************/
14110
14111 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14112 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14113
14114 static struct glyph_row *
14115 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14116 struct window *w;
14117 Lisp_Object overlay_arrow_string;
14118 {
14119 struct frame *f = XFRAME (WINDOW_FRAME (w));
14120 struct buffer *buffer = XBUFFER (w->buffer);
14121 struct buffer *old = current_buffer;
14122 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14123 int arrow_len = SCHARS (overlay_arrow_string);
14124 const unsigned char *arrow_end = arrow_string + arrow_len;
14125 const unsigned char *p;
14126 struct it it;
14127 int multibyte_p;
14128 int n_glyphs_before;
14129
14130 set_buffer_temp (buffer);
14131 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14132 it.glyph_row->used[TEXT_AREA] = 0;
14133 SET_TEXT_POS (it.position, 0, 0);
14134
14135 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14136 p = arrow_string;
14137 while (p < arrow_end)
14138 {
14139 Lisp_Object face, ilisp;
14140
14141 /* Get the next character. */
14142 if (multibyte_p)
14143 it.c = string_char_and_length (p, arrow_len, &it.len);
14144 else
14145 it.c = *p, it.len = 1;
14146 p += it.len;
14147
14148 /* Get its face. */
14149 ilisp = make_number (p - arrow_string);
14150 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14151 it.face_id = compute_char_face (f, it.c, face);
14152
14153 /* Compute its width, get its glyphs. */
14154 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14155 SET_TEXT_POS (it.position, -1, -1);
14156 PRODUCE_GLYPHS (&it);
14157
14158 /* If this character doesn't fit any more in the line, we have
14159 to remove some glyphs. */
14160 if (it.current_x > it.last_visible_x)
14161 {
14162 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14163 break;
14164 }
14165 }
14166
14167 set_buffer_temp (old);
14168 return it.glyph_row;
14169 }
14170
14171
14172 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14173 glyphs are only inserted for terminal frames since we can't really
14174 win with truncation glyphs when partially visible glyphs are
14175 involved. Which glyphs to insert is determined by
14176 produce_special_glyphs. */
14177
14178 static void
14179 insert_left_trunc_glyphs (it)
14180 struct it *it;
14181 {
14182 struct it truncate_it;
14183 struct glyph *from, *end, *to, *toend;
14184
14185 xassert (!FRAME_WINDOW_P (it->f));
14186
14187 /* Get the truncation glyphs. */
14188 truncate_it = *it;
14189 truncate_it.current_x = 0;
14190 truncate_it.face_id = DEFAULT_FACE_ID;
14191 truncate_it.glyph_row = &scratch_glyph_row;
14192 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14193 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14194 truncate_it.object = make_number (0);
14195 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14196
14197 /* Overwrite glyphs from IT with truncation glyphs. */
14198 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14199 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14200 to = it->glyph_row->glyphs[TEXT_AREA];
14201 toend = to + it->glyph_row->used[TEXT_AREA];
14202
14203 while (from < end)
14204 *to++ = *from++;
14205
14206 /* There may be padding glyphs left over. Overwrite them too. */
14207 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14208 {
14209 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14210 while (from < end)
14211 *to++ = *from++;
14212 }
14213
14214 if (to > toend)
14215 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14216 }
14217
14218
14219 /* Compute the pixel height and width of IT->glyph_row.
14220
14221 Most of the time, ascent and height of a display line will be equal
14222 to the max_ascent and max_height values of the display iterator
14223 structure. This is not the case if
14224
14225 1. We hit ZV without displaying anything. In this case, max_ascent
14226 and max_height will be zero.
14227
14228 2. We have some glyphs that don't contribute to the line height.
14229 (The glyph row flag contributes_to_line_height_p is for future
14230 pixmap extensions).
14231
14232 The first case is easily covered by using default values because in
14233 these cases, the line height does not really matter, except that it
14234 must not be zero. */
14235
14236 static void
14237 compute_line_metrics (it)
14238 struct it *it;
14239 {
14240 struct glyph_row *row = it->glyph_row;
14241 int area, i;
14242
14243 if (FRAME_WINDOW_P (it->f))
14244 {
14245 int i, min_y, max_y;
14246
14247 /* The line may consist of one space only, that was added to
14248 place the cursor on it. If so, the row's height hasn't been
14249 computed yet. */
14250 if (row->height == 0)
14251 {
14252 if (it->max_ascent + it->max_descent == 0)
14253 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14254 row->ascent = it->max_ascent;
14255 row->height = it->max_ascent + it->max_descent;
14256 row->phys_ascent = it->max_phys_ascent;
14257 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14258 row->extra_line_spacing = it->max_extra_line_spacing;
14259 }
14260
14261 /* Compute the width of this line. */
14262 row->pixel_width = row->x;
14263 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14264 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14265
14266 xassert (row->pixel_width >= 0);
14267 xassert (row->ascent >= 0 && row->height > 0);
14268
14269 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14270 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14271
14272 /* If first line's physical ascent is larger than its logical
14273 ascent, use the physical ascent, and make the row taller.
14274 This makes accented characters fully visible. */
14275 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14276 && row->phys_ascent > row->ascent)
14277 {
14278 row->height += row->phys_ascent - row->ascent;
14279 row->ascent = row->phys_ascent;
14280 }
14281
14282 /* Compute how much of the line is visible. */
14283 row->visible_height = row->height;
14284
14285 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14286 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14287
14288 if (row->y < min_y)
14289 row->visible_height -= min_y - row->y;
14290 if (row->y + row->height > max_y)
14291 row->visible_height -= row->y + row->height - max_y;
14292 }
14293 else
14294 {
14295 row->pixel_width = row->used[TEXT_AREA];
14296 if (row->continued_p)
14297 row->pixel_width -= it->continuation_pixel_width;
14298 else if (row->truncated_on_right_p)
14299 row->pixel_width -= it->truncation_pixel_width;
14300 row->ascent = row->phys_ascent = 0;
14301 row->height = row->phys_height = row->visible_height = 1;
14302 row->extra_line_spacing = 0;
14303 }
14304
14305 /* Compute a hash code for this row. */
14306 row->hash = 0;
14307 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14308 for (i = 0; i < row->used[area]; ++i)
14309 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14310 + row->glyphs[area][i].u.val
14311 + row->glyphs[area][i].face_id
14312 + row->glyphs[area][i].padding_p
14313 + (row->glyphs[area][i].type << 2));
14314
14315 it->max_ascent = it->max_descent = 0;
14316 it->max_phys_ascent = it->max_phys_descent = 0;
14317 }
14318
14319
14320 /* Append one space to the glyph row of iterator IT if doing a
14321 window-based redisplay. The space has the same face as
14322 IT->face_id. Value is non-zero if a space was added.
14323
14324 This function is called to make sure that there is always one glyph
14325 at the end of a glyph row that the cursor can be set on under
14326 window-systems. (If there weren't such a glyph we would not know
14327 how wide and tall a box cursor should be displayed).
14328
14329 At the same time this space let's a nicely handle clearing to the
14330 end of the line if the row ends in italic text. */
14331
14332 static int
14333 append_space_for_newline (it, default_face_p)
14334 struct it *it;
14335 int default_face_p;
14336 {
14337 if (FRAME_WINDOW_P (it->f))
14338 {
14339 int n = it->glyph_row->used[TEXT_AREA];
14340
14341 if (it->glyph_row->glyphs[TEXT_AREA] + n
14342 < it->glyph_row->glyphs[1 + TEXT_AREA])
14343 {
14344 /* Save some values that must not be changed.
14345 Must save IT->c and IT->len because otherwise
14346 ITERATOR_AT_END_P wouldn't work anymore after
14347 append_space_for_newline has been called. */
14348 enum display_element_type saved_what = it->what;
14349 int saved_c = it->c, saved_len = it->len;
14350 int saved_x = it->current_x;
14351 int saved_face_id = it->face_id;
14352 struct text_pos saved_pos;
14353 Lisp_Object saved_object;
14354 struct face *face;
14355
14356 saved_object = it->object;
14357 saved_pos = it->position;
14358
14359 it->what = IT_CHARACTER;
14360 bzero (&it->position, sizeof it->position);
14361 it->object = make_number (0);
14362 it->c = ' ';
14363 it->len = 1;
14364
14365 if (default_face_p)
14366 it->face_id = DEFAULT_FACE_ID;
14367 else if (it->face_before_selective_p)
14368 it->face_id = it->saved_face_id;
14369 face = FACE_FROM_ID (it->f, it->face_id);
14370 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14371
14372 PRODUCE_GLYPHS (it);
14373
14374 it->override_ascent = -1;
14375 it->constrain_row_ascent_descent_p = 0;
14376 it->current_x = saved_x;
14377 it->object = saved_object;
14378 it->position = saved_pos;
14379 it->what = saved_what;
14380 it->face_id = saved_face_id;
14381 it->len = saved_len;
14382 it->c = saved_c;
14383 return 1;
14384 }
14385 }
14386
14387 return 0;
14388 }
14389
14390
14391 /* Extend the face of the last glyph in the text area of IT->glyph_row
14392 to the end of the display line. Called from display_line.
14393 If the glyph row is empty, add a space glyph to it so that we
14394 know the face to draw. Set the glyph row flag fill_line_p. */
14395
14396 static void
14397 extend_face_to_end_of_line (it)
14398 struct it *it;
14399 {
14400 struct face *face;
14401 struct frame *f = it->f;
14402
14403 /* If line is already filled, do nothing. */
14404 if (it->current_x >= it->last_visible_x)
14405 return;
14406
14407 /* Face extension extends the background and box of IT->face_id
14408 to the end of the line. If the background equals the background
14409 of the frame, we don't have to do anything. */
14410 if (it->face_before_selective_p)
14411 face = FACE_FROM_ID (it->f, it->saved_face_id);
14412 else
14413 face = FACE_FROM_ID (f, it->face_id);
14414
14415 if (FRAME_WINDOW_P (f)
14416 && face->box == FACE_NO_BOX
14417 && face->background == FRAME_BACKGROUND_PIXEL (f)
14418 && !face->stipple)
14419 return;
14420
14421 /* Set the glyph row flag indicating that the face of the last glyph
14422 in the text area has to be drawn to the end of the text area. */
14423 it->glyph_row->fill_line_p = 1;
14424
14425 /* If current character of IT is not ASCII, make sure we have the
14426 ASCII face. This will be automatically undone the next time
14427 get_next_display_element returns a multibyte character. Note
14428 that the character will always be single byte in unibyte text. */
14429 if (!SINGLE_BYTE_CHAR_P (it->c))
14430 {
14431 it->face_id = FACE_FOR_CHAR (f, face, 0);
14432 }
14433
14434 if (FRAME_WINDOW_P (f))
14435 {
14436 /* If the row is empty, add a space with the current face of IT,
14437 so that we know which face to draw. */
14438 if (it->glyph_row->used[TEXT_AREA] == 0)
14439 {
14440 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14441 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14442 it->glyph_row->used[TEXT_AREA] = 1;
14443 }
14444 }
14445 else
14446 {
14447 /* Save some values that must not be changed. */
14448 int saved_x = it->current_x;
14449 struct text_pos saved_pos;
14450 Lisp_Object saved_object;
14451 enum display_element_type saved_what = it->what;
14452 int saved_face_id = it->face_id;
14453
14454 saved_object = it->object;
14455 saved_pos = it->position;
14456
14457 it->what = IT_CHARACTER;
14458 bzero (&it->position, sizeof it->position);
14459 it->object = make_number (0);
14460 it->c = ' ';
14461 it->len = 1;
14462 it->face_id = face->id;
14463
14464 PRODUCE_GLYPHS (it);
14465
14466 while (it->current_x <= it->last_visible_x)
14467 PRODUCE_GLYPHS (it);
14468
14469 /* Don't count these blanks really. It would let us insert a left
14470 truncation glyph below and make us set the cursor on them, maybe. */
14471 it->current_x = saved_x;
14472 it->object = saved_object;
14473 it->position = saved_pos;
14474 it->what = saved_what;
14475 it->face_id = saved_face_id;
14476 }
14477 }
14478
14479
14480 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14481 trailing whitespace. */
14482
14483 static int
14484 trailing_whitespace_p (charpos)
14485 int charpos;
14486 {
14487 int bytepos = CHAR_TO_BYTE (charpos);
14488 int c = 0;
14489
14490 while (bytepos < ZV_BYTE
14491 && (c = FETCH_CHAR (bytepos),
14492 c == ' ' || c == '\t'))
14493 ++bytepos;
14494
14495 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14496 {
14497 if (bytepos != PT_BYTE)
14498 return 1;
14499 }
14500 return 0;
14501 }
14502
14503
14504 /* Highlight trailing whitespace, if any, in ROW. */
14505
14506 void
14507 highlight_trailing_whitespace (f, row)
14508 struct frame *f;
14509 struct glyph_row *row;
14510 {
14511 int used = row->used[TEXT_AREA];
14512
14513 if (used)
14514 {
14515 struct glyph *start = row->glyphs[TEXT_AREA];
14516 struct glyph *glyph = start + used - 1;
14517
14518 /* Skip over glyphs inserted to display the cursor at the
14519 end of a line, for extending the face of the last glyph
14520 to the end of the line on terminals, and for truncation
14521 and continuation glyphs. */
14522 while (glyph >= start
14523 && glyph->type == CHAR_GLYPH
14524 && INTEGERP (glyph->object))
14525 --glyph;
14526
14527 /* If last glyph is a space or stretch, and it's trailing
14528 whitespace, set the face of all trailing whitespace glyphs in
14529 IT->glyph_row to `trailing-whitespace'. */
14530 if (glyph >= start
14531 && BUFFERP (glyph->object)
14532 && (glyph->type == STRETCH_GLYPH
14533 || (glyph->type == CHAR_GLYPH
14534 && glyph->u.ch == ' '))
14535 && trailing_whitespace_p (glyph->charpos))
14536 {
14537 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14538
14539 while (glyph >= start
14540 && BUFFERP (glyph->object)
14541 && (glyph->type == STRETCH_GLYPH
14542 || (glyph->type == CHAR_GLYPH
14543 && glyph->u.ch == ' ')))
14544 (glyph--)->face_id = face_id;
14545 }
14546 }
14547 }
14548
14549
14550 /* Value is non-zero if glyph row ROW in window W should be
14551 used to hold the cursor. */
14552
14553 static int
14554 cursor_row_p (w, row)
14555 struct window *w;
14556 struct glyph_row *row;
14557 {
14558 int cursor_row_p = 1;
14559
14560 if (PT == MATRIX_ROW_END_CHARPOS (row))
14561 {
14562 /* If the row ends with a newline from a string, we don't want
14563 the cursor there (if the row is continued it doesn't end in a
14564 newline). */
14565 if (CHARPOS (row->end.string_pos) >= 0
14566 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14567 cursor_row_p = row->continued_p;
14568
14569 /* If the row ends at ZV, display the cursor at the end of that
14570 row instead of at the start of the row below. */
14571 else if (row->ends_at_zv_p)
14572 cursor_row_p = 1;
14573 else
14574 cursor_row_p = 0;
14575 }
14576
14577 return cursor_row_p;
14578 }
14579
14580
14581 /* Construct the glyph row IT->glyph_row in the desired matrix of
14582 IT->w from text at the current position of IT. See dispextern.h
14583 for an overview of struct it. Value is non-zero if
14584 IT->glyph_row displays text, as opposed to a line displaying ZV
14585 only. */
14586
14587 static int
14588 display_line (it)
14589 struct it *it;
14590 {
14591 struct glyph_row *row = it->glyph_row;
14592 int overlay_arrow_bitmap;
14593 Lisp_Object overlay_arrow_string;
14594
14595 /* We always start displaying at hpos zero even if hscrolled. */
14596 xassert (it->hpos == 0 && it->current_x == 0);
14597
14598 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14599 >= it->w->desired_matrix->nrows)
14600 {
14601 it->w->nrows_scale_factor++;
14602 fonts_changed_p = 1;
14603 return 0;
14604 }
14605
14606 /* Is IT->w showing the region? */
14607 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14608
14609 /* Clear the result glyph row and enable it. */
14610 prepare_desired_row (row);
14611
14612 row->y = it->current_y;
14613 row->start = it->start;
14614 row->continuation_lines_width = it->continuation_lines_width;
14615 row->displays_text_p = 1;
14616 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14617 it->starts_in_middle_of_char_p = 0;
14618
14619 /* Arrange the overlays nicely for our purposes. Usually, we call
14620 display_line on only one line at a time, in which case this
14621 can't really hurt too much, or we call it on lines which appear
14622 one after another in the buffer, in which case all calls to
14623 recenter_overlay_lists but the first will be pretty cheap. */
14624 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14625
14626 /* Move over display elements that are not visible because we are
14627 hscrolled. This may stop at an x-position < IT->first_visible_x
14628 if the first glyph is partially visible or if we hit a line end. */
14629 if (it->current_x < it->first_visible_x)
14630 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14631 MOVE_TO_POS | MOVE_TO_X);
14632
14633 /* Get the initial row height. This is either the height of the
14634 text hscrolled, if there is any, or zero. */
14635 row->ascent = it->max_ascent;
14636 row->height = it->max_ascent + it->max_descent;
14637 row->phys_ascent = it->max_phys_ascent;
14638 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14639 row->extra_line_spacing = it->max_extra_line_spacing;
14640
14641 /* Loop generating characters. The loop is left with IT on the next
14642 character to display. */
14643 while (1)
14644 {
14645 int n_glyphs_before, hpos_before, x_before;
14646 int x, i, nglyphs;
14647 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14648
14649 /* Retrieve the next thing to display. Value is zero if end of
14650 buffer reached. */
14651 if (!get_next_display_element (it))
14652 {
14653 /* Maybe add a space at the end of this line that is used to
14654 display the cursor there under X. Set the charpos of the
14655 first glyph of blank lines not corresponding to any text
14656 to -1. */
14657 #ifdef HAVE_WINDOW_SYSTEM
14658 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14659 row->exact_window_width_line_p = 1;
14660 else
14661 #endif /* HAVE_WINDOW_SYSTEM */
14662 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14663 || row->used[TEXT_AREA] == 0)
14664 {
14665 row->glyphs[TEXT_AREA]->charpos = -1;
14666 row->displays_text_p = 0;
14667
14668 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14669 && (!MINI_WINDOW_P (it->w)
14670 || (minibuf_level && EQ (it->window, minibuf_window))))
14671 row->indicate_empty_line_p = 1;
14672 }
14673
14674 it->continuation_lines_width = 0;
14675 row->ends_at_zv_p = 1;
14676 break;
14677 }
14678
14679 /* Now, get the metrics of what we want to display. This also
14680 generates glyphs in `row' (which is IT->glyph_row). */
14681 n_glyphs_before = row->used[TEXT_AREA];
14682 x = it->current_x;
14683
14684 /* Remember the line height so far in case the next element doesn't
14685 fit on the line. */
14686 if (!it->truncate_lines_p)
14687 {
14688 ascent = it->max_ascent;
14689 descent = it->max_descent;
14690 phys_ascent = it->max_phys_ascent;
14691 phys_descent = it->max_phys_descent;
14692 }
14693
14694 PRODUCE_GLYPHS (it);
14695
14696 /* If this display element was in marginal areas, continue with
14697 the next one. */
14698 if (it->area != TEXT_AREA)
14699 {
14700 row->ascent = max (row->ascent, it->max_ascent);
14701 row->height = max (row->height, it->max_ascent + it->max_descent);
14702 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14703 row->phys_height = max (row->phys_height,
14704 it->max_phys_ascent + it->max_phys_descent);
14705 row->extra_line_spacing = max (row->extra_line_spacing,
14706 it->max_extra_line_spacing);
14707 set_iterator_to_next (it, 1);
14708 continue;
14709 }
14710
14711 /* Does the display element fit on the line? If we truncate
14712 lines, we should draw past the right edge of the window. If
14713 we don't truncate, we want to stop so that we can display the
14714 continuation glyph before the right margin. If lines are
14715 continued, there are two possible strategies for characters
14716 resulting in more than 1 glyph (e.g. tabs): Display as many
14717 glyphs as possible in this line and leave the rest for the
14718 continuation line, or display the whole element in the next
14719 line. Original redisplay did the former, so we do it also. */
14720 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14721 hpos_before = it->hpos;
14722 x_before = x;
14723
14724 if (/* Not a newline. */
14725 nglyphs > 0
14726 /* Glyphs produced fit entirely in the line. */
14727 && it->current_x < it->last_visible_x)
14728 {
14729 it->hpos += nglyphs;
14730 row->ascent = max (row->ascent, it->max_ascent);
14731 row->height = max (row->height, it->max_ascent + it->max_descent);
14732 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14733 row->phys_height = max (row->phys_height,
14734 it->max_phys_ascent + it->max_phys_descent);
14735 row->extra_line_spacing = max (row->extra_line_spacing,
14736 it->max_extra_line_spacing);
14737 if (it->current_x - it->pixel_width < it->first_visible_x)
14738 row->x = x - it->first_visible_x;
14739 }
14740 else
14741 {
14742 int new_x;
14743 struct glyph *glyph;
14744
14745 for (i = 0; i < nglyphs; ++i, x = new_x)
14746 {
14747 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14748 new_x = x + glyph->pixel_width;
14749
14750 if (/* Lines are continued. */
14751 !it->truncate_lines_p
14752 && (/* Glyph doesn't fit on the line. */
14753 new_x > it->last_visible_x
14754 /* Or it fits exactly on a window system frame. */
14755 || (new_x == it->last_visible_x
14756 && FRAME_WINDOW_P (it->f))))
14757 {
14758 /* End of a continued line. */
14759
14760 if (it->hpos == 0
14761 || (new_x == it->last_visible_x
14762 && FRAME_WINDOW_P (it->f)))
14763 {
14764 /* Current glyph is the only one on the line or
14765 fits exactly on the line. We must continue
14766 the line because we can't draw the cursor
14767 after the glyph. */
14768 row->continued_p = 1;
14769 it->current_x = new_x;
14770 it->continuation_lines_width += new_x;
14771 ++it->hpos;
14772 if (i == nglyphs - 1)
14773 {
14774 set_iterator_to_next (it, 1);
14775 #ifdef HAVE_WINDOW_SYSTEM
14776 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14777 {
14778 if (!get_next_display_element (it))
14779 {
14780 row->exact_window_width_line_p = 1;
14781 it->continuation_lines_width = 0;
14782 row->continued_p = 0;
14783 row->ends_at_zv_p = 1;
14784 }
14785 else if (ITERATOR_AT_END_OF_LINE_P (it))
14786 {
14787 row->continued_p = 0;
14788 row->exact_window_width_line_p = 1;
14789 }
14790 else if (it->method == next_element_from_display_vector)
14791 it->face_id = it->saved_face_id;
14792 }
14793 #endif /* HAVE_WINDOW_SYSTEM */
14794 }
14795 }
14796 else if (CHAR_GLYPH_PADDING_P (*glyph)
14797 && !FRAME_WINDOW_P (it->f))
14798 {
14799 /* A padding glyph that doesn't fit on this line.
14800 This means the whole character doesn't fit
14801 on the line. */
14802 row->used[TEXT_AREA] = n_glyphs_before;
14803
14804 /* Fill the rest of the row with continuation
14805 glyphs like in 20.x. */
14806 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14807 < row->glyphs[1 + TEXT_AREA])
14808 produce_special_glyphs (it, IT_CONTINUATION);
14809
14810 row->continued_p = 1;
14811 it->current_x = x_before;
14812 it->continuation_lines_width += x_before;
14813
14814 /* Restore the height to what it was before the
14815 element not fitting on the line. */
14816 it->max_ascent = ascent;
14817 it->max_descent = descent;
14818 it->max_phys_ascent = phys_ascent;
14819 it->max_phys_descent = phys_descent;
14820 }
14821 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14822 {
14823 /* A TAB that extends past the right edge of the
14824 window. This produces a single glyph on
14825 window system frames. We leave the glyph in
14826 this row and let it fill the row, but don't
14827 consume the TAB. */
14828 it->continuation_lines_width += it->last_visible_x;
14829 row->ends_in_middle_of_char_p = 1;
14830 row->continued_p = 1;
14831 glyph->pixel_width = it->last_visible_x - x;
14832 it->starts_in_middle_of_char_p = 1;
14833 }
14834 else
14835 {
14836 /* Something other than a TAB that draws past
14837 the right edge of the window. Restore
14838 positions to values before the element. */
14839 row->used[TEXT_AREA] = n_glyphs_before + i;
14840
14841 /* Display continuation glyphs. */
14842 if (!FRAME_WINDOW_P (it->f))
14843 produce_special_glyphs (it, IT_CONTINUATION);
14844 row->continued_p = 1;
14845
14846 it->continuation_lines_width += x;
14847
14848 if (nglyphs > 1 && i > 0)
14849 {
14850 row->ends_in_middle_of_char_p = 1;
14851 it->starts_in_middle_of_char_p = 1;
14852 }
14853
14854 /* Restore the height to what it was before the
14855 element not fitting on the line. */
14856 it->max_ascent = ascent;
14857 it->max_descent = descent;
14858 it->max_phys_ascent = phys_ascent;
14859 it->max_phys_descent = phys_descent;
14860 }
14861
14862 break;
14863 }
14864 else if (new_x > it->first_visible_x)
14865 {
14866 /* Increment number of glyphs actually displayed. */
14867 ++it->hpos;
14868
14869 if (x < it->first_visible_x)
14870 /* Glyph is partially visible, i.e. row starts at
14871 negative X position. */
14872 row->x = x - it->first_visible_x;
14873 }
14874 else
14875 {
14876 /* Glyph is completely off the left margin of the
14877 window. This should not happen because of the
14878 move_it_in_display_line at the start of this
14879 function, unless the text display area of the
14880 window is empty. */
14881 xassert (it->first_visible_x <= it->last_visible_x);
14882 }
14883 }
14884
14885 row->ascent = max (row->ascent, it->max_ascent);
14886 row->height = max (row->height, it->max_ascent + it->max_descent);
14887 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14888 row->phys_height = max (row->phys_height,
14889 it->max_phys_ascent + it->max_phys_descent);
14890 row->extra_line_spacing = max (row->extra_line_spacing,
14891 it->max_extra_line_spacing);
14892
14893 /* End of this display line if row is continued. */
14894 if (row->continued_p || row->ends_at_zv_p)
14895 break;
14896 }
14897
14898 at_end_of_line:
14899 /* Is this a line end? If yes, we're also done, after making
14900 sure that a non-default face is extended up to the right
14901 margin of the window. */
14902 if (ITERATOR_AT_END_OF_LINE_P (it))
14903 {
14904 int used_before = row->used[TEXT_AREA];
14905
14906 row->ends_in_newline_from_string_p = STRINGP (it->object);
14907
14908 #ifdef HAVE_WINDOW_SYSTEM
14909 /* Add a space at the end of the line that is used to
14910 display the cursor there. */
14911 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14912 append_space_for_newline (it, 0);
14913 #endif /* HAVE_WINDOW_SYSTEM */
14914
14915 /* Extend the face to the end of the line. */
14916 extend_face_to_end_of_line (it);
14917
14918 /* Make sure we have the position. */
14919 if (used_before == 0)
14920 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14921
14922 /* Consume the line end. This skips over invisible lines. */
14923 set_iterator_to_next (it, 1);
14924 it->continuation_lines_width = 0;
14925 break;
14926 }
14927
14928 /* Proceed with next display element. Note that this skips
14929 over lines invisible because of selective display. */
14930 set_iterator_to_next (it, 1);
14931
14932 /* If we truncate lines, we are done when the last displayed
14933 glyphs reach past the right margin of the window. */
14934 if (it->truncate_lines_p
14935 && (FRAME_WINDOW_P (it->f)
14936 ? (it->current_x >= it->last_visible_x)
14937 : (it->current_x > it->last_visible_x)))
14938 {
14939 /* Maybe add truncation glyphs. */
14940 if (!FRAME_WINDOW_P (it->f))
14941 {
14942 int i, n;
14943
14944 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14945 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14946 break;
14947
14948 for (n = row->used[TEXT_AREA]; i < n; ++i)
14949 {
14950 row->used[TEXT_AREA] = i;
14951 produce_special_glyphs (it, IT_TRUNCATION);
14952 }
14953 }
14954 #ifdef HAVE_WINDOW_SYSTEM
14955 else
14956 {
14957 /* Don't truncate if we can overflow newline into fringe. */
14958 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14959 {
14960 if (!get_next_display_element (it))
14961 {
14962 it->continuation_lines_width = 0;
14963 row->ends_at_zv_p = 1;
14964 row->exact_window_width_line_p = 1;
14965 break;
14966 }
14967 if (ITERATOR_AT_END_OF_LINE_P (it))
14968 {
14969 row->exact_window_width_line_p = 1;
14970 goto at_end_of_line;
14971 }
14972 }
14973 }
14974 #endif /* HAVE_WINDOW_SYSTEM */
14975
14976 row->truncated_on_right_p = 1;
14977 it->continuation_lines_width = 0;
14978 reseat_at_next_visible_line_start (it, 0);
14979 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14980 it->hpos = hpos_before;
14981 it->current_x = x_before;
14982 break;
14983 }
14984 }
14985
14986 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14987 at the left window margin. */
14988 if (it->first_visible_x
14989 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14990 {
14991 if (!FRAME_WINDOW_P (it->f))
14992 insert_left_trunc_glyphs (it);
14993 row->truncated_on_left_p = 1;
14994 }
14995
14996 /* If the start of this line is the overlay arrow-position, then
14997 mark this glyph row as the one containing the overlay arrow.
14998 This is clearly a mess with variable size fonts. It would be
14999 better to let it be displayed like cursors under X. */
15000 if (! overlay_arrow_seen
15001 && (overlay_arrow_string
15002 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15003 !NILP (overlay_arrow_string)))
15004 {
15005 /* Overlay arrow in window redisplay is a fringe bitmap. */
15006 if (STRINGP (overlay_arrow_string))
15007 {
15008 struct glyph_row *arrow_row
15009 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15010 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15011 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15012 struct glyph *p = row->glyphs[TEXT_AREA];
15013 struct glyph *p2, *end;
15014
15015 /* Copy the arrow glyphs. */
15016 while (glyph < arrow_end)
15017 *p++ = *glyph++;
15018
15019 /* Throw away padding glyphs. */
15020 p2 = p;
15021 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15022 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15023 ++p2;
15024 if (p2 > p)
15025 {
15026 while (p2 < end)
15027 *p++ = *p2++;
15028 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15029 }
15030 }
15031 else
15032 {
15033 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15034 row->overlay_arrow_p = 1;
15035 }
15036 overlay_arrow_seen = 1;
15037 }
15038
15039 /* Compute pixel dimensions of this line. */
15040 compute_line_metrics (it);
15041
15042 /* Remember the position at which this line ends. */
15043 row->end = it->current;
15044
15045 /* Save fringe bitmaps in this row. */
15046 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15047 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15048 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15049 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15050
15051 it->left_user_fringe_bitmap = 0;
15052 it->left_user_fringe_face_id = 0;
15053 it->right_user_fringe_bitmap = 0;
15054 it->right_user_fringe_face_id = 0;
15055
15056 /* Maybe set the cursor. */
15057 if (it->w->cursor.vpos < 0
15058 && PT >= MATRIX_ROW_START_CHARPOS (row)
15059 && PT <= MATRIX_ROW_END_CHARPOS (row)
15060 && cursor_row_p (it->w, row))
15061 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15062
15063 /* Highlight trailing whitespace. */
15064 if (!NILP (Vshow_trailing_whitespace))
15065 highlight_trailing_whitespace (it->f, it->glyph_row);
15066
15067 /* Prepare for the next line. This line starts horizontally at (X
15068 HPOS) = (0 0). Vertical positions are incremented. As a
15069 convenience for the caller, IT->glyph_row is set to the next
15070 row to be used. */
15071 it->current_x = it->hpos = 0;
15072 it->current_y += row->height;
15073 ++it->vpos;
15074 ++it->glyph_row;
15075 it->start = it->current;
15076 return row->displays_text_p;
15077 }
15078
15079
15080 \f
15081 /***********************************************************************
15082 Menu Bar
15083 ***********************************************************************/
15084
15085 /* Redisplay the menu bar in the frame for window W.
15086
15087 The menu bar of X frames that don't have X toolkit support is
15088 displayed in a special window W->frame->menu_bar_window.
15089
15090 The menu bar of terminal frames is treated specially as far as
15091 glyph matrices are concerned. Menu bar lines are not part of
15092 windows, so the update is done directly on the frame matrix rows
15093 for the menu bar. */
15094
15095 static void
15096 display_menu_bar (w)
15097 struct window *w;
15098 {
15099 struct frame *f = XFRAME (WINDOW_FRAME (w));
15100 struct it it;
15101 Lisp_Object items;
15102 int i;
15103
15104 /* Don't do all this for graphical frames. */
15105 #ifdef HAVE_NTGUI
15106 if (!NILP (Vwindow_system))
15107 return;
15108 #endif
15109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15110 if (FRAME_X_P (f))
15111 return;
15112 #endif
15113 #ifdef MAC_OS
15114 if (FRAME_MAC_P (f))
15115 return;
15116 #endif
15117
15118 #ifdef USE_X_TOOLKIT
15119 xassert (!FRAME_WINDOW_P (f));
15120 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15121 it.first_visible_x = 0;
15122 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15123 #else /* not USE_X_TOOLKIT */
15124 if (FRAME_WINDOW_P (f))
15125 {
15126 /* Menu bar lines are displayed in the desired matrix of the
15127 dummy window menu_bar_window. */
15128 struct window *menu_w;
15129 xassert (WINDOWP (f->menu_bar_window));
15130 menu_w = XWINDOW (f->menu_bar_window);
15131 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15132 MENU_FACE_ID);
15133 it.first_visible_x = 0;
15134 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15135 }
15136 else
15137 {
15138 /* This is a TTY frame, i.e. character hpos/vpos are used as
15139 pixel x/y. */
15140 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15141 MENU_FACE_ID);
15142 it.first_visible_x = 0;
15143 it.last_visible_x = FRAME_COLS (f);
15144 }
15145 #endif /* not USE_X_TOOLKIT */
15146
15147 if (! mode_line_inverse_video)
15148 /* Force the menu-bar to be displayed in the default face. */
15149 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15150
15151 /* Clear all rows of the menu bar. */
15152 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15153 {
15154 struct glyph_row *row = it.glyph_row + i;
15155 clear_glyph_row (row);
15156 row->enabled_p = 1;
15157 row->full_width_p = 1;
15158 }
15159
15160 /* Display all items of the menu bar. */
15161 items = FRAME_MENU_BAR_ITEMS (it.f);
15162 for (i = 0; i < XVECTOR (items)->size; i += 4)
15163 {
15164 Lisp_Object string;
15165
15166 /* Stop at nil string. */
15167 string = AREF (items, i + 1);
15168 if (NILP (string))
15169 break;
15170
15171 /* Remember where item was displayed. */
15172 AREF (items, i + 3) = make_number (it.hpos);
15173
15174 /* Display the item, pad with one space. */
15175 if (it.current_x < it.last_visible_x)
15176 display_string (NULL, string, Qnil, 0, 0, &it,
15177 SCHARS (string) + 1, 0, 0, -1);
15178 }
15179
15180 /* Fill out the line with spaces. */
15181 if (it.current_x < it.last_visible_x)
15182 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15183
15184 /* Compute the total height of the lines. */
15185 compute_line_metrics (&it);
15186 }
15187
15188
15189 \f
15190 /***********************************************************************
15191 Mode Line
15192 ***********************************************************************/
15193
15194 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15195 FORCE is non-zero, redisplay mode lines unconditionally.
15196 Otherwise, redisplay only mode lines that are garbaged. Value is
15197 the number of windows whose mode lines were redisplayed. */
15198
15199 static int
15200 redisplay_mode_lines (window, force)
15201 Lisp_Object window;
15202 int force;
15203 {
15204 int nwindows = 0;
15205
15206 while (!NILP (window))
15207 {
15208 struct window *w = XWINDOW (window);
15209
15210 if (WINDOWP (w->hchild))
15211 nwindows += redisplay_mode_lines (w->hchild, force);
15212 else if (WINDOWP (w->vchild))
15213 nwindows += redisplay_mode_lines (w->vchild, force);
15214 else if (force
15215 || FRAME_GARBAGED_P (XFRAME (w->frame))
15216 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15217 {
15218 struct text_pos lpoint;
15219 struct buffer *old = current_buffer;
15220
15221 /* Set the window's buffer for the mode line display. */
15222 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15223 set_buffer_internal_1 (XBUFFER (w->buffer));
15224
15225 /* Point refers normally to the selected window. For any
15226 other window, set up appropriate value. */
15227 if (!EQ (window, selected_window))
15228 {
15229 struct text_pos pt;
15230
15231 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15232 if (CHARPOS (pt) < BEGV)
15233 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15234 else if (CHARPOS (pt) > (ZV - 1))
15235 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15236 else
15237 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15238 }
15239
15240 /* Display mode lines. */
15241 clear_glyph_matrix (w->desired_matrix);
15242 if (display_mode_lines (w))
15243 {
15244 ++nwindows;
15245 w->must_be_updated_p = 1;
15246 }
15247
15248 /* Restore old settings. */
15249 set_buffer_internal_1 (old);
15250 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15251 }
15252
15253 window = w->next;
15254 }
15255
15256 return nwindows;
15257 }
15258
15259
15260 /* Display the mode and/or top line of window W. Value is the number
15261 of mode lines displayed. */
15262
15263 static int
15264 display_mode_lines (w)
15265 struct window *w;
15266 {
15267 Lisp_Object old_selected_window, old_selected_frame;
15268 int n = 0;
15269
15270 old_selected_frame = selected_frame;
15271 selected_frame = w->frame;
15272 old_selected_window = selected_window;
15273 XSETWINDOW (selected_window, w);
15274
15275 /* These will be set while the mode line specs are processed. */
15276 line_number_displayed = 0;
15277 w->column_number_displayed = Qnil;
15278
15279 if (WINDOW_WANTS_MODELINE_P (w))
15280 {
15281 struct window *sel_w = XWINDOW (old_selected_window);
15282
15283 /* Select mode line face based on the real selected window. */
15284 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15285 current_buffer->mode_line_format);
15286 ++n;
15287 }
15288
15289 if (WINDOW_WANTS_HEADER_LINE_P (w))
15290 {
15291 display_mode_line (w, HEADER_LINE_FACE_ID,
15292 current_buffer->header_line_format);
15293 ++n;
15294 }
15295
15296 selected_frame = old_selected_frame;
15297 selected_window = old_selected_window;
15298 return n;
15299 }
15300
15301
15302 /* Display mode or top line of window W. FACE_ID specifies which line
15303 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15304 FORMAT is the mode line format to display. Value is the pixel
15305 height of the mode line displayed. */
15306
15307 static int
15308 display_mode_line (w, face_id, format)
15309 struct window *w;
15310 enum face_id face_id;
15311 Lisp_Object format;
15312 {
15313 struct it it;
15314 struct face *face;
15315
15316 init_iterator (&it, w, -1, -1, NULL, face_id);
15317 prepare_desired_row (it.glyph_row);
15318
15319 it.glyph_row->mode_line_p = 1;
15320
15321 if (! mode_line_inverse_video)
15322 /* Force the mode-line to be displayed in the default face. */
15323 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15324
15325 /* Temporarily make frame's keyboard the current kboard so that
15326 kboard-local variables in the mode_line_format will get the right
15327 values. */
15328 push_frame_kboard (it.f);
15329 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15330 pop_frame_kboard ();
15331
15332 /* Fill up with spaces. */
15333 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15334
15335 compute_line_metrics (&it);
15336 it.glyph_row->full_width_p = 1;
15337 it.glyph_row->continued_p = 0;
15338 it.glyph_row->truncated_on_left_p = 0;
15339 it.glyph_row->truncated_on_right_p = 0;
15340
15341 /* Make a 3D mode-line have a shadow at its right end. */
15342 face = FACE_FROM_ID (it.f, face_id);
15343 extend_face_to_end_of_line (&it);
15344 if (face->box != FACE_NO_BOX)
15345 {
15346 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15347 + it.glyph_row->used[TEXT_AREA] - 1);
15348 last->right_box_line_p = 1;
15349 }
15350
15351 return it.glyph_row->height;
15352 }
15353
15354 /* Alist that caches the results of :propertize.
15355 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15356 Lisp_Object mode_line_proptrans_alist;
15357
15358 /* List of strings making up the mode-line. */
15359 Lisp_Object mode_line_string_list;
15360
15361 /* Base face property when building propertized mode line string. */
15362 static Lisp_Object mode_line_string_face;
15363 static Lisp_Object mode_line_string_face_prop;
15364
15365
15366 /* Contribute ELT to the mode line for window IT->w. How it
15367 translates into text depends on its data type.
15368
15369 IT describes the display environment in which we display, as usual.
15370
15371 DEPTH is the depth in recursion. It is used to prevent
15372 infinite recursion here.
15373
15374 FIELD_WIDTH is the number of characters the display of ELT should
15375 occupy in the mode line, and PRECISION is the maximum number of
15376 characters to display from ELT's representation. See
15377 display_string for details.
15378
15379 Returns the hpos of the end of the text generated by ELT.
15380
15381 PROPS is a property list to add to any string we encounter.
15382
15383 If RISKY is nonzero, remove (disregard) any properties in any string
15384 we encounter, and ignore :eval and :propertize.
15385
15386 If the global variable `frame_title_ptr' is non-NULL, then the output
15387 is passed to `store_frame_title' instead of `display_string'. */
15388
15389 static int
15390 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15391 struct it *it;
15392 int depth;
15393 int field_width, precision;
15394 Lisp_Object elt, props;
15395 int risky;
15396 {
15397 int n = 0, field, prec;
15398 int literal = 0;
15399
15400 tail_recurse:
15401 if (depth > 100)
15402 elt = build_string ("*too-deep*");
15403
15404 depth++;
15405
15406 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15407 {
15408 case Lisp_String:
15409 {
15410 /* A string: output it and check for %-constructs within it. */
15411 unsigned char c;
15412 const unsigned char *this, *lisp_string;
15413
15414 if (!NILP (props) || risky)
15415 {
15416 Lisp_Object oprops, aelt;
15417 oprops = Ftext_properties_at (make_number (0), elt);
15418
15419 /* If the starting string's properties are not what
15420 we want, translate the string. Also, if the string
15421 is risky, do that anyway. */
15422
15423 if (NILP (Fequal (props, oprops)) || risky)
15424 {
15425 /* If the starting string has properties,
15426 merge the specified ones onto the existing ones. */
15427 if (! NILP (oprops) && !risky)
15428 {
15429 Lisp_Object tem;
15430
15431 oprops = Fcopy_sequence (oprops);
15432 tem = props;
15433 while (CONSP (tem))
15434 {
15435 oprops = Fplist_put (oprops, XCAR (tem),
15436 XCAR (XCDR (tem)));
15437 tem = XCDR (XCDR (tem));
15438 }
15439 props = oprops;
15440 }
15441
15442 aelt = Fassoc (elt, mode_line_proptrans_alist);
15443 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15444 {
15445 mode_line_proptrans_alist
15446 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15447 elt = XCAR (aelt);
15448 }
15449 else
15450 {
15451 Lisp_Object tem;
15452
15453 elt = Fcopy_sequence (elt);
15454 Fset_text_properties (make_number (0), Flength (elt),
15455 props, elt);
15456 /* Add this item to mode_line_proptrans_alist. */
15457 mode_line_proptrans_alist
15458 = Fcons (Fcons (elt, props),
15459 mode_line_proptrans_alist);
15460 /* Truncate mode_line_proptrans_alist
15461 to at most 50 elements. */
15462 tem = Fnthcdr (make_number (50),
15463 mode_line_proptrans_alist);
15464 if (! NILP (tem))
15465 XSETCDR (tem, Qnil);
15466 }
15467 }
15468 }
15469
15470 this = SDATA (elt);
15471 lisp_string = this;
15472
15473 if (literal)
15474 {
15475 prec = precision - n;
15476 if (frame_title_ptr)
15477 n += store_frame_title (SDATA (elt), -1, prec);
15478 else if (!NILP (mode_line_string_list))
15479 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15480 else
15481 n += display_string (NULL, elt, Qnil, 0, 0, it,
15482 0, prec, 0, STRING_MULTIBYTE (elt));
15483
15484 break;
15485 }
15486
15487 while ((precision <= 0 || n < precision)
15488 && *this
15489 && (frame_title_ptr
15490 || !NILP (mode_line_string_list)
15491 || it->current_x < it->last_visible_x))
15492 {
15493 const unsigned char *last = this;
15494
15495 /* Advance to end of string or next format specifier. */
15496 while ((c = *this++) != '\0' && c != '%')
15497 ;
15498
15499 if (this - 1 != last)
15500 {
15501 int nchars, nbytes;
15502
15503 /* Output to end of string or up to '%'. Field width
15504 is length of string. Don't output more than
15505 PRECISION allows us. */
15506 --this;
15507
15508 prec = c_string_width (last, this - last, precision - n,
15509 &nchars, &nbytes);
15510
15511 if (frame_title_ptr)
15512 n += store_frame_title (last, 0, prec);
15513 else if (!NILP (mode_line_string_list))
15514 {
15515 int bytepos = last - lisp_string;
15516 int charpos = string_byte_to_char (elt, bytepos);
15517 int endpos = (precision <= 0
15518 ? string_byte_to_char (elt,
15519 this - lisp_string)
15520 : charpos + nchars);
15521
15522 n += store_mode_line_string (NULL,
15523 Fsubstring (elt, make_number (charpos),
15524 make_number (endpos)),
15525 0, 0, 0, Qnil);
15526 }
15527 else
15528 {
15529 int bytepos = last - lisp_string;
15530 int charpos = string_byte_to_char (elt, bytepos);
15531 n += display_string (NULL, elt, Qnil, 0, charpos,
15532 it, 0, prec, 0,
15533 STRING_MULTIBYTE (elt));
15534 }
15535 }
15536 else /* c == '%' */
15537 {
15538 const unsigned char *percent_position = this;
15539
15540 /* Get the specified minimum width. Zero means
15541 don't pad. */
15542 field = 0;
15543 while ((c = *this++) >= '0' && c <= '9')
15544 field = field * 10 + c - '0';
15545
15546 /* Don't pad beyond the total padding allowed. */
15547 if (field_width - n > 0 && field > field_width - n)
15548 field = field_width - n;
15549
15550 /* Note that either PRECISION <= 0 or N < PRECISION. */
15551 prec = precision - n;
15552
15553 if (c == 'M')
15554 n += display_mode_element (it, depth, field, prec,
15555 Vglobal_mode_string, props,
15556 risky);
15557 else if (c != 0)
15558 {
15559 int multibyte;
15560 int bytepos, charpos;
15561 unsigned char *spec;
15562
15563 bytepos = percent_position - lisp_string;
15564 charpos = (STRING_MULTIBYTE (elt)
15565 ? string_byte_to_char (elt, bytepos)
15566 : bytepos);
15567
15568 spec
15569 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15570
15571 if (frame_title_ptr)
15572 n += store_frame_title (spec, field, prec);
15573 else if (!NILP (mode_line_string_list))
15574 {
15575 int len = strlen (spec);
15576 Lisp_Object tem = make_string (spec, len);
15577 props = Ftext_properties_at (make_number (charpos), elt);
15578 /* Should only keep face property in props */
15579 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15580 }
15581 else
15582 {
15583 int nglyphs_before, nwritten;
15584
15585 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15586 nwritten = display_string (spec, Qnil, elt,
15587 charpos, 0, it,
15588 field, prec, 0,
15589 multibyte);
15590
15591 /* Assign to the glyphs written above the
15592 string where the `%x' came from, position
15593 of the `%'. */
15594 if (nwritten > 0)
15595 {
15596 struct glyph *glyph
15597 = (it->glyph_row->glyphs[TEXT_AREA]
15598 + nglyphs_before);
15599 int i;
15600
15601 for (i = 0; i < nwritten; ++i)
15602 {
15603 glyph[i].object = elt;
15604 glyph[i].charpos = charpos;
15605 }
15606
15607 n += nwritten;
15608 }
15609 }
15610 }
15611 else /* c == 0 */
15612 break;
15613 }
15614 }
15615 }
15616 break;
15617
15618 case Lisp_Symbol:
15619 /* A symbol: process the value of the symbol recursively
15620 as if it appeared here directly. Avoid error if symbol void.
15621 Special case: if value of symbol is a string, output the string
15622 literally. */
15623 {
15624 register Lisp_Object tem;
15625
15626 /* If the variable is not marked as risky to set
15627 then its contents are risky to use. */
15628 if (NILP (Fget (elt, Qrisky_local_variable)))
15629 risky = 1;
15630
15631 tem = Fboundp (elt);
15632 if (!NILP (tem))
15633 {
15634 tem = Fsymbol_value (elt);
15635 /* If value is a string, output that string literally:
15636 don't check for % within it. */
15637 if (STRINGP (tem))
15638 literal = 1;
15639
15640 if (!EQ (tem, elt))
15641 {
15642 /* Give up right away for nil or t. */
15643 elt = tem;
15644 goto tail_recurse;
15645 }
15646 }
15647 }
15648 break;
15649
15650 case Lisp_Cons:
15651 {
15652 register Lisp_Object car, tem;
15653
15654 /* A cons cell: five distinct cases.
15655 If first element is :eval or :propertize, do something special.
15656 If first element is a string or a cons, process all the elements
15657 and effectively concatenate them.
15658 If first element is a negative number, truncate displaying cdr to
15659 at most that many characters. If positive, pad (with spaces)
15660 to at least that many characters.
15661 If first element is a symbol, process the cadr or caddr recursively
15662 according to whether the symbol's value is non-nil or nil. */
15663 car = XCAR (elt);
15664 if (EQ (car, QCeval))
15665 {
15666 /* An element of the form (:eval FORM) means evaluate FORM
15667 and use the result as mode line elements. */
15668
15669 if (risky)
15670 break;
15671
15672 if (CONSP (XCDR (elt)))
15673 {
15674 Lisp_Object spec;
15675 spec = safe_eval (XCAR (XCDR (elt)));
15676 n += display_mode_element (it, depth, field_width - n,
15677 precision - n, spec, props,
15678 risky);
15679 }
15680 }
15681 else if (EQ (car, QCpropertize))
15682 {
15683 /* An element of the form (:propertize ELT PROPS...)
15684 means display ELT but applying properties PROPS. */
15685
15686 if (risky)
15687 break;
15688
15689 if (CONSP (XCDR (elt)))
15690 n += display_mode_element (it, depth, field_width - n,
15691 precision - n, XCAR (XCDR (elt)),
15692 XCDR (XCDR (elt)), risky);
15693 }
15694 else if (SYMBOLP (car))
15695 {
15696 tem = Fboundp (car);
15697 elt = XCDR (elt);
15698 if (!CONSP (elt))
15699 goto invalid;
15700 /* elt is now the cdr, and we know it is a cons cell.
15701 Use its car if CAR has a non-nil value. */
15702 if (!NILP (tem))
15703 {
15704 tem = Fsymbol_value (car);
15705 if (!NILP (tem))
15706 {
15707 elt = XCAR (elt);
15708 goto tail_recurse;
15709 }
15710 }
15711 /* Symbol's value is nil (or symbol is unbound)
15712 Get the cddr of the original list
15713 and if possible find the caddr and use that. */
15714 elt = XCDR (elt);
15715 if (NILP (elt))
15716 break;
15717 else if (!CONSP (elt))
15718 goto invalid;
15719 elt = XCAR (elt);
15720 goto tail_recurse;
15721 }
15722 else if (INTEGERP (car))
15723 {
15724 register int lim = XINT (car);
15725 elt = XCDR (elt);
15726 if (lim < 0)
15727 {
15728 /* Negative int means reduce maximum width. */
15729 if (precision <= 0)
15730 precision = -lim;
15731 else
15732 precision = min (precision, -lim);
15733 }
15734 else if (lim > 0)
15735 {
15736 /* Padding specified. Don't let it be more than
15737 current maximum. */
15738 if (precision > 0)
15739 lim = min (precision, lim);
15740
15741 /* If that's more padding than already wanted, queue it.
15742 But don't reduce padding already specified even if
15743 that is beyond the current truncation point. */
15744 field_width = max (lim, field_width);
15745 }
15746 goto tail_recurse;
15747 }
15748 else if (STRINGP (car) || CONSP (car))
15749 {
15750 register int limit = 50;
15751 /* Limit is to protect against circular lists. */
15752 while (CONSP (elt)
15753 && --limit > 0
15754 && (precision <= 0 || n < precision))
15755 {
15756 n += display_mode_element (it, depth, field_width - n,
15757 precision - n, XCAR (elt),
15758 props, risky);
15759 elt = XCDR (elt);
15760 }
15761 }
15762 }
15763 break;
15764
15765 default:
15766 invalid:
15767 elt = build_string ("*invalid*");
15768 goto tail_recurse;
15769 }
15770
15771 /* Pad to FIELD_WIDTH. */
15772 if (field_width > 0 && n < field_width)
15773 {
15774 if (frame_title_ptr)
15775 n += store_frame_title ("", field_width - n, 0);
15776 else if (!NILP (mode_line_string_list))
15777 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15778 else
15779 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15780 0, 0, 0);
15781 }
15782
15783 return n;
15784 }
15785
15786 /* Store a mode-line string element in mode_line_string_list.
15787
15788 If STRING is non-null, display that C string. Otherwise, the Lisp
15789 string LISP_STRING is displayed.
15790
15791 FIELD_WIDTH is the minimum number of output glyphs to produce.
15792 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15793 with spaces. FIELD_WIDTH <= 0 means don't pad.
15794
15795 PRECISION is the maximum number of characters to output from
15796 STRING. PRECISION <= 0 means don't truncate the string.
15797
15798 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15799 properties to the string.
15800
15801 PROPS are the properties to add to the string.
15802 The mode_line_string_face face property is always added to the string.
15803 */
15804
15805 static int
15806 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15807 char *string;
15808 Lisp_Object lisp_string;
15809 int copy_string;
15810 int field_width;
15811 int precision;
15812 Lisp_Object props;
15813 {
15814 int len;
15815 int n = 0;
15816
15817 if (string != NULL)
15818 {
15819 len = strlen (string);
15820 if (precision > 0 && len > precision)
15821 len = precision;
15822 lisp_string = make_string (string, len);
15823 if (NILP (props))
15824 props = mode_line_string_face_prop;
15825 else if (!NILP (mode_line_string_face))
15826 {
15827 Lisp_Object face = Fsafe_plist_get (props, Qface);
15828 props = Fcopy_sequence (props);
15829 if (NILP (face))
15830 face = mode_line_string_face;
15831 else
15832 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15833 props = Fplist_put (props, Qface, face);
15834 }
15835 Fadd_text_properties (make_number (0), make_number (len),
15836 props, lisp_string);
15837 }
15838 else
15839 {
15840 len = XFASTINT (Flength (lisp_string));
15841 if (precision > 0 && len > precision)
15842 {
15843 len = precision;
15844 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15845 precision = -1;
15846 }
15847 if (!NILP (mode_line_string_face))
15848 {
15849 Lisp_Object face;
15850 if (NILP (props))
15851 props = Ftext_properties_at (make_number (0), lisp_string);
15852 face = Fsafe_plist_get (props, Qface);
15853 if (NILP (face))
15854 face = mode_line_string_face;
15855 else
15856 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15857 props = Fcons (Qface, Fcons (face, Qnil));
15858 if (copy_string)
15859 lisp_string = Fcopy_sequence (lisp_string);
15860 }
15861 if (!NILP (props))
15862 Fadd_text_properties (make_number (0), make_number (len),
15863 props, lisp_string);
15864 }
15865
15866 if (len > 0)
15867 {
15868 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15869 n += len;
15870 }
15871
15872 if (field_width > len)
15873 {
15874 field_width -= len;
15875 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15876 if (!NILP (props))
15877 Fadd_text_properties (make_number (0), make_number (field_width),
15878 props, lisp_string);
15879 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15880 n += field_width;
15881 }
15882
15883 return n;
15884 }
15885
15886
15887 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15888 0, 4, 0,
15889 doc: /* Return the mode-line of selected window as a string.
15890 First optional arg FORMAT specifies a different format string (see
15891 `mode-line-format' for details) to use. If FORMAT is t, return
15892 the buffer's header-line. Second optional arg WINDOW specifies a
15893 different window to use as the context for the formatting.
15894 If third optional arg NO-PROPS is non-nil, string is not propertized.
15895 Fourth optional arg BUFFER specifies which buffer to use. */)
15896 (format, window, no_props, buffer)
15897 Lisp_Object format, window, no_props, buffer;
15898 {
15899 struct it it;
15900 int len;
15901 struct window *w;
15902 struct buffer *old_buffer = NULL;
15903 enum face_id face_id = DEFAULT_FACE_ID;
15904
15905 if (NILP (window))
15906 window = selected_window;
15907 CHECK_WINDOW (window);
15908 w = XWINDOW (window);
15909
15910 if (NILP (buffer))
15911 buffer = w->buffer;
15912
15913 CHECK_BUFFER (buffer);
15914
15915 if (XBUFFER (buffer) != current_buffer)
15916 {
15917 old_buffer = current_buffer;
15918 set_buffer_internal_1 (XBUFFER (buffer));
15919 }
15920
15921 if (NILP (format) || EQ (format, Qt))
15922 {
15923 face_id = (NILP (format)
15924 ? CURRENT_MODE_LINE_FACE_ID (w)
15925 : HEADER_LINE_FACE_ID);
15926 format = (NILP (format)
15927 ? current_buffer->mode_line_format
15928 : current_buffer->header_line_format);
15929 }
15930
15931 init_iterator (&it, w, -1, -1, NULL, face_id);
15932
15933 if (NILP (no_props))
15934 {
15935 mode_line_string_face
15936 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15937 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15938 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15939
15940 mode_line_string_face_prop
15941 = (NILP (mode_line_string_face) ? Qnil
15942 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15943
15944 /* We need a dummy last element in mode_line_string_list to
15945 indicate we are building the propertized mode-line string.
15946 Using mode_line_string_face_prop here GC protects it. */
15947 mode_line_string_list
15948 = Fcons (mode_line_string_face_prop, Qnil);
15949 frame_title_ptr = NULL;
15950 }
15951 else
15952 {
15953 mode_line_string_face_prop = Qnil;
15954 mode_line_string_list = Qnil;
15955 frame_title_ptr = frame_title_buf;
15956 }
15957
15958 push_frame_kboard (it.f);
15959 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15960 pop_frame_kboard ();
15961
15962 if (old_buffer)
15963 set_buffer_internal_1 (old_buffer);
15964
15965 if (NILP (no_props))
15966 {
15967 Lisp_Object str;
15968 mode_line_string_list = Fnreverse (mode_line_string_list);
15969 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15970 make_string ("", 0));
15971 mode_line_string_face_prop = Qnil;
15972 mode_line_string_list = Qnil;
15973 return str;
15974 }
15975
15976 len = frame_title_ptr - frame_title_buf;
15977 if (len > 0 && frame_title_ptr[-1] == '-')
15978 {
15979 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15980 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15981 ;
15982 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15983 if (len > frame_title_ptr - frame_title_buf)
15984 len = frame_title_ptr - frame_title_buf;
15985 }
15986
15987 frame_title_ptr = NULL;
15988 return make_string (frame_title_buf, len);
15989 }
15990
15991 /* Write a null-terminated, right justified decimal representation of
15992 the positive integer D to BUF using a minimal field width WIDTH. */
15993
15994 static void
15995 pint2str (buf, width, d)
15996 register char *buf;
15997 register int width;
15998 register int d;
15999 {
16000 register char *p = buf;
16001
16002 if (d <= 0)
16003 *p++ = '0';
16004 else
16005 {
16006 while (d > 0)
16007 {
16008 *p++ = d % 10 + '0';
16009 d /= 10;
16010 }
16011 }
16012
16013 for (width -= (int) (p - buf); width > 0; --width)
16014 *p++ = ' ';
16015 *p-- = '\0';
16016 while (p > buf)
16017 {
16018 d = *buf;
16019 *buf++ = *p;
16020 *p-- = d;
16021 }
16022 }
16023
16024 /* Write a null-terminated, right justified decimal and "human
16025 readable" representation of the nonnegative integer D to BUF using
16026 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16027
16028 static const char power_letter[] =
16029 {
16030 0, /* not used */
16031 'k', /* kilo */
16032 'M', /* mega */
16033 'G', /* giga */
16034 'T', /* tera */
16035 'P', /* peta */
16036 'E', /* exa */
16037 'Z', /* zetta */
16038 'Y' /* yotta */
16039 };
16040
16041 static void
16042 pint2hrstr (buf, width, d)
16043 char *buf;
16044 int width;
16045 int d;
16046 {
16047 /* We aim to represent the nonnegative integer D as
16048 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16049 int quotient = d;
16050 int remainder = 0;
16051 /* -1 means: do not use TENTHS. */
16052 int tenths = -1;
16053 int exponent = 0;
16054
16055 /* Length of QUOTIENT.TENTHS as a string. */
16056 int length;
16057
16058 char * psuffix;
16059 char * p;
16060
16061 if (1000 <= quotient)
16062 {
16063 /* Scale to the appropriate EXPONENT. */
16064 do
16065 {
16066 remainder = quotient % 1000;
16067 quotient /= 1000;
16068 exponent++;
16069 }
16070 while (1000 <= quotient);
16071
16072 /* Round to nearest and decide whether to use TENTHS or not. */
16073 if (quotient <= 9)
16074 {
16075 tenths = remainder / 100;
16076 if (50 <= remainder % 100)
16077 {
16078 if (tenths < 9)
16079 tenths++;
16080 else
16081 {
16082 quotient++;
16083 if (quotient == 10)
16084 tenths = -1;
16085 else
16086 tenths = 0;
16087 }
16088 }
16089 }
16090 else
16091 if (500 <= remainder)
16092 {
16093 if (quotient < 999)
16094 quotient++;
16095 else
16096 {
16097 quotient = 1;
16098 exponent++;
16099 tenths = 0;
16100 }
16101 }
16102 }
16103
16104 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16105 if (tenths == -1 && quotient <= 99)
16106 if (quotient <= 9)
16107 length = 1;
16108 else
16109 length = 2;
16110 else
16111 length = 3;
16112 p = psuffix = buf + max (width, length);
16113
16114 /* Print EXPONENT. */
16115 if (exponent)
16116 *psuffix++ = power_letter[exponent];
16117 *psuffix = '\0';
16118
16119 /* Print TENTHS. */
16120 if (tenths >= 0)
16121 {
16122 *--p = '0' + tenths;
16123 *--p = '.';
16124 }
16125
16126 /* Print QUOTIENT. */
16127 do
16128 {
16129 int digit = quotient % 10;
16130 *--p = '0' + digit;
16131 }
16132 while ((quotient /= 10) != 0);
16133
16134 /* Print leading spaces. */
16135 while (buf < p)
16136 *--p = ' ';
16137 }
16138
16139 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16140 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16141 type of CODING_SYSTEM. Return updated pointer into BUF. */
16142
16143 static unsigned char invalid_eol_type[] = "(*invalid*)";
16144
16145 static char *
16146 decode_mode_spec_coding (coding_system, buf, eol_flag)
16147 Lisp_Object coding_system;
16148 register char *buf;
16149 int eol_flag;
16150 {
16151 Lisp_Object val;
16152 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16153 const unsigned char *eol_str;
16154 int eol_str_len;
16155 /* The EOL conversion we are using. */
16156 Lisp_Object eoltype;
16157
16158 val = Fget (coding_system, Qcoding_system);
16159 eoltype = Qnil;
16160
16161 if (!VECTORP (val)) /* Not yet decided. */
16162 {
16163 if (multibyte)
16164 *buf++ = '-';
16165 if (eol_flag)
16166 eoltype = eol_mnemonic_undecided;
16167 /* Don't mention EOL conversion if it isn't decided. */
16168 }
16169 else
16170 {
16171 Lisp_Object eolvalue;
16172
16173 eolvalue = Fget (coding_system, Qeol_type);
16174
16175 if (multibyte)
16176 *buf++ = XFASTINT (AREF (val, 1));
16177
16178 if (eol_flag)
16179 {
16180 /* The EOL conversion that is normal on this system. */
16181
16182 if (NILP (eolvalue)) /* Not yet decided. */
16183 eoltype = eol_mnemonic_undecided;
16184 else if (VECTORP (eolvalue)) /* Not yet decided. */
16185 eoltype = eol_mnemonic_undecided;
16186 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16187 eoltype = (XFASTINT (eolvalue) == 0
16188 ? eol_mnemonic_unix
16189 : (XFASTINT (eolvalue) == 1
16190 ? eol_mnemonic_dos : eol_mnemonic_mac));
16191 }
16192 }
16193
16194 if (eol_flag)
16195 {
16196 /* Mention the EOL conversion if it is not the usual one. */
16197 if (STRINGP (eoltype))
16198 {
16199 eol_str = SDATA (eoltype);
16200 eol_str_len = SBYTES (eoltype);
16201 }
16202 else if (INTEGERP (eoltype)
16203 && CHAR_VALID_P (XINT (eoltype), 0))
16204 {
16205 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16206 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16207 eol_str = tmp;
16208 }
16209 else
16210 {
16211 eol_str = invalid_eol_type;
16212 eol_str_len = sizeof (invalid_eol_type) - 1;
16213 }
16214 bcopy (eol_str, buf, eol_str_len);
16215 buf += eol_str_len;
16216 }
16217
16218 return buf;
16219 }
16220
16221 /* Return a string for the output of a mode line %-spec for window W,
16222 generated by character C. PRECISION >= 0 means don't return a
16223 string longer than that value. FIELD_WIDTH > 0 means pad the
16224 string returned with spaces to that value. Return 1 in *MULTIBYTE
16225 if the result is multibyte text.
16226
16227 Note we operate on the current buffer for most purposes,
16228 the exception being w->base_line_pos. */
16229
16230 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16231
16232 static char *
16233 decode_mode_spec (w, c, field_width, precision, multibyte)
16234 struct window *w;
16235 register int c;
16236 int field_width, precision;
16237 int *multibyte;
16238 {
16239 Lisp_Object obj;
16240 struct frame *f = XFRAME (WINDOW_FRAME (w));
16241 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16242 struct buffer *b = current_buffer;
16243
16244 obj = Qnil;
16245 *multibyte = 0;
16246
16247 switch (c)
16248 {
16249 case '*':
16250 if (!NILP (b->read_only))
16251 return "%";
16252 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16253 return "*";
16254 return "-";
16255
16256 case '+':
16257 /* This differs from %* only for a modified read-only buffer. */
16258 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16259 return "*";
16260 if (!NILP (b->read_only))
16261 return "%";
16262 return "-";
16263
16264 case '&':
16265 /* This differs from %* in ignoring read-only-ness. */
16266 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16267 return "*";
16268 return "-";
16269
16270 case '%':
16271 return "%";
16272
16273 case '[':
16274 {
16275 int i;
16276 char *p;
16277
16278 if (command_loop_level > 5)
16279 return "[[[... ";
16280 p = decode_mode_spec_buf;
16281 for (i = 0; i < command_loop_level; i++)
16282 *p++ = '[';
16283 *p = 0;
16284 return decode_mode_spec_buf;
16285 }
16286
16287 case ']':
16288 {
16289 int i;
16290 char *p;
16291
16292 if (command_loop_level > 5)
16293 return " ...]]]";
16294 p = decode_mode_spec_buf;
16295 for (i = 0; i < command_loop_level; i++)
16296 *p++ = ']';
16297 *p = 0;
16298 return decode_mode_spec_buf;
16299 }
16300
16301 case '-':
16302 {
16303 register int i;
16304
16305 /* Let lots_of_dashes be a string of infinite length. */
16306 if (!NILP (mode_line_string_list))
16307 return "--";
16308 if (field_width <= 0
16309 || field_width > sizeof (lots_of_dashes))
16310 {
16311 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16312 decode_mode_spec_buf[i] = '-';
16313 decode_mode_spec_buf[i] = '\0';
16314 return decode_mode_spec_buf;
16315 }
16316 else
16317 return lots_of_dashes;
16318 }
16319
16320 case 'b':
16321 obj = b->name;
16322 break;
16323
16324 case 'c':
16325 {
16326 int col = (int) current_column (); /* iftc */
16327 w->column_number_displayed = make_number (col);
16328 pint2str (decode_mode_spec_buf, field_width, col);
16329 return decode_mode_spec_buf;
16330 }
16331
16332 case 'F':
16333 /* %F displays the frame name. */
16334 if (!NILP (f->title))
16335 return (char *) SDATA (f->title);
16336 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16337 return (char *) SDATA (f->name);
16338 return "Emacs";
16339
16340 case 'f':
16341 obj = b->filename;
16342 break;
16343
16344 case 'i':
16345 {
16346 int size = ZV - BEGV;
16347 pint2str (decode_mode_spec_buf, field_width, size);
16348 return decode_mode_spec_buf;
16349 }
16350
16351 case 'I':
16352 {
16353 int size = ZV - BEGV;
16354 pint2hrstr (decode_mode_spec_buf, field_width, size);
16355 return decode_mode_spec_buf;
16356 }
16357
16358 case 'l':
16359 {
16360 int startpos = XMARKER (w->start)->charpos;
16361 int startpos_byte = marker_byte_position (w->start);
16362 int line, linepos, linepos_byte, topline;
16363 int nlines, junk;
16364 int height = WINDOW_TOTAL_LINES (w);
16365
16366 /* If we decided that this buffer isn't suitable for line numbers,
16367 don't forget that too fast. */
16368 if (EQ (w->base_line_pos, w->buffer))
16369 goto no_value;
16370 /* But do forget it, if the window shows a different buffer now. */
16371 else if (BUFFERP (w->base_line_pos))
16372 w->base_line_pos = Qnil;
16373
16374 /* If the buffer is very big, don't waste time. */
16375 if (INTEGERP (Vline_number_display_limit)
16376 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16377 {
16378 w->base_line_pos = Qnil;
16379 w->base_line_number = Qnil;
16380 goto no_value;
16381 }
16382
16383 if (!NILP (w->base_line_number)
16384 && !NILP (w->base_line_pos)
16385 && XFASTINT (w->base_line_pos) <= startpos)
16386 {
16387 line = XFASTINT (w->base_line_number);
16388 linepos = XFASTINT (w->base_line_pos);
16389 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16390 }
16391 else
16392 {
16393 line = 1;
16394 linepos = BUF_BEGV (b);
16395 linepos_byte = BUF_BEGV_BYTE (b);
16396 }
16397
16398 /* Count lines from base line to window start position. */
16399 nlines = display_count_lines (linepos, linepos_byte,
16400 startpos_byte,
16401 startpos, &junk);
16402
16403 topline = nlines + line;
16404
16405 /* Determine a new base line, if the old one is too close
16406 or too far away, or if we did not have one.
16407 "Too close" means it's plausible a scroll-down would
16408 go back past it. */
16409 if (startpos == BUF_BEGV (b))
16410 {
16411 w->base_line_number = make_number (topline);
16412 w->base_line_pos = make_number (BUF_BEGV (b));
16413 }
16414 else if (nlines < height + 25 || nlines > height * 3 + 50
16415 || linepos == BUF_BEGV (b))
16416 {
16417 int limit = BUF_BEGV (b);
16418 int limit_byte = BUF_BEGV_BYTE (b);
16419 int position;
16420 int distance = (height * 2 + 30) * line_number_display_limit_width;
16421
16422 if (startpos - distance > limit)
16423 {
16424 limit = startpos - distance;
16425 limit_byte = CHAR_TO_BYTE (limit);
16426 }
16427
16428 nlines = display_count_lines (startpos, startpos_byte,
16429 limit_byte,
16430 - (height * 2 + 30),
16431 &position);
16432 /* If we couldn't find the lines we wanted within
16433 line_number_display_limit_width chars per line,
16434 give up on line numbers for this window. */
16435 if (position == limit_byte && limit == startpos - distance)
16436 {
16437 w->base_line_pos = w->buffer;
16438 w->base_line_number = Qnil;
16439 goto no_value;
16440 }
16441
16442 w->base_line_number = make_number (topline - nlines);
16443 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16444 }
16445
16446 /* Now count lines from the start pos to point. */
16447 nlines = display_count_lines (startpos, startpos_byte,
16448 PT_BYTE, PT, &junk);
16449
16450 /* Record that we did display the line number. */
16451 line_number_displayed = 1;
16452
16453 /* Make the string to show. */
16454 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16455 return decode_mode_spec_buf;
16456 no_value:
16457 {
16458 char* p = decode_mode_spec_buf;
16459 int pad = field_width - 2;
16460 while (pad-- > 0)
16461 *p++ = ' ';
16462 *p++ = '?';
16463 *p++ = '?';
16464 *p = '\0';
16465 return decode_mode_spec_buf;
16466 }
16467 }
16468 break;
16469
16470 case 'm':
16471 obj = b->mode_name;
16472 break;
16473
16474 case 'n':
16475 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16476 return " Narrow";
16477 break;
16478
16479 case 'p':
16480 {
16481 int pos = marker_position (w->start);
16482 int total = BUF_ZV (b) - BUF_BEGV (b);
16483
16484 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16485 {
16486 if (pos <= BUF_BEGV (b))
16487 return "All";
16488 else
16489 return "Bottom";
16490 }
16491 else if (pos <= BUF_BEGV (b))
16492 return "Top";
16493 else
16494 {
16495 if (total > 1000000)
16496 /* Do it differently for a large value, to avoid overflow. */
16497 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16498 else
16499 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16500 /* We can't normally display a 3-digit number,
16501 so get us a 2-digit number that is close. */
16502 if (total == 100)
16503 total = 99;
16504 sprintf (decode_mode_spec_buf, "%2d%%", total);
16505 return decode_mode_spec_buf;
16506 }
16507 }
16508
16509 /* Display percentage of size above the bottom of the screen. */
16510 case 'P':
16511 {
16512 int toppos = marker_position (w->start);
16513 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16514 int total = BUF_ZV (b) - BUF_BEGV (b);
16515
16516 if (botpos >= BUF_ZV (b))
16517 {
16518 if (toppos <= BUF_BEGV (b))
16519 return "All";
16520 else
16521 return "Bottom";
16522 }
16523 else
16524 {
16525 if (total > 1000000)
16526 /* Do it differently for a large value, to avoid overflow. */
16527 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16528 else
16529 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16530 /* We can't normally display a 3-digit number,
16531 so get us a 2-digit number that is close. */
16532 if (total == 100)
16533 total = 99;
16534 if (toppos <= BUF_BEGV (b))
16535 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16536 else
16537 sprintf (decode_mode_spec_buf, "%2d%%", total);
16538 return decode_mode_spec_buf;
16539 }
16540 }
16541
16542 case 's':
16543 /* status of process */
16544 obj = Fget_buffer_process (Fcurrent_buffer ());
16545 if (NILP (obj))
16546 return "no process";
16547 #ifdef subprocesses
16548 obj = Fsymbol_name (Fprocess_status (obj));
16549 #endif
16550 break;
16551
16552 case 't': /* indicate TEXT or BINARY */
16553 #ifdef MODE_LINE_BINARY_TEXT
16554 return MODE_LINE_BINARY_TEXT (b);
16555 #else
16556 return "T";
16557 #endif
16558
16559 case 'z':
16560 /* coding-system (not including end-of-line format) */
16561 case 'Z':
16562 /* coding-system (including end-of-line type) */
16563 {
16564 int eol_flag = (c == 'Z');
16565 char *p = decode_mode_spec_buf;
16566
16567 if (! FRAME_WINDOW_P (f))
16568 {
16569 /* No need to mention EOL here--the terminal never needs
16570 to do EOL conversion. */
16571 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16572 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16573 }
16574 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16575 p, eol_flag);
16576
16577 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16578 #ifdef subprocesses
16579 obj = Fget_buffer_process (Fcurrent_buffer ());
16580 if (PROCESSP (obj))
16581 {
16582 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16583 p, eol_flag);
16584 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16585 p, eol_flag);
16586 }
16587 #endif /* subprocesses */
16588 #endif /* 0 */
16589 *p = 0;
16590 return decode_mode_spec_buf;
16591 }
16592 }
16593
16594 if (STRINGP (obj))
16595 {
16596 *multibyte = STRING_MULTIBYTE (obj);
16597 return (char *) SDATA (obj);
16598 }
16599 else
16600 return "";
16601 }
16602
16603
16604 /* Count up to COUNT lines starting from START / START_BYTE.
16605 But don't go beyond LIMIT_BYTE.
16606 Return the number of lines thus found (always nonnegative).
16607
16608 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16609
16610 static int
16611 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16612 int start, start_byte, limit_byte, count;
16613 int *byte_pos_ptr;
16614 {
16615 register unsigned char *cursor;
16616 unsigned char *base;
16617
16618 register int ceiling;
16619 register unsigned char *ceiling_addr;
16620 int orig_count = count;
16621
16622 /* If we are not in selective display mode,
16623 check only for newlines. */
16624 int selective_display = (!NILP (current_buffer->selective_display)
16625 && !INTEGERP (current_buffer->selective_display));
16626
16627 if (count > 0)
16628 {
16629 while (start_byte < limit_byte)
16630 {
16631 ceiling = BUFFER_CEILING_OF (start_byte);
16632 ceiling = min (limit_byte - 1, ceiling);
16633 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16634 base = (cursor = BYTE_POS_ADDR (start_byte));
16635 while (1)
16636 {
16637 if (selective_display)
16638 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16639 ;
16640 else
16641 while (*cursor != '\n' && ++cursor != ceiling_addr)
16642 ;
16643
16644 if (cursor != ceiling_addr)
16645 {
16646 if (--count == 0)
16647 {
16648 start_byte += cursor - base + 1;
16649 *byte_pos_ptr = start_byte;
16650 return orig_count;
16651 }
16652 else
16653 if (++cursor == ceiling_addr)
16654 break;
16655 }
16656 else
16657 break;
16658 }
16659 start_byte += cursor - base;
16660 }
16661 }
16662 else
16663 {
16664 while (start_byte > limit_byte)
16665 {
16666 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16667 ceiling = max (limit_byte, ceiling);
16668 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16669 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16670 while (1)
16671 {
16672 if (selective_display)
16673 while (--cursor != ceiling_addr
16674 && *cursor != '\n' && *cursor != 015)
16675 ;
16676 else
16677 while (--cursor != ceiling_addr && *cursor != '\n')
16678 ;
16679
16680 if (cursor != ceiling_addr)
16681 {
16682 if (++count == 0)
16683 {
16684 start_byte += cursor - base + 1;
16685 *byte_pos_ptr = start_byte;
16686 /* When scanning backwards, we should
16687 not count the newline posterior to which we stop. */
16688 return - orig_count - 1;
16689 }
16690 }
16691 else
16692 break;
16693 }
16694 /* Here we add 1 to compensate for the last decrement
16695 of CURSOR, which took it past the valid range. */
16696 start_byte += cursor - base + 1;
16697 }
16698 }
16699
16700 *byte_pos_ptr = limit_byte;
16701
16702 if (count < 0)
16703 return - orig_count + count;
16704 return orig_count - count;
16705
16706 }
16707
16708
16709 \f
16710 /***********************************************************************
16711 Displaying strings
16712 ***********************************************************************/
16713
16714 /* Display a NUL-terminated string, starting with index START.
16715
16716 If STRING is non-null, display that C string. Otherwise, the Lisp
16717 string LISP_STRING is displayed.
16718
16719 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16720 FACE_STRING. Display STRING or LISP_STRING with the face at
16721 FACE_STRING_POS in FACE_STRING:
16722
16723 Display the string in the environment given by IT, but use the
16724 standard display table, temporarily.
16725
16726 FIELD_WIDTH is the minimum number of output glyphs to produce.
16727 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16728 with spaces. If STRING has more characters, more than FIELD_WIDTH
16729 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16730
16731 PRECISION is the maximum number of characters to output from
16732 STRING. PRECISION < 0 means don't truncate the string.
16733
16734 This is roughly equivalent to printf format specifiers:
16735
16736 FIELD_WIDTH PRECISION PRINTF
16737 ----------------------------------------
16738 -1 -1 %s
16739 -1 10 %.10s
16740 10 -1 %10s
16741 20 10 %20.10s
16742
16743 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16744 display them, and < 0 means obey the current buffer's value of
16745 enable_multibyte_characters.
16746
16747 Value is the number of glyphs produced. */
16748
16749 static int
16750 display_string (string, lisp_string, face_string, face_string_pos,
16751 start, it, field_width, precision, max_x, multibyte)
16752 unsigned char *string;
16753 Lisp_Object lisp_string;
16754 Lisp_Object face_string;
16755 int face_string_pos;
16756 int start;
16757 struct it *it;
16758 int field_width, precision, max_x;
16759 int multibyte;
16760 {
16761 int hpos_at_start = it->hpos;
16762 int saved_face_id = it->face_id;
16763 struct glyph_row *row = it->glyph_row;
16764
16765 /* Initialize the iterator IT for iteration over STRING beginning
16766 with index START. */
16767 reseat_to_string (it, string, lisp_string, start,
16768 precision, field_width, multibyte);
16769
16770 /* If displaying STRING, set up the face of the iterator
16771 from LISP_STRING, if that's given. */
16772 if (STRINGP (face_string))
16773 {
16774 int endptr;
16775 struct face *face;
16776
16777 it->face_id
16778 = face_at_string_position (it->w, face_string, face_string_pos,
16779 0, it->region_beg_charpos,
16780 it->region_end_charpos,
16781 &endptr, it->base_face_id, 0);
16782 face = FACE_FROM_ID (it->f, it->face_id);
16783 it->face_box_p = face->box != FACE_NO_BOX;
16784 }
16785
16786 /* Set max_x to the maximum allowed X position. Don't let it go
16787 beyond the right edge of the window. */
16788 if (max_x <= 0)
16789 max_x = it->last_visible_x;
16790 else
16791 max_x = min (max_x, it->last_visible_x);
16792
16793 /* Skip over display elements that are not visible. because IT->w is
16794 hscrolled. */
16795 if (it->current_x < it->first_visible_x)
16796 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16797 MOVE_TO_POS | MOVE_TO_X);
16798
16799 row->ascent = it->max_ascent;
16800 row->height = it->max_ascent + it->max_descent;
16801 row->phys_ascent = it->max_phys_ascent;
16802 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16803 row->extra_line_spacing = it->max_extra_line_spacing;
16804
16805 /* This condition is for the case that we are called with current_x
16806 past last_visible_x. */
16807 while (it->current_x < max_x)
16808 {
16809 int x_before, x, n_glyphs_before, i, nglyphs;
16810
16811 /* Get the next display element. */
16812 if (!get_next_display_element (it))
16813 break;
16814
16815 /* Produce glyphs. */
16816 x_before = it->current_x;
16817 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16818 PRODUCE_GLYPHS (it);
16819
16820 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16821 i = 0;
16822 x = x_before;
16823 while (i < nglyphs)
16824 {
16825 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16826
16827 if (!it->truncate_lines_p
16828 && x + glyph->pixel_width > max_x)
16829 {
16830 /* End of continued line or max_x reached. */
16831 if (CHAR_GLYPH_PADDING_P (*glyph))
16832 {
16833 /* A wide character is unbreakable. */
16834 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16835 it->current_x = x_before;
16836 }
16837 else
16838 {
16839 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16840 it->current_x = x;
16841 }
16842 break;
16843 }
16844 else if (x + glyph->pixel_width > it->first_visible_x)
16845 {
16846 /* Glyph is at least partially visible. */
16847 ++it->hpos;
16848 if (x < it->first_visible_x)
16849 it->glyph_row->x = x - it->first_visible_x;
16850 }
16851 else
16852 {
16853 /* Glyph is off the left margin of the display area.
16854 Should not happen. */
16855 abort ();
16856 }
16857
16858 row->ascent = max (row->ascent, it->max_ascent);
16859 row->height = max (row->height, it->max_ascent + it->max_descent);
16860 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16861 row->phys_height = max (row->phys_height,
16862 it->max_phys_ascent + it->max_phys_descent);
16863 row->extra_line_spacing = max (row->extra_line_spacing,
16864 it->max_extra_line_spacing);
16865 x += glyph->pixel_width;
16866 ++i;
16867 }
16868
16869 /* Stop if max_x reached. */
16870 if (i < nglyphs)
16871 break;
16872
16873 /* Stop at line ends. */
16874 if (ITERATOR_AT_END_OF_LINE_P (it))
16875 {
16876 it->continuation_lines_width = 0;
16877 break;
16878 }
16879
16880 set_iterator_to_next (it, 1);
16881
16882 /* Stop if truncating at the right edge. */
16883 if (it->truncate_lines_p
16884 && it->current_x >= it->last_visible_x)
16885 {
16886 /* Add truncation mark, but don't do it if the line is
16887 truncated at a padding space. */
16888 if (IT_CHARPOS (*it) < it->string_nchars)
16889 {
16890 if (!FRAME_WINDOW_P (it->f))
16891 {
16892 int i, n;
16893
16894 if (it->current_x > it->last_visible_x)
16895 {
16896 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16897 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16898 break;
16899 for (n = row->used[TEXT_AREA]; i < n; ++i)
16900 {
16901 row->used[TEXT_AREA] = i;
16902 produce_special_glyphs (it, IT_TRUNCATION);
16903 }
16904 }
16905 produce_special_glyphs (it, IT_TRUNCATION);
16906 }
16907 it->glyph_row->truncated_on_right_p = 1;
16908 }
16909 break;
16910 }
16911 }
16912
16913 /* Maybe insert a truncation at the left. */
16914 if (it->first_visible_x
16915 && IT_CHARPOS (*it) > 0)
16916 {
16917 if (!FRAME_WINDOW_P (it->f))
16918 insert_left_trunc_glyphs (it);
16919 it->glyph_row->truncated_on_left_p = 1;
16920 }
16921
16922 it->face_id = saved_face_id;
16923
16924 /* Value is number of columns displayed. */
16925 return it->hpos - hpos_at_start;
16926 }
16927
16928
16929 \f
16930 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16931 appears as an element of LIST or as the car of an element of LIST.
16932 If PROPVAL is a list, compare each element against LIST in that
16933 way, and return 1/2 if any element of PROPVAL is found in LIST.
16934 Otherwise return 0. This function cannot quit.
16935 The return value is 2 if the text is invisible but with an ellipsis
16936 and 1 if it's invisible and without an ellipsis. */
16937
16938 int
16939 invisible_p (propval, list)
16940 register Lisp_Object propval;
16941 Lisp_Object list;
16942 {
16943 register Lisp_Object tail, proptail;
16944
16945 for (tail = list; CONSP (tail); tail = XCDR (tail))
16946 {
16947 register Lisp_Object tem;
16948 tem = XCAR (tail);
16949 if (EQ (propval, tem))
16950 return 1;
16951 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16952 return NILP (XCDR (tem)) ? 1 : 2;
16953 }
16954
16955 if (CONSP (propval))
16956 {
16957 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16958 {
16959 Lisp_Object propelt;
16960 propelt = XCAR (proptail);
16961 for (tail = list; CONSP (tail); tail = XCDR (tail))
16962 {
16963 register Lisp_Object tem;
16964 tem = XCAR (tail);
16965 if (EQ (propelt, tem))
16966 return 1;
16967 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16968 return NILP (XCDR (tem)) ? 1 : 2;
16969 }
16970 }
16971 }
16972
16973 return 0;
16974 }
16975
16976 /* Calculate a width or height in pixels from a specification using
16977 the following elements:
16978
16979 SPEC ::=
16980 NUM - a (fractional) multiple of the default font width/height
16981 (NUM) - specifies exactly NUM pixels
16982 UNIT - a fixed number of pixels, see below.
16983 ELEMENT - size of a display element in pixels, see below.
16984 (NUM . SPEC) - equals NUM * SPEC
16985 (+ SPEC SPEC ...) - add pixel values
16986 (- SPEC SPEC ...) - subtract pixel values
16987 (- SPEC) - negate pixel value
16988
16989 NUM ::=
16990 INT or FLOAT - a number constant
16991 SYMBOL - use symbol's (buffer local) variable binding.
16992
16993 UNIT ::=
16994 in - pixels per inch *)
16995 mm - pixels per 1/1000 meter *)
16996 cm - pixels per 1/100 meter *)
16997 width - width of current font in pixels.
16998 height - height of current font in pixels.
16999
17000 *) using the ratio(s) defined in display-pixels-per-inch.
17001
17002 ELEMENT ::=
17003
17004 left-fringe - left fringe width in pixels
17005 right-fringe - right fringe width in pixels
17006
17007 left-margin - left margin width in pixels
17008 right-margin - right margin width in pixels
17009
17010 scroll-bar - scroll-bar area width in pixels
17011
17012 Examples:
17013
17014 Pixels corresponding to 5 inches:
17015 (5 . in)
17016
17017 Total width of non-text areas on left side of window (if scroll-bar is on left):
17018 '(space :width (+ left-fringe left-margin scroll-bar))
17019
17020 Align to first text column (in header line):
17021 '(space :align-to 0)
17022
17023 Align to middle of text area minus half the width of variable `my-image'
17024 containing a loaded image:
17025 '(space :align-to (0.5 . (- text my-image)))
17026
17027 Width of left margin minus width of 1 character in the default font:
17028 '(space :width (- left-margin 1))
17029
17030 Width of left margin minus width of 2 characters in the current font:
17031 '(space :width (- left-margin (2 . width)))
17032
17033 Center 1 character over left-margin (in header line):
17034 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17035
17036 Different ways to express width of left fringe plus left margin minus one pixel:
17037 '(space :width (- (+ left-fringe left-margin) (1)))
17038 '(space :width (+ left-fringe left-margin (- (1))))
17039 '(space :width (+ left-fringe left-margin (-1)))
17040
17041 */
17042
17043 #define NUMVAL(X) \
17044 ((INTEGERP (X) || FLOATP (X)) \
17045 ? XFLOATINT (X) \
17046 : - 1)
17047
17048 int
17049 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17050 double *res;
17051 struct it *it;
17052 Lisp_Object prop;
17053 void *font;
17054 int width_p, *align_to;
17055 {
17056 double pixels;
17057
17058 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17059 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17060
17061 if (NILP (prop))
17062 return OK_PIXELS (0);
17063
17064 if (SYMBOLP (prop))
17065 {
17066 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17067 {
17068 char *unit = SDATA (SYMBOL_NAME (prop));
17069
17070 if (unit[0] == 'i' && unit[1] == 'n')
17071 pixels = 1.0;
17072 else if (unit[0] == 'm' && unit[1] == 'm')
17073 pixels = 25.4;
17074 else if (unit[0] == 'c' && unit[1] == 'm')
17075 pixels = 2.54;
17076 else
17077 pixels = 0;
17078 if (pixels > 0)
17079 {
17080 double ppi;
17081 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17082 || (CONSP (Vdisplay_pixels_per_inch)
17083 && (ppi = (width_p
17084 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17085 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17086 ppi > 0)))
17087 return OK_PIXELS (ppi / pixels);
17088
17089 return 0;
17090 }
17091 }
17092
17093 #ifdef HAVE_WINDOW_SYSTEM
17094 if (EQ (prop, Qheight))
17095 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17096 if (EQ (prop, Qwidth))
17097 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17098 #else
17099 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17100 return OK_PIXELS (1);
17101 #endif
17102
17103 if (EQ (prop, Qtext))
17104 return OK_PIXELS (width_p
17105 ? window_box_width (it->w, TEXT_AREA)
17106 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17107
17108 if (align_to && *align_to < 0)
17109 {
17110 *res = 0;
17111 if (EQ (prop, Qleft))
17112 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17113 if (EQ (prop, Qright))
17114 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17115 if (EQ (prop, Qcenter))
17116 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17117 + window_box_width (it->w, TEXT_AREA) / 2);
17118 if (EQ (prop, Qleft_fringe))
17119 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17120 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17121 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17122 if (EQ (prop, Qright_fringe))
17123 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17124 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17125 : window_box_right_offset (it->w, TEXT_AREA));
17126 if (EQ (prop, Qleft_margin))
17127 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17128 if (EQ (prop, Qright_margin))
17129 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17130 if (EQ (prop, Qscroll_bar))
17131 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17132 ? 0
17133 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17134 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17135 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17136 : 0)));
17137 }
17138 else
17139 {
17140 if (EQ (prop, Qleft_fringe))
17141 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17142 if (EQ (prop, Qright_fringe))
17143 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17144 if (EQ (prop, Qleft_margin))
17145 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17146 if (EQ (prop, Qright_margin))
17147 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17148 if (EQ (prop, Qscroll_bar))
17149 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17150 }
17151
17152 prop = Fbuffer_local_value (prop, it->w->buffer);
17153 }
17154
17155 if (INTEGERP (prop) || FLOATP (prop))
17156 {
17157 int base_unit = (width_p
17158 ? FRAME_COLUMN_WIDTH (it->f)
17159 : FRAME_LINE_HEIGHT (it->f));
17160 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17161 }
17162
17163 if (CONSP (prop))
17164 {
17165 Lisp_Object car = XCAR (prop);
17166 Lisp_Object cdr = XCDR (prop);
17167
17168 if (SYMBOLP (car))
17169 {
17170 #ifdef HAVE_WINDOW_SYSTEM
17171 if (valid_image_p (prop))
17172 {
17173 int id = lookup_image (it->f, prop);
17174 struct image *img = IMAGE_FROM_ID (it->f, id);
17175
17176 return OK_PIXELS (width_p ? img->width : img->height);
17177 }
17178 #endif
17179 if (EQ (car, Qplus) || EQ (car, Qminus))
17180 {
17181 int first = 1;
17182 double px;
17183
17184 pixels = 0;
17185 while (CONSP (cdr))
17186 {
17187 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17188 font, width_p, align_to))
17189 return 0;
17190 if (first)
17191 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17192 else
17193 pixels += px;
17194 cdr = XCDR (cdr);
17195 }
17196 if (EQ (car, Qminus))
17197 pixels = -pixels;
17198 return OK_PIXELS (pixels);
17199 }
17200
17201 car = Fbuffer_local_value (car, it->w->buffer);
17202 }
17203
17204 if (INTEGERP (car) || FLOATP (car))
17205 {
17206 double fact;
17207 pixels = XFLOATINT (car);
17208 if (NILP (cdr))
17209 return OK_PIXELS (pixels);
17210 if (calc_pixel_width_or_height (&fact, it, cdr,
17211 font, width_p, align_to))
17212 return OK_PIXELS (pixels * fact);
17213 return 0;
17214 }
17215
17216 return 0;
17217 }
17218
17219 return 0;
17220 }
17221
17222 \f
17223 /***********************************************************************
17224 Glyph Display
17225 ***********************************************************************/
17226
17227 #ifdef HAVE_WINDOW_SYSTEM
17228
17229 #if GLYPH_DEBUG
17230
17231 void
17232 dump_glyph_string (s)
17233 struct glyph_string *s;
17234 {
17235 fprintf (stderr, "glyph string\n");
17236 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17237 s->x, s->y, s->width, s->height);
17238 fprintf (stderr, " ybase = %d\n", s->ybase);
17239 fprintf (stderr, " hl = %d\n", s->hl);
17240 fprintf (stderr, " left overhang = %d, right = %d\n",
17241 s->left_overhang, s->right_overhang);
17242 fprintf (stderr, " nchars = %d\n", s->nchars);
17243 fprintf (stderr, " extends to end of line = %d\n",
17244 s->extends_to_end_of_line_p);
17245 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17246 fprintf (stderr, " bg width = %d\n", s->background_width);
17247 }
17248
17249 #endif /* GLYPH_DEBUG */
17250
17251 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17252 of XChar2b structures for S; it can't be allocated in
17253 init_glyph_string because it must be allocated via `alloca'. W
17254 is the window on which S is drawn. ROW and AREA are the glyph row
17255 and area within the row from which S is constructed. START is the
17256 index of the first glyph structure covered by S. HL is a
17257 face-override for drawing S. */
17258
17259 #ifdef HAVE_NTGUI
17260 #define OPTIONAL_HDC(hdc) hdc,
17261 #define DECLARE_HDC(hdc) HDC hdc;
17262 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17263 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17264 #endif
17265
17266 #ifndef OPTIONAL_HDC
17267 #define OPTIONAL_HDC(hdc)
17268 #define DECLARE_HDC(hdc)
17269 #define ALLOCATE_HDC(hdc, f)
17270 #define RELEASE_HDC(hdc, f)
17271 #endif
17272
17273 static void
17274 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17275 struct glyph_string *s;
17276 DECLARE_HDC (hdc)
17277 XChar2b *char2b;
17278 struct window *w;
17279 struct glyph_row *row;
17280 enum glyph_row_area area;
17281 int start;
17282 enum draw_glyphs_face hl;
17283 {
17284 bzero (s, sizeof *s);
17285 s->w = w;
17286 s->f = XFRAME (w->frame);
17287 #ifdef HAVE_NTGUI
17288 s->hdc = hdc;
17289 #endif
17290 s->display = FRAME_X_DISPLAY (s->f);
17291 s->window = FRAME_X_WINDOW (s->f);
17292 s->char2b = char2b;
17293 s->hl = hl;
17294 s->row = row;
17295 s->area = area;
17296 s->first_glyph = row->glyphs[area] + start;
17297 s->height = row->height;
17298 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17299
17300 /* Display the internal border below the tool-bar window. */
17301 if (WINDOWP (s->f->tool_bar_window)
17302 && s->w == XWINDOW (s->f->tool_bar_window))
17303 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17304
17305 s->ybase = s->y + row->ascent;
17306 }
17307
17308
17309 /* Append the list of glyph strings with head H and tail T to the list
17310 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17311
17312 static INLINE void
17313 append_glyph_string_lists (head, tail, h, t)
17314 struct glyph_string **head, **tail;
17315 struct glyph_string *h, *t;
17316 {
17317 if (h)
17318 {
17319 if (*head)
17320 (*tail)->next = h;
17321 else
17322 *head = h;
17323 h->prev = *tail;
17324 *tail = t;
17325 }
17326 }
17327
17328
17329 /* Prepend the list of glyph strings with head H and tail T to the
17330 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17331 result. */
17332
17333 static INLINE void
17334 prepend_glyph_string_lists (head, tail, h, t)
17335 struct glyph_string **head, **tail;
17336 struct glyph_string *h, *t;
17337 {
17338 if (h)
17339 {
17340 if (*head)
17341 (*head)->prev = t;
17342 else
17343 *tail = t;
17344 t->next = *head;
17345 *head = h;
17346 }
17347 }
17348
17349
17350 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17351 Set *HEAD and *TAIL to the resulting list. */
17352
17353 static INLINE void
17354 append_glyph_string (head, tail, s)
17355 struct glyph_string **head, **tail;
17356 struct glyph_string *s;
17357 {
17358 s->next = s->prev = NULL;
17359 append_glyph_string_lists (head, tail, s, s);
17360 }
17361
17362
17363 /* Get face and two-byte form of character glyph GLYPH on frame F.
17364 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17365 a pointer to a realized face that is ready for display. */
17366
17367 static INLINE struct face *
17368 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17369 struct frame *f;
17370 struct glyph *glyph;
17371 XChar2b *char2b;
17372 int *two_byte_p;
17373 {
17374 struct face *face;
17375
17376 xassert (glyph->type == CHAR_GLYPH);
17377 face = FACE_FROM_ID (f, glyph->face_id);
17378
17379 if (two_byte_p)
17380 *two_byte_p = 0;
17381
17382 if (!glyph->multibyte_p)
17383 {
17384 /* Unibyte case. We don't have to encode, but we have to make
17385 sure to use a face suitable for unibyte. */
17386 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17387 }
17388 else if (glyph->u.ch < 128
17389 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17390 {
17391 /* Case of ASCII in a face known to fit ASCII. */
17392 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17393 }
17394 else
17395 {
17396 int c1, c2, charset;
17397
17398 /* Split characters into bytes. If c2 is -1 afterwards, C is
17399 really a one-byte character so that byte1 is zero. */
17400 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17401 if (c2 > 0)
17402 STORE_XCHAR2B (char2b, c1, c2);
17403 else
17404 STORE_XCHAR2B (char2b, 0, c1);
17405
17406 /* Maybe encode the character in *CHAR2B. */
17407 if (charset != CHARSET_ASCII)
17408 {
17409 struct font_info *font_info
17410 = FONT_INFO_FROM_ID (f, face->font_info_id);
17411 if (font_info)
17412 glyph->font_type
17413 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17414 }
17415 }
17416
17417 /* Make sure X resources of the face are allocated. */
17418 xassert (face != NULL);
17419 PREPARE_FACE_FOR_DISPLAY (f, face);
17420 return face;
17421 }
17422
17423
17424 /* Fill glyph string S with composition components specified by S->cmp.
17425
17426 FACES is an array of faces for all components of this composition.
17427 S->gidx is the index of the first component for S.
17428 OVERLAPS_P non-zero means S should draw the foreground only, and
17429 use its physical height for clipping.
17430
17431 Value is the index of a component not in S. */
17432
17433 static int
17434 fill_composite_glyph_string (s, faces, overlaps_p)
17435 struct glyph_string *s;
17436 struct face **faces;
17437 int overlaps_p;
17438 {
17439 int i;
17440
17441 xassert (s);
17442
17443 s->for_overlaps_p = overlaps_p;
17444
17445 s->face = faces[s->gidx];
17446 s->font = s->face->font;
17447 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17448
17449 /* For all glyphs of this composition, starting at the offset
17450 S->gidx, until we reach the end of the definition or encounter a
17451 glyph that requires the different face, add it to S. */
17452 ++s->nchars;
17453 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17454 ++s->nchars;
17455
17456 /* All glyph strings for the same composition has the same width,
17457 i.e. the width set for the first component of the composition. */
17458
17459 s->width = s->first_glyph->pixel_width;
17460
17461 /* If the specified font could not be loaded, use the frame's
17462 default font, but record the fact that we couldn't load it in
17463 the glyph string so that we can draw rectangles for the
17464 characters of the glyph string. */
17465 if (s->font == NULL)
17466 {
17467 s->font_not_found_p = 1;
17468 s->font = FRAME_FONT (s->f);
17469 }
17470
17471 /* Adjust base line for subscript/superscript text. */
17472 s->ybase += s->first_glyph->voffset;
17473
17474 xassert (s->face && s->face->gc);
17475
17476 /* This glyph string must always be drawn with 16-bit functions. */
17477 s->two_byte_p = 1;
17478
17479 return s->gidx + s->nchars;
17480 }
17481
17482
17483 /* Fill glyph string S from a sequence of character glyphs.
17484
17485 FACE_ID is the face id of the string. START is the index of the
17486 first glyph to consider, END is the index of the last + 1.
17487 OVERLAPS_P non-zero means S should draw the foreground only, and
17488 use its physical height for clipping.
17489
17490 Value is the index of the first glyph not in S. */
17491
17492 static int
17493 fill_glyph_string (s, face_id, start, end, overlaps_p)
17494 struct glyph_string *s;
17495 int face_id;
17496 int start, end, overlaps_p;
17497 {
17498 struct glyph *glyph, *last;
17499 int voffset;
17500 int glyph_not_available_p;
17501
17502 xassert (s->f == XFRAME (s->w->frame));
17503 xassert (s->nchars == 0);
17504 xassert (start >= 0 && end > start);
17505
17506 s->for_overlaps_p = overlaps_p,
17507 glyph = s->row->glyphs[s->area] + start;
17508 last = s->row->glyphs[s->area] + end;
17509 voffset = glyph->voffset;
17510
17511 glyph_not_available_p = glyph->glyph_not_available_p;
17512
17513 while (glyph < last
17514 && glyph->type == CHAR_GLYPH
17515 && glyph->voffset == voffset
17516 /* Same face id implies same font, nowadays. */
17517 && glyph->face_id == face_id
17518 && glyph->glyph_not_available_p == glyph_not_available_p)
17519 {
17520 int two_byte_p;
17521
17522 s->face = get_glyph_face_and_encoding (s->f, glyph,
17523 s->char2b + s->nchars,
17524 &two_byte_p);
17525 s->two_byte_p = two_byte_p;
17526 ++s->nchars;
17527 xassert (s->nchars <= end - start);
17528 s->width += glyph->pixel_width;
17529 ++glyph;
17530 }
17531
17532 s->font = s->face->font;
17533 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17534
17535 /* If the specified font could not be loaded, use the frame's font,
17536 but record the fact that we couldn't load it in
17537 S->font_not_found_p so that we can draw rectangles for the
17538 characters of the glyph string. */
17539 if (s->font == NULL || glyph_not_available_p)
17540 {
17541 s->font_not_found_p = 1;
17542 s->font = FRAME_FONT (s->f);
17543 }
17544
17545 /* Adjust base line for subscript/superscript text. */
17546 s->ybase += voffset;
17547
17548 xassert (s->face && s->face->gc);
17549 return glyph - s->row->glyphs[s->area];
17550 }
17551
17552
17553 /* Fill glyph string S from image glyph S->first_glyph. */
17554
17555 static void
17556 fill_image_glyph_string (s)
17557 struct glyph_string *s;
17558 {
17559 xassert (s->first_glyph->type == IMAGE_GLYPH);
17560 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17561 xassert (s->img);
17562 s->slice = s->first_glyph->slice;
17563 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17564 s->font = s->face->font;
17565 s->width = s->first_glyph->pixel_width;
17566
17567 /* Adjust base line for subscript/superscript text. */
17568 s->ybase += s->first_glyph->voffset;
17569 }
17570
17571
17572 /* Fill glyph string S from a sequence of stretch glyphs.
17573
17574 ROW is the glyph row in which the glyphs are found, AREA is the
17575 area within the row. START is the index of the first glyph to
17576 consider, END is the index of the last + 1.
17577
17578 Value is the index of the first glyph not in S. */
17579
17580 static int
17581 fill_stretch_glyph_string (s, row, area, start, end)
17582 struct glyph_string *s;
17583 struct glyph_row *row;
17584 enum glyph_row_area area;
17585 int start, end;
17586 {
17587 struct glyph *glyph, *last;
17588 int voffset, face_id;
17589
17590 xassert (s->first_glyph->type == STRETCH_GLYPH);
17591
17592 glyph = s->row->glyphs[s->area] + start;
17593 last = s->row->glyphs[s->area] + end;
17594 face_id = glyph->face_id;
17595 s->face = FACE_FROM_ID (s->f, face_id);
17596 s->font = s->face->font;
17597 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17598 s->width = glyph->pixel_width;
17599 voffset = glyph->voffset;
17600
17601 for (++glyph;
17602 (glyph < last
17603 && glyph->type == STRETCH_GLYPH
17604 && glyph->voffset == voffset
17605 && glyph->face_id == face_id);
17606 ++glyph)
17607 s->width += glyph->pixel_width;
17608
17609 /* Adjust base line for subscript/superscript text. */
17610 s->ybase += voffset;
17611
17612 /* The case that face->gc == 0 is handled when drawing the glyph
17613 string by calling PREPARE_FACE_FOR_DISPLAY. */
17614 xassert (s->face);
17615 return glyph - s->row->glyphs[s->area];
17616 }
17617
17618
17619 /* EXPORT for RIF:
17620 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17621 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17622 assumed to be zero. */
17623
17624 void
17625 x_get_glyph_overhangs (glyph, f, left, right)
17626 struct glyph *glyph;
17627 struct frame *f;
17628 int *left, *right;
17629 {
17630 *left = *right = 0;
17631
17632 if (glyph->type == CHAR_GLYPH)
17633 {
17634 XFontStruct *font;
17635 struct face *face;
17636 struct font_info *font_info;
17637 XChar2b char2b;
17638 XCharStruct *pcm;
17639
17640 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17641 font = face->font;
17642 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17643 if (font /* ++KFS: Should this be font_info ? */
17644 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17645 {
17646 if (pcm->rbearing > pcm->width)
17647 *right = pcm->rbearing - pcm->width;
17648 if (pcm->lbearing < 0)
17649 *left = -pcm->lbearing;
17650 }
17651 }
17652 }
17653
17654
17655 /* Return the index of the first glyph preceding glyph string S that
17656 is overwritten by S because of S's left overhang. Value is -1
17657 if no glyphs are overwritten. */
17658
17659 static int
17660 left_overwritten (s)
17661 struct glyph_string *s;
17662 {
17663 int k;
17664
17665 if (s->left_overhang)
17666 {
17667 int x = 0, i;
17668 struct glyph *glyphs = s->row->glyphs[s->area];
17669 int first = s->first_glyph - glyphs;
17670
17671 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17672 x -= glyphs[i].pixel_width;
17673
17674 k = i + 1;
17675 }
17676 else
17677 k = -1;
17678
17679 return k;
17680 }
17681
17682
17683 /* Return the index of the first glyph preceding glyph string S that
17684 is overwriting S because of its right overhang. Value is -1 if no
17685 glyph in front of S overwrites S. */
17686
17687 static int
17688 left_overwriting (s)
17689 struct glyph_string *s;
17690 {
17691 int i, k, x;
17692 struct glyph *glyphs = s->row->glyphs[s->area];
17693 int first = s->first_glyph - glyphs;
17694
17695 k = -1;
17696 x = 0;
17697 for (i = first - 1; i >= 0; --i)
17698 {
17699 int left, right;
17700 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17701 if (x + right > 0)
17702 k = i;
17703 x -= glyphs[i].pixel_width;
17704 }
17705
17706 return k;
17707 }
17708
17709
17710 /* Return the index of the last glyph following glyph string S that is
17711 not overwritten by S because of S's right overhang. Value is -1 if
17712 no such glyph is found. */
17713
17714 static int
17715 right_overwritten (s)
17716 struct glyph_string *s;
17717 {
17718 int k = -1;
17719
17720 if (s->right_overhang)
17721 {
17722 int x = 0, i;
17723 struct glyph *glyphs = s->row->glyphs[s->area];
17724 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17725 int end = s->row->used[s->area];
17726
17727 for (i = first; i < end && s->right_overhang > x; ++i)
17728 x += glyphs[i].pixel_width;
17729
17730 k = i;
17731 }
17732
17733 return k;
17734 }
17735
17736
17737 /* Return the index of the last glyph following glyph string S that
17738 overwrites S because of its left overhang. Value is negative
17739 if no such glyph is found. */
17740
17741 static int
17742 right_overwriting (s)
17743 struct glyph_string *s;
17744 {
17745 int i, k, x;
17746 int end = s->row->used[s->area];
17747 struct glyph *glyphs = s->row->glyphs[s->area];
17748 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17749
17750 k = -1;
17751 x = 0;
17752 for (i = first; i < end; ++i)
17753 {
17754 int left, right;
17755 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17756 if (x - left < 0)
17757 k = i;
17758 x += glyphs[i].pixel_width;
17759 }
17760
17761 return k;
17762 }
17763
17764
17765 /* Get face and two-byte form of character C in face FACE_ID on frame
17766 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17767 means we want to display multibyte text. DISPLAY_P non-zero means
17768 make sure that X resources for the face returned are allocated.
17769 Value is a pointer to a realized face that is ready for display if
17770 DISPLAY_P is non-zero. */
17771
17772 static INLINE struct face *
17773 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17774 struct frame *f;
17775 int c, face_id;
17776 XChar2b *char2b;
17777 int multibyte_p, display_p;
17778 {
17779 struct face *face = FACE_FROM_ID (f, face_id);
17780
17781 if (!multibyte_p)
17782 {
17783 /* Unibyte case. We don't have to encode, but we have to make
17784 sure to use a face suitable for unibyte. */
17785 STORE_XCHAR2B (char2b, 0, c);
17786 face_id = FACE_FOR_CHAR (f, face, c);
17787 face = FACE_FROM_ID (f, face_id);
17788 }
17789 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17790 {
17791 /* Case of ASCII in a face known to fit ASCII. */
17792 STORE_XCHAR2B (char2b, 0, c);
17793 }
17794 else
17795 {
17796 int c1, c2, charset;
17797
17798 /* Split characters into bytes. If c2 is -1 afterwards, C is
17799 really a one-byte character so that byte1 is zero. */
17800 SPLIT_CHAR (c, charset, c1, c2);
17801 if (c2 > 0)
17802 STORE_XCHAR2B (char2b, c1, c2);
17803 else
17804 STORE_XCHAR2B (char2b, 0, c1);
17805
17806 /* Maybe encode the character in *CHAR2B. */
17807 if (face->font != NULL)
17808 {
17809 struct font_info *font_info
17810 = FONT_INFO_FROM_ID (f, face->font_info_id);
17811 if (font_info)
17812 rif->encode_char (c, char2b, font_info, 0);
17813 }
17814 }
17815
17816 /* Make sure X resources of the face are allocated. */
17817 #ifdef HAVE_X_WINDOWS
17818 if (display_p)
17819 #endif
17820 {
17821 xassert (face != NULL);
17822 PREPARE_FACE_FOR_DISPLAY (f, face);
17823 }
17824
17825 return face;
17826 }
17827
17828
17829 /* Set background width of glyph string S. START is the index of the
17830 first glyph following S. LAST_X is the right-most x-position + 1
17831 in the drawing area. */
17832
17833 static INLINE void
17834 set_glyph_string_background_width (s, start, last_x)
17835 struct glyph_string *s;
17836 int start;
17837 int last_x;
17838 {
17839 /* If the face of this glyph string has to be drawn to the end of
17840 the drawing area, set S->extends_to_end_of_line_p. */
17841 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17842
17843 if (start == s->row->used[s->area]
17844 && s->area == TEXT_AREA
17845 && ((s->hl == DRAW_NORMAL_TEXT
17846 && (s->row->fill_line_p
17847 || s->face->background != default_face->background
17848 || s->face->stipple != default_face->stipple
17849 || s->row->mouse_face_p))
17850 || s->hl == DRAW_MOUSE_FACE
17851 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17852 && s->row->fill_line_p)))
17853 s->extends_to_end_of_line_p = 1;
17854
17855 /* If S extends its face to the end of the line, set its
17856 background_width to the distance to the right edge of the drawing
17857 area. */
17858 if (s->extends_to_end_of_line_p)
17859 s->background_width = last_x - s->x + 1;
17860 else
17861 s->background_width = s->width;
17862 }
17863
17864
17865 /* Compute overhangs and x-positions for glyph string S and its
17866 predecessors, or successors. X is the starting x-position for S.
17867 BACKWARD_P non-zero means process predecessors. */
17868
17869 static void
17870 compute_overhangs_and_x (s, x, backward_p)
17871 struct glyph_string *s;
17872 int x;
17873 int backward_p;
17874 {
17875 if (backward_p)
17876 {
17877 while (s)
17878 {
17879 if (rif->compute_glyph_string_overhangs)
17880 rif->compute_glyph_string_overhangs (s);
17881 x -= s->width;
17882 s->x = x;
17883 s = s->prev;
17884 }
17885 }
17886 else
17887 {
17888 while (s)
17889 {
17890 if (rif->compute_glyph_string_overhangs)
17891 rif->compute_glyph_string_overhangs (s);
17892 s->x = x;
17893 x += s->width;
17894 s = s->next;
17895 }
17896 }
17897 }
17898
17899
17900
17901 /* The following macros are only called from draw_glyphs below.
17902 They reference the following parameters of that function directly:
17903 `w', `row', `area', and `overlap_p'
17904 as well as the following local variables:
17905 `s', `f', and `hdc' (in W32) */
17906
17907 #ifdef HAVE_NTGUI
17908 /* On W32, silently add local `hdc' variable to argument list of
17909 init_glyph_string. */
17910 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17911 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17912 #else
17913 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17914 init_glyph_string (s, char2b, w, row, area, start, hl)
17915 #endif
17916
17917 /* Add a glyph string for a stretch glyph to the list of strings
17918 between HEAD and TAIL. START is the index of the stretch glyph in
17919 row area AREA of glyph row ROW. END is the index of the last glyph
17920 in that glyph row area. X is the current output position assigned
17921 to the new glyph string constructed. HL overrides that face of the
17922 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17923 is the right-most x-position of the drawing area. */
17924
17925 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17926 and below -- keep them on one line. */
17927 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17928 do \
17929 { \
17930 s = (struct glyph_string *) alloca (sizeof *s); \
17931 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17932 START = fill_stretch_glyph_string (s, row, area, START, END); \
17933 append_glyph_string (&HEAD, &TAIL, s); \
17934 s->x = (X); \
17935 } \
17936 while (0)
17937
17938
17939 /* Add a glyph string for an image glyph to the list of strings
17940 between HEAD and TAIL. START is the index of the image glyph in
17941 row area AREA of glyph row ROW. END is the index of the last glyph
17942 in that glyph row area. X is the current output position assigned
17943 to the new glyph string constructed. HL overrides that face of the
17944 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17945 is the right-most x-position of the drawing area. */
17946
17947 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17948 do \
17949 { \
17950 s = (struct glyph_string *) alloca (sizeof *s); \
17951 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17952 fill_image_glyph_string (s); \
17953 append_glyph_string (&HEAD, &TAIL, s); \
17954 ++START; \
17955 s->x = (X); \
17956 } \
17957 while (0)
17958
17959
17960 /* Add a glyph string for a sequence of character glyphs to the list
17961 of strings between HEAD and TAIL. START is the index of the first
17962 glyph in row area AREA of glyph row ROW that is part of the new
17963 glyph string. END is the index of the last glyph in that glyph row
17964 area. X is the current output position assigned to the new glyph
17965 string constructed. HL overrides that face of the glyph; e.g. it
17966 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17967 right-most x-position of the drawing area. */
17968
17969 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17970 do \
17971 { \
17972 int c, face_id; \
17973 XChar2b *char2b; \
17974 \
17975 c = (row)->glyphs[area][START].u.ch; \
17976 face_id = (row)->glyphs[area][START].face_id; \
17977 \
17978 s = (struct glyph_string *) alloca (sizeof *s); \
17979 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17980 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17981 append_glyph_string (&HEAD, &TAIL, s); \
17982 s->x = (X); \
17983 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17984 } \
17985 while (0)
17986
17987
17988 /* Add a glyph string for a composite sequence to the list of strings
17989 between HEAD and TAIL. START is the index of the first glyph in
17990 row area AREA of glyph row ROW that is part of the new glyph
17991 string. END is the index of the last glyph in that glyph row area.
17992 X is the current output position assigned to the new glyph string
17993 constructed. HL overrides that face of the glyph; e.g. it is
17994 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17995 x-position of the drawing area. */
17996
17997 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17998 do { \
17999 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18000 int face_id = (row)->glyphs[area][START].face_id; \
18001 struct face *base_face = FACE_FROM_ID (f, face_id); \
18002 struct composition *cmp = composition_table[cmp_id]; \
18003 int glyph_len = cmp->glyph_len; \
18004 XChar2b *char2b; \
18005 struct face **faces; \
18006 struct glyph_string *first_s = NULL; \
18007 int n; \
18008 \
18009 base_face = base_face->ascii_face; \
18010 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18011 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18012 /* At first, fill in `char2b' and `faces'. */ \
18013 for (n = 0; n < glyph_len; n++) \
18014 { \
18015 int c = COMPOSITION_GLYPH (cmp, n); \
18016 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18017 faces[n] = FACE_FROM_ID (f, this_face_id); \
18018 get_char_face_and_encoding (f, c, this_face_id, \
18019 char2b + n, 1, 1); \
18020 } \
18021 \
18022 /* Make glyph_strings for each glyph sequence that is drawable by \
18023 the same face, and append them to HEAD/TAIL. */ \
18024 for (n = 0; n < cmp->glyph_len;) \
18025 { \
18026 s = (struct glyph_string *) alloca (sizeof *s); \
18027 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18028 append_glyph_string (&(HEAD), &(TAIL), s); \
18029 s->cmp = cmp; \
18030 s->gidx = n; \
18031 s->x = (X); \
18032 \
18033 if (n == 0) \
18034 first_s = s; \
18035 \
18036 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18037 } \
18038 \
18039 ++START; \
18040 s = first_s; \
18041 } while (0)
18042
18043
18044 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18045 of AREA of glyph row ROW on window W between indices START and END.
18046 HL overrides the face for drawing glyph strings, e.g. it is
18047 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18048 x-positions of the drawing area.
18049
18050 This is an ugly monster macro construct because we must use alloca
18051 to allocate glyph strings (because draw_glyphs can be called
18052 asynchronously). */
18053
18054 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18055 do \
18056 { \
18057 HEAD = TAIL = NULL; \
18058 while (START < END) \
18059 { \
18060 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18061 switch (first_glyph->type) \
18062 { \
18063 case CHAR_GLYPH: \
18064 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18065 HL, X, LAST_X); \
18066 break; \
18067 \
18068 case COMPOSITE_GLYPH: \
18069 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18070 HL, X, LAST_X); \
18071 break; \
18072 \
18073 case STRETCH_GLYPH: \
18074 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18075 HL, X, LAST_X); \
18076 break; \
18077 \
18078 case IMAGE_GLYPH: \
18079 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18080 HL, X, LAST_X); \
18081 break; \
18082 \
18083 default: \
18084 abort (); \
18085 } \
18086 \
18087 set_glyph_string_background_width (s, START, LAST_X); \
18088 (X) += s->width; \
18089 } \
18090 } \
18091 while (0)
18092
18093
18094 /* Draw glyphs between START and END in AREA of ROW on window W,
18095 starting at x-position X. X is relative to AREA in W. HL is a
18096 face-override with the following meaning:
18097
18098 DRAW_NORMAL_TEXT draw normally
18099 DRAW_CURSOR draw in cursor face
18100 DRAW_MOUSE_FACE draw in mouse face.
18101 DRAW_INVERSE_VIDEO draw in mode line face
18102 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18103 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18104
18105 If OVERLAPS_P is non-zero, draw only the foreground of characters
18106 and clip to the physical height of ROW.
18107
18108 Value is the x-position reached, relative to AREA of W. */
18109
18110 static int
18111 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18112 struct window *w;
18113 int x;
18114 struct glyph_row *row;
18115 enum glyph_row_area area;
18116 int start, end;
18117 enum draw_glyphs_face hl;
18118 int overlaps_p;
18119 {
18120 struct glyph_string *head, *tail;
18121 struct glyph_string *s;
18122 int last_x, area_width;
18123 int x_reached;
18124 int i, j;
18125 struct frame *f = XFRAME (WINDOW_FRAME (w));
18126 DECLARE_HDC (hdc);
18127
18128 ALLOCATE_HDC (hdc, f);
18129
18130 /* Let's rather be paranoid than getting a SEGV. */
18131 end = min (end, row->used[area]);
18132 start = max (0, start);
18133 start = min (end, start);
18134
18135 /* Translate X to frame coordinates. Set last_x to the right
18136 end of the drawing area. */
18137 if (row->full_width_p)
18138 {
18139 /* X is relative to the left edge of W, without scroll bars
18140 or fringes. */
18141 x += WINDOW_LEFT_EDGE_X (w);
18142 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18143 }
18144 else
18145 {
18146 int area_left = window_box_left (w, area);
18147 x += area_left;
18148 area_width = window_box_width (w, area);
18149 last_x = area_left + area_width;
18150 }
18151
18152 /* Build a doubly-linked list of glyph_string structures between
18153 head and tail from what we have to draw. Note that the macro
18154 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18155 the reason we use a separate variable `i'. */
18156 i = start;
18157 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18158 if (tail)
18159 x_reached = tail->x + tail->background_width;
18160 else
18161 x_reached = x;
18162
18163 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18164 the row, redraw some glyphs in front or following the glyph
18165 strings built above. */
18166 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18167 {
18168 int dummy_x = 0;
18169 struct glyph_string *h, *t;
18170
18171 /* Compute overhangs for all glyph strings. */
18172 if (rif->compute_glyph_string_overhangs)
18173 for (s = head; s; s = s->next)
18174 rif->compute_glyph_string_overhangs (s);
18175
18176 /* Prepend glyph strings for glyphs in front of the first glyph
18177 string that are overwritten because of the first glyph
18178 string's left overhang. The background of all strings
18179 prepended must be drawn because the first glyph string
18180 draws over it. */
18181 i = left_overwritten (head);
18182 if (i >= 0)
18183 {
18184 j = i;
18185 BUILD_GLYPH_STRINGS (j, start, h, t,
18186 DRAW_NORMAL_TEXT, dummy_x, last_x);
18187 start = i;
18188 compute_overhangs_and_x (t, head->x, 1);
18189 prepend_glyph_string_lists (&head, &tail, h, t);
18190 }
18191
18192 /* Prepend glyph strings for glyphs in front of the first glyph
18193 string that overwrite that glyph string because of their
18194 right overhang. For these strings, only the foreground must
18195 be drawn, because it draws over the glyph string at `head'.
18196 The background must not be drawn because this would overwrite
18197 right overhangs of preceding glyphs for which no glyph
18198 strings exist. */
18199 i = left_overwriting (head);
18200 if (i >= 0)
18201 {
18202 BUILD_GLYPH_STRINGS (i, start, h, t,
18203 DRAW_NORMAL_TEXT, dummy_x, last_x);
18204 for (s = h; s; s = s->next)
18205 s->background_filled_p = 1;
18206 compute_overhangs_and_x (t, head->x, 1);
18207 prepend_glyph_string_lists (&head, &tail, h, t);
18208 }
18209
18210 /* Append glyphs strings for glyphs following the last glyph
18211 string tail that are overwritten by tail. The background of
18212 these strings has to be drawn because tail's foreground draws
18213 over it. */
18214 i = right_overwritten (tail);
18215 if (i >= 0)
18216 {
18217 BUILD_GLYPH_STRINGS (end, i, h, t,
18218 DRAW_NORMAL_TEXT, x, last_x);
18219 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18220 append_glyph_string_lists (&head, &tail, h, t);
18221 }
18222
18223 /* Append glyph strings for glyphs following the last glyph
18224 string tail that overwrite tail. The foreground of such
18225 glyphs has to be drawn because it writes into the background
18226 of tail. The background must not be drawn because it could
18227 paint over the foreground of following glyphs. */
18228 i = right_overwriting (tail);
18229 if (i >= 0)
18230 {
18231 BUILD_GLYPH_STRINGS (end, i, h, t,
18232 DRAW_NORMAL_TEXT, x, last_x);
18233 for (s = h; s; s = s->next)
18234 s->background_filled_p = 1;
18235 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18236 append_glyph_string_lists (&head, &tail, h, t);
18237 }
18238 }
18239
18240 /* Draw all strings. */
18241 for (s = head; s; s = s->next)
18242 rif->draw_glyph_string (s);
18243
18244 if (area == TEXT_AREA
18245 && !row->full_width_p
18246 /* When drawing overlapping rows, only the glyph strings'
18247 foreground is drawn, which doesn't erase a cursor
18248 completely. */
18249 && !overlaps_p)
18250 {
18251 int x0 = head ? head->x : x;
18252 int x1 = tail ? tail->x + tail->background_width : x;
18253
18254 int text_left = window_box_left (w, TEXT_AREA);
18255 x0 -= text_left;
18256 x1 -= text_left;
18257
18258 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18259 row->y, MATRIX_ROW_BOTTOM_Y (row));
18260 }
18261
18262 /* Value is the x-position up to which drawn, relative to AREA of W.
18263 This doesn't include parts drawn because of overhangs. */
18264 if (row->full_width_p)
18265 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18266 else
18267 x_reached -= window_box_left (w, area);
18268
18269 RELEASE_HDC (hdc, f);
18270
18271 return x_reached;
18272 }
18273
18274 /* Expand row matrix if too narrow. Don't expand if area
18275 is not present. */
18276
18277 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18278 { \
18279 if (!fonts_changed_p \
18280 && (it->glyph_row->glyphs[area] \
18281 < it->glyph_row->glyphs[area + 1])) \
18282 { \
18283 it->w->ncols_scale_factor++; \
18284 fonts_changed_p = 1; \
18285 } \
18286 }
18287
18288 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18289 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18290
18291 static INLINE void
18292 append_glyph (it)
18293 struct it *it;
18294 {
18295 struct glyph *glyph;
18296 enum glyph_row_area area = it->area;
18297
18298 xassert (it->glyph_row);
18299 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18300
18301 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18302 if (glyph < it->glyph_row->glyphs[area + 1])
18303 {
18304 glyph->charpos = CHARPOS (it->position);
18305 glyph->object = it->object;
18306 glyph->pixel_width = it->pixel_width;
18307 glyph->ascent = it->ascent;
18308 glyph->descent = it->descent;
18309 glyph->voffset = it->voffset;
18310 glyph->type = CHAR_GLYPH;
18311 glyph->multibyte_p = it->multibyte_p;
18312 glyph->left_box_line_p = it->start_of_box_run_p;
18313 glyph->right_box_line_p = it->end_of_box_run_p;
18314 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18315 || it->phys_descent > it->descent);
18316 glyph->padding_p = 0;
18317 glyph->glyph_not_available_p = it->glyph_not_available_p;
18318 glyph->face_id = it->face_id;
18319 glyph->u.ch = it->char_to_display;
18320 glyph->slice = null_glyph_slice;
18321 glyph->font_type = FONT_TYPE_UNKNOWN;
18322 ++it->glyph_row->used[area];
18323 }
18324 else
18325 IT_EXPAND_MATRIX_WIDTH (it, area);
18326 }
18327
18328 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18329 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18330
18331 static INLINE void
18332 append_composite_glyph (it)
18333 struct it *it;
18334 {
18335 struct glyph *glyph;
18336 enum glyph_row_area area = it->area;
18337
18338 xassert (it->glyph_row);
18339
18340 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18341 if (glyph < it->glyph_row->glyphs[area + 1])
18342 {
18343 glyph->charpos = CHARPOS (it->position);
18344 glyph->object = it->object;
18345 glyph->pixel_width = it->pixel_width;
18346 glyph->ascent = it->ascent;
18347 glyph->descent = it->descent;
18348 glyph->voffset = it->voffset;
18349 glyph->type = COMPOSITE_GLYPH;
18350 glyph->multibyte_p = it->multibyte_p;
18351 glyph->left_box_line_p = it->start_of_box_run_p;
18352 glyph->right_box_line_p = it->end_of_box_run_p;
18353 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18354 || it->phys_descent > it->descent);
18355 glyph->padding_p = 0;
18356 glyph->glyph_not_available_p = 0;
18357 glyph->face_id = it->face_id;
18358 glyph->u.cmp_id = it->cmp_id;
18359 glyph->slice = null_glyph_slice;
18360 glyph->font_type = FONT_TYPE_UNKNOWN;
18361 ++it->glyph_row->used[area];
18362 }
18363 else
18364 IT_EXPAND_MATRIX_WIDTH (it, area);
18365 }
18366
18367
18368 /* Change IT->ascent and IT->height according to the setting of
18369 IT->voffset. */
18370
18371 static INLINE void
18372 take_vertical_position_into_account (it)
18373 struct it *it;
18374 {
18375 if (it->voffset)
18376 {
18377 if (it->voffset < 0)
18378 /* Increase the ascent so that we can display the text higher
18379 in the line. */
18380 it->ascent -= it->voffset;
18381 else
18382 /* Increase the descent so that we can display the text lower
18383 in the line. */
18384 it->descent += it->voffset;
18385 }
18386 }
18387
18388
18389 /* Produce glyphs/get display metrics for the image IT is loaded with.
18390 See the description of struct display_iterator in dispextern.h for
18391 an overview of struct display_iterator. */
18392
18393 static void
18394 produce_image_glyph (it)
18395 struct it *it;
18396 {
18397 struct image *img;
18398 struct face *face;
18399 int glyph_ascent;
18400 struct glyph_slice slice;
18401
18402 xassert (it->what == IT_IMAGE);
18403
18404 face = FACE_FROM_ID (it->f, it->face_id);
18405 xassert (face);
18406 /* Make sure X resources of the face is loaded. */
18407 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18408
18409 if (it->image_id < 0)
18410 {
18411 /* Fringe bitmap. */
18412 it->ascent = it->phys_ascent = 0;
18413 it->descent = it->phys_descent = 0;
18414 it->pixel_width = 0;
18415 it->nglyphs = 0;
18416 return;
18417 }
18418
18419 img = IMAGE_FROM_ID (it->f, it->image_id);
18420 xassert (img);
18421 /* Make sure X resources of the image is loaded. */
18422 prepare_image_for_display (it->f, img);
18423
18424 slice.x = slice.y = 0;
18425 slice.width = img->width;
18426 slice.height = img->height;
18427
18428 if (INTEGERP (it->slice.x))
18429 slice.x = XINT (it->slice.x);
18430 else if (FLOATP (it->slice.x))
18431 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18432
18433 if (INTEGERP (it->slice.y))
18434 slice.y = XINT (it->slice.y);
18435 else if (FLOATP (it->slice.y))
18436 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18437
18438 if (INTEGERP (it->slice.width))
18439 slice.width = XINT (it->slice.width);
18440 else if (FLOATP (it->slice.width))
18441 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18442
18443 if (INTEGERP (it->slice.height))
18444 slice.height = XINT (it->slice.height);
18445 else if (FLOATP (it->slice.height))
18446 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18447
18448 if (slice.x >= img->width)
18449 slice.x = img->width;
18450 if (slice.y >= img->height)
18451 slice.y = img->height;
18452 if (slice.x + slice.width >= img->width)
18453 slice.width = img->width - slice.x;
18454 if (slice.y + slice.height > img->height)
18455 slice.height = img->height - slice.y;
18456
18457 if (slice.width == 0 || slice.height == 0)
18458 return;
18459
18460 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18461
18462 it->descent = slice.height - glyph_ascent;
18463 if (slice.y == 0)
18464 it->descent += img->vmargin;
18465 if (slice.y + slice.height == img->height)
18466 it->descent += img->vmargin;
18467 it->phys_descent = it->descent;
18468
18469 it->pixel_width = slice.width;
18470 if (slice.x == 0)
18471 it->pixel_width += img->hmargin;
18472 if (slice.x + slice.width == img->width)
18473 it->pixel_width += img->hmargin;
18474
18475 /* It's quite possible for images to have an ascent greater than
18476 their height, so don't get confused in that case. */
18477 if (it->descent < 0)
18478 it->descent = 0;
18479
18480 #if 0 /* this breaks image tiling */
18481 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18482 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18483 if (face_ascent > it->ascent)
18484 it->ascent = it->phys_ascent = face_ascent;
18485 #endif
18486
18487 it->nglyphs = 1;
18488
18489 if (face->box != FACE_NO_BOX)
18490 {
18491 if (face->box_line_width > 0)
18492 {
18493 if (slice.y == 0)
18494 it->ascent += face->box_line_width;
18495 if (slice.y + slice.height == img->height)
18496 it->descent += face->box_line_width;
18497 }
18498
18499 if (it->start_of_box_run_p && slice.x == 0)
18500 it->pixel_width += abs (face->box_line_width);
18501 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18502 it->pixel_width += abs (face->box_line_width);
18503 }
18504
18505 take_vertical_position_into_account (it);
18506
18507 if (it->glyph_row)
18508 {
18509 struct glyph *glyph;
18510 enum glyph_row_area area = it->area;
18511
18512 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18513 if (glyph < it->glyph_row->glyphs[area + 1])
18514 {
18515 glyph->charpos = CHARPOS (it->position);
18516 glyph->object = it->object;
18517 glyph->pixel_width = it->pixel_width;
18518 glyph->ascent = glyph_ascent;
18519 glyph->descent = it->descent;
18520 glyph->voffset = it->voffset;
18521 glyph->type = IMAGE_GLYPH;
18522 glyph->multibyte_p = it->multibyte_p;
18523 glyph->left_box_line_p = it->start_of_box_run_p;
18524 glyph->right_box_line_p = it->end_of_box_run_p;
18525 glyph->overlaps_vertically_p = 0;
18526 glyph->padding_p = 0;
18527 glyph->glyph_not_available_p = 0;
18528 glyph->face_id = it->face_id;
18529 glyph->u.img_id = img->id;
18530 glyph->slice = slice;
18531 glyph->font_type = FONT_TYPE_UNKNOWN;
18532 ++it->glyph_row->used[area];
18533 }
18534 else
18535 IT_EXPAND_MATRIX_WIDTH (it, area);
18536 }
18537 }
18538
18539
18540 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18541 of the glyph, WIDTH and HEIGHT are the width and height of the
18542 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18543
18544 static void
18545 append_stretch_glyph (it, object, width, height, ascent)
18546 struct it *it;
18547 Lisp_Object object;
18548 int width, height;
18549 int ascent;
18550 {
18551 struct glyph *glyph;
18552 enum glyph_row_area area = it->area;
18553
18554 xassert (ascent >= 0 && ascent <= height);
18555
18556 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18557 if (glyph < it->glyph_row->glyphs[area + 1])
18558 {
18559 glyph->charpos = CHARPOS (it->position);
18560 glyph->object = object;
18561 glyph->pixel_width = width;
18562 glyph->ascent = ascent;
18563 glyph->descent = height - ascent;
18564 glyph->voffset = it->voffset;
18565 glyph->type = STRETCH_GLYPH;
18566 glyph->multibyte_p = it->multibyte_p;
18567 glyph->left_box_line_p = it->start_of_box_run_p;
18568 glyph->right_box_line_p = it->end_of_box_run_p;
18569 glyph->overlaps_vertically_p = 0;
18570 glyph->padding_p = 0;
18571 glyph->glyph_not_available_p = 0;
18572 glyph->face_id = it->face_id;
18573 glyph->u.stretch.ascent = ascent;
18574 glyph->u.stretch.height = height;
18575 glyph->slice = null_glyph_slice;
18576 glyph->font_type = FONT_TYPE_UNKNOWN;
18577 ++it->glyph_row->used[area];
18578 }
18579 else
18580 IT_EXPAND_MATRIX_WIDTH (it, area);
18581 }
18582
18583
18584 /* Produce a stretch glyph for iterator IT. IT->object is the value
18585 of the glyph property displayed. The value must be a list
18586 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18587 being recognized:
18588
18589 1. `:width WIDTH' specifies that the space should be WIDTH *
18590 canonical char width wide. WIDTH may be an integer or floating
18591 point number.
18592
18593 2. `:relative-width FACTOR' specifies that the width of the stretch
18594 should be computed from the width of the first character having the
18595 `glyph' property, and should be FACTOR times that width.
18596
18597 3. `:align-to HPOS' specifies that the space should be wide enough
18598 to reach HPOS, a value in canonical character units.
18599
18600 Exactly one of the above pairs must be present.
18601
18602 4. `:height HEIGHT' specifies that the height of the stretch produced
18603 should be HEIGHT, measured in canonical character units.
18604
18605 5. `:relative-height FACTOR' specifies that the height of the
18606 stretch should be FACTOR times the height of the characters having
18607 the glyph property.
18608
18609 Either none or exactly one of 4 or 5 must be present.
18610
18611 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18612 of the stretch should be used for the ascent of the stretch.
18613 ASCENT must be in the range 0 <= ASCENT <= 100. */
18614
18615 static void
18616 produce_stretch_glyph (it)
18617 struct it *it;
18618 {
18619 /* (space :width WIDTH :height HEIGHT ...) */
18620 Lisp_Object prop, plist;
18621 int width = 0, height = 0, align_to = -1;
18622 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18623 int ascent = 0;
18624 double tem;
18625 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18626 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18627
18628 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18629
18630 /* List should start with `space'. */
18631 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18632 plist = XCDR (it->object);
18633
18634 /* Compute the width of the stretch. */
18635 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18636 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18637 {
18638 /* Absolute width `:width WIDTH' specified and valid. */
18639 zero_width_ok_p = 1;
18640 width = (int)tem;
18641 }
18642 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18643 NUMVAL (prop) > 0)
18644 {
18645 /* Relative width `:relative-width FACTOR' specified and valid.
18646 Compute the width of the characters having the `glyph'
18647 property. */
18648 struct it it2;
18649 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18650
18651 it2 = *it;
18652 if (it->multibyte_p)
18653 {
18654 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18655 - IT_BYTEPOS (*it));
18656 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18657 }
18658 else
18659 it2.c = *p, it2.len = 1;
18660
18661 it2.glyph_row = NULL;
18662 it2.what = IT_CHARACTER;
18663 x_produce_glyphs (&it2);
18664 width = NUMVAL (prop) * it2.pixel_width;
18665 }
18666 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18667 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18668 {
18669 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18670 align_to = (align_to < 0
18671 ? 0
18672 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18673 else if (align_to < 0)
18674 align_to = window_box_left_offset (it->w, TEXT_AREA);
18675 width = max (0, (int)tem + align_to - it->current_x);
18676 zero_width_ok_p = 1;
18677 }
18678 else
18679 /* Nothing specified -> width defaults to canonical char width. */
18680 width = FRAME_COLUMN_WIDTH (it->f);
18681
18682 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18683 width = 1;
18684
18685 /* Compute height. */
18686 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18687 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18688 {
18689 height = (int)tem;
18690 zero_height_ok_p = 1;
18691 }
18692 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18693 NUMVAL (prop) > 0)
18694 height = FONT_HEIGHT (font) * NUMVAL (prop);
18695 else
18696 height = FONT_HEIGHT (font);
18697
18698 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18699 height = 1;
18700
18701 /* Compute percentage of height used for ascent. If
18702 `:ascent ASCENT' is present and valid, use that. Otherwise,
18703 derive the ascent from the font in use. */
18704 if (prop = Fsafe_plist_get (plist, QCascent),
18705 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18706 ascent = height * NUMVAL (prop) / 100.0;
18707 else if (!NILP (prop)
18708 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18709 ascent = min (max (0, (int)tem), height);
18710 else
18711 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18712
18713 if (width > 0 && height > 0 && it->glyph_row)
18714 {
18715 Lisp_Object object = it->stack[it->sp - 1].string;
18716 if (!STRINGP (object))
18717 object = it->w->buffer;
18718 append_stretch_glyph (it, object, width, height, ascent);
18719 }
18720
18721 it->pixel_width = width;
18722 it->ascent = it->phys_ascent = ascent;
18723 it->descent = it->phys_descent = height - it->ascent;
18724 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18725
18726 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18727 {
18728 if (face->box_line_width > 0)
18729 {
18730 it->ascent += face->box_line_width;
18731 it->descent += face->box_line_width;
18732 }
18733
18734 if (it->start_of_box_run_p)
18735 it->pixel_width += abs (face->box_line_width);
18736 if (it->end_of_box_run_p)
18737 it->pixel_width += abs (face->box_line_width);
18738 }
18739
18740 take_vertical_position_into_account (it);
18741 }
18742
18743 /* Calculate line-height and line-spacing properties.
18744 An integer value specifies explicit pixel value.
18745 A float value specifies relative value to current face height.
18746 A cons (float . face-name) specifies relative value to
18747 height of specified face font.
18748
18749 Returns height in pixels, or nil. */
18750
18751 static Lisp_Object
18752 calc_line_height_property (it, prop, font, boff, total)
18753 struct it *it;
18754 Lisp_Object prop;
18755 XFontStruct *font;
18756 int boff, *total;
18757 {
18758 Lisp_Object position, val;
18759 Lisp_Object face_name = Qnil;
18760 int ascent, descent, height, override;
18761
18762 if (STRINGP (it->object))
18763 position = make_number (IT_STRING_CHARPOS (*it));
18764 else if (BUFFERP (it->object))
18765 position = make_number (IT_CHARPOS (*it));
18766 else
18767 return Qnil;
18768
18769 val = Fget_char_property (position, prop, it->object);
18770
18771 if (NILP (val))
18772 return val;
18773
18774 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18775 {
18776 *total = 1;
18777 val = XCDR (val);
18778 }
18779
18780 if (INTEGERP (val))
18781 return val;
18782
18783 if (CONSP (val))
18784 {
18785 face_name = XCDR (val);
18786 val = XCAR (val);
18787 }
18788 else if (SYMBOLP (val))
18789 {
18790 face_name = val;
18791 val = Qnil;
18792 }
18793
18794 override = EQ (prop, Qline_height);
18795
18796 if (NILP (face_name))
18797 {
18798 font = FRAME_FONT (it->f);
18799 boff = FRAME_BASELINE_OFFSET (it->f);
18800 }
18801 else if (EQ (face_name, Qt))
18802 {
18803 override = 0;
18804 }
18805 else
18806 {
18807 int face_id;
18808 struct face *face;
18809 struct font_info *font_info;
18810
18811 face_id = lookup_named_face (it->f, face_name, ' ');
18812 if (face_id < 0)
18813 return make_number (-1);
18814
18815 face = FACE_FROM_ID (it->f, face_id);
18816 font = face->font;
18817 if (font == NULL)
18818 return make_number (-1);
18819
18820 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18821 boff = font_info->baseline_offset;
18822 if (font_info->vertical_centering)
18823 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18824 }
18825
18826 ascent = FONT_BASE (font) + boff;
18827 descent = FONT_DESCENT (font) - boff;
18828
18829 if (override)
18830 {
18831 it->override_ascent = ascent;
18832 it->override_descent = descent;
18833 it->override_boff = boff;
18834 }
18835
18836 height = ascent + descent;
18837 if (FLOATP (val))
18838 height = (int)(XFLOAT_DATA (val) * height);
18839 else if (INTEGERP (val))
18840 height *= XINT (val);
18841
18842 return make_number (height);
18843 }
18844
18845
18846 /* RIF:
18847 Produce glyphs/get display metrics for the display element IT is
18848 loaded with. See the description of struct display_iterator in
18849 dispextern.h for an overview of struct display_iterator. */
18850
18851 void
18852 x_produce_glyphs (it)
18853 struct it *it;
18854 {
18855 int extra_line_spacing = it->extra_line_spacing;
18856
18857 it->glyph_not_available_p = 0;
18858
18859 if (it->what == IT_CHARACTER)
18860 {
18861 XChar2b char2b;
18862 XFontStruct *font;
18863 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18864 XCharStruct *pcm;
18865 int font_not_found_p;
18866 struct font_info *font_info;
18867 int boff; /* baseline offset */
18868 /* We may change it->multibyte_p upon unibyte<->multibyte
18869 conversion. So, save the current value now and restore it
18870 later.
18871
18872 Note: It seems that we don't have to record multibyte_p in
18873 struct glyph because the character code itself tells if or
18874 not the character is multibyte. Thus, in the future, we must
18875 consider eliminating the field `multibyte_p' in the struct
18876 glyph. */
18877 int saved_multibyte_p = it->multibyte_p;
18878
18879 /* Maybe translate single-byte characters to multibyte, or the
18880 other way. */
18881 it->char_to_display = it->c;
18882 if (!ASCII_BYTE_P (it->c))
18883 {
18884 if (unibyte_display_via_language_environment
18885 && SINGLE_BYTE_CHAR_P (it->c)
18886 && (it->c >= 0240
18887 || !NILP (Vnonascii_translation_table)))
18888 {
18889 it->char_to_display = unibyte_char_to_multibyte (it->c);
18890 it->multibyte_p = 1;
18891 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18892 face = FACE_FROM_ID (it->f, it->face_id);
18893 }
18894 else if (!SINGLE_BYTE_CHAR_P (it->c)
18895 && !it->multibyte_p)
18896 {
18897 it->multibyte_p = 1;
18898 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18899 face = FACE_FROM_ID (it->f, it->face_id);
18900 }
18901 }
18902
18903 /* Get font to use. Encode IT->char_to_display. */
18904 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18905 &char2b, it->multibyte_p, 0);
18906 font = face->font;
18907
18908 /* When no suitable font found, use the default font. */
18909 font_not_found_p = font == NULL;
18910 if (font_not_found_p)
18911 {
18912 font = FRAME_FONT (it->f);
18913 boff = FRAME_BASELINE_OFFSET (it->f);
18914 font_info = NULL;
18915 }
18916 else
18917 {
18918 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18919 boff = font_info->baseline_offset;
18920 if (font_info->vertical_centering)
18921 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18922 }
18923
18924 if (it->char_to_display >= ' '
18925 && (!it->multibyte_p || it->char_to_display < 128))
18926 {
18927 /* Either unibyte or ASCII. */
18928 int stretched_p;
18929
18930 it->nglyphs = 1;
18931
18932 pcm = rif->per_char_metric (font, &char2b,
18933 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18934
18935 if (it->override_ascent >= 0)
18936 {
18937 it->ascent = it->override_ascent;
18938 it->descent = it->override_descent;
18939 boff = it->override_boff;
18940 }
18941 else
18942 {
18943 it->ascent = FONT_BASE (font) + boff;
18944 it->descent = FONT_DESCENT (font) - boff;
18945 }
18946
18947 if (pcm)
18948 {
18949 it->phys_ascent = pcm->ascent + boff;
18950 it->phys_descent = pcm->descent - boff;
18951 it->pixel_width = pcm->width;
18952 }
18953 else
18954 {
18955 it->glyph_not_available_p = 1;
18956 it->phys_ascent = it->ascent;
18957 it->phys_descent = it->descent;
18958 it->pixel_width = FONT_WIDTH (font);
18959 }
18960
18961 if (it->constrain_row_ascent_descent_p)
18962 {
18963 if (it->descent > it->max_descent)
18964 {
18965 it->ascent += it->descent - it->max_descent;
18966 it->descent = it->max_descent;
18967 }
18968 if (it->ascent > it->max_ascent)
18969 {
18970 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18971 it->ascent = it->max_ascent;
18972 }
18973 it->phys_ascent = min (it->phys_ascent, it->ascent);
18974 it->phys_descent = min (it->phys_descent, it->descent);
18975 extra_line_spacing = 0;
18976 }
18977
18978 /* If this is a space inside a region of text with
18979 `space-width' property, change its width. */
18980 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18981 if (stretched_p)
18982 it->pixel_width *= XFLOATINT (it->space_width);
18983
18984 /* If face has a box, add the box thickness to the character
18985 height. If character has a box line to the left and/or
18986 right, add the box line width to the character's width. */
18987 if (face->box != FACE_NO_BOX)
18988 {
18989 int thick = face->box_line_width;
18990
18991 if (thick > 0)
18992 {
18993 it->ascent += thick;
18994 it->descent += thick;
18995 }
18996 else
18997 thick = -thick;
18998
18999 if (it->start_of_box_run_p)
19000 it->pixel_width += thick;
19001 if (it->end_of_box_run_p)
19002 it->pixel_width += thick;
19003 }
19004
19005 /* If face has an overline, add the height of the overline
19006 (1 pixel) and a 1 pixel margin to the character height. */
19007 if (face->overline_p)
19008 it->ascent += 2;
19009
19010 if (it->constrain_row_ascent_descent_p)
19011 {
19012 if (it->ascent > it->max_ascent)
19013 it->ascent = it->max_ascent;
19014 if (it->descent > it->max_descent)
19015 it->descent = it->max_descent;
19016 }
19017
19018 take_vertical_position_into_account (it);
19019
19020 /* If we have to actually produce glyphs, do it. */
19021 if (it->glyph_row)
19022 {
19023 if (stretched_p)
19024 {
19025 /* Translate a space with a `space-width' property
19026 into a stretch glyph. */
19027 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19028 / FONT_HEIGHT (font));
19029 append_stretch_glyph (it, it->object, it->pixel_width,
19030 it->ascent + it->descent, ascent);
19031 }
19032 else
19033 append_glyph (it);
19034
19035 /* If characters with lbearing or rbearing are displayed
19036 in this line, record that fact in a flag of the
19037 glyph row. This is used to optimize X output code. */
19038 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19039 it->glyph_row->contains_overlapping_glyphs_p = 1;
19040 }
19041 }
19042 else if (it->char_to_display == '\n')
19043 {
19044 /* A newline has no width but we need the height of the line.
19045 But if previous part of the line set a height, don't
19046 increase that height */
19047
19048 Lisp_Object height;
19049
19050 it->override_ascent = -1;
19051 it->pixel_width = 0;
19052 it->nglyphs = 0;
19053
19054 height = calc_line_height_property(it, Qline_height, font, boff, 0);
19055
19056 if (it->override_ascent >= 0)
19057 {
19058 it->ascent = it->override_ascent;
19059 it->descent = it->override_descent;
19060 boff = it->override_boff;
19061 }
19062 else
19063 {
19064 it->ascent = FONT_BASE (font) + boff;
19065 it->descent = FONT_DESCENT (font) - boff;
19066 }
19067
19068 if (EQ (height, make_number(0)))
19069 {
19070 if (it->descent > it->max_descent)
19071 {
19072 it->ascent += it->descent - it->max_descent;
19073 it->descent = it->max_descent;
19074 }
19075 if (it->ascent > it->max_ascent)
19076 {
19077 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19078 it->ascent = it->max_ascent;
19079 }
19080 it->phys_ascent = min (it->phys_ascent, it->ascent);
19081 it->phys_descent = min (it->phys_descent, it->descent);
19082 it->constrain_row_ascent_descent_p = 1;
19083 extra_line_spacing = 0;
19084 }
19085 else
19086 {
19087 Lisp_Object spacing;
19088 int total = 0;
19089
19090 it->phys_ascent = it->ascent;
19091 it->phys_descent = it->descent;
19092
19093 if ((it->max_ascent > 0 || it->max_descent > 0)
19094 && face->box != FACE_NO_BOX
19095 && face->box_line_width > 0)
19096 {
19097 it->ascent += face->box_line_width;
19098 it->descent += face->box_line_width;
19099 }
19100 if (!NILP (height)
19101 && XINT (height) > it->ascent + it->descent)
19102 it->ascent = XINT (height) - it->descent;
19103
19104 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19105 if (INTEGERP (spacing))
19106 {
19107 extra_line_spacing = XINT (spacing);
19108 if (total)
19109 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19110 }
19111 }
19112 }
19113 else if (it->char_to_display == '\t')
19114 {
19115 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19116 int x = it->current_x + it->continuation_lines_width;
19117 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19118
19119 /* If the distance from the current position to the next tab
19120 stop is less than a canonical character width, use the
19121 tab stop after that. */
19122 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19123 next_tab_x += tab_width;
19124
19125 it->pixel_width = next_tab_x - x;
19126 it->nglyphs = 1;
19127 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19128 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19129
19130 if (it->glyph_row)
19131 {
19132 append_stretch_glyph (it, it->object, it->pixel_width,
19133 it->ascent + it->descent, it->ascent);
19134 }
19135 }
19136 else
19137 {
19138 /* A multi-byte character. Assume that the display width of the
19139 character is the width of the character multiplied by the
19140 width of the font. */
19141
19142 /* If we found a font, this font should give us the right
19143 metrics. If we didn't find a font, use the frame's
19144 default font and calculate the width of the character
19145 from the charset width; this is what old redisplay code
19146 did. */
19147
19148 pcm = rif->per_char_metric (font, &char2b,
19149 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19150
19151 if (font_not_found_p || !pcm)
19152 {
19153 int charset = CHAR_CHARSET (it->char_to_display);
19154
19155 it->glyph_not_available_p = 1;
19156 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19157 * CHARSET_WIDTH (charset));
19158 it->phys_ascent = FONT_BASE (font) + boff;
19159 it->phys_descent = FONT_DESCENT (font) - boff;
19160 }
19161 else
19162 {
19163 it->pixel_width = pcm->width;
19164 it->phys_ascent = pcm->ascent + boff;
19165 it->phys_descent = pcm->descent - boff;
19166 if (it->glyph_row
19167 && (pcm->lbearing < 0
19168 || pcm->rbearing > pcm->width))
19169 it->glyph_row->contains_overlapping_glyphs_p = 1;
19170 }
19171 it->nglyphs = 1;
19172 it->ascent = FONT_BASE (font) + boff;
19173 it->descent = FONT_DESCENT (font) - boff;
19174 if (face->box != FACE_NO_BOX)
19175 {
19176 int thick = face->box_line_width;
19177
19178 if (thick > 0)
19179 {
19180 it->ascent += thick;
19181 it->descent += thick;
19182 }
19183 else
19184 thick = - thick;
19185
19186 if (it->start_of_box_run_p)
19187 it->pixel_width += thick;
19188 if (it->end_of_box_run_p)
19189 it->pixel_width += thick;
19190 }
19191
19192 /* If face has an overline, add the height of the overline
19193 (1 pixel) and a 1 pixel margin to the character height. */
19194 if (face->overline_p)
19195 it->ascent += 2;
19196
19197 take_vertical_position_into_account (it);
19198
19199 if (it->glyph_row)
19200 append_glyph (it);
19201 }
19202 it->multibyte_p = saved_multibyte_p;
19203 }
19204 else if (it->what == IT_COMPOSITION)
19205 {
19206 /* Note: A composition is represented as one glyph in the
19207 glyph matrix. There are no padding glyphs. */
19208 XChar2b char2b;
19209 XFontStruct *font;
19210 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19211 XCharStruct *pcm;
19212 int font_not_found_p;
19213 struct font_info *font_info;
19214 int boff; /* baseline offset */
19215 struct composition *cmp = composition_table[it->cmp_id];
19216
19217 /* Maybe translate single-byte characters to multibyte. */
19218 it->char_to_display = it->c;
19219 if (unibyte_display_via_language_environment
19220 && SINGLE_BYTE_CHAR_P (it->c)
19221 && (it->c >= 0240
19222 || (it->c >= 0200
19223 && !NILP (Vnonascii_translation_table))))
19224 {
19225 it->char_to_display = unibyte_char_to_multibyte (it->c);
19226 }
19227
19228 /* Get face and font to use. Encode IT->char_to_display. */
19229 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19230 face = FACE_FROM_ID (it->f, it->face_id);
19231 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19232 &char2b, it->multibyte_p, 0);
19233 font = face->font;
19234
19235 /* When no suitable font found, use the default font. */
19236 font_not_found_p = font == NULL;
19237 if (font_not_found_p)
19238 {
19239 font = FRAME_FONT (it->f);
19240 boff = FRAME_BASELINE_OFFSET (it->f);
19241 font_info = NULL;
19242 }
19243 else
19244 {
19245 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19246 boff = font_info->baseline_offset;
19247 if (font_info->vertical_centering)
19248 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19249 }
19250
19251 /* There are no padding glyphs, so there is only one glyph to
19252 produce for the composition. Important is that pixel_width,
19253 ascent and descent are the values of what is drawn by
19254 draw_glyphs (i.e. the values of the overall glyphs composed). */
19255 it->nglyphs = 1;
19256
19257 /* If we have not yet calculated pixel size data of glyphs of
19258 the composition for the current face font, calculate them
19259 now. Theoretically, we have to check all fonts for the
19260 glyphs, but that requires much time and memory space. So,
19261 here we check only the font of the first glyph. This leads
19262 to incorrect display very rarely, and C-l (recenter) can
19263 correct the display anyway. */
19264 if (cmp->font != (void *) font)
19265 {
19266 /* Ascent and descent of the font of the first character of
19267 this composition (adjusted by baseline offset). Ascent
19268 and descent of overall glyphs should not be less than
19269 them respectively. */
19270 int font_ascent = FONT_BASE (font) + boff;
19271 int font_descent = FONT_DESCENT (font) - boff;
19272 /* Bounding box of the overall glyphs. */
19273 int leftmost, rightmost, lowest, highest;
19274 int i, width, ascent, descent;
19275
19276 cmp->font = (void *) font;
19277
19278 /* Initialize the bounding box. */
19279 if (font_info
19280 && (pcm = rif->per_char_metric (font, &char2b,
19281 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19282 {
19283 width = pcm->width;
19284 ascent = pcm->ascent;
19285 descent = pcm->descent;
19286 }
19287 else
19288 {
19289 width = FONT_WIDTH (font);
19290 ascent = FONT_BASE (font);
19291 descent = FONT_DESCENT (font);
19292 }
19293
19294 rightmost = width;
19295 lowest = - descent + boff;
19296 highest = ascent + boff;
19297 leftmost = 0;
19298
19299 if (font_info
19300 && font_info->default_ascent
19301 && CHAR_TABLE_P (Vuse_default_ascent)
19302 && !NILP (Faref (Vuse_default_ascent,
19303 make_number (it->char_to_display))))
19304 highest = font_info->default_ascent + boff;
19305
19306 /* Draw the first glyph at the normal position. It may be
19307 shifted to right later if some other glyphs are drawn at
19308 the left. */
19309 cmp->offsets[0] = 0;
19310 cmp->offsets[1] = boff;
19311
19312 /* Set cmp->offsets for the remaining glyphs. */
19313 for (i = 1; i < cmp->glyph_len; i++)
19314 {
19315 int left, right, btm, top;
19316 int ch = COMPOSITION_GLYPH (cmp, i);
19317 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19318
19319 face = FACE_FROM_ID (it->f, face_id);
19320 get_char_face_and_encoding (it->f, ch, face->id,
19321 &char2b, it->multibyte_p, 0);
19322 font = face->font;
19323 if (font == NULL)
19324 {
19325 font = FRAME_FONT (it->f);
19326 boff = FRAME_BASELINE_OFFSET (it->f);
19327 font_info = NULL;
19328 }
19329 else
19330 {
19331 font_info
19332 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19333 boff = font_info->baseline_offset;
19334 if (font_info->vertical_centering)
19335 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19336 }
19337
19338 if (font_info
19339 && (pcm = rif->per_char_metric (font, &char2b,
19340 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19341 {
19342 width = pcm->width;
19343 ascent = pcm->ascent;
19344 descent = pcm->descent;
19345 }
19346 else
19347 {
19348 width = FONT_WIDTH (font);
19349 ascent = 1;
19350 descent = 0;
19351 }
19352
19353 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19354 {
19355 /* Relative composition with or without
19356 alternate chars. */
19357 left = (leftmost + rightmost - width) / 2;
19358 btm = - descent + boff;
19359 if (font_info && font_info->relative_compose
19360 && (! CHAR_TABLE_P (Vignore_relative_composition)
19361 || NILP (Faref (Vignore_relative_composition,
19362 make_number (ch)))))
19363 {
19364
19365 if (- descent >= font_info->relative_compose)
19366 /* One extra pixel between two glyphs. */
19367 btm = highest + 1;
19368 else if (ascent <= 0)
19369 /* One extra pixel between two glyphs. */
19370 btm = lowest - 1 - ascent - descent;
19371 }
19372 }
19373 else
19374 {
19375 /* A composition rule is specified by an integer
19376 value that encodes global and new reference
19377 points (GREF and NREF). GREF and NREF are
19378 specified by numbers as below:
19379
19380 0---1---2 -- ascent
19381 | |
19382 | |
19383 | |
19384 9--10--11 -- center
19385 | |
19386 ---3---4---5--- baseline
19387 | |
19388 6---7---8 -- descent
19389 */
19390 int rule = COMPOSITION_RULE (cmp, i);
19391 int gref, nref, grefx, grefy, nrefx, nrefy;
19392
19393 COMPOSITION_DECODE_RULE (rule, gref, nref);
19394 grefx = gref % 3, nrefx = nref % 3;
19395 grefy = gref / 3, nrefy = nref / 3;
19396
19397 left = (leftmost
19398 + grefx * (rightmost - leftmost) / 2
19399 - nrefx * width / 2);
19400 btm = ((grefy == 0 ? highest
19401 : grefy == 1 ? 0
19402 : grefy == 2 ? lowest
19403 : (highest + lowest) / 2)
19404 - (nrefy == 0 ? ascent + descent
19405 : nrefy == 1 ? descent - boff
19406 : nrefy == 2 ? 0
19407 : (ascent + descent) / 2));
19408 }
19409
19410 cmp->offsets[i * 2] = left;
19411 cmp->offsets[i * 2 + 1] = btm + descent;
19412
19413 /* Update the bounding box of the overall glyphs. */
19414 right = left + width;
19415 top = btm + descent + ascent;
19416 if (left < leftmost)
19417 leftmost = left;
19418 if (right > rightmost)
19419 rightmost = right;
19420 if (top > highest)
19421 highest = top;
19422 if (btm < lowest)
19423 lowest = btm;
19424 }
19425
19426 /* If there are glyphs whose x-offsets are negative,
19427 shift all glyphs to the right and make all x-offsets
19428 non-negative. */
19429 if (leftmost < 0)
19430 {
19431 for (i = 0; i < cmp->glyph_len; i++)
19432 cmp->offsets[i * 2] -= leftmost;
19433 rightmost -= leftmost;
19434 }
19435
19436 cmp->pixel_width = rightmost;
19437 cmp->ascent = highest;
19438 cmp->descent = - lowest;
19439 if (cmp->ascent < font_ascent)
19440 cmp->ascent = font_ascent;
19441 if (cmp->descent < font_descent)
19442 cmp->descent = font_descent;
19443 }
19444
19445 it->pixel_width = cmp->pixel_width;
19446 it->ascent = it->phys_ascent = cmp->ascent;
19447 it->descent = it->phys_descent = cmp->descent;
19448
19449 if (face->box != FACE_NO_BOX)
19450 {
19451 int thick = face->box_line_width;
19452
19453 if (thick > 0)
19454 {
19455 it->ascent += thick;
19456 it->descent += thick;
19457 }
19458 else
19459 thick = - thick;
19460
19461 if (it->start_of_box_run_p)
19462 it->pixel_width += thick;
19463 if (it->end_of_box_run_p)
19464 it->pixel_width += thick;
19465 }
19466
19467 /* If face has an overline, add the height of the overline
19468 (1 pixel) and a 1 pixel margin to the character height. */
19469 if (face->overline_p)
19470 it->ascent += 2;
19471
19472 take_vertical_position_into_account (it);
19473
19474 if (it->glyph_row)
19475 append_composite_glyph (it);
19476 }
19477 else if (it->what == IT_IMAGE)
19478 produce_image_glyph (it);
19479 else if (it->what == IT_STRETCH)
19480 produce_stretch_glyph (it);
19481
19482 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19483 because this isn't true for images with `:ascent 100'. */
19484 xassert (it->ascent >= 0 && it->descent >= 0);
19485 if (it->area == TEXT_AREA)
19486 it->current_x += it->pixel_width;
19487
19488 if (extra_line_spacing > 0)
19489 {
19490 it->descent += extra_line_spacing;
19491 if (extra_line_spacing > it->max_extra_line_spacing)
19492 it->max_extra_line_spacing = extra_line_spacing;
19493 }
19494
19495 it->max_ascent = max (it->max_ascent, it->ascent);
19496 it->max_descent = max (it->max_descent, it->descent);
19497 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19498 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19499 }
19500
19501 /* EXPORT for RIF:
19502 Output LEN glyphs starting at START at the nominal cursor position.
19503 Advance the nominal cursor over the text. The global variable
19504 updated_window contains the window being updated, updated_row is
19505 the glyph row being updated, and updated_area is the area of that
19506 row being updated. */
19507
19508 void
19509 x_write_glyphs (start, len)
19510 struct glyph *start;
19511 int len;
19512 {
19513 int x, hpos;
19514
19515 xassert (updated_window && updated_row);
19516 BLOCK_INPUT;
19517
19518 /* Write glyphs. */
19519
19520 hpos = start - updated_row->glyphs[updated_area];
19521 x = draw_glyphs (updated_window, output_cursor.x,
19522 updated_row, updated_area,
19523 hpos, hpos + len,
19524 DRAW_NORMAL_TEXT, 0);
19525
19526 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19527 if (updated_area == TEXT_AREA
19528 && updated_window->phys_cursor_on_p
19529 && updated_window->phys_cursor.vpos == output_cursor.vpos
19530 && updated_window->phys_cursor.hpos >= hpos
19531 && updated_window->phys_cursor.hpos < hpos + len)
19532 updated_window->phys_cursor_on_p = 0;
19533
19534 UNBLOCK_INPUT;
19535
19536 /* Advance the output cursor. */
19537 output_cursor.hpos += len;
19538 output_cursor.x = x;
19539 }
19540
19541
19542 /* EXPORT for RIF:
19543 Insert LEN glyphs from START at the nominal cursor position. */
19544
19545 void
19546 x_insert_glyphs (start, len)
19547 struct glyph *start;
19548 int len;
19549 {
19550 struct frame *f;
19551 struct window *w;
19552 int line_height, shift_by_width, shifted_region_width;
19553 struct glyph_row *row;
19554 struct glyph *glyph;
19555 int frame_x, frame_y, hpos;
19556
19557 xassert (updated_window && updated_row);
19558 BLOCK_INPUT;
19559 w = updated_window;
19560 f = XFRAME (WINDOW_FRAME (w));
19561
19562 /* Get the height of the line we are in. */
19563 row = updated_row;
19564 line_height = row->height;
19565
19566 /* Get the width of the glyphs to insert. */
19567 shift_by_width = 0;
19568 for (glyph = start; glyph < start + len; ++glyph)
19569 shift_by_width += glyph->pixel_width;
19570
19571 /* Get the width of the region to shift right. */
19572 shifted_region_width = (window_box_width (w, updated_area)
19573 - output_cursor.x
19574 - shift_by_width);
19575
19576 /* Shift right. */
19577 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19578 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19579
19580 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19581 line_height, shift_by_width);
19582
19583 /* Write the glyphs. */
19584 hpos = start - row->glyphs[updated_area];
19585 draw_glyphs (w, output_cursor.x, row, updated_area,
19586 hpos, hpos + len,
19587 DRAW_NORMAL_TEXT, 0);
19588
19589 /* Advance the output cursor. */
19590 output_cursor.hpos += len;
19591 output_cursor.x += shift_by_width;
19592 UNBLOCK_INPUT;
19593 }
19594
19595
19596 /* EXPORT for RIF:
19597 Erase the current text line from the nominal cursor position
19598 (inclusive) to pixel column TO_X (exclusive). The idea is that
19599 everything from TO_X onward is already erased.
19600
19601 TO_X is a pixel position relative to updated_area of
19602 updated_window. TO_X == -1 means clear to the end of this area. */
19603
19604 void
19605 x_clear_end_of_line (to_x)
19606 int to_x;
19607 {
19608 struct frame *f;
19609 struct window *w = updated_window;
19610 int max_x, min_y, max_y;
19611 int from_x, from_y, to_y;
19612
19613 xassert (updated_window && updated_row);
19614 f = XFRAME (w->frame);
19615
19616 if (updated_row->full_width_p)
19617 max_x = WINDOW_TOTAL_WIDTH (w);
19618 else
19619 max_x = window_box_width (w, updated_area);
19620 max_y = window_text_bottom_y (w);
19621
19622 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19623 of window. For TO_X > 0, truncate to end of drawing area. */
19624 if (to_x == 0)
19625 return;
19626 else if (to_x < 0)
19627 to_x = max_x;
19628 else
19629 to_x = min (to_x, max_x);
19630
19631 to_y = min (max_y, output_cursor.y + updated_row->height);
19632
19633 /* Notice if the cursor will be cleared by this operation. */
19634 if (!updated_row->full_width_p)
19635 notice_overwritten_cursor (w, updated_area,
19636 output_cursor.x, -1,
19637 updated_row->y,
19638 MATRIX_ROW_BOTTOM_Y (updated_row));
19639
19640 from_x = output_cursor.x;
19641
19642 /* Translate to frame coordinates. */
19643 if (updated_row->full_width_p)
19644 {
19645 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19646 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19647 }
19648 else
19649 {
19650 int area_left = window_box_left (w, updated_area);
19651 from_x += area_left;
19652 to_x += area_left;
19653 }
19654
19655 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19656 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19657 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19658
19659 /* Prevent inadvertently clearing to end of the X window. */
19660 if (to_x > from_x && to_y > from_y)
19661 {
19662 BLOCK_INPUT;
19663 rif->clear_frame_area (f, from_x, from_y,
19664 to_x - from_x, to_y - from_y);
19665 UNBLOCK_INPUT;
19666 }
19667 }
19668
19669 #endif /* HAVE_WINDOW_SYSTEM */
19670
19671
19672 \f
19673 /***********************************************************************
19674 Cursor types
19675 ***********************************************************************/
19676
19677 /* Value is the internal representation of the specified cursor type
19678 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19679 of the bar cursor. */
19680
19681 static enum text_cursor_kinds
19682 get_specified_cursor_type (arg, width)
19683 Lisp_Object arg;
19684 int *width;
19685 {
19686 enum text_cursor_kinds type;
19687
19688 if (NILP (arg))
19689 return NO_CURSOR;
19690
19691 if (EQ (arg, Qbox))
19692 return FILLED_BOX_CURSOR;
19693
19694 if (EQ (arg, Qhollow))
19695 return HOLLOW_BOX_CURSOR;
19696
19697 if (EQ (arg, Qbar))
19698 {
19699 *width = 2;
19700 return BAR_CURSOR;
19701 }
19702
19703 if (CONSP (arg)
19704 && EQ (XCAR (arg), Qbar)
19705 && INTEGERP (XCDR (arg))
19706 && XINT (XCDR (arg)) >= 0)
19707 {
19708 *width = XINT (XCDR (arg));
19709 return BAR_CURSOR;
19710 }
19711
19712 if (EQ (arg, Qhbar))
19713 {
19714 *width = 2;
19715 return HBAR_CURSOR;
19716 }
19717
19718 if (CONSP (arg)
19719 && EQ (XCAR (arg), Qhbar)
19720 && INTEGERP (XCDR (arg))
19721 && XINT (XCDR (arg)) >= 0)
19722 {
19723 *width = XINT (XCDR (arg));
19724 return HBAR_CURSOR;
19725 }
19726
19727 /* Treat anything unknown as "hollow box cursor".
19728 It was bad to signal an error; people have trouble fixing
19729 .Xdefaults with Emacs, when it has something bad in it. */
19730 type = HOLLOW_BOX_CURSOR;
19731
19732 return type;
19733 }
19734
19735 /* Set the default cursor types for specified frame. */
19736 void
19737 set_frame_cursor_types (f, arg)
19738 struct frame *f;
19739 Lisp_Object arg;
19740 {
19741 int width;
19742 Lisp_Object tem;
19743
19744 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19745 FRAME_CURSOR_WIDTH (f) = width;
19746
19747 /* By default, set up the blink-off state depending on the on-state. */
19748
19749 tem = Fassoc (arg, Vblink_cursor_alist);
19750 if (!NILP (tem))
19751 {
19752 FRAME_BLINK_OFF_CURSOR (f)
19753 = get_specified_cursor_type (XCDR (tem), &width);
19754 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19755 }
19756 else
19757 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19758 }
19759
19760
19761 /* Return the cursor we want to be displayed in window W. Return
19762 width of bar/hbar cursor through WIDTH arg. Return with
19763 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19764 (i.e. if the `system caret' should track this cursor).
19765
19766 In a mini-buffer window, we want the cursor only to appear if we
19767 are reading input from this window. For the selected window, we
19768 want the cursor type given by the frame parameter or buffer local
19769 setting of cursor-type. If explicitly marked off, draw no cursor.
19770 In all other cases, we want a hollow box cursor. */
19771
19772 static enum text_cursor_kinds
19773 get_window_cursor_type (w, glyph, width, active_cursor)
19774 struct window *w;
19775 struct glyph *glyph;
19776 int *width;
19777 int *active_cursor;
19778 {
19779 struct frame *f = XFRAME (w->frame);
19780 struct buffer *b = XBUFFER (w->buffer);
19781 int cursor_type = DEFAULT_CURSOR;
19782 Lisp_Object alt_cursor;
19783 int non_selected = 0;
19784
19785 *active_cursor = 1;
19786
19787 /* Echo area */
19788 if (cursor_in_echo_area
19789 && FRAME_HAS_MINIBUF_P (f)
19790 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19791 {
19792 if (w == XWINDOW (echo_area_window))
19793 {
19794 *width = FRAME_CURSOR_WIDTH (f);
19795 return FRAME_DESIRED_CURSOR (f);
19796 }
19797
19798 *active_cursor = 0;
19799 non_selected = 1;
19800 }
19801
19802 /* Nonselected window or nonselected frame. */
19803 else if (w != XWINDOW (f->selected_window)
19804 #ifdef HAVE_WINDOW_SYSTEM
19805 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19806 #endif
19807 )
19808 {
19809 *active_cursor = 0;
19810
19811 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19812 return NO_CURSOR;
19813
19814 non_selected = 1;
19815 }
19816
19817 /* Never display a cursor in a window in which cursor-type is nil. */
19818 if (NILP (b->cursor_type))
19819 return NO_CURSOR;
19820
19821 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19822 if (non_selected)
19823 {
19824 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19825 return get_specified_cursor_type (alt_cursor, width);
19826 }
19827
19828 /* Get the normal cursor type for this window. */
19829 if (EQ (b->cursor_type, Qt))
19830 {
19831 cursor_type = FRAME_DESIRED_CURSOR (f);
19832 *width = FRAME_CURSOR_WIDTH (f);
19833 }
19834 else
19835 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19836
19837 /* Use normal cursor if not blinked off. */
19838 if (!w->cursor_off_p)
19839 {
19840 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19841 if (cursor_type == FILLED_BOX_CURSOR)
19842 cursor_type = HOLLOW_BOX_CURSOR;
19843 }
19844 return cursor_type;
19845 }
19846
19847 /* Cursor is blinked off, so determine how to "toggle" it. */
19848
19849 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19850 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19851 return get_specified_cursor_type (XCDR (alt_cursor), width);
19852
19853 /* Then see if frame has specified a specific blink off cursor type. */
19854 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19855 {
19856 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19857 return FRAME_BLINK_OFF_CURSOR (f);
19858 }
19859
19860 #if 0
19861 /* Some people liked having a permanently visible blinking cursor,
19862 while others had very strong opinions against it. So it was
19863 decided to remove it. KFS 2003-09-03 */
19864
19865 /* Finally perform built-in cursor blinking:
19866 filled box <-> hollow box
19867 wide [h]bar <-> narrow [h]bar
19868 narrow [h]bar <-> no cursor
19869 other type <-> no cursor */
19870
19871 if (cursor_type == FILLED_BOX_CURSOR)
19872 return HOLLOW_BOX_CURSOR;
19873
19874 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19875 {
19876 *width = 1;
19877 return cursor_type;
19878 }
19879 #endif
19880
19881 return NO_CURSOR;
19882 }
19883
19884
19885 #ifdef HAVE_WINDOW_SYSTEM
19886
19887 /* Notice when the text cursor of window W has been completely
19888 overwritten by a drawing operation that outputs glyphs in AREA
19889 starting at X0 and ending at X1 in the line starting at Y0 and
19890 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19891 the rest of the line after X0 has been written. Y coordinates
19892 are window-relative. */
19893
19894 static void
19895 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19896 struct window *w;
19897 enum glyph_row_area area;
19898 int x0, y0, x1, y1;
19899 {
19900 int cx0, cx1, cy0, cy1;
19901 struct glyph_row *row;
19902
19903 if (!w->phys_cursor_on_p)
19904 return;
19905 if (area != TEXT_AREA)
19906 return;
19907
19908 row = w->current_matrix->rows + w->phys_cursor.vpos;
19909 if (!row->displays_text_p)
19910 return;
19911
19912 if (row->cursor_in_fringe_p)
19913 {
19914 row->cursor_in_fringe_p = 0;
19915 draw_fringe_bitmap (w, row, 0);
19916 w->phys_cursor_on_p = 0;
19917 return;
19918 }
19919
19920 cx0 = w->phys_cursor.x;
19921 cx1 = cx0 + w->phys_cursor_width;
19922 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19923 return;
19924
19925 /* The cursor image will be completely removed from the
19926 screen if the output area intersects the cursor area in
19927 y-direction. When we draw in [y0 y1[, and some part of
19928 the cursor is at y < y0, that part must have been drawn
19929 before. When scrolling, the cursor is erased before
19930 actually scrolling, so we don't come here. When not
19931 scrolling, the rows above the old cursor row must have
19932 changed, and in this case these rows must have written
19933 over the cursor image.
19934
19935 Likewise if part of the cursor is below y1, with the
19936 exception of the cursor being in the first blank row at
19937 the buffer and window end because update_text_area
19938 doesn't draw that row. (Except when it does, but
19939 that's handled in update_text_area.) */
19940
19941 cy0 = w->phys_cursor.y;
19942 cy1 = cy0 + w->phys_cursor_height;
19943 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19944 return;
19945
19946 w->phys_cursor_on_p = 0;
19947 }
19948
19949 #endif /* HAVE_WINDOW_SYSTEM */
19950
19951 \f
19952 /************************************************************************
19953 Mouse Face
19954 ************************************************************************/
19955
19956 #ifdef HAVE_WINDOW_SYSTEM
19957
19958 /* EXPORT for RIF:
19959 Fix the display of area AREA of overlapping row ROW in window W. */
19960
19961 void
19962 x_fix_overlapping_area (w, row, area)
19963 struct window *w;
19964 struct glyph_row *row;
19965 enum glyph_row_area area;
19966 {
19967 int i, x;
19968
19969 BLOCK_INPUT;
19970
19971 x = 0;
19972 for (i = 0; i < row->used[area];)
19973 {
19974 if (row->glyphs[area][i].overlaps_vertically_p)
19975 {
19976 int start = i, start_x = x;
19977
19978 do
19979 {
19980 x += row->glyphs[area][i].pixel_width;
19981 ++i;
19982 }
19983 while (i < row->used[area]
19984 && row->glyphs[area][i].overlaps_vertically_p);
19985
19986 draw_glyphs (w, start_x, row, area,
19987 start, i,
19988 DRAW_NORMAL_TEXT, 1);
19989 }
19990 else
19991 {
19992 x += row->glyphs[area][i].pixel_width;
19993 ++i;
19994 }
19995 }
19996
19997 UNBLOCK_INPUT;
19998 }
19999
20000
20001 /* EXPORT:
20002 Draw the cursor glyph of window W in glyph row ROW. See the
20003 comment of draw_glyphs for the meaning of HL. */
20004
20005 void
20006 draw_phys_cursor_glyph (w, row, hl)
20007 struct window *w;
20008 struct glyph_row *row;
20009 enum draw_glyphs_face hl;
20010 {
20011 /* If cursor hpos is out of bounds, don't draw garbage. This can
20012 happen in mini-buffer windows when switching between echo area
20013 glyphs and mini-buffer. */
20014 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20015 {
20016 int on_p = w->phys_cursor_on_p;
20017 int x1;
20018 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20019 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20020 hl, 0);
20021 w->phys_cursor_on_p = on_p;
20022
20023 if (hl == DRAW_CURSOR)
20024 w->phys_cursor_width = x1 - w->phys_cursor.x;
20025 /* When we erase the cursor, and ROW is overlapped by other
20026 rows, make sure that these overlapping parts of other rows
20027 are redrawn. */
20028 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20029 {
20030 if (row > w->current_matrix->rows
20031 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20032 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20033
20034 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20035 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20036 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20037 }
20038 }
20039 }
20040
20041
20042 /* EXPORT:
20043 Erase the image of a cursor of window W from the screen. */
20044
20045 void
20046 erase_phys_cursor (w)
20047 struct window *w;
20048 {
20049 struct frame *f = XFRAME (w->frame);
20050 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20051 int hpos = w->phys_cursor.hpos;
20052 int vpos = w->phys_cursor.vpos;
20053 int mouse_face_here_p = 0;
20054 struct glyph_matrix *active_glyphs = w->current_matrix;
20055 struct glyph_row *cursor_row;
20056 struct glyph *cursor_glyph;
20057 enum draw_glyphs_face hl;
20058
20059 /* No cursor displayed or row invalidated => nothing to do on the
20060 screen. */
20061 if (w->phys_cursor_type == NO_CURSOR)
20062 goto mark_cursor_off;
20063
20064 /* VPOS >= active_glyphs->nrows means that window has been resized.
20065 Don't bother to erase the cursor. */
20066 if (vpos >= active_glyphs->nrows)
20067 goto mark_cursor_off;
20068
20069 /* If row containing cursor is marked invalid, there is nothing we
20070 can do. */
20071 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20072 if (!cursor_row->enabled_p)
20073 goto mark_cursor_off;
20074
20075 /* If line spacing is > 0, old cursor may only be partially visible in
20076 window after split-window. So adjust visible height. */
20077 cursor_row->visible_height = min (cursor_row->visible_height,
20078 window_text_bottom_y (w) - cursor_row->y);
20079
20080 /* If row is completely invisible, don't attempt to delete a cursor which
20081 isn't there. This can happen if cursor is at top of a window, and
20082 we switch to a buffer with a header line in that window. */
20083 if (cursor_row->visible_height <= 0)
20084 goto mark_cursor_off;
20085
20086 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20087 if (cursor_row->cursor_in_fringe_p)
20088 {
20089 cursor_row->cursor_in_fringe_p = 0;
20090 draw_fringe_bitmap (w, cursor_row, 0);
20091 goto mark_cursor_off;
20092 }
20093
20094 /* This can happen when the new row is shorter than the old one.
20095 In this case, either draw_glyphs or clear_end_of_line
20096 should have cleared the cursor. Note that we wouldn't be
20097 able to erase the cursor in this case because we don't have a
20098 cursor glyph at hand. */
20099 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20100 goto mark_cursor_off;
20101
20102 /* If the cursor is in the mouse face area, redisplay that when
20103 we clear the cursor. */
20104 if (! NILP (dpyinfo->mouse_face_window)
20105 && w == XWINDOW (dpyinfo->mouse_face_window)
20106 && (vpos > dpyinfo->mouse_face_beg_row
20107 || (vpos == dpyinfo->mouse_face_beg_row
20108 && hpos >= dpyinfo->mouse_face_beg_col))
20109 && (vpos < dpyinfo->mouse_face_end_row
20110 || (vpos == dpyinfo->mouse_face_end_row
20111 && hpos < dpyinfo->mouse_face_end_col))
20112 /* Don't redraw the cursor's spot in mouse face if it is at the
20113 end of a line (on a newline). The cursor appears there, but
20114 mouse highlighting does not. */
20115 && cursor_row->used[TEXT_AREA] > hpos)
20116 mouse_face_here_p = 1;
20117
20118 /* Maybe clear the display under the cursor. */
20119 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20120 {
20121 int x, y;
20122 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20123 int width;
20124
20125 cursor_glyph = get_phys_cursor_glyph (w);
20126 if (cursor_glyph == NULL)
20127 goto mark_cursor_off;
20128
20129 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20130 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20131 width = min (cursor_glyph->pixel_width,
20132 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20133
20134 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20135 }
20136
20137 /* Erase the cursor by redrawing the character underneath it. */
20138 if (mouse_face_here_p)
20139 hl = DRAW_MOUSE_FACE;
20140 else
20141 hl = DRAW_NORMAL_TEXT;
20142 draw_phys_cursor_glyph (w, cursor_row, hl);
20143
20144 mark_cursor_off:
20145 w->phys_cursor_on_p = 0;
20146 w->phys_cursor_type = NO_CURSOR;
20147 }
20148
20149
20150 /* EXPORT:
20151 Display or clear cursor of window W. If ON is zero, clear the
20152 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20153 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20154
20155 void
20156 display_and_set_cursor (w, on, hpos, vpos, x, y)
20157 struct window *w;
20158 int on, hpos, vpos, x, y;
20159 {
20160 struct frame *f = XFRAME (w->frame);
20161 int new_cursor_type;
20162 int new_cursor_width;
20163 int active_cursor;
20164 struct glyph_row *glyph_row;
20165 struct glyph *glyph;
20166
20167 /* This is pointless on invisible frames, and dangerous on garbaged
20168 windows and frames; in the latter case, the frame or window may
20169 be in the midst of changing its size, and x and y may be off the
20170 window. */
20171 if (! FRAME_VISIBLE_P (f)
20172 || FRAME_GARBAGED_P (f)
20173 || vpos >= w->current_matrix->nrows
20174 || hpos >= w->current_matrix->matrix_w)
20175 return;
20176
20177 /* If cursor is off and we want it off, return quickly. */
20178 if (!on && !w->phys_cursor_on_p)
20179 return;
20180
20181 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20182 /* If cursor row is not enabled, we don't really know where to
20183 display the cursor. */
20184 if (!glyph_row->enabled_p)
20185 {
20186 w->phys_cursor_on_p = 0;
20187 return;
20188 }
20189
20190 glyph = NULL;
20191 if (!glyph_row->exact_window_width_line_p
20192 || hpos < glyph_row->used[TEXT_AREA])
20193 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20194
20195 xassert (interrupt_input_blocked);
20196
20197 /* Set new_cursor_type to the cursor we want to be displayed. */
20198 new_cursor_type = get_window_cursor_type (w, glyph,
20199 &new_cursor_width, &active_cursor);
20200
20201 /* If cursor is currently being shown and we don't want it to be or
20202 it is in the wrong place, or the cursor type is not what we want,
20203 erase it. */
20204 if (w->phys_cursor_on_p
20205 && (!on
20206 || w->phys_cursor.x != x
20207 || w->phys_cursor.y != y
20208 || new_cursor_type != w->phys_cursor_type
20209 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20210 && new_cursor_width != w->phys_cursor_width)))
20211 erase_phys_cursor (w);
20212
20213 /* Don't check phys_cursor_on_p here because that flag is only set
20214 to zero in some cases where we know that the cursor has been
20215 completely erased, to avoid the extra work of erasing the cursor
20216 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20217 still not be visible, or it has only been partly erased. */
20218 if (on)
20219 {
20220 w->phys_cursor_ascent = glyph_row->ascent;
20221 w->phys_cursor_height = glyph_row->height;
20222
20223 /* Set phys_cursor_.* before x_draw_.* is called because some
20224 of them may need the information. */
20225 w->phys_cursor.x = x;
20226 w->phys_cursor.y = glyph_row->y;
20227 w->phys_cursor.hpos = hpos;
20228 w->phys_cursor.vpos = vpos;
20229 }
20230
20231 rif->draw_window_cursor (w, glyph_row, x, y,
20232 new_cursor_type, new_cursor_width,
20233 on, active_cursor);
20234 }
20235
20236
20237 /* Switch the display of W's cursor on or off, according to the value
20238 of ON. */
20239
20240 static void
20241 update_window_cursor (w, on)
20242 struct window *w;
20243 int on;
20244 {
20245 /* Don't update cursor in windows whose frame is in the process
20246 of being deleted. */
20247 if (w->current_matrix)
20248 {
20249 BLOCK_INPUT;
20250 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20251 w->phys_cursor.x, w->phys_cursor.y);
20252 UNBLOCK_INPUT;
20253 }
20254 }
20255
20256
20257 /* Call update_window_cursor with parameter ON_P on all leaf windows
20258 in the window tree rooted at W. */
20259
20260 static void
20261 update_cursor_in_window_tree (w, on_p)
20262 struct window *w;
20263 int on_p;
20264 {
20265 while (w)
20266 {
20267 if (!NILP (w->hchild))
20268 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20269 else if (!NILP (w->vchild))
20270 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20271 else
20272 update_window_cursor (w, on_p);
20273
20274 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20275 }
20276 }
20277
20278
20279 /* EXPORT:
20280 Display the cursor on window W, or clear it, according to ON_P.
20281 Don't change the cursor's position. */
20282
20283 void
20284 x_update_cursor (f, on_p)
20285 struct frame *f;
20286 int on_p;
20287 {
20288 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20289 }
20290
20291
20292 /* EXPORT:
20293 Clear the cursor of window W to background color, and mark the
20294 cursor as not shown. This is used when the text where the cursor
20295 is is about to be rewritten. */
20296
20297 void
20298 x_clear_cursor (w)
20299 struct window *w;
20300 {
20301 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20302 update_window_cursor (w, 0);
20303 }
20304
20305
20306 /* EXPORT:
20307 Display the active region described by mouse_face_* according to DRAW. */
20308
20309 void
20310 show_mouse_face (dpyinfo, draw)
20311 Display_Info *dpyinfo;
20312 enum draw_glyphs_face draw;
20313 {
20314 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20315 struct frame *f = XFRAME (WINDOW_FRAME (w));
20316
20317 if (/* If window is in the process of being destroyed, don't bother
20318 to do anything. */
20319 w->current_matrix != NULL
20320 /* Don't update mouse highlight if hidden */
20321 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20322 /* Recognize when we are called to operate on rows that don't exist
20323 anymore. This can happen when a window is split. */
20324 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20325 {
20326 int phys_cursor_on_p = w->phys_cursor_on_p;
20327 struct glyph_row *row, *first, *last;
20328
20329 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20330 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20331
20332 for (row = first; row <= last && row->enabled_p; ++row)
20333 {
20334 int start_hpos, end_hpos, start_x;
20335
20336 /* For all but the first row, the highlight starts at column 0. */
20337 if (row == first)
20338 {
20339 start_hpos = dpyinfo->mouse_face_beg_col;
20340 start_x = dpyinfo->mouse_face_beg_x;
20341 }
20342 else
20343 {
20344 start_hpos = 0;
20345 start_x = 0;
20346 }
20347
20348 if (row == last)
20349 end_hpos = dpyinfo->mouse_face_end_col;
20350 else
20351 end_hpos = row->used[TEXT_AREA];
20352
20353 if (end_hpos > start_hpos)
20354 {
20355 draw_glyphs (w, start_x, row, TEXT_AREA,
20356 start_hpos, end_hpos,
20357 draw, 0);
20358
20359 row->mouse_face_p
20360 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20361 }
20362 }
20363
20364 /* When we've written over the cursor, arrange for it to
20365 be displayed again. */
20366 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20367 {
20368 BLOCK_INPUT;
20369 display_and_set_cursor (w, 1,
20370 w->phys_cursor.hpos, w->phys_cursor.vpos,
20371 w->phys_cursor.x, w->phys_cursor.y);
20372 UNBLOCK_INPUT;
20373 }
20374 }
20375
20376 /* Change the mouse cursor. */
20377 if (draw == DRAW_NORMAL_TEXT)
20378 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20379 else if (draw == DRAW_MOUSE_FACE)
20380 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20381 else
20382 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20383 }
20384
20385 /* EXPORT:
20386 Clear out the mouse-highlighted active region.
20387 Redraw it un-highlighted first. Value is non-zero if mouse
20388 face was actually drawn unhighlighted. */
20389
20390 int
20391 clear_mouse_face (dpyinfo)
20392 Display_Info *dpyinfo;
20393 {
20394 int cleared = 0;
20395
20396 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20397 {
20398 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20399 cleared = 1;
20400 }
20401
20402 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20403 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20404 dpyinfo->mouse_face_window = Qnil;
20405 dpyinfo->mouse_face_overlay = Qnil;
20406 return cleared;
20407 }
20408
20409
20410 /* EXPORT:
20411 Non-zero if physical cursor of window W is within mouse face. */
20412
20413 int
20414 cursor_in_mouse_face_p (w)
20415 struct window *w;
20416 {
20417 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20418 int in_mouse_face = 0;
20419
20420 if (WINDOWP (dpyinfo->mouse_face_window)
20421 && XWINDOW (dpyinfo->mouse_face_window) == w)
20422 {
20423 int hpos = w->phys_cursor.hpos;
20424 int vpos = w->phys_cursor.vpos;
20425
20426 if (vpos >= dpyinfo->mouse_face_beg_row
20427 && vpos <= dpyinfo->mouse_face_end_row
20428 && (vpos > dpyinfo->mouse_face_beg_row
20429 || hpos >= dpyinfo->mouse_face_beg_col)
20430 && (vpos < dpyinfo->mouse_face_end_row
20431 || hpos < dpyinfo->mouse_face_end_col
20432 || dpyinfo->mouse_face_past_end))
20433 in_mouse_face = 1;
20434 }
20435
20436 return in_mouse_face;
20437 }
20438
20439
20440
20441 \f
20442 /* Find the glyph matrix position of buffer position CHARPOS in window
20443 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20444 current glyphs must be up to date. If CHARPOS is above window
20445 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20446 of last line in W. In the row containing CHARPOS, stop before glyphs
20447 having STOP as object. */
20448
20449 #if 1 /* This is a version of fast_find_position that's more correct
20450 in the presence of hscrolling, for example. I didn't install
20451 it right away because the problem fixed is minor, it failed
20452 in 20.x as well, and I think it's too risky to install
20453 so near the release of 21.1. 2001-09-25 gerd. */
20454
20455 static int
20456 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20457 struct window *w;
20458 int charpos;
20459 int *hpos, *vpos, *x, *y;
20460 Lisp_Object stop;
20461 {
20462 struct glyph_row *row, *first;
20463 struct glyph *glyph, *end;
20464 int past_end = 0;
20465
20466 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20467 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20468 {
20469 *x = first->x;
20470 *y = first->y;
20471 *hpos = 0;
20472 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20473 return 1;
20474 }
20475
20476 row = row_containing_pos (w, charpos, first, NULL, 0);
20477 if (row == NULL)
20478 {
20479 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20480 past_end = 1;
20481 }
20482
20483 *x = row->x;
20484 *y = row->y;
20485 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20486
20487 glyph = row->glyphs[TEXT_AREA];
20488 end = glyph + row->used[TEXT_AREA];
20489
20490 /* Skip over glyphs not having an object at the start of the row.
20491 These are special glyphs like truncation marks on terminal
20492 frames. */
20493 if (row->displays_text_p)
20494 while (glyph < end
20495 && INTEGERP (glyph->object)
20496 && !EQ (stop, glyph->object)
20497 && glyph->charpos < 0)
20498 {
20499 *x += glyph->pixel_width;
20500 ++glyph;
20501 }
20502
20503 while (glyph < end
20504 && !INTEGERP (glyph->object)
20505 && !EQ (stop, glyph->object)
20506 && (!BUFFERP (glyph->object)
20507 || glyph->charpos < charpos))
20508 {
20509 *x += glyph->pixel_width;
20510 ++glyph;
20511 }
20512
20513 *hpos = glyph - row->glyphs[TEXT_AREA];
20514 return !past_end;
20515 }
20516
20517 #else /* not 1 */
20518
20519 static int
20520 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20521 struct window *w;
20522 int pos;
20523 int *hpos, *vpos, *x, *y;
20524 Lisp_Object stop;
20525 {
20526 int i;
20527 int lastcol;
20528 int maybe_next_line_p = 0;
20529 int line_start_position;
20530 int yb = window_text_bottom_y (w);
20531 struct glyph_row *row, *best_row;
20532 int row_vpos, best_row_vpos;
20533 int current_x;
20534
20535 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20536 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20537
20538 while (row->y < yb)
20539 {
20540 if (row->used[TEXT_AREA])
20541 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20542 else
20543 line_start_position = 0;
20544
20545 if (line_start_position > pos)
20546 break;
20547 /* If the position sought is the end of the buffer,
20548 don't include the blank lines at the bottom of the window. */
20549 else if (line_start_position == pos
20550 && pos == BUF_ZV (XBUFFER (w->buffer)))
20551 {
20552 maybe_next_line_p = 1;
20553 break;
20554 }
20555 else if (line_start_position > 0)
20556 {
20557 best_row = row;
20558 best_row_vpos = row_vpos;
20559 }
20560
20561 if (row->y + row->height >= yb)
20562 break;
20563
20564 ++row;
20565 ++row_vpos;
20566 }
20567
20568 /* Find the right column within BEST_ROW. */
20569 lastcol = 0;
20570 current_x = best_row->x;
20571 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20572 {
20573 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20574 int charpos = glyph->charpos;
20575
20576 if (BUFFERP (glyph->object))
20577 {
20578 if (charpos == pos)
20579 {
20580 *hpos = i;
20581 *vpos = best_row_vpos;
20582 *x = current_x;
20583 *y = best_row->y;
20584 return 1;
20585 }
20586 else if (charpos > pos)
20587 break;
20588 }
20589 else if (EQ (glyph->object, stop))
20590 break;
20591
20592 if (charpos > 0)
20593 lastcol = i;
20594 current_x += glyph->pixel_width;
20595 }
20596
20597 /* If we're looking for the end of the buffer,
20598 and we didn't find it in the line we scanned,
20599 use the start of the following line. */
20600 if (maybe_next_line_p)
20601 {
20602 ++best_row;
20603 ++best_row_vpos;
20604 lastcol = 0;
20605 current_x = best_row->x;
20606 }
20607
20608 *vpos = best_row_vpos;
20609 *hpos = lastcol + 1;
20610 *x = current_x;
20611 *y = best_row->y;
20612 return 0;
20613 }
20614
20615 #endif /* not 1 */
20616
20617
20618 /* Find the position of the glyph for position POS in OBJECT in
20619 window W's current matrix, and return in *X, *Y the pixel
20620 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20621
20622 RIGHT_P non-zero means return the position of the right edge of the
20623 glyph, RIGHT_P zero means return the left edge position.
20624
20625 If no glyph for POS exists in the matrix, return the position of
20626 the glyph with the next smaller position that is in the matrix, if
20627 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20628 exists in the matrix, return the position of the glyph with the
20629 next larger position in OBJECT.
20630
20631 Value is non-zero if a glyph was found. */
20632
20633 static int
20634 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20635 struct window *w;
20636 int pos;
20637 Lisp_Object object;
20638 int *hpos, *vpos, *x, *y;
20639 int right_p;
20640 {
20641 int yb = window_text_bottom_y (w);
20642 struct glyph_row *r;
20643 struct glyph *best_glyph = NULL;
20644 struct glyph_row *best_row = NULL;
20645 int best_x = 0;
20646
20647 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20648 r->enabled_p && r->y < yb;
20649 ++r)
20650 {
20651 struct glyph *g = r->glyphs[TEXT_AREA];
20652 struct glyph *e = g + r->used[TEXT_AREA];
20653 int gx;
20654
20655 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20656 if (EQ (g->object, object))
20657 {
20658 if (g->charpos == pos)
20659 {
20660 best_glyph = g;
20661 best_x = gx;
20662 best_row = r;
20663 goto found;
20664 }
20665 else if (best_glyph == NULL
20666 || ((abs (g->charpos - pos)
20667 < abs (best_glyph->charpos - pos))
20668 && (right_p
20669 ? g->charpos < pos
20670 : g->charpos > pos)))
20671 {
20672 best_glyph = g;
20673 best_x = gx;
20674 best_row = r;
20675 }
20676 }
20677 }
20678
20679 found:
20680
20681 if (best_glyph)
20682 {
20683 *x = best_x;
20684 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20685
20686 if (right_p)
20687 {
20688 *x += best_glyph->pixel_width;
20689 ++*hpos;
20690 }
20691
20692 *y = best_row->y;
20693 *vpos = best_row - w->current_matrix->rows;
20694 }
20695
20696 return best_glyph != NULL;
20697 }
20698
20699
20700 /* See if position X, Y is within a hot-spot of an image. */
20701
20702 static int
20703 on_hot_spot_p (hot_spot, x, y)
20704 Lisp_Object hot_spot;
20705 int x, y;
20706 {
20707 if (!CONSP (hot_spot))
20708 return 0;
20709
20710 if (EQ (XCAR (hot_spot), Qrect))
20711 {
20712 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20713 Lisp_Object rect = XCDR (hot_spot);
20714 Lisp_Object tem;
20715 if (!CONSP (rect))
20716 return 0;
20717 if (!CONSP (XCAR (rect)))
20718 return 0;
20719 if (!CONSP (XCDR (rect)))
20720 return 0;
20721 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20722 return 0;
20723 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20724 return 0;
20725 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20726 return 0;
20727 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20728 return 0;
20729 return 1;
20730 }
20731 else if (EQ (XCAR (hot_spot), Qcircle))
20732 {
20733 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20734 Lisp_Object circ = XCDR (hot_spot);
20735 Lisp_Object lr, lx0, ly0;
20736 if (CONSP (circ)
20737 && CONSP (XCAR (circ))
20738 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20739 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20740 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20741 {
20742 double r = XFLOATINT (lr);
20743 double dx = XINT (lx0) - x;
20744 double dy = XINT (ly0) - y;
20745 return (dx * dx + dy * dy <= r * r);
20746 }
20747 }
20748 else if (EQ (XCAR (hot_spot), Qpoly))
20749 {
20750 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20751 if (VECTORP (XCDR (hot_spot)))
20752 {
20753 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20754 Lisp_Object *poly = v->contents;
20755 int n = v->size;
20756 int i;
20757 int inside = 0;
20758 Lisp_Object lx, ly;
20759 int x0, y0;
20760
20761 /* Need an even number of coordinates, and at least 3 edges. */
20762 if (n < 6 || n & 1)
20763 return 0;
20764
20765 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20766 If count is odd, we are inside polygon. Pixels on edges
20767 may or may not be included depending on actual geometry of the
20768 polygon. */
20769 if ((lx = poly[n-2], !INTEGERP (lx))
20770 || (ly = poly[n-1], !INTEGERP (lx)))
20771 return 0;
20772 x0 = XINT (lx), y0 = XINT (ly);
20773 for (i = 0; i < n; i += 2)
20774 {
20775 int x1 = x0, y1 = y0;
20776 if ((lx = poly[i], !INTEGERP (lx))
20777 || (ly = poly[i+1], !INTEGERP (ly)))
20778 return 0;
20779 x0 = XINT (lx), y0 = XINT (ly);
20780
20781 /* Does this segment cross the X line? */
20782 if (x0 >= x)
20783 {
20784 if (x1 >= x)
20785 continue;
20786 }
20787 else if (x1 < x)
20788 continue;
20789 if (y > y0 && y > y1)
20790 continue;
20791 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20792 inside = !inside;
20793 }
20794 return inside;
20795 }
20796 }
20797 /* If we don't understand the format, pretend we're not in the hot-spot. */
20798 return 0;
20799 }
20800
20801 Lisp_Object
20802 find_hot_spot (map, x, y)
20803 Lisp_Object map;
20804 int x, y;
20805 {
20806 while (CONSP (map))
20807 {
20808 if (CONSP (XCAR (map))
20809 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20810 return XCAR (map);
20811 map = XCDR (map);
20812 }
20813
20814 return Qnil;
20815 }
20816
20817 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20818 3, 3, 0,
20819 doc: /* Lookup in image map MAP coordinates X and Y.
20820 An image map is an alist where each element has the format (AREA ID PLIST).
20821 An AREA is specified as either a rectangle, a circle, or a polygon:
20822 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20823 pixel coordinates of the upper left and bottom right corners.
20824 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20825 and the radius of the circle; r may be a float or integer.
20826 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20827 vector describes one corner in the polygon.
20828 Returns the alist element for the first matching AREA in MAP. */)
20829 (map, x, y)
20830 Lisp_Object map;
20831 Lisp_Object x, y;
20832 {
20833 if (NILP (map))
20834 return Qnil;
20835
20836 CHECK_NUMBER (x);
20837 CHECK_NUMBER (y);
20838
20839 return find_hot_spot (map, XINT (x), XINT (y));
20840 }
20841
20842
20843 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20844 static void
20845 define_frame_cursor1 (f, cursor, pointer)
20846 struct frame *f;
20847 Cursor cursor;
20848 Lisp_Object pointer;
20849 {
20850 /* Do not change cursor shape while dragging mouse. */
20851 if (!NILP (do_mouse_tracking))
20852 return;
20853
20854 if (!NILP (pointer))
20855 {
20856 if (EQ (pointer, Qarrow))
20857 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20858 else if (EQ (pointer, Qhand))
20859 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20860 else if (EQ (pointer, Qtext))
20861 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20862 else if (EQ (pointer, intern ("hdrag")))
20863 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20864 #ifdef HAVE_X_WINDOWS
20865 else if (EQ (pointer, intern ("vdrag")))
20866 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20867 #endif
20868 else if (EQ (pointer, intern ("hourglass")))
20869 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20870 else if (EQ (pointer, Qmodeline))
20871 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20872 else
20873 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20874 }
20875
20876 if (cursor != No_Cursor)
20877 rif->define_frame_cursor (f, cursor);
20878 }
20879
20880 /* Take proper action when mouse has moved to the mode or header line
20881 or marginal area AREA of window W, x-position X and y-position Y.
20882 X is relative to the start of the text display area of W, so the
20883 width of bitmap areas and scroll bars must be subtracted to get a
20884 position relative to the start of the mode line. */
20885
20886 static void
20887 note_mode_line_or_margin_highlight (w, x, y, area)
20888 struct window *w;
20889 int x, y;
20890 enum window_part area;
20891 {
20892 struct frame *f = XFRAME (w->frame);
20893 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20894 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20895 Lisp_Object pointer = Qnil;
20896 int charpos, dx, dy, width, height;
20897 Lisp_Object string, object = Qnil;
20898 Lisp_Object pos, help;
20899
20900 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20901 string = mode_line_string (w, area, &x, &y, &charpos,
20902 &object, &dx, &dy, &width, &height);
20903 else
20904 {
20905 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20906 string = marginal_area_string (w, area, &x, &y, &charpos,
20907 &object, &dx, &dy, &width, &height);
20908 }
20909
20910 help = Qnil;
20911
20912 if (IMAGEP (object))
20913 {
20914 Lisp_Object image_map, hotspot;
20915 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
20916 !NILP (image_map))
20917 && (hotspot = find_hot_spot (image_map, dx, dy),
20918 CONSP (hotspot))
20919 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20920 {
20921 Lisp_Object area_id, plist;
20922
20923 area_id = XCAR (hotspot);
20924 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20925 If so, we could look for mouse-enter, mouse-leave
20926 properties in PLIST (and do something...). */
20927 hotspot = XCDR (hotspot);
20928 if (CONSP (hotspot)
20929 && (plist = XCAR (hotspot), CONSP (plist)))
20930 {
20931 pointer = Fsafe_plist_get (plist, Qpointer);
20932 if (NILP (pointer))
20933 pointer = Qhand;
20934 help = Fsafe_plist_get (plist, Qhelp_echo);
20935 if (!NILP (help))
20936 {
20937 help_echo_string = help;
20938 /* Is this correct? ++kfs */
20939 XSETWINDOW (help_echo_window, w);
20940 help_echo_object = w->buffer;
20941 help_echo_pos = charpos;
20942 }
20943 }
20944 if (NILP (pointer))
20945 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
20946 }
20947 }
20948
20949 if (STRINGP (string))
20950 {
20951 pos = make_number (charpos);
20952 /* If we're on a string with `help-echo' text property, arrange
20953 for the help to be displayed. This is done by setting the
20954 global variable help_echo_string to the help string. */
20955 if (NILP (help))
20956 {
20957 help = Fget_text_property (pos, Qhelp_echo, string);
20958 if (!NILP (help))
20959 {
20960 help_echo_string = help;
20961 XSETWINDOW (help_echo_window, w);
20962 help_echo_object = string;
20963 help_echo_pos = charpos;
20964 }
20965 }
20966
20967 if (NILP (pointer))
20968 pointer = Fget_text_property (pos, Qpointer, string);
20969
20970 /* Change the mouse pointer according to what is under X/Y. */
20971 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20972 {
20973 Lisp_Object map;
20974 map = Fget_text_property (pos, Qlocal_map, string);
20975 if (!KEYMAPP (map))
20976 map = Fget_text_property (pos, Qkeymap, string);
20977 if (!KEYMAPP (map))
20978 cursor = dpyinfo->vertical_scroll_bar_cursor;
20979 }
20980 }
20981
20982 define_frame_cursor1 (f, cursor, pointer);
20983 }
20984
20985
20986 /* EXPORT:
20987 Take proper action when the mouse has moved to position X, Y on
20988 frame F as regards highlighting characters that have mouse-face
20989 properties. Also de-highlighting chars where the mouse was before.
20990 X and Y can be negative or out of range. */
20991
20992 void
20993 note_mouse_highlight (f, x, y)
20994 struct frame *f;
20995 int x, y;
20996 {
20997 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20998 enum window_part part;
20999 Lisp_Object window;
21000 struct window *w;
21001 Cursor cursor = No_Cursor;
21002 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21003 struct buffer *b;
21004
21005 /* When a menu is active, don't highlight because this looks odd. */
21006 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21007 if (popup_activated ())
21008 return;
21009 #endif
21010
21011 if (NILP (Vmouse_highlight)
21012 || !f->glyphs_initialized_p)
21013 return;
21014
21015 dpyinfo->mouse_face_mouse_x = x;
21016 dpyinfo->mouse_face_mouse_y = y;
21017 dpyinfo->mouse_face_mouse_frame = f;
21018
21019 if (dpyinfo->mouse_face_defer)
21020 return;
21021
21022 if (gc_in_progress)
21023 {
21024 dpyinfo->mouse_face_deferred_gc = 1;
21025 return;
21026 }
21027
21028 /* Which window is that in? */
21029 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21030
21031 /* If we were displaying active text in another window, clear that.
21032 Also clear if we move out of text area in same window. */
21033 if (! EQ (window, dpyinfo->mouse_face_window)
21034 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21035 clear_mouse_face (dpyinfo);
21036
21037 /* Not on a window -> return. */
21038 if (!WINDOWP (window))
21039 return;
21040
21041 /* Reset help_echo_string. It will get recomputed below. */
21042 help_echo_string = Qnil;
21043
21044 /* Convert to window-relative pixel coordinates. */
21045 w = XWINDOW (window);
21046 frame_to_window_pixel_xy (w, &x, &y);
21047
21048 /* Handle tool-bar window differently since it doesn't display a
21049 buffer. */
21050 if (EQ (window, f->tool_bar_window))
21051 {
21052 note_tool_bar_highlight (f, x, y);
21053 return;
21054 }
21055
21056 /* Mouse is on the mode, header line or margin? */
21057 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21058 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21059 {
21060 note_mode_line_or_margin_highlight (w, x, y, part);
21061 return;
21062 }
21063
21064 if (part == ON_VERTICAL_BORDER)
21065 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21066 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21067 || part == ON_SCROLL_BAR)
21068 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21069 else
21070 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21071
21072 /* Are we in a window whose display is up to date?
21073 And verify the buffer's text has not changed. */
21074 b = XBUFFER (w->buffer);
21075 if (part == ON_TEXT
21076 && EQ (w->window_end_valid, w->buffer)
21077 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21078 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21079 {
21080 int hpos, vpos, pos, i, dx, dy, area;
21081 struct glyph *glyph;
21082 Lisp_Object object;
21083 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21084 Lisp_Object *overlay_vec = NULL;
21085 int noverlays;
21086 struct buffer *obuf;
21087 int obegv, ozv, same_region;
21088
21089 /* Find the glyph under X/Y. */
21090 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21091
21092 /* Look for :pointer property on image. */
21093 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21094 {
21095 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21096 if (img != NULL && IMAGEP (img->spec))
21097 {
21098 Lisp_Object image_map, hotspot;
21099 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21100 !NILP (image_map))
21101 && (hotspot = find_hot_spot (image_map,
21102 glyph->slice.x + dx,
21103 glyph->slice.y + dy),
21104 CONSP (hotspot))
21105 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21106 {
21107 Lisp_Object area_id, plist;
21108
21109 area_id = XCAR (hotspot);
21110 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21111 If so, we could look for mouse-enter, mouse-leave
21112 properties in PLIST (and do something...). */
21113 hotspot = XCDR (hotspot);
21114 if (CONSP (hotspot)
21115 && (plist = XCAR (hotspot), CONSP (plist)))
21116 {
21117 pointer = Fsafe_plist_get (plist, Qpointer);
21118 if (NILP (pointer))
21119 pointer = Qhand;
21120 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21121 if (!NILP (help_echo_string))
21122 {
21123 help_echo_window = window;
21124 help_echo_object = glyph->object;
21125 help_echo_pos = glyph->charpos;
21126 }
21127 }
21128 }
21129 if (NILP (pointer))
21130 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21131 }
21132 }
21133
21134 /* Clear mouse face if X/Y not over text. */
21135 if (glyph == NULL
21136 || area != TEXT_AREA
21137 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21138 {
21139 if (clear_mouse_face (dpyinfo))
21140 cursor = No_Cursor;
21141 if (NILP (pointer))
21142 {
21143 if (area != TEXT_AREA)
21144 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21145 else
21146 pointer = Vvoid_text_area_pointer;
21147 }
21148 goto set_cursor;
21149 }
21150
21151 pos = glyph->charpos;
21152 object = glyph->object;
21153 if (!STRINGP (object) && !BUFFERP (object))
21154 goto set_cursor;
21155
21156 /* If we get an out-of-range value, return now; avoid an error. */
21157 if (BUFFERP (object) && pos > BUF_Z (b))
21158 goto set_cursor;
21159
21160 /* Make the window's buffer temporarily current for
21161 overlays_at and compute_char_face. */
21162 obuf = current_buffer;
21163 current_buffer = b;
21164 obegv = BEGV;
21165 ozv = ZV;
21166 BEGV = BEG;
21167 ZV = Z;
21168
21169 /* Is this char mouse-active or does it have help-echo? */
21170 position = make_number (pos);
21171
21172 if (BUFFERP (object))
21173 {
21174 /* Put all the overlays we want in a vector in overlay_vec. */
21175 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21176 /* Sort overlays into increasing priority order. */
21177 noverlays = sort_overlays (overlay_vec, noverlays, w);
21178 }
21179 else
21180 noverlays = 0;
21181
21182 same_region = (EQ (window, dpyinfo->mouse_face_window)
21183 && vpos >= dpyinfo->mouse_face_beg_row
21184 && vpos <= dpyinfo->mouse_face_end_row
21185 && (vpos > dpyinfo->mouse_face_beg_row
21186 || hpos >= dpyinfo->mouse_face_beg_col)
21187 && (vpos < dpyinfo->mouse_face_end_row
21188 || hpos < dpyinfo->mouse_face_end_col
21189 || dpyinfo->mouse_face_past_end));
21190
21191 if (same_region)
21192 cursor = No_Cursor;
21193
21194 /* Check mouse-face highlighting. */
21195 if (! same_region
21196 /* If there exists an overlay with mouse-face overlapping
21197 the one we are currently highlighting, we have to
21198 check if we enter the overlapping overlay, and then
21199 highlight only that. */
21200 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21201 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21202 {
21203 /* Find the highest priority overlay that has a mouse-face
21204 property. */
21205 overlay = Qnil;
21206 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21207 {
21208 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21209 if (!NILP (mouse_face))
21210 overlay = overlay_vec[i];
21211 }
21212
21213 /* If we're actually highlighting the same overlay as
21214 before, there's no need to do that again. */
21215 if (!NILP (overlay)
21216 && EQ (overlay, dpyinfo->mouse_face_overlay))
21217 goto check_help_echo;
21218
21219 dpyinfo->mouse_face_overlay = overlay;
21220
21221 /* Clear the display of the old active region, if any. */
21222 if (clear_mouse_face (dpyinfo))
21223 cursor = No_Cursor;
21224
21225 /* If no overlay applies, get a text property. */
21226 if (NILP (overlay))
21227 mouse_face = Fget_text_property (position, Qmouse_face, object);
21228
21229 /* Handle the overlay case. */
21230 if (!NILP (overlay))
21231 {
21232 /* Find the range of text around this char that
21233 should be active. */
21234 Lisp_Object before, after;
21235 int ignore;
21236
21237 before = Foverlay_start (overlay);
21238 after = Foverlay_end (overlay);
21239 /* Record this as the current active region. */
21240 fast_find_position (w, XFASTINT (before),
21241 &dpyinfo->mouse_face_beg_col,
21242 &dpyinfo->mouse_face_beg_row,
21243 &dpyinfo->mouse_face_beg_x,
21244 &dpyinfo->mouse_face_beg_y, Qnil);
21245
21246 dpyinfo->mouse_face_past_end
21247 = !fast_find_position (w, XFASTINT (after),
21248 &dpyinfo->mouse_face_end_col,
21249 &dpyinfo->mouse_face_end_row,
21250 &dpyinfo->mouse_face_end_x,
21251 &dpyinfo->mouse_face_end_y, Qnil);
21252 dpyinfo->mouse_face_window = window;
21253
21254 dpyinfo->mouse_face_face_id
21255 = face_at_buffer_position (w, pos, 0, 0,
21256 &ignore, pos + 1,
21257 !dpyinfo->mouse_face_hidden);
21258
21259 /* Display it as active. */
21260 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21261 cursor = No_Cursor;
21262 }
21263 /* Handle the text property case. */
21264 else if (!NILP (mouse_face) && BUFFERP (object))
21265 {
21266 /* Find the range of text around this char that
21267 should be active. */
21268 Lisp_Object before, after, beginning, end;
21269 int ignore;
21270
21271 beginning = Fmarker_position (w->start);
21272 end = make_number (BUF_Z (XBUFFER (object))
21273 - XFASTINT (w->window_end_pos));
21274 before
21275 = Fprevious_single_property_change (make_number (pos + 1),
21276 Qmouse_face,
21277 object, beginning);
21278 after
21279 = Fnext_single_property_change (position, Qmouse_face,
21280 object, end);
21281
21282 /* Record this as the current active region. */
21283 fast_find_position (w, XFASTINT (before),
21284 &dpyinfo->mouse_face_beg_col,
21285 &dpyinfo->mouse_face_beg_row,
21286 &dpyinfo->mouse_face_beg_x,
21287 &dpyinfo->mouse_face_beg_y, Qnil);
21288 dpyinfo->mouse_face_past_end
21289 = !fast_find_position (w, XFASTINT (after),
21290 &dpyinfo->mouse_face_end_col,
21291 &dpyinfo->mouse_face_end_row,
21292 &dpyinfo->mouse_face_end_x,
21293 &dpyinfo->mouse_face_end_y, Qnil);
21294 dpyinfo->mouse_face_window = window;
21295
21296 if (BUFFERP (object))
21297 dpyinfo->mouse_face_face_id
21298 = face_at_buffer_position (w, pos, 0, 0,
21299 &ignore, pos + 1,
21300 !dpyinfo->mouse_face_hidden);
21301
21302 /* Display it as active. */
21303 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21304 cursor = No_Cursor;
21305 }
21306 else if (!NILP (mouse_face) && STRINGP (object))
21307 {
21308 Lisp_Object b, e;
21309 int ignore;
21310
21311 b = Fprevious_single_property_change (make_number (pos + 1),
21312 Qmouse_face,
21313 object, Qnil);
21314 e = Fnext_single_property_change (position, Qmouse_face,
21315 object, Qnil);
21316 if (NILP (b))
21317 b = make_number (0);
21318 if (NILP (e))
21319 e = make_number (SCHARS (object) - 1);
21320 fast_find_string_pos (w, XINT (b), object,
21321 &dpyinfo->mouse_face_beg_col,
21322 &dpyinfo->mouse_face_beg_row,
21323 &dpyinfo->mouse_face_beg_x,
21324 &dpyinfo->mouse_face_beg_y, 0);
21325 fast_find_string_pos (w, XINT (e), object,
21326 &dpyinfo->mouse_face_end_col,
21327 &dpyinfo->mouse_face_end_row,
21328 &dpyinfo->mouse_face_end_x,
21329 &dpyinfo->mouse_face_end_y, 1);
21330 dpyinfo->mouse_face_past_end = 0;
21331 dpyinfo->mouse_face_window = window;
21332 dpyinfo->mouse_face_face_id
21333 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21334 glyph->face_id, 1);
21335 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21336 cursor = No_Cursor;
21337 }
21338 else if (STRINGP (object) && NILP (mouse_face))
21339 {
21340 /* A string which doesn't have mouse-face, but
21341 the text ``under'' it might have. */
21342 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21343 int start = MATRIX_ROW_START_CHARPOS (r);
21344
21345 pos = string_buffer_position (w, object, start);
21346 if (pos > 0)
21347 mouse_face = get_char_property_and_overlay (make_number (pos),
21348 Qmouse_face,
21349 w->buffer,
21350 &overlay);
21351 if (!NILP (mouse_face) && !NILP (overlay))
21352 {
21353 Lisp_Object before = Foverlay_start (overlay);
21354 Lisp_Object after = Foverlay_end (overlay);
21355 int ignore;
21356
21357 /* Note that we might not be able to find position
21358 BEFORE in the glyph matrix if the overlay is
21359 entirely covered by a `display' property. In
21360 this case, we overshoot. So let's stop in
21361 the glyph matrix before glyphs for OBJECT. */
21362 fast_find_position (w, XFASTINT (before),
21363 &dpyinfo->mouse_face_beg_col,
21364 &dpyinfo->mouse_face_beg_row,
21365 &dpyinfo->mouse_face_beg_x,
21366 &dpyinfo->mouse_face_beg_y,
21367 object);
21368
21369 dpyinfo->mouse_face_past_end
21370 = !fast_find_position (w, XFASTINT (after),
21371 &dpyinfo->mouse_face_end_col,
21372 &dpyinfo->mouse_face_end_row,
21373 &dpyinfo->mouse_face_end_x,
21374 &dpyinfo->mouse_face_end_y,
21375 Qnil);
21376 dpyinfo->mouse_face_window = window;
21377 dpyinfo->mouse_face_face_id
21378 = face_at_buffer_position (w, pos, 0, 0,
21379 &ignore, pos + 1,
21380 !dpyinfo->mouse_face_hidden);
21381
21382 /* Display it as active. */
21383 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21384 cursor = No_Cursor;
21385 }
21386 }
21387 }
21388
21389 check_help_echo:
21390
21391 /* Look for a `help-echo' property. */
21392 if (NILP (help_echo_string)) {
21393 Lisp_Object help, overlay;
21394
21395 /* Check overlays first. */
21396 help = overlay = Qnil;
21397 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21398 {
21399 overlay = overlay_vec[i];
21400 help = Foverlay_get (overlay, Qhelp_echo);
21401 }
21402
21403 if (!NILP (help))
21404 {
21405 help_echo_string = help;
21406 help_echo_window = window;
21407 help_echo_object = overlay;
21408 help_echo_pos = pos;
21409 }
21410 else
21411 {
21412 Lisp_Object object = glyph->object;
21413 int charpos = glyph->charpos;
21414
21415 /* Try text properties. */
21416 if (STRINGP (object)
21417 && charpos >= 0
21418 && charpos < SCHARS (object))
21419 {
21420 help = Fget_text_property (make_number (charpos),
21421 Qhelp_echo, object);
21422 if (NILP (help))
21423 {
21424 /* If the string itself doesn't specify a help-echo,
21425 see if the buffer text ``under'' it does. */
21426 struct glyph_row *r
21427 = MATRIX_ROW (w->current_matrix, vpos);
21428 int start = MATRIX_ROW_START_CHARPOS (r);
21429 int pos = string_buffer_position (w, object, start);
21430 if (pos > 0)
21431 {
21432 help = Fget_char_property (make_number (pos),
21433 Qhelp_echo, w->buffer);
21434 if (!NILP (help))
21435 {
21436 charpos = pos;
21437 object = w->buffer;
21438 }
21439 }
21440 }
21441 }
21442 else if (BUFFERP (object)
21443 && charpos >= BEGV
21444 && charpos < ZV)
21445 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21446 object);
21447
21448 if (!NILP (help))
21449 {
21450 help_echo_string = help;
21451 help_echo_window = window;
21452 help_echo_object = object;
21453 help_echo_pos = charpos;
21454 }
21455 }
21456 }
21457
21458 /* Look for a `pointer' property. */
21459 if (NILP (pointer))
21460 {
21461 /* Check overlays first. */
21462 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21463 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21464
21465 if (NILP (pointer))
21466 {
21467 Lisp_Object object = glyph->object;
21468 int charpos = glyph->charpos;
21469
21470 /* Try text properties. */
21471 if (STRINGP (object)
21472 && charpos >= 0
21473 && charpos < SCHARS (object))
21474 {
21475 pointer = Fget_text_property (make_number (charpos),
21476 Qpointer, object);
21477 if (NILP (pointer))
21478 {
21479 /* If the string itself doesn't specify a pointer,
21480 see if the buffer text ``under'' it does. */
21481 struct glyph_row *r
21482 = MATRIX_ROW (w->current_matrix, vpos);
21483 int start = MATRIX_ROW_START_CHARPOS (r);
21484 int pos = string_buffer_position (w, object, start);
21485 if (pos > 0)
21486 pointer = Fget_char_property (make_number (pos),
21487 Qpointer, w->buffer);
21488 }
21489 }
21490 else if (BUFFERP (object)
21491 && charpos >= BEGV
21492 && charpos < ZV)
21493 pointer = Fget_text_property (make_number (charpos),
21494 Qpointer, object);
21495 }
21496 }
21497
21498 BEGV = obegv;
21499 ZV = ozv;
21500 current_buffer = obuf;
21501 }
21502
21503 set_cursor:
21504
21505 define_frame_cursor1 (f, cursor, pointer);
21506 }
21507
21508
21509 /* EXPORT for RIF:
21510 Clear any mouse-face on window W. This function is part of the
21511 redisplay interface, and is called from try_window_id and similar
21512 functions to ensure the mouse-highlight is off. */
21513
21514 void
21515 x_clear_window_mouse_face (w)
21516 struct window *w;
21517 {
21518 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21519 Lisp_Object window;
21520
21521 BLOCK_INPUT;
21522 XSETWINDOW (window, w);
21523 if (EQ (window, dpyinfo->mouse_face_window))
21524 clear_mouse_face (dpyinfo);
21525 UNBLOCK_INPUT;
21526 }
21527
21528
21529 /* EXPORT:
21530 Just discard the mouse face information for frame F, if any.
21531 This is used when the size of F is changed. */
21532
21533 void
21534 cancel_mouse_face (f)
21535 struct frame *f;
21536 {
21537 Lisp_Object window;
21538 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21539
21540 window = dpyinfo->mouse_face_window;
21541 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21542 {
21543 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21544 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21545 dpyinfo->mouse_face_window = Qnil;
21546 }
21547 }
21548
21549
21550 #endif /* HAVE_WINDOW_SYSTEM */
21551
21552 \f
21553 /***********************************************************************
21554 Exposure Events
21555 ***********************************************************************/
21556
21557 #ifdef HAVE_WINDOW_SYSTEM
21558
21559 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21560 which intersects rectangle R. R is in window-relative coordinates. */
21561
21562 static void
21563 expose_area (w, row, r, area)
21564 struct window *w;
21565 struct glyph_row *row;
21566 XRectangle *r;
21567 enum glyph_row_area area;
21568 {
21569 struct glyph *first = row->glyphs[area];
21570 struct glyph *end = row->glyphs[area] + row->used[area];
21571 struct glyph *last;
21572 int first_x, start_x, x;
21573
21574 if (area == TEXT_AREA && row->fill_line_p)
21575 /* If row extends face to end of line write the whole line. */
21576 draw_glyphs (w, 0, row, area,
21577 0, row->used[area],
21578 DRAW_NORMAL_TEXT, 0);
21579 else
21580 {
21581 /* Set START_X to the window-relative start position for drawing glyphs of
21582 AREA. The first glyph of the text area can be partially visible.
21583 The first glyphs of other areas cannot. */
21584 start_x = window_box_left_offset (w, area);
21585 x = start_x;
21586 if (area == TEXT_AREA)
21587 x += row->x;
21588
21589 /* Find the first glyph that must be redrawn. */
21590 while (first < end
21591 && x + first->pixel_width < r->x)
21592 {
21593 x += first->pixel_width;
21594 ++first;
21595 }
21596
21597 /* Find the last one. */
21598 last = first;
21599 first_x = x;
21600 while (last < end
21601 && x < r->x + r->width)
21602 {
21603 x += last->pixel_width;
21604 ++last;
21605 }
21606
21607 /* Repaint. */
21608 if (last > first)
21609 draw_glyphs (w, first_x - start_x, row, area,
21610 first - row->glyphs[area], last - row->glyphs[area],
21611 DRAW_NORMAL_TEXT, 0);
21612 }
21613 }
21614
21615
21616 /* Redraw the parts of the glyph row ROW on window W intersecting
21617 rectangle R. R is in window-relative coordinates. Value is
21618 non-zero if mouse-face was overwritten. */
21619
21620 static int
21621 expose_line (w, row, r)
21622 struct window *w;
21623 struct glyph_row *row;
21624 XRectangle *r;
21625 {
21626 xassert (row->enabled_p);
21627
21628 if (row->mode_line_p || w->pseudo_window_p)
21629 draw_glyphs (w, 0, row, TEXT_AREA,
21630 0, row->used[TEXT_AREA],
21631 DRAW_NORMAL_TEXT, 0);
21632 else
21633 {
21634 if (row->used[LEFT_MARGIN_AREA])
21635 expose_area (w, row, r, LEFT_MARGIN_AREA);
21636 if (row->used[TEXT_AREA])
21637 expose_area (w, row, r, TEXT_AREA);
21638 if (row->used[RIGHT_MARGIN_AREA])
21639 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21640 draw_row_fringe_bitmaps (w, row);
21641 }
21642
21643 return row->mouse_face_p;
21644 }
21645
21646
21647 /* Redraw those parts of glyphs rows during expose event handling that
21648 overlap other rows. Redrawing of an exposed line writes over parts
21649 of lines overlapping that exposed line; this function fixes that.
21650
21651 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21652 row in W's current matrix that is exposed and overlaps other rows.
21653 LAST_OVERLAPPING_ROW is the last such row. */
21654
21655 static void
21656 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21657 struct window *w;
21658 struct glyph_row *first_overlapping_row;
21659 struct glyph_row *last_overlapping_row;
21660 {
21661 struct glyph_row *row;
21662
21663 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21664 if (row->overlapping_p)
21665 {
21666 xassert (row->enabled_p && !row->mode_line_p);
21667
21668 if (row->used[LEFT_MARGIN_AREA])
21669 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21670
21671 if (row->used[TEXT_AREA])
21672 x_fix_overlapping_area (w, row, TEXT_AREA);
21673
21674 if (row->used[RIGHT_MARGIN_AREA])
21675 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21676 }
21677 }
21678
21679
21680 /* Return non-zero if W's cursor intersects rectangle R. */
21681
21682 static int
21683 phys_cursor_in_rect_p (w, r)
21684 struct window *w;
21685 XRectangle *r;
21686 {
21687 XRectangle cr, result;
21688 struct glyph *cursor_glyph;
21689
21690 cursor_glyph = get_phys_cursor_glyph (w);
21691 if (cursor_glyph)
21692 {
21693 /* r is relative to W's box, but w->phys_cursor.x is relative
21694 to left edge of W's TEXT area. Adjust it. */
21695 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21696 cr.y = w->phys_cursor.y;
21697 cr.width = cursor_glyph->pixel_width;
21698 cr.height = w->phys_cursor_height;
21699 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21700 I assume the effect is the same -- and this is portable. */
21701 return x_intersect_rectangles (&cr, r, &result);
21702 }
21703 else
21704 return 0;
21705 }
21706
21707
21708 /* EXPORT:
21709 Draw a vertical window border to the right of window W if W doesn't
21710 have vertical scroll bars. */
21711
21712 void
21713 x_draw_vertical_border (w)
21714 struct window *w;
21715 {
21716 /* We could do better, if we knew what type of scroll-bar the adjacent
21717 windows (on either side) have... But we don't :-(
21718 However, I think this works ok. ++KFS 2003-04-25 */
21719
21720 /* Redraw borders between horizontally adjacent windows. Don't
21721 do it for frames with vertical scroll bars because either the
21722 right scroll bar of a window, or the left scroll bar of its
21723 neighbor will suffice as a border. */
21724 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21725 return;
21726
21727 if (!WINDOW_RIGHTMOST_P (w)
21728 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21729 {
21730 int x0, x1, y0, y1;
21731
21732 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21733 y1 -= 1;
21734
21735 rif->draw_vertical_window_border (w, x1, y0, y1);
21736 }
21737 else if (!WINDOW_LEFTMOST_P (w)
21738 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21739 {
21740 int x0, x1, y0, y1;
21741
21742 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21743 y1 -= 1;
21744
21745 rif->draw_vertical_window_border (w, x0, y0, y1);
21746 }
21747 }
21748
21749
21750 /* Redraw the part of window W intersection rectangle FR. Pixel
21751 coordinates in FR are frame-relative. Call this function with
21752 input blocked. Value is non-zero if the exposure overwrites
21753 mouse-face. */
21754
21755 static int
21756 expose_window (w, fr)
21757 struct window *w;
21758 XRectangle *fr;
21759 {
21760 struct frame *f = XFRAME (w->frame);
21761 XRectangle wr, r;
21762 int mouse_face_overwritten_p = 0;
21763
21764 /* If window is not yet fully initialized, do nothing. This can
21765 happen when toolkit scroll bars are used and a window is split.
21766 Reconfiguring the scroll bar will generate an expose for a newly
21767 created window. */
21768 if (w->current_matrix == NULL)
21769 return 0;
21770
21771 /* When we're currently updating the window, display and current
21772 matrix usually don't agree. Arrange for a thorough display
21773 later. */
21774 if (w == updated_window)
21775 {
21776 SET_FRAME_GARBAGED (f);
21777 return 0;
21778 }
21779
21780 /* Frame-relative pixel rectangle of W. */
21781 wr.x = WINDOW_LEFT_EDGE_X (w);
21782 wr.y = WINDOW_TOP_EDGE_Y (w);
21783 wr.width = WINDOW_TOTAL_WIDTH (w);
21784 wr.height = WINDOW_TOTAL_HEIGHT (w);
21785
21786 if (x_intersect_rectangles (fr, &wr, &r))
21787 {
21788 int yb = window_text_bottom_y (w);
21789 struct glyph_row *row;
21790 int cursor_cleared_p;
21791 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21792
21793 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21794 r.x, r.y, r.width, r.height));
21795
21796 /* Convert to window coordinates. */
21797 r.x -= WINDOW_LEFT_EDGE_X (w);
21798 r.y -= WINDOW_TOP_EDGE_Y (w);
21799
21800 /* Turn off the cursor. */
21801 if (!w->pseudo_window_p
21802 && phys_cursor_in_rect_p (w, &r))
21803 {
21804 x_clear_cursor (w);
21805 cursor_cleared_p = 1;
21806 }
21807 else
21808 cursor_cleared_p = 0;
21809
21810 /* Update lines intersecting rectangle R. */
21811 first_overlapping_row = last_overlapping_row = NULL;
21812 for (row = w->current_matrix->rows;
21813 row->enabled_p;
21814 ++row)
21815 {
21816 int y0 = row->y;
21817 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21818
21819 if ((y0 >= r.y && y0 < r.y + r.height)
21820 || (y1 > r.y && y1 < r.y + r.height)
21821 || (r.y >= y0 && r.y < y1)
21822 || (r.y + r.height > y0 && r.y + r.height < y1))
21823 {
21824 if (row->overlapping_p)
21825 {
21826 if (first_overlapping_row == NULL)
21827 first_overlapping_row = row;
21828 last_overlapping_row = row;
21829 }
21830
21831 if (expose_line (w, row, &r))
21832 mouse_face_overwritten_p = 1;
21833 }
21834
21835 if (y1 >= yb)
21836 break;
21837 }
21838
21839 /* Display the mode line if there is one. */
21840 if (WINDOW_WANTS_MODELINE_P (w)
21841 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21842 row->enabled_p)
21843 && row->y < r.y + r.height)
21844 {
21845 if (expose_line (w, row, &r))
21846 mouse_face_overwritten_p = 1;
21847 }
21848
21849 if (!w->pseudo_window_p)
21850 {
21851 /* Fix the display of overlapping rows. */
21852 if (first_overlapping_row)
21853 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21854
21855 /* Draw border between windows. */
21856 x_draw_vertical_border (w);
21857
21858 /* Turn the cursor on again. */
21859 if (cursor_cleared_p)
21860 update_window_cursor (w, 1);
21861 }
21862 }
21863
21864 #ifdef HAVE_CARBON
21865 /* Display scroll bar for this window. */
21866 if (!NILP (w->vertical_scroll_bar))
21867 {
21868 /* ++KFS:
21869 If this doesn't work here (maybe some header files are missing),
21870 make a function in macterm.c and call it to do the job! */
21871 ControlHandle ch
21872 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21873
21874 Draw1Control (ch);
21875 }
21876 #endif
21877
21878 return mouse_face_overwritten_p;
21879 }
21880
21881
21882
21883 /* Redraw (parts) of all windows in the window tree rooted at W that
21884 intersect R. R contains frame pixel coordinates. Value is
21885 non-zero if the exposure overwrites mouse-face. */
21886
21887 static int
21888 expose_window_tree (w, r)
21889 struct window *w;
21890 XRectangle *r;
21891 {
21892 struct frame *f = XFRAME (w->frame);
21893 int mouse_face_overwritten_p = 0;
21894
21895 while (w && !FRAME_GARBAGED_P (f))
21896 {
21897 if (!NILP (w->hchild))
21898 mouse_face_overwritten_p
21899 |= expose_window_tree (XWINDOW (w->hchild), r);
21900 else if (!NILP (w->vchild))
21901 mouse_face_overwritten_p
21902 |= expose_window_tree (XWINDOW (w->vchild), r);
21903 else
21904 mouse_face_overwritten_p |= expose_window (w, r);
21905
21906 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21907 }
21908
21909 return mouse_face_overwritten_p;
21910 }
21911
21912
21913 /* EXPORT:
21914 Redisplay an exposed area of frame F. X and Y are the upper-left
21915 corner of the exposed rectangle. W and H are width and height of
21916 the exposed area. All are pixel values. W or H zero means redraw
21917 the entire frame. */
21918
21919 void
21920 expose_frame (f, x, y, w, h)
21921 struct frame *f;
21922 int x, y, w, h;
21923 {
21924 XRectangle r;
21925 int mouse_face_overwritten_p = 0;
21926
21927 TRACE ((stderr, "expose_frame "));
21928
21929 /* No need to redraw if frame will be redrawn soon. */
21930 if (FRAME_GARBAGED_P (f))
21931 {
21932 TRACE ((stderr, " garbaged\n"));
21933 return;
21934 }
21935
21936 #ifdef HAVE_CARBON
21937 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21938 or deactivated here, for unknown reasons, activated scroll bars
21939 are shown in deactivated frames in some instances. */
21940 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21941 activate_scroll_bars (f);
21942 else
21943 deactivate_scroll_bars (f);
21944 #endif
21945
21946 /* If basic faces haven't been realized yet, there is no point in
21947 trying to redraw anything. This can happen when we get an expose
21948 event while Emacs is starting, e.g. by moving another window. */
21949 if (FRAME_FACE_CACHE (f) == NULL
21950 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21951 {
21952 TRACE ((stderr, " no faces\n"));
21953 return;
21954 }
21955
21956 if (w == 0 || h == 0)
21957 {
21958 r.x = r.y = 0;
21959 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21960 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21961 }
21962 else
21963 {
21964 r.x = x;
21965 r.y = y;
21966 r.width = w;
21967 r.height = h;
21968 }
21969
21970 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21971 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21972
21973 if (WINDOWP (f->tool_bar_window))
21974 mouse_face_overwritten_p
21975 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21976
21977 #ifdef HAVE_X_WINDOWS
21978 #ifndef MSDOS
21979 #ifndef USE_X_TOOLKIT
21980 if (WINDOWP (f->menu_bar_window))
21981 mouse_face_overwritten_p
21982 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21983 #endif /* not USE_X_TOOLKIT */
21984 #endif
21985 #endif
21986
21987 /* Some window managers support a focus-follows-mouse style with
21988 delayed raising of frames. Imagine a partially obscured frame,
21989 and moving the mouse into partially obscured mouse-face on that
21990 frame. The visible part of the mouse-face will be highlighted,
21991 then the WM raises the obscured frame. With at least one WM, KDE
21992 2.1, Emacs is not getting any event for the raising of the frame
21993 (even tried with SubstructureRedirectMask), only Expose events.
21994 These expose events will draw text normally, i.e. not
21995 highlighted. Which means we must redo the highlight here.
21996 Subsume it under ``we love X''. --gerd 2001-08-15 */
21997 /* Included in Windows version because Windows most likely does not
21998 do the right thing if any third party tool offers
21999 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22000 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22001 {
22002 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22003 if (f == dpyinfo->mouse_face_mouse_frame)
22004 {
22005 int x = dpyinfo->mouse_face_mouse_x;
22006 int y = dpyinfo->mouse_face_mouse_y;
22007 clear_mouse_face (dpyinfo);
22008 note_mouse_highlight (f, x, y);
22009 }
22010 }
22011 }
22012
22013
22014 /* EXPORT:
22015 Determine the intersection of two rectangles R1 and R2. Return
22016 the intersection in *RESULT. Value is non-zero if RESULT is not
22017 empty. */
22018
22019 int
22020 x_intersect_rectangles (r1, r2, result)
22021 XRectangle *r1, *r2, *result;
22022 {
22023 XRectangle *left, *right;
22024 XRectangle *upper, *lower;
22025 int intersection_p = 0;
22026
22027 /* Rearrange so that R1 is the left-most rectangle. */
22028 if (r1->x < r2->x)
22029 left = r1, right = r2;
22030 else
22031 left = r2, right = r1;
22032
22033 /* X0 of the intersection is right.x0, if this is inside R1,
22034 otherwise there is no intersection. */
22035 if (right->x <= left->x + left->width)
22036 {
22037 result->x = right->x;
22038
22039 /* The right end of the intersection is the minimum of the
22040 the right ends of left and right. */
22041 result->width = (min (left->x + left->width, right->x + right->width)
22042 - result->x);
22043
22044 /* Same game for Y. */
22045 if (r1->y < r2->y)
22046 upper = r1, lower = r2;
22047 else
22048 upper = r2, lower = r1;
22049
22050 /* The upper end of the intersection is lower.y0, if this is inside
22051 of upper. Otherwise, there is no intersection. */
22052 if (lower->y <= upper->y + upper->height)
22053 {
22054 result->y = lower->y;
22055
22056 /* The lower end of the intersection is the minimum of the lower
22057 ends of upper and lower. */
22058 result->height = (min (lower->y + lower->height,
22059 upper->y + upper->height)
22060 - result->y);
22061 intersection_p = 1;
22062 }
22063 }
22064
22065 return intersection_p;
22066 }
22067
22068 #endif /* HAVE_WINDOW_SYSTEM */
22069
22070 \f
22071 /***********************************************************************
22072 Initialization
22073 ***********************************************************************/
22074
22075 void
22076 syms_of_xdisp ()
22077 {
22078 Vwith_echo_area_save_vector = Qnil;
22079 staticpro (&Vwith_echo_area_save_vector);
22080
22081 Vmessage_stack = Qnil;
22082 staticpro (&Vmessage_stack);
22083
22084 Qinhibit_redisplay = intern ("inhibit-redisplay");
22085 staticpro (&Qinhibit_redisplay);
22086
22087 message_dolog_marker1 = Fmake_marker ();
22088 staticpro (&message_dolog_marker1);
22089 message_dolog_marker2 = Fmake_marker ();
22090 staticpro (&message_dolog_marker2);
22091 message_dolog_marker3 = Fmake_marker ();
22092 staticpro (&message_dolog_marker3);
22093
22094 #if GLYPH_DEBUG
22095 defsubr (&Sdump_frame_glyph_matrix);
22096 defsubr (&Sdump_glyph_matrix);
22097 defsubr (&Sdump_glyph_row);
22098 defsubr (&Sdump_tool_bar_row);
22099 defsubr (&Strace_redisplay);
22100 defsubr (&Strace_to_stderr);
22101 #endif
22102 #ifdef HAVE_WINDOW_SYSTEM
22103 defsubr (&Stool_bar_lines_needed);
22104 defsubr (&Slookup_image_map);
22105 #endif
22106 defsubr (&Sformat_mode_line);
22107
22108 staticpro (&Qmenu_bar_update_hook);
22109 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22110
22111 staticpro (&Qoverriding_terminal_local_map);
22112 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22113
22114 staticpro (&Qoverriding_local_map);
22115 Qoverriding_local_map = intern ("overriding-local-map");
22116
22117 staticpro (&Qwindow_scroll_functions);
22118 Qwindow_scroll_functions = intern ("window-scroll-functions");
22119
22120 staticpro (&Qredisplay_end_trigger_functions);
22121 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22122
22123 staticpro (&Qinhibit_point_motion_hooks);
22124 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22125
22126 QCdata = intern (":data");
22127 staticpro (&QCdata);
22128 Qdisplay = intern ("display");
22129 staticpro (&Qdisplay);
22130 Qspace_width = intern ("space-width");
22131 staticpro (&Qspace_width);
22132 Qraise = intern ("raise");
22133 staticpro (&Qraise);
22134 Qslice = intern ("slice");
22135 staticpro (&Qslice);
22136 Qspace = intern ("space");
22137 staticpro (&Qspace);
22138 Qmargin = intern ("margin");
22139 staticpro (&Qmargin);
22140 Qpointer = intern ("pointer");
22141 staticpro (&Qpointer);
22142 Qleft_margin = intern ("left-margin");
22143 staticpro (&Qleft_margin);
22144 Qright_margin = intern ("right-margin");
22145 staticpro (&Qright_margin);
22146 Qcenter = intern ("center");
22147 staticpro (&Qcenter);
22148 Qline_height = intern ("line-height");
22149 staticpro (&Qline_height);
22150 Qtotal = intern ("total");
22151 staticpro (&Qtotal);
22152 QCalign_to = intern (":align-to");
22153 staticpro (&QCalign_to);
22154 QCrelative_width = intern (":relative-width");
22155 staticpro (&QCrelative_width);
22156 QCrelative_height = intern (":relative-height");
22157 staticpro (&QCrelative_height);
22158 QCeval = intern (":eval");
22159 staticpro (&QCeval);
22160 QCpropertize = intern (":propertize");
22161 staticpro (&QCpropertize);
22162 QCfile = intern (":file");
22163 staticpro (&QCfile);
22164 Qfontified = intern ("fontified");
22165 staticpro (&Qfontified);
22166 Qfontification_functions = intern ("fontification-functions");
22167 staticpro (&Qfontification_functions);
22168 Qtrailing_whitespace = intern ("trailing-whitespace");
22169 staticpro (&Qtrailing_whitespace);
22170 Qimage = intern ("image");
22171 staticpro (&Qimage);
22172 QCmap = intern (":map");
22173 staticpro (&QCmap);
22174 QCpointer = intern (":pointer");
22175 staticpro (&QCpointer);
22176 Qrect = intern ("rect");
22177 staticpro (&Qrect);
22178 Qcircle = intern ("circle");
22179 staticpro (&Qcircle);
22180 Qpoly = intern ("poly");
22181 staticpro (&Qpoly);
22182 Qmessage_truncate_lines = intern ("message-truncate-lines");
22183 staticpro (&Qmessage_truncate_lines);
22184 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22185 staticpro (&Qcursor_in_non_selected_windows);
22186 Qgrow_only = intern ("grow-only");
22187 staticpro (&Qgrow_only);
22188 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22189 staticpro (&Qinhibit_menubar_update);
22190 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22191 staticpro (&Qinhibit_eval_during_redisplay);
22192 Qposition = intern ("position");
22193 staticpro (&Qposition);
22194 Qbuffer_position = intern ("buffer-position");
22195 staticpro (&Qbuffer_position);
22196 Qobject = intern ("object");
22197 staticpro (&Qobject);
22198 Qbar = intern ("bar");
22199 staticpro (&Qbar);
22200 Qhbar = intern ("hbar");
22201 staticpro (&Qhbar);
22202 Qbox = intern ("box");
22203 staticpro (&Qbox);
22204 Qhollow = intern ("hollow");
22205 staticpro (&Qhollow);
22206 Qhand = intern ("hand");
22207 staticpro (&Qhand);
22208 Qarrow = intern ("arrow");
22209 staticpro (&Qarrow);
22210 Qtext = intern ("text");
22211 staticpro (&Qtext);
22212 Qrisky_local_variable = intern ("risky-local-variable");
22213 staticpro (&Qrisky_local_variable);
22214 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22215 staticpro (&Qinhibit_free_realized_faces);
22216
22217 list_of_error = Fcons (Fcons (intern ("error"),
22218 Fcons (intern ("void-variable"), Qnil)),
22219 Qnil);
22220 staticpro (&list_of_error);
22221
22222 Qlast_arrow_position = intern ("last-arrow-position");
22223 staticpro (&Qlast_arrow_position);
22224 Qlast_arrow_string = intern ("last-arrow-string");
22225 staticpro (&Qlast_arrow_string);
22226
22227 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22228 staticpro (&Qoverlay_arrow_string);
22229 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22230 staticpro (&Qoverlay_arrow_bitmap);
22231
22232 echo_buffer[0] = echo_buffer[1] = Qnil;
22233 staticpro (&echo_buffer[0]);
22234 staticpro (&echo_buffer[1]);
22235
22236 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22237 staticpro (&echo_area_buffer[0]);
22238 staticpro (&echo_area_buffer[1]);
22239
22240 Vmessages_buffer_name = build_string ("*Messages*");
22241 staticpro (&Vmessages_buffer_name);
22242
22243 mode_line_proptrans_alist = Qnil;
22244 staticpro (&mode_line_proptrans_alist);
22245
22246 mode_line_string_list = Qnil;
22247 staticpro (&mode_line_string_list);
22248
22249 help_echo_string = Qnil;
22250 staticpro (&help_echo_string);
22251 help_echo_object = Qnil;
22252 staticpro (&help_echo_object);
22253 help_echo_window = Qnil;
22254 staticpro (&help_echo_window);
22255 previous_help_echo_string = Qnil;
22256 staticpro (&previous_help_echo_string);
22257 help_echo_pos = -1;
22258
22259 #ifdef HAVE_WINDOW_SYSTEM
22260 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22261 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22262 For example, if a block cursor is over a tab, it will be drawn as
22263 wide as that tab on the display. */);
22264 x_stretch_cursor_p = 0;
22265 #endif
22266
22267 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22268 doc: /* *Non-nil means highlight trailing whitespace.
22269 The face used for trailing whitespace is `trailing-whitespace'. */);
22270 Vshow_trailing_whitespace = Qnil;
22271
22272 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22273 doc: /* *The pointer shape to show in void text areas.
22274 Nil means to show the text pointer. Other options are `arrow', `text',
22275 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22276 Vvoid_text_area_pointer = Qarrow;
22277
22278 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22279 doc: /* Non-nil means don't actually do any redisplay.
22280 This is used for internal purposes. */);
22281 Vinhibit_redisplay = Qnil;
22282
22283 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22284 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22285 Vglobal_mode_string = Qnil;
22286
22287 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22288 doc: /* Marker for where to display an arrow on top of the buffer text.
22289 This must be the beginning of a line in order to work.
22290 See also `overlay-arrow-string'. */);
22291 Voverlay_arrow_position = Qnil;
22292
22293 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22294 doc: /* String to display as an arrow in non-window frames.
22295 See also `overlay-arrow-position'. */);
22296 Voverlay_arrow_string = Qnil;
22297
22298 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22299 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22300 The symbols on this list are examined during redisplay to determine
22301 where to display overlay arrows. */);
22302 Voverlay_arrow_variable_list
22303 = Fcons (intern ("overlay-arrow-position"), Qnil);
22304
22305 DEFVAR_INT ("scroll-step", &scroll_step,
22306 doc: /* *The number of lines to try scrolling a window by when point moves out.
22307 If that fails to bring point back on frame, point is centered instead.
22308 If this is zero, point is always centered after it moves off frame.
22309 If you want scrolling to always be a line at a time, you should set
22310 `scroll-conservatively' to a large value rather than set this to 1. */);
22311
22312 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22313 doc: /* *Scroll up to this many lines, to bring point back on screen.
22314 A value of zero means to scroll the text to center point vertically
22315 in the window. */);
22316 scroll_conservatively = 0;
22317
22318 DEFVAR_INT ("scroll-margin", &scroll_margin,
22319 doc: /* *Number of lines of margin at the top and bottom of a window.
22320 Recenter the window whenever point gets within this many lines
22321 of the top or bottom of the window. */);
22322 scroll_margin = 0;
22323
22324 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22325 doc: /* Pixels per inch on current display.
22326 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22327 Vdisplay_pixels_per_inch = make_float (72.0);
22328
22329 #if GLYPH_DEBUG
22330 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22331 #endif
22332
22333 DEFVAR_BOOL ("truncate-partial-width-windows",
22334 &truncate_partial_width_windows,
22335 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22336 truncate_partial_width_windows = 1;
22337
22338 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22339 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22340 Any other value means to use the appropriate face, `mode-line',
22341 `header-line', or `menu' respectively. */);
22342 mode_line_inverse_video = 1;
22343
22344 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22345 doc: /* *Maximum buffer size for which line number should be displayed.
22346 If the buffer is bigger than this, the line number does not appear
22347 in the mode line. A value of nil means no limit. */);
22348 Vline_number_display_limit = Qnil;
22349
22350 DEFVAR_INT ("line-number-display-limit-width",
22351 &line_number_display_limit_width,
22352 doc: /* *Maximum line width (in characters) for line number display.
22353 If the average length of the lines near point is bigger than this, then the
22354 line number may be omitted from the mode line. */);
22355 line_number_display_limit_width = 200;
22356
22357 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22358 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22359 highlight_nonselected_windows = 0;
22360
22361 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22362 doc: /* Non-nil if more than one frame is visible on this display.
22363 Minibuffer-only frames don't count, but iconified frames do.
22364 This variable is not guaranteed to be accurate except while processing
22365 `frame-title-format' and `icon-title-format'. */);
22366
22367 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22368 doc: /* Template for displaying the title bar of visible frames.
22369 \(Assuming the window manager supports this feature.)
22370 This variable has the same structure as `mode-line-format' (which see),
22371 and is used only on frames for which no explicit name has been set
22372 \(see `modify-frame-parameters'). */);
22373
22374 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22375 doc: /* Template for displaying the title bar of an iconified frame.
22376 \(Assuming the window manager supports this feature.)
22377 This variable has the same structure as `mode-line-format' (which see),
22378 and is used only on frames for which no explicit name has been set
22379 \(see `modify-frame-parameters'). */);
22380 Vicon_title_format
22381 = Vframe_title_format
22382 = Fcons (intern ("multiple-frames"),
22383 Fcons (build_string ("%b"),
22384 Fcons (Fcons (empty_string,
22385 Fcons (intern ("invocation-name"),
22386 Fcons (build_string ("@"),
22387 Fcons (intern ("system-name"),
22388 Qnil)))),
22389 Qnil)));
22390
22391 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22392 doc: /* Maximum number of lines to keep in the message log buffer.
22393 If nil, disable message logging. If t, log messages but don't truncate
22394 the buffer when it becomes large. */);
22395 Vmessage_log_max = make_number (50);
22396
22397 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22398 doc: /* Functions called before redisplay, if window sizes have changed.
22399 The value should be a list of functions that take one argument.
22400 Just before redisplay, for each frame, if any of its windows have changed
22401 size since the last redisplay, or have been split or deleted,
22402 all the functions in the list are called, with the frame as argument. */);
22403 Vwindow_size_change_functions = Qnil;
22404
22405 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22406 doc: /* List of functions to call before redisplaying a window with scrolling.
22407 Each function is called with two arguments, the window
22408 and its new display-start position. Note that the value of `window-end'
22409 is not valid when these functions are called. */);
22410 Vwindow_scroll_functions = Qnil;
22411
22412 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22413 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22414 mouse_autoselect_window = 0;
22415
22416 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22417 doc: /* *Non-nil means automatically resize tool-bars.
22418 This increases a tool-bar's height if not all tool-bar items are visible.
22419 It decreases a tool-bar's height when it would display blank lines
22420 otherwise. */);
22421 auto_resize_tool_bars_p = 1;
22422
22423 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22424 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22425 auto_raise_tool_bar_buttons_p = 1;
22426
22427 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22428 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22429 make_cursor_line_fully_visible_p = 1;
22430
22431 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22432 doc: /* *Margin around tool-bar buttons in pixels.
22433 If an integer, use that for both horizontal and vertical margins.
22434 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22435 HORZ specifying the horizontal margin, and VERT specifying the
22436 vertical margin. */);
22437 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22438
22439 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22440 doc: /* *Relief thickness of tool-bar buttons. */);
22441 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22442
22443 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22444 doc: /* List of functions to call to fontify regions of text.
22445 Each function is called with one argument POS. Functions must
22446 fontify a region starting at POS in the current buffer, and give
22447 fontified regions the property `fontified'. */);
22448 Vfontification_functions = Qnil;
22449 Fmake_variable_buffer_local (Qfontification_functions);
22450
22451 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22452 &unibyte_display_via_language_environment,
22453 doc: /* *Non-nil means display unibyte text according to language environment.
22454 Specifically this means that unibyte non-ASCII characters
22455 are displayed by converting them to the equivalent multibyte characters
22456 according to the current language environment. As a result, they are
22457 displayed according to the current fontset. */);
22458 unibyte_display_via_language_environment = 0;
22459
22460 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22461 doc: /* *Maximum height for resizing mini-windows.
22462 If a float, it specifies a fraction of the mini-window frame's height.
22463 If an integer, it specifies a number of lines. */);
22464 Vmax_mini_window_height = make_float (0.25);
22465
22466 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22467 doc: /* *How to resize mini-windows.
22468 A value of nil means don't automatically resize mini-windows.
22469 A value of t means resize them to fit the text displayed in them.
22470 A value of `grow-only', the default, means let mini-windows grow
22471 only, until their display becomes empty, at which point the windows
22472 go back to their normal size. */);
22473 Vresize_mini_windows = Qgrow_only;
22474
22475 DEFVAR_LISP ("cursor-in-non-selected-windows",
22476 &Vcursor_in_non_selected_windows,
22477 doc: /* *Cursor type to display in non-selected windows.
22478 t means to use hollow box cursor. See `cursor-type' for other values. */);
22479 Vcursor_in_non_selected_windows = Qt;
22480
22481 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22482 doc: /* Alist specifying how to blink the cursor off.
22483 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22484 `cursor-type' frame-parameter or variable equals ON-STATE,
22485 comparing using `equal', Emacs uses OFF-STATE to specify
22486 how to blink it off. */);
22487 Vblink_cursor_alist = Qnil;
22488
22489 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22490 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22491 automatic_hscrolling_p = 1;
22492
22493 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22494 doc: /* *How many columns away from the window edge point is allowed to get
22495 before automatic hscrolling will horizontally scroll the window. */);
22496 hscroll_margin = 5;
22497
22498 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22499 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22500 When point is less than `automatic-hscroll-margin' columns from the window
22501 edge, automatic hscrolling will scroll the window by the amount of columns
22502 determined by this variable. If its value is a positive integer, scroll that
22503 many columns. If it's a positive floating-point number, it specifies the
22504 fraction of the window's width to scroll. If it's nil or zero, point will be
22505 centered horizontally after the scroll. Any other value, including negative
22506 numbers, are treated as if the value were zero.
22507
22508 Automatic hscrolling always moves point outside the scroll margin, so if
22509 point was more than scroll step columns inside the margin, the window will
22510 scroll more than the value given by the scroll step.
22511
22512 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22513 and `scroll-right' overrides this variable's effect. */);
22514 Vhscroll_step = make_number (0);
22515
22516 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22517 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22518 Bind this around calls to `message' to let it take effect. */);
22519 message_truncate_lines = 0;
22520
22521 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22522 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22523 Can be used to update submenus whose contents should vary. */);
22524 Vmenu_bar_update_hook = Qnil;
22525
22526 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22527 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22528 inhibit_menubar_update = 0;
22529
22530 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22531 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22532 inhibit_eval_during_redisplay = 0;
22533
22534 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22535 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22536 inhibit_free_realized_faces = 0;
22537
22538 #if GLYPH_DEBUG
22539 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22540 doc: /* Inhibit try_window_id display optimization. */);
22541 inhibit_try_window_id = 0;
22542
22543 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22544 doc: /* Inhibit try_window_reusing display optimization. */);
22545 inhibit_try_window_reusing = 0;
22546
22547 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22548 doc: /* Inhibit try_cursor_movement display optimization. */);
22549 inhibit_try_cursor_movement = 0;
22550 #endif /* GLYPH_DEBUG */
22551 }
22552
22553
22554 /* Initialize this module when Emacs starts. */
22555
22556 void
22557 init_xdisp ()
22558 {
22559 Lisp_Object root_window;
22560 struct window *mini_w;
22561
22562 current_header_line_height = current_mode_line_height = -1;
22563
22564 CHARPOS (this_line_start_pos) = 0;
22565
22566 mini_w = XWINDOW (minibuf_window);
22567 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22568
22569 if (!noninteractive)
22570 {
22571 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22572 int i;
22573
22574 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22575 set_window_height (root_window,
22576 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22577 0);
22578 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22579 set_window_height (minibuf_window, 1, 0);
22580
22581 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22582 mini_w->total_cols = make_number (FRAME_COLS (f));
22583
22584 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22585 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22586 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22587
22588 /* The default ellipsis glyphs `...'. */
22589 for (i = 0; i < 3; ++i)
22590 default_invis_vector[i] = make_number ('.');
22591 }
22592
22593 {
22594 /* Allocate the buffer for frame titles.
22595 Also used for `format-mode-line'. */
22596 int size = 100;
22597 frame_title_buf = (char *) xmalloc (size);
22598 frame_title_buf_end = frame_title_buf + size;
22599 frame_title_ptr = NULL;
22600 }
22601
22602 help_echo_showing_p = 0;
22603 }
22604
22605
22606 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22607 (do not change this comment) */