Merged in changes from CVS trunk.
[bpt/emacs.git] / 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 /* Non-zero while redisplay_internal is in progress. */
777
778 int redisplaying_p;
779
780 /* Non-zero means don't free realized faces. Bound while freeing
781 realized faces is dangerous because glyph matrices might still
782 reference them. */
783
784 int inhibit_free_realized_faces;
785 Lisp_Object Qinhibit_free_realized_faces;
786
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
789
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 int help_echo_pos;
794
795 /* Temporary variable for XTread_socket. */
796
797 Lisp_Object previous_help_echo_string;
798
799 /* Null glyph slice */
800
801 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
802
803 \f
804 /* Function prototypes. */
805
806 static void setup_for_ellipsis P_ ((struct it *));
807 static void mark_window_display_accurate_1 P_ ((struct window *, int));
808 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
809 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
810 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
811 static int redisplay_mode_lines P_ ((Lisp_Object, int));
812 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
813
814 #if 0
815 static int invisible_text_between_p P_ ((struct it *, int, int));
816 #endif
817
818 static int next_element_from_ellipsis P_ ((struct it *));
819 static void pint2str P_ ((char *, int, int));
820 static void pint2hrstr P_ ((char *, int, int));
821 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
822 struct text_pos));
823 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
824 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
825 static void store_frame_title_char P_ ((char));
826 static int store_frame_title P_ ((const unsigned char *, int, int));
827 static void x_consider_frame_title P_ ((Lisp_Object));
828 static void handle_stop P_ ((struct it *));
829 static int tool_bar_lines_needed P_ ((struct frame *));
830 static int single_display_prop_intangible_p P_ ((Lisp_Object));
831 static void ensure_echo_area_buffers P_ ((void));
832 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
833 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
834 static int with_echo_area_buffer P_ ((struct window *, int,
835 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
836 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static void clear_garbaged_frames P_ ((void));
838 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
839 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static int display_echo_area P_ ((struct window *));
842 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
843 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
845 static int string_char_and_length P_ ((const unsigned char *, int, int *));
846 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
847 struct text_pos));
848 static int compute_window_start_on_continuation_line P_ ((struct window *));
849 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
850 static void insert_left_trunc_glyphs P_ ((struct it *));
851 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
852 Lisp_Object));
853 static void extend_face_to_end_of_line P_ ((struct it *));
854 static int append_space_for_newline P_ ((struct it *, int));
855 static int make_cursor_line_fully_visible P_ ((struct window *, int));
856 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
857 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
858 static int trailing_whitespace_p P_ ((int));
859 static int message_log_check_duplicate P_ ((int, int, int, int));
860 static void push_it P_ ((struct it *));
861 static void pop_it P_ ((struct it *));
862 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
863 static void select_frame_for_redisplay P_ ((Lisp_Object));
864 static void redisplay_internal P_ ((int));
865 static int echo_area_display P_ ((int));
866 static void redisplay_windows P_ ((Lisp_Object));
867 static void redisplay_window P_ ((Lisp_Object, int));
868 static Lisp_Object redisplay_window_error ();
869 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
870 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
871 static void update_menu_bar P_ ((struct frame *, int));
872 static int try_window_reusing_current_matrix P_ ((struct window *));
873 static int try_window_id P_ ((struct window *));
874 static int display_line P_ ((struct it *));
875 static int display_mode_lines P_ ((struct window *));
876 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
877 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
878 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
879 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
880 static void display_menu_bar P_ ((struct window *));
881 static int display_count_lines P_ ((int, int, int, int, int *));
882 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
883 int, int, struct it *, int, int, int, int));
884 static void compute_line_metrics P_ ((struct it *));
885 static void run_redisplay_end_trigger_hook P_ ((struct it *));
886 static int get_overlay_strings P_ ((struct it *, int));
887 static void next_overlay_string P_ ((struct it *));
888 static void reseat P_ ((struct it *, struct text_pos, int));
889 static void reseat_1 P_ ((struct it *, struct text_pos, int));
890 static void back_to_previous_visible_line_start P_ ((struct it *));
891 void reseat_at_previous_visible_line_start P_ ((struct it *));
892 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
893 static int next_element_from_display_vector P_ ((struct it *));
894 static int next_element_from_string P_ ((struct it *));
895 static int next_element_from_c_string P_ ((struct it *));
896 static int next_element_from_buffer P_ ((struct it *));
897 static int next_element_from_composition P_ ((struct it *));
898 static int next_element_from_image P_ ((struct it *));
899 static int next_element_from_stretch P_ ((struct it *));
900 static void load_overlay_strings P_ ((struct it *, int));
901 static int init_from_display_pos P_ ((struct it *, struct window *,
902 struct display_pos *));
903 static void reseat_to_string P_ ((struct it *, unsigned char *,
904 Lisp_Object, int, int, int, int));
905 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
906 int, int, int));
907 void move_it_vertically_backward P_ ((struct it *, int));
908 static void init_to_row_start P_ ((struct it *, struct window *,
909 struct glyph_row *));
910 static int init_to_row_end P_ ((struct it *, struct window *,
911 struct glyph_row *));
912 static void back_to_previous_line_start P_ ((struct it *));
913 static int forward_to_next_line_start P_ ((struct it *, int *));
914 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
915 Lisp_Object, int));
916 static struct text_pos string_pos P_ ((int, Lisp_Object));
917 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
918 static int number_of_chars P_ ((unsigned char *, int));
919 static void compute_stop_pos P_ ((struct it *));
920 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
921 Lisp_Object));
922 static int face_before_or_after_it_pos P_ ((struct it *, int));
923 static int next_overlay_change P_ ((int));
924 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
925 Lisp_Object, struct text_pos *,
926 int));
927 static int underlying_face_id P_ ((struct it *));
928 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
929 struct window *));
930
931 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
932 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
933
934 #ifdef HAVE_WINDOW_SYSTEM
935
936 static void update_tool_bar P_ ((struct frame *, int));
937 static void build_desired_tool_bar_string P_ ((struct frame *f));
938 static int redisplay_tool_bar P_ ((struct frame *));
939 static void display_tool_bar_line P_ ((struct it *));
940 static void notice_overwritten_cursor P_ ((struct window *,
941 enum glyph_row_area,
942 int, int, int, int));
943
944
945
946 #endif /* HAVE_WINDOW_SYSTEM */
947
948 \f
949 /***********************************************************************
950 Window display dimensions
951 ***********************************************************************/
952
953 /* Return the bottom boundary y-position for text lines in window W.
954 This is the first y position at which a line cannot start.
955 It is relative to the top of the window.
956
957 This is the height of W minus the height of a mode line, if any. */
958
959 INLINE int
960 window_text_bottom_y (w)
961 struct window *w;
962 {
963 int height = WINDOW_TOTAL_HEIGHT (w);
964
965 if (WINDOW_WANTS_MODELINE_P (w))
966 height -= CURRENT_MODE_LINE_HEIGHT (w);
967 return height;
968 }
969
970 /* Return the pixel width of display area AREA of window W. AREA < 0
971 means return the total width of W, not including fringes to
972 the left and right of the window. */
973
974 INLINE int
975 window_box_width (w, area)
976 struct window *w;
977 int area;
978 {
979 int cols = XFASTINT (w->total_cols);
980 int pixels = 0;
981
982 if (!w->pseudo_window_p)
983 {
984 cols -= WINDOW_SCROLL_BAR_COLS (w);
985
986 if (area == TEXT_AREA)
987 {
988 if (INTEGERP (w->left_margin_cols))
989 cols -= XFASTINT (w->left_margin_cols);
990 if (INTEGERP (w->right_margin_cols))
991 cols -= XFASTINT (w->right_margin_cols);
992 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
993 }
994 else if (area == LEFT_MARGIN_AREA)
995 {
996 cols = (INTEGERP (w->left_margin_cols)
997 ? XFASTINT (w->left_margin_cols) : 0);
998 pixels = 0;
999 }
1000 else if (area == RIGHT_MARGIN_AREA)
1001 {
1002 cols = (INTEGERP (w->right_margin_cols)
1003 ? XFASTINT (w->right_margin_cols) : 0);
1004 pixels = 0;
1005 }
1006 }
1007
1008 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1009 }
1010
1011
1012 /* Return the pixel height of the display area of window W, not
1013 including mode lines of W, if any. */
1014
1015 INLINE int
1016 window_box_height (w)
1017 struct window *w;
1018 {
1019 struct frame *f = XFRAME (w->frame);
1020 int height = WINDOW_TOTAL_HEIGHT (w);
1021
1022 xassert (height >= 0);
1023
1024 /* Note: the code below that determines the mode-line/header-line
1025 height is essentially the same as that contained in the macro
1026 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1027 the appropriate glyph row has its `mode_line_p' flag set,
1028 and if it doesn't, uses estimate_mode_line_height instead. */
1029
1030 if (WINDOW_WANTS_MODELINE_P (w))
1031 {
1032 struct glyph_row *ml_row
1033 = (w->current_matrix && w->current_matrix->rows
1034 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1035 : 0);
1036 if (ml_row && ml_row->mode_line_p)
1037 height -= ml_row->height;
1038 else
1039 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1040 }
1041
1042 if (WINDOW_WANTS_HEADER_LINE_P (w))
1043 {
1044 struct glyph_row *hl_row
1045 = (w->current_matrix && w->current_matrix->rows
1046 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1047 : 0);
1048 if (hl_row && hl_row->mode_line_p)
1049 height -= hl_row->height;
1050 else
1051 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1052 }
1053
1054 /* With a very small font and a mode-line that's taller than
1055 default, we might end up with a negative height. */
1056 return max (0, height);
1057 }
1058
1059 /* Return the window-relative coordinate of the left edge of display
1060 area AREA of window W. AREA < 0 means return the left edge of the
1061 whole window, to the right of the left fringe of W. */
1062
1063 INLINE int
1064 window_box_left_offset (w, area)
1065 struct window *w;
1066 int area;
1067 {
1068 int x;
1069
1070 if (w->pseudo_window_p)
1071 return 0;
1072
1073 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1074
1075 if (area == TEXT_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA));
1078 else if (area == RIGHT_MARGIN_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA)
1081 + window_box_width (w, TEXT_AREA)
1082 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1083 ? 0
1084 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1085 else if (area == LEFT_MARGIN_AREA
1086 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1087 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1088
1089 return x;
1090 }
1091
1092
1093 /* Return the window-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the left of the right fringe of W. */
1096
1097 INLINE int
1098 window_box_right_offset (w, area)
1099 struct window *w;
1100 int area;
1101 {
1102 return window_box_left_offset (w, area) + window_box_width (w, area);
1103 }
1104
1105 /* Return the frame-relative coordinate of the left edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the right of the left fringe of W. */
1108
1109 INLINE int
1110 window_box_left (w, area)
1111 struct window *w;
1112 int area;
1113 {
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1116
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1119
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 INLINE int
1132 window_box_right (w, area)
1133 struct window *w;
1134 int area;
1135 {
1136 return window_box_left (w, area) + window_box_width (w, area);
1137 }
1138
1139 /* Get the bounding box of the display area AREA of window W, without
1140 mode lines, in frame-relative coordinates. AREA < 0 means the
1141 whole window, not including the left and right fringes of
1142 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1143 coordinates of the upper-left corner of the box. Return in
1144 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1145
1146 INLINE void
1147 window_box (w, area, box_x, box_y, box_width, box_height)
1148 struct window *w;
1149 int area;
1150 int *box_x, *box_y, *box_width, *box_height;
1151 {
1152 if (box_width)
1153 *box_width = window_box_width (w, area);
1154 if (box_height)
1155 *box_height = window_box_height (w);
1156 if (box_x)
1157 *box_x = window_box_left (w, area);
1158 if (box_y)
1159 {
1160 *box_y = WINDOW_TOP_EDGE_Y (w);
1161 if (WINDOW_WANTS_HEADER_LINE_P (w))
1162 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1163 }
1164 }
1165
1166
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines. AREA < 0 means the whole window, not including the
1169 left and right fringe of the window. Return in *TOP_LEFT_X
1170 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1171 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1172 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1173 box. */
1174
1175 INLINE void
1176 window_box_edges (w, area, top_left_x, top_left_y,
1177 bottom_right_x, bottom_right_y)
1178 struct window *w;
1179 int area;
1180 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1181 {
1182 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1183 bottom_right_y);
1184 *bottom_right_x += *top_left_x;
1185 *bottom_right_y += *top_left_y;
1186 }
1187
1188
1189 \f
1190 /***********************************************************************
1191 Utilities
1192 ***********************************************************************/
1193
1194 /* Return the bottom y-position of the line the iterator IT is in.
1195 This can modify IT's settings. */
1196
1197 int
1198 line_bottom_y (it)
1199 struct it *it;
1200 {
1201 int line_height = it->max_ascent + it->max_descent;
1202 int line_top_y = it->current_y;
1203
1204 if (line_height == 0)
1205 {
1206 if (last_height)
1207 line_height = last_height;
1208 else if (IT_CHARPOS (*it) < ZV)
1209 {
1210 move_it_by_lines (it, 1, 1);
1211 line_height = (it->max_ascent || it->max_descent
1212 ? it->max_ascent + it->max_descent
1213 : last_height);
1214 }
1215 else
1216 {
1217 struct glyph_row *row = it->glyph_row;
1218
1219 /* Use the default character height. */
1220 it->glyph_row = NULL;
1221 it->what = IT_CHARACTER;
1222 it->c = ' ';
1223 it->len = 1;
1224 PRODUCE_GLYPHS (it);
1225 line_height = it->ascent + it->descent;
1226 it->glyph_row = row;
1227 }
1228 }
1229
1230 return line_top_y + line_height;
1231 }
1232
1233
1234 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1235 1 if POS is visible and the line containing POS is fully visible.
1236 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1237 and header-lines heights. */
1238
1239 int
1240 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1241 struct window *w;
1242 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1243 {
1244 struct it it;
1245 struct text_pos top;
1246 int visible_p;
1247 struct buffer *old_buffer = NULL;
1248
1249 if (XBUFFER (w->buffer) != current_buffer)
1250 {
1251 old_buffer = current_buffer;
1252 set_buffer_internal_1 (XBUFFER (w->buffer));
1253 }
1254
1255 *fully = visible_p = 0;
1256 SET_TEXT_POS_FROM_MARKER (top, w->start);
1257
1258 /* Compute exact mode line heights, if requested. */
1259 if (exact_mode_line_heights_p)
1260 {
1261 if (WINDOW_WANTS_MODELINE_P (w))
1262 current_mode_line_height
1263 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1264 current_buffer->mode_line_format);
1265
1266 if (WINDOW_WANTS_HEADER_LINE_P (w))
1267 current_header_line_height
1268 = display_mode_line (w, HEADER_LINE_FACE_ID,
1269 current_buffer->header_line_format);
1270 }
1271
1272 start_display (&it, w, top);
1273 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1274 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1275
1276 /* Note that we may overshoot because of invisible text. */
1277 if (IT_CHARPOS (it) >= charpos)
1278 {
1279 int top_y = it.current_y;
1280 int bottom_y = line_bottom_y (&it);
1281 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1282
1283 if (top_y < window_top_y)
1284 visible_p = bottom_y > window_top_y;
1285 else if (top_y < it.last_visible_y)
1286 {
1287 visible_p = 1;
1288 *fully = bottom_y <= it.last_visible_y;
1289 }
1290 if (visible_p && x)
1291 {
1292 *x = it.current_x;
1293 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1294 }
1295 }
1296 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1297 {
1298 struct it it2;
1299
1300 it2 = it;
1301 move_it_by_lines (&it, 1, 0);
1302 if (charpos < IT_CHARPOS (it))
1303 {
1304 visible_p = 1;
1305 if (x)
1306 {
1307 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1308 *x = it2.current_x;
1309 *y = it2.current_y + it2.max_ascent - it2.ascent;
1310 }
1311 }
1312 }
1313
1314 if (old_buffer)
1315 set_buffer_internal_1 (old_buffer);
1316
1317 current_header_line_height = current_mode_line_height = -1;
1318
1319 return visible_p;
1320 }
1321
1322
1323 /* Return the next character from STR which is MAXLEN bytes long.
1324 Return in *LEN the length of the character. This is like
1325 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1326 we find one, we return a `?', but with the length of the invalid
1327 character. */
1328
1329 static INLINE int
1330 string_char_and_length (str, maxlen, len)
1331 const unsigned char *str;
1332 int maxlen, *len;
1333 {
1334 int c;
1335
1336 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1337 if (!CHAR_VALID_P (c, 1))
1338 /* We may not change the length here because other places in Emacs
1339 don't use this function, i.e. they silently accept invalid
1340 characters. */
1341 c = '?';
1342
1343 return c;
1344 }
1345
1346
1347
1348 /* Given a position POS containing a valid character and byte position
1349 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1350
1351 static struct text_pos
1352 string_pos_nchars_ahead (pos, string, nchars)
1353 struct text_pos pos;
1354 Lisp_Object string;
1355 int nchars;
1356 {
1357 xassert (STRINGP (string) && nchars >= 0);
1358
1359 if (STRING_MULTIBYTE (string))
1360 {
1361 int rest = SBYTES (string) - BYTEPOS (pos);
1362 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1363 int len;
1364
1365 while (nchars--)
1366 {
1367 string_char_and_length (p, rest, &len);
1368 p += len, rest -= len;
1369 xassert (rest >= 0);
1370 CHARPOS (pos) += 1;
1371 BYTEPOS (pos) += len;
1372 }
1373 }
1374 else
1375 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1376
1377 return pos;
1378 }
1379
1380
1381 /* Value is the text position, i.e. character and byte position,
1382 for character position CHARPOS in STRING. */
1383
1384 static INLINE struct text_pos
1385 string_pos (charpos, string)
1386 int charpos;
1387 Lisp_Object string;
1388 {
1389 struct text_pos pos;
1390 xassert (STRINGP (string));
1391 xassert (charpos >= 0);
1392 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1393 return pos;
1394 }
1395
1396
1397 /* Value is a text position, i.e. character and byte position, for
1398 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1399 means recognize multibyte characters. */
1400
1401 static struct text_pos
1402 c_string_pos (charpos, s, multibyte_p)
1403 int charpos;
1404 unsigned char *s;
1405 int multibyte_p;
1406 {
1407 struct text_pos pos;
1408
1409 xassert (s != NULL);
1410 xassert (charpos >= 0);
1411
1412 if (multibyte_p)
1413 {
1414 int rest = strlen (s), len;
1415
1416 SET_TEXT_POS (pos, 0, 0);
1417 while (charpos--)
1418 {
1419 string_char_and_length (s, rest, &len);
1420 s += len, rest -= len;
1421 xassert (rest >= 0);
1422 CHARPOS (pos) += 1;
1423 BYTEPOS (pos) += len;
1424 }
1425 }
1426 else
1427 SET_TEXT_POS (pos, charpos, charpos);
1428
1429 return pos;
1430 }
1431
1432
1433 /* Value is the number of characters in C string S. MULTIBYTE_P
1434 non-zero means recognize multibyte characters. */
1435
1436 static int
1437 number_of_chars (s, multibyte_p)
1438 unsigned char *s;
1439 int multibyte_p;
1440 {
1441 int nchars;
1442
1443 if (multibyte_p)
1444 {
1445 int rest = strlen (s), len;
1446 unsigned char *p = (unsigned char *) s;
1447
1448 for (nchars = 0; rest > 0; ++nchars)
1449 {
1450 string_char_and_length (p, rest, &len);
1451 rest -= len, p += len;
1452 }
1453 }
1454 else
1455 nchars = strlen (s);
1456
1457 return nchars;
1458 }
1459
1460
1461 /* Compute byte position NEWPOS->bytepos corresponding to
1462 NEWPOS->charpos. POS is a known position in string STRING.
1463 NEWPOS->charpos must be >= POS.charpos. */
1464
1465 static void
1466 compute_string_pos (newpos, pos, string)
1467 struct text_pos *newpos, pos;
1468 Lisp_Object string;
1469 {
1470 xassert (STRINGP (string));
1471 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1472
1473 if (STRING_MULTIBYTE (string))
1474 *newpos = string_pos_nchars_ahead (pos, string,
1475 CHARPOS (*newpos) - CHARPOS (pos));
1476 else
1477 BYTEPOS (*newpos) = CHARPOS (*newpos);
1478 }
1479
1480 /* EXPORT:
1481 Return an estimation of the pixel height of mode or top lines on
1482 frame F. FACE_ID specifies what line's height to estimate. */
1483
1484 int
1485 estimate_mode_line_height (f, face_id)
1486 struct frame *f;
1487 enum face_id face_id;
1488 {
1489 #ifdef HAVE_WINDOW_SYSTEM
1490 if (FRAME_WINDOW_P (f))
1491 {
1492 int height = FONT_HEIGHT (FRAME_FONT (f));
1493
1494 /* This function is called so early when Emacs starts that the face
1495 cache and mode line face are not yet initialized. */
1496 if (FRAME_FACE_CACHE (f))
1497 {
1498 struct face *face = FACE_FROM_ID (f, face_id);
1499 if (face)
1500 {
1501 if (face->font)
1502 height = FONT_HEIGHT (face->font);
1503 if (face->box_line_width > 0)
1504 height += 2 * face->box_line_width;
1505 }
1506 }
1507
1508 return height;
1509 }
1510 #endif
1511
1512 return 1;
1513 }
1514
1515 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1516 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1517 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1518 not force the value into range. */
1519
1520 void
1521 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1522 FRAME_PTR f;
1523 register int pix_x, pix_y;
1524 int *x, *y;
1525 NativeRectangle *bounds;
1526 int noclip;
1527 {
1528
1529 #ifdef HAVE_WINDOW_SYSTEM
1530 if (FRAME_WINDOW_P (f))
1531 {
1532 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1533 even for negative values. */
1534 if (pix_x < 0)
1535 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1536 if (pix_y < 0)
1537 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1538
1539 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1540 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1541
1542 if (bounds)
1543 STORE_NATIVE_RECT (*bounds,
1544 FRAME_COL_TO_PIXEL_X (f, pix_x),
1545 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1546 FRAME_COLUMN_WIDTH (f) - 1,
1547 FRAME_LINE_HEIGHT (f) - 1);
1548
1549 if (!noclip)
1550 {
1551 if (pix_x < 0)
1552 pix_x = 0;
1553 else if (pix_x > FRAME_TOTAL_COLS (f))
1554 pix_x = FRAME_TOTAL_COLS (f);
1555
1556 if (pix_y < 0)
1557 pix_y = 0;
1558 else if (pix_y > FRAME_LINES (f))
1559 pix_y = FRAME_LINES (f);
1560 }
1561 }
1562 #endif
1563
1564 *x = pix_x;
1565 *y = pix_y;
1566 }
1567
1568
1569 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1570 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1571 can't tell the positions because W's display is not up to date,
1572 return 0. */
1573
1574 int
1575 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1576 struct window *w;
1577 int hpos, vpos;
1578 int *frame_x, *frame_y;
1579 {
1580 #ifdef HAVE_WINDOW_SYSTEM
1581 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1582 {
1583 int success_p;
1584
1585 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1586 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1587
1588 if (display_completed)
1589 {
1590 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1591 struct glyph *glyph = row->glyphs[TEXT_AREA];
1592 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1593
1594 hpos = row->x;
1595 vpos = row->y;
1596 while (glyph < end)
1597 {
1598 hpos += glyph->pixel_width;
1599 ++glyph;
1600 }
1601
1602 /* If first glyph is partially visible, its first visible position is still 0. */
1603 if (hpos < 0)
1604 hpos = 0;
1605
1606 success_p = 1;
1607 }
1608 else
1609 {
1610 hpos = vpos = 0;
1611 success_p = 0;
1612 }
1613
1614 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1615 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1616 return success_p;
1617 }
1618 #endif
1619
1620 *frame_x = hpos;
1621 *frame_y = vpos;
1622 return 1;
1623 }
1624
1625
1626 #ifdef HAVE_WINDOW_SYSTEM
1627
1628 /* Find the glyph under window-relative coordinates X/Y in window W.
1629 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1630 strings. Return in *HPOS and *VPOS the row and column number of
1631 the glyph found. Return in *AREA the glyph area containing X.
1632 Value is a pointer to the glyph found or null if X/Y is not on
1633 text, or we can't tell because W's current matrix is not up to
1634 date. */
1635
1636 static struct glyph *
1637 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1638 struct window *w;
1639 int x, y;
1640 int *hpos, *vpos, *dx, *dy, *area;
1641 {
1642 struct glyph *glyph, *end;
1643 struct glyph_row *row = NULL;
1644 int x0, i;
1645
1646 /* Find row containing Y. Give up if some row is not enabled. */
1647 for (i = 0; i < w->current_matrix->nrows; ++i)
1648 {
1649 row = MATRIX_ROW (w->current_matrix, i);
1650 if (!row->enabled_p)
1651 return NULL;
1652 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1653 break;
1654 }
1655
1656 *vpos = i;
1657 *hpos = 0;
1658
1659 /* Give up if Y is not in the window. */
1660 if (i == w->current_matrix->nrows)
1661 return NULL;
1662
1663 /* Get the glyph area containing X. */
1664 if (w->pseudo_window_p)
1665 {
1666 *area = TEXT_AREA;
1667 x0 = 0;
1668 }
1669 else
1670 {
1671 if (x < window_box_left_offset (w, TEXT_AREA))
1672 {
1673 *area = LEFT_MARGIN_AREA;
1674 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1675 }
1676 else if (x < window_box_right_offset (w, TEXT_AREA))
1677 {
1678 *area = TEXT_AREA;
1679 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1680 }
1681 else
1682 {
1683 *area = RIGHT_MARGIN_AREA;
1684 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1685 }
1686 }
1687
1688 /* Find glyph containing X. */
1689 glyph = row->glyphs[*area];
1690 end = glyph + row->used[*area];
1691 x -= x0;
1692 while (glyph < end && x >= glyph->pixel_width)
1693 {
1694 x -= glyph->pixel_width;
1695 ++glyph;
1696 }
1697
1698 if (glyph == end)
1699 return NULL;
1700
1701 if (dx)
1702 {
1703 *dx = x;
1704 *dy = y - (row->y + row->ascent - glyph->ascent);
1705 }
1706
1707 *hpos = glyph - row->glyphs[*area];
1708 return glyph;
1709 }
1710
1711
1712 /* EXPORT:
1713 Convert frame-relative x/y to coordinates relative to window W.
1714 Takes pseudo-windows into account. */
1715
1716 void
1717 frame_to_window_pixel_xy (w, x, y)
1718 struct window *w;
1719 int *x, *y;
1720 {
1721 if (w->pseudo_window_p)
1722 {
1723 /* A pseudo-window is always full-width, and starts at the
1724 left edge of the frame, plus a frame border. */
1725 struct frame *f = XFRAME (w->frame);
1726 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1727 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1728 }
1729 else
1730 {
1731 *x -= WINDOW_LEFT_EDGE_X (w);
1732 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1733 }
1734 }
1735
1736 /* EXPORT:
1737 Return in *R the clipping rectangle for glyph string S. */
1738
1739 void
1740 get_glyph_string_clip_rect (s, nr)
1741 struct glyph_string *s;
1742 NativeRectangle *nr;
1743 {
1744 XRectangle r;
1745
1746 if (s->row->full_width_p)
1747 {
1748 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1749 r.x = WINDOW_LEFT_EDGE_X (s->w);
1750 r.width = WINDOW_TOTAL_WIDTH (s->w);
1751
1752 /* Unless displaying a mode or menu bar line, which are always
1753 fully visible, clip to the visible part of the row. */
1754 if (s->w->pseudo_window_p)
1755 r.height = s->row->visible_height;
1756 else
1757 r.height = s->height;
1758 }
1759 else
1760 {
1761 /* This is a text line that may be partially visible. */
1762 r.x = window_box_left (s->w, s->area);
1763 r.width = window_box_width (s->w, s->area);
1764 r.height = s->row->visible_height;
1765 }
1766
1767 /* If S draws overlapping rows, it's sufficient to use the top and
1768 bottom of the window for clipping because this glyph string
1769 intentionally draws over other lines. */
1770 if (s->for_overlaps_p)
1771 {
1772 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1773 r.height = window_text_bottom_y (s->w) - r.y;
1774 }
1775 else
1776 {
1777 /* Don't use S->y for clipping because it doesn't take partially
1778 visible lines into account. For example, it can be negative for
1779 partially visible lines at the top of a window. */
1780 if (!s->row->full_width_p
1781 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1782 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1783 else
1784 r.y = max (0, s->row->y);
1785
1786 /* If drawing a tool-bar window, draw it over the internal border
1787 at the top of the window. */
1788 if (WINDOWP (s->f->tool_bar_window)
1789 && s->w == XWINDOW (s->f->tool_bar_window))
1790 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1791 }
1792
1793 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1794
1795 /* If drawing the cursor, don't let glyph draw outside its
1796 advertised boundaries. Cleartype does this under some circumstances. */
1797 if (s->hl == DRAW_CURSOR)
1798 {
1799 struct glyph *glyph = s->first_glyph;
1800 int height;
1801
1802 if (s->x > r.x)
1803 {
1804 r.width -= s->x - r.x;
1805 r.x = s->x;
1806 }
1807 r.width = min (r.width, glyph->pixel_width);
1808
1809 /* Don't draw cursor glyph taller than our actual glyph. */
1810 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1811 if (height < r.height)
1812 {
1813 int max_y = r.y + r.height;
1814 r.y = min (max_y, s->ybase + glyph->descent - height);
1815 r.height = min (max_y - r.y, height);
1816 }
1817 }
1818
1819 #ifdef CONVERT_FROM_XRECT
1820 CONVERT_FROM_XRECT (r, *nr);
1821 #else
1822 *nr = r;
1823 #endif
1824 }
1825
1826 #endif /* HAVE_WINDOW_SYSTEM */
1827
1828 \f
1829 /***********************************************************************
1830 Lisp form evaluation
1831 ***********************************************************************/
1832
1833 /* Error handler for safe_eval and safe_call. */
1834
1835 static Lisp_Object
1836 safe_eval_handler (arg)
1837 Lisp_Object arg;
1838 {
1839 add_to_log ("Error during redisplay: %s", arg, Qnil);
1840 return Qnil;
1841 }
1842
1843
1844 /* Evaluate SEXPR and return the result, or nil if something went
1845 wrong. Prevent redisplay during the evaluation. */
1846
1847 Lisp_Object
1848 safe_eval (sexpr)
1849 Lisp_Object sexpr;
1850 {
1851 Lisp_Object val;
1852
1853 if (inhibit_eval_during_redisplay)
1854 val = Qnil;
1855 else
1856 {
1857 int count = SPECPDL_INDEX ();
1858 struct gcpro gcpro1;
1859
1860 GCPRO1 (sexpr);
1861 specbind (Qinhibit_redisplay, Qt);
1862 /* Use Qt to ensure debugger does not run,
1863 so there is no possibility of wanting to redisplay. */
1864 val = internal_condition_case_1 (Feval, sexpr, Qt,
1865 safe_eval_handler);
1866 UNGCPRO;
1867 val = unbind_to (count, val);
1868 }
1869
1870 return val;
1871 }
1872
1873
1874 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1875 Return the result, or nil if something went wrong. Prevent
1876 redisplay during the evaluation. */
1877
1878 Lisp_Object
1879 safe_call (nargs, args)
1880 int nargs;
1881 Lisp_Object *args;
1882 {
1883 Lisp_Object val;
1884
1885 if (inhibit_eval_during_redisplay)
1886 val = Qnil;
1887 else
1888 {
1889 int count = SPECPDL_INDEX ();
1890 struct gcpro gcpro1;
1891
1892 GCPRO1 (args[0]);
1893 gcpro1.nvars = nargs;
1894 specbind (Qinhibit_redisplay, Qt);
1895 /* Use Qt to ensure debugger does not run,
1896 so there is no possibility of wanting to redisplay. */
1897 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1898 safe_eval_handler);
1899 UNGCPRO;
1900 val = unbind_to (count, val);
1901 }
1902
1903 return val;
1904 }
1905
1906
1907 /* Call function FN with one argument ARG.
1908 Return the result, or nil if something went wrong. */
1909
1910 Lisp_Object
1911 safe_call1 (fn, arg)
1912 Lisp_Object fn, arg;
1913 {
1914 Lisp_Object args[2];
1915 args[0] = fn;
1916 args[1] = arg;
1917 return safe_call (2, args);
1918 }
1919
1920
1921 \f
1922 /***********************************************************************
1923 Debugging
1924 ***********************************************************************/
1925
1926 #if 0
1927
1928 /* Define CHECK_IT to perform sanity checks on iterators.
1929 This is for debugging. It is too slow to do unconditionally. */
1930
1931 static void
1932 check_it (it)
1933 struct it *it;
1934 {
1935 if (it->method == next_element_from_string)
1936 {
1937 xassert (STRINGP (it->string));
1938 xassert (IT_STRING_CHARPOS (*it) >= 0);
1939 }
1940 else
1941 {
1942 xassert (IT_STRING_CHARPOS (*it) < 0);
1943 if (it->method == next_element_from_buffer)
1944 {
1945 /* Check that character and byte positions agree. */
1946 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1947 }
1948 }
1949
1950 if (it->dpvec)
1951 xassert (it->current.dpvec_index >= 0);
1952 else
1953 xassert (it->current.dpvec_index < 0);
1954 }
1955
1956 #define CHECK_IT(IT) check_it ((IT))
1957
1958 #else /* not 0 */
1959
1960 #define CHECK_IT(IT) (void) 0
1961
1962 #endif /* not 0 */
1963
1964
1965 #if GLYPH_DEBUG
1966
1967 /* Check that the window end of window W is what we expect it
1968 to be---the last row in the current matrix displaying text. */
1969
1970 static void
1971 check_window_end (w)
1972 struct window *w;
1973 {
1974 if (!MINI_WINDOW_P (w)
1975 && !NILP (w->window_end_valid))
1976 {
1977 struct glyph_row *row;
1978 xassert ((row = MATRIX_ROW (w->current_matrix,
1979 XFASTINT (w->window_end_vpos)),
1980 !row->enabled_p
1981 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1982 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1983 }
1984 }
1985
1986 #define CHECK_WINDOW_END(W) check_window_end ((W))
1987
1988 #else /* not GLYPH_DEBUG */
1989
1990 #define CHECK_WINDOW_END(W) (void) 0
1991
1992 #endif /* not GLYPH_DEBUG */
1993
1994
1995 \f
1996 /***********************************************************************
1997 Iterator initialization
1998 ***********************************************************************/
1999
2000 /* Initialize IT for displaying current_buffer in window W, starting
2001 at character position CHARPOS. CHARPOS < 0 means that no buffer
2002 position is specified which is useful when the iterator is assigned
2003 a position later. BYTEPOS is the byte position corresponding to
2004 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2005
2006 If ROW is not null, calls to produce_glyphs with IT as parameter
2007 will produce glyphs in that row.
2008
2009 BASE_FACE_ID is the id of a base face to use. It must be one of
2010 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2011 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2012 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2013
2014 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2015 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2016 will be initialized to use the corresponding mode line glyph row of
2017 the desired matrix of W. */
2018
2019 void
2020 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2021 struct it *it;
2022 struct window *w;
2023 int charpos, bytepos;
2024 struct glyph_row *row;
2025 enum face_id base_face_id;
2026 {
2027 int highlight_region_p;
2028
2029 /* Some precondition checks. */
2030 xassert (w != NULL && it != NULL);
2031 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2032 && charpos <= ZV));
2033
2034 /* If face attributes have been changed since the last redisplay,
2035 free realized faces now because they depend on face definitions
2036 that might have changed. Don't free faces while there might be
2037 desired matrices pending which reference these faces. */
2038 if (face_change_count && !inhibit_free_realized_faces)
2039 {
2040 face_change_count = 0;
2041 free_all_realized_faces (Qnil);
2042 }
2043
2044 /* Use one of the mode line rows of W's desired matrix if
2045 appropriate. */
2046 if (row == NULL)
2047 {
2048 if (base_face_id == MODE_LINE_FACE_ID
2049 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2050 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2051 else if (base_face_id == HEADER_LINE_FACE_ID)
2052 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2053 }
2054
2055 /* Clear IT. */
2056 bzero (it, sizeof *it);
2057 it->current.overlay_string_index = -1;
2058 it->current.dpvec_index = -1;
2059 it->base_face_id = base_face_id;
2060 it->string = Qnil;
2061 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2062
2063 /* The window in which we iterate over current_buffer: */
2064 XSETWINDOW (it->window, w);
2065 it->w = w;
2066 it->f = XFRAME (w->frame);
2067
2068 /* Extra space between lines (on window systems only). */
2069 if (base_face_id == DEFAULT_FACE_ID
2070 && FRAME_WINDOW_P (it->f))
2071 {
2072 if (NATNUMP (current_buffer->extra_line_spacing))
2073 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2074 else if (FLOATP (current_buffer->extra_line_spacing))
2075 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2076 * FRAME_LINE_HEIGHT (it->f));
2077 else if (it->f->extra_line_spacing > 0)
2078 it->extra_line_spacing = it->f->extra_line_spacing;
2079 it->max_extra_line_spacing = 0;
2080 }
2081
2082 /* If realized faces have been removed, e.g. because of face
2083 attribute changes of named faces, recompute them. When running
2084 in batch mode, the face cache of the initial frame is null. If
2085 we happen to get called, make a dummy face cache. */
2086 if (FRAME_FACE_CACHE (it->f) == NULL)
2087 init_frame_faces (it->f);
2088 if (FRAME_FACE_CACHE (it->f)->used == 0)
2089 recompute_basic_faces (it->f);
2090
2091 /* Current value of the `slice', `space-width', and 'height' properties. */
2092 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2093 it->space_width = Qnil;
2094 it->font_height = Qnil;
2095 it->override_ascent = -1;
2096
2097 /* Are control characters displayed as `^C'? */
2098 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2099
2100 /* -1 means everything between a CR and the following line end
2101 is invisible. >0 means lines indented more than this value are
2102 invisible. */
2103 it->selective = (INTEGERP (current_buffer->selective_display)
2104 ? XFASTINT (current_buffer->selective_display)
2105 : (!NILP (current_buffer->selective_display)
2106 ? -1 : 0));
2107 it->selective_display_ellipsis_p
2108 = !NILP (current_buffer->selective_display_ellipses);
2109
2110 /* Display table to use. */
2111 it->dp = window_display_table (w);
2112
2113 /* Are multibyte characters enabled in current_buffer? */
2114 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2115
2116 /* Non-zero if we should highlight the region. */
2117 highlight_region_p
2118 = (!NILP (Vtransient_mark_mode)
2119 && !NILP (current_buffer->mark_active)
2120 && XMARKER (current_buffer->mark)->buffer != 0);
2121
2122 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2123 start and end of a visible region in window IT->w. Set both to
2124 -1 to indicate no region. */
2125 if (highlight_region_p
2126 /* Maybe highlight only in selected window. */
2127 && (/* Either show region everywhere. */
2128 highlight_nonselected_windows
2129 /* Or show region in the selected window. */
2130 || w == XWINDOW (selected_window)
2131 /* Or show the region if we are in the mini-buffer and W is
2132 the window the mini-buffer refers to. */
2133 || (MINI_WINDOW_P (XWINDOW (selected_window))
2134 && WINDOWP (minibuf_selected_window)
2135 && w == XWINDOW (minibuf_selected_window))))
2136 {
2137 int charpos = marker_position (current_buffer->mark);
2138 it->region_beg_charpos = min (PT, charpos);
2139 it->region_end_charpos = max (PT, charpos);
2140 }
2141 else
2142 it->region_beg_charpos = it->region_end_charpos = -1;
2143
2144 /* Get the position at which the redisplay_end_trigger hook should
2145 be run, if it is to be run at all. */
2146 if (MARKERP (w->redisplay_end_trigger)
2147 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2148 it->redisplay_end_trigger_charpos
2149 = marker_position (w->redisplay_end_trigger);
2150 else if (INTEGERP (w->redisplay_end_trigger))
2151 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2152
2153 /* Correct bogus values of tab_width. */
2154 it->tab_width = XINT (current_buffer->tab_width);
2155 if (it->tab_width <= 0 || it->tab_width > 1000)
2156 it->tab_width = 8;
2157
2158 /* Are lines in the display truncated? */
2159 it->truncate_lines_p
2160 = (base_face_id != DEFAULT_FACE_ID
2161 || XINT (it->w->hscroll)
2162 || (truncate_partial_width_windows
2163 && !WINDOW_FULL_WIDTH_P (it->w))
2164 || !NILP (current_buffer->truncate_lines));
2165
2166 /* Get dimensions of truncation and continuation glyphs. These are
2167 displayed as fringe bitmaps under X, so we don't need them for such
2168 frames. */
2169 if (!FRAME_WINDOW_P (it->f))
2170 {
2171 if (it->truncate_lines_p)
2172 {
2173 /* We will need the truncation glyph. */
2174 xassert (it->glyph_row == NULL);
2175 produce_special_glyphs (it, IT_TRUNCATION);
2176 it->truncation_pixel_width = it->pixel_width;
2177 }
2178 else
2179 {
2180 /* We will need the continuation glyph. */
2181 xassert (it->glyph_row == NULL);
2182 produce_special_glyphs (it, IT_CONTINUATION);
2183 it->continuation_pixel_width = it->pixel_width;
2184 }
2185
2186 /* Reset these values to zero because the produce_special_glyphs
2187 above has changed them. */
2188 it->pixel_width = it->ascent = it->descent = 0;
2189 it->phys_ascent = it->phys_descent = 0;
2190 }
2191
2192 /* Set this after getting the dimensions of truncation and
2193 continuation glyphs, so that we don't produce glyphs when calling
2194 produce_special_glyphs, above. */
2195 it->glyph_row = row;
2196 it->area = TEXT_AREA;
2197
2198 /* Get the dimensions of the display area. The display area
2199 consists of the visible window area plus a horizontally scrolled
2200 part to the left of the window. All x-values are relative to the
2201 start of this total display area. */
2202 if (base_face_id != DEFAULT_FACE_ID)
2203 {
2204 /* Mode lines, menu bar in terminal frames. */
2205 it->first_visible_x = 0;
2206 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2207 }
2208 else
2209 {
2210 it->first_visible_x
2211 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2212 it->last_visible_x = (it->first_visible_x
2213 + window_box_width (w, TEXT_AREA));
2214
2215 /* If we truncate lines, leave room for the truncator glyph(s) at
2216 the right margin. Otherwise, leave room for the continuation
2217 glyph(s). Truncation and continuation glyphs are not inserted
2218 for window-based redisplay. */
2219 if (!FRAME_WINDOW_P (it->f))
2220 {
2221 if (it->truncate_lines_p)
2222 it->last_visible_x -= it->truncation_pixel_width;
2223 else
2224 it->last_visible_x -= it->continuation_pixel_width;
2225 }
2226
2227 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2228 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2229 }
2230
2231 /* Leave room for a border glyph. */
2232 if (!FRAME_WINDOW_P (it->f)
2233 && !WINDOW_RIGHTMOST_P (it->w))
2234 it->last_visible_x -= 1;
2235
2236 it->last_visible_y = window_text_bottom_y (w);
2237
2238 /* For mode lines and alike, arrange for the first glyph having a
2239 left box line if the face specifies a box. */
2240 if (base_face_id != DEFAULT_FACE_ID)
2241 {
2242 struct face *face;
2243
2244 it->face_id = base_face_id;
2245
2246 /* If we have a boxed mode line, make the first character appear
2247 with a left box line. */
2248 face = FACE_FROM_ID (it->f, base_face_id);
2249 if (face->box != FACE_NO_BOX)
2250 it->start_of_box_run_p = 1;
2251 }
2252
2253 /* If a buffer position was specified, set the iterator there,
2254 getting overlays and face properties from that position. */
2255 if (charpos >= BUF_BEG (current_buffer))
2256 {
2257 it->end_charpos = ZV;
2258 it->face_id = -1;
2259 IT_CHARPOS (*it) = charpos;
2260
2261 /* Compute byte position if not specified. */
2262 if (bytepos < charpos)
2263 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2264 else
2265 IT_BYTEPOS (*it) = bytepos;
2266
2267 it->start = it->current;
2268
2269 /* Compute faces etc. */
2270 reseat (it, it->current.pos, 1);
2271 }
2272
2273 CHECK_IT (it);
2274 }
2275
2276
2277 /* Initialize IT for the display of window W with window start POS. */
2278
2279 void
2280 start_display (it, w, pos)
2281 struct it *it;
2282 struct window *w;
2283 struct text_pos pos;
2284 {
2285 struct glyph_row *row;
2286 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2287
2288 row = w->desired_matrix->rows + first_vpos;
2289 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2290 it->first_vpos = first_vpos;
2291
2292 if (!it->truncate_lines_p)
2293 {
2294 int start_at_line_beg_p;
2295 int first_y = it->current_y;
2296
2297 /* If window start is not at a line start, skip forward to POS to
2298 get the correct continuation lines width. */
2299 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2300 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2301 if (!start_at_line_beg_p)
2302 {
2303 int new_x;
2304
2305 reseat_at_previous_visible_line_start (it);
2306 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2307
2308 new_x = it->current_x + it->pixel_width;
2309
2310 /* If lines are continued, this line may end in the middle
2311 of a multi-glyph character (e.g. a control character
2312 displayed as \003, or in the middle of an overlay
2313 string). In this case move_it_to above will not have
2314 taken us to the start of the continuation line but to the
2315 end of the continued line. */
2316 if (it->current_x > 0
2317 && !it->truncate_lines_p /* Lines are continued. */
2318 && (/* And glyph doesn't fit on the line. */
2319 new_x > it->last_visible_x
2320 /* Or it fits exactly and we're on a window
2321 system frame. */
2322 || (new_x == it->last_visible_x
2323 && FRAME_WINDOW_P (it->f))))
2324 {
2325 if (it->current.dpvec_index >= 0
2326 || it->current.overlay_string_index >= 0)
2327 {
2328 set_iterator_to_next (it, 1);
2329 move_it_in_display_line_to (it, -1, -1, 0);
2330 }
2331
2332 it->continuation_lines_width += it->current_x;
2333 }
2334
2335 /* We're starting a new display line, not affected by the
2336 height of the continued line, so clear the appropriate
2337 fields in the iterator structure. */
2338 it->max_ascent = it->max_descent = 0;
2339 it->max_phys_ascent = it->max_phys_descent = 0;
2340
2341 it->current_y = first_y;
2342 it->vpos = 0;
2343 it->current_x = it->hpos = 0;
2344 }
2345 }
2346
2347 #if 0 /* Don't assert the following because start_display is sometimes
2348 called intentionally with a window start that is not at a
2349 line start. Please leave this code in as a comment. */
2350
2351 /* Window start should be on a line start, now. */
2352 xassert (it->continuation_lines_width
2353 || IT_CHARPOS (it) == BEGV
2354 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2355 #endif /* 0 */
2356 }
2357
2358
2359 /* Return 1 if POS is a position in ellipses displayed for invisible
2360 text. W is the window we display, for text property lookup. */
2361
2362 static int
2363 in_ellipses_for_invisible_text_p (pos, w)
2364 struct display_pos *pos;
2365 struct window *w;
2366 {
2367 Lisp_Object prop, window;
2368 int ellipses_p = 0;
2369 int charpos = CHARPOS (pos->pos);
2370
2371 /* If POS specifies a position in a display vector, this might
2372 be for an ellipsis displayed for invisible text. We won't
2373 get the iterator set up for delivering that ellipsis unless
2374 we make sure that it gets aware of the invisible text. */
2375 if (pos->dpvec_index >= 0
2376 && pos->overlay_string_index < 0
2377 && CHARPOS (pos->string_pos) < 0
2378 && charpos > BEGV
2379 && (XSETWINDOW (window, w),
2380 prop = Fget_char_property (make_number (charpos),
2381 Qinvisible, window),
2382 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2383 {
2384 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2385 window);
2386 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2387 }
2388
2389 return ellipses_p;
2390 }
2391
2392
2393 /* Initialize IT for stepping through current_buffer in window W,
2394 starting at position POS that includes overlay string and display
2395 vector/ control character translation position information. Value
2396 is zero if there are overlay strings with newlines at POS. */
2397
2398 static int
2399 init_from_display_pos (it, w, pos)
2400 struct it *it;
2401 struct window *w;
2402 struct display_pos *pos;
2403 {
2404 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2405 int i, overlay_strings_with_newlines = 0;
2406
2407 /* If POS specifies a position in a display vector, this might
2408 be for an ellipsis displayed for invisible text. We won't
2409 get the iterator set up for delivering that ellipsis unless
2410 we make sure that it gets aware of the invisible text. */
2411 if (in_ellipses_for_invisible_text_p (pos, w))
2412 {
2413 --charpos;
2414 bytepos = 0;
2415 }
2416
2417 /* Keep in mind: the call to reseat in init_iterator skips invisible
2418 text, so we might end up at a position different from POS. This
2419 is only a problem when POS is a row start after a newline and an
2420 overlay starts there with an after-string, and the overlay has an
2421 invisible property. Since we don't skip invisible text in
2422 display_line and elsewhere immediately after consuming the
2423 newline before the row start, such a POS will not be in a string,
2424 but the call to init_iterator below will move us to the
2425 after-string. */
2426 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2427
2428 for (i = 0; i < it->n_overlay_strings; ++i)
2429 {
2430 const char *s = SDATA (it->overlay_strings[i]);
2431 const char *e = s + SBYTES (it->overlay_strings[i]);
2432
2433 while (s < e && *s != '\n')
2434 ++s;
2435
2436 if (s < e)
2437 {
2438 overlay_strings_with_newlines = 1;
2439 break;
2440 }
2441 }
2442
2443 /* If position is within an overlay string, set up IT to the right
2444 overlay string. */
2445 if (pos->overlay_string_index >= 0)
2446 {
2447 int relative_index;
2448
2449 /* If the first overlay string happens to have a `display'
2450 property for an image, the iterator will be set up for that
2451 image, and we have to undo that setup first before we can
2452 correct the overlay string index. */
2453 if (it->method == next_element_from_image)
2454 pop_it (it);
2455
2456 /* We already have the first chunk of overlay strings in
2457 IT->overlay_strings. Load more until the one for
2458 pos->overlay_string_index is in IT->overlay_strings. */
2459 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2460 {
2461 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2462 it->current.overlay_string_index = 0;
2463 while (n--)
2464 {
2465 load_overlay_strings (it, 0);
2466 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2467 }
2468 }
2469
2470 it->current.overlay_string_index = pos->overlay_string_index;
2471 relative_index = (it->current.overlay_string_index
2472 % OVERLAY_STRING_CHUNK_SIZE);
2473 it->string = it->overlay_strings[relative_index];
2474 xassert (STRINGP (it->string));
2475 it->current.string_pos = pos->string_pos;
2476 it->method = next_element_from_string;
2477 }
2478
2479 #if 0 /* This is bogus because POS not having an overlay string
2480 position does not mean it's after the string. Example: A
2481 line starting with a before-string and initialization of IT
2482 to the previous row's end position. */
2483 else if (it->current.overlay_string_index >= 0)
2484 {
2485 /* If POS says we're already after an overlay string ending at
2486 POS, make sure to pop the iterator because it will be in
2487 front of that overlay string. When POS is ZV, we've thereby
2488 also ``processed'' overlay strings at ZV. */
2489 while (it->sp)
2490 pop_it (it);
2491 it->current.overlay_string_index = -1;
2492 it->method = next_element_from_buffer;
2493 if (CHARPOS (pos->pos) == ZV)
2494 it->overlay_strings_at_end_processed_p = 1;
2495 }
2496 #endif /* 0 */
2497
2498 if (CHARPOS (pos->string_pos) >= 0)
2499 {
2500 /* Recorded position is not in an overlay string, but in another
2501 string. This can only be a string from a `display' property.
2502 IT should already be filled with that string. */
2503 it->current.string_pos = pos->string_pos;
2504 xassert (STRINGP (it->string));
2505 }
2506
2507 /* Restore position in display vector translations, control
2508 character translations or ellipses. */
2509 if (pos->dpvec_index >= 0)
2510 {
2511 if (it->dpvec == NULL)
2512 get_next_display_element (it);
2513 xassert (it->dpvec && it->current.dpvec_index == 0);
2514 it->current.dpvec_index = pos->dpvec_index;
2515 }
2516
2517 CHECK_IT (it);
2518 return !overlay_strings_with_newlines;
2519 }
2520
2521
2522 /* Initialize IT for stepping through current_buffer in window W
2523 starting at ROW->start. */
2524
2525 static void
2526 init_to_row_start (it, w, row)
2527 struct it *it;
2528 struct window *w;
2529 struct glyph_row *row;
2530 {
2531 init_from_display_pos (it, w, &row->start);
2532 it->start = row->start;
2533 it->continuation_lines_width = row->continuation_lines_width;
2534 CHECK_IT (it);
2535 }
2536
2537
2538 /* Initialize IT for stepping through current_buffer in window W
2539 starting in the line following ROW, i.e. starting at ROW->end.
2540 Value is zero if there are overlay strings with newlines at ROW's
2541 end position. */
2542
2543 static int
2544 init_to_row_end (it, w, row)
2545 struct it *it;
2546 struct window *w;
2547 struct glyph_row *row;
2548 {
2549 int success = 0;
2550
2551 if (init_from_display_pos (it, w, &row->end))
2552 {
2553 if (row->continued_p)
2554 it->continuation_lines_width
2555 = row->continuation_lines_width + row->pixel_width;
2556 CHECK_IT (it);
2557 success = 1;
2558 }
2559
2560 return success;
2561 }
2562
2563
2564
2565 \f
2566 /***********************************************************************
2567 Text properties
2568 ***********************************************************************/
2569
2570 /* Called when IT reaches IT->stop_charpos. Handle text property and
2571 overlay changes. Set IT->stop_charpos to the next position where
2572 to stop. */
2573
2574 static void
2575 handle_stop (it)
2576 struct it *it;
2577 {
2578 enum prop_handled handled;
2579 int handle_overlay_change_p = 1;
2580 struct props *p;
2581
2582 it->dpvec = NULL;
2583 it->current.dpvec_index = -1;
2584
2585 do
2586 {
2587 handled = HANDLED_NORMALLY;
2588
2589 /* Call text property handlers. */
2590 for (p = it_props; p->handler; ++p)
2591 {
2592 handled = p->handler (it);
2593
2594 if (handled == HANDLED_RECOMPUTE_PROPS)
2595 break;
2596 else if (handled == HANDLED_RETURN)
2597 return;
2598 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2599 handle_overlay_change_p = 0;
2600 }
2601
2602 if (handled != HANDLED_RECOMPUTE_PROPS)
2603 {
2604 /* Don't check for overlay strings below when set to deliver
2605 characters from a display vector. */
2606 if (it->method == next_element_from_display_vector)
2607 handle_overlay_change_p = 0;
2608
2609 /* Handle overlay changes. */
2610 if (handle_overlay_change_p)
2611 handled = handle_overlay_change (it);
2612
2613 /* Determine where to stop next. */
2614 if (handled == HANDLED_NORMALLY)
2615 compute_stop_pos (it);
2616 }
2617 }
2618 while (handled == HANDLED_RECOMPUTE_PROPS);
2619 }
2620
2621
2622 /* Compute IT->stop_charpos from text property and overlay change
2623 information for IT's current position. */
2624
2625 static void
2626 compute_stop_pos (it)
2627 struct it *it;
2628 {
2629 register INTERVAL iv, next_iv;
2630 Lisp_Object object, limit, position;
2631
2632 /* If nowhere else, stop at the end. */
2633 it->stop_charpos = it->end_charpos;
2634
2635 if (STRINGP (it->string))
2636 {
2637 /* Strings are usually short, so don't limit the search for
2638 properties. */
2639 object = it->string;
2640 limit = Qnil;
2641 position = make_number (IT_STRING_CHARPOS (*it));
2642 }
2643 else
2644 {
2645 int charpos;
2646
2647 /* If next overlay change is in front of the current stop pos
2648 (which is IT->end_charpos), stop there. Note: value of
2649 next_overlay_change is point-max if no overlay change
2650 follows. */
2651 charpos = next_overlay_change (IT_CHARPOS (*it));
2652 if (charpos < it->stop_charpos)
2653 it->stop_charpos = charpos;
2654
2655 /* If showing the region, we have to stop at the region
2656 start or end because the face might change there. */
2657 if (it->region_beg_charpos > 0)
2658 {
2659 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2660 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2661 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2662 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2663 }
2664
2665 /* Set up variables for computing the stop position from text
2666 property changes. */
2667 XSETBUFFER (object, current_buffer);
2668 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2669 position = make_number (IT_CHARPOS (*it));
2670
2671 }
2672
2673 /* Get the interval containing IT's position. Value is a null
2674 interval if there isn't such an interval. */
2675 iv = validate_interval_range (object, &position, &position, 0);
2676 if (!NULL_INTERVAL_P (iv))
2677 {
2678 Lisp_Object values_here[LAST_PROP_IDX];
2679 struct props *p;
2680
2681 /* Get properties here. */
2682 for (p = it_props; p->handler; ++p)
2683 values_here[p->idx] = textget (iv->plist, *p->name);
2684
2685 /* Look for an interval following iv that has different
2686 properties. */
2687 for (next_iv = next_interval (iv);
2688 (!NULL_INTERVAL_P (next_iv)
2689 && (NILP (limit)
2690 || XFASTINT (limit) > next_iv->position));
2691 next_iv = next_interval (next_iv))
2692 {
2693 for (p = it_props; p->handler; ++p)
2694 {
2695 Lisp_Object new_value;
2696
2697 new_value = textget (next_iv->plist, *p->name);
2698 if (!EQ (values_here[p->idx], new_value))
2699 break;
2700 }
2701
2702 if (p->handler)
2703 break;
2704 }
2705
2706 if (!NULL_INTERVAL_P (next_iv))
2707 {
2708 if (INTEGERP (limit)
2709 && next_iv->position >= XFASTINT (limit))
2710 /* No text property change up to limit. */
2711 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2712 else
2713 /* Text properties change in next_iv. */
2714 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2715 }
2716 }
2717
2718 xassert (STRINGP (it->string)
2719 || (it->stop_charpos >= BEGV
2720 && it->stop_charpos >= IT_CHARPOS (*it)));
2721 }
2722
2723
2724 /* Return the position of the next overlay change after POS in
2725 current_buffer. Value is point-max if no overlay change
2726 follows. This is like `next-overlay-change' but doesn't use
2727 xmalloc. */
2728
2729 static int
2730 next_overlay_change (pos)
2731 int pos;
2732 {
2733 int noverlays;
2734 int endpos;
2735 Lisp_Object *overlays;
2736 int i;
2737
2738 /* Get all overlays at the given position. */
2739 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2740
2741 /* If any of these overlays ends before endpos,
2742 use its ending point instead. */
2743 for (i = 0; i < noverlays; ++i)
2744 {
2745 Lisp_Object oend;
2746 int oendpos;
2747
2748 oend = OVERLAY_END (overlays[i]);
2749 oendpos = OVERLAY_POSITION (oend);
2750 endpos = min (endpos, oendpos);
2751 }
2752
2753 return endpos;
2754 }
2755
2756
2757 \f
2758 /***********************************************************************
2759 Fontification
2760 ***********************************************************************/
2761
2762 /* Handle changes in the `fontified' property of the current buffer by
2763 calling hook functions from Qfontification_functions to fontify
2764 regions of text. */
2765
2766 static enum prop_handled
2767 handle_fontified_prop (it)
2768 struct it *it;
2769 {
2770 Lisp_Object prop, pos;
2771 enum prop_handled handled = HANDLED_NORMALLY;
2772
2773 /* Get the value of the `fontified' property at IT's current buffer
2774 position. (The `fontified' property doesn't have a special
2775 meaning in strings.) If the value is nil, call functions from
2776 Qfontification_functions. */
2777 if (!STRINGP (it->string)
2778 && it->s == NULL
2779 && !NILP (Vfontification_functions)
2780 && !NILP (Vrun_hooks)
2781 && (pos = make_number (IT_CHARPOS (*it)),
2782 prop = Fget_char_property (pos, Qfontified, Qnil),
2783 NILP (prop)))
2784 {
2785 int count = SPECPDL_INDEX ();
2786 Lisp_Object val;
2787
2788 val = Vfontification_functions;
2789 specbind (Qfontification_functions, Qnil);
2790
2791 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2792 safe_call1 (val, pos);
2793 else
2794 {
2795 Lisp_Object globals, fn;
2796 struct gcpro gcpro1, gcpro2;
2797
2798 globals = Qnil;
2799 GCPRO2 (val, globals);
2800
2801 for (; CONSP (val); val = XCDR (val))
2802 {
2803 fn = XCAR (val);
2804
2805 if (EQ (fn, Qt))
2806 {
2807 /* A value of t indicates this hook has a local
2808 binding; it means to run the global binding too.
2809 In a global value, t should not occur. If it
2810 does, we must ignore it to avoid an endless
2811 loop. */
2812 for (globals = Fdefault_value (Qfontification_functions);
2813 CONSP (globals);
2814 globals = XCDR (globals))
2815 {
2816 fn = XCAR (globals);
2817 if (!EQ (fn, Qt))
2818 safe_call1 (fn, pos);
2819 }
2820 }
2821 else
2822 safe_call1 (fn, pos);
2823 }
2824
2825 UNGCPRO;
2826 }
2827
2828 unbind_to (count, Qnil);
2829
2830 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2831 something. This avoids an endless loop if they failed to
2832 fontify the text for which reason ever. */
2833 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2834 handled = HANDLED_RECOMPUTE_PROPS;
2835 }
2836
2837 return handled;
2838 }
2839
2840
2841 \f
2842 /***********************************************************************
2843 Faces
2844 ***********************************************************************/
2845
2846 /* Set up iterator IT from face properties at its current position.
2847 Called from handle_stop. */
2848
2849 static enum prop_handled
2850 handle_face_prop (it)
2851 struct it *it;
2852 {
2853 int new_face_id, next_stop;
2854
2855 if (!STRINGP (it->string))
2856 {
2857 new_face_id
2858 = face_at_buffer_position (it->w,
2859 IT_CHARPOS (*it),
2860 it->region_beg_charpos,
2861 it->region_end_charpos,
2862 &next_stop,
2863 (IT_CHARPOS (*it)
2864 + TEXT_PROP_DISTANCE_LIMIT),
2865 0);
2866
2867 /* Is this a start of a run of characters with box face?
2868 Caveat: this can be called for a freshly initialized
2869 iterator; face_id is -1 in this case. We know that the new
2870 face will not change until limit, i.e. if the new face has a
2871 box, all characters up to limit will have one. But, as
2872 usual, we don't know whether limit is really the end. */
2873 if (new_face_id != it->face_id)
2874 {
2875 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2876
2877 /* If new face has a box but old face has not, this is
2878 the start of a run of characters with box, i.e. it has
2879 a shadow on the left side. The value of face_id of the
2880 iterator will be -1 if this is the initial call that gets
2881 the face. In this case, we have to look in front of IT's
2882 position and see whether there is a face != new_face_id. */
2883 it->start_of_box_run_p
2884 = (new_face->box != FACE_NO_BOX
2885 && (it->face_id >= 0
2886 || IT_CHARPOS (*it) == BEG
2887 || new_face_id != face_before_it_pos (it)));
2888 it->face_box_p = new_face->box != FACE_NO_BOX;
2889 }
2890 }
2891 else
2892 {
2893 int base_face_id, bufpos;
2894
2895 if (it->current.overlay_string_index >= 0)
2896 bufpos = IT_CHARPOS (*it);
2897 else
2898 bufpos = 0;
2899
2900 /* For strings from a buffer, i.e. overlay strings or strings
2901 from a `display' property, use the face at IT's current
2902 buffer position as the base face to merge with, so that
2903 overlay strings appear in the same face as surrounding
2904 text, unless they specify their own faces. */
2905 base_face_id = underlying_face_id (it);
2906
2907 new_face_id = face_at_string_position (it->w,
2908 it->string,
2909 IT_STRING_CHARPOS (*it),
2910 bufpos,
2911 it->region_beg_charpos,
2912 it->region_end_charpos,
2913 &next_stop,
2914 base_face_id, 0);
2915
2916 #if 0 /* This shouldn't be neccessary. Let's check it. */
2917 /* If IT is used to display a mode line we would really like to
2918 use the mode line face instead of the frame's default face. */
2919 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2920 && new_face_id == DEFAULT_FACE_ID)
2921 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2922 #endif
2923
2924 /* Is this a start of a run of characters with box? Caveat:
2925 this can be called for a freshly allocated iterator; face_id
2926 is -1 is this case. We know that the new face will not
2927 change until the next check pos, i.e. if the new face has a
2928 box, all characters up to that position will have a
2929 box. But, as usual, we don't know whether that position
2930 is really the end. */
2931 if (new_face_id != it->face_id)
2932 {
2933 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2934 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2935
2936 /* If new face has a box but old face hasn't, this is the
2937 start of a run of characters with box, i.e. it has a
2938 shadow on the left side. */
2939 it->start_of_box_run_p
2940 = new_face->box && (old_face == NULL || !old_face->box);
2941 it->face_box_p = new_face->box != FACE_NO_BOX;
2942 }
2943 }
2944
2945 it->face_id = new_face_id;
2946 return HANDLED_NORMALLY;
2947 }
2948
2949
2950 /* Return the ID of the face ``underlying'' IT's current position,
2951 which is in a string. If the iterator is associated with a
2952 buffer, return the face at IT's current buffer position.
2953 Otherwise, use the iterator's base_face_id. */
2954
2955 static int
2956 underlying_face_id (it)
2957 struct it *it;
2958 {
2959 int face_id = it->base_face_id, i;
2960
2961 xassert (STRINGP (it->string));
2962
2963 for (i = it->sp - 1; i >= 0; --i)
2964 if (NILP (it->stack[i].string))
2965 face_id = it->stack[i].face_id;
2966
2967 return face_id;
2968 }
2969
2970
2971 /* Compute the face one character before or after the current position
2972 of IT. BEFORE_P non-zero means get the face in front of IT's
2973 position. Value is the id of the face. */
2974
2975 static int
2976 face_before_or_after_it_pos (it, before_p)
2977 struct it *it;
2978 int before_p;
2979 {
2980 int face_id, limit;
2981 int next_check_charpos;
2982 struct text_pos pos;
2983
2984 xassert (it->s == NULL);
2985
2986 if (STRINGP (it->string))
2987 {
2988 int bufpos, base_face_id;
2989
2990 /* No face change past the end of the string (for the case
2991 we are padding with spaces). No face change before the
2992 string start. */
2993 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2994 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2995 return it->face_id;
2996
2997 /* Set pos to the position before or after IT's current position. */
2998 if (before_p)
2999 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3000 else
3001 /* For composition, we must check the character after the
3002 composition. */
3003 pos = (it->what == IT_COMPOSITION
3004 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3005 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3006
3007 if (it->current.overlay_string_index >= 0)
3008 bufpos = IT_CHARPOS (*it);
3009 else
3010 bufpos = 0;
3011
3012 base_face_id = underlying_face_id (it);
3013
3014 /* Get the face for ASCII, or unibyte. */
3015 face_id = face_at_string_position (it->w,
3016 it->string,
3017 CHARPOS (pos),
3018 bufpos,
3019 it->region_beg_charpos,
3020 it->region_end_charpos,
3021 &next_check_charpos,
3022 base_face_id, 0);
3023
3024 /* Correct the face for charsets different from ASCII. Do it
3025 for the multibyte case only. The face returned above is
3026 suitable for unibyte text if IT->string is unibyte. */
3027 if (STRING_MULTIBYTE (it->string))
3028 {
3029 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3030 int rest = SBYTES (it->string) - BYTEPOS (pos);
3031 int c, len;
3032 struct face *face = FACE_FROM_ID (it->f, face_id);
3033
3034 c = string_char_and_length (p, rest, &len);
3035 face_id = FACE_FOR_CHAR (it->f, face, c);
3036 }
3037 }
3038 else
3039 {
3040 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3041 || (IT_CHARPOS (*it) <= BEGV && before_p))
3042 return it->face_id;
3043
3044 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3045 pos = it->current.pos;
3046
3047 if (before_p)
3048 DEC_TEXT_POS (pos, it->multibyte_p);
3049 else
3050 {
3051 if (it->what == IT_COMPOSITION)
3052 /* For composition, we must check the position after the
3053 composition. */
3054 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3055 else
3056 INC_TEXT_POS (pos, it->multibyte_p);
3057 }
3058
3059 /* Determine face for CHARSET_ASCII, or unibyte. */
3060 face_id = face_at_buffer_position (it->w,
3061 CHARPOS (pos),
3062 it->region_beg_charpos,
3063 it->region_end_charpos,
3064 &next_check_charpos,
3065 limit, 0);
3066
3067 /* Correct the face for charsets different from ASCII. Do it
3068 for the multibyte case only. The face returned above is
3069 suitable for unibyte text if current_buffer is unibyte. */
3070 if (it->multibyte_p)
3071 {
3072 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3073 struct face *face = FACE_FROM_ID (it->f, face_id);
3074 face_id = FACE_FOR_CHAR (it->f, face, c);
3075 }
3076 }
3077
3078 return face_id;
3079 }
3080
3081
3082 \f
3083 /***********************************************************************
3084 Invisible text
3085 ***********************************************************************/
3086
3087 /* Set up iterator IT from invisible properties at its current
3088 position. Called from handle_stop. */
3089
3090 static enum prop_handled
3091 handle_invisible_prop (it)
3092 struct it *it;
3093 {
3094 enum prop_handled handled = HANDLED_NORMALLY;
3095
3096 if (STRINGP (it->string))
3097 {
3098 extern Lisp_Object Qinvisible;
3099 Lisp_Object prop, end_charpos, limit, charpos;
3100
3101 /* Get the value of the invisible text property at the
3102 current position. Value will be nil if there is no such
3103 property. */
3104 charpos = make_number (IT_STRING_CHARPOS (*it));
3105 prop = Fget_text_property (charpos, Qinvisible, it->string);
3106
3107 if (!NILP (prop)
3108 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3109 {
3110 handled = HANDLED_RECOMPUTE_PROPS;
3111
3112 /* Get the position at which the next change of the
3113 invisible text property can be found in IT->string.
3114 Value will be nil if the property value is the same for
3115 all the rest of IT->string. */
3116 XSETINT (limit, SCHARS (it->string));
3117 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3118 it->string, limit);
3119
3120 /* Text at current position is invisible. The next
3121 change in the property is at position end_charpos.
3122 Move IT's current position to that position. */
3123 if (INTEGERP (end_charpos)
3124 && XFASTINT (end_charpos) < XFASTINT (limit))
3125 {
3126 struct text_pos old;
3127 old = it->current.string_pos;
3128 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3129 compute_string_pos (&it->current.string_pos, old, it->string);
3130 }
3131 else
3132 {
3133 /* The rest of the string is invisible. If this is an
3134 overlay string, proceed with the next overlay string
3135 or whatever comes and return a character from there. */
3136 if (it->current.overlay_string_index >= 0)
3137 {
3138 next_overlay_string (it);
3139 /* Don't check for overlay strings when we just
3140 finished processing them. */
3141 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3142 }
3143 else
3144 {
3145 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3146 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3147 }
3148 }
3149 }
3150 }
3151 else
3152 {
3153 int invis_p, newpos, next_stop, start_charpos;
3154 Lisp_Object pos, prop, overlay;
3155
3156 /* First of all, is there invisible text at this position? */
3157 start_charpos = IT_CHARPOS (*it);
3158 pos = make_number (IT_CHARPOS (*it));
3159 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3160 &overlay);
3161 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3162
3163 /* If we are on invisible text, skip over it. */
3164 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3165 {
3166 /* Record whether we have to display an ellipsis for the
3167 invisible text. */
3168 int display_ellipsis_p = invis_p == 2;
3169
3170 handled = HANDLED_RECOMPUTE_PROPS;
3171
3172 /* Loop skipping over invisible text. The loop is left at
3173 ZV or with IT on the first char being visible again. */
3174 do
3175 {
3176 /* Try to skip some invisible text. Return value is the
3177 position reached which can be equal to IT's position
3178 if there is nothing invisible here. This skips both
3179 over invisible text properties and overlays with
3180 invisible property. */
3181 newpos = skip_invisible (IT_CHARPOS (*it),
3182 &next_stop, ZV, it->window);
3183
3184 /* If we skipped nothing at all we weren't at invisible
3185 text in the first place. If everything to the end of
3186 the buffer was skipped, end the loop. */
3187 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3188 invis_p = 0;
3189 else
3190 {
3191 /* We skipped some characters but not necessarily
3192 all there are. Check if we ended up on visible
3193 text. Fget_char_property returns the property of
3194 the char before the given position, i.e. if we
3195 get invis_p = 0, this means that the char at
3196 newpos is visible. */
3197 pos = make_number (newpos);
3198 prop = Fget_char_property (pos, Qinvisible, it->window);
3199 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3200 }
3201
3202 /* If we ended up on invisible text, proceed to
3203 skip starting with next_stop. */
3204 if (invis_p)
3205 IT_CHARPOS (*it) = next_stop;
3206 }
3207 while (invis_p);
3208
3209 /* The position newpos is now either ZV or on visible text. */
3210 IT_CHARPOS (*it) = newpos;
3211 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3212
3213 /* If there are before-strings at the start of invisible
3214 text, and the text is invisible because of a text
3215 property, arrange to show before-strings because 20.x did
3216 it that way. (If the text is invisible because of an
3217 overlay property instead of a text property, this is
3218 already handled in the overlay code.) */
3219 if (NILP (overlay)
3220 && get_overlay_strings (it, start_charpos))
3221 {
3222 handled = HANDLED_RECOMPUTE_PROPS;
3223 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3224 }
3225 else if (display_ellipsis_p)
3226 setup_for_ellipsis (it);
3227 }
3228 }
3229
3230 return handled;
3231 }
3232
3233
3234 /* Make iterator IT return `...' next. */
3235
3236 static void
3237 setup_for_ellipsis (it)
3238 struct it *it;
3239 {
3240 if (it->dp
3241 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3242 {
3243 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3244 it->dpvec = v->contents;
3245 it->dpend = v->contents + v->size;
3246 }
3247 else
3248 {
3249 /* Default `...'. */
3250 it->dpvec = default_invis_vector;
3251 it->dpend = default_invis_vector + 3;
3252 }
3253
3254 /* The ellipsis display does not replace the display of the
3255 character at the new position. Indicate this by setting
3256 IT->dpvec_char_len to zero. */
3257 it->dpvec_char_len = 0;
3258
3259 it->current.dpvec_index = 0;
3260 it->method = next_element_from_display_vector;
3261 }
3262
3263
3264 \f
3265 /***********************************************************************
3266 'display' property
3267 ***********************************************************************/
3268
3269 /* Set up iterator IT from `display' property at its current position.
3270 Called from handle_stop. */
3271
3272 static enum prop_handled
3273 handle_display_prop (it)
3274 struct it *it;
3275 {
3276 Lisp_Object prop, object;
3277 struct text_pos *position;
3278 int display_replaced_p = 0;
3279
3280 if (STRINGP (it->string))
3281 {
3282 object = it->string;
3283 position = &it->current.string_pos;
3284 }
3285 else
3286 {
3287 object = it->w->buffer;
3288 position = &it->current.pos;
3289 }
3290
3291 /* Reset those iterator values set from display property values. */
3292 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3293 it->space_width = Qnil;
3294 it->font_height = Qnil;
3295 it->voffset = 0;
3296
3297 /* We don't support recursive `display' properties, i.e. string
3298 values that have a string `display' property, that have a string
3299 `display' property etc. */
3300 if (!it->string_from_display_prop_p)
3301 it->area = TEXT_AREA;
3302
3303 prop = Fget_char_property (make_number (position->charpos),
3304 Qdisplay, object);
3305 if (NILP (prop))
3306 return HANDLED_NORMALLY;
3307
3308 if (CONSP (prop)
3309 /* Simple properties. */
3310 && !EQ (XCAR (prop), Qimage)
3311 && !EQ (XCAR (prop), Qspace)
3312 && !EQ (XCAR (prop), Qwhen)
3313 && !EQ (XCAR (prop), Qslice)
3314 && !EQ (XCAR (prop), Qspace_width)
3315 && !EQ (XCAR (prop), Qheight)
3316 && !EQ (XCAR (prop), Qraise)
3317 /* Marginal area specifications. */
3318 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3319 && !EQ (XCAR (prop), Qleft_fringe)
3320 && !EQ (XCAR (prop), Qright_fringe)
3321 && !NILP (XCAR (prop)))
3322 {
3323 for (; CONSP (prop); prop = XCDR (prop))
3324 {
3325 if (handle_single_display_prop (it, XCAR (prop), object,
3326 position, display_replaced_p))
3327 display_replaced_p = 1;
3328 }
3329 }
3330 else if (VECTORP (prop))
3331 {
3332 int i;
3333 for (i = 0; i < ASIZE (prop); ++i)
3334 if (handle_single_display_prop (it, AREF (prop, i), object,
3335 position, display_replaced_p))
3336 display_replaced_p = 1;
3337 }
3338 else
3339 {
3340 if (handle_single_display_prop (it, prop, object, position, 0))
3341 display_replaced_p = 1;
3342 }
3343
3344 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3345 }
3346
3347
3348 /* Value is the position of the end of the `display' property starting
3349 at START_POS in OBJECT. */
3350
3351 static struct text_pos
3352 display_prop_end (it, object, start_pos)
3353 struct it *it;
3354 Lisp_Object object;
3355 struct text_pos start_pos;
3356 {
3357 Lisp_Object end;
3358 struct text_pos end_pos;
3359
3360 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3361 Qdisplay, object, Qnil);
3362 CHARPOS (end_pos) = XFASTINT (end);
3363 if (STRINGP (object))
3364 compute_string_pos (&end_pos, start_pos, it->string);
3365 else
3366 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3367
3368 return end_pos;
3369 }
3370
3371
3372 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3373 is the object in which the `display' property was found. *POSITION
3374 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3375 means that we previously saw a display sub-property which already
3376 replaced text display with something else, for example an image;
3377 ignore such properties after the first one has been processed.
3378
3379 If PROP is a `space' or `image' sub-property, set *POSITION to the
3380 end position of the `display' property.
3381
3382 Value is non-zero if something was found which replaces the display
3383 of buffer or string text. */
3384
3385 static int
3386 handle_single_display_prop (it, prop, object, position,
3387 display_replaced_before_p)
3388 struct it *it;
3389 Lisp_Object prop;
3390 Lisp_Object object;
3391 struct text_pos *position;
3392 int display_replaced_before_p;
3393 {
3394 Lisp_Object value;
3395 int replaces_text_display_p = 0;
3396 Lisp_Object form;
3397
3398 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3399 evaluated. If the result is nil, VALUE is ignored. */
3400 form = Qt;
3401 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3402 {
3403 prop = XCDR (prop);
3404 if (!CONSP (prop))
3405 return 0;
3406 form = XCAR (prop);
3407 prop = XCDR (prop);
3408 }
3409
3410 if (!NILP (form) && !EQ (form, Qt))
3411 {
3412 int count = SPECPDL_INDEX ();
3413 struct gcpro gcpro1;
3414
3415 /* Bind `object' to the object having the `display' property, a
3416 buffer or string. Bind `position' to the position in the
3417 object where the property was found, and `buffer-position'
3418 to the current position in the buffer. */
3419 specbind (Qobject, object);
3420 specbind (Qposition, make_number (CHARPOS (*position)));
3421 specbind (Qbuffer_position,
3422 make_number (STRINGP (object)
3423 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3424 GCPRO1 (form);
3425 form = safe_eval (form);
3426 UNGCPRO;
3427 unbind_to (count, Qnil);
3428 }
3429
3430 if (NILP (form))
3431 return 0;
3432
3433 if (CONSP (prop)
3434 && EQ (XCAR (prop), Qheight)
3435 && CONSP (XCDR (prop)))
3436 {
3437 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3438 return 0;
3439
3440 /* `(height HEIGHT)'. */
3441 it->font_height = XCAR (XCDR (prop));
3442 if (!NILP (it->font_height))
3443 {
3444 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3445 int new_height = -1;
3446
3447 if (CONSP (it->font_height)
3448 && (EQ (XCAR (it->font_height), Qplus)
3449 || EQ (XCAR (it->font_height), Qminus))
3450 && CONSP (XCDR (it->font_height))
3451 && INTEGERP (XCAR (XCDR (it->font_height))))
3452 {
3453 /* `(+ N)' or `(- N)' where N is an integer. */
3454 int steps = XINT (XCAR (XCDR (it->font_height)));
3455 if (EQ (XCAR (it->font_height), Qplus))
3456 steps = - steps;
3457 it->face_id = smaller_face (it->f, it->face_id, steps);
3458 }
3459 else if (FUNCTIONP (it->font_height))
3460 {
3461 /* Call function with current height as argument.
3462 Value is the new height. */
3463 Lisp_Object height;
3464 height = safe_call1 (it->font_height,
3465 face->lface[LFACE_HEIGHT_INDEX]);
3466 if (NUMBERP (height))
3467 new_height = XFLOATINT (height);
3468 }
3469 else if (NUMBERP (it->font_height))
3470 {
3471 /* Value is a multiple of the canonical char height. */
3472 struct face *face;
3473
3474 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3475 new_height = (XFLOATINT (it->font_height)
3476 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3477 }
3478 else
3479 {
3480 /* Evaluate IT->font_height with `height' bound to the
3481 current specified height to get the new height. */
3482 Lisp_Object value;
3483 int count = SPECPDL_INDEX ();
3484
3485 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3486 value = safe_eval (it->font_height);
3487 unbind_to (count, Qnil);
3488
3489 if (NUMBERP (value))
3490 new_height = XFLOATINT (value);
3491 }
3492
3493 if (new_height > 0)
3494 it->face_id = face_with_height (it->f, it->face_id, new_height);
3495 }
3496 }
3497 else if (CONSP (prop)
3498 && EQ (XCAR (prop), Qspace_width)
3499 && CONSP (XCDR (prop)))
3500 {
3501 /* `(space_width WIDTH)'. */
3502 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3503 return 0;
3504
3505 value = XCAR (XCDR (prop));
3506 if (NUMBERP (value) && XFLOATINT (value) > 0)
3507 it->space_width = value;
3508 }
3509 else if (CONSP (prop)
3510 && EQ (XCAR (prop), Qslice))
3511 {
3512 /* `(slice X Y WIDTH HEIGHT)'. */
3513 Lisp_Object tem;
3514
3515 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3516 return 0;
3517
3518 if (tem = XCDR (prop), CONSP (tem))
3519 {
3520 it->slice.x = XCAR (tem);
3521 if (tem = XCDR (tem), CONSP (tem))
3522 {
3523 it->slice.y = XCAR (tem);
3524 if (tem = XCDR (tem), CONSP (tem))
3525 {
3526 it->slice.width = XCAR (tem);
3527 if (tem = XCDR (tem), CONSP (tem))
3528 it->slice.height = XCAR (tem);
3529 }
3530 }
3531 }
3532 }
3533 else if (CONSP (prop)
3534 && EQ (XCAR (prop), Qraise)
3535 && CONSP (XCDR (prop)))
3536 {
3537 /* `(raise FACTOR)'. */
3538 if (!FRAME_WINDOW_P (it->f))
3539 return 0;
3540
3541 #ifdef HAVE_WINDOW_SYSTEM
3542 value = XCAR (XCDR (prop));
3543 if (NUMBERP (value))
3544 {
3545 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3546 it->voffset = - (XFLOATINT (value)
3547 * (FONT_HEIGHT (face->font)));
3548 }
3549 #endif /* HAVE_WINDOW_SYSTEM */
3550 }
3551 else if (!it->string_from_display_prop_p)
3552 {
3553 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3554 VALUE) or `((margin nil) VALUE)' or VALUE. */
3555 Lisp_Object location, value;
3556 struct text_pos start_pos;
3557 int valid_p;
3558
3559 /* Characters having this form of property are not displayed, so
3560 we have to find the end of the property. */
3561 start_pos = *position;
3562 *position = display_prop_end (it, object, start_pos);
3563 value = Qnil;
3564
3565 /* Let's stop at the new position and assume that all
3566 text properties change there. */
3567 it->stop_charpos = position->charpos;
3568
3569 if (CONSP (prop)
3570 && (EQ (XCAR (prop), Qleft_fringe)
3571 || EQ (XCAR (prop), Qright_fringe))
3572 && CONSP (XCDR (prop)))
3573 {
3574 unsigned face_id = DEFAULT_FACE_ID;
3575 int fringe_bitmap;
3576
3577 /* Save current settings of IT so that we can restore them
3578 when we are finished with the glyph property value. */
3579
3580 /* `(left-fringe BITMAP FACE)'. */
3581 if (!FRAME_WINDOW_P (it->f))
3582 return 0;
3583
3584 #ifdef HAVE_WINDOW_SYSTEM
3585 value = XCAR (XCDR (prop));
3586 if (!SYMBOLP (value)
3587 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3588 return 0;
3589
3590 if (CONSP (XCDR (XCDR (prop))))
3591 {
3592 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3593
3594 face_id = lookup_named_face (it->f, face_name, 'A');
3595 if (face_id < 0)
3596 return 0;
3597 }
3598
3599 push_it (it);
3600
3601 it->area = TEXT_AREA;
3602 it->what = IT_IMAGE;
3603 it->image_id = -1; /* no image */
3604 it->position = start_pos;
3605 it->object = NILP (object) ? it->w->buffer : object;
3606 it->method = next_element_from_image;
3607 it->face_id = face_id;
3608
3609 /* Say that we haven't consumed the characters with
3610 `display' property yet. The call to pop_it in
3611 set_iterator_to_next will clean this up. */
3612 *position = start_pos;
3613
3614 if (EQ (XCAR (prop), Qleft_fringe))
3615 {
3616 it->left_user_fringe_bitmap = fringe_bitmap;
3617 it->left_user_fringe_face_id = face_id;
3618 }
3619 else
3620 {
3621 it->right_user_fringe_bitmap = fringe_bitmap;
3622 it->right_user_fringe_face_id = face_id;
3623 }
3624 #endif /* HAVE_WINDOW_SYSTEM */
3625 return 1;
3626 }
3627
3628 location = Qunbound;
3629 if (CONSP (prop) && CONSP (XCAR (prop)))
3630 {
3631 Lisp_Object tem;
3632
3633 value = XCDR (prop);
3634 if (CONSP (value))
3635 value = XCAR (value);
3636
3637 tem = XCAR (prop);
3638 if (EQ (XCAR (tem), Qmargin)
3639 && (tem = XCDR (tem),
3640 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3641 (NILP (tem)
3642 || EQ (tem, Qleft_margin)
3643 || EQ (tem, Qright_margin))))
3644 location = tem;
3645 }
3646
3647 if (EQ (location, Qunbound))
3648 {
3649 location = Qnil;
3650 value = prop;
3651 }
3652
3653 valid_p = (STRINGP (value)
3654 #ifdef HAVE_WINDOW_SYSTEM
3655 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
3656 #endif /* not HAVE_WINDOW_SYSTEM */
3657 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3658
3659 if ((EQ (location, Qleft_margin)
3660 || EQ (location, Qright_margin)
3661 || NILP (location))
3662 && valid_p
3663 && !display_replaced_before_p)
3664 {
3665 replaces_text_display_p = 1;
3666
3667 /* Save current settings of IT so that we can restore them
3668 when we are finished with the glyph property value. */
3669 push_it (it);
3670
3671 if (NILP (location))
3672 it->area = TEXT_AREA;
3673 else if (EQ (location, Qleft_margin))
3674 it->area = LEFT_MARGIN_AREA;
3675 else
3676 it->area = RIGHT_MARGIN_AREA;
3677
3678 if (STRINGP (value))
3679 {
3680 it->string = value;
3681 it->multibyte_p = STRING_MULTIBYTE (it->string);
3682 it->current.overlay_string_index = -1;
3683 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3684 it->end_charpos = it->string_nchars = SCHARS (it->string);
3685 it->method = next_element_from_string;
3686 it->stop_charpos = 0;
3687 it->string_from_display_prop_p = 1;
3688 /* Say that we haven't consumed the characters with
3689 `display' property yet. The call to pop_it in
3690 set_iterator_to_next will clean this up. */
3691 *position = start_pos;
3692 }
3693 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3694 {
3695 it->method = next_element_from_stretch;
3696 it->object = value;
3697 it->current.pos = it->position = start_pos;
3698 }
3699 #ifdef HAVE_WINDOW_SYSTEM
3700 else
3701 {
3702 if (FRAME_WINDOW_P (it->f))
3703 {
3704 it->what = IT_IMAGE;
3705 it->image_id = lookup_image (it->f, value);
3706 it->position = start_pos;
3707 it->object = NILP (object) ? it->w->buffer : object;
3708 it->method = next_element_from_image;
3709 }
3710
3711 /* Say that we haven't consumed the characters with
3712 `display' property yet. The call to pop_it in
3713 set_iterator_to_next will clean this up. */
3714 *position = start_pos;
3715 }
3716 #endif /* HAVE_WINDOW_SYSTEM */
3717 }
3718 else
3719 /* Invalid property or property not supported. Restore
3720 the position to what it was before. */
3721 *position = start_pos;
3722 }
3723
3724 return replaces_text_display_p;
3725 }
3726
3727
3728 /* Check if PROP is a display sub-property value whose text should be
3729 treated as intangible. */
3730
3731 static int
3732 single_display_prop_intangible_p (prop)
3733 Lisp_Object prop;
3734 {
3735 /* Skip over `when FORM'. */
3736 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3737 {
3738 prop = XCDR (prop);
3739 if (!CONSP (prop))
3740 return 0;
3741 prop = XCDR (prop);
3742 }
3743
3744 if (STRINGP (prop))
3745 return 1;
3746
3747 if (!CONSP (prop))
3748 return 0;
3749
3750 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3751 we don't need to treat text as intangible. */
3752 if (EQ (XCAR (prop), Qmargin))
3753 {
3754 prop = XCDR (prop);
3755 if (!CONSP (prop))
3756 return 0;
3757
3758 prop = XCDR (prop);
3759 if (!CONSP (prop)
3760 || EQ (XCAR (prop), Qleft_margin)
3761 || EQ (XCAR (prop), Qright_margin))
3762 return 0;
3763 }
3764
3765 return (CONSP (prop)
3766 && (EQ (XCAR (prop), Qimage)
3767 || EQ (XCAR (prop), Qspace)));
3768 }
3769
3770
3771 /* Check if PROP is a display property value whose text should be
3772 treated as intangible. */
3773
3774 int
3775 display_prop_intangible_p (prop)
3776 Lisp_Object prop;
3777 {
3778 if (CONSP (prop)
3779 && CONSP (XCAR (prop))
3780 && !EQ (Qmargin, XCAR (XCAR (prop))))
3781 {
3782 /* A list of sub-properties. */
3783 while (CONSP (prop))
3784 {
3785 if (single_display_prop_intangible_p (XCAR (prop)))
3786 return 1;
3787 prop = XCDR (prop);
3788 }
3789 }
3790 else if (VECTORP (prop))
3791 {
3792 /* A vector of sub-properties. */
3793 int i;
3794 for (i = 0; i < ASIZE (prop); ++i)
3795 if (single_display_prop_intangible_p (AREF (prop, i)))
3796 return 1;
3797 }
3798 else
3799 return single_display_prop_intangible_p (prop);
3800
3801 return 0;
3802 }
3803
3804
3805 /* Return 1 if PROP is a display sub-property value containing STRING. */
3806
3807 static int
3808 single_display_prop_string_p (prop, string)
3809 Lisp_Object prop, string;
3810 {
3811 if (EQ (string, prop))
3812 return 1;
3813
3814 /* Skip over `when FORM'. */
3815 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3816 {
3817 prop = XCDR (prop);
3818 if (!CONSP (prop))
3819 return 0;
3820 prop = XCDR (prop);
3821 }
3822
3823 if (CONSP (prop))
3824 /* Skip over `margin LOCATION'. */
3825 if (EQ (XCAR (prop), Qmargin))
3826 {
3827 prop = XCDR (prop);
3828 if (!CONSP (prop))
3829 return 0;
3830
3831 prop = XCDR (prop);
3832 if (!CONSP (prop))
3833 return 0;
3834 }
3835
3836 return CONSP (prop) && EQ (XCAR (prop), string);
3837 }
3838
3839
3840 /* Return 1 if STRING appears in the `display' property PROP. */
3841
3842 static int
3843 display_prop_string_p (prop, string)
3844 Lisp_Object prop, string;
3845 {
3846 if (CONSP (prop)
3847 && CONSP (XCAR (prop))
3848 && !EQ (Qmargin, XCAR (XCAR (prop))))
3849 {
3850 /* A list of sub-properties. */
3851 while (CONSP (prop))
3852 {
3853 if (single_display_prop_string_p (XCAR (prop), string))
3854 return 1;
3855 prop = XCDR (prop);
3856 }
3857 }
3858 else if (VECTORP (prop))
3859 {
3860 /* A vector of sub-properties. */
3861 int i;
3862 for (i = 0; i < ASIZE (prop); ++i)
3863 if (single_display_prop_string_p (AREF (prop, i), string))
3864 return 1;
3865 }
3866 else
3867 return single_display_prop_string_p (prop, string);
3868
3869 return 0;
3870 }
3871
3872
3873 /* Determine from which buffer position in W's buffer STRING comes
3874 from. AROUND_CHARPOS is an approximate position where it could
3875 be from. Value is the buffer position or 0 if it couldn't be
3876 determined.
3877
3878 W's buffer must be current.
3879
3880 This function is necessary because we don't record buffer positions
3881 in glyphs generated from strings (to keep struct glyph small).
3882 This function may only use code that doesn't eval because it is
3883 called asynchronously from note_mouse_highlight. */
3884
3885 int
3886 string_buffer_position (w, string, around_charpos)
3887 struct window *w;
3888 Lisp_Object string;
3889 int around_charpos;
3890 {
3891 Lisp_Object limit, prop, pos;
3892 const int MAX_DISTANCE = 1000;
3893 int found = 0;
3894
3895 pos = make_number (around_charpos);
3896 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3897 while (!found && !EQ (pos, limit))
3898 {
3899 prop = Fget_char_property (pos, Qdisplay, Qnil);
3900 if (!NILP (prop) && display_prop_string_p (prop, string))
3901 found = 1;
3902 else
3903 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3904 }
3905
3906 if (!found)
3907 {
3908 pos = make_number (around_charpos);
3909 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3910 while (!found && !EQ (pos, limit))
3911 {
3912 prop = Fget_char_property (pos, Qdisplay, Qnil);
3913 if (!NILP (prop) && display_prop_string_p (prop, string))
3914 found = 1;
3915 else
3916 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3917 limit);
3918 }
3919 }
3920
3921 return found ? XINT (pos) : 0;
3922 }
3923
3924
3925 \f
3926 /***********************************************************************
3927 `composition' property
3928 ***********************************************************************/
3929
3930 /* Set up iterator IT from `composition' property at its current
3931 position. Called from handle_stop. */
3932
3933 static enum prop_handled
3934 handle_composition_prop (it)
3935 struct it *it;
3936 {
3937 Lisp_Object prop, string;
3938 int pos, pos_byte, end;
3939 enum prop_handled handled = HANDLED_NORMALLY;
3940
3941 if (STRINGP (it->string))
3942 {
3943 pos = IT_STRING_CHARPOS (*it);
3944 pos_byte = IT_STRING_BYTEPOS (*it);
3945 string = it->string;
3946 }
3947 else
3948 {
3949 pos = IT_CHARPOS (*it);
3950 pos_byte = IT_BYTEPOS (*it);
3951 string = Qnil;
3952 }
3953
3954 /* If there's a valid composition and point is not inside of the
3955 composition (in the case that the composition is from the current
3956 buffer), draw a glyph composed from the composition components. */
3957 if (find_composition (pos, -1, &pos, &end, &prop, string)
3958 && COMPOSITION_VALID_P (pos, end, prop)
3959 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3960 {
3961 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3962
3963 if (id >= 0)
3964 {
3965 it->method = next_element_from_composition;
3966 it->cmp_id = id;
3967 it->cmp_len = COMPOSITION_LENGTH (prop);
3968 /* For a terminal, draw only the first character of the
3969 components. */
3970 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3971 it->len = (STRINGP (it->string)
3972 ? string_char_to_byte (it->string, end)
3973 : CHAR_TO_BYTE (end)) - pos_byte;
3974 it->stop_charpos = end;
3975 handled = HANDLED_RETURN;
3976 }
3977 }
3978
3979 return handled;
3980 }
3981
3982
3983 \f
3984 /***********************************************************************
3985 Overlay strings
3986 ***********************************************************************/
3987
3988 /* The following structure is used to record overlay strings for
3989 later sorting in load_overlay_strings. */
3990
3991 struct overlay_entry
3992 {
3993 Lisp_Object overlay;
3994 Lisp_Object string;
3995 int priority;
3996 int after_string_p;
3997 };
3998
3999
4000 /* Set up iterator IT from overlay strings at its current position.
4001 Called from handle_stop. */
4002
4003 static enum prop_handled
4004 handle_overlay_change (it)
4005 struct it *it;
4006 {
4007 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4008 return HANDLED_RECOMPUTE_PROPS;
4009 else
4010 return HANDLED_NORMALLY;
4011 }
4012
4013
4014 /* Set up the next overlay string for delivery by IT, if there is an
4015 overlay string to deliver. Called by set_iterator_to_next when the
4016 end of the current overlay string is reached. If there are more
4017 overlay strings to display, IT->string and
4018 IT->current.overlay_string_index are set appropriately here.
4019 Otherwise IT->string is set to nil. */
4020
4021 static void
4022 next_overlay_string (it)
4023 struct it *it;
4024 {
4025 ++it->current.overlay_string_index;
4026 if (it->current.overlay_string_index == it->n_overlay_strings)
4027 {
4028 /* No more overlay strings. Restore IT's settings to what
4029 they were before overlay strings were processed, and
4030 continue to deliver from current_buffer. */
4031 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4032
4033 pop_it (it);
4034 xassert (it->stop_charpos >= BEGV
4035 && it->stop_charpos <= it->end_charpos);
4036 it->string = Qnil;
4037 it->current.overlay_string_index = -1;
4038 SET_TEXT_POS (it->current.string_pos, -1, -1);
4039 it->n_overlay_strings = 0;
4040 it->method = next_element_from_buffer;
4041
4042 /* If we're at the end of the buffer, record that we have
4043 processed the overlay strings there already, so that
4044 next_element_from_buffer doesn't try it again. */
4045 if (IT_CHARPOS (*it) >= it->end_charpos)
4046 it->overlay_strings_at_end_processed_p = 1;
4047
4048 /* If we have to display `...' for invisible text, set
4049 the iterator up for that. */
4050 if (display_ellipsis_p)
4051 setup_for_ellipsis (it);
4052 }
4053 else
4054 {
4055 /* There are more overlay strings to process. If
4056 IT->current.overlay_string_index has advanced to a position
4057 where we must load IT->overlay_strings with more strings, do
4058 it. */
4059 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4060
4061 if (it->current.overlay_string_index && i == 0)
4062 load_overlay_strings (it, 0);
4063
4064 /* Initialize IT to deliver display elements from the overlay
4065 string. */
4066 it->string = it->overlay_strings[i];
4067 it->multibyte_p = STRING_MULTIBYTE (it->string);
4068 SET_TEXT_POS (it->current.string_pos, 0, 0);
4069 it->method = next_element_from_string;
4070 it->stop_charpos = 0;
4071 }
4072
4073 CHECK_IT (it);
4074 }
4075
4076
4077 /* Compare two overlay_entry structures E1 and E2. Used as a
4078 comparison function for qsort in load_overlay_strings. Overlay
4079 strings for the same position are sorted so that
4080
4081 1. All after-strings come in front of before-strings, except
4082 when they come from the same overlay.
4083
4084 2. Within after-strings, strings are sorted so that overlay strings
4085 from overlays with higher priorities come first.
4086
4087 2. Within before-strings, strings are sorted so that overlay
4088 strings from overlays with higher priorities come last.
4089
4090 Value is analogous to strcmp. */
4091
4092
4093 static int
4094 compare_overlay_entries (e1, e2)
4095 void *e1, *e2;
4096 {
4097 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4098 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4099 int result;
4100
4101 if (entry1->after_string_p != entry2->after_string_p)
4102 {
4103 /* Let after-strings appear in front of before-strings if
4104 they come from different overlays. */
4105 if (EQ (entry1->overlay, entry2->overlay))
4106 result = entry1->after_string_p ? 1 : -1;
4107 else
4108 result = entry1->after_string_p ? -1 : 1;
4109 }
4110 else if (entry1->after_string_p)
4111 /* After-strings sorted in order of decreasing priority. */
4112 result = entry2->priority - entry1->priority;
4113 else
4114 /* Before-strings sorted in order of increasing priority. */
4115 result = entry1->priority - entry2->priority;
4116
4117 return result;
4118 }
4119
4120
4121 /* Load the vector IT->overlay_strings with overlay strings from IT's
4122 current buffer position, or from CHARPOS if that is > 0. Set
4123 IT->n_overlays to the total number of overlay strings found.
4124
4125 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4126 a time. On entry into load_overlay_strings,
4127 IT->current.overlay_string_index gives the number of overlay
4128 strings that have already been loaded by previous calls to this
4129 function.
4130
4131 IT->add_overlay_start contains an additional overlay start
4132 position to consider for taking overlay strings from, if non-zero.
4133 This position comes into play when the overlay has an `invisible'
4134 property, and both before and after-strings. When we've skipped to
4135 the end of the overlay, because of its `invisible' property, we
4136 nevertheless want its before-string to appear.
4137 IT->add_overlay_start will contain the overlay start position
4138 in this case.
4139
4140 Overlay strings are sorted so that after-string strings come in
4141 front of before-string strings. Within before and after-strings,
4142 strings are sorted by overlay priority. See also function
4143 compare_overlay_entries. */
4144
4145 static void
4146 load_overlay_strings (it, charpos)
4147 struct it *it;
4148 int charpos;
4149 {
4150 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4151 Lisp_Object overlay, window, str, invisible;
4152 struct Lisp_Overlay *ov;
4153 int start, end;
4154 int size = 20;
4155 int n = 0, i, j, invis_p;
4156 struct overlay_entry *entries
4157 = (struct overlay_entry *) alloca (size * sizeof *entries);
4158
4159 if (charpos <= 0)
4160 charpos = IT_CHARPOS (*it);
4161
4162 /* Append the overlay string STRING of overlay OVERLAY to vector
4163 `entries' which has size `size' and currently contains `n'
4164 elements. AFTER_P non-zero means STRING is an after-string of
4165 OVERLAY. */
4166 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4167 do \
4168 { \
4169 Lisp_Object priority; \
4170 \
4171 if (n == size) \
4172 { \
4173 int new_size = 2 * size; \
4174 struct overlay_entry *old = entries; \
4175 entries = \
4176 (struct overlay_entry *) alloca (new_size \
4177 * sizeof *entries); \
4178 bcopy (old, entries, size * sizeof *entries); \
4179 size = new_size; \
4180 } \
4181 \
4182 entries[n].string = (STRING); \
4183 entries[n].overlay = (OVERLAY); \
4184 priority = Foverlay_get ((OVERLAY), Qpriority); \
4185 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4186 entries[n].after_string_p = (AFTER_P); \
4187 ++n; \
4188 } \
4189 while (0)
4190
4191 /* Process overlay before the overlay center. */
4192 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4193 {
4194 XSETMISC (overlay, ov);
4195 xassert (OVERLAYP (overlay));
4196 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4197 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4198
4199 if (end < charpos)
4200 break;
4201
4202 /* Skip this overlay if it doesn't start or end at IT's current
4203 position. */
4204 if (end != charpos && start != charpos)
4205 continue;
4206
4207 /* Skip this overlay if it doesn't apply to IT->w. */
4208 window = Foverlay_get (overlay, Qwindow);
4209 if (WINDOWP (window) && XWINDOW (window) != it->w)
4210 continue;
4211
4212 /* If the text ``under'' the overlay is invisible, both before-
4213 and after-strings from this overlay are visible; start and
4214 end position are indistinguishable. */
4215 invisible = Foverlay_get (overlay, Qinvisible);
4216 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4217
4218 /* If overlay has a non-empty before-string, record it. */
4219 if ((start == charpos || (end == charpos && invis_p))
4220 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4221 && SCHARS (str))
4222 RECORD_OVERLAY_STRING (overlay, str, 0);
4223
4224 /* If overlay has a non-empty after-string, record it. */
4225 if ((end == charpos || (start == charpos && invis_p))
4226 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4227 && SCHARS (str))
4228 RECORD_OVERLAY_STRING (overlay, str, 1);
4229 }
4230
4231 /* Process overlays after the overlay center. */
4232 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4233 {
4234 XSETMISC (overlay, ov);
4235 xassert (OVERLAYP (overlay));
4236 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4237 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4238
4239 if (start > charpos)
4240 break;
4241
4242 /* Skip this overlay if it doesn't start or end at IT's current
4243 position. */
4244 if (end != charpos && start != charpos)
4245 continue;
4246
4247 /* Skip this overlay if it doesn't apply to IT->w. */
4248 window = Foverlay_get (overlay, Qwindow);
4249 if (WINDOWP (window) && XWINDOW (window) != it->w)
4250 continue;
4251
4252 /* If the text ``under'' the overlay is invisible, it has a zero
4253 dimension, and both before- and after-strings apply. */
4254 invisible = Foverlay_get (overlay, Qinvisible);
4255 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4256
4257 /* If overlay has a non-empty before-string, record it. */
4258 if ((start == charpos || (end == charpos && invis_p))
4259 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4260 && SCHARS (str))
4261 RECORD_OVERLAY_STRING (overlay, str, 0);
4262
4263 /* If overlay has a non-empty after-string, record it. */
4264 if ((end == charpos || (start == charpos && invis_p))
4265 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4266 && SCHARS (str))
4267 RECORD_OVERLAY_STRING (overlay, str, 1);
4268 }
4269
4270 #undef RECORD_OVERLAY_STRING
4271
4272 /* Sort entries. */
4273 if (n > 1)
4274 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4275
4276 /* Record the total number of strings to process. */
4277 it->n_overlay_strings = n;
4278
4279 /* IT->current.overlay_string_index is the number of overlay strings
4280 that have already been consumed by IT. Copy some of the
4281 remaining overlay strings to IT->overlay_strings. */
4282 i = 0;
4283 j = it->current.overlay_string_index;
4284 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4285 it->overlay_strings[i++] = entries[j++].string;
4286
4287 CHECK_IT (it);
4288 }
4289
4290
4291 /* Get the first chunk of overlay strings at IT's current buffer
4292 position, or at CHARPOS if that is > 0. Value is non-zero if at
4293 least one overlay string was found. */
4294
4295 static int
4296 get_overlay_strings (it, charpos)
4297 struct it *it;
4298 int charpos;
4299 {
4300 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4301 process. This fills IT->overlay_strings with strings, and sets
4302 IT->n_overlay_strings to the total number of strings to process.
4303 IT->pos.overlay_string_index has to be set temporarily to zero
4304 because load_overlay_strings needs this; it must be set to -1
4305 when no overlay strings are found because a zero value would
4306 indicate a position in the first overlay string. */
4307 it->current.overlay_string_index = 0;
4308 load_overlay_strings (it, charpos);
4309
4310 /* If we found overlay strings, set up IT to deliver display
4311 elements from the first one. Otherwise set up IT to deliver
4312 from current_buffer. */
4313 if (it->n_overlay_strings)
4314 {
4315 /* Make sure we know settings in current_buffer, so that we can
4316 restore meaningful values when we're done with the overlay
4317 strings. */
4318 compute_stop_pos (it);
4319 xassert (it->face_id >= 0);
4320
4321 /* Save IT's settings. They are restored after all overlay
4322 strings have been processed. */
4323 xassert (it->sp == 0);
4324 push_it (it);
4325
4326 /* Set up IT to deliver display elements from the first overlay
4327 string. */
4328 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4329 it->string = it->overlay_strings[0];
4330 it->stop_charpos = 0;
4331 xassert (STRINGP (it->string));
4332 it->end_charpos = SCHARS (it->string);
4333 it->multibyte_p = STRING_MULTIBYTE (it->string);
4334 it->method = next_element_from_string;
4335 }
4336 else
4337 {
4338 it->string = Qnil;
4339 it->current.overlay_string_index = -1;
4340 it->method = next_element_from_buffer;
4341 }
4342
4343 CHECK_IT (it);
4344
4345 /* Value is non-zero if we found at least one overlay string. */
4346 return STRINGP (it->string);
4347 }
4348
4349
4350 \f
4351 /***********************************************************************
4352 Saving and restoring state
4353 ***********************************************************************/
4354
4355 /* Save current settings of IT on IT->stack. Called, for example,
4356 before setting up IT for an overlay string, to be able to restore
4357 IT's settings to what they were after the overlay string has been
4358 processed. */
4359
4360 static void
4361 push_it (it)
4362 struct it *it;
4363 {
4364 struct iterator_stack_entry *p;
4365
4366 xassert (it->sp < 2);
4367 p = it->stack + it->sp;
4368
4369 p->stop_charpos = it->stop_charpos;
4370 xassert (it->face_id >= 0);
4371 p->face_id = it->face_id;
4372 p->string = it->string;
4373 p->pos = it->current;
4374 p->end_charpos = it->end_charpos;
4375 p->string_nchars = it->string_nchars;
4376 p->area = it->area;
4377 p->multibyte_p = it->multibyte_p;
4378 p->slice = it->slice;
4379 p->space_width = it->space_width;
4380 p->font_height = it->font_height;
4381 p->voffset = it->voffset;
4382 p->string_from_display_prop_p = it->string_from_display_prop_p;
4383 p->display_ellipsis_p = 0;
4384 ++it->sp;
4385 }
4386
4387
4388 /* Restore IT's settings from IT->stack. Called, for example, when no
4389 more overlay strings must be processed, and we return to delivering
4390 display elements from a buffer, or when the end of a string from a
4391 `display' property is reached and we return to delivering display
4392 elements from an overlay string, or from a buffer. */
4393
4394 static void
4395 pop_it (it)
4396 struct it *it;
4397 {
4398 struct iterator_stack_entry *p;
4399
4400 xassert (it->sp > 0);
4401 --it->sp;
4402 p = it->stack + it->sp;
4403 it->stop_charpos = p->stop_charpos;
4404 it->face_id = p->face_id;
4405 it->string = p->string;
4406 it->current = p->pos;
4407 it->end_charpos = p->end_charpos;
4408 it->string_nchars = p->string_nchars;
4409 it->area = p->area;
4410 it->multibyte_p = p->multibyte_p;
4411 it->slice = p->slice;
4412 it->space_width = p->space_width;
4413 it->font_height = p->font_height;
4414 it->voffset = p->voffset;
4415 it->string_from_display_prop_p = p->string_from_display_prop_p;
4416 }
4417
4418
4419 \f
4420 /***********************************************************************
4421 Moving over lines
4422 ***********************************************************************/
4423
4424 /* Set IT's current position to the previous line start. */
4425
4426 static void
4427 back_to_previous_line_start (it)
4428 struct it *it;
4429 {
4430 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4431 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4432 }
4433
4434
4435 /* Move IT to the next line start.
4436
4437 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4438 we skipped over part of the text (as opposed to moving the iterator
4439 continuously over the text). Otherwise, don't change the value
4440 of *SKIPPED_P.
4441
4442 Newlines may come from buffer text, overlay strings, or strings
4443 displayed via the `display' property. That's the reason we can't
4444 simply use find_next_newline_no_quit.
4445
4446 Note that this function may not skip over invisible text that is so
4447 because of text properties and immediately follows a newline. If
4448 it would, function reseat_at_next_visible_line_start, when called
4449 from set_iterator_to_next, would effectively make invisible
4450 characters following a newline part of the wrong glyph row, which
4451 leads to wrong cursor motion. */
4452
4453 static int
4454 forward_to_next_line_start (it, skipped_p)
4455 struct it *it;
4456 int *skipped_p;
4457 {
4458 int old_selective, newline_found_p, n;
4459 const int MAX_NEWLINE_DISTANCE = 500;
4460
4461 /* If already on a newline, just consume it to avoid unintended
4462 skipping over invisible text below. */
4463 if (it->what == IT_CHARACTER
4464 && it->c == '\n'
4465 && CHARPOS (it->position) == IT_CHARPOS (*it))
4466 {
4467 set_iterator_to_next (it, 0);
4468 it->c = 0;
4469 return 1;
4470 }
4471
4472 /* Don't handle selective display in the following. It's (a)
4473 unnecessary because it's done by the caller, and (b) leads to an
4474 infinite recursion because next_element_from_ellipsis indirectly
4475 calls this function. */
4476 old_selective = it->selective;
4477 it->selective = 0;
4478
4479 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4480 from buffer text. */
4481 for (n = newline_found_p = 0;
4482 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4483 n += STRINGP (it->string) ? 0 : 1)
4484 {
4485 if (!get_next_display_element (it))
4486 return 0;
4487 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4488 set_iterator_to_next (it, 0);
4489 }
4490
4491 /* If we didn't find a newline near enough, see if we can use a
4492 short-cut. */
4493 if (!newline_found_p)
4494 {
4495 int start = IT_CHARPOS (*it);
4496 int limit = find_next_newline_no_quit (start, 1);
4497 Lisp_Object pos;
4498
4499 xassert (!STRINGP (it->string));
4500
4501 /* If there isn't any `display' property in sight, and no
4502 overlays, we can just use the position of the newline in
4503 buffer text. */
4504 if (it->stop_charpos >= limit
4505 || ((pos = Fnext_single_property_change (make_number (start),
4506 Qdisplay,
4507 Qnil, make_number (limit)),
4508 NILP (pos))
4509 && next_overlay_change (start) == ZV))
4510 {
4511 IT_CHARPOS (*it) = limit;
4512 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4513 *skipped_p = newline_found_p = 1;
4514 }
4515 else
4516 {
4517 while (get_next_display_element (it)
4518 && !newline_found_p)
4519 {
4520 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4521 set_iterator_to_next (it, 0);
4522 }
4523 }
4524 }
4525
4526 it->selective = old_selective;
4527 return newline_found_p;
4528 }
4529
4530
4531 /* Set IT's current position to the previous visible line start. Skip
4532 invisible text that is so either due to text properties or due to
4533 selective display. Caution: this does not change IT->current_x and
4534 IT->hpos. */
4535
4536 static void
4537 back_to_previous_visible_line_start (it)
4538 struct it *it;
4539 {
4540 int visible_p = 0;
4541
4542 /* Go back one newline if not on BEGV already. */
4543 if (IT_CHARPOS (*it) > BEGV)
4544 back_to_previous_line_start (it);
4545
4546 /* Move over lines that are invisible because of selective display
4547 or text properties. */
4548 while (IT_CHARPOS (*it) > BEGV
4549 && !visible_p)
4550 {
4551 visible_p = 1;
4552
4553 /* If selective > 0, then lines indented more than that values
4554 are invisible. */
4555 if (it->selective > 0
4556 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4557 (double) it->selective)) /* iftc */
4558 visible_p = 0;
4559 else
4560 {
4561 Lisp_Object prop;
4562
4563 /* Check the newline before point for invisibility. */
4564 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4565 Qinvisible, it->window);
4566 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4567 visible_p = 0;
4568 }
4569
4570 if (visible_p)
4571 {
4572 struct it it2 = *it;
4573
4574 if (handle_display_prop (&it2) == HANDLED_RETURN)
4575 visible_p = 0;
4576 }
4577
4578 /* Back one more newline if the current one is invisible. */
4579 if (!visible_p)
4580 back_to_previous_line_start (it);
4581 }
4582
4583 xassert (IT_CHARPOS (*it) >= BEGV);
4584 xassert (IT_CHARPOS (*it) == BEGV
4585 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4586 CHECK_IT (it);
4587 }
4588
4589
4590 /* Reseat iterator IT at the previous visible line start. Skip
4591 invisible text that is so either due to text properties or due to
4592 selective display. At the end, update IT's overlay information,
4593 face information etc. */
4594
4595 void
4596 reseat_at_previous_visible_line_start (it)
4597 struct it *it;
4598 {
4599 back_to_previous_visible_line_start (it);
4600 reseat (it, it->current.pos, 1);
4601 CHECK_IT (it);
4602 }
4603
4604
4605 /* Reseat iterator IT on the next visible line start in the current
4606 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4607 preceding the line start. Skip over invisible text that is so
4608 because of selective display. Compute faces, overlays etc at the
4609 new position. Note that this function does not skip over text that
4610 is invisible because of text properties. */
4611
4612 static void
4613 reseat_at_next_visible_line_start (it, on_newline_p)
4614 struct it *it;
4615 int on_newline_p;
4616 {
4617 int newline_found_p, skipped_p = 0;
4618
4619 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4620
4621 /* Skip over lines that are invisible because they are indented
4622 more than the value of IT->selective. */
4623 if (it->selective > 0)
4624 while (IT_CHARPOS (*it) < ZV
4625 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4626 (double) it->selective)) /* iftc */
4627 {
4628 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4629 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4630 }
4631
4632 /* Position on the newline if that's what's requested. */
4633 if (on_newline_p && newline_found_p)
4634 {
4635 if (STRINGP (it->string))
4636 {
4637 if (IT_STRING_CHARPOS (*it) > 0)
4638 {
4639 --IT_STRING_CHARPOS (*it);
4640 --IT_STRING_BYTEPOS (*it);
4641 }
4642 }
4643 else if (IT_CHARPOS (*it) > BEGV)
4644 {
4645 --IT_CHARPOS (*it);
4646 --IT_BYTEPOS (*it);
4647 reseat (it, it->current.pos, 0);
4648 }
4649 }
4650 else if (skipped_p)
4651 reseat (it, it->current.pos, 0);
4652
4653 CHECK_IT (it);
4654 }
4655
4656
4657 \f
4658 /***********************************************************************
4659 Changing an iterator's position
4660 ***********************************************************************/
4661
4662 /* Change IT's current position to POS in current_buffer. If FORCE_P
4663 is non-zero, always check for text properties at the new position.
4664 Otherwise, text properties are only looked up if POS >=
4665 IT->check_charpos of a property. */
4666
4667 static void
4668 reseat (it, pos, force_p)
4669 struct it *it;
4670 struct text_pos pos;
4671 int force_p;
4672 {
4673 int original_pos = IT_CHARPOS (*it);
4674
4675 reseat_1 (it, pos, 0);
4676
4677 /* Determine where to check text properties. Avoid doing it
4678 where possible because text property lookup is very expensive. */
4679 if (force_p
4680 || CHARPOS (pos) > it->stop_charpos
4681 || CHARPOS (pos) < original_pos)
4682 handle_stop (it);
4683
4684 CHECK_IT (it);
4685 }
4686
4687
4688 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4689 IT->stop_pos to POS, also. */
4690
4691 static void
4692 reseat_1 (it, pos, set_stop_p)
4693 struct it *it;
4694 struct text_pos pos;
4695 int set_stop_p;
4696 {
4697 /* Don't call this function when scanning a C string. */
4698 xassert (it->s == NULL);
4699
4700 /* POS must be a reasonable value. */
4701 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4702
4703 it->current.pos = it->position = pos;
4704 XSETBUFFER (it->object, current_buffer);
4705 it->end_charpos = ZV;
4706 it->dpvec = NULL;
4707 it->current.dpvec_index = -1;
4708 it->current.overlay_string_index = -1;
4709 IT_STRING_CHARPOS (*it) = -1;
4710 IT_STRING_BYTEPOS (*it) = -1;
4711 it->string = Qnil;
4712 it->method = next_element_from_buffer;
4713 /* RMS: I added this to fix a bug in move_it_vertically_backward
4714 where it->area continued to relate to the starting point
4715 for the backward motion. Bug report from
4716 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4717 However, I am not sure whether reseat still does the right thing
4718 in general after this change. */
4719 it->area = TEXT_AREA;
4720 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4721 it->sp = 0;
4722 it->face_before_selective_p = 0;
4723
4724 if (set_stop_p)
4725 it->stop_charpos = CHARPOS (pos);
4726 }
4727
4728
4729 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4730 If S is non-null, it is a C string to iterate over. Otherwise,
4731 STRING gives a Lisp string to iterate over.
4732
4733 If PRECISION > 0, don't return more then PRECISION number of
4734 characters from the string.
4735
4736 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4737 characters have been returned. FIELD_WIDTH < 0 means an infinite
4738 field width.
4739
4740 MULTIBYTE = 0 means disable processing of multibyte characters,
4741 MULTIBYTE > 0 means enable it,
4742 MULTIBYTE < 0 means use IT->multibyte_p.
4743
4744 IT must be initialized via a prior call to init_iterator before
4745 calling this function. */
4746
4747 static void
4748 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4749 struct it *it;
4750 unsigned char *s;
4751 Lisp_Object string;
4752 int charpos;
4753 int precision, field_width, multibyte;
4754 {
4755 /* No region in strings. */
4756 it->region_beg_charpos = it->region_end_charpos = -1;
4757
4758 /* No text property checks performed by default, but see below. */
4759 it->stop_charpos = -1;
4760
4761 /* Set iterator position and end position. */
4762 bzero (&it->current, sizeof it->current);
4763 it->current.overlay_string_index = -1;
4764 it->current.dpvec_index = -1;
4765 xassert (charpos >= 0);
4766
4767 /* If STRING is specified, use its multibyteness, otherwise use the
4768 setting of MULTIBYTE, if specified. */
4769 if (multibyte >= 0)
4770 it->multibyte_p = multibyte > 0;
4771
4772 if (s == NULL)
4773 {
4774 xassert (STRINGP (string));
4775 it->string = string;
4776 it->s = NULL;
4777 it->end_charpos = it->string_nchars = SCHARS (string);
4778 it->method = next_element_from_string;
4779 it->current.string_pos = string_pos (charpos, string);
4780 }
4781 else
4782 {
4783 it->s = s;
4784 it->string = Qnil;
4785
4786 /* Note that we use IT->current.pos, not it->current.string_pos,
4787 for displaying C strings. */
4788 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4789 if (it->multibyte_p)
4790 {
4791 it->current.pos = c_string_pos (charpos, s, 1);
4792 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4793 }
4794 else
4795 {
4796 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4797 it->end_charpos = it->string_nchars = strlen (s);
4798 }
4799
4800 it->method = next_element_from_c_string;
4801 }
4802
4803 /* PRECISION > 0 means don't return more than PRECISION characters
4804 from the string. */
4805 if (precision > 0 && it->end_charpos - charpos > precision)
4806 it->end_charpos = it->string_nchars = charpos + precision;
4807
4808 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4809 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4810 FIELD_WIDTH < 0 means infinite field width. This is useful for
4811 padding with `-' at the end of a mode line. */
4812 if (field_width < 0)
4813 field_width = INFINITY;
4814 if (field_width > it->end_charpos - charpos)
4815 it->end_charpos = charpos + field_width;
4816
4817 /* Use the standard display table for displaying strings. */
4818 if (DISP_TABLE_P (Vstandard_display_table))
4819 it->dp = XCHAR_TABLE (Vstandard_display_table);
4820
4821 it->stop_charpos = charpos;
4822 CHECK_IT (it);
4823 }
4824
4825
4826 \f
4827 /***********************************************************************
4828 Iteration
4829 ***********************************************************************/
4830
4831 /* Load IT's display element fields with information about the next
4832 display element from the current position of IT. Value is zero if
4833 end of buffer (or C string) is reached. */
4834
4835 int
4836 get_next_display_element (it)
4837 struct it *it;
4838 {
4839 /* Non-zero means that we found a display element. Zero means that
4840 we hit the end of what we iterate over. Performance note: the
4841 function pointer `method' used here turns out to be faster than
4842 using a sequence of if-statements. */
4843 int success_p = (*it->method) (it);
4844
4845 if (it->what == IT_CHARACTER)
4846 {
4847 /* Map via display table or translate control characters.
4848 IT->c, IT->len etc. have been set to the next character by
4849 the function call above. If we have a display table, and it
4850 contains an entry for IT->c, translate it. Don't do this if
4851 IT->c itself comes from a display table, otherwise we could
4852 end up in an infinite recursion. (An alternative could be to
4853 count the recursion depth of this function and signal an
4854 error when a certain maximum depth is reached.) Is it worth
4855 it? */
4856 if (success_p && it->dpvec == NULL)
4857 {
4858 Lisp_Object dv;
4859
4860 if (it->dp
4861 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4862 VECTORP (dv)))
4863 {
4864 struct Lisp_Vector *v = XVECTOR (dv);
4865
4866 /* Return the first character from the display table
4867 entry, if not empty. If empty, don't display the
4868 current character. */
4869 if (v->size)
4870 {
4871 it->dpvec_char_len = it->len;
4872 it->dpvec = v->contents;
4873 it->dpend = v->contents + v->size;
4874 it->current.dpvec_index = 0;
4875 it->method = next_element_from_display_vector;
4876 success_p = get_next_display_element (it);
4877 }
4878 else
4879 {
4880 set_iterator_to_next (it, 0);
4881 success_p = get_next_display_element (it);
4882 }
4883 }
4884
4885 /* Translate control characters into `\003' or `^C' form.
4886 Control characters coming from a display table entry are
4887 currently not translated because we use IT->dpvec to hold
4888 the translation. This could easily be changed but I
4889 don't believe that it is worth doing.
4890
4891 If it->multibyte_p is nonzero, eight-bit characters and
4892 non-printable multibyte characters are also translated to
4893 octal form.
4894
4895 If it->multibyte_p is zero, eight-bit characters that
4896 don't have corresponding multibyte char code are also
4897 translated to octal form. */
4898 else if ((it->c < ' '
4899 && (it->area != TEXT_AREA
4900 /* In mode line, treat \n like other crl chars. */
4901 || (it->c != '\n'
4902 && it->glyph_row && it->glyph_row->mode_line_p)
4903 || (it->c != '\n' && it->c != '\t')))
4904 || (it->multibyte_p
4905 ? ((it->c >= 127
4906 && it->len == 1)
4907 || !CHAR_PRINTABLE_P (it->c))
4908 : (it->c >= 127
4909 && (!unibyte_display_via_language_environment
4910 || it->c == unibyte_char_to_multibyte (it->c)))))
4911 {
4912 /* IT->c is a control character which must be displayed
4913 either as '\003' or as `^C' where the '\\' and '^'
4914 can be defined in the display table. Fill
4915 IT->ctl_chars with glyphs for what we have to
4916 display. Then, set IT->dpvec to these glyphs. */
4917 GLYPH g;
4918
4919 if (it->c < 128 && it->ctl_arrow_p)
4920 {
4921 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4922 if (it->dp
4923 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4924 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4925 g = XINT (DISP_CTRL_GLYPH (it->dp));
4926 else
4927 g = FAST_MAKE_GLYPH ('^', 0);
4928 XSETINT (it->ctl_chars[0], g);
4929
4930 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4931 XSETINT (it->ctl_chars[1], g);
4932
4933 /* Set up IT->dpvec and return first character from it. */
4934 it->dpvec_char_len = it->len;
4935 it->dpvec = it->ctl_chars;
4936 it->dpend = it->dpvec + 2;
4937 it->current.dpvec_index = 0;
4938 it->method = next_element_from_display_vector;
4939 get_next_display_element (it);
4940 }
4941 else
4942 {
4943 unsigned char str[MAX_MULTIBYTE_LENGTH];
4944 int len;
4945 int i;
4946 GLYPH escape_glyph;
4947
4948 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4949 if (it->dp
4950 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4951 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4952 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4953 else
4954 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4955
4956 if (SINGLE_BYTE_CHAR_P (it->c))
4957 str[0] = it->c, len = 1;
4958 else
4959 {
4960 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4961 if (len < 0)
4962 {
4963 /* It's an invalid character, which
4964 shouldn't happen actually, but due to
4965 bugs it may happen. Let's print the char
4966 as is, there's not much meaningful we can
4967 do with it. */
4968 str[0] = it->c;
4969 str[1] = it->c >> 8;
4970 str[2] = it->c >> 16;
4971 str[3] = it->c >> 24;
4972 len = 4;
4973 }
4974 }
4975
4976 for (i = 0; i < len; i++)
4977 {
4978 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4979 /* Insert three more glyphs into IT->ctl_chars for
4980 the octal display of the character. */
4981 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4982 XSETINT (it->ctl_chars[i * 4 + 1], g);
4983 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4984 XSETINT (it->ctl_chars[i * 4 + 2], g);
4985 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4986 XSETINT (it->ctl_chars[i * 4 + 3], g);
4987 }
4988
4989 /* Set up IT->dpvec and return the first character
4990 from it. */
4991 it->dpvec_char_len = it->len;
4992 it->dpvec = it->ctl_chars;
4993 it->dpend = it->dpvec + len * 4;
4994 it->current.dpvec_index = 0;
4995 it->method = next_element_from_display_vector;
4996 get_next_display_element (it);
4997 }
4998 }
4999 }
5000
5001 /* Adjust face id for a multibyte character. There are no
5002 multibyte character in unibyte text. */
5003 if (it->multibyte_p
5004 && success_p
5005 && FRAME_WINDOW_P (it->f))
5006 {
5007 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5008 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5009 }
5010 }
5011
5012 /* Is this character the last one of a run of characters with
5013 box? If yes, set IT->end_of_box_run_p to 1. */
5014 if (it->face_box_p
5015 && it->s == NULL)
5016 {
5017 int face_id;
5018 struct face *face;
5019
5020 it->end_of_box_run_p
5021 = ((face_id = face_after_it_pos (it),
5022 face_id != it->face_id)
5023 && (face = FACE_FROM_ID (it->f, face_id),
5024 face->box == FACE_NO_BOX));
5025 }
5026
5027 /* Value is 0 if end of buffer or string reached. */
5028 return success_p;
5029 }
5030
5031
5032 /* Move IT to the next display element.
5033
5034 RESEAT_P non-zero means if called on a newline in buffer text,
5035 skip to the next visible line start.
5036
5037 Functions get_next_display_element and set_iterator_to_next are
5038 separate because I find this arrangement easier to handle than a
5039 get_next_display_element function that also increments IT's
5040 position. The way it is we can first look at an iterator's current
5041 display element, decide whether it fits on a line, and if it does,
5042 increment the iterator position. The other way around we probably
5043 would either need a flag indicating whether the iterator has to be
5044 incremented the next time, or we would have to implement a
5045 decrement position function which would not be easy to write. */
5046
5047 void
5048 set_iterator_to_next (it, reseat_p)
5049 struct it *it;
5050 int reseat_p;
5051 {
5052 /* Reset flags indicating start and end of a sequence of characters
5053 with box. Reset them at the start of this function because
5054 moving the iterator to a new position might set them. */
5055 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5056
5057 if (it->method == next_element_from_buffer)
5058 {
5059 /* The current display element of IT is a character from
5060 current_buffer. Advance in the buffer, and maybe skip over
5061 invisible lines that are so because of selective display. */
5062 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5063 reseat_at_next_visible_line_start (it, 0);
5064 else
5065 {
5066 xassert (it->len != 0);
5067 IT_BYTEPOS (*it) += it->len;
5068 IT_CHARPOS (*it) += 1;
5069 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5070 }
5071 }
5072 else if (it->method == next_element_from_composition)
5073 {
5074 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5075 if (STRINGP (it->string))
5076 {
5077 IT_STRING_BYTEPOS (*it) += it->len;
5078 IT_STRING_CHARPOS (*it) += it->cmp_len;
5079 it->method = next_element_from_string;
5080 goto consider_string_end;
5081 }
5082 else
5083 {
5084 IT_BYTEPOS (*it) += it->len;
5085 IT_CHARPOS (*it) += it->cmp_len;
5086 it->method = next_element_from_buffer;
5087 }
5088 }
5089 else if (it->method == next_element_from_c_string)
5090 {
5091 /* Current display element of IT is from a C string. */
5092 IT_BYTEPOS (*it) += it->len;
5093 IT_CHARPOS (*it) += 1;
5094 }
5095 else if (it->method == next_element_from_display_vector)
5096 {
5097 /* Current display element of IT is from a display table entry.
5098 Advance in the display table definition. Reset it to null if
5099 end reached, and continue with characters from buffers/
5100 strings. */
5101 ++it->current.dpvec_index;
5102
5103 /* Restore face of the iterator to what they were before the
5104 display vector entry (these entries may contain faces). */
5105 it->face_id = it->saved_face_id;
5106
5107 if (it->dpvec + it->current.dpvec_index == it->dpend)
5108 {
5109 if (it->s)
5110 it->method = next_element_from_c_string;
5111 else if (STRINGP (it->string))
5112 it->method = next_element_from_string;
5113 else
5114 it->method = next_element_from_buffer;
5115
5116 it->dpvec = NULL;
5117 it->current.dpvec_index = -1;
5118
5119 /* Skip over characters which were displayed via IT->dpvec. */
5120 if (it->dpvec_char_len < 0)
5121 reseat_at_next_visible_line_start (it, 1);
5122 else if (it->dpvec_char_len > 0)
5123 {
5124 it->len = it->dpvec_char_len;
5125 set_iterator_to_next (it, reseat_p);
5126 }
5127 }
5128 }
5129 else if (it->method == next_element_from_string)
5130 {
5131 /* Current display element is a character from a Lisp string. */
5132 xassert (it->s == NULL && STRINGP (it->string));
5133 IT_STRING_BYTEPOS (*it) += it->len;
5134 IT_STRING_CHARPOS (*it) += 1;
5135
5136 consider_string_end:
5137
5138 if (it->current.overlay_string_index >= 0)
5139 {
5140 /* IT->string is an overlay string. Advance to the
5141 next, if there is one. */
5142 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5143 next_overlay_string (it);
5144 }
5145 else
5146 {
5147 /* IT->string is not an overlay string. If we reached
5148 its end, and there is something on IT->stack, proceed
5149 with what is on the stack. This can be either another
5150 string, this time an overlay string, or a buffer. */
5151 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5152 && it->sp > 0)
5153 {
5154 pop_it (it);
5155 if (!STRINGP (it->string))
5156 it->method = next_element_from_buffer;
5157 else
5158 goto consider_string_end;
5159 }
5160 }
5161 }
5162 else if (it->method == next_element_from_image
5163 || it->method == next_element_from_stretch)
5164 {
5165 /* The position etc with which we have to proceed are on
5166 the stack. The position may be at the end of a string,
5167 if the `display' property takes up the whole string. */
5168 pop_it (it);
5169 it->image_id = 0;
5170 if (STRINGP (it->string))
5171 {
5172 it->method = next_element_from_string;
5173 goto consider_string_end;
5174 }
5175 else
5176 it->method = next_element_from_buffer;
5177 }
5178 else
5179 /* There are no other methods defined, so this should be a bug. */
5180 abort ();
5181
5182 xassert (it->method != next_element_from_string
5183 || (STRINGP (it->string)
5184 && IT_STRING_CHARPOS (*it) >= 0));
5185 }
5186
5187
5188 /* Load IT's display element fields with information about the next
5189 display element which comes from a display table entry or from the
5190 result of translating a control character to one of the forms `^C'
5191 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5192
5193 static int
5194 next_element_from_display_vector (it)
5195 struct it *it;
5196 {
5197 /* Precondition. */
5198 xassert (it->dpvec && it->current.dpvec_index >= 0);
5199
5200 /* Remember the current face id in case glyphs specify faces.
5201 IT's face is restored in set_iterator_to_next. */
5202 it->saved_face_id = it->face_id;
5203
5204 if (INTEGERP (*it->dpvec)
5205 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5206 {
5207 int lface_id;
5208 GLYPH g;
5209
5210 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5211 it->c = FAST_GLYPH_CHAR (g);
5212 it->len = CHAR_BYTES (it->c);
5213
5214 /* The entry may contain a face id to use. Such a face id is
5215 the id of a Lisp face, not a realized face. A face id of
5216 zero means no face is specified. */
5217 lface_id = FAST_GLYPH_FACE (g);
5218 if (lface_id)
5219 {
5220 /* The function returns -1 if lface_id is invalid. */
5221 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5222 if (face_id >= 0)
5223 it->face_id = face_id;
5224 }
5225 }
5226 else
5227 /* Display table entry is invalid. Return a space. */
5228 it->c = ' ', it->len = 1;
5229
5230 /* Don't change position and object of the iterator here. They are
5231 still the values of the character that had this display table
5232 entry or was translated, and that's what we want. */
5233 it->what = IT_CHARACTER;
5234 return 1;
5235 }
5236
5237
5238 /* Load IT with the next display element from Lisp string IT->string.
5239 IT->current.string_pos is the current position within the string.
5240 If IT->current.overlay_string_index >= 0, the Lisp string is an
5241 overlay string. */
5242
5243 static int
5244 next_element_from_string (it)
5245 struct it *it;
5246 {
5247 struct text_pos position;
5248
5249 xassert (STRINGP (it->string));
5250 xassert (IT_STRING_CHARPOS (*it) >= 0);
5251 position = it->current.string_pos;
5252
5253 /* Time to check for invisible text? */
5254 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5255 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5256 {
5257 handle_stop (it);
5258
5259 /* Since a handler may have changed IT->method, we must
5260 recurse here. */
5261 return get_next_display_element (it);
5262 }
5263
5264 if (it->current.overlay_string_index >= 0)
5265 {
5266 /* Get the next character from an overlay string. In overlay
5267 strings, There is no field width or padding with spaces to
5268 do. */
5269 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5270 {
5271 it->what = IT_EOB;
5272 return 0;
5273 }
5274 else if (STRING_MULTIBYTE (it->string))
5275 {
5276 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5277 const unsigned char *s = (SDATA (it->string)
5278 + IT_STRING_BYTEPOS (*it));
5279 it->c = string_char_and_length (s, remaining, &it->len);
5280 }
5281 else
5282 {
5283 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5284 it->len = 1;
5285 }
5286 }
5287 else
5288 {
5289 /* Get the next character from a Lisp string that is not an
5290 overlay string. Such strings come from the mode line, for
5291 example. We may have to pad with spaces, or truncate the
5292 string. See also next_element_from_c_string. */
5293 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5294 {
5295 it->what = IT_EOB;
5296 return 0;
5297 }
5298 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5299 {
5300 /* Pad with spaces. */
5301 it->c = ' ', it->len = 1;
5302 CHARPOS (position) = BYTEPOS (position) = -1;
5303 }
5304 else if (STRING_MULTIBYTE (it->string))
5305 {
5306 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5307 const unsigned char *s = (SDATA (it->string)
5308 + IT_STRING_BYTEPOS (*it));
5309 it->c = string_char_and_length (s, maxlen, &it->len);
5310 }
5311 else
5312 {
5313 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5314 it->len = 1;
5315 }
5316 }
5317
5318 /* Record what we have and where it came from. Note that we store a
5319 buffer position in IT->position although it could arguably be a
5320 string position. */
5321 it->what = IT_CHARACTER;
5322 it->object = it->string;
5323 it->position = position;
5324 return 1;
5325 }
5326
5327
5328 /* Load IT with next display element from C string IT->s.
5329 IT->string_nchars is the maximum number of characters to return
5330 from the string. IT->end_charpos may be greater than
5331 IT->string_nchars when this function is called, in which case we
5332 may have to return padding spaces. Value is zero if end of string
5333 reached, including padding spaces. */
5334
5335 static int
5336 next_element_from_c_string (it)
5337 struct it *it;
5338 {
5339 int success_p = 1;
5340
5341 xassert (it->s);
5342 it->what = IT_CHARACTER;
5343 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5344 it->object = Qnil;
5345
5346 /* IT's position can be greater IT->string_nchars in case a field
5347 width or precision has been specified when the iterator was
5348 initialized. */
5349 if (IT_CHARPOS (*it) >= it->end_charpos)
5350 {
5351 /* End of the game. */
5352 it->what = IT_EOB;
5353 success_p = 0;
5354 }
5355 else if (IT_CHARPOS (*it) >= it->string_nchars)
5356 {
5357 /* Pad with spaces. */
5358 it->c = ' ', it->len = 1;
5359 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5360 }
5361 else if (it->multibyte_p)
5362 {
5363 /* Implementation note: The calls to strlen apparently aren't a
5364 performance problem because there is no noticeable performance
5365 difference between Emacs running in unibyte or multibyte mode. */
5366 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5367 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5368 maxlen, &it->len);
5369 }
5370 else
5371 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5372
5373 return success_p;
5374 }
5375
5376
5377 /* Set up IT to return characters from an ellipsis, if appropriate.
5378 The definition of the ellipsis glyphs may come from a display table
5379 entry. This function Fills IT with the first glyph from the
5380 ellipsis if an ellipsis is to be displayed. */
5381
5382 static int
5383 next_element_from_ellipsis (it)
5384 struct it *it;
5385 {
5386 if (it->selective_display_ellipsis_p)
5387 {
5388 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5389 {
5390 /* Use the display table definition for `...'. Invalid glyphs
5391 will be handled by the method returning elements from dpvec. */
5392 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5393 it->dpvec_char_len = it->len;
5394 it->dpvec = v->contents;
5395 it->dpend = v->contents + v->size;
5396 it->current.dpvec_index = 0;
5397 it->method = next_element_from_display_vector;
5398 }
5399 else
5400 {
5401 /* Use default `...' which is stored in default_invis_vector. */
5402 it->dpvec_char_len = it->len;
5403 it->dpvec = default_invis_vector;
5404 it->dpend = default_invis_vector + 3;
5405 it->current.dpvec_index = 0;
5406 it->method = next_element_from_display_vector;
5407 }
5408 }
5409 else
5410 {
5411 /* The face at the current position may be different from the
5412 face we find after the invisible text. Remember what it
5413 was in IT->saved_face_id, and signal that it's there by
5414 setting face_before_selective_p. */
5415 it->saved_face_id = it->face_id;
5416 it->method = next_element_from_buffer;
5417 reseat_at_next_visible_line_start (it, 1);
5418 it->face_before_selective_p = 1;
5419 }
5420
5421 return get_next_display_element (it);
5422 }
5423
5424
5425 /* Deliver an image display element. The iterator IT is already
5426 filled with image information (done in handle_display_prop). Value
5427 is always 1. */
5428
5429
5430 static int
5431 next_element_from_image (it)
5432 struct it *it;
5433 {
5434 it->what = IT_IMAGE;
5435 return 1;
5436 }
5437
5438
5439 /* Fill iterator IT with next display element from a stretch glyph
5440 property. IT->object is the value of the text property. Value is
5441 always 1. */
5442
5443 static int
5444 next_element_from_stretch (it)
5445 struct it *it;
5446 {
5447 it->what = IT_STRETCH;
5448 return 1;
5449 }
5450
5451
5452 /* Load IT with the next display element from current_buffer. Value
5453 is zero if end of buffer reached. IT->stop_charpos is the next
5454 position at which to stop and check for text properties or buffer
5455 end. */
5456
5457 static int
5458 next_element_from_buffer (it)
5459 struct it *it;
5460 {
5461 int success_p = 1;
5462
5463 /* Check this assumption, otherwise, we would never enter the
5464 if-statement, below. */
5465 xassert (IT_CHARPOS (*it) >= BEGV
5466 && IT_CHARPOS (*it) <= it->stop_charpos);
5467
5468 if (IT_CHARPOS (*it) >= it->stop_charpos)
5469 {
5470 if (IT_CHARPOS (*it) >= it->end_charpos)
5471 {
5472 int overlay_strings_follow_p;
5473
5474 /* End of the game, except when overlay strings follow that
5475 haven't been returned yet. */
5476 if (it->overlay_strings_at_end_processed_p)
5477 overlay_strings_follow_p = 0;
5478 else
5479 {
5480 it->overlay_strings_at_end_processed_p = 1;
5481 overlay_strings_follow_p = get_overlay_strings (it, 0);
5482 }
5483
5484 if (overlay_strings_follow_p)
5485 success_p = get_next_display_element (it);
5486 else
5487 {
5488 it->what = IT_EOB;
5489 it->position = it->current.pos;
5490 success_p = 0;
5491 }
5492 }
5493 else
5494 {
5495 handle_stop (it);
5496 return get_next_display_element (it);
5497 }
5498 }
5499 else
5500 {
5501 /* No face changes, overlays etc. in sight, so just return a
5502 character from current_buffer. */
5503 unsigned char *p;
5504
5505 /* Maybe run the redisplay end trigger hook. Performance note:
5506 This doesn't seem to cost measurable time. */
5507 if (it->redisplay_end_trigger_charpos
5508 && it->glyph_row
5509 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5510 run_redisplay_end_trigger_hook (it);
5511
5512 /* Get the next character, maybe multibyte. */
5513 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5514 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5515 {
5516 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5517 - IT_BYTEPOS (*it));
5518 it->c = string_char_and_length (p, maxlen, &it->len);
5519 }
5520 else
5521 it->c = *p, it->len = 1;
5522
5523 /* Record what we have and where it came from. */
5524 it->what = IT_CHARACTER;;
5525 it->object = it->w->buffer;
5526 it->position = it->current.pos;
5527
5528 /* Normally we return the character found above, except when we
5529 really want to return an ellipsis for selective display. */
5530 if (it->selective)
5531 {
5532 if (it->c == '\n')
5533 {
5534 /* A value of selective > 0 means hide lines indented more
5535 than that number of columns. */
5536 if (it->selective > 0
5537 && IT_CHARPOS (*it) + 1 < ZV
5538 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5539 IT_BYTEPOS (*it) + 1,
5540 (double) it->selective)) /* iftc */
5541 {
5542 success_p = next_element_from_ellipsis (it);
5543 it->dpvec_char_len = -1;
5544 }
5545 }
5546 else if (it->c == '\r' && it->selective == -1)
5547 {
5548 /* A value of selective == -1 means that everything from the
5549 CR to the end of the line is invisible, with maybe an
5550 ellipsis displayed for it. */
5551 success_p = next_element_from_ellipsis (it);
5552 it->dpvec_char_len = -1;
5553 }
5554 }
5555 }
5556
5557 /* Value is zero if end of buffer reached. */
5558 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5559 return success_p;
5560 }
5561
5562
5563 /* Run the redisplay end trigger hook for IT. */
5564
5565 static void
5566 run_redisplay_end_trigger_hook (it)
5567 struct it *it;
5568 {
5569 Lisp_Object args[3];
5570
5571 /* IT->glyph_row should be non-null, i.e. we should be actually
5572 displaying something, or otherwise we should not run the hook. */
5573 xassert (it->glyph_row);
5574
5575 /* Set up hook arguments. */
5576 args[0] = Qredisplay_end_trigger_functions;
5577 args[1] = it->window;
5578 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5579 it->redisplay_end_trigger_charpos = 0;
5580
5581 /* Since we are *trying* to run these functions, don't try to run
5582 them again, even if they get an error. */
5583 it->w->redisplay_end_trigger = Qnil;
5584 Frun_hook_with_args (3, args);
5585
5586 /* Notice if it changed the face of the character we are on. */
5587 handle_face_prop (it);
5588 }
5589
5590
5591 /* Deliver a composition display element. The iterator IT is already
5592 filled with composition information (done in
5593 handle_composition_prop). Value is always 1. */
5594
5595 static int
5596 next_element_from_composition (it)
5597 struct it *it;
5598 {
5599 it->what = IT_COMPOSITION;
5600 it->position = (STRINGP (it->string)
5601 ? it->current.string_pos
5602 : it->current.pos);
5603 return 1;
5604 }
5605
5606
5607 \f
5608 /***********************************************************************
5609 Moving an iterator without producing glyphs
5610 ***********************************************************************/
5611
5612 /* Move iterator IT to a specified buffer or X position within one
5613 line on the display without producing glyphs.
5614
5615 OP should be a bit mask including some or all of these bits:
5616 MOVE_TO_X: Stop on reaching x-position TO_X.
5617 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5618 Regardless of OP's value, stop in reaching the end of the display line.
5619
5620 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5621 This means, in particular, that TO_X includes window's horizontal
5622 scroll amount.
5623
5624 The return value has several possible values that
5625 say what condition caused the scan to stop:
5626
5627 MOVE_POS_MATCH_OR_ZV
5628 - when TO_POS or ZV was reached.
5629
5630 MOVE_X_REACHED
5631 -when TO_X was reached before TO_POS or ZV were reached.
5632
5633 MOVE_LINE_CONTINUED
5634 - when we reached the end of the display area and the line must
5635 be continued.
5636
5637 MOVE_LINE_TRUNCATED
5638 - when we reached the end of the display area and the line is
5639 truncated.
5640
5641 MOVE_NEWLINE_OR_CR
5642 - when we stopped at a line end, i.e. a newline or a CR and selective
5643 display is on. */
5644
5645 static enum move_it_result
5646 move_it_in_display_line_to (it, to_charpos, to_x, op)
5647 struct it *it;
5648 int to_charpos, to_x, op;
5649 {
5650 enum move_it_result result = MOVE_UNDEFINED;
5651 struct glyph_row *saved_glyph_row;
5652
5653 /* Don't produce glyphs in produce_glyphs. */
5654 saved_glyph_row = it->glyph_row;
5655 it->glyph_row = NULL;
5656
5657 #define BUFFER_POS_REACHED_P() \
5658 ((op & MOVE_TO_POS) != 0 \
5659 && BUFFERP (it->object) \
5660 && IT_CHARPOS (*it) >= to_charpos)
5661
5662 while (1)
5663 {
5664 int x, i, ascent = 0, descent = 0;
5665
5666 /* Stop when ZV reached.
5667 We used to stop here when TO_CHARPOS reached as well, but that is
5668 too soon if this glyph does not fit on this line. So we handle it
5669 explicitly below. */
5670 if (!get_next_display_element (it)
5671 || (it->truncate_lines_p
5672 && BUFFER_POS_REACHED_P ()))
5673 {
5674 result = MOVE_POS_MATCH_OR_ZV;
5675 break;
5676 }
5677
5678 /* The call to produce_glyphs will get the metrics of the
5679 display element IT is loaded with. We record in x the
5680 x-position before this display element in case it does not
5681 fit on the line. */
5682 x = it->current_x;
5683
5684 /* Remember the line height so far in case the next element doesn't
5685 fit on the line. */
5686 if (!it->truncate_lines_p)
5687 {
5688 ascent = it->max_ascent;
5689 descent = it->max_descent;
5690 }
5691
5692 PRODUCE_GLYPHS (it);
5693
5694 if (it->area != TEXT_AREA)
5695 {
5696 set_iterator_to_next (it, 1);
5697 continue;
5698 }
5699
5700 /* The number of glyphs we get back in IT->nglyphs will normally
5701 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5702 character on a terminal frame, or (iii) a line end. For the
5703 second case, IT->nglyphs - 1 padding glyphs will be present
5704 (on X frames, there is only one glyph produced for a
5705 composite character.
5706
5707 The behavior implemented below means, for continuation lines,
5708 that as many spaces of a TAB as fit on the current line are
5709 displayed there. For terminal frames, as many glyphs of a
5710 multi-glyph character are displayed in the current line, too.
5711 This is what the old redisplay code did, and we keep it that
5712 way. Under X, the whole shape of a complex character must
5713 fit on the line or it will be completely displayed in the
5714 next line.
5715
5716 Note that both for tabs and padding glyphs, all glyphs have
5717 the same width. */
5718 if (it->nglyphs)
5719 {
5720 /* More than one glyph or glyph doesn't fit on line. All
5721 glyphs have the same width. */
5722 int single_glyph_width = it->pixel_width / it->nglyphs;
5723 int new_x;
5724
5725 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5726 {
5727 new_x = x + single_glyph_width;
5728
5729 /* We want to leave anything reaching TO_X to the caller. */
5730 if ((op & MOVE_TO_X) && new_x > to_x)
5731 {
5732 if (BUFFER_POS_REACHED_P ())
5733 goto buffer_pos_reached;
5734 it->current_x = x;
5735 result = MOVE_X_REACHED;
5736 break;
5737 }
5738 else if (/* Lines are continued. */
5739 !it->truncate_lines_p
5740 && (/* And glyph doesn't fit on the line. */
5741 new_x > it->last_visible_x
5742 /* Or it fits exactly and we're on a window
5743 system frame. */
5744 || (new_x == it->last_visible_x
5745 && FRAME_WINDOW_P (it->f))))
5746 {
5747 if (/* IT->hpos == 0 means the very first glyph
5748 doesn't fit on the line, e.g. a wide image. */
5749 it->hpos == 0
5750 || (new_x == it->last_visible_x
5751 && FRAME_WINDOW_P (it->f)))
5752 {
5753 ++it->hpos;
5754 it->current_x = new_x;
5755 if (i == it->nglyphs - 1)
5756 {
5757 set_iterator_to_next (it, 1);
5758 #ifdef HAVE_WINDOW_SYSTEM
5759 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5760 {
5761 if (!get_next_display_element (it))
5762 {
5763 result = MOVE_POS_MATCH_OR_ZV;
5764 break;
5765 }
5766 if (BUFFER_POS_REACHED_P ())
5767 {
5768 if (ITERATOR_AT_END_OF_LINE_P (it))
5769 result = MOVE_POS_MATCH_OR_ZV;
5770 else
5771 result = MOVE_LINE_CONTINUED;
5772 break;
5773 }
5774 if (ITERATOR_AT_END_OF_LINE_P (it))
5775 {
5776 result = MOVE_NEWLINE_OR_CR;
5777 break;
5778 }
5779 if (it->method == next_element_from_display_vector)
5780 it->face_id = it->saved_face_id;
5781 }
5782 #endif /* HAVE_WINDOW_SYSTEM */
5783 }
5784 }
5785 else
5786 {
5787 it->current_x = x;
5788 it->max_ascent = ascent;
5789 it->max_descent = descent;
5790 }
5791
5792 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5793 IT_CHARPOS (*it)));
5794 result = MOVE_LINE_CONTINUED;
5795 break;
5796 }
5797 else if (BUFFER_POS_REACHED_P ())
5798 goto buffer_pos_reached;
5799 else if (new_x > it->first_visible_x)
5800 {
5801 /* Glyph is visible. Increment number of glyphs that
5802 would be displayed. */
5803 ++it->hpos;
5804 }
5805 else
5806 {
5807 /* Glyph is completely off the left margin of the display
5808 area. Nothing to do. */
5809 }
5810 }
5811
5812 if (result != MOVE_UNDEFINED)
5813 break;
5814 }
5815 else if (BUFFER_POS_REACHED_P ())
5816 {
5817 buffer_pos_reached:
5818 it->current_x = x;
5819 it->max_ascent = ascent;
5820 it->max_descent = descent;
5821 result = MOVE_POS_MATCH_OR_ZV;
5822 break;
5823 }
5824 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5825 {
5826 /* Stop when TO_X specified and reached. This check is
5827 necessary here because of lines consisting of a line end,
5828 only. The line end will not produce any glyphs and we
5829 would never get MOVE_X_REACHED. */
5830 xassert (it->nglyphs == 0);
5831 result = MOVE_X_REACHED;
5832 break;
5833 }
5834
5835 /* Is this a line end? If yes, we're done. */
5836 if (ITERATOR_AT_END_OF_LINE_P (it))
5837 {
5838 result = MOVE_NEWLINE_OR_CR;
5839 break;
5840 }
5841
5842 /* The current display element has been consumed. Advance
5843 to the next. */
5844 set_iterator_to_next (it, 1);
5845
5846 /* Stop if lines are truncated and IT's current x-position is
5847 past the right edge of the window now. */
5848 if (it->truncate_lines_p
5849 && it->current_x >= it->last_visible_x)
5850 {
5851 #ifdef HAVE_WINDOW_SYSTEM
5852 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5853 {
5854 if (!get_next_display_element (it)
5855 || BUFFER_POS_REACHED_P ())
5856 {
5857 result = MOVE_POS_MATCH_OR_ZV;
5858 break;
5859 }
5860 if (ITERATOR_AT_END_OF_LINE_P (it))
5861 {
5862 result = MOVE_NEWLINE_OR_CR;
5863 break;
5864 }
5865 }
5866 #endif /* HAVE_WINDOW_SYSTEM */
5867 result = MOVE_LINE_TRUNCATED;
5868 break;
5869 }
5870 }
5871
5872 #undef BUFFER_POS_REACHED_P
5873
5874 /* Restore the iterator settings altered at the beginning of this
5875 function. */
5876 it->glyph_row = saved_glyph_row;
5877 return result;
5878 }
5879
5880
5881 /* Move IT forward until it satisfies one or more of the criteria in
5882 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5883
5884 OP is a bit-mask that specifies where to stop, and in particular,
5885 which of those four position arguments makes a difference. See the
5886 description of enum move_operation_enum.
5887
5888 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5889 screen line, this function will set IT to the next position >
5890 TO_CHARPOS. */
5891
5892 void
5893 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5894 struct it *it;
5895 int to_charpos, to_x, to_y, to_vpos;
5896 int op;
5897 {
5898 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5899 int line_height;
5900 int reached = 0;
5901
5902 for (;;)
5903 {
5904 if (op & MOVE_TO_VPOS)
5905 {
5906 /* If no TO_CHARPOS and no TO_X specified, stop at the
5907 start of the line TO_VPOS. */
5908 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5909 {
5910 if (it->vpos == to_vpos)
5911 {
5912 reached = 1;
5913 break;
5914 }
5915 else
5916 skip = move_it_in_display_line_to (it, -1, -1, 0);
5917 }
5918 else
5919 {
5920 /* TO_VPOS >= 0 means stop at TO_X in the line at
5921 TO_VPOS, or at TO_POS, whichever comes first. */
5922 if (it->vpos == to_vpos)
5923 {
5924 reached = 2;
5925 break;
5926 }
5927
5928 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5929
5930 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5931 {
5932 reached = 3;
5933 break;
5934 }
5935 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5936 {
5937 /* We have reached TO_X but not in the line we want. */
5938 skip = move_it_in_display_line_to (it, to_charpos,
5939 -1, MOVE_TO_POS);
5940 if (skip == MOVE_POS_MATCH_OR_ZV)
5941 {
5942 reached = 4;
5943 break;
5944 }
5945 }
5946 }
5947 }
5948 else if (op & MOVE_TO_Y)
5949 {
5950 struct it it_backup;
5951
5952 /* TO_Y specified means stop at TO_X in the line containing
5953 TO_Y---or at TO_CHARPOS if this is reached first. The
5954 problem is that we can't really tell whether the line
5955 contains TO_Y before we have completely scanned it, and
5956 this may skip past TO_X. What we do is to first scan to
5957 TO_X.
5958
5959 If TO_X is not specified, use a TO_X of zero. The reason
5960 is to make the outcome of this function more predictable.
5961 If we didn't use TO_X == 0, we would stop at the end of
5962 the line which is probably not what a caller would expect
5963 to happen. */
5964 skip = move_it_in_display_line_to (it, to_charpos,
5965 ((op & MOVE_TO_X)
5966 ? to_x : 0),
5967 (MOVE_TO_X
5968 | (op & MOVE_TO_POS)));
5969
5970 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5971 if (skip == MOVE_POS_MATCH_OR_ZV)
5972 {
5973 reached = 5;
5974 break;
5975 }
5976
5977 /* If TO_X was reached, we would like to know whether TO_Y
5978 is in the line. This can only be said if we know the
5979 total line height which requires us to scan the rest of
5980 the line. */
5981 if (skip == MOVE_X_REACHED)
5982 {
5983 it_backup = *it;
5984 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5985 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5986 op & MOVE_TO_POS);
5987 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5988 }
5989
5990 /* Now, decide whether TO_Y is in this line. */
5991 line_height = it->max_ascent + it->max_descent;
5992 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5993
5994 if (to_y >= it->current_y
5995 && to_y < it->current_y + line_height)
5996 {
5997 if (skip == MOVE_X_REACHED)
5998 /* If TO_Y is in this line and TO_X was reached above,
5999 we scanned too far. We have to restore IT's settings
6000 to the ones before skipping. */
6001 *it = it_backup;
6002 reached = 6;
6003 }
6004 else if (skip == MOVE_X_REACHED)
6005 {
6006 skip = skip2;
6007 if (skip == MOVE_POS_MATCH_OR_ZV)
6008 reached = 7;
6009 }
6010
6011 if (reached)
6012 break;
6013 }
6014 else
6015 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6016
6017 switch (skip)
6018 {
6019 case MOVE_POS_MATCH_OR_ZV:
6020 reached = 8;
6021 goto out;
6022
6023 case MOVE_NEWLINE_OR_CR:
6024 set_iterator_to_next (it, 1);
6025 it->continuation_lines_width = 0;
6026 break;
6027
6028 case MOVE_LINE_TRUNCATED:
6029 it->continuation_lines_width = 0;
6030 reseat_at_next_visible_line_start (it, 0);
6031 if ((op & MOVE_TO_POS) != 0
6032 && IT_CHARPOS (*it) > to_charpos)
6033 {
6034 reached = 9;
6035 goto out;
6036 }
6037 break;
6038
6039 case MOVE_LINE_CONTINUED:
6040 it->continuation_lines_width += it->current_x;
6041 break;
6042
6043 default:
6044 abort ();
6045 }
6046
6047 /* Reset/increment for the next run. */
6048 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6049 it->current_x = it->hpos = 0;
6050 it->current_y += it->max_ascent + it->max_descent;
6051 ++it->vpos;
6052 last_height = it->max_ascent + it->max_descent;
6053 last_max_ascent = it->max_ascent;
6054 it->max_ascent = it->max_descent = 0;
6055 }
6056
6057 out:
6058
6059 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6060 }
6061
6062
6063 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6064
6065 If DY > 0, move IT backward at least that many pixels. DY = 0
6066 means move IT backward to the preceding line start or BEGV. This
6067 function may move over more than DY pixels if IT->current_y - DY
6068 ends up in the middle of a line; in this case IT->current_y will be
6069 set to the top of the line moved to. */
6070
6071 void
6072 move_it_vertically_backward (it, dy)
6073 struct it *it;
6074 int dy;
6075 {
6076 int nlines, h;
6077 struct it it2, it3;
6078 int start_pos;
6079
6080 move_further_back:
6081 xassert (dy >= 0);
6082
6083 start_pos = IT_CHARPOS (*it);
6084
6085 /* Estimate how many newlines we must move back. */
6086 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6087
6088 /* Set the iterator's position that many lines back. */
6089 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6090 back_to_previous_visible_line_start (it);
6091
6092 /* Reseat the iterator here. When moving backward, we don't want
6093 reseat to skip forward over invisible text, set up the iterator
6094 to deliver from overlay strings at the new position etc. So,
6095 use reseat_1 here. */
6096 reseat_1 (it, it->current.pos, 1);
6097
6098 /* We are now surely at a line start. */
6099 it->current_x = it->hpos = 0;
6100 it->continuation_lines_width = 0;
6101
6102 /* Move forward and see what y-distance we moved. First move to the
6103 start of the next line so that we get its height. We need this
6104 height to be able to tell whether we reached the specified
6105 y-distance. */
6106 it2 = *it;
6107 it2.max_ascent = it2.max_descent = 0;
6108 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6109 MOVE_TO_POS | MOVE_TO_VPOS);
6110 xassert (IT_CHARPOS (*it) >= BEGV);
6111 it3 = it2;
6112
6113 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6114 xassert (IT_CHARPOS (*it) >= BEGV);
6115 /* H is the actual vertical distance from the position in *IT
6116 and the starting position. */
6117 h = it2.current_y - it->current_y;
6118 /* NLINES is the distance in number of lines. */
6119 nlines = it2.vpos - it->vpos;
6120
6121 /* Correct IT's y and vpos position
6122 so that they are relative to the starting point. */
6123 it->vpos -= nlines;
6124 it->current_y -= h;
6125
6126 if (dy == 0)
6127 {
6128 /* DY == 0 means move to the start of the screen line. The
6129 value of nlines is > 0 if continuation lines were involved. */
6130 if (nlines > 0)
6131 move_it_by_lines (it, nlines, 1);
6132 xassert (IT_CHARPOS (*it) <= start_pos);
6133 }
6134 else
6135 {
6136 /* The y-position we try to reach, relative to *IT.
6137 Note that H has been subtracted in front of the if-statement. */
6138 int target_y = it->current_y + h - dy;
6139 int y0 = it3.current_y;
6140 int y1 = line_bottom_y (&it3);
6141 int line_height = y1 - y0;
6142
6143 /* If we did not reach target_y, try to move further backward if
6144 we can. If we moved too far backward, try to move forward. */
6145 if (target_y < it->current_y
6146 /* This is heuristic. In a window that's 3 lines high, with
6147 a line height of 13 pixels each, recentering with point
6148 on the bottom line will try to move -39/2 = 19 pixels
6149 backward. Try to avoid moving into the first line. */
6150 && it->current_y - target_y > line_height * 2 / 3
6151 && IT_CHARPOS (*it) > BEGV)
6152 {
6153 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6154 target_y - it->current_y));
6155 dy = it->current_y - target_y;
6156 goto move_further_back;
6157 }
6158 else if (target_y >= it->current_y + line_height
6159 && IT_CHARPOS (*it) < ZV)
6160 {
6161 /* Should move forward by at least one line, maybe more.
6162
6163 Note: Calling move_it_by_lines can be expensive on
6164 terminal frames, where compute_motion is used (via
6165 vmotion) to do the job, when there are very long lines
6166 and truncate-lines is nil. That's the reason for
6167 treating terminal frames specially here. */
6168
6169 if (!FRAME_WINDOW_P (it->f))
6170 move_it_vertically (it, target_y - (it->current_y + line_height));
6171 else
6172 {
6173 do
6174 {
6175 move_it_by_lines (it, 1, 1);
6176 }
6177 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6178 }
6179
6180 xassert (IT_CHARPOS (*it) >= BEGV);
6181 }
6182 }
6183 }
6184
6185
6186 /* Move IT by a specified amount of pixel lines DY. DY negative means
6187 move backwards. DY = 0 means move to start of screen line. At the
6188 end, IT will be on the start of a screen line. */
6189
6190 void
6191 move_it_vertically (it, dy)
6192 struct it *it;
6193 int dy;
6194 {
6195 if (dy <= 0)
6196 move_it_vertically_backward (it, -dy);
6197 else
6198 {
6199 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6200 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6201 MOVE_TO_POS | MOVE_TO_Y);
6202 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6203
6204 /* If buffer ends in ZV without a newline, move to the start of
6205 the line to satisfy the post-condition. */
6206 if (IT_CHARPOS (*it) == ZV
6207 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6208 move_it_by_lines (it, 0, 0);
6209 }
6210 }
6211
6212
6213 /* Move iterator IT past the end of the text line it is in. */
6214
6215 void
6216 move_it_past_eol (it)
6217 struct it *it;
6218 {
6219 enum move_it_result rc;
6220
6221 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6222 if (rc == MOVE_NEWLINE_OR_CR)
6223 set_iterator_to_next (it, 0);
6224 }
6225
6226
6227 #if 0 /* Currently not used. */
6228
6229 /* Return non-zero if some text between buffer positions START_CHARPOS
6230 and END_CHARPOS is invisible. IT->window is the window for text
6231 property lookup. */
6232
6233 static int
6234 invisible_text_between_p (it, start_charpos, end_charpos)
6235 struct it *it;
6236 int start_charpos, end_charpos;
6237 {
6238 Lisp_Object prop, limit;
6239 int invisible_found_p;
6240
6241 xassert (it != NULL && start_charpos <= end_charpos);
6242
6243 /* Is text at START invisible? */
6244 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6245 it->window);
6246 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6247 invisible_found_p = 1;
6248 else
6249 {
6250 limit = Fnext_single_char_property_change (make_number (start_charpos),
6251 Qinvisible, Qnil,
6252 make_number (end_charpos));
6253 invisible_found_p = XFASTINT (limit) < end_charpos;
6254 }
6255
6256 return invisible_found_p;
6257 }
6258
6259 #endif /* 0 */
6260
6261
6262 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6263 negative means move up. DVPOS == 0 means move to the start of the
6264 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6265 NEED_Y_P is zero, IT->current_y will be left unchanged.
6266
6267 Further optimization ideas: If we would know that IT->f doesn't use
6268 a face with proportional font, we could be faster for
6269 truncate-lines nil. */
6270
6271 void
6272 move_it_by_lines (it, dvpos, need_y_p)
6273 struct it *it;
6274 int dvpos, need_y_p;
6275 {
6276 struct position pos;
6277
6278 if (!FRAME_WINDOW_P (it->f))
6279 {
6280 struct text_pos textpos;
6281
6282 /* We can use vmotion on frames without proportional fonts. */
6283 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6284 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6285 reseat (it, textpos, 1);
6286 it->vpos += pos.vpos;
6287 it->current_y += pos.vpos;
6288 }
6289 else if (dvpos == 0)
6290 {
6291 /* DVPOS == 0 means move to the start of the screen line. */
6292 move_it_vertically_backward (it, 0);
6293 xassert (it->current_x == 0 && it->hpos == 0);
6294 /* Let next call to line_bottom_y calculate real line height */
6295 last_height = 0;
6296 }
6297 else if (dvpos > 0)
6298 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6299 else
6300 {
6301 struct it it2;
6302 int start_charpos, i;
6303
6304 /* Start at the beginning of the screen line containing IT's
6305 position. */
6306 move_it_vertically_backward (it, 0);
6307
6308 /* Go back -DVPOS visible lines and reseat the iterator there. */
6309 start_charpos = IT_CHARPOS (*it);
6310 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6311 back_to_previous_visible_line_start (it);
6312 reseat (it, it->current.pos, 1);
6313 it->current_x = it->hpos = 0;
6314
6315 /* Above call may have moved too far if continuation lines
6316 are involved. Scan forward and see if it did. */
6317 it2 = *it;
6318 it2.vpos = it2.current_y = 0;
6319 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6320 it->vpos -= it2.vpos;
6321 it->current_y -= it2.current_y;
6322 it->current_x = it->hpos = 0;
6323
6324 /* If we moved too far, move IT some lines forward. */
6325 if (it2.vpos > -dvpos)
6326 {
6327 int delta = it2.vpos + dvpos;
6328 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6329 }
6330 }
6331 }
6332
6333 /* Return 1 if IT points into the middle of a display vector. */
6334
6335 int
6336 in_display_vector_p (it)
6337 struct it *it;
6338 {
6339 return (it->method == next_element_from_display_vector
6340 && it->current.dpvec_index > 0
6341 && it->dpvec + it->current.dpvec_index != it->dpend);
6342 }
6343
6344 \f
6345 /***********************************************************************
6346 Messages
6347 ***********************************************************************/
6348
6349
6350 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6351 to *Messages*. */
6352
6353 void
6354 add_to_log (format, arg1, arg2)
6355 char *format;
6356 Lisp_Object arg1, arg2;
6357 {
6358 Lisp_Object args[3];
6359 Lisp_Object msg, fmt;
6360 char *buffer;
6361 int len;
6362 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6363 USE_SAFE_ALLOCA;
6364
6365 /* Do nothing if called asynchronously. Inserting text into
6366 a buffer may call after-change-functions and alike and
6367 that would means running Lisp asynchronously. */
6368 if (handling_signal)
6369 return;
6370
6371 fmt = msg = Qnil;
6372 GCPRO4 (fmt, msg, arg1, arg2);
6373
6374 args[0] = fmt = build_string (format);
6375 args[1] = arg1;
6376 args[2] = arg2;
6377 msg = Fformat (3, args);
6378
6379 len = SBYTES (msg) + 1;
6380 SAFE_ALLOCA (buffer, char *, len);
6381 bcopy (SDATA (msg), buffer, len);
6382
6383 message_dolog (buffer, len - 1, 1, 0);
6384 SAFE_FREE ();
6385
6386 UNGCPRO;
6387 }
6388
6389
6390 /* Output a newline in the *Messages* buffer if "needs" one. */
6391
6392 void
6393 message_log_maybe_newline ()
6394 {
6395 if (message_log_need_newline)
6396 message_dolog ("", 0, 1, 0);
6397 }
6398
6399
6400 /* Add a string M of length NBYTES to the message log, optionally
6401 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6402 nonzero, means interpret the contents of M as multibyte. This
6403 function calls low-level routines in order to bypass text property
6404 hooks, etc. which might not be safe to run. */
6405
6406 void
6407 message_dolog (m, nbytes, nlflag, multibyte)
6408 const char *m;
6409 int nbytes, nlflag, multibyte;
6410 {
6411 if (!NILP (Vmemory_full))
6412 return;
6413
6414 if (!NILP (Vmessage_log_max))
6415 {
6416 struct buffer *oldbuf;
6417 Lisp_Object oldpoint, oldbegv, oldzv;
6418 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6419 int point_at_end = 0;
6420 int zv_at_end = 0;
6421 Lisp_Object old_deactivate_mark, tem;
6422 struct gcpro gcpro1;
6423
6424 old_deactivate_mark = Vdeactivate_mark;
6425 oldbuf = current_buffer;
6426 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6427 current_buffer->undo_list = Qt;
6428
6429 oldpoint = message_dolog_marker1;
6430 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6431 oldbegv = message_dolog_marker2;
6432 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6433 oldzv = message_dolog_marker3;
6434 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6435 GCPRO1 (old_deactivate_mark);
6436
6437 if (PT == Z)
6438 point_at_end = 1;
6439 if (ZV == Z)
6440 zv_at_end = 1;
6441
6442 BEGV = BEG;
6443 BEGV_BYTE = BEG_BYTE;
6444 ZV = Z;
6445 ZV_BYTE = Z_BYTE;
6446 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6447
6448 /* Insert the string--maybe converting multibyte to single byte
6449 or vice versa, so that all the text fits the buffer. */
6450 if (multibyte
6451 && NILP (current_buffer->enable_multibyte_characters))
6452 {
6453 int i, c, char_bytes;
6454 unsigned char work[1];
6455
6456 /* Convert a multibyte string to single-byte
6457 for the *Message* buffer. */
6458 for (i = 0; i < nbytes; i += char_bytes)
6459 {
6460 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6461 work[0] = (SINGLE_BYTE_CHAR_P (c)
6462 ? c
6463 : multibyte_char_to_unibyte (c, Qnil));
6464 insert_1_both (work, 1, 1, 1, 0, 0);
6465 }
6466 }
6467 else if (! multibyte
6468 && ! NILP (current_buffer->enable_multibyte_characters))
6469 {
6470 int i, c, char_bytes;
6471 unsigned char *msg = (unsigned char *) m;
6472 unsigned char str[MAX_MULTIBYTE_LENGTH];
6473 /* Convert a single-byte string to multibyte
6474 for the *Message* buffer. */
6475 for (i = 0; i < nbytes; i++)
6476 {
6477 c = unibyte_char_to_multibyte (msg[i]);
6478 char_bytes = CHAR_STRING (c, str);
6479 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6480 }
6481 }
6482 else if (nbytes)
6483 insert_1 (m, nbytes, 1, 0, 0);
6484
6485 if (nlflag)
6486 {
6487 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6488 insert_1 ("\n", 1, 1, 0, 0);
6489
6490 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6491 this_bol = PT;
6492 this_bol_byte = PT_BYTE;
6493
6494 /* See if this line duplicates the previous one.
6495 If so, combine duplicates. */
6496 if (this_bol > BEG)
6497 {
6498 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6499 prev_bol = PT;
6500 prev_bol_byte = PT_BYTE;
6501
6502 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6503 this_bol, this_bol_byte);
6504 if (dup)
6505 {
6506 del_range_both (prev_bol, prev_bol_byte,
6507 this_bol, this_bol_byte, 0);
6508 if (dup > 1)
6509 {
6510 char dupstr[40];
6511 int duplen;
6512
6513 /* If you change this format, don't forget to also
6514 change message_log_check_duplicate. */
6515 sprintf (dupstr, " [%d times]", dup);
6516 duplen = strlen (dupstr);
6517 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6518 insert_1 (dupstr, duplen, 1, 0, 1);
6519 }
6520 }
6521 }
6522
6523 /* If we have more than the desired maximum number of lines
6524 in the *Messages* buffer now, delete the oldest ones.
6525 This is safe because we don't have undo in this buffer. */
6526
6527 if (NATNUMP (Vmessage_log_max))
6528 {
6529 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6530 -XFASTINT (Vmessage_log_max) - 1, 0);
6531 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6532 }
6533 }
6534 BEGV = XMARKER (oldbegv)->charpos;
6535 BEGV_BYTE = marker_byte_position (oldbegv);
6536
6537 if (zv_at_end)
6538 {
6539 ZV = Z;
6540 ZV_BYTE = Z_BYTE;
6541 }
6542 else
6543 {
6544 ZV = XMARKER (oldzv)->charpos;
6545 ZV_BYTE = marker_byte_position (oldzv);
6546 }
6547
6548 if (point_at_end)
6549 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6550 else
6551 /* We can't do Fgoto_char (oldpoint) because it will run some
6552 Lisp code. */
6553 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6554 XMARKER (oldpoint)->bytepos);
6555
6556 UNGCPRO;
6557 unchain_marker (XMARKER (oldpoint));
6558 unchain_marker (XMARKER (oldbegv));
6559 unchain_marker (XMARKER (oldzv));
6560
6561 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6562 set_buffer_internal (oldbuf);
6563 if (NILP (tem))
6564 windows_or_buffers_changed = old_windows_or_buffers_changed;
6565 message_log_need_newline = !nlflag;
6566 Vdeactivate_mark = old_deactivate_mark;
6567 }
6568 }
6569
6570
6571 /* We are at the end of the buffer after just having inserted a newline.
6572 (Note: We depend on the fact we won't be crossing the gap.)
6573 Check to see if the most recent message looks a lot like the previous one.
6574 Return 0 if different, 1 if the new one should just replace it, or a
6575 value N > 1 if we should also append " [N times]". */
6576
6577 static int
6578 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6579 int prev_bol, this_bol;
6580 int prev_bol_byte, this_bol_byte;
6581 {
6582 int i;
6583 int len = Z_BYTE - 1 - this_bol_byte;
6584 int seen_dots = 0;
6585 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6586 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6587
6588 for (i = 0; i < len; i++)
6589 {
6590 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6591 seen_dots = 1;
6592 if (p1[i] != p2[i])
6593 return seen_dots;
6594 }
6595 p1 += len;
6596 if (*p1 == '\n')
6597 return 2;
6598 if (*p1++ == ' ' && *p1++ == '[')
6599 {
6600 int n = 0;
6601 while (*p1 >= '0' && *p1 <= '9')
6602 n = n * 10 + *p1++ - '0';
6603 if (strncmp (p1, " times]\n", 8) == 0)
6604 return n+1;
6605 }
6606 return 0;
6607 }
6608
6609
6610 /* Display an echo area message M with a specified length of NBYTES
6611 bytes. The string may include null characters. If M is 0, clear
6612 out any existing message, and let the mini-buffer text show
6613 through.
6614
6615 The buffer M must continue to exist until after the echo area gets
6616 cleared or some other message gets displayed there. This means do
6617 not pass text that is stored in a Lisp string; do not pass text in
6618 a buffer that was alloca'd. */
6619
6620 void
6621 message2 (m, nbytes, multibyte)
6622 const char *m;
6623 int nbytes;
6624 int multibyte;
6625 {
6626 /* First flush out any partial line written with print. */
6627 message_log_maybe_newline ();
6628 if (m)
6629 message_dolog (m, nbytes, 1, multibyte);
6630 message2_nolog (m, nbytes, multibyte);
6631 }
6632
6633
6634 /* The non-logging counterpart of message2. */
6635
6636 void
6637 message2_nolog (m, nbytes, multibyte)
6638 const char *m;
6639 int nbytes, multibyte;
6640 {
6641 struct frame *sf = SELECTED_FRAME ();
6642 message_enable_multibyte = multibyte;
6643
6644 if (noninteractive)
6645 {
6646 if (noninteractive_need_newline)
6647 putc ('\n', stderr);
6648 noninteractive_need_newline = 0;
6649 if (m)
6650 fwrite (m, nbytes, 1, stderr);
6651 if (cursor_in_echo_area == 0)
6652 fprintf (stderr, "\n");
6653 fflush (stderr);
6654 }
6655 /* A null message buffer means that the frame hasn't really been
6656 initialized yet. Error messages get reported properly by
6657 cmd_error, so this must be just an informative message; toss it. */
6658 else if (INTERACTIVE
6659 && sf->glyphs_initialized_p
6660 && FRAME_MESSAGE_BUF (sf))
6661 {
6662 Lisp_Object mini_window;
6663 struct frame *f;
6664
6665 /* Get the frame containing the mini-buffer
6666 that the selected frame is using. */
6667 mini_window = FRAME_MINIBUF_WINDOW (sf);
6668 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6669
6670 FRAME_SAMPLE_VISIBILITY (f);
6671 if (FRAME_VISIBLE_P (sf)
6672 && ! FRAME_VISIBLE_P (f))
6673 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6674
6675 if (m)
6676 {
6677 set_message (m, Qnil, nbytes, multibyte);
6678 if (minibuffer_auto_raise)
6679 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6680 }
6681 else
6682 clear_message (1, 1);
6683
6684 do_pending_window_change (0);
6685 echo_area_display (1);
6686 do_pending_window_change (0);
6687 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6688 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6689 }
6690 }
6691
6692
6693 /* Display an echo area message M with a specified length of NBYTES
6694 bytes. The string may include null characters. If M is not a
6695 string, clear out any existing message, and let the mini-buffer
6696 text show through. */
6697
6698 void
6699 message3 (m, nbytes, multibyte)
6700 Lisp_Object m;
6701 int nbytes;
6702 int multibyte;
6703 {
6704 struct gcpro gcpro1;
6705
6706 GCPRO1 (m);
6707 clear_message (1,1);
6708
6709 /* First flush out any partial line written with print. */
6710 message_log_maybe_newline ();
6711 if (STRINGP (m))
6712 message_dolog (SDATA (m), nbytes, 1, multibyte);
6713 message3_nolog (m, nbytes, multibyte);
6714
6715 UNGCPRO;
6716 }
6717
6718
6719 /* The non-logging version of message3. */
6720
6721 void
6722 message3_nolog (m, nbytes, multibyte)
6723 Lisp_Object m;
6724 int nbytes, multibyte;
6725 {
6726 struct frame *sf = SELECTED_FRAME ();
6727 message_enable_multibyte = multibyte;
6728
6729 if (noninteractive)
6730 {
6731 if (noninteractive_need_newline)
6732 putc ('\n', stderr);
6733 noninteractive_need_newline = 0;
6734 if (STRINGP (m))
6735 fwrite (SDATA (m), nbytes, 1, stderr);
6736 if (cursor_in_echo_area == 0)
6737 fprintf (stderr, "\n");
6738 fflush (stderr);
6739 }
6740 /* A null message buffer means that the frame hasn't really been
6741 initialized yet. Error messages get reported properly by
6742 cmd_error, so this must be just an informative message; toss it. */
6743 else if (INTERACTIVE
6744 && sf->glyphs_initialized_p
6745 && FRAME_MESSAGE_BUF (sf))
6746 {
6747 Lisp_Object mini_window;
6748 Lisp_Object frame;
6749 struct frame *f;
6750
6751 /* Get the frame containing the mini-buffer
6752 that the selected frame is using. */
6753 mini_window = FRAME_MINIBUF_WINDOW (sf);
6754 frame = XWINDOW (mini_window)->frame;
6755 f = XFRAME (frame);
6756
6757 FRAME_SAMPLE_VISIBILITY (f);
6758 if (FRAME_VISIBLE_P (sf)
6759 && !FRAME_VISIBLE_P (f))
6760 Fmake_frame_visible (frame);
6761
6762 if (STRINGP (m) && SCHARS (m) > 0)
6763 {
6764 set_message (NULL, m, nbytes, multibyte);
6765 if (minibuffer_auto_raise)
6766 Fraise_frame (frame);
6767 }
6768 else
6769 clear_message (1, 1);
6770
6771 do_pending_window_change (0);
6772 echo_area_display (1);
6773 do_pending_window_change (0);
6774 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6775 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6776 }
6777 }
6778
6779
6780 /* Display a null-terminated echo area message M. If M is 0, clear
6781 out any existing message, and let the mini-buffer text show through.
6782
6783 The buffer M must continue to exist until after the echo area gets
6784 cleared or some other message gets displayed there. Do not pass
6785 text that is stored in a Lisp string. Do not pass text in a buffer
6786 that was alloca'd. */
6787
6788 void
6789 message1 (m)
6790 char *m;
6791 {
6792 message2 (m, (m ? strlen (m) : 0), 0);
6793 }
6794
6795
6796 /* The non-logging counterpart of message1. */
6797
6798 void
6799 message1_nolog (m)
6800 char *m;
6801 {
6802 message2_nolog (m, (m ? strlen (m) : 0), 0);
6803 }
6804
6805 /* Display a message M which contains a single %s
6806 which gets replaced with STRING. */
6807
6808 void
6809 message_with_string (m, string, log)
6810 char *m;
6811 Lisp_Object string;
6812 int log;
6813 {
6814 CHECK_STRING (string);
6815
6816 if (noninteractive)
6817 {
6818 if (m)
6819 {
6820 if (noninteractive_need_newline)
6821 putc ('\n', stderr);
6822 noninteractive_need_newline = 0;
6823 fprintf (stderr, m, SDATA (string));
6824 if (cursor_in_echo_area == 0)
6825 fprintf (stderr, "\n");
6826 fflush (stderr);
6827 }
6828 }
6829 else if (INTERACTIVE)
6830 {
6831 /* The frame whose minibuffer we're going to display the message on.
6832 It may be larger than the selected frame, so we need
6833 to use its buffer, not the selected frame's buffer. */
6834 Lisp_Object mini_window;
6835 struct frame *f, *sf = SELECTED_FRAME ();
6836
6837 /* Get the frame containing the minibuffer
6838 that the selected frame is using. */
6839 mini_window = FRAME_MINIBUF_WINDOW (sf);
6840 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6841
6842 /* A null message buffer means that the frame hasn't really been
6843 initialized yet. Error messages get reported properly by
6844 cmd_error, so this must be just an informative message; toss it. */
6845 if (FRAME_MESSAGE_BUF (f))
6846 {
6847 Lisp_Object args[2], message;
6848 struct gcpro gcpro1, gcpro2;
6849
6850 args[0] = build_string (m);
6851 args[1] = message = string;
6852 GCPRO2 (args[0], message);
6853 gcpro1.nvars = 2;
6854
6855 message = Fformat (2, args);
6856
6857 if (log)
6858 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6859 else
6860 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6861
6862 UNGCPRO;
6863
6864 /* Print should start at the beginning of the message
6865 buffer next time. */
6866 message_buf_print = 0;
6867 }
6868 }
6869 }
6870
6871
6872 /* Dump an informative message to the minibuf. If M is 0, clear out
6873 any existing message, and let the mini-buffer text show through. */
6874
6875 /* VARARGS 1 */
6876 void
6877 message (m, a1, a2, a3)
6878 char *m;
6879 EMACS_INT a1, a2, a3;
6880 {
6881 if (noninteractive)
6882 {
6883 if (m)
6884 {
6885 if (noninteractive_need_newline)
6886 putc ('\n', stderr);
6887 noninteractive_need_newline = 0;
6888 fprintf (stderr, m, a1, a2, a3);
6889 if (cursor_in_echo_area == 0)
6890 fprintf (stderr, "\n");
6891 fflush (stderr);
6892 }
6893 }
6894 else if (INTERACTIVE)
6895 {
6896 /* The frame whose mini-buffer we're going to display the message
6897 on. It may be larger than the selected frame, so we need to
6898 use its buffer, not the selected frame's buffer. */
6899 Lisp_Object mini_window;
6900 struct frame *f, *sf = SELECTED_FRAME ();
6901
6902 /* Get the frame containing the mini-buffer
6903 that the selected frame is using. */
6904 mini_window = FRAME_MINIBUF_WINDOW (sf);
6905 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6906
6907 /* A null message buffer means that the frame hasn't really been
6908 initialized yet. Error messages get reported properly by
6909 cmd_error, so this must be just an informative message; toss
6910 it. */
6911 if (FRAME_MESSAGE_BUF (f))
6912 {
6913 if (m)
6914 {
6915 int len;
6916 #ifdef NO_ARG_ARRAY
6917 char *a[3];
6918 a[0] = (char *) a1;
6919 a[1] = (char *) a2;
6920 a[2] = (char *) a3;
6921
6922 len = doprnt (FRAME_MESSAGE_BUF (f),
6923 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6924 #else
6925 len = doprnt (FRAME_MESSAGE_BUF (f),
6926 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6927 (char **) &a1);
6928 #endif /* NO_ARG_ARRAY */
6929
6930 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6931 }
6932 else
6933 message1 (0);
6934
6935 /* Print should start at the beginning of the message
6936 buffer next time. */
6937 message_buf_print = 0;
6938 }
6939 }
6940 }
6941
6942
6943 /* The non-logging version of message. */
6944
6945 void
6946 message_nolog (m, a1, a2, a3)
6947 char *m;
6948 EMACS_INT a1, a2, a3;
6949 {
6950 Lisp_Object old_log_max;
6951 old_log_max = Vmessage_log_max;
6952 Vmessage_log_max = Qnil;
6953 message (m, a1, a2, a3);
6954 Vmessage_log_max = old_log_max;
6955 }
6956
6957
6958 /* Display the current message in the current mini-buffer. This is
6959 only called from error handlers in process.c, and is not time
6960 critical. */
6961
6962 void
6963 update_echo_area ()
6964 {
6965 if (!NILP (echo_area_buffer[0]))
6966 {
6967 Lisp_Object string;
6968 string = Fcurrent_message ();
6969 message3 (string, SBYTES (string),
6970 !NILP (current_buffer->enable_multibyte_characters));
6971 }
6972 }
6973
6974
6975 /* Make sure echo area buffers in `echo_buffers' are live.
6976 If they aren't, make new ones. */
6977
6978 static void
6979 ensure_echo_area_buffers ()
6980 {
6981 int i;
6982
6983 for (i = 0; i < 2; ++i)
6984 if (!BUFFERP (echo_buffer[i])
6985 || NILP (XBUFFER (echo_buffer[i])->name))
6986 {
6987 char name[30];
6988 Lisp_Object old_buffer;
6989 int j;
6990
6991 old_buffer = echo_buffer[i];
6992 sprintf (name, " *Echo Area %d*", i);
6993 echo_buffer[i] = Fget_buffer_create (build_string (name));
6994 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6995
6996 for (j = 0; j < 2; ++j)
6997 if (EQ (old_buffer, echo_area_buffer[j]))
6998 echo_area_buffer[j] = echo_buffer[i];
6999 }
7000 }
7001
7002
7003 /* Call FN with args A1..A4 with either the current or last displayed
7004 echo_area_buffer as current buffer.
7005
7006 WHICH zero means use the current message buffer
7007 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7008 from echo_buffer[] and clear it.
7009
7010 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7011 suitable buffer from echo_buffer[] and clear it.
7012
7013 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7014 that the current message becomes the last displayed one, make
7015 choose a suitable buffer for echo_area_buffer[0], and clear it.
7016
7017 Value is what FN returns. */
7018
7019 static int
7020 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7021 struct window *w;
7022 int which;
7023 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7024 EMACS_INT a1;
7025 Lisp_Object a2;
7026 EMACS_INT a3, a4;
7027 {
7028 Lisp_Object buffer;
7029 int this_one, the_other, clear_buffer_p, rc;
7030 int count = SPECPDL_INDEX ();
7031
7032 /* If buffers aren't live, make new ones. */
7033 ensure_echo_area_buffers ();
7034
7035 clear_buffer_p = 0;
7036
7037 if (which == 0)
7038 this_one = 0, the_other = 1;
7039 else if (which > 0)
7040 this_one = 1, the_other = 0;
7041 else
7042 {
7043 this_one = 0, the_other = 1;
7044 clear_buffer_p = 1;
7045
7046 /* We need a fresh one in case the current echo buffer equals
7047 the one containing the last displayed echo area message. */
7048 if (!NILP (echo_area_buffer[this_one])
7049 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7050 echo_area_buffer[this_one] = Qnil;
7051 }
7052
7053 /* Choose a suitable buffer from echo_buffer[] is we don't
7054 have one. */
7055 if (NILP (echo_area_buffer[this_one]))
7056 {
7057 echo_area_buffer[this_one]
7058 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7059 ? echo_buffer[the_other]
7060 : echo_buffer[this_one]);
7061 clear_buffer_p = 1;
7062 }
7063
7064 buffer = echo_area_buffer[this_one];
7065
7066 /* Don't get confused by reusing the buffer used for echoing
7067 for a different purpose. */
7068 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7069 cancel_echoing ();
7070
7071 record_unwind_protect (unwind_with_echo_area_buffer,
7072 with_echo_area_buffer_unwind_data (w));
7073
7074 /* Make the echo area buffer current. Note that for display
7075 purposes, it is not necessary that the displayed window's buffer
7076 == current_buffer, except for text property lookup. So, let's
7077 only set that buffer temporarily here without doing a full
7078 Fset_window_buffer. We must also change w->pointm, though,
7079 because otherwise an assertions in unshow_buffer fails, and Emacs
7080 aborts. */
7081 set_buffer_internal_1 (XBUFFER (buffer));
7082 if (w)
7083 {
7084 w->buffer = buffer;
7085 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7086 }
7087
7088 current_buffer->undo_list = Qt;
7089 current_buffer->read_only = Qnil;
7090 specbind (Qinhibit_read_only, Qt);
7091 specbind (Qinhibit_modification_hooks, Qt);
7092
7093 if (clear_buffer_p && Z > BEG)
7094 del_range (BEG, Z);
7095
7096 xassert (BEGV >= BEG);
7097 xassert (ZV <= Z && ZV >= BEGV);
7098
7099 rc = fn (a1, a2, a3, a4);
7100
7101 xassert (BEGV >= BEG);
7102 xassert (ZV <= Z && ZV >= BEGV);
7103
7104 unbind_to (count, Qnil);
7105 return rc;
7106 }
7107
7108
7109 /* Save state that should be preserved around the call to the function
7110 FN called in with_echo_area_buffer. */
7111
7112 static Lisp_Object
7113 with_echo_area_buffer_unwind_data (w)
7114 struct window *w;
7115 {
7116 int i = 0;
7117 Lisp_Object vector;
7118
7119 /* Reduce consing by keeping one vector in
7120 Vwith_echo_area_save_vector. */
7121 vector = Vwith_echo_area_save_vector;
7122 Vwith_echo_area_save_vector = Qnil;
7123
7124 if (NILP (vector))
7125 vector = Fmake_vector (make_number (7), Qnil);
7126
7127 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7128 AREF (vector, i) = Vdeactivate_mark, ++i;
7129 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7130
7131 if (w)
7132 {
7133 XSETWINDOW (AREF (vector, i), w); ++i;
7134 AREF (vector, i) = w->buffer; ++i;
7135 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7136 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7137 }
7138 else
7139 {
7140 int end = i + 4;
7141 for (; i < end; ++i)
7142 AREF (vector, i) = Qnil;
7143 }
7144
7145 xassert (i == ASIZE (vector));
7146 return vector;
7147 }
7148
7149
7150 /* Restore global state from VECTOR which was created by
7151 with_echo_area_buffer_unwind_data. */
7152
7153 static Lisp_Object
7154 unwind_with_echo_area_buffer (vector)
7155 Lisp_Object vector;
7156 {
7157 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7158 Vdeactivate_mark = AREF (vector, 1);
7159 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7160
7161 if (WINDOWP (AREF (vector, 3)))
7162 {
7163 struct window *w;
7164 Lisp_Object buffer, charpos, bytepos;
7165
7166 w = XWINDOW (AREF (vector, 3));
7167 buffer = AREF (vector, 4);
7168 charpos = AREF (vector, 5);
7169 bytepos = AREF (vector, 6);
7170
7171 w->buffer = buffer;
7172 set_marker_both (w->pointm, buffer,
7173 XFASTINT (charpos), XFASTINT (bytepos));
7174 }
7175
7176 Vwith_echo_area_save_vector = vector;
7177 return Qnil;
7178 }
7179
7180
7181 /* Set up the echo area for use by print functions. MULTIBYTE_P
7182 non-zero means we will print multibyte. */
7183
7184 void
7185 setup_echo_area_for_printing (multibyte_p)
7186 int multibyte_p;
7187 {
7188 /* If we can't find an echo area any more, exit. */
7189 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7190 Fkill_emacs (Qnil);
7191
7192 ensure_echo_area_buffers ();
7193
7194 if (!message_buf_print)
7195 {
7196 /* A message has been output since the last time we printed.
7197 Choose a fresh echo area buffer. */
7198 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7199 echo_area_buffer[0] = echo_buffer[1];
7200 else
7201 echo_area_buffer[0] = echo_buffer[0];
7202
7203 /* Switch to that buffer and clear it. */
7204 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7205 current_buffer->truncate_lines = Qnil;
7206
7207 if (Z > BEG)
7208 {
7209 int count = SPECPDL_INDEX ();
7210 specbind (Qinhibit_read_only, Qt);
7211 /* Note that undo recording is always disabled. */
7212 del_range (BEG, Z);
7213 unbind_to (count, Qnil);
7214 }
7215 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7216
7217 /* Set up the buffer for the multibyteness we need. */
7218 if (multibyte_p
7219 != !NILP (current_buffer->enable_multibyte_characters))
7220 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7221
7222 /* Raise the frame containing the echo area. */
7223 if (minibuffer_auto_raise)
7224 {
7225 struct frame *sf = SELECTED_FRAME ();
7226 Lisp_Object mini_window;
7227 mini_window = FRAME_MINIBUF_WINDOW (sf);
7228 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7229 }
7230
7231 message_log_maybe_newline ();
7232 message_buf_print = 1;
7233 }
7234 else
7235 {
7236 if (NILP (echo_area_buffer[0]))
7237 {
7238 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7239 echo_area_buffer[0] = echo_buffer[1];
7240 else
7241 echo_area_buffer[0] = echo_buffer[0];
7242 }
7243
7244 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7245 {
7246 /* Someone switched buffers between print requests. */
7247 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7248 current_buffer->truncate_lines = Qnil;
7249 }
7250 }
7251 }
7252
7253
7254 /* Display an echo area message in window W. Value is non-zero if W's
7255 height is changed. If display_last_displayed_message_p is
7256 non-zero, display the message that was last displayed, otherwise
7257 display the current message. */
7258
7259 static int
7260 display_echo_area (w)
7261 struct window *w;
7262 {
7263 int i, no_message_p, window_height_changed_p, count;
7264
7265 /* Temporarily disable garbage collections while displaying the echo
7266 area. This is done because a GC can print a message itself.
7267 That message would modify the echo area buffer's contents while a
7268 redisplay of the buffer is going on, and seriously confuse
7269 redisplay. */
7270 count = inhibit_garbage_collection ();
7271
7272 /* If there is no message, we must call display_echo_area_1
7273 nevertheless because it resizes the window. But we will have to
7274 reset the echo_area_buffer in question to nil at the end because
7275 with_echo_area_buffer will sets it to an empty buffer. */
7276 i = display_last_displayed_message_p ? 1 : 0;
7277 no_message_p = NILP (echo_area_buffer[i]);
7278
7279 window_height_changed_p
7280 = with_echo_area_buffer (w, display_last_displayed_message_p,
7281 display_echo_area_1,
7282 (EMACS_INT) w, Qnil, 0, 0);
7283
7284 if (no_message_p)
7285 echo_area_buffer[i] = Qnil;
7286
7287 unbind_to (count, Qnil);
7288 return window_height_changed_p;
7289 }
7290
7291
7292 /* Helper for display_echo_area. Display the current buffer which
7293 contains the current echo area message in window W, a mini-window,
7294 a pointer to which is passed in A1. A2..A4 are currently not used.
7295 Change the height of W so that all of the message is displayed.
7296 Value is non-zero if height of W was changed. */
7297
7298 static int
7299 display_echo_area_1 (a1, a2, a3, a4)
7300 EMACS_INT a1;
7301 Lisp_Object a2;
7302 EMACS_INT a3, a4;
7303 {
7304 struct window *w = (struct window *) a1;
7305 Lisp_Object window;
7306 struct text_pos start;
7307 int window_height_changed_p = 0;
7308
7309 /* Do this before displaying, so that we have a large enough glyph
7310 matrix for the display. */
7311 window_height_changed_p = resize_mini_window (w, 0);
7312
7313 /* Display. */
7314 clear_glyph_matrix (w->desired_matrix);
7315 XSETWINDOW (window, w);
7316 SET_TEXT_POS (start, BEG, BEG_BYTE);
7317 try_window (window, start);
7318
7319 return window_height_changed_p;
7320 }
7321
7322
7323 /* Resize the echo area window to exactly the size needed for the
7324 currently displayed message, if there is one. If a mini-buffer
7325 is active, don't shrink it. */
7326
7327 void
7328 resize_echo_area_exactly ()
7329 {
7330 if (BUFFERP (echo_area_buffer[0])
7331 && WINDOWP (echo_area_window))
7332 {
7333 struct window *w = XWINDOW (echo_area_window);
7334 int resized_p;
7335 Lisp_Object resize_exactly;
7336
7337 if (minibuf_level == 0)
7338 resize_exactly = Qt;
7339 else
7340 resize_exactly = Qnil;
7341
7342 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7343 (EMACS_INT) w, resize_exactly, 0, 0);
7344 if (resized_p)
7345 {
7346 ++windows_or_buffers_changed;
7347 ++update_mode_lines;
7348 redisplay_internal (0);
7349 }
7350 }
7351 }
7352
7353
7354 /* Callback function for with_echo_area_buffer, when used from
7355 resize_echo_area_exactly. A1 contains a pointer to the window to
7356 resize, EXACTLY non-nil means resize the mini-window exactly to the
7357 size of the text displayed. A3 and A4 are not used. Value is what
7358 resize_mini_window returns. */
7359
7360 static int
7361 resize_mini_window_1 (a1, exactly, a3, a4)
7362 EMACS_INT a1;
7363 Lisp_Object exactly;
7364 EMACS_INT a3, a4;
7365 {
7366 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7367 }
7368
7369
7370 /* Resize mini-window W to fit the size of its contents. EXACT:P
7371 means size the window exactly to the size needed. Otherwise, it's
7372 only enlarged until W's buffer is empty. Value is non-zero if
7373 the window height has been changed. */
7374
7375 int
7376 resize_mini_window (w, exact_p)
7377 struct window *w;
7378 int exact_p;
7379 {
7380 struct frame *f = XFRAME (w->frame);
7381 int window_height_changed_p = 0;
7382
7383 xassert (MINI_WINDOW_P (w));
7384
7385 /* Don't resize windows while redisplaying a window; it would
7386 confuse redisplay functions when the size of the window they are
7387 displaying changes from under them. Such a resizing can happen,
7388 for instance, when which-func prints a long message while
7389 we are running fontification-functions. We're running these
7390 functions with safe_call which binds inhibit-redisplay to t. */
7391 if (!NILP (Vinhibit_redisplay))
7392 return 0;
7393
7394 /* Nil means don't try to resize. */
7395 if (NILP (Vresize_mini_windows)
7396 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7397 return 0;
7398
7399 if (!FRAME_MINIBUF_ONLY_P (f))
7400 {
7401 struct it it;
7402 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7403 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7404 int height, max_height;
7405 int unit = FRAME_LINE_HEIGHT (f);
7406 struct text_pos start;
7407 struct buffer *old_current_buffer = NULL;
7408
7409 if (current_buffer != XBUFFER (w->buffer))
7410 {
7411 old_current_buffer = current_buffer;
7412 set_buffer_internal (XBUFFER (w->buffer));
7413 }
7414
7415 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7416
7417 /* Compute the max. number of lines specified by the user. */
7418 if (FLOATP (Vmax_mini_window_height))
7419 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7420 else if (INTEGERP (Vmax_mini_window_height))
7421 max_height = XINT (Vmax_mini_window_height);
7422 else
7423 max_height = total_height / 4;
7424
7425 /* Correct that max. height if it's bogus. */
7426 max_height = max (1, max_height);
7427 max_height = min (total_height, max_height);
7428
7429 /* Find out the height of the text in the window. */
7430 if (it.truncate_lines_p)
7431 height = 1;
7432 else
7433 {
7434 last_height = 0;
7435 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7436 if (it.max_ascent == 0 && it.max_descent == 0)
7437 height = it.current_y + last_height;
7438 else
7439 height = it.current_y + it.max_ascent + it.max_descent;
7440 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7441 height = (height + unit - 1) / unit;
7442 }
7443
7444 /* Compute a suitable window start. */
7445 if (height > max_height)
7446 {
7447 height = max_height;
7448 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7449 move_it_vertically_backward (&it, (height - 1) * unit);
7450 start = it.current.pos;
7451 }
7452 else
7453 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7454 SET_MARKER_FROM_TEXT_POS (w->start, start);
7455
7456 if (EQ (Vresize_mini_windows, Qgrow_only))
7457 {
7458 /* Let it grow only, until we display an empty message, in which
7459 case the window shrinks again. */
7460 if (height > WINDOW_TOTAL_LINES (w))
7461 {
7462 int old_height = WINDOW_TOTAL_LINES (w);
7463 freeze_window_starts (f, 1);
7464 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7465 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7466 }
7467 else if (height < WINDOW_TOTAL_LINES (w)
7468 && (exact_p || BEGV == ZV))
7469 {
7470 int old_height = WINDOW_TOTAL_LINES (w);
7471 freeze_window_starts (f, 0);
7472 shrink_mini_window (w);
7473 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7474 }
7475 }
7476 else
7477 {
7478 /* Always resize to exact size needed. */
7479 if (height > WINDOW_TOTAL_LINES (w))
7480 {
7481 int old_height = WINDOW_TOTAL_LINES (w);
7482 freeze_window_starts (f, 1);
7483 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7484 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7485 }
7486 else if (height < WINDOW_TOTAL_LINES (w))
7487 {
7488 int old_height = WINDOW_TOTAL_LINES (w);
7489 freeze_window_starts (f, 0);
7490 shrink_mini_window (w);
7491
7492 if (height)
7493 {
7494 freeze_window_starts (f, 1);
7495 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7496 }
7497
7498 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7499 }
7500 }
7501
7502 if (old_current_buffer)
7503 set_buffer_internal (old_current_buffer);
7504 }
7505
7506 return window_height_changed_p;
7507 }
7508
7509
7510 /* Value is the current message, a string, or nil if there is no
7511 current message. */
7512
7513 Lisp_Object
7514 current_message ()
7515 {
7516 Lisp_Object msg;
7517
7518 if (NILP (echo_area_buffer[0]))
7519 msg = Qnil;
7520 else
7521 {
7522 with_echo_area_buffer (0, 0, current_message_1,
7523 (EMACS_INT) &msg, Qnil, 0, 0);
7524 if (NILP (msg))
7525 echo_area_buffer[0] = Qnil;
7526 }
7527
7528 return msg;
7529 }
7530
7531
7532 static int
7533 current_message_1 (a1, a2, a3, a4)
7534 EMACS_INT a1;
7535 Lisp_Object a2;
7536 EMACS_INT a3, a4;
7537 {
7538 Lisp_Object *msg = (Lisp_Object *) a1;
7539
7540 if (Z > BEG)
7541 *msg = make_buffer_string (BEG, Z, 1);
7542 else
7543 *msg = Qnil;
7544 return 0;
7545 }
7546
7547
7548 /* Push the current message on Vmessage_stack for later restauration
7549 by restore_message. Value is non-zero if the current message isn't
7550 empty. This is a relatively infrequent operation, so it's not
7551 worth optimizing. */
7552
7553 int
7554 push_message ()
7555 {
7556 Lisp_Object msg;
7557 msg = current_message ();
7558 Vmessage_stack = Fcons (msg, Vmessage_stack);
7559 return STRINGP (msg);
7560 }
7561
7562
7563 /* Restore message display from the top of Vmessage_stack. */
7564
7565 void
7566 restore_message ()
7567 {
7568 Lisp_Object msg;
7569
7570 xassert (CONSP (Vmessage_stack));
7571 msg = XCAR (Vmessage_stack);
7572 if (STRINGP (msg))
7573 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7574 else
7575 message3_nolog (msg, 0, 0);
7576 }
7577
7578
7579 /* Handler for record_unwind_protect calling pop_message. */
7580
7581 Lisp_Object
7582 pop_message_unwind (dummy)
7583 Lisp_Object dummy;
7584 {
7585 pop_message ();
7586 return Qnil;
7587 }
7588
7589 /* Pop the top-most entry off Vmessage_stack. */
7590
7591 void
7592 pop_message ()
7593 {
7594 xassert (CONSP (Vmessage_stack));
7595 Vmessage_stack = XCDR (Vmessage_stack);
7596 }
7597
7598
7599 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7600 exits. If the stack is not empty, we have a missing pop_message
7601 somewhere. */
7602
7603 void
7604 check_message_stack ()
7605 {
7606 if (!NILP (Vmessage_stack))
7607 abort ();
7608 }
7609
7610
7611 /* Truncate to NCHARS what will be displayed in the echo area the next
7612 time we display it---but don't redisplay it now. */
7613
7614 void
7615 truncate_echo_area (nchars)
7616 int nchars;
7617 {
7618 if (nchars == 0)
7619 echo_area_buffer[0] = Qnil;
7620 /* A null message buffer means that the frame hasn't really been
7621 initialized yet. Error messages get reported properly by
7622 cmd_error, so this must be just an informative message; toss it. */
7623 else if (!noninteractive
7624 && INTERACTIVE
7625 && !NILP (echo_area_buffer[0]))
7626 {
7627 struct frame *sf = SELECTED_FRAME ();
7628 if (FRAME_MESSAGE_BUF (sf))
7629 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7630 }
7631 }
7632
7633
7634 /* Helper function for truncate_echo_area. Truncate the current
7635 message to at most NCHARS characters. */
7636
7637 static int
7638 truncate_message_1 (nchars, a2, a3, a4)
7639 EMACS_INT nchars;
7640 Lisp_Object a2;
7641 EMACS_INT a3, a4;
7642 {
7643 if (BEG + nchars < Z)
7644 del_range (BEG + nchars, Z);
7645 if (Z == BEG)
7646 echo_area_buffer[0] = Qnil;
7647 return 0;
7648 }
7649
7650
7651 /* Set the current message to a substring of S or STRING.
7652
7653 If STRING is a Lisp string, set the message to the first NBYTES
7654 bytes from STRING. NBYTES zero means use the whole string. If
7655 STRING is multibyte, the message will be displayed multibyte.
7656
7657 If S is not null, set the message to the first LEN bytes of S. LEN
7658 zero means use the whole string. MULTIBYTE_P non-zero means S is
7659 multibyte. Display the message multibyte in that case. */
7660
7661 void
7662 set_message (s, string, nbytes, multibyte_p)
7663 const char *s;
7664 Lisp_Object string;
7665 int nbytes, multibyte_p;
7666 {
7667 message_enable_multibyte
7668 = ((s && multibyte_p)
7669 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7670
7671 with_echo_area_buffer (0, -1, set_message_1,
7672 (EMACS_INT) s, string, nbytes, multibyte_p);
7673 message_buf_print = 0;
7674 help_echo_showing_p = 0;
7675 }
7676
7677
7678 /* Helper function for set_message. Arguments have the same meaning
7679 as there, with A1 corresponding to S and A2 corresponding to STRING
7680 This function is called with the echo area buffer being
7681 current. */
7682
7683 static int
7684 set_message_1 (a1, a2, nbytes, multibyte_p)
7685 EMACS_INT a1;
7686 Lisp_Object a2;
7687 EMACS_INT nbytes, multibyte_p;
7688 {
7689 const char *s = (const char *) a1;
7690 Lisp_Object string = a2;
7691
7692 xassert (BEG == Z);
7693
7694 /* Change multibyteness of the echo buffer appropriately. */
7695 if (message_enable_multibyte
7696 != !NILP (current_buffer->enable_multibyte_characters))
7697 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7698
7699 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7700
7701 /* Insert new message at BEG. */
7702 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7703
7704 if (STRINGP (string))
7705 {
7706 int nchars;
7707
7708 if (nbytes == 0)
7709 nbytes = SBYTES (string);
7710 nchars = string_byte_to_char (string, nbytes);
7711
7712 /* This function takes care of single/multibyte conversion. We
7713 just have to ensure that the echo area buffer has the right
7714 setting of enable_multibyte_characters. */
7715 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7716 }
7717 else if (s)
7718 {
7719 if (nbytes == 0)
7720 nbytes = strlen (s);
7721
7722 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7723 {
7724 /* Convert from multi-byte to single-byte. */
7725 int i, c, n;
7726 unsigned char work[1];
7727
7728 /* Convert a multibyte string to single-byte. */
7729 for (i = 0; i < nbytes; i += n)
7730 {
7731 c = string_char_and_length (s + i, nbytes - i, &n);
7732 work[0] = (SINGLE_BYTE_CHAR_P (c)
7733 ? c
7734 : multibyte_char_to_unibyte (c, Qnil));
7735 insert_1_both (work, 1, 1, 1, 0, 0);
7736 }
7737 }
7738 else if (!multibyte_p
7739 && !NILP (current_buffer->enable_multibyte_characters))
7740 {
7741 /* Convert from single-byte to multi-byte. */
7742 int i, c, n;
7743 const unsigned char *msg = (const unsigned char *) s;
7744 unsigned char str[MAX_MULTIBYTE_LENGTH];
7745
7746 /* Convert a single-byte string to multibyte. */
7747 for (i = 0; i < nbytes; i++)
7748 {
7749 c = unibyte_char_to_multibyte (msg[i]);
7750 n = CHAR_STRING (c, str);
7751 insert_1_both (str, 1, n, 1, 0, 0);
7752 }
7753 }
7754 else
7755 insert_1 (s, nbytes, 1, 0, 0);
7756 }
7757
7758 return 0;
7759 }
7760
7761
7762 /* Clear messages. CURRENT_P non-zero means clear the current
7763 message. LAST_DISPLAYED_P non-zero means clear the message
7764 last displayed. */
7765
7766 void
7767 clear_message (current_p, last_displayed_p)
7768 int current_p, last_displayed_p;
7769 {
7770 if (current_p)
7771 {
7772 echo_area_buffer[0] = Qnil;
7773 message_cleared_p = 1;
7774 }
7775
7776 if (last_displayed_p)
7777 echo_area_buffer[1] = Qnil;
7778
7779 message_buf_print = 0;
7780 }
7781
7782 /* Clear garbaged frames.
7783
7784 This function is used where the old redisplay called
7785 redraw_garbaged_frames which in turn called redraw_frame which in
7786 turn called clear_frame. The call to clear_frame was a source of
7787 flickering. I believe a clear_frame is not necessary. It should
7788 suffice in the new redisplay to invalidate all current matrices,
7789 and ensure a complete redisplay of all windows. */
7790
7791 static void
7792 clear_garbaged_frames ()
7793 {
7794 if (frame_garbaged)
7795 {
7796 Lisp_Object tail, frame;
7797 int changed_count = 0;
7798
7799 FOR_EACH_FRAME (tail, frame)
7800 {
7801 struct frame *f = XFRAME (frame);
7802
7803 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7804 {
7805 if (f->resized_p)
7806 {
7807 Fredraw_frame (frame);
7808 f->force_flush_display_p = 1;
7809 }
7810 clear_current_matrices (f);
7811 changed_count++;
7812 f->garbaged = 0;
7813 f->resized_p = 0;
7814 }
7815 }
7816
7817 frame_garbaged = 0;
7818 if (changed_count)
7819 ++windows_or_buffers_changed;
7820 }
7821 }
7822
7823
7824 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7825 is non-zero update selected_frame. Value is non-zero if the
7826 mini-windows height has been changed. */
7827
7828 static int
7829 echo_area_display (update_frame_p)
7830 int update_frame_p;
7831 {
7832 Lisp_Object mini_window;
7833 struct window *w;
7834 struct frame *f;
7835 int window_height_changed_p = 0;
7836 struct frame *sf = SELECTED_FRAME ();
7837
7838 mini_window = FRAME_MINIBUF_WINDOW (sf);
7839 w = XWINDOW (mini_window);
7840 f = XFRAME (WINDOW_FRAME (w));
7841
7842 /* Don't display if frame is invisible or not yet initialized. */
7843 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7844 return 0;
7845
7846 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7847 #ifndef MAC_OS8
7848 #ifdef HAVE_WINDOW_SYSTEM
7849 /* When Emacs starts, selected_frame may be the initial terminal
7850 frame. If we let this through, a message would be displayed on
7851 the terminal. */
7852 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
7853 return 0;
7854 #endif /* HAVE_WINDOW_SYSTEM */
7855 #endif
7856
7857 /* Redraw garbaged frames. */
7858 if (frame_garbaged)
7859 clear_garbaged_frames ();
7860
7861 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7862 {
7863 echo_area_window = mini_window;
7864 window_height_changed_p = display_echo_area (w);
7865 w->must_be_updated_p = 1;
7866
7867 /* Update the display, unless called from redisplay_internal.
7868 Also don't update the screen during redisplay itself. The
7869 update will happen at the end of redisplay, and an update
7870 here could cause confusion. */
7871 if (update_frame_p && !redisplaying_p)
7872 {
7873 int n = 0;
7874
7875 /* If the display update has been interrupted by pending
7876 input, update mode lines in the frame. Due to the
7877 pending input, it might have been that redisplay hasn't
7878 been called, so that mode lines above the echo area are
7879 garbaged. This looks odd, so we prevent it here. */
7880 if (!display_completed)
7881 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7882
7883 if (window_height_changed_p
7884 /* Don't do this if Emacs is shutting down. Redisplay
7885 needs to run hooks. */
7886 && !NILP (Vrun_hooks))
7887 {
7888 /* Must update other windows. Likewise as in other
7889 cases, don't let this update be interrupted by
7890 pending input. */
7891 int count = SPECPDL_INDEX ();
7892 specbind (Qredisplay_dont_pause, Qt);
7893 windows_or_buffers_changed = 1;
7894 redisplay_internal (0);
7895 unbind_to (count, Qnil);
7896 }
7897 else if (FRAME_WINDOW_P (f) && n == 0)
7898 {
7899 /* Window configuration is the same as before.
7900 Can do with a display update of the echo area,
7901 unless we displayed some mode lines. */
7902 update_single_window (w, 1);
7903 FRAME_RIF (f)->flush_display (f);
7904 }
7905 else
7906 update_frame (f, 1, 1);
7907
7908 /* If cursor is in the echo area, make sure that the next
7909 redisplay displays the minibuffer, so that the cursor will
7910 be replaced with what the minibuffer wants. */
7911 if (cursor_in_echo_area)
7912 ++windows_or_buffers_changed;
7913 }
7914 }
7915 else if (!EQ (mini_window, selected_window))
7916 windows_or_buffers_changed++;
7917
7918 /* Last displayed message is now the current message. */
7919 echo_area_buffer[1] = echo_area_buffer[0];
7920
7921 /* Prevent redisplay optimization in redisplay_internal by resetting
7922 this_line_start_pos. This is done because the mini-buffer now
7923 displays the message instead of its buffer text. */
7924 if (EQ (mini_window, selected_window))
7925 CHARPOS (this_line_start_pos) = 0;
7926
7927 return window_height_changed_p;
7928 }
7929
7930
7931 \f
7932 /***********************************************************************
7933 Frame Titles
7934 ***********************************************************************/
7935
7936
7937 /* The frame title buffering code is also used by Fformat_mode_line.
7938 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7939
7940 /* A buffer for constructing frame titles in it; allocated from the
7941 heap in init_xdisp and resized as needed in store_frame_title_char. */
7942
7943 static char *frame_title_buf;
7944
7945 /* The buffer's end, and a current output position in it. */
7946
7947 static char *frame_title_buf_end;
7948 static char *frame_title_ptr;
7949
7950
7951 /* Store a single character C for the frame title in frame_title_buf.
7952 Re-allocate frame_title_buf if necessary. */
7953
7954 static void
7955 #ifdef PROTOTYPES
7956 store_frame_title_char (char c)
7957 #else
7958 store_frame_title_char (c)
7959 char c;
7960 #endif
7961 {
7962 /* If output position has reached the end of the allocated buffer,
7963 double the buffer's size. */
7964 if (frame_title_ptr == frame_title_buf_end)
7965 {
7966 int len = frame_title_ptr - frame_title_buf;
7967 int new_size = 2 * len * sizeof *frame_title_buf;
7968 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7969 frame_title_buf_end = frame_title_buf + new_size;
7970 frame_title_ptr = frame_title_buf + len;
7971 }
7972
7973 *frame_title_ptr++ = c;
7974 }
7975
7976
7977 /* Store part of a frame title in frame_title_buf, beginning at
7978 frame_title_ptr. STR is the string to store. Do not copy
7979 characters that yield more columns than PRECISION; PRECISION <= 0
7980 means copy the whole string. Pad with spaces until FIELD_WIDTH
7981 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7982 pad. Called from display_mode_element when it is used to build a
7983 frame title. */
7984
7985 static int
7986 store_frame_title (str, field_width, precision)
7987 const unsigned char *str;
7988 int field_width, precision;
7989 {
7990 int n = 0;
7991 int dummy, nbytes;
7992
7993 /* Copy at most PRECISION chars from STR. */
7994 nbytes = strlen (str);
7995 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
7996 while (nbytes--)
7997 store_frame_title_char (*str++);
7998
7999 /* Fill up with spaces until FIELD_WIDTH reached. */
8000 while (field_width > 0
8001 && n < field_width)
8002 {
8003 store_frame_title_char (' ');
8004 ++n;
8005 }
8006
8007 return n;
8008 }
8009
8010 #ifdef HAVE_WINDOW_SYSTEM
8011
8012 /* Set the title of FRAME, if it has changed. The title format is
8013 Vicon_title_format if FRAME is iconified, otherwise it is
8014 frame_title_format. */
8015
8016 static void
8017 x_consider_frame_title (frame)
8018 Lisp_Object frame;
8019 {
8020 struct frame *f = XFRAME (frame);
8021
8022 if (FRAME_WINDOW_P (f)
8023 || FRAME_MINIBUF_ONLY_P (f)
8024 || f->explicit_name)
8025 {
8026 /* Do we have more than one visible frame on this X display? */
8027 Lisp_Object tail;
8028 Lisp_Object fmt;
8029 struct buffer *obuf;
8030 int len;
8031 struct it it;
8032
8033 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8034 {
8035 Lisp_Object other_frame = XCAR (tail);
8036 struct frame *tf = XFRAME (other_frame);
8037
8038 if (tf != f
8039 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8040 && !FRAME_MINIBUF_ONLY_P (tf)
8041 && !EQ (other_frame, tip_frame)
8042 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8043 break;
8044 }
8045
8046 /* Set global variable indicating that multiple frames exist. */
8047 multiple_frames = CONSP (tail);
8048
8049 /* Switch to the buffer of selected window of the frame. Set up
8050 frame_title_ptr so that display_mode_element will output into it;
8051 then display the title. */
8052 obuf = current_buffer;
8053 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8054 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8055 frame_title_ptr = frame_title_buf;
8056 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8057 NULL, DEFAULT_FACE_ID);
8058 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8059 len = frame_title_ptr - frame_title_buf;
8060 frame_title_ptr = NULL;
8061 set_buffer_internal_1 (obuf);
8062
8063 /* Set the title only if it's changed. This avoids consing in
8064 the common case where it hasn't. (If it turns out that we've
8065 already wasted too much time by walking through the list with
8066 display_mode_element, then we might need to optimize at a
8067 higher level than this.) */
8068 if (! STRINGP (f->name)
8069 || SBYTES (f->name) != len
8070 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8071 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8072 }
8073 }
8074
8075 #endif /* not HAVE_WINDOW_SYSTEM */
8076
8077
8078
8079 \f
8080 /***********************************************************************
8081 Menu Bars
8082 ***********************************************************************/
8083
8084
8085 /* Prepare for redisplay by updating menu-bar item lists when
8086 appropriate. This can call eval. */
8087
8088 void
8089 prepare_menu_bars ()
8090 {
8091 int all_windows;
8092 struct gcpro gcpro1, gcpro2;
8093 struct frame *f;
8094 Lisp_Object tooltip_frame;
8095
8096 #ifdef HAVE_WINDOW_SYSTEM
8097 tooltip_frame = tip_frame;
8098 #else
8099 tooltip_frame = Qnil;
8100 #endif
8101
8102 /* Update all frame titles based on their buffer names, etc. We do
8103 this before the menu bars so that the buffer-menu will show the
8104 up-to-date frame titles. */
8105 #ifdef HAVE_WINDOW_SYSTEM
8106 if (windows_or_buffers_changed || update_mode_lines)
8107 {
8108 Lisp_Object tail, frame;
8109
8110 FOR_EACH_FRAME (tail, frame)
8111 {
8112 f = XFRAME (frame);
8113 if (!EQ (frame, tooltip_frame)
8114 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8115 x_consider_frame_title (frame);
8116 }
8117 }
8118 #endif /* HAVE_WINDOW_SYSTEM */
8119
8120 /* Update the menu bar item lists, if appropriate. This has to be
8121 done before any actual redisplay or generation of display lines. */
8122 all_windows = (update_mode_lines
8123 || buffer_shared > 1
8124 || windows_or_buffers_changed);
8125 if (all_windows)
8126 {
8127 Lisp_Object tail, frame;
8128 int count = SPECPDL_INDEX ();
8129
8130 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8131
8132 FOR_EACH_FRAME (tail, frame)
8133 {
8134 f = XFRAME (frame);
8135
8136 /* Ignore tooltip frame. */
8137 if (EQ (frame, tooltip_frame))
8138 continue;
8139
8140 /* If a window on this frame changed size, report that to
8141 the user and clear the size-change flag. */
8142 if (FRAME_WINDOW_SIZES_CHANGED (f))
8143 {
8144 Lisp_Object functions;
8145
8146 /* Clear flag first in case we get an error below. */
8147 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8148 functions = Vwindow_size_change_functions;
8149 GCPRO2 (tail, functions);
8150
8151 while (CONSP (functions))
8152 {
8153 call1 (XCAR (functions), frame);
8154 functions = XCDR (functions);
8155 }
8156 UNGCPRO;
8157 }
8158
8159 GCPRO1 (tail);
8160 update_menu_bar (f, 0);
8161 #ifdef HAVE_WINDOW_SYSTEM
8162 update_tool_bar (f, 0);
8163 #endif
8164 UNGCPRO;
8165 }
8166
8167 unbind_to (count, Qnil);
8168 }
8169 else
8170 {
8171 struct frame *sf = SELECTED_FRAME ();
8172 update_menu_bar (sf, 1);
8173 #ifdef HAVE_WINDOW_SYSTEM
8174 update_tool_bar (sf, 1);
8175 #endif
8176 }
8177
8178 /* Motif needs this. See comment in xmenu.c. Turn it off when
8179 pending_menu_activation is not defined. */
8180 #ifdef USE_X_TOOLKIT
8181 pending_menu_activation = 0;
8182 #endif
8183 }
8184
8185
8186 /* Update the menu bar item list for frame F. This has to be done
8187 before we start to fill in any display lines, because it can call
8188 eval.
8189
8190 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8191
8192 static void
8193 update_menu_bar (f, save_match_data)
8194 struct frame *f;
8195 int save_match_data;
8196 {
8197 Lisp_Object window;
8198 register struct window *w;
8199
8200 /* If called recursively during a menu update, do nothing. This can
8201 happen when, for instance, an activate-menubar-hook causes a
8202 redisplay. */
8203 if (inhibit_menubar_update)
8204 return;
8205
8206 window = FRAME_SELECTED_WINDOW (f);
8207 w = XWINDOW (window);
8208
8209 #if 0 /* The if statement below this if statement used to include the
8210 condition !NILP (w->update_mode_line), rather than using
8211 update_mode_lines directly, and this if statement may have
8212 been added to make that condition work. Now the if
8213 statement below matches its comment, this isn't needed. */
8214 if (update_mode_lines)
8215 w->update_mode_line = Qt;
8216 #endif
8217
8218 if (FRAME_WINDOW_P (f)
8219 ?
8220 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8221 || defined (USE_GTK)
8222 FRAME_EXTERNAL_MENU_BAR (f)
8223 #else
8224 FRAME_MENU_BAR_LINES (f) > 0
8225 #endif
8226 : FRAME_MENU_BAR_LINES (f) > 0)
8227 {
8228 /* If the user has switched buffers or windows, we need to
8229 recompute to reflect the new bindings. But we'll
8230 recompute when update_mode_lines is set too; that means
8231 that people can use force-mode-line-update to request
8232 that the menu bar be recomputed. The adverse effect on
8233 the rest of the redisplay algorithm is about the same as
8234 windows_or_buffers_changed anyway. */
8235 if (windows_or_buffers_changed
8236 /* This used to test w->update_mode_line, but we believe
8237 there is no need to recompute the menu in that case. */
8238 || update_mode_lines
8239 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8240 < BUF_MODIFF (XBUFFER (w->buffer)))
8241 != !NILP (w->last_had_star))
8242 || ((!NILP (Vtransient_mark_mode)
8243 && !NILP (XBUFFER (w->buffer)->mark_active))
8244 != !NILP (w->region_showing)))
8245 {
8246 struct buffer *prev = current_buffer;
8247 int count = SPECPDL_INDEX ();
8248
8249 specbind (Qinhibit_menubar_update, Qt);
8250
8251 set_buffer_internal_1 (XBUFFER (w->buffer));
8252 if (save_match_data)
8253 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8254 if (NILP (Voverriding_local_map_menu_flag))
8255 {
8256 specbind (Qoverriding_terminal_local_map, Qnil);
8257 specbind (Qoverriding_local_map, Qnil);
8258 }
8259
8260 /* Run the Lucid hook. */
8261 safe_run_hooks (Qactivate_menubar_hook);
8262
8263 /* If it has changed current-menubar from previous value,
8264 really recompute the menu-bar from the value. */
8265 if (! NILP (Vlucid_menu_bar_dirty_flag))
8266 call0 (Qrecompute_lucid_menubar);
8267
8268 safe_run_hooks (Qmenu_bar_update_hook);
8269 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8270
8271 /* Redisplay the menu bar in case we changed it. */
8272 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8273 || defined (USE_GTK)
8274 if (FRAME_WINDOW_P (f)
8275 #if defined (MAC_OS)
8276 /* All frames on Mac OS share the same menubar. So only the
8277 selected frame should be allowed to set it. */
8278 && f == SELECTED_FRAME ()
8279 #endif
8280 )
8281 set_frame_menubar (f, 0, 0);
8282 else
8283 /* On a terminal screen, the menu bar is an ordinary screen
8284 line, and this makes it get updated. */
8285 w->update_mode_line = Qt;
8286 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8287 /* In the non-toolkit version, the menu bar is an ordinary screen
8288 line, and this makes it get updated. */
8289 w->update_mode_line = Qt;
8290 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8291
8292 unbind_to (count, Qnil);
8293 set_buffer_internal_1 (prev);
8294 }
8295 }
8296 }
8297
8298
8299 \f
8300 /***********************************************************************
8301 Output Cursor
8302 ***********************************************************************/
8303
8304 #ifdef HAVE_WINDOW_SYSTEM
8305
8306 /* EXPORT:
8307 Nominal cursor position -- where to draw output.
8308 HPOS and VPOS are window relative glyph matrix coordinates.
8309 X and Y are window relative pixel coordinates. */
8310
8311 struct cursor_pos output_cursor;
8312
8313
8314 /* EXPORT:
8315 Set the global variable output_cursor to CURSOR. All cursor
8316 positions are relative to updated_window. */
8317
8318 void
8319 set_output_cursor (cursor)
8320 struct cursor_pos *cursor;
8321 {
8322 output_cursor.hpos = cursor->hpos;
8323 output_cursor.vpos = cursor->vpos;
8324 output_cursor.x = cursor->x;
8325 output_cursor.y = cursor->y;
8326 }
8327
8328
8329 /* EXPORT for RIF:
8330 Set a nominal cursor position.
8331
8332 HPOS and VPOS are column/row positions in a window glyph matrix. X
8333 and Y are window text area relative pixel positions.
8334
8335 If this is done during an update, updated_window will contain the
8336 window that is being updated and the position is the future output
8337 cursor position for that window. If updated_window is null, use
8338 selected_window and display the cursor at the given position. */
8339
8340 void
8341 x_cursor_to (vpos, hpos, y, x)
8342 int vpos, hpos, y, x;
8343 {
8344 struct window *w;
8345
8346 /* If updated_window is not set, work on selected_window. */
8347 if (updated_window)
8348 w = updated_window;
8349 else
8350 w = XWINDOW (selected_window);
8351
8352 /* Set the output cursor. */
8353 output_cursor.hpos = hpos;
8354 output_cursor.vpos = vpos;
8355 output_cursor.x = x;
8356 output_cursor.y = y;
8357
8358 /* If not called as part of an update, really display the cursor.
8359 This will also set the cursor position of W. */
8360 if (updated_window == NULL)
8361 {
8362 BLOCK_INPUT;
8363 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8364 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8365 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8366 UNBLOCK_INPUT;
8367 }
8368 }
8369
8370 #endif /* HAVE_WINDOW_SYSTEM */
8371
8372 \f
8373 /***********************************************************************
8374 Tool-bars
8375 ***********************************************************************/
8376
8377 #ifdef HAVE_WINDOW_SYSTEM
8378
8379 /* Where the mouse was last time we reported a mouse event. */
8380
8381 FRAME_PTR last_mouse_frame;
8382
8383 /* Tool-bar item index of the item on which a mouse button was pressed
8384 or -1. */
8385
8386 int last_tool_bar_item;
8387
8388
8389 /* Update the tool-bar item list for frame F. This has to be done
8390 before we start to fill in any display lines. Called from
8391 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8392 and restore it here. */
8393
8394 static void
8395 update_tool_bar (f, save_match_data)
8396 struct frame *f;
8397 int save_match_data;
8398 {
8399 #ifdef USE_GTK
8400 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8401 #else
8402 int do_update = WINDOWP (f->tool_bar_window)
8403 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8404 #endif
8405
8406 if (do_update)
8407 {
8408 Lisp_Object window;
8409 struct window *w;
8410
8411 window = FRAME_SELECTED_WINDOW (f);
8412 w = XWINDOW (window);
8413
8414 /* If the user has switched buffers or windows, we need to
8415 recompute to reflect the new bindings. But we'll
8416 recompute when update_mode_lines is set too; that means
8417 that people can use force-mode-line-update to request
8418 that the menu bar be recomputed. The adverse effect on
8419 the rest of the redisplay algorithm is about the same as
8420 windows_or_buffers_changed anyway. */
8421 if (windows_or_buffers_changed
8422 || !NILP (w->update_mode_line)
8423 || update_mode_lines
8424 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8425 < BUF_MODIFF (XBUFFER (w->buffer)))
8426 != !NILP (w->last_had_star))
8427 || ((!NILP (Vtransient_mark_mode)
8428 && !NILP (XBUFFER (w->buffer)->mark_active))
8429 != !NILP (w->region_showing)))
8430 {
8431 struct buffer *prev = current_buffer;
8432 int count = SPECPDL_INDEX ();
8433 Lisp_Object new_tool_bar;
8434 int new_n_tool_bar;
8435 struct gcpro gcpro1;
8436
8437 /* Set current_buffer to the buffer of the selected
8438 window of the frame, so that we get the right local
8439 keymaps. */
8440 set_buffer_internal_1 (XBUFFER (w->buffer));
8441
8442 /* Save match data, if we must. */
8443 if (save_match_data)
8444 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8445
8446 /* Make sure that we don't accidentally use bogus keymaps. */
8447 if (NILP (Voverriding_local_map_menu_flag))
8448 {
8449 specbind (Qoverriding_terminal_local_map, Qnil);
8450 specbind (Qoverriding_local_map, Qnil);
8451 }
8452
8453 GCPRO1 (new_tool_bar);
8454
8455 /* Build desired tool-bar items from keymaps. */
8456 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8457 &new_n_tool_bar);
8458
8459 /* Redisplay the tool-bar if we changed it. */
8460 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8461 {
8462 /* Redisplay that happens asynchronously due to an expose event
8463 may access f->tool_bar_items. Make sure we update both
8464 variables within BLOCK_INPUT so no such event interrupts. */
8465 BLOCK_INPUT;
8466 f->tool_bar_items = new_tool_bar;
8467 f->n_tool_bar_items = new_n_tool_bar;
8468 w->update_mode_line = Qt;
8469 UNBLOCK_INPUT;
8470 }
8471
8472 UNGCPRO;
8473
8474 unbind_to (count, Qnil);
8475 set_buffer_internal_1 (prev);
8476 }
8477 }
8478 }
8479
8480
8481 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8482 F's desired tool-bar contents. F->tool_bar_items must have
8483 been set up previously by calling prepare_menu_bars. */
8484
8485 static void
8486 build_desired_tool_bar_string (f)
8487 struct frame *f;
8488 {
8489 int i, size, size_needed;
8490 struct gcpro gcpro1, gcpro2, gcpro3;
8491 Lisp_Object image, plist, props;
8492
8493 image = plist = props = Qnil;
8494 GCPRO3 (image, plist, props);
8495
8496 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8497 Otherwise, make a new string. */
8498
8499 /* The size of the string we might be able to reuse. */
8500 size = (STRINGP (f->desired_tool_bar_string)
8501 ? SCHARS (f->desired_tool_bar_string)
8502 : 0);
8503
8504 /* We need one space in the string for each image. */
8505 size_needed = f->n_tool_bar_items;
8506
8507 /* Reuse f->desired_tool_bar_string, if possible. */
8508 if (size < size_needed || NILP (f->desired_tool_bar_string))
8509 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8510 make_number (' '));
8511 else
8512 {
8513 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8514 Fremove_text_properties (make_number (0), make_number (size),
8515 props, f->desired_tool_bar_string);
8516 }
8517
8518 /* Put a `display' property on the string for the images to display,
8519 put a `menu_item' property on tool-bar items with a value that
8520 is the index of the item in F's tool-bar item vector. */
8521 for (i = 0; i < f->n_tool_bar_items; ++i)
8522 {
8523 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8524
8525 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8526 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8527 int hmargin, vmargin, relief, idx, end;
8528 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8529
8530 /* If image is a vector, choose the image according to the
8531 button state. */
8532 image = PROP (TOOL_BAR_ITEM_IMAGES);
8533 if (VECTORP (image))
8534 {
8535 if (enabled_p)
8536 idx = (selected_p
8537 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8538 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8539 else
8540 idx = (selected_p
8541 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8542 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8543
8544 xassert (ASIZE (image) >= idx);
8545 image = AREF (image, idx);
8546 }
8547 else
8548 idx = -1;
8549
8550 /* Ignore invalid image specifications. */
8551 if (!valid_image_p (image))
8552 continue;
8553
8554 /* Display the tool-bar button pressed, or depressed. */
8555 plist = Fcopy_sequence (XCDR (image));
8556
8557 /* Compute margin and relief to draw. */
8558 relief = (tool_bar_button_relief >= 0
8559 ? tool_bar_button_relief
8560 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8561 hmargin = vmargin = relief;
8562
8563 if (INTEGERP (Vtool_bar_button_margin)
8564 && XINT (Vtool_bar_button_margin) > 0)
8565 {
8566 hmargin += XFASTINT (Vtool_bar_button_margin);
8567 vmargin += XFASTINT (Vtool_bar_button_margin);
8568 }
8569 else if (CONSP (Vtool_bar_button_margin))
8570 {
8571 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8572 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8573 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8574
8575 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8576 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8577 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8578 }
8579
8580 if (auto_raise_tool_bar_buttons_p)
8581 {
8582 /* Add a `:relief' property to the image spec if the item is
8583 selected. */
8584 if (selected_p)
8585 {
8586 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8587 hmargin -= relief;
8588 vmargin -= relief;
8589 }
8590 }
8591 else
8592 {
8593 /* If image is selected, display it pressed, i.e. with a
8594 negative relief. If it's not selected, display it with a
8595 raised relief. */
8596 plist = Fplist_put (plist, QCrelief,
8597 (selected_p
8598 ? make_number (-relief)
8599 : make_number (relief)));
8600 hmargin -= relief;
8601 vmargin -= relief;
8602 }
8603
8604 /* Put a margin around the image. */
8605 if (hmargin || vmargin)
8606 {
8607 if (hmargin == vmargin)
8608 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8609 else
8610 plist = Fplist_put (plist, QCmargin,
8611 Fcons (make_number (hmargin),
8612 make_number (vmargin)));
8613 }
8614
8615 /* If button is not enabled, and we don't have special images
8616 for the disabled state, make the image appear disabled by
8617 applying an appropriate algorithm to it. */
8618 if (!enabled_p && idx < 0)
8619 plist = Fplist_put (plist, QCconversion, Qdisabled);
8620
8621 /* Put a `display' text property on the string for the image to
8622 display. Put a `menu-item' property on the string that gives
8623 the start of this item's properties in the tool-bar items
8624 vector. */
8625 image = Fcons (Qimage, plist);
8626 props = list4 (Qdisplay, image,
8627 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8628
8629 /* Let the last image hide all remaining spaces in the tool bar
8630 string. The string can be longer than needed when we reuse a
8631 previous string. */
8632 if (i + 1 == f->n_tool_bar_items)
8633 end = SCHARS (f->desired_tool_bar_string);
8634 else
8635 end = i + 1;
8636 Fadd_text_properties (make_number (i), make_number (end),
8637 props, f->desired_tool_bar_string);
8638 #undef PROP
8639 }
8640
8641 UNGCPRO;
8642 }
8643
8644
8645 /* Display one line of the tool-bar of frame IT->f. */
8646
8647 static void
8648 display_tool_bar_line (it)
8649 struct it *it;
8650 {
8651 struct glyph_row *row = it->glyph_row;
8652 int max_x = it->last_visible_x;
8653 struct glyph *last;
8654
8655 prepare_desired_row (row);
8656 row->y = it->current_y;
8657
8658 /* Note that this isn't made use of if the face hasn't a box,
8659 so there's no need to check the face here. */
8660 it->start_of_box_run_p = 1;
8661
8662 while (it->current_x < max_x)
8663 {
8664 int x_before, x, n_glyphs_before, i, nglyphs;
8665
8666 /* Get the next display element. */
8667 if (!get_next_display_element (it))
8668 break;
8669
8670 /* Produce glyphs. */
8671 x_before = it->current_x;
8672 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8673 PRODUCE_GLYPHS (it);
8674
8675 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8676 i = 0;
8677 x = x_before;
8678 while (i < nglyphs)
8679 {
8680 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8681
8682 if (x + glyph->pixel_width > max_x)
8683 {
8684 /* Glyph doesn't fit on line. */
8685 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8686 it->current_x = x;
8687 goto out;
8688 }
8689
8690 ++it->hpos;
8691 x += glyph->pixel_width;
8692 ++i;
8693 }
8694
8695 /* Stop at line ends. */
8696 if (ITERATOR_AT_END_OF_LINE_P (it))
8697 break;
8698
8699 set_iterator_to_next (it, 1);
8700 }
8701
8702 out:;
8703
8704 row->displays_text_p = row->used[TEXT_AREA] != 0;
8705 extend_face_to_end_of_line (it);
8706 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8707 last->right_box_line_p = 1;
8708 if (last == row->glyphs[TEXT_AREA])
8709 last->left_box_line_p = 1;
8710 compute_line_metrics (it);
8711
8712 /* If line is empty, make it occupy the rest of the tool-bar. */
8713 if (!row->displays_text_p)
8714 {
8715 row->height = row->phys_height = it->last_visible_y - row->y;
8716 row->ascent = row->phys_ascent = 0;
8717 row->extra_line_spacing = 0;
8718 }
8719
8720 row->full_width_p = 1;
8721 row->continued_p = 0;
8722 row->truncated_on_left_p = 0;
8723 row->truncated_on_right_p = 0;
8724
8725 it->current_x = it->hpos = 0;
8726 it->current_y += row->height;
8727 ++it->vpos;
8728 ++it->glyph_row;
8729 }
8730
8731
8732 /* Value is the number of screen lines needed to make all tool-bar
8733 items of frame F visible. */
8734
8735 static int
8736 tool_bar_lines_needed (f)
8737 struct frame *f;
8738 {
8739 struct window *w = XWINDOW (f->tool_bar_window);
8740 struct it it;
8741
8742 /* Initialize an iterator for iteration over
8743 F->desired_tool_bar_string in the tool-bar window of frame F. */
8744 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8745 it.first_visible_x = 0;
8746 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8747 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8748
8749 while (!ITERATOR_AT_END_P (&it))
8750 {
8751 it.glyph_row = w->desired_matrix->rows;
8752 clear_glyph_row (it.glyph_row);
8753 display_tool_bar_line (&it);
8754 }
8755
8756 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8757 }
8758
8759
8760 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8761 0, 1, 0,
8762 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8763 (frame)
8764 Lisp_Object frame;
8765 {
8766 struct frame *f;
8767 struct window *w;
8768 int nlines = 0;
8769
8770 if (NILP (frame))
8771 frame = selected_frame;
8772 else
8773 CHECK_FRAME (frame);
8774 f = XFRAME (frame);
8775
8776 if (WINDOWP (f->tool_bar_window)
8777 || (w = XWINDOW (f->tool_bar_window),
8778 WINDOW_TOTAL_LINES (w) > 0))
8779 {
8780 update_tool_bar (f, 1);
8781 if (f->n_tool_bar_items)
8782 {
8783 build_desired_tool_bar_string (f);
8784 nlines = tool_bar_lines_needed (f);
8785 }
8786 }
8787
8788 return make_number (nlines);
8789 }
8790
8791
8792 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8793 height should be changed. */
8794
8795 static int
8796 redisplay_tool_bar (f)
8797 struct frame *f;
8798 {
8799 struct window *w;
8800 struct it it;
8801 struct glyph_row *row;
8802 int change_height_p = 0;
8803
8804 #ifdef USE_GTK
8805 if (FRAME_EXTERNAL_TOOL_BAR (f))
8806 update_frame_tool_bar (f);
8807 return 0;
8808 #endif
8809
8810 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8811 do anything. This means you must start with tool-bar-lines
8812 non-zero to get the auto-sizing effect. Or in other words, you
8813 can turn off tool-bars by specifying tool-bar-lines zero. */
8814 if (!WINDOWP (f->tool_bar_window)
8815 || (w = XWINDOW (f->tool_bar_window),
8816 WINDOW_TOTAL_LINES (w) == 0))
8817 return 0;
8818
8819 /* Set up an iterator for the tool-bar window. */
8820 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8821 it.first_visible_x = 0;
8822 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8823 row = it.glyph_row;
8824
8825 /* Build a string that represents the contents of the tool-bar. */
8826 build_desired_tool_bar_string (f);
8827 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8828
8829 /* Display as many lines as needed to display all tool-bar items. */
8830 while (it.current_y < it.last_visible_y)
8831 display_tool_bar_line (&it);
8832
8833 /* It doesn't make much sense to try scrolling in the tool-bar
8834 window, so don't do it. */
8835 w->desired_matrix->no_scrolling_p = 1;
8836 w->must_be_updated_p = 1;
8837
8838 if (auto_resize_tool_bars_p)
8839 {
8840 int nlines;
8841
8842 /* If we couldn't display everything, change the tool-bar's
8843 height. */
8844 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8845 change_height_p = 1;
8846
8847 /* If there are blank lines at the end, except for a partially
8848 visible blank line at the end that is smaller than
8849 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8850 row = it.glyph_row - 1;
8851 if (!row->displays_text_p
8852 && row->height >= FRAME_LINE_HEIGHT (f))
8853 change_height_p = 1;
8854
8855 /* If row displays tool-bar items, but is partially visible,
8856 change the tool-bar's height. */
8857 if (row->displays_text_p
8858 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8859 change_height_p = 1;
8860
8861 /* Resize windows as needed by changing the `tool-bar-lines'
8862 frame parameter. */
8863 if (change_height_p
8864 && (nlines = tool_bar_lines_needed (f),
8865 nlines != WINDOW_TOTAL_LINES (w)))
8866 {
8867 extern Lisp_Object Qtool_bar_lines;
8868 Lisp_Object frame;
8869 int old_height = WINDOW_TOTAL_LINES (w);
8870
8871 XSETFRAME (frame, f);
8872 clear_glyph_matrix (w->desired_matrix);
8873 Fmodify_frame_parameters (frame,
8874 Fcons (Fcons (Qtool_bar_lines,
8875 make_number (nlines)),
8876 Qnil));
8877 if (WINDOW_TOTAL_LINES (w) != old_height)
8878 fonts_changed_p = 1;
8879 }
8880 }
8881
8882 return change_height_p;
8883 }
8884
8885
8886 /* Get information about the tool-bar item which is displayed in GLYPH
8887 on frame F. Return in *PROP_IDX the index where tool-bar item
8888 properties start in F->tool_bar_items. Value is zero if
8889 GLYPH doesn't display a tool-bar item. */
8890
8891 static int
8892 tool_bar_item_info (f, glyph, prop_idx)
8893 struct frame *f;
8894 struct glyph *glyph;
8895 int *prop_idx;
8896 {
8897 Lisp_Object prop;
8898 int success_p;
8899 int charpos;
8900
8901 /* This function can be called asynchronously, which means we must
8902 exclude any possibility that Fget_text_property signals an
8903 error. */
8904 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8905 charpos = max (0, charpos);
8906
8907 /* Get the text property `menu-item' at pos. The value of that
8908 property is the start index of this item's properties in
8909 F->tool_bar_items. */
8910 prop = Fget_text_property (make_number (charpos),
8911 Qmenu_item, f->current_tool_bar_string);
8912 if (INTEGERP (prop))
8913 {
8914 *prop_idx = XINT (prop);
8915 success_p = 1;
8916 }
8917 else
8918 success_p = 0;
8919
8920 return success_p;
8921 }
8922
8923 \f
8924 /* Get information about the tool-bar item at position X/Y on frame F.
8925 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8926 the current matrix of the tool-bar window of F, or NULL if not
8927 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8928 item in F->tool_bar_items. Value is
8929
8930 -1 if X/Y is not on a tool-bar item
8931 0 if X/Y is on the same item that was highlighted before.
8932 1 otherwise. */
8933
8934 static int
8935 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8936 struct frame *f;
8937 int x, y;
8938 struct glyph **glyph;
8939 int *hpos, *vpos, *prop_idx;
8940 {
8941 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8942 struct window *w = XWINDOW (f->tool_bar_window);
8943 int area;
8944
8945 /* Find the glyph under X/Y. */
8946 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8947 if (*glyph == NULL)
8948 return -1;
8949
8950 /* Get the start of this tool-bar item's properties in
8951 f->tool_bar_items. */
8952 if (!tool_bar_item_info (f, *glyph, prop_idx))
8953 return -1;
8954
8955 /* Is mouse on the highlighted item? */
8956 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8957 && *vpos >= dpyinfo->mouse_face_beg_row
8958 && *vpos <= dpyinfo->mouse_face_end_row
8959 && (*vpos > dpyinfo->mouse_face_beg_row
8960 || *hpos >= dpyinfo->mouse_face_beg_col)
8961 && (*vpos < dpyinfo->mouse_face_end_row
8962 || *hpos < dpyinfo->mouse_face_end_col
8963 || dpyinfo->mouse_face_past_end))
8964 return 0;
8965
8966 return 1;
8967 }
8968
8969
8970 /* EXPORT:
8971 Handle mouse button event on the tool-bar of frame F, at
8972 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8973 0 for button release. MODIFIERS is event modifiers for button
8974 release. */
8975
8976 void
8977 handle_tool_bar_click (f, x, y, down_p, modifiers)
8978 struct frame *f;
8979 int x, y, down_p;
8980 unsigned int modifiers;
8981 {
8982 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8983 struct window *w = XWINDOW (f->tool_bar_window);
8984 int hpos, vpos, prop_idx;
8985 struct glyph *glyph;
8986 Lisp_Object enabled_p;
8987
8988 /* If not on the highlighted tool-bar item, return. */
8989 frame_to_window_pixel_xy (w, &x, &y);
8990 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8991 return;
8992
8993 /* If item is disabled, do nothing. */
8994 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8995 if (NILP (enabled_p))
8996 return;
8997
8998 if (down_p)
8999 {
9000 /* Show item in pressed state. */
9001 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9002 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9003 last_tool_bar_item = prop_idx;
9004 }
9005 else
9006 {
9007 Lisp_Object key, frame;
9008 struct input_event event;
9009 EVENT_INIT (event);
9010
9011 /* Show item in released state. */
9012 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9013 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9014
9015 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9016
9017 XSETFRAME (frame, f);
9018 event.kind = TOOL_BAR_EVENT;
9019 event.frame_or_window = frame;
9020 event.arg = frame;
9021 kbd_buffer_store_event (&event);
9022
9023 event.kind = TOOL_BAR_EVENT;
9024 event.frame_or_window = frame;
9025 event.arg = key;
9026 event.modifiers = modifiers;
9027 kbd_buffer_store_event (&event);
9028 last_tool_bar_item = -1;
9029 }
9030 }
9031
9032
9033 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9034 tool-bar window-relative coordinates X/Y. Called from
9035 note_mouse_highlight. */
9036
9037 static void
9038 note_tool_bar_highlight (f, x, y)
9039 struct frame *f;
9040 int x, y;
9041 {
9042 Lisp_Object window = f->tool_bar_window;
9043 struct window *w = XWINDOW (window);
9044 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9045 int hpos, vpos;
9046 struct glyph *glyph;
9047 struct glyph_row *row;
9048 int i;
9049 Lisp_Object enabled_p;
9050 int prop_idx;
9051 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9052 int mouse_down_p, rc;
9053
9054 /* Function note_mouse_highlight is called with negative x(y
9055 values when mouse moves outside of the frame. */
9056 if (x <= 0 || y <= 0)
9057 {
9058 clear_mouse_face (dpyinfo);
9059 return;
9060 }
9061
9062 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9063 if (rc < 0)
9064 {
9065 /* Not on tool-bar item. */
9066 clear_mouse_face (dpyinfo);
9067 return;
9068 }
9069 else if (rc == 0)
9070 /* On same tool-bar item as before. */
9071 goto set_help_echo;
9072
9073 clear_mouse_face (dpyinfo);
9074
9075 /* Mouse is down, but on different tool-bar item? */
9076 mouse_down_p = (dpyinfo->grabbed
9077 && f == last_mouse_frame
9078 && FRAME_LIVE_P (f));
9079 if (mouse_down_p
9080 && last_tool_bar_item != prop_idx)
9081 return;
9082
9083 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9084 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9085
9086 /* If tool-bar item is not enabled, don't highlight it. */
9087 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9088 if (!NILP (enabled_p))
9089 {
9090 /* Compute the x-position of the glyph. In front and past the
9091 image is a space. We include this in the highlighted area. */
9092 row = MATRIX_ROW (w->current_matrix, vpos);
9093 for (i = x = 0; i < hpos; ++i)
9094 x += row->glyphs[TEXT_AREA][i].pixel_width;
9095
9096 /* Record this as the current active region. */
9097 dpyinfo->mouse_face_beg_col = hpos;
9098 dpyinfo->mouse_face_beg_row = vpos;
9099 dpyinfo->mouse_face_beg_x = x;
9100 dpyinfo->mouse_face_beg_y = row->y;
9101 dpyinfo->mouse_face_past_end = 0;
9102
9103 dpyinfo->mouse_face_end_col = hpos + 1;
9104 dpyinfo->mouse_face_end_row = vpos;
9105 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9106 dpyinfo->mouse_face_end_y = row->y;
9107 dpyinfo->mouse_face_window = window;
9108 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9109
9110 /* Display it as active. */
9111 show_mouse_face (dpyinfo, draw);
9112 dpyinfo->mouse_face_image_state = draw;
9113 }
9114
9115 set_help_echo:
9116
9117 /* Set help_echo_string to a help string to display for this tool-bar item.
9118 XTread_socket does the rest. */
9119 help_echo_object = help_echo_window = Qnil;
9120 help_echo_pos = -1;
9121 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9122 if (NILP (help_echo_string))
9123 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9124 }
9125
9126 #endif /* HAVE_WINDOW_SYSTEM */
9127
9128
9129 \f
9130 /************************************************************************
9131 Horizontal scrolling
9132 ************************************************************************/
9133
9134 static int hscroll_window_tree P_ ((Lisp_Object));
9135 static int hscroll_windows P_ ((Lisp_Object));
9136
9137 /* For all leaf windows in the window tree rooted at WINDOW, set their
9138 hscroll value so that PT is (i) visible in the window, and (ii) so
9139 that it is not within a certain margin at the window's left and
9140 right border. Value is non-zero if any window's hscroll has been
9141 changed. */
9142
9143 static int
9144 hscroll_window_tree (window)
9145 Lisp_Object window;
9146 {
9147 int hscrolled_p = 0;
9148 int hscroll_relative_p = FLOATP (Vhscroll_step);
9149 int hscroll_step_abs = 0;
9150 double hscroll_step_rel = 0;
9151
9152 if (hscroll_relative_p)
9153 {
9154 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9155 if (hscroll_step_rel < 0)
9156 {
9157 hscroll_relative_p = 0;
9158 hscroll_step_abs = 0;
9159 }
9160 }
9161 else if (INTEGERP (Vhscroll_step))
9162 {
9163 hscroll_step_abs = XINT (Vhscroll_step);
9164 if (hscroll_step_abs < 0)
9165 hscroll_step_abs = 0;
9166 }
9167 else
9168 hscroll_step_abs = 0;
9169
9170 while (WINDOWP (window))
9171 {
9172 struct window *w = XWINDOW (window);
9173
9174 if (WINDOWP (w->hchild))
9175 hscrolled_p |= hscroll_window_tree (w->hchild);
9176 else if (WINDOWP (w->vchild))
9177 hscrolled_p |= hscroll_window_tree (w->vchild);
9178 else if (w->cursor.vpos >= 0)
9179 {
9180 int h_margin;
9181 int text_area_width;
9182 struct glyph_row *current_cursor_row
9183 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9184 struct glyph_row *desired_cursor_row
9185 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9186 struct glyph_row *cursor_row
9187 = (desired_cursor_row->enabled_p
9188 ? desired_cursor_row
9189 : current_cursor_row);
9190
9191 text_area_width = window_box_width (w, TEXT_AREA);
9192
9193 /* Scroll when cursor is inside this scroll margin. */
9194 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9195
9196 if ((XFASTINT (w->hscroll)
9197 && w->cursor.x <= h_margin)
9198 || (cursor_row->enabled_p
9199 && cursor_row->truncated_on_right_p
9200 && (w->cursor.x >= text_area_width - h_margin)))
9201 {
9202 struct it it;
9203 int hscroll;
9204 struct buffer *saved_current_buffer;
9205 int pt;
9206 int wanted_x;
9207
9208 /* Find point in a display of infinite width. */
9209 saved_current_buffer = current_buffer;
9210 current_buffer = XBUFFER (w->buffer);
9211
9212 if (w == XWINDOW (selected_window))
9213 pt = BUF_PT (current_buffer);
9214 else
9215 {
9216 pt = marker_position (w->pointm);
9217 pt = max (BEGV, pt);
9218 pt = min (ZV, pt);
9219 }
9220
9221 /* Move iterator to pt starting at cursor_row->start in
9222 a line with infinite width. */
9223 init_to_row_start (&it, w, cursor_row);
9224 it.last_visible_x = INFINITY;
9225 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9226 current_buffer = saved_current_buffer;
9227
9228 /* Position cursor in window. */
9229 if (!hscroll_relative_p && hscroll_step_abs == 0)
9230 hscroll = max (0, (it.current_x
9231 - (ITERATOR_AT_END_OF_LINE_P (&it)
9232 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9233 : (text_area_width / 2))))
9234 / FRAME_COLUMN_WIDTH (it.f);
9235 else if (w->cursor.x >= text_area_width - h_margin)
9236 {
9237 if (hscroll_relative_p)
9238 wanted_x = text_area_width * (1 - hscroll_step_rel)
9239 - h_margin;
9240 else
9241 wanted_x = text_area_width
9242 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9243 - h_margin;
9244 hscroll
9245 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9246 }
9247 else
9248 {
9249 if (hscroll_relative_p)
9250 wanted_x = text_area_width * hscroll_step_rel
9251 + h_margin;
9252 else
9253 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9254 + h_margin;
9255 hscroll
9256 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9257 }
9258 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9259
9260 /* Don't call Fset_window_hscroll if value hasn't
9261 changed because it will prevent redisplay
9262 optimizations. */
9263 if (XFASTINT (w->hscroll) != hscroll)
9264 {
9265 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9266 w->hscroll = make_number (hscroll);
9267 hscrolled_p = 1;
9268 }
9269 }
9270 }
9271
9272 window = w->next;
9273 }
9274
9275 /* Value is non-zero if hscroll of any leaf window has been changed. */
9276 return hscrolled_p;
9277 }
9278
9279
9280 /* Set hscroll so that cursor is visible and not inside horizontal
9281 scroll margins for all windows in the tree rooted at WINDOW. See
9282 also hscroll_window_tree above. Value is non-zero if any window's
9283 hscroll has been changed. If it has, desired matrices on the frame
9284 of WINDOW are cleared. */
9285
9286 static int
9287 hscroll_windows (window)
9288 Lisp_Object window;
9289 {
9290 int hscrolled_p;
9291
9292 if (automatic_hscrolling_p)
9293 {
9294 hscrolled_p = hscroll_window_tree (window);
9295 if (hscrolled_p)
9296 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9297 }
9298 else
9299 hscrolled_p = 0;
9300 return hscrolled_p;
9301 }
9302
9303
9304 \f
9305 /************************************************************************
9306 Redisplay
9307 ************************************************************************/
9308
9309 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9310 to a non-zero value. This is sometimes handy to have in a debugger
9311 session. */
9312
9313 #if GLYPH_DEBUG
9314
9315 /* First and last unchanged row for try_window_id. */
9316
9317 int debug_first_unchanged_at_end_vpos;
9318 int debug_last_unchanged_at_beg_vpos;
9319
9320 /* Delta vpos and y. */
9321
9322 int debug_dvpos, debug_dy;
9323
9324 /* Delta in characters and bytes for try_window_id. */
9325
9326 int debug_delta, debug_delta_bytes;
9327
9328 /* Values of window_end_pos and window_end_vpos at the end of
9329 try_window_id. */
9330
9331 EMACS_INT debug_end_pos, debug_end_vpos;
9332
9333 /* Append a string to W->desired_matrix->method. FMT is a printf
9334 format string. A1...A9 are a supplement for a variable-length
9335 argument list. If trace_redisplay_p is non-zero also printf the
9336 resulting string to stderr. */
9337
9338 static void
9339 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9340 struct window *w;
9341 char *fmt;
9342 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9343 {
9344 char buffer[512];
9345 char *method = w->desired_matrix->method;
9346 int len = strlen (method);
9347 int size = sizeof w->desired_matrix->method;
9348 int remaining = size - len - 1;
9349
9350 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9351 if (len && remaining)
9352 {
9353 method[len] = '|';
9354 --remaining, ++len;
9355 }
9356
9357 strncpy (method + len, buffer, remaining);
9358
9359 if (trace_redisplay_p)
9360 fprintf (stderr, "%p (%s): %s\n",
9361 w,
9362 ((BUFFERP (w->buffer)
9363 && STRINGP (XBUFFER (w->buffer)->name))
9364 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9365 : "no buffer"),
9366 buffer);
9367 }
9368
9369 #endif /* GLYPH_DEBUG */
9370
9371
9372 /* Value is non-zero if all changes in window W, which displays
9373 current_buffer, are in the text between START and END. START is a
9374 buffer position, END is given as a distance from Z. Used in
9375 redisplay_internal for display optimization. */
9376
9377 static INLINE int
9378 text_outside_line_unchanged_p (w, start, end)
9379 struct window *w;
9380 int start, end;
9381 {
9382 int unchanged_p = 1;
9383
9384 /* If text or overlays have changed, see where. */
9385 if (XFASTINT (w->last_modified) < MODIFF
9386 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9387 {
9388 /* Gap in the line? */
9389 if (GPT < start || Z - GPT < end)
9390 unchanged_p = 0;
9391
9392 /* Changes start in front of the line, or end after it? */
9393 if (unchanged_p
9394 && (BEG_UNCHANGED < start - 1
9395 || END_UNCHANGED < end))
9396 unchanged_p = 0;
9397
9398 /* If selective display, can't optimize if changes start at the
9399 beginning of the line. */
9400 if (unchanged_p
9401 && INTEGERP (current_buffer->selective_display)
9402 && XINT (current_buffer->selective_display) > 0
9403 && (BEG_UNCHANGED < start || GPT <= start))
9404 unchanged_p = 0;
9405
9406 /* If there are overlays at the start or end of the line, these
9407 may have overlay strings with newlines in them. A change at
9408 START, for instance, may actually concern the display of such
9409 overlay strings as well, and they are displayed on different
9410 lines. So, quickly rule out this case. (For the future, it
9411 might be desirable to implement something more telling than
9412 just BEG/END_UNCHANGED.) */
9413 if (unchanged_p)
9414 {
9415 if (BEG + BEG_UNCHANGED == start
9416 && overlay_touches_p (start))
9417 unchanged_p = 0;
9418 if (END_UNCHANGED == end
9419 && overlay_touches_p (Z - end))
9420 unchanged_p = 0;
9421 }
9422 }
9423
9424 return unchanged_p;
9425 }
9426
9427
9428 /* Do a frame update, taking possible shortcuts into account. This is
9429 the main external entry point for redisplay.
9430
9431 If the last redisplay displayed an echo area message and that message
9432 is no longer requested, we clear the echo area or bring back the
9433 mini-buffer if that is in use. */
9434
9435 void
9436 redisplay ()
9437 {
9438 redisplay_internal (0);
9439 }
9440
9441
9442 static Lisp_Object
9443 overlay_arrow_string_or_property (var, pbitmap)
9444 Lisp_Object var;
9445 int *pbitmap;
9446 {
9447 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9448 Lisp_Object bitmap;
9449
9450 if (pbitmap)
9451 {
9452 *pbitmap = 0;
9453 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9454 *pbitmap = XINT (bitmap);
9455 }
9456
9457 if (!NILP (pstr))
9458 return pstr;
9459 return Voverlay_arrow_string;
9460 }
9461
9462 /* Return 1 if there are any overlay-arrows in current_buffer. */
9463 static int
9464 overlay_arrow_in_current_buffer_p ()
9465 {
9466 Lisp_Object vlist;
9467
9468 for (vlist = Voverlay_arrow_variable_list;
9469 CONSP (vlist);
9470 vlist = XCDR (vlist))
9471 {
9472 Lisp_Object var = XCAR (vlist);
9473 Lisp_Object val;
9474
9475 if (!SYMBOLP (var))
9476 continue;
9477 val = find_symbol_value (var);
9478 if (MARKERP (val)
9479 && current_buffer == XMARKER (val)->buffer)
9480 return 1;
9481 }
9482 return 0;
9483 }
9484
9485
9486 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9487 has changed. */
9488
9489 static int
9490 overlay_arrows_changed_p ()
9491 {
9492 Lisp_Object vlist;
9493
9494 for (vlist = Voverlay_arrow_variable_list;
9495 CONSP (vlist);
9496 vlist = XCDR (vlist))
9497 {
9498 Lisp_Object var = XCAR (vlist);
9499 Lisp_Object val, pstr;
9500
9501 if (!SYMBOLP (var))
9502 continue;
9503 val = find_symbol_value (var);
9504 if (!MARKERP (val))
9505 continue;
9506 if (! EQ (COERCE_MARKER (val),
9507 Fget (var, Qlast_arrow_position))
9508 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9509 EQ (pstr, Fget (var, Qlast_arrow_string))))
9510 return 1;
9511 }
9512 return 0;
9513 }
9514
9515 /* Mark overlay arrows to be updated on next redisplay. */
9516
9517 static void
9518 update_overlay_arrows (up_to_date)
9519 int up_to_date;
9520 {
9521 Lisp_Object vlist;
9522
9523 for (vlist = Voverlay_arrow_variable_list;
9524 CONSP (vlist);
9525 vlist = XCDR (vlist))
9526 {
9527 Lisp_Object var = XCAR (vlist);
9528
9529 if (!SYMBOLP (var))
9530 continue;
9531
9532 if (up_to_date > 0)
9533 {
9534 Lisp_Object val = find_symbol_value (var);
9535 Fput (var, Qlast_arrow_position,
9536 COERCE_MARKER (val));
9537 Fput (var, Qlast_arrow_string,
9538 overlay_arrow_string_or_property (var, 0));
9539 }
9540 else if (up_to_date < 0
9541 || !NILP (Fget (var, Qlast_arrow_position)))
9542 {
9543 Fput (var, Qlast_arrow_position, Qt);
9544 Fput (var, Qlast_arrow_string, Qt);
9545 }
9546 }
9547 }
9548
9549
9550 /* Return overlay arrow string to display at row.
9551 Return t if display as bitmap in left fringe.
9552 Return nil if no overlay arrow. */
9553
9554 static Lisp_Object
9555 overlay_arrow_at_row (it, row, pbitmap)
9556 struct it *it;
9557 struct glyph_row *row;
9558 int *pbitmap;
9559 {
9560 Lisp_Object vlist;
9561
9562 for (vlist = Voverlay_arrow_variable_list;
9563 CONSP (vlist);
9564 vlist = XCDR (vlist))
9565 {
9566 Lisp_Object var = XCAR (vlist);
9567 Lisp_Object val;
9568
9569 if (!SYMBOLP (var))
9570 continue;
9571
9572 val = find_symbol_value (var);
9573
9574 if (MARKERP (val)
9575 && current_buffer == XMARKER (val)->buffer
9576 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9577 {
9578 val = overlay_arrow_string_or_property (var, pbitmap);
9579 if (FRAME_WINDOW_P (it->f)
9580 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9581 return Qt;
9582 if (STRINGP (val))
9583 return val;
9584 break;
9585 }
9586 }
9587
9588 *pbitmap = 0;
9589 return Qnil;
9590 }
9591
9592 /* Return 1 if point moved out of or into a composition. Otherwise
9593 return 0. PREV_BUF and PREV_PT are the last point buffer and
9594 position. BUF and PT are the current point buffer and position. */
9595
9596 int
9597 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9598 struct buffer *prev_buf, *buf;
9599 int prev_pt, pt;
9600 {
9601 int start, end;
9602 Lisp_Object prop;
9603 Lisp_Object buffer;
9604
9605 XSETBUFFER (buffer, buf);
9606 /* Check a composition at the last point if point moved within the
9607 same buffer. */
9608 if (prev_buf == buf)
9609 {
9610 if (prev_pt == pt)
9611 /* Point didn't move. */
9612 return 0;
9613
9614 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9615 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9616 && COMPOSITION_VALID_P (start, end, prop)
9617 && start < prev_pt && end > prev_pt)
9618 /* The last point was within the composition. Return 1 iff
9619 point moved out of the composition. */
9620 return (pt <= start || pt >= end);
9621 }
9622
9623 /* Check a composition at the current point. */
9624 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9625 && find_composition (pt, -1, &start, &end, &prop, buffer)
9626 && COMPOSITION_VALID_P (start, end, prop)
9627 && start < pt && end > pt);
9628 }
9629
9630
9631 /* Reconsider the setting of B->clip_changed which is displayed
9632 in window W. */
9633
9634 static INLINE void
9635 reconsider_clip_changes (w, b)
9636 struct window *w;
9637 struct buffer *b;
9638 {
9639 if (b->clip_changed
9640 && !NILP (w->window_end_valid)
9641 && w->current_matrix->buffer == b
9642 && w->current_matrix->zv == BUF_ZV (b)
9643 && w->current_matrix->begv == BUF_BEGV (b))
9644 b->clip_changed = 0;
9645
9646 /* If display wasn't paused, and W is not a tool bar window, see if
9647 point has been moved into or out of a composition. In that case,
9648 we set b->clip_changed to 1 to force updating the screen. If
9649 b->clip_changed has already been set to 1, we can skip this
9650 check. */
9651 if (!b->clip_changed
9652 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9653 {
9654 int pt;
9655
9656 if (w == XWINDOW (selected_window))
9657 pt = BUF_PT (current_buffer);
9658 else
9659 pt = marker_position (w->pointm);
9660
9661 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9662 || pt != XINT (w->last_point))
9663 && check_point_in_composition (w->current_matrix->buffer,
9664 XINT (w->last_point),
9665 XBUFFER (w->buffer), pt))
9666 b->clip_changed = 1;
9667 }
9668 }
9669 \f
9670
9671 /* Select FRAME to forward the values of frame-local variables into C
9672 variables so that the redisplay routines can access those values
9673 directly. */
9674
9675 static void
9676 select_frame_for_redisplay (frame)
9677 Lisp_Object frame;
9678 {
9679 Lisp_Object tail, sym, val;
9680 Lisp_Object old = selected_frame;
9681
9682 selected_frame = frame;
9683
9684 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9685 if (CONSP (XCAR (tail))
9686 && (sym = XCAR (XCAR (tail)),
9687 SYMBOLP (sym))
9688 && (sym = indirect_variable (sym),
9689 val = SYMBOL_VALUE (sym),
9690 (BUFFER_LOCAL_VALUEP (val)
9691 || SOME_BUFFER_LOCAL_VALUEP (val)))
9692 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9693 Fsymbol_value (sym);
9694
9695 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9696 if (CONSP (XCAR (tail))
9697 && (sym = XCAR (XCAR (tail)),
9698 SYMBOLP (sym))
9699 && (sym = indirect_variable (sym),
9700 val = SYMBOL_VALUE (sym),
9701 (BUFFER_LOCAL_VALUEP (val)
9702 || SOME_BUFFER_LOCAL_VALUEP (val)))
9703 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9704 Fsymbol_value (sym);
9705 }
9706
9707
9708 #define STOP_POLLING \
9709 do { if (! polling_stopped_here) stop_polling (); \
9710 polling_stopped_here = 1; } while (0)
9711
9712 #define RESUME_POLLING \
9713 do { if (polling_stopped_here) start_polling (); \
9714 polling_stopped_here = 0; } while (0)
9715
9716
9717 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9718 response to any user action; therefore, we should preserve the echo
9719 area. (Actually, our caller does that job.) Perhaps in the future
9720 avoid recentering windows if it is not necessary; currently that
9721 causes some problems. */
9722
9723 static void
9724 redisplay_internal (preserve_echo_area)
9725 int preserve_echo_area;
9726 {
9727 struct window *w = XWINDOW (selected_window);
9728 struct frame *f = XFRAME (w->frame);
9729 int pause;
9730 int must_finish = 0;
9731 struct text_pos tlbufpos, tlendpos;
9732 int number_of_visible_frames;
9733 int count;
9734 struct frame *sf = SELECTED_FRAME ();
9735 int polling_stopped_here = 0;
9736
9737 /* Non-zero means redisplay has to consider all windows on all
9738 frames. Zero means, only selected_window is considered. */
9739 int consider_all_windows_p;
9740
9741 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9742
9743 /* No redisplay if running in batch mode or frame is not yet fully
9744 initialized, or redisplay is explicitly turned off by setting
9745 Vinhibit_redisplay. */
9746 if (noninteractive
9747 || !NILP (Vinhibit_redisplay)
9748 || !f->glyphs_initialized_p)
9749 return;
9750
9751 /* The flag redisplay_performed_directly_p is set by
9752 direct_output_for_insert when it already did the whole screen
9753 update necessary. */
9754 if (redisplay_performed_directly_p)
9755 {
9756 redisplay_performed_directly_p = 0;
9757 if (!hscroll_windows (selected_window))
9758 return;
9759 }
9760
9761 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9762 if (popup_activated ())
9763 return;
9764 #endif
9765
9766 /* I don't think this happens but let's be paranoid. */
9767 if (redisplaying_p)
9768 return;
9769
9770 /* Record a function that resets redisplaying_p to its old value
9771 when we leave this function. */
9772 count = SPECPDL_INDEX ();
9773 record_unwind_protect (unwind_redisplay,
9774 Fcons (make_number (redisplaying_p), selected_frame));
9775 ++redisplaying_p;
9776 specbind (Qinhibit_free_realized_faces, Qnil);
9777
9778 retry:
9779 pause = 0;
9780 reconsider_clip_changes (w, current_buffer);
9781
9782 /* If new fonts have been loaded that make a glyph matrix adjustment
9783 necessary, do it. */
9784 if (fonts_changed_p)
9785 {
9786 adjust_glyphs (NULL);
9787 ++windows_or_buffers_changed;
9788 fonts_changed_p = 0;
9789 }
9790
9791 /* If face_change_count is non-zero, init_iterator will free all
9792 realized faces, which includes the faces referenced from current
9793 matrices. So, we can't reuse current matrices in this case. */
9794 if (face_change_count)
9795 ++windows_or_buffers_changed;
9796
9797 if (FRAME_TERMCAP_P (sf)
9798 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9799 {
9800 /* Since frames on a single ASCII terminal share the same
9801 display area, displaying a different frame means redisplay
9802 the whole thing. */
9803 windows_or_buffers_changed++;
9804 SET_FRAME_GARBAGED (sf);
9805 FRAME_TTY (sf)->previous_terminal_frame = sf;
9806 }
9807
9808 /* Set the visible flags for all frames. Do this before checking
9809 for resized or garbaged frames; they want to know if their frames
9810 are visible. See the comment in frame.h for
9811 FRAME_SAMPLE_VISIBILITY. */
9812 {
9813 Lisp_Object tail, frame;
9814
9815 number_of_visible_frames = 0;
9816
9817 FOR_EACH_FRAME (tail, frame)
9818 {
9819 struct frame *f = XFRAME (frame);
9820
9821 FRAME_SAMPLE_VISIBILITY (f);
9822 if (FRAME_VISIBLE_P (f))
9823 ++number_of_visible_frames;
9824 clear_desired_matrices (f);
9825 }
9826 }
9827
9828
9829 /* Notice any pending interrupt request to change frame size. */
9830 do_pending_window_change (1);
9831
9832 /* Clear frames marked as garbaged. */
9833 if (frame_garbaged)
9834 clear_garbaged_frames ();
9835
9836 /* Build menubar and tool-bar items. */
9837 prepare_menu_bars ();
9838
9839 if (windows_or_buffers_changed)
9840 update_mode_lines++;
9841
9842 /* Detect case that we need to write or remove a star in the mode line. */
9843 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9844 {
9845 w->update_mode_line = Qt;
9846 if (buffer_shared > 1)
9847 update_mode_lines++;
9848 }
9849
9850 /* If %c is in the mode line, update it if needed. */
9851 if (!NILP (w->column_number_displayed)
9852 /* This alternative quickly identifies a common case
9853 where no change is needed. */
9854 && !(PT == XFASTINT (w->last_point)
9855 && XFASTINT (w->last_modified) >= MODIFF
9856 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9857 && (XFASTINT (w->column_number_displayed)
9858 != (int) current_column ())) /* iftc */
9859 w->update_mode_line = Qt;
9860
9861 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9862
9863 /* The variable buffer_shared is set in redisplay_window and
9864 indicates that we redisplay a buffer in different windows. See
9865 there. */
9866 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9867 || cursor_type_changed);
9868
9869 /* If specs for an arrow have changed, do thorough redisplay
9870 to ensure we remove any arrow that should no longer exist. */
9871 if (overlay_arrows_changed_p ())
9872 consider_all_windows_p = windows_or_buffers_changed = 1;
9873
9874 /* Normally the message* functions will have already displayed and
9875 updated the echo area, but the frame may have been trashed, or
9876 the update may have been preempted, so display the echo area
9877 again here. Checking message_cleared_p captures the case that
9878 the echo area should be cleared. */
9879 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9880 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9881 || (message_cleared_p
9882 && minibuf_level == 0
9883 /* If the mini-window is currently selected, this means the
9884 echo-area doesn't show through. */
9885 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9886 {
9887 int window_height_changed_p = echo_area_display (0);
9888 must_finish = 1;
9889
9890 /* If we don't display the current message, don't clear the
9891 message_cleared_p flag, because, if we did, we wouldn't clear
9892 the echo area in the next redisplay which doesn't preserve
9893 the echo area. */
9894 if (!display_last_displayed_message_p)
9895 message_cleared_p = 0;
9896
9897 if (fonts_changed_p)
9898 goto retry;
9899 else if (window_height_changed_p)
9900 {
9901 consider_all_windows_p = 1;
9902 ++update_mode_lines;
9903 ++windows_or_buffers_changed;
9904
9905 /* If window configuration was changed, frames may have been
9906 marked garbaged. Clear them or we will experience
9907 surprises wrt scrolling. */
9908 if (frame_garbaged)
9909 clear_garbaged_frames ();
9910 }
9911 }
9912 else if (EQ (selected_window, minibuf_window)
9913 && (current_buffer->clip_changed
9914 || XFASTINT (w->last_modified) < MODIFF
9915 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9916 && resize_mini_window (w, 0))
9917 {
9918 /* Resized active mini-window to fit the size of what it is
9919 showing if its contents might have changed. */
9920 must_finish = 1;
9921 consider_all_windows_p = 1;
9922 ++windows_or_buffers_changed;
9923 ++update_mode_lines;
9924
9925 /* If window configuration was changed, frames may have been
9926 marked garbaged. Clear them or we will experience
9927 surprises wrt scrolling. */
9928 if (frame_garbaged)
9929 clear_garbaged_frames ();
9930 }
9931
9932
9933 /* If showing the region, and mark has changed, we must redisplay
9934 the whole window. The assignment to this_line_start_pos prevents
9935 the optimization directly below this if-statement. */
9936 if (((!NILP (Vtransient_mark_mode)
9937 && !NILP (XBUFFER (w->buffer)->mark_active))
9938 != !NILP (w->region_showing))
9939 || (!NILP (w->region_showing)
9940 && !EQ (w->region_showing,
9941 Fmarker_position (XBUFFER (w->buffer)->mark))))
9942 CHARPOS (this_line_start_pos) = 0;
9943
9944 /* Optimize the case that only the line containing the cursor in the
9945 selected window has changed. Variables starting with this_ are
9946 set in display_line and record information about the line
9947 containing the cursor. */
9948 tlbufpos = this_line_start_pos;
9949 tlendpos = this_line_end_pos;
9950 if (!consider_all_windows_p
9951 && CHARPOS (tlbufpos) > 0
9952 && NILP (w->update_mode_line)
9953 && !current_buffer->clip_changed
9954 && !current_buffer->prevent_redisplay_optimizations_p
9955 && FRAME_VISIBLE_P (XFRAME (w->frame))
9956 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9957 /* Make sure recorded data applies to current buffer, etc. */
9958 && this_line_buffer == current_buffer
9959 && current_buffer == XBUFFER (w->buffer)
9960 && NILP (w->force_start)
9961 && NILP (w->optional_new_start)
9962 /* Point must be on the line that we have info recorded about. */
9963 && PT >= CHARPOS (tlbufpos)
9964 && PT <= Z - CHARPOS (tlendpos)
9965 /* All text outside that line, including its final newline,
9966 must be unchanged */
9967 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9968 CHARPOS (tlendpos)))
9969 {
9970 if (CHARPOS (tlbufpos) > BEGV
9971 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9972 && (CHARPOS (tlbufpos) == ZV
9973 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9974 /* Former continuation line has disappeared by becoming empty */
9975 goto cancel;
9976 else if (XFASTINT (w->last_modified) < MODIFF
9977 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9978 || MINI_WINDOW_P (w))
9979 {
9980 /* We have to handle the case of continuation around a
9981 wide-column character (See the comment in indent.c around
9982 line 885).
9983
9984 For instance, in the following case:
9985
9986 -------- Insert --------
9987 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9988 J_I_ ==> J_I_ `^^' are cursors.
9989 ^^ ^^
9990 -------- --------
9991
9992 As we have to redraw the line above, we should goto cancel. */
9993
9994 struct it it;
9995 int line_height_before = this_line_pixel_height;
9996
9997 /* Note that start_display will handle the case that the
9998 line starting at tlbufpos is a continuation lines. */
9999 start_display (&it, w, tlbufpos);
10000
10001 /* Implementation note: It this still necessary? */
10002 if (it.current_x != this_line_start_x)
10003 goto cancel;
10004
10005 TRACE ((stderr, "trying display optimization 1\n"));
10006 w->cursor.vpos = -1;
10007 overlay_arrow_seen = 0;
10008 it.vpos = this_line_vpos;
10009 it.current_y = this_line_y;
10010 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10011 display_line (&it);
10012
10013 /* If line contains point, is not continued,
10014 and ends at same distance from eob as before, we win */
10015 if (w->cursor.vpos >= 0
10016 /* Line is not continued, otherwise this_line_start_pos
10017 would have been set to 0 in display_line. */
10018 && CHARPOS (this_line_start_pos)
10019 /* Line ends as before. */
10020 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10021 /* Line has same height as before. Otherwise other lines
10022 would have to be shifted up or down. */
10023 && this_line_pixel_height == line_height_before)
10024 {
10025 /* If this is not the window's last line, we must adjust
10026 the charstarts of the lines below. */
10027 if (it.current_y < it.last_visible_y)
10028 {
10029 struct glyph_row *row
10030 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10031 int delta, delta_bytes;
10032
10033 if (Z - CHARPOS (tlendpos) == ZV)
10034 {
10035 /* This line ends at end of (accessible part of)
10036 buffer. There is no newline to count. */
10037 delta = (Z
10038 - CHARPOS (tlendpos)
10039 - MATRIX_ROW_START_CHARPOS (row));
10040 delta_bytes = (Z_BYTE
10041 - BYTEPOS (tlendpos)
10042 - MATRIX_ROW_START_BYTEPOS (row));
10043 }
10044 else
10045 {
10046 /* This line ends in a newline. Must take
10047 account of the newline and the rest of the
10048 text that follows. */
10049 delta = (Z
10050 - CHARPOS (tlendpos)
10051 - MATRIX_ROW_START_CHARPOS (row));
10052 delta_bytes = (Z_BYTE
10053 - BYTEPOS (tlendpos)
10054 - MATRIX_ROW_START_BYTEPOS (row));
10055 }
10056
10057 increment_matrix_positions (w->current_matrix,
10058 this_line_vpos + 1,
10059 w->current_matrix->nrows,
10060 delta, delta_bytes);
10061 }
10062
10063 /* If this row displays text now but previously didn't,
10064 or vice versa, w->window_end_vpos may have to be
10065 adjusted. */
10066 if ((it.glyph_row - 1)->displays_text_p)
10067 {
10068 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10069 XSETINT (w->window_end_vpos, this_line_vpos);
10070 }
10071 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10072 && this_line_vpos > 0)
10073 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10074 w->window_end_valid = Qnil;
10075
10076 /* Update hint: No need to try to scroll in update_window. */
10077 w->desired_matrix->no_scrolling_p = 1;
10078
10079 #if GLYPH_DEBUG
10080 *w->desired_matrix->method = 0;
10081 debug_method_add (w, "optimization 1");
10082 #endif
10083 #ifdef HAVE_WINDOW_SYSTEM
10084 update_window_fringes (w, 0);
10085 #endif
10086 goto update;
10087 }
10088 else
10089 goto cancel;
10090 }
10091 else if (/* Cursor position hasn't changed. */
10092 PT == XFASTINT (w->last_point)
10093 /* Make sure the cursor was last displayed
10094 in this window. Otherwise we have to reposition it. */
10095 && 0 <= w->cursor.vpos
10096 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10097 {
10098 if (!must_finish)
10099 {
10100 do_pending_window_change (1);
10101
10102 /* We used to always goto end_of_redisplay here, but this
10103 isn't enough if we have a blinking cursor. */
10104 if (w->cursor_off_p == w->last_cursor_off_p)
10105 goto end_of_redisplay;
10106 }
10107 goto update;
10108 }
10109 /* If highlighting the region, or if the cursor is in the echo area,
10110 then we can't just move the cursor. */
10111 else if (! (!NILP (Vtransient_mark_mode)
10112 && !NILP (current_buffer->mark_active))
10113 && (EQ (selected_window, current_buffer->last_selected_window)
10114 || highlight_nonselected_windows)
10115 && NILP (w->region_showing)
10116 && NILP (Vshow_trailing_whitespace)
10117 && !cursor_in_echo_area)
10118 {
10119 struct it it;
10120 struct glyph_row *row;
10121
10122 /* Skip from tlbufpos to PT and see where it is. Note that
10123 PT may be in invisible text. If so, we will end at the
10124 next visible position. */
10125 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10126 NULL, DEFAULT_FACE_ID);
10127 it.current_x = this_line_start_x;
10128 it.current_y = this_line_y;
10129 it.vpos = this_line_vpos;
10130
10131 /* The call to move_it_to stops in front of PT, but
10132 moves over before-strings. */
10133 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10134
10135 if (it.vpos == this_line_vpos
10136 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10137 row->enabled_p))
10138 {
10139 xassert (this_line_vpos == it.vpos);
10140 xassert (this_line_y == it.current_y);
10141 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10142 #if GLYPH_DEBUG
10143 *w->desired_matrix->method = 0;
10144 debug_method_add (w, "optimization 3");
10145 #endif
10146 goto update;
10147 }
10148 else
10149 goto cancel;
10150 }
10151
10152 cancel:
10153 /* Text changed drastically or point moved off of line. */
10154 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10155 }
10156
10157 CHARPOS (this_line_start_pos) = 0;
10158 consider_all_windows_p |= buffer_shared > 1;
10159 ++clear_face_cache_count;
10160
10161
10162 /* Build desired matrices, and update the display. If
10163 consider_all_windows_p is non-zero, do it for all windows on all
10164 frames. Otherwise do it for selected_window, only. */
10165
10166 if (consider_all_windows_p)
10167 {
10168 Lisp_Object tail, frame;
10169 int i, n = 0, size = 50;
10170 struct frame **updated
10171 = (struct frame **) alloca (size * sizeof *updated);
10172
10173 /* Clear the face cache eventually. */
10174 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10175 {
10176 clear_face_cache (0);
10177 clear_face_cache_count = 0;
10178 }
10179
10180 /* Recompute # windows showing selected buffer. This will be
10181 incremented each time such a window is displayed. */
10182 buffer_shared = 0;
10183
10184 FOR_EACH_FRAME (tail, frame)
10185 {
10186 struct frame *f = XFRAME (frame);
10187
10188 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10189 {
10190 if (! EQ (frame, selected_frame))
10191 /* Select the frame, for the sake of frame-local
10192 variables. */
10193 select_frame_for_redisplay (frame);
10194
10195 #ifdef HAVE_WINDOW_SYSTEM
10196 if (clear_face_cache_count % 50 == 0
10197 && FRAME_WINDOW_P (f))
10198 clear_image_cache (f, 0);
10199 #endif /* HAVE_WINDOW_SYSTEM */
10200
10201 /* Mark all the scroll bars to be removed; we'll redeem
10202 the ones we want when we redisplay their windows. */
10203 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10204 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10205
10206 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10207 redisplay_windows (FRAME_ROOT_WINDOW (f));
10208
10209 /* Any scroll bars which redisplay_windows should have
10210 nuked should now go away. */
10211 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10212 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10213
10214 /* If fonts changed, display again. */
10215 /* ??? rms: I suspect it is a mistake to jump all the way
10216 back to retry here. It should just retry this frame. */
10217 if (fonts_changed_p)
10218 goto retry;
10219
10220 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10221 {
10222 /* See if we have to hscroll. */
10223 if (hscroll_windows (f->root_window))
10224 goto retry;
10225
10226 /* Prevent various kinds of signals during display
10227 update. stdio is not robust about handling
10228 signals, which can cause an apparent I/O
10229 error. */
10230 if (interrupt_input)
10231 unrequest_sigio ();
10232 STOP_POLLING;
10233
10234 /* Update the display. */
10235 set_window_update_flags (XWINDOW (f->root_window), 1);
10236 pause |= update_frame (f, 0, 0);
10237 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10238 if (pause)
10239 break;
10240 #endif
10241
10242 if (n == size)
10243 {
10244 int nbytes = size * sizeof *updated;
10245 struct frame **p = (struct frame **) alloca (2 * nbytes);
10246 bcopy (updated, p, nbytes);
10247 size *= 2;
10248 }
10249
10250 updated[n++] = f;
10251 }
10252 }
10253 }
10254
10255 if (!pause)
10256 {
10257 /* Do the mark_window_display_accurate after all windows have
10258 been redisplayed because this call resets flags in buffers
10259 which are needed for proper redisplay. */
10260 for (i = 0; i < n; ++i)
10261 {
10262 struct frame *f = updated[i];
10263 mark_window_display_accurate (f->root_window, 1);
10264 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10265 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10266 }
10267 }
10268 }
10269 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10270 {
10271 Lisp_Object mini_window;
10272 struct frame *mini_frame;
10273
10274 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10275 /* Use list_of_error, not Qerror, so that
10276 we catch only errors and don't run the debugger. */
10277 internal_condition_case_1 (redisplay_window_1, selected_window,
10278 list_of_error,
10279 redisplay_window_error);
10280
10281 /* Compare desired and current matrices, perform output. */
10282
10283 update:
10284 /* If fonts changed, display again. */
10285 if (fonts_changed_p)
10286 goto retry;
10287
10288 /* Prevent various kinds of signals during display update.
10289 stdio is not robust about handling signals,
10290 which can cause an apparent I/O error. */
10291 if (interrupt_input)
10292 unrequest_sigio ();
10293 STOP_POLLING;
10294
10295 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10296 {
10297 if (hscroll_windows (selected_window))
10298 goto retry;
10299
10300 XWINDOW (selected_window)->must_be_updated_p = 1;
10301 pause = update_frame (sf, 0, 0);
10302 }
10303
10304 /* We may have called echo_area_display at the top of this
10305 function. If the echo area is on another frame, that may
10306 have put text on a frame other than the selected one, so the
10307 above call to update_frame would not have caught it. Catch
10308 it here. */
10309 mini_window = FRAME_MINIBUF_WINDOW (sf);
10310 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10311
10312 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10313 {
10314 XWINDOW (mini_window)->must_be_updated_p = 1;
10315 pause |= update_frame (mini_frame, 0, 0);
10316 if (!pause && hscroll_windows (mini_window))
10317 goto retry;
10318 }
10319 }
10320
10321 /* If display was paused because of pending input, make sure we do a
10322 thorough update the next time. */
10323 if (pause)
10324 {
10325 /* Prevent the optimization at the beginning of
10326 redisplay_internal that tries a single-line update of the
10327 line containing the cursor in the selected window. */
10328 CHARPOS (this_line_start_pos) = 0;
10329
10330 /* Let the overlay arrow be updated the next time. */
10331 update_overlay_arrows (0);
10332
10333 /* If we pause after scrolling, some rows in the current
10334 matrices of some windows are not valid. */
10335 if (!WINDOW_FULL_WIDTH_P (w)
10336 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10337 update_mode_lines = 1;
10338 }
10339 else
10340 {
10341 if (!consider_all_windows_p)
10342 {
10343 /* This has already been done above if
10344 consider_all_windows_p is set. */
10345 mark_window_display_accurate_1 (w, 1);
10346
10347 /* Say overlay arrows are up to date. */
10348 update_overlay_arrows (1);
10349
10350 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10351 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10352 }
10353
10354 update_mode_lines = 0;
10355 windows_or_buffers_changed = 0;
10356 cursor_type_changed = 0;
10357 }
10358
10359 /* Start SIGIO interrupts coming again. Having them off during the
10360 code above makes it less likely one will discard output, but not
10361 impossible, since there might be stuff in the system buffer here.
10362 But it is much hairier to try to do anything about that. */
10363 if (interrupt_input)
10364 request_sigio ();
10365 RESUME_POLLING;
10366
10367 /* If a frame has become visible which was not before, redisplay
10368 again, so that we display it. Expose events for such a frame
10369 (which it gets when becoming visible) don't call the parts of
10370 redisplay constructing glyphs, so simply exposing a frame won't
10371 display anything in this case. So, we have to display these
10372 frames here explicitly. */
10373 if (!pause)
10374 {
10375 Lisp_Object tail, frame;
10376 int new_count = 0;
10377
10378 FOR_EACH_FRAME (tail, frame)
10379 {
10380 int this_is_visible = 0;
10381
10382 if (XFRAME (frame)->visible)
10383 this_is_visible = 1;
10384 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10385 if (XFRAME (frame)->visible)
10386 this_is_visible = 1;
10387
10388 if (this_is_visible)
10389 new_count++;
10390 }
10391
10392 if (new_count != number_of_visible_frames)
10393 windows_or_buffers_changed++;
10394 }
10395
10396 /* Change frame size now if a change is pending. */
10397 do_pending_window_change (1);
10398
10399 /* If we just did a pending size change, or have additional
10400 visible frames, redisplay again. */
10401 if (windows_or_buffers_changed && !pause)
10402 goto retry;
10403
10404 end_of_redisplay:
10405 unbind_to (count, Qnil);
10406 RESUME_POLLING;
10407 }
10408
10409
10410 /* Redisplay, but leave alone any recent echo area message unless
10411 another message has been requested in its place.
10412
10413 This is useful in situations where you need to redisplay but no
10414 user action has occurred, making it inappropriate for the message
10415 area to be cleared. See tracking_off and
10416 wait_reading_process_output for examples of these situations.
10417
10418 FROM_WHERE is an integer saying from where this function was
10419 called. This is useful for debugging. */
10420
10421 void
10422 redisplay_preserve_echo_area (from_where)
10423 int from_where;
10424 {
10425 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10426
10427 if (!NILP (echo_area_buffer[1]))
10428 {
10429 /* We have a previously displayed message, but no current
10430 message. Redisplay the previous message. */
10431 display_last_displayed_message_p = 1;
10432 redisplay_internal (1);
10433 display_last_displayed_message_p = 0;
10434 }
10435 else
10436 redisplay_internal (1);
10437
10438 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
10439 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10440 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
10441 }
10442
10443
10444 /* Function registered with record_unwind_protect in
10445 redisplay_internal. Reset redisplaying_p to the value it had
10446 before redisplay_internal was called, and clear
10447 prevent_freeing_realized_faces_p. It also selects the previously
10448 selected frame. */
10449
10450 static Lisp_Object
10451 unwind_redisplay (val)
10452 Lisp_Object val;
10453 {
10454 Lisp_Object old_redisplaying_p, old_frame;
10455
10456 old_redisplaying_p = XCAR (val);
10457 redisplaying_p = XFASTINT (old_redisplaying_p);
10458 old_frame = XCDR (val);
10459 if (! EQ (old_frame, selected_frame))
10460 select_frame_for_redisplay (old_frame);
10461 return Qnil;
10462 }
10463
10464
10465 /* Mark the display of window W as accurate or inaccurate. If
10466 ACCURATE_P is non-zero mark display of W as accurate. If
10467 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10468 redisplay_internal is called. */
10469
10470 static void
10471 mark_window_display_accurate_1 (w, accurate_p)
10472 struct window *w;
10473 int accurate_p;
10474 {
10475 if (BUFFERP (w->buffer))
10476 {
10477 struct buffer *b = XBUFFER (w->buffer);
10478
10479 w->last_modified
10480 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10481 w->last_overlay_modified
10482 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10483 w->last_had_star
10484 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10485
10486 if (accurate_p)
10487 {
10488 b->clip_changed = 0;
10489 b->prevent_redisplay_optimizations_p = 0;
10490
10491 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10492 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10493 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10494 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10495
10496 w->current_matrix->buffer = b;
10497 w->current_matrix->begv = BUF_BEGV (b);
10498 w->current_matrix->zv = BUF_ZV (b);
10499
10500 w->last_cursor = w->cursor;
10501 w->last_cursor_off_p = w->cursor_off_p;
10502
10503 if (w == XWINDOW (selected_window))
10504 w->last_point = make_number (BUF_PT (b));
10505 else
10506 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10507 }
10508 }
10509
10510 if (accurate_p)
10511 {
10512 w->window_end_valid = w->buffer;
10513 #if 0 /* This is incorrect with variable-height lines. */
10514 xassert (XINT (w->window_end_vpos)
10515 < (WINDOW_TOTAL_LINES (w)
10516 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10517 #endif
10518 w->update_mode_line = Qnil;
10519 }
10520 }
10521
10522
10523 /* Mark the display of windows in the window tree rooted at WINDOW as
10524 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10525 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10526 be redisplayed the next time redisplay_internal is called. */
10527
10528 void
10529 mark_window_display_accurate (window, accurate_p)
10530 Lisp_Object window;
10531 int accurate_p;
10532 {
10533 struct window *w;
10534
10535 for (; !NILP (window); window = w->next)
10536 {
10537 w = XWINDOW (window);
10538 mark_window_display_accurate_1 (w, accurate_p);
10539
10540 if (!NILP (w->vchild))
10541 mark_window_display_accurate (w->vchild, accurate_p);
10542 if (!NILP (w->hchild))
10543 mark_window_display_accurate (w->hchild, accurate_p);
10544 }
10545
10546 if (accurate_p)
10547 {
10548 update_overlay_arrows (1);
10549 }
10550 else
10551 {
10552 /* Force a thorough redisplay the next time by setting
10553 last_arrow_position and last_arrow_string to t, which is
10554 unequal to any useful value of Voverlay_arrow_... */
10555 update_overlay_arrows (-1);
10556 }
10557 }
10558
10559
10560 /* Return value in display table DP (Lisp_Char_Table *) for character
10561 C. Since a display table doesn't have any parent, we don't have to
10562 follow parent. Do not call this function directly but use the
10563 macro DISP_CHAR_VECTOR. */
10564
10565 Lisp_Object
10566 disp_char_vector (dp, c)
10567 struct Lisp_Char_Table *dp;
10568 int c;
10569 {
10570 int code[4], i;
10571 Lisp_Object val;
10572
10573 if (SINGLE_BYTE_CHAR_P (c))
10574 return (dp->contents[c]);
10575
10576 SPLIT_CHAR (c, code[0], code[1], code[2]);
10577 if (code[1] < 32)
10578 code[1] = -1;
10579 else if (code[2] < 32)
10580 code[2] = -1;
10581
10582 /* Here, the possible range of code[0] (== charset ID) is
10583 128..max_charset. Since the top level char table contains data
10584 for multibyte characters after 256th element, we must increment
10585 code[0] by 128 to get a correct index. */
10586 code[0] += 128;
10587 code[3] = -1; /* anchor */
10588
10589 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10590 {
10591 val = dp->contents[code[i]];
10592 if (!SUB_CHAR_TABLE_P (val))
10593 return (NILP (val) ? dp->defalt : val);
10594 }
10595
10596 /* Here, val is a sub char table. We return the default value of
10597 it. */
10598 return (dp->defalt);
10599 }
10600
10601
10602 \f
10603 /***********************************************************************
10604 Window Redisplay
10605 ***********************************************************************/
10606
10607 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10608
10609 static void
10610 redisplay_windows (window)
10611 Lisp_Object window;
10612 {
10613 while (!NILP (window))
10614 {
10615 struct window *w = XWINDOW (window);
10616
10617 if (!NILP (w->hchild))
10618 redisplay_windows (w->hchild);
10619 else if (!NILP (w->vchild))
10620 redisplay_windows (w->vchild);
10621 else
10622 {
10623 displayed_buffer = XBUFFER (w->buffer);
10624 /* Use list_of_error, not Qerror, so that
10625 we catch only errors and don't run the debugger. */
10626 internal_condition_case_1 (redisplay_window_0, window,
10627 list_of_error,
10628 redisplay_window_error);
10629 }
10630
10631 window = w->next;
10632 }
10633 }
10634
10635 static Lisp_Object
10636 redisplay_window_error ()
10637 {
10638 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10639 return Qnil;
10640 }
10641
10642 static Lisp_Object
10643 redisplay_window_0 (window)
10644 Lisp_Object window;
10645 {
10646 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10647 redisplay_window (window, 0);
10648 return Qnil;
10649 }
10650
10651 static Lisp_Object
10652 redisplay_window_1 (window)
10653 Lisp_Object window;
10654 {
10655 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10656 redisplay_window (window, 1);
10657 return Qnil;
10658 }
10659 \f
10660
10661 /* Increment GLYPH until it reaches END or CONDITION fails while
10662 adding (GLYPH)->pixel_width to X. */
10663
10664 #define SKIP_GLYPHS(glyph, end, x, condition) \
10665 do \
10666 { \
10667 (x) += (glyph)->pixel_width; \
10668 ++(glyph); \
10669 } \
10670 while ((glyph) < (end) && (condition))
10671
10672
10673 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10674 DELTA is the number of bytes by which positions recorded in ROW
10675 differ from current buffer positions. */
10676
10677 void
10678 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10679 struct window *w;
10680 struct glyph_row *row;
10681 struct glyph_matrix *matrix;
10682 int delta, delta_bytes, dy, dvpos;
10683 {
10684 struct glyph *glyph = row->glyphs[TEXT_AREA];
10685 struct glyph *end = glyph + row->used[TEXT_AREA];
10686 struct glyph *cursor = NULL;
10687 /* The first glyph that starts a sequence of glyphs from string. */
10688 struct glyph *string_start;
10689 /* The X coordinate of string_start. */
10690 int string_start_x;
10691 /* The last known character position. */
10692 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10693 /* The last known character position before string_start. */
10694 int string_before_pos;
10695 int x = row->x;
10696 int cursor_x = x;
10697 int cursor_from_overlay_pos = 0;
10698 int pt_old = PT - delta;
10699
10700 /* Skip over glyphs not having an object at the start of the row.
10701 These are special glyphs like truncation marks on terminal
10702 frames. */
10703 if (row->displays_text_p)
10704 while (glyph < end
10705 && INTEGERP (glyph->object)
10706 && glyph->charpos < 0)
10707 {
10708 x += glyph->pixel_width;
10709 ++glyph;
10710 }
10711
10712 string_start = NULL;
10713 while (glyph < end
10714 && !INTEGERP (glyph->object)
10715 && (!BUFFERP (glyph->object)
10716 || (last_pos = glyph->charpos) < pt_old))
10717 {
10718 if (! STRINGP (glyph->object))
10719 {
10720 string_start = NULL;
10721 x += glyph->pixel_width;
10722 ++glyph;
10723 if (cursor_from_overlay_pos
10724 && last_pos > cursor_from_overlay_pos)
10725 {
10726 cursor_from_overlay_pos = 0;
10727 cursor = 0;
10728 }
10729 }
10730 else
10731 {
10732 string_before_pos = last_pos;
10733 string_start = glyph;
10734 string_start_x = x;
10735 /* Skip all glyphs from string. */
10736 do
10737 {
10738 int pos;
10739 if ((cursor == NULL || glyph > cursor)
10740 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10741 Qcursor, (glyph)->object))
10742 && (pos = string_buffer_position (w, glyph->object,
10743 string_before_pos),
10744 (pos == 0 /* From overlay */
10745 || pos == pt_old)))
10746 {
10747 /* Estimate overlay buffer position from the buffer
10748 positions of the glyphs before and after the overlay.
10749 Add 1 to last_pos so that if point corresponds to the
10750 glyph right after the overlay, we still use a 'cursor'
10751 property found in that overlay. */
10752 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10753 cursor = glyph;
10754 cursor_x = x;
10755 }
10756 x += glyph->pixel_width;
10757 ++glyph;
10758 }
10759 while (glyph < end && STRINGP (glyph->object));
10760 }
10761 }
10762
10763 if (cursor != NULL)
10764 {
10765 glyph = cursor;
10766 x = cursor_x;
10767 }
10768 else if (string_start
10769 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10770 {
10771 /* We may have skipped over point because the previous glyphs
10772 are from string. As there's no easy way to know the
10773 character position of the current glyph, find the correct
10774 glyph on point by scanning from string_start again. */
10775 Lisp_Object limit;
10776 Lisp_Object string;
10777 int pos;
10778
10779 limit = make_number (pt_old + 1);
10780 end = glyph;
10781 glyph = string_start;
10782 x = string_start_x;
10783 string = glyph->object;
10784 pos = string_buffer_position (w, string, string_before_pos);
10785 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10786 because we always put cursor after overlay strings. */
10787 while (pos == 0 && glyph < end)
10788 {
10789 string = glyph->object;
10790 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10791 if (glyph < end)
10792 pos = string_buffer_position (w, glyph->object, string_before_pos);
10793 }
10794
10795 while (glyph < end)
10796 {
10797 pos = XINT (Fnext_single_char_property_change
10798 (make_number (pos), Qdisplay, Qnil, limit));
10799 if (pos > pt_old)
10800 break;
10801 /* Skip glyphs from the same string. */
10802 string = glyph->object;
10803 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10804 /* Skip glyphs from an overlay. */
10805 while (glyph < end
10806 && ! string_buffer_position (w, glyph->object, pos))
10807 {
10808 string = glyph->object;
10809 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10810 }
10811 }
10812 }
10813
10814 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10815 w->cursor.x = x;
10816 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10817 w->cursor.y = row->y + dy;
10818
10819 if (w == XWINDOW (selected_window))
10820 {
10821 if (!row->continued_p
10822 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10823 && row->x == 0)
10824 {
10825 this_line_buffer = XBUFFER (w->buffer);
10826
10827 CHARPOS (this_line_start_pos)
10828 = MATRIX_ROW_START_CHARPOS (row) + delta;
10829 BYTEPOS (this_line_start_pos)
10830 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10831
10832 CHARPOS (this_line_end_pos)
10833 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10834 BYTEPOS (this_line_end_pos)
10835 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10836
10837 this_line_y = w->cursor.y;
10838 this_line_pixel_height = row->height;
10839 this_line_vpos = w->cursor.vpos;
10840 this_line_start_x = row->x;
10841 }
10842 else
10843 CHARPOS (this_line_start_pos) = 0;
10844 }
10845 }
10846
10847
10848 /* Run window scroll functions, if any, for WINDOW with new window
10849 start STARTP. Sets the window start of WINDOW to that position.
10850
10851 We assume that the window's buffer is really current. */
10852
10853 static INLINE struct text_pos
10854 run_window_scroll_functions (window, startp)
10855 Lisp_Object window;
10856 struct text_pos startp;
10857 {
10858 struct window *w = XWINDOW (window);
10859 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10860
10861 if (current_buffer != XBUFFER (w->buffer))
10862 abort ();
10863
10864 if (!NILP (Vwindow_scroll_functions))
10865 {
10866 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10867 make_number (CHARPOS (startp)));
10868 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10869 /* In case the hook functions switch buffers. */
10870 if (current_buffer != XBUFFER (w->buffer))
10871 set_buffer_internal_1 (XBUFFER (w->buffer));
10872 }
10873
10874 return startp;
10875 }
10876
10877
10878 /* Make sure the line containing the cursor is fully visible.
10879 A value of 1 means there is nothing to be done.
10880 (Either the line is fully visible, or it cannot be made so,
10881 or we cannot tell.)
10882
10883 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10884 is higher than window.
10885
10886 A value of 0 means the caller should do scrolling
10887 as if point had gone off the screen. */
10888
10889 static int
10890 make_cursor_line_fully_visible (w, force_p)
10891 struct window *w;
10892 int force_p;
10893 {
10894 struct glyph_matrix *matrix;
10895 struct glyph_row *row;
10896 int window_height;
10897
10898 if (!make_cursor_line_fully_visible_p)
10899 return 1;
10900
10901 /* It's not always possible to find the cursor, e.g, when a window
10902 is full of overlay strings. Don't do anything in that case. */
10903 if (w->cursor.vpos < 0)
10904 return 1;
10905
10906 matrix = w->desired_matrix;
10907 row = MATRIX_ROW (matrix, w->cursor.vpos);
10908
10909 /* If the cursor row is not partially visible, there's nothing to do. */
10910 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10911 return 1;
10912
10913 /* If the row the cursor is in is taller than the window's height,
10914 it's not clear what to do, so do nothing. */
10915 window_height = window_box_height (w);
10916 if (row->height >= window_height)
10917 {
10918 if (!force_p || w->vscroll)
10919 return 1;
10920 }
10921 return 0;
10922
10923 #if 0
10924 /* This code used to try to scroll the window just enough to make
10925 the line visible. It returned 0 to say that the caller should
10926 allocate larger glyph matrices. */
10927
10928 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10929 {
10930 int dy = row->height - row->visible_height;
10931 w->vscroll = 0;
10932 w->cursor.y += dy;
10933 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10934 }
10935 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10936 {
10937 int dy = - (row->height - row->visible_height);
10938 w->vscroll = dy;
10939 w->cursor.y += dy;
10940 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10941 }
10942
10943 /* When we change the cursor y-position of the selected window,
10944 change this_line_y as well so that the display optimization for
10945 the cursor line of the selected window in redisplay_internal uses
10946 the correct y-position. */
10947 if (w == XWINDOW (selected_window))
10948 this_line_y = w->cursor.y;
10949
10950 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10951 redisplay with larger matrices. */
10952 if (matrix->nrows < required_matrix_height (w))
10953 {
10954 fonts_changed_p = 1;
10955 return 0;
10956 }
10957
10958 return 1;
10959 #endif /* 0 */
10960 }
10961
10962
10963 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10964 non-zero means only WINDOW is redisplayed in redisplay_internal.
10965 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10966 in redisplay_window to bring a partially visible line into view in
10967 the case that only the cursor has moved.
10968
10969 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10970 last screen line's vertical height extends past the end of the screen.
10971
10972 Value is
10973
10974 1 if scrolling succeeded
10975
10976 0 if scrolling didn't find point.
10977
10978 -1 if new fonts have been loaded so that we must interrupt
10979 redisplay, adjust glyph matrices, and try again. */
10980
10981 enum
10982 {
10983 SCROLLING_SUCCESS,
10984 SCROLLING_FAILED,
10985 SCROLLING_NEED_LARGER_MATRICES
10986 };
10987
10988 static int
10989 try_scrolling (window, just_this_one_p, scroll_conservatively,
10990 scroll_step, temp_scroll_step, last_line_misfit)
10991 Lisp_Object window;
10992 int just_this_one_p;
10993 EMACS_INT scroll_conservatively, scroll_step;
10994 int temp_scroll_step;
10995 int last_line_misfit;
10996 {
10997 struct window *w = XWINDOW (window);
10998 struct frame *f = XFRAME (w->frame);
10999 struct text_pos scroll_margin_pos;
11000 struct text_pos pos;
11001 struct text_pos startp;
11002 struct it it;
11003 Lisp_Object window_end;
11004 int this_scroll_margin;
11005 int dy = 0;
11006 int scroll_max;
11007 int rc;
11008 int amount_to_scroll = 0;
11009 Lisp_Object aggressive;
11010 int height;
11011 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11012
11013 #if GLYPH_DEBUG
11014 debug_method_add (w, "try_scrolling");
11015 #endif
11016
11017 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11018
11019 /* Compute scroll margin height in pixels. We scroll when point is
11020 within this distance from the top or bottom of the window. */
11021 if (scroll_margin > 0)
11022 {
11023 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11024 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11025 }
11026 else
11027 this_scroll_margin = 0;
11028
11029 /* Force scroll_conservatively to have a reasonable value so it doesn't
11030 cause an overflow while computing how much to scroll. */
11031 if (scroll_conservatively)
11032 scroll_conservatively = min (scroll_conservatively,
11033 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11034
11035 /* Compute how much we should try to scroll maximally to bring point
11036 into view. */
11037 if (scroll_step || scroll_conservatively || temp_scroll_step)
11038 scroll_max = max (scroll_step,
11039 max (scroll_conservatively, temp_scroll_step));
11040 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11041 || NUMBERP (current_buffer->scroll_up_aggressively))
11042 /* We're trying to scroll because of aggressive scrolling
11043 but no scroll_step is set. Choose an arbitrary one. Maybe
11044 there should be a variable for this. */
11045 scroll_max = 10;
11046 else
11047 scroll_max = 0;
11048 scroll_max *= FRAME_LINE_HEIGHT (f);
11049
11050 /* Decide whether we have to scroll down. Start at the window end
11051 and move this_scroll_margin up to find the position of the scroll
11052 margin. */
11053 window_end = Fwindow_end (window, Qt);
11054
11055 too_near_end:
11056
11057 CHARPOS (scroll_margin_pos) = XINT (window_end);
11058 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11059
11060 if (this_scroll_margin || extra_scroll_margin_lines)
11061 {
11062 start_display (&it, w, scroll_margin_pos);
11063 if (this_scroll_margin)
11064 move_it_vertically_backward (&it, this_scroll_margin);
11065 if (extra_scroll_margin_lines)
11066 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11067 scroll_margin_pos = it.current.pos;
11068 }
11069
11070 if (PT >= CHARPOS (scroll_margin_pos))
11071 {
11072 int y0;
11073
11074 /* Point is in the scroll margin at the bottom of the window, or
11075 below. Compute a new window start that makes point visible. */
11076
11077 /* Compute the distance from the scroll margin to PT.
11078 Give up if the distance is greater than scroll_max. */
11079 start_display (&it, w, scroll_margin_pos);
11080 y0 = it.current_y;
11081 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11082 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11083
11084 /* To make point visible, we have to move the window start
11085 down so that the line the cursor is in is visible, which
11086 means we have to add in the height of the cursor line. */
11087 dy = line_bottom_y (&it) - y0;
11088
11089 if (dy > scroll_max)
11090 return SCROLLING_FAILED;
11091
11092 /* Move the window start down. If scrolling conservatively,
11093 move it just enough down to make point visible. If
11094 scroll_step is set, move it down by scroll_step. */
11095 start_display (&it, w, startp);
11096
11097 if (scroll_conservatively)
11098 /* Set AMOUNT_TO_SCROLL to at least one line,
11099 and at most scroll_conservatively lines. */
11100 amount_to_scroll
11101 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11102 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11103 else if (scroll_step || temp_scroll_step)
11104 amount_to_scroll = scroll_max;
11105 else
11106 {
11107 aggressive = current_buffer->scroll_up_aggressively;
11108 height = WINDOW_BOX_TEXT_HEIGHT (w);
11109 if (NUMBERP (aggressive))
11110 {
11111 double float_amount = XFLOATINT (aggressive) * height;
11112 amount_to_scroll = float_amount;
11113 if (amount_to_scroll == 0 && float_amount > 0)
11114 amount_to_scroll = 1;
11115 }
11116 }
11117
11118 if (amount_to_scroll <= 0)
11119 return SCROLLING_FAILED;
11120
11121 /* If moving by amount_to_scroll leaves STARTP unchanged,
11122 move it down one screen line. */
11123
11124 move_it_vertically (&it, amount_to_scroll);
11125 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11126 move_it_by_lines (&it, 1, 1);
11127 startp = it.current.pos;
11128 }
11129 else
11130 {
11131 /* See if point is inside the scroll margin at the top of the
11132 window. */
11133 scroll_margin_pos = startp;
11134 if (this_scroll_margin)
11135 {
11136 start_display (&it, w, startp);
11137 move_it_vertically (&it, this_scroll_margin);
11138 scroll_margin_pos = it.current.pos;
11139 }
11140
11141 if (PT < CHARPOS (scroll_margin_pos))
11142 {
11143 /* Point is in the scroll margin at the top of the window or
11144 above what is displayed in the window. */
11145 int y0;
11146
11147 /* Compute the vertical distance from PT to the scroll
11148 margin position. Give up if distance is greater than
11149 scroll_max. */
11150 SET_TEXT_POS (pos, PT, PT_BYTE);
11151 start_display (&it, w, pos);
11152 y0 = it.current_y;
11153 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11154 it.last_visible_y, -1,
11155 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11156 dy = it.current_y - y0;
11157 if (dy > scroll_max)
11158 return SCROLLING_FAILED;
11159
11160 /* Compute new window start. */
11161 start_display (&it, w, startp);
11162
11163 if (scroll_conservatively)
11164 amount_to_scroll
11165 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11166 else if (scroll_step || temp_scroll_step)
11167 amount_to_scroll = scroll_max;
11168 else
11169 {
11170 aggressive = current_buffer->scroll_down_aggressively;
11171 height = WINDOW_BOX_TEXT_HEIGHT (w);
11172 if (NUMBERP (aggressive))
11173 {
11174 double float_amount = XFLOATINT (aggressive) * height;
11175 amount_to_scroll = float_amount;
11176 if (amount_to_scroll == 0 && float_amount > 0)
11177 amount_to_scroll = 1;
11178 }
11179 }
11180
11181 if (amount_to_scroll <= 0)
11182 return SCROLLING_FAILED;
11183
11184 move_it_vertically_backward (&it, amount_to_scroll);
11185 startp = it.current.pos;
11186 }
11187 }
11188
11189 /* Run window scroll functions. */
11190 startp = run_window_scroll_functions (window, startp);
11191
11192 /* Display the window. Give up if new fonts are loaded, or if point
11193 doesn't appear. */
11194 if (!try_window (window, startp))
11195 rc = SCROLLING_NEED_LARGER_MATRICES;
11196 else if (w->cursor.vpos < 0)
11197 {
11198 clear_glyph_matrix (w->desired_matrix);
11199 rc = SCROLLING_FAILED;
11200 }
11201 else
11202 {
11203 /* Maybe forget recorded base line for line number display. */
11204 if (!just_this_one_p
11205 || current_buffer->clip_changed
11206 || BEG_UNCHANGED < CHARPOS (startp))
11207 w->base_line_number = Qnil;
11208
11209 /* If cursor ends up on a partially visible line,
11210 treat that as being off the bottom of the screen. */
11211 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11212 {
11213 clear_glyph_matrix (w->desired_matrix);
11214 ++extra_scroll_margin_lines;
11215 goto too_near_end;
11216 }
11217 rc = SCROLLING_SUCCESS;
11218 }
11219
11220 return rc;
11221 }
11222
11223
11224 /* Compute a suitable window start for window W if display of W starts
11225 on a continuation line. Value is non-zero if a new window start
11226 was computed.
11227
11228 The new window start will be computed, based on W's width, starting
11229 from the start of the continued line. It is the start of the
11230 screen line with the minimum distance from the old start W->start. */
11231
11232 static int
11233 compute_window_start_on_continuation_line (w)
11234 struct window *w;
11235 {
11236 struct text_pos pos, start_pos;
11237 int window_start_changed_p = 0;
11238
11239 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11240
11241 /* If window start is on a continuation line... Window start may be
11242 < BEGV in case there's invisible text at the start of the
11243 buffer (M-x rmail, for example). */
11244 if (CHARPOS (start_pos) > BEGV
11245 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11246 {
11247 struct it it;
11248 struct glyph_row *row;
11249
11250 /* Handle the case that the window start is out of range. */
11251 if (CHARPOS (start_pos) < BEGV)
11252 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11253 else if (CHARPOS (start_pos) > ZV)
11254 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11255
11256 /* Find the start of the continued line. This should be fast
11257 because scan_buffer is fast (newline cache). */
11258 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11259 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11260 row, DEFAULT_FACE_ID);
11261 reseat_at_previous_visible_line_start (&it);
11262
11263 /* If the line start is "too far" away from the window start,
11264 say it takes too much time to compute a new window start. */
11265 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11266 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11267 {
11268 int min_distance, distance;
11269
11270 /* Move forward by display lines to find the new window
11271 start. If window width was enlarged, the new start can
11272 be expected to be > the old start. If window width was
11273 decreased, the new window start will be < the old start.
11274 So, we're looking for the display line start with the
11275 minimum distance from the old window start. */
11276 pos = it.current.pos;
11277 min_distance = INFINITY;
11278 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11279 distance < min_distance)
11280 {
11281 min_distance = distance;
11282 pos = it.current.pos;
11283 move_it_by_lines (&it, 1, 0);
11284 }
11285
11286 /* Set the window start there. */
11287 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11288 window_start_changed_p = 1;
11289 }
11290 }
11291
11292 return window_start_changed_p;
11293 }
11294
11295
11296 /* Try cursor movement in case text has not changed in window WINDOW,
11297 with window start STARTP. Value is
11298
11299 CURSOR_MOVEMENT_SUCCESS if successful
11300
11301 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11302
11303 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11304 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11305 we want to scroll as if scroll-step were set to 1. See the code.
11306
11307 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11308 which case we have to abort this redisplay, and adjust matrices
11309 first. */
11310
11311 enum
11312 {
11313 CURSOR_MOVEMENT_SUCCESS,
11314 CURSOR_MOVEMENT_CANNOT_BE_USED,
11315 CURSOR_MOVEMENT_MUST_SCROLL,
11316 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11317 };
11318
11319 static int
11320 try_cursor_movement (window, startp, scroll_step)
11321 Lisp_Object window;
11322 struct text_pos startp;
11323 int *scroll_step;
11324 {
11325 struct window *w = XWINDOW (window);
11326 struct frame *f = XFRAME (w->frame);
11327 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11328
11329 #if GLYPH_DEBUG
11330 if (inhibit_try_cursor_movement)
11331 return rc;
11332 #endif
11333
11334 /* Handle case where text has not changed, only point, and it has
11335 not moved off the frame. */
11336 if (/* Point may be in this window. */
11337 PT >= CHARPOS (startp)
11338 /* Selective display hasn't changed. */
11339 && !current_buffer->clip_changed
11340 /* Function force-mode-line-update is used to force a thorough
11341 redisplay. It sets either windows_or_buffers_changed or
11342 update_mode_lines. So don't take a shortcut here for these
11343 cases. */
11344 && !update_mode_lines
11345 && !windows_or_buffers_changed
11346 && !cursor_type_changed
11347 /* Can't use this case if highlighting a region. When a
11348 region exists, cursor movement has to do more than just
11349 set the cursor. */
11350 && !(!NILP (Vtransient_mark_mode)
11351 && !NILP (current_buffer->mark_active))
11352 && NILP (w->region_showing)
11353 && NILP (Vshow_trailing_whitespace)
11354 /* Right after splitting windows, last_point may be nil. */
11355 && INTEGERP (w->last_point)
11356 /* This code is not used for mini-buffer for the sake of the case
11357 of redisplaying to replace an echo area message; since in
11358 that case the mini-buffer contents per se are usually
11359 unchanged. This code is of no real use in the mini-buffer
11360 since the handling of this_line_start_pos, etc., in redisplay
11361 handles the same cases. */
11362 && !EQ (window, minibuf_window)
11363 /* When splitting windows or for new windows, it happens that
11364 redisplay is called with a nil window_end_vpos or one being
11365 larger than the window. This should really be fixed in
11366 window.c. I don't have this on my list, now, so we do
11367 approximately the same as the old redisplay code. --gerd. */
11368 && INTEGERP (w->window_end_vpos)
11369 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11370 && (FRAME_WINDOW_P (f)
11371 || !overlay_arrow_in_current_buffer_p ()))
11372 {
11373 int this_scroll_margin, top_scroll_margin;
11374 struct glyph_row *row = NULL;
11375
11376 #if GLYPH_DEBUG
11377 debug_method_add (w, "cursor movement");
11378 #endif
11379
11380 /* Scroll if point within this distance from the top or bottom
11381 of the window. This is a pixel value. */
11382 this_scroll_margin = max (0, scroll_margin);
11383 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11384 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11385
11386 top_scroll_margin = this_scroll_margin;
11387 if (WINDOW_WANTS_HEADER_LINE_P (w))
11388 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11389
11390 /* Start with the row the cursor was displayed during the last
11391 not paused redisplay. Give up if that row is not valid. */
11392 if (w->last_cursor.vpos < 0
11393 || w->last_cursor.vpos >= w->current_matrix->nrows)
11394 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11395 else
11396 {
11397 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11398 if (row->mode_line_p)
11399 ++row;
11400 if (!row->enabled_p)
11401 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11402 }
11403
11404 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11405 {
11406 int scroll_p = 0;
11407 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11408
11409 if (PT > XFASTINT (w->last_point))
11410 {
11411 /* Point has moved forward. */
11412 while (MATRIX_ROW_END_CHARPOS (row) < PT
11413 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11414 {
11415 xassert (row->enabled_p);
11416 ++row;
11417 }
11418
11419 /* The end position of a row equals the start position
11420 of the next row. If PT is there, we would rather
11421 display it in the next line. */
11422 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11423 && MATRIX_ROW_END_CHARPOS (row) == PT
11424 && !cursor_row_p (w, row))
11425 ++row;
11426
11427 /* If within the scroll margin, scroll. Note that
11428 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11429 the next line would be drawn, and that
11430 this_scroll_margin can be zero. */
11431 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11432 || PT > MATRIX_ROW_END_CHARPOS (row)
11433 /* Line is completely visible last line in window
11434 and PT is to be set in the next line. */
11435 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11436 && PT == MATRIX_ROW_END_CHARPOS (row)
11437 && !row->ends_at_zv_p
11438 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11439 scroll_p = 1;
11440 }
11441 else if (PT < XFASTINT (w->last_point))
11442 {
11443 /* Cursor has to be moved backward. Note that PT >=
11444 CHARPOS (startp) because of the outer if-statement. */
11445 while (!row->mode_line_p
11446 && (MATRIX_ROW_START_CHARPOS (row) > PT
11447 || (MATRIX_ROW_START_CHARPOS (row) == PT
11448 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11449 && (row->y > top_scroll_margin
11450 || CHARPOS (startp) == BEGV))
11451 {
11452 xassert (row->enabled_p);
11453 --row;
11454 }
11455
11456 /* Consider the following case: Window starts at BEGV,
11457 there is invisible, intangible text at BEGV, so that
11458 display starts at some point START > BEGV. It can
11459 happen that we are called with PT somewhere between
11460 BEGV and START. Try to handle that case. */
11461 if (row < w->current_matrix->rows
11462 || row->mode_line_p)
11463 {
11464 row = w->current_matrix->rows;
11465 if (row->mode_line_p)
11466 ++row;
11467 }
11468
11469 /* Due to newlines in overlay strings, we may have to
11470 skip forward over overlay strings. */
11471 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11472 && MATRIX_ROW_END_CHARPOS (row) == PT
11473 && !cursor_row_p (w, row))
11474 ++row;
11475
11476 /* If within the scroll margin, scroll. */
11477 if (row->y < top_scroll_margin
11478 && CHARPOS (startp) != BEGV)
11479 scroll_p = 1;
11480 }
11481
11482 if (PT < MATRIX_ROW_START_CHARPOS (row)
11483 || PT > MATRIX_ROW_END_CHARPOS (row))
11484 {
11485 /* if PT is not in the glyph row, give up. */
11486 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11487 }
11488 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11489 && make_cursor_line_fully_visible_p)
11490 {
11491 if (PT == MATRIX_ROW_END_CHARPOS (row)
11492 && !row->ends_at_zv_p
11493 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11494 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11495 else if (row->height > window_box_height (w))
11496 {
11497 /* If we end up in a partially visible line, let's
11498 make it fully visible, except when it's taller
11499 than the window, in which case we can't do much
11500 about it. */
11501 *scroll_step = 1;
11502 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11503 }
11504 else
11505 {
11506 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11507 if (!make_cursor_line_fully_visible (w, 0))
11508 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11509 else
11510 rc = CURSOR_MOVEMENT_SUCCESS;
11511 }
11512 }
11513 else if (scroll_p)
11514 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11515 else
11516 {
11517 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11518 rc = CURSOR_MOVEMENT_SUCCESS;
11519 }
11520 }
11521 }
11522
11523 return rc;
11524 }
11525
11526 void
11527 set_vertical_scroll_bar (w)
11528 struct window *w;
11529 {
11530 int start, end, whole;
11531
11532 /* Calculate the start and end positions for the current window.
11533 At some point, it would be nice to choose between scrollbars
11534 which reflect the whole buffer size, with special markers
11535 indicating narrowing, and scrollbars which reflect only the
11536 visible region.
11537
11538 Note that mini-buffers sometimes aren't displaying any text. */
11539 if (!MINI_WINDOW_P (w)
11540 || (w == XWINDOW (minibuf_window)
11541 && NILP (echo_area_buffer[0])))
11542 {
11543 struct buffer *buf = XBUFFER (w->buffer);
11544 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11545 start = marker_position (w->start) - BUF_BEGV (buf);
11546 /* I don't think this is guaranteed to be right. For the
11547 moment, we'll pretend it is. */
11548 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11549
11550 if (end < start)
11551 end = start;
11552 if (whole < (end - start))
11553 whole = end - start;
11554 }
11555 else
11556 start = end = whole = 0;
11557
11558 /* Indicate what this scroll bar ought to be displaying now. */
11559 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11560 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11561 (w, end - start, whole, start);
11562 }
11563
11564
11565 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11566 selected_window is redisplayed.
11567
11568 We can return without actually redisplaying the window if
11569 fonts_changed_p is nonzero. In that case, redisplay_internal will
11570 retry. */
11571
11572 static void
11573 redisplay_window (window, just_this_one_p)
11574 Lisp_Object window;
11575 int just_this_one_p;
11576 {
11577 struct window *w = XWINDOW (window);
11578 struct frame *f = XFRAME (w->frame);
11579 struct buffer *buffer = XBUFFER (w->buffer);
11580 struct buffer *old = current_buffer;
11581 struct text_pos lpoint, opoint, startp;
11582 int update_mode_line;
11583 int tem;
11584 struct it it;
11585 /* Record it now because it's overwritten. */
11586 int current_matrix_up_to_date_p = 0;
11587 int used_current_matrix_p = 0;
11588 /* This is less strict than current_matrix_up_to_date_p.
11589 It indictes that the buffer contents and narrowing are unchanged. */
11590 int buffer_unchanged_p = 0;
11591 int temp_scroll_step = 0;
11592 int count = SPECPDL_INDEX ();
11593 int rc;
11594 int centering_position;
11595 int last_line_misfit = 0;
11596
11597 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11598 opoint = lpoint;
11599
11600 /* W must be a leaf window here. */
11601 xassert (!NILP (w->buffer));
11602 #if GLYPH_DEBUG
11603 *w->desired_matrix->method = 0;
11604 #endif
11605
11606 specbind (Qinhibit_point_motion_hooks, Qt);
11607
11608 reconsider_clip_changes (w, buffer);
11609
11610 /* Has the mode line to be updated? */
11611 update_mode_line = (!NILP (w->update_mode_line)
11612 || update_mode_lines
11613 || buffer->clip_changed
11614 || buffer->prevent_redisplay_optimizations_p);
11615
11616 if (MINI_WINDOW_P (w))
11617 {
11618 if (w == XWINDOW (echo_area_window)
11619 && !NILP (echo_area_buffer[0]))
11620 {
11621 if (update_mode_line)
11622 /* We may have to update a tty frame's menu bar or a
11623 tool-bar. Example `M-x C-h C-h C-g'. */
11624 goto finish_menu_bars;
11625 else
11626 /* We've already displayed the echo area glyphs in this window. */
11627 goto finish_scroll_bars;
11628 }
11629 else if ((w != XWINDOW (minibuf_window)
11630 || minibuf_level == 0)
11631 /* When buffer is nonempty, redisplay window normally. */
11632 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11633 /* Quail displays non-mini buffers in minibuffer window.
11634 In that case, redisplay the window normally. */
11635 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11636 {
11637 /* W is a mini-buffer window, but it's not active, so clear
11638 it. */
11639 int yb = window_text_bottom_y (w);
11640 struct glyph_row *row;
11641 int y;
11642
11643 for (y = 0, row = w->desired_matrix->rows;
11644 y < yb;
11645 y += row->height, ++row)
11646 blank_row (w, row, y);
11647 goto finish_scroll_bars;
11648 }
11649
11650 clear_glyph_matrix (w->desired_matrix);
11651 }
11652
11653 /* Otherwise set up data on this window; select its buffer and point
11654 value. */
11655 /* Really select the buffer, for the sake of buffer-local
11656 variables. */
11657 set_buffer_internal_1 (XBUFFER (w->buffer));
11658 SET_TEXT_POS (opoint, PT, PT_BYTE);
11659
11660 current_matrix_up_to_date_p
11661 = (!NILP (w->window_end_valid)
11662 && !current_buffer->clip_changed
11663 && !current_buffer->prevent_redisplay_optimizations_p
11664 && XFASTINT (w->last_modified) >= MODIFF
11665 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11666
11667 buffer_unchanged_p
11668 = (!NILP (w->window_end_valid)
11669 && !current_buffer->clip_changed
11670 && XFASTINT (w->last_modified) >= MODIFF
11671 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11672
11673 /* When windows_or_buffers_changed is non-zero, we can't rely on
11674 the window end being valid, so set it to nil there. */
11675 if (windows_or_buffers_changed)
11676 {
11677 /* If window starts on a continuation line, maybe adjust the
11678 window start in case the window's width changed. */
11679 if (XMARKER (w->start)->buffer == current_buffer)
11680 compute_window_start_on_continuation_line (w);
11681
11682 w->window_end_valid = Qnil;
11683 }
11684
11685 /* Some sanity checks. */
11686 CHECK_WINDOW_END (w);
11687 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11688 abort ();
11689 if (BYTEPOS (opoint) < CHARPOS (opoint))
11690 abort ();
11691
11692 /* If %c is in mode line, update it if needed. */
11693 if (!NILP (w->column_number_displayed)
11694 /* This alternative quickly identifies a common case
11695 where no change is needed. */
11696 && !(PT == XFASTINT (w->last_point)
11697 && XFASTINT (w->last_modified) >= MODIFF
11698 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11699 && (XFASTINT (w->column_number_displayed)
11700 != (int) current_column ())) /* iftc */
11701 update_mode_line = 1;
11702
11703 /* Count number of windows showing the selected buffer. An indirect
11704 buffer counts as its base buffer. */
11705 if (!just_this_one_p)
11706 {
11707 struct buffer *current_base, *window_base;
11708 current_base = current_buffer;
11709 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11710 if (current_base->base_buffer)
11711 current_base = current_base->base_buffer;
11712 if (window_base->base_buffer)
11713 window_base = window_base->base_buffer;
11714 if (current_base == window_base)
11715 buffer_shared++;
11716 }
11717
11718 /* Point refers normally to the selected window. For any other
11719 window, set up appropriate value. */
11720 if (!EQ (window, selected_window))
11721 {
11722 int new_pt = XMARKER (w->pointm)->charpos;
11723 int new_pt_byte = marker_byte_position (w->pointm);
11724 if (new_pt < BEGV)
11725 {
11726 new_pt = BEGV;
11727 new_pt_byte = BEGV_BYTE;
11728 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11729 }
11730 else if (new_pt > (ZV - 1))
11731 {
11732 new_pt = ZV;
11733 new_pt_byte = ZV_BYTE;
11734 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11735 }
11736
11737 /* We don't use SET_PT so that the point-motion hooks don't run. */
11738 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11739 }
11740
11741 /* If any of the character widths specified in the display table
11742 have changed, invalidate the width run cache. It's true that
11743 this may be a bit late to catch such changes, but the rest of
11744 redisplay goes (non-fatally) haywire when the display table is
11745 changed, so why should we worry about doing any better? */
11746 if (current_buffer->width_run_cache)
11747 {
11748 struct Lisp_Char_Table *disptab = buffer_display_table ();
11749
11750 if (! disptab_matches_widthtab (disptab,
11751 XVECTOR (current_buffer->width_table)))
11752 {
11753 invalidate_region_cache (current_buffer,
11754 current_buffer->width_run_cache,
11755 BEG, Z);
11756 recompute_width_table (current_buffer, disptab);
11757 }
11758 }
11759
11760 /* If window-start is screwed up, choose a new one. */
11761 if (XMARKER (w->start)->buffer != current_buffer)
11762 goto recenter;
11763
11764 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11765
11766 /* If someone specified a new starting point but did not insist,
11767 check whether it can be used. */
11768 if (!NILP (w->optional_new_start)
11769 && CHARPOS (startp) >= BEGV
11770 && CHARPOS (startp) <= ZV)
11771 {
11772 w->optional_new_start = Qnil;
11773 start_display (&it, w, startp);
11774 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11775 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11776 if (IT_CHARPOS (it) == PT)
11777 w->force_start = Qt;
11778 /* IT may overshoot PT if text at PT is invisible. */
11779 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11780 w->force_start = Qt;
11781
11782
11783 }
11784
11785 /* Handle case where place to start displaying has been specified,
11786 unless the specified location is outside the accessible range. */
11787 if (!NILP (w->force_start)
11788 || w->frozen_window_start_p)
11789 {
11790 /* We set this later on if we have to adjust point. */
11791 int new_vpos = -1;
11792
11793 w->force_start = Qnil;
11794 w->vscroll = 0;
11795 w->window_end_valid = Qnil;
11796
11797 /* Forget any recorded base line for line number display. */
11798 if (!buffer_unchanged_p)
11799 w->base_line_number = Qnil;
11800
11801 /* Redisplay the mode line. Select the buffer properly for that.
11802 Also, run the hook window-scroll-functions
11803 because we have scrolled. */
11804 /* Note, we do this after clearing force_start because
11805 if there's an error, it is better to forget about force_start
11806 than to get into an infinite loop calling the hook functions
11807 and having them get more errors. */
11808 if (!update_mode_line
11809 || ! NILP (Vwindow_scroll_functions))
11810 {
11811 update_mode_line = 1;
11812 w->update_mode_line = Qt;
11813 startp = run_window_scroll_functions (window, startp);
11814 }
11815
11816 w->last_modified = make_number (0);
11817 w->last_overlay_modified = make_number (0);
11818 if (CHARPOS (startp) < BEGV)
11819 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11820 else if (CHARPOS (startp) > ZV)
11821 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11822
11823 /* Redisplay, then check if cursor has been set during the
11824 redisplay. Give up if new fonts were loaded. */
11825 if (!try_window (window, startp))
11826 {
11827 w->force_start = Qt;
11828 clear_glyph_matrix (w->desired_matrix);
11829 goto need_larger_matrices;
11830 }
11831
11832 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11833 {
11834 /* If point does not appear, try to move point so it does
11835 appear. The desired matrix has been built above, so we
11836 can use it here. */
11837 new_vpos = window_box_height (w) / 2;
11838 }
11839
11840 if (!make_cursor_line_fully_visible (w, 0))
11841 {
11842 /* Point does appear, but on a line partly visible at end of window.
11843 Move it back to a fully-visible line. */
11844 new_vpos = window_box_height (w);
11845 }
11846
11847 /* If we need to move point for either of the above reasons,
11848 now actually do it. */
11849 if (new_vpos >= 0)
11850 {
11851 struct glyph_row *row;
11852
11853 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11854 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11855 ++row;
11856
11857 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11858 MATRIX_ROW_START_BYTEPOS (row));
11859
11860 if (w != XWINDOW (selected_window))
11861 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11862 else if (current_buffer == old)
11863 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11864
11865 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11866
11867 /* If we are highlighting the region, then we just changed
11868 the region, so redisplay to show it. */
11869 if (!NILP (Vtransient_mark_mode)
11870 && !NILP (current_buffer->mark_active))
11871 {
11872 clear_glyph_matrix (w->desired_matrix);
11873 if (!try_window (window, startp))
11874 goto need_larger_matrices;
11875 }
11876 }
11877
11878 #if GLYPH_DEBUG
11879 debug_method_add (w, "forced window start");
11880 #endif
11881 goto done;
11882 }
11883
11884 /* Handle case where text has not changed, only point, and it has
11885 not moved off the frame, and we are not retrying after hscroll.
11886 (current_matrix_up_to_date_p is nonzero when retrying.) */
11887 if (current_matrix_up_to_date_p
11888 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11889 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11890 {
11891 switch (rc)
11892 {
11893 case CURSOR_MOVEMENT_SUCCESS:
11894 used_current_matrix_p = 1;
11895 goto done;
11896
11897 #if 0 /* try_cursor_movement never returns this value. */
11898 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11899 goto need_larger_matrices;
11900 #endif
11901
11902 case CURSOR_MOVEMENT_MUST_SCROLL:
11903 goto try_to_scroll;
11904
11905 default:
11906 abort ();
11907 }
11908 }
11909 /* If current starting point was originally the beginning of a line
11910 but no longer is, find a new starting point. */
11911 else if (!NILP (w->start_at_line_beg)
11912 && !(CHARPOS (startp) <= BEGV
11913 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11914 {
11915 #if GLYPH_DEBUG
11916 debug_method_add (w, "recenter 1");
11917 #endif
11918 goto recenter;
11919 }
11920
11921 /* Try scrolling with try_window_id. Value is > 0 if update has
11922 been done, it is -1 if we know that the same window start will
11923 not work. It is 0 if unsuccessful for some other reason. */
11924 else if ((tem = try_window_id (w)) != 0)
11925 {
11926 #if GLYPH_DEBUG
11927 debug_method_add (w, "try_window_id %d", tem);
11928 #endif
11929
11930 if (fonts_changed_p)
11931 goto need_larger_matrices;
11932 if (tem > 0)
11933 goto done;
11934
11935 /* Otherwise try_window_id has returned -1 which means that we
11936 don't want the alternative below this comment to execute. */
11937 }
11938 else if (CHARPOS (startp) >= BEGV
11939 && CHARPOS (startp) <= ZV
11940 && PT >= CHARPOS (startp)
11941 && (CHARPOS (startp) < ZV
11942 /* Avoid starting at end of buffer. */
11943 || CHARPOS (startp) == BEGV
11944 || (XFASTINT (w->last_modified) >= MODIFF
11945 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11946 {
11947 #if GLYPH_DEBUG
11948 debug_method_add (w, "same window start");
11949 #endif
11950
11951 /* Try to redisplay starting at same place as before.
11952 If point has not moved off frame, accept the results. */
11953 if (!current_matrix_up_to_date_p
11954 /* Don't use try_window_reusing_current_matrix in this case
11955 because a window scroll function can have changed the
11956 buffer. */
11957 || !NILP (Vwindow_scroll_functions)
11958 || MINI_WINDOW_P (w)
11959 || !(used_current_matrix_p
11960 = try_window_reusing_current_matrix (w)))
11961 {
11962 IF_DEBUG (debug_method_add (w, "1"));
11963 try_window (window, startp);
11964 }
11965
11966 if (fonts_changed_p)
11967 goto need_larger_matrices;
11968
11969 if (w->cursor.vpos >= 0)
11970 {
11971 if (!just_this_one_p
11972 || current_buffer->clip_changed
11973 || BEG_UNCHANGED < CHARPOS (startp))
11974 /* Forget any recorded base line for line number display. */
11975 w->base_line_number = Qnil;
11976
11977 if (!make_cursor_line_fully_visible (w, 1))
11978 {
11979 clear_glyph_matrix (w->desired_matrix);
11980 last_line_misfit = 1;
11981 }
11982 /* Drop through and scroll. */
11983 else
11984 goto done;
11985 }
11986 else
11987 clear_glyph_matrix (w->desired_matrix);
11988 }
11989
11990 try_to_scroll:
11991
11992 w->last_modified = make_number (0);
11993 w->last_overlay_modified = make_number (0);
11994
11995 /* Redisplay the mode line. Select the buffer properly for that. */
11996 if (!update_mode_line)
11997 {
11998 update_mode_line = 1;
11999 w->update_mode_line = Qt;
12000 }
12001
12002 /* Try to scroll by specified few lines. */
12003 if ((scroll_conservatively
12004 || scroll_step
12005 || temp_scroll_step
12006 || NUMBERP (current_buffer->scroll_up_aggressively)
12007 || NUMBERP (current_buffer->scroll_down_aggressively))
12008 && !current_buffer->clip_changed
12009 && CHARPOS (startp) >= BEGV
12010 && CHARPOS (startp) <= ZV)
12011 {
12012 /* The function returns -1 if new fonts were loaded, 1 if
12013 successful, 0 if not successful. */
12014 int rc = try_scrolling (window, just_this_one_p,
12015 scroll_conservatively,
12016 scroll_step,
12017 temp_scroll_step, last_line_misfit);
12018 switch (rc)
12019 {
12020 case SCROLLING_SUCCESS:
12021 goto done;
12022
12023 case SCROLLING_NEED_LARGER_MATRICES:
12024 goto need_larger_matrices;
12025
12026 case SCROLLING_FAILED:
12027 break;
12028
12029 default:
12030 abort ();
12031 }
12032 }
12033
12034 /* Finally, just choose place to start which centers point */
12035
12036 recenter:
12037 centering_position = window_box_height (w) / 2;
12038
12039 point_at_top:
12040 /* Jump here with centering_position already set to 0. */
12041
12042 #if GLYPH_DEBUG
12043 debug_method_add (w, "recenter");
12044 #endif
12045
12046 /* w->vscroll = 0; */
12047
12048 /* Forget any previously recorded base line for line number display. */
12049 if (!buffer_unchanged_p)
12050 w->base_line_number = Qnil;
12051
12052 /* Move backward half the height of the window. */
12053 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12054 it.current_y = it.last_visible_y;
12055 move_it_vertically_backward (&it, centering_position);
12056 xassert (IT_CHARPOS (it) >= BEGV);
12057
12058 /* The function move_it_vertically_backward may move over more
12059 than the specified y-distance. If it->w is small, e.g. a
12060 mini-buffer window, we may end up in front of the window's
12061 display area. Start displaying at the start of the line
12062 containing PT in this case. */
12063 if (it.current_y <= 0)
12064 {
12065 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12066 move_it_vertically_backward (&it, 0);
12067 xassert (IT_CHARPOS (it) <= PT);
12068 it.current_y = 0;
12069 }
12070
12071 it.current_x = it.hpos = 0;
12072
12073 /* Set startp here explicitly in case that helps avoid an infinite loop
12074 in case the window-scroll-functions functions get errors. */
12075 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12076
12077 /* Run scroll hooks. */
12078 startp = run_window_scroll_functions (window, it.current.pos);
12079
12080 /* Redisplay the window. */
12081 if (!current_matrix_up_to_date_p
12082 || windows_or_buffers_changed
12083 || cursor_type_changed
12084 /* Don't use try_window_reusing_current_matrix in this case
12085 because it can have changed the buffer. */
12086 || !NILP (Vwindow_scroll_functions)
12087 || !just_this_one_p
12088 || MINI_WINDOW_P (w)
12089 || !(used_current_matrix_p
12090 = try_window_reusing_current_matrix (w)))
12091 try_window (window, startp);
12092
12093 /* If new fonts have been loaded (due to fontsets), give up. We
12094 have to start a new redisplay since we need to re-adjust glyph
12095 matrices. */
12096 if (fonts_changed_p)
12097 goto need_larger_matrices;
12098
12099 /* If cursor did not appear assume that the middle of the window is
12100 in the first line of the window. Do it again with the next line.
12101 (Imagine a window of height 100, displaying two lines of height
12102 60. Moving back 50 from it->last_visible_y will end in the first
12103 line.) */
12104 if (w->cursor.vpos < 0)
12105 {
12106 if (!NILP (w->window_end_valid)
12107 && PT >= Z - XFASTINT (w->window_end_pos))
12108 {
12109 clear_glyph_matrix (w->desired_matrix);
12110 move_it_by_lines (&it, 1, 0);
12111 try_window (window, it.current.pos);
12112 }
12113 else if (PT < IT_CHARPOS (it))
12114 {
12115 clear_glyph_matrix (w->desired_matrix);
12116 move_it_by_lines (&it, -1, 0);
12117 try_window (window, it.current.pos);
12118 }
12119 else
12120 {
12121 /* Not much we can do about it. */
12122 }
12123 }
12124
12125 /* Consider the following case: Window starts at BEGV, there is
12126 invisible, intangible text at BEGV, so that display starts at
12127 some point START > BEGV. It can happen that we are called with
12128 PT somewhere between BEGV and START. Try to handle that case. */
12129 if (w->cursor.vpos < 0)
12130 {
12131 struct glyph_row *row = w->current_matrix->rows;
12132 if (row->mode_line_p)
12133 ++row;
12134 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12135 }
12136
12137 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12138 {
12139 /* If vscroll is enabled, disable it and try again. */
12140 if (w->vscroll)
12141 {
12142 w->vscroll = 0;
12143 clear_glyph_matrix (w->desired_matrix);
12144 goto recenter;
12145 }
12146
12147 /* If centering point failed to make the whole line visible,
12148 put point at the top instead. That has to make the whole line
12149 visible, if it can be done. */
12150 clear_glyph_matrix (w->desired_matrix);
12151 centering_position = 0;
12152 goto point_at_top;
12153 }
12154
12155 done:
12156
12157 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12158 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12159 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12160 ? Qt : Qnil);
12161
12162 /* Display the mode line, if we must. */
12163 if ((update_mode_line
12164 /* If window not full width, must redo its mode line
12165 if (a) the window to its side is being redone and
12166 (b) we do a frame-based redisplay. This is a consequence
12167 of how inverted lines are drawn in frame-based redisplay. */
12168 || (!just_this_one_p
12169 && !FRAME_WINDOW_P (f)
12170 && !WINDOW_FULL_WIDTH_P (w))
12171 /* Line number to display. */
12172 || INTEGERP (w->base_line_pos)
12173 /* Column number is displayed and different from the one displayed. */
12174 || (!NILP (w->column_number_displayed)
12175 && (XFASTINT (w->column_number_displayed)
12176 != (int) current_column ()))) /* iftc */
12177 /* This means that the window has a mode line. */
12178 && (WINDOW_WANTS_MODELINE_P (w)
12179 || WINDOW_WANTS_HEADER_LINE_P (w)))
12180 {
12181 display_mode_lines (w);
12182
12183 /* If mode line height has changed, arrange for a thorough
12184 immediate redisplay using the correct mode line height. */
12185 if (WINDOW_WANTS_MODELINE_P (w)
12186 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12187 {
12188 fonts_changed_p = 1;
12189 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12190 = DESIRED_MODE_LINE_HEIGHT (w);
12191 }
12192
12193 /* If top line height has changed, arrange for a thorough
12194 immediate redisplay using the correct mode line height. */
12195 if (WINDOW_WANTS_HEADER_LINE_P (w)
12196 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12197 {
12198 fonts_changed_p = 1;
12199 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12200 = DESIRED_HEADER_LINE_HEIGHT (w);
12201 }
12202
12203 if (fonts_changed_p)
12204 goto need_larger_matrices;
12205 }
12206
12207 if (!line_number_displayed
12208 && !BUFFERP (w->base_line_pos))
12209 {
12210 w->base_line_pos = Qnil;
12211 w->base_line_number = Qnil;
12212 }
12213
12214 finish_menu_bars:
12215
12216 /* When we reach a frame's selected window, redo the frame's menu bar. */
12217 if (update_mode_line
12218 && EQ (FRAME_SELECTED_WINDOW (f), window))
12219 {
12220 int redisplay_menu_p = 0;
12221 int redisplay_tool_bar_p = 0;
12222
12223 if (FRAME_WINDOW_P (f))
12224 {
12225 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12226 || defined (USE_GTK)
12227 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12228 #else
12229 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12230 #endif
12231 }
12232 else
12233 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12234
12235 if (redisplay_menu_p)
12236 display_menu_bar (w);
12237
12238 #ifdef HAVE_WINDOW_SYSTEM
12239 if (FRAME_WINDOW_P (f))
12240 {
12241 #ifdef USE_GTK
12242 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12243 #else
12244 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12245 && (FRAME_TOOL_BAR_LINES (f) > 0
12246 || auto_resize_tool_bars_p);
12247 #endif
12248
12249 if (redisplay_tool_bar_p)
12250 redisplay_tool_bar (f);
12251 }
12252 #endif
12253 }
12254
12255 #ifdef HAVE_WINDOW_SYSTEM
12256 if (FRAME_WINDOW_P (f)
12257 && update_window_fringes (w, 0)
12258 && !just_this_one_p
12259 && (used_current_matrix_p || overlay_arrow_seen)
12260 && !w->pseudo_window_p)
12261 {
12262 update_begin (f);
12263 BLOCK_INPUT;
12264 if (draw_window_fringes (w, 1))
12265 x_draw_vertical_border (w);
12266 UNBLOCK_INPUT;
12267 update_end (f);
12268 }
12269 #endif /* HAVE_WINDOW_SYSTEM */
12270
12271 /* We go to this label, with fonts_changed_p nonzero,
12272 if it is necessary to try again using larger glyph matrices.
12273 We have to redeem the scroll bar even in this case,
12274 because the loop in redisplay_internal expects that. */
12275 need_larger_matrices:
12276 ;
12277 finish_scroll_bars:
12278
12279 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12280 {
12281 /* Set the thumb's position and size. */
12282 set_vertical_scroll_bar (w);
12283
12284 /* Note that we actually used the scroll bar attached to this
12285 window, so it shouldn't be deleted at the end of redisplay. */
12286 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12287 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12288 }
12289
12290 /* Restore current_buffer and value of point in it. */
12291 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12292 set_buffer_internal_1 (old);
12293 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12294
12295 unbind_to (count, Qnil);
12296 }
12297
12298
12299 /* Build the complete desired matrix of WINDOW with a window start
12300 buffer position POS. Value is non-zero if successful. It is zero
12301 if fonts were loaded during redisplay which makes re-adjusting
12302 glyph matrices necessary. */
12303
12304 int
12305 try_window (window, pos)
12306 Lisp_Object window;
12307 struct text_pos pos;
12308 {
12309 struct window *w = XWINDOW (window);
12310 struct it it;
12311 struct glyph_row *last_text_row = NULL;
12312
12313 /* Make POS the new window start. */
12314 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12315
12316 /* Mark cursor position as unknown. No overlay arrow seen. */
12317 w->cursor.vpos = -1;
12318 overlay_arrow_seen = 0;
12319
12320 /* Initialize iterator and info to start at POS. */
12321 start_display (&it, w, pos);
12322
12323 /* Display all lines of W. */
12324 while (it.current_y < it.last_visible_y)
12325 {
12326 if (display_line (&it))
12327 last_text_row = it.glyph_row - 1;
12328 if (fonts_changed_p)
12329 return 0;
12330 }
12331
12332 /* If bottom moved off end of frame, change mode line percentage. */
12333 if (XFASTINT (w->window_end_pos) <= 0
12334 && Z != IT_CHARPOS (it))
12335 w->update_mode_line = Qt;
12336
12337 /* Set window_end_pos to the offset of the last character displayed
12338 on the window from the end of current_buffer. Set
12339 window_end_vpos to its row number. */
12340 if (last_text_row)
12341 {
12342 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12343 w->window_end_bytepos
12344 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12345 w->window_end_pos
12346 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12347 w->window_end_vpos
12348 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12349 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12350 ->displays_text_p);
12351 }
12352 else
12353 {
12354 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12355 w->window_end_pos = make_number (Z - ZV);
12356 w->window_end_vpos = make_number (0);
12357 }
12358
12359 /* But that is not valid info until redisplay finishes. */
12360 w->window_end_valid = Qnil;
12361 return 1;
12362 }
12363
12364
12365 \f
12366 /************************************************************************
12367 Window redisplay reusing current matrix when buffer has not changed
12368 ************************************************************************/
12369
12370 /* Try redisplay of window W showing an unchanged buffer with a
12371 different window start than the last time it was displayed by
12372 reusing its current matrix. Value is non-zero if successful.
12373 W->start is the new window start. */
12374
12375 static int
12376 try_window_reusing_current_matrix (w)
12377 struct window *w;
12378 {
12379 struct frame *f = XFRAME (w->frame);
12380 struct glyph_row *row, *bottom_row;
12381 struct it it;
12382 struct run run;
12383 struct text_pos start, new_start;
12384 int nrows_scrolled, i;
12385 struct glyph_row *last_text_row;
12386 struct glyph_row *last_reused_text_row;
12387 struct glyph_row *start_row;
12388 int start_vpos, min_y, max_y;
12389
12390 #if GLYPH_DEBUG
12391 if (inhibit_try_window_reusing)
12392 return 0;
12393 #endif
12394
12395 if (/* This function doesn't handle terminal frames. */
12396 !FRAME_WINDOW_P (f)
12397 /* Don't try to reuse the display if windows have been split
12398 or such. */
12399 || windows_or_buffers_changed
12400 || cursor_type_changed)
12401 return 0;
12402
12403 /* Can't do this if region may have changed. */
12404 if ((!NILP (Vtransient_mark_mode)
12405 && !NILP (current_buffer->mark_active))
12406 || !NILP (w->region_showing)
12407 || !NILP (Vshow_trailing_whitespace))
12408 return 0;
12409
12410 /* If top-line visibility has changed, give up. */
12411 if (WINDOW_WANTS_HEADER_LINE_P (w)
12412 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12413 return 0;
12414
12415 /* Give up if old or new display is scrolled vertically. We could
12416 make this function handle this, but right now it doesn't. */
12417 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12418 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12419 return 0;
12420
12421 /* The variable new_start now holds the new window start. The old
12422 start `start' can be determined from the current matrix. */
12423 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12424 start = start_row->start.pos;
12425 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12426
12427 /* Clear the desired matrix for the display below. */
12428 clear_glyph_matrix (w->desired_matrix);
12429
12430 if (CHARPOS (new_start) <= CHARPOS (start))
12431 {
12432 int first_row_y;
12433
12434 /* Don't use this method if the display starts with an ellipsis
12435 displayed for invisible text. It's not easy to handle that case
12436 below, and it's certainly not worth the effort since this is
12437 not a frequent case. */
12438 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12439 return 0;
12440
12441 IF_DEBUG (debug_method_add (w, "twu1"));
12442
12443 /* Display up to a row that can be reused. The variable
12444 last_text_row is set to the last row displayed that displays
12445 text. Note that it.vpos == 0 if or if not there is a
12446 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12447 start_display (&it, w, new_start);
12448 first_row_y = it.current_y;
12449 w->cursor.vpos = -1;
12450 last_text_row = last_reused_text_row = NULL;
12451
12452 while (it.current_y < it.last_visible_y
12453 && !fonts_changed_p)
12454 {
12455 /* If we have reached into the characters in the START row,
12456 that means the line boundaries have changed. So we
12457 can't start copying with the row START. Maybe it will
12458 work to start copying with the following row. */
12459 while (IT_CHARPOS (it) > CHARPOS (start))
12460 {
12461 /* Advance to the next row as the "start". */
12462 start_row++;
12463 start = start_row->start.pos;
12464 /* If there are no more rows to try, or just one, give up. */
12465 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12466 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12467 || CHARPOS (start) == ZV)
12468 {
12469 clear_glyph_matrix (w->desired_matrix);
12470 return 0;
12471 }
12472
12473 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12474 }
12475 /* If we have reached alignment,
12476 we can copy the rest of the rows. */
12477 if (IT_CHARPOS (it) == CHARPOS (start))
12478 break;
12479
12480 if (display_line (&it))
12481 last_text_row = it.glyph_row - 1;
12482 }
12483
12484 /* A value of current_y < last_visible_y means that we stopped
12485 at the previous window start, which in turn means that we
12486 have at least one reusable row. */
12487 if (it.current_y < it.last_visible_y)
12488 {
12489 /* IT.vpos always starts from 0; it counts text lines. */
12490 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12491
12492 /* Find PT if not already found in the lines displayed. */
12493 if (w->cursor.vpos < 0)
12494 {
12495 int dy = it.current_y - start_row->y;
12496
12497 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12498 row = row_containing_pos (w, PT, row, NULL, dy);
12499 if (row)
12500 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12501 dy, nrows_scrolled);
12502 else
12503 {
12504 clear_glyph_matrix (w->desired_matrix);
12505 return 0;
12506 }
12507 }
12508
12509 /* Scroll the display. Do it before the current matrix is
12510 changed. The problem here is that update has not yet
12511 run, i.e. part of the current matrix is not up to date.
12512 scroll_run_hook will clear the cursor, and use the
12513 current matrix to get the height of the row the cursor is
12514 in. */
12515 run.current_y = start_row->y;
12516 run.desired_y = it.current_y;
12517 run.height = it.last_visible_y - it.current_y;
12518
12519 if (run.height > 0 && run.current_y != run.desired_y)
12520 {
12521 update_begin (f);
12522 FRAME_RIF (f)->update_window_begin_hook (w);
12523 FRAME_RIF (f)->clear_window_mouse_face (w);
12524 FRAME_RIF (f)->scroll_run_hook (w, &run);
12525 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12526 update_end (f);
12527 }
12528
12529 /* Shift current matrix down by nrows_scrolled lines. */
12530 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12531 rotate_matrix (w->current_matrix,
12532 start_vpos,
12533 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12534 nrows_scrolled);
12535
12536 /* Disable lines that must be updated. */
12537 for (i = 0; i < it.vpos; ++i)
12538 (start_row + i)->enabled_p = 0;
12539
12540 /* Re-compute Y positions. */
12541 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12542 max_y = it.last_visible_y;
12543 for (row = start_row + nrows_scrolled;
12544 row < bottom_row;
12545 ++row)
12546 {
12547 row->y = it.current_y;
12548 row->visible_height = row->height;
12549
12550 if (row->y < min_y)
12551 row->visible_height -= min_y - row->y;
12552 if (row->y + row->height > max_y)
12553 row->visible_height -= row->y + row->height - max_y;
12554 row->redraw_fringe_bitmaps_p = 1;
12555
12556 it.current_y += row->height;
12557
12558 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12559 last_reused_text_row = row;
12560 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12561 break;
12562 }
12563
12564 /* Disable lines in the current matrix which are now
12565 below the window. */
12566 for (++row; row < bottom_row; ++row)
12567 row->enabled_p = 0;
12568 }
12569
12570 /* Update window_end_pos etc.; last_reused_text_row is the last
12571 reused row from the current matrix containing text, if any.
12572 The value of last_text_row is the last displayed line
12573 containing text. */
12574 if (last_reused_text_row)
12575 {
12576 w->window_end_bytepos
12577 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12578 w->window_end_pos
12579 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12580 w->window_end_vpos
12581 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12582 w->current_matrix));
12583 }
12584 else if (last_text_row)
12585 {
12586 w->window_end_bytepos
12587 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12588 w->window_end_pos
12589 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12590 w->window_end_vpos
12591 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12592 }
12593 else
12594 {
12595 /* This window must be completely empty. */
12596 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12597 w->window_end_pos = make_number (Z - ZV);
12598 w->window_end_vpos = make_number (0);
12599 }
12600 w->window_end_valid = Qnil;
12601
12602 /* Update hint: don't try scrolling again in update_window. */
12603 w->desired_matrix->no_scrolling_p = 1;
12604
12605 #if GLYPH_DEBUG
12606 debug_method_add (w, "try_window_reusing_current_matrix 1");
12607 #endif
12608 return 1;
12609 }
12610 else if (CHARPOS (new_start) > CHARPOS (start))
12611 {
12612 struct glyph_row *pt_row, *row;
12613 struct glyph_row *first_reusable_row;
12614 struct glyph_row *first_row_to_display;
12615 int dy;
12616 int yb = window_text_bottom_y (w);
12617
12618 /* Find the row starting at new_start, if there is one. Don't
12619 reuse a partially visible line at the end. */
12620 first_reusable_row = start_row;
12621 while (first_reusable_row->enabled_p
12622 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12623 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12624 < CHARPOS (new_start)))
12625 ++first_reusable_row;
12626
12627 /* Give up if there is no row to reuse. */
12628 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12629 || !first_reusable_row->enabled_p
12630 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12631 != CHARPOS (new_start)))
12632 return 0;
12633
12634 /* We can reuse fully visible rows beginning with
12635 first_reusable_row to the end of the window. Set
12636 first_row_to_display to the first row that cannot be reused.
12637 Set pt_row to the row containing point, if there is any. */
12638 pt_row = NULL;
12639 for (first_row_to_display = first_reusable_row;
12640 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12641 ++first_row_to_display)
12642 {
12643 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12644 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12645 pt_row = first_row_to_display;
12646 }
12647
12648 /* Start displaying at the start of first_row_to_display. */
12649 xassert (first_row_to_display->y < yb);
12650 init_to_row_start (&it, w, first_row_to_display);
12651
12652 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12653 - start_vpos);
12654 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12655 - nrows_scrolled);
12656 it.current_y = (first_row_to_display->y - first_reusable_row->y
12657 + WINDOW_HEADER_LINE_HEIGHT (w));
12658
12659 /* Display lines beginning with first_row_to_display in the
12660 desired matrix. Set last_text_row to the last row displayed
12661 that displays text. */
12662 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12663 if (pt_row == NULL)
12664 w->cursor.vpos = -1;
12665 last_text_row = NULL;
12666 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12667 if (display_line (&it))
12668 last_text_row = it.glyph_row - 1;
12669
12670 /* Give up If point isn't in a row displayed or reused. */
12671 if (w->cursor.vpos < 0)
12672 {
12673 clear_glyph_matrix (w->desired_matrix);
12674 return 0;
12675 }
12676
12677 /* If point is in a reused row, adjust y and vpos of the cursor
12678 position. */
12679 if (pt_row)
12680 {
12681 w->cursor.vpos -= nrows_scrolled;
12682 w->cursor.y -= first_reusable_row->y - start_row->y;
12683 }
12684
12685 /* Scroll the display. */
12686 run.current_y = first_reusable_row->y;
12687 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12688 run.height = it.last_visible_y - run.current_y;
12689 dy = run.current_y - run.desired_y;
12690
12691 if (run.height)
12692 {
12693 update_begin (f);
12694 FRAME_RIF (f)->update_window_begin_hook (w);
12695 FRAME_RIF (f)->clear_window_mouse_face (w);
12696 FRAME_RIF (f)->scroll_run_hook (w, &run);
12697 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12698 update_end (f);
12699 }
12700
12701 /* Adjust Y positions of reused rows. */
12702 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12703 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12704 max_y = it.last_visible_y;
12705 for (row = first_reusable_row; row < first_row_to_display; ++row)
12706 {
12707 row->y -= dy;
12708 row->visible_height = row->height;
12709 if (row->y < min_y)
12710 row->visible_height -= min_y - row->y;
12711 if (row->y + row->height > max_y)
12712 row->visible_height -= row->y + row->height - max_y;
12713 row->redraw_fringe_bitmaps_p = 1;
12714 }
12715
12716 /* Scroll the current matrix. */
12717 xassert (nrows_scrolled > 0);
12718 rotate_matrix (w->current_matrix,
12719 start_vpos,
12720 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12721 -nrows_scrolled);
12722
12723 /* Disable rows not reused. */
12724 for (row -= nrows_scrolled; row < bottom_row; ++row)
12725 row->enabled_p = 0;
12726
12727 /* Point may have moved to a different line, so we cannot assume that
12728 the previous cursor position is valid; locate the correct row. */
12729 if (pt_row)
12730 {
12731 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12732 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12733 row++)
12734 {
12735 w->cursor.vpos++;
12736 w->cursor.y = row->y;
12737 }
12738 if (row < bottom_row)
12739 {
12740 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12741 while (glyph->charpos < PT)
12742 {
12743 w->cursor.hpos++;
12744 w->cursor.x += glyph->pixel_width;
12745 glyph++;
12746 }
12747 }
12748 }
12749
12750 /* Adjust window end. A null value of last_text_row means that
12751 the window end is in reused rows which in turn means that
12752 only its vpos can have changed. */
12753 if (last_text_row)
12754 {
12755 w->window_end_bytepos
12756 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12757 w->window_end_pos
12758 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12759 w->window_end_vpos
12760 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12761 }
12762 else
12763 {
12764 w->window_end_vpos
12765 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12766 }
12767
12768 w->window_end_valid = Qnil;
12769 w->desired_matrix->no_scrolling_p = 1;
12770
12771 #if GLYPH_DEBUG
12772 debug_method_add (w, "try_window_reusing_current_matrix 2");
12773 #endif
12774 return 1;
12775 }
12776
12777 return 0;
12778 }
12779
12780
12781 \f
12782 /************************************************************************
12783 Window redisplay reusing current matrix when buffer has changed
12784 ************************************************************************/
12785
12786 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12787 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12788 int *, int *));
12789 static struct glyph_row *
12790 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12791 struct glyph_row *));
12792
12793
12794 /* Return the last row in MATRIX displaying text. If row START is
12795 non-null, start searching with that row. IT gives the dimensions
12796 of the display. Value is null if matrix is empty; otherwise it is
12797 a pointer to the row found. */
12798
12799 static struct glyph_row *
12800 find_last_row_displaying_text (matrix, it, start)
12801 struct glyph_matrix *matrix;
12802 struct it *it;
12803 struct glyph_row *start;
12804 {
12805 struct glyph_row *row, *row_found;
12806
12807 /* Set row_found to the last row in IT->w's current matrix
12808 displaying text. The loop looks funny but think of partially
12809 visible lines. */
12810 row_found = NULL;
12811 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12812 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12813 {
12814 xassert (row->enabled_p);
12815 row_found = row;
12816 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12817 break;
12818 ++row;
12819 }
12820
12821 return row_found;
12822 }
12823
12824
12825 /* Return the last row in the current matrix of W that is not affected
12826 by changes at the start of current_buffer that occurred since W's
12827 current matrix was built. Value is null if no such row exists.
12828
12829 BEG_UNCHANGED us the number of characters unchanged at the start of
12830 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12831 first changed character in current_buffer. Characters at positions <
12832 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12833 when the current matrix was built. */
12834
12835 static struct glyph_row *
12836 find_last_unchanged_at_beg_row (w)
12837 struct window *w;
12838 {
12839 int first_changed_pos = BEG + BEG_UNCHANGED;
12840 struct glyph_row *row;
12841 struct glyph_row *row_found = NULL;
12842 int yb = window_text_bottom_y (w);
12843
12844 /* Find the last row displaying unchanged text. */
12845 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12846 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12847 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12848 {
12849 if (/* If row ends before first_changed_pos, it is unchanged,
12850 except in some case. */
12851 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12852 /* When row ends in ZV and we write at ZV it is not
12853 unchanged. */
12854 && !row->ends_at_zv_p
12855 /* When first_changed_pos is the end of a continued line,
12856 row is not unchanged because it may be no longer
12857 continued. */
12858 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12859 && (row->continued_p
12860 || row->exact_window_width_line_p)))
12861 row_found = row;
12862
12863 /* Stop if last visible row. */
12864 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12865 break;
12866
12867 ++row;
12868 }
12869
12870 return row_found;
12871 }
12872
12873
12874 /* Find the first glyph row in the current matrix of W that is not
12875 affected by changes at the end of current_buffer since the
12876 time W's current matrix was built.
12877
12878 Return in *DELTA the number of chars by which buffer positions in
12879 unchanged text at the end of current_buffer must be adjusted.
12880
12881 Return in *DELTA_BYTES the corresponding number of bytes.
12882
12883 Value is null if no such row exists, i.e. all rows are affected by
12884 changes. */
12885
12886 static struct glyph_row *
12887 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12888 struct window *w;
12889 int *delta, *delta_bytes;
12890 {
12891 struct glyph_row *row;
12892 struct glyph_row *row_found = NULL;
12893
12894 *delta = *delta_bytes = 0;
12895
12896 /* Display must not have been paused, otherwise the current matrix
12897 is not up to date. */
12898 if (NILP (w->window_end_valid))
12899 abort ();
12900
12901 /* A value of window_end_pos >= END_UNCHANGED means that the window
12902 end is in the range of changed text. If so, there is no
12903 unchanged row at the end of W's current matrix. */
12904 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12905 return NULL;
12906
12907 /* Set row to the last row in W's current matrix displaying text. */
12908 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12909
12910 /* If matrix is entirely empty, no unchanged row exists. */
12911 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12912 {
12913 /* The value of row is the last glyph row in the matrix having a
12914 meaningful buffer position in it. The end position of row
12915 corresponds to window_end_pos. This allows us to translate
12916 buffer positions in the current matrix to current buffer
12917 positions for characters not in changed text. */
12918 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12919 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12920 int last_unchanged_pos, last_unchanged_pos_old;
12921 struct glyph_row *first_text_row
12922 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12923
12924 *delta = Z - Z_old;
12925 *delta_bytes = Z_BYTE - Z_BYTE_old;
12926
12927 /* Set last_unchanged_pos to the buffer position of the last
12928 character in the buffer that has not been changed. Z is the
12929 index + 1 of the last character in current_buffer, i.e. by
12930 subtracting END_UNCHANGED we get the index of the last
12931 unchanged character, and we have to add BEG to get its buffer
12932 position. */
12933 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12934 last_unchanged_pos_old = last_unchanged_pos - *delta;
12935
12936 /* Search backward from ROW for a row displaying a line that
12937 starts at a minimum position >= last_unchanged_pos_old. */
12938 for (; row > first_text_row; --row)
12939 {
12940 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12941 abort ();
12942
12943 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12944 row_found = row;
12945 }
12946 }
12947
12948 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12949 abort ();
12950
12951 return row_found;
12952 }
12953
12954
12955 /* Make sure that glyph rows in the current matrix of window W
12956 reference the same glyph memory as corresponding rows in the
12957 frame's frame matrix. This function is called after scrolling W's
12958 current matrix on a terminal frame in try_window_id and
12959 try_window_reusing_current_matrix. */
12960
12961 static void
12962 sync_frame_with_window_matrix_rows (w)
12963 struct window *w;
12964 {
12965 struct frame *f = XFRAME (w->frame);
12966 struct glyph_row *window_row, *window_row_end, *frame_row;
12967
12968 /* Preconditions: W must be a leaf window and full-width. Its frame
12969 must have a frame matrix. */
12970 xassert (NILP (w->hchild) && NILP (w->vchild));
12971 xassert (WINDOW_FULL_WIDTH_P (w));
12972 xassert (!FRAME_WINDOW_P (f));
12973
12974 /* If W is a full-width window, glyph pointers in W's current matrix
12975 have, by definition, to be the same as glyph pointers in the
12976 corresponding frame matrix. Note that frame matrices have no
12977 marginal areas (see build_frame_matrix). */
12978 window_row = w->current_matrix->rows;
12979 window_row_end = window_row + w->current_matrix->nrows;
12980 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12981 while (window_row < window_row_end)
12982 {
12983 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12984 struct glyph *end = window_row->glyphs[LAST_AREA];
12985
12986 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12987 frame_row->glyphs[TEXT_AREA] = start;
12988 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12989 frame_row->glyphs[LAST_AREA] = end;
12990
12991 /* Disable frame rows whose corresponding window rows have
12992 been disabled in try_window_id. */
12993 if (!window_row->enabled_p)
12994 frame_row->enabled_p = 0;
12995
12996 ++window_row, ++frame_row;
12997 }
12998 }
12999
13000
13001 /* Find the glyph row in window W containing CHARPOS. Consider all
13002 rows between START and END (not inclusive). END null means search
13003 all rows to the end of the display area of W. Value is the row
13004 containing CHARPOS or null. */
13005
13006 struct glyph_row *
13007 row_containing_pos (w, charpos, start, end, dy)
13008 struct window *w;
13009 int charpos;
13010 struct glyph_row *start, *end;
13011 int dy;
13012 {
13013 struct glyph_row *row = start;
13014 int last_y;
13015
13016 /* If we happen to start on a header-line, skip that. */
13017 if (row->mode_line_p)
13018 ++row;
13019
13020 if ((end && row >= end) || !row->enabled_p)
13021 return NULL;
13022
13023 last_y = window_text_bottom_y (w) - dy;
13024
13025 while (1)
13026 {
13027 /* Give up if we have gone too far. */
13028 if (end && row >= end)
13029 return NULL;
13030 /* This formerly returned if they were equal.
13031 I think that both quantities are of a "last plus one" type;
13032 if so, when they are equal, the row is within the screen. -- rms. */
13033 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13034 return NULL;
13035
13036 /* If it is in this row, return this row. */
13037 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13038 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13039 /* The end position of a row equals the start
13040 position of the next row. If CHARPOS is there, we
13041 would rather display it in the next line, except
13042 when this line ends in ZV. */
13043 && !row->ends_at_zv_p
13044 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13045 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13046 return row;
13047 ++row;
13048 }
13049 }
13050
13051
13052 /* Try to redisplay window W by reusing its existing display. W's
13053 current matrix must be up to date when this function is called,
13054 i.e. window_end_valid must not be nil.
13055
13056 Value is
13057
13058 1 if display has been updated
13059 0 if otherwise unsuccessful
13060 -1 if redisplay with same window start is known not to succeed
13061
13062 The following steps are performed:
13063
13064 1. Find the last row in the current matrix of W that is not
13065 affected by changes at the start of current_buffer. If no such row
13066 is found, give up.
13067
13068 2. Find the first row in W's current matrix that is not affected by
13069 changes at the end of current_buffer. Maybe there is no such row.
13070
13071 3. Display lines beginning with the row + 1 found in step 1 to the
13072 row found in step 2 or, if step 2 didn't find a row, to the end of
13073 the window.
13074
13075 4. If cursor is not known to appear on the window, give up.
13076
13077 5. If display stopped at the row found in step 2, scroll the
13078 display and current matrix as needed.
13079
13080 6. Maybe display some lines at the end of W, if we must. This can
13081 happen under various circumstances, like a partially visible line
13082 becoming fully visible, or because newly displayed lines are displayed
13083 in smaller font sizes.
13084
13085 7. Update W's window end information. */
13086
13087 static int
13088 try_window_id (w)
13089 struct window *w;
13090 {
13091 struct frame *f = XFRAME (w->frame);
13092 struct glyph_matrix *current_matrix = w->current_matrix;
13093 struct glyph_matrix *desired_matrix = w->desired_matrix;
13094 struct glyph_row *last_unchanged_at_beg_row;
13095 struct glyph_row *first_unchanged_at_end_row;
13096 struct glyph_row *row;
13097 struct glyph_row *bottom_row;
13098 int bottom_vpos;
13099 struct it it;
13100 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13101 struct text_pos start_pos;
13102 struct run run;
13103 int first_unchanged_at_end_vpos = 0;
13104 struct glyph_row *last_text_row, *last_text_row_at_end;
13105 struct text_pos start;
13106 int first_changed_charpos, last_changed_charpos;
13107
13108 #if GLYPH_DEBUG
13109 if (inhibit_try_window_id)
13110 return 0;
13111 #endif
13112
13113 /* This is handy for debugging. */
13114 #if 0
13115 #define GIVE_UP(X) \
13116 do { \
13117 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13118 return 0; \
13119 } while (0)
13120 #else
13121 #define GIVE_UP(X) return 0
13122 #endif
13123
13124 SET_TEXT_POS_FROM_MARKER (start, w->start);
13125
13126 /* Don't use this for mini-windows because these can show
13127 messages and mini-buffers, and we don't handle that here. */
13128 if (MINI_WINDOW_P (w))
13129 GIVE_UP (1);
13130
13131 /* This flag is used to prevent redisplay optimizations. */
13132 if (windows_or_buffers_changed || cursor_type_changed)
13133 GIVE_UP (2);
13134
13135 /* Verify that narrowing has not changed.
13136 Also verify that we were not told to prevent redisplay optimizations.
13137 It would be nice to further
13138 reduce the number of cases where this prevents try_window_id. */
13139 if (current_buffer->clip_changed
13140 || current_buffer->prevent_redisplay_optimizations_p)
13141 GIVE_UP (3);
13142
13143 /* Window must either use window-based redisplay or be full width. */
13144 if (!FRAME_WINDOW_P (f)
13145 && (!FRAME_LINE_INS_DEL_OK (f)
13146 || !WINDOW_FULL_WIDTH_P (w)))
13147 GIVE_UP (4);
13148
13149 /* Give up if point is not known NOT to appear in W. */
13150 if (PT < CHARPOS (start))
13151 GIVE_UP (5);
13152
13153 /* Another way to prevent redisplay optimizations. */
13154 if (XFASTINT (w->last_modified) == 0)
13155 GIVE_UP (6);
13156
13157 /* Verify that window is not hscrolled. */
13158 if (XFASTINT (w->hscroll) != 0)
13159 GIVE_UP (7);
13160
13161 /* Verify that display wasn't paused. */
13162 if (NILP (w->window_end_valid))
13163 GIVE_UP (8);
13164
13165 /* Can't use this if highlighting a region because a cursor movement
13166 will do more than just set the cursor. */
13167 if (!NILP (Vtransient_mark_mode)
13168 && !NILP (current_buffer->mark_active))
13169 GIVE_UP (9);
13170
13171 /* Likewise if highlighting trailing whitespace. */
13172 if (!NILP (Vshow_trailing_whitespace))
13173 GIVE_UP (11);
13174
13175 /* Likewise if showing a region. */
13176 if (!NILP (w->region_showing))
13177 GIVE_UP (10);
13178
13179 /* Can use this if overlay arrow position and or string have changed. */
13180 if (overlay_arrows_changed_p ())
13181 GIVE_UP (12);
13182
13183
13184 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13185 only if buffer has really changed. The reason is that the gap is
13186 initially at Z for freshly visited files. The code below would
13187 set end_unchanged to 0 in that case. */
13188 if (MODIFF > SAVE_MODIFF
13189 /* This seems to happen sometimes after saving a buffer. */
13190 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13191 {
13192 if (GPT - BEG < BEG_UNCHANGED)
13193 BEG_UNCHANGED = GPT - BEG;
13194 if (Z - GPT < END_UNCHANGED)
13195 END_UNCHANGED = Z - GPT;
13196 }
13197
13198 /* The position of the first and last character that has been changed. */
13199 first_changed_charpos = BEG + BEG_UNCHANGED;
13200 last_changed_charpos = Z - END_UNCHANGED;
13201
13202 /* If window starts after a line end, and the last change is in
13203 front of that newline, then changes don't affect the display.
13204 This case happens with stealth-fontification. Note that although
13205 the display is unchanged, glyph positions in the matrix have to
13206 be adjusted, of course. */
13207 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13208 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13209 && ((last_changed_charpos < CHARPOS (start)
13210 && CHARPOS (start) == BEGV)
13211 || (last_changed_charpos < CHARPOS (start) - 1
13212 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13213 {
13214 int Z_old, delta, Z_BYTE_old, delta_bytes;
13215 struct glyph_row *r0;
13216
13217 /* Compute how many chars/bytes have been added to or removed
13218 from the buffer. */
13219 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13220 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13221 delta = Z - Z_old;
13222 delta_bytes = Z_BYTE - Z_BYTE_old;
13223
13224 /* Give up if PT is not in the window. Note that it already has
13225 been checked at the start of try_window_id that PT is not in
13226 front of the window start. */
13227 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13228 GIVE_UP (13);
13229
13230 /* If window start is unchanged, we can reuse the whole matrix
13231 as is, after adjusting glyph positions. No need to compute
13232 the window end again, since its offset from Z hasn't changed. */
13233 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13234 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13235 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13236 /* PT must not be in a partially visible line. */
13237 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13238 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13239 {
13240 /* Adjust positions in the glyph matrix. */
13241 if (delta || delta_bytes)
13242 {
13243 struct glyph_row *r1
13244 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13245 increment_matrix_positions (w->current_matrix,
13246 MATRIX_ROW_VPOS (r0, current_matrix),
13247 MATRIX_ROW_VPOS (r1, current_matrix),
13248 delta, delta_bytes);
13249 }
13250
13251 /* Set the cursor. */
13252 row = row_containing_pos (w, PT, r0, NULL, 0);
13253 if (row)
13254 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13255 else
13256 abort ();
13257 return 1;
13258 }
13259 }
13260
13261 /* Handle the case that changes are all below what is displayed in
13262 the window, and that PT is in the window. This shortcut cannot
13263 be taken if ZV is visible in the window, and text has been added
13264 there that is visible in the window. */
13265 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13266 /* ZV is not visible in the window, or there are no
13267 changes at ZV, actually. */
13268 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13269 || first_changed_charpos == last_changed_charpos))
13270 {
13271 struct glyph_row *r0;
13272
13273 /* Give up if PT is not in the window. Note that it already has
13274 been checked at the start of try_window_id that PT is not in
13275 front of the window start. */
13276 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13277 GIVE_UP (14);
13278
13279 /* If window start is unchanged, we can reuse the whole matrix
13280 as is, without changing glyph positions since no text has
13281 been added/removed in front of the window end. */
13282 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13283 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13284 /* PT must not be in a partially visible line. */
13285 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13286 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13287 {
13288 /* We have to compute the window end anew since text
13289 can have been added/removed after it. */
13290 w->window_end_pos
13291 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13292 w->window_end_bytepos
13293 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13294
13295 /* Set the cursor. */
13296 row = row_containing_pos (w, PT, r0, NULL, 0);
13297 if (row)
13298 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13299 else
13300 abort ();
13301 return 2;
13302 }
13303 }
13304
13305 /* Give up if window start is in the changed area.
13306
13307 The condition used to read
13308
13309 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13310
13311 but why that was tested escapes me at the moment. */
13312 if (CHARPOS (start) >= first_changed_charpos
13313 && CHARPOS (start) <= last_changed_charpos)
13314 GIVE_UP (15);
13315
13316 /* Check that window start agrees with the start of the first glyph
13317 row in its current matrix. Check this after we know the window
13318 start is not in changed text, otherwise positions would not be
13319 comparable. */
13320 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13321 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13322 GIVE_UP (16);
13323
13324 /* Give up if the window ends in strings. Overlay strings
13325 at the end are difficult to handle, so don't try. */
13326 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13327 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13328 GIVE_UP (20);
13329
13330 /* Compute the position at which we have to start displaying new
13331 lines. Some of the lines at the top of the window might be
13332 reusable because they are not displaying changed text. Find the
13333 last row in W's current matrix not affected by changes at the
13334 start of current_buffer. Value is null if changes start in the
13335 first line of window. */
13336 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13337 if (last_unchanged_at_beg_row)
13338 {
13339 /* Avoid starting to display in the moddle of a character, a TAB
13340 for instance. This is easier than to set up the iterator
13341 exactly, and it's not a frequent case, so the additional
13342 effort wouldn't really pay off. */
13343 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13344 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13345 && last_unchanged_at_beg_row > w->current_matrix->rows)
13346 --last_unchanged_at_beg_row;
13347
13348 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13349 GIVE_UP (17);
13350
13351 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13352 GIVE_UP (18);
13353 start_pos = it.current.pos;
13354
13355 /* Start displaying new lines in the desired matrix at the same
13356 vpos we would use in the current matrix, i.e. below
13357 last_unchanged_at_beg_row. */
13358 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13359 current_matrix);
13360 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13361 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13362
13363 xassert (it.hpos == 0 && it.current_x == 0);
13364 }
13365 else
13366 {
13367 /* There are no reusable lines at the start of the window.
13368 Start displaying in the first text line. */
13369 start_display (&it, w, start);
13370 it.vpos = it.first_vpos;
13371 start_pos = it.current.pos;
13372 }
13373
13374 /* Find the first row that is not affected by changes at the end of
13375 the buffer. Value will be null if there is no unchanged row, in
13376 which case we must redisplay to the end of the window. delta
13377 will be set to the value by which buffer positions beginning with
13378 first_unchanged_at_end_row have to be adjusted due to text
13379 changes. */
13380 first_unchanged_at_end_row
13381 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13382 IF_DEBUG (debug_delta = delta);
13383 IF_DEBUG (debug_delta_bytes = delta_bytes);
13384
13385 /* Set stop_pos to the buffer position up to which we will have to
13386 display new lines. If first_unchanged_at_end_row != NULL, this
13387 is the buffer position of the start of the line displayed in that
13388 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13389 that we don't stop at a buffer position. */
13390 stop_pos = 0;
13391 if (first_unchanged_at_end_row)
13392 {
13393 xassert (last_unchanged_at_beg_row == NULL
13394 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13395
13396 /* If this is a continuation line, move forward to the next one
13397 that isn't. Changes in lines above affect this line.
13398 Caution: this may move first_unchanged_at_end_row to a row
13399 not displaying text. */
13400 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13401 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13402 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13403 < it.last_visible_y))
13404 ++first_unchanged_at_end_row;
13405
13406 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13407 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13408 >= it.last_visible_y))
13409 first_unchanged_at_end_row = NULL;
13410 else
13411 {
13412 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13413 + delta);
13414 first_unchanged_at_end_vpos
13415 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13416 xassert (stop_pos >= Z - END_UNCHANGED);
13417 }
13418 }
13419 else if (last_unchanged_at_beg_row == NULL)
13420 GIVE_UP (19);
13421
13422
13423 #if GLYPH_DEBUG
13424
13425 /* Either there is no unchanged row at the end, or the one we have
13426 now displays text. This is a necessary condition for the window
13427 end pos calculation at the end of this function. */
13428 xassert (first_unchanged_at_end_row == NULL
13429 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13430
13431 debug_last_unchanged_at_beg_vpos
13432 = (last_unchanged_at_beg_row
13433 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13434 : -1);
13435 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13436
13437 #endif /* GLYPH_DEBUG != 0 */
13438
13439
13440 /* Display new lines. Set last_text_row to the last new line
13441 displayed which has text on it, i.e. might end up as being the
13442 line where the window_end_vpos is. */
13443 w->cursor.vpos = -1;
13444 last_text_row = NULL;
13445 overlay_arrow_seen = 0;
13446 while (it.current_y < it.last_visible_y
13447 && !fonts_changed_p
13448 && (first_unchanged_at_end_row == NULL
13449 || IT_CHARPOS (it) < stop_pos))
13450 {
13451 if (display_line (&it))
13452 last_text_row = it.glyph_row - 1;
13453 }
13454
13455 if (fonts_changed_p)
13456 return -1;
13457
13458
13459 /* Compute differences in buffer positions, y-positions etc. for
13460 lines reused at the bottom of the window. Compute what we can
13461 scroll. */
13462 if (first_unchanged_at_end_row
13463 /* No lines reused because we displayed everything up to the
13464 bottom of the window. */
13465 && it.current_y < it.last_visible_y)
13466 {
13467 dvpos = (it.vpos
13468 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13469 current_matrix));
13470 dy = it.current_y - first_unchanged_at_end_row->y;
13471 run.current_y = first_unchanged_at_end_row->y;
13472 run.desired_y = run.current_y + dy;
13473 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13474 }
13475 else
13476 {
13477 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13478 first_unchanged_at_end_row = NULL;
13479 }
13480 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13481
13482
13483 /* Find the cursor if not already found. We have to decide whether
13484 PT will appear on this window (it sometimes doesn't, but this is
13485 not a very frequent case.) This decision has to be made before
13486 the current matrix is altered. A value of cursor.vpos < 0 means
13487 that PT is either in one of the lines beginning at
13488 first_unchanged_at_end_row or below the window. Don't care for
13489 lines that might be displayed later at the window end; as
13490 mentioned, this is not a frequent case. */
13491 if (w->cursor.vpos < 0)
13492 {
13493 /* Cursor in unchanged rows at the top? */
13494 if (PT < CHARPOS (start_pos)
13495 && last_unchanged_at_beg_row)
13496 {
13497 row = row_containing_pos (w, PT,
13498 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13499 last_unchanged_at_beg_row + 1, 0);
13500 if (row)
13501 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13502 }
13503
13504 /* Start from first_unchanged_at_end_row looking for PT. */
13505 else if (first_unchanged_at_end_row)
13506 {
13507 row = row_containing_pos (w, PT - delta,
13508 first_unchanged_at_end_row, NULL, 0);
13509 if (row)
13510 set_cursor_from_row (w, row, w->current_matrix, delta,
13511 delta_bytes, dy, dvpos);
13512 }
13513
13514 /* Give up if cursor was not found. */
13515 if (w->cursor.vpos < 0)
13516 {
13517 clear_glyph_matrix (w->desired_matrix);
13518 return -1;
13519 }
13520 }
13521
13522 /* Don't let the cursor end in the scroll margins. */
13523 {
13524 int this_scroll_margin, cursor_height;
13525
13526 this_scroll_margin = max (0, scroll_margin);
13527 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13528 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13529 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13530
13531 if ((w->cursor.y < this_scroll_margin
13532 && CHARPOS (start) > BEGV)
13533 /* Old redisplay didn't take scroll margin into account at the bottom,
13534 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13535 || (w->cursor.y + (make_cursor_line_fully_visible_p
13536 ? cursor_height + this_scroll_margin
13537 : 1)) > it.last_visible_y)
13538 {
13539 w->cursor.vpos = -1;
13540 clear_glyph_matrix (w->desired_matrix);
13541 return -1;
13542 }
13543 }
13544
13545 /* Scroll the display. Do it before changing the current matrix so
13546 that xterm.c doesn't get confused about where the cursor glyph is
13547 found. */
13548 if (dy && run.height)
13549 {
13550 update_begin (f);
13551
13552 if (FRAME_WINDOW_P (f))
13553 {
13554 FRAME_RIF (f)->update_window_begin_hook (w);
13555 FRAME_RIF (f)->clear_window_mouse_face (w);
13556 FRAME_RIF (f)->scroll_run_hook (w, &run);
13557 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13558 }
13559 else
13560 {
13561 /* Terminal frame. In this case, dvpos gives the number of
13562 lines to scroll by; dvpos < 0 means scroll up. */
13563 int first_unchanged_at_end_vpos
13564 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13565 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13566 int end = (WINDOW_TOP_EDGE_LINE (w)
13567 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13568 + window_internal_height (w));
13569
13570 /* Perform the operation on the screen. */
13571 if (dvpos > 0)
13572 {
13573 /* Scroll last_unchanged_at_beg_row to the end of the
13574 window down dvpos lines. */
13575 set_terminal_window (f, end);
13576
13577 /* On dumb terminals delete dvpos lines at the end
13578 before inserting dvpos empty lines. */
13579 if (!FRAME_SCROLL_REGION_OK (f))
13580 ins_del_lines (f, end - dvpos, -dvpos);
13581
13582 /* Insert dvpos empty lines in front of
13583 last_unchanged_at_beg_row. */
13584 ins_del_lines (f, from, dvpos);
13585 }
13586 else if (dvpos < 0)
13587 {
13588 /* Scroll up last_unchanged_at_beg_vpos to the end of
13589 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13590 set_terminal_window (f, end);
13591
13592 /* Delete dvpos lines in front of
13593 last_unchanged_at_beg_vpos. ins_del_lines will set
13594 the cursor to the given vpos and emit |dvpos| delete
13595 line sequences. */
13596 ins_del_lines (f, from + dvpos, dvpos);
13597
13598 /* On a dumb terminal insert dvpos empty lines at the
13599 end. */
13600 if (!FRAME_SCROLL_REGION_OK (f))
13601 ins_del_lines (f, end + dvpos, -dvpos);
13602 }
13603
13604 set_terminal_window (f, 0);
13605 }
13606
13607 update_end (f);
13608 }
13609
13610 /* Shift reused rows of the current matrix to the right position.
13611 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13612 text. */
13613 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13614 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13615 if (dvpos < 0)
13616 {
13617 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13618 bottom_vpos, dvpos);
13619 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13620 bottom_vpos, 0);
13621 }
13622 else if (dvpos > 0)
13623 {
13624 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13625 bottom_vpos, dvpos);
13626 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13627 first_unchanged_at_end_vpos + dvpos, 0);
13628 }
13629
13630 /* For frame-based redisplay, make sure that current frame and window
13631 matrix are in sync with respect to glyph memory. */
13632 if (!FRAME_WINDOW_P (f))
13633 sync_frame_with_window_matrix_rows (w);
13634
13635 /* Adjust buffer positions in reused rows. */
13636 if (delta)
13637 increment_matrix_positions (current_matrix,
13638 first_unchanged_at_end_vpos + dvpos,
13639 bottom_vpos, delta, delta_bytes);
13640
13641 /* Adjust Y positions. */
13642 if (dy)
13643 shift_glyph_matrix (w, current_matrix,
13644 first_unchanged_at_end_vpos + dvpos,
13645 bottom_vpos, dy);
13646
13647 if (first_unchanged_at_end_row)
13648 first_unchanged_at_end_row += dvpos;
13649
13650 /* If scrolling up, there may be some lines to display at the end of
13651 the window. */
13652 last_text_row_at_end = NULL;
13653 if (dy < 0)
13654 {
13655 /* Scrolling up can leave for example a partially visible line
13656 at the end of the window to be redisplayed. */
13657 /* Set last_row to the glyph row in the current matrix where the
13658 window end line is found. It has been moved up or down in
13659 the matrix by dvpos. */
13660 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13661 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13662
13663 /* If last_row is the window end line, it should display text. */
13664 xassert (last_row->displays_text_p);
13665
13666 /* If window end line was partially visible before, begin
13667 displaying at that line. Otherwise begin displaying with the
13668 line following it. */
13669 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13670 {
13671 init_to_row_start (&it, w, last_row);
13672 it.vpos = last_vpos;
13673 it.current_y = last_row->y;
13674 }
13675 else
13676 {
13677 init_to_row_end (&it, w, last_row);
13678 it.vpos = 1 + last_vpos;
13679 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13680 ++last_row;
13681 }
13682
13683 /* We may start in a continuation line. If so, we have to
13684 get the right continuation_lines_width and current_x. */
13685 it.continuation_lines_width = last_row->continuation_lines_width;
13686 it.hpos = it.current_x = 0;
13687
13688 /* Display the rest of the lines at the window end. */
13689 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13690 while (it.current_y < it.last_visible_y
13691 && !fonts_changed_p)
13692 {
13693 /* Is it always sure that the display agrees with lines in
13694 the current matrix? I don't think so, so we mark rows
13695 displayed invalid in the current matrix by setting their
13696 enabled_p flag to zero. */
13697 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13698 if (display_line (&it))
13699 last_text_row_at_end = it.glyph_row - 1;
13700 }
13701 }
13702
13703 /* Update window_end_pos and window_end_vpos. */
13704 if (first_unchanged_at_end_row
13705 && first_unchanged_at_end_row->y < it.last_visible_y
13706 && !last_text_row_at_end)
13707 {
13708 /* Window end line if one of the preserved rows from the current
13709 matrix. Set row to the last row displaying text in current
13710 matrix starting at first_unchanged_at_end_row, after
13711 scrolling. */
13712 xassert (first_unchanged_at_end_row->displays_text_p);
13713 row = find_last_row_displaying_text (w->current_matrix, &it,
13714 first_unchanged_at_end_row);
13715 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13716
13717 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13718 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13719 w->window_end_vpos
13720 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13721 xassert (w->window_end_bytepos >= 0);
13722 IF_DEBUG (debug_method_add (w, "A"));
13723 }
13724 else if (last_text_row_at_end)
13725 {
13726 w->window_end_pos
13727 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13728 w->window_end_bytepos
13729 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13730 w->window_end_vpos
13731 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13732 xassert (w->window_end_bytepos >= 0);
13733 IF_DEBUG (debug_method_add (w, "B"));
13734 }
13735 else if (last_text_row)
13736 {
13737 /* We have displayed either to the end of the window or at the
13738 end of the window, i.e. the last row with text is to be found
13739 in the desired matrix. */
13740 w->window_end_pos
13741 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13742 w->window_end_bytepos
13743 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13744 w->window_end_vpos
13745 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13746 xassert (w->window_end_bytepos >= 0);
13747 }
13748 else if (first_unchanged_at_end_row == NULL
13749 && last_text_row == NULL
13750 && last_text_row_at_end == NULL)
13751 {
13752 /* Displayed to end of window, but no line containing text was
13753 displayed. Lines were deleted at the end of the window. */
13754 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13755 int vpos = XFASTINT (w->window_end_vpos);
13756 struct glyph_row *current_row = current_matrix->rows + vpos;
13757 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13758
13759 for (row = NULL;
13760 row == NULL && vpos >= first_vpos;
13761 --vpos, --current_row, --desired_row)
13762 {
13763 if (desired_row->enabled_p)
13764 {
13765 if (desired_row->displays_text_p)
13766 row = desired_row;
13767 }
13768 else if (current_row->displays_text_p)
13769 row = current_row;
13770 }
13771
13772 xassert (row != NULL);
13773 w->window_end_vpos = make_number (vpos + 1);
13774 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13775 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13776 xassert (w->window_end_bytepos >= 0);
13777 IF_DEBUG (debug_method_add (w, "C"));
13778 }
13779 else
13780 abort ();
13781
13782 #if 0 /* This leads to problems, for instance when the cursor is
13783 at ZV, and the cursor line displays no text. */
13784 /* Disable rows below what's displayed in the window. This makes
13785 debugging easier. */
13786 enable_glyph_matrix_rows (current_matrix,
13787 XFASTINT (w->window_end_vpos) + 1,
13788 bottom_vpos, 0);
13789 #endif
13790
13791 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13792 debug_end_vpos = XFASTINT (w->window_end_vpos));
13793
13794 /* Record that display has not been completed. */
13795 w->window_end_valid = Qnil;
13796 w->desired_matrix->no_scrolling_p = 1;
13797 return 3;
13798
13799 #undef GIVE_UP
13800 }
13801
13802
13803 \f
13804 /***********************************************************************
13805 More debugging support
13806 ***********************************************************************/
13807
13808 #if GLYPH_DEBUG
13809
13810 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13811 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13812 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13813
13814
13815 /* Dump the contents of glyph matrix MATRIX on stderr.
13816
13817 GLYPHS 0 means don't show glyph contents.
13818 GLYPHS 1 means show glyphs in short form
13819 GLYPHS > 1 means show glyphs in long form. */
13820
13821 void
13822 dump_glyph_matrix (matrix, glyphs)
13823 struct glyph_matrix *matrix;
13824 int glyphs;
13825 {
13826 int i;
13827 for (i = 0; i < matrix->nrows; ++i)
13828 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13829 }
13830
13831
13832 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13833 the glyph row and area where the glyph comes from. */
13834
13835 void
13836 dump_glyph (row, glyph, area)
13837 struct glyph_row *row;
13838 struct glyph *glyph;
13839 int area;
13840 {
13841 if (glyph->type == CHAR_GLYPH)
13842 {
13843 fprintf (stderr,
13844 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13845 glyph - row->glyphs[TEXT_AREA],
13846 'C',
13847 glyph->charpos,
13848 (BUFFERP (glyph->object)
13849 ? 'B'
13850 : (STRINGP (glyph->object)
13851 ? 'S'
13852 : '-')),
13853 glyph->pixel_width,
13854 glyph->u.ch,
13855 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13856 ? glyph->u.ch
13857 : '.'),
13858 glyph->face_id,
13859 glyph->left_box_line_p,
13860 glyph->right_box_line_p);
13861 }
13862 else if (glyph->type == STRETCH_GLYPH)
13863 {
13864 fprintf (stderr,
13865 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13866 glyph - row->glyphs[TEXT_AREA],
13867 'S',
13868 glyph->charpos,
13869 (BUFFERP (glyph->object)
13870 ? 'B'
13871 : (STRINGP (glyph->object)
13872 ? 'S'
13873 : '-')),
13874 glyph->pixel_width,
13875 0,
13876 '.',
13877 glyph->face_id,
13878 glyph->left_box_line_p,
13879 glyph->right_box_line_p);
13880 }
13881 else if (glyph->type == IMAGE_GLYPH)
13882 {
13883 fprintf (stderr,
13884 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13885 glyph - row->glyphs[TEXT_AREA],
13886 'I',
13887 glyph->charpos,
13888 (BUFFERP (glyph->object)
13889 ? 'B'
13890 : (STRINGP (glyph->object)
13891 ? 'S'
13892 : '-')),
13893 glyph->pixel_width,
13894 glyph->u.img_id,
13895 '.',
13896 glyph->face_id,
13897 glyph->left_box_line_p,
13898 glyph->right_box_line_p);
13899 }
13900 }
13901
13902
13903 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13904 GLYPHS 0 means don't show glyph contents.
13905 GLYPHS 1 means show glyphs in short form
13906 GLYPHS > 1 means show glyphs in long form. */
13907
13908 void
13909 dump_glyph_row (row, vpos, glyphs)
13910 struct glyph_row *row;
13911 int vpos, glyphs;
13912 {
13913 if (glyphs != 1)
13914 {
13915 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13916 fprintf (stderr, "=======================================================================\n");
13917
13918 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13919 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13920 vpos,
13921 MATRIX_ROW_START_CHARPOS (row),
13922 MATRIX_ROW_END_CHARPOS (row),
13923 row->used[TEXT_AREA],
13924 row->contains_overlapping_glyphs_p,
13925 row->enabled_p,
13926 row->truncated_on_left_p,
13927 row->truncated_on_right_p,
13928 row->overlay_arrow_p,
13929 row->continued_p,
13930 MATRIX_ROW_CONTINUATION_LINE_P (row),
13931 row->displays_text_p,
13932 row->ends_at_zv_p,
13933 row->fill_line_p,
13934 row->ends_in_middle_of_char_p,
13935 row->starts_in_middle_of_char_p,
13936 row->mouse_face_p,
13937 row->x,
13938 row->y,
13939 row->pixel_width,
13940 row->height,
13941 row->visible_height,
13942 row->ascent,
13943 row->phys_ascent);
13944 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13945 row->end.overlay_string_index,
13946 row->continuation_lines_width);
13947 fprintf (stderr, "%9d %5d\n",
13948 CHARPOS (row->start.string_pos),
13949 CHARPOS (row->end.string_pos));
13950 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13951 row->end.dpvec_index);
13952 }
13953
13954 if (glyphs > 1)
13955 {
13956 int area;
13957
13958 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13959 {
13960 struct glyph *glyph = row->glyphs[area];
13961 struct glyph *glyph_end = glyph + row->used[area];
13962
13963 /* Glyph for a line end in text. */
13964 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13965 ++glyph_end;
13966
13967 if (glyph < glyph_end)
13968 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13969
13970 for (; glyph < glyph_end; ++glyph)
13971 dump_glyph (row, glyph, area);
13972 }
13973 }
13974 else if (glyphs == 1)
13975 {
13976 int area;
13977
13978 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13979 {
13980 char *s = (char *) alloca (row->used[area] + 1);
13981 int i;
13982
13983 for (i = 0; i < row->used[area]; ++i)
13984 {
13985 struct glyph *glyph = row->glyphs[area] + i;
13986 if (glyph->type == CHAR_GLYPH
13987 && glyph->u.ch < 0x80
13988 && glyph->u.ch >= ' ')
13989 s[i] = glyph->u.ch;
13990 else
13991 s[i] = '.';
13992 }
13993
13994 s[i] = '\0';
13995 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13996 }
13997 }
13998 }
13999
14000
14001 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14002 Sdump_glyph_matrix, 0, 1, "p",
14003 doc: /* Dump the current matrix of the selected window to stderr.
14004 Shows contents of glyph row structures. With non-nil
14005 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14006 glyphs in short form, otherwise show glyphs in long form. */)
14007 (glyphs)
14008 Lisp_Object glyphs;
14009 {
14010 struct window *w = XWINDOW (selected_window);
14011 struct buffer *buffer = XBUFFER (w->buffer);
14012
14013 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14014 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14015 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14016 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14017 fprintf (stderr, "=============================================\n");
14018 dump_glyph_matrix (w->current_matrix,
14019 NILP (glyphs) ? 0 : XINT (glyphs));
14020 return Qnil;
14021 }
14022
14023
14024 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14025 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14026 ()
14027 {
14028 struct frame *f = XFRAME (selected_frame);
14029 dump_glyph_matrix (f->current_matrix, 1);
14030 return Qnil;
14031 }
14032
14033
14034 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14035 doc: /* Dump glyph row ROW to stderr.
14036 GLYPH 0 means don't dump glyphs.
14037 GLYPH 1 means dump glyphs in short form.
14038 GLYPH > 1 or omitted means dump glyphs in long form. */)
14039 (row, glyphs)
14040 Lisp_Object row, glyphs;
14041 {
14042 struct glyph_matrix *matrix;
14043 int vpos;
14044
14045 CHECK_NUMBER (row);
14046 matrix = XWINDOW (selected_window)->current_matrix;
14047 vpos = XINT (row);
14048 if (vpos >= 0 && vpos < matrix->nrows)
14049 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14050 vpos,
14051 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14052 return Qnil;
14053 }
14054
14055
14056 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14057 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14058 GLYPH 0 means don't dump glyphs.
14059 GLYPH 1 means dump glyphs in short form.
14060 GLYPH > 1 or omitted means dump glyphs in long form. */)
14061 (row, glyphs)
14062 Lisp_Object row, glyphs;
14063 {
14064 struct frame *sf = SELECTED_FRAME ();
14065 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14066 int vpos;
14067
14068 CHECK_NUMBER (row);
14069 vpos = XINT (row);
14070 if (vpos >= 0 && vpos < m->nrows)
14071 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14072 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14073 return Qnil;
14074 }
14075
14076
14077 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14078 doc: /* Toggle tracing of redisplay.
14079 With ARG, turn tracing on if and only if ARG is positive. */)
14080 (arg)
14081 Lisp_Object arg;
14082 {
14083 if (NILP (arg))
14084 trace_redisplay_p = !trace_redisplay_p;
14085 else
14086 {
14087 arg = Fprefix_numeric_value (arg);
14088 trace_redisplay_p = XINT (arg) > 0;
14089 }
14090
14091 return Qnil;
14092 }
14093
14094
14095 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14096 doc: /* Like `format', but print result to stderr.
14097 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14098 (nargs, args)
14099 int nargs;
14100 Lisp_Object *args;
14101 {
14102 Lisp_Object s = Fformat (nargs, args);
14103 fprintf (stderr, "%s", SDATA (s));
14104 return Qnil;
14105 }
14106
14107 #endif /* GLYPH_DEBUG */
14108
14109
14110 \f
14111 /***********************************************************************
14112 Building Desired Matrix Rows
14113 ***********************************************************************/
14114
14115 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14116 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14117
14118 static struct glyph_row *
14119 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14120 struct window *w;
14121 Lisp_Object overlay_arrow_string;
14122 {
14123 struct frame *f = XFRAME (WINDOW_FRAME (w));
14124 struct buffer *buffer = XBUFFER (w->buffer);
14125 struct buffer *old = current_buffer;
14126 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14127 int arrow_len = SCHARS (overlay_arrow_string);
14128 const unsigned char *arrow_end = arrow_string + arrow_len;
14129 const unsigned char *p;
14130 struct it it;
14131 int multibyte_p;
14132 int n_glyphs_before;
14133
14134 set_buffer_temp (buffer);
14135 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14136 it.glyph_row->used[TEXT_AREA] = 0;
14137 SET_TEXT_POS (it.position, 0, 0);
14138
14139 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14140 p = arrow_string;
14141 while (p < arrow_end)
14142 {
14143 Lisp_Object face, ilisp;
14144
14145 /* Get the next character. */
14146 if (multibyte_p)
14147 it.c = string_char_and_length (p, arrow_len, &it.len);
14148 else
14149 it.c = *p, it.len = 1;
14150 p += it.len;
14151
14152 /* Get its face. */
14153 ilisp = make_number (p - arrow_string);
14154 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14155 it.face_id = compute_char_face (f, it.c, face);
14156
14157 /* Compute its width, get its glyphs. */
14158 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14159 SET_TEXT_POS (it.position, -1, -1);
14160 PRODUCE_GLYPHS (&it);
14161
14162 /* If this character doesn't fit any more in the line, we have
14163 to remove some glyphs. */
14164 if (it.current_x > it.last_visible_x)
14165 {
14166 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14167 break;
14168 }
14169 }
14170
14171 set_buffer_temp (old);
14172 return it.glyph_row;
14173 }
14174
14175
14176 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14177 glyphs are only inserted for terminal frames since we can't really
14178 win with truncation glyphs when partially visible glyphs are
14179 involved. Which glyphs to insert is determined by
14180 produce_special_glyphs. */
14181
14182 static void
14183 insert_left_trunc_glyphs (it)
14184 struct it *it;
14185 {
14186 struct it truncate_it;
14187 struct glyph *from, *end, *to, *toend;
14188
14189 xassert (!FRAME_WINDOW_P (it->f));
14190
14191 /* Get the truncation glyphs. */
14192 truncate_it = *it;
14193 truncate_it.current_x = 0;
14194 truncate_it.face_id = DEFAULT_FACE_ID;
14195 truncate_it.glyph_row = &scratch_glyph_row;
14196 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14197 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14198 truncate_it.object = make_number (0);
14199 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14200
14201 /* Overwrite glyphs from IT with truncation glyphs. */
14202 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14203 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14204 to = it->glyph_row->glyphs[TEXT_AREA];
14205 toend = to + it->glyph_row->used[TEXT_AREA];
14206
14207 while (from < end)
14208 *to++ = *from++;
14209
14210 /* There may be padding glyphs left over. Overwrite them too. */
14211 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14212 {
14213 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14214 while (from < end)
14215 *to++ = *from++;
14216 }
14217
14218 if (to > toend)
14219 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14220 }
14221
14222
14223 /* Compute the pixel height and width of IT->glyph_row.
14224
14225 Most of the time, ascent and height of a display line will be equal
14226 to the max_ascent and max_height values of the display iterator
14227 structure. This is not the case if
14228
14229 1. We hit ZV without displaying anything. In this case, max_ascent
14230 and max_height will be zero.
14231
14232 2. We have some glyphs that don't contribute to the line height.
14233 (The glyph row flag contributes_to_line_height_p is for future
14234 pixmap extensions).
14235
14236 The first case is easily covered by using default values because in
14237 these cases, the line height does not really matter, except that it
14238 must not be zero. */
14239
14240 static void
14241 compute_line_metrics (it)
14242 struct it *it;
14243 {
14244 struct glyph_row *row = it->glyph_row;
14245 int area, i;
14246
14247 if (FRAME_WINDOW_P (it->f))
14248 {
14249 int i, min_y, max_y;
14250
14251 /* The line may consist of one space only, that was added to
14252 place the cursor on it. If so, the row's height hasn't been
14253 computed yet. */
14254 if (row->height == 0)
14255 {
14256 if (it->max_ascent + it->max_descent == 0)
14257 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14258 row->ascent = it->max_ascent;
14259 row->height = it->max_ascent + it->max_descent;
14260 row->phys_ascent = it->max_phys_ascent;
14261 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14262 row->extra_line_spacing = it->max_extra_line_spacing;
14263 }
14264
14265 /* Compute the width of this line. */
14266 row->pixel_width = row->x;
14267 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14268 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14269
14270 xassert (row->pixel_width >= 0);
14271 xassert (row->ascent >= 0 && row->height > 0);
14272
14273 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14274 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14275
14276 /* If first line's physical ascent is larger than its logical
14277 ascent, use the physical ascent, and make the row taller.
14278 This makes accented characters fully visible. */
14279 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14280 && row->phys_ascent > row->ascent)
14281 {
14282 row->height += row->phys_ascent - row->ascent;
14283 row->ascent = row->phys_ascent;
14284 }
14285
14286 /* Compute how much of the line is visible. */
14287 row->visible_height = row->height;
14288
14289 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14290 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14291
14292 if (row->y < min_y)
14293 row->visible_height -= min_y - row->y;
14294 if (row->y + row->height > max_y)
14295 row->visible_height -= row->y + row->height - max_y;
14296 }
14297 else
14298 {
14299 row->pixel_width = row->used[TEXT_AREA];
14300 if (row->continued_p)
14301 row->pixel_width -= it->continuation_pixel_width;
14302 else if (row->truncated_on_right_p)
14303 row->pixel_width -= it->truncation_pixel_width;
14304 row->ascent = row->phys_ascent = 0;
14305 row->height = row->phys_height = row->visible_height = 1;
14306 row->extra_line_spacing = 0;
14307 }
14308
14309 /* Compute a hash code for this row. */
14310 row->hash = 0;
14311 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14312 for (i = 0; i < row->used[area]; ++i)
14313 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14314 + row->glyphs[area][i].u.val
14315 + row->glyphs[area][i].face_id
14316 + row->glyphs[area][i].padding_p
14317 + (row->glyphs[area][i].type << 2));
14318
14319 it->max_ascent = it->max_descent = 0;
14320 it->max_phys_ascent = it->max_phys_descent = 0;
14321 }
14322
14323
14324 /* Append one space to the glyph row of iterator IT if doing a
14325 window-based redisplay. The space has the same face as
14326 IT->face_id. Value is non-zero if a space was added.
14327
14328 This function is called to make sure that there is always one glyph
14329 at the end of a glyph row that the cursor can be set on under
14330 window-systems. (If there weren't such a glyph we would not know
14331 how wide and tall a box cursor should be displayed).
14332
14333 At the same time this space let's a nicely handle clearing to the
14334 end of the line if the row ends in italic text. */
14335
14336 static int
14337 append_space_for_newline (it, default_face_p)
14338 struct it *it;
14339 int default_face_p;
14340 {
14341 if (FRAME_WINDOW_P (it->f))
14342 {
14343 int n = it->glyph_row->used[TEXT_AREA];
14344
14345 if (it->glyph_row->glyphs[TEXT_AREA] + n
14346 < it->glyph_row->glyphs[1 + TEXT_AREA])
14347 {
14348 /* Save some values that must not be changed.
14349 Must save IT->c and IT->len because otherwise
14350 ITERATOR_AT_END_P wouldn't work anymore after
14351 append_space_for_newline has been called. */
14352 enum display_element_type saved_what = it->what;
14353 int saved_c = it->c, saved_len = it->len;
14354 int saved_x = it->current_x;
14355 int saved_face_id = it->face_id;
14356 struct text_pos saved_pos;
14357 Lisp_Object saved_object;
14358 struct face *face;
14359
14360 saved_object = it->object;
14361 saved_pos = it->position;
14362
14363 it->what = IT_CHARACTER;
14364 bzero (&it->position, sizeof it->position);
14365 it->object = make_number (0);
14366 it->c = ' ';
14367 it->len = 1;
14368
14369 if (default_face_p)
14370 it->face_id = DEFAULT_FACE_ID;
14371 else if (it->face_before_selective_p)
14372 it->face_id = it->saved_face_id;
14373 face = FACE_FROM_ID (it->f, it->face_id);
14374 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14375
14376 PRODUCE_GLYPHS (it);
14377
14378 it->override_ascent = -1;
14379 it->constrain_row_ascent_descent_p = 0;
14380 it->current_x = saved_x;
14381 it->object = saved_object;
14382 it->position = saved_pos;
14383 it->what = saved_what;
14384 it->face_id = saved_face_id;
14385 it->len = saved_len;
14386 it->c = saved_c;
14387 return 1;
14388 }
14389 }
14390
14391 return 0;
14392 }
14393
14394
14395 /* Extend the face of the last glyph in the text area of IT->glyph_row
14396 to the end of the display line. Called from display_line.
14397 If the glyph row is empty, add a space glyph to it so that we
14398 know the face to draw. Set the glyph row flag fill_line_p. */
14399
14400 static void
14401 extend_face_to_end_of_line (it)
14402 struct it *it;
14403 {
14404 struct face *face;
14405 struct frame *f = it->f;
14406
14407 /* If line is already filled, do nothing. */
14408 if (it->current_x >= it->last_visible_x)
14409 return;
14410
14411 /* Face extension extends the background and box of IT->face_id
14412 to the end of the line. If the background equals the background
14413 of the frame, we don't have to do anything. */
14414 if (it->face_before_selective_p)
14415 face = FACE_FROM_ID (it->f, it->saved_face_id);
14416 else
14417 face = FACE_FROM_ID (f, it->face_id);
14418
14419 if (FRAME_WINDOW_P (f)
14420 && face->box == FACE_NO_BOX
14421 && face->background == FRAME_BACKGROUND_PIXEL (f)
14422 && !face->stipple)
14423 return;
14424
14425 /* Set the glyph row flag indicating that the face of the last glyph
14426 in the text area has to be drawn to the end of the text area. */
14427 it->glyph_row->fill_line_p = 1;
14428
14429 /* If current character of IT is not ASCII, make sure we have the
14430 ASCII face. This will be automatically undone the next time
14431 get_next_display_element returns a multibyte character. Note
14432 that the character will always be single byte in unibyte text. */
14433 if (!SINGLE_BYTE_CHAR_P (it->c))
14434 {
14435 it->face_id = FACE_FOR_CHAR (f, face, 0);
14436 }
14437
14438 if (FRAME_WINDOW_P (f))
14439 {
14440 /* If the row is empty, add a space with the current face of IT,
14441 so that we know which face to draw. */
14442 if (it->glyph_row->used[TEXT_AREA] == 0)
14443 {
14444 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14445 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14446 it->glyph_row->used[TEXT_AREA] = 1;
14447 }
14448 }
14449 else
14450 {
14451 /* Save some values that must not be changed. */
14452 int saved_x = it->current_x;
14453 struct text_pos saved_pos;
14454 Lisp_Object saved_object;
14455 enum display_element_type saved_what = it->what;
14456 int saved_face_id = it->face_id;
14457
14458 saved_object = it->object;
14459 saved_pos = it->position;
14460
14461 it->what = IT_CHARACTER;
14462 bzero (&it->position, sizeof it->position);
14463 it->object = make_number (0);
14464 it->c = ' ';
14465 it->len = 1;
14466 it->face_id = face->id;
14467
14468 PRODUCE_GLYPHS (it);
14469
14470 while (it->current_x <= it->last_visible_x)
14471 PRODUCE_GLYPHS (it);
14472
14473 /* Don't count these blanks really. It would let us insert a left
14474 truncation glyph below and make us set the cursor on them, maybe. */
14475 it->current_x = saved_x;
14476 it->object = saved_object;
14477 it->position = saved_pos;
14478 it->what = saved_what;
14479 it->face_id = saved_face_id;
14480 }
14481 }
14482
14483
14484 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14485 trailing whitespace. */
14486
14487 static int
14488 trailing_whitespace_p (charpos)
14489 int charpos;
14490 {
14491 int bytepos = CHAR_TO_BYTE (charpos);
14492 int c = 0;
14493
14494 while (bytepos < ZV_BYTE
14495 && (c = FETCH_CHAR (bytepos),
14496 c == ' ' || c == '\t'))
14497 ++bytepos;
14498
14499 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14500 {
14501 if (bytepos != PT_BYTE)
14502 return 1;
14503 }
14504 return 0;
14505 }
14506
14507
14508 /* Highlight trailing whitespace, if any, in ROW. */
14509
14510 void
14511 highlight_trailing_whitespace (f, row)
14512 struct frame *f;
14513 struct glyph_row *row;
14514 {
14515 int used = row->used[TEXT_AREA];
14516
14517 if (used)
14518 {
14519 struct glyph *start = row->glyphs[TEXT_AREA];
14520 struct glyph *glyph = start + used - 1;
14521
14522 /* Skip over glyphs inserted to display the cursor at the
14523 end of a line, for extending the face of the last glyph
14524 to the end of the line on terminals, and for truncation
14525 and continuation glyphs. */
14526 while (glyph >= start
14527 && glyph->type == CHAR_GLYPH
14528 && INTEGERP (glyph->object))
14529 --glyph;
14530
14531 /* If last glyph is a space or stretch, and it's trailing
14532 whitespace, set the face of all trailing whitespace glyphs in
14533 IT->glyph_row to `trailing-whitespace'. */
14534 if (glyph >= start
14535 && BUFFERP (glyph->object)
14536 && (glyph->type == STRETCH_GLYPH
14537 || (glyph->type == CHAR_GLYPH
14538 && glyph->u.ch == ' '))
14539 && trailing_whitespace_p (glyph->charpos))
14540 {
14541 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14542
14543 while (glyph >= start
14544 && BUFFERP (glyph->object)
14545 && (glyph->type == STRETCH_GLYPH
14546 || (glyph->type == CHAR_GLYPH
14547 && glyph->u.ch == ' ')))
14548 (glyph--)->face_id = face_id;
14549 }
14550 }
14551 }
14552
14553
14554 /* Value is non-zero if glyph row ROW in window W should be
14555 used to hold the cursor. */
14556
14557 static int
14558 cursor_row_p (w, row)
14559 struct window *w;
14560 struct glyph_row *row;
14561 {
14562 int cursor_row_p = 1;
14563
14564 if (PT == MATRIX_ROW_END_CHARPOS (row))
14565 {
14566 /* If the row ends with a newline from a string, we don't want
14567 the cursor there (if the row is continued it doesn't end in a
14568 newline). */
14569 if (CHARPOS (row->end.string_pos) >= 0
14570 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14571 cursor_row_p = row->continued_p;
14572
14573 /* If the row ends at ZV, display the cursor at the end of that
14574 row instead of at the start of the row below. */
14575 else if (row->ends_at_zv_p)
14576 cursor_row_p = 1;
14577 else
14578 cursor_row_p = 0;
14579 }
14580
14581 return cursor_row_p;
14582 }
14583
14584
14585 /* Construct the glyph row IT->glyph_row in the desired matrix of
14586 IT->w from text at the current position of IT. See dispextern.h
14587 for an overview of struct it. Value is non-zero if
14588 IT->glyph_row displays text, as opposed to a line displaying ZV
14589 only. */
14590
14591 static int
14592 display_line (it)
14593 struct it *it;
14594 {
14595 struct glyph_row *row = it->glyph_row;
14596 int overlay_arrow_bitmap;
14597 Lisp_Object overlay_arrow_string;
14598
14599 /* We always start displaying at hpos zero even if hscrolled. */
14600 xassert (it->hpos == 0 && it->current_x == 0);
14601
14602 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14603 >= it->w->desired_matrix->nrows)
14604 {
14605 it->w->nrows_scale_factor++;
14606 fonts_changed_p = 1;
14607 return 0;
14608 }
14609
14610 /* Is IT->w showing the region? */
14611 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14612
14613 /* Clear the result glyph row and enable it. */
14614 prepare_desired_row (row);
14615
14616 row->y = it->current_y;
14617 row->start = it->start;
14618 row->continuation_lines_width = it->continuation_lines_width;
14619 row->displays_text_p = 1;
14620 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14621 it->starts_in_middle_of_char_p = 0;
14622
14623 /* Arrange the overlays nicely for our purposes. Usually, we call
14624 display_line on only one line at a time, in which case this
14625 can't really hurt too much, or we call it on lines which appear
14626 one after another in the buffer, in which case all calls to
14627 recenter_overlay_lists but the first will be pretty cheap. */
14628 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14629
14630 /* Move over display elements that are not visible because we are
14631 hscrolled. This may stop at an x-position < IT->first_visible_x
14632 if the first glyph is partially visible or if we hit a line end. */
14633 if (it->current_x < it->first_visible_x)
14634 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14635 MOVE_TO_POS | MOVE_TO_X);
14636
14637 /* Get the initial row height. This is either the height of the
14638 text hscrolled, if there is any, or zero. */
14639 row->ascent = it->max_ascent;
14640 row->height = it->max_ascent + it->max_descent;
14641 row->phys_ascent = it->max_phys_ascent;
14642 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14643 row->extra_line_spacing = it->max_extra_line_spacing;
14644
14645 /* Loop generating characters. The loop is left with IT on the next
14646 character to display. */
14647 while (1)
14648 {
14649 int n_glyphs_before, hpos_before, x_before;
14650 int x, i, nglyphs;
14651 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14652
14653 /* Retrieve the next thing to display. Value is zero if end of
14654 buffer reached. */
14655 if (!get_next_display_element (it))
14656 {
14657 /* Maybe add a space at the end of this line that is used to
14658 display the cursor there under X. Set the charpos of the
14659 first glyph of blank lines not corresponding to any text
14660 to -1. */
14661 #ifdef HAVE_WINDOW_SYSTEM
14662 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14663 row->exact_window_width_line_p = 1;
14664 else
14665 #endif /* HAVE_WINDOW_SYSTEM */
14666 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14667 || row->used[TEXT_AREA] == 0)
14668 {
14669 row->glyphs[TEXT_AREA]->charpos = -1;
14670 row->displays_text_p = 0;
14671
14672 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14673 && (!MINI_WINDOW_P (it->w)
14674 || (minibuf_level && EQ (it->window, minibuf_window))))
14675 row->indicate_empty_line_p = 1;
14676 }
14677
14678 it->continuation_lines_width = 0;
14679 row->ends_at_zv_p = 1;
14680 break;
14681 }
14682
14683 /* Now, get the metrics of what we want to display. This also
14684 generates glyphs in `row' (which is IT->glyph_row). */
14685 n_glyphs_before = row->used[TEXT_AREA];
14686 x = it->current_x;
14687
14688 /* Remember the line height so far in case the next element doesn't
14689 fit on the line. */
14690 if (!it->truncate_lines_p)
14691 {
14692 ascent = it->max_ascent;
14693 descent = it->max_descent;
14694 phys_ascent = it->max_phys_ascent;
14695 phys_descent = it->max_phys_descent;
14696 }
14697
14698 PRODUCE_GLYPHS (it);
14699
14700 /* If this display element was in marginal areas, continue with
14701 the next one. */
14702 if (it->area != TEXT_AREA)
14703 {
14704 row->ascent = max (row->ascent, it->max_ascent);
14705 row->height = max (row->height, it->max_ascent + it->max_descent);
14706 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14707 row->phys_height = max (row->phys_height,
14708 it->max_phys_ascent + it->max_phys_descent);
14709 row->extra_line_spacing = max (row->extra_line_spacing,
14710 it->max_extra_line_spacing);
14711 set_iterator_to_next (it, 1);
14712 continue;
14713 }
14714
14715 /* Does the display element fit on the line? If we truncate
14716 lines, we should draw past the right edge of the window. If
14717 we don't truncate, we want to stop so that we can display the
14718 continuation glyph before the right margin. If lines are
14719 continued, there are two possible strategies for characters
14720 resulting in more than 1 glyph (e.g. tabs): Display as many
14721 glyphs as possible in this line and leave the rest for the
14722 continuation line, or display the whole element in the next
14723 line. Original redisplay did the former, so we do it also. */
14724 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14725 hpos_before = it->hpos;
14726 x_before = x;
14727
14728 if (/* Not a newline. */
14729 nglyphs > 0
14730 /* Glyphs produced fit entirely in the line. */
14731 && it->current_x < it->last_visible_x)
14732 {
14733 it->hpos += nglyphs;
14734 row->ascent = max (row->ascent, it->max_ascent);
14735 row->height = max (row->height, it->max_ascent + it->max_descent);
14736 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14737 row->phys_height = max (row->phys_height,
14738 it->max_phys_ascent + it->max_phys_descent);
14739 row->extra_line_spacing = max (row->extra_line_spacing,
14740 it->max_extra_line_spacing);
14741 if (it->current_x - it->pixel_width < it->first_visible_x)
14742 row->x = x - it->first_visible_x;
14743 }
14744 else
14745 {
14746 int new_x;
14747 struct glyph *glyph;
14748
14749 for (i = 0; i < nglyphs; ++i, x = new_x)
14750 {
14751 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14752 new_x = x + glyph->pixel_width;
14753
14754 if (/* Lines are continued. */
14755 !it->truncate_lines_p
14756 && (/* Glyph doesn't fit on the line. */
14757 new_x > it->last_visible_x
14758 /* Or it fits exactly on a window system frame. */
14759 || (new_x == it->last_visible_x
14760 && FRAME_WINDOW_P (it->f))))
14761 {
14762 /* End of a continued line. */
14763
14764 if (it->hpos == 0
14765 || (new_x == it->last_visible_x
14766 && FRAME_WINDOW_P (it->f)))
14767 {
14768 /* Current glyph is the only one on the line or
14769 fits exactly on the line. We must continue
14770 the line because we can't draw the cursor
14771 after the glyph. */
14772 row->continued_p = 1;
14773 it->current_x = new_x;
14774 it->continuation_lines_width += new_x;
14775 ++it->hpos;
14776 if (i == nglyphs - 1)
14777 {
14778 set_iterator_to_next (it, 1);
14779 #ifdef HAVE_WINDOW_SYSTEM
14780 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14781 {
14782 if (!get_next_display_element (it))
14783 {
14784 row->exact_window_width_line_p = 1;
14785 it->continuation_lines_width = 0;
14786 row->continued_p = 0;
14787 row->ends_at_zv_p = 1;
14788 }
14789 else if (ITERATOR_AT_END_OF_LINE_P (it))
14790 {
14791 row->continued_p = 0;
14792 row->exact_window_width_line_p = 1;
14793 }
14794 else if (it->method == next_element_from_display_vector)
14795 it->face_id = it->saved_face_id;
14796 }
14797 #endif /* HAVE_WINDOW_SYSTEM */
14798 }
14799 }
14800 else if (CHAR_GLYPH_PADDING_P (*glyph)
14801 && !FRAME_WINDOW_P (it->f))
14802 {
14803 /* A padding glyph that doesn't fit on this line.
14804 This means the whole character doesn't fit
14805 on the line. */
14806 row->used[TEXT_AREA] = n_glyphs_before;
14807
14808 /* Fill the rest of the row with continuation
14809 glyphs like in 20.x. */
14810 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14811 < row->glyphs[1 + TEXT_AREA])
14812 produce_special_glyphs (it, IT_CONTINUATION);
14813
14814 row->continued_p = 1;
14815 it->current_x = x_before;
14816 it->continuation_lines_width += x_before;
14817
14818 /* Restore the height to what it was before the
14819 element not fitting on the line. */
14820 it->max_ascent = ascent;
14821 it->max_descent = descent;
14822 it->max_phys_ascent = phys_ascent;
14823 it->max_phys_descent = phys_descent;
14824 }
14825 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14826 {
14827 /* A TAB that extends past the right edge of the
14828 window. This produces a single glyph on
14829 window system frames. We leave the glyph in
14830 this row and let it fill the row, but don't
14831 consume the TAB. */
14832 it->continuation_lines_width += it->last_visible_x;
14833 row->ends_in_middle_of_char_p = 1;
14834 row->continued_p = 1;
14835 glyph->pixel_width = it->last_visible_x - x;
14836 it->starts_in_middle_of_char_p = 1;
14837 }
14838 else
14839 {
14840 /* Something other than a TAB that draws past
14841 the right edge of the window. Restore
14842 positions to values before the element. */
14843 row->used[TEXT_AREA] = n_glyphs_before + i;
14844
14845 /* Display continuation glyphs. */
14846 if (!FRAME_WINDOW_P (it->f))
14847 produce_special_glyphs (it, IT_CONTINUATION);
14848 row->continued_p = 1;
14849
14850 it->continuation_lines_width += x;
14851
14852 if (nglyphs > 1 && i > 0)
14853 {
14854 row->ends_in_middle_of_char_p = 1;
14855 it->starts_in_middle_of_char_p = 1;
14856 }
14857
14858 /* Restore the height to what it was before the
14859 element not fitting on the line. */
14860 it->max_ascent = ascent;
14861 it->max_descent = descent;
14862 it->max_phys_ascent = phys_ascent;
14863 it->max_phys_descent = phys_descent;
14864 }
14865
14866 break;
14867 }
14868 else if (new_x > it->first_visible_x)
14869 {
14870 /* Increment number of glyphs actually displayed. */
14871 ++it->hpos;
14872
14873 if (x < it->first_visible_x)
14874 /* Glyph is partially visible, i.e. row starts at
14875 negative X position. */
14876 row->x = x - it->first_visible_x;
14877 }
14878 else
14879 {
14880 /* Glyph is completely off the left margin of the
14881 window. This should not happen because of the
14882 move_it_in_display_line at the start of this
14883 function, unless the text display area of the
14884 window is empty. */
14885 xassert (it->first_visible_x <= it->last_visible_x);
14886 }
14887 }
14888
14889 row->ascent = max (row->ascent, it->max_ascent);
14890 row->height = max (row->height, it->max_ascent + it->max_descent);
14891 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14892 row->phys_height = max (row->phys_height,
14893 it->max_phys_ascent + it->max_phys_descent);
14894 row->extra_line_spacing = max (row->extra_line_spacing,
14895 it->max_extra_line_spacing);
14896
14897 /* End of this display line if row is continued. */
14898 if (row->continued_p || row->ends_at_zv_p)
14899 break;
14900 }
14901
14902 at_end_of_line:
14903 /* Is this a line end? If yes, we're also done, after making
14904 sure that a non-default face is extended up to the right
14905 margin of the window. */
14906 if (ITERATOR_AT_END_OF_LINE_P (it))
14907 {
14908 int used_before = row->used[TEXT_AREA];
14909
14910 row->ends_in_newline_from_string_p = STRINGP (it->object);
14911
14912 #ifdef HAVE_WINDOW_SYSTEM
14913 /* Add a space at the end of the line that is used to
14914 display the cursor there. */
14915 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14916 append_space_for_newline (it, 0);
14917 #endif /* HAVE_WINDOW_SYSTEM */
14918
14919 /* Extend the face to the end of the line. */
14920 extend_face_to_end_of_line (it);
14921
14922 /* Make sure we have the position. */
14923 if (used_before == 0)
14924 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14925
14926 /* Consume the line end. This skips over invisible lines. */
14927 set_iterator_to_next (it, 1);
14928 it->continuation_lines_width = 0;
14929 break;
14930 }
14931
14932 /* Proceed with next display element. Note that this skips
14933 over lines invisible because of selective display. */
14934 set_iterator_to_next (it, 1);
14935
14936 /* If we truncate lines, we are done when the last displayed
14937 glyphs reach past the right margin of the window. */
14938 if (it->truncate_lines_p
14939 && (FRAME_WINDOW_P (it->f)
14940 ? (it->current_x >= it->last_visible_x)
14941 : (it->current_x > it->last_visible_x)))
14942 {
14943 /* Maybe add truncation glyphs. */
14944 if (!FRAME_WINDOW_P (it->f))
14945 {
14946 int i, n;
14947
14948 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14949 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14950 break;
14951
14952 for (n = row->used[TEXT_AREA]; i < n; ++i)
14953 {
14954 row->used[TEXT_AREA] = i;
14955 produce_special_glyphs (it, IT_TRUNCATION);
14956 }
14957 }
14958 #ifdef HAVE_WINDOW_SYSTEM
14959 else
14960 {
14961 /* Don't truncate if we can overflow newline into fringe. */
14962 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14963 {
14964 if (!get_next_display_element (it))
14965 {
14966 it->continuation_lines_width = 0;
14967 row->ends_at_zv_p = 1;
14968 row->exact_window_width_line_p = 1;
14969 break;
14970 }
14971 if (ITERATOR_AT_END_OF_LINE_P (it))
14972 {
14973 row->exact_window_width_line_p = 1;
14974 goto at_end_of_line;
14975 }
14976 }
14977 }
14978 #endif /* HAVE_WINDOW_SYSTEM */
14979
14980 row->truncated_on_right_p = 1;
14981 it->continuation_lines_width = 0;
14982 reseat_at_next_visible_line_start (it, 0);
14983 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14984 it->hpos = hpos_before;
14985 it->current_x = x_before;
14986 break;
14987 }
14988 }
14989
14990 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14991 at the left window margin. */
14992 if (it->first_visible_x
14993 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14994 {
14995 if (!FRAME_WINDOW_P (it->f))
14996 insert_left_trunc_glyphs (it);
14997 row->truncated_on_left_p = 1;
14998 }
14999
15000 /* If the start of this line is the overlay arrow-position, then
15001 mark this glyph row as the one containing the overlay arrow.
15002 This is clearly a mess with variable size fonts. It would be
15003 better to let it be displayed like cursors under X. */
15004 if (! overlay_arrow_seen
15005 && (overlay_arrow_string
15006 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15007 !NILP (overlay_arrow_string)))
15008 {
15009 /* Overlay arrow in window redisplay is a fringe bitmap. */
15010 if (STRINGP (overlay_arrow_string))
15011 {
15012 struct glyph_row *arrow_row
15013 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15014 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15015 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15016 struct glyph *p = row->glyphs[TEXT_AREA];
15017 struct glyph *p2, *end;
15018
15019 /* Copy the arrow glyphs. */
15020 while (glyph < arrow_end)
15021 *p++ = *glyph++;
15022
15023 /* Throw away padding glyphs. */
15024 p2 = p;
15025 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15026 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15027 ++p2;
15028 if (p2 > p)
15029 {
15030 while (p2 < end)
15031 *p++ = *p2++;
15032 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15033 }
15034 }
15035 else
15036 {
15037 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15038 row->overlay_arrow_p = 1;
15039 }
15040 overlay_arrow_seen = 1;
15041 }
15042
15043 /* Compute pixel dimensions of this line. */
15044 compute_line_metrics (it);
15045
15046 /* Remember the position at which this line ends. */
15047 row->end = it->current;
15048
15049 /* Save fringe bitmaps in this row. */
15050 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15051 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15052 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15053 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15054
15055 it->left_user_fringe_bitmap = 0;
15056 it->left_user_fringe_face_id = 0;
15057 it->right_user_fringe_bitmap = 0;
15058 it->right_user_fringe_face_id = 0;
15059
15060 /* Maybe set the cursor. */
15061 if (it->w->cursor.vpos < 0
15062 && PT >= MATRIX_ROW_START_CHARPOS (row)
15063 && PT <= MATRIX_ROW_END_CHARPOS (row)
15064 && cursor_row_p (it->w, row))
15065 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15066
15067 /* Highlight trailing whitespace. */
15068 if (!NILP (Vshow_trailing_whitespace))
15069 highlight_trailing_whitespace (it->f, it->glyph_row);
15070
15071 /* Prepare for the next line. This line starts horizontally at (X
15072 HPOS) = (0 0). Vertical positions are incremented. As a
15073 convenience for the caller, IT->glyph_row is set to the next
15074 row to be used. */
15075 it->current_x = it->hpos = 0;
15076 it->current_y += row->height;
15077 ++it->vpos;
15078 ++it->glyph_row;
15079 it->start = it->current;
15080 return row->displays_text_p;
15081 }
15082
15083
15084 \f
15085 /***********************************************************************
15086 Menu Bar
15087 ***********************************************************************/
15088
15089 /* Redisplay the menu bar in the frame for window W.
15090
15091 The menu bar of X frames that don't have X toolkit support is
15092 displayed in a special window W->frame->menu_bar_window.
15093
15094 The menu bar of terminal frames is treated specially as far as
15095 glyph matrices are concerned. Menu bar lines are not part of
15096 windows, so the update is done directly on the frame matrix rows
15097 for the menu bar. */
15098
15099 static void
15100 display_menu_bar (w)
15101 struct window *w;
15102 {
15103 struct frame *f = XFRAME (WINDOW_FRAME (w));
15104 struct it it;
15105 Lisp_Object items;
15106 int i;
15107
15108 /* Don't do all this for graphical frames. */
15109 #ifdef HAVE_NTGUI
15110 if (!NILP (Vwindow_system))
15111 return;
15112 #endif
15113 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15114 if (FRAME_X_P (f))
15115 return;
15116 #endif
15117 #ifdef MAC_OS
15118 if (FRAME_MAC_P (f))
15119 return;
15120 #endif
15121
15122 #ifdef USE_X_TOOLKIT
15123 xassert (!FRAME_WINDOW_P (f));
15124 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15125 it.first_visible_x = 0;
15126 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15127 #else /* not USE_X_TOOLKIT */
15128 if (FRAME_WINDOW_P (f))
15129 {
15130 /* Menu bar lines are displayed in the desired matrix of the
15131 dummy window menu_bar_window. */
15132 struct window *menu_w;
15133 xassert (WINDOWP (f->menu_bar_window));
15134 menu_w = XWINDOW (f->menu_bar_window);
15135 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15136 MENU_FACE_ID);
15137 it.first_visible_x = 0;
15138 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15139 }
15140 else
15141 {
15142 /* This is a TTY frame, i.e. character hpos/vpos are used as
15143 pixel x/y. */
15144 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15145 MENU_FACE_ID);
15146 it.first_visible_x = 0;
15147 it.last_visible_x = FRAME_COLS (f);
15148 }
15149 #endif /* not USE_X_TOOLKIT */
15150
15151 if (! mode_line_inverse_video)
15152 /* Force the menu-bar to be displayed in the default face. */
15153 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15154
15155 /* Clear all rows of the menu bar. */
15156 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15157 {
15158 struct glyph_row *row = it.glyph_row + i;
15159 clear_glyph_row (row);
15160 row->enabled_p = 1;
15161 row->full_width_p = 1;
15162 }
15163
15164 /* Display all items of the menu bar. */
15165 items = FRAME_MENU_BAR_ITEMS (it.f);
15166 for (i = 0; i < XVECTOR (items)->size; i += 4)
15167 {
15168 Lisp_Object string;
15169
15170 /* Stop at nil string. */
15171 string = AREF (items, i + 1);
15172 if (NILP (string))
15173 break;
15174
15175 /* Remember where item was displayed. */
15176 AREF (items, i + 3) = make_number (it.hpos);
15177
15178 /* Display the item, pad with one space. */
15179 if (it.current_x < it.last_visible_x)
15180 display_string (NULL, string, Qnil, 0, 0, &it,
15181 SCHARS (string) + 1, 0, 0, -1);
15182 }
15183
15184 /* Fill out the line with spaces. */
15185 if (it.current_x < it.last_visible_x)
15186 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15187
15188 /* Compute the total height of the lines. */
15189 compute_line_metrics (&it);
15190 }
15191
15192
15193 \f
15194 /***********************************************************************
15195 Mode Line
15196 ***********************************************************************/
15197
15198 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15199 FORCE is non-zero, redisplay mode lines unconditionally.
15200 Otherwise, redisplay only mode lines that are garbaged. Value is
15201 the number of windows whose mode lines were redisplayed. */
15202
15203 static int
15204 redisplay_mode_lines (window, force)
15205 Lisp_Object window;
15206 int force;
15207 {
15208 int nwindows = 0;
15209
15210 while (!NILP (window))
15211 {
15212 struct window *w = XWINDOW (window);
15213
15214 if (WINDOWP (w->hchild))
15215 nwindows += redisplay_mode_lines (w->hchild, force);
15216 else if (WINDOWP (w->vchild))
15217 nwindows += redisplay_mode_lines (w->vchild, force);
15218 else if (force
15219 || FRAME_GARBAGED_P (XFRAME (w->frame))
15220 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15221 {
15222 struct text_pos lpoint;
15223 struct buffer *old = current_buffer;
15224
15225 /* Set the window's buffer for the mode line display. */
15226 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15227 set_buffer_internal_1 (XBUFFER (w->buffer));
15228
15229 /* Point refers normally to the selected window. For any
15230 other window, set up appropriate value. */
15231 if (!EQ (window, selected_window))
15232 {
15233 struct text_pos pt;
15234
15235 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15236 if (CHARPOS (pt) < BEGV)
15237 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15238 else if (CHARPOS (pt) > (ZV - 1))
15239 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15240 else
15241 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15242 }
15243
15244 /* Display mode lines. */
15245 clear_glyph_matrix (w->desired_matrix);
15246 if (display_mode_lines (w))
15247 {
15248 ++nwindows;
15249 w->must_be_updated_p = 1;
15250 }
15251
15252 /* Restore old settings. */
15253 set_buffer_internal_1 (old);
15254 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15255 }
15256
15257 window = w->next;
15258 }
15259
15260 return nwindows;
15261 }
15262
15263
15264 /* Display the mode and/or top line of window W. Value is the number
15265 of mode lines displayed. */
15266
15267 static int
15268 display_mode_lines (w)
15269 struct window *w;
15270 {
15271 Lisp_Object old_selected_window, old_selected_frame;
15272 int n = 0;
15273
15274 old_selected_frame = selected_frame;
15275 selected_frame = w->frame;
15276 old_selected_window = selected_window;
15277 XSETWINDOW (selected_window, w);
15278
15279 /* These will be set while the mode line specs are processed. */
15280 line_number_displayed = 0;
15281 w->column_number_displayed = Qnil;
15282
15283 if (WINDOW_WANTS_MODELINE_P (w))
15284 {
15285 struct window *sel_w = XWINDOW (old_selected_window);
15286
15287 /* Select mode line face based on the real selected window. */
15288 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15289 current_buffer->mode_line_format);
15290 ++n;
15291 }
15292
15293 if (WINDOW_WANTS_HEADER_LINE_P (w))
15294 {
15295 display_mode_line (w, HEADER_LINE_FACE_ID,
15296 current_buffer->header_line_format);
15297 ++n;
15298 }
15299
15300 selected_frame = old_selected_frame;
15301 selected_window = old_selected_window;
15302 return n;
15303 }
15304
15305
15306 /* Display mode or top line of window W. FACE_ID specifies which line
15307 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15308 FORMAT is the mode line format to display. Value is the pixel
15309 height of the mode line displayed. */
15310
15311 static int
15312 display_mode_line (w, face_id, format)
15313 struct window *w;
15314 enum face_id face_id;
15315 Lisp_Object format;
15316 {
15317 struct it it;
15318 struct face *face;
15319
15320 init_iterator (&it, w, -1, -1, NULL, face_id);
15321 prepare_desired_row (it.glyph_row);
15322
15323 it.glyph_row->mode_line_p = 1;
15324
15325 if (! mode_line_inverse_video)
15326 /* Force the mode-line to be displayed in the default face. */
15327 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15328
15329 /* Temporarily make frame's keyboard the current kboard so that
15330 kboard-local variables in the mode_line_format will get the right
15331 values. */
15332 push_frame_kboard (it.f);
15333 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15334 pop_frame_kboard ();
15335
15336 /* Fill up with spaces. */
15337 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15338
15339 compute_line_metrics (&it);
15340 it.glyph_row->full_width_p = 1;
15341 it.glyph_row->continued_p = 0;
15342 it.glyph_row->truncated_on_left_p = 0;
15343 it.glyph_row->truncated_on_right_p = 0;
15344
15345 /* Make a 3D mode-line have a shadow at its right end. */
15346 face = FACE_FROM_ID (it.f, face_id);
15347 extend_face_to_end_of_line (&it);
15348 if (face->box != FACE_NO_BOX)
15349 {
15350 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15351 + it.glyph_row->used[TEXT_AREA] - 1);
15352 last->right_box_line_p = 1;
15353 }
15354
15355 return it.glyph_row->height;
15356 }
15357
15358 /* Alist that caches the results of :propertize.
15359 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15360 Lisp_Object mode_line_proptrans_alist;
15361
15362 /* List of strings making up the mode-line. */
15363 Lisp_Object mode_line_string_list;
15364
15365 /* Base face property when building propertized mode line string. */
15366 static Lisp_Object mode_line_string_face;
15367 static Lisp_Object mode_line_string_face_prop;
15368
15369
15370 /* Contribute ELT to the mode line for window IT->w. How it
15371 translates into text depends on its data type.
15372
15373 IT describes the display environment in which we display, as usual.
15374
15375 DEPTH is the depth in recursion. It is used to prevent
15376 infinite recursion here.
15377
15378 FIELD_WIDTH is the number of characters the display of ELT should
15379 occupy in the mode line, and PRECISION is the maximum number of
15380 characters to display from ELT's representation. See
15381 display_string for details.
15382
15383 Returns the hpos of the end of the text generated by ELT.
15384
15385 PROPS is a property list to add to any string we encounter.
15386
15387 If RISKY is nonzero, remove (disregard) any properties in any string
15388 we encounter, and ignore :eval and :propertize.
15389
15390 If the global variable `frame_title_ptr' is non-NULL, then the output
15391 is passed to `store_frame_title' instead of `display_string'. */
15392
15393 static int
15394 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15395 struct it *it;
15396 int depth;
15397 int field_width, precision;
15398 Lisp_Object elt, props;
15399 int risky;
15400 {
15401 int n = 0, field, prec;
15402 int literal = 0;
15403
15404 tail_recurse:
15405 if (depth > 100)
15406 elt = build_string ("*too-deep*");
15407
15408 depth++;
15409
15410 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15411 {
15412 case Lisp_String:
15413 {
15414 /* A string: output it and check for %-constructs within it. */
15415 unsigned char c;
15416 const unsigned char *this, *lisp_string;
15417
15418 if (!NILP (props) || risky)
15419 {
15420 Lisp_Object oprops, aelt;
15421 oprops = Ftext_properties_at (make_number (0), elt);
15422
15423 /* If the starting string's properties are not what
15424 we want, translate the string. Also, if the string
15425 is risky, do that anyway. */
15426
15427 if (NILP (Fequal (props, oprops)) || risky)
15428 {
15429 /* If the starting string has properties,
15430 merge the specified ones onto the existing ones. */
15431 if (! NILP (oprops) && !risky)
15432 {
15433 Lisp_Object tem;
15434
15435 oprops = Fcopy_sequence (oprops);
15436 tem = props;
15437 while (CONSP (tem))
15438 {
15439 oprops = Fplist_put (oprops, XCAR (tem),
15440 XCAR (XCDR (tem)));
15441 tem = XCDR (XCDR (tem));
15442 }
15443 props = oprops;
15444 }
15445
15446 aelt = Fassoc (elt, mode_line_proptrans_alist);
15447 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15448 {
15449 mode_line_proptrans_alist
15450 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15451 elt = XCAR (aelt);
15452 }
15453 else
15454 {
15455 Lisp_Object tem;
15456
15457 elt = Fcopy_sequence (elt);
15458 Fset_text_properties (make_number (0), Flength (elt),
15459 props, elt);
15460 /* Add this item to mode_line_proptrans_alist. */
15461 mode_line_proptrans_alist
15462 = Fcons (Fcons (elt, props),
15463 mode_line_proptrans_alist);
15464 /* Truncate mode_line_proptrans_alist
15465 to at most 50 elements. */
15466 tem = Fnthcdr (make_number (50),
15467 mode_line_proptrans_alist);
15468 if (! NILP (tem))
15469 XSETCDR (tem, Qnil);
15470 }
15471 }
15472 }
15473
15474 this = SDATA (elt);
15475 lisp_string = this;
15476
15477 if (literal)
15478 {
15479 prec = precision - n;
15480 if (frame_title_ptr)
15481 n += store_frame_title (SDATA (elt), -1, prec);
15482 else if (!NILP (mode_line_string_list))
15483 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15484 else
15485 n += display_string (NULL, elt, Qnil, 0, 0, it,
15486 0, prec, 0, STRING_MULTIBYTE (elt));
15487
15488 break;
15489 }
15490
15491 while ((precision <= 0 || n < precision)
15492 && *this
15493 && (frame_title_ptr
15494 || !NILP (mode_line_string_list)
15495 || it->current_x < it->last_visible_x))
15496 {
15497 const unsigned char *last = this;
15498
15499 /* Advance to end of string or next format specifier. */
15500 while ((c = *this++) != '\0' && c != '%')
15501 ;
15502
15503 if (this - 1 != last)
15504 {
15505 int nchars, nbytes;
15506
15507 /* Output to end of string or up to '%'. Field width
15508 is length of string. Don't output more than
15509 PRECISION allows us. */
15510 --this;
15511
15512 prec = c_string_width (last, this - last, precision - n,
15513 &nchars, &nbytes);
15514
15515 if (frame_title_ptr)
15516 n += store_frame_title (last, 0, prec);
15517 else if (!NILP (mode_line_string_list))
15518 {
15519 int bytepos = last - lisp_string;
15520 int charpos = string_byte_to_char (elt, bytepos);
15521 int endpos = (precision <= 0
15522 ? string_byte_to_char (elt,
15523 this - lisp_string)
15524 : charpos + nchars);
15525
15526 n += store_mode_line_string (NULL,
15527 Fsubstring (elt, make_number (charpos),
15528 make_number (endpos)),
15529 0, 0, 0, Qnil);
15530 }
15531 else
15532 {
15533 int bytepos = last - lisp_string;
15534 int charpos = string_byte_to_char (elt, bytepos);
15535 n += display_string (NULL, elt, Qnil, 0, charpos,
15536 it, 0, prec, 0,
15537 STRING_MULTIBYTE (elt));
15538 }
15539 }
15540 else /* c == '%' */
15541 {
15542 const unsigned char *percent_position = this;
15543
15544 /* Get the specified minimum width. Zero means
15545 don't pad. */
15546 field = 0;
15547 while ((c = *this++) >= '0' && c <= '9')
15548 field = field * 10 + c - '0';
15549
15550 /* Don't pad beyond the total padding allowed. */
15551 if (field_width - n > 0 && field > field_width - n)
15552 field = field_width - n;
15553
15554 /* Note that either PRECISION <= 0 or N < PRECISION. */
15555 prec = precision - n;
15556
15557 if (c == 'M')
15558 n += display_mode_element (it, depth, field, prec,
15559 Vglobal_mode_string, props,
15560 risky);
15561 else if (c != 0)
15562 {
15563 int multibyte;
15564 int bytepos, charpos;
15565 unsigned char *spec;
15566
15567 bytepos = percent_position - lisp_string;
15568 charpos = (STRING_MULTIBYTE (elt)
15569 ? string_byte_to_char (elt, bytepos)
15570 : bytepos);
15571
15572 spec
15573 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15574
15575 if (frame_title_ptr)
15576 n += store_frame_title (spec, field, prec);
15577 else if (!NILP (mode_line_string_list))
15578 {
15579 int len = strlen (spec);
15580 Lisp_Object tem = make_string (spec, len);
15581 props = Ftext_properties_at (make_number (charpos), elt);
15582 /* Should only keep face property in props */
15583 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15584 }
15585 else
15586 {
15587 int nglyphs_before, nwritten;
15588
15589 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15590 nwritten = display_string (spec, Qnil, elt,
15591 charpos, 0, it,
15592 field, prec, 0,
15593 multibyte);
15594
15595 /* Assign to the glyphs written above the
15596 string where the `%x' came from, position
15597 of the `%'. */
15598 if (nwritten > 0)
15599 {
15600 struct glyph *glyph
15601 = (it->glyph_row->glyphs[TEXT_AREA]
15602 + nglyphs_before);
15603 int i;
15604
15605 for (i = 0; i < nwritten; ++i)
15606 {
15607 glyph[i].object = elt;
15608 glyph[i].charpos = charpos;
15609 }
15610
15611 n += nwritten;
15612 }
15613 }
15614 }
15615 else /* c == 0 */
15616 break;
15617 }
15618 }
15619 }
15620 break;
15621
15622 case Lisp_Symbol:
15623 /* A symbol: process the value of the symbol recursively
15624 as if it appeared here directly. Avoid error if symbol void.
15625 Special case: if value of symbol is a string, output the string
15626 literally. */
15627 {
15628 register Lisp_Object tem;
15629
15630 /* If the variable is not marked as risky to set
15631 then its contents are risky to use. */
15632 if (NILP (Fget (elt, Qrisky_local_variable)))
15633 risky = 1;
15634
15635 tem = Fboundp (elt);
15636 if (!NILP (tem))
15637 {
15638 tem = Fsymbol_value (elt);
15639 /* If value is a string, output that string literally:
15640 don't check for % within it. */
15641 if (STRINGP (tem))
15642 literal = 1;
15643
15644 if (!EQ (tem, elt))
15645 {
15646 /* Give up right away for nil or t. */
15647 elt = tem;
15648 goto tail_recurse;
15649 }
15650 }
15651 }
15652 break;
15653
15654 case Lisp_Cons:
15655 {
15656 register Lisp_Object car, tem;
15657
15658 /* A cons cell: five distinct cases.
15659 If first element is :eval or :propertize, do something special.
15660 If first element is a string or a cons, process all the elements
15661 and effectively concatenate them.
15662 If first element is a negative number, truncate displaying cdr to
15663 at most that many characters. If positive, pad (with spaces)
15664 to at least that many characters.
15665 If first element is a symbol, process the cadr or caddr recursively
15666 according to whether the symbol's value is non-nil or nil. */
15667 car = XCAR (elt);
15668 if (EQ (car, QCeval))
15669 {
15670 /* An element of the form (:eval FORM) means evaluate FORM
15671 and use the result as mode line elements. */
15672
15673 if (risky)
15674 break;
15675
15676 if (CONSP (XCDR (elt)))
15677 {
15678 Lisp_Object spec;
15679 spec = safe_eval (XCAR (XCDR (elt)));
15680 n += display_mode_element (it, depth, field_width - n,
15681 precision - n, spec, props,
15682 risky);
15683 }
15684 }
15685 else if (EQ (car, QCpropertize))
15686 {
15687 /* An element of the form (:propertize ELT PROPS...)
15688 means display ELT but applying properties PROPS. */
15689
15690 if (risky)
15691 break;
15692
15693 if (CONSP (XCDR (elt)))
15694 n += display_mode_element (it, depth, field_width - n,
15695 precision - n, XCAR (XCDR (elt)),
15696 XCDR (XCDR (elt)), risky);
15697 }
15698 else if (SYMBOLP (car))
15699 {
15700 tem = Fboundp (car);
15701 elt = XCDR (elt);
15702 if (!CONSP (elt))
15703 goto invalid;
15704 /* elt is now the cdr, and we know it is a cons cell.
15705 Use its car if CAR has a non-nil value. */
15706 if (!NILP (tem))
15707 {
15708 tem = Fsymbol_value (car);
15709 if (!NILP (tem))
15710 {
15711 elt = XCAR (elt);
15712 goto tail_recurse;
15713 }
15714 }
15715 /* Symbol's value is nil (or symbol is unbound)
15716 Get the cddr of the original list
15717 and if possible find the caddr and use that. */
15718 elt = XCDR (elt);
15719 if (NILP (elt))
15720 break;
15721 else if (!CONSP (elt))
15722 goto invalid;
15723 elt = XCAR (elt);
15724 goto tail_recurse;
15725 }
15726 else if (INTEGERP (car))
15727 {
15728 register int lim = XINT (car);
15729 elt = XCDR (elt);
15730 if (lim < 0)
15731 {
15732 /* Negative int means reduce maximum width. */
15733 if (precision <= 0)
15734 precision = -lim;
15735 else
15736 precision = min (precision, -lim);
15737 }
15738 else if (lim > 0)
15739 {
15740 /* Padding specified. Don't let it be more than
15741 current maximum. */
15742 if (precision > 0)
15743 lim = min (precision, lim);
15744
15745 /* If that's more padding than already wanted, queue it.
15746 But don't reduce padding already specified even if
15747 that is beyond the current truncation point. */
15748 field_width = max (lim, field_width);
15749 }
15750 goto tail_recurse;
15751 }
15752 else if (STRINGP (car) || CONSP (car))
15753 {
15754 register int limit = 50;
15755 /* Limit is to protect against circular lists. */
15756 while (CONSP (elt)
15757 && --limit > 0
15758 && (precision <= 0 || n < precision))
15759 {
15760 n += display_mode_element (it, depth, field_width - n,
15761 precision - n, XCAR (elt),
15762 props, risky);
15763 elt = XCDR (elt);
15764 }
15765 }
15766 }
15767 break;
15768
15769 default:
15770 invalid:
15771 elt = build_string ("*invalid*");
15772 goto tail_recurse;
15773 }
15774
15775 /* Pad to FIELD_WIDTH. */
15776 if (field_width > 0 && n < field_width)
15777 {
15778 if (frame_title_ptr)
15779 n += store_frame_title ("", field_width - n, 0);
15780 else if (!NILP (mode_line_string_list))
15781 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15782 else
15783 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15784 0, 0, 0);
15785 }
15786
15787 return n;
15788 }
15789
15790 /* Store a mode-line string element in mode_line_string_list.
15791
15792 If STRING is non-null, display that C string. Otherwise, the Lisp
15793 string LISP_STRING is displayed.
15794
15795 FIELD_WIDTH is the minimum number of output glyphs to produce.
15796 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15797 with spaces. FIELD_WIDTH <= 0 means don't pad.
15798
15799 PRECISION is the maximum number of characters to output from
15800 STRING. PRECISION <= 0 means don't truncate the string.
15801
15802 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15803 properties to the string.
15804
15805 PROPS are the properties to add to the string.
15806 The mode_line_string_face face property is always added to the string.
15807 */
15808
15809 static int
15810 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15811 char *string;
15812 Lisp_Object lisp_string;
15813 int copy_string;
15814 int field_width;
15815 int precision;
15816 Lisp_Object props;
15817 {
15818 int len;
15819 int n = 0;
15820
15821 if (string != NULL)
15822 {
15823 len = strlen (string);
15824 if (precision > 0 && len > precision)
15825 len = precision;
15826 lisp_string = make_string (string, len);
15827 if (NILP (props))
15828 props = mode_line_string_face_prop;
15829 else if (!NILP (mode_line_string_face))
15830 {
15831 Lisp_Object face = Fsafe_plist_get (props, Qface);
15832 props = Fcopy_sequence (props);
15833 if (NILP (face))
15834 face = mode_line_string_face;
15835 else
15836 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15837 props = Fplist_put (props, Qface, face);
15838 }
15839 Fadd_text_properties (make_number (0), make_number (len),
15840 props, lisp_string);
15841 }
15842 else
15843 {
15844 len = XFASTINT (Flength (lisp_string));
15845 if (precision > 0 && len > precision)
15846 {
15847 len = precision;
15848 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15849 precision = -1;
15850 }
15851 if (!NILP (mode_line_string_face))
15852 {
15853 Lisp_Object face;
15854 if (NILP (props))
15855 props = Ftext_properties_at (make_number (0), lisp_string);
15856 face = Fsafe_plist_get (props, Qface);
15857 if (NILP (face))
15858 face = mode_line_string_face;
15859 else
15860 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15861 props = Fcons (Qface, Fcons (face, Qnil));
15862 if (copy_string)
15863 lisp_string = Fcopy_sequence (lisp_string);
15864 }
15865 if (!NILP (props))
15866 Fadd_text_properties (make_number (0), make_number (len),
15867 props, lisp_string);
15868 }
15869
15870 if (len > 0)
15871 {
15872 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15873 n += len;
15874 }
15875
15876 if (field_width > len)
15877 {
15878 field_width -= len;
15879 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15880 if (!NILP (props))
15881 Fadd_text_properties (make_number (0), make_number (field_width),
15882 props, lisp_string);
15883 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15884 n += field_width;
15885 }
15886
15887 return n;
15888 }
15889
15890
15891 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15892 0, 4, 0,
15893 doc: /* Return the mode-line of selected window as a string.
15894 First optional arg FORMAT specifies a different format string (see
15895 `mode-line-format' for details) to use. If FORMAT is t, return
15896 the buffer's header-line. Second optional arg WINDOW specifies a
15897 different window to use as the context for the formatting.
15898 If third optional arg NO-PROPS is non-nil, string is not propertized.
15899 Fourth optional arg BUFFER specifies which buffer to use. */)
15900 (format, window, no_props, buffer)
15901 Lisp_Object format, window, no_props, buffer;
15902 {
15903 struct it it;
15904 int len;
15905 struct window *w;
15906 struct buffer *old_buffer = NULL;
15907 enum face_id face_id = DEFAULT_FACE_ID;
15908
15909 if (NILP (window))
15910 window = selected_window;
15911 CHECK_WINDOW (window);
15912 w = XWINDOW (window);
15913
15914 if (NILP (buffer))
15915 buffer = w->buffer;
15916
15917 CHECK_BUFFER (buffer);
15918
15919 if (XBUFFER (buffer) != current_buffer)
15920 {
15921 old_buffer = current_buffer;
15922 set_buffer_internal_1 (XBUFFER (buffer));
15923 }
15924
15925 if (NILP (format) || EQ (format, Qt))
15926 {
15927 face_id = (NILP (format)
15928 ? CURRENT_MODE_LINE_FACE_ID (w)
15929 : HEADER_LINE_FACE_ID);
15930 format = (NILP (format)
15931 ? current_buffer->mode_line_format
15932 : current_buffer->header_line_format);
15933 }
15934
15935 init_iterator (&it, w, -1, -1, NULL, face_id);
15936
15937 if (NILP (no_props))
15938 {
15939 mode_line_string_face
15940 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15941 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15942 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15943
15944 mode_line_string_face_prop
15945 = (NILP (mode_line_string_face) ? Qnil
15946 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15947
15948 /* We need a dummy last element in mode_line_string_list to
15949 indicate we are building the propertized mode-line string.
15950 Using mode_line_string_face_prop here GC protects it. */
15951 mode_line_string_list
15952 = Fcons (mode_line_string_face_prop, Qnil);
15953 frame_title_ptr = NULL;
15954 }
15955 else
15956 {
15957 mode_line_string_face_prop = Qnil;
15958 mode_line_string_list = Qnil;
15959 frame_title_ptr = frame_title_buf;
15960 }
15961
15962 push_frame_kboard (it.f);
15963 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15964 pop_frame_kboard ();
15965
15966 if (old_buffer)
15967 set_buffer_internal_1 (old_buffer);
15968
15969 if (NILP (no_props))
15970 {
15971 Lisp_Object str;
15972 mode_line_string_list = Fnreverse (mode_line_string_list);
15973 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15974 make_string ("", 0));
15975 mode_line_string_face_prop = Qnil;
15976 mode_line_string_list = Qnil;
15977 return str;
15978 }
15979
15980 len = frame_title_ptr - frame_title_buf;
15981 if (len > 0 && frame_title_ptr[-1] == '-')
15982 {
15983 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15984 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15985 ;
15986 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15987 if (len > frame_title_ptr - frame_title_buf)
15988 len = frame_title_ptr - frame_title_buf;
15989 }
15990
15991 frame_title_ptr = NULL;
15992 return make_string (frame_title_buf, len);
15993 }
15994
15995 /* Write a null-terminated, right justified decimal representation of
15996 the positive integer D to BUF using a minimal field width WIDTH. */
15997
15998 static void
15999 pint2str (buf, width, d)
16000 register char *buf;
16001 register int width;
16002 register int d;
16003 {
16004 register char *p = buf;
16005
16006 if (d <= 0)
16007 *p++ = '0';
16008 else
16009 {
16010 while (d > 0)
16011 {
16012 *p++ = d % 10 + '0';
16013 d /= 10;
16014 }
16015 }
16016
16017 for (width -= (int) (p - buf); width > 0; --width)
16018 *p++ = ' ';
16019 *p-- = '\0';
16020 while (p > buf)
16021 {
16022 d = *buf;
16023 *buf++ = *p;
16024 *p-- = d;
16025 }
16026 }
16027
16028 /* Write a null-terminated, right justified decimal and "human
16029 readable" representation of the nonnegative integer D to BUF using
16030 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16031
16032 static const char power_letter[] =
16033 {
16034 0, /* not used */
16035 'k', /* kilo */
16036 'M', /* mega */
16037 'G', /* giga */
16038 'T', /* tera */
16039 'P', /* peta */
16040 'E', /* exa */
16041 'Z', /* zetta */
16042 'Y' /* yotta */
16043 };
16044
16045 static void
16046 pint2hrstr (buf, width, d)
16047 char *buf;
16048 int width;
16049 int d;
16050 {
16051 /* We aim to represent the nonnegative integer D as
16052 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16053 int quotient = d;
16054 int remainder = 0;
16055 /* -1 means: do not use TENTHS. */
16056 int tenths = -1;
16057 int exponent = 0;
16058
16059 /* Length of QUOTIENT.TENTHS as a string. */
16060 int length;
16061
16062 char * psuffix;
16063 char * p;
16064
16065 if (1000 <= quotient)
16066 {
16067 /* Scale to the appropriate EXPONENT. */
16068 do
16069 {
16070 remainder = quotient % 1000;
16071 quotient /= 1000;
16072 exponent++;
16073 }
16074 while (1000 <= quotient);
16075
16076 /* Round to nearest and decide whether to use TENTHS or not. */
16077 if (quotient <= 9)
16078 {
16079 tenths = remainder / 100;
16080 if (50 <= remainder % 100)
16081 {
16082 if (tenths < 9)
16083 tenths++;
16084 else
16085 {
16086 quotient++;
16087 if (quotient == 10)
16088 tenths = -1;
16089 else
16090 tenths = 0;
16091 }
16092 }
16093 }
16094 else
16095 if (500 <= remainder)
16096 {
16097 if (quotient < 999)
16098 quotient++;
16099 else
16100 {
16101 quotient = 1;
16102 exponent++;
16103 tenths = 0;
16104 }
16105 }
16106 }
16107
16108 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16109 if (tenths == -1 && quotient <= 99)
16110 if (quotient <= 9)
16111 length = 1;
16112 else
16113 length = 2;
16114 else
16115 length = 3;
16116 p = psuffix = buf + max (width, length);
16117
16118 /* Print EXPONENT. */
16119 if (exponent)
16120 *psuffix++ = power_letter[exponent];
16121 *psuffix = '\0';
16122
16123 /* Print TENTHS. */
16124 if (tenths >= 0)
16125 {
16126 *--p = '0' + tenths;
16127 *--p = '.';
16128 }
16129
16130 /* Print QUOTIENT. */
16131 do
16132 {
16133 int digit = quotient % 10;
16134 *--p = '0' + digit;
16135 }
16136 while ((quotient /= 10) != 0);
16137
16138 /* Print leading spaces. */
16139 while (buf < p)
16140 *--p = ' ';
16141 }
16142
16143 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16144 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16145 type of CODING_SYSTEM. Return updated pointer into BUF. */
16146
16147 static unsigned char invalid_eol_type[] = "(*invalid*)";
16148
16149 static char *
16150 decode_mode_spec_coding (coding_system, buf, eol_flag)
16151 Lisp_Object coding_system;
16152 register char *buf;
16153 int eol_flag;
16154 {
16155 Lisp_Object val;
16156 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16157 const unsigned char *eol_str;
16158 int eol_str_len;
16159 /* The EOL conversion we are using. */
16160 Lisp_Object eoltype;
16161
16162 val = Fget (coding_system, Qcoding_system);
16163 eoltype = Qnil;
16164
16165 if (!VECTORP (val)) /* Not yet decided. */
16166 {
16167 if (multibyte)
16168 *buf++ = '-';
16169 if (eol_flag)
16170 eoltype = eol_mnemonic_undecided;
16171 /* Don't mention EOL conversion if it isn't decided. */
16172 }
16173 else
16174 {
16175 Lisp_Object eolvalue;
16176
16177 eolvalue = Fget (coding_system, Qeol_type);
16178
16179 if (multibyte)
16180 *buf++ = XFASTINT (AREF (val, 1));
16181
16182 if (eol_flag)
16183 {
16184 /* The EOL conversion that is normal on this system. */
16185
16186 if (NILP (eolvalue)) /* Not yet decided. */
16187 eoltype = eol_mnemonic_undecided;
16188 else if (VECTORP (eolvalue)) /* Not yet decided. */
16189 eoltype = eol_mnemonic_undecided;
16190 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16191 eoltype = (XFASTINT (eolvalue) == 0
16192 ? eol_mnemonic_unix
16193 : (XFASTINT (eolvalue) == 1
16194 ? eol_mnemonic_dos : eol_mnemonic_mac));
16195 }
16196 }
16197
16198 if (eol_flag)
16199 {
16200 /* Mention the EOL conversion if it is not the usual one. */
16201 if (STRINGP (eoltype))
16202 {
16203 eol_str = SDATA (eoltype);
16204 eol_str_len = SBYTES (eoltype);
16205 }
16206 else if (INTEGERP (eoltype)
16207 && CHAR_VALID_P (XINT (eoltype), 0))
16208 {
16209 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16210 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16211 eol_str = tmp;
16212 }
16213 else
16214 {
16215 eol_str = invalid_eol_type;
16216 eol_str_len = sizeof (invalid_eol_type) - 1;
16217 }
16218 bcopy (eol_str, buf, eol_str_len);
16219 buf += eol_str_len;
16220 }
16221
16222 return buf;
16223 }
16224
16225 /* Return a string for the output of a mode line %-spec for window W,
16226 generated by character C. PRECISION >= 0 means don't return a
16227 string longer than that value. FIELD_WIDTH > 0 means pad the
16228 string returned with spaces to that value. Return 1 in *MULTIBYTE
16229 if the result is multibyte text.
16230
16231 Note we operate on the current buffer for most purposes,
16232 the exception being w->base_line_pos. */
16233
16234 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16235
16236 static char *
16237 decode_mode_spec (w, c, field_width, precision, multibyte)
16238 struct window *w;
16239 register int c;
16240 int field_width, precision;
16241 int *multibyte;
16242 {
16243 Lisp_Object obj;
16244 struct frame *f = XFRAME (WINDOW_FRAME (w));
16245 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16246 struct buffer *b = current_buffer;
16247
16248 obj = Qnil;
16249 *multibyte = 0;
16250
16251 switch (c)
16252 {
16253 case '*':
16254 if (!NILP (b->read_only))
16255 return "%";
16256 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16257 return "*";
16258 return "-";
16259
16260 case '+':
16261 /* This differs from %* only for a modified read-only buffer. */
16262 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16263 return "*";
16264 if (!NILP (b->read_only))
16265 return "%";
16266 return "-";
16267
16268 case '&':
16269 /* This differs from %* in ignoring read-only-ness. */
16270 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16271 return "*";
16272 return "-";
16273
16274 case '%':
16275 return "%";
16276
16277 case '[':
16278 {
16279 int i;
16280 char *p;
16281
16282 if (command_loop_level > 5)
16283 return "[[[... ";
16284 p = decode_mode_spec_buf;
16285 for (i = 0; i < command_loop_level; i++)
16286 *p++ = '[';
16287 *p = 0;
16288 return decode_mode_spec_buf;
16289 }
16290
16291 case ']':
16292 {
16293 int i;
16294 char *p;
16295
16296 if (command_loop_level > 5)
16297 return " ...]]]";
16298 p = decode_mode_spec_buf;
16299 for (i = 0; i < command_loop_level; i++)
16300 *p++ = ']';
16301 *p = 0;
16302 return decode_mode_spec_buf;
16303 }
16304
16305 case '-':
16306 {
16307 register int i;
16308
16309 /* Let lots_of_dashes be a string of infinite length. */
16310 if (!NILP (mode_line_string_list))
16311 return "--";
16312 if (field_width <= 0
16313 || field_width > sizeof (lots_of_dashes))
16314 {
16315 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16316 decode_mode_spec_buf[i] = '-';
16317 decode_mode_spec_buf[i] = '\0';
16318 return decode_mode_spec_buf;
16319 }
16320 else
16321 return lots_of_dashes;
16322 }
16323
16324 case 'b':
16325 obj = b->name;
16326 break;
16327
16328 case 'c':
16329 {
16330 int col = (int) current_column (); /* iftc */
16331 w->column_number_displayed = make_number (col);
16332 pint2str (decode_mode_spec_buf, field_width, col);
16333 return decode_mode_spec_buf;
16334 }
16335
16336 case 'F':
16337 /* %F displays the frame name. */
16338 if (!NILP (f->title))
16339 return (char *) SDATA (f->title);
16340 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16341 return (char *) SDATA (f->name);
16342 return "Emacs";
16343
16344 case 'f':
16345 obj = b->filename;
16346 break;
16347
16348 case 'i':
16349 {
16350 int size = ZV - BEGV;
16351 pint2str (decode_mode_spec_buf, field_width, size);
16352 return decode_mode_spec_buf;
16353 }
16354
16355 case 'I':
16356 {
16357 int size = ZV - BEGV;
16358 pint2hrstr (decode_mode_spec_buf, field_width, size);
16359 return decode_mode_spec_buf;
16360 }
16361
16362 case 'l':
16363 {
16364 int startpos = XMARKER (w->start)->charpos;
16365 int startpos_byte = marker_byte_position (w->start);
16366 int line, linepos, linepos_byte, topline;
16367 int nlines, junk;
16368 int height = WINDOW_TOTAL_LINES (w);
16369
16370 /* If we decided that this buffer isn't suitable for line numbers,
16371 don't forget that too fast. */
16372 if (EQ (w->base_line_pos, w->buffer))
16373 goto no_value;
16374 /* But do forget it, if the window shows a different buffer now. */
16375 else if (BUFFERP (w->base_line_pos))
16376 w->base_line_pos = Qnil;
16377
16378 /* If the buffer is very big, don't waste time. */
16379 if (INTEGERP (Vline_number_display_limit)
16380 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16381 {
16382 w->base_line_pos = Qnil;
16383 w->base_line_number = Qnil;
16384 goto no_value;
16385 }
16386
16387 if (!NILP (w->base_line_number)
16388 && !NILP (w->base_line_pos)
16389 && XFASTINT (w->base_line_pos) <= startpos)
16390 {
16391 line = XFASTINT (w->base_line_number);
16392 linepos = XFASTINT (w->base_line_pos);
16393 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16394 }
16395 else
16396 {
16397 line = 1;
16398 linepos = BUF_BEGV (b);
16399 linepos_byte = BUF_BEGV_BYTE (b);
16400 }
16401
16402 /* Count lines from base line to window start position. */
16403 nlines = display_count_lines (linepos, linepos_byte,
16404 startpos_byte,
16405 startpos, &junk);
16406
16407 topline = nlines + line;
16408
16409 /* Determine a new base line, if the old one is too close
16410 or too far away, or if we did not have one.
16411 "Too close" means it's plausible a scroll-down would
16412 go back past it. */
16413 if (startpos == BUF_BEGV (b))
16414 {
16415 w->base_line_number = make_number (topline);
16416 w->base_line_pos = make_number (BUF_BEGV (b));
16417 }
16418 else if (nlines < height + 25 || nlines > height * 3 + 50
16419 || linepos == BUF_BEGV (b))
16420 {
16421 int limit = BUF_BEGV (b);
16422 int limit_byte = BUF_BEGV_BYTE (b);
16423 int position;
16424 int distance = (height * 2 + 30) * line_number_display_limit_width;
16425
16426 if (startpos - distance > limit)
16427 {
16428 limit = startpos - distance;
16429 limit_byte = CHAR_TO_BYTE (limit);
16430 }
16431
16432 nlines = display_count_lines (startpos, startpos_byte,
16433 limit_byte,
16434 - (height * 2 + 30),
16435 &position);
16436 /* If we couldn't find the lines we wanted within
16437 line_number_display_limit_width chars per line,
16438 give up on line numbers for this window. */
16439 if (position == limit_byte && limit == startpos - distance)
16440 {
16441 w->base_line_pos = w->buffer;
16442 w->base_line_number = Qnil;
16443 goto no_value;
16444 }
16445
16446 w->base_line_number = make_number (topline - nlines);
16447 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16448 }
16449
16450 /* Now count lines from the start pos to point. */
16451 nlines = display_count_lines (startpos, startpos_byte,
16452 PT_BYTE, PT, &junk);
16453
16454 /* Record that we did display the line number. */
16455 line_number_displayed = 1;
16456
16457 /* Make the string to show. */
16458 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16459 return decode_mode_spec_buf;
16460 no_value:
16461 {
16462 char* p = decode_mode_spec_buf;
16463 int pad = field_width - 2;
16464 while (pad-- > 0)
16465 *p++ = ' ';
16466 *p++ = '?';
16467 *p++ = '?';
16468 *p = '\0';
16469 return decode_mode_spec_buf;
16470 }
16471 }
16472 break;
16473
16474 case 'm':
16475 obj = b->mode_name;
16476 break;
16477
16478 case 'n':
16479 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16480 return " Narrow";
16481 break;
16482
16483 case 'p':
16484 {
16485 int pos = marker_position (w->start);
16486 int total = BUF_ZV (b) - BUF_BEGV (b);
16487
16488 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16489 {
16490 if (pos <= BUF_BEGV (b))
16491 return "All";
16492 else
16493 return "Bottom";
16494 }
16495 else if (pos <= BUF_BEGV (b))
16496 return "Top";
16497 else
16498 {
16499 if (total > 1000000)
16500 /* Do it differently for a large value, to avoid overflow. */
16501 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16502 else
16503 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16504 /* We can't normally display a 3-digit number,
16505 so get us a 2-digit number that is close. */
16506 if (total == 100)
16507 total = 99;
16508 sprintf (decode_mode_spec_buf, "%2d%%", total);
16509 return decode_mode_spec_buf;
16510 }
16511 }
16512
16513 /* Display percentage of size above the bottom of the screen. */
16514 case 'P':
16515 {
16516 int toppos = marker_position (w->start);
16517 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16518 int total = BUF_ZV (b) - BUF_BEGV (b);
16519
16520 if (botpos >= BUF_ZV (b))
16521 {
16522 if (toppos <= BUF_BEGV (b))
16523 return "All";
16524 else
16525 return "Bottom";
16526 }
16527 else
16528 {
16529 if (total > 1000000)
16530 /* Do it differently for a large value, to avoid overflow. */
16531 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16532 else
16533 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16534 /* We can't normally display a 3-digit number,
16535 so get us a 2-digit number that is close. */
16536 if (total == 100)
16537 total = 99;
16538 if (toppos <= BUF_BEGV (b))
16539 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16540 else
16541 sprintf (decode_mode_spec_buf, "%2d%%", total);
16542 return decode_mode_spec_buf;
16543 }
16544 }
16545
16546 case 's':
16547 /* status of process */
16548 obj = Fget_buffer_process (Fcurrent_buffer ());
16549 if (NILP (obj))
16550 return "no process";
16551 #ifdef subprocesses
16552 obj = Fsymbol_name (Fprocess_status (obj));
16553 #endif
16554 break;
16555
16556 case 't': /* indicate TEXT or BINARY */
16557 #ifdef MODE_LINE_BINARY_TEXT
16558 return MODE_LINE_BINARY_TEXT (b);
16559 #else
16560 return "T";
16561 #endif
16562
16563 case 'z':
16564 /* coding-system (not including end-of-line format) */
16565 case 'Z':
16566 /* coding-system (including end-of-line type) */
16567 {
16568 int eol_flag = (c == 'Z');
16569 char *p = decode_mode_spec_buf;
16570
16571 if (! FRAME_WINDOW_P (f))
16572 {
16573 /* No need to mention EOL here--the terminal never needs
16574 to do EOL conversion. */
16575 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
16576 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
16577 }
16578 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16579 p, eol_flag);
16580
16581 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16582 #ifdef subprocesses
16583 obj = Fget_buffer_process (Fcurrent_buffer ());
16584 if (PROCESSP (obj))
16585 {
16586 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16587 p, eol_flag);
16588 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16589 p, eol_flag);
16590 }
16591 #endif /* subprocesses */
16592 #endif /* 0 */
16593 *p = 0;
16594 return decode_mode_spec_buf;
16595 }
16596 }
16597
16598 if (STRINGP (obj))
16599 {
16600 *multibyte = STRING_MULTIBYTE (obj);
16601 return (char *) SDATA (obj);
16602 }
16603 else
16604 return "";
16605 }
16606
16607
16608 /* Count up to COUNT lines starting from START / START_BYTE.
16609 But don't go beyond LIMIT_BYTE.
16610 Return the number of lines thus found (always nonnegative).
16611
16612 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16613
16614 static int
16615 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16616 int start, start_byte, limit_byte, count;
16617 int *byte_pos_ptr;
16618 {
16619 register unsigned char *cursor;
16620 unsigned char *base;
16621
16622 register int ceiling;
16623 register unsigned char *ceiling_addr;
16624 int orig_count = count;
16625
16626 /* If we are not in selective display mode,
16627 check only for newlines. */
16628 int selective_display = (!NILP (current_buffer->selective_display)
16629 && !INTEGERP (current_buffer->selective_display));
16630
16631 if (count > 0)
16632 {
16633 while (start_byte < limit_byte)
16634 {
16635 ceiling = BUFFER_CEILING_OF (start_byte);
16636 ceiling = min (limit_byte - 1, ceiling);
16637 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16638 base = (cursor = BYTE_POS_ADDR (start_byte));
16639 while (1)
16640 {
16641 if (selective_display)
16642 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16643 ;
16644 else
16645 while (*cursor != '\n' && ++cursor != ceiling_addr)
16646 ;
16647
16648 if (cursor != ceiling_addr)
16649 {
16650 if (--count == 0)
16651 {
16652 start_byte += cursor - base + 1;
16653 *byte_pos_ptr = start_byte;
16654 return orig_count;
16655 }
16656 else
16657 if (++cursor == ceiling_addr)
16658 break;
16659 }
16660 else
16661 break;
16662 }
16663 start_byte += cursor - base;
16664 }
16665 }
16666 else
16667 {
16668 while (start_byte > limit_byte)
16669 {
16670 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16671 ceiling = max (limit_byte, ceiling);
16672 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16673 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16674 while (1)
16675 {
16676 if (selective_display)
16677 while (--cursor != ceiling_addr
16678 && *cursor != '\n' && *cursor != 015)
16679 ;
16680 else
16681 while (--cursor != ceiling_addr && *cursor != '\n')
16682 ;
16683
16684 if (cursor != ceiling_addr)
16685 {
16686 if (++count == 0)
16687 {
16688 start_byte += cursor - base + 1;
16689 *byte_pos_ptr = start_byte;
16690 /* When scanning backwards, we should
16691 not count the newline posterior to which we stop. */
16692 return - orig_count - 1;
16693 }
16694 }
16695 else
16696 break;
16697 }
16698 /* Here we add 1 to compensate for the last decrement
16699 of CURSOR, which took it past the valid range. */
16700 start_byte += cursor - base + 1;
16701 }
16702 }
16703
16704 *byte_pos_ptr = limit_byte;
16705
16706 if (count < 0)
16707 return - orig_count + count;
16708 return orig_count - count;
16709
16710 }
16711
16712
16713 \f
16714 /***********************************************************************
16715 Displaying strings
16716 ***********************************************************************/
16717
16718 /* Display a NUL-terminated string, starting with index START.
16719
16720 If STRING is non-null, display that C string. Otherwise, the Lisp
16721 string LISP_STRING is displayed.
16722
16723 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16724 FACE_STRING. Display STRING or LISP_STRING with the face at
16725 FACE_STRING_POS in FACE_STRING:
16726
16727 Display the string in the environment given by IT, but use the
16728 standard display table, temporarily.
16729
16730 FIELD_WIDTH is the minimum number of output glyphs to produce.
16731 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16732 with spaces. If STRING has more characters, more than FIELD_WIDTH
16733 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16734
16735 PRECISION is the maximum number of characters to output from
16736 STRING. PRECISION < 0 means don't truncate the string.
16737
16738 This is roughly equivalent to printf format specifiers:
16739
16740 FIELD_WIDTH PRECISION PRINTF
16741 ----------------------------------------
16742 -1 -1 %s
16743 -1 10 %.10s
16744 10 -1 %10s
16745 20 10 %20.10s
16746
16747 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16748 display them, and < 0 means obey the current buffer's value of
16749 enable_multibyte_characters.
16750
16751 Value is the number of glyphs produced. */
16752
16753 static int
16754 display_string (string, lisp_string, face_string, face_string_pos,
16755 start, it, field_width, precision, max_x, multibyte)
16756 unsigned char *string;
16757 Lisp_Object lisp_string;
16758 Lisp_Object face_string;
16759 int face_string_pos;
16760 int start;
16761 struct it *it;
16762 int field_width, precision, max_x;
16763 int multibyte;
16764 {
16765 int hpos_at_start = it->hpos;
16766 int saved_face_id = it->face_id;
16767 struct glyph_row *row = it->glyph_row;
16768
16769 /* Initialize the iterator IT for iteration over STRING beginning
16770 with index START. */
16771 reseat_to_string (it, string, lisp_string, start,
16772 precision, field_width, multibyte);
16773
16774 /* If displaying STRING, set up the face of the iterator
16775 from LISP_STRING, if that's given. */
16776 if (STRINGP (face_string))
16777 {
16778 int endptr;
16779 struct face *face;
16780
16781 it->face_id
16782 = face_at_string_position (it->w, face_string, face_string_pos,
16783 0, it->region_beg_charpos,
16784 it->region_end_charpos,
16785 &endptr, it->base_face_id, 0);
16786 face = FACE_FROM_ID (it->f, it->face_id);
16787 it->face_box_p = face->box != FACE_NO_BOX;
16788 }
16789
16790 /* Set max_x to the maximum allowed X position. Don't let it go
16791 beyond the right edge of the window. */
16792 if (max_x <= 0)
16793 max_x = it->last_visible_x;
16794 else
16795 max_x = min (max_x, it->last_visible_x);
16796
16797 /* Skip over display elements that are not visible. because IT->w is
16798 hscrolled. */
16799 if (it->current_x < it->first_visible_x)
16800 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16801 MOVE_TO_POS | MOVE_TO_X);
16802
16803 row->ascent = it->max_ascent;
16804 row->height = it->max_ascent + it->max_descent;
16805 row->phys_ascent = it->max_phys_ascent;
16806 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16807 row->extra_line_spacing = it->max_extra_line_spacing;
16808
16809 /* This condition is for the case that we are called with current_x
16810 past last_visible_x. */
16811 while (it->current_x < max_x)
16812 {
16813 int x_before, x, n_glyphs_before, i, nglyphs;
16814
16815 /* Get the next display element. */
16816 if (!get_next_display_element (it))
16817 break;
16818
16819 /* Produce glyphs. */
16820 x_before = it->current_x;
16821 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16822 PRODUCE_GLYPHS (it);
16823
16824 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16825 i = 0;
16826 x = x_before;
16827 while (i < nglyphs)
16828 {
16829 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16830
16831 if (!it->truncate_lines_p
16832 && x + glyph->pixel_width > max_x)
16833 {
16834 /* End of continued line or max_x reached. */
16835 if (CHAR_GLYPH_PADDING_P (*glyph))
16836 {
16837 /* A wide character is unbreakable. */
16838 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16839 it->current_x = x_before;
16840 }
16841 else
16842 {
16843 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16844 it->current_x = x;
16845 }
16846 break;
16847 }
16848 else if (x + glyph->pixel_width > it->first_visible_x)
16849 {
16850 /* Glyph is at least partially visible. */
16851 ++it->hpos;
16852 if (x < it->first_visible_x)
16853 it->glyph_row->x = x - it->first_visible_x;
16854 }
16855 else
16856 {
16857 /* Glyph is off the left margin of the display area.
16858 Should not happen. */
16859 abort ();
16860 }
16861
16862 row->ascent = max (row->ascent, it->max_ascent);
16863 row->height = max (row->height, it->max_ascent + it->max_descent);
16864 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16865 row->phys_height = max (row->phys_height,
16866 it->max_phys_ascent + it->max_phys_descent);
16867 row->extra_line_spacing = max (row->extra_line_spacing,
16868 it->max_extra_line_spacing);
16869 x += glyph->pixel_width;
16870 ++i;
16871 }
16872
16873 /* Stop if max_x reached. */
16874 if (i < nglyphs)
16875 break;
16876
16877 /* Stop at line ends. */
16878 if (ITERATOR_AT_END_OF_LINE_P (it))
16879 {
16880 it->continuation_lines_width = 0;
16881 break;
16882 }
16883
16884 set_iterator_to_next (it, 1);
16885
16886 /* Stop if truncating at the right edge. */
16887 if (it->truncate_lines_p
16888 && it->current_x >= it->last_visible_x)
16889 {
16890 /* Add truncation mark, but don't do it if the line is
16891 truncated at a padding space. */
16892 if (IT_CHARPOS (*it) < it->string_nchars)
16893 {
16894 if (!FRAME_WINDOW_P (it->f))
16895 {
16896 int i, n;
16897
16898 if (it->current_x > it->last_visible_x)
16899 {
16900 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16901 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16902 break;
16903 for (n = row->used[TEXT_AREA]; i < n; ++i)
16904 {
16905 row->used[TEXT_AREA] = i;
16906 produce_special_glyphs (it, IT_TRUNCATION);
16907 }
16908 }
16909 produce_special_glyphs (it, IT_TRUNCATION);
16910 }
16911 it->glyph_row->truncated_on_right_p = 1;
16912 }
16913 break;
16914 }
16915 }
16916
16917 /* Maybe insert a truncation at the left. */
16918 if (it->first_visible_x
16919 && IT_CHARPOS (*it) > 0)
16920 {
16921 if (!FRAME_WINDOW_P (it->f))
16922 insert_left_trunc_glyphs (it);
16923 it->glyph_row->truncated_on_left_p = 1;
16924 }
16925
16926 it->face_id = saved_face_id;
16927
16928 /* Value is number of columns displayed. */
16929 return it->hpos - hpos_at_start;
16930 }
16931
16932
16933 \f
16934 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16935 appears as an element of LIST or as the car of an element of LIST.
16936 If PROPVAL is a list, compare each element against LIST in that
16937 way, and return 1/2 if any element of PROPVAL is found in LIST.
16938 Otherwise return 0. This function cannot quit.
16939 The return value is 2 if the text is invisible but with an ellipsis
16940 and 1 if it's invisible and without an ellipsis. */
16941
16942 int
16943 invisible_p (propval, list)
16944 register Lisp_Object propval;
16945 Lisp_Object list;
16946 {
16947 register Lisp_Object tail, proptail;
16948
16949 for (tail = list; CONSP (tail); tail = XCDR (tail))
16950 {
16951 register Lisp_Object tem;
16952 tem = XCAR (tail);
16953 if (EQ (propval, tem))
16954 return 1;
16955 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16956 return NILP (XCDR (tem)) ? 1 : 2;
16957 }
16958
16959 if (CONSP (propval))
16960 {
16961 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16962 {
16963 Lisp_Object propelt;
16964 propelt = XCAR (proptail);
16965 for (tail = list; CONSP (tail); tail = XCDR (tail))
16966 {
16967 register Lisp_Object tem;
16968 tem = XCAR (tail);
16969 if (EQ (propelt, tem))
16970 return 1;
16971 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16972 return NILP (XCDR (tem)) ? 1 : 2;
16973 }
16974 }
16975 }
16976
16977 return 0;
16978 }
16979
16980 /* Calculate a width or height in pixels from a specification using
16981 the following elements:
16982
16983 SPEC ::=
16984 NUM - a (fractional) multiple of the default font width/height
16985 (NUM) - specifies exactly NUM pixels
16986 UNIT - a fixed number of pixels, see below.
16987 ELEMENT - size of a display element in pixels, see below.
16988 (NUM . SPEC) - equals NUM * SPEC
16989 (+ SPEC SPEC ...) - add pixel values
16990 (- SPEC SPEC ...) - subtract pixel values
16991 (- SPEC) - negate pixel value
16992
16993 NUM ::=
16994 INT or FLOAT - a number constant
16995 SYMBOL - use symbol's (buffer local) variable binding.
16996
16997 UNIT ::=
16998 in - pixels per inch *)
16999 mm - pixels per 1/1000 meter *)
17000 cm - pixels per 1/100 meter *)
17001 width - width of current font in pixels.
17002 height - height of current font in pixels.
17003
17004 *) using the ratio(s) defined in display-pixels-per-inch.
17005
17006 ELEMENT ::=
17007
17008 left-fringe - left fringe width in pixels
17009 right-fringe - right fringe width in pixels
17010
17011 left-margin - left margin width in pixels
17012 right-margin - right margin width in pixels
17013
17014 scroll-bar - scroll-bar area width in pixels
17015
17016 Examples:
17017
17018 Pixels corresponding to 5 inches:
17019 (5 . in)
17020
17021 Total width of non-text areas on left side of window (if scroll-bar is on left):
17022 '(space :width (+ left-fringe left-margin scroll-bar))
17023
17024 Align to first text column (in header line):
17025 '(space :align-to 0)
17026
17027 Align to middle of text area minus half the width of variable `my-image'
17028 containing a loaded image:
17029 '(space :align-to (0.5 . (- text my-image)))
17030
17031 Width of left margin minus width of 1 character in the default font:
17032 '(space :width (- left-margin 1))
17033
17034 Width of left margin minus width of 2 characters in the current font:
17035 '(space :width (- left-margin (2 . width)))
17036
17037 Center 1 character over left-margin (in header line):
17038 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17039
17040 Different ways to express width of left fringe plus left margin minus one pixel:
17041 '(space :width (- (+ left-fringe left-margin) (1)))
17042 '(space :width (+ left-fringe left-margin (- (1))))
17043 '(space :width (+ left-fringe left-margin (-1)))
17044
17045 */
17046
17047 #define NUMVAL(X) \
17048 ((INTEGERP (X) || FLOATP (X)) \
17049 ? XFLOATINT (X) \
17050 : - 1)
17051
17052 int
17053 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17054 double *res;
17055 struct it *it;
17056 Lisp_Object prop;
17057 void *font;
17058 int width_p, *align_to;
17059 {
17060 double pixels;
17061
17062 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17063 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17064
17065 if (NILP (prop))
17066 return OK_PIXELS (0);
17067
17068 if (SYMBOLP (prop))
17069 {
17070 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17071 {
17072 char *unit = SDATA (SYMBOL_NAME (prop));
17073
17074 if (unit[0] == 'i' && unit[1] == 'n')
17075 pixels = 1.0;
17076 else if (unit[0] == 'm' && unit[1] == 'm')
17077 pixels = 25.4;
17078 else if (unit[0] == 'c' && unit[1] == 'm')
17079 pixels = 2.54;
17080 else
17081 pixels = 0;
17082 if (pixels > 0)
17083 {
17084 double ppi;
17085 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17086 || (CONSP (Vdisplay_pixels_per_inch)
17087 && (ppi = (width_p
17088 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17089 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17090 ppi > 0)))
17091 return OK_PIXELS (ppi / pixels);
17092
17093 return 0;
17094 }
17095 }
17096
17097 #ifdef HAVE_WINDOW_SYSTEM
17098 if (EQ (prop, Qheight))
17099 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17100 if (EQ (prop, Qwidth))
17101 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17102 #else
17103 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17104 return OK_PIXELS (1);
17105 #endif
17106
17107 if (EQ (prop, Qtext))
17108 return OK_PIXELS (width_p
17109 ? window_box_width (it->w, TEXT_AREA)
17110 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17111
17112 if (align_to && *align_to < 0)
17113 {
17114 *res = 0;
17115 if (EQ (prop, Qleft))
17116 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17117 if (EQ (prop, Qright))
17118 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17119 if (EQ (prop, Qcenter))
17120 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17121 + window_box_width (it->w, TEXT_AREA) / 2);
17122 if (EQ (prop, Qleft_fringe))
17123 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17124 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17125 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17126 if (EQ (prop, Qright_fringe))
17127 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17128 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17129 : window_box_right_offset (it->w, TEXT_AREA));
17130 if (EQ (prop, Qleft_margin))
17131 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17132 if (EQ (prop, Qright_margin))
17133 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17134 if (EQ (prop, Qscroll_bar))
17135 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17136 ? 0
17137 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17138 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17139 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17140 : 0)));
17141 }
17142 else
17143 {
17144 if (EQ (prop, Qleft_fringe))
17145 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17146 if (EQ (prop, Qright_fringe))
17147 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17148 if (EQ (prop, Qleft_margin))
17149 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17150 if (EQ (prop, Qright_margin))
17151 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17152 if (EQ (prop, Qscroll_bar))
17153 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17154 }
17155
17156 prop = Fbuffer_local_value (prop, it->w->buffer);
17157 }
17158
17159 if (INTEGERP (prop) || FLOATP (prop))
17160 {
17161 int base_unit = (width_p
17162 ? FRAME_COLUMN_WIDTH (it->f)
17163 : FRAME_LINE_HEIGHT (it->f));
17164 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17165 }
17166
17167 if (CONSP (prop))
17168 {
17169 Lisp_Object car = XCAR (prop);
17170 Lisp_Object cdr = XCDR (prop);
17171
17172 if (SYMBOLP (car))
17173 {
17174 #ifdef HAVE_WINDOW_SYSTEM
17175 if (valid_image_p (prop))
17176 {
17177 int id = lookup_image (it->f, prop);
17178 struct image *img = IMAGE_FROM_ID (it->f, id);
17179
17180 return OK_PIXELS (width_p ? img->width : img->height);
17181 }
17182 #endif
17183 if (EQ (car, Qplus) || EQ (car, Qminus))
17184 {
17185 int first = 1;
17186 double px;
17187
17188 pixels = 0;
17189 while (CONSP (cdr))
17190 {
17191 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17192 font, width_p, align_to))
17193 return 0;
17194 if (first)
17195 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17196 else
17197 pixels += px;
17198 cdr = XCDR (cdr);
17199 }
17200 if (EQ (car, Qminus))
17201 pixels = -pixels;
17202 return OK_PIXELS (pixels);
17203 }
17204
17205 car = Fbuffer_local_value (car, it->w->buffer);
17206 }
17207
17208 if (INTEGERP (car) || FLOATP (car))
17209 {
17210 double fact;
17211 pixels = XFLOATINT (car);
17212 if (NILP (cdr))
17213 return OK_PIXELS (pixels);
17214 if (calc_pixel_width_or_height (&fact, it, cdr,
17215 font, width_p, align_to))
17216 return OK_PIXELS (pixels * fact);
17217 return 0;
17218 }
17219
17220 return 0;
17221 }
17222
17223 return 0;
17224 }
17225
17226 \f
17227 /***********************************************************************
17228 Glyph Display
17229 ***********************************************************************/
17230
17231 #ifdef HAVE_WINDOW_SYSTEM
17232
17233 #if GLYPH_DEBUG
17234
17235 void
17236 dump_glyph_string (s)
17237 struct glyph_string *s;
17238 {
17239 fprintf (stderr, "glyph string\n");
17240 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17241 s->x, s->y, s->width, s->height);
17242 fprintf (stderr, " ybase = %d\n", s->ybase);
17243 fprintf (stderr, " hl = %d\n", s->hl);
17244 fprintf (stderr, " left overhang = %d, right = %d\n",
17245 s->left_overhang, s->right_overhang);
17246 fprintf (stderr, " nchars = %d\n", s->nchars);
17247 fprintf (stderr, " extends to end of line = %d\n",
17248 s->extends_to_end_of_line_p);
17249 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17250 fprintf (stderr, " bg width = %d\n", s->background_width);
17251 }
17252
17253 #endif /* GLYPH_DEBUG */
17254
17255 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17256 of XChar2b structures for S; it can't be allocated in
17257 init_glyph_string because it must be allocated via `alloca'. W
17258 is the window on which S is drawn. ROW and AREA are the glyph row
17259 and area within the row from which S is constructed. START is the
17260 index of the first glyph structure covered by S. HL is a
17261 face-override for drawing S. */
17262
17263 #ifdef HAVE_NTGUI
17264 #define OPTIONAL_HDC(hdc) hdc,
17265 #define DECLARE_HDC(hdc) HDC hdc;
17266 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17267 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17268 #endif
17269
17270 #ifndef OPTIONAL_HDC
17271 #define OPTIONAL_HDC(hdc)
17272 #define DECLARE_HDC(hdc)
17273 #define ALLOCATE_HDC(hdc, f)
17274 #define RELEASE_HDC(hdc, f)
17275 #endif
17276
17277 static void
17278 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17279 struct glyph_string *s;
17280 DECLARE_HDC (hdc)
17281 XChar2b *char2b;
17282 struct window *w;
17283 struct glyph_row *row;
17284 enum glyph_row_area area;
17285 int start;
17286 enum draw_glyphs_face hl;
17287 {
17288 bzero (s, sizeof *s);
17289 s->w = w;
17290 s->f = XFRAME (w->frame);
17291 #ifdef HAVE_NTGUI
17292 s->hdc = hdc;
17293 #endif
17294 s->display = FRAME_X_DISPLAY (s->f);
17295 s->window = FRAME_X_WINDOW (s->f);
17296 s->char2b = char2b;
17297 s->hl = hl;
17298 s->row = row;
17299 s->area = area;
17300 s->first_glyph = row->glyphs[area] + start;
17301 s->height = row->height;
17302 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17303
17304 /* Display the internal border below the tool-bar window. */
17305 if (WINDOWP (s->f->tool_bar_window)
17306 && s->w == XWINDOW (s->f->tool_bar_window))
17307 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17308
17309 s->ybase = s->y + row->ascent;
17310 }
17311
17312
17313 /* Append the list of glyph strings with head H and tail T to the list
17314 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17315
17316 static INLINE void
17317 append_glyph_string_lists (head, tail, h, t)
17318 struct glyph_string **head, **tail;
17319 struct glyph_string *h, *t;
17320 {
17321 if (h)
17322 {
17323 if (*head)
17324 (*tail)->next = h;
17325 else
17326 *head = h;
17327 h->prev = *tail;
17328 *tail = t;
17329 }
17330 }
17331
17332
17333 /* Prepend the list of glyph strings with head H and tail T to the
17334 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17335 result. */
17336
17337 static INLINE void
17338 prepend_glyph_string_lists (head, tail, h, t)
17339 struct glyph_string **head, **tail;
17340 struct glyph_string *h, *t;
17341 {
17342 if (h)
17343 {
17344 if (*head)
17345 (*head)->prev = t;
17346 else
17347 *tail = t;
17348 t->next = *head;
17349 *head = h;
17350 }
17351 }
17352
17353
17354 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17355 Set *HEAD and *TAIL to the resulting list. */
17356
17357 static INLINE void
17358 append_glyph_string (head, tail, s)
17359 struct glyph_string **head, **tail;
17360 struct glyph_string *s;
17361 {
17362 s->next = s->prev = NULL;
17363 append_glyph_string_lists (head, tail, s, s);
17364 }
17365
17366
17367 /* Get face and two-byte form of character glyph GLYPH on frame F.
17368 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17369 a pointer to a realized face that is ready for display. */
17370
17371 static INLINE struct face *
17372 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17373 struct frame *f;
17374 struct glyph *glyph;
17375 XChar2b *char2b;
17376 int *two_byte_p;
17377 {
17378 struct face *face;
17379
17380 xassert (glyph->type == CHAR_GLYPH);
17381 face = FACE_FROM_ID (f, glyph->face_id);
17382
17383 if (two_byte_p)
17384 *two_byte_p = 0;
17385
17386 if (!glyph->multibyte_p)
17387 {
17388 /* Unibyte case. We don't have to encode, but we have to make
17389 sure to use a face suitable for unibyte. */
17390 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17391 }
17392 else if (glyph->u.ch < 128
17393 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17394 {
17395 /* Case of ASCII in a face known to fit ASCII. */
17396 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17397 }
17398 else
17399 {
17400 int c1, c2, charset;
17401
17402 /* Split characters into bytes. If c2 is -1 afterwards, C is
17403 really a one-byte character so that byte1 is zero. */
17404 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17405 if (c2 > 0)
17406 STORE_XCHAR2B (char2b, c1, c2);
17407 else
17408 STORE_XCHAR2B (char2b, 0, c1);
17409
17410 /* Maybe encode the character in *CHAR2B. */
17411 if (charset != CHARSET_ASCII)
17412 {
17413 struct font_info *font_info
17414 = FONT_INFO_FROM_ID (f, face->font_info_id);
17415 if (font_info)
17416 glyph->font_type
17417 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17418 }
17419 }
17420
17421 /* Make sure X resources of the face are allocated. */
17422 xassert (face != NULL);
17423 PREPARE_FACE_FOR_DISPLAY (f, face);
17424 return face;
17425 }
17426
17427
17428 /* Fill glyph string S with composition components specified by S->cmp.
17429
17430 FACES is an array of faces for all components of this composition.
17431 S->gidx is the index of the first component for S.
17432 OVERLAPS_P non-zero means S should draw the foreground only, and
17433 use its physical height for clipping.
17434
17435 Value is the index of a component not in S. */
17436
17437 static int
17438 fill_composite_glyph_string (s, faces, overlaps_p)
17439 struct glyph_string *s;
17440 struct face **faces;
17441 int overlaps_p;
17442 {
17443 int i;
17444
17445 xassert (s);
17446
17447 s->for_overlaps_p = overlaps_p;
17448
17449 s->face = faces[s->gidx];
17450 s->font = s->face->font;
17451 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17452
17453 /* For all glyphs of this composition, starting at the offset
17454 S->gidx, until we reach the end of the definition or encounter a
17455 glyph that requires the different face, add it to S. */
17456 ++s->nchars;
17457 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17458 ++s->nchars;
17459
17460 /* All glyph strings for the same composition has the same width,
17461 i.e. the width set for the first component of the composition. */
17462
17463 s->width = s->first_glyph->pixel_width;
17464
17465 /* If the specified font could not be loaded, use the frame's
17466 default font, but record the fact that we couldn't load it in
17467 the glyph string so that we can draw rectangles for the
17468 characters of the glyph string. */
17469 if (s->font == NULL)
17470 {
17471 s->font_not_found_p = 1;
17472 s->font = FRAME_FONT (s->f);
17473 }
17474
17475 /* Adjust base line for subscript/superscript text. */
17476 s->ybase += s->first_glyph->voffset;
17477
17478 xassert (s->face && s->face->gc);
17479
17480 /* This glyph string must always be drawn with 16-bit functions. */
17481 s->two_byte_p = 1;
17482
17483 return s->gidx + s->nchars;
17484 }
17485
17486
17487 /* Fill glyph string S from a sequence of character glyphs.
17488
17489 FACE_ID is the face id of the string. START is the index of the
17490 first glyph to consider, END is the index of the last + 1.
17491 OVERLAPS_P non-zero means S should draw the foreground only, and
17492 use its physical height for clipping.
17493
17494 Value is the index of the first glyph not in S. */
17495
17496 static int
17497 fill_glyph_string (s, face_id, start, end, overlaps_p)
17498 struct glyph_string *s;
17499 int face_id;
17500 int start, end, overlaps_p;
17501 {
17502 struct glyph *glyph, *last;
17503 int voffset;
17504 int glyph_not_available_p;
17505
17506 xassert (s->f == XFRAME (s->w->frame));
17507 xassert (s->nchars == 0);
17508 xassert (start >= 0 && end > start);
17509
17510 s->for_overlaps_p = overlaps_p,
17511 glyph = s->row->glyphs[s->area] + start;
17512 last = s->row->glyphs[s->area] + end;
17513 voffset = glyph->voffset;
17514
17515 glyph_not_available_p = glyph->glyph_not_available_p;
17516
17517 while (glyph < last
17518 && glyph->type == CHAR_GLYPH
17519 && glyph->voffset == voffset
17520 /* Same face id implies same font, nowadays. */
17521 && glyph->face_id == face_id
17522 && glyph->glyph_not_available_p == glyph_not_available_p)
17523 {
17524 int two_byte_p;
17525
17526 s->face = get_glyph_face_and_encoding (s->f, glyph,
17527 s->char2b + s->nchars,
17528 &two_byte_p);
17529 s->two_byte_p = two_byte_p;
17530 ++s->nchars;
17531 xassert (s->nchars <= end - start);
17532 s->width += glyph->pixel_width;
17533 ++glyph;
17534 }
17535
17536 s->font = s->face->font;
17537 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17538
17539 /* If the specified font could not be loaded, use the frame's font,
17540 but record the fact that we couldn't load it in
17541 S->font_not_found_p so that we can draw rectangles for the
17542 characters of the glyph string. */
17543 if (s->font == NULL || glyph_not_available_p)
17544 {
17545 s->font_not_found_p = 1;
17546 s->font = FRAME_FONT (s->f);
17547 }
17548
17549 /* Adjust base line for subscript/superscript text. */
17550 s->ybase += voffset;
17551
17552 xassert (s->face && s->face->gc);
17553 return glyph - s->row->glyphs[s->area];
17554 }
17555
17556
17557 /* Fill glyph string S from image glyph S->first_glyph. */
17558
17559 static void
17560 fill_image_glyph_string (s)
17561 struct glyph_string *s;
17562 {
17563 xassert (s->first_glyph->type == IMAGE_GLYPH);
17564 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17565 xassert (s->img);
17566 s->slice = s->first_glyph->slice;
17567 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17568 s->font = s->face->font;
17569 s->width = s->first_glyph->pixel_width;
17570
17571 /* Adjust base line for subscript/superscript text. */
17572 s->ybase += s->first_glyph->voffset;
17573 }
17574
17575
17576 /* Fill glyph string S from a sequence of stretch glyphs.
17577
17578 ROW is the glyph row in which the glyphs are found, AREA is the
17579 area within the row. START is the index of the first glyph to
17580 consider, END is the index of the last + 1.
17581
17582 Value is the index of the first glyph not in S. */
17583
17584 static int
17585 fill_stretch_glyph_string (s, row, area, start, end)
17586 struct glyph_string *s;
17587 struct glyph_row *row;
17588 enum glyph_row_area area;
17589 int start, end;
17590 {
17591 struct glyph *glyph, *last;
17592 int voffset, face_id;
17593
17594 xassert (s->first_glyph->type == STRETCH_GLYPH);
17595
17596 glyph = s->row->glyphs[s->area] + start;
17597 last = s->row->glyphs[s->area] + end;
17598 face_id = glyph->face_id;
17599 s->face = FACE_FROM_ID (s->f, face_id);
17600 s->font = s->face->font;
17601 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17602 s->width = glyph->pixel_width;
17603 voffset = glyph->voffset;
17604
17605 for (++glyph;
17606 (glyph < last
17607 && glyph->type == STRETCH_GLYPH
17608 && glyph->voffset == voffset
17609 && glyph->face_id == face_id);
17610 ++glyph)
17611 s->width += glyph->pixel_width;
17612
17613 /* Adjust base line for subscript/superscript text. */
17614 s->ybase += voffset;
17615
17616 /* The case that face->gc == 0 is handled when drawing the glyph
17617 string by calling PREPARE_FACE_FOR_DISPLAY. */
17618 xassert (s->face);
17619 return glyph - s->row->glyphs[s->area];
17620 }
17621
17622
17623 /* EXPORT for RIF:
17624 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17625 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17626 assumed to be zero. */
17627
17628 void
17629 x_get_glyph_overhangs (glyph, f, left, right)
17630 struct glyph *glyph;
17631 struct frame *f;
17632 int *left, *right;
17633 {
17634 *left = *right = 0;
17635
17636 if (glyph->type == CHAR_GLYPH)
17637 {
17638 XFontStruct *font;
17639 struct face *face;
17640 struct font_info *font_info;
17641 XChar2b char2b;
17642 XCharStruct *pcm;
17643
17644 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17645 font = face->font;
17646 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17647 if (font /* ++KFS: Should this be font_info ? */
17648 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17649 {
17650 if (pcm->rbearing > pcm->width)
17651 *right = pcm->rbearing - pcm->width;
17652 if (pcm->lbearing < 0)
17653 *left = -pcm->lbearing;
17654 }
17655 }
17656 }
17657
17658
17659 /* Return the index of the first glyph preceding glyph string S that
17660 is overwritten by S because of S's left overhang. Value is -1
17661 if no glyphs are overwritten. */
17662
17663 static int
17664 left_overwritten (s)
17665 struct glyph_string *s;
17666 {
17667 int k;
17668
17669 if (s->left_overhang)
17670 {
17671 int x = 0, i;
17672 struct glyph *glyphs = s->row->glyphs[s->area];
17673 int first = s->first_glyph - glyphs;
17674
17675 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17676 x -= glyphs[i].pixel_width;
17677
17678 k = i + 1;
17679 }
17680 else
17681 k = -1;
17682
17683 return k;
17684 }
17685
17686
17687 /* Return the index of the first glyph preceding glyph string S that
17688 is overwriting S because of its right overhang. Value is -1 if no
17689 glyph in front of S overwrites S. */
17690
17691 static int
17692 left_overwriting (s)
17693 struct glyph_string *s;
17694 {
17695 int i, k, x;
17696 struct glyph *glyphs = s->row->glyphs[s->area];
17697 int first = s->first_glyph - glyphs;
17698
17699 k = -1;
17700 x = 0;
17701 for (i = first - 1; i >= 0; --i)
17702 {
17703 int left, right;
17704 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17705 if (x + right > 0)
17706 k = i;
17707 x -= glyphs[i].pixel_width;
17708 }
17709
17710 return k;
17711 }
17712
17713
17714 /* Return the index of the last glyph following glyph string S that is
17715 not overwritten by S because of S's right overhang. Value is -1 if
17716 no such glyph is found. */
17717
17718 static int
17719 right_overwritten (s)
17720 struct glyph_string *s;
17721 {
17722 int k = -1;
17723
17724 if (s->right_overhang)
17725 {
17726 int x = 0, i;
17727 struct glyph *glyphs = s->row->glyphs[s->area];
17728 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17729 int end = s->row->used[s->area];
17730
17731 for (i = first; i < end && s->right_overhang > x; ++i)
17732 x += glyphs[i].pixel_width;
17733
17734 k = i;
17735 }
17736
17737 return k;
17738 }
17739
17740
17741 /* Return the index of the last glyph following glyph string S that
17742 overwrites S because of its left overhang. Value is negative
17743 if no such glyph is found. */
17744
17745 static int
17746 right_overwriting (s)
17747 struct glyph_string *s;
17748 {
17749 int i, k, x;
17750 int end = s->row->used[s->area];
17751 struct glyph *glyphs = s->row->glyphs[s->area];
17752 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17753
17754 k = -1;
17755 x = 0;
17756 for (i = first; i < end; ++i)
17757 {
17758 int left, right;
17759 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17760 if (x - left < 0)
17761 k = i;
17762 x += glyphs[i].pixel_width;
17763 }
17764
17765 return k;
17766 }
17767
17768
17769 /* Get face and two-byte form of character C in face FACE_ID on frame
17770 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17771 means we want to display multibyte text. DISPLAY_P non-zero means
17772 make sure that X resources for the face returned are allocated.
17773 Value is a pointer to a realized face that is ready for display if
17774 DISPLAY_P is non-zero. */
17775
17776 static INLINE struct face *
17777 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17778 struct frame *f;
17779 int c, face_id;
17780 XChar2b *char2b;
17781 int multibyte_p, display_p;
17782 {
17783 struct face *face = FACE_FROM_ID (f, face_id);
17784
17785 if (!multibyte_p)
17786 {
17787 /* Unibyte case. We don't have to encode, but we have to make
17788 sure to use a face suitable for unibyte. */
17789 STORE_XCHAR2B (char2b, 0, c);
17790 face_id = FACE_FOR_CHAR (f, face, c);
17791 face = FACE_FROM_ID (f, face_id);
17792 }
17793 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17794 {
17795 /* Case of ASCII in a face known to fit ASCII. */
17796 STORE_XCHAR2B (char2b, 0, c);
17797 }
17798 else
17799 {
17800 int c1, c2, charset;
17801
17802 /* Split characters into bytes. If c2 is -1 afterwards, C is
17803 really a one-byte character so that byte1 is zero. */
17804 SPLIT_CHAR (c, charset, c1, c2);
17805 if (c2 > 0)
17806 STORE_XCHAR2B (char2b, c1, c2);
17807 else
17808 STORE_XCHAR2B (char2b, 0, c1);
17809
17810 /* Maybe encode the character in *CHAR2B. */
17811 if (face->font != NULL)
17812 {
17813 struct font_info *font_info
17814 = FONT_INFO_FROM_ID (f, face->font_info_id);
17815 if (font_info)
17816 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17817 }
17818 }
17819
17820 /* Make sure X resources of the face are allocated. */
17821 #ifdef HAVE_X_WINDOWS
17822 if (display_p)
17823 #endif
17824 {
17825 xassert (face != NULL);
17826 PREPARE_FACE_FOR_DISPLAY (f, face);
17827 }
17828
17829 return face;
17830 }
17831
17832
17833 /* Set background width of glyph string S. START is the index of the
17834 first glyph following S. LAST_X is the right-most x-position + 1
17835 in the drawing area. */
17836
17837 static INLINE void
17838 set_glyph_string_background_width (s, start, last_x)
17839 struct glyph_string *s;
17840 int start;
17841 int last_x;
17842 {
17843 /* If the face of this glyph string has to be drawn to the end of
17844 the drawing area, set S->extends_to_end_of_line_p. */
17845 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17846
17847 if (start == s->row->used[s->area]
17848 && s->area == TEXT_AREA
17849 && ((s->hl == DRAW_NORMAL_TEXT
17850 && (s->row->fill_line_p
17851 || s->face->background != default_face->background
17852 || s->face->stipple != default_face->stipple
17853 || s->row->mouse_face_p))
17854 || s->hl == DRAW_MOUSE_FACE
17855 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17856 && s->row->fill_line_p)))
17857 s->extends_to_end_of_line_p = 1;
17858
17859 /* If S extends its face to the end of the line, set its
17860 background_width to the distance to the right edge of the drawing
17861 area. */
17862 if (s->extends_to_end_of_line_p)
17863 s->background_width = last_x - s->x + 1;
17864 else
17865 s->background_width = s->width;
17866 }
17867
17868
17869 /* Compute overhangs and x-positions for glyph string S and its
17870 predecessors, or successors. X is the starting x-position for S.
17871 BACKWARD_P non-zero means process predecessors. */
17872
17873 static void
17874 compute_overhangs_and_x (s, x, backward_p)
17875 struct glyph_string *s;
17876 int x;
17877 int backward_p;
17878 {
17879 if (backward_p)
17880 {
17881 while (s)
17882 {
17883 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17884 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17885 x -= s->width;
17886 s->x = x;
17887 s = s->prev;
17888 }
17889 }
17890 else
17891 {
17892 while (s)
17893 {
17894 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17895 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17896 s->x = x;
17897 x += s->width;
17898 s = s->next;
17899 }
17900 }
17901 }
17902
17903
17904
17905 /* The following macros are only called from draw_glyphs below.
17906 They reference the following parameters of that function directly:
17907 `w', `row', `area', and `overlap_p'
17908 as well as the following local variables:
17909 `s', `f', and `hdc' (in W32) */
17910
17911 #ifdef HAVE_NTGUI
17912 /* On W32, silently add local `hdc' variable to argument list of
17913 init_glyph_string. */
17914 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17915 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17916 #else
17917 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17918 init_glyph_string (s, char2b, w, row, area, start, hl)
17919 #endif
17920
17921 /* Add a glyph string for a stretch glyph to the list of strings
17922 between HEAD and TAIL. START is the index of the stretch glyph in
17923 row area AREA of glyph row ROW. END is the index of the last glyph
17924 in that glyph row area. X is the current output position assigned
17925 to the new glyph string constructed. HL overrides that face of the
17926 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17927 is the right-most x-position of the drawing area. */
17928
17929 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17930 and below -- keep them on one line. */
17931 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17932 do \
17933 { \
17934 s = (struct glyph_string *) alloca (sizeof *s); \
17935 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17936 START = fill_stretch_glyph_string (s, row, area, START, END); \
17937 append_glyph_string (&HEAD, &TAIL, s); \
17938 s->x = (X); \
17939 } \
17940 while (0)
17941
17942
17943 /* Add a glyph string for an image glyph to the list of strings
17944 between HEAD and TAIL. START is the index of the image glyph in
17945 row area AREA of glyph row ROW. END is the index of the last glyph
17946 in that glyph row area. X is the current output position assigned
17947 to the new glyph string constructed. HL overrides that face of the
17948 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17949 is the right-most x-position of the drawing area. */
17950
17951 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17952 do \
17953 { \
17954 s = (struct glyph_string *) alloca (sizeof *s); \
17955 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17956 fill_image_glyph_string (s); \
17957 append_glyph_string (&HEAD, &TAIL, s); \
17958 ++START; \
17959 s->x = (X); \
17960 } \
17961 while (0)
17962
17963
17964 /* Add a glyph string for a sequence of character glyphs to the list
17965 of strings between HEAD and TAIL. START is the index of the first
17966 glyph in row area AREA of glyph row ROW that is part of the new
17967 glyph string. END is the index of the last glyph in that glyph row
17968 area. X is the current output position assigned to the new glyph
17969 string constructed. HL overrides that face of the glyph; e.g. it
17970 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17971 right-most x-position of the drawing area. */
17972
17973 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17974 do \
17975 { \
17976 int c, face_id; \
17977 XChar2b *char2b; \
17978 \
17979 c = (row)->glyphs[area][START].u.ch; \
17980 face_id = (row)->glyphs[area][START].face_id; \
17981 \
17982 s = (struct glyph_string *) alloca (sizeof *s); \
17983 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17984 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17985 append_glyph_string (&HEAD, &TAIL, s); \
17986 s->x = (X); \
17987 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17988 } \
17989 while (0)
17990
17991
17992 /* Add a glyph string for a composite sequence to the list of strings
17993 between HEAD and TAIL. START is the index of the first glyph in
17994 row area AREA of glyph row ROW that is part of the new glyph
17995 string. END is the index of the last glyph in that glyph row area.
17996 X is the current output position assigned to the new glyph string
17997 constructed. HL overrides that face of the glyph; e.g. it is
17998 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17999 x-position of the drawing area. */
18000
18001 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18002 do { \
18003 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18004 int face_id = (row)->glyphs[area][START].face_id; \
18005 struct face *base_face = FACE_FROM_ID (f, face_id); \
18006 struct composition *cmp = composition_table[cmp_id]; \
18007 int glyph_len = cmp->glyph_len; \
18008 XChar2b *char2b; \
18009 struct face **faces; \
18010 struct glyph_string *first_s = NULL; \
18011 int n; \
18012 \
18013 base_face = base_face->ascii_face; \
18014 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18015 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18016 /* At first, fill in `char2b' and `faces'. */ \
18017 for (n = 0; n < glyph_len; n++) \
18018 { \
18019 int c = COMPOSITION_GLYPH (cmp, n); \
18020 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18021 faces[n] = FACE_FROM_ID (f, this_face_id); \
18022 get_char_face_and_encoding (f, c, this_face_id, \
18023 char2b + n, 1, 1); \
18024 } \
18025 \
18026 /* Make glyph_strings for each glyph sequence that is drawable by \
18027 the same face, and append them to HEAD/TAIL. */ \
18028 for (n = 0; n < cmp->glyph_len;) \
18029 { \
18030 s = (struct glyph_string *) alloca (sizeof *s); \
18031 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18032 append_glyph_string (&(HEAD), &(TAIL), s); \
18033 s->cmp = cmp; \
18034 s->gidx = n; \
18035 s->x = (X); \
18036 \
18037 if (n == 0) \
18038 first_s = s; \
18039 \
18040 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18041 } \
18042 \
18043 ++START; \
18044 s = first_s; \
18045 } while (0)
18046
18047
18048 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18049 of AREA of glyph row ROW on window W between indices START and END.
18050 HL overrides the face for drawing glyph strings, e.g. it is
18051 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18052 x-positions of the drawing area.
18053
18054 This is an ugly monster macro construct because we must use alloca
18055 to allocate glyph strings (because draw_glyphs can be called
18056 asynchronously). */
18057
18058 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18059 do \
18060 { \
18061 HEAD = TAIL = NULL; \
18062 while (START < END) \
18063 { \
18064 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18065 switch (first_glyph->type) \
18066 { \
18067 case CHAR_GLYPH: \
18068 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18069 HL, X, LAST_X); \
18070 break; \
18071 \
18072 case COMPOSITE_GLYPH: \
18073 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18074 HL, X, LAST_X); \
18075 break; \
18076 \
18077 case STRETCH_GLYPH: \
18078 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18079 HL, X, LAST_X); \
18080 break; \
18081 \
18082 case IMAGE_GLYPH: \
18083 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18084 HL, X, LAST_X); \
18085 break; \
18086 \
18087 default: \
18088 abort (); \
18089 } \
18090 \
18091 set_glyph_string_background_width (s, START, LAST_X); \
18092 (X) += s->width; \
18093 } \
18094 } \
18095 while (0)
18096
18097
18098 /* Draw glyphs between START and END in AREA of ROW on window W,
18099 starting at x-position X. X is relative to AREA in W. HL is a
18100 face-override with the following meaning:
18101
18102 DRAW_NORMAL_TEXT draw normally
18103 DRAW_CURSOR draw in cursor face
18104 DRAW_MOUSE_FACE draw in mouse face.
18105 DRAW_INVERSE_VIDEO draw in mode line face
18106 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18107 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18108
18109 If OVERLAPS_P is non-zero, draw only the foreground of characters
18110 and clip to the physical height of ROW.
18111
18112 Value is the x-position reached, relative to AREA of W. */
18113
18114 static int
18115 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18116 struct window *w;
18117 int x;
18118 struct glyph_row *row;
18119 enum glyph_row_area area;
18120 int start, end;
18121 enum draw_glyphs_face hl;
18122 int overlaps_p;
18123 {
18124 struct glyph_string *head, *tail;
18125 struct glyph_string *s;
18126 int last_x, area_width;
18127 int x_reached;
18128 int i, j;
18129 struct frame *f = XFRAME (WINDOW_FRAME (w));
18130 DECLARE_HDC (hdc);
18131
18132 ALLOCATE_HDC (hdc, f);
18133
18134 /* Let's rather be paranoid than getting a SEGV. */
18135 end = min (end, row->used[area]);
18136 start = max (0, start);
18137 start = min (end, start);
18138
18139 /* Translate X to frame coordinates. Set last_x to the right
18140 end of the drawing area. */
18141 if (row->full_width_p)
18142 {
18143 /* X is relative to the left edge of W, without scroll bars
18144 or fringes. */
18145 x += WINDOW_LEFT_EDGE_X (w);
18146 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18147 }
18148 else
18149 {
18150 int area_left = window_box_left (w, area);
18151 x += area_left;
18152 area_width = window_box_width (w, area);
18153 last_x = area_left + area_width;
18154 }
18155
18156 /* Build a doubly-linked list of glyph_string structures between
18157 head and tail from what we have to draw. Note that the macro
18158 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18159 the reason we use a separate variable `i'. */
18160 i = start;
18161 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18162 if (tail)
18163 x_reached = tail->x + tail->background_width;
18164 else
18165 x_reached = x;
18166
18167 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18168 the row, redraw some glyphs in front or following the glyph
18169 strings built above. */
18170 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18171 {
18172 int dummy_x = 0;
18173 struct glyph_string *h, *t;
18174
18175 /* Compute overhangs for all glyph strings. */
18176 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
18177 for (s = head; s; s = s->next)
18178 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
18179
18180 /* Prepend glyph strings for glyphs in front of the first glyph
18181 string that are overwritten because of the first glyph
18182 string's left overhang. The background of all strings
18183 prepended must be drawn because the first glyph string
18184 draws over it. */
18185 i = left_overwritten (head);
18186 if (i >= 0)
18187 {
18188 j = i;
18189 BUILD_GLYPH_STRINGS (j, start, h, t,
18190 DRAW_NORMAL_TEXT, dummy_x, last_x);
18191 start = i;
18192 compute_overhangs_and_x (t, head->x, 1);
18193 prepend_glyph_string_lists (&head, &tail, h, t);
18194 }
18195
18196 /* Prepend glyph strings for glyphs in front of the first glyph
18197 string that overwrite that glyph string because of their
18198 right overhang. For these strings, only the foreground must
18199 be drawn, because it draws over the glyph string at `head'.
18200 The background must not be drawn because this would overwrite
18201 right overhangs of preceding glyphs for which no glyph
18202 strings exist. */
18203 i = left_overwriting (head);
18204 if (i >= 0)
18205 {
18206 BUILD_GLYPH_STRINGS (i, start, h, t,
18207 DRAW_NORMAL_TEXT, dummy_x, last_x);
18208 for (s = h; s; s = s->next)
18209 s->background_filled_p = 1;
18210 compute_overhangs_and_x (t, head->x, 1);
18211 prepend_glyph_string_lists (&head, &tail, h, t);
18212 }
18213
18214 /* Append glyphs strings for glyphs following the last glyph
18215 string tail that are overwritten by tail. The background of
18216 these strings has to be drawn because tail's foreground draws
18217 over it. */
18218 i = right_overwritten (tail);
18219 if (i >= 0)
18220 {
18221 BUILD_GLYPH_STRINGS (end, i, h, t,
18222 DRAW_NORMAL_TEXT, x, last_x);
18223 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18224 append_glyph_string_lists (&head, &tail, h, t);
18225 }
18226
18227 /* Append glyph strings for glyphs following the last glyph
18228 string tail that overwrite tail. The foreground of such
18229 glyphs has to be drawn because it writes into the background
18230 of tail. The background must not be drawn because it could
18231 paint over the foreground of following glyphs. */
18232 i = right_overwriting (tail);
18233 if (i >= 0)
18234 {
18235 BUILD_GLYPH_STRINGS (end, i, h, t,
18236 DRAW_NORMAL_TEXT, x, last_x);
18237 for (s = h; s; s = s->next)
18238 s->background_filled_p = 1;
18239 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18240 append_glyph_string_lists (&head, &tail, h, t);
18241 }
18242 }
18243
18244 /* Draw all strings. */
18245 for (s = head; s; s = s->next)
18246 FRAME_RIF (f)->draw_glyph_string (s);
18247
18248 if (area == TEXT_AREA
18249 && !row->full_width_p
18250 /* When drawing overlapping rows, only the glyph strings'
18251 foreground is drawn, which doesn't erase a cursor
18252 completely. */
18253 && !overlaps_p)
18254 {
18255 int x0 = head ? head->x : x;
18256 int x1 = tail ? tail->x + tail->background_width : x;
18257
18258 int text_left = window_box_left (w, TEXT_AREA);
18259 x0 -= text_left;
18260 x1 -= text_left;
18261
18262 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18263 row->y, MATRIX_ROW_BOTTOM_Y (row));
18264 }
18265
18266 /* Value is the x-position up to which drawn, relative to AREA of W.
18267 This doesn't include parts drawn because of overhangs. */
18268 if (row->full_width_p)
18269 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18270 else
18271 x_reached -= window_box_left (w, area);
18272
18273 RELEASE_HDC (hdc, f);
18274
18275 return x_reached;
18276 }
18277
18278 /* Expand row matrix if too narrow. Don't expand if area
18279 is not present. */
18280
18281 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18282 { \
18283 if (!fonts_changed_p \
18284 && (it->glyph_row->glyphs[area] \
18285 < it->glyph_row->glyphs[area + 1])) \
18286 { \
18287 it->w->ncols_scale_factor++; \
18288 fonts_changed_p = 1; \
18289 } \
18290 }
18291
18292 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18293 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18294
18295 static INLINE void
18296 append_glyph (it)
18297 struct it *it;
18298 {
18299 struct glyph *glyph;
18300 enum glyph_row_area area = it->area;
18301
18302 xassert (it->glyph_row);
18303 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18304
18305 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18306 if (glyph < it->glyph_row->glyphs[area + 1])
18307 {
18308 glyph->charpos = CHARPOS (it->position);
18309 glyph->object = it->object;
18310 glyph->pixel_width = it->pixel_width;
18311 glyph->ascent = it->ascent;
18312 glyph->descent = it->descent;
18313 glyph->voffset = it->voffset;
18314 glyph->type = CHAR_GLYPH;
18315 glyph->multibyte_p = it->multibyte_p;
18316 glyph->left_box_line_p = it->start_of_box_run_p;
18317 glyph->right_box_line_p = it->end_of_box_run_p;
18318 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18319 || it->phys_descent > it->descent);
18320 glyph->padding_p = 0;
18321 glyph->glyph_not_available_p = it->glyph_not_available_p;
18322 glyph->face_id = it->face_id;
18323 glyph->u.ch = it->char_to_display;
18324 glyph->slice = null_glyph_slice;
18325 glyph->font_type = FONT_TYPE_UNKNOWN;
18326 ++it->glyph_row->used[area];
18327 }
18328 else
18329 IT_EXPAND_MATRIX_WIDTH (it, area);
18330 }
18331
18332 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18333 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18334
18335 static INLINE void
18336 append_composite_glyph (it)
18337 struct it *it;
18338 {
18339 struct glyph *glyph;
18340 enum glyph_row_area area = it->area;
18341
18342 xassert (it->glyph_row);
18343
18344 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18345 if (glyph < it->glyph_row->glyphs[area + 1])
18346 {
18347 glyph->charpos = CHARPOS (it->position);
18348 glyph->object = it->object;
18349 glyph->pixel_width = it->pixel_width;
18350 glyph->ascent = it->ascent;
18351 glyph->descent = it->descent;
18352 glyph->voffset = it->voffset;
18353 glyph->type = COMPOSITE_GLYPH;
18354 glyph->multibyte_p = it->multibyte_p;
18355 glyph->left_box_line_p = it->start_of_box_run_p;
18356 glyph->right_box_line_p = it->end_of_box_run_p;
18357 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18358 || it->phys_descent > it->descent);
18359 glyph->padding_p = 0;
18360 glyph->glyph_not_available_p = 0;
18361 glyph->face_id = it->face_id;
18362 glyph->u.cmp_id = it->cmp_id;
18363 glyph->slice = null_glyph_slice;
18364 glyph->font_type = FONT_TYPE_UNKNOWN;
18365 ++it->glyph_row->used[area];
18366 }
18367 else
18368 IT_EXPAND_MATRIX_WIDTH (it, area);
18369 }
18370
18371
18372 /* Change IT->ascent and IT->height according to the setting of
18373 IT->voffset. */
18374
18375 static INLINE void
18376 take_vertical_position_into_account (it)
18377 struct it *it;
18378 {
18379 if (it->voffset)
18380 {
18381 if (it->voffset < 0)
18382 /* Increase the ascent so that we can display the text higher
18383 in the line. */
18384 it->ascent -= it->voffset;
18385 else
18386 /* Increase the descent so that we can display the text lower
18387 in the line. */
18388 it->descent += it->voffset;
18389 }
18390 }
18391
18392
18393 /* Produce glyphs/get display metrics for the image IT is loaded with.
18394 See the description of struct display_iterator in dispextern.h for
18395 an overview of struct display_iterator. */
18396
18397 static void
18398 produce_image_glyph (it)
18399 struct it *it;
18400 {
18401 struct image *img;
18402 struct face *face;
18403 int glyph_ascent;
18404 struct glyph_slice slice;
18405
18406 xassert (it->what == IT_IMAGE);
18407
18408 face = FACE_FROM_ID (it->f, it->face_id);
18409 xassert (face);
18410 /* Make sure X resources of the face is loaded. */
18411 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18412
18413 if (it->image_id < 0)
18414 {
18415 /* Fringe bitmap. */
18416 it->ascent = it->phys_ascent = 0;
18417 it->descent = it->phys_descent = 0;
18418 it->pixel_width = 0;
18419 it->nglyphs = 0;
18420 return;
18421 }
18422
18423 img = IMAGE_FROM_ID (it->f, it->image_id);
18424 xassert (img);
18425 /* Make sure X resources of the image is loaded. */
18426 prepare_image_for_display (it->f, img);
18427
18428 slice.x = slice.y = 0;
18429 slice.width = img->width;
18430 slice.height = img->height;
18431
18432 if (INTEGERP (it->slice.x))
18433 slice.x = XINT (it->slice.x);
18434 else if (FLOATP (it->slice.x))
18435 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18436
18437 if (INTEGERP (it->slice.y))
18438 slice.y = XINT (it->slice.y);
18439 else if (FLOATP (it->slice.y))
18440 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18441
18442 if (INTEGERP (it->slice.width))
18443 slice.width = XINT (it->slice.width);
18444 else if (FLOATP (it->slice.width))
18445 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18446
18447 if (INTEGERP (it->slice.height))
18448 slice.height = XINT (it->slice.height);
18449 else if (FLOATP (it->slice.height))
18450 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18451
18452 if (slice.x >= img->width)
18453 slice.x = img->width;
18454 if (slice.y >= img->height)
18455 slice.y = img->height;
18456 if (slice.x + slice.width >= img->width)
18457 slice.width = img->width - slice.x;
18458 if (slice.y + slice.height > img->height)
18459 slice.height = img->height - slice.y;
18460
18461 if (slice.width == 0 || slice.height == 0)
18462 return;
18463
18464 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18465
18466 it->descent = slice.height - glyph_ascent;
18467 if (slice.y == 0)
18468 it->descent += img->vmargin;
18469 if (slice.y + slice.height == img->height)
18470 it->descent += img->vmargin;
18471 it->phys_descent = it->descent;
18472
18473 it->pixel_width = slice.width;
18474 if (slice.x == 0)
18475 it->pixel_width += img->hmargin;
18476 if (slice.x + slice.width == img->width)
18477 it->pixel_width += img->hmargin;
18478
18479 /* It's quite possible for images to have an ascent greater than
18480 their height, so don't get confused in that case. */
18481 if (it->descent < 0)
18482 it->descent = 0;
18483
18484 #if 0 /* this breaks image tiling */
18485 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18486 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18487 if (face_ascent > it->ascent)
18488 it->ascent = it->phys_ascent = face_ascent;
18489 #endif
18490
18491 it->nglyphs = 1;
18492
18493 if (face->box != FACE_NO_BOX)
18494 {
18495 if (face->box_line_width > 0)
18496 {
18497 if (slice.y == 0)
18498 it->ascent += face->box_line_width;
18499 if (slice.y + slice.height == img->height)
18500 it->descent += face->box_line_width;
18501 }
18502
18503 if (it->start_of_box_run_p && slice.x == 0)
18504 it->pixel_width += abs (face->box_line_width);
18505 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18506 it->pixel_width += abs (face->box_line_width);
18507 }
18508
18509 take_vertical_position_into_account (it);
18510
18511 if (it->glyph_row)
18512 {
18513 struct glyph *glyph;
18514 enum glyph_row_area area = it->area;
18515
18516 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18517 if (glyph < it->glyph_row->glyphs[area + 1])
18518 {
18519 glyph->charpos = CHARPOS (it->position);
18520 glyph->object = it->object;
18521 glyph->pixel_width = it->pixel_width;
18522 glyph->ascent = glyph_ascent;
18523 glyph->descent = it->descent;
18524 glyph->voffset = it->voffset;
18525 glyph->type = IMAGE_GLYPH;
18526 glyph->multibyte_p = it->multibyte_p;
18527 glyph->left_box_line_p = it->start_of_box_run_p;
18528 glyph->right_box_line_p = it->end_of_box_run_p;
18529 glyph->overlaps_vertically_p = 0;
18530 glyph->padding_p = 0;
18531 glyph->glyph_not_available_p = 0;
18532 glyph->face_id = it->face_id;
18533 glyph->u.img_id = img->id;
18534 glyph->slice = slice;
18535 glyph->font_type = FONT_TYPE_UNKNOWN;
18536 ++it->glyph_row->used[area];
18537 }
18538 else
18539 IT_EXPAND_MATRIX_WIDTH (it, area);
18540 }
18541 }
18542
18543
18544 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18545 of the glyph, WIDTH and HEIGHT are the width and height of the
18546 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18547
18548 static void
18549 append_stretch_glyph (it, object, width, height, ascent)
18550 struct it *it;
18551 Lisp_Object object;
18552 int width, height;
18553 int ascent;
18554 {
18555 struct glyph *glyph;
18556 enum glyph_row_area area = it->area;
18557
18558 xassert (ascent >= 0 && ascent <= height);
18559
18560 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18561 if (glyph < it->glyph_row->glyphs[area + 1])
18562 {
18563 glyph->charpos = CHARPOS (it->position);
18564 glyph->object = object;
18565 glyph->pixel_width = width;
18566 glyph->ascent = ascent;
18567 glyph->descent = height - ascent;
18568 glyph->voffset = it->voffset;
18569 glyph->type = STRETCH_GLYPH;
18570 glyph->multibyte_p = it->multibyte_p;
18571 glyph->left_box_line_p = it->start_of_box_run_p;
18572 glyph->right_box_line_p = it->end_of_box_run_p;
18573 glyph->overlaps_vertically_p = 0;
18574 glyph->padding_p = 0;
18575 glyph->glyph_not_available_p = 0;
18576 glyph->face_id = it->face_id;
18577 glyph->u.stretch.ascent = ascent;
18578 glyph->u.stretch.height = height;
18579 glyph->slice = null_glyph_slice;
18580 glyph->font_type = FONT_TYPE_UNKNOWN;
18581 ++it->glyph_row->used[area];
18582 }
18583 else
18584 IT_EXPAND_MATRIX_WIDTH (it, area);
18585 }
18586
18587
18588 /* Produce a stretch glyph for iterator IT. IT->object is the value
18589 of the glyph property displayed. The value must be a list
18590 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18591 being recognized:
18592
18593 1. `:width WIDTH' specifies that the space should be WIDTH *
18594 canonical char width wide. WIDTH may be an integer or floating
18595 point number.
18596
18597 2. `:relative-width FACTOR' specifies that the width of the stretch
18598 should be computed from the width of the first character having the
18599 `glyph' property, and should be FACTOR times that width.
18600
18601 3. `:align-to HPOS' specifies that the space should be wide enough
18602 to reach HPOS, a value in canonical character units.
18603
18604 Exactly one of the above pairs must be present.
18605
18606 4. `:height HEIGHT' specifies that the height of the stretch produced
18607 should be HEIGHT, measured in canonical character units.
18608
18609 5. `:relative-height FACTOR' specifies that the height of the
18610 stretch should be FACTOR times the height of the characters having
18611 the glyph property.
18612
18613 Either none or exactly one of 4 or 5 must be present.
18614
18615 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18616 of the stretch should be used for the ascent of the stretch.
18617 ASCENT must be in the range 0 <= ASCENT <= 100. */
18618
18619 static void
18620 produce_stretch_glyph (it)
18621 struct it *it;
18622 {
18623 /* (space :width WIDTH :height HEIGHT ...) */
18624 Lisp_Object prop, plist;
18625 int width = 0, height = 0, align_to = -1;
18626 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18627 int ascent = 0;
18628 double tem;
18629 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18630 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18631
18632 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18633
18634 /* List should start with `space'. */
18635 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18636 plist = XCDR (it->object);
18637
18638 /* Compute the width of the stretch. */
18639 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18640 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18641 {
18642 /* Absolute width `:width WIDTH' specified and valid. */
18643 zero_width_ok_p = 1;
18644 width = (int)tem;
18645 }
18646 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18647 NUMVAL (prop) > 0)
18648 {
18649 /* Relative width `:relative-width FACTOR' specified and valid.
18650 Compute the width of the characters having the `glyph'
18651 property. */
18652 struct it it2;
18653 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18654
18655 it2 = *it;
18656 if (it->multibyte_p)
18657 {
18658 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18659 - IT_BYTEPOS (*it));
18660 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18661 }
18662 else
18663 it2.c = *p, it2.len = 1;
18664
18665 it2.glyph_row = NULL;
18666 it2.what = IT_CHARACTER;
18667 x_produce_glyphs (&it2);
18668 width = NUMVAL (prop) * it2.pixel_width;
18669 }
18670 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18671 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18672 {
18673 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18674 align_to = (align_to < 0
18675 ? 0
18676 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18677 else if (align_to < 0)
18678 align_to = window_box_left_offset (it->w, TEXT_AREA);
18679 width = max (0, (int)tem + align_to - it->current_x);
18680 zero_width_ok_p = 1;
18681 }
18682 else
18683 /* Nothing specified -> width defaults to canonical char width. */
18684 width = FRAME_COLUMN_WIDTH (it->f);
18685
18686 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18687 width = 1;
18688
18689 /* Compute height. */
18690 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18691 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18692 {
18693 height = (int)tem;
18694 zero_height_ok_p = 1;
18695 }
18696 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18697 NUMVAL (prop) > 0)
18698 height = FONT_HEIGHT (font) * NUMVAL (prop);
18699 else
18700 height = FONT_HEIGHT (font);
18701
18702 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18703 height = 1;
18704
18705 /* Compute percentage of height used for ascent. If
18706 `:ascent ASCENT' is present and valid, use that. Otherwise,
18707 derive the ascent from the font in use. */
18708 if (prop = Fsafe_plist_get (plist, QCascent),
18709 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18710 ascent = height * NUMVAL (prop) / 100.0;
18711 else if (!NILP (prop)
18712 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18713 ascent = min (max (0, (int)tem), height);
18714 else
18715 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18716
18717 if (width > 0 && height > 0 && it->glyph_row)
18718 {
18719 Lisp_Object object = it->stack[it->sp - 1].string;
18720 if (!STRINGP (object))
18721 object = it->w->buffer;
18722 append_stretch_glyph (it, object, width, height, ascent);
18723 }
18724
18725 it->pixel_width = width;
18726 it->ascent = it->phys_ascent = ascent;
18727 it->descent = it->phys_descent = height - it->ascent;
18728 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18729
18730 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18731 {
18732 if (face->box_line_width > 0)
18733 {
18734 it->ascent += face->box_line_width;
18735 it->descent += face->box_line_width;
18736 }
18737
18738 if (it->start_of_box_run_p)
18739 it->pixel_width += abs (face->box_line_width);
18740 if (it->end_of_box_run_p)
18741 it->pixel_width += abs (face->box_line_width);
18742 }
18743
18744 take_vertical_position_into_account (it);
18745 }
18746
18747 /* Calculate line-height and line-spacing properties.
18748 An integer value specifies explicit pixel value.
18749 A float value specifies relative value to current face height.
18750 A cons (float . face-name) specifies relative value to
18751 height of specified face font.
18752
18753 Returns height in pixels, or nil. */
18754
18755 static Lisp_Object
18756 calc_line_height_property (it, prop, font, boff, total)
18757 struct it *it;
18758 Lisp_Object prop;
18759 XFontStruct *font;
18760 int boff, *total;
18761 {
18762 Lisp_Object position, val;
18763 Lisp_Object face_name = Qnil;
18764 int ascent, descent, height, override;
18765
18766 if (STRINGP (it->object))
18767 position = make_number (IT_STRING_CHARPOS (*it));
18768 else if (BUFFERP (it->object))
18769 position = make_number (IT_CHARPOS (*it));
18770 else
18771 return Qnil;
18772
18773 val = Fget_char_property (position, prop, it->object);
18774
18775 if (NILP (val))
18776 return val;
18777
18778 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18779 {
18780 *total = 1;
18781 val = XCDR (val);
18782 }
18783
18784 if (INTEGERP (val))
18785 return val;
18786
18787 if (CONSP (val))
18788 {
18789 face_name = XCDR (val);
18790 val = XCAR (val);
18791 }
18792 else if (SYMBOLP (val))
18793 {
18794 face_name = val;
18795 val = Qnil;
18796 }
18797
18798 override = EQ (prop, Qline_height);
18799
18800 if (NILP (face_name))
18801 {
18802 font = FRAME_FONT (it->f);
18803 boff = FRAME_BASELINE_OFFSET (it->f);
18804 }
18805 else if (EQ (face_name, Qt))
18806 {
18807 override = 0;
18808 }
18809 else
18810 {
18811 int face_id;
18812 struct face *face;
18813 struct font_info *font_info;
18814
18815 face_id = lookup_named_face (it->f, face_name, ' ');
18816 if (face_id < 0)
18817 return make_number (-1);
18818
18819 face = FACE_FROM_ID (it->f, face_id);
18820 font = face->font;
18821 if (font == NULL)
18822 return make_number (-1);
18823
18824 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18825 boff = font_info->baseline_offset;
18826 if (font_info->vertical_centering)
18827 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18828 }
18829
18830 ascent = FONT_BASE (font) + boff;
18831 descent = FONT_DESCENT (font) - boff;
18832
18833 if (override)
18834 {
18835 it->override_ascent = ascent;
18836 it->override_descent = descent;
18837 it->override_boff = boff;
18838 }
18839
18840 height = ascent + descent;
18841 if (FLOATP (val))
18842 height = (int)(XFLOAT_DATA (val) * height);
18843 else if (INTEGERP (val))
18844 height *= XINT (val);
18845
18846 return make_number (height);
18847 }
18848
18849
18850 /* RIF:
18851 Produce glyphs/get display metrics for the display element IT is
18852 loaded with. See the description of struct display_iterator in
18853 dispextern.h for an overview of struct display_iterator. */
18854
18855 void
18856 x_produce_glyphs (it)
18857 struct it *it;
18858 {
18859 int extra_line_spacing = it->extra_line_spacing;
18860
18861 it->glyph_not_available_p = 0;
18862
18863 if (it->what == IT_CHARACTER)
18864 {
18865 XChar2b char2b;
18866 XFontStruct *font;
18867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18868 XCharStruct *pcm;
18869 int font_not_found_p;
18870 struct font_info *font_info;
18871 int boff; /* baseline offset */
18872 /* We may change it->multibyte_p upon unibyte<->multibyte
18873 conversion. So, save the current value now and restore it
18874 later.
18875
18876 Note: It seems that we don't have to record multibyte_p in
18877 struct glyph because the character code itself tells if or
18878 not the character is multibyte. Thus, in the future, we must
18879 consider eliminating the field `multibyte_p' in the struct
18880 glyph. */
18881 int saved_multibyte_p = it->multibyte_p;
18882
18883 /* Maybe translate single-byte characters to multibyte, or the
18884 other way. */
18885 it->char_to_display = it->c;
18886 if (!ASCII_BYTE_P (it->c))
18887 {
18888 if (unibyte_display_via_language_environment
18889 && SINGLE_BYTE_CHAR_P (it->c)
18890 && (it->c >= 0240
18891 || !NILP (Vnonascii_translation_table)))
18892 {
18893 it->char_to_display = unibyte_char_to_multibyte (it->c);
18894 it->multibyte_p = 1;
18895 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18896 face = FACE_FROM_ID (it->f, it->face_id);
18897 }
18898 else if (!SINGLE_BYTE_CHAR_P (it->c)
18899 && !it->multibyte_p)
18900 {
18901 it->multibyte_p = 1;
18902 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18903 face = FACE_FROM_ID (it->f, it->face_id);
18904 }
18905 }
18906
18907 /* Get font to use. Encode IT->char_to_display. */
18908 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18909 &char2b, it->multibyte_p, 0);
18910 font = face->font;
18911
18912 /* When no suitable font found, use the default font. */
18913 font_not_found_p = font == NULL;
18914 if (font_not_found_p)
18915 {
18916 font = FRAME_FONT (it->f);
18917 boff = FRAME_BASELINE_OFFSET (it->f);
18918 font_info = NULL;
18919 }
18920 else
18921 {
18922 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18923 boff = font_info->baseline_offset;
18924 if (font_info->vertical_centering)
18925 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18926 }
18927
18928 if (it->char_to_display >= ' '
18929 && (!it->multibyte_p || it->char_to_display < 128))
18930 {
18931 /* Either unibyte or ASCII. */
18932 int stretched_p;
18933
18934 it->nglyphs = 1;
18935
18936 pcm = FRAME_RIF (it->f)->per_char_metric
18937 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18938
18939 if (it->override_ascent >= 0)
18940 {
18941 it->ascent = it->override_ascent;
18942 it->descent = it->override_descent;
18943 boff = it->override_boff;
18944 }
18945 else
18946 {
18947 it->ascent = FONT_BASE (font) + boff;
18948 it->descent = FONT_DESCENT (font) - boff;
18949 }
18950
18951 if (pcm)
18952 {
18953 it->phys_ascent = pcm->ascent + boff;
18954 it->phys_descent = pcm->descent - boff;
18955 it->pixel_width = pcm->width;
18956 }
18957 else
18958 {
18959 it->glyph_not_available_p = 1;
18960 it->phys_ascent = it->ascent;
18961 it->phys_descent = it->descent;
18962 it->pixel_width = FONT_WIDTH (font);
18963 }
18964
18965 if (it->constrain_row_ascent_descent_p)
18966 {
18967 if (it->descent > it->max_descent)
18968 {
18969 it->ascent += it->descent - it->max_descent;
18970 it->descent = it->max_descent;
18971 }
18972 if (it->ascent > it->max_ascent)
18973 {
18974 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18975 it->ascent = it->max_ascent;
18976 }
18977 it->phys_ascent = min (it->phys_ascent, it->ascent);
18978 it->phys_descent = min (it->phys_descent, it->descent);
18979 extra_line_spacing = 0;
18980 }
18981
18982 /* If this is a space inside a region of text with
18983 `space-width' property, change its width. */
18984 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18985 if (stretched_p)
18986 it->pixel_width *= XFLOATINT (it->space_width);
18987
18988 /* If face has a box, add the box thickness to the character
18989 height. If character has a box line to the left and/or
18990 right, add the box line width to the character's width. */
18991 if (face->box != FACE_NO_BOX)
18992 {
18993 int thick = face->box_line_width;
18994
18995 if (thick > 0)
18996 {
18997 it->ascent += thick;
18998 it->descent += thick;
18999 }
19000 else
19001 thick = -thick;
19002
19003 if (it->start_of_box_run_p)
19004 it->pixel_width += thick;
19005 if (it->end_of_box_run_p)
19006 it->pixel_width += thick;
19007 }
19008
19009 /* If face has an overline, add the height of the overline
19010 (1 pixel) and a 1 pixel margin to the character height. */
19011 if (face->overline_p)
19012 it->ascent += 2;
19013
19014 if (it->constrain_row_ascent_descent_p)
19015 {
19016 if (it->ascent > it->max_ascent)
19017 it->ascent = it->max_ascent;
19018 if (it->descent > it->max_descent)
19019 it->descent = it->max_descent;
19020 }
19021
19022 take_vertical_position_into_account (it);
19023
19024 /* If we have to actually produce glyphs, do it. */
19025 if (it->glyph_row)
19026 {
19027 if (stretched_p)
19028 {
19029 /* Translate a space with a `space-width' property
19030 into a stretch glyph. */
19031 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19032 / FONT_HEIGHT (font));
19033 append_stretch_glyph (it, it->object, it->pixel_width,
19034 it->ascent + it->descent, ascent);
19035 }
19036 else
19037 append_glyph (it);
19038
19039 /* If characters with lbearing or rbearing are displayed
19040 in this line, record that fact in a flag of the
19041 glyph row. This is used to optimize X output code. */
19042 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19043 it->glyph_row->contains_overlapping_glyphs_p = 1;
19044 }
19045 }
19046 else if (it->char_to_display == '\n')
19047 {
19048 /* A newline has no width but we need the height of the line.
19049 But if previous part of the line set a height, don't
19050 increase that height */
19051
19052 Lisp_Object height;
19053
19054 it->override_ascent = -1;
19055 it->pixel_width = 0;
19056 it->nglyphs = 0;
19057
19058 height = calc_line_height_property(it, Qline_height, font, boff, 0);
19059
19060 if (it->override_ascent >= 0)
19061 {
19062 it->ascent = it->override_ascent;
19063 it->descent = it->override_descent;
19064 boff = it->override_boff;
19065 }
19066 else
19067 {
19068 it->ascent = FONT_BASE (font) + boff;
19069 it->descent = FONT_DESCENT (font) - boff;
19070 }
19071
19072 if (EQ (height, make_number(0)))
19073 {
19074 if (it->descent > it->max_descent)
19075 {
19076 it->ascent += it->descent - it->max_descent;
19077 it->descent = it->max_descent;
19078 }
19079 if (it->ascent > it->max_ascent)
19080 {
19081 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19082 it->ascent = it->max_ascent;
19083 }
19084 it->phys_ascent = min (it->phys_ascent, it->ascent);
19085 it->phys_descent = min (it->phys_descent, it->descent);
19086 it->constrain_row_ascent_descent_p = 1;
19087 extra_line_spacing = 0;
19088 }
19089 else
19090 {
19091 Lisp_Object spacing;
19092 int total = 0;
19093
19094 it->phys_ascent = it->ascent;
19095 it->phys_descent = it->descent;
19096
19097 if ((it->max_ascent > 0 || it->max_descent > 0)
19098 && face->box != FACE_NO_BOX
19099 && face->box_line_width > 0)
19100 {
19101 it->ascent += face->box_line_width;
19102 it->descent += face->box_line_width;
19103 }
19104 if (!NILP (height)
19105 && XINT (height) > it->ascent + it->descent)
19106 it->ascent = XINT (height) - it->descent;
19107
19108 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19109 if (INTEGERP (spacing))
19110 {
19111 extra_line_spacing = XINT (spacing);
19112 if (total)
19113 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19114 }
19115 }
19116 }
19117 else if (it->char_to_display == '\t')
19118 {
19119 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19120 int x = it->current_x + it->continuation_lines_width;
19121 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19122
19123 /* If the distance from the current position to the next tab
19124 stop is less than a canonical character width, use the
19125 tab stop after that. */
19126 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19127 next_tab_x += tab_width;
19128
19129 it->pixel_width = next_tab_x - x;
19130 it->nglyphs = 1;
19131 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19132 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19133
19134 if (it->glyph_row)
19135 {
19136 append_stretch_glyph (it, it->object, it->pixel_width,
19137 it->ascent + it->descent, it->ascent);
19138 }
19139 }
19140 else
19141 {
19142 /* A multi-byte character. Assume that the display width of the
19143 character is the width of the character multiplied by the
19144 width of the font. */
19145
19146 /* If we found a font, this font should give us the right
19147 metrics. If we didn't find a font, use the frame's
19148 default font and calculate the width of the character
19149 from the charset width; this is what old redisplay code
19150 did. */
19151
19152 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19153 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19154
19155 if (font_not_found_p || !pcm)
19156 {
19157 int charset = CHAR_CHARSET (it->char_to_display);
19158
19159 it->glyph_not_available_p = 1;
19160 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19161 * CHARSET_WIDTH (charset));
19162 it->phys_ascent = FONT_BASE (font) + boff;
19163 it->phys_descent = FONT_DESCENT (font) - boff;
19164 }
19165 else
19166 {
19167 it->pixel_width = pcm->width;
19168 it->phys_ascent = pcm->ascent + boff;
19169 it->phys_descent = pcm->descent - boff;
19170 if (it->glyph_row
19171 && (pcm->lbearing < 0
19172 || pcm->rbearing > pcm->width))
19173 it->glyph_row->contains_overlapping_glyphs_p = 1;
19174 }
19175 it->nglyphs = 1;
19176 it->ascent = FONT_BASE (font) + boff;
19177 it->descent = FONT_DESCENT (font) - boff;
19178 if (face->box != FACE_NO_BOX)
19179 {
19180 int thick = face->box_line_width;
19181
19182 if (thick > 0)
19183 {
19184 it->ascent += thick;
19185 it->descent += thick;
19186 }
19187 else
19188 thick = - thick;
19189
19190 if (it->start_of_box_run_p)
19191 it->pixel_width += thick;
19192 if (it->end_of_box_run_p)
19193 it->pixel_width += thick;
19194 }
19195
19196 /* If face has an overline, add the height of the overline
19197 (1 pixel) and a 1 pixel margin to the character height. */
19198 if (face->overline_p)
19199 it->ascent += 2;
19200
19201 take_vertical_position_into_account (it);
19202
19203 if (it->glyph_row)
19204 append_glyph (it);
19205 }
19206 it->multibyte_p = saved_multibyte_p;
19207 }
19208 else if (it->what == IT_COMPOSITION)
19209 {
19210 /* Note: A composition is represented as one glyph in the
19211 glyph matrix. There are no padding glyphs. */
19212 XChar2b char2b;
19213 XFontStruct *font;
19214 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19215 XCharStruct *pcm;
19216 int font_not_found_p;
19217 struct font_info *font_info;
19218 int boff; /* baseline offset */
19219 struct composition *cmp = composition_table[it->cmp_id];
19220
19221 /* Maybe translate single-byte characters to multibyte. */
19222 it->char_to_display = it->c;
19223 if (unibyte_display_via_language_environment
19224 && SINGLE_BYTE_CHAR_P (it->c)
19225 && (it->c >= 0240
19226 || (it->c >= 0200
19227 && !NILP (Vnonascii_translation_table))))
19228 {
19229 it->char_to_display = unibyte_char_to_multibyte (it->c);
19230 }
19231
19232 /* Get face and font to use. Encode IT->char_to_display. */
19233 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19234 face = FACE_FROM_ID (it->f, it->face_id);
19235 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19236 &char2b, it->multibyte_p, 0);
19237 font = face->font;
19238
19239 /* When no suitable font found, use the default font. */
19240 font_not_found_p = font == NULL;
19241 if (font_not_found_p)
19242 {
19243 font = FRAME_FONT (it->f);
19244 boff = FRAME_BASELINE_OFFSET (it->f);
19245 font_info = NULL;
19246 }
19247 else
19248 {
19249 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19250 boff = font_info->baseline_offset;
19251 if (font_info->vertical_centering)
19252 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19253 }
19254
19255 /* There are no padding glyphs, so there is only one glyph to
19256 produce for the composition. Important is that pixel_width,
19257 ascent and descent are the values of what is drawn by
19258 draw_glyphs (i.e. the values of the overall glyphs composed). */
19259 it->nglyphs = 1;
19260
19261 /* If we have not yet calculated pixel size data of glyphs of
19262 the composition for the current face font, calculate them
19263 now. Theoretically, we have to check all fonts for the
19264 glyphs, but that requires much time and memory space. So,
19265 here we check only the font of the first glyph. This leads
19266 to incorrect display very rarely, and C-l (recenter) can
19267 correct the display anyway. */
19268 if (cmp->font != (void *) font)
19269 {
19270 /* Ascent and descent of the font of the first character of
19271 this composition (adjusted by baseline offset). Ascent
19272 and descent of overall glyphs should not be less than
19273 them respectively. */
19274 int font_ascent = FONT_BASE (font) + boff;
19275 int font_descent = FONT_DESCENT (font) - boff;
19276 /* Bounding box of the overall glyphs. */
19277 int leftmost, rightmost, lowest, highest;
19278 int i, width, ascent, descent;
19279
19280 cmp->font = (void *) font;
19281
19282 /* Initialize the bounding box. */
19283 if (font_info
19284 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19285 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19286 {
19287 width = pcm->width;
19288 ascent = pcm->ascent;
19289 descent = pcm->descent;
19290 }
19291 else
19292 {
19293 width = FONT_WIDTH (font);
19294 ascent = FONT_BASE (font);
19295 descent = FONT_DESCENT (font);
19296 }
19297
19298 rightmost = width;
19299 lowest = - descent + boff;
19300 highest = ascent + boff;
19301 leftmost = 0;
19302
19303 if (font_info
19304 && font_info->default_ascent
19305 && CHAR_TABLE_P (Vuse_default_ascent)
19306 && !NILP (Faref (Vuse_default_ascent,
19307 make_number (it->char_to_display))))
19308 highest = font_info->default_ascent + boff;
19309
19310 /* Draw the first glyph at the normal position. It may be
19311 shifted to right later if some other glyphs are drawn at
19312 the left. */
19313 cmp->offsets[0] = 0;
19314 cmp->offsets[1] = boff;
19315
19316 /* Set cmp->offsets for the remaining glyphs. */
19317 for (i = 1; i < cmp->glyph_len; i++)
19318 {
19319 int left, right, btm, top;
19320 int ch = COMPOSITION_GLYPH (cmp, i);
19321 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19322
19323 face = FACE_FROM_ID (it->f, face_id);
19324 get_char_face_and_encoding (it->f, ch, face->id,
19325 &char2b, it->multibyte_p, 0);
19326 font = face->font;
19327 if (font == NULL)
19328 {
19329 font = FRAME_FONT (it->f);
19330 boff = FRAME_BASELINE_OFFSET (it->f);
19331 font_info = NULL;
19332 }
19333 else
19334 {
19335 font_info
19336 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19337 boff = font_info->baseline_offset;
19338 if (font_info->vertical_centering)
19339 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19340 }
19341
19342 if (font_info
19343 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19344 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19345 {
19346 width = pcm->width;
19347 ascent = pcm->ascent;
19348 descent = pcm->descent;
19349 }
19350 else
19351 {
19352 width = FONT_WIDTH (font);
19353 ascent = 1;
19354 descent = 0;
19355 }
19356
19357 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19358 {
19359 /* Relative composition with or without
19360 alternate chars. */
19361 left = (leftmost + rightmost - width) / 2;
19362 btm = - descent + boff;
19363 if (font_info && font_info->relative_compose
19364 && (! CHAR_TABLE_P (Vignore_relative_composition)
19365 || NILP (Faref (Vignore_relative_composition,
19366 make_number (ch)))))
19367 {
19368
19369 if (- descent >= font_info->relative_compose)
19370 /* One extra pixel between two glyphs. */
19371 btm = highest + 1;
19372 else if (ascent <= 0)
19373 /* One extra pixel between two glyphs. */
19374 btm = lowest - 1 - ascent - descent;
19375 }
19376 }
19377 else
19378 {
19379 /* A composition rule is specified by an integer
19380 value that encodes global and new reference
19381 points (GREF and NREF). GREF and NREF are
19382 specified by numbers as below:
19383
19384 0---1---2 -- ascent
19385 | |
19386 | |
19387 | |
19388 9--10--11 -- center
19389 | |
19390 ---3---4---5--- baseline
19391 | |
19392 6---7---8 -- descent
19393 */
19394 int rule = COMPOSITION_RULE (cmp, i);
19395 int gref, nref, grefx, grefy, nrefx, nrefy;
19396
19397 COMPOSITION_DECODE_RULE (rule, gref, nref);
19398 grefx = gref % 3, nrefx = nref % 3;
19399 grefy = gref / 3, nrefy = nref / 3;
19400
19401 left = (leftmost
19402 + grefx * (rightmost - leftmost) / 2
19403 - nrefx * width / 2);
19404 btm = ((grefy == 0 ? highest
19405 : grefy == 1 ? 0
19406 : grefy == 2 ? lowest
19407 : (highest + lowest) / 2)
19408 - (nrefy == 0 ? ascent + descent
19409 : nrefy == 1 ? descent - boff
19410 : nrefy == 2 ? 0
19411 : (ascent + descent) / 2));
19412 }
19413
19414 cmp->offsets[i * 2] = left;
19415 cmp->offsets[i * 2 + 1] = btm + descent;
19416
19417 /* Update the bounding box of the overall glyphs. */
19418 right = left + width;
19419 top = btm + descent + ascent;
19420 if (left < leftmost)
19421 leftmost = left;
19422 if (right > rightmost)
19423 rightmost = right;
19424 if (top > highest)
19425 highest = top;
19426 if (btm < lowest)
19427 lowest = btm;
19428 }
19429
19430 /* If there are glyphs whose x-offsets are negative,
19431 shift all glyphs to the right and make all x-offsets
19432 non-negative. */
19433 if (leftmost < 0)
19434 {
19435 for (i = 0; i < cmp->glyph_len; i++)
19436 cmp->offsets[i * 2] -= leftmost;
19437 rightmost -= leftmost;
19438 }
19439
19440 cmp->pixel_width = rightmost;
19441 cmp->ascent = highest;
19442 cmp->descent = - lowest;
19443 if (cmp->ascent < font_ascent)
19444 cmp->ascent = font_ascent;
19445 if (cmp->descent < font_descent)
19446 cmp->descent = font_descent;
19447 }
19448
19449 it->pixel_width = cmp->pixel_width;
19450 it->ascent = it->phys_ascent = cmp->ascent;
19451 it->descent = it->phys_descent = cmp->descent;
19452
19453 if (face->box != FACE_NO_BOX)
19454 {
19455 int thick = face->box_line_width;
19456
19457 if (thick > 0)
19458 {
19459 it->ascent += thick;
19460 it->descent += thick;
19461 }
19462 else
19463 thick = - thick;
19464
19465 if (it->start_of_box_run_p)
19466 it->pixel_width += thick;
19467 if (it->end_of_box_run_p)
19468 it->pixel_width += thick;
19469 }
19470
19471 /* If face has an overline, add the height of the overline
19472 (1 pixel) and a 1 pixel margin to the character height. */
19473 if (face->overline_p)
19474 it->ascent += 2;
19475
19476 take_vertical_position_into_account (it);
19477
19478 if (it->glyph_row)
19479 append_composite_glyph (it);
19480 }
19481 else if (it->what == IT_IMAGE)
19482 produce_image_glyph (it);
19483 else if (it->what == IT_STRETCH)
19484 produce_stretch_glyph (it);
19485
19486 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19487 because this isn't true for images with `:ascent 100'. */
19488 xassert (it->ascent >= 0 && it->descent >= 0);
19489 if (it->area == TEXT_AREA)
19490 it->current_x += it->pixel_width;
19491
19492 if (extra_line_spacing > 0)
19493 {
19494 it->descent += extra_line_spacing;
19495 if (extra_line_spacing > it->max_extra_line_spacing)
19496 it->max_extra_line_spacing = extra_line_spacing;
19497 }
19498
19499 it->max_ascent = max (it->max_ascent, it->ascent);
19500 it->max_descent = max (it->max_descent, it->descent);
19501 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19502 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19503 }
19504
19505 /* EXPORT for RIF:
19506 Output LEN glyphs starting at START at the nominal cursor position.
19507 Advance the nominal cursor over the text. The global variable
19508 updated_window contains the window being updated, updated_row is
19509 the glyph row being updated, and updated_area is the area of that
19510 row being updated. */
19511
19512 void
19513 x_write_glyphs (start, len)
19514 struct glyph *start;
19515 int len;
19516 {
19517 int x, hpos;
19518
19519 xassert (updated_window && updated_row);
19520 BLOCK_INPUT;
19521
19522 /* Write glyphs. */
19523
19524 hpos = start - updated_row->glyphs[updated_area];
19525 x = draw_glyphs (updated_window, output_cursor.x,
19526 updated_row, updated_area,
19527 hpos, hpos + len,
19528 DRAW_NORMAL_TEXT, 0);
19529
19530 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19531 if (updated_area == TEXT_AREA
19532 && updated_window->phys_cursor_on_p
19533 && updated_window->phys_cursor.vpos == output_cursor.vpos
19534 && updated_window->phys_cursor.hpos >= hpos
19535 && updated_window->phys_cursor.hpos < hpos + len)
19536 updated_window->phys_cursor_on_p = 0;
19537
19538 UNBLOCK_INPUT;
19539
19540 /* Advance the output cursor. */
19541 output_cursor.hpos += len;
19542 output_cursor.x = x;
19543 }
19544
19545
19546 /* EXPORT for RIF:
19547 Insert LEN glyphs from START at the nominal cursor position. */
19548
19549 void
19550 x_insert_glyphs (start, len)
19551 struct glyph *start;
19552 int len;
19553 {
19554 struct frame *f;
19555 struct window *w;
19556 int line_height, shift_by_width, shifted_region_width;
19557 struct glyph_row *row;
19558 struct glyph *glyph;
19559 int frame_x, frame_y, hpos;
19560
19561 xassert (updated_window && updated_row);
19562 BLOCK_INPUT;
19563 w = updated_window;
19564 f = XFRAME (WINDOW_FRAME (w));
19565
19566 /* Get the height of the line we are in. */
19567 row = updated_row;
19568 line_height = row->height;
19569
19570 /* Get the width of the glyphs to insert. */
19571 shift_by_width = 0;
19572 for (glyph = start; glyph < start + len; ++glyph)
19573 shift_by_width += glyph->pixel_width;
19574
19575 /* Get the width of the region to shift right. */
19576 shifted_region_width = (window_box_width (w, updated_area)
19577 - output_cursor.x
19578 - shift_by_width);
19579
19580 /* Shift right. */
19581 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19582 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19583
19584 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19585 line_height, shift_by_width);
19586
19587 /* Write the glyphs. */
19588 hpos = start - row->glyphs[updated_area];
19589 draw_glyphs (w, output_cursor.x, row, updated_area,
19590 hpos, hpos + len,
19591 DRAW_NORMAL_TEXT, 0);
19592
19593 /* Advance the output cursor. */
19594 output_cursor.hpos += len;
19595 output_cursor.x += shift_by_width;
19596 UNBLOCK_INPUT;
19597 }
19598
19599
19600 /* EXPORT for RIF:
19601 Erase the current text line from the nominal cursor position
19602 (inclusive) to pixel column TO_X (exclusive). The idea is that
19603 everything from TO_X onward is already erased.
19604
19605 TO_X is a pixel position relative to updated_area of
19606 updated_window. TO_X == -1 means clear to the end of this area. */
19607
19608 void
19609 x_clear_end_of_line (to_x)
19610 int to_x;
19611 {
19612 struct frame *f;
19613 struct window *w = updated_window;
19614 int max_x, min_y, max_y;
19615 int from_x, from_y, to_y;
19616
19617 xassert (updated_window && updated_row);
19618 f = XFRAME (w->frame);
19619
19620 if (updated_row->full_width_p)
19621 max_x = WINDOW_TOTAL_WIDTH (w);
19622 else
19623 max_x = window_box_width (w, updated_area);
19624 max_y = window_text_bottom_y (w);
19625
19626 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19627 of window. For TO_X > 0, truncate to end of drawing area. */
19628 if (to_x == 0)
19629 return;
19630 else if (to_x < 0)
19631 to_x = max_x;
19632 else
19633 to_x = min (to_x, max_x);
19634
19635 to_y = min (max_y, output_cursor.y + updated_row->height);
19636
19637 /* Notice if the cursor will be cleared by this operation. */
19638 if (!updated_row->full_width_p)
19639 notice_overwritten_cursor (w, updated_area,
19640 output_cursor.x, -1,
19641 updated_row->y,
19642 MATRIX_ROW_BOTTOM_Y (updated_row));
19643
19644 from_x = output_cursor.x;
19645
19646 /* Translate to frame coordinates. */
19647 if (updated_row->full_width_p)
19648 {
19649 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19650 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19651 }
19652 else
19653 {
19654 int area_left = window_box_left (w, updated_area);
19655 from_x += area_left;
19656 to_x += area_left;
19657 }
19658
19659 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19660 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19661 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19662
19663 /* Prevent inadvertently clearing to end of the X window. */
19664 if (to_x > from_x && to_y > from_y)
19665 {
19666 BLOCK_INPUT;
19667 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19668 to_x - from_x, to_y - from_y);
19669 UNBLOCK_INPUT;
19670 }
19671 }
19672
19673 #endif /* HAVE_WINDOW_SYSTEM */
19674
19675
19676 \f
19677 /***********************************************************************
19678 Cursor types
19679 ***********************************************************************/
19680
19681 /* Value is the internal representation of the specified cursor type
19682 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19683 of the bar cursor. */
19684
19685 static enum text_cursor_kinds
19686 get_specified_cursor_type (arg, width)
19687 Lisp_Object arg;
19688 int *width;
19689 {
19690 enum text_cursor_kinds type;
19691
19692 if (NILP (arg))
19693 return NO_CURSOR;
19694
19695 if (EQ (arg, Qbox))
19696 return FILLED_BOX_CURSOR;
19697
19698 if (EQ (arg, Qhollow))
19699 return HOLLOW_BOX_CURSOR;
19700
19701 if (EQ (arg, Qbar))
19702 {
19703 *width = 2;
19704 return BAR_CURSOR;
19705 }
19706
19707 if (CONSP (arg)
19708 && EQ (XCAR (arg), Qbar)
19709 && INTEGERP (XCDR (arg))
19710 && XINT (XCDR (arg)) >= 0)
19711 {
19712 *width = XINT (XCDR (arg));
19713 return BAR_CURSOR;
19714 }
19715
19716 if (EQ (arg, Qhbar))
19717 {
19718 *width = 2;
19719 return HBAR_CURSOR;
19720 }
19721
19722 if (CONSP (arg)
19723 && EQ (XCAR (arg), Qhbar)
19724 && INTEGERP (XCDR (arg))
19725 && XINT (XCDR (arg)) >= 0)
19726 {
19727 *width = XINT (XCDR (arg));
19728 return HBAR_CURSOR;
19729 }
19730
19731 /* Treat anything unknown as "hollow box cursor".
19732 It was bad to signal an error; people have trouble fixing
19733 .Xdefaults with Emacs, when it has something bad in it. */
19734 type = HOLLOW_BOX_CURSOR;
19735
19736 return type;
19737 }
19738
19739 /* Set the default cursor types for specified frame. */
19740 void
19741 set_frame_cursor_types (f, arg)
19742 struct frame *f;
19743 Lisp_Object arg;
19744 {
19745 int width;
19746 Lisp_Object tem;
19747
19748 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19749 FRAME_CURSOR_WIDTH (f) = width;
19750
19751 /* By default, set up the blink-off state depending on the on-state. */
19752
19753 tem = Fassoc (arg, Vblink_cursor_alist);
19754 if (!NILP (tem))
19755 {
19756 FRAME_BLINK_OFF_CURSOR (f)
19757 = get_specified_cursor_type (XCDR (tem), &width);
19758 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19759 }
19760 else
19761 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19762 }
19763
19764
19765 /* Return the cursor we want to be displayed in window W. Return
19766 width of bar/hbar cursor through WIDTH arg. Return with
19767 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19768 (i.e. if the `system caret' should track this cursor).
19769
19770 In a mini-buffer window, we want the cursor only to appear if we
19771 are reading input from this window. For the selected window, we
19772 want the cursor type given by the frame parameter or buffer local
19773 setting of cursor-type. If explicitly marked off, draw no cursor.
19774 In all other cases, we want a hollow box cursor. */
19775
19776 static enum text_cursor_kinds
19777 get_window_cursor_type (w, glyph, width, active_cursor)
19778 struct window *w;
19779 struct glyph *glyph;
19780 int *width;
19781 int *active_cursor;
19782 {
19783 struct frame *f = XFRAME (w->frame);
19784 struct buffer *b = XBUFFER (w->buffer);
19785 int cursor_type = DEFAULT_CURSOR;
19786 Lisp_Object alt_cursor;
19787 int non_selected = 0;
19788
19789 *active_cursor = 1;
19790
19791 /* Echo area */
19792 if (cursor_in_echo_area
19793 && FRAME_HAS_MINIBUF_P (f)
19794 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19795 {
19796 if (w == XWINDOW (echo_area_window))
19797 {
19798 *width = FRAME_CURSOR_WIDTH (f);
19799 return FRAME_DESIRED_CURSOR (f);
19800 }
19801
19802 *active_cursor = 0;
19803 non_selected = 1;
19804 }
19805
19806 /* Nonselected window or nonselected frame. */
19807 else if (w != XWINDOW (f->selected_window)
19808 #ifdef HAVE_WINDOW_SYSTEM
19809 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19810 #endif
19811 )
19812 {
19813 *active_cursor = 0;
19814
19815 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19816 return NO_CURSOR;
19817
19818 non_selected = 1;
19819 }
19820
19821 /* Never display a cursor in a window in which cursor-type is nil. */
19822 if (NILP (b->cursor_type))
19823 return NO_CURSOR;
19824
19825 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19826 if (non_selected)
19827 {
19828 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19829 return get_specified_cursor_type (alt_cursor, width);
19830 }
19831
19832 /* Get the normal cursor type for this window. */
19833 if (EQ (b->cursor_type, Qt))
19834 {
19835 cursor_type = FRAME_DESIRED_CURSOR (f);
19836 *width = FRAME_CURSOR_WIDTH (f);
19837 }
19838 else
19839 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19840
19841 /* Use normal cursor if not blinked off. */
19842 if (!w->cursor_off_p)
19843 {
19844 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19845 if (cursor_type == FILLED_BOX_CURSOR)
19846 cursor_type = HOLLOW_BOX_CURSOR;
19847 }
19848 return cursor_type;
19849 }
19850
19851 /* Cursor is blinked off, so determine how to "toggle" it. */
19852
19853 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19854 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19855 return get_specified_cursor_type (XCDR (alt_cursor), width);
19856
19857 /* Then see if frame has specified a specific blink off cursor type. */
19858 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19859 {
19860 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19861 return FRAME_BLINK_OFF_CURSOR (f);
19862 }
19863
19864 #if 0
19865 /* Some people liked having a permanently visible blinking cursor,
19866 while others had very strong opinions against it. So it was
19867 decided to remove it. KFS 2003-09-03 */
19868
19869 /* Finally perform built-in cursor blinking:
19870 filled box <-> hollow box
19871 wide [h]bar <-> narrow [h]bar
19872 narrow [h]bar <-> no cursor
19873 other type <-> no cursor */
19874
19875 if (cursor_type == FILLED_BOX_CURSOR)
19876 return HOLLOW_BOX_CURSOR;
19877
19878 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19879 {
19880 *width = 1;
19881 return cursor_type;
19882 }
19883 #endif
19884
19885 return NO_CURSOR;
19886 }
19887
19888
19889 #ifdef HAVE_WINDOW_SYSTEM
19890
19891 /* Notice when the text cursor of window W has been completely
19892 overwritten by a drawing operation that outputs glyphs in AREA
19893 starting at X0 and ending at X1 in the line starting at Y0 and
19894 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19895 the rest of the line after X0 has been written. Y coordinates
19896 are window-relative. */
19897
19898 static void
19899 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19900 struct window *w;
19901 enum glyph_row_area area;
19902 int x0, y0, x1, y1;
19903 {
19904 int cx0, cx1, cy0, cy1;
19905 struct glyph_row *row;
19906
19907 if (!w->phys_cursor_on_p)
19908 return;
19909 if (area != TEXT_AREA)
19910 return;
19911
19912 row = w->current_matrix->rows + w->phys_cursor.vpos;
19913 if (!row->displays_text_p)
19914 return;
19915
19916 if (row->cursor_in_fringe_p)
19917 {
19918 row->cursor_in_fringe_p = 0;
19919 draw_fringe_bitmap (w, row, 0);
19920 w->phys_cursor_on_p = 0;
19921 return;
19922 }
19923
19924 cx0 = w->phys_cursor.x;
19925 cx1 = cx0 + w->phys_cursor_width;
19926 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19927 return;
19928
19929 /* The cursor image will be completely removed from the
19930 screen if the output area intersects the cursor area in
19931 y-direction. When we draw in [y0 y1[, and some part of
19932 the cursor is at y < y0, that part must have been drawn
19933 before. When scrolling, the cursor is erased before
19934 actually scrolling, so we don't come here. When not
19935 scrolling, the rows above the old cursor row must have
19936 changed, and in this case these rows must have written
19937 over the cursor image.
19938
19939 Likewise if part of the cursor is below y1, with the
19940 exception of the cursor being in the first blank row at
19941 the buffer and window end because update_text_area
19942 doesn't draw that row. (Except when it does, but
19943 that's handled in update_text_area.) */
19944
19945 cy0 = w->phys_cursor.y;
19946 cy1 = cy0 + w->phys_cursor_height;
19947 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19948 return;
19949
19950 w->phys_cursor_on_p = 0;
19951 }
19952
19953 #endif /* HAVE_WINDOW_SYSTEM */
19954
19955 \f
19956 /************************************************************************
19957 Mouse Face
19958 ************************************************************************/
19959
19960 #ifdef HAVE_WINDOW_SYSTEM
19961
19962 /* EXPORT for RIF:
19963 Fix the display of area AREA of overlapping row ROW in window W. */
19964
19965 void
19966 x_fix_overlapping_area (w, row, area)
19967 struct window *w;
19968 struct glyph_row *row;
19969 enum glyph_row_area area;
19970 {
19971 int i, x;
19972
19973 BLOCK_INPUT;
19974
19975 x = 0;
19976 for (i = 0; i < row->used[area];)
19977 {
19978 if (row->glyphs[area][i].overlaps_vertically_p)
19979 {
19980 int start = i, start_x = x;
19981
19982 do
19983 {
19984 x += row->glyphs[area][i].pixel_width;
19985 ++i;
19986 }
19987 while (i < row->used[area]
19988 && row->glyphs[area][i].overlaps_vertically_p);
19989
19990 draw_glyphs (w, start_x, row, area,
19991 start, i,
19992 DRAW_NORMAL_TEXT, 1);
19993 }
19994 else
19995 {
19996 x += row->glyphs[area][i].pixel_width;
19997 ++i;
19998 }
19999 }
20000
20001 UNBLOCK_INPUT;
20002 }
20003
20004
20005 /* EXPORT:
20006 Draw the cursor glyph of window W in glyph row ROW. See the
20007 comment of draw_glyphs for the meaning of HL. */
20008
20009 void
20010 draw_phys_cursor_glyph (w, row, hl)
20011 struct window *w;
20012 struct glyph_row *row;
20013 enum draw_glyphs_face hl;
20014 {
20015 /* If cursor hpos is out of bounds, don't draw garbage. This can
20016 happen in mini-buffer windows when switching between echo area
20017 glyphs and mini-buffer. */
20018 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20019 {
20020 int on_p = w->phys_cursor_on_p;
20021 int x1;
20022 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20023 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20024 hl, 0);
20025 w->phys_cursor_on_p = on_p;
20026
20027 if (hl == DRAW_CURSOR)
20028 w->phys_cursor_width = x1 - w->phys_cursor.x;
20029 /* When we erase the cursor, and ROW is overlapped by other
20030 rows, make sure that these overlapping parts of other rows
20031 are redrawn. */
20032 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20033 {
20034 if (row > w->current_matrix->rows
20035 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20036 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20037
20038 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20039 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20040 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20041 }
20042 }
20043 }
20044
20045
20046 /* EXPORT:
20047 Erase the image of a cursor of window W from the screen. */
20048
20049 void
20050 erase_phys_cursor (w)
20051 struct window *w;
20052 {
20053 struct frame *f = XFRAME (w->frame);
20054 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20055 int hpos = w->phys_cursor.hpos;
20056 int vpos = w->phys_cursor.vpos;
20057 int mouse_face_here_p = 0;
20058 struct glyph_matrix *active_glyphs = w->current_matrix;
20059 struct glyph_row *cursor_row;
20060 struct glyph *cursor_glyph;
20061 enum draw_glyphs_face hl;
20062
20063 /* No cursor displayed or row invalidated => nothing to do on the
20064 screen. */
20065 if (w->phys_cursor_type == NO_CURSOR)
20066 goto mark_cursor_off;
20067
20068 /* VPOS >= active_glyphs->nrows means that window has been resized.
20069 Don't bother to erase the cursor. */
20070 if (vpos >= active_glyphs->nrows)
20071 goto mark_cursor_off;
20072
20073 /* If row containing cursor is marked invalid, there is nothing we
20074 can do. */
20075 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20076 if (!cursor_row->enabled_p)
20077 goto mark_cursor_off;
20078
20079 /* If line spacing is > 0, old cursor may only be partially visible in
20080 window after split-window. So adjust visible height. */
20081 cursor_row->visible_height = min (cursor_row->visible_height,
20082 window_text_bottom_y (w) - cursor_row->y);
20083
20084 /* If row is completely invisible, don't attempt to delete a cursor which
20085 isn't there. This can happen if cursor is at top of a window, and
20086 we switch to a buffer with a header line in that window. */
20087 if (cursor_row->visible_height <= 0)
20088 goto mark_cursor_off;
20089
20090 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20091 if (cursor_row->cursor_in_fringe_p)
20092 {
20093 cursor_row->cursor_in_fringe_p = 0;
20094 draw_fringe_bitmap (w, cursor_row, 0);
20095 goto mark_cursor_off;
20096 }
20097
20098 /* This can happen when the new row is shorter than the old one.
20099 In this case, either draw_glyphs or clear_end_of_line
20100 should have cleared the cursor. Note that we wouldn't be
20101 able to erase the cursor in this case because we don't have a
20102 cursor glyph at hand. */
20103 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20104 goto mark_cursor_off;
20105
20106 /* If the cursor is in the mouse face area, redisplay that when
20107 we clear the cursor. */
20108 if (! NILP (dpyinfo->mouse_face_window)
20109 && w == XWINDOW (dpyinfo->mouse_face_window)
20110 && (vpos > dpyinfo->mouse_face_beg_row
20111 || (vpos == dpyinfo->mouse_face_beg_row
20112 && hpos >= dpyinfo->mouse_face_beg_col))
20113 && (vpos < dpyinfo->mouse_face_end_row
20114 || (vpos == dpyinfo->mouse_face_end_row
20115 && hpos < dpyinfo->mouse_face_end_col))
20116 /* Don't redraw the cursor's spot in mouse face if it is at the
20117 end of a line (on a newline). The cursor appears there, but
20118 mouse highlighting does not. */
20119 && cursor_row->used[TEXT_AREA] > hpos)
20120 mouse_face_here_p = 1;
20121
20122 /* Maybe clear the display under the cursor. */
20123 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20124 {
20125 int x, y;
20126 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20127 int width;
20128
20129 cursor_glyph = get_phys_cursor_glyph (w);
20130 if (cursor_glyph == NULL)
20131 goto mark_cursor_off;
20132
20133 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20134 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20135 width = min (cursor_glyph->pixel_width,
20136 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20137
20138 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20139 }
20140
20141 /* Erase the cursor by redrawing the character underneath it. */
20142 if (mouse_face_here_p)
20143 hl = DRAW_MOUSE_FACE;
20144 else
20145 hl = DRAW_NORMAL_TEXT;
20146 draw_phys_cursor_glyph (w, cursor_row, hl);
20147
20148 mark_cursor_off:
20149 w->phys_cursor_on_p = 0;
20150 w->phys_cursor_type = NO_CURSOR;
20151 }
20152
20153
20154 /* EXPORT:
20155 Display or clear cursor of window W. If ON is zero, clear the
20156 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20157 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20158
20159 void
20160 display_and_set_cursor (w, on, hpos, vpos, x, y)
20161 struct window *w;
20162 int on, hpos, vpos, x, y;
20163 {
20164 struct frame *f = XFRAME (w->frame);
20165 int new_cursor_type;
20166 int new_cursor_width;
20167 int active_cursor;
20168 struct glyph_row *glyph_row;
20169 struct glyph *glyph;
20170
20171 /* This is pointless on invisible frames, and dangerous on garbaged
20172 windows and frames; in the latter case, the frame or window may
20173 be in the midst of changing its size, and x and y may be off the
20174 window. */
20175 if (! FRAME_VISIBLE_P (f)
20176 || FRAME_GARBAGED_P (f)
20177 || vpos >= w->current_matrix->nrows
20178 || hpos >= w->current_matrix->matrix_w)
20179 return;
20180
20181 /* If cursor is off and we want it off, return quickly. */
20182 if (!on && !w->phys_cursor_on_p)
20183 return;
20184
20185 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20186 /* If cursor row is not enabled, we don't really know where to
20187 display the cursor. */
20188 if (!glyph_row->enabled_p)
20189 {
20190 w->phys_cursor_on_p = 0;
20191 return;
20192 }
20193
20194 glyph = NULL;
20195 if (!glyph_row->exact_window_width_line_p
20196 || hpos < glyph_row->used[TEXT_AREA])
20197 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20198
20199 xassert (interrupt_input_blocked);
20200
20201 /* Set new_cursor_type to the cursor we want to be displayed. */
20202 new_cursor_type = get_window_cursor_type (w, glyph,
20203 &new_cursor_width, &active_cursor);
20204
20205 /* If cursor is currently being shown and we don't want it to be or
20206 it is in the wrong place, or the cursor type is not what we want,
20207 erase it. */
20208 if (w->phys_cursor_on_p
20209 && (!on
20210 || w->phys_cursor.x != x
20211 || w->phys_cursor.y != y
20212 || new_cursor_type != w->phys_cursor_type
20213 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20214 && new_cursor_width != w->phys_cursor_width)))
20215 erase_phys_cursor (w);
20216
20217 /* Don't check phys_cursor_on_p here because that flag is only set
20218 to zero in some cases where we know that the cursor has been
20219 completely erased, to avoid the extra work of erasing the cursor
20220 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20221 still not be visible, or it has only been partly erased. */
20222 if (on)
20223 {
20224 w->phys_cursor_ascent = glyph_row->ascent;
20225 w->phys_cursor_height = glyph_row->height;
20226
20227 /* Set phys_cursor_.* before x_draw_.* is called because some
20228 of them may need the information. */
20229 w->phys_cursor.x = x;
20230 w->phys_cursor.y = glyph_row->y;
20231 w->phys_cursor.hpos = hpos;
20232 w->phys_cursor.vpos = vpos;
20233 }
20234
20235 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
20236 new_cursor_type, new_cursor_width,
20237 on, active_cursor);
20238 }
20239
20240
20241 /* Switch the display of W's cursor on or off, according to the value
20242 of ON. */
20243
20244 static void
20245 update_window_cursor (w, on)
20246 struct window *w;
20247 int on;
20248 {
20249 /* Don't update cursor in windows whose frame is in the process
20250 of being deleted. */
20251 if (w->current_matrix)
20252 {
20253 BLOCK_INPUT;
20254 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20255 w->phys_cursor.x, w->phys_cursor.y);
20256 UNBLOCK_INPUT;
20257 }
20258 }
20259
20260
20261 /* Call update_window_cursor with parameter ON_P on all leaf windows
20262 in the window tree rooted at W. */
20263
20264 static void
20265 update_cursor_in_window_tree (w, on_p)
20266 struct window *w;
20267 int on_p;
20268 {
20269 while (w)
20270 {
20271 if (!NILP (w->hchild))
20272 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20273 else if (!NILP (w->vchild))
20274 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20275 else
20276 update_window_cursor (w, on_p);
20277
20278 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20279 }
20280 }
20281
20282
20283 /* EXPORT:
20284 Display the cursor on window W, or clear it, according to ON_P.
20285 Don't change the cursor's position. */
20286
20287 void
20288 x_update_cursor (f, on_p)
20289 struct frame *f;
20290 int on_p;
20291 {
20292 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20293 }
20294
20295
20296 /* EXPORT:
20297 Clear the cursor of window W to background color, and mark the
20298 cursor as not shown. This is used when the text where the cursor
20299 is is about to be rewritten. */
20300
20301 void
20302 x_clear_cursor (w)
20303 struct window *w;
20304 {
20305 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20306 update_window_cursor (w, 0);
20307 }
20308
20309
20310 /* EXPORT:
20311 Display the active region described by mouse_face_* according to DRAW. */
20312
20313 void
20314 show_mouse_face (dpyinfo, draw)
20315 Display_Info *dpyinfo;
20316 enum draw_glyphs_face draw;
20317 {
20318 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20319 struct frame *f = XFRAME (WINDOW_FRAME (w));
20320
20321 if (/* If window is in the process of being destroyed, don't bother
20322 to do anything. */
20323 w->current_matrix != NULL
20324 /* Don't update mouse highlight if hidden */
20325 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20326 /* Recognize when we are called to operate on rows that don't exist
20327 anymore. This can happen when a window is split. */
20328 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20329 {
20330 int phys_cursor_on_p = w->phys_cursor_on_p;
20331 struct glyph_row *row, *first, *last;
20332
20333 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20334 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20335
20336 for (row = first; row <= last && row->enabled_p; ++row)
20337 {
20338 int start_hpos, end_hpos, start_x;
20339
20340 /* For all but the first row, the highlight starts at column 0. */
20341 if (row == first)
20342 {
20343 start_hpos = dpyinfo->mouse_face_beg_col;
20344 start_x = dpyinfo->mouse_face_beg_x;
20345 }
20346 else
20347 {
20348 start_hpos = 0;
20349 start_x = 0;
20350 }
20351
20352 if (row == last)
20353 end_hpos = dpyinfo->mouse_face_end_col;
20354 else
20355 end_hpos = row->used[TEXT_AREA];
20356
20357 if (end_hpos > start_hpos)
20358 {
20359 draw_glyphs (w, start_x, row, TEXT_AREA,
20360 start_hpos, end_hpos,
20361 draw, 0);
20362
20363 row->mouse_face_p
20364 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20365 }
20366 }
20367
20368 /* When we've written over the cursor, arrange for it to
20369 be displayed again. */
20370 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20371 {
20372 BLOCK_INPUT;
20373 display_and_set_cursor (w, 1,
20374 w->phys_cursor.hpos, w->phys_cursor.vpos,
20375 w->phys_cursor.x, w->phys_cursor.y);
20376 UNBLOCK_INPUT;
20377 }
20378 }
20379
20380 /* Change the mouse cursor. */
20381 if (draw == DRAW_NORMAL_TEXT)
20382 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20383 else if (draw == DRAW_MOUSE_FACE)
20384 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20385 else
20386 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20387 }
20388
20389 /* EXPORT:
20390 Clear out the mouse-highlighted active region.
20391 Redraw it un-highlighted first. Value is non-zero if mouse
20392 face was actually drawn unhighlighted. */
20393
20394 int
20395 clear_mouse_face (dpyinfo)
20396 Display_Info *dpyinfo;
20397 {
20398 int cleared = 0;
20399
20400 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20401 {
20402 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20403 cleared = 1;
20404 }
20405
20406 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20407 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20408 dpyinfo->mouse_face_window = Qnil;
20409 dpyinfo->mouse_face_overlay = Qnil;
20410 return cleared;
20411 }
20412
20413
20414 /* EXPORT:
20415 Non-zero if physical cursor of window W is within mouse face. */
20416
20417 int
20418 cursor_in_mouse_face_p (w)
20419 struct window *w;
20420 {
20421 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20422 int in_mouse_face = 0;
20423
20424 if (WINDOWP (dpyinfo->mouse_face_window)
20425 && XWINDOW (dpyinfo->mouse_face_window) == w)
20426 {
20427 int hpos = w->phys_cursor.hpos;
20428 int vpos = w->phys_cursor.vpos;
20429
20430 if (vpos >= dpyinfo->mouse_face_beg_row
20431 && vpos <= dpyinfo->mouse_face_end_row
20432 && (vpos > dpyinfo->mouse_face_beg_row
20433 || hpos >= dpyinfo->mouse_face_beg_col)
20434 && (vpos < dpyinfo->mouse_face_end_row
20435 || hpos < dpyinfo->mouse_face_end_col
20436 || dpyinfo->mouse_face_past_end))
20437 in_mouse_face = 1;
20438 }
20439
20440 return in_mouse_face;
20441 }
20442
20443
20444
20445 \f
20446 /* Find the glyph matrix position of buffer position CHARPOS in window
20447 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20448 current glyphs must be up to date. If CHARPOS is above window
20449 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20450 of last line in W. In the row containing CHARPOS, stop before glyphs
20451 having STOP as object. */
20452
20453 #if 1 /* This is a version of fast_find_position that's more correct
20454 in the presence of hscrolling, for example. I didn't install
20455 it right away because the problem fixed is minor, it failed
20456 in 20.x as well, and I think it's too risky to install
20457 so near the release of 21.1. 2001-09-25 gerd. */
20458
20459 static int
20460 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20461 struct window *w;
20462 int charpos;
20463 int *hpos, *vpos, *x, *y;
20464 Lisp_Object stop;
20465 {
20466 struct glyph_row *row, *first;
20467 struct glyph *glyph, *end;
20468 int past_end = 0;
20469
20470 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20471 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20472 {
20473 *x = first->x;
20474 *y = first->y;
20475 *hpos = 0;
20476 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20477 return 1;
20478 }
20479
20480 row = row_containing_pos (w, charpos, first, NULL, 0);
20481 if (row == NULL)
20482 {
20483 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20484 past_end = 1;
20485 }
20486
20487 *x = row->x;
20488 *y = row->y;
20489 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20490
20491 glyph = row->glyphs[TEXT_AREA];
20492 end = glyph + row->used[TEXT_AREA];
20493
20494 /* Skip over glyphs not having an object at the start of the row.
20495 These are special glyphs like truncation marks on terminal
20496 frames. */
20497 if (row->displays_text_p)
20498 while (glyph < end
20499 && INTEGERP (glyph->object)
20500 && !EQ (stop, glyph->object)
20501 && glyph->charpos < 0)
20502 {
20503 *x += glyph->pixel_width;
20504 ++glyph;
20505 }
20506
20507 while (glyph < end
20508 && !INTEGERP (glyph->object)
20509 && !EQ (stop, glyph->object)
20510 && (!BUFFERP (glyph->object)
20511 || glyph->charpos < charpos))
20512 {
20513 *x += glyph->pixel_width;
20514 ++glyph;
20515 }
20516
20517 *hpos = glyph - row->glyphs[TEXT_AREA];
20518 return !past_end;
20519 }
20520
20521 #else /* not 1 */
20522
20523 static int
20524 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20525 struct window *w;
20526 int pos;
20527 int *hpos, *vpos, *x, *y;
20528 Lisp_Object stop;
20529 {
20530 int i;
20531 int lastcol;
20532 int maybe_next_line_p = 0;
20533 int line_start_position;
20534 int yb = window_text_bottom_y (w);
20535 struct glyph_row *row, *best_row;
20536 int row_vpos, best_row_vpos;
20537 int current_x;
20538
20539 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20540 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20541
20542 while (row->y < yb)
20543 {
20544 if (row->used[TEXT_AREA])
20545 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20546 else
20547 line_start_position = 0;
20548
20549 if (line_start_position > pos)
20550 break;
20551 /* If the position sought is the end of the buffer,
20552 don't include the blank lines at the bottom of the window. */
20553 else if (line_start_position == pos
20554 && pos == BUF_ZV (XBUFFER (w->buffer)))
20555 {
20556 maybe_next_line_p = 1;
20557 break;
20558 }
20559 else if (line_start_position > 0)
20560 {
20561 best_row = row;
20562 best_row_vpos = row_vpos;
20563 }
20564
20565 if (row->y + row->height >= yb)
20566 break;
20567
20568 ++row;
20569 ++row_vpos;
20570 }
20571
20572 /* Find the right column within BEST_ROW. */
20573 lastcol = 0;
20574 current_x = best_row->x;
20575 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20576 {
20577 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20578 int charpos = glyph->charpos;
20579
20580 if (BUFFERP (glyph->object))
20581 {
20582 if (charpos == pos)
20583 {
20584 *hpos = i;
20585 *vpos = best_row_vpos;
20586 *x = current_x;
20587 *y = best_row->y;
20588 return 1;
20589 }
20590 else if (charpos > pos)
20591 break;
20592 }
20593 else if (EQ (glyph->object, stop))
20594 break;
20595
20596 if (charpos > 0)
20597 lastcol = i;
20598 current_x += glyph->pixel_width;
20599 }
20600
20601 /* If we're looking for the end of the buffer,
20602 and we didn't find it in the line we scanned,
20603 use the start of the following line. */
20604 if (maybe_next_line_p)
20605 {
20606 ++best_row;
20607 ++best_row_vpos;
20608 lastcol = 0;
20609 current_x = best_row->x;
20610 }
20611
20612 *vpos = best_row_vpos;
20613 *hpos = lastcol + 1;
20614 *x = current_x;
20615 *y = best_row->y;
20616 return 0;
20617 }
20618
20619 #endif /* not 1 */
20620
20621
20622 /* Find the position of the glyph for position POS in OBJECT in
20623 window W's current matrix, and return in *X, *Y the pixel
20624 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20625
20626 RIGHT_P non-zero means return the position of the right edge of the
20627 glyph, RIGHT_P zero means return the left edge position.
20628
20629 If no glyph for POS exists in the matrix, return the position of
20630 the glyph with the next smaller position that is in the matrix, if
20631 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20632 exists in the matrix, return the position of the glyph with the
20633 next larger position in OBJECT.
20634
20635 Value is non-zero if a glyph was found. */
20636
20637 static int
20638 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20639 struct window *w;
20640 int pos;
20641 Lisp_Object object;
20642 int *hpos, *vpos, *x, *y;
20643 int right_p;
20644 {
20645 int yb = window_text_bottom_y (w);
20646 struct glyph_row *r;
20647 struct glyph *best_glyph = NULL;
20648 struct glyph_row *best_row = NULL;
20649 int best_x = 0;
20650
20651 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20652 r->enabled_p && r->y < yb;
20653 ++r)
20654 {
20655 struct glyph *g = r->glyphs[TEXT_AREA];
20656 struct glyph *e = g + r->used[TEXT_AREA];
20657 int gx;
20658
20659 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20660 if (EQ (g->object, object))
20661 {
20662 if (g->charpos == pos)
20663 {
20664 best_glyph = g;
20665 best_x = gx;
20666 best_row = r;
20667 goto found;
20668 }
20669 else if (best_glyph == NULL
20670 || ((abs (g->charpos - pos)
20671 < abs (best_glyph->charpos - pos))
20672 && (right_p
20673 ? g->charpos < pos
20674 : g->charpos > pos)))
20675 {
20676 best_glyph = g;
20677 best_x = gx;
20678 best_row = r;
20679 }
20680 }
20681 }
20682
20683 found:
20684
20685 if (best_glyph)
20686 {
20687 *x = best_x;
20688 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20689
20690 if (right_p)
20691 {
20692 *x += best_glyph->pixel_width;
20693 ++*hpos;
20694 }
20695
20696 *y = best_row->y;
20697 *vpos = best_row - w->current_matrix->rows;
20698 }
20699
20700 return best_glyph != NULL;
20701 }
20702
20703
20704 /* See if position X, Y is within a hot-spot of an image. */
20705
20706 static int
20707 on_hot_spot_p (hot_spot, x, y)
20708 Lisp_Object hot_spot;
20709 int x, y;
20710 {
20711 if (!CONSP (hot_spot))
20712 return 0;
20713
20714 if (EQ (XCAR (hot_spot), Qrect))
20715 {
20716 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20717 Lisp_Object rect = XCDR (hot_spot);
20718 Lisp_Object tem;
20719 if (!CONSP (rect))
20720 return 0;
20721 if (!CONSP (XCAR (rect)))
20722 return 0;
20723 if (!CONSP (XCDR (rect)))
20724 return 0;
20725 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20726 return 0;
20727 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20728 return 0;
20729 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20730 return 0;
20731 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20732 return 0;
20733 return 1;
20734 }
20735 else if (EQ (XCAR (hot_spot), Qcircle))
20736 {
20737 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20738 Lisp_Object circ = XCDR (hot_spot);
20739 Lisp_Object lr, lx0, ly0;
20740 if (CONSP (circ)
20741 && CONSP (XCAR (circ))
20742 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20743 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20744 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20745 {
20746 double r = XFLOATINT (lr);
20747 double dx = XINT (lx0) - x;
20748 double dy = XINT (ly0) - y;
20749 return (dx * dx + dy * dy <= r * r);
20750 }
20751 }
20752 else if (EQ (XCAR (hot_spot), Qpoly))
20753 {
20754 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20755 if (VECTORP (XCDR (hot_spot)))
20756 {
20757 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20758 Lisp_Object *poly = v->contents;
20759 int n = v->size;
20760 int i;
20761 int inside = 0;
20762 Lisp_Object lx, ly;
20763 int x0, y0;
20764
20765 /* Need an even number of coordinates, and at least 3 edges. */
20766 if (n < 6 || n & 1)
20767 return 0;
20768
20769 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20770 If count is odd, we are inside polygon. Pixels on edges
20771 may or may not be included depending on actual geometry of the
20772 polygon. */
20773 if ((lx = poly[n-2], !INTEGERP (lx))
20774 || (ly = poly[n-1], !INTEGERP (lx)))
20775 return 0;
20776 x0 = XINT (lx), y0 = XINT (ly);
20777 for (i = 0; i < n; i += 2)
20778 {
20779 int x1 = x0, y1 = y0;
20780 if ((lx = poly[i], !INTEGERP (lx))
20781 || (ly = poly[i+1], !INTEGERP (ly)))
20782 return 0;
20783 x0 = XINT (lx), y0 = XINT (ly);
20784
20785 /* Does this segment cross the X line? */
20786 if (x0 >= x)
20787 {
20788 if (x1 >= x)
20789 continue;
20790 }
20791 else if (x1 < x)
20792 continue;
20793 if (y > y0 && y > y1)
20794 continue;
20795 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20796 inside = !inside;
20797 }
20798 return inside;
20799 }
20800 }
20801 return 0;
20802 }
20803
20804 Lisp_Object
20805 find_hot_spot (map, x, y)
20806 Lisp_Object map;
20807 int x, y;
20808 {
20809 while (CONSP (map))
20810 {
20811 if (CONSP (XCAR (map))
20812 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20813 return XCAR (map);
20814 map = XCDR (map);
20815 }
20816
20817 return Qnil;
20818 }
20819
20820 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20821 3, 3, 0,
20822 doc: /* Lookup in image map MAP coordinates X and Y.
20823 An image map is an alist where each element has the format (AREA ID PLIST).
20824 An AREA is specified as either a rectangle, a circle, or a polygon:
20825 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20826 pixel coordinates of the upper left and bottom right corners.
20827 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20828 and the radius of the circle; r may be a float or integer.
20829 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20830 vector describes one corner in the polygon.
20831 Returns the alist element for the first matching AREA in MAP. */)
20832 (map, x, y)
20833 Lisp_Object map;
20834 Lisp_Object x, y;
20835 {
20836 if (NILP (map))
20837 return Qnil;
20838
20839 CHECK_NUMBER (x);
20840 CHECK_NUMBER (y);
20841
20842 return find_hot_spot (map, XINT (x), XINT (y));
20843 }
20844
20845
20846 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20847 static void
20848 define_frame_cursor1 (f, cursor, pointer)
20849 struct frame *f;
20850 Cursor cursor;
20851 Lisp_Object pointer;
20852 {
20853 /* Do not change cursor shape while dragging mouse. */
20854 if (!NILP (do_mouse_tracking))
20855 return;
20856
20857 if (!NILP (pointer))
20858 {
20859 if (EQ (pointer, Qarrow))
20860 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20861 else if (EQ (pointer, Qhand))
20862 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20863 else if (EQ (pointer, Qtext))
20864 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20865 else if (EQ (pointer, intern ("hdrag")))
20866 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20867 #ifdef HAVE_X_WINDOWS
20868 else if (EQ (pointer, intern ("vdrag")))
20869 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20870 #endif
20871 else if (EQ (pointer, intern ("hourglass")))
20872 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20873 else if (EQ (pointer, Qmodeline))
20874 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20875 else
20876 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20877 }
20878
20879 if (cursor != No_Cursor)
20880 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20881 }
20882
20883 /* Take proper action when mouse has moved to the mode or header line
20884 or marginal area AREA of window W, x-position X and y-position Y.
20885 X is relative to the start of the text display area of W, so the
20886 width of bitmap areas and scroll bars must be subtracted to get a
20887 position relative to the start of the mode line. */
20888
20889 static void
20890 note_mode_line_or_margin_highlight (w, x, y, area)
20891 struct window *w;
20892 int x, y;
20893 enum window_part area;
20894 {
20895 struct frame *f = XFRAME (w->frame);
20896 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20897 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20898 Lisp_Object pointer = Qnil;
20899 int charpos, dx, dy, width, height;
20900 Lisp_Object string, object = Qnil;
20901 Lisp_Object pos, help;
20902
20903 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20904 string = mode_line_string (w, area, &x, &y, &charpos,
20905 &object, &dx, &dy, &width, &height);
20906 else
20907 {
20908 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20909 string = marginal_area_string (w, area, &x, &y, &charpos,
20910 &object, &dx, &dy, &width, &height);
20911 }
20912
20913 help = Qnil;
20914
20915 if (IMAGEP (object))
20916 {
20917 Lisp_Object image_map, hotspot;
20918 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
20919 !NILP (image_map))
20920 && (hotspot = find_hot_spot (image_map, dx, dy),
20921 CONSP (hotspot))
20922 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20923 {
20924 Lisp_Object area_id, plist;
20925
20926 area_id = XCAR (hotspot);
20927 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20928 If so, we could look for mouse-enter, mouse-leave
20929 properties in PLIST (and do something...). */
20930 hotspot = XCDR (hotspot);
20931 if (CONSP (hotspot)
20932 && (plist = XCAR (hotspot), CONSP (plist)))
20933 {
20934 pointer = Fsafe_plist_get (plist, Qpointer);
20935 if (NILP (pointer))
20936 pointer = Qhand;
20937 help = Fsafe_plist_get (plist, Qhelp_echo);
20938 if (!NILP (help))
20939 {
20940 help_echo_string = help;
20941 /* Is this correct? ++kfs */
20942 XSETWINDOW (help_echo_window, w);
20943 help_echo_object = w->buffer;
20944 help_echo_pos = charpos;
20945 }
20946 }
20947 if (NILP (pointer))
20948 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
20949 }
20950 }
20951
20952 if (STRINGP (string))
20953 {
20954 pos = make_number (charpos);
20955 /* If we're on a string with `help-echo' text property, arrange
20956 for the help to be displayed. This is done by setting the
20957 global variable help_echo_string to the help string. */
20958 if (NILP (help))
20959 {
20960 help = Fget_text_property (pos, Qhelp_echo, string);
20961 if (!NILP (help))
20962 {
20963 help_echo_string = help;
20964 XSETWINDOW (help_echo_window, w);
20965 help_echo_object = string;
20966 help_echo_pos = charpos;
20967 }
20968 }
20969
20970 if (NILP (pointer))
20971 pointer = Fget_text_property (pos, Qpointer, string);
20972
20973 /* Change the mouse pointer according to what is under X/Y. */
20974 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20975 {
20976 Lisp_Object map;
20977 map = Fget_text_property (pos, Qlocal_map, string);
20978 if (!KEYMAPP (map))
20979 map = Fget_text_property (pos, Qkeymap, string);
20980 if (!KEYMAPP (map))
20981 cursor = dpyinfo->vertical_scroll_bar_cursor;
20982 }
20983 }
20984
20985 define_frame_cursor1 (f, cursor, pointer);
20986 }
20987
20988
20989 /* EXPORT:
20990 Take proper action when the mouse has moved to position X, Y on
20991 frame F as regards highlighting characters that have mouse-face
20992 properties. Also de-highlighting chars where the mouse was before.
20993 X and Y can be negative or out of range. */
20994
20995 void
20996 note_mouse_highlight (f, x, y)
20997 struct frame *f;
20998 int x, y;
20999 {
21000 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21001 enum window_part part;
21002 Lisp_Object window;
21003 struct window *w;
21004 Cursor cursor = No_Cursor;
21005 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21006 struct buffer *b;
21007
21008 /* When a menu is active, don't highlight because this looks odd. */
21009 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21010 if (popup_activated ())
21011 return;
21012 #endif
21013
21014 if (NILP (Vmouse_highlight)
21015 || !f->glyphs_initialized_p)
21016 return;
21017
21018 dpyinfo->mouse_face_mouse_x = x;
21019 dpyinfo->mouse_face_mouse_y = y;
21020 dpyinfo->mouse_face_mouse_frame = f;
21021
21022 if (dpyinfo->mouse_face_defer)
21023 return;
21024
21025 if (gc_in_progress)
21026 {
21027 dpyinfo->mouse_face_deferred_gc = 1;
21028 return;
21029 }
21030
21031 /* Which window is that in? */
21032 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21033
21034 /* If we were displaying active text in another window, clear that.
21035 Also clear if we move out of text area in same window. */
21036 if (! EQ (window, dpyinfo->mouse_face_window)
21037 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21038 clear_mouse_face (dpyinfo);
21039
21040 /* Not on a window -> return. */
21041 if (!WINDOWP (window))
21042 return;
21043
21044 /* Reset help_echo_string. It will get recomputed below. */
21045 help_echo_string = Qnil;
21046
21047 /* Convert to window-relative pixel coordinates. */
21048 w = XWINDOW (window);
21049 frame_to_window_pixel_xy (w, &x, &y);
21050
21051 /* Handle tool-bar window differently since it doesn't display a
21052 buffer. */
21053 if (EQ (window, f->tool_bar_window))
21054 {
21055 note_tool_bar_highlight (f, x, y);
21056 return;
21057 }
21058
21059 /* Mouse is on the mode, header line or margin? */
21060 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21061 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21062 {
21063 note_mode_line_or_margin_highlight (w, x, y, part);
21064 return;
21065 }
21066
21067 if (part == ON_VERTICAL_BORDER)
21068 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21069 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21070 || part == ON_SCROLL_BAR)
21071 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21072 else
21073 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21074
21075 /* Are we in a window whose display is up to date?
21076 And verify the buffer's text has not changed. */
21077 b = XBUFFER (w->buffer);
21078 if (part == ON_TEXT
21079 && EQ (w->window_end_valid, w->buffer)
21080 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21081 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21082 {
21083 int hpos, vpos, pos, i, dx, dy, area;
21084 struct glyph *glyph;
21085 Lisp_Object object;
21086 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21087 Lisp_Object *overlay_vec = NULL;
21088 int noverlays;
21089 struct buffer *obuf;
21090 int obegv, ozv, same_region;
21091
21092 /* Find the glyph under X/Y. */
21093 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21094
21095 /* Look for :pointer property on image. */
21096 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21097 {
21098 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21099 if (img != NULL && IMAGEP (img->spec))
21100 {
21101 Lisp_Object image_map, hotspot;
21102 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21103 !NILP (image_map))
21104 && (hotspot = find_hot_spot (image_map,
21105 glyph->slice.x + dx,
21106 glyph->slice.y + dy),
21107 CONSP (hotspot))
21108 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21109 {
21110 Lisp_Object area_id, plist;
21111
21112 area_id = XCAR (hotspot);
21113 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21114 If so, we could look for mouse-enter, mouse-leave
21115 properties in PLIST (and do something...). */
21116 hotspot = XCDR (hotspot);
21117 if (CONSP (hotspot)
21118 && (plist = XCAR (hotspot), CONSP (plist)))
21119 {
21120 pointer = Fsafe_plist_get (plist, Qpointer);
21121 if (NILP (pointer))
21122 pointer = Qhand;
21123 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21124 if (!NILP (help_echo_string))
21125 {
21126 help_echo_window = window;
21127 help_echo_object = glyph->object;
21128 help_echo_pos = glyph->charpos;
21129 }
21130 }
21131 }
21132 if (NILP (pointer))
21133 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21134 }
21135 }
21136
21137 /* Clear mouse face if X/Y not over text. */
21138 if (glyph == NULL
21139 || area != TEXT_AREA
21140 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21141 {
21142 if (clear_mouse_face (dpyinfo))
21143 cursor = No_Cursor;
21144 if (NILP (pointer))
21145 {
21146 if (area != TEXT_AREA)
21147 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21148 else
21149 pointer = Vvoid_text_area_pointer;
21150 }
21151 goto set_cursor;
21152 }
21153
21154 pos = glyph->charpos;
21155 object = glyph->object;
21156 if (!STRINGP (object) && !BUFFERP (object))
21157 goto set_cursor;
21158
21159 /* If we get an out-of-range value, return now; avoid an error. */
21160 if (BUFFERP (object) && pos > BUF_Z (b))
21161 goto set_cursor;
21162
21163 /* Make the window's buffer temporarily current for
21164 overlays_at and compute_char_face. */
21165 obuf = current_buffer;
21166 current_buffer = b;
21167 obegv = BEGV;
21168 ozv = ZV;
21169 BEGV = BEG;
21170 ZV = Z;
21171
21172 /* Is this char mouse-active or does it have help-echo? */
21173 position = make_number (pos);
21174
21175 if (BUFFERP (object))
21176 {
21177 /* Put all the overlays we want in a vector in overlay_vec. */
21178 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21179 /* Sort overlays into increasing priority order. */
21180 noverlays = sort_overlays (overlay_vec, noverlays, w);
21181 }
21182 else
21183 noverlays = 0;
21184
21185 same_region = (EQ (window, dpyinfo->mouse_face_window)
21186 && vpos >= dpyinfo->mouse_face_beg_row
21187 && vpos <= dpyinfo->mouse_face_end_row
21188 && (vpos > dpyinfo->mouse_face_beg_row
21189 || hpos >= dpyinfo->mouse_face_beg_col)
21190 && (vpos < dpyinfo->mouse_face_end_row
21191 || hpos < dpyinfo->mouse_face_end_col
21192 || dpyinfo->mouse_face_past_end));
21193
21194 if (same_region)
21195 cursor = No_Cursor;
21196
21197 /* Check mouse-face highlighting. */
21198 if (! same_region
21199 /* If there exists an overlay with mouse-face overlapping
21200 the one we are currently highlighting, we have to
21201 check if we enter the overlapping overlay, and then
21202 highlight only that. */
21203 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21204 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21205 {
21206 /* Find the highest priority overlay that has a mouse-face
21207 property. */
21208 overlay = Qnil;
21209 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21210 {
21211 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21212 if (!NILP (mouse_face))
21213 overlay = overlay_vec[i];
21214 }
21215
21216 /* If we're actually highlighting the same overlay as
21217 before, there's no need to do that again. */
21218 if (!NILP (overlay)
21219 && EQ (overlay, dpyinfo->mouse_face_overlay))
21220 goto check_help_echo;
21221
21222 dpyinfo->mouse_face_overlay = overlay;
21223
21224 /* Clear the display of the old active region, if any. */
21225 if (clear_mouse_face (dpyinfo))
21226 cursor = No_Cursor;
21227
21228 /* If no overlay applies, get a text property. */
21229 if (NILP (overlay))
21230 mouse_face = Fget_text_property (position, Qmouse_face, object);
21231
21232 /* Handle the overlay case. */
21233 if (!NILP (overlay))
21234 {
21235 /* Find the range of text around this char that
21236 should be active. */
21237 Lisp_Object before, after;
21238 int ignore;
21239
21240 before = Foverlay_start (overlay);
21241 after = Foverlay_end (overlay);
21242 /* Record this as the current active region. */
21243 fast_find_position (w, XFASTINT (before),
21244 &dpyinfo->mouse_face_beg_col,
21245 &dpyinfo->mouse_face_beg_row,
21246 &dpyinfo->mouse_face_beg_x,
21247 &dpyinfo->mouse_face_beg_y, Qnil);
21248
21249 dpyinfo->mouse_face_past_end
21250 = !fast_find_position (w, XFASTINT (after),
21251 &dpyinfo->mouse_face_end_col,
21252 &dpyinfo->mouse_face_end_row,
21253 &dpyinfo->mouse_face_end_x,
21254 &dpyinfo->mouse_face_end_y, Qnil);
21255 dpyinfo->mouse_face_window = window;
21256
21257 dpyinfo->mouse_face_face_id
21258 = face_at_buffer_position (w, pos, 0, 0,
21259 &ignore, pos + 1,
21260 !dpyinfo->mouse_face_hidden);
21261
21262 /* Display it as active. */
21263 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21264 cursor = No_Cursor;
21265 }
21266 /* Handle the text property case. */
21267 else if (!NILP (mouse_face) && BUFFERP (object))
21268 {
21269 /* Find the range of text around this char that
21270 should be active. */
21271 Lisp_Object before, after, beginning, end;
21272 int ignore;
21273
21274 beginning = Fmarker_position (w->start);
21275 end = make_number (BUF_Z (XBUFFER (object))
21276 - XFASTINT (w->window_end_pos));
21277 before
21278 = Fprevious_single_property_change (make_number (pos + 1),
21279 Qmouse_face,
21280 object, beginning);
21281 after
21282 = Fnext_single_property_change (position, Qmouse_face,
21283 object, end);
21284
21285 /* Record this as the current active region. */
21286 fast_find_position (w, XFASTINT (before),
21287 &dpyinfo->mouse_face_beg_col,
21288 &dpyinfo->mouse_face_beg_row,
21289 &dpyinfo->mouse_face_beg_x,
21290 &dpyinfo->mouse_face_beg_y, Qnil);
21291 dpyinfo->mouse_face_past_end
21292 = !fast_find_position (w, XFASTINT (after),
21293 &dpyinfo->mouse_face_end_col,
21294 &dpyinfo->mouse_face_end_row,
21295 &dpyinfo->mouse_face_end_x,
21296 &dpyinfo->mouse_face_end_y, Qnil);
21297 dpyinfo->mouse_face_window = window;
21298
21299 if (BUFFERP (object))
21300 dpyinfo->mouse_face_face_id
21301 = face_at_buffer_position (w, pos, 0, 0,
21302 &ignore, pos + 1,
21303 !dpyinfo->mouse_face_hidden);
21304
21305 /* Display it as active. */
21306 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21307 cursor = No_Cursor;
21308 }
21309 else if (!NILP (mouse_face) && STRINGP (object))
21310 {
21311 Lisp_Object b, e;
21312 int ignore;
21313
21314 b = Fprevious_single_property_change (make_number (pos + 1),
21315 Qmouse_face,
21316 object, Qnil);
21317 e = Fnext_single_property_change (position, Qmouse_face,
21318 object, Qnil);
21319 if (NILP (b))
21320 b = make_number (0);
21321 if (NILP (e))
21322 e = make_number (SCHARS (object) - 1);
21323 fast_find_string_pos (w, XINT (b), object,
21324 &dpyinfo->mouse_face_beg_col,
21325 &dpyinfo->mouse_face_beg_row,
21326 &dpyinfo->mouse_face_beg_x,
21327 &dpyinfo->mouse_face_beg_y, 0);
21328 fast_find_string_pos (w, XINT (e), object,
21329 &dpyinfo->mouse_face_end_col,
21330 &dpyinfo->mouse_face_end_row,
21331 &dpyinfo->mouse_face_end_x,
21332 &dpyinfo->mouse_face_end_y, 1);
21333 dpyinfo->mouse_face_past_end = 0;
21334 dpyinfo->mouse_face_window = window;
21335 dpyinfo->mouse_face_face_id
21336 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21337 glyph->face_id, 1);
21338 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21339 cursor = No_Cursor;
21340 }
21341 else if (STRINGP (object) && NILP (mouse_face))
21342 {
21343 /* A string which doesn't have mouse-face, but
21344 the text ``under'' it might have. */
21345 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21346 int start = MATRIX_ROW_START_CHARPOS (r);
21347
21348 pos = string_buffer_position (w, object, start);
21349 if (pos > 0)
21350 mouse_face = get_char_property_and_overlay (make_number (pos),
21351 Qmouse_face,
21352 w->buffer,
21353 &overlay);
21354 if (!NILP (mouse_face) && !NILP (overlay))
21355 {
21356 Lisp_Object before = Foverlay_start (overlay);
21357 Lisp_Object after = Foverlay_end (overlay);
21358 int ignore;
21359
21360 /* Note that we might not be able to find position
21361 BEFORE in the glyph matrix if the overlay is
21362 entirely covered by a `display' property. In
21363 this case, we overshoot. So let's stop in
21364 the glyph matrix before glyphs for OBJECT. */
21365 fast_find_position (w, XFASTINT (before),
21366 &dpyinfo->mouse_face_beg_col,
21367 &dpyinfo->mouse_face_beg_row,
21368 &dpyinfo->mouse_face_beg_x,
21369 &dpyinfo->mouse_face_beg_y,
21370 object);
21371
21372 dpyinfo->mouse_face_past_end
21373 = !fast_find_position (w, XFASTINT (after),
21374 &dpyinfo->mouse_face_end_col,
21375 &dpyinfo->mouse_face_end_row,
21376 &dpyinfo->mouse_face_end_x,
21377 &dpyinfo->mouse_face_end_y,
21378 Qnil);
21379 dpyinfo->mouse_face_window = window;
21380 dpyinfo->mouse_face_face_id
21381 = face_at_buffer_position (w, pos, 0, 0,
21382 &ignore, pos + 1,
21383 !dpyinfo->mouse_face_hidden);
21384
21385 /* Display it as active. */
21386 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21387 cursor = No_Cursor;
21388 }
21389 }
21390 }
21391
21392 check_help_echo:
21393
21394 /* Look for a `help-echo' property. */
21395 if (NILP (help_echo_string)) {
21396 Lisp_Object help, overlay;
21397
21398 /* Check overlays first. */
21399 help = overlay = Qnil;
21400 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21401 {
21402 overlay = overlay_vec[i];
21403 help = Foverlay_get (overlay, Qhelp_echo);
21404 }
21405
21406 if (!NILP (help))
21407 {
21408 help_echo_string = help;
21409 help_echo_window = window;
21410 help_echo_object = overlay;
21411 help_echo_pos = pos;
21412 }
21413 else
21414 {
21415 Lisp_Object object = glyph->object;
21416 int charpos = glyph->charpos;
21417
21418 /* Try text properties. */
21419 if (STRINGP (object)
21420 && charpos >= 0
21421 && charpos < SCHARS (object))
21422 {
21423 help = Fget_text_property (make_number (charpos),
21424 Qhelp_echo, object);
21425 if (NILP (help))
21426 {
21427 /* If the string itself doesn't specify a help-echo,
21428 see if the buffer text ``under'' it does. */
21429 struct glyph_row *r
21430 = MATRIX_ROW (w->current_matrix, vpos);
21431 int start = MATRIX_ROW_START_CHARPOS (r);
21432 int pos = string_buffer_position (w, object, start);
21433 if (pos > 0)
21434 {
21435 help = Fget_char_property (make_number (pos),
21436 Qhelp_echo, w->buffer);
21437 if (!NILP (help))
21438 {
21439 charpos = pos;
21440 object = w->buffer;
21441 }
21442 }
21443 }
21444 }
21445 else if (BUFFERP (object)
21446 && charpos >= BEGV
21447 && charpos < ZV)
21448 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21449 object);
21450
21451 if (!NILP (help))
21452 {
21453 help_echo_string = help;
21454 help_echo_window = window;
21455 help_echo_object = object;
21456 help_echo_pos = charpos;
21457 }
21458 }
21459 }
21460
21461 /* Look for a `pointer' property. */
21462 if (NILP (pointer))
21463 {
21464 /* Check overlays first. */
21465 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21466 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21467
21468 if (NILP (pointer))
21469 {
21470 Lisp_Object object = glyph->object;
21471 int charpos = glyph->charpos;
21472
21473 /* Try text properties. */
21474 if (STRINGP (object)
21475 && charpos >= 0
21476 && charpos < SCHARS (object))
21477 {
21478 pointer = Fget_text_property (make_number (charpos),
21479 Qpointer, object);
21480 if (NILP (pointer))
21481 {
21482 /* If the string itself doesn't specify a pointer,
21483 see if the buffer text ``under'' it does. */
21484 struct glyph_row *r
21485 = MATRIX_ROW (w->current_matrix, vpos);
21486 int start = MATRIX_ROW_START_CHARPOS (r);
21487 int pos = string_buffer_position (w, object, start);
21488 if (pos > 0)
21489 pointer = Fget_char_property (make_number (pos),
21490 Qpointer, w->buffer);
21491 }
21492 }
21493 else if (BUFFERP (object)
21494 && charpos >= BEGV
21495 && charpos < ZV)
21496 pointer = Fget_text_property (make_number (charpos),
21497 Qpointer, object);
21498 }
21499 }
21500
21501 BEGV = obegv;
21502 ZV = ozv;
21503 current_buffer = obuf;
21504 }
21505
21506 set_cursor:
21507
21508 define_frame_cursor1 (f, cursor, pointer);
21509 }
21510
21511
21512 /* EXPORT for RIF:
21513 Clear any mouse-face on window W. This function is part of the
21514 redisplay interface, and is called from try_window_id and similar
21515 functions to ensure the mouse-highlight is off. */
21516
21517 void
21518 x_clear_window_mouse_face (w)
21519 struct window *w;
21520 {
21521 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21522 Lisp_Object window;
21523
21524 BLOCK_INPUT;
21525 XSETWINDOW (window, w);
21526 if (EQ (window, dpyinfo->mouse_face_window))
21527 clear_mouse_face (dpyinfo);
21528 UNBLOCK_INPUT;
21529 }
21530
21531
21532 /* EXPORT:
21533 Just discard the mouse face information for frame F, if any.
21534 This is used when the size of F is changed. */
21535
21536 void
21537 cancel_mouse_face (f)
21538 struct frame *f;
21539 {
21540 Lisp_Object window;
21541 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21542
21543 window = dpyinfo->mouse_face_window;
21544 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21545 {
21546 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21547 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21548 dpyinfo->mouse_face_window = Qnil;
21549 }
21550 }
21551
21552
21553 #endif /* HAVE_WINDOW_SYSTEM */
21554
21555 \f
21556 /***********************************************************************
21557 Exposure Events
21558 ***********************************************************************/
21559
21560 #ifdef HAVE_WINDOW_SYSTEM
21561
21562 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21563 which intersects rectangle R. R is in window-relative coordinates. */
21564
21565 static void
21566 expose_area (w, row, r, area)
21567 struct window *w;
21568 struct glyph_row *row;
21569 XRectangle *r;
21570 enum glyph_row_area area;
21571 {
21572 struct glyph *first = row->glyphs[area];
21573 struct glyph *end = row->glyphs[area] + row->used[area];
21574 struct glyph *last;
21575 int first_x, start_x, x;
21576
21577 if (area == TEXT_AREA && row->fill_line_p)
21578 /* If row extends face to end of line write the whole line. */
21579 draw_glyphs (w, 0, row, area,
21580 0, row->used[area],
21581 DRAW_NORMAL_TEXT, 0);
21582 else
21583 {
21584 /* Set START_X to the window-relative start position for drawing glyphs of
21585 AREA. The first glyph of the text area can be partially visible.
21586 The first glyphs of other areas cannot. */
21587 start_x = window_box_left_offset (w, area);
21588 x = start_x;
21589 if (area == TEXT_AREA)
21590 x += row->x;
21591
21592 /* Find the first glyph that must be redrawn. */
21593 while (first < end
21594 && x + first->pixel_width < r->x)
21595 {
21596 x += first->pixel_width;
21597 ++first;
21598 }
21599
21600 /* Find the last one. */
21601 last = first;
21602 first_x = x;
21603 while (last < end
21604 && x < r->x + r->width)
21605 {
21606 x += last->pixel_width;
21607 ++last;
21608 }
21609
21610 /* Repaint. */
21611 if (last > first)
21612 draw_glyphs (w, first_x - start_x, row, area,
21613 first - row->glyphs[area], last - row->glyphs[area],
21614 DRAW_NORMAL_TEXT, 0);
21615 }
21616 }
21617
21618
21619 /* Redraw the parts of the glyph row ROW on window W intersecting
21620 rectangle R. R is in window-relative coordinates. Value is
21621 non-zero if mouse-face was overwritten. */
21622
21623 static int
21624 expose_line (w, row, r)
21625 struct window *w;
21626 struct glyph_row *row;
21627 XRectangle *r;
21628 {
21629 xassert (row->enabled_p);
21630
21631 if (row->mode_line_p || w->pseudo_window_p)
21632 draw_glyphs (w, 0, row, TEXT_AREA,
21633 0, row->used[TEXT_AREA],
21634 DRAW_NORMAL_TEXT, 0);
21635 else
21636 {
21637 if (row->used[LEFT_MARGIN_AREA])
21638 expose_area (w, row, r, LEFT_MARGIN_AREA);
21639 if (row->used[TEXT_AREA])
21640 expose_area (w, row, r, TEXT_AREA);
21641 if (row->used[RIGHT_MARGIN_AREA])
21642 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21643 draw_row_fringe_bitmaps (w, row);
21644 }
21645
21646 return row->mouse_face_p;
21647 }
21648
21649
21650 /* Redraw those parts of glyphs rows during expose event handling that
21651 overlap other rows. Redrawing of an exposed line writes over parts
21652 of lines overlapping that exposed line; this function fixes that.
21653
21654 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21655 row in W's current matrix that is exposed and overlaps other rows.
21656 LAST_OVERLAPPING_ROW is the last such row. */
21657
21658 static void
21659 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21660 struct window *w;
21661 struct glyph_row *first_overlapping_row;
21662 struct glyph_row *last_overlapping_row;
21663 {
21664 struct glyph_row *row;
21665
21666 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21667 if (row->overlapping_p)
21668 {
21669 xassert (row->enabled_p && !row->mode_line_p);
21670
21671 if (row->used[LEFT_MARGIN_AREA])
21672 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21673
21674 if (row->used[TEXT_AREA])
21675 x_fix_overlapping_area (w, row, TEXT_AREA);
21676
21677 if (row->used[RIGHT_MARGIN_AREA])
21678 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21679 }
21680 }
21681
21682
21683 /* Return non-zero if W's cursor intersects rectangle R. */
21684
21685 static int
21686 phys_cursor_in_rect_p (w, r)
21687 struct window *w;
21688 XRectangle *r;
21689 {
21690 XRectangle cr, result;
21691 struct glyph *cursor_glyph;
21692
21693 cursor_glyph = get_phys_cursor_glyph (w);
21694 if (cursor_glyph)
21695 {
21696 /* r is relative to W's box, but w->phys_cursor.x is relative
21697 to left edge of W's TEXT area. Adjust it. */
21698 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21699 cr.y = w->phys_cursor.y;
21700 cr.width = cursor_glyph->pixel_width;
21701 cr.height = w->phys_cursor_height;
21702 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21703 I assume the effect is the same -- and this is portable. */
21704 return x_intersect_rectangles (&cr, r, &result);
21705 }
21706 /* If we don't understand the format, pretend we're not in the hot-spot. */
21707 return 0;
21708 }
21709
21710
21711 /* EXPORT:
21712 Draw a vertical window border to the right of window W if W doesn't
21713 have vertical scroll bars. */
21714
21715 void
21716 x_draw_vertical_border (w)
21717 struct window *w;
21718 {
21719 struct frame *f = XFRAME (WINDOW_FRAME (w));
21720
21721 /* We could do better, if we knew what type of scroll-bar the adjacent
21722 windows (on either side) have... But we don't :-(
21723 However, I think this works ok. ++KFS 2003-04-25 */
21724
21725 /* Redraw borders between horizontally adjacent windows. Don't
21726 do it for frames with vertical scroll bars because either the
21727 right scroll bar of a window, or the left scroll bar of its
21728 neighbor will suffice as a border. */
21729 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21730 return;
21731
21732 if (!WINDOW_RIGHTMOST_P (w)
21733 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21734 {
21735 int x0, x1, y0, y1;
21736
21737 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21738 y1 -= 1;
21739
21740 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21741 }
21742 else if (!WINDOW_LEFTMOST_P (w)
21743 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21744 {
21745 int x0, x1, y0, y1;
21746
21747 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21748 y1 -= 1;
21749
21750 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21751 }
21752 }
21753
21754
21755 /* Redraw the part of window W intersection rectangle FR. Pixel
21756 coordinates in FR are frame-relative. Call this function with
21757 input blocked. Value is non-zero if the exposure overwrites
21758 mouse-face. */
21759
21760 static int
21761 expose_window (w, fr)
21762 struct window *w;
21763 XRectangle *fr;
21764 {
21765 struct frame *f = XFRAME (w->frame);
21766 XRectangle wr, r;
21767 int mouse_face_overwritten_p = 0;
21768
21769 /* If window is not yet fully initialized, do nothing. This can
21770 happen when toolkit scroll bars are used and a window is split.
21771 Reconfiguring the scroll bar will generate an expose for a newly
21772 created window. */
21773 if (w->current_matrix == NULL)
21774 return 0;
21775
21776 /* When we're currently updating the window, display and current
21777 matrix usually don't agree. Arrange for a thorough display
21778 later. */
21779 if (w == updated_window)
21780 {
21781 SET_FRAME_GARBAGED (f);
21782 return 0;
21783 }
21784
21785 /* Frame-relative pixel rectangle of W. */
21786 wr.x = WINDOW_LEFT_EDGE_X (w);
21787 wr.y = WINDOW_TOP_EDGE_Y (w);
21788 wr.width = WINDOW_TOTAL_WIDTH (w);
21789 wr.height = WINDOW_TOTAL_HEIGHT (w);
21790
21791 if (x_intersect_rectangles (fr, &wr, &r))
21792 {
21793 int yb = window_text_bottom_y (w);
21794 struct glyph_row *row;
21795 int cursor_cleared_p;
21796 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21797
21798 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21799 r.x, r.y, r.width, r.height));
21800
21801 /* Convert to window coordinates. */
21802 r.x -= WINDOW_LEFT_EDGE_X (w);
21803 r.y -= WINDOW_TOP_EDGE_Y (w);
21804
21805 /* Turn off the cursor. */
21806 if (!w->pseudo_window_p
21807 && phys_cursor_in_rect_p (w, &r))
21808 {
21809 x_clear_cursor (w);
21810 cursor_cleared_p = 1;
21811 }
21812 else
21813 cursor_cleared_p = 0;
21814
21815 /* Update lines intersecting rectangle R. */
21816 first_overlapping_row = last_overlapping_row = NULL;
21817 for (row = w->current_matrix->rows;
21818 row->enabled_p;
21819 ++row)
21820 {
21821 int y0 = row->y;
21822 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21823
21824 if ((y0 >= r.y && y0 < r.y + r.height)
21825 || (y1 > r.y && y1 < r.y + r.height)
21826 || (r.y >= y0 && r.y < y1)
21827 || (r.y + r.height > y0 && r.y + r.height < y1))
21828 {
21829 if (row->overlapping_p)
21830 {
21831 if (first_overlapping_row == NULL)
21832 first_overlapping_row = row;
21833 last_overlapping_row = row;
21834 }
21835
21836 if (expose_line (w, row, &r))
21837 mouse_face_overwritten_p = 1;
21838 }
21839
21840 if (y1 >= yb)
21841 break;
21842 }
21843
21844 /* Display the mode line if there is one. */
21845 if (WINDOW_WANTS_MODELINE_P (w)
21846 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21847 row->enabled_p)
21848 && row->y < r.y + r.height)
21849 {
21850 if (expose_line (w, row, &r))
21851 mouse_face_overwritten_p = 1;
21852 }
21853
21854 if (!w->pseudo_window_p)
21855 {
21856 /* Fix the display of overlapping rows. */
21857 if (first_overlapping_row)
21858 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21859
21860 /* Draw border between windows. */
21861 x_draw_vertical_border (w);
21862
21863 /* Turn the cursor on again. */
21864 if (cursor_cleared_p)
21865 update_window_cursor (w, 1);
21866 }
21867 }
21868
21869 #ifdef HAVE_CARBON
21870 /* Display scroll bar for this window. */
21871 if (!NILP (w->vertical_scroll_bar))
21872 {
21873 /* ++KFS:
21874 If this doesn't work here (maybe some header files are missing),
21875 make a function in macterm.c and call it to do the job! */
21876 ControlHandle ch
21877 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21878
21879 Draw1Control (ch);
21880 }
21881 #endif
21882
21883 return mouse_face_overwritten_p;
21884 }
21885
21886
21887
21888 /* Redraw (parts) of all windows in the window tree rooted at W that
21889 intersect R. R contains frame pixel coordinates. Value is
21890 non-zero if the exposure overwrites mouse-face. */
21891
21892 static int
21893 expose_window_tree (w, r)
21894 struct window *w;
21895 XRectangle *r;
21896 {
21897 struct frame *f = XFRAME (w->frame);
21898 int mouse_face_overwritten_p = 0;
21899
21900 while (w && !FRAME_GARBAGED_P (f))
21901 {
21902 if (!NILP (w->hchild))
21903 mouse_face_overwritten_p
21904 |= expose_window_tree (XWINDOW (w->hchild), r);
21905 else if (!NILP (w->vchild))
21906 mouse_face_overwritten_p
21907 |= expose_window_tree (XWINDOW (w->vchild), r);
21908 else
21909 mouse_face_overwritten_p |= expose_window (w, r);
21910
21911 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21912 }
21913
21914 return mouse_face_overwritten_p;
21915 }
21916
21917
21918 /* EXPORT:
21919 Redisplay an exposed area of frame F. X and Y are the upper-left
21920 corner of the exposed rectangle. W and H are width and height of
21921 the exposed area. All are pixel values. W or H zero means redraw
21922 the entire frame. */
21923
21924 void
21925 expose_frame (f, x, y, w, h)
21926 struct frame *f;
21927 int x, y, w, h;
21928 {
21929 XRectangle r;
21930 int mouse_face_overwritten_p = 0;
21931
21932 TRACE ((stderr, "expose_frame "));
21933
21934 /* No need to redraw if frame will be redrawn soon. */
21935 if (FRAME_GARBAGED_P (f))
21936 {
21937 TRACE ((stderr, " garbaged\n"));
21938 return;
21939 }
21940
21941 #ifdef HAVE_CARBON
21942 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21943 or deactivated here, for unknown reasons, activated scroll bars
21944 are shown in deactivated frames in some instances. */
21945 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21946 activate_scroll_bars (f);
21947 else
21948 deactivate_scroll_bars (f);
21949 #endif
21950
21951 /* If basic faces haven't been realized yet, there is no point in
21952 trying to redraw anything. This can happen when we get an expose
21953 event while Emacs is starting, e.g. by moving another window. */
21954 if (FRAME_FACE_CACHE (f) == NULL
21955 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21956 {
21957 TRACE ((stderr, " no faces\n"));
21958 return;
21959 }
21960
21961 if (w == 0 || h == 0)
21962 {
21963 r.x = r.y = 0;
21964 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21965 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21966 }
21967 else
21968 {
21969 r.x = x;
21970 r.y = y;
21971 r.width = w;
21972 r.height = h;
21973 }
21974
21975 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21976 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21977
21978 if (WINDOWP (f->tool_bar_window))
21979 mouse_face_overwritten_p
21980 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21981
21982 #ifdef HAVE_X_WINDOWS
21983 #ifndef MSDOS
21984 #ifndef USE_X_TOOLKIT
21985 if (WINDOWP (f->menu_bar_window))
21986 mouse_face_overwritten_p
21987 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21988 #endif /* not USE_X_TOOLKIT */
21989 #endif
21990 #endif
21991
21992 /* Some window managers support a focus-follows-mouse style with
21993 delayed raising of frames. Imagine a partially obscured frame,
21994 and moving the mouse into partially obscured mouse-face on that
21995 frame. The visible part of the mouse-face will be highlighted,
21996 then the WM raises the obscured frame. With at least one WM, KDE
21997 2.1, Emacs is not getting any event for the raising of the frame
21998 (even tried with SubstructureRedirectMask), only Expose events.
21999 These expose events will draw text normally, i.e. not
22000 highlighted. Which means we must redo the highlight here.
22001 Subsume it under ``we love X''. --gerd 2001-08-15 */
22002 /* Included in Windows version because Windows most likely does not
22003 do the right thing if any third party tool offers
22004 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22005 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22006 {
22007 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22008 if (f == dpyinfo->mouse_face_mouse_frame)
22009 {
22010 int x = dpyinfo->mouse_face_mouse_x;
22011 int y = dpyinfo->mouse_face_mouse_y;
22012 clear_mouse_face (dpyinfo);
22013 note_mouse_highlight (f, x, y);
22014 }
22015 }
22016 }
22017
22018
22019 /* EXPORT:
22020 Determine the intersection of two rectangles R1 and R2. Return
22021 the intersection in *RESULT. Value is non-zero if RESULT is not
22022 empty. */
22023
22024 int
22025 x_intersect_rectangles (r1, r2, result)
22026 XRectangle *r1, *r2, *result;
22027 {
22028 XRectangle *left, *right;
22029 XRectangle *upper, *lower;
22030 int intersection_p = 0;
22031
22032 /* Rearrange so that R1 is the left-most rectangle. */
22033 if (r1->x < r2->x)
22034 left = r1, right = r2;
22035 else
22036 left = r2, right = r1;
22037
22038 /* X0 of the intersection is right.x0, if this is inside R1,
22039 otherwise there is no intersection. */
22040 if (right->x <= left->x + left->width)
22041 {
22042 result->x = right->x;
22043
22044 /* The right end of the intersection is the minimum of the
22045 the right ends of left and right. */
22046 result->width = (min (left->x + left->width, right->x + right->width)
22047 - result->x);
22048
22049 /* Same game for Y. */
22050 if (r1->y < r2->y)
22051 upper = r1, lower = r2;
22052 else
22053 upper = r2, lower = r1;
22054
22055 /* The upper end of the intersection is lower.y0, if this is inside
22056 of upper. Otherwise, there is no intersection. */
22057 if (lower->y <= upper->y + upper->height)
22058 {
22059 result->y = lower->y;
22060
22061 /* The lower end of the intersection is the minimum of the lower
22062 ends of upper and lower. */
22063 result->height = (min (lower->y + lower->height,
22064 upper->y + upper->height)
22065 - result->y);
22066 intersection_p = 1;
22067 }
22068 }
22069
22070 return intersection_p;
22071 }
22072
22073 #endif /* HAVE_WINDOW_SYSTEM */
22074
22075 \f
22076 /***********************************************************************
22077 Initialization
22078 ***********************************************************************/
22079
22080 void
22081 syms_of_xdisp ()
22082 {
22083 Vwith_echo_area_save_vector = Qnil;
22084 staticpro (&Vwith_echo_area_save_vector);
22085
22086 Vmessage_stack = Qnil;
22087 staticpro (&Vmessage_stack);
22088
22089 Qinhibit_redisplay = intern ("inhibit-redisplay");
22090 staticpro (&Qinhibit_redisplay);
22091
22092 message_dolog_marker1 = Fmake_marker ();
22093 staticpro (&message_dolog_marker1);
22094 message_dolog_marker2 = Fmake_marker ();
22095 staticpro (&message_dolog_marker2);
22096 message_dolog_marker3 = Fmake_marker ();
22097 staticpro (&message_dolog_marker3);
22098
22099 #if GLYPH_DEBUG
22100 defsubr (&Sdump_frame_glyph_matrix);
22101 defsubr (&Sdump_glyph_matrix);
22102 defsubr (&Sdump_glyph_row);
22103 defsubr (&Sdump_tool_bar_row);
22104 defsubr (&Strace_redisplay);
22105 defsubr (&Strace_to_stderr);
22106 #endif
22107 #ifdef HAVE_WINDOW_SYSTEM
22108 defsubr (&Stool_bar_lines_needed);
22109 defsubr (&Slookup_image_map);
22110 #endif
22111 defsubr (&Sformat_mode_line);
22112
22113 staticpro (&Qmenu_bar_update_hook);
22114 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22115
22116 staticpro (&Qoverriding_terminal_local_map);
22117 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22118
22119 staticpro (&Qoverriding_local_map);
22120 Qoverriding_local_map = intern ("overriding-local-map");
22121
22122 staticpro (&Qwindow_scroll_functions);
22123 Qwindow_scroll_functions = intern ("window-scroll-functions");
22124
22125 staticpro (&Qredisplay_end_trigger_functions);
22126 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22127
22128 staticpro (&Qinhibit_point_motion_hooks);
22129 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22130
22131 QCdata = intern (":data");
22132 staticpro (&QCdata);
22133 Qdisplay = intern ("display");
22134 staticpro (&Qdisplay);
22135 Qspace_width = intern ("space-width");
22136 staticpro (&Qspace_width);
22137 Qraise = intern ("raise");
22138 staticpro (&Qraise);
22139 Qslice = intern ("slice");
22140 staticpro (&Qslice);
22141 Qspace = intern ("space");
22142 staticpro (&Qspace);
22143 Qmargin = intern ("margin");
22144 staticpro (&Qmargin);
22145 Qpointer = intern ("pointer");
22146 staticpro (&Qpointer);
22147 Qleft_margin = intern ("left-margin");
22148 staticpro (&Qleft_margin);
22149 Qright_margin = intern ("right-margin");
22150 staticpro (&Qright_margin);
22151 Qcenter = intern ("center");
22152 staticpro (&Qcenter);
22153 Qline_height = intern ("line-height");
22154 staticpro (&Qline_height);
22155 Qtotal = intern ("total");
22156 staticpro (&Qtotal);
22157 QCalign_to = intern (":align-to");
22158 staticpro (&QCalign_to);
22159 QCrelative_width = intern (":relative-width");
22160 staticpro (&QCrelative_width);
22161 QCrelative_height = intern (":relative-height");
22162 staticpro (&QCrelative_height);
22163 QCeval = intern (":eval");
22164 staticpro (&QCeval);
22165 QCpropertize = intern (":propertize");
22166 staticpro (&QCpropertize);
22167 QCfile = intern (":file");
22168 staticpro (&QCfile);
22169 Qfontified = intern ("fontified");
22170 staticpro (&Qfontified);
22171 Qfontification_functions = intern ("fontification-functions");
22172 staticpro (&Qfontification_functions);
22173 Qtrailing_whitespace = intern ("trailing-whitespace");
22174 staticpro (&Qtrailing_whitespace);
22175 Qimage = intern ("image");
22176 staticpro (&Qimage);
22177 QCmap = intern (":map");
22178 staticpro (&QCmap);
22179 QCpointer = intern (":pointer");
22180 staticpro (&QCpointer);
22181 Qrect = intern ("rect");
22182 staticpro (&Qrect);
22183 Qcircle = intern ("circle");
22184 staticpro (&Qcircle);
22185 Qpoly = intern ("poly");
22186 staticpro (&Qpoly);
22187 Qmessage_truncate_lines = intern ("message-truncate-lines");
22188 staticpro (&Qmessage_truncate_lines);
22189 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22190 staticpro (&Qcursor_in_non_selected_windows);
22191 Qgrow_only = intern ("grow-only");
22192 staticpro (&Qgrow_only);
22193 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22194 staticpro (&Qinhibit_menubar_update);
22195 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22196 staticpro (&Qinhibit_eval_during_redisplay);
22197 Qposition = intern ("position");
22198 staticpro (&Qposition);
22199 Qbuffer_position = intern ("buffer-position");
22200 staticpro (&Qbuffer_position);
22201 Qobject = intern ("object");
22202 staticpro (&Qobject);
22203 Qbar = intern ("bar");
22204 staticpro (&Qbar);
22205 Qhbar = intern ("hbar");
22206 staticpro (&Qhbar);
22207 Qbox = intern ("box");
22208 staticpro (&Qbox);
22209 Qhollow = intern ("hollow");
22210 staticpro (&Qhollow);
22211 Qhand = intern ("hand");
22212 staticpro (&Qhand);
22213 Qarrow = intern ("arrow");
22214 staticpro (&Qarrow);
22215 Qtext = intern ("text");
22216 staticpro (&Qtext);
22217 Qrisky_local_variable = intern ("risky-local-variable");
22218 staticpro (&Qrisky_local_variable);
22219 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22220 staticpro (&Qinhibit_free_realized_faces);
22221
22222 list_of_error = Fcons (Fcons (intern ("error"),
22223 Fcons (intern ("void-variable"), Qnil)),
22224 Qnil);
22225 staticpro (&list_of_error);
22226
22227 Qlast_arrow_position = intern ("last-arrow-position");
22228 staticpro (&Qlast_arrow_position);
22229 Qlast_arrow_string = intern ("last-arrow-string");
22230 staticpro (&Qlast_arrow_string);
22231
22232 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22233 staticpro (&Qoverlay_arrow_string);
22234 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22235 staticpro (&Qoverlay_arrow_bitmap);
22236
22237 echo_buffer[0] = echo_buffer[1] = Qnil;
22238 staticpro (&echo_buffer[0]);
22239 staticpro (&echo_buffer[1]);
22240
22241 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22242 staticpro (&echo_area_buffer[0]);
22243 staticpro (&echo_area_buffer[1]);
22244
22245 Vmessages_buffer_name = build_string ("*Messages*");
22246 staticpro (&Vmessages_buffer_name);
22247
22248 mode_line_proptrans_alist = Qnil;
22249 staticpro (&mode_line_proptrans_alist);
22250
22251 mode_line_string_list = Qnil;
22252 staticpro (&mode_line_string_list);
22253
22254 help_echo_string = Qnil;
22255 staticpro (&help_echo_string);
22256 help_echo_object = Qnil;
22257 staticpro (&help_echo_object);
22258 help_echo_window = Qnil;
22259 staticpro (&help_echo_window);
22260 previous_help_echo_string = Qnil;
22261 staticpro (&previous_help_echo_string);
22262 help_echo_pos = -1;
22263
22264 #ifdef HAVE_WINDOW_SYSTEM
22265 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22266 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22267 For example, if a block cursor is over a tab, it will be drawn as
22268 wide as that tab on the display. */);
22269 x_stretch_cursor_p = 0;
22270 #endif
22271
22272 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22273 doc: /* *Non-nil means highlight trailing whitespace.
22274 The face used for trailing whitespace is `trailing-whitespace'. */);
22275 Vshow_trailing_whitespace = Qnil;
22276
22277 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22278 doc: /* *The pointer shape to show in void text areas.
22279 Nil means to show the text pointer. Other options are `arrow', `text',
22280 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22281 Vvoid_text_area_pointer = Qarrow;
22282
22283 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22284 doc: /* Non-nil means don't actually do any redisplay.
22285 This is used for internal purposes. */);
22286 Vinhibit_redisplay = Qnil;
22287
22288 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22289 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22290 Vglobal_mode_string = Qnil;
22291
22292 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22293 doc: /* Marker for where to display an arrow on top of the buffer text.
22294 This must be the beginning of a line in order to work.
22295 See also `overlay-arrow-string'. */);
22296 Voverlay_arrow_position = Qnil;
22297
22298 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22299 doc: /* String to display as an arrow in non-window frames.
22300 See also `overlay-arrow-position'. */);
22301 Voverlay_arrow_string = Qnil;
22302
22303 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22304 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22305 The symbols on this list are examined during redisplay to determine
22306 where to display overlay arrows. */);
22307 Voverlay_arrow_variable_list
22308 = Fcons (intern ("overlay-arrow-position"), Qnil);
22309
22310 DEFVAR_INT ("scroll-step", &scroll_step,
22311 doc: /* *The number of lines to try scrolling a window by when point moves out.
22312 If that fails to bring point back on frame, point is centered instead.
22313 If this is zero, point is always centered after it moves off frame.
22314 If you want scrolling to always be a line at a time, you should set
22315 `scroll-conservatively' to a large value rather than set this to 1. */);
22316
22317 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22318 doc: /* *Scroll up to this many lines, to bring point back on screen.
22319 A value of zero means to scroll the text to center point vertically
22320 in the window. */);
22321 scroll_conservatively = 0;
22322
22323 DEFVAR_INT ("scroll-margin", &scroll_margin,
22324 doc: /* *Number of lines of margin at the top and bottom of a window.
22325 Recenter the window whenever point gets within this many lines
22326 of the top or bottom of the window. */);
22327 scroll_margin = 0;
22328
22329 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22330 doc: /* Pixels per inch on current display.
22331 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22332 Vdisplay_pixels_per_inch = make_float (72.0);
22333
22334 #if GLYPH_DEBUG
22335 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22336 #endif
22337
22338 DEFVAR_BOOL ("truncate-partial-width-windows",
22339 &truncate_partial_width_windows,
22340 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22341 truncate_partial_width_windows = 1;
22342
22343 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22344 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22345 Any other value means to use the appropriate face, `mode-line',
22346 `header-line', or `menu' respectively. */);
22347 mode_line_inverse_video = 1;
22348
22349 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22350 doc: /* *Maximum buffer size for which line number should be displayed.
22351 If the buffer is bigger than this, the line number does not appear
22352 in the mode line. A value of nil means no limit. */);
22353 Vline_number_display_limit = Qnil;
22354
22355 DEFVAR_INT ("line-number-display-limit-width",
22356 &line_number_display_limit_width,
22357 doc: /* *Maximum line width (in characters) for line number display.
22358 If the average length of the lines near point is bigger than this, then the
22359 line number may be omitted from the mode line. */);
22360 line_number_display_limit_width = 200;
22361
22362 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22363 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22364 highlight_nonselected_windows = 0;
22365
22366 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22367 doc: /* Non-nil if more than one frame is visible on this display.
22368 Minibuffer-only frames don't count, but iconified frames do.
22369 This variable is not guaranteed to be accurate except while processing
22370 `frame-title-format' and `icon-title-format'. */);
22371
22372 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22373 doc: /* Template for displaying the title bar of visible frames.
22374 \(Assuming the window manager supports this feature.)
22375 This variable has the same structure as `mode-line-format' (which see),
22376 and is used only on frames for which no explicit name has been set
22377 \(see `modify-frame-parameters'). */);
22378
22379 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22380 doc: /* Template for displaying the title bar of an iconified frame.
22381 \(Assuming the window manager supports this feature.)
22382 This variable has the same structure as `mode-line-format' (which see),
22383 and is used only on frames for which no explicit name has been set
22384 \(see `modify-frame-parameters'). */);
22385 Vicon_title_format
22386 = Vframe_title_format
22387 = Fcons (intern ("multiple-frames"),
22388 Fcons (build_string ("%b"),
22389 Fcons (Fcons (empty_string,
22390 Fcons (intern ("invocation-name"),
22391 Fcons (build_string ("@"),
22392 Fcons (intern ("system-name"),
22393 Qnil)))),
22394 Qnil)));
22395
22396 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22397 doc: /* Maximum number of lines to keep in the message log buffer.
22398 If nil, disable message logging. If t, log messages but don't truncate
22399 the buffer when it becomes large. */);
22400 Vmessage_log_max = make_number (50);
22401
22402 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22403 doc: /* Functions called before redisplay, if window sizes have changed.
22404 The value should be a list of functions that take one argument.
22405 Just before redisplay, for each frame, if any of its windows have changed
22406 size since the last redisplay, or have been split or deleted,
22407 all the functions in the list are called, with the frame as argument. */);
22408 Vwindow_size_change_functions = Qnil;
22409
22410 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22411 doc: /* List of functions to call before redisplaying a window with scrolling.
22412 Each function is called with two arguments, the window
22413 and its new display-start position. Note that the value of `window-end'
22414 is not valid when these functions are called. */);
22415 Vwindow_scroll_functions = Qnil;
22416
22417 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22418 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22419 mouse_autoselect_window = 0;
22420
22421 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22422 doc: /* *Non-nil means automatically resize tool-bars.
22423 This increases a tool-bar's height if not all tool-bar items are visible.
22424 It decreases a tool-bar's height when it would display blank lines
22425 otherwise. */);
22426 auto_resize_tool_bars_p = 1;
22427
22428 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22429 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22430 auto_raise_tool_bar_buttons_p = 1;
22431
22432 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22433 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22434 make_cursor_line_fully_visible_p = 1;
22435
22436 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22437 doc: /* *Margin around tool-bar buttons in pixels.
22438 If an integer, use that for both horizontal and vertical margins.
22439 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22440 HORZ specifying the horizontal margin, and VERT specifying the
22441 vertical margin. */);
22442 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22443
22444 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22445 doc: /* *Relief thickness of tool-bar buttons. */);
22446 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22447
22448 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22449 doc: /* List of functions to call to fontify regions of text.
22450 Each function is called with one argument POS. Functions must
22451 fontify a region starting at POS in the current buffer, and give
22452 fontified regions the property `fontified'. */);
22453 Vfontification_functions = Qnil;
22454 Fmake_variable_buffer_local (Qfontification_functions);
22455
22456 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22457 &unibyte_display_via_language_environment,
22458 doc: /* *Non-nil means display unibyte text according to language environment.
22459 Specifically this means that unibyte non-ASCII characters
22460 are displayed by converting them to the equivalent multibyte characters
22461 according to the current language environment. As a result, they are
22462 displayed according to the current fontset. */);
22463 unibyte_display_via_language_environment = 0;
22464
22465 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22466 doc: /* *Maximum height for resizing mini-windows.
22467 If a float, it specifies a fraction of the mini-window frame's height.
22468 If an integer, it specifies a number of lines. */);
22469 Vmax_mini_window_height = make_float (0.25);
22470
22471 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22472 doc: /* *How to resize mini-windows.
22473 A value of nil means don't automatically resize mini-windows.
22474 A value of t means resize them to fit the text displayed in them.
22475 A value of `grow-only', the default, means let mini-windows grow
22476 only, until their display becomes empty, at which point the windows
22477 go back to their normal size. */);
22478 Vresize_mini_windows = Qgrow_only;
22479
22480 DEFVAR_LISP ("cursor-in-non-selected-windows",
22481 &Vcursor_in_non_selected_windows,
22482 doc: /* *Cursor type to display in non-selected windows.
22483 t means to use hollow box cursor. See `cursor-type' for other values. */);
22484 Vcursor_in_non_selected_windows = Qt;
22485
22486 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22487 doc: /* Alist specifying how to blink the cursor off.
22488 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22489 `cursor-type' frame-parameter or variable equals ON-STATE,
22490 comparing using `equal', Emacs uses OFF-STATE to specify
22491 how to blink it off. */);
22492 Vblink_cursor_alist = Qnil;
22493
22494 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22495 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22496 automatic_hscrolling_p = 1;
22497
22498 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22499 doc: /* *How many columns away from the window edge point is allowed to get
22500 before automatic hscrolling will horizontally scroll the window. */);
22501 hscroll_margin = 5;
22502
22503 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22504 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22505 When point is less than `automatic-hscroll-margin' columns from the window
22506 edge, automatic hscrolling will scroll the window by the amount of columns
22507 determined by this variable. If its value is a positive integer, scroll that
22508 many columns. If it's a positive floating-point number, it specifies the
22509 fraction of the window's width to scroll. If it's nil or zero, point will be
22510 centered horizontally after the scroll. Any other value, including negative
22511 numbers, are treated as if the value were zero.
22512
22513 Automatic hscrolling always moves point outside the scroll margin, so if
22514 point was more than scroll step columns inside the margin, the window will
22515 scroll more than the value given by the scroll step.
22516
22517 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22518 and `scroll-right' overrides this variable's effect. */);
22519 Vhscroll_step = make_number (0);
22520
22521 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22522 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22523 Bind this around calls to `message' to let it take effect. */);
22524 message_truncate_lines = 0;
22525
22526 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22527 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22528 Can be used to update submenus whose contents should vary. */);
22529 Vmenu_bar_update_hook = Qnil;
22530
22531 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22532 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22533 inhibit_menubar_update = 0;
22534
22535 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22536 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22537 inhibit_eval_during_redisplay = 0;
22538
22539 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22540 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22541 inhibit_free_realized_faces = 0;
22542
22543 #if GLYPH_DEBUG
22544 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22545 doc: /* Inhibit try_window_id display optimization. */);
22546 inhibit_try_window_id = 0;
22547
22548 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22549 doc: /* Inhibit try_window_reusing display optimization. */);
22550 inhibit_try_window_reusing = 0;
22551
22552 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22553 doc: /* Inhibit try_cursor_movement display optimization. */);
22554 inhibit_try_cursor_movement = 0;
22555 #endif /* GLYPH_DEBUG */
22556 }
22557
22558
22559 /* Initialize this module when Emacs starts. */
22560
22561 void
22562 init_xdisp ()
22563 {
22564 Lisp_Object root_window;
22565 struct window *mini_w;
22566
22567 current_header_line_height = current_mode_line_height = -1;
22568
22569 CHARPOS (this_line_start_pos) = 0;
22570
22571 mini_w = XWINDOW (minibuf_window);
22572 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22573
22574 if (!noninteractive)
22575 {
22576 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22577 int i;
22578
22579 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22580 set_window_height (root_window,
22581 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22582 0);
22583 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22584 set_window_height (minibuf_window, 1, 0);
22585
22586 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22587 mini_w->total_cols = make_number (FRAME_COLS (f));
22588
22589 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22590 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22591 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22592
22593 /* The default ellipsis glyphs `...'. */
22594 for (i = 0; i < 3; ++i)
22595 default_invis_vector[i] = make_number ('.');
22596 }
22597
22598 {
22599 /* Allocate the buffer for frame titles.
22600 Also used for `format-mode-line'. */
22601 int size = 100;
22602 frame_title_buf = (char *) xmalloc (size);
22603 frame_title_buf_end = frame_title_buf + size;
22604 frame_title_ptr = NULL;
22605 }
22606
22607 help_echo_showing_p = 0;
22608 }
22609
22610
22611 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22612 (do not change this comment) */