(iswitchb-global-map): Fix typo. Removed unwanted ###autoloads from
[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
202 Cursor No_Cursor;
203 #endif
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
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 /* Margin around tool bar buttons in pixels. */
268
269 Lisp_Object Vtool_bar_button_margin;
270
271 /* Thickness of shadow to draw around tool bar buttons. */
272
273 EMACS_INT tool_bar_button_relief;
274
275 /* Non-zero means automatically resize tool-bars so that all tool-bar
276 items are visible, and no blank lines remain. */
277
278 int auto_resize_tool_bars_p;
279
280 /* Non-zero means draw block and hollow cursor as wide as the glyph
281 under it. For example, if a block cursor is over a tab, it will be
282 drawn as wide as that tab on the display. */
283
284 int x_stretch_cursor_p;
285
286 /* Non-nil means don't actually do any redisplay. */
287
288 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
289
290 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
291
292 int inhibit_eval_during_redisplay;
293
294 /* Names of text properties relevant for redisplay. */
295
296 Lisp_Object Qdisplay;
297 extern Lisp_Object Qface, Qinvisible, Qwidth;
298
299 /* Symbols used in text property values. */
300
301 Lisp_Object Vdisplay_pixels_per_inch;
302 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
303 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
304 Lisp_Object Qslice;
305 Lisp_Object Qcenter;
306 Lisp_Object Qmargin, Qpointer;
307 Lisp_Object Qline_height, Qtotal;
308 extern Lisp_Object Qheight;
309 extern Lisp_Object QCwidth, QCheight, QCascent;
310 extern Lisp_Object Qscroll_bar;
311
312 /* Non-nil means highlight trailing whitespace. */
313
314 Lisp_Object Vshow_trailing_whitespace;
315
316 #ifdef HAVE_WINDOW_SYSTEM
317 extern Lisp_Object Voverflow_newline_into_fringe;
318
319 /* Test if overflow newline into fringe. Called with iterator IT
320 at or past right window margin, and with IT->current_x set. */
321
322 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
323 (!NILP (Voverflow_newline_into_fringe) \
324 && FRAME_WINDOW_P (it->f) \
325 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
326 && it->current_x == it->last_visible_x)
327
328 #endif /* HAVE_WINDOW_SYSTEM */
329
330 /* Non-nil means show the text cursor in void text areas
331 i.e. in blank areas after eol and eob. This used to be
332 the default in 21.3. */
333
334 Lisp_Object Vvoid_text_area_pointer;
335
336 /* Name of the face used to highlight trailing whitespace. */
337
338 Lisp_Object Qtrailing_whitespace;
339
340 /* The symbol `image' which is the car of the lists used to represent
341 images in Lisp. */
342
343 Lisp_Object Qimage;
344
345 /* The image map types. */
346 Lisp_Object QCmap, QCpointer;
347 Lisp_Object Qrect, Qcircle, Qpoly;
348
349 /* Non-zero means print newline to stdout before next mini-buffer
350 message. */
351
352 int noninteractive_need_newline;
353
354 /* Non-zero means print newline to message log before next message. */
355
356 static int message_log_need_newline;
357
358 /* Three markers that message_dolog uses.
359 It could allocate them itself, but that causes trouble
360 in handling memory-full errors. */
361 static Lisp_Object message_dolog_marker1;
362 static Lisp_Object message_dolog_marker2;
363 static Lisp_Object message_dolog_marker3;
364 \f
365 /* The buffer position of the first character appearing entirely or
366 partially on the line of the selected window which contains the
367 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
368 redisplay optimization in redisplay_internal. */
369
370 static struct text_pos this_line_start_pos;
371
372 /* Number of characters past the end of the line above, including the
373 terminating newline. */
374
375 static struct text_pos this_line_end_pos;
376
377 /* The vertical positions and the height of this line. */
378
379 static int this_line_vpos;
380 static int this_line_y;
381 static int this_line_pixel_height;
382
383 /* X position at which this display line starts. Usually zero;
384 negative if first character is partially visible. */
385
386 static int this_line_start_x;
387
388 /* Buffer that this_line_.* variables are referring to. */
389
390 static struct buffer *this_line_buffer;
391
392 /* Nonzero means truncate lines in all windows less wide than the
393 frame. */
394
395 int truncate_partial_width_windows;
396
397 /* A flag to control how to display unibyte 8-bit character. */
398
399 int unibyte_display_via_language_environment;
400
401 /* Nonzero means we have more than one non-mini-buffer-only frame.
402 Not guaranteed to be accurate except while parsing
403 frame-title-format. */
404
405 int multiple_frames;
406
407 Lisp_Object Vglobal_mode_string;
408
409
410 /* List of variables (symbols) which hold markers for overlay arrows.
411 The symbols on this list are examined during redisplay to determine
412 where to display overlay arrows. */
413
414 Lisp_Object Voverlay_arrow_variable_list;
415
416 /* Marker for where to display an arrow on top of the buffer text. */
417
418 Lisp_Object Voverlay_arrow_position;
419
420 /* String to display for the arrow. Only used on terminal frames. */
421
422 Lisp_Object Voverlay_arrow_string;
423
424 /* Values of those variables at last redisplay are stored as
425 properties on `overlay-arrow-position' symbol. However, if
426 Voverlay_arrow_position is a marker, last-arrow-position is its
427 numerical position. */
428
429 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
430
431 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
432 properties on a symbol in overlay-arrow-variable-list. */
433
434 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
435
436 /* Like mode-line-format, but for the title bar on a visible frame. */
437
438 Lisp_Object Vframe_title_format;
439
440 /* Like mode-line-format, but for the title bar on an iconified frame. */
441
442 Lisp_Object Vicon_title_format;
443
444 /* List of functions to call when a window's size changes. These
445 functions get one arg, a frame on which one or more windows' sizes
446 have changed. */
447
448 static Lisp_Object Vwindow_size_change_functions;
449
450 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
451
452 /* Nonzero if overlay arrow has been displayed once in this window. */
453
454 static int overlay_arrow_seen;
455
456 /* Nonzero means highlight the region even in nonselected windows. */
457
458 int highlight_nonselected_windows;
459
460 /* If cursor motion alone moves point off frame, try scrolling this
461 many lines up or down if that will bring it back. */
462
463 static EMACS_INT scroll_step;
464
465 /* Nonzero means scroll just far enough to bring point back on the
466 screen, when appropriate. */
467
468 static EMACS_INT scroll_conservatively;
469
470 /* Recenter the window whenever point gets within this many lines of
471 the top or bottom of the window. This value is translated into a
472 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
473 that there is really a fixed pixel height scroll margin. */
474
475 EMACS_INT scroll_margin;
476
477 /* Number of windows showing the buffer of the selected window (or
478 another buffer with the same base buffer). keyboard.c refers to
479 this. */
480
481 int buffer_shared;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* Zero means display the mode-line/header-line/menu-bar in the default face
488 (this slightly odd definition is for compatibility with previous versions
489 of emacs), non-zero means display them using their respective faces.
490
491 This variable is deprecated. */
492
493 int mode_line_inverse_video;
494
495 /* Prompt to display in front of the mini-buffer contents. */
496
497 Lisp_Object minibuf_prompt;
498
499 /* Width of current mini-buffer prompt. Only set after display_line
500 of the line that contains the prompt. */
501
502 int minibuf_prompt_width;
503
504 /* This is the window where the echo area message was displayed. It
505 is always a mini-buffer window, but it may not be the same window
506 currently active as a mini-buffer. */
507
508 Lisp_Object echo_area_window;
509
510 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
511 pushes the current message and the value of
512 message_enable_multibyte on the stack, the function restore_message
513 pops the stack and displays MESSAGE again. */
514
515 Lisp_Object Vmessage_stack;
516
517 /* Nonzero means multibyte characters were enabled when the echo area
518 message was specified. */
519
520 int message_enable_multibyte;
521
522 /* Nonzero if we should redraw the mode lines on the next redisplay. */
523
524 int update_mode_lines;
525
526 /* Nonzero if window sizes or contents have changed since last
527 redisplay that finished. */
528
529 int windows_or_buffers_changed;
530
531 /* Nonzero means a frame's cursor type has been changed. */
532
533 int cursor_type_changed;
534
535 /* Nonzero after display_mode_line if %l was used and it displayed a
536 line number. */
537
538 int line_number_displayed;
539
540 /* Maximum buffer size for which to display line numbers. */
541
542 Lisp_Object Vline_number_display_limit;
543
544 /* Line width to consider when repositioning for line number display. */
545
546 static EMACS_INT line_number_display_limit_width;
547
548 /* Number of lines to keep in the message log buffer. t means
549 infinite. nil means don't log at all. */
550
551 Lisp_Object Vmessage_log_max;
552
553 /* The name of the *Messages* buffer, a string. */
554
555 static Lisp_Object Vmessages_buffer_name;
556
557 /* Current, index 0, and last displayed echo area message. Either
558 buffers from echo_buffers, or nil to indicate no message. */
559
560 Lisp_Object echo_area_buffer[2];
561
562 /* The buffers referenced from echo_area_buffer. */
563
564 static Lisp_Object echo_buffer[2];
565
566 /* A vector saved used in with_area_buffer to reduce consing. */
567
568 static Lisp_Object Vwith_echo_area_save_vector;
569
570 /* Non-zero means display_echo_area should display the last echo area
571 message again. Set by redisplay_preserve_echo_area. */
572
573 static int display_last_displayed_message_p;
574
575 /* Nonzero if echo area is being used by print; zero if being used by
576 message. */
577
578 int message_buf_print;
579
580 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
581
582 Lisp_Object Qinhibit_menubar_update;
583 int inhibit_menubar_update;
584
585 /* Maximum height for resizing mini-windows. Either a float
586 specifying a fraction of the available height, or an integer
587 specifying a number of lines. */
588
589 Lisp_Object Vmax_mini_window_height;
590
591 /* Non-zero means messages should be displayed with truncated
592 lines instead of being continued. */
593
594 int message_truncate_lines;
595 Lisp_Object Qmessage_truncate_lines;
596
597 /* Set to 1 in clear_message to make redisplay_internal aware
598 of an emptied echo area. */
599
600 static int message_cleared_p;
601
602 /* Non-zero means we want a hollow cursor in windows that are not
603 selected. Zero means there's no cursor in such windows. */
604
605 Lisp_Object Vcursor_in_non_selected_windows;
606 Lisp_Object Qcursor_in_non_selected_windows;
607
608 /* How to blink the default frame cursor off. */
609 Lisp_Object Vblink_cursor_alist;
610
611 /* A scratch glyph row with contents used for generating truncation
612 glyphs. Also used in direct_output_for_insert. */
613
614 #define MAX_SCRATCH_GLYPHS 100
615 struct glyph_row scratch_glyph_row;
616 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
617
618 /* Ascent and height of the last line processed by move_it_to. */
619
620 static int last_max_ascent, last_height;
621
622 /* Non-zero if there's a help-echo in the echo area. */
623
624 int help_echo_showing_p;
625
626 /* If >= 0, computed, exact values of mode-line and header-line height
627 to use in the macros CURRENT_MODE_LINE_HEIGHT and
628 CURRENT_HEADER_LINE_HEIGHT. */
629
630 int current_mode_line_height, current_header_line_height;
631
632 /* The maximum distance to look ahead for text properties. Values
633 that are too small let us call compute_char_face and similar
634 functions too often which is expensive. Values that are too large
635 let us call compute_char_face and alike too often because we
636 might not be interested in text properties that far away. */
637
638 #define TEXT_PROP_DISTANCE_LIMIT 100
639
640 #if GLYPH_DEBUG
641
642 /* Variables to turn off display optimizations from Lisp. */
643
644 int inhibit_try_window_id, inhibit_try_window_reusing;
645 int inhibit_try_cursor_movement;
646
647 /* Non-zero means print traces of redisplay if compiled with
648 GLYPH_DEBUG != 0. */
649
650 int trace_redisplay_p;
651
652 #endif /* GLYPH_DEBUG */
653
654 #ifdef DEBUG_TRACE_MOVE
655 /* Non-zero means trace with TRACE_MOVE to stderr. */
656 int trace_move;
657
658 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
659 #else
660 #define TRACE_MOVE(x) (void) 0
661 #endif
662
663 /* Non-zero means automatically scroll windows horizontally to make
664 point visible. */
665
666 int automatic_hscrolling_p;
667
668 /* How close to the margin can point get before the window is scrolled
669 horizontally. */
670 EMACS_INT hscroll_margin;
671
672 /* How much to scroll horizontally when point is inside the above margin. */
673 Lisp_Object Vhscroll_step;
674
675 /* A list of symbols, one for each supported image type. */
676
677 Lisp_Object Vimage_types;
678
679 /* The variable `resize-mini-windows'. If nil, don't resize
680 mini-windows. If t, always resize them to fit the text they
681 display. If `grow-only', let mini-windows grow only until they
682 become empty. */
683
684 Lisp_Object Vresize_mini_windows;
685
686 /* Buffer being redisplayed -- for redisplay_window_error. */
687
688 struct buffer *displayed_buffer;
689
690 /* Value returned from text property handlers (see below). */
691
692 enum prop_handled
693 {
694 HANDLED_NORMALLY,
695 HANDLED_RECOMPUTE_PROPS,
696 HANDLED_OVERLAY_STRING_CONSUMED,
697 HANDLED_RETURN
698 };
699
700 /* A description of text properties that redisplay is interested
701 in. */
702
703 struct props
704 {
705 /* The name of the property. */
706 Lisp_Object *name;
707
708 /* A unique index for the property. */
709 enum prop_idx idx;
710
711 /* A handler function called to set up iterator IT from the property
712 at IT's current position. Value is used to steer handle_stop. */
713 enum prop_handled (*handler) P_ ((struct it *it));
714 };
715
716 static enum prop_handled handle_face_prop P_ ((struct it *));
717 static enum prop_handled handle_invisible_prop P_ ((struct it *));
718 static enum prop_handled handle_display_prop P_ ((struct it *));
719 static enum prop_handled handle_composition_prop P_ ((struct it *));
720 static enum prop_handled handle_overlay_change P_ ((struct it *));
721 static enum prop_handled handle_fontified_prop P_ ((struct it *));
722
723 /* Properties handled by iterators. */
724
725 static struct props it_props[] =
726 {
727 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
728 /* Handle `face' before `display' because some sub-properties of
729 `display' need to know the face. */
730 {&Qface, FACE_PROP_IDX, handle_face_prop},
731 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
732 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
733 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
734 {NULL, 0, NULL}
735 };
736
737 /* Value is the position described by X. If X is a marker, value is
738 the marker_position of X. Otherwise, value is X. */
739
740 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
741
742 /* Enumeration returned by some move_it_.* functions internally. */
743
744 enum move_it_result
745 {
746 /* Not used. Undefined value. */
747 MOVE_UNDEFINED,
748
749 /* Move ended at the requested buffer position or ZV. */
750 MOVE_POS_MATCH_OR_ZV,
751
752 /* Move ended at the requested X pixel position. */
753 MOVE_X_REACHED,
754
755 /* Move within a line ended at the end of a line that must be
756 continued. */
757 MOVE_LINE_CONTINUED,
758
759 /* Move within a line ended at the end of a line that would
760 be displayed truncated. */
761 MOVE_LINE_TRUNCATED,
762
763 /* Move within a line ended at a line end. */
764 MOVE_NEWLINE_OR_CR
765 };
766
767 /* This counter is used to clear the face cache every once in a while
768 in redisplay_internal. It is incremented for each redisplay.
769 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
770 cleared. */
771
772 #define CLEAR_FACE_CACHE_COUNT 500
773 static int clear_face_cache_count;
774
775 /* Record the previous terminal frame we displayed. */
776
777 static struct frame *previous_terminal_frame;
778
779 /* Non-zero while redisplay_internal is in progress. */
780
781 int redisplaying_p;
782
783 /* Non-zero means don't free realized faces. Bound while freeing
784 realized faces is dangerous because glyph matrices might still
785 reference them. */
786
787 int inhibit_free_realized_faces;
788 Lisp_Object Qinhibit_free_realized_faces;
789
790 /* If a string, XTread_socket generates an event to display that string.
791 (The display is done in read_char.) */
792
793 Lisp_Object help_echo_string;
794 Lisp_Object help_echo_window;
795 Lisp_Object help_echo_object;
796 int help_echo_pos;
797
798 /* Temporary variable for XTread_socket. */
799
800 Lisp_Object previous_help_echo_string;
801
802 /* Null glyph slice */
803
804 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
805
806 \f
807 /* Function prototypes. */
808
809 static void setup_for_ellipsis P_ ((struct it *));
810 static void mark_window_display_accurate_1 P_ ((struct window *, int));
811 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
812 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
813 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
814 static int redisplay_mode_lines P_ ((Lisp_Object, int));
815 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
816
817 #if 0
818 static int invisible_text_between_p P_ ((struct it *, int, int));
819 #endif
820
821 static int next_element_from_ellipsis P_ ((struct it *));
822 static void pint2str P_ ((char *, int, int));
823 static void pint2hrstr P_ ((char *, int, int));
824 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
825 struct text_pos));
826 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
827 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
828 static void store_frame_title_char P_ ((char));
829 static int store_frame_title P_ ((const unsigned char *, int, int));
830 static void x_consider_frame_title P_ ((Lisp_Object));
831 static void handle_stop P_ ((struct it *));
832 static int tool_bar_lines_needed P_ ((struct frame *));
833 static int single_display_prop_intangible_p P_ ((Lisp_Object));
834 static void ensure_echo_area_buffers P_ ((void));
835 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
836 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
837 static int with_echo_area_buffer P_ ((struct window *, int,
838 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
839 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840 static void clear_garbaged_frames P_ ((void));
841 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
842 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
843 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static int display_echo_area P_ ((struct window *));
845 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
846 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
847 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
848 static int string_char_and_length P_ ((const unsigned char *, int, int *));
849 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
850 struct text_pos));
851 static int compute_window_start_on_continuation_line P_ ((struct window *));
852 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
853 static void insert_left_trunc_glyphs P_ ((struct it *));
854 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
855 Lisp_Object));
856 static void extend_face_to_end_of_line P_ ((struct it *));
857 static int append_space_for_newline P_ ((struct it *, int));
858 static int make_cursor_line_fully_visible P_ ((struct window *, int));
859 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
860 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
861 static int trailing_whitespace_p P_ ((int));
862 static int message_log_check_duplicate P_ ((int, int, int, int));
863 static void push_it P_ ((struct it *));
864 static void pop_it P_ ((struct it *));
865 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
866 static void select_frame_for_redisplay P_ ((Lisp_Object));
867 static void redisplay_internal P_ ((int));
868 static int echo_area_display P_ ((int));
869 static void redisplay_windows P_ ((Lisp_Object));
870 static void redisplay_window P_ ((Lisp_Object, int));
871 static Lisp_Object redisplay_window_error ();
872 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
873 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
874 static void update_menu_bar P_ ((struct frame *, int));
875 static int try_window_reusing_current_matrix P_ ((struct window *));
876 static int try_window_id P_ ((struct window *));
877 static int display_line P_ ((struct it *));
878 static int display_mode_lines P_ ((struct window *));
879 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
880 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
881 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
882 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
883 static void display_menu_bar P_ ((struct window *));
884 static int display_count_lines P_ ((int, int, int, int, int *));
885 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
886 int, int, struct it *, int, int, int, int));
887 static void compute_line_metrics P_ ((struct it *));
888 static void run_redisplay_end_trigger_hook P_ ((struct it *));
889 static int get_overlay_strings P_ ((struct it *, int));
890 static void next_overlay_string P_ ((struct it *));
891 static void reseat P_ ((struct it *, struct text_pos, int));
892 static void reseat_1 P_ ((struct it *, struct text_pos, int));
893 static void back_to_previous_visible_line_start P_ ((struct it *));
894 static void reseat_at_previous_visible_line_start P_ ((struct it *));
895 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
896 static int next_element_from_display_vector P_ ((struct it *));
897 static int next_element_from_string P_ ((struct it *));
898 static int next_element_from_c_string P_ ((struct it *));
899 static int next_element_from_buffer P_ ((struct it *));
900 static int next_element_from_composition P_ ((struct it *));
901 static int next_element_from_image P_ ((struct it *));
902 static int next_element_from_stretch P_ ((struct it *));
903 static void load_overlay_strings P_ ((struct it *, int));
904 static int init_from_display_pos P_ ((struct it *, struct window *,
905 struct display_pos *));
906 static void reseat_to_string P_ ((struct it *, unsigned char *,
907 Lisp_Object, int, int, int, int));
908 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
909 int, int, int));
910 void move_it_vertically_backward P_ ((struct it *, int));
911 static void init_to_row_start P_ ((struct it *, struct window *,
912 struct glyph_row *));
913 static int init_to_row_end P_ ((struct it *, struct window *,
914 struct glyph_row *));
915 static void back_to_previous_line_start P_ ((struct it *));
916 static int forward_to_next_line_start P_ ((struct it *, int *));
917 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
918 Lisp_Object, int));
919 static struct text_pos string_pos P_ ((int, Lisp_Object));
920 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
921 static int number_of_chars P_ ((unsigned char *, int));
922 static void compute_stop_pos P_ ((struct it *));
923 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
924 Lisp_Object));
925 static int face_before_or_after_it_pos P_ ((struct it *, int));
926 static int next_overlay_change P_ ((int));
927 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
928 Lisp_Object, struct text_pos *,
929 int));
930 static int underlying_face_id P_ ((struct it *));
931 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
932 struct window *));
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void update_tool_bar P_ ((struct frame *, int));
940 static void build_desired_tool_bar_string P_ ((struct frame *f));
941 static int redisplay_tool_bar P_ ((struct frame *));
942 static void display_tool_bar_line P_ ((struct it *));
943 static void notice_overwritten_cursor P_ ((struct window *,
944 enum glyph_row_area,
945 int, int, int, int));
946
947
948
949 #endif /* HAVE_WINDOW_SYSTEM */
950
951 \f
952 /***********************************************************************
953 Window display dimensions
954 ***********************************************************************/
955
956 /* Return the bottom boundary y-position for text lines in window W.
957 This is the first y position at which a line cannot start.
958 It is relative to the top of the window.
959
960 This is the height of W minus the height of a mode line, if any. */
961
962 INLINE int
963 window_text_bottom_y (w)
964 struct window *w;
965 {
966 int height = WINDOW_TOTAL_HEIGHT (w);
967
968 if (WINDOW_WANTS_MODELINE_P (w))
969 height -= CURRENT_MODE_LINE_HEIGHT (w);
970 return height;
971 }
972
973 /* Return the pixel width of display area AREA of window W. AREA < 0
974 means return the total width of W, not including fringes to
975 the left and right of the window. */
976
977 INLINE int
978 window_box_width (w, area)
979 struct window *w;
980 int area;
981 {
982 int cols = XFASTINT (w->total_cols);
983 int pixels = 0;
984
985 if (!w->pseudo_window_p)
986 {
987 cols -= WINDOW_SCROLL_BAR_COLS (w);
988
989 if (area == TEXT_AREA)
990 {
991 if (INTEGERP (w->left_margin_cols))
992 cols -= XFASTINT (w->left_margin_cols);
993 if (INTEGERP (w->right_margin_cols))
994 cols -= XFASTINT (w->right_margin_cols);
995 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
996 }
997 else if (area == LEFT_MARGIN_AREA)
998 {
999 cols = (INTEGERP (w->left_margin_cols)
1000 ? XFASTINT (w->left_margin_cols) : 0);
1001 pixels = 0;
1002 }
1003 else if (area == RIGHT_MARGIN_AREA)
1004 {
1005 cols = (INTEGERP (w->right_margin_cols)
1006 ? XFASTINT (w->right_margin_cols) : 0);
1007 pixels = 0;
1008 }
1009 }
1010
1011 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1012 }
1013
1014
1015 /* Return the pixel height of the display area of window W, not
1016 including mode lines of W, if any. */
1017
1018 INLINE int
1019 window_box_height (w)
1020 struct window *w;
1021 {
1022 struct frame *f = XFRAME (w->frame);
1023 int height = WINDOW_TOTAL_HEIGHT (w);
1024
1025 xassert (height >= 0);
1026
1027 /* Note: the code below that determines the mode-line/header-line
1028 height is essentially the same as that contained in the macro
1029 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1030 the appropriate glyph row has its `mode_line_p' flag set,
1031 and if it doesn't, uses estimate_mode_line_height instead. */
1032
1033 if (WINDOW_WANTS_MODELINE_P (w))
1034 {
1035 struct glyph_row *ml_row
1036 = (w->current_matrix && w->current_matrix->rows
1037 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1038 : 0);
1039 if (ml_row && ml_row->mode_line_p)
1040 height -= ml_row->height;
1041 else
1042 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1043 }
1044
1045 if (WINDOW_WANTS_HEADER_LINE_P (w))
1046 {
1047 struct glyph_row *hl_row
1048 = (w->current_matrix && w->current_matrix->rows
1049 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1050 : 0);
1051 if (hl_row && hl_row->mode_line_p)
1052 height -= hl_row->height;
1053 else
1054 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1055 }
1056
1057 /* With a very small font and a mode-line that's taller than
1058 default, we might end up with a negative height. */
1059 return max (0, height);
1060 }
1061
1062 /* Return the window-relative coordinate of the left edge of display
1063 area AREA of window W. AREA < 0 means return the left edge of the
1064 whole window, to the right of the left fringe of W. */
1065
1066 INLINE int
1067 window_box_left_offset (w, area)
1068 struct window *w;
1069 int area;
1070 {
1071 int x;
1072
1073 if (w->pseudo_window_p)
1074 return 0;
1075
1076 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1077
1078 if (area == TEXT_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA));
1081 else if (area == RIGHT_MARGIN_AREA)
1082 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1083 + window_box_width (w, LEFT_MARGIN_AREA)
1084 + window_box_width (w, TEXT_AREA)
1085 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1086 ? 0
1087 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1088 else if (area == LEFT_MARGIN_AREA
1089 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1090 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1091
1092 return x;
1093 }
1094
1095
1096 /* Return the window-relative coordinate of the right edge of display
1097 area AREA of window W. AREA < 0 means return the left edge of the
1098 whole window, to the left of the right fringe of W. */
1099
1100 INLINE int
1101 window_box_right_offset (w, area)
1102 struct window *w;
1103 int area;
1104 {
1105 return window_box_left_offset (w, area) + window_box_width (w, area);
1106 }
1107
1108 /* Return the frame-relative coordinate of the left edge of display
1109 area AREA of window W. AREA < 0 means return the left edge of the
1110 whole window, to the right of the left fringe of W. */
1111
1112 INLINE int
1113 window_box_left (w, area)
1114 struct window *w;
1115 int area;
1116 {
1117 struct frame *f = XFRAME (w->frame);
1118 int x;
1119
1120 if (w->pseudo_window_p)
1121 return FRAME_INTERNAL_BORDER_WIDTH (f);
1122
1123 x = (WINDOW_LEFT_EDGE_X (w)
1124 + window_box_left_offset (w, area));
1125
1126 return x;
1127 }
1128
1129
1130 /* Return the frame-relative coordinate of the right edge of display
1131 area AREA of window W. AREA < 0 means return the left edge of the
1132 whole window, to the left of the right fringe of W. */
1133
1134 INLINE int
1135 window_box_right (w, area)
1136 struct window *w;
1137 int area;
1138 {
1139 return window_box_left (w, area) + window_box_width (w, area);
1140 }
1141
1142 /* Get the bounding box of the display area AREA of window W, without
1143 mode lines, in frame-relative coordinates. AREA < 0 means the
1144 whole window, not including the left and right fringes of
1145 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1146 coordinates of the upper-left corner of the box. Return in
1147 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1148
1149 INLINE void
1150 window_box (w, area, box_x, box_y, box_width, box_height)
1151 struct window *w;
1152 int area;
1153 int *box_x, *box_y, *box_width, *box_height;
1154 {
1155 if (box_width)
1156 *box_width = window_box_width (w, area);
1157 if (box_height)
1158 *box_height = window_box_height (w);
1159 if (box_x)
1160 *box_x = window_box_left (w, area);
1161 if (box_y)
1162 {
1163 *box_y = WINDOW_TOP_EDGE_Y (w);
1164 if (WINDOW_WANTS_HEADER_LINE_P (w))
1165 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1166 }
1167 }
1168
1169
1170 /* Get the bounding box of the display area AREA of window W, without
1171 mode lines. AREA < 0 means the whole window, not including the
1172 left and right fringe of the window. Return in *TOP_LEFT_X
1173 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1174 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1175 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1176 box. */
1177
1178 INLINE void
1179 window_box_edges (w, area, top_left_x, top_left_y,
1180 bottom_right_x, bottom_right_y)
1181 struct window *w;
1182 int area;
1183 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1184 {
1185 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1186 bottom_right_y);
1187 *bottom_right_x += *top_left_x;
1188 *bottom_right_y += *top_left_y;
1189 }
1190
1191
1192 \f
1193 /***********************************************************************
1194 Utilities
1195 ***********************************************************************/
1196
1197 /* Return the bottom y-position of the line the iterator IT is in.
1198 This can modify IT's settings. */
1199
1200 int
1201 line_bottom_y (it)
1202 struct it *it;
1203 {
1204 int line_height = it->max_ascent + it->max_descent;
1205 int line_top_y = it->current_y;
1206
1207 if (line_height == 0)
1208 {
1209 if (last_height)
1210 line_height = last_height;
1211 else if (IT_CHARPOS (*it) < ZV)
1212 {
1213 move_it_by_lines (it, 1, 1);
1214 line_height = (it->max_ascent || it->max_descent
1215 ? it->max_ascent + it->max_descent
1216 : last_height);
1217 }
1218 else
1219 {
1220 struct glyph_row *row = it->glyph_row;
1221
1222 /* Use the default character height. */
1223 it->glyph_row = NULL;
1224 it->what = IT_CHARACTER;
1225 it->c = ' ';
1226 it->len = 1;
1227 PRODUCE_GLYPHS (it);
1228 line_height = it->ascent + it->descent;
1229 it->glyph_row = row;
1230 }
1231 }
1232
1233 return line_top_y + line_height;
1234 }
1235
1236
1237 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1238 1 if POS is visible and the line containing POS is fully visible.
1239 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1240 and header-lines heights. */
1241
1242 int
1243 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1244 struct window *w;
1245 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1246 {
1247 struct it it;
1248 struct text_pos top;
1249 int visible_p;
1250 struct buffer *old_buffer = NULL;
1251
1252 if (XBUFFER (w->buffer) != current_buffer)
1253 {
1254 old_buffer = current_buffer;
1255 set_buffer_internal_1 (XBUFFER (w->buffer));
1256 }
1257
1258 *fully = visible_p = 0;
1259 SET_TEXT_POS_FROM_MARKER (top, w->start);
1260
1261 /* Compute exact mode line heights, if requested. */
1262 if (exact_mode_line_heights_p)
1263 {
1264 if (WINDOW_WANTS_MODELINE_P (w))
1265 current_mode_line_height
1266 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1267 current_buffer->mode_line_format);
1268
1269 if (WINDOW_WANTS_HEADER_LINE_P (w))
1270 current_header_line_height
1271 = display_mode_line (w, HEADER_LINE_FACE_ID,
1272 current_buffer->header_line_format);
1273 }
1274
1275 start_display (&it, w, top);
1276 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1277 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1278
1279 /* Note that we may overshoot because of invisible text. */
1280 if (IT_CHARPOS (it) >= charpos)
1281 {
1282 int top_y = it.current_y;
1283 int bottom_y = line_bottom_y (&it);
1284 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1285
1286 if (top_y < window_top_y)
1287 visible_p = bottom_y > window_top_y;
1288 else if (top_y < it.last_visible_y)
1289 {
1290 visible_p = 1;
1291 *fully = bottom_y <= it.last_visible_y;
1292 }
1293 if (visible_p && x)
1294 {
1295 *x = it.current_x;
1296 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1297 }
1298 }
1299 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1300 {
1301 struct it it2;
1302
1303 it2 = it;
1304 move_it_by_lines (&it, 1, 0);
1305 if (charpos < IT_CHARPOS (it))
1306 {
1307 visible_p = 1;
1308 if (x)
1309 {
1310 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1311 *x = it2.current_x;
1312 *y = it2.current_y + it2.max_ascent - it2.ascent;
1313 }
1314 }
1315 }
1316
1317 if (old_buffer)
1318 set_buffer_internal_1 (old_buffer);
1319
1320 current_header_line_height = current_mode_line_height = -1;
1321
1322 return visible_p;
1323 }
1324
1325
1326 /* Return the next character from STR which is MAXLEN bytes long.
1327 Return in *LEN the length of the character. This is like
1328 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1329 we find one, we return a `?', but with the length of the invalid
1330 character. */
1331
1332 static INLINE int
1333 string_char_and_length (str, maxlen, len)
1334 const unsigned char *str;
1335 int maxlen, *len;
1336 {
1337 int c;
1338
1339 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1340 if (!CHAR_VALID_P (c, 1))
1341 /* We may not change the length here because other places in Emacs
1342 don't use this function, i.e. they silently accept invalid
1343 characters. */
1344 c = '?';
1345
1346 return c;
1347 }
1348
1349
1350
1351 /* Given a position POS containing a valid character and byte position
1352 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1353
1354 static struct text_pos
1355 string_pos_nchars_ahead (pos, string, nchars)
1356 struct text_pos pos;
1357 Lisp_Object string;
1358 int nchars;
1359 {
1360 xassert (STRINGP (string) && nchars >= 0);
1361
1362 if (STRING_MULTIBYTE (string))
1363 {
1364 int rest = SBYTES (string) - BYTEPOS (pos);
1365 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1366 int len;
1367
1368 while (nchars--)
1369 {
1370 string_char_and_length (p, rest, &len);
1371 p += len, rest -= len;
1372 xassert (rest >= 0);
1373 CHARPOS (pos) += 1;
1374 BYTEPOS (pos) += len;
1375 }
1376 }
1377 else
1378 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1379
1380 return pos;
1381 }
1382
1383
1384 /* Value is the text position, i.e. character and byte position,
1385 for character position CHARPOS in STRING. */
1386
1387 static INLINE struct text_pos
1388 string_pos (charpos, string)
1389 int charpos;
1390 Lisp_Object string;
1391 {
1392 struct text_pos pos;
1393 xassert (STRINGP (string));
1394 xassert (charpos >= 0);
1395 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1396 return pos;
1397 }
1398
1399
1400 /* Value is a text position, i.e. character and byte position, for
1401 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1402 means recognize multibyte characters. */
1403
1404 static struct text_pos
1405 c_string_pos (charpos, s, multibyte_p)
1406 int charpos;
1407 unsigned char *s;
1408 int multibyte_p;
1409 {
1410 struct text_pos pos;
1411
1412 xassert (s != NULL);
1413 xassert (charpos >= 0);
1414
1415 if (multibyte_p)
1416 {
1417 int rest = strlen (s), len;
1418
1419 SET_TEXT_POS (pos, 0, 0);
1420 while (charpos--)
1421 {
1422 string_char_and_length (s, rest, &len);
1423 s += len, rest -= len;
1424 xassert (rest >= 0);
1425 CHARPOS (pos) += 1;
1426 BYTEPOS (pos) += len;
1427 }
1428 }
1429 else
1430 SET_TEXT_POS (pos, charpos, charpos);
1431
1432 return pos;
1433 }
1434
1435
1436 /* Value is the number of characters in C string S. MULTIBYTE_P
1437 non-zero means recognize multibyte characters. */
1438
1439 static int
1440 number_of_chars (s, multibyte_p)
1441 unsigned char *s;
1442 int multibyte_p;
1443 {
1444 int nchars;
1445
1446 if (multibyte_p)
1447 {
1448 int rest = strlen (s), len;
1449 unsigned char *p = (unsigned char *) s;
1450
1451 for (nchars = 0; rest > 0; ++nchars)
1452 {
1453 string_char_and_length (p, rest, &len);
1454 rest -= len, p += len;
1455 }
1456 }
1457 else
1458 nchars = strlen (s);
1459
1460 return nchars;
1461 }
1462
1463
1464 /* Compute byte position NEWPOS->bytepos corresponding to
1465 NEWPOS->charpos. POS is a known position in string STRING.
1466 NEWPOS->charpos must be >= POS.charpos. */
1467
1468 static void
1469 compute_string_pos (newpos, pos, string)
1470 struct text_pos *newpos, pos;
1471 Lisp_Object string;
1472 {
1473 xassert (STRINGP (string));
1474 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1475
1476 if (STRING_MULTIBYTE (string))
1477 *newpos = string_pos_nchars_ahead (pos, string,
1478 CHARPOS (*newpos) - CHARPOS (pos));
1479 else
1480 BYTEPOS (*newpos) = CHARPOS (*newpos);
1481 }
1482
1483 /* EXPORT:
1484 Return an estimation of the pixel height of mode or top lines on
1485 frame F. FACE_ID specifies what line's height to estimate. */
1486
1487 int
1488 estimate_mode_line_height (f, face_id)
1489 struct frame *f;
1490 enum face_id face_id;
1491 {
1492 #ifdef HAVE_WINDOW_SYSTEM
1493 if (FRAME_WINDOW_P (f))
1494 {
1495 int height = FONT_HEIGHT (FRAME_FONT (f));
1496
1497 /* This function is called so early when Emacs starts that the face
1498 cache and mode line face are not yet initialized. */
1499 if (FRAME_FACE_CACHE (f))
1500 {
1501 struct face *face = FACE_FROM_ID (f, face_id);
1502 if (face)
1503 {
1504 if (face->font)
1505 height = FONT_HEIGHT (face->font);
1506 if (face->box_line_width > 0)
1507 height += 2 * face->box_line_width;
1508 }
1509 }
1510
1511 return height;
1512 }
1513 #endif
1514
1515 return 1;
1516 }
1517
1518 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1519 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1520 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1521 not force the value into range. */
1522
1523 void
1524 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1525 FRAME_PTR f;
1526 register int pix_x, pix_y;
1527 int *x, *y;
1528 NativeRectangle *bounds;
1529 int noclip;
1530 {
1531
1532 #ifdef HAVE_WINDOW_SYSTEM
1533 if (FRAME_WINDOW_P (f))
1534 {
1535 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1536 even for negative values. */
1537 if (pix_x < 0)
1538 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1539 if (pix_y < 0)
1540 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1541
1542 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1543 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1544
1545 if (bounds)
1546 STORE_NATIVE_RECT (*bounds,
1547 FRAME_COL_TO_PIXEL_X (f, pix_x),
1548 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1549 FRAME_COLUMN_WIDTH (f) - 1,
1550 FRAME_LINE_HEIGHT (f) - 1);
1551
1552 if (!noclip)
1553 {
1554 if (pix_x < 0)
1555 pix_x = 0;
1556 else if (pix_x > FRAME_TOTAL_COLS (f))
1557 pix_x = FRAME_TOTAL_COLS (f);
1558
1559 if (pix_y < 0)
1560 pix_y = 0;
1561 else if (pix_y > FRAME_LINES (f))
1562 pix_y = FRAME_LINES (f);
1563 }
1564 }
1565 #endif
1566
1567 *x = pix_x;
1568 *y = pix_y;
1569 }
1570
1571
1572 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1573 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1574 can't tell the positions because W's display is not up to date,
1575 return 0. */
1576
1577 int
1578 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1579 struct window *w;
1580 int hpos, vpos;
1581 int *frame_x, *frame_y;
1582 {
1583 #ifdef HAVE_WINDOW_SYSTEM
1584 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1585 {
1586 int success_p;
1587
1588 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1589 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1590
1591 if (display_completed)
1592 {
1593 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1594 struct glyph *glyph = row->glyphs[TEXT_AREA];
1595 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1596
1597 hpos = row->x;
1598 vpos = row->y;
1599 while (glyph < end)
1600 {
1601 hpos += glyph->pixel_width;
1602 ++glyph;
1603 }
1604
1605 /* If first glyph is partially visible, its first visible position is still 0. */
1606 if (hpos < 0)
1607 hpos = 0;
1608
1609 success_p = 1;
1610 }
1611 else
1612 {
1613 hpos = vpos = 0;
1614 success_p = 0;
1615 }
1616
1617 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1618 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1619 return success_p;
1620 }
1621 #endif
1622
1623 *frame_x = hpos;
1624 *frame_y = vpos;
1625 return 1;
1626 }
1627
1628
1629 #ifdef HAVE_WINDOW_SYSTEM
1630
1631 /* Find the glyph under window-relative coordinates X/Y in window W.
1632 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1633 strings. Return in *HPOS and *VPOS the row and column number of
1634 the glyph found. Return in *AREA the glyph area containing X.
1635 Value is a pointer to the glyph found or null if X/Y is not on
1636 text, or we can't tell because W's current matrix is not up to
1637 date. */
1638
1639 static struct glyph *
1640 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1641 struct window *w;
1642 int x, y;
1643 int *hpos, *vpos, *dx, *dy, *area;
1644 {
1645 struct glyph *glyph, *end;
1646 struct glyph_row *row = NULL;
1647 int x0, i;
1648
1649 /* Find row containing Y. Give up if some row is not enabled. */
1650 for (i = 0; i < w->current_matrix->nrows; ++i)
1651 {
1652 row = MATRIX_ROW (w->current_matrix, i);
1653 if (!row->enabled_p)
1654 return NULL;
1655 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1656 break;
1657 }
1658
1659 *vpos = i;
1660 *hpos = 0;
1661
1662 /* Give up if Y is not in the window. */
1663 if (i == w->current_matrix->nrows)
1664 return NULL;
1665
1666 /* Get the glyph area containing X. */
1667 if (w->pseudo_window_p)
1668 {
1669 *area = TEXT_AREA;
1670 x0 = 0;
1671 }
1672 else
1673 {
1674 if (x < window_box_left_offset (w, TEXT_AREA))
1675 {
1676 *area = LEFT_MARGIN_AREA;
1677 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1678 }
1679 else if (x < window_box_right_offset (w, TEXT_AREA))
1680 {
1681 *area = TEXT_AREA;
1682 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1683 }
1684 else
1685 {
1686 *area = RIGHT_MARGIN_AREA;
1687 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1688 }
1689 }
1690
1691 /* Find glyph containing X. */
1692 glyph = row->glyphs[*area];
1693 end = glyph + row->used[*area];
1694 x -= x0;
1695 while (glyph < end && x >= glyph->pixel_width)
1696 {
1697 x -= glyph->pixel_width;
1698 ++glyph;
1699 }
1700
1701 if (glyph == end)
1702 return NULL;
1703
1704 if (dx)
1705 {
1706 *dx = x;
1707 *dy = y - (row->y + row->ascent - glyph->ascent);
1708 }
1709
1710 *hpos = glyph - row->glyphs[*area];
1711 return glyph;
1712 }
1713
1714
1715 /* EXPORT:
1716 Convert frame-relative x/y to coordinates relative to window W.
1717 Takes pseudo-windows into account. */
1718
1719 void
1720 frame_to_window_pixel_xy (w, x, y)
1721 struct window *w;
1722 int *x, *y;
1723 {
1724 if (w->pseudo_window_p)
1725 {
1726 /* A pseudo-window is always full-width, and starts at the
1727 left edge of the frame, plus a frame border. */
1728 struct frame *f = XFRAME (w->frame);
1729 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1730 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1731 }
1732 else
1733 {
1734 *x -= WINDOW_LEFT_EDGE_X (w);
1735 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1736 }
1737 }
1738
1739 /* EXPORT:
1740 Return in *R the clipping rectangle for glyph string S. */
1741
1742 void
1743 get_glyph_string_clip_rect (s, nr)
1744 struct glyph_string *s;
1745 NativeRectangle *nr;
1746 {
1747 XRectangle r;
1748
1749 if (s->row->full_width_p)
1750 {
1751 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1752 r.x = WINDOW_LEFT_EDGE_X (s->w);
1753 r.width = WINDOW_TOTAL_WIDTH (s->w);
1754
1755 /* Unless displaying a mode or menu bar line, which are always
1756 fully visible, clip to the visible part of the row. */
1757 if (s->w->pseudo_window_p)
1758 r.height = s->row->visible_height;
1759 else
1760 r.height = s->height;
1761 }
1762 else
1763 {
1764 /* This is a text line that may be partially visible. */
1765 r.x = window_box_left (s->w, s->area);
1766 r.width = window_box_width (s->w, s->area);
1767 r.height = s->row->visible_height;
1768 }
1769
1770 /* If S draws overlapping rows, it's sufficient to use the top and
1771 bottom of the window for clipping because this glyph string
1772 intentionally draws over other lines. */
1773 if (s->for_overlaps_p)
1774 {
1775 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1776 r.height = window_text_bottom_y (s->w) - r.y;
1777 }
1778 else
1779 {
1780 /* Don't use S->y for clipping because it doesn't take partially
1781 visible lines into account. For example, it can be negative for
1782 partially visible lines at the top of a window. */
1783 if (!s->row->full_width_p
1784 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1785 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1786 else
1787 r.y = max (0, s->row->y);
1788
1789 /* If drawing a tool-bar window, draw it over the internal border
1790 at the top of the window. */
1791 if (s->w == XWINDOW (s->f->tool_bar_window))
1792 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1793 }
1794
1795 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1796
1797 /* If drawing the cursor, don't let glyph draw outside its
1798 advertised boundaries. Cleartype does this under some circumstances. */
1799 if (s->hl == DRAW_CURSOR)
1800 {
1801 struct glyph *glyph = s->first_glyph;
1802 int height;
1803
1804 if (s->x > r.x)
1805 {
1806 r.width -= s->x - r.x;
1807 r.x = s->x;
1808 }
1809 r.width = min (r.width, glyph->pixel_width);
1810
1811 /* Don't draw cursor glyph taller than our actual glyph. */
1812 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1813 if (height < r.height)
1814 {
1815 int max_y = r.y + r.height;
1816 r.y = min (max_y, s->ybase + glyph->descent - height);
1817 r.height = min (max_y - r.y, height);
1818 }
1819 }
1820
1821 #ifdef CONVERT_FROM_XRECT
1822 CONVERT_FROM_XRECT (r, *nr);
1823 #else
1824 *nr = r;
1825 #endif
1826 }
1827
1828 #endif /* HAVE_WINDOW_SYSTEM */
1829
1830 \f
1831 /***********************************************************************
1832 Lisp form evaluation
1833 ***********************************************************************/
1834
1835 /* Error handler for safe_eval and safe_call. */
1836
1837 static Lisp_Object
1838 safe_eval_handler (arg)
1839 Lisp_Object arg;
1840 {
1841 add_to_log ("Error during redisplay: %s", arg, Qnil);
1842 return Qnil;
1843 }
1844
1845
1846 /* Evaluate SEXPR and return the result, or nil if something went
1847 wrong. Prevent redisplay during the evaluation. */
1848
1849 Lisp_Object
1850 safe_eval (sexpr)
1851 Lisp_Object sexpr;
1852 {
1853 Lisp_Object val;
1854
1855 if (inhibit_eval_during_redisplay)
1856 val = Qnil;
1857 else
1858 {
1859 int count = SPECPDL_INDEX ();
1860 struct gcpro gcpro1;
1861
1862 GCPRO1 (sexpr);
1863 specbind (Qinhibit_redisplay, Qt);
1864 /* Use Qt to ensure debugger does not run,
1865 so there is no possibility of wanting to redisplay. */
1866 val = internal_condition_case_1 (Feval, sexpr, Qt,
1867 safe_eval_handler);
1868 UNGCPRO;
1869 val = unbind_to (count, val);
1870 }
1871
1872 return val;
1873 }
1874
1875
1876 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1877 Return the result, or nil if something went wrong. Prevent
1878 redisplay during the evaluation. */
1879
1880 Lisp_Object
1881 safe_call (nargs, args)
1882 int nargs;
1883 Lisp_Object *args;
1884 {
1885 Lisp_Object val;
1886
1887 if (inhibit_eval_during_redisplay)
1888 val = Qnil;
1889 else
1890 {
1891 int count = SPECPDL_INDEX ();
1892 struct gcpro gcpro1;
1893
1894 GCPRO1 (args[0]);
1895 gcpro1.nvars = nargs;
1896 specbind (Qinhibit_redisplay, Qt);
1897 /* Use Qt to ensure debugger does not run,
1898 so there is no possibility of wanting to redisplay. */
1899 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1900 safe_eval_handler);
1901 UNGCPRO;
1902 val = unbind_to (count, val);
1903 }
1904
1905 return val;
1906 }
1907
1908
1909 /* Call function FN with one argument ARG.
1910 Return the result, or nil if something went wrong. */
1911
1912 Lisp_Object
1913 safe_call1 (fn, arg)
1914 Lisp_Object fn, arg;
1915 {
1916 Lisp_Object args[2];
1917 args[0] = fn;
1918 args[1] = arg;
1919 return safe_call (2, args);
1920 }
1921
1922
1923 \f
1924 /***********************************************************************
1925 Debugging
1926 ***********************************************************************/
1927
1928 #if 0
1929
1930 /* Define CHECK_IT to perform sanity checks on iterators.
1931 This is for debugging. It is too slow to do unconditionally. */
1932
1933 static void
1934 check_it (it)
1935 struct it *it;
1936 {
1937 if (it->method == next_element_from_string)
1938 {
1939 xassert (STRINGP (it->string));
1940 xassert (IT_STRING_CHARPOS (*it) >= 0);
1941 }
1942 else
1943 {
1944 xassert (IT_STRING_CHARPOS (*it) < 0);
1945 if (it->method == next_element_from_buffer)
1946 {
1947 /* Check that character and byte positions agree. */
1948 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1949 }
1950 }
1951
1952 if (it->dpvec)
1953 xassert (it->current.dpvec_index >= 0);
1954 else
1955 xassert (it->current.dpvec_index < 0);
1956 }
1957
1958 #define CHECK_IT(IT) check_it ((IT))
1959
1960 #else /* not 0 */
1961
1962 #define CHECK_IT(IT) (void) 0
1963
1964 #endif /* not 0 */
1965
1966
1967 #if GLYPH_DEBUG
1968
1969 /* Check that the window end of window W is what we expect it
1970 to be---the last row in the current matrix displaying text. */
1971
1972 static void
1973 check_window_end (w)
1974 struct window *w;
1975 {
1976 if (!MINI_WINDOW_P (w)
1977 && !NILP (w->window_end_valid))
1978 {
1979 struct glyph_row *row;
1980 xassert ((row = MATRIX_ROW (w->current_matrix,
1981 XFASTINT (w->window_end_vpos)),
1982 !row->enabled_p
1983 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1984 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1985 }
1986 }
1987
1988 #define CHECK_WINDOW_END(W) check_window_end ((W))
1989
1990 #else /* not GLYPH_DEBUG */
1991
1992 #define CHECK_WINDOW_END(W) (void) 0
1993
1994 #endif /* not GLYPH_DEBUG */
1995
1996
1997 \f
1998 /***********************************************************************
1999 Iterator initialization
2000 ***********************************************************************/
2001
2002 /* Initialize IT for displaying current_buffer in window W, starting
2003 at character position CHARPOS. CHARPOS < 0 means that no buffer
2004 position is specified which is useful when the iterator is assigned
2005 a position later. BYTEPOS is the byte position corresponding to
2006 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2007
2008 If ROW is not null, calls to produce_glyphs with IT as parameter
2009 will produce glyphs in that row.
2010
2011 BASE_FACE_ID is the id of a base face to use. It must be one of
2012 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2013 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2014 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2015
2016 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2017 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2018 will be initialized to use the corresponding mode line glyph row of
2019 the desired matrix of W. */
2020
2021 void
2022 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2023 struct it *it;
2024 struct window *w;
2025 int charpos, bytepos;
2026 struct glyph_row *row;
2027 enum face_id base_face_id;
2028 {
2029 int highlight_region_p;
2030
2031 /* Some precondition checks. */
2032 xassert (w != NULL && it != NULL);
2033 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2034 && charpos <= ZV));
2035
2036 /* If face attributes have been changed since the last redisplay,
2037 free realized faces now because they depend on face definitions
2038 that might have changed. Don't free faces while there might be
2039 desired matrices pending which reference these faces. */
2040 if (face_change_count && !inhibit_free_realized_faces)
2041 {
2042 face_change_count = 0;
2043 free_all_realized_faces (Qnil);
2044 }
2045
2046 /* Use one of the mode line rows of W's desired matrix if
2047 appropriate. */
2048 if (row == NULL)
2049 {
2050 if (base_face_id == MODE_LINE_FACE_ID
2051 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2052 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2053 else if (base_face_id == HEADER_LINE_FACE_ID)
2054 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2055 }
2056
2057 /* Clear IT. */
2058 bzero (it, sizeof *it);
2059 it->current.overlay_string_index = -1;
2060 it->current.dpvec_index = -1;
2061 it->base_face_id = base_face_id;
2062 it->string = Qnil;
2063 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2064
2065 /* The window in which we iterate over current_buffer: */
2066 XSETWINDOW (it->window, w);
2067 it->w = w;
2068 it->f = XFRAME (w->frame);
2069
2070 /* Extra space between lines (on window systems only). */
2071 if (base_face_id == DEFAULT_FACE_ID
2072 && FRAME_WINDOW_P (it->f))
2073 {
2074 if (NATNUMP (current_buffer->extra_line_spacing))
2075 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2076 else if (FLOATP (current_buffer->extra_line_spacing))
2077 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2078 * FRAME_LINE_HEIGHT (it->f));
2079 else if (it->f->extra_line_spacing > 0)
2080 it->extra_line_spacing = it->f->extra_line_spacing;
2081 }
2082
2083 /* If realized faces have been removed, e.g. because of face
2084 attribute changes of named faces, recompute them. When running
2085 in batch mode, the face cache of Vterminal_frame is null. If
2086 we happen to get called, make a dummy face cache. */
2087 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2088 init_frame_faces (it->f);
2089 if (FRAME_FACE_CACHE (it->f)->used == 0)
2090 recompute_basic_faces (it->f);
2091
2092 /* Current value of the `slice', `space-width', and 'height' properties. */
2093 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2094 it->space_width = Qnil;
2095 it->font_height = Qnil;
2096 it->override_ascent = -1;
2097
2098 /* Are control characters displayed as `^C'? */
2099 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2100
2101 /* -1 means everything between a CR and the following line end
2102 is invisible. >0 means lines indented more than this value are
2103 invisible. */
2104 it->selective = (INTEGERP (current_buffer->selective_display)
2105 ? XFASTINT (current_buffer->selective_display)
2106 : (!NILP (current_buffer->selective_display)
2107 ? -1 : 0));
2108 it->selective_display_ellipsis_p
2109 = !NILP (current_buffer->selective_display_ellipses);
2110
2111 /* Display table to use. */
2112 it->dp = window_display_table (w);
2113
2114 /* Are multibyte characters enabled in current_buffer? */
2115 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2116
2117 /* Non-zero if we should highlight the region. */
2118 highlight_region_p
2119 = (!NILP (Vtransient_mark_mode)
2120 && !NILP (current_buffer->mark_active)
2121 && XMARKER (current_buffer->mark)->buffer != 0);
2122
2123 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2124 start and end of a visible region in window IT->w. Set both to
2125 -1 to indicate no region. */
2126 if (highlight_region_p
2127 /* Maybe highlight only in selected window. */
2128 && (/* Either show region everywhere. */
2129 highlight_nonselected_windows
2130 /* Or show region in the selected window. */
2131 || w == XWINDOW (selected_window)
2132 /* Or show the region if we are in the mini-buffer and W is
2133 the window the mini-buffer refers to. */
2134 || (MINI_WINDOW_P (XWINDOW (selected_window))
2135 && WINDOWP (minibuf_selected_window)
2136 && w == XWINDOW (minibuf_selected_window))))
2137 {
2138 int charpos = marker_position (current_buffer->mark);
2139 it->region_beg_charpos = min (PT, charpos);
2140 it->region_end_charpos = max (PT, charpos);
2141 }
2142 else
2143 it->region_beg_charpos = it->region_end_charpos = -1;
2144
2145 /* Get the position at which the redisplay_end_trigger hook should
2146 be run, if it is to be run at all. */
2147 if (MARKERP (w->redisplay_end_trigger)
2148 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2149 it->redisplay_end_trigger_charpos
2150 = marker_position (w->redisplay_end_trigger);
2151 else if (INTEGERP (w->redisplay_end_trigger))
2152 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2153
2154 /* Correct bogus values of tab_width. */
2155 it->tab_width = XINT (current_buffer->tab_width);
2156 if (it->tab_width <= 0 || it->tab_width > 1000)
2157 it->tab_width = 8;
2158
2159 /* Are lines in the display truncated? */
2160 it->truncate_lines_p
2161 = (base_face_id != DEFAULT_FACE_ID
2162 || XINT (it->w->hscroll)
2163 || (truncate_partial_width_windows
2164 && !WINDOW_FULL_WIDTH_P (it->w))
2165 || !NILP (current_buffer->truncate_lines));
2166
2167 /* Get dimensions of truncation and continuation glyphs. These are
2168 displayed as fringe bitmaps under X, so we don't need them for such
2169 frames. */
2170 if (!FRAME_WINDOW_P (it->f))
2171 {
2172 if (it->truncate_lines_p)
2173 {
2174 /* We will need the truncation glyph. */
2175 xassert (it->glyph_row == NULL);
2176 produce_special_glyphs (it, IT_TRUNCATION);
2177 it->truncation_pixel_width = it->pixel_width;
2178 }
2179 else
2180 {
2181 /* We will need the continuation glyph. */
2182 xassert (it->glyph_row == NULL);
2183 produce_special_glyphs (it, IT_CONTINUATION);
2184 it->continuation_pixel_width = it->pixel_width;
2185 }
2186
2187 /* Reset these values to zero because the produce_special_glyphs
2188 above has changed them. */
2189 it->pixel_width = it->ascent = it->descent = 0;
2190 it->phys_ascent = it->phys_descent = 0;
2191 }
2192
2193 /* Set this after getting the dimensions of truncation and
2194 continuation glyphs, so that we don't produce glyphs when calling
2195 produce_special_glyphs, above. */
2196 it->glyph_row = row;
2197 it->area = TEXT_AREA;
2198
2199 /* Get the dimensions of the display area. The display area
2200 consists of the visible window area plus a horizontally scrolled
2201 part to the left of the window. All x-values are relative to the
2202 start of this total display area. */
2203 if (base_face_id != DEFAULT_FACE_ID)
2204 {
2205 /* Mode lines, menu bar in terminal frames. */
2206 it->first_visible_x = 0;
2207 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2208 }
2209 else
2210 {
2211 it->first_visible_x
2212 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2213 it->last_visible_x = (it->first_visible_x
2214 + window_box_width (w, TEXT_AREA));
2215
2216 /* If we truncate lines, leave room for the truncator glyph(s) at
2217 the right margin. Otherwise, leave room for the continuation
2218 glyph(s). Truncation and continuation glyphs are not inserted
2219 for window-based redisplay. */
2220 if (!FRAME_WINDOW_P (it->f))
2221 {
2222 if (it->truncate_lines_p)
2223 it->last_visible_x -= it->truncation_pixel_width;
2224 else
2225 it->last_visible_x -= it->continuation_pixel_width;
2226 }
2227
2228 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2229 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2230 }
2231
2232 /* Leave room for a border glyph. */
2233 if (!FRAME_WINDOW_P (it->f)
2234 && !WINDOW_RIGHTMOST_P (it->w))
2235 it->last_visible_x -= 1;
2236
2237 it->last_visible_y = window_text_bottom_y (w);
2238
2239 /* For mode lines and alike, arrange for the first glyph having a
2240 left box line if the face specifies a box. */
2241 if (base_face_id != DEFAULT_FACE_ID)
2242 {
2243 struct face *face;
2244
2245 it->face_id = base_face_id;
2246
2247 /* If we have a boxed mode line, make the first character appear
2248 with a left box line. */
2249 face = FACE_FROM_ID (it->f, base_face_id);
2250 if (face->box != FACE_NO_BOX)
2251 it->start_of_box_run_p = 1;
2252 }
2253
2254 /* If a buffer position was specified, set the iterator there,
2255 getting overlays and face properties from that position. */
2256 if (charpos >= BUF_BEG (current_buffer))
2257 {
2258 it->end_charpos = ZV;
2259 it->face_id = -1;
2260 IT_CHARPOS (*it) = charpos;
2261
2262 /* Compute byte position if not specified. */
2263 if (bytepos < charpos)
2264 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2265 else
2266 IT_BYTEPOS (*it) = bytepos;
2267
2268 it->start = it->current;
2269
2270 /* Compute faces etc. */
2271 reseat (it, it->current.pos, 1);
2272 }
2273
2274 CHECK_IT (it);
2275 }
2276
2277
2278 /* Initialize IT for the display of window W with window start POS. */
2279
2280 void
2281 start_display (it, w, pos)
2282 struct it *it;
2283 struct window *w;
2284 struct text_pos pos;
2285 {
2286 struct glyph_row *row;
2287 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2288
2289 row = w->desired_matrix->rows + first_vpos;
2290 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2291 it->first_vpos = first_vpos;
2292
2293 if (!it->truncate_lines_p)
2294 {
2295 int start_at_line_beg_p;
2296 int first_y = it->current_y;
2297
2298 /* If window start is not at a line start, skip forward to POS to
2299 get the correct continuation lines width. */
2300 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2301 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2302 if (!start_at_line_beg_p)
2303 {
2304 int new_x;
2305
2306 reseat_at_previous_visible_line_start (it);
2307 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2308
2309 new_x = it->current_x + it->pixel_width;
2310
2311 /* If lines are continued, this line may end in the middle
2312 of a multi-glyph character (e.g. a control character
2313 displayed as \003, or in the middle of an overlay
2314 string). In this case move_it_to above will not have
2315 taken us to the start of the continuation line but to the
2316 end of the continued line. */
2317 if (it->current_x > 0
2318 && !it->truncate_lines_p /* Lines are continued. */
2319 && (/* And glyph doesn't fit on the line. */
2320 new_x > it->last_visible_x
2321 /* Or it fits exactly and we're on a window
2322 system frame. */
2323 || (new_x == it->last_visible_x
2324 && FRAME_WINDOW_P (it->f))))
2325 {
2326 if (it->current.dpvec_index >= 0
2327 || it->current.overlay_string_index >= 0)
2328 {
2329 set_iterator_to_next (it, 1);
2330 move_it_in_display_line_to (it, -1, -1, 0);
2331 }
2332
2333 it->continuation_lines_width += it->current_x;
2334 }
2335
2336 /* We're starting a new display line, not affected by the
2337 height of the continued line, so clear the appropriate
2338 fields in the iterator structure. */
2339 it->max_ascent = it->max_descent = 0;
2340 it->max_phys_ascent = it->max_phys_descent = 0;
2341
2342 it->current_y = first_y;
2343 it->vpos = 0;
2344 it->current_x = it->hpos = 0;
2345 }
2346 }
2347
2348 #if 0 /* Don't assert the following because start_display is sometimes
2349 called intentionally with a window start that is not at a
2350 line start. Please leave this code in as a comment. */
2351
2352 /* Window start should be on a line start, now. */
2353 xassert (it->continuation_lines_width
2354 || IT_CHARPOS (it) == BEGV
2355 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2356 #endif /* 0 */
2357 }
2358
2359
2360 /* Return 1 if POS is a position in ellipses displayed for invisible
2361 text. W is the window we display, for text property lookup. */
2362
2363 static int
2364 in_ellipses_for_invisible_text_p (pos, w)
2365 struct display_pos *pos;
2366 struct window *w;
2367 {
2368 Lisp_Object prop, window;
2369 int ellipses_p = 0;
2370 int charpos = CHARPOS (pos->pos);
2371
2372 /* If POS specifies a position in a display vector, this might
2373 be for an ellipsis displayed for invisible text. We won't
2374 get the iterator set up for delivering that ellipsis unless
2375 we make sure that it gets aware of the invisible text. */
2376 if (pos->dpvec_index >= 0
2377 && pos->overlay_string_index < 0
2378 && CHARPOS (pos->string_pos) < 0
2379 && charpos > BEGV
2380 && (XSETWINDOW (window, w),
2381 prop = Fget_char_property (make_number (charpos),
2382 Qinvisible, window),
2383 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2384 {
2385 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2386 window);
2387 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2388 }
2389
2390 return ellipses_p;
2391 }
2392
2393
2394 /* Initialize IT for stepping through current_buffer in window W,
2395 starting at position POS that includes overlay string and display
2396 vector/ control character translation position information. Value
2397 is zero if there are overlay strings with newlines at POS. */
2398
2399 static int
2400 init_from_display_pos (it, w, pos)
2401 struct it *it;
2402 struct window *w;
2403 struct display_pos *pos;
2404 {
2405 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2406 int i, overlay_strings_with_newlines = 0;
2407
2408 /* If POS specifies a position in a display vector, this might
2409 be for an ellipsis displayed for invisible text. We won't
2410 get the iterator set up for delivering that ellipsis unless
2411 we make sure that it gets aware of the invisible text. */
2412 if (in_ellipses_for_invisible_text_p (pos, w))
2413 {
2414 --charpos;
2415 bytepos = 0;
2416 }
2417
2418 /* Keep in mind: the call to reseat in init_iterator skips invisible
2419 text, so we might end up at a position different from POS. This
2420 is only a problem when POS is a row start after a newline and an
2421 overlay starts there with an after-string, and the overlay has an
2422 invisible property. Since we don't skip invisible text in
2423 display_line and elsewhere immediately after consuming the
2424 newline before the row start, such a POS will not be in a string,
2425 but the call to init_iterator below will move us to the
2426 after-string. */
2427 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2428
2429 for (i = 0; i < it->n_overlay_strings; ++i)
2430 {
2431 const char *s = SDATA (it->overlay_strings[i]);
2432 const char *e = s + SBYTES (it->overlay_strings[i]);
2433
2434 while (s < e && *s != '\n')
2435 ++s;
2436
2437 if (s < e)
2438 {
2439 overlay_strings_with_newlines = 1;
2440 break;
2441 }
2442 }
2443
2444 /* If position is within an overlay string, set up IT to the right
2445 overlay string. */
2446 if (pos->overlay_string_index >= 0)
2447 {
2448 int relative_index;
2449
2450 /* If the first overlay string happens to have a `display'
2451 property for an image, the iterator will be set up for that
2452 image, and we have to undo that setup first before we can
2453 correct the overlay string index. */
2454 if (it->method == next_element_from_image)
2455 pop_it (it);
2456
2457 /* We already have the first chunk of overlay strings in
2458 IT->overlay_strings. Load more until the one for
2459 pos->overlay_string_index is in IT->overlay_strings. */
2460 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2461 {
2462 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2463 it->current.overlay_string_index = 0;
2464 while (n--)
2465 {
2466 load_overlay_strings (it, 0);
2467 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2468 }
2469 }
2470
2471 it->current.overlay_string_index = pos->overlay_string_index;
2472 relative_index = (it->current.overlay_string_index
2473 % OVERLAY_STRING_CHUNK_SIZE);
2474 it->string = it->overlay_strings[relative_index];
2475 xassert (STRINGP (it->string));
2476 it->current.string_pos = pos->string_pos;
2477 it->method = next_element_from_string;
2478 }
2479
2480 #if 0 /* This is bogus because POS not having an overlay string
2481 position does not mean it's after the string. Example: A
2482 line starting with a before-string and initialization of IT
2483 to the previous row's end position. */
2484 else if (it->current.overlay_string_index >= 0)
2485 {
2486 /* If POS says we're already after an overlay string ending at
2487 POS, make sure to pop the iterator because it will be in
2488 front of that overlay string. When POS is ZV, we've thereby
2489 also ``processed'' overlay strings at ZV. */
2490 while (it->sp)
2491 pop_it (it);
2492 it->current.overlay_string_index = -1;
2493 it->method = next_element_from_buffer;
2494 if (CHARPOS (pos->pos) == ZV)
2495 it->overlay_strings_at_end_processed_p = 1;
2496 }
2497 #endif /* 0 */
2498
2499 if (CHARPOS (pos->string_pos) >= 0)
2500 {
2501 /* Recorded position is not in an overlay string, but in another
2502 string. This can only be a string from a `display' property.
2503 IT should already be filled with that string. */
2504 it->current.string_pos = pos->string_pos;
2505 xassert (STRINGP (it->string));
2506 }
2507
2508 /* Restore position in display vector translations, control
2509 character translations or ellipses. */
2510 if (pos->dpvec_index >= 0)
2511 {
2512 if (it->dpvec == NULL)
2513 get_next_display_element (it);
2514 xassert (it->dpvec && it->current.dpvec_index == 0);
2515 it->current.dpvec_index = pos->dpvec_index;
2516 }
2517
2518 CHECK_IT (it);
2519 return !overlay_strings_with_newlines;
2520 }
2521
2522
2523 /* Initialize IT for stepping through current_buffer in window W
2524 starting at ROW->start. */
2525
2526 static void
2527 init_to_row_start (it, w, row)
2528 struct it *it;
2529 struct window *w;
2530 struct glyph_row *row;
2531 {
2532 init_from_display_pos (it, w, &row->start);
2533 it->start = row->start;
2534 it->continuation_lines_width = row->continuation_lines_width;
2535 CHECK_IT (it);
2536 }
2537
2538
2539 /* Initialize IT for stepping through current_buffer in window W
2540 starting in the line following ROW, i.e. starting at ROW->end.
2541 Value is zero if there are overlay strings with newlines at ROW's
2542 end position. */
2543
2544 static int
2545 init_to_row_end (it, w, row)
2546 struct it *it;
2547 struct window *w;
2548 struct glyph_row *row;
2549 {
2550 int success = 0;
2551
2552 if (init_from_display_pos (it, w, &row->end))
2553 {
2554 if (row->continued_p)
2555 it->continuation_lines_width
2556 = row->continuation_lines_width + row->pixel_width;
2557 CHECK_IT (it);
2558 success = 1;
2559 }
2560
2561 return success;
2562 }
2563
2564
2565
2566 \f
2567 /***********************************************************************
2568 Text properties
2569 ***********************************************************************/
2570
2571 /* Called when IT reaches IT->stop_charpos. Handle text property and
2572 overlay changes. Set IT->stop_charpos to the next position where
2573 to stop. */
2574
2575 static void
2576 handle_stop (it)
2577 struct it *it;
2578 {
2579 enum prop_handled handled;
2580 int handle_overlay_change_p = 1;
2581 struct props *p;
2582
2583 it->dpvec = NULL;
2584 it->current.dpvec_index = -1;
2585
2586 do
2587 {
2588 handled = HANDLED_NORMALLY;
2589
2590 /* Call text property handlers. */
2591 for (p = it_props; p->handler; ++p)
2592 {
2593 handled = p->handler (it);
2594
2595 if (handled == HANDLED_RECOMPUTE_PROPS)
2596 break;
2597 else if (handled == HANDLED_RETURN)
2598 return;
2599 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2600 handle_overlay_change_p = 0;
2601 }
2602
2603 if (handled != HANDLED_RECOMPUTE_PROPS)
2604 {
2605 /* Don't check for overlay strings below when set to deliver
2606 characters from a display vector. */
2607 if (it->method == next_element_from_display_vector)
2608 handle_overlay_change_p = 0;
2609
2610 /* Handle overlay changes. */
2611 if (handle_overlay_change_p)
2612 handled = handle_overlay_change (it);
2613
2614 /* Determine where to stop next. */
2615 if (handled == HANDLED_NORMALLY)
2616 compute_stop_pos (it);
2617 }
2618 }
2619 while (handled == HANDLED_RECOMPUTE_PROPS);
2620 }
2621
2622
2623 /* Compute IT->stop_charpos from text property and overlay change
2624 information for IT's current position. */
2625
2626 static void
2627 compute_stop_pos (it)
2628 struct it *it;
2629 {
2630 register INTERVAL iv, next_iv;
2631 Lisp_Object object, limit, position;
2632
2633 /* If nowhere else, stop at the end. */
2634 it->stop_charpos = it->end_charpos;
2635
2636 if (STRINGP (it->string))
2637 {
2638 /* Strings are usually short, so don't limit the search for
2639 properties. */
2640 object = it->string;
2641 limit = Qnil;
2642 position = make_number (IT_STRING_CHARPOS (*it));
2643 }
2644 else
2645 {
2646 int charpos;
2647
2648 /* If next overlay change is in front of the current stop pos
2649 (which is IT->end_charpos), stop there. Note: value of
2650 next_overlay_change is point-max if no overlay change
2651 follows. */
2652 charpos = next_overlay_change (IT_CHARPOS (*it));
2653 if (charpos < it->stop_charpos)
2654 it->stop_charpos = charpos;
2655
2656 /* If showing the region, we have to stop at the region
2657 start or end because the face might change there. */
2658 if (it->region_beg_charpos > 0)
2659 {
2660 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2661 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2662 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2663 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2664 }
2665
2666 /* Set up variables for computing the stop position from text
2667 property changes. */
2668 XSETBUFFER (object, current_buffer);
2669 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2670 position = make_number (IT_CHARPOS (*it));
2671
2672 }
2673
2674 /* Get the interval containing IT's position. Value is a null
2675 interval if there isn't such an interval. */
2676 iv = validate_interval_range (object, &position, &position, 0);
2677 if (!NULL_INTERVAL_P (iv))
2678 {
2679 Lisp_Object values_here[LAST_PROP_IDX];
2680 struct props *p;
2681
2682 /* Get properties here. */
2683 for (p = it_props; p->handler; ++p)
2684 values_here[p->idx] = textget (iv->plist, *p->name);
2685
2686 /* Look for an interval following iv that has different
2687 properties. */
2688 for (next_iv = next_interval (iv);
2689 (!NULL_INTERVAL_P (next_iv)
2690 && (NILP (limit)
2691 || XFASTINT (limit) > next_iv->position));
2692 next_iv = next_interval (next_iv))
2693 {
2694 for (p = it_props; p->handler; ++p)
2695 {
2696 Lisp_Object new_value;
2697
2698 new_value = textget (next_iv->plist, *p->name);
2699 if (!EQ (values_here[p->idx], new_value))
2700 break;
2701 }
2702
2703 if (p->handler)
2704 break;
2705 }
2706
2707 if (!NULL_INTERVAL_P (next_iv))
2708 {
2709 if (INTEGERP (limit)
2710 && next_iv->position >= XFASTINT (limit))
2711 /* No text property change up to limit. */
2712 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2713 else
2714 /* Text properties change in next_iv. */
2715 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2716 }
2717 }
2718
2719 xassert (STRINGP (it->string)
2720 || (it->stop_charpos >= BEGV
2721 && it->stop_charpos >= IT_CHARPOS (*it)));
2722 }
2723
2724
2725 /* Return the position of the next overlay change after POS in
2726 current_buffer. Value is point-max if no overlay change
2727 follows. This is like `next-overlay-change' but doesn't use
2728 xmalloc. */
2729
2730 static int
2731 next_overlay_change (pos)
2732 int pos;
2733 {
2734 int noverlays;
2735 int endpos;
2736 Lisp_Object *overlays;
2737 int len;
2738 int i;
2739
2740 /* Get all overlays at the given position. */
2741 len = 10;
2742 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2743 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2744 if (noverlays > len)
2745 {
2746 len = noverlays;
2747 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2748 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2749 }
2750
2751 /* If any of these overlays ends before endpos,
2752 use its ending point instead. */
2753 for (i = 0; i < noverlays; ++i)
2754 {
2755 Lisp_Object oend;
2756 int oendpos;
2757
2758 oend = OVERLAY_END (overlays[i]);
2759 oendpos = OVERLAY_POSITION (oend);
2760 endpos = min (endpos, oendpos);
2761 }
2762
2763 return endpos;
2764 }
2765
2766
2767 \f
2768 /***********************************************************************
2769 Fontification
2770 ***********************************************************************/
2771
2772 /* Handle changes in the `fontified' property of the current buffer by
2773 calling hook functions from Qfontification_functions to fontify
2774 regions of text. */
2775
2776 static enum prop_handled
2777 handle_fontified_prop (it)
2778 struct it *it;
2779 {
2780 Lisp_Object prop, pos;
2781 enum prop_handled handled = HANDLED_NORMALLY;
2782
2783 /* Get the value of the `fontified' property at IT's current buffer
2784 position. (The `fontified' property doesn't have a special
2785 meaning in strings.) If the value is nil, call functions from
2786 Qfontification_functions. */
2787 if (!STRINGP (it->string)
2788 && it->s == NULL
2789 && !NILP (Vfontification_functions)
2790 && !NILP (Vrun_hooks)
2791 && (pos = make_number (IT_CHARPOS (*it)),
2792 prop = Fget_char_property (pos, Qfontified, Qnil),
2793 NILP (prop)))
2794 {
2795 int count = SPECPDL_INDEX ();
2796 Lisp_Object val;
2797
2798 val = Vfontification_functions;
2799 specbind (Qfontification_functions, Qnil);
2800
2801 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2802 safe_call1 (val, pos);
2803 else
2804 {
2805 Lisp_Object globals, fn;
2806 struct gcpro gcpro1, gcpro2;
2807
2808 globals = Qnil;
2809 GCPRO2 (val, globals);
2810
2811 for (; CONSP (val); val = XCDR (val))
2812 {
2813 fn = XCAR (val);
2814
2815 if (EQ (fn, Qt))
2816 {
2817 /* A value of t indicates this hook has a local
2818 binding; it means to run the global binding too.
2819 In a global value, t should not occur. If it
2820 does, we must ignore it to avoid an endless
2821 loop. */
2822 for (globals = Fdefault_value (Qfontification_functions);
2823 CONSP (globals);
2824 globals = XCDR (globals))
2825 {
2826 fn = XCAR (globals);
2827 if (!EQ (fn, Qt))
2828 safe_call1 (fn, pos);
2829 }
2830 }
2831 else
2832 safe_call1 (fn, pos);
2833 }
2834
2835 UNGCPRO;
2836 }
2837
2838 unbind_to (count, Qnil);
2839
2840 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2841 something. This avoids an endless loop if they failed to
2842 fontify the text for which reason ever. */
2843 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2844 handled = HANDLED_RECOMPUTE_PROPS;
2845 }
2846
2847 return handled;
2848 }
2849
2850
2851 \f
2852 /***********************************************************************
2853 Faces
2854 ***********************************************************************/
2855
2856 /* Set up iterator IT from face properties at its current position.
2857 Called from handle_stop. */
2858
2859 static enum prop_handled
2860 handle_face_prop (it)
2861 struct it *it;
2862 {
2863 int new_face_id, next_stop;
2864
2865 if (!STRINGP (it->string))
2866 {
2867 new_face_id
2868 = face_at_buffer_position (it->w,
2869 IT_CHARPOS (*it),
2870 it->region_beg_charpos,
2871 it->region_end_charpos,
2872 &next_stop,
2873 (IT_CHARPOS (*it)
2874 + TEXT_PROP_DISTANCE_LIMIT),
2875 0);
2876
2877 /* Is this a start of a run of characters with box face?
2878 Caveat: this can be called for a freshly initialized
2879 iterator; face_id is -1 in this case. We know that the new
2880 face will not change until limit, i.e. if the new face has a
2881 box, all characters up to limit will have one. But, as
2882 usual, we don't know whether limit is really the end. */
2883 if (new_face_id != it->face_id)
2884 {
2885 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2886
2887 /* If new face has a box but old face has not, this is
2888 the start of a run of characters with box, i.e. it has
2889 a shadow on the left side. The value of face_id of the
2890 iterator will be -1 if this is the initial call that gets
2891 the face. In this case, we have to look in front of IT's
2892 position and see whether there is a face != new_face_id. */
2893 it->start_of_box_run_p
2894 = (new_face->box != FACE_NO_BOX
2895 && (it->face_id >= 0
2896 || IT_CHARPOS (*it) == BEG
2897 || new_face_id != face_before_it_pos (it)));
2898 it->face_box_p = new_face->box != FACE_NO_BOX;
2899 }
2900 }
2901 else
2902 {
2903 int base_face_id, bufpos;
2904
2905 if (it->current.overlay_string_index >= 0)
2906 bufpos = IT_CHARPOS (*it);
2907 else
2908 bufpos = 0;
2909
2910 /* For strings from a buffer, i.e. overlay strings or strings
2911 from a `display' property, use the face at IT's current
2912 buffer position as the base face to merge with, so that
2913 overlay strings appear in the same face as surrounding
2914 text, unless they specify their own faces. */
2915 base_face_id = underlying_face_id (it);
2916
2917 new_face_id = face_at_string_position (it->w,
2918 it->string,
2919 IT_STRING_CHARPOS (*it),
2920 bufpos,
2921 it->region_beg_charpos,
2922 it->region_end_charpos,
2923 &next_stop,
2924 base_face_id, 0);
2925
2926 #if 0 /* This shouldn't be neccessary. Let's check it. */
2927 /* If IT is used to display a mode line we would really like to
2928 use the mode line face instead of the frame's default face. */
2929 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2930 && new_face_id == DEFAULT_FACE_ID)
2931 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2932 #endif
2933
2934 /* Is this a start of a run of characters with box? Caveat:
2935 this can be called for a freshly allocated iterator; face_id
2936 is -1 is this case. We know that the new face will not
2937 change until the next check pos, i.e. if the new face has a
2938 box, all characters up to that position will have a
2939 box. But, as usual, we don't know whether that position
2940 is really the end. */
2941 if (new_face_id != it->face_id)
2942 {
2943 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2944 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2945
2946 /* If new face has a box but old face hasn't, this is the
2947 start of a run of characters with box, i.e. it has a
2948 shadow on the left side. */
2949 it->start_of_box_run_p
2950 = new_face->box && (old_face == NULL || !old_face->box);
2951 it->face_box_p = new_face->box != FACE_NO_BOX;
2952 }
2953 }
2954
2955 it->face_id = new_face_id;
2956 return HANDLED_NORMALLY;
2957 }
2958
2959
2960 /* Return the ID of the face ``underlying'' IT's current position,
2961 which is in a string. If the iterator is associated with a
2962 buffer, return the face at IT's current buffer position.
2963 Otherwise, use the iterator's base_face_id. */
2964
2965 static int
2966 underlying_face_id (it)
2967 struct it *it;
2968 {
2969 int face_id = it->base_face_id, i;
2970
2971 xassert (STRINGP (it->string));
2972
2973 for (i = it->sp - 1; i >= 0; --i)
2974 if (NILP (it->stack[i].string))
2975 face_id = it->stack[i].face_id;
2976
2977 return face_id;
2978 }
2979
2980
2981 /* Compute the face one character before or after the current position
2982 of IT. BEFORE_P non-zero means get the face in front of IT's
2983 position. Value is the id of the face. */
2984
2985 static int
2986 face_before_or_after_it_pos (it, before_p)
2987 struct it *it;
2988 int before_p;
2989 {
2990 int face_id, limit;
2991 int next_check_charpos;
2992 struct text_pos pos;
2993
2994 xassert (it->s == NULL);
2995
2996 if (STRINGP (it->string))
2997 {
2998 int bufpos, base_face_id;
2999
3000 /* No face change past the end of the string (for the case
3001 we are padding with spaces). No face change before the
3002 string start. */
3003 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3004 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3005 return it->face_id;
3006
3007 /* Set pos to the position before or after IT's current position. */
3008 if (before_p)
3009 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3010 else
3011 /* For composition, we must check the character after the
3012 composition. */
3013 pos = (it->what == IT_COMPOSITION
3014 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3015 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3016
3017 if (it->current.overlay_string_index >= 0)
3018 bufpos = IT_CHARPOS (*it);
3019 else
3020 bufpos = 0;
3021
3022 base_face_id = underlying_face_id (it);
3023
3024 /* Get the face for ASCII, or unibyte. */
3025 face_id = face_at_string_position (it->w,
3026 it->string,
3027 CHARPOS (pos),
3028 bufpos,
3029 it->region_beg_charpos,
3030 it->region_end_charpos,
3031 &next_check_charpos,
3032 base_face_id, 0);
3033
3034 /* Correct the face for charsets different from ASCII. Do it
3035 for the multibyte case only. The face returned above is
3036 suitable for unibyte text if IT->string is unibyte. */
3037 if (STRING_MULTIBYTE (it->string))
3038 {
3039 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3040 int rest = SBYTES (it->string) - BYTEPOS (pos);
3041 int c, len;
3042 struct face *face = FACE_FROM_ID (it->f, face_id);
3043
3044 c = string_char_and_length (p, rest, &len);
3045 face_id = FACE_FOR_CHAR (it->f, face, c);
3046 }
3047 }
3048 else
3049 {
3050 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3051 || (IT_CHARPOS (*it) <= BEGV && before_p))
3052 return it->face_id;
3053
3054 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3055 pos = it->current.pos;
3056
3057 if (before_p)
3058 DEC_TEXT_POS (pos, it->multibyte_p);
3059 else
3060 {
3061 if (it->what == IT_COMPOSITION)
3062 /* For composition, we must check the position after the
3063 composition. */
3064 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3065 else
3066 INC_TEXT_POS (pos, it->multibyte_p);
3067 }
3068
3069 /* Determine face for CHARSET_ASCII, or unibyte. */
3070 face_id = face_at_buffer_position (it->w,
3071 CHARPOS (pos),
3072 it->region_beg_charpos,
3073 it->region_end_charpos,
3074 &next_check_charpos,
3075 limit, 0);
3076
3077 /* Correct the face for charsets different from ASCII. Do it
3078 for the multibyte case only. The face returned above is
3079 suitable for unibyte text if current_buffer is unibyte. */
3080 if (it->multibyte_p)
3081 {
3082 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3083 struct face *face = FACE_FROM_ID (it->f, face_id);
3084 face_id = FACE_FOR_CHAR (it->f, face, c);
3085 }
3086 }
3087
3088 return face_id;
3089 }
3090
3091
3092 \f
3093 /***********************************************************************
3094 Invisible text
3095 ***********************************************************************/
3096
3097 /* Set up iterator IT from invisible properties at its current
3098 position. Called from handle_stop. */
3099
3100 static enum prop_handled
3101 handle_invisible_prop (it)
3102 struct it *it;
3103 {
3104 enum prop_handled handled = HANDLED_NORMALLY;
3105
3106 if (STRINGP (it->string))
3107 {
3108 extern Lisp_Object Qinvisible;
3109 Lisp_Object prop, end_charpos, limit, charpos;
3110
3111 /* Get the value of the invisible text property at the
3112 current position. Value will be nil if there is no such
3113 property. */
3114 charpos = make_number (IT_STRING_CHARPOS (*it));
3115 prop = Fget_text_property (charpos, Qinvisible, it->string);
3116
3117 if (!NILP (prop)
3118 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3119 {
3120 handled = HANDLED_RECOMPUTE_PROPS;
3121
3122 /* Get the position at which the next change of the
3123 invisible text property can be found in IT->string.
3124 Value will be nil if the property value is the same for
3125 all the rest of IT->string. */
3126 XSETINT (limit, SCHARS (it->string));
3127 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3128 it->string, limit);
3129
3130 /* Text at current position is invisible. The next
3131 change in the property is at position end_charpos.
3132 Move IT's current position to that position. */
3133 if (INTEGERP (end_charpos)
3134 && XFASTINT (end_charpos) < XFASTINT (limit))
3135 {
3136 struct text_pos old;
3137 old = it->current.string_pos;
3138 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3139 compute_string_pos (&it->current.string_pos, old, it->string);
3140 }
3141 else
3142 {
3143 /* The rest of the string is invisible. If this is an
3144 overlay string, proceed with the next overlay string
3145 or whatever comes and return a character from there. */
3146 if (it->current.overlay_string_index >= 0)
3147 {
3148 next_overlay_string (it);
3149 /* Don't check for overlay strings when we just
3150 finished processing them. */
3151 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3152 }
3153 else
3154 {
3155 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3156 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3157 }
3158 }
3159 }
3160 }
3161 else
3162 {
3163 int invis_p, newpos, next_stop, start_charpos;
3164 Lisp_Object pos, prop, overlay;
3165
3166 /* First of all, is there invisible text at this position? */
3167 start_charpos = IT_CHARPOS (*it);
3168 pos = make_number (IT_CHARPOS (*it));
3169 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3170 &overlay);
3171 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3172
3173 /* If we are on invisible text, skip over it. */
3174 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3175 {
3176 /* Record whether we have to display an ellipsis for the
3177 invisible text. */
3178 int display_ellipsis_p = invis_p == 2;
3179
3180 handled = HANDLED_RECOMPUTE_PROPS;
3181
3182 /* Loop skipping over invisible text. The loop is left at
3183 ZV or with IT on the first char being visible again. */
3184 do
3185 {
3186 /* Try to skip some invisible text. Return value is the
3187 position reached which can be equal to IT's position
3188 if there is nothing invisible here. This skips both
3189 over invisible text properties and overlays with
3190 invisible property. */
3191 newpos = skip_invisible (IT_CHARPOS (*it),
3192 &next_stop, ZV, it->window);
3193
3194 /* If we skipped nothing at all we weren't at invisible
3195 text in the first place. If everything to the end of
3196 the buffer was skipped, end the loop. */
3197 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3198 invis_p = 0;
3199 else
3200 {
3201 /* We skipped some characters but not necessarily
3202 all there are. Check if we ended up on visible
3203 text. Fget_char_property returns the property of
3204 the char before the given position, i.e. if we
3205 get invis_p = 0, this means that the char at
3206 newpos is visible. */
3207 pos = make_number (newpos);
3208 prop = Fget_char_property (pos, Qinvisible, it->window);
3209 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3210 }
3211
3212 /* If we ended up on invisible text, proceed to
3213 skip starting with next_stop. */
3214 if (invis_p)
3215 IT_CHARPOS (*it) = next_stop;
3216 }
3217 while (invis_p);
3218
3219 /* The position newpos is now either ZV or on visible text. */
3220 IT_CHARPOS (*it) = newpos;
3221 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3222
3223 /* If there are before-strings at the start of invisible
3224 text, and the text is invisible because of a text
3225 property, arrange to show before-strings because 20.x did
3226 it that way. (If the text is invisible because of an
3227 overlay property instead of a text property, this is
3228 already handled in the overlay code.) */
3229 if (NILP (overlay)
3230 && get_overlay_strings (it, start_charpos))
3231 {
3232 handled = HANDLED_RECOMPUTE_PROPS;
3233 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3234 }
3235 else if (display_ellipsis_p)
3236 setup_for_ellipsis (it);
3237 }
3238 }
3239
3240 return handled;
3241 }
3242
3243
3244 /* Make iterator IT return `...' next. */
3245
3246 static void
3247 setup_for_ellipsis (it)
3248 struct it *it;
3249 {
3250 if (it->dp
3251 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3252 {
3253 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3254 it->dpvec = v->contents;
3255 it->dpend = v->contents + v->size;
3256 }
3257 else
3258 {
3259 /* Default `...'. */
3260 it->dpvec = default_invis_vector;
3261 it->dpend = default_invis_vector + 3;
3262 }
3263
3264 /* The ellipsis display does not replace the display of the
3265 character at the new position. Indicate this by setting
3266 IT->dpvec_char_len to zero. */
3267 it->dpvec_char_len = 0;
3268
3269 it->current.dpvec_index = 0;
3270 it->method = next_element_from_display_vector;
3271 }
3272
3273
3274 \f
3275 /***********************************************************************
3276 'display' property
3277 ***********************************************************************/
3278
3279 /* Set up iterator IT from `display' property at its current position.
3280 Called from handle_stop. */
3281
3282 static enum prop_handled
3283 handle_display_prop (it)
3284 struct it *it;
3285 {
3286 Lisp_Object prop, object;
3287 struct text_pos *position;
3288 int display_replaced_p = 0;
3289
3290 if (STRINGP (it->string))
3291 {
3292 object = it->string;
3293 position = &it->current.string_pos;
3294 }
3295 else
3296 {
3297 object = it->w->buffer;
3298 position = &it->current.pos;
3299 }
3300
3301 /* Reset those iterator values set from display property values. */
3302 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3303 it->space_width = Qnil;
3304 it->font_height = Qnil;
3305 it->voffset = 0;
3306
3307 /* We don't support recursive `display' properties, i.e. string
3308 values that have a string `display' property, that have a string
3309 `display' property etc. */
3310 if (!it->string_from_display_prop_p)
3311 it->area = TEXT_AREA;
3312
3313 prop = Fget_char_property (make_number (position->charpos),
3314 Qdisplay, object);
3315 if (NILP (prop))
3316 return HANDLED_NORMALLY;
3317
3318 if (CONSP (prop)
3319 /* Simple properties. */
3320 && !EQ (XCAR (prop), Qimage)
3321 && !EQ (XCAR (prop), Qspace)
3322 && !EQ (XCAR (prop), Qwhen)
3323 && !EQ (XCAR (prop), Qslice)
3324 && !EQ (XCAR (prop), Qspace_width)
3325 && !EQ (XCAR (prop), Qheight)
3326 && !EQ (XCAR (prop), Qraise)
3327 /* Marginal area specifications. */
3328 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3329 && !EQ (XCAR (prop), Qleft_fringe)
3330 && !EQ (XCAR (prop), Qright_fringe)
3331 && !NILP (XCAR (prop)))
3332 {
3333 for (; CONSP (prop); prop = XCDR (prop))
3334 {
3335 if (handle_single_display_prop (it, XCAR (prop), object,
3336 position, display_replaced_p))
3337 display_replaced_p = 1;
3338 }
3339 }
3340 else if (VECTORP (prop))
3341 {
3342 int i;
3343 for (i = 0; i < ASIZE (prop); ++i)
3344 if (handle_single_display_prop (it, AREF (prop, i), object,
3345 position, display_replaced_p))
3346 display_replaced_p = 1;
3347 }
3348 else
3349 {
3350 if (handle_single_display_prop (it, prop, object, position, 0))
3351 display_replaced_p = 1;
3352 }
3353
3354 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3355 }
3356
3357
3358 /* Value is the position of the end of the `display' property starting
3359 at START_POS in OBJECT. */
3360
3361 static struct text_pos
3362 display_prop_end (it, object, start_pos)
3363 struct it *it;
3364 Lisp_Object object;
3365 struct text_pos start_pos;
3366 {
3367 Lisp_Object end;
3368 struct text_pos end_pos;
3369
3370 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3371 Qdisplay, object, Qnil);
3372 CHARPOS (end_pos) = XFASTINT (end);
3373 if (STRINGP (object))
3374 compute_string_pos (&end_pos, start_pos, it->string);
3375 else
3376 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3377
3378 return end_pos;
3379 }
3380
3381
3382 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3383 is the object in which the `display' property was found. *POSITION
3384 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3385 means that we previously saw a display sub-property which already
3386 replaced text display with something else, for example an image;
3387 ignore such properties after the first one has been processed.
3388
3389 If PROP is a `space' or `image' sub-property, set *POSITION to the
3390 end position of the `display' property.
3391
3392 Value is non-zero if something was found which replaces the display
3393 of buffer or string text. */
3394
3395 static int
3396 handle_single_display_prop (it, prop, object, position,
3397 display_replaced_before_p)
3398 struct it *it;
3399 Lisp_Object prop;
3400 Lisp_Object object;
3401 struct text_pos *position;
3402 int display_replaced_before_p;
3403 {
3404 Lisp_Object value;
3405 int replaces_text_display_p = 0;
3406 Lisp_Object form;
3407
3408 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3409 evaluated. If the result is nil, VALUE is ignored. */
3410 form = Qt;
3411 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3412 {
3413 prop = XCDR (prop);
3414 if (!CONSP (prop))
3415 return 0;
3416 form = XCAR (prop);
3417 prop = XCDR (prop);
3418 }
3419
3420 if (!NILP (form) && !EQ (form, Qt))
3421 {
3422 int count = SPECPDL_INDEX ();
3423 struct gcpro gcpro1;
3424
3425 /* Bind `object' to the object having the `display' property, a
3426 buffer or string. Bind `position' to the position in the
3427 object where the property was found, and `buffer-position'
3428 to the current position in the buffer. */
3429 specbind (Qobject, object);
3430 specbind (Qposition, make_number (CHARPOS (*position)));
3431 specbind (Qbuffer_position,
3432 make_number (STRINGP (object)
3433 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3434 GCPRO1 (form);
3435 form = safe_eval (form);
3436 UNGCPRO;
3437 unbind_to (count, Qnil);
3438 }
3439
3440 if (NILP (form))
3441 return 0;
3442
3443 if (CONSP (prop)
3444 && EQ (XCAR (prop), Qheight)
3445 && CONSP (XCDR (prop)))
3446 {
3447 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3448 return 0;
3449
3450 /* `(height HEIGHT)'. */
3451 it->font_height = XCAR (XCDR (prop));
3452 if (!NILP (it->font_height))
3453 {
3454 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3455 int new_height = -1;
3456
3457 if (CONSP (it->font_height)
3458 && (EQ (XCAR (it->font_height), Qplus)
3459 || EQ (XCAR (it->font_height), Qminus))
3460 && CONSP (XCDR (it->font_height))
3461 && INTEGERP (XCAR (XCDR (it->font_height))))
3462 {
3463 /* `(+ N)' or `(- N)' where N is an integer. */
3464 int steps = XINT (XCAR (XCDR (it->font_height)));
3465 if (EQ (XCAR (it->font_height), Qplus))
3466 steps = - steps;
3467 it->face_id = smaller_face (it->f, it->face_id, steps);
3468 }
3469 else if (FUNCTIONP (it->font_height))
3470 {
3471 /* Call function with current height as argument.
3472 Value is the new height. */
3473 Lisp_Object height;
3474 height = safe_call1 (it->font_height,
3475 face->lface[LFACE_HEIGHT_INDEX]);
3476 if (NUMBERP (height))
3477 new_height = XFLOATINT (height);
3478 }
3479 else if (NUMBERP (it->font_height))
3480 {
3481 /* Value is a multiple of the canonical char height. */
3482 struct face *face;
3483
3484 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3485 new_height = (XFLOATINT (it->font_height)
3486 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3487 }
3488 else
3489 {
3490 /* Evaluate IT->font_height with `height' bound to the
3491 current specified height to get the new height. */
3492 Lisp_Object value;
3493 int count = SPECPDL_INDEX ();
3494
3495 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3496 value = safe_eval (it->font_height);
3497 unbind_to (count, Qnil);
3498
3499 if (NUMBERP (value))
3500 new_height = XFLOATINT (value);
3501 }
3502
3503 if (new_height > 0)
3504 it->face_id = face_with_height (it->f, it->face_id, new_height);
3505 }
3506 }
3507 else if (CONSP (prop)
3508 && EQ (XCAR (prop), Qspace_width)
3509 && CONSP (XCDR (prop)))
3510 {
3511 /* `(space_width WIDTH)'. */
3512 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3513 return 0;
3514
3515 value = XCAR (XCDR (prop));
3516 if (NUMBERP (value) && XFLOATINT (value) > 0)
3517 it->space_width = value;
3518 }
3519 else if (CONSP (prop)
3520 && EQ (XCAR (prop), Qslice))
3521 {
3522 /* `(slice X Y WIDTH HEIGHT)'. */
3523 Lisp_Object tem;
3524
3525 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3526 return 0;
3527
3528 if (tem = XCDR (prop), CONSP (tem))
3529 {
3530 it->slice.x = XCAR (tem);
3531 if (tem = XCDR (tem), CONSP (tem))
3532 {
3533 it->slice.y = XCAR (tem);
3534 if (tem = XCDR (tem), CONSP (tem))
3535 {
3536 it->slice.width = XCAR (tem);
3537 if (tem = XCDR (tem), CONSP (tem))
3538 it->slice.height = XCAR (tem);
3539 }
3540 }
3541 }
3542 }
3543 else if (CONSP (prop)
3544 && EQ (XCAR (prop), Qraise)
3545 && CONSP (XCDR (prop)))
3546 {
3547 /* `(raise FACTOR)'. */
3548 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3549 return 0;
3550
3551 #ifdef HAVE_WINDOW_SYSTEM
3552 value = XCAR (XCDR (prop));
3553 if (NUMBERP (value))
3554 {
3555 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3556 it->voffset = - (XFLOATINT (value)
3557 * (FONT_HEIGHT (face->font)));
3558 }
3559 #endif /* HAVE_WINDOW_SYSTEM */
3560 }
3561 else if (!it->string_from_display_prop_p)
3562 {
3563 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3564 VALUE) or `((margin nil) VALUE)' or VALUE. */
3565 Lisp_Object location, value;
3566 struct text_pos start_pos;
3567 int valid_p;
3568
3569 /* Characters having this form of property are not displayed, so
3570 we have to find the end of the property. */
3571 start_pos = *position;
3572 *position = display_prop_end (it, object, start_pos);
3573 value = Qnil;
3574
3575 /* Let's stop at the new position and assume that all
3576 text properties change there. */
3577 it->stop_charpos = position->charpos;
3578
3579 if (CONSP (prop)
3580 && (EQ (XCAR (prop), Qleft_fringe)
3581 || EQ (XCAR (prop), Qright_fringe))
3582 && CONSP (XCDR (prop)))
3583 {
3584 unsigned face_id = DEFAULT_FACE_ID;
3585
3586 /* Save current settings of IT so that we can restore them
3587 when we are finished with the glyph property value. */
3588
3589 /* `(left-fringe BITMAP FACE)'. */
3590 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3591 return 0;
3592
3593 #ifdef HAVE_WINDOW_SYSTEM
3594 value = XCAR (XCDR (prop));
3595 if (!NUMBERP (value)
3596 || !valid_fringe_bitmap_id_p (XINT (value)))
3597 return 0;
3598
3599 if (CONSP (XCDR (XCDR (prop))))
3600 {
3601 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3602
3603 face_id = lookup_named_face (it->f, face_name, 'A');
3604 if (face_id < 0)
3605 return 0;
3606 }
3607
3608 push_it (it);
3609
3610 it->area = TEXT_AREA;
3611 it->what = IT_IMAGE;
3612 it->image_id = -1; /* no image */
3613 it->position = start_pos;
3614 it->object = NILP (object) ? it->w->buffer : object;
3615 it->method = next_element_from_image;
3616 it->face_id = face_id;
3617
3618 /* Say that we haven't consumed the characters with
3619 `display' property yet. The call to pop_it in
3620 set_iterator_to_next will clean this up. */
3621 *position = start_pos;
3622
3623 if (EQ (XCAR (prop), Qleft_fringe))
3624 {
3625 it->left_user_fringe_bitmap = XINT (value);
3626 it->left_user_fringe_face_id = face_id;
3627 }
3628 else
3629 {
3630 it->right_user_fringe_bitmap = XINT (value);
3631 it->right_user_fringe_face_id = face_id;
3632 }
3633 #endif /* HAVE_WINDOW_SYSTEM */
3634 return 1;
3635 }
3636
3637 location = Qunbound;
3638 if (CONSP (prop) && CONSP (XCAR (prop)))
3639 {
3640 Lisp_Object tem;
3641
3642 value = XCDR (prop);
3643 if (CONSP (value))
3644 value = XCAR (value);
3645
3646 tem = XCAR (prop);
3647 if (EQ (XCAR (tem), Qmargin)
3648 && (tem = XCDR (tem),
3649 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3650 (NILP (tem)
3651 || EQ (tem, Qleft_margin)
3652 || EQ (tem, Qright_margin))))
3653 location = tem;
3654 }
3655
3656 if (EQ (location, Qunbound))
3657 {
3658 location = Qnil;
3659 value = prop;
3660 }
3661
3662 valid_p = (STRINGP (value)
3663 #ifdef HAVE_WINDOW_SYSTEM
3664 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3665 #endif /* not HAVE_WINDOW_SYSTEM */
3666 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3667
3668 if ((EQ (location, Qleft_margin)
3669 || EQ (location, Qright_margin)
3670 || NILP (location))
3671 && valid_p
3672 && !display_replaced_before_p)
3673 {
3674 replaces_text_display_p = 1;
3675
3676 /* Save current settings of IT so that we can restore them
3677 when we are finished with the glyph property value. */
3678 push_it (it);
3679
3680 if (NILP (location))
3681 it->area = TEXT_AREA;
3682 else if (EQ (location, Qleft_margin))
3683 it->area = LEFT_MARGIN_AREA;
3684 else
3685 it->area = RIGHT_MARGIN_AREA;
3686
3687 if (STRINGP (value))
3688 {
3689 it->string = value;
3690 it->multibyte_p = STRING_MULTIBYTE (it->string);
3691 it->current.overlay_string_index = -1;
3692 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3693 it->end_charpos = it->string_nchars = SCHARS (it->string);
3694 it->method = next_element_from_string;
3695 it->stop_charpos = 0;
3696 it->string_from_display_prop_p = 1;
3697 /* Say that we haven't consumed the characters with
3698 `display' property yet. The call to pop_it in
3699 set_iterator_to_next will clean this up. */
3700 *position = start_pos;
3701 }
3702 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3703 {
3704 it->method = next_element_from_stretch;
3705 it->object = value;
3706 it->current.pos = it->position = start_pos;
3707 }
3708 #ifdef HAVE_WINDOW_SYSTEM
3709 else
3710 {
3711 it->what = IT_IMAGE;
3712 it->image_id = lookup_image (it->f, value);
3713 it->position = start_pos;
3714 it->object = NILP (object) ? it->w->buffer : object;
3715 it->method = next_element_from_image;
3716
3717 /* Say that we haven't consumed the characters with
3718 `display' property yet. The call to pop_it in
3719 set_iterator_to_next will clean this up. */
3720 *position = start_pos;
3721 }
3722 #endif /* HAVE_WINDOW_SYSTEM */
3723 }
3724 else
3725 /* Invalid property or property not supported. Restore
3726 the position to what it was before. */
3727 *position = start_pos;
3728 }
3729
3730 return replaces_text_display_p;
3731 }
3732
3733
3734 /* Check if PROP is a display sub-property value whose text should be
3735 treated as intangible. */
3736
3737 static int
3738 single_display_prop_intangible_p (prop)
3739 Lisp_Object prop;
3740 {
3741 /* Skip over `when FORM'. */
3742 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3743 {
3744 prop = XCDR (prop);
3745 if (!CONSP (prop))
3746 return 0;
3747 prop = XCDR (prop);
3748 }
3749
3750 if (STRINGP (prop))
3751 return 1;
3752
3753 if (!CONSP (prop))
3754 return 0;
3755
3756 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3757 we don't need to treat text as intangible. */
3758 if (EQ (XCAR (prop), Qmargin))
3759 {
3760 prop = XCDR (prop);
3761 if (!CONSP (prop))
3762 return 0;
3763
3764 prop = XCDR (prop);
3765 if (!CONSP (prop)
3766 || EQ (XCAR (prop), Qleft_margin)
3767 || EQ (XCAR (prop), Qright_margin))
3768 return 0;
3769 }
3770
3771 return (CONSP (prop)
3772 && (EQ (XCAR (prop), Qimage)
3773 || EQ (XCAR (prop), Qspace)));
3774 }
3775
3776
3777 /* Check if PROP is a display property value whose text should be
3778 treated as intangible. */
3779
3780 int
3781 display_prop_intangible_p (prop)
3782 Lisp_Object prop;
3783 {
3784 if (CONSP (prop)
3785 && CONSP (XCAR (prop))
3786 && !EQ (Qmargin, XCAR (XCAR (prop))))
3787 {
3788 /* A list of sub-properties. */
3789 while (CONSP (prop))
3790 {
3791 if (single_display_prop_intangible_p (XCAR (prop)))
3792 return 1;
3793 prop = XCDR (prop);
3794 }
3795 }
3796 else if (VECTORP (prop))
3797 {
3798 /* A vector of sub-properties. */
3799 int i;
3800 for (i = 0; i < ASIZE (prop); ++i)
3801 if (single_display_prop_intangible_p (AREF (prop, i)))
3802 return 1;
3803 }
3804 else
3805 return single_display_prop_intangible_p (prop);
3806
3807 return 0;
3808 }
3809
3810
3811 /* Return 1 if PROP is a display sub-property value containing STRING. */
3812
3813 static int
3814 single_display_prop_string_p (prop, string)
3815 Lisp_Object prop, string;
3816 {
3817 if (EQ (string, prop))
3818 return 1;
3819
3820 /* Skip over `when FORM'. */
3821 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3822 {
3823 prop = XCDR (prop);
3824 if (!CONSP (prop))
3825 return 0;
3826 prop = XCDR (prop);
3827 }
3828
3829 if (CONSP (prop))
3830 /* Skip over `margin LOCATION'. */
3831 if (EQ (XCAR (prop), Qmargin))
3832 {
3833 prop = XCDR (prop);
3834 if (!CONSP (prop))
3835 return 0;
3836
3837 prop = XCDR (prop);
3838 if (!CONSP (prop))
3839 return 0;
3840 }
3841
3842 return CONSP (prop) && EQ (XCAR (prop), string);
3843 }
3844
3845
3846 /* Return 1 if STRING appears in the `display' property PROP. */
3847
3848 static int
3849 display_prop_string_p (prop, string)
3850 Lisp_Object prop, string;
3851 {
3852 if (CONSP (prop)
3853 && CONSP (XCAR (prop))
3854 && !EQ (Qmargin, XCAR (XCAR (prop))))
3855 {
3856 /* A list of sub-properties. */
3857 while (CONSP (prop))
3858 {
3859 if (single_display_prop_string_p (XCAR (prop), string))
3860 return 1;
3861 prop = XCDR (prop);
3862 }
3863 }
3864 else if (VECTORP (prop))
3865 {
3866 /* A vector of sub-properties. */
3867 int i;
3868 for (i = 0; i < ASIZE (prop); ++i)
3869 if (single_display_prop_string_p (AREF (prop, i), string))
3870 return 1;
3871 }
3872 else
3873 return single_display_prop_string_p (prop, string);
3874
3875 return 0;
3876 }
3877
3878
3879 /* Determine from which buffer position in W's buffer STRING comes
3880 from. AROUND_CHARPOS is an approximate position where it could
3881 be from. Value is the buffer position or 0 if it couldn't be
3882 determined.
3883
3884 W's buffer must be current.
3885
3886 This function is necessary because we don't record buffer positions
3887 in glyphs generated from strings (to keep struct glyph small).
3888 This function may only use code that doesn't eval because it is
3889 called asynchronously from note_mouse_highlight. */
3890
3891 int
3892 string_buffer_position (w, string, around_charpos)
3893 struct window *w;
3894 Lisp_Object string;
3895 int around_charpos;
3896 {
3897 Lisp_Object limit, prop, pos;
3898 const int MAX_DISTANCE = 1000;
3899 int found = 0;
3900
3901 pos = make_number (around_charpos);
3902 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3903 while (!found && !EQ (pos, limit))
3904 {
3905 prop = Fget_char_property (pos, Qdisplay, Qnil);
3906 if (!NILP (prop) && display_prop_string_p (prop, string))
3907 found = 1;
3908 else
3909 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3910 }
3911
3912 if (!found)
3913 {
3914 pos = make_number (around_charpos);
3915 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3916 while (!found && !EQ (pos, limit))
3917 {
3918 prop = Fget_char_property (pos, Qdisplay, Qnil);
3919 if (!NILP (prop) && display_prop_string_p (prop, string))
3920 found = 1;
3921 else
3922 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3923 limit);
3924 }
3925 }
3926
3927 return found ? XINT (pos) : 0;
3928 }
3929
3930
3931 \f
3932 /***********************************************************************
3933 `composition' property
3934 ***********************************************************************/
3935
3936 /* Set up iterator IT from `composition' property at its current
3937 position. Called from handle_stop. */
3938
3939 static enum prop_handled
3940 handle_composition_prop (it)
3941 struct it *it;
3942 {
3943 Lisp_Object prop, string;
3944 int pos, pos_byte, end;
3945 enum prop_handled handled = HANDLED_NORMALLY;
3946
3947 if (STRINGP (it->string))
3948 {
3949 pos = IT_STRING_CHARPOS (*it);
3950 pos_byte = IT_STRING_BYTEPOS (*it);
3951 string = it->string;
3952 }
3953 else
3954 {
3955 pos = IT_CHARPOS (*it);
3956 pos_byte = IT_BYTEPOS (*it);
3957 string = Qnil;
3958 }
3959
3960 /* If there's a valid composition and point is not inside of the
3961 composition (in the case that the composition is from the current
3962 buffer), draw a glyph composed from the composition components. */
3963 if (find_composition (pos, -1, &pos, &end, &prop, string)
3964 && COMPOSITION_VALID_P (pos, end, prop)
3965 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3966 {
3967 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3968
3969 if (id >= 0)
3970 {
3971 it->method = next_element_from_composition;
3972 it->cmp_id = id;
3973 it->cmp_len = COMPOSITION_LENGTH (prop);
3974 /* For a terminal, draw only the first character of the
3975 components. */
3976 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3977 it->len = (STRINGP (it->string)
3978 ? string_char_to_byte (it->string, end)
3979 : CHAR_TO_BYTE (end)) - pos_byte;
3980 it->stop_charpos = end;
3981 handled = HANDLED_RETURN;
3982 }
3983 }
3984
3985 return handled;
3986 }
3987
3988
3989 \f
3990 /***********************************************************************
3991 Overlay strings
3992 ***********************************************************************/
3993
3994 /* The following structure is used to record overlay strings for
3995 later sorting in load_overlay_strings. */
3996
3997 struct overlay_entry
3998 {
3999 Lisp_Object overlay;
4000 Lisp_Object string;
4001 int priority;
4002 int after_string_p;
4003 };
4004
4005
4006 /* Set up iterator IT from overlay strings at its current position.
4007 Called from handle_stop. */
4008
4009 static enum prop_handled
4010 handle_overlay_change (it)
4011 struct it *it;
4012 {
4013 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4014 return HANDLED_RECOMPUTE_PROPS;
4015 else
4016 return HANDLED_NORMALLY;
4017 }
4018
4019
4020 /* Set up the next overlay string for delivery by IT, if there is an
4021 overlay string to deliver. Called by set_iterator_to_next when the
4022 end of the current overlay string is reached. If there are more
4023 overlay strings to display, IT->string and
4024 IT->current.overlay_string_index are set appropriately here.
4025 Otherwise IT->string is set to nil. */
4026
4027 static void
4028 next_overlay_string (it)
4029 struct it *it;
4030 {
4031 ++it->current.overlay_string_index;
4032 if (it->current.overlay_string_index == it->n_overlay_strings)
4033 {
4034 /* No more overlay strings. Restore IT's settings to what
4035 they were before overlay strings were processed, and
4036 continue to deliver from current_buffer. */
4037 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4038
4039 pop_it (it);
4040 xassert (it->stop_charpos >= BEGV
4041 && it->stop_charpos <= it->end_charpos);
4042 it->string = Qnil;
4043 it->current.overlay_string_index = -1;
4044 SET_TEXT_POS (it->current.string_pos, -1, -1);
4045 it->n_overlay_strings = 0;
4046 it->method = next_element_from_buffer;
4047
4048 /* If we're at the end of the buffer, record that we have
4049 processed the overlay strings there already, so that
4050 next_element_from_buffer doesn't try it again. */
4051 if (IT_CHARPOS (*it) >= it->end_charpos)
4052 it->overlay_strings_at_end_processed_p = 1;
4053
4054 /* If we have to display `...' for invisible text, set
4055 the iterator up for that. */
4056 if (display_ellipsis_p)
4057 setup_for_ellipsis (it);
4058 }
4059 else
4060 {
4061 /* There are more overlay strings to process. If
4062 IT->current.overlay_string_index has advanced to a position
4063 where we must load IT->overlay_strings with more strings, do
4064 it. */
4065 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4066
4067 if (it->current.overlay_string_index && i == 0)
4068 load_overlay_strings (it, 0);
4069
4070 /* Initialize IT to deliver display elements from the overlay
4071 string. */
4072 it->string = it->overlay_strings[i];
4073 it->multibyte_p = STRING_MULTIBYTE (it->string);
4074 SET_TEXT_POS (it->current.string_pos, 0, 0);
4075 it->method = next_element_from_string;
4076 it->stop_charpos = 0;
4077 }
4078
4079 CHECK_IT (it);
4080 }
4081
4082
4083 /* Compare two overlay_entry structures E1 and E2. Used as a
4084 comparison function for qsort in load_overlay_strings. Overlay
4085 strings for the same position are sorted so that
4086
4087 1. All after-strings come in front of before-strings, except
4088 when they come from the same overlay.
4089
4090 2. Within after-strings, strings are sorted so that overlay strings
4091 from overlays with higher priorities come first.
4092
4093 2. Within before-strings, strings are sorted so that overlay
4094 strings from overlays with higher priorities come last.
4095
4096 Value is analogous to strcmp. */
4097
4098
4099 static int
4100 compare_overlay_entries (e1, e2)
4101 void *e1, *e2;
4102 {
4103 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4104 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4105 int result;
4106
4107 if (entry1->after_string_p != entry2->after_string_p)
4108 {
4109 /* Let after-strings appear in front of before-strings if
4110 they come from different overlays. */
4111 if (EQ (entry1->overlay, entry2->overlay))
4112 result = entry1->after_string_p ? 1 : -1;
4113 else
4114 result = entry1->after_string_p ? -1 : 1;
4115 }
4116 else if (entry1->after_string_p)
4117 /* After-strings sorted in order of decreasing priority. */
4118 result = entry2->priority - entry1->priority;
4119 else
4120 /* Before-strings sorted in order of increasing priority. */
4121 result = entry1->priority - entry2->priority;
4122
4123 return result;
4124 }
4125
4126
4127 /* Load the vector IT->overlay_strings with overlay strings from IT's
4128 current buffer position, or from CHARPOS if that is > 0. Set
4129 IT->n_overlays to the total number of overlay strings found.
4130
4131 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4132 a time. On entry into load_overlay_strings,
4133 IT->current.overlay_string_index gives the number of overlay
4134 strings that have already been loaded by previous calls to this
4135 function.
4136
4137 IT->add_overlay_start contains an additional overlay start
4138 position to consider for taking overlay strings from, if non-zero.
4139 This position comes into play when the overlay has an `invisible'
4140 property, and both before and after-strings. When we've skipped to
4141 the end of the overlay, because of its `invisible' property, we
4142 nevertheless want its before-string to appear.
4143 IT->add_overlay_start will contain the overlay start position
4144 in this case.
4145
4146 Overlay strings are sorted so that after-string strings come in
4147 front of before-string strings. Within before and after-strings,
4148 strings are sorted by overlay priority. See also function
4149 compare_overlay_entries. */
4150
4151 static void
4152 load_overlay_strings (it, charpos)
4153 struct it *it;
4154 int charpos;
4155 {
4156 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4157 Lisp_Object overlay, window, str, invisible;
4158 struct Lisp_Overlay *ov;
4159 int start, end;
4160 int size = 20;
4161 int n = 0, i, j, invis_p;
4162 struct overlay_entry *entries
4163 = (struct overlay_entry *) alloca (size * sizeof *entries);
4164
4165 if (charpos <= 0)
4166 charpos = IT_CHARPOS (*it);
4167
4168 /* Append the overlay string STRING of overlay OVERLAY to vector
4169 `entries' which has size `size' and currently contains `n'
4170 elements. AFTER_P non-zero means STRING is an after-string of
4171 OVERLAY. */
4172 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4173 do \
4174 { \
4175 Lisp_Object priority; \
4176 \
4177 if (n == size) \
4178 { \
4179 int new_size = 2 * size; \
4180 struct overlay_entry *old = entries; \
4181 entries = \
4182 (struct overlay_entry *) alloca (new_size \
4183 * sizeof *entries); \
4184 bcopy (old, entries, size * sizeof *entries); \
4185 size = new_size; \
4186 } \
4187 \
4188 entries[n].string = (STRING); \
4189 entries[n].overlay = (OVERLAY); \
4190 priority = Foverlay_get ((OVERLAY), Qpriority); \
4191 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4192 entries[n].after_string_p = (AFTER_P); \
4193 ++n; \
4194 } \
4195 while (0)
4196
4197 /* Process overlay before the overlay center. */
4198 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4199 {
4200 XSETMISC (overlay, ov);
4201 xassert (OVERLAYP (overlay));
4202 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4203 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4204
4205 if (end < charpos)
4206 break;
4207
4208 /* Skip this overlay if it doesn't start or end at IT's current
4209 position. */
4210 if (end != charpos && start != charpos)
4211 continue;
4212
4213 /* Skip this overlay if it doesn't apply to IT->w. */
4214 window = Foverlay_get (overlay, Qwindow);
4215 if (WINDOWP (window) && XWINDOW (window) != it->w)
4216 continue;
4217
4218 /* If the text ``under'' the overlay is invisible, both before-
4219 and after-strings from this overlay are visible; start and
4220 end position are indistinguishable. */
4221 invisible = Foverlay_get (overlay, Qinvisible);
4222 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4223
4224 /* If overlay has a non-empty before-string, record it. */
4225 if ((start == charpos || (end == charpos && invis_p))
4226 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4227 && SCHARS (str))
4228 RECORD_OVERLAY_STRING (overlay, str, 0);
4229
4230 /* If overlay has a non-empty after-string, record it. */
4231 if ((end == charpos || (start == charpos && invis_p))
4232 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4233 && SCHARS (str))
4234 RECORD_OVERLAY_STRING (overlay, str, 1);
4235 }
4236
4237 /* Process overlays after the overlay center. */
4238 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4239 {
4240 XSETMISC (overlay, ov);
4241 xassert (OVERLAYP (overlay));
4242 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4243 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4244
4245 if (start > charpos)
4246 break;
4247
4248 /* Skip this overlay if it doesn't start or end at IT's current
4249 position. */
4250 if (end != charpos && start != charpos)
4251 continue;
4252
4253 /* Skip this overlay if it doesn't apply to IT->w. */
4254 window = Foverlay_get (overlay, Qwindow);
4255 if (WINDOWP (window) && XWINDOW (window) != it->w)
4256 continue;
4257
4258 /* If the text ``under'' the overlay is invisible, it has a zero
4259 dimension, and both before- and after-strings apply. */
4260 invisible = Foverlay_get (overlay, Qinvisible);
4261 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4262
4263 /* If overlay has a non-empty before-string, record it. */
4264 if ((start == charpos || (end == charpos && invis_p))
4265 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4266 && SCHARS (str))
4267 RECORD_OVERLAY_STRING (overlay, str, 0);
4268
4269 /* If overlay has a non-empty after-string, record it. */
4270 if ((end == charpos || (start == charpos && invis_p))
4271 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4272 && SCHARS (str))
4273 RECORD_OVERLAY_STRING (overlay, str, 1);
4274 }
4275
4276 #undef RECORD_OVERLAY_STRING
4277
4278 /* Sort entries. */
4279 if (n > 1)
4280 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4281
4282 /* Record the total number of strings to process. */
4283 it->n_overlay_strings = n;
4284
4285 /* IT->current.overlay_string_index is the number of overlay strings
4286 that have already been consumed by IT. Copy some of the
4287 remaining overlay strings to IT->overlay_strings. */
4288 i = 0;
4289 j = it->current.overlay_string_index;
4290 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4291 it->overlay_strings[i++] = entries[j++].string;
4292
4293 CHECK_IT (it);
4294 }
4295
4296
4297 /* Get the first chunk of overlay strings at IT's current buffer
4298 position, or at CHARPOS if that is > 0. Value is non-zero if at
4299 least one overlay string was found. */
4300
4301 static int
4302 get_overlay_strings (it, charpos)
4303 struct it *it;
4304 int charpos;
4305 {
4306 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4307 process. This fills IT->overlay_strings with strings, and sets
4308 IT->n_overlay_strings to the total number of strings to process.
4309 IT->pos.overlay_string_index has to be set temporarily to zero
4310 because load_overlay_strings needs this; it must be set to -1
4311 when no overlay strings are found because a zero value would
4312 indicate a position in the first overlay string. */
4313 it->current.overlay_string_index = 0;
4314 load_overlay_strings (it, charpos);
4315
4316 /* If we found overlay strings, set up IT to deliver display
4317 elements from the first one. Otherwise set up IT to deliver
4318 from current_buffer. */
4319 if (it->n_overlay_strings)
4320 {
4321 /* Make sure we know settings in current_buffer, so that we can
4322 restore meaningful values when we're done with the overlay
4323 strings. */
4324 compute_stop_pos (it);
4325 xassert (it->face_id >= 0);
4326
4327 /* Save IT's settings. They are restored after all overlay
4328 strings have been processed. */
4329 xassert (it->sp == 0);
4330 push_it (it);
4331
4332 /* Set up IT to deliver display elements from the first overlay
4333 string. */
4334 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4335 it->string = it->overlay_strings[0];
4336 it->stop_charpos = 0;
4337 xassert (STRINGP (it->string));
4338 it->end_charpos = SCHARS (it->string);
4339 it->multibyte_p = STRING_MULTIBYTE (it->string);
4340 it->method = next_element_from_string;
4341 }
4342 else
4343 {
4344 it->string = Qnil;
4345 it->current.overlay_string_index = -1;
4346 it->method = next_element_from_buffer;
4347 }
4348
4349 CHECK_IT (it);
4350
4351 /* Value is non-zero if we found at least one overlay string. */
4352 return STRINGP (it->string);
4353 }
4354
4355
4356 \f
4357 /***********************************************************************
4358 Saving and restoring state
4359 ***********************************************************************/
4360
4361 /* Save current settings of IT on IT->stack. Called, for example,
4362 before setting up IT for an overlay string, to be able to restore
4363 IT's settings to what they were after the overlay string has been
4364 processed. */
4365
4366 static void
4367 push_it (it)
4368 struct it *it;
4369 {
4370 struct iterator_stack_entry *p;
4371
4372 xassert (it->sp < 2);
4373 p = it->stack + it->sp;
4374
4375 p->stop_charpos = it->stop_charpos;
4376 xassert (it->face_id >= 0);
4377 p->face_id = it->face_id;
4378 p->string = it->string;
4379 p->pos = it->current;
4380 p->end_charpos = it->end_charpos;
4381 p->string_nchars = it->string_nchars;
4382 p->area = it->area;
4383 p->multibyte_p = it->multibyte_p;
4384 p->slice = it->slice;
4385 p->space_width = it->space_width;
4386 p->font_height = it->font_height;
4387 p->voffset = it->voffset;
4388 p->string_from_display_prop_p = it->string_from_display_prop_p;
4389 p->display_ellipsis_p = 0;
4390 ++it->sp;
4391 }
4392
4393
4394 /* Restore IT's settings from IT->stack. Called, for example, when no
4395 more overlay strings must be processed, and we return to delivering
4396 display elements from a buffer, or when the end of a string from a
4397 `display' property is reached and we return to delivering display
4398 elements from an overlay string, or from a buffer. */
4399
4400 static void
4401 pop_it (it)
4402 struct it *it;
4403 {
4404 struct iterator_stack_entry *p;
4405
4406 xassert (it->sp > 0);
4407 --it->sp;
4408 p = it->stack + it->sp;
4409 it->stop_charpos = p->stop_charpos;
4410 it->face_id = p->face_id;
4411 it->string = p->string;
4412 it->current = p->pos;
4413 it->end_charpos = p->end_charpos;
4414 it->string_nchars = p->string_nchars;
4415 it->area = p->area;
4416 it->multibyte_p = p->multibyte_p;
4417 it->slice = p->slice;
4418 it->space_width = p->space_width;
4419 it->font_height = p->font_height;
4420 it->voffset = p->voffset;
4421 it->string_from_display_prop_p = p->string_from_display_prop_p;
4422 }
4423
4424
4425 \f
4426 /***********************************************************************
4427 Moving over lines
4428 ***********************************************************************/
4429
4430 /* Set IT's current position to the previous line start. */
4431
4432 static void
4433 back_to_previous_line_start (it)
4434 struct it *it;
4435 {
4436 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4437 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4438 }
4439
4440
4441 /* Move IT to the next line start.
4442
4443 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4444 we skipped over part of the text (as opposed to moving the iterator
4445 continuously over the text). Otherwise, don't change the value
4446 of *SKIPPED_P.
4447
4448 Newlines may come from buffer text, overlay strings, or strings
4449 displayed via the `display' property. That's the reason we can't
4450 simply use find_next_newline_no_quit.
4451
4452 Note that this function may not skip over invisible text that is so
4453 because of text properties and immediately follows a newline. If
4454 it would, function reseat_at_next_visible_line_start, when called
4455 from set_iterator_to_next, would effectively make invisible
4456 characters following a newline part of the wrong glyph row, which
4457 leads to wrong cursor motion. */
4458
4459 static int
4460 forward_to_next_line_start (it, skipped_p)
4461 struct it *it;
4462 int *skipped_p;
4463 {
4464 int old_selective, newline_found_p, n;
4465 const int MAX_NEWLINE_DISTANCE = 500;
4466
4467 /* If already on a newline, just consume it to avoid unintended
4468 skipping over invisible text below. */
4469 if (it->what == IT_CHARACTER
4470 && it->c == '\n'
4471 && CHARPOS (it->position) == IT_CHARPOS (*it))
4472 {
4473 set_iterator_to_next (it, 0);
4474 it->c = 0;
4475 return 1;
4476 }
4477
4478 /* Don't handle selective display in the following. It's (a)
4479 unnecessary because it's done by the caller, and (b) leads to an
4480 infinite recursion because next_element_from_ellipsis indirectly
4481 calls this function. */
4482 old_selective = it->selective;
4483 it->selective = 0;
4484
4485 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4486 from buffer text. */
4487 for (n = newline_found_p = 0;
4488 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4489 n += STRINGP (it->string) ? 0 : 1)
4490 {
4491 if (!get_next_display_element (it))
4492 return 0;
4493 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4494 set_iterator_to_next (it, 0);
4495 }
4496
4497 /* If we didn't find a newline near enough, see if we can use a
4498 short-cut. */
4499 if (!newline_found_p)
4500 {
4501 int start = IT_CHARPOS (*it);
4502 int limit = find_next_newline_no_quit (start, 1);
4503 Lisp_Object pos;
4504
4505 xassert (!STRINGP (it->string));
4506
4507 /* If there isn't any `display' property in sight, and no
4508 overlays, we can just use the position of the newline in
4509 buffer text. */
4510 if (it->stop_charpos >= limit
4511 || ((pos = Fnext_single_property_change (make_number (start),
4512 Qdisplay,
4513 Qnil, make_number (limit)),
4514 NILP (pos))
4515 && next_overlay_change (start) == ZV))
4516 {
4517 IT_CHARPOS (*it) = limit;
4518 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4519 *skipped_p = newline_found_p = 1;
4520 }
4521 else
4522 {
4523 while (get_next_display_element (it)
4524 && !newline_found_p)
4525 {
4526 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4527 set_iterator_to_next (it, 0);
4528 }
4529 }
4530 }
4531
4532 it->selective = old_selective;
4533 return newline_found_p;
4534 }
4535
4536
4537 /* Set IT's current position to the previous visible line start. Skip
4538 invisible text that is so either due to text properties or due to
4539 selective display. Caution: this does not change IT->current_x and
4540 IT->hpos. */
4541
4542 static void
4543 back_to_previous_visible_line_start (it)
4544 struct it *it;
4545 {
4546 int visible_p = 0;
4547
4548 /* Go back one newline if not on BEGV already. */
4549 if (IT_CHARPOS (*it) > BEGV)
4550 back_to_previous_line_start (it);
4551
4552 /* Move over lines that are invisible because of selective display
4553 or text properties. */
4554 while (IT_CHARPOS (*it) > BEGV
4555 && !visible_p)
4556 {
4557 visible_p = 1;
4558
4559 /* If selective > 0, then lines indented more than that values
4560 are invisible. */
4561 if (it->selective > 0
4562 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4563 (double) it->selective)) /* iftc */
4564 visible_p = 0;
4565 else
4566 {
4567 Lisp_Object prop;
4568
4569 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4570 Qinvisible, it->window);
4571 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4572 visible_p = 0;
4573 }
4574
4575 /* Back one more newline if the current one is invisible. */
4576 if (!visible_p)
4577 back_to_previous_line_start (it);
4578 }
4579
4580 xassert (IT_CHARPOS (*it) >= BEGV);
4581 xassert (IT_CHARPOS (*it) == BEGV
4582 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4583 CHECK_IT (it);
4584 }
4585
4586
4587 /* Reseat iterator IT at the previous visible line start. Skip
4588 invisible text that is so either due to text properties or due to
4589 selective display. At the end, update IT's overlay information,
4590 face information etc. */
4591
4592 static void
4593 reseat_at_previous_visible_line_start (it)
4594 struct it *it;
4595 {
4596 back_to_previous_visible_line_start (it);
4597 reseat (it, it->current.pos, 1);
4598 CHECK_IT (it);
4599 }
4600
4601
4602 /* Reseat iterator IT on the next visible line start in the current
4603 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4604 preceding the line start. Skip over invisible text that is so
4605 because of selective display. Compute faces, overlays etc at the
4606 new position. Note that this function does not skip over text that
4607 is invisible because of text properties. */
4608
4609 static void
4610 reseat_at_next_visible_line_start (it, on_newline_p)
4611 struct it *it;
4612 int on_newline_p;
4613 {
4614 int newline_found_p, skipped_p = 0;
4615
4616 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4617
4618 /* Skip over lines that are invisible because they are indented
4619 more than the value of IT->selective. */
4620 if (it->selective > 0)
4621 while (IT_CHARPOS (*it) < ZV
4622 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4623 (double) it->selective)) /* iftc */
4624 {
4625 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4626 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4627 }
4628
4629 /* Position on the newline if that's what's requested. */
4630 if (on_newline_p && newline_found_p)
4631 {
4632 if (STRINGP (it->string))
4633 {
4634 if (IT_STRING_CHARPOS (*it) > 0)
4635 {
4636 --IT_STRING_CHARPOS (*it);
4637 --IT_STRING_BYTEPOS (*it);
4638 }
4639 }
4640 else if (IT_CHARPOS (*it) > BEGV)
4641 {
4642 --IT_CHARPOS (*it);
4643 --IT_BYTEPOS (*it);
4644 reseat (it, it->current.pos, 0);
4645 }
4646 }
4647 else if (skipped_p)
4648 reseat (it, it->current.pos, 0);
4649
4650 CHECK_IT (it);
4651 }
4652
4653
4654 \f
4655 /***********************************************************************
4656 Changing an iterator's position
4657 ***********************************************************************/
4658
4659 /* Change IT's current position to POS in current_buffer. If FORCE_P
4660 is non-zero, always check for text properties at the new position.
4661 Otherwise, text properties are only looked up if POS >=
4662 IT->check_charpos of a property. */
4663
4664 static void
4665 reseat (it, pos, force_p)
4666 struct it *it;
4667 struct text_pos pos;
4668 int force_p;
4669 {
4670 int original_pos = IT_CHARPOS (*it);
4671
4672 reseat_1 (it, pos, 0);
4673
4674 /* Determine where to check text properties. Avoid doing it
4675 where possible because text property lookup is very expensive. */
4676 if (force_p
4677 || CHARPOS (pos) > it->stop_charpos
4678 || CHARPOS (pos) < original_pos)
4679 handle_stop (it);
4680
4681 CHECK_IT (it);
4682 }
4683
4684
4685 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4686 IT->stop_pos to POS, also. */
4687
4688 static void
4689 reseat_1 (it, pos, set_stop_p)
4690 struct it *it;
4691 struct text_pos pos;
4692 int set_stop_p;
4693 {
4694 /* Don't call this function when scanning a C string. */
4695 xassert (it->s == NULL);
4696
4697 /* POS must be a reasonable value. */
4698 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4699
4700 it->current.pos = it->position = pos;
4701 XSETBUFFER (it->object, current_buffer);
4702 it->end_charpos = ZV;
4703 it->dpvec = NULL;
4704 it->current.dpvec_index = -1;
4705 it->current.overlay_string_index = -1;
4706 IT_STRING_CHARPOS (*it) = -1;
4707 IT_STRING_BYTEPOS (*it) = -1;
4708 it->string = Qnil;
4709 it->method = next_element_from_buffer;
4710 /* RMS: I added this to fix a bug in move_it_vertically_backward
4711 where it->area continued to relate to the starting point
4712 for the backward motion. Bug report from
4713 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4714 However, I am not sure whether reseat still does the right thing
4715 in general after this change. */
4716 it->area = TEXT_AREA;
4717 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4718 it->sp = 0;
4719 it->face_before_selective_p = 0;
4720
4721 if (set_stop_p)
4722 it->stop_charpos = CHARPOS (pos);
4723 }
4724
4725
4726 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4727 If S is non-null, it is a C string to iterate over. Otherwise,
4728 STRING gives a Lisp string to iterate over.
4729
4730 If PRECISION > 0, don't return more then PRECISION number of
4731 characters from the string.
4732
4733 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4734 characters have been returned. FIELD_WIDTH < 0 means an infinite
4735 field width.
4736
4737 MULTIBYTE = 0 means disable processing of multibyte characters,
4738 MULTIBYTE > 0 means enable it,
4739 MULTIBYTE < 0 means use IT->multibyte_p.
4740
4741 IT must be initialized via a prior call to init_iterator before
4742 calling this function. */
4743
4744 static void
4745 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4746 struct it *it;
4747 unsigned char *s;
4748 Lisp_Object string;
4749 int charpos;
4750 int precision, field_width, multibyte;
4751 {
4752 /* No region in strings. */
4753 it->region_beg_charpos = it->region_end_charpos = -1;
4754
4755 /* No text property checks performed by default, but see below. */
4756 it->stop_charpos = -1;
4757
4758 /* Set iterator position and end position. */
4759 bzero (&it->current, sizeof it->current);
4760 it->current.overlay_string_index = -1;
4761 it->current.dpvec_index = -1;
4762 xassert (charpos >= 0);
4763
4764 /* If STRING is specified, use its multibyteness, otherwise use the
4765 setting of MULTIBYTE, if specified. */
4766 if (multibyte >= 0)
4767 it->multibyte_p = multibyte > 0;
4768
4769 if (s == NULL)
4770 {
4771 xassert (STRINGP (string));
4772 it->string = string;
4773 it->s = NULL;
4774 it->end_charpos = it->string_nchars = SCHARS (string);
4775 it->method = next_element_from_string;
4776 it->current.string_pos = string_pos (charpos, string);
4777 }
4778 else
4779 {
4780 it->s = s;
4781 it->string = Qnil;
4782
4783 /* Note that we use IT->current.pos, not it->current.string_pos,
4784 for displaying C strings. */
4785 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4786 if (it->multibyte_p)
4787 {
4788 it->current.pos = c_string_pos (charpos, s, 1);
4789 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4790 }
4791 else
4792 {
4793 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4794 it->end_charpos = it->string_nchars = strlen (s);
4795 }
4796
4797 it->method = next_element_from_c_string;
4798 }
4799
4800 /* PRECISION > 0 means don't return more than PRECISION characters
4801 from the string. */
4802 if (precision > 0 && it->end_charpos - charpos > precision)
4803 it->end_charpos = it->string_nchars = charpos + precision;
4804
4805 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4806 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4807 FIELD_WIDTH < 0 means infinite field width. This is useful for
4808 padding with `-' at the end of a mode line. */
4809 if (field_width < 0)
4810 field_width = INFINITY;
4811 if (field_width > it->end_charpos - charpos)
4812 it->end_charpos = charpos + field_width;
4813
4814 /* Use the standard display table for displaying strings. */
4815 if (DISP_TABLE_P (Vstandard_display_table))
4816 it->dp = XCHAR_TABLE (Vstandard_display_table);
4817
4818 it->stop_charpos = charpos;
4819 CHECK_IT (it);
4820 }
4821
4822
4823 \f
4824 /***********************************************************************
4825 Iteration
4826 ***********************************************************************/
4827
4828 /* Load IT's display element fields with information about the next
4829 display element from the current position of IT. Value is zero if
4830 end of buffer (or C string) is reached. */
4831
4832 int
4833 get_next_display_element (it)
4834 struct it *it;
4835 {
4836 /* Non-zero means that we found a display element. Zero means that
4837 we hit the end of what we iterate over. Performance note: the
4838 function pointer `method' used here turns out to be faster than
4839 using a sequence of if-statements. */
4840 int success_p = (*it->method) (it);
4841
4842 if (it->what == IT_CHARACTER)
4843 {
4844 /* Map via display table or translate control characters.
4845 IT->c, IT->len etc. have been set to the next character by
4846 the function call above. If we have a display table, and it
4847 contains an entry for IT->c, translate it. Don't do this if
4848 IT->c itself comes from a display table, otherwise we could
4849 end up in an infinite recursion. (An alternative could be to
4850 count the recursion depth of this function and signal an
4851 error when a certain maximum depth is reached.) Is it worth
4852 it? */
4853 if (success_p && it->dpvec == NULL)
4854 {
4855 Lisp_Object dv;
4856
4857 if (it->dp
4858 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4859 VECTORP (dv)))
4860 {
4861 struct Lisp_Vector *v = XVECTOR (dv);
4862
4863 /* Return the first character from the display table
4864 entry, if not empty. If empty, don't display the
4865 current character. */
4866 if (v->size)
4867 {
4868 it->dpvec_char_len = it->len;
4869 it->dpvec = v->contents;
4870 it->dpend = v->contents + v->size;
4871 it->current.dpvec_index = 0;
4872 it->method = next_element_from_display_vector;
4873 success_p = get_next_display_element (it);
4874 }
4875 else
4876 {
4877 set_iterator_to_next (it, 0);
4878 success_p = get_next_display_element (it);
4879 }
4880 }
4881
4882 /* Translate control characters into `\003' or `^C' form.
4883 Control characters coming from a display table entry are
4884 currently not translated because we use IT->dpvec to hold
4885 the translation. This could easily be changed but I
4886 don't believe that it is worth doing.
4887
4888 If it->multibyte_p is nonzero, eight-bit characters and
4889 non-printable multibyte characters are also translated to
4890 octal form.
4891
4892 If it->multibyte_p is zero, eight-bit characters that
4893 don't have corresponding multibyte char code are also
4894 translated to octal form. */
4895 else if ((it->c < ' '
4896 && (it->area != TEXT_AREA
4897 || (it->c != '\n' && it->c != '\t')))
4898 || (it->multibyte_p
4899 ? ((it->c >= 127
4900 && it->len == 1)
4901 || !CHAR_PRINTABLE_P (it->c))
4902 : (it->c >= 127
4903 && it->c == unibyte_char_to_multibyte (it->c))))
4904 {
4905 /* IT->c is a control character which must be displayed
4906 either as '\003' or as `^C' where the '\\' and '^'
4907 can be defined in the display table. Fill
4908 IT->ctl_chars with glyphs for what we have to
4909 display. Then, set IT->dpvec to these glyphs. */
4910 GLYPH g;
4911
4912 if (it->c < 128 && it->ctl_arrow_p)
4913 {
4914 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4915 if (it->dp
4916 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4917 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4918 g = XINT (DISP_CTRL_GLYPH (it->dp));
4919 else
4920 g = FAST_MAKE_GLYPH ('^', 0);
4921 XSETINT (it->ctl_chars[0], g);
4922
4923 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4924 XSETINT (it->ctl_chars[1], g);
4925
4926 /* Set up IT->dpvec and return first character from it. */
4927 it->dpvec_char_len = it->len;
4928 it->dpvec = it->ctl_chars;
4929 it->dpend = it->dpvec + 2;
4930 it->current.dpvec_index = 0;
4931 it->method = next_element_from_display_vector;
4932 get_next_display_element (it);
4933 }
4934 else
4935 {
4936 unsigned char str[MAX_MULTIBYTE_LENGTH];
4937 int len;
4938 int i;
4939 GLYPH escape_glyph;
4940
4941 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4942 if (it->dp
4943 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4944 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4945 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4946 else
4947 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4948
4949 if (SINGLE_BYTE_CHAR_P (it->c))
4950 str[0] = it->c, len = 1;
4951 else
4952 {
4953 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4954 if (len < 0)
4955 {
4956 /* It's an invalid character, which
4957 shouldn't happen actually, but due to
4958 bugs it may happen. Let's print the char
4959 as is, there's not much meaningful we can
4960 do with it. */
4961 str[0] = it->c;
4962 str[1] = it->c >> 8;
4963 str[2] = it->c >> 16;
4964 str[3] = it->c >> 24;
4965 len = 4;
4966 }
4967 }
4968
4969 for (i = 0; i < len; i++)
4970 {
4971 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4972 /* Insert three more glyphs into IT->ctl_chars for
4973 the octal display of the character. */
4974 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4975 XSETINT (it->ctl_chars[i * 4 + 1], g);
4976 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4977 XSETINT (it->ctl_chars[i * 4 + 2], g);
4978 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4979 XSETINT (it->ctl_chars[i * 4 + 3], g);
4980 }
4981
4982 /* Set up IT->dpvec and return the first character
4983 from it. */
4984 it->dpvec_char_len = it->len;
4985 it->dpvec = it->ctl_chars;
4986 it->dpend = it->dpvec + len * 4;
4987 it->current.dpvec_index = 0;
4988 it->method = next_element_from_display_vector;
4989 get_next_display_element (it);
4990 }
4991 }
4992 }
4993
4994 /* Adjust face id for a multibyte character. There are no
4995 multibyte character in unibyte text. */
4996 if (it->multibyte_p
4997 && success_p
4998 && FRAME_WINDOW_P (it->f))
4999 {
5000 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5001 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5002 }
5003 }
5004
5005 /* Is this character the last one of a run of characters with
5006 box? If yes, set IT->end_of_box_run_p to 1. */
5007 if (it->face_box_p
5008 && it->s == NULL)
5009 {
5010 int face_id;
5011 struct face *face;
5012
5013 it->end_of_box_run_p
5014 = ((face_id = face_after_it_pos (it),
5015 face_id != it->face_id)
5016 && (face = FACE_FROM_ID (it->f, face_id),
5017 face->box == FACE_NO_BOX));
5018 }
5019
5020 /* Value is 0 if end of buffer or string reached. */
5021 return success_p;
5022 }
5023
5024
5025 /* Move IT to the next display element.
5026
5027 RESEAT_P non-zero means if called on a newline in buffer text,
5028 skip to the next visible line start.
5029
5030 Functions get_next_display_element and set_iterator_to_next are
5031 separate because I find this arrangement easier to handle than a
5032 get_next_display_element function that also increments IT's
5033 position. The way it is we can first look at an iterator's current
5034 display element, decide whether it fits on a line, and if it does,
5035 increment the iterator position. The other way around we probably
5036 would either need a flag indicating whether the iterator has to be
5037 incremented the next time, or we would have to implement a
5038 decrement position function which would not be easy to write. */
5039
5040 void
5041 set_iterator_to_next (it, reseat_p)
5042 struct it *it;
5043 int reseat_p;
5044 {
5045 /* Reset flags indicating start and end of a sequence of characters
5046 with box. Reset them at the start of this function because
5047 moving the iterator to a new position might set them. */
5048 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5049
5050 if (it->method == next_element_from_buffer)
5051 {
5052 /* The current display element of IT is a character from
5053 current_buffer. Advance in the buffer, and maybe skip over
5054 invisible lines that are so because of selective display. */
5055 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5056 reseat_at_next_visible_line_start (it, 0);
5057 else
5058 {
5059 xassert (it->len != 0);
5060 IT_BYTEPOS (*it) += it->len;
5061 IT_CHARPOS (*it) += 1;
5062 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5063 }
5064 }
5065 else if (it->method == next_element_from_composition)
5066 {
5067 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5068 if (STRINGP (it->string))
5069 {
5070 IT_STRING_BYTEPOS (*it) += it->len;
5071 IT_STRING_CHARPOS (*it) += it->cmp_len;
5072 it->method = next_element_from_string;
5073 goto consider_string_end;
5074 }
5075 else
5076 {
5077 IT_BYTEPOS (*it) += it->len;
5078 IT_CHARPOS (*it) += it->cmp_len;
5079 it->method = next_element_from_buffer;
5080 }
5081 }
5082 else if (it->method == next_element_from_c_string)
5083 {
5084 /* Current display element of IT is from a C string. */
5085 IT_BYTEPOS (*it) += it->len;
5086 IT_CHARPOS (*it) += 1;
5087 }
5088 else if (it->method == next_element_from_display_vector)
5089 {
5090 /* Current display element of IT is from a display table entry.
5091 Advance in the display table definition. Reset it to null if
5092 end reached, and continue with characters from buffers/
5093 strings. */
5094 ++it->current.dpvec_index;
5095
5096 /* Restore face of the iterator to what they were before the
5097 display vector entry (these entries may contain faces). */
5098 it->face_id = it->saved_face_id;
5099
5100 if (it->dpvec + it->current.dpvec_index == it->dpend)
5101 {
5102 if (it->s)
5103 it->method = next_element_from_c_string;
5104 else if (STRINGP (it->string))
5105 it->method = next_element_from_string;
5106 else
5107 it->method = next_element_from_buffer;
5108
5109 it->dpvec = NULL;
5110 it->current.dpvec_index = -1;
5111
5112 /* Skip over characters which were displayed via IT->dpvec. */
5113 if (it->dpvec_char_len < 0)
5114 reseat_at_next_visible_line_start (it, 1);
5115 else if (it->dpvec_char_len > 0)
5116 {
5117 it->len = it->dpvec_char_len;
5118 set_iterator_to_next (it, reseat_p);
5119 }
5120 }
5121 }
5122 else if (it->method == next_element_from_string)
5123 {
5124 /* Current display element is a character from a Lisp string. */
5125 xassert (it->s == NULL && STRINGP (it->string));
5126 IT_STRING_BYTEPOS (*it) += it->len;
5127 IT_STRING_CHARPOS (*it) += 1;
5128
5129 consider_string_end:
5130
5131 if (it->current.overlay_string_index >= 0)
5132 {
5133 /* IT->string is an overlay string. Advance to the
5134 next, if there is one. */
5135 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5136 next_overlay_string (it);
5137 }
5138 else
5139 {
5140 /* IT->string is not an overlay string. If we reached
5141 its end, and there is something on IT->stack, proceed
5142 with what is on the stack. This can be either another
5143 string, this time an overlay string, or a buffer. */
5144 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5145 && it->sp > 0)
5146 {
5147 pop_it (it);
5148 if (!STRINGP (it->string))
5149 it->method = next_element_from_buffer;
5150 else
5151 goto consider_string_end;
5152 }
5153 }
5154 }
5155 else if (it->method == next_element_from_image
5156 || it->method == next_element_from_stretch)
5157 {
5158 /* The position etc with which we have to proceed are on
5159 the stack. The position may be at the end of a string,
5160 if the `display' property takes up the whole string. */
5161 pop_it (it);
5162 it->image_id = 0;
5163 if (STRINGP (it->string))
5164 {
5165 it->method = next_element_from_string;
5166 goto consider_string_end;
5167 }
5168 else
5169 it->method = next_element_from_buffer;
5170 }
5171 else
5172 /* There are no other methods defined, so this should be a bug. */
5173 abort ();
5174
5175 xassert (it->method != next_element_from_string
5176 || (STRINGP (it->string)
5177 && IT_STRING_CHARPOS (*it) >= 0));
5178 }
5179
5180
5181 /* Load IT's display element fields with information about the next
5182 display element which comes from a display table entry or from the
5183 result of translating a control character to one of the forms `^C'
5184 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5185
5186 static int
5187 next_element_from_display_vector (it)
5188 struct it *it;
5189 {
5190 /* Precondition. */
5191 xassert (it->dpvec && it->current.dpvec_index >= 0);
5192
5193 /* Remember the current face id in case glyphs specify faces.
5194 IT's face is restored in set_iterator_to_next. */
5195 it->saved_face_id = it->face_id;
5196
5197 if (INTEGERP (*it->dpvec)
5198 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5199 {
5200 int lface_id;
5201 GLYPH g;
5202
5203 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5204 it->c = FAST_GLYPH_CHAR (g);
5205 it->len = CHAR_BYTES (it->c);
5206
5207 /* The entry may contain a face id to use. Such a face id is
5208 the id of a Lisp face, not a realized face. A face id of
5209 zero means no face is specified. */
5210 lface_id = FAST_GLYPH_FACE (g);
5211 if (lface_id)
5212 {
5213 /* The function returns -1 if lface_id is invalid. */
5214 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5215 if (face_id >= 0)
5216 it->face_id = face_id;
5217 }
5218 }
5219 else
5220 /* Display table entry is invalid. Return a space. */
5221 it->c = ' ', it->len = 1;
5222
5223 /* Don't change position and object of the iterator here. They are
5224 still the values of the character that had this display table
5225 entry or was translated, and that's what we want. */
5226 it->what = IT_CHARACTER;
5227 return 1;
5228 }
5229
5230
5231 /* Load IT with the next display element from Lisp string IT->string.
5232 IT->current.string_pos is the current position within the string.
5233 If IT->current.overlay_string_index >= 0, the Lisp string is an
5234 overlay string. */
5235
5236 static int
5237 next_element_from_string (it)
5238 struct it *it;
5239 {
5240 struct text_pos position;
5241
5242 xassert (STRINGP (it->string));
5243 xassert (IT_STRING_CHARPOS (*it) >= 0);
5244 position = it->current.string_pos;
5245
5246 /* Time to check for invisible text? */
5247 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5248 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5249 {
5250 handle_stop (it);
5251
5252 /* Since a handler may have changed IT->method, we must
5253 recurse here. */
5254 return get_next_display_element (it);
5255 }
5256
5257 if (it->current.overlay_string_index >= 0)
5258 {
5259 /* Get the next character from an overlay string. In overlay
5260 strings, There is no field width or padding with spaces to
5261 do. */
5262 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5263 {
5264 it->what = IT_EOB;
5265 return 0;
5266 }
5267 else if (STRING_MULTIBYTE (it->string))
5268 {
5269 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5270 const unsigned char *s = (SDATA (it->string)
5271 + IT_STRING_BYTEPOS (*it));
5272 it->c = string_char_and_length (s, remaining, &it->len);
5273 }
5274 else
5275 {
5276 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5277 it->len = 1;
5278 }
5279 }
5280 else
5281 {
5282 /* Get the next character from a Lisp string that is not an
5283 overlay string. Such strings come from the mode line, for
5284 example. We may have to pad with spaces, or truncate the
5285 string. See also next_element_from_c_string. */
5286 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5287 {
5288 it->what = IT_EOB;
5289 return 0;
5290 }
5291 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5292 {
5293 /* Pad with spaces. */
5294 it->c = ' ', it->len = 1;
5295 CHARPOS (position) = BYTEPOS (position) = -1;
5296 }
5297 else if (STRING_MULTIBYTE (it->string))
5298 {
5299 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5300 const unsigned char *s = (SDATA (it->string)
5301 + IT_STRING_BYTEPOS (*it));
5302 it->c = string_char_and_length (s, maxlen, &it->len);
5303 }
5304 else
5305 {
5306 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5307 it->len = 1;
5308 }
5309 }
5310
5311 /* Record what we have and where it came from. Note that we store a
5312 buffer position in IT->position although it could arguably be a
5313 string position. */
5314 it->what = IT_CHARACTER;
5315 it->object = it->string;
5316 it->position = position;
5317 return 1;
5318 }
5319
5320
5321 /* Load IT with next display element from C string IT->s.
5322 IT->string_nchars is the maximum number of characters to return
5323 from the string. IT->end_charpos may be greater than
5324 IT->string_nchars when this function is called, in which case we
5325 may have to return padding spaces. Value is zero if end of string
5326 reached, including padding spaces. */
5327
5328 static int
5329 next_element_from_c_string (it)
5330 struct it *it;
5331 {
5332 int success_p = 1;
5333
5334 xassert (it->s);
5335 it->what = IT_CHARACTER;
5336 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5337 it->object = Qnil;
5338
5339 /* IT's position can be greater IT->string_nchars in case a field
5340 width or precision has been specified when the iterator was
5341 initialized. */
5342 if (IT_CHARPOS (*it) >= it->end_charpos)
5343 {
5344 /* End of the game. */
5345 it->what = IT_EOB;
5346 success_p = 0;
5347 }
5348 else if (IT_CHARPOS (*it) >= it->string_nchars)
5349 {
5350 /* Pad with spaces. */
5351 it->c = ' ', it->len = 1;
5352 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5353 }
5354 else if (it->multibyte_p)
5355 {
5356 /* Implementation note: The calls to strlen apparently aren't a
5357 performance problem because there is no noticeable performance
5358 difference between Emacs running in unibyte or multibyte mode. */
5359 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5360 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5361 maxlen, &it->len);
5362 }
5363 else
5364 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5365
5366 return success_p;
5367 }
5368
5369
5370 /* Set up IT to return characters from an ellipsis, if appropriate.
5371 The definition of the ellipsis glyphs may come from a display table
5372 entry. This function Fills IT with the first glyph from the
5373 ellipsis if an ellipsis is to be displayed. */
5374
5375 static int
5376 next_element_from_ellipsis (it)
5377 struct it *it;
5378 {
5379 if (it->selective_display_ellipsis_p)
5380 {
5381 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5382 {
5383 /* Use the display table definition for `...'. Invalid glyphs
5384 will be handled by the method returning elements from dpvec. */
5385 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5386 it->dpvec_char_len = it->len;
5387 it->dpvec = v->contents;
5388 it->dpend = v->contents + v->size;
5389 it->current.dpvec_index = 0;
5390 it->method = next_element_from_display_vector;
5391 }
5392 else
5393 {
5394 /* Use default `...' which is stored in default_invis_vector. */
5395 it->dpvec_char_len = it->len;
5396 it->dpvec = default_invis_vector;
5397 it->dpend = default_invis_vector + 3;
5398 it->current.dpvec_index = 0;
5399 it->method = next_element_from_display_vector;
5400 }
5401 }
5402 else
5403 {
5404 /* The face at the current position may be different from the
5405 face we find after the invisible text. Remember what it
5406 was in IT->saved_face_id, and signal that it's there by
5407 setting face_before_selective_p. */
5408 it->saved_face_id = it->face_id;
5409 it->method = next_element_from_buffer;
5410 reseat_at_next_visible_line_start (it, 1);
5411 it->face_before_selective_p = 1;
5412 }
5413
5414 return get_next_display_element (it);
5415 }
5416
5417
5418 /* Deliver an image display element. The iterator IT is already
5419 filled with image information (done in handle_display_prop). Value
5420 is always 1. */
5421
5422
5423 static int
5424 next_element_from_image (it)
5425 struct it *it;
5426 {
5427 it->what = IT_IMAGE;
5428 return 1;
5429 }
5430
5431
5432 /* Fill iterator IT with next display element from a stretch glyph
5433 property. IT->object is the value of the text property. Value is
5434 always 1. */
5435
5436 static int
5437 next_element_from_stretch (it)
5438 struct it *it;
5439 {
5440 it->what = IT_STRETCH;
5441 return 1;
5442 }
5443
5444
5445 /* Load IT with the next display element from current_buffer. Value
5446 is zero if end of buffer reached. IT->stop_charpos is the next
5447 position at which to stop and check for text properties or buffer
5448 end. */
5449
5450 static int
5451 next_element_from_buffer (it)
5452 struct it *it;
5453 {
5454 int success_p = 1;
5455
5456 /* Check this assumption, otherwise, we would never enter the
5457 if-statement, below. */
5458 xassert (IT_CHARPOS (*it) >= BEGV
5459 && IT_CHARPOS (*it) <= it->stop_charpos);
5460
5461 if (IT_CHARPOS (*it) >= it->stop_charpos)
5462 {
5463 if (IT_CHARPOS (*it) >= it->end_charpos)
5464 {
5465 int overlay_strings_follow_p;
5466
5467 /* End of the game, except when overlay strings follow that
5468 haven't been returned yet. */
5469 if (it->overlay_strings_at_end_processed_p)
5470 overlay_strings_follow_p = 0;
5471 else
5472 {
5473 it->overlay_strings_at_end_processed_p = 1;
5474 overlay_strings_follow_p = get_overlay_strings (it, 0);
5475 }
5476
5477 if (overlay_strings_follow_p)
5478 success_p = get_next_display_element (it);
5479 else
5480 {
5481 it->what = IT_EOB;
5482 it->position = it->current.pos;
5483 success_p = 0;
5484 }
5485 }
5486 else
5487 {
5488 handle_stop (it);
5489 return get_next_display_element (it);
5490 }
5491 }
5492 else
5493 {
5494 /* No face changes, overlays etc. in sight, so just return a
5495 character from current_buffer. */
5496 unsigned char *p;
5497
5498 /* Maybe run the redisplay end trigger hook. Performance note:
5499 This doesn't seem to cost measurable time. */
5500 if (it->redisplay_end_trigger_charpos
5501 && it->glyph_row
5502 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5503 run_redisplay_end_trigger_hook (it);
5504
5505 /* Get the next character, maybe multibyte. */
5506 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5507 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5508 {
5509 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5510 - IT_BYTEPOS (*it));
5511 it->c = string_char_and_length (p, maxlen, &it->len);
5512 }
5513 else
5514 it->c = *p, it->len = 1;
5515
5516 /* Record what we have and where it came from. */
5517 it->what = IT_CHARACTER;;
5518 it->object = it->w->buffer;
5519 it->position = it->current.pos;
5520
5521 /* Normally we return the character found above, except when we
5522 really want to return an ellipsis for selective display. */
5523 if (it->selective)
5524 {
5525 if (it->c == '\n')
5526 {
5527 /* A value of selective > 0 means hide lines indented more
5528 than that number of columns. */
5529 if (it->selective > 0
5530 && IT_CHARPOS (*it) + 1 < ZV
5531 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5532 IT_BYTEPOS (*it) + 1,
5533 (double) it->selective)) /* iftc */
5534 {
5535 success_p = next_element_from_ellipsis (it);
5536 it->dpvec_char_len = -1;
5537 }
5538 }
5539 else if (it->c == '\r' && it->selective == -1)
5540 {
5541 /* A value of selective == -1 means that everything from the
5542 CR to the end of the line is invisible, with maybe an
5543 ellipsis displayed for it. */
5544 success_p = next_element_from_ellipsis (it);
5545 it->dpvec_char_len = -1;
5546 }
5547 }
5548 }
5549
5550 /* Value is zero if end of buffer reached. */
5551 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5552 return success_p;
5553 }
5554
5555
5556 /* Run the redisplay end trigger hook for IT. */
5557
5558 static void
5559 run_redisplay_end_trigger_hook (it)
5560 struct it *it;
5561 {
5562 Lisp_Object args[3];
5563
5564 /* IT->glyph_row should be non-null, i.e. we should be actually
5565 displaying something, or otherwise we should not run the hook. */
5566 xassert (it->glyph_row);
5567
5568 /* Set up hook arguments. */
5569 args[0] = Qredisplay_end_trigger_functions;
5570 args[1] = it->window;
5571 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5572 it->redisplay_end_trigger_charpos = 0;
5573
5574 /* Since we are *trying* to run these functions, don't try to run
5575 them again, even if they get an error. */
5576 it->w->redisplay_end_trigger = Qnil;
5577 Frun_hook_with_args (3, args);
5578
5579 /* Notice if it changed the face of the character we are on. */
5580 handle_face_prop (it);
5581 }
5582
5583
5584 /* Deliver a composition display element. The iterator IT is already
5585 filled with composition information (done in
5586 handle_composition_prop). Value is always 1. */
5587
5588 static int
5589 next_element_from_composition (it)
5590 struct it *it;
5591 {
5592 it->what = IT_COMPOSITION;
5593 it->position = (STRINGP (it->string)
5594 ? it->current.string_pos
5595 : it->current.pos);
5596 return 1;
5597 }
5598
5599
5600 \f
5601 /***********************************************************************
5602 Moving an iterator without producing glyphs
5603 ***********************************************************************/
5604
5605 /* Move iterator IT to a specified buffer or X position within one
5606 line on the display without producing glyphs.
5607
5608 OP should be a bit mask including some or all of these bits:
5609 MOVE_TO_X: Stop on reaching x-position TO_X.
5610 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5611 Regardless of OP's value, stop in reaching the end of the display line.
5612
5613 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5614 This means, in particular, that TO_X includes window's horizontal
5615 scroll amount.
5616
5617 The return value has several possible values that
5618 say what condition caused the scan to stop:
5619
5620 MOVE_POS_MATCH_OR_ZV
5621 - when TO_POS or ZV was reached.
5622
5623 MOVE_X_REACHED
5624 -when TO_X was reached before TO_POS or ZV were reached.
5625
5626 MOVE_LINE_CONTINUED
5627 - when we reached the end of the display area and the line must
5628 be continued.
5629
5630 MOVE_LINE_TRUNCATED
5631 - when we reached the end of the display area and the line is
5632 truncated.
5633
5634 MOVE_NEWLINE_OR_CR
5635 - when we stopped at a line end, i.e. a newline or a CR and selective
5636 display is on. */
5637
5638 static enum move_it_result
5639 move_it_in_display_line_to (it, to_charpos, to_x, op)
5640 struct it *it;
5641 int to_charpos, to_x, op;
5642 {
5643 enum move_it_result result = MOVE_UNDEFINED;
5644 struct glyph_row *saved_glyph_row;
5645
5646 /* Don't produce glyphs in produce_glyphs. */
5647 saved_glyph_row = it->glyph_row;
5648 it->glyph_row = NULL;
5649
5650 #define BUFFER_POS_REACHED_P() \
5651 ((op & MOVE_TO_POS) != 0 \
5652 && BUFFERP (it->object) \
5653 && IT_CHARPOS (*it) >= to_charpos)
5654
5655 while (1)
5656 {
5657 int x, i, ascent = 0, descent = 0;
5658
5659 /* Stop when ZV or TO_CHARPOS reached. */
5660 if (!get_next_display_element (it)
5661 || BUFFER_POS_REACHED_P ())
5662 {
5663 result = MOVE_POS_MATCH_OR_ZV;
5664 break;
5665 }
5666
5667 /* The call to produce_glyphs will get the metrics of the
5668 display element IT is loaded with. We record in x the
5669 x-position before this display element in case it does not
5670 fit on the line. */
5671 x = it->current_x;
5672
5673 /* Remember the line height so far in case the next element doesn't
5674 fit on the line. */
5675 if (!it->truncate_lines_p)
5676 {
5677 ascent = it->max_ascent;
5678 descent = it->max_descent;
5679 }
5680
5681 PRODUCE_GLYPHS (it);
5682
5683 if (it->area != TEXT_AREA)
5684 {
5685 set_iterator_to_next (it, 1);
5686 continue;
5687 }
5688
5689 /* The number of glyphs we get back in IT->nglyphs will normally
5690 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5691 character on a terminal frame, or (iii) a line end. For the
5692 second case, IT->nglyphs - 1 padding glyphs will be present
5693 (on X frames, there is only one glyph produced for a
5694 composite character.
5695
5696 The behavior implemented below means, for continuation lines,
5697 that as many spaces of a TAB as fit on the current line are
5698 displayed there. For terminal frames, as many glyphs of a
5699 multi-glyph character are displayed in the current line, too.
5700 This is what the old redisplay code did, and we keep it that
5701 way. Under X, the whole shape of a complex character must
5702 fit on the line or it will be completely displayed in the
5703 next line.
5704
5705 Note that both for tabs and padding glyphs, all glyphs have
5706 the same width. */
5707 if (it->nglyphs)
5708 {
5709 /* More than one glyph or glyph doesn't fit on line. All
5710 glyphs have the same width. */
5711 int single_glyph_width = it->pixel_width / it->nglyphs;
5712 int new_x;
5713
5714 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5715 {
5716 new_x = x + single_glyph_width;
5717
5718 /* We want to leave anything reaching TO_X to the caller. */
5719 if ((op & MOVE_TO_X) && new_x > to_x)
5720 {
5721 it->current_x = x;
5722 result = MOVE_X_REACHED;
5723 break;
5724 }
5725 else if (/* Lines are continued. */
5726 !it->truncate_lines_p
5727 && (/* And glyph doesn't fit on the line. */
5728 new_x > it->last_visible_x
5729 /* Or it fits exactly and we're on a window
5730 system frame. */
5731 || (new_x == it->last_visible_x
5732 && FRAME_WINDOW_P (it->f))))
5733 {
5734 if (/* IT->hpos == 0 means the very first glyph
5735 doesn't fit on the line, e.g. a wide image. */
5736 it->hpos == 0
5737 || (new_x == it->last_visible_x
5738 && FRAME_WINDOW_P (it->f)))
5739 {
5740 ++it->hpos;
5741 it->current_x = new_x;
5742 if (i == it->nglyphs - 1)
5743 {
5744 set_iterator_to_next (it, 1);
5745 #ifdef HAVE_WINDOW_SYSTEM
5746 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5747 {
5748 if (!get_next_display_element (it)
5749 || BUFFER_POS_REACHED_P ())
5750 {
5751 result = MOVE_POS_MATCH_OR_ZV;
5752 break;
5753 }
5754 if (ITERATOR_AT_END_OF_LINE_P (it))
5755 {
5756 result = MOVE_NEWLINE_OR_CR;
5757 break;
5758 }
5759 }
5760 #endif /* HAVE_WINDOW_SYSTEM */
5761 }
5762 }
5763 else
5764 {
5765 it->current_x = x;
5766 it->max_ascent = ascent;
5767 it->max_descent = descent;
5768 }
5769
5770 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5771 IT_CHARPOS (*it)));
5772 result = MOVE_LINE_CONTINUED;
5773 break;
5774 }
5775 else if (new_x > it->first_visible_x)
5776 {
5777 /* Glyph is visible. Increment number of glyphs that
5778 would be displayed. */
5779 ++it->hpos;
5780 }
5781 else
5782 {
5783 /* Glyph is completely off the left margin of the display
5784 area. Nothing to do. */
5785 }
5786 }
5787
5788 if (result != MOVE_UNDEFINED)
5789 break;
5790 }
5791 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5792 {
5793 /* Stop when TO_X specified and reached. This check is
5794 necessary here because of lines consisting of a line end,
5795 only. The line end will not produce any glyphs and we
5796 would never get MOVE_X_REACHED. */
5797 xassert (it->nglyphs == 0);
5798 result = MOVE_X_REACHED;
5799 break;
5800 }
5801
5802 /* Is this a line end? If yes, we're done. */
5803 if (ITERATOR_AT_END_OF_LINE_P (it))
5804 {
5805 result = MOVE_NEWLINE_OR_CR;
5806 break;
5807 }
5808
5809 /* The current display element has been consumed. Advance
5810 to the next. */
5811 set_iterator_to_next (it, 1);
5812
5813 /* Stop if lines are truncated and IT's current x-position is
5814 past the right edge of the window now. */
5815 if (it->truncate_lines_p
5816 && it->current_x >= it->last_visible_x)
5817 {
5818 #ifdef HAVE_WINDOW_SYSTEM
5819 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5820 {
5821 if (!get_next_display_element (it)
5822 || BUFFER_POS_REACHED_P ())
5823 {
5824 result = MOVE_POS_MATCH_OR_ZV;
5825 break;
5826 }
5827 if (ITERATOR_AT_END_OF_LINE_P (it))
5828 {
5829 result = MOVE_NEWLINE_OR_CR;
5830 break;
5831 }
5832 }
5833 #endif /* HAVE_WINDOW_SYSTEM */
5834 result = MOVE_LINE_TRUNCATED;
5835 break;
5836 }
5837 }
5838
5839 #undef BUFFER_POS_REACHED_P
5840
5841 /* Restore the iterator settings altered at the beginning of this
5842 function. */
5843 it->glyph_row = saved_glyph_row;
5844 return result;
5845 }
5846
5847
5848 /* Move IT forward until it satisfies one or more of the criteria in
5849 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5850
5851 OP is a bit-mask that specifies where to stop, and in particular,
5852 which of those four position arguments makes a difference. See the
5853 description of enum move_operation_enum.
5854
5855 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5856 screen line, this function will set IT to the next position >
5857 TO_CHARPOS. */
5858
5859 void
5860 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5861 struct it *it;
5862 int to_charpos, to_x, to_y, to_vpos;
5863 int op;
5864 {
5865 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5866 int line_height;
5867 int reached = 0;
5868
5869 for (;;)
5870 {
5871 if (op & MOVE_TO_VPOS)
5872 {
5873 /* If no TO_CHARPOS and no TO_X specified, stop at the
5874 start of the line TO_VPOS. */
5875 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5876 {
5877 if (it->vpos == to_vpos)
5878 {
5879 reached = 1;
5880 break;
5881 }
5882 else
5883 skip = move_it_in_display_line_to (it, -1, -1, 0);
5884 }
5885 else
5886 {
5887 /* TO_VPOS >= 0 means stop at TO_X in the line at
5888 TO_VPOS, or at TO_POS, whichever comes first. */
5889 if (it->vpos == to_vpos)
5890 {
5891 reached = 2;
5892 break;
5893 }
5894
5895 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5896
5897 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5898 {
5899 reached = 3;
5900 break;
5901 }
5902 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5903 {
5904 /* We have reached TO_X but not in the line we want. */
5905 skip = move_it_in_display_line_to (it, to_charpos,
5906 -1, MOVE_TO_POS);
5907 if (skip == MOVE_POS_MATCH_OR_ZV)
5908 {
5909 reached = 4;
5910 break;
5911 }
5912 }
5913 }
5914 }
5915 else if (op & MOVE_TO_Y)
5916 {
5917 struct it it_backup;
5918
5919 /* TO_Y specified means stop at TO_X in the line containing
5920 TO_Y---or at TO_CHARPOS if this is reached first. The
5921 problem is that we can't really tell whether the line
5922 contains TO_Y before we have completely scanned it, and
5923 this may skip past TO_X. What we do is to first scan to
5924 TO_X.
5925
5926 If TO_X is not specified, use a TO_X of zero. The reason
5927 is to make the outcome of this function more predictable.
5928 If we didn't use TO_X == 0, we would stop at the end of
5929 the line which is probably not what a caller would expect
5930 to happen. */
5931 skip = move_it_in_display_line_to (it, to_charpos,
5932 ((op & MOVE_TO_X)
5933 ? to_x : 0),
5934 (MOVE_TO_X
5935 | (op & MOVE_TO_POS)));
5936
5937 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5938 if (skip == MOVE_POS_MATCH_OR_ZV)
5939 {
5940 reached = 5;
5941 break;
5942 }
5943
5944 /* If TO_X was reached, we would like to know whether TO_Y
5945 is in the line. This can only be said if we know the
5946 total line height which requires us to scan the rest of
5947 the line. */
5948 if (skip == MOVE_X_REACHED)
5949 {
5950 it_backup = *it;
5951 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5952 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5953 op & MOVE_TO_POS);
5954 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5955 }
5956
5957 /* Now, decide whether TO_Y is in this line. */
5958 line_height = it->max_ascent + it->max_descent;
5959 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5960
5961 if (to_y >= it->current_y
5962 && to_y < it->current_y + line_height)
5963 {
5964 if (skip == MOVE_X_REACHED)
5965 /* If TO_Y is in this line and TO_X was reached above,
5966 we scanned too far. We have to restore IT's settings
5967 to the ones before skipping. */
5968 *it = it_backup;
5969 reached = 6;
5970 }
5971 else if (skip == MOVE_X_REACHED)
5972 {
5973 skip = skip2;
5974 if (skip == MOVE_POS_MATCH_OR_ZV)
5975 reached = 7;
5976 }
5977
5978 if (reached)
5979 break;
5980 }
5981 else
5982 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5983
5984 switch (skip)
5985 {
5986 case MOVE_POS_MATCH_OR_ZV:
5987 reached = 8;
5988 goto out;
5989
5990 case MOVE_NEWLINE_OR_CR:
5991 set_iterator_to_next (it, 1);
5992 it->continuation_lines_width = 0;
5993 break;
5994
5995 case MOVE_LINE_TRUNCATED:
5996 it->continuation_lines_width = 0;
5997 reseat_at_next_visible_line_start (it, 0);
5998 if ((op & MOVE_TO_POS) != 0
5999 && IT_CHARPOS (*it) > to_charpos)
6000 {
6001 reached = 9;
6002 goto out;
6003 }
6004 break;
6005
6006 case MOVE_LINE_CONTINUED:
6007 it->continuation_lines_width += it->current_x;
6008 break;
6009
6010 default:
6011 abort ();
6012 }
6013
6014 /* Reset/increment for the next run. */
6015 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6016 it->current_x = it->hpos = 0;
6017 it->current_y += it->max_ascent + it->max_descent;
6018 ++it->vpos;
6019 last_height = it->max_ascent + it->max_descent;
6020 last_max_ascent = it->max_ascent;
6021 it->max_ascent = it->max_descent = 0;
6022 }
6023
6024 out:
6025
6026 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6027 }
6028
6029
6030 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6031
6032 If DY > 0, move IT backward at least that many pixels. DY = 0
6033 means move IT backward to the preceding line start or BEGV. This
6034 function may move over more than DY pixels if IT->current_y - DY
6035 ends up in the middle of a line; in this case IT->current_y will be
6036 set to the top of the line moved to. */
6037
6038 void
6039 move_it_vertically_backward (it, dy)
6040 struct it *it;
6041 int dy;
6042 {
6043 int nlines, h;
6044 struct it it2, it3;
6045 int start_pos = IT_CHARPOS (*it);
6046
6047 xassert (dy >= 0);
6048
6049 /* Estimate how many newlines we must move back. */
6050 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6051
6052 /* Set the iterator's position that many lines back. */
6053 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6054 back_to_previous_visible_line_start (it);
6055
6056 /* Reseat the iterator here. When moving backward, we don't want
6057 reseat to skip forward over invisible text, set up the iterator
6058 to deliver from overlay strings at the new position etc. So,
6059 use reseat_1 here. */
6060 reseat_1 (it, it->current.pos, 1);
6061
6062 /* We are now surely at a line start. */
6063 it->current_x = it->hpos = 0;
6064 it->continuation_lines_width = 0;
6065
6066 /* Move forward and see what y-distance we moved. First move to the
6067 start of the next line so that we get its height. We need this
6068 height to be able to tell whether we reached the specified
6069 y-distance. */
6070 it2 = *it;
6071 it2.max_ascent = it2.max_descent = 0;
6072 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6073 MOVE_TO_POS | MOVE_TO_VPOS);
6074 xassert (IT_CHARPOS (*it) >= BEGV);
6075 it3 = it2;
6076
6077 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6078 xassert (IT_CHARPOS (*it) >= BEGV);
6079 /* H is the actual vertical distance from the position in *IT
6080 and the starting position. */
6081 h = it2.current_y - it->current_y;
6082 /* NLINES is the distance in number of lines. */
6083 nlines = it2.vpos - it->vpos;
6084
6085 /* Correct IT's y and vpos position
6086 so that they are relative to the starting point. */
6087 it->vpos -= nlines;
6088 it->current_y -= h;
6089
6090 if (dy == 0)
6091 {
6092 /* DY == 0 means move to the start of the screen line. The
6093 value of nlines is > 0 if continuation lines were involved. */
6094 if (nlines > 0)
6095 move_it_by_lines (it, nlines, 1);
6096 xassert (IT_CHARPOS (*it) <= start_pos);
6097 }
6098 else
6099 {
6100 /* The y-position we try to reach, relative to *IT.
6101 Note that H has been subtracted in front of the if-statement. */
6102 int target_y = it->current_y + h - dy;
6103 int y0 = it3.current_y;
6104 int y1 = line_bottom_y (&it3);
6105 int line_height = y1 - y0;
6106
6107 /* If we did not reach target_y, try to move further backward if
6108 we can. If we moved too far backward, try to move forward. */
6109 if (target_y < it->current_y
6110 /* This is heuristic. In a window that's 3 lines high, with
6111 a line height of 13 pixels each, recentering with point
6112 on the bottom line will try to move -39/2 = 19 pixels
6113 backward. Try to avoid moving into the first line. */
6114 && it->current_y - target_y > line_height / 3 * 2
6115 && IT_CHARPOS (*it) > BEGV)
6116 {
6117 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6118 target_y - it->current_y));
6119 move_it_vertically (it, target_y - it->current_y);
6120 xassert (IT_CHARPOS (*it) >= BEGV);
6121 }
6122 else if (target_y >= it->current_y + line_height
6123 && IT_CHARPOS (*it) < ZV)
6124 {
6125 /* Should move forward by at least one line, maybe more.
6126
6127 Note: Calling move_it_by_lines can be expensive on
6128 terminal frames, where compute_motion is used (via
6129 vmotion) to do the job, when there are very long lines
6130 and truncate-lines is nil. That's the reason for
6131 treating terminal frames specially here. */
6132
6133 if (!FRAME_WINDOW_P (it->f))
6134 move_it_vertically (it, target_y - (it->current_y + line_height));
6135 else
6136 {
6137 do
6138 {
6139 move_it_by_lines (it, 1, 1);
6140 }
6141 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6142 }
6143
6144 xassert (IT_CHARPOS (*it) >= BEGV);
6145 }
6146 }
6147 }
6148
6149
6150 /* Move IT by a specified amount of pixel lines DY. DY negative means
6151 move backwards. DY = 0 means move to start of screen line. At the
6152 end, IT will be on the start of a screen line. */
6153
6154 void
6155 move_it_vertically (it, dy)
6156 struct it *it;
6157 int dy;
6158 {
6159 if (dy <= 0)
6160 move_it_vertically_backward (it, -dy);
6161 else if (dy > 0)
6162 {
6163 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6164 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6165 MOVE_TO_POS | MOVE_TO_Y);
6166 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6167
6168 /* If buffer ends in ZV without a newline, move to the start of
6169 the line to satisfy the post-condition. */
6170 if (IT_CHARPOS (*it) == ZV
6171 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6172 move_it_by_lines (it, 0, 0);
6173 }
6174 }
6175
6176
6177 /* Move iterator IT past the end of the text line it is in. */
6178
6179 void
6180 move_it_past_eol (it)
6181 struct it *it;
6182 {
6183 enum move_it_result rc;
6184
6185 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6186 if (rc == MOVE_NEWLINE_OR_CR)
6187 set_iterator_to_next (it, 0);
6188 }
6189
6190
6191 #if 0 /* Currently not used. */
6192
6193 /* Return non-zero if some text between buffer positions START_CHARPOS
6194 and END_CHARPOS is invisible. IT->window is the window for text
6195 property lookup. */
6196
6197 static int
6198 invisible_text_between_p (it, start_charpos, end_charpos)
6199 struct it *it;
6200 int start_charpos, end_charpos;
6201 {
6202 Lisp_Object prop, limit;
6203 int invisible_found_p;
6204
6205 xassert (it != NULL && start_charpos <= end_charpos);
6206
6207 /* Is text at START invisible? */
6208 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6209 it->window);
6210 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6211 invisible_found_p = 1;
6212 else
6213 {
6214 limit = Fnext_single_char_property_change (make_number (start_charpos),
6215 Qinvisible, Qnil,
6216 make_number (end_charpos));
6217 invisible_found_p = XFASTINT (limit) < end_charpos;
6218 }
6219
6220 return invisible_found_p;
6221 }
6222
6223 #endif /* 0 */
6224
6225
6226 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6227 negative means move up. DVPOS == 0 means move to the start of the
6228 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6229 NEED_Y_P is zero, IT->current_y will be left unchanged.
6230
6231 Further optimization ideas: If we would know that IT->f doesn't use
6232 a face with proportional font, we could be faster for
6233 truncate-lines nil. */
6234
6235 void
6236 move_it_by_lines (it, dvpos, need_y_p)
6237 struct it *it;
6238 int dvpos, need_y_p;
6239 {
6240 struct position pos;
6241
6242 if (!FRAME_WINDOW_P (it->f))
6243 {
6244 struct text_pos textpos;
6245
6246 /* We can use vmotion on frames without proportional fonts. */
6247 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6248 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6249 reseat (it, textpos, 1);
6250 it->vpos += pos.vpos;
6251 it->current_y += pos.vpos;
6252 }
6253 else if (dvpos == 0)
6254 {
6255 /* DVPOS == 0 means move to the start of the screen line. */
6256 move_it_vertically_backward (it, 0);
6257 xassert (it->current_x == 0 && it->hpos == 0);
6258 }
6259 else if (dvpos > 0)
6260 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6261 else
6262 {
6263 struct it it2;
6264 int start_charpos, i;
6265
6266 /* Start at the beginning of the screen line containing IT's
6267 position. */
6268 move_it_vertically_backward (it, 0);
6269
6270 /* Go back -DVPOS visible lines and reseat the iterator there. */
6271 start_charpos = IT_CHARPOS (*it);
6272 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6273 back_to_previous_visible_line_start (it);
6274 reseat (it, it->current.pos, 1);
6275 it->current_x = it->hpos = 0;
6276
6277 /* Above call may have moved too far if continuation lines
6278 are involved. Scan forward and see if it did. */
6279 it2 = *it;
6280 it2.vpos = it2.current_y = 0;
6281 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6282 it->vpos -= it2.vpos;
6283 it->current_y -= it2.current_y;
6284 it->current_x = it->hpos = 0;
6285
6286 /* If we moved too far, move IT some lines forward. */
6287 if (it2.vpos > -dvpos)
6288 {
6289 int delta = it2.vpos + dvpos;
6290 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6291 }
6292 }
6293 }
6294
6295 /* Return 1 if IT points into the middle of a display vector. */
6296
6297 int
6298 in_display_vector_p (it)
6299 struct it *it;
6300 {
6301 return (it->method == next_element_from_display_vector
6302 && it->current.dpvec_index > 0
6303 && it->dpvec + it->current.dpvec_index != it->dpend);
6304 }
6305
6306 \f
6307 /***********************************************************************
6308 Messages
6309 ***********************************************************************/
6310
6311
6312 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6313 to *Messages*. */
6314
6315 void
6316 add_to_log (format, arg1, arg2)
6317 char *format;
6318 Lisp_Object arg1, arg2;
6319 {
6320 Lisp_Object args[3];
6321 Lisp_Object msg, fmt;
6322 char *buffer;
6323 int len;
6324 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6325
6326 /* Do nothing if called asynchronously. Inserting text into
6327 a buffer may call after-change-functions and alike and
6328 that would means running Lisp asynchronously. */
6329 if (handling_signal)
6330 return;
6331
6332 fmt = msg = Qnil;
6333 GCPRO4 (fmt, msg, arg1, arg2);
6334
6335 args[0] = fmt = build_string (format);
6336 args[1] = arg1;
6337 args[2] = arg2;
6338 msg = Fformat (3, args);
6339
6340 len = SBYTES (msg) + 1;
6341 buffer = (char *) alloca (len);
6342 bcopy (SDATA (msg), buffer, len);
6343
6344 message_dolog (buffer, len - 1, 1, 0);
6345 UNGCPRO;
6346 }
6347
6348
6349 /* Output a newline in the *Messages* buffer if "needs" one. */
6350
6351 void
6352 message_log_maybe_newline ()
6353 {
6354 if (message_log_need_newline)
6355 message_dolog ("", 0, 1, 0);
6356 }
6357
6358
6359 /* Add a string M of length NBYTES to the message log, optionally
6360 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6361 nonzero, means interpret the contents of M as multibyte. This
6362 function calls low-level routines in order to bypass text property
6363 hooks, etc. which might not be safe to run. */
6364
6365 void
6366 message_dolog (m, nbytes, nlflag, multibyte)
6367 const char *m;
6368 int nbytes, nlflag, multibyte;
6369 {
6370 if (!NILP (Vmemory_full))
6371 return;
6372
6373 if (!NILP (Vmessage_log_max))
6374 {
6375 struct buffer *oldbuf;
6376 Lisp_Object oldpoint, oldbegv, oldzv;
6377 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6378 int point_at_end = 0;
6379 int zv_at_end = 0;
6380 Lisp_Object old_deactivate_mark, tem;
6381 struct gcpro gcpro1;
6382
6383 old_deactivate_mark = Vdeactivate_mark;
6384 oldbuf = current_buffer;
6385 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6386 current_buffer->undo_list = Qt;
6387
6388 oldpoint = message_dolog_marker1;
6389 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6390 oldbegv = message_dolog_marker2;
6391 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6392 oldzv = message_dolog_marker3;
6393 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6394 GCPRO1 (old_deactivate_mark);
6395
6396 if (PT == Z)
6397 point_at_end = 1;
6398 if (ZV == Z)
6399 zv_at_end = 1;
6400
6401 BEGV = BEG;
6402 BEGV_BYTE = BEG_BYTE;
6403 ZV = Z;
6404 ZV_BYTE = Z_BYTE;
6405 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6406
6407 /* Insert the string--maybe converting multibyte to single byte
6408 or vice versa, so that all the text fits the buffer. */
6409 if (multibyte
6410 && NILP (current_buffer->enable_multibyte_characters))
6411 {
6412 int i, c, char_bytes;
6413 unsigned char work[1];
6414
6415 /* Convert a multibyte string to single-byte
6416 for the *Message* buffer. */
6417 for (i = 0; i < nbytes; i += char_bytes)
6418 {
6419 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6420 work[0] = (SINGLE_BYTE_CHAR_P (c)
6421 ? c
6422 : multibyte_char_to_unibyte (c, Qnil));
6423 insert_1_both (work, 1, 1, 1, 0, 0);
6424 }
6425 }
6426 else if (! multibyte
6427 && ! NILP (current_buffer->enable_multibyte_characters))
6428 {
6429 int i, c, char_bytes;
6430 unsigned char *msg = (unsigned char *) m;
6431 unsigned char str[MAX_MULTIBYTE_LENGTH];
6432 /* Convert a single-byte string to multibyte
6433 for the *Message* buffer. */
6434 for (i = 0; i < nbytes; i++)
6435 {
6436 c = unibyte_char_to_multibyte (msg[i]);
6437 char_bytes = CHAR_STRING (c, str);
6438 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6439 }
6440 }
6441 else if (nbytes)
6442 insert_1 (m, nbytes, 1, 0, 0);
6443
6444 if (nlflag)
6445 {
6446 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6447 insert_1 ("\n", 1, 1, 0, 0);
6448
6449 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6450 this_bol = PT;
6451 this_bol_byte = PT_BYTE;
6452
6453 /* See if this line duplicates the previous one.
6454 If so, combine duplicates. */
6455 if (this_bol > BEG)
6456 {
6457 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6458 prev_bol = PT;
6459 prev_bol_byte = PT_BYTE;
6460
6461 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6462 this_bol, this_bol_byte);
6463 if (dup)
6464 {
6465 del_range_both (prev_bol, prev_bol_byte,
6466 this_bol, this_bol_byte, 0);
6467 if (dup > 1)
6468 {
6469 char dupstr[40];
6470 int duplen;
6471
6472 /* If you change this format, don't forget to also
6473 change message_log_check_duplicate. */
6474 sprintf (dupstr, " [%d times]", dup);
6475 duplen = strlen (dupstr);
6476 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6477 insert_1 (dupstr, duplen, 1, 0, 1);
6478 }
6479 }
6480 }
6481
6482 /* If we have more than the desired maximum number of lines
6483 in the *Messages* buffer now, delete the oldest ones.
6484 This is safe because we don't have undo in this buffer. */
6485
6486 if (NATNUMP (Vmessage_log_max))
6487 {
6488 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6489 -XFASTINT (Vmessage_log_max) - 1, 0);
6490 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6491 }
6492 }
6493 BEGV = XMARKER (oldbegv)->charpos;
6494 BEGV_BYTE = marker_byte_position (oldbegv);
6495
6496 if (zv_at_end)
6497 {
6498 ZV = Z;
6499 ZV_BYTE = Z_BYTE;
6500 }
6501 else
6502 {
6503 ZV = XMARKER (oldzv)->charpos;
6504 ZV_BYTE = marker_byte_position (oldzv);
6505 }
6506
6507 if (point_at_end)
6508 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6509 else
6510 /* We can't do Fgoto_char (oldpoint) because it will run some
6511 Lisp code. */
6512 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6513 XMARKER (oldpoint)->bytepos);
6514
6515 UNGCPRO;
6516 unchain_marker (XMARKER (oldpoint));
6517 unchain_marker (XMARKER (oldbegv));
6518 unchain_marker (XMARKER (oldzv));
6519
6520 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6521 set_buffer_internal (oldbuf);
6522 if (NILP (tem))
6523 windows_or_buffers_changed = old_windows_or_buffers_changed;
6524 message_log_need_newline = !nlflag;
6525 Vdeactivate_mark = old_deactivate_mark;
6526 }
6527 }
6528
6529
6530 /* We are at the end of the buffer after just having inserted a newline.
6531 (Note: We depend on the fact we won't be crossing the gap.)
6532 Check to see if the most recent message looks a lot like the previous one.
6533 Return 0 if different, 1 if the new one should just replace it, or a
6534 value N > 1 if we should also append " [N times]". */
6535
6536 static int
6537 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6538 int prev_bol, this_bol;
6539 int prev_bol_byte, this_bol_byte;
6540 {
6541 int i;
6542 int len = Z_BYTE - 1 - this_bol_byte;
6543 int seen_dots = 0;
6544 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6545 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6546
6547 for (i = 0; i < len; i++)
6548 {
6549 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6550 seen_dots = 1;
6551 if (p1[i] != p2[i])
6552 return seen_dots;
6553 }
6554 p1 += len;
6555 if (*p1 == '\n')
6556 return 2;
6557 if (*p1++ == ' ' && *p1++ == '[')
6558 {
6559 int n = 0;
6560 while (*p1 >= '0' && *p1 <= '9')
6561 n = n * 10 + *p1++ - '0';
6562 if (strncmp (p1, " times]\n", 8) == 0)
6563 return n+1;
6564 }
6565 return 0;
6566 }
6567
6568
6569 /* Display an echo area message M with a specified length of NBYTES
6570 bytes. The string may include null characters. If M is 0, clear
6571 out any existing message, and let the mini-buffer text show
6572 through.
6573
6574 The buffer M must continue to exist until after the echo area gets
6575 cleared or some other message gets displayed there. This means do
6576 not pass text that is stored in a Lisp string; do not pass text in
6577 a buffer that was alloca'd. */
6578
6579 void
6580 message2 (m, nbytes, multibyte)
6581 const char *m;
6582 int nbytes;
6583 int multibyte;
6584 {
6585 /* First flush out any partial line written with print. */
6586 message_log_maybe_newline ();
6587 if (m)
6588 message_dolog (m, nbytes, 1, multibyte);
6589 message2_nolog (m, nbytes, multibyte);
6590 }
6591
6592
6593 /* The non-logging counterpart of message2. */
6594
6595 void
6596 message2_nolog (m, nbytes, multibyte)
6597 const char *m;
6598 int nbytes, multibyte;
6599 {
6600 struct frame *sf = SELECTED_FRAME ();
6601 message_enable_multibyte = multibyte;
6602
6603 if (noninteractive)
6604 {
6605 if (noninteractive_need_newline)
6606 putc ('\n', stderr);
6607 noninteractive_need_newline = 0;
6608 if (m)
6609 fwrite (m, nbytes, 1, stderr);
6610 if (cursor_in_echo_area == 0)
6611 fprintf (stderr, "\n");
6612 fflush (stderr);
6613 }
6614 /* A null message buffer means that the frame hasn't really been
6615 initialized yet. Error messages get reported properly by
6616 cmd_error, so this must be just an informative message; toss it. */
6617 else if (INTERACTIVE
6618 && sf->glyphs_initialized_p
6619 && FRAME_MESSAGE_BUF (sf))
6620 {
6621 Lisp_Object mini_window;
6622 struct frame *f;
6623
6624 /* Get the frame containing the mini-buffer
6625 that the selected frame is using. */
6626 mini_window = FRAME_MINIBUF_WINDOW (sf);
6627 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6628
6629 FRAME_SAMPLE_VISIBILITY (f);
6630 if (FRAME_VISIBLE_P (sf)
6631 && ! FRAME_VISIBLE_P (f))
6632 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6633
6634 if (m)
6635 {
6636 set_message (m, Qnil, nbytes, multibyte);
6637 if (minibuffer_auto_raise)
6638 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6639 }
6640 else
6641 clear_message (1, 1);
6642
6643 do_pending_window_change (0);
6644 echo_area_display (1);
6645 do_pending_window_change (0);
6646 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6647 (*frame_up_to_date_hook) (f);
6648 }
6649 }
6650
6651
6652 /* Display an echo area message M with a specified length of NBYTES
6653 bytes. The string may include null characters. If M is not a
6654 string, clear out any existing message, and let the mini-buffer
6655 text show through. */
6656
6657 void
6658 message3 (m, nbytes, multibyte)
6659 Lisp_Object m;
6660 int nbytes;
6661 int multibyte;
6662 {
6663 struct gcpro gcpro1;
6664
6665 GCPRO1 (m);
6666
6667 /* First flush out any partial line written with print. */
6668 message_log_maybe_newline ();
6669 if (STRINGP (m))
6670 message_dolog (SDATA (m), nbytes, 1, multibyte);
6671 message3_nolog (m, nbytes, multibyte);
6672
6673 UNGCPRO;
6674 }
6675
6676
6677 /* The non-logging version of message3. */
6678
6679 void
6680 message3_nolog (m, nbytes, multibyte)
6681 Lisp_Object m;
6682 int nbytes, multibyte;
6683 {
6684 struct frame *sf = SELECTED_FRAME ();
6685 message_enable_multibyte = multibyte;
6686
6687 if (noninteractive)
6688 {
6689 if (noninteractive_need_newline)
6690 putc ('\n', stderr);
6691 noninteractive_need_newline = 0;
6692 if (STRINGP (m))
6693 fwrite (SDATA (m), nbytes, 1, stderr);
6694 if (cursor_in_echo_area == 0)
6695 fprintf (stderr, "\n");
6696 fflush (stderr);
6697 }
6698 /* A null message buffer means that the frame hasn't really been
6699 initialized yet. Error messages get reported properly by
6700 cmd_error, so this must be just an informative message; toss it. */
6701 else if (INTERACTIVE
6702 && sf->glyphs_initialized_p
6703 && FRAME_MESSAGE_BUF (sf))
6704 {
6705 Lisp_Object mini_window;
6706 Lisp_Object frame;
6707 struct frame *f;
6708
6709 /* Get the frame containing the mini-buffer
6710 that the selected frame is using. */
6711 mini_window = FRAME_MINIBUF_WINDOW (sf);
6712 frame = XWINDOW (mini_window)->frame;
6713 f = XFRAME (frame);
6714
6715 FRAME_SAMPLE_VISIBILITY (f);
6716 if (FRAME_VISIBLE_P (sf)
6717 && !FRAME_VISIBLE_P (f))
6718 Fmake_frame_visible (frame);
6719
6720 if (STRINGP (m) && SCHARS (m) > 0)
6721 {
6722 set_message (NULL, m, nbytes, multibyte);
6723 if (minibuffer_auto_raise)
6724 Fraise_frame (frame);
6725 }
6726 else
6727 clear_message (1, 1);
6728
6729 do_pending_window_change (0);
6730 echo_area_display (1);
6731 do_pending_window_change (0);
6732 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6733 (*frame_up_to_date_hook) (f);
6734 }
6735 }
6736
6737
6738 /* Display a null-terminated echo area message M. If M is 0, clear
6739 out any existing message, and let the mini-buffer text show through.
6740
6741 The buffer M must continue to exist until after the echo area gets
6742 cleared or some other message gets displayed there. Do not pass
6743 text that is stored in a Lisp string. Do not pass text in a buffer
6744 that was alloca'd. */
6745
6746 void
6747 message1 (m)
6748 char *m;
6749 {
6750 message2 (m, (m ? strlen (m) : 0), 0);
6751 }
6752
6753
6754 /* The non-logging counterpart of message1. */
6755
6756 void
6757 message1_nolog (m)
6758 char *m;
6759 {
6760 message2_nolog (m, (m ? strlen (m) : 0), 0);
6761 }
6762
6763 /* Display a message M which contains a single %s
6764 which gets replaced with STRING. */
6765
6766 void
6767 message_with_string (m, string, log)
6768 char *m;
6769 Lisp_Object string;
6770 int log;
6771 {
6772 CHECK_STRING (string);
6773
6774 if (noninteractive)
6775 {
6776 if (m)
6777 {
6778 if (noninteractive_need_newline)
6779 putc ('\n', stderr);
6780 noninteractive_need_newline = 0;
6781 fprintf (stderr, m, SDATA (string));
6782 if (cursor_in_echo_area == 0)
6783 fprintf (stderr, "\n");
6784 fflush (stderr);
6785 }
6786 }
6787 else if (INTERACTIVE)
6788 {
6789 /* The frame whose minibuffer we're going to display the message on.
6790 It may be larger than the selected frame, so we need
6791 to use its buffer, not the selected frame's buffer. */
6792 Lisp_Object mini_window;
6793 struct frame *f, *sf = SELECTED_FRAME ();
6794
6795 /* Get the frame containing the minibuffer
6796 that the selected frame is using. */
6797 mini_window = FRAME_MINIBUF_WINDOW (sf);
6798 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6799
6800 /* A null message buffer means that the frame hasn't really been
6801 initialized yet. Error messages get reported properly by
6802 cmd_error, so this must be just an informative message; toss it. */
6803 if (FRAME_MESSAGE_BUF (f))
6804 {
6805 Lisp_Object args[2], message;
6806 struct gcpro gcpro1, gcpro2;
6807
6808 args[0] = build_string (m);
6809 args[1] = message = string;
6810 GCPRO2 (args[0], message);
6811 gcpro1.nvars = 2;
6812
6813 message = Fformat (2, args);
6814
6815 if (log)
6816 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6817 else
6818 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6819
6820 UNGCPRO;
6821
6822 /* Print should start at the beginning of the message
6823 buffer next time. */
6824 message_buf_print = 0;
6825 }
6826 }
6827 }
6828
6829
6830 /* Dump an informative message to the minibuf. If M is 0, clear out
6831 any existing message, and let the mini-buffer text show through. */
6832
6833 /* VARARGS 1 */
6834 void
6835 message (m, a1, a2, a3)
6836 char *m;
6837 EMACS_INT a1, a2, a3;
6838 {
6839 if (noninteractive)
6840 {
6841 if (m)
6842 {
6843 if (noninteractive_need_newline)
6844 putc ('\n', stderr);
6845 noninteractive_need_newline = 0;
6846 fprintf (stderr, m, a1, a2, a3);
6847 if (cursor_in_echo_area == 0)
6848 fprintf (stderr, "\n");
6849 fflush (stderr);
6850 }
6851 }
6852 else if (INTERACTIVE)
6853 {
6854 /* The frame whose mini-buffer we're going to display the message
6855 on. It may be larger than the selected frame, so we need to
6856 use its buffer, not the selected frame's buffer. */
6857 Lisp_Object mini_window;
6858 struct frame *f, *sf = SELECTED_FRAME ();
6859
6860 /* Get the frame containing the mini-buffer
6861 that the selected frame is using. */
6862 mini_window = FRAME_MINIBUF_WINDOW (sf);
6863 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6864
6865 /* A null message buffer means that the frame hasn't really been
6866 initialized yet. Error messages get reported properly by
6867 cmd_error, so this must be just an informative message; toss
6868 it. */
6869 if (FRAME_MESSAGE_BUF (f))
6870 {
6871 if (m)
6872 {
6873 int len;
6874 #ifdef NO_ARG_ARRAY
6875 char *a[3];
6876 a[0] = (char *) a1;
6877 a[1] = (char *) a2;
6878 a[2] = (char *) a3;
6879
6880 len = doprnt (FRAME_MESSAGE_BUF (f),
6881 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6882 #else
6883 len = doprnt (FRAME_MESSAGE_BUF (f),
6884 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6885 (char **) &a1);
6886 #endif /* NO_ARG_ARRAY */
6887
6888 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6889 }
6890 else
6891 message1 (0);
6892
6893 /* Print should start at the beginning of the message
6894 buffer next time. */
6895 message_buf_print = 0;
6896 }
6897 }
6898 }
6899
6900
6901 /* The non-logging version of message. */
6902
6903 void
6904 message_nolog (m, a1, a2, a3)
6905 char *m;
6906 EMACS_INT a1, a2, a3;
6907 {
6908 Lisp_Object old_log_max;
6909 old_log_max = Vmessage_log_max;
6910 Vmessage_log_max = Qnil;
6911 message (m, a1, a2, a3);
6912 Vmessage_log_max = old_log_max;
6913 }
6914
6915
6916 /* Display the current message in the current mini-buffer. This is
6917 only called from error handlers in process.c, and is not time
6918 critical. */
6919
6920 void
6921 update_echo_area ()
6922 {
6923 if (!NILP (echo_area_buffer[0]))
6924 {
6925 Lisp_Object string;
6926 string = Fcurrent_message ();
6927 message3 (string, SBYTES (string),
6928 !NILP (current_buffer->enable_multibyte_characters));
6929 }
6930 }
6931
6932
6933 /* Make sure echo area buffers in `echo_buffers' are live.
6934 If they aren't, make new ones. */
6935
6936 static void
6937 ensure_echo_area_buffers ()
6938 {
6939 int i;
6940
6941 for (i = 0; i < 2; ++i)
6942 if (!BUFFERP (echo_buffer[i])
6943 || NILP (XBUFFER (echo_buffer[i])->name))
6944 {
6945 char name[30];
6946 Lisp_Object old_buffer;
6947 int j;
6948
6949 old_buffer = echo_buffer[i];
6950 sprintf (name, " *Echo Area %d*", i);
6951 echo_buffer[i] = Fget_buffer_create (build_string (name));
6952 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6953
6954 for (j = 0; j < 2; ++j)
6955 if (EQ (old_buffer, echo_area_buffer[j]))
6956 echo_area_buffer[j] = echo_buffer[i];
6957 }
6958 }
6959
6960
6961 /* Call FN with args A1..A4 with either the current or last displayed
6962 echo_area_buffer as current buffer.
6963
6964 WHICH zero means use the current message buffer
6965 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6966 from echo_buffer[] and clear it.
6967
6968 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6969 suitable buffer from echo_buffer[] and clear it.
6970
6971 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6972 that the current message becomes the last displayed one, make
6973 choose a suitable buffer for echo_area_buffer[0], and clear it.
6974
6975 Value is what FN returns. */
6976
6977 static int
6978 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6979 struct window *w;
6980 int which;
6981 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6982 EMACS_INT a1;
6983 Lisp_Object a2;
6984 EMACS_INT a3, a4;
6985 {
6986 Lisp_Object buffer;
6987 int this_one, the_other, clear_buffer_p, rc;
6988 int count = SPECPDL_INDEX ();
6989
6990 /* If buffers aren't live, make new ones. */
6991 ensure_echo_area_buffers ();
6992
6993 clear_buffer_p = 0;
6994
6995 if (which == 0)
6996 this_one = 0, the_other = 1;
6997 else if (which > 0)
6998 this_one = 1, the_other = 0;
6999 else
7000 {
7001 this_one = 0, the_other = 1;
7002 clear_buffer_p = 1;
7003
7004 /* We need a fresh one in case the current echo buffer equals
7005 the one containing the last displayed echo area message. */
7006 if (!NILP (echo_area_buffer[this_one])
7007 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7008 echo_area_buffer[this_one] = Qnil;
7009 }
7010
7011 /* Choose a suitable buffer from echo_buffer[] is we don't
7012 have one. */
7013 if (NILP (echo_area_buffer[this_one]))
7014 {
7015 echo_area_buffer[this_one]
7016 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7017 ? echo_buffer[the_other]
7018 : echo_buffer[this_one]);
7019 clear_buffer_p = 1;
7020 }
7021
7022 buffer = echo_area_buffer[this_one];
7023
7024 /* Don't get confused by reusing the buffer used for echoing
7025 for a different purpose. */
7026 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7027 cancel_echoing ();
7028
7029 record_unwind_protect (unwind_with_echo_area_buffer,
7030 with_echo_area_buffer_unwind_data (w));
7031
7032 /* Make the echo area buffer current. Note that for display
7033 purposes, it is not necessary that the displayed window's buffer
7034 == current_buffer, except for text property lookup. So, let's
7035 only set that buffer temporarily here without doing a full
7036 Fset_window_buffer. We must also change w->pointm, though,
7037 because otherwise an assertions in unshow_buffer fails, and Emacs
7038 aborts. */
7039 set_buffer_internal_1 (XBUFFER (buffer));
7040 if (w)
7041 {
7042 w->buffer = buffer;
7043 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7044 }
7045
7046 current_buffer->undo_list = Qt;
7047 current_buffer->read_only = Qnil;
7048 specbind (Qinhibit_read_only, Qt);
7049 specbind (Qinhibit_modification_hooks, Qt);
7050
7051 if (clear_buffer_p && Z > BEG)
7052 del_range (BEG, Z);
7053
7054 xassert (BEGV >= BEG);
7055 xassert (ZV <= Z && ZV >= BEGV);
7056
7057 rc = fn (a1, a2, a3, a4);
7058
7059 xassert (BEGV >= BEG);
7060 xassert (ZV <= Z && ZV >= BEGV);
7061
7062 unbind_to (count, Qnil);
7063 return rc;
7064 }
7065
7066
7067 /* Save state that should be preserved around the call to the function
7068 FN called in with_echo_area_buffer. */
7069
7070 static Lisp_Object
7071 with_echo_area_buffer_unwind_data (w)
7072 struct window *w;
7073 {
7074 int i = 0;
7075 Lisp_Object vector;
7076
7077 /* Reduce consing by keeping one vector in
7078 Vwith_echo_area_save_vector. */
7079 vector = Vwith_echo_area_save_vector;
7080 Vwith_echo_area_save_vector = Qnil;
7081
7082 if (NILP (vector))
7083 vector = Fmake_vector (make_number (7), Qnil);
7084
7085 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7086 AREF (vector, i) = Vdeactivate_mark, ++i;
7087 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7088
7089 if (w)
7090 {
7091 XSETWINDOW (AREF (vector, i), w); ++i;
7092 AREF (vector, i) = w->buffer; ++i;
7093 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7094 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7095 }
7096 else
7097 {
7098 int end = i + 4;
7099 for (; i < end; ++i)
7100 AREF (vector, i) = Qnil;
7101 }
7102
7103 xassert (i == ASIZE (vector));
7104 return vector;
7105 }
7106
7107
7108 /* Restore global state from VECTOR which was created by
7109 with_echo_area_buffer_unwind_data. */
7110
7111 static Lisp_Object
7112 unwind_with_echo_area_buffer (vector)
7113 Lisp_Object vector;
7114 {
7115 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7116 Vdeactivate_mark = AREF (vector, 1);
7117 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7118
7119 if (WINDOWP (AREF (vector, 3)))
7120 {
7121 struct window *w;
7122 Lisp_Object buffer, charpos, bytepos;
7123
7124 w = XWINDOW (AREF (vector, 3));
7125 buffer = AREF (vector, 4);
7126 charpos = AREF (vector, 5);
7127 bytepos = AREF (vector, 6);
7128
7129 w->buffer = buffer;
7130 set_marker_both (w->pointm, buffer,
7131 XFASTINT (charpos), XFASTINT (bytepos));
7132 }
7133
7134 Vwith_echo_area_save_vector = vector;
7135 return Qnil;
7136 }
7137
7138
7139 /* Set up the echo area for use by print functions. MULTIBYTE_P
7140 non-zero means we will print multibyte. */
7141
7142 void
7143 setup_echo_area_for_printing (multibyte_p)
7144 int multibyte_p;
7145 {
7146 /* If we can't find an echo area any more, exit. */
7147 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7148 Fkill_emacs (Qnil);
7149
7150 ensure_echo_area_buffers ();
7151
7152 if (!message_buf_print)
7153 {
7154 /* A message has been output since the last time we printed.
7155 Choose a fresh echo area buffer. */
7156 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7157 echo_area_buffer[0] = echo_buffer[1];
7158 else
7159 echo_area_buffer[0] = echo_buffer[0];
7160
7161 /* Switch to that buffer and clear it. */
7162 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7163 current_buffer->truncate_lines = Qnil;
7164
7165 if (Z > BEG)
7166 {
7167 int count = SPECPDL_INDEX ();
7168 specbind (Qinhibit_read_only, Qt);
7169 /* Note that undo recording is always disabled. */
7170 del_range (BEG, Z);
7171 unbind_to (count, Qnil);
7172 }
7173 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7174
7175 /* Set up the buffer for the multibyteness we need. */
7176 if (multibyte_p
7177 != !NILP (current_buffer->enable_multibyte_characters))
7178 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7179
7180 /* Raise the frame containing the echo area. */
7181 if (minibuffer_auto_raise)
7182 {
7183 struct frame *sf = SELECTED_FRAME ();
7184 Lisp_Object mini_window;
7185 mini_window = FRAME_MINIBUF_WINDOW (sf);
7186 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7187 }
7188
7189 message_log_maybe_newline ();
7190 message_buf_print = 1;
7191 }
7192 else
7193 {
7194 if (NILP (echo_area_buffer[0]))
7195 {
7196 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7197 echo_area_buffer[0] = echo_buffer[1];
7198 else
7199 echo_area_buffer[0] = echo_buffer[0];
7200 }
7201
7202 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7203 {
7204 /* Someone switched buffers between print requests. */
7205 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7206 current_buffer->truncate_lines = Qnil;
7207 }
7208 }
7209 }
7210
7211
7212 /* Display an echo area message in window W. Value is non-zero if W's
7213 height is changed. If display_last_displayed_message_p is
7214 non-zero, display the message that was last displayed, otherwise
7215 display the current message. */
7216
7217 static int
7218 display_echo_area (w)
7219 struct window *w;
7220 {
7221 int i, no_message_p, window_height_changed_p, count;
7222
7223 /* Temporarily disable garbage collections while displaying the echo
7224 area. This is done because a GC can print a message itself.
7225 That message would modify the echo area buffer's contents while a
7226 redisplay of the buffer is going on, and seriously confuse
7227 redisplay. */
7228 count = inhibit_garbage_collection ();
7229
7230 /* If there is no message, we must call display_echo_area_1
7231 nevertheless because it resizes the window. But we will have to
7232 reset the echo_area_buffer in question to nil at the end because
7233 with_echo_area_buffer will sets it to an empty buffer. */
7234 i = display_last_displayed_message_p ? 1 : 0;
7235 no_message_p = NILP (echo_area_buffer[i]);
7236
7237 window_height_changed_p
7238 = with_echo_area_buffer (w, display_last_displayed_message_p,
7239 display_echo_area_1,
7240 (EMACS_INT) w, Qnil, 0, 0);
7241
7242 if (no_message_p)
7243 echo_area_buffer[i] = Qnil;
7244
7245 unbind_to (count, Qnil);
7246 return window_height_changed_p;
7247 }
7248
7249
7250 /* Helper for display_echo_area. Display the current buffer which
7251 contains the current echo area message in window W, a mini-window,
7252 a pointer to which is passed in A1. A2..A4 are currently not used.
7253 Change the height of W so that all of the message is displayed.
7254 Value is non-zero if height of W was changed. */
7255
7256 static int
7257 display_echo_area_1 (a1, a2, a3, a4)
7258 EMACS_INT a1;
7259 Lisp_Object a2;
7260 EMACS_INT a3, a4;
7261 {
7262 struct window *w = (struct window *) a1;
7263 Lisp_Object window;
7264 struct text_pos start;
7265 int window_height_changed_p = 0;
7266
7267 /* Do this before displaying, so that we have a large enough glyph
7268 matrix for the display. */
7269 window_height_changed_p = resize_mini_window (w, 0);
7270
7271 /* Display. */
7272 clear_glyph_matrix (w->desired_matrix);
7273 XSETWINDOW (window, w);
7274 SET_TEXT_POS (start, BEG, BEG_BYTE);
7275 try_window (window, start);
7276
7277 return window_height_changed_p;
7278 }
7279
7280
7281 /* Resize the echo area window to exactly the size needed for the
7282 currently displayed message, if there is one. If a mini-buffer
7283 is active, don't shrink it. */
7284
7285 void
7286 resize_echo_area_exactly ()
7287 {
7288 if (BUFFERP (echo_area_buffer[0])
7289 && WINDOWP (echo_area_window))
7290 {
7291 struct window *w = XWINDOW (echo_area_window);
7292 int resized_p;
7293 Lisp_Object resize_exactly;
7294
7295 if (minibuf_level == 0)
7296 resize_exactly = Qt;
7297 else
7298 resize_exactly = Qnil;
7299
7300 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7301 (EMACS_INT) w, resize_exactly, 0, 0);
7302 if (resized_p)
7303 {
7304 ++windows_or_buffers_changed;
7305 ++update_mode_lines;
7306 redisplay_internal (0);
7307 }
7308 }
7309 }
7310
7311
7312 /* Callback function for with_echo_area_buffer, when used from
7313 resize_echo_area_exactly. A1 contains a pointer to the window to
7314 resize, EXACTLY non-nil means resize the mini-window exactly to the
7315 size of the text displayed. A3 and A4 are not used. Value is what
7316 resize_mini_window returns. */
7317
7318 static int
7319 resize_mini_window_1 (a1, exactly, a3, a4)
7320 EMACS_INT a1;
7321 Lisp_Object exactly;
7322 EMACS_INT a3, a4;
7323 {
7324 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7325 }
7326
7327
7328 /* Resize mini-window W to fit the size of its contents. EXACT:P
7329 means size the window exactly to the size needed. Otherwise, it's
7330 only enlarged until W's buffer is empty. Value is non-zero if
7331 the window height has been changed. */
7332
7333 int
7334 resize_mini_window (w, exact_p)
7335 struct window *w;
7336 int exact_p;
7337 {
7338 struct frame *f = XFRAME (w->frame);
7339 int window_height_changed_p = 0;
7340
7341 xassert (MINI_WINDOW_P (w));
7342
7343 /* Don't resize windows while redisplaying a window; it would
7344 confuse redisplay functions when the size of the window they are
7345 displaying changes from under them. Such a resizing can happen,
7346 for instance, when which-func prints a long message while
7347 we are running fontification-functions. We're running these
7348 functions with safe_call which binds inhibit-redisplay to t. */
7349 if (!NILP (Vinhibit_redisplay))
7350 return 0;
7351
7352 /* Nil means don't try to resize. */
7353 if (NILP (Vresize_mini_windows)
7354 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7355 return 0;
7356
7357 if (!FRAME_MINIBUF_ONLY_P (f))
7358 {
7359 struct it it;
7360 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7361 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7362 int height, max_height;
7363 int unit = FRAME_LINE_HEIGHT (f);
7364 struct text_pos start;
7365 struct buffer *old_current_buffer = NULL;
7366
7367 if (current_buffer != XBUFFER (w->buffer))
7368 {
7369 old_current_buffer = current_buffer;
7370 set_buffer_internal (XBUFFER (w->buffer));
7371 }
7372
7373 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7374
7375 /* Compute the max. number of lines specified by the user. */
7376 if (FLOATP (Vmax_mini_window_height))
7377 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7378 else if (INTEGERP (Vmax_mini_window_height))
7379 max_height = XINT (Vmax_mini_window_height);
7380 else
7381 max_height = total_height / 4;
7382
7383 /* Correct that max. height if it's bogus. */
7384 max_height = max (1, max_height);
7385 max_height = min (total_height, max_height);
7386
7387 /* Find out the height of the text in the window. */
7388 if (it.truncate_lines_p)
7389 height = 1;
7390 else
7391 {
7392 last_height = 0;
7393 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7394 if (it.max_ascent == 0 && it.max_descent == 0)
7395 height = it.current_y + last_height;
7396 else
7397 height = it.current_y + it.max_ascent + it.max_descent;
7398 height -= it.extra_line_spacing;
7399 height = (height + unit - 1) / unit;
7400 }
7401
7402 /* Compute a suitable window start. */
7403 if (height > max_height)
7404 {
7405 height = max_height;
7406 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7407 move_it_vertically_backward (&it, (height - 1) * unit);
7408 start = it.current.pos;
7409 }
7410 else
7411 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7412 SET_MARKER_FROM_TEXT_POS (w->start, start);
7413
7414 if (EQ (Vresize_mini_windows, Qgrow_only))
7415 {
7416 /* Let it grow only, until we display an empty message, in which
7417 case the window shrinks again. */
7418 if (height > WINDOW_TOTAL_LINES (w))
7419 {
7420 int old_height = WINDOW_TOTAL_LINES (w);
7421 freeze_window_starts (f, 1);
7422 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7423 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7424 }
7425 else if (height < WINDOW_TOTAL_LINES (w)
7426 && (exact_p || BEGV == ZV))
7427 {
7428 int old_height = WINDOW_TOTAL_LINES (w);
7429 freeze_window_starts (f, 0);
7430 shrink_mini_window (w);
7431 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7432 }
7433 }
7434 else
7435 {
7436 /* Always resize to exact size needed. */
7437 if (height > WINDOW_TOTAL_LINES (w))
7438 {
7439 int old_height = WINDOW_TOTAL_LINES (w);
7440 freeze_window_starts (f, 1);
7441 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7442 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7443 }
7444 else if (height < WINDOW_TOTAL_LINES (w))
7445 {
7446 int old_height = WINDOW_TOTAL_LINES (w);
7447 freeze_window_starts (f, 0);
7448 shrink_mini_window (w);
7449
7450 if (height)
7451 {
7452 freeze_window_starts (f, 1);
7453 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7454 }
7455
7456 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7457 }
7458 }
7459
7460 if (old_current_buffer)
7461 set_buffer_internal (old_current_buffer);
7462 }
7463
7464 return window_height_changed_p;
7465 }
7466
7467
7468 /* Value is the current message, a string, or nil if there is no
7469 current message. */
7470
7471 Lisp_Object
7472 current_message ()
7473 {
7474 Lisp_Object msg;
7475
7476 if (NILP (echo_area_buffer[0]))
7477 msg = Qnil;
7478 else
7479 {
7480 with_echo_area_buffer (0, 0, current_message_1,
7481 (EMACS_INT) &msg, Qnil, 0, 0);
7482 if (NILP (msg))
7483 echo_area_buffer[0] = Qnil;
7484 }
7485
7486 return msg;
7487 }
7488
7489
7490 static int
7491 current_message_1 (a1, a2, a3, a4)
7492 EMACS_INT a1;
7493 Lisp_Object a2;
7494 EMACS_INT a3, a4;
7495 {
7496 Lisp_Object *msg = (Lisp_Object *) a1;
7497
7498 if (Z > BEG)
7499 *msg = make_buffer_string (BEG, Z, 1);
7500 else
7501 *msg = Qnil;
7502 return 0;
7503 }
7504
7505
7506 /* Push the current message on Vmessage_stack for later restauration
7507 by restore_message. Value is non-zero if the current message isn't
7508 empty. This is a relatively infrequent operation, so it's not
7509 worth optimizing. */
7510
7511 int
7512 push_message ()
7513 {
7514 Lisp_Object msg;
7515 msg = current_message ();
7516 Vmessage_stack = Fcons (msg, Vmessage_stack);
7517 return STRINGP (msg);
7518 }
7519
7520
7521 /* Restore message display from the top of Vmessage_stack. */
7522
7523 void
7524 restore_message ()
7525 {
7526 Lisp_Object msg;
7527
7528 xassert (CONSP (Vmessage_stack));
7529 msg = XCAR (Vmessage_stack);
7530 if (STRINGP (msg))
7531 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7532 else
7533 message3_nolog (msg, 0, 0);
7534 }
7535
7536
7537 /* Handler for record_unwind_protect calling pop_message. */
7538
7539 Lisp_Object
7540 pop_message_unwind (dummy)
7541 Lisp_Object dummy;
7542 {
7543 pop_message ();
7544 return Qnil;
7545 }
7546
7547 /* Pop the top-most entry off Vmessage_stack. */
7548
7549 void
7550 pop_message ()
7551 {
7552 xassert (CONSP (Vmessage_stack));
7553 Vmessage_stack = XCDR (Vmessage_stack);
7554 }
7555
7556
7557 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7558 exits. If the stack is not empty, we have a missing pop_message
7559 somewhere. */
7560
7561 void
7562 check_message_stack ()
7563 {
7564 if (!NILP (Vmessage_stack))
7565 abort ();
7566 }
7567
7568
7569 /* Truncate to NCHARS what will be displayed in the echo area the next
7570 time we display it---but don't redisplay it now. */
7571
7572 void
7573 truncate_echo_area (nchars)
7574 int nchars;
7575 {
7576 if (nchars == 0)
7577 echo_area_buffer[0] = Qnil;
7578 /* A null message buffer means that the frame hasn't really been
7579 initialized yet. Error messages get reported properly by
7580 cmd_error, so this must be just an informative message; toss it. */
7581 else if (!noninteractive
7582 && INTERACTIVE
7583 && !NILP (echo_area_buffer[0]))
7584 {
7585 struct frame *sf = SELECTED_FRAME ();
7586 if (FRAME_MESSAGE_BUF (sf))
7587 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7588 }
7589 }
7590
7591
7592 /* Helper function for truncate_echo_area. Truncate the current
7593 message to at most NCHARS characters. */
7594
7595 static int
7596 truncate_message_1 (nchars, a2, a3, a4)
7597 EMACS_INT nchars;
7598 Lisp_Object a2;
7599 EMACS_INT a3, a4;
7600 {
7601 if (BEG + nchars < Z)
7602 del_range (BEG + nchars, Z);
7603 if (Z == BEG)
7604 echo_area_buffer[0] = Qnil;
7605 return 0;
7606 }
7607
7608
7609 /* Set the current message to a substring of S or STRING.
7610
7611 If STRING is a Lisp string, set the message to the first NBYTES
7612 bytes from STRING. NBYTES zero means use the whole string. If
7613 STRING is multibyte, the message will be displayed multibyte.
7614
7615 If S is not null, set the message to the first LEN bytes of S. LEN
7616 zero means use the whole string. MULTIBYTE_P non-zero means S is
7617 multibyte. Display the message multibyte in that case. */
7618
7619 void
7620 set_message (s, string, nbytes, multibyte_p)
7621 const char *s;
7622 Lisp_Object string;
7623 int nbytes, multibyte_p;
7624 {
7625 message_enable_multibyte
7626 = ((s && multibyte_p)
7627 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7628
7629 with_echo_area_buffer (0, -1, set_message_1,
7630 (EMACS_INT) s, string, nbytes, multibyte_p);
7631 message_buf_print = 0;
7632 help_echo_showing_p = 0;
7633 }
7634
7635
7636 /* Helper function for set_message. Arguments have the same meaning
7637 as there, with A1 corresponding to S and A2 corresponding to STRING
7638 This function is called with the echo area buffer being
7639 current. */
7640
7641 static int
7642 set_message_1 (a1, a2, nbytes, multibyte_p)
7643 EMACS_INT a1;
7644 Lisp_Object a2;
7645 EMACS_INT nbytes, multibyte_p;
7646 {
7647 const char *s = (const char *) a1;
7648 Lisp_Object string = a2;
7649
7650 xassert (BEG == Z);
7651
7652 /* Change multibyteness of the echo buffer appropriately. */
7653 if (message_enable_multibyte
7654 != !NILP (current_buffer->enable_multibyte_characters))
7655 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7656
7657 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7658
7659 /* Insert new message at BEG. */
7660 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7661
7662 if (STRINGP (string))
7663 {
7664 int nchars;
7665
7666 if (nbytes == 0)
7667 nbytes = SBYTES (string);
7668 nchars = string_byte_to_char (string, nbytes);
7669
7670 /* This function takes care of single/multibyte conversion. We
7671 just have to ensure that the echo area buffer has the right
7672 setting of enable_multibyte_characters. */
7673 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7674 }
7675 else if (s)
7676 {
7677 if (nbytes == 0)
7678 nbytes = strlen (s);
7679
7680 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7681 {
7682 /* Convert from multi-byte to single-byte. */
7683 int i, c, n;
7684 unsigned char work[1];
7685
7686 /* Convert a multibyte string to single-byte. */
7687 for (i = 0; i < nbytes; i += n)
7688 {
7689 c = string_char_and_length (s + i, nbytes - i, &n);
7690 work[0] = (SINGLE_BYTE_CHAR_P (c)
7691 ? c
7692 : multibyte_char_to_unibyte (c, Qnil));
7693 insert_1_both (work, 1, 1, 1, 0, 0);
7694 }
7695 }
7696 else if (!multibyte_p
7697 && !NILP (current_buffer->enable_multibyte_characters))
7698 {
7699 /* Convert from single-byte to multi-byte. */
7700 int i, c, n;
7701 const unsigned char *msg = (const unsigned char *) s;
7702 unsigned char str[MAX_MULTIBYTE_LENGTH];
7703
7704 /* Convert a single-byte string to multibyte. */
7705 for (i = 0; i < nbytes; i++)
7706 {
7707 c = unibyte_char_to_multibyte (msg[i]);
7708 n = CHAR_STRING (c, str);
7709 insert_1_both (str, 1, n, 1, 0, 0);
7710 }
7711 }
7712 else
7713 insert_1 (s, nbytes, 1, 0, 0);
7714 }
7715
7716 return 0;
7717 }
7718
7719
7720 /* Clear messages. CURRENT_P non-zero means clear the current
7721 message. LAST_DISPLAYED_P non-zero means clear the message
7722 last displayed. */
7723
7724 void
7725 clear_message (current_p, last_displayed_p)
7726 int current_p, last_displayed_p;
7727 {
7728 if (current_p)
7729 {
7730 echo_area_buffer[0] = Qnil;
7731 message_cleared_p = 1;
7732 }
7733
7734 if (last_displayed_p)
7735 echo_area_buffer[1] = Qnil;
7736
7737 message_buf_print = 0;
7738 }
7739
7740 /* Clear garbaged frames.
7741
7742 This function is used where the old redisplay called
7743 redraw_garbaged_frames which in turn called redraw_frame which in
7744 turn called clear_frame. The call to clear_frame was a source of
7745 flickering. I believe a clear_frame is not necessary. It should
7746 suffice in the new redisplay to invalidate all current matrices,
7747 and ensure a complete redisplay of all windows. */
7748
7749 static void
7750 clear_garbaged_frames ()
7751 {
7752 if (frame_garbaged)
7753 {
7754 Lisp_Object tail, frame;
7755 int changed_count = 0;
7756
7757 FOR_EACH_FRAME (tail, frame)
7758 {
7759 struct frame *f = XFRAME (frame);
7760
7761 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7762 {
7763 if (f->resized_p)
7764 {
7765 Fredraw_frame (frame);
7766 f->force_flush_display_p = 1;
7767 }
7768 clear_current_matrices (f);
7769 changed_count++;
7770 f->garbaged = 0;
7771 f->resized_p = 0;
7772 }
7773 }
7774
7775 frame_garbaged = 0;
7776 if (changed_count)
7777 ++windows_or_buffers_changed;
7778 }
7779 }
7780
7781
7782 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7783 is non-zero update selected_frame. Value is non-zero if the
7784 mini-windows height has been changed. */
7785
7786 static int
7787 echo_area_display (update_frame_p)
7788 int update_frame_p;
7789 {
7790 Lisp_Object mini_window;
7791 struct window *w;
7792 struct frame *f;
7793 int window_height_changed_p = 0;
7794 struct frame *sf = SELECTED_FRAME ();
7795
7796 mini_window = FRAME_MINIBUF_WINDOW (sf);
7797 w = XWINDOW (mini_window);
7798 f = XFRAME (WINDOW_FRAME (w));
7799
7800 /* Don't display if frame is invisible or not yet initialized. */
7801 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7802 return 0;
7803
7804 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7805 #ifndef MAC_OS8
7806 #ifdef HAVE_WINDOW_SYSTEM
7807 /* When Emacs starts, selected_frame may be a visible terminal
7808 frame, even if we run under a window system. If we let this
7809 through, a message would be displayed on the terminal. */
7810 if (EQ (selected_frame, Vterminal_frame)
7811 && !NILP (Vwindow_system))
7812 return 0;
7813 #endif /* HAVE_WINDOW_SYSTEM */
7814 #endif
7815
7816 /* Redraw garbaged frames. */
7817 if (frame_garbaged)
7818 clear_garbaged_frames ();
7819
7820 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7821 {
7822 echo_area_window = mini_window;
7823 window_height_changed_p = display_echo_area (w);
7824 w->must_be_updated_p = 1;
7825
7826 /* Update the display, unless called from redisplay_internal.
7827 Also don't update the screen during redisplay itself. The
7828 update will happen at the end of redisplay, and an update
7829 here could cause confusion. */
7830 if (update_frame_p && !redisplaying_p)
7831 {
7832 int n = 0;
7833
7834 /* If the display update has been interrupted by pending
7835 input, update mode lines in the frame. Due to the
7836 pending input, it might have been that redisplay hasn't
7837 been called, so that mode lines above the echo area are
7838 garbaged. This looks odd, so we prevent it here. */
7839 if (!display_completed)
7840 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7841
7842 if (window_height_changed_p
7843 /* Don't do this if Emacs is shutting down. Redisplay
7844 needs to run hooks. */
7845 && !NILP (Vrun_hooks))
7846 {
7847 /* Must update other windows. Likewise as in other
7848 cases, don't let this update be interrupted by
7849 pending input. */
7850 int count = SPECPDL_INDEX ();
7851 specbind (Qredisplay_dont_pause, Qt);
7852 windows_or_buffers_changed = 1;
7853 redisplay_internal (0);
7854 unbind_to (count, Qnil);
7855 }
7856 else if (FRAME_WINDOW_P (f) && n == 0)
7857 {
7858 /* Window configuration is the same as before.
7859 Can do with a display update of the echo area,
7860 unless we displayed some mode lines. */
7861 update_single_window (w, 1);
7862 rif->flush_display (f);
7863 }
7864 else
7865 update_frame (f, 1, 1);
7866
7867 /* If cursor is in the echo area, make sure that the next
7868 redisplay displays the minibuffer, so that the cursor will
7869 be replaced with what the minibuffer wants. */
7870 if (cursor_in_echo_area)
7871 ++windows_or_buffers_changed;
7872 }
7873 }
7874 else if (!EQ (mini_window, selected_window))
7875 windows_or_buffers_changed++;
7876
7877 /* Last displayed message is now the current message. */
7878 echo_area_buffer[1] = echo_area_buffer[0];
7879
7880 /* Prevent redisplay optimization in redisplay_internal by resetting
7881 this_line_start_pos. This is done because the mini-buffer now
7882 displays the message instead of its buffer text. */
7883 if (EQ (mini_window, selected_window))
7884 CHARPOS (this_line_start_pos) = 0;
7885
7886 return window_height_changed_p;
7887 }
7888
7889
7890 \f
7891 /***********************************************************************
7892 Frame Titles
7893 ***********************************************************************/
7894
7895
7896 /* The frame title buffering code is also used by Fformat_mode_line.
7897 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7898
7899 /* A buffer for constructing frame titles in it; allocated from the
7900 heap in init_xdisp and resized as needed in store_frame_title_char. */
7901
7902 static char *frame_title_buf;
7903
7904 /* The buffer's end, and a current output position in it. */
7905
7906 static char *frame_title_buf_end;
7907 static char *frame_title_ptr;
7908
7909
7910 /* Store a single character C for the frame title in frame_title_buf.
7911 Re-allocate frame_title_buf if necessary. */
7912
7913 static void
7914 #ifdef PROTOTYPES
7915 store_frame_title_char (char c)
7916 #else
7917 store_frame_title_char (c)
7918 char c;
7919 #endif
7920 {
7921 /* If output position has reached the end of the allocated buffer,
7922 double the buffer's size. */
7923 if (frame_title_ptr == frame_title_buf_end)
7924 {
7925 int len = frame_title_ptr - frame_title_buf;
7926 int new_size = 2 * len * sizeof *frame_title_buf;
7927 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7928 frame_title_buf_end = frame_title_buf + new_size;
7929 frame_title_ptr = frame_title_buf + len;
7930 }
7931
7932 *frame_title_ptr++ = c;
7933 }
7934
7935
7936 /* Store part of a frame title in frame_title_buf, beginning at
7937 frame_title_ptr. STR is the string to store. Do not copy
7938 characters that yield more columns than PRECISION; PRECISION <= 0
7939 means copy the whole string. Pad with spaces until FIELD_WIDTH
7940 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7941 pad. Called from display_mode_element when it is used to build a
7942 frame title. */
7943
7944 static int
7945 store_frame_title (str, field_width, precision)
7946 const unsigned char *str;
7947 int field_width, precision;
7948 {
7949 int n = 0;
7950 int dummy, nbytes;
7951
7952 /* Copy at most PRECISION chars from STR. */
7953 nbytes = strlen (str);
7954 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7955 while (nbytes--)
7956 store_frame_title_char (*str++);
7957
7958 /* Fill up with spaces until FIELD_WIDTH reached. */
7959 while (field_width > 0
7960 && n < field_width)
7961 {
7962 store_frame_title_char (' ');
7963 ++n;
7964 }
7965
7966 return n;
7967 }
7968
7969 #ifdef HAVE_WINDOW_SYSTEM
7970
7971 /* Set the title of FRAME, if it has changed. The title format is
7972 Vicon_title_format if FRAME is iconified, otherwise it is
7973 frame_title_format. */
7974
7975 static void
7976 x_consider_frame_title (frame)
7977 Lisp_Object frame;
7978 {
7979 struct frame *f = XFRAME (frame);
7980
7981 if (FRAME_WINDOW_P (f)
7982 || FRAME_MINIBUF_ONLY_P (f)
7983 || f->explicit_name)
7984 {
7985 /* Do we have more than one visible frame on this X display? */
7986 Lisp_Object tail;
7987 Lisp_Object fmt;
7988 struct buffer *obuf;
7989 int len;
7990 struct it it;
7991
7992 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7993 {
7994 Lisp_Object other_frame = XCAR (tail);
7995 struct frame *tf = XFRAME (other_frame);
7996
7997 if (tf != f
7998 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7999 && !FRAME_MINIBUF_ONLY_P (tf)
8000 && !EQ (other_frame, tip_frame)
8001 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8002 break;
8003 }
8004
8005 /* Set global variable indicating that multiple frames exist. */
8006 multiple_frames = CONSP (tail);
8007
8008 /* Switch to the buffer of selected window of the frame. Set up
8009 frame_title_ptr so that display_mode_element will output into it;
8010 then display the title. */
8011 obuf = current_buffer;
8012 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8013 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8014 frame_title_ptr = frame_title_buf;
8015 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8016 NULL, DEFAULT_FACE_ID);
8017 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8018 len = frame_title_ptr - frame_title_buf;
8019 frame_title_ptr = NULL;
8020 set_buffer_internal_1 (obuf);
8021
8022 /* Set the title only if it's changed. This avoids consing in
8023 the common case where it hasn't. (If it turns out that we've
8024 already wasted too much time by walking through the list with
8025 display_mode_element, then we might need to optimize at a
8026 higher level than this.) */
8027 if (! STRINGP (f->name)
8028 || SBYTES (f->name) != len
8029 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8030 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8031 }
8032 }
8033
8034 #endif /* not HAVE_WINDOW_SYSTEM */
8035
8036
8037
8038 \f
8039 /***********************************************************************
8040 Menu Bars
8041 ***********************************************************************/
8042
8043
8044 /* Prepare for redisplay by updating menu-bar item lists when
8045 appropriate. This can call eval. */
8046
8047 void
8048 prepare_menu_bars ()
8049 {
8050 int all_windows;
8051 struct gcpro gcpro1, gcpro2;
8052 struct frame *f;
8053 Lisp_Object tooltip_frame;
8054
8055 #ifdef HAVE_WINDOW_SYSTEM
8056 tooltip_frame = tip_frame;
8057 #else
8058 tooltip_frame = Qnil;
8059 #endif
8060
8061 /* Update all frame titles based on their buffer names, etc. We do
8062 this before the menu bars so that the buffer-menu will show the
8063 up-to-date frame titles. */
8064 #ifdef HAVE_WINDOW_SYSTEM
8065 if (windows_or_buffers_changed || update_mode_lines)
8066 {
8067 Lisp_Object tail, frame;
8068
8069 FOR_EACH_FRAME (tail, frame)
8070 {
8071 f = XFRAME (frame);
8072 if (!EQ (frame, tooltip_frame)
8073 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8074 x_consider_frame_title (frame);
8075 }
8076 }
8077 #endif /* HAVE_WINDOW_SYSTEM */
8078
8079 /* Update the menu bar item lists, if appropriate. This has to be
8080 done before any actual redisplay or generation of display lines. */
8081 all_windows = (update_mode_lines
8082 || buffer_shared > 1
8083 || windows_or_buffers_changed);
8084 if (all_windows)
8085 {
8086 Lisp_Object tail, frame;
8087 int count = SPECPDL_INDEX ();
8088
8089 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8090
8091 FOR_EACH_FRAME (tail, frame)
8092 {
8093 f = XFRAME (frame);
8094
8095 /* Ignore tooltip frame. */
8096 if (EQ (frame, tooltip_frame))
8097 continue;
8098
8099 /* If a window on this frame changed size, report that to
8100 the user and clear the size-change flag. */
8101 if (FRAME_WINDOW_SIZES_CHANGED (f))
8102 {
8103 Lisp_Object functions;
8104
8105 /* Clear flag first in case we get an error below. */
8106 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8107 functions = Vwindow_size_change_functions;
8108 GCPRO2 (tail, functions);
8109
8110 while (CONSP (functions))
8111 {
8112 call1 (XCAR (functions), frame);
8113 functions = XCDR (functions);
8114 }
8115 UNGCPRO;
8116 }
8117
8118 GCPRO1 (tail);
8119 update_menu_bar (f, 0);
8120 #ifdef HAVE_WINDOW_SYSTEM
8121 update_tool_bar (f, 0);
8122 #endif
8123 UNGCPRO;
8124 }
8125
8126 unbind_to (count, Qnil);
8127 }
8128 else
8129 {
8130 struct frame *sf = SELECTED_FRAME ();
8131 update_menu_bar (sf, 1);
8132 #ifdef HAVE_WINDOW_SYSTEM
8133 update_tool_bar (sf, 1);
8134 #endif
8135 }
8136
8137 /* Motif needs this. See comment in xmenu.c. Turn it off when
8138 pending_menu_activation is not defined. */
8139 #ifdef USE_X_TOOLKIT
8140 pending_menu_activation = 0;
8141 #endif
8142 }
8143
8144
8145 /* Update the menu bar item list for frame F. This has to be done
8146 before we start to fill in any display lines, because it can call
8147 eval.
8148
8149 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8150
8151 static void
8152 update_menu_bar (f, save_match_data)
8153 struct frame *f;
8154 int save_match_data;
8155 {
8156 Lisp_Object window;
8157 register struct window *w;
8158
8159 /* If called recursively during a menu update, do nothing. This can
8160 happen when, for instance, an activate-menubar-hook causes a
8161 redisplay. */
8162 if (inhibit_menubar_update)
8163 return;
8164
8165 window = FRAME_SELECTED_WINDOW (f);
8166 w = XWINDOW (window);
8167
8168 #if 0 /* The if statement below this if statement used to include the
8169 condition !NILP (w->update_mode_line), rather than using
8170 update_mode_lines directly, and this if statement may have
8171 been added to make that condition work. Now the if
8172 statement below matches its comment, this isn't needed. */
8173 if (update_mode_lines)
8174 w->update_mode_line = Qt;
8175 #endif
8176
8177 if (FRAME_WINDOW_P (f)
8178 ?
8179 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8180 || defined (USE_GTK)
8181 FRAME_EXTERNAL_MENU_BAR (f)
8182 #else
8183 FRAME_MENU_BAR_LINES (f) > 0
8184 #endif
8185 : FRAME_MENU_BAR_LINES (f) > 0)
8186 {
8187 /* If the user has switched buffers or windows, we need to
8188 recompute to reflect the new bindings. But we'll
8189 recompute when update_mode_lines is set too; that means
8190 that people can use force-mode-line-update to request
8191 that the menu bar be recomputed. The adverse effect on
8192 the rest of the redisplay algorithm is about the same as
8193 windows_or_buffers_changed anyway. */
8194 if (windows_or_buffers_changed
8195 /* This used to test w->update_mode_line, but we believe
8196 there is no need to recompute the menu in that case. */
8197 || update_mode_lines
8198 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8199 < BUF_MODIFF (XBUFFER (w->buffer)))
8200 != !NILP (w->last_had_star))
8201 || ((!NILP (Vtransient_mark_mode)
8202 && !NILP (XBUFFER (w->buffer)->mark_active))
8203 != !NILP (w->region_showing)))
8204 {
8205 struct buffer *prev = current_buffer;
8206 int count = SPECPDL_INDEX ();
8207
8208 specbind (Qinhibit_menubar_update, Qt);
8209
8210 set_buffer_internal_1 (XBUFFER (w->buffer));
8211 if (save_match_data)
8212 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8213 if (NILP (Voverriding_local_map_menu_flag))
8214 {
8215 specbind (Qoverriding_terminal_local_map, Qnil);
8216 specbind (Qoverriding_local_map, Qnil);
8217 }
8218
8219 /* Run the Lucid hook. */
8220 safe_run_hooks (Qactivate_menubar_hook);
8221
8222 /* If it has changed current-menubar from previous value,
8223 really recompute the menu-bar from the value. */
8224 if (! NILP (Vlucid_menu_bar_dirty_flag))
8225 call0 (Qrecompute_lucid_menubar);
8226
8227 safe_run_hooks (Qmenu_bar_update_hook);
8228 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8229
8230 /* Redisplay the menu bar in case we changed it. */
8231 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8232 || defined (USE_GTK)
8233 if (FRAME_WINDOW_P (f)
8234 #if defined (MAC_OS)
8235 /* All frames on Mac OS share the same menubar. So only the
8236 selected frame should be allowed to set it. */
8237 && f == SELECTED_FRAME ()
8238 #endif
8239 )
8240 set_frame_menubar (f, 0, 0);
8241 else
8242 /* On a terminal screen, the menu bar is an ordinary screen
8243 line, and this makes it get updated. */
8244 w->update_mode_line = Qt;
8245 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8246 /* In the non-toolkit version, the menu bar is an ordinary screen
8247 line, and this makes it get updated. */
8248 w->update_mode_line = Qt;
8249 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8250
8251 unbind_to (count, Qnil);
8252 set_buffer_internal_1 (prev);
8253 }
8254 }
8255 }
8256
8257
8258 \f
8259 /***********************************************************************
8260 Output Cursor
8261 ***********************************************************************/
8262
8263 #ifdef HAVE_WINDOW_SYSTEM
8264
8265 /* EXPORT:
8266 Nominal cursor position -- where to draw output.
8267 HPOS and VPOS are window relative glyph matrix coordinates.
8268 X and Y are window relative pixel coordinates. */
8269
8270 struct cursor_pos output_cursor;
8271
8272
8273 /* EXPORT:
8274 Set the global variable output_cursor to CURSOR. All cursor
8275 positions are relative to updated_window. */
8276
8277 void
8278 set_output_cursor (cursor)
8279 struct cursor_pos *cursor;
8280 {
8281 output_cursor.hpos = cursor->hpos;
8282 output_cursor.vpos = cursor->vpos;
8283 output_cursor.x = cursor->x;
8284 output_cursor.y = cursor->y;
8285 }
8286
8287
8288 /* EXPORT for RIF:
8289 Set a nominal cursor position.
8290
8291 HPOS and VPOS are column/row positions in a window glyph matrix. X
8292 and Y are window text area relative pixel positions.
8293
8294 If this is done during an update, updated_window will contain the
8295 window that is being updated and the position is the future output
8296 cursor position for that window. If updated_window is null, use
8297 selected_window and display the cursor at the given position. */
8298
8299 void
8300 x_cursor_to (vpos, hpos, y, x)
8301 int vpos, hpos, y, x;
8302 {
8303 struct window *w;
8304
8305 /* If updated_window is not set, work on selected_window. */
8306 if (updated_window)
8307 w = updated_window;
8308 else
8309 w = XWINDOW (selected_window);
8310
8311 /* Set the output cursor. */
8312 output_cursor.hpos = hpos;
8313 output_cursor.vpos = vpos;
8314 output_cursor.x = x;
8315 output_cursor.y = y;
8316
8317 /* If not called as part of an update, really display the cursor.
8318 This will also set the cursor position of W. */
8319 if (updated_window == NULL)
8320 {
8321 BLOCK_INPUT;
8322 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8323 if (rif->flush_display_optional)
8324 rif->flush_display_optional (SELECTED_FRAME ());
8325 UNBLOCK_INPUT;
8326 }
8327 }
8328
8329 #endif /* HAVE_WINDOW_SYSTEM */
8330
8331 \f
8332 /***********************************************************************
8333 Tool-bars
8334 ***********************************************************************/
8335
8336 #ifdef HAVE_WINDOW_SYSTEM
8337
8338 /* Where the mouse was last time we reported a mouse event. */
8339
8340 FRAME_PTR last_mouse_frame;
8341
8342 /* Tool-bar item index of the item on which a mouse button was pressed
8343 or -1. */
8344
8345 int last_tool_bar_item;
8346
8347
8348 /* Update the tool-bar item list for frame F. This has to be done
8349 before we start to fill in any display lines. Called from
8350 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8351 and restore it here. */
8352
8353 static void
8354 update_tool_bar (f, save_match_data)
8355 struct frame *f;
8356 int save_match_data;
8357 {
8358 #ifdef USE_GTK
8359 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8360 #else
8361 int do_update = WINDOWP (f->tool_bar_window)
8362 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8363 #endif
8364
8365 if (do_update)
8366 {
8367 Lisp_Object window;
8368 struct window *w;
8369
8370 window = FRAME_SELECTED_WINDOW (f);
8371 w = XWINDOW (window);
8372
8373 /* If the user has switched buffers or windows, we need to
8374 recompute to reflect the new bindings. But we'll
8375 recompute when update_mode_lines is set too; that means
8376 that people can use force-mode-line-update to request
8377 that the menu bar be recomputed. The adverse effect on
8378 the rest of the redisplay algorithm is about the same as
8379 windows_or_buffers_changed anyway. */
8380 if (windows_or_buffers_changed
8381 || !NILP (w->update_mode_line)
8382 || update_mode_lines
8383 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8384 < BUF_MODIFF (XBUFFER (w->buffer)))
8385 != !NILP (w->last_had_star))
8386 || ((!NILP (Vtransient_mark_mode)
8387 && !NILP (XBUFFER (w->buffer)->mark_active))
8388 != !NILP (w->region_showing)))
8389 {
8390 struct buffer *prev = current_buffer;
8391 int count = SPECPDL_INDEX ();
8392 Lisp_Object old_tool_bar;
8393 struct gcpro gcpro1;
8394
8395 /* Set current_buffer to the buffer of the selected
8396 window of the frame, so that we get the right local
8397 keymaps. */
8398 set_buffer_internal_1 (XBUFFER (w->buffer));
8399
8400 /* Save match data, if we must. */
8401 if (save_match_data)
8402 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8403
8404 /* Make sure that we don't accidentally use bogus keymaps. */
8405 if (NILP (Voverriding_local_map_menu_flag))
8406 {
8407 specbind (Qoverriding_terminal_local_map, Qnil);
8408 specbind (Qoverriding_local_map, Qnil);
8409 }
8410
8411 old_tool_bar = f->tool_bar_items;
8412 GCPRO1 (old_tool_bar);
8413
8414 /* Build desired tool-bar items from keymaps. */
8415 BLOCK_INPUT;
8416 f->tool_bar_items
8417 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8418 UNBLOCK_INPUT;
8419
8420 /* Redisplay the tool-bar if we changed it. */
8421 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8422 w->update_mode_line = Qt;
8423
8424 UNGCPRO;
8425
8426 unbind_to (count, Qnil);
8427 set_buffer_internal_1 (prev);
8428 }
8429 }
8430 }
8431
8432
8433 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8434 F's desired tool-bar contents. F->tool_bar_items must have
8435 been set up previously by calling prepare_menu_bars. */
8436
8437 static void
8438 build_desired_tool_bar_string (f)
8439 struct frame *f;
8440 {
8441 int i, size, size_needed;
8442 struct gcpro gcpro1, gcpro2, gcpro3;
8443 Lisp_Object image, plist, props;
8444
8445 image = plist = props = Qnil;
8446 GCPRO3 (image, plist, props);
8447
8448 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8449 Otherwise, make a new string. */
8450
8451 /* The size of the string we might be able to reuse. */
8452 size = (STRINGP (f->desired_tool_bar_string)
8453 ? SCHARS (f->desired_tool_bar_string)
8454 : 0);
8455
8456 /* We need one space in the string for each image. */
8457 size_needed = f->n_tool_bar_items;
8458
8459 /* Reuse f->desired_tool_bar_string, if possible. */
8460 if (size < size_needed || NILP (f->desired_tool_bar_string))
8461 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8462 make_number (' '));
8463 else
8464 {
8465 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8466 Fremove_text_properties (make_number (0), make_number (size),
8467 props, f->desired_tool_bar_string);
8468 }
8469
8470 /* Put a `display' property on the string for the images to display,
8471 put a `menu_item' property on tool-bar items with a value that
8472 is the index of the item in F's tool-bar item vector. */
8473 for (i = 0; i < f->n_tool_bar_items; ++i)
8474 {
8475 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8476
8477 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8478 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8479 int hmargin, vmargin, relief, idx, end;
8480 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8481
8482 /* If image is a vector, choose the image according to the
8483 button state. */
8484 image = PROP (TOOL_BAR_ITEM_IMAGES);
8485 if (VECTORP (image))
8486 {
8487 if (enabled_p)
8488 idx = (selected_p
8489 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8490 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8491 else
8492 idx = (selected_p
8493 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8494 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8495
8496 xassert (ASIZE (image) >= idx);
8497 image = AREF (image, idx);
8498 }
8499 else
8500 idx = -1;
8501
8502 /* Ignore invalid image specifications. */
8503 if (!valid_image_p (image))
8504 continue;
8505
8506 /* Display the tool-bar button pressed, or depressed. */
8507 plist = Fcopy_sequence (XCDR (image));
8508
8509 /* Compute margin and relief to draw. */
8510 relief = (tool_bar_button_relief >= 0
8511 ? tool_bar_button_relief
8512 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8513 hmargin = vmargin = relief;
8514
8515 if (INTEGERP (Vtool_bar_button_margin)
8516 && XINT (Vtool_bar_button_margin) > 0)
8517 {
8518 hmargin += XFASTINT (Vtool_bar_button_margin);
8519 vmargin += XFASTINT (Vtool_bar_button_margin);
8520 }
8521 else if (CONSP (Vtool_bar_button_margin))
8522 {
8523 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8524 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8525 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8526
8527 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8528 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8529 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8530 }
8531
8532 if (auto_raise_tool_bar_buttons_p)
8533 {
8534 /* Add a `:relief' property to the image spec if the item is
8535 selected. */
8536 if (selected_p)
8537 {
8538 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8539 hmargin -= relief;
8540 vmargin -= relief;
8541 }
8542 }
8543 else
8544 {
8545 /* If image is selected, display it pressed, i.e. with a
8546 negative relief. If it's not selected, display it with a
8547 raised relief. */
8548 plist = Fplist_put (plist, QCrelief,
8549 (selected_p
8550 ? make_number (-relief)
8551 : make_number (relief)));
8552 hmargin -= relief;
8553 vmargin -= relief;
8554 }
8555
8556 /* Put a margin around the image. */
8557 if (hmargin || vmargin)
8558 {
8559 if (hmargin == vmargin)
8560 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8561 else
8562 plist = Fplist_put (plist, QCmargin,
8563 Fcons (make_number (hmargin),
8564 make_number (vmargin)));
8565 }
8566
8567 /* If button is not enabled, and we don't have special images
8568 for the disabled state, make the image appear disabled by
8569 applying an appropriate algorithm to it. */
8570 if (!enabled_p && idx < 0)
8571 plist = Fplist_put (plist, QCconversion, Qdisabled);
8572
8573 /* Put a `display' text property on the string for the image to
8574 display. Put a `menu-item' property on the string that gives
8575 the start of this item's properties in the tool-bar items
8576 vector. */
8577 image = Fcons (Qimage, plist);
8578 props = list4 (Qdisplay, image,
8579 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8580
8581 /* Let the last image hide all remaining spaces in the tool bar
8582 string. The string can be longer than needed when we reuse a
8583 previous string. */
8584 if (i + 1 == f->n_tool_bar_items)
8585 end = SCHARS (f->desired_tool_bar_string);
8586 else
8587 end = i + 1;
8588 Fadd_text_properties (make_number (i), make_number (end),
8589 props, f->desired_tool_bar_string);
8590 #undef PROP
8591 }
8592
8593 UNGCPRO;
8594 }
8595
8596
8597 /* Display one line of the tool-bar of frame IT->f. */
8598
8599 static void
8600 display_tool_bar_line (it)
8601 struct it *it;
8602 {
8603 struct glyph_row *row = it->glyph_row;
8604 int max_x = it->last_visible_x;
8605 struct glyph *last;
8606
8607 prepare_desired_row (row);
8608 row->y = it->current_y;
8609
8610 /* Note that this isn't made use of if the face hasn't a box,
8611 so there's no need to check the face here. */
8612 it->start_of_box_run_p = 1;
8613
8614 while (it->current_x < max_x)
8615 {
8616 int x_before, x, n_glyphs_before, i, nglyphs;
8617
8618 /* Get the next display element. */
8619 if (!get_next_display_element (it))
8620 break;
8621
8622 /* Produce glyphs. */
8623 x_before = it->current_x;
8624 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8625 PRODUCE_GLYPHS (it);
8626
8627 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8628 i = 0;
8629 x = x_before;
8630 while (i < nglyphs)
8631 {
8632 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8633
8634 if (x + glyph->pixel_width > max_x)
8635 {
8636 /* Glyph doesn't fit on line. */
8637 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8638 it->current_x = x;
8639 goto out;
8640 }
8641
8642 ++it->hpos;
8643 x += glyph->pixel_width;
8644 ++i;
8645 }
8646
8647 /* Stop at line ends. */
8648 if (ITERATOR_AT_END_OF_LINE_P (it))
8649 break;
8650
8651 set_iterator_to_next (it, 1);
8652 }
8653
8654 out:;
8655
8656 row->displays_text_p = row->used[TEXT_AREA] != 0;
8657 extend_face_to_end_of_line (it);
8658 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8659 last->right_box_line_p = 1;
8660 if (last == row->glyphs[TEXT_AREA])
8661 last->left_box_line_p = 1;
8662 compute_line_metrics (it);
8663
8664 /* If line is empty, make it occupy the rest of the tool-bar. */
8665 if (!row->displays_text_p)
8666 {
8667 row->height = row->phys_height = it->last_visible_y - row->y;
8668 row->ascent = row->phys_ascent = 0;
8669 }
8670
8671 row->full_width_p = 1;
8672 row->continued_p = 0;
8673 row->truncated_on_left_p = 0;
8674 row->truncated_on_right_p = 0;
8675
8676 it->current_x = it->hpos = 0;
8677 it->current_y += row->height;
8678 ++it->vpos;
8679 ++it->glyph_row;
8680 }
8681
8682
8683 /* Value is the number of screen lines needed to make all tool-bar
8684 items of frame F visible. */
8685
8686 static int
8687 tool_bar_lines_needed (f)
8688 struct frame *f;
8689 {
8690 struct window *w = XWINDOW (f->tool_bar_window);
8691 struct it it;
8692
8693 /* Initialize an iterator for iteration over
8694 F->desired_tool_bar_string in the tool-bar window of frame F. */
8695 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8696 it.first_visible_x = 0;
8697 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8698 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8699
8700 while (!ITERATOR_AT_END_P (&it))
8701 {
8702 it.glyph_row = w->desired_matrix->rows;
8703 clear_glyph_row (it.glyph_row);
8704 display_tool_bar_line (&it);
8705 }
8706
8707 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8708 }
8709
8710
8711 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8712 0, 1, 0,
8713 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8714 (frame)
8715 Lisp_Object frame;
8716 {
8717 struct frame *f;
8718 struct window *w;
8719 int nlines = 0;
8720
8721 if (NILP (frame))
8722 frame = selected_frame;
8723 else
8724 CHECK_FRAME (frame);
8725 f = XFRAME (frame);
8726
8727 if (WINDOWP (f->tool_bar_window)
8728 || (w = XWINDOW (f->tool_bar_window),
8729 WINDOW_TOTAL_LINES (w) > 0))
8730 {
8731 update_tool_bar (f, 1);
8732 if (f->n_tool_bar_items)
8733 {
8734 build_desired_tool_bar_string (f);
8735 nlines = tool_bar_lines_needed (f);
8736 }
8737 }
8738
8739 return make_number (nlines);
8740 }
8741
8742
8743 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8744 height should be changed. */
8745
8746 static int
8747 redisplay_tool_bar (f)
8748 struct frame *f;
8749 {
8750 struct window *w;
8751 struct it it;
8752 struct glyph_row *row;
8753 int change_height_p = 0;
8754
8755 #ifdef USE_GTK
8756 if (FRAME_EXTERNAL_TOOL_BAR (f))
8757 update_frame_tool_bar (f);
8758 return 0;
8759 #endif
8760
8761 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8762 do anything. This means you must start with tool-bar-lines
8763 non-zero to get the auto-sizing effect. Or in other words, you
8764 can turn off tool-bars by specifying tool-bar-lines zero. */
8765 if (!WINDOWP (f->tool_bar_window)
8766 || (w = XWINDOW (f->tool_bar_window),
8767 WINDOW_TOTAL_LINES (w) == 0))
8768 return 0;
8769
8770 /* Set up an iterator for the tool-bar window. */
8771 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8772 it.first_visible_x = 0;
8773 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8774 row = it.glyph_row;
8775
8776 /* Build a string that represents the contents of the tool-bar. */
8777 build_desired_tool_bar_string (f);
8778 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8779
8780 /* Display as many lines as needed to display all tool-bar items. */
8781 while (it.current_y < it.last_visible_y)
8782 display_tool_bar_line (&it);
8783
8784 /* It doesn't make much sense to try scrolling in the tool-bar
8785 window, so don't do it. */
8786 w->desired_matrix->no_scrolling_p = 1;
8787 w->must_be_updated_p = 1;
8788
8789 if (auto_resize_tool_bars_p)
8790 {
8791 int nlines;
8792
8793 /* If we couldn't display everything, change the tool-bar's
8794 height. */
8795 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8796 change_height_p = 1;
8797
8798 /* If there are blank lines at the end, except for a partially
8799 visible blank line at the end that is smaller than
8800 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8801 row = it.glyph_row - 1;
8802 if (!row->displays_text_p
8803 && row->height >= FRAME_LINE_HEIGHT (f))
8804 change_height_p = 1;
8805
8806 /* If row displays tool-bar items, but is partially visible,
8807 change the tool-bar's height. */
8808 if (row->displays_text_p
8809 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8810 change_height_p = 1;
8811
8812 /* Resize windows as needed by changing the `tool-bar-lines'
8813 frame parameter. */
8814 if (change_height_p
8815 && (nlines = tool_bar_lines_needed (f),
8816 nlines != WINDOW_TOTAL_LINES (w)))
8817 {
8818 extern Lisp_Object Qtool_bar_lines;
8819 Lisp_Object frame;
8820 int old_height = WINDOW_TOTAL_LINES (w);
8821
8822 XSETFRAME (frame, f);
8823 clear_glyph_matrix (w->desired_matrix);
8824 Fmodify_frame_parameters (frame,
8825 Fcons (Fcons (Qtool_bar_lines,
8826 make_number (nlines)),
8827 Qnil));
8828 if (WINDOW_TOTAL_LINES (w) != old_height)
8829 fonts_changed_p = 1;
8830 }
8831 }
8832
8833 return change_height_p;
8834 }
8835
8836
8837 /* Get information about the tool-bar item which is displayed in GLYPH
8838 on frame F. Return in *PROP_IDX the index where tool-bar item
8839 properties start in F->tool_bar_items. Value is zero if
8840 GLYPH doesn't display a tool-bar item. */
8841
8842 static int
8843 tool_bar_item_info (f, glyph, prop_idx)
8844 struct frame *f;
8845 struct glyph *glyph;
8846 int *prop_idx;
8847 {
8848 Lisp_Object prop;
8849 int success_p;
8850 int charpos;
8851
8852 /* This function can be called asynchronously, which means we must
8853 exclude any possibility that Fget_text_property signals an
8854 error. */
8855 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8856 charpos = max (0, charpos);
8857
8858 /* Get the text property `menu-item' at pos. The value of that
8859 property is the start index of this item's properties in
8860 F->tool_bar_items. */
8861 prop = Fget_text_property (make_number (charpos),
8862 Qmenu_item, f->current_tool_bar_string);
8863 if (INTEGERP (prop))
8864 {
8865 *prop_idx = XINT (prop);
8866 success_p = 1;
8867 }
8868 else
8869 success_p = 0;
8870
8871 return success_p;
8872 }
8873
8874 \f
8875 /* Get information about the tool-bar item at position X/Y on frame F.
8876 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8877 the current matrix of the tool-bar window of F, or NULL if not
8878 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8879 item in F->tool_bar_items. Value is
8880
8881 -1 if X/Y is not on a tool-bar item
8882 0 if X/Y is on the same item that was highlighted before.
8883 1 otherwise. */
8884
8885 static int
8886 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8887 struct frame *f;
8888 int x, y;
8889 struct glyph **glyph;
8890 int *hpos, *vpos, *prop_idx;
8891 {
8892 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8893 struct window *w = XWINDOW (f->tool_bar_window);
8894 int area;
8895
8896 /* Find the glyph under X/Y. */
8897 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8898 if (*glyph == NULL)
8899 return -1;
8900
8901 /* Get the start of this tool-bar item's properties in
8902 f->tool_bar_items. */
8903 if (!tool_bar_item_info (f, *glyph, prop_idx))
8904 return -1;
8905
8906 /* Is mouse on the highlighted item? */
8907 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8908 && *vpos >= dpyinfo->mouse_face_beg_row
8909 && *vpos <= dpyinfo->mouse_face_end_row
8910 && (*vpos > dpyinfo->mouse_face_beg_row
8911 || *hpos >= dpyinfo->mouse_face_beg_col)
8912 && (*vpos < dpyinfo->mouse_face_end_row
8913 || *hpos < dpyinfo->mouse_face_end_col
8914 || dpyinfo->mouse_face_past_end))
8915 return 0;
8916
8917 return 1;
8918 }
8919
8920
8921 /* EXPORT:
8922 Handle mouse button event on the tool-bar of frame F, at
8923 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8924 0 for button release. MODIFIERS is event modifiers for button
8925 release. */
8926
8927 void
8928 handle_tool_bar_click (f, x, y, down_p, modifiers)
8929 struct frame *f;
8930 int x, y, down_p;
8931 unsigned int modifiers;
8932 {
8933 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8934 struct window *w = XWINDOW (f->tool_bar_window);
8935 int hpos, vpos, prop_idx;
8936 struct glyph *glyph;
8937 Lisp_Object enabled_p;
8938
8939 /* If not on the highlighted tool-bar item, return. */
8940 frame_to_window_pixel_xy (w, &x, &y);
8941 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8942 return;
8943
8944 /* If item is disabled, do nothing. */
8945 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8946 if (NILP (enabled_p))
8947 return;
8948
8949 if (down_p)
8950 {
8951 /* Show item in pressed state. */
8952 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8953 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8954 last_tool_bar_item = prop_idx;
8955 }
8956 else
8957 {
8958 Lisp_Object key, frame;
8959 struct input_event event;
8960 EVENT_INIT (event);
8961
8962 /* Show item in released state. */
8963 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8964 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8965
8966 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8967
8968 XSETFRAME (frame, f);
8969 event.kind = TOOL_BAR_EVENT;
8970 event.frame_or_window = frame;
8971 event.arg = frame;
8972 kbd_buffer_store_event (&event);
8973
8974 event.kind = TOOL_BAR_EVENT;
8975 event.frame_or_window = frame;
8976 event.arg = key;
8977 event.modifiers = modifiers;
8978 kbd_buffer_store_event (&event);
8979 last_tool_bar_item = -1;
8980 }
8981 }
8982
8983
8984 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8985 tool-bar window-relative coordinates X/Y. Called from
8986 note_mouse_highlight. */
8987
8988 static void
8989 note_tool_bar_highlight (f, x, y)
8990 struct frame *f;
8991 int x, y;
8992 {
8993 Lisp_Object window = f->tool_bar_window;
8994 struct window *w = XWINDOW (window);
8995 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8996 int hpos, vpos;
8997 struct glyph *glyph;
8998 struct glyph_row *row;
8999 int i;
9000 Lisp_Object enabled_p;
9001 int prop_idx;
9002 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9003 int mouse_down_p, rc;
9004
9005 /* Function note_mouse_highlight is called with negative x(y
9006 values when mouse moves outside of the frame. */
9007 if (x <= 0 || y <= 0)
9008 {
9009 clear_mouse_face (dpyinfo);
9010 return;
9011 }
9012
9013 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9014 if (rc < 0)
9015 {
9016 /* Not on tool-bar item. */
9017 clear_mouse_face (dpyinfo);
9018 return;
9019 }
9020 else if (rc == 0)
9021 /* On same tool-bar item as before. */
9022 goto set_help_echo;
9023
9024 clear_mouse_face (dpyinfo);
9025
9026 /* Mouse is down, but on different tool-bar item? */
9027 mouse_down_p = (dpyinfo->grabbed
9028 && f == last_mouse_frame
9029 && FRAME_LIVE_P (f));
9030 if (mouse_down_p
9031 && last_tool_bar_item != prop_idx)
9032 return;
9033
9034 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9035 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9036
9037 /* If tool-bar item is not enabled, don't highlight it. */
9038 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9039 if (!NILP (enabled_p))
9040 {
9041 /* Compute the x-position of the glyph. In front and past the
9042 image is a space. We include this in the highlighted area. */
9043 row = MATRIX_ROW (w->current_matrix, vpos);
9044 for (i = x = 0; i < hpos; ++i)
9045 x += row->glyphs[TEXT_AREA][i].pixel_width;
9046
9047 /* Record this as the current active region. */
9048 dpyinfo->mouse_face_beg_col = hpos;
9049 dpyinfo->mouse_face_beg_row = vpos;
9050 dpyinfo->mouse_face_beg_x = x;
9051 dpyinfo->mouse_face_beg_y = row->y;
9052 dpyinfo->mouse_face_past_end = 0;
9053
9054 dpyinfo->mouse_face_end_col = hpos + 1;
9055 dpyinfo->mouse_face_end_row = vpos;
9056 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9057 dpyinfo->mouse_face_end_y = row->y;
9058 dpyinfo->mouse_face_window = window;
9059 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9060
9061 /* Display it as active. */
9062 show_mouse_face (dpyinfo, draw);
9063 dpyinfo->mouse_face_image_state = draw;
9064 }
9065
9066 set_help_echo:
9067
9068 /* Set help_echo_string to a help string to display for this tool-bar item.
9069 XTread_socket does the rest. */
9070 help_echo_object = help_echo_window = Qnil;
9071 help_echo_pos = -1;
9072 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9073 if (NILP (help_echo_string))
9074 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9075 }
9076
9077 #endif /* HAVE_WINDOW_SYSTEM */
9078
9079
9080 \f
9081 /************************************************************************
9082 Horizontal scrolling
9083 ************************************************************************/
9084
9085 static int hscroll_window_tree P_ ((Lisp_Object));
9086 static int hscroll_windows P_ ((Lisp_Object));
9087
9088 /* For all leaf windows in the window tree rooted at WINDOW, set their
9089 hscroll value so that PT is (i) visible in the window, and (ii) so
9090 that it is not within a certain margin at the window's left and
9091 right border. Value is non-zero if any window's hscroll has been
9092 changed. */
9093
9094 static int
9095 hscroll_window_tree (window)
9096 Lisp_Object window;
9097 {
9098 int hscrolled_p = 0;
9099 int hscroll_relative_p = FLOATP (Vhscroll_step);
9100 int hscroll_step_abs = 0;
9101 double hscroll_step_rel = 0;
9102
9103 if (hscroll_relative_p)
9104 {
9105 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9106 if (hscroll_step_rel < 0)
9107 {
9108 hscroll_relative_p = 0;
9109 hscroll_step_abs = 0;
9110 }
9111 }
9112 else if (INTEGERP (Vhscroll_step))
9113 {
9114 hscroll_step_abs = XINT (Vhscroll_step);
9115 if (hscroll_step_abs < 0)
9116 hscroll_step_abs = 0;
9117 }
9118 else
9119 hscroll_step_abs = 0;
9120
9121 while (WINDOWP (window))
9122 {
9123 struct window *w = XWINDOW (window);
9124
9125 if (WINDOWP (w->hchild))
9126 hscrolled_p |= hscroll_window_tree (w->hchild);
9127 else if (WINDOWP (w->vchild))
9128 hscrolled_p |= hscroll_window_tree (w->vchild);
9129 else if (w->cursor.vpos >= 0)
9130 {
9131 int h_margin;
9132 int text_area_width;
9133 struct glyph_row *current_cursor_row
9134 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9135 struct glyph_row *desired_cursor_row
9136 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9137 struct glyph_row *cursor_row
9138 = (desired_cursor_row->enabled_p
9139 ? desired_cursor_row
9140 : current_cursor_row);
9141
9142 text_area_width = window_box_width (w, TEXT_AREA);
9143
9144 /* Scroll when cursor is inside this scroll margin. */
9145 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9146
9147 if ((XFASTINT (w->hscroll)
9148 && w->cursor.x <= h_margin)
9149 || (cursor_row->enabled_p
9150 && cursor_row->truncated_on_right_p
9151 && (w->cursor.x >= text_area_width - h_margin)))
9152 {
9153 struct it it;
9154 int hscroll;
9155 struct buffer *saved_current_buffer;
9156 int pt;
9157 int wanted_x;
9158
9159 /* Find point in a display of infinite width. */
9160 saved_current_buffer = current_buffer;
9161 current_buffer = XBUFFER (w->buffer);
9162
9163 if (w == XWINDOW (selected_window))
9164 pt = BUF_PT (current_buffer);
9165 else
9166 {
9167 pt = marker_position (w->pointm);
9168 pt = max (BEGV, pt);
9169 pt = min (ZV, pt);
9170 }
9171
9172 /* Move iterator to pt starting at cursor_row->start in
9173 a line with infinite width. */
9174 init_to_row_start (&it, w, cursor_row);
9175 it.last_visible_x = INFINITY;
9176 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9177 current_buffer = saved_current_buffer;
9178
9179 /* Position cursor in window. */
9180 if (!hscroll_relative_p && hscroll_step_abs == 0)
9181 hscroll = max (0, (it.current_x
9182 - (ITERATOR_AT_END_OF_LINE_P (&it)
9183 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9184 : (text_area_width / 2))))
9185 / FRAME_COLUMN_WIDTH (it.f);
9186 else if (w->cursor.x >= text_area_width - h_margin)
9187 {
9188 if (hscroll_relative_p)
9189 wanted_x = text_area_width * (1 - hscroll_step_rel)
9190 - h_margin;
9191 else
9192 wanted_x = text_area_width
9193 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9194 - h_margin;
9195 hscroll
9196 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9197 }
9198 else
9199 {
9200 if (hscroll_relative_p)
9201 wanted_x = text_area_width * hscroll_step_rel
9202 + h_margin;
9203 else
9204 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9205 + h_margin;
9206 hscroll
9207 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9208 }
9209 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9210
9211 /* Don't call Fset_window_hscroll if value hasn't
9212 changed because it will prevent redisplay
9213 optimizations. */
9214 if (XFASTINT (w->hscroll) != hscroll)
9215 {
9216 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9217 w->hscroll = make_number (hscroll);
9218 hscrolled_p = 1;
9219 }
9220 }
9221 }
9222
9223 window = w->next;
9224 }
9225
9226 /* Value is non-zero if hscroll of any leaf window has been changed. */
9227 return hscrolled_p;
9228 }
9229
9230
9231 /* Set hscroll so that cursor is visible and not inside horizontal
9232 scroll margins for all windows in the tree rooted at WINDOW. See
9233 also hscroll_window_tree above. Value is non-zero if any window's
9234 hscroll has been changed. If it has, desired matrices on the frame
9235 of WINDOW are cleared. */
9236
9237 static int
9238 hscroll_windows (window)
9239 Lisp_Object window;
9240 {
9241 int hscrolled_p;
9242
9243 if (automatic_hscrolling_p)
9244 {
9245 hscrolled_p = hscroll_window_tree (window);
9246 if (hscrolled_p)
9247 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9248 }
9249 else
9250 hscrolled_p = 0;
9251 return hscrolled_p;
9252 }
9253
9254
9255 \f
9256 /************************************************************************
9257 Redisplay
9258 ************************************************************************/
9259
9260 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9261 to a non-zero value. This is sometimes handy to have in a debugger
9262 session. */
9263
9264 #if GLYPH_DEBUG
9265
9266 /* First and last unchanged row for try_window_id. */
9267
9268 int debug_first_unchanged_at_end_vpos;
9269 int debug_last_unchanged_at_beg_vpos;
9270
9271 /* Delta vpos and y. */
9272
9273 int debug_dvpos, debug_dy;
9274
9275 /* Delta in characters and bytes for try_window_id. */
9276
9277 int debug_delta, debug_delta_bytes;
9278
9279 /* Values of window_end_pos and window_end_vpos at the end of
9280 try_window_id. */
9281
9282 EMACS_INT debug_end_pos, debug_end_vpos;
9283
9284 /* Append a string to W->desired_matrix->method. FMT is a printf
9285 format string. A1...A9 are a supplement for a variable-length
9286 argument list. If trace_redisplay_p is non-zero also printf the
9287 resulting string to stderr. */
9288
9289 static void
9290 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9291 struct window *w;
9292 char *fmt;
9293 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9294 {
9295 char buffer[512];
9296 char *method = w->desired_matrix->method;
9297 int len = strlen (method);
9298 int size = sizeof w->desired_matrix->method;
9299 int remaining = size - len - 1;
9300
9301 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9302 if (len && remaining)
9303 {
9304 method[len] = '|';
9305 --remaining, ++len;
9306 }
9307
9308 strncpy (method + len, buffer, remaining);
9309
9310 if (trace_redisplay_p)
9311 fprintf (stderr, "%p (%s): %s\n",
9312 w,
9313 ((BUFFERP (w->buffer)
9314 && STRINGP (XBUFFER (w->buffer)->name))
9315 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9316 : "no buffer"),
9317 buffer);
9318 }
9319
9320 #endif /* GLYPH_DEBUG */
9321
9322
9323 /* Value is non-zero if all changes in window W, which displays
9324 current_buffer, are in the text between START and END. START is a
9325 buffer position, END is given as a distance from Z. Used in
9326 redisplay_internal for display optimization. */
9327
9328 static INLINE int
9329 text_outside_line_unchanged_p (w, start, end)
9330 struct window *w;
9331 int start, end;
9332 {
9333 int unchanged_p = 1;
9334
9335 /* If text or overlays have changed, see where. */
9336 if (XFASTINT (w->last_modified) < MODIFF
9337 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9338 {
9339 /* Gap in the line? */
9340 if (GPT < start || Z - GPT < end)
9341 unchanged_p = 0;
9342
9343 /* Changes start in front of the line, or end after it? */
9344 if (unchanged_p
9345 && (BEG_UNCHANGED < start - 1
9346 || END_UNCHANGED < end))
9347 unchanged_p = 0;
9348
9349 /* If selective display, can't optimize if changes start at the
9350 beginning of the line. */
9351 if (unchanged_p
9352 && INTEGERP (current_buffer->selective_display)
9353 && XINT (current_buffer->selective_display) > 0
9354 && (BEG_UNCHANGED < start || GPT <= start))
9355 unchanged_p = 0;
9356
9357 /* If there are overlays at the start or end of the line, these
9358 may have overlay strings with newlines in them. A change at
9359 START, for instance, may actually concern the display of such
9360 overlay strings as well, and they are displayed on different
9361 lines. So, quickly rule out this case. (For the future, it
9362 might be desirable to implement something more telling than
9363 just BEG/END_UNCHANGED.) */
9364 if (unchanged_p)
9365 {
9366 if (BEG + BEG_UNCHANGED == start
9367 && overlay_touches_p (start))
9368 unchanged_p = 0;
9369 if (END_UNCHANGED == end
9370 && overlay_touches_p (Z - end))
9371 unchanged_p = 0;
9372 }
9373 }
9374
9375 return unchanged_p;
9376 }
9377
9378
9379 /* Do a frame update, taking possible shortcuts into account. This is
9380 the main external entry point for redisplay.
9381
9382 If the last redisplay displayed an echo area message and that message
9383 is no longer requested, we clear the echo area or bring back the
9384 mini-buffer if that is in use. */
9385
9386 void
9387 redisplay ()
9388 {
9389 redisplay_internal (0);
9390 }
9391
9392
9393 static Lisp_Object
9394 overlay_arrow_string_or_property (var, pbitmap)
9395 Lisp_Object var;
9396 int *pbitmap;
9397 {
9398 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9399 Lisp_Object bitmap;
9400
9401 if (pbitmap)
9402 {
9403 *pbitmap = 0;
9404 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9405 *pbitmap = XINT (bitmap);
9406 }
9407
9408 if (!NILP (pstr))
9409 return pstr;
9410 return Voverlay_arrow_string;
9411 }
9412
9413 /* Return 1 if there are any overlay-arrows in current_buffer. */
9414 static int
9415 overlay_arrow_in_current_buffer_p ()
9416 {
9417 Lisp_Object vlist;
9418
9419 for (vlist = Voverlay_arrow_variable_list;
9420 CONSP (vlist);
9421 vlist = XCDR (vlist))
9422 {
9423 Lisp_Object var = XCAR (vlist);
9424 Lisp_Object val;
9425
9426 if (!SYMBOLP (var))
9427 continue;
9428 val = find_symbol_value (var);
9429 if (MARKERP (val)
9430 && current_buffer == XMARKER (val)->buffer)
9431 return 1;
9432 }
9433 return 0;
9434 }
9435
9436
9437 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9438 has changed. */
9439
9440 static int
9441 overlay_arrows_changed_p ()
9442 {
9443 Lisp_Object vlist;
9444
9445 for (vlist = Voverlay_arrow_variable_list;
9446 CONSP (vlist);
9447 vlist = XCDR (vlist))
9448 {
9449 Lisp_Object var = XCAR (vlist);
9450 Lisp_Object val, pstr;
9451
9452 if (!SYMBOLP (var))
9453 continue;
9454 val = find_symbol_value (var);
9455 if (!MARKERP (val))
9456 continue;
9457 if (! EQ (COERCE_MARKER (val),
9458 Fget (var, Qlast_arrow_position))
9459 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9460 EQ (pstr, Fget (var, Qlast_arrow_string))))
9461 return 1;
9462 }
9463 return 0;
9464 }
9465
9466 /* Mark overlay arrows to be updated on next redisplay. */
9467
9468 static void
9469 update_overlay_arrows (up_to_date)
9470 int up_to_date;
9471 {
9472 Lisp_Object vlist;
9473
9474 for (vlist = Voverlay_arrow_variable_list;
9475 CONSP (vlist);
9476 vlist = XCDR (vlist))
9477 {
9478 Lisp_Object var = XCAR (vlist);
9479
9480 if (!SYMBOLP (var))
9481 continue;
9482
9483 if (up_to_date > 0)
9484 {
9485 Lisp_Object val = find_symbol_value (var);
9486 Fput (var, Qlast_arrow_position,
9487 COERCE_MARKER (val));
9488 Fput (var, Qlast_arrow_string,
9489 overlay_arrow_string_or_property (var, 0));
9490 }
9491 else if (up_to_date < 0
9492 || !NILP (Fget (var, Qlast_arrow_position)))
9493 {
9494 Fput (var, Qlast_arrow_position, Qt);
9495 Fput (var, Qlast_arrow_string, Qt);
9496 }
9497 }
9498 }
9499
9500
9501 /* Return overlay arrow string at row, or nil. */
9502
9503 static Lisp_Object
9504 overlay_arrow_at_row (f, row, pbitmap)
9505 struct frame *f;
9506 struct glyph_row *row;
9507 int *pbitmap;
9508 {
9509 Lisp_Object vlist;
9510
9511 for (vlist = Voverlay_arrow_variable_list;
9512 CONSP (vlist);
9513 vlist = XCDR (vlist))
9514 {
9515 Lisp_Object var = XCAR (vlist);
9516 Lisp_Object val;
9517
9518 if (!SYMBOLP (var))
9519 continue;
9520
9521 val = find_symbol_value (var);
9522
9523 if (MARKERP (val)
9524 && current_buffer == XMARKER (val)->buffer
9525 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9526 {
9527 val = overlay_arrow_string_or_property (var, pbitmap);
9528 if (FRAME_WINDOW_P (f))
9529 return Qt;
9530 else if (STRINGP (val))
9531 return val;
9532 break;
9533 }
9534 }
9535
9536 *pbitmap = 0;
9537 return Qnil;
9538 }
9539
9540 /* Return 1 if point moved out of or into a composition. Otherwise
9541 return 0. PREV_BUF and PREV_PT are the last point buffer and
9542 position. BUF and PT are the current point buffer and position. */
9543
9544 int
9545 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9546 struct buffer *prev_buf, *buf;
9547 int prev_pt, pt;
9548 {
9549 int start, end;
9550 Lisp_Object prop;
9551 Lisp_Object buffer;
9552
9553 XSETBUFFER (buffer, buf);
9554 /* Check a composition at the last point if point moved within the
9555 same buffer. */
9556 if (prev_buf == buf)
9557 {
9558 if (prev_pt == pt)
9559 /* Point didn't move. */
9560 return 0;
9561
9562 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9563 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9564 && COMPOSITION_VALID_P (start, end, prop)
9565 && start < prev_pt && end > prev_pt)
9566 /* The last point was within the composition. Return 1 iff
9567 point moved out of the composition. */
9568 return (pt <= start || pt >= end);
9569 }
9570
9571 /* Check a composition at the current point. */
9572 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9573 && find_composition (pt, -1, &start, &end, &prop, buffer)
9574 && COMPOSITION_VALID_P (start, end, prop)
9575 && start < pt && end > pt);
9576 }
9577
9578
9579 /* Reconsider the setting of B->clip_changed which is displayed
9580 in window W. */
9581
9582 static INLINE void
9583 reconsider_clip_changes (w, b)
9584 struct window *w;
9585 struct buffer *b;
9586 {
9587 if (b->clip_changed
9588 && !NILP (w->window_end_valid)
9589 && w->current_matrix->buffer == b
9590 && w->current_matrix->zv == BUF_ZV (b)
9591 && w->current_matrix->begv == BUF_BEGV (b))
9592 b->clip_changed = 0;
9593
9594 /* If display wasn't paused, and W is not a tool bar window, see if
9595 point has been moved into or out of a composition. In that case,
9596 we set b->clip_changed to 1 to force updating the screen. If
9597 b->clip_changed has already been set to 1, we can skip this
9598 check. */
9599 if (!b->clip_changed
9600 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9601 {
9602 int pt;
9603
9604 if (w == XWINDOW (selected_window))
9605 pt = BUF_PT (current_buffer);
9606 else
9607 pt = marker_position (w->pointm);
9608
9609 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9610 || pt != XINT (w->last_point))
9611 && check_point_in_composition (w->current_matrix->buffer,
9612 XINT (w->last_point),
9613 XBUFFER (w->buffer), pt))
9614 b->clip_changed = 1;
9615 }
9616 }
9617 \f
9618
9619 /* Select FRAME to forward the values of frame-local variables into C
9620 variables so that the redisplay routines can access those values
9621 directly. */
9622
9623 static void
9624 select_frame_for_redisplay (frame)
9625 Lisp_Object frame;
9626 {
9627 Lisp_Object tail, sym, val;
9628 Lisp_Object old = selected_frame;
9629
9630 selected_frame = frame;
9631
9632 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9633 if (CONSP (XCAR (tail))
9634 && (sym = XCAR (XCAR (tail)),
9635 SYMBOLP (sym))
9636 && (sym = indirect_variable (sym),
9637 val = SYMBOL_VALUE (sym),
9638 (BUFFER_LOCAL_VALUEP (val)
9639 || SOME_BUFFER_LOCAL_VALUEP (val)))
9640 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9641 Fsymbol_value (sym);
9642
9643 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9644 if (CONSP (XCAR (tail))
9645 && (sym = XCAR (XCAR (tail)),
9646 SYMBOLP (sym))
9647 && (sym = indirect_variable (sym),
9648 val = SYMBOL_VALUE (sym),
9649 (BUFFER_LOCAL_VALUEP (val)
9650 || SOME_BUFFER_LOCAL_VALUEP (val)))
9651 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9652 Fsymbol_value (sym);
9653 }
9654
9655
9656 #define STOP_POLLING \
9657 do { if (! polling_stopped_here) stop_polling (); \
9658 polling_stopped_here = 1; } while (0)
9659
9660 #define RESUME_POLLING \
9661 do { if (polling_stopped_here) start_polling (); \
9662 polling_stopped_here = 0; } while (0)
9663
9664
9665 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9666 response to any user action; therefore, we should preserve the echo
9667 area. (Actually, our caller does that job.) Perhaps in the future
9668 avoid recentering windows if it is not necessary; currently that
9669 causes some problems. */
9670
9671 static void
9672 redisplay_internal (preserve_echo_area)
9673 int preserve_echo_area;
9674 {
9675 struct window *w = XWINDOW (selected_window);
9676 struct frame *f = XFRAME (w->frame);
9677 int pause;
9678 int must_finish = 0;
9679 struct text_pos tlbufpos, tlendpos;
9680 int number_of_visible_frames;
9681 int count;
9682 struct frame *sf = SELECTED_FRAME ();
9683 int polling_stopped_here = 0;
9684
9685 /* Non-zero means redisplay has to consider all windows on all
9686 frames. Zero means, only selected_window is considered. */
9687 int consider_all_windows_p;
9688
9689 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9690
9691 /* No redisplay if running in batch mode or frame is not yet fully
9692 initialized, or redisplay is explicitly turned off by setting
9693 Vinhibit_redisplay. */
9694 if (noninteractive
9695 || !NILP (Vinhibit_redisplay)
9696 || !f->glyphs_initialized_p)
9697 return;
9698
9699 /* The flag redisplay_performed_directly_p is set by
9700 direct_output_for_insert when it already did the whole screen
9701 update necessary. */
9702 if (redisplay_performed_directly_p)
9703 {
9704 redisplay_performed_directly_p = 0;
9705 if (!hscroll_windows (selected_window))
9706 return;
9707 }
9708
9709 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9710 if (popup_activated ())
9711 return;
9712 #endif
9713
9714 /* I don't think this happens but let's be paranoid. */
9715 if (redisplaying_p)
9716 return;
9717
9718 /* Record a function that resets redisplaying_p to its old value
9719 when we leave this function. */
9720 count = SPECPDL_INDEX ();
9721 record_unwind_protect (unwind_redisplay,
9722 Fcons (make_number (redisplaying_p), selected_frame));
9723 ++redisplaying_p;
9724 specbind (Qinhibit_free_realized_faces, Qnil);
9725
9726 retry:
9727 pause = 0;
9728 reconsider_clip_changes (w, current_buffer);
9729
9730 /* If new fonts have been loaded that make a glyph matrix adjustment
9731 necessary, do it. */
9732 if (fonts_changed_p)
9733 {
9734 adjust_glyphs (NULL);
9735 ++windows_or_buffers_changed;
9736 fonts_changed_p = 0;
9737 }
9738
9739 /* If face_change_count is non-zero, init_iterator will free all
9740 realized faces, which includes the faces referenced from current
9741 matrices. So, we can't reuse current matrices in this case. */
9742 if (face_change_count)
9743 ++windows_or_buffers_changed;
9744
9745 if (! FRAME_WINDOW_P (sf)
9746 && previous_terminal_frame != sf)
9747 {
9748 /* Since frames on an ASCII terminal share the same display
9749 area, displaying a different frame means redisplay the whole
9750 thing. */
9751 windows_or_buffers_changed++;
9752 SET_FRAME_GARBAGED (sf);
9753 XSETFRAME (Vterminal_frame, sf);
9754 }
9755 previous_terminal_frame = sf;
9756
9757 /* Set the visible flags for all frames. Do this before checking
9758 for resized or garbaged frames; they want to know if their frames
9759 are visible. See the comment in frame.h for
9760 FRAME_SAMPLE_VISIBILITY. */
9761 {
9762 Lisp_Object tail, frame;
9763
9764 number_of_visible_frames = 0;
9765
9766 FOR_EACH_FRAME (tail, frame)
9767 {
9768 struct frame *f = XFRAME (frame);
9769
9770 FRAME_SAMPLE_VISIBILITY (f);
9771 if (FRAME_VISIBLE_P (f))
9772 ++number_of_visible_frames;
9773 clear_desired_matrices (f);
9774 }
9775 }
9776
9777 /* Notice any pending interrupt request to change frame size. */
9778 do_pending_window_change (1);
9779
9780 /* Clear frames marked as garbaged. */
9781 if (frame_garbaged)
9782 clear_garbaged_frames ();
9783
9784 /* Build menubar and tool-bar items. */
9785 prepare_menu_bars ();
9786
9787 if (windows_or_buffers_changed)
9788 update_mode_lines++;
9789
9790 /* Detect case that we need to write or remove a star in the mode line. */
9791 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9792 {
9793 w->update_mode_line = Qt;
9794 if (buffer_shared > 1)
9795 update_mode_lines++;
9796 }
9797
9798 /* If %c is in the mode line, update it if needed. */
9799 if (!NILP (w->column_number_displayed)
9800 /* This alternative quickly identifies a common case
9801 where no change is needed. */
9802 && !(PT == XFASTINT (w->last_point)
9803 && XFASTINT (w->last_modified) >= MODIFF
9804 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9805 && (XFASTINT (w->column_number_displayed)
9806 != (int) current_column ())) /* iftc */
9807 w->update_mode_line = Qt;
9808
9809 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9810
9811 /* The variable buffer_shared is set in redisplay_window and
9812 indicates that we redisplay a buffer in different windows. See
9813 there. */
9814 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9815 || cursor_type_changed);
9816
9817 /* If specs for an arrow have changed, do thorough redisplay
9818 to ensure we remove any arrow that should no longer exist. */
9819 if (overlay_arrows_changed_p ())
9820 consider_all_windows_p = windows_or_buffers_changed = 1;
9821
9822 /* Normally the message* functions will have already displayed and
9823 updated the echo area, but the frame may have been trashed, or
9824 the update may have been preempted, so display the echo area
9825 again here. Checking message_cleared_p captures the case that
9826 the echo area should be cleared. */
9827 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9828 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9829 || (message_cleared_p
9830 && minibuf_level == 0
9831 /* If the mini-window is currently selected, this means the
9832 echo-area doesn't show through. */
9833 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9834 {
9835 int window_height_changed_p = echo_area_display (0);
9836 must_finish = 1;
9837
9838 /* If we don't display the current message, don't clear the
9839 message_cleared_p flag, because, if we did, we wouldn't clear
9840 the echo area in the next redisplay which doesn't preserve
9841 the echo area. */
9842 if (!display_last_displayed_message_p)
9843 message_cleared_p = 0;
9844
9845 if (fonts_changed_p)
9846 goto retry;
9847 else if (window_height_changed_p)
9848 {
9849 consider_all_windows_p = 1;
9850 ++update_mode_lines;
9851 ++windows_or_buffers_changed;
9852
9853 /* If window configuration was changed, frames may have been
9854 marked garbaged. Clear them or we will experience
9855 surprises wrt scrolling. */
9856 if (frame_garbaged)
9857 clear_garbaged_frames ();
9858 }
9859 }
9860 else if (EQ (selected_window, minibuf_window)
9861 && (current_buffer->clip_changed
9862 || XFASTINT (w->last_modified) < MODIFF
9863 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9864 && resize_mini_window (w, 0))
9865 {
9866 /* Resized active mini-window to fit the size of what it is
9867 showing if its contents might have changed. */
9868 must_finish = 1;
9869 consider_all_windows_p = 1;
9870 ++windows_or_buffers_changed;
9871 ++update_mode_lines;
9872
9873 /* If window configuration was changed, frames may have been
9874 marked garbaged. Clear them or we will experience
9875 surprises wrt scrolling. */
9876 if (frame_garbaged)
9877 clear_garbaged_frames ();
9878 }
9879
9880
9881 /* If showing the region, and mark has changed, we must redisplay
9882 the whole window. The assignment to this_line_start_pos prevents
9883 the optimization directly below this if-statement. */
9884 if (((!NILP (Vtransient_mark_mode)
9885 && !NILP (XBUFFER (w->buffer)->mark_active))
9886 != !NILP (w->region_showing))
9887 || (!NILP (w->region_showing)
9888 && !EQ (w->region_showing,
9889 Fmarker_position (XBUFFER (w->buffer)->mark))))
9890 CHARPOS (this_line_start_pos) = 0;
9891
9892 /* Optimize the case that only the line containing the cursor in the
9893 selected window has changed. Variables starting with this_ are
9894 set in display_line and record information about the line
9895 containing the cursor. */
9896 tlbufpos = this_line_start_pos;
9897 tlendpos = this_line_end_pos;
9898 if (!consider_all_windows_p
9899 && CHARPOS (tlbufpos) > 0
9900 && NILP (w->update_mode_line)
9901 && !current_buffer->clip_changed
9902 && !current_buffer->prevent_redisplay_optimizations_p
9903 && FRAME_VISIBLE_P (XFRAME (w->frame))
9904 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9905 /* Make sure recorded data applies to current buffer, etc. */
9906 && this_line_buffer == current_buffer
9907 && current_buffer == XBUFFER (w->buffer)
9908 && NILP (w->force_start)
9909 && NILP (w->optional_new_start)
9910 /* Point must be on the line that we have info recorded about. */
9911 && PT >= CHARPOS (tlbufpos)
9912 && PT <= Z - CHARPOS (tlendpos)
9913 /* All text outside that line, including its final newline,
9914 must be unchanged */
9915 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9916 CHARPOS (tlendpos)))
9917 {
9918 if (CHARPOS (tlbufpos) > BEGV
9919 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9920 && (CHARPOS (tlbufpos) == ZV
9921 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9922 /* Former continuation line has disappeared by becoming empty */
9923 goto cancel;
9924 else if (XFASTINT (w->last_modified) < MODIFF
9925 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9926 || MINI_WINDOW_P (w))
9927 {
9928 /* We have to handle the case of continuation around a
9929 wide-column character (See the comment in indent.c around
9930 line 885).
9931
9932 For instance, in the following case:
9933
9934 -------- Insert --------
9935 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9936 J_I_ ==> J_I_ `^^' are cursors.
9937 ^^ ^^
9938 -------- --------
9939
9940 As we have to redraw the line above, we should goto cancel. */
9941
9942 struct it it;
9943 int line_height_before = this_line_pixel_height;
9944
9945 /* Note that start_display will handle the case that the
9946 line starting at tlbufpos is a continuation lines. */
9947 start_display (&it, w, tlbufpos);
9948
9949 /* Implementation note: It this still necessary? */
9950 if (it.current_x != this_line_start_x)
9951 goto cancel;
9952
9953 TRACE ((stderr, "trying display optimization 1\n"));
9954 w->cursor.vpos = -1;
9955 overlay_arrow_seen = 0;
9956 it.vpos = this_line_vpos;
9957 it.current_y = this_line_y;
9958 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9959 display_line (&it);
9960
9961 /* If line contains point, is not continued,
9962 and ends at same distance from eob as before, we win */
9963 if (w->cursor.vpos >= 0
9964 /* Line is not continued, otherwise this_line_start_pos
9965 would have been set to 0 in display_line. */
9966 && CHARPOS (this_line_start_pos)
9967 /* Line ends as before. */
9968 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9969 /* Line has same height as before. Otherwise other lines
9970 would have to be shifted up or down. */
9971 && this_line_pixel_height == line_height_before)
9972 {
9973 /* If this is not the window's last line, we must adjust
9974 the charstarts of the lines below. */
9975 if (it.current_y < it.last_visible_y)
9976 {
9977 struct glyph_row *row
9978 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9979 int delta, delta_bytes;
9980
9981 if (Z - CHARPOS (tlendpos) == ZV)
9982 {
9983 /* This line ends at end of (accessible part of)
9984 buffer. There is no newline to count. */
9985 delta = (Z
9986 - CHARPOS (tlendpos)
9987 - MATRIX_ROW_START_CHARPOS (row));
9988 delta_bytes = (Z_BYTE
9989 - BYTEPOS (tlendpos)
9990 - MATRIX_ROW_START_BYTEPOS (row));
9991 }
9992 else
9993 {
9994 /* This line ends in a newline. Must take
9995 account of the newline and the rest of the
9996 text that follows. */
9997 delta = (Z
9998 - CHARPOS (tlendpos)
9999 - MATRIX_ROW_START_CHARPOS (row));
10000 delta_bytes = (Z_BYTE
10001 - BYTEPOS (tlendpos)
10002 - MATRIX_ROW_START_BYTEPOS (row));
10003 }
10004
10005 increment_matrix_positions (w->current_matrix,
10006 this_line_vpos + 1,
10007 w->current_matrix->nrows,
10008 delta, delta_bytes);
10009 }
10010
10011 /* If this row displays text now but previously didn't,
10012 or vice versa, w->window_end_vpos may have to be
10013 adjusted. */
10014 if ((it.glyph_row - 1)->displays_text_p)
10015 {
10016 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10017 XSETINT (w->window_end_vpos, this_line_vpos);
10018 }
10019 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10020 && this_line_vpos > 0)
10021 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10022 w->window_end_valid = Qnil;
10023
10024 /* Update hint: No need to try to scroll in update_window. */
10025 w->desired_matrix->no_scrolling_p = 1;
10026
10027 #if GLYPH_DEBUG
10028 *w->desired_matrix->method = 0;
10029 debug_method_add (w, "optimization 1");
10030 #endif
10031 #ifdef HAVE_WINDOW_SYSTEM
10032 update_window_fringes (w, 0);
10033 #endif
10034 goto update;
10035 }
10036 else
10037 goto cancel;
10038 }
10039 else if (/* Cursor position hasn't changed. */
10040 PT == XFASTINT (w->last_point)
10041 /* Make sure the cursor was last displayed
10042 in this window. Otherwise we have to reposition it. */
10043 && 0 <= w->cursor.vpos
10044 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10045 {
10046 if (!must_finish)
10047 {
10048 do_pending_window_change (1);
10049
10050 /* We used to always goto end_of_redisplay here, but this
10051 isn't enough if we have a blinking cursor. */
10052 if (w->cursor_off_p == w->last_cursor_off_p)
10053 goto end_of_redisplay;
10054 }
10055 goto update;
10056 }
10057 /* If highlighting the region, or if the cursor is in the echo area,
10058 then we can't just move the cursor. */
10059 else if (! (!NILP (Vtransient_mark_mode)
10060 && !NILP (current_buffer->mark_active))
10061 && (EQ (selected_window, current_buffer->last_selected_window)
10062 || highlight_nonselected_windows)
10063 && NILP (w->region_showing)
10064 && NILP (Vshow_trailing_whitespace)
10065 && !cursor_in_echo_area)
10066 {
10067 struct it it;
10068 struct glyph_row *row;
10069
10070 /* Skip from tlbufpos to PT and see where it is. Note that
10071 PT may be in invisible text. If so, we will end at the
10072 next visible position. */
10073 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10074 NULL, DEFAULT_FACE_ID);
10075 it.current_x = this_line_start_x;
10076 it.current_y = this_line_y;
10077 it.vpos = this_line_vpos;
10078
10079 /* The call to move_it_to stops in front of PT, but
10080 moves over before-strings. */
10081 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10082
10083 if (it.vpos == this_line_vpos
10084 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10085 row->enabled_p))
10086 {
10087 xassert (this_line_vpos == it.vpos);
10088 xassert (this_line_y == it.current_y);
10089 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10090 #if GLYPH_DEBUG
10091 *w->desired_matrix->method = 0;
10092 debug_method_add (w, "optimization 3");
10093 #endif
10094 goto update;
10095 }
10096 else
10097 goto cancel;
10098 }
10099
10100 cancel:
10101 /* Text changed drastically or point moved off of line. */
10102 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10103 }
10104
10105 CHARPOS (this_line_start_pos) = 0;
10106 consider_all_windows_p |= buffer_shared > 1;
10107 ++clear_face_cache_count;
10108
10109
10110 /* Build desired matrices, and update the display. If
10111 consider_all_windows_p is non-zero, do it for all windows on all
10112 frames. Otherwise do it for selected_window, only. */
10113
10114 if (consider_all_windows_p)
10115 {
10116 Lisp_Object tail, frame;
10117 int i, n = 0, size = 50;
10118 struct frame **updated
10119 = (struct frame **) alloca (size * sizeof *updated);
10120
10121 /* Clear the face cache eventually. */
10122 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10123 {
10124 clear_face_cache (0);
10125 clear_face_cache_count = 0;
10126 }
10127
10128 /* Recompute # windows showing selected buffer. This will be
10129 incremented each time such a window is displayed. */
10130 buffer_shared = 0;
10131
10132 FOR_EACH_FRAME (tail, frame)
10133 {
10134 struct frame *f = XFRAME (frame);
10135
10136 if (FRAME_WINDOW_P (f) || f == sf)
10137 {
10138 if (! EQ (frame, selected_frame))
10139 /* Select the frame, for the sake of frame-local
10140 variables. */
10141 select_frame_for_redisplay (frame);
10142
10143 #ifdef HAVE_WINDOW_SYSTEM
10144 if (clear_face_cache_count % 50 == 0
10145 && FRAME_WINDOW_P (f))
10146 clear_image_cache (f, 0);
10147 #endif /* HAVE_WINDOW_SYSTEM */
10148
10149 /* Mark all the scroll bars to be removed; we'll redeem
10150 the ones we want when we redisplay their windows. */
10151 if (condemn_scroll_bars_hook)
10152 condemn_scroll_bars_hook (f);
10153
10154 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10155 redisplay_windows (FRAME_ROOT_WINDOW (f));
10156
10157 /* Any scroll bars which redisplay_windows should have
10158 nuked should now go away. */
10159 if (judge_scroll_bars_hook)
10160 judge_scroll_bars_hook (f);
10161
10162 /* If fonts changed, display again. */
10163 /* ??? rms: I suspect it is a mistake to jump all the way
10164 back to retry here. It should just retry this frame. */
10165 if (fonts_changed_p)
10166 goto retry;
10167
10168 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10169 {
10170 /* See if we have to hscroll. */
10171 if (hscroll_windows (f->root_window))
10172 goto retry;
10173
10174 /* Prevent various kinds of signals during display
10175 update. stdio is not robust about handling
10176 signals, which can cause an apparent I/O
10177 error. */
10178 if (interrupt_input)
10179 unrequest_sigio ();
10180 STOP_POLLING;
10181
10182 /* Update the display. */
10183 set_window_update_flags (XWINDOW (f->root_window), 1);
10184 pause |= update_frame (f, 0, 0);
10185 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10186 if (pause)
10187 break;
10188 #endif
10189
10190 if (n == size)
10191 {
10192 int nbytes = size * sizeof *updated;
10193 struct frame **p = (struct frame **) alloca (2 * nbytes);
10194 bcopy (updated, p, nbytes);
10195 size *= 2;
10196 }
10197
10198 updated[n++] = f;
10199 }
10200 }
10201 }
10202
10203 if (!pause)
10204 {
10205 /* Do the mark_window_display_accurate after all windows have
10206 been redisplayed because this call resets flags in buffers
10207 which are needed for proper redisplay. */
10208 for (i = 0; i < n; ++i)
10209 {
10210 struct frame *f = updated[i];
10211 mark_window_display_accurate (f->root_window, 1);
10212 if (frame_up_to_date_hook)
10213 frame_up_to_date_hook (f);
10214 }
10215 }
10216 }
10217 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10218 {
10219 Lisp_Object mini_window;
10220 struct frame *mini_frame;
10221
10222 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10223 /* Use list_of_error, not Qerror, so that
10224 we catch only errors and don't run the debugger. */
10225 internal_condition_case_1 (redisplay_window_1, selected_window,
10226 list_of_error,
10227 redisplay_window_error);
10228
10229 /* Compare desired and current matrices, perform output. */
10230
10231 update:
10232 /* If fonts changed, display again. */
10233 if (fonts_changed_p)
10234 goto retry;
10235
10236 /* Prevent various kinds of signals during display update.
10237 stdio is not robust about handling signals,
10238 which can cause an apparent I/O error. */
10239 if (interrupt_input)
10240 unrequest_sigio ();
10241 STOP_POLLING;
10242
10243 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10244 {
10245 if (hscroll_windows (selected_window))
10246 goto retry;
10247
10248 XWINDOW (selected_window)->must_be_updated_p = 1;
10249 pause = update_frame (sf, 0, 0);
10250 }
10251
10252 /* We may have called echo_area_display at the top of this
10253 function. If the echo area is on another frame, that may
10254 have put text on a frame other than the selected one, so the
10255 above call to update_frame would not have caught it. Catch
10256 it here. */
10257 mini_window = FRAME_MINIBUF_WINDOW (sf);
10258 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10259
10260 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10261 {
10262 XWINDOW (mini_window)->must_be_updated_p = 1;
10263 pause |= update_frame (mini_frame, 0, 0);
10264 if (!pause && hscroll_windows (mini_window))
10265 goto retry;
10266 }
10267 }
10268
10269 /* If display was paused because of pending input, make sure we do a
10270 thorough update the next time. */
10271 if (pause)
10272 {
10273 /* Prevent the optimization at the beginning of
10274 redisplay_internal that tries a single-line update of the
10275 line containing the cursor in the selected window. */
10276 CHARPOS (this_line_start_pos) = 0;
10277
10278 /* Let the overlay arrow be updated the next time. */
10279 update_overlay_arrows (0);
10280
10281 /* If we pause after scrolling, some rows in the current
10282 matrices of some windows are not valid. */
10283 if (!WINDOW_FULL_WIDTH_P (w)
10284 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10285 update_mode_lines = 1;
10286 }
10287 else
10288 {
10289 if (!consider_all_windows_p)
10290 {
10291 /* This has already been done above if
10292 consider_all_windows_p is set. */
10293 mark_window_display_accurate_1 (w, 1);
10294
10295 /* Say overlay arrows are up to date. */
10296 update_overlay_arrows (1);
10297
10298 if (frame_up_to_date_hook != 0)
10299 frame_up_to_date_hook (sf);
10300 }
10301
10302 update_mode_lines = 0;
10303 windows_or_buffers_changed = 0;
10304 cursor_type_changed = 0;
10305 }
10306
10307 /* Start SIGIO interrupts coming again. Having them off during the
10308 code above makes it less likely one will discard output, but not
10309 impossible, since there might be stuff in the system buffer here.
10310 But it is much hairier to try to do anything about that. */
10311 if (interrupt_input)
10312 request_sigio ();
10313 RESUME_POLLING;
10314
10315 /* If a frame has become visible which was not before, redisplay
10316 again, so that we display it. Expose events for such a frame
10317 (which it gets when becoming visible) don't call the parts of
10318 redisplay constructing glyphs, so simply exposing a frame won't
10319 display anything in this case. So, we have to display these
10320 frames here explicitly. */
10321 if (!pause)
10322 {
10323 Lisp_Object tail, frame;
10324 int new_count = 0;
10325
10326 FOR_EACH_FRAME (tail, frame)
10327 {
10328 int this_is_visible = 0;
10329
10330 if (XFRAME (frame)->visible)
10331 this_is_visible = 1;
10332 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10333 if (XFRAME (frame)->visible)
10334 this_is_visible = 1;
10335
10336 if (this_is_visible)
10337 new_count++;
10338 }
10339
10340 if (new_count != number_of_visible_frames)
10341 windows_or_buffers_changed++;
10342 }
10343
10344 /* Change frame size now if a change is pending. */
10345 do_pending_window_change (1);
10346
10347 /* If we just did a pending size change, or have additional
10348 visible frames, redisplay again. */
10349 if (windows_or_buffers_changed && !pause)
10350 goto retry;
10351
10352 end_of_redisplay:
10353 unbind_to (count, Qnil);
10354 RESUME_POLLING;
10355 }
10356
10357
10358 /* Redisplay, but leave alone any recent echo area message unless
10359 another message has been requested in its place.
10360
10361 This is useful in situations where you need to redisplay but no
10362 user action has occurred, making it inappropriate for the message
10363 area to be cleared. See tracking_off and
10364 wait_reading_process_input for examples of these situations.
10365
10366 FROM_WHERE is an integer saying from where this function was
10367 called. This is useful for debugging. */
10368
10369 void
10370 redisplay_preserve_echo_area (from_where)
10371 int from_where;
10372 {
10373 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10374
10375 if (!NILP (echo_area_buffer[1]))
10376 {
10377 /* We have a previously displayed message, but no current
10378 message. Redisplay the previous message. */
10379 display_last_displayed_message_p = 1;
10380 redisplay_internal (1);
10381 display_last_displayed_message_p = 0;
10382 }
10383 else
10384 redisplay_internal (1);
10385 }
10386
10387
10388 /* Function registered with record_unwind_protect in
10389 redisplay_internal. Reset redisplaying_p to the value it had
10390 before redisplay_internal was called, and clear
10391 prevent_freeing_realized_faces_p. It also selects the previously
10392 selected frame. */
10393
10394 static Lisp_Object
10395 unwind_redisplay (val)
10396 Lisp_Object val;
10397 {
10398 Lisp_Object old_redisplaying_p, old_frame;
10399
10400 old_redisplaying_p = XCAR (val);
10401 redisplaying_p = XFASTINT (old_redisplaying_p);
10402 old_frame = XCDR (val);
10403 if (! EQ (old_frame, selected_frame))
10404 select_frame_for_redisplay (old_frame);
10405 return Qnil;
10406 }
10407
10408
10409 /* Mark the display of window W as accurate or inaccurate. If
10410 ACCURATE_P is non-zero mark display of W as accurate. If
10411 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10412 redisplay_internal is called. */
10413
10414 static void
10415 mark_window_display_accurate_1 (w, accurate_p)
10416 struct window *w;
10417 int accurate_p;
10418 {
10419 if (BUFFERP (w->buffer))
10420 {
10421 struct buffer *b = XBUFFER (w->buffer);
10422
10423 w->last_modified
10424 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10425 w->last_overlay_modified
10426 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10427 w->last_had_star
10428 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10429
10430 if (accurate_p)
10431 {
10432 b->clip_changed = 0;
10433 b->prevent_redisplay_optimizations_p = 0;
10434
10435 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10436 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10437 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10438 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10439
10440 w->current_matrix->buffer = b;
10441 w->current_matrix->begv = BUF_BEGV (b);
10442 w->current_matrix->zv = BUF_ZV (b);
10443
10444 w->last_cursor = w->cursor;
10445 w->last_cursor_off_p = w->cursor_off_p;
10446
10447 if (w == XWINDOW (selected_window))
10448 w->last_point = make_number (BUF_PT (b));
10449 else
10450 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10451 }
10452 }
10453
10454 if (accurate_p)
10455 {
10456 w->window_end_valid = w->buffer;
10457 #if 0 /* This is incorrect with variable-height lines. */
10458 xassert (XINT (w->window_end_vpos)
10459 < (WINDOW_TOTAL_LINES (w)
10460 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10461 #endif
10462 w->update_mode_line = Qnil;
10463 }
10464 }
10465
10466
10467 /* Mark the display of windows in the window tree rooted at WINDOW as
10468 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10469 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10470 be redisplayed the next time redisplay_internal is called. */
10471
10472 void
10473 mark_window_display_accurate (window, accurate_p)
10474 Lisp_Object window;
10475 int accurate_p;
10476 {
10477 struct window *w;
10478
10479 for (; !NILP (window); window = w->next)
10480 {
10481 w = XWINDOW (window);
10482 mark_window_display_accurate_1 (w, accurate_p);
10483
10484 if (!NILP (w->vchild))
10485 mark_window_display_accurate (w->vchild, accurate_p);
10486 if (!NILP (w->hchild))
10487 mark_window_display_accurate (w->hchild, accurate_p);
10488 }
10489
10490 if (accurate_p)
10491 {
10492 update_overlay_arrows (1);
10493 }
10494 else
10495 {
10496 /* Force a thorough redisplay the next time by setting
10497 last_arrow_position and last_arrow_string to t, which is
10498 unequal to any useful value of Voverlay_arrow_... */
10499 update_overlay_arrows (-1);
10500 }
10501 }
10502
10503
10504 /* Return value in display table DP (Lisp_Char_Table *) for character
10505 C. Since a display table doesn't have any parent, we don't have to
10506 follow parent. Do not call this function directly but use the
10507 macro DISP_CHAR_VECTOR. */
10508
10509 Lisp_Object
10510 disp_char_vector (dp, c)
10511 struct Lisp_Char_Table *dp;
10512 int c;
10513 {
10514 int code[4], i;
10515 Lisp_Object val;
10516
10517 if (SINGLE_BYTE_CHAR_P (c))
10518 return (dp->contents[c]);
10519
10520 SPLIT_CHAR (c, code[0], code[1], code[2]);
10521 if (code[1] < 32)
10522 code[1] = -1;
10523 else if (code[2] < 32)
10524 code[2] = -1;
10525
10526 /* Here, the possible range of code[0] (== charset ID) is
10527 128..max_charset. Since the top level char table contains data
10528 for multibyte characters after 256th element, we must increment
10529 code[0] by 128 to get a correct index. */
10530 code[0] += 128;
10531 code[3] = -1; /* anchor */
10532
10533 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10534 {
10535 val = dp->contents[code[i]];
10536 if (!SUB_CHAR_TABLE_P (val))
10537 return (NILP (val) ? dp->defalt : val);
10538 }
10539
10540 /* Here, val is a sub char table. We return the default value of
10541 it. */
10542 return (dp->defalt);
10543 }
10544
10545
10546 \f
10547 /***********************************************************************
10548 Window Redisplay
10549 ***********************************************************************/
10550
10551 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10552
10553 static void
10554 redisplay_windows (window)
10555 Lisp_Object window;
10556 {
10557 while (!NILP (window))
10558 {
10559 struct window *w = XWINDOW (window);
10560
10561 if (!NILP (w->hchild))
10562 redisplay_windows (w->hchild);
10563 else if (!NILP (w->vchild))
10564 redisplay_windows (w->vchild);
10565 else
10566 {
10567 displayed_buffer = XBUFFER (w->buffer);
10568 /* Use list_of_error, not Qerror, so that
10569 we catch only errors and don't run the debugger. */
10570 internal_condition_case_1 (redisplay_window_0, window,
10571 list_of_error,
10572 redisplay_window_error);
10573 }
10574
10575 window = w->next;
10576 }
10577 }
10578
10579 static Lisp_Object
10580 redisplay_window_error ()
10581 {
10582 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10583 return Qnil;
10584 }
10585
10586 static Lisp_Object
10587 redisplay_window_0 (window)
10588 Lisp_Object window;
10589 {
10590 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10591 redisplay_window (window, 0);
10592 return Qnil;
10593 }
10594
10595 static Lisp_Object
10596 redisplay_window_1 (window)
10597 Lisp_Object window;
10598 {
10599 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10600 redisplay_window (window, 1);
10601 return Qnil;
10602 }
10603 \f
10604
10605 /* Increment GLYPH until it reaches END or CONDITION fails while
10606 adding (GLYPH)->pixel_width to X. */
10607
10608 #define SKIP_GLYPHS(glyph, end, x, condition) \
10609 do \
10610 { \
10611 (x) += (glyph)->pixel_width; \
10612 ++(glyph); \
10613 } \
10614 while ((glyph) < (end) && (condition))
10615
10616
10617 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10618 DELTA is the number of bytes by which positions recorded in ROW
10619 differ from current buffer positions. */
10620
10621 void
10622 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10623 struct window *w;
10624 struct glyph_row *row;
10625 struct glyph_matrix *matrix;
10626 int delta, delta_bytes, dy, dvpos;
10627 {
10628 struct glyph *glyph = row->glyphs[TEXT_AREA];
10629 struct glyph *end = glyph + row->used[TEXT_AREA];
10630 /* The first glyph that starts a sequence of glyphs from string. */
10631 struct glyph *string_start;
10632 /* The X coordinate of string_start. */
10633 int string_start_x;
10634 /* The last known character position. */
10635 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10636 /* The last known character position before string_start. */
10637 int string_before_pos;
10638 int x = row->x;
10639 int pt_old = PT - delta;
10640
10641 /* Skip over glyphs not having an object at the start of the row.
10642 These are special glyphs like truncation marks on terminal
10643 frames. */
10644 if (row->displays_text_p)
10645 while (glyph < end
10646 && INTEGERP (glyph->object)
10647 && glyph->charpos < 0)
10648 {
10649 x += glyph->pixel_width;
10650 ++glyph;
10651 }
10652
10653 string_start = NULL;
10654 while (glyph < end
10655 && !INTEGERP (glyph->object)
10656 && (!BUFFERP (glyph->object)
10657 || (last_pos = glyph->charpos) < pt_old))
10658 {
10659 if (! STRINGP (glyph->object))
10660 {
10661 string_start = NULL;
10662 x += glyph->pixel_width;
10663 ++glyph;
10664 }
10665 else
10666 {
10667 string_before_pos = last_pos;
10668 string_start = glyph;
10669 string_start_x = x;
10670 /* Skip all glyphs from string. */
10671 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10672 }
10673 }
10674
10675 if (string_start
10676 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10677 {
10678 /* We may have skipped over point because the previous glyphs
10679 are from string. As there's no easy way to know the
10680 character position of the current glyph, find the correct
10681 glyph on point by scanning from string_start again. */
10682 Lisp_Object limit;
10683 Lisp_Object string;
10684 int pos;
10685
10686 limit = make_number (pt_old + 1);
10687 end = glyph;
10688 glyph = string_start;
10689 x = string_start_x;
10690 string = glyph->object;
10691 pos = string_buffer_position (w, string, string_before_pos);
10692 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10693 because we always put cursor after overlay strings. */
10694 while (pos == 0 && glyph < end)
10695 {
10696 string = glyph->object;
10697 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10698 if (glyph < end)
10699 pos = string_buffer_position (w, glyph->object, string_before_pos);
10700 }
10701
10702 while (glyph < end)
10703 {
10704 pos = XINT (Fnext_single_char_property_change
10705 (make_number (pos), Qdisplay, Qnil, limit));
10706 if (pos > pt_old)
10707 break;
10708 /* Skip glyphs from the same string. */
10709 string = glyph->object;
10710 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10711 /* Skip glyphs from an overlay. */
10712 while (glyph < end
10713 && ! string_buffer_position (w, glyph->object, pos))
10714 {
10715 string = glyph->object;
10716 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10717 }
10718 }
10719 }
10720
10721 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10722 w->cursor.x = x;
10723 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10724 w->cursor.y = row->y + dy;
10725
10726 if (w == XWINDOW (selected_window))
10727 {
10728 if (!row->continued_p
10729 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10730 && row->x == 0)
10731 {
10732 this_line_buffer = XBUFFER (w->buffer);
10733
10734 CHARPOS (this_line_start_pos)
10735 = MATRIX_ROW_START_CHARPOS (row) + delta;
10736 BYTEPOS (this_line_start_pos)
10737 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10738
10739 CHARPOS (this_line_end_pos)
10740 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10741 BYTEPOS (this_line_end_pos)
10742 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10743
10744 this_line_y = w->cursor.y;
10745 this_line_pixel_height = row->height;
10746 this_line_vpos = w->cursor.vpos;
10747 this_line_start_x = row->x;
10748 }
10749 else
10750 CHARPOS (this_line_start_pos) = 0;
10751 }
10752 }
10753
10754
10755 /* Run window scroll functions, if any, for WINDOW with new window
10756 start STARTP. Sets the window start of WINDOW to that position.
10757
10758 We assume that the window's buffer is really current. */
10759
10760 static INLINE struct text_pos
10761 run_window_scroll_functions (window, startp)
10762 Lisp_Object window;
10763 struct text_pos startp;
10764 {
10765 struct window *w = XWINDOW (window);
10766 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10767
10768 if (current_buffer != XBUFFER (w->buffer))
10769 abort ();
10770
10771 if (!NILP (Vwindow_scroll_functions))
10772 {
10773 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10774 make_number (CHARPOS (startp)));
10775 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10776 /* In case the hook functions switch buffers. */
10777 if (current_buffer != XBUFFER (w->buffer))
10778 set_buffer_internal_1 (XBUFFER (w->buffer));
10779 }
10780
10781 return startp;
10782 }
10783
10784
10785 /* Make sure the line containing the cursor is fully visible.
10786 A value of 1 means there is nothing to be done.
10787 (Either the line is fully visible, or it cannot be made so,
10788 or we cannot tell.)
10789
10790 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10791 is higher than window.
10792
10793 A value of 0 means the caller should do scrolling
10794 as if point had gone off the screen. */
10795
10796 static int
10797 make_cursor_line_fully_visible (w, force_p)
10798 struct window *w;
10799 int force_p;
10800 {
10801 struct glyph_matrix *matrix;
10802 struct glyph_row *row;
10803 int window_height;
10804
10805 /* It's not always possible to find the cursor, e.g, when a window
10806 is full of overlay strings. Don't do anything in that case. */
10807 if (w->cursor.vpos < 0)
10808 return 1;
10809
10810 matrix = w->desired_matrix;
10811 row = MATRIX_ROW (matrix, w->cursor.vpos);
10812
10813 /* If the cursor row is not partially visible, there's nothing to do. */
10814 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10815 return 1;
10816
10817 /* If the row the cursor is in is taller than the window's height,
10818 it's not clear what to do, so do nothing. */
10819 window_height = window_box_height (w);
10820 if (row->height >= window_height)
10821 {
10822 if (!force_p || w->vscroll)
10823 return 1;
10824 }
10825 return 0;
10826
10827 #if 0
10828 /* This code used to try to scroll the window just enough to make
10829 the line visible. It returned 0 to say that the caller should
10830 allocate larger glyph matrices. */
10831
10832 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10833 {
10834 int dy = row->height - row->visible_height;
10835 w->vscroll = 0;
10836 w->cursor.y += dy;
10837 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10838 }
10839 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10840 {
10841 int dy = - (row->height - row->visible_height);
10842 w->vscroll = dy;
10843 w->cursor.y += dy;
10844 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10845 }
10846
10847 /* When we change the cursor y-position of the selected window,
10848 change this_line_y as well so that the display optimization for
10849 the cursor line of the selected window in redisplay_internal uses
10850 the correct y-position. */
10851 if (w == XWINDOW (selected_window))
10852 this_line_y = w->cursor.y;
10853
10854 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10855 redisplay with larger matrices. */
10856 if (matrix->nrows < required_matrix_height (w))
10857 {
10858 fonts_changed_p = 1;
10859 return 0;
10860 }
10861
10862 return 1;
10863 #endif /* 0 */
10864 }
10865
10866
10867 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10868 non-zero means only WINDOW is redisplayed in redisplay_internal.
10869 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10870 in redisplay_window to bring a partially visible line into view in
10871 the case that only the cursor has moved.
10872
10873 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10874 last screen line's vertical height extends past the end of the screen.
10875
10876 Value is
10877
10878 1 if scrolling succeeded
10879
10880 0 if scrolling didn't find point.
10881
10882 -1 if new fonts have been loaded so that we must interrupt
10883 redisplay, adjust glyph matrices, and try again. */
10884
10885 enum
10886 {
10887 SCROLLING_SUCCESS,
10888 SCROLLING_FAILED,
10889 SCROLLING_NEED_LARGER_MATRICES
10890 };
10891
10892 static int
10893 try_scrolling (window, just_this_one_p, scroll_conservatively,
10894 scroll_step, temp_scroll_step, last_line_misfit)
10895 Lisp_Object window;
10896 int just_this_one_p;
10897 EMACS_INT scroll_conservatively, scroll_step;
10898 int temp_scroll_step;
10899 int last_line_misfit;
10900 {
10901 struct window *w = XWINDOW (window);
10902 struct frame *f = XFRAME (w->frame);
10903 struct text_pos scroll_margin_pos;
10904 struct text_pos pos;
10905 struct text_pos startp;
10906 struct it it;
10907 Lisp_Object window_end;
10908 int this_scroll_margin;
10909 int dy = 0;
10910 int scroll_max;
10911 int rc;
10912 int amount_to_scroll = 0;
10913 Lisp_Object aggressive;
10914 int height;
10915 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10916
10917 #if GLYPH_DEBUG
10918 debug_method_add (w, "try_scrolling");
10919 #endif
10920
10921 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10922
10923 /* Compute scroll margin height in pixels. We scroll when point is
10924 within this distance from the top or bottom of the window. */
10925 if (scroll_margin > 0)
10926 {
10927 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10928 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10929 }
10930 else
10931 this_scroll_margin = 0;
10932
10933 /* Force scroll_conservatively to have a reasonable value so it doesn't
10934 cause an overflow while computing how much to scroll. */
10935 if (scroll_conservatively)
10936 scroll_conservatively = min (scroll_conservatively,
10937 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10938
10939 /* Compute how much we should try to scroll maximally to bring point
10940 into view. */
10941 if (scroll_step || scroll_conservatively || temp_scroll_step)
10942 scroll_max = max (scroll_step,
10943 max (scroll_conservatively, temp_scroll_step));
10944 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10945 || NUMBERP (current_buffer->scroll_up_aggressively))
10946 /* We're trying to scroll because of aggressive scrolling
10947 but no scroll_step is set. Choose an arbitrary one. Maybe
10948 there should be a variable for this. */
10949 scroll_max = 10;
10950 else
10951 scroll_max = 0;
10952 scroll_max *= FRAME_LINE_HEIGHT (f);
10953
10954 /* Decide whether we have to scroll down. Start at the window end
10955 and move this_scroll_margin up to find the position of the scroll
10956 margin. */
10957 window_end = Fwindow_end (window, Qt);
10958
10959 too_near_end:
10960
10961 CHARPOS (scroll_margin_pos) = XINT (window_end);
10962 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10963
10964 if (this_scroll_margin || extra_scroll_margin_lines)
10965 {
10966 start_display (&it, w, scroll_margin_pos);
10967 if (this_scroll_margin)
10968 move_it_vertically (&it, - this_scroll_margin);
10969 if (extra_scroll_margin_lines)
10970 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
10971 scroll_margin_pos = it.current.pos;
10972 }
10973
10974 if (PT >= CHARPOS (scroll_margin_pos))
10975 {
10976 int y0;
10977
10978 /* Point is in the scroll margin at the bottom of the window, or
10979 below. Compute a new window start that makes point visible. */
10980
10981 /* Compute the distance from the scroll margin to PT.
10982 Give up if the distance is greater than scroll_max. */
10983 start_display (&it, w, scroll_margin_pos);
10984 y0 = it.current_y;
10985 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10986 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10987
10988 /* To make point visible, we have to move the window start
10989 down so that the line the cursor is in is visible, which
10990 means we have to add in the height of the cursor line. */
10991 dy = line_bottom_y (&it) - y0;
10992
10993 if (dy > scroll_max)
10994 return SCROLLING_FAILED;
10995
10996 /* Move the window start down. If scrolling conservatively,
10997 move it just enough down to make point visible. If
10998 scroll_step is set, move it down by scroll_step. */
10999 start_display (&it, w, startp);
11000
11001 if (scroll_conservatively)
11002 /* Set AMOUNT_TO_SCROLL to at least one line,
11003 and at most scroll_conservatively lines. */
11004 amount_to_scroll
11005 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11006 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11007 else if (scroll_step || temp_scroll_step)
11008 amount_to_scroll = scroll_max;
11009 else
11010 {
11011 aggressive = current_buffer->scroll_up_aggressively;
11012 height = WINDOW_BOX_TEXT_HEIGHT (w);
11013 if (NUMBERP (aggressive))
11014 amount_to_scroll = XFLOATINT (aggressive) * height;
11015 }
11016
11017 if (amount_to_scroll <= 0)
11018 return SCROLLING_FAILED;
11019
11020 /* If moving by amount_to_scroll leaves STARTP unchanged,
11021 move it down one screen line. */
11022
11023 move_it_vertically (&it, amount_to_scroll);
11024 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11025 move_it_by_lines (&it, 1, 1);
11026 startp = it.current.pos;
11027 }
11028 else
11029 {
11030 /* See if point is inside the scroll margin at the top of the
11031 window. */
11032 scroll_margin_pos = startp;
11033 if (this_scroll_margin)
11034 {
11035 start_display (&it, w, startp);
11036 move_it_vertically (&it, this_scroll_margin);
11037 scroll_margin_pos = it.current.pos;
11038 }
11039
11040 if (PT < CHARPOS (scroll_margin_pos))
11041 {
11042 /* Point is in the scroll margin at the top of the window or
11043 above what is displayed in the window. */
11044 int y0;
11045
11046 /* Compute the vertical distance from PT to the scroll
11047 margin position. Give up if distance is greater than
11048 scroll_max. */
11049 SET_TEXT_POS (pos, PT, PT_BYTE);
11050 start_display (&it, w, pos);
11051 y0 = it.current_y;
11052 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11053 it.last_visible_y, -1,
11054 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11055 dy = it.current_y - y0;
11056 if (dy > scroll_max)
11057 return SCROLLING_FAILED;
11058
11059 /* Compute new window start. */
11060 start_display (&it, w, startp);
11061
11062 if (scroll_conservatively)
11063 amount_to_scroll =
11064 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11065 else if (scroll_step || temp_scroll_step)
11066 amount_to_scroll = scroll_max;
11067 else
11068 {
11069 aggressive = current_buffer->scroll_down_aggressively;
11070 height = WINDOW_BOX_TEXT_HEIGHT (w);
11071 if (NUMBERP (aggressive))
11072 amount_to_scroll = XFLOATINT (aggressive) * height;
11073 }
11074
11075 if (amount_to_scroll <= 0)
11076 return SCROLLING_FAILED;
11077
11078 move_it_vertically (&it, - amount_to_scroll);
11079 startp = it.current.pos;
11080 }
11081 }
11082
11083 /* Run window scroll functions. */
11084 startp = run_window_scroll_functions (window, startp);
11085
11086 /* Display the window. Give up if new fonts are loaded, or if point
11087 doesn't appear. */
11088 if (!try_window (window, startp))
11089 rc = SCROLLING_NEED_LARGER_MATRICES;
11090 else if (w->cursor.vpos < 0)
11091 {
11092 clear_glyph_matrix (w->desired_matrix);
11093 rc = SCROLLING_FAILED;
11094 }
11095 else
11096 {
11097 /* Maybe forget recorded base line for line number display. */
11098 if (!just_this_one_p
11099 || current_buffer->clip_changed
11100 || BEG_UNCHANGED < CHARPOS (startp))
11101 w->base_line_number = Qnil;
11102
11103 /* If cursor ends up on a partially visible line,
11104 treat that as being off the bottom of the screen. */
11105 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11106 {
11107 clear_glyph_matrix (w->desired_matrix);
11108 ++extra_scroll_margin_lines;
11109 goto too_near_end;
11110 }
11111 rc = SCROLLING_SUCCESS;
11112 }
11113
11114 return rc;
11115 }
11116
11117
11118 /* Compute a suitable window start for window W if display of W starts
11119 on a continuation line. Value is non-zero if a new window start
11120 was computed.
11121
11122 The new window start will be computed, based on W's width, starting
11123 from the start of the continued line. It is the start of the
11124 screen line with the minimum distance from the old start W->start. */
11125
11126 static int
11127 compute_window_start_on_continuation_line (w)
11128 struct window *w;
11129 {
11130 struct text_pos pos, start_pos;
11131 int window_start_changed_p = 0;
11132
11133 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11134
11135 /* If window start is on a continuation line... Window start may be
11136 < BEGV in case there's invisible text at the start of the
11137 buffer (M-x rmail, for example). */
11138 if (CHARPOS (start_pos) > BEGV
11139 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11140 {
11141 struct it it;
11142 struct glyph_row *row;
11143
11144 /* Handle the case that the window start is out of range. */
11145 if (CHARPOS (start_pos) < BEGV)
11146 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11147 else if (CHARPOS (start_pos) > ZV)
11148 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11149
11150 /* Find the start of the continued line. This should be fast
11151 because scan_buffer is fast (newline cache). */
11152 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11153 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11154 row, DEFAULT_FACE_ID);
11155 reseat_at_previous_visible_line_start (&it);
11156
11157 /* If the line start is "too far" away from the window start,
11158 say it takes too much time to compute a new window start. */
11159 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11160 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11161 {
11162 int min_distance, distance;
11163
11164 /* Move forward by display lines to find the new window
11165 start. If window width was enlarged, the new start can
11166 be expected to be > the old start. If window width was
11167 decreased, the new window start will be < the old start.
11168 So, we're looking for the display line start with the
11169 minimum distance from the old window start. */
11170 pos = it.current.pos;
11171 min_distance = INFINITY;
11172 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11173 distance < min_distance)
11174 {
11175 min_distance = distance;
11176 pos = it.current.pos;
11177 move_it_by_lines (&it, 1, 0);
11178 }
11179
11180 /* Set the window start there. */
11181 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11182 window_start_changed_p = 1;
11183 }
11184 }
11185
11186 return window_start_changed_p;
11187 }
11188
11189
11190 /* Try cursor movement in case text has not changed in window WINDOW,
11191 with window start STARTP. Value is
11192
11193 CURSOR_MOVEMENT_SUCCESS if successful
11194
11195 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11196
11197 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11198 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11199 we want to scroll as if scroll-step were set to 1. See the code.
11200
11201 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11202 which case we have to abort this redisplay, and adjust matrices
11203 first. */
11204
11205 enum
11206 {
11207 CURSOR_MOVEMENT_SUCCESS,
11208 CURSOR_MOVEMENT_CANNOT_BE_USED,
11209 CURSOR_MOVEMENT_MUST_SCROLL,
11210 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11211 };
11212
11213 static int
11214 try_cursor_movement (window, startp, scroll_step)
11215 Lisp_Object window;
11216 struct text_pos startp;
11217 int *scroll_step;
11218 {
11219 struct window *w = XWINDOW (window);
11220 struct frame *f = XFRAME (w->frame);
11221 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11222
11223 #if GLYPH_DEBUG
11224 if (inhibit_try_cursor_movement)
11225 return rc;
11226 #endif
11227
11228 /* Handle case where text has not changed, only point, and it has
11229 not moved off the frame. */
11230 if (/* Point may be in this window. */
11231 PT >= CHARPOS (startp)
11232 /* Selective display hasn't changed. */
11233 && !current_buffer->clip_changed
11234 /* Function force-mode-line-update is used to force a thorough
11235 redisplay. It sets either windows_or_buffers_changed or
11236 update_mode_lines. So don't take a shortcut here for these
11237 cases. */
11238 && !update_mode_lines
11239 && !windows_or_buffers_changed
11240 && !cursor_type_changed
11241 /* Can't use this case if highlighting a region. When a
11242 region exists, cursor movement has to do more than just
11243 set the cursor. */
11244 && !(!NILP (Vtransient_mark_mode)
11245 && !NILP (current_buffer->mark_active))
11246 && NILP (w->region_showing)
11247 && NILP (Vshow_trailing_whitespace)
11248 /* Right after splitting windows, last_point may be nil. */
11249 && INTEGERP (w->last_point)
11250 /* This code is not used for mini-buffer for the sake of the case
11251 of redisplaying to replace an echo area message; since in
11252 that case the mini-buffer contents per se are usually
11253 unchanged. This code is of no real use in the mini-buffer
11254 since the handling of this_line_start_pos, etc., in redisplay
11255 handles the same cases. */
11256 && !EQ (window, minibuf_window)
11257 /* When splitting windows or for new windows, it happens that
11258 redisplay is called with a nil window_end_vpos or one being
11259 larger than the window. This should really be fixed in
11260 window.c. I don't have this on my list, now, so we do
11261 approximately the same as the old redisplay code. --gerd. */
11262 && INTEGERP (w->window_end_vpos)
11263 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11264 && (FRAME_WINDOW_P (f)
11265 || !overlay_arrow_in_current_buffer_p ()))
11266 {
11267 int this_scroll_margin;
11268 struct glyph_row *row = NULL;
11269
11270 #if GLYPH_DEBUG
11271 debug_method_add (w, "cursor movement");
11272 #endif
11273
11274 /* Scroll if point within this distance from the top or bottom
11275 of the window. This is a pixel value. */
11276 this_scroll_margin = max (0, scroll_margin);
11277 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11278 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11279
11280 /* Start with the row the cursor was displayed during the last
11281 not paused redisplay. Give up if that row is not valid. */
11282 if (w->last_cursor.vpos < 0
11283 || w->last_cursor.vpos >= w->current_matrix->nrows)
11284 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11285 else
11286 {
11287 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11288 if (row->mode_line_p)
11289 ++row;
11290 if (!row->enabled_p)
11291 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11292 }
11293
11294 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11295 {
11296 int scroll_p = 0;
11297 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11298
11299 if (PT > XFASTINT (w->last_point))
11300 {
11301 /* Point has moved forward. */
11302 while (MATRIX_ROW_END_CHARPOS (row) < PT
11303 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11304 {
11305 xassert (row->enabled_p);
11306 ++row;
11307 }
11308
11309 /* The end position of a row equals the start position
11310 of the next row. If PT is there, we would rather
11311 display it in the next line. */
11312 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11313 && MATRIX_ROW_END_CHARPOS (row) == PT
11314 && !cursor_row_p (w, row))
11315 ++row;
11316
11317 /* If within the scroll margin, scroll. Note that
11318 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11319 the next line would be drawn, and that
11320 this_scroll_margin can be zero. */
11321 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11322 || PT > MATRIX_ROW_END_CHARPOS (row)
11323 /* Line is completely visible last line in window
11324 and PT is to be set in the next line. */
11325 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11326 && PT == MATRIX_ROW_END_CHARPOS (row)
11327 && !row->ends_at_zv_p
11328 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11329 scroll_p = 1;
11330 }
11331 else if (PT < XFASTINT (w->last_point))
11332 {
11333 /* Cursor has to be moved backward. Note that PT >=
11334 CHARPOS (startp) because of the outer
11335 if-statement. */
11336 while (!row->mode_line_p
11337 && (MATRIX_ROW_START_CHARPOS (row) > PT
11338 || (MATRIX_ROW_START_CHARPOS (row) == PT
11339 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11340 && (row->y > this_scroll_margin
11341 || CHARPOS (startp) == BEGV))
11342 {
11343 xassert (row->enabled_p);
11344 --row;
11345 }
11346
11347 /* Consider the following case: Window starts at BEGV,
11348 there is invisible, intangible text at BEGV, so that
11349 display starts at some point START > BEGV. It can
11350 happen that we are called with PT somewhere between
11351 BEGV and START. Try to handle that case. */
11352 if (row < w->current_matrix->rows
11353 || row->mode_line_p)
11354 {
11355 row = w->current_matrix->rows;
11356 if (row->mode_line_p)
11357 ++row;
11358 }
11359
11360 /* Due to newlines in overlay strings, we may have to
11361 skip forward over overlay strings. */
11362 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11363 && MATRIX_ROW_END_CHARPOS (row) == PT
11364 && !cursor_row_p (w, row))
11365 ++row;
11366
11367 /* If within the scroll margin, scroll. */
11368 if (row->y < this_scroll_margin
11369 && CHARPOS (startp) != BEGV)
11370 scroll_p = 1;
11371 }
11372
11373 if (PT < MATRIX_ROW_START_CHARPOS (row)
11374 || PT > MATRIX_ROW_END_CHARPOS (row))
11375 {
11376 /* if PT is not in the glyph row, give up. */
11377 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11378 }
11379 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11380 {
11381 if (PT == MATRIX_ROW_END_CHARPOS (row)
11382 && !row->ends_at_zv_p
11383 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11384 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11385 else if (row->height > window_box_height (w))
11386 {
11387 /* If we end up in a partially visible line, let's
11388 make it fully visible, except when it's taller
11389 than the window, in which case we can't do much
11390 about it. */
11391 *scroll_step = 1;
11392 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11393 }
11394 else
11395 {
11396 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11397 if (!make_cursor_line_fully_visible (w, 0))
11398 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11399 else
11400 rc = CURSOR_MOVEMENT_SUCCESS;
11401 }
11402 }
11403 else if (scroll_p)
11404 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11405 else
11406 {
11407 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11408 rc = CURSOR_MOVEMENT_SUCCESS;
11409 }
11410 }
11411 }
11412
11413 return rc;
11414 }
11415
11416 void
11417 set_vertical_scroll_bar (w)
11418 struct window *w;
11419 {
11420 int start, end, whole;
11421
11422 /* Calculate the start and end positions for the current window.
11423 At some point, it would be nice to choose between scrollbars
11424 which reflect the whole buffer size, with special markers
11425 indicating narrowing, and scrollbars which reflect only the
11426 visible region.
11427
11428 Note that mini-buffers sometimes aren't displaying any text. */
11429 if (!MINI_WINDOW_P (w)
11430 || (w == XWINDOW (minibuf_window)
11431 && NILP (echo_area_buffer[0])))
11432 {
11433 struct buffer *buf = XBUFFER (w->buffer);
11434 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11435 start = marker_position (w->start) - BUF_BEGV (buf);
11436 /* I don't think this is guaranteed to be right. For the
11437 moment, we'll pretend it is. */
11438 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11439
11440 if (end < start)
11441 end = start;
11442 if (whole < (end - start))
11443 whole = end - start;
11444 }
11445 else
11446 start = end = whole = 0;
11447
11448 /* Indicate what this scroll bar ought to be displaying now. */
11449 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11450 }
11451
11452
11453 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11454 selected_window is redisplayed.
11455
11456 We can return without actually redisplaying the window if
11457 fonts_changed_p is nonzero. In that case, redisplay_internal will
11458 retry. */
11459
11460 static void
11461 redisplay_window (window, just_this_one_p)
11462 Lisp_Object window;
11463 int just_this_one_p;
11464 {
11465 struct window *w = XWINDOW (window);
11466 struct frame *f = XFRAME (w->frame);
11467 struct buffer *buffer = XBUFFER (w->buffer);
11468 struct buffer *old = current_buffer;
11469 struct text_pos lpoint, opoint, startp;
11470 int update_mode_line;
11471 int tem;
11472 struct it it;
11473 /* Record it now because it's overwritten. */
11474 int current_matrix_up_to_date_p = 0;
11475 int used_current_matrix_p = 0;
11476 /* This is less strict than current_matrix_up_to_date_p.
11477 It indictes that the buffer contents and narrowing are unchanged. */
11478 int buffer_unchanged_p = 0;
11479 int temp_scroll_step = 0;
11480 int count = SPECPDL_INDEX ();
11481 int rc;
11482 int centering_position;
11483 int last_line_misfit = 0;
11484
11485 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11486 opoint = lpoint;
11487
11488 /* W must be a leaf window here. */
11489 xassert (!NILP (w->buffer));
11490 #if GLYPH_DEBUG
11491 *w->desired_matrix->method = 0;
11492 #endif
11493
11494 specbind (Qinhibit_point_motion_hooks, Qt);
11495
11496 reconsider_clip_changes (w, buffer);
11497
11498 /* Has the mode line to be updated? */
11499 update_mode_line = (!NILP (w->update_mode_line)
11500 || update_mode_lines
11501 || buffer->clip_changed
11502 || buffer->prevent_redisplay_optimizations_p);
11503
11504 if (MINI_WINDOW_P (w))
11505 {
11506 if (w == XWINDOW (echo_area_window)
11507 && !NILP (echo_area_buffer[0]))
11508 {
11509 if (update_mode_line)
11510 /* We may have to update a tty frame's menu bar or a
11511 tool-bar. Example `M-x C-h C-h C-g'. */
11512 goto finish_menu_bars;
11513 else
11514 /* We've already displayed the echo area glyphs in this window. */
11515 goto finish_scroll_bars;
11516 }
11517 else if ((w != XWINDOW (minibuf_window)
11518 || minibuf_level == 0)
11519 /* When buffer is nonempty, redisplay window normally. */
11520 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11521 /* Quail displays non-mini buffers in minibuffer window.
11522 In that case, redisplay the window normally. */
11523 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11524 {
11525 /* W is a mini-buffer window, but it's not active, so clear
11526 it. */
11527 int yb = window_text_bottom_y (w);
11528 struct glyph_row *row;
11529 int y;
11530
11531 for (y = 0, row = w->desired_matrix->rows;
11532 y < yb;
11533 y += row->height, ++row)
11534 blank_row (w, row, y);
11535 goto finish_scroll_bars;
11536 }
11537
11538 clear_glyph_matrix (w->desired_matrix);
11539 }
11540
11541 /* Otherwise set up data on this window; select its buffer and point
11542 value. */
11543 /* Really select the buffer, for the sake of buffer-local
11544 variables. */
11545 set_buffer_internal_1 (XBUFFER (w->buffer));
11546 SET_TEXT_POS (opoint, PT, PT_BYTE);
11547
11548 current_matrix_up_to_date_p
11549 = (!NILP (w->window_end_valid)
11550 && !current_buffer->clip_changed
11551 && !current_buffer->prevent_redisplay_optimizations_p
11552 && XFASTINT (w->last_modified) >= MODIFF
11553 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11554
11555 buffer_unchanged_p
11556 = (!NILP (w->window_end_valid)
11557 && !current_buffer->clip_changed
11558 && XFASTINT (w->last_modified) >= MODIFF
11559 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11560
11561 /* When windows_or_buffers_changed is non-zero, we can't rely on
11562 the window end being valid, so set it to nil there. */
11563 if (windows_or_buffers_changed)
11564 {
11565 /* If window starts on a continuation line, maybe adjust the
11566 window start in case the window's width changed. */
11567 if (XMARKER (w->start)->buffer == current_buffer)
11568 compute_window_start_on_continuation_line (w);
11569
11570 w->window_end_valid = Qnil;
11571 }
11572
11573 /* Some sanity checks. */
11574 CHECK_WINDOW_END (w);
11575 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11576 abort ();
11577 if (BYTEPOS (opoint) < CHARPOS (opoint))
11578 abort ();
11579
11580 /* If %c is in mode line, update it if needed. */
11581 if (!NILP (w->column_number_displayed)
11582 /* This alternative quickly identifies a common case
11583 where no change is needed. */
11584 && !(PT == XFASTINT (w->last_point)
11585 && XFASTINT (w->last_modified) >= MODIFF
11586 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11587 && (XFASTINT (w->column_number_displayed)
11588 != (int) current_column ())) /* iftc */
11589 update_mode_line = 1;
11590
11591 /* Count number of windows showing the selected buffer. An indirect
11592 buffer counts as its base buffer. */
11593 if (!just_this_one_p)
11594 {
11595 struct buffer *current_base, *window_base;
11596 current_base = current_buffer;
11597 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11598 if (current_base->base_buffer)
11599 current_base = current_base->base_buffer;
11600 if (window_base->base_buffer)
11601 window_base = window_base->base_buffer;
11602 if (current_base == window_base)
11603 buffer_shared++;
11604 }
11605
11606 /* Point refers normally to the selected window. For any other
11607 window, set up appropriate value. */
11608 if (!EQ (window, selected_window))
11609 {
11610 int new_pt = XMARKER (w->pointm)->charpos;
11611 int new_pt_byte = marker_byte_position (w->pointm);
11612 if (new_pt < BEGV)
11613 {
11614 new_pt = BEGV;
11615 new_pt_byte = BEGV_BYTE;
11616 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11617 }
11618 else if (new_pt > (ZV - 1))
11619 {
11620 new_pt = ZV;
11621 new_pt_byte = ZV_BYTE;
11622 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11623 }
11624
11625 /* We don't use SET_PT so that the point-motion hooks don't run. */
11626 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11627 }
11628
11629 /* If any of the character widths specified in the display table
11630 have changed, invalidate the width run cache. It's true that
11631 this may be a bit late to catch such changes, but the rest of
11632 redisplay goes (non-fatally) haywire when the display table is
11633 changed, so why should we worry about doing any better? */
11634 if (current_buffer->width_run_cache)
11635 {
11636 struct Lisp_Char_Table *disptab = buffer_display_table ();
11637
11638 if (! disptab_matches_widthtab (disptab,
11639 XVECTOR (current_buffer->width_table)))
11640 {
11641 invalidate_region_cache (current_buffer,
11642 current_buffer->width_run_cache,
11643 BEG, Z);
11644 recompute_width_table (current_buffer, disptab);
11645 }
11646 }
11647
11648 /* If window-start is screwed up, choose a new one. */
11649 if (XMARKER (w->start)->buffer != current_buffer)
11650 goto recenter;
11651
11652 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11653
11654 /* If someone specified a new starting point but did not insist,
11655 check whether it can be used. */
11656 if (!NILP (w->optional_new_start)
11657 && CHARPOS (startp) >= BEGV
11658 && CHARPOS (startp) <= ZV)
11659 {
11660 w->optional_new_start = Qnil;
11661 start_display (&it, w, startp);
11662 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11663 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11664 if (IT_CHARPOS (it) == PT)
11665 w->force_start = Qt;
11666 /* IT may overshoot PT if text at PT is invisible. */
11667 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11668 w->force_start = Qt;
11669
11670
11671 }
11672
11673 /* Handle case where place to start displaying has been specified,
11674 unless the specified location is outside the accessible range. */
11675 if (!NILP (w->force_start)
11676 || w->frozen_window_start_p)
11677 {
11678 /* We set this later on if we have to adjust point. */
11679 int new_vpos = -1;
11680
11681 w->force_start = Qnil;
11682 w->vscroll = 0;
11683 w->window_end_valid = Qnil;
11684
11685 /* Forget any recorded base line for line number display. */
11686 if (!buffer_unchanged_p)
11687 w->base_line_number = Qnil;
11688
11689 /* Redisplay the mode line. Select the buffer properly for that.
11690 Also, run the hook window-scroll-functions
11691 because we have scrolled. */
11692 /* Note, we do this after clearing force_start because
11693 if there's an error, it is better to forget about force_start
11694 than to get into an infinite loop calling the hook functions
11695 and having them get more errors. */
11696 if (!update_mode_line
11697 || ! NILP (Vwindow_scroll_functions))
11698 {
11699 update_mode_line = 1;
11700 w->update_mode_line = Qt;
11701 startp = run_window_scroll_functions (window, startp);
11702 }
11703
11704 w->last_modified = make_number (0);
11705 w->last_overlay_modified = make_number (0);
11706 if (CHARPOS (startp) < BEGV)
11707 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11708 else if (CHARPOS (startp) > ZV)
11709 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11710
11711 /* Redisplay, then check if cursor has been set during the
11712 redisplay. Give up if new fonts were loaded. */
11713 if (!try_window (window, startp))
11714 {
11715 w->force_start = Qt;
11716 clear_glyph_matrix (w->desired_matrix);
11717 goto need_larger_matrices;
11718 }
11719
11720 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11721 {
11722 /* If point does not appear, try to move point so it does
11723 appear. The desired matrix has been built above, so we
11724 can use it here. */
11725 new_vpos = window_box_height (w) / 2;
11726 }
11727
11728 if (!make_cursor_line_fully_visible (w, 0))
11729 {
11730 /* Point does appear, but on a line partly visible at end of window.
11731 Move it back to a fully-visible line. */
11732 new_vpos = window_box_height (w);
11733 }
11734
11735 /* If we need to move point for either of the above reasons,
11736 now actually do it. */
11737 if (new_vpos >= 0)
11738 {
11739 struct glyph_row *row;
11740
11741 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11742 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11743 ++row;
11744
11745 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11746 MATRIX_ROW_START_BYTEPOS (row));
11747
11748 if (w != XWINDOW (selected_window))
11749 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11750 else if (current_buffer == old)
11751 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11752
11753 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11754
11755 /* If we are highlighting the region, then we just changed
11756 the region, so redisplay to show it. */
11757 if (!NILP (Vtransient_mark_mode)
11758 && !NILP (current_buffer->mark_active))
11759 {
11760 clear_glyph_matrix (w->desired_matrix);
11761 if (!try_window (window, startp))
11762 goto need_larger_matrices;
11763 }
11764 }
11765
11766 #if GLYPH_DEBUG
11767 debug_method_add (w, "forced window start");
11768 #endif
11769 goto done;
11770 }
11771
11772 /* Handle case where text has not changed, only point, and it has
11773 not moved off the frame, and we are not retrying after hscroll.
11774 (current_matrix_up_to_date_p is nonzero when retrying.) */
11775 if (current_matrix_up_to_date_p
11776 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11777 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11778 {
11779 switch (rc)
11780 {
11781 case CURSOR_MOVEMENT_SUCCESS:
11782 used_current_matrix_p = 1;
11783 goto done;
11784
11785 #if 0 /* try_cursor_movement never returns this value. */
11786 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11787 goto need_larger_matrices;
11788 #endif
11789
11790 case CURSOR_MOVEMENT_MUST_SCROLL:
11791 goto try_to_scroll;
11792
11793 default:
11794 abort ();
11795 }
11796 }
11797 /* If current starting point was originally the beginning of a line
11798 but no longer is, find a new starting point. */
11799 else if (!NILP (w->start_at_line_beg)
11800 && !(CHARPOS (startp) <= BEGV
11801 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11802 {
11803 #if GLYPH_DEBUG
11804 debug_method_add (w, "recenter 1");
11805 #endif
11806 goto recenter;
11807 }
11808
11809 /* Try scrolling with try_window_id. Value is > 0 if update has
11810 been done, it is -1 if we know that the same window start will
11811 not work. It is 0 if unsuccessful for some other reason. */
11812 else if ((tem = try_window_id (w)) != 0)
11813 {
11814 #if GLYPH_DEBUG
11815 debug_method_add (w, "try_window_id %d", tem);
11816 #endif
11817
11818 if (fonts_changed_p)
11819 goto need_larger_matrices;
11820 if (tem > 0)
11821 goto done;
11822
11823 /* Otherwise try_window_id has returned -1 which means that we
11824 don't want the alternative below this comment to execute. */
11825 }
11826 else if (CHARPOS (startp) >= BEGV
11827 && CHARPOS (startp) <= ZV
11828 && PT >= CHARPOS (startp)
11829 && (CHARPOS (startp) < ZV
11830 /* Avoid starting at end of buffer. */
11831 || CHARPOS (startp) == BEGV
11832 || (XFASTINT (w->last_modified) >= MODIFF
11833 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11834 {
11835 #if GLYPH_DEBUG
11836 debug_method_add (w, "same window start");
11837 #endif
11838
11839 /* Try to redisplay starting at same place as before.
11840 If point has not moved off frame, accept the results. */
11841 if (!current_matrix_up_to_date_p
11842 /* Don't use try_window_reusing_current_matrix in this case
11843 because a window scroll function can have changed the
11844 buffer. */
11845 || !NILP (Vwindow_scroll_functions)
11846 || MINI_WINDOW_P (w)
11847 || !(used_current_matrix_p =
11848 try_window_reusing_current_matrix (w)))
11849 {
11850 IF_DEBUG (debug_method_add (w, "1"));
11851 try_window (window, startp);
11852 }
11853
11854 if (fonts_changed_p)
11855 goto need_larger_matrices;
11856
11857 if (w->cursor.vpos >= 0)
11858 {
11859 if (!just_this_one_p
11860 || current_buffer->clip_changed
11861 || BEG_UNCHANGED < CHARPOS (startp))
11862 /* Forget any recorded base line for line number display. */
11863 w->base_line_number = Qnil;
11864
11865 if (!make_cursor_line_fully_visible (w, 1))
11866 {
11867 clear_glyph_matrix (w->desired_matrix);
11868 last_line_misfit = 1;
11869 }
11870 /* Drop through and scroll. */
11871 else
11872 goto done;
11873 }
11874 else
11875 clear_glyph_matrix (w->desired_matrix);
11876 }
11877
11878 try_to_scroll:
11879
11880 w->last_modified = make_number (0);
11881 w->last_overlay_modified = make_number (0);
11882
11883 /* Redisplay the mode line. Select the buffer properly for that. */
11884 if (!update_mode_line)
11885 {
11886 update_mode_line = 1;
11887 w->update_mode_line = Qt;
11888 }
11889
11890 /* Try to scroll by specified few lines. */
11891 if ((scroll_conservatively
11892 || scroll_step
11893 || temp_scroll_step
11894 || NUMBERP (current_buffer->scroll_up_aggressively)
11895 || NUMBERP (current_buffer->scroll_down_aggressively))
11896 && !current_buffer->clip_changed
11897 && CHARPOS (startp) >= BEGV
11898 && CHARPOS (startp) <= ZV)
11899 {
11900 /* The function returns -1 if new fonts were loaded, 1 if
11901 successful, 0 if not successful. */
11902 int rc = try_scrolling (window, just_this_one_p,
11903 scroll_conservatively,
11904 scroll_step,
11905 temp_scroll_step, last_line_misfit);
11906 switch (rc)
11907 {
11908 case SCROLLING_SUCCESS:
11909 goto done;
11910
11911 case SCROLLING_NEED_LARGER_MATRICES:
11912 goto need_larger_matrices;
11913
11914 case SCROLLING_FAILED:
11915 break;
11916
11917 default:
11918 abort ();
11919 }
11920 }
11921
11922 /* Finally, just choose place to start which centers point */
11923
11924 recenter:
11925 centering_position = window_box_height (w) / 2;
11926
11927 point_at_top:
11928 /* Jump here with centering_position already set to 0. */
11929
11930 #if GLYPH_DEBUG
11931 debug_method_add (w, "recenter");
11932 #endif
11933
11934 /* w->vscroll = 0; */
11935
11936 /* Forget any previously recorded base line for line number display. */
11937 if (!buffer_unchanged_p)
11938 w->base_line_number = Qnil;
11939
11940 /* Move backward half the height of the window. */
11941 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11942 it.current_y = it.last_visible_y;
11943 move_it_vertically_backward (&it, centering_position);
11944 xassert (IT_CHARPOS (it) >= BEGV);
11945
11946 /* The function move_it_vertically_backward may move over more
11947 than the specified y-distance. If it->w is small, e.g. a
11948 mini-buffer window, we may end up in front of the window's
11949 display area. Start displaying at the start of the line
11950 containing PT in this case. */
11951 if (it.current_y <= 0)
11952 {
11953 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11954 move_it_vertically (&it, 0);
11955 xassert (IT_CHARPOS (it) <= PT);
11956 it.current_y = 0;
11957 }
11958
11959 it.current_x = it.hpos = 0;
11960
11961 /* Set startp here explicitly in case that helps avoid an infinite loop
11962 in case the window-scroll-functions functions get errors. */
11963 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11964
11965 /* Run scroll hooks. */
11966 startp = run_window_scroll_functions (window, it.current.pos);
11967
11968 /* Redisplay the window. */
11969 if (!current_matrix_up_to_date_p
11970 || windows_or_buffers_changed
11971 || cursor_type_changed
11972 /* Don't use try_window_reusing_current_matrix in this case
11973 because it can have changed the buffer. */
11974 || !NILP (Vwindow_scroll_functions)
11975 || !just_this_one_p
11976 || MINI_WINDOW_P (w)
11977 || !(used_current_matrix_p =
11978 try_window_reusing_current_matrix (w)))
11979 try_window (window, startp);
11980
11981 /* If new fonts have been loaded (due to fontsets), give up. We
11982 have to start a new redisplay since we need to re-adjust glyph
11983 matrices. */
11984 if (fonts_changed_p)
11985 goto need_larger_matrices;
11986
11987 /* If cursor did not appear assume that the middle of the window is
11988 in the first line of the window. Do it again with the next line.
11989 (Imagine a window of height 100, displaying two lines of height
11990 60. Moving back 50 from it->last_visible_y will end in the first
11991 line.) */
11992 if (w->cursor.vpos < 0)
11993 {
11994 if (!NILP (w->window_end_valid)
11995 && PT >= Z - XFASTINT (w->window_end_pos))
11996 {
11997 clear_glyph_matrix (w->desired_matrix);
11998 move_it_by_lines (&it, 1, 0);
11999 try_window (window, it.current.pos);
12000 }
12001 else if (PT < IT_CHARPOS (it))
12002 {
12003 clear_glyph_matrix (w->desired_matrix);
12004 move_it_by_lines (&it, -1, 0);
12005 try_window (window, it.current.pos);
12006 }
12007 else
12008 {
12009 /* Not much we can do about it. */
12010 }
12011 }
12012
12013 /* Consider the following case: Window starts at BEGV, there is
12014 invisible, intangible text at BEGV, so that display starts at
12015 some point START > BEGV. It can happen that we are called with
12016 PT somewhere between BEGV and START. Try to handle that case. */
12017 if (w->cursor.vpos < 0)
12018 {
12019 struct glyph_row *row = w->current_matrix->rows;
12020 if (row->mode_line_p)
12021 ++row;
12022 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12023 }
12024
12025 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12026 {
12027 /* If vscroll is enabled, disable it and try again. */
12028 if (w->vscroll)
12029 {
12030 w->vscroll = 0;
12031 clear_glyph_matrix (w->desired_matrix);
12032 goto recenter;
12033 }
12034
12035 /* If centering point failed to make the whole line visible,
12036 put point at the top instead. That has to make the whole line
12037 visible, if it can be done. */
12038 clear_glyph_matrix (w->desired_matrix);
12039 centering_position = 0;
12040 goto point_at_top;
12041 }
12042
12043 done:
12044
12045 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12046 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12047 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12048 ? Qt : Qnil);
12049
12050 /* Display the mode line, if we must. */
12051 if ((update_mode_line
12052 /* If window not full width, must redo its mode line
12053 if (a) the window to its side is being redone and
12054 (b) we do a frame-based redisplay. This is a consequence
12055 of how inverted lines are drawn in frame-based redisplay. */
12056 || (!just_this_one_p
12057 && !FRAME_WINDOW_P (f)
12058 && !WINDOW_FULL_WIDTH_P (w))
12059 /* Line number to display. */
12060 || INTEGERP (w->base_line_pos)
12061 /* Column number is displayed and different from the one displayed. */
12062 || (!NILP (w->column_number_displayed)
12063 && (XFASTINT (w->column_number_displayed)
12064 != (int) current_column ()))) /* iftc */
12065 /* This means that the window has a mode line. */
12066 && (WINDOW_WANTS_MODELINE_P (w)
12067 || WINDOW_WANTS_HEADER_LINE_P (w)))
12068 {
12069 display_mode_lines (w);
12070
12071 /* If mode line height has changed, arrange for a thorough
12072 immediate redisplay using the correct mode line height. */
12073 if (WINDOW_WANTS_MODELINE_P (w)
12074 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12075 {
12076 fonts_changed_p = 1;
12077 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12078 = DESIRED_MODE_LINE_HEIGHT (w);
12079 }
12080
12081 /* If top line height has changed, arrange for a thorough
12082 immediate redisplay using the correct mode line height. */
12083 if (WINDOW_WANTS_HEADER_LINE_P (w)
12084 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12085 {
12086 fonts_changed_p = 1;
12087 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12088 = DESIRED_HEADER_LINE_HEIGHT (w);
12089 }
12090
12091 if (fonts_changed_p)
12092 goto need_larger_matrices;
12093 }
12094
12095 if (!line_number_displayed
12096 && !BUFFERP (w->base_line_pos))
12097 {
12098 w->base_line_pos = Qnil;
12099 w->base_line_number = Qnil;
12100 }
12101
12102 finish_menu_bars:
12103
12104 /* When we reach a frame's selected window, redo the frame's menu bar. */
12105 if (update_mode_line
12106 && EQ (FRAME_SELECTED_WINDOW (f), window))
12107 {
12108 int redisplay_menu_p = 0;
12109 int redisplay_tool_bar_p = 0;
12110
12111 if (FRAME_WINDOW_P (f))
12112 {
12113 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12114 || defined (USE_GTK)
12115 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12116 #else
12117 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12118 #endif
12119 }
12120 else
12121 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12122
12123 if (redisplay_menu_p)
12124 display_menu_bar (w);
12125
12126 #ifdef HAVE_WINDOW_SYSTEM
12127 #ifdef USE_GTK
12128 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12129 #else
12130 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12131 && (FRAME_TOOL_BAR_LINES (f) > 0
12132 || auto_resize_tool_bars_p);
12133
12134 #endif
12135
12136 if (redisplay_tool_bar_p)
12137 redisplay_tool_bar (f);
12138 #endif
12139 }
12140
12141 #ifdef HAVE_WINDOW_SYSTEM
12142 if (update_window_fringes (w, 0)
12143 && !just_this_one_p
12144 && (used_current_matrix_p || overlay_arrow_seen)
12145 && !w->pseudo_window_p)
12146 {
12147 update_begin (f);
12148 BLOCK_INPUT;
12149 draw_window_fringes (w);
12150 UNBLOCK_INPUT;
12151 update_end (f);
12152 }
12153 #endif /* HAVE_WINDOW_SYSTEM */
12154
12155 /* We go to this label, with fonts_changed_p nonzero,
12156 if it is necessary to try again using larger glyph matrices.
12157 We have to redeem the scroll bar even in this case,
12158 because the loop in redisplay_internal expects that. */
12159 need_larger_matrices:
12160 ;
12161 finish_scroll_bars:
12162
12163 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12164 {
12165 /* Set the thumb's position and size. */
12166 set_vertical_scroll_bar (w);
12167
12168 /* Note that we actually used the scroll bar attached to this
12169 window, so it shouldn't be deleted at the end of redisplay. */
12170 redeem_scroll_bar_hook (w);
12171 }
12172
12173 /* Restore current_buffer and value of point in it. */
12174 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12175 set_buffer_internal_1 (old);
12176 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12177
12178 unbind_to (count, Qnil);
12179 }
12180
12181
12182 /* Build the complete desired matrix of WINDOW with a window start
12183 buffer position POS. Value is non-zero if successful. It is zero
12184 if fonts were loaded during redisplay which makes re-adjusting
12185 glyph matrices necessary. */
12186
12187 int
12188 try_window (window, pos)
12189 Lisp_Object window;
12190 struct text_pos pos;
12191 {
12192 struct window *w = XWINDOW (window);
12193 struct it it;
12194 struct glyph_row *last_text_row = NULL;
12195
12196 /* Make POS the new window start. */
12197 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12198
12199 /* Mark cursor position as unknown. No overlay arrow seen. */
12200 w->cursor.vpos = -1;
12201 overlay_arrow_seen = 0;
12202
12203 /* Initialize iterator and info to start at POS. */
12204 start_display (&it, w, pos);
12205
12206 /* Display all lines of W. */
12207 while (it.current_y < it.last_visible_y)
12208 {
12209 if (display_line (&it))
12210 last_text_row = it.glyph_row - 1;
12211 if (fonts_changed_p)
12212 return 0;
12213 }
12214
12215 /* If bottom moved off end of frame, change mode line percentage. */
12216 if (XFASTINT (w->window_end_pos) <= 0
12217 && Z != IT_CHARPOS (it))
12218 w->update_mode_line = Qt;
12219
12220 /* Set window_end_pos to the offset of the last character displayed
12221 on the window from the end of current_buffer. Set
12222 window_end_vpos to its row number. */
12223 if (last_text_row)
12224 {
12225 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12226 w->window_end_bytepos
12227 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12228 w->window_end_pos
12229 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12230 w->window_end_vpos
12231 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12232 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12233 ->displays_text_p);
12234 }
12235 else
12236 {
12237 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12238 w->window_end_pos = make_number (Z - ZV);
12239 w->window_end_vpos = make_number (0);
12240 }
12241
12242 /* But that is not valid info until redisplay finishes. */
12243 w->window_end_valid = Qnil;
12244 return 1;
12245 }
12246
12247
12248 \f
12249 /************************************************************************
12250 Window redisplay reusing current matrix when buffer has not changed
12251 ************************************************************************/
12252
12253 /* Try redisplay of window W showing an unchanged buffer with a
12254 different window start than the last time it was displayed by
12255 reusing its current matrix. Value is non-zero if successful.
12256 W->start is the new window start. */
12257
12258 static int
12259 try_window_reusing_current_matrix (w)
12260 struct window *w;
12261 {
12262 struct frame *f = XFRAME (w->frame);
12263 struct glyph_row *row, *bottom_row;
12264 struct it it;
12265 struct run run;
12266 struct text_pos start, new_start;
12267 int nrows_scrolled, i;
12268 struct glyph_row *last_text_row;
12269 struct glyph_row *last_reused_text_row;
12270 struct glyph_row *start_row;
12271 int start_vpos, min_y, max_y;
12272
12273 #if GLYPH_DEBUG
12274 if (inhibit_try_window_reusing)
12275 return 0;
12276 #endif
12277
12278 if (/* This function doesn't handle terminal frames. */
12279 !FRAME_WINDOW_P (f)
12280 /* Don't try to reuse the display if windows have been split
12281 or such. */
12282 || windows_or_buffers_changed
12283 || cursor_type_changed)
12284 return 0;
12285
12286 /* Can't do this if region may have changed. */
12287 if ((!NILP (Vtransient_mark_mode)
12288 && !NILP (current_buffer->mark_active))
12289 || !NILP (w->region_showing)
12290 || !NILP (Vshow_trailing_whitespace))
12291 return 0;
12292
12293 /* If top-line visibility has changed, give up. */
12294 if (WINDOW_WANTS_HEADER_LINE_P (w)
12295 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12296 return 0;
12297
12298 /* Give up if old or new display is scrolled vertically. We could
12299 make this function handle this, but right now it doesn't. */
12300 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12301 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12302 return 0;
12303
12304 /* The variable new_start now holds the new window start. The old
12305 start `start' can be determined from the current matrix. */
12306 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12307 start = start_row->start.pos;
12308 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12309
12310 /* Clear the desired matrix for the display below. */
12311 clear_glyph_matrix (w->desired_matrix);
12312
12313 if (CHARPOS (new_start) <= CHARPOS (start))
12314 {
12315 int first_row_y;
12316
12317 /* Don't use this method if the display starts with an ellipsis
12318 displayed for invisible text. It's not easy to handle that case
12319 below, and it's certainly not worth the effort since this is
12320 not a frequent case. */
12321 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12322 return 0;
12323
12324 IF_DEBUG (debug_method_add (w, "twu1"));
12325
12326 /* Display up to a row that can be reused. The variable
12327 last_text_row is set to the last row displayed that displays
12328 text. Note that it.vpos == 0 if or if not there is a
12329 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12330 start_display (&it, w, new_start);
12331 first_row_y = it.current_y;
12332 w->cursor.vpos = -1;
12333 last_text_row = last_reused_text_row = NULL;
12334
12335 while (it.current_y < it.last_visible_y
12336 && IT_CHARPOS (it) < CHARPOS (start)
12337 && !fonts_changed_p)
12338 if (display_line (&it))
12339 last_text_row = it.glyph_row - 1;
12340
12341 /* A value of current_y < last_visible_y means that we stopped
12342 at the previous window start, which in turn means that we
12343 have at least one reusable row. */
12344 if (it.current_y < it.last_visible_y)
12345 {
12346 /* IT.vpos always starts from 0; it counts text lines. */
12347 nrows_scrolled = it.vpos;
12348
12349 /* Find PT if not already found in the lines displayed. */
12350 if (w->cursor.vpos < 0)
12351 {
12352 int dy = it.current_y - first_row_y;
12353
12354 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12355 row = row_containing_pos (w, PT, row, NULL, dy);
12356 if (row)
12357 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12358 dy, nrows_scrolled);
12359 else
12360 {
12361 clear_glyph_matrix (w->desired_matrix);
12362 return 0;
12363 }
12364 }
12365
12366 /* Scroll the display. Do it before the current matrix is
12367 changed. The problem here is that update has not yet
12368 run, i.e. part of the current matrix is not up to date.
12369 scroll_run_hook will clear the cursor, and use the
12370 current matrix to get the height of the row the cursor is
12371 in. */
12372 run.current_y = first_row_y;
12373 run.desired_y = it.current_y;
12374 run.height = it.last_visible_y - it.current_y;
12375
12376 if (run.height > 0 && run.current_y != run.desired_y)
12377 {
12378 update_begin (f);
12379 rif->update_window_begin_hook (w);
12380 rif->clear_window_mouse_face (w);
12381 rif->scroll_run_hook (w, &run);
12382 rif->update_window_end_hook (w, 0, 0);
12383 update_end (f);
12384 }
12385
12386 /* Shift current matrix down by nrows_scrolled lines. */
12387 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12388 rotate_matrix (w->current_matrix,
12389 start_vpos,
12390 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12391 nrows_scrolled);
12392
12393 /* Disable lines that must be updated. */
12394 for (i = 0; i < it.vpos; ++i)
12395 (start_row + i)->enabled_p = 0;
12396
12397 /* Re-compute Y positions. */
12398 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12399 max_y = it.last_visible_y;
12400 for (row = start_row + nrows_scrolled;
12401 row < bottom_row;
12402 ++row)
12403 {
12404 row->y = it.current_y;
12405 row->visible_height = row->height;
12406
12407 if (row->y < min_y)
12408 row->visible_height -= min_y - row->y;
12409 if (row->y + row->height > max_y)
12410 row->visible_height -= row->y + row->height - max_y;
12411 row->redraw_fringe_bitmaps_p = 1;
12412
12413 it.current_y += row->height;
12414
12415 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12416 last_reused_text_row = row;
12417 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12418 break;
12419 }
12420
12421 /* Disable lines in the current matrix which are now
12422 below the window. */
12423 for (++row; row < bottom_row; ++row)
12424 row->enabled_p = 0;
12425 }
12426
12427 /* Update window_end_pos etc.; last_reused_text_row is the last
12428 reused row from the current matrix containing text, if any.
12429 The value of last_text_row is the last displayed line
12430 containing text. */
12431 if (last_reused_text_row)
12432 {
12433 w->window_end_bytepos
12434 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12435 w->window_end_pos
12436 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12437 w->window_end_vpos
12438 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12439 w->current_matrix));
12440 }
12441 else if (last_text_row)
12442 {
12443 w->window_end_bytepos
12444 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12445 w->window_end_pos
12446 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12447 w->window_end_vpos
12448 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12449 }
12450 else
12451 {
12452 /* This window must be completely empty. */
12453 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12454 w->window_end_pos = make_number (Z - ZV);
12455 w->window_end_vpos = make_number (0);
12456 }
12457 w->window_end_valid = Qnil;
12458
12459 /* Update hint: don't try scrolling again in update_window. */
12460 w->desired_matrix->no_scrolling_p = 1;
12461
12462 #if GLYPH_DEBUG
12463 debug_method_add (w, "try_window_reusing_current_matrix 1");
12464 #endif
12465 return 1;
12466 }
12467 else if (CHARPOS (new_start) > CHARPOS (start))
12468 {
12469 struct glyph_row *pt_row, *row;
12470 struct glyph_row *first_reusable_row;
12471 struct glyph_row *first_row_to_display;
12472 int dy;
12473 int yb = window_text_bottom_y (w);
12474
12475 /* Find the row starting at new_start, if there is one. Don't
12476 reuse a partially visible line at the end. */
12477 first_reusable_row = start_row;
12478 while (first_reusable_row->enabled_p
12479 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12480 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12481 < CHARPOS (new_start)))
12482 ++first_reusable_row;
12483
12484 /* Give up if there is no row to reuse. */
12485 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12486 || !first_reusable_row->enabled_p
12487 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12488 != CHARPOS (new_start)))
12489 return 0;
12490
12491 /* We can reuse fully visible rows beginning with
12492 first_reusable_row to the end of the window. Set
12493 first_row_to_display to the first row that cannot be reused.
12494 Set pt_row to the row containing point, if there is any. */
12495 pt_row = NULL;
12496 for (first_row_to_display = first_reusable_row;
12497 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12498 ++first_row_to_display)
12499 {
12500 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12501 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12502 pt_row = first_row_to_display;
12503 }
12504
12505 /* Start displaying at the start of first_row_to_display. */
12506 xassert (first_row_to_display->y < yb);
12507 init_to_row_start (&it, w, first_row_to_display);
12508
12509 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12510 - start_vpos);
12511 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12512 - nrows_scrolled);
12513 it.current_y = (first_row_to_display->y - first_reusable_row->y
12514 + WINDOW_HEADER_LINE_HEIGHT (w));
12515
12516 /* Display lines beginning with first_row_to_display in the
12517 desired matrix. Set last_text_row to the last row displayed
12518 that displays text. */
12519 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12520 if (pt_row == NULL)
12521 w->cursor.vpos = -1;
12522 last_text_row = NULL;
12523 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12524 if (display_line (&it))
12525 last_text_row = it.glyph_row - 1;
12526
12527 /* Give up If point isn't in a row displayed or reused. */
12528 if (w->cursor.vpos < 0)
12529 {
12530 clear_glyph_matrix (w->desired_matrix);
12531 return 0;
12532 }
12533
12534 /* If point is in a reused row, adjust y and vpos of the cursor
12535 position. */
12536 if (pt_row)
12537 {
12538 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12539 w->current_matrix);
12540 w->cursor.y -= first_reusable_row->y;
12541 }
12542
12543 /* Scroll the display. */
12544 run.current_y = first_reusable_row->y;
12545 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12546 run.height = it.last_visible_y - run.current_y;
12547 dy = run.current_y - run.desired_y;
12548
12549 if (run.height)
12550 {
12551 update_begin (f);
12552 rif->update_window_begin_hook (w);
12553 rif->clear_window_mouse_face (w);
12554 rif->scroll_run_hook (w, &run);
12555 rif->update_window_end_hook (w, 0, 0);
12556 update_end (f);
12557 }
12558
12559 /* Adjust Y positions of reused rows. */
12560 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12561 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12562 max_y = it.last_visible_y;
12563 for (row = first_reusable_row; row < first_row_to_display; ++row)
12564 {
12565 row->y -= dy;
12566 row->visible_height = row->height;
12567 if (row->y < min_y)
12568 row->visible_height -= min_y - row->y;
12569 if (row->y + row->height > max_y)
12570 row->visible_height -= row->y + row->height - max_y;
12571 row->redraw_fringe_bitmaps_p = 1;
12572 }
12573
12574 /* Scroll the current matrix. */
12575 xassert (nrows_scrolled > 0);
12576 rotate_matrix (w->current_matrix,
12577 start_vpos,
12578 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12579 -nrows_scrolled);
12580
12581 /* Disable rows not reused. */
12582 for (row -= nrows_scrolled; row < bottom_row; ++row)
12583 row->enabled_p = 0;
12584
12585 /* Adjust window end. A null value of last_text_row means that
12586 the window end is in reused rows which in turn means that
12587 only its vpos can have changed. */
12588 if (last_text_row)
12589 {
12590 w->window_end_bytepos
12591 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12592 w->window_end_pos
12593 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12594 w->window_end_vpos
12595 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12596 }
12597 else
12598 {
12599 w->window_end_vpos
12600 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12601 }
12602
12603 w->window_end_valid = Qnil;
12604 w->desired_matrix->no_scrolling_p = 1;
12605
12606 #if GLYPH_DEBUG
12607 debug_method_add (w, "try_window_reusing_current_matrix 2");
12608 #endif
12609 return 1;
12610 }
12611
12612 return 0;
12613 }
12614
12615
12616 \f
12617 /************************************************************************
12618 Window redisplay reusing current matrix when buffer has changed
12619 ************************************************************************/
12620
12621 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12622 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12623 int *, int *));
12624 static struct glyph_row *
12625 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12626 struct glyph_row *));
12627
12628
12629 /* Return the last row in MATRIX displaying text. If row START is
12630 non-null, start searching with that row. IT gives the dimensions
12631 of the display. Value is null if matrix is empty; otherwise it is
12632 a pointer to the row found. */
12633
12634 static struct glyph_row *
12635 find_last_row_displaying_text (matrix, it, start)
12636 struct glyph_matrix *matrix;
12637 struct it *it;
12638 struct glyph_row *start;
12639 {
12640 struct glyph_row *row, *row_found;
12641
12642 /* Set row_found to the last row in IT->w's current matrix
12643 displaying text. The loop looks funny but think of partially
12644 visible lines. */
12645 row_found = NULL;
12646 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12647 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12648 {
12649 xassert (row->enabled_p);
12650 row_found = row;
12651 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12652 break;
12653 ++row;
12654 }
12655
12656 return row_found;
12657 }
12658
12659
12660 /* Return the last row in the current matrix of W that is not affected
12661 by changes at the start of current_buffer that occurred since W's
12662 current matrix was built. Value is null if no such row exists.
12663
12664 BEG_UNCHANGED us the number of characters unchanged at the start of
12665 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12666 first changed character in current_buffer. Characters at positions <
12667 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12668 when the current matrix was built. */
12669
12670 static struct glyph_row *
12671 find_last_unchanged_at_beg_row (w)
12672 struct window *w;
12673 {
12674 int first_changed_pos = BEG + BEG_UNCHANGED;
12675 struct glyph_row *row;
12676 struct glyph_row *row_found = NULL;
12677 int yb = window_text_bottom_y (w);
12678
12679 /* Find the last row displaying unchanged text. */
12680 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12681 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12682 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12683 {
12684 if (/* If row ends before first_changed_pos, it is unchanged,
12685 except in some case. */
12686 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12687 /* When row ends in ZV and we write at ZV it is not
12688 unchanged. */
12689 && !row->ends_at_zv_p
12690 /* When first_changed_pos is the end of a continued line,
12691 row is not unchanged because it may be no longer
12692 continued. */
12693 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12694 && (row->continued_p
12695 || row->exact_window_width_line_p)))
12696 row_found = row;
12697
12698 /* Stop if last visible row. */
12699 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12700 break;
12701
12702 ++row;
12703 }
12704
12705 return row_found;
12706 }
12707
12708
12709 /* Find the first glyph row in the current matrix of W that is not
12710 affected by changes at the end of current_buffer since the
12711 time W's current matrix was built.
12712
12713 Return in *DELTA the number of chars by which buffer positions in
12714 unchanged text at the end of current_buffer must be adjusted.
12715
12716 Return in *DELTA_BYTES the corresponding number of bytes.
12717
12718 Value is null if no such row exists, i.e. all rows are affected by
12719 changes. */
12720
12721 static struct glyph_row *
12722 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12723 struct window *w;
12724 int *delta, *delta_bytes;
12725 {
12726 struct glyph_row *row;
12727 struct glyph_row *row_found = NULL;
12728
12729 *delta = *delta_bytes = 0;
12730
12731 /* Display must not have been paused, otherwise the current matrix
12732 is not up to date. */
12733 if (NILP (w->window_end_valid))
12734 abort ();
12735
12736 /* A value of window_end_pos >= END_UNCHANGED means that the window
12737 end is in the range of changed text. If so, there is no
12738 unchanged row at the end of W's current matrix. */
12739 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12740 return NULL;
12741
12742 /* Set row to the last row in W's current matrix displaying text. */
12743 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12744
12745 /* If matrix is entirely empty, no unchanged row exists. */
12746 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12747 {
12748 /* The value of row is the last glyph row in the matrix having a
12749 meaningful buffer position in it. The end position of row
12750 corresponds to window_end_pos. This allows us to translate
12751 buffer positions in the current matrix to current buffer
12752 positions for characters not in changed text. */
12753 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12754 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12755 int last_unchanged_pos, last_unchanged_pos_old;
12756 struct glyph_row *first_text_row
12757 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12758
12759 *delta = Z - Z_old;
12760 *delta_bytes = Z_BYTE - Z_BYTE_old;
12761
12762 /* Set last_unchanged_pos to the buffer position of the last
12763 character in the buffer that has not been changed. Z is the
12764 index + 1 of the last character in current_buffer, i.e. by
12765 subtracting END_UNCHANGED we get the index of the last
12766 unchanged character, and we have to add BEG to get its buffer
12767 position. */
12768 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12769 last_unchanged_pos_old = last_unchanged_pos - *delta;
12770
12771 /* Search backward from ROW for a row displaying a line that
12772 starts at a minimum position >= last_unchanged_pos_old. */
12773 for (; row > first_text_row; --row)
12774 {
12775 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12776 abort ();
12777
12778 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12779 row_found = row;
12780 }
12781 }
12782
12783 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12784 abort ();
12785
12786 return row_found;
12787 }
12788
12789
12790 /* Make sure that glyph rows in the current matrix of window W
12791 reference the same glyph memory as corresponding rows in the
12792 frame's frame matrix. This function is called after scrolling W's
12793 current matrix on a terminal frame in try_window_id and
12794 try_window_reusing_current_matrix. */
12795
12796 static void
12797 sync_frame_with_window_matrix_rows (w)
12798 struct window *w;
12799 {
12800 struct frame *f = XFRAME (w->frame);
12801 struct glyph_row *window_row, *window_row_end, *frame_row;
12802
12803 /* Preconditions: W must be a leaf window and full-width. Its frame
12804 must have a frame matrix. */
12805 xassert (NILP (w->hchild) && NILP (w->vchild));
12806 xassert (WINDOW_FULL_WIDTH_P (w));
12807 xassert (!FRAME_WINDOW_P (f));
12808
12809 /* If W is a full-width window, glyph pointers in W's current matrix
12810 have, by definition, to be the same as glyph pointers in the
12811 corresponding frame matrix. Note that frame matrices have no
12812 marginal areas (see build_frame_matrix). */
12813 window_row = w->current_matrix->rows;
12814 window_row_end = window_row + w->current_matrix->nrows;
12815 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12816 while (window_row < window_row_end)
12817 {
12818 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12819 struct glyph *end = window_row->glyphs[LAST_AREA];
12820
12821 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12822 frame_row->glyphs[TEXT_AREA] = start;
12823 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12824 frame_row->glyphs[LAST_AREA] = end;
12825
12826 /* Disable frame rows whose corresponding window rows have
12827 been disabled in try_window_id. */
12828 if (!window_row->enabled_p)
12829 frame_row->enabled_p = 0;
12830
12831 ++window_row, ++frame_row;
12832 }
12833 }
12834
12835
12836 /* Find the glyph row in window W containing CHARPOS. Consider all
12837 rows between START and END (not inclusive). END null means search
12838 all rows to the end of the display area of W. Value is the row
12839 containing CHARPOS or null. */
12840
12841 struct glyph_row *
12842 row_containing_pos (w, charpos, start, end, dy)
12843 struct window *w;
12844 int charpos;
12845 struct glyph_row *start, *end;
12846 int dy;
12847 {
12848 struct glyph_row *row = start;
12849 int last_y;
12850
12851 /* If we happen to start on a header-line, skip that. */
12852 if (row->mode_line_p)
12853 ++row;
12854
12855 if ((end && row >= end) || !row->enabled_p)
12856 return NULL;
12857
12858 last_y = window_text_bottom_y (w) - dy;
12859
12860 while (1)
12861 {
12862 /* Give up if we have gone too far. */
12863 if (end && row >= end)
12864 return NULL;
12865 /* This formerly returned if they were equal.
12866 I think that both quantities are of a "last plus one" type;
12867 if so, when they are equal, the row is within the screen. -- rms. */
12868 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12869 return NULL;
12870
12871 /* If it is in this row, return this row. */
12872 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12873 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12874 /* The end position of a row equals the start
12875 position of the next row. If CHARPOS is there, we
12876 would rather display it in the next line, except
12877 when this line ends in ZV. */
12878 && !row->ends_at_zv_p
12879 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12880 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12881 return row;
12882 ++row;
12883 }
12884 }
12885
12886
12887 /* Try to redisplay window W by reusing its existing display. W's
12888 current matrix must be up to date when this function is called,
12889 i.e. window_end_valid must not be nil.
12890
12891 Value is
12892
12893 1 if display has been updated
12894 0 if otherwise unsuccessful
12895 -1 if redisplay with same window start is known not to succeed
12896
12897 The following steps are performed:
12898
12899 1. Find the last row in the current matrix of W that is not
12900 affected by changes at the start of current_buffer. If no such row
12901 is found, give up.
12902
12903 2. Find the first row in W's current matrix that is not affected by
12904 changes at the end of current_buffer. Maybe there is no such row.
12905
12906 3. Display lines beginning with the row + 1 found in step 1 to the
12907 row found in step 2 or, if step 2 didn't find a row, to the end of
12908 the window.
12909
12910 4. If cursor is not known to appear on the window, give up.
12911
12912 5. If display stopped at the row found in step 2, scroll the
12913 display and current matrix as needed.
12914
12915 6. Maybe display some lines at the end of W, if we must. This can
12916 happen under various circumstances, like a partially visible line
12917 becoming fully visible, or because newly displayed lines are displayed
12918 in smaller font sizes.
12919
12920 7. Update W's window end information. */
12921
12922 static int
12923 try_window_id (w)
12924 struct window *w;
12925 {
12926 struct frame *f = XFRAME (w->frame);
12927 struct glyph_matrix *current_matrix = w->current_matrix;
12928 struct glyph_matrix *desired_matrix = w->desired_matrix;
12929 struct glyph_row *last_unchanged_at_beg_row;
12930 struct glyph_row *first_unchanged_at_end_row;
12931 struct glyph_row *row;
12932 struct glyph_row *bottom_row;
12933 int bottom_vpos;
12934 struct it it;
12935 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12936 struct text_pos start_pos;
12937 struct run run;
12938 int first_unchanged_at_end_vpos = 0;
12939 struct glyph_row *last_text_row, *last_text_row_at_end;
12940 struct text_pos start;
12941 int first_changed_charpos, last_changed_charpos;
12942
12943 #if GLYPH_DEBUG
12944 if (inhibit_try_window_id)
12945 return 0;
12946 #endif
12947
12948 /* This is handy for debugging. */
12949 #if 0
12950 #define GIVE_UP(X) \
12951 do { \
12952 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12953 return 0; \
12954 } while (0)
12955 #else
12956 #define GIVE_UP(X) return 0
12957 #endif
12958
12959 SET_TEXT_POS_FROM_MARKER (start, w->start);
12960
12961 /* Don't use this for mini-windows because these can show
12962 messages and mini-buffers, and we don't handle that here. */
12963 if (MINI_WINDOW_P (w))
12964 GIVE_UP (1);
12965
12966 /* This flag is used to prevent redisplay optimizations. */
12967 if (windows_or_buffers_changed || cursor_type_changed)
12968 GIVE_UP (2);
12969
12970 /* Verify that narrowing has not changed.
12971 Also verify that we were not told to prevent redisplay optimizations.
12972 It would be nice to further
12973 reduce the number of cases where this prevents try_window_id. */
12974 if (current_buffer->clip_changed
12975 || current_buffer->prevent_redisplay_optimizations_p)
12976 GIVE_UP (3);
12977
12978 /* Window must either use window-based redisplay or be full width. */
12979 if (!FRAME_WINDOW_P (f)
12980 && (!line_ins_del_ok
12981 || !WINDOW_FULL_WIDTH_P (w)))
12982 GIVE_UP (4);
12983
12984 /* Give up if point is not known NOT to appear in W. */
12985 if (PT < CHARPOS (start))
12986 GIVE_UP (5);
12987
12988 /* Another way to prevent redisplay optimizations. */
12989 if (XFASTINT (w->last_modified) == 0)
12990 GIVE_UP (6);
12991
12992 /* Verify that window is not hscrolled. */
12993 if (XFASTINT (w->hscroll) != 0)
12994 GIVE_UP (7);
12995
12996 /* Verify that display wasn't paused. */
12997 if (NILP (w->window_end_valid))
12998 GIVE_UP (8);
12999
13000 /* Can't use this if highlighting a region because a cursor movement
13001 will do more than just set the cursor. */
13002 if (!NILP (Vtransient_mark_mode)
13003 && !NILP (current_buffer->mark_active))
13004 GIVE_UP (9);
13005
13006 /* Likewise if highlighting trailing whitespace. */
13007 if (!NILP (Vshow_trailing_whitespace))
13008 GIVE_UP (11);
13009
13010 /* Likewise if showing a region. */
13011 if (!NILP (w->region_showing))
13012 GIVE_UP (10);
13013
13014 /* Can use this if overlay arrow position and or string have changed. */
13015 if (overlay_arrows_changed_p ())
13016 GIVE_UP (12);
13017
13018
13019 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13020 only if buffer has really changed. The reason is that the gap is
13021 initially at Z for freshly visited files. The code below would
13022 set end_unchanged to 0 in that case. */
13023 if (MODIFF > SAVE_MODIFF
13024 /* This seems to happen sometimes after saving a buffer. */
13025 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13026 {
13027 if (GPT - BEG < BEG_UNCHANGED)
13028 BEG_UNCHANGED = GPT - BEG;
13029 if (Z - GPT < END_UNCHANGED)
13030 END_UNCHANGED = Z - GPT;
13031 }
13032
13033 /* The position of the first and last character that has been changed. */
13034 first_changed_charpos = BEG + BEG_UNCHANGED;
13035 last_changed_charpos = Z - END_UNCHANGED;
13036
13037 /* If window starts after a line end, and the last change is in
13038 front of that newline, then changes don't affect the display.
13039 This case happens with stealth-fontification. Note that although
13040 the display is unchanged, glyph positions in the matrix have to
13041 be adjusted, of course. */
13042 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13043 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13044 && ((last_changed_charpos < CHARPOS (start)
13045 && CHARPOS (start) == BEGV)
13046 || (last_changed_charpos < CHARPOS (start) - 1
13047 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13048 {
13049 int Z_old, delta, Z_BYTE_old, delta_bytes;
13050 struct glyph_row *r0;
13051
13052 /* Compute how many chars/bytes have been added to or removed
13053 from the buffer. */
13054 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13055 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13056 delta = Z - Z_old;
13057 delta_bytes = Z_BYTE - Z_BYTE_old;
13058
13059 /* Give up if PT is not in the window. Note that it already has
13060 been checked at the start of try_window_id that PT is not in
13061 front of the window start. */
13062 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13063 GIVE_UP (13);
13064
13065 /* If window start is unchanged, we can reuse the whole matrix
13066 as is, after adjusting glyph positions. No need to compute
13067 the window end again, since its offset from Z hasn't changed. */
13068 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13069 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13070 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13071 /* PT must not be in a partially visible line. */
13072 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13073 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13074 {
13075 /* Adjust positions in the glyph matrix. */
13076 if (delta || delta_bytes)
13077 {
13078 struct glyph_row *r1
13079 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13080 increment_matrix_positions (w->current_matrix,
13081 MATRIX_ROW_VPOS (r0, current_matrix),
13082 MATRIX_ROW_VPOS (r1, current_matrix),
13083 delta, delta_bytes);
13084 }
13085
13086 /* Set the cursor. */
13087 row = row_containing_pos (w, PT, r0, NULL, 0);
13088 if (row)
13089 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13090 else
13091 abort ();
13092 return 1;
13093 }
13094 }
13095
13096 /* Handle the case that changes are all below what is displayed in
13097 the window, and that PT is in the window. This shortcut cannot
13098 be taken if ZV is visible in the window, and text has been added
13099 there that is visible in the window. */
13100 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13101 /* ZV is not visible in the window, or there are no
13102 changes at ZV, actually. */
13103 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13104 || first_changed_charpos == last_changed_charpos))
13105 {
13106 struct glyph_row *r0;
13107
13108 /* Give up if PT is not in the window. Note that it already has
13109 been checked at the start of try_window_id that PT is not in
13110 front of the window start. */
13111 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13112 GIVE_UP (14);
13113
13114 /* If window start is unchanged, we can reuse the whole matrix
13115 as is, without changing glyph positions since no text has
13116 been added/removed in front of the window end. */
13117 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13118 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13119 /* PT must not be in a partially visible line. */
13120 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13121 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13122 {
13123 /* We have to compute the window end anew since text
13124 can have been added/removed after it. */
13125 w->window_end_pos
13126 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13127 w->window_end_bytepos
13128 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13129
13130 /* Set the cursor. */
13131 row = row_containing_pos (w, PT, r0, NULL, 0);
13132 if (row)
13133 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13134 else
13135 abort ();
13136 return 2;
13137 }
13138 }
13139
13140 /* Give up if window start is in the changed area.
13141
13142 The condition used to read
13143
13144 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13145
13146 but why that was tested escapes me at the moment. */
13147 if (CHARPOS (start) >= first_changed_charpos
13148 && CHARPOS (start) <= last_changed_charpos)
13149 GIVE_UP (15);
13150
13151 /* Check that window start agrees with the start of the first glyph
13152 row in its current matrix. Check this after we know the window
13153 start is not in changed text, otherwise positions would not be
13154 comparable. */
13155 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13156 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13157 GIVE_UP (16);
13158
13159 /* Give up if the window ends in strings. Overlay strings
13160 at the end are difficult to handle, so don't try. */
13161 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13162 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13163 GIVE_UP (20);
13164
13165 /* Compute the position at which we have to start displaying new
13166 lines. Some of the lines at the top of the window might be
13167 reusable because they are not displaying changed text. Find the
13168 last row in W's current matrix not affected by changes at the
13169 start of current_buffer. Value is null if changes start in the
13170 first line of window. */
13171 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13172 if (last_unchanged_at_beg_row)
13173 {
13174 /* Avoid starting to display in the moddle of a character, a TAB
13175 for instance. This is easier than to set up the iterator
13176 exactly, and it's not a frequent case, so the additional
13177 effort wouldn't really pay off. */
13178 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13179 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13180 && last_unchanged_at_beg_row > w->current_matrix->rows)
13181 --last_unchanged_at_beg_row;
13182
13183 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13184 GIVE_UP (17);
13185
13186 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13187 GIVE_UP (18);
13188 start_pos = it.current.pos;
13189
13190 /* Start displaying new lines in the desired matrix at the same
13191 vpos we would use in the current matrix, i.e. below
13192 last_unchanged_at_beg_row. */
13193 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13194 current_matrix);
13195 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13196 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13197
13198 xassert (it.hpos == 0 && it.current_x == 0);
13199 }
13200 else
13201 {
13202 /* There are no reusable lines at the start of the window.
13203 Start displaying in the first text line. */
13204 start_display (&it, w, start);
13205 it.vpos = it.first_vpos;
13206 start_pos = it.current.pos;
13207 }
13208
13209 /* Find the first row that is not affected by changes at the end of
13210 the buffer. Value will be null if there is no unchanged row, in
13211 which case we must redisplay to the end of the window. delta
13212 will be set to the value by which buffer positions beginning with
13213 first_unchanged_at_end_row have to be adjusted due to text
13214 changes. */
13215 first_unchanged_at_end_row
13216 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13217 IF_DEBUG (debug_delta = delta);
13218 IF_DEBUG (debug_delta_bytes = delta_bytes);
13219
13220 /* Set stop_pos to the buffer position up to which we will have to
13221 display new lines. If first_unchanged_at_end_row != NULL, this
13222 is the buffer position of the start of the line displayed in that
13223 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13224 that we don't stop at a buffer position. */
13225 stop_pos = 0;
13226 if (first_unchanged_at_end_row)
13227 {
13228 xassert (last_unchanged_at_beg_row == NULL
13229 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13230
13231 /* If this is a continuation line, move forward to the next one
13232 that isn't. Changes in lines above affect this line.
13233 Caution: this may move first_unchanged_at_end_row to a row
13234 not displaying text. */
13235 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13236 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13237 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13238 < it.last_visible_y))
13239 ++first_unchanged_at_end_row;
13240
13241 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13242 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13243 >= it.last_visible_y))
13244 first_unchanged_at_end_row = NULL;
13245 else
13246 {
13247 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13248 + delta);
13249 first_unchanged_at_end_vpos
13250 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13251 xassert (stop_pos >= Z - END_UNCHANGED);
13252 }
13253 }
13254 else if (last_unchanged_at_beg_row == NULL)
13255 GIVE_UP (19);
13256
13257
13258 #if GLYPH_DEBUG
13259
13260 /* Either there is no unchanged row at the end, or the one we have
13261 now displays text. This is a necessary condition for the window
13262 end pos calculation at the end of this function. */
13263 xassert (first_unchanged_at_end_row == NULL
13264 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13265
13266 debug_last_unchanged_at_beg_vpos
13267 = (last_unchanged_at_beg_row
13268 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13269 : -1);
13270 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13271
13272 #endif /* GLYPH_DEBUG != 0 */
13273
13274
13275 /* Display new lines. Set last_text_row to the last new line
13276 displayed which has text on it, i.e. might end up as being the
13277 line where the window_end_vpos is. */
13278 w->cursor.vpos = -1;
13279 last_text_row = NULL;
13280 overlay_arrow_seen = 0;
13281 while (it.current_y < it.last_visible_y
13282 && !fonts_changed_p
13283 && (first_unchanged_at_end_row == NULL
13284 || IT_CHARPOS (it) < stop_pos))
13285 {
13286 if (display_line (&it))
13287 last_text_row = it.glyph_row - 1;
13288 }
13289
13290 if (fonts_changed_p)
13291 return -1;
13292
13293
13294 /* Compute differences in buffer positions, y-positions etc. for
13295 lines reused at the bottom of the window. Compute what we can
13296 scroll. */
13297 if (first_unchanged_at_end_row
13298 /* No lines reused because we displayed everything up to the
13299 bottom of the window. */
13300 && it.current_y < it.last_visible_y)
13301 {
13302 dvpos = (it.vpos
13303 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13304 current_matrix));
13305 dy = it.current_y - first_unchanged_at_end_row->y;
13306 run.current_y = first_unchanged_at_end_row->y;
13307 run.desired_y = run.current_y + dy;
13308 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13309 }
13310 else
13311 {
13312 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13313 first_unchanged_at_end_row = NULL;
13314 }
13315 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13316
13317
13318 /* Find the cursor if not already found. We have to decide whether
13319 PT will appear on this window (it sometimes doesn't, but this is
13320 not a very frequent case.) This decision has to be made before
13321 the current matrix is altered. A value of cursor.vpos < 0 means
13322 that PT is either in one of the lines beginning at
13323 first_unchanged_at_end_row or below the window. Don't care for
13324 lines that might be displayed later at the window end; as
13325 mentioned, this is not a frequent case. */
13326 if (w->cursor.vpos < 0)
13327 {
13328 /* Cursor in unchanged rows at the top? */
13329 if (PT < CHARPOS (start_pos)
13330 && last_unchanged_at_beg_row)
13331 {
13332 row = row_containing_pos (w, PT,
13333 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13334 last_unchanged_at_beg_row + 1, 0);
13335 if (row)
13336 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13337 }
13338
13339 /* Start from first_unchanged_at_end_row looking for PT. */
13340 else if (first_unchanged_at_end_row)
13341 {
13342 row = row_containing_pos (w, PT - delta,
13343 first_unchanged_at_end_row, NULL, 0);
13344 if (row)
13345 set_cursor_from_row (w, row, w->current_matrix, delta,
13346 delta_bytes, dy, dvpos);
13347 }
13348
13349 /* Give up if cursor was not found. */
13350 if (w->cursor.vpos < 0)
13351 {
13352 clear_glyph_matrix (w->desired_matrix);
13353 return -1;
13354 }
13355 }
13356
13357 /* Don't let the cursor end in the scroll margins. */
13358 {
13359 int this_scroll_margin, cursor_height;
13360
13361 this_scroll_margin = max (0, scroll_margin);
13362 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13363 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13364 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13365
13366 if ((w->cursor.y < this_scroll_margin
13367 && CHARPOS (start) > BEGV)
13368 /* Don't take scroll margin into account at the bottom because
13369 old redisplay didn't do it either. */
13370 || w->cursor.y + cursor_height > it.last_visible_y)
13371 {
13372 w->cursor.vpos = -1;
13373 clear_glyph_matrix (w->desired_matrix);
13374 return -1;
13375 }
13376 }
13377
13378 /* Scroll the display. Do it before changing the current matrix so
13379 that xterm.c doesn't get confused about where the cursor glyph is
13380 found. */
13381 if (dy && run.height)
13382 {
13383 update_begin (f);
13384
13385 if (FRAME_WINDOW_P (f))
13386 {
13387 rif->update_window_begin_hook (w);
13388 rif->clear_window_mouse_face (w);
13389 rif->scroll_run_hook (w, &run);
13390 rif->update_window_end_hook (w, 0, 0);
13391 }
13392 else
13393 {
13394 /* Terminal frame. In this case, dvpos gives the number of
13395 lines to scroll by; dvpos < 0 means scroll up. */
13396 int first_unchanged_at_end_vpos
13397 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13398 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13399 int end = (WINDOW_TOP_EDGE_LINE (w)
13400 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13401 + window_internal_height (w));
13402
13403 /* Perform the operation on the screen. */
13404 if (dvpos > 0)
13405 {
13406 /* Scroll last_unchanged_at_beg_row to the end of the
13407 window down dvpos lines. */
13408 set_terminal_window (end);
13409
13410 /* On dumb terminals delete dvpos lines at the end
13411 before inserting dvpos empty lines. */
13412 if (!scroll_region_ok)
13413 ins_del_lines (end - dvpos, -dvpos);
13414
13415 /* Insert dvpos empty lines in front of
13416 last_unchanged_at_beg_row. */
13417 ins_del_lines (from, dvpos);
13418 }
13419 else if (dvpos < 0)
13420 {
13421 /* Scroll up last_unchanged_at_beg_vpos to the end of
13422 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13423 set_terminal_window (end);
13424
13425 /* Delete dvpos lines in front of
13426 last_unchanged_at_beg_vpos. ins_del_lines will set
13427 the cursor to the given vpos and emit |dvpos| delete
13428 line sequences. */
13429 ins_del_lines (from + dvpos, dvpos);
13430
13431 /* On a dumb terminal insert dvpos empty lines at the
13432 end. */
13433 if (!scroll_region_ok)
13434 ins_del_lines (end + dvpos, -dvpos);
13435 }
13436
13437 set_terminal_window (0);
13438 }
13439
13440 update_end (f);
13441 }
13442
13443 /* Shift reused rows of the current matrix to the right position.
13444 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13445 text. */
13446 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13447 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13448 if (dvpos < 0)
13449 {
13450 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13451 bottom_vpos, dvpos);
13452 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13453 bottom_vpos, 0);
13454 }
13455 else if (dvpos > 0)
13456 {
13457 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13458 bottom_vpos, dvpos);
13459 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13460 first_unchanged_at_end_vpos + dvpos, 0);
13461 }
13462
13463 /* For frame-based redisplay, make sure that current frame and window
13464 matrix are in sync with respect to glyph memory. */
13465 if (!FRAME_WINDOW_P (f))
13466 sync_frame_with_window_matrix_rows (w);
13467
13468 /* Adjust buffer positions in reused rows. */
13469 if (delta)
13470 increment_matrix_positions (current_matrix,
13471 first_unchanged_at_end_vpos + dvpos,
13472 bottom_vpos, delta, delta_bytes);
13473
13474 /* Adjust Y positions. */
13475 if (dy)
13476 shift_glyph_matrix (w, current_matrix,
13477 first_unchanged_at_end_vpos + dvpos,
13478 bottom_vpos, dy);
13479
13480 if (first_unchanged_at_end_row)
13481 first_unchanged_at_end_row += dvpos;
13482
13483 /* If scrolling up, there may be some lines to display at the end of
13484 the window. */
13485 last_text_row_at_end = NULL;
13486 if (dy < 0)
13487 {
13488 /* Scrolling up can leave for example a partially visible line
13489 at the end of the window to be redisplayed. */
13490 /* Set last_row to the glyph row in the current matrix where the
13491 window end line is found. It has been moved up or down in
13492 the matrix by dvpos. */
13493 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13494 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13495
13496 /* If last_row is the window end line, it should display text. */
13497 xassert (last_row->displays_text_p);
13498
13499 /* If window end line was partially visible before, begin
13500 displaying at that line. Otherwise begin displaying with the
13501 line following it. */
13502 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13503 {
13504 init_to_row_start (&it, w, last_row);
13505 it.vpos = last_vpos;
13506 it.current_y = last_row->y;
13507 }
13508 else
13509 {
13510 init_to_row_end (&it, w, last_row);
13511 it.vpos = 1 + last_vpos;
13512 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13513 ++last_row;
13514 }
13515
13516 /* We may start in a continuation line. If so, we have to
13517 get the right continuation_lines_width and current_x. */
13518 it.continuation_lines_width = last_row->continuation_lines_width;
13519 it.hpos = it.current_x = 0;
13520
13521 /* Display the rest of the lines at the window end. */
13522 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13523 while (it.current_y < it.last_visible_y
13524 && !fonts_changed_p)
13525 {
13526 /* Is it always sure that the display agrees with lines in
13527 the current matrix? I don't think so, so we mark rows
13528 displayed invalid in the current matrix by setting their
13529 enabled_p flag to zero. */
13530 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13531 if (display_line (&it))
13532 last_text_row_at_end = it.glyph_row - 1;
13533 }
13534 }
13535
13536 /* Update window_end_pos and window_end_vpos. */
13537 if (first_unchanged_at_end_row
13538 && first_unchanged_at_end_row->y < it.last_visible_y
13539 && !last_text_row_at_end)
13540 {
13541 /* Window end line if one of the preserved rows from the current
13542 matrix. Set row to the last row displaying text in current
13543 matrix starting at first_unchanged_at_end_row, after
13544 scrolling. */
13545 xassert (first_unchanged_at_end_row->displays_text_p);
13546 row = find_last_row_displaying_text (w->current_matrix, &it,
13547 first_unchanged_at_end_row);
13548 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13549
13550 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13551 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13552 w->window_end_vpos
13553 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13554 xassert (w->window_end_bytepos >= 0);
13555 IF_DEBUG (debug_method_add (w, "A"));
13556 }
13557 else if (last_text_row_at_end)
13558 {
13559 w->window_end_pos
13560 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13561 w->window_end_bytepos
13562 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13563 w->window_end_vpos
13564 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13565 xassert (w->window_end_bytepos >= 0);
13566 IF_DEBUG (debug_method_add (w, "B"));
13567 }
13568 else if (last_text_row)
13569 {
13570 /* We have displayed either to the end of the window or at the
13571 end of the window, i.e. the last row with text is to be found
13572 in the desired matrix. */
13573 w->window_end_pos
13574 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13575 w->window_end_bytepos
13576 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13577 w->window_end_vpos
13578 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13579 xassert (w->window_end_bytepos >= 0);
13580 }
13581 else if (first_unchanged_at_end_row == NULL
13582 && last_text_row == NULL
13583 && last_text_row_at_end == NULL)
13584 {
13585 /* Displayed to end of window, but no line containing text was
13586 displayed. Lines were deleted at the end of the window. */
13587 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13588 int vpos = XFASTINT (w->window_end_vpos);
13589 struct glyph_row *current_row = current_matrix->rows + vpos;
13590 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13591
13592 for (row = NULL;
13593 row == NULL && vpos >= first_vpos;
13594 --vpos, --current_row, --desired_row)
13595 {
13596 if (desired_row->enabled_p)
13597 {
13598 if (desired_row->displays_text_p)
13599 row = desired_row;
13600 }
13601 else if (current_row->displays_text_p)
13602 row = current_row;
13603 }
13604
13605 xassert (row != NULL);
13606 w->window_end_vpos = make_number (vpos + 1);
13607 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13608 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13609 xassert (w->window_end_bytepos >= 0);
13610 IF_DEBUG (debug_method_add (w, "C"));
13611 }
13612 else
13613 abort ();
13614
13615 #if 0 /* This leads to problems, for instance when the cursor is
13616 at ZV, and the cursor line displays no text. */
13617 /* Disable rows below what's displayed in the window. This makes
13618 debugging easier. */
13619 enable_glyph_matrix_rows (current_matrix,
13620 XFASTINT (w->window_end_vpos) + 1,
13621 bottom_vpos, 0);
13622 #endif
13623
13624 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13625 debug_end_vpos = XFASTINT (w->window_end_vpos));
13626
13627 /* Record that display has not been completed. */
13628 w->window_end_valid = Qnil;
13629 w->desired_matrix->no_scrolling_p = 1;
13630 return 3;
13631
13632 #undef GIVE_UP
13633 }
13634
13635
13636 \f
13637 /***********************************************************************
13638 More debugging support
13639 ***********************************************************************/
13640
13641 #if GLYPH_DEBUG
13642
13643 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13644 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13645 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13646
13647
13648 /* Dump the contents of glyph matrix MATRIX on stderr.
13649
13650 GLYPHS 0 means don't show glyph contents.
13651 GLYPHS 1 means show glyphs in short form
13652 GLYPHS > 1 means show glyphs in long form. */
13653
13654 void
13655 dump_glyph_matrix (matrix, glyphs)
13656 struct glyph_matrix *matrix;
13657 int glyphs;
13658 {
13659 int i;
13660 for (i = 0; i < matrix->nrows; ++i)
13661 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13662 }
13663
13664
13665 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13666 the glyph row and area where the glyph comes from. */
13667
13668 void
13669 dump_glyph (row, glyph, area)
13670 struct glyph_row *row;
13671 struct glyph *glyph;
13672 int area;
13673 {
13674 if (glyph->type == CHAR_GLYPH)
13675 {
13676 fprintf (stderr,
13677 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13678 glyph - row->glyphs[TEXT_AREA],
13679 'C',
13680 glyph->charpos,
13681 (BUFFERP (glyph->object)
13682 ? 'B'
13683 : (STRINGP (glyph->object)
13684 ? 'S'
13685 : '-')),
13686 glyph->pixel_width,
13687 glyph->u.ch,
13688 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13689 ? glyph->u.ch
13690 : '.'),
13691 glyph->face_id,
13692 glyph->left_box_line_p,
13693 glyph->right_box_line_p);
13694 }
13695 else if (glyph->type == STRETCH_GLYPH)
13696 {
13697 fprintf (stderr,
13698 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13699 glyph - row->glyphs[TEXT_AREA],
13700 'S',
13701 glyph->charpos,
13702 (BUFFERP (glyph->object)
13703 ? 'B'
13704 : (STRINGP (glyph->object)
13705 ? 'S'
13706 : '-')),
13707 glyph->pixel_width,
13708 0,
13709 '.',
13710 glyph->face_id,
13711 glyph->left_box_line_p,
13712 glyph->right_box_line_p);
13713 }
13714 else if (glyph->type == IMAGE_GLYPH)
13715 {
13716 fprintf (stderr,
13717 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13718 glyph - row->glyphs[TEXT_AREA],
13719 'I',
13720 glyph->charpos,
13721 (BUFFERP (glyph->object)
13722 ? 'B'
13723 : (STRINGP (glyph->object)
13724 ? 'S'
13725 : '-')),
13726 glyph->pixel_width,
13727 glyph->u.img_id,
13728 '.',
13729 glyph->face_id,
13730 glyph->left_box_line_p,
13731 glyph->right_box_line_p);
13732 }
13733 }
13734
13735
13736 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13737 GLYPHS 0 means don't show glyph contents.
13738 GLYPHS 1 means show glyphs in short form
13739 GLYPHS > 1 means show glyphs in long form. */
13740
13741 void
13742 dump_glyph_row (row, vpos, glyphs)
13743 struct glyph_row *row;
13744 int vpos, glyphs;
13745 {
13746 if (glyphs != 1)
13747 {
13748 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13749 fprintf (stderr, "=======================================================================\n");
13750
13751 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13752 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13753 vpos,
13754 MATRIX_ROW_START_CHARPOS (row),
13755 MATRIX_ROW_END_CHARPOS (row),
13756 row->used[TEXT_AREA],
13757 row->contains_overlapping_glyphs_p,
13758 row->enabled_p,
13759 row->truncated_on_left_p,
13760 row->truncated_on_right_p,
13761 row->overlay_arrow_p,
13762 row->continued_p,
13763 MATRIX_ROW_CONTINUATION_LINE_P (row),
13764 row->displays_text_p,
13765 row->ends_at_zv_p,
13766 row->fill_line_p,
13767 row->ends_in_middle_of_char_p,
13768 row->starts_in_middle_of_char_p,
13769 row->mouse_face_p,
13770 row->x,
13771 row->y,
13772 row->pixel_width,
13773 row->height,
13774 row->visible_height,
13775 row->ascent,
13776 row->phys_ascent);
13777 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13778 row->end.overlay_string_index,
13779 row->continuation_lines_width);
13780 fprintf (stderr, "%9d %5d\n",
13781 CHARPOS (row->start.string_pos),
13782 CHARPOS (row->end.string_pos));
13783 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13784 row->end.dpvec_index);
13785 }
13786
13787 if (glyphs > 1)
13788 {
13789 int area;
13790
13791 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13792 {
13793 struct glyph *glyph = row->glyphs[area];
13794 struct glyph *glyph_end = glyph + row->used[area];
13795
13796 /* Glyph for a line end in text. */
13797 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13798 ++glyph_end;
13799
13800 if (glyph < glyph_end)
13801 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13802
13803 for (; glyph < glyph_end; ++glyph)
13804 dump_glyph (row, glyph, area);
13805 }
13806 }
13807 else if (glyphs == 1)
13808 {
13809 int area;
13810
13811 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13812 {
13813 char *s = (char *) alloca (row->used[area] + 1);
13814 int i;
13815
13816 for (i = 0; i < row->used[area]; ++i)
13817 {
13818 struct glyph *glyph = row->glyphs[area] + i;
13819 if (glyph->type == CHAR_GLYPH
13820 && glyph->u.ch < 0x80
13821 && glyph->u.ch >= ' ')
13822 s[i] = glyph->u.ch;
13823 else
13824 s[i] = '.';
13825 }
13826
13827 s[i] = '\0';
13828 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13829 }
13830 }
13831 }
13832
13833
13834 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13835 Sdump_glyph_matrix, 0, 1, "p",
13836 doc: /* Dump the current matrix of the selected window to stderr.
13837 Shows contents of glyph row structures. With non-nil
13838 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13839 glyphs in short form, otherwise show glyphs in long form. */)
13840 (glyphs)
13841 Lisp_Object glyphs;
13842 {
13843 struct window *w = XWINDOW (selected_window);
13844 struct buffer *buffer = XBUFFER (w->buffer);
13845
13846 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13847 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13848 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13849 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13850 fprintf (stderr, "=============================================\n");
13851 dump_glyph_matrix (w->current_matrix,
13852 NILP (glyphs) ? 0 : XINT (glyphs));
13853 return Qnil;
13854 }
13855
13856
13857 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13858 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13859 ()
13860 {
13861 struct frame *f = XFRAME (selected_frame);
13862 dump_glyph_matrix (f->current_matrix, 1);
13863 return Qnil;
13864 }
13865
13866
13867 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13868 doc: /* Dump glyph row ROW to stderr.
13869 GLYPH 0 means don't dump glyphs.
13870 GLYPH 1 means dump glyphs in short form.
13871 GLYPH > 1 or omitted means dump glyphs in long form. */)
13872 (row, glyphs)
13873 Lisp_Object row, glyphs;
13874 {
13875 struct glyph_matrix *matrix;
13876 int vpos;
13877
13878 CHECK_NUMBER (row);
13879 matrix = XWINDOW (selected_window)->current_matrix;
13880 vpos = XINT (row);
13881 if (vpos >= 0 && vpos < matrix->nrows)
13882 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13883 vpos,
13884 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13885 return Qnil;
13886 }
13887
13888
13889 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13890 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13891 GLYPH 0 means don't dump glyphs.
13892 GLYPH 1 means dump glyphs in short form.
13893 GLYPH > 1 or omitted means dump glyphs in long form. */)
13894 (row, glyphs)
13895 Lisp_Object row, glyphs;
13896 {
13897 struct frame *sf = SELECTED_FRAME ();
13898 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13899 int vpos;
13900
13901 CHECK_NUMBER (row);
13902 vpos = XINT (row);
13903 if (vpos >= 0 && vpos < m->nrows)
13904 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13905 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13906 return Qnil;
13907 }
13908
13909
13910 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13911 doc: /* Toggle tracing of redisplay.
13912 With ARG, turn tracing on if and only if ARG is positive. */)
13913 (arg)
13914 Lisp_Object arg;
13915 {
13916 if (NILP (arg))
13917 trace_redisplay_p = !trace_redisplay_p;
13918 else
13919 {
13920 arg = Fprefix_numeric_value (arg);
13921 trace_redisplay_p = XINT (arg) > 0;
13922 }
13923
13924 return Qnil;
13925 }
13926
13927
13928 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13929 doc: /* Like `format', but print result to stderr.
13930 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13931 (nargs, args)
13932 int nargs;
13933 Lisp_Object *args;
13934 {
13935 Lisp_Object s = Fformat (nargs, args);
13936 fprintf (stderr, "%s", SDATA (s));
13937 return Qnil;
13938 }
13939
13940 #endif /* GLYPH_DEBUG */
13941
13942
13943 \f
13944 /***********************************************************************
13945 Building Desired Matrix Rows
13946 ***********************************************************************/
13947
13948 /* Return a temporary glyph row holding the glyphs of an overlay
13949 arrow. Only used for non-window-redisplay windows. */
13950
13951 static struct glyph_row *
13952 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
13953 struct window *w;
13954 Lisp_Object overlay_arrow_string;
13955 {
13956 struct frame *f = XFRAME (WINDOW_FRAME (w));
13957 struct buffer *buffer = XBUFFER (w->buffer);
13958 struct buffer *old = current_buffer;
13959 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13960 int arrow_len = SCHARS (overlay_arrow_string);
13961 const unsigned char *arrow_end = arrow_string + arrow_len;
13962 const unsigned char *p;
13963 struct it it;
13964 int multibyte_p;
13965 int n_glyphs_before;
13966
13967 set_buffer_temp (buffer);
13968 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13969 it.glyph_row->used[TEXT_AREA] = 0;
13970 SET_TEXT_POS (it.position, 0, 0);
13971
13972 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13973 p = arrow_string;
13974 while (p < arrow_end)
13975 {
13976 Lisp_Object face, ilisp;
13977
13978 /* Get the next character. */
13979 if (multibyte_p)
13980 it.c = string_char_and_length (p, arrow_len, &it.len);
13981 else
13982 it.c = *p, it.len = 1;
13983 p += it.len;
13984
13985 /* Get its face. */
13986 ilisp = make_number (p - arrow_string);
13987 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
13988 it.face_id = compute_char_face (f, it.c, face);
13989
13990 /* Compute its width, get its glyphs. */
13991 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13992 SET_TEXT_POS (it.position, -1, -1);
13993 PRODUCE_GLYPHS (&it);
13994
13995 /* If this character doesn't fit any more in the line, we have
13996 to remove some glyphs. */
13997 if (it.current_x > it.last_visible_x)
13998 {
13999 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14000 break;
14001 }
14002 }
14003
14004 set_buffer_temp (old);
14005 return it.glyph_row;
14006 }
14007
14008
14009 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14010 glyphs are only inserted for terminal frames since we can't really
14011 win with truncation glyphs when partially visible glyphs are
14012 involved. Which glyphs to insert is determined by
14013 produce_special_glyphs. */
14014
14015 static void
14016 insert_left_trunc_glyphs (it)
14017 struct it *it;
14018 {
14019 struct it truncate_it;
14020 struct glyph *from, *end, *to, *toend;
14021
14022 xassert (!FRAME_WINDOW_P (it->f));
14023
14024 /* Get the truncation glyphs. */
14025 truncate_it = *it;
14026 truncate_it.current_x = 0;
14027 truncate_it.face_id = DEFAULT_FACE_ID;
14028 truncate_it.glyph_row = &scratch_glyph_row;
14029 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14030 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14031 truncate_it.object = make_number (0);
14032 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14033
14034 /* Overwrite glyphs from IT with truncation glyphs. */
14035 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14036 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14037 to = it->glyph_row->glyphs[TEXT_AREA];
14038 toend = to + it->glyph_row->used[TEXT_AREA];
14039
14040 while (from < end)
14041 *to++ = *from++;
14042
14043 /* There may be padding glyphs left over. Overwrite them too. */
14044 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14045 {
14046 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14047 while (from < end)
14048 *to++ = *from++;
14049 }
14050
14051 if (to > toend)
14052 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14053 }
14054
14055
14056 /* Compute the pixel height and width of IT->glyph_row.
14057
14058 Most of the time, ascent and height of a display line will be equal
14059 to the max_ascent and max_height values of the display iterator
14060 structure. This is not the case if
14061
14062 1. We hit ZV without displaying anything. In this case, max_ascent
14063 and max_height will be zero.
14064
14065 2. We have some glyphs that don't contribute to the line height.
14066 (The glyph row flag contributes_to_line_height_p is for future
14067 pixmap extensions).
14068
14069 The first case is easily covered by using default values because in
14070 these cases, the line height does not really matter, except that it
14071 must not be zero. */
14072
14073 static void
14074 compute_line_metrics (it)
14075 struct it *it;
14076 {
14077 struct glyph_row *row = it->glyph_row;
14078 int area, i;
14079
14080 if (FRAME_WINDOW_P (it->f))
14081 {
14082 int i, min_y, max_y;
14083
14084 /* The line may consist of one space only, that was added to
14085 place the cursor on it. If so, the row's height hasn't been
14086 computed yet. */
14087 if (row->height == 0)
14088 {
14089 if (it->max_ascent + it->max_descent == 0)
14090 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14091 row->ascent = it->max_ascent;
14092 row->height = it->max_ascent + it->max_descent;
14093 row->phys_ascent = it->max_phys_ascent;
14094 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14095 }
14096
14097 /* Compute the width of this line. */
14098 row->pixel_width = row->x;
14099 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14100 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14101
14102 xassert (row->pixel_width >= 0);
14103 xassert (row->ascent >= 0 && row->height > 0);
14104
14105 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14106 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14107
14108 /* If first line's physical ascent is larger than its logical
14109 ascent, use the physical ascent, and make the row taller.
14110 This makes accented characters fully visible. */
14111 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14112 && row->phys_ascent > row->ascent)
14113 {
14114 row->height += row->phys_ascent - row->ascent;
14115 row->ascent = row->phys_ascent;
14116 }
14117
14118 /* Compute how much of the line is visible. */
14119 row->visible_height = row->height;
14120
14121 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14122 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14123
14124 if (row->y < min_y)
14125 row->visible_height -= min_y - row->y;
14126 if (row->y + row->height > max_y)
14127 row->visible_height -= row->y + row->height - max_y;
14128 }
14129 else
14130 {
14131 row->pixel_width = row->used[TEXT_AREA];
14132 if (row->continued_p)
14133 row->pixel_width -= it->continuation_pixel_width;
14134 else if (row->truncated_on_right_p)
14135 row->pixel_width -= it->truncation_pixel_width;
14136 row->ascent = row->phys_ascent = 0;
14137 row->height = row->phys_height = row->visible_height = 1;
14138 }
14139
14140 /* Compute a hash code for this row. */
14141 row->hash = 0;
14142 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14143 for (i = 0; i < row->used[area]; ++i)
14144 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14145 + row->glyphs[area][i].u.val
14146 + row->glyphs[area][i].face_id
14147 + row->glyphs[area][i].padding_p
14148 + (row->glyphs[area][i].type << 2));
14149
14150 it->max_ascent = it->max_descent = 0;
14151 it->max_phys_ascent = it->max_phys_descent = 0;
14152 }
14153
14154
14155 /* Append one space to the glyph row of iterator IT if doing a
14156 window-based redisplay. The space has the same face as
14157 IT->face_id. Value is non-zero if a space was added.
14158
14159 This function is called to make sure that there is always one glyph
14160 at the end of a glyph row that the cursor can be set on under
14161 window-systems. (If there weren't such a glyph we would not know
14162 how wide and tall a box cursor should be displayed).
14163
14164 At the same time this space let's a nicely handle clearing to the
14165 end of the line if the row ends in italic text. */
14166
14167 static int
14168 append_space_for_newline (it, default_face_p)
14169 struct it *it;
14170 int default_face_p;
14171 {
14172 if (FRAME_WINDOW_P (it->f))
14173 {
14174 int n = it->glyph_row->used[TEXT_AREA];
14175
14176 if (it->glyph_row->glyphs[TEXT_AREA] + n
14177 < it->glyph_row->glyphs[1 + TEXT_AREA])
14178 {
14179 /* Save some values that must not be changed.
14180 Must save IT->c and IT->len because otherwise
14181 ITERATOR_AT_END_P wouldn't work anymore after
14182 append_space_for_newline has been called. */
14183 enum display_element_type saved_what = it->what;
14184 int saved_c = it->c, saved_len = it->len;
14185 int saved_x = it->current_x;
14186 int saved_face_id = it->face_id;
14187 struct text_pos saved_pos;
14188 Lisp_Object saved_object;
14189 struct face *face;
14190
14191 saved_object = it->object;
14192 saved_pos = it->position;
14193
14194 it->what = IT_CHARACTER;
14195 bzero (&it->position, sizeof it->position);
14196 it->object = make_number (0);
14197 it->c = ' ';
14198 it->len = 1;
14199
14200 if (default_face_p)
14201 it->face_id = DEFAULT_FACE_ID;
14202 else if (it->face_before_selective_p)
14203 it->face_id = it->saved_face_id;
14204 face = FACE_FROM_ID (it->f, it->face_id);
14205 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14206
14207 PRODUCE_GLYPHS (it);
14208
14209 it->override_ascent = -1;
14210 it->constrain_row_ascent_descent_p = 0;
14211 it->current_x = saved_x;
14212 it->object = saved_object;
14213 it->position = saved_pos;
14214 it->what = saved_what;
14215 it->face_id = saved_face_id;
14216 it->len = saved_len;
14217 it->c = saved_c;
14218 return 1;
14219 }
14220 }
14221
14222 return 0;
14223 }
14224
14225
14226 /* Extend the face of the last glyph in the text area of IT->glyph_row
14227 to the end of the display line. Called from display_line.
14228 If the glyph row is empty, add a space glyph to it so that we
14229 know the face to draw. Set the glyph row flag fill_line_p. */
14230
14231 static void
14232 extend_face_to_end_of_line (it)
14233 struct it *it;
14234 {
14235 struct face *face;
14236 struct frame *f = it->f;
14237
14238 /* If line is already filled, do nothing. */
14239 if (it->current_x >= it->last_visible_x)
14240 return;
14241
14242 /* Face extension extends the background and box of IT->face_id
14243 to the end of the line. If the background equals the background
14244 of the frame, we don't have to do anything. */
14245 if (it->face_before_selective_p)
14246 face = FACE_FROM_ID (it->f, it->saved_face_id);
14247 else
14248 face = FACE_FROM_ID (f, it->face_id);
14249
14250 if (FRAME_WINDOW_P (f)
14251 && face->box == FACE_NO_BOX
14252 && face->background == FRAME_BACKGROUND_PIXEL (f)
14253 && !face->stipple)
14254 return;
14255
14256 /* Set the glyph row flag indicating that the face of the last glyph
14257 in the text area has to be drawn to the end of the text area. */
14258 it->glyph_row->fill_line_p = 1;
14259
14260 /* If current character of IT is not ASCII, make sure we have the
14261 ASCII face. This will be automatically undone the next time
14262 get_next_display_element returns a multibyte character. Note
14263 that the character will always be single byte in unibyte text. */
14264 if (!SINGLE_BYTE_CHAR_P (it->c))
14265 {
14266 it->face_id = FACE_FOR_CHAR (f, face, 0);
14267 }
14268
14269 if (FRAME_WINDOW_P (f))
14270 {
14271 /* If the row is empty, add a space with the current face of IT,
14272 so that we know which face to draw. */
14273 if (it->glyph_row->used[TEXT_AREA] == 0)
14274 {
14275 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14276 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14277 it->glyph_row->used[TEXT_AREA] = 1;
14278 }
14279 }
14280 else
14281 {
14282 /* Save some values that must not be changed. */
14283 int saved_x = it->current_x;
14284 struct text_pos saved_pos;
14285 Lisp_Object saved_object;
14286 enum display_element_type saved_what = it->what;
14287 int saved_face_id = it->face_id;
14288
14289 saved_object = it->object;
14290 saved_pos = it->position;
14291
14292 it->what = IT_CHARACTER;
14293 bzero (&it->position, sizeof it->position);
14294 it->object = make_number (0);
14295 it->c = ' ';
14296 it->len = 1;
14297 it->face_id = face->id;
14298
14299 PRODUCE_GLYPHS (it);
14300
14301 while (it->current_x <= it->last_visible_x)
14302 PRODUCE_GLYPHS (it);
14303
14304 /* Don't count these blanks really. It would let us insert a left
14305 truncation glyph below and make us set the cursor on them, maybe. */
14306 it->current_x = saved_x;
14307 it->object = saved_object;
14308 it->position = saved_pos;
14309 it->what = saved_what;
14310 it->face_id = saved_face_id;
14311 }
14312 }
14313
14314
14315 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14316 trailing whitespace. */
14317
14318 static int
14319 trailing_whitespace_p (charpos)
14320 int charpos;
14321 {
14322 int bytepos = CHAR_TO_BYTE (charpos);
14323 int c = 0;
14324
14325 while (bytepos < ZV_BYTE
14326 && (c = FETCH_CHAR (bytepos),
14327 c == ' ' || c == '\t'))
14328 ++bytepos;
14329
14330 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14331 {
14332 if (bytepos != PT_BYTE)
14333 return 1;
14334 }
14335 return 0;
14336 }
14337
14338
14339 /* Highlight trailing whitespace, if any, in ROW. */
14340
14341 void
14342 highlight_trailing_whitespace (f, row)
14343 struct frame *f;
14344 struct glyph_row *row;
14345 {
14346 int used = row->used[TEXT_AREA];
14347
14348 if (used)
14349 {
14350 struct glyph *start = row->glyphs[TEXT_AREA];
14351 struct glyph *glyph = start + used - 1;
14352
14353 /* Skip over glyphs inserted to display the cursor at the
14354 end of a line, for extending the face of the last glyph
14355 to the end of the line on terminals, and for truncation
14356 and continuation glyphs. */
14357 while (glyph >= start
14358 && glyph->type == CHAR_GLYPH
14359 && INTEGERP (glyph->object))
14360 --glyph;
14361
14362 /* If last glyph is a space or stretch, and it's trailing
14363 whitespace, set the face of all trailing whitespace glyphs in
14364 IT->glyph_row to `trailing-whitespace'. */
14365 if (glyph >= start
14366 && BUFFERP (glyph->object)
14367 && (glyph->type == STRETCH_GLYPH
14368 || (glyph->type == CHAR_GLYPH
14369 && glyph->u.ch == ' '))
14370 && trailing_whitespace_p (glyph->charpos))
14371 {
14372 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14373
14374 while (glyph >= start
14375 && BUFFERP (glyph->object)
14376 && (glyph->type == STRETCH_GLYPH
14377 || (glyph->type == CHAR_GLYPH
14378 && glyph->u.ch == ' ')))
14379 (glyph--)->face_id = face_id;
14380 }
14381 }
14382 }
14383
14384
14385 /* Value is non-zero if glyph row ROW in window W should be
14386 used to hold the cursor. */
14387
14388 static int
14389 cursor_row_p (w, row)
14390 struct window *w;
14391 struct glyph_row *row;
14392 {
14393 int cursor_row_p = 1;
14394
14395 if (PT == MATRIX_ROW_END_CHARPOS (row))
14396 {
14397 /* If the row ends with a newline from a string, we don't want
14398 the cursor there (if the row is continued it doesn't end in a
14399 newline). */
14400 if (CHARPOS (row->end.string_pos) >= 0
14401 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14402 cursor_row_p = row->continued_p;
14403
14404 /* If the row ends at ZV, display the cursor at the end of that
14405 row instead of at the start of the row below. */
14406 else if (row->ends_at_zv_p)
14407 cursor_row_p = 1;
14408 else
14409 cursor_row_p = 0;
14410 }
14411
14412 return cursor_row_p;
14413 }
14414
14415
14416 /* Construct the glyph row IT->glyph_row in the desired matrix of
14417 IT->w from text at the current position of IT. See dispextern.h
14418 for an overview of struct it. Value is non-zero if
14419 IT->glyph_row displays text, as opposed to a line displaying ZV
14420 only. */
14421
14422 static int
14423 display_line (it)
14424 struct it *it;
14425 {
14426 struct glyph_row *row = it->glyph_row;
14427 int overlay_arrow_bitmap;
14428 Lisp_Object overlay_arrow_string;
14429
14430 /* We always start displaying at hpos zero even if hscrolled. */
14431 xassert (it->hpos == 0 && it->current_x == 0);
14432
14433 /* We must not display in a row that's not a text row. */
14434 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14435 < it->w->desired_matrix->nrows);
14436
14437 /* Is IT->w showing the region? */
14438 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14439
14440 /* Clear the result glyph row and enable it. */
14441 prepare_desired_row (row);
14442
14443 row->y = it->current_y;
14444 row->start = it->start;
14445 row->continuation_lines_width = it->continuation_lines_width;
14446 row->displays_text_p = 1;
14447 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14448 it->starts_in_middle_of_char_p = 0;
14449
14450 /* Arrange the overlays nicely for our purposes. Usually, we call
14451 display_line on only one line at a time, in which case this
14452 can't really hurt too much, or we call it on lines which appear
14453 one after another in the buffer, in which case all calls to
14454 recenter_overlay_lists but the first will be pretty cheap. */
14455 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14456
14457 /* Move over display elements that are not visible because we are
14458 hscrolled. This may stop at an x-position < IT->first_visible_x
14459 if the first glyph is partially visible or if we hit a line end. */
14460 if (it->current_x < it->first_visible_x)
14461 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14462 MOVE_TO_POS | MOVE_TO_X);
14463
14464 /* Get the initial row height. This is either the height of the
14465 text hscrolled, if there is any, or zero. */
14466 row->ascent = it->max_ascent;
14467 row->height = it->max_ascent + it->max_descent;
14468 row->phys_ascent = it->max_phys_ascent;
14469 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14470
14471 /* Loop generating characters. The loop is left with IT on the next
14472 character to display. */
14473 while (1)
14474 {
14475 int n_glyphs_before, hpos_before, x_before;
14476 int x, i, nglyphs;
14477 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14478
14479 /* Retrieve the next thing to display. Value is zero if end of
14480 buffer reached. */
14481 if (!get_next_display_element (it))
14482 {
14483 /* Maybe add a space at the end of this line that is used to
14484 display the cursor there under X. Set the charpos of the
14485 first glyph of blank lines not corresponding to any text
14486 to -1. */
14487 #ifdef HAVE_WINDOW_SYSTEM
14488 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14489 row->exact_window_width_line_p = 1;
14490 else
14491 #endif /* HAVE_WINDOW_SYSTEM */
14492 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14493 || row->used[TEXT_AREA] == 0)
14494 {
14495 row->glyphs[TEXT_AREA]->charpos = -1;
14496 row->displays_text_p = 0;
14497
14498 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14499 && (!MINI_WINDOW_P (it->w)
14500 || (minibuf_level && EQ (it->window, minibuf_window))))
14501 row->indicate_empty_line_p = 1;
14502 }
14503
14504 it->continuation_lines_width = 0;
14505 row->ends_at_zv_p = 1;
14506 break;
14507 }
14508
14509 /* Now, get the metrics of what we want to display. This also
14510 generates glyphs in `row' (which is IT->glyph_row). */
14511 n_glyphs_before = row->used[TEXT_AREA];
14512 x = it->current_x;
14513
14514 /* Remember the line height so far in case the next element doesn't
14515 fit on the line. */
14516 if (!it->truncate_lines_p)
14517 {
14518 ascent = it->max_ascent;
14519 descent = it->max_descent;
14520 phys_ascent = it->max_phys_ascent;
14521 phys_descent = it->max_phys_descent;
14522 }
14523
14524 PRODUCE_GLYPHS (it);
14525
14526 /* If this display element was in marginal areas, continue with
14527 the next one. */
14528 if (it->area != TEXT_AREA)
14529 {
14530 row->ascent = max (row->ascent, it->max_ascent);
14531 row->height = max (row->height, it->max_ascent + it->max_descent);
14532 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14533 row->phys_height = max (row->phys_height,
14534 it->max_phys_ascent + it->max_phys_descent);
14535 set_iterator_to_next (it, 1);
14536 continue;
14537 }
14538
14539 /* Does the display element fit on the line? If we truncate
14540 lines, we should draw past the right edge of the window. If
14541 we don't truncate, we want to stop so that we can display the
14542 continuation glyph before the right margin. If lines are
14543 continued, there are two possible strategies for characters
14544 resulting in more than 1 glyph (e.g. tabs): Display as many
14545 glyphs as possible in this line and leave the rest for the
14546 continuation line, or display the whole element in the next
14547 line. Original redisplay did the former, so we do it also. */
14548 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14549 hpos_before = it->hpos;
14550 x_before = x;
14551
14552 if (/* Not a newline. */
14553 nglyphs > 0
14554 /* Glyphs produced fit entirely in the line. */
14555 && it->current_x < it->last_visible_x)
14556 {
14557 it->hpos += nglyphs;
14558 row->ascent = max (row->ascent, it->max_ascent);
14559 row->height = max (row->height, it->max_ascent + it->max_descent);
14560 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14561 row->phys_height = max (row->phys_height,
14562 it->max_phys_ascent + it->max_phys_descent);
14563 if (it->current_x - it->pixel_width < it->first_visible_x)
14564 row->x = x - it->first_visible_x;
14565 }
14566 else
14567 {
14568 int new_x;
14569 struct glyph *glyph;
14570
14571 for (i = 0; i < nglyphs; ++i, x = new_x)
14572 {
14573 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14574 new_x = x + glyph->pixel_width;
14575
14576 if (/* Lines are continued. */
14577 !it->truncate_lines_p
14578 && (/* Glyph doesn't fit on the line. */
14579 new_x > it->last_visible_x
14580 /* Or it fits exactly on a window system frame. */
14581 || (new_x == it->last_visible_x
14582 && FRAME_WINDOW_P (it->f))))
14583 {
14584 /* End of a continued line. */
14585
14586 if (it->hpos == 0
14587 || (new_x == it->last_visible_x
14588 && FRAME_WINDOW_P (it->f)))
14589 {
14590 /* Current glyph is the only one on the line or
14591 fits exactly on the line. We must continue
14592 the line because we can't draw the cursor
14593 after the glyph. */
14594 row->continued_p = 1;
14595 it->current_x = new_x;
14596 it->continuation_lines_width += new_x;
14597 ++it->hpos;
14598 if (i == nglyphs - 1)
14599 {
14600 set_iterator_to_next (it, 1);
14601 #ifdef HAVE_WINDOW_SYSTEM
14602 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14603 {
14604 if (!get_next_display_element (it))
14605 {
14606 row->exact_window_width_line_p = 1;
14607 it->continuation_lines_width = 0;
14608 row->continued_p = 0;
14609 row->ends_at_zv_p = 1;
14610 }
14611 else if (ITERATOR_AT_END_OF_LINE_P (it))
14612 {
14613 row->continued_p = 0;
14614 row->exact_window_width_line_p = 1;
14615 }
14616 }
14617 #endif /* HAVE_WINDOW_SYSTEM */
14618 }
14619 }
14620 else if (CHAR_GLYPH_PADDING_P (*glyph)
14621 && !FRAME_WINDOW_P (it->f))
14622 {
14623 /* A padding glyph that doesn't fit on this line.
14624 This means the whole character doesn't fit
14625 on the line. */
14626 row->used[TEXT_AREA] = n_glyphs_before;
14627
14628 /* Fill the rest of the row with continuation
14629 glyphs like in 20.x. */
14630 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14631 < row->glyphs[1 + TEXT_AREA])
14632 produce_special_glyphs (it, IT_CONTINUATION);
14633
14634 row->continued_p = 1;
14635 it->current_x = x_before;
14636 it->continuation_lines_width += x_before;
14637
14638 /* Restore the height to what it was before the
14639 element not fitting on the line. */
14640 it->max_ascent = ascent;
14641 it->max_descent = descent;
14642 it->max_phys_ascent = phys_ascent;
14643 it->max_phys_descent = phys_descent;
14644 }
14645 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14646 {
14647 /* A TAB that extends past the right edge of the
14648 window. This produces a single glyph on
14649 window system frames. We leave the glyph in
14650 this row and let it fill the row, but don't
14651 consume the TAB. */
14652 it->continuation_lines_width += it->last_visible_x;
14653 row->ends_in_middle_of_char_p = 1;
14654 row->continued_p = 1;
14655 glyph->pixel_width = it->last_visible_x - x;
14656 it->starts_in_middle_of_char_p = 1;
14657 }
14658 else
14659 {
14660 /* Something other than a TAB that draws past
14661 the right edge of the window. Restore
14662 positions to values before the element. */
14663 row->used[TEXT_AREA] = n_glyphs_before + i;
14664
14665 /* Display continuation glyphs. */
14666 if (!FRAME_WINDOW_P (it->f))
14667 produce_special_glyphs (it, IT_CONTINUATION);
14668 row->continued_p = 1;
14669
14670 it->continuation_lines_width += x;
14671
14672 if (nglyphs > 1 && i > 0)
14673 {
14674 row->ends_in_middle_of_char_p = 1;
14675 it->starts_in_middle_of_char_p = 1;
14676 }
14677
14678 /* Restore the height to what it was before the
14679 element not fitting on the line. */
14680 it->max_ascent = ascent;
14681 it->max_descent = descent;
14682 it->max_phys_ascent = phys_ascent;
14683 it->max_phys_descent = phys_descent;
14684 }
14685
14686 break;
14687 }
14688 else if (new_x > it->first_visible_x)
14689 {
14690 /* Increment number of glyphs actually displayed. */
14691 ++it->hpos;
14692
14693 if (x < it->first_visible_x)
14694 /* Glyph is partially visible, i.e. row starts at
14695 negative X position. */
14696 row->x = x - it->first_visible_x;
14697 }
14698 else
14699 {
14700 /* Glyph is completely off the left margin of the
14701 window. This should not happen because of the
14702 move_it_in_display_line at the start of this
14703 function, unless the text display area of the
14704 window is empty. */
14705 xassert (it->first_visible_x <= it->last_visible_x);
14706 }
14707 }
14708
14709 row->ascent = max (row->ascent, it->max_ascent);
14710 row->height = max (row->height, it->max_ascent + it->max_descent);
14711 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14712 row->phys_height = max (row->phys_height,
14713 it->max_phys_ascent + it->max_phys_descent);
14714
14715 /* End of this display line if row is continued. */
14716 if (row->continued_p || row->ends_at_zv_p)
14717 break;
14718 }
14719
14720 at_end_of_line:
14721 /* Is this a line end? If yes, we're also done, after making
14722 sure that a non-default face is extended up to the right
14723 margin of the window. */
14724 if (ITERATOR_AT_END_OF_LINE_P (it))
14725 {
14726 int used_before = row->used[TEXT_AREA];
14727
14728 row->ends_in_newline_from_string_p = STRINGP (it->object);
14729
14730 #ifdef HAVE_WINDOW_SYSTEM
14731 /* Add a space at the end of the line that is used to
14732 display the cursor there. */
14733 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14734 append_space_for_newline (it, 0);
14735 #endif /* HAVE_WINDOW_SYSTEM */
14736
14737 /* Extend the face to the end of the line. */
14738 extend_face_to_end_of_line (it);
14739
14740 /* Make sure we have the position. */
14741 if (used_before == 0)
14742 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14743
14744 /* Consume the line end. This skips over invisible lines. */
14745 set_iterator_to_next (it, 1);
14746 it->continuation_lines_width = 0;
14747 break;
14748 }
14749
14750 /* Proceed with next display element. Note that this skips
14751 over lines invisible because of selective display. */
14752 set_iterator_to_next (it, 1);
14753
14754 /* If we truncate lines, we are done when the last displayed
14755 glyphs reach past the right margin of the window. */
14756 if (it->truncate_lines_p
14757 && (FRAME_WINDOW_P (it->f)
14758 ? (it->current_x >= it->last_visible_x)
14759 : (it->current_x > it->last_visible_x)))
14760 {
14761 /* Maybe add truncation glyphs. */
14762 if (!FRAME_WINDOW_P (it->f))
14763 {
14764 int i, n;
14765
14766 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14767 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14768 break;
14769
14770 for (n = row->used[TEXT_AREA]; i < n; ++i)
14771 {
14772 row->used[TEXT_AREA] = i;
14773 produce_special_glyphs (it, IT_TRUNCATION);
14774 }
14775 }
14776 #ifdef HAVE_WINDOW_SYSTEM
14777 else
14778 {
14779 /* Don't truncate if we can overflow newline into fringe. */
14780 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14781 {
14782 if (!get_next_display_element (it))
14783 {
14784 #ifdef HAVE_WINDOW_SYSTEM
14785 it->continuation_lines_width = 0;
14786 row->ends_at_zv_p = 1;
14787 row->exact_window_width_line_p = 1;
14788 break;
14789 #endif /* HAVE_WINDOW_SYSTEM */
14790 }
14791 if (ITERATOR_AT_END_OF_LINE_P (it))
14792 {
14793 row->exact_window_width_line_p = 1;
14794 goto at_end_of_line;
14795 }
14796 }
14797 }
14798 #endif /* HAVE_WINDOW_SYSTEM */
14799
14800 row->truncated_on_right_p = 1;
14801 it->continuation_lines_width = 0;
14802 reseat_at_next_visible_line_start (it, 0);
14803 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14804 it->hpos = hpos_before;
14805 it->current_x = x_before;
14806 break;
14807 }
14808 }
14809
14810 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14811 at the left window margin. */
14812 if (it->first_visible_x
14813 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14814 {
14815 if (!FRAME_WINDOW_P (it->f))
14816 insert_left_trunc_glyphs (it);
14817 row->truncated_on_left_p = 1;
14818 }
14819
14820 /* If the start of this line is the overlay arrow-position, then
14821 mark this glyph row as the one containing the overlay arrow.
14822 This is clearly a mess with variable size fonts. It would be
14823 better to let it be displayed like cursors under X. */
14824 if (! overlay_arrow_seen
14825 && (overlay_arrow_string
14826 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14827 !NILP (overlay_arrow_string)))
14828 {
14829 /* Overlay arrow in window redisplay is a fringe bitmap. */
14830 if (!FRAME_WINDOW_P (it->f))
14831 {
14832 struct glyph_row *arrow_row
14833 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14834 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14835 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14836 struct glyph *p = row->glyphs[TEXT_AREA];
14837 struct glyph *p2, *end;
14838
14839 /* Copy the arrow glyphs. */
14840 while (glyph < arrow_end)
14841 *p++ = *glyph++;
14842
14843 /* Throw away padding glyphs. */
14844 p2 = p;
14845 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14846 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14847 ++p2;
14848 if (p2 > p)
14849 {
14850 while (p2 < end)
14851 *p++ = *p2++;
14852 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14853 }
14854 }
14855
14856 overlay_arrow_seen = 1;
14857 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14858 row->overlay_arrow_p = 1;
14859 }
14860
14861 /* Compute pixel dimensions of this line. */
14862 compute_line_metrics (it);
14863
14864 /* Remember the position at which this line ends. */
14865 row->end = it->current;
14866
14867 /* Save fringe bitmaps in this row. */
14868 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14869 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14870 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14871 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14872
14873 it->left_user_fringe_bitmap = 0;
14874 it->left_user_fringe_face_id = 0;
14875 it->right_user_fringe_bitmap = 0;
14876 it->right_user_fringe_face_id = 0;
14877
14878 /* Maybe set the cursor. */
14879 if (it->w->cursor.vpos < 0
14880 && PT >= MATRIX_ROW_START_CHARPOS (row)
14881 && PT <= MATRIX_ROW_END_CHARPOS (row)
14882 && cursor_row_p (it->w, row))
14883 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14884
14885 /* Highlight trailing whitespace. */
14886 if (!NILP (Vshow_trailing_whitespace))
14887 highlight_trailing_whitespace (it->f, it->glyph_row);
14888
14889 /* Prepare for the next line. This line starts horizontally at (X
14890 HPOS) = (0 0). Vertical positions are incremented. As a
14891 convenience for the caller, IT->glyph_row is set to the next
14892 row to be used. */
14893 it->current_x = it->hpos = 0;
14894 it->current_y += row->height;
14895 ++it->vpos;
14896 ++it->glyph_row;
14897 it->start = it->current;
14898 return row->displays_text_p;
14899 }
14900
14901
14902 \f
14903 /***********************************************************************
14904 Menu Bar
14905 ***********************************************************************/
14906
14907 /* Redisplay the menu bar in the frame for window W.
14908
14909 The menu bar of X frames that don't have X toolkit support is
14910 displayed in a special window W->frame->menu_bar_window.
14911
14912 The menu bar of terminal frames is treated specially as far as
14913 glyph matrices are concerned. Menu bar lines are not part of
14914 windows, so the update is done directly on the frame matrix rows
14915 for the menu bar. */
14916
14917 static void
14918 display_menu_bar (w)
14919 struct window *w;
14920 {
14921 struct frame *f = XFRAME (WINDOW_FRAME (w));
14922 struct it it;
14923 Lisp_Object items;
14924 int i;
14925
14926 /* Don't do all this for graphical frames. */
14927 #ifdef HAVE_NTGUI
14928 if (!NILP (Vwindow_system))
14929 return;
14930 #endif
14931 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14932 if (FRAME_X_P (f))
14933 return;
14934 #endif
14935 #ifdef MAC_OS
14936 if (FRAME_MAC_P (f))
14937 return;
14938 #endif
14939
14940 #ifdef USE_X_TOOLKIT
14941 xassert (!FRAME_WINDOW_P (f));
14942 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14943 it.first_visible_x = 0;
14944 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14945 #else /* not USE_X_TOOLKIT */
14946 if (FRAME_WINDOW_P (f))
14947 {
14948 /* Menu bar lines are displayed in the desired matrix of the
14949 dummy window menu_bar_window. */
14950 struct window *menu_w;
14951 xassert (WINDOWP (f->menu_bar_window));
14952 menu_w = XWINDOW (f->menu_bar_window);
14953 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14954 MENU_FACE_ID);
14955 it.first_visible_x = 0;
14956 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14957 }
14958 else
14959 {
14960 /* This is a TTY frame, i.e. character hpos/vpos are used as
14961 pixel x/y. */
14962 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14963 MENU_FACE_ID);
14964 it.first_visible_x = 0;
14965 it.last_visible_x = FRAME_COLS (f);
14966 }
14967 #endif /* not USE_X_TOOLKIT */
14968
14969 if (! mode_line_inverse_video)
14970 /* Force the menu-bar to be displayed in the default face. */
14971 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14972
14973 /* Clear all rows of the menu bar. */
14974 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14975 {
14976 struct glyph_row *row = it.glyph_row + i;
14977 clear_glyph_row (row);
14978 row->enabled_p = 1;
14979 row->full_width_p = 1;
14980 }
14981
14982 /* Display all items of the menu bar. */
14983 items = FRAME_MENU_BAR_ITEMS (it.f);
14984 for (i = 0; i < XVECTOR (items)->size; i += 4)
14985 {
14986 Lisp_Object string;
14987
14988 /* Stop at nil string. */
14989 string = AREF (items, i + 1);
14990 if (NILP (string))
14991 break;
14992
14993 /* Remember where item was displayed. */
14994 AREF (items, i + 3) = make_number (it.hpos);
14995
14996 /* Display the item, pad with one space. */
14997 if (it.current_x < it.last_visible_x)
14998 display_string (NULL, string, Qnil, 0, 0, &it,
14999 SCHARS (string) + 1, 0, 0, -1);
15000 }
15001
15002 /* Fill out the line with spaces. */
15003 if (it.current_x < it.last_visible_x)
15004 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15005
15006 /* Compute the total height of the lines. */
15007 compute_line_metrics (&it);
15008 }
15009
15010
15011 \f
15012 /***********************************************************************
15013 Mode Line
15014 ***********************************************************************/
15015
15016 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15017 FORCE is non-zero, redisplay mode lines unconditionally.
15018 Otherwise, redisplay only mode lines that are garbaged. Value is
15019 the number of windows whose mode lines were redisplayed. */
15020
15021 static int
15022 redisplay_mode_lines (window, force)
15023 Lisp_Object window;
15024 int force;
15025 {
15026 int nwindows = 0;
15027
15028 while (!NILP (window))
15029 {
15030 struct window *w = XWINDOW (window);
15031
15032 if (WINDOWP (w->hchild))
15033 nwindows += redisplay_mode_lines (w->hchild, force);
15034 else if (WINDOWP (w->vchild))
15035 nwindows += redisplay_mode_lines (w->vchild, force);
15036 else if (force
15037 || FRAME_GARBAGED_P (XFRAME (w->frame))
15038 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15039 {
15040 struct text_pos lpoint;
15041 struct buffer *old = current_buffer;
15042
15043 /* Set the window's buffer for the mode line display. */
15044 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15045 set_buffer_internal_1 (XBUFFER (w->buffer));
15046
15047 /* Point refers normally to the selected window. For any
15048 other window, set up appropriate value. */
15049 if (!EQ (window, selected_window))
15050 {
15051 struct text_pos pt;
15052
15053 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15054 if (CHARPOS (pt) < BEGV)
15055 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15056 else if (CHARPOS (pt) > (ZV - 1))
15057 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15058 else
15059 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15060 }
15061
15062 /* Display mode lines. */
15063 clear_glyph_matrix (w->desired_matrix);
15064 if (display_mode_lines (w))
15065 {
15066 ++nwindows;
15067 w->must_be_updated_p = 1;
15068 }
15069
15070 /* Restore old settings. */
15071 set_buffer_internal_1 (old);
15072 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15073 }
15074
15075 window = w->next;
15076 }
15077
15078 return nwindows;
15079 }
15080
15081
15082 /* Display the mode and/or top line of window W. Value is the number
15083 of mode lines displayed. */
15084
15085 static int
15086 display_mode_lines (w)
15087 struct window *w;
15088 {
15089 Lisp_Object old_selected_window, old_selected_frame;
15090 int n = 0;
15091
15092 old_selected_frame = selected_frame;
15093 selected_frame = w->frame;
15094 old_selected_window = selected_window;
15095 XSETWINDOW (selected_window, w);
15096
15097 /* These will be set while the mode line specs are processed. */
15098 line_number_displayed = 0;
15099 w->column_number_displayed = Qnil;
15100
15101 if (WINDOW_WANTS_MODELINE_P (w))
15102 {
15103 struct window *sel_w = XWINDOW (old_selected_window);
15104
15105 /* Select mode line face based on the real selected window. */
15106 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15107 current_buffer->mode_line_format);
15108 ++n;
15109 }
15110
15111 if (WINDOW_WANTS_HEADER_LINE_P (w))
15112 {
15113 display_mode_line (w, HEADER_LINE_FACE_ID,
15114 current_buffer->header_line_format);
15115 ++n;
15116 }
15117
15118 selected_frame = old_selected_frame;
15119 selected_window = old_selected_window;
15120 return n;
15121 }
15122
15123
15124 /* Display mode or top line of window W. FACE_ID specifies which line
15125 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15126 FORMAT is the mode line format to display. Value is the pixel
15127 height of the mode line displayed. */
15128
15129 static int
15130 display_mode_line (w, face_id, format)
15131 struct window *w;
15132 enum face_id face_id;
15133 Lisp_Object format;
15134 {
15135 struct it it;
15136 struct face *face;
15137
15138 init_iterator (&it, w, -1, -1, NULL, face_id);
15139 prepare_desired_row (it.glyph_row);
15140
15141 it.glyph_row->mode_line_p = 1;
15142
15143 if (! mode_line_inverse_video)
15144 /* Force the mode-line to be displayed in the default face. */
15145 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15146
15147 /* Temporarily make frame's keyboard the current kboard so that
15148 kboard-local variables in the mode_line_format will get the right
15149 values. */
15150 push_frame_kboard (it.f);
15151 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15152 pop_frame_kboard ();
15153
15154 /* Fill up with spaces. */
15155 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15156
15157 compute_line_metrics (&it);
15158 it.glyph_row->full_width_p = 1;
15159 it.glyph_row->continued_p = 0;
15160 it.glyph_row->truncated_on_left_p = 0;
15161 it.glyph_row->truncated_on_right_p = 0;
15162
15163 /* Make a 3D mode-line have a shadow at its right end. */
15164 face = FACE_FROM_ID (it.f, face_id);
15165 extend_face_to_end_of_line (&it);
15166 if (face->box != FACE_NO_BOX)
15167 {
15168 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15169 + it.glyph_row->used[TEXT_AREA] - 1);
15170 last->right_box_line_p = 1;
15171 }
15172
15173 return it.glyph_row->height;
15174 }
15175
15176 /* Alist that caches the results of :propertize.
15177 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15178 Lisp_Object mode_line_proptrans_alist;
15179
15180 /* List of strings making up the mode-line. */
15181 Lisp_Object mode_line_string_list;
15182
15183 /* Base face property when building propertized mode line string. */
15184 static Lisp_Object mode_line_string_face;
15185 static Lisp_Object mode_line_string_face_prop;
15186
15187
15188 /* Contribute ELT to the mode line for window IT->w. How it
15189 translates into text depends on its data type.
15190
15191 IT describes the display environment in which we display, as usual.
15192
15193 DEPTH is the depth in recursion. It is used to prevent
15194 infinite recursion here.
15195
15196 FIELD_WIDTH is the number of characters the display of ELT should
15197 occupy in the mode line, and PRECISION is the maximum number of
15198 characters to display from ELT's representation. See
15199 display_string for details.
15200
15201 Returns the hpos of the end of the text generated by ELT.
15202
15203 PROPS is a property list to add to any string we encounter.
15204
15205 If RISKY is nonzero, remove (disregard) any properties in any string
15206 we encounter, and ignore :eval and :propertize.
15207
15208 If the global variable `frame_title_ptr' is non-NULL, then the output
15209 is passed to `store_frame_title' instead of `display_string'. */
15210
15211 static int
15212 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15213 struct it *it;
15214 int depth;
15215 int field_width, precision;
15216 Lisp_Object elt, props;
15217 int risky;
15218 {
15219 int n = 0, field, prec;
15220 int literal = 0;
15221
15222 tail_recurse:
15223 if (depth > 100)
15224 elt = build_string ("*too-deep*");
15225
15226 depth++;
15227
15228 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15229 {
15230 case Lisp_String:
15231 {
15232 /* A string: output it and check for %-constructs within it. */
15233 unsigned char c;
15234 const unsigned char *this, *lisp_string;
15235
15236 if (!NILP (props) || risky)
15237 {
15238 Lisp_Object oprops, aelt;
15239 oprops = Ftext_properties_at (make_number (0), elt);
15240
15241 if (NILP (Fequal (props, oprops)) || risky)
15242 {
15243 /* If the starting string has properties,
15244 merge the specified ones onto the existing ones. */
15245 if (! NILP (oprops) && !risky)
15246 {
15247 Lisp_Object tem;
15248
15249 oprops = Fcopy_sequence (oprops);
15250 tem = props;
15251 while (CONSP (tem))
15252 {
15253 oprops = Fplist_put (oprops, XCAR (tem),
15254 XCAR (XCDR (tem)));
15255 tem = XCDR (XCDR (tem));
15256 }
15257 props = oprops;
15258 }
15259
15260 aelt = Fassoc (elt, mode_line_proptrans_alist);
15261 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15262 {
15263 mode_line_proptrans_alist
15264 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15265 elt = XCAR (aelt);
15266 }
15267 else
15268 {
15269 Lisp_Object tem;
15270
15271 elt = Fcopy_sequence (elt);
15272 Fset_text_properties (make_number (0), Flength (elt),
15273 props, elt);
15274 /* Add this item to mode_line_proptrans_alist. */
15275 mode_line_proptrans_alist
15276 = Fcons (Fcons (elt, props),
15277 mode_line_proptrans_alist);
15278 /* Truncate mode_line_proptrans_alist
15279 to at most 50 elements. */
15280 tem = Fnthcdr (make_number (50),
15281 mode_line_proptrans_alist);
15282 if (! NILP (tem))
15283 XSETCDR (tem, Qnil);
15284 }
15285 }
15286 }
15287
15288 this = SDATA (elt);
15289 lisp_string = this;
15290
15291 if (literal)
15292 {
15293 prec = precision - n;
15294 if (frame_title_ptr)
15295 n += store_frame_title (SDATA (elt), -1, prec);
15296 else if (!NILP (mode_line_string_list))
15297 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15298 else
15299 n += display_string (NULL, elt, Qnil, 0, 0, it,
15300 0, prec, 0, STRING_MULTIBYTE (elt));
15301
15302 break;
15303 }
15304
15305 while ((precision <= 0 || n < precision)
15306 && *this
15307 && (frame_title_ptr
15308 || !NILP (mode_line_string_list)
15309 || it->current_x < it->last_visible_x))
15310 {
15311 const unsigned char *last = this;
15312
15313 /* Advance to end of string or next format specifier. */
15314 while ((c = *this++) != '\0' && c != '%')
15315 ;
15316
15317 if (this - 1 != last)
15318 {
15319 /* Output to end of string or up to '%'. Field width
15320 is length of string. Don't output more than
15321 PRECISION allows us. */
15322 --this;
15323
15324 prec = chars_in_text (last, this - last);
15325 if (precision > 0 && prec > precision - n)
15326 prec = precision - n;
15327
15328 if (frame_title_ptr)
15329 n += store_frame_title (last, 0, prec);
15330 else if (!NILP (mode_line_string_list))
15331 {
15332 int bytepos = last - lisp_string;
15333 int charpos = string_byte_to_char (elt, bytepos);
15334 n += store_mode_line_string (NULL,
15335 Fsubstring (elt, make_number (charpos),
15336 make_number (charpos + prec)),
15337 0, 0, 0, Qnil);
15338 }
15339 else
15340 {
15341 int bytepos = last - lisp_string;
15342 int charpos = string_byte_to_char (elt, bytepos);
15343 n += display_string (NULL, elt, Qnil, 0, charpos,
15344 it, 0, prec, 0,
15345 STRING_MULTIBYTE (elt));
15346 }
15347 }
15348 else /* c == '%' */
15349 {
15350 const unsigned char *percent_position = this;
15351
15352 /* Get the specified minimum width. Zero means
15353 don't pad. */
15354 field = 0;
15355 while ((c = *this++) >= '0' && c <= '9')
15356 field = field * 10 + c - '0';
15357
15358 /* Don't pad beyond the total padding allowed. */
15359 if (field_width - n > 0 && field > field_width - n)
15360 field = field_width - n;
15361
15362 /* Note that either PRECISION <= 0 or N < PRECISION. */
15363 prec = precision - n;
15364
15365 if (c == 'M')
15366 n += display_mode_element (it, depth, field, prec,
15367 Vglobal_mode_string, props,
15368 risky);
15369 else if (c != 0)
15370 {
15371 int multibyte;
15372 int bytepos, charpos;
15373 unsigned char *spec;
15374
15375 bytepos = percent_position - lisp_string;
15376 charpos = (STRING_MULTIBYTE (elt)
15377 ? string_byte_to_char (elt, bytepos)
15378 : bytepos);
15379
15380 spec
15381 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15382
15383 if (frame_title_ptr)
15384 n += store_frame_title (spec, field, prec);
15385 else if (!NILP (mode_line_string_list))
15386 {
15387 int len = strlen (spec);
15388 Lisp_Object tem = make_string (spec, len);
15389 props = Ftext_properties_at (make_number (charpos), elt);
15390 /* Should only keep face property in props */
15391 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15392 }
15393 else
15394 {
15395 int nglyphs_before, nwritten;
15396
15397 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15398 nwritten = display_string (spec, Qnil, elt,
15399 charpos, 0, it,
15400 field, prec, 0,
15401 multibyte);
15402
15403 /* Assign to the glyphs written above the
15404 string where the `%x' came from, position
15405 of the `%'. */
15406 if (nwritten > 0)
15407 {
15408 struct glyph *glyph
15409 = (it->glyph_row->glyphs[TEXT_AREA]
15410 + nglyphs_before);
15411 int i;
15412
15413 for (i = 0; i < nwritten; ++i)
15414 {
15415 glyph[i].object = elt;
15416 glyph[i].charpos = charpos;
15417 }
15418
15419 n += nwritten;
15420 }
15421 }
15422 }
15423 else /* c == 0 */
15424 break;
15425 }
15426 }
15427 }
15428 break;
15429
15430 case Lisp_Symbol:
15431 /* A symbol: process the value of the symbol recursively
15432 as if it appeared here directly. Avoid error if symbol void.
15433 Special case: if value of symbol is a string, output the string
15434 literally. */
15435 {
15436 register Lisp_Object tem;
15437
15438 /* If the variable is not marked as risky to set
15439 then its contents are risky to use. */
15440 if (NILP (Fget (elt, Qrisky_local_variable)))
15441 risky = 1;
15442
15443 tem = Fboundp (elt);
15444 if (!NILP (tem))
15445 {
15446 tem = Fsymbol_value (elt);
15447 /* If value is a string, output that string literally:
15448 don't check for % within it. */
15449 if (STRINGP (tem))
15450 literal = 1;
15451
15452 if (!EQ (tem, elt))
15453 {
15454 /* Give up right away for nil or t. */
15455 elt = tem;
15456 goto tail_recurse;
15457 }
15458 }
15459 }
15460 break;
15461
15462 case Lisp_Cons:
15463 {
15464 register Lisp_Object car, tem;
15465
15466 /* A cons cell: five distinct cases.
15467 If first element is :eval or :propertize, do something special.
15468 If first element is a string or a cons, process all the elements
15469 and effectively concatenate them.
15470 If first element is a negative number, truncate displaying cdr to
15471 at most that many characters. If positive, pad (with spaces)
15472 to at least that many characters.
15473 If first element is a symbol, process the cadr or caddr recursively
15474 according to whether the symbol's value is non-nil or nil. */
15475 car = XCAR (elt);
15476 if (EQ (car, QCeval))
15477 {
15478 /* An element of the form (:eval FORM) means evaluate FORM
15479 and use the result as mode line elements. */
15480
15481 if (risky)
15482 break;
15483
15484 if (CONSP (XCDR (elt)))
15485 {
15486 Lisp_Object spec;
15487 spec = safe_eval (XCAR (XCDR (elt)));
15488 n += display_mode_element (it, depth, field_width - n,
15489 precision - n, spec, props,
15490 risky);
15491 }
15492 }
15493 else if (EQ (car, QCpropertize))
15494 {
15495 /* An element of the form (:propertize ELT PROPS...)
15496 means display ELT but applying properties PROPS. */
15497
15498 if (risky)
15499 break;
15500
15501 if (CONSP (XCDR (elt)))
15502 n += display_mode_element (it, depth, field_width - n,
15503 precision - n, XCAR (XCDR (elt)),
15504 XCDR (XCDR (elt)), risky);
15505 }
15506 else if (SYMBOLP (car))
15507 {
15508 tem = Fboundp (car);
15509 elt = XCDR (elt);
15510 if (!CONSP (elt))
15511 goto invalid;
15512 /* elt is now the cdr, and we know it is a cons cell.
15513 Use its car if CAR has a non-nil value. */
15514 if (!NILP (tem))
15515 {
15516 tem = Fsymbol_value (car);
15517 if (!NILP (tem))
15518 {
15519 elt = XCAR (elt);
15520 goto tail_recurse;
15521 }
15522 }
15523 /* Symbol's value is nil (or symbol is unbound)
15524 Get the cddr of the original list
15525 and if possible find the caddr and use that. */
15526 elt = XCDR (elt);
15527 if (NILP (elt))
15528 break;
15529 else if (!CONSP (elt))
15530 goto invalid;
15531 elt = XCAR (elt);
15532 goto tail_recurse;
15533 }
15534 else if (INTEGERP (car))
15535 {
15536 register int lim = XINT (car);
15537 elt = XCDR (elt);
15538 if (lim < 0)
15539 {
15540 /* Negative int means reduce maximum width. */
15541 if (precision <= 0)
15542 precision = -lim;
15543 else
15544 precision = min (precision, -lim);
15545 }
15546 else if (lim > 0)
15547 {
15548 /* Padding specified. Don't let it be more than
15549 current maximum. */
15550 if (precision > 0)
15551 lim = min (precision, lim);
15552
15553 /* If that's more padding than already wanted, queue it.
15554 But don't reduce padding already specified even if
15555 that is beyond the current truncation point. */
15556 field_width = max (lim, field_width);
15557 }
15558 goto tail_recurse;
15559 }
15560 else if (STRINGP (car) || CONSP (car))
15561 {
15562 register int limit = 50;
15563 /* Limit is to protect against circular lists. */
15564 while (CONSP (elt)
15565 && --limit > 0
15566 && (precision <= 0 || n < precision))
15567 {
15568 n += display_mode_element (it, depth, field_width - n,
15569 precision - n, XCAR (elt),
15570 props, risky);
15571 elt = XCDR (elt);
15572 }
15573 }
15574 }
15575 break;
15576
15577 default:
15578 invalid:
15579 elt = build_string ("*invalid*");
15580 goto tail_recurse;
15581 }
15582
15583 /* Pad to FIELD_WIDTH. */
15584 if (field_width > 0 && n < field_width)
15585 {
15586 if (frame_title_ptr)
15587 n += store_frame_title ("", field_width - n, 0);
15588 else if (!NILP (mode_line_string_list))
15589 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15590 else
15591 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15592 0, 0, 0);
15593 }
15594
15595 return n;
15596 }
15597
15598 /* Store a mode-line string element in mode_line_string_list.
15599
15600 If STRING is non-null, display that C string. Otherwise, the Lisp
15601 string LISP_STRING is displayed.
15602
15603 FIELD_WIDTH is the minimum number of output glyphs to produce.
15604 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15605 with spaces. FIELD_WIDTH <= 0 means don't pad.
15606
15607 PRECISION is the maximum number of characters to output from
15608 STRING. PRECISION <= 0 means don't truncate the string.
15609
15610 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15611 properties to the string.
15612
15613 PROPS are the properties to add to the string.
15614 The mode_line_string_face face property is always added to the string.
15615 */
15616
15617 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15618 char *string;
15619 Lisp_Object lisp_string;
15620 int copy_string;
15621 int field_width;
15622 int precision;
15623 Lisp_Object props;
15624 {
15625 int len;
15626 int n = 0;
15627
15628 if (string != NULL)
15629 {
15630 len = strlen (string);
15631 if (precision > 0 && len > precision)
15632 len = precision;
15633 lisp_string = make_string (string, len);
15634 if (NILP (props))
15635 props = mode_line_string_face_prop;
15636 else if (!NILP (mode_line_string_face))
15637 {
15638 Lisp_Object face = Fplist_get (props, Qface);
15639 props = Fcopy_sequence (props);
15640 if (NILP (face))
15641 face = mode_line_string_face;
15642 else
15643 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15644 props = Fplist_put (props, Qface, face);
15645 }
15646 Fadd_text_properties (make_number (0), make_number (len),
15647 props, lisp_string);
15648 }
15649 else
15650 {
15651 len = XFASTINT (Flength (lisp_string));
15652 if (precision > 0 && len > precision)
15653 {
15654 len = precision;
15655 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15656 precision = -1;
15657 }
15658 if (!NILP (mode_line_string_face))
15659 {
15660 Lisp_Object face;
15661 if (NILP (props))
15662 props = Ftext_properties_at (make_number (0), lisp_string);
15663 face = Fplist_get (props, Qface);
15664 if (NILP (face))
15665 face = mode_line_string_face;
15666 else
15667 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15668 props = Fcons (Qface, Fcons (face, Qnil));
15669 if (copy_string)
15670 lisp_string = Fcopy_sequence (lisp_string);
15671 }
15672 if (!NILP (props))
15673 Fadd_text_properties (make_number (0), make_number (len),
15674 props, lisp_string);
15675 }
15676
15677 if (len > 0)
15678 {
15679 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15680 n += len;
15681 }
15682
15683 if (field_width > len)
15684 {
15685 field_width -= len;
15686 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15687 if (!NILP (props))
15688 Fadd_text_properties (make_number (0), make_number (field_width),
15689 props, lisp_string);
15690 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15691 n += field_width;
15692 }
15693
15694 return n;
15695 }
15696
15697
15698 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15699 0, 3, 0,
15700 doc: /* Return the mode-line of selected window as a string.
15701 First optional arg FORMAT specifies a different format string (see
15702 `mode-line-format' for details) to use. If FORMAT is t, return
15703 the buffer's header-line. Second optional arg WINDOW specifies a
15704 different window to use as the context for the formatting.
15705 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15706 (format, window, no_props)
15707 Lisp_Object format, window, no_props;
15708 {
15709 struct it it;
15710 int len;
15711 struct window *w;
15712 struct buffer *old_buffer = NULL;
15713 enum face_id face_id = DEFAULT_FACE_ID;
15714
15715 if (NILP (window))
15716 window = selected_window;
15717 CHECK_WINDOW (window);
15718 w = XWINDOW (window);
15719 CHECK_BUFFER (w->buffer);
15720
15721 if (XBUFFER (w->buffer) != current_buffer)
15722 {
15723 old_buffer = current_buffer;
15724 set_buffer_internal_1 (XBUFFER (w->buffer));
15725 }
15726
15727 if (NILP (format) || EQ (format, Qt))
15728 {
15729 face_id = NILP (format)
15730 ? CURRENT_MODE_LINE_FACE_ID (w) :
15731 HEADER_LINE_FACE_ID;
15732 format = NILP (format)
15733 ? current_buffer->mode_line_format
15734 : current_buffer->header_line_format;
15735 }
15736
15737 init_iterator (&it, w, -1, -1, NULL, face_id);
15738
15739 if (NILP (no_props))
15740 {
15741 mode_line_string_face =
15742 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15743 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15744 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15745
15746 mode_line_string_face_prop =
15747 NILP (mode_line_string_face) ? Qnil :
15748 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15749
15750 /* We need a dummy last element in mode_line_string_list to
15751 indicate we are building the propertized mode-line string.
15752 Using mode_line_string_face_prop here GC protects it. */
15753 mode_line_string_list =
15754 Fcons (mode_line_string_face_prop, Qnil);
15755 frame_title_ptr = NULL;
15756 }
15757 else
15758 {
15759 mode_line_string_face_prop = Qnil;
15760 mode_line_string_list = Qnil;
15761 frame_title_ptr = frame_title_buf;
15762 }
15763
15764 push_frame_kboard (it.f);
15765 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15766 pop_frame_kboard ();
15767
15768 if (old_buffer)
15769 set_buffer_internal_1 (old_buffer);
15770
15771 if (NILP (no_props))
15772 {
15773 Lisp_Object str;
15774 mode_line_string_list = Fnreverse (mode_line_string_list);
15775 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15776 make_string ("", 0));
15777 mode_line_string_face_prop = Qnil;
15778 mode_line_string_list = Qnil;
15779 return str;
15780 }
15781
15782 len = frame_title_ptr - frame_title_buf;
15783 if (len > 0 && frame_title_ptr[-1] == '-')
15784 {
15785 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15786 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15787 ;
15788 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15789 if (len > frame_title_ptr - frame_title_buf)
15790 len = frame_title_ptr - frame_title_buf;
15791 }
15792
15793 frame_title_ptr = NULL;
15794 return make_string (frame_title_buf, len);
15795 }
15796
15797 /* Write a null-terminated, right justified decimal representation of
15798 the positive integer D to BUF using a minimal field width WIDTH. */
15799
15800 static void
15801 pint2str (buf, width, d)
15802 register char *buf;
15803 register int width;
15804 register int d;
15805 {
15806 register char *p = buf;
15807
15808 if (d <= 0)
15809 *p++ = '0';
15810 else
15811 {
15812 while (d > 0)
15813 {
15814 *p++ = d % 10 + '0';
15815 d /= 10;
15816 }
15817 }
15818
15819 for (width -= (int) (p - buf); width > 0; --width)
15820 *p++ = ' ';
15821 *p-- = '\0';
15822 while (p > buf)
15823 {
15824 d = *buf;
15825 *buf++ = *p;
15826 *p-- = d;
15827 }
15828 }
15829
15830 /* Write a null-terminated, right justified decimal and "human
15831 readable" representation of the nonnegative integer D to BUF using
15832 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15833
15834 static const char power_letter[] =
15835 {
15836 0, /* not used */
15837 'k', /* kilo */
15838 'M', /* mega */
15839 'G', /* giga */
15840 'T', /* tera */
15841 'P', /* peta */
15842 'E', /* exa */
15843 'Z', /* zetta */
15844 'Y' /* yotta */
15845 };
15846
15847 static void
15848 pint2hrstr (buf, width, d)
15849 char *buf;
15850 int width;
15851 int d;
15852 {
15853 /* We aim to represent the nonnegative integer D as
15854 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15855 int quotient = d;
15856 int remainder = 0;
15857 /* -1 means: do not use TENTHS. */
15858 int tenths = -1;
15859 int exponent = 0;
15860
15861 /* Length of QUOTIENT.TENTHS as a string. */
15862 int length;
15863
15864 char * psuffix;
15865 char * p;
15866
15867 if (1000 <= quotient)
15868 {
15869 /* Scale to the appropriate EXPONENT. */
15870 do
15871 {
15872 remainder = quotient % 1000;
15873 quotient /= 1000;
15874 exponent++;
15875 }
15876 while (1000 <= quotient);
15877
15878 /* Round to nearest and decide whether to use TENTHS or not. */
15879 if (quotient <= 9)
15880 {
15881 tenths = remainder / 100;
15882 if (50 <= remainder % 100)
15883 if (tenths < 9)
15884 tenths++;
15885 else
15886 {
15887 quotient++;
15888 if (quotient == 10)
15889 tenths = -1;
15890 else
15891 tenths = 0;
15892 }
15893 }
15894 else
15895 if (500 <= remainder)
15896 if (quotient < 999)
15897 quotient++;
15898 else
15899 {
15900 quotient = 1;
15901 exponent++;
15902 tenths = 0;
15903 }
15904 }
15905
15906 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15907 if (tenths == -1 && quotient <= 99)
15908 if (quotient <= 9)
15909 length = 1;
15910 else
15911 length = 2;
15912 else
15913 length = 3;
15914 p = psuffix = buf + max (width, length);
15915
15916 /* Print EXPONENT. */
15917 if (exponent)
15918 *psuffix++ = power_letter[exponent];
15919 *psuffix = '\0';
15920
15921 /* Print TENTHS. */
15922 if (tenths >= 0)
15923 {
15924 *--p = '0' + tenths;
15925 *--p = '.';
15926 }
15927
15928 /* Print QUOTIENT. */
15929 do
15930 {
15931 int digit = quotient % 10;
15932 *--p = '0' + digit;
15933 }
15934 while ((quotient /= 10) != 0);
15935
15936 /* Print leading spaces. */
15937 while (buf < p)
15938 *--p = ' ';
15939 }
15940
15941 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15942 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15943 type of CODING_SYSTEM. Return updated pointer into BUF. */
15944
15945 static unsigned char invalid_eol_type[] = "(*invalid*)";
15946
15947 static char *
15948 decode_mode_spec_coding (coding_system, buf, eol_flag)
15949 Lisp_Object coding_system;
15950 register char *buf;
15951 int eol_flag;
15952 {
15953 Lisp_Object val;
15954 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15955 const unsigned char *eol_str;
15956 int eol_str_len;
15957 /* The EOL conversion we are using. */
15958 Lisp_Object eoltype;
15959
15960 val = Fget (coding_system, Qcoding_system);
15961 eoltype = Qnil;
15962
15963 if (!VECTORP (val)) /* Not yet decided. */
15964 {
15965 if (multibyte)
15966 *buf++ = '-';
15967 if (eol_flag)
15968 eoltype = eol_mnemonic_undecided;
15969 /* Don't mention EOL conversion if it isn't decided. */
15970 }
15971 else
15972 {
15973 Lisp_Object eolvalue;
15974
15975 eolvalue = Fget (coding_system, Qeol_type);
15976
15977 if (multibyte)
15978 *buf++ = XFASTINT (AREF (val, 1));
15979
15980 if (eol_flag)
15981 {
15982 /* The EOL conversion that is normal on this system. */
15983
15984 if (NILP (eolvalue)) /* Not yet decided. */
15985 eoltype = eol_mnemonic_undecided;
15986 else if (VECTORP (eolvalue)) /* Not yet decided. */
15987 eoltype = eol_mnemonic_undecided;
15988 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15989 eoltype = (XFASTINT (eolvalue) == 0
15990 ? eol_mnemonic_unix
15991 : (XFASTINT (eolvalue) == 1
15992 ? eol_mnemonic_dos : eol_mnemonic_mac));
15993 }
15994 }
15995
15996 if (eol_flag)
15997 {
15998 /* Mention the EOL conversion if it is not the usual one. */
15999 if (STRINGP (eoltype))
16000 {
16001 eol_str = SDATA (eoltype);
16002 eol_str_len = SBYTES (eoltype);
16003 }
16004 else if (INTEGERP (eoltype)
16005 && CHAR_VALID_P (XINT (eoltype), 0))
16006 {
16007 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16008 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16009 eol_str = tmp;
16010 }
16011 else
16012 {
16013 eol_str = invalid_eol_type;
16014 eol_str_len = sizeof (invalid_eol_type) - 1;
16015 }
16016 bcopy (eol_str, buf, eol_str_len);
16017 buf += eol_str_len;
16018 }
16019
16020 return buf;
16021 }
16022
16023 /* Return a string for the output of a mode line %-spec for window W,
16024 generated by character C. PRECISION >= 0 means don't return a
16025 string longer than that value. FIELD_WIDTH > 0 means pad the
16026 string returned with spaces to that value. Return 1 in *MULTIBYTE
16027 if the result is multibyte text. */
16028
16029 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16030
16031 static char *
16032 decode_mode_spec (w, c, field_width, precision, multibyte)
16033 struct window *w;
16034 register int c;
16035 int field_width, precision;
16036 int *multibyte;
16037 {
16038 Lisp_Object obj;
16039 struct frame *f = XFRAME (WINDOW_FRAME (w));
16040 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16041 struct buffer *b = XBUFFER (w->buffer);
16042
16043 obj = Qnil;
16044 *multibyte = 0;
16045
16046 switch (c)
16047 {
16048 case '*':
16049 if (!NILP (b->read_only))
16050 return "%";
16051 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16052 return "*";
16053 return "-";
16054
16055 case '+':
16056 /* This differs from %* only for a modified read-only buffer. */
16057 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16058 return "*";
16059 if (!NILP (b->read_only))
16060 return "%";
16061 return "-";
16062
16063 case '&':
16064 /* This differs from %* in ignoring read-only-ness. */
16065 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16066 return "*";
16067 return "-";
16068
16069 case '%':
16070 return "%";
16071
16072 case '[':
16073 {
16074 int i;
16075 char *p;
16076
16077 if (command_loop_level > 5)
16078 return "[[[... ";
16079 p = decode_mode_spec_buf;
16080 for (i = 0; i < command_loop_level; i++)
16081 *p++ = '[';
16082 *p = 0;
16083 return decode_mode_spec_buf;
16084 }
16085
16086 case ']':
16087 {
16088 int i;
16089 char *p;
16090
16091 if (command_loop_level > 5)
16092 return " ...]]]";
16093 p = decode_mode_spec_buf;
16094 for (i = 0; i < command_loop_level; i++)
16095 *p++ = ']';
16096 *p = 0;
16097 return decode_mode_spec_buf;
16098 }
16099
16100 case '-':
16101 {
16102 register int i;
16103
16104 /* Let lots_of_dashes be a string of infinite length. */
16105 if (!NILP (mode_line_string_list))
16106 return "--";
16107 if (field_width <= 0
16108 || field_width > sizeof (lots_of_dashes))
16109 {
16110 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16111 decode_mode_spec_buf[i] = '-';
16112 decode_mode_spec_buf[i] = '\0';
16113 return decode_mode_spec_buf;
16114 }
16115 else
16116 return lots_of_dashes;
16117 }
16118
16119 case 'b':
16120 obj = b->name;
16121 break;
16122
16123 case 'c':
16124 {
16125 int col = (int) current_column (); /* iftc */
16126 w->column_number_displayed = make_number (col);
16127 pint2str (decode_mode_spec_buf, field_width, col);
16128 return decode_mode_spec_buf;
16129 }
16130
16131 case 'F':
16132 /* %F displays the frame name. */
16133 if (!NILP (f->title))
16134 return (char *) SDATA (f->title);
16135 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16136 return (char *) SDATA (f->name);
16137 return "Emacs";
16138
16139 case 'f':
16140 obj = b->filename;
16141 break;
16142
16143 case 'i':
16144 {
16145 int size = ZV - BEGV;
16146 pint2str (decode_mode_spec_buf, field_width, size);
16147 return decode_mode_spec_buf;
16148 }
16149
16150 case 'I':
16151 {
16152 int size = ZV - BEGV;
16153 pint2hrstr (decode_mode_spec_buf, field_width, size);
16154 return decode_mode_spec_buf;
16155 }
16156
16157 case 'l':
16158 {
16159 int startpos = XMARKER (w->start)->charpos;
16160 int startpos_byte = marker_byte_position (w->start);
16161 int line, linepos, linepos_byte, topline;
16162 int nlines, junk;
16163 int height = WINDOW_TOTAL_LINES (w);
16164
16165 /* If we decided that this buffer isn't suitable for line numbers,
16166 don't forget that too fast. */
16167 if (EQ (w->base_line_pos, w->buffer))
16168 goto no_value;
16169 /* But do forget it, if the window shows a different buffer now. */
16170 else if (BUFFERP (w->base_line_pos))
16171 w->base_line_pos = Qnil;
16172
16173 /* If the buffer is very big, don't waste time. */
16174 if (INTEGERP (Vline_number_display_limit)
16175 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16176 {
16177 w->base_line_pos = Qnil;
16178 w->base_line_number = Qnil;
16179 goto no_value;
16180 }
16181
16182 if (!NILP (w->base_line_number)
16183 && !NILP (w->base_line_pos)
16184 && XFASTINT (w->base_line_pos) <= startpos)
16185 {
16186 line = XFASTINT (w->base_line_number);
16187 linepos = XFASTINT (w->base_line_pos);
16188 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16189 }
16190 else
16191 {
16192 line = 1;
16193 linepos = BUF_BEGV (b);
16194 linepos_byte = BUF_BEGV_BYTE (b);
16195 }
16196
16197 /* Count lines from base line to window start position. */
16198 nlines = display_count_lines (linepos, linepos_byte,
16199 startpos_byte,
16200 startpos, &junk);
16201
16202 topline = nlines + line;
16203
16204 /* Determine a new base line, if the old one is too close
16205 or too far away, or if we did not have one.
16206 "Too close" means it's plausible a scroll-down would
16207 go back past it. */
16208 if (startpos == BUF_BEGV (b))
16209 {
16210 w->base_line_number = make_number (topline);
16211 w->base_line_pos = make_number (BUF_BEGV (b));
16212 }
16213 else if (nlines < height + 25 || nlines > height * 3 + 50
16214 || linepos == BUF_BEGV (b))
16215 {
16216 int limit = BUF_BEGV (b);
16217 int limit_byte = BUF_BEGV_BYTE (b);
16218 int position;
16219 int distance = (height * 2 + 30) * line_number_display_limit_width;
16220
16221 if (startpos - distance > limit)
16222 {
16223 limit = startpos - distance;
16224 limit_byte = CHAR_TO_BYTE (limit);
16225 }
16226
16227 nlines = display_count_lines (startpos, startpos_byte,
16228 limit_byte,
16229 - (height * 2 + 30),
16230 &position);
16231 /* If we couldn't find the lines we wanted within
16232 line_number_display_limit_width chars per line,
16233 give up on line numbers for this window. */
16234 if (position == limit_byte && limit == startpos - distance)
16235 {
16236 w->base_line_pos = w->buffer;
16237 w->base_line_number = Qnil;
16238 goto no_value;
16239 }
16240
16241 w->base_line_number = make_number (topline - nlines);
16242 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16243 }
16244
16245 /* Now count lines from the start pos to point. */
16246 nlines = display_count_lines (startpos, startpos_byte,
16247 PT_BYTE, PT, &junk);
16248
16249 /* Record that we did display the line number. */
16250 line_number_displayed = 1;
16251
16252 /* Make the string to show. */
16253 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16254 return decode_mode_spec_buf;
16255 no_value:
16256 {
16257 char* p = decode_mode_spec_buf;
16258 int pad = field_width - 2;
16259 while (pad-- > 0)
16260 *p++ = ' ';
16261 *p++ = '?';
16262 *p++ = '?';
16263 *p = '\0';
16264 return decode_mode_spec_buf;
16265 }
16266 }
16267 break;
16268
16269 case 'm':
16270 obj = b->mode_name;
16271 break;
16272
16273 case 'n':
16274 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16275 return " Narrow";
16276 break;
16277
16278 case 'p':
16279 {
16280 int pos = marker_position (w->start);
16281 int total = BUF_ZV (b) - BUF_BEGV (b);
16282
16283 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16284 {
16285 if (pos <= BUF_BEGV (b))
16286 return "All";
16287 else
16288 return "Bottom";
16289 }
16290 else if (pos <= BUF_BEGV (b))
16291 return "Top";
16292 else
16293 {
16294 if (total > 1000000)
16295 /* Do it differently for a large value, to avoid overflow. */
16296 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16297 else
16298 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16299 /* We can't normally display a 3-digit number,
16300 so get us a 2-digit number that is close. */
16301 if (total == 100)
16302 total = 99;
16303 sprintf (decode_mode_spec_buf, "%2d%%", total);
16304 return decode_mode_spec_buf;
16305 }
16306 }
16307
16308 /* Display percentage of size above the bottom of the screen. */
16309 case 'P':
16310 {
16311 int toppos = marker_position (w->start);
16312 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16313 int total = BUF_ZV (b) - BUF_BEGV (b);
16314
16315 if (botpos >= BUF_ZV (b))
16316 {
16317 if (toppos <= BUF_BEGV (b))
16318 return "All";
16319 else
16320 return "Bottom";
16321 }
16322 else
16323 {
16324 if (total > 1000000)
16325 /* Do it differently for a large value, to avoid overflow. */
16326 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16327 else
16328 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16329 /* We can't normally display a 3-digit number,
16330 so get us a 2-digit number that is close. */
16331 if (total == 100)
16332 total = 99;
16333 if (toppos <= BUF_BEGV (b))
16334 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16335 else
16336 sprintf (decode_mode_spec_buf, "%2d%%", total);
16337 return decode_mode_spec_buf;
16338 }
16339 }
16340
16341 case 's':
16342 /* status of process */
16343 obj = Fget_buffer_process (w->buffer);
16344 if (NILP (obj))
16345 return "no process";
16346 #ifdef subprocesses
16347 obj = Fsymbol_name (Fprocess_status (obj));
16348 #endif
16349 break;
16350
16351 case 't': /* indicate TEXT or BINARY */
16352 #ifdef MODE_LINE_BINARY_TEXT
16353 return MODE_LINE_BINARY_TEXT (b);
16354 #else
16355 return "T";
16356 #endif
16357
16358 case 'z':
16359 /* coding-system (not including end-of-line format) */
16360 case 'Z':
16361 /* coding-system (including end-of-line type) */
16362 {
16363 int eol_flag = (c == 'Z');
16364 char *p = decode_mode_spec_buf;
16365
16366 if (! FRAME_WINDOW_P (f))
16367 {
16368 /* No need to mention EOL here--the terminal never needs
16369 to do EOL conversion. */
16370 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16371 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16372 }
16373 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16374 p, eol_flag);
16375
16376 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16377 #ifdef subprocesses
16378 obj = Fget_buffer_process (Fcurrent_buffer ());
16379 if (PROCESSP (obj))
16380 {
16381 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16382 p, eol_flag);
16383 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16384 p, eol_flag);
16385 }
16386 #endif /* subprocesses */
16387 #endif /* 0 */
16388 *p = 0;
16389 return decode_mode_spec_buf;
16390 }
16391 }
16392
16393 if (STRINGP (obj))
16394 {
16395 *multibyte = STRING_MULTIBYTE (obj);
16396 return (char *) SDATA (obj);
16397 }
16398 else
16399 return "";
16400 }
16401
16402
16403 /* Count up to COUNT lines starting from START / START_BYTE.
16404 But don't go beyond LIMIT_BYTE.
16405 Return the number of lines thus found (always nonnegative).
16406
16407 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16408
16409 static int
16410 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16411 int start, start_byte, limit_byte, count;
16412 int *byte_pos_ptr;
16413 {
16414 register unsigned char *cursor;
16415 unsigned char *base;
16416
16417 register int ceiling;
16418 register unsigned char *ceiling_addr;
16419 int orig_count = count;
16420
16421 /* If we are not in selective display mode,
16422 check only for newlines. */
16423 int selective_display = (!NILP (current_buffer->selective_display)
16424 && !INTEGERP (current_buffer->selective_display));
16425
16426 if (count > 0)
16427 {
16428 while (start_byte < limit_byte)
16429 {
16430 ceiling = BUFFER_CEILING_OF (start_byte);
16431 ceiling = min (limit_byte - 1, ceiling);
16432 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16433 base = (cursor = BYTE_POS_ADDR (start_byte));
16434 while (1)
16435 {
16436 if (selective_display)
16437 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16438 ;
16439 else
16440 while (*cursor != '\n' && ++cursor != ceiling_addr)
16441 ;
16442
16443 if (cursor != ceiling_addr)
16444 {
16445 if (--count == 0)
16446 {
16447 start_byte += cursor - base + 1;
16448 *byte_pos_ptr = start_byte;
16449 return orig_count;
16450 }
16451 else
16452 if (++cursor == ceiling_addr)
16453 break;
16454 }
16455 else
16456 break;
16457 }
16458 start_byte += cursor - base;
16459 }
16460 }
16461 else
16462 {
16463 while (start_byte > limit_byte)
16464 {
16465 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16466 ceiling = max (limit_byte, ceiling);
16467 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16468 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16469 while (1)
16470 {
16471 if (selective_display)
16472 while (--cursor != ceiling_addr
16473 && *cursor != '\n' && *cursor != 015)
16474 ;
16475 else
16476 while (--cursor != ceiling_addr && *cursor != '\n')
16477 ;
16478
16479 if (cursor != ceiling_addr)
16480 {
16481 if (++count == 0)
16482 {
16483 start_byte += cursor - base + 1;
16484 *byte_pos_ptr = start_byte;
16485 /* When scanning backwards, we should
16486 not count the newline posterior to which we stop. */
16487 return - orig_count - 1;
16488 }
16489 }
16490 else
16491 break;
16492 }
16493 /* Here we add 1 to compensate for the last decrement
16494 of CURSOR, which took it past the valid range. */
16495 start_byte += cursor - base + 1;
16496 }
16497 }
16498
16499 *byte_pos_ptr = limit_byte;
16500
16501 if (count < 0)
16502 return - orig_count + count;
16503 return orig_count - count;
16504
16505 }
16506
16507
16508 \f
16509 /***********************************************************************
16510 Displaying strings
16511 ***********************************************************************/
16512
16513 /* Display a NUL-terminated string, starting with index START.
16514
16515 If STRING is non-null, display that C string. Otherwise, the Lisp
16516 string LISP_STRING is displayed.
16517
16518 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16519 FACE_STRING. Display STRING or LISP_STRING with the face at
16520 FACE_STRING_POS in FACE_STRING:
16521
16522 Display the string in the environment given by IT, but use the
16523 standard display table, temporarily.
16524
16525 FIELD_WIDTH is the minimum number of output glyphs to produce.
16526 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16527 with spaces. If STRING has more characters, more than FIELD_WIDTH
16528 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16529
16530 PRECISION is the maximum number of characters to output from
16531 STRING. PRECISION < 0 means don't truncate the string.
16532
16533 This is roughly equivalent to printf format specifiers:
16534
16535 FIELD_WIDTH PRECISION PRINTF
16536 ----------------------------------------
16537 -1 -1 %s
16538 -1 10 %.10s
16539 10 -1 %10s
16540 20 10 %20.10s
16541
16542 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16543 display them, and < 0 means obey the current buffer's value of
16544 enable_multibyte_characters.
16545
16546 Value is the number of glyphs produced. */
16547
16548 static int
16549 display_string (string, lisp_string, face_string, face_string_pos,
16550 start, it, field_width, precision, max_x, multibyte)
16551 unsigned char *string;
16552 Lisp_Object lisp_string;
16553 Lisp_Object face_string;
16554 int face_string_pos;
16555 int start;
16556 struct it *it;
16557 int field_width, precision, max_x;
16558 int multibyte;
16559 {
16560 int hpos_at_start = it->hpos;
16561 int saved_face_id = it->face_id;
16562 struct glyph_row *row = it->glyph_row;
16563
16564 /* Initialize the iterator IT for iteration over STRING beginning
16565 with index START. */
16566 reseat_to_string (it, string, lisp_string, start,
16567 precision, field_width, multibyte);
16568
16569 /* If displaying STRING, set up the face of the iterator
16570 from LISP_STRING, if that's given. */
16571 if (STRINGP (face_string))
16572 {
16573 int endptr;
16574 struct face *face;
16575
16576 it->face_id
16577 = face_at_string_position (it->w, face_string, face_string_pos,
16578 0, it->region_beg_charpos,
16579 it->region_end_charpos,
16580 &endptr, it->base_face_id, 0);
16581 face = FACE_FROM_ID (it->f, it->face_id);
16582 it->face_box_p = face->box != FACE_NO_BOX;
16583 }
16584
16585 /* Set max_x to the maximum allowed X position. Don't let it go
16586 beyond the right edge of the window. */
16587 if (max_x <= 0)
16588 max_x = it->last_visible_x;
16589 else
16590 max_x = min (max_x, it->last_visible_x);
16591
16592 /* Skip over display elements that are not visible. because IT->w is
16593 hscrolled. */
16594 if (it->current_x < it->first_visible_x)
16595 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16596 MOVE_TO_POS | MOVE_TO_X);
16597
16598 row->ascent = it->max_ascent;
16599 row->height = it->max_ascent + it->max_descent;
16600 row->phys_ascent = it->max_phys_ascent;
16601 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16602
16603 /* This condition is for the case that we are called with current_x
16604 past last_visible_x. */
16605 while (it->current_x < max_x)
16606 {
16607 int x_before, x, n_glyphs_before, i, nglyphs;
16608
16609 /* Get the next display element. */
16610 if (!get_next_display_element (it))
16611 break;
16612
16613 /* Produce glyphs. */
16614 x_before = it->current_x;
16615 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16616 PRODUCE_GLYPHS (it);
16617
16618 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16619 i = 0;
16620 x = x_before;
16621 while (i < nglyphs)
16622 {
16623 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16624
16625 if (!it->truncate_lines_p
16626 && x + glyph->pixel_width > max_x)
16627 {
16628 /* End of continued line or max_x reached. */
16629 if (CHAR_GLYPH_PADDING_P (*glyph))
16630 {
16631 /* A wide character is unbreakable. */
16632 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16633 it->current_x = x_before;
16634 }
16635 else
16636 {
16637 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16638 it->current_x = x;
16639 }
16640 break;
16641 }
16642 else if (x + glyph->pixel_width > it->first_visible_x)
16643 {
16644 /* Glyph is at least partially visible. */
16645 ++it->hpos;
16646 if (x < it->first_visible_x)
16647 it->glyph_row->x = x - it->first_visible_x;
16648 }
16649 else
16650 {
16651 /* Glyph is off the left margin of the display area.
16652 Should not happen. */
16653 abort ();
16654 }
16655
16656 row->ascent = max (row->ascent, it->max_ascent);
16657 row->height = max (row->height, it->max_ascent + it->max_descent);
16658 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16659 row->phys_height = max (row->phys_height,
16660 it->max_phys_ascent + it->max_phys_descent);
16661 x += glyph->pixel_width;
16662 ++i;
16663 }
16664
16665 /* Stop if max_x reached. */
16666 if (i < nglyphs)
16667 break;
16668
16669 /* Stop at line ends. */
16670 if (ITERATOR_AT_END_OF_LINE_P (it))
16671 {
16672 it->continuation_lines_width = 0;
16673 break;
16674 }
16675
16676 set_iterator_to_next (it, 1);
16677
16678 /* Stop if truncating at the right edge. */
16679 if (it->truncate_lines_p
16680 && it->current_x >= it->last_visible_x)
16681 {
16682 /* Add truncation mark, but don't do it if the line is
16683 truncated at a padding space. */
16684 if (IT_CHARPOS (*it) < it->string_nchars)
16685 {
16686 if (!FRAME_WINDOW_P (it->f))
16687 {
16688 int i, n;
16689
16690 if (it->current_x > it->last_visible_x)
16691 {
16692 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16693 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16694 break;
16695 for (n = row->used[TEXT_AREA]; i < n; ++i)
16696 {
16697 row->used[TEXT_AREA] = i;
16698 produce_special_glyphs (it, IT_TRUNCATION);
16699 }
16700 }
16701 produce_special_glyphs (it, IT_TRUNCATION);
16702 }
16703 it->glyph_row->truncated_on_right_p = 1;
16704 }
16705 break;
16706 }
16707 }
16708
16709 /* Maybe insert a truncation at the left. */
16710 if (it->first_visible_x
16711 && IT_CHARPOS (*it) > 0)
16712 {
16713 if (!FRAME_WINDOW_P (it->f))
16714 insert_left_trunc_glyphs (it);
16715 it->glyph_row->truncated_on_left_p = 1;
16716 }
16717
16718 it->face_id = saved_face_id;
16719
16720 /* Value is number of columns displayed. */
16721 return it->hpos - hpos_at_start;
16722 }
16723
16724
16725 \f
16726 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16727 appears as an element of LIST or as the car of an element of LIST.
16728 If PROPVAL is a list, compare each element against LIST in that
16729 way, and return 1/2 if any element of PROPVAL is found in LIST.
16730 Otherwise return 0. This function cannot quit.
16731 The return value is 2 if the text is invisible but with an ellipsis
16732 and 1 if it's invisible and without an ellipsis. */
16733
16734 int
16735 invisible_p (propval, list)
16736 register Lisp_Object propval;
16737 Lisp_Object list;
16738 {
16739 register Lisp_Object tail, proptail;
16740
16741 for (tail = list; CONSP (tail); tail = XCDR (tail))
16742 {
16743 register Lisp_Object tem;
16744 tem = XCAR (tail);
16745 if (EQ (propval, tem))
16746 return 1;
16747 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16748 return NILP (XCDR (tem)) ? 1 : 2;
16749 }
16750
16751 if (CONSP (propval))
16752 {
16753 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16754 {
16755 Lisp_Object propelt;
16756 propelt = XCAR (proptail);
16757 for (tail = list; CONSP (tail); tail = XCDR (tail))
16758 {
16759 register Lisp_Object tem;
16760 tem = XCAR (tail);
16761 if (EQ (propelt, tem))
16762 return 1;
16763 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16764 return NILP (XCDR (tem)) ? 1 : 2;
16765 }
16766 }
16767 }
16768
16769 return 0;
16770 }
16771
16772 /* Calculate a width or height in pixels from a specification using
16773 the following elements:
16774
16775 SPEC ::=
16776 NUM - a (fractional) multiple of the default font width/height
16777 (NUM) - specifies exactly NUM pixels
16778 UNIT - a fixed number of pixels, see below.
16779 ELEMENT - size of a display element in pixels, see below.
16780 (NUM . SPEC) - equals NUM * SPEC
16781 (+ SPEC SPEC ...) - add pixel values
16782 (- SPEC SPEC ...) - subtract pixel values
16783 (- SPEC) - negate pixel value
16784
16785 NUM ::=
16786 INT or FLOAT - a number constant
16787 SYMBOL - use symbol's (buffer local) variable binding.
16788
16789 UNIT ::=
16790 in - pixels per inch *)
16791 mm - pixels per 1/1000 meter *)
16792 cm - pixels per 1/100 meter *)
16793 width - width of current font in pixels.
16794 height - height of current font in pixels.
16795
16796 *) using the ratio(s) defined in display-pixels-per-inch.
16797
16798 ELEMENT ::=
16799
16800 left-fringe - left fringe width in pixels
16801 right-fringe - right fringe width in pixels
16802
16803 left-margin - left margin width in pixels
16804 right-margin - right margin width in pixels
16805
16806 scroll-bar - scroll-bar area width in pixels
16807
16808 Examples:
16809
16810 Pixels corresponding to 5 inches:
16811 (5 . in)
16812
16813 Total width of non-text areas on left side of window (if scroll-bar is on left):
16814 '(space :width (+ left-fringe left-margin scroll-bar))
16815
16816 Align to first text column (in header line):
16817 '(space :align-to 0)
16818
16819 Align to middle of text area minus half the width of variable `my-image'
16820 containing a loaded image:
16821 '(space :align-to (0.5 . (- text my-image)))
16822
16823 Width of left margin minus width of 1 character in the default font:
16824 '(space :width (- left-margin 1))
16825
16826 Width of left margin minus width of 2 characters in the current font:
16827 '(space :width (- left-margin (2 . width)))
16828
16829 Center 1 character over left-margin (in header line):
16830 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16831
16832 Different ways to express width of left fringe plus left margin minus one pixel:
16833 '(space :width (- (+ left-fringe left-margin) (1)))
16834 '(space :width (+ left-fringe left-margin (- (1))))
16835 '(space :width (+ left-fringe left-margin (-1)))
16836
16837 */
16838
16839 #define NUMVAL(X) \
16840 ((INTEGERP (X) || FLOATP (X)) \
16841 ? XFLOATINT (X) \
16842 : - 1)
16843
16844 int
16845 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16846 double *res;
16847 struct it *it;
16848 Lisp_Object prop;
16849 void *font;
16850 int width_p, *align_to;
16851 {
16852 double pixels;
16853
16854 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16855 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16856
16857 if (NILP (prop))
16858 return OK_PIXELS (0);
16859
16860 if (SYMBOLP (prop))
16861 {
16862 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16863 {
16864 char *unit = SDATA (SYMBOL_NAME (prop));
16865
16866 if (unit[0] == 'i' && unit[1] == 'n')
16867 pixels = 1.0;
16868 else if (unit[0] == 'm' && unit[1] == 'm')
16869 pixels = 25.4;
16870 else if (unit[0] == 'c' && unit[1] == 'm')
16871 pixels = 2.54;
16872 else
16873 pixels = 0;
16874 if (pixels > 0)
16875 {
16876 double ppi;
16877 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16878 || (CONSP (Vdisplay_pixels_per_inch)
16879 && (ppi = (width_p
16880 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16881 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16882 ppi > 0)))
16883 return OK_PIXELS (ppi / pixels);
16884
16885 return 0;
16886 }
16887 }
16888
16889 #ifdef HAVE_WINDOW_SYSTEM
16890 if (EQ (prop, Qheight))
16891 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16892 if (EQ (prop, Qwidth))
16893 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16894 #else
16895 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16896 return OK_PIXELS (1);
16897 #endif
16898
16899 if (EQ (prop, Qtext))
16900 return OK_PIXELS (width_p
16901 ? window_box_width (it->w, TEXT_AREA)
16902 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16903
16904 if (align_to && *align_to < 0)
16905 {
16906 *res = 0;
16907 if (EQ (prop, Qleft))
16908 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16909 if (EQ (prop, Qright))
16910 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16911 if (EQ (prop, Qcenter))
16912 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16913 + window_box_width (it->w, TEXT_AREA) / 2);
16914 if (EQ (prop, Qleft_fringe))
16915 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16916 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16917 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16918 if (EQ (prop, Qright_fringe))
16919 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16920 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16921 : window_box_right_offset (it->w, TEXT_AREA));
16922 if (EQ (prop, Qleft_margin))
16923 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16924 if (EQ (prop, Qright_margin))
16925 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16926 if (EQ (prop, Qscroll_bar))
16927 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16928 ? 0
16929 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16930 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16931 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16932 : 0)));
16933 }
16934 else
16935 {
16936 if (EQ (prop, Qleft_fringe))
16937 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16938 if (EQ (prop, Qright_fringe))
16939 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
16940 if (EQ (prop, Qleft_margin))
16941 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
16942 if (EQ (prop, Qright_margin))
16943 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
16944 if (EQ (prop, Qscroll_bar))
16945 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
16946 }
16947
16948 prop = Fbuffer_local_value (prop, it->w->buffer);
16949 }
16950
16951 if (INTEGERP (prop) || FLOATP (prop))
16952 {
16953 int base_unit = (width_p
16954 ? FRAME_COLUMN_WIDTH (it->f)
16955 : FRAME_LINE_HEIGHT (it->f));
16956 return OK_PIXELS (XFLOATINT (prop) * base_unit);
16957 }
16958
16959 if (CONSP (prop))
16960 {
16961 Lisp_Object car = XCAR (prop);
16962 Lisp_Object cdr = XCDR (prop);
16963
16964 if (SYMBOLP (car))
16965 {
16966 #ifdef HAVE_WINDOW_SYSTEM
16967 if (valid_image_p (prop))
16968 {
16969 int id = lookup_image (it->f, prop);
16970 struct image *img = IMAGE_FROM_ID (it->f, id);
16971
16972 return OK_PIXELS (width_p ? img->width : img->height);
16973 }
16974 #endif
16975 if (EQ (car, Qplus) || EQ (car, Qminus))
16976 {
16977 int first = 1;
16978 double px;
16979
16980 pixels = 0;
16981 while (CONSP (cdr))
16982 {
16983 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
16984 font, width_p, align_to))
16985 return 0;
16986 if (first)
16987 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
16988 else
16989 pixels += px;
16990 cdr = XCDR (cdr);
16991 }
16992 if (EQ (car, Qminus))
16993 pixels = -pixels;
16994 return OK_PIXELS (pixels);
16995 }
16996
16997 car = Fbuffer_local_value (car, it->w->buffer);
16998 }
16999
17000 if (INTEGERP (car) || FLOATP (car))
17001 {
17002 double fact;
17003 pixels = XFLOATINT (car);
17004 if (NILP (cdr))
17005 return OK_PIXELS (pixels);
17006 if (calc_pixel_width_or_height (&fact, it, cdr,
17007 font, width_p, align_to))
17008 return OK_PIXELS (pixels * fact);
17009 return 0;
17010 }
17011
17012 return 0;
17013 }
17014
17015 return 0;
17016 }
17017
17018 \f
17019 /***********************************************************************
17020 Glyph Display
17021 ***********************************************************************/
17022
17023 #ifdef HAVE_WINDOW_SYSTEM
17024
17025 #if GLYPH_DEBUG
17026
17027 void
17028 dump_glyph_string (s)
17029 struct glyph_string *s;
17030 {
17031 fprintf (stderr, "glyph string\n");
17032 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17033 s->x, s->y, s->width, s->height);
17034 fprintf (stderr, " ybase = %d\n", s->ybase);
17035 fprintf (stderr, " hl = %d\n", s->hl);
17036 fprintf (stderr, " left overhang = %d, right = %d\n",
17037 s->left_overhang, s->right_overhang);
17038 fprintf (stderr, " nchars = %d\n", s->nchars);
17039 fprintf (stderr, " extends to end of line = %d\n",
17040 s->extends_to_end_of_line_p);
17041 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17042 fprintf (stderr, " bg width = %d\n", s->background_width);
17043 }
17044
17045 #endif /* GLYPH_DEBUG */
17046
17047 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17048 of XChar2b structures for S; it can't be allocated in
17049 init_glyph_string because it must be allocated via `alloca'. W
17050 is the window on which S is drawn. ROW and AREA are the glyph row
17051 and area within the row from which S is constructed. START is the
17052 index of the first glyph structure covered by S. HL is a
17053 face-override for drawing S. */
17054
17055 #ifdef HAVE_NTGUI
17056 #define OPTIONAL_HDC(hdc) hdc,
17057 #define DECLARE_HDC(hdc) HDC hdc;
17058 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17059 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17060 #endif
17061
17062 #ifndef OPTIONAL_HDC
17063 #define OPTIONAL_HDC(hdc)
17064 #define DECLARE_HDC(hdc)
17065 #define ALLOCATE_HDC(hdc, f)
17066 #define RELEASE_HDC(hdc, f)
17067 #endif
17068
17069 static void
17070 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17071 struct glyph_string *s;
17072 DECLARE_HDC (hdc)
17073 XChar2b *char2b;
17074 struct window *w;
17075 struct glyph_row *row;
17076 enum glyph_row_area area;
17077 int start;
17078 enum draw_glyphs_face hl;
17079 {
17080 bzero (s, sizeof *s);
17081 s->w = w;
17082 s->f = XFRAME (w->frame);
17083 #ifdef HAVE_NTGUI
17084 s->hdc = hdc;
17085 #endif
17086 s->display = FRAME_X_DISPLAY (s->f);
17087 s->window = FRAME_X_WINDOW (s->f);
17088 s->char2b = char2b;
17089 s->hl = hl;
17090 s->row = row;
17091 s->area = area;
17092 s->first_glyph = row->glyphs[area] + start;
17093 s->height = row->height;
17094 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17095
17096 /* Display the internal border below the tool-bar window. */
17097 if (s->w == XWINDOW (s->f->tool_bar_window))
17098 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17099
17100 s->ybase = s->y + row->ascent;
17101 }
17102
17103
17104 /* Append the list of glyph strings with head H and tail T to the list
17105 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17106
17107 static INLINE void
17108 append_glyph_string_lists (head, tail, h, t)
17109 struct glyph_string **head, **tail;
17110 struct glyph_string *h, *t;
17111 {
17112 if (h)
17113 {
17114 if (*head)
17115 (*tail)->next = h;
17116 else
17117 *head = h;
17118 h->prev = *tail;
17119 *tail = t;
17120 }
17121 }
17122
17123
17124 /* Prepend the list of glyph strings with head H and tail T to the
17125 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17126 result. */
17127
17128 static INLINE void
17129 prepend_glyph_string_lists (head, tail, h, t)
17130 struct glyph_string **head, **tail;
17131 struct glyph_string *h, *t;
17132 {
17133 if (h)
17134 {
17135 if (*head)
17136 (*head)->prev = t;
17137 else
17138 *tail = t;
17139 t->next = *head;
17140 *head = h;
17141 }
17142 }
17143
17144
17145 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17146 Set *HEAD and *TAIL to the resulting list. */
17147
17148 static INLINE void
17149 append_glyph_string (head, tail, s)
17150 struct glyph_string **head, **tail;
17151 struct glyph_string *s;
17152 {
17153 s->next = s->prev = NULL;
17154 append_glyph_string_lists (head, tail, s, s);
17155 }
17156
17157
17158 /* Get face and two-byte form of character glyph GLYPH on frame F.
17159 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17160 a pointer to a realized face that is ready for display. */
17161
17162 static INLINE struct face *
17163 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17164 struct frame *f;
17165 struct glyph *glyph;
17166 XChar2b *char2b;
17167 int *two_byte_p;
17168 {
17169 struct face *face;
17170
17171 xassert (glyph->type == CHAR_GLYPH);
17172 face = FACE_FROM_ID (f, glyph->face_id);
17173
17174 if (two_byte_p)
17175 *two_byte_p = 0;
17176
17177 if (!glyph->multibyte_p)
17178 {
17179 /* Unibyte case. We don't have to encode, but we have to make
17180 sure to use a face suitable for unibyte. */
17181 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17182 }
17183 else if (glyph->u.ch < 128
17184 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17185 {
17186 /* Case of ASCII in a face known to fit ASCII. */
17187 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17188 }
17189 else
17190 {
17191 int c1, c2, charset;
17192
17193 /* Split characters into bytes. If c2 is -1 afterwards, C is
17194 really a one-byte character so that byte1 is zero. */
17195 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17196 if (c2 > 0)
17197 STORE_XCHAR2B (char2b, c1, c2);
17198 else
17199 STORE_XCHAR2B (char2b, 0, c1);
17200
17201 /* Maybe encode the character in *CHAR2B. */
17202 if (charset != CHARSET_ASCII)
17203 {
17204 struct font_info *font_info
17205 = FONT_INFO_FROM_ID (f, face->font_info_id);
17206 if (font_info)
17207 glyph->font_type
17208 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17209 }
17210 }
17211
17212 /* Make sure X resources of the face are allocated. */
17213 xassert (face != NULL);
17214 PREPARE_FACE_FOR_DISPLAY (f, face);
17215 return face;
17216 }
17217
17218
17219 /* Fill glyph string S with composition components specified by S->cmp.
17220
17221 FACES is an array of faces for all components of this composition.
17222 S->gidx is the index of the first component for S.
17223 OVERLAPS_P non-zero means S should draw the foreground only, and
17224 use its physical height for clipping.
17225
17226 Value is the index of a component not in S. */
17227
17228 static int
17229 fill_composite_glyph_string (s, faces, overlaps_p)
17230 struct glyph_string *s;
17231 struct face **faces;
17232 int overlaps_p;
17233 {
17234 int i;
17235
17236 xassert (s);
17237
17238 s->for_overlaps_p = overlaps_p;
17239
17240 s->face = faces[s->gidx];
17241 s->font = s->face->font;
17242 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17243
17244 /* For all glyphs of this composition, starting at the offset
17245 S->gidx, until we reach the end of the definition or encounter a
17246 glyph that requires the different face, add it to S. */
17247 ++s->nchars;
17248 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17249 ++s->nchars;
17250
17251 /* All glyph strings for the same composition has the same width,
17252 i.e. the width set for the first component of the composition. */
17253
17254 s->width = s->first_glyph->pixel_width;
17255
17256 /* If the specified font could not be loaded, use the frame's
17257 default font, but record the fact that we couldn't load it in
17258 the glyph string so that we can draw rectangles for the
17259 characters of the glyph string. */
17260 if (s->font == NULL)
17261 {
17262 s->font_not_found_p = 1;
17263 s->font = FRAME_FONT (s->f);
17264 }
17265
17266 /* Adjust base line for subscript/superscript text. */
17267 s->ybase += s->first_glyph->voffset;
17268
17269 xassert (s->face && s->face->gc);
17270
17271 /* This glyph string must always be drawn with 16-bit functions. */
17272 s->two_byte_p = 1;
17273
17274 return s->gidx + s->nchars;
17275 }
17276
17277
17278 /* Fill glyph string S from a sequence of character glyphs.
17279
17280 FACE_ID is the face id of the string. START is the index of the
17281 first glyph to consider, END is the index of the last + 1.
17282 OVERLAPS_P non-zero means S should draw the foreground only, and
17283 use its physical height for clipping.
17284
17285 Value is the index of the first glyph not in S. */
17286
17287 static int
17288 fill_glyph_string (s, face_id, start, end, overlaps_p)
17289 struct glyph_string *s;
17290 int face_id;
17291 int start, end, overlaps_p;
17292 {
17293 struct glyph *glyph, *last;
17294 int voffset;
17295 int glyph_not_available_p;
17296
17297 xassert (s->f == XFRAME (s->w->frame));
17298 xassert (s->nchars == 0);
17299 xassert (start >= 0 && end > start);
17300
17301 s->for_overlaps_p = overlaps_p,
17302 glyph = s->row->glyphs[s->area] + start;
17303 last = s->row->glyphs[s->area] + end;
17304 voffset = glyph->voffset;
17305
17306 glyph_not_available_p = glyph->glyph_not_available_p;
17307
17308 while (glyph < last
17309 && glyph->type == CHAR_GLYPH
17310 && glyph->voffset == voffset
17311 /* Same face id implies same font, nowadays. */
17312 && glyph->face_id == face_id
17313 && glyph->glyph_not_available_p == glyph_not_available_p)
17314 {
17315 int two_byte_p;
17316
17317 s->face = get_glyph_face_and_encoding (s->f, glyph,
17318 s->char2b + s->nchars,
17319 &two_byte_p);
17320 s->two_byte_p = two_byte_p;
17321 ++s->nchars;
17322 xassert (s->nchars <= end - start);
17323 s->width += glyph->pixel_width;
17324 ++glyph;
17325 }
17326
17327 s->font = s->face->font;
17328 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17329
17330 /* If the specified font could not be loaded, use the frame's font,
17331 but record the fact that we couldn't load it in
17332 S->font_not_found_p so that we can draw rectangles for the
17333 characters of the glyph string. */
17334 if (s->font == NULL || glyph_not_available_p)
17335 {
17336 s->font_not_found_p = 1;
17337 s->font = FRAME_FONT (s->f);
17338 }
17339
17340 /* Adjust base line for subscript/superscript text. */
17341 s->ybase += voffset;
17342
17343 xassert (s->face && s->face->gc);
17344 return glyph - s->row->glyphs[s->area];
17345 }
17346
17347
17348 /* Fill glyph string S from image glyph S->first_glyph. */
17349
17350 static void
17351 fill_image_glyph_string (s)
17352 struct glyph_string *s;
17353 {
17354 xassert (s->first_glyph->type == IMAGE_GLYPH);
17355 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17356 xassert (s->img);
17357 s->slice = s->first_glyph->slice;
17358 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17359 s->font = s->face->font;
17360 s->width = s->first_glyph->pixel_width;
17361
17362 /* Adjust base line for subscript/superscript text. */
17363 s->ybase += s->first_glyph->voffset;
17364 }
17365
17366
17367 /* Fill glyph string S from a sequence of stretch glyphs.
17368
17369 ROW is the glyph row in which the glyphs are found, AREA is the
17370 area within the row. START is the index of the first glyph to
17371 consider, END is the index of the last + 1.
17372
17373 Value is the index of the first glyph not in S. */
17374
17375 static int
17376 fill_stretch_glyph_string (s, row, area, start, end)
17377 struct glyph_string *s;
17378 struct glyph_row *row;
17379 enum glyph_row_area area;
17380 int start, end;
17381 {
17382 struct glyph *glyph, *last;
17383 int voffset, face_id;
17384
17385 xassert (s->first_glyph->type == STRETCH_GLYPH);
17386
17387 glyph = s->row->glyphs[s->area] + start;
17388 last = s->row->glyphs[s->area] + end;
17389 face_id = glyph->face_id;
17390 s->face = FACE_FROM_ID (s->f, face_id);
17391 s->font = s->face->font;
17392 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17393 s->width = glyph->pixel_width;
17394 voffset = glyph->voffset;
17395
17396 for (++glyph;
17397 (glyph < last
17398 && glyph->type == STRETCH_GLYPH
17399 && glyph->voffset == voffset
17400 && glyph->face_id == face_id);
17401 ++glyph)
17402 s->width += glyph->pixel_width;
17403
17404 /* Adjust base line for subscript/superscript text. */
17405 s->ybase += voffset;
17406
17407 /* The case that face->gc == 0 is handled when drawing the glyph
17408 string by calling PREPARE_FACE_FOR_DISPLAY. */
17409 xassert (s->face);
17410 return glyph - s->row->glyphs[s->area];
17411 }
17412
17413
17414 /* EXPORT for RIF:
17415 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17416 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17417 assumed to be zero. */
17418
17419 void
17420 x_get_glyph_overhangs (glyph, f, left, right)
17421 struct glyph *glyph;
17422 struct frame *f;
17423 int *left, *right;
17424 {
17425 *left = *right = 0;
17426
17427 if (glyph->type == CHAR_GLYPH)
17428 {
17429 XFontStruct *font;
17430 struct face *face;
17431 struct font_info *font_info;
17432 XChar2b char2b;
17433 XCharStruct *pcm;
17434
17435 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17436 font = face->font;
17437 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17438 if (font /* ++KFS: Should this be font_info ? */
17439 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17440 {
17441 if (pcm->rbearing > pcm->width)
17442 *right = pcm->rbearing - pcm->width;
17443 if (pcm->lbearing < 0)
17444 *left = -pcm->lbearing;
17445 }
17446 }
17447 }
17448
17449
17450 /* Return the index of the first glyph preceding glyph string S that
17451 is overwritten by S because of S's left overhang. Value is -1
17452 if no glyphs are overwritten. */
17453
17454 static int
17455 left_overwritten (s)
17456 struct glyph_string *s;
17457 {
17458 int k;
17459
17460 if (s->left_overhang)
17461 {
17462 int x = 0, i;
17463 struct glyph *glyphs = s->row->glyphs[s->area];
17464 int first = s->first_glyph - glyphs;
17465
17466 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17467 x -= glyphs[i].pixel_width;
17468
17469 k = i + 1;
17470 }
17471 else
17472 k = -1;
17473
17474 return k;
17475 }
17476
17477
17478 /* Return the index of the first glyph preceding glyph string S that
17479 is overwriting S because of its right overhang. Value is -1 if no
17480 glyph in front of S overwrites S. */
17481
17482 static int
17483 left_overwriting (s)
17484 struct glyph_string *s;
17485 {
17486 int i, k, x;
17487 struct glyph *glyphs = s->row->glyphs[s->area];
17488 int first = s->first_glyph - glyphs;
17489
17490 k = -1;
17491 x = 0;
17492 for (i = first - 1; i >= 0; --i)
17493 {
17494 int left, right;
17495 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17496 if (x + right > 0)
17497 k = i;
17498 x -= glyphs[i].pixel_width;
17499 }
17500
17501 return k;
17502 }
17503
17504
17505 /* Return the index of the last glyph following glyph string S that is
17506 not overwritten by S because of S's right overhang. Value is -1 if
17507 no such glyph is found. */
17508
17509 static int
17510 right_overwritten (s)
17511 struct glyph_string *s;
17512 {
17513 int k = -1;
17514
17515 if (s->right_overhang)
17516 {
17517 int x = 0, i;
17518 struct glyph *glyphs = s->row->glyphs[s->area];
17519 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17520 int end = s->row->used[s->area];
17521
17522 for (i = first; i < end && s->right_overhang > x; ++i)
17523 x += glyphs[i].pixel_width;
17524
17525 k = i;
17526 }
17527
17528 return k;
17529 }
17530
17531
17532 /* Return the index of the last glyph following glyph string S that
17533 overwrites S because of its left overhang. Value is negative
17534 if no such glyph is found. */
17535
17536 static int
17537 right_overwriting (s)
17538 struct glyph_string *s;
17539 {
17540 int i, k, x;
17541 int end = s->row->used[s->area];
17542 struct glyph *glyphs = s->row->glyphs[s->area];
17543 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17544
17545 k = -1;
17546 x = 0;
17547 for (i = first; i < end; ++i)
17548 {
17549 int left, right;
17550 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17551 if (x - left < 0)
17552 k = i;
17553 x += glyphs[i].pixel_width;
17554 }
17555
17556 return k;
17557 }
17558
17559
17560 /* Get face and two-byte form of character C in face FACE_ID on frame
17561 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17562 means we want to display multibyte text. DISPLAY_P non-zero means
17563 make sure that X resources for the face returned are allocated.
17564 Value is a pointer to a realized face that is ready for display if
17565 DISPLAY_P is non-zero. */
17566
17567 static INLINE struct face *
17568 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17569 struct frame *f;
17570 int c, face_id;
17571 XChar2b *char2b;
17572 int multibyte_p, display_p;
17573 {
17574 struct face *face = FACE_FROM_ID (f, face_id);
17575
17576 if (!multibyte_p)
17577 {
17578 /* Unibyte case. We don't have to encode, but we have to make
17579 sure to use a face suitable for unibyte. */
17580 STORE_XCHAR2B (char2b, 0, c);
17581 face_id = FACE_FOR_CHAR (f, face, c);
17582 face = FACE_FROM_ID (f, face_id);
17583 }
17584 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17585 {
17586 /* Case of ASCII in a face known to fit ASCII. */
17587 STORE_XCHAR2B (char2b, 0, c);
17588 }
17589 else
17590 {
17591 int c1, c2, charset;
17592
17593 /* Split characters into bytes. If c2 is -1 afterwards, C is
17594 really a one-byte character so that byte1 is zero. */
17595 SPLIT_CHAR (c, charset, c1, c2);
17596 if (c2 > 0)
17597 STORE_XCHAR2B (char2b, c1, c2);
17598 else
17599 STORE_XCHAR2B (char2b, 0, c1);
17600
17601 /* Maybe encode the character in *CHAR2B. */
17602 if (face->font != NULL)
17603 {
17604 struct font_info *font_info
17605 = FONT_INFO_FROM_ID (f, face->font_info_id);
17606 if (font_info)
17607 rif->encode_char (c, char2b, font_info, 0);
17608 }
17609 }
17610
17611 /* Make sure X resources of the face are allocated. */
17612 #ifdef HAVE_X_WINDOWS
17613 if (display_p)
17614 #endif
17615 {
17616 xassert (face != NULL);
17617 PREPARE_FACE_FOR_DISPLAY (f, face);
17618 }
17619
17620 return face;
17621 }
17622
17623
17624 /* Set background width of glyph string S. START is the index of the
17625 first glyph following S. LAST_X is the right-most x-position + 1
17626 in the drawing area. */
17627
17628 static INLINE void
17629 set_glyph_string_background_width (s, start, last_x)
17630 struct glyph_string *s;
17631 int start;
17632 int last_x;
17633 {
17634 /* If the face of this glyph string has to be drawn to the end of
17635 the drawing area, set S->extends_to_end_of_line_p. */
17636 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17637
17638 if (start == s->row->used[s->area]
17639 && s->area == TEXT_AREA
17640 && ((s->hl == DRAW_NORMAL_TEXT
17641 && (s->row->fill_line_p
17642 || s->face->background != default_face->background
17643 || s->face->stipple != default_face->stipple
17644 || s->row->mouse_face_p))
17645 || s->hl == DRAW_MOUSE_FACE
17646 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17647 && s->row->fill_line_p)))
17648 s->extends_to_end_of_line_p = 1;
17649
17650 /* If S extends its face to the end of the line, set its
17651 background_width to the distance to the right edge of the drawing
17652 area. */
17653 if (s->extends_to_end_of_line_p)
17654 s->background_width = last_x - s->x + 1;
17655 else
17656 s->background_width = s->width;
17657 }
17658
17659
17660 /* Compute overhangs and x-positions for glyph string S and its
17661 predecessors, or successors. X is the starting x-position for S.
17662 BACKWARD_P non-zero means process predecessors. */
17663
17664 static void
17665 compute_overhangs_and_x (s, x, backward_p)
17666 struct glyph_string *s;
17667 int x;
17668 int backward_p;
17669 {
17670 if (backward_p)
17671 {
17672 while (s)
17673 {
17674 if (rif->compute_glyph_string_overhangs)
17675 rif->compute_glyph_string_overhangs (s);
17676 x -= s->width;
17677 s->x = x;
17678 s = s->prev;
17679 }
17680 }
17681 else
17682 {
17683 while (s)
17684 {
17685 if (rif->compute_glyph_string_overhangs)
17686 rif->compute_glyph_string_overhangs (s);
17687 s->x = x;
17688 x += s->width;
17689 s = s->next;
17690 }
17691 }
17692 }
17693
17694
17695
17696 /* The following macros are only called from draw_glyphs below.
17697 They reference the following parameters of that function directly:
17698 `w', `row', `area', and `overlap_p'
17699 as well as the following local variables:
17700 `s', `f', and `hdc' (in W32) */
17701
17702 #ifdef HAVE_NTGUI
17703 /* On W32, silently add local `hdc' variable to argument list of
17704 init_glyph_string. */
17705 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17706 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17707 #else
17708 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17709 init_glyph_string (s, char2b, w, row, area, start, hl)
17710 #endif
17711
17712 /* Add a glyph string for a stretch glyph to the list of strings
17713 between HEAD and TAIL. START is the index of the stretch glyph in
17714 row area AREA of glyph row ROW. END is the index of the last glyph
17715 in that glyph row area. X is the current output position assigned
17716 to the new glyph string constructed. HL overrides that face of the
17717 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17718 is the right-most x-position of the drawing area. */
17719
17720 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17721 and below -- keep them on one line. */
17722 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17723 do \
17724 { \
17725 s = (struct glyph_string *) alloca (sizeof *s); \
17726 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17727 START = fill_stretch_glyph_string (s, row, area, START, END); \
17728 append_glyph_string (&HEAD, &TAIL, s); \
17729 s->x = (X); \
17730 } \
17731 while (0)
17732
17733
17734 /* Add a glyph string for an image glyph to the list of strings
17735 between HEAD and TAIL. START is the index of the image glyph in
17736 row area AREA of glyph row ROW. END is the index of the last glyph
17737 in that glyph row area. X is the current output position assigned
17738 to the new glyph string constructed. HL overrides that face of the
17739 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17740 is the right-most x-position of the drawing area. */
17741
17742 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17743 do \
17744 { \
17745 s = (struct glyph_string *) alloca (sizeof *s); \
17746 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17747 fill_image_glyph_string (s); \
17748 append_glyph_string (&HEAD, &TAIL, s); \
17749 ++START; \
17750 s->x = (X); \
17751 } \
17752 while (0)
17753
17754
17755 /* Add a glyph string for a sequence of character glyphs to the list
17756 of strings between HEAD and TAIL. START is the index of the first
17757 glyph in row area AREA of glyph row ROW that is part of the new
17758 glyph string. END is the index of the last glyph in that glyph row
17759 area. X is the current output position assigned to the new glyph
17760 string constructed. HL overrides that face of the glyph; e.g. it
17761 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17762 right-most x-position of the drawing area. */
17763
17764 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17765 do \
17766 { \
17767 int c, face_id; \
17768 XChar2b *char2b; \
17769 \
17770 c = (row)->glyphs[area][START].u.ch; \
17771 face_id = (row)->glyphs[area][START].face_id; \
17772 \
17773 s = (struct glyph_string *) alloca (sizeof *s); \
17774 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17775 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17776 append_glyph_string (&HEAD, &TAIL, s); \
17777 s->x = (X); \
17778 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17779 } \
17780 while (0)
17781
17782
17783 /* Add a glyph string for a composite sequence to the list of strings
17784 between HEAD and TAIL. START is the index of the first glyph in
17785 row area AREA of glyph row ROW that is part of the new glyph
17786 string. END is the index of the last glyph in that glyph row area.
17787 X is the current output position assigned to the new glyph string
17788 constructed. HL overrides that face of the glyph; e.g. it is
17789 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17790 x-position of the drawing area. */
17791
17792 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17793 do { \
17794 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17795 int face_id = (row)->glyphs[area][START].face_id; \
17796 struct face *base_face = FACE_FROM_ID (f, face_id); \
17797 struct composition *cmp = composition_table[cmp_id]; \
17798 int glyph_len = cmp->glyph_len; \
17799 XChar2b *char2b; \
17800 struct face **faces; \
17801 struct glyph_string *first_s = NULL; \
17802 int n; \
17803 \
17804 base_face = base_face->ascii_face; \
17805 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17806 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17807 /* At first, fill in `char2b' and `faces'. */ \
17808 for (n = 0; n < glyph_len; n++) \
17809 { \
17810 int c = COMPOSITION_GLYPH (cmp, n); \
17811 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17812 faces[n] = FACE_FROM_ID (f, this_face_id); \
17813 get_char_face_and_encoding (f, c, this_face_id, \
17814 char2b + n, 1, 1); \
17815 } \
17816 \
17817 /* Make glyph_strings for each glyph sequence that is drawable by \
17818 the same face, and append them to HEAD/TAIL. */ \
17819 for (n = 0; n < cmp->glyph_len;) \
17820 { \
17821 s = (struct glyph_string *) alloca (sizeof *s); \
17822 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17823 append_glyph_string (&(HEAD), &(TAIL), s); \
17824 s->cmp = cmp; \
17825 s->gidx = n; \
17826 s->x = (X); \
17827 \
17828 if (n == 0) \
17829 first_s = s; \
17830 \
17831 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17832 } \
17833 \
17834 ++START; \
17835 s = first_s; \
17836 } while (0)
17837
17838
17839 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17840 of AREA of glyph row ROW on window W between indices START and END.
17841 HL overrides the face for drawing glyph strings, e.g. it is
17842 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17843 x-positions of the drawing area.
17844
17845 This is an ugly monster macro construct because we must use alloca
17846 to allocate glyph strings (because draw_glyphs can be called
17847 asynchronously). */
17848
17849 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17850 do \
17851 { \
17852 HEAD = TAIL = NULL; \
17853 while (START < END) \
17854 { \
17855 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17856 switch (first_glyph->type) \
17857 { \
17858 case CHAR_GLYPH: \
17859 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17860 HL, X, LAST_X); \
17861 break; \
17862 \
17863 case COMPOSITE_GLYPH: \
17864 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17865 HL, X, LAST_X); \
17866 break; \
17867 \
17868 case STRETCH_GLYPH: \
17869 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17870 HL, X, LAST_X); \
17871 break; \
17872 \
17873 case IMAGE_GLYPH: \
17874 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17875 HL, X, LAST_X); \
17876 break; \
17877 \
17878 default: \
17879 abort (); \
17880 } \
17881 \
17882 set_glyph_string_background_width (s, START, LAST_X); \
17883 (X) += s->width; \
17884 } \
17885 } \
17886 while (0)
17887
17888
17889 /* Draw glyphs between START and END in AREA of ROW on window W,
17890 starting at x-position X. X is relative to AREA in W. HL is a
17891 face-override with the following meaning:
17892
17893 DRAW_NORMAL_TEXT draw normally
17894 DRAW_CURSOR draw in cursor face
17895 DRAW_MOUSE_FACE draw in mouse face.
17896 DRAW_INVERSE_VIDEO draw in mode line face
17897 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17898 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17899
17900 If OVERLAPS_P is non-zero, draw only the foreground of characters
17901 and clip to the physical height of ROW.
17902
17903 Value is the x-position reached, relative to AREA of W. */
17904
17905 static int
17906 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17907 struct window *w;
17908 int x;
17909 struct glyph_row *row;
17910 enum glyph_row_area area;
17911 int start, end;
17912 enum draw_glyphs_face hl;
17913 int overlaps_p;
17914 {
17915 struct glyph_string *head, *tail;
17916 struct glyph_string *s;
17917 int last_x, area_width;
17918 int x_reached;
17919 int i, j;
17920 struct frame *f = XFRAME (WINDOW_FRAME (w));
17921 DECLARE_HDC (hdc);
17922
17923 ALLOCATE_HDC (hdc, f);
17924
17925 /* Let's rather be paranoid than getting a SEGV. */
17926 end = min (end, row->used[area]);
17927 start = max (0, start);
17928 start = min (end, start);
17929
17930 /* Translate X to frame coordinates. Set last_x to the right
17931 end of the drawing area. */
17932 if (row->full_width_p)
17933 {
17934 /* X is relative to the left edge of W, without scroll bars
17935 or fringes. */
17936 x += WINDOW_LEFT_EDGE_X (w);
17937 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17938 }
17939 else
17940 {
17941 int area_left = window_box_left (w, area);
17942 x += area_left;
17943 area_width = window_box_width (w, area);
17944 last_x = area_left + area_width;
17945 }
17946
17947 /* Build a doubly-linked list of glyph_string structures between
17948 head and tail from what we have to draw. Note that the macro
17949 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17950 the reason we use a separate variable `i'. */
17951 i = start;
17952 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17953 if (tail)
17954 x_reached = tail->x + tail->background_width;
17955 else
17956 x_reached = x;
17957
17958 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17959 the row, redraw some glyphs in front or following the glyph
17960 strings built above. */
17961 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17962 {
17963 int dummy_x = 0;
17964 struct glyph_string *h, *t;
17965
17966 /* Compute overhangs for all glyph strings. */
17967 if (rif->compute_glyph_string_overhangs)
17968 for (s = head; s; s = s->next)
17969 rif->compute_glyph_string_overhangs (s);
17970
17971 /* Prepend glyph strings for glyphs in front of the first glyph
17972 string that are overwritten because of the first glyph
17973 string's left overhang. The background of all strings
17974 prepended must be drawn because the first glyph string
17975 draws over it. */
17976 i = left_overwritten (head);
17977 if (i >= 0)
17978 {
17979 j = i;
17980 BUILD_GLYPH_STRINGS (j, start, h, t,
17981 DRAW_NORMAL_TEXT, dummy_x, last_x);
17982 start = i;
17983 compute_overhangs_and_x (t, head->x, 1);
17984 prepend_glyph_string_lists (&head, &tail, h, t);
17985 }
17986
17987 /* Prepend glyph strings for glyphs in front of the first glyph
17988 string that overwrite that glyph string because of their
17989 right overhang. For these strings, only the foreground must
17990 be drawn, because it draws over the glyph string at `head'.
17991 The background must not be drawn because this would overwrite
17992 right overhangs of preceding glyphs for which no glyph
17993 strings exist. */
17994 i = left_overwriting (head);
17995 if (i >= 0)
17996 {
17997 BUILD_GLYPH_STRINGS (i, start, h, t,
17998 DRAW_NORMAL_TEXT, dummy_x, last_x);
17999 for (s = h; s; s = s->next)
18000 s->background_filled_p = 1;
18001 compute_overhangs_and_x (t, head->x, 1);
18002 prepend_glyph_string_lists (&head, &tail, h, t);
18003 }
18004
18005 /* Append glyphs strings for glyphs following the last glyph
18006 string tail that are overwritten by tail. The background of
18007 these strings has to be drawn because tail's foreground draws
18008 over it. */
18009 i = right_overwritten (tail);
18010 if (i >= 0)
18011 {
18012 BUILD_GLYPH_STRINGS (end, i, h, t,
18013 DRAW_NORMAL_TEXT, x, last_x);
18014 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18015 append_glyph_string_lists (&head, &tail, h, t);
18016 }
18017
18018 /* Append glyph strings for glyphs following the last glyph
18019 string tail that overwrite tail. The foreground of such
18020 glyphs has to be drawn because it writes into the background
18021 of tail. The background must not be drawn because it could
18022 paint over the foreground of following glyphs. */
18023 i = right_overwriting (tail);
18024 if (i >= 0)
18025 {
18026 BUILD_GLYPH_STRINGS (end, i, h, t,
18027 DRAW_NORMAL_TEXT, x, last_x);
18028 for (s = h; s; s = s->next)
18029 s->background_filled_p = 1;
18030 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18031 append_glyph_string_lists (&head, &tail, h, t);
18032 }
18033 }
18034
18035 /* Draw all strings. */
18036 for (s = head; s; s = s->next)
18037 rif->draw_glyph_string (s);
18038
18039 if (area == TEXT_AREA
18040 && !row->full_width_p
18041 /* When drawing overlapping rows, only the glyph strings'
18042 foreground is drawn, which doesn't erase a cursor
18043 completely. */
18044 && !overlaps_p)
18045 {
18046 int x0 = head ? head->x : x;
18047 int x1 = tail ? tail->x + tail->background_width : x;
18048
18049 int text_left = window_box_left (w, TEXT_AREA);
18050 x0 -= text_left;
18051 x1 -= text_left;
18052
18053 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18054 row->y, MATRIX_ROW_BOTTOM_Y (row));
18055 }
18056
18057 /* Value is the x-position up to which drawn, relative to AREA of W.
18058 This doesn't include parts drawn because of overhangs. */
18059 if (row->full_width_p)
18060 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18061 else
18062 x_reached -= window_box_left (w, area);
18063
18064 RELEASE_HDC (hdc, f);
18065
18066 return x_reached;
18067 }
18068
18069
18070 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18071 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18072
18073 static INLINE void
18074 append_glyph (it)
18075 struct it *it;
18076 {
18077 struct glyph *glyph;
18078 enum glyph_row_area area = it->area;
18079
18080 xassert (it->glyph_row);
18081 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18082
18083 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18084 if (glyph < it->glyph_row->glyphs[area + 1])
18085 {
18086 glyph->charpos = CHARPOS (it->position);
18087 glyph->object = it->object;
18088 glyph->pixel_width = it->pixel_width;
18089 glyph->ascent = it->ascent;
18090 glyph->descent = it->descent;
18091 glyph->voffset = it->voffset;
18092 glyph->type = CHAR_GLYPH;
18093 glyph->multibyte_p = it->multibyte_p;
18094 glyph->left_box_line_p = it->start_of_box_run_p;
18095 glyph->right_box_line_p = it->end_of_box_run_p;
18096 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18097 || it->phys_descent > it->descent);
18098 glyph->padding_p = 0;
18099 glyph->glyph_not_available_p = it->glyph_not_available_p;
18100 glyph->face_id = it->face_id;
18101 glyph->u.ch = it->char_to_display;
18102 glyph->slice = null_glyph_slice;
18103 glyph->font_type = FONT_TYPE_UNKNOWN;
18104 ++it->glyph_row->used[area];
18105 }
18106 }
18107
18108 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18109 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18110
18111 static INLINE void
18112 append_composite_glyph (it)
18113 struct it *it;
18114 {
18115 struct glyph *glyph;
18116 enum glyph_row_area area = it->area;
18117
18118 xassert (it->glyph_row);
18119
18120 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18121 if (glyph < it->glyph_row->glyphs[area + 1])
18122 {
18123 glyph->charpos = CHARPOS (it->position);
18124 glyph->object = it->object;
18125 glyph->pixel_width = it->pixel_width;
18126 glyph->ascent = it->ascent;
18127 glyph->descent = it->descent;
18128 glyph->voffset = it->voffset;
18129 glyph->type = COMPOSITE_GLYPH;
18130 glyph->multibyte_p = it->multibyte_p;
18131 glyph->left_box_line_p = it->start_of_box_run_p;
18132 glyph->right_box_line_p = it->end_of_box_run_p;
18133 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18134 || it->phys_descent > it->descent);
18135 glyph->padding_p = 0;
18136 glyph->glyph_not_available_p = 0;
18137 glyph->face_id = it->face_id;
18138 glyph->u.cmp_id = it->cmp_id;
18139 glyph->slice = null_glyph_slice;
18140 glyph->font_type = FONT_TYPE_UNKNOWN;
18141 ++it->glyph_row->used[area];
18142 }
18143 }
18144
18145
18146 /* Change IT->ascent and IT->height according to the setting of
18147 IT->voffset. */
18148
18149 static INLINE void
18150 take_vertical_position_into_account (it)
18151 struct it *it;
18152 {
18153 if (it->voffset)
18154 {
18155 if (it->voffset < 0)
18156 /* Increase the ascent so that we can display the text higher
18157 in the line. */
18158 it->ascent -= it->voffset;
18159 else
18160 /* Increase the descent so that we can display the text lower
18161 in the line. */
18162 it->descent += it->voffset;
18163 }
18164 }
18165
18166
18167 /* Produce glyphs/get display metrics for the image IT is loaded with.
18168 See the description of struct display_iterator in dispextern.h for
18169 an overview of struct display_iterator. */
18170
18171 static void
18172 produce_image_glyph (it)
18173 struct it *it;
18174 {
18175 struct image *img;
18176 struct face *face;
18177 int face_ascent, glyph_ascent;
18178 struct glyph_slice slice;
18179
18180 xassert (it->what == IT_IMAGE);
18181
18182 face = FACE_FROM_ID (it->f, it->face_id);
18183 xassert (face);
18184 /* Make sure X resources of the face is loaded. */
18185 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18186
18187 if (it->image_id < 0)
18188 {
18189 /* Fringe bitmap. */
18190 it->ascent = it->phys_ascent = 0;
18191 it->descent = it->phys_descent = 0;
18192 it->pixel_width = 0;
18193 it->nglyphs = 0;
18194 return;
18195 }
18196
18197 img = IMAGE_FROM_ID (it->f, it->image_id);
18198 xassert (img);
18199 /* Make sure X resources of the image is loaded. */
18200 prepare_image_for_display (it->f, img);
18201
18202 slice.x = slice.y = 0;
18203 slice.width = img->width;
18204 slice.height = img->height;
18205
18206 if (INTEGERP (it->slice.x))
18207 slice.x = XINT (it->slice.x);
18208 else if (FLOATP (it->slice.x))
18209 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18210
18211 if (INTEGERP (it->slice.y))
18212 slice.y = XINT (it->slice.y);
18213 else if (FLOATP (it->slice.y))
18214 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18215
18216 if (INTEGERP (it->slice.width))
18217 slice.width = XINT (it->slice.width);
18218 else if (FLOATP (it->slice.width))
18219 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18220
18221 if (INTEGERP (it->slice.height))
18222 slice.height = XINT (it->slice.height);
18223 else if (FLOATP (it->slice.height))
18224 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18225
18226 if (slice.x >= img->width)
18227 slice.x = img->width;
18228 if (slice.y >= img->height)
18229 slice.y = img->height;
18230 if (slice.x + slice.width >= img->width)
18231 slice.width = img->width - slice.x;
18232 if (slice.y + slice.height > img->height)
18233 slice.height = img->height - slice.y;
18234
18235 if (slice.width == 0 || slice.height == 0)
18236 return;
18237
18238 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18239
18240 it->descent = slice.height - glyph_ascent;
18241 if (slice.y == 0)
18242 it->descent += img->vmargin;
18243 if (slice.y + slice.height == img->height)
18244 it->descent += img->vmargin;
18245 it->phys_descent = it->descent;
18246
18247 it->pixel_width = slice.width;
18248 if (slice.x == 0)
18249 it->pixel_width += img->hmargin;
18250 if (slice.x + slice.width == img->width)
18251 it->pixel_width += img->hmargin;
18252
18253 /* It's quite possible for images to have an ascent greater than
18254 their height, so don't get confused in that case. */
18255 if (it->descent < 0)
18256 it->descent = 0;
18257
18258 #if 0 /* this breaks image tiling */
18259 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18260 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18261 if (face_ascent > it->ascent)
18262 it->ascent = it->phys_ascent = face_ascent;
18263 #endif
18264
18265 it->nglyphs = 1;
18266
18267 if (face->box != FACE_NO_BOX)
18268 {
18269 if (face->box_line_width > 0)
18270 {
18271 if (slice.y == 0)
18272 it->ascent += face->box_line_width;
18273 if (slice.y + slice.height == img->height)
18274 it->descent += face->box_line_width;
18275 }
18276
18277 if (it->start_of_box_run_p && slice.x == 0)
18278 it->pixel_width += abs (face->box_line_width);
18279 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18280 it->pixel_width += abs (face->box_line_width);
18281 }
18282
18283 take_vertical_position_into_account (it);
18284
18285 if (it->glyph_row)
18286 {
18287 struct glyph *glyph;
18288 enum glyph_row_area area = it->area;
18289
18290 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18291 if (glyph < it->glyph_row->glyphs[area + 1])
18292 {
18293 glyph->charpos = CHARPOS (it->position);
18294 glyph->object = it->object;
18295 glyph->pixel_width = it->pixel_width;
18296 glyph->ascent = glyph_ascent;
18297 glyph->descent = it->descent;
18298 glyph->voffset = it->voffset;
18299 glyph->type = IMAGE_GLYPH;
18300 glyph->multibyte_p = it->multibyte_p;
18301 glyph->left_box_line_p = it->start_of_box_run_p;
18302 glyph->right_box_line_p = it->end_of_box_run_p;
18303 glyph->overlaps_vertically_p = 0;
18304 glyph->padding_p = 0;
18305 glyph->glyph_not_available_p = 0;
18306 glyph->face_id = it->face_id;
18307 glyph->u.img_id = img->id;
18308 glyph->slice = slice;
18309 glyph->font_type = FONT_TYPE_UNKNOWN;
18310 ++it->glyph_row->used[area];
18311 }
18312 }
18313 }
18314
18315
18316 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18317 of the glyph, WIDTH and HEIGHT are the width and height of the
18318 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18319
18320 static void
18321 append_stretch_glyph (it, object, width, height, ascent)
18322 struct it *it;
18323 Lisp_Object object;
18324 int width, height;
18325 int ascent;
18326 {
18327 struct glyph *glyph;
18328 enum glyph_row_area area = it->area;
18329
18330 xassert (ascent >= 0 && ascent <= height);
18331
18332 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18333 if (glyph < it->glyph_row->glyphs[area + 1])
18334 {
18335 glyph->charpos = CHARPOS (it->position);
18336 glyph->object = object;
18337 glyph->pixel_width = width;
18338 glyph->ascent = ascent;
18339 glyph->descent = height - ascent;
18340 glyph->voffset = it->voffset;
18341 glyph->type = STRETCH_GLYPH;
18342 glyph->multibyte_p = it->multibyte_p;
18343 glyph->left_box_line_p = it->start_of_box_run_p;
18344 glyph->right_box_line_p = it->end_of_box_run_p;
18345 glyph->overlaps_vertically_p = 0;
18346 glyph->padding_p = 0;
18347 glyph->glyph_not_available_p = 0;
18348 glyph->face_id = it->face_id;
18349 glyph->u.stretch.ascent = ascent;
18350 glyph->u.stretch.height = height;
18351 glyph->slice = null_glyph_slice;
18352 glyph->font_type = FONT_TYPE_UNKNOWN;
18353 ++it->glyph_row->used[area];
18354 }
18355 }
18356
18357
18358 /* Produce a stretch glyph for iterator IT. IT->object is the value
18359 of the glyph property displayed. The value must be a list
18360 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18361 being recognized:
18362
18363 1. `:width WIDTH' specifies that the space should be WIDTH *
18364 canonical char width wide. WIDTH may be an integer or floating
18365 point number.
18366
18367 2. `:relative-width FACTOR' specifies that the width of the stretch
18368 should be computed from the width of the first character having the
18369 `glyph' property, and should be FACTOR times that width.
18370
18371 3. `:align-to HPOS' specifies that the space should be wide enough
18372 to reach HPOS, a value in canonical character units.
18373
18374 Exactly one of the above pairs must be present.
18375
18376 4. `:height HEIGHT' specifies that the height of the stretch produced
18377 should be HEIGHT, measured in canonical character units.
18378
18379 5. `:relative-height FACTOR' specifies that the height of the
18380 stretch should be FACTOR times the height of the characters having
18381 the glyph property.
18382
18383 Either none or exactly one of 4 or 5 must be present.
18384
18385 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18386 of the stretch should be used for the ascent of the stretch.
18387 ASCENT must be in the range 0 <= ASCENT <= 100. */
18388
18389 static void
18390 produce_stretch_glyph (it)
18391 struct it *it;
18392 {
18393 /* (space :width WIDTH :height HEIGHT ...) */
18394 Lisp_Object prop, plist;
18395 int width = 0, height = 0, align_to = -1;
18396 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18397 int ascent = 0;
18398 double tem;
18399 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18400 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18401
18402 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18403
18404 /* List should start with `space'. */
18405 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18406 plist = XCDR (it->object);
18407
18408 /* Compute the width of the stretch. */
18409 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18410 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18411 {
18412 /* Absolute width `:width WIDTH' specified and valid. */
18413 zero_width_ok_p = 1;
18414 width = (int)tem;
18415 }
18416 else if (prop = Fplist_get (plist, QCrelative_width),
18417 NUMVAL (prop) > 0)
18418 {
18419 /* Relative width `:relative-width FACTOR' specified and valid.
18420 Compute the width of the characters having the `glyph'
18421 property. */
18422 struct it it2;
18423 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18424
18425 it2 = *it;
18426 if (it->multibyte_p)
18427 {
18428 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18429 - IT_BYTEPOS (*it));
18430 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18431 }
18432 else
18433 it2.c = *p, it2.len = 1;
18434
18435 it2.glyph_row = NULL;
18436 it2.what = IT_CHARACTER;
18437 x_produce_glyphs (&it2);
18438 width = NUMVAL (prop) * it2.pixel_width;
18439 }
18440 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18441 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18442 {
18443 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18444 align_to = (align_to < 0
18445 ? 0
18446 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18447 else if (align_to < 0)
18448 align_to = window_box_left_offset (it->w, TEXT_AREA);
18449 width = max (0, (int)tem + align_to - it->current_x);
18450 zero_width_ok_p = 1;
18451 }
18452 else
18453 /* Nothing specified -> width defaults to canonical char width. */
18454 width = FRAME_COLUMN_WIDTH (it->f);
18455
18456 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18457 width = 1;
18458
18459 /* Compute height. */
18460 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18461 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18462 {
18463 height = (int)tem;
18464 zero_height_ok_p = 1;
18465 }
18466 else if (prop = Fplist_get (plist, QCrelative_height),
18467 NUMVAL (prop) > 0)
18468 height = FONT_HEIGHT (font) * NUMVAL (prop);
18469 else
18470 height = FONT_HEIGHT (font);
18471
18472 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18473 height = 1;
18474
18475 /* Compute percentage of height used for ascent. If
18476 `:ascent ASCENT' is present and valid, use that. Otherwise,
18477 derive the ascent from the font in use. */
18478 if (prop = Fplist_get (plist, QCascent),
18479 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18480 ascent = height * NUMVAL (prop) / 100.0;
18481 else if (!NILP (prop)
18482 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18483 ascent = min (max (0, (int)tem), height);
18484 else
18485 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18486
18487 if (width > 0 && height > 0 && it->glyph_row)
18488 {
18489 Lisp_Object object = it->stack[it->sp - 1].string;
18490 if (!STRINGP (object))
18491 object = it->w->buffer;
18492 append_stretch_glyph (it, object, width, height, ascent);
18493 }
18494
18495 it->pixel_width = width;
18496 it->ascent = it->phys_ascent = ascent;
18497 it->descent = it->phys_descent = height - it->ascent;
18498 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18499
18500 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18501 {
18502 if (face->box_line_width > 0)
18503 {
18504 it->ascent += face->box_line_width;
18505 it->descent += face->box_line_width;
18506 }
18507
18508 if (it->start_of_box_run_p)
18509 it->pixel_width += abs (face->box_line_width);
18510 if (it->end_of_box_run_p)
18511 it->pixel_width += abs (face->box_line_width);
18512 }
18513
18514 take_vertical_position_into_account (it);
18515 }
18516
18517 /* Calculate line-height and line-spacing properties.
18518 An integer value specifies explicit pixel value.
18519 A float value specifies relative value to current face height.
18520 A cons (float . face-name) specifies relative value to
18521 height of specified face font.
18522
18523 Returns height in pixels, or nil. */
18524
18525 static Lisp_Object
18526 calc_line_height_property (it, prop, font, boff, total)
18527 struct it *it;
18528 Lisp_Object prop;
18529 XFontStruct *font;
18530 int boff, *total;
18531 {
18532 Lisp_Object val;
18533 Lisp_Object face_name = Qnil;
18534 int ascent, descent, height, override;
18535
18536 val = Fget_char_property (make_number (IT_CHARPOS (*it)),
18537 prop, it->object);
18538
18539 if (NILP (val))
18540 return val;
18541
18542 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18543 {
18544 *total = 1;
18545 val = XCDR (val);
18546 }
18547
18548 if (INTEGERP (val))
18549 return val;
18550
18551 if (CONSP (val))
18552 {
18553 face_name = XCDR (val);
18554 val = XCAR (val);
18555 }
18556 else if (SYMBOLP (val))
18557 {
18558 face_name = val;
18559 val = Qnil;
18560 }
18561
18562 override = EQ (prop, Qline_height);
18563
18564 if (NILP (face_name))
18565 {
18566 font = FRAME_FONT (it->f);
18567 boff = FRAME_BASELINE_OFFSET (it->f);
18568 }
18569 else if (EQ (face_name, Qt))
18570 {
18571 override = 0;
18572 }
18573 else
18574 {
18575 int face_id;
18576 struct face *face;
18577 struct font_info *font_info;
18578
18579 face_id = lookup_named_face (it->f, face_name, ' ');
18580 if (face_id < 0)
18581 return make_number (-1);
18582
18583 face = FACE_FROM_ID (it->f, face_id);
18584 font = face->font;
18585 if (font == NULL)
18586 return make_number (-1);
18587
18588 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18589 boff = font_info->baseline_offset;
18590 if (font_info->vertical_centering)
18591 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18592 }
18593
18594 ascent = FONT_BASE (font) + boff;
18595 descent = FONT_DESCENT (font) - boff;
18596
18597 if (override)
18598 {
18599 it->override_ascent = ascent;
18600 it->override_descent = descent;
18601 it->override_boff = boff;
18602 }
18603
18604 height = ascent + descent;
18605 if (FLOATP (val))
18606 height = (int)(XFLOAT_DATA (val) * height);
18607 else if (INTEGERP (val))
18608 height *= XINT (val);
18609
18610 return make_number (height);
18611 }
18612
18613
18614 /* RIF:
18615 Produce glyphs/get display metrics for the display element IT is
18616 loaded with. See the description of struct display_iterator in
18617 dispextern.h for an overview of struct display_iterator. */
18618
18619 void
18620 x_produce_glyphs (it)
18621 struct it *it;
18622 {
18623 int extra_line_spacing = it->extra_line_spacing;
18624
18625 it->glyph_not_available_p = 0;
18626
18627 if (it->what == IT_CHARACTER)
18628 {
18629 XChar2b char2b;
18630 XFontStruct *font;
18631 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18632 XCharStruct *pcm;
18633 int font_not_found_p;
18634 struct font_info *font_info;
18635 int boff; /* baseline offset */
18636 /* We may change it->multibyte_p upon unibyte<->multibyte
18637 conversion. So, save the current value now and restore it
18638 later.
18639
18640 Note: It seems that we don't have to record multibyte_p in
18641 struct glyph because the character code itself tells if or
18642 not the character is multibyte. Thus, in the future, we must
18643 consider eliminating the field `multibyte_p' in the struct
18644 glyph. */
18645 int saved_multibyte_p = it->multibyte_p;
18646
18647 /* Maybe translate single-byte characters to multibyte, or the
18648 other way. */
18649 it->char_to_display = it->c;
18650 if (!ASCII_BYTE_P (it->c))
18651 {
18652 if (unibyte_display_via_language_environment
18653 && SINGLE_BYTE_CHAR_P (it->c)
18654 && (it->c >= 0240
18655 || !NILP (Vnonascii_translation_table)))
18656 {
18657 it->char_to_display = unibyte_char_to_multibyte (it->c);
18658 it->multibyte_p = 1;
18659 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18660 face = FACE_FROM_ID (it->f, it->face_id);
18661 }
18662 else if (!SINGLE_BYTE_CHAR_P (it->c)
18663 && !it->multibyte_p)
18664 {
18665 it->multibyte_p = 1;
18666 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18667 face = FACE_FROM_ID (it->f, it->face_id);
18668 }
18669 }
18670
18671 /* Get font to use. Encode IT->char_to_display. */
18672 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18673 &char2b, it->multibyte_p, 0);
18674 font = face->font;
18675
18676 /* When no suitable font found, use the default font. */
18677 font_not_found_p = font == NULL;
18678 if (font_not_found_p)
18679 {
18680 font = FRAME_FONT (it->f);
18681 boff = FRAME_BASELINE_OFFSET (it->f);
18682 font_info = NULL;
18683 }
18684 else
18685 {
18686 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18687 boff = font_info->baseline_offset;
18688 if (font_info->vertical_centering)
18689 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18690 }
18691
18692 if (it->char_to_display >= ' '
18693 && (!it->multibyte_p || it->char_to_display < 128))
18694 {
18695 /* Either unibyte or ASCII. */
18696 int stretched_p;
18697
18698 it->nglyphs = 1;
18699
18700 pcm = rif->per_char_metric (font, &char2b,
18701 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18702
18703 if (it->override_ascent >= 0)
18704 {
18705 it->ascent = it->override_ascent;
18706 it->descent = it->override_descent;
18707 boff = it->override_boff;
18708 }
18709 else
18710 {
18711 it->ascent = FONT_BASE (font) + boff;
18712 it->descent = FONT_DESCENT (font) - boff;
18713 }
18714
18715 if (pcm)
18716 {
18717 it->phys_ascent = pcm->ascent + boff;
18718 it->phys_descent = pcm->descent - boff;
18719 it->pixel_width = pcm->width;
18720 }
18721 else
18722 {
18723 it->glyph_not_available_p = 1;
18724 it->phys_ascent = it->ascent;
18725 it->phys_descent = it->descent;
18726 it->pixel_width = FONT_WIDTH (font);
18727 }
18728
18729 if (it->constrain_row_ascent_descent_p)
18730 {
18731 if (it->descent > it->max_descent)
18732 {
18733 it->ascent += it->descent - it->max_descent;
18734 it->descent = it->max_descent;
18735 }
18736 if (it->ascent > it->max_ascent)
18737 {
18738 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18739 it->ascent = it->max_ascent;
18740 }
18741 it->phys_ascent = min (it->phys_ascent, it->ascent);
18742 it->phys_descent = min (it->phys_descent, it->descent);
18743 extra_line_spacing = 0;
18744 }
18745
18746 /* If this is a space inside a region of text with
18747 `space-width' property, change its width. */
18748 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18749 if (stretched_p)
18750 it->pixel_width *= XFLOATINT (it->space_width);
18751
18752 /* If face has a box, add the box thickness to the character
18753 height. If character has a box line to the left and/or
18754 right, add the box line width to the character's width. */
18755 if (face->box != FACE_NO_BOX)
18756 {
18757 int thick = face->box_line_width;
18758
18759 if (thick > 0)
18760 {
18761 it->ascent += thick;
18762 it->descent += thick;
18763 }
18764 else
18765 thick = -thick;
18766
18767 if (it->start_of_box_run_p)
18768 it->pixel_width += thick;
18769 if (it->end_of_box_run_p)
18770 it->pixel_width += thick;
18771 }
18772
18773 /* If face has an overline, add the height of the overline
18774 (1 pixel) and a 1 pixel margin to the character height. */
18775 if (face->overline_p)
18776 it->ascent += 2;
18777
18778 if (it->constrain_row_ascent_descent_p)
18779 {
18780 if (it->ascent > it->max_ascent)
18781 it->ascent = it->max_ascent;
18782 if (it->descent > it->max_descent)
18783 it->descent = it->max_descent;
18784 }
18785
18786 take_vertical_position_into_account (it);
18787
18788 /* If we have to actually produce glyphs, do it. */
18789 if (it->glyph_row)
18790 {
18791 if (stretched_p)
18792 {
18793 /* Translate a space with a `space-width' property
18794 into a stretch glyph. */
18795 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18796 / FONT_HEIGHT (font));
18797 append_stretch_glyph (it, it->object, it->pixel_width,
18798 it->ascent + it->descent, ascent);
18799 }
18800 else
18801 append_glyph (it);
18802
18803 /* If characters with lbearing or rbearing are displayed
18804 in this line, record that fact in a flag of the
18805 glyph row. This is used to optimize X output code. */
18806 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18807 it->glyph_row->contains_overlapping_glyphs_p = 1;
18808 }
18809 }
18810 else if (it->char_to_display == '\n')
18811 {
18812 /* A newline has no width but we need the height of the line.
18813 But if previous part of the line set a height, don't
18814 increase that height */
18815
18816 Lisp_Object height;
18817
18818 it->override_ascent = -1;
18819 it->pixel_width = 0;
18820 it->nglyphs = 0;
18821
18822 height = calc_line_height_property(it, Qline_height, font, boff, 0);
18823
18824 if (it->override_ascent >= 0)
18825 {
18826 it->ascent = it->override_ascent;
18827 it->descent = it->override_descent;
18828 boff = it->override_boff;
18829 }
18830 else
18831 {
18832 it->ascent = FONT_BASE (font) + boff;
18833 it->descent = FONT_DESCENT (font) - boff;
18834 }
18835
18836 if (EQ (height, make_number(0)))
18837 {
18838 if (it->descent > it->max_descent)
18839 {
18840 it->ascent += it->descent - it->max_descent;
18841 it->descent = it->max_descent;
18842 }
18843 if (it->ascent > it->max_ascent)
18844 {
18845 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18846 it->ascent = it->max_ascent;
18847 }
18848 it->phys_ascent = min (it->phys_ascent, it->ascent);
18849 it->phys_descent = min (it->phys_descent, it->descent);
18850 it->constrain_row_ascent_descent_p = 1;
18851 extra_line_spacing = 0;
18852 }
18853 else
18854 {
18855 Lisp_Object spacing;
18856 int total = 0;
18857
18858 it->phys_ascent = it->ascent;
18859 it->phys_descent = it->descent;
18860
18861 if ((it->max_ascent > 0 || it->max_descent > 0)
18862 && face->box != FACE_NO_BOX
18863 && face->box_line_width > 0)
18864 {
18865 it->ascent += face->box_line_width;
18866 it->descent += face->box_line_width;
18867 }
18868 if (!NILP (height)
18869 && XINT (height) > it->ascent + it->descent)
18870 it->ascent = XINT (height) - it->descent;
18871
18872 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
18873 if (INTEGERP (spacing))
18874 {
18875 extra_line_spacing = XINT (spacing);
18876 if (total)
18877 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
18878 }
18879 }
18880 }
18881 else if (it->char_to_display == '\t')
18882 {
18883 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18884 int x = it->current_x + it->continuation_lines_width;
18885 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18886
18887 /* If the distance from the current position to the next tab
18888 stop is less than a canonical character width, use the
18889 tab stop after that. */
18890 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18891 next_tab_x += tab_width;
18892
18893 it->pixel_width = next_tab_x - x;
18894 it->nglyphs = 1;
18895 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18896 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18897
18898 if (it->glyph_row)
18899 {
18900 append_stretch_glyph (it, it->object, it->pixel_width,
18901 it->ascent + it->descent, it->ascent);
18902 }
18903 }
18904 else
18905 {
18906 /* A multi-byte character. Assume that the display width of the
18907 character is the width of the character multiplied by the
18908 width of the font. */
18909
18910 /* If we found a font, this font should give us the right
18911 metrics. If we didn't find a font, use the frame's
18912 default font and calculate the width of the character
18913 from the charset width; this is what old redisplay code
18914 did. */
18915
18916 pcm = rif->per_char_metric (font, &char2b,
18917 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18918
18919 if (font_not_found_p || !pcm)
18920 {
18921 int charset = CHAR_CHARSET (it->char_to_display);
18922
18923 it->glyph_not_available_p = 1;
18924 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18925 * CHARSET_WIDTH (charset));
18926 it->phys_ascent = FONT_BASE (font) + boff;
18927 it->phys_descent = FONT_DESCENT (font) - boff;
18928 }
18929 else
18930 {
18931 it->pixel_width = pcm->width;
18932 it->phys_ascent = pcm->ascent + boff;
18933 it->phys_descent = pcm->descent - boff;
18934 if (it->glyph_row
18935 && (pcm->lbearing < 0
18936 || pcm->rbearing > pcm->width))
18937 it->glyph_row->contains_overlapping_glyphs_p = 1;
18938 }
18939 it->nglyphs = 1;
18940 it->ascent = FONT_BASE (font) + boff;
18941 it->descent = FONT_DESCENT (font) - boff;
18942 if (face->box != FACE_NO_BOX)
18943 {
18944 int thick = face->box_line_width;
18945
18946 if (thick > 0)
18947 {
18948 it->ascent += thick;
18949 it->descent += thick;
18950 }
18951 else
18952 thick = - thick;
18953
18954 if (it->start_of_box_run_p)
18955 it->pixel_width += thick;
18956 if (it->end_of_box_run_p)
18957 it->pixel_width += thick;
18958 }
18959
18960 /* If face has an overline, add the height of the overline
18961 (1 pixel) and a 1 pixel margin to the character height. */
18962 if (face->overline_p)
18963 it->ascent += 2;
18964
18965 take_vertical_position_into_account (it);
18966
18967 if (it->glyph_row)
18968 append_glyph (it);
18969 }
18970 it->multibyte_p = saved_multibyte_p;
18971 }
18972 else if (it->what == IT_COMPOSITION)
18973 {
18974 /* Note: A composition is represented as one glyph in the
18975 glyph matrix. There are no padding glyphs. */
18976 XChar2b char2b;
18977 XFontStruct *font;
18978 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18979 XCharStruct *pcm;
18980 int font_not_found_p;
18981 struct font_info *font_info;
18982 int boff; /* baseline offset */
18983 struct composition *cmp = composition_table[it->cmp_id];
18984
18985 /* Maybe translate single-byte characters to multibyte. */
18986 it->char_to_display = it->c;
18987 if (unibyte_display_via_language_environment
18988 && SINGLE_BYTE_CHAR_P (it->c)
18989 && (it->c >= 0240
18990 || (it->c >= 0200
18991 && !NILP (Vnonascii_translation_table))))
18992 {
18993 it->char_to_display = unibyte_char_to_multibyte (it->c);
18994 }
18995
18996 /* Get face and font to use. Encode IT->char_to_display. */
18997 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18998 face = FACE_FROM_ID (it->f, it->face_id);
18999 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19000 &char2b, it->multibyte_p, 0);
19001 font = face->font;
19002
19003 /* When no suitable font found, use the default font. */
19004 font_not_found_p = font == NULL;
19005 if (font_not_found_p)
19006 {
19007 font = FRAME_FONT (it->f);
19008 boff = FRAME_BASELINE_OFFSET (it->f);
19009 font_info = NULL;
19010 }
19011 else
19012 {
19013 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19014 boff = font_info->baseline_offset;
19015 if (font_info->vertical_centering)
19016 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19017 }
19018
19019 /* There are no padding glyphs, so there is only one glyph to
19020 produce for the composition. Important is that pixel_width,
19021 ascent and descent are the values of what is drawn by
19022 draw_glyphs (i.e. the values of the overall glyphs composed). */
19023 it->nglyphs = 1;
19024
19025 /* If we have not yet calculated pixel size data of glyphs of
19026 the composition for the current face font, calculate them
19027 now. Theoretically, we have to check all fonts for the
19028 glyphs, but that requires much time and memory space. So,
19029 here we check only the font of the first glyph. This leads
19030 to incorrect display very rarely, and C-l (recenter) can
19031 correct the display anyway. */
19032 if (cmp->font != (void *) font)
19033 {
19034 /* Ascent and descent of the font of the first character of
19035 this composition (adjusted by baseline offset). Ascent
19036 and descent of overall glyphs should not be less than
19037 them respectively. */
19038 int font_ascent = FONT_BASE (font) + boff;
19039 int font_descent = FONT_DESCENT (font) - boff;
19040 /* Bounding box of the overall glyphs. */
19041 int leftmost, rightmost, lowest, highest;
19042 int i, width, ascent, descent;
19043
19044 cmp->font = (void *) font;
19045
19046 /* Initialize the bounding box. */
19047 if (font_info
19048 && (pcm = rif->per_char_metric (font, &char2b,
19049 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19050 {
19051 width = pcm->width;
19052 ascent = pcm->ascent;
19053 descent = pcm->descent;
19054 }
19055 else
19056 {
19057 width = FONT_WIDTH (font);
19058 ascent = FONT_BASE (font);
19059 descent = FONT_DESCENT (font);
19060 }
19061
19062 rightmost = width;
19063 lowest = - descent + boff;
19064 highest = ascent + boff;
19065 leftmost = 0;
19066
19067 if (font_info
19068 && font_info->default_ascent
19069 && CHAR_TABLE_P (Vuse_default_ascent)
19070 && !NILP (Faref (Vuse_default_ascent,
19071 make_number (it->char_to_display))))
19072 highest = font_info->default_ascent + boff;
19073
19074 /* Draw the first glyph at the normal position. It may be
19075 shifted to right later if some other glyphs are drawn at
19076 the left. */
19077 cmp->offsets[0] = 0;
19078 cmp->offsets[1] = boff;
19079
19080 /* Set cmp->offsets for the remaining glyphs. */
19081 for (i = 1; i < cmp->glyph_len; i++)
19082 {
19083 int left, right, btm, top;
19084 int ch = COMPOSITION_GLYPH (cmp, i);
19085 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19086
19087 face = FACE_FROM_ID (it->f, face_id);
19088 get_char_face_and_encoding (it->f, ch, face->id,
19089 &char2b, it->multibyte_p, 0);
19090 font = face->font;
19091 if (font == NULL)
19092 {
19093 font = FRAME_FONT (it->f);
19094 boff = FRAME_BASELINE_OFFSET (it->f);
19095 font_info = NULL;
19096 }
19097 else
19098 {
19099 font_info
19100 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19101 boff = font_info->baseline_offset;
19102 if (font_info->vertical_centering)
19103 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19104 }
19105
19106 if (font_info
19107 && (pcm = rif->per_char_metric (font, &char2b,
19108 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19109 {
19110 width = pcm->width;
19111 ascent = pcm->ascent;
19112 descent = pcm->descent;
19113 }
19114 else
19115 {
19116 width = FONT_WIDTH (font);
19117 ascent = 1;
19118 descent = 0;
19119 }
19120
19121 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19122 {
19123 /* Relative composition with or without
19124 alternate chars. */
19125 left = (leftmost + rightmost - width) / 2;
19126 btm = - descent + boff;
19127 if (font_info && font_info->relative_compose
19128 && (! CHAR_TABLE_P (Vignore_relative_composition)
19129 || NILP (Faref (Vignore_relative_composition,
19130 make_number (ch)))))
19131 {
19132
19133 if (- descent >= font_info->relative_compose)
19134 /* One extra pixel between two glyphs. */
19135 btm = highest + 1;
19136 else if (ascent <= 0)
19137 /* One extra pixel between two glyphs. */
19138 btm = lowest - 1 - ascent - descent;
19139 }
19140 }
19141 else
19142 {
19143 /* A composition rule is specified by an integer
19144 value that encodes global and new reference
19145 points (GREF and NREF). GREF and NREF are
19146 specified by numbers as below:
19147
19148 0---1---2 -- ascent
19149 | |
19150 | |
19151 | |
19152 9--10--11 -- center
19153 | |
19154 ---3---4---5--- baseline
19155 | |
19156 6---7---8 -- descent
19157 */
19158 int rule = COMPOSITION_RULE (cmp, i);
19159 int gref, nref, grefx, grefy, nrefx, nrefy;
19160
19161 COMPOSITION_DECODE_RULE (rule, gref, nref);
19162 grefx = gref % 3, nrefx = nref % 3;
19163 grefy = gref / 3, nrefy = nref / 3;
19164
19165 left = (leftmost
19166 + grefx * (rightmost - leftmost) / 2
19167 - nrefx * width / 2);
19168 btm = ((grefy == 0 ? highest
19169 : grefy == 1 ? 0
19170 : grefy == 2 ? lowest
19171 : (highest + lowest) / 2)
19172 - (nrefy == 0 ? ascent + descent
19173 : nrefy == 1 ? descent - boff
19174 : nrefy == 2 ? 0
19175 : (ascent + descent) / 2));
19176 }
19177
19178 cmp->offsets[i * 2] = left;
19179 cmp->offsets[i * 2 + 1] = btm + descent;
19180
19181 /* Update the bounding box of the overall glyphs. */
19182 right = left + width;
19183 top = btm + descent + ascent;
19184 if (left < leftmost)
19185 leftmost = left;
19186 if (right > rightmost)
19187 rightmost = right;
19188 if (top > highest)
19189 highest = top;
19190 if (btm < lowest)
19191 lowest = btm;
19192 }
19193
19194 /* If there are glyphs whose x-offsets are negative,
19195 shift all glyphs to the right and make all x-offsets
19196 non-negative. */
19197 if (leftmost < 0)
19198 {
19199 for (i = 0; i < cmp->glyph_len; i++)
19200 cmp->offsets[i * 2] -= leftmost;
19201 rightmost -= leftmost;
19202 }
19203
19204 cmp->pixel_width = rightmost;
19205 cmp->ascent = highest;
19206 cmp->descent = - lowest;
19207 if (cmp->ascent < font_ascent)
19208 cmp->ascent = font_ascent;
19209 if (cmp->descent < font_descent)
19210 cmp->descent = font_descent;
19211 }
19212
19213 it->pixel_width = cmp->pixel_width;
19214 it->ascent = it->phys_ascent = cmp->ascent;
19215 it->descent = it->phys_descent = cmp->descent;
19216
19217 if (face->box != FACE_NO_BOX)
19218 {
19219 int thick = face->box_line_width;
19220
19221 if (thick > 0)
19222 {
19223 it->ascent += thick;
19224 it->descent += thick;
19225 }
19226 else
19227 thick = - thick;
19228
19229 if (it->start_of_box_run_p)
19230 it->pixel_width += thick;
19231 if (it->end_of_box_run_p)
19232 it->pixel_width += thick;
19233 }
19234
19235 /* If face has an overline, add the height of the overline
19236 (1 pixel) and a 1 pixel margin to the character height. */
19237 if (face->overline_p)
19238 it->ascent += 2;
19239
19240 take_vertical_position_into_account (it);
19241
19242 if (it->glyph_row)
19243 append_composite_glyph (it);
19244 }
19245 else if (it->what == IT_IMAGE)
19246 produce_image_glyph (it);
19247 else if (it->what == IT_STRETCH)
19248 produce_stretch_glyph (it);
19249
19250 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19251 because this isn't true for images with `:ascent 100'. */
19252 xassert (it->ascent >= 0 && it->descent >= 0);
19253 if (it->area == TEXT_AREA)
19254 it->current_x += it->pixel_width;
19255
19256 if (extra_line_spacing > 0)
19257 it->descent += extra_line_spacing;
19258
19259 it->max_ascent = max (it->max_ascent, it->ascent);
19260 it->max_descent = max (it->max_descent, it->descent);
19261 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19262 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19263 }
19264
19265 /* EXPORT for RIF:
19266 Output LEN glyphs starting at START at the nominal cursor position.
19267 Advance the nominal cursor over the text. The global variable
19268 updated_window contains the window being updated, updated_row is
19269 the glyph row being updated, and updated_area is the area of that
19270 row being updated. */
19271
19272 void
19273 x_write_glyphs (start, len)
19274 struct glyph *start;
19275 int len;
19276 {
19277 int x, hpos;
19278
19279 xassert (updated_window && updated_row);
19280 BLOCK_INPUT;
19281
19282 /* Write glyphs. */
19283
19284 hpos = start - updated_row->glyphs[updated_area];
19285 x = draw_glyphs (updated_window, output_cursor.x,
19286 updated_row, updated_area,
19287 hpos, hpos + len,
19288 DRAW_NORMAL_TEXT, 0);
19289
19290 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19291 if (updated_area == TEXT_AREA
19292 && updated_window->phys_cursor_on_p
19293 && updated_window->phys_cursor.vpos == output_cursor.vpos
19294 && updated_window->phys_cursor.hpos >= hpos
19295 && updated_window->phys_cursor.hpos < hpos + len)
19296 updated_window->phys_cursor_on_p = 0;
19297
19298 UNBLOCK_INPUT;
19299
19300 /* Advance the output cursor. */
19301 output_cursor.hpos += len;
19302 output_cursor.x = x;
19303 }
19304
19305
19306 /* EXPORT for RIF:
19307 Insert LEN glyphs from START at the nominal cursor position. */
19308
19309 void
19310 x_insert_glyphs (start, len)
19311 struct glyph *start;
19312 int len;
19313 {
19314 struct frame *f;
19315 struct window *w;
19316 int line_height, shift_by_width, shifted_region_width;
19317 struct glyph_row *row;
19318 struct glyph *glyph;
19319 int frame_x, frame_y, hpos;
19320
19321 xassert (updated_window && updated_row);
19322 BLOCK_INPUT;
19323 w = updated_window;
19324 f = XFRAME (WINDOW_FRAME (w));
19325
19326 /* Get the height of the line we are in. */
19327 row = updated_row;
19328 line_height = row->height;
19329
19330 /* Get the width of the glyphs to insert. */
19331 shift_by_width = 0;
19332 for (glyph = start; glyph < start + len; ++glyph)
19333 shift_by_width += glyph->pixel_width;
19334
19335 /* Get the width of the region to shift right. */
19336 shifted_region_width = (window_box_width (w, updated_area)
19337 - output_cursor.x
19338 - shift_by_width);
19339
19340 /* Shift right. */
19341 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19342 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19343
19344 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19345 line_height, shift_by_width);
19346
19347 /* Write the glyphs. */
19348 hpos = start - row->glyphs[updated_area];
19349 draw_glyphs (w, output_cursor.x, row, updated_area,
19350 hpos, hpos + len,
19351 DRAW_NORMAL_TEXT, 0);
19352
19353 /* Advance the output cursor. */
19354 output_cursor.hpos += len;
19355 output_cursor.x += shift_by_width;
19356 UNBLOCK_INPUT;
19357 }
19358
19359
19360 /* EXPORT for RIF:
19361 Erase the current text line from the nominal cursor position
19362 (inclusive) to pixel column TO_X (exclusive). The idea is that
19363 everything from TO_X onward is already erased.
19364
19365 TO_X is a pixel position relative to updated_area of
19366 updated_window. TO_X == -1 means clear to the end of this area. */
19367
19368 void
19369 x_clear_end_of_line (to_x)
19370 int to_x;
19371 {
19372 struct frame *f;
19373 struct window *w = updated_window;
19374 int max_x, min_y, max_y;
19375 int from_x, from_y, to_y;
19376
19377 xassert (updated_window && updated_row);
19378 f = XFRAME (w->frame);
19379
19380 if (updated_row->full_width_p)
19381 max_x = WINDOW_TOTAL_WIDTH (w);
19382 else
19383 max_x = window_box_width (w, updated_area);
19384 max_y = window_text_bottom_y (w);
19385
19386 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19387 of window. For TO_X > 0, truncate to end of drawing area. */
19388 if (to_x == 0)
19389 return;
19390 else if (to_x < 0)
19391 to_x = max_x;
19392 else
19393 to_x = min (to_x, max_x);
19394
19395 to_y = min (max_y, output_cursor.y + updated_row->height);
19396
19397 /* Notice if the cursor will be cleared by this operation. */
19398 if (!updated_row->full_width_p)
19399 notice_overwritten_cursor (w, updated_area,
19400 output_cursor.x, -1,
19401 updated_row->y,
19402 MATRIX_ROW_BOTTOM_Y (updated_row));
19403
19404 from_x = output_cursor.x;
19405
19406 /* Translate to frame coordinates. */
19407 if (updated_row->full_width_p)
19408 {
19409 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19410 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19411 }
19412 else
19413 {
19414 int area_left = window_box_left (w, updated_area);
19415 from_x += area_left;
19416 to_x += area_left;
19417 }
19418
19419 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19420 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19421 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19422
19423 /* Prevent inadvertently clearing to end of the X window. */
19424 if (to_x > from_x && to_y > from_y)
19425 {
19426 BLOCK_INPUT;
19427 rif->clear_frame_area (f, from_x, from_y,
19428 to_x - from_x, to_y - from_y);
19429 UNBLOCK_INPUT;
19430 }
19431 }
19432
19433 #endif /* HAVE_WINDOW_SYSTEM */
19434
19435
19436 \f
19437 /***********************************************************************
19438 Cursor types
19439 ***********************************************************************/
19440
19441 /* Value is the internal representation of the specified cursor type
19442 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19443 of the bar cursor. */
19444
19445 static enum text_cursor_kinds
19446 get_specified_cursor_type (arg, width)
19447 Lisp_Object arg;
19448 int *width;
19449 {
19450 enum text_cursor_kinds type;
19451
19452 if (NILP (arg))
19453 return NO_CURSOR;
19454
19455 if (EQ (arg, Qbox))
19456 return FILLED_BOX_CURSOR;
19457
19458 if (EQ (arg, Qhollow))
19459 return HOLLOW_BOX_CURSOR;
19460
19461 if (EQ (arg, Qbar))
19462 {
19463 *width = 2;
19464 return BAR_CURSOR;
19465 }
19466
19467 if (CONSP (arg)
19468 && EQ (XCAR (arg), Qbar)
19469 && INTEGERP (XCDR (arg))
19470 && XINT (XCDR (arg)) >= 0)
19471 {
19472 *width = XINT (XCDR (arg));
19473 return BAR_CURSOR;
19474 }
19475
19476 if (EQ (arg, Qhbar))
19477 {
19478 *width = 2;
19479 return HBAR_CURSOR;
19480 }
19481
19482 if (CONSP (arg)
19483 && EQ (XCAR (arg), Qhbar)
19484 && INTEGERP (XCDR (arg))
19485 && XINT (XCDR (arg)) >= 0)
19486 {
19487 *width = XINT (XCDR (arg));
19488 return HBAR_CURSOR;
19489 }
19490
19491 /* Treat anything unknown as "hollow box cursor".
19492 It was bad to signal an error; people have trouble fixing
19493 .Xdefaults with Emacs, when it has something bad in it. */
19494 type = HOLLOW_BOX_CURSOR;
19495
19496 return type;
19497 }
19498
19499 /* Set the default cursor types for specified frame. */
19500 void
19501 set_frame_cursor_types (f, arg)
19502 struct frame *f;
19503 Lisp_Object arg;
19504 {
19505 int width;
19506 Lisp_Object tem;
19507
19508 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19509 FRAME_CURSOR_WIDTH (f) = width;
19510
19511 /* By default, set up the blink-off state depending on the on-state. */
19512
19513 tem = Fassoc (arg, Vblink_cursor_alist);
19514 if (!NILP (tem))
19515 {
19516 FRAME_BLINK_OFF_CURSOR (f)
19517 = get_specified_cursor_type (XCDR (tem), &width);
19518 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19519 }
19520 else
19521 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19522 }
19523
19524
19525 /* Return the cursor we want to be displayed in window W. Return
19526 width of bar/hbar cursor through WIDTH arg. Return with
19527 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19528 (i.e. if the `system caret' should track this cursor).
19529
19530 In a mini-buffer window, we want the cursor only to appear if we
19531 are reading input from this window. For the selected window, we
19532 want the cursor type given by the frame parameter or buffer local
19533 setting of cursor-type. If explicitly marked off, draw no cursor.
19534 In all other cases, we want a hollow box cursor. */
19535
19536 static enum text_cursor_kinds
19537 get_window_cursor_type (w, glyph, width, active_cursor)
19538 struct window *w;
19539 struct glyph *glyph;
19540 int *width;
19541 int *active_cursor;
19542 {
19543 struct frame *f = XFRAME (w->frame);
19544 struct buffer *b = XBUFFER (w->buffer);
19545 int cursor_type = DEFAULT_CURSOR;
19546 Lisp_Object alt_cursor;
19547 int non_selected = 0;
19548
19549 *active_cursor = 1;
19550
19551 /* Echo area */
19552 if (cursor_in_echo_area
19553 && FRAME_HAS_MINIBUF_P (f)
19554 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19555 {
19556 if (w == XWINDOW (echo_area_window))
19557 {
19558 *width = FRAME_CURSOR_WIDTH (f);
19559 return FRAME_DESIRED_CURSOR (f);
19560 }
19561
19562 *active_cursor = 0;
19563 non_selected = 1;
19564 }
19565
19566 /* Nonselected window or nonselected frame. */
19567 else if (w != XWINDOW (f->selected_window)
19568 #ifdef HAVE_WINDOW_SYSTEM
19569 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19570 #endif
19571 )
19572 {
19573 *active_cursor = 0;
19574
19575 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19576 return NO_CURSOR;
19577
19578 non_selected = 1;
19579 }
19580
19581 /* Never display a cursor in a window in which cursor-type is nil. */
19582 if (NILP (b->cursor_type))
19583 return NO_CURSOR;
19584
19585 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19586 if (non_selected)
19587 {
19588 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19589 return get_specified_cursor_type (alt_cursor, width);
19590 }
19591
19592 /* Get the normal cursor type for this window. */
19593 if (EQ (b->cursor_type, Qt))
19594 {
19595 cursor_type = FRAME_DESIRED_CURSOR (f);
19596 *width = FRAME_CURSOR_WIDTH (f);
19597 }
19598 else
19599 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19600
19601 /* Use normal cursor if not blinked off. */
19602 if (!w->cursor_off_p)
19603 {
19604 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19605 if (cursor_type == FILLED_BOX_CURSOR)
19606 cursor_type = HOLLOW_BOX_CURSOR;
19607 }
19608 return cursor_type;
19609 }
19610
19611 /* Cursor is blinked off, so determine how to "toggle" it. */
19612
19613 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19614 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19615 return get_specified_cursor_type (XCDR (alt_cursor), width);
19616
19617 /* Then see if frame has specified a specific blink off cursor type. */
19618 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19619 {
19620 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19621 return FRAME_BLINK_OFF_CURSOR (f);
19622 }
19623
19624 #if 0
19625 /* Some people liked having a permanently visible blinking cursor,
19626 while others had very strong opinions against it. So it was
19627 decided to remove it. KFS 2003-09-03 */
19628
19629 /* Finally perform built-in cursor blinking:
19630 filled box <-> hollow box
19631 wide [h]bar <-> narrow [h]bar
19632 narrow [h]bar <-> no cursor
19633 other type <-> no cursor */
19634
19635 if (cursor_type == FILLED_BOX_CURSOR)
19636 return HOLLOW_BOX_CURSOR;
19637
19638 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19639 {
19640 *width = 1;
19641 return cursor_type;
19642 }
19643 #endif
19644
19645 return NO_CURSOR;
19646 }
19647
19648
19649 #ifdef HAVE_WINDOW_SYSTEM
19650
19651 /* Notice when the text cursor of window W has been completely
19652 overwritten by a drawing operation that outputs glyphs in AREA
19653 starting at X0 and ending at X1 in the line starting at Y0 and
19654 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19655 the rest of the line after X0 has been written. Y coordinates
19656 are window-relative. */
19657
19658 static void
19659 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19660 struct window *w;
19661 enum glyph_row_area area;
19662 int x0, y0, x1, y1;
19663 {
19664 int cx0, cx1, cy0, cy1;
19665 struct glyph_row *row;
19666
19667 if (!w->phys_cursor_on_p)
19668 return;
19669 if (area != TEXT_AREA)
19670 return;
19671
19672 row = w->current_matrix->rows + w->phys_cursor.vpos;
19673 if (!row->displays_text_p)
19674 return;
19675
19676 if (row->cursor_in_fringe_p)
19677 {
19678 row->cursor_in_fringe_p = 0;
19679 draw_fringe_bitmap (w, row, 0);
19680 w->phys_cursor_on_p = 0;
19681 return;
19682 }
19683
19684 cx0 = w->phys_cursor.x;
19685 cx1 = cx0 + w->phys_cursor_width;
19686 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19687 return;
19688
19689 /* The cursor image will be completely removed from the
19690 screen if the output area intersects the cursor area in
19691 y-direction. When we draw in [y0 y1[, and some part of
19692 the cursor is at y < y0, that part must have been drawn
19693 before. When scrolling, the cursor is erased before
19694 actually scrolling, so we don't come here. When not
19695 scrolling, the rows above the old cursor row must have
19696 changed, and in this case these rows must have written
19697 over the cursor image.
19698
19699 Likewise if part of the cursor is below y1, with the
19700 exception of the cursor being in the first blank row at
19701 the buffer and window end because update_text_area
19702 doesn't draw that row. (Except when it does, but
19703 that's handled in update_text_area.) */
19704
19705 cy0 = w->phys_cursor.y;
19706 cy1 = cy0 + w->phys_cursor_height;
19707 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19708 return;
19709
19710 w->phys_cursor_on_p = 0;
19711 }
19712
19713 #endif /* HAVE_WINDOW_SYSTEM */
19714
19715 \f
19716 /************************************************************************
19717 Mouse Face
19718 ************************************************************************/
19719
19720 #ifdef HAVE_WINDOW_SYSTEM
19721
19722 /* EXPORT for RIF:
19723 Fix the display of area AREA of overlapping row ROW in window W. */
19724
19725 void
19726 x_fix_overlapping_area (w, row, area)
19727 struct window *w;
19728 struct glyph_row *row;
19729 enum glyph_row_area area;
19730 {
19731 int i, x;
19732
19733 BLOCK_INPUT;
19734
19735 x = 0;
19736 for (i = 0; i < row->used[area];)
19737 {
19738 if (row->glyphs[area][i].overlaps_vertically_p)
19739 {
19740 int start = i, start_x = x;
19741
19742 do
19743 {
19744 x += row->glyphs[area][i].pixel_width;
19745 ++i;
19746 }
19747 while (i < row->used[area]
19748 && row->glyphs[area][i].overlaps_vertically_p);
19749
19750 draw_glyphs (w, start_x, row, area,
19751 start, i,
19752 DRAW_NORMAL_TEXT, 1);
19753 }
19754 else
19755 {
19756 x += row->glyphs[area][i].pixel_width;
19757 ++i;
19758 }
19759 }
19760
19761 UNBLOCK_INPUT;
19762 }
19763
19764
19765 /* EXPORT:
19766 Draw the cursor glyph of window W in glyph row ROW. See the
19767 comment of draw_glyphs for the meaning of HL. */
19768
19769 void
19770 draw_phys_cursor_glyph (w, row, hl)
19771 struct window *w;
19772 struct glyph_row *row;
19773 enum draw_glyphs_face hl;
19774 {
19775 /* If cursor hpos is out of bounds, don't draw garbage. This can
19776 happen in mini-buffer windows when switching between echo area
19777 glyphs and mini-buffer. */
19778 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19779 {
19780 int on_p = w->phys_cursor_on_p;
19781 int x1;
19782 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19783 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19784 hl, 0);
19785 w->phys_cursor_on_p = on_p;
19786
19787 if (hl == DRAW_CURSOR)
19788 w->phys_cursor_width = x1 - w->phys_cursor.x;
19789 /* When we erase the cursor, and ROW is overlapped by other
19790 rows, make sure that these overlapping parts of other rows
19791 are redrawn. */
19792 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19793 {
19794 if (row > w->current_matrix->rows
19795 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19796 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19797
19798 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19799 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19800 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19801 }
19802 }
19803 }
19804
19805
19806 /* EXPORT:
19807 Erase the image of a cursor of window W from the screen. */
19808
19809 void
19810 erase_phys_cursor (w)
19811 struct window *w;
19812 {
19813 struct frame *f = XFRAME (w->frame);
19814 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19815 int hpos = w->phys_cursor.hpos;
19816 int vpos = w->phys_cursor.vpos;
19817 int mouse_face_here_p = 0;
19818 struct glyph_matrix *active_glyphs = w->current_matrix;
19819 struct glyph_row *cursor_row;
19820 struct glyph *cursor_glyph;
19821 enum draw_glyphs_face hl;
19822
19823 /* No cursor displayed or row invalidated => nothing to do on the
19824 screen. */
19825 if (w->phys_cursor_type == NO_CURSOR)
19826 goto mark_cursor_off;
19827
19828 /* VPOS >= active_glyphs->nrows means that window has been resized.
19829 Don't bother to erase the cursor. */
19830 if (vpos >= active_glyphs->nrows)
19831 goto mark_cursor_off;
19832
19833 /* If row containing cursor is marked invalid, there is nothing we
19834 can do. */
19835 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19836 if (!cursor_row->enabled_p)
19837 goto mark_cursor_off;
19838
19839 /* If row is completely invisible, don't attempt to delete a cursor which
19840 isn't there. This can happen if cursor is at top of a window, and
19841 we switch to a buffer with a header line in that window. */
19842 if (cursor_row->visible_height <= 0)
19843 goto mark_cursor_off;
19844
19845 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19846 if (cursor_row->cursor_in_fringe_p)
19847 {
19848 cursor_row->cursor_in_fringe_p = 0;
19849 draw_fringe_bitmap (w, cursor_row, 0);
19850 goto mark_cursor_off;
19851 }
19852
19853 /* This can happen when the new row is shorter than the old one.
19854 In this case, either draw_glyphs or clear_end_of_line
19855 should have cleared the cursor. Note that we wouldn't be
19856 able to erase the cursor in this case because we don't have a
19857 cursor glyph at hand. */
19858 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19859 goto mark_cursor_off;
19860
19861 /* If the cursor is in the mouse face area, redisplay that when
19862 we clear the cursor. */
19863 if (! NILP (dpyinfo->mouse_face_window)
19864 && w == XWINDOW (dpyinfo->mouse_face_window)
19865 && (vpos > dpyinfo->mouse_face_beg_row
19866 || (vpos == dpyinfo->mouse_face_beg_row
19867 && hpos >= dpyinfo->mouse_face_beg_col))
19868 && (vpos < dpyinfo->mouse_face_end_row
19869 || (vpos == dpyinfo->mouse_face_end_row
19870 && hpos < dpyinfo->mouse_face_end_col))
19871 /* Don't redraw the cursor's spot in mouse face if it is at the
19872 end of a line (on a newline). The cursor appears there, but
19873 mouse highlighting does not. */
19874 && cursor_row->used[TEXT_AREA] > hpos)
19875 mouse_face_here_p = 1;
19876
19877 /* Maybe clear the display under the cursor. */
19878 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19879 {
19880 int x, y;
19881 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19882
19883 cursor_glyph = get_phys_cursor_glyph (w);
19884 if (cursor_glyph == NULL)
19885 goto mark_cursor_off;
19886
19887 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19888 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19889
19890 rif->clear_frame_area (f, x, y,
19891 cursor_glyph->pixel_width, cursor_row->visible_height);
19892 }
19893
19894 /* Erase the cursor by redrawing the character underneath it. */
19895 if (mouse_face_here_p)
19896 hl = DRAW_MOUSE_FACE;
19897 else
19898 hl = DRAW_NORMAL_TEXT;
19899 draw_phys_cursor_glyph (w, cursor_row, hl);
19900
19901 mark_cursor_off:
19902 w->phys_cursor_on_p = 0;
19903 w->phys_cursor_type = NO_CURSOR;
19904 }
19905
19906
19907 /* EXPORT:
19908 Display or clear cursor of window W. If ON is zero, clear the
19909 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19910 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19911
19912 void
19913 display_and_set_cursor (w, on, hpos, vpos, x, y)
19914 struct window *w;
19915 int on, hpos, vpos, x, y;
19916 {
19917 struct frame *f = XFRAME (w->frame);
19918 int new_cursor_type;
19919 int new_cursor_width;
19920 int active_cursor;
19921 struct glyph_row *glyph_row;
19922 struct glyph *glyph;
19923
19924 /* This is pointless on invisible frames, and dangerous on garbaged
19925 windows and frames; in the latter case, the frame or window may
19926 be in the midst of changing its size, and x and y may be off the
19927 window. */
19928 if (! FRAME_VISIBLE_P (f)
19929 || FRAME_GARBAGED_P (f)
19930 || vpos >= w->current_matrix->nrows
19931 || hpos >= w->current_matrix->matrix_w)
19932 return;
19933
19934 /* If cursor is off and we want it off, return quickly. */
19935 if (!on && !w->phys_cursor_on_p)
19936 return;
19937
19938 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
19939 /* If cursor row is not enabled, we don't really know where to
19940 display the cursor. */
19941 if (!glyph_row->enabled_p)
19942 {
19943 w->phys_cursor_on_p = 0;
19944 return;
19945 }
19946
19947 glyph = NULL;
19948 if (!glyph_row->exact_window_width_line_p
19949 || hpos < glyph_row->used[TEXT_AREA])
19950 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19951
19952 xassert (interrupt_input_blocked);
19953
19954 /* Set new_cursor_type to the cursor we want to be displayed. */
19955 new_cursor_type = get_window_cursor_type (w, glyph,
19956 &new_cursor_width, &active_cursor);
19957
19958 /* If cursor is currently being shown and we don't want it to be or
19959 it is in the wrong place, or the cursor type is not what we want,
19960 erase it. */
19961 if (w->phys_cursor_on_p
19962 && (!on
19963 || w->phys_cursor.x != x
19964 || w->phys_cursor.y != y
19965 || new_cursor_type != w->phys_cursor_type
19966 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19967 && new_cursor_width != w->phys_cursor_width)))
19968 erase_phys_cursor (w);
19969
19970 /* Don't check phys_cursor_on_p here because that flag is only set
19971 to zero in some cases where we know that the cursor has been
19972 completely erased, to avoid the extra work of erasing the cursor
19973 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19974 still not be visible, or it has only been partly erased. */
19975 if (on)
19976 {
19977 w->phys_cursor_ascent = glyph_row->ascent;
19978 w->phys_cursor_height = glyph_row->height;
19979
19980 /* Set phys_cursor_.* before x_draw_.* is called because some
19981 of them may need the information. */
19982 w->phys_cursor.x = x;
19983 w->phys_cursor.y = glyph_row->y;
19984 w->phys_cursor.hpos = hpos;
19985 w->phys_cursor.vpos = vpos;
19986 }
19987
19988 rif->draw_window_cursor (w, glyph_row, x, y,
19989 new_cursor_type, new_cursor_width,
19990 on, active_cursor);
19991 }
19992
19993
19994 /* Switch the display of W's cursor on or off, according to the value
19995 of ON. */
19996
19997 static void
19998 update_window_cursor (w, on)
19999 struct window *w;
20000 int on;
20001 {
20002 /* Don't update cursor in windows whose frame is in the process
20003 of being deleted. */
20004 if (w->current_matrix)
20005 {
20006 BLOCK_INPUT;
20007 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20008 w->phys_cursor.x, w->phys_cursor.y);
20009 UNBLOCK_INPUT;
20010 }
20011 }
20012
20013
20014 /* Call update_window_cursor with parameter ON_P on all leaf windows
20015 in the window tree rooted at W. */
20016
20017 static void
20018 update_cursor_in_window_tree (w, on_p)
20019 struct window *w;
20020 int on_p;
20021 {
20022 while (w)
20023 {
20024 if (!NILP (w->hchild))
20025 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20026 else if (!NILP (w->vchild))
20027 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20028 else
20029 update_window_cursor (w, on_p);
20030
20031 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20032 }
20033 }
20034
20035
20036 /* EXPORT:
20037 Display the cursor on window W, or clear it, according to ON_P.
20038 Don't change the cursor's position. */
20039
20040 void
20041 x_update_cursor (f, on_p)
20042 struct frame *f;
20043 int on_p;
20044 {
20045 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20046 }
20047
20048
20049 /* EXPORT:
20050 Clear the cursor of window W to background color, and mark the
20051 cursor as not shown. This is used when the text where the cursor
20052 is is about to be rewritten. */
20053
20054 void
20055 x_clear_cursor (w)
20056 struct window *w;
20057 {
20058 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20059 update_window_cursor (w, 0);
20060 }
20061
20062
20063 /* EXPORT:
20064 Display the active region described by mouse_face_* according to DRAW. */
20065
20066 void
20067 show_mouse_face (dpyinfo, draw)
20068 Display_Info *dpyinfo;
20069 enum draw_glyphs_face draw;
20070 {
20071 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20072 struct frame *f = XFRAME (WINDOW_FRAME (w));
20073
20074 if (/* If window is in the process of being destroyed, don't bother
20075 to do anything. */
20076 w->current_matrix != NULL
20077 /* Don't update mouse highlight if hidden */
20078 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20079 /* Recognize when we are called to operate on rows that don't exist
20080 anymore. This can happen when a window is split. */
20081 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20082 {
20083 int phys_cursor_on_p = w->phys_cursor_on_p;
20084 struct glyph_row *row, *first, *last;
20085
20086 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20087 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20088
20089 for (row = first; row <= last && row->enabled_p; ++row)
20090 {
20091 int start_hpos, end_hpos, start_x;
20092
20093 /* For all but the first row, the highlight starts at column 0. */
20094 if (row == first)
20095 {
20096 start_hpos = dpyinfo->mouse_face_beg_col;
20097 start_x = dpyinfo->mouse_face_beg_x;
20098 }
20099 else
20100 {
20101 start_hpos = 0;
20102 start_x = 0;
20103 }
20104
20105 if (row == last)
20106 end_hpos = dpyinfo->mouse_face_end_col;
20107 else
20108 end_hpos = row->used[TEXT_AREA];
20109
20110 if (end_hpos > start_hpos)
20111 {
20112 draw_glyphs (w, start_x, row, TEXT_AREA,
20113 start_hpos, end_hpos,
20114 draw, 0);
20115
20116 row->mouse_face_p
20117 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20118 }
20119 }
20120
20121 /* When we've written over the cursor, arrange for it to
20122 be displayed again. */
20123 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20124 {
20125 BLOCK_INPUT;
20126 display_and_set_cursor (w, 1,
20127 w->phys_cursor.hpos, w->phys_cursor.vpos,
20128 w->phys_cursor.x, w->phys_cursor.y);
20129 UNBLOCK_INPUT;
20130 }
20131 }
20132
20133 /* Change the mouse cursor. */
20134 if (draw == DRAW_NORMAL_TEXT)
20135 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20136 else if (draw == DRAW_MOUSE_FACE)
20137 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20138 else
20139 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20140 }
20141
20142 /* EXPORT:
20143 Clear out the mouse-highlighted active region.
20144 Redraw it un-highlighted first. Value is non-zero if mouse
20145 face was actually drawn unhighlighted. */
20146
20147 int
20148 clear_mouse_face (dpyinfo)
20149 Display_Info *dpyinfo;
20150 {
20151 int cleared = 0;
20152
20153 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20154 {
20155 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20156 cleared = 1;
20157 }
20158
20159 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20160 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20161 dpyinfo->mouse_face_window = Qnil;
20162 dpyinfo->mouse_face_overlay = Qnil;
20163 return cleared;
20164 }
20165
20166
20167 /* EXPORT:
20168 Non-zero if physical cursor of window W is within mouse face. */
20169
20170 int
20171 cursor_in_mouse_face_p (w)
20172 struct window *w;
20173 {
20174 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20175 int in_mouse_face = 0;
20176
20177 if (WINDOWP (dpyinfo->mouse_face_window)
20178 && XWINDOW (dpyinfo->mouse_face_window) == w)
20179 {
20180 int hpos = w->phys_cursor.hpos;
20181 int vpos = w->phys_cursor.vpos;
20182
20183 if (vpos >= dpyinfo->mouse_face_beg_row
20184 && vpos <= dpyinfo->mouse_face_end_row
20185 && (vpos > dpyinfo->mouse_face_beg_row
20186 || hpos >= dpyinfo->mouse_face_beg_col)
20187 && (vpos < dpyinfo->mouse_face_end_row
20188 || hpos < dpyinfo->mouse_face_end_col
20189 || dpyinfo->mouse_face_past_end))
20190 in_mouse_face = 1;
20191 }
20192
20193 return in_mouse_face;
20194 }
20195
20196
20197
20198 \f
20199 /* Find the glyph matrix position of buffer position CHARPOS in window
20200 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20201 current glyphs must be up to date. If CHARPOS is above window
20202 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20203 of last line in W. In the row containing CHARPOS, stop before glyphs
20204 having STOP as object. */
20205
20206 #if 1 /* This is a version of fast_find_position that's more correct
20207 in the presence of hscrolling, for example. I didn't install
20208 it right away because the problem fixed is minor, it failed
20209 in 20.x as well, and I think it's too risky to install
20210 so near the release of 21.1. 2001-09-25 gerd. */
20211
20212 static int
20213 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20214 struct window *w;
20215 int charpos;
20216 int *hpos, *vpos, *x, *y;
20217 Lisp_Object stop;
20218 {
20219 struct glyph_row *row, *first;
20220 struct glyph *glyph, *end;
20221 int past_end = 0;
20222
20223 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20224 row = row_containing_pos (w, charpos, first, NULL, 0);
20225 if (row == NULL)
20226 {
20227 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20228 {
20229 *x = *y = *hpos = *vpos = 0;
20230 return 1;
20231 }
20232 else
20233 {
20234 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20235 past_end = 1;
20236 }
20237 }
20238
20239 *x = row->x;
20240 *y = row->y;
20241 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20242
20243 glyph = row->glyphs[TEXT_AREA];
20244 end = glyph + row->used[TEXT_AREA];
20245
20246 /* Skip over glyphs not having an object at the start of the row.
20247 These are special glyphs like truncation marks on terminal
20248 frames. */
20249 if (row->displays_text_p)
20250 while (glyph < end
20251 && INTEGERP (glyph->object)
20252 && !EQ (stop, glyph->object)
20253 && glyph->charpos < 0)
20254 {
20255 *x += glyph->pixel_width;
20256 ++glyph;
20257 }
20258
20259 while (glyph < end
20260 && !INTEGERP (glyph->object)
20261 && !EQ (stop, glyph->object)
20262 && (!BUFFERP (glyph->object)
20263 || glyph->charpos < charpos))
20264 {
20265 *x += glyph->pixel_width;
20266 ++glyph;
20267 }
20268
20269 *hpos = glyph - row->glyphs[TEXT_AREA];
20270 return !past_end;
20271 }
20272
20273 #else /* not 1 */
20274
20275 static int
20276 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20277 struct window *w;
20278 int pos;
20279 int *hpos, *vpos, *x, *y;
20280 Lisp_Object stop;
20281 {
20282 int i;
20283 int lastcol;
20284 int maybe_next_line_p = 0;
20285 int line_start_position;
20286 int yb = window_text_bottom_y (w);
20287 struct glyph_row *row, *best_row;
20288 int row_vpos, best_row_vpos;
20289 int current_x;
20290
20291 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20292 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20293
20294 while (row->y < yb)
20295 {
20296 if (row->used[TEXT_AREA])
20297 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20298 else
20299 line_start_position = 0;
20300
20301 if (line_start_position > pos)
20302 break;
20303 /* If the position sought is the end of the buffer,
20304 don't include the blank lines at the bottom of the window. */
20305 else if (line_start_position == pos
20306 && pos == BUF_ZV (XBUFFER (w->buffer)))
20307 {
20308 maybe_next_line_p = 1;
20309 break;
20310 }
20311 else if (line_start_position > 0)
20312 {
20313 best_row = row;
20314 best_row_vpos = row_vpos;
20315 }
20316
20317 if (row->y + row->height >= yb)
20318 break;
20319
20320 ++row;
20321 ++row_vpos;
20322 }
20323
20324 /* Find the right column within BEST_ROW. */
20325 lastcol = 0;
20326 current_x = best_row->x;
20327 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20328 {
20329 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20330 int charpos = glyph->charpos;
20331
20332 if (BUFFERP (glyph->object))
20333 {
20334 if (charpos == pos)
20335 {
20336 *hpos = i;
20337 *vpos = best_row_vpos;
20338 *x = current_x;
20339 *y = best_row->y;
20340 return 1;
20341 }
20342 else if (charpos > pos)
20343 break;
20344 }
20345 else if (EQ (glyph->object, stop))
20346 break;
20347
20348 if (charpos > 0)
20349 lastcol = i;
20350 current_x += glyph->pixel_width;
20351 }
20352
20353 /* If we're looking for the end of the buffer,
20354 and we didn't find it in the line we scanned,
20355 use the start of the following line. */
20356 if (maybe_next_line_p)
20357 {
20358 ++best_row;
20359 ++best_row_vpos;
20360 lastcol = 0;
20361 current_x = best_row->x;
20362 }
20363
20364 *vpos = best_row_vpos;
20365 *hpos = lastcol + 1;
20366 *x = current_x;
20367 *y = best_row->y;
20368 return 0;
20369 }
20370
20371 #endif /* not 1 */
20372
20373
20374 /* Find the position of the glyph for position POS in OBJECT in
20375 window W's current matrix, and return in *X, *Y the pixel
20376 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20377
20378 RIGHT_P non-zero means return the position of the right edge of the
20379 glyph, RIGHT_P zero means return the left edge position.
20380
20381 If no glyph for POS exists in the matrix, return the position of
20382 the glyph with the next smaller position that is in the matrix, if
20383 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20384 exists in the matrix, return the position of the glyph with the
20385 next larger position in OBJECT.
20386
20387 Value is non-zero if a glyph was found. */
20388
20389 static int
20390 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20391 struct window *w;
20392 int pos;
20393 Lisp_Object object;
20394 int *hpos, *vpos, *x, *y;
20395 int right_p;
20396 {
20397 int yb = window_text_bottom_y (w);
20398 struct glyph_row *r;
20399 struct glyph *best_glyph = NULL;
20400 struct glyph_row *best_row = NULL;
20401 int best_x = 0;
20402
20403 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20404 r->enabled_p && r->y < yb;
20405 ++r)
20406 {
20407 struct glyph *g = r->glyphs[TEXT_AREA];
20408 struct glyph *e = g + r->used[TEXT_AREA];
20409 int gx;
20410
20411 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20412 if (EQ (g->object, object))
20413 {
20414 if (g->charpos == pos)
20415 {
20416 best_glyph = g;
20417 best_x = gx;
20418 best_row = r;
20419 goto found;
20420 }
20421 else if (best_glyph == NULL
20422 || ((abs (g->charpos - pos)
20423 < abs (best_glyph->charpos - pos))
20424 && (right_p
20425 ? g->charpos < pos
20426 : g->charpos > pos)))
20427 {
20428 best_glyph = g;
20429 best_x = gx;
20430 best_row = r;
20431 }
20432 }
20433 }
20434
20435 found:
20436
20437 if (best_glyph)
20438 {
20439 *x = best_x;
20440 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20441
20442 if (right_p)
20443 {
20444 *x += best_glyph->pixel_width;
20445 ++*hpos;
20446 }
20447
20448 *y = best_row->y;
20449 *vpos = best_row - w->current_matrix->rows;
20450 }
20451
20452 return best_glyph != NULL;
20453 }
20454
20455
20456 /* See if position X, Y is within a hot-spot of an image. */
20457
20458 static int
20459 on_hot_spot_p (hot_spot, x, y)
20460 Lisp_Object hot_spot;
20461 int x, y;
20462 {
20463 if (!CONSP (hot_spot))
20464 return 0;
20465
20466 if (EQ (XCAR (hot_spot), Qrect))
20467 {
20468 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20469 Lisp_Object rect = XCDR (hot_spot);
20470 Lisp_Object tem;
20471 if (!CONSP (rect))
20472 return 0;
20473 if (!CONSP (XCAR (rect)))
20474 return 0;
20475 if (!CONSP (XCDR (rect)))
20476 return 0;
20477 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20478 return 0;
20479 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20480 return 0;
20481 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20482 return 0;
20483 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20484 return 0;
20485 return 1;
20486 }
20487 else if (EQ (XCAR (hot_spot), Qcircle))
20488 {
20489 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20490 Lisp_Object circ = XCDR (hot_spot);
20491 Lisp_Object lr, lx0, ly0;
20492 if (CONSP (circ)
20493 && CONSP (XCAR (circ))
20494 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20495 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20496 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20497 {
20498 double r = XFLOATINT (lr);
20499 double dx = XINT (lx0) - x;
20500 double dy = XINT (ly0) - y;
20501 return (dx * dx + dy * dy <= r * r);
20502 }
20503 }
20504 else if (EQ (XCAR (hot_spot), Qpoly))
20505 {
20506 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20507 if (VECTORP (XCDR (hot_spot)))
20508 {
20509 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20510 Lisp_Object *poly = v->contents;
20511 int n = v->size;
20512 int i;
20513 int inside = 0;
20514 Lisp_Object lx, ly;
20515 int x0, y0;
20516
20517 /* Need an even number of coordinates, and at least 3 edges. */
20518 if (n < 6 || n & 1)
20519 return 0;
20520
20521 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20522 If count is odd, we are inside polygon. Pixels on edges
20523 may or may not be included depending on actual geometry of the
20524 polygon. */
20525 if ((lx = poly[n-2], !INTEGERP (lx))
20526 || (ly = poly[n-1], !INTEGERP (lx)))
20527 return 0;
20528 x0 = XINT (lx), y0 = XINT (ly);
20529 for (i = 0; i < n; i += 2)
20530 {
20531 int x1 = x0, y1 = y0;
20532 if ((lx = poly[i], !INTEGERP (lx))
20533 || (ly = poly[i+1], !INTEGERP (ly)))
20534 return 0;
20535 x0 = XINT (lx), y0 = XINT (ly);
20536
20537 /* Does this segment cross the X line? */
20538 if (x0 >= x)
20539 {
20540 if (x1 >= x)
20541 continue;
20542 }
20543 else if (x1 < x)
20544 continue;
20545 if (y > y0 && y > y1)
20546 continue;
20547 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20548 inside = !inside;
20549 }
20550 return inside;
20551 }
20552 }
20553 /* If we don't understand the format, pretend we're not in the hot-spot. */
20554 return 0;
20555 }
20556
20557 Lisp_Object
20558 find_hot_spot (map, x, y)
20559 Lisp_Object map;
20560 int x, y;
20561 {
20562 while (CONSP (map))
20563 {
20564 if (CONSP (XCAR (map))
20565 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20566 return XCAR (map);
20567 map = XCDR (map);
20568 }
20569
20570 return Qnil;
20571 }
20572
20573 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20574 3, 3, 0,
20575 doc: /* Lookup in image map MAP coordinates X and Y.
20576 An image map is an alist where each element has the format (AREA ID PLIST).
20577 An AREA is specified as either a rectangle, a circle, or a polygon:
20578 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20579 pixel coordinates of the upper left and bottom right corners.
20580 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20581 and the radius of the circle; r may be a float or integer.
20582 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20583 vector describes one corner in the polygon.
20584 Returns the alist element for the first matching AREA in MAP. */)
20585 (map, x, y)
20586 Lisp_Object map;
20587 Lisp_Object x, y;
20588 {
20589 if (NILP (map))
20590 return Qnil;
20591
20592 CHECK_NUMBER (x);
20593 CHECK_NUMBER (y);
20594
20595 return find_hot_spot (map, XINT (x), XINT (y));
20596 }
20597
20598
20599 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20600 static void
20601 define_frame_cursor1 (f, cursor, pointer)
20602 struct frame *f;
20603 Cursor cursor;
20604 Lisp_Object pointer;
20605 {
20606 if (!NILP (pointer))
20607 {
20608 if (EQ (pointer, Qarrow))
20609 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20610 else if (EQ (pointer, Qhand))
20611 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20612 else if (EQ (pointer, Qtext))
20613 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20614 else if (EQ (pointer, intern ("hdrag")))
20615 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20616 #ifdef HAVE_X_WINDOWS
20617 else if (EQ (pointer, intern ("vdrag")))
20618 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20619 #endif
20620 else if (EQ (pointer, intern ("hourglass")))
20621 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20622 else if (EQ (pointer, Qmodeline))
20623 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20624 else
20625 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20626 }
20627
20628 #ifndef HAVE_CARBON
20629 if (cursor != No_Cursor)
20630 #else
20631 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20632 #endif
20633 rif->define_frame_cursor (f, cursor);
20634 }
20635
20636 /* Take proper action when mouse has moved to the mode or header line
20637 or marginal area AREA of window W, x-position X and y-position Y.
20638 X is relative to the start of the text display area of W, so the
20639 width of bitmap areas and scroll bars must be subtracted to get a
20640 position relative to the start of the mode line. */
20641
20642 static void
20643 note_mode_line_or_margin_highlight (w, x, y, area)
20644 struct window *w;
20645 int x, y;
20646 enum window_part area;
20647 {
20648 struct frame *f = XFRAME (w->frame);
20649 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20650 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20651 Lisp_Object pointer = Qnil;
20652 int charpos, dx, dy, width, height;
20653 Lisp_Object string, object = Qnil;
20654 Lisp_Object pos, help;
20655
20656 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20657 string = mode_line_string (w, area, &x, &y, &charpos,
20658 &object, &dx, &dy, &width, &height);
20659 else
20660 {
20661 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20662 string = marginal_area_string (w, area, &x, &y, &charpos,
20663 &object, &dx, &dy, &width, &height);
20664 }
20665
20666 help = Qnil;
20667
20668 if (IMAGEP (object))
20669 {
20670 Lisp_Object image_map, hotspot;
20671 if ((image_map = Fplist_get (XCDR (object), QCmap),
20672 !NILP (image_map))
20673 && (hotspot = find_hot_spot (image_map, dx, dy),
20674 CONSP (hotspot))
20675 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20676 {
20677 Lisp_Object area_id, plist;
20678
20679 area_id = XCAR (hotspot);
20680 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20681 If so, we could look for mouse-enter, mouse-leave
20682 properties in PLIST (and do something...). */
20683 if ((plist = XCDR (hotspot), CONSP (plist)))
20684 {
20685 pointer = Fplist_get (plist, Qpointer);
20686 if (NILP (pointer))
20687 pointer = Qhand;
20688 help = Fplist_get (plist, Qhelp_echo);
20689 if (!NILP (help))
20690 {
20691 help_echo_string = help;
20692 /* Is this correct? ++kfs */
20693 XSETWINDOW (help_echo_window, w);
20694 help_echo_object = w->buffer;
20695 help_echo_pos = charpos;
20696 }
20697 }
20698 if (NILP (pointer))
20699 pointer = Fplist_get (XCDR (object), QCpointer);
20700 }
20701 }
20702
20703 if (STRINGP (string))
20704 {
20705 pos = make_number (charpos);
20706 /* If we're on a string with `help-echo' text property, arrange
20707 for the help to be displayed. This is done by setting the
20708 global variable help_echo_string to the help string. */
20709 help = Fget_text_property (pos, Qhelp_echo, string);
20710 if (!NILP (help))
20711 {
20712 help_echo_string = help;
20713 XSETWINDOW (help_echo_window, w);
20714 help_echo_object = string;
20715 help_echo_pos = charpos;
20716 }
20717
20718 if (NILP (pointer))
20719 pointer = Fget_text_property (pos, Qpointer, string);
20720
20721 /* Change the mouse pointer according to what is under X/Y. */
20722 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20723 {
20724 Lisp_Object map;
20725 map = Fget_text_property (pos, Qlocal_map, string);
20726 if (!KEYMAPP (map))
20727 map = Fget_text_property (pos, Qkeymap, string);
20728 if (!KEYMAPP (map))
20729 cursor = dpyinfo->vertical_scroll_bar_cursor;
20730 }
20731 }
20732
20733 define_frame_cursor1 (f, cursor, pointer);
20734 }
20735
20736
20737 /* EXPORT:
20738 Take proper action when the mouse has moved to position X, Y on
20739 frame F as regards highlighting characters that have mouse-face
20740 properties. Also de-highlighting chars where the mouse was before.
20741 X and Y can be negative or out of range. */
20742
20743 void
20744 note_mouse_highlight (f, x, y)
20745 struct frame *f;
20746 int x, y;
20747 {
20748 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20749 enum window_part part;
20750 Lisp_Object window;
20751 struct window *w;
20752 Cursor cursor = No_Cursor;
20753 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20754 struct buffer *b;
20755
20756 /* When a menu is active, don't highlight because this looks odd. */
20757 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20758 if (popup_activated ())
20759 return;
20760 #endif
20761
20762 if (NILP (Vmouse_highlight)
20763 || !f->glyphs_initialized_p)
20764 return;
20765
20766 dpyinfo->mouse_face_mouse_x = x;
20767 dpyinfo->mouse_face_mouse_y = y;
20768 dpyinfo->mouse_face_mouse_frame = f;
20769
20770 if (dpyinfo->mouse_face_defer)
20771 return;
20772
20773 if (gc_in_progress)
20774 {
20775 dpyinfo->mouse_face_deferred_gc = 1;
20776 return;
20777 }
20778
20779 /* Which window is that in? */
20780 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20781
20782 /* If we were displaying active text in another window, clear that. */
20783 if (! EQ (window, dpyinfo->mouse_face_window))
20784 clear_mouse_face (dpyinfo);
20785
20786 /* Not on a window -> return. */
20787 if (!WINDOWP (window))
20788 return;
20789
20790 /* Reset help_echo_string. It will get recomputed below. */
20791 help_echo_string = Qnil;
20792
20793 /* Convert to window-relative pixel coordinates. */
20794 w = XWINDOW (window);
20795 frame_to_window_pixel_xy (w, &x, &y);
20796
20797 /* Handle tool-bar window differently since it doesn't display a
20798 buffer. */
20799 if (EQ (window, f->tool_bar_window))
20800 {
20801 note_tool_bar_highlight (f, x, y);
20802 return;
20803 }
20804
20805 /* Mouse is on the mode, header line or margin? */
20806 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20807 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20808 {
20809 note_mode_line_or_margin_highlight (w, x, y, part);
20810 return;
20811 }
20812
20813 if (part == ON_VERTICAL_BORDER)
20814 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20815 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20816 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20817 else
20818 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20819
20820 /* Are we in a window whose display is up to date?
20821 And verify the buffer's text has not changed. */
20822 b = XBUFFER (w->buffer);
20823 if (part == ON_TEXT
20824 && EQ (w->window_end_valid, w->buffer)
20825 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20826 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20827 {
20828 int hpos, vpos, pos, i, dx, dy, area;
20829 struct glyph *glyph;
20830 Lisp_Object object;
20831 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20832 Lisp_Object *overlay_vec = NULL;
20833 int len, noverlays;
20834 struct buffer *obuf;
20835 int obegv, ozv, same_region;
20836
20837 /* Find the glyph under X/Y. */
20838 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20839
20840 /* Look for :pointer property on image. */
20841 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20842 {
20843 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20844 if (img != NULL && IMAGEP (img->spec))
20845 {
20846 Lisp_Object image_map, hotspot;
20847 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20848 !NILP (image_map))
20849 && (hotspot = find_hot_spot (image_map,
20850 glyph->slice.x + dx,
20851 glyph->slice.y + dy),
20852 CONSP (hotspot))
20853 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20854 {
20855 Lisp_Object area_id, plist;
20856
20857 area_id = XCAR (hotspot);
20858 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20859 If so, we could look for mouse-enter, mouse-leave
20860 properties in PLIST (and do something...). */
20861 if ((plist = XCDR (hotspot), CONSP (plist)))
20862 {
20863 pointer = Fplist_get (plist, Qpointer);
20864 if (NILP (pointer))
20865 pointer = Qhand;
20866 help_echo_string = Fplist_get (plist, Qhelp_echo);
20867 if (!NILP (help_echo_string))
20868 {
20869 help_echo_window = window;
20870 help_echo_object = glyph->object;
20871 help_echo_pos = glyph->charpos;
20872 }
20873 }
20874 }
20875 if (NILP (pointer))
20876 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20877 }
20878 }
20879
20880 /* Clear mouse face if X/Y not over text. */
20881 if (glyph == NULL
20882 || area != TEXT_AREA
20883 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20884 {
20885 if (clear_mouse_face (dpyinfo))
20886 cursor = No_Cursor;
20887 if (NILP (pointer))
20888 {
20889 if (area != TEXT_AREA)
20890 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20891 else
20892 pointer = Vvoid_text_area_pointer;
20893 }
20894 goto set_cursor;
20895 }
20896
20897 pos = glyph->charpos;
20898 object = glyph->object;
20899 if (!STRINGP (object) && !BUFFERP (object))
20900 goto set_cursor;
20901
20902 /* If we get an out-of-range value, return now; avoid an error. */
20903 if (BUFFERP (object) && pos > BUF_Z (b))
20904 goto set_cursor;
20905
20906 /* Make the window's buffer temporarily current for
20907 overlays_at and compute_char_face. */
20908 obuf = current_buffer;
20909 current_buffer = b;
20910 obegv = BEGV;
20911 ozv = ZV;
20912 BEGV = BEG;
20913 ZV = Z;
20914
20915 /* Is this char mouse-active or does it have help-echo? */
20916 position = make_number (pos);
20917
20918 if (BUFFERP (object))
20919 {
20920 /* Put all the overlays we want in a vector in overlay_vec.
20921 Store the length in len. If there are more than 40, make
20922 enough space for all, and try again. */
20923 len = 40;
20924 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20925 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20926 if (noverlays > len)
20927 {
20928 len = noverlays;
20929 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20930 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20931 }
20932
20933 /* Sort overlays into increasing priority order. */
20934 noverlays = sort_overlays (overlay_vec, noverlays, w);
20935 }
20936 else
20937 noverlays = 0;
20938
20939 same_region = (EQ (window, dpyinfo->mouse_face_window)
20940 && vpos >= dpyinfo->mouse_face_beg_row
20941 && vpos <= dpyinfo->mouse_face_end_row
20942 && (vpos > dpyinfo->mouse_face_beg_row
20943 || hpos >= dpyinfo->mouse_face_beg_col)
20944 && (vpos < dpyinfo->mouse_face_end_row
20945 || hpos < dpyinfo->mouse_face_end_col
20946 || dpyinfo->mouse_face_past_end));
20947
20948 if (same_region)
20949 cursor = No_Cursor;
20950
20951 /* Check mouse-face highlighting. */
20952 if (! same_region
20953 /* If there exists an overlay with mouse-face overlapping
20954 the one we are currently highlighting, we have to
20955 check if we enter the overlapping overlay, and then
20956 highlight only that. */
20957 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20958 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20959 {
20960 /* Find the highest priority overlay that has a mouse-face
20961 property. */
20962 overlay = Qnil;
20963 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20964 {
20965 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20966 if (!NILP (mouse_face))
20967 overlay = overlay_vec[i];
20968 }
20969
20970 /* If we're actually highlighting the same overlay as
20971 before, there's no need to do that again. */
20972 if (!NILP (overlay)
20973 && EQ (overlay, dpyinfo->mouse_face_overlay))
20974 goto check_help_echo;
20975
20976 dpyinfo->mouse_face_overlay = overlay;
20977
20978 /* Clear the display of the old active region, if any. */
20979 if (clear_mouse_face (dpyinfo))
20980 cursor = No_Cursor;
20981
20982 /* If no overlay applies, get a text property. */
20983 if (NILP (overlay))
20984 mouse_face = Fget_text_property (position, Qmouse_face, object);
20985
20986 /* Handle the overlay case. */
20987 if (!NILP (overlay))
20988 {
20989 /* Find the range of text around this char that
20990 should be active. */
20991 Lisp_Object before, after;
20992 int ignore;
20993
20994 before = Foverlay_start (overlay);
20995 after = Foverlay_end (overlay);
20996 /* Record this as the current active region. */
20997 fast_find_position (w, XFASTINT (before),
20998 &dpyinfo->mouse_face_beg_col,
20999 &dpyinfo->mouse_face_beg_row,
21000 &dpyinfo->mouse_face_beg_x,
21001 &dpyinfo->mouse_face_beg_y, Qnil);
21002
21003 dpyinfo->mouse_face_past_end
21004 = !fast_find_position (w, XFASTINT (after),
21005 &dpyinfo->mouse_face_end_col,
21006 &dpyinfo->mouse_face_end_row,
21007 &dpyinfo->mouse_face_end_x,
21008 &dpyinfo->mouse_face_end_y, Qnil);
21009 dpyinfo->mouse_face_window = window;
21010
21011 dpyinfo->mouse_face_face_id
21012 = face_at_buffer_position (w, pos, 0, 0,
21013 &ignore, pos + 1,
21014 !dpyinfo->mouse_face_hidden);
21015
21016 /* Display it as active. */
21017 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21018 cursor = No_Cursor;
21019 }
21020 /* Handle the text property case. */
21021 else if (!NILP (mouse_face) && BUFFERP (object))
21022 {
21023 /* Find the range of text around this char that
21024 should be active. */
21025 Lisp_Object before, after, beginning, end;
21026 int ignore;
21027
21028 beginning = Fmarker_position (w->start);
21029 end = make_number (BUF_Z (XBUFFER (object))
21030 - XFASTINT (w->window_end_pos));
21031 before
21032 = Fprevious_single_property_change (make_number (pos + 1),
21033 Qmouse_face,
21034 object, beginning);
21035 after
21036 = Fnext_single_property_change (position, Qmouse_face,
21037 object, end);
21038
21039 /* Record this as the current active region. */
21040 fast_find_position (w, XFASTINT (before),
21041 &dpyinfo->mouse_face_beg_col,
21042 &dpyinfo->mouse_face_beg_row,
21043 &dpyinfo->mouse_face_beg_x,
21044 &dpyinfo->mouse_face_beg_y, Qnil);
21045 dpyinfo->mouse_face_past_end
21046 = !fast_find_position (w, XFASTINT (after),
21047 &dpyinfo->mouse_face_end_col,
21048 &dpyinfo->mouse_face_end_row,
21049 &dpyinfo->mouse_face_end_x,
21050 &dpyinfo->mouse_face_end_y, Qnil);
21051 dpyinfo->mouse_face_window = window;
21052
21053 if (BUFFERP (object))
21054 dpyinfo->mouse_face_face_id
21055 = face_at_buffer_position (w, pos, 0, 0,
21056 &ignore, pos + 1,
21057 !dpyinfo->mouse_face_hidden);
21058
21059 /* Display it as active. */
21060 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21061 cursor = No_Cursor;
21062 }
21063 else if (!NILP (mouse_face) && STRINGP (object))
21064 {
21065 Lisp_Object b, e;
21066 int ignore;
21067
21068 b = Fprevious_single_property_change (make_number (pos + 1),
21069 Qmouse_face,
21070 object, Qnil);
21071 e = Fnext_single_property_change (position, Qmouse_face,
21072 object, Qnil);
21073 if (NILP (b))
21074 b = make_number (0);
21075 if (NILP (e))
21076 e = make_number (SCHARS (object) - 1);
21077 fast_find_string_pos (w, XINT (b), object,
21078 &dpyinfo->mouse_face_beg_col,
21079 &dpyinfo->mouse_face_beg_row,
21080 &dpyinfo->mouse_face_beg_x,
21081 &dpyinfo->mouse_face_beg_y, 0);
21082 fast_find_string_pos (w, XINT (e), object,
21083 &dpyinfo->mouse_face_end_col,
21084 &dpyinfo->mouse_face_end_row,
21085 &dpyinfo->mouse_face_end_x,
21086 &dpyinfo->mouse_face_end_y, 1);
21087 dpyinfo->mouse_face_past_end = 0;
21088 dpyinfo->mouse_face_window = window;
21089 dpyinfo->mouse_face_face_id
21090 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21091 glyph->face_id, 1);
21092 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21093 cursor = No_Cursor;
21094 }
21095 else if (STRINGP (object) && NILP (mouse_face))
21096 {
21097 /* A string which doesn't have mouse-face, but
21098 the text ``under'' it might have. */
21099 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21100 int start = MATRIX_ROW_START_CHARPOS (r);
21101
21102 pos = string_buffer_position (w, object, start);
21103 if (pos > 0)
21104 mouse_face = get_char_property_and_overlay (make_number (pos),
21105 Qmouse_face,
21106 w->buffer,
21107 &overlay);
21108 if (!NILP (mouse_face) && !NILP (overlay))
21109 {
21110 Lisp_Object before = Foverlay_start (overlay);
21111 Lisp_Object after = Foverlay_end (overlay);
21112 int ignore;
21113
21114 /* Note that we might not be able to find position
21115 BEFORE in the glyph matrix if the overlay is
21116 entirely covered by a `display' property. In
21117 this case, we overshoot. So let's stop in
21118 the glyph matrix before glyphs for OBJECT. */
21119 fast_find_position (w, XFASTINT (before),
21120 &dpyinfo->mouse_face_beg_col,
21121 &dpyinfo->mouse_face_beg_row,
21122 &dpyinfo->mouse_face_beg_x,
21123 &dpyinfo->mouse_face_beg_y,
21124 object);
21125
21126 dpyinfo->mouse_face_past_end
21127 = !fast_find_position (w, XFASTINT (after),
21128 &dpyinfo->mouse_face_end_col,
21129 &dpyinfo->mouse_face_end_row,
21130 &dpyinfo->mouse_face_end_x,
21131 &dpyinfo->mouse_face_end_y,
21132 Qnil);
21133 dpyinfo->mouse_face_window = window;
21134 dpyinfo->mouse_face_face_id
21135 = face_at_buffer_position (w, pos, 0, 0,
21136 &ignore, pos + 1,
21137 !dpyinfo->mouse_face_hidden);
21138
21139 /* Display it as active. */
21140 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21141 cursor = No_Cursor;
21142 }
21143 }
21144 }
21145
21146 check_help_echo:
21147
21148 /* Look for a `help-echo' property. */
21149 if (NILP (help_echo_string)) {
21150 Lisp_Object help, overlay;
21151
21152 /* Check overlays first. */
21153 help = overlay = Qnil;
21154 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21155 {
21156 overlay = overlay_vec[i];
21157 help = Foverlay_get (overlay, Qhelp_echo);
21158 }
21159
21160 if (!NILP (help))
21161 {
21162 help_echo_string = help;
21163 help_echo_window = window;
21164 help_echo_object = overlay;
21165 help_echo_pos = pos;
21166 }
21167 else
21168 {
21169 Lisp_Object object = glyph->object;
21170 int charpos = glyph->charpos;
21171
21172 /* Try text properties. */
21173 if (STRINGP (object)
21174 && charpos >= 0
21175 && charpos < SCHARS (object))
21176 {
21177 help = Fget_text_property (make_number (charpos),
21178 Qhelp_echo, object);
21179 if (NILP (help))
21180 {
21181 /* If the string itself doesn't specify a help-echo,
21182 see if the buffer text ``under'' it does. */
21183 struct glyph_row *r
21184 = MATRIX_ROW (w->current_matrix, vpos);
21185 int start = MATRIX_ROW_START_CHARPOS (r);
21186 int pos = string_buffer_position (w, object, start);
21187 if (pos > 0)
21188 {
21189 help = Fget_char_property (make_number (pos),
21190 Qhelp_echo, w->buffer);
21191 if (!NILP (help))
21192 {
21193 charpos = pos;
21194 object = w->buffer;
21195 }
21196 }
21197 }
21198 }
21199 else if (BUFFERP (object)
21200 && charpos >= BEGV
21201 && charpos < ZV)
21202 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21203 object);
21204
21205 if (!NILP (help))
21206 {
21207 help_echo_string = help;
21208 help_echo_window = window;
21209 help_echo_object = object;
21210 help_echo_pos = charpos;
21211 }
21212 }
21213 }
21214
21215 /* Look for a `pointer' property. */
21216 if (NILP (pointer))
21217 {
21218 /* Check overlays first. */
21219 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21220 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21221
21222 if (NILP (pointer))
21223 {
21224 Lisp_Object object = glyph->object;
21225 int charpos = glyph->charpos;
21226
21227 /* Try text properties. */
21228 if (STRINGP (object)
21229 && charpos >= 0
21230 && charpos < SCHARS (object))
21231 {
21232 pointer = Fget_text_property (make_number (charpos),
21233 Qpointer, object);
21234 if (NILP (pointer))
21235 {
21236 /* If the string itself doesn't specify a pointer,
21237 see if the buffer text ``under'' it does. */
21238 struct glyph_row *r
21239 = MATRIX_ROW (w->current_matrix, vpos);
21240 int start = MATRIX_ROW_START_CHARPOS (r);
21241 int pos = string_buffer_position (w, object, start);
21242 if (pos > 0)
21243 pointer = Fget_char_property (make_number (pos),
21244 Qpointer, w->buffer);
21245 }
21246 }
21247 else if (BUFFERP (object)
21248 && charpos >= BEGV
21249 && charpos < ZV)
21250 pointer = Fget_text_property (make_number (charpos),
21251 Qpointer, object);
21252 }
21253 }
21254
21255 BEGV = obegv;
21256 ZV = ozv;
21257 current_buffer = obuf;
21258 }
21259
21260 set_cursor:
21261
21262 define_frame_cursor1 (f, cursor, pointer);
21263 }
21264
21265
21266 /* EXPORT for RIF:
21267 Clear any mouse-face on window W. This function is part of the
21268 redisplay interface, and is called from try_window_id and similar
21269 functions to ensure the mouse-highlight is off. */
21270
21271 void
21272 x_clear_window_mouse_face (w)
21273 struct window *w;
21274 {
21275 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21276 Lisp_Object window;
21277
21278 BLOCK_INPUT;
21279 XSETWINDOW (window, w);
21280 if (EQ (window, dpyinfo->mouse_face_window))
21281 clear_mouse_face (dpyinfo);
21282 UNBLOCK_INPUT;
21283 }
21284
21285
21286 /* EXPORT:
21287 Just discard the mouse face information for frame F, if any.
21288 This is used when the size of F is changed. */
21289
21290 void
21291 cancel_mouse_face (f)
21292 struct frame *f;
21293 {
21294 Lisp_Object window;
21295 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21296
21297 window = dpyinfo->mouse_face_window;
21298 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21299 {
21300 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21301 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21302 dpyinfo->mouse_face_window = Qnil;
21303 }
21304 }
21305
21306
21307 #endif /* HAVE_WINDOW_SYSTEM */
21308
21309 \f
21310 /***********************************************************************
21311 Exposure Events
21312 ***********************************************************************/
21313
21314 #ifdef HAVE_WINDOW_SYSTEM
21315
21316 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21317 which intersects rectangle R. R is in window-relative coordinates. */
21318
21319 static void
21320 expose_area (w, row, r, area)
21321 struct window *w;
21322 struct glyph_row *row;
21323 XRectangle *r;
21324 enum glyph_row_area area;
21325 {
21326 struct glyph *first = row->glyphs[area];
21327 struct glyph *end = row->glyphs[area] + row->used[area];
21328 struct glyph *last;
21329 int first_x, start_x, x;
21330
21331 if (area == TEXT_AREA && row->fill_line_p)
21332 /* If row extends face to end of line write the whole line. */
21333 draw_glyphs (w, 0, row, area,
21334 0, row->used[area],
21335 DRAW_NORMAL_TEXT, 0);
21336 else
21337 {
21338 /* Set START_X to the window-relative start position for drawing glyphs of
21339 AREA. The first glyph of the text area can be partially visible.
21340 The first glyphs of other areas cannot. */
21341 start_x = window_box_left_offset (w, area);
21342 x = start_x;
21343 if (area == TEXT_AREA)
21344 x += row->x;
21345
21346 /* Find the first glyph that must be redrawn. */
21347 while (first < end
21348 && x + first->pixel_width < r->x)
21349 {
21350 x += first->pixel_width;
21351 ++first;
21352 }
21353
21354 /* Find the last one. */
21355 last = first;
21356 first_x = x;
21357 while (last < end
21358 && x < r->x + r->width)
21359 {
21360 x += last->pixel_width;
21361 ++last;
21362 }
21363
21364 /* Repaint. */
21365 if (last > first)
21366 draw_glyphs (w, first_x - start_x, row, area,
21367 first - row->glyphs[area], last - row->glyphs[area],
21368 DRAW_NORMAL_TEXT, 0);
21369 }
21370 }
21371
21372
21373 /* Redraw the parts of the glyph row ROW on window W intersecting
21374 rectangle R. R is in window-relative coordinates. Value is
21375 non-zero if mouse-face was overwritten. */
21376
21377 static int
21378 expose_line (w, row, r)
21379 struct window *w;
21380 struct glyph_row *row;
21381 XRectangle *r;
21382 {
21383 xassert (row->enabled_p);
21384
21385 if (row->mode_line_p || w->pseudo_window_p)
21386 draw_glyphs (w, 0, row, TEXT_AREA,
21387 0, row->used[TEXT_AREA],
21388 DRAW_NORMAL_TEXT, 0);
21389 else
21390 {
21391 if (row->used[LEFT_MARGIN_AREA])
21392 expose_area (w, row, r, LEFT_MARGIN_AREA);
21393 if (row->used[TEXT_AREA])
21394 expose_area (w, row, r, TEXT_AREA);
21395 if (row->used[RIGHT_MARGIN_AREA])
21396 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21397 draw_row_fringe_bitmaps (w, row);
21398 }
21399
21400 return row->mouse_face_p;
21401 }
21402
21403
21404 /* Redraw those parts of glyphs rows during expose event handling that
21405 overlap other rows. Redrawing of an exposed line writes over parts
21406 of lines overlapping that exposed line; this function fixes that.
21407
21408 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21409 row in W's current matrix that is exposed and overlaps other rows.
21410 LAST_OVERLAPPING_ROW is the last such row. */
21411
21412 static void
21413 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21414 struct window *w;
21415 struct glyph_row *first_overlapping_row;
21416 struct glyph_row *last_overlapping_row;
21417 {
21418 struct glyph_row *row;
21419
21420 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21421 if (row->overlapping_p)
21422 {
21423 xassert (row->enabled_p && !row->mode_line_p);
21424
21425 if (row->used[LEFT_MARGIN_AREA])
21426 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21427
21428 if (row->used[TEXT_AREA])
21429 x_fix_overlapping_area (w, row, TEXT_AREA);
21430
21431 if (row->used[RIGHT_MARGIN_AREA])
21432 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21433 }
21434 }
21435
21436
21437 /* Return non-zero if W's cursor intersects rectangle R. */
21438
21439 static int
21440 phys_cursor_in_rect_p (w, r)
21441 struct window *w;
21442 XRectangle *r;
21443 {
21444 XRectangle cr, result;
21445 struct glyph *cursor_glyph;
21446
21447 cursor_glyph = get_phys_cursor_glyph (w);
21448 if (cursor_glyph)
21449 {
21450 /* r is relative to W's box, but w->phys_cursor.x is relative
21451 to left edge of W's TEXT area. Adjust it. */
21452 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21453 cr.y = w->phys_cursor.y;
21454 cr.width = cursor_glyph->pixel_width;
21455 cr.height = w->phys_cursor_height;
21456 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21457 I assume the effect is the same -- and this is portable. */
21458 return x_intersect_rectangles (&cr, r, &result);
21459 }
21460 else
21461 return 0;
21462 }
21463
21464
21465 /* EXPORT:
21466 Draw a vertical window border to the right of window W if W doesn't
21467 have vertical scroll bars. */
21468
21469 void
21470 x_draw_vertical_border (w)
21471 struct window *w;
21472 {
21473 /* We could do better, if we knew what type of scroll-bar the adjacent
21474 windows (on either side) have... But we don't :-(
21475 However, I think this works ok. ++KFS 2003-04-25 */
21476
21477 /* Redraw borders between horizontally adjacent windows. Don't
21478 do it for frames with vertical scroll bars because either the
21479 right scroll bar of a window, or the left scroll bar of its
21480 neighbor will suffice as a border. */
21481 if (!WINDOW_RIGHTMOST_P (w)
21482 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21483 {
21484 int x0, x1, y0, y1;
21485
21486 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21487 y1 -= 1;
21488
21489 rif->draw_vertical_window_border (w, x1, y0, y1);
21490 }
21491 else if (!WINDOW_LEFTMOST_P (w)
21492 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21493 {
21494 int x0, x1, y0, y1;
21495
21496 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21497 y1 -= 1;
21498
21499 rif->draw_vertical_window_border (w, x0, y0, y1);
21500 }
21501 }
21502
21503
21504 /* Redraw the part of window W intersection rectangle FR. Pixel
21505 coordinates in FR are frame-relative. Call this function with
21506 input blocked. Value is non-zero if the exposure overwrites
21507 mouse-face. */
21508
21509 static int
21510 expose_window (w, fr)
21511 struct window *w;
21512 XRectangle *fr;
21513 {
21514 struct frame *f = XFRAME (w->frame);
21515 XRectangle wr, r;
21516 int mouse_face_overwritten_p = 0;
21517
21518 /* If window is not yet fully initialized, do nothing. This can
21519 happen when toolkit scroll bars are used and a window is split.
21520 Reconfiguring the scroll bar will generate an expose for a newly
21521 created window. */
21522 if (w->current_matrix == NULL)
21523 return 0;
21524
21525 /* When we're currently updating the window, display and current
21526 matrix usually don't agree. Arrange for a thorough display
21527 later. */
21528 if (w == updated_window)
21529 {
21530 SET_FRAME_GARBAGED (f);
21531 return 0;
21532 }
21533
21534 /* Frame-relative pixel rectangle of W. */
21535 wr.x = WINDOW_LEFT_EDGE_X (w);
21536 wr.y = WINDOW_TOP_EDGE_Y (w);
21537 wr.width = WINDOW_TOTAL_WIDTH (w);
21538 wr.height = WINDOW_TOTAL_HEIGHT (w);
21539
21540 if (x_intersect_rectangles (fr, &wr, &r))
21541 {
21542 int yb = window_text_bottom_y (w);
21543 struct glyph_row *row;
21544 int cursor_cleared_p;
21545 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21546
21547 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21548 r.x, r.y, r.width, r.height));
21549
21550 /* Convert to window coordinates. */
21551 r.x -= WINDOW_LEFT_EDGE_X (w);
21552 r.y -= WINDOW_TOP_EDGE_Y (w);
21553
21554 /* Turn off the cursor. */
21555 if (!w->pseudo_window_p
21556 && phys_cursor_in_rect_p (w, &r))
21557 {
21558 x_clear_cursor (w);
21559 cursor_cleared_p = 1;
21560 }
21561 else
21562 cursor_cleared_p = 0;
21563
21564 /* Update lines intersecting rectangle R. */
21565 first_overlapping_row = last_overlapping_row = NULL;
21566 for (row = w->current_matrix->rows;
21567 row->enabled_p;
21568 ++row)
21569 {
21570 int y0 = row->y;
21571 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21572
21573 if ((y0 >= r.y && y0 < r.y + r.height)
21574 || (y1 > r.y && y1 < r.y + r.height)
21575 || (r.y >= y0 && r.y < y1)
21576 || (r.y + r.height > y0 && r.y + r.height < y1))
21577 {
21578 if (row->overlapping_p)
21579 {
21580 if (first_overlapping_row == NULL)
21581 first_overlapping_row = row;
21582 last_overlapping_row = row;
21583 }
21584
21585 if (expose_line (w, row, &r))
21586 mouse_face_overwritten_p = 1;
21587 }
21588
21589 if (y1 >= yb)
21590 break;
21591 }
21592
21593 /* Display the mode line if there is one. */
21594 if (WINDOW_WANTS_MODELINE_P (w)
21595 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21596 row->enabled_p)
21597 && row->y < r.y + r.height)
21598 {
21599 if (expose_line (w, row, &r))
21600 mouse_face_overwritten_p = 1;
21601 }
21602
21603 if (!w->pseudo_window_p)
21604 {
21605 /* Fix the display of overlapping rows. */
21606 if (first_overlapping_row)
21607 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21608
21609 /* Draw border between windows. */
21610 x_draw_vertical_border (w);
21611
21612 /* Turn the cursor on again. */
21613 if (cursor_cleared_p)
21614 update_window_cursor (w, 1);
21615 }
21616 }
21617
21618 #ifdef HAVE_CARBON
21619 /* Display scroll bar for this window. */
21620 if (!NILP (w->vertical_scroll_bar))
21621 {
21622 /* ++KFS:
21623 If this doesn't work here (maybe some header files are missing),
21624 make a function in macterm.c and call it to do the job! */
21625 ControlHandle ch
21626 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21627
21628 Draw1Control (ch);
21629 }
21630 #endif
21631
21632 return mouse_face_overwritten_p;
21633 }
21634
21635
21636
21637 /* Redraw (parts) of all windows in the window tree rooted at W that
21638 intersect R. R contains frame pixel coordinates. Value is
21639 non-zero if the exposure overwrites mouse-face. */
21640
21641 static int
21642 expose_window_tree (w, r)
21643 struct window *w;
21644 XRectangle *r;
21645 {
21646 struct frame *f = XFRAME (w->frame);
21647 int mouse_face_overwritten_p = 0;
21648
21649 while (w && !FRAME_GARBAGED_P (f))
21650 {
21651 if (!NILP (w->hchild))
21652 mouse_face_overwritten_p
21653 |= expose_window_tree (XWINDOW (w->hchild), r);
21654 else if (!NILP (w->vchild))
21655 mouse_face_overwritten_p
21656 |= expose_window_tree (XWINDOW (w->vchild), r);
21657 else
21658 mouse_face_overwritten_p |= expose_window (w, r);
21659
21660 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21661 }
21662
21663 return mouse_face_overwritten_p;
21664 }
21665
21666
21667 /* EXPORT:
21668 Redisplay an exposed area of frame F. X and Y are the upper-left
21669 corner of the exposed rectangle. W and H are width and height of
21670 the exposed area. All are pixel values. W or H zero means redraw
21671 the entire frame. */
21672
21673 void
21674 expose_frame (f, x, y, w, h)
21675 struct frame *f;
21676 int x, y, w, h;
21677 {
21678 XRectangle r;
21679 int mouse_face_overwritten_p = 0;
21680
21681 TRACE ((stderr, "expose_frame "));
21682
21683 /* No need to redraw if frame will be redrawn soon. */
21684 if (FRAME_GARBAGED_P (f))
21685 {
21686 TRACE ((stderr, " garbaged\n"));
21687 return;
21688 }
21689
21690 #ifdef HAVE_CARBON
21691 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21692 or deactivated here, for unknown reasons, activated scroll bars
21693 are shown in deactivated frames in some instances. */
21694 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21695 activate_scroll_bars (f);
21696 else
21697 deactivate_scroll_bars (f);
21698 #endif
21699
21700 /* If basic faces haven't been realized yet, there is no point in
21701 trying to redraw anything. This can happen when we get an expose
21702 event while Emacs is starting, e.g. by moving another window. */
21703 if (FRAME_FACE_CACHE (f) == NULL
21704 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21705 {
21706 TRACE ((stderr, " no faces\n"));
21707 return;
21708 }
21709
21710 if (w == 0 || h == 0)
21711 {
21712 r.x = r.y = 0;
21713 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21714 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21715 }
21716 else
21717 {
21718 r.x = x;
21719 r.y = y;
21720 r.width = w;
21721 r.height = h;
21722 }
21723
21724 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21725 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21726
21727 if (WINDOWP (f->tool_bar_window))
21728 mouse_face_overwritten_p
21729 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21730
21731 #ifdef HAVE_X_WINDOWS
21732 #ifndef MSDOS
21733 #ifndef USE_X_TOOLKIT
21734 if (WINDOWP (f->menu_bar_window))
21735 mouse_face_overwritten_p
21736 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21737 #endif /* not USE_X_TOOLKIT */
21738 #endif
21739 #endif
21740
21741 /* Some window managers support a focus-follows-mouse style with
21742 delayed raising of frames. Imagine a partially obscured frame,
21743 and moving the mouse into partially obscured mouse-face on that
21744 frame. The visible part of the mouse-face will be highlighted,
21745 then the WM raises the obscured frame. With at least one WM, KDE
21746 2.1, Emacs is not getting any event for the raising of the frame
21747 (even tried with SubstructureRedirectMask), only Expose events.
21748 These expose events will draw text normally, i.e. not
21749 highlighted. Which means we must redo the highlight here.
21750 Subsume it under ``we love X''. --gerd 2001-08-15 */
21751 /* Included in Windows version because Windows most likely does not
21752 do the right thing if any third party tool offers
21753 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21754 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21755 {
21756 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21757 if (f == dpyinfo->mouse_face_mouse_frame)
21758 {
21759 int x = dpyinfo->mouse_face_mouse_x;
21760 int y = dpyinfo->mouse_face_mouse_y;
21761 clear_mouse_face (dpyinfo);
21762 note_mouse_highlight (f, x, y);
21763 }
21764 }
21765 }
21766
21767
21768 /* EXPORT:
21769 Determine the intersection of two rectangles R1 and R2. Return
21770 the intersection in *RESULT. Value is non-zero if RESULT is not
21771 empty. */
21772
21773 int
21774 x_intersect_rectangles (r1, r2, result)
21775 XRectangle *r1, *r2, *result;
21776 {
21777 XRectangle *left, *right;
21778 XRectangle *upper, *lower;
21779 int intersection_p = 0;
21780
21781 /* Rearrange so that R1 is the left-most rectangle. */
21782 if (r1->x < r2->x)
21783 left = r1, right = r2;
21784 else
21785 left = r2, right = r1;
21786
21787 /* X0 of the intersection is right.x0, if this is inside R1,
21788 otherwise there is no intersection. */
21789 if (right->x <= left->x + left->width)
21790 {
21791 result->x = right->x;
21792
21793 /* The right end of the intersection is the minimum of the
21794 the right ends of left and right. */
21795 result->width = (min (left->x + left->width, right->x + right->width)
21796 - result->x);
21797
21798 /* Same game for Y. */
21799 if (r1->y < r2->y)
21800 upper = r1, lower = r2;
21801 else
21802 upper = r2, lower = r1;
21803
21804 /* The upper end of the intersection is lower.y0, if this is inside
21805 of upper. Otherwise, there is no intersection. */
21806 if (lower->y <= upper->y + upper->height)
21807 {
21808 result->y = lower->y;
21809
21810 /* The lower end of the intersection is the minimum of the lower
21811 ends of upper and lower. */
21812 result->height = (min (lower->y + lower->height,
21813 upper->y + upper->height)
21814 - result->y);
21815 intersection_p = 1;
21816 }
21817 }
21818
21819 return intersection_p;
21820 }
21821
21822 #endif /* HAVE_WINDOW_SYSTEM */
21823
21824 \f
21825 /***********************************************************************
21826 Initialization
21827 ***********************************************************************/
21828
21829 void
21830 syms_of_xdisp ()
21831 {
21832 Vwith_echo_area_save_vector = Qnil;
21833 staticpro (&Vwith_echo_area_save_vector);
21834
21835 Vmessage_stack = Qnil;
21836 staticpro (&Vmessage_stack);
21837
21838 Qinhibit_redisplay = intern ("inhibit-redisplay");
21839 staticpro (&Qinhibit_redisplay);
21840
21841 message_dolog_marker1 = Fmake_marker ();
21842 staticpro (&message_dolog_marker1);
21843 message_dolog_marker2 = Fmake_marker ();
21844 staticpro (&message_dolog_marker2);
21845 message_dolog_marker3 = Fmake_marker ();
21846 staticpro (&message_dolog_marker3);
21847
21848 #if GLYPH_DEBUG
21849 defsubr (&Sdump_frame_glyph_matrix);
21850 defsubr (&Sdump_glyph_matrix);
21851 defsubr (&Sdump_glyph_row);
21852 defsubr (&Sdump_tool_bar_row);
21853 defsubr (&Strace_redisplay);
21854 defsubr (&Strace_to_stderr);
21855 #endif
21856 #ifdef HAVE_WINDOW_SYSTEM
21857 defsubr (&Stool_bar_lines_needed);
21858 defsubr (&Slookup_image_map);
21859 #endif
21860 defsubr (&Sformat_mode_line);
21861
21862 staticpro (&Qmenu_bar_update_hook);
21863 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21864
21865 staticpro (&Qoverriding_terminal_local_map);
21866 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21867
21868 staticpro (&Qoverriding_local_map);
21869 Qoverriding_local_map = intern ("overriding-local-map");
21870
21871 staticpro (&Qwindow_scroll_functions);
21872 Qwindow_scroll_functions = intern ("window-scroll-functions");
21873
21874 staticpro (&Qredisplay_end_trigger_functions);
21875 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21876
21877 staticpro (&Qinhibit_point_motion_hooks);
21878 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21879
21880 QCdata = intern (":data");
21881 staticpro (&QCdata);
21882 Qdisplay = intern ("display");
21883 staticpro (&Qdisplay);
21884 Qspace_width = intern ("space-width");
21885 staticpro (&Qspace_width);
21886 Qraise = intern ("raise");
21887 staticpro (&Qraise);
21888 Qslice = intern ("slice");
21889 staticpro (&Qslice);
21890 Qspace = intern ("space");
21891 staticpro (&Qspace);
21892 Qmargin = intern ("margin");
21893 staticpro (&Qmargin);
21894 Qpointer = intern ("pointer");
21895 staticpro (&Qpointer);
21896 Qleft_margin = intern ("left-margin");
21897 staticpro (&Qleft_margin);
21898 Qright_margin = intern ("right-margin");
21899 staticpro (&Qright_margin);
21900 Qcenter = intern ("center");
21901 staticpro (&Qcenter);
21902 Qline_height = intern ("line-height");
21903 staticpro (&Qline_height);
21904 Qtotal = intern ("total");
21905 staticpro (&Qtotal);
21906 QCalign_to = intern (":align-to");
21907 staticpro (&QCalign_to);
21908 QCrelative_width = intern (":relative-width");
21909 staticpro (&QCrelative_width);
21910 QCrelative_height = intern (":relative-height");
21911 staticpro (&QCrelative_height);
21912 QCeval = intern (":eval");
21913 staticpro (&QCeval);
21914 QCpropertize = intern (":propertize");
21915 staticpro (&QCpropertize);
21916 QCfile = intern (":file");
21917 staticpro (&QCfile);
21918 Qfontified = intern ("fontified");
21919 staticpro (&Qfontified);
21920 Qfontification_functions = intern ("fontification-functions");
21921 staticpro (&Qfontification_functions);
21922 Qtrailing_whitespace = intern ("trailing-whitespace");
21923 staticpro (&Qtrailing_whitespace);
21924 Qimage = intern ("image");
21925 staticpro (&Qimage);
21926 QCmap = intern (":map");
21927 staticpro (&QCmap);
21928 QCpointer = intern (":pointer");
21929 staticpro (&QCpointer);
21930 Qrect = intern ("rect");
21931 staticpro (&Qrect);
21932 Qcircle = intern ("circle");
21933 staticpro (&Qcircle);
21934 Qpoly = intern ("poly");
21935 staticpro (&Qpoly);
21936 Qmessage_truncate_lines = intern ("message-truncate-lines");
21937 staticpro (&Qmessage_truncate_lines);
21938 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21939 staticpro (&Qcursor_in_non_selected_windows);
21940 Qgrow_only = intern ("grow-only");
21941 staticpro (&Qgrow_only);
21942 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21943 staticpro (&Qinhibit_menubar_update);
21944 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21945 staticpro (&Qinhibit_eval_during_redisplay);
21946 Qposition = intern ("position");
21947 staticpro (&Qposition);
21948 Qbuffer_position = intern ("buffer-position");
21949 staticpro (&Qbuffer_position);
21950 Qobject = intern ("object");
21951 staticpro (&Qobject);
21952 Qbar = intern ("bar");
21953 staticpro (&Qbar);
21954 Qhbar = intern ("hbar");
21955 staticpro (&Qhbar);
21956 Qbox = intern ("box");
21957 staticpro (&Qbox);
21958 Qhollow = intern ("hollow");
21959 staticpro (&Qhollow);
21960 Qhand = intern ("hand");
21961 staticpro (&Qhand);
21962 Qarrow = intern ("arrow");
21963 staticpro (&Qarrow);
21964 Qtext = intern ("text");
21965 staticpro (&Qtext);
21966 Qrisky_local_variable = intern ("risky-local-variable");
21967 staticpro (&Qrisky_local_variable);
21968 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21969 staticpro (&Qinhibit_free_realized_faces);
21970
21971 list_of_error = Fcons (Fcons (intern ("error"),
21972 Fcons (intern ("void-variable"), Qnil)),
21973 Qnil);
21974 staticpro (&list_of_error);
21975
21976 Qlast_arrow_position = intern ("last-arrow-position");
21977 staticpro (&Qlast_arrow_position);
21978 Qlast_arrow_string = intern ("last-arrow-string");
21979 staticpro (&Qlast_arrow_string);
21980
21981 Qoverlay_arrow_string = intern ("overlay-arrow-string");
21982 staticpro (&Qoverlay_arrow_string);
21983 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
21984 staticpro (&Qoverlay_arrow_bitmap);
21985
21986 echo_buffer[0] = echo_buffer[1] = Qnil;
21987 staticpro (&echo_buffer[0]);
21988 staticpro (&echo_buffer[1]);
21989
21990 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21991 staticpro (&echo_area_buffer[0]);
21992 staticpro (&echo_area_buffer[1]);
21993
21994 Vmessages_buffer_name = build_string ("*Messages*");
21995 staticpro (&Vmessages_buffer_name);
21996
21997 mode_line_proptrans_alist = Qnil;
21998 staticpro (&mode_line_proptrans_alist);
21999
22000 mode_line_string_list = Qnil;
22001 staticpro (&mode_line_string_list);
22002
22003 help_echo_string = Qnil;
22004 staticpro (&help_echo_string);
22005 help_echo_object = Qnil;
22006 staticpro (&help_echo_object);
22007 help_echo_window = Qnil;
22008 staticpro (&help_echo_window);
22009 previous_help_echo_string = Qnil;
22010 staticpro (&previous_help_echo_string);
22011 help_echo_pos = -1;
22012
22013 #ifdef HAVE_WINDOW_SYSTEM
22014 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22015 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22016 For example, if a block cursor is over a tab, it will be drawn as
22017 wide as that tab on the display. */);
22018 x_stretch_cursor_p = 0;
22019 #endif
22020
22021 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22022 doc: /* *Non-nil means highlight trailing whitespace.
22023 The face used for trailing whitespace is `trailing-whitespace'. */);
22024 Vshow_trailing_whitespace = Qnil;
22025
22026 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22027 doc: /* *The pointer shape to show in void text areas.
22028 Nil means to show the text pointer. Other options are `arrow', `text',
22029 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22030 Vvoid_text_area_pointer = Qarrow;
22031
22032 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22033 doc: /* Non-nil means don't actually do any redisplay.
22034 This is used for internal purposes. */);
22035 Vinhibit_redisplay = Qnil;
22036
22037 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22038 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22039 Vglobal_mode_string = Qnil;
22040
22041 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22042 doc: /* Marker for where to display an arrow on top of the buffer text.
22043 This must be the beginning of a line in order to work.
22044 See also `overlay-arrow-string'. */);
22045 Voverlay_arrow_position = Qnil;
22046
22047 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22048 doc: /* String to display as an arrow in non-window frames.
22049 See also `overlay-arrow-position'. */);
22050 Voverlay_arrow_string = Qnil;
22051
22052 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22053 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22054 The symbols on this list are examined during redisplay to determine
22055 where to display overlay arrows. */);
22056 Voverlay_arrow_variable_list
22057 = Fcons (intern ("overlay-arrow-position"), Qnil);
22058
22059 DEFVAR_INT ("scroll-step", &scroll_step,
22060 doc: /* *The number of lines to try scrolling a window by when point moves out.
22061 If that fails to bring point back on frame, point is centered instead.
22062 If this is zero, point is always centered after it moves off frame.
22063 If you want scrolling to always be a line at a time, you should set
22064 `scroll-conservatively' to a large value rather than set this to 1. */);
22065
22066 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22067 doc: /* *Scroll up to this many lines, to bring point back on screen.
22068 A value of zero means to scroll the text to center point vertically
22069 in the window. */);
22070 scroll_conservatively = 0;
22071
22072 DEFVAR_INT ("scroll-margin", &scroll_margin,
22073 doc: /* *Number of lines of margin at the top and bottom of a window.
22074 Recenter the window whenever point gets within this many lines
22075 of the top or bottom of the window. */);
22076 scroll_margin = 0;
22077
22078 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22079 doc: /* Pixels per inch on current display.
22080 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22081 Vdisplay_pixels_per_inch = make_float (72.0);
22082
22083 #if GLYPH_DEBUG
22084 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22085 #endif
22086
22087 DEFVAR_BOOL ("truncate-partial-width-windows",
22088 &truncate_partial_width_windows,
22089 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22090 truncate_partial_width_windows = 1;
22091
22092 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22093 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22094 Any other value means to use the appropriate face, `mode-line',
22095 `header-line', or `menu' respectively. */);
22096 mode_line_inverse_video = 1;
22097
22098 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22099 doc: /* *Maximum buffer size for which line number should be displayed.
22100 If the buffer is bigger than this, the line number does not appear
22101 in the mode line. A value of nil means no limit. */);
22102 Vline_number_display_limit = Qnil;
22103
22104 DEFVAR_INT ("line-number-display-limit-width",
22105 &line_number_display_limit_width,
22106 doc: /* *Maximum line width (in characters) for line number display.
22107 If the average length of the lines near point is bigger than this, then the
22108 line number may be omitted from the mode line. */);
22109 line_number_display_limit_width = 200;
22110
22111 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22112 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22113 highlight_nonselected_windows = 0;
22114
22115 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22116 doc: /* Non-nil if more than one frame is visible on this display.
22117 Minibuffer-only frames don't count, but iconified frames do.
22118 This variable is not guaranteed to be accurate except while processing
22119 `frame-title-format' and `icon-title-format'. */);
22120
22121 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22122 doc: /* Template for displaying the title bar of visible frames.
22123 \(Assuming the window manager supports this feature.)
22124 This variable has the same structure as `mode-line-format' (which see),
22125 and is used only on frames for which no explicit name has been set
22126 \(see `modify-frame-parameters'). */);
22127
22128 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22129 doc: /* Template for displaying the title bar of an iconified frame.
22130 \(Assuming the window manager supports this feature.)
22131 This variable has the same structure as `mode-line-format' (which see),
22132 and is used only on frames for which no explicit name has been set
22133 \(see `modify-frame-parameters'). */);
22134 Vicon_title_format
22135 = Vframe_title_format
22136 = Fcons (intern ("multiple-frames"),
22137 Fcons (build_string ("%b"),
22138 Fcons (Fcons (empty_string,
22139 Fcons (intern ("invocation-name"),
22140 Fcons (build_string ("@"),
22141 Fcons (intern ("system-name"),
22142 Qnil)))),
22143 Qnil)));
22144
22145 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22146 doc: /* Maximum number of lines to keep in the message log buffer.
22147 If nil, disable message logging. If t, log messages but don't truncate
22148 the buffer when it becomes large. */);
22149 Vmessage_log_max = make_number (50);
22150
22151 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22152 doc: /* Functions called before redisplay, if window sizes have changed.
22153 The value should be a list of functions that take one argument.
22154 Just before redisplay, for each frame, if any of its windows have changed
22155 size since the last redisplay, or have been split or deleted,
22156 all the functions in the list are called, with the frame as argument. */);
22157 Vwindow_size_change_functions = Qnil;
22158
22159 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22160 doc: /* List of Functions to call before redisplaying a window with scrolling.
22161 Each function is called with two arguments, the window
22162 and its new display-start position. Note that the value of `window-end'
22163 is not valid when these functions are called. */);
22164 Vwindow_scroll_functions = Qnil;
22165
22166 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22167 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22168 mouse_autoselect_window = 0;
22169
22170 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22171 doc: /* *Non-nil means automatically resize tool-bars.
22172 This increases a tool-bar's height if not all tool-bar items are visible.
22173 It decreases a tool-bar's height when it would display blank lines
22174 otherwise. */);
22175 auto_resize_tool_bars_p = 1;
22176
22177 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22178 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22179 auto_raise_tool_bar_buttons_p = 1;
22180
22181 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22182 doc: /* *Margin around tool-bar buttons in pixels.
22183 If an integer, use that for both horizontal and vertical margins.
22184 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22185 HORZ specifying the horizontal margin, and VERT specifying the
22186 vertical margin. */);
22187 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22188
22189 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22190 doc: /* *Relief thickness of tool-bar buttons. */);
22191 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22192
22193 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22194 doc: /* List of functions to call to fontify regions of text.
22195 Each function is called with one argument POS. Functions must
22196 fontify a region starting at POS in the current buffer, and give
22197 fontified regions the property `fontified'. */);
22198 Vfontification_functions = Qnil;
22199 Fmake_variable_buffer_local (Qfontification_functions);
22200
22201 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22202 &unibyte_display_via_language_environment,
22203 doc: /* *Non-nil means display unibyte text according to language environment.
22204 Specifically this means that unibyte non-ASCII characters
22205 are displayed by converting them to the equivalent multibyte characters
22206 according to the current language environment. As a result, they are
22207 displayed according to the current fontset. */);
22208 unibyte_display_via_language_environment = 0;
22209
22210 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22211 doc: /* *Maximum height for resizing mini-windows.
22212 If a float, it specifies a fraction of the mini-window frame's height.
22213 If an integer, it specifies a number of lines. */);
22214 Vmax_mini_window_height = make_float (0.25);
22215
22216 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22217 doc: /* *How to resize mini-windows.
22218 A value of nil means don't automatically resize mini-windows.
22219 A value of t means resize them to fit the text displayed in them.
22220 A value of `grow-only', the default, means let mini-windows grow
22221 only, until their display becomes empty, at which point the windows
22222 go back to their normal size. */);
22223 Vresize_mini_windows = Qgrow_only;
22224
22225 DEFVAR_LISP ("cursor-in-non-selected-windows",
22226 &Vcursor_in_non_selected_windows,
22227 doc: /* *Cursor type to display in non-selected windows.
22228 t means to use hollow box cursor. See `cursor-type' for other values. */);
22229 Vcursor_in_non_selected_windows = Qt;
22230
22231 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22232 doc: /* Alist specifying how to blink the cursor off.
22233 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22234 `cursor-type' frame-parameter or variable equals ON-STATE,
22235 comparing using `equal', Emacs uses OFF-STATE to specify
22236 how to blink it off. */);
22237 Vblink_cursor_alist = Qnil;
22238
22239 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22240 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22241 automatic_hscrolling_p = 1;
22242
22243 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22244 doc: /* *How many columns away from the window edge point is allowed to get
22245 before automatic hscrolling will horizontally scroll the window. */);
22246 hscroll_margin = 5;
22247
22248 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22249 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22250 When point is less than `automatic-hscroll-margin' columns from the window
22251 edge, automatic hscrolling will scroll the window by the amount of columns
22252 determined by this variable. If its value is a positive integer, scroll that
22253 many columns. If it's a positive floating-point number, it specifies the
22254 fraction of the window's width to scroll. If it's nil or zero, point will be
22255 centered horizontally after the scroll. Any other value, including negative
22256 numbers, are treated as if the value were zero.
22257
22258 Automatic hscrolling always moves point outside the scroll margin, so if
22259 point was more than scroll step columns inside the margin, the window will
22260 scroll more than the value given by the scroll step.
22261
22262 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22263 and `scroll-right' overrides this variable's effect. */);
22264 Vhscroll_step = make_number (0);
22265
22266 DEFVAR_LISP ("image-types", &Vimage_types,
22267 doc: /* List of supported image types.
22268 Each element of the list is a symbol for a supported image type. */);
22269 Vimage_types = Qnil;
22270
22271 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22272 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22273 Bind this around calls to `message' to let it take effect. */);
22274 message_truncate_lines = 0;
22275
22276 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22277 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22278 Can be used to update submenus whose contents should vary. */);
22279 Vmenu_bar_update_hook = Qnil;
22280
22281 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22282 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22283 inhibit_menubar_update = 0;
22284
22285 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22286 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22287 inhibit_eval_during_redisplay = 0;
22288
22289 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22290 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22291 inhibit_free_realized_faces = 0;
22292
22293 #if GLYPH_DEBUG
22294 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22295 doc: /* Inhibit try_window_id display optimization. */);
22296 inhibit_try_window_id = 0;
22297
22298 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22299 doc: /* Inhibit try_window_reusing display optimization. */);
22300 inhibit_try_window_reusing = 0;
22301
22302 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22303 doc: /* Inhibit try_cursor_movement display optimization. */);
22304 inhibit_try_cursor_movement = 0;
22305 #endif /* GLYPH_DEBUG */
22306 }
22307
22308
22309 /* Initialize this module when Emacs starts. */
22310
22311 void
22312 init_xdisp ()
22313 {
22314 Lisp_Object root_window;
22315 struct window *mini_w;
22316
22317 current_header_line_height = current_mode_line_height = -1;
22318
22319 CHARPOS (this_line_start_pos) = 0;
22320
22321 mini_w = XWINDOW (minibuf_window);
22322 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22323
22324 if (!noninteractive)
22325 {
22326 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22327 int i;
22328
22329 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22330 set_window_height (root_window,
22331 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22332 0);
22333 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22334 set_window_height (minibuf_window, 1, 0);
22335
22336 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22337 mini_w->total_cols = make_number (FRAME_COLS (f));
22338
22339 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22340 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22341 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22342
22343 /* The default ellipsis glyphs `...'. */
22344 for (i = 0; i < 3; ++i)
22345 default_invis_vector[i] = make_number ('.');
22346 }
22347
22348 {
22349 /* Allocate the buffer for frame titles.
22350 Also used for `format-mode-line'. */
22351 int size = 100;
22352 frame_title_buf = (char *) xmalloc (size);
22353 frame_title_buf_end = frame_title_buf + size;
22354 frame_title_ptr = NULL;
22355 }
22356
22357 help_echo_showing_p = 0;
22358 }
22359
22360
22361 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22362 (do not change this comment) */