]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merged in changes from CVS HEAD
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201
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 Qmargin, Qpointer;
305 extern Lisp_Object Qheight;
306 extern Lisp_Object QCwidth, QCheight, QCascent;
307 extern Lisp_Object Qscroll_bar;
308
309 /* Non-nil means highlight trailing whitespace. */
310
311 Lisp_Object Vshow_trailing_whitespace;
312
313 #ifdef HAVE_WINDOW_SYSTEM
314 extern Lisp_Object Voverflow_newline_into_fringe;
315
316 /* Test if overflow newline into fringe. Called with iterator IT
317 at or past right window margin, and with IT->current_x set. */
318
319 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
320 (!NILP (Voverflow_newline_into_fringe) \
321 && FRAME_WINDOW_P (it->f) \
322 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
323 && it->current_x == it->last_visible_x)
324
325 #endif /* HAVE_WINDOW_SYSTEM */
326
327 /* Non-nil means show the text cursor in void text areas
328 i.e. in blank areas after eol and eob. This used to be
329 the default in 21.3. */
330
331 Lisp_Object Vvoid_text_area_pointer;
332
333 /* Name of the face used to highlight trailing whitespace. */
334
335 Lisp_Object Qtrailing_whitespace;
336
337 /* The symbol `image' which is the car of the lists used to represent
338 images in Lisp. */
339
340 Lisp_Object Qimage;
341
342 /* The image map types. */
343 Lisp_Object QCmap, QCpointer;
344 Lisp_Object Qrect, Qcircle, Qpoly;
345
346 /* Non-zero means print newline to stdout before next mini-buffer
347 message. */
348
349 int noninteractive_need_newline;
350
351 /* Non-zero means print newline to message log before next message. */
352
353 static int message_log_need_newline;
354
355 /* Three markers that message_dolog uses.
356 It could allocate them itself, but that causes trouble
357 in handling memory-full errors. */
358 static Lisp_Object message_dolog_marker1;
359 static Lisp_Object message_dolog_marker2;
360 static Lisp_Object message_dolog_marker3;
361 \f
362 /* The buffer position of the first character appearing entirely or
363 partially on the line of the selected window which contains the
364 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
365 redisplay optimization in redisplay_internal. */
366
367 static struct text_pos this_line_start_pos;
368
369 /* Number of characters past the end of the line above, including the
370 terminating newline. */
371
372 static struct text_pos this_line_end_pos;
373
374 /* The vertical positions and the height of this line. */
375
376 static int this_line_vpos;
377 static int this_line_y;
378 static int this_line_pixel_height;
379
380 /* X position at which this display line starts. Usually zero;
381 negative if first character is partially visible. */
382
383 static int this_line_start_x;
384
385 /* Buffer that this_line_.* variables are referring to. */
386
387 static struct buffer *this_line_buffer;
388
389 /* Nonzero means truncate lines in all windows less wide than the
390 frame. */
391
392 int truncate_partial_width_windows;
393
394 /* A flag to control how to display unibyte 8-bit character. */
395
396 int unibyte_display_via_language_environment;
397
398 /* Nonzero means we have more than one non-mini-buffer-only frame.
399 Not guaranteed to be accurate except while parsing
400 frame-title-format. */
401
402 int multiple_frames;
403
404 Lisp_Object Vglobal_mode_string;
405
406
407 /* List of variables (symbols) which hold markers for overlay arrows.
408 The symbols on this list are examined during redisplay to determine
409 where to display overlay arrows. */
410
411 Lisp_Object Voverlay_arrow_variable_list;
412
413 /* Marker for where to display an arrow on top of the buffer text. */
414
415 Lisp_Object Voverlay_arrow_position;
416
417 /* String to display for the arrow. Only used on terminal frames. */
418
419 Lisp_Object Voverlay_arrow_string;
420
421 /* Values of those variables at last redisplay are stored as
422 properties on `overlay-arrow-position' symbol. However, if
423 Voverlay_arrow_position is a marker, last-arrow-position is its
424 numerical position. */
425
426 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
427
428 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
429 properties on a symbol in overlay-arrow-variable-list. */
430
431 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
432
433 /* Like mode-line-format, but for the title bar on a visible frame. */
434
435 Lisp_Object Vframe_title_format;
436
437 /* Like mode-line-format, but for the title bar on an iconified frame. */
438
439 Lisp_Object Vicon_title_format;
440
441 /* List of functions to call when a window's size changes. These
442 functions get one arg, a frame on which one or more windows' sizes
443 have changed. */
444
445 static Lisp_Object Vwindow_size_change_functions;
446
447 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
448
449 /* Nonzero if overlay arrow has been displayed once in this window. */
450
451 static int overlay_arrow_seen;
452
453 /* Nonzero means highlight the region even in nonselected windows. */
454
455 int highlight_nonselected_windows;
456
457 /* If cursor motion alone moves point off frame, try scrolling this
458 many lines up or down if that will bring it back. */
459
460 static EMACS_INT scroll_step;
461
462 /* Nonzero means scroll just far enough to bring point back on the
463 screen, when appropriate. */
464
465 static EMACS_INT scroll_conservatively;
466
467 /* Recenter the window whenever point gets within this many lines of
468 the top or bottom of the window. This value is translated into a
469 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
470 that there is really a fixed pixel height scroll margin. */
471
472 EMACS_INT scroll_margin;
473
474 /* Number of windows showing the buffer of the selected window (or
475 another buffer with the same base buffer). keyboard.c refers to
476 this. */
477
478 int buffer_shared;
479
480 /* Vector containing glyphs for an ellipsis `...'. */
481
482 static Lisp_Object default_invis_vector[3];
483
484 /* Zero means display the mode-line/header-line/menu-bar in the default face
485 (this slightly odd definition is for compatibility with previous versions
486 of emacs), non-zero means display them using their respective faces.
487
488 This variable is deprecated. */
489
490 int mode_line_inverse_video;
491
492 /* Prompt to display in front of the mini-buffer contents. */
493
494 Lisp_Object minibuf_prompt;
495
496 /* Width of current mini-buffer prompt. Only set after display_line
497 of the line that contains the prompt. */
498
499 int minibuf_prompt_width;
500
501 /* This is the window where the echo area message was displayed. It
502 is always a mini-buffer window, but it may not be the same window
503 currently active as a mini-buffer. */
504
505 Lisp_Object echo_area_window;
506
507 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
508 pushes the current message and the value of
509 message_enable_multibyte on the stack, the function restore_message
510 pops the stack and displays MESSAGE again. */
511
512 Lisp_Object Vmessage_stack;
513
514 /* Nonzero means multibyte characters were enabled when the echo area
515 message was specified. */
516
517 int message_enable_multibyte;
518
519 /* Nonzero if we should redraw the mode lines on the next redisplay. */
520
521 int update_mode_lines;
522
523 /* Nonzero if window sizes or contents have changed since last
524 redisplay that finished. */
525
526 int windows_or_buffers_changed;
527
528 /* Nonzero means a frame's cursor type has been changed. */
529
530 int cursor_type_changed;
531
532 /* Nonzero after display_mode_line if %l was used and it displayed a
533 line number. */
534
535 int line_number_displayed;
536
537 /* Maximum buffer size for which to display line numbers. */
538
539 Lisp_Object Vline_number_display_limit;
540
541 /* Line width to consider when repositioning for line number display. */
542
543 static EMACS_INT line_number_display_limit_width;
544
545 /* Number of lines to keep in the message log buffer. t means
546 infinite. nil means don't log at all. */
547
548 Lisp_Object Vmessage_log_max;
549
550 /* The name of the *Messages* buffer, a string. */
551
552 static Lisp_Object Vmessages_buffer_name;
553
554 /* Current, index 0, and last displayed echo area message. Either
555 buffers from echo_buffers, or nil to indicate no message. */
556
557 Lisp_Object echo_area_buffer[2];
558
559 /* The buffers referenced from echo_area_buffer. */
560
561 static Lisp_Object echo_buffer[2];
562
563 /* A vector saved used in with_area_buffer to reduce consing. */
564
565 static Lisp_Object Vwith_echo_area_save_vector;
566
567 /* Non-zero means display_echo_area should display the last echo area
568 message again. Set by redisplay_preserve_echo_area. */
569
570 static int display_last_displayed_message_p;
571
572 /* Nonzero if echo area is being used by print; zero if being used by
573 message. */
574
575 int message_buf_print;
576
577 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
578
579 Lisp_Object Qinhibit_menubar_update;
580 int inhibit_menubar_update;
581
582 /* Maximum height for resizing mini-windows. Either a float
583 specifying a fraction of the available height, or an integer
584 specifying a number of lines. */
585
586 Lisp_Object Vmax_mini_window_height;
587
588 /* Non-zero means messages should be displayed with truncated
589 lines instead of being continued. */
590
591 int message_truncate_lines;
592 Lisp_Object Qmessage_truncate_lines;
593
594 /* Set to 1 in clear_message to make redisplay_internal aware
595 of an emptied echo area. */
596
597 static int message_cleared_p;
598
599 /* Non-zero means we want a hollow cursor in windows that are not
600 selected. Zero means there's no cursor in such windows. */
601
602 Lisp_Object Vcursor_in_non_selected_windows;
603 Lisp_Object Qcursor_in_non_selected_windows;
604
605 /* How to blink the default frame cursor off. */
606 Lisp_Object Vblink_cursor_alist;
607
608 /* A scratch glyph row with contents used for generating truncation
609 glyphs. Also used in direct_output_for_insert. */
610
611 #define MAX_SCRATCH_GLYPHS 100
612 struct glyph_row scratch_glyph_row;
613 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
614
615 /* Ascent and height of the last line processed by move_it_to. */
616
617 static int last_max_ascent, last_height;
618
619 /* Non-zero if there's a help-echo in the echo area. */
620
621 int help_echo_showing_p;
622
623 /* If >= 0, computed, exact values of mode-line and header-line height
624 to use in the macros CURRENT_MODE_LINE_HEIGHT and
625 CURRENT_HEADER_LINE_HEIGHT. */
626
627 int current_mode_line_height, current_header_line_height;
628
629 /* The maximum distance to look ahead for text properties. Values
630 that are too small let us call compute_char_face and similar
631 functions too often which is expensive. Values that are too large
632 let us call compute_char_face and alike too often because we
633 might not be interested in text properties that far away. */
634
635 #define TEXT_PROP_DISTANCE_LIMIT 100
636
637 #if GLYPH_DEBUG
638
639 /* Variables to turn off display optimizations from Lisp. */
640
641 int inhibit_try_window_id, inhibit_try_window_reusing;
642 int inhibit_try_cursor_movement;
643
644 /* Non-zero means print traces of redisplay if compiled with
645 GLYPH_DEBUG != 0. */
646
647 int trace_redisplay_p;
648
649 #endif /* GLYPH_DEBUG */
650
651 #ifdef DEBUG_TRACE_MOVE
652 /* Non-zero means trace with TRACE_MOVE to stderr. */
653 int trace_move;
654
655 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
656 #else
657 #define TRACE_MOVE(x) (void) 0
658 #endif
659
660 /* Non-zero means automatically scroll windows horizontally to make
661 point visible. */
662
663 int automatic_hscrolling_p;
664
665 /* How close to the margin can point get before the window is scrolled
666 horizontally. */
667 EMACS_INT hscroll_margin;
668
669 /* How much to scroll horizontally when point is inside the above margin. */
670 Lisp_Object Vhscroll_step;
671
672 /* A list of symbols, one for each supported image type. */
673
674 Lisp_Object Vimage_types;
675
676 /* The variable `resize-mini-windows'. If nil, don't resize
677 mini-windows. If t, always resize them to fit the text they
678 display. If `grow-only', let mini-windows grow only until they
679 become empty. */
680
681 Lisp_Object Vresize_mini_windows;
682
683 /* Buffer being redisplayed -- for redisplay_window_error. */
684
685 struct buffer *displayed_buffer;
686
687 /* Value returned from text property handlers (see below). */
688
689 enum prop_handled
690 {
691 HANDLED_NORMALLY,
692 HANDLED_RECOMPUTE_PROPS,
693 HANDLED_OVERLAY_STRING_CONSUMED,
694 HANDLED_RETURN
695 };
696
697 /* A description of text properties that redisplay is interested
698 in. */
699
700 struct props
701 {
702 /* The name of the property. */
703 Lisp_Object *name;
704
705 /* A unique index for the property. */
706 enum prop_idx idx;
707
708 /* A handler function called to set up iterator IT from the property
709 at IT's current position. Value is used to steer handle_stop. */
710 enum prop_handled (*handler) P_ ((struct it *it));
711 };
712
713 static enum prop_handled handle_face_prop P_ ((struct it *));
714 static enum prop_handled handle_invisible_prop P_ ((struct it *));
715 static enum prop_handled handle_display_prop P_ ((struct it *));
716 static enum prop_handled handle_composition_prop P_ ((struct it *));
717 static enum prop_handled handle_overlay_change P_ ((struct it *));
718 static enum prop_handled handle_fontified_prop P_ ((struct it *));
719
720 /* Properties handled by iterators. */
721
722 static struct props it_props[] =
723 {
724 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
725 /* Handle `face' before `display' because some sub-properties of
726 `display' need to know the face. */
727 {&Qface, FACE_PROP_IDX, handle_face_prop},
728 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
729 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
730 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
731 {NULL, 0, NULL}
732 };
733
734 /* Value is the position described by X. If X is a marker, value is
735 the marker_position of X. Otherwise, value is X. */
736
737 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
738
739 /* Enumeration returned by some move_it_.* functions internally. */
740
741 enum move_it_result
742 {
743 /* Not used. Undefined value. */
744 MOVE_UNDEFINED,
745
746 /* Move ended at the requested buffer position or ZV. */
747 MOVE_POS_MATCH_OR_ZV,
748
749 /* Move ended at the requested X pixel position. */
750 MOVE_X_REACHED,
751
752 /* Move within a line ended at the end of a line that must be
753 continued. */
754 MOVE_LINE_CONTINUED,
755
756 /* Move within a line ended at the end of a line that would
757 be displayed truncated. */
758 MOVE_LINE_TRUNCATED,
759
760 /* Move within a line ended at a line end. */
761 MOVE_NEWLINE_OR_CR
762 };
763
764 /* This counter is used to clear the face cache every once in a while
765 in redisplay_internal. It is incremented for each redisplay.
766 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
767 cleared. */
768
769 #define CLEAR_FACE_CACHE_COUNT 500
770 static int clear_face_cache_count;
771
772 /* Non-zero while redisplay_internal is in progress. */
773
774 int redisplaying_p;
775
776 /* Non-zero means don't free realized faces. Bound while freeing
777 realized faces is dangerous because glyph matrices might still
778 reference them. */
779
780 int inhibit_free_realized_faces;
781 Lisp_Object Qinhibit_free_realized_faces;
782
783 /* If a string, XTread_socket generates an event to display that string.
784 (The display is done in read_char.) */
785
786 Lisp_Object help_echo_string;
787 Lisp_Object help_echo_window;
788 Lisp_Object help_echo_object;
789 int help_echo_pos;
790
791 /* Temporary variable for XTread_socket. */
792
793 Lisp_Object previous_help_echo_string;
794
795
796 \f
797 /* Function prototypes. */
798
799 static void setup_for_ellipsis P_ ((struct it *));
800 static void mark_window_display_accurate_1 P_ ((struct window *, int));
801 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
802 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
803 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
804 static int redisplay_mode_lines P_ ((Lisp_Object, int));
805 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
806
807 #if 0
808 static int invisible_text_between_p P_ ((struct it *, int, int));
809 #endif
810
811 static int next_element_from_ellipsis P_ ((struct it *));
812 static void pint2str P_ ((char *, int, int));
813 static void pint2hrstr P_ ((char *, int, int));
814 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
815 struct text_pos));
816 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
817 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
818 static void store_frame_title_char P_ ((char));
819 static int store_frame_title P_ ((const unsigned char *, int, int));
820 static void x_consider_frame_title P_ ((Lisp_Object));
821 static void handle_stop P_ ((struct it *));
822 static int tool_bar_lines_needed P_ ((struct frame *));
823 static int single_display_prop_intangible_p P_ ((Lisp_Object));
824 static void ensure_echo_area_buffers P_ ((void));
825 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
826 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
827 static int with_echo_area_buffer P_ ((struct window *, int,
828 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
829 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
830 static void clear_garbaged_frames P_ ((void));
831 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
832 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
833 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
834 static int display_echo_area P_ ((struct window *));
835 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
836 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
838 static int string_char_and_length P_ ((const unsigned char *, int, int *));
839 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
840 struct text_pos));
841 static int compute_window_start_on_continuation_line P_ ((struct window *));
842 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
843 static void insert_left_trunc_glyphs P_ ((struct it *));
844 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
845 Lisp_Object));
846 static void extend_face_to_end_of_line P_ ((struct it *));
847 static int append_space P_ ((struct it *, int));
848 static int make_cursor_line_fully_visible P_ ((struct window *));
849 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
850 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
851 static int trailing_whitespace_p P_ ((int));
852 static int message_log_check_duplicate P_ ((int, int, int, int));
853 static void push_it P_ ((struct it *));
854 static void pop_it P_ ((struct it *));
855 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
856 static void select_frame_for_redisplay P_ ((Lisp_Object));
857 static void redisplay_internal P_ ((int));
858 static int echo_area_display P_ ((int));
859 static void redisplay_windows P_ ((Lisp_Object));
860 static void redisplay_window P_ ((Lisp_Object, int));
861 static Lisp_Object redisplay_window_error ();
862 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
863 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
864 static void update_menu_bar P_ ((struct frame *, int));
865 static int try_window_reusing_current_matrix P_ ((struct window *));
866 static int try_window_id P_ ((struct window *));
867 static int display_line P_ ((struct it *));
868 static int display_mode_lines P_ ((struct window *));
869 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
870 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
871 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
872 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
873 static void display_menu_bar P_ ((struct window *));
874 static int display_count_lines P_ ((int, int, int, int, int *));
875 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
876 int, int, struct it *, int, int, int, int));
877 static void compute_line_metrics P_ ((struct it *));
878 static void run_redisplay_end_trigger_hook P_ ((struct it *));
879 static int get_overlay_strings P_ ((struct it *, int));
880 static void next_overlay_string P_ ((struct it *));
881 static void reseat P_ ((struct it *, struct text_pos, int));
882 static void reseat_1 P_ ((struct it *, struct text_pos, int));
883 static void back_to_previous_visible_line_start P_ ((struct it *));
884 static void reseat_at_previous_visible_line_start P_ ((struct it *));
885 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
886 static int next_element_from_display_vector P_ ((struct it *));
887 static int next_element_from_string P_ ((struct it *));
888 static int next_element_from_c_string P_ ((struct it *));
889 static int next_element_from_buffer P_ ((struct it *));
890 static int next_element_from_composition P_ ((struct it *));
891 static int next_element_from_image P_ ((struct it *));
892 static int next_element_from_stretch P_ ((struct it *));
893 static void load_overlay_strings P_ ((struct it *, int));
894 static int init_from_display_pos P_ ((struct it *, struct window *,
895 struct display_pos *));
896 static void reseat_to_string P_ ((struct it *, unsigned char *,
897 Lisp_Object, int, int, int, int));
898 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
899 int, int, int));
900 void move_it_vertically_backward P_ ((struct it *, int));
901 static void init_to_row_start P_ ((struct it *, struct window *,
902 struct glyph_row *));
903 static int init_to_row_end P_ ((struct it *, struct window *,
904 struct glyph_row *));
905 static void back_to_previous_line_start P_ ((struct it *));
906 static int forward_to_next_line_start P_ ((struct it *, int *));
907 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
908 Lisp_Object, int));
909 static struct text_pos string_pos P_ ((int, Lisp_Object));
910 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
911 static int number_of_chars P_ ((unsigned char *, int));
912 static void compute_stop_pos P_ ((struct it *));
913 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
914 Lisp_Object));
915 static int face_before_or_after_it_pos P_ ((struct it *, int));
916 static int next_overlay_change P_ ((int));
917 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
918 Lisp_Object, struct text_pos *,
919 int));
920 static int underlying_face_id P_ ((struct it *));
921 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
922 struct window *));
923
924 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
925 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
926
927 #ifdef HAVE_WINDOW_SYSTEM
928
929 static void update_tool_bar P_ ((struct frame *, int));
930 static void build_desired_tool_bar_string P_ ((struct frame *f));
931 static int redisplay_tool_bar P_ ((struct frame *));
932 static void display_tool_bar_line P_ ((struct it *));
933 static void notice_overwritten_cursor P_ ((struct window *,
934 enum glyph_row_area,
935 int, int, int, int));
936
937
938
939 #endif /* HAVE_WINDOW_SYSTEM */
940
941 \f
942 /***********************************************************************
943 Window display dimensions
944 ***********************************************************************/
945
946 /* Return the bottom boundary y-position for text lines in window W.
947 This is the first y position at which a line cannot start.
948 It is relative to the top of the window.
949
950 This is the height of W minus the height of a mode line, if any. */
951
952 INLINE int
953 window_text_bottom_y (w)
954 struct window *w;
955 {
956 int height = WINDOW_TOTAL_HEIGHT (w);
957
958 if (WINDOW_WANTS_MODELINE_P (w))
959 height -= CURRENT_MODE_LINE_HEIGHT (w);
960 return height;
961 }
962
963 /* Return the pixel width of display area AREA of window W. AREA < 0
964 means return the total width of W, not including fringes to
965 the left and right of the window. */
966
967 INLINE int
968 window_box_width (w, area)
969 struct window *w;
970 int area;
971 {
972 int cols = XFASTINT (w->total_cols);
973 int pixels = 0;
974
975 if (!w->pseudo_window_p)
976 {
977 cols -= WINDOW_SCROLL_BAR_COLS (w);
978
979 if (area == TEXT_AREA)
980 {
981 if (INTEGERP (w->left_margin_cols))
982 cols -= XFASTINT (w->left_margin_cols);
983 if (INTEGERP (w->right_margin_cols))
984 cols -= XFASTINT (w->right_margin_cols);
985 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
986 }
987 else if (area == LEFT_MARGIN_AREA)
988 {
989 cols = (INTEGERP (w->left_margin_cols)
990 ? XFASTINT (w->left_margin_cols) : 0);
991 pixels = 0;
992 }
993 else if (area == RIGHT_MARGIN_AREA)
994 {
995 cols = (INTEGERP (w->right_margin_cols)
996 ? XFASTINT (w->right_margin_cols) : 0);
997 pixels = 0;
998 }
999 }
1000
1001 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1002 }
1003
1004
1005 /* Return the pixel height of the display area of window W, not
1006 including mode lines of W, if any. */
1007
1008 INLINE int
1009 window_box_height (w)
1010 struct window *w;
1011 {
1012 struct frame *f = XFRAME (w->frame);
1013 int height = WINDOW_TOTAL_HEIGHT (w);
1014
1015 xassert (height >= 0);
1016
1017 /* Note: the code below that determines the mode-line/header-line
1018 height is essentially the same as that contained in the macro
1019 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1020 the appropriate glyph row has its `mode_line_p' flag set,
1021 and if it doesn't, uses estimate_mode_line_height instead. */
1022
1023 if (WINDOW_WANTS_MODELINE_P (w))
1024 {
1025 struct glyph_row *ml_row
1026 = (w->current_matrix && w->current_matrix->rows
1027 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1028 : 0);
1029 if (ml_row && ml_row->mode_line_p)
1030 height -= ml_row->height;
1031 else
1032 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1033 }
1034
1035 if (WINDOW_WANTS_HEADER_LINE_P (w))
1036 {
1037 struct glyph_row *hl_row
1038 = (w->current_matrix && w->current_matrix->rows
1039 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1040 : 0);
1041 if (hl_row && hl_row->mode_line_p)
1042 height -= hl_row->height;
1043 else
1044 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1045 }
1046
1047 /* With a very small font and a mode-line that's taller than
1048 default, we might end up with a negative height. */
1049 return max (0, height);
1050 }
1051
1052 /* Return the window-relative coordinate of the left edge of display
1053 area AREA of window W. AREA < 0 means return the left edge of the
1054 whole window, to the right of the left fringe of W. */
1055
1056 INLINE int
1057 window_box_left_offset (w, area)
1058 struct window *w;
1059 int area;
1060 {
1061 int x;
1062
1063 if (w->pseudo_window_p)
1064 return 0;
1065
1066 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1067
1068 if (area == TEXT_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA));
1071 else if (area == RIGHT_MARGIN_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA)
1074 + window_box_width (w, TEXT_AREA)
1075 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1076 ? 0
1077 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1078 else if (area == LEFT_MARGIN_AREA
1079 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1080 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1081
1082 return x;
1083 }
1084
1085
1086 /* Return the window-relative coordinate of the right edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the left of the right fringe of W. */
1089
1090 INLINE int
1091 window_box_right_offset (w, area)
1092 struct window *w;
1093 int area;
1094 {
1095 return window_box_left_offset (w, area) + window_box_width (w, area);
1096 }
1097
1098 /* Return the frame-relative coordinate of the left edge of display
1099 area AREA of window W. AREA < 0 means return the left edge of the
1100 whole window, to the right of the left fringe of W. */
1101
1102 INLINE int
1103 window_box_left (w, area)
1104 struct window *w;
1105 int area;
1106 {
1107 struct frame *f = XFRAME (w->frame);
1108 int x;
1109
1110 if (w->pseudo_window_p)
1111 return FRAME_INTERNAL_BORDER_WIDTH (f);
1112
1113 x = (WINDOW_LEFT_EDGE_X (w)
1114 + window_box_left_offset (w, area));
1115
1116 return x;
1117 }
1118
1119
1120 /* Return the frame-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124 INLINE int
1125 window_box_right (w, area)
1126 struct window *w;
1127 int area;
1128 {
1129 return window_box_left (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines, in frame-relative coordinates. AREA < 0 means the
1134 whole window, not including the left and right fringes of
1135 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1136 coordinates of the upper-left corner of the box. Return in
1137 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1138
1139 INLINE void
1140 window_box (w, area, box_x, box_y, box_width, box_height)
1141 struct window *w;
1142 int area;
1143 int *box_x, *box_y, *box_width, *box_height;
1144 {
1145 if (box_width)
1146 *box_width = window_box_width (w, area);
1147 if (box_height)
1148 *box_height = window_box_height (w);
1149 if (box_x)
1150 *box_x = window_box_left (w, area);
1151 if (box_y)
1152 {
1153 *box_y = WINDOW_TOP_EDGE_Y (w);
1154 if (WINDOW_WANTS_HEADER_LINE_P (w))
1155 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1156 }
1157 }
1158
1159
1160 /* Get the bounding box of the display area AREA of window W, without
1161 mode lines. AREA < 0 means the whole window, not including the
1162 left and right fringe of the window. Return in *TOP_LEFT_X
1163 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1164 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1165 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1166 box. */
1167
1168 INLINE void
1169 window_box_edges (w, area, top_left_x, top_left_y,
1170 bottom_right_x, bottom_right_y)
1171 struct window *w;
1172 int area;
1173 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1174 {
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1179 }
1180
1181
1182 \f
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1186
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1189
1190 int
1191 line_bottom_y (it)
1192 struct it *it;
1193 {
1194 int line_height = it->max_ascent + it->max_descent;
1195 int line_top_y = it->current_y;
1196
1197 if (line_height == 0)
1198 {
1199 if (last_height)
1200 line_height = last_height;
1201 else if (IT_CHARPOS (*it) < ZV)
1202 {
1203 move_it_by_lines (it, 1, 1);
1204 line_height = (it->max_ascent || it->max_descent
1205 ? it->max_ascent + it->max_descent
1206 : last_height);
1207 }
1208 else
1209 {
1210 struct glyph_row *row = it->glyph_row;
1211
1212 /* Use the default character height. */
1213 it->glyph_row = NULL;
1214 it->what = IT_CHARACTER;
1215 it->c = ' ';
1216 it->len = 1;
1217 PRODUCE_GLYPHS (it);
1218 line_height = it->ascent + it->descent;
1219 it->glyph_row = row;
1220 }
1221 }
1222
1223 return line_top_y + line_height;
1224 }
1225
1226
1227 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1228 1 if POS is visible and the line containing POS is fully visible.
1229 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1230 and header-lines heights. */
1231
1232 int
1233 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1234 struct window *w;
1235 int charpos, *fully, exact_mode_line_heights_p;
1236 {
1237 struct it it;
1238 struct text_pos top;
1239 int visible_p;
1240 struct buffer *old_buffer = NULL;
1241
1242 if (XBUFFER (w->buffer) != current_buffer)
1243 {
1244 old_buffer = current_buffer;
1245 set_buffer_internal_1 (XBUFFER (w->buffer));
1246 }
1247
1248 *fully = visible_p = 0;
1249 SET_TEXT_POS_FROM_MARKER (top, w->start);
1250
1251 /* Compute exact mode line heights, if requested. */
1252 if (exact_mode_line_heights_p)
1253 {
1254 if (WINDOW_WANTS_MODELINE_P (w))
1255 current_mode_line_height
1256 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1257 current_buffer->mode_line_format);
1258
1259 if (WINDOW_WANTS_HEADER_LINE_P (w))
1260 current_header_line_height
1261 = display_mode_line (w, HEADER_LINE_FACE_ID,
1262 current_buffer->header_line_format);
1263 }
1264
1265 start_display (&it, w, top);
1266 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1267 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1268
1269 /* Note that we may overshoot because of invisible text. */
1270 if (IT_CHARPOS (it) >= charpos)
1271 {
1272 int top_y = it.current_y;
1273 int bottom_y = line_bottom_y (&it);
1274 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1275
1276 if (top_y < window_top_y)
1277 visible_p = bottom_y > window_top_y;
1278 else if (top_y < it.last_visible_y)
1279 {
1280 visible_p = 1;
1281 *fully = bottom_y <= it.last_visible_y;
1282 }
1283 }
1284 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1285 {
1286 move_it_by_lines (&it, 1, 0);
1287 if (charpos < IT_CHARPOS (it))
1288 {
1289 visible_p = 1;
1290 *fully = 0;
1291 }
1292 }
1293
1294 if (old_buffer)
1295 set_buffer_internal_1 (old_buffer);
1296
1297 current_header_line_height = current_mode_line_height = -1;
1298 return visible_p;
1299 }
1300
1301
1302 /* Return the next character from STR which is MAXLEN bytes long.
1303 Return in *LEN the length of the character. This is like
1304 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1305 we find one, we return a `?', but with the length of the invalid
1306 character. */
1307
1308 static INLINE int
1309 string_char_and_length (str, maxlen, len)
1310 const unsigned char *str;
1311 int maxlen, *len;
1312 {
1313 int c;
1314
1315 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1316 if (!CHAR_VALID_P (c, 1))
1317 /* We may not change the length here because other places in Emacs
1318 don't use this function, i.e. they silently accept invalid
1319 characters. */
1320 c = '?';
1321
1322 return c;
1323 }
1324
1325
1326
1327 /* Given a position POS containing a valid character and byte position
1328 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1329
1330 static struct text_pos
1331 string_pos_nchars_ahead (pos, string, nchars)
1332 struct text_pos pos;
1333 Lisp_Object string;
1334 int nchars;
1335 {
1336 xassert (STRINGP (string) && nchars >= 0);
1337
1338 if (STRING_MULTIBYTE (string))
1339 {
1340 int rest = SBYTES (string) - BYTEPOS (pos);
1341 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1342 int len;
1343
1344 while (nchars--)
1345 {
1346 string_char_and_length (p, rest, &len);
1347 p += len, rest -= len;
1348 xassert (rest >= 0);
1349 CHARPOS (pos) += 1;
1350 BYTEPOS (pos) += len;
1351 }
1352 }
1353 else
1354 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1355
1356 return pos;
1357 }
1358
1359
1360 /* Value is the text position, i.e. character and byte position,
1361 for character position CHARPOS in STRING. */
1362
1363 static INLINE struct text_pos
1364 string_pos (charpos, string)
1365 int charpos;
1366 Lisp_Object string;
1367 {
1368 struct text_pos pos;
1369 xassert (STRINGP (string));
1370 xassert (charpos >= 0);
1371 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1372 return pos;
1373 }
1374
1375
1376 /* Value is a text position, i.e. character and byte position, for
1377 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1378 means recognize multibyte characters. */
1379
1380 static struct text_pos
1381 c_string_pos (charpos, s, multibyte_p)
1382 int charpos;
1383 unsigned char *s;
1384 int multibyte_p;
1385 {
1386 struct text_pos pos;
1387
1388 xassert (s != NULL);
1389 xassert (charpos >= 0);
1390
1391 if (multibyte_p)
1392 {
1393 int rest = strlen (s), len;
1394
1395 SET_TEXT_POS (pos, 0, 0);
1396 while (charpos--)
1397 {
1398 string_char_and_length (s, rest, &len);
1399 s += len, rest -= len;
1400 xassert (rest >= 0);
1401 CHARPOS (pos) += 1;
1402 BYTEPOS (pos) += len;
1403 }
1404 }
1405 else
1406 SET_TEXT_POS (pos, charpos, charpos);
1407
1408 return pos;
1409 }
1410
1411
1412 /* Value is the number of characters in C string S. MULTIBYTE_P
1413 non-zero means recognize multibyte characters. */
1414
1415 static int
1416 number_of_chars (s, multibyte_p)
1417 unsigned char *s;
1418 int multibyte_p;
1419 {
1420 int nchars;
1421
1422 if (multibyte_p)
1423 {
1424 int rest = strlen (s), len;
1425 unsigned char *p = (unsigned char *) s;
1426
1427 for (nchars = 0; rest > 0; ++nchars)
1428 {
1429 string_char_and_length (p, rest, &len);
1430 rest -= len, p += len;
1431 }
1432 }
1433 else
1434 nchars = strlen (s);
1435
1436 return nchars;
1437 }
1438
1439
1440 /* Compute byte position NEWPOS->bytepos corresponding to
1441 NEWPOS->charpos. POS is a known position in string STRING.
1442 NEWPOS->charpos must be >= POS.charpos. */
1443
1444 static void
1445 compute_string_pos (newpos, pos, string)
1446 struct text_pos *newpos, pos;
1447 Lisp_Object string;
1448 {
1449 xassert (STRINGP (string));
1450 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1451
1452 if (STRING_MULTIBYTE (string))
1453 *newpos = string_pos_nchars_ahead (pos, string,
1454 CHARPOS (*newpos) - CHARPOS (pos));
1455 else
1456 BYTEPOS (*newpos) = CHARPOS (*newpos);
1457 }
1458
1459 /* EXPORT:
1460 Return an estimation of the pixel height of mode or top lines on
1461 frame F. FACE_ID specifies what line's height to estimate. */
1462
1463 int
1464 estimate_mode_line_height (f, face_id)
1465 struct frame *f;
1466 enum face_id face_id;
1467 {
1468 #ifdef HAVE_WINDOW_SYSTEM
1469 if (FRAME_WINDOW_P (f))
1470 {
1471 int height = FONT_HEIGHT (FRAME_FONT (f));
1472
1473 /* This function is called so early when Emacs starts that the face
1474 cache and mode line face are not yet initialized. */
1475 if (FRAME_FACE_CACHE (f))
1476 {
1477 struct face *face = FACE_FROM_ID (f, face_id);
1478 if (face)
1479 {
1480 if (face->font)
1481 height = FONT_HEIGHT (face->font);
1482 if (face->box_line_width > 0)
1483 height += 2 * face->box_line_width;
1484 }
1485 }
1486
1487 return height;
1488 }
1489 #endif
1490
1491 return 1;
1492 }
1493
1494 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1495 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1496 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1497 not force the value into range. */
1498
1499 void
1500 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1501 FRAME_PTR f;
1502 register int pix_x, pix_y;
1503 int *x, *y;
1504 NativeRectangle *bounds;
1505 int noclip;
1506 {
1507
1508 #ifdef HAVE_WINDOW_SYSTEM
1509 if (FRAME_WINDOW_P (f))
1510 {
1511 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1512 even for negative values. */
1513 if (pix_x < 0)
1514 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1515 if (pix_y < 0)
1516 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1517
1518 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1519 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1520
1521 if (bounds)
1522 STORE_NATIVE_RECT (*bounds,
1523 FRAME_COL_TO_PIXEL_X (f, pix_x),
1524 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1525 FRAME_COLUMN_WIDTH (f) - 1,
1526 FRAME_LINE_HEIGHT (f) - 1);
1527
1528 if (!noclip)
1529 {
1530 if (pix_x < 0)
1531 pix_x = 0;
1532 else if (pix_x > FRAME_TOTAL_COLS (f))
1533 pix_x = FRAME_TOTAL_COLS (f);
1534
1535 if (pix_y < 0)
1536 pix_y = 0;
1537 else if (pix_y > FRAME_LINES (f))
1538 pix_y = FRAME_LINES (f);
1539 }
1540 }
1541 #endif
1542
1543 *x = pix_x;
1544 *y = pix_y;
1545 }
1546
1547
1548 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1549 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1550 can't tell the positions because W's display is not up to date,
1551 return 0. */
1552
1553 int
1554 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1555 struct window *w;
1556 int hpos, vpos;
1557 int *frame_x, *frame_y;
1558 {
1559 #ifdef HAVE_WINDOW_SYSTEM
1560 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1561 {
1562 int success_p;
1563
1564 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1565 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1566
1567 if (display_completed)
1568 {
1569 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1570 struct glyph *glyph = row->glyphs[TEXT_AREA];
1571 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1572
1573 hpos = row->x;
1574 vpos = row->y;
1575 while (glyph < end)
1576 {
1577 hpos += glyph->pixel_width;
1578 ++glyph;
1579 }
1580
1581 /* If first glyph is partially visible, its first visible position is still 0. */
1582 if (hpos < 0)
1583 hpos = 0;
1584
1585 success_p = 1;
1586 }
1587 else
1588 {
1589 hpos = vpos = 0;
1590 success_p = 0;
1591 }
1592
1593 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1594 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1595 return success_p;
1596 }
1597 #endif
1598
1599 *frame_x = hpos;
1600 *frame_y = vpos;
1601 return 1;
1602 }
1603
1604
1605 #ifdef HAVE_WINDOW_SYSTEM
1606
1607 /* Find the glyph under window-relative coordinates X/Y in window W.
1608 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1609 strings. Return in *HPOS and *VPOS the row and column number of
1610 the glyph found. Return in *AREA the glyph area containing X.
1611 Value is a pointer to the glyph found or null if X/Y is not on
1612 text, or we can't tell because W's current matrix is not up to
1613 date. */
1614
1615 static struct glyph *
1616 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1617 struct window *w;
1618 int x, y;
1619 int *hpos, *vpos, *dx, *dy, *area;
1620 {
1621 struct glyph *glyph, *end;
1622 struct glyph_row *row = NULL;
1623 int x0, i;
1624
1625 /* Find row containing Y. Give up if some row is not enabled. */
1626 for (i = 0; i < w->current_matrix->nrows; ++i)
1627 {
1628 row = MATRIX_ROW (w->current_matrix, i);
1629 if (!row->enabled_p)
1630 return NULL;
1631 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1632 break;
1633 }
1634
1635 *vpos = i;
1636 *hpos = 0;
1637
1638 /* Give up if Y is not in the window. */
1639 if (i == w->current_matrix->nrows)
1640 return NULL;
1641
1642 /* Get the glyph area containing X. */
1643 if (w->pseudo_window_p)
1644 {
1645 *area = TEXT_AREA;
1646 x0 = 0;
1647 }
1648 else
1649 {
1650 if (x < window_box_left_offset (w, TEXT_AREA))
1651 {
1652 *area = LEFT_MARGIN_AREA;
1653 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1654 }
1655 else if (x < window_box_right_offset (w, TEXT_AREA))
1656 {
1657 *area = TEXT_AREA;
1658 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1659 }
1660 else
1661 {
1662 *area = RIGHT_MARGIN_AREA;
1663 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1664 }
1665 }
1666
1667 /* Find glyph containing X. */
1668 glyph = row->glyphs[*area];
1669 end = glyph + row->used[*area];
1670 x -= x0;
1671 while (glyph < end && x >= glyph->pixel_width)
1672 {
1673 x -= glyph->pixel_width;
1674 ++glyph;
1675 }
1676
1677 if (glyph == end)
1678 return NULL;
1679
1680 if (dx)
1681 {
1682 *dx = x;
1683 *dy = y - (row->y + row->ascent - glyph->ascent);
1684 }
1685
1686 *hpos = glyph - row->glyphs[*area];
1687 return glyph;
1688 }
1689
1690
1691 /* EXPORT:
1692 Convert frame-relative x/y to coordinates relative to window W.
1693 Takes pseudo-windows into account. */
1694
1695 void
1696 frame_to_window_pixel_xy (w, x, y)
1697 struct window *w;
1698 int *x, *y;
1699 {
1700 if (w->pseudo_window_p)
1701 {
1702 /* A pseudo-window is always full-width, and starts at the
1703 left edge of the frame, plus a frame border. */
1704 struct frame *f = XFRAME (w->frame);
1705 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1706 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1707 }
1708 else
1709 {
1710 *x -= WINDOW_LEFT_EDGE_X (w);
1711 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1712 }
1713 }
1714
1715 /* EXPORT:
1716 Return in *R the clipping rectangle for glyph string S. */
1717
1718 void
1719 get_glyph_string_clip_rect (s, nr)
1720 struct glyph_string *s;
1721 NativeRectangle *nr;
1722 {
1723 XRectangle r;
1724
1725 if (s->row->full_width_p)
1726 {
1727 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1728 r.x = WINDOW_LEFT_EDGE_X (s->w);
1729 r.width = WINDOW_TOTAL_WIDTH (s->w);
1730
1731 /* Unless displaying a mode or menu bar line, which are always
1732 fully visible, clip to the visible part of the row. */
1733 if (s->w->pseudo_window_p)
1734 r.height = s->row->visible_height;
1735 else
1736 r.height = s->height;
1737 }
1738 else
1739 {
1740 /* This is a text line that may be partially visible. */
1741 r.x = window_box_left (s->w, s->area);
1742 r.width = window_box_width (s->w, s->area);
1743 r.height = s->row->visible_height;
1744 }
1745
1746 /* If S draws overlapping rows, it's sufficient to use the top and
1747 bottom of the window for clipping because this glyph string
1748 intentionally draws over other lines. */
1749 if (s->for_overlaps_p)
1750 {
1751 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1752 r.height = window_text_bottom_y (s->w) - r.y;
1753 }
1754 else
1755 {
1756 /* Don't use S->y for clipping because it doesn't take partially
1757 visible lines into account. For example, it can be negative for
1758 partially visible lines at the top of a window. */
1759 if (!s->row->full_width_p
1760 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1761 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1762 else
1763 r.y = max (0, s->row->y);
1764
1765 /* If drawing a tool-bar window, draw it over the internal border
1766 at the top of the window. */
1767 if (s->w == XWINDOW (s->f->tool_bar_window))
1768 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1769 }
1770
1771 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1772
1773 /* If drawing the cursor, don't let glyph draw outside its
1774 advertised boundaries. Cleartype does this under some circumstances. */
1775 if (s->hl == DRAW_CURSOR)
1776 {
1777 struct glyph *glyph = s->first_glyph;
1778 int height;
1779
1780 if (s->x > r.x)
1781 {
1782 r.width -= s->x - r.x;
1783 r.x = s->x;
1784 }
1785 r.width = min (r.width, glyph->pixel_width);
1786
1787 /* Don't draw cursor glyph taller than our actual glyph. */
1788 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1789 if (height < r.height)
1790 {
1791 r.y = s->ybase + glyph->descent - height;
1792 r.height = height;
1793 }
1794 }
1795
1796 #ifdef CONVERT_FROM_XRECT
1797 CONVERT_FROM_XRECT (r, *nr);
1798 #else
1799 *nr = r;
1800 #endif
1801 }
1802
1803 #endif /* HAVE_WINDOW_SYSTEM */
1804
1805 \f
1806 /***********************************************************************
1807 Lisp form evaluation
1808 ***********************************************************************/
1809
1810 /* Error handler for safe_eval and safe_call. */
1811
1812 static Lisp_Object
1813 safe_eval_handler (arg)
1814 Lisp_Object arg;
1815 {
1816 add_to_log ("Error during redisplay: %s", arg, Qnil);
1817 return Qnil;
1818 }
1819
1820
1821 /* Evaluate SEXPR and return the result, or nil if something went
1822 wrong. Prevent redisplay during the evaluation. */
1823
1824 Lisp_Object
1825 safe_eval (sexpr)
1826 Lisp_Object sexpr;
1827 {
1828 Lisp_Object val;
1829
1830 if (inhibit_eval_during_redisplay)
1831 val = Qnil;
1832 else
1833 {
1834 int count = SPECPDL_INDEX ();
1835 struct gcpro gcpro1;
1836
1837 GCPRO1 (sexpr);
1838 specbind (Qinhibit_redisplay, Qt);
1839 /* Use Qt to ensure debugger does not run,
1840 so there is no possibility of wanting to redisplay. */
1841 val = internal_condition_case_1 (Feval, sexpr, Qt,
1842 safe_eval_handler);
1843 UNGCPRO;
1844 val = unbind_to (count, val);
1845 }
1846
1847 return val;
1848 }
1849
1850
1851 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1852 Return the result, or nil if something went wrong. Prevent
1853 redisplay during the evaluation. */
1854
1855 Lisp_Object
1856 safe_call (nargs, args)
1857 int nargs;
1858 Lisp_Object *args;
1859 {
1860 Lisp_Object val;
1861
1862 if (inhibit_eval_during_redisplay)
1863 val = Qnil;
1864 else
1865 {
1866 int count = SPECPDL_INDEX ();
1867 struct gcpro gcpro1;
1868
1869 GCPRO1 (args[0]);
1870 gcpro1.nvars = nargs;
1871 specbind (Qinhibit_redisplay, Qt);
1872 /* Use Qt to ensure debugger does not run,
1873 so there is no possibility of wanting to redisplay. */
1874 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1875 safe_eval_handler);
1876 UNGCPRO;
1877 val = unbind_to (count, val);
1878 }
1879
1880 return val;
1881 }
1882
1883
1884 /* Call function FN with one argument ARG.
1885 Return the result, or nil if something went wrong. */
1886
1887 Lisp_Object
1888 safe_call1 (fn, arg)
1889 Lisp_Object fn, arg;
1890 {
1891 Lisp_Object args[2];
1892 args[0] = fn;
1893 args[1] = arg;
1894 return safe_call (2, args);
1895 }
1896
1897
1898 \f
1899 /***********************************************************************
1900 Debugging
1901 ***********************************************************************/
1902
1903 #if 0
1904
1905 /* Define CHECK_IT to perform sanity checks on iterators.
1906 This is for debugging. It is too slow to do unconditionally. */
1907
1908 static void
1909 check_it (it)
1910 struct it *it;
1911 {
1912 if (it->method == next_element_from_string)
1913 {
1914 xassert (STRINGP (it->string));
1915 xassert (IT_STRING_CHARPOS (*it) >= 0);
1916 }
1917 else
1918 {
1919 xassert (IT_STRING_CHARPOS (*it) < 0);
1920 if (it->method == next_element_from_buffer)
1921 {
1922 /* Check that character and byte positions agree. */
1923 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1924 }
1925 }
1926
1927 if (it->dpvec)
1928 xassert (it->current.dpvec_index >= 0);
1929 else
1930 xassert (it->current.dpvec_index < 0);
1931 }
1932
1933 #define CHECK_IT(IT) check_it ((IT))
1934
1935 #else /* not 0 */
1936
1937 #define CHECK_IT(IT) (void) 0
1938
1939 #endif /* not 0 */
1940
1941
1942 #if GLYPH_DEBUG
1943
1944 /* Check that the window end of window W is what we expect it
1945 to be---the last row in the current matrix displaying text. */
1946
1947 static void
1948 check_window_end (w)
1949 struct window *w;
1950 {
1951 if (!MINI_WINDOW_P (w)
1952 && !NILP (w->window_end_valid))
1953 {
1954 struct glyph_row *row;
1955 xassert ((row = MATRIX_ROW (w->current_matrix,
1956 XFASTINT (w->window_end_vpos)),
1957 !row->enabled_p
1958 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1959 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1960 }
1961 }
1962
1963 #define CHECK_WINDOW_END(W) check_window_end ((W))
1964
1965 #else /* not GLYPH_DEBUG */
1966
1967 #define CHECK_WINDOW_END(W) (void) 0
1968
1969 #endif /* not GLYPH_DEBUG */
1970
1971
1972 \f
1973 /***********************************************************************
1974 Iterator initialization
1975 ***********************************************************************/
1976
1977 /* Initialize IT for displaying current_buffer in window W, starting
1978 at character position CHARPOS. CHARPOS < 0 means that no buffer
1979 position is specified which is useful when the iterator is assigned
1980 a position later. BYTEPOS is the byte position corresponding to
1981 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1982
1983 If ROW is not null, calls to produce_glyphs with IT as parameter
1984 will produce glyphs in that row.
1985
1986 BASE_FACE_ID is the id of a base face to use. It must be one of
1987 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1988 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1989 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1990
1991 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1992 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1993 will be initialized to use the corresponding mode line glyph row of
1994 the desired matrix of W. */
1995
1996 void
1997 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1998 struct it *it;
1999 struct window *w;
2000 int charpos, bytepos;
2001 struct glyph_row *row;
2002 enum face_id base_face_id;
2003 {
2004 int highlight_region_p;
2005
2006 /* Some precondition checks. */
2007 xassert (w != NULL && it != NULL);
2008 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2009 && charpos <= ZV));
2010
2011 /* If face attributes have been changed since the last redisplay,
2012 free realized faces now because they depend on face definitions
2013 that might have changed. Don't free faces while there might be
2014 desired matrices pending which reference these faces. */
2015 if (face_change_count && !inhibit_free_realized_faces)
2016 {
2017 face_change_count = 0;
2018 free_all_realized_faces (Qnil);
2019 }
2020
2021 /* Use one of the mode line rows of W's desired matrix if
2022 appropriate. */
2023 if (row == NULL)
2024 {
2025 if (base_face_id == MODE_LINE_FACE_ID
2026 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2027 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2028 else if (base_face_id == HEADER_LINE_FACE_ID)
2029 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2030 }
2031
2032 /* Clear IT. */
2033 bzero (it, sizeof *it);
2034 it->current.overlay_string_index = -1;
2035 it->current.dpvec_index = -1;
2036 it->base_face_id = base_face_id;
2037 it->string = Qnil;
2038 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2039
2040 /* The window in which we iterate over current_buffer: */
2041 XSETWINDOW (it->window, w);
2042 it->w = w;
2043 it->f = XFRAME (w->frame);
2044
2045 /* Extra space between lines (on window systems only). */
2046 if (base_face_id == DEFAULT_FACE_ID
2047 && FRAME_WINDOW_P (it->f))
2048 {
2049 if (NATNUMP (current_buffer->extra_line_spacing))
2050 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2051 else if (it->f->extra_line_spacing > 0)
2052 it->extra_line_spacing = it->f->extra_line_spacing;
2053 }
2054
2055 /* If realized faces have been removed, e.g. because of face
2056 attribute changes of named faces, recompute them. When running
2057 in batch mode, the face cache of the initial frame is null. If
2058 we happen to get called, make a dummy face cache. */
2059 if (FRAME_FACE_CACHE (it->f) == NULL)
2060 init_frame_faces (it->f);
2061 if (FRAME_FACE_CACHE (it->f)->used == 0)
2062 recompute_basic_faces (it->f);
2063
2064 /* Current value of the `space-width', and 'height' properties. */
2065 it->space_width = Qnil;
2066 it->font_height = Qnil;
2067
2068 /* Are control characters displayed as `^C'? */
2069 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2070
2071 /* -1 means everything between a CR and the following line end
2072 is invisible. >0 means lines indented more than this value are
2073 invisible. */
2074 it->selective = (INTEGERP (current_buffer->selective_display)
2075 ? XFASTINT (current_buffer->selective_display)
2076 : (!NILP (current_buffer->selective_display)
2077 ? -1 : 0));
2078 it->selective_display_ellipsis_p
2079 = !NILP (current_buffer->selective_display_ellipses);
2080
2081 /* Display table to use. */
2082 it->dp = window_display_table (w);
2083
2084 /* Are multibyte characters enabled in current_buffer? */
2085 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2086
2087 /* Non-zero if we should highlight the region. */
2088 highlight_region_p
2089 = (!NILP (Vtransient_mark_mode)
2090 && !NILP (current_buffer->mark_active)
2091 && XMARKER (current_buffer->mark)->buffer != 0);
2092
2093 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2094 start and end of a visible region in window IT->w. Set both to
2095 -1 to indicate no region. */
2096 if (highlight_region_p
2097 /* Maybe highlight only in selected window. */
2098 && (/* Either show region everywhere. */
2099 highlight_nonselected_windows
2100 /* Or show region in the selected window. */
2101 || w == XWINDOW (selected_window)
2102 /* Or show the region if we are in the mini-buffer and W is
2103 the window the mini-buffer refers to. */
2104 || (MINI_WINDOW_P (XWINDOW (selected_window))
2105 && WINDOWP (minibuf_selected_window)
2106 && w == XWINDOW (minibuf_selected_window))))
2107 {
2108 int charpos = marker_position (current_buffer->mark);
2109 it->region_beg_charpos = min (PT, charpos);
2110 it->region_end_charpos = max (PT, charpos);
2111 }
2112 else
2113 it->region_beg_charpos = it->region_end_charpos = -1;
2114
2115 /* Get the position at which the redisplay_end_trigger hook should
2116 be run, if it is to be run at all. */
2117 if (MARKERP (w->redisplay_end_trigger)
2118 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2119 it->redisplay_end_trigger_charpos
2120 = marker_position (w->redisplay_end_trigger);
2121 else if (INTEGERP (w->redisplay_end_trigger))
2122 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2123
2124 /* Correct bogus values of tab_width. */
2125 it->tab_width = XINT (current_buffer->tab_width);
2126 if (it->tab_width <= 0 || it->tab_width > 1000)
2127 it->tab_width = 8;
2128
2129 /* Are lines in the display truncated? */
2130 it->truncate_lines_p
2131 = (base_face_id != DEFAULT_FACE_ID
2132 || XINT (it->w->hscroll)
2133 || (truncate_partial_width_windows
2134 && !WINDOW_FULL_WIDTH_P (it->w))
2135 || !NILP (current_buffer->truncate_lines));
2136
2137 /* Get dimensions of truncation and continuation glyphs. These are
2138 displayed as fringe bitmaps under X, so we don't need them for such
2139 frames. */
2140 if (!FRAME_WINDOW_P (it->f))
2141 {
2142 if (it->truncate_lines_p)
2143 {
2144 /* We will need the truncation glyph. */
2145 xassert (it->glyph_row == NULL);
2146 produce_special_glyphs (it, IT_TRUNCATION);
2147 it->truncation_pixel_width = it->pixel_width;
2148 }
2149 else
2150 {
2151 /* We will need the continuation glyph. */
2152 xassert (it->glyph_row == NULL);
2153 produce_special_glyphs (it, IT_CONTINUATION);
2154 it->continuation_pixel_width = it->pixel_width;
2155 }
2156
2157 /* Reset these values to zero because the produce_special_glyphs
2158 above has changed them. */
2159 it->pixel_width = it->ascent = it->descent = 0;
2160 it->phys_ascent = it->phys_descent = 0;
2161 }
2162
2163 /* Set this after getting the dimensions of truncation and
2164 continuation glyphs, so that we don't produce glyphs when calling
2165 produce_special_glyphs, above. */
2166 it->glyph_row = row;
2167 it->area = TEXT_AREA;
2168
2169 /* Get the dimensions of the display area. The display area
2170 consists of the visible window area plus a horizontally scrolled
2171 part to the left of the window. All x-values are relative to the
2172 start of this total display area. */
2173 if (base_face_id != DEFAULT_FACE_ID)
2174 {
2175 /* Mode lines, menu bar in terminal frames. */
2176 it->first_visible_x = 0;
2177 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2178 }
2179 else
2180 {
2181 it->first_visible_x
2182 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2183 it->last_visible_x = (it->first_visible_x
2184 + window_box_width (w, TEXT_AREA));
2185
2186 /* If we truncate lines, leave room for the truncator glyph(s) at
2187 the right margin. Otherwise, leave room for the continuation
2188 glyph(s). Truncation and continuation glyphs are not inserted
2189 for window-based redisplay. */
2190 if (!FRAME_WINDOW_P (it->f))
2191 {
2192 if (it->truncate_lines_p)
2193 it->last_visible_x -= it->truncation_pixel_width;
2194 else
2195 it->last_visible_x -= it->continuation_pixel_width;
2196 }
2197
2198 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2199 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2200 }
2201
2202 /* Leave room for a border glyph. */
2203 if (!FRAME_WINDOW_P (it->f)
2204 && !WINDOW_RIGHTMOST_P (it->w))
2205 it->last_visible_x -= 1;
2206
2207 it->last_visible_y = window_text_bottom_y (w);
2208
2209 /* For mode lines and alike, arrange for the first glyph having a
2210 left box line if the face specifies a box. */
2211 if (base_face_id != DEFAULT_FACE_ID)
2212 {
2213 struct face *face;
2214
2215 it->face_id = base_face_id;
2216
2217 /* If we have a boxed mode line, make the first character appear
2218 with a left box line. */
2219 face = FACE_FROM_ID (it->f, base_face_id);
2220 if (face->box != FACE_NO_BOX)
2221 it->start_of_box_run_p = 1;
2222 }
2223
2224 /* If a buffer position was specified, set the iterator there,
2225 getting overlays and face properties from that position. */
2226 if (charpos >= BUF_BEG (current_buffer))
2227 {
2228 it->end_charpos = ZV;
2229 it->face_id = -1;
2230 IT_CHARPOS (*it) = charpos;
2231
2232 /* Compute byte position if not specified. */
2233 if (bytepos < charpos)
2234 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2235 else
2236 IT_BYTEPOS (*it) = bytepos;
2237
2238 it->start = it->current;
2239
2240 /* Compute faces etc. */
2241 reseat (it, it->current.pos, 1);
2242 }
2243
2244 CHECK_IT (it);
2245 }
2246
2247
2248 /* Initialize IT for the display of window W with window start POS. */
2249
2250 void
2251 start_display (it, w, pos)
2252 struct it *it;
2253 struct window *w;
2254 struct text_pos pos;
2255 {
2256 struct glyph_row *row;
2257 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2258
2259 row = w->desired_matrix->rows + first_vpos;
2260 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2261 it->first_vpos = first_vpos;
2262
2263 if (!it->truncate_lines_p)
2264 {
2265 int start_at_line_beg_p;
2266 int first_y = it->current_y;
2267
2268 /* If window start is not at a line start, skip forward to POS to
2269 get the correct continuation lines width. */
2270 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2271 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2272 if (!start_at_line_beg_p)
2273 {
2274 int new_x;
2275
2276 reseat_at_previous_visible_line_start (it);
2277 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2278
2279 new_x = it->current_x + it->pixel_width;
2280
2281 /* If lines are continued, this line may end in the middle
2282 of a multi-glyph character (e.g. a control character
2283 displayed as \003, or in the middle of an overlay
2284 string). In this case move_it_to above will not have
2285 taken us to the start of the continuation line but to the
2286 end of the continued line. */
2287 if (it->current_x > 0
2288 && !it->truncate_lines_p /* Lines are continued. */
2289 && (/* And glyph doesn't fit on the line. */
2290 new_x > it->last_visible_x
2291 /* Or it fits exactly and we're on a window
2292 system frame. */
2293 || (new_x == it->last_visible_x
2294 && FRAME_WINDOW_P (it->f))))
2295 {
2296 if (it->current.dpvec_index >= 0
2297 || it->current.overlay_string_index >= 0)
2298 {
2299 set_iterator_to_next (it, 1);
2300 move_it_in_display_line_to (it, -1, -1, 0);
2301 }
2302
2303 it->continuation_lines_width += it->current_x;
2304 }
2305
2306 /* We're starting a new display line, not affected by the
2307 height of the continued line, so clear the appropriate
2308 fields in the iterator structure. */
2309 it->max_ascent = it->max_descent = 0;
2310 it->max_phys_ascent = it->max_phys_descent = 0;
2311
2312 it->current_y = first_y;
2313 it->vpos = 0;
2314 it->current_x = it->hpos = 0;
2315 }
2316 }
2317
2318 #if 0 /* Don't assert the following because start_display is sometimes
2319 called intentionally with a window start that is not at a
2320 line start. Please leave this code in as a comment. */
2321
2322 /* Window start should be on a line start, now. */
2323 xassert (it->continuation_lines_width
2324 || IT_CHARPOS (it) == BEGV
2325 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2326 #endif /* 0 */
2327 }
2328
2329
2330 /* Return 1 if POS is a position in ellipses displayed for invisible
2331 text. W is the window we display, for text property lookup. */
2332
2333 static int
2334 in_ellipses_for_invisible_text_p (pos, w)
2335 struct display_pos *pos;
2336 struct window *w;
2337 {
2338 Lisp_Object prop, window;
2339 int ellipses_p = 0;
2340 int charpos = CHARPOS (pos->pos);
2341
2342 /* If POS specifies a position in a display vector, this might
2343 be for an ellipsis displayed for invisible text. We won't
2344 get the iterator set up for delivering that ellipsis unless
2345 we make sure that it gets aware of the invisible text. */
2346 if (pos->dpvec_index >= 0
2347 && pos->overlay_string_index < 0
2348 && CHARPOS (pos->string_pos) < 0
2349 && charpos > BEGV
2350 && (XSETWINDOW (window, w),
2351 prop = Fget_char_property (make_number (charpos),
2352 Qinvisible, window),
2353 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2354 {
2355 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2356 window);
2357 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2358 }
2359
2360 return ellipses_p;
2361 }
2362
2363
2364 /* Initialize IT for stepping through current_buffer in window W,
2365 starting at position POS that includes overlay string and display
2366 vector/ control character translation position information. Value
2367 is zero if there are overlay strings with newlines at POS. */
2368
2369 static int
2370 init_from_display_pos (it, w, pos)
2371 struct it *it;
2372 struct window *w;
2373 struct display_pos *pos;
2374 {
2375 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2376 int i, overlay_strings_with_newlines = 0;
2377
2378 /* If POS specifies a position in a display vector, this might
2379 be for an ellipsis displayed for invisible text. We won't
2380 get the iterator set up for delivering that ellipsis unless
2381 we make sure that it gets aware of the invisible text. */
2382 if (in_ellipses_for_invisible_text_p (pos, w))
2383 {
2384 --charpos;
2385 bytepos = 0;
2386 }
2387
2388 /* Keep in mind: the call to reseat in init_iterator skips invisible
2389 text, so we might end up at a position different from POS. This
2390 is only a problem when POS is a row start after a newline and an
2391 overlay starts there with an after-string, and the overlay has an
2392 invisible property. Since we don't skip invisible text in
2393 display_line and elsewhere immediately after consuming the
2394 newline before the row start, such a POS will not be in a string,
2395 but the call to init_iterator below will move us to the
2396 after-string. */
2397 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2398
2399 for (i = 0; i < it->n_overlay_strings; ++i)
2400 {
2401 const char *s = SDATA (it->overlay_strings[i]);
2402 const char *e = s + SBYTES (it->overlay_strings[i]);
2403
2404 while (s < e && *s != '\n')
2405 ++s;
2406
2407 if (s < e)
2408 {
2409 overlay_strings_with_newlines = 1;
2410 break;
2411 }
2412 }
2413
2414 /* If position is within an overlay string, set up IT to the right
2415 overlay string. */
2416 if (pos->overlay_string_index >= 0)
2417 {
2418 int relative_index;
2419
2420 /* If the first overlay string happens to have a `display'
2421 property for an image, the iterator will be set up for that
2422 image, and we have to undo that setup first before we can
2423 correct the overlay string index. */
2424 if (it->method == next_element_from_image)
2425 pop_it (it);
2426
2427 /* We already have the first chunk of overlay strings in
2428 IT->overlay_strings. Load more until the one for
2429 pos->overlay_string_index is in IT->overlay_strings. */
2430 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2431 {
2432 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2433 it->current.overlay_string_index = 0;
2434 while (n--)
2435 {
2436 load_overlay_strings (it, 0);
2437 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2438 }
2439 }
2440
2441 it->current.overlay_string_index = pos->overlay_string_index;
2442 relative_index = (it->current.overlay_string_index
2443 % OVERLAY_STRING_CHUNK_SIZE);
2444 it->string = it->overlay_strings[relative_index];
2445 xassert (STRINGP (it->string));
2446 it->current.string_pos = pos->string_pos;
2447 it->method = next_element_from_string;
2448 }
2449
2450 #if 0 /* This is bogus because POS not having an overlay string
2451 position does not mean it's after the string. Example: A
2452 line starting with a before-string and initialization of IT
2453 to the previous row's end position. */
2454 else if (it->current.overlay_string_index >= 0)
2455 {
2456 /* If POS says we're already after an overlay string ending at
2457 POS, make sure to pop the iterator because it will be in
2458 front of that overlay string. When POS is ZV, we've thereby
2459 also ``processed'' overlay strings at ZV. */
2460 while (it->sp)
2461 pop_it (it);
2462 it->current.overlay_string_index = -1;
2463 it->method = next_element_from_buffer;
2464 if (CHARPOS (pos->pos) == ZV)
2465 it->overlay_strings_at_end_processed_p = 1;
2466 }
2467 #endif /* 0 */
2468
2469 if (CHARPOS (pos->string_pos) >= 0)
2470 {
2471 /* Recorded position is not in an overlay string, but in another
2472 string. This can only be a string from a `display' property.
2473 IT should already be filled with that string. */
2474 it->current.string_pos = pos->string_pos;
2475 xassert (STRINGP (it->string));
2476 }
2477
2478 /* Restore position in display vector translations, control
2479 character translations or ellipses. */
2480 if (pos->dpvec_index >= 0)
2481 {
2482 if (it->dpvec == NULL)
2483 get_next_display_element (it);
2484 xassert (it->dpvec && it->current.dpvec_index == 0);
2485 it->current.dpvec_index = pos->dpvec_index;
2486 }
2487
2488 CHECK_IT (it);
2489 return !overlay_strings_with_newlines;
2490 }
2491
2492
2493 /* Initialize IT for stepping through current_buffer in window W
2494 starting at ROW->start. */
2495
2496 static void
2497 init_to_row_start (it, w, row)
2498 struct it *it;
2499 struct window *w;
2500 struct glyph_row *row;
2501 {
2502 init_from_display_pos (it, w, &row->start);
2503 it->start = row->start;
2504 it->continuation_lines_width = row->continuation_lines_width;
2505 CHECK_IT (it);
2506 }
2507
2508
2509 /* Initialize IT for stepping through current_buffer in window W
2510 starting in the line following ROW, i.e. starting at ROW->end.
2511 Value is zero if there are overlay strings with newlines at ROW's
2512 end position. */
2513
2514 static int
2515 init_to_row_end (it, w, row)
2516 struct it *it;
2517 struct window *w;
2518 struct glyph_row *row;
2519 {
2520 int success = 0;
2521
2522 if (init_from_display_pos (it, w, &row->end))
2523 {
2524 if (row->continued_p)
2525 it->continuation_lines_width
2526 = row->continuation_lines_width + row->pixel_width;
2527 CHECK_IT (it);
2528 success = 1;
2529 }
2530
2531 return success;
2532 }
2533
2534
2535
2536 \f
2537 /***********************************************************************
2538 Text properties
2539 ***********************************************************************/
2540
2541 /* Called when IT reaches IT->stop_charpos. Handle text property and
2542 overlay changes. Set IT->stop_charpos to the next position where
2543 to stop. */
2544
2545 static void
2546 handle_stop (it)
2547 struct it *it;
2548 {
2549 enum prop_handled handled;
2550 int handle_overlay_change_p = 1;
2551 struct props *p;
2552
2553 it->dpvec = NULL;
2554 it->current.dpvec_index = -1;
2555
2556 do
2557 {
2558 handled = HANDLED_NORMALLY;
2559
2560 /* Call text property handlers. */
2561 for (p = it_props; p->handler; ++p)
2562 {
2563 handled = p->handler (it);
2564
2565 if (handled == HANDLED_RECOMPUTE_PROPS)
2566 break;
2567 else if (handled == HANDLED_RETURN)
2568 return;
2569 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2570 handle_overlay_change_p = 0;
2571 }
2572
2573 if (handled != HANDLED_RECOMPUTE_PROPS)
2574 {
2575 /* Don't check for overlay strings below when set to deliver
2576 characters from a display vector. */
2577 if (it->method == next_element_from_display_vector)
2578 handle_overlay_change_p = 0;
2579
2580 /* Handle overlay changes. */
2581 if (handle_overlay_change_p)
2582 handled = handle_overlay_change (it);
2583
2584 /* Determine where to stop next. */
2585 if (handled == HANDLED_NORMALLY)
2586 compute_stop_pos (it);
2587 }
2588 }
2589 while (handled == HANDLED_RECOMPUTE_PROPS);
2590 }
2591
2592
2593 /* Compute IT->stop_charpos from text property and overlay change
2594 information for IT's current position. */
2595
2596 static void
2597 compute_stop_pos (it)
2598 struct it *it;
2599 {
2600 register INTERVAL iv, next_iv;
2601 Lisp_Object object, limit, position;
2602
2603 /* If nowhere else, stop at the end. */
2604 it->stop_charpos = it->end_charpos;
2605
2606 if (STRINGP (it->string))
2607 {
2608 /* Strings are usually short, so don't limit the search for
2609 properties. */
2610 object = it->string;
2611 limit = Qnil;
2612 position = make_number (IT_STRING_CHARPOS (*it));
2613 }
2614 else
2615 {
2616 int charpos;
2617
2618 /* If next overlay change is in front of the current stop pos
2619 (which is IT->end_charpos), stop there. Note: value of
2620 next_overlay_change is point-max if no overlay change
2621 follows. */
2622 charpos = next_overlay_change (IT_CHARPOS (*it));
2623 if (charpos < it->stop_charpos)
2624 it->stop_charpos = charpos;
2625
2626 /* If showing the region, we have to stop at the region
2627 start or end because the face might change there. */
2628 if (it->region_beg_charpos > 0)
2629 {
2630 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2631 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2632 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2633 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2634 }
2635
2636 /* Set up variables for computing the stop position from text
2637 property changes. */
2638 XSETBUFFER (object, current_buffer);
2639 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2640 position = make_number (IT_CHARPOS (*it));
2641
2642 }
2643
2644 /* Get the interval containing IT's position. Value is a null
2645 interval if there isn't such an interval. */
2646 iv = validate_interval_range (object, &position, &position, 0);
2647 if (!NULL_INTERVAL_P (iv))
2648 {
2649 Lisp_Object values_here[LAST_PROP_IDX];
2650 struct props *p;
2651
2652 /* Get properties here. */
2653 for (p = it_props; p->handler; ++p)
2654 values_here[p->idx] = textget (iv->plist, *p->name);
2655
2656 /* Look for an interval following iv that has different
2657 properties. */
2658 for (next_iv = next_interval (iv);
2659 (!NULL_INTERVAL_P (next_iv)
2660 && (NILP (limit)
2661 || XFASTINT (limit) > next_iv->position));
2662 next_iv = next_interval (next_iv))
2663 {
2664 for (p = it_props; p->handler; ++p)
2665 {
2666 Lisp_Object new_value;
2667
2668 new_value = textget (next_iv->plist, *p->name);
2669 if (!EQ (values_here[p->idx], new_value))
2670 break;
2671 }
2672
2673 if (p->handler)
2674 break;
2675 }
2676
2677 if (!NULL_INTERVAL_P (next_iv))
2678 {
2679 if (INTEGERP (limit)
2680 && next_iv->position >= XFASTINT (limit))
2681 /* No text property change up to limit. */
2682 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2683 else
2684 /* Text properties change in next_iv. */
2685 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2686 }
2687 }
2688
2689 xassert (STRINGP (it->string)
2690 || (it->stop_charpos >= BEGV
2691 && it->stop_charpos >= IT_CHARPOS (*it)));
2692 }
2693
2694
2695 /* Return the position of the next overlay change after POS in
2696 current_buffer. Value is point-max if no overlay change
2697 follows. This is like `next-overlay-change' but doesn't use
2698 xmalloc. */
2699
2700 static int
2701 next_overlay_change (pos)
2702 int pos;
2703 {
2704 int noverlays;
2705 int endpos;
2706 Lisp_Object *overlays;
2707 int len;
2708 int i;
2709
2710 /* Get all overlays at the given position. */
2711 len = 10;
2712 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2713 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2714 if (noverlays > len)
2715 {
2716 len = noverlays;
2717 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2718 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2719 }
2720
2721 /* If any of these overlays ends before endpos,
2722 use its ending point instead. */
2723 for (i = 0; i < noverlays; ++i)
2724 {
2725 Lisp_Object oend;
2726 int oendpos;
2727
2728 oend = OVERLAY_END (overlays[i]);
2729 oendpos = OVERLAY_POSITION (oend);
2730 endpos = min (endpos, oendpos);
2731 }
2732
2733 return endpos;
2734 }
2735
2736
2737 \f
2738 /***********************************************************************
2739 Fontification
2740 ***********************************************************************/
2741
2742 /* Handle changes in the `fontified' property of the current buffer by
2743 calling hook functions from Qfontification_functions to fontify
2744 regions of text. */
2745
2746 static enum prop_handled
2747 handle_fontified_prop (it)
2748 struct it *it;
2749 {
2750 Lisp_Object prop, pos;
2751 enum prop_handled handled = HANDLED_NORMALLY;
2752
2753 /* Get the value of the `fontified' property at IT's current buffer
2754 position. (The `fontified' property doesn't have a special
2755 meaning in strings.) If the value is nil, call functions from
2756 Qfontification_functions. */
2757 if (!STRINGP (it->string)
2758 && it->s == NULL
2759 && !NILP (Vfontification_functions)
2760 && !NILP (Vrun_hooks)
2761 && (pos = make_number (IT_CHARPOS (*it)),
2762 prop = Fget_char_property (pos, Qfontified, Qnil),
2763 NILP (prop)))
2764 {
2765 int count = SPECPDL_INDEX ();
2766 Lisp_Object val;
2767
2768 val = Vfontification_functions;
2769 specbind (Qfontification_functions, Qnil);
2770
2771 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2772 safe_call1 (val, pos);
2773 else
2774 {
2775 Lisp_Object globals, fn;
2776 struct gcpro gcpro1, gcpro2;
2777
2778 globals = Qnil;
2779 GCPRO2 (val, globals);
2780
2781 for (; CONSP (val); val = XCDR (val))
2782 {
2783 fn = XCAR (val);
2784
2785 if (EQ (fn, Qt))
2786 {
2787 /* A value of t indicates this hook has a local
2788 binding; it means to run the global binding too.
2789 In a global value, t should not occur. If it
2790 does, we must ignore it to avoid an endless
2791 loop. */
2792 for (globals = Fdefault_value (Qfontification_functions);
2793 CONSP (globals);
2794 globals = XCDR (globals))
2795 {
2796 fn = XCAR (globals);
2797 if (!EQ (fn, Qt))
2798 safe_call1 (fn, pos);
2799 }
2800 }
2801 else
2802 safe_call1 (fn, pos);
2803 }
2804
2805 UNGCPRO;
2806 }
2807
2808 unbind_to (count, Qnil);
2809
2810 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2811 something. This avoids an endless loop if they failed to
2812 fontify the text for which reason ever. */
2813 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2814 handled = HANDLED_RECOMPUTE_PROPS;
2815 }
2816
2817 return handled;
2818 }
2819
2820
2821 \f
2822 /***********************************************************************
2823 Faces
2824 ***********************************************************************/
2825
2826 /* Set up iterator IT from face properties at its current position.
2827 Called from handle_stop. */
2828
2829 static enum prop_handled
2830 handle_face_prop (it)
2831 struct it *it;
2832 {
2833 int new_face_id, next_stop;
2834
2835 if (!STRINGP (it->string))
2836 {
2837 new_face_id
2838 = face_at_buffer_position (it->w,
2839 IT_CHARPOS (*it),
2840 it->region_beg_charpos,
2841 it->region_end_charpos,
2842 &next_stop,
2843 (IT_CHARPOS (*it)
2844 + TEXT_PROP_DISTANCE_LIMIT),
2845 0);
2846
2847 /* Is this a start of a run of characters with box face?
2848 Caveat: this can be called for a freshly initialized
2849 iterator; face_id is -1 in this case. We know that the new
2850 face will not change until limit, i.e. if the new face has a
2851 box, all characters up to limit will have one. But, as
2852 usual, we don't know whether limit is really the end. */
2853 if (new_face_id != it->face_id)
2854 {
2855 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2856
2857 /* If new face has a box but old face has not, this is
2858 the start of a run of characters with box, i.e. it has
2859 a shadow on the left side. The value of face_id of the
2860 iterator will be -1 if this is the initial call that gets
2861 the face. In this case, we have to look in front of IT's
2862 position and see whether there is a face != new_face_id. */
2863 it->start_of_box_run_p
2864 = (new_face->box != FACE_NO_BOX
2865 && (it->face_id >= 0
2866 || IT_CHARPOS (*it) == BEG
2867 || new_face_id != face_before_it_pos (it)));
2868 it->face_box_p = new_face->box != FACE_NO_BOX;
2869 }
2870 }
2871 else
2872 {
2873 int base_face_id, bufpos;
2874
2875 if (it->current.overlay_string_index >= 0)
2876 bufpos = IT_CHARPOS (*it);
2877 else
2878 bufpos = 0;
2879
2880 /* For strings from a buffer, i.e. overlay strings or strings
2881 from a `display' property, use the face at IT's current
2882 buffer position as the base face to merge with, so that
2883 overlay strings appear in the same face as surrounding
2884 text, unless they specify their own faces. */
2885 base_face_id = underlying_face_id (it);
2886
2887 new_face_id = face_at_string_position (it->w,
2888 it->string,
2889 IT_STRING_CHARPOS (*it),
2890 bufpos,
2891 it->region_beg_charpos,
2892 it->region_end_charpos,
2893 &next_stop,
2894 base_face_id, 0);
2895
2896 #if 0 /* This shouldn't be neccessary. Let's check it. */
2897 /* If IT is used to display a mode line we would really like to
2898 use the mode line face instead of the frame's default face. */
2899 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2900 && new_face_id == DEFAULT_FACE_ID)
2901 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2902 #endif
2903
2904 /* Is this a start of a run of characters with box? Caveat:
2905 this can be called for a freshly allocated iterator; face_id
2906 is -1 is this case. We know that the new face will not
2907 change until the next check pos, i.e. if the new face has a
2908 box, all characters up to that position will have a
2909 box. But, as usual, we don't know whether that position
2910 is really the end. */
2911 if (new_face_id != it->face_id)
2912 {
2913 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2914 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2915
2916 /* If new face has a box but old face hasn't, this is the
2917 start of a run of characters with box, i.e. it has a
2918 shadow on the left side. */
2919 it->start_of_box_run_p
2920 = new_face->box && (old_face == NULL || !old_face->box);
2921 it->face_box_p = new_face->box != FACE_NO_BOX;
2922 }
2923 }
2924
2925 it->face_id = new_face_id;
2926 return HANDLED_NORMALLY;
2927 }
2928
2929
2930 /* Return the ID of the face ``underlying'' IT's current position,
2931 which is in a string. If the iterator is associated with a
2932 buffer, return the face at IT's current buffer position.
2933 Otherwise, use the iterator's base_face_id. */
2934
2935 static int
2936 underlying_face_id (it)
2937 struct it *it;
2938 {
2939 int face_id = it->base_face_id, i;
2940
2941 xassert (STRINGP (it->string));
2942
2943 for (i = it->sp - 1; i >= 0; --i)
2944 if (NILP (it->stack[i].string))
2945 face_id = it->stack[i].face_id;
2946
2947 return face_id;
2948 }
2949
2950
2951 /* Compute the face one character before or after the current position
2952 of IT. BEFORE_P non-zero means get the face in front of IT's
2953 position. Value is the id of the face. */
2954
2955 static int
2956 face_before_or_after_it_pos (it, before_p)
2957 struct it *it;
2958 int before_p;
2959 {
2960 int face_id, limit;
2961 int next_check_charpos;
2962 struct text_pos pos;
2963
2964 xassert (it->s == NULL);
2965
2966 if (STRINGP (it->string))
2967 {
2968 int bufpos, base_face_id;
2969
2970 /* No face change past the end of the string (for the case
2971 we are padding with spaces). No face change before the
2972 string start. */
2973 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2974 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2975 return it->face_id;
2976
2977 /* Set pos to the position before or after IT's current position. */
2978 if (before_p)
2979 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2980 else
2981 /* For composition, we must check the character after the
2982 composition. */
2983 pos = (it->what == IT_COMPOSITION
2984 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2985 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2986
2987 if (it->current.overlay_string_index >= 0)
2988 bufpos = IT_CHARPOS (*it);
2989 else
2990 bufpos = 0;
2991
2992 base_face_id = underlying_face_id (it);
2993
2994 /* Get the face for ASCII, or unibyte. */
2995 face_id = face_at_string_position (it->w,
2996 it->string,
2997 CHARPOS (pos),
2998 bufpos,
2999 it->region_beg_charpos,
3000 it->region_end_charpos,
3001 &next_check_charpos,
3002 base_face_id, 0);
3003
3004 /* Correct the face for charsets different from ASCII. Do it
3005 for the multibyte case only. The face returned above is
3006 suitable for unibyte text if IT->string is unibyte. */
3007 if (STRING_MULTIBYTE (it->string))
3008 {
3009 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3010 int rest = SBYTES (it->string) - BYTEPOS (pos);
3011 int c, len;
3012 struct face *face = FACE_FROM_ID (it->f, face_id);
3013
3014 c = string_char_and_length (p, rest, &len);
3015 face_id = FACE_FOR_CHAR (it->f, face, c);
3016 }
3017 }
3018 else
3019 {
3020 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3021 || (IT_CHARPOS (*it) <= BEGV && before_p))
3022 return it->face_id;
3023
3024 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3025 pos = it->current.pos;
3026
3027 if (before_p)
3028 DEC_TEXT_POS (pos, it->multibyte_p);
3029 else
3030 {
3031 if (it->what == IT_COMPOSITION)
3032 /* For composition, we must check the position after the
3033 composition. */
3034 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3035 else
3036 INC_TEXT_POS (pos, it->multibyte_p);
3037 }
3038
3039 /* Determine face for CHARSET_ASCII, or unibyte. */
3040 face_id = face_at_buffer_position (it->w,
3041 CHARPOS (pos),
3042 it->region_beg_charpos,
3043 it->region_end_charpos,
3044 &next_check_charpos,
3045 limit, 0);
3046
3047 /* Correct the face for charsets different from ASCII. Do it
3048 for the multibyte case only. The face returned above is
3049 suitable for unibyte text if current_buffer is unibyte. */
3050 if (it->multibyte_p)
3051 {
3052 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3053 struct face *face = FACE_FROM_ID (it->f, face_id);
3054 face_id = FACE_FOR_CHAR (it->f, face, c);
3055 }
3056 }
3057
3058 return face_id;
3059 }
3060
3061
3062 \f
3063 /***********************************************************************
3064 Invisible text
3065 ***********************************************************************/
3066
3067 /* Set up iterator IT from invisible properties at its current
3068 position. Called from handle_stop. */
3069
3070 static enum prop_handled
3071 handle_invisible_prop (it)
3072 struct it *it;
3073 {
3074 enum prop_handled handled = HANDLED_NORMALLY;
3075
3076 if (STRINGP (it->string))
3077 {
3078 extern Lisp_Object Qinvisible;
3079 Lisp_Object prop, end_charpos, limit, charpos;
3080
3081 /* Get the value of the invisible text property at the
3082 current position. Value will be nil if there is no such
3083 property. */
3084 charpos = make_number (IT_STRING_CHARPOS (*it));
3085 prop = Fget_text_property (charpos, Qinvisible, it->string);
3086
3087 if (!NILP (prop)
3088 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3089 {
3090 handled = HANDLED_RECOMPUTE_PROPS;
3091
3092 /* Get the position at which the next change of the
3093 invisible text property can be found in IT->string.
3094 Value will be nil if the property value is the same for
3095 all the rest of IT->string. */
3096 XSETINT (limit, SCHARS (it->string));
3097 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3098 it->string, limit);
3099
3100 /* Text at current position is invisible. The next
3101 change in the property is at position end_charpos.
3102 Move IT's current position to that position. */
3103 if (INTEGERP (end_charpos)
3104 && XFASTINT (end_charpos) < XFASTINT (limit))
3105 {
3106 struct text_pos old;
3107 old = it->current.string_pos;
3108 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3109 compute_string_pos (&it->current.string_pos, old, it->string);
3110 }
3111 else
3112 {
3113 /* The rest of the string is invisible. If this is an
3114 overlay string, proceed with the next overlay string
3115 or whatever comes and return a character from there. */
3116 if (it->current.overlay_string_index >= 0)
3117 {
3118 next_overlay_string (it);
3119 /* Don't check for overlay strings when we just
3120 finished processing them. */
3121 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3122 }
3123 else
3124 {
3125 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3126 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3127 }
3128 }
3129 }
3130 }
3131 else
3132 {
3133 int invis_p, newpos, next_stop, start_charpos;
3134 Lisp_Object pos, prop, overlay;
3135
3136 /* First of all, is there invisible text at this position? */
3137 start_charpos = IT_CHARPOS (*it);
3138 pos = make_number (IT_CHARPOS (*it));
3139 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3140 &overlay);
3141 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3142
3143 /* If we are on invisible text, skip over it. */
3144 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3145 {
3146 /* Record whether we have to display an ellipsis for the
3147 invisible text. */
3148 int display_ellipsis_p = invis_p == 2;
3149
3150 handled = HANDLED_RECOMPUTE_PROPS;
3151
3152 /* Loop skipping over invisible text. The loop is left at
3153 ZV or with IT on the first char being visible again. */
3154 do
3155 {
3156 /* Try to skip some invisible text. Return value is the
3157 position reached which can be equal to IT's position
3158 if there is nothing invisible here. This skips both
3159 over invisible text properties and overlays with
3160 invisible property. */
3161 newpos = skip_invisible (IT_CHARPOS (*it),
3162 &next_stop, ZV, it->window);
3163
3164 /* If we skipped nothing at all we weren't at invisible
3165 text in the first place. If everything to the end of
3166 the buffer was skipped, end the loop. */
3167 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3168 invis_p = 0;
3169 else
3170 {
3171 /* We skipped some characters but not necessarily
3172 all there are. Check if we ended up on visible
3173 text. Fget_char_property returns the property of
3174 the char before the given position, i.e. if we
3175 get invis_p = 0, this means that the char at
3176 newpos is visible. */
3177 pos = make_number (newpos);
3178 prop = Fget_char_property (pos, Qinvisible, it->window);
3179 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3180 }
3181
3182 /* If we ended up on invisible text, proceed to
3183 skip starting with next_stop. */
3184 if (invis_p)
3185 IT_CHARPOS (*it) = next_stop;
3186 }
3187 while (invis_p);
3188
3189 /* The position newpos is now either ZV or on visible text. */
3190 IT_CHARPOS (*it) = newpos;
3191 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3192
3193 /* If there are before-strings at the start of invisible
3194 text, and the text is invisible because of a text
3195 property, arrange to show before-strings because 20.x did
3196 it that way. (If the text is invisible because of an
3197 overlay property instead of a text property, this is
3198 already handled in the overlay code.) */
3199 if (NILP (overlay)
3200 && get_overlay_strings (it, start_charpos))
3201 {
3202 handled = HANDLED_RECOMPUTE_PROPS;
3203 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3204 }
3205 else if (display_ellipsis_p)
3206 setup_for_ellipsis (it);
3207 }
3208 }
3209
3210 return handled;
3211 }
3212
3213
3214 /* Make iterator IT return `...' next. */
3215
3216 static void
3217 setup_for_ellipsis (it)
3218 struct it *it;
3219 {
3220 if (it->dp
3221 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3222 {
3223 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3224 it->dpvec = v->contents;
3225 it->dpend = v->contents + v->size;
3226 }
3227 else
3228 {
3229 /* Default `...'. */
3230 it->dpvec = default_invis_vector;
3231 it->dpend = default_invis_vector + 3;
3232 }
3233
3234 /* The ellipsis display does not replace the display of the
3235 character at the new position. Indicate this by setting
3236 IT->dpvec_char_len to zero. */
3237 it->dpvec_char_len = 0;
3238
3239 it->current.dpvec_index = 0;
3240 it->method = next_element_from_display_vector;
3241 }
3242
3243
3244 \f
3245 /***********************************************************************
3246 'display' property
3247 ***********************************************************************/
3248
3249 /* Set up iterator IT from `display' property at its current position.
3250 Called from handle_stop. */
3251
3252 static enum prop_handled
3253 handle_display_prop (it)
3254 struct it *it;
3255 {
3256 Lisp_Object prop, object;
3257 struct text_pos *position;
3258 int display_replaced_p = 0;
3259
3260 if (STRINGP (it->string))
3261 {
3262 object = it->string;
3263 position = &it->current.string_pos;
3264 }
3265 else
3266 {
3267 object = it->w->buffer;
3268 position = &it->current.pos;
3269 }
3270
3271 /* Reset those iterator values set from display property values. */
3272 it->font_height = Qnil;
3273 it->space_width = Qnil;
3274 it->voffset = 0;
3275
3276 /* We don't support recursive `display' properties, i.e. string
3277 values that have a string `display' property, that have a string
3278 `display' property etc. */
3279 if (!it->string_from_display_prop_p)
3280 it->area = TEXT_AREA;
3281
3282 prop = Fget_char_property (make_number (position->charpos),
3283 Qdisplay, object);
3284 if (NILP (prop))
3285 return HANDLED_NORMALLY;
3286
3287 if (CONSP (prop)
3288 /* Simple properties. */
3289 && !EQ (XCAR (prop), Qimage)
3290 && !EQ (XCAR (prop), Qspace)
3291 && !EQ (XCAR (prop), Qwhen)
3292 && !EQ (XCAR (prop), Qspace_width)
3293 && !EQ (XCAR (prop), Qheight)
3294 && !EQ (XCAR (prop), Qraise)
3295 /* Marginal area specifications. */
3296 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3297 && !EQ (XCAR (prop), Qleft_fringe)
3298 && !EQ (XCAR (prop), Qright_fringe)
3299 && !NILP (XCAR (prop)))
3300 {
3301 for (; CONSP (prop); prop = XCDR (prop))
3302 {
3303 if (handle_single_display_prop (it, XCAR (prop), object,
3304 position, display_replaced_p))
3305 display_replaced_p = 1;
3306 }
3307 }
3308 else if (VECTORP (prop))
3309 {
3310 int i;
3311 for (i = 0; i < ASIZE (prop); ++i)
3312 if (handle_single_display_prop (it, AREF (prop, i), object,
3313 position, display_replaced_p))
3314 display_replaced_p = 1;
3315 }
3316 else
3317 {
3318 if (handle_single_display_prop (it, prop, object, position, 0))
3319 display_replaced_p = 1;
3320 }
3321
3322 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3323 }
3324
3325
3326 /* Value is the position of the end of the `display' property starting
3327 at START_POS in OBJECT. */
3328
3329 static struct text_pos
3330 display_prop_end (it, object, start_pos)
3331 struct it *it;
3332 Lisp_Object object;
3333 struct text_pos start_pos;
3334 {
3335 Lisp_Object end;
3336 struct text_pos end_pos;
3337
3338 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3339 Qdisplay, object, Qnil);
3340 CHARPOS (end_pos) = XFASTINT (end);
3341 if (STRINGP (object))
3342 compute_string_pos (&end_pos, start_pos, it->string);
3343 else
3344 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3345
3346 return end_pos;
3347 }
3348
3349
3350 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3351 is the object in which the `display' property was found. *POSITION
3352 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3353 means that we previously saw a display sub-property which already
3354 replaced text display with something else, for example an image;
3355 ignore such properties after the first one has been processed.
3356
3357 If PROP is a `space' or `image' sub-property, set *POSITION to the
3358 end position of the `display' property.
3359
3360 Value is non-zero if something was found which replaces the display
3361 of buffer or string text. */
3362
3363 static int
3364 handle_single_display_prop (it, prop, object, position,
3365 display_replaced_before_p)
3366 struct it *it;
3367 Lisp_Object prop;
3368 Lisp_Object object;
3369 struct text_pos *position;
3370 int display_replaced_before_p;
3371 {
3372 Lisp_Object value;
3373 int replaces_text_display_p = 0;
3374 Lisp_Object form;
3375
3376 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3377 evaluated. If the result is nil, VALUE is ignored. */
3378 form = Qt;
3379 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3380 {
3381 prop = XCDR (prop);
3382 if (!CONSP (prop))
3383 return 0;
3384 form = XCAR (prop);
3385 prop = XCDR (prop);
3386 }
3387
3388 if (!NILP (form) && !EQ (form, Qt))
3389 {
3390 int count = SPECPDL_INDEX ();
3391 struct gcpro gcpro1;
3392
3393 /* Bind `object' to the object having the `display' property, a
3394 buffer or string. Bind `position' to the position in the
3395 object where the property was found, and `buffer-position'
3396 to the current position in the buffer. */
3397 specbind (Qobject, object);
3398 specbind (Qposition, make_number (CHARPOS (*position)));
3399 specbind (Qbuffer_position,
3400 make_number (STRINGP (object)
3401 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3402 GCPRO1 (form);
3403 form = safe_eval (form);
3404 UNGCPRO;
3405 unbind_to (count, Qnil);
3406 }
3407
3408 if (NILP (form))
3409 return 0;
3410
3411 if (CONSP (prop)
3412 && EQ (XCAR (prop), Qheight)
3413 && CONSP (XCDR (prop)))
3414 {
3415 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3416 return 0;
3417
3418 /* `(height HEIGHT)'. */
3419 it->font_height = XCAR (XCDR (prop));
3420 if (!NILP (it->font_height))
3421 {
3422 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3423 int new_height = -1;
3424
3425 if (CONSP (it->font_height)
3426 && (EQ (XCAR (it->font_height), Qplus)
3427 || EQ (XCAR (it->font_height), Qminus))
3428 && CONSP (XCDR (it->font_height))
3429 && INTEGERP (XCAR (XCDR (it->font_height))))
3430 {
3431 /* `(+ N)' or `(- N)' where N is an integer. */
3432 int steps = XINT (XCAR (XCDR (it->font_height)));
3433 if (EQ (XCAR (it->font_height), Qplus))
3434 steps = - steps;
3435 it->face_id = smaller_face (it->f, it->face_id, steps);
3436 }
3437 else if (FUNCTIONP (it->font_height))
3438 {
3439 /* Call function with current height as argument.
3440 Value is the new height. */
3441 Lisp_Object height;
3442 height = safe_call1 (it->font_height,
3443 face->lface[LFACE_HEIGHT_INDEX]);
3444 if (NUMBERP (height))
3445 new_height = XFLOATINT (height);
3446 }
3447 else if (NUMBERP (it->font_height))
3448 {
3449 /* Value is a multiple of the canonical char height. */
3450 struct face *face;
3451
3452 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3453 new_height = (XFLOATINT (it->font_height)
3454 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3455 }
3456 else
3457 {
3458 /* Evaluate IT->font_height with `height' bound to the
3459 current specified height to get the new height. */
3460 Lisp_Object value;
3461 int count = SPECPDL_INDEX ();
3462
3463 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3464 value = safe_eval (it->font_height);
3465 unbind_to (count, Qnil);
3466
3467 if (NUMBERP (value))
3468 new_height = XFLOATINT (value);
3469 }
3470
3471 if (new_height > 0)
3472 it->face_id = face_with_height (it->f, it->face_id, new_height);
3473 }
3474 }
3475 else if (CONSP (prop)
3476 && EQ (XCAR (prop), Qspace_width)
3477 && CONSP (XCDR (prop)))
3478 {
3479 /* `(space_width WIDTH)'. */
3480 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3481 return 0;
3482
3483 value = XCAR (XCDR (prop));
3484 if (NUMBERP (value) && XFLOATINT (value) > 0)
3485 it->space_width = value;
3486 }
3487 else if (CONSP (prop)
3488 && EQ (XCAR (prop), Qraise)
3489 && CONSP (XCDR (prop)))
3490 {
3491 /* `(raise FACTOR)'. */
3492 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3493 return 0;
3494
3495 #ifdef HAVE_WINDOW_SYSTEM
3496 value = XCAR (XCDR (prop));
3497 if (NUMBERP (value))
3498 {
3499 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3500 it->voffset = - (XFLOATINT (value)
3501 * (FONT_HEIGHT (face->font)));
3502 }
3503 #endif /* HAVE_WINDOW_SYSTEM */
3504 }
3505 else if (!it->string_from_display_prop_p)
3506 {
3507 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3508 VALUE) or `((margin nil) VALUE)' or VALUE. */
3509 Lisp_Object location, value;
3510 struct text_pos start_pos;
3511 int valid_p;
3512
3513 /* Characters having this form of property are not displayed, so
3514 we have to find the end of the property. */
3515 start_pos = *position;
3516 *position = display_prop_end (it, object, start_pos);
3517 value = Qnil;
3518
3519 /* Let's stop at the new position and assume that all
3520 text properties change there. */
3521 it->stop_charpos = position->charpos;
3522
3523 if (CONSP (prop)
3524 && (EQ (XCAR (prop), Qleft_fringe)
3525 || EQ (XCAR (prop), Qright_fringe))
3526 && CONSP (XCDR (prop)))
3527 {
3528 unsigned face_id = DEFAULT_FACE_ID;
3529
3530 /* Save current settings of IT so that we can restore them
3531 when we are finished with the glyph property value. */
3532
3533 /* `(left-fringe BITMAP FACE)'. */
3534 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3535 return 0;
3536
3537 #ifdef HAVE_WINDOW_SYSTEM
3538 value = XCAR (XCDR (prop));
3539 if (!NUMBERP (value)
3540 || !valid_fringe_bitmap_id_p (XINT (value)))
3541 return 0;
3542
3543 if (CONSP (XCDR (XCDR (prop))))
3544 {
3545 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3546
3547 face_id = lookup_named_face (it->f, face_name, 'A');
3548 if (face_id < 0)
3549 return 0;
3550 }
3551
3552 push_it (it);
3553
3554 it->area = TEXT_AREA;
3555 it->what = IT_IMAGE;
3556 it->image_id = -1; /* no image */
3557 it->position = start_pos;
3558 it->object = NILP (object) ? it->w->buffer : object;
3559 it->method = next_element_from_image;
3560 it->face_id = face_id;
3561
3562 /* Say that we haven't consumed the characters with
3563 `display' property yet. The call to pop_it in
3564 set_iterator_to_next will clean this up. */
3565 *position = start_pos;
3566
3567 if (EQ (XCAR (prop), Qleft_fringe))
3568 {
3569 it->left_user_fringe_bitmap = XINT (value);
3570 it->left_user_fringe_face_id = face_id;
3571 }
3572 else
3573 {
3574 it->right_user_fringe_bitmap = XINT (value);
3575 it->right_user_fringe_face_id = face_id;
3576 }
3577 #endif /* HAVE_WINDOW_SYSTEM */
3578 return 1;
3579 }
3580
3581 location = Qunbound;
3582 if (CONSP (prop) && CONSP (XCAR (prop)))
3583 {
3584 Lisp_Object tem;
3585
3586 value = XCDR (prop);
3587 if (CONSP (value))
3588 value = XCAR (value);
3589
3590 tem = XCAR (prop);
3591 if (EQ (XCAR (tem), Qmargin)
3592 && (tem = XCDR (tem),
3593 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3594 (NILP (tem)
3595 || EQ (tem, Qleft_margin)
3596 || EQ (tem, Qright_margin))))
3597 location = tem;
3598 }
3599
3600 if (EQ (location, Qunbound))
3601 {
3602 location = Qnil;
3603 value = prop;
3604 }
3605
3606 #ifdef HAVE_WINDOW_SYSTEM
3607 if (FRAME_TERMCAP_P (it->f))
3608 valid_p = STRINGP (value);
3609 else
3610 valid_p = (STRINGP (value)
3611 || (CONSP (value) && EQ (XCAR (value), Qspace))
3612 || valid_image_p (value));
3613 #else /* not HAVE_WINDOW_SYSTEM */
3614 valid_p = STRINGP (value);
3615 #endif /* not HAVE_WINDOW_SYSTEM */
3616
3617 if ((EQ (location, Qleft_margin)
3618 || EQ (location, Qright_margin)
3619 || NILP (location))
3620 && valid_p
3621 && !display_replaced_before_p)
3622 {
3623 replaces_text_display_p = 1;
3624
3625 /* Save current settings of IT so that we can restore them
3626 when we are finished with the glyph property value. */
3627 push_it (it);
3628
3629 if (NILP (location))
3630 it->area = TEXT_AREA;
3631 else if (EQ (location, Qleft_margin))
3632 it->area = LEFT_MARGIN_AREA;
3633 else
3634 it->area = RIGHT_MARGIN_AREA;
3635
3636 if (STRINGP (value))
3637 {
3638 it->string = value;
3639 it->multibyte_p = STRING_MULTIBYTE (it->string);
3640 it->current.overlay_string_index = -1;
3641 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3642 it->end_charpos = it->string_nchars = SCHARS (it->string);
3643 it->method = next_element_from_string;
3644 it->stop_charpos = 0;
3645 it->string_from_display_prop_p = 1;
3646 /* Say that we haven't consumed the characters with
3647 `display' property yet. The call to pop_it in
3648 set_iterator_to_next will clean this up. */
3649 *position = start_pos;
3650 }
3651 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3652 {
3653 it->method = next_element_from_stretch;
3654 it->object = value;
3655 it->current.pos = it->position = start_pos;
3656 }
3657 #ifdef HAVE_WINDOW_SYSTEM
3658 else
3659 {
3660 it->what = IT_IMAGE;
3661 it->image_id = lookup_image (it->f, value);
3662 it->position = start_pos;
3663 it->object = NILP (object) ? it->w->buffer : object;
3664 it->method = next_element_from_image;
3665
3666 /* Say that we haven't consumed the characters with
3667 `display' property yet. The call to pop_it in
3668 set_iterator_to_next will clean this up. */
3669 *position = start_pos;
3670 }
3671 #endif /* HAVE_WINDOW_SYSTEM */
3672 }
3673 else
3674 /* Invalid property or property not supported. Restore
3675 the position to what it was before. */
3676 *position = start_pos;
3677 }
3678
3679 return replaces_text_display_p;
3680 }
3681
3682
3683 /* Check if PROP is a display sub-property value whose text should be
3684 treated as intangible. */
3685
3686 static int
3687 single_display_prop_intangible_p (prop)
3688 Lisp_Object prop;
3689 {
3690 /* Skip over `when FORM'. */
3691 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3692 {
3693 prop = XCDR (prop);
3694 if (!CONSP (prop))
3695 return 0;
3696 prop = XCDR (prop);
3697 }
3698
3699 if (STRINGP (prop))
3700 return 1;
3701
3702 if (!CONSP (prop))
3703 return 0;
3704
3705 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3706 we don't need to treat text as intangible. */
3707 if (EQ (XCAR (prop), Qmargin))
3708 {
3709 prop = XCDR (prop);
3710 if (!CONSP (prop))
3711 return 0;
3712
3713 prop = XCDR (prop);
3714 if (!CONSP (prop)
3715 || EQ (XCAR (prop), Qleft_margin)
3716 || EQ (XCAR (prop), Qright_margin))
3717 return 0;
3718 }
3719
3720 return (CONSP (prop)
3721 && (EQ (XCAR (prop), Qimage)
3722 || EQ (XCAR (prop), Qspace)));
3723 }
3724
3725
3726 /* Check if PROP is a display property value whose text should be
3727 treated as intangible. */
3728
3729 int
3730 display_prop_intangible_p (prop)
3731 Lisp_Object prop;
3732 {
3733 if (CONSP (prop)
3734 && CONSP (XCAR (prop))
3735 && !EQ (Qmargin, XCAR (XCAR (prop))))
3736 {
3737 /* A list of sub-properties. */
3738 while (CONSP (prop))
3739 {
3740 if (single_display_prop_intangible_p (XCAR (prop)))
3741 return 1;
3742 prop = XCDR (prop);
3743 }
3744 }
3745 else if (VECTORP (prop))
3746 {
3747 /* A vector of sub-properties. */
3748 int i;
3749 for (i = 0; i < ASIZE (prop); ++i)
3750 if (single_display_prop_intangible_p (AREF (prop, i)))
3751 return 1;
3752 }
3753 else
3754 return single_display_prop_intangible_p (prop);
3755
3756 return 0;
3757 }
3758
3759
3760 /* Return 1 if PROP is a display sub-property value containing STRING. */
3761
3762 static int
3763 single_display_prop_string_p (prop, string)
3764 Lisp_Object prop, string;
3765 {
3766 if (EQ (string, prop))
3767 return 1;
3768
3769 /* Skip over `when FORM'. */
3770 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3771 {
3772 prop = XCDR (prop);
3773 if (!CONSP (prop))
3774 return 0;
3775 prop = XCDR (prop);
3776 }
3777
3778 if (CONSP (prop))
3779 /* Skip over `margin LOCATION'. */
3780 if (EQ (XCAR (prop), Qmargin))
3781 {
3782 prop = XCDR (prop);
3783 if (!CONSP (prop))
3784 return 0;
3785
3786 prop = XCDR (prop);
3787 if (!CONSP (prop))
3788 return 0;
3789 }
3790
3791 return CONSP (prop) && EQ (XCAR (prop), string);
3792 }
3793
3794
3795 /* Return 1 if STRING appears in the `display' property PROP. */
3796
3797 static int
3798 display_prop_string_p (prop, string)
3799 Lisp_Object prop, string;
3800 {
3801 if (CONSP (prop)
3802 && CONSP (XCAR (prop))
3803 && !EQ (Qmargin, XCAR (XCAR (prop))))
3804 {
3805 /* A list of sub-properties. */
3806 while (CONSP (prop))
3807 {
3808 if (single_display_prop_string_p (XCAR (prop), string))
3809 return 1;
3810 prop = XCDR (prop);
3811 }
3812 }
3813 else if (VECTORP (prop))
3814 {
3815 /* A vector of sub-properties. */
3816 int i;
3817 for (i = 0; i < ASIZE (prop); ++i)
3818 if (single_display_prop_string_p (AREF (prop, i), string))
3819 return 1;
3820 }
3821 else
3822 return single_display_prop_string_p (prop, string);
3823
3824 return 0;
3825 }
3826
3827
3828 /* Determine from which buffer position in W's buffer STRING comes
3829 from. AROUND_CHARPOS is an approximate position where it could
3830 be from. Value is the buffer position or 0 if it couldn't be
3831 determined.
3832
3833 W's buffer must be current.
3834
3835 This function is necessary because we don't record buffer positions
3836 in glyphs generated from strings (to keep struct glyph small).
3837 This function may only use code that doesn't eval because it is
3838 called asynchronously from note_mouse_highlight. */
3839
3840 int
3841 string_buffer_position (w, string, around_charpos)
3842 struct window *w;
3843 Lisp_Object string;
3844 int around_charpos;
3845 {
3846 Lisp_Object limit, prop, pos;
3847 const int MAX_DISTANCE = 1000;
3848 int found = 0;
3849
3850 pos = make_number (around_charpos);
3851 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3852 while (!found && !EQ (pos, limit))
3853 {
3854 prop = Fget_char_property (pos, Qdisplay, Qnil);
3855 if (!NILP (prop) && display_prop_string_p (prop, string))
3856 found = 1;
3857 else
3858 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3859 }
3860
3861 if (!found)
3862 {
3863 pos = make_number (around_charpos);
3864 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3865 while (!found && !EQ (pos, limit))
3866 {
3867 prop = Fget_char_property (pos, Qdisplay, Qnil);
3868 if (!NILP (prop) && display_prop_string_p (prop, string))
3869 found = 1;
3870 else
3871 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3872 limit);
3873 }
3874 }
3875
3876 return found ? XINT (pos) : 0;
3877 }
3878
3879
3880 \f
3881 /***********************************************************************
3882 `composition' property
3883 ***********************************************************************/
3884
3885 /* Set up iterator IT from `composition' property at its current
3886 position. Called from handle_stop. */
3887
3888 static enum prop_handled
3889 handle_composition_prop (it)
3890 struct it *it;
3891 {
3892 Lisp_Object prop, string;
3893 int pos, pos_byte, end;
3894 enum prop_handled handled = HANDLED_NORMALLY;
3895
3896 if (STRINGP (it->string))
3897 {
3898 pos = IT_STRING_CHARPOS (*it);
3899 pos_byte = IT_STRING_BYTEPOS (*it);
3900 string = it->string;
3901 }
3902 else
3903 {
3904 pos = IT_CHARPOS (*it);
3905 pos_byte = IT_BYTEPOS (*it);
3906 string = Qnil;
3907 }
3908
3909 /* If there's a valid composition and point is not inside of the
3910 composition (in the case that the composition is from the current
3911 buffer), draw a glyph composed from the composition components. */
3912 if (find_composition (pos, -1, &pos, &end, &prop, string)
3913 && COMPOSITION_VALID_P (pos, end, prop)
3914 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3915 {
3916 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3917
3918 if (id >= 0)
3919 {
3920 it->method = next_element_from_composition;
3921 it->cmp_id = id;
3922 it->cmp_len = COMPOSITION_LENGTH (prop);
3923 /* For a terminal, draw only the first character of the
3924 components. */
3925 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3926 it->len = (STRINGP (it->string)
3927 ? string_char_to_byte (it->string, end)
3928 : CHAR_TO_BYTE (end)) - pos_byte;
3929 it->stop_charpos = end;
3930 handled = HANDLED_RETURN;
3931 }
3932 }
3933
3934 return handled;
3935 }
3936
3937
3938 \f
3939 /***********************************************************************
3940 Overlay strings
3941 ***********************************************************************/
3942
3943 /* The following structure is used to record overlay strings for
3944 later sorting in load_overlay_strings. */
3945
3946 struct overlay_entry
3947 {
3948 Lisp_Object overlay;
3949 Lisp_Object string;
3950 int priority;
3951 int after_string_p;
3952 };
3953
3954
3955 /* Set up iterator IT from overlay strings at its current position.
3956 Called from handle_stop. */
3957
3958 static enum prop_handled
3959 handle_overlay_change (it)
3960 struct it *it;
3961 {
3962 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3963 return HANDLED_RECOMPUTE_PROPS;
3964 else
3965 return HANDLED_NORMALLY;
3966 }
3967
3968
3969 /* Set up the next overlay string for delivery by IT, if there is an
3970 overlay string to deliver. Called by set_iterator_to_next when the
3971 end of the current overlay string is reached. If there are more
3972 overlay strings to display, IT->string and
3973 IT->current.overlay_string_index are set appropriately here.
3974 Otherwise IT->string is set to nil. */
3975
3976 static void
3977 next_overlay_string (it)
3978 struct it *it;
3979 {
3980 ++it->current.overlay_string_index;
3981 if (it->current.overlay_string_index == it->n_overlay_strings)
3982 {
3983 /* No more overlay strings. Restore IT's settings to what
3984 they were before overlay strings were processed, and
3985 continue to deliver from current_buffer. */
3986 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3987
3988 pop_it (it);
3989 xassert (it->stop_charpos >= BEGV
3990 && it->stop_charpos <= it->end_charpos);
3991 it->string = Qnil;
3992 it->current.overlay_string_index = -1;
3993 SET_TEXT_POS (it->current.string_pos, -1, -1);
3994 it->n_overlay_strings = 0;
3995 it->method = next_element_from_buffer;
3996
3997 /* If we're at the end of the buffer, record that we have
3998 processed the overlay strings there already, so that
3999 next_element_from_buffer doesn't try it again. */
4000 if (IT_CHARPOS (*it) >= it->end_charpos)
4001 it->overlay_strings_at_end_processed_p = 1;
4002
4003 /* If we have to display `...' for invisible text, set
4004 the iterator up for that. */
4005 if (display_ellipsis_p)
4006 setup_for_ellipsis (it);
4007 }
4008 else
4009 {
4010 /* There are more overlay strings to process. If
4011 IT->current.overlay_string_index has advanced to a position
4012 where we must load IT->overlay_strings with more strings, do
4013 it. */
4014 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4015
4016 if (it->current.overlay_string_index && i == 0)
4017 load_overlay_strings (it, 0);
4018
4019 /* Initialize IT to deliver display elements from the overlay
4020 string. */
4021 it->string = it->overlay_strings[i];
4022 it->multibyte_p = STRING_MULTIBYTE (it->string);
4023 SET_TEXT_POS (it->current.string_pos, 0, 0);
4024 it->method = next_element_from_string;
4025 it->stop_charpos = 0;
4026 }
4027
4028 CHECK_IT (it);
4029 }
4030
4031
4032 /* Compare two overlay_entry structures E1 and E2. Used as a
4033 comparison function for qsort in load_overlay_strings. Overlay
4034 strings for the same position are sorted so that
4035
4036 1. All after-strings come in front of before-strings, except
4037 when they come from the same overlay.
4038
4039 2. Within after-strings, strings are sorted so that overlay strings
4040 from overlays with higher priorities come first.
4041
4042 2. Within before-strings, strings are sorted so that overlay
4043 strings from overlays with higher priorities come last.
4044
4045 Value is analogous to strcmp. */
4046
4047
4048 static int
4049 compare_overlay_entries (e1, e2)
4050 void *e1, *e2;
4051 {
4052 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4053 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4054 int result;
4055
4056 if (entry1->after_string_p != entry2->after_string_p)
4057 {
4058 /* Let after-strings appear in front of before-strings if
4059 they come from different overlays. */
4060 if (EQ (entry1->overlay, entry2->overlay))
4061 result = entry1->after_string_p ? 1 : -1;
4062 else
4063 result = entry1->after_string_p ? -1 : 1;
4064 }
4065 else if (entry1->after_string_p)
4066 /* After-strings sorted in order of decreasing priority. */
4067 result = entry2->priority - entry1->priority;
4068 else
4069 /* Before-strings sorted in order of increasing priority. */
4070 result = entry1->priority - entry2->priority;
4071
4072 return result;
4073 }
4074
4075
4076 /* Load the vector IT->overlay_strings with overlay strings from IT's
4077 current buffer position, or from CHARPOS if that is > 0. Set
4078 IT->n_overlays to the total number of overlay strings found.
4079
4080 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4081 a time. On entry into load_overlay_strings,
4082 IT->current.overlay_string_index gives the number of overlay
4083 strings that have already been loaded by previous calls to this
4084 function.
4085
4086 IT->add_overlay_start contains an additional overlay start
4087 position to consider for taking overlay strings from, if non-zero.
4088 This position comes into play when the overlay has an `invisible'
4089 property, and both before and after-strings. When we've skipped to
4090 the end of the overlay, because of its `invisible' property, we
4091 nevertheless want its before-string to appear.
4092 IT->add_overlay_start will contain the overlay start position
4093 in this case.
4094
4095 Overlay strings are sorted so that after-string strings come in
4096 front of before-string strings. Within before and after-strings,
4097 strings are sorted by overlay priority. See also function
4098 compare_overlay_entries. */
4099
4100 static void
4101 load_overlay_strings (it, charpos)
4102 struct it *it;
4103 int charpos;
4104 {
4105 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4106 Lisp_Object overlay, window, str, invisible;
4107 struct Lisp_Overlay *ov;
4108 int start, end;
4109 int size = 20;
4110 int n = 0, i, j, invis_p;
4111 struct overlay_entry *entries
4112 = (struct overlay_entry *) alloca (size * sizeof *entries);
4113
4114 if (charpos <= 0)
4115 charpos = IT_CHARPOS (*it);
4116
4117 /* Append the overlay string STRING of overlay OVERLAY to vector
4118 `entries' which has size `size' and currently contains `n'
4119 elements. AFTER_P non-zero means STRING is an after-string of
4120 OVERLAY. */
4121 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4122 do \
4123 { \
4124 Lisp_Object priority; \
4125 \
4126 if (n == size) \
4127 { \
4128 int new_size = 2 * size; \
4129 struct overlay_entry *old = entries; \
4130 entries = \
4131 (struct overlay_entry *) alloca (new_size \
4132 * sizeof *entries); \
4133 bcopy (old, entries, size * sizeof *entries); \
4134 size = new_size; \
4135 } \
4136 \
4137 entries[n].string = (STRING); \
4138 entries[n].overlay = (OVERLAY); \
4139 priority = Foverlay_get ((OVERLAY), Qpriority); \
4140 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4141 entries[n].after_string_p = (AFTER_P); \
4142 ++n; \
4143 } \
4144 while (0)
4145
4146 /* Process overlay before the overlay center. */
4147 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4148 {
4149 XSETMISC (overlay, ov);
4150 xassert (OVERLAYP (overlay));
4151 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4152 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4153
4154 if (end < charpos)
4155 break;
4156
4157 /* Skip this overlay if it doesn't start or end at IT's current
4158 position. */
4159 if (end != charpos && start != charpos)
4160 continue;
4161
4162 /* Skip this overlay if it doesn't apply to IT->w. */
4163 window = Foverlay_get (overlay, Qwindow);
4164 if (WINDOWP (window) && XWINDOW (window) != it->w)
4165 continue;
4166
4167 /* If the text ``under'' the overlay is invisible, both before-
4168 and after-strings from this overlay are visible; start and
4169 end position are indistinguishable. */
4170 invisible = Foverlay_get (overlay, Qinvisible);
4171 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4172
4173 /* If overlay has a non-empty before-string, record it. */
4174 if ((start == charpos || (end == charpos && invis_p))
4175 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4176 && SCHARS (str))
4177 RECORD_OVERLAY_STRING (overlay, str, 0);
4178
4179 /* If overlay has a non-empty after-string, record it. */
4180 if ((end == charpos || (start == charpos && invis_p))
4181 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4182 && SCHARS (str))
4183 RECORD_OVERLAY_STRING (overlay, str, 1);
4184 }
4185
4186 /* Process overlays after the overlay center. */
4187 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4188 {
4189 XSETMISC (overlay, ov);
4190 xassert (OVERLAYP (overlay));
4191 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4192 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4193
4194 if (start > charpos)
4195 break;
4196
4197 /* Skip this overlay if it doesn't start or end at IT's current
4198 position. */
4199 if (end != charpos && start != charpos)
4200 continue;
4201
4202 /* Skip this overlay if it doesn't apply to IT->w. */
4203 window = Foverlay_get (overlay, Qwindow);
4204 if (WINDOWP (window) && XWINDOW (window) != it->w)
4205 continue;
4206
4207 /* If the text ``under'' the overlay is invisible, it has a zero
4208 dimension, and both before- and after-strings apply. */
4209 invisible = Foverlay_get (overlay, Qinvisible);
4210 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4211
4212 /* If overlay has a non-empty before-string, record it. */
4213 if ((start == charpos || (end == charpos && invis_p))
4214 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4215 && SCHARS (str))
4216 RECORD_OVERLAY_STRING (overlay, str, 0);
4217
4218 /* If overlay has a non-empty after-string, record it. */
4219 if ((end == charpos || (start == charpos && invis_p))
4220 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4221 && SCHARS (str))
4222 RECORD_OVERLAY_STRING (overlay, str, 1);
4223 }
4224
4225 #undef RECORD_OVERLAY_STRING
4226
4227 /* Sort entries. */
4228 if (n > 1)
4229 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4230
4231 /* Record the total number of strings to process. */
4232 it->n_overlay_strings = n;
4233
4234 /* IT->current.overlay_string_index is the number of overlay strings
4235 that have already been consumed by IT. Copy some of the
4236 remaining overlay strings to IT->overlay_strings. */
4237 i = 0;
4238 j = it->current.overlay_string_index;
4239 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4240 it->overlay_strings[i++] = entries[j++].string;
4241
4242 CHECK_IT (it);
4243 }
4244
4245
4246 /* Get the first chunk of overlay strings at IT's current buffer
4247 position, or at CHARPOS if that is > 0. Value is non-zero if at
4248 least one overlay string was found. */
4249
4250 static int
4251 get_overlay_strings (it, charpos)
4252 struct it *it;
4253 int charpos;
4254 {
4255 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4256 process. This fills IT->overlay_strings with strings, and sets
4257 IT->n_overlay_strings to the total number of strings to process.
4258 IT->pos.overlay_string_index has to be set temporarily to zero
4259 because load_overlay_strings needs this; it must be set to -1
4260 when no overlay strings are found because a zero value would
4261 indicate a position in the first overlay string. */
4262 it->current.overlay_string_index = 0;
4263 load_overlay_strings (it, charpos);
4264
4265 /* If we found overlay strings, set up IT to deliver display
4266 elements from the first one. Otherwise set up IT to deliver
4267 from current_buffer. */
4268 if (it->n_overlay_strings)
4269 {
4270 /* Make sure we know settings in current_buffer, so that we can
4271 restore meaningful values when we're done with the overlay
4272 strings. */
4273 compute_stop_pos (it);
4274 xassert (it->face_id >= 0);
4275
4276 /* Save IT's settings. They are restored after all overlay
4277 strings have been processed. */
4278 xassert (it->sp == 0);
4279 push_it (it);
4280
4281 /* Set up IT to deliver display elements from the first overlay
4282 string. */
4283 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4284 it->string = it->overlay_strings[0];
4285 it->stop_charpos = 0;
4286 xassert (STRINGP (it->string));
4287 it->end_charpos = SCHARS (it->string);
4288 it->multibyte_p = STRING_MULTIBYTE (it->string);
4289 it->method = next_element_from_string;
4290 }
4291 else
4292 {
4293 it->string = Qnil;
4294 it->current.overlay_string_index = -1;
4295 it->method = next_element_from_buffer;
4296 }
4297
4298 CHECK_IT (it);
4299
4300 /* Value is non-zero if we found at least one overlay string. */
4301 return STRINGP (it->string);
4302 }
4303
4304
4305 \f
4306 /***********************************************************************
4307 Saving and restoring state
4308 ***********************************************************************/
4309
4310 /* Save current settings of IT on IT->stack. Called, for example,
4311 before setting up IT for an overlay string, to be able to restore
4312 IT's settings to what they were after the overlay string has been
4313 processed. */
4314
4315 static void
4316 push_it (it)
4317 struct it *it;
4318 {
4319 struct iterator_stack_entry *p;
4320
4321 xassert (it->sp < 2);
4322 p = it->stack + it->sp;
4323
4324 p->stop_charpos = it->stop_charpos;
4325 xassert (it->face_id >= 0);
4326 p->face_id = it->face_id;
4327 p->string = it->string;
4328 p->pos = it->current;
4329 p->end_charpos = it->end_charpos;
4330 p->string_nchars = it->string_nchars;
4331 p->area = it->area;
4332 p->multibyte_p = it->multibyte_p;
4333 p->space_width = it->space_width;
4334 p->font_height = it->font_height;
4335 p->voffset = it->voffset;
4336 p->string_from_display_prop_p = it->string_from_display_prop_p;
4337 p->display_ellipsis_p = 0;
4338 ++it->sp;
4339 }
4340
4341
4342 /* Restore IT's settings from IT->stack. Called, for example, when no
4343 more overlay strings must be processed, and we return to delivering
4344 display elements from a buffer, or when the end of a string from a
4345 `display' property is reached and we return to delivering display
4346 elements from an overlay string, or from a buffer. */
4347
4348 static void
4349 pop_it (it)
4350 struct it *it;
4351 {
4352 struct iterator_stack_entry *p;
4353
4354 xassert (it->sp > 0);
4355 --it->sp;
4356 p = it->stack + it->sp;
4357 it->stop_charpos = p->stop_charpos;
4358 it->face_id = p->face_id;
4359 it->string = p->string;
4360 it->current = p->pos;
4361 it->end_charpos = p->end_charpos;
4362 it->string_nchars = p->string_nchars;
4363 it->area = p->area;
4364 it->multibyte_p = p->multibyte_p;
4365 it->space_width = p->space_width;
4366 it->font_height = p->font_height;
4367 it->voffset = p->voffset;
4368 it->string_from_display_prop_p = p->string_from_display_prop_p;
4369 }
4370
4371
4372 \f
4373 /***********************************************************************
4374 Moving over lines
4375 ***********************************************************************/
4376
4377 /* Set IT's current position to the previous line start. */
4378
4379 static void
4380 back_to_previous_line_start (it)
4381 struct it *it;
4382 {
4383 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4384 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4385 }
4386
4387
4388 /* Move IT to the next line start.
4389
4390 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4391 we skipped over part of the text (as opposed to moving the iterator
4392 continuously over the text). Otherwise, don't change the value
4393 of *SKIPPED_P.
4394
4395 Newlines may come from buffer text, overlay strings, or strings
4396 displayed via the `display' property. That's the reason we can't
4397 simply use find_next_newline_no_quit.
4398
4399 Note that this function may not skip over invisible text that is so
4400 because of text properties and immediately follows a newline. If
4401 it would, function reseat_at_next_visible_line_start, when called
4402 from set_iterator_to_next, would effectively make invisible
4403 characters following a newline part of the wrong glyph row, which
4404 leads to wrong cursor motion. */
4405
4406 static int
4407 forward_to_next_line_start (it, skipped_p)
4408 struct it *it;
4409 int *skipped_p;
4410 {
4411 int old_selective, newline_found_p, n;
4412 const int MAX_NEWLINE_DISTANCE = 500;
4413
4414 /* If already on a newline, just consume it to avoid unintended
4415 skipping over invisible text below. */
4416 if (it->what == IT_CHARACTER
4417 && it->c == '\n'
4418 && CHARPOS (it->position) == IT_CHARPOS (*it))
4419 {
4420 set_iterator_to_next (it, 0);
4421 it->c = 0;
4422 return 1;
4423 }
4424
4425 /* Don't handle selective display in the following. It's (a)
4426 unnecessary because it's done by the caller, and (b) leads to an
4427 infinite recursion because next_element_from_ellipsis indirectly
4428 calls this function. */
4429 old_selective = it->selective;
4430 it->selective = 0;
4431
4432 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4433 from buffer text. */
4434 for (n = newline_found_p = 0;
4435 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4436 n += STRINGP (it->string) ? 0 : 1)
4437 {
4438 if (!get_next_display_element (it))
4439 return 0;
4440 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4441 set_iterator_to_next (it, 0);
4442 }
4443
4444 /* If we didn't find a newline near enough, see if we can use a
4445 short-cut. */
4446 if (!newline_found_p)
4447 {
4448 int start = IT_CHARPOS (*it);
4449 int limit = find_next_newline_no_quit (start, 1);
4450 Lisp_Object pos;
4451
4452 xassert (!STRINGP (it->string));
4453
4454 /* If there isn't any `display' property in sight, and no
4455 overlays, we can just use the position of the newline in
4456 buffer text. */
4457 if (it->stop_charpos >= limit
4458 || ((pos = Fnext_single_property_change (make_number (start),
4459 Qdisplay,
4460 Qnil, make_number (limit)),
4461 NILP (pos))
4462 && next_overlay_change (start) == ZV))
4463 {
4464 IT_CHARPOS (*it) = limit;
4465 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4466 *skipped_p = newline_found_p = 1;
4467 }
4468 else
4469 {
4470 while (get_next_display_element (it)
4471 && !newline_found_p)
4472 {
4473 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4474 set_iterator_to_next (it, 0);
4475 }
4476 }
4477 }
4478
4479 it->selective = old_selective;
4480 return newline_found_p;
4481 }
4482
4483
4484 /* Set IT's current position to the previous visible line start. Skip
4485 invisible text that is so either due to text properties or due to
4486 selective display. Caution: this does not change IT->current_x and
4487 IT->hpos. */
4488
4489 static void
4490 back_to_previous_visible_line_start (it)
4491 struct it *it;
4492 {
4493 int visible_p = 0;
4494
4495 /* Go back one newline if not on BEGV already. */
4496 if (IT_CHARPOS (*it) > BEGV)
4497 back_to_previous_line_start (it);
4498
4499 /* Move over lines that are invisible because of selective display
4500 or text properties. */
4501 while (IT_CHARPOS (*it) > BEGV
4502 && !visible_p)
4503 {
4504 visible_p = 1;
4505
4506 /* If selective > 0, then lines indented more than that values
4507 are invisible. */
4508 if (it->selective > 0
4509 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4510 (double) it->selective)) /* iftc */
4511 visible_p = 0;
4512 else
4513 {
4514 Lisp_Object prop;
4515
4516 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4517 Qinvisible, it->window);
4518 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4519 visible_p = 0;
4520 }
4521
4522 /* Back one more newline if the current one is invisible. */
4523 if (!visible_p)
4524 back_to_previous_line_start (it);
4525 }
4526
4527 xassert (IT_CHARPOS (*it) >= BEGV);
4528 xassert (IT_CHARPOS (*it) == BEGV
4529 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4530 CHECK_IT (it);
4531 }
4532
4533
4534 /* Reseat iterator IT at the previous visible line start. Skip
4535 invisible text that is so either due to text properties or due to
4536 selective display. At the end, update IT's overlay information,
4537 face information etc. */
4538
4539 static void
4540 reseat_at_previous_visible_line_start (it)
4541 struct it *it;
4542 {
4543 back_to_previous_visible_line_start (it);
4544 reseat (it, it->current.pos, 1);
4545 CHECK_IT (it);
4546 }
4547
4548
4549 /* Reseat iterator IT on the next visible line start in the current
4550 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4551 preceding the line start. Skip over invisible text that is so
4552 because of selective display. Compute faces, overlays etc at the
4553 new position. Note that this function does not skip over text that
4554 is invisible because of text properties. */
4555
4556 static void
4557 reseat_at_next_visible_line_start (it, on_newline_p)
4558 struct it *it;
4559 int on_newline_p;
4560 {
4561 int newline_found_p, skipped_p = 0;
4562
4563 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4564
4565 /* Skip over lines that are invisible because they are indented
4566 more than the value of IT->selective. */
4567 if (it->selective > 0)
4568 while (IT_CHARPOS (*it) < ZV
4569 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4570 (double) it->selective)) /* iftc */
4571 {
4572 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4573 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4574 }
4575
4576 /* Position on the newline if that's what's requested. */
4577 if (on_newline_p && newline_found_p)
4578 {
4579 if (STRINGP (it->string))
4580 {
4581 if (IT_STRING_CHARPOS (*it) > 0)
4582 {
4583 --IT_STRING_CHARPOS (*it);
4584 --IT_STRING_BYTEPOS (*it);
4585 }
4586 }
4587 else if (IT_CHARPOS (*it) > BEGV)
4588 {
4589 --IT_CHARPOS (*it);
4590 --IT_BYTEPOS (*it);
4591 reseat (it, it->current.pos, 0);
4592 }
4593 }
4594 else if (skipped_p)
4595 reseat (it, it->current.pos, 0);
4596
4597 CHECK_IT (it);
4598 }
4599
4600
4601 \f
4602 /***********************************************************************
4603 Changing an iterator's position
4604 ***********************************************************************/
4605
4606 /* Change IT's current position to POS in current_buffer. If FORCE_P
4607 is non-zero, always check for text properties at the new position.
4608 Otherwise, text properties are only looked up if POS >=
4609 IT->check_charpos of a property. */
4610
4611 static void
4612 reseat (it, pos, force_p)
4613 struct it *it;
4614 struct text_pos pos;
4615 int force_p;
4616 {
4617 int original_pos = IT_CHARPOS (*it);
4618
4619 reseat_1 (it, pos, 0);
4620
4621 /* Determine where to check text properties. Avoid doing it
4622 where possible because text property lookup is very expensive. */
4623 if (force_p
4624 || CHARPOS (pos) > it->stop_charpos
4625 || CHARPOS (pos) < original_pos)
4626 handle_stop (it);
4627
4628 CHECK_IT (it);
4629 }
4630
4631
4632 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4633 IT->stop_pos to POS, also. */
4634
4635 static void
4636 reseat_1 (it, pos, set_stop_p)
4637 struct it *it;
4638 struct text_pos pos;
4639 int set_stop_p;
4640 {
4641 /* Don't call this function when scanning a C string. */
4642 xassert (it->s == NULL);
4643
4644 /* POS must be a reasonable value. */
4645 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4646
4647 it->current.pos = it->position = pos;
4648 XSETBUFFER (it->object, current_buffer);
4649 it->end_charpos = ZV;
4650 it->dpvec = NULL;
4651 it->current.dpvec_index = -1;
4652 it->current.overlay_string_index = -1;
4653 IT_STRING_CHARPOS (*it) = -1;
4654 IT_STRING_BYTEPOS (*it) = -1;
4655 it->string = Qnil;
4656 it->method = next_element_from_buffer;
4657 /* RMS: I added this to fix a bug in move_it_vertically_backward
4658 where it->area continued to relate to the starting point
4659 for the backward motion. Bug report from
4660 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4661 However, I am not sure whether reseat still does the right thing
4662 in general after this change. */
4663 it->area = TEXT_AREA;
4664 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4665 it->sp = 0;
4666 it->face_before_selective_p = 0;
4667
4668 if (set_stop_p)
4669 it->stop_charpos = CHARPOS (pos);
4670 }
4671
4672
4673 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4674 If S is non-null, it is a C string to iterate over. Otherwise,
4675 STRING gives a Lisp string to iterate over.
4676
4677 If PRECISION > 0, don't return more then PRECISION number of
4678 characters from the string.
4679
4680 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4681 characters have been returned. FIELD_WIDTH < 0 means an infinite
4682 field width.
4683
4684 MULTIBYTE = 0 means disable processing of multibyte characters,
4685 MULTIBYTE > 0 means enable it,
4686 MULTIBYTE < 0 means use IT->multibyte_p.
4687
4688 IT must be initialized via a prior call to init_iterator before
4689 calling this function. */
4690
4691 static void
4692 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4693 struct it *it;
4694 unsigned char *s;
4695 Lisp_Object string;
4696 int charpos;
4697 int precision, field_width, multibyte;
4698 {
4699 /* No region in strings. */
4700 it->region_beg_charpos = it->region_end_charpos = -1;
4701
4702 /* No text property checks performed by default, but see below. */
4703 it->stop_charpos = -1;
4704
4705 /* Set iterator position and end position. */
4706 bzero (&it->current, sizeof it->current);
4707 it->current.overlay_string_index = -1;
4708 it->current.dpvec_index = -1;
4709 xassert (charpos >= 0);
4710
4711 /* If STRING is specified, use its multibyteness, otherwise use the
4712 setting of MULTIBYTE, if specified. */
4713 if (multibyte >= 0)
4714 it->multibyte_p = multibyte > 0;
4715
4716 if (s == NULL)
4717 {
4718 xassert (STRINGP (string));
4719 it->string = string;
4720 it->s = NULL;
4721 it->end_charpos = it->string_nchars = SCHARS (string);
4722 it->method = next_element_from_string;
4723 it->current.string_pos = string_pos (charpos, string);
4724 }
4725 else
4726 {
4727 it->s = s;
4728 it->string = Qnil;
4729
4730 /* Note that we use IT->current.pos, not it->current.string_pos,
4731 for displaying C strings. */
4732 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4733 if (it->multibyte_p)
4734 {
4735 it->current.pos = c_string_pos (charpos, s, 1);
4736 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4737 }
4738 else
4739 {
4740 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4741 it->end_charpos = it->string_nchars = strlen (s);
4742 }
4743
4744 it->method = next_element_from_c_string;
4745 }
4746
4747 /* PRECISION > 0 means don't return more than PRECISION characters
4748 from the string. */
4749 if (precision > 0 && it->end_charpos - charpos > precision)
4750 it->end_charpos = it->string_nchars = charpos + precision;
4751
4752 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4753 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4754 FIELD_WIDTH < 0 means infinite field width. This is useful for
4755 padding with `-' at the end of a mode line. */
4756 if (field_width < 0)
4757 field_width = INFINITY;
4758 if (field_width > it->end_charpos - charpos)
4759 it->end_charpos = charpos + field_width;
4760
4761 /* Use the standard display table for displaying strings. */
4762 if (DISP_TABLE_P (Vstandard_display_table))
4763 it->dp = XCHAR_TABLE (Vstandard_display_table);
4764
4765 it->stop_charpos = charpos;
4766 CHECK_IT (it);
4767 }
4768
4769
4770 \f
4771 /***********************************************************************
4772 Iteration
4773 ***********************************************************************/
4774
4775 /* Load IT's display element fields with information about the next
4776 display element from the current position of IT. Value is zero if
4777 end of buffer (or C string) is reached. */
4778
4779 int
4780 get_next_display_element (it)
4781 struct it *it;
4782 {
4783 /* Non-zero means that we found a display element. Zero means that
4784 we hit the end of what we iterate over. Performance note: the
4785 function pointer `method' used here turns out to be faster than
4786 using a sequence of if-statements. */
4787 int success_p = (*it->method) (it);
4788
4789 if (it->what == IT_CHARACTER)
4790 {
4791 /* Map via display table or translate control characters.
4792 IT->c, IT->len etc. have been set to the next character by
4793 the function call above. If we have a display table, and it
4794 contains an entry for IT->c, translate it. Don't do this if
4795 IT->c itself comes from a display table, otherwise we could
4796 end up in an infinite recursion. (An alternative could be to
4797 count the recursion depth of this function and signal an
4798 error when a certain maximum depth is reached.) Is it worth
4799 it? */
4800 if (success_p && it->dpvec == NULL)
4801 {
4802 Lisp_Object dv;
4803
4804 if (it->dp
4805 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4806 VECTORP (dv)))
4807 {
4808 struct Lisp_Vector *v = XVECTOR (dv);
4809
4810 /* Return the first character from the display table
4811 entry, if not empty. If empty, don't display the
4812 current character. */
4813 if (v->size)
4814 {
4815 it->dpvec_char_len = it->len;
4816 it->dpvec = v->contents;
4817 it->dpend = v->contents + v->size;
4818 it->current.dpvec_index = 0;
4819 it->method = next_element_from_display_vector;
4820 success_p = get_next_display_element (it);
4821 }
4822 else
4823 {
4824 set_iterator_to_next (it, 0);
4825 success_p = get_next_display_element (it);
4826 }
4827 }
4828
4829 /* Translate control characters into `\003' or `^C' form.
4830 Control characters coming from a display table entry are
4831 currently not translated because we use IT->dpvec to hold
4832 the translation. This could easily be changed but I
4833 don't believe that it is worth doing.
4834
4835 If it->multibyte_p is nonzero, eight-bit characters and
4836 non-printable multibyte characters are also translated to
4837 octal form.
4838
4839 If it->multibyte_p is zero, eight-bit characters that
4840 don't have corresponding multibyte char code are also
4841 translated to octal form. */
4842 else if ((it->c < ' '
4843 && (it->area != TEXT_AREA
4844 || (it->c != '\n' && it->c != '\t')))
4845 || (it->multibyte_p
4846 ? ((it->c >= 127
4847 && it->len == 1)
4848 || !CHAR_PRINTABLE_P (it->c))
4849 : (it->c >= 127
4850 && it->c == unibyte_char_to_multibyte (it->c))))
4851 {
4852 /* IT->c is a control character which must be displayed
4853 either as '\003' or as `^C' where the '\\' and '^'
4854 can be defined in the display table. Fill
4855 IT->ctl_chars with glyphs for what we have to
4856 display. Then, set IT->dpvec to these glyphs. */
4857 GLYPH g;
4858
4859 if (it->c < 128 && it->ctl_arrow_p)
4860 {
4861 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4862 if (it->dp
4863 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4864 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4865 g = XINT (DISP_CTRL_GLYPH (it->dp));
4866 else
4867 g = FAST_MAKE_GLYPH ('^', 0);
4868 XSETINT (it->ctl_chars[0], g);
4869
4870 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4871 XSETINT (it->ctl_chars[1], g);
4872
4873 /* Set up IT->dpvec and return first character from it. */
4874 it->dpvec_char_len = it->len;
4875 it->dpvec = it->ctl_chars;
4876 it->dpend = it->dpvec + 2;
4877 it->current.dpvec_index = 0;
4878 it->method = next_element_from_display_vector;
4879 get_next_display_element (it);
4880 }
4881 else
4882 {
4883 unsigned char str[MAX_MULTIBYTE_LENGTH];
4884 int len;
4885 int i;
4886 GLYPH escape_glyph;
4887
4888 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4889 if (it->dp
4890 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4891 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4892 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4893 else
4894 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4895
4896 if (SINGLE_BYTE_CHAR_P (it->c))
4897 str[0] = it->c, len = 1;
4898 else
4899 {
4900 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4901 if (len < 0)
4902 {
4903 /* It's an invalid character, which
4904 shouldn't happen actually, but due to
4905 bugs it may happen. Let's print the char
4906 as is, there's not much meaningful we can
4907 do with it. */
4908 str[0] = it->c;
4909 str[1] = it->c >> 8;
4910 str[2] = it->c >> 16;
4911 str[3] = it->c >> 24;
4912 len = 4;
4913 }
4914 }
4915
4916 for (i = 0; i < len; i++)
4917 {
4918 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4919 /* Insert three more glyphs into IT->ctl_chars for
4920 the octal display of the character. */
4921 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4922 XSETINT (it->ctl_chars[i * 4 + 1], g);
4923 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4924 XSETINT (it->ctl_chars[i * 4 + 2], g);
4925 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4926 XSETINT (it->ctl_chars[i * 4 + 3], g);
4927 }
4928
4929 /* Set up IT->dpvec and return the first character
4930 from it. */
4931 it->dpvec_char_len = it->len;
4932 it->dpvec = it->ctl_chars;
4933 it->dpend = it->dpvec + len * 4;
4934 it->current.dpvec_index = 0;
4935 it->method = next_element_from_display_vector;
4936 get_next_display_element (it);
4937 }
4938 }
4939 }
4940
4941 /* Adjust face id for a multibyte character. There are no
4942 multibyte character in unibyte text. */
4943 if (it->multibyte_p
4944 && success_p
4945 && FRAME_WINDOW_P (it->f))
4946 {
4947 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4948 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4949 }
4950 }
4951
4952 /* Is this character the last one of a run of characters with
4953 box? If yes, set IT->end_of_box_run_p to 1. */
4954 if (it->face_box_p
4955 && it->s == NULL)
4956 {
4957 int face_id;
4958 struct face *face;
4959
4960 it->end_of_box_run_p
4961 = ((face_id = face_after_it_pos (it),
4962 face_id != it->face_id)
4963 && (face = FACE_FROM_ID (it->f, face_id),
4964 face->box == FACE_NO_BOX));
4965 }
4966
4967 /* Value is 0 if end of buffer or string reached. */
4968 return success_p;
4969 }
4970
4971
4972 /* Move IT to the next display element.
4973
4974 RESEAT_P non-zero means if called on a newline in buffer text,
4975 skip to the next visible line start.
4976
4977 Functions get_next_display_element and set_iterator_to_next are
4978 separate because I find this arrangement easier to handle than a
4979 get_next_display_element function that also increments IT's
4980 position. The way it is we can first look at an iterator's current
4981 display element, decide whether it fits on a line, and if it does,
4982 increment the iterator position. The other way around we probably
4983 would either need a flag indicating whether the iterator has to be
4984 incremented the next time, or we would have to implement a
4985 decrement position function which would not be easy to write. */
4986
4987 void
4988 set_iterator_to_next (it, reseat_p)
4989 struct it *it;
4990 int reseat_p;
4991 {
4992 /* Reset flags indicating start and end of a sequence of characters
4993 with box. Reset them at the start of this function because
4994 moving the iterator to a new position might set them. */
4995 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4996
4997 if (it->method == next_element_from_buffer)
4998 {
4999 /* The current display element of IT is a character from
5000 current_buffer. Advance in the buffer, and maybe skip over
5001 invisible lines that are so because of selective display. */
5002 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5003 reseat_at_next_visible_line_start (it, 0);
5004 else
5005 {
5006 xassert (it->len != 0);
5007 IT_BYTEPOS (*it) += it->len;
5008 IT_CHARPOS (*it) += 1;
5009 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5010 }
5011 }
5012 else if (it->method == next_element_from_composition)
5013 {
5014 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5015 if (STRINGP (it->string))
5016 {
5017 IT_STRING_BYTEPOS (*it) += it->len;
5018 IT_STRING_CHARPOS (*it) += it->cmp_len;
5019 it->method = next_element_from_string;
5020 goto consider_string_end;
5021 }
5022 else
5023 {
5024 IT_BYTEPOS (*it) += it->len;
5025 IT_CHARPOS (*it) += it->cmp_len;
5026 it->method = next_element_from_buffer;
5027 }
5028 }
5029 else if (it->method == next_element_from_c_string)
5030 {
5031 /* Current display element of IT is from a C string. */
5032 IT_BYTEPOS (*it) += it->len;
5033 IT_CHARPOS (*it) += 1;
5034 }
5035 else if (it->method == next_element_from_display_vector)
5036 {
5037 /* Current display element of IT is from a display table entry.
5038 Advance in the display table definition. Reset it to null if
5039 end reached, and continue with characters from buffers/
5040 strings. */
5041 ++it->current.dpvec_index;
5042
5043 /* Restore face of the iterator to what they were before the
5044 display vector entry (these entries may contain faces). */
5045 it->face_id = it->saved_face_id;
5046
5047 if (it->dpvec + it->current.dpvec_index == it->dpend)
5048 {
5049 if (it->s)
5050 it->method = next_element_from_c_string;
5051 else if (STRINGP (it->string))
5052 it->method = next_element_from_string;
5053 else
5054 it->method = next_element_from_buffer;
5055
5056 it->dpvec = NULL;
5057 it->current.dpvec_index = -1;
5058
5059 /* Skip over characters which were displayed via IT->dpvec. */
5060 if (it->dpvec_char_len < 0)
5061 reseat_at_next_visible_line_start (it, 1);
5062 else if (it->dpvec_char_len > 0)
5063 {
5064 it->len = it->dpvec_char_len;
5065 set_iterator_to_next (it, reseat_p);
5066 }
5067 }
5068 }
5069 else if (it->method == next_element_from_string)
5070 {
5071 /* Current display element is a character from a Lisp string. */
5072 xassert (it->s == NULL && STRINGP (it->string));
5073 IT_STRING_BYTEPOS (*it) += it->len;
5074 IT_STRING_CHARPOS (*it) += 1;
5075
5076 consider_string_end:
5077
5078 if (it->current.overlay_string_index >= 0)
5079 {
5080 /* IT->string is an overlay string. Advance to the
5081 next, if there is one. */
5082 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5083 next_overlay_string (it);
5084 }
5085 else
5086 {
5087 /* IT->string is not an overlay string. If we reached
5088 its end, and there is something on IT->stack, proceed
5089 with what is on the stack. This can be either another
5090 string, this time an overlay string, or a buffer. */
5091 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5092 && it->sp > 0)
5093 {
5094 pop_it (it);
5095 if (!STRINGP (it->string))
5096 it->method = next_element_from_buffer;
5097 else
5098 goto consider_string_end;
5099 }
5100 }
5101 }
5102 else if (it->method == next_element_from_image
5103 || it->method == next_element_from_stretch)
5104 {
5105 /* The position etc with which we have to proceed are on
5106 the stack. The position may be at the end of a string,
5107 if the `display' property takes up the whole string. */
5108 pop_it (it);
5109 it->image_id = 0;
5110 if (STRINGP (it->string))
5111 {
5112 it->method = next_element_from_string;
5113 goto consider_string_end;
5114 }
5115 else
5116 it->method = next_element_from_buffer;
5117 }
5118 else
5119 /* There are no other methods defined, so this should be a bug. */
5120 abort ();
5121
5122 xassert (it->method != next_element_from_string
5123 || (STRINGP (it->string)
5124 && IT_STRING_CHARPOS (*it) >= 0));
5125 }
5126
5127
5128 /* Load IT's display element fields with information about the next
5129 display element which comes from a display table entry or from the
5130 result of translating a control character to one of the forms `^C'
5131 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5132
5133 static int
5134 next_element_from_display_vector (it)
5135 struct it *it;
5136 {
5137 /* Precondition. */
5138 xassert (it->dpvec && it->current.dpvec_index >= 0);
5139
5140 /* Remember the current face id in case glyphs specify faces.
5141 IT's face is restored in set_iterator_to_next. */
5142 it->saved_face_id = it->face_id;
5143
5144 if (INTEGERP (*it->dpvec)
5145 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5146 {
5147 int lface_id;
5148 GLYPH g;
5149
5150 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5151 it->c = FAST_GLYPH_CHAR (g);
5152 it->len = CHAR_BYTES (it->c);
5153
5154 /* The entry may contain a face id to use. Such a face id is
5155 the id of a Lisp face, not a realized face. A face id of
5156 zero means no face is specified. */
5157 lface_id = FAST_GLYPH_FACE (g);
5158 if (lface_id)
5159 {
5160 /* The function returns -1 if lface_id is invalid. */
5161 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5162 if (face_id >= 0)
5163 it->face_id = face_id;
5164 }
5165 }
5166 else
5167 /* Display table entry is invalid. Return a space. */
5168 it->c = ' ', it->len = 1;
5169
5170 /* Don't change position and object of the iterator here. They are
5171 still the values of the character that had this display table
5172 entry or was translated, and that's what we want. */
5173 it->what = IT_CHARACTER;
5174 return 1;
5175 }
5176
5177
5178 /* Load IT with the next display element from Lisp string IT->string.
5179 IT->current.string_pos is the current position within the string.
5180 If IT->current.overlay_string_index >= 0, the Lisp string is an
5181 overlay string. */
5182
5183 static int
5184 next_element_from_string (it)
5185 struct it *it;
5186 {
5187 struct text_pos position;
5188
5189 xassert (STRINGP (it->string));
5190 xassert (IT_STRING_CHARPOS (*it) >= 0);
5191 position = it->current.string_pos;
5192
5193 /* Time to check for invisible text? */
5194 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5195 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5196 {
5197 handle_stop (it);
5198
5199 /* Since a handler may have changed IT->method, we must
5200 recurse here. */
5201 return get_next_display_element (it);
5202 }
5203
5204 if (it->current.overlay_string_index >= 0)
5205 {
5206 /* Get the next character from an overlay string. In overlay
5207 strings, There is no field width or padding with spaces to
5208 do. */
5209 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5210 {
5211 it->what = IT_EOB;
5212 return 0;
5213 }
5214 else if (STRING_MULTIBYTE (it->string))
5215 {
5216 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5217 const unsigned char *s = (SDATA (it->string)
5218 + IT_STRING_BYTEPOS (*it));
5219 it->c = string_char_and_length (s, remaining, &it->len);
5220 }
5221 else
5222 {
5223 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5224 it->len = 1;
5225 }
5226 }
5227 else
5228 {
5229 /* Get the next character from a Lisp string that is not an
5230 overlay string. Such strings come from the mode line, for
5231 example. We may have to pad with spaces, or truncate the
5232 string. See also next_element_from_c_string. */
5233 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5234 {
5235 it->what = IT_EOB;
5236 return 0;
5237 }
5238 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5239 {
5240 /* Pad with spaces. */
5241 it->c = ' ', it->len = 1;
5242 CHARPOS (position) = BYTEPOS (position) = -1;
5243 }
5244 else if (STRING_MULTIBYTE (it->string))
5245 {
5246 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5247 const unsigned char *s = (SDATA (it->string)
5248 + IT_STRING_BYTEPOS (*it));
5249 it->c = string_char_and_length (s, maxlen, &it->len);
5250 }
5251 else
5252 {
5253 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5254 it->len = 1;
5255 }
5256 }
5257
5258 /* Record what we have and where it came from. Note that we store a
5259 buffer position in IT->position although it could arguably be a
5260 string position. */
5261 it->what = IT_CHARACTER;
5262 it->object = it->string;
5263 it->position = position;
5264 return 1;
5265 }
5266
5267
5268 /* Load IT with next display element from C string IT->s.
5269 IT->string_nchars is the maximum number of characters to return
5270 from the string. IT->end_charpos may be greater than
5271 IT->string_nchars when this function is called, in which case we
5272 may have to return padding spaces. Value is zero if end of string
5273 reached, including padding spaces. */
5274
5275 static int
5276 next_element_from_c_string (it)
5277 struct it *it;
5278 {
5279 int success_p = 1;
5280
5281 xassert (it->s);
5282 it->what = IT_CHARACTER;
5283 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5284 it->object = Qnil;
5285
5286 /* IT's position can be greater IT->string_nchars in case a field
5287 width or precision has been specified when the iterator was
5288 initialized. */
5289 if (IT_CHARPOS (*it) >= it->end_charpos)
5290 {
5291 /* End of the game. */
5292 it->what = IT_EOB;
5293 success_p = 0;
5294 }
5295 else if (IT_CHARPOS (*it) >= it->string_nchars)
5296 {
5297 /* Pad with spaces. */
5298 it->c = ' ', it->len = 1;
5299 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5300 }
5301 else if (it->multibyte_p)
5302 {
5303 /* Implementation note: The calls to strlen apparently aren't a
5304 performance problem because there is no noticeable performance
5305 difference between Emacs running in unibyte or multibyte mode. */
5306 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5307 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5308 maxlen, &it->len);
5309 }
5310 else
5311 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5312
5313 return success_p;
5314 }
5315
5316
5317 /* Set up IT to return characters from an ellipsis, if appropriate.
5318 The definition of the ellipsis glyphs may come from a display table
5319 entry. This function Fills IT with the first glyph from the
5320 ellipsis if an ellipsis is to be displayed. */
5321
5322 static int
5323 next_element_from_ellipsis (it)
5324 struct it *it;
5325 {
5326 if (it->selective_display_ellipsis_p)
5327 {
5328 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5329 {
5330 /* Use the display table definition for `...'. Invalid glyphs
5331 will be handled by the method returning elements from dpvec. */
5332 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5333 it->dpvec_char_len = it->len;
5334 it->dpvec = v->contents;
5335 it->dpend = v->contents + v->size;
5336 it->current.dpvec_index = 0;
5337 it->method = next_element_from_display_vector;
5338 }
5339 else
5340 {
5341 /* Use default `...' which is stored in default_invis_vector. */
5342 it->dpvec_char_len = it->len;
5343 it->dpvec = default_invis_vector;
5344 it->dpend = default_invis_vector + 3;
5345 it->current.dpvec_index = 0;
5346 it->method = next_element_from_display_vector;
5347 }
5348 }
5349 else
5350 {
5351 /* The face at the current position may be different from the
5352 face we find after the invisible text. Remember what it
5353 was in IT->saved_face_id, and signal that it's there by
5354 setting face_before_selective_p. */
5355 it->saved_face_id = it->face_id;
5356 it->method = next_element_from_buffer;
5357 reseat_at_next_visible_line_start (it, 1);
5358 it->face_before_selective_p = 1;
5359 }
5360
5361 return get_next_display_element (it);
5362 }
5363
5364
5365 /* Deliver an image display element. The iterator IT is already
5366 filled with image information (done in handle_display_prop). Value
5367 is always 1. */
5368
5369
5370 static int
5371 next_element_from_image (it)
5372 struct it *it;
5373 {
5374 it->what = IT_IMAGE;
5375 return 1;
5376 }
5377
5378
5379 /* Fill iterator IT with next display element from a stretch glyph
5380 property. IT->object is the value of the text property. Value is
5381 always 1. */
5382
5383 static int
5384 next_element_from_stretch (it)
5385 struct it *it;
5386 {
5387 it->what = IT_STRETCH;
5388 return 1;
5389 }
5390
5391
5392 /* Load IT with the next display element from current_buffer. Value
5393 is zero if end of buffer reached. IT->stop_charpos is the next
5394 position at which to stop and check for text properties or buffer
5395 end. */
5396
5397 static int
5398 next_element_from_buffer (it)
5399 struct it *it;
5400 {
5401 int success_p = 1;
5402
5403 /* Check this assumption, otherwise, we would never enter the
5404 if-statement, below. */
5405 xassert (IT_CHARPOS (*it) >= BEGV
5406 && IT_CHARPOS (*it) <= it->stop_charpos);
5407
5408 if (IT_CHARPOS (*it) >= it->stop_charpos)
5409 {
5410 if (IT_CHARPOS (*it) >= it->end_charpos)
5411 {
5412 int overlay_strings_follow_p;
5413
5414 /* End of the game, except when overlay strings follow that
5415 haven't been returned yet. */
5416 if (it->overlay_strings_at_end_processed_p)
5417 overlay_strings_follow_p = 0;
5418 else
5419 {
5420 it->overlay_strings_at_end_processed_p = 1;
5421 overlay_strings_follow_p = get_overlay_strings (it, 0);
5422 }
5423
5424 if (overlay_strings_follow_p)
5425 success_p = get_next_display_element (it);
5426 else
5427 {
5428 it->what = IT_EOB;
5429 it->position = it->current.pos;
5430 success_p = 0;
5431 }
5432 }
5433 else
5434 {
5435 handle_stop (it);
5436 return get_next_display_element (it);
5437 }
5438 }
5439 else
5440 {
5441 /* No face changes, overlays etc. in sight, so just return a
5442 character from current_buffer. */
5443 unsigned char *p;
5444
5445 /* Maybe run the redisplay end trigger hook. Performance note:
5446 This doesn't seem to cost measurable time. */
5447 if (it->redisplay_end_trigger_charpos
5448 && it->glyph_row
5449 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5450 run_redisplay_end_trigger_hook (it);
5451
5452 /* Get the next character, maybe multibyte. */
5453 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5454 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5455 {
5456 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5457 - IT_BYTEPOS (*it));
5458 it->c = string_char_and_length (p, maxlen, &it->len);
5459 }
5460 else
5461 it->c = *p, it->len = 1;
5462
5463 /* Record what we have and where it came from. */
5464 it->what = IT_CHARACTER;;
5465 it->object = it->w->buffer;
5466 it->position = it->current.pos;
5467
5468 /* Normally we return the character found above, except when we
5469 really want to return an ellipsis for selective display. */
5470 if (it->selective)
5471 {
5472 if (it->c == '\n')
5473 {
5474 /* A value of selective > 0 means hide lines indented more
5475 than that number of columns. */
5476 if (it->selective > 0
5477 && IT_CHARPOS (*it) + 1 < ZV
5478 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5479 IT_BYTEPOS (*it) + 1,
5480 (double) it->selective)) /* iftc */
5481 {
5482 success_p = next_element_from_ellipsis (it);
5483 it->dpvec_char_len = -1;
5484 }
5485 }
5486 else if (it->c == '\r' && it->selective == -1)
5487 {
5488 /* A value of selective == -1 means that everything from the
5489 CR to the end of the line is invisible, with maybe an
5490 ellipsis displayed for it. */
5491 success_p = next_element_from_ellipsis (it);
5492 it->dpvec_char_len = -1;
5493 }
5494 }
5495 }
5496
5497 /* Value is zero if end of buffer reached. */
5498 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5499 return success_p;
5500 }
5501
5502
5503 /* Run the redisplay end trigger hook for IT. */
5504
5505 static void
5506 run_redisplay_end_trigger_hook (it)
5507 struct it *it;
5508 {
5509 Lisp_Object args[3];
5510
5511 /* IT->glyph_row should be non-null, i.e. we should be actually
5512 displaying something, or otherwise we should not run the hook. */
5513 xassert (it->glyph_row);
5514
5515 /* Set up hook arguments. */
5516 args[0] = Qredisplay_end_trigger_functions;
5517 args[1] = it->window;
5518 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5519 it->redisplay_end_trigger_charpos = 0;
5520
5521 /* Since we are *trying* to run these functions, don't try to run
5522 them again, even if they get an error. */
5523 it->w->redisplay_end_trigger = Qnil;
5524 Frun_hook_with_args (3, args);
5525
5526 /* Notice if it changed the face of the character we are on. */
5527 handle_face_prop (it);
5528 }
5529
5530
5531 /* Deliver a composition display element. The iterator IT is already
5532 filled with composition information (done in
5533 handle_composition_prop). Value is always 1. */
5534
5535 static int
5536 next_element_from_composition (it)
5537 struct it *it;
5538 {
5539 it->what = IT_COMPOSITION;
5540 it->position = (STRINGP (it->string)
5541 ? it->current.string_pos
5542 : it->current.pos);
5543 return 1;
5544 }
5545
5546
5547 \f
5548 /***********************************************************************
5549 Moving an iterator without producing glyphs
5550 ***********************************************************************/
5551
5552 /* Move iterator IT to a specified buffer or X position within one
5553 line on the display without producing glyphs.
5554
5555 OP should be a bit mask including some or all of these bits:
5556 MOVE_TO_X: Stop on reaching x-position TO_X.
5557 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5558 Regardless of OP's value, stop in reaching the end of the display line.
5559
5560 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5561 This means, in particular, that TO_X includes window's horizontal
5562 scroll amount.
5563
5564 The return value has several possible values that
5565 say what condition caused the scan to stop:
5566
5567 MOVE_POS_MATCH_OR_ZV
5568 - when TO_POS or ZV was reached.
5569
5570 MOVE_X_REACHED
5571 -when TO_X was reached before TO_POS or ZV were reached.
5572
5573 MOVE_LINE_CONTINUED
5574 - when we reached the end of the display area and the line must
5575 be continued.
5576
5577 MOVE_LINE_TRUNCATED
5578 - when we reached the end of the display area and the line is
5579 truncated.
5580
5581 MOVE_NEWLINE_OR_CR
5582 - when we stopped at a line end, i.e. a newline or a CR and selective
5583 display is on. */
5584
5585 static enum move_it_result
5586 move_it_in_display_line_to (it, to_charpos, to_x, op)
5587 struct it *it;
5588 int to_charpos, to_x, op;
5589 {
5590 enum move_it_result result = MOVE_UNDEFINED;
5591 struct glyph_row *saved_glyph_row;
5592
5593 /* Don't produce glyphs in produce_glyphs. */
5594 saved_glyph_row = it->glyph_row;
5595 it->glyph_row = NULL;
5596
5597 while (1)
5598 {
5599 int x, i, ascent = 0, descent = 0;
5600
5601 /* Stop when ZV or TO_CHARPOS reached. */
5602 if (!get_next_display_element (it)
5603 || ((op & MOVE_TO_POS) != 0
5604 && BUFFERP (it->object)
5605 && IT_CHARPOS (*it) >= to_charpos))
5606 {
5607 result = MOVE_POS_MATCH_OR_ZV;
5608 break;
5609 }
5610
5611 /* The call to produce_glyphs will get the metrics of the
5612 display element IT is loaded with. We record in x the
5613 x-position before this display element in case it does not
5614 fit on the line. */
5615 x = it->current_x;
5616
5617 /* Remember the line height so far in case the next element doesn't
5618 fit on the line. */
5619 if (!it->truncate_lines_p)
5620 {
5621 ascent = it->max_ascent;
5622 descent = it->max_descent;
5623 }
5624
5625 PRODUCE_GLYPHS (it);
5626
5627 if (it->area != TEXT_AREA)
5628 {
5629 set_iterator_to_next (it, 1);
5630 continue;
5631 }
5632
5633 /* The number of glyphs we get back in IT->nglyphs will normally
5634 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5635 character on a terminal frame, or (iii) a line end. For the
5636 second case, IT->nglyphs - 1 padding glyphs will be present
5637 (on X frames, there is only one glyph produced for a
5638 composite character.
5639
5640 The behavior implemented below means, for continuation lines,
5641 that as many spaces of a TAB as fit on the current line are
5642 displayed there. For terminal frames, as many glyphs of a
5643 multi-glyph character are displayed in the current line, too.
5644 This is what the old redisplay code did, and we keep it that
5645 way. Under X, the whole shape of a complex character must
5646 fit on the line or it will be completely displayed in the
5647 next line.
5648
5649 Note that both for tabs and padding glyphs, all glyphs have
5650 the same width. */
5651 if (it->nglyphs)
5652 {
5653 /* More than one glyph or glyph doesn't fit on line. All
5654 glyphs have the same width. */
5655 int single_glyph_width = it->pixel_width / it->nglyphs;
5656 int new_x;
5657
5658 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5659 {
5660 new_x = x + single_glyph_width;
5661
5662 /* We want to leave anything reaching TO_X to the caller. */
5663 if ((op & MOVE_TO_X) && new_x > to_x)
5664 {
5665 it->current_x = x;
5666 result = MOVE_X_REACHED;
5667 break;
5668 }
5669 else if (/* Lines are continued. */
5670 !it->truncate_lines_p
5671 && (/* And glyph doesn't fit on the line. */
5672 new_x > it->last_visible_x
5673 /* Or it fits exactly and we're on a window
5674 system frame. */
5675 || (new_x == it->last_visible_x
5676 && FRAME_WINDOW_P (it->f))))
5677 {
5678 if (/* IT->hpos == 0 means the very first glyph
5679 doesn't fit on the line, e.g. a wide image. */
5680 it->hpos == 0
5681 || (new_x == it->last_visible_x
5682 && FRAME_WINDOW_P (it->f)))
5683 {
5684 ++it->hpos;
5685 it->current_x = new_x;
5686 if (i == it->nglyphs - 1)
5687 {
5688 set_iterator_to_next (it, 1);
5689 #ifdef HAVE_WINDOW_SYSTEM
5690 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5691 {
5692 if (!get_next_display_element (it))
5693 {
5694 result = MOVE_POS_MATCH_OR_ZV;
5695 break;
5696 }
5697 if (ITERATOR_AT_END_OF_LINE_P (it))
5698 {
5699 result = MOVE_NEWLINE_OR_CR;
5700 break;
5701 }
5702 }
5703 #endif /* HAVE_WINDOW_SYSTEM */
5704 }
5705 }
5706 else
5707 {
5708 it->current_x = x;
5709 it->max_ascent = ascent;
5710 it->max_descent = descent;
5711 }
5712
5713 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5714 IT_CHARPOS (*it)));
5715 result = MOVE_LINE_CONTINUED;
5716 break;
5717 }
5718 else if (new_x > it->first_visible_x)
5719 {
5720 /* Glyph is visible. Increment number of glyphs that
5721 would be displayed. */
5722 ++it->hpos;
5723 }
5724 else
5725 {
5726 /* Glyph is completely off the left margin of the display
5727 area. Nothing to do. */
5728 }
5729 }
5730
5731 if (result != MOVE_UNDEFINED)
5732 break;
5733 }
5734 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5735 {
5736 /* Stop when TO_X specified and reached. This check is
5737 necessary here because of lines consisting of a line end,
5738 only. The line end will not produce any glyphs and we
5739 would never get MOVE_X_REACHED. */
5740 xassert (it->nglyphs == 0);
5741 result = MOVE_X_REACHED;
5742 break;
5743 }
5744
5745 /* Is this a line end? If yes, we're done. */
5746 if (ITERATOR_AT_END_OF_LINE_P (it))
5747 {
5748 result = MOVE_NEWLINE_OR_CR;
5749 break;
5750 }
5751
5752 /* The current display element has been consumed. Advance
5753 to the next. */
5754 set_iterator_to_next (it, 1);
5755
5756 /* Stop if lines are truncated and IT's current x-position is
5757 past the right edge of the window now. */
5758 if (it->truncate_lines_p
5759 && it->current_x >= it->last_visible_x)
5760 {
5761 #ifdef HAVE_WINDOW_SYSTEM
5762 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5763 {
5764 if (!get_next_display_element (it))
5765 {
5766 result = MOVE_POS_MATCH_OR_ZV;
5767 break;
5768 }
5769 if (ITERATOR_AT_END_OF_LINE_P (it))
5770 {
5771 result = MOVE_NEWLINE_OR_CR;
5772 break;
5773 }
5774 }
5775 #endif /* HAVE_WINDOW_SYSTEM */
5776 result = MOVE_LINE_TRUNCATED;
5777 break;
5778 }
5779 }
5780
5781 /* Restore the iterator settings altered at the beginning of this
5782 function. */
5783 it->glyph_row = saved_glyph_row;
5784 return result;
5785 }
5786
5787
5788 /* Move IT forward until it satisfies one or more of the criteria in
5789 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5790
5791 OP is a bit-mask that specifies where to stop, and in particular,
5792 which of those four position arguments makes a difference. See the
5793 description of enum move_operation_enum.
5794
5795 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5796 screen line, this function will set IT to the next position >
5797 TO_CHARPOS. */
5798
5799 void
5800 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5801 struct it *it;
5802 int to_charpos, to_x, to_y, to_vpos;
5803 int op;
5804 {
5805 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5806 int line_height;
5807 int reached = 0;
5808
5809 for (;;)
5810 {
5811 if (op & MOVE_TO_VPOS)
5812 {
5813 /* If no TO_CHARPOS and no TO_X specified, stop at the
5814 start of the line TO_VPOS. */
5815 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5816 {
5817 if (it->vpos == to_vpos)
5818 {
5819 reached = 1;
5820 break;
5821 }
5822 else
5823 skip = move_it_in_display_line_to (it, -1, -1, 0);
5824 }
5825 else
5826 {
5827 /* TO_VPOS >= 0 means stop at TO_X in the line at
5828 TO_VPOS, or at TO_POS, whichever comes first. */
5829 if (it->vpos == to_vpos)
5830 {
5831 reached = 2;
5832 break;
5833 }
5834
5835 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5836
5837 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5838 {
5839 reached = 3;
5840 break;
5841 }
5842 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5843 {
5844 /* We have reached TO_X but not in the line we want. */
5845 skip = move_it_in_display_line_to (it, to_charpos,
5846 -1, MOVE_TO_POS);
5847 if (skip == MOVE_POS_MATCH_OR_ZV)
5848 {
5849 reached = 4;
5850 break;
5851 }
5852 }
5853 }
5854 }
5855 else if (op & MOVE_TO_Y)
5856 {
5857 struct it it_backup;
5858
5859 /* TO_Y specified means stop at TO_X in the line containing
5860 TO_Y---or at TO_CHARPOS if this is reached first. The
5861 problem is that we can't really tell whether the line
5862 contains TO_Y before we have completely scanned it, and
5863 this may skip past TO_X. What we do is to first scan to
5864 TO_X.
5865
5866 If TO_X is not specified, use a TO_X of zero. The reason
5867 is to make the outcome of this function more predictable.
5868 If we didn't use TO_X == 0, we would stop at the end of
5869 the line which is probably not what a caller would expect
5870 to happen. */
5871 skip = move_it_in_display_line_to (it, to_charpos,
5872 ((op & MOVE_TO_X)
5873 ? to_x : 0),
5874 (MOVE_TO_X
5875 | (op & MOVE_TO_POS)));
5876
5877 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5878 if (skip == MOVE_POS_MATCH_OR_ZV)
5879 {
5880 reached = 5;
5881 break;
5882 }
5883
5884 /* If TO_X was reached, we would like to know whether TO_Y
5885 is in the line. This can only be said if we know the
5886 total line height which requires us to scan the rest of
5887 the line. */
5888 if (skip == MOVE_X_REACHED)
5889 {
5890 it_backup = *it;
5891 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5892 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5893 op & MOVE_TO_POS);
5894 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5895 }
5896
5897 /* Now, decide whether TO_Y is in this line. */
5898 line_height = it->max_ascent + it->max_descent;
5899 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5900
5901 if (to_y >= it->current_y
5902 && to_y < it->current_y + line_height)
5903 {
5904 if (skip == MOVE_X_REACHED)
5905 /* If TO_Y is in this line and TO_X was reached above,
5906 we scanned too far. We have to restore IT's settings
5907 to the ones before skipping. */
5908 *it = it_backup;
5909 reached = 6;
5910 }
5911 else if (skip == MOVE_X_REACHED)
5912 {
5913 skip = skip2;
5914 if (skip == MOVE_POS_MATCH_OR_ZV)
5915 reached = 7;
5916 }
5917
5918 if (reached)
5919 break;
5920 }
5921 else
5922 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5923
5924 switch (skip)
5925 {
5926 case MOVE_POS_MATCH_OR_ZV:
5927 reached = 8;
5928 goto out;
5929
5930 case MOVE_NEWLINE_OR_CR:
5931 set_iterator_to_next (it, 1);
5932 it->continuation_lines_width = 0;
5933 break;
5934
5935 case MOVE_LINE_TRUNCATED:
5936 it->continuation_lines_width = 0;
5937 reseat_at_next_visible_line_start (it, 0);
5938 if ((op & MOVE_TO_POS) != 0
5939 && IT_CHARPOS (*it) > to_charpos)
5940 {
5941 reached = 9;
5942 goto out;
5943 }
5944 break;
5945
5946 case MOVE_LINE_CONTINUED:
5947 it->continuation_lines_width += it->current_x;
5948 break;
5949
5950 default:
5951 abort ();
5952 }
5953
5954 /* Reset/increment for the next run. */
5955 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5956 it->current_x = it->hpos = 0;
5957 it->current_y += it->max_ascent + it->max_descent;
5958 ++it->vpos;
5959 last_height = it->max_ascent + it->max_descent;
5960 last_max_ascent = it->max_ascent;
5961 it->max_ascent = it->max_descent = 0;
5962 }
5963
5964 out:
5965
5966 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5967 }
5968
5969
5970 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5971
5972 If DY > 0, move IT backward at least that many pixels. DY = 0
5973 means move IT backward to the preceding line start or BEGV. This
5974 function may move over more than DY pixels if IT->current_y - DY
5975 ends up in the middle of a line; in this case IT->current_y will be
5976 set to the top of the line moved to. */
5977
5978 void
5979 move_it_vertically_backward (it, dy)
5980 struct it *it;
5981 int dy;
5982 {
5983 int nlines, h;
5984 struct it it2, it3;
5985 int start_pos = IT_CHARPOS (*it);
5986
5987 xassert (dy >= 0);
5988
5989 /* Estimate how many newlines we must move back. */
5990 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5991
5992 /* Set the iterator's position that many lines back. */
5993 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5994 back_to_previous_visible_line_start (it);
5995
5996 /* Reseat the iterator here. When moving backward, we don't want
5997 reseat to skip forward over invisible text, set up the iterator
5998 to deliver from overlay strings at the new position etc. So,
5999 use reseat_1 here. */
6000 reseat_1 (it, it->current.pos, 1);
6001
6002 /* We are now surely at a line start. */
6003 it->current_x = it->hpos = 0;
6004 it->continuation_lines_width = 0;
6005
6006 /* Move forward and see what y-distance we moved. First move to the
6007 start of the next line so that we get its height. We need this
6008 height to be able to tell whether we reached the specified
6009 y-distance. */
6010 it2 = *it;
6011 it2.max_ascent = it2.max_descent = 0;
6012 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6013 MOVE_TO_POS | MOVE_TO_VPOS);
6014 xassert (IT_CHARPOS (*it) >= BEGV);
6015 it3 = it2;
6016
6017 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6018 xassert (IT_CHARPOS (*it) >= BEGV);
6019 /* H is the actual vertical distance from the position in *IT
6020 and the starting position. */
6021 h = it2.current_y - it->current_y;
6022 /* NLINES is the distance in number of lines. */
6023 nlines = it2.vpos - it->vpos;
6024
6025 /* Correct IT's y and vpos position
6026 so that they are relative to the starting point. */
6027 it->vpos -= nlines;
6028 it->current_y -= h;
6029
6030 if (dy == 0)
6031 {
6032 /* DY == 0 means move to the start of the screen line. The
6033 value of nlines is > 0 if continuation lines were involved. */
6034 if (nlines > 0)
6035 move_it_by_lines (it, nlines, 1);
6036 xassert (IT_CHARPOS (*it) <= start_pos);
6037 }
6038 else
6039 {
6040 /* The y-position we try to reach, relative to *IT.
6041 Note that H has been subtracted in front of the if-statement. */
6042 int target_y = it->current_y + h - dy;
6043 int y0 = it3.current_y;
6044 int y1 = line_bottom_y (&it3);
6045 int line_height = y1 - y0;
6046
6047 /* If we did not reach target_y, try to move further backward if
6048 we can. If we moved too far backward, try to move forward. */
6049 if (target_y < it->current_y
6050 /* This is heuristic. In a window that's 3 lines high, with
6051 a line height of 13 pixels each, recentering with point
6052 on the bottom line will try to move -39/2 = 19 pixels
6053 backward. Try to avoid moving into the first line. */
6054 && it->current_y - target_y > line_height / 3 * 2
6055 && IT_CHARPOS (*it) > BEGV)
6056 {
6057 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6058 target_y - it->current_y));
6059 move_it_vertically (it, target_y - it->current_y);
6060 xassert (IT_CHARPOS (*it) >= BEGV);
6061 }
6062 else if (target_y >= it->current_y + line_height
6063 && IT_CHARPOS (*it) < ZV)
6064 {
6065 /* Should move forward by at least one line, maybe more.
6066
6067 Note: Calling move_it_by_lines can be expensive on
6068 terminal frames, where compute_motion is used (via
6069 vmotion) to do the job, when there are very long lines
6070 and truncate-lines is nil. That's the reason for
6071 treating terminal frames specially here. */
6072
6073 if (!FRAME_WINDOW_P (it->f))
6074 move_it_vertically (it, target_y - (it->current_y + line_height));
6075 else
6076 {
6077 do
6078 {
6079 move_it_by_lines (it, 1, 1);
6080 }
6081 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6082 }
6083
6084 xassert (IT_CHARPOS (*it) >= BEGV);
6085 }
6086 }
6087 }
6088
6089
6090 /* Move IT by a specified amount of pixel lines DY. DY negative means
6091 move backwards. DY = 0 means move to start of screen line. At the
6092 end, IT will be on the start of a screen line. */
6093
6094 void
6095 move_it_vertically (it, dy)
6096 struct it *it;
6097 int dy;
6098 {
6099 if (dy <= 0)
6100 move_it_vertically_backward (it, -dy);
6101 else if (dy > 0)
6102 {
6103 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6104 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6105 MOVE_TO_POS | MOVE_TO_Y);
6106 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6107
6108 /* If buffer ends in ZV without a newline, move to the start of
6109 the line to satisfy the post-condition. */
6110 if (IT_CHARPOS (*it) == ZV
6111 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6112 move_it_by_lines (it, 0, 0);
6113 }
6114 }
6115
6116
6117 /* Move iterator IT past the end of the text line it is in. */
6118
6119 void
6120 move_it_past_eol (it)
6121 struct it *it;
6122 {
6123 enum move_it_result rc;
6124
6125 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6126 if (rc == MOVE_NEWLINE_OR_CR)
6127 set_iterator_to_next (it, 0);
6128 }
6129
6130
6131 #if 0 /* Currently not used. */
6132
6133 /* Return non-zero if some text between buffer positions START_CHARPOS
6134 and END_CHARPOS is invisible. IT->window is the window for text
6135 property lookup. */
6136
6137 static int
6138 invisible_text_between_p (it, start_charpos, end_charpos)
6139 struct it *it;
6140 int start_charpos, end_charpos;
6141 {
6142 Lisp_Object prop, limit;
6143 int invisible_found_p;
6144
6145 xassert (it != NULL && start_charpos <= end_charpos);
6146
6147 /* Is text at START invisible? */
6148 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6149 it->window);
6150 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6151 invisible_found_p = 1;
6152 else
6153 {
6154 limit = Fnext_single_char_property_change (make_number (start_charpos),
6155 Qinvisible, Qnil,
6156 make_number (end_charpos));
6157 invisible_found_p = XFASTINT (limit) < end_charpos;
6158 }
6159
6160 return invisible_found_p;
6161 }
6162
6163 #endif /* 0 */
6164
6165
6166 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6167 negative means move up. DVPOS == 0 means move to the start of the
6168 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6169 NEED_Y_P is zero, IT->current_y will be left unchanged.
6170
6171 Further optimization ideas: If we would know that IT->f doesn't use
6172 a face with proportional font, we could be faster for
6173 truncate-lines nil. */
6174
6175 void
6176 move_it_by_lines (it, dvpos, need_y_p)
6177 struct it *it;
6178 int dvpos, need_y_p;
6179 {
6180 struct position pos;
6181
6182 if (!FRAME_WINDOW_P (it->f))
6183 {
6184 struct text_pos textpos;
6185
6186 /* We can use vmotion on frames without proportional fonts. */
6187 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6188 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6189 reseat (it, textpos, 1);
6190 it->vpos += pos.vpos;
6191 it->current_y += pos.vpos;
6192 }
6193 else if (dvpos == 0)
6194 {
6195 /* DVPOS == 0 means move to the start of the screen line. */
6196 move_it_vertically_backward (it, 0);
6197 xassert (it->current_x == 0 && it->hpos == 0);
6198 }
6199 else if (dvpos > 0)
6200 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6201 else
6202 {
6203 struct it it2;
6204 int start_charpos, i;
6205
6206 /* Start at the beginning of the screen line containing IT's
6207 position. */
6208 move_it_vertically_backward (it, 0);
6209
6210 /* Go back -DVPOS visible lines and reseat the iterator there. */
6211 start_charpos = IT_CHARPOS (*it);
6212 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6213 back_to_previous_visible_line_start (it);
6214 reseat (it, it->current.pos, 1);
6215 it->current_x = it->hpos = 0;
6216
6217 /* Above call may have moved too far if continuation lines
6218 are involved. Scan forward and see if it did. */
6219 it2 = *it;
6220 it2.vpos = it2.current_y = 0;
6221 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6222 it->vpos -= it2.vpos;
6223 it->current_y -= it2.current_y;
6224 it->current_x = it->hpos = 0;
6225
6226 /* If we moved too far, move IT some lines forward. */
6227 if (it2.vpos > -dvpos)
6228 {
6229 int delta = it2.vpos + dvpos;
6230 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6231 }
6232 }
6233 }
6234
6235 /* Return 1 if IT points into the middle of a display vector. */
6236
6237 int
6238 in_display_vector_p (it)
6239 struct it *it;
6240 {
6241 return (it->method == next_element_from_display_vector
6242 && it->current.dpvec_index > 0
6243 && it->dpvec + it->current.dpvec_index != it->dpend);
6244 }
6245
6246 \f
6247 /***********************************************************************
6248 Messages
6249 ***********************************************************************/
6250
6251
6252 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6253 to *Messages*. */
6254
6255 void
6256 add_to_log (format, arg1, arg2)
6257 char *format;
6258 Lisp_Object arg1, arg2;
6259 {
6260 Lisp_Object args[3];
6261 Lisp_Object msg, fmt;
6262 char *buffer;
6263 int len;
6264 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6265
6266 /* Do nothing if called asynchronously. Inserting text into
6267 a buffer may call after-change-functions and alike and
6268 that would means running Lisp asynchronously. */
6269 if (handling_signal)
6270 return;
6271
6272 fmt = msg = Qnil;
6273 GCPRO4 (fmt, msg, arg1, arg2);
6274
6275 args[0] = fmt = build_string (format);
6276 args[1] = arg1;
6277 args[2] = arg2;
6278 msg = Fformat (3, args);
6279
6280 len = SBYTES (msg) + 1;
6281 buffer = (char *) alloca (len);
6282 bcopy (SDATA (msg), buffer, len);
6283
6284 message_dolog (buffer, len - 1, 1, 0);
6285 UNGCPRO;
6286 }
6287
6288
6289 /* Output a newline in the *Messages* buffer if "needs" one. */
6290
6291 void
6292 message_log_maybe_newline ()
6293 {
6294 if (message_log_need_newline)
6295 message_dolog ("", 0, 1, 0);
6296 }
6297
6298
6299 /* Add a string M of length NBYTES to the message log, optionally
6300 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6301 nonzero, means interpret the contents of M as multibyte. This
6302 function calls low-level routines in order to bypass text property
6303 hooks, etc. which might not be safe to run. */
6304
6305 void
6306 message_dolog (m, nbytes, nlflag, multibyte)
6307 const char *m;
6308 int nbytes, nlflag, multibyte;
6309 {
6310 if (!NILP (Vmemory_full))
6311 return;
6312
6313 if (!NILP (Vmessage_log_max))
6314 {
6315 struct buffer *oldbuf;
6316 Lisp_Object oldpoint, oldbegv, oldzv;
6317 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6318 int point_at_end = 0;
6319 int zv_at_end = 0;
6320 Lisp_Object old_deactivate_mark, tem;
6321 struct gcpro gcpro1;
6322
6323 old_deactivate_mark = Vdeactivate_mark;
6324 oldbuf = current_buffer;
6325 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6326 current_buffer->undo_list = Qt;
6327
6328 oldpoint = message_dolog_marker1;
6329 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6330 oldbegv = message_dolog_marker2;
6331 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6332 oldzv = message_dolog_marker3;
6333 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6334 GCPRO1 (old_deactivate_mark);
6335
6336 if (PT == Z)
6337 point_at_end = 1;
6338 if (ZV == Z)
6339 zv_at_end = 1;
6340
6341 BEGV = BEG;
6342 BEGV_BYTE = BEG_BYTE;
6343 ZV = Z;
6344 ZV_BYTE = Z_BYTE;
6345 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6346
6347 /* Insert the string--maybe converting multibyte to single byte
6348 or vice versa, so that all the text fits the buffer. */
6349 if (multibyte
6350 && NILP (current_buffer->enable_multibyte_characters))
6351 {
6352 int i, c, char_bytes;
6353 unsigned char work[1];
6354
6355 /* Convert a multibyte string to single-byte
6356 for the *Message* buffer. */
6357 for (i = 0; i < nbytes; i += char_bytes)
6358 {
6359 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6360 work[0] = (SINGLE_BYTE_CHAR_P (c)
6361 ? c
6362 : multibyte_char_to_unibyte (c, Qnil));
6363 insert_1_both (work, 1, 1, 1, 0, 0);
6364 }
6365 }
6366 else if (! multibyte
6367 && ! NILP (current_buffer->enable_multibyte_characters))
6368 {
6369 int i, c, char_bytes;
6370 unsigned char *msg = (unsigned char *) m;
6371 unsigned char str[MAX_MULTIBYTE_LENGTH];
6372 /* Convert a single-byte string to multibyte
6373 for the *Message* buffer. */
6374 for (i = 0; i < nbytes; i++)
6375 {
6376 c = unibyte_char_to_multibyte (msg[i]);
6377 char_bytes = CHAR_STRING (c, str);
6378 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6379 }
6380 }
6381 else if (nbytes)
6382 insert_1 (m, nbytes, 1, 0, 0);
6383
6384 if (nlflag)
6385 {
6386 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6387 insert_1 ("\n", 1, 1, 0, 0);
6388
6389 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6390 this_bol = PT;
6391 this_bol_byte = PT_BYTE;
6392
6393 /* See if this line duplicates the previous one.
6394 If so, combine duplicates. */
6395 if (this_bol > BEG)
6396 {
6397 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6398 prev_bol = PT;
6399 prev_bol_byte = PT_BYTE;
6400
6401 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6402 this_bol, this_bol_byte);
6403 if (dup)
6404 {
6405 del_range_both (prev_bol, prev_bol_byte,
6406 this_bol, this_bol_byte, 0);
6407 if (dup > 1)
6408 {
6409 char dupstr[40];
6410 int duplen;
6411
6412 /* If you change this format, don't forget to also
6413 change message_log_check_duplicate. */
6414 sprintf (dupstr, " [%d times]", dup);
6415 duplen = strlen (dupstr);
6416 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6417 insert_1 (dupstr, duplen, 1, 0, 1);
6418 }
6419 }
6420 }
6421
6422 /* If we have more than the desired maximum number of lines
6423 in the *Messages* buffer now, delete the oldest ones.
6424 This is safe because we don't have undo in this buffer. */
6425
6426 if (NATNUMP (Vmessage_log_max))
6427 {
6428 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6429 -XFASTINT (Vmessage_log_max) - 1, 0);
6430 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6431 }
6432 }
6433 BEGV = XMARKER (oldbegv)->charpos;
6434 BEGV_BYTE = marker_byte_position (oldbegv);
6435
6436 if (zv_at_end)
6437 {
6438 ZV = Z;
6439 ZV_BYTE = Z_BYTE;
6440 }
6441 else
6442 {
6443 ZV = XMARKER (oldzv)->charpos;
6444 ZV_BYTE = marker_byte_position (oldzv);
6445 }
6446
6447 if (point_at_end)
6448 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6449 else
6450 /* We can't do Fgoto_char (oldpoint) because it will run some
6451 Lisp code. */
6452 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6453 XMARKER (oldpoint)->bytepos);
6454
6455 UNGCPRO;
6456 unchain_marker (XMARKER (oldpoint));
6457 unchain_marker (XMARKER (oldbegv));
6458 unchain_marker (XMARKER (oldzv));
6459
6460 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6461 set_buffer_internal (oldbuf);
6462 if (NILP (tem))
6463 windows_or_buffers_changed = old_windows_or_buffers_changed;
6464 message_log_need_newline = !nlflag;
6465 Vdeactivate_mark = old_deactivate_mark;
6466 }
6467 }
6468
6469
6470 /* We are at the end of the buffer after just having inserted a newline.
6471 (Note: We depend on the fact we won't be crossing the gap.)
6472 Check to see if the most recent message looks a lot like the previous one.
6473 Return 0 if different, 1 if the new one should just replace it, or a
6474 value N > 1 if we should also append " [N times]". */
6475
6476 static int
6477 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6478 int prev_bol, this_bol;
6479 int prev_bol_byte, this_bol_byte;
6480 {
6481 int i;
6482 int len = Z_BYTE - 1 - this_bol_byte;
6483 int seen_dots = 0;
6484 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6485 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6486
6487 for (i = 0; i < len; i++)
6488 {
6489 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6490 seen_dots = 1;
6491 if (p1[i] != p2[i])
6492 return seen_dots;
6493 }
6494 p1 += len;
6495 if (*p1 == '\n')
6496 return 2;
6497 if (*p1++ == ' ' && *p1++ == '[')
6498 {
6499 int n = 0;
6500 while (*p1 >= '0' && *p1 <= '9')
6501 n = n * 10 + *p1++ - '0';
6502 if (strncmp (p1, " times]\n", 8) == 0)
6503 return n+1;
6504 }
6505 return 0;
6506 }
6507
6508
6509 /* Display an echo area message M with a specified length of NBYTES
6510 bytes. The string may include null characters. If M is 0, clear
6511 out any existing message, and let the mini-buffer text show
6512 through.
6513
6514 The buffer M must continue to exist until after the echo area gets
6515 cleared or some other message gets displayed there. This means do
6516 not pass text that is stored in a Lisp string; do not pass text in
6517 a buffer that was alloca'd. */
6518
6519 void
6520 message2 (m, nbytes, multibyte)
6521 const char *m;
6522 int nbytes;
6523 int multibyte;
6524 {
6525 /* First flush out any partial line written with print. */
6526 message_log_maybe_newline ();
6527 if (m)
6528 message_dolog (m, nbytes, 1, multibyte);
6529 message2_nolog (m, nbytes, multibyte);
6530 }
6531
6532
6533 /* The non-logging counterpart of message2. */
6534
6535 void
6536 message2_nolog (m, nbytes, multibyte)
6537 const char *m;
6538 int nbytes, multibyte;
6539 {
6540 struct frame *sf = SELECTED_FRAME ();
6541 message_enable_multibyte = multibyte;
6542
6543 if (noninteractive)
6544 {
6545 if (noninteractive_need_newline)
6546 putc ('\n', stderr);
6547 noninteractive_need_newline = 0;
6548 if (m)
6549 fwrite (m, nbytes, 1, stderr);
6550 if (cursor_in_echo_area == 0)
6551 fprintf (stderr, "\n");
6552 fflush (stderr);
6553 }
6554 /* A null message buffer means that the frame hasn't really been
6555 initialized yet. Error messages get reported properly by
6556 cmd_error, so this must be just an informative message; toss it. */
6557 else if (INTERACTIVE
6558 && sf->glyphs_initialized_p
6559 && FRAME_MESSAGE_BUF (sf))
6560 {
6561 Lisp_Object mini_window;
6562 struct frame *f;
6563
6564 /* Get the frame containing the mini-buffer
6565 that the selected frame is using. */
6566 mini_window = FRAME_MINIBUF_WINDOW (sf);
6567 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6568
6569 FRAME_SAMPLE_VISIBILITY (f);
6570 if (FRAME_VISIBLE_P (sf)
6571 && ! FRAME_VISIBLE_P (f))
6572 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6573
6574 if (m)
6575 {
6576 set_message (m, Qnil, nbytes, multibyte);
6577 if (minibuffer_auto_raise)
6578 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6579 }
6580 else
6581 clear_message (1, 1);
6582
6583 do_pending_window_change (0);
6584 echo_area_display (1);
6585 do_pending_window_change (0);
6586 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6587 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6588 }
6589 }
6590
6591
6592 /* Display an echo area message M with a specified length of NBYTES
6593 bytes. The string may include null characters. If M is not a
6594 string, clear out any existing message, and let the mini-buffer
6595 text show through. */
6596
6597 void
6598 message3 (m, nbytes, multibyte)
6599 Lisp_Object m;
6600 int nbytes;
6601 int multibyte;
6602 {
6603 struct gcpro gcpro1;
6604
6605 GCPRO1 (m);
6606
6607 /* First flush out any partial line written with print. */
6608 message_log_maybe_newline ();
6609 if (STRINGP (m))
6610 message_dolog (SDATA (m), nbytes, 1, multibyte);
6611 message3_nolog (m, nbytes, multibyte);
6612
6613 UNGCPRO;
6614 }
6615
6616
6617 /* The non-logging version of message3. */
6618
6619 void
6620 message3_nolog (m, nbytes, multibyte)
6621 Lisp_Object m;
6622 int nbytes, multibyte;
6623 {
6624 struct frame *sf = SELECTED_FRAME ();
6625 message_enable_multibyte = multibyte;
6626
6627 if (noninteractive)
6628 {
6629 if (noninteractive_need_newline)
6630 putc ('\n', stderr);
6631 noninteractive_need_newline = 0;
6632 if (STRINGP (m))
6633 fwrite (SDATA (m), nbytes, 1, stderr);
6634 if (cursor_in_echo_area == 0)
6635 fprintf (stderr, "\n");
6636 fflush (stderr);
6637 }
6638 /* A null message buffer means that the frame hasn't really been
6639 initialized yet. Error messages get reported properly by
6640 cmd_error, so this must be just an informative message; toss it. */
6641 else if (INTERACTIVE
6642 && sf->glyphs_initialized_p
6643 && FRAME_MESSAGE_BUF (sf))
6644 {
6645 Lisp_Object mini_window;
6646 Lisp_Object frame;
6647 struct frame *f;
6648
6649 /* Get the frame containing the mini-buffer
6650 that the selected frame is using. */
6651 mini_window = FRAME_MINIBUF_WINDOW (sf);
6652 frame = XWINDOW (mini_window)->frame;
6653 f = XFRAME (frame);
6654
6655 FRAME_SAMPLE_VISIBILITY (f);
6656 if (FRAME_VISIBLE_P (sf)
6657 && !FRAME_VISIBLE_P (f))
6658 Fmake_frame_visible (frame);
6659
6660 if (STRINGP (m) && SCHARS (m) > 0)
6661 {
6662 set_message (NULL, m, nbytes, multibyte);
6663 if (minibuffer_auto_raise)
6664 Fraise_frame (frame);
6665 }
6666 else
6667 clear_message (1, 1);
6668
6669 do_pending_window_change (0);
6670 echo_area_display (1);
6671 do_pending_window_change (0);
6672 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6673 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6674 }
6675 }
6676
6677
6678 /* Display a null-terminated echo area message M. If M is 0, clear
6679 out any existing message, and let the mini-buffer text show through.
6680
6681 The buffer M must continue to exist until after the echo area gets
6682 cleared or some other message gets displayed there. Do not pass
6683 text that is stored in a Lisp string. Do not pass text in a buffer
6684 that was alloca'd. */
6685
6686 void
6687 message1 (m)
6688 char *m;
6689 {
6690 message2 (m, (m ? strlen (m) : 0), 0);
6691 }
6692
6693
6694 /* The non-logging counterpart of message1. */
6695
6696 void
6697 message1_nolog (m)
6698 char *m;
6699 {
6700 message2_nolog (m, (m ? strlen (m) : 0), 0);
6701 }
6702
6703 /* Display a message M which contains a single %s
6704 which gets replaced with STRING. */
6705
6706 void
6707 message_with_string (m, string, log)
6708 char *m;
6709 Lisp_Object string;
6710 int log;
6711 {
6712 CHECK_STRING (string);
6713
6714 if (noninteractive)
6715 {
6716 if (m)
6717 {
6718 if (noninteractive_need_newline)
6719 putc ('\n', stderr);
6720 noninteractive_need_newline = 0;
6721 fprintf (stderr, m, SDATA (string));
6722 if (cursor_in_echo_area == 0)
6723 fprintf (stderr, "\n");
6724 fflush (stderr);
6725 }
6726 }
6727 else if (INTERACTIVE)
6728 {
6729 /* The frame whose minibuffer we're going to display the message on.
6730 It may be larger than the selected frame, so we need
6731 to use its buffer, not the selected frame's buffer. */
6732 Lisp_Object mini_window;
6733 struct frame *f, *sf = SELECTED_FRAME ();
6734
6735 /* Get the frame containing the minibuffer
6736 that the selected frame is using. */
6737 mini_window = FRAME_MINIBUF_WINDOW (sf);
6738 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6739
6740 /* A null message buffer means that the frame hasn't really been
6741 initialized yet. Error messages get reported properly by
6742 cmd_error, so this must be just an informative message; toss it. */
6743 if (FRAME_MESSAGE_BUF (f))
6744 {
6745 Lisp_Object args[2], message;
6746 struct gcpro gcpro1, gcpro2;
6747
6748 args[0] = build_string (m);
6749 args[1] = message = string;
6750 GCPRO2 (args[0], message);
6751 gcpro1.nvars = 2;
6752
6753 message = Fformat (2, args);
6754
6755 if (log)
6756 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6757 else
6758 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6759
6760 UNGCPRO;
6761
6762 /* Print should start at the beginning of the message
6763 buffer next time. */
6764 message_buf_print = 0;
6765 }
6766 }
6767 }
6768
6769
6770 /* Dump an informative message to the minibuf. If M is 0, clear out
6771 any existing message, and let the mini-buffer text show through. */
6772
6773 /* VARARGS 1 */
6774 void
6775 message (m, a1, a2, a3)
6776 char *m;
6777 EMACS_INT a1, a2, a3;
6778 {
6779 if (noninteractive)
6780 {
6781 if (m)
6782 {
6783 if (noninteractive_need_newline)
6784 putc ('\n', stderr);
6785 noninteractive_need_newline = 0;
6786 fprintf (stderr, m, a1, a2, a3);
6787 if (cursor_in_echo_area == 0)
6788 fprintf (stderr, "\n");
6789 fflush (stderr);
6790 }
6791 }
6792 else if (INTERACTIVE)
6793 {
6794 /* The frame whose mini-buffer we're going to display the message
6795 on. It may be larger than the selected frame, so we need to
6796 use its buffer, not the selected frame's buffer. */
6797 Lisp_Object mini_window;
6798 struct frame *f, *sf = SELECTED_FRAME ();
6799
6800 /* Get the frame containing the mini-buffer
6801 that the selected frame is using. */
6802 mini_window = FRAME_MINIBUF_WINDOW (sf);
6803 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6804
6805 /* A null message buffer means that the frame hasn't really been
6806 initialized yet. Error messages get reported properly by
6807 cmd_error, so this must be just an informative message; toss
6808 it. */
6809 if (FRAME_MESSAGE_BUF (f))
6810 {
6811 if (m)
6812 {
6813 int len;
6814 #ifdef NO_ARG_ARRAY
6815 char *a[3];
6816 a[0] = (char *) a1;
6817 a[1] = (char *) a2;
6818 a[2] = (char *) a3;
6819
6820 len = doprnt (FRAME_MESSAGE_BUF (f),
6821 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6822 #else
6823 len = doprnt (FRAME_MESSAGE_BUF (f),
6824 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6825 (char **) &a1);
6826 #endif /* NO_ARG_ARRAY */
6827
6828 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6829 }
6830 else
6831 message1 (0);
6832
6833 /* Print should start at the beginning of the message
6834 buffer next time. */
6835 message_buf_print = 0;
6836 }
6837 }
6838 }
6839
6840
6841 /* The non-logging version of message. */
6842
6843 void
6844 message_nolog (m, a1, a2, a3)
6845 char *m;
6846 EMACS_INT a1, a2, a3;
6847 {
6848 Lisp_Object old_log_max;
6849 old_log_max = Vmessage_log_max;
6850 Vmessage_log_max = Qnil;
6851 message (m, a1, a2, a3);
6852 Vmessage_log_max = old_log_max;
6853 }
6854
6855
6856 /* Display the current message in the current mini-buffer. This is
6857 only called from error handlers in process.c, and is not time
6858 critical. */
6859
6860 void
6861 update_echo_area ()
6862 {
6863 if (!NILP (echo_area_buffer[0]))
6864 {
6865 Lisp_Object string;
6866 string = Fcurrent_message ();
6867 message3 (string, SBYTES (string),
6868 !NILP (current_buffer->enable_multibyte_characters));
6869 }
6870 }
6871
6872
6873 /* Make sure echo area buffers in `echo_buffers' are live.
6874 If they aren't, make new ones. */
6875
6876 static void
6877 ensure_echo_area_buffers ()
6878 {
6879 int i;
6880
6881 for (i = 0; i < 2; ++i)
6882 if (!BUFFERP (echo_buffer[i])
6883 || NILP (XBUFFER (echo_buffer[i])->name))
6884 {
6885 char name[30];
6886 Lisp_Object old_buffer;
6887 int j;
6888
6889 old_buffer = echo_buffer[i];
6890 sprintf (name, " *Echo Area %d*", i);
6891 echo_buffer[i] = Fget_buffer_create (build_string (name));
6892 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6893
6894 for (j = 0; j < 2; ++j)
6895 if (EQ (old_buffer, echo_area_buffer[j]))
6896 echo_area_buffer[j] = echo_buffer[i];
6897 }
6898 }
6899
6900
6901 /* Call FN with args A1..A4 with either the current or last displayed
6902 echo_area_buffer as current buffer.
6903
6904 WHICH zero means use the current message buffer
6905 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6906 from echo_buffer[] and clear it.
6907
6908 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6909 suitable buffer from echo_buffer[] and clear it.
6910
6911 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6912 that the current message becomes the last displayed one, make
6913 choose a suitable buffer for echo_area_buffer[0], and clear it.
6914
6915 Value is what FN returns. */
6916
6917 static int
6918 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6919 struct window *w;
6920 int which;
6921 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6922 EMACS_INT a1;
6923 Lisp_Object a2;
6924 EMACS_INT a3, a4;
6925 {
6926 Lisp_Object buffer;
6927 int this_one, the_other, clear_buffer_p, rc;
6928 int count = SPECPDL_INDEX ();
6929
6930 /* If buffers aren't live, make new ones. */
6931 ensure_echo_area_buffers ();
6932
6933 clear_buffer_p = 0;
6934
6935 if (which == 0)
6936 this_one = 0, the_other = 1;
6937 else if (which > 0)
6938 this_one = 1, the_other = 0;
6939 else
6940 {
6941 this_one = 0, the_other = 1;
6942 clear_buffer_p = 1;
6943
6944 /* We need a fresh one in case the current echo buffer equals
6945 the one containing the last displayed echo area message. */
6946 if (!NILP (echo_area_buffer[this_one])
6947 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6948 echo_area_buffer[this_one] = Qnil;
6949 }
6950
6951 /* Choose a suitable buffer from echo_buffer[] is we don't
6952 have one. */
6953 if (NILP (echo_area_buffer[this_one]))
6954 {
6955 echo_area_buffer[this_one]
6956 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6957 ? echo_buffer[the_other]
6958 : echo_buffer[this_one]);
6959 clear_buffer_p = 1;
6960 }
6961
6962 buffer = echo_area_buffer[this_one];
6963
6964 /* Don't get confused by reusing the buffer used for echoing
6965 for a different purpose. */
6966 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6967 cancel_echoing ();
6968
6969 record_unwind_protect (unwind_with_echo_area_buffer,
6970 with_echo_area_buffer_unwind_data (w));
6971
6972 /* Make the echo area buffer current. Note that for display
6973 purposes, it is not necessary that the displayed window's buffer
6974 == current_buffer, except for text property lookup. So, let's
6975 only set that buffer temporarily here without doing a full
6976 Fset_window_buffer. We must also change w->pointm, though,
6977 because otherwise an assertions in unshow_buffer fails, and Emacs
6978 aborts. */
6979 set_buffer_internal_1 (XBUFFER (buffer));
6980 if (w)
6981 {
6982 w->buffer = buffer;
6983 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6984 }
6985
6986 current_buffer->undo_list = Qt;
6987 current_buffer->read_only = Qnil;
6988 specbind (Qinhibit_read_only, Qt);
6989 specbind (Qinhibit_modification_hooks, Qt);
6990
6991 if (clear_buffer_p && Z > BEG)
6992 del_range (BEG, Z);
6993
6994 xassert (BEGV >= BEG);
6995 xassert (ZV <= Z && ZV >= BEGV);
6996
6997 rc = fn (a1, a2, a3, a4);
6998
6999 xassert (BEGV >= BEG);
7000 xassert (ZV <= Z && ZV >= BEGV);
7001
7002 unbind_to (count, Qnil);
7003 return rc;
7004 }
7005
7006
7007 /* Save state that should be preserved around the call to the function
7008 FN called in with_echo_area_buffer. */
7009
7010 static Lisp_Object
7011 with_echo_area_buffer_unwind_data (w)
7012 struct window *w;
7013 {
7014 int i = 0;
7015 Lisp_Object vector;
7016
7017 /* Reduce consing by keeping one vector in
7018 Vwith_echo_area_save_vector. */
7019 vector = Vwith_echo_area_save_vector;
7020 Vwith_echo_area_save_vector = Qnil;
7021
7022 if (NILP (vector))
7023 vector = Fmake_vector (make_number (7), Qnil);
7024
7025 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7026 AREF (vector, i) = Vdeactivate_mark, ++i;
7027 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7028
7029 if (w)
7030 {
7031 XSETWINDOW (AREF (vector, i), w); ++i;
7032 AREF (vector, i) = w->buffer; ++i;
7033 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7034 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7035 }
7036 else
7037 {
7038 int end = i + 4;
7039 for (; i < end; ++i)
7040 AREF (vector, i) = Qnil;
7041 }
7042
7043 xassert (i == ASIZE (vector));
7044 return vector;
7045 }
7046
7047
7048 /* Restore global state from VECTOR which was created by
7049 with_echo_area_buffer_unwind_data. */
7050
7051 static Lisp_Object
7052 unwind_with_echo_area_buffer (vector)
7053 Lisp_Object vector;
7054 {
7055 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7056 Vdeactivate_mark = AREF (vector, 1);
7057 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7058
7059 if (WINDOWP (AREF (vector, 3)))
7060 {
7061 struct window *w;
7062 Lisp_Object buffer, charpos, bytepos;
7063
7064 w = XWINDOW (AREF (vector, 3));
7065 buffer = AREF (vector, 4);
7066 charpos = AREF (vector, 5);
7067 bytepos = AREF (vector, 6);
7068
7069 w->buffer = buffer;
7070 set_marker_both (w->pointm, buffer,
7071 XFASTINT (charpos), XFASTINT (bytepos));
7072 }
7073
7074 Vwith_echo_area_save_vector = vector;
7075 return Qnil;
7076 }
7077
7078
7079 /* Set up the echo area for use by print functions. MULTIBYTE_P
7080 non-zero means we will print multibyte. */
7081
7082 void
7083 setup_echo_area_for_printing (multibyte_p)
7084 int multibyte_p;
7085 {
7086 /* If we can't find an echo area any more, exit. */
7087 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7088 Fkill_emacs (Qnil);
7089
7090 ensure_echo_area_buffers ();
7091
7092 if (!message_buf_print)
7093 {
7094 /* A message has been output since the last time we printed.
7095 Choose a fresh echo area buffer. */
7096 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7097 echo_area_buffer[0] = echo_buffer[1];
7098 else
7099 echo_area_buffer[0] = echo_buffer[0];
7100
7101 /* Switch to that buffer and clear it. */
7102 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7103 current_buffer->truncate_lines = Qnil;
7104
7105 if (Z > BEG)
7106 {
7107 int count = SPECPDL_INDEX ();
7108 specbind (Qinhibit_read_only, Qt);
7109 /* Note that undo recording is always disabled. */
7110 del_range (BEG, Z);
7111 unbind_to (count, Qnil);
7112 }
7113 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7114
7115 /* Set up the buffer for the multibyteness we need. */
7116 if (multibyte_p
7117 != !NILP (current_buffer->enable_multibyte_characters))
7118 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7119
7120 /* Raise the frame containing the echo area. */
7121 if (minibuffer_auto_raise)
7122 {
7123 struct frame *sf = SELECTED_FRAME ();
7124 Lisp_Object mini_window;
7125 mini_window = FRAME_MINIBUF_WINDOW (sf);
7126 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7127 }
7128
7129 message_log_maybe_newline ();
7130 message_buf_print = 1;
7131 }
7132 else
7133 {
7134 if (NILP (echo_area_buffer[0]))
7135 {
7136 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7137 echo_area_buffer[0] = echo_buffer[1];
7138 else
7139 echo_area_buffer[0] = echo_buffer[0];
7140 }
7141
7142 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7143 {
7144 /* Someone switched buffers between print requests. */
7145 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7146 current_buffer->truncate_lines = Qnil;
7147 }
7148 }
7149 }
7150
7151
7152 /* Display an echo area message in window W. Value is non-zero if W's
7153 height is changed. If display_last_displayed_message_p is
7154 non-zero, display the message that was last displayed, otherwise
7155 display the current message. */
7156
7157 static int
7158 display_echo_area (w)
7159 struct window *w;
7160 {
7161 int i, no_message_p, window_height_changed_p, count;
7162
7163 /* Temporarily disable garbage collections while displaying the echo
7164 area. This is done because a GC can print a message itself.
7165 That message would modify the echo area buffer's contents while a
7166 redisplay of the buffer is going on, and seriously confuse
7167 redisplay. */
7168 count = inhibit_garbage_collection ();
7169
7170 /* If there is no message, we must call display_echo_area_1
7171 nevertheless because it resizes the window. But we will have to
7172 reset the echo_area_buffer in question to nil at the end because
7173 with_echo_area_buffer will sets it to an empty buffer. */
7174 i = display_last_displayed_message_p ? 1 : 0;
7175 no_message_p = NILP (echo_area_buffer[i]);
7176
7177 window_height_changed_p
7178 = with_echo_area_buffer (w, display_last_displayed_message_p,
7179 display_echo_area_1,
7180 (EMACS_INT) w, Qnil, 0, 0);
7181
7182 if (no_message_p)
7183 echo_area_buffer[i] = Qnil;
7184
7185 unbind_to (count, Qnil);
7186 return window_height_changed_p;
7187 }
7188
7189
7190 /* Helper for display_echo_area. Display the current buffer which
7191 contains the current echo area message in window W, a mini-window,
7192 a pointer to which is passed in A1. A2..A4 are currently not used.
7193 Change the height of W so that all of the message is displayed.
7194 Value is non-zero if height of W was changed. */
7195
7196 static int
7197 display_echo_area_1 (a1, a2, a3, a4)
7198 EMACS_INT a1;
7199 Lisp_Object a2;
7200 EMACS_INT a3, a4;
7201 {
7202 struct window *w = (struct window *) a1;
7203 Lisp_Object window;
7204 struct text_pos start;
7205 int window_height_changed_p = 0;
7206
7207 /* Do this before displaying, so that we have a large enough glyph
7208 matrix for the display. */
7209 window_height_changed_p = resize_mini_window (w, 0);
7210
7211 /* Display. */
7212 clear_glyph_matrix (w->desired_matrix);
7213 XSETWINDOW (window, w);
7214 SET_TEXT_POS (start, BEG, BEG_BYTE);
7215 try_window (window, start);
7216
7217 return window_height_changed_p;
7218 }
7219
7220
7221 /* Resize the echo area window to exactly the size needed for the
7222 currently displayed message, if there is one. If a mini-buffer
7223 is active, don't shrink it. */
7224
7225 void
7226 resize_echo_area_exactly ()
7227 {
7228 if (BUFFERP (echo_area_buffer[0])
7229 && WINDOWP (echo_area_window))
7230 {
7231 struct window *w = XWINDOW (echo_area_window);
7232 int resized_p;
7233 Lisp_Object resize_exactly;
7234
7235 if (minibuf_level == 0)
7236 resize_exactly = Qt;
7237 else
7238 resize_exactly = Qnil;
7239
7240 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7241 (EMACS_INT) w, resize_exactly, 0, 0);
7242 if (resized_p)
7243 {
7244 ++windows_or_buffers_changed;
7245 ++update_mode_lines;
7246 redisplay_internal (0);
7247 }
7248 }
7249 }
7250
7251
7252 /* Callback function for with_echo_area_buffer, when used from
7253 resize_echo_area_exactly. A1 contains a pointer to the window to
7254 resize, EXACTLY non-nil means resize the mini-window exactly to the
7255 size of the text displayed. A3 and A4 are not used. Value is what
7256 resize_mini_window returns. */
7257
7258 static int
7259 resize_mini_window_1 (a1, exactly, a3, a4)
7260 EMACS_INT a1;
7261 Lisp_Object exactly;
7262 EMACS_INT a3, a4;
7263 {
7264 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7265 }
7266
7267
7268 /* Resize mini-window W to fit the size of its contents. EXACT:P
7269 means size the window exactly to the size needed. Otherwise, it's
7270 only enlarged until W's buffer is empty. Value is non-zero if
7271 the window height has been changed. */
7272
7273 int
7274 resize_mini_window (w, exact_p)
7275 struct window *w;
7276 int exact_p;
7277 {
7278 struct frame *f = XFRAME (w->frame);
7279 int window_height_changed_p = 0;
7280
7281 xassert (MINI_WINDOW_P (w));
7282
7283 /* Don't resize windows while redisplaying a window; it would
7284 confuse redisplay functions when the size of the window they are
7285 displaying changes from under them. Such a resizing can happen,
7286 for instance, when which-func prints a long message while
7287 we are running fontification-functions. We're running these
7288 functions with safe_call which binds inhibit-redisplay to t. */
7289 if (!NILP (Vinhibit_redisplay))
7290 return 0;
7291
7292 /* Nil means don't try to resize. */
7293 if (NILP (Vresize_mini_windows)
7294 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7295 return 0;
7296
7297 if (!FRAME_MINIBUF_ONLY_P (f))
7298 {
7299 struct it it;
7300 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7301 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7302 int height, max_height;
7303 int unit = FRAME_LINE_HEIGHT (f);
7304 struct text_pos start;
7305 struct buffer *old_current_buffer = NULL;
7306
7307 if (current_buffer != XBUFFER (w->buffer))
7308 {
7309 old_current_buffer = current_buffer;
7310 set_buffer_internal (XBUFFER (w->buffer));
7311 }
7312
7313 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7314
7315 /* Compute the max. number of lines specified by the user. */
7316 if (FLOATP (Vmax_mini_window_height))
7317 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7318 else if (INTEGERP (Vmax_mini_window_height))
7319 max_height = XINT (Vmax_mini_window_height);
7320 else
7321 max_height = total_height / 4;
7322
7323 /* Correct that max. height if it's bogus. */
7324 max_height = max (1, max_height);
7325 max_height = min (total_height, max_height);
7326
7327 /* Find out the height of the text in the window. */
7328 if (it.truncate_lines_p)
7329 height = 1;
7330 else
7331 {
7332 last_height = 0;
7333 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7334 if (it.max_ascent == 0 && it.max_descent == 0)
7335 height = it.current_y + last_height;
7336 else
7337 height = it.current_y + it.max_ascent + it.max_descent;
7338 height -= it.extra_line_spacing;
7339 height = (height + unit - 1) / unit;
7340 }
7341
7342 /* Compute a suitable window start. */
7343 if (height > max_height)
7344 {
7345 height = max_height;
7346 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7347 move_it_vertically_backward (&it, (height - 1) * unit);
7348 start = it.current.pos;
7349 }
7350 else
7351 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7352 SET_MARKER_FROM_TEXT_POS (w->start, start);
7353
7354 if (EQ (Vresize_mini_windows, Qgrow_only))
7355 {
7356 /* Let it grow only, until we display an empty message, in which
7357 case the window shrinks again. */
7358 if (height > WINDOW_TOTAL_LINES (w))
7359 {
7360 int old_height = WINDOW_TOTAL_LINES (w);
7361 freeze_window_starts (f, 1);
7362 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7363 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7364 }
7365 else if (height < WINDOW_TOTAL_LINES (w)
7366 && (exact_p || BEGV == ZV))
7367 {
7368 int old_height = WINDOW_TOTAL_LINES (w);
7369 freeze_window_starts (f, 0);
7370 shrink_mini_window (w);
7371 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7372 }
7373 }
7374 else
7375 {
7376 /* Always resize to exact size needed. */
7377 if (height > WINDOW_TOTAL_LINES (w))
7378 {
7379 int old_height = WINDOW_TOTAL_LINES (w);
7380 freeze_window_starts (f, 1);
7381 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7382 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7383 }
7384 else if (height < WINDOW_TOTAL_LINES (w))
7385 {
7386 int old_height = WINDOW_TOTAL_LINES (w);
7387 freeze_window_starts (f, 0);
7388 shrink_mini_window (w);
7389
7390 if (height)
7391 {
7392 freeze_window_starts (f, 1);
7393 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7394 }
7395
7396 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7397 }
7398 }
7399
7400 if (old_current_buffer)
7401 set_buffer_internal (old_current_buffer);
7402 }
7403
7404 return window_height_changed_p;
7405 }
7406
7407
7408 /* Value is the current message, a string, or nil if there is no
7409 current message. */
7410
7411 Lisp_Object
7412 current_message ()
7413 {
7414 Lisp_Object msg;
7415
7416 if (NILP (echo_area_buffer[0]))
7417 msg = Qnil;
7418 else
7419 {
7420 with_echo_area_buffer (0, 0, current_message_1,
7421 (EMACS_INT) &msg, Qnil, 0, 0);
7422 if (NILP (msg))
7423 echo_area_buffer[0] = Qnil;
7424 }
7425
7426 return msg;
7427 }
7428
7429
7430 static int
7431 current_message_1 (a1, a2, a3, a4)
7432 EMACS_INT a1;
7433 Lisp_Object a2;
7434 EMACS_INT a3, a4;
7435 {
7436 Lisp_Object *msg = (Lisp_Object *) a1;
7437
7438 if (Z > BEG)
7439 *msg = make_buffer_string (BEG, Z, 1);
7440 else
7441 *msg = Qnil;
7442 return 0;
7443 }
7444
7445
7446 /* Push the current message on Vmessage_stack for later restauration
7447 by restore_message. Value is non-zero if the current message isn't
7448 empty. This is a relatively infrequent operation, so it's not
7449 worth optimizing. */
7450
7451 int
7452 push_message ()
7453 {
7454 Lisp_Object msg;
7455 msg = current_message ();
7456 Vmessage_stack = Fcons (msg, Vmessage_stack);
7457 return STRINGP (msg);
7458 }
7459
7460
7461 /* Restore message display from the top of Vmessage_stack. */
7462
7463 void
7464 restore_message ()
7465 {
7466 Lisp_Object msg;
7467
7468 xassert (CONSP (Vmessage_stack));
7469 msg = XCAR (Vmessage_stack);
7470 if (STRINGP (msg))
7471 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7472 else
7473 message3_nolog (msg, 0, 0);
7474 }
7475
7476
7477 /* Handler for record_unwind_protect calling pop_message. */
7478
7479 Lisp_Object
7480 pop_message_unwind (dummy)
7481 Lisp_Object dummy;
7482 {
7483 pop_message ();
7484 return Qnil;
7485 }
7486
7487 /* Pop the top-most entry off Vmessage_stack. */
7488
7489 void
7490 pop_message ()
7491 {
7492 xassert (CONSP (Vmessage_stack));
7493 Vmessage_stack = XCDR (Vmessage_stack);
7494 }
7495
7496
7497 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7498 exits. If the stack is not empty, we have a missing pop_message
7499 somewhere. */
7500
7501 void
7502 check_message_stack ()
7503 {
7504 if (!NILP (Vmessage_stack))
7505 abort ();
7506 }
7507
7508
7509 /* Truncate to NCHARS what will be displayed in the echo area the next
7510 time we display it---but don't redisplay it now. */
7511
7512 void
7513 truncate_echo_area (nchars)
7514 int nchars;
7515 {
7516 if (nchars == 0)
7517 echo_area_buffer[0] = Qnil;
7518 /* A null message buffer means that the frame hasn't really been
7519 initialized yet. Error messages get reported properly by
7520 cmd_error, so this must be just an informative message; toss it. */
7521 else if (!noninteractive
7522 && INTERACTIVE
7523 && !NILP (echo_area_buffer[0]))
7524 {
7525 struct frame *sf = SELECTED_FRAME ();
7526 if (FRAME_MESSAGE_BUF (sf))
7527 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7528 }
7529 }
7530
7531
7532 /* Helper function for truncate_echo_area. Truncate the current
7533 message to at most NCHARS characters. */
7534
7535 static int
7536 truncate_message_1 (nchars, a2, a3, a4)
7537 EMACS_INT nchars;
7538 Lisp_Object a2;
7539 EMACS_INT a3, a4;
7540 {
7541 if (BEG + nchars < Z)
7542 del_range (BEG + nchars, Z);
7543 if (Z == BEG)
7544 echo_area_buffer[0] = Qnil;
7545 return 0;
7546 }
7547
7548
7549 /* Set the current message to a substring of S or STRING.
7550
7551 If STRING is a Lisp string, set the message to the first NBYTES
7552 bytes from STRING. NBYTES zero means use the whole string. If
7553 STRING is multibyte, the message will be displayed multibyte.
7554
7555 If S is not null, set the message to the first LEN bytes of S. LEN
7556 zero means use the whole string. MULTIBYTE_P non-zero means S is
7557 multibyte. Display the message multibyte in that case. */
7558
7559 void
7560 set_message (s, string, nbytes, multibyte_p)
7561 const char *s;
7562 Lisp_Object string;
7563 int nbytes, multibyte_p;
7564 {
7565 message_enable_multibyte
7566 = ((s && multibyte_p)
7567 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7568
7569 with_echo_area_buffer (0, -1, set_message_1,
7570 (EMACS_INT) s, string, nbytes, multibyte_p);
7571 message_buf_print = 0;
7572 help_echo_showing_p = 0;
7573 }
7574
7575
7576 /* Helper function for set_message. Arguments have the same meaning
7577 as there, with A1 corresponding to S and A2 corresponding to STRING
7578 This function is called with the echo area buffer being
7579 current. */
7580
7581 static int
7582 set_message_1 (a1, a2, nbytes, multibyte_p)
7583 EMACS_INT a1;
7584 Lisp_Object a2;
7585 EMACS_INT nbytes, multibyte_p;
7586 {
7587 const char *s = (const char *) a1;
7588 Lisp_Object string = a2;
7589
7590 xassert (BEG == Z);
7591
7592 /* Change multibyteness of the echo buffer appropriately. */
7593 if (message_enable_multibyte
7594 != !NILP (current_buffer->enable_multibyte_characters))
7595 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7596
7597 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7598
7599 /* Insert new message at BEG. */
7600 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7601
7602 if (STRINGP (string))
7603 {
7604 int nchars;
7605
7606 if (nbytes == 0)
7607 nbytes = SBYTES (string);
7608 nchars = string_byte_to_char (string, nbytes);
7609
7610 /* This function takes care of single/multibyte conversion. We
7611 just have to ensure that the echo area buffer has the right
7612 setting of enable_multibyte_characters. */
7613 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7614 }
7615 else if (s)
7616 {
7617 if (nbytes == 0)
7618 nbytes = strlen (s);
7619
7620 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7621 {
7622 /* Convert from multi-byte to single-byte. */
7623 int i, c, n;
7624 unsigned char work[1];
7625
7626 /* Convert a multibyte string to single-byte. */
7627 for (i = 0; i < nbytes; i += n)
7628 {
7629 c = string_char_and_length (s + i, nbytes - i, &n);
7630 work[0] = (SINGLE_BYTE_CHAR_P (c)
7631 ? c
7632 : multibyte_char_to_unibyte (c, Qnil));
7633 insert_1_both (work, 1, 1, 1, 0, 0);
7634 }
7635 }
7636 else if (!multibyte_p
7637 && !NILP (current_buffer->enable_multibyte_characters))
7638 {
7639 /* Convert from single-byte to multi-byte. */
7640 int i, c, n;
7641 const unsigned char *msg = (const unsigned char *) s;
7642 unsigned char str[MAX_MULTIBYTE_LENGTH];
7643
7644 /* Convert a single-byte string to multibyte. */
7645 for (i = 0; i < nbytes; i++)
7646 {
7647 c = unibyte_char_to_multibyte (msg[i]);
7648 n = CHAR_STRING (c, str);
7649 insert_1_both (str, 1, n, 1, 0, 0);
7650 }
7651 }
7652 else
7653 insert_1 (s, nbytes, 1, 0, 0);
7654 }
7655
7656 return 0;
7657 }
7658
7659
7660 /* Clear messages. CURRENT_P non-zero means clear the current
7661 message. LAST_DISPLAYED_P non-zero means clear the message
7662 last displayed. */
7663
7664 void
7665 clear_message (current_p, last_displayed_p)
7666 int current_p, last_displayed_p;
7667 {
7668 if (current_p)
7669 {
7670 echo_area_buffer[0] = Qnil;
7671 message_cleared_p = 1;
7672 }
7673
7674 if (last_displayed_p)
7675 echo_area_buffer[1] = Qnil;
7676
7677 message_buf_print = 0;
7678 }
7679
7680 /* Clear garbaged frames.
7681
7682 This function is used where the old redisplay called
7683 redraw_garbaged_frames which in turn called redraw_frame which in
7684 turn called clear_frame. The call to clear_frame was a source of
7685 flickering. I believe a clear_frame is not necessary. It should
7686 suffice in the new redisplay to invalidate all current matrices,
7687 and ensure a complete redisplay of all windows. */
7688
7689 static void
7690 clear_garbaged_frames ()
7691 {
7692 if (frame_garbaged)
7693 {
7694 Lisp_Object tail, frame;
7695 int changed_count = 0;
7696
7697 FOR_EACH_FRAME (tail, frame)
7698 {
7699 struct frame *f = XFRAME (frame);
7700
7701 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7702 {
7703 if (f->resized_p)
7704 {
7705 Fredraw_frame (frame);
7706 f->force_flush_display_p = 1;
7707 }
7708 clear_current_matrices (f);
7709 changed_count++;
7710 f->garbaged = 0;
7711 f->resized_p = 0;
7712 }
7713 }
7714
7715 frame_garbaged = 0;
7716 if (changed_count)
7717 ++windows_or_buffers_changed;
7718 }
7719 }
7720
7721
7722 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7723 is non-zero update selected_frame. Value is non-zero if the
7724 mini-windows height has been changed. */
7725
7726 static int
7727 echo_area_display (update_frame_p)
7728 int update_frame_p;
7729 {
7730 Lisp_Object mini_window;
7731 struct window *w;
7732 struct frame *f;
7733 int window_height_changed_p = 0;
7734 struct frame *sf = SELECTED_FRAME ();
7735
7736 mini_window = FRAME_MINIBUF_WINDOW (sf);
7737 w = XWINDOW (mini_window);
7738 f = XFRAME (WINDOW_FRAME (w));
7739
7740 /* Don't display if frame is invisible or not yet initialized. */
7741 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7742 return 0;
7743
7744 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7745 #ifndef MAC_OS8
7746 #ifdef HAVE_WINDOW_SYSTEM
7747 /* When Emacs starts, selected_frame may be the initial terminal
7748 frame. If we let this through, a message would be displayed on
7749 the terminal. */
7750 if (FRAME_TERMCAP_P (XFRAME (selected_frame))
7751 && FRAME_TTY (XFRAME (selected_frame))->type == NULL)
7752 return 0;
7753 #endif /* HAVE_WINDOW_SYSTEM */
7754 #endif
7755
7756 /* Redraw garbaged frames. */
7757 if (frame_garbaged)
7758 clear_garbaged_frames ();
7759
7760 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7761 {
7762 echo_area_window = mini_window;
7763 window_height_changed_p = display_echo_area (w);
7764 w->must_be_updated_p = 1;
7765
7766 /* Update the display, unless called from redisplay_internal.
7767 Also don't update the screen during redisplay itself. The
7768 update will happen at the end of redisplay, and an update
7769 here could cause confusion. */
7770 if (update_frame_p && !redisplaying_p)
7771 {
7772 int n = 0;
7773
7774 /* If the display update has been interrupted by pending
7775 input, update mode lines in the frame. Due to the
7776 pending input, it might have been that redisplay hasn't
7777 been called, so that mode lines above the echo area are
7778 garbaged. This looks odd, so we prevent it here. */
7779 if (!display_completed)
7780 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7781
7782 if (window_height_changed_p
7783 /* Don't do this if Emacs is shutting down. Redisplay
7784 needs to run hooks. */
7785 && !NILP (Vrun_hooks))
7786 {
7787 /* Must update other windows. Likewise as in other
7788 cases, don't let this update be interrupted by
7789 pending input. */
7790 int count = SPECPDL_INDEX ();
7791 specbind (Qredisplay_dont_pause, Qt);
7792 windows_or_buffers_changed = 1;
7793 redisplay_internal (0);
7794 unbind_to (count, Qnil);
7795 }
7796 else if (FRAME_WINDOW_P (f) && n == 0)
7797 {
7798 /* Window configuration is the same as before.
7799 Can do with a display update of the echo area,
7800 unless we displayed some mode lines. */
7801 update_single_window (w, 1);
7802 FRAME_RIF (f)->flush_display (f);
7803 }
7804 else
7805 update_frame (f, 1, 1);
7806
7807 /* If cursor is in the echo area, make sure that the next
7808 redisplay displays the minibuffer, so that the cursor will
7809 be replaced with what the minibuffer wants. */
7810 if (cursor_in_echo_area)
7811 ++windows_or_buffers_changed;
7812 }
7813 }
7814 else if (!EQ (mini_window, selected_window))
7815 windows_or_buffers_changed++;
7816
7817 /* Last displayed message is now the current message. */
7818 echo_area_buffer[1] = echo_area_buffer[0];
7819
7820 /* Prevent redisplay optimization in redisplay_internal by resetting
7821 this_line_start_pos. This is done because the mini-buffer now
7822 displays the message instead of its buffer text. */
7823 if (EQ (mini_window, selected_window))
7824 CHARPOS (this_line_start_pos) = 0;
7825
7826 return window_height_changed_p;
7827 }
7828
7829
7830 \f
7831 /***********************************************************************
7832 Frame Titles
7833 ***********************************************************************/
7834
7835
7836 /* The frame title buffering code is also used by Fformat_mode_line.
7837 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7838
7839 /* A buffer for constructing frame titles in it; allocated from the
7840 heap in init_xdisp and resized as needed in store_frame_title_char. */
7841
7842 static char *frame_title_buf;
7843
7844 /* The buffer's end, and a current output position in it. */
7845
7846 static char *frame_title_buf_end;
7847 static char *frame_title_ptr;
7848
7849
7850 /* Store a single character C for the frame title in frame_title_buf.
7851 Re-allocate frame_title_buf if necessary. */
7852
7853 static void
7854 #ifdef PROTOTYPES
7855 store_frame_title_char (char c)
7856 #else
7857 store_frame_title_char (c)
7858 char c;
7859 #endif
7860 {
7861 /* If output position has reached the end of the allocated buffer,
7862 double the buffer's size. */
7863 if (frame_title_ptr == frame_title_buf_end)
7864 {
7865 int len = frame_title_ptr - frame_title_buf;
7866 int new_size = 2 * len * sizeof *frame_title_buf;
7867 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7868 frame_title_buf_end = frame_title_buf + new_size;
7869 frame_title_ptr = frame_title_buf + len;
7870 }
7871
7872 *frame_title_ptr++ = c;
7873 }
7874
7875
7876 /* Store part of a frame title in frame_title_buf, beginning at
7877 frame_title_ptr. STR is the string to store. Do not copy
7878 characters that yield more columns than PRECISION; PRECISION <= 0
7879 means copy the whole string. Pad with spaces until FIELD_WIDTH
7880 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7881 pad. Called from display_mode_element when it is used to build a
7882 frame title. */
7883
7884 static int
7885 store_frame_title (str, field_width, precision)
7886 const unsigned char *str;
7887 int field_width, precision;
7888 {
7889 int n = 0;
7890 int dummy, nbytes;
7891
7892 /* Copy at most PRECISION chars from STR. */
7893 nbytes = strlen (str);
7894 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7895 while (nbytes--)
7896 store_frame_title_char (*str++);
7897
7898 /* Fill up with spaces until FIELD_WIDTH reached. */
7899 while (field_width > 0
7900 && n < field_width)
7901 {
7902 store_frame_title_char (' ');
7903 ++n;
7904 }
7905
7906 return n;
7907 }
7908
7909 #ifdef HAVE_WINDOW_SYSTEM
7910
7911 /* Set the title of FRAME, if it has changed. The title format is
7912 Vicon_title_format if FRAME is iconified, otherwise it is
7913 frame_title_format. */
7914
7915 static void
7916 x_consider_frame_title (frame)
7917 Lisp_Object frame;
7918 {
7919 struct frame *f = XFRAME (frame);
7920
7921 if (FRAME_WINDOW_P (f)
7922 || FRAME_MINIBUF_ONLY_P (f)
7923 || f->explicit_name)
7924 {
7925 /* Do we have more than one visible frame on this X display? */
7926 Lisp_Object tail;
7927 Lisp_Object fmt;
7928 struct buffer *obuf;
7929 int len;
7930 struct it it;
7931
7932 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7933 {
7934 Lisp_Object other_frame = XCAR (tail);
7935 struct frame *tf = XFRAME (other_frame);
7936
7937 if (tf != f
7938 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7939 && !FRAME_MINIBUF_ONLY_P (tf)
7940 && !EQ (other_frame, tip_frame)
7941 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7942 break;
7943 }
7944
7945 /* Set global variable indicating that multiple frames exist. */
7946 multiple_frames = CONSP (tail);
7947
7948 /* Switch to the buffer of selected window of the frame. Set up
7949 frame_title_ptr so that display_mode_element will output into it;
7950 then display the title. */
7951 obuf = current_buffer;
7952 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7953 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7954 frame_title_ptr = frame_title_buf;
7955 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7956 NULL, DEFAULT_FACE_ID);
7957 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7958 len = frame_title_ptr - frame_title_buf;
7959 frame_title_ptr = NULL;
7960 set_buffer_internal_1 (obuf);
7961
7962 /* Set the title only if it's changed. This avoids consing in
7963 the common case where it hasn't. (If it turns out that we've
7964 already wasted too much time by walking through the list with
7965 display_mode_element, then we might need to optimize at a
7966 higher level than this.) */
7967 if (! STRINGP (f->name)
7968 || SBYTES (f->name) != len
7969 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7970 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7971 }
7972 }
7973
7974 #endif /* not HAVE_WINDOW_SYSTEM */
7975
7976
7977
7978 \f
7979 /***********************************************************************
7980 Menu Bars
7981 ***********************************************************************/
7982
7983
7984 /* Prepare for redisplay by updating menu-bar item lists when
7985 appropriate. This can call eval. */
7986
7987 void
7988 prepare_menu_bars ()
7989 {
7990 int all_windows;
7991 struct gcpro gcpro1, gcpro2;
7992 struct frame *f;
7993 Lisp_Object tooltip_frame;
7994
7995 #ifdef HAVE_WINDOW_SYSTEM
7996 tooltip_frame = tip_frame;
7997 #else
7998 tooltip_frame = Qnil;
7999 #endif
8000
8001 /* Update all frame titles based on their buffer names, etc. We do
8002 this before the menu bars so that the buffer-menu will show the
8003 up-to-date frame titles. */
8004 #ifdef HAVE_WINDOW_SYSTEM
8005 if (windows_or_buffers_changed || update_mode_lines)
8006 {
8007 Lisp_Object tail, frame;
8008
8009 FOR_EACH_FRAME (tail, frame)
8010 {
8011 f = XFRAME (frame);
8012 if (!EQ (frame, tooltip_frame)
8013 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8014 x_consider_frame_title (frame);
8015 }
8016 }
8017 #endif /* HAVE_WINDOW_SYSTEM */
8018
8019 /* Update the menu bar item lists, if appropriate. This has to be
8020 done before any actual redisplay or generation of display lines. */
8021 all_windows = (update_mode_lines
8022 || buffer_shared > 1
8023 || windows_or_buffers_changed);
8024 if (all_windows)
8025 {
8026 Lisp_Object tail, frame;
8027 int count = SPECPDL_INDEX ();
8028
8029 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8030
8031 FOR_EACH_FRAME (tail, frame)
8032 {
8033 f = XFRAME (frame);
8034
8035 /* Ignore tooltip frame. */
8036 if (EQ (frame, tooltip_frame))
8037 continue;
8038
8039 /* If a window on this frame changed size, report that to
8040 the user and clear the size-change flag. */
8041 if (FRAME_WINDOW_SIZES_CHANGED (f))
8042 {
8043 Lisp_Object functions;
8044
8045 /* Clear flag first in case we get an error below. */
8046 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8047 functions = Vwindow_size_change_functions;
8048 GCPRO2 (tail, functions);
8049
8050 while (CONSP (functions))
8051 {
8052 call1 (XCAR (functions), frame);
8053 functions = XCDR (functions);
8054 }
8055 UNGCPRO;
8056 }
8057
8058 GCPRO1 (tail);
8059 update_menu_bar (f, 0);
8060 #ifdef HAVE_WINDOW_SYSTEM
8061 update_tool_bar (f, 0);
8062 #endif
8063 UNGCPRO;
8064 }
8065
8066 unbind_to (count, Qnil);
8067 }
8068 else
8069 {
8070 struct frame *sf = SELECTED_FRAME ();
8071 update_menu_bar (sf, 1);
8072 #ifdef HAVE_WINDOW_SYSTEM
8073 update_tool_bar (sf, 1);
8074 #endif
8075 }
8076
8077 /* Motif needs this. See comment in xmenu.c. Turn it off when
8078 pending_menu_activation is not defined. */
8079 #ifdef USE_X_TOOLKIT
8080 pending_menu_activation = 0;
8081 #endif
8082 }
8083
8084
8085 /* Update the menu bar item list for frame F. This has to be done
8086 before we start to fill in any display lines, because it can call
8087 eval.
8088
8089 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8090
8091 static void
8092 update_menu_bar (f, save_match_data)
8093 struct frame *f;
8094 int save_match_data;
8095 {
8096 Lisp_Object window;
8097 register struct window *w;
8098
8099 /* If called recursively during a menu update, do nothing. This can
8100 happen when, for instance, an activate-menubar-hook causes a
8101 redisplay. */
8102 if (inhibit_menubar_update)
8103 return;
8104
8105 window = FRAME_SELECTED_WINDOW (f);
8106 w = XWINDOW (window);
8107
8108 #if 0 /* The if statement below this if statement used to include the
8109 condition !NILP (w->update_mode_line), rather than using
8110 update_mode_lines directly, and this if statement may have
8111 been added to make that condition work. Now the if
8112 statement below matches its comment, this isn't needed. */
8113 if (update_mode_lines)
8114 w->update_mode_line = Qt;
8115 #endif
8116
8117 if (FRAME_WINDOW_P (f)
8118 ?
8119 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8120 || defined (USE_GTK)
8121 FRAME_EXTERNAL_MENU_BAR (f)
8122 #else
8123 FRAME_MENU_BAR_LINES (f) > 0
8124 #endif
8125 : FRAME_MENU_BAR_LINES (f) > 0)
8126 {
8127 /* If the user has switched buffers or windows, we need to
8128 recompute to reflect the new bindings. But we'll
8129 recompute when update_mode_lines is set too; that means
8130 that people can use force-mode-line-update to request
8131 that the menu bar be recomputed. The adverse effect on
8132 the rest of the redisplay algorithm is about the same as
8133 windows_or_buffers_changed anyway. */
8134 if (windows_or_buffers_changed
8135 /* This used to test w->update_mode_line, but we believe
8136 there is no need to recompute the menu in that case. */
8137 || update_mode_lines
8138 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8139 < BUF_MODIFF (XBUFFER (w->buffer)))
8140 != !NILP (w->last_had_star))
8141 || ((!NILP (Vtransient_mark_mode)
8142 && !NILP (XBUFFER (w->buffer)->mark_active))
8143 != !NILP (w->region_showing)))
8144 {
8145 struct buffer *prev = current_buffer;
8146 int count = SPECPDL_INDEX ();
8147
8148 specbind (Qinhibit_menubar_update, Qt);
8149
8150 set_buffer_internal_1 (XBUFFER (w->buffer));
8151 if (save_match_data)
8152 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8153 if (NILP (Voverriding_local_map_menu_flag))
8154 {
8155 specbind (Qoverriding_terminal_local_map, Qnil);
8156 specbind (Qoverriding_local_map, Qnil);
8157 }
8158
8159 /* Run the Lucid hook. */
8160 safe_run_hooks (Qactivate_menubar_hook);
8161
8162 /* If it has changed current-menubar from previous value,
8163 really recompute the menu-bar from the value. */
8164 if (! NILP (Vlucid_menu_bar_dirty_flag))
8165 call0 (Qrecompute_lucid_menubar);
8166
8167 safe_run_hooks (Qmenu_bar_update_hook);
8168 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8169
8170 /* Redisplay the menu bar in case we changed it. */
8171 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8172 || defined (USE_GTK)
8173 if (FRAME_WINDOW_P (f)
8174 #if defined (MAC_OS)
8175 /* All frames on Mac OS share the same menubar. So only the
8176 selected frame should be allowed to set it. */
8177 && f == SELECTED_FRAME ()
8178 #endif
8179 )
8180 set_frame_menubar (f, 0, 0);
8181 else
8182 /* On a terminal screen, the menu bar is an ordinary screen
8183 line, and this makes it get updated. */
8184 w->update_mode_line = Qt;
8185 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8186 /* In the non-toolkit version, the menu bar is an ordinary screen
8187 line, and this makes it get updated. */
8188 w->update_mode_line = Qt;
8189 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8190
8191 unbind_to (count, Qnil);
8192 set_buffer_internal_1 (prev);
8193 }
8194 }
8195 }
8196
8197
8198 \f
8199 /***********************************************************************
8200 Output Cursor
8201 ***********************************************************************/
8202
8203 #ifdef HAVE_WINDOW_SYSTEM
8204
8205 /* EXPORT:
8206 Nominal cursor position -- where to draw output.
8207 HPOS and VPOS are window relative glyph matrix coordinates.
8208 X and Y are window relative pixel coordinates. */
8209
8210 struct cursor_pos output_cursor;
8211
8212
8213 /* EXPORT:
8214 Set the global variable output_cursor to CURSOR. All cursor
8215 positions are relative to updated_window. */
8216
8217 void
8218 set_output_cursor (cursor)
8219 struct cursor_pos *cursor;
8220 {
8221 output_cursor.hpos = cursor->hpos;
8222 output_cursor.vpos = cursor->vpos;
8223 output_cursor.x = cursor->x;
8224 output_cursor.y = cursor->y;
8225 }
8226
8227
8228 /* EXPORT for RIF:
8229 Set a nominal cursor position.
8230
8231 HPOS and VPOS are column/row positions in a window glyph matrix. X
8232 and Y are window text area relative pixel positions.
8233
8234 If this is done during an update, updated_window will contain the
8235 window that is being updated and the position is the future output
8236 cursor position for that window. If updated_window is null, use
8237 selected_window and display the cursor at the given position. */
8238
8239 void
8240 x_cursor_to (vpos, hpos, y, x)
8241 int vpos, hpos, y, x;
8242 {
8243 struct window *w;
8244
8245 /* If updated_window is not set, work on selected_window. */
8246 if (updated_window)
8247 w = updated_window;
8248 else
8249 w = XWINDOW (selected_window);
8250
8251 /* Set the output cursor. */
8252 output_cursor.hpos = hpos;
8253 output_cursor.vpos = vpos;
8254 output_cursor.x = x;
8255 output_cursor.y = y;
8256
8257 /* If not called as part of an update, really display the cursor.
8258 This will also set the cursor position of W. */
8259 if (updated_window == NULL)
8260 {
8261 BLOCK_INPUT;
8262 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8263 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8264 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8265 UNBLOCK_INPUT;
8266 }
8267 }
8268
8269 #endif /* HAVE_WINDOW_SYSTEM */
8270
8271 \f
8272 /***********************************************************************
8273 Tool-bars
8274 ***********************************************************************/
8275
8276 #ifdef HAVE_WINDOW_SYSTEM
8277
8278 /* Where the mouse was last time we reported a mouse event. */
8279
8280 FRAME_PTR last_mouse_frame;
8281
8282 /* Tool-bar item index of the item on which a mouse button was pressed
8283 or -1. */
8284
8285 int last_tool_bar_item;
8286
8287
8288 /* Update the tool-bar item list for frame F. This has to be done
8289 before we start to fill in any display lines. Called from
8290 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8291 and restore it here. */
8292
8293 static void
8294 update_tool_bar (f, save_match_data)
8295 struct frame *f;
8296 int save_match_data;
8297 {
8298 #ifdef USE_GTK
8299 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8300 #else
8301 int do_update = WINDOWP (f->tool_bar_window)
8302 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8303 #endif
8304
8305 if (do_update)
8306 {
8307 Lisp_Object window;
8308 struct window *w;
8309
8310 window = FRAME_SELECTED_WINDOW (f);
8311 w = XWINDOW (window);
8312
8313 /* If the user has switched buffers or windows, we need to
8314 recompute to reflect the new bindings. But we'll
8315 recompute when update_mode_lines is set too; that means
8316 that people can use force-mode-line-update to request
8317 that the menu bar be recomputed. The adverse effect on
8318 the rest of the redisplay algorithm is about the same as
8319 windows_or_buffers_changed anyway. */
8320 if (windows_or_buffers_changed
8321 || !NILP (w->update_mode_line)
8322 || update_mode_lines
8323 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8324 < BUF_MODIFF (XBUFFER (w->buffer)))
8325 != !NILP (w->last_had_star))
8326 || ((!NILP (Vtransient_mark_mode)
8327 && !NILP (XBUFFER (w->buffer)->mark_active))
8328 != !NILP (w->region_showing)))
8329 {
8330 struct buffer *prev = current_buffer;
8331 int count = SPECPDL_INDEX ();
8332 Lisp_Object old_tool_bar;
8333 struct gcpro gcpro1;
8334
8335 /* Set current_buffer to the buffer of the selected
8336 window of the frame, so that we get the right local
8337 keymaps. */
8338 set_buffer_internal_1 (XBUFFER (w->buffer));
8339
8340 /* Save match data, if we must. */
8341 if (save_match_data)
8342 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8343
8344 /* Make sure that we don't accidentally use bogus keymaps. */
8345 if (NILP (Voverriding_local_map_menu_flag))
8346 {
8347 specbind (Qoverriding_terminal_local_map, Qnil);
8348 specbind (Qoverriding_local_map, Qnil);
8349 }
8350
8351 old_tool_bar = f->tool_bar_items;
8352 GCPRO1 (old_tool_bar);
8353
8354 /* Build desired tool-bar items from keymaps. */
8355 BLOCK_INPUT;
8356 f->tool_bar_items
8357 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8358 UNBLOCK_INPUT;
8359
8360 /* Redisplay the tool-bar if we changed it. */
8361 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8362 w->update_mode_line = Qt;
8363
8364 UNGCPRO;
8365
8366 unbind_to (count, Qnil);
8367 set_buffer_internal_1 (prev);
8368 }
8369 }
8370 }
8371
8372
8373 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8374 F's desired tool-bar contents. F->tool_bar_items must have
8375 been set up previously by calling prepare_menu_bars. */
8376
8377 static void
8378 build_desired_tool_bar_string (f)
8379 struct frame *f;
8380 {
8381 int i, size, size_needed;
8382 struct gcpro gcpro1, gcpro2, gcpro3;
8383 Lisp_Object image, plist, props;
8384
8385 image = plist = props = Qnil;
8386 GCPRO3 (image, plist, props);
8387
8388 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8389 Otherwise, make a new string. */
8390
8391 /* The size of the string we might be able to reuse. */
8392 size = (STRINGP (f->desired_tool_bar_string)
8393 ? SCHARS (f->desired_tool_bar_string)
8394 : 0);
8395
8396 /* We need one space in the string for each image. */
8397 size_needed = f->n_tool_bar_items;
8398
8399 /* Reuse f->desired_tool_bar_string, if possible. */
8400 if (size < size_needed || NILP (f->desired_tool_bar_string))
8401 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8402 make_number (' '));
8403 else
8404 {
8405 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8406 Fremove_text_properties (make_number (0), make_number (size),
8407 props, f->desired_tool_bar_string);
8408 }
8409
8410 /* Put a `display' property on the string for the images to display,
8411 put a `menu_item' property on tool-bar items with a value that
8412 is the index of the item in F's tool-bar item vector. */
8413 for (i = 0; i < f->n_tool_bar_items; ++i)
8414 {
8415 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8416
8417 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8418 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8419 int hmargin, vmargin, relief, idx, end;
8420 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8421
8422 /* If image is a vector, choose the image according to the
8423 button state. */
8424 image = PROP (TOOL_BAR_ITEM_IMAGES);
8425 if (VECTORP (image))
8426 {
8427 if (enabled_p)
8428 idx = (selected_p
8429 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8430 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8431 else
8432 idx = (selected_p
8433 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8434 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8435
8436 xassert (ASIZE (image) >= idx);
8437 image = AREF (image, idx);
8438 }
8439 else
8440 idx = -1;
8441
8442 /* Ignore invalid image specifications. */
8443 if (!valid_image_p (image))
8444 continue;
8445
8446 /* Display the tool-bar button pressed, or depressed. */
8447 plist = Fcopy_sequence (XCDR (image));
8448
8449 /* Compute margin and relief to draw. */
8450 relief = (tool_bar_button_relief >= 0
8451 ? tool_bar_button_relief
8452 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8453 hmargin = vmargin = relief;
8454
8455 if (INTEGERP (Vtool_bar_button_margin)
8456 && XINT (Vtool_bar_button_margin) > 0)
8457 {
8458 hmargin += XFASTINT (Vtool_bar_button_margin);
8459 vmargin += XFASTINT (Vtool_bar_button_margin);
8460 }
8461 else if (CONSP (Vtool_bar_button_margin))
8462 {
8463 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8464 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8465 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8466
8467 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8468 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8469 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8470 }
8471
8472 if (auto_raise_tool_bar_buttons_p)
8473 {
8474 /* Add a `:relief' property to the image spec if the item is
8475 selected. */
8476 if (selected_p)
8477 {
8478 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8479 hmargin -= relief;
8480 vmargin -= relief;
8481 }
8482 }
8483 else
8484 {
8485 /* If image is selected, display it pressed, i.e. with a
8486 negative relief. If it's not selected, display it with a
8487 raised relief. */
8488 plist = Fplist_put (plist, QCrelief,
8489 (selected_p
8490 ? make_number (-relief)
8491 : make_number (relief)));
8492 hmargin -= relief;
8493 vmargin -= relief;
8494 }
8495
8496 /* Put a margin around the image. */
8497 if (hmargin || vmargin)
8498 {
8499 if (hmargin == vmargin)
8500 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8501 else
8502 plist = Fplist_put (plist, QCmargin,
8503 Fcons (make_number (hmargin),
8504 make_number (vmargin)));
8505 }
8506
8507 /* If button is not enabled, and we don't have special images
8508 for the disabled state, make the image appear disabled by
8509 applying an appropriate algorithm to it. */
8510 if (!enabled_p && idx < 0)
8511 plist = Fplist_put (plist, QCconversion, Qdisabled);
8512
8513 /* Put a `display' text property on the string for the image to
8514 display. Put a `menu-item' property on the string that gives
8515 the start of this item's properties in the tool-bar items
8516 vector. */
8517 image = Fcons (Qimage, plist);
8518 props = list4 (Qdisplay, image,
8519 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8520
8521 /* Let the last image hide all remaining spaces in the tool bar
8522 string. The string can be longer than needed when we reuse a
8523 previous string. */
8524 if (i + 1 == f->n_tool_bar_items)
8525 end = SCHARS (f->desired_tool_bar_string);
8526 else
8527 end = i + 1;
8528 Fadd_text_properties (make_number (i), make_number (end),
8529 props, f->desired_tool_bar_string);
8530 #undef PROP
8531 }
8532
8533 UNGCPRO;
8534 }
8535
8536
8537 /* Display one line of the tool-bar of frame IT->f. */
8538
8539 static void
8540 display_tool_bar_line (it)
8541 struct it *it;
8542 {
8543 struct glyph_row *row = it->glyph_row;
8544 int max_x = it->last_visible_x;
8545 struct glyph *last;
8546
8547 prepare_desired_row (row);
8548 row->y = it->current_y;
8549
8550 /* Note that this isn't made use of if the face hasn't a box,
8551 so there's no need to check the face here. */
8552 it->start_of_box_run_p = 1;
8553
8554 while (it->current_x < max_x)
8555 {
8556 int x_before, x, n_glyphs_before, i, nglyphs;
8557
8558 /* Get the next display element. */
8559 if (!get_next_display_element (it))
8560 break;
8561
8562 /* Produce glyphs. */
8563 x_before = it->current_x;
8564 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8565 PRODUCE_GLYPHS (it);
8566
8567 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8568 i = 0;
8569 x = x_before;
8570 while (i < nglyphs)
8571 {
8572 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8573
8574 if (x + glyph->pixel_width > max_x)
8575 {
8576 /* Glyph doesn't fit on line. */
8577 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8578 it->current_x = x;
8579 goto out;
8580 }
8581
8582 ++it->hpos;
8583 x += glyph->pixel_width;
8584 ++i;
8585 }
8586
8587 /* Stop at line ends. */
8588 if (ITERATOR_AT_END_OF_LINE_P (it))
8589 break;
8590
8591 set_iterator_to_next (it, 1);
8592 }
8593
8594 out:;
8595
8596 row->displays_text_p = row->used[TEXT_AREA] != 0;
8597 extend_face_to_end_of_line (it);
8598 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8599 last->right_box_line_p = 1;
8600 if (last == row->glyphs[TEXT_AREA])
8601 last->left_box_line_p = 1;
8602 compute_line_metrics (it);
8603
8604 /* If line is empty, make it occupy the rest of the tool-bar. */
8605 if (!row->displays_text_p)
8606 {
8607 row->height = row->phys_height = it->last_visible_y - row->y;
8608 row->ascent = row->phys_ascent = 0;
8609 }
8610
8611 row->full_width_p = 1;
8612 row->continued_p = 0;
8613 row->truncated_on_left_p = 0;
8614 row->truncated_on_right_p = 0;
8615
8616 it->current_x = it->hpos = 0;
8617 it->current_y += row->height;
8618 ++it->vpos;
8619 ++it->glyph_row;
8620 }
8621
8622
8623 /* Value is the number of screen lines needed to make all tool-bar
8624 items of frame F visible. */
8625
8626 static int
8627 tool_bar_lines_needed (f)
8628 struct frame *f;
8629 {
8630 struct window *w = XWINDOW (f->tool_bar_window);
8631 struct it it;
8632
8633 /* Initialize an iterator for iteration over
8634 F->desired_tool_bar_string in the tool-bar window of frame F. */
8635 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8636 it.first_visible_x = 0;
8637 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8638 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8639
8640 while (!ITERATOR_AT_END_P (&it))
8641 {
8642 it.glyph_row = w->desired_matrix->rows;
8643 clear_glyph_row (it.glyph_row);
8644 display_tool_bar_line (&it);
8645 }
8646
8647 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8648 }
8649
8650
8651 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8652 0, 1, 0,
8653 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8654 (frame)
8655 Lisp_Object frame;
8656 {
8657 struct frame *f;
8658 struct window *w;
8659 int nlines = 0;
8660
8661 if (NILP (frame))
8662 frame = selected_frame;
8663 else
8664 CHECK_FRAME (frame);
8665 f = XFRAME (frame);
8666
8667 if (WINDOWP (f->tool_bar_window)
8668 || (w = XWINDOW (f->tool_bar_window),
8669 WINDOW_TOTAL_LINES (w) > 0))
8670 {
8671 update_tool_bar (f, 1);
8672 if (f->n_tool_bar_items)
8673 {
8674 build_desired_tool_bar_string (f);
8675 nlines = tool_bar_lines_needed (f);
8676 }
8677 }
8678
8679 return make_number (nlines);
8680 }
8681
8682
8683 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8684 height should be changed. */
8685
8686 static int
8687 redisplay_tool_bar (f)
8688 struct frame *f;
8689 {
8690 struct window *w;
8691 struct it it;
8692 struct glyph_row *row;
8693 int change_height_p = 0;
8694
8695 #ifdef USE_GTK
8696 if (FRAME_EXTERNAL_TOOL_BAR (f))
8697 update_frame_tool_bar (f);
8698 return 0;
8699 #endif
8700
8701 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8702 do anything. This means you must start with tool-bar-lines
8703 non-zero to get the auto-sizing effect. Or in other words, you
8704 can turn off tool-bars by specifying tool-bar-lines zero. */
8705 if (!WINDOWP (f->tool_bar_window)
8706 || (w = XWINDOW (f->tool_bar_window),
8707 WINDOW_TOTAL_LINES (w) == 0))
8708 return 0;
8709
8710 /* Set up an iterator for the tool-bar window. */
8711 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8712 it.first_visible_x = 0;
8713 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8714 row = it.glyph_row;
8715
8716 /* Build a string that represents the contents of the tool-bar. */
8717 build_desired_tool_bar_string (f);
8718 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8719
8720 /* Display as many lines as needed to display all tool-bar items. */
8721 while (it.current_y < it.last_visible_y)
8722 display_tool_bar_line (&it);
8723
8724 /* It doesn't make much sense to try scrolling in the tool-bar
8725 window, so don't do it. */
8726 w->desired_matrix->no_scrolling_p = 1;
8727 w->must_be_updated_p = 1;
8728
8729 if (auto_resize_tool_bars_p)
8730 {
8731 int nlines;
8732
8733 /* If we couldn't display everything, change the tool-bar's
8734 height. */
8735 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8736 change_height_p = 1;
8737
8738 /* If there are blank lines at the end, except for a partially
8739 visible blank line at the end that is smaller than
8740 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8741 row = it.glyph_row - 1;
8742 if (!row->displays_text_p
8743 && row->height >= FRAME_LINE_HEIGHT (f))
8744 change_height_p = 1;
8745
8746 /* If row displays tool-bar items, but is partially visible,
8747 change the tool-bar's height. */
8748 if (row->displays_text_p
8749 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8750 change_height_p = 1;
8751
8752 /* Resize windows as needed by changing the `tool-bar-lines'
8753 frame parameter. */
8754 if (change_height_p
8755 && (nlines = tool_bar_lines_needed (f),
8756 nlines != WINDOW_TOTAL_LINES (w)))
8757 {
8758 extern Lisp_Object Qtool_bar_lines;
8759 Lisp_Object frame;
8760 int old_height = WINDOW_TOTAL_LINES (w);
8761
8762 XSETFRAME (frame, f);
8763 clear_glyph_matrix (w->desired_matrix);
8764 Fmodify_frame_parameters (frame,
8765 Fcons (Fcons (Qtool_bar_lines,
8766 make_number (nlines)),
8767 Qnil));
8768 if (WINDOW_TOTAL_LINES (w) != old_height)
8769 fonts_changed_p = 1;
8770 }
8771 }
8772
8773 return change_height_p;
8774 }
8775
8776
8777 /* Get information about the tool-bar item which is displayed in GLYPH
8778 on frame F. Return in *PROP_IDX the index where tool-bar item
8779 properties start in F->tool_bar_items. Value is zero if
8780 GLYPH doesn't display a tool-bar item. */
8781
8782 static int
8783 tool_bar_item_info (f, glyph, prop_idx)
8784 struct frame *f;
8785 struct glyph *glyph;
8786 int *prop_idx;
8787 {
8788 Lisp_Object prop;
8789 int success_p;
8790 int charpos;
8791
8792 /* This function can be called asynchronously, which means we must
8793 exclude any possibility that Fget_text_property signals an
8794 error. */
8795 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8796 charpos = max (0, charpos);
8797
8798 /* Get the text property `menu-item' at pos. The value of that
8799 property is the start index of this item's properties in
8800 F->tool_bar_items. */
8801 prop = Fget_text_property (make_number (charpos),
8802 Qmenu_item, f->current_tool_bar_string);
8803 if (INTEGERP (prop))
8804 {
8805 *prop_idx = XINT (prop);
8806 success_p = 1;
8807 }
8808 else
8809 success_p = 0;
8810
8811 return success_p;
8812 }
8813
8814 \f
8815 /* Get information about the tool-bar item at position X/Y on frame F.
8816 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8817 the current matrix of the tool-bar window of F, or NULL if not
8818 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8819 item in F->tool_bar_items. Value is
8820
8821 -1 if X/Y is not on a tool-bar item
8822 0 if X/Y is on the same item that was highlighted before.
8823 1 otherwise. */
8824
8825 static int
8826 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8827 struct frame *f;
8828 int x, y;
8829 struct glyph **glyph;
8830 int *hpos, *vpos, *prop_idx;
8831 {
8832 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8833 struct window *w = XWINDOW (f->tool_bar_window);
8834 int area;
8835
8836 /* Find the glyph under X/Y. */
8837 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8838 if (*glyph == NULL)
8839 return -1;
8840
8841 /* Get the start of this tool-bar item's properties in
8842 f->tool_bar_items. */
8843 if (!tool_bar_item_info (f, *glyph, prop_idx))
8844 return -1;
8845
8846 /* Is mouse on the highlighted item? */
8847 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8848 && *vpos >= dpyinfo->mouse_face_beg_row
8849 && *vpos <= dpyinfo->mouse_face_end_row
8850 && (*vpos > dpyinfo->mouse_face_beg_row
8851 || *hpos >= dpyinfo->mouse_face_beg_col)
8852 && (*vpos < dpyinfo->mouse_face_end_row
8853 || *hpos < dpyinfo->mouse_face_end_col
8854 || dpyinfo->mouse_face_past_end))
8855 return 0;
8856
8857 return 1;
8858 }
8859
8860
8861 /* EXPORT:
8862 Handle mouse button event on the tool-bar of frame F, at
8863 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8864 0 for button release. MODIFIERS is event modifiers for button
8865 release. */
8866
8867 void
8868 handle_tool_bar_click (f, x, y, down_p, modifiers)
8869 struct frame *f;
8870 int x, y, down_p;
8871 unsigned int modifiers;
8872 {
8873 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8874 struct window *w = XWINDOW (f->tool_bar_window);
8875 int hpos, vpos, prop_idx;
8876 struct glyph *glyph;
8877 Lisp_Object enabled_p;
8878
8879 /* If not on the highlighted tool-bar item, return. */
8880 frame_to_window_pixel_xy (w, &x, &y);
8881 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8882 return;
8883
8884 /* If item is disabled, do nothing. */
8885 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8886 if (NILP (enabled_p))
8887 return;
8888
8889 if (down_p)
8890 {
8891 /* Show item in pressed state. */
8892 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8893 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8894 last_tool_bar_item = prop_idx;
8895 }
8896 else
8897 {
8898 Lisp_Object key, frame;
8899 struct input_event event;
8900 EVENT_INIT (event);
8901
8902 /* Show item in released state. */
8903 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8904 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8905
8906 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8907
8908 XSETFRAME (frame, f);
8909 event.kind = TOOL_BAR_EVENT;
8910 event.frame_or_window = frame;
8911 event.arg = frame;
8912 kbd_buffer_store_event (&event);
8913
8914 event.kind = TOOL_BAR_EVENT;
8915 event.frame_or_window = frame;
8916 event.arg = key;
8917 event.modifiers = modifiers;
8918 kbd_buffer_store_event (&event);
8919 last_tool_bar_item = -1;
8920 }
8921 }
8922
8923
8924 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8925 tool-bar window-relative coordinates X/Y. Called from
8926 note_mouse_highlight. */
8927
8928 static void
8929 note_tool_bar_highlight (f, x, y)
8930 struct frame *f;
8931 int x, y;
8932 {
8933 Lisp_Object window = f->tool_bar_window;
8934 struct window *w = XWINDOW (window);
8935 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8936 int hpos, vpos;
8937 struct glyph *glyph;
8938 struct glyph_row *row;
8939 int i;
8940 Lisp_Object enabled_p;
8941 int prop_idx;
8942 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8943 int mouse_down_p, rc;
8944
8945 /* Function note_mouse_highlight is called with negative x(y
8946 values when mouse moves outside of the frame. */
8947 if (x <= 0 || y <= 0)
8948 {
8949 clear_mouse_face (dpyinfo);
8950 return;
8951 }
8952
8953 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8954 if (rc < 0)
8955 {
8956 /* Not on tool-bar item. */
8957 clear_mouse_face (dpyinfo);
8958 return;
8959 }
8960 else if (rc == 0)
8961 /* On same tool-bar item as before. */
8962 goto set_help_echo;
8963
8964 clear_mouse_face (dpyinfo);
8965
8966 /* Mouse is down, but on different tool-bar item? */
8967 mouse_down_p = (dpyinfo->grabbed
8968 && f == last_mouse_frame
8969 && FRAME_LIVE_P (f));
8970 if (mouse_down_p
8971 && last_tool_bar_item != prop_idx)
8972 return;
8973
8974 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8975 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8976
8977 /* If tool-bar item is not enabled, don't highlight it. */
8978 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8979 if (!NILP (enabled_p))
8980 {
8981 /* Compute the x-position of the glyph. In front and past the
8982 image is a space. We include this in the highlighted area. */
8983 row = MATRIX_ROW (w->current_matrix, vpos);
8984 for (i = x = 0; i < hpos; ++i)
8985 x += row->glyphs[TEXT_AREA][i].pixel_width;
8986
8987 /* Record this as the current active region. */
8988 dpyinfo->mouse_face_beg_col = hpos;
8989 dpyinfo->mouse_face_beg_row = vpos;
8990 dpyinfo->mouse_face_beg_x = x;
8991 dpyinfo->mouse_face_beg_y = row->y;
8992 dpyinfo->mouse_face_past_end = 0;
8993
8994 dpyinfo->mouse_face_end_col = hpos + 1;
8995 dpyinfo->mouse_face_end_row = vpos;
8996 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8997 dpyinfo->mouse_face_end_y = row->y;
8998 dpyinfo->mouse_face_window = window;
8999 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9000
9001 /* Display it as active. */
9002 show_mouse_face (dpyinfo, draw);
9003 dpyinfo->mouse_face_image_state = draw;
9004 }
9005
9006 set_help_echo:
9007
9008 /* Set help_echo_string to a help string to display for this tool-bar item.
9009 XTread_socket does the rest. */
9010 help_echo_object = help_echo_window = Qnil;
9011 help_echo_pos = -1;
9012 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9013 if (NILP (help_echo_string))
9014 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9015 }
9016
9017 #endif /* HAVE_WINDOW_SYSTEM */
9018
9019
9020 \f
9021 /************************************************************************
9022 Horizontal scrolling
9023 ************************************************************************/
9024
9025 static int hscroll_window_tree P_ ((Lisp_Object));
9026 static int hscroll_windows P_ ((Lisp_Object));
9027
9028 /* For all leaf windows in the window tree rooted at WINDOW, set their
9029 hscroll value so that PT is (i) visible in the window, and (ii) so
9030 that it is not within a certain margin at the window's left and
9031 right border. Value is non-zero if any window's hscroll has been
9032 changed. */
9033
9034 static int
9035 hscroll_window_tree (window)
9036 Lisp_Object window;
9037 {
9038 int hscrolled_p = 0;
9039 int hscroll_relative_p = FLOATP (Vhscroll_step);
9040 int hscroll_step_abs = 0;
9041 double hscroll_step_rel = 0;
9042
9043 if (hscroll_relative_p)
9044 {
9045 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9046 if (hscroll_step_rel < 0)
9047 {
9048 hscroll_relative_p = 0;
9049 hscroll_step_abs = 0;
9050 }
9051 }
9052 else if (INTEGERP (Vhscroll_step))
9053 {
9054 hscroll_step_abs = XINT (Vhscroll_step);
9055 if (hscroll_step_abs < 0)
9056 hscroll_step_abs = 0;
9057 }
9058 else
9059 hscroll_step_abs = 0;
9060
9061 while (WINDOWP (window))
9062 {
9063 struct window *w = XWINDOW (window);
9064
9065 if (WINDOWP (w->hchild))
9066 hscrolled_p |= hscroll_window_tree (w->hchild);
9067 else if (WINDOWP (w->vchild))
9068 hscrolled_p |= hscroll_window_tree (w->vchild);
9069 else if (w->cursor.vpos >= 0)
9070 {
9071 int h_margin;
9072 int text_area_width;
9073 struct glyph_row *current_cursor_row
9074 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9075 struct glyph_row *desired_cursor_row
9076 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9077 struct glyph_row *cursor_row
9078 = (desired_cursor_row->enabled_p
9079 ? desired_cursor_row
9080 : current_cursor_row);
9081
9082 text_area_width = window_box_width (w, TEXT_AREA);
9083
9084 /* Scroll when cursor is inside this scroll margin. */
9085 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9086
9087 if ((XFASTINT (w->hscroll)
9088 && w->cursor.x <= h_margin)
9089 || (cursor_row->enabled_p
9090 && cursor_row->truncated_on_right_p
9091 && (w->cursor.x >= text_area_width - h_margin)))
9092 {
9093 struct it it;
9094 int hscroll;
9095 struct buffer *saved_current_buffer;
9096 int pt;
9097 int wanted_x;
9098
9099 /* Find point in a display of infinite width. */
9100 saved_current_buffer = current_buffer;
9101 current_buffer = XBUFFER (w->buffer);
9102
9103 if (w == XWINDOW (selected_window))
9104 pt = BUF_PT (current_buffer);
9105 else
9106 {
9107 pt = marker_position (w->pointm);
9108 pt = max (BEGV, pt);
9109 pt = min (ZV, pt);
9110 }
9111
9112 /* Move iterator to pt starting at cursor_row->start in
9113 a line with infinite width. */
9114 init_to_row_start (&it, w, cursor_row);
9115 it.last_visible_x = INFINITY;
9116 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9117 current_buffer = saved_current_buffer;
9118
9119 /* Position cursor in window. */
9120 if (!hscroll_relative_p && hscroll_step_abs == 0)
9121 hscroll = max (0, (it.current_x
9122 - (ITERATOR_AT_END_OF_LINE_P (&it)
9123 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9124 : (text_area_width / 2))))
9125 / FRAME_COLUMN_WIDTH (it.f);
9126 else if (w->cursor.x >= text_area_width - h_margin)
9127 {
9128 if (hscroll_relative_p)
9129 wanted_x = text_area_width * (1 - hscroll_step_rel)
9130 - h_margin;
9131 else
9132 wanted_x = text_area_width
9133 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9134 - h_margin;
9135 hscroll
9136 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9137 }
9138 else
9139 {
9140 if (hscroll_relative_p)
9141 wanted_x = text_area_width * hscroll_step_rel
9142 + h_margin;
9143 else
9144 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9145 + h_margin;
9146 hscroll
9147 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9148 }
9149 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9150
9151 /* Don't call Fset_window_hscroll if value hasn't
9152 changed because it will prevent redisplay
9153 optimizations. */
9154 if (XFASTINT (w->hscroll) != hscroll)
9155 {
9156 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9157 w->hscroll = make_number (hscroll);
9158 hscrolled_p = 1;
9159 }
9160 }
9161 }
9162
9163 window = w->next;
9164 }
9165
9166 /* Value is non-zero if hscroll of any leaf window has been changed. */
9167 return hscrolled_p;
9168 }
9169
9170
9171 /* Set hscroll so that cursor is visible and not inside horizontal
9172 scroll margins for all windows in the tree rooted at WINDOW. See
9173 also hscroll_window_tree above. Value is non-zero if any window's
9174 hscroll has been changed. If it has, desired matrices on the frame
9175 of WINDOW are cleared. */
9176
9177 static int
9178 hscroll_windows (window)
9179 Lisp_Object window;
9180 {
9181 int hscrolled_p;
9182
9183 if (automatic_hscrolling_p)
9184 {
9185 hscrolled_p = hscroll_window_tree (window);
9186 if (hscrolled_p)
9187 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9188 }
9189 else
9190 hscrolled_p = 0;
9191 return hscrolled_p;
9192 }
9193
9194
9195 \f
9196 /************************************************************************
9197 Redisplay
9198 ************************************************************************/
9199
9200 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9201 to a non-zero value. This is sometimes handy to have in a debugger
9202 session. */
9203
9204 #if GLYPH_DEBUG
9205
9206 /* First and last unchanged row for try_window_id. */
9207
9208 int debug_first_unchanged_at_end_vpos;
9209 int debug_last_unchanged_at_beg_vpos;
9210
9211 /* Delta vpos and y. */
9212
9213 int debug_dvpos, debug_dy;
9214
9215 /* Delta in characters and bytes for try_window_id. */
9216
9217 int debug_delta, debug_delta_bytes;
9218
9219 /* Values of window_end_pos and window_end_vpos at the end of
9220 try_window_id. */
9221
9222 EMACS_INT debug_end_pos, debug_end_vpos;
9223
9224 /* Append a string to W->desired_matrix->method. FMT is a printf
9225 format string. A1...A9 are a supplement for a variable-length
9226 argument list. If trace_redisplay_p is non-zero also printf the
9227 resulting string to stderr. */
9228
9229 static void
9230 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9231 struct window *w;
9232 char *fmt;
9233 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9234 {
9235 char buffer[512];
9236 char *method = w->desired_matrix->method;
9237 int len = strlen (method);
9238 int size = sizeof w->desired_matrix->method;
9239 int remaining = size - len - 1;
9240
9241 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9242 if (len && remaining)
9243 {
9244 method[len] = '|';
9245 --remaining, ++len;
9246 }
9247
9248 strncpy (method + len, buffer, remaining);
9249
9250 if (trace_redisplay_p)
9251 fprintf (stderr, "%p (%s): %s\n",
9252 w,
9253 ((BUFFERP (w->buffer)
9254 && STRINGP (XBUFFER (w->buffer)->name))
9255 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9256 : "no buffer"),
9257 buffer);
9258 }
9259
9260 #endif /* GLYPH_DEBUG */
9261
9262
9263 /* Value is non-zero if all changes in window W, which displays
9264 current_buffer, are in the text between START and END. START is a
9265 buffer position, END is given as a distance from Z. Used in
9266 redisplay_internal for display optimization. */
9267
9268 static INLINE int
9269 text_outside_line_unchanged_p (w, start, end)
9270 struct window *w;
9271 int start, end;
9272 {
9273 int unchanged_p = 1;
9274
9275 /* If text or overlays have changed, see where. */
9276 if (XFASTINT (w->last_modified) < MODIFF
9277 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9278 {
9279 /* Gap in the line? */
9280 if (GPT < start || Z - GPT < end)
9281 unchanged_p = 0;
9282
9283 /* Changes start in front of the line, or end after it? */
9284 if (unchanged_p
9285 && (BEG_UNCHANGED < start - 1
9286 || END_UNCHANGED < end))
9287 unchanged_p = 0;
9288
9289 /* If selective display, can't optimize if changes start at the
9290 beginning of the line. */
9291 if (unchanged_p
9292 && INTEGERP (current_buffer->selective_display)
9293 && XINT (current_buffer->selective_display) > 0
9294 && (BEG_UNCHANGED < start || GPT <= start))
9295 unchanged_p = 0;
9296
9297 /* If there are overlays at the start or end of the line, these
9298 may have overlay strings with newlines in them. A change at
9299 START, for instance, may actually concern the display of such
9300 overlay strings as well, and they are displayed on different
9301 lines. So, quickly rule out this case. (For the future, it
9302 might be desirable to implement something more telling than
9303 just BEG/END_UNCHANGED.) */
9304 if (unchanged_p)
9305 {
9306 if (BEG + BEG_UNCHANGED == start
9307 && overlay_touches_p (start))
9308 unchanged_p = 0;
9309 if (END_UNCHANGED == end
9310 && overlay_touches_p (Z - end))
9311 unchanged_p = 0;
9312 }
9313 }
9314
9315 return unchanged_p;
9316 }
9317
9318
9319 /* Do a frame update, taking possible shortcuts into account. This is
9320 the main external entry point for redisplay.
9321
9322 If the last redisplay displayed an echo area message and that message
9323 is no longer requested, we clear the echo area or bring back the
9324 mini-buffer if that is in use. */
9325
9326 void
9327 redisplay ()
9328 {
9329 redisplay_internal (0);
9330 }
9331
9332
9333 static Lisp_Object
9334 overlay_arrow_string_or_property (var, pbitmap)
9335 Lisp_Object var;
9336 int *pbitmap;
9337 {
9338 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9339 Lisp_Object bitmap;
9340
9341 if (pbitmap)
9342 {
9343 *pbitmap = 0;
9344 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9345 *pbitmap = XINT (bitmap);
9346 }
9347
9348 if (!NILP (pstr))
9349 return pstr;
9350 return Voverlay_arrow_string;
9351 }
9352
9353 /* Return 1 if there are any overlay-arrows in current_buffer. */
9354 static int
9355 overlay_arrow_in_current_buffer_p ()
9356 {
9357 Lisp_Object vlist;
9358
9359 for (vlist = Voverlay_arrow_variable_list;
9360 CONSP (vlist);
9361 vlist = XCDR (vlist))
9362 {
9363 Lisp_Object var = XCAR (vlist);
9364 Lisp_Object val;
9365
9366 if (!SYMBOLP (var))
9367 continue;
9368 val = find_symbol_value (var);
9369 if (MARKERP (val)
9370 && current_buffer == XMARKER (val)->buffer)
9371 return 1;
9372 }
9373 return 0;
9374 }
9375
9376
9377 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9378 has changed. */
9379
9380 static int
9381 overlay_arrows_changed_p ()
9382 {
9383 Lisp_Object vlist;
9384
9385 for (vlist = Voverlay_arrow_variable_list;
9386 CONSP (vlist);
9387 vlist = XCDR (vlist))
9388 {
9389 Lisp_Object var = XCAR (vlist);
9390 Lisp_Object val, pstr;
9391
9392 if (!SYMBOLP (var))
9393 continue;
9394 val = find_symbol_value (var);
9395 if (!MARKERP (val))
9396 continue;
9397 if (! EQ (COERCE_MARKER (val),
9398 Fget (var, Qlast_arrow_position))
9399 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9400 EQ (pstr, Fget (var, Qlast_arrow_string))))
9401 return 1;
9402 }
9403 return 0;
9404 }
9405
9406 /* Mark overlay arrows to be updated on next redisplay. */
9407
9408 static void
9409 update_overlay_arrows (up_to_date)
9410 int up_to_date;
9411 {
9412 Lisp_Object vlist;
9413
9414 for (vlist = Voverlay_arrow_variable_list;
9415 CONSP (vlist);
9416 vlist = XCDR (vlist))
9417 {
9418 Lisp_Object var = XCAR (vlist);
9419
9420 if (!SYMBOLP (var))
9421 continue;
9422
9423 if (up_to_date)
9424 {
9425 Lisp_Object val = find_symbol_value (var);
9426 Fput (var, Qlast_arrow_position,
9427 COERCE_MARKER (val));
9428 Fput (var, Qlast_arrow_string,
9429 overlay_arrow_string_or_property (var, 0));
9430 }
9431 else if (up_to_date < 0
9432 || !NILP (Fget (var, Qlast_arrow_position)))
9433 {
9434 Fput (var, Qlast_arrow_position, Qt);
9435 Fput (var, Qlast_arrow_string, Qt);
9436 }
9437 }
9438 }
9439
9440
9441 /* Return overlay arrow string at row, or nil. */
9442
9443 static Lisp_Object
9444 overlay_arrow_at_row (f, row, pbitmap)
9445 struct frame *f;
9446 struct glyph_row *row;
9447 int *pbitmap;
9448 {
9449 Lisp_Object vlist;
9450
9451 for (vlist = Voverlay_arrow_variable_list;
9452 CONSP (vlist);
9453 vlist = XCDR (vlist))
9454 {
9455 Lisp_Object var = XCAR (vlist);
9456 Lisp_Object val;
9457
9458 if (!SYMBOLP (var))
9459 continue;
9460
9461 val = find_symbol_value (var);
9462
9463 if (MARKERP (val)
9464 && current_buffer == XMARKER (val)->buffer
9465 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9466 {
9467 val = overlay_arrow_string_or_property (var, pbitmap);
9468 if (FRAME_WINDOW_P (f))
9469 return Qt;
9470 else if (STRINGP (val))
9471 return val;
9472 break;
9473 }
9474 }
9475
9476 *pbitmap = 0;
9477 return Qnil;
9478 }
9479
9480 /* Return 1 if point moved out of or into a composition. Otherwise
9481 return 0. PREV_BUF and PREV_PT are the last point buffer and
9482 position. BUF and PT are the current point buffer and position. */
9483
9484 int
9485 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9486 struct buffer *prev_buf, *buf;
9487 int prev_pt, pt;
9488 {
9489 int start, end;
9490 Lisp_Object prop;
9491 Lisp_Object buffer;
9492
9493 XSETBUFFER (buffer, buf);
9494 /* Check a composition at the last point if point moved within the
9495 same buffer. */
9496 if (prev_buf == buf)
9497 {
9498 if (prev_pt == pt)
9499 /* Point didn't move. */
9500 return 0;
9501
9502 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9503 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9504 && COMPOSITION_VALID_P (start, end, prop)
9505 && start < prev_pt && end > prev_pt)
9506 /* The last point was within the composition. Return 1 iff
9507 point moved out of the composition. */
9508 return (pt <= start || pt >= end);
9509 }
9510
9511 /* Check a composition at the current point. */
9512 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9513 && find_composition (pt, -1, &start, &end, &prop, buffer)
9514 && COMPOSITION_VALID_P (start, end, prop)
9515 && start < pt && end > pt);
9516 }
9517
9518
9519 /* Reconsider the setting of B->clip_changed which is displayed
9520 in window W. */
9521
9522 static INLINE void
9523 reconsider_clip_changes (w, b)
9524 struct window *w;
9525 struct buffer *b;
9526 {
9527 if (b->clip_changed
9528 && !NILP (w->window_end_valid)
9529 && w->current_matrix->buffer == b
9530 && w->current_matrix->zv == BUF_ZV (b)
9531 && w->current_matrix->begv == BUF_BEGV (b))
9532 b->clip_changed = 0;
9533
9534 /* If display wasn't paused, and W is not a tool bar window, see if
9535 point has been moved into or out of a composition. In that case,
9536 we set b->clip_changed to 1 to force updating the screen. If
9537 b->clip_changed has already been set to 1, we can skip this
9538 check. */
9539 if (!b->clip_changed
9540 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9541 {
9542 int pt;
9543
9544 if (w == XWINDOW (selected_window))
9545 pt = BUF_PT (current_buffer);
9546 else
9547 pt = marker_position (w->pointm);
9548
9549 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9550 || pt != XINT (w->last_point))
9551 && check_point_in_composition (w->current_matrix->buffer,
9552 XINT (w->last_point),
9553 XBUFFER (w->buffer), pt))
9554 b->clip_changed = 1;
9555 }
9556 }
9557 \f
9558
9559 /* Select FRAME to forward the values of frame-local variables into C
9560 variables so that the redisplay routines can access those values
9561 directly. */
9562
9563 static void
9564 select_frame_for_redisplay (frame)
9565 Lisp_Object frame;
9566 {
9567 Lisp_Object tail, sym, val;
9568 Lisp_Object old = selected_frame;
9569
9570 selected_frame = frame;
9571
9572 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9573 if (CONSP (XCAR (tail))
9574 && (sym = XCAR (XCAR (tail)),
9575 SYMBOLP (sym))
9576 && (sym = indirect_variable (sym),
9577 val = SYMBOL_VALUE (sym),
9578 (BUFFER_LOCAL_VALUEP (val)
9579 || SOME_BUFFER_LOCAL_VALUEP (val)))
9580 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9581 Fsymbol_value (sym);
9582
9583 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9584 if (CONSP (XCAR (tail))
9585 && (sym = XCAR (XCAR (tail)),
9586 SYMBOLP (sym))
9587 && (sym = indirect_variable (sym),
9588 val = SYMBOL_VALUE (sym),
9589 (BUFFER_LOCAL_VALUEP (val)
9590 || SOME_BUFFER_LOCAL_VALUEP (val)))
9591 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9592 Fsymbol_value (sym);
9593 }
9594
9595
9596 #define STOP_POLLING \
9597 do { if (! polling_stopped_here) stop_polling (); \
9598 polling_stopped_here = 1; } while (0)
9599
9600 #define RESUME_POLLING \
9601 do { if (polling_stopped_here) start_polling (); \
9602 polling_stopped_here = 0; } while (0)
9603
9604
9605 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9606 response to any user action; therefore, we should preserve the echo
9607 area. (Actually, our caller does that job.) Perhaps in the future
9608 avoid recentering windows if it is not necessary; currently that
9609 causes some problems. */
9610
9611 static void
9612 redisplay_internal (preserve_echo_area)
9613 int preserve_echo_area;
9614 {
9615 struct window *w = XWINDOW (selected_window);
9616 struct frame *f = XFRAME (w->frame);
9617 int pause;
9618 int must_finish = 0;
9619 struct text_pos tlbufpos, tlendpos;
9620 int number_of_visible_frames;
9621 int count;
9622 struct frame *sf = SELECTED_FRAME ();
9623 int polling_stopped_here = 0;
9624
9625 /* Non-zero means redisplay has to consider all windows on all
9626 frames. Zero means, only selected_window is considered. */
9627 int consider_all_windows_p;
9628
9629 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9630
9631 /* No redisplay if running in batch mode or frame is not yet fully
9632 initialized, or redisplay is explicitly turned off by setting
9633 Vinhibit_redisplay. */
9634 if (noninteractive
9635 || !NILP (Vinhibit_redisplay)
9636 || !f->glyphs_initialized_p)
9637 return;
9638
9639 /* The flag redisplay_performed_directly_p is set by
9640 direct_output_for_insert when it already did the whole screen
9641 update necessary. */
9642 if (redisplay_performed_directly_p)
9643 {
9644 redisplay_performed_directly_p = 0;
9645 if (!hscroll_windows (selected_window))
9646 return;
9647 }
9648
9649 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9650 if (popup_activated ())
9651 return;
9652 #endif
9653
9654 /* I don't think this happens but let's be paranoid. */
9655 if (redisplaying_p)
9656 return;
9657
9658 /* Record a function that resets redisplaying_p to its old value
9659 when we leave this function. */
9660 count = SPECPDL_INDEX ();
9661 record_unwind_protect (unwind_redisplay,
9662 Fcons (make_number (redisplaying_p), selected_frame));
9663 ++redisplaying_p;
9664 specbind (Qinhibit_free_realized_faces, Qnil);
9665
9666 retry:
9667 pause = 0;
9668 reconsider_clip_changes (w, current_buffer);
9669
9670 /* If new fonts have been loaded that make a glyph matrix adjustment
9671 necessary, do it. */
9672 if (fonts_changed_p)
9673 {
9674 adjust_glyphs (NULL);
9675 ++windows_or_buffers_changed;
9676 fonts_changed_p = 0;
9677 }
9678
9679 /* If face_change_count is non-zero, init_iterator will free all
9680 realized faces, which includes the faces referenced from current
9681 matrices. So, we can't reuse current matrices in this case. */
9682 if (face_change_count)
9683 ++windows_or_buffers_changed;
9684
9685 if (FRAME_TERMCAP_P (sf)
9686 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9687 {
9688 /* Since frames on a single ASCII terminal share the same
9689 display area, displaying a different frame means redisplay
9690 the whole thing. */
9691 windows_or_buffers_changed++;
9692 SET_FRAME_GARBAGED (sf);
9693 FRAME_TTY (sf)->previous_terminal_frame = sf;
9694 }
9695
9696 /* Set the visible flags for all frames. Do this before checking
9697 for resized or garbaged frames; they want to know if their frames
9698 are visible. See the comment in frame.h for
9699 FRAME_SAMPLE_VISIBILITY. */
9700 {
9701 Lisp_Object tail, frame;
9702
9703 number_of_visible_frames = 0;
9704
9705 FOR_EACH_FRAME (tail, frame)
9706 {
9707 struct frame *f = XFRAME (frame);
9708
9709 FRAME_SAMPLE_VISIBILITY (f);
9710 if (FRAME_VISIBLE_P (f))
9711 ++number_of_visible_frames;
9712 clear_desired_matrices (f);
9713 }
9714 }
9715
9716
9717 /* Notice any pending interrupt request to change frame size. */
9718 do_pending_window_change (1);
9719
9720 /* Clear frames marked as garbaged. */
9721 if (frame_garbaged)
9722 clear_garbaged_frames ();
9723
9724 /* Build menubar and tool-bar items. */
9725 prepare_menu_bars ();
9726
9727 if (windows_or_buffers_changed)
9728 update_mode_lines++;
9729
9730 /* Detect case that we need to write or remove a star in the mode line. */
9731 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9732 {
9733 w->update_mode_line = Qt;
9734 if (buffer_shared > 1)
9735 update_mode_lines++;
9736 }
9737
9738 /* If %c is in the mode line, update it if needed. */
9739 if (!NILP (w->column_number_displayed)
9740 /* This alternative quickly identifies a common case
9741 where no change is needed. */
9742 && !(PT == XFASTINT (w->last_point)
9743 && XFASTINT (w->last_modified) >= MODIFF
9744 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9745 && (XFASTINT (w->column_number_displayed)
9746 != (int) current_column ())) /* iftc */
9747 w->update_mode_line = Qt;
9748
9749 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9750
9751 /* The variable buffer_shared is set in redisplay_window and
9752 indicates that we redisplay a buffer in different windows. See
9753 there. */
9754 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9755 || cursor_type_changed);
9756
9757 /* If specs for an arrow have changed, do thorough redisplay
9758 to ensure we remove any arrow that should no longer exist. */
9759 if (overlay_arrows_changed_p ())
9760 consider_all_windows_p = windows_or_buffers_changed = 1;
9761
9762 /* Normally the message* functions will have already displayed and
9763 updated the echo area, but the frame may have been trashed, or
9764 the update may have been preempted, so display the echo area
9765 again here. Checking message_cleared_p captures the case that
9766 the echo area should be cleared. */
9767 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9768 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9769 || (message_cleared_p
9770 && minibuf_level == 0
9771 /* If the mini-window is currently selected, this means the
9772 echo-area doesn't show through. */
9773 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9774 {
9775 int window_height_changed_p = echo_area_display (0);
9776 must_finish = 1;
9777
9778 /* If we don't display the current message, don't clear the
9779 message_cleared_p flag, because, if we did, we wouldn't clear
9780 the echo area in the next redisplay which doesn't preserve
9781 the echo area. */
9782 if (!display_last_displayed_message_p)
9783 message_cleared_p = 0;
9784
9785 if (fonts_changed_p)
9786 goto retry;
9787 else if (window_height_changed_p)
9788 {
9789 consider_all_windows_p = 1;
9790 ++update_mode_lines;
9791 ++windows_or_buffers_changed;
9792
9793 /* If window configuration was changed, frames may have been
9794 marked garbaged. Clear them or we will experience
9795 surprises wrt scrolling. */
9796 if (frame_garbaged)
9797 clear_garbaged_frames ();
9798 }
9799 }
9800 else if (EQ (selected_window, minibuf_window)
9801 && (current_buffer->clip_changed
9802 || XFASTINT (w->last_modified) < MODIFF
9803 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9804 && resize_mini_window (w, 0))
9805 {
9806 /* Resized active mini-window to fit the size of what it is
9807 showing if its contents might have changed. */
9808 must_finish = 1;
9809 consider_all_windows_p = 1;
9810 ++windows_or_buffers_changed;
9811 ++update_mode_lines;
9812
9813 /* If window configuration was changed, frames may have been
9814 marked garbaged. Clear them or we will experience
9815 surprises wrt scrolling. */
9816 if (frame_garbaged)
9817 clear_garbaged_frames ();
9818 }
9819
9820
9821 /* If showing the region, and mark has changed, we must redisplay
9822 the whole window. The assignment to this_line_start_pos prevents
9823 the optimization directly below this if-statement. */
9824 if (((!NILP (Vtransient_mark_mode)
9825 && !NILP (XBUFFER (w->buffer)->mark_active))
9826 != !NILP (w->region_showing))
9827 || (!NILP (w->region_showing)
9828 && !EQ (w->region_showing,
9829 Fmarker_position (XBUFFER (w->buffer)->mark))))
9830 CHARPOS (this_line_start_pos) = 0;
9831
9832 /* Optimize the case that only the line containing the cursor in the
9833 selected window has changed. Variables starting with this_ are
9834 set in display_line and record information about the line
9835 containing the cursor. */
9836 tlbufpos = this_line_start_pos;
9837 tlendpos = this_line_end_pos;
9838 if (!consider_all_windows_p
9839 && CHARPOS (tlbufpos) > 0
9840 && NILP (w->update_mode_line)
9841 && !current_buffer->clip_changed
9842 && !current_buffer->prevent_redisplay_optimizations_p
9843 && FRAME_VISIBLE_P (XFRAME (w->frame))
9844 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9845 /* Make sure recorded data applies to current buffer, etc. */
9846 && this_line_buffer == current_buffer
9847 && current_buffer == XBUFFER (w->buffer)
9848 && NILP (w->force_start)
9849 && NILP (w->optional_new_start)
9850 /* Point must be on the line that we have info recorded about. */
9851 && PT >= CHARPOS (tlbufpos)
9852 && PT <= Z - CHARPOS (tlendpos)
9853 /* All text outside that line, including its final newline,
9854 must be unchanged */
9855 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9856 CHARPOS (tlendpos)))
9857 {
9858 if (CHARPOS (tlbufpos) > BEGV
9859 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9860 && (CHARPOS (tlbufpos) == ZV
9861 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9862 /* Former continuation line has disappeared by becoming empty */
9863 goto cancel;
9864 else if (XFASTINT (w->last_modified) < MODIFF
9865 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9866 || MINI_WINDOW_P (w))
9867 {
9868 /* We have to handle the case of continuation around a
9869 wide-column character (See the comment in indent.c around
9870 line 885).
9871
9872 For instance, in the following case:
9873
9874 -------- Insert --------
9875 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9876 J_I_ ==> J_I_ `^^' are cursors.
9877 ^^ ^^
9878 -------- --------
9879
9880 As we have to redraw the line above, we should goto cancel. */
9881
9882 struct it it;
9883 int line_height_before = this_line_pixel_height;
9884
9885 /* Note that start_display will handle the case that the
9886 line starting at tlbufpos is a continuation lines. */
9887 start_display (&it, w, tlbufpos);
9888
9889 /* Implementation note: It this still necessary? */
9890 if (it.current_x != this_line_start_x)
9891 goto cancel;
9892
9893 TRACE ((stderr, "trying display optimization 1\n"));
9894 w->cursor.vpos = -1;
9895 overlay_arrow_seen = 0;
9896 it.vpos = this_line_vpos;
9897 it.current_y = this_line_y;
9898 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9899 display_line (&it);
9900
9901 /* If line contains point, is not continued,
9902 and ends at same distance from eob as before, we win */
9903 if (w->cursor.vpos >= 0
9904 /* Line is not continued, otherwise this_line_start_pos
9905 would have been set to 0 in display_line. */
9906 && CHARPOS (this_line_start_pos)
9907 /* Line ends as before. */
9908 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9909 /* Line has same height as before. Otherwise other lines
9910 would have to be shifted up or down. */
9911 && this_line_pixel_height == line_height_before)
9912 {
9913 /* If this is not the window's last line, we must adjust
9914 the charstarts of the lines below. */
9915 if (it.current_y < it.last_visible_y)
9916 {
9917 struct glyph_row *row
9918 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9919 int delta, delta_bytes;
9920
9921 if (Z - CHARPOS (tlendpos) == ZV)
9922 {
9923 /* This line ends at end of (accessible part of)
9924 buffer. There is no newline to count. */
9925 delta = (Z
9926 - CHARPOS (tlendpos)
9927 - MATRIX_ROW_START_CHARPOS (row));
9928 delta_bytes = (Z_BYTE
9929 - BYTEPOS (tlendpos)
9930 - MATRIX_ROW_START_BYTEPOS (row));
9931 }
9932 else
9933 {
9934 /* This line ends in a newline. Must take
9935 account of the newline and the rest of the
9936 text that follows. */
9937 delta = (Z
9938 - CHARPOS (tlendpos)
9939 - MATRIX_ROW_START_CHARPOS (row));
9940 delta_bytes = (Z_BYTE
9941 - BYTEPOS (tlendpos)
9942 - MATRIX_ROW_START_BYTEPOS (row));
9943 }
9944
9945 increment_matrix_positions (w->current_matrix,
9946 this_line_vpos + 1,
9947 w->current_matrix->nrows,
9948 delta, delta_bytes);
9949 }
9950
9951 /* If this row displays text now but previously didn't,
9952 or vice versa, w->window_end_vpos may have to be
9953 adjusted. */
9954 if ((it.glyph_row - 1)->displays_text_p)
9955 {
9956 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9957 XSETINT (w->window_end_vpos, this_line_vpos);
9958 }
9959 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9960 && this_line_vpos > 0)
9961 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9962 w->window_end_valid = Qnil;
9963
9964 /* Update hint: No need to try to scroll in update_window. */
9965 w->desired_matrix->no_scrolling_p = 1;
9966
9967 #if GLYPH_DEBUG
9968 *w->desired_matrix->method = 0;
9969 debug_method_add (w, "optimization 1");
9970 #endif
9971 #ifdef HAVE_WINDOW_SYSTEM
9972 update_window_fringes (w, 0);
9973 #endif
9974 goto update;
9975 }
9976 else
9977 goto cancel;
9978 }
9979 else if (/* Cursor position hasn't changed. */
9980 PT == XFASTINT (w->last_point)
9981 /* Make sure the cursor was last displayed
9982 in this window. Otherwise we have to reposition it. */
9983 && 0 <= w->cursor.vpos
9984 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9985 {
9986 if (!must_finish)
9987 {
9988 do_pending_window_change (1);
9989
9990 /* We used to always goto end_of_redisplay here, but this
9991 isn't enough if we have a blinking cursor. */
9992 if (w->cursor_off_p == w->last_cursor_off_p)
9993 goto end_of_redisplay;
9994 }
9995 goto update;
9996 }
9997 /* If highlighting the region, or if the cursor is in the echo area,
9998 then we can't just move the cursor. */
9999 else if (! (!NILP (Vtransient_mark_mode)
10000 && !NILP (current_buffer->mark_active))
10001 && (EQ (selected_window, current_buffer->last_selected_window)
10002 || highlight_nonselected_windows)
10003 && NILP (w->region_showing)
10004 && NILP (Vshow_trailing_whitespace)
10005 && !cursor_in_echo_area)
10006 {
10007 struct it it;
10008 struct glyph_row *row;
10009
10010 /* Skip from tlbufpos to PT and see where it is. Note that
10011 PT may be in invisible text. If so, we will end at the
10012 next visible position. */
10013 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10014 NULL, DEFAULT_FACE_ID);
10015 it.current_x = this_line_start_x;
10016 it.current_y = this_line_y;
10017 it.vpos = this_line_vpos;
10018
10019 /* The call to move_it_to stops in front of PT, but
10020 moves over before-strings. */
10021 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10022
10023 if (it.vpos == this_line_vpos
10024 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10025 row->enabled_p))
10026 {
10027 xassert (this_line_vpos == it.vpos);
10028 xassert (this_line_y == it.current_y);
10029 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10030 #if GLYPH_DEBUG
10031 *w->desired_matrix->method = 0;
10032 debug_method_add (w, "optimization 3");
10033 #endif
10034 goto update;
10035 }
10036 else
10037 goto cancel;
10038 }
10039
10040 cancel:
10041 /* Text changed drastically or point moved off of line. */
10042 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10043 }
10044
10045 CHARPOS (this_line_start_pos) = 0;
10046 consider_all_windows_p |= buffer_shared > 1;
10047 ++clear_face_cache_count;
10048
10049
10050 /* Build desired matrices, and update the display. If
10051 consider_all_windows_p is non-zero, do it for all windows on all
10052 frames. Otherwise do it for selected_window, only. */
10053
10054 if (consider_all_windows_p)
10055 {
10056 Lisp_Object tail, frame;
10057 int i, n = 0, size = 50;
10058 struct frame **updated
10059 = (struct frame **) alloca (size * sizeof *updated);
10060
10061 /* Clear the face cache eventually. */
10062 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10063 {
10064 clear_face_cache (0);
10065 clear_face_cache_count = 0;
10066 }
10067
10068 /* Recompute # windows showing selected buffer. This will be
10069 incremented each time such a window is displayed. */
10070 buffer_shared = 0;
10071
10072 FOR_EACH_FRAME (tail, frame)
10073 {
10074 struct frame *f = XFRAME (frame);
10075
10076 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10077 {
10078 if (! EQ (frame, selected_frame))
10079 /* Select the frame, for the sake of frame-local
10080 variables. */
10081 select_frame_for_redisplay (frame);
10082
10083 #ifdef HAVE_WINDOW_SYSTEM
10084 if (clear_face_cache_count % 50 == 0
10085 && FRAME_WINDOW_P (f))
10086 clear_image_cache (f, 0);
10087 #endif /* HAVE_WINDOW_SYSTEM */
10088
10089 /* Mark all the scroll bars to be removed; we'll redeem
10090 the ones we want when we redisplay their windows. */
10091 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10092 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10093
10094 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10095 redisplay_windows (FRAME_ROOT_WINDOW (f));
10096
10097 /* Any scroll bars which redisplay_windows should have
10098 nuked should now go away. */
10099 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10100 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10101
10102 /* If fonts changed, display again. */
10103 /* ??? rms: I suspect it is a mistake to jump all the way
10104 back to retry here. It should just retry this frame. */
10105 if (fonts_changed_p)
10106 goto retry;
10107
10108 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10109 {
10110 /* See if we have to hscroll. */
10111 if (hscroll_windows (f->root_window))
10112 goto retry;
10113
10114 /* Prevent various kinds of signals during display
10115 update. stdio is not robust about handling
10116 signals, which can cause an apparent I/O
10117 error. */
10118 if (interrupt_input)
10119 unrequest_sigio ();
10120 STOP_POLLING;
10121
10122 /* Update the display. */
10123 set_window_update_flags (XWINDOW (f->root_window), 1);
10124 pause |= update_frame (f, 0, 0);
10125 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10126 if (pause)
10127 break;
10128 #endif
10129
10130 if (n == size)
10131 {
10132 int nbytes = size * sizeof *updated;
10133 struct frame **p = (struct frame **) alloca (2 * nbytes);
10134 bcopy (updated, p, nbytes);
10135 size *= 2;
10136 }
10137
10138 updated[n++] = f;
10139 }
10140 }
10141 }
10142
10143 if (!pause)
10144 {
10145 /* Do the mark_window_display_accurate after all windows have
10146 been redisplayed because this call resets flags in buffers
10147 which are needed for proper redisplay. */
10148 for (i = 0; i < n; ++i)
10149 {
10150 struct frame *f = updated[i];
10151 mark_window_display_accurate (f->root_window, 1);
10152 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10153 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10154 }
10155 }
10156 }
10157 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10158 {
10159 Lisp_Object mini_window;
10160 struct frame *mini_frame;
10161
10162 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10163 /* Use list_of_error, not Qerror, so that
10164 we catch only errors and don't run the debugger. */
10165 internal_condition_case_1 (redisplay_window_1, selected_window,
10166 list_of_error,
10167 redisplay_window_error);
10168
10169 /* Compare desired and current matrices, perform output. */
10170
10171 update:
10172 /* If fonts changed, display again. */
10173 if (fonts_changed_p)
10174 goto retry;
10175
10176 /* Prevent various kinds of signals during display update.
10177 stdio is not robust about handling signals,
10178 which can cause an apparent I/O error. */
10179 if (interrupt_input)
10180 unrequest_sigio ();
10181 STOP_POLLING;
10182
10183 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10184 {
10185 if (hscroll_windows (selected_window))
10186 goto retry;
10187
10188 XWINDOW (selected_window)->must_be_updated_p = 1;
10189 pause = update_frame (sf, 0, 0);
10190 }
10191
10192 /* We may have called echo_area_display at the top of this
10193 function. If the echo area is on another frame, that may
10194 have put text on a frame other than the selected one, so the
10195 above call to update_frame would not have caught it. Catch
10196 it here. */
10197 mini_window = FRAME_MINIBUF_WINDOW (sf);
10198 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10199
10200 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10201 {
10202 XWINDOW (mini_window)->must_be_updated_p = 1;
10203 pause |= update_frame (mini_frame, 0, 0);
10204 if (!pause && hscroll_windows (mini_window))
10205 goto retry;
10206 }
10207 }
10208
10209 /* If display was paused because of pending input, make sure we do a
10210 thorough update the next time. */
10211 if (pause)
10212 {
10213 /* Prevent the optimization at the beginning of
10214 redisplay_internal that tries a single-line update of the
10215 line containing the cursor in the selected window. */
10216 CHARPOS (this_line_start_pos) = 0;
10217
10218 /* Let the overlay arrow be updated the next time. */
10219 update_overlay_arrows (0);
10220
10221 /* If we pause after scrolling, some rows in the current
10222 matrices of some windows are not valid. */
10223 if (!WINDOW_FULL_WIDTH_P (w)
10224 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10225 update_mode_lines = 1;
10226 }
10227 else
10228 {
10229 if (!consider_all_windows_p)
10230 {
10231 /* This has already been done above if
10232 consider_all_windows_p is set. */
10233 mark_window_display_accurate_1 (w, 1);
10234
10235 /* Say overlay arrows are up to date. */
10236 update_overlay_arrows (1);
10237
10238 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10239 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10240 }
10241
10242 update_mode_lines = 0;
10243 windows_or_buffers_changed = 0;
10244 cursor_type_changed = 0;
10245 }
10246
10247 /* Start SIGIO interrupts coming again. Having them off during the
10248 code above makes it less likely one will discard output, but not
10249 impossible, since there might be stuff in the system buffer here.
10250 But it is much hairier to try to do anything about that. */
10251 if (interrupt_input)
10252 request_sigio ();
10253 RESUME_POLLING;
10254
10255 /* If a frame has become visible which was not before, redisplay
10256 again, so that we display it. Expose events for such a frame
10257 (which it gets when becoming visible) don't call the parts of
10258 redisplay constructing glyphs, so simply exposing a frame won't
10259 display anything in this case. So, we have to display these
10260 frames here explicitly. */
10261 if (!pause)
10262 {
10263 Lisp_Object tail, frame;
10264 int new_count = 0;
10265
10266 FOR_EACH_FRAME (tail, frame)
10267 {
10268 int this_is_visible = 0;
10269
10270 if (XFRAME (frame)->visible)
10271 this_is_visible = 1;
10272 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10273 if (XFRAME (frame)->visible)
10274 this_is_visible = 1;
10275
10276 if (this_is_visible)
10277 new_count++;
10278 }
10279
10280 if (new_count != number_of_visible_frames)
10281 windows_or_buffers_changed++;
10282 }
10283
10284 /* Change frame size now if a change is pending. */
10285 do_pending_window_change (1);
10286
10287 /* If we just did a pending size change, or have additional
10288 visible frames, redisplay again. */
10289 if (windows_or_buffers_changed && !pause)
10290 goto retry;
10291
10292 end_of_redisplay:
10293 unbind_to (count, Qnil);
10294 RESUME_POLLING;
10295 }
10296
10297
10298 /* Redisplay, but leave alone any recent echo area message unless
10299 another message has been requested in its place.
10300
10301 This is useful in situations where you need to redisplay but no
10302 user action has occurred, making it inappropriate for the message
10303 area to be cleared. See tracking_off and
10304 wait_reading_process_input for examples of these situations.
10305
10306 FROM_WHERE is an integer saying from where this function was
10307 called. This is useful for debugging. */
10308
10309 void
10310 redisplay_preserve_echo_area (from_where)
10311 int from_where;
10312 {
10313 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10314
10315 if (!NILP (echo_area_buffer[1]))
10316 {
10317 /* We have a previously displayed message, but no current
10318 message. Redisplay the previous message. */
10319 display_last_displayed_message_p = 1;
10320 redisplay_internal (1);
10321 display_last_displayed_message_p = 0;
10322 }
10323 else
10324 redisplay_internal (1);
10325 }
10326
10327
10328 /* Function registered with record_unwind_protect in
10329 redisplay_internal. Reset redisplaying_p to the value it had
10330 before redisplay_internal was called, and clear
10331 prevent_freeing_realized_faces_p. It also selects the previously
10332 selected frame. */
10333
10334 static Lisp_Object
10335 unwind_redisplay (val)
10336 Lisp_Object val;
10337 {
10338 Lisp_Object old_redisplaying_p, old_frame;
10339
10340 old_redisplaying_p = XCAR (val);
10341 redisplaying_p = XFASTINT (old_redisplaying_p);
10342 old_frame = XCDR (val);
10343 if (! EQ (old_frame, selected_frame))
10344 select_frame_for_redisplay (old_frame);
10345 return Qnil;
10346 }
10347
10348
10349 /* Mark the display of window W as accurate or inaccurate. If
10350 ACCURATE_P is non-zero mark display of W as accurate. If
10351 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10352 redisplay_internal is called. */
10353
10354 static void
10355 mark_window_display_accurate_1 (w, accurate_p)
10356 struct window *w;
10357 int accurate_p;
10358 {
10359 if (BUFFERP (w->buffer))
10360 {
10361 struct buffer *b = XBUFFER (w->buffer);
10362
10363 w->last_modified
10364 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10365 w->last_overlay_modified
10366 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10367 w->last_had_star
10368 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10369
10370 if (accurate_p)
10371 {
10372 b->clip_changed = 0;
10373 b->prevent_redisplay_optimizations_p = 0;
10374
10375 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10376 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10377 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10378 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10379
10380 w->current_matrix->buffer = b;
10381 w->current_matrix->begv = BUF_BEGV (b);
10382 w->current_matrix->zv = BUF_ZV (b);
10383
10384 w->last_cursor = w->cursor;
10385 w->last_cursor_off_p = w->cursor_off_p;
10386
10387 if (w == XWINDOW (selected_window))
10388 w->last_point = make_number (BUF_PT (b));
10389 else
10390 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10391 }
10392 }
10393
10394 if (accurate_p)
10395 {
10396 w->window_end_valid = w->buffer;
10397 #if 0 /* This is incorrect with variable-height lines. */
10398 xassert (XINT (w->window_end_vpos)
10399 < (WINDOW_TOTAL_LINES (w)
10400 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10401 #endif
10402 w->update_mode_line = Qnil;
10403 }
10404 }
10405
10406
10407 /* Mark the display of windows in the window tree rooted at WINDOW as
10408 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10409 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10410 be redisplayed the next time redisplay_internal is called. */
10411
10412 void
10413 mark_window_display_accurate (window, accurate_p)
10414 Lisp_Object window;
10415 int accurate_p;
10416 {
10417 struct window *w;
10418
10419 for (; !NILP (window); window = w->next)
10420 {
10421 w = XWINDOW (window);
10422 mark_window_display_accurate_1 (w, accurate_p);
10423
10424 if (!NILP (w->vchild))
10425 mark_window_display_accurate (w->vchild, accurate_p);
10426 if (!NILP (w->hchild))
10427 mark_window_display_accurate (w->hchild, accurate_p);
10428 }
10429
10430 if (accurate_p)
10431 {
10432 update_overlay_arrows (1);
10433 }
10434 else
10435 {
10436 /* Force a thorough redisplay the next time by setting
10437 last_arrow_position and last_arrow_string to t, which is
10438 unequal to any useful value of Voverlay_arrow_... */
10439 update_overlay_arrows (-1);
10440 }
10441 }
10442
10443
10444 /* Return value in display table DP (Lisp_Char_Table *) for character
10445 C. Since a display table doesn't have any parent, we don't have to
10446 follow parent. Do not call this function directly but use the
10447 macro DISP_CHAR_VECTOR. */
10448
10449 Lisp_Object
10450 disp_char_vector (dp, c)
10451 struct Lisp_Char_Table *dp;
10452 int c;
10453 {
10454 int code[4], i;
10455 Lisp_Object val;
10456
10457 if (SINGLE_BYTE_CHAR_P (c))
10458 return (dp->contents[c]);
10459
10460 SPLIT_CHAR (c, code[0], code[1], code[2]);
10461 if (code[1] < 32)
10462 code[1] = -1;
10463 else if (code[2] < 32)
10464 code[2] = -1;
10465
10466 /* Here, the possible range of code[0] (== charset ID) is
10467 128..max_charset. Since the top level char table contains data
10468 for multibyte characters after 256th element, we must increment
10469 code[0] by 128 to get a correct index. */
10470 code[0] += 128;
10471 code[3] = -1; /* anchor */
10472
10473 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10474 {
10475 val = dp->contents[code[i]];
10476 if (!SUB_CHAR_TABLE_P (val))
10477 return (NILP (val) ? dp->defalt : val);
10478 }
10479
10480 /* Here, val is a sub char table. We return the default value of
10481 it. */
10482 return (dp->defalt);
10483 }
10484
10485
10486 \f
10487 /***********************************************************************
10488 Window Redisplay
10489 ***********************************************************************/
10490
10491 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10492
10493 static void
10494 redisplay_windows (window)
10495 Lisp_Object window;
10496 {
10497 while (!NILP (window))
10498 {
10499 struct window *w = XWINDOW (window);
10500
10501 if (!NILP (w->hchild))
10502 redisplay_windows (w->hchild);
10503 else if (!NILP (w->vchild))
10504 redisplay_windows (w->vchild);
10505 else
10506 {
10507 displayed_buffer = XBUFFER (w->buffer);
10508 /* Use list_of_error, not Qerror, so that
10509 we catch only errors and don't run the debugger. */
10510 internal_condition_case_1 (redisplay_window_0, window,
10511 list_of_error,
10512 redisplay_window_error);
10513 }
10514
10515 window = w->next;
10516 }
10517 }
10518
10519 static Lisp_Object
10520 redisplay_window_error ()
10521 {
10522 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10523 return Qnil;
10524 }
10525
10526 static Lisp_Object
10527 redisplay_window_0 (window)
10528 Lisp_Object window;
10529 {
10530 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10531 redisplay_window (window, 0);
10532 return Qnil;
10533 }
10534
10535 static Lisp_Object
10536 redisplay_window_1 (window)
10537 Lisp_Object window;
10538 {
10539 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10540 redisplay_window (window, 1);
10541 return Qnil;
10542 }
10543 \f
10544
10545 /* Increment GLYPH until it reaches END or CONDITION fails while
10546 adding (GLYPH)->pixel_width to X. */
10547
10548 #define SKIP_GLYPHS(glyph, end, x, condition) \
10549 do \
10550 { \
10551 (x) += (glyph)->pixel_width; \
10552 ++(glyph); \
10553 } \
10554 while ((glyph) < (end) && (condition))
10555
10556
10557 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10558 DELTA is the number of bytes by which positions recorded in ROW
10559 differ from current buffer positions. */
10560
10561 void
10562 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10563 struct window *w;
10564 struct glyph_row *row;
10565 struct glyph_matrix *matrix;
10566 int delta, delta_bytes, dy, dvpos;
10567 {
10568 struct glyph *glyph = row->glyphs[TEXT_AREA];
10569 struct glyph *end = glyph + row->used[TEXT_AREA];
10570 /* The first glyph that starts a sequence of glyphs from string. */
10571 struct glyph *string_start;
10572 /* The X coordinate of string_start. */
10573 int string_start_x;
10574 /* The last known character position. */
10575 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10576 /* The last known character position before string_start. */
10577 int string_before_pos;
10578 int x = row->x;
10579 int pt_old = PT - delta;
10580
10581 /* Skip over glyphs not having an object at the start of the row.
10582 These are special glyphs like truncation marks on terminal
10583 frames. */
10584 if (row->displays_text_p)
10585 while (glyph < end
10586 && INTEGERP (glyph->object)
10587 && glyph->charpos < 0)
10588 {
10589 x += glyph->pixel_width;
10590 ++glyph;
10591 }
10592
10593 string_start = NULL;
10594 while (glyph < end
10595 && !INTEGERP (glyph->object)
10596 && (!BUFFERP (glyph->object)
10597 || (last_pos = glyph->charpos) < pt_old))
10598 {
10599 if (! STRINGP (glyph->object))
10600 {
10601 string_start = NULL;
10602 x += glyph->pixel_width;
10603 ++glyph;
10604 }
10605 else
10606 {
10607 string_before_pos = last_pos;
10608 string_start = glyph;
10609 string_start_x = x;
10610 /* Skip all glyphs from string. */
10611 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10612 }
10613 }
10614
10615 if (string_start
10616 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10617 {
10618 /* We may have skipped over point because the previous glyphs
10619 are from string. As there's no easy way to know the
10620 character position of the current glyph, find the correct
10621 glyph on point by scanning from string_start again. */
10622 Lisp_Object limit;
10623 Lisp_Object string;
10624 int pos;
10625
10626 limit = make_number (pt_old + 1);
10627 end = glyph;
10628 glyph = string_start;
10629 x = string_start_x;
10630 string = glyph->object;
10631 pos = string_buffer_position (w, string, string_before_pos);
10632 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10633 because we always put cursor after overlay strings. */
10634 while (pos == 0 && glyph < end)
10635 {
10636 string = glyph->object;
10637 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10638 if (glyph < end)
10639 pos = string_buffer_position (w, glyph->object, string_before_pos);
10640 }
10641
10642 while (glyph < end)
10643 {
10644 pos = XINT (Fnext_single_char_property_change
10645 (make_number (pos), Qdisplay, Qnil, limit));
10646 if (pos > pt_old)
10647 break;
10648 /* Skip glyphs from the same string. */
10649 string = glyph->object;
10650 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10651 /* Skip glyphs from an overlay. */
10652 while (glyph < end
10653 && ! string_buffer_position (w, glyph->object, pos))
10654 {
10655 string = glyph->object;
10656 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10657 }
10658 }
10659 }
10660
10661 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10662 w->cursor.x = x;
10663 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10664 w->cursor.y = row->y + dy;
10665
10666 if (w == XWINDOW (selected_window))
10667 {
10668 if (!row->continued_p
10669 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10670 && row->x == 0)
10671 {
10672 this_line_buffer = XBUFFER (w->buffer);
10673
10674 CHARPOS (this_line_start_pos)
10675 = MATRIX_ROW_START_CHARPOS (row) + delta;
10676 BYTEPOS (this_line_start_pos)
10677 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10678
10679 CHARPOS (this_line_end_pos)
10680 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10681 BYTEPOS (this_line_end_pos)
10682 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10683
10684 this_line_y = w->cursor.y;
10685 this_line_pixel_height = row->height;
10686 this_line_vpos = w->cursor.vpos;
10687 this_line_start_x = row->x;
10688 }
10689 else
10690 CHARPOS (this_line_start_pos) = 0;
10691 }
10692 }
10693
10694
10695 /* Run window scroll functions, if any, for WINDOW with new window
10696 start STARTP. Sets the window start of WINDOW to that position.
10697
10698 We assume that the window's buffer is really current. */
10699
10700 static INLINE struct text_pos
10701 run_window_scroll_functions (window, startp)
10702 Lisp_Object window;
10703 struct text_pos startp;
10704 {
10705 struct window *w = XWINDOW (window);
10706 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10707
10708 if (current_buffer != XBUFFER (w->buffer))
10709 abort ();
10710
10711 if (!NILP (Vwindow_scroll_functions))
10712 {
10713 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10714 make_number (CHARPOS (startp)));
10715 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10716 /* In case the hook functions switch buffers. */
10717 if (current_buffer != XBUFFER (w->buffer))
10718 set_buffer_internal_1 (XBUFFER (w->buffer));
10719 }
10720
10721 return startp;
10722 }
10723
10724
10725 /* Make sure the line containing the cursor is fully visible.
10726 A value of 1 means there is nothing to be done.
10727 (Either the line is fully visible, or it cannot be made so,
10728 or we cannot tell.)
10729 A value of 0 means the caller should do scrolling
10730 as if point had gone off the screen. */
10731
10732 static int
10733 make_cursor_line_fully_visible (w)
10734 struct window *w;
10735 {
10736 struct glyph_matrix *matrix;
10737 struct glyph_row *row;
10738 int window_height;
10739
10740 /* It's not always possible to find the cursor, e.g, when a window
10741 is full of overlay strings. Don't do anything in that case. */
10742 if (w->cursor.vpos < 0)
10743 return 1;
10744
10745 matrix = w->desired_matrix;
10746 row = MATRIX_ROW (matrix, w->cursor.vpos);
10747
10748 /* If the cursor row is not partially visible, there's nothing to do. */
10749 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10750 return 1;
10751
10752 /* If the row the cursor is in is taller than the window's height,
10753 it's not clear what to do, so do nothing. */
10754 window_height = window_box_height (w);
10755 if (row->height >= window_height)
10756 return 1;
10757
10758 return 0;
10759
10760 #if 0
10761 /* This code used to try to scroll the window just enough to make
10762 the line visible. It returned 0 to say that the caller should
10763 allocate larger glyph matrices. */
10764
10765 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10766 {
10767 int dy = row->height - row->visible_height;
10768 w->vscroll = 0;
10769 w->cursor.y += dy;
10770 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10771 }
10772 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10773 {
10774 int dy = - (row->height - row->visible_height);
10775 w->vscroll = dy;
10776 w->cursor.y += dy;
10777 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10778 }
10779
10780 /* When we change the cursor y-position of the selected window,
10781 change this_line_y as well so that the display optimization for
10782 the cursor line of the selected window in redisplay_internal uses
10783 the correct y-position. */
10784 if (w == XWINDOW (selected_window))
10785 this_line_y = w->cursor.y;
10786
10787 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10788 redisplay with larger matrices. */
10789 if (matrix->nrows < required_matrix_height (w))
10790 {
10791 fonts_changed_p = 1;
10792 return 0;
10793 }
10794
10795 return 1;
10796 #endif /* 0 */
10797 }
10798
10799
10800 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10801 non-zero means only WINDOW is redisplayed in redisplay_internal.
10802 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10803 in redisplay_window to bring a partially visible line into view in
10804 the case that only the cursor has moved.
10805
10806 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10807 last screen line's vertical height extends past the end of the screen.
10808
10809 Value is
10810
10811 1 if scrolling succeeded
10812
10813 0 if scrolling didn't find point.
10814
10815 -1 if new fonts have been loaded so that we must interrupt
10816 redisplay, adjust glyph matrices, and try again. */
10817
10818 enum
10819 {
10820 SCROLLING_SUCCESS,
10821 SCROLLING_FAILED,
10822 SCROLLING_NEED_LARGER_MATRICES
10823 };
10824
10825 static int
10826 try_scrolling (window, just_this_one_p, scroll_conservatively,
10827 scroll_step, temp_scroll_step, last_line_misfit)
10828 Lisp_Object window;
10829 int just_this_one_p;
10830 EMACS_INT scroll_conservatively, scroll_step;
10831 int temp_scroll_step;
10832 int last_line_misfit;
10833 {
10834 struct window *w = XWINDOW (window);
10835 struct frame *f = XFRAME (w->frame);
10836 struct text_pos scroll_margin_pos;
10837 struct text_pos pos;
10838 struct text_pos startp;
10839 struct it it;
10840 Lisp_Object window_end;
10841 int this_scroll_margin;
10842 int dy = 0;
10843 int scroll_max;
10844 int rc;
10845 int amount_to_scroll = 0;
10846 Lisp_Object aggressive;
10847 int height;
10848 int end_scroll_margin;
10849
10850 #if GLYPH_DEBUG
10851 debug_method_add (w, "try_scrolling");
10852 #endif
10853
10854 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10855
10856 /* Compute scroll margin height in pixels. We scroll when point is
10857 within this distance from the top or bottom of the window. */
10858 if (scroll_margin > 0)
10859 {
10860 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10861 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10862 }
10863 else
10864 this_scroll_margin = 0;
10865
10866 /* Compute how much we should try to scroll maximally to bring point
10867 into view. */
10868 if (scroll_step || scroll_conservatively || temp_scroll_step)
10869 scroll_max = max (scroll_step,
10870 max (scroll_conservatively, temp_scroll_step));
10871 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10872 || NUMBERP (current_buffer->scroll_up_aggressively))
10873 /* We're trying to scroll because of aggressive scrolling
10874 but no scroll_step is set. Choose an arbitrary one. Maybe
10875 there should be a variable for this. */
10876 scroll_max = 10;
10877 else
10878 scroll_max = 0;
10879 scroll_max *= FRAME_LINE_HEIGHT (f);
10880
10881 /* Decide whether we have to scroll down. Start at the window end
10882 and move this_scroll_margin up to find the position of the scroll
10883 margin. */
10884 window_end = Fwindow_end (window, Qt);
10885
10886 too_near_end:
10887
10888 CHARPOS (scroll_margin_pos) = XINT (window_end);
10889 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10890
10891 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10892 if (end_scroll_margin)
10893 {
10894 start_display (&it, w, scroll_margin_pos);
10895 move_it_vertically (&it, - end_scroll_margin);
10896 scroll_margin_pos = it.current.pos;
10897 }
10898
10899 if (PT >= CHARPOS (scroll_margin_pos))
10900 {
10901 int y0;
10902
10903 /* Point is in the scroll margin at the bottom of the window, or
10904 below. Compute a new window start that makes point visible. */
10905
10906 /* Compute the distance from the scroll margin to PT.
10907 Give up if the distance is greater than scroll_max. */
10908 start_display (&it, w, scroll_margin_pos);
10909 y0 = it.current_y;
10910 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10911 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10912
10913 /* To make point visible, we have to move the window start
10914 down so that the line the cursor is in is visible, which
10915 means we have to add in the height of the cursor line. */
10916 dy = line_bottom_y (&it) - y0;
10917
10918 if (dy > scroll_max)
10919 return SCROLLING_FAILED;
10920
10921 /* Move the window start down. If scrolling conservatively,
10922 move it just enough down to make point visible. If
10923 scroll_step is set, move it down by scroll_step. */
10924 start_display (&it, w, startp);
10925
10926 if (scroll_conservatively)
10927 /* Set AMOUNT_TO_SCROLL to at least one line,
10928 and at most scroll_conservatively lines. */
10929 amount_to_scroll
10930 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10931 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10932 else if (scroll_step || temp_scroll_step)
10933 amount_to_scroll = scroll_max;
10934 else
10935 {
10936 aggressive = current_buffer->scroll_up_aggressively;
10937 height = WINDOW_BOX_TEXT_HEIGHT (w);
10938 if (NUMBERP (aggressive))
10939 amount_to_scroll = XFLOATINT (aggressive) * height;
10940 }
10941
10942 if (amount_to_scroll <= 0)
10943 return SCROLLING_FAILED;
10944
10945 /* If moving by amount_to_scroll leaves STARTP unchanged,
10946 move it down one screen line. */
10947
10948 move_it_vertically (&it, amount_to_scroll);
10949 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10950 move_it_by_lines (&it, 1, 1);
10951 startp = it.current.pos;
10952 }
10953 else
10954 {
10955 /* See if point is inside the scroll margin at the top of the
10956 window. */
10957 scroll_margin_pos = startp;
10958 if (this_scroll_margin)
10959 {
10960 start_display (&it, w, startp);
10961 move_it_vertically (&it, this_scroll_margin);
10962 scroll_margin_pos = it.current.pos;
10963 }
10964
10965 if (PT < CHARPOS (scroll_margin_pos))
10966 {
10967 /* Point is in the scroll margin at the top of the window or
10968 above what is displayed in the window. */
10969 int y0;
10970
10971 /* Compute the vertical distance from PT to the scroll
10972 margin position. Give up if distance is greater than
10973 scroll_max. */
10974 SET_TEXT_POS (pos, PT, PT_BYTE);
10975 start_display (&it, w, pos);
10976 y0 = it.current_y;
10977 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10978 it.last_visible_y, -1,
10979 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10980 dy = it.current_y - y0;
10981 if (dy > scroll_max)
10982 return SCROLLING_FAILED;
10983
10984 /* Compute new window start. */
10985 start_display (&it, w, startp);
10986
10987 if (scroll_conservatively)
10988 amount_to_scroll =
10989 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
10990 else if (scroll_step || temp_scroll_step)
10991 amount_to_scroll = scroll_max;
10992 else
10993 {
10994 aggressive = current_buffer->scroll_down_aggressively;
10995 height = WINDOW_BOX_TEXT_HEIGHT (w);
10996 if (NUMBERP (aggressive))
10997 amount_to_scroll = XFLOATINT (aggressive) * height;
10998 }
10999
11000 if (amount_to_scroll <= 0)
11001 return SCROLLING_FAILED;
11002
11003 move_it_vertically (&it, - amount_to_scroll);
11004 startp = it.current.pos;
11005 }
11006 }
11007
11008 /* Run window scroll functions. */
11009 startp = run_window_scroll_functions (window, startp);
11010
11011 /* Display the window. Give up if new fonts are loaded, or if point
11012 doesn't appear. */
11013 if (!try_window (window, startp))
11014 rc = SCROLLING_NEED_LARGER_MATRICES;
11015 else if (w->cursor.vpos < 0)
11016 {
11017 clear_glyph_matrix (w->desired_matrix);
11018 rc = SCROLLING_FAILED;
11019 }
11020 else
11021 {
11022 /* Maybe forget recorded base line for line number display. */
11023 if (!just_this_one_p
11024 || current_buffer->clip_changed
11025 || BEG_UNCHANGED < CHARPOS (startp))
11026 w->base_line_number = Qnil;
11027
11028 /* If cursor ends up on a partially visible line,
11029 treat that as being off the bottom of the screen. */
11030 if (! make_cursor_line_fully_visible (w))
11031 {
11032 clear_glyph_matrix (w->desired_matrix);
11033 last_line_misfit = 1;
11034 goto too_near_end;
11035 }
11036 rc = SCROLLING_SUCCESS;
11037 }
11038
11039 return rc;
11040 }
11041
11042
11043 /* Compute a suitable window start for window W if display of W starts
11044 on a continuation line. Value is non-zero if a new window start
11045 was computed.
11046
11047 The new window start will be computed, based on W's width, starting
11048 from the start of the continued line. It is the start of the
11049 screen line with the minimum distance from the old start W->start. */
11050
11051 static int
11052 compute_window_start_on_continuation_line (w)
11053 struct window *w;
11054 {
11055 struct text_pos pos, start_pos;
11056 int window_start_changed_p = 0;
11057
11058 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11059
11060 /* If window start is on a continuation line... Window start may be
11061 < BEGV in case there's invisible text at the start of the
11062 buffer (M-x rmail, for example). */
11063 if (CHARPOS (start_pos) > BEGV
11064 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11065 {
11066 struct it it;
11067 struct glyph_row *row;
11068
11069 /* Handle the case that the window start is out of range. */
11070 if (CHARPOS (start_pos) < BEGV)
11071 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11072 else if (CHARPOS (start_pos) > ZV)
11073 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11074
11075 /* Find the start of the continued line. This should be fast
11076 because scan_buffer is fast (newline cache). */
11077 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11078 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11079 row, DEFAULT_FACE_ID);
11080 reseat_at_previous_visible_line_start (&it);
11081
11082 /* If the line start is "too far" away from the window start,
11083 say it takes too much time to compute a new window start. */
11084 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11085 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11086 {
11087 int min_distance, distance;
11088
11089 /* Move forward by display lines to find the new window
11090 start. If window width was enlarged, the new start can
11091 be expected to be > the old start. If window width was
11092 decreased, the new window start will be < the old start.
11093 So, we're looking for the display line start with the
11094 minimum distance from the old window start. */
11095 pos = it.current.pos;
11096 min_distance = INFINITY;
11097 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11098 distance < min_distance)
11099 {
11100 min_distance = distance;
11101 pos = it.current.pos;
11102 move_it_by_lines (&it, 1, 0);
11103 }
11104
11105 /* Set the window start there. */
11106 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11107 window_start_changed_p = 1;
11108 }
11109 }
11110
11111 return window_start_changed_p;
11112 }
11113
11114
11115 /* Try cursor movement in case text has not changed in window WINDOW,
11116 with window start STARTP. Value is
11117
11118 CURSOR_MOVEMENT_SUCCESS if successful
11119
11120 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11121
11122 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11123 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11124 we want to scroll as if scroll-step were set to 1. See the code.
11125
11126 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11127 which case we have to abort this redisplay, and adjust matrices
11128 first. */
11129
11130 enum
11131 {
11132 CURSOR_MOVEMENT_SUCCESS,
11133 CURSOR_MOVEMENT_CANNOT_BE_USED,
11134 CURSOR_MOVEMENT_MUST_SCROLL,
11135 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11136 };
11137
11138 static int
11139 try_cursor_movement (window, startp, scroll_step)
11140 Lisp_Object window;
11141 struct text_pos startp;
11142 int *scroll_step;
11143 {
11144 struct window *w = XWINDOW (window);
11145 struct frame *f = XFRAME (w->frame);
11146 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11147
11148 #if GLYPH_DEBUG
11149 if (inhibit_try_cursor_movement)
11150 return rc;
11151 #endif
11152
11153 /* Handle case where text has not changed, only point, and it has
11154 not moved off the frame. */
11155 if (/* Point may be in this window. */
11156 PT >= CHARPOS (startp)
11157 /* Selective display hasn't changed. */
11158 && !current_buffer->clip_changed
11159 /* Function force-mode-line-update is used to force a thorough
11160 redisplay. It sets either windows_or_buffers_changed or
11161 update_mode_lines. So don't take a shortcut here for these
11162 cases. */
11163 && !update_mode_lines
11164 && !windows_or_buffers_changed
11165 && !cursor_type_changed
11166 /* Can't use this case if highlighting a region. When a
11167 region exists, cursor movement has to do more than just
11168 set the cursor. */
11169 && !(!NILP (Vtransient_mark_mode)
11170 && !NILP (current_buffer->mark_active))
11171 && NILP (w->region_showing)
11172 && NILP (Vshow_trailing_whitespace)
11173 /* Right after splitting windows, last_point may be nil. */
11174 && INTEGERP (w->last_point)
11175 /* This code is not used for mini-buffer for the sake of the case
11176 of redisplaying to replace an echo area message; since in
11177 that case the mini-buffer contents per se are usually
11178 unchanged. This code is of no real use in the mini-buffer
11179 since the handling of this_line_start_pos, etc., in redisplay
11180 handles the same cases. */
11181 && !EQ (window, minibuf_window)
11182 /* When splitting windows or for new windows, it happens that
11183 redisplay is called with a nil window_end_vpos or one being
11184 larger than the window. This should really be fixed in
11185 window.c. I don't have this on my list, now, so we do
11186 approximately the same as the old redisplay code. --gerd. */
11187 && INTEGERP (w->window_end_vpos)
11188 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11189 && (FRAME_WINDOW_P (f)
11190 || !overlay_arrow_in_current_buffer_p ()))
11191 {
11192 int this_scroll_margin;
11193 struct glyph_row *row = NULL;
11194
11195 #if GLYPH_DEBUG
11196 debug_method_add (w, "cursor movement");
11197 #endif
11198
11199 /* Scroll if point within this distance from the top or bottom
11200 of the window. This is a pixel value. */
11201 this_scroll_margin = max (0, scroll_margin);
11202 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11203 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11204
11205 /* Start with the row the cursor was displayed during the last
11206 not paused redisplay. Give up if that row is not valid. */
11207 if (w->last_cursor.vpos < 0
11208 || w->last_cursor.vpos >= w->current_matrix->nrows)
11209 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11210 else
11211 {
11212 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11213 if (row->mode_line_p)
11214 ++row;
11215 if (!row->enabled_p)
11216 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11217 }
11218
11219 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11220 {
11221 int scroll_p = 0;
11222 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11223
11224 if (PT > XFASTINT (w->last_point))
11225 {
11226 /* Point has moved forward. */
11227 while (MATRIX_ROW_END_CHARPOS (row) < PT
11228 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11229 {
11230 xassert (row->enabled_p);
11231 ++row;
11232 }
11233
11234 /* The end position of a row equals the start position
11235 of the next row. If PT is there, we would rather
11236 display it in the next line. */
11237 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11238 && MATRIX_ROW_END_CHARPOS (row) == PT
11239 && !cursor_row_p (w, row))
11240 ++row;
11241
11242 /* If within the scroll margin, scroll. Note that
11243 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11244 the next line would be drawn, and that
11245 this_scroll_margin can be zero. */
11246 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11247 || PT > MATRIX_ROW_END_CHARPOS (row)
11248 /* Line is completely visible last line in window
11249 and PT is to be set in the next line. */
11250 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11251 && PT == MATRIX_ROW_END_CHARPOS (row)
11252 && !row->ends_at_zv_p
11253 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11254 scroll_p = 1;
11255 }
11256 else if (PT < XFASTINT (w->last_point))
11257 {
11258 /* Cursor has to be moved backward. Note that PT >=
11259 CHARPOS (startp) because of the outer
11260 if-statement. */
11261 while (!row->mode_line_p
11262 && (MATRIX_ROW_START_CHARPOS (row) > PT
11263 || (MATRIX_ROW_START_CHARPOS (row) == PT
11264 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11265 && (row->y > this_scroll_margin
11266 || CHARPOS (startp) == BEGV))
11267 {
11268 xassert (row->enabled_p);
11269 --row;
11270 }
11271
11272 /* Consider the following case: Window starts at BEGV,
11273 there is invisible, intangible text at BEGV, so that
11274 display starts at some point START > BEGV. It can
11275 happen that we are called with PT somewhere between
11276 BEGV and START. Try to handle that case. */
11277 if (row < w->current_matrix->rows
11278 || row->mode_line_p)
11279 {
11280 row = w->current_matrix->rows;
11281 if (row->mode_line_p)
11282 ++row;
11283 }
11284
11285 /* Due to newlines in overlay strings, we may have to
11286 skip forward over overlay strings. */
11287 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11288 && MATRIX_ROW_END_CHARPOS (row) == PT
11289 && !cursor_row_p (w, row))
11290 ++row;
11291
11292 /* If within the scroll margin, scroll. */
11293 if (row->y < this_scroll_margin
11294 && CHARPOS (startp) != BEGV)
11295 scroll_p = 1;
11296 }
11297
11298 if (PT < MATRIX_ROW_START_CHARPOS (row)
11299 || PT > MATRIX_ROW_END_CHARPOS (row))
11300 {
11301 /* if PT is not in the glyph row, give up. */
11302 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11303 }
11304 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11305 {
11306 if (PT == MATRIX_ROW_END_CHARPOS (row)
11307 && !row->ends_at_zv_p
11308 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11309 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11310 else if (row->height > window_box_height (w))
11311 {
11312 /* If we end up in a partially visible line, let's
11313 make it fully visible, except when it's taller
11314 than the window, in which case we can't do much
11315 about it. */
11316 *scroll_step = 1;
11317 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11318 }
11319 else
11320 {
11321 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11322 if (!make_cursor_line_fully_visible (w))
11323 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11324 else
11325 rc = CURSOR_MOVEMENT_SUCCESS;
11326 }
11327 }
11328 else if (scroll_p)
11329 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11330 else
11331 {
11332 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11333 rc = CURSOR_MOVEMENT_SUCCESS;
11334 }
11335 }
11336 }
11337
11338 return rc;
11339 }
11340
11341 void
11342 set_vertical_scroll_bar (w)
11343 struct window *w;
11344 {
11345 int start, end, whole;
11346
11347 /* Calculate the start and end positions for the current window.
11348 At some point, it would be nice to choose between scrollbars
11349 which reflect the whole buffer size, with special markers
11350 indicating narrowing, and scrollbars which reflect only the
11351 visible region.
11352
11353 Note that mini-buffers sometimes aren't displaying any text. */
11354 if (!MINI_WINDOW_P (w)
11355 || (w == XWINDOW (minibuf_window)
11356 && NILP (echo_area_buffer[0])))
11357 {
11358 struct buffer *buf = XBUFFER (w->buffer);
11359 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11360 start = marker_position (w->start) - BUF_BEGV (buf);
11361 /* I don't think this is guaranteed to be right. For the
11362 moment, we'll pretend it is. */
11363 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11364
11365 if (end < start)
11366 end = start;
11367 if (whole < (end - start))
11368 whole = end - start;
11369 }
11370 else
11371 start = end = whole = 0;
11372
11373 /* Indicate what this scroll bar ought to be displaying now. */
11374 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11375 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11376 (w, end - start, whole, start);
11377 }
11378
11379
11380 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11381 selected_window is redisplayed.
11382
11383 We can return without actually redisplaying the window if
11384 fonts_changed_p is nonzero. In that case, redisplay_internal will
11385 retry. */
11386
11387 static void
11388 redisplay_window (window, just_this_one_p)
11389 Lisp_Object window;
11390 int just_this_one_p;
11391 {
11392 struct window *w = XWINDOW (window);
11393 struct frame *f = XFRAME (w->frame);
11394 struct buffer *buffer = XBUFFER (w->buffer);
11395 struct buffer *old = current_buffer;
11396 struct text_pos lpoint, opoint, startp;
11397 int update_mode_line;
11398 int tem;
11399 struct it it;
11400 /* Record it now because it's overwritten. */
11401 int current_matrix_up_to_date_p = 0;
11402 int used_current_matrix_p = 0;
11403 /* This is less strict than current_matrix_up_to_date_p.
11404 It indictes that the buffer contents and narrowing are unchanged. */
11405 int buffer_unchanged_p = 0;
11406 int temp_scroll_step = 0;
11407 int count = SPECPDL_INDEX ();
11408 int rc;
11409 int centering_position;
11410 int last_line_misfit = 0;
11411
11412 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11413 opoint = lpoint;
11414
11415 /* W must be a leaf window here. */
11416 xassert (!NILP (w->buffer));
11417 #if GLYPH_DEBUG
11418 *w->desired_matrix->method = 0;
11419 #endif
11420
11421 specbind (Qinhibit_point_motion_hooks, Qt);
11422
11423 reconsider_clip_changes (w, buffer);
11424
11425 /* Has the mode line to be updated? */
11426 update_mode_line = (!NILP (w->update_mode_line)
11427 || update_mode_lines
11428 || buffer->clip_changed
11429 || buffer->prevent_redisplay_optimizations_p);
11430
11431 if (MINI_WINDOW_P (w))
11432 {
11433 if (w == XWINDOW (echo_area_window)
11434 && !NILP (echo_area_buffer[0]))
11435 {
11436 if (update_mode_line)
11437 /* We may have to update a tty frame's menu bar or a
11438 tool-bar. Example `M-x C-h C-h C-g'. */
11439 goto finish_menu_bars;
11440 else
11441 /* We've already displayed the echo area glyphs in this window. */
11442 goto finish_scroll_bars;
11443 }
11444 else if ((w != XWINDOW (minibuf_window)
11445 || minibuf_level == 0)
11446 /* When buffer is nonempty, redisplay window normally. */
11447 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11448 /* Quail displays non-mini buffers in minibuffer window.
11449 In that case, redisplay the window normally. */
11450 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11451 {
11452 /* W is a mini-buffer window, but it's not active, so clear
11453 it. */
11454 int yb = window_text_bottom_y (w);
11455 struct glyph_row *row;
11456 int y;
11457
11458 for (y = 0, row = w->desired_matrix->rows;
11459 y < yb;
11460 y += row->height, ++row)
11461 blank_row (w, row, y);
11462 goto finish_scroll_bars;
11463 }
11464
11465 clear_glyph_matrix (w->desired_matrix);
11466 }
11467
11468 /* Otherwise set up data on this window; select its buffer and point
11469 value. */
11470 /* Really select the buffer, for the sake of buffer-local
11471 variables. */
11472 set_buffer_internal_1 (XBUFFER (w->buffer));
11473 SET_TEXT_POS (opoint, PT, PT_BYTE);
11474
11475 current_matrix_up_to_date_p
11476 = (!NILP (w->window_end_valid)
11477 && !current_buffer->clip_changed
11478 && !current_buffer->prevent_redisplay_optimizations_p
11479 && XFASTINT (w->last_modified) >= MODIFF
11480 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11481
11482 buffer_unchanged_p
11483 = (!NILP (w->window_end_valid)
11484 && !current_buffer->clip_changed
11485 && XFASTINT (w->last_modified) >= MODIFF
11486 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11487
11488 /* When windows_or_buffers_changed is non-zero, we can't rely on
11489 the window end being valid, so set it to nil there. */
11490 if (windows_or_buffers_changed)
11491 {
11492 /* If window starts on a continuation line, maybe adjust the
11493 window start in case the window's width changed. */
11494 if (XMARKER (w->start)->buffer == current_buffer)
11495 compute_window_start_on_continuation_line (w);
11496
11497 w->window_end_valid = Qnil;
11498 }
11499
11500 /* Some sanity checks. */
11501 CHECK_WINDOW_END (w);
11502 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11503 abort ();
11504 if (BYTEPOS (opoint) < CHARPOS (opoint))
11505 abort ();
11506
11507 /* If %c is in mode line, update it if needed. */
11508 if (!NILP (w->column_number_displayed)
11509 /* This alternative quickly identifies a common case
11510 where no change is needed. */
11511 && !(PT == XFASTINT (w->last_point)
11512 && XFASTINT (w->last_modified) >= MODIFF
11513 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11514 && (XFASTINT (w->column_number_displayed)
11515 != (int) current_column ())) /* iftc */
11516 update_mode_line = 1;
11517
11518 /* Count number of windows showing the selected buffer. An indirect
11519 buffer counts as its base buffer. */
11520 if (!just_this_one_p)
11521 {
11522 struct buffer *current_base, *window_base;
11523 current_base = current_buffer;
11524 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11525 if (current_base->base_buffer)
11526 current_base = current_base->base_buffer;
11527 if (window_base->base_buffer)
11528 window_base = window_base->base_buffer;
11529 if (current_base == window_base)
11530 buffer_shared++;
11531 }
11532
11533 /* Point refers normally to the selected window. For any other
11534 window, set up appropriate value. */
11535 if (!EQ (window, selected_window))
11536 {
11537 int new_pt = XMARKER (w->pointm)->charpos;
11538 int new_pt_byte = marker_byte_position (w->pointm);
11539 if (new_pt < BEGV)
11540 {
11541 new_pt = BEGV;
11542 new_pt_byte = BEGV_BYTE;
11543 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11544 }
11545 else if (new_pt > (ZV - 1))
11546 {
11547 new_pt = ZV;
11548 new_pt_byte = ZV_BYTE;
11549 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11550 }
11551
11552 /* We don't use SET_PT so that the point-motion hooks don't run. */
11553 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11554 }
11555
11556 /* If any of the character widths specified in the display table
11557 have changed, invalidate the width run cache. It's true that
11558 this may be a bit late to catch such changes, but the rest of
11559 redisplay goes (non-fatally) haywire when the display table is
11560 changed, so why should we worry about doing any better? */
11561 if (current_buffer->width_run_cache)
11562 {
11563 struct Lisp_Char_Table *disptab = buffer_display_table ();
11564
11565 if (! disptab_matches_widthtab (disptab,
11566 XVECTOR (current_buffer->width_table)))
11567 {
11568 invalidate_region_cache (current_buffer,
11569 current_buffer->width_run_cache,
11570 BEG, Z);
11571 recompute_width_table (current_buffer, disptab);
11572 }
11573 }
11574
11575 /* If window-start is screwed up, choose a new one. */
11576 if (XMARKER (w->start)->buffer != current_buffer)
11577 goto recenter;
11578
11579 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11580
11581 /* If someone specified a new starting point but did not insist,
11582 check whether it can be used. */
11583 if (!NILP (w->optional_new_start)
11584 && CHARPOS (startp) >= BEGV
11585 && CHARPOS (startp) <= ZV)
11586 {
11587 w->optional_new_start = Qnil;
11588 start_display (&it, w, startp);
11589 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11590 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11591 if (IT_CHARPOS (it) == PT)
11592 w->force_start = Qt;
11593 /* IT may overshoot PT if text at PT is invisible. */
11594 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11595 w->force_start = Qt;
11596
11597
11598 }
11599
11600 /* Handle case where place to start displaying has been specified,
11601 unless the specified location is outside the accessible range. */
11602 if (!NILP (w->force_start)
11603 || w->frozen_window_start_p)
11604 {
11605 /* We set this later on if we have to adjust point. */
11606 int new_vpos = -1;
11607
11608 w->force_start = Qnil;
11609 w->vscroll = 0;
11610 w->window_end_valid = Qnil;
11611
11612 /* Forget any recorded base line for line number display. */
11613 if (!buffer_unchanged_p)
11614 w->base_line_number = Qnil;
11615
11616 /* Redisplay the mode line. Select the buffer properly for that.
11617 Also, run the hook window-scroll-functions
11618 because we have scrolled. */
11619 /* Note, we do this after clearing force_start because
11620 if there's an error, it is better to forget about force_start
11621 than to get into an infinite loop calling the hook functions
11622 and having them get more errors. */
11623 if (!update_mode_line
11624 || ! NILP (Vwindow_scroll_functions))
11625 {
11626 update_mode_line = 1;
11627 w->update_mode_line = Qt;
11628 startp = run_window_scroll_functions (window, startp);
11629 }
11630
11631 w->last_modified = make_number (0);
11632 w->last_overlay_modified = make_number (0);
11633 if (CHARPOS (startp) < BEGV)
11634 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11635 else if (CHARPOS (startp) > ZV)
11636 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11637
11638 /* Redisplay, then check if cursor has been set during the
11639 redisplay. Give up if new fonts were loaded. */
11640 if (!try_window (window, startp))
11641 {
11642 w->force_start = Qt;
11643 clear_glyph_matrix (w->desired_matrix);
11644 goto need_larger_matrices;
11645 }
11646
11647 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11648 {
11649 /* If point does not appear, try to move point so it does
11650 appear. The desired matrix has been built above, so we
11651 can use it here. */
11652 new_vpos = window_box_height (w) / 2;
11653 }
11654
11655 if (!make_cursor_line_fully_visible (w))
11656 {
11657 /* Point does appear, but on a line partly visible at end of window.
11658 Move it back to a fully-visible line. */
11659 new_vpos = window_box_height (w);
11660 }
11661
11662 /* If we need to move point for either of the above reasons,
11663 now actually do it. */
11664 if (new_vpos >= 0)
11665 {
11666 struct glyph_row *row;
11667
11668 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11669 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11670 ++row;
11671
11672 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11673 MATRIX_ROW_START_BYTEPOS (row));
11674
11675 if (w != XWINDOW (selected_window))
11676 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11677 else if (current_buffer == old)
11678 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11679
11680 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11681
11682 /* If we are highlighting the region, then we just changed
11683 the region, so redisplay to show it. */
11684 if (!NILP (Vtransient_mark_mode)
11685 && !NILP (current_buffer->mark_active))
11686 {
11687 clear_glyph_matrix (w->desired_matrix);
11688 if (!try_window (window, startp))
11689 goto need_larger_matrices;
11690 }
11691 }
11692
11693 #if GLYPH_DEBUG
11694 debug_method_add (w, "forced window start");
11695 #endif
11696 goto done;
11697 }
11698
11699 /* Handle case where text has not changed, only point, and it has
11700 not moved off the frame, and we are not retrying after hscroll.
11701 (current_matrix_up_to_date_p is nonzero when retrying.) */
11702 if (current_matrix_up_to_date_p
11703 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11704 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11705 {
11706 switch (rc)
11707 {
11708 case CURSOR_MOVEMENT_SUCCESS:
11709 used_current_matrix_p = 1;
11710 goto done;
11711
11712 #if 0 /* try_cursor_movement never returns this value. */
11713 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11714 goto need_larger_matrices;
11715 #endif
11716
11717 case CURSOR_MOVEMENT_MUST_SCROLL:
11718 goto try_to_scroll;
11719
11720 default:
11721 abort ();
11722 }
11723 }
11724 /* If current starting point was originally the beginning of a line
11725 but no longer is, find a new starting point. */
11726 else if (!NILP (w->start_at_line_beg)
11727 && !(CHARPOS (startp) <= BEGV
11728 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11729 {
11730 #if GLYPH_DEBUG
11731 debug_method_add (w, "recenter 1");
11732 #endif
11733 goto recenter;
11734 }
11735
11736 /* Try scrolling with try_window_id. Value is > 0 if update has
11737 been done, it is -1 if we know that the same window start will
11738 not work. It is 0 if unsuccessful for some other reason. */
11739 else if ((tem = try_window_id (w)) != 0)
11740 {
11741 #if GLYPH_DEBUG
11742 debug_method_add (w, "try_window_id %d", tem);
11743 #endif
11744
11745 if (fonts_changed_p)
11746 goto need_larger_matrices;
11747 if (tem > 0)
11748 goto done;
11749
11750 /* Otherwise try_window_id has returned -1 which means that we
11751 don't want the alternative below this comment to execute. */
11752 }
11753 else if (CHARPOS (startp) >= BEGV
11754 && CHARPOS (startp) <= ZV
11755 && PT >= CHARPOS (startp)
11756 && (CHARPOS (startp) < ZV
11757 /* Avoid starting at end of buffer. */
11758 || CHARPOS (startp) == BEGV
11759 || (XFASTINT (w->last_modified) >= MODIFF
11760 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11761 {
11762 #if GLYPH_DEBUG
11763 debug_method_add (w, "same window start");
11764 #endif
11765
11766 /* Try to redisplay starting at same place as before.
11767 If point has not moved off frame, accept the results. */
11768 if (!current_matrix_up_to_date_p
11769 /* Don't use try_window_reusing_current_matrix in this case
11770 because a window scroll function can have changed the
11771 buffer. */
11772 || !NILP (Vwindow_scroll_functions)
11773 || MINI_WINDOW_P (w)
11774 || !(used_current_matrix_p =
11775 try_window_reusing_current_matrix (w)))
11776 {
11777 IF_DEBUG (debug_method_add (w, "1"));
11778 try_window (window, startp);
11779 }
11780
11781 if (fonts_changed_p)
11782 goto need_larger_matrices;
11783
11784 if (w->cursor.vpos >= 0)
11785 {
11786 if (!just_this_one_p
11787 || current_buffer->clip_changed
11788 || BEG_UNCHANGED < CHARPOS (startp))
11789 /* Forget any recorded base line for line number display. */
11790 w->base_line_number = Qnil;
11791
11792 if (!make_cursor_line_fully_visible (w))
11793 {
11794 clear_glyph_matrix (w->desired_matrix);
11795 last_line_misfit = 1;
11796 }
11797 /* Drop through and scroll. */
11798 else
11799 goto done;
11800 }
11801 else
11802 clear_glyph_matrix (w->desired_matrix);
11803 }
11804
11805 try_to_scroll:
11806
11807 w->last_modified = make_number (0);
11808 w->last_overlay_modified = make_number (0);
11809
11810 /* Redisplay the mode line. Select the buffer properly for that. */
11811 if (!update_mode_line)
11812 {
11813 update_mode_line = 1;
11814 w->update_mode_line = Qt;
11815 }
11816
11817 /* Try to scroll by specified few lines. */
11818 if ((scroll_conservatively
11819 || scroll_step
11820 || temp_scroll_step
11821 || NUMBERP (current_buffer->scroll_up_aggressively)
11822 || NUMBERP (current_buffer->scroll_down_aggressively))
11823 && !current_buffer->clip_changed
11824 && CHARPOS (startp) >= BEGV
11825 && CHARPOS (startp) <= ZV)
11826 {
11827 /* The function returns -1 if new fonts were loaded, 1 if
11828 successful, 0 if not successful. */
11829 int rc = try_scrolling (window, just_this_one_p,
11830 scroll_conservatively,
11831 scroll_step,
11832 temp_scroll_step, last_line_misfit);
11833 switch (rc)
11834 {
11835 case SCROLLING_SUCCESS:
11836 goto done;
11837
11838 case SCROLLING_NEED_LARGER_MATRICES:
11839 goto need_larger_matrices;
11840
11841 case SCROLLING_FAILED:
11842 break;
11843
11844 default:
11845 abort ();
11846 }
11847 }
11848
11849 /* Finally, just choose place to start which centers point */
11850
11851 recenter:
11852 centering_position = window_box_height (w) / 2;
11853
11854 point_at_top:
11855 /* Jump here with centering_position already set to 0. */
11856
11857 #if GLYPH_DEBUG
11858 debug_method_add (w, "recenter");
11859 #endif
11860
11861 /* w->vscroll = 0; */
11862
11863 /* Forget any previously recorded base line for line number display. */
11864 if (!buffer_unchanged_p)
11865 w->base_line_number = Qnil;
11866
11867 /* Move backward half the height of the window. */
11868 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11869 it.current_y = it.last_visible_y;
11870 move_it_vertically_backward (&it, centering_position);
11871 xassert (IT_CHARPOS (it) >= BEGV);
11872
11873 /* The function move_it_vertically_backward may move over more
11874 than the specified y-distance. If it->w is small, e.g. a
11875 mini-buffer window, we may end up in front of the window's
11876 display area. Start displaying at the start of the line
11877 containing PT in this case. */
11878 if (it.current_y <= 0)
11879 {
11880 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11881 move_it_vertically (&it, 0);
11882 xassert (IT_CHARPOS (it) <= PT);
11883 it.current_y = 0;
11884 }
11885
11886 it.current_x = it.hpos = 0;
11887
11888 /* Set startp here explicitly in case that helps avoid an infinite loop
11889 in case the window-scroll-functions functions get errors. */
11890 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11891
11892 /* Run scroll hooks. */
11893 startp = run_window_scroll_functions (window, it.current.pos);
11894
11895 /* Redisplay the window. */
11896 if (!current_matrix_up_to_date_p
11897 || windows_or_buffers_changed
11898 || cursor_type_changed
11899 /* Don't use try_window_reusing_current_matrix in this case
11900 because it can have changed the buffer. */
11901 || !NILP (Vwindow_scroll_functions)
11902 || !just_this_one_p
11903 || MINI_WINDOW_P (w)
11904 || !(used_current_matrix_p =
11905 try_window_reusing_current_matrix (w)))
11906 try_window (window, startp);
11907
11908 /* If new fonts have been loaded (due to fontsets), give up. We
11909 have to start a new redisplay since we need to re-adjust glyph
11910 matrices. */
11911 if (fonts_changed_p)
11912 goto need_larger_matrices;
11913
11914 /* If cursor did not appear assume that the middle of the window is
11915 in the first line of the window. Do it again with the next line.
11916 (Imagine a window of height 100, displaying two lines of height
11917 60. Moving back 50 from it->last_visible_y will end in the first
11918 line.) */
11919 if (w->cursor.vpos < 0)
11920 {
11921 if (!NILP (w->window_end_valid)
11922 && PT >= Z - XFASTINT (w->window_end_pos))
11923 {
11924 clear_glyph_matrix (w->desired_matrix);
11925 move_it_by_lines (&it, 1, 0);
11926 try_window (window, it.current.pos);
11927 }
11928 else if (PT < IT_CHARPOS (it))
11929 {
11930 clear_glyph_matrix (w->desired_matrix);
11931 move_it_by_lines (&it, -1, 0);
11932 try_window (window, it.current.pos);
11933 }
11934 else
11935 {
11936 /* Not much we can do about it. */
11937 }
11938 }
11939
11940 /* Consider the following case: Window starts at BEGV, there is
11941 invisible, intangible text at BEGV, so that display starts at
11942 some point START > BEGV. It can happen that we are called with
11943 PT somewhere between BEGV and START. Try to handle that case. */
11944 if (w->cursor.vpos < 0)
11945 {
11946 struct glyph_row *row = w->current_matrix->rows;
11947 if (row->mode_line_p)
11948 ++row;
11949 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11950 }
11951
11952 if (!make_cursor_line_fully_visible (w))
11953 {
11954 /* If vscroll is enabled, disable it and try again. */
11955 if (w->vscroll)
11956 {
11957 w->vscroll = 0;
11958 clear_glyph_matrix (w->desired_matrix);
11959 goto recenter;
11960 }
11961
11962 /* If centering point failed to make the whole line visible,
11963 put point at the top instead. That has to make the whole line
11964 visible, if it can be done. */
11965 centering_position = 0;
11966 goto point_at_top;
11967 }
11968
11969 done:
11970
11971 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11972 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11973 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11974 ? Qt : Qnil);
11975
11976 /* Display the mode line, if we must. */
11977 if ((update_mode_line
11978 /* If window not full width, must redo its mode line
11979 if (a) the window to its side is being redone and
11980 (b) we do a frame-based redisplay. This is a consequence
11981 of how inverted lines are drawn in frame-based redisplay. */
11982 || (!just_this_one_p
11983 && !FRAME_WINDOW_P (f)
11984 && !WINDOW_FULL_WIDTH_P (w))
11985 /* Line number to display. */
11986 || INTEGERP (w->base_line_pos)
11987 /* Column number is displayed and different from the one displayed. */
11988 || (!NILP (w->column_number_displayed)
11989 && (XFASTINT (w->column_number_displayed)
11990 != (int) current_column ()))) /* iftc */
11991 /* This means that the window has a mode line. */
11992 && (WINDOW_WANTS_MODELINE_P (w)
11993 || WINDOW_WANTS_HEADER_LINE_P (w)))
11994 {
11995 display_mode_lines (w);
11996
11997 /* If mode line height has changed, arrange for a thorough
11998 immediate redisplay using the correct mode line height. */
11999 if (WINDOW_WANTS_MODELINE_P (w)
12000 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12001 {
12002 fonts_changed_p = 1;
12003 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12004 = DESIRED_MODE_LINE_HEIGHT (w);
12005 }
12006
12007 /* If top line height has changed, arrange for a thorough
12008 immediate redisplay using the correct mode line height. */
12009 if (WINDOW_WANTS_HEADER_LINE_P (w)
12010 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12011 {
12012 fonts_changed_p = 1;
12013 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12014 = DESIRED_HEADER_LINE_HEIGHT (w);
12015 }
12016
12017 if (fonts_changed_p)
12018 goto need_larger_matrices;
12019 }
12020
12021 if (!line_number_displayed
12022 && !BUFFERP (w->base_line_pos))
12023 {
12024 w->base_line_pos = Qnil;
12025 w->base_line_number = Qnil;
12026 }
12027
12028 finish_menu_bars:
12029
12030 /* When we reach a frame's selected window, redo the frame's menu bar. */
12031 if (update_mode_line
12032 && EQ (FRAME_SELECTED_WINDOW (f), window))
12033 {
12034 int redisplay_menu_p = 0;
12035 int redisplay_tool_bar_p = 0;
12036
12037 if (FRAME_WINDOW_P (f))
12038 {
12039 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12040 || defined (USE_GTK)
12041 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12042 #else
12043 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12044 #endif
12045 }
12046 else
12047 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12048
12049 if (redisplay_menu_p)
12050 display_menu_bar (w);
12051
12052 #ifdef HAVE_WINDOW_SYSTEM
12053 #ifdef USE_GTK
12054 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12055 #else
12056 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12057 && (FRAME_TOOL_BAR_LINES (f) > 0
12058 || auto_resize_tool_bars_p);
12059
12060 #endif
12061
12062 if (redisplay_tool_bar_p)
12063 redisplay_tool_bar (f);
12064 #endif
12065 }
12066
12067 #ifdef HAVE_WINDOW_SYSTEM
12068 if (update_window_fringes (w, 0)
12069 && !just_this_one_p
12070 && (used_current_matrix_p || overlay_arrow_seen)
12071 && !w->pseudo_window_p)
12072 {
12073 update_begin (f);
12074 BLOCK_INPUT;
12075 draw_window_fringes (w);
12076 UNBLOCK_INPUT;
12077 update_end (f);
12078 }
12079 #endif /* HAVE_WINDOW_SYSTEM */
12080
12081 /* We go to this label, with fonts_changed_p nonzero,
12082 if it is necessary to try again using larger glyph matrices.
12083 We have to redeem the scroll bar even in this case,
12084 because the loop in redisplay_internal expects that. */
12085 need_larger_matrices:
12086 ;
12087 finish_scroll_bars:
12088
12089 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12090 {
12091 /* Set the thumb's position and size. */
12092 set_vertical_scroll_bar (w);
12093
12094 /* Note that we actually used the scroll bar attached to this
12095 window, so it shouldn't be deleted at the end of redisplay. */
12096 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12097 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12098 }
12099
12100 /* Restore current_buffer and value of point in it. */
12101 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12102 set_buffer_internal_1 (old);
12103 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12104
12105 unbind_to (count, Qnil);
12106 }
12107
12108
12109 /* Build the complete desired matrix of WINDOW with a window start
12110 buffer position POS. Value is non-zero if successful. It is zero
12111 if fonts were loaded during redisplay which makes re-adjusting
12112 glyph matrices necessary. */
12113
12114 int
12115 try_window (window, pos)
12116 Lisp_Object window;
12117 struct text_pos pos;
12118 {
12119 struct window *w = XWINDOW (window);
12120 struct it it;
12121 struct glyph_row *last_text_row = NULL;
12122
12123 /* Make POS the new window start. */
12124 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12125
12126 /* Mark cursor position as unknown. No overlay arrow seen. */
12127 w->cursor.vpos = -1;
12128 overlay_arrow_seen = 0;
12129
12130 /* Initialize iterator and info to start at POS. */
12131 start_display (&it, w, pos);
12132
12133 /* Display all lines of W. */
12134 while (it.current_y < it.last_visible_y)
12135 {
12136 if (display_line (&it))
12137 last_text_row = it.glyph_row - 1;
12138 if (fonts_changed_p)
12139 return 0;
12140 }
12141
12142 /* If bottom moved off end of frame, change mode line percentage. */
12143 if (XFASTINT (w->window_end_pos) <= 0
12144 && Z != IT_CHARPOS (it))
12145 w->update_mode_line = Qt;
12146
12147 /* Set window_end_pos to the offset of the last character displayed
12148 on the window from the end of current_buffer. Set
12149 window_end_vpos to its row number. */
12150 if (last_text_row)
12151 {
12152 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12153 w->window_end_bytepos
12154 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12155 w->window_end_pos
12156 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12157 w->window_end_vpos
12158 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12159 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12160 ->displays_text_p);
12161 }
12162 else
12163 {
12164 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12165 w->window_end_pos = make_number (Z - ZV);
12166 w->window_end_vpos = make_number (0);
12167 }
12168
12169 /* But that is not valid info until redisplay finishes. */
12170 w->window_end_valid = Qnil;
12171 return 1;
12172 }
12173
12174
12175 \f
12176 /************************************************************************
12177 Window redisplay reusing current matrix when buffer has not changed
12178 ************************************************************************/
12179
12180 /* Try redisplay of window W showing an unchanged buffer with a
12181 different window start than the last time it was displayed by
12182 reusing its current matrix. Value is non-zero if successful.
12183 W->start is the new window start. */
12184
12185 static int
12186 try_window_reusing_current_matrix (w)
12187 struct window *w;
12188 {
12189 struct frame *f = XFRAME (w->frame);
12190 struct glyph_row *row, *bottom_row;
12191 struct it it;
12192 struct run run;
12193 struct text_pos start, new_start;
12194 int nrows_scrolled, i;
12195 struct glyph_row *last_text_row;
12196 struct glyph_row *last_reused_text_row;
12197 struct glyph_row *start_row;
12198 int start_vpos, min_y, max_y;
12199
12200 #if GLYPH_DEBUG
12201 if (inhibit_try_window_reusing)
12202 return 0;
12203 #endif
12204
12205 if (/* This function doesn't handle terminal frames. */
12206 !FRAME_WINDOW_P (f)
12207 /* Don't try to reuse the display if windows have been split
12208 or such. */
12209 || windows_or_buffers_changed
12210 || cursor_type_changed)
12211 return 0;
12212
12213 /* Can't do this if region may have changed. */
12214 if ((!NILP (Vtransient_mark_mode)
12215 && !NILP (current_buffer->mark_active))
12216 || !NILP (w->region_showing)
12217 || !NILP (Vshow_trailing_whitespace))
12218 return 0;
12219
12220 /* If top-line visibility has changed, give up. */
12221 if (WINDOW_WANTS_HEADER_LINE_P (w)
12222 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12223 return 0;
12224
12225 /* Give up if old or new display is scrolled vertically. We could
12226 make this function handle this, but right now it doesn't. */
12227 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12228 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12229 return 0;
12230
12231 /* The variable new_start now holds the new window start. The old
12232 start `start' can be determined from the current matrix. */
12233 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12234 start = start_row->start.pos;
12235 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12236
12237 /* Clear the desired matrix for the display below. */
12238 clear_glyph_matrix (w->desired_matrix);
12239
12240 if (CHARPOS (new_start) <= CHARPOS (start))
12241 {
12242 int first_row_y;
12243
12244 /* Don't use this method if the display starts with an ellipsis
12245 displayed for invisible text. It's not easy to handle that case
12246 below, and it's certainly not worth the effort since this is
12247 not a frequent case. */
12248 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12249 return 0;
12250
12251 IF_DEBUG (debug_method_add (w, "twu1"));
12252
12253 /* Display up to a row that can be reused. The variable
12254 last_text_row is set to the last row displayed that displays
12255 text. Note that it.vpos == 0 if or if not there is a
12256 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12257 start_display (&it, w, new_start);
12258 first_row_y = it.current_y;
12259 w->cursor.vpos = -1;
12260 last_text_row = last_reused_text_row = NULL;
12261
12262 while (it.current_y < it.last_visible_y
12263 && IT_CHARPOS (it) < CHARPOS (start)
12264 && !fonts_changed_p)
12265 if (display_line (&it))
12266 last_text_row = it.glyph_row - 1;
12267
12268 /* A value of current_y < last_visible_y means that we stopped
12269 at the previous window start, which in turn means that we
12270 have at least one reusable row. */
12271 if (it.current_y < it.last_visible_y)
12272 {
12273 /* IT.vpos always starts from 0; it counts text lines. */
12274 nrows_scrolled = it.vpos;
12275
12276 /* Find PT if not already found in the lines displayed. */
12277 if (w->cursor.vpos < 0)
12278 {
12279 int dy = it.current_y - first_row_y;
12280
12281 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12282 row = row_containing_pos (w, PT, row, NULL, dy);
12283 if (row)
12284 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12285 dy, nrows_scrolled);
12286 else
12287 {
12288 clear_glyph_matrix (w->desired_matrix);
12289 return 0;
12290 }
12291 }
12292
12293 /* Scroll the display. Do it before the current matrix is
12294 changed. The problem here is that update has not yet
12295 run, i.e. part of the current matrix is not up to date.
12296 scroll_run_hook will clear the cursor, and use the
12297 current matrix to get the height of the row the cursor is
12298 in. */
12299 run.current_y = first_row_y;
12300 run.desired_y = it.current_y;
12301 run.height = it.last_visible_y - it.current_y;
12302
12303 if (run.height > 0 && run.current_y != run.desired_y)
12304 {
12305 update_begin (f);
12306 FRAME_RIF (f)->update_window_begin_hook (w);
12307 FRAME_RIF (f)->clear_window_mouse_face (w);
12308 FRAME_RIF (f)->scroll_run_hook (w, &run);
12309 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12310 update_end (f);
12311 }
12312
12313 /* Shift current matrix down by nrows_scrolled lines. */
12314 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12315 rotate_matrix (w->current_matrix,
12316 start_vpos,
12317 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12318 nrows_scrolled);
12319
12320 /* Disable lines that must be updated. */
12321 for (i = 0; i < it.vpos; ++i)
12322 (start_row + i)->enabled_p = 0;
12323
12324 /* Re-compute Y positions. */
12325 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12326 max_y = it.last_visible_y;
12327 for (row = start_row + nrows_scrolled;
12328 row < bottom_row;
12329 ++row)
12330 {
12331 row->y = it.current_y;
12332 row->visible_height = row->height;
12333
12334 if (row->y < min_y)
12335 row->visible_height -= min_y - row->y;
12336 if (row->y + row->height > max_y)
12337 row->visible_height -= row->y + row->height - max_y;
12338 row->redraw_fringe_bitmaps_p = 1;
12339
12340 it.current_y += row->height;
12341
12342 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12343 last_reused_text_row = row;
12344 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12345 break;
12346 }
12347
12348 /* Disable lines in the current matrix which are now
12349 below the window. */
12350 for (++row; row < bottom_row; ++row)
12351 row->enabled_p = 0;
12352 }
12353
12354 /* Update window_end_pos etc.; last_reused_text_row is the last
12355 reused row from the current matrix containing text, if any.
12356 The value of last_text_row is the last displayed line
12357 containing text. */
12358 if (last_reused_text_row)
12359 {
12360 w->window_end_bytepos
12361 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12362 w->window_end_pos
12363 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12364 w->window_end_vpos
12365 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12366 w->current_matrix));
12367 }
12368 else if (last_text_row)
12369 {
12370 w->window_end_bytepos
12371 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12372 w->window_end_pos
12373 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12374 w->window_end_vpos
12375 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12376 }
12377 else
12378 {
12379 /* This window must be completely empty. */
12380 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12381 w->window_end_pos = make_number (Z - ZV);
12382 w->window_end_vpos = make_number (0);
12383 }
12384 w->window_end_valid = Qnil;
12385
12386 /* Update hint: don't try scrolling again in update_window. */
12387 w->desired_matrix->no_scrolling_p = 1;
12388
12389 #if GLYPH_DEBUG
12390 debug_method_add (w, "try_window_reusing_current_matrix 1");
12391 #endif
12392 return 1;
12393 }
12394 else if (CHARPOS (new_start) > CHARPOS (start))
12395 {
12396 struct glyph_row *pt_row, *row;
12397 struct glyph_row *first_reusable_row;
12398 struct glyph_row *first_row_to_display;
12399 int dy;
12400 int yb = window_text_bottom_y (w);
12401
12402 /* Find the row starting at new_start, if there is one. Don't
12403 reuse a partially visible line at the end. */
12404 first_reusable_row = start_row;
12405 while (first_reusable_row->enabled_p
12406 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12407 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12408 < CHARPOS (new_start)))
12409 ++first_reusable_row;
12410
12411 /* Give up if there is no row to reuse. */
12412 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12413 || !first_reusable_row->enabled_p
12414 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12415 != CHARPOS (new_start)))
12416 return 0;
12417
12418 /* We can reuse fully visible rows beginning with
12419 first_reusable_row to the end of the window. Set
12420 first_row_to_display to the first row that cannot be reused.
12421 Set pt_row to the row containing point, if there is any. */
12422 pt_row = NULL;
12423 for (first_row_to_display = first_reusable_row;
12424 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12425 ++first_row_to_display)
12426 {
12427 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12428 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12429 pt_row = first_row_to_display;
12430 }
12431
12432 /* Start displaying at the start of first_row_to_display. */
12433 xassert (first_row_to_display->y < yb);
12434 init_to_row_start (&it, w, first_row_to_display);
12435
12436 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12437 - start_vpos);
12438 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12439 - nrows_scrolled);
12440 it.current_y = (first_row_to_display->y - first_reusable_row->y
12441 + WINDOW_HEADER_LINE_HEIGHT (w));
12442
12443 /* Display lines beginning with first_row_to_display in the
12444 desired matrix. Set last_text_row to the last row displayed
12445 that displays text. */
12446 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12447 if (pt_row == NULL)
12448 w->cursor.vpos = -1;
12449 last_text_row = NULL;
12450 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12451 if (display_line (&it))
12452 last_text_row = it.glyph_row - 1;
12453
12454 /* Give up If point isn't in a row displayed or reused. */
12455 if (w->cursor.vpos < 0)
12456 {
12457 clear_glyph_matrix (w->desired_matrix);
12458 return 0;
12459 }
12460
12461 /* If point is in a reused row, adjust y and vpos of the cursor
12462 position. */
12463 if (pt_row)
12464 {
12465 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12466 w->current_matrix);
12467 w->cursor.y -= first_reusable_row->y;
12468 }
12469
12470 /* Scroll the display. */
12471 run.current_y = first_reusable_row->y;
12472 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12473 run.height = it.last_visible_y - run.current_y;
12474 dy = run.current_y - run.desired_y;
12475
12476 if (run.height)
12477 {
12478 update_begin (f);
12479 FRAME_RIF (f)->update_window_begin_hook (w);
12480 FRAME_RIF (f)->clear_window_mouse_face (w);
12481 FRAME_RIF (f)->scroll_run_hook (w, &run);
12482 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12483 update_end (f);
12484 }
12485
12486 /* Adjust Y positions of reused rows. */
12487 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12488 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12489 max_y = it.last_visible_y;
12490 for (row = first_reusable_row; row < first_row_to_display; ++row)
12491 {
12492 row->y -= dy;
12493 row->visible_height = row->height;
12494 if (row->y < min_y)
12495 row->visible_height -= min_y - row->y;
12496 if (row->y + row->height > max_y)
12497 row->visible_height -= row->y + row->height - max_y;
12498 row->redraw_fringe_bitmaps_p = 1;
12499 }
12500
12501 /* Scroll the current matrix. */
12502 xassert (nrows_scrolled > 0);
12503 rotate_matrix (w->current_matrix,
12504 start_vpos,
12505 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12506 -nrows_scrolled);
12507
12508 /* Disable rows not reused. */
12509 for (row -= nrows_scrolled; row < bottom_row; ++row)
12510 row->enabled_p = 0;
12511
12512 /* Adjust window end. A null value of last_text_row means that
12513 the window end is in reused rows which in turn means that
12514 only its vpos can have changed. */
12515 if (last_text_row)
12516 {
12517 w->window_end_bytepos
12518 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12519 w->window_end_pos
12520 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12521 w->window_end_vpos
12522 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12523 }
12524 else
12525 {
12526 w->window_end_vpos
12527 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12528 }
12529
12530 w->window_end_valid = Qnil;
12531 w->desired_matrix->no_scrolling_p = 1;
12532
12533 #if GLYPH_DEBUG
12534 debug_method_add (w, "try_window_reusing_current_matrix 2");
12535 #endif
12536 return 1;
12537 }
12538
12539 return 0;
12540 }
12541
12542
12543 \f
12544 /************************************************************************
12545 Window redisplay reusing current matrix when buffer has changed
12546 ************************************************************************/
12547
12548 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12549 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12550 int *, int *));
12551 static struct glyph_row *
12552 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12553 struct glyph_row *));
12554
12555
12556 /* Return the last row in MATRIX displaying text. If row START is
12557 non-null, start searching with that row. IT gives the dimensions
12558 of the display. Value is null if matrix is empty; otherwise it is
12559 a pointer to the row found. */
12560
12561 static struct glyph_row *
12562 find_last_row_displaying_text (matrix, it, start)
12563 struct glyph_matrix *matrix;
12564 struct it *it;
12565 struct glyph_row *start;
12566 {
12567 struct glyph_row *row, *row_found;
12568
12569 /* Set row_found to the last row in IT->w's current matrix
12570 displaying text. The loop looks funny but think of partially
12571 visible lines. */
12572 row_found = NULL;
12573 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12574 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12575 {
12576 xassert (row->enabled_p);
12577 row_found = row;
12578 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12579 break;
12580 ++row;
12581 }
12582
12583 return row_found;
12584 }
12585
12586
12587 /* Return the last row in the current matrix of W that is not affected
12588 by changes at the start of current_buffer that occurred since W's
12589 current matrix was built. Value is null if no such row exists.
12590
12591 BEG_UNCHANGED us the number of characters unchanged at the start of
12592 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12593 first changed character in current_buffer. Characters at positions <
12594 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12595 when the current matrix was built. */
12596
12597 static struct glyph_row *
12598 find_last_unchanged_at_beg_row (w)
12599 struct window *w;
12600 {
12601 int first_changed_pos = BEG + BEG_UNCHANGED;
12602 struct glyph_row *row;
12603 struct glyph_row *row_found = NULL;
12604 int yb = window_text_bottom_y (w);
12605
12606 /* Find the last row displaying unchanged text. */
12607 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12608 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12609 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12610 {
12611 if (/* If row ends before first_changed_pos, it is unchanged,
12612 except in some case. */
12613 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12614 /* When row ends in ZV and we write at ZV it is not
12615 unchanged. */
12616 && !row->ends_at_zv_p
12617 /* When first_changed_pos is the end of a continued line,
12618 row is not unchanged because it may be no longer
12619 continued. */
12620 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12621 && (row->continued_p
12622 || row->exact_window_width_line_p)))
12623 row_found = row;
12624
12625 /* Stop if last visible row. */
12626 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12627 break;
12628
12629 ++row;
12630 }
12631
12632 return row_found;
12633 }
12634
12635
12636 /* Find the first glyph row in the current matrix of W that is not
12637 affected by changes at the end of current_buffer since the
12638 time W's current matrix was built.
12639
12640 Return in *DELTA the number of chars by which buffer positions in
12641 unchanged text at the end of current_buffer must be adjusted.
12642
12643 Return in *DELTA_BYTES the corresponding number of bytes.
12644
12645 Value is null if no such row exists, i.e. all rows are affected by
12646 changes. */
12647
12648 static struct glyph_row *
12649 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12650 struct window *w;
12651 int *delta, *delta_bytes;
12652 {
12653 struct glyph_row *row;
12654 struct glyph_row *row_found = NULL;
12655
12656 *delta = *delta_bytes = 0;
12657
12658 /* Display must not have been paused, otherwise the current matrix
12659 is not up to date. */
12660 if (NILP (w->window_end_valid))
12661 abort ();
12662
12663 /* A value of window_end_pos >= END_UNCHANGED means that the window
12664 end is in the range of changed text. If so, there is no
12665 unchanged row at the end of W's current matrix. */
12666 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12667 return NULL;
12668
12669 /* Set row to the last row in W's current matrix displaying text. */
12670 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12671
12672 /* If matrix is entirely empty, no unchanged row exists. */
12673 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12674 {
12675 /* The value of row is the last glyph row in the matrix having a
12676 meaningful buffer position in it. The end position of row
12677 corresponds to window_end_pos. This allows us to translate
12678 buffer positions in the current matrix to current buffer
12679 positions for characters not in changed text. */
12680 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12681 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12682 int last_unchanged_pos, last_unchanged_pos_old;
12683 struct glyph_row *first_text_row
12684 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12685
12686 *delta = Z - Z_old;
12687 *delta_bytes = Z_BYTE - Z_BYTE_old;
12688
12689 /* Set last_unchanged_pos to the buffer position of the last
12690 character in the buffer that has not been changed. Z is the
12691 index + 1 of the last character in current_buffer, i.e. by
12692 subtracting END_UNCHANGED we get the index of the last
12693 unchanged character, and we have to add BEG to get its buffer
12694 position. */
12695 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12696 last_unchanged_pos_old = last_unchanged_pos - *delta;
12697
12698 /* Search backward from ROW for a row displaying a line that
12699 starts at a minimum position >= last_unchanged_pos_old. */
12700 for (; row > first_text_row; --row)
12701 {
12702 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12703 abort ();
12704
12705 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12706 row_found = row;
12707 }
12708 }
12709
12710 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12711 abort ();
12712
12713 return row_found;
12714 }
12715
12716
12717 /* Make sure that glyph rows in the current matrix of window W
12718 reference the same glyph memory as corresponding rows in the
12719 frame's frame matrix. This function is called after scrolling W's
12720 current matrix on a terminal frame in try_window_id and
12721 try_window_reusing_current_matrix. */
12722
12723 static void
12724 sync_frame_with_window_matrix_rows (w)
12725 struct window *w;
12726 {
12727 struct frame *f = XFRAME (w->frame);
12728 struct glyph_row *window_row, *window_row_end, *frame_row;
12729
12730 /* Preconditions: W must be a leaf window and full-width. Its frame
12731 must have a frame matrix. */
12732 xassert (NILP (w->hchild) && NILP (w->vchild));
12733 xassert (WINDOW_FULL_WIDTH_P (w));
12734 xassert (!FRAME_WINDOW_P (f));
12735
12736 /* If W is a full-width window, glyph pointers in W's current matrix
12737 have, by definition, to be the same as glyph pointers in the
12738 corresponding frame matrix. Note that frame matrices have no
12739 marginal areas (see build_frame_matrix). */
12740 window_row = w->current_matrix->rows;
12741 window_row_end = window_row + w->current_matrix->nrows;
12742 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12743 while (window_row < window_row_end)
12744 {
12745 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12746 struct glyph *end = window_row->glyphs[LAST_AREA];
12747
12748 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12749 frame_row->glyphs[TEXT_AREA] = start;
12750 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12751 frame_row->glyphs[LAST_AREA] = end;
12752
12753 /* Disable frame rows whose corresponding window rows have
12754 been disabled in try_window_id. */
12755 if (!window_row->enabled_p)
12756 frame_row->enabled_p = 0;
12757
12758 ++window_row, ++frame_row;
12759 }
12760 }
12761
12762
12763 /* Find the glyph row in window W containing CHARPOS. Consider all
12764 rows between START and END (not inclusive). END null means search
12765 all rows to the end of the display area of W. Value is the row
12766 containing CHARPOS or null. */
12767
12768 struct glyph_row *
12769 row_containing_pos (w, charpos, start, end, dy)
12770 struct window *w;
12771 int charpos;
12772 struct glyph_row *start, *end;
12773 int dy;
12774 {
12775 struct glyph_row *row = start;
12776 int last_y;
12777
12778 /* If we happen to start on a header-line, skip that. */
12779 if (row->mode_line_p)
12780 ++row;
12781
12782 if ((end && row >= end) || !row->enabled_p)
12783 return NULL;
12784
12785 last_y = window_text_bottom_y (w) - dy;
12786
12787 while (1)
12788 {
12789 /* Give up if we have gone too far. */
12790 if (end && row >= end)
12791 return NULL;
12792 /* This formerly returned if they were equal.
12793 I think that both quantities are of a "last plus one" type;
12794 if so, when they are equal, the row is within the screen. -- rms. */
12795 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12796 return NULL;
12797
12798 /* If it is in this row, return this row. */
12799 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12800 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12801 /* The end position of a row equals the start
12802 position of the next row. If CHARPOS is there, we
12803 would rather display it in the next line, except
12804 when this line ends in ZV. */
12805 && !row->ends_at_zv_p
12806 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12807 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12808 return row;
12809 ++row;
12810 }
12811 }
12812
12813
12814 /* Try to redisplay window W by reusing its existing display. W's
12815 current matrix must be up to date when this function is called,
12816 i.e. window_end_valid must not be nil.
12817
12818 Value is
12819
12820 1 if display has been updated
12821 0 if otherwise unsuccessful
12822 -1 if redisplay with same window start is known not to succeed
12823
12824 The following steps are performed:
12825
12826 1. Find the last row in the current matrix of W that is not
12827 affected by changes at the start of current_buffer. If no such row
12828 is found, give up.
12829
12830 2. Find the first row in W's current matrix that is not affected by
12831 changes at the end of current_buffer. Maybe there is no such row.
12832
12833 3. Display lines beginning with the row + 1 found in step 1 to the
12834 row found in step 2 or, if step 2 didn't find a row, to the end of
12835 the window.
12836
12837 4. If cursor is not known to appear on the window, give up.
12838
12839 5. If display stopped at the row found in step 2, scroll the
12840 display and current matrix as needed.
12841
12842 6. Maybe display some lines at the end of W, if we must. This can
12843 happen under various circumstances, like a partially visible line
12844 becoming fully visible, or because newly displayed lines are displayed
12845 in smaller font sizes.
12846
12847 7. Update W's window end information. */
12848
12849 static int
12850 try_window_id (w)
12851 struct window *w;
12852 {
12853 struct frame *f = XFRAME (w->frame);
12854 struct glyph_matrix *current_matrix = w->current_matrix;
12855 struct glyph_matrix *desired_matrix = w->desired_matrix;
12856 struct glyph_row *last_unchanged_at_beg_row;
12857 struct glyph_row *first_unchanged_at_end_row;
12858 struct glyph_row *row;
12859 struct glyph_row *bottom_row;
12860 int bottom_vpos;
12861 struct it it;
12862 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12863 struct text_pos start_pos;
12864 struct run run;
12865 int first_unchanged_at_end_vpos = 0;
12866 struct glyph_row *last_text_row, *last_text_row_at_end;
12867 struct text_pos start;
12868 int first_changed_charpos, last_changed_charpos;
12869
12870 #if GLYPH_DEBUG
12871 if (inhibit_try_window_id)
12872 return 0;
12873 #endif
12874
12875 /* This is handy for debugging. */
12876 #if 0
12877 #define GIVE_UP(X) \
12878 do { \
12879 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12880 return 0; \
12881 } while (0)
12882 #else
12883 #define GIVE_UP(X) return 0
12884 #endif
12885
12886 SET_TEXT_POS_FROM_MARKER (start, w->start);
12887
12888 /* Don't use this for mini-windows because these can show
12889 messages and mini-buffers, and we don't handle that here. */
12890 if (MINI_WINDOW_P (w))
12891 GIVE_UP (1);
12892
12893 /* This flag is used to prevent redisplay optimizations. */
12894 if (windows_or_buffers_changed || cursor_type_changed)
12895 GIVE_UP (2);
12896
12897 /* Verify that narrowing has not changed.
12898 Also verify that we were not told to prevent redisplay optimizations.
12899 It would be nice to further
12900 reduce the number of cases where this prevents try_window_id. */
12901 if (current_buffer->clip_changed
12902 || current_buffer->prevent_redisplay_optimizations_p)
12903 GIVE_UP (3);
12904
12905 /* Window must either use window-based redisplay or be full width. */
12906 if (!FRAME_WINDOW_P (f)
12907 && (!FRAME_LINE_INS_DEL_OK (f)
12908 || !WINDOW_FULL_WIDTH_P (w)))
12909 GIVE_UP (4);
12910
12911 /* Give up if point is not known NOT to appear in W. */
12912 if (PT < CHARPOS (start))
12913 GIVE_UP (5);
12914
12915 /* Another way to prevent redisplay optimizations. */
12916 if (XFASTINT (w->last_modified) == 0)
12917 GIVE_UP (6);
12918
12919 /* Verify that window is not hscrolled. */
12920 if (XFASTINT (w->hscroll) != 0)
12921 GIVE_UP (7);
12922
12923 /* Verify that display wasn't paused. */
12924 if (NILP (w->window_end_valid))
12925 GIVE_UP (8);
12926
12927 /* Can't use this if highlighting a region because a cursor movement
12928 will do more than just set the cursor. */
12929 if (!NILP (Vtransient_mark_mode)
12930 && !NILP (current_buffer->mark_active))
12931 GIVE_UP (9);
12932
12933 /* Likewise if highlighting trailing whitespace. */
12934 if (!NILP (Vshow_trailing_whitespace))
12935 GIVE_UP (11);
12936
12937 /* Likewise if showing a region. */
12938 if (!NILP (w->region_showing))
12939 GIVE_UP (10);
12940
12941 /* Can use this if overlay arrow position and or string have changed. */
12942 if (overlay_arrows_changed_p ())
12943 GIVE_UP (12);
12944
12945
12946 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12947 only if buffer has really changed. The reason is that the gap is
12948 initially at Z for freshly visited files. The code below would
12949 set end_unchanged to 0 in that case. */
12950 if (MODIFF > SAVE_MODIFF
12951 /* This seems to happen sometimes after saving a buffer. */
12952 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12953 {
12954 if (GPT - BEG < BEG_UNCHANGED)
12955 BEG_UNCHANGED = GPT - BEG;
12956 if (Z - GPT < END_UNCHANGED)
12957 END_UNCHANGED = Z - GPT;
12958 }
12959
12960 /* The position of the first and last character that has been changed. */
12961 first_changed_charpos = BEG + BEG_UNCHANGED;
12962 last_changed_charpos = Z - END_UNCHANGED;
12963
12964 /* If window starts after a line end, and the last change is in
12965 front of that newline, then changes don't affect the display.
12966 This case happens with stealth-fontification. Note that although
12967 the display is unchanged, glyph positions in the matrix have to
12968 be adjusted, of course. */
12969 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12970 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12971 && ((last_changed_charpos < CHARPOS (start)
12972 && CHARPOS (start) == BEGV)
12973 || (last_changed_charpos < CHARPOS (start) - 1
12974 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12975 {
12976 int Z_old, delta, Z_BYTE_old, delta_bytes;
12977 struct glyph_row *r0;
12978
12979 /* Compute how many chars/bytes have been added to or removed
12980 from the buffer. */
12981 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12982 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12983 delta = Z - Z_old;
12984 delta_bytes = Z_BYTE - Z_BYTE_old;
12985
12986 /* Give up if PT is not in the window. Note that it already has
12987 been checked at the start of try_window_id that PT is not in
12988 front of the window start. */
12989 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12990 GIVE_UP (13);
12991
12992 /* If window start is unchanged, we can reuse the whole matrix
12993 as is, after adjusting glyph positions. No need to compute
12994 the window end again, since its offset from Z hasn't changed. */
12995 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12996 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12997 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12998 /* PT must not be in a partially visible line. */
12999 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13000 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13001 {
13002 /* Adjust positions in the glyph matrix. */
13003 if (delta || delta_bytes)
13004 {
13005 struct glyph_row *r1
13006 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13007 increment_matrix_positions (w->current_matrix,
13008 MATRIX_ROW_VPOS (r0, current_matrix),
13009 MATRIX_ROW_VPOS (r1, current_matrix),
13010 delta, delta_bytes);
13011 }
13012
13013 /* Set the cursor. */
13014 row = row_containing_pos (w, PT, r0, NULL, 0);
13015 if (row)
13016 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13017 else
13018 abort ();
13019 return 1;
13020 }
13021 }
13022
13023 /* Handle the case that changes are all below what is displayed in
13024 the window, and that PT is in the window. This shortcut cannot
13025 be taken if ZV is visible in the window, and text has been added
13026 there that is visible in the window. */
13027 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13028 /* ZV is not visible in the window, or there are no
13029 changes at ZV, actually. */
13030 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13031 || first_changed_charpos == last_changed_charpos))
13032 {
13033 struct glyph_row *r0;
13034
13035 /* Give up if PT is not in the window. Note that it already has
13036 been checked at the start of try_window_id that PT is not in
13037 front of the window start. */
13038 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13039 GIVE_UP (14);
13040
13041 /* If window start is unchanged, we can reuse the whole matrix
13042 as is, without changing glyph positions since no text has
13043 been added/removed in front of the window end. */
13044 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13045 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13046 /* PT must not be in a partially visible line. */
13047 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13048 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13049 {
13050 /* We have to compute the window end anew since text
13051 can have been added/removed after it. */
13052 w->window_end_pos
13053 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13054 w->window_end_bytepos
13055 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13056
13057 /* Set the cursor. */
13058 row = row_containing_pos (w, PT, r0, NULL, 0);
13059 if (row)
13060 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13061 else
13062 abort ();
13063 return 2;
13064 }
13065 }
13066
13067 /* Give up if window start is in the changed area.
13068
13069 The condition used to read
13070
13071 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13072
13073 but why that was tested escapes me at the moment. */
13074 if (CHARPOS (start) >= first_changed_charpos
13075 && CHARPOS (start) <= last_changed_charpos)
13076 GIVE_UP (15);
13077
13078 /* Check that window start agrees with the start of the first glyph
13079 row in its current matrix. Check this after we know the window
13080 start is not in changed text, otherwise positions would not be
13081 comparable. */
13082 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13083 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13084 GIVE_UP (16);
13085
13086 /* Give up if the window ends in strings. Overlay strings
13087 at the end are difficult to handle, so don't try. */
13088 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13089 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13090 GIVE_UP (20);
13091
13092 /* Compute the position at which we have to start displaying new
13093 lines. Some of the lines at the top of the window might be
13094 reusable because they are not displaying changed text. Find the
13095 last row in W's current matrix not affected by changes at the
13096 start of current_buffer. Value is null if changes start in the
13097 first line of window. */
13098 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13099 if (last_unchanged_at_beg_row)
13100 {
13101 /* Avoid starting to display in the moddle of a character, a TAB
13102 for instance. This is easier than to set up the iterator
13103 exactly, and it's not a frequent case, so the additional
13104 effort wouldn't really pay off. */
13105 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13106 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13107 && last_unchanged_at_beg_row > w->current_matrix->rows)
13108 --last_unchanged_at_beg_row;
13109
13110 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13111 GIVE_UP (17);
13112
13113 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13114 GIVE_UP (18);
13115 start_pos = it.current.pos;
13116
13117 /* Start displaying new lines in the desired matrix at the same
13118 vpos we would use in the current matrix, i.e. below
13119 last_unchanged_at_beg_row. */
13120 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13121 current_matrix);
13122 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13123 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13124
13125 xassert (it.hpos == 0 && it.current_x == 0);
13126 }
13127 else
13128 {
13129 /* There are no reusable lines at the start of the window.
13130 Start displaying in the first text line. */
13131 start_display (&it, w, start);
13132 it.vpos = it.first_vpos;
13133 start_pos = it.current.pos;
13134 }
13135
13136 /* Find the first row that is not affected by changes at the end of
13137 the buffer. Value will be null if there is no unchanged row, in
13138 which case we must redisplay to the end of the window. delta
13139 will be set to the value by which buffer positions beginning with
13140 first_unchanged_at_end_row have to be adjusted due to text
13141 changes. */
13142 first_unchanged_at_end_row
13143 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13144 IF_DEBUG (debug_delta = delta);
13145 IF_DEBUG (debug_delta_bytes = delta_bytes);
13146
13147 /* Set stop_pos to the buffer position up to which we will have to
13148 display new lines. If first_unchanged_at_end_row != NULL, this
13149 is the buffer position of the start of the line displayed in that
13150 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13151 that we don't stop at a buffer position. */
13152 stop_pos = 0;
13153 if (first_unchanged_at_end_row)
13154 {
13155 xassert (last_unchanged_at_beg_row == NULL
13156 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13157
13158 /* If this is a continuation line, move forward to the next one
13159 that isn't. Changes in lines above affect this line.
13160 Caution: this may move first_unchanged_at_end_row to a row
13161 not displaying text. */
13162 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13163 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13164 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13165 < it.last_visible_y))
13166 ++first_unchanged_at_end_row;
13167
13168 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13169 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13170 >= it.last_visible_y))
13171 first_unchanged_at_end_row = NULL;
13172 else
13173 {
13174 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13175 + delta);
13176 first_unchanged_at_end_vpos
13177 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13178 xassert (stop_pos >= Z - END_UNCHANGED);
13179 }
13180 }
13181 else if (last_unchanged_at_beg_row == NULL)
13182 GIVE_UP (19);
13183
13184
13185 #if GLYPH_DEBUG
13186
13187 /* Either there is no unchanged row at the end, or the one we have
13188 now displays text. This is a necessary condition for the window
13189 end pos calculation at the end of this function. */
13190 xassert (first_unchanged_at_end_row == NULL
13191 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13192
13193 debug_last_unchanged_at_beg_vpos
13194 = (last_unchanged_at_beg_row
13195 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13196 : -1);
13197 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13198
13199 #endif /* GLYPH_DEBUG != 0 */
13200
13201
13202 /* Display new lines. Set last_text_row to the last new line
13203 displayed which has text on it, i.e. might end up as being the
13204 line where the window_end_vpos is. */
13205 w->cursor.vpos = -1;
13206 last_text_row = NULL;
13207 overlay_arrow_seen = 0;
13208 while (it.current_y < it.last_visible_y
13209 && !fonts_changed_p
13210 && (first_unchanged_at_end_row == NULL
13211 || IT_CHARPOS (it) < stop_pos))
13212 {
13213 if (display_line (&it))
13214 last_text_row = it.glyph_row - 1;
13215 }
13216
13217 if (fonts_changed_p)
13218 return -1;
13219
13220
13221 /* Compute differences in buffer positions, y-positions etc. for
13222 lines reused at the bottom of the window. Compute what we can
13223 scroll. */
13224 if (first_unchanged_at_end_row
13225 /* No lines reused because we displayed everything up to the
13226 bottom of the window. */
13227 && it.current_y < it.last_visible_y)
13228 {
13229 dvpos = (it.vpos
13230 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13231 current_matrix));
13232 dy = it.current_y - first_unchanged_at_end_row->y;
13233 run.current_y = first_unchanged_at_end_row->y;
13234 run.desired_y = run.current_y + dy;
13235 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13236 }
13237 else
13238 {
13239 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13240 first_unchanged_at_end_row = NULL;
13241 }
13242 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13243
13244
13245 /* Find the cursor if not already found. We have to decide whether
13246 PT will appear on this window (it sometimes doesn't, but this is
13247 not a very frequent case.) This decision has to be made before
13248 the current matrix is altered. A value of cursor.vpos < 0 means
13249 that PT is either in one of the lines beginning at
13250 first_unchanged_at_end_row or below the window. Don't care for
13251 lines that might be displayed later at the window end; as
13252 mentioned, this is not a frequent case. */
13253 if (w->cursor.vpos < 0)
13254 {
13255 /* Cursor in unchanged rows at the top? */
13256 if (PT < CHARPOS (start_pos)
13257 && last_unchanged_at_beg_row)
13258 {
13259 row = row_containing_pos (w, PT,
13260 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13261 last_unchanged_at_beg_row + 1, 0);
13262 if (row)
13263 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13264 }
13265
13266 /* Start from first_unchanged_at_end_row looking for PT. */
13267 else if (first_unchanged_at_end_row)
13268 {
13269 row = row_containing_pos (w, PT - delta,
13270 first_unchanged_at_end_row, NULL, 0);
13271 if (row)
13272 set_cursor_from_row (w, row, w->current_matrix, delta,
13273 delta_bytes, dy, dvpos);
13274 }
13275
13276 /* Give up if cursor was not found. */
13277 if (w->cursor.vpos < 0)
13278 {
13279 clear_glyph_matrix (w->desired_matrix);
13280 return -1;
13281 }
13282 }
13283
13284 /* Don't let the cursor end in the scroll margins. */
13285 {
13286 int this_scroll_margin, cursor_height;
13287
13288 this_scroll_margin = max (0, scroll_margin);
13289 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13290 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13291 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13292
13293 if ((w->cursor.y < this_scroll_margin
13294 && CHARPOS (start) > BEGV)
13295 /* Don't take scroll margin into account at the bottom because
13296 old redisplay didn't do it either. */
13297 || w->cursor.y + cursor_height > it.last_visible_y)
13298 {
13299 w->cursor.vpos = -1;
13300 clear_glyph_matrix (w->desired_matrix);
13301 return -1;
13302 }
13303 }
13304
13305 /* Scroll the display. Do it before changing the current matrix so
13306 that xterm.c doesn't get confused about where the cursor glyph is
13307 found. */
13308 if (dy && run.height)
13309 {
13310 update_begin (f);
13311
13312 if (FRAME_WINDOW_P (f))
13313 {
13314 FRAME_RIF (f)->update_window_begin_hook (w);
13315 FRAME_RIF (f)->clear_window_mouse_face (w);
13316 FRAME_RIF (f)->scroll_run_hook (w, &run);
13317 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13318 }
13319 else
13320 {
13321 /* Terminal frame. In this case, dvpos gives the number of
13322 lines to scroll by; dvpos < 0 means scroll up. */
13323 int first_unchanged_at_end_vpos
13324 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13325 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13326 int end = (WINDOW_TOP_EDGE_LINE (w)
13327 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13328 + window_internal_height (w));
13329
13330 /* Perform the operation on the screen. */
13331 if (dvpos > 0)
13332 {
13333 /* Scroll last_unchanged_at_beg_row to the end of the
13334 window down dvpos lines. */
13335 set_terminal_window (end);
13336
13337 /* On dumb terminals delete dvpos lines at the end
13338 before inserting dvpos empty lines. */
13339 if (!FRAME_SCROLL_REGION_OK (f))
13340 ins_del_lines (end - dvpos, -dvpos);
13341
13342 /* Insert dvpos empty lines in front of
13343 last_unchanged_at_beg_row. */
13344 ins_del_lines (from, dvpos);
13345 }
13346 else if (dvpos < 0)
13347 {
13348 /* Scroll up last_unchanged_at_beg_vpos to the end of
13349 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13350 set_terminal_window (end);
13351
13352 /* Delete dvpos lines in front of
13353 last_unchanged_at_beg_vpos. ins_del_lines will set
13354 the cursor to the given vpos and emit |dvpos| delete
13355 line sequences. */
13356 ins_del_lines (from + dvpos, dvpos);
13357
13358 /* On a dumb terminal insert dvpos empty lines at the
13359 end. */
13360 if (!FRAME_SCROLL_REGION_OK (f))
13361 ins_del_lines (end + dvpos, -dvpos);
13362 }
13363
13364 set_terminal_window (0);
13365 }
13366
13367 update_end (f);
13368 }
13369
13370 /* Shift reused rows of the current matrix to the right position.
13371 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13372 text. */
13373 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13374 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13375 if (dvpos < 0)
13376 {
13377 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13378 bottom_vpos, dvpos);
13379 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13380 bottom_vpos, 0);
13381 }
13382 else if (dvpos > 0)
13383 {
13384 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13385 bottom_vpos, dvpos);
13386 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13387 first_unchanged_at_end_vpos + dvpos, 0);
13388 }
13389
13390 /* For frame-based redisplay, make sure that current frame and window
13391 matrix are in sync with respect to glyph memory. */
13392 if (!FRAME_WINDOW_P (f))
13393 sync_frame_with_window_matrix_rows (w);
13394
13395 /* Adjust buffer positions in reused rows. */
13396 if (delta)
13397 increment_matrix_positions (current_matrix,
13398 first_unchanged_at_end_vpos + dvpos,
13399 bottom_vpos, delta, delta_bytes);
13400
13401 /* Adjust Y positions. */
13402 if (dy)
13403 shift_glyph_matrix (w, current_matrix,
13404 first_unchanged_at_end_vpos + dvpos,
13405 bottom_vpos, dy);
13406
13407 if (first_unchanged_at_end_row)
13408 first_unchanged_at_end_row += dvpos;
13409
13410 /* If scrolling up, there may be some lines to display at the end of
13411 the window. */
13412 last_text_row_at_end = NULL;
13413 if (dy < 0)
13414 {
13415 /* Scrolling up can leave for example a partially visible line
13416 at the end of the window to be redisplayed. */
13417 /* Set last_row to the glyph row in the current matrix where the
13418 window end line is found. It has been moved up or down in
13419 the matrix by dvpos. */
13420 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13421 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13422
13423 /* If last_row is the window end line, it should display text. */
13424 xassert (last_row->displays_text_p);
13425
13426 /* If window end line was partially visible before, begin
13427 displaying at that line. Otherwise begin displaying with the
13428 line following it. */
13429 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13430 {
13431 init_to_row_start (&it, w, last_row);
13432 it.vpos = last_vpos;
13433 it.current_y = last_row->y;
13434 }
13435 else
13436 {
13437 init_to_row_end (&it, w, last_row);
13438 it.vpos = 1 + last_vpos;
13439 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13440 ++last_row;
13441 }
13442
13443 /* We may start in a continuation line. If so, we have to
13444 get the right continuation_lines_width and current_x. */
13445 it.continuation_lines_width = last_row->continuation_lines_width;
13446 it.hpos = it.current_x = 0;
13447
13448 /* Display the rest of the lines at the window end. */
13449 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13450 while (it.current_y < it.last_visible_y
13451 && !fonts_changed_p)
13452 {
13453 /* Is it always sure that the display agrees with lines in
13454 the current matrix? I don't think so, so we mark rows
13455 displayed invalid in the current matrix by setting their
13456 enabled_p flag to zero. */
13457 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13458 if (display_line (&it))
13459 last_text_row_at_end = it.glyph_row - 1;
13460 }
13461 }
13462
13463 /* Update window_end_pos and window_end_vpos. */
13464 if (first_unchanged_at_end_row
13465 && first_unchanged_at_end_row->y < it.last_visible_y
13466 && !last_text_row_at_end)
13467 {
13468 /* Window end line if one of the preserved rows from the current
13469 matrix. Set row to the last row displaying text in current
13470 matrix starting at first_unchanged_at_end_row, after
13471 scrolling. */
13472 xassert (first_unchanged_at_end_row->displays_text_p);
13473 row = find_last_row_displaying_text (w->current_matrix, &it,
13474 first_unchanged_at_end_row);
13475 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13476
13477 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13478 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13479 w->window_end_vpos
13480 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13481 xassert (w->window_end_bytepos >= 0);
13482 IF_DEBUG (debug_method_add (w, "A"));
13483 }
13484 else if (last_text_row_at_end)
13485 {
13486 w->window_end_pos
13487 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13488 w->window_end_bytepos
13489 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13490 w->window_end_vpos
13491 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13492 xassert (w->window_end_bytepos >= 0);
13493 IF_DEBUG (debug_method_add (w, "B"));
13494 }
13495 else if (last_text_row)
13496 {
13497 /* We have displayed either to the end of the window or at the
13498 end of the window, i.e. the last row with text is to be found
13499 in the desired matrix. */
13500 w->window_end_pos
13501 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13502 w->window_end_bytepos
13503 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13504 w->window_end_vpos
13505 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13506 xassert (w->window_end_bytepos >= 0);
13507 }
13508 else if (first_unchanged_at_end_row == NULL
13509 && last_text_row == NULL
13510 && last_text_row_at_end == NULL)
13511 {
13512 /* Displayed to end of window, but no line containing text was
13513 displayed. Lines were deleted at the end of the window. */
13514 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13515 int vpos = XFASTINT (w->window_end_vpos);
13516 struct glyph_row *current_row = current_matrix->rows + vpos;
13517 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13518
13519 for (row = NULL;
13520 row == NULL && vpos >= first_vpos;
13521 --vpos, --current_row, --desired_row)
13522 {
13523 if (desired_row->enabled_p)
13524 {
13525 if (desired_row->displays_text_p)
13526 row = desired_row;
13527 }
13528 else if (current_row->displays_text_p)
13529 row = current_row;
13530 }
13531
13532 xassert (row != NULL);
13533 w->window_end_vpos = make_number (vpos + 1);
13534 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13535 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13536 xassert (w->window_end_bytepos >= 0);
13537 IF_DEBUG (debug_method_add (w, "C"));
13538 }
13539 else
13540 abort ();
13541
13542 #if 0 /* This leads to problems, for instance when the cursor is
13543 at ZV, and the cursor line displays no text. */
13544 /* Disable rows below what's displayed in the window. This makes
13545 debugging easier. */
13546 enable_glyph_matrix_rows (current_matrix,
13547 XFASTINT (w->window_end_vpos) + 1,
13548 bottom_vpos, 0);
13549 #endif
13550
13551 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13552 debug_end_vpos = XFASTINT (w->window_end_vpos));
13553
13554 /* Record that display has not been completed. */
13555 w->window_end_valid = Qnil;
13556 w->desired_matrix->no_scrolling_p = 1;
13557 return 3;
13558
13559 #undef GIVE_UP
13560 }
13561
13562
13563 \f
13564 /***********************************************************************
13565 More debugging support
13566 ***********************************************************************/
13567
13568 #if GLYPH_DEBUG
13569
13570 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13571 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13572 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13573
13574
13575 /* Dump the contents of glyph matrix MATRIX on stderr.
13576
13577 GLYPHS 0 means don't show glyph contents.
13578 GLYPHS 1 means show glyphs in short form
13579 GLYPHS > 1 means show glyphs in long form. */
13580
13581 void
13582 dump_glyph_matrix (matrix, glyphs)
13583 struct glyph_matrix *matrix;
13584 int glyphs;
13585 {
13586 int i;
13587 for (i = 0; i < matrix->nrows; ++i)
13588 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13589 }
13590
13591
13592 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13593 the glyph row and area where the glyph comes from. */
13594
13595 void
13596 dump_glyph (row, glyph, area)
13597 struct glyph_row *row;
13598 struct glyph *glyph;
13599 int area;
13600 {
13601 if (glyph->type == CHAR_GLYPH)
13602 {
13603 fprintf (stderr,
13604 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13605 glyph - row->glyphs[TEXT_AREA],
13606 'C',
13607 glyph->charpos,
13608 (BUFFERP (glyph->object)
13609 ? 'B'
13610 : (STRINGP (glyph->object)
13611 ? 'S'
13612 : '-')),
13613 glyph->pixel_width,
13614 glyph->u.ch,
13615 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13616 ? glyph->u.ch
13617 : '.'),
13618 glyph->face_id,
13619 glyph->left_box_line_p,
13620 glyph->right_box_line_p);
13621 }
13622 else if (glyph->type == STRETCH_GLYPH)
13623 {
13624 fprintf (stderr,
13625 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13626 glyph - row->glyphs[TEXT_AREA],
13627 'S',
13628 glyph->charpos,
13629 (BUFFERP (glyph->object)
13630 ? 'B'
13631 : (STRINGP (glyph->object)
13632 ? 'S'
13633 : '-')),
13634 glyph->pixel_width,
13635 0,
13636 '.',
13637 glyph->face_id,
13638 glyph->left_box_line_p,
13639 glyph->right_box_line_p);
13640 }
13641 else if (glyph->type == IMAGE_GLYPH)
13642 {
13643 fprintf (stderr,
13644 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13645 glyph - row->glyphs[TEXT_AREA],
13646 'I',
13647 glyph->charpos,
13648 (BUFFERP (glyph->object)
13649 ? 'B'
13650 : (STRINGP (glyph->object)
13651 ? 'S'
13652 : '-')),
13653 glyph->pixel_width,
13654 glyph->u.img_id,
13655 '.',
13656 glyph->face_id,
13657 glyph->left_box_line_p,
13658 glyph->right_box_line_p);
13659 }
13660 }
13661
13662
13663 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13664 GLYPHS 0 means don't show glyph contents.
13665 GLYPHS 1 means show glyphs in short form
13666 GLYPHS > 1 means show glyphs in long form. */
13667
13668 void
13669 dump_glyph_row (row, vpos, glyphs)
13670 struct glyph_row *row;
13671 int vpos, glyphs;
13672 {
13673 if (glyphs != 1)
13674 {
13675 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13676 fprintf (stderr, "=======================================================================\n");
13677
13678 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13679 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13680 vpos,
13681 MATRIX_ROW_START_CHARPOS (row),
13682 MATRIX_ROW_END_CHARPOS (row),
13683 row->used[TEXT_AREA],
13684 row->contains_overlapping_glyphs_p,
13685 row->enabled_p,
13686 row->truncated_on_left_p,
13687 row->truncated_on_right_p,
13688 row->overlay_arrow_p,
13689 row->continued_p,
13690 MATRIX_ROW_CONTINUATION_LINE_P (row),
13691 row->displays_text_p,
13692 row->ends_at_zv_p,
13693 row->fill_line_p,
13694 row->ends_in_middle_of_char_p,
13695 row->starts_in_middle_of_char_p,
13696 row->mouse_face_p,
13697 row->x,
13698 row->y,
13699 row->pixel_width,
13700 row->height,
13701 row->visible_height,
13702 row->ascent,
13703 row->phys_ascent);
13704 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13705 row->end.overlay_string_index,
13706 row->continuation_lines_width);
13707 fprintf (stderr, "%9d %5d\n",
13708 CHARPOS (row->start.string_pos),
13709 CHARPOS (row->end.string_pos));
13710 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13711 row->end.dpvec_index);
13712 }
13713
13714 if (glyphs > 1)
13715 {
13716 int area;
13717
13718 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13719 {
13720 struct glyph *glyph = row->glyphs[area];
13721 struct glyph *glyph_end = glyph + row->used[area];
13722
13723 /* Glyph for a line end in text. */
13724 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13725 ++glyph_end;
13726
13727 if (glyph < glyph_end)
13728 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13729
13730 for (; glyph < glyph_end; ++glyph)
13731 dump_glyph (row, glyph, area);
13732 }
13733 }
13734 else if (glyphs == 1)
13735 {
13736 int area;
13737
13738 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13739 {
13740 char *s = (char *) alloca (row->used[area] + 1);
13741 int i;
13742
13743 for (i = 0; i < row->used[area]; ++i)
13744 {
13745 struct glyph *glyph = row->glyphs[area] + i;
13746 if (glyph->type == CHAR_GLYPH
13747 && glyph->u.ch < 0x80
13748 && glyph->u.ch >= ' ')
13749 s[i] = glyph->u.ch;
13750 else
13751 s[i] = '.';
13752 }
13753
13754 s[i] = '\0';
13755 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13756 }
13757 }
13758 }
13759
13760
13761 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13762 Sdump_glyph_matrix, 0, 1, "p",
13763 doc: /* Dump the current matrix of the selected window to stderr.
13764 Shows contents of glyph row structures. With non-nil
13765 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13766 glyphs in short form, otherwise show glyphs in long form. */)
13767 (glyphs)
13768 Lisp_Object glyphs;
13769 {
13770 struct window *w = XWINDOW (selected_window);
13771 struct buffer *buffer = XBUFFER (w->buffer);
13772
13773 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13774 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13775 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13776 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13777 fprintf (stderr, "=============================================\n");
13778 dump_glyph_matrix (w->current_matrix,
13779 NILP (glyphs) ? 0 : XINT (glyphs));
13780 return Qnil;
13781 }
13782
13783
13784 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13785 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13786 ()
13787 {
13788 struct frame *f = XFRAME (selected_frame);
13789 dump_glyph_matrix (f->current_matrix, 1);
13790 return Qnil;
13791 }
13792
13793
13794 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13795 doc: /* Dump glyph row ROW to stderr.
13796 GLYPH 0 means don't dump glyphs.
13797 GLYPH 1 means dump glyphs in short form.
13798 GLYPH > 1 or omitted means dump glyphs in long form. */)
13799 (row, glyphs)
13800 Lisp_Object row, glyphs;
13801 {
13802 struct glyph_matrix *matrix;
13803 int vpos;
13804
13805 CHECK_NUMBER (row);
13806 matrix = XWINDOW (selected_window)->current_matrix;
13807 vpos = XINT (row);
13808 if (vpos >= 0 && vpos < matrix->nrows)
13809 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13810 vpos,
13811 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13812 return Qnil;
13813 }
13814
13815
13816 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13817 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13818 GLYPH 0 means don't dump glyphs.
13819 GLYPH 1 means dump glyphs in short form.
13820 GLYPH > 1 or omitted means dump glyphs in long form. */)
13821 (row, glyphs)
13822 Lisp_Object row, glyphs;
13823 {
13824 struct frame *sf = SELECTED_FRAME ();
13825 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13826 int vpos;
13827
13828 CHECK_NUMBER (row);
13829 vpos = XINT (row);
13830 if (vpos >= 0 && vpos < m->nrows)
13831 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13832 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13833 return Qnil;
13834 }
13835
13836
13837 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13838 doc: /* Toggle tracing of redisplay.
13839 With ARG, turn tracing on if and only if ARG is positive. */)
13840 (arg)
13841 Lisp_Object arg;
13842 {
13843 if (NILP (arg))
13844 trace_redisplay_p = !trace_redisplay_p;
13845 else
13846 {
13847 arg = Fprefix_numeric_value (arg);
13848 trace_redisplay_p = XINT (arg) > 0;
13849 }
13850
13851 return Qnil;
13852 }
13853
13854
13855 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13856 doc: /* Like `format', but print result to stderr.
13857 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13858 (nargs, args)
13859 int nargs;
13860 Lisp_Object *args;
13861 {
13862 Lisp_Object s = Fformat (nargs, args);
13863 fprintf (stderr, "%s", SDATA (s));
13864 return Qnil;
13865 }
13866
13867 #endif /* GLYPH_DEBUG */
13868
13869
13870 \f
13871 /***********************************************************************
13872 Building Desired Matrix Rows
13873 ***********************************************************************/
13874
13875 /* Return a temporary glyph row holding the glyphs of an overlay
13876 arrow. Only used for non-window-redisplay windows. */
13877
13878 static struct glyph_row *
13879 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
13880 struct window *w;
13881 Lisp_Object overlay_arrow_string;
13882 {
13883 struct frame *f = XFRAME (WINDOW_FRAME (w));
13884 struct buffer *buffer = XBUFFER (w->buffer);
13885 struct buffer *old = current_buffer;
13886 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13887 int arrow_len = SCHARS (overlay_arrow_string);
13888 const unsigned char *arrow_end = arrow_string + arrow_len;
13889 const unsigned char *p;
13890 struct it it;
13891 int multibyte_p;
13892 int n_glyphs_before;
13893
13894 set_buffer_temp (buffer);
13895 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13896 it.glyph_row->used[TEXT_AREA] = 0;
13897 SET_TEXT_POS (it.position, 0, 0);
13898
13899 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13900 p = arrow_string;
13901 while (p < arrow_end)
13902 {
13903 Lisp_Object face, ilisp;
13904
13905 /* Get the next character. */
13906 if (multibyte_p)
13907 it.c = string_char_and_length (p, arrow_len, &it.len);
13908 else
13909 it.c = *p, it.len = 1;
13910 p += it.len;
13911
13912 /* Get its face. */
13913 ilisp = make_number (p - arrow_string);
13914 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
13915 it.face_id = compute_char_face (f, it.c, face);
13916
13917 /* Compute its width, get its glyphs. */
13918 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13919 SET_TEXT_POS (it.position, -1, -1);
13920 PRODUCE_GLYPHS (&it);
13921
13922 /* If this character doesn't fit any more in the line, we have
13923 to remove some glyphs. */
13924 if (it.current_x > it.last_visible_x)
13925 {
13926 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13927 break;
13928 }
13929 }
13930
13931 set_buffer_temp (old);
13932 return it.glyph_row;
13933 }
13934
13935
13936 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13937 glyphs are only inserted for terminal frames since we can't really
13938 win with truncation glyphs when partially visible glyphs are
13939 involved. Which glyphs to insert is determined by
13940 produce_special_glyphs. */
13941
13942 static void
13943 insert_left_trunc_glyphs (it)
13944 struct it *it;
13945 {
13946 struct it truncate_it;
13947 struct glyph *from, *end, *to, *toend;
13948
13949 xassert (!FRAME_WINDOW_P (it->f));
13950
13951 /* Get the truncation glyphs. */
13952 truncate_it = *it;
13953 truncate_it.current_x = 0;
13954 truncate_it.face_id = DEFAULT_FACE_ID;
13955 truncate_it.glyph_row = &scratch_glyph_row;
13956 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13957 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13958 truncate_it.object = make_number (0);
13959 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13960
13961 /* Overwrite glyphs from IT with truncation glyphs. */
13962 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13963 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13964 to = it->glyph_row->glyphs[TEXT_AREA];
13965 toend = to + it->glyph_row->used[TEXT_AREA];
13966
13967 while (from < end)
13968 *to++ = *from++;
13969
13970 /* There may be padding glyphs left over. Overwrite them too. */
13971 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13972 {
13973 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13974 while (from < end)
13975 *to++ = *from++;
13976 }
13977
13978 if (to > toend)
13979 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13980 }
13981
13982
13983 /* Compute the pixel height and width of IT->glyph_row.
13984
13985 Most of the time, ascent and height of a display line will be equal
13986 to the max_ascent and max_height values of the display iterator
13987 structure. This is not the case if
13988
13989 1. We hit ZV without displaying anything. In this case, max_ascent
13990 and max_height will be zero.
13991
13992 2. We have some glyphs that don't contribute to the line height.
13993 (The glyph row flag contributes_to_line_height_p is for future
13994 pixmap extensions).
13995
13996 The first case is easily covered by using default values because in
13997 these cases, the line height does not really matter, except that it
13998 must not be zero. */
13999
14000 static void
14001 compute_line_metrics (it)
14002 struct it *it;
14003 {
14004 struct glyph_row *row = it->glyph_row;
14005 int area, i;
14006
14007 if (FRAME_WINDOW_P (it->f))
14008 {
14009 int i, min_y, max_y;
14010
14011 /* The line may consist of one space only, that was added to
14012 place the cursor on it. If so, the row's height hasn't been
14013 computed yet. */
14014 if (row->height == 0)
14015 {
14016 if (it->max_ascent + it->max_descent == 0)
14017 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14018 row->ascent = it->max_ascent;
14019 row->height = it->max_ascent + it->max_descent;
14020 row->phys_ascent = it->max_phys_ascent;
14021 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14022 }
14023
14024 /* Compute the width of this line. */
14025 row->pixel_width = row->x;
14026 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14027 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14028
14029 xassert (row->pixel_width >= 0);
14030 xassert (row->ascent >= 0 && row->height > 0);
14031
14032 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14033 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14034
14035 /* If first line's physical ascent is larger than its logical
14036 ascent, use the physical ascent, and make the row taller.
14037 This makes accented characters fully visible. */
14038 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14039 && row->phys_ascent > row->ascent)
14040 {
14041 row->height += row->phys_ascent - row->ascent;
14042 row->ascent = row->phys_ascent;
14043 }
14044
14045 /* Compute how much of the line is visible. */
14046 row->visible_height = row->height;
14047
14048 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14049 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14050
14051 if (row->y < min_y)
14052 row->visible_height -= min_y - row->y;
14053 if (row->y + row->height > max_y)
14054 row->visible_height -= row->y + row->height - max_y;
14055 }
14056 else
14057 {
14058 row->pixel_width = row->used[TEXT_AREA];
14059 if (row->continued_p)
14060 row->pixel_width -= it->continuation_pixel_width;
14061 else if (row->truncated_on_right_p)
14062 row->pixel_width -= it->truncation_pixel_width;
14063 row->ascent = row->phys_ascent = 0;
14064 row->height = row->phys_height = row->visible_height = 1;
14065 }
14066
14067 /* Compute a hash code for this row. */
14068 row->hash = 0;
14069 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14070 for (i = 0; i < row->used[area]; ++i)
14071 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14072 + row->glyphs[area][i].u.val
14073 + row->glyphs[area][i].face_id
14074 + row->glyphs[area][i].padding_p
14075 + (row->glyphs[area][i].type << 2));
14076
14077 it->max_ascent = it->max_descent = 0;
14078 it->max_phys_ascent = it->max_phys_descent = 0;
14079 }
14080
14081
14082 /* Append one space to the glyph row of iterator IT if doing a
14083 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14084 space have the default face, otherwise let it have the same face as
14085 IT->face_id. Value is non-zero if a space was added.
14086
14087 This function is called to make sure that there is always one glyph
14088 at the end of a glyph row that the cursor can be set on under
14089 window-systems. (If there weren't such a glyph we would not know
14090 how wide and tall a box cursor should be displayed).
14091
14092 At the same time this space let's a nicely handle clearing to the
14093 end of the line if the row ends in italic text. */
14094
14095 static int
14096 append_space (it, default_face_p)
14097 struct it *it;
14098 int default_face_p;
14099 {
14100 if (FRAME_WINDOW_P (it->f))
14101 {
14102 int n = it->glyph_row->used[TEXT_AREA];
14103
14104 if (it->glyph_row->glyphs[TEXT_AREA] + n
14105 < it->glyph_row->glyphs[1 + TEXT_AREA])
14106 {
14107 /* Save some values that must not be changed.
14108 Must save IT->c and IT->len because otherwise
14109 ITERATOR_AT_END_P wouldn't work anymore after
14110 append_space has been called. */
14111 enum display_element_type saved_what = it->what;
14112 int saved_c = it->c, saved_len = it->len;
14113 int saved_x = it->current_x;
14114 int saved_face_id = it->face_id;
14115 struct text_pos saved_pos;
14116 Lisp_Object saved_object;
14117 struct face *face;
14118
14119 saved_object = it->object;
14120 saved_pos = it->position;
14121
14122 it->what = IT_CHARACTER;
14123 bzero (&it->position, sizeof it->position);
14124 it->object = make_number (0);
14125 it->c = ' ';
14126 it->len = 1;
14127
14128 if (default_face_p)
14129 it->face_id = DEFAULT_FACE_ID;
14130 else if (it->face_before_selective_p)
14131 it->face_id = it->saved_face_id;
14132 face = FACE_FROM_ID (it->f, it->face_id);
14133 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14134
14135 PRODUCE_GLYPHS (it);
14136
14137 it->current_x = saved_x;
14138 it->object = saved_object;
14139 it->position = saved_pos;
14140 it->what = saved_what;
14141 it->face_id = saved_face_id;
14142 it->len = saved_len;
14143 it->c = saved_c;
14144 return 1;
14145 }
14146 }
14147
14148 return 0;
14149 }
14150
14151
14152 /* Extend the face of the last glyph in the text area of IT->glyph_row
14153 to the end of the display line. Called from display_line.
14154 If the glyph row is empty, add a space glyph to it so that we
14155 know the face to draw. Set the glyph row flag fill_line_p. */
14156
14157 static void
14158 extend_face_to_end_of_line (it)
14159 struct it *it;
14160 {
14161 struct face *face;
14162 struct frame *f = it->f;
14163
14164 /* If line is already filled, do nothing. */
14165 if (it->current_x >= it->last_visible_x)
14166 return;
14167
14168 /* Face extension extends the background and box of IT->face_id
14169 to the end of the line. If the background equals the background
14170 of the frame, we don't have to do anything. */
14171 if (it->face_before_selective_p)
14172 face = FACE_FROM_ID (it->f, it->saved_face_id);
14173 else
14174 face = FACE_FROM_ID (f, it->face_id);
14175
14176 if (FRAME_WINDOW_P (f)
14177 && face->box == FACE_NO_BOX
14178 && face->background == FRAME_BACKGROUND_PIXEL (f)
14179 && !face->stipple)
14180 return;
14181
14182 /* Set the glyph row flag indicating that the face of the last glyph
14183 in the text area has to be drawn to the end of the text area. */
14184 it->glyph_row->fill_line_p = 1;
14185
14186 /* If current character of IT is not ASCII, make sure we have the
14187 ASCII face. This will be automatically undone the next time
14188 get_next_display_element returns a multibyte character. Note
14189 that the character will always be single byte in unibyte text. */
14190 if (!SINGLE_BYTE_CHAR_P (it->c))
14191 {
14192 it->face_id = FACE_FOR_CHAR (f, face, 0);
14193 }
14194
14195 if (FRAME_WINDOW_P (f))
14196 {
14197 /* If the row is empty, add a space with the current face of IT,
14198 so that we know which face to draw. */
14199 if (it->glyph_row->used[TEXT_AREA] == 0)
14200 {
14201 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14202 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14203 it->glyph_row->used[TEXT_AREA] = 1;
14204 }
14205 }
14206 else
14207 {
14208 /* Save some values that must not be changed. */
14209 int saved_x = it->current_x;
14210 struct text_pos saved_pos;
14211 Lisp_Object saved_object;
14212 enum display_element_type saved_what = it->what;
14213 int saved_face_id = it->face_id;
14214
14215 saved_object = it->object;
14216 saved_pos = it->position;
14217
14218 it->what = IT_CHARACTER;
14219 bzero (&it->position, sizeof it->position);
14220 it->object = make_number (0);
14221 it->c = ' ';
14222 it->len = 1;
14223 it->face_id = face->id;
14224
14225 PRODUCE_GLYPHS (it);
14226
14227 while (it->current_x <= it->last_visible_x)
14228 PRODUCE_GLYPHS (it);
14229
14230 /* Don't count these blanks really. It would let us insert a left
14231 truncation glyph below and make us set the cursor on them, maybe. */
14232 it->current_x = saved_x;
14233 it->object = saved_object;
14234 it->position = saved_pos;
14235 it->what = saved_what;
14236 it->face_id = saved_face_id;
14237 }
14238 }
14239
14240
14241 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14242 trailing whitespace. */
14243
14244 static int
14245 trailing_whitespace_p (charpos)
14246 int charpos;
14247 {
14248 int bytepos = CHAR_TO_BYTE (charpos);
14249 int c = 0;
14250
14251 while (bytepos < ZV_BYTE
14252 && (c = FETCH_CHAR (bytepos),
14253 c == ' ' || c == '\t'))
14254 ++bytepos;
14255
14256 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14257 {
14258 if (bytepos != PT_BYTE)
14259 return 1;
14260 }
14261 return 0;
14262 }
14263
14264
14265 /* Highlight trailing whitespace, if any, in ROW. */
14266
14267 void
14268 highlight_trailing_whitespace (f, row)
14269 struct frame *f;
14270 struct glyph_row *row;
14271 {
14272 int used = row->used[TEXT_AREA];
14273
14274 if (used)
14275 {
14276 struct glyph *start = row->glyphs[TEXT_AREA];
14277 struct glyph *glyph = start + used - 1;
14278
14279 /* Skip over glyphs inserted to display the cursor at the
14280 end of a line, for extending the face of the last glyph
14281 to the end of the line on terminals, and for truncation
14282 and continuation glyphs. */
14283 while (glyph >= start
14284 && glyph->type == CHAR_GLYPH
14285 && INTEGERP (glyph->object))
14286 --glyph;
14287
14288 /* If last glyph is a space or stretch, and it's trailing
14289 whitespace, set the face of all trailing whitespace glyphs in
14290 IT->glyph_row to `trailing-whitespace'. */
14291 if (glyph >= start
14292 && BUFFERP (glyph->object)
14293 && (glyph->type == STRETCH_GLYPH
14294 || (glyph->type == CHAR_GLYPH
14295 && glyph->u.ch == ' '))
14296 && trailing_whitespace_p (glyph->charpos))
14297 {
14298 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14299
14300 while (glyph >= start
14301 && BUFFERP (glyph->object)
14302 && (glyph->type == STRETCH_GLYPH
14303 || (glyph->type == CHAR_GLYPH
14304 && glyph->u.ch == ' ')))
14305 (glyph--)->face_id = face_id;
14306 }
14307 }
14308 }
14309
14310
14311 /* Value is non-zero if glyph row ROW in window W should be
14312 used to hold the cursor. */
14313
14314 static int
14315 cursor_row_p (w, row)
14316 struct window *w;
14317 struct glyph_row *row;
14318 {
14319 int cursor_row_p = 1;
14320
14321 if (PT == MATRIX_ROW_END_CHARPOS (row))
14322 {
14323 /* If the row ends with a newline from a string, we don't want
14324 the cursor there (if the row is continued it doesn't end in a
14325 newline). */
14326 if (CHARPOS (row->end.string_pos) >= 0
14327 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14328 cursor_row_p = row->continued_p;
14329
14330 /* If the row ends at ZV, display the cursor at the end of that
14331 row instead of at the start of the row below. */
14332 else if (row->ends_at_zv_p)
14333 cursor_row_p = 1;
14334 else
14335 cursor_row_p = 0;
14336 }
14337
14338 return cursor_row_p;
14339 }
14340
14341
14342 /* Construct the glyph row IT->glyph_row in the desired matrix of
14343 IT->w from text at the current position of IT. See dispextern.h
14344 for an overview of struct it. Value is non-zero if
14345 IT->glyph_row displays text, as opposed to a line displaying ZV
14346 only. */
14347
14348 static int
14349 display_line (it)
14350 struct it *it;
14351 {
14352 struct glyph_row *row = it->glyph_row;
14353 int overlay_arrow_bitmap;
14354 Lisp_Object overlay_arrow_string;
14355
14356 /* We always start displaying at hpos zero even if hscrolled. */
14357 xassert (it->hpos == 0 && it->current_x == 0);
14358
14359 /* We must not display in a row that's not a text row. */
14360 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14361 < it->w->desired_matrix->nrows);
14362
14363 /* Is IT->w showing the region? */
14364 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14365
14366 /* Clear the result glyph row and enable it. */
14367 prepare_desired_row (row);
14368
14369 row->y = it->current_y;
14370 row->start = it->start;
14371 row->continuation_lines_width = it->continuation_lines_width;
14372 row->displays_text_p = 1;
14373 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14374 it->starts_in_middle_of_char_p = 0;
14375
14376 /* Arrange the overlays nicely for our purposes. Usually, we call
14377 display_line on only one line at a time, in which case this
14378 can't really hurt too much, or we call it on lines which appear
14379 one after another in the buffer, in which case all calls to
14380 recenter_overlay_lists but the first will be pretty cheap. */
14381 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14382
14383 /* Move over display elements that are not visible because we are
14384 hscrolled. This may stop at an x-position < IT->first_visible_x
14385 if the first glyph is partially visible or if we hit a line end. */
14386 if (it->current_x < it->first_visible_x)
14387 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14388 MOVE_TO_POS | MOVE_TO_X);
14389
14390 /* Get the initial row height. This is either the height of the
14391 text hscrolled, if there is any, or zero. */
14392 row->ascent = it->max_ascent;
14393 row->height = it->max_ascent + it->max_descent;
14394 row->phys_ascent = it->max_phys_ascent;
14395 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14396
14397 /* Loop generating characters. The loop is left with IT on the next
14398 character to display. */
14399 while (1)
14400 {
14401 int n_glyphs_before, hpos_before, x_before;
14402 int x, i, nglyphs;
14403 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14404
14405 /* Retrieve the next thing to display. Value is zero if end of
14406 buffer reached. */
14407 if (!get_next_display_element (it))
14408 {
14409 /* Maybe add a space at the end of this line that is used to
14410 display the cursor there under X. Set the charpos of the
14411 first glyph of blank lines not corresponding to any text
14412 to -1. */
14413 #ifdef HAVE_WINDOW_SYSTEM
14414 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14415 row->exact_window_width_line_p = 1;
14416 else
14417 #endif /* HAVE_WINDOW_SYSTEM */
14418 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14419 || row->used[TEXT_AREA] == 0)
14420 {
14421 row->glyphs[TEXT_AREA]->charpos = -1;
14422 row->displays_text_p = 0;
14423
14424 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14425 && (!MINI_WINDOW_P (it->w)
14426 || (minibuf_level && EQ (it->window, minibuf_window))))
14427 row->indicate_empty_line_p = 1;
14428 }
14429
14430 it->continuation_lines_width = 0;
14431 row->ends_at_zv_p = 1;
14432 break;
14433 }
14434
14435 /* Now, get the metrics of what we want to display. This also
14436 generates glyphs in `row' (which is IT->glyph_row). */
14437 n_glyphs_before = row->used[TEXT_AREA];
14438 x = it->current_x;
14439
14440 /* Remember the line height so far in case the next element doesn't
14441 fit on the line. */
14442 if (!it->truncate_lines_p)
14443 {
14444 ascent = it->max_ascent;
14445 descent = it->max_descent;
14446 phys_ascent = it->max_phys_ascent;
14447 phys_descent = it->max_phys_descent;
14448 }
14449
14450 PRODUCE_GLYPHS (it);
14451
14452 /* If this display element was in marginal areas, continue with
14453 the next one. */
14454 if (it->area != TEXT_AREA)
14455 {
14456 row->ascent = max (row->ascent, it->max_ascent);
14457 row->height = max (row->height, it->max_ascent + it->max_descent);
14458 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14459 row->phys_height = max (row->phys_height,
14460 it->max_phys_ascent + it->max_phys_descent);
14461 set_iterator_to_next (it, 1);
14462 continue;
14463 }
14464
14465 /* Does the display element fit on the line? If we truncate
14466 lines, we should draw past the right edge of the window. If
14467 we don't truncate, we want to stop so that we can display the
14468 continuation glyph before the right margin. If lines are
14469 continued, there are two possible strategies for characters
14470 resulting in more than 1 glyph (e.g. tabs): Display as many
14471 glyphs as possible in this line and leave the rest for the
14472 continuation line, or display the whole element in the next
14473 line. Original redisplay did the former, so we do it also. */
14474 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14475 hpos_before = it->hpos;
14476 x_before = x;
14477
14478 if (/* Not a newline. */
14479 nglyphs > 0
14480 /* Glyphs produced fit entirely in the line. */
14481 && it->current_x < it->last_visible_x)
14482 {
14483 it->hpos += nglyphs;
14484 row->ascent = max (row->ascent, it->max_ascent);
14485 row->height = max (row->height, it->max_ascent + it->max_descent);
14486 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14487 row->phys_height = max (row->phys_height,
14488 it->max_phys_ascent + it->max_phys_descent);
14489 if (it->current_x - it->pixel_width < it->first_visible_x)
14490 row->x = x - it->first_visible_x;
14491 }
14492 else
14493 {
14494 int new_x;
14495 struct glyph *glyph;
14496
14497 for (i = 0; i < nglyphs; ++i, x = new_x)
14498 {
14499 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14500 new_x = x + glyph->pixel_width;
14501
14502 if (/* Lines are continued. */
14503 !it->truncate_lines_p
14504 && (/* Glyph doesn't fit on the line. */
14505 new_x > it->last_visible_x
14506 /* Or it fits exactly on a window system frame. */
14507 || (new_x == it->last_visible_x
14508 && FRAME_WINDOW_P (it->f))))
14509 {
14510 /* End of a continued line. */
14511
14512 if (it->hpos == 0
14513 || (new_x == it->last_visible_x
14514 && FRAME_WINDOW_P (it->f)))
14515 {
14516 /* Current glyph is the only one on the line or
14517 fits exactly on the line. We must continue
14518 the line because we can't draw the cursor
14519 after the glyph. */
14520 row->continued_p = 1;
14521 it->current_x = new_x;
14522 it->continuation_lines_width += new_x;
14523 ++it->hpos;
14524 if (i == nglyphs - 1)
14525 {
14526 set_iterator_to_next (it, 1);
14527 #ifdef HAVE_WINDOW_SYSTEM
14528 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14529 {
14530 if (!get_next_display_element (it))
14531 {
14532 row->exact_window_width_line_p = 1;
14533 it->continuation_lines_width = 0;
14534 row->continued_p = 0;
14535 row->ends_at_zv_p = 1;
14536 }
14537 else if (ITERATOR_AT_END_OF_LINE_P (it))
14538 {
14539 row->continued_p = 0;
14540 row->exact_window_width_line_p = 1;
14541 }
14542 }
14543 #endif /* HAVE_WINDOW_SYSTEM */
14544 }
14545 }
14546 else if (CHAR_GLYPH_PADDING_P (*glyph)
14547 && !FRAME_WINDOW_P (it->f))
14548 {
14549 /* A padding glyph that doesn't fit on this line.
14550 This means the whole character doesn't fit
14551 on the line. */
14552 row->used[TEXT_AREA] = n_glyphs_before;
14553
14554 /* Fill the rest of the row with continuation
14555 glyphs like in 20.x. */
14556 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14557 < row->glyphs[1 + TEXT_AREA])
14558 produce_special_glyphs (it, IT_CONTINUATION);
14559
14560 row->continued_p = 1;
14561 it->current_x = x_before;
14562 it->continuation_lines_width += x_before;
14563
14564 /* Restore the height to what it was before the
14565 element not fitting on the line. */
14566 it->max_ascent = ascent;
14567 it->max_descent = descent;
14568 it->max_phys_ascent = phys_ascent;
14569 it->max_phys_descent = phys_descent;
14570 }
14571 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14572 {
14573 /* A TAB that extends past the right edge of the
14574 window. This produces a single glyph on
14575 window system frames. We leave the glyph in
14576 this row and let it fill the row, but don't
14577 consume the TAB. */
14578 it->continuation_lines_width += it->last_visible_x;
14579 row->ends_in_middle_of_char_p = 1;
14580 row->continued_p = 1;
14581 glyph->pixel_width = it->last_visible_x - x;
14582 it->starts_in_middle_of_char_p = 1;
14583 }
14584 else
14585 {
14586 /* Something other than a TAB that draws past
14587 the right edge of the window. Restore
14588 positions to values before the element. */
14589 row->used[TEXT_AREA] = n_glyphs_before + i;
14590
14591 /* Display continuation glyphs. */
14592 if (!FRAME_WINDOW_P (it->f))
14593 produce_special_glyphs (it, IT_CONTINUATION);
14594 row->continued_p = 1;
14595
14596 it->continuation_lines_width += x;
14597
14598 if (nglyphs > 1 && i > 0)
14599 {
14600 row->ends_in_middle_of_char_p = 1;
14601 it->starts_in_middle_of_char_p = 1;
14602 }
14603
14604 /* Restore the height to what it was before the
14605 element not fitting on the line. */
14606 it->max_ascent = ascent;
14607 it->max_descent = descent;
14608 it->max_phys_ascent = phys_ascent;
14609 it->max_phys_descent = phys_descent;
14610 }
14611
14612 break;
14613 }
14614 else if (new_x > it->first_visible_x)
14615 {
14616 /* Increment number of glyphs actually displayed. */
14617 ++it->hpos;
14618
14619 if (x < it->first_visible_x)
14620 /* Glyph is partially visible, i.e. row starts at
14621 negative X position. */
14622 row->x = x - it->first_visible_x;
14623 }
14624 else
14625 {
14626 /* Glyph is completely off the left margin of the
14627 window. This should not happen because of the
14628 move_it_in_display_line at the start of this
14629 function, unless the text display area of the
14630 window is empty. */
14631 xassert (it->first_visible_x <= it->last_visible_x);
14632 }
14633 }
14634
14635 row->ascent = max (row->ascent, it->max_ascent);
14636 row->height = max (row->height, it->max_ascent + it->max_descent);
14637 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14638 row->phys_height = max (row->phys_height,
14639 it->max_phys_ascent + it->max_phys_descent);
14640
14641 /* End of this display line if row is continued. */
14642 if (row->continued_p || row->ends_at_zv_p)
14643 break;
14644 }
14645
14646 at_end_of_line:
14647 /* Is this a line end? If yes, we're also done, after making
14648 sure that a non-default face is extended up to the right
14649 margin of the window. */
14650 if (ITERATOR_AT_END_OF_LINE_P (it))
14651 {
14652 int used_before = row->used[TEXT_AREA];
14653
14654 row->ends_in_newline_from_string_p = STRINGP (it->object);
14655
14656 #ifdef HAVE_WINDOW_SYSTEM
14657 /* Add a space at the end of the line that is used to
14658 display the cursor there. */
14659 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14660 append_space (it, 0);
14661 #endif /* HAVE_WINDOW_SYSTEM */
14662
14663 /* Extend the face to the end of the line. */
14664 extend_face_to_end_of_line (it);
14665
14666 /* Make sure we have the position. */
14667 if (used_before == 0)
14668 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14669
14670 /* Consume the line end. This skips over invisible lines. */
14671 set_iterator_to_next (it, 1);
14672 it->continuation_lines_width = 0;
14673 break;
14674 }
14675
14676 /* Proceed with next display element. Note that this skips
14677 over lines invisible because of selective display. */
14678 set_iterator_to_next (it, 1);
14679
14680 /* If we truncate lines, we are done when the last displayed
14681 glyphs reach past the right margin of the window. */
14682 if (it->truncate_lines_p
14683 && (FRAME_WINDOW_P (it->f)
14684 ? (it->current_x >= it->last_visible_x)
14685 : (it->current_x > it->last_visible_x)))
14686 {
14687 /* Maybe add truncation glyphs. */
14688 if (!FRAME_WINDOW_P (it->f))
14689 {
14690 int i, n;
14691
14692 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14693 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14694 break;
14695
14696 for (n = row->used[TEXT_AREA]; i < n; ++i)
14697 {
14698 row->used[TEXT_AREA] = i;
14699 produce_special_glyphs (it, IT_TRUNCATION);
14700 }
14701 }
14702 #ifdef HAVE_WINDOW_SYSTEM
14703 else
14704 {
14705 /* Don't truncate if we can overflow newline into fringe. */
14706 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14707 {
14708 if (!get_next_display_element (it))
14709 {
14710 #ifdef HAVE_WINDOW_SYSTEM
14711 it->continuation_lines_width = 0;
14712 row->ends_at_zv_p = 1;
14713 row->exact_window_width_line_p = 1;
14714 break;
14715 #endif /* HAVE_WINDOW_SYSTEM */
14716 }
14717 if (ITERATOR_AT_END_OF_LINE_P (it))
14718 {
14719 row->exact_window_width_line_p = 1;
14720 goto at_end_of_line;
14721 }
14722 }
14723 }
14724 #endif /* HAVE_WINDOW_SYSTEM */
14725
14726 row->truncated_on_right_p = 1;
14727 it->continuation_lines_width = 0;
14728 reseat_at_next_visible_line_start (it, 0);
14729 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14730 it->hpos = hpos_before;
14731 it->current_x = x_before;
14732 break;
14733 }
14734 }
14735
14736 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14737 at the left window margin. */
14738 if (it->first_visible_x
14739 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14740 {
14741 if (!FRAME_WINDOW_P (it->f))
14742 insert_left_trunc_glyphs (it);
14743 row->truncated_on_left_p = 1;
14744 }
14745
14746 /* If the start of this line is the overlay arrow-position, then
14747 mark this glyph row as the one containing the overlay arrow.
14748 This is clearly a mess with variable size fonts. It would be
14749 better to let it be displayed like cursors under X. */
14750 if (! overlay_arrow_seen
14751 && (overlay_arrow_string
14752 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14753 !NILP (overlay_arrow_string)))
14754 {
14755 /* Overlay arrow in window redisplay is a fringe bitmap. */
14756 if (!FRAME_WINDOW_P (it->f))
14757 {
14758 struct glyph_row *arrow_row
14759 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14760 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14761 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14762 struct glyph *p = row->glyphs[TEXT_AREA];
14763 struct glyph *p2, *end;
14764
14765 /* Copy the arrow glyphs. */
14766 while (glyph < arrow_end)
14767 *p++ = *glyph++;
14768
14769 /* Throw away padding glyphs. */
14770 p2 = p;
14771 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14772 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14773 ++p2;
14774 if (p2 > p)
14775 {
14776 while (p2 < end)
14777 *p++ = *p2++;
14778 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14779 }
14780 }
14781
14782 overlay_arrow_seen = 1;
14783 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14784 row->overlay_arrow_p = 1;
14785 }
14786
14787 /* Compute pixel dimensions of this line. */
14788 compute_line_metrics (it);
14789
14790 /* Remember the position at which this line ends. */
14791 row->end = it->current;
14792
14793 /* Save fringe bitmaps in this row. */
14794 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14795 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14796 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14797 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14798
14799 it->left_user_fringe_bitmap = 0;
14800 it->left_user_fringe_face_id = 0;
14801 it->right_user_fringe_bitmap = 0;
14802 it->right_user_fringe_face_id = 0;
14803
14804 /* Maybe set the cursor. */
14805 if (it->w->cursor.vpos < 0
14806 && PT >= MATRIX_ROW_START_CHARPOS (row)
14807 && PT <= MATRIX_ROW_END_CHARPOS (row)
14808 && cursor_row_p (it->w, row))
14809 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14810
14811 /* Highlight trailing whitespace. */
14812 if (!NILP (Vshow_trailing_whitespace))
14813 highlight_trailing_whitespace (it->f, it->glyph_row);
14814
14815 /* Prepare for the next line. This line starts horizontally at (X
14816 HPOS) = (0 0). Vertical positions are incremented. As a
14817 convenience for the caller, IT->glyph_row is set to the next
14818 row to be used. */
14819 it->current_x = it->hpos = 0;
14820 it->current_y += row->height;
14821 ++it->vpos;
14822 ++it->glyph_row;
14823 it->start = it->current;
14824 return row->displays_text_p;
14825 }
14826
14827
14828 \f
14829 /***********************************************************************
14830 Menu Bar
14831 ***********************************************************************/
14832
14833 /* Redisplay the menu bar in the frame for window W.
14834
14835 The menu bar of X frames that don't have X toolkit support is
14836 displayed in a special window W->frame->menu_bar_window.
14837
14838 The menu bar of terminal frames is treated specially as far as
14839 glyph matrices are concerned. Menu bar lines are not part of
14840 windows, so the update is done directly on the frame matrix rows
14841 for the menu bar. */
14842
14843 static void
14844 display_menu_bar (w)
14845 struct window *w;
14846 {
14847 struct frame *f = XFRAME (WINDOW_FRAME (w));
14848 struct it it;
14849 Lisp_Object items;
14850 int i;
14851
14852 /* Don't do all this for graphical frames. */
14853 #ifdef HAVE_NTGUI
14854 if (!NILP (Vwindow_system))
14855 return;
14856 #endif
14857 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14858 if (FRAME_X_P (f))
14859 return;
14860 #endif
14861 #ifdef MAC_OS
14862 if (FRAME_MAC_P (f))
14863 return;
14864 #endif
14865
14866 #ifdef USE_X_TOOLKIT
14867 xassert (!FRAME_WINDOW_P (f));
14868 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14869 it.first_visible_x = 0;
14870 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14871 #else /* not USE_X_TOOLKIT */
14872 if (FRAME_WINDOW_P (f))
14873 {
14874 /* Menu bar lines are displayed in the desired matrix of the
14875 dummy window menu_bar_window. */
14876 struct window *menu_w;
14877 xassert (WINDOWP (f->menu_bar_window));
14878 menu_w = XWINDOW (f->menu_bar_window);
14879 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14880 MENU_FACE_ID);
14881 it.first_visible_x = 0;
14882 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14883 }
14884 else
14885 {
14886 /* This is a TTY frame, i.e. character hpos/vpos are used as
14887 pixel x/y. */
14888 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14889 MENU_FACE_ID);
14890 it.first_visible_x = 0;
14891 it.last_visible_x = FRAME_COLS (f);
14892 }
14893 #endif /* not USE_X_TOOLKIT */
14894
14895 if (! mode_line_inverse_video)
14896 /* Force the menu-bar to be displayed in the default face. */
14897 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14898
14899 /* Clear all rows of the menu bar. */
14900 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14901 {
14902 struct glyph_row *row = it.glyph_row + i;
14903 clear_glyph_row (row);
14904 row->enabled_p = 1;
14905 row->full_width_p = 1;
14906 }
14907
14908 /* Display all items of the menu bar. */
14909 items = FRAME_MENU_BAR_ITEMS (it.f);
14910 for (i = 0; i < XVECTOR (items)->size; i += 4)
14911 {
14912 Lisp_Object string;
14913
14914 /* Stop at nil string. */
14915 string = AREF (items, i + 1);
14916 if (NILP (string))
14917 break;
14918
14919 /* Remember where item was displayed. */
14920 AREF (items, i + 3) = make_number (it.hpos);
14921
14922 /* Display the item, pad with one space. */
14923 if (it.current_x < it.last_visible_x)
14924 display_string (NULL, string, Qnil, 0, 0, &it,
14925 SCHARS (string) + 1, 0, 0, -1);
14926 }
14927
14928 /* Fill out the line with spaces. */
14929 if (it.current_x < it.last_visible_x)
14930 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14931
14932 /* Compute the total height of the lines. */
14933 compute_line_metrics (&it);
14934 }
14935
14936
14937 \f
14938 /***********************************************************************
14939 Mode Line
14940 ***********************************************************************/
14941
14942 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14943 FORCE is non-zero, redisplay mode lines unconditionally.
14944 Otherwise, redisplay only mode lines that are garbaged. Value is
14945 the number of windows whose mode lines were redisplayed. */
14946
14947 static int
14948 redisplay_mode_lines (window, force)
14949 Lisp_Object window;
14950 int force;
14951 {
14952 int nwindows = 0;
14953
14954 while (!NILP (window))
14955 {
14956 struct window *w = XWINDOW (window);
14957
14958 if (WINDOWP (w->hchild))
14959 nwindows += redisplay_mode_lines (w->hchild, force);
14960 else if (WINDOWP (w->vchild))
14961 nwindows += redisplay_mode_lines (w->vchild, force);
14962 else if (force
14963 || FRAME_GARBAGED_P (XFRAME (w->frame))
14964 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14965 {
14966 struct text_pos lpoint;
14967 struct buffer *old = current_buffer;
14968
14969 /* Set the window's buffer for the mode line display. */
14970 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14971 set_buffer_internal_1 (XBUFFER (w->buffer));
14972
14973 /* Point refers normally to the selected window. For any
14974 other window, set up appropriate value. */
14975 if (!EQ (window, selected_window))
14976 {
14977 struct text_pos pt;
14978
14979 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14980 if (CHARPOS (pt) < BEGV)
14981 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14982 else if (CHARPOS (pt) > (ZV - 1))
14983 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14984 else
14985 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14986 }
14987
14988 /* Display mode lines. */
14989 clear_glyph_matrix (w->desired_matrix);
14990 if (display_mode_lines (w))
14991 {
14992 ++nwindows;
14993 w->must_be_updated_p = 1;
14994 }
14995
14996 /* Restore old settings. */
14997 set_buffer_internal_1 (old);
14998 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14999 }
15000
15001 window = w->next;
15002 }
15003
15004 return nwindows;
15005 }
15006
15007
15008 /* Display the mode and/or top line of window W. Value is the number
15009 of mode lines displayed. */
15010
15011 static int
15012 display_mode_lines (w)
15013 struct window *w;
15014 {
15015 Lisp_Object old_selected_window, old_selected_frame;
15016 int n = 0;
15017
15018 old_selected_frame = selected_frame;
15019 selected_frame = w->frame;
15020 old_selected_window = selected_window;
15021 XSETWINDOW (selected_window, w);
15022
15023 /* These will be set while the mode line specs are processed. */
15024 line_number_displayed = 0;
15025 w->column_number_displayed = Qnil;
15026
15027 if (WINDOW_WANTS_MODELINE_P (w))
15028 {
15029 struct window *sel_w = XWINDOW (old_selected_window);
15030
15031 /* Select mode line face based on the real selected window. */
15032 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15033 current_buffer->mode_line_format);
15034 ++n;
15035 }
15036
15037 if (WINDOW_WANTS_HEADER_LINE_P (w))
15038 {
15039 display_mode_line (w, HEADER_LINE_FACE_ID,
15040 current_buffer->header_line_format);
15041 ++n;
15042 }
15043
15044 selected_frame = old_selected_frame;
15045 selected_window = old_selected_window;
15046 return n;
15047 }
15048
15049
15050 /* Display mode or top line of window W. FACE_ID specifies which line
15051 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15052 FORMAT is the mode line format to display. Value is the pixel
15053 height of the mode line displayed. */
15054
15055 static int
15056 display_mode_line (w, face_id, format)
15057 struct window *w;
15058 enum face_id face_id;
15059 Lisp_Object format;
15060 {
15061 struct it it;
15062 struct face *face;
15063
15064 init_iterator (&it, w, -1, -1, NULL, face_id);
15065 prepare_desired_row (it.glyph_row);
15066
15067 if (! mode_line_inverse_video)
15068 /* Force the mode-line to be displayed in the default face. */
15069 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15070
15071 /* Temporarily make frame's keyboard the current kboard so that
15072 kboard-local variables in the mode_line_format will get the right
15073 values. */
15074 push_frame_kboard (it.f);
15075 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15076 pop_frame_kboard ();
15077
15078 /* Fill up with spaces. */
15079 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15080
15081 compute_line_metrics (&it);
15082 it.glyph_row->full_width_p = 1;
15083 it.glyph_row->mode_line_p = 1;
15084 it.glyph_row->continued_p = 0;
15085 it.glyph_row->truncated_on_left_p = 0;
15086 it.glyph_row->truncated_on_right_p = 0;
15087
15088 /* Make a 3D mode-line have a shadow at its right end. */
15089 face = FACE_FROM_ID (it.f, face_id);
15090 extend_face_to_end_of_line (&it);
15091 if (face->box != FACE_NO_BOX)
15092 {
15093 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15094 + it.glyph_row->used[TEXT_AREA] - 1);
15095 last->right_box_line_p = 1;
15096 }
15097
15098 return it.glyph_row->height;
15099 }
15100
15101 /* Alist that caches the results of :propertize.
15102 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15103 Lisp_Object mode_line_proptrans_alist;
15104
15105 /* List of strings making up the mode-line. */
15106 Lisp_Object mode_line_string_list;
15107
15108 /* Base face property when building propertized mode line string. */
15109 static Lisp_Object mode_line_string_face;
15110 static Lisp_Object mode_line_string_face_prop;
15111
15112
15113 /* Contribute ELT to the mode line for window IT->w. How it
15114 translates into text depends on its data type.
15115
15116 IT describes the display environment in which we display, as usual.
15117
15118 DEPTH is the depth in recursion. It is used to prevent
15119 infinite recursion here.
15120
15121 FIELD_WIDTH is the number of characters the display of ELT should
15122 occupy in the mode line, and PRECISION is the maximum number of
15123 characters to display from ELT's representation. See
15124 display_string for details.
15125
15126 Returns the hpos of the end of the text generated by ELT.
15127
15128 PROPS is a property list to add to any string we encounter.
15129
15130 If RISKY is nonzero, remove (disregard) any properties in any string
15131 we encounter, and ignore :eval and :propertize.
15132
15133 If the global variable `frame_title_ptr' is non-NULL, then the output
15134 is passed to `store_frame_title' instead of `display_string'. */
15135
15136 static int
15137 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15138 struct it *it;
15139 int depth;
15140 int field_width, precision;
15141 Lisp_Object elt, props;
15142 int risky;
15143 {
15144 int n = 0, field, prec;
15145 int literal = 0;
15146
15147 tail_recurse:
15148 if (depth > 100)
15149 elt = build_string ("*too-deep*");
15150
15151 depth++;
15152
15153 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15154 {
15155 case Lisp_String:
15156 {
15157 /* A string: output it and check for %-constructs within it. */
15158 unsigned char c;
15159 const unsigned char *this, *lisp_string;
15160
15161 if (!NILP (props) || risky)
15162 {
15163 Lisp_Object oprops, aelt;
15164 oprops = Ftext_properties_at (make_number (0), elt);
15165
15166 if (NILP (Fequal (props, oprops)) || risky)
15167 {
15168 /* If the starting string has properties,
15169 merge the specified ones onto the existing ones. */
15170 if (! NILP (oprops) && !risky)
15171 {
15172 Lisp_Object tem;
15173
15174 oprops = Fcopy_sequence (oprops);
15175 tem = props;
15176 while (CONSP (tem))
15177 {
15178 oprops = Fplist_put (oprops, XCAR (tem),
15179 XCAR (XCDR (tem)));
15180 tem = XCDR (XCDR (tem));
15181 }
15182 props = oprops;
15183 }
15184
15185 aelt = Fassoc (elt, mode_line_proptrans_alist);
15186 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15187 {
15188 mode_line_proptrans_alist
15189 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15190 elt = XCAR (aelt);
15191 }
15192 else
15193 {
15194 Lisp_Object tem;
15195
15196 elt = Fcopy_sequence (elt);
15197 Fset_text_properties (make_number (0), Flength (elt),
15198 props, elt);
15199 /* Add this item to mode_line_proptrans_alist. */
15200 mode_line_proptrans_alist
15201 = Fcons (Fcons (elt, props),
15202 mode_line_proptrans_alist);
15203 /* Truncate mode_line_proptrans_alist
15204 to at most 50 elements. */
15205 tem = Fnthcdr (make_number (50),
15206 mode_line_proptrans_alist);
15207 if (! NILP (tem))
15208 XSETCDR (tem, Qnil);
15209 }
15210 }
15211 }
15212
15213 this = SDATA (elt);
15214 lisp_string = this;
15215
15216 if (literal)
15217 {
15218 prec = precision - n;
15219 if (frame_title_ptr)
15220 n += store_frame_title (SDATA (elt), -1, prec);
15221 else if (!NILP (mode_line_string_list))
15222 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15223 else
15224 n += display_string (NULL, elt, Qnil, 0, 0, it,
15225 0, prec, 0, STRING_MULTIBYTE (elt));
15226
15227 break;
15228 }
15229
15230 while ((precision <= 0 || n < precision)
15231 && *this
15232 && (frame_title_ptr
15233 || !NILP (mode_line_string_list)
15234 || it->current_x < it->last_visible_x))
15235 {
15236 const unsigned char *last = this;
15237
15238 /* Advance to end of string or next format specifier. */
15239 while ((c = *this++) != '\0' && c != '%')
15240 ;
15241
15242 if (this - 1 != last)
15243 {
15244 /* Output to end of string or up to '%'. Field width
15245 is length of string. Don't output more than
15246 PRECISION allows us. */
15247 --this;
15248
15249 prec = chars_in_text (last, this - last);
15250 if (precision > 0 && prec > precision - n)
15251 prec = precision - n;
15252
15253 if (frame_title_ptr)
15254 n += store_frame_title (last, 0, prec);
15255 else if (!NILP (mode_line_string_list))
15256 {
15257 int bytepos = last - lisp_string;
15258 int charpos = string_byte_to_char (elt, bytepos);
15259 n += store_mode_line_string (NULL,
15260 Fsubstring (elt, make_number (charpos),
15261 make_number (charpos + prec)),
15262 0, 0, 0, Qnil);
15263 }
15264 else
15265 {
15266 int bytepos = last - lisp_string;
15267 int charpos = string_byte_to_char (elt, bytepos);
15268 n += display_string (NULL, elt, Qnil, 0, charpos,
15269 it, 0, prec, 0,
15270 STRING_MULTIBYTE (elt));
15271 }
15272 }
15273 else /* c == '%' */
15274 {
15275 const unsigned char *percent_position = this;
15276
15277 /* Get the specified minimum width. Zero means
15278 don't pad. */
15279 field = 0;
15280 while ((c = *this++) >= '0' && c <= '9')
15281 field = field * 10 + c - '0';
15282
15283 /* Don't pad beyond the total padding allowed. */
15284 if (field_width - n > 0 && field > field_width - n)
15285 field = field_width - n;
15286
15287 /* Note that either PRECISION <= 0 or N < PRECISION. */
15288 prec = precision - n;
15289
15290 if (c == 'M')
15291 n += display_mode_element (it, depth, field, prec,
15292 Vglobal_mode_string, props,
15293 risky);
15294 else if (c != 0)
15295 {
15296 int multibyte;
15297 int bytepos, charpos;
15298 unsigned char *spec;
15299
15300 bytepos = percent_position - lisp_string;
15301 charpos = (STRING_MULTIBYTE (elt)
15302 ? string_byte_to_char (elt, bytepos)
15303 : bytepos);
15304
15305 spec
15306 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15307
15308 if (frame_title_ptr)
15309 n += store_frame_title (spec, field, prec);
15310 else if (!NILP (mode_line_string_list))
15311 {
15312 int len = strlen (spec);
15313 Lisp_Object tem = make_string (spec, len);
15314 props = Ftext_properties_at (make_number (charpos), elt);
15315 /* Should only keep face property in props */
15316 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15317 }
15318 else
15319 {
15320 int nglyphs_before, nwritten;
15321
15322 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15323 nwritten = display_string (spec, Qnil, elt,
15324 charpos, 0, it,
15325 field, prec, 0,
15326 multibyte);
15327
15328 /* Assign to the glyphs written above the
15329 string where the `%x' came from, position
15330 of the `%'. */
15331 if (nwritten > 0)
15332 {
15333 struct glyph *glyph
15334 = (it->glyph_row->glyphs[TEXT_AREA]
15335 + nglyphs_before);
15336 int i;
15337
15338 for (i = 0; i < nwritten; ++i)
15339 {
15340 glyph[i].object = elt;
15341 glyph[i].charpos = charpos;
15342 }
15343
15344 n += nwritten;
15345 }
15346 }
15347 }
15348 else /* c == 0 */
15349 break;
15350 }
15351 }
15352 }
15353 break;
15354
15355 case Lisp_Symbol:
15356 /* A symbol: process the value of the symbol recursively
15357 as if it appeared here directly. Avoid error if symbol void.
15358 Special case: if value of symbol is a string, output the string
15359 literally. */
15360 {
15361 register Lisp_Object tem;
15362
15363 /* If the variable is not marked as risky to set
15364 then its contents are risky to use. */
15365 if (NILP (Fget (elt, Qrisky_local_variable)))
15366 risky = 1;
15367
15368 tem = Fboundp (elt);
15369 if (!NILP (tem))
15370 {
15371 tem = Fsymbol_value (elt);
15372 /* If value is a string, output that string literally:
15373 don't check for % within it. */
15374 if (STRINGP (tem))
15375 literal = 1;
15376
15377 if (!EQ (tem, elt))
15378 {
15379 /* Give up right away for nil or t. */
15380 elt = tem;
15381 goto tail_recurse;
15382 }
15383 }
15384 }
15385 break;
15386
15387 case Lisp_Cons:
15388 {
15389 register Lisp_Object car, tem;
15390
15391 /* A cons cell: five distinct cases.
15392 If first element is :eval or :propertize, do something special.
15393 If first element is a string or a cons, process all the elements
15394 and effectively concatenate them.
15395 If first element is a negative number, truncate displaying cdr to
15396 at most that many characters. If positive, pad (with spaces)
15397 to at least that many characters.
15398 If first element is a symbol, process the cadr or caddr recursively
15399 according to whether the symbol's value is non-nil or nil. */
15400 car = XCAR (elt);
15401 if (EQ (car, QCeval))
15402 {
15403 /* An element of the form (:eval FORM) means evaluate FORM
15404 and use the result as mode line elements. */
15405
15406 if (risky)
15407 break;
15408
15409 if (CONSP (XCDR (elt)))
15410 {
15411 Lisp_Object spec;
15412 spec = safe_eval (XCAR (XCDR (elt)));
15413 n += display_mode_element (it, depth, field_width - n,
15414 precision - n, spec, props,
15415 risky);
15416 }
15417 }
15418 else if (EQ (car, QCpropertize))
15419 {
15420 /* An element of the form (:propertize ELT PROPS...)
15421 means display ELT but applying properties PROPS. */
15422
15423 if (risky)
15424 break;
15425
15426 if (CONSP (XCDR (elt)))
15427 n += display_mode_element (it, depth, field_width - n,
15428 precision - n, XCAR (XCDR (elt)),
15429 XCDR (XCDR (elt)), risky);
15430 }
15431 else if (SYMBOLP (car))
15432 {
15433 tem = Fboundp (car);
15434 elt = XCDR (elt);
15435 if (!CONSP (elt))
15436 goto invalid;
15437 /* elt is now the cdr, and we know it is a cons cell.
15438 Use its car if CAR has a non-nil value. */
15439 if (!NILP (tem))
15440 {
15441 tem = Fsymbol_value (car);
15442 if (!NILP (tem))
15443 {
15444 elt = XCAR (elt);
15445 goto tail_recurse;
15446 }
15447 }
15448 /* Symbol's value is nil (or symbol is unbound)
15449 Get the cddr of the original list
15450 and if possible find the caddr and use that. */
15451 elt = XCDR (elt);
15452 if (NILP (elt))
15453 break;
15454 else if (!CONSP (elt))
15455 goto invalid;
15456 elt = XCAR (elt);
15457 goto tail_recurse;
15458 }
15459 else if (INTEGERP (car))
15460 {
15461 register int lim = XINT (car);
15462 elt = XCDR (elt);
15463 if (lim < 0)
15464 {
15465 /* Negative int means reduce maximum width. */
15466 if (precision <= 0)
15467 precision = -lim;
15468 else
15469 precision = min (precision, -lim);
15470 }
15471 else if (lim > 0)
15472 {
15473 /* Padding specified. Don't let it be more than
15474 current maximum. */
15475 if (precision > 0)
15476 lim = min (precision, lim);
15477
15478 /* If that's more padding than already wanted, queue it.
15479 But don't reduce padding already specified even if
15480 that is beyond the current truncation point. */
15481 field_width = max (lim, field_width);
15482 }
15483 goto tail_recurse;
15484 }
15485 else if (STRINGP (car) || CONSP (car))
15486 {
15487 register int limit = 50;
15488 /* Limit is to protect against circular lists. */
15489 while (CONSP (elt)
15490 && --limit > 0
15491 && (precision <= 0 || n < precision))
15492 {
15493 n += display_mode_element (it, depth, field_width - n,
15494 precision - n, XCAR (elt),
15495 props, risky);
15496 elt = XCDR (elt);
15497 }
15498 }
15499 }
15500 break;
15501
15502 default:
15503 invalid:
15504 elt = build_string ("*invalid*");
15505 goto tail_recurse;
15506 }
15507
15508 /* Pad to FIELD_WIDTH. */
15509 if (field_width > 0 && n < field_width)
15510 {
15511 if (frame_title_ptr)
15512 n += store_frame_title ("", field_width - n, 0);
15513 else if (!NILP (mode_line_string_list))
15514 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15515 else
15516 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15517 0, 0, 0);
15518 }
15519
15520 return n;
15521 }
15522
15523 /* Store a mode-line string element in mode_line_string_list.
15524
15525 If STRING is non-null, display that C string. Otherwise, the Lisp
15526 string LISP_STRING is displayed.
15527
15528 FIELD_WIDTH is the minimum number of output glyphs to produce.
15529 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15530 with spaces. FIELD_WIDTH <= 0 means don't pad.
15531
15532 PRECISION is the maximum number of characters to output from
15533 STRING. PRECISION <= 0 means don't truncate the string.
15534
15535 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15536 properties to the string.
15537
15538 PROPS are the properties to add to the string.
15539 The mode_line_string_face face property is always added to the string.
15540 */
15541
15542 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15543 char *string;
15544 Lisp_Object lisp_string;
15545 int copy_string;
15546 int field_width;
15547 int precision;
15548 Lisp_Object props;
15549 {
15550 int len;
15551 int n = 0;
15552
15553 if (string != NULL)
15554 {
15555 len = strlen (string);
15556 if (precision > 0 && len > precision)
15557 len = precision;
15558 lisp_string = make_string (string, len);
15559 if (NILP (props))
15560 props = mode_line_string_face_prop;
15561 else if (!NILP (mode_line_string_face))
15562 {
15563 Lisp_Object face = Fplist_get (props, Qface);
15564 props = Fcopy_sequence (props);
15565 if (NILP (face))
15566 face = mode_line_string_face;
15567 else
15568 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15569 props = Fplist_put (props, Qface, face);
15570 }
15571 Fadd_text_properties (make_number (0), make_number (len),
15572 props, lisp_string);
15573 }
15574 else
15575 {
15576 len = XFASTINT (Flength (lisp_string));
15577 if (precision > 0 && len > precision)
15578 {
15579 len = precision;
15580 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15581 precision = -1;
15582 }
15583 if (!NILP (mode_line_string_face))
15584 {
15585 Lisp_Object face;
15586 if (NILP (props))
15587 props = Ftext_properties_at (make_number (0), lisp_string);
15588 face = Fplist_get (props, Qface);
15589 if (NILP (face))
15590 face = mode_line_string_face;
15591 else
15592 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15593 props = Fcons (Qface, Fcons (face, Qnil));
15594 if (copy_string)
15595 lisp_string = Fcopy_sequence (lisp_string);
15596 }
15597 if (!NILP (props))
15598 Fadd_text_properties (make_number (0), make_number (len),
15599 props, lisp_string);
15600 }
15601
15602 if (len > 0)
15603 {
15604 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15605 n += len;
15606 }
15607
15608 if (field_width > len)
15609 {
15610 field_width -= len;
15611 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15612 if (!NILP (props))
15613 Fadd_text_properties (make_number (0), make_number (field_width),
15614 props, lisp_string);
15615 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15616 n += field_width;
15617 }
15618
15619 return n;
15620 }
15621
15622
15623 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15624 0, 3, 0,
15625 doc: /* Return the mode-line of selected window as a string.
15626 First optional arg FORMAT specifies a different format string (see
15627 `mode-line-format' for details) to use. If FORMAT is t, return
15628 the buffer's header-line. Second optional arg WINDOW specifies a
15629 different window to use as the context for the formatting.
15630 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15631 (format, window, no_props)
15632 Lisp_Object format, window, no_props;
15633 {
15634 struct it it;
15635 int len;
15636 struct window *w;
15637 struct buffer *old_buffer = NULL;
15638 enum face_id face_id = DEFAULT_FACE_ID;
15639
15640 if (NILP (window))
15641 window = selected_window;
15642 CHECK_WINDOW (window);
15643 w = XWINDOW (window);
15644 CHECK_BUFFER (w->buffer);
15645
15646 if (XBUFFER (w->buffer) != current_buffer)
15647 {
15648 old_buffer = current_buffer;
15649 set_buffer_internal_1 (XBUFFER (w->buffer));
15650 }
15651
15652 if (NILP (format) || EQ (format, Qt))
15653 {
15654 face_id = NILP (format)
15655 ? CURRENT_MODE_LINE_FACE_ID (w) :
15656 HEADER_LINE_FACE_ID;
15657 format = NILP (format)
15658 ? current_buffer->mode_line_format
15659 : current_buffer->header_line_format;
15660 }
15661
15662 init_iterator (&it, w, -1, -1, NULL, face_id);
15663
15664 if (NILP (no_props))
15665 {
15666 mode_line_string_face =
15667 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15668 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15669 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15670
15671 mode_line_string_face_prop =
15672 NILP (mode_line_string_face) ? Qnil :
15673 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15674
15675 /* We need a dummy last element in mode_line_string_list to
15676 indicate we are building the propertized mode-line string.
15677 Using mode_line_string_face_prop here GC protects it. */
15678 mode_line_string_list =
15679 Fcons (mode_line_string_face_prop, Qnil);
15680 frame_title_ptr = NULL;
15681 }
15682 else
15683 {
15684 mode_line_string_face_prop = Qnil;
15685 mode_line_string_list = Qnil;
15686 frame_title_ptr = frame_title_buf;
15687 }
15688
15689 push_frame_kboard (it.f);
15690 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15691 pop_frame_kboard ();
15692
15693 if (old_buffer)
15694 set_buffer_internal_1 (old_buffer);
15695
15696 if (NILP (no_props))
15697 {
15698 Lisp_Object str;
15699 mode_line_string_list = Fnreverse (mode_line_string_list);
15700 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15701 make_string ("", 0));
15702 mode_line_string_face_prop = Qnil;
15703 mode_line_string_list = Qnil;
15704 return str;
15705 }
15706
15707 len = frame_title_ptr - frame_title_buf;
15708 if (len > 0 && frame_title_ptr[-1] == '-')
15709 {
15710 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15711 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15712 ;
15713 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15714 if (len > frame_title_ptr - frame_title_buf)
15715 len = frame_title_ptr - frame_title_buf;
15716 }
15717
15718 frame_title_ptr = NULL;
15719 return make_string (frame_title_buf, len);
15720 }
15721
15722 /* Write a null-terminated, right justified decimal representation of
15723 the positive integer D to BUF using a minimal field width WIDTH. */
15724
15725 static void
15726 pint2str (buf, width, d)
15727 register char *buf;
15728 register int width;
15729 register int d;
15730 {
15731 register char *p = buf;
15732
15733 if (d <= 0)
15734 *p++ = '0';
15735 else
15736 {
15737 while (d > 0)
15738 {
15739 *p++ = d % 10 + '0';
15740 d /= 10;
15741 }
15742 }
15743
15744 for (width -= (int) (p - buf); width > 0; --width)
15745 *p++ = ' ';
15746 *p-- = '\0';
15747 while (p > buf)
15748 {
15749 d = *buf;
15750 *buf++ = *p;
15751 *p-- = d;
15752 }
15753 }
15754
15755 /* Write a null-terminated, right justified decimal and "human
15756 readable" representation of the nonnegative integer D to BUF using
15757 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15758
15759 static const char power_letter[] =
15760 {
15761 0, /* not used */
15762 'k', /* kilo */
15763 'M', /* mega */
15764 'G', /* giga */
15765 'T', /* tera */
15766 'P', /* peta */
15767 'E', /* exa */
15768 'Z', /* zetta */
15769 'Y' /* yotta */
15770 };
15771
15772 static void
15773 pint2hrstr (buf, width, d)
15774 char *buf;
15775 int width;
15776 int d;
15777 {
15778 /* We aim to represent the nonnegative integer D as
15779 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15780 int quotient = d;
15781 int remainder = 0;
15782 /* -1 means: do not use TENTHS. */
15783 int tenths = -1;
15784 int exponent = 0;
15785
15786 /* Length of QUOTIENT.TENTHS as a string. */
15787 int length;
15788
15789 char * psuffix;
15790 char * p;
15791
15792 if (1000 <= quotient)
15793 {
15794 /* Scale to the appropriate EXPONENT. */
15795 do
15796 {
15797 remainder = quotient % 1000;
15798 quotient /= 1000;
15799 exponent++;
15800 }
15801 while (1000 <= quotient);
15802
15803 /* Round to nearest and decide whether to use TENTHS or not. */
15804 if (quotient <= 9)
15805 {
15806 tenths = remainder / 100;
15807 if (50 <= remainder % 100)
15808 if (tenths < 9)
15809 tenths++;
15810 else
15811 {
15812 quotient++;
15813 if (quotient == 10)
15814 tenths = -1;
15815 else
15816 tenths = 0;
15817 }
15818 }
15819 else
15820 if (500 <= remainder)
15821 if (quotient < 999)
15822 quotient++;
15823 else
15824 {
15825 quotient = 1;
15826 exponent++;
15827 tenths = 0;
15828 }
15829 }
15830
15831 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15832 if (tenths == -1 && quotient <= 99)
15833 if (quotient <= 9)
15834 length = 1;
15835 else
15836 length = 2;
15837 else
15838 length = 3;
15839 p = psuffix = buf + max (width, length);
15840
15841 /* Print EXPONENT. */
15842 if (exponent)
15843 *psuffix++ = power_letter[exponent];
15844 *psuffix = '\0';
15845
15846 /* Print TENTHS. */
15847 if (tenths >= 0)
15848 {
15849 *--p = '0' + tenths;
15850 *--p = '.';
15851 }
15852
15853 /* Print QUOTIENT. */
15854 do
15855 {
15856 int digit = quotient % 10;
15857 *--p = '0' + digit;
15858 }
15859 while ((quotient /= 10) != 0);
15860
15861 /* Print leading spaces. */
15862 while (buf < p)
15863 *--p = ' ';
15864 }
15865
15866 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15867 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15868 type of CODING_SYSTEM. Return updated pointer into BUF. */
15869
15870 static unsigned char invalid_eol_type[] = "(*invalid*)";
15871
15872 static char *
15873 decode_mode_spec_coding (coding_system, buf, eol_flag)
15874 Lisp_Object coding_system;
15875 register char *buf;
15876 int eol_flag;
15877 {
15878 Lisp_Object val;
15879 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15880 const unsigned char *eol_str;
15881 int eol_str_len;
15882 /* The EOL conversion we are using. */
15883 Lisp_Object eoltype;
15884
15885 val = Fget (coding_system, Qcoding_system);
15886 eoltype = Qnil;
15887
15888 if (!VECTORP (val)) /* Not yet decided. */
15889 {
15890 if (multibyte)
15891 *buf++ = '-';
15892 if (eol_flag)
15893 eoltype = eol_mnemonic_undecided;
15894 /* Don't mention EOL conversion if it isn't decided. */
15895 }
15896 else
15897 {
15898 Lisp_Object eolvalue;
15899
15900 eolvalue = Fget (coding_system, Qeol_type);
15901
15902 if (multibyte)
15903 *buf++ = XFASTINT (AREF (val, 1));
15904
15905 if (eol_flag)
15906 {
15907 /* The EOL conversion that is normal on this system. */
15908
15909 if (NILP (eolvalue)) /* Not yet decided. */
15910 eoltype = eol_mnemonic_undecided;
15911 else if (VECTORP (eolvalue)) /* Not yet decided. */
15912 eoltype = eol_mnemonic_undecided;
15913 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15914 eoltype = (XFASTINT (eolvalue) == 0
15915 ? eol_mnemonic_unix
15916 : (XFASTINT (eolvalue) == 1
15917 ? eol_mnemonic_dos : eol_mnemonic_mac));
15918 }
15919 }
15920
15921 if (eol_flag)
15922 {
15923 /* Mention the EOL conversion if it is not the usual one. */
15924 if (STRINGP (eoltype))
15925 {
15926 eol_str = SDATA (eoltype);
15927 eol_str_len = SBYTES (eoltype);
15928 }
15929 else if (INTEGERP (eoltype)
15930 && CHAR_VALID_P (XINT (eoltype), 0))
15931 {
15932 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15933 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15934 eol_str = tmp;
15935 }
15936 else
15937 {
15938 eol_str = invalid_eol_type;
15939 eol_str_len = sizeof (invalid_eol_type) - 1;
15940 }
15941 bcopy (eol_str, buf, eol_str_len);
15942 buf += eol_str_len;
15943 }
15944
15945 return buf;
15946 }
15947
15948 /* Return a string for the output of a mode line %-spec for window W,
15949 generated by character C. PRECISION >= 0 means don't return a
15950 string longer than that value. FIELD_WIDTH > 0 means pad the
15951 string returned with spaces to that value. Return 1 in *MULTIBYTE
15952 if the result is multibyte text. */
15953
15954 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15955
15956 static char *
15957 decode_mode_spec (w, c, field_width, precision, multibyte)
15958 struct window *w;
15959 register int c;
15960 int field_width, precision;
15961 int *multibyte;
15962 {
15963 Lisp_Object obj;
15964 struct frame *f = XFRAME (WINDOW_FRAME (w));
15965 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15966 struct buffer *b = XBUFFER (w->buffer);
15967
15968 obj = Qnil;
15969 *multibyte = 0;
15970
15971 switch (c)
15972 {
15973 case '*':
15974 if (!NILP (b->read_only))
15975 return "%";
15976 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15977 return "*";
15978 return "-";
15979
15980 case '+':
15981 /* This differs from %* only for a modified read-only buffer. */
15982 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15983 return "*";
15984 if (!NILP (b->read_only))
15985 return "%";
15986 return "-";
15987
15988 case '&':
15989 /* This differs from %* in ignoring read-only-ness. */
15990 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15991 return "*";
15992 return "-";
15993
15994 case '%':
15995 return "%";
15996
15997 case '[':
15998 {
15999 int i;
16000 char *p;
16001
16002 if (command_loop_level > 5)
16003 return "[[[... ";
16004 p = decode_mode_spec_buf;
16005 for (i = 0; i < command_loop_level; i++)
16006 *p++ = '[';
16007 *p = 0;
16008 return decode_mode_spec_buf;
16009 }
16010
16011 case ']':
16012 {
16013 int i;
16014 char *p;
16015
16016 if (command_loop_level > 5)
16017 return " ...]]]";
16018 p = decode_mode_spec_buf;
16019 for (i = 0; i < command_loop_level; i++)
16020 *p++ = ']';
16021 *p = 0;
16022 return decode_mode_spec_buf;
16023 }
16024
16025 case '-':
16026 {
16027 register int i;
16028
16029 /* Let lots_of_dashes be a string of infinite length. */
16030 if (!NILP (mode_line_string_list))
16031 return "--";
16032 if (field_width <= 0
16033 || field_width > sizeof (lots_of_dashes))
16034 {
16035 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16036 decode_mode_spec_buf[i] = '-';
16037 decode_mode_spec_buf[i] = '\0';
16038 return decode_mode_spec_buf;
16039 }
16040 else
16041 return lots_of_dashes;
16042 }
16043
16044 case 'b':
16045 obj = b->name;
16046 break;
16047
16048 case 'c':
16049 {
16050 int col = (int) current_column (); /* iftc */
16051 w->column_number_displayed = make_number (col);
16052 pint2str (decode_mode_spec_buf, field_width, col);
16053 return decode_mode_spec_buf;
16054 }
16055
16056 case 'F':
16057 /* %F displays the frame name. */
16058 if (!NILP (f->title))
16059 return (char *) SDATA (f->title);
16060 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16061 return (char *) SDATA (f->name);
16062 return "Emacs";
16063
16064 case 'f':
16065 obj = b->filename;
16066 break;
16067
16068 case 'i':
16069 {
16070 int size = ZV - BEGV;
16071 pint2str (decode_mode_spec_buf, field_width, size);
16072 return decode_mode_spec_buf;
16073 }
16074
16075 case 'I':
16076 {
16077 int size = ZV - BEGV;
16078 pint2hrstr (decode_mode_spec_buf, field_width, size);
16079 return decode_mode_spec_buf;
16080 }
16081
16082 case 'l':
16083 {
16084 int startpos = XMARKER (w->start)->charpos;
16085 int startpos_byte = marker_byte_position (w->start);
16086 int line, linepos, linepos_byte, topline;
16087 int nlines, junk;
16088 int height = WINDOW_TOTAL_LINES (w);
16089
16090 /* If we decided that this buffer isn't suitable for line numbers,
16091 don't forget that too fast. */
16092 if (EQ (w->base_line_pos, w->buffer))
16093 goto no_value;
16094 /* But do forget it, if the window shows a different buffer now. */
16095 else if (BUFFERP (w->base_line_pos))
16096 w->base_line_pos = Qnil;
16097
16098 /* If the buffer is very big, don't waste time. */
16099 if (INTEGERP (Vline_number_display_limit)
16100 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16101 {
16102 w->base_line_pos = Qnil;
16103 w->base_line_number = Qnil;
16104 goto no_value;
16105 }
16106
16107 if (!NILP (w->base_line_number)
16108 && !NILP (w->base_line_pos)
16109 && XFASTINT (w->base_line_pos) <= startpos)
16110 {
16111 line = XFASTINT (w->base_line_number);
16112 linepos = XFASTINT (w->base_line_pos);
16113 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16114 }
16115 else
16116 {
16117 line = 1;
16118 linepos = BUF_BEGV (b);
16119 linepos_byte = BUF_BEGV_BYTE (b);
16120 }
16121
16122 /* Count lines from base line to window start position. */
16123 nlines = display_count_lines (linepos, linepos_byte,
16124 startpos_byte,
16125 startpos, &junk);
16126
16127 topline = nlines + line;
16128
16129 /* Determine a new base line, if the old one is too close
16130 or too far away, or if we did not have one.
16131 "Too close" means it's plausible a scroll-down would
16132 go back past it. */
16133 if (startpos == BUF_BEGV (b))
16134 {
16135 w->base_line_number = make_number (topline);
16136 w->base_line_pos = make_number (BUF_BEGV (b));
16137 }
16138 else if (nlines < height + 25 || nlines > height * 3 + 50
16139 || linepos == BUF_BEGV (b))
16140 {
16141 int limit = BUF_BEGV (b);
16142 int limit_byte = BUF_BEGV_BYTE (b);
16143 int position;
16144 int distance = (height * 2 + 30) * line_number_display_limit_width;
16145
16146 if (startpos - distance > limit)
16147 {
16148 limit = startpos - distance;
16149 limit_byte = CHAR_TO_BYTE (limit);
16150 }
16151
16152 nlines = display_count_lines (startpos, startpos_byte,
16153 limit_byte,
16154 - (height * 2 + 30),
16155 &position);
16156 /* If we couldn't find the lines we wanted within
16157 line_number_display_limit_width chars per line,
16158 give up on line numbers for this window. */
16159 if (position == limit_byte && limit == startpos - distance)
16160 {
16161 w->base_line_pos = w->buffer;
16162 w->base_line_number = Qnil;
16163 goto no_value;
16164 }
16165
16166 w->base_line_number = make_number (topline - nlines);
16167 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16168 }
16169
16170 /* Now count lines from the start pos to point. */
16171 nlines = display_count_lines (startpos, startpos_byte,
16172 PT_BYTE, PT, &junk);
16173
16174 /* Record that we did display the line number. */
16175 line_number_displayed = 1;
16176
16177 /* Make the string to show. */
16178 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16179 return decode_mode_spec_buf;
16180 no_value:
16181 {
16182 char* p = decode_mode_spec_buf;
16183 int pad = field_width - 2;
16184 while (pad-- > 0)
16185 *p++ = ' ';
16186 *p++ = '?';
16187 *p++ = '?';
16188 *p = '\0';
16189 return decode_mode_spec_buf;
16190 }
16191 }
16192 break;
16193
16194 case 'm':
16195 obj = b->mode_name;
16196 break;
16197
16198 case 'n':
16199 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16200 return " Narrow";
16201 break;
16202
16203 case 'p':
16204 {
16205 int pos = marker_position (w->start);
16206 int total = BUF_ZV (b) - BUF_BEGV (b);
16207
16208 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16209 {
16210 if (pos <= BUF_BEGV (b))
16211 return "All";
16212 else
16213 return "Bottom";
16214 }
16215 else if (pos <= BUF_BEGV (b))
16216 return "Top";
16217 else
16218 {
16219 if (total > 1000000)
16220 /* Do it differently for a large value, to avoid overflow. */
16221 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16222 else
16223 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16224 /* We can't normally display a 3-digit number,
16225 so get us a 2-digit number that is close. */
16226 if (total == 100)
16227 total = 99;
16228 sprintf (decode_mode_spec_buf, "%2d%%", total);
16229 return decode_mode_spec_buf;
16230 }
16231 }
16232
16233 /* Display percentage of size above the bottom of the screen. */
16234 case 'P':
16235 {
16236 int toppos = marker_position (w->start);
16237 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16238 int total = BUF_ZV (b) - BUF_BEGV (b);
16239
16240 if (botpos >= BUF_ZV (b))
16241 {
16242 if (toppos <= BUF_BEGV (b))
16243 return "All";
16244 else
16245 return "Bottom";
16246 }
16247 else
16248 {
16249 if (total > 1000000)
16250 /* Do it differently for a large value, to avoid overflow. */
16251 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16252 else
16253 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16254 /* We can't normally display a 3-digit number,
16255 so get us a 2-digit number that is close. */
16256 if (total == 100)
16257 total = 99;
16258 if (toppos <= BUF_BEGV (b))
16259 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16260 else
16261 sprintf (decode_mode_spec_buf, "%2d%%", total);
16262 return decode_mode_spec_buf;
16263 }
16264 }
16265
16266 case 's':
16267 /* status of process */
16268 obj = Fget_buffer_process (w->buffer);
16269 if (NILP (obj))
16270 return "no process";
16271 #ifdef subprocesses
16272 obj = Fsymbol_name (Fprocess_status (obj));
16273 #endif
16274 break;
16275
16276 case 't': /* indicate TEXT or BINARY */
16277 #ifdef MODE_LINE_BINARY_TEXT
16278 return MODE_LINE_BINARY_TEXT (b);
16279 #else
16280 return "T";
16281 #endif
16282
16283 case 'z':
16284 /* coding-system (not including end-of-line format) */
16285 case 'Z':
16286 /* coding-system (including end-of-line type) */
16287 {
16288 int eol_flag = (c == 'Z');
16289 char *p = decode_mode_spec_buf;
16290
16291 if (! FRAME_WINDOW_P (f))
16292 {
16293 /* No need to mention EOL here--the terminal never needs
16294 to do EOL conversion. */
16295 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16296 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16297 }
16298 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16299 p, eol_flag);
16300
16301 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16302 #ifdef subprocesses
16303 obj = Fget_buffer_process (Fcurrent_buffer ());
16304 if (PROCESSP (obj))
16305 {
16306 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16307 p, eol_flag);
16308 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16309 p, eol_flag);
16310 }
16311 #endif /* subprocesses */
16312 #endif /* 0 */
16313 *p = 0;
16314 return decode_mode_spec_buf;
16315 }
16316 }
16317
16318 if (STRINGP (obj))
16319 {
16320 *multibyte = STRING_MULTIBYTE (obj);
16321 return (char *) SDATA (obj);
16322 }
16323 else
16324 return "";
16325 }
16326
16327
16328 /* Count up to COUNT lines starting from START / START_BYTE.
16329 But don't go beyond LIMIT_BYTE.
16330 Return the number of lines thus found (always nonnegative).
16331
16332 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16333
16334 static int
16335 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16336 int start, start_byte, limit_byte, count;
16337 int *byte_pos_ptr;
16338 {
16339 register unsigned char *cursor;
16340 unsigned char *base;
16341
16342 register int ceiling;
16343 register unsigned char *ceiling_addr;
16344 int orig_count = count;
16345
16346 /* If we are not in selective display mode,
16347 check only for newlines. */
16348 int selective_display = (!NILP (current_buffer->selective_display)
16349 && !INTEGERP (current_buffer->selective_display));
16350
16351 if (count > 0)
16352 {
16353 while (start_byte < limit_byte)
16354 {
16355 ceiling = BUFFER_CEILING_OF (start_byte);
16356 ceiling = min (limit_byte - 1, ceiling);
16357 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16358 base = (cursor = BYTE_POS_ADDR (start_byte));
16359 while (1)
16360 {
16361 if (selective_display)
16362 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16363 ;
16364 else
16365 while (*cursor != '\n' && ++cursor != ceiling_addr)
16366 ;
16367
16368 if (cursor != ceiling_addr)
16369 {
16370 if (--count == 0)
16371 {
16372 start_byte += cursor - base + 1;
16373 *byte_pos_ptr = start_byte;
16374 return orig_count;
16375 }
16376 else
16377 if (++cursor == ceiling_addr)
16378 break;
16379 }
16380 else
16381 break;
16382 }
16383 start_byte += cursor - base;
16384 }
16385 }
16386 else
16387 {
16388 while (start_byte > limit_byte)
16389 {
16390 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16391 ceiling = max (limit_byte, ceiling);
16392 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16393 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16394 while (1)
16395 {
16396 if (selective_display)
16397 while (--cursor != ceiling_addr
16398 && *cursor != '\n' && *cursor != 015)
16399 ;
16400 else
16401 while (--cursor != ceiling_addr && *cursor != '\n')
16402 ;
16403
16404 if (cursor != ceiling_addr)
16405 {
16406 if (++count == 0)
16407 {
16408 start_byte += cursor - base + 1;
16409 *byte_pos_ptr = start_byte;
16410 /* When scanning backwards, we should
16411 not count the newline posterior to which we stop. */
16412 return - orig_count - 1;
16413 }
16414 }
16415 else
16416 break;
16417 }
16418 /* Here we add 1 to compensate for the last decrement
16419 of CURSOR, which took it past the valid range. */
16420 start_byte += cursor - base + 1;
16421 }
16422 }
16423
16424 *byte_pos_ptr = limit_byte;
16425
16426 if (count < 0)
16427 return - orig_count + count;
16428 return orig_count - count;
16429
16430 }
16431
16432
16433 \f
16434 /***********************************************************************
16435 Displaying strings
16436 ***********************************************************************/
16437
16438 /* Display a NUL-terminated string, starting with index START.
16439
16440 If STRING is non-null, display that C string. Otherwise, the Lisp
16441 string LISP_STRING is displayed.
16442
16443 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16444 FACE_STRING. Display STRING or LISP_STRING with the face at
16445 FACE_STRING_POS in FACE_STRING:
16446
16447 Display the string in the environment given by IT, but use the
16448 standard display table, temporarily.
16449
16450 FIELD_WIDTH is the minimum number of output glyphs to produce.
16451 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16452 with spaces. If STRING has more characters, more than FIELD_WIDTH
16453 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16454
16455 PRECISION is the maximum number of characters to output from
16456 STRING. PRECISION < 0 means don't truncate the string.
16457
16458 This is roughly equivalent to printf format specifiers:
16459
16460 FIELD_WIDTH PRECISION PRINTF
16461 ----------------------------------------
16462 -1 -1 %s
16463 -1 10 %.10s
16464 10 -1 %10s
16465 20 10 %20.10s
16466
16467 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16468 display them, and < 0 means obey the current buffer's value of
16469 enable_multibyte_characters.
16470
16471 Value is the number of glyphs produced. */
16472
16473 static int
16474 display_string (string, lisp_string, face_string, face_string_pos,
16475 start, it, field_width, precision, max_x, multibyte)
16476 unsigned char *string;
16477 Lisp_Object lisp_string;
16478 Lisp_Object face_string;
16479 int face_string_pos;
16480 int start;
16481 struct it *it;
16482 int field_width, precision, max_x;
16483 int multibyte;
16484 {
16485 int hpos_at_start = it->hpos;
16486 int saved_face_id = it->face_id;
16487 struct glyph_row *row = it->glyph_row;
16488
16489 /* Initialize the iterator IT for iteration over STRING beginning
16490 with index START. */
16491 reseat_to_string (it, string, lisp_string, start,
16492 precision, field_width, multibyte);
16493
16494 /* If displaying STRING, set up the face of the iterator
16495 from LISP_STRING, if that's given. */
16496 if (STRINGP (face_string))
16497 {
16498 int endptr;
16499 struct face *face;
16500
16501 it->face_id
16502 = face_at_string_position (it->w, face_string, face_string_pos,
16503 0, it->region_beg_charpos,
16504 it->region_end_charpos,
16505 &endptr, it->base_face_id, 0);
16506 face = FACE_FROM_ID (it->f, it->face_id);
16507 it->face_box_p = face->box != FACE_NO_BOX;
16508 }
16509
16510 /* Set max_x to the maximum allowed X position. Don't let it go
16511 beyond the right edge of the window. */
16512 if (max_x <= 0)
16513 max_x = it->last_visible_x;
16514 else
16515 max_x = min (max_x, it->last_visible_x);
16516
16517 /* Skip over display elements that are not visible. because IT->w is
16518 hscrolled. */
16519 if (it->current_x < it->first_visible_x)
16520 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16521 MOVE_TO_POS | MOVE_TO_X);
16522
16523 row->ascent = it->max_ascent;
16524 row->height = it->max_ascent + it->max_descent;
16525 row->phys_ascent = it->max_phys_ascent;
16526 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16527
16528 /* This condition is for the case that we are called with current_x
16529 past last_visible_x. */
16530 while (it->current_x < max_x)
16531 {
16532 int x_before, x, n_glyphs_before, i, nglyphs;
16533
16534 /* Get the next display element. */
16535 if (!get_next_display_element (it))
16536 break;
16537
16538 /* Produce glyphs. */
16539 x_before = it->current_x;
16540 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16541 PRODUCE_GLYPHS (it);
16542
16543 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16544 i = 0;
16545 x = x_before;
16546 while (i < nglyphs)
16547 {
16548 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16549
16550 if (!it->truncate_lines_p
16551 && x + glyph->pixel_width > max_x)
16552 {
16553 /* End of continued line or max_x reached. */
16554 if (CHAR_GLYPH_PADDING_P (*glyph))
16555 {
16556 /* A wide character is unbreakable. */
16557 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16558 it->current_x = x_before;
16559 }
16560 else
16561 {
16562 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16563 it->current_x = x;
16564 }
16565 break;
16566 }
16567 else if (x + glyph->pixel_width > it->first_visible_x)
16568 {
16569 /* Glyph is at least partially visible. */
16570 ++it->hpos;
16571 if (x < it->first_visible_x)
16572 it->glyph_row->x = x - it->first_visible_x;
16573 }
16574 else
16575 {
16576 /* Glyph is off the left margin of the display area.
16577 Should not happen. */
16578 abort ();
16579 }
16580
16581 row->ascent = max (row->ascent, it->max_ascent);
16582 row->height = max (row->height, it->max_ascent + it->max_descent);
16583 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16584 row->phys_height = max (row->phys_height,
16585 it->max_phys_ascent + it->max_phys_descent);
16586 x += glyph->pixel_width;
16587 ++i;
16588 }
16589
16590 /* Stop if max_x reached. */
16591 if (i < nglyphs)
16592 break;
16593
16594 /* Stop at line ends. */
16595 if (ITERATOR_AT_END_OF_LINE_P (it))
16596 {
16597 it->continuation_lines_width = 0;
16598 break;
16599 }
16600
16601 set_iterator_to_next (it, 1);
16602
16603 /* Stop if truncating at the right edge. */
16604 if (it->truncate_lines_p
16605 && it->current_x >= it->last_visible_x)
16606 {
16607 /* Add truncation mark, but don't do it if the line is
16608 truncated at a padding space. */
16609 if (IT_CHARPOS (*it) < it->string_nchars)
16610 {
16611 if (!FRAME_WINDOW_P (it->f))
16612 {
16613 int i, n;
16614
16615 if (it->current_x > it->last_visible_x)
16616 {
16617 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16618 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16619 break;
16620 for (n = row->used[TEXT_AREA]; i < n; ++i)
16621 {
16622 row->used[TEXT_AREA] = i;
16623 produce_special_glyphs (it, IT_TRUNCATION);
16624 }
16625 }
16626 produce_special_glyphs (it, IT_TRUNCATION);
16627 }
16628 it->glyph_row->truncated_on_right_p = 1;
16629 }
16630 break;
16631 }
16632 }
16633
16634 /* Maybe insert a truncation at the left. */
16635 if (it->first_visible_x
16636 && IT_CHARPOS (*it) > 0)
16637 {
16638 if (!FRAME_WINDOW_P (it->f))
16639 insert_left_trunc_glyphs (it);
16640 it->glyph_row->truncated_on_left_p = 1;
16641 }
16642
16643 it->face_id = saved_face_id;
16644
16645 /* Value is number of columns displayed. */
16646 return it->hpos - hpos_at_start;
16647 }
16648
16649
16650 \f
16651 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16652 appears as an element of LIST or as the car of an element of LIST.
16653 If PROPVAL is a list, compare each element against LIST in that
16654 way, and return 1/2 if any element of PROPVAL is found in LIST.
16655 Otherwise return 0. This function cannot quit.
16656 The return value is 2 if the text is invisible but with an ellipsis
16657 and 1 if it's invisible and without an ellipsis. */
16658
16659 int
16660 invisible_p (propval, list)
16661 register Lisp_Object propval;
16662 Lisp_Object list;
16663 {
16664 register Lisp_Object tail, proptail;
16665
16666 for (tail = list; CONSP (tail); tail = XCDR (tail))
16667 {
16668 register Lisp_Object tem;
16669 tem = XCAR (tail);
16670 if (EQ (propval, tem))
16671 return 1;
16672 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16673 return NILP (XCDR (tem)) ? 1 : 2;
16674 }
16675
16676 if (CONSP (propval))
16677 {
16678 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16679 {
16680 Lisp_Object propelt;
16681 propelt = XCAR (proptail);
16682 for (tail = list; CONSP (tail); tail = XCDR (tail))
16683 {
16684 register Lisp_Object tem;
16685 tem = XCAR (tail);
16686 if (EQ (propelt, tem))
16687 return 1;
16688 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16689 return NILP (XCDR (tem)) ? 1 : 2;
16690 }
16691 }
16692 }
16693
16694 return 0;
16695 }
16696
16697 \f
16698 /***********************************************************************
16699 Glyph Display
16700 ***********************************************************************/
16701
16702 #ifdef HAVE_WINDOW_SYSTEM
16703
16704 #if GLYPH_DEBUG
16705
16706 void
16707 dump_glyph_string (s)
16708 struct glyph_string *s;
16709 {
16710 fprintf (stderr, "glyph string\n");
16711 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16712 s->x, s->y, s->width, s->height);
16713 fprintf (stderr, " ybase = %d\n", s->ybase);
16714 fprintf (stderr, " hl = %d\n", s->hl);
16715 fprintf (stderr, " left overhang = %d, right = %d\n",
16716 s->left_overhang, s->right_overhang);
16717 fprintf (stderr, " nchars = %d\n", s->nchars);
16718 fprintf (stderr, " extends to end of line = %d\n",
16719 s->extends_to_end_of_line_p);
16720 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16721 fprintf (stderr, " bg width = %d\n", s->background_width);
16722 }
16723
16724 #endif /* GLYPH_DEBUG */
16725
16726 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16727 of XChar2b structures for S; it can't be allocated in
16728 init_glyph_string because it must be allocated via `alloca'. W
16729 is the window on which S is drawn. ROW and AREA are the glyph row
16730 and area within the row from which S is constructed. START is the
16731 index of the first glyph structure covered by S. HL is a
16732 face-override for drawing S. */
16733
16734 #ifdef HAVE_NTGUI
16735 #define OPTIONAL_HDC(hdc) hdc,
16736 #define DECLARE_HDC(hdc) HDC hdc;
16737 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16738 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16739 #endif
16740
16741 #ifndef OPTIONAL_HDC
16742 #define OPTIONAL_HDC(hdc)
16743 #define DECLARE_HDC(hdc)
16744 #define ALLOCATE_HDC(hdc, f)
16745 #define RELEASE_HDC(hdc, f)
16746 #endif
16747
16748 static void
16749 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16750 struct glyph_string *s;
16751 DECLARE_HDC (hdc)
16752 XChar2b *char2b;
16753 struct window *w;
16754 struct glyph_row *row;
16755 enum glyph_row_area area;
16756 int start;
16757 enum draw_glyphs_face hl;
16758 {
16759 bzero (s, sizeof *s);
16760 s->w = w;
16761 s->f = XFRAME (w->frame);
16762 #ifdef HAVE_NTGUI
16763 s->hdc = hdc;
16764 #endif
16765 s->display = FRAME_X_DISPLAY (s->f);
16766 s->window = FRAME_X_WINDOW (s->f);
16767 s->char2b = char2b;
16768 s->hl = hl;
16769 s->row = row;
16770 s->area = area;
16771 s->first_glyph = row->glyphs[area] + start;
16772 s->height = row->height;
16773 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16774
16775 /* Display the internal border below the tool-bar window. */
16776 if (s->w == XWINDOW (s->f->tool_bar_window))
16777 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16778
16779 s->ybase = s->y + row->ascent;
16780 }
16781
16782
16783 /* Append the list of glyph strings with head H and tail T to the list
16784 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16785
16786 static INLINE void
16787 append_glyph_string_lists (head, tail, h, t)
16788 struct glyph_string **head, **tail;
16789 struct glyph_string *h, *t;
16790 {
16791 if (h)
16792 {
16793 if (*head)
16794 (*tail)->next = h;
16795 else
16796 *head = h;
16797 h->prev = *tail;
16798 *tail = t;
16799 }
16800 }
16801
16802
16803 /* Prepend the list of glyph strings with head H and tail T to the
16804 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16805 result. */
16806
16807 static INLINE void
16808 prepend_glyph_string_lists (head, tail, h, t)
16809 struct glyph_string **head, **tail;
16810 struct glyph_string *h, *t;
16811 {
16812 if (h)
16813 {
16814 if (*head)
16815 (*head)->prev = t;
16816 else
16817 *tail = t;
16818 t->next = *head;
16819 *head = h;
16820 }
16821 }
16822
16823
16824 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16825 Set *HEAD and *TAIL to the resulting list. */
16826
16827 static INLINE void
16828 append_glyph_string (head, tail, s)
16829 struct glyph_string **head, **tail;
16830 struct glyph_string *s;
16831 {
16832 s->next = s->prev = NULL;
16833 append_glyph_string_lists (head, tail, s, s);
16834 }
16835
16836
16837 /* Get face and two-byte form of character glyph GLYPH on frame F.
16838 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16839 a pointer to a realized face that is ready for display. */
16840
16841 static INLINE struct face *
16842 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16843 struct frame *f;
16844 struct glyph *glyph;
16845 XChar2b *char2b;
16846 int *two_byte_p;
16847 {
16848 struct face *face;
16849
16850 xassert (glyph->type == CHAR_GLYPH);
16851 face = FACE_FROM_ID (f, glyph->face_id);
16852
16853 if (two_byte_p)
16854 *two_byte_p = 0;
16855
16856 if (!glyph->multibyte_p)
16857 {
16858 /* Unibyte case. We don't have to encode, but we have to make
16859 sure to use a face suitable for unibyte. */
16860 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16861 }
16862 else if (glyph->u.ch < 128
16863 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16864 {
16865 /* Case of ASCII in a face known to fit ASCII. */
16866 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16867 }
16868 else
16869 {
16870 int c1, c2, charset;
16871
16872 /* Split characters into bytes. If c2 is -1 afterwards, C is
16873 really a one-byte character so that byte1 is zero. */
16874 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16875 if (c2 > 0)
16876 STORE_XCHAR2B (char2b, c1, c2);
16877 else
16878 STORE_XCHAR2B (char2b, 0, c1);
16879
16880 /* Maybe encode the character in *CHAR2B. */
16881 if (charset != CHARSET_ASCII)
16882 {
16883 struct font_info *font_info
16884 = FONT_INFO_FROM_ID (f, face->font_info_id);
16885 if (font_info)
16886 glyph->font_type
16887 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16888 }
16889 }
16890
16891 /* Make sure X resources of the face are allocated. */
16892 xassert (face != NULL);
16893 PREPARE_FACE_FOR_DISPLAY (f, face);
16894 return face;
16895 }
16896
16897
16898 /* Fill glyph string S with composition components specified by S->cmp.
16899
16900 FACES is an array of faces for all components of this composition.
16901 S->gidx is the index of the first component for S.
16902 OVERLAPS_P non-zero means S should draw the foreground only, and
16903 use its physical height for clipping.
16904
16905 Value is the index of a component not in S. */
16906
16907 static int
16908 fill_composite_glyph_string (s, faces, overlaps_p)
16909 struct glyph_string *s;
16910 struct face **faces;
16911 int overlaps_p;
16912 {
16913 int i;
16914
16915 xassert (s);
16916
16917 s->for_overlaps_p = overlaps_p;
16918
16919 s->face = faces[s->gidx];
16920 s->font = s->face->font;
16921 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16922
16923 /* For all glyphs of this composition, starting at the offset
16924 S->gidx, until we reach the end of the definition or encounter a
16925 glyph that requires the different face, add it to S. */
16926 ++s->nchars;
16927 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16928 ++s->nchars;
16929
16930 /* All glyph strings for the same composition has the same width,
16931 i.e. the width set for the first component of the composition. */
16932
16933 s->width = s->first_glyph->pixel_width;
16934
16935 /* If the specified font could not be loaded, use the frame's
16936 default font, but record the fact that we couldn't load it in
16937 the glyph string so that we can draw rectangles for the
16938 characters of the glyph string. */
16939 if (s->font == NULL)
16940 {
16941 s->font_not_found_p = 1;
16942 s->font = FRAME_FONT (s->f);
16943 }
16944
16945 /* Adjust base line for subscript/superscript text. */
16946 s->ybase += s->first_glyph->voffset;
16947
16948 xassert (s->face && s->face->gc);
16949
16950 /* This glyph string must always be drawn with 16-bit functions. */
16951 s->two_byte_p = 1;
16952
16953 return s->gidx + s->nchars;
16954 }
16955
16956
16957 /* Fill glyph string S from a sequence of character glyphs.
16958
16959 FACE_ID is the face id of the string. START is the index of the
16960 first glyph to consider, END is the index of the last + 1.
16961 OVERLAPS_P non-zero means S should draw the foreground only, and
16962 use its physical height for clipping.
16963
16964 Value is the index of the first glyph not in S. */
16965
16966 static int
16967 fill_glyph_string (s, face_id, start, end, overlaps_p)
16968 struct glyph_string *s;
16969 int face_id;
16970 int start, end, overlaps_p;
16971 {
16972 struct glyph *glyph, *last;
16973 int voffset;
16974 int glyph_not_available_p;
16975
16976 xassert (s->f == XFRAME (s->w->frame));
16977 xassert (s->nchars == 0);
16978 xassert (start >= 0 && end > start);
16979
16980 s->for_overlaps_p = overlaps_p,
16981 glyph = s->row->glyphs[s->area] + start;
16982 last = s->row->glyphs[s->area] + end;
16983 voffset = glyph->voffset;
16984
16985 glyph_not_available_p = glyph->glyph_not_available_p;
16986
16987 while (glyph < last
16988 && glyph->type == CHAR_GLYPH
16989 && glyph->voffset == voffset
16990 /* Same face id implies same font, nowadays. */
16991 && glyph->face_id == face_id
16992 && glyph->glyph_not_available_p == glyph_not_available_p)
16993 {
16994 int two_byte_p;
16995
16996 s->face = get_glyph_face_and_encoding (s->f, glyph,
16997 s->char2b + s->nchars,
16998 &two_byte_p);
16999 s->two_byte_p = two_byte_p;
17000 ++s->nchars;
17001 xassert (s->nchars <= end - start);
17002 s->width += glyph->pixel_width;
17003 ++glyph;
17004 }
17005
17006 s->font = s->face->font;
17007 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17008
17009 /* If the specified font could not be loaded, use the frame's font,
17010 but record the fact that we couldn't load it in
17011 S->font_not_found_p so that we can draw rectangles for the
17012 characters of the glyph string. */
17013 if (s->font == NULL || glyph_not_available_p)
17014 {
17015 s->font_not_found_p = 1;
17016 s->font = FRAME_FONT (s->f);
17017 }
17018
17019 /* Adjust base line for subscript/superscript text. */
17020 s->ybase += voffset;
17021
17022 xassert (s->face && s->face->gc);
17023 return glyph - s->row->glyphs[s->area];
17024 }
17025
17026
17027 /* Fill glyph string S from image glyph S->first_glyph. */
17028
17029 static void
17030 fill_image_glyph_string (s)
17031 struct glyph_string *s;
17032 {
17033 xassert (s->first_glyph->type == IMAGE_GLYPH);
17034 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17035 xassert (s->img);
17036 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17037 s->font = s->face->font;
17038 s->width = s->first_glyph->pixel_width;
17039
17040 /* Adjust base line for subscript/superscript text. */
17041 s->ybase += s->first_glyph->voffset;
17042 }
17043
17044
17045 /* Fill glyph string S from a sequence of stretch glyphs.
17046
17047 ROW is the glyph row in which the glyphs are found, AREA is the
17048 area within the row. START is the index of the first glyph to
17049 consider, END is the index of the last + 1.
17050
17051 Value is the index of the first glyph not in S. */
17052
17053 static int
17054 fill_stretch_glyph_string (s, row, area, start, end)
17055 struct glyph_string *s;
17056 struct glyph_row *row;
17057 enum glyph_row_area area;
17058 int start, end;
17059 {
17060 struct glyph *glyph, *last;
17061 int voffset, face_id;
17062
17063 xassert (s->first_glyph->type == STRETCH_GLYPH);
17064
17065 glyph = s->row->glyphs[s->area] + start;
17066 last = s->row->glyphs[s->area] + end;
17067 face_id = glyph->face_id;
17068 s->face = FACE_FROM_ID (s->f, face_id);
17069 s->font = s->face->font;
17070 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17071 s->width = glyph->pixel_width;
17072 voffset = glyph->voffset;
17073
17074 for (++glyph;
17075 (glyph < last
17076 && glyph->type == STRETCH_GLYPH
17077 && glyph->voffset == voffset
17078 && glyph->face_id == face_id);
17079 ++glyph)
17080 s->width += glyph->pixel_width;
17081
17082 /* Adjust base line for subscript/superscript text. */
17083 s->ybase += voffset;
17084
17085 /* The case that face->gc == 0 is handled when drawing the glyph
17086 string by calling PREPARE_FACE_FOR_DISPLAY. */
17087 xassert (s->face);
17088 return glyph - s->row->glyphs[s->area];
17089 }
17090
17091
17092 /* EXPORT for RIF:
17093 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17094 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17095 assumed to be zero. */
17096
17097 void
17098 x_get_glyph_overhangs (glyph, f, left, right)
17099 struct glyph *glyph;
17100 struct frame *f;
17101 int *left, *right;
17102 {
17103 *left = *right = 0;
17104
17105 if (glyph->type == CHAR_GLYPH)
17106 {
17107 XFontStruct *font;
17108 struct face *face;
17109 struct font_info *font_info;
17110 XChar2b char2b;
17111 XCharStruct *pcm;
17112
17113 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17114 font = face->font;
17115 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17116 if (font /* ++KFS: Should this be font_info ? */
17117 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17118 {
17119 if (pcm->rbearing > pcm->width)
17120 *right = pcm->rbearing - pcm->width;
17121 if (pcm->lbearing < 0)
17122 *left = -pcm->lbearing;
17123 }
17124 }
17125 }
17126
17127
17128 /* Return the index of the first glyph preceding glyph string S that
17129 is overwritten by S because of S's left overhang. Value is -1
17130 if no glyphs are overwritten. */
17131
17132 static int
17133 left_overwritten (s)
17134 struct glyph_string *s;
17135 {
17136 int k;
17137
17138 if (s->left_overhang)
17139 {
17140 int x = 0, i;
17141 struct glyph *glyphs = s->row->glyphs[s->area];
17142 int first = s->first_glyph - glyphs;
17143
17144 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17145 x -= glyphs[i].pixel_width;
17146
17147 k = i + 1;
17148 }
17149 else
17150 k = -1;
17151
17152 return k;
17153 }
17154
17155
17156 /* Return the index of the first glyph preceding glyph string S that
17157 is overwriting S because of its right overhang. Value is -1 if no
17158 glyph in front of S overwrites S. */
17159
17160 static int
17161 left_overwriting (s)
17162 struct glyph_string *s;
17163 {
17164 int i, k, x;
17165 struct glyph *glyphs = s->row->glyphs[s->area];
17166 int first = s->first_glyph - glyphs;
17167
17168 k = -1;
17169 x = 0;
17170 for (i = first - 1; i >= 0; --i)
17171 {
17172 int left, right;
17173 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17174 if (x + right > 0)
17175 k = i;
17176 x -= glyphs[i].pixel_width;
17177 }
17178
17179 return k;
17180 }
17181
17182
17183 /* Return the index of the last glyph following glyph string S that is
17184 not overwritten by S because of S's right overhang. Value is -1 if
17185 no such glyph is found. */
17186
17187 static int
17188 right_overwritten (s)
17189 struct glyph_string *s;
17190 {
17191 int k = -1;
17192
17193 if (s->right_overhang)
17194 {
17195 int x = 0, i;
17196 struct glyph *glyphs = s->row->glyphs[s->area];
17197 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17198 int end = s->row->used[s->area];
17199
17200 for (i = first; i < end && s->right_overhang > x; ++i)
17201 x += glyphs[i].pixel_width;
17202
17203 k = i;
17204 }
17205
17206 return k;
17207 }
17208
17209
17210 /* Return the index of the last glyph following glyph string S that
17211 overwrites S because of its left overhang. Value is negative
17212 if no such glyph is found. */
17213
17214 static int
17215 right_overwriting (s)
17216 struct glyph_string *s;
17217 {
17218 int i, k, x;
17219 int end = s->row->used[s->area];
17220 struct glyph *glyphs = s->row->glyphs[s->area];
17221 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17222
17223 k = -1;
17224 x = 0;
17225 for (i = first; i < end; ++i)
17226 {
17227 int left, right;
17228 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17229 if (x - left < 0)
17230 k = i;
17231 x += glyphs[i].pixel_width;
17232 }
17233
17234 return k;
17235 }
17236
17237
17238 /* Get face and two-byte form of character C in face FACE_ID on frame
17239 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17240 means we want to display multibyte text. DISPLAY_P non-zero means
17241 make sure that X resources for the face returned are allocated.
17242 Value is a pointer to a realized face that is ready for display if
17243 DISPLAY_P is non-zero. */
17244
17245 static INLINE struct face *
17246 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17247 struct frame *f;
17248 int c, face_id;
17249 XChar2b *char2b;
17250 int multibyte_p, display_p;
17251 {
17252 struct face *face = FACE_FROM_ID (f, face_id);
17253
17254 if (!multibyte_p)
17255 {
17256 /* Unibyte case. We don't have to encode, but we have to make
17257 sure to use a face suitable for unibyte. */
17258 STORE_XCHAR2B (char2b, 0, c);
17259 face_id = FACE_FOR_CHAR (f, face, c);
17260 face = FACE_FROM_ID (f, face_id);
17261 }
17262 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17263 {
17264 /* Case of ASCII in a face known to fit ASCII. */
17265 STORE_XCHAR2B (char2b, 0, c);
17266 }
17267 else
17268 {
17269 int c1, c2, charset;
17270
17271 /* Split characters into bytes. If c2 is -1 afterwards, C is
17272 really a one-byte character so that byte1 is zero. */
17273 SPLIT_CHAR (c, charset, c1, c2);
17274 if (c2 > 0)
17275 STORE_XCHAR2B (char2b, c1, c2);
17276 else
17277 STORE_XCHAR2B (char2b, 0, c1);
17278
17279 /* Maybe encode the character in *CHAR2B. */
17280 if (face->font != NULL)
17281 {
17282 struct font_info *font_info
17283 = FONT_INFO_FROM_ID (f, face->font_info_id);
17284 if (font_info)
17285 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17286 }
17287 }
17288
17289 /* Make sure X resources of the face are allocated. */
17290 #ifdef HAVE_X_WINDOWS
17291 if (display_p)
17292 #endif
17293 {
17294 xassert (face != NULL);
17295 PREPARE_FACE_FOR_DISPLAY (f, face);
17296 }
17297
17298 return face;
17299 }
17300
17301
17302 /* Set background width of glyph string S. START is the index of the
17303 first glyph following S. LAST_X is the right-most x-position + 1
17304 in the drawing area. */
17305
17306 static INLINE void
17307 set_glyph_string_background_width (s, start, last_x)
17308 struct glyph_string *s;
17309 int start;
17310 int last_x;
17311 {
17312 /* If the face of this glyph string has to be drawn to the end of
17313 the drawing area, set S->extends_to_end_of_line_p. */
17314 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17315
17316 if (start == s->row->used[s->area]
17317 && s->area == TEXT_AREA
17318 && ((s->hl == DRAW_NORMAL_TEXT
17319 && (s->row->fill_line_p
17320 || s->face->background != default_face->background
17321 || s->face->stipple != default_face->stipple
17322 || s->row->mouse_face_p))
17323 || s->hl == DRAW_MOUSE_FACE
17324 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17325 && s->row->fill_line_p)))
17326 s->extends_to_end_of_line_p = 1;
17327
17328 /* If S extends its face to the end of the line, set its
17329 background_width to the distance to the right edge of the drawing
17330 area. */
17331 if (s->extends_to_end_of_line_p)
17332 s->background_width = last_x - s->x + 1;
17333 else
17334 s->background_width = s->width;
17335 }
17336
17337
17338 /* Compute overhangs and x-positions for glyph string S and its
17339 predecessors, or successors. X is the starting x-position for S.
17340 BACKWARD_P non-zero means process predecessors. */
17341
17342 static void
17343 compute_overhangs_and_x (s, x, backward_p)
17344 struct glyph_string *s;
17345 int x;
17346 int backward_p;
17347 {
17348 if (backward_p)
17349 {
17350 while (s)
17351 {
17352 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17353 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17354 x -= s->width;
17355 s->x = x;
17356 s = s->prev;
17357 }
17358 }
17359 else
17360 {
17361 while (s)
17362 {
17363 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17364 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17365 s->x = x;
17366 x += s->width;
17367 s = s->next;
17368 }
17369 }
17370 }
17371
17372
17373
17374 /* The following macros are only called from draw_glyphs below.
17375 They reference the following parameters of that function directly:
17376 `w', `row', `area', and `overlap_p'
17377 as well as the following local variables:
17378 `s', `f', and `hdc' (in W32) */
17379
17380 #ifdef HAVE_NTGUI
17381 /* On W32, silently add local `hdc' variable to argument list of
17382 init_glyph_string. */
17383 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17384 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17385 #else
17386 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17387 init_glyph_string (s, char2b, w, row, area, start, hl)
17388 #endif
17389
17390 /* Add a glyph string for a stretch glyph to the list of strings
17391 between HEAD and TAIL. START is the index of the stretch glyph in
17392 row area AREA of glyph row ROW. END is the index of the last glyph
17393 in that glyph row area. X is the current output position assigned
17394 to the new glyph string constructed. HL overrides that face of the
17395 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17396 is the right-most x-position of the drawing area. */
17397
17398 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17399 and below -- keep them on one line. */
17400 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17401 do \
17402 { \
17403 s = (struct glyph_string *) alloca (sizeof *s); \
17404 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17405 START = fill_stretch_glyph_string (s, row, area, START, END); \
17406 append_glyph_string (&HEAD, &TAIL, s); \
17407 s->x = (X); \
17408 } \
17409 while (0)
17410
17411
17412 /* Add a glyph string for an image glyph to the list of strings
17413 between HEAD and TAIL. START is the index of the image glyph in
17414 row area AREA of glyph row ROW. END is the index of the last glyph
17415 in that glyph row area. X is the current output position assigned
17416 to the new glyph string constructed. HL overrides that face of the
17417 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17418 is the right-most x-position of the drawing area. */
17419
17420 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17421 do \
17422 { \
17423 s = (struct glyph_string *) alloca (sizeof *s); \
17424 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17425 fill_image_glyph_string (s); \
17426 append_glyph_string (&HEAD, &TAIL, s); \
17427 ++START; \
17428 s->x = (X); \
17429 } \
17430 while (0)
17431
17432
17433 /* Add a glyph string for a sequence of character glyphs to the list
17434 of strings between HEAD and TAIL. START is the index of the first
17435 glyph in row area AREA of glyph row ROW that is part of the new
17436 glyph string. END is the index of the last glyph in that glyph row
17437 area. X is the current output position assigned to the new glyph
17438 string constructed. HL overrides that face of the glyph; e.g. it
17439 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17440 right-most x-position of the drawing area. */
17441
17442 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17443 do \
17444 { \
17445 int c, face_id; \
17446 XChar2b *char2b; \
17447 \
17448 c = (row)->glyphs[area][START].u.ch; \
17449 face_id = (row)->glyphs[area][START].face_id; \
17450 \
17451 s = (struct glyph_string *) alloca (sizeof *s); \
17452 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17453 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17454 append_glyph_string (&HEAD, &TAIL, s); \
17455 s->x = (X); \
17456 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17457 } \
17458 while (0)
17459
17460
17461 /* Add a glyph string for a composite sequence to the list of strings
17462 between HEAD and TAIL. START is the index of the first glyph in
17463 row area AREA of glyph row ROW that is part of the new glyph
17464 string. END is the index of the last glyph in that glyph row area.
17465 X is the current output position assigned to the new glyph string
17466 constructed. HL overrides that face of the glyph; e.g. it is
17467 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17468 x-position of the drawing area. */
17469
17470 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17471 do { \
17472 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17473 int face_id = (row)->glyphs[area][START].face_id; \
17474 struct face *base_face = FACE_FROM_ID (f, face_id); \
17475 struct composition *cmp = composition_table[cmp_id]; \
17476 int glyph_len = cmp->glyph_len; \
17477 XChar2b *char2b; \
17478 struct face **faces; \
17479 struct glyph_string *first_s = NULL; \
17480 int n; \
17481 \
17482 base_face = base_face->ascii_face; \
17483 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17484 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17485 /* At first, fill in `char2b' and `faces'. */ \
17486 for (n = 0; n < glyph_len; n++) \
17487 { \
17488 int c = COMPOSITION_GLYPH (cmp, n); \
17489 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17490 faces[n] = FACE_FROM_ID (f, this_face_id); \
17491 get_char_face_and_encoding (f, c, this_face_id, \
17492 char2b + n, 1, 1); \
17493 } \
17494 \
17495 /* Make glyph_strings for each glyph sequence that is drawable by \
17496 the same face, and append them to HEAD/TAIL. */ \
17497 for (n = 0; n < cmp->glyph_len;) \
17498 { \
17499 s = (struct glyph_string *) alloca (sizeof *s); \
17500 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17501 append_glyph_string (&(HEAD), &(TAIL), s); \
17502 s->cmp = cmp; \
17503 s->gidx = n; \
17504 s->x = (X); \
17505 \
17506 if (n == 0) \
17507 first_s = s; \
17508 \
17509 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17510 } \
17511 \
17512 ++START; \
17513 s = first_s; \
17514 } while (0)
17515
17516
17517 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17518 of AREA of glyph row ROW on window W between indices START and END.
17519 HL overrides the face for drawing glyph strings, e.g. it is
17520 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17521 x-positions of the drawing area.
17522
17523 This is an ugly monster macro construct because we must use alloca
17524 to allocate glyph strings (because draw_glyphs can be called
17525 asynchronously). */
17526
17527 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17528 do \
17529 { \
17530 HEAD = TAIL = NULL; \
17531 while (START < END) \
17532 { \
17533 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17534 switch (first_glyph->type) \
17535 { \
17536 case CHAR_GLYPH: \
17537 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17538 HL, X, LAST_X); \
17539 break; \
17540 \
17541 case COMPOSITE_GLYPH: \
17542 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17543 HL, X, LAST_X); \
17544 break; \
17545 \
17546 case STRETCH_GLYPH: \
17547 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17548 HL, X, LAST_X); \
17549 break; \
17550 \
17551 case IMAGE_GLYPH: \
17552 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17553 HL, X, LAST_X); \
17554 break; \
17555 \
17556 default: \
17557 abort (); \
17558 } \
17559 \
17560 set_glyph_string_background_width (s, START, LAST_X); \
17561 (X) += s->width; \
17562 } \
17563 } \
17564 while (0)
17565
17566
17567 /* Draw glyphs between START and END in AREA of ROW on window W,
17568 starting at x-position X. X is relative to AREA in W. HL is a
17569 face-override with the following meaning:
17570
17571 DRAW_NORMAL_TEXT draw normally
17572 DRAW_CURSOR draw in cursor face
17573 DRAW_MOUSE_FACE draw in mouse face.
17574 DRAW_INVERSE_VIDEO draw in mode line face
17575 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17576 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17577
17578 If OVERLAPS_P is non-zero, draw only the foreground of characters
17579 and clip to the physical height of ROW.
17580
17581 Value is the x-position reached, relative to AREA of W. */
17582
17583 static int
17584 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17585 struct window *w;
17586 int x;
17587 struct glyph_row *row;
17588 enum glyph_row_area area;
17589 int start, end;
17590 enum draw_glyphs_face hl;
17591 int overlaps_p;
17592 {
17593 struct glyph_string *head, *tail;
17594 struct glyph_string *s;
17595 int last_x, area_width;
17596 int x_reached;
17597 int i, j;
17598 struct frame *f = XFRAME (WINDOW_FRAME (w));
17599 DECLARE_HDC (hdc);
17600
17601 ALLOCATE_HDC (hdc, f);
17602
17603 /* Let's rather be paranoid than getting a SEGV. */
17604 end = min (end, row->used[area]);
17605 start = max (0, start);
17606 start = min (end, start);
17607
17608 /* Translate X to frame coordinates. Set last_x to the right
17609 end of the drawing area. */
17610 if (row->full_width_p)
17611 {
17612 /* X is relative to the left edge of W, without scroll bars
17613 or fringes. */
17614 x += WINDOW_LEFT_EDGE_X (w);
17615 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17616 }
17617 else
17618 {
17619 int area_left = window_box_left (w, area);
17620 x += area_left;
17621 area_width = window_box_width (w, area);
17622 last_x = area_left + area_width;
17623 }
17624
17625 /* Build a doubly-linked list of glyph_string structures between
17626 head and tail from what we have to draw. Note that the macro
17627 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17628 the reason we use a separate variable `i'. */
17629 i = start;
17630 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17631 if (tail)
17632 x_reached = tail->x + tail->background_width;
17633 else
17634 x_reached = x;
17635
17636 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17637 the row, redraw some glyphs in front or following the glyph
17638 strings built above. */
17639 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17640 {
17641 int dummy_x = 0;
17642 struct glyph_string *h, *t;
17643
17644 /* Compute overhangs for all glyph strings. */
17645 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
17646 for (s = head; s; s = s->next)
17647 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
17648
17649 /* Prepend glyph strings for glyphs in front of the first glyph
17650 string that are overwritten because of the first glyph
17651 string's left overhang. The background of all strings
17652 prepended must be drawn because the first glyph string
17653 draws over it. */
17654 i = left_overwritten (head);
17655 if (i >= 0)
17656 {
17657 j = i;
17658 BUILD_GLYPH_STRINGS (j, start, h, t,
17659 DRAW_NORMAL_TEXT, dummy_x, last_x);
17660 start = i;
17661 compute_overhangs_and_x (t, head->x, 1);
17662 prepend_glyph_string_lists (&head, &tail, h, t);
17663 }
17664
17665 /* Prepend glyph strings for glyphs in front of the first glyph
17666 string that overwrite that glyph string because of their
17667 right overhang. For these strings, only the foreground must
17668 be drawn, because it draws over the glyph string at `head'.
17669 The background must not be drawn because this would overwrite
17670 right overhangs of preceding glyphs for which no glyph
17671 strings exist. */
17672 i = left_overwriting (head);
17673 if (i >= 0)
17674 {
17675 BUILD_GLYPH_STRINGS (i, start, h, t,
17676 DRAW_NORMAL_TEXT, dummy_x, last_x);
17677 for (s = h; s; s = s->next)
17678 s->background_filled_p = 1;
17679 compute_overhangs_and_x (t, head->x, 1);
17680 prepend_glyph_string_lists (&head, &tail, h, t);
17681 }
17682
17683 /* Append glyphs strings for glyphs following the last glyph
17684 string tail that are overwritten by tail. The background of
17685 these strings has to be drawn because tail's foreground draws
17686 over it. */
17687 i = right_overwritten (tail);
17688 if (i >= 0)
17689 {
17690 BUILD_GLYPH_STRINGS (end, i, h, t,
17691 DRAW_NORMAL_TEXT, x, last_x);
17692 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17693 append_glyph_string_lists (&head, &tail, h, t);
17694 }
17695
17696 /* Append glyph strings for glyphs following the last glyph
17697 string tail that overwrite tail. The foreground of such
17698 glyphs has to be drawn because it writes into the background
17699 of tail. The background must not be drawn because it could
17700 paint over the foreground of following glyphs. */
17701 i = right_overwriting (tail);
17702 if (i >= 0)
17703 {
17704 BUILD_GLYPH_STRINGS (end, i, h, t,
17705 DRAW_NORMAL_TEXT, x, last_x);
17706 for (s = h; s; s = s->next)
17707 s->background_filled_p = 1;
17708 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17709 append_glyph_string_lists (&head, &tail, h, t);
17710 }
17711 }
17712
17713 /* Draw all strings. */
17714 for (s = head; s; s = s->next)
17715 FRAME_RIF (f)->draw_glyph_string (s);
17716
17717 if (area == TEXT_AREA
17718 && !row->full_width_p
17719 /* When drawing overlapping rows, only the glyph strings'
17720 foreground is drawn, which doesn't erase a cursor
17721 completely. */
17722 && !overlaps_p)
17723 {
17724 int x0 = head ? head->x : x;
17725 int x1 = tail ? tail->x + tail->background_width : x;
17726
17727 int text_left = window_box_left (w, TEXT_AREA);
17728 x0 -= text_left;
17729 x1 -= text_left;
17730
17731 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
17732 row->y, MATRIX_ROW_BOTTOM_Y (row));
17733 }
17734
17735 /* Value is the x-position up to which drawn, relative to AREA of W.
17736 This doesn't include parts drawn because of overhangs. */
17737 if (row->full_width_p)
17738 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17739 else
17740 x_reached -= window_box_left (w, area);
17741
17742 RELEASE_HDC (hdc, f);
17743
17744 return x_reached;
17745 }
17746
17747
17748 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17749 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17750
17751 static INLINE void
17752 append_glyph (it)
17753 struct it *it;
17754 {
17755 struct glyph *glyph;
17756 enum glyph_row_area area = it->area;
17757
17758 xassert (it->glyph_row);
17759 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17760
17761 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17762 if (glyph < it->glyph_row->glyphs[area + 1])
17763 {
17764 glyph->charpos = CHARPOS (it->position);
17765 glyph->object = it->object;
17766 glyph->pixel_width = it->pixel_width;
17767 glyph->ascent = it->ascent;
17768 glyph->descent = it->descent;
17769 glyph->voffset = it->voffset;
17770 glyph->type = CHAR_GLYPH;
17771 glyph->multibyte_p = it->multibyte_p;
17772 glyph->left_box_line_p = it->start_of_box_run_p;
17773 glyph->right_box_line_p = it->end_of_box_run_p;
17774 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17775 || it->phys_descent > it->descent);
17776 glyph->padding_p = 0;
17777 glyph->glyph_not_available_p = it->glyph_not_available_p;
17778 glyph->face_id = it->face_id;
17779 glyph->u.ch = it->char_to_display;
17780 glyph->font_type = FONT_TYPE_UNKNOWN;
17781 ++it->glyph_row->used[area];
17782 }
17783 }
17784
17785 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17786 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17787
17788 static INLINE void
17789 append_composite_glyph (it)
17790 struct it *it;
17791 {
17792 struct glyph *glyph;
17793 enum glyph_row_area area = it->area;
17794
17795 xassert (it->glyph_row);
17796
17797 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17798 if (glyph < it->glyph_row->glyphs[area + 1])
17799 {
17800 glyph->charpos = CHARPOS (it->position);
17801 glyph->object = it->object;
17802 glyph->pixel_width = it->pixel_width;
17803 glyph->ascent = it->ascent;
17804 glyph->descent = it->descent;
17805 glyph->voffset = it->voffset;
17806 glyph->type = COMPOSITE_GLYPH;
17807 glyph->multibyte_p = it->multibyte_p;
17808 glyph->left_box_line_p = it->start_of_box_run_p;
17809 glyph->right_box_line_p = it->end_of_box_run_p;
17810 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17811 || it->phys_descent > it->descent);
17812 glyph->padding_p = 0;
17813 glyph->glyph_not_available_p = 0;
17814 glyph->face_id = it->face_id;
17815 glyph->u.cmp_id = it->cmp_id;
17816 glyph->font_type = FONT_TYPE_UNKNOWN;
17817 ++it->glyph_row->used[area];
17818 }
17819 }
17820
17821
17822 /* Change IT->ascent and IT->height according to the setting of
17823 IT->voffset. */
17824
17825 static INLINE void
17826 take_vertical_position_into_account (it)
17827 struct it *it;
17828 {
17829 if (it->voffset)
17830 {
17831 if (it->voffset < 0)
17832 /* Increase the ascent so that we can display the text higher
17833 in the line. */
17834 it->ascent += abs (it->voffset);
17835 else
17836 /* Increase the descent so that we can display the text lower
17837 in the line. */
17838 it->descent += it->voffset;
17839 }
17840 }
17841
17842
17843 /* Produce glyphs/get display metrics for the image IT is loaded with.
17844 See the description of struct display_iterator in dispextern.h for
17845 an overview of struct display_iterator. */
17846
17847 static void
17848 produce_image_glyph (it)
17849 struct it *it;
17850 {
17851 struct image *img;
17852 struct face *face;
17853 int face_ascent, glyph_ascent;
17854
17855 xassert (it->what == IT_IMAGE);
17856
17857 face = FACE_FROM_ID (it->f, it->face_id);
17858 xassert (face);
17859 /* Make sure X resources of the face is loaded. */
17860 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17861
17862 if (it->image_id < 0)
17863 {
17864 /* Fringe bitmap. */
17865 it->ascent = it->phys_ascent = 0;
17866 it->descent = it->phys_descent = 0;
17867 it->pixel_width = 0;
17868 it->nglyphs = 0;
17869 return;
17870 }
17871
17872 img = IMAGE_FROM_ID (it->f, it->image_id);
17873 xassert (img);
17874 /* Make sure X resources of the image is loaded. */
17875 prepare_image_for_display (it->f, img);
17876
17877 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face);
17878 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17879 it->pixel_width = img->width + 2 * img->hmargin;
17880
17881 /* It's quite possible for images to have an ascent greater than
17882 their height, so don't get confused in that case. */
17883 if (it->descent < 0)
17884 it->descent = 0;
17885
17886 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
17887 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
17888 if (face_ascent > it->ascent)
17889 it->ascent = it->phys_ascent = face_ascent;
17890
17891 it->nglyphs = 1;
17892
17893 if (face->box != FACE_NO_BOX)
17894 {
17895 if (face->box_line_width > 0)
17896 {
17897 it->ascent += face->box_line_width;
17898 it->descent += face->box_line_width;
17899 }
17900
17901 if (it->start_of_box_run_p)
17902 it->pixel_width += abs (face->box_line_width);
17903 if (it->end_of_box_run_p)
17904 it->pixel_width += abs (face->box_line_width);
17905 }
17906
17907 take_vertical_position_into_account (it);
17908
17909 if (it->glyph_row)
17910 {
17911 struct glyph *glyph;
17912 enum glyph_row_area area = it->area;
17913
17914 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17915 if (glyph < it->glyph_row->glyphs[area + 1])
17916 {
17917 glyph->charpos = CHARPOS (it->position);
17918 glyph->object = it->object;
17919 glyph->pixel_width = it->pixel_width;
17920 glyph->ascent = glyph_ascent;
17921 glyph->descent = it->descent;
17922 glyph->voffset = it->voffset;
17923 glyph->type = IMAGE_GLYPH;
17924 glyph->multibyte_p = it->multibyte_p;
17925 glyph->left_box_line_p = it->start_of_box_run_p;
17926 glyph->right_box_line_p = it->end_of_box_run_p;
17927 glyph->overlaps_vertically_p = 0;
17928 glyph->padding_p = 0;
17929 glyph->glyph_not_available_p = 0;
17930 glyph->face_id = it->face_id;
17931 glyph->u.img_id = img->id;
17932 glyph->font_type = FONT_TYPE_UNKNOWN;
17933 ++it->glyph_row->used[area];
17934 }
17935 }
17936 }
17937
17938
17939 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17940 of the glyph, WIDTH and HEIGHT are the width and height of the
17941 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
17942
17943 static void
17944 append_stretch_glyph (it, object, width, height, ascent)
17945 struct it *it;
17946 Lisp_Object object;
17947 int width, height;
17948 int ascent;
17949 {
17950 struct glyph *glyph;
17951 enum glyph_row_area area = it->area;
17952
17953 xassert (ascent >= 0 && ascent <= height);
17954
17955 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17956 if (glyph < it->glyph_row->glyphs[area + 1])
17957 {
17958 glyph->charpos = CHARPOS (it->position);
17959 glyph->object = object;
17960 glyph->pixel_width = width;
17961 glyph->ascent = ascent;
17962 glyph->descent = height - ascent;
17963 glyph->voffset = it->voffset;
17964 glyph->type = STRETCH_GLYPH;
17965 glyph->multibyte_p = it->multibyte_p;
17966 glyph->left_box_line_p = it->start_of_box_run_p;
17967 glyph->right_box_line_p = it->end_of_box_run_p;
17968 glyph->overlaps_vertically_p = 0;
17969 glyph->padding_p = 0;
17970 glyph->glyph_not_available_p = 0;
17971 glyph->face_id = it->face_id;
17972 glyph->u.stretch.ascent = ascent;
17973 glyph->u.stretch.height = height;
17974 glyph->font_type = FONT_TYPE_UNKNOWN;
17975 ++it->glyph_row->used[area];
17976 }
17977 }
17978
17979
17980 /* Calculate a width or height in pixels from a specification using
17981 the following elements:
17982
17983 SPEC ::=
17984 NUM - a (fractional) multiple of the default font width/height
17985 (NUM) - specifies exactly NUM pixels
17986 UNIT - a fixed number of pixels, see below.
17987 ELEMENT - size of a display element in pixels, see below.
17988 (NUM . SPEC) - equals NUM * SPEC
17989 (+ SPEC SPEC ...) - add pixel values
17990 (- SPEC SPEC ...) - subtract pixel values
17991 (- SPEC) - negate pixel value
17992
17993 NUM ::=
17994 INT or FLOAT - a number constant
17995 SYMBOL - use symbol's (buffer local) variable binding.
17996
17997 UNIT ::=
17998 in - pixels per inch *)
17999 mm - pixels per 1/1000 meter *)
18000 cm - pixels per 1/100 meter *)
18001 width - width of current font in pixels.
18002 height - height of current font in pixels.
18003
18004 *) using the ratio(s) defined in display-pixels-per-inch.
18005
18006 ELEMENT ::=
18007
18008 left-fringe - left fringe width in pixels
18009 (left-fringe . nil) - left fringe width if inside margins, else 0
18010 (left-fringe . t) - left fringe width if outside margins, else 0
18011
18012 right-fringe - right fringe width in pixels
18013 (right-fringe . nil) - right fringe width if inside margins, else 0
18014 (right-fringe . t) - right fringe width if outside margins, else 0
18015
18016 left-margin - left margin width in pixels
18017 right-margin - right margin width in pixels
18018
18019 scroll-bar - scroll-bar area width in pixels
18020 (scroll-bar . left) - scroll-bar width if on left, else 0
18021 (scroll-bar . right) - scroll-bar width if on right, else 0
18022
18023 Examples:
18024
18025 Pixels corresponding to 5 inches:
18026 (5 . in)
18027
18028 Total width of non-text areas on left side of window:
18029 (+ left-fringe left-margin (scroll-bar . left))
18030
18031 Total width of fringes if inside display margins:
18032 (+ (left-fringe) (right-fringe))
18033
18034 Width of left margin minus width of 1 character in the default font:
18035 (- left-margin 1)
18036
18037 Width of left margin minus width of 2 characters in the current font:
18038 (- left-margin (2 . width))
18039
18040 Width of left fringe plus left margin minus one pixel:
18041 (- (+ left-fringe left-margin) (1))
18042 (+ left-fringe left-margin (- (1)))
18043 (+ left-fringe left-margin (-1))
18044
18045 */
18046
18047 #define NUMVAL(X) \
18048 ((INTEGERP (X) || FLOATP (X)) \
18049 ? XFLOATINT (X) \
18050 : - 1)
18051
18052 static int
18053 calc_pixel_width_or_height (res, it, prop, font, width_p)
18054 double *res;
18055 struct it *it;
18056 Lisp_Object prop;
18057 XFontStruct *font;
18058 int width_p;
18059 {
18060 double pixels;
18061
18062 #define OK_PIXELS(val) ((*res = (val)), 1)
18063
18064 if (SYMBOLP (prop))
18065 {
18066 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18067 {
18068 char *unit = SDATA (SYMBOL_NAME (prop));
18069
18070 if (unit[0] == 'i' && unit[1] == 'n')
18071 pixels = 1.0;
18072 else if (unit[0] == 'm' && unit[1] == 'm')
18073 pixels = 25.4;
18074 else if (unit[0] == 'c' && unit[1] == 'm')
18075 pixels = 2.54;
18076 else
18077 pixels = 0;
18078 if (pixels > 0)
18079 {
18080 double ppi;
18081 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18082 || (CONSP (Vdisplay_pixels_per_inch)
18083 && (ppi = (width_p
18084 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18085 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18086 ppi > 0)))
18087 return OK_PIXELS (ppi / pixels);
18088
18089 return 0;
18090 }
18091 }
18092
18093 if (EQ (prop, Qheight))
18094 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18095 if (EQ (prop, Qwidth))
18096 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18097 if (EQ (prop, Qleft_fringe))
18098 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18099 if (EQ (prop, Qright_fringe))
18100 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18101 if (EQ (prop, Qleft_margin))
18102 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18103 if (EQ (prop, Qright_margin))
18104 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18105 if (EQ (prop, Qscroll_bar))
18106 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18107
18108 prop = Fbuffer_local_value (prop, it->w->buffer);
18109 }
18110
18111 if (INTEGERP (prop) || FLOATP (prop))
18112 {
18113 int base_unit = (width_p
18114 ? FRAME_COLUMN_WIDTH (it->f)
18115 : FRAME_LINE_HEIGHT (it->f));
18116 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18117 }
18118
18119 if (CONSP (prop))
18120 {
18121 Lisp_Object car = XCAR (prop);
18122 Lisp_Object cdr = XCDR (prop);
18123
18124 if (SYMBOLP (car))
18125 {
18126 if (EQ (car, Qplus) || EQ (car, Qminus))
18127 {
18128 int first = 1;
18129 double px;
18130
18131 pixels = 0;
18132 while (CONSP (cdr))
18133 {
18134 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr), font, width_p))
18135 return 0;
18136 if (first)
18137 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18138 else
18139 pixels += px;
18140 cdr = XCDR (cdr);
18141 }
18142 if (EQ (car, Qminus))
18143 pixels = -pixels;
18144 return OK_PIXELS (pixels);
18145 }
18146
18147 if (EQ (car, Qleft_fringe))
18148 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18149 == !NILP (cdr))
18150 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
18151 : 0);
18152 if (EQ (car, Qright_fringe))
18153 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18154 == !NILP (cdr))
18155 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18156 : 0);
18157 if (EQ (car, Qscroll_bar))
18158 return OK_PIXELS ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18159 == EQ (cdr, Qleft))
18160 ? WINDOW_SCROLL_BAR_AREA_WIDTH (it->w)
18161 : 0);
18162
18163 car = Fbuffer_local_value (car, it->w->buffer);
18164 }
18165
18166 if (INTEGERP (car) || FLOATP (car))
18167 {
18168 double fact;
18169 pixels = XFLOATINT (car);
18170 if (NILP (cdr))
18171 return OK_PIXELS (pixels);
18172 if (calc_pixel_width_or_height (&fact, it, cdr, font, width_p))
18173 return OK_PIXELS (pixels * fact);
18174 return 0;
18175 }
18176
18177 return 0;
18178 }
18179
18180 return 0;
18181 }
18182
18183 /* Produce a stretch glyph for iterator IT. IT->object is the value
18184 of the glyph property displayed. The value must be a list
18185 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18186 being recognized:
18187
18188 1. `:width WIDTH' specifies that the space should be WIDTH *
18189 canonical char width wide. WIDTH may be an integer or floating
18190 point number.
18191
18192 2. `:relative-width FACTOR' specifies that the width of the stretch
18193 should be computed from the width of the first character having the
18194 `glyph' property, and should be FACTOR times that width.
18195
18196 3. `:align-to HPOS' specifies that the space should be wide enough
18197 to reach HPOS, a value in canonical character units.
18198
18199 Exactly one of the above pairs must be present.
18200
18201 4. `:height HEIGHT' specifies that the height of the stretch produced
18202 should be HEIGHT, measured in canonical character units.
18203
18204 5. `:relative-height FACTOR' specifies that the height of the
18205 stretch should be FACTOR times the height of the characters having
18206 the glyph property.
18207
18208 Either none or exactly one of 4 or 5 must be present.
18209
18210 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18211 of the stretch should be used for the ascent of the stretch.
18212 ASCENT must be in the range 0 <= ASCENT <= 100. */
18213
18214 static void
18215 produce_stretch_glyph (it)
18216 struct it *it;
18217 {
18218 /* (space :width WIDTH :height HEIGHT ...) */
18219 Lisp_Object prop, plist;
18220 int width = 0, height = 0;
18221 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18222 int ascent = 0;
18223 double tem;
18224 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18225 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18226
18227 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18228
18229 /* List should start with `space'. */
18230 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18231 plist = XCDR (it->object);
18232
18233 /* Compute the width of the stretch. */
18234 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18235 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18236 {
18237 /* Absolute width `:width WIDTH' specified and valid. */
18238 zero_width_ok_p = 1;
18239 width = (int)tem;
18240 }
18241 else if (prop = Fplist_get (plist, QCrelative_width),
18242 NUMVAL (prop) > 0)
18243 {
18244 /* Relative width `:relative-width FACTOR' specified and valid.
18245 Compute the width of the characters having the `glyph'
18246 property. */
18247 struct it it2;
18248 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18249
18250 it2 = *it;
18251 if (it->multibyte_p)
18252 {
18253 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18254 - IT_BYTEPOS (*it));
18255 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18256 }
18257 else
18258 it2.c = *p, it2.len = 1;
18259
18260 it2.glyph_row = NULL;
18261 it2.what = IT_CHARACTER;
18262 x_produce_glyphs (&it2);
18263 width = NUMVAL (prop) * it2.pixel_width;
18264 }
18265 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18266 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18267 {
18268 width = max (0, (int)tem - it->current_x);
18269 zero_width_ok_p = 1;
18270 }
18271 else
18272 /* Nothing specified -> width defaults to canonical char width. */
18273 width = FRAME_COLUMN_WIDTH (it->f);
18274
18275 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18276 width = 1;
18277
18278 /* Compute height. */
18279 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18280 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18281 {
18282 height = (int)tem;
18283 zero_height_ok_p = 1;
18284 }
18285 else if (prop = Fplist_get (plist, QCrelative_height),
18286 NUMVAL (prop) > 0)
18287 height = FONT_HEIGHT (font) * NUMVAL (prop);
18288 else
18289 height = FONT_HEIGHT (font);
18290
18291 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18292 height = 1;
18293
18294 /* Compute percentage of height used for ascent. If
18295 `:ascent ASCENT' is present and valid, use that. Otherwise,
18296 derive the ascent from the font in use. */
18297 if (prop = Fplist_get (plist, QCascent),
18298 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18299 ascent = height * NUMVAL (prop) / 100.0;
18300 else if (!NILP (prop)
18301 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18302 ascent = min (max (0, (int)tem), height);
18303 else
18304 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18305
18306 if (width > 0 && height > 0 && it->glyph_row)
18307 {
18308 Lisp_Object object = it->stack[it->sp - 1].string;
18309 if (!STRINGP (object))
18310 object = it->w->buffer;
18311 append_stretch_glyph (it, object, width, height, ascent);
18312 }
18313
18314 it->pixel_width = width;
18315 it->ascent = it->phys_ascent = ascent;
18316 it->descent = it->phys_descent = height - it->ascent;
18317 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18318
18319 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18320 {
18321 if (face->box_line_width > 0)
18322 {
18323 it->ascent += face->box_line_width;
18324 it->descent += face->box_line_width;
18325 }
18326
18327 if (it->start_of_box_run_p)
18328 it->pixel_width += abs (face->box_line_width);
18329 if (it->end_of_box_run_p)
18330 it->pixel_width += abs (face->box_line_width);
18331 }
18332
18333 take_vertical_position_into_account (it);
18334 }
18335
18336 /* RIF:
18337 Produce glyphs/get display metrics for the display element IT is
18338 loaded with. See the description of struct display_iterator in
18339 dispextern.h for an overview of struct display_iterator. */
18340
18341 void
18342 x_produce_glyphs (it)
18343 struct it *it;
18344 {
18345 it->glyph_not_available_p = 0;
18346
18347 if (it->what == IT_CHARACTER)
18348 {
18349 XChar2b char2b;
18350 XFontStruct *font;
18351 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18352 XCharStruct *pcm;
18353 int font_not_found_p;
18354 struct font_info *font_info;
18355 int boff; /* baseline offset */
18356 /* We may change it->multibyte_p upon unibyte<->multibyte
18357 conversion. So, save the current value now and restore it
18358 later.
18359
18360 Note: It seems that we don't have to record multibyte_p in
18361 struct glyph because the character code itself tells if or
18362 not the character is multibyte. Thus, in the future, we must
18363 consider eliminating the field `multibyte_p' in the struct
18364 glyph. */
18365 int saved_multibyte_p = it->multibyte_p;
18366
18367 /* Maybe translate single-byte characters to multibyte, or the
18368 other way. */
18369 it->char_to_display = it->c;
18370 if (!ASCII_BYTE_P (it->c))
18371 {
18372 if (unibyte_display_via_language_environment
18373 && SINGLE_BYTE_CHAR_P (it->c)
18374 && (it->c >= 0240
18375 || !NILP (Vnonascii_translation_table)))
18376 {
18377 it->char_to_display = unibyte_char_to_multibyte (it->c);
18378 it->multibyte_p = 1;
18379 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18380 face = FACE_FROM_ID (it->f, it->face_id);
18381 }
18382 else if (!SINGLE_BYTE_CHAR_P (it->c)
18383 && !it->multibyte_p)
18384 {
18385 it->multibyte_p = 1;
18386 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18387 face = FACE_FROM_ID (it->f, it->face_id);
18388 }
18389 }
18390
18391 /* Get font to use. Encode IT->char_to_display. */
18392 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18393 &char2b, it->multibyte_p, 0);
18394 font = face->font;
18395
18396 /* When no suitable font found, use the default font. */
18397 font_not_found_p = font == NULL;
18398 if (font_not_found_p)
18399 {
18400 font = FRAME_FONT (it->f);
18401 boff = FRAME_BASELINE_OFFSET (it->f);
18402 font_info = NULL;
18403 }
18404 else
18405 {
18406 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18407 boff = font_info->baseline_offset;
18408 if (font_info->vertical_centering)
18409 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18410 }
18411
18412 if (it->char_to_display >= ' '
18413 && (!it->multibyte_p || it->char_to_display < 128))
18414 {
18415 /* Either unibyte or ASCII. */
18416 int stretched_p;
18417
18418 it->nglyphs = 1;
18419
18420 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18421 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18422 it->ascent = FONT_BASE (font) + boff;
18423 it->descent = FONT_DESCENT (font) - boff;
18424
18425 if (pcm)
18426 {
18427 it->phys_ascent = pcm->ascent + boff;
18428 it->phys_descent = pcm->descent - boff;
18429 it->pixel_width = pcm->width;
18430 }
18431 else
18432 {
18433 it->glyph_not_available_p = 1;
18434 it->phys_ascent = FONT_BASE (font) + boff;
18435 it->phys_descent = FONT_DESCENT (font) - boff;
18436 it->pixel_width = FONT_WIDTH (font);
18437 }
18438
18439 /* If this is a space inside a region of text with
18440 `space-width' property, change its width. */
18441 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18442 if (stretched_p)
18443 it->pixel_width *= XFLOATINT (it->space_width);
18444
18445 /* If face has a box, add the box thickness to the character
18446 height. If character has a box line to the left and/or
18447 right, add the box line width to the character's width. */
18448 if (face->box != FACE_NO_BOX)
18449 {
18450 int thick = face->box_line_width;
18451
18452 if (thick > 0)
18453 {
18454 it->ascent += thick;
18455 it->descent += thick;
18456 }
18457 else
18458 thick = -thick;
18459
18460 if (it->start_of_box_run_p)
18461 it->pixel_width += thick;
18462 if (it->end_of_box_run_p)
18463 it->pixel_width += thick;
18464 }
18465
18466 /* If face has an overline, add the height of the overline
18467 (1 pixel) and a 1 pixel margin to the character height. */
18468 if (face->overline_p)
18469 it->ascent += 2;
18470
18471 take_vertical_position_into_account (it);
18472
18473 /* If we have to actually produce glyphs, do it. */
18474 if (it->glyph_row)
18475 {
18476 if (stretched_p)
18477 {
18478 /* Translate a space with a `space-width' property
18479 into a stretch glyph. */
18480 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18481 / FONT_HEIGHT (font));
18482 append_stretch_glyph (it, it->object, it->pixel_width,
18483 it->ascent + it->descent, ascent);
18484 }
18485 else
18486 append_glyph (it);
18487
18488 /* If characters with lbearing or rbearing are displayed
18489 in this line, record that fact in a flag of the
18490 glyph row. This is used to optimize X output code. */
18491 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18492 it->glyph_row->contains_overlapping_glyphs_p = 1;
18493 }
18494 }
18495 else if (it->char_to_display == '\n')
18496 {
18497 /* A newline has no width but we need the height of the line. */
18498 it->pixel_width = 0;
18499 it->nglyphs = 0;
18500 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18501 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18502
18503 if (face->box != FACE_NO_BOX
18504 && face->box_line_width > 0)
18505 {
18506 it->ascent += face->box_line_width;
18507 it->descent += face->box_line_width;
18508 }
18509 }
18510 else if (it->char_to_display == '\t')
18511 {
18512 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18513 int x = it->current_x + it->continuation_lines_width;
18514 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18515
18516 /* If the distance from the current position to the next tab
18517 stop is less than a canonical character width, use the
18518 tab stop after that. */
18519 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18520 next_tab_x += tab_width;
18521
18522 it->pixel_width = next_tab_x - x;
18523 it->nglyphs = 1;
18524 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18525 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18526
18527 if (it->glyph_row)
18528 {
18529 append_stretch_glyph (it, it->object, it->pixel_width,
18530 it->ascent + it->descent, it->ascent);
18531 }
18532 }
18533 else
18534 {
18535 /* A multi-byte character. Assume that the display width of the
18536 character is the width of the character multiplied by the
18537 width of the font. */
18538
18539 /* If we found a font, this font should give us the right
18540 metrics. If we didn't find a font, use the frame's
18541 default font and calculate the width of the character
18542 from the charset width; this is what old redisplay code
18543 did. */
18544
18545 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18546 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18547
18548 if (font_not_found_p || !pcm)
18549 {
18550 int charset = CHAR_CHARSET (it->char_to_display);
18551
18552 it->glyph_not_available_p = 1;
18553 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18554 * CHARSET_WIDTH (charset));
18555 it->phys_ascent = FONT_BASE (font) + boff;
18556 it->phys_descent = FONT_DESCENT (font) - boff;
18557 }
18558 else
18559 {
18560 it->pixel_width = pcm->width;
18561 it->phys_ascent = pcm->ascent + boff;
18562 it->phys_descent = pcm->descent - boff;
18563 if (it->glyph_row
18564 && (pcm->lbearing < 0
18565 || pcm->rbearing > pcm->width))
18566 it->glyph_row->contains_overlapping_glyphs_p = 1;
18567 }
18568 it->nglyphs = 1;
18569 it->ascent = FONT_BASE (font) + boff;
18570 it->descent = FONT_DESCENT (font) - boff;
18571 if (face->box != FACE_NO_BOX)
18572 {
18573 int thick = face->box_line_width;
18574
18575 if (thick > 0)
18576 {
18577 it->ascent += thick;
18578 it->descent += thick;
18579 }
18580 else
18581 thick = - thick;
18582
18583 if (it->start_of_box_run_p)
18584 it->pixel_width += thick;
18585 if (it->end_of_box_run_p)
18586 it->pixel_width += thick;
18587 }
18588
18589 /* If face has an overline, add the height of the overline
18590 (1 pixel) and a 1 pixel margin to the character height. */
18591 if (face->overline_p)
18592 it->ascent += 2;
18593
18594 take_vertical_position_into_account (it);
18595
18596 if (it->glyph_row)
18597 append_glyph (it);
18598 }
18599 it->multibyte_p = saved_multibyte_p;
18600 }
18601 else if (it->what == IT_COMPOSITION)
18602 {
18603 /* Note: A composition is represented as one glyph in the
18604 glyph matrix. There are no padding glyphs. */
18605 XChar2b char2b;
18606 XFontStruct *font;
18607 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18608 XCharStruct *pcm;
18609 int font_not_found_p;
18610 struct font_info *font_info;
18611 int boff; /* baseline offset */
18612 struct composition *cmp = composition_table[it->cmp_id];
18613
18614 /* Maybe translate single-byte characters to multibyte. */
18615 it->char_to_display = it->c;
18616 if (unibyte_display_via_language_environment
18617 && SINGLE_BYTE_CHAR_P (it->c)
18618 && (it->c >= 0240
18619 || (it->c >= 0200
18620 && !NILP (Vnonascii_translation_table))))
18621 {
18622 it->char_to_display = unibyte_char_to_multibyte (it->c);
18623 }
18624
18625 /* Get face and font to use. Encode IT->char_to_display. */
18626 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18627 face = FACE_FROM_ID (it->f, it->face_id);
18628 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18629 &char2b, it->multibyte_p, 0);
18630 font = face->font;
18631
18632 /* When no suitable font found, use the default font. */
18633 font_not_found_p = font == NULL;
18634 if (font_not_found_p)
18635 {
18636 font = FRAME_FONT (it->f);
18637 boff = FRAME_BASELINE_OFFSET (it->f);
18638 font_info = NULL;
18639 }
18640 else
18641 {
18642 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18643 boff = font_info->baseline_offset;
18644 if (font_info->vertical_centering)
18645 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18646 }
18647
18648 /* There are no padding glyphs, so there is only one glyph to
18649 produce for the composition. Important is that pixel_width,
18650 ascent and descent are the values of what is drawn by
18651 draw_glyphs (i.e. the values of the overall glyphs composed). */
18652 it->nglyphs = 1;
18653
18654 /* If we have not yet calculated pixel size data of glyphs of
18655 the composition for the current face font, calculate them
18656 now. Theoretically, we have to check all fonts for the
18657 glyphs, but that requires much time and memory space. So,
18658 here we check only the font of the first glyph. This leads
18659 to incorrect display very rarely, and C-l (recenter) can
18660 correct the display anyway. */
18661 if (cmp->font != (void *) font)
18662 {
18663 /* Ascent and descent of the font of the first character of
18664 this composition (adjusted by baseline offset). Ascent
18665 and descent of overall glyphs should not be less than
18666 them respectively. */
18667 int font_ascent = FONT_BASE (font) + boff;
18668 int font_descent = FONT_DESCENT (font) - boff;
18669 /* Bounding box of the overall glyphs. */
18670 int leftmost, rightmost, lowest, highest;
18671 int i, width, ascent, descent;
18672
18673 cmp->font = (void *) font;
18674
18675 /* Initialize the bounding box. */
18676 if (font_info
18677 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18678 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18679 {
18680 width = pcm->width;
18681 ascent = pcm->ascent;
18682 descent = pcm->descent;
18683 }
18684 else
18685 {
18686 width = FONT_WIDTH (font);
18687 ascent = FONT_BASE (font);
18688 descent = FONT_DESCENT (font);
18689 }
18690
18691 rightmost = width;
18692 lowest = - descent + boff;
18693 highest = ascent + boff;
18694 leftmost = 0;
18695
18696 if (font_info
18697 && font_info->default_ascent
18698 && CHAR_TABLE_P (Vuse_default_ascent)
18699 && !NILP (Faref (Vuse_default_ascent,
18700 make_number (it->char_to_display))))
18701 highest = font_info->default_ascent + boff;
18702
18703 /* Draw the first glyph at the normal position. It may be
18704 shifted to right later if some other glyphs are drawn at
18705 the left. */
18706 cmp->offsets[0] = 0;
18707 cmp->offsets[1] = boff;
18708
18709 /* Set cmp->offsets for the remaining glyphs. */
18710 for (i = 1; i < cmp->glyph_len; i++)
18711 {
18712 int left, right, btm, top;
18713 int ch = COMPOSITION_GLYPH (cmp, i);
18714 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18715
18716 face = FACE_FROM_ID (it->f, face_id);
18717 get_char_face_and_encoding (it->f, ch, face->id,
18718 &char2b, it->multibyte_p, 0);
18719 font = face->font;
18720 if (font == NULL)
18721 {
18722 font = FRAME_FONT (it->f);
18723 boff = FRAME_BASELINE_OFFSET (it->f);
18724 font_info = NULL;
18725 }
18726 else
18727 {
18728 font_info
18729 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18730 boff = font_info->baseline_offset;
18731 if (font_info->vertical_centering)
18732 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18733 }
18734
18735 if (font_info
18736 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18737 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18738 {
18739 width = pcm->width;
18740 ascent = pcm->ascent;
18741 descent = pcm->descent;
18742 }
18743 else
18744 {
18745 width = FONT_WIDTH (font);
18746 ascent = 1;
18747 descent = 0;
18748 }
18749
18750 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18751 {
18752 /* Relative composition with or without
18753 alternate chars. */
18754 left = (leftmost + rightmost - width) / 2;
18755 btm = - descent + boff;
18756 if (font_info && font_info->relative_compose
18757 && (! CHAR_TABLE_P (Vignore_relative_composition)
18758 || NILP (Faref (Vignore_relative_composition,
18759 make_number (ch)))))
18760 {
18761
18762 if (- descent >= font_info->relative_compose)
18763 /* One extra pixel between two glyphs. */
18764 btm = highest + 1;
18765 else if (ascent <= 0)
18766 /* One extra pixel between two glyphs. */
18767 btm = lowest - 1 - ascent - descent;
18768 }
18769 }
18770 else
18771 {
18772 /* A composition rule is specified by an integer
18773 value that encodes global and new reference
18774 points (GREF and NREF). GREF and NREF are
18775 specified by numbers as below:
18776
18777 0---1---2 -- ascent
18778 | |
18779 | |
18780 | |
18781 9--10--11 -- center
18782 | |
18783 ---3---4---5--- baseline
18784 | |
18785 6---7---8 -- descent
18786 */
18787 int rule = COMPOSITION_RULE (cmp, i);
18788 int gref, nref, grefx, grefy, nrefx, nrefy;
18789
18790 COMPOSITION_DECODE_RULE (rule, gref, nref);
18791 grefx = gref % 3, nrefx = nref % 3;
18792 grefy = gref / 3, nrefy = nref / 3;
18793
18794 left = (leftmost
18795 + grefx * (rightmost - leftmost) / 2
18796 - nrefx * width / 2);
18797 btm = ((grefy == 0 ? highest
18798 : grefy == 1 ? 0
18799 : grefy == 2 ? lowest
18800 : (highest + lowest) / 2)
18801 - (nrefy == 0 ? ascent + descent
18802 : nrefy == 1 ? descent - boff
18803 : nrefy == 2 ? 0
18804 : (ascent + descent) / 2));
18805 }
18806
18807 cmp->offsets[i * 2] = left;
18808 cmp->offsets[i * 2 + 1] = btm + descent;
18809
18810 /* Update the bounding box of the overall glyphs. */
18811 right = left + width;
18812 top = btm + descent + ascent;
18813 if (left < leftmost)
18814 leftmost = left;
18815 if (right > rightmost)
18816 rightmost = right;
18817 if (top > highest)
18818 highest = top;
18819 if (btm < lowest)
18820 lowest = btm;
18821 }
18822
18823 /* If there are glyphs whose x-offsets are negative,
18824 shift all glyphs to the right and make all x-offsets
18825 non-negative. */
18826 if (leftmost < 0)
18827 {
18828 for (i = 0; i < cmp->glyph_len; i++)
18829 cmp->offsets[i * 2] -= leftmost;
18830 rightmost -= leftmost;
18831 }
18832
18833 cmp->pixel_width = rightmost;
18834 cmp->ascent = highest;
18835 cmp->descent = - lowest;
18836 if (cmp->ascent < font_ascent)
18837 cmp->ascent = font_ascent;
18838 if (cmp->descent < font_descent)
18839 cmp->descent = font_descent;
18840 }
18841
18842 it->pixel_width = cmp->pixel_width;
18843 it->ascent = it->phys_ascent = cmp->ascent;
18844 it->descent = it->phys_descent = cmp->descent;
18845
18846 if (face->box != FACE_NO_BOX)
18847 {
18848 int thick = face->box_line_width;
18849
18850 if (thick > 0)
18851 {
18852 it->ascent += thick;
18853 it->descent += thick;
18854 }
18855 else
18856 thick = - thick;
18857
18858 if (it->start_of_box_run_p)
18859 it->pixel_width += thick;
18860 if (it->end_of_box_run_p)
18861 it->pixel_width += thick;
18862 }
18863
18864 /* If face has an overline, add the height of the overline
18865 (1 pixel) and a 1 pixel margin to the character height. */
18866 if (face->overline_p)
18867 it->ascent += 2;
18868
18869 take_vertical_position_into_account (it);
18870
18871 if (it->glyph_row)
18872 append_composite_glyph (it);
18873 }
18874 else if (it->what == IT_IMAGE)
18875 produce_image_glyph (it);
18876 else if (it->what == IT_STRETCH)
18877 produce_stretch_glyph (it);
18878
18879 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18880 because this isn't true for images with `:ascent 100'. */
18881 xassert (it->ascent >= 0 && it->descent >= 0);
18882 if (it->area == TEXT_AREA)
18883 it->current_x += it->pixel_width;
18884
18885 it->descent += it->extra_line_spacing;
18886
18887 it->max_ascent = max (it->max_ascent, it->ascent);
18888 it->max_descent = max (it->max_descent, it->descent);
18889 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18890 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18891 }
18892
18893 /* EXPORT for RIF:
18894 Output LEN glyphs starting at START at the nominal cursor position.
18895 Advance the nominal cursor over the text. The global variable
18896 updated_window contains the window being updated, updated_row is
18897 the glyph row being updated, and updated_area is the area of that
18898 row being updated. */
18899
18900 void
18901 x_write_glyphs (start, len)
18902 struct glyph *start;
18903 int len;
18904 {
18905 int x, hpos;
18906
18907 xassert (updated_window && updated_row);
18908 BLOCK_INPUT;
18909
18910 /* Write glyphs. */
18911
18912 hpos = start - updated_row->glyphs[updated_area];
18913 x = draw_glyphs (updated_window, output_cursor.x,
18914 updated_row, updated_area,
18915 hpos, hpos + len,
18916 DRAW_NORMAL_TEXT, 0);
18917
18918 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18919 if (updated_area == TEXT_AREA
18920 && updated_window->phys_cursor_on_p
18921 && updated_window->phys_cursor.vpos == output_cursor.vpos
18922 && updated_window->phys_cursor.hpos >= hpos
18923 && updated_window->phys_cursor.hpos < hpos + len)
18924 updated_window->phys_cursor_on_p = 0;
18925
18926 UNBLOCK_INPUT;
18927
18928 /* Advance the output cursor. */
18929 output_cursor.hpos += len;
18930 output_cursor.x = x;
18931 }
18932
18933
18934 /* EXPORT for RIF:
18935 Insert LEN glyphs from START at the nominal cursor position. */
18936
18937 void
18938 x_insert_glyphs (start, len)
18939 struct glyph *start;
18940 int len;
18941 {
18942 struct frame *f;
18943 struct window *w;
18944 int line_height, shift_by_width, shifted_region_width;
18945 struct glyph_row *row;
18946 struct glyph *glyph;
18947 int frame_x, frame_y, hpos;
18948
18949 xassert (updated_window && updated_row);
18950 BLOCK_INPUT;
18951 w = updated_window;
18952 f = XFRAME (WINDOW_FRAME (w));
18953
18954 /* Get the height of the line we are in. */
18955 row = updated_row;
18956 line_height = row->height;
18957
18958 /* Get the width of the glyphs to insert. */
18959 shift_by_width = 0;
18960 for (glyph = start; glyph < start + len; ++glyph)
18961 shift_by_width += glyph->pixel_width;
18962
18963 /* Get the width of the region to shift right. */
18964 shifted_region_width = (window_box_width (w, updated_area)
18965 - output_cursor.x
18966 - shift_by_width);
18967
18968 /* Shift right. */
18969 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18970 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18971
18972 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18973 line_height, shift_by_width);
18974
18975 /* Write the glyphs. */
18976 hpos = start - row->glyphs[updated_area];
18977 draw_glyphs (w, output_cursor.x, row, updated_area,
18978 hpos, hpos + len,
18979 DRAW_NORMAL_TEXT, 0);
18980
18981 /* Advance the output cursor. */
18982 output_cursor.hpos += len;
18983 output_cursor.x += shift_by_width;
18984 UNBLOCK_INPUT;
18985 }
18986
18987
18988 /* EXPORT for RIF:
18989 Erase the current text line from the nominal cursor position
18990 (inclusive) to pixel column TO_X (exclusive). The idea is that
18991 everything from TO_X onward is already erased.
18992
18993 TO_X is a pixel position relative to updated_area of
18994 updated_window. TO_X == -1 means clear to the end of this area. */
18995
18996 void
18997 x_clear_end_of_line (to_x)
18998 int to_x;
18999 {
19000 struct frame *f;
19001 struct window *w = updated_window;
19002 int max_x, min_y, max_y;
19003 int from_x, from_y, to_y;
19004
19005 xassert (updated_window && updated_row);
19006 f = XFRAME (w->frame);
19007
19008 if (updated_row->full_width_p)
19009 max_x = WINDOW_TOTAL_WIDTH (w);
19010 else
19011 max_x = window_box_width (w, updated_area);
19012 max_y = window_text_bottom_y (w);
19013
19014 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19015 of window. For TO_X > 0, truncate to end of drawing area. */
19016 if (to_x == 0)
19017 return;
19018 else if (to_x < 0)
19019 to_x = max_x;
19020 else
19021 to_x = min (to_x, max_x);
19022
19023 to_y = min (max_y, output_cursor.y + updated_row->height);
19024
19025 /* Notice if the cursor will be cleared by this operation. */
19026 if (!updated_row->full_width_p)
19027 notice_overwritten_cursor (w, updated_area,
19028 output_cursor.x, -1,
19029 updated_row->y,
19030 MATRIX_ROW_BOTTOM_Y (updated_row));
19031
19032 from_x = output_cursor.x;
19033
19034 /* Translate to frame coordinates. */
19035 if (updated_row->full_width_p)
19036 {
19037 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19038 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19039 }
19040 else
19041 {
19042 int area_left = window_box_left (w, updated_area);
19043 from_x += area_left;
19044 to_x += area_left;
19045 }
19046
19047 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19048 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19049 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19050
19051 /* Prevent inadvertently clearing to end of the X window. */
19052 if (to_x > from_x && to_y > from_y)
19053 {
19054 BLOCK_INPUT;
19055 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19056 to_x - from_x, to_y - from_y);
19057 UNBLOCK_INPUT;
19058 }
19059 }
19060
19061 #endif /* HAVE_WINDOW_SYSTEM */
19062
19063
19064 \f
19065 /***********************************************************************
19066 Cursor types
19067 ***********************************************************************/
19068
19069 /* Value is the internal representation of the specified cursor type
19070 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19071 of the bar cursor. */
19072
19073 static enum text_cursor_kinds
19074 get_specified_cursor_type (arg, width)
19075 Lisp_Object arg;
19076 int *width;
19077 {
19078 enum text_cursor_kinds type;
19079
19080 if (NILP (arg))
19081 return NO_CURSOR;
19082
19083 if (EQ (arg, Qbox))
19084 return FILLED_BOX_CURSOR;
19085
19086 if (EQ (arg, Qhollow))
19087 return HOLLOW_BOX_CURSOR;
19088
19089 if (EQ (arg, Qbar))
19090 {
19091 *width = 2;
19092 return BAR_CURSOR;
19093 }
19094
19095 if (CONSP (arg)
19096 && EQ (XCAR (arg), Qbar)
19097 && INTEGERP (XCDR (arg))
19098 && XINT (XCDR (arg)) >= 0)
19099 {
19100 *width = XINT (XCDR (arg));
19101 return BAR_CURSOR;
19102 }
19103
19104 if (EQ (arg, Qhbar))
19105 {
19106 *width = 2;
19107 return HBAR_CURSOR;
19108 }
19109
19110 if (CONSP (arg)
19111 && EQ (XCAR (arg), Qhbar)
19112 && INTEGERP (XCDR (arg))
19113 && XINT (XCDR (arg)) >= 0)
19114 {
19115 *width = XINT (XCDR (arg));
19116 return HBAR_CURSOR;
19117 }
19118
19119 /* Treat anything unknown as "hollow box cursor".
19120 It was bad to signal an error; people have trouble fixing
19121 .Xdefaults with Emacs, when it has something bad in it. */
19122 type = HOLLOW_BOX_CURSOR;
19123
19124 return type;
19125 }
19126
19127 /* Set the default cursor types for specified frame. */
19128 void
19129 set_frame_cursor_types (f, arg)
19130 struct frame *f;
19131 Lisp_Object arg;
19132 {
19133 int width;
19134 Lisp_Object tem;
19135
19136 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19137 FRAME_CURSOR_WIDTH (f) = width;
19138
19139 /* By default, set up the blink-off state depending on the on-state. */
19140
19141 tem = Fassoc (arg, Vblink_cursor_alist);
19142 if (!NILP (tem))
19143 {
19144 FRAME_BLINK_OFF_CURSOR (f)
19145 = get_specified_cursor_type (XCDR (tem), &width);
19146 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19147 }
19148 else
19149 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19150 }
19151
19152
19153 /* Return the cursor we want to be displayed in window W. Return
19154 width of bar/hbar cursor through WIDTH arg. Return with
19155 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19156 (i.e. if the `system caret' should track this cursor).
19157
19158 In a mini-buffer window, we want the cursor only to appear if we
19159 are reading input from this window. For the selected window, we
19160 want the cursor type given by the frame parameter or buffer local
19161 setting of cursor-type. If explicitly marked off, draw no cursor.
19162 In all other cases, we want a hollow box cursor. */
19163
19164 static enum text_cursor_kinds
19165 get_window_cursor_type (w, glyph, width, active_cursor)
19166 struct window *w;
19167 struct glyph *glyph;
19168 int *width;
19169 int *active_cursor;
19170 {
19171 struct frame *f = XFRAME (w->frame);
19172 struct buffer *b = XBUFFER (w->buffer);
19173 int cursor_type = DEFAULT_CURSOR;
19174 Lisp_Object alt_cursor;
19175 int non_selected = 0;
19176
19177 *active_cursor = 1;
19178
19179 /* Echo area */
19180 if (cursor_in_echo_area
19181 && FRAME_HAS_MINIBUF_P (f)
19182 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19183 {
19184 if (w == XWINDOW (echo_area_window))
19185 {
19186 *width = FRAME_CURSOR_WIDTH (f);
19187 return FRAME_DESIRED_CURSOR (f);
19188 }
19189
19190 *active_cursor = 0;
19191 non_selected = 1;
19192 }
19193
19194 /* Nonselected window or nonselected frame. */
19195 else if (w != XWINDOW (f->selected_window)
19196 #ifdef HAVE_WINDOW_SYSTEM
19197 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19198 #endif
19199 )
19200 {
19201 *active_cursor = 0;
19202
19203 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19204 return NO_CURSOR;
19205
19206 non_selected = 1;
19207 }
19208
19209 /* Never display a cursor in a window in which cursor-type is nil. */
19210 if (NILP (b->cursor_type))
19211 return NO_CURSOR;
19212
19213 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19214 if (non_selected)
19215 {
19216 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19217 return get_specified_cursor_type (alt_cursor, width);
19218 }
19219
19220 /* Get the normal cursor type for this window. */
19221 if (EQ (b->cursor_type, Qt))
19222 {
19223 cursor_type = FRAME_DESIRED_CURSOR (f);
19224 *width = FRAME_CURSOR_WIDTH (f);
19225 }
19226 else
19227 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19228
19229 /* Use normal cursor if not blinked off. */
19230 if (!w->cursor_off_p)
19231 {
19232 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19233 if (cursor_type == FILLED_BOX_CURSOR)
19234 cursor_type = HOLLOW_BOX_CURSOR;
19235 }
19236 return cursor_type;
19237 }
19238
19239 /* Cursor is blinked off, so determine how to "toggle" it. */
19240
19241 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19242 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19243 return get_specified_cursor_type (XCDR (alt_cursor), width);
19244
19245 /* Then see if frame has specified a specific blink off cursor type. */
19246 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19247 {
19248 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19249 return FRAME_BLINK_OFF_CURSOR (f);
19250 }
19251
19252 #if 0
19253 /* Some people liked having a permanently visible blinking cursor,
19254 while others had very strong opinions against it. So it was
19255 decided to remove it. KFS 2003-09-03 */
19256
19257 /* Finally perform built-in cursor blinking:
19258 filled box <-> hollow box
19259 wide [h]bar <-> narrow [h]bar
19260 narrow [h]bar <-> no cursor
19261 other type <-> no cursor */
19262
19263 if (cursor_type == FILLED_BOX_CURSOR)
19264 return HOLLOW_BOX_CURSOR;
19265
19266 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19267 {
19268 *width = 1;
19269 return cursor_type;
19270 }
19271 #endif
19272
19273 return NO_CURSOR;
19274 }
19275
19276
19277 #ifdef HAVE_WINDOW_SYSTEM
19278
19279 /* Notice when the text cursor of window W has been completely
19280 overwritten by a drawing operation that outputs glyphs in AREA
19281 starting at X0 and ending at X1 in the line starting at Y0 and
19282 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19283 the rest of the line after X0 has been written. Y coordinates
19284 are window-relative. */
19285
19286 static void
19287 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19288 struct window *w;
19289 enum glyph_row_area area;
19290 int x0, y0, x1, y1;
19291 {
19292 int cx0, cx1, cy0, cy1;
19293 struct glyph_row *row;
19294
19295 if (!w->phys_cursor_on_p)
19296 return;
19297 if (area != TEXT_AREA)
19298 return;
19299
19300 row = w->current_matrix->rows + w->phys_cursor.vpos;
19301 if (!row->displays_text_p)
19302 return;
19303
19304 if (row->cursor_in_fringe_p)
19305 {
19306 row->cursor_in_fringe_p = 0;
19307 draw_fringe_bitmap (w, row, 0);
19308 w->phys_cursor_on_p = 0;
19309 return;
19310 }
19311
19312 cx0 = w->phys_cursor.x;
19313 cx1 = cx0 + w->phys_cursor_width;
19314 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19315 return;
19316
19317 /* The cursor image will be completely removed from the
19318 screen if the output area intersects the cursor area in
19319 y-direction. When we draw in [y0 y1[, and some part of
19320 the cursor is at y < y0, that part must have been drawn
19321 before. When scrolling, the cursor is erased before
19322 actually scrolling, so we don't come here. When not
19323 scrolling, the rows above the old cursor row must have
19324 changed, and in this case these rows must have written
19325 over the cursor image.
19326
19327 Likewise if part of the cursor is below y1, with the
19328 exception of the cursor being in the first blank row at
19329 the buffer and window end because update_text_area
19330 doesn't draw that row. (Except when it does, but
19331 that's handled in update_text_area.) */
19332
19333 cy0 = w->phys_cursor.y;
19334 cy1 = cy0 + w->phys_cursor_height;
19335 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19336 return;
19337
19338 w->phys_cursor_on_p = 0;
19339 }
19340
19341 #endif /* HAVE_WINDOW_SYSTEM */
19342
19343 \f
19344 /************************************************************************
19345 Mouse Face
19346 ************************************************************************/
19347
19348 #ifdef HAVE_WINDOW_SYSTEM
19349
19350 /* EXPORT for RIF:
19351 Fix the display of area AREA of overlapping row ROW in window W. */
19352
19353 void
19354 x_fix_overlapping_area (w, row, area)
19355 struct window *w;
19356 struct glyph_row *row;
19357 enum glyph_row_area area;
19358 {
19359 int i, x;
19360
19361 BLOCK_INPUT;
19362
19363 x = 0;
19364 for (i = 0; i < row->used[area];)
19365 {
19366 if (row->glyphs[area][i].overlaps_vertically_p)
19367 {
19368 int start = i, start_x = x;
19369
19370 do
19371 {
19372 x += row->glyphs[area][i].pixel_width;
19373 ++i;
19374 }
19375 while (i < row->used[area]
19376 && row->glyphs[area][i].overlaps_vertically_p);
19377
19378 draw_glyphs (w, start_x, row, area,
19379 start, i,
19380 DRAW_NORMAL_TEXT, 1);
19381 }
19382 else
19383 {
19384 x += row->glyphs[area][i].pixel_width;
19385 ++i;
19386 }
19387 }
19388
19389 UNBLOCK_INPUT;
19390 }
19391
19392
19393 /* EXPORT:
19394 Draw the cursor glyph of window W in glyph row ROW. See the
19395 comment of draw_glyphs for the meaning of HL. */
19396
19397 void
19398 draw_phys_cursor_glyph (w, row, hl)
19399 struct window *w;
19400 struct glyph_row *row;
19401 enum draw_glyphs_face hl;
19402 {
19403 /* If cursor hpos is out of bounds, don't draw garbage. This can
19404 happen in mini-buffer windows when switching between echo area
19405 glyphs and mini-buffer. */
19406 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19407 {
19408 int on_p = w->phys_cursor_on_p;
19409 int x1;
19410 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19411 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19412 hl, 0);
19413 w->phys_cursor_on_p = on_p;
19414
19415 if (hl == DRAW_CURSOR)
19416 w->phys_cursor_width = x1 - w->phys_cursor.x;
19417 /* When we erase the cursor, and ROW is overlapped by other
19418 rows, make sure that these overlapping parts of other rows
19419 are redrawn. */
19420 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19421 {
19422 if (row > w->current_matrix->rows
19423 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19424 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19425
19426 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19427 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19428 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19429 }
19430 }
19431 }
19432
19433
19434 /* EXPORT:
19435 Erase the image of a cursor of window W from the screen. */
19436
19437 void
19438 erase_phys_cursor (w)
19439 struct window *w;
19440 {
19441 struct frame *f = XFRAME (w->frame);
19442 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19443 int hpos = w->phys_cursor.hpos;
19444 int vpos = w->phys_cursor.vpos;
19445 int mouse_face_here_p = 0;
19446 struct glyph_matrix *active_glyphs = w->current_matrix;
19447 struct glyph_row *cursor_row;
19448 struct glyph *cursor_glyph;
19449 enum draw_glyphs_face hl;
19450
19451 /* No cursor displayed or row invalidated => nothing to do on the
19452 screen. */
19453 if (w->phys_cursor_type == NO_CURSOR)
19454 goto mark_cursor_off;
19455
19456 /* VPOS >= active_glyphs->nrows means that window has been resized.
19457 Don't bother to erase the cursor. */
19458 if (vpos >= active_glyphs->nrows)
19459 goto mark_cursor_off;
19460
19461 /* If row containing cursor is marked invalid, there is nothing we
19462 can do. */
19463 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19464 if (!cursor_row->enabled_p)
19465 goto mark_cursor_off;
19466
19467 /* If row is completely invisible, don't attempt to delete a cursor which
19468 isn't there. This can happen if cursor is at top of a window, and
19469 we switch to a buffer with a header line in that window. */
19470 if (cursor_row->visible_height <= 0)
19471 goto mark_cursor_off;
19472
19473 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19474 if (cursor_row->cursor_in_fringe_p)
19475 {
19476 cursor_row->cursor_in_fringe_p = 0;
19477 draw_fringe_bitmap (w, cursor_row, 0);
19478 goto mark_cursor_off;
19479 }
19480
19481 /* This can happen when the new row is shorter than the old one.
19482 In this case, either draw_glyphs or clear_end_of_line
19483 should have cleared the cursor. Note that we wouldn't be
19484 able to erase the cursor in this case because we don't have a
19485 cursor glyph at hand. */
19486 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19487 goto mark_cursor_off;
19488
19489 /* If the cursor is in the mouse face area, redisplay that when
19490 we clear the cursor. */
19491 if (! NILP (dpyinfo->mouse_face_window)
19492 && w == XWINDOW (dpyinfo->mouse_face_window)
19493 && (vpos > dpyinfo->mouse_face_beg_row
19494 || (vpos == dpyinfo->mouse_face_beg_row
19495 && hpos >= dpyinfo->mouse_face_beg_col))
19496 && (vpos < dpyinfo->mouse_face_end_row
19497 || (vpos == dpyinfo->mouse_face_end_row
19498 && hpos < dpyinfo->mouse_face_end_col))
19499 /* Don't redraw the cursor's spot in mouse face if it is at the
19500 end of a line (on a newline). The cursor appears there, but
19501 mouse highlighting does not. */
19502 && cursor_row->used[TEXT_AREA] > hpos)
19503 mouse_face_here_p = 1;
19504
19505 /* Maybe clear the display under the cursor. */
19506 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19507 {
19508 int x, y;
19509 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19510
19511 cursor_glyph = get_phys_cursor_glyph (w);
19512 if (cursor_glyph == NULL)
19513 goto mark_cursor_off;
19514
19515 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19516 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19517
19518 FRAME_RIF (f)->clear_frame_area (f, x, y,
19519 cursor_glyph->pixel_width, cursor_row->visible_height);
19520 }
19521
19522 /* Erase the cursor by redrawing the character underneath it. */
19523 if (mouse_face_here_p)
19524 hl = DRAW_MOUSE_FACE;
19525 else
19526 hl = DRAW_NORMAL_TEXT;
19527 draw_phys_cursor_glyph (w, cursor_row, hl);
19528
19529 mark_cursor_off:
19530 w->phys_cursor_on_p = 0;
19531 w->phys_cursor_type = NO_CURSOR;
19532 }
19533
19534
19535 /* EXPORT:
19536 Display or clear cursor of window W. If ON is zero, clear the
19537 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19538 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19539
19540 void
19541 display_and_set_cursor (w, on, hpos, vpos, x, y)
19542 struct window *w;
19543 int on, hpos, vpos, x, y;
19544 {
19545 struct frame *f = XFRAME (w->frame);
19546 int new_cursor_type;
19547 int new_cursor_width;
19548 int active_cursor;
19549 struct glyph_row *glyph_row;
19550 struct glyph *glyph;
19551
19552 /* This is pointless on invisible frames, and dangerous on garbaged
19553 windows and frames; in the latter case, the frame or window may
19554 be in the midst of changing its size, and x and y may be off the
19555 window. */
19556 if (! FRAME_VISIBLE_P (f)
19557 || FRAME_GARBAGED_P (f)
19558 || vpos >= w->current_matrix->nrows
19559 || hpos >= w->current_matrix->matrix_w)
19560 return;
19561
19562 /* If cursor is off and we want it off, return quickly. */
19563 if (!on && !w->phys_cursor_on_p)
19564 return;
19565
19566 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
19567 /* If cursor row is not enabled, we don't really know where to
19568 display the cursor. */
19569 if (!glyph_row->enabled_p)
19570 {
19571 w->phys_cursor_on_p = 0;
19572 return;
19573 }
19574
19575 glyph = NULL;
19576 if (!glyph_row->exact_window_width_line_p
19577 || hpos < glyph_row->used[TEXT_AREA])
19578 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19579
19580 xassert (interrupt_input_blocked);
19581
19582 /* Set new_cursor_type to the cursor we want to be displayed. */
19583 new_cursor_type = get_window_cursor_type (w, glyph,
19584 &new_cursor_width, &active_cursor);
19585
19586 /* If cursor is currently being shown and we don't want it to be or
19587 it is in the wrong place, or the cursor type is not what we want,
19588 erase it. */
19589 if (w->phys_cursor_on_p
19590 && (!on
19591 || w->phys_cursor.x != x
19592 || w->phys_cursor.y != y
19593 || new_cursor_type != w->phys_cursor_type
19594 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19595 && new_cursor_width != w->phys_cursor_width)))
19596 erase_phys_cursor (w);
19597
19598 /* Don't check phys_cursor_on_p here because that flag is only set
19599 to zero in some cases where we know that the cursor has been
19600 completely erased, to avoid the extra work of erasing the cursor
19601 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19602 still not be visible, or it has only been partly erased. */
19603 if (on)
19604 {
19605 w->phys_cursor_ascent = glyph_row->ascent;
19606 w->phys_cursor_height = glyph_row->height;
19607
19608 /* Set phys_cursor_.* before x_draw_.* is called because some
19609 of them may need the information. */
19610 w->phys_cursor.x = x;
19611 w->phys_cursor.y = glyph_row->y;
19612 w->phys_cursor.hpos = hpos;
19613 w->phys_cursor.vpos = vpos;
19614 }
19615
19616 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
19617 new_cursor_type, new_cursor_width,
19618 on, active_cursor);
19619 }
19620
19621
19622 /* Switch the display of W's cursor on or off, according to the value
19623 of ON. */
19624
19625 static void
19626 update_window_cursor (w, on)
19627 struct window *w;
19628 int on;
19629 {
19630 /* Don't update cursor in windows whose frame is in the process
19631 of being deleted. */
19632 if (w->current_matrix)
19633 {
19634 BLOCK_INPUT;
19635 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19636 w->phys_cursor.x, w->phys_cursor.y);
19637 UNBLOCK_INPUT;
19638 }
19639 }
19640
19641
19642 /* Call update_window_cursor with parameter ON_P on all leaf windows
19643 in the window tree rooted at W. */
19644
19645 static void
19646 update_cursor_in_window_tree (w, on_p)
19647 struct window *w;
19648 int on_p;
19649 {
19650 while (w)
19651 {
19652 if (!NILP (w->hchild))
19653 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19654 else if (!NILP (w->vchild))
19655 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19656 else
19657 update_window_cursor (w, on_p);
19658
19659 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19660 }
19661 }
19662
19663
19664 /* EXPORT:
19665 Display the cursor on window W, or clear it, according to ON_P.
19666 Don't change the cursor's position. */
19667
19668 void
19669 x_update_cursor (f, on_p)
19670 struct frame *f;
19671 int on_p;
19672 {
19673 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19674 }
19675
19676
19677 /* EXPORT:
19678 Clear the cursor of window W to background color, and mark the
19679 cursor as not shown. This is used when the text where the cursor
19680 is is about to be rewritten. */
19681
19682 void
19683 x_clear_cursor (w)
19684 struct window *w;
19685 {
19686 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19687 update_window_cursor (w, 0);
19688 }
19689
19690
19691 /* EXPORT:
19692 Display the active region described by mouse_face_* according to DRAW. */
19693
19694 void
19695 show_mouse_face (dpyinfo, draw)
19696 Display_Info *dpyinfo;
19697 enum draw_glyphs_face draw;
19698 {
19699 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19700 struct frame *f = XFRAME (WINDOW_FRAME (w));
19701
19702 if (/* If window is in the process of being destroyed, don't bother
19703 to do anything. */
19704 w->current_matrix != NULL
19705 /* Don't update mouse highlight if hidden */
19706 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19707 /* Recognize when we are called to operate on rows that don't exist
19708 anymore. This can happen when a window is split. */
19709 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19710 {
19711 int phys_cursor_on_p = w->phys_cursor_on_p;
19712 struct glyph_row *row, *first, *last;
19713
19714 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19715 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19716
19717 for (row = first; row <= last && row->enabled_p; ++row)
19718 {
19719 int start_hpos, end_hpos, start_x;
19720
19721 /* For all but the first row, the highlight starts at column 0. */
19722 if (row == first)
19723 {
19724 start_hpos = dpyinfo->mouse_face_beg_col;
19725 start_x = dpyinfo->mouse_face_beg_x;
19726 }
19727 else
19728 {
19729 start_hpos = 0;
19730 start_x = 0;
19731 }
19732
19733 if (row == last)
19734 end_hpos = dpyinfo->mouse_face_end_col;
19735 else
19736 end_hpos = row->used[TEXT_AREA];
19737
19738 if (end_hpos > start_hpos)
19739 {
19740 draw_glyphs (w, start_x, row, TEXT_AREA,
19741 start_hpos, end_hpos,
19742 draw, 0);
19743
19744 row->mouse_face_p
19745 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19746 }
19747 }
19748
19749 /* When we've written over the cursor, arrange for it to
19750 be displayed again. */
19751 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19752 {
19753 BLOCK_INPUT;
19754 display_and_set_cursor (w, 1,
19755 w->phys_cursor.hpos, w->phys_cursor.vpos,
19756 w->phys_cursor.x, w->phys_cursor.y);
19757 UNBLOCK_INPUT;
19758 }
19759 }
19760
19761 /* Change the mouse cursor. */
19762 if (draw == DRAW_NORMAL_TEXT)
19763 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19764 else if (draw == DRAW_MOUSE_FACE)
19765 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19766 else
19767 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19768 }
19769
19770 /* EXPORT:
19771 Clear out the mouse-highlighted active region.
19772 Redraw it un-highlighted first. Value is non-zero if mouse
19773 face was actually drawn unhighlighted. */
19774
19775 int
19776 clear_mouse_face (dpyinfo)
19777 Display_Info *dpyinfo;
19778 {
19779 int cleared = 0;
19780
19781 if (!NILP (dpyinfo->mouse_face_window))
19782 {
19783 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19784 cleared = 1;
19785 }
19786
19787 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19788 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19789 dpyinfo->mouse_face_window = Qnil;
19790 dpyinfo->mouse_face_overlay = Qnil;
19791 return cleared;
19792 }
19793
19794
19795 /* EXPORT:
19796 Non-zero if physical cursor of window W is within mouse face. */
19797
19798 int
19799 cursor_in_mouse_face_p (w)
19800 struct window *w;
19801 {
19802 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19803 int in_mouse_face = 0;
19804
19805 if (WINDOWP (dpyinfo->mouse_face_window)
19806 && XWINDOW (dpyinfo->mouse_face_window) == w)
19807 {
19808 int hpos = w->phys_cursor.hpos;
19809 int vpos = w->phys_cursor.vpos;
19810
19811 if (vpos >= dpyinfo->mouse_face_beg_row
19812 && vpos <= dpyinfo->mouse_face_end_row
19813 && (vpos > dpyinfo->mouse_face_beg_row
19814 || hpos >= dpyinfo->mouse_face_beg_col)
19815 && (vpos < dpyinfo->mouse_face_end_row
19816 || hpos < dpyinfo->mouse_face_end_col
19817 || dpyinfo->mouse_face_past_end))
19818 in_mouse_face = 1;
19819 }
19820
19821 return in_mouse_face;
19822 }
19823
19824
19825
19826 \f
19827 /* Find the glyph matrix position of buffer position CHARPOS in window
19828 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19829 current glyphs must be up to date. If CHARPOS is above window
19830 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19831 of last line in W. In the row containing CHARPOS, stop before glyphs
19832 having STOP as object. */
19833
19834 #if 1 /* This is a version of fast_find_position that's more correct
19835 in the presence of hscrolling, for example. I didn't install
19836 it right away because the problem fixed is minor, it failed
19837 in 20.x as well, and I think it's too risky to install
19838 so near the release of 21.1. 2001-09-25 gerd. */
19839
19840 static int
19841 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19842 struct window *w;
19843 int charpos;
19844 int *hpos, *vpos, *x, *y;
19845 Lisp_Object stop;
19846 {
19847 struct glyph_row *row, *first;
19848 struct glyph *glyph, *end;
19849 int past_end = 0;
19850
19851 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19852 row = row_containing_pos (w, charpos, first, NULL, 0);
19853 if (row == NULL)
19854 {
19855 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19856 {
19857 *x = *y = *hpos = *vpos = 0;
19858 return 1;
19859 }
19860 else
19861 {
19862 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19863 past_end = 1;
19864 }
19865 }
19866
19867 *x = row->x;
19868 *y = row->y;
19869 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19870
19871 glyph = row->glyphs[TEXT_AREA];
19872 end = glyph + row->used[TEXT_AREA];
19873
19874 /* Skip over glyphs not having an object at the start of the row.
19875 These are special glyphs like truncation marks on terminal
19876 frames. */
19877 if (row->displays_text_p)
19878 while (glyph < end
19879 && INTEGERP (glyph->object)
19880 && !EQ (stop, glyph->object)
19881 && glyph->charpos < 0)
19882 {
19883 *x += glyph->pixel_width;
19884 ++glyph;
19885 }
19886
19887 while (glyph < end
19888 && !INTEGERP (glyph->object)
19889 && !EQ (stop, glyph->object)
19890 && (!BUFFERP (glyph->object)
19891 || glyph->charpos < charpos))
19892 {
19893 *x += glyph->pixel_width;
19894 ++glyph;
19895 }
19896
19897 *hpos = glyph - row->glyphs[TEXT_AREA];
19898 return !past_end;
19899 }
19900
19901 #else /* not 1 */
19902
19903 static int
19904 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19905 struct window *w;
19906 int pos;
19907 int *hpos, *vpos, *x, *y;
19908 Lisp_Object stop;
19909 {
19910 int i;
19911 int lastcol;
19912 int maybe_next_line_p = 0;
19913 int line_start_position;
19914 int yb = window_text_bottom_y (w);
19915 struct glyph_row *row, *best_row;
19916 int row_vpos, best_row_vpos;
19917 int current_x;
19918
19919 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19920 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19921
19922 while (row->y < yb)
19923 {
19924 if (row->used[TEXT_AREA])
19925 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19926 else
19927 line_start_position = 0;
19928
19929 if (line_start_position > pos)
19930 break;
19931 /* If the position sought is the end of the buffer,
19932 don't include the blank lines at the bottom of the window. */
19933 else if (line_start_position == pos
19934 && pos == BUF_ZV (XBUFFER (w->buffer)))
19935 {
19936 maybe_next_line_p = 1;
19937 break;
19938 }
19939 else if (line_start_position > 0)
19940 {
19941 best_row = row;
19942 best_row_vpos = row_vpos;
19943 }
19944
19945 if (row->y + row->height >= yb)
19946 break;
19947
19948 ++row;
19949 ++row_vpos;
19950 }
19951
19952 /* Find the right column within BEST_ROW. */
19953 lastcol = 0;
19954 current_x = best_row->x;
19955 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19956 {
19957 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19958 int charpos = glyph->charpos;
19959
19960 if (BUFFERP (glyph->object))
19961 {
19962 if (charpos == pos)
19963 {
19964 *hpos = i;
19965 *vpos = best_row_vpos;
19966 *x = current_x;
19967 *y = best_row->y;
19968 return 1;
19969 }
19970 else if (charpos > pos)
19971 break;
19972 }
19973 else if (EQ (glyph->object, stop))
19974 break;
19975
19976 if (charpos > 0)
19977 lastcol = i;
19978 current_x += glyph->pixel_width;
19979 }
19980
19981 /* If we're looking for the end of the buffer,
19982 and we didn't find it in the line we scanned,
19983 use the start of the following line. */
19984 if (maybe_next_line_p)
19985 {
19986 ++best_row;
19987 ++best_row_vpos;
19988 lastcol = 0;
19989 current_x = best_row->x;
19990 }
19991
19992 *vpos = best_row_vpos;
19993 *hpos = lastcol + 1;
19994 *x = current_x;
19995 *y = best_row->y;
19996 return 0;
19997 }
19998
19999 #endif /* not 1 */
20000
20001
20002 /* Find the position of the glyph for position POS in OBJECT in
20003 window W's current matrix, and return in *X, *Y the pixel
20004 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20005
20006 RIGHT_P non-zero means return the position of the right edge of the
20007 glyph, RIGHT_P zero means return the left edge position.
20008
20009 If no glyph for POS exists in the matrix, return the position of
20010 the glyph with the next smaller position that is in the matrix, if
20011 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20012 exists in the matrix, return the position of the glyph with the
20013 next larger position in OBJECT.
20014
20015 Value is non-zero if a glyph was found. */
20016
20017 static int
20018 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20019 struct window *w;
20020 int pos;
20021 Lisp_Object object;
20022 int *hpos, *vpos, *x, *y;
20023 int right_p;
20024 {
20025 int yb = window_text_bottom_y (w);
20026 struct glyph_row *r;
20027 struct glyph *best_glyph = NULL;
20028 struct glyph_row *best_row = NULL;
20029 int best_x = 0;
20030
20031 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20032 r->enabled_p && r->y < yb;
20033 ++r)
20034 {
20035 struct glyph *g = r->glyphs[TEXT_AREA];
20036 struct glyph *e = g + r->used[TEXT_AREA];
20037 int gx;
20038
20039 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20040 if (EQ (g->object, object))
20041 {
20042 if (g->charpos == pos)
20043 {
20044 best_glyph = g;
20045 best_x = gx;
20046 best_row = r;
20047 goto found;
20048 }
20049 else if (best_glyph == NULL
20050 || ((abs (g->charpos - pos)
20051 < abs (best_glyph->charpos - pos))
20052 && (right_p
20053 ? g->charpos < pos
20054 : g->charpos > pos)))
20055 {
20056 best_glyph = g;
20057 best_x = gx;
20058 best_row = r;
20059 }
20060 }
20061 }
20062
20063 found:
20064
20065 if (best_glyph)
20066 {
20067 *x = best_x;
20068 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20069
20070 if (right_p)
20071 {
20072 *x += best_glyph->pixel_width;
20073 ++*hpos;
20074 }
20075
20076 *y = best_row->y;
20077 *vpos = best_row - w->current_matrix->rows;
20078 }
20079
20080 return best_glyph != NULL;
20081 }
20082
20083
20084 /* See if position X, Y is within a hot-spot of an image. */
20085
20086 static int
20087 on_hot_spot_p (hot_spot, x, y)
20088 Lisp_Object hot_spot;
20089 int x, y;
20090 {
20091 if (!CONSP (hot_spot))
20092 return 0;
20093
20094 if (EQ (XCAR (hot_spot), Qrect))
20095 {
20096 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20097 Lisp_Object rect = XCDR (hot_spot);
20098 Lisp_Object tem;
20099 if (!CONSP (rect))
20100 return 0;
20101 if (!CONSP (XCAR (rect)))
20102 return 0;
20103 if (!CONSP (XCDR (rect)))
20104 return 0;
20105 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20106 return 0;
20107 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20108 return 0;
20109 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20110 return 0;
20111 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20112 return 0;
20113 return 1;
20114 }
20115 else if (EQ (XCAR (hot_spot), Qcircle))
20116 {
20117 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20118 Lisp_Object circ = XCDR (hot_spot);
20119 Lisp_Object lr, lx0, ly0;
20120 if (CONSP (circ)
20121 && CONSP (XCAR (circ))
20122 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20123 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20124 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20125 {
20126 double r = XFLOATINT (lr);
20127 double dx = XINT (lx0) - x;
20128 double dy = XINT (ly0) - y;
20129 return (dx * dx + dy * dy <= r * r);
20130 }
20131 }
20132 else if (EQ (XCAR (hot_spot), Qpoly))
20133 {
20134 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20135 if (VECTORP (XCDR (hot_spot)))
20136 {
20137 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20138 Lisp_Object *poly = v->contents;
20139 int n = v->size;
20140 int i;
20141 int inside = 0;
20142 Lisp_Object lx, ly;
20143 int x0, y0;
20144
20145 /* Need an even number of coordinates, and at least 3 edges. */
20146 if (n < 6 || n & 1)
20147 return 0;
20148
20149 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20150 If count is odd, we are inside polygon. Pixels on edges
20151 may or may not be included depending on actual geometry of the
20152 polygon. */
20153 if ((lx = poly[n-2], !INTEGERP (lx))
20154 || (ly = poly[n-1], !INTEGERP (lx)))
20155 return 0;
20156 x0 = XINT (lx), y0 = XINT (ly);
20157 for (i = 0; i < n; i += 2)
20158 {
20159 int x1 = x0, y1 = y0;
20160 if ((lx = poly[i], !INTEGERP (lx))
20161 || (ly = poly[i+1], !INTEGERP (ly)))
20162 return 0;
20163 x0 = XINT (lx), y0 = XINT (ly);
20164
20165 /* Does this segment cross the X line? */
20166 if (x0 >= x)
20167 {
20168 if (x1 >= x)
20169 continue;
20170 }
20171 else if (x1 < x)
20172 continue;
20173 if (y > y0 && y > y1)
20174 continue;
20175 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20176 inside = !inside;
20177 }
20178 return inside;
20179 }
20180 }
20181 return 0;
20182 }
20183
20184 Lisp_Object
20185 find_hot_spot (map, x, y)
20186 Lisp_Object map;
20187 int x, y;
20188 {
20189 while (CONSP (map))
20190 {
20191 if (CONSP (XCAR (map))
20192 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20193 return XCAR (map);
20194 map = XCDR (map);
20195 }
20196
20197 return Qnil;
20198 }
20199
20200 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20201 3, 3, 0,
20202 doc: /* Lookup in image map MAP coordinates X and Y.
20203 An image map is an alist where each element has the format (AREA ID PLIST).
20204 An AREA is specified as either a rectangle, a circle, or a polygon:
20205 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20206 pixel coordinates of the upper left and bottom right corners.
20207 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20208 and the radius of the circle; r may be a float or integer.
20209 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20210 vector describes one corner in the polygon.
20211 Returns the alist element for the first matching AREA in MAP. */)
20212 (map, x, y)
20213 Lisp_Object map;
20214 Lisp_Object x, y;
20215 {
20216 int ix, iy;
20217 if (NILP (map))
20218 return Qnil;
20219
20220 CHECK_NUMBER (x);
20221 CHECK_NUMBER (y);
20222
20223 return find_hot_spot (map, XINT (x), XINT (y));
20224 }
20225
20226
20227 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20228 static void
20229 define_frame_cursor1 (f, cursor, pointer)
20230 struct frame *f;
20231 Cursor cursor;
20232 Lisp_Object pointer;
20233 {
20234 if (!NILP (pointer))
20235 {
20236 if (EQ (pointer, Qarrow))
20237 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20238 else if (EQ (pointer, Qhand))
20239 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20240 else if (EQ (pointer, Qtext))
20241 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20242 else if (EQ (pointer, intern ("hdrag")))
20243 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20244 #ifdef HAVE_X_WINDOWS
20245 else if (EQ (pointer, intern ("vdrag")))
20246 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20247 #endif
20248 else if (EQ (pointer, intern ("hourglass")))
20249 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20250 else if (EQ (pointer, Qmodeline))
20251 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20252 else
20253 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20254 }
20255
20256 #ifndef HAVE_CARBON
20257 if (cursor != No_Cursor)
20258 #else
20259 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20260 #endif
20261 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20262 }
20263
20264 /* Take proper action when mouse has moved to the mode or header line
20265 or marginal area AREA of window W, x-position X and y-position Y.
20266 X is relative to the start of the text display area of W, so the
20267 width of bitmap areas and scroll bars must be subtracted to get a
20268 position relative to the start of the mode line. */
20269
20270 static void
20271 note_mode_line_or_margin_highlight (w, x, y, area)
20272 struct window *w;
20273 int x, y;
20274 enum window_part area;
20275 {
20276 struct frame *f = XFRAME (w->frame);
20277 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20278 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20279 Lisp_Object pointer = Qnil;
20280 int charpos, dx, dy, width, height;
20281 Lisp_Object string, object = Qnil;
20282 Lisp_Object pos, help, image;
20283
20284 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20285 string = mode_line_string (w, area, &x, &y, &charpos,
20286 &object, &dx, &dy, &width, &height);
20287 else
20288 {
20289 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20290 string = marginal_area_string (w, area, &x, &y, &charpos,
20291 &object, &dx, &dy, &width, &height);
20292 }
20293
20294 help = Qnil;
20295
20296 if (IMAGEP (object))
20297 {
20298 Lisp_Object image_map, hotspot;
20299 if ((image_map = Fplist_get (XCDR (object), QCmap),
20300 !NILP (image_map))
20301 && (hotspot = find_hot_spot (image_map, dx, dy),
20302 CONSP (hotspot))
20303 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20304 {
20305 Lisp_Object area_id, plist;
20306
20307 area_id = XCAR (hotspot);
20308 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20309 If so, we could look for mouse-enter, mouse-leave
20310 properties in PLIST (and do something...). */
20311 if ((plist = XCDR (hotspot), CONSP (plist)))
20312 {
20313 pointer = Fplist_get (plist, Qpointer);
20314 if (NILP (pointer))
20315 pointer = Qhand;
20316 help = Fplist_get (plist, Qhelp_echo);
20317 if (!NILP (help))
20318 {
20319 help_echo_string = help;
20320 /* Is this correct? ++kfs */
20321 XSETWINDOW (help_echo_window, w);
20322 help_echo_object = w->buffer;
20323 help_echo_pos = charpos;
20324 }
20325 }
20326 if (NILP (pointer))
20327 pointer = Fplist_get (XCDR (object), QCpointer);
20328 }
20329 }
20330
20331 if (STRINGP (string))
20332 {
20333 pos = make_number (charpos);
20334 /* If we're on a string with `help-echo' text property, arrange
20335 for the help to be displayed. This is done by setting the
20336 global variable help_echo_string to the help string. */
20337 help = Fget_text_property (pos, Qhelp_echo, string);
20338 if (!NILP (help))
20339 {
20340 help_echo_string = help;
20341 XSETWINDOW (help_echo_window, w);
20342 help_echo_object = string;
20343 help_echo_pos = charpos;
20344 }
20345
20346 if (NILP (pointer))
20347 pointer = Fget_text_property (pos, Qpointer, string);
20348
20349 /* Change the mouse pointer according to what is under X/Y. */
20350 if (NILP (pointer) && area == ON_MODE_LINE)
20351 {
20352 Lisp_Object map;
20353 map = Fget_text_property (pos, Qlocal_map, string);
20354 if (!KEYMAPP (map))
20355 map = Fget_text_property (pos, Qkeymap, string);
20356 if (!KEYMAPP (map))
20357 cursor = dpyinfo->vertical_scroll_bar_cursor;
20358 }
20359 }
20360
20361 define_frame_cursor1 (f, cursor, pointer);
20362 }
20363
20364
20365 /* EXPORT:
20366 Take proper action when the mouse has moved to position X, Y on
20367 frame F as regards highlighting characters that have mouse-face
20368 properties. Also de-highlighting chars where the mouse was before.
20369 X and Y can be negative or out of range. */
20370
20371 void
20372 note_mouse_highlight (f, x, y)
20373 struct frame *f;
20374 int x, y;
20375 {
20376 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20377 enum window_part part;
20378 Lisp_Object window;
20379 struct window *w;
20380 Cursor cursor = No_Cursor;
20381 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20382 struct buffer *b;
20383
20384 /* When a menu is active, don't highlight because this looks odd. */
20385 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20386 if (popup_activated ())
20387 return;
20388 #endif
20389
20390 if (NILP (Vmouse_highlight)
20391 || !f->glyphs_initialized_p)
20392 return;
20393
20394 dpyinfo->mouse_face_mouse_x = x;
20395 dpyinfo->mouse_face_mouse_y = y;
20396 dpyinfo->mouse_face_mouse_frame = f;
20397
20398 if (dpyinfo->mouse_face_defer)
20399 return;
20400
20401 if (gc_in_progress)
20402 {
20403 dpyinfo->mouse_face_deferred_gc = 1;
20404 return;
20405 }
20406
20407 /* Which window is that in? */
20408 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20409
20410 /* If we were displaying active text in another window, clear that. */
20411 if (! EQ (window, dpyinfo->mouse_face_window))
20412 clear_mouse_face (dpyinfo);
20413
20414 /* Not on a window -> return. */
20415 if (!WINDOWP (window))
20416 return;
20417
20418 /* Reset help_echo_string. It will get recomputed below. */
20419 help_echo_string = Qnil;
20420
20421 /* Convert to window-relative pixel coordinates. */
20422 w = XWINDOW (window);
20423 frame_to_window_pixel_xy (w, &x, &y);
20424
20425 /* Handle tool-bar window differently since it doesn't display a
20426 buffer. */
20427 if (EQ (window, f->tool_bar_window))
20428 {
20429 note_tool_bar_highlight (f, x, y);
20430 return;
20431 }
20432
20433 /* Mouse is on the mode, header line or margin? */
20434 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20435 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20436 {
20437 note_mode_line_or_margin_highlight (w, x, y, part);
20438 return;
20439 }
20440
20441 if (part == ON_VERTICAL_BORDER)
20442 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20443 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20444 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20445 else
20446 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20447
20448 /* Are we in a window whose display is up to date?
20449 And verify the buffer's text has not changed. */
20450 b = XBUFFER (w->buffer);
20451 if (part == ON_TEXT
20452 && EQ (w->window_end_valid, w->buffer)
20453 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20454 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20455 {
20456 int hpos, vpos, pos, i, dx, dy, area;
20457 struct glyph *glyph;
20458 Lisp_Object object;
20459 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20460 Lisp_Object *overlay_vec = NULL;
20461 int len, noverlays;
20462 struct buffer *obuf;
20463 int obegv, ozv, same_region;
20464
20465 /* Find the glyph under X/Y. */
20466 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20467
20468 /* Look for :pointer property on image. */
20469 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20470 {
20471 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20472 if (img != NULL && IMAGEP (img->spec))
20473 {
20474 Lisp_Object image_map, hotspot;
20475 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20476 !NILP (image_map))
20477 && (hotspot = find_hot_spot (image_map, dx, dy),
20478 CONSP (hotspot))
20479 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20480 {
20481 Lisp_Object area_id, plist;
20482
20483 area_id = XCAR (hotspot);
20484 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20485 If so, we could look for mouse-enter, mouse-leave
20486 properties in PLIST (and do something...). */
20487 if ((plist = XCDR (hotspot), CONSP (plist)))
20488 {
20489 pointer = Fplist_get (plist, Qpointer);
20490 if (NILP (pointer))
20491 pointer = Qhand;
20492 help_echo_string = Fplist_get (plist, Qhelp_echo);
20493 if (!NILP (help_echo_string))
20494 {
20495 help_echo_window = window;
20496 help_echo_object = glyph->object;
20497 help_echo_pos = glyph->charpos;
20498 }
20499 }
20500 }
20501 if (NILP (pointer))
20502 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20503 }
20504 }
20505
20506 /* Clear mouse face if X/Y not over text. */
20507 if (glyph == NULL
20508 || area != TEXT_AREA
20509 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20510 {
20511 if (clear_mouse_face (dpyinfo))
20512 cursor = No_Cursor;
20513 if (NILP (pointer))
20514 {
20515 if (area != TEXT_AREA)
20516 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20517 else
20518 pointer = Vvoid_text_area_pointer;
20519 }
20520 goto set_cursor;
20521 }
20522
20523 pos = glyph->charpos;
20524 object = glyph->object;
20525 if (!STRINGP (object) && !BUFFERP (object))
20526 goto set_cursor;
20527
20528 /* If we get an out-of-range value, return now; avoid an error. */
20529 if (BUFFERP (object) && pos > BUF_Z (b))
20530 goto set_cursor;
20531
20532 /* Make the window's buffer temporarily current for
20533 overlays_at and compute_char_face. */
20534 obuf = current_buffer;
20535 current_buffer = b;
20536 obegv = BEGV;
20537 ozv = ZV;
20538 BEGV = BEG;
20539 ZV = Z;
20540
20541 /* Is this char mouse-active or does it have help-echo? */
20542 position = make_number (pos);
20543
20544 if (BUFFERP (object))
20545 {
20546 /* Put all the overlays we want in a vector in overlay_vec.
20547 Store the length in len. If there are more than 10, make
20548 enough space for all, and try again. */
20549 len = 10;
20550 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20551 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20552 if (noverlays > len)
20553 {
20554 len = noverlays;
20555 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20556 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20557 }
20558
20559 /* Sort overlays into increasing priority order. */
20560 noverlays = sort_overlays (overlay_vec, noverlays, w);
20561 }
20562 else
20563 noverlays = 0;
20564
20565 same_region = (EQ (window, dpyinfo->mouse_face_window)
20566 && vpos >= dpyinfo->mouse_face_beg_row
20567 && vpos <= dpyinfo->mouse_face_end_row
20568 && (vpos > dpyinfo->mouse_face_beg_row
20569 || hpos >= dpyinfo->mouse_face_beg_col)
20570 && (vpos < dpyinfo->mouse_face_end_row
20571 || hpos < dpyinfo->mouse_face_end_col
20572 || dpyinfo->mouse_face_past_end));
20573
20574 if (same_region)
20575 cursor = No_Cursor;
20576
20577 /* Check mouse-face highlighting. */
20578 if (! same_region
20579 /* If there exists an overlay with mouse-face overlapping
20580 the one we are currently highlighting, we have to
20581 check if we enter the overlapping overlay, and then
20582 highlight only that. */
20583 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20584 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20585 {
20586 /* Find the highest priority overlay that has a mouse-face
20587 property. */
20588 overlay = Qnil;
20589 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20590 {
20591 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20592 if (!NILP (mouse_face))
20593 overlay = overlay_vec[i];
20594 }
20595
20596 /* If we're actually highlighting the same overlay as
20597 before, there's no need to do that again. */
20598 if (!NILP (overlay)
20599 && EQ (overlay, dpyinfo->mouse_face_overlay))
20600 goto check_help_echo;
20601
20602 dpyinfo->mouse_face_overlay = overlay;
20603
20604 /* Clear the display of the old active region, if any. */
20605 if (clear_mouse_face (dpyinfo))
20606 cursor = No_Cursor;
20607
20608 /* If no overlay applies, get a text property. */
20609 if (NILP (overlay))
20610 mouse_face = Fget_text_property (position, Qmouse_face, object);
20611
20612 /* Handle the overlay case. */
20613 if (!NILP (overlay))
20614 {
20615 /* Find the range of text around this char that
20616 should be active. */
20617 Lisp_Object before, after;
20618 int ignore;
20619
20620 before = Foverlay_start (overlay);
20621 after = Foverlay_end (overlay);
20622 /* Record this as the current active region. */
20623 fast_find_position (w, XFASTINT (before),
20624 &dpyinfo->mouse_face_beg_col,
20625 &dpyinfo->mouse_face_beg_row,
20626 &dpyinfo->mouse_face_beg_x,
20627 &dpyinfo->mouse_face_beg_y, Qnil);
20628
20629 dpyinfo->mouse_face_past_end
20630 = !fast_find_position (w, XFASTINT (after),
20631 &dpyinfo->mouse_face_end_col,
20632 &dpyinfo->mouse_face_end_row,
20633 &dpyinfo->mouse_face_end_x,
20634 &dpyinfo->mouse_face_end_y, Qnil);
20635 dpyinfo->mouse_face_window = window;
20636
20637 dpyinfo->mouse_face_face_id
20638 = face_at_buffer_position (w, pos, 0, 0,
20639 &ignore, pos + 1,
20640 !dpyinfo->mouse_face_hidden);
20641
20642 /* Display it as active. */
20643 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20644 cursor = No_Cursor;
20645 }
20646 /* Handle the text property case. */
20647 else if (!NILP (mouse_face) && BUFFERP (object))
20648 {
20649 /* Find the range of text around this char that
20650 should be active. */
20651 Lisp_Object before, after, beginning, end;
20652 int ignore;
20653
20654 beginning = Fmarker_position (w->start);
20655 end = make_number (BUF_Z (XBUFFER (object))
20656 - XFASTINT (w->window_end_pos));
20657 before
20658 = Fprevious_single_property_change (make_number (pos + 1),
20659 Qmouse_face,
20660 object, beginning);
20661 after
20662 = Fnext_single_property_change (position, Qmouse_face,
20663 object, end);
20664
20665 /* Record this as the current active region. */
20666 fast_find_position (w, XFASTINT (before),
20667 &dpyinfo->mouse_face_beg_col,
20668 &dpyinfo->mouse_face_beg_row,
20669 &dpyinfo->mouse_face_beg_x,
20670 &dpyinfo->mouse_face_beg_y, Qnil);
20671 dpyinfo->mouse_face_past_end
20672 = !fast_find_position (w, XFASTINT (after),
20673 &dpyinfo->mouse_face_end_col,
20674 &dpyinfo->mouse_face_end_row,
20675 &dpyinfo->mouse_face_end_x,
20676 &dpyinfo->mouse_face_end_y, Qnil);
20677 dpyinfo->mouse_face_window = window;
20678
20679 if (BUFFERP (object))
20680 dpyinfo->mouse_face_face_id
20681 = face_at_buffer_position (w, pos, 0, 0,
20682 &ignore, pos + 1,
20683 !dpyinfo->mouse_face_hidden);
20684
20685 /* Display it as active. */
20686 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20687 cursor = No_Cursor;
20688 }
20689 else if (!NILP (mouse_face) && STRINGP (object))
20690 {
20691 Lisp_Object b, e;
20692 int ignore;
20693
20694 b = Fprevious_single_property_change (make_number (pos + 1),
20695 Qmouse_face,
20696 object, Qnil);
20697 e = Fnext_single_property_change (position, Qmouse_face,
20698 object, Qnil);
20699 if (NILP (b))
20700 b = make_number (0);
20701 if (NILP (e))
20702 e = make_number (SCHARS (object) - 1);
20703 fast_find_string_pos (w, XINT (b), object,
20704 &dpyinfo->mouse_face_beg_col,
20705 &dpyinfo->mouse_face_beg_row,
20706 &dpyinfo->mouse_face_beg_x,
20707 &dpyinfo->mouse_face_beg_y, 0);
20708 fast_find_string_pos (w, XINT (e), object,
20709 &dpyinfo->mouse_face_end_col,
20710 &dpyinfo->mouse_face_end_row,
20711 &dpyinfo->mouse_face_end_x,
20712 &dpyinfo->mouse_face_end_y, 1);
20713 dpyinfo->mouse_face_past_end = 0;
20714 dpyinfo->mouse_face_window = window;
20715 dpyinfo->mouse_face_face_id
20716 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20717 glyph->face_id, 1);
20718 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20719 cursor = No_Cursor;
20720 }
20721 else if (STRINGP (object) && NILP (mouse_face))
20722 {
20723 /* A string which doesn't have mouse-face, but
20724 the text ``under'' it might have. */
20725 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20726 int start = MATRIX_ROW_START_CHARPOS (r);
20727
20728 pos = string_buffer_position (w, object, start);
20729 if (pos > 0)
20730 mouse_face = get_char_property_and_overlay (make_number (pos),
20731 Qmouse_face,
20732 w->buffer,
20733 &overlay);
20734 if (!NILP (mouse_face) && !NILP (overlay))
20735 {
20736 Lisp_Object before = Foverlay_start (overlay);
20737 Lisp_Object after = Foverlay_end (overlay);
20738 int ignore;
20739
20740 /* Note that we might not be able to find position
20741 BEFORE in the glyph matrix if the overlay is
20742 entirely covered by a `display' property. In
20743 this case, we overshoot. So let's stop in
20744 the glyph matrix before glyphs for OBJECT. */
20745 fast_find_position (w, XFASTINT (before),
20746 &dpyinfo->mouse_face_beg_col,
20747 &dpyinfo->mouse_face_beg_row,
20748 &dpyinfo->mouse_face_beg_x,
20749 &dpyinfo->mouse_face_beg_y,
20750 object);
20751
20752 dpyinfo->mouse_face_past_end
20753 = !fast_find_position (w, XFASTINT (after),
20754 &dpyinfo->mouse_face_end_col,
20755 &dpyinfo->mouse_face_end_row,
20756 &dpyinfo->mouse_face_end_x,
20757 &dpyinfo->mouse_face_end_y,
20758 Qnil);
20759 dpyinfo->mouse_face_window = window;
20760 dpyinfo->mouse_face_face_id
20761 = face_at_buffer_position (w, pos, 0, 0,
20762 &ignore, pos + 1,
20763 !dpyinfo->mouse_face_hidden);
20764
20765 /* Display it as active. */
20766 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20767 cursor = No_Cursor;
20768 }
20769 }
20770 }
20771
20772 check_help_echo:
20773
20774 /* Look for a `help-echo' property. */
20775 if (NILP (help_echo_string)) {
20776 Lisp_Object help, overlay;
20777
20778 /* Check overlays first. */
20779 help = overlay = Qnil;
20780 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20781 {
20782 overlay = overlay_vec[i];
20783 help = Foverlay_get (overlay, Qhelp_echo);
20784 }
20785
20786 if (!NILP (help))
20787 {
20788 help_echo_string = help;
20789 help_echo_window = window;
20790 help_echo_object = overlay;
20791 help_echo_pos = pos;
20792 }
20793 else
20794 {
20795 Lisp_Object object = glyph->object;
20796 int charpos = glyph->charpos;
20797
20798 /* Try text properties. */
20799 if (STRINGP (object)
20800 && charpos >= 0
20801 && charpos < SCHARS (object))
20802 {
20803 help = Fget_text_property (make_number (charpos),
20804 Qhelp_echo, object);
20805 if (NILP (help))
20806 {
20807 /* If the string itself doesn't specify a help-echo,
20808 see if the buffer text ``under'' it does. */
20809 struct glyph_row *r
20810 = MATRIX_ROW (w->current_matrix, vpos);
20811 int start = MATRIX_ROW_START_CHARPOS (r);
20812 int pos = string_buffer_position (w, object, start);
20813 if (pos > 0)
20814 {
20815 help = Fget_char_property (make_number (pos),
20816 Qhelp_echo, w->buffer);
20817 if (!NILP (help))
20818 {
20819 charpos = pos;
20820 object = w->buffer;
20821 }
20822 }
20823 }
20824 }
20825 else if (BUFFERP (object)
20826 && charpos >= BEGV
20827 && charpos < ZV)
20828 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20829 object);
20830
20831 if (!NILP (help))
20832 {
20833 help_echo_string = help;
20834 help_echo_window = window;
20835 help_echo_object = object;
20836 help_echo_pos = charpos;
20837 }
20838 }
20839 }
20840
20841 /* Look for a `pointer' property. */
20842 if (NILP (pointer))
20843 {
20844 /* Check overlays first. */
20845 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
20846 pointer = Foverlay_get (overlay_vec[i], Qpointer);
20847
20848 if (NILP (pointer))
20849 {
20850 Lisp_Object object = glyph->object;
20851 int charpos = glyph->charpos;
20852
20853 /* Try text properties. */
20854 if (STRINGP (object)
20855 && charpos >= 0
20856 && charpos < SCHARS (object))
20857 {
20858 pointer = Fget_text_property (make_number (charpos),
20859 Qpointer, object);
20860 if (NILP (pointer))
20861 {
20862 /* If the string itself doesn't specify a pointer,
20863 see if the buffer text ``under'' it does. */
20864 struct glyph_row *r
20865 = MATRIX_ROW (w->current_matrix, vpos);
20866 int start = MATRIX_ROW_START_CHARPOS (r);
20867 int pos = string_buffer_position (w, object, start);
20868 if (pos > 0)
20869 pointer = Fget_char_property (make_number (pos),
20870 Qpointer, w->buffer);
20871 }
20872 }
20873 else if (BUFFERP (object)
20874 && charpos >= BEGV
20875 && charpos < ZV)
20876 pointer = Fget_text_property (make_number (charpos),
20877 Qpointer, object);
20878 }
20879 }
20880
20881 BEGV = obegv;
20882 ZV = ozv;
20883 current_buffer = obuf;
20884 }
20885
20886 set_cursor:
20887
20888 define_frame_cursor1 (f, cursor, pointer);
20889 }
20890
20891
20892 /* EXPORT for RIF:
20893 Clear any mouse-face on window W. This function is part of the
20894 redisplay interface, and is called from try_window_id and similar
20895 functions to ensure the mouse-highlight is off. */
20896
20897 void
20898 x_clear_window_mouse_face (w)
20899 struct window *w;
20900 {
20901 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20902 Lisp_Object window;
20903
20904 BLOCK_INPUT;
20905 XSETWINDOW (window, w);
20906 if (EQ (window, dpyinfo->mouse_face_window))
20907 clear_mouse_face (dpyinfo);
20908 UNBLOCK_INPUT;
20909 }
20910
20911
20912 /* EXPORT:
20913 Just discard the mouse face information for frame F, if any.
20914 This is used when the size of F is changed. */
20915
20916 void
20917 cancel_mouse_face (f)
20918 struct frame *f;
20919 {
20920 Lisp_Object window;
20921 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20922
20923 window = dpyinfo->mouse_face_window;
20924 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20925 {
20926 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20927 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20928 dpyinfo->mouse_face_window = Qnil;
20929 }
20930 }
20931
20932
20933 #endif /* HAVE_WINDOW_SYSTEM */
20934
20935 \f
20936 /***********************************************************************
20937 Exposure Events
20938 ***********************************************************************/
20939
20940 #ifdef HAVE_WINDOW_SYSTEM
20941
20942 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20943 which intersects rectangle R. R is in window-relative coordinates. */
20944
20945 static void
20946 expose_area (w, row, r, area)
20947 struct window *w;
20948 struct glyph_row *row;
20949 XRectangle *r;
20950 enum glyph_row_area area;
20951 {
20952 struct glyph *first = row->glyphs[area];
20953 struct glyph *end = row->glyphs[area] + row->used[area];
20954 struct glyph *last;
20955 int first_x, start_x, x;
20956
20957 if (area == TEXT_AREA && row->fill_line_p)
20958 /* If row extends face to end of line write the whole line. */
20959 draw_glyphs (w, 0, row, area,
20960 0, row->used[area],
20961 DRAW_NORMAL_TEXT, 0);
20962 else
20963 {
20964 /* Set START_X to the window-relative start position for drawing glyphs of
20965 AREA. The first glyph of the text area can be partially visible.
20966 The first glyphs of other areas cannot. */
20967 start_x = window_box_left_offset (w, area);
20968 x = start_x;
20969 if (area == TEXT_AREA)
20970 x += row->x;
20971
20972 /* Find the first glyph that must be redrawn. */
20973 while (first < end
20974 && x + first->pixel_width < r->x)
20975 {
20976 x += first->pixel_width;
20977 ++first;
20978 }
20979
20980 /* Find the last one. */
20981 last = first;
20982 first_x = x;
20983 while (last < end
20984 && x < r->x + r->width)
20985 {
20986 x += last->pixel_width;
20987 ++last;
20988 }
20989
20990 /* Repaint. */
20991 if (last > first)
20992 draw_glyphs (w, first_x - start_x, row, area,
20993 first - row->glyphs[area], last - row->glyphs[area],
20994 DRAW_NORMAL_TEXT, 0);
20995 }
20996 }
20997
20998
20999 /* Redraw the parts of the glyph row ROW on window W intersecting
21000 rectangle R. R is in window-relative coordinates. Value is
21001 non-zero if mouse-face was overwritten. */
21002
21003 static int
21004 expose_line (w, row, r)
21005 struct window *w;
21006 struct glyph_row *row;
21007 XRectangle *r;
21008 {
21009 xassert (row->enabled_p);
21010
21011 if (row->mode_line_p || w->pseudo_window_p)
21012 draw_glyphs (w, 0, row, TEXT_AREA,
21013 0, row->used[TEXT_AREA],
21014 DRAW_NORMAL_TEXT, 0);
21015 else
21016 {
21017 if (row->used[LEFT_MARGIN_AREA])
21018 expose_area (w, row, r, LEFT_MARGIN_AREA);
21019 if (row->used[TEXT_AREA])
21020 expose_area (w, row, r, TEXT_AREA);
21021 if (row->used[RIGHT_MARGIN_AREA])
21022 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21023 draw_row_fringe_bitmaps (w, row);
21024 }
21025
21026 return row->mouse_face_p;
21027 }
21028
21029
21030 /* Redraw those parts of glyphs rows during expose event handling that
21031 overlap other rows. Redrawing of an exposed line writes over parts
21032 of lines overlapping that exposed line; this function fixes that.
21033
21034 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21035 row in W's current matrix that is exposed and overlaps other rows.
21036 LAST_OVERLAPPING_ROW is the last such row. */
21037
21038 static void
21039 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21040 struct window *w;
21041 struct glyph_row *first_overlapping_row;
21042 struct glyph_row *last_overlapping_row;
21043 {
21044 struct glyph_row *row;
21045
21046 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21047 if (row->overlapping_p)
21048 {
21049 xassert (row->enabled_p && !row->mode_line_p);
21050
21051 if (row->used[LEFT_MARGIN_AREA])
21052 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21053
21054 if (row->used[TEXT_AREA])
21055 x_fix_overlapping_area (w, row, TEXT_AREA);
21056
21057 if (row->used[RIGHT_MARGIN_AREA])
21058 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21059 }
21060 }
21061
21062
21063 /* Return non-zero if W's cursor intersects rectangle R. */
21064
21065 static int
21066 phys_cursor_in_rect_p (w, r)
21067 struct window *w;
21068 XRectangle *r;
21069 {
21070 XRectangle cr, result;
21071 struct glyph *cursor_glyph;
21072
21073 cursor_glyph = get_phys_cursor_glyph (w);
21074 if (cursor_glyph)
21075 {
21076 /* r is relative to W's box, but w->phys_cursor.x is relative
21077 to left edge of W's TEXT area. Adjust it. */
21078 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21079 cr.y = w->phys_cursor.y;
21080 cr.width = cursor_glyph->pixel_width;
21081 cr.height = w->phys_cursor_height;
21082 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21083 I assume the effect is the same -- and this is portable. */
21084 return x_intersect_rectangles (&cr, r, &result);
21085 }
21086 else
21087 return 0;
21088 }
21089
21090
21091 /* EXPORT:
21092 Draw a vertical window border to the right of window W if W doesn't
21093 have vertical scroll bars. */
21094
21095 void
21096 x_draw_vertical_border (w)
21097 struct window *w;
21098 {
21099 struct frame *f = XFRAME (WINDOW_FRAME (w));
21100
21101 /* We could do better, if we knew what type of scroll-bar the adjacent
21102 windows (on either side) have... But we don't :-(
21103 However, I think this works ok. ++KFS 2003-04-25 */
21104
21105 /* Redraw borders between horizontally adjacent windows. Don't
21106 do it for frames with vertical scroll bars because either the
21107 right scroll bar of a window, or the left scroll bar of its
21108 neighbor will suffice as a border. */
21109 if (!WINDOW_RIGHTMOST_P (w)
21110 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21111 {
21112 int x0, x1, y0, y1;
21113
21114 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21115 y1 -= 1;
21116
21117 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21118 }
21119 else if (!WINDOW_LEFTMOST_P (w)
21120 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21121 {
21122 int x0, x1, y0, y1;
21123
21124 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21125 y1 -= 1;
21126
21127 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21128 }
21129 }
21130
21131
21132 /* Redraw the part of window W intersection rectangle FR. Pixel
21133 coordinates in FR are frame-relative. Call this function with
21134 input blocked. Value is non-zero if the exposure overwrites
21135 mouse-face. */
21136
21137 static int
21138 expose_window (w, fr)
21139 struct window *w;
21140 XRectangle *fr;
21141 {
21142 struct frame *f = XFRAME (w->frame);
21143 XRectangle wr, r;
21144 int mouse_face_overwritten_p = 0;
21145
21146 /* If window is not yet fully initialized, do nothing. This can
21147 happen when toolkit scroll bars are used and a window is split.
21148 Reconfiguring the scroll bar will generate an expose for a newly
21149 created window. */
21150 if (w->current_matrix == NULL)
21151 return 0;
21152
21153 /* When we're currently updating the window, display and current
21154 matrix usually don't agree. Arrange for a thorough display
21155 later. */
21156 if (w == updated_window)
21157 {
21158 SET_FRAME_GARBAGED (f);
21159 return 0;
21160 }
21161
21162 /* Frame-relative pixel rectangle of W. */
21163 wr.x = WINDOW_LEFT_EDGE_X (w);
21164 wr.y = WINDOW_TOP_EDGE_Y (w);
21165 wr.width = WINDOW_TOTAL_WIDTH (w);
21166 wr.height = WINDOW_TOTAL_HEIGHT (w);
21167
21168 if (x_intersect_rectangles (fr, &wr, &r))
21169 {
21170 int yb = window_text_bottom_y (w);
21171 struct glyph_row *row;
21172 int cursor_cleared_p;
21173 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21174
21175 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21176 r.x, r.y, r.width, r.height));
21177
21178 /* Convert to window coordinates. */
21179 r.x -= WINDOW_LEFT_EDGE_X (w);
21180 r.y -= WINDOW_TOP_EDGE_Y (w);
21181
21182 /* Turn off the cursor. */
21183 if (!w->pseudo_window_p
21184 && phys_cursor_in_rect_p (w, &r))
21185 {
21186 x_clear_cursor (w);
21187 cursor_cleared_p = 1;
21188 }
21189 else
21190 cursor_cleared_p = 0;
21191
21192 /* Update lines intersecting rectangle R. */
21193 first_overlapping_row = last_overlapping_row = NULL;
21194 for (row = w->current_matrix->rows;
21195 row->enabled_p;
21196 ++row)
21197 {
21198 int y0 = row->y;
21199 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21200
21201 if ((y0 >= r.y && y0 < r.y + r.height)
21202 || (y1 > r.y && y1 < r.y + r.height)
21203 || (r.y >= y0 && r.y < y1)
21204 || (r.y + r.height > y0 && r.y + r.height < y1))
21205 {
21206 if (row->overlapping_p)
21207 {
21208 if (first_overlapping_row == NULL)
21209 first_overlapping_row = row;
21210 last_overlapping_row = row;
21211 }
21212
21213 if (expose_line (w, row, &r))
21214 mouse_face_overwritten_p = 1;
21215 }
21216
21217 if (y1 >= yb)
21218 break;
21219 }
21220
21221 /* Display the mode line if there is one. */
21222 if (WINDOW_WANTS_MODELINE_P (w)
21223 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21224 row->enabled_p)
21225 && row->y < r.y + r.height)
21226 {
21227 if (expose_line (w, row, &r))
21228 mouse_face_overwritten_p = 1;
21229 }
21230
21231 if (!w->pseudo_window_p)
21232 {
21233 /* Fix the display of overlapping rows. */
21234 if (first_overlapping_row)
21235 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21236
21237 /* Draw border between windows. */
21238 x_draw_vertical_border (w);
21239
21240 /* Turn the cursor on again. */
21241 if (cursor_cleared_p)
21242 update_window_cursor (w, 1);
21243 }
21244 }
21245
21246 #ifdef HAVE_CARBON
21247 /* Display scroll bar for this window. */
21248 if (!NILP (w->vertical_scroll_bar))
21249 {
21250 /* ++KFS:
21251 If this doesn't work here (maybe some header files are missing),
21252 make a function in macterm.c and call it to do the job! */
21253 ControlHandle ch
21254 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21255
21256 Draw1Control (ch);
21257 }
21258 #endif
21259
21260 return mouse_face_overwritten_p;
21261 }
21262
21263
21264
21265 /* Redraw (parts) of all windows in the window tree rooted at W that
21266 intersect R. R contains frame pixel coordinates. Value is
21267 non-zero if the exposure overwrites mouse-face. */
21268
21269 static int
21270 expose_window_tree (w, r)
21271 struct window *w;
21272 XRectangle *r;
21273 {
21274 struct frame *f = XFRAME (w->frame);
21275 int mouse_face_overwritten_p = 0;
21276
21277 while (w && !FRAME_GARBAGED_P (f))
21278 {
21279 if (!NILP (w->hchild))
21280 mouse_face_overwritten_p
21281 |= expose_window_tree (XWINDOW (w->hchild), r);
21282 else if (!NILP (w->vchild))
21283 mouse_face_overwritten_p
21284 |= expose_window_tree (XWINDOW (w->vchild), r);
21285 else
21286 mouse_face_overwritten_p |= expose_window (w, r);
21287
21288 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21289 }
21290
21291 return mouse_face_overwritten_p;
21292 }
21293
21294
21295 /* EXPORT:
21296 Redisplay an exposed area of frame F. X and Y are the upper-left
21297 corner of the exposed rectangle. W and H are width and height of
21298 the exposed area. All are pixel values. W or H zero means redraw
21299 the entire frame. */
21300
21301 void
21302 expose_frame (f, x, y, w, h)
21303 struct frame *f;
21304 int x, y, w, h;
21305 {
21306 XRectangle r;
21307 int mouse_face_overwritten_p = 0;
21308
21309 TRACE ((stderr, "expose_frame "));
21310
21311 /* No need to redraw if frame will be redrawn soon. */
21312 if (FRAME_GARBAGED_P (f))
21313 {
21314 TRACE ((stderr, " garbaged\n"));
21315 return;
21316 }
21317
21318 #ifdef HAVE_CARBON
21319 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21320 or deactivated here, for unknown reasons, activated scroll bars
21321 are shown in deactivated frames in some instances. */
21322 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21323 activate_scroll_bars (f);
21324 else
21325 deactivate_scroll_bars (f);
21326 #endif
21327
21328 /* If basic faces haven't been realized yet, there is no point in
21329 trying to redraw anything. This can happen when we get an expose
21330 event while Emacs is starting, e.g. by moving another window. */
21331 if (FRAME_FACE_CACHE (f) == NULL
21332 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21333 {
21334 TRACE ((stderr, " no faces\n"));
21335 return;
21336 }
21337
21338 if (w == 0 || h == 0)
21339 {
21340 r.x = r.y = 0;
21341 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21342 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21343 }
21344 else
21345 {
21346 r.x = x;
21347 r.y = y;
21348 r.width = w;
21349 r.height = h;
21350 }
21351
21352 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21353 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21354
21355 if (WINDOWP (f->tool_bar_window))
21356 mouse_face_overwritten_p
21357 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21358
21359 #ifdef HAVE_X_WINDOWS
21360 #ifndef MSDOS
21361 #ifndef USE_X_TOOLKIT
21362 if (WINDOWP (f->menu_bar_window))
21363 mouse_face_overwritten_p
21364 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21365 #endif /* not USE_X_TOOLKIT */
21366 #endif
21367 #endif
21368
21369 /* Some window managers support a focus-follows-mouse style with
21370 delayed raising of frames. Imagine a partially obscured frame,
21371 and moving the mouse into partially obscured mouse-face on that
21372 frame. The visible part of the mouse-face will be highlighted,
21373 then the WM raises the obscured frame. With at least one WM, KDE
21374 2.1, Emacs is not getting any event for the raising of the frame
21375 (even tried with SubstructureRedirectMask), only Expose events.
21376 These expose events will draw text normally, i.e. not
21377 highlighted. Which means we must redo the highlight here.
21378 Subsume it under ``we love X''. --gerd 2001-08-15 */
21379 /* Included in Windows version because Windows most likely does not
21380 do the right thing if any third party tool offers
21381 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21382 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21383 {
21384 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21385 if (f == dpyinfo->mouse_face_mouse_frame)
21386 {
21387 int x = dpyinfo->mouse_face_mouse_x;
21388 int y = dpyinfo->mouse_face_mouse_y;
21389 clear_mouse_face (dpyinfo);
21390 note_mouse_highlight (f, x, y);
21391 }
21392 }
21393 }
21394
21395
21396 /* EXPORT:
21397 Determine the intersection of two rectangles R1 and R2. Return
21398 the intersection in *RESULT. Value is non-zero if RESULT is not
21399 empty. */
21400
21401 int
21402 x_intersect_rectangles (r1, r2, result)
21403 XRectangle *r1, *r2, *result;
21404 {
21405 XRectangle *left, *right;
21406 XRectangle *upper, *lower;
21407 int intersection_p = 0;
21408
21409 /* Rearrange so that R1 is the left-most rectangle. */
21410 if (r1->x < r2->x)
21411 left = r1, right = r2;
21412 else
21413 left = r2, right = r1;
21414
21415 /* X0 of the intersection is right.x0, if this is inside R1,
21416 otherwise there is no intersection. */
21417 if (right->x <= left->x + left->width)
21418 {
21419 result->x = right->x;
21420
21421 /* The right end of the intersection is the minimum of the
21422 the right ends of left and right. */
21423 result->width = (min (left->x + left->width, right->x + right->width)
21424 - result->x);
21425
21426 /* Same game for Y. */
21427 if (r1->y < r2->y)
21428 upper = r1, lower = r2;
21429 else
21430 upper = r2, lower = r1;
21431
21432 /* The upper end of the intersection is lower.y0, if this is inside
21433 of upper. Otherwise, there is no intersection. */
21434 if (lower->y <= upper->y + upper->height)
21435 {
21436 result->y = lower->y;
21437
21438 /* The lower end of the intersection is the minimum of the lower
21439 ends of upper and lower. */
21440 result->height = (min (lower->y + lower->height,
21441 upper->y + upper->height)
21442 - result->y);
21443 intersection_p = 1;
21444 }
21445 }
21446
21447 return intersection_p;
21448 }
21449
21450 #endif /* HAVE_WINDOW_SYSTEM */
21451
21452 \f
21453 /***********************************************************************
21454 Initialization
21455 ***********************************************************************/
21456
21457 void
21458 syms_of_xdisp ()
21459 {
21460 Vwith_echo_area_save_vector = Qnil;
21461 staticpro (&Vwith_echo_area_save_vector);
21462
21463 Vmessage_stack = Qnil;
21464 staticpro (&Vmessage_stack);
21465
21466 Qinhibit_redisplay = intern ("inhibit-redisplay");
21467 staticpro (&Qinhibit_redisplay);
21468
21469 message_dolog_marker1 = Fmake_marker ();
21470 staticpro (&message_dolog_marker1);
21471 message_dolog_marker2 = Fmake_marker ();
21472 staticpro (&message_dolog_marker2);
21473 message_dolog_marker3 = Fmake_marker ();
21474 staticpro (&message_dolog_marker3);
21475
21476 #if GLYPH_DEBUG
21477 defsubr (&Sdump_frame_glyph_matrix);
21478 defsubr (&Sdump_glyph_matrix);
21479 defsubr (&Sdump_glyph_row);
21480 defsubr (&Sdump_tool_bar_row);
21481 defsubr (&Strace_redisplay);
21482 defsubr (&Strace_to_stderr);
21483 #endif
21484 #ifdef HAVE_WINDOW_SYSTEM
21485 defsubr (&Stool_bar_lines_needed);
21486 defsubr (&Slookup_image_map);
21487 #endif
21488 defsubr (&Sformat_mode_line);
21489
21490 staticpro (&Qmenu_bar_update_hook);
21491 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21492
21493 staticpro (&Qoverriding_terminal_local_map);
21494 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21495
21496 staticpro (&Qoverriding_local_map);
21497 Qoverriding_local_map = intern ("overriding-local-map");
21498
21499 staticpro (&Qwindow_scroll_functions);
21500 Qwindow_scroll_functions = intern ("window-scroll-functions");
21501
21502 staticpro (&Qredisplay_end_trigger_functions);
21503 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21504
21505 staticpro (&Qinhibit_point_motion_hooks);
21506 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21507
21508 QCdata = intern (":data");
21509 staticpro (&QCdata);
21510 Qdisplay = intern ("display");
21511 staticpro (&Qdisplay);
21512 Qspace_width = intern ("space-width");
21513 staticpro (&Qspace_width);
21514 Qraise = intern ("raise");
21515 staticpro (&Qraise);
21516 Qspace = intern ("space");
21517 staticpro (&Qspace);
21518 Qmargin = intern ("margin");
21519 staticpro (&Qmargin);
21520 Qpointer = intern ("pointer");
21521 staticpro (&Qpointer);
21522 Qleft_margin = intern ("left-margin");
21523 staticpro (&Qleft_margin);
21524 Qright_margin = intern ("right-margin");
21525 staticpro (&Qright_margin);
21526 QCalign_to = intern (":align-to");
21527 staticpro (&QCalign_to);
21528 QCrelative_width = intern (":relative-width");
21529 staticpro (&QCrelative_width);
21530 QCrelative_height = intern (":relative-height");
21531 staticpro (&QCrelative_height);
21532 QCeval = intern (":eval");
21533 staticpro (&QCeval);
21534 QCpropertize = intern (":propertize");
21535 staticpro (&QCpropertize);
21536 QCfile = intern (":file");
21537 staticpro (&QCfile);
21538 Qfontified = intern ("fontified");
21539 staticpro (&Qfontified);
21540 Qfontification_functions = intern ("fontification-functions");
21541 staticpro (&Qfontification_functions);
21542 Qtrailing_whitespace = intern ("trailing-whitespace");
21543 staticpro (&Qtrailing_whitespace);
21544 Qimage = intern ("image");
21545 staticpro (&Qimage);
21546 QCmap = intern (":map");
21547 staticpro (&QCmap);
21548 QCpointer = intern (":pointer");
21549 staticpro (&QCpointer);
21550 Qrect = intern ("rect");
21551 staticpro (&Qrect);
21552 Qcircle = intern ("circle");
21553 staticpro (&Qcircle);
21554 Qpoly = intern ("poly");
21555 staticpro (&Qpoly);
21556 Qmessage_truncate_lines = intern ("message-truncate-lines");
21557 staticpro (&Qmessage_truncate_lines);
21558 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21559 staticpro (&Qcursor_in_non_selected_windows);
21560 Qgrow_only = intern ("grow-only");
21561 staticpro (&Qgrow_only);
21562 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21563 staticpro (&Qinhibit_menubar_update);
21564 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21565 staticpro (&Qinhibit_eval_during_redisplay);
21566 Qposition = intern ("position");
21567 staticpro (&Qposition);
21568 Qbuffer_position = intern ("buffer-position");
21569 staticpro (&Qbuffer_position);
21570 Qobject = intern ("object");
21571 staticpro (&Qobject);
21572 Qbar = intern ("bar");
21573 staticpro (&Qbar);
21574 Qhbar = intern ("hbar");
21575 staticpro (&Qhbar);
21576 Qbox = intern ("box");
21577 staticpro (&Qbox);
21578 Qhollow = intern ("hollow");
21579 staticpro (&Qhollow);
21580 Qhand = intern ("hand");
21581 staticpro (&Qhand);
21582 Qarrow = intern ("arrow");
21583 staticpro (&Qarrow);
21584 Qtext = intern ("text");
21585 staticpro (&Qtext);
21586 Qrisky_local_variable = intern ("risky-local-variable");
21587 staticpro (&Qrisky_local_variable);
21588 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21589 staticpro (&Qinhibit_free_realized_faces);
21590
21591 list_of_error = Fcons (intern ("error"), Qnil);
21592 staticpro (&list_of_error);
21593
21594 Qlast_arrow_position = intern ("last-arrow-position");
21595 staticpro (&Qlast_arrow_position);
21596 Qlast_arrow_string = intern ("last-arrow-string");
21597 staticpro (&Qlast_arrow_string);
21598
21599 Qoverlay_arrow_string = intern ("overlay-arrow-string");
21600 staticpro (&Qoverlay_arrow_string);
21601 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
21602 staticpro (&Qoverlay_arrow_bitmap);
21603
21604 echo_buffer[0] = echo_buffer[1] = Qnil;
21605 staticpro (&echo_buffer[0]);
21606 staticpro (&echo_buffer[1]);
21607
21608 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21609 staticpro (&echo_area_buffer[0]);
21610 staticpro (&echo_area_buffer[1]);
21611
21612 Vmessages_buffer_name = build_string ("*Messages*");
21613 staticpro (&Vmessages_buffer_name);
21614
21615 mode_line_proptrans_alist = Qnil;
21616 staticpro (&mode_line_proptrans_alist);
21617
21618 mode_line_string_list = Qnil;
21619 staticpro (&mode_line_string_list);
21620
21621 help_echo_string = Qnil;
21622 staticpro (&help_echo_string);
21623 help_echo_object = Qnil;
21624 staticpro (&help_echo_object);
21625 help_echo_window = Qnil;
21626 staticpro (&help_echo_window);
21627 previous_help_echo_string = Qnil;
21628 staticpro (&previous_help_echo_string);
21629 help_echo_pos = -1;
21630
21631 #ifdef HAVE_WINDOW_SYSTEM
21632 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
21633 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
21634 For example, if a block cursor is over a tab, it will be drawn as
21635 wide as that tab on the display. */);
21636 x_stretch_cursor_p = 0;
21637 #endif
21638
21639 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
21640 doc: /* *Non-nil means highlight trailing whitespace.
21641 The face used for trailing whitespace is `trailing-whitespace'. */);
21642 Vshow_trailing_whitespace = Qnil;
21643
21644 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
21645 doc: /* *The pointer shape to show in void text areas.
21646 Nil means to show the text pointer. Other options are `arrow', `text',
21647 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
21648 Vvoid_text_area_pointer = Qarrow;
21649
21650 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
21651 doc: /* Non-nil means don't actually do any redisplay.
21652 This is used for internal purposes. */);
21653 Vinhibit_redisplay = Qnil;
21654
21655 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
21656 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
21657 Vglobal_mode_string = Qnil;
21658
21659 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
21660 doc: /* Marker for where to display an arrow on top of the buffer text.
21661 This must be the beginning of a line in order to work.
21662 See also `overlay-arrow-string'. */);
21663 Voverlay_arrow_position = Qnil;
21664
21665 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
21666 doc: /* String to display as an arrow in non-window frames.
21667 See also `overlay-arrow-position'. */);
21668 Voverlay_arrow_string = Qnil;
21669
21670 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
21671 doc: /* List of variables (symbols) which hold markers for overlay arrows.
21672 The symbols on this list are examined during redisplay to determine
21673 where to display overlay arrows. */);
21674 Voverlay_arrow_variable_list
21675 = Fcons (intern ("overlay-arrow-position"), Qnil);
21676
21677 DEFVAR_INT ("scroll-step", &scroll_step,
21678 doc: /* *The number of lines to try scrolling a window by when point moves out.
21679 If that fails to bring point back on frame, point is centered instead.
21680 If this is zero, point is always centered after it moves off frame.
21681 If you want scrolling to always be a line at a time, you should set
21682 `scroll-conservatively' to a large value rather than set this to 1. */);
21683
21684 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
21685 doc: /* *Scroll up to this many lines, to bring point back on screen.
21686 A value of zero means to scroll the text to center point vertically
21687 in the window. */);
21688 scroll_conservatively = 0;
21689
21690 DEFVAR_INT ("scroll-margin", &scroll_margin,
21691 doc: /* *Number of lines of margin at the top and bottom of a window.
21692 Recenter the window whenever point gets within this many lines
21693 of the top or bottom of the window. */);
21694 scroll_margin = 0;
21695
21696 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
21697 doc: /* Pixels per inch on current display.
21698 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
21699 Vdisplay_pixels_per_inch = make_float (72.0);
21700
21701 #if GLYPH_DEBUG
21702 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
21703 #endif
21704
21705 DEFVAR_BOOL ("truncate-partial-width-windows",
21706 &truncate_partial_width_windows,
21707 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
21708 truncate_partial_width_windows = 1;
21709
21710 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
21711 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
21712 Any other value means to use the appropriate face, `mode-line',
21713 `header-line', or `menu' respectively. */);
21714 mode_line_inverse_video = 1;
21715
21716 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
21717 doc: /* *Maximum buffer size for which line number should be displayed.
21718 If the buffer is bigger than this, the line number does not appear
21719 in the mode line. A value of nil means no limit. */);
21720 Vline_number_display_limit = Qnil;
21721
21722 DEFVAR_INT ("line-number-display-limit-width",
21723 &line_number_display_limit_width,
21724 doc: /* *Maximum line width (in characters) for line number display.
21725 If the average length of the lines near point is bigger than this, then the
21726 line number may be omitted from the mode line. */);
21727 line_number_display_limit_width = 200;
21728
21729 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
21730 doc: /* *Non-nil means highlight region even in nonselected windows. */);
21731 highlight_nonselected_windows = 0;
21732
21733 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
21734 doc: /* Non-nil if more than one frame is visible on this display.
21735 Minibuffer-only frames don't count, but iconified frames do.
21736 This variable is not guaranteed to be accurate except while processing
21737 `frame-title-format' and `icon-title-format'. */);
21738
21739 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
21740 doc: /* Template for displaying the title bar of visible frames.
21741 \(Assuming the window manager supports this feature.)
21742 This variable has the same structure as `mode-line-format' (which see),
21743 and is used only on frames for which no explicit name has been set
21744 \(see `modify-frame-parameters'). */);
21745
21746 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
21747 doc: /* Template for displaying the title bar of an iconified frame.
21748 \(Assuming the window manager supports this feature.)
21749 This variable has the same structure as `mode-line-format' (which see),
21750 and is used only on frames for which no explicit name has been set
21751 \(see `modify-frame-parameters'). */);
21752 Vicon_title_format
21753 = Vframe_title_format
21754 = Fcons (intern ("multiple-frames"),
21755 Fcons (build_string ("%b"),
21756 Fcons (Fcons (empty_string,
21757 Fcons (intern ("invocation-name"),
21758 Fcons (build_string ("@"),
21759 Fcons (intern ("system-name"),
21760 Qnil)))),
21761 Qnil)));
21762
21763 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
21764 doc: /* Maximum number of lines to keep in the message log buffer.
21765 If nil, disable message logging. If t, log messages but don't truncate
21766 the buffer when it becomes large. */);
21767 Vmessage_log_max = make_number (50);
21768
21769 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
21770 doc: /* Functions called before redisplay, if window sizes have changed.
21771 The value should be a list of functions that take one argument.
21772 Just before redisplay, for each frame, if any of its windows have changed
21773 size since the last redisplay, or have been split or deleted,
21774 all the functions in the list are called, with the frame as argument. */);
21775 Vwindow_size_change_functions = Qnil;
21776
21777 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
21778 doc: /* List of Functions to call before redisplaying a window with scrolling.
21779 Each function is called with two arguments, the window
21780 and its new display-start position. Note that the value of `window-end'
21781 is not valid when these functions are called. */);
21782 Vwindow_scroll_functions = Qnil;
21783
21784 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
21785 doc: /* *Non-nil means autoselect window with mouse pointer. */);
21786 mouse_autoselect_window = 0;
21787
21788 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
21789 doc: /* *Non-nil means automatically resize tool-bars.
21790 This increases a tool-bar's height if not all tool-bar items are visible.
21791 It decreases a tool-bar's height when it would display blank lines
21792 otherwise. */);
21793 auto_resize_tool_bars_p = 1;
21794
21795 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21796 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
21797 auto_raise_tool_bar_buttons_p = 1;
21798
21799 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21800 doc: /* *Margin around tool-bar buttons in pixels.
21801 If an integer, use that for both horizontal and vertical margins.
21802 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
21803 HORZ specifying the horizontal margin, and VERT specifying the
21804 vertical margin. */);
21805 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
21806
21807 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
21808 doc: /* *Relief thickness of tool-bar buttons. */);
21809 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
21810
21811 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21812 doc: /* List of functions to call to fontify regions of text.
21813 Each function is called with one argument POS. Functions must
21814 fontify a region starting at POS in the current buffer, and give
21815 fontified regions the property `fontified'. */);
21816 Vfontification_functions = Qnil;
21817 Fmake_variable_buffer_local (Qfontification_functions);
21818
21819 DEFVAR_BOOL ("unibyte-display-via-language-environment",
21820 &unibyte_display_via_language_environment,
21821 doc: /* *Non-nil means display unibyte text according to language environment.
21822 Specifically this means that unibyte non-ASCII characters
21823 are displayed by converting them to the equivalent multibyte characters
21824 according to the current language environment. As a result, they are
21825 displayed according to the current fontset. */);
21826 unibyte_display_via_language_environment = 0;
21827
21828 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21829 doc: /* *Maximum height for resizing mini-windows.
21830 If a float, it specifies a fraction of the mini-window frame's height.
21831 If an integer, it specifies a number of lines. */);
21832 Vmax_mini_window_height = make_float (0.25);
21833
21834 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21835 doc: /* *How to resize mini-windows.
21836 A value of nil means don't automatically resize mini-windows.
21837 A value of t means resize them to fit the text displayed in them.
21838 A value of `grow-only', the default, means let mini-windows grow
21839 only, until their display becomes empty, at which point the windows
21840 go back to their normal size. */);
21841 Vresize_mini_windows = Qgrow_only;
21842
21843 DEFVAR_LISP ("cursor-in-non-selected-windows",
21844 &Vcursor_in_non_selected_windows,
21845 doc: /* *Cursor type to display in non-selected windows.
21846 t means to use hollow box cursor. See `cursor-type' for other values. */);
21847 Vcursor_in_non_selected_windows = Qt;
21848
21849 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21850 doc: /* Alist specifying how to blink the cursor off.
21851 Each element has the form (ON-STATE . OFF-STATE). Whenever the
21852 `cursor-type' frame-parameter or variable equals ON-STATE,
21853 comparing using `equal', Emacs uses OFF-STATE to specify
21854 how to blink it off. */);
21855 Vblink_cursor_alist = Qnil;
21856
21857 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
21858 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
21859 automatic_hscrolling_p = 1;
21860
21861 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
21862 doc: /* *How many columns away from the window edge point is allowed to get
21863 before automatic hscrolling will horizontally scroll the window. */);
21864 hscroll_margin = 5;
21865
21866 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
21867 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21868 When point is less than `automatic-hscroll-margin' columns from the window
21869 edge, automatic hscrolling will scroll the window by the amount of columns
21870 determined by this variable. If its value is a positive integer, scroll that
21871 many columns. If it's a positive floating-point number, it specifies the
21872 fraction of the window's width to scroll. If it's nil or zero, point will be
21873 centered horizontally after the scroll. Any other value, including negative
21874 numbers, are treated as if the value were zero.
21875
21876 Automatic hscrolling always moves point outside the scroll margin, so if
21877 point was more than scroll step columns inside the margin, the window will
21878 scroll more than the value given by the scroll step.
21879
21880 Note that the lower bound for automatic hscrolling specified by `scroll-left'
21881 and `scroll-right' overrides this variable's effect. */);
21882 Vhscroll_step = make_number (0);
21883
21884 DEFVAR_LISP ("image-types", &Vimage_types,
21885 doc: /* List of supported image types.
21886 Each element of the list is a symbol for a supported image type. */);
21887 Vimage_types = Qnil;
21888
21889 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21890 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
21891 Bind this around calls to `message' to let it take effect. */);
21892 message_truncate_lines = 0;
21893
21894 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21895 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
21896 Can be used to update submenus whose contents should vary. */);
21897 Vmenu_bar_update_hook = Qnil;
21898
21899 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21900 doc: /* Non-nil means don't update menu bars. Internal use only. */);
21901 inhibit_menubar_update = 0;
21902
21903 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21904 doc: /* Non-nil means don't eval Lisp during redisplay. */);
21905 inhibit_eval_during_redisplay = 0;
21906
21907 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21908 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21909 inhibit_free_realized_faces = 0;
21910
21911 #if GLYPH_DEBUG
21912 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21913 doc: /* Inhibit try_window_id display optimization. */);
21914 inhibit_try_window_id = 0;
21915
21916 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21917 doc: /* Inhibit try_window_reusing display optimization. */);
21918 inhibit_try_window_reusing = 0;
21919
21920 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21921 doc: /* Inhibit try_cursor_movement display optimization. */);
21922 inhibit_try_cursor_movement = 0;
21923 #endif /* GLYPH_DEBUG */
21924 }
21925
21926
21927 /* Initialize this module when Emacs starts. */
21928
21929 void
21930 init_xdisp ()
21931 {
21932 Lisp_Object root_window;
21933 struct window *mini_w;
21934
21935 current_header_line_height = current_mode_line_height = -1;
21936
21937 CHARPOS (this_line_start_pos) = 0;
21938
21939 mini_w = XWINDOW (minibuf_window);
21940 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21941
21942 if (!noninteractive)
21943 {
21944 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21945 int i;
21946
21947 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
21948 set_window_height (root_window,
21949 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
21950 0);
21951 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
21952 set_window_height (minibuf_window, 1, 0);
21953
21954 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21955 mini_w->total_cols = make_number (FRAME_COLS (f));
21956
21957 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21958 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21959 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21960
21961 /* The default ellipsis glyphs `...'. */
21962 for (i = 0; i < 3; ++i)
21963 default_invis_vector[i] = make_number ('.');
21964 }
21965
21966 {
21967 /* Allocate the buffer for frame titles.
21968 Also used for `format-mode-line'. */
21969 int size = 100;
21970 frame_title_buf = (char *) xmalloc (size);
21971 frame_title_buf_end = frame_title_buf + size;
21972 frame_title_ptr = NULL;
21973 }
21974
21975 help_echo_showing_p = 0;
21976 }
21977
21978
21979 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
21980 (do not change this comment) */