(Fw32_set_clipboard_data): Get sequence number after closing the clipboard.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201
202 Cursor No_Cursor;
203 #endif
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Margin around tool bar buttons in pixels. */
268
269 Lisp_Object Vtool_bar_button_margin;
270
271 /* Thickness of shadow to draw around tool bar buttons. */
272
273 EMACS_INT tool_bar_button_relief;
274
275 /* Non-zero means automatically resize tool-bars so that all tool-bar
276 items are visible, and no blank lines remain. */
277
278 int auto_resize_tool_bars_p;
279
280 /* Non-zero means draw block and hollow cursor as wide as the glyph
281 under it. For example, if a block cursor is over a tab, it will be
282 drawn as wide as that tab on the display. */
283
284 int x_stretch_cursor_p;
285
286 /* Non-nil means don't actually do any redisplay. */
287
288 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
289
290 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
291
292 int inhibit_eval_during_redisplay;
293
294 /* Names of text properties relevant for redisplay. */
295
296 Lisp_Object Qdisplay;
297 extern Lisp_Object Qface, Qinvisible, Qwidth;
298
299 /* Symbols used in text property values. */
300
301 Lisp_Object Vdisplay_pixels_per_inch;
302 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
303 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
304 Lisp_Object Qcenter;
305 Lisp_Object Qmargin, Qpointer;
306 extern Lisp_Object Qheight;
307 extern Lisp_Object QCwidth, QCheight, QCascent;
308 extern Lisp_Object Qscroll_bar;
309
310 /* Non-nil means highlight trailing whitespace. */
311
312 Lisp_Object Vshow_trailing_whitespace;
313
314 #ifdef HAVE_WINDOW_SYSTEM
315 extern Lisp_Object Voverflow_newline_into_fringe;
316
317 /* Test if overflow newline into fringe. Called with iterator IT
318 at or past right window margin, and with IT->current_x set. */
319
320 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
321 (!NILP (Voverflow_newline_into_fringe) \
322 && FRAME_WINDOW_P (it->f) \
323 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
324 && it->current_x == it->last_visible_x)
325
326 #endif /* HAVE_WINDOW_SYSTEM */
327
328 /* Non-nil means show the text cursor in void text areas
329 i.e. in blank areas after eol and eob. This used to be
330 the default in 21.3. */
331
332 Lisp_Object Vvoid_text_area_pointer;
333
334 /* Name of the face used to highlight trailing whitespace. */
335
336 Lisp_Object Qtrailing_whitespace;
337
338 /* The symbol `image' which is the car of the lists used to represent
339 images in Lisp. */
340
341 Lisp_Object Qimage;
342
343 /* The image map types. */
344 Lisp_Object QCmap, QCpointer;
345 Lisp_Object Qrect, Qcircle, Qpoly;
346
347 /* Non-zero means print newline to stdout before next mini-buffer
348 message. */
349
350 int noninteractive_need_newline;
351
352 /* Non-zero means print newline to message log before next message. */
353
354 static int message_log_need_newline;
355
356 /* Three markers that message_dolog uses.
357 It could allocate them itself, but that causes trouble
358 in handling memory-full errors. */
359 static Lisp_Object message_dolog_marker1;
360 static Lisp_Object message_dolog_marker2;
361 static Lisp_Object message_dolog_marker3;
362 \f
363 /* The buffer position of the first character appearing entirely or
364 partially on the line of the selected window which contains the
365 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
366 redisplay optimization in redisplay_internal. */
367
368 static struct text_pos this_line_start_pos;
369
370 /* Number of characters past the end of the line above, including the
371 terminating newline. */
372
373 static struct text_pos this_line_end_pos;
374
375 /* The vertical positions and the height of this line. */
376
377 static int this_line_vpos;
378 static int this_line_y;
379 static int this_line_pixel_height;
380
381 /* X position at which this display line starts. Usually zero;
382 negative if first character is partially visible. */
383
384 static int this_line_start_x;
385
386 /* Buffer that this_line_.* variables are referring to. */
387
388 static struct buffer *this_line_buffer;
389
390 /* Nonzero means truncate lines in all windows less wide than the
391 frame. */
392
393 int truncate_partial_width_windows;
394
395 /* A flag to control how to display unibyte 8-bit character. */
396
397 int unibyte_display_via_language_environment;
398
399 /* Nonzero means we have more than one non-mini-buffer-only frame.
400 Not guaranteed to be accurate except while parsing
401 frame-title-format. */
402
403 int multiple_frames;
404
405 Lisp_Object Vglobal_mode_string;
406
407
408 /* List of variables (symbols) which hold markers for overlay arrows.
409 The symbols on this list are examined during redisplay to determine
410 where to display overlay arrows. */
411
412 Lisp_Object Voverlay_arrow_variable_list;
413
414 /* Marker for where to display an arrow on top of the buffer text. */
415
416 Lisp_Object Voverlay_arrow_position;
417
418 /* String to display for the arrow. Only used on terminal frames. */
419
420 Lisp_Object Voverlay_arrow_string;
421
422 /* Values of those variables at last redisplay are stored as
423 properties on `overlay-arrow-position' symbol. However, if
424 Voverlay_arrow_position is a marker, last-arrow-position is its
425 numerical position. */
426
427 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
428
429 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
430 properties on a symbol in overlay-arrow-variable-list. */
431
432 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
433
434 /* Like mode-line-format, but for the title bar on a visible frame. */
435
436 Lisp_Object Vframe_title_format;
437
438 /* Like mode-line-format, but for the title bar on an iconified frame. */
439
440 Lisp_Object Vicon_title_format;
441
442 /* List of functions to call when a window's size changes. These
443 functions get one arg, a frame on which one or more windows' sizes
444 have changed. */
445
446 static Lisp_Object Vwindow_size_change_functions;
447
448 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
449
450 /* Nonzero if overlay arrow has been displayed once in this window. */
451
452 static int overlay_arrow_seen;
453
454 /* Nonzero means highlight the region even in nonselected windows. */
455
456 int highlight_nonselected_windows;
457
458 /* If cursor motion alone moves point off frame, try scrolling this
459 many lines up or down if that will bring it back. */
460
461 static EMACS_INT scroll_step;
462
463 /* Nonzero means scroll just far enough to bring point back on the
464 screen, when appropriate. */
465
466 static EMACS_INT scroll_conservatively;
467
468 /* Recenter the window whenever point gets within this many lines of
469 the top or bottom of the window. This value is translated into a
470 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
471 that there is really a fixed pixel height scroll margin. */
472
473 EMACS_INT scroll_margin;
474
475 /* Number of windows showing the buffer of the selected window (or
476 another buffer with the same base buffer). keyboard.c refers to
477 this. */
478
479 int buffer_shared;
480
481 /* Vector containing glyphs for an ellipsis `...'. */
482
483 static Lisp_Object default_invis_vector[3];
484
485 /* Zero means display the mode-line/header-line/menu-bar in the default face
486 (this slightly odd definition is for compatibility with previous versions
487 of emacs), non-zero means display them using their respective faces.
488
489 This variable is deprecated. */
490
491 int mode_line_inverse_video;
492
493 /* Prompt to display in front of the mini-buffer contents. */
494
495 Lisp_Object minibuf_prompt;
496
497 /* Width of current mini-buffer prompt. Only set after display_line
498 of the line that contains the prompt. */
499
500 int minibuf_prompt_width;
501
502 /* This is the window where the echo area message was displayed. It
503 is always a mini-buffer window, but it may not be the same window
504 currently active as a mini-buffer. */
505
506 Lisp_Object echo_area_window;
507
508 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
509 pushes the current message and the value of
510 message_enable_multibyte on the stack, the function restore_message
511 pops the stack and displays MESSAGE again. */
512
513 Lisp_Object Vmessage_stack;
514
515 /* Nonzero means multibyte characters were enabled when the echo area
516 message was specified. */
517
518 int message_enable_multibyte;
519
520 /* Nonzero if we should redraw the mode lines on the next redisplay. */
521
522 int update_mode_lines;
523
524 /* Nonzero if window sizes or contents have changed since last
525 redisplay that finished. */
526
527 int windows_or_buffers_changed;
528
529 /* Nonzero means a frame's cursor type has been changed. */
530
531 int cursor_type_changed;
532
533 /* Nonzero after display_mode_line if %l was used and it displayed a
534 line number. */
535
536 int line_number_displayed;
537
538 /* Maximum buffer size for which to display line numbers. */
539
540 Lisp_Object Vline_number_display_limit;
541
542 /* Line width to consider when repositioning for line number display. */
543
544 static EMACS_INT line_number_display_limit_width;
545
546 /* Number of lines to keep in the message log buffer. t means
547 infinite. nil means don't log at all. */
548
549 Lisp_Object Vmessage_log_max;
550
551 /* The name of the *Messages* buffer, a string. */
552
553 static Lisp_Object Vmessages_buffer_name;
554
555 /* Current, index 0, and last displayed echo area message. Either
556 buffers from echo_buffers, or nil to indicate no message. */
557
558 Lisp_Object echo_area_buffer[2];
559
560 /* The buffers referenced from echo_area_buffer. */
561
562 static Lisp_Object echo_buffer[2];
563
564 /* A vector saved used in with_area_buffer to reduce consing. */
565
566 static Lisp_Object Vwith_echo_area_save_vector;
567
568 /* Non-zero means display_echo_area should display the last echo area
569 message again. Set by redisplay_preserve_echo_area. */
570
571 static int display_last_displayed_message_p;
572
573 /* Nonzero if echo area is being used by print; zero if being used by
574 message. */
575
576 int message_buf_print;
577
578 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
579
580 Lisp_Object Qinhibit_menubar_update;
581 int inhibit_menubar_update;
582
583 /* Maximum height for resizing mini-windows. Either a float
584 specifying a fraction of the available height, or an integer
585 specifying a number of lines. */
586
587 Lisp_Object Vmax_mini_window_height;
588
589 /* Non-zero means messages should be displayed with truncated
590 lines instead of being continued. */
591
592 int message_truncate_lines;
593 Lisp_Object Qmessage_truncate_lines;
594
595 /* Set to 1 in clear_message to make redisplay_internal aware
596 of an emptied echo area. */
597
598 static int message_cleared_p;
599
600 /* Non-zero means we want a hollow cursor in windows that are not
601 selected. Zero means there's no cursor in such windows. */
602
603 Lisp_Object Vcursor_in_non_selected_windows;
604 Lisp_Object Qcursor_in_non_selected_windows;
605
606 /* How to blink the default frame cursor off. */
607 Lisp_Object Vblink_cursor_alist;
608
609 /* A scratch glyph row with contents used for generating truncation
610 glyphs. Also used in direct_output_for_insert. */
611
612 #define MAX_SCRATCH_GLYPHS 100
613 struct glyph_row scratch_glyph_row;
614 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
615
616 /* Ascent and height of the last line processed by move_it_to. */
617
618 static int last_max_ascent, last_height;
619
620 /* Non-zero if there's a help-echo in the echo area. */
621
622 int help_echo_showing_p;
623
624 /* If >= 0, computed, exact values of mode-line and header-line height
625 to use in the macros CURRENT_MODE_LINE_HEIGHT and
626 CURRENT_HEADER_LINE_HEIGHT. */
627
628 int current_mode_line_height, current_header_line_height;
629
630 /* The maximum distance to look ahead for text properties. Values
631 that are too small let us call compute_char_face and similar
632 functions too often which is expensive. Values that are too large
633 let us call compute_char_face and alike too often because we
634 might not be interested in text properties that far away. */
635
636 #define TEXT_PROP_DISTANCE_LIMIT 100
637
638 #if GLYPH_DEBUG
639
640 /* Variables to turn off display optimizations from Lisp. */
641
642 int inhibit_try_window_id, inhibit_try_window_reusing;
643 int inhibit_try_cursor_movement;
644
645 /* Non-zero means print traces of redisplay if compiled with
646 GLYPH_DEBUG != 0. */
647
648 int trace_redisplay_p;
649
650 #endif /* GLYPH_DEBUG */
651
652 #ifdef DEBUG_TRACE_MOVE
653 /* Non-zero means trace with TRACE_MOVE to stderr. */
654 int trace_move;
655
656 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
657 #else
658 #define TRACE_MOVE(x) (void) 0
659 #endif
660
661 /* Non-zero means automatically scroll windows horizontally to make
662 point visible. */
663
664 int automatic_hscrolling_p;
665
666 /* How close to the margin can point get before the window is scrolled
667 horizontally. */
668 EMACS_INT hscroll_margin;
669
670 /* How much to scroll horizontally when point is inside the above margin. */
671 Lisp_Object Vhscroll_step;
672
673 /* A list of symbols, one for each supported image type. */
674
675 Lisp_Object Vimage_types;
676
677 /* The variable `resize-mini-windows'. If nil, don't resize
678 mini-windows. If t, always resize them to fit the text they
679 display. If `grow-only', let mini-windows grow only until they
680 become empty. */
681
682 Lisp_Object Vresize_mini_windows;
683
684 /* Buffer being redisplayed -- for redisplay_window_error. */
685
686 struct buffer *displayed_buffer;
687
688 /* Value returned from text property handlers (see below). */
689
690 enum prop_handled
691 {
692 HANDLED_NORMALLY,
693 HANDLED_RECOMPUTE_PROPS,
694 HANDLED_OVERLAY_STRING_CONSUMED,
695 HANDLED_RETURN
696 };
697
698 /* A description of text properties that redisplay is interested
699 in. */
700
701 struct props
702 {
703 /* The name of the property. */
704 Lisp_Object *name;
705
706 /* A unique index for the property. */
707 enum prop_idx idx;
708
709 /* A handler function called to set up iterator IT from the property
710 at IT's current position. Value is used to steer handle_stop. */
711 enum prop_handled (*handler) P_ ((struct it *it));
712 };
713
714 static enum prop_handled handle_face_prop P_ ((struct it *));
715 static enum prop_handled handle_invisible_prop P_ ((struct it *));
716 static enum prop_handled handle_display_prop P_ ((struct it *));
717 static enum prop_handled handle_composition_prop P_ ((struct it *));
718 static enum prop_handled handle_overlay_change P_ ((struct it *));
719 static enum prop_handled handle_fontified_prop P_ ((struct it *));
720
721 /* Properties handled by iterators. */
722
723 static struct props it_props[] =
724 {
725 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
726 /* Handle `face' before `display' because some sub-properties of
727 `display' need to know the face. */
728 {&Qface, FACE_PROP_IDX, handle_face_prop},
729 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
730 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
731 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
732 {NULL, 0, NULL}
733 };
734
735 /* Value is the position described by X. If X is a marker, value is
736 the marker_position of X. Otherwise, value is X. */
737
738 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
739
740 /* Enumeration returned by some move_it_.* functions internally. */
741
742 enum move_it_result
743 {
744 /* Not used. Undefined value. */
745 MOVE_UNDEFINED,
746
747 /* Move ended at the requested buffer position or ZV. */
748 MOVE_POS_MATCH_OR_ZV,
749
750 /* Move ended at the requested X pixel position. */
751 MOVE_X_REACHED,
752
753 /* Move within a line ended at the end of a line that must be
754 continued. */
755 MOVE_LINE_CONTINUED,
756
757 /* Move within a line ended at the end of a line that would
758 be displayed truncated. */
759 MOVE_LINE_TRUNCATED,
760
761 /* Move within a line ended at a line end. */
762 MOVE_NEWLINE_OR_CR
763 };
764
765 /* This counter is used to clear the face cache every once in a while
766 in redisplay_internal. It is incremented for each redisplay.
767 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
768 cleared. */
769
770 #define CLEAR_FACE_CACHE_COUNT 500
771 static int clear_face_cache_count;
772
773 /* Record the previous terminal frame we displayed. */
774
775 static struct frame *previous_terminal_frame;
776
777 /* Non-zero while redisplay_internal is in progress. */
778
779 int redisplaying_p;
780
781 /* Non-zero means don't free realized faces. Bound while freeing
782 realized faces is dangerous because glyph matrices might still
783 reference them. */
784
785 int inhibit_free_realized_faces;
786 Lisp_Object Qinhibit_free_realized_faces;
787
788 /* If a string, XTread_socket generates an event to display that string.
789 (The display is done in read_char.) */
790
791 Lisp_Object help_echo_string;
792 Lisp_Object help_echo_window;
793 Lisp_Object help_echo_object;
794 int help_echo_pos;
795
796 /* Temporary variable for XTread_socket. */
797
798 Lisp_Object previous_help_echo_string;
799
800
801 \f
802 /* Function prototypes. */
803
804 static void setup_for_ellipsis P_ ((struct it *));
805 static void mark_window_display_accurate_1 P_ ((struct window *, int));
806 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
807 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
808 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
809 static int redisplay_mode_lines P_ ((Lisp_Object, int));
810 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
811
812 #if 0
813 static int invisible_text_between_p P_ ((struct it *, int, int));
814 #endif
815
816 static int next_element_from_ellipsis P_ ((struct it *));
817 static void pint2str P_ ((char *, int, int));
818 static void pint2hrstr P_ ((char *, int, int));
819 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
820 struct text_pos));
821 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
822 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
823 static void store_frame_title_char P_ ((char));
824 static int store_frame_title P_ ((const unsigned char *, int, int));
825 static void x_consider_frame_title P_ ((Lisp_Object));
826 static void handle_stop P_ ((struct it *));
827 static int tool_bar_lines_needed P_ ((struct frame *));
828 static int single_display_prop_intangible_p P_ ((Lisp_Object));
829 static void ensure_echo_area_buffers P_ ((void));
830 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
831 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
832 static int with_echo_area_buffer P_ ((struct window *, int,
833 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
834 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
835 static void clear_garbaged_frames P_ ((void));
836 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
838 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
839 static int display_echo_area P_ ((struct window *));
840 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
842 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
843 static int string_char_and_length P_ ((const unsigned char *, int, int *));
844 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
845 struct text_pos));
846 static int compute_window_start_on_continuation_line P_ ((struct window *));
847 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
848 static void insert_left_trunc_glyphs P_ ((struct it *));
849 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
850 Lisp_Object));
851 static void extend_face_to_end_of_line P_ ((struct it *));
852 static int append_space P_ ((struct it *, int));
853 static int make_cursor_line_fully_visible P_ ((struct window *, int));
854 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
855 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
856 static int trailing_whitespace_p P_ ((int));
857 static int message_log_check_duplicate P_ ((int, int, int, int));
858 static void push_it P_ ((struct it *));
859 static void pop_it P_ ((struct it *));
860 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
861 static void select_frame_for_redisplay P_ ((Lisp_Object));
862 static void redisplay_internal P_ ((int));
863 static int echo_area_display P_ ((int));
864 static void redisplay_windows P_ ((Lisp_Object));
865 static void redisplay_window P_ ((Lisp_Object, int));
866 static Lisp_Object redisplay_window_error ();
867 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
868 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
869 static void update_menu_bar P_ ((struct frame *, int));
870 static int try_window_reusing_current_matrix P_ ((struct window *));
871 static int try_window_id P_ ((struct window *));
872 static int display_line P_ ((struct it *));
873 static int display_mode_lines P_ ((struct window *));
874 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
875 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
876 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
877 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
878 static void display_menu_bar P_ ((struct window *));
879 static int display_count_lines P_ ((int, int, int, int, int *));
880 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
881 int, int, struct it *, int, int, int, int));
882 static void compute_line_metrics P_ ((struct it *));
883 static void run_redisplay_end_trigger_hook P_ ((struct it *));
884 static int get_overlay_strings P_ ((struct it *, int));
885 static void next_overlay_string P_ ((struct it *));
886 static void reseat P_ ((struct it *, struct text_pos, int));
887 static void reseat_1 P_ ((struct it *, struct text_pos, int));
888 static void back_to_previous_visible_line_start P_ ((struct it *));
889 static void reseat_at_previous_visible_line_start P_ ((struct it *));
890 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
891 static int next_element_from_display_vector P_ ((struct it *));
892 static int next_element_from_string P_ ((struct it *));
893 static int next_element_from_c_string P_ ((struct it *));
894 static int next_element_from_buffer P_ ((struct it *));
895 static int next_element_from_composition P_ ((struct it *));
896 static int next_element_from_image P_ ((struct it *));
897 static int next_element_from_stretch P_ ((struct it *));
898 static void load_overlay_strings P_ ((struct it *, int));
899 static int init_from_display_pos P_ ((struct it *, struct window *,
900 struct display_pos *));
901 static void reseat_to_string P_ ((struct it *, unsigned char *,
902 Lisp_Object, int, int, int, int));
903 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
904 int, int, int));
905 void move_it_vertically_backward P_ ((struct it *, int));
906 static void init_to_row_start P_ ((struct it *, struct window *,
907 struct glyph_row *));
908 static int init_to_row_end P_ ((struct it *, struct window *,
909 struct glyph_row *));
910 static void back_to_previous_line_start P_ ((struct it *));
911 static int forward_to_next_line_start P_ ((struct it *, int *));
912 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
913 Lisp_Object, int));
914 static struct text_pos string_pos P_ ((int, Lisp_Object));
915 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
916 static int number_of_chars P_ ((unsigned char *, int));
917 static void compute_stop_pos P_ ((struct it *));
918 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
919 Lisp_Object));
920 static int face_before_or_after_it_pos P_ ((struct it *, int));
921 static int next_overlay_change P_ ((int));
922 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
923 Lisp_Object, struct text_pos *,
924 int));
925 static int underlying_face_id P_ ((struct it *));
926 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
927 struct window *));
928
929 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
930 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
931
932 #ifdef HAVE_WINDOW_SYSTEM
933
934 static void update_tool_bar P_ ((struct frame *, int));
935 static void build_desired_tool_bar_string P_ ((struct frame *f));
936 static int redisplay_tool_bar P_ ((struct frame *));
937 static void display_tool_bar_line P_ ((struct it *));
938 static void notice_overwritten_cursor P_ ((struct window *,
939 enum glyph_row_area,
940 int, int, int, int));
941
942
943
944 #endif /* HAVE_WINDOW_SYSTEM */
945
946 \f
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
950
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
956
957 INLINE int
958 window_text_bottom_y (w)
959 struct window *w;
960 {
961 int height = WINDOW_TOTAL_HEIGHT (w);
962
963 if (WINDOW_WANTS_MODELINE_P (w))
964 height -= CURRENT_MODE_LINE_HEIGHT (w);
965 return height;
966 }
967
968 /* Return the pixel width of display area AREA of window W. AREA < 0
969 means return the total width of W, not including fringes to
970 the left and right of the window. */
971
972 INLINE int
973 window_box_width (w, area)
974 struct window *w;
975 int area;
976 {
977 int cols = XFASTINT (w->total_cols);
978 int pixels = 0;
979
980 if (!w->pseudo_window_p)
981 {
982 cols -= WINDOW_SCROLL_BAR_COLS (w);
983
984 if (area == TEXT_AREA)
985 {
986 if (INTEGERP (w->left_margin_cols))
987 cols -= XFASTINT (w->left_margin_cols);
988 if (INTEGERP (w->right_margin_cols))
989 cols -= XFASTINT (w->right_margin_cols);
990 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
991 }
992 else if (area == LEFT_MARGIN_AREA)
993 {
994 cols = (INTEGERP (w->left_margin_cols)
995 ? XFASTINT (w->left_margin_cols) : 0);
996 pixels = 0;
997 }
998 else if (area == RIGHT_MARGIN_AREA)
999 {
1000 cols = (INTEGERP (w->right_margin_cols)
1001 ? XFASTINT (w->right_margin_cols) : 0);
1002 pixels = 0;
1003 }
1004 }
1005
1006 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1007 }
1008
1009
1010 /* Return the pixel height of the display area of window W, not
1011 including mode lines of W, if any. */
1012
1013 INLINE int
1014 window_box_height (w)
1015 struct window *w;
1016 {
1017 struct frame *f = XFRAME (w->frame);
1018 int height = WINDOW_TOTAL_HEIGHT (w);
1019
1020 xassert (height >= 0);
1021
1022 /* Note: the code below that determines the mode-line/header-line
1023 height is essentially the same as that contained in the macro
1024 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1025 the appropriate glyph row has its `mode_line_p' flag set,
1026 and if it doesn't, uses estimate_mode_line_height instead. */
1027
1028 if (WINDOW_WANTS_MODELINE_P (w))
1029 {
1030 struct glyph_row *ml_row
1031 = (w->current_matrix && w->current_matrix->rows
1032 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1033 : 0);
1034 if (ml_row && ml_row->mode_line_p)
1035 height -= ml_row->height;
1036 else
1037 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1038 }
1039
1040 if (WINDOW_WANTS_HEADER_LINE_P (w))
1041 {
1042 struct glyph_row *hl_row
1043 = (w->current_matrix && w->current_matrix->rows
1044 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1045 : 0);
1046 if (hl_row && hl_row->mode_line_p)
1047 height -= hl_row->height;
1048 else
1049 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1050 }
1051
1052 /* With a very small font and a mode-line that's taller than
1053 default, we might end up with a negative height. */
1054 return max (0, height);
1055 }
1056
1057 /* Return the window-relative coordinate of the left edge of display
1058 area AREA of window W. AREA < 0 means return the left edge of the
1059 whole window, to the right of the left fringe of W. */
1060
1061 INLINE int
1062 window_box_left_offset (w, area)
1063 struct window *w;
1064 int area;
1065 {
1066 int x;
1067
1068 if (w->pseudo_window_p)
1069 return 0;
1070
1071 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1072
1073 if (area == TEXT_AREA)
1074 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1075 + window_box_width (w, LEFT_MARGIN_AREA));
1076 else if (area == RIGHT_MARGIN_AREA)
1077 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1078 + window_box_width (w, LEFT_MARGIN_AREA)
1079 + window_box_width (w, TEXT_AREA)
1080 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1081 ? 0
1082 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1083 else if (area == LEFT_MARGIN_AREA
1084 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1085 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1086
1087 return x;
1088 }
1089
1090
1091 /* Return the window-relative coordinate of the right edge of display
1092 area AREA of window W. AREA < 0 means return the left edge of the
1093 whole window, to the left of the right fringe of W. */
1094
1095 INLINE int
1096 window_box_right_offset (w, area)
1097 struct window *w;
1098 int area;
1099 {
1100 return window_box_left_offset (w, area) + window_box_width (w, area);
1101 }
1102
1103 /* Return the frame-relative coordinate of the left edge of display
1104 area AREA of window W. AREA < 0 means return the left edge of the
1105 whole window, to the right of the left fringe of W. */
1106
1107 INLINE int
1108 window_box_left (w, area)
1109 struct window *w;
1110 int area;
1111 {
1112 struct frame *f = XFRAME (w->frame);
1113 int x;
1114
1115 if (w->pseudo_window_p)
1116 return FRAME_INTERNAL_BORDER_WIDTH (f);
1117
1118 x = (WINDOW_LEFT_EDGE_X (w)
1119 + window_box_left_offset (w, area));
1120
1121 return x;
1122 }
1123
1124
1125 /* Return the frame-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the left of the right fringe of W. */
1128
1129 INLINE int
1130 window_box_right (w, area)
1131 struct window *w;
1132 int area;
1133 {
1134 return window_box_left (w, area) + window_box_width (w, area);
1135 }
1136
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1143
1144 INLINE void
1145 window_box (w, area, box_x, box_y, box_width, box_height)
1146 struct window *w;
1147 int area;
1148 int *box_x, *box_y, *box_width, *box_height;
1149 {
1150 if (box_width)
1151 *box_width = window_box_width (w, area);
1152 if (box_height)
1153 *box_height = window_box_height (w);
1154 if (box_x)
1155 *box_x = window_box_left (w, area);
1156 if (box_y)
1157 {
1158 *box_y = WINDOW_TOP_EDGE_Y (w);
1159 if (WINDOW_WANTS_HEADER_LINE_P (w))
1160 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1161 }
1162 }
1163
1164
1165 /* Get the bounding box of the display area AREA of window W, without
1166 mode lines. AREA < 0 means the whole window, not including the
1167 left and right fringe of the window. Return in *TOP_LEFT_X
1168 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1169 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1170 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1171 box. */
1172
1173 INLINE void
1174 window_box_edges (w, area, top_left_x, top_left_y,
1175 bottom_right_x, bottom_right_y)
1176 struct window *w;
1177 int area;
1178 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1179 {
1180 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1181 bottom_right_y);
1182 *bottom_right_x += *top_left_x;
1183 *bottom_right_y += *top_left_y;
1184 }
1185
1186
1187 \f
1188 /***********************************************************************
1189 Utilities
1190 ***********************************************************************/
1191
1192 /* Return the bottom y-position of the line the iterator IT is in.
1193 This can modify IT's settings. */
1194
1195 int
1196 line_bottom_y (it)
1197 struct it *it;
1198 {
1199 int line_height = it->max_ascent + it->max_descent;
1200 int line_top_y = it->current_y;
1201
1202 if (line_height == 0)
1203 {
1204 if (last_height)
1205 line_height = last_height;
1206 else if (IT_CHARPOS (*it) < ZV)
1207 {
1208 move_it_by_lines (it, 1, 1);
1209 line_height = (it->max_ascent || it->max_descent
1210 ? it->max_ascent + it->max_descent
1211 : last_height);
1212 }
1213 else
1214 {
1215 struct glyph_row *row = it->glyph_row;
1216
1217 /* Use the default character height. */
1218 it->glyph_row = NULL;
1219 it->what = IT_CHARACTER;
1220 it->c = ' ';
1221 it->len = 1;
1222 PRODUCE_GLYPHS (it);
1223 line_height = it->ascent + it->descent;
1224 it->glyph_row = row;
1225 }
1226 }
1227
1228 return line_top_y + line_height;
1229 }
1230
1231
1232 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1233 1 if POS is visible and the line containing POS is fully visible.
1234 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1235 and header-lines heights. */
1236
1237 int
1238 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1239 struct window *w;
1240 int charpos, *fully, exact_mode_line_heights_p;
1241 {
1242 struct it it;
1243 struct text_pos top;
1244 int visible_p;
1245 struct buffer *old_buffer = NULL;
1246
1247 if (XBUFFER (w->buffer) != current_buffer)
1248 {
1249 old_buffer = current_buffer;
1250 set_buffer_internal_1 (XBUFFER (w->buffer));
1251 }
1252
1253 *fully = visible_p = 0;
1254 SET_TEXT_POS_FROM_MARKER (top, w->start);
1255
1256 /* Compute exact mode line heights, if requested. */
1257 if (exact_mode_line_heights_p)
1258 {
1259 if (WINDOW_WANTS_MODELINE_P (w))
1260 current_mode_line_height
1261 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1262 current_buffer->mode_line_format);
1263
1264 if (WINDOW_WANTS_HEADER_LINE_P (w))
1265 current_header_line_height
1266 = display_mode_line (w, HEADER_LINE_FACE_ID,
1267 current_buffer->header_line_format);
1268 }
1269
1270 start_display (&it, w, top);
1271 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1272 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1273
1274 /* Note that we may overshoot because of invisible text. */
1275 if (IT_CHARPOS (it) >= charpos)
1276 {
1277 int top_y = it.current_y;
1278 int bottom_y = line_bottom_y (&it);
1279 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1280
1281 if (top_y < window_top_y)
1282 visible_p = bottom_y > window_top_y;
1283 else if (top_y < it.last_visible_y)
1284 {
1285 visible_p = 1;
1286 *fully = bottom_y <= it.last_visible_y;
1287 }
1288 }
1289 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1290 {
1291 move_it_by_lines (&it, 1, 0);
1292 if (charpos < IT_CHARPOS (it))
1293 {
1294 visible_p = 1;
1295 *fully = 0;
1296 }
1297 }
1298
1299 if (old_buffer)
1300 set_buffer_internal_1 (old_buffer);
1301
1302 current_header_line_height = current_mode_line_height = -1;
1303 return visible_p;
1304 }
1305
1306
1307 /* Return the next character from STR which is MAXLEN bytes long.
1308 Return in *LEN the length of the character. This is like
1309 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1310 we find one, we return a `?', but with the length of the invalid
1311 character. */
1312
1313 static INLINE int
1314 string_char_and_length (str, maxlen, len)
1315 const unsigned char *str;
1316 int maxlen, *len;
1317 {
1318 int c;
1319
1320 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1321 if (!CHAR_VALID_P (c, 1))
1322 /* We may not change the length here because other places in Emacs
1323 don't use this function, i.e. they silently accept invalid
1324 characters. */
1325 c = '?';
1326
1327 return c;
1328 }
1329
1330
1331
1332 /* Given a position POS containing a valid character and byte position
1333 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1334
1335 static struct text_pos
1336 string_pos_nchars_ahead (pos, string, nchars)
1337 struct text_pos pos;
1338 Lisp_Object string;
1339 int nchars;
1340 {
1341 xassert (STRINGP (string) && nchars >= 0);
1342
1343 if (STRING_MULTIBYTE (string))
1344 {
1345 int rest = SBYTES (string) - BYTEPOS (pos);
1346 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1347 int len;
1348
1349 while (nchars--)
1350 {
1351 string_char_and_length (p, rest, &len);
1352 p += len, rest -= len;
1353 xassert (rest >= 0);
1354 CHARPOS (pos) += 1;
1355 BYTEPOS (pos) += len;
1356 }
1357 }
1358 else
1359 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1360
1361 return pos;
1362 }
1363
1364
1365 /* Value is the text position, i.e. character and byte position,
1366 for character position CHARPOS in STRING. */
1367
1368 static INLINE struct text_pos
1369 string_pos (charpos, string)
1370 int charpos;
1371 Lisp_Object string;
1372 {
1373 struct text_pos pos;
1374 xassert (STRINGP (string));
1375 xassert (charpos >= 0);
1376 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1377 return pos;
1378 }
1379
1380
1381 /* Value is a text position, i.e. character and byte position, for
1382 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1383 means recognize multibyte characters. */
1384
1385 static struct text_pos
1386 c_string_pos (charpos, s, multibyte_p)
1387 int charpos;
1388 unsigned char *s;
1389 int multibyte_p;
1390 {
1391 struct text_pos pos;
1392
1393 xassert (s != NULL);
1394 xassert (charpos >= 0);
1395
1396 if (multibyte_p)
1397 {
1398 int rest = strlen (s), len;
1399
1400 SET_TEXT_POS (pos, 0, 0);
1401 while (charpos--)
1402 {
1403 string_char_and_length (s, rest, &len);
1404 s += len, rest -= len;
1405 xassert (rest >= 0);
1406 CHARPOS (pos) += 1;
1407 BYTEPOS (pos) += len;
1408 }
1409 }
1410 else
1411 SET_TEXT_POS (pos, charpos, charpos);
1412
1413 return pos;
1414 }
1415
1416
1417 /* Value is the number of characters in C string S. MULTIBYTE_P
1418 non-zero means recognize multibyte characters. */
1419
1420 static int
1421 number_of_chars (s, multibyte_p)
1422 unsigned char *s;
1423 int multibyte_p;
1424 {
1425 int nchars;
1426
1427 if (multibyte_p)
1428 {
1429 int rest = strlen (s), len;
1430 unsigned char *p = (unsigned char *) s;
1431
1432 for (nchars = 0; rest > 0; ++nchars)
1433 {
1434 string_char_and_length (p, rest, &len);
1435 rest -= len, p += len;
1436 }
1437 }
1438 else
1439 nchars = strlen (s);
1440
1441 return nchars;
1442 }
1443
1444
1445 /* Compute byte position NEWPOS->bytepos corresponding to
1446 NEWPOS->charpos. POS is a known position in string STRING.
1447 NEWPOS->charpos must be >= POS.charpos. */
1448
1449 static void
1450 compute_string_pos (newpos, pos, string)
1451 struct text_pos *newpos, pos;
1452 Lisp_Object string;
1453 {
1454 xassert (STRINGP (string));
1455 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1456
1457 if (STRING_MULTIBYTE (string))
1458 *newpos = string_pos_nchars_ahead (pos, string,
1459 CHARPOS (*newpos) - CHARPOS (pos));
1460 else
1461 BYTEPOS (*newpos) = CHARPOS (*newpos);
1462 }
1463
1464 /* EXPORT:
1465 Return an estimation of the pixel height of mode or top lines on
1466 frame F. FACE_ID specifies what line's height to estimate. */
1467
1468 int
1469 estimate_mode_line_height (f, face_id)
1470 struct frame *f;
1471 enum face_id face_id;
1472 {
1473 #ifdef HAVE_WINDOW_SYSTEM
1474 if (FRAME_WINDOW_P (f))
1475 {
1476 int height = FONT_HEIGHT (FRAME_FONT (f));
1477
1478 /* This function is called so early when Emacs starts that the face
1479 cache and mode line face are not yet initialized. */
1480 if (FRAME_FACE_CACHE (f))
1481 {
1482 struct face *face = FACE_FROM_ID (f, face_id);
1483 if (face)
1484 {
1485 if (face->font)
1486 height = FONT_HEIGHT (face->font);
1487 if (face->box_line_width > 0)
1488 height += 2 * face->box_line_width;
1489 }
1490 }
1491
1492 return height;
1493 }
1494 #endif
1495
1496 return 1;
1497 }
1498
1499 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1500 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1501 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1502 not force the value into range. */
1503
1504 void
1505 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1506 FRAME_PTR f;
1507 register int pix_x, pix_y;
1508 int *x, *y;
1509 NativeRectangle *bounds;
1510 int noclip;
1511 {
1512
1513 #ifdef HAVE_WINDOW_SYSTEM
1514 if (FRAME_WINDOW_P (f))
1515 {
1516 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1517 even for negative values. */
1518 if (pix_x < 0)
1519 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1520 if (pix_y < 0)
1521 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1522
1523 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1524 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1525
1526 if (bounds)
1527 STORE_NATIVE_RECT (*bounds,
1528 FRAME_COL_TO_PIXEL_X (f, pix_x),
1529 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1530 FRAME_COLUMN_WIDTH (f) - 1,
1531 FRAME_LINE_HEIGHT (f) - 1);
1532
1533 if (!noclip)
1534 {
1535 if (pix_x < 0)
1536 pix_x = 0;
1537 else if (pix_x > FRAME_TOTAL_COLS (f))
1538 pix_x = FRAME_TOTAL_COLS (f);
1539
1540 if (pix_y < 0)
1541 pix_y = 0;
1542 else if (pix_y > FRAME_LINES (f))
1543 pix_y = FRAME_LINES (f);
1544 }
1545 }
1546 #endif
1547
1548 *x = pix_x;
1549 *y = pix_y;
1550 }
1551
1552
1553 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1554 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1555 can't tell the positions because W's display is not up to date,
1556 return 0. */
1557
1558 int
1559 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1560 struct window *w;
1561 int hpos, vpos;
1562 int *frame_x, *frame_y;
1563 {
1564 #ifdef HAVE_WINDOW_SYSTEM
1565 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1566 {
1567 int success_p;
1568
1569 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1570 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1571
1572 if (display_completed)
1573 {
1574 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1575 struct glyph *glyph = row->glyphs[TEXT_AREA];
1576 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1577
1578 hpos = row->x;
1579 vpos = row->y;
1580 while (glyph < end)
1581 {
1582 hpos += glyph->pixel_width;
1583 ++glyph;
1584 }
1585
1586 /* If first glyph is partially visible, its first visible position is still 0. */
1587 if (hpos < 0)
1588 hpos = 0;
1589
1590 success_p = 1;
1591 }
1592 else
1593 {
1594 hpos = vpos = 0;
1595 success_p = 0;
1596 }
1597
1598 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1599 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1600 return success_p;
1601 }
1602 #endif
1603
1604 *frame_x = hpos;
1605 *frame_y = vpos;
1606 return 1;
1607 }
1608
1609
1610 #ifdef HAVE_WINDOW_SYSTEM
1611
1612 /* Find the glyph under window-relative coordinates X/Y in window W.
1613 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1614 strings. Return in *HPOS and *VPOS the row and column number of
1615 the glyph found. Return in *AREA the glyph area containing X.
1616 Value is a pointer to the glyph found or null if X/Y is not on
1617 text, or we can't tell because W's current matrix is not up to
1618 date. */
1619
1620 static struct glyph *
1621 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1622 struct window *w;
1623 int x, y;
1624 int *hpos, *vpos, *dx, *dy, *area;
1625 {
1626 struct glyph *glyph, *end;
1627 struct glyph_row *row = NULL;
1628 int x0, i;
1629
1630 /* Find row containing Y. Give up if some row is not enabled. */
1631 for (i = 0; i < w->current_matrix->nrows; ++i)
1632 {
1633 row = MATRIX_ROW (w->current_matrix, i);
1634 if (!row->enabled_p)
1635 return NULL;
1636 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1637 break;
1638 }
1639
1640 *vpos = i;
1641 *hpos = 0;
1642
1643 /* Give up if Y is not in the window. */
1644 if (i == w->current_matrix->nrows)
1645 return NULL;
1646
1647 /* Get the glyph area containing X. */
1648 if (w->pseudo_window_p)
1649 {
1650 *area = TEXT_AREA;
1651 x0 = 0;
1652 }
1653 else
1654 {
1655 if (x < window_box_left_offset (w, TEXT_AREA))
1656 {
1657 *area = LEFT_MARGIN_AREA;
1658 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1659 }
1660 else if (x < window_box_right_offset (w, TEXT_AREA))
1661 {
1662 *area = TEXT_AREA;
1663 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1664 }
1665 else
1666 {
1667 *area = RIGHT_MARGIN_AREA;
1668 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1669 }
1670 }
1671
1672 /* Find glyph containing X. */
1673 glyph = row->glyphs[*area];
1674 end = glyph + row->used[*area];
1675 x -= x0;
1676 while (glyph < end && x >= glyph->pixel_width)
1677 {
1678 x -= glyph->pixel_width;
1679 ++glyph;
1680 }
1681
1682 if (glyph == end)
1683 return NULL;
1684
1685 if (dx)
1686 {
1687 *dx = x;
1688 *dy = y - (row->y + row->ascent - glyph->ascent);
1689 }
1690
1691 *hpos = glyph - row->glyphs[*area];
1692 return glyph;
1693 }
1694
1695
1696 /* EXPORT:
1697 Convert frame-relative x/y to coordinates relative to window W.
1698 Takes pseudo-windows into account. */
1699
1700 void
1701 frame_to_window_pixel_xy (w, x, y)
1702 struct window *w;
1703 int *x, *y;
1704 {
1705 if (w->pseudo_window_p)
1706 {
1707 /* A pseudo-window is always full-width, and starts at the
1708 left edge of the frame, plus a frame border. */
1709 struct frame *f = XFRAME (w->frame);
1710 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1711 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1712 }
1713 else
1714 {
1715 *x -= WINDOW_LEFT_EDGE_X (w);
1716 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1717 }
1718 }
1719
1720 /* EXPORT:
1721 Return in *R the clipping rectangle for glyph string S. */
1722
1723 void
1724 get_glyph_string_clip_rect (s, nr)
1725 struct glyph_string *s;
1726 NativeRectangle *nr;
1727 {
1728 XRectangle r;
1729
1730 if (s->row->full_width_p)
1731 {
1732 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1733 r.x = WINDOW_LEFT_EDGE_X (s->w);
1734 r.width = WINDOW_TOTAL_WIDTH (s->w);
1735
1736 /* Unless displaying a mode or menu bar line, which are always
1737 fully visible, clip to the visible part of the row. */
1738 if (s->w->pseudo_window_p)
1739 r.height = s->row->visible_height;
1740 else
1741 r.height = s->height;
1742 }
1743 else
1744 {
1745 /* This is a text line that may be partially visible. */
1746 r.x = window_box_left (s->w, s->area);
1747 r.width = window_box_width (s->w, s->area);
1748 r.height = s->row->visible_height;
1749 }
1750
1751 /* If S draws overlapping rows, it's sufficient to use the top and
1752 bottom of the window for clipping because this glyph string
1753 intentionally draws over other lines. */
1754 if (s->for_overlaps_p)
1755 {
1756 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1757 r.height = window_text_bottom_y (s->w) - r.y;
1758 }
1759 else
1760 {
1761 /* Don't use S->y for clipping because it doesn't take partially
1762 visible lines into account. For example, it can be negative for
1763 partially visible lines at the top of a window. */
1764 if (!s->row->full_width_p
1765 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1766 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1767 else
1768 r.y = max (0, s->row->y);
1769
1770 /* If drawing a tool-bar window, draw it over the internal border
1771 at the top of the window. */
1772 if (s->w == XWINDOW (s->f->tool_bar_window))
1773 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1774 }
1775
1776 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1777
1778 /* If drawing the cursor, don't let glyph draw outside its
1779 advertised boundaries. Cleartype does this under some circumstances. */
1780 if (s->hl == DRAW_CURSOR)
1781 {
1782 struct glyph *glyph = s->first_glyph;
1783 int height;
1784
1785 if (s->x > r.x)
1786 {
1787 r.width -= s->x - r.x;
1788 r.x = s->x;
1789 }
1790 r.width = min (r.width, glyph->pixel_width);
1791
1792 /* Don't draw cursor glyph taller than our actual glyph. */
1793 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1794 if (height < r.height)
1795 {
1796 int max_y = r.y + r.height;
1797 r.y = min (max_y, s->ybase + glyph->descent - height);
1798 r.height = min (max_y - r.y, height);
1799 }
1800 }
1801
1802 #ifdef CONVERT_FROM_XRECT
1803 CONVERT_FROM_XRECT (r, *nr);
1804 #else
1805 *nr = r;
1806 #endif
1807 }
1808
1809 #endif /* HAVE_WINDOW_SYSTEM */
1810
1811 \f
1812 /***********************************************************************
1813 Lisp form evaluation
1814 ***********************************************************************/
1815
1816 /* Error handler for safe_eval and safe_call. */
1817
1818 static Lisp_Object
1819 safe_eval_handler (arg)
1820 Lisp_Object arg;
1821 {
1822 add_to_log ("Error during redisplay: %s", arg, Qnil);
1823 return Qnil;
1824 }
1825
1826
1827 /* Evaluate SEXPR and return the result, or nil if something went
1828 wrong. Prevent redisplay during the evaluation. */
1829
1830 Lisp_Object
1831 safe_eval (sexpr)
1832 Lisp_Object sexpr;
1833 {
1834 Lisp_Object val;
1835
1836 if (inhibit_eval_during_redisplay)
1837 val = Qnil;
1838 else
1839 {
1840 int count = SPECPDL_INDEX ();
1841 struct gcpro gcpro1;
1842
1843 GCPRO1 (sexpr);
1844 specbind (Qinhibit_redisplay, Qt);
1845 /* Use Qt to ensure debugger does not run,
1846 so there is no possibility of wanting to redisplay. */
1847 val = internal_condition_case_1 (Feval, sexpr, Qt,
1848 safe_eval_handler);
1849 UNGCPRO;
1850 val = unbind_to (count, val);
1851 }
1852
1853 return val;
1854 }
1855
1856
1857 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1858 Return the result, or nil if something went wrong. Prevent
1859 redisplay during the evaluation. */
1860
1861 Lisp_Object
1862 safe_call (nargs, args)
1863 int nargs;
1864 Lisp_Object *args;
1865 {
1866 Lisp_Object val;
1867
1868 if (inhibit_eval_during_redisplay)
1869 val = Qnil;
1870 else
1871 {
1872 int count = SPECPDL_INDEX ();
1873 struct gcpro gcpro1;
1874
1875 GCPRO1 (args[0]);
1876 gcpro1.nvars = nargs;
1877 specbind (Qinhibit_redisplay, Qt);
1878 /* Use Qt to ensure debugger does not run,
1879 so there is no possibility of wanting to redisplay. */
1880 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1881 safe_eval_handler);
1882 UNGCPRO;
1883 val = unbind_to (count, val);
1884 }
1885
1886 return val;
1887 }
1888
1889
1890 /* Call function FN with one argument ARG.
1891 Return the result, or nil if something went wrong. */
1892
1893 Lisp_Object
1894 safe_call1 (fn, arg)
1895 Lisp_Object fn, arg;
1896 {
1897 Lisp_Object args[2];
1898 args[0] = fn;
1899 args[1] = arg;
1900 return safe_call (2, args);
1901 }
1902
1903
1904 \f
1905 /***********************************************************************
1906 Debugging
1907 ***********************************************************************/
1908
1909 #if 0
1910
1911 /* Define CHECK_IT to perform sanity checks on iterators.
1912 This is for debugging. It is too slow to do unconditionally. */
1913
1914 static void
1915 check_it (it)
1916 struct it *it;
1917 {
1918 if (it->method == next_element_from_string)
1919 {
1920 xassert (STRINGP (it->string));
1921 xassert (IT_STRING_CHARPOS (*it) >= 0);
1922 }
1923 else
1924 {
1925 xassert (IT_STRING_CHARPOS (*it) < 0);
1926 if (it->method == next_element_from_buffer)
1927 {
1928 /* Check that character and byte positions agree. */
1929 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1930 }
1931 }
1932
1933 if (it->dpvec)
1934 xassert (it->current.dpvec_index >= 0);
1935 else
1936 xassert (it->current.dpvec_index < 0);
1937 }
1938
1939 #define CHECK_IT(IT) check_it ((IT))
1940
1941 #else /* not 0 */
1942
1943 #define CHECK_IT(IT) (void) 0
1944
1945 #endif /* not 0 */
1946
1947
1948 #if GLYPH_DEBUG
1949
1950 /* Check that the window end of window W is what we expect it
1951 to be---the last row in the current matrix displaying text. */
1952
1953 static void
1954 check_window_end (w)
1955 struct window *w;
1956 {
1957 if (!MINI_WINDOW_P (w)
1958 && !NILP (w->window_end_valid))
1959 {
1960 struct glyph_row *row;
1961 xassert ((row = MATRIX_ROW (w->current_matrix,
1962 XFASTINT (w->window_end_vpos)),
1963 !row->enabled_p
1964 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1965 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1966 }
1967 }
1968
1969 #define CHECK_WINDOW_END(W) check_window_end ((W))
1970
1971 #else /* not GLYPH_DEBUG */
1972
1973 #define CHECK_WINDOW_END(W) (void) 0
1974
1975 #endif /* not GLYPH_DEBUG */
1976
1977
1978 \f
1979 /***********************************************************************
1980 Iterator initialization
1981 ***********************************************************************/
1982
1983 /* Initialize IT for displaying current_buffer in window W, starting
1984 at character position CHARPOS. CHARPOS < 0 means that no buffer
1985 position is specified which is useful when the iterator is assigned
1986 a position later. BYTEPOS is the byte position corresponding to
1987 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1988
1989 If ROW is not null, calls to produce_glyphs with IT as parameter
1990 will produce glyphs in that row.
1991
1992 BASE_FACE_ID is the id of a base face to use. It must be one of
1993 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1994 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1995 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1996
1997 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1998 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1999 will be initialized to use the corresponding mode line glyph row of
2000 the desired matrix of W. */
2001
2002 void
2003 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2004 struct it *it;
2005 struct window *w;
2006 int charpos, bytepos;
2007 struct glyph_row *row;
2008 enum face_id base_face_id;
2009 {
2010 int highlight_region_p;
2011
2012 /* Some precondition checks. */
2013 xassert (w != NULL && it != NULL);
2014 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2015 && charpos <= ZV));
2016
2017 /* If face attributes have been changed since the last redisplay,
2018 free realized faces now because they depend on face definitions
2019 that might have changed. Don't free faces while there might be
2020 desired matrices pending which reference these faces. */
2021 if (face_change_count && !inhibit_free_realized_faces)
2022 {
2023 face_change_count = 0;
2024 free_all_realized_faces (Qnil);
2025 }
2026
2027 /* Use one of the mode line rows of W's desired matrix if
2028 appropriate. */
2029 if (row == NULL)
2030 {
2031 if (base_face_id == MODE_LINE_FACE_ID
2032 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2033 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2034 else if (base_face_id == HEADER_LINE_FACE_ID)
2035 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2036 }
2037
2038 /* Clear IT. */
2039 bzero (it, sizeof *it);
2040 it->current.overlay_string_index = -1;
2041 it->current.dpvec_index = -1;
2042 it->base_face_id = base_face_id;
2043 it->string = Qnil;
2044 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2045
2046 /* The window in which we iterate over current_buffer: */
2047 XSETWINDOW (it->window, w);
2048 it->w = w;
2049 it->f = XFRAME (w->frame);
2050
2051 /* Extra space between lines (on window systems only). */
2052 if (base_face_id == DEFAULT_FACE_ID
2053 && FRAME_WINDOW_P (it->f))
2054 {
2055 if (NATNUMP (current_buffer->extra_line_spacing))
2056 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2057 else if (it->f->extra_line_spacing > 0)
2058 it->extra_line_spacing = it->f->extra_line_spacing;
2059 }
2060
2061 /* If realized faces have been removed, e.g. because of face
2062 attribute changes of named faces, recompute them. When running
2063 in batch mode, the face cache of Vterminal_frame is null. If
2064 we happen to get called, make a dummy face cache. */
2065 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2066 init_frame_faces (it->f);
2067 if (FRAME_FACE_CACHE (it->f)->used == 0)
2068 recompute_basic_faces (it->f);
2069
2070 /* Current value of the `space-width', and 'height' properties. */
2071 it->space_width = Qnil;
2072 it->font_height = Qnil;
2073
2074 /* Are control characters displayed as `^C'? */
2075 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2076
2077 /* -1 means everything between a CR and the following line end
2078 is invisible. >0 means lines indented more than this value are
2079 invisible. */
2080 it->selective = (INTEGERP (current_buffer->selective_display)
2081 ? XFASTINT (current_buffer->selective_display)
2082 : (!NILP (current_buffer->selective_display)
2083 ? -1 : 0));
2084 it->selective_display_ellipsis_p
2085 = !NILP (current_buffer->selective_display_ellipses);
2086
2087 /* Display table to use. */
2088 it->dp = window_display_table (w);
2089
2090 /* Are multibyte characters enabled in current_buffer? */
2091 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2092
2093 /* Non-zero if we should highlight the region. */
2094 highlight_region_p
2095 = (!NILP (Vtransient_mark_mode)
2096 && !NILP (current_buffer->mark_active)
2097 && XMARKER (current_buffer->mark)->buffer != 0);
2098
2099 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2100 start and end of a visible region in window IT->w. Set both to
2101 -1 to indicate no region. */
2102 if (highlight_region_p
2103 /* Maybe highlight only in selected window. */
2104 && (/* Either show region everywhere. */
2105 highlight_nonselected_windows
2106 /* Or show region in the selected window. */
2107 || w == XWINDOW (selected_window)
2108 /* Or show the region if we are in the mini-buffer and W is
2109 the window the mini-buffer refers to. */
2110 || (MINI_WINDOW_P (XWINDOW (selected_window))
2111 && WINDOWP (minibuf_selected_window)
2112 && w == XWINDOW (minibuf_selected_window))))
2113 {
2114 int charpos = marker_position (current_buffer->mark);
2115 it->region_beg_charpos = min (PT, charpos);
2116 it->region_end_charpos = max (PT, charpos);
2117 }
2118 else
2119 it->region_beg_charpos = it->region_end_charpos = -1;
2120
2121 /* Get the position at which the redisplay_end_trigger hook should
2122 be run, if it is to be run at all. */
2123 if (MARKERP (w->redisplay_end_trigger)
2124 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2125 it->redisplay_end_trigger_charpos
2126 = marker_position (w->redisplay_end_trigger);
2127 else if (INTEGERP (w->redisplay_end_trigger))
2128 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2129
2130 /* Correct bogus values of tab_width. */
2131 it->tab_width = XINT (current_buffer->tab_width);
2132 if (it->tab_width <= 0 || it->tab_width > 1000)
2133 it->tab_width = 8;
2134
2135 /* Are lines in the display truncated? */
2136 it->truncate_lines_p
2137 = (base_face_id != DEFAULT_FACE_ID
2138 || XINT (it->w->hscroll)
2139 || (truncate_partial_width_windows
2140 && !WINDOW_FULL_WIDTH_P (it->w))
2141 || !NILP (current_buffer->truncate_lines));
2142
2143 /* Get dimensions of truncation and continuation glyphs. These are
2144 displayed as fringe bitmaps under X, so we don't need them for such
2145 frames. */
2146 if (!FRAME_WINDOW_P (it->f))
2147 {
2148 if (it->truncate_lines_p)
2149 {
2150 /* We will need the truncation glyph. */
2151 xassert (it->glyph_row == NULL);
2152 produce_special_glyphs (it, IT_TRUNCATION);
2153 it->truncation_pixel_width = it->pixel_width;
2154 }
2155 else
2156 {
2157 /* We will need the continuation glyph. */
2158 xassert (it->glyph_row == NULL);
2159 produce_special_glyphs (it, IT_CONTINUATION);
2160 it->continuation_pixel_width = it->pixel_width;
2161 }
2162
2163 /* Reset these values to zero because the produce_special_glyphs
2164 above has changed them. */
2165 it->pixel_width = it->ascent = it->descent = 0;
2166 it->phys_ascent = it->phys_descent = 0;
2167 }
2168
2169 /* Set this after getting the dimensions of truncation and
2170 continuation glyphs, so that we don't produce glyphs when calling
2171 produce_special_glyphs, above. */
2172 it->glyph_row = row;
2173 it->area = TEXT_AREA;
2174
2175 /* Get the dimensions of the display area. The display area
2176 consists of the visible window area plus a horizontally scrolled
2177 part to the left of the window. All x-values are relative to the
2178 start of this total display area. */
2179 if (base_face_id != DEFAULT_FACE_ID)
2180 {
2181 /* Mode lines, menu bar in terminal frames. */
2182 it->first_visible_x = 0;
2183 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2184 }
2185 else
2186 {
2187 it->first_visible_x
2188 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2189 it->last_visible_x = (it->first_visible_x
2190 + window_box_width (w, TEXT_AREA));
2191
2192 /* If we truncate lines, leave room for the truncator glyph(s) at
2193 the right margin. Otherwise, leave room for the continuation
2194 glyph(s). Truncation and continuation glyphs are not inserted
2195 for window-based redisplay. */
2196 if (!FRAME_WINDOW_P (it->f))
2197 {
2198 if (it->truncate_lines_p)
2199 it->last_visible_x -= it->truncation_pixel_width;
2200 else
2201 it->last_visible_x -= it->continuation_pixel_width;
2202 }
2203
2204 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2205 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2206 }
2207
2208 /* Leave room for a border glyph. */
2209 if (!FRAME_WINDOW_P (it->f)
2210 && !WINDOW_RIGHTMOST_P (it->w))
2211 it->last_visible_x -= 1;
2212
2213 it->last_visible_y = window_text_bottom_y (w);
2214
2215 /* For mode lines and alike, arrange for the first glyph having a
2216 left box line if the face specifies a box. */
2217 if (base_face_id != DEFAULT_FACE_ID)
2218 {
2219 struct face *face;
2220
2221 it->face_id = base_face_id;
2222
2223 /* If we have a boxed mode line, make the first character appear
2224 with a left box line. */
2225 face = FACE_FROM_ID (it->f, base_face_id);
2226 if (face->box != FACE_NO_BOX)
2227 it->start_of_box_run_p = 1;
2228 }
2229
2230 /* If a buffer position was specified, set the iterator there,
2231 getting overlays and face properties from that position. */
2232 if (charpos >= BUF_BEG (current_buffer))
2233 {
2234 it->end_charpos = ZV;
2235 it->face_id = -1;
2236 IT_CHARPOS (*it) = charpos;
2237
2238 /* Compute byte position if not specified. */
2239 if (bytepos < charpos)
2240 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2241 else
2242 IT_BYTEPOS (*it) = bytepos;
2243
2244 it->start = it->current;
2245
2246 /* Compute faces etc. */
2247 reseat (it, it->current.pos, 1);
2248 }
2249
2250 CHECK_IT (it);
2251 }
2252
2253
2254 /* Initialize IT for the display of window W with window start POS. */
2255
2256 void
2257 start_display (it, w, pos)
2258 struct it *it;
2259 struct window *w;
2260 struct text_pos pos;
2261 {
2262 struct glyph_row *row;
2263 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2264
2265 row = w->desired_matrix->rows + first_vpos;
2266 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2267 it->first_vpos = first_vpos;
2268
2269 if (!it->truncate_lines_p)
2270 {
2271 int start_at_line_beg_p;
2272 int first_y = it->current_y;
2273
2274 /* If window start is not at a line start, skip forward to POS to
2275 get the correct continuation lines width. */
2276 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2277 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2278 if (!start_at_line_beg_p)
2279 {
2280 int new_x;
2281
2282 reseat_at_previous_visible_line_start (it);
2283 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2284
2285 new_x = it->current_x + it->pixel_width;
2286
2287 /* If lines are continued, this line may end in the middle
2288 of a multi-glyph character (e.g. a control character
2289 displayed as \003, or in the middle of an overlay
2290 string). In this case move_it_to above will not have
2291 taken us to the start of the continuation line but to the
2292 end of the continued line. */
2293 if (it->current_x > 0
2294 && !it->truncate_lines_p /* Lines are continued. */
2295 && (/* And glyph doesn't fit on the line. */
2296 new_x > it->last_visible_x
2297 /* Or it fits exactly and we're on a window
2298 system frame. */
2299 || (new_x == it->last_visible_x
2300 && FRAME_WINDOW_P (it->f))))
2301 {
2302 if (it->current.dpvec_index >= 0
2303 || it->current.overlay_string_index >= 0)
2304 {
2305 set_iterator_to_next (it, 1);
2306 move_it_in_display_line_to (it, -1, -1, 0);
2307 }
2308
2309 it->continuation_lines_width += it->current_x;
2310 }
2311
2312 /* We're starting a new display line, not affected by the
2313 height of the continued line, so clear the appropriate
2314 fields in the iterator structure. */
2315 it->max_ascent = it->max_descent = 0;
2316 it->max_phys_ascent = it->max_phys_descent = 0;
2317
2318 it->current_y = first_y;
2319 it->vpos = 0;
2320 it->current_x = it->hpos = 0;
2321 }
2322 }
2323
2324 #if 0 /* Don't assert the following because start_display is sometimes
2325 called intentionally with a window start that is not at a
2326 line start. Please leave this code in as a comment. */
2327
2328 /* Window start should be on a line start, now. */
2329 xassert (it->continuation_lines_width
2330 || IT_CHARPOS (it) == BEGV
2331 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2332 #endif /* 0 */
2333 }
2334
2335
2336 /* Return 1 if POS is a position in ellipses displayed for invisible
2337 text. W is the window we display, for text property lookup. */
2338
2339 static int
2340 in_ellipses_for_invisible_text_p (pos, w)
2341 struct display_pos *pos;
2342 struct window *w;
2343 {
2344 Lisp_Object prop, window;
2345 int ellipses_p = 0;
2346 int charpos = CHARPOS (pos->pos);
2347
2348 /* If POS specifies a position in a display vector, this might
2349 be for an ellipsis displayed for invisible text. We won't
2350 get the iterator set up for delivering that ellipsis unless
2351 we make sure that it gets aware of the invisible text. */
2352 if (pos->dpvec_index >= 0
2353 && pos->overlay_string_index < 0
2354 && CHARPOS (pos->string_pos) < 0
2355 && charpos > BEGV
2356 && (XSETWINDOW (window, w),
2357 prop = Fget_char_property (make_number (charpos),
2358 Qinvisible, window),
2359 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2360 {
2361 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2362 window);
2363 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2364 }
2365
2366 return ellipses_p;
2367 }
2368
2369
2370 /* Initialize IT for stepping through current_buffer in window W,
2371 starting at position POS that includes overlay string and display
2372 vector/ control character translation position information. Value
2373 is zero if there are overlay strings with newlines at POS. */
2374
2375 static int
2376 init_from_display_pos (it, w, pos)
2377 struct it *it;
2378 struct window *w;
2379 struct display_pos *pos;
2380 {
2381 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2382 int i, overlay_strings_with_newlines = 0;
2383
2384 /* If POS specifies a position in a display vector, this might
2385 be for an ellipsis displayed for invisible text. We won't
2386 get the iterator set up for delivering that ellipsis unless
2387 we make sure that it gets aware of the invisible text. */
2388 if (in_ellipses_for_invisible_text_p (pos, w))
2389 {
2390 --charpos;
2391 bytepos = 0;
2392 }
2393
2394 /* Keep in mind: the call to reseat in init_iterator skips invisible
2395 text, so we might end up at a position different from POS. This
2396 is only a problem when POS is a row start after a newline and an
2397 overlay starts there with an after-string, and the overlay has an
2398 invisible property. Since we don't skip invisible text in
2399 display_line and elsewhere immediately after consuming the
2400 newline before the row start, such a POS will not be in a string,
2401 but the call to init_iterator below will move us to the
2402 after-string. */
2403 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2404
2405 for (i = 0; i < it->n_overlay_strings; ++i)
2406 {
2407 const char *s = SDATA (it->overlay_strings[i]);
2408 const char *e = s + SBYTES (it->overlay_strings[i]);
2409
2410 while (s < e && *s != '\n')
2411 ++s;
2412
2413 if (s < e)
2414 {
2415 overlay_strings_with_newlines = 1;
2416 break;
2417 }
2418 }
2419
2420 /* If position is within an overlay string, set up IT to the right
2421 overlay string. */
2422 if (pos->overlay_string_index >= 0)
2423 {
2424 int relative_index;
2425
2426 /* If the first overlay string happens to have a `display'
2427 property for an image, the iterator will be set up for that
2428 image, and we have to undo that setup first before we can
2429 correct the overlay string index. */
2430 if (it->method == next_element_from_image)
2431 pop_it (it);
2432
2433 /* We already have the first chunk of overlay strings in
2434 IT->overlay_strings. Load more until the one for
2435 pos->overlay_string_index is in IT->overlay_strings. */
2436 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2437 {
2438 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2439 it->current.overlay_string_index = 0;
2440 while (n--)
2441 {
2442 load_overlay_strings (it, 0);
2443 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2444 }
2445 }
2446
2447 it->current.overlay_string_index = pos->overlay_string_index;
2448 relative_index = (it->current.overlay_string_index
2449 % OVERLAY_STRING_CHUNK_SIZE);
2450 it->string = it->overlay_strings[relative_index];
2451 xassert (STRINGP (it->string));
2452 it->current.string_pos = pos->string_pos;
2453 it->method = next_element_from_string;
2454 }
2455
2456 #if 0 /* This is bogus because POS not having an overlay string
2457 position does not mean it's after the string. Example: A
2458 line starting with a before-string and initialization of IT
2459 to the previous row's end position. */
2460 else if (it->current.overlay_string_index >= 0)
2461 {
2462 /* If POS says we're already after an overlay string ending at
2463 POS, make sure to pop the iterator because it will be in
2464 front of that overlay string. When POS is ZV, we've thereby
2465 also ``processed'' overlay strings at ZV. */
2466 while (it->sp)
2467 pop_it (it);
2468 it->current.overlay_string_index = -1;
2469 it->method = next_element_from_buffer;
2470 if (CHARPOS (pos->pos) == ZV)
2471 it->overlay_strings_at_end_processed_p = 1;
2472 }
2473 #endif /* 0 */
2474
2475 if (CHARPOS (pos->string_pos) >= 0)
2476 {
2477 /* Recorded position is not in an overlay string, but in another
2478 string. This can only be a string from a `display' property.
2479 IT should already be filled with that string. */
2480 it->current.string_pos = pos->string_pos;
2481 xassert (STRINGP (it->string));
2482 }
2483
2484 /* Restore position in display vector translations, control
2485 character translations or ellipses. */
2486 if (pos->dpvec_index >= 0)
2487 {
2488 if (it->dpvec == NULL)
2489 get_next_display_element (it);
2490 xassert (it->dpvec && it->current.dpvec_index == 0);
2491 it->current.dpvec_index = pos->dpvec_index;
2492 }
2493
2494 CHECK_IT (it);
2495 return !overlay_strings_with_newlines;
2496 }
2497
2498
2499 /* Initialize IT for stepping through current_buffer in window W
2500 starting at ROW->start. */
2501
2502 static void
2503 init_to_row_start (it, w, row)
2504 struct it *it;
2505 struct window *w;
2506 struct glyph_row *row;
2507 {
2508 init_from_display_pos (it, w, &row->start);
2509 it->start = row->start;
2510 it->continuation_lines_width = row->continuation_lines_width;
2511 CHECK_IT (it);
2512 }
2513
2514
2515 /* Initialize IT for stepping through current_buffer in window W
2516 starting in the line following ROW, i.e. starting at ROW->end.
2517 Value is zero if there are overlay strings with newlines at ROW's
2518 end position. */
2519
2520 static int
2521 init_to_row_end (it, w, row)
2522 struct it *it;
2523 struct window *w;
2524 struct glyph_row *row;
2525 {
2526 int success = 0;
2527
2528 if (init_from_display_pos (it, w, &row->end))
2529 {
2530 if (row->continued_p)
2531 it->continuation_lines_width
2532 = row->continuation_lines_width + row->pixel_width;
2533 CHECK_IT (it);
2534 success = 1;
2535 }
2536
2537 return success;
2538 }
2539
2540
2541
2542 \f
2543 /***********************************************************************
2544 Text properties
2545 ***********************************************************************/
2546
2547 /* Called when IT reaches IT->stop_charpos. Handle text property and
2548 overlay changes. Set IT->stop_charpos to the next position where
2549 to stop. */
2550
2551 static void
2552 handle_stop (it)
2553 struct it *it;
2554 {
2555 enum prop_handled handled;
2556 int handle_overlay_change_p = 1;
2557 struct props *p;
2558
2559 it->dpvec = NULL;
2560 it->current.dpvec_index = -1;
2561
2562 do
2563 {
2564 handled = HANDLED_NORMALLY;
2565
2566 /* Call text property handlers. */
2567 for (p = it_props; p->handler; ++p)
2568 {
2569 handled = p->handler (it);
2570
2571 if (handled == HANDLED_RECOMPUTE_PROPS)
2572 break;
2573 else if (handled == HANDLED_RETURN)
2574 return;
2575 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2576 handle_overlay_change_p = 0;
2577 }
2578
2579 if (handled != HANDLED_RECOMPUTE_PROPS)
2580 {
2581 /* Don't check for overlay strings below when set to deliver
2582 characters from a display vector. */
2583 if (it->method == next_element_from_display_vector)
2584 handle_overlay_change_p = 0;
2585
2586 /* Handle overlay changes. */
2587 if (handle_overlay_change_p)
2588 handled = handle_overlay_change (it);
2589
2590 /* Determine where to stop next. */
2591 if (handled == HANDLED_NORMALLY)
2592 compute_stop_pos (it);
2593 }
2594 }
2595 while (handled == HANDLED_RECOMPUTE_PROPS);
2596 }
2597
2598
2599 /* Compute IT->stop_charpos from text property and overlay change
2600 information for IT's current position. */
2601
2602 static void
2603 compute_stop_pos (it)
2604 struct it *it;
2605 {
2606 register INTERVAL iv, next_iv;
2607 Lisp_Object object, limit, position;
2608
2609 /* If nowhere else, stop at the end. */
2610 it->stop_charpos = it->end_charpos;
2611
2612 if (STRINGP (it->string))
2613 {
2614 /* Strings are usually short, so don't limit the search for
2615 properties. */
2616 object = it->string;
2617 limit = Qnil;
2618 position = make_number (IT_STRING_CHARPOS (*it));
2619 }
2620 else
2621 {
2622 int charpos;
2623
2624 /* If next overlay change is in front of the current stop pos
2625 (which is IT->end_charpos), stop there. Note: value of
2626 next_overlay_change is point-max if no overlay change
2627 follows. */
2628 charpos = next_overlay_change (IT_CHARPOS (*it));
2629 if (charpos < it->stop_charpos)
2630 it->stop_charpos = charpos;
2631
2632 /* If showing the region, we have to stop at the region
2633 start or end because the face might change there. */
2634 if (it->region_beg_charpos > 0)
2635 {
2636 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2637 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2638 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2639 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2640 }
2641
2642 /* Set up variables for computing the stop position from text
2643 property changes. */
2644 XSETBUFFER (object, current_buffer);
2645 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2646 position = make_number (IT_CHARPOS (*it));
2647
2648 }
2649
2650 /* Get the interval containing IT's position. Value is a null
2651 interval if there isn't such an interval. */
2652 iv = validate_interval_range (object, &position, &position, 0);
2653 if (!NULL_INTERVAL_P (iv))
2654 {
2655 Lisp_Object values_here[LAST_PROP_IDX];
2656 struct props *p;
2657
2658 /* Get properties here. */
2659 for (p = it_props; p->handler; ++p)
2660 values_here[p->idx] = textget (iv->plist, *p->name);
2661
2662 /* Look for an interval following iv that has different
2663 properties. */
2664 for (next_iv = next_interval (iv);
2665 (!NULL_INTERVAL_P (next_iv)
2666 && (NILP (limit)
2667 || XFASTINT (limit) > next_iv->position));
2668 next_iv = next_interval (next_iv))
2669 {
2670 for (p = it_props; p->handler; ++p)
2671 {
2672 Lisp_Object new_value;
2673
2674 new_value = textget (next_iv->plist, *p->name);
2675 if (!EQ (values_here[p->idx], new_value))
2676 break;
2677 }
2678
2679 if (p->handler)
2680 break;
2681 }
2682
2683 if (!NULL_INTERVAL_P (next_iv))
2684 {
2685 if (INTEGERP (limit)
2686 && next_iv->position >= XFASTINT (limit))
2687 /* No text property change up to limit. */
2688 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2689 else
2690 /* Text properties change in next_iv. */
2691 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2692 }
2693 }
2694
2695 xassert (STRINGP (it->string)
2696 || (it->stop_charpos >= BEGV
2697 && it->stop_charpos >= IT_CHARPOS (*it)));
2698 }
2699
2700
2701 /* Return the position of the next overlay change after POS in
2702 current_buffer. Value is point-max if no overlay change
2703 follows. This is like `next-overlay-change' but doesn't use
2704 xmalloc. */
2705
2706 static int
2707 next_overlay_change (pos)
2708 int pos;
2709 {
2710 int noverlays;
2711 int endpos;
2712 Lisp_Object *overlays;
2713 int len;
2714 int i;
2715
2716 /* Get all overlays at the given position. */
2717 len = 10;
2718 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2719 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2720 if (noverlays > len)
2721 {
2722 len = noverlays;
2723 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2724 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2725 }
2726
2727 /* If any of these overlays ends before endpos,
2728 use its ending point instead. */
2729 for (i = 0; i < noverlays; ++i)
2730 {
2731 Lisp_Object oend;
2732 int oendpos;
2733
2734 oend = OVERLAY_END (overlays[i]);
2735 oendpos = OVERLAY_POSITION (oend);
2736 endpos = min (endpos, oendpos);
2737 }
2738
2739 return endpos;
2740 }
2741
2742
2743 \f
2744 /***********************************************************************
2745 Fontification
2746 ***********************************************************************/
2747
2748 /* Handle changes in the `fontified' property of the current buffer by
2749 calling hook functions from Qfontification_functions to fontify
2750 regions of text. */
2751
2752 static enum prop_handled
2753 handle_fontified_prop (it)
2754 struct it *it;
2755 {
2756 Lisp_Object prop, pos;
2757 enum prop_handled handled = HANDLED_NORMALLY;
2758
2759 /* Get the value of the `fontified' property at IT's current buffer
2760 position. (The `fontified' property doesn't have a special
2761 meaning in strings.) If the value is nil, call functions from
2762 Qfontification_functions. */
2763 if (!STRINGP (it->string)
2764 && it->s == NULL
2765 && !NILP (Vfontification_functions)
2766 && !NILP (Vrun_hooks)
2767 && (pos = make_number (IT_CHARPOS (*it)),
2768 prop = Fget_char_property (pos, Qfontified, Qnil),
2769 NILP (prop)))
2770 {
2771 int count = SPECPDL_INDEX ();
2772 Lisp_Object val;
2773
2774 val = Vfontification_functions;
2775 specbind (Qfontification_functions, Qnil);
2776
2777 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2778 safe_call1 (val, pos);
2779 else
2780 {
2781 Lisp_Object globals, fn;
2782 struct gcpro gcpro1, gcpro2;
2783
2784 globals = Qnil;
2785 GCPRO2 (val, globals);
2786
2787 for (; CONSP (val); val = XCDR (val))
2788 {
2789 fn = XCAR (val);
2790
2791 if (EQ (fn, Qt))
2792 {
2793 /* A value of t indicates this hook has a local
2794 binding; it means to run the global binding too.
2795 In a global value, t should not occur. If it
2796 does, we must ignore it to avoid an endless
2797 loop. */
2798 for (globals = Fdefault_value (Qfontification_functions);
2799 CONSP (globals);
2800 globals = XCDR (globals))
2801 {
2802 fn = XCAR (globals);
2803 if (!EQ (fn, Qt))
2804 safe_call1 (fn, pos);
2805 }
2806 }
2807 else
2808 safe_call1 (fn, pos);
2809 }
2810
2811 UNGCPRO;
2812 }
2813
2814 unbind_to (count, Qnil);
2815
2816 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2817 something. This avoids an endless loop if they failed to
2818 fontify the text for which reason ever. */
2819 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2820 handled = HANDLED_RECOMPUTE_PROPS;
2821 }
2822
2823 return handled;
2824 }
2825
2826
2827 \f
2828 /***********************************************************************
2829 Faces
2830 ***********************************************************************/
2831
2832 /* Set up iterator IT from face properties at its current position.
2833 Called from handle_stop. */
2834
2835 static enum prop_handled
2836 handle_face_prop (it)
2837 struct it *it;
2838 {
2839 int new_face_id, next_stop;
2840
2841 if (!STRINGP (it->string))
2842 {
2843 new_face_id
2844 = face_at_buffer_position (it->w,
2845 IT_CHARPOS (*it),
2846 it->region_beg_charpos,
2847 it->region_end_charpos,
2848 &next_stop,
2849 (IT_CHARPOS (*it)
2850 + TEXT_PROP_DISTANCE_LIMIT),
2851 0);
2852
2853 /* Is this a start of a run of characters with box face?
2854 Caveat: this can be called for a freshly initialized
2855 iterator; face_id is -1 in this case. We know that the new
2856 face will not change until limit, i.e. if the new face has a
2857 box, all characters up to limit will have one. But, as
2858 usual, we don't know whether limit is really the end. */
2859 if (new_face_id != it->face_id)
2860 {
2861 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2862
2863 /* If new face has a box but old face has not, this is
2864 the start of a run of characters with box, i.e. it has
2865 a shadow on the left side. The value of face_id of the
2866 iterator will be -1 if this is the initial call that gets
2867 the face. In this case, we have to look in front of IT's
2868 position and see whether there is a face != new_face_id. */
2869 it->start_of_box_run_p
2870 = (new_face->box != FACE_NO_BOX
2871 && (it->face_id >= 0
2872 || IT_CHARPOS (*it) == BEG
2873 || new_face_id != face_before_it_pos (it)));
2874 it->face_box_p = new_face->box != FACE_NO_BOX;
2875 }
2876 }
2877 else
2878 {
2879 int base_face_id, bufpos;
2880
2881 if (it->current.overlay_string_index >= 0)
2882 bufpos = IT_CHARPOS (*it);
2883 else
2884 bufpos = 0;
2885
2886 /* For strings from a buffer, i.e. overlay strings or strings
2887 from a `display' property, use the face at IT's current
2888 buffer position as the base face to merge with, so that
2889 overlay strings appear in the same face as surrounding
2890 text, unless they specify their own faces. */
2891 base_face_id = underlying_face_id (it);
2892
2893 new_face_id = face_at_string_position (it->w,
2894 it->string,
2895 IT_STRING_CHARPOS (*it),
2896 bufpos,
2897 it->region_beg_charpos,
2898 it->region_end_charpos,
2899 &next_stop,
2900 base_face_id, 0);
2901
2902 #if 0 /* This shouldn't be neccessary. Let's check it. */
2903 /* If IT is used to display a mode line we would really like to
2904 use the mode line face instead of the frame's default face. */
2905 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2906 && new_face_id == DEFAULT_FACE_ID)
2907 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2908 #endif
2909
2910 /* Is this a start of a run of characters with box? Caveat:
2911 this can be called for a freshly allocated iterator; face_id
2912 is -1 is this case. We know that the new face will not
2913 change until the next check pos, i.e. if the new face has a
2914 box, all characters up to that position will have a
2915 box. But, as usual, we don't know whether that position
2916 is really the end. */
2917 if (new_face_id != it->face_id)
2918 {
2919 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2920 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2921
2922 /* If new face has a box but old face hasn't, this is the
2923 start of a run of characters with box, i.e. it has a
2924 shadow on the left side. */
2925 it->start_of_box_run_p
2926 = new_face->box && (old_face == NULL || !old_face->box);
2927 it->face_box_p = new_face->box != FACE_NO_BOX;
2928 }
2929 }
2930
2931 it->face_id = new_face_id;
2932 return HANDLED_NORMALLY;
2933 }
2934
2935
2936 /* Return the ID of the face ``underlying'' IT's current position,
2937 which is in a string. If the iterator is associated with a
2938 buffer, return the face at IT's current buffer position.
2939 Otherwise, use the iterator's base_face_id. */
2940
2941 static int
2942 underlying_face_id (it)
2943 struct it *it;
2944 {
2945 int face_id = it->base_face_id, i;
2946
2947 xassert (STRINGP (it->string));
2948
2949 for (i = it->sp - 1; i >= 0; --i)
2950 if (NILP (it->stack[i].string))
2951 face_id = it->stack[i].face_id;
2952
2953 return face_id;
2954 }
2955
2956
2957 /* Compute the face one character before or after the current position
2958 of IT. BEFORE_P non-zero means get the face in front of IT's
2959 position. Value is the id of the face. */
2960
2961 static int
2962 face_before_or_after_it_pos (it, before_p)
2963 struct it *it;
2964 int before_p;
2965 {
2966 int face_id, limit;
2967 int next_check_charpos;
2968 struct text_pos pos;
2969
2970 xassert (it->s == NULL);
2971
2972 if (STRINGP (it->string))
2973 {
2974 int bufpos, base_face_id;
2975
2976 /* No face change past the end of the string (for the case
2977 we are padding with spaces). No face change before the
2978 string start. */
2979 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2980 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2981 return it->face_id;
2982
2983 /* Set pos to the position before or after IT's current position. */
2984 if (before_p)
2985 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2986 else
2987 /* For composition, we must check the character after the
2988 composition. */
2989 pos = (it->what == IT_COMPOSITION
2990 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2991 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2992
2993 if (it->current.overlay_string_index >= 0)
2994 bufpos = IT_CHARPOS (*it);
2995 else
2996 bufpos = 0;
2997
2998 base_face_id = underlying_face_id (it);
2999
3000 /* Get the face for ASCII, or unibyte. */
3001 face_id = face_at_string_position (it->w,
3002 it->string,
3003 CHARPOS (pos),
3004 bufpos,
3005 it->region_beg_charpos,
3006 it->region_end_charpos,
3007 &next_check_charpos,
3008 base_face_id, 0);
3009
3010 /* Correct the face for charsets different from ASCII. Do it
3011 for the multibyte case only. The face returned above is
3012 suitable for unibyte text if IT->string is unibyte. */
3013 if (STRING_MULTIBYTE (it->string))
3014 {
3015 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3016 int rest = SBYTES (it->string) - BYTEPOS (pos);
3017 int c, len;
3018 struct face *face = FACE_FROM_ID (it->f, face_id);
3019
3020 c = string_char_and_length (p, rest, &len);
3021 face_id = FACE_FOR_CHAR (it->f, face, c);
3022 }
3023 }
3024 else
3025 {
3026 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3027 || (IT_CHARPOS (*it) <= BEGV && before_p))
3028 return it->face_id;
3029
3030 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3031 pos = it->current.pos;
3032
3033 if (before_p)
3034 DEC_TEXT_POS (pos, it->multibyte_p);
3035 else
3036 {
3037 if (it->what == IT_COMPOSITION)
3038 /* For composition, we must check the position after the
3039 composition. */
3040 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3041 else
3042 INC_TEXT_POS (pos, it->multibyte_p);
3043 }
3044
3045 /* Determine face for CHARSET_ASCII, or unibyte. */
3046 face_id = face_at_buffer_position (it->w,
3047 CHARPOS (pos),
3048 it->region_beg_charpos,
3049 it->region_end_charpos,
3050 &next_check_charpos,
3051 limit, 0);
3052
3053 /* Correct the face for charsets different from ASCII. Do it
3054 for the multibyte case only. The face returned above is
3055 suitable for unibyte text if current_buffer is unibyte. */
3056 if (it->multibyte_p)
3057 {
3058 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3059 struct face *face = FACE_FROM_ID (it->f, face_id);
3060 face_id = FACE_FOR_CHAR (it->f, face, c);
3061 }
3062 }
3063
3064 return face_id;
3065 }
3066
3067
3068 \f
3069 /***********************************************************************
3070 Invisible text
3071 ***********************************************************************/
3072
3073 /* Set up iterator IT from invisible properties at its current
3074 position. Called from handle_stop. */
3075
3076 static enum prop_handled
3077 handle_invisible_prop (it)
3078 struct it *it;
3079 {
3080 enum prop_handled handled = HANDLED_NORMALLY;
3081
3082 if (STRINGP (it->string))
3083 {
3084 extern Lisp_Object Qinvisible;
3085 Lisp_Object prop, end_charpos, limit, charpos;
3086
3087 /* Get the value of the invisible text property at the
3088 current position. Value will be nil if there is no such
3089 property. */
3090 charpos = make_number (IT_STRING_CHARPOS (*it));
3091 prop = Fget_text_property (charpos, Qinvisible, it->string);
3092
3093 if (!NILP (prop)
3094 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3095 {
3096 handled = HANDLED_RECOMPUTE_PROPS;
3097
3098 /* Get the position at which the next change of the
3099 invisible text property can be found in IT->string.
3100 Value will be nil if the property value is the same for
3101 all the rest of IT->string. */
3102 XSETINT (limit, SCHARS (it->string));
3103 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3104 it->string, limit);
3105
3106 /* Text at current position is invisible. The next
3107 change in the property is at position end_charpos.
3108 Move IT's current position to that position. */
3109 if (INTEGERP (end_charpos)
3110 && XFASTINT (end_charpos) < XFASTINT (limit))
3111 {
3112 struct text_pos old;
3113 old = it->current.string_pos;
3114 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3115 compute_string_pos (&it->current.string_pos, old, it->string);
3116 }
3117 else
3118 {
3119 /* The rest of the string is invisible. If this is an
3120 overlay string, proceed with the next overlay string
3121 or whatever comes and return a character from there. */
3122 if (it->current.overlay_string_index >= 0)
3123 {
3124 next_overlay_string (it);
3125 /* Don't check for overlay strings when we just
3126 finished processing them. */
3127 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3128 }
3129 else
3130 {
3131 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3132 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3133 }
3134 }
3135 }
3136 }
3137 else
3138 {
3139 int invis_p, newpos, next_stop, start_charpos;
3140 Lisp_Object pos, prop, overlay;
3141
3142 /* First of all, is there invisible text at this position? */
3143 start_charpos = IT_CHARPOS (*it);
3144 pos = make_number (IT_CHARPOS (*it));
3145 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3146 &overlay);
3147 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3148
3149 /* If we are on invisible text, skip over it. */
3150 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3151 {
3152 /* Record whether we have to display an ellipsis for the
3153 invisible text. */
3154 int display_ellipsis_p = invis_p == 2;
3155
3156 handled = HANDLED_RECOMPUTE_PROPS;
3157
3158 /* Loop skipping over invisible text. The loop is left at
3159 ZV or with IT on the first char being visible again. */
3160 do
3161 {
3162 /* Try to skip some invisible text. Return value is the
3163 position reached which can be equal to IT's position
3164 if there is nothing invisible here. This skips both
3165 over invisible text properties and overlays with
3166 invisible property. */
3167 newpos = skip_invisible (IT_CHARPOS (*it),
3168 &next_stop, ZV, it->window);
3169
3170 /* If we skipped nothing at all we weren't at invisible
3171 text in the first place. If everything to the end of
3172 the buffer was skipped, end the loop. */
3173 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3174 invis_p = 0;
3175 else
3176 {
3177 /* We skipped some characters but not necessarily
3178 all there are. Check if we ended up on visible
3179 text. Fget_char_property returns the property of
3180 the char before the given position, i.e. if we
3181 get invis_p = 0, this means that the char at
3182 newpos is visible. */
3183 pos = make_number (newpos);
3184 prop = Fget_char_property (pos, Qinvisible, it->window);
3185 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3186 }
3187
3188 /* If we ended up on invisible text, proceed to
3189 skip starting with next_stop. */
3190 if (invis_p)
3191 IT_CHARPOS (*it) = next_stop;
3192 }
3193 while (invis_p);
3194
3195 /* The position newpos is now either ZV or on visible text. */
3196 IT_CHARPOS (*it) = newpos;
3197 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3198
3199 /* If there are before-strings at the start of invisible
3200 text, and the text is invisible because of a text
3201 property, arrange to show before-strings because 20.x did
3202 it that way. (If the text is invisible because of an
3203 overlay property instead of a text property, this is
3204 already handled in the overlay code.) */
3205 if (NILP (overlay)
3206 && get_overlay_strings (it, start_charpos))
3207 {
3208 handled = HANDLED_RECOMPUTE_PROPS;
3209 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3210 }
3211 else if (display_ellipsis_p)
3212 setup_for_ellipsis (it);
3213 }
3214 }
3215
3216 return handled;
3217 }
3218
3219
3220 /* Make iterator IT return `...' next. */
3221
3222 static void
3223 setup_for_ellipsis (it)
3224 struct it *it;
3225 {
3226 if (it->dp
3227 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3228 {
3229 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3230 it->dpvec = v->contents;
3231 it->dpend = v->contents + v->size;
3232 }
3233 else
3234 {
3235 /* Default `...'. */
3236 it->dpvec = default_invis_vector;
3237 it->dpend = default_invis_vector + 3;
3238 }
3239
3240 /* The ellipsis display does not replace the display of the
3241 character at the new position. Indicate this by setting
3242 IT->dpvec_char_len to zero. */
3243 it->dpvec_char_len = 0;
3244
3245 it->current.dpvec_index = 0;
3246 it->method = next_element_from_display_vector;
3247 }
3248
3249
3250 \f
3251 /***********************************************************************
3252 'display' property
3253 ***********************************************************************/
3254
3255 /* Set up iterator IT from `display' property at its current position.
3256 Called from handle_stop. */
3257
3258 static enum prop_handled
3259 handle_display_prop (it)
3260 struct it *it;
3261 {
3262 Lisp_Object prop, object;
3263 struct text_pos *position;
3264 int display_replaced_p = 0;
3265
3266 if (STRINGP (it->string))
3267 {
3268 object = it->string;
3269 position = &it->current.string_pos;
3270 }
3271 else
3272 {
3273 object = it->w->buffer;
3274 position = &it->current.pos;
3275 }
3276
3277 /* Reset those iterator values set from display property values. */
3278 it->font_height = Qnil;
3279 it->space_width = Qnil;
3280 it->voffset = 0;
3281
3282 /* We don't support recursive `display' properties, i.e. string
3283 values that have a string `display' property, that have a string
3284 `display' property etc. */
3285 if (!it->string_from_display_prop_p)
3286 it->area = TEXT_AREA;
3287
3288 prop = Fget_char_property (make_number (position->charpos),
3289 Qdisplay, object);
3290 if (NILP (prop))
3291 return HANDLED_NORMALLY;
3292
3293 if (CONSP (prop)
3294 /* Simple properties. */
3295 && !EQ (XCAR (prop), Qimage)
3296 && !EQ (XCAR (prop), Qspace)
3297 && !EQ (XCAR (prop), Qwhen)
3298 && !EQ (XCAR (prop), Qspace_width)
3299 && !EQ (XCAR (prop), Qheight)
3300 && !EQ (XCAR (prop), Qraise)
3301 /* Marginal area specifications. */
3302 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3303 && !EQ (XCAR (prop), Qleft_fringe)
3304 && !EQ (XCAR (prop), Qright_fringe)
3305 && !NILP (XCAR (prop)))
3306 {
3307 for (; CONSP (prop); prop = XCDR (prop))
3308 {
3309 if (handle_single_display_prop (it, XCAR (prop), object,
3310 position, display_replaced_p))
3311 display_replaced_p = 1;
3312 }
3313 }
3314 else if (VECTORP (prop))
3315 {
3316 int i;
3317 for (i = 0; i < ASIZE (prop); ++i)
3318 if (handle_single_display_prop (it, AREF (prop, i), object,
3319 position, display_replaced_p))
3320 display_replaced_p = 1;
3321 }
3322 else
3323 {
3324 if (handle_single_display_prop (it, prop, object, position, 0))
3325 display_replaced_p = 1;
3326 }
3327
3328 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3329 }
3330
3331
3332 /* Value is the position of the end of the `display' property starting
3333 at START_POS in OBJECT. */
3334
3335 static struct text_pos
3336 display_prop_end (it, object, start_pos)
3337 struct it *it;
3338 Lisp_Object object;
3339 struct text_pos start_pos;
3340 {
3341 Lisp_Object end;
3342 struct text_pos end_pos;
3343
3344 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3345 Qdisplay, object, Qnil);
3346 CHARPOS (end_pos) = XFASTINT (end);
3347 if (STRINGP (object))
3348 compute_string_pos (&end_pos, start_pos, it->string);
3349 else
3350 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3351
3352 return end_pos;
3353 }
3354
3355
3356 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3357 is the object in which the `display' property was found. *POSITION
3358 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3359 means that we previously saw a display sub-property which already
3360 replaced text display with something else, for example an image;
3361 ignore such properties after the first one has been processed.
3362
3363 If PROP is a `space' or `image' sub-property, set *POSITION to the
3364 end position of the `display' property.
3365
3366 Value is non-zero if something was found which replaces the display
3367 of buffer or string text. */
3368
3369 static int
3370 handle_single_display_prop (it, prop, object, position,
3371 display_replaced_before_p)
3372 struct it *it;
3373 Lisp_Object prop;
3374 Lisp_Object object;
3375 struct text_pos *position;
3376 int display_replaced_before_p;
3377 {
3378 Lisp_Object value;
3379 int replaces_text_display_p = 0;
3380 Lisp_Object form;
3381
3382 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3383 evaluated. If the result is nil, VALUE is ignored. */
3384 form = Qt;
3385 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3386 {
3387 prop = XCDR (prop);
3388 if (!CONSP (prop))
3389 return 0;
3390 form = XCAR (prop);
3391 prop = XCDR (prop);
3392 }
3393
3394 if (!NILP (form) && !EQ (form, Qt))
3395 {
3396 int count = SPECPDL_INDEX ();
3397 struct gcpro gcpro1;
3398
3399 /* Bind `object' to the object having the `display' property, a
3400 buffer or string. Bind `position' to the position in the
3401 object where the property was found, and `buffer-position'
3402 to the current position in the buffer. */
3403 specbind (Qobject, object);
3404 specbind (Qposition, make_number (CHARPOS (*position)));
3405 specbind (Qbuffer_position,
3406 make_number (STRINGP (object)
3407 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3408 GCPRO1 (form);
3409 form = safe_eval (form);
3410 UNGCPRO;
3411 unbind_to (count, Qnil);
3412 }
3413
3414 if (NILP (form))
3415 return 0;
3416
3417 if (CONSP (prop)
3418 && EQ (XCAR (prop), Qheight)
3419 && CONSP (XCDR (prop)))
3420 {
3421 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3422 return 0;
3423
3424 /* `(height HEIGHT)'. */
3425 it->font_height = XCAR (XCDR (prop));
3426 if (!NILP (it->font_height))
3427 {
3428 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3429 int new_height = -1;
3430
3431 if (CONSP (it->font_height)
3432 && (EQ (XCAR (it->font_height), Qplus)
3433 || EQ (XCAR (it->font_height), Qminus))
3434 && CONSP (XCDR (it->font_height))
3435 && INTEGERP (XCAR (XCDR (it->font_height))))
3436 {
3437 /* `(+ N)' or `(- N)' where N is an integer. */
3438 int steps = XINT (XCAR (XCDR (it->font_height)));
3439 if (EQ (XCAR (it->font_height), Qplus))
3440 steps = - steps;
3441 it->face_id = smaller_face (it->f, it->face_id, steps);
3442 }
3443 else if (FUNCTIONP (it->font_height))
3444 {
3445 /* Call function with current height as argument.
3446 Value is the new height. */
3447 Lisp_Object height;
3448 height = safe_call1 (it->font_height,
3449 face->lface[LFACE_HEIGHT_INDEX]);
3450 if (NUMBERP (height))
3451 new_height = XFLOATINT (height);
3452 }
3453 else if (NUMBERP (it->font_height))
3454 {
3455 /* Value is a multiple of the canonical char height. */
3456 struct face *face;
3457
3458 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3459 new_height = (XFLOATINT (it->font_height)
3460 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3461 }
3462 else
3463 {
3464 /* Evaluate IT->font_height with `height' bound to the
3465 current specified height to get the new height. */
3466 Lisp_Object value;
3467 int count = SPECPDL_INDEX ();
3468
3469 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3470 value = safe_eval (it->font_height);
3471 unbind_to (count, Qnil);
3472
3473 if (NUMBERP (value))
3474 new_height = XFLOATINT (value);
3475 }
3476
3477 if (new_height > 0)
3478 it->face_id = face_with_height (it->f, it->face_id, new_height);
3479 }
3480 }
3481 else if (CONSP (prop)
3482 && EQ (XCAR (prop), Qspace_width)
3483 && CONSP (XCDR (prop)))
3484 {
3485 /* `(space_width WIDTH)'. */
3486 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3487 return 0;
3488
3489 value = XCAR (XCDR (prop));
3490 if (NUMBERP (value) && XFLOATINT (value) > 0)
3491 it->space_width = value;
3492 }
3493 else if (CONSP (prop)
3494 && EQ (XCAR (prop), Qraise)
3495 && CONSP (XCDR (prop)))
3496 {
3497 /* `(raise FACTOR)'. */
3498 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3499 return 0;
3500
3501 #ifdef HAVE_WINDOW_SYSTEM
3502 value = XCAR (XCDR (prop));
3503 if (NUMBERP (value))
3504 {
3505 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3506 it->voffset = - (XFLOATINT (value)
3507 * (FONT_HEIGHT (face->font)));
3508 }
3509 #endif /* HAVE_WINDOW_SYSTEM */
3510 }
3511 else if (!it->string_from_display_prop_p)
3512 {
3513 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3514 VALUE) or `((margin nil) VALUE)' or VALUE. */
3515 Lisp_Object location, value;
3516 struct text_pos start_pos;
3517 int valid_p;
3518
3519 /* Characters having this form of property are not displayed, so
3520 we have to find the end of the property. */
3521 start_pos = *position;
3522 *position = display_prop_end (it, object, start_pos);
3523 value = Qnil;
3524
3525 /* Let's stop at the new position and assume that all
3526 text properties change there. */
3527 it->stop_charpos = position->charpos;
3528
3529 if (CONSP (prop)
3530 && (EQ (XCAR (prop), Qleft_fringe)
3531 || EQ (XCAR (prop), Qright_fringe))
3532 && CONSP (XCDR (prop)))
3533 {
3534 unsigned face_id = DEFAULT_FACE_ID;
3535
3536 /* Save current settings of IT so that we can restore them
3537 when we are finished with the glyph property value. */
3538
3539 /* `(left-fringe BITMAP FACE)'. */
3540 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3541 return 0;
3542
3543 #ifdef HAVE_WINDOW_SYSTEM
3544 value = XCAR (XCDR (prop));
3545 if (!NUMBERP (value)
3546 || !valid_fringe_bitmap_id_p (XINT (value)))
3547 return 0;
3548
3549 if (CONSP (XCDR (XCDR (prop))))
3550 {
3551 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3552
3553 face_id = lookup_named_face (it->f, face_name, 'A');
3554 if (face_id < 0)
3555 return 0;
3556 }
3557
3558 push_it (it);
3559
3560 it->area = TEXT_AREA;
3561 it->what = IT_IMAGE;
3562 it->image_id = -1; /* no image */
3563 it->position = start_pos;
3564 it->object = NILP (object) ? it->w->buffer : object;
3565 it->method = next_element_from_image;
3566 it->face_id = face_id;
3567
3568 /* Say that we haven't consumed the characters with
3569 `display' property yet. The call to pop_it in
3570 set_iterator_to_next will clean this up. */
3571 *position = start_pos;
3572
3573 if (EQ (XCAR (prop), Qleft_fringe))
3574 {
3575 it->left_user_fringe_bitmap = XINT (value);
3576 it->left_user_fringe_face_id = face_id;
3577 }
3578 else
3579 {
3580 it->right_user_fringe_bitmap = XINT (value);
3581 it->right_user_fringe_face_id = face_id;
3582 }
3583 #endif /* HAVE_WINDOW_SYSTEM */
3584 return 1;
3585 }
3586
3587 location = Qunbound;
3588 if (CONSP (prop) && CONSP (XCAR (prop)))
3589 {
3590 Lisp_Object tem;
3591
3592 value = XCDR (prop);
3593 if (CONSP (value))
3594 value = XCAR (value);
3595
3596 tem = XCAR (prop);
3597 if (EQ (XCAR (tem), Qmargin)
3598 && (tem = XCDR (tem),
3599 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3600 (NILP (tem)
3601 || EQ (tem, Qleft_margin)
3602 || EQ (tem, Qright_margin))))
3603 location = tem;
3604 }
3605
3606 if (EQ (location, Qunbound))
3607 {
3608 location = Qnil;
3609 value = prop;
3610 }
3611
3612 valid_p = (STRINGP (value)
3613 #ifdef HAVE_WINDOW_SYSTEM
3614 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3615 #endif /* not HAVE_WINDOW_SYSTEM */
3616 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3617
3618 if ((EQ (location, Qleft_margin)
3619 || EQ (location, Qright_margin)
3620 || NILP (location))
3621 && valid_p
3622 && !display_replaced_before_p)
3623 {
3624 replaces_text_display_p = 1;
3625
3626 /* Save current settings of IT so that we can restore them
3627 when we are finished with the glyph property value. */
3628 push_it (it);
3629
3630 if (NILP (location))
3631 it->area = TEXT_AREA;
3632 else if (EQ (location, Qleft_margin))
3633 it->area = LEFT_MARGIN_AREA;
3634 else
3635 it->area = RIGHT_MARGIN_AREA;
3636
3637 if (STRINGP (value))
3638 {
3639 it->string = value;
3640 it->multibyte_p = STRING_MULTIBYTE (it->string);
3641 it->current.overlay_string_index = -1;
3642 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3643 it->end_charpos = it->string_nchars = SCHARS (it->string);
3644 it->method = next_element_from_string;
3645 it->stop_charpos = 0;
3646 it->string_from_display_prop_p = 1;
3647 /* Say that we haven't consumed the characters with
3648 `display' property yet. The call to pop_it in
3649 set_iterator_to_next will clean this up. */
3650 *position = start_pos;
3651 }
3652 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3653 {
3654 it->method = next_element_from_stretch;
3655 it->object = value;
3656 it->current.pos = it->position = start_pos;
3657 }
3658 #ifdef HAVE_WINDOW_SYSTEM
3659 else
3660 {
3661 it->what = IT_IMAGE;
3662 it->image_id = lookup_image (it->f, value);
3663 it->position = start_pos;
3664 it->object = NILP (object) ? it->w->buffer : object;
3665 it->method = next_element_from_image;
3666
3667 /* Say that we haven't consumed the characters with
3668 `display' property yet. The call to pop_it in
3669 set_iterator_to_next will clean this up. */
3670 *position = start_pos;
3671 }
3672 #endif /* HAVE_WINDOW_SYSTEM */
3673 }
3674 else
3675 /* Invalid property or property not supported. Restore
3676 the position to what it was before. */
3677 *position = start_pos;
3678 }
3679
3680 return replaces_text_display_p;
3681 }
3682
3683
3684 /* Check if PROP is a display sub-property value whose text should be
3685 treated as intangible. */
3686
3687 static int
3688 single_display_prop_intangible_p (prop)
3689 Lisp_Object prop;
3690 {
3691 /* Skip over `when FORM'. */
3692 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3693 {
3694 prop = XCDR (prop);
3695 if (!CONSP (prop))
3696 return 0;
3697 prop = XCDR (prop);
3698 }
3699
3700 if (STRINGP (prop))
3701 return 1;
3702
3703 if (!CONSP (prop))
3704 return 0;
3705
3706 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3707 we don't need to treat text as intangible. */
3708 if (EQ (XCAR (prop), Qmargin))
3709 {
3710 prop = XCDR (prop);
3711 if (!CONSP (prop))
3712 return 0;
3713
3714 prop = XCDR (prop);
3715 if (!CONSP (prop)
3716 || EQ (XCAR (prop), Qleft_margin)
3717 || EQ (XCAR (prop), Qright_margin))
3718 return 0;
3719 }
3720
3721 return (CONSP (prop)
3722 && (EQ (XCAR (prop), Qimage)
3723 || EQ (XCAR (prop), Qspace)));
3724 }
3725
3726
3727 /* Check if PROP is a display property value whose text should be
3728 treated as intangible. */
3729
3730 int
3731 display_prop_intangible_p (prop)
3732 Lisp_Object prop;
3733 {
3734 if (CONSP (prop)
3735 && CONSP (XCAR (prop))
3736 && !EQ (Qmargin, XCAR (XCAR (prop))))
3737 {
3738 /* A list of sub-properties. */
3739 while (CONSP (prop))
3740 {
3741 if (single_display_prop_intangible_p (XCAR (prop)))
3742 return 1;
3743 prop = XCDR (prop);
3744 }
3745 }
3746 else if (VECTORP (prop))
3747 {
3748 /* A vector of sub-properties. */
3749 int i;
3750 for (i = 0; i < ASIZE (prop); ++i)
3751 if (single_display_prop_intangible_p (AREF (prop, i)))
3752 return 1;
3753 }
3754 else
3755 return single_display_prop_intangible_p (prop);
3756
3757 return 0;
3758 }
3759
3760
3761 /* Return 1 if PROP is a display sub-property value containing STRING. */
3762
3763 static int
3764 single_display_prop_string_p (prop, string)
3765 Lisp_Object prop, string;
3766 {
3767 if (EQ (string, prop))
3768 return 1;
3769
3770 /* Skip over `when FORM'. */
3771 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3772 {
3773 prop = XCDR (prop);
3774 if (!CONSP (prop))
3775 return 0;
3776 prop = XCDR (prop);
3777 }
3778
3779 if (CONSP (prop))
3780 /* Skip over `margin LOCATION'. */
3781 if (EQ (XCAR (prop), Qmargin))
3782 {
3783 prop = XCDR (prop);
3784 if (!CONSP (prop))
3785 return 0;
3786
3787 prop = XCDR (prop);
3788 if (!CONSP (prop))
3789 return 0;
3790 }
3791
3792 return CONSP (prop) && EQ (XCAR (prop), string);
3793 }
3794
3795
3796 /* Return 1 if STRING appears in the `display' property PROP. */
3797
3798 static int
3799 display_prop_string_p (prop, string)
3800 Lisp_Object prop, string;
3801 {
3802 if (CONSP (prop)
3803 && CONSP (XCAR (prop))
3804 && !EQ (Qmargin, XCAR (XCAR (prop))))
3805 {
3806 /* A list of sub-properties. */
3807 while (CONSP (prop))
3808 {
3809 if (single_display_prop_string_p (XCAR (prop), string))
3810 return 1;
3811 prop = XCDR (prop);
3812 }
3813 }
3814 else if (VECTORP (prop))
3815 {
3816 /* A vector of sub-properties. */
3817 int i;
3818 for (i = 0; i < ASIZE (prop); ++i)
3819 if (single_display_prop_string_p (AREF (prop, i), string))
3820 return 1;
3821 }
3822 else
3823 return single_display_prop_string_p (prop, string);
3824
3825 return 0;
3826 }
3827
3828
3829 /* Determine from which buffer position in W's buffer STRING comes
3830 from. AROUND_CHARPOS is an approximate position where it could
3831 be from. Value is the buffer position or 0 if it couldn't be
3832 determined.
3833
3834 W's buffer must be current.
3835
3836 This function is necessary because we don't record buffer positions
3837 in glyphs generated from strings (to keep struct glyph small).
3838 This function may only use code that doesn't eval because it is
3839 called asynchronously from note_mouse_highlight. */
3840
3841 int
3842 string_buffer_position (w, string, around_charpos)
3843 struct window *w;
3844 Lisp_Object string;
3845 int around_charpos;
3846 {
3847 Lisp_Object limit, prop, pos;
3848 const int MAX_DISTANCE = 1000;
3849 int found = 0;
3850
3851 pos = make_number (around_charpos);
3852 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3853 while (!found && !EQ (pos, limit))
3854 {
3855 prop = Fget_char_property (pos, Qdisplay, Qnil);
3856 if (!NILP (prop) && display_prop_string_p (prop, string))
3857 found = 1;
3858 else
3859 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3860 }
3861
3862 if (!found)
3863 {
3864 pos = make_number (around_charpos);
3865 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3866 while (!found && !EQ (pos, limit))
3867 {
3868 prop = Fget_char_property (pos, Qdisplay, Qnil);
3869 if (!NILP (prop) && display_prop_string_p (prop, string))
3870 found = 1;
3871 else
3872 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3873 limit);
3874 }
3875 }
3876
3877 return found ? XINT (pos) : 0;
3878 }
3879
3880
3881 \f
3882 /***********************************************************************
3883 `composition' property
3884 ***********************************************************************/
3885
3886 /* Set up iterator IT from `composition' property at its current
3887 position. Called from handle_stop. */
3888
3889 static enum prop_handled
3890 handle_composition_prop (it)
3891 struct it *it;
3892 {
3893 Lisp_Object prop, string;
3894 int pos, pos_byte, end;
3895 enum prop_handled handled = HANDLED_NORMALLY;
3896
3897 if (STRINGP (it->string))
3898 {
3899 pos = IT_STRING_CHARPOS (*it);
3900 pos_byte = IT_STRING_BYTEPOS (*it);
3901 string = it->string;
3902 }
3903 else
3904 {
3905 pos = IT_CHARPOS (*it);
3906 pos_byte = IT_BYTEPOS (*it);
3907 string = Qnil;
3908 }
3909
3910 /* If there's a valid composition and point is not inside of the
3911 composition (in the case that the composition is from the current
3912 buffer), draw a glyph composed from the composition components. */
3913 if (find_composition (pos, -1, &pos, &end, &prop, string)
3914 && COMPOSITION_VALID_P (pos, end, prop)
3915 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3916 {
3917 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3918
3919 if (id >= 0)
3920 {
3921 it->method = next_element_from_composition;
3922 it->cmp_id = id;
3923 it->cmp_len = COMPOSITION_LENGTH (prop);
3924 /* For a terminal, draw only the first character of the
3925 components. */
3926 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3927 it->len = (STRINGP (it->string)
3928 ? string_char_to_byte (it->string, end)
3929 : CHAR_TO_BYTE (end)) - pos_byte;
3930 it->stop_charpos = end;
3931 handled = HANDLED_RETURN;
3932 }
3933 }
3934
3935 return handled;
3936 }
3937
3938
3939 \f
3940 /***********************************************************************
3941 Overlay strings
3942 ***********************************************************************/
3943
3944 /* The following structure is used to record overlay strings for
3945 later sorting in load_overlay_strings. */
3946
3947 struct overlay_entry
3948 {
3949 Lisp_Object overlay;
3950 Lisp_Object string;
3951 int priority;
3952 int after_string_p;
3953 };
3954
3955
3956 /* Set up iterator IT from overlay strings at its current position.
3957 Called from handle_stop. */
3958
3959 static enum prop_handled
3960 handle_overlay_change (it)
3961 struct it *it;
3962 {
3963 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3964 return HANDLED_RECOMPUTE_PROPS;
3965 else
3966 return HANDLED_NORMALLY;
3967 }
3968
3969
3970 /* Set up the next overlay string for delivery by IT, if there is an
3971 overlay string to deliver. Called by set_iterator_to_next when the
3972 end of the current overlay string is reached. If there are more
3973 overlay strings to display, IT->string and
3974 IT->current.overlay_string_index are set appropriately here.
3975 Otherwise IT->string is set to nil. */
3976
3977 static void
3978 next_overlay_string (it)
3979 struct it *it;
3980 {
3981 ++it->current.overlay_string_index;
3982 if (it->current.overlay_string_index == it->n_overlay_strings)
3983 {
3984 /* No more overlay strings. Restore IT's settings to what
3985 they were before overlay strings were processed, and
3986 continue to deliver from current_buffer. */
3987 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3988
3989 pop_it (it);
3990 xassert (it->stop_charpos >= BEGV
3991 && it->stop_charpos <= it->end_charpos);
3992 it->string = Qnil;
3993 it->current.overlay_string_index = -1;
3994 SET_TEXT_POS (it->current.string_pos, -1, -1);
3995 it->n_overlay_strings = 0;
3996 it->method = next_element_from_buffer;
3997
3998 /* If we're at the end of the buffer, record that we have
3999 processed the overlay strings there already, so that
4000 next_element_from_buffer doesn't try it again. */
4001 if (IT_CHARPOS (*it) >= it->end_charpos)
4002 it->overlay_strings_at_end_processed_p = 1;
4003
4004 /* If we have to display `...' for invisible text, set
4005 the iterator up for that. */
4006 if (display_ellipsis_p)
4007 setup_for_ellipsis (it);
4008 }
4009 else
4010 {
4011 /* There are more overlay strings to process. If
4012 IT->current.overlay_string_index has advanced to a position
4013 where we must load IT->overlay_strings with more strings, do
4014 it. */
4015 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4016
4017 if (it->current.overlay_string_index && i == 0)
4018 load_overlay_strings (it, 0);
4019
4020 /* Initialize IT to deliver display elements from the overlay
4021 string. */
4022 it->string = it->overlay_strings[i];
4023 it->multibyte_p = STRING_MULTIBYTE (it->string);
4024 SET_TEXT_POS (it->current.string_pos, 0, 0);
4025 it->method = next_element_from_string;
4026 it->stop_charpos = 0;
4027 }
4028
4029 CHECK_IT (it);
4030 }
4031
4032
4033 /* Compare two overlay_entry structures E1 and E2. Used as a
4034 comparison function for qsort in load_overlay_strings. Overlay
4035 strings for the same position are sorted so that
4036
4037 1. All after-strings come in front of before-strings, except
4038 when they come from the same overlay.
4039
4040 2. Within after-strings, strings are sorted so that overlay strings
4041 from overlays with higher priorities come first.
4042
4043 2. Within before-strings, strings are sorted so that overlay
4044 strings from overlays with higher priorities come last.
4045
4046 Value is analogous to strcmp. */
4047
4048
4049 static int
4050 compare_overlay_entries (e1, e2)
4051 void *e1, *e2;
4052 {
4053 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4054 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4055 int result;
4056
4057 if (entry1->after_string_p != entry2->after_string_p)
4058 {
4059 /* Let after-strings appear in front of before-strings if
4060 they come from different overlays. */
4061 if (EQ (entry1->overlay, entry2->overlay))
4062 result = entry1->after_string_p ? 1 : -1;
4063 else
4064 result = entry1->after_string_p ? -1 : 1;
4065 }
4066 else if (entry1->after_string_p)
4067 /* After-strings sorted in order of decreasing priority. */
4068 result = entry2->priority - entry1->priority;
4069 else
4070 /* Before-strings sorted in order of increasing priority. */
4071 result = entry1->priority - entry2->priority;
4072
4073 return result;
4074 }
4075
4076
4077 /* Load the vector IT->overlay_strings with overlay strings from IT's
4078 current buffer position, or from CHARPOS if that is > 0. Set
4079 IT->n_overlays to the total number of overlay strings found.
4080
4081 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4082 a time. On entry into load_overlay_strings,
4083 IT->current.overlay_string_index gives the number of overlay
4084 strings that have already been loaded by previous calls to this
4085 function.
4086
4087 IT->add_overlay_start contains an additional overlay start
4088 position to consider for taking overlay strings from, if non-zero.
4089 This position comes into play when the overlay has an `invisible'
4090 property, and both before and after-strings. When we've skipped to
4091 the end of the overlay, because of its `invisible' property, we
4092 nevertheless want its before-string to appear.
4093 IT->add_overlay_start will contain the overlay start position
4094 in this case.
4095
4096 Overlay strings are sorted so that after-string strings come in
4097 front of before-string strings. Within before and after-strings,
4098 strings are sorted by overlay priority. See also function
4099 compare_overlay_entries. */
4100
4101 static void
4102 load_overlay_strings (it, charpos)
4103 struct it *it;
4104 int charpos;
4105 {
4106 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4107 Lisp_Object overlay, window, str, invisible;
4108 struct Lisp_Overlay *ov;
4109 int start, end;
4110 int size = 20;
4111 int n = 0, i, j, invis_p;
4112 struct overlay_entry *entries
4113 = (struct overlay_entry *) alloca (size * sizeof *entries);
4114
4115 if (charpos <= 0)
4116 charpos = IT_CHARPOS (*it);
4117
4118 /* Append the overlay string STRING of overlay OVERLAY to vector
4119 `entries' which has size `size' and currently contains `n'
4120 elements. AFTER_P non-zero means STRING is an after-string of
4121 OVERLAY. */
4122 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4123 do \
4124 { \
4125 Lisp_Object priority; \
4126 \
4127 if (n == size) \
4128 { \
4129 int new_size = 2 * size; \
4130 struct overlay_entry *old = entries; \
4131 entries = \
4132 (struct overlay_entry *) alloca (new_size \
4133 * sizeof *entries); \
4134 bcopy (old, entries, size * sizeof *entries); \
4135 size = new_size; \
4136 } \
4137 \
4138 entries[n].string = (STRING); \
4139 entries[n].overlay = (OVERLAY); \
4140 priority = Foverlay_get ((OVERLAY), Qpriority); \
4141 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4142 entries[n].after_string_p = (AFTER_P); \
4143 ++n; \
4144 } \
4145 while (0)
4146
4147 /* Process overlay before the overlay center. */
4148 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4149 {
4150 XSETMISC (overlay, ov);
4151 xassert (OVERLAYP (overlay));
4152 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4153 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4154
4155 if (end < charpos)
4156 break;
4157
4158 /* Skip this overlay if it doesn't start or end at IT's current
4159 position. */
4160 if (end != charpos && start != charpos)
4161 continue;
4162
4163 /* Skip this overlay if it doesn't apply to IT->w. */
4164 window = Foverlay_get (overlay, Qwindow);
4165 if (WINDOWP (window) && XWINDOW (window) != it->w)
4166 continue;
4167
4168 /* If the text ``under'' the overlay is invisible, both before-
4169 and after-strings from this overlay are visible; start and
4170 end position are indistinguishable. */
4171 invisible = Foverlay_get (overlay, Qinvisible);
4172 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4173
4174 /* If overlay has a non-empty before-string, record it. */
4175 if ((start == charpos || (end == charpos && invis_p))
4176 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4177 && SCHARS (str))
4178 RECORD_OVERLAY_STRING (overlay, str, 0);
4179
4180 /* If overlay has a non-empty after-string, record it. */
4181 if ((end == charpos || (start == charpos && invis_p))
4182 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4183 && SCHARS (str))
4184 RECORD_OVERLAY_STRING (overlay, str, 1);
4185 }
4186
4187 /* Process overlays after the overlay center. */
4188 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4189 {
4190 XSETMISC (overlay, ov);
4191 xassert (OVERLAYP (overlay));
4192 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4193 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4194
4195 if (start > charpos)
4196 break;
4197
4198 /* Skip this overlay if it doesn't start or end at IT's current
4199 position. */
4200 if (end != charpos && start != charpos)
4201 continue;
4202
4203 /* Skip this overlay if it doesn't apply to IT->w. */
4204 window = Foverlay_get (overlay, Qwindow);
4205 if (WINDOWP (window) && XWINDOW (window) != it->w)
4206 continue;
4207
4208 /* If the text ``under'' the overlay is invisible, it has a zero
4209 dimension, and both before- and after-strings apply. */
4210 invisible = Foverlay_get (overlay, Qinvisible);
4211 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4212
4213 /* If overlay has a non-empty before-string, record it. */
4214 if ((start == charpos || (end == charpos && invis_p))
4215 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4216 && SCHARS (str))
4217 RECORD_OVERLAY_STRING (overlay, str, 0);
4218
4219 /* If overlay has a non-empty after-string, record it. */
4220 if ((end == charpos || (start == charpos && invis_p))
4221 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4222 && SCHARS (str))
4223 RECORD_OVERLAY_STRING (overlay, str, 1);
4224 }
4225
4226 #undef RECORD_OVERLAY_STRING
4227
4228 /* Sort entries. */
4229 if (n > 1)
4230 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4231
4232 /* Record the total number of strings to process. */
4233 it->n_overlay_strings = n;
4234
4235 /* IT->current.overlay_string_index is the number of overlay strings
4236 that have already been consumed by IT. Copy some of the
4237 remaining overlay strings to IT->overlay_strings. */
4238 i = 0;
4239 j = it->current.overlay_string_index;
4240 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4241 it->overlay_strings[i++] = entries[j++].string;
4242
4243 CHECK_IT (it);
4244 }
4245
4246
4247 /* Get the first chunk of overlay strings at IT's current buffer
4248 position, or at CHARPOS if that is > 0. Value is non-zero if at
4249 least one overlay string was found. */
4250
4251 static int
4252 get_overlay_strings (it, charpos)
4253 struct it *it;
4254 int charpos;
4255 {
4256 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4257 process. This fills IT->overlay_strings with strings, and sets
4258 IT->n_overlay_strings to the total number of strings to process.
4259 IT->pos.overlay_string_index has to be set temporarily to zero
4260 because load_overlay_strings needs this; it must be set to -1
4261 when no overlay strings are found because a zero value would
4262 indicate a position in the first overlay string. */
4263 it->current.overlay_string_index = 0;
4264 load_overlay_strings (it, charpos);
4265
4266 /* If we found overlay strings, set up IT to deliver display
4267 elements from the first one. Otherwise set up IT to deliver
4268 from current_buffer. */
4269 if (it->n_overlay_strings)
4270 {
4271 /* Make sure we know settings in current_buffer, so that we can
4272 restore meaningful values when we're done with the overlay
4273 strings. */
4274 compute_stop_pos (it);
4275 xassert (it->face_id >= 0);
4276
4277 /* Save IT's settings. They are restored after all overlay
4278 strings have been processed. */
4279 xassert (it->sp == 0);
4280 push_it (it);
4281
4282 /* Set up IT to deliver display elements from the first overlay
4283 string. */
4284 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4285 it->string = it->overlay_strings[0];
4286 it->stop_charpos = 0;
4287 xassert (STRINGP (it->string));
4288 it->end_charpos = SCHARS (it->string);
4289 it->multibyte_p = STRING_MULTIBYTE (it->string);
4290 it->method = next_element_from_string;
4291 }
4292 else
4293 {
4294 it->string = Qnil;
4295 it->current.overlay_string_index = -1;
4296 it->method = next_element_from_buffer;
4297 }
4298
4299 CHECK_IT (it);
4300
4301 /* Value is non-zero if we found at least one overlay string. */
4302 return STRINGP (it->string);
4303 }
4304
4305
4306 \f
4307 /***********************************************************************
4308 Saving and restoring state
4309 ***********************************************************************/
4310
4311 /* Save current settings of IT on IT->stack. Called, for example,
4312 before setting up IT for an overlay string, to be able to restore
4313 IT's settings to what they were after the overlay string has been
4314 processed. */
4315
4316 static void
4317 push_it (it)
4318 struct it *it;
4319 {
4320 struct iterator_stack_entry *p;
4321
4322 xassert (it->sp < 2);
4323 p = it->stack + it->sp;
4324
4325 p->stop_charpos = it->stop_charpos;
4326 xassert (it->face_id >= 0);
4327 p->face_id = it->face_id;
4328 p->string = it->string;
4329 p->pos = it->current;
4330 p->end_charpos = it->end_charpos;
4331 p->string_nchars = it->string_nchars;
4332 p->area = it->area;
4333 p->multibyte_p = it->multibyte_p;
4334 p->space_width = it->space_width;
4335 p->font_height = it->font_height;
4336 p->voffset = it->voffset;
4337 p->string_from_display_prop_p = it->string_from_display_prop_p;
4338 p->display_ellipsis_p = 0;
4339 ++it->sp;
4340 }
4341
4342
4343 /* Restore IT's settings from IT->stack. Called, for example, when no
4344 more overlay strings must be processed, and we return to delivering
4345 display elements from a buffer, or when the end of a string from a
4346 `display' property is reached and we return to delivering display
4347 elements from an overlay string, or from a buffer. */
4348
4349 static void
4350 pop_it (it)
4351 struct it *it;
4352 {
4353 struct iterator_stack_entry *p;
4354
4355 xassert (it->sp > 0);
4356 --it->sp;
4357 p = it->stack + it->sp;
4358 it->stop_charpos = p->stop_charpos;
4359 it->face_id = p->face_id;
4360 it->string = p->string;
4361 it->current = p->pos;
4362 it->end_charpos = p->end_charpos;
4363 it->string_nchars = p->string_nchars;
4364 it->area = p->area;
4365 it->multibyte_p = p->multibyte_p;
4366 it->space_width = p->space_width;
4367 it->font_height = p->font_height;
4368 it->voffset = p->voffset;
4369 it->string_from_display_prop_p = p->string_from_display_prop_p;
4370 }
4371
4372
4373 \f
4374 /***********************************************************************
4375 Moving over lines
4376 ***********************************************************************/
4377
4378 /* Set IT's current position to the previous line start. */
4379
4380 static void
4381 back_to_previous_line_start (it)
4382 struct it *it;
4383 {
4384 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4385 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4386 }
4387
4388
4389 /* Move IT to the next line start.
4390
4391 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4392 we skipped over part of the text (as opposed to moving the iterator
4393 continuously over the text). Otherwise, don't change the value
4394 of *SKIPPED_P.
4395
4396 Newlines may come from buffer text, overlay strings, or strings
4397 displayed via the `display' property. That's the reason we can't
4398 simply use find_next_newline_no_quit.
4399
4400 Note that this function may not skip over invisible text that is so
4401 because of text properties and immediately follows a newline. If
4402 it would, function reseat_at_next_visible_line_start, when called
4403 from set_iterator_to_next, would effectively make invisible
4404 characters following a newline part of the wrong glyph row, which
4405 leads to wrong cursor motion. */
4406
4407 static int
4408 forward_to_next_line_start (it, skipped_p)
4409 struct it *it;
4410 int *skipped_p;
4411 {
4412 int old_selective, newline_found_p, n;
4413 const int MAX_NEWLINE_DISTANCE = 500;
4414
4415 /* If already on a newline, just consume it to avoid unintended
4416 skipping over invisible text below. */
4417 if (it->what == IT_CHARACTER
4418 && it->c == '\n'
4419 && CHARPOS (it->position) == IT_CHARPOS (*it))
4420 {
4421 set_iterator_to_next (it, 0);
4422 it->c = 0;
4423 return 1;
4424 }
4425
4426 /* Don't handle selective display in the following. It's (a)
4427 unnecessary because it's done by the caller, and (b) leads to an
4428 infinite recursion because next_element_from_ellipsis indirectly
4429 calls this function. */
4430 old_selective = it->selective;
4431 it->selective = 0;
4432
4433 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4434 from buffer text. */
4435 for (n = newline_found_p = 0;
4436 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4437 n += STRINGP (it->string) ? 0 : 1)
4438 {
4439 if (!get_next_display_element (it))
4440 return 0;
4441 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4442 set_iterator_to_next (it, 0);
4443 }
4444
4445 /* If we didn't find a newline near enough, see if we can use a
4446 short-cut. */
4447 if (!newline_found_p)
4448 {
4449 int start = IT_CHARPOS (*it);
4450 int limit = find_next_newline_no_quit (start, 1);
4451 Lisp_Object pos;
4452
4453 xassert (!STRINGP (it->string));
4454
4455 /* If there isn't any `display' property in sight, and no
4456 overlays, we can just use the position of the newline in
4457 buffer text. */
4458 if (it->stop_charpos >= limit
4459 || ((pos = Fnext_single_property_change (make_number (start),
4460 Qdisplay,
4461 Qnil, make_number (limit)),
4462 NILP (pos))
4463 && next_overlay_change (start) == ZV))
4464 {
4465 IT_CHARPOS (*it) = limit;
4466 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4467 *skipped_p = newline_found_p = 1;
4468 }
4469 else
4470 {
4471 while (get_next_display_element (it)
4472 && !newline_found_p)
4473 {
4474 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4475 set_iterator_to_next (it, 0);
4476 }
4477 }
4478 }
4479
4480 it->selective = old_selective;
4481 return newline_found_p;
4482 }
4483
4484
4485 /* Set IT's current position to the previous visible line start. Skip
4486 invisible text that is so either due to text properties or due to
4487 selective display. Caution: this does not change IT->current_x and
4488 IT->hpos. */
4489
4490 static void
4491 back_to_previous_visible_line_start (it)
4492 struct it *it;
4493 {
4494 int visible_p = 0;
4495
4496 /* Go back one newline if not on BEGV already. */
4497 if (IT_CHARPOS (*it) > BEGV)
4498 back_to_previous_line_start (it);
4499
4500 /* Move over lines that are invisible because of selective display
4501 or text properties. */
4502 while (IT_CHARPOS (*it) > BEGV
4503 && !visible_p)
4504 {
4505 visible_p = 1;
4506
4507 /* If selective > 0, then lines indented more than that values
4508 are invisible. */
4509 if (it->selective > 0
4510 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4511 (double) it->selective)) /* iftc */
4512 visible_p = 0;
4513 else
4514 {
4515 Lisp_Object prop;
4516
4517 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4518 Qinvisible, it->window);
4519 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4520 visible_p = 0;
4521 }
4522
4523 /* Back one more newline if the current one is invisible. */
4524 if (!visible_p)
4525 back_to_previous_line_start (it);
4526 }
4527
4528 xassert (IT_CHARPOS (*it) >= BEGV);
4529 xassert (IT_CHARPOS (*it) == BEGV
4530 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4531 CHECK_IT (it);
4532 }
4533
4534
4535 /* Reseat iterator IT at the previous visible line start. Skip
4536 invisible text that is so either due to text properties or due to
4537 selective display. At the end, update IT's overlay information,
4538 face information etc. */
4539
4540 static void
4541 reseat_at_previous_visible_line_start (it)
4542 struct it *it;
4543 {
4544 back_to_previous_visible_line_start (it);
4545 reseat (it, it->current.pos, 1);
4546 CHECK_IT (it);
4547 }
4548
4549
4550 /* Reseat iterator IT on the next visible line start in the current
4551 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4552 preceding the line start. Skip over invisible text that is so
4553 because of selective display. Compute faces, overlays etc at the
4554 new position. Note that this function does not skip over text that
4555 is invisible because of text properties. */
4556
4557 static void
4558 reseat_at_next_visible_line_start (it, on_newline_p)
4559 struct it *it;
4560 int on_newline_p;
4561 {
4562 int newline_found_p, skipped_p = 0;
4563
4564 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4565
4566 /* Skip over lines that are invisible because they are indented
4567 more than the value of IT->selective. */
4568 if (it->selective > 0)
4569 while (IT_CHARPOS (*it) < ZV
4570 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4571 (double) it->selective)) /* iftc */
4572 {
4573 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4574 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4575 }
4576
4577 /* Position on the newline if that's what's requested. */
4578 if (on_newline_p && newline_found_p)
4579 {
4580 if (STRINGP (it->string))
4581 {
4582 if (IT_STRING_CHARPOS (*it) > 0)
4583 {
4584 --IT_STRING_CHARPOS (*it);
4585 --IT_STRING_BYTEPOS (*it);
4586 }
4587 }
4588 else if (IT_CHARPOS (*it) > BEGV)
4589 {
4590 --IT_CHARPOS (*it);
4591 --IT_BYTEPOS (*it);
4592 reseat (it, it->current.pos, 0);
4593 }
4594 }
4595 else if (skipped_p)
4596 reseat (it, it->current.pos, 0);
4597
4598 CHECK_IT (it);
4599 }
4600
4601
4602 \f
4603 /***********************************************************************
4604 Changing an iterator's position
4605 ***********************************************************************/
4606
4607 /* Change IT's current position to POS in current_buffer. If FORCE_P
4608 is non-zero, always check for text properties at the new position.
4609 Otherwise, text properties are only looked up if POS >=
4610 IT->check_charpos of a property. */
4611
4612 static void
4613 reseat (it, pos, force_p)
4614 struct it *it;
4615 struct text_pos pos;
4616 int force_p;
4617 {
4618 int original_pos = IT_CHARPOS (*it);
4619
4620 reseat_1 (it, pos, 0);
4621
4622 /* Determine where to check text properties. Avoid doing it
4623 where possible because text property lookup is very expensive. */
4624 if (force_p
4625 || CHARPOS (pos) > it->stop_charpos
4626 || CHARPOS (pos) < original_pos)
4627 handle_stop (it);
4628
4629 CHECK_IT (it);
4630 }
4631
4632
4633 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4634 IT->stop_pos to POS, also. */
4635
4636 static void
4637 reseat_1 (it, pos, set_stop_p)
4638 struct it *it;
4639 struct text_pos pos;
4640 int set_stop_p;
4641 {
4642 /* Don't call this function when scanning a C string. */
4643 xassert (it->s == NULL);
4644
4645 /* POS must be a reasonable value. */
4646 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4647
4648 it->current.pos = it->position = pos;
4649 XSETBUFFER (it->object, current_buffer);
4650 it->end_charpos = ZV;
4651 it->dpvec = NULL;
4652 it->current.dpvec_index = -1;
4653 it->current.overlay_string_index = -1;
4654 IT_STRING_CHARPOS (*it) = -1;
4655 IT_STRING_BYTEPOS (*it) = -1;
4656 it->string = Qnil;
4657 it->method = next_element_from_buffer;
4658 /* RMS: I added this to fix a bug in move_it_vertically_backward
4659 where it->area continued to relate to the starting point
4660 for the backward motion. Bug report from
4661 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4662 However, I am not sure whether reseat still does the right thing
4663 in general after this change. */
4664 it->area = TEXT_AREA;
4665 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4666 it->sp = 0;
4667 it->face_before_selective_p = 0;
4668
4669 if (set_stop_p)
4670 it->stop_charpos = CHARPOS (pos);
4671 }
4672
4673
4674 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4675 If S is non-null, it is a C string to iterate over. Otherwise,
4676 STRING gives a Lisp string to iterate over.
4677
4678 If PRECISION > 0, don't return more then PRECISION number of
4679 characters from the string.
4680
4681 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4682 characters have been returned. FIELD_WIDTH < 0 means an infinite
4683 field width.
4684
4685 MULTIBYTE = 0 means disable processing of multibyte characters,
4686 MULTIBYTE > 0 means enable it,
4687 MULTIBYTE < 0 means use IT->multibyte_p.
4688
4689 IT must be initialized via a prior call to init_iterator before
4690 calling this function. */
4691
4692 static void
4693 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4694 struct it *it;
4695 unsigned char *s;
4696 Lisp_Object string;
4697 int charpos;
4698 int precision, field_width, multibyte;
4699 {
4700 /* No region in strings. */
4701 it->region_beg_charpos = it->region_end_charpos = -1;
4702
4703 /* No text property checks performed by default, but see below. */
4704 it->stop_charpos = -1;
4705
4706 /* Set iterator position and end position. */
4707 bzero (&it->current, sizeof it->current);
4708 it->current.overlay_string_index = -1;
4709 it->current.dpvec_index = -1;
4710 xassert (charpos >= 0);
4711
4712 /* If STRING is specified, use its multibyteness, otherwise use the
4713 setting of MULTIBYTE, if specified. */
4714 if (multibyte >= 0)
4715 it->multibyte_p = multibyte > 0;
4716
4717 if (s == NULL)
4718 {
4719 xassert (STRINGP (string));
4720 it->string = string;
4721 it->s = NULL;
4722 it->end_charpos = it->string_nchars = SCHARS (string);
4723 it->method = next_element_from_string;
4724 it->current.string_pos = string_pos (charpos, string);
4725 }
4726 else
4727 {
4728 it->s = s;
4729 it->string = Qnil;
4730
4731 /* Note that we use IT->current.pos, not it->current.string_pos,
4732 for displaying C strings. */
4733 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4734 if (it->multibyte_p)
4735 {
4736 it->current.pos = c_string_pos (charpos, s, 1);
4737 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4738 }
4739 else
4740 {
4741 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4742 it->end_charpos = it->string_nchars = strlen (s);
4743 }
4744
4745 it->method = next_element_from_c_string;
4746 }
4747
4748 /* PRECISION > 0 means don't return more than PRECISION characters
4749 from the string. */
4750 if (precision > 0 && it->end_charpos - charpos > precision)
4751 it->end_charpos = it->string_nchars = charpos + precision;
4752
4753 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4754 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4755 FIELD_WIDTH < 0 means infinite field width. This is useful for
4756 padding with `-' at the end of a mode line. */
4757 if (field_width < 0)
4758 field_width = INFINITY;
4759 if (field_width > it->end_charpos - charpos)
4760 it->end_charpos = charpos + field_width;
4761
4762 /* Use the standard display table for displaying strings. */
4763 if (DISP_TABLE_P (Vstandard_display_table))
4764 it->dp = XCHAR_TABLE (Vstandard_display_table);
4765
4766 it->stop_charpos = charpos;
4767 CHECK_IT (it);
4768 }
4769
4770
4771 \f
4772 /***********************************************************************
4773 Iteration
4774 ***********************************************************************/
4775
4776 /* Load IT's display element fields with information about the next
4777 display element from the current position of IT. Value is zero if
4778 end of buffer (or C string) is reached. */
4779
4780 int
4781 get_next_display_element (it)
4782 struct it *it;
4783 {
4784 /* Non-zero means that we found a display element. Zero means that
4785 we hit the end of what we iterate over. Performance note: the
4786 function pointer `method' used here turns out to be faster than
4787 using a sequence of if-statements. */
4788 int success_p = (*it->method) (it);
4789
4790 if (it->what == IT_CHARACTER)
4791 {
4792 /* Map via display table or translate control characters.
4793 IT->c, IT->len etc. have been set to the next character by
4794 the function call above. If we have a display table, and it
4795 contains an entry for IT->c, translate it. Don't do this if
4796 IT->c itself comes from a display table, otherwise we could
4797 end up in an infinite recursion. (An alternative could be to
4798 count the recursion depth of this function and signal an
4799 error when a certain maximum depth is reached.) Is it worth
4800 it? */
4801 if (success_p && it->dpvec == NULL)
4802 {
4803 Lisp_Object dv;
4804
4805 if (it->dp
4806 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4807 VECTORP (dv)))
4808 {
4809 struct Lisp_Vector *v = XVECTOR (dv);
4810
4811 /* Return the first character from the display table
4812 entry, if not empty. If empty, don't display the
4813 current character. */
4814 if (v->size)
4815 {
4816 it->dpvec_char_len = it->len;
4817 it->dpvec = v->contents;
4818 it->dpend = v->contents + v->size;
4819 it->current.dpvec_index = 0;
4820 it->method = next_element_from_display_vector;
4821 success_p = get_next_display_element (it);
4822 }
4823 else
4824 {
4825 set_iterator_to_next (it, 0);
4826 success_p = get_next_display_element (it);
4827 }
4828 }
4829
4830 /* Translate control characters into `\003' or `^C' form.
4831 Control characters coming from a display table entry are
4832 currently not translated because we use IT->dpvec to hold
4833 the translation. This could easily be changed but I
4834 don't believe that it is worth doing.
4835
4836 If it->multibyte_p is nonzero, eight-bit characters and
4837 non-printable multibyte characters are also translated to
4838 octal form.
4839
4840 If it->multibyte_p is zero, eight-bit characters that
4841 don't have corresponding multibyte char code are also
4842 translated to octal form. */
4843 else if ((it->c < ' '
4844 && (it->area != TEXT_AREA
4845 || (it->c != '\n' && it->c != '\t')))
4846 || (it->multibyte_p
4847 ? ((it->c >= 127
4848 && it->len == 1)
4849 || !CHAR_PRINTABLE_P (it->c))
4850 : (it->c >= 127
4851 && it->c == unibyte_char_to_multibyte (it->c))))
4852 {
4853 /* IT->c is a control character which must be displayed
4854 either as '\003' or as `^C' where the '\\' and '^'
4855 can be defined in the display table. Fill
4856 IT->ctl_chars with glyphs for what we have to
4857 display. Then, set IT->dpvec to these glyphs. */
4858 GLYPH g;
4859
4860 if (it->c < 128 && it->ctl_arrow_p)
4861 {
4862 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4863 if (it->dp
4864 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4865 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4866 g = XINT (DISP_CTRL_GLYPH (it->dp));
4867 else
4868 g = FAST_MAKE_GLYPH ('^', 0);
4869 XSETINT (it->ctl_chars[0], g);
4870
4871 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4872 XSETINT (it->ctl_chars[1], g);
4873
4874 /* Set up IT->dpvec and return first character from it. */
4875 it->dpvec_char_len = it->len;
4876 it->dpvec = it->ctl_chars;
4877 it->dpend = it->dpvec + 2;
4878 it->current.dpvec_index = 0;
4879 it->method = next_element_from_display_vector;
4880 get_next_display_element (it);
4881 }
4882 else
4883 {
4884 unsigned char str[MAX_MULTIBYTE_LENGTH];
4885 int len;
4886 int i;
4887 GLYPH escape_glyph;
4888
4889 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4890 if (it->dp
4891 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4892 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4893 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4894 else
4895 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4896
4897 if (SINGLE_BYTE_CHAR_P (it->c))
4898 str[0] = it->c, len = 1;
4899 else
4900 {
4901 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4902 if (len < 0)
4903 {
4904 /* It's an invalid character, which
4905 shouldn't happen actually, but due to
4906 bugs it may happen. Let's print the char
4907 as is, there's not much meaningful we can
4908 do with it. */
4909 str[0] = it->c;
4910 str[1] = it->c >> 8;
4911 str[2] = it->c >> 16;
4912 str[3] = it->c >> 24;
4913 len = 4;
4914 }
4915 }
4916
4917 for (i = 0; i < len; i++)
4918 {
4919 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4920 /* Insert three more glyphs into IT->ctl_chars for
4921 the octal display of the character. */
4922 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4923 XSETINT (it->ctl_chars[i * 4 + 1], g);
4924 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4925 XSETINT (it->ctl_chars[i * 4 + 2], g);
4926 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4927 XSETINT (it->ctl_chars[i * 4 + 3], g);
4928 }
4929
4930 /* Set up IT->dpvec and return the first character
4931 from it. */
4932 it->dpvec_char_len = it->len;
4933 it->dpvec = it->ctl_chars;
4934 it->dpend = it->dpvec + len * 4;
4935 it->current.dpvec_index = 0;
4936 it->method = next_element_from_display_vector;
4937 get_next_display_element (it);
4938 }
4939 }
4940 }
4941
4942 /* Adjust face id for a multibyte character. There are no
4943 multibyte character in unibyte text. */
4944 if (it->multibyte_p
4945 && success_p
4946 && FRAME_WINDOW_P (it->f))
4947 {
4948 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4949 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4950 }
4951 }
4952
4953 /* Is this character the last one of a run of characters with
4954 box? If yes, set IT->end_of_box_run_p to 1. */
4955 if (it->face_box_p
4956 && it->s == NULL)
4957 {
4958 int face_id;
4959 struct face *face;
4960
4961 it->end_of_box_run_p
4962 = ((face_id = face_after_it_pos (it),
4963 face_id != it->face_id)
4964 && (face = FACE_FROM_ID (it->f, face_id),
4965 face->box == FACE_NO_BOX));
4966 }
4967
4968 /* Value is 0 if end of buffer or string reached. */
4969 return success_p;
4970 }
4971
4972
4973 /* Move IT to the next display element.
4974
4975 RESEAT_P non-zero means if called on a newline in buffer text,
4976 skip to the next visible line start.
4977
4978 Functions get_next_display_element and set_iterator_to_next are
4979 separate because I find this arrangement easier to handle than a
4980 get_next_display_element function that also increments IT's
4981 position. The way it is we can first look at an iterator's current
4982 display element, decide whether it fits on a line, and if it does,
4983 increment the iterator position. The other way around we probably
4984 would either need a flag indicating whether the iterator has to be
4985 incremented the next time, or we would have to implement a
4986 decrement position function which would not be easy to write. */
4987
4988 void
4989 set_iterator_to_next (it, reseat_p)
4990 struct it *it;
4991 int reseat_p;
4992 {
4993 /* Reset flags indicating start and end of a sequence of characters
4994 with box. Reset them at the start of this function because
4995 moving the iterator to a new position might set them. */
4996 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4997
4998 if (it->method == next_element_from_buffer)
4999 {
5000 /* The current display element of IT is a character from
5001 current_buffer. Advance in the buffer, and maybe skip over
5002 invisible lines that are so because of selective display. */
5003 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5004 reseat_at_next_visible_line_start (it, 0);
5005 else
5006 {
5007 xassert (it->len != 0);
5008 IT_BYTEPOS (*it) += it->len;
5009 IT_CHARPOS (*it) += 1;
5010 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5011 }
5012 }
5013 else if (it->method == next_element_from_composition)
5014 {
5015 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5016 if (STRINGP (it->string))
5017 {
5018 IT_STRING_BYTEPOS (*it) += it->len;
5019 IT_STRING_CHARPOS (*it) += it->cmp_len;
5020 it->method = next_element_from_string;
5021 goto consider_string_end;
5022 }
5023 else
5024 {
5025 IT_BYTEPOS (*it) += it->len;
5026 IT_CHARPOS (*it) += it->cmp_len;
5027 it->method = next_element_from_buffer;
5028 }
5029 }
5030 else if (it->method == next_element_from_c_string)
5031 {
5032 /* Current display element of IT is from a C string. */
5033 IT_BYTEPOS (*it) += it->len;
5034 IT_CHARPOS (*it) += 1;
5035 }
5036 else if (it->method == next_element_from_display_vector)
5037 {
5038 /* Current display element of IT is from a display table entry.
5039 Advance in the display table definition. Reset it to null if
5040 end reached, and continue with characters from buffers/
5041 strings. */
5042 ++it->current.dpvec_index;
5043
5044 /* Restore face of the iterator to what they were before the
5045 display vector entry (these entries may contain faces). */
5046 it->face_id = it->saved_face_id;
5047
5048 if (it->dpvec + it->current.dpvec_index == it->dpend)
5049 {
5050 if (it->s)
5051 it->method = next_element_from_c_string;
5052 else if (STRINGP (it->string))
5053 it->method = next_element_from_string;
5054 else
5055 it->method = next_element_from_buffer;
5056
5057 it->dpvec = NULL;
5058 it->current.dpvec_index = -1;
5059
5060 /* Skip over characters which were displayed via IT->dpvec. */
5061 if (it->dpvec_char_len < 0)
5062 reseat_at_next_visible_line_start (it, 1);
5063 else if (it->dpvec_char_len > 0)
5064 {
5065 it->len = it->dpvec_char_len;
5066 set_iterator_to_next (it, reseat_p);
5067 }
5068 }
5069 }
5070 else if (it->method == next_element_from_string)
5071 {
5072 /* Current display element is a character from a Lisp string. */
5073 xassert (it->s == NULL && STRINGP (it->string));
5074 IT_STRING_BYTEPOS (*it) += it->len;
5075 IT_STRING_CHARPOS (*it) += 1;
5076
5077 consider_string_end:
5078
5079 if (it->current.overlay_string_index >= 0)
5080 {
5081 /* IT->string is an overlay string. Advance to the
5082 next, if there is one. */
5083 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5084 next_overlay_string (it);
5085 }
5086 else
5087 {
5088 /* IT->string is not an overlay string. If we reached
5089 its end, and there is something on IT->stack, proceed
5090 with what is on the stack. This can be either another
5091 string, this time an overlay string, or a buffer. */
5092 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5093 && it->sp > 0)
5094 {
5095 pop_it (it);
5096 if (!STRINGP (it->string))
5097 it->method = next_element_from_buffer;
5098 else
5099 goto consider_string_end;
5100 }
5101 }
5102 }
5103 else if (it->method == next_element_from_image
5104 || it->method == next_element_from_stretch)
5105 {
5106 /* The position etc with which we have to proceed are on
5107 the stack. The position may be at the end of a string,
5108 if the `display' property takes up the whole string. */
5109 pop_it (it);
5110 it->image_id = 0;
5111 if (STRINGP (it->string))
5112 {
5113 it->method = next_element_from_string;
5114 goto consider_string_end;
5115 }
5116 else
5117 it->method = next_element_from_buffer;
5118 }
5119 else
5120 /* There are no other methods defined, so this should be a bug. */
5121 abort ();
5122
5123 xassert (it->method != next_element_from_string
5124 || (STRINGP (it->string)
5125 && IT_STRING_CHARPOS (*it) >= 0));
5126 }
5127
5128
5129 /* Load IT's display element fields with information about the next
5130 display element which comes from a display table entry or from the
5131 result of translating a control character to one of the forms `^C'
5132 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5133
5134 static int
5135 next_element_from_display_vector (it)
5136 struct it *it;
5137 {
5138 /* Precondition. */
5139 xassert (it->dpvec && it->current.dpvec_index >= 0);
5140
5141 /* Remember the current face id in case glyphs specify faces.
5142 IT's face is restored in set_iterator_to_next. */
5143 it->saved_face_id = it->face_id;
5144
5145 if (INTEGERP (*it->dpvec)
5146 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5147 {
5148 int lface_id;
5149 GLYPH g;
5150
5151 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5152 it->c = FAST_GLYPH_CHAR (g);
5153 it->len = CHAR_BYTES (it->c);
5154
5155 /* The entry may contain a face id to use. Such a face id is
5156 the id of a Lisp face, not a realized face. A face id of
5157 zero means no face is specified. */
5158 lface_id = FAST_GLYPH_FACE (g);
5159 if (lface_id)
5160 {
5161 /* The function returns -1 if lface_id is invalid. */
5162 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5163 if (face_id >= 0)
5164 it->face_id = face_id;
5165 }
5166 }
5167 else
5168 /* Display table entry is invalid. Return a space. */
5169 it->c = ' ', it->len = 1;
5170
5171 /* Don't change position and object of the iterator here. They are
5172 still the values of the character that had this display table
5173 entry or was translated, and that's what we want. */
5174 it->what = IT_CHARACTER;
5175 return 1;
5176 }
5177
5178
5179 /* Load IT with the next display element from Lisp string IT->string.
5180 IT->current.string_pos is the current position within the string.
5181 If IT->current.overlay_string_index >= 0, the Lisp string is an
5182 overlay string. */
5183
5184 static int
5185 next_element_from_string (it)
5186 struct it *it;
5187 {
5188 struct text_pos position;
5189
5190 xassert (STRINGP (it->string));
5191 xassert (IT_STRING_CHARPOS (*it) >= 0);
5192 position = it->current.string_pos;
5193
5194 /* Time to check for invisible text? */
5195 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5196 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5197 {
5198 handle_stop (it);
5199
5200 /* Since a handler may have changed IT->method, we must
5201 recurse here. */
5202 return get_next_display_element (it);
5203 }
5204
5205 if (it->current.overlay_string_index >= 0)
5206 {
5207 /* Get the next character from an overlay string. In overlay
5208 strings, There is no field width or padding with spaces to
5209 do. */
5210 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5211 {
5212 it->what = IT_EOB;
5213 return 0;
5214 }
5215 else if (STRING_MULTIBYTE (it->string))
5216 {
5217 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5218 const unsigned char *s = (SDATA (it->string)
5219 + IT_STRING_BYTEPOS (*it));
5220 it->c = string_char_and_length (s, remaining, &it->len);
5221 }
5222 else
5223 {
5224 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5225 it->len = 1;
5226 }
5227 }
5228 else
5229 {
5230 /* Get the next character from a Lisp string that is not an
5231 overlay string. Such strings come from the mode line, for
5232 example. We may have to pad with spaces, or truncate the
5233 string. See also next_element_from_c_string. */
5234 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5235 {
5236 it->what = IT_EOB;
5237 return 0;
5238 }
5239 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5240 {
5241 /* Pad with spaces. */
5242 it->c = ' ', it->len = 1;
5243 CHARPOS (position) = BYTEPOS (position) = -1;
5244 }
5245 else if (STRING_MULTIBYTE (it->string))
5246 {
5247 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5248 const unsigned char *s = (SDATA (it->string)
5249 + IT_STRING_BYTEPOS (*it));
5250 it->c = string_char_and_length (s, maxlen, &it->len);
5251 }
5252 else
5253 {
5254 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5255 it->len = 1;
5256 }
5257 }
5258
5259 /* Record what we have and where it came from. Note that we store a
5260 buffer position in IT->position although it could arguably be a
5261 string position. */
5262 it->what = IT_CHARACTER;
5263 it->object = it->string;
5264 it->position = position;
5265 return 1;
5266 }
5267
5268
5269 /* Load IT with next display element from C string IT->s.
5270 IT->string_nchars is the maximum number of characters to return
5271 from the string. IT->end_charpos may be greater than
5272 IT->string_nchars when this function is called, in which case we
5273 may have to return padding spaces. Value is zero if end of string
5274 reached, including padding spaces. */
5275
5276 static int
5277 next_element_from_c_string (it)
5278 struct it *it;
5279 {
5280 int success_p = 1;
5281
5282 xassert (it->s);
5283 it->what = IT_CHARACTER;
5284 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5285 it->object = Qnil;
5286
5287 /* IT's position can be greater IT->string_nchars in case a field
5288 width or precision has been specified when the iterator was
5289 initialized. */
5290 if (IT_CHARPOS (*it) >= it->end_charpos)
5291 {
5292 /* End of the game. */
5293 it->what = IT_EOB;
5294 success_p = 0;
5295 }
5296 else if (IT_CHARPOS (*it) >= it->string_nchars)
5297 {
5298 /* Pad with spaces. */
5299 it->c = ' ', it->len = 1;
5300 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5301 }
5302 else if (it->multibyte_p)
5303 {
5304 /* Implementation note: The calls to strlen apparently aren't a
5305 performance problem because there is no noticeable performance
5306 difference between Emacs running in unibyte or multibyte mode. */
5307 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5308 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5309 maxlen, &it->len);
5310 }
5311 else
5312 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5313
5314 return success_p;
5315 }
5316
5317
5318 /* Set up IT to return characters from an ellipsis, if appropriate.
5319 The definition of the ellipsis glyphs may come from a display table
5320 entry. This function Fills IT with the first glyph from the
5321 ellipsis if an ellipsis is to be displayed. */
5322
5323 static int
5324 next_element_from_ellipsis (it)
5325 struct it *it;
5326 {
5327 if (it->selective_display_ellipsis_p)
5328 {
5329 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5330 {
5331 /* Use the display table definition for `...'. Invalid glyphs
5332 will be handled by the method returning elements from dpvec. */
5333 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5334 it->dpvec_char_len = it->len;
5335 it->dpvec = v->contents;
5336 it->dpend = v->contents + v->size;
5337 it->current.dpvec_index = 0;
5338 it->method = next_element_from_display_vector;
5339 }
5340 else
5341 {
5342 /* Use default `...' which is stored in default_invis_vector. */
5343 it->dpvec_char_len = it->len;
5344 it->dpvec = default_invis_vector;
5345 it->dpend = default_invis_vector + 3;
5346 it->current.dpvec_index = 0;
5347 it->method = next_element_from_display_vector;
5348 }
5349 }
5350 else
5351 {
5352 /* The face at the current position may be different from the
5353 face we find after the invisible text. Remember what it
5354 was in IT->saved_face_id, and signal that it's there by
5355 setting face_before_selective_p. */
5356 it->saved_face_id = it->face_id;
5357 it->method = next_element_from_buffer;
5358 reseat_at_next_visible_line_start (it, 1);
5359 it->face_before_selective_p = 1;
5360 }
5361
5362 return get_next_display_element (it);
5363 }
5364
5365
5366 /* Deliver an image display element. The iterator IT is already
5367 filled with image information (done in handle_display_prop). Value
5368 is always 1. */
5369
5370
5371 static int
5372 next_element_from_image (it)
5373 struct it *it;
5374 {
5375 it->what = IT_IMAGE;
5376 return 1;
5377 }
5378
5379
5380 /* Fill iterator IT with next display element from a stretch glyph
5381 property. IT->object is the value of the text property. Value is
5382 always 1. */
5383
5384 static int
5385 next_element_from_stretch (it)
5386 struct it *it;
5387 {
5388 it->what = IT_STRETCH;
5389 return 1;
5390 }
5391
5392
5393 /* Load IT with the next display element from current_buffer. Value
5394 is zero if end of buffer reached. IT->stop_charpos is the next
5395 position at which to stop and check for text properties or buffer
5396 end. */
5397
5398 static int
5399 next_element_from_buffer (it)
5400 struct it *it;
5401 {
5402 int success_p = 1;
5403
5404 /* Check this assumption, otherwise, we would never enter the
5405 if-statement, below. */
5406 xassert (IT_CHARPOS (*it) >= BEGV
5407 && IT_CHARPOS (*it) <= it->stop_charpos);
5408
5409 if (IT_CHARPOS (*it) >= it->stop_charpos)
5410 {
5411 if (IT_CHARPOS (*it) >= it->end_charpos)
5412 {
5413 int overlay_strings_follow_p;
5414
5415 /* End of the game, except when overlay strings follow that
5416 haven't been returned yet. */
5417 if (it->overlay_strings_at_end_processed_p)
5418 overlay_strings_follow_p = 0;
5419 else
5420 {
5421 it->overlay_strings_at_end_processed_p = 1;
5422 overlay_strings_follow_p = get_overlay_strings (it, 0);
5423 }
5424
5425 if (overlay_strings_follow_p)
5426 success_p = get_next_display_element (it);
5427 else
5428 {
5429 it->what = IT_EOB;
5430 it->position = it->current.pos;
5431 success_p = 0;
5432 }
5433 }
5434 else
5435 {
5436 handle_stop (it);
5437 return get_next_display_element (it);
5438 }
5439 }
5440 else
5441 {
5442 /* No face changes, overlays etc. in sight, so just return a
5443 character from current_buffer. */
5444 unsigned char *p;
5445
5446 /* Maybe run the redisplay end trigger hook. Performance note:
5447 This doesn't seem to cost measurable time. */
5448 if (it->redisplay_end_trigger_charpos
5449 && it->glyph_row
5450 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5451 run_redisplay_end_trigger_hook (it);
5452
5453 /* Get the next character, maybe multibyte. */
5454 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5455 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5456 {
5457 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5458 - IT_BYTEPOS (*it));
5459 it->c = string_char_and_length (p, maxlen, &it->len);
5460 }
5461 else
5462 it->c = *p, it->len = 1;
5463
5464 /* Record what we have and where it came from. */
5465 it->what = IT_CHARACTER;;
5466 it->object = it->w->buffer;
5467 it->position = it->current.pos;
5468
5469 /* Normally we return the character found above, except when we
5470 really want to return an ellipsis for selective display. */
5471 if (it->selective)
5472 {
5473 if (it->c == '\n')
5474 {
5475 /* A value of selective > 0 means hide lines indented more
5476 than that number of columns. */
5477 if (it->selective > 0
5478 && IT_CHARPOS (*it) + 1 < ZV
5479 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5480 IT_BYTEPOS (*it) + 1,
5481 (double) it->selective)) /* iftc */
5482 {
5483 success_p = next_element_from_ellipsis (it);
5484 it->dpvec_char_len = -1;
5485 }
5486 }
5487 else if (it->c == '\r' && it->selective == -1)
5488 {
5489 /* A value of selective == -1 means that everything from the
5490 CR to the end of the line is invisible, with maybe an
5491 ellipsis displayed for it. */
5492 success_p = next_element_from_ellipsis (it);
5493 it->dpvec_char_len = -1;
5494 }
5495 }
5496 }
5497
5498 /* Value is zero if end of buffer reached. */
5499 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5500 return success_p;
5501 }
5502
5503
5504 /* Run the redisplay end trigger hook for IT. */
5505
5506 static void
5507 run_redisplay_end_trigger_hook (it)
5508 struct it *it;
5509 {
5510 Lisp_Object args[3];
5511
5512 /* IT->glyph_row should be non-null, i.e. we should be actually
5513 displaying something, or otherwise we should not run the hook. */
5514 xassert (it->glyph_row);
5515
5516 /* Set up hook arguments. */
5517 args[0] = Qredisplay_end_trigger_functions;
5518 args[1] = it->window;
5519 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5520 it->redisplay_end_trigger_charpos = 0;
5521
5522 /* Since we are *trying* to run these functions, don't try to run
5523 them again, even if they get an error. */
5524 it->w->redisplay_end_trigger = Qnil;
5525 Frun_hook_with_args (3, args);
5526
5527 /* Notice if it changed the face of the character we are on. */
5528 handle_face_prop (it);
5529 }
5530
5531
5532 /* Deliver a composition display element. The iterator IT is already
5533 filled with composition information (done in
5534 handle_composition_prop). Value is always 1. */
5535
5536 static int
5537 next_element_from_composition (it)
5538 struct it *it;
5539 {
5540 it->what = IT_COMPOSITION;
5541 it->position = (STRINGP (it->string)
5542 ? it->current.string_pos
5543 : it->current.pos);
5544 return 1;
5545 }
5546
5547
5548 \f
5549 /***********************************************************************
5550 Moving an iterator without producing glyphs
5551 ***********************************************************************/
5552
5553 /* Move iterator IT to a specified buffer or X position within one
5554 line on the display without producing glyphs.
5555
5556 OP should be a bit mask including some or all of these bits:
5557 MOVE_TO_X: Stop on reaching x-position TO_X.
5558 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5559 Regardless of OP's value, stop in reaching the end of the display line.
5560
5561 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5562 This means, in particular, that TO_X includes window's horizontal
5563 scroll amount.
5564
5565 The return value has several possible values that
5566 say what condition caused the scan to stop:
5567
5568 MOVE_POS_MATCH_OR_ZV
5569 - when TO_POS or ZV was reached.
5570
5571 MOVE_X_REACHED
5572 -when TO_X was reached before TO_POS or ZV were reached.
5573
5574 MOVE_LINE_CONTINUED
5575 - when we reached the end of the display area and the line must
5576 be continued.
5577
5578 MOVE_LINE_TRUNCATED
5579 - when we reached the end of the display area and the line is
5580 truncated.
5581
5582 MOVE_NEWLINE_OR_CR
5583 - when we stopped at a line end, i.e. a newline or a CR and selective
5584 display is on. */
5585
5586 static enum move_it_result
5587 move_it_in_display_line_to (it, to_charpos, to_x, op)
5588 struct it *it;
5589 int to_charpos, to_x, op;
5590 {
5591 enum move_it_result result = MOVE_UNDEFINED;
5592 struct glyph_row *saved_glyph_row;
5593
5594 /* Don't produce glyphs in produce_glyphs. */
5595 saved_glyph_row = it->glyph_row;
5596 it->glyph_row = NULL;
5597
5598 #define BUFFER_POS_REACHED_P() \
5599 ((op & MOVE_TO_POS) != 0 \
5600 && BUFFERP (it->object) \
5601 && IT_CHARPOS (*it) >= to_charpos)
5602
5603 while (1)
5604 {
5605 int x, i, ascent = 0, descent = 0;
5606
5607 /* Stop when ZV or TO_CHARPOS reached. */
5608 if (!get_next_display_element (it)
5609 || BUFFER_POS_REACHED_P ())
5610 {
5611 result = MOVE_POS_MATCH_OR_ZV;
5612 break;
5613 }
5614
5615 /* The call to produce_glyphs will get the metrics of the
5616 display element IT is loaded with. We record in x the
5617 x-position before this display element in case it does not
5618 fit on the line. */
5619 x = it->current_x;
5620
5621 /* Remember the line height so far in case the next element doesn't
5622 fit on the line. */
5623 if (!it->truncate_lines_p)
5624 {
5625 ascent = it->max_ascent;
5626 descent = it->max_descent;
5627 }
5628
5629 PRODUCE_GLYPHS (it);
5630
5631 if (it->area != TEXT_AREA)
5632 {
5633 set_iterator_to_next (it, 1);
5634 continue;
5635 }
5636
5637 /* The number of glyphs we get back in IT->nglyphs will normally
5638 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5639 character on a terminal frame, or (iii) a line end. For the
5640 second case, IT->nglyphs - 1 padding glyphs will be present
5641 (on X frames, there is only one glyph produced for a
5642 composite character.
5643
5644 The behavior implemented below means, for continuation lines,
5645 that as many spaces of a TAB as fit on the current line are
5646 displayed there. For terminal frames, as many glyphs of a
5647 multi-glyph character are displayed in the current line, too.
5648 This is what the old redisplay code did, and we keep it that
5649 way. Under X, the whole shape of a complex character must
5650 fit on the line or it will be completely displayed in the
5651 next line.
5652
5653 Note that both for tabs and padding glyphs, all glyphs have
5654 the same width. */
5655 if (it->nglyphs)
5656 {
5657 /* More than one glyph or glyph doesn't fit on line. All
5658 glyphs have the same width. */
5659 int single_glyph_width = it->pixel_width / it->nglyphs;
5660 int new_x;
5661
5662 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5663 {
5664 new_x = x + single_glyph_width;
5665
5666 /* We want to leave anything reaching TO_X to the caller. */
5667 if ((op & MOVE_TO_X) && new_x > to_x)
5668 {
5669 it->current_x = x;
5670 result = MOVE_X_REACHED;
5671 break;
5672 }
5673 else if (/* Lines are continued. */
5674 !it->truncate_lines_p
5675 && (/* And glyph doesn't fit on the line. */
5676 new_x > it->last_visible_x
5677 /* Or it fits exactly and we're on a window
5678 system frame. */
5679 || (new_x == it->last_visible_x
5680 && FRAME_WINDOW_P (it->f))))
5681 {
5682 if (/* IT->hpos == 0 means the very first glyph
5683 doesn't fit on the line, e.g. a wide image. */
5684 it->hpos == 0
5685 || (new_x == it->last_visible_x
5686 && FRAME_WINDOW_P (it->f)))
5687 {
5688 ++it->hpos;
5689 it->current_x = new_x;
5690 if (i == it->nglyphs - 1)
5691 {
5692 set_iterator_to_next (it, 1);
5693 #ifdef HAVE_WINDOW_SYSTEM
5694 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5695 {
5696 if (!get_next_display_element (it)
5697 || BUFFER_POS_REACHED_P ())
5698 {
5699 result = MOVE_POS_MATCH_OR_ZV;
5700 break;
5701 }
5702 if (ITERATOR_AT_END_OF_LINE_P (it))
5703 {
5704 result = MOVE_NEWLINE_OR_CR;
5705 break;
5706 }
5707 }
5708 #endif /* HAVE_WINDOW_SYSTEM */
5709 }
5710 }
5711 else
5712 {
5713 it->current_x = x;
5714 it->max_ascent = ascent;
5715 it->max_descent = descent;
5716 }
5717
5718 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5719 IT_CHARPOS (*it)));
5720 result = MOVE_LINE_CONTINUED;
5721 break;
5722 }
5723 else if (new_x > it->first_visible_x)
5724 {
5725 /* Glyph is visible. Increment number of glyphs that
5726 would be displayed. */
5727 ++it->hpos;
5728 }
5729 else
5730 {
5731 /* Glyph is completely off the left margin of the display
5732 area. Nothing to do. */
5733 }
5734 }
5735
5736 if (result != MOVE_UNDEFINED)
5737 break;
5738 }
5739 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5740 {
5741 /* Stop when TO_X specified and reached. This check is
5742 necessary here because of lines consisting of a line end,
5743 only. The line end will not produce any glyphs and we
5744 would never get MOVE_X_REACHED. */
5745 xassert (it->nglyphs == 0);
5746 result = MOVE_X_REACHED;
5747 break;
5748 }
5749
5750 /* Is this a line end? If yes, we're done. */
5751 if (ITERATOR_AT_END_OF_LINE_P (it))
5752 {
5753 result = MOVE_NEWLINE_OR_CR;
5754 break;
5755 }
5756
5757 /* The current display element has been consumed. Advance
5758 to the next. */
5759 set_iterator_to_next (it, 1);
5760
5761 /* Stop if lines are truncated and IT's current x-position is
5762 past the right edge of the window now. */
5763 if (it->truncate_lines_p
5764 && it->current_x >= it->last_visible_x)
5765 {
5766 #ifdef HAVE_WINDOW_SYSTEM
5767 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5768 {
5769 if (!get_next_display_element (it)
5770 || BUFFER_POS_REACHED_P ())
5771 {
5772 result = MOVE_POS_MATCH_OR_ZV;
5773 break;
5774 }
5775 if (ITERATOR_AT_END_OF_LINE_P (it))
5776 {
5777 result = MOVE_NEWLINE_OR_CR;
5778 break;
5779 }
5780 }
5781 #endif /* HAVE_WINDOW_SYSTEM */
5782 result = MOVE_LINE_TRUNCATED;
5783 break;
5784 }
5785 }
5786
5787 #undef BUFFER_POS_REACHED_P
5788
5789 /* Restore the iterator settings altered at the beginning of this
5790 function. */
5791 it->glyph_row = saved_glyph_row;
5792 return result;
5793 }
5794
5795
5796 /* Move IT forward until it satisfies one or more of the criteria in
5797 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5798
5799 OP is a bit-mask that specifies where to stop, and in particular,
5800 which of those four position arguments makes a difference. See the
5801 description of enum move_operation_enum.
5802
5803 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5804 screen line, this function will set IT to the next position >
5805 TO_CHARPOS. */
5806
5807 void
5808 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5809 struct it *it;
5810 int to_charpos, to_x, to_y, to_vpos;
5811 int op;
5812 {
5813 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5814 int line_height;
5815 int reached = 0;
5816
5817 for (;;)
5818 {
5819 if (op & MOVE_TO_VPOS)
5820 {
5821 /* If no TO_CHARPOS and no TO_X specified, stop at the
5822 start of the line TO_VPOS. */
5823 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5824 {
5825 if (it->vpos == to_vpos)
5826 {
5827 reached = 1;
5828 break;
5829 }
5830 else
5831 skip = move_it_in_display_line_to (it, -1, -1, 0);
5832 }
5833 else
5834 {
5835 /* TO_VPOS >= 0 means stop at TO_X in the line at
5836 TO_VPOS, or at TO_POS, whichever comes first. */
5837 if (it->vpos == to_vpos)
5838 {
5839 reached = 2;
5840 break;
5841 }
5842
5843 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5844
5845 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5846 {
5847 reached = 3;
5848 break;
5849 }
5850 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5851 {
5852 /* We have reached TO_X but not in the line we want. */
5853 skip = move_it_in_display_line_to (it, to_charpos,
5854 -1, MOVE_TO_POS);
5855 if (skip == MOVE_POS_MATCH_OR_ZV)
5856 {
5857 reached = 4;
5858 break;
5859 }
5860 }
5861 }
5862 }
5863 else if (op & MOVE_TO_Y)
5864 {
5865 struct it it_backup;
5866
5867 /* TO_Y specified means stop at TO_X in the line containing
5868 TO_Y---or at TO_CHARPOS if this is reached first. The
5869 problem is that we can't really tell whether the line
5870 contains TO_Y before we have completely scanned it, and
5871 this may skip past TO_X. What we do is to first scan to
5872 TO_X.
5873
5874 If TO_X is not specified, use a TO_X of zero. The reason
5875 is to make the outcome of this function more predictable.
5876 If we didn't use TO_X == 0, we would stop at the end of
5877 the line which is probably not what a caller would expect
5878 to happen. */
5879 skip = move_it_in_display_line_to (it, to_charpos,
5880 ((op & MOVE_TO_X)
5881 ? to_x : 0),
5882 (MOVE_TO_X
5883 | (op & MOVE_TO_POS)));
5884
5885 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5886 if (skip == MOVE_POS_MATCH_OR_ZV)
5887 {
5888 reached = 5;
5889 break;
5890 }
5891
5892 /* If TO_X was reached, we would like to know whether TO_Y
5893 is in the line. This can only be said if we know the
5894 total line height which requires us to scan the rest of
5895 the line. */
5896 if (skip == MOVE_X_REACHED)
5897 {
5898 it_backup = *it;
5899 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5900 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5901 op & MOVE_TO_POS);
5902 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5903 }
5904
5905 /* Now, decide whether TO_Y is in this line. */
5906 line_height = it->max_ascent + it->max_descent;
5907 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5908
5909 if (to_y >= it->current_y
5910 && to_y < it->current_y + line_height)
5911 {
5912 if (skip == MOVE_X_REACHED)
5913 /* If TO_Y is in this line and TO_X was reached above,
5914 we scanned too far. We have to restore IT's settings
5915 to the ones before skipping. */
5916 *it = it_backup;
5917 reached = 6;
5918 }
5919 else if (skip == MOVE_X_REACHED)
5920 {
5921 skip = skip2;
5922 if (skip == MOVE_POS_MATCH_OR_ZV)
5923 reached = 7;
5924 }
5925
5926 if (reached)
5927 break;
5928 }
5929 else
5930 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5931
5932 switch (skip)
5933 {
5934 case MOVE_POS_MATCH_OR_ZV:
5935 reached = 8;
5936 goto out;
5937
5938 case MOVE_NEWLINE_OR_CR:
5939 set_iterator_to_next (it, 1);
5940 it->continuation_lines_width = 0;
5941 break;
5942
5943 case MOVE_LINE_TRUNCATED:
5944 it->continuation_lines_width = 0;
5945 reseat_at_next_visible_line_start (it, 0);
5946 if ((op & MOVE_TO_POS) != 0
5947 && IT_CHARPOS (*it) > to_charpos)
5948 {
5949 reached = 9;
5950 goto out;
5951 }
5952 break;
5953
5954 case MOVE_LINE_CONTINUED:
5955 it->continuation_lines_width += it->current_x;
5956 break;
5957
5958 default:
5959 abort ();
5960 }
5961
5962 /* Reset/increment for the next run. */
5963 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5964 it->current_x = it->hpos = 0;
5965 it->current_y += it->max_ascent + it->max_descent;
5966 ++it->vpos;
5967 last_height = it->max_ascent + it->max_descent;
5968 last_max_ascent = it->max_ascent;
5969 it->max_ascent = it->max_descent = 0;
5970 }
5971
5972 out:
5973
5974 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5975 }
5976
5977
5978 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5979
5980 If DY > 0, move IT backward at least that many pixels. DY = 0
5981 means move IT backward to the preceding line start or BEGV. This
5982 function may move over more than DY pixels if IT->current_y - DY
5983 ends up in the middle of a line; in this case IT->current_y will be
5984 set to the top of the line moved to. */
5985
5986 void
5987 move_it_vertically_backward (it, dy)
5988 struct it *it;
5989 int dy;
5990 {
5991 int nlines, h;
5992 struct it it2, it3;
5993 int start_pos = IT_CHARPOS (*it);
5994
5995 xassert (dy >= 0);
5996
5997 /* Estimate how many newlines we must move back. */
5998 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5999
6000 /* Set the iterator's position that many lines back. */
6001 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6002 back_to_previous_visible_line_start (it);
6003
6004 /* Reseat the iterator here. When moving backward, we don't want
6005 reseat to skip forward over invisible text, set up the iterator
6006 to deliver from overlay strings at the new position etc. So,
6007 use reseat_1 here. */
6008 reseat_1 (it, it->current.pos, 1);
6009
6010 /* We are now surely at a line start. */
6011 it->current_x = it->hpos = 0;
6012 it->continuation_lines_width = 0;
6013
6014 /* Move forward and see what y-distance we moved. First move to the
6015 start of the next line so that we get its height. We need this
6016 height to be able to tell whether we reached the specified
6017 y-distance. */
6018 it2 = *it;
6019 it2.max_ascent = it2.max_descent = 0;
6020 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6021 MOVE_TO_POS | MOVE_TO_VPOS);
6022 xassert (IT_CHARPOS (*it) >= BEGV);
6023 it3 = it2;
6024
6025 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6026 xassert (IT_CHARPOS (*it) >= BEGV);
6027 /* H is the actual vertical distance from the position in *IT
6028 and the starting position. */
6029 h = it2.current_y - it->current_y;
6030 /* NLINES is the distance in number of lines. */
6031 nlines = it2.vpos - it->vpos;
6032
6033 /* Correct IT's y and vpos position
6034 so that they are relative to the starting point. */
6035 it->vpos -= nlines;
6036 it->current_y -= h;
6037
6038 if (dy == 0)
6039 {
6040 /* DY == 0 means move to the start of the screen line. The
6041 value of nlines is > 0 if continuation lines were involved. */
6042 if (nlines > 0)
6043 move_it_by_lines (it, nlines, 1);
6044 xassert (IT_CHARPOS (*it) <= start_pos);
6045 }
6046 else
6047 {
6048 /* The y-position we try to reach, relative to *IT.
6049 Note that H has been subtracted in front of the if-statement. */
6050 int target_y = it->current_y + h - dy;
6051 int y0 = it3.current_y;
6052 int y1 = line_bottom_y (&it3);
6053 int line_height = y1 - y0;
6054
6055 /* If we did not reach target_y, try to move further backward if
6056 we can. If we moved too far backward, try to move forward. */
6057 if (target_y < it->current_y
6058 /* This is heuristic. In a window that's 3 lines high, with
6059 a line height of 13 pixels each, recentering with point
6060 on the bottom line will try to move -39/2 = 19 pixels
6061 backward. Try to avoid moving into the first line. */
6062 && it->current_y - target_y > line_height / 3 * 2
6063 && IT_CHARPOS (*it) > BEGV)
6064 {
6065 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6066 target_y - it->current_y));
6067 move_it_vertically (it, target_y - it->current_y);
6068 xassert (IT_CHARPOS (*it) >= BEGV);
6069 }
6070 else if (target_y >= it->current_y + line_height
6071 && IT_CHARPOS (*it) < ZV)
6072 {
6073 /* Should move forward by at least one line, maybe more.
6074
6075 Note: Calling move_it_by_lines can be expensive on
6076 terminal frames, where compute_motion is used (via
6077 vmotion) to do the job, when there are very long lines
6078 and truncate-lines is nil. That's the reason for
6079 treating terminal frames specially here. */
6080
6081 if (!FRAME_WINDOW_P (it->f))
6082 move_it_vertically (it, target_y - (it->current_y + line_height));
6083 else
6084 {
6085 do
6086 {
6087 move_it_by_lines (it, 1, 1);
6088 }
6089 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6090 }
6091
6092 xassert (IT_CHARPOS (*it) >= BEGV);
6093 }
6094 }
6095 }
6096
6097
6098 /* Move IT by a specified amount of pixel lines DY. DY negative means
6099 move backwards. DY = 0 means move to start of screen line. At the
6100 end, IT will be on the start of a screen line. */
6101
6102 void
6103 move_it_vertically (it, dy)
6104 struct it *it;
6105 int dy;
6106 {
6107 if (dy <= 0)
6108 move_it_vertically_backward (it, -dy);
6109 else if (dy > 0)
6110 {
6111 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6112 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6113 MOVE_TO_POS | MOVE_TO_Y);
6114 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6115
6116 /* If buffer ends in ZV without a newline, move to the start of
6117 the line to satisfy the post-condition. */
6118 if (IT_CHARPOS (*it) == ZV
6119 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6120 move_it_by_lines (it, 0, 0);
6121 }
6122 }
6123
6124
6125 /* Move iterator IT past the end of the text line it is in. */
6126
6127 void
6128 move_it_past_eol (it)
6129 struct it *it;
6130 {
6131 enum move_it_result rc;
6132
6133 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6134 if (rc == MOVE_NEWLINE_OR_CR)
6135 set_iterator_to_next (it, 0);
6136 }
6137
6138
6139 #if 0 /* Currently not used. */
6140
6141 /* Return non-zero if some text between buffer positions START_CHARPOS
6142 and END_CHARPOS is invisible. IT->window is the window for text
6143 property lookup. */
6144
6145 static int
6146 invisible_text_between_p (it, start_charpos, end_charpos)
6147 struct it *it;
6148 int start_charpos, end_charpos;
6149 {
6150 Lisp_Object prop, limit;
6151 int invisible_found_p;
6152
6153 xassert (it != NULL && start_charpos <= end_charpos);
6154
6155 /* Is text at START invisible? */
6156 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6157 it->window);
6158 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6159 invisible_found_p = 1;
6160 else
6161 {
6162 limit = Fnext_single_char_property_change (make_number (start_charpos),
6163 Qinvisible, Qnil,
6164 make_number (end_charpos));
6165 invisible_found_p = XFASTINT (limit) < end_charpos;
6166 }
6167
6168 return invisible_found_p;
6169 }
6170
6171 #endif /* 0 */
6172
6173
6174 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6175 negative means move up. DVPOS == 0 means move to the start of the
6176 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6177 NEED_Y_P is zero, IT->current_y will be left unchanged.
6178
6179 Further optimization ideas: If we would know that IT->f doesn't use
6180 a face with proportional font, we could be faster for
6181 truncate-lines nil. */
6182
6183 void
6184 move_it_by_lines (it, dvpos, need_y_p)
6185 struct it *it;
6186 int dvpos, need_y_p;
6187 {
6188 struct position pos;
6189
6190 if (!FRAME_WINDOW_P (it->f))
6191 {
6192 struct text_pos textpos;
6193
6194 /* We can use vmotion on frames without proportional fonts. */
6195 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6196 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6197 reseat (it, textpos, 1);
6198 it->vpos += pos.vpos;
6199 it->current_y += pos.vpos;
6200 }
6201 else if (dvpos == 0)
6202 {
6203 /* DVPOS == 0 means move to the start of the screen line. */
6204 move_it_vertically_backward (it, 0);
6205 xassert (it->current_x == 0 && it->hpos == 0);
6206 }
6207 else if (dvpos > 0)
6208 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6209 else
6210 {
6211 struct it it2;
6212 int start_charpos, i;
6213
6214 /* Start at the beginning of the screen line containing IT's
6215 position. */
6216 move_it_vertically_backward (it, 0);
6217
6218 /* Go back -DVPOS visible lines and reseat the iterator there. */
6219 start_charpos = IT_CHARPOS (*it);
6220 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6221 back_to_previous_visible_line_start (it);
6222 reseat (it, it->current.pos, 1);
6223 it->current_x = it->hpos = 0;
6224
6225 /* Above call may have moved too far if continuation lines
6226 are involved. Scan forward and see if it did. */
6227 it2 = *it;
6228 it2.vpos = it2.current_y = 0;
6229 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6230 it->vpos -= it2.vpos;
6231 it->current_y -= it2.current_y;
6232 it->current_x = it->hpos = 0;
6233
6234 /* If we moved too far, move IT some lines forward. */
6235 if (it2.vpos > -dvpos)
6236 {
6237 int delta = it2.vpos + dvpos;
6238 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6239 }
6240 }
6241 }
6242
6243 /* Return 1 if IT points into the middle of a display vector. */
6244
6245 int
6246 in_display_vector_p (it)
6247 struct it *it;
6248 {
6249 return (it->method == next_element_from_display_vector
6250 && it->current.dpvec_index > 0
6251 && it->dpvec + it->current.dpvec_index != it->dpend);
6252 }
6253
6254 \f
6255 /***********************************************************************
6256 Messages
6257 ***********************************************************************/
6258
6259
6260 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6261 to *Messages*. */
6262
6263 void
6264 add_to_log (format, arg1, arg2)
6265 char *format;
6266 Lisp_Object arg1, arg2;
6267 {
6268 Lisp_Object args[3];
6269 Lisp_Object msg, fmt;
6270 char *buffer;
6271 int len;
6272 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6273
6274 /* Do nothing if called asynchronously. Inserting text into
6275 a buffer may call after-change-functions and alike and
6276 that would means running Lisp asynchronously. */
6277 if (handling_signal)
6278 return;
6279
6280 fmt = msg = Qnil;
6281 GCPRO4 (fmt, msg, arg1, arg2);
6282
6283 args[0] = fmt = build_string (format);
6284 args[1] = arg1;
6285 args[2] = arg2;
6286 msg = Fformat (3, args);
6287
6288 len = SBYTES (msg) + 1;
6289 buffer = (char *) alloca (len);
6290 bcopy (SDATA (msg), buffer, len);
6291
6292 message_dolog (buffer, len - 1, 1, 0);
6293 UNGCPRO;
6294 }
6295
6296
6297 /* Output a newline in the *Messages* buffer if "needs" one. */
6298
6299 void
6300 message_log_maybe_newline ()
6301 {
6302 if (message_log_need_newline)
6303 message_dolog ("", 0, 1, 0);
6304 }
6305
6306
6307 /* Add a string M of length NBYTES to the message log, optionally
6308 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6309 nonzero, means interpret the contents of M as multibyte. This
6310 function calls low-level routines in order to bypass text property
6311 hooks, etc. which might not be safe to run. */
6312
6313 void
6314 message_dolog (m, nbytes, nlflag, multibyte)
6315 const char *m;
6316 int nbytes, nlflag, multibyte;
6317 {
6318 if (!NILP (Vmemory_full))
6319 return;
6320
6321 if (!NILP (Vmessage_log_max))
6322 {
6323 struct buffer *oldbuf;
6324 Lisp_Object oldpoint, oldbegv, oldzv;
6325 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6326 int point_at_end = 0;
6327 int zv_at_end = 0;
6328 Lisp_Object old_deactivate_mark, tem;
6329 struct gcpro gcpro1;
6330
6331 old_deactivate_mark = Vdeactivate_mark;
6332 oldbuf = current_buffer;
6333 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6334 current_buffer->undo_list = Qt;
6335
6336 oldpoint = message_dolog_marker1;
6337 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6338 oldbegv = message_dolog_marker2;
6339 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6340 oldzv = message_dolog_marker3;
6341 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6342 GCPRO1 (old_deactivate_mark);
6343
6344 if (PT == Z)
6345 point_at_end = 1;
6346 if (ZV == Z)
6347 zv_at_end = 1;
6348
6349 BEGV = BEG;
6350 BEGV_BYTE = BEG_BYTE;
6351 ZV = Z;
6352 ZV_BYTE = Z_BYTE;
6353 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6354
6355 /* Insert the string--maybe converting multibyte to single byte
6356 or vice versa, so that all the text fits the buffer. */
6357 if (multibyte
6358 && NILP (current_buffer->enable_multibyte_characters))
6359 {
6360 int i, c, char_bytes;
6361 unsigned char work[1];
6362
6363 /* Convert a multibyte string to single-byte
6364 for the *Message* buffer. */
6365 for (i = 0; i < nbytes; i += char_bytes)
6366 {
6367 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6368 work[0] = (SINGLE_BYTE_CHAR_P (c)
6369 ? c
6370 : multibyte_char_to_unibyte (c, Qnil));
6371 insert_1_both (work, 1, 1, 1, 0, 0);
6372 }
6373 }
6374 else if (! multibyte
6375 && ! NILP (current_buffer->enable_multibyte_characters))
6376 {
6377 int i, c, char_bytes;
6378 unsigned char *msg = (unsigned char *) m;
6379 unsigned char str[MAX_MULTIBYTE_LENGTH];
6380 /* Convert a single-byte string to multibyte
6381 for the *Message* buffer. */
6382 for (i = 0; i < nbytes; i++)
6383 {
6384 c = unibyte_char_to_multibyte (msg[i]);
6385 char_bytes = CHAR_STRING (c, str);
6386 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6387 }
6388 }
6389 else if (nbytes)
6390 insert_1 (m, nbytes, 1, 0, 0);
6391
6392 if (nlflag)
6393 {
6394 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6395 insert_1 ("\n", 1, 1, 0, 0);
6396
6397 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6398 this_bol = PT;
6399 this_bol_byte = PT_BYTE;
6400
6401 /* See if this line duplicates the previous one.
6402 If so, combine duplicates. */
6403 if (this_bol > BEG)
6404 {
6405 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6406 prev_bol = PT;
6407 prev_bol_byte = PT_BYTE;
6408
6409 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6410 this_bol, this_bol_byte);
6411 if (dup)
6412 {
6413 del_range_both (prev_bol, prev_bol_byte,
6414 this_bol, this_bol_byte, 0);
6415 if (dup > 1)
6416 {
6417 char dupstr[40];
6418 int duplen;
6419
6420 /* If you change this format, don't forget to also
6421 change message_log_check_duplicate. */
6422 sprintf (dupstr, " [%d times]", dup);
6423 duplen = strlen (dupstr);
6424 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6425 insert_1 (dupstr, duplen, 1, 0, 1);
6426 }
6427 }
6428 }
6429
6430 /* If we have more than the desired maximum number of lines
6431 in the *Messages* buffer now, delete the oldest ones.
6432 This is safe because we don't have undo in this buffer. */
6433
6434 if (NATNUMP (Vmessage_log_max))
6435 {
6436 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6437 -XFASTINT (Vmessage_log_max) - 1, 0);
6438 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6439 }
6440 }
6441 BEGV = XMARKER (oldbegv)->charpos;
6442 BEGV_BYTE = marker_byte_position (oldbegv);
6443
6444 if (zv_at_end)
6445 {
6446 ZV = Z;
6447 ZV_BYTE = Z_BYTE;
6448 }
6449 else
6450 {
6451 ZV = XMARKER (oldzv)->charpos;
6452 ZV_BYTE = marker_byte_position (oldzv);
6453 }
6454
6455 if (point_at_end)
6456 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6457 else
6458 /* We can't do Fgoto_char (oldpoint) because it will run some
6459 Lisp code. */
6460 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6461 XMARKER (oldpoint)->bytepos);
6462
6463 UNGCPRO;
6464 unchain_marker (XMARKER (oldpoint));
6465 unchain_marker (XMARKER (oldbegv));
6466 unchain_marker (XMARKER (oldzv));
6467
6468 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6469 set_buffer_internal (oldbuf);
6470 if (NILP (tem))
6471 windows_or_buffers_changed = old_windows_or_buffers_changed;
6472 message_log_need_newline = !nlflag;
6473 Vdeactivate_mark = old_deactivate_mark;
6474 }
6475 }
6476
6477
6478 /* We are at the end of the buffer after just having inserted a newline.
6479 (Note: We depend on the fact we won't be crossing the gap.)
6480 Check to see if the most recent message looks a lot like the previous one.
6481 Return 0 if different, 1 if the new one should just replace it, or a
6482 value N > 1 if we should also append " [N times]". */
6483
6484 static int
6485 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6486 int prev_bol, this_bol;
6487 int prev_bol_byte, this_bol_byte;
6488 {
6489 int i;
6490 int len = Z_BYTE - 1 - this_bol_byte;
6491 int seen_dots = 0;
6492 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6493 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6494
6495 for (i = 0; i < len; i++)
6496 {
6497 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6498 seen_dots = 1;
6499 if (p1[i] != p2[i])
6500 return seen_dots;
6501 }
6502 p1 += len;
6503 if (*p1 == '\n')
6504 return 2;
6505 if (*p1++ == ' ' && *p1++ == '[')
6506 {
6507 int n = 0;
6508 while (*p1 >= '0' && *p1 <= '9')
6509 n = n * 10 + *p1++ - '0';
6510 if (strncmp (p1, " times]\n", 8) == 0)
6511 return n+1;
6512 }
6513 return 0;
6514 }
6515
6516
6517 /* Display an echo area message M with a specified length of NBYTES
6518 bytes. The string may include null characters. If M is 0, clear
6519 out any existing message, and let the mini-buffer text show
6520 through.
6521
6522 The buffer M must continue to exist until after the echo area gets
6523 cleared or some other message gets displayed there. This means do
6524 not pass text that is stored in a Lisp string; do not pass text in
6525 a buffer that was alloca'd. */
6526
6527 void
6528 message2 (m, nbytes, multibyte)
6529 const char *m;
6530 int nbytes;
6531 int multibyte;
6532 {
6533 /* First flush out any partial line written with print. */
6534 message_log_maybe_newline ();
6535 if (m)
6536 message_dolog (m, nbytes, 1, multibyte);
6537 message2_nolog (m, nbytes, multibyte);
6538 }
6539
6540
6541 /* The non-logging counterpart of message2. */
6542
6543 void
6544 message2_nolog (m, nbytes, multibyte)
6545 const char *m;
6546 int nbytes, multibyte;
6547 {
6548 struct frame *sf = SELECTED_FRAME ();
6549 message_enable_multibyte = multibyte;
6550
6551 if (noninteractive)
6552 {
6553 if (noninteractive_need_newline)
6554 putc ('\n', stderr);
6555 noninteractive_need_newline = 0;
6556 if (m)
6557 fwrite (m, nbytes, 1, stderr);
6558 if (cursor_in_echo_area == 0)
6559 fprintf (stderr, "\n");
6560 fflush (stderr);
6561 }
6562 /* A null message buffer means that the frame hasn't really been
6563 initialized yet. Error messages get reported properly by
6564 cmd_error, so this must be just an informative message; toss it. */
6565 else if (INTERACTIVE
6566 && sf->glyphs_initialized_p
6567 && FRAME_MESSAGE_BUF (sf))
6568 {
6569 Lisp_Object mini_window;
6570 struct frame *f;
6571
6572 /* Get the frame containing the mini-buffer
6573 that the selected frame is using. */
6574 mini_window = FRAME_MINIBUF_WINDOW (sf);
6575 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6576
6577 FRAME_SAMPLE_VISIBILITY (f);
6578 if (FRAME_VISIBLE_P (sf)
6579 && ! FRAME_VISIBLE_P (f))
6580 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6581
6582 if (m)
6583 {
6584 set_message (m, Qnil, nbytes, multibyte);
6585 if (minibuffer_auto_raise)
6586 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6587 }
6588 else
6589 clear_message (1, 1);
6590
6591 do_pending_window_change (0);
6592 echo_area_display (1);
6593 do_pending_window_change (0);
6594 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6595 (*frame_up_to_date_hook) (f);
6596 }
6597 }
6598
6599
6600 /* Display an echo area message M with a specified length of NBYTES
6601 bytes. The string may include null characters. If M is not a
6602 string, clear out any existing message, and let the mini-buffer
6603 text show through. */
6604
6605 void
6606 message3 (m, nbytes, multibyte)
6607 Lisp_Object m;
6608 int nbytes;
6609 int multibyte;
6610 {
6611 struct gcpro gcpro1;
6612
6613 GCPRO1 (m);
6614
6615 /* First flush out any partial line written with print. */
6616 message_log_maybe_newline ();
6617 if (STRINGP (m))
6618 message_dolog (SDATA (m), nbytes, 1, multibyte);
6619 message3_nolog (m, nbytes, multibyte);
6620
6621 UNGCPRO;
6622 }
6623
6624
6625 /* The non-logging version of message3. */
6626
6627 void
6628 message3_nolog (m, nbytes, multibyte)
6629 Lisp_Object m;
6630 int nbytes, multibyte;
6631 {
6632 struct frame *sf = SELECTED_FRAME ();
6633 message_enable_multibyte = multibyte;
6634
6635 if (noninteractive)
6636 {
6637 if (noninteractive_need_newline)
6638 putc ('\n', stderr);
6639 noninteractive_need_newline = 0;
6640 if (STRINGP (m))
6641 fwrite (SDATA (m), nbytes, 1, stderr);
6642 if (cursor_in_echo_area == 0)
6643 fprintf (stderr, "\n");
6644 fflush (stderr);
6645 }
6646 /* A null message buffer means that the frame hasn't really been
6647 initialized yet. Error messages get reported properly by
6648 cmd_error, so this must be just an informative message; toss it. */
6649 else if (INTERACTIVE
6650 && sf->glyphs_initialized_p
6651 && FRAME_MESSAGE_BUF (sf))
6652 {
6653 Lisp_Object mini_window;
6654 Lisp_Object frame;
6655 struct frame *f;
6656
6657 /* Get the frame containing the mini-buffer
6658 that the selected frame is using. */
6659 mini_window = FRAME_MINIBUF_WINDOW (sf);
6660 frame = XWINDOW (mini_window)->frame;
6661 f = XFRAME (frame);
6662
6663 FRAME_SAMPLE_VISIBILITY (f);
6664 if (FRAME_VISIBLE_P (sf)
6665 && !FRAME_VISIBLE_P (f))
6666 Fmake_frame_visible (frame);
6667
6668 if (STRINGP (m) && SCHARS (m) > 0)
6669 {
6670 set_message (NULL, m, nbytes, multibyte);
6671 if (minibuffer_auto_raise)
6672 Fraise_frame (frame);
6673 }
6674 else
6675 clear_message (1, 1);
6676
6677 do_pending_window_change (0);
6678 echo_area_display (1);
6679 do_pending_window_change (0);
6680 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6681 (*frame_up_to_date_hook) (f);
6682 }
6683 }
6684
6685
6686 /* Display a null-terminated echo area message M. If M is 0, clear
6687 out any existing message, and let the mini-buffer text show through.
6688
6689 The buffer M must continue to exist until after the echo area gets
6690 cleared or some other message gets displayed there. Do not pass
6691 text that is stored in a Lisp string. Do not pass text in a buffer
6692 that was alloca'd. */
6693
6694 void
6695 message1 (m)
6696 char *m;
6697 {
6698 message2 (m, (m ? strlen (m) : 0), 0);
6699 }
6700
6701
6702 /* The non-logging counterpart of message1. */
6703
6704 void
6705 message1_nolog (m)
6706 char *m;
6707 {
6708 message2_nolog (m, (m ? strlen (m) : 0), 0);
6709 }
6710
6711 /* Display a message M which contains a single %s
6712 which gets replaced with STRING. */
6713
6714 void
6715 message_with_string (m, string, log)
6716 char *m;
6717 Lisp_Object string;
6718 int log;
6719 {
6720 CHECK_STRING (string);
6721
6722 if (noninteractive)
6723 {
6724 if (m)
6725 {
6726 if (noninteractive_need_newline)
6727 putc ('\n', stderr);
6728 noninteractive_need_newline = 0;
6729 fprintf (stderr, m, SDATA (string));
6730 if (cursor_in_echo_area == 0)
6731 fprintf (stderr, "\n");
6732 fflush (stderr);
6733 }
6734 }
6735 else if (INTERACTIVE)
6736 {
6737 /* The frame whose minibuffer we're going to display the message on.
6738 It may be larger than the selected frame, so we need
6739 to use its buffer, not the selected frame's buffer. */
6740 Lisp_Object mini_window;
6741 struct frame *f, *sf = SELECTED_FRAME ();
6742
6743 /* Get the frame containing the minibuffer
6744 that the selected frame is using. */
6745 mini_window = FRAME_MINIBUF_WINDOW (sf);
6746 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6747
6748 /* A null message buffer means that the frame hasn't really been
6749 initialized yet. Error messages get reported properly by
6750 cmd_error, so this must be just an informative message; toss it. */
6751 if (FRAME_MESSAGE_BUF (f))
6752 {
6753 Lisp_Object args[2], message;
6754 struct gcpro gcpro1, gcpro2;
6755
6756 args[0] = build_string (m);
6757 args[1] = message = string;
6758 GCPRO2 (args[0], message);
6759 gcpro1.nvars = 2;
6760
6761 message = Fformat (2, args);
6762
6763 if (log)
6764 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6765 else
6766 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6767
6768 UNGCPRO;
6769
6770 /* Print should start at the beginning of the message
6771 buffer next time. */
6772 message_buf_print = 0;
6773 }
6774 }
6775 }
6776
6777
6778 /* Dump an informative message to the minibuf. If M is 0, clear out
6779 any existing message, and let the mini-buffer text show through. */
6780
6781 /* VARARGS 1 */
6782 void
6783 message (m, a1, a2, a3)
6784 char *m;
6785 EMACS_INT a1, a2, a3;
6786 {
6787 if (noninteractive)
6788 {
6789 if (m)
6790 {
6791 if (noninteractive_need_newline)
6792 putc ('\n', stderr);
6793 noninteractive_need_newline = 0;
6794 fprintf (stderr, m, a1, a2, a3);
6795 if (cursor_in_echo_area == 0)
6796 fprintf (stderr, "\n");
6797 fflush (stderr);
6798 }
6799 }
6800 else if (INTERACTIVE)
6801 {
6802 /* The frame whose mini-buffer we're going to display the message
6803 on. It may be larger than the selected frame, so we need to
6804 use its buffer, not the selected frame's buffer. */
6805 Lisp_Object mini_window;
6806 struct frame *f, *sf = SELECTED_FRAME ();
6807
6808 /* Get the frame containing the mini-buffer
6809 that the selected frame is using. */
6810 mini_window = FRAME_MINIBUF_WINDOW (sf);
6811 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6812
6813 /* A null message buffer means that the frame hasn't really been
6814 initialized yet. Error messages get reported properly by
6815 cmd_error, so this must be just an informative message; toss
6816 it. */
6817 if (FRAME_MESSAGE_BUF (f))
6818 {
6819 if (m)
6820 {
6821 int len;
6822 #ifdef NO_ARG_ARRAY
6823 char *a[3];
6824 a[0] = (char *) a1;
6825 a[1] = (char *) a2;
6826 a[2] = (char *) a3;
6827
6828 len = doprnt (FRAME_MESSAGE_BUF (f),
6829 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6830 #else
6831 len = doprnt (FRAME_MESSAGE_BUF (f),
6832 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6833 (char **) &a1);
6834 #endif /* NO_ARG_ARRAY */
6835
6836 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6837 }
6838 else
6839 message1 (0);
6840
6841 /* Print should start at the beginning of the message
6842 buffer next time. */
6843 message_buf_print = 0;
6844 }
6845 }
6846 }
6847
6848
6849 /* The non-logging version of message. */
6850
6851 void
6852 message_nolog (m, a1, a2, a3)
6853 char *m;
6854 EMACS_INT a1, a2, a3;
6855 {
6856 Lisp_Object old_log_max;
6857 old_log_max = Vmessage_log_max;
6858 Vmessage_log_max = Qnil;
6859 message (m, a1, a2, a3);
6860 Vmessage_log_max = old_log_max;
6861 }
6862
6863
6864 /* Display the current message in the current mini-buffer. This is
6865 only called from error handlers in process.c, and is not time
6866 critical. */
6867
6868 void
6869 update_echo_area ()
6870 {
6871 if (!NILP (echo_area_buffer[0]))
6872 {
6873 Lisp_Object string;
6874 string = Fcurrent_message ();
6875 message3 (string, SBYTES (string),
6876 !NILP (current_buffer->enable_multibyte_characters));
6877 }
6878 }
6879
6880
6881 /* Make sure echo area buffers in `echo_buffers' are live.
6882 If they aren't, make new ones. */
6883
6884 static void
6885 ensure_echo_area_buffers ()
6886 {
6887 int i;
6888
6889 for (i = 0; i < 2; ++i)
6890 if (!BUFFERP (echo_buffer[i])
6891 || NILP (XBUFFER (echo_buffer[i])->name))
6892 {
6893 char name[30];
6894 Lisp_Object old_buffer;
6895 int j;
6896
6897 old_buffer = echo_buffer[i];
6898 sprintf (name, " *Echo Area %d*", i);
6899 echo_buffer[i] = Fget_buffer_create (build_string (name));
6900 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6901
6902 for (j = 0; j < 2; ++j)
6903 if (EQ (old_buffer, echo_area_buffer[j]))
6904 echo_area_buffer[j] = echo_buffer[i];
6905 }
6906 }
6907
6908
6909 /* Call FN with args A1..A4 with either the current or last displayed
6910 echo_area_buffer as current buffer.
6911
6912 WHICH zero means use the current message buffer
6913 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6914 from echo_buffer[] and clear it.
6915
6916 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6917 suitable buffer from echo_buffer[] and clear it.
6918
6919 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6920 that the current message becomes the last displayed one, make
6921 choose a suitable buffer for echo_area_buffer[0], and clear it.
6922
6923 Value is what FN returns. */
6924
6925 static int
6926 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6927 struct window *w;
6928 int which;
6929 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6930 EMACS_INT a1;
6931 Lisp_Object a2;
6932 EMACS_INT a3, a4;
6933 {
6934 Lisp_Object buffer;
6935 int this_one, the_other, clear_buffer_p, rc;
6936 int count = SPECPDL_INDEX ();
6937
6938 /* If buffers aren't live, make new ones. */
6939 ensure_echo_area_buffers ();
6940
6941 clear_buffer_p = 0;
6942
6943 if (which == 0)
6944 this_one = 0, the_other = 1;
6945 else if (which > 0)
6946 this_one = 1, the_other = 0;
6947 else
6948 {
6949 this_one = 0, the_other = 1;
6950 clear_buffer_p = 1;
6951
6952 /* We need a fresh one in case the current echo buffer equals
6953 the one containing the last displayed echo area message. */
6954 if (!NILP (echo_area_buffer[this_one])
6955 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6956 echo_area_buffer[this_one] = Qnil;
6957 }
6958
6959 /* Choose a suitable buffer from echo_buffer[] is we don't
6960 have one. */
6961 if (NILP (echo_area_buffer[this_one]))
6962 {
6963 echo_area_buffer[this_one]
6964 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6965 ? echo_buffer[the_other]
6966 : echo_buffer[this_one]);
6967 clear_buffer_p = 1;
6968 }
6969
6970 buffer = echo_area_buffer[this_one];
6971
6972 /* Don't get confused by reusing the buffer used for echoing
6973 for a different purpose. */
6974 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6975 cancel_echoing ();
6976
6977 record_unwind_protect (unwind_with_echo_area_buffer,
6978 with_echo_area_buffer_unwind_data (w));
6979
6980 /* Make the echo area buffer current. Note that for display
6981 purposes, it is not necessary that the displayed window's buffer
6982 == current_buffer, except for text property lookup. So, let's
6983 only set that buffer temporarily here without doing a full
6984 Fset_window_buffer. We must also change w->pointm, though,
6985 because otherwise an assertions in unshow_buffer fails, and Emacs
6986 aborts. */
6987 set_buffer_internal_1 (XBUFFER (buffer));
6988 if (w)
6989 {
6990 w->buffer = buffer;
6991 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6992 }
6993
6994 current_buffer->undo_list = Qt;
6995 current_buffer->read_only = Qnil;
6996 specbind (Qinhibit_read_only, Qt);
6997 specbind (Qinhibit_modification_hooks, Qt);
6998
6999 if (clear_buffer_p && Z > BEG)
7000 del_range (BEG, Z);
7001
7002 xassert (BEGV >= BEG);
7003 xassert (ZV <= Z && ZV >= BEGV);
7004
7005 rc = fn (a1, a2, a3, a4);
7006
7007 xassert (BEGV >= BEG);
7008 xassert (ZV <= Z && ZV >= BEGV);
7009
7010 unbind_to (count, Qnil);
7011 return rc;
7012 }
7013
7014
7015 /* Save state that should be preserved around the call to the function
7016 FN called in with_echo_area_buffer. */
7017
7018 static Lisp_Object
7019 with_echo_area_buffer_unwind_data (w)
7020 struct window *w;
7021 {
7022 int i = 0;
7023 Lisp_Object vector;
7024
7025 /* Reduce consing by keeping one vector in
7026 Vwith_echo_area_save_vector. */
7027 vector = Vwith_echo_area_save_vector;
7028 Vwith_echo_area_save_vector = Qnil;
7029
7030 if (NILP (vector))
7031 vector = Fmake_vector (make_number (7), Qnil);
7032
7033 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7034 AREF (vector, i) = Vdeactivate_mark, ++i;
7035 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7036
7037 if (w)
7038 {
7039 XSETWINDOW (AREF (vector, i), w); ++i;
7040 AREF (vector, i) = w->buffer; ++i;
7041 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7042 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7043 }
7044 else
7045 {
7046 int end = i + 4;
7047 for (; i < end; ++i)
7048 AREF (vector, i) = Qnil;
7049 }
7050
7051 xassert (i == ASIZE (vector));
7052 return vector;
7053 }
7054
7055
7056 /* Restore global state from VECTOR which was created by
7057 with_echo_area_buffer_unwind_data. */
7058
7059 static Lisp_Object
7060 unwind_with_echo_area_buffer (vector)
7061 Lisp_Object vector;
7062 {
7063 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7064 Vdeactivate_mark = AREF (vector, 1);
7065 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7066
7067 if (WINDOWP (AREF (vector, 3)))
7068 {
7069 struct window *w;
7070 Lisp_Object buffer, charpos, bytepos;
7071
7072 w = XWINDOW (AREF (vector, 3));
7073 buffer = AREF (vector, 4);
7074 charpos = AREF (vector, 5);
7075 bytepos = AREF (vector, 6);
7076
7077 w->buffer = buffer;
7078 set_marker_both (w->pointm, buffer,
7079 XFASTINT (charpos), XFASTINT (bytepos));
7080 }
7081
7082 Vwith_echo_area_save_vector = vector;
7083 return Qnil;
7084 }
7085
7086
7087 /* Set up the echo area for use by print functions. MULTIBYTE_P
7088 non-zero means we will print multibyte. */
7089
7090 void
7091 setup_echo_area_for_printing (multibyte_p)
7092 int multibyte_p;
7093 {
7094 /* If we can't find an echo area any more, exit. */
7095 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7096 Fkill_emacs (Qnil);
7097
7098 ensure_echo_area_buffers ();
7099
7100 if (!message_buf_print)
7101 {
7102 /* A message has been output since the last time we printed.
7103 Choose a fresh echo area buffer. */
7104 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7105 echo_area_buffer[0] = echo_buffer[1];
7106 else
7107 echo_area_buffer[0] = echo_buffer[0];
7108
7109 /* Switch to that buffer and clear it. */
7110 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7111 current_buffer->truncate_lines = Qnil;
7112
7113 if (Z > BEG)
7114 {
7115 int count = SPECPDL_INDEX ();
7116 specbind (Qinhibit_read_only, Qt);
7117 /* Note that undo recording is always disabled. */
7118 del_range (BEG, Z);
7119 unbind_to (count, Qnil);
7120 }
7121 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7122
7123 /* Set up the buffer for the multibyteness we need. */
7124 if (multibyte_p
7125 != !NILP (current_buffer->enable_multibyte_characters))
7126 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7127
7128 /* Raise the frame containing the echo area. */
7129 if (minibuffer_auto_raise)
7130 {
7131 struct frame *sf = SELECTED_FRAME ();
7132 Lisp_Object mini_window;
7133 mini_window = FRAME_MINIBUF_WINDOW (sf);
7134 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7135 }
7136
7137 message_log_maybe_newline ();
7138 message_buf_print = 1;
7139 }
7140 else
7141 {
7142 if (NILP (echo_area_buffer[0]))
7143 {
7144 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7145 echo_area_buffer[0] = echo_buffer[1];
7146 else
7147 echo_area_buffer[0] = echo_buffer[0];
7148 }
7149
7150 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7151 {
7152 /* Someone switched buffers between print requests. */
7153 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7154 current_buffer->truncate_lines = Qnil;
7155 }
7156 }
7157 }
7158
7159
7160 /* Display an echo area message in window W. Value is non-zero if W's
7161 height is changed. If display_last_displayed_message_p is
7162 non-zero, display the message that was last displayed, otherwise
7163 display the current message. */
7164
7165 static int
7166 display_echo_area (w)
7167 struct window *w;
7168 {
7169 int i, no_message_p, window_height_changed_p, count;
7170
7171 /* Temporarily disable garbage collections while displaying the echo
7172 area. This is done because a GC can print a message itself.
7173 That message would modify the echo area buffer's contents while a
7174 redisplay of the buffer is going on, and seriously confuse
7175 redisplay. */
7176 count = inhibit_garbage_collection ();
7177
7178 /* If there is no message, we must call display_echo_area_1
7179 nevertheless because it resizes the window. But we will have to
7180 reset the echo_area_buffer in question to nil at the end because
7181 with_echo_area_buffer will sets it to an empty buffer. */
7182 i = display_last_displayed_message_p ? 1 : 0;
7183 no_message_p = NILP (echo_area_buffer[i]);
7184
7185 window_height_changed_p
7186 = with_echo_area_buffer (w, display_last_displayed_message_p,
7187 display_echo_area_1,
7188 (EMACS_INT) w, Qnil, 0, 0);
7189
7190 if (no_message_p)
7191 echo_area_buffer[i] = Qnil;
7192
7193 unbind_to (count, Qnil);
7194 return window_height_changed_p;
7195 }
7196
7197
7198 /* Helper for display_echo_area. Display the current buffer which
7199 contains the current echo area message in window W, a mini-window,
7200 a pointer to which is passed in A1. A2..A4 are currently not used.
7201 Change the height of W so that all of the message is displayed.
7202 Value is non-zero if height of W was changed. */
7203
7204 static int
7205 display_echo_area_1 (a1, a2, a3, a4)
7206 EMACS_INT a1;
7207 Lisp_Object a2;
7208 EMACS_INT a3, a4;
7209 {
7210 struct window *w = (struct window *) a1;
7211 Lisp_Object window;
7212 struct text_pos start;
7213 int window_height_changed_p = 0;
7214
7215 /* Do this before displaying, so that we have a large enough glyph
7216 matrix for the display. */
7217 window_height_changed_p = resize_mini_window (w, 0);
7218
7219 /* Display. */
7220 clear_glyph_matrix (w->desired_matrix);
7221 XSETWINDOW (window, w);
7222 SET_TEXT_POS (start, BEG, BEG_BYTE);
7223 try_window (window, start);
7224
7225 return window_height_changed_p;
7226 }
7227
7228
7229 /* Resize the echo area window to exactly the size needed for the
7230 currently displayed message, if there is one. If a mini-buffer
7231 is active, don't shrink it. */
7232
7233 void
7234 resize_echo_area_exactly ()
7235 {
7236 if (BUFFERP (echo_area_buffer[0])
7237 && WINDOWP (echo_area_window))
7238 {
7239 struct window *w = XWINDOW (echo_area_window);
7240 int resized_p;
7241 Lisp_Object resize_exactly;
7242
7243 if (minibuf_level == 0)
7244 resize_exactly = Qt;
7245 else
7246 resize_exactly = Qnil;
7247
7248 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7249 (EMACS_INT) w, resize_exactly, 0, 0);
7250 if (resized_p)
7251 {
7252 ++windows_or_buffers_changed;
7253 ++update_mode_lines;
7254 redisplay_internal (0);
7255 }
7256 }
7257 }
7258
7259
7260 /* Callback function for with_echo_area_buffer, when used from
7261 resize_echo_area_exactly. A1 contains a pointer to the window to
7262 resize, EXACTLY non-nil means resize the mini-window exactly to the
7263 size of the text displayed. A3 and A4 are not used. Value is what
7264 resize_mini_window returns. */
7265
7266 static int
7267 resize_mini_window_1 (a1, exactly, a3, a4)
7268 EMACS_INT a1;
7269 Lisp_Object exactly;
7270 EMACS_INT a3, a4;
7271 {
7272 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7273 }
7274
7275
7276 /* Resize mini-window W to fit the size of its contents. EXACT:P
7277 means size the window exactly to the size needed. Otherwise, it's
7278 only enlarged until W's buffer is empty. Value is non-zero if
7279 the window height has been changed. */
7280
7281 int
7282 resize_mini_window (w, exact_p)
7283 struct window *w;
7284 int exact_p;
7285 {
7286 struct frame *f = XFRAME (w->frame);
7287 int window_height_changed_p = 0;
7288
7289 xassert (MINI_WINDOW_P (w));
7290
7291 /* Don't resize windows while redisplaying a window; it would
7292 confuse redisplay functions when the size of the window they are
7293 displaying changes from under them. Such a resizing can happen,
7294 for instance, when which-func prints a long message while
7295 we are running fontification-functions. We're running these
7296 functions with safe_call which binds inhibit-redisplay to t. */
7297 if (!NILP (Vinhibit_redisplay))
7298 return 0;
7299
7300 /* Nil means don't try to resize. */
7301 if (NILP (Vresize_mini_windows)
7302 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7303 return 0;
7304
7305 if (!FRAME_MINIBUF_ONLY_P (f))
7306 {
7307 struct it it;
7308 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7309 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7310 int height, max_height;
7311 int unit = FRAME_LINE_HEIGHT (f);
7312 struct text_pos start;
7313 struct buffer *old_current_buffer = NULL;
7314
7315 if (current_buffer != XBUFFER (w->buffer))
7316 {
7317 old_current_buffer = current_buffer;
7318 set_buffer_internal (XBUFFER (w->buffer));
7319 }
7320
7321 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7322
7323 /* Compute the max. number of lines specified by the user. */
7324 if (FLOATP (Vmax_mini_window_height))
7325 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7326 else if (INTEGERP (Vmax_mini_window_height))
7327 max_height = XINT (Vmax_mini_window_height);
7328 else
7329 max_height = total_height / 4;
7330
7331 /* Correct that max. height if it's bogus. */
7332 max_height = max (1, max_height);
7333 max_height = min (total_height, max_height);
7334
7335 /* Find out the height of the text in the window. */
7336 if (it.truncate_lines_p)
7337 height = 1;
7338 else
7339 {
7340 last_height = 0;
7341 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7342 if (it.max_ascent == 0 && it.max_descent == 0)
7343 height = it.current_y + last_height;
7344 else
7345 height = it.current_y + it.max_ascent + it.max_descent;
7346 height -= it.extra_line_spacing;
7347 height = (height + unit - 1) / unit;
7348 }
7349
7350 /* Compute a suitable window start. */
7351 if (height > max_height)
7352 {
7353 height = max_height;
7354 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7355 move_it_vertically_backward (&it, (height - 1) * unit);
7356 start = it.current.pos;
7357 }
7358 else
7359 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7360 SET_MARKER_FROM_TEXT_POS (w->start, start);
7361
7362 if (EQ (Vresize_mini_windows, Qgrow_only))
7363 {
7364 /* Let it grow only, until we display an empty message, in which
7365 case the window shrinks again. */
7366 if (height > WINDOW_TOTAL_LINES (w))
7367 {
7368 int old_height = WINDOW_TOTAL_LINES (w);
7369 freeze_window_starts (f, 1);
7370 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7371 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7372 }
7373 else if (height < WINDOW_TOTAL_LINES (w)
7374 && (exact_p || BEGV == ZV))
7375 {
7376 int old_height = WINDOW_TOTAL_LINES (w);
7377 freeze_window_starts (f, 0);
7378 shrink_mini_window (w);
7379 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7380 }
7381 }
7382 else
7383 {
7384 /* Always resize to exact size needed. */
7385 if (height > WINDOW_TOTAL_LINES (w))
7386 {
7387 int old_height = WINDOW_TOTAL_LINES (w);
7388 freeze_window_starts (f, 1);
7389 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7390 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7391 }
7392 else if (height < WINDOW_TOTAL_LINES (w))
7393 {
7394 int old_height = WINDOW_TOTAL_LINES (w);
7395 freeze_window_starts (f, 0);
7396 shrink_mini_window (w);
7397
7398 if (height)
7399 {
7400 freeze_window_starts (f, 1);
7401 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7402 }
7403
7404 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7405 }
7406 }
7407
7408 if (old_current_buffer)
7409 set_buffer_internal (old_current_buffer);
7410 }
7411
7412 return window_height_changed_p;
7413 }
7414
7415
7416 /* Value is the current message, a string, or nil if there is no
7417 current message. */
7418
7419 Lisp_Object
7420 current_message ()
7421 {
7422 Lisp_Object msg;
7423
7424 if (NILP (echo_area_buffer[0]))
7425 msg = Qnil;
7426 else
7427 {
7428 with_echo_area_buffer (0, 0, current_message_1,
7429 (EMACS_INT) &msg, Qnil, 0, 0);
7430 if (NILP (msg))
7431 echo_area_buffer[0] = Qnil;
7432 }
7433
7434 return msg;
7435 }
7436
7437
7438 static int
7439 current_message_1 (a1, a2, a3, a4)
7440 EMACS_INT a1;
7441 Lisp_Object a2;
7442 EMACS_INT a3, a4;
7443 {
7444 Lisp_Object *msg = (Lisp_Object *) a1;
7445
7446 if (Z > BEG)
7447 *msg = make_buffer_string (BEG, Z, 1);
7448 else
7449 *msg = Qnil;
7450 return 0;
7451 }
7452
7453
7454 /* Push the current message on Vmessage_stack for later restauration
7455 by restore_message. Value is non-zero if the current message isn't
7456 empty. This is a relatively infrequent operation, so it's not
7457 worth optimizing. */
7458
7459 int
7460 push_message ()
7461 {
7462 Lisp_Object msg;
7463 msg = current_message ();
7464 Vmessage_stack = Fcons (msg, Vmessage_stack);
7465 return STRINGP (msg);
7466 }
7467
7468
7469 /* Restore message display from the top of Vmessage_stack. */
7470
7471 void
7472 restore_message ()
7473 {
7474 Lisp_Object msg;
7475
7476 xassert (CONSP (Vmessage_stack));
7477 msg = XCAR (Vmessage_stack);
7478 if (STRINGP (msg))
7479 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7480 else
7481 message3_nolog (msg, 0, 0);
7482 }
7483
7484
7485 /* Handler for record_unwind_protect calling pop_message. */
7486
7487 Lisp_Object
7488 pop_message_unwind (dummy)
7489 Lisp_Object dummy;
7490 {
7491 pop_message ();
7492 return Qnil;
7493 }
7494
7495 /* Pop the top-most entry off Vmessage_stack. */
7496
7497 void
7498 pop_message ()
7499 {
7500 xassert (CONSP (Vmessage_stack));
7501 Vmessage_stack = XCDR (Vmessage_stack);
7502 }
7503
7504
7505 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7506 exits. If the stack is not empty, we have a missing pop_message
7507 somewhere. */
7508
7509 void
7510 check_message_stack ()
7511 {
7512 if (!NILP (Vmessage_stack))
7513 abort ();
7514 }
7515
7516
7517 /* Truncate to NCHARS what will be displayed in the echo area the next
7518 time we display it---but don't redisplay it now. */
7519
7520 void
7521 truncate_echo_area (nchars)
7522 int nchars;
7523 {
7524 if (nchars == 0)
7525 echo_area_buffer[0] = Qnil;
7526 /* A null message buffer means that the frame hasn't really been
7527 initialized yet. Error messages get reported properly by
7528 cmd_error, so this must be just an informative message; toss it. */
7529 else if (!noninteractive
7530 && INTERACTIVE
7531 && !NILP (echo_area_buffer[0]))
7532 {
7533 struct frame *sf = SELECTED_FRAME ();
7534 if (FRAME_MESSAGE_BUF (sf))
7535 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7536 }
7537 }
7538
7539
7540 /* Helper function for truncate_echo_area. Truncate the current
7541 message to at most NCHARS characters. */
7542
7543 static int
7544 truncate_message_1 (nchars, a2, a3, a4)
7545 EMACS_INT nchars;
7546 Lisp_Object a2;
7547 EMACS_INT a3, a4;
7548 {
7549 if (BEG + nchars < Z)
7550 del_range (BEG + nchars, Z);
7551 if (Z == BEG)
7552 echo_area_buffer[0] = Qnil;
7553 return 0;
7554 }
7555
7556
7557 /* Set the current message to a substring of S or STRING.
7558
7559 If STRING is a Lisp string, set the message to the first NBYTES
7560 bytes from STRING. NBYTES zero means use the whole string. If
7561 STRING is multibyte, the message will be displayed multibyte.
7562
7563 If S is not null, set the message to the first LEN bytes of S. LEN
7564 zero means use the whole string. MULTIBYTE_P non-zero means S is
7565 multibyte. Display the message multibyte in that case. */
7566
7567 void
7568 set_message (s, string, nbytes, multibyte_p)
7569 const char *s;
7570 Lisp_Object string;
7571 int nbytes, multibyte_p;
7572 {
7573 message_enable_multibyte
7574 = ((s && multibyte_p)
7575 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7576
7577 with_echo_area_buffer (0, -1, set_message_1,
7578 (EMACS_INT) s, string, nbytes, multibyte_p);
7579 message_buf_print = 0;
7580 help_echo_showing_p = 0;
7581 }
7582
7583
7584 /* Helper function for set_message. Arguments have the same meaning
7585 as there, with A1 corresponding to S and A2 corresponding to STRING
7586 This function is called with the echo area buffer being
7587 current. */
7588
7589 static int
7590 set_message_1 (a1, a2, nbytes, multibyte_p)
7591 EMACS_INT a1;
7592 Lisp_Object a2;
7593 EMACS_INT nbytes, multibyte_p;
7594 {
7595 const char *s = (const char *) a1;
7596 Lisp_Object string = a2;
7597
7598 xassert (BEG == Z);
7599
7600 /* Change multibyteness of the echo buffer appropriately. */
7601 if (message_enable_multibyte
7602 != !NILP (current_buffer->enable_multibyte_characters))
7603 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7604
7605 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7606
7607 /* Insert new message at BEG. */
7608 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7609
7610 if (STRINGP (string))
7611 {
7612 int nchars;
7613
7614 if (nbytes == 0)
7615 nbytes = SBYTES (string);
7616 nchars = string_byte_to_char (string, nbytes);
7617
7618 /* This function takes care of single/multibyte conversion. We
7619 just have to ensure that the echo area buffer has the right
7620 setting of enable_multibyte_characters. */
7621 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7622 }
7623 else if (s)
7624 {
7625 if (nbytes == 0)
7626 nbytes = strlen (s);
7627
7628 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7629 {
7630 /* Convert from multi-byte to single-byte. */
7631 int i, c, n;
7632 unsigned char work[1];
7633
7634 /* Convert a multibyte string to single-byte. */
7635 for (i = 0; i < nbytes; i += n)
7636 {
7637 c = string_char_and_length (s + i, nbytes - i, &n);
7638 work[0] = (SINGLE_BYTE_CHAR_P (c)
7639 ? c
7640 : multibyte_char_to_unibyte (c, Qnil));
7641 insert_1_both (work, 1, 1, 1, 0, 0);
7642 }
7643 }
7644 else if (!multibyte_p
7645 && !NILP (current_buffer->enable_multibyte_characters))
7646 {
7647 /* Convert from single-byte to multi-byte. */
7648 int i, c, n;
7649 const unsigned char *msg = (const unsigned char *) s;
7650 unsigned char str[MAX_MULTIBYTE_LENGTH];
7651
7652 /* Convert a single-byte string to multibyte. */
7653 for (i = 0; i < nbytes; i++)
7654 {
7655 c = unibyte_char_to_multibyte (msg[i]);
7656 n = CHAR_STRING (c, str);
7657 insert_1_both (str, 1, n, 1, 0, 0);
7658 }
7659 }
7660 else
7661 insert_1 (s, nbytes, 1, 0, 0);
7662 }
7663
7664 return 0;
7665 }
7666
7667
7668 /* Clear messages. CURRENT_P non-zero means clear the current
7669 message. LAST_DISPLAYED_P non-zero means clear the message
7670 last displayed. */
7671
7672 void
7673 clear_message (current_p, last_displayed_p)
7674 int current_p, last_displayed_p;
7675 {
7676 if (current_p)
7677 {
7678 echo_area_buffer[0] = Qnil;
7679 message_cleared_p = 1;
7680 }
7681
7682 if (last_displayed_p)
7683 echo_area_buffer[1] = Qnil;
7684
7685 message_buf_print = 0;
7686 }
7687
7688 /* Clear garbaged frames.
7689
7690 This function is used where the old redisplay called
7691 redraw_garbaged_frames which in turn called redraw_frame which in
7692 turn called clear_frame. The call to clear_frame was a source of
7693 flickering. I believe a clear_frame is not necessary. It should
7694 suffice in the new redisplay to invalidate all current matrices,
7695 and ensure a complete redisplay of all windows. */
7696
7697 static void
7698 clear_garbaged_frames ()
7699 {
7700 if (frame_garbaged)
7701 {
7702 Lisp_Object tail, frame;
7703 int changed_count = 0;
7704
7705 FOR_EACH_FRAME (tail, frame)
7706 {
7707 struct frame *f = XFRAME (frame);
7708
7709 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7710 {
7711 if (f->resized_p)
7712 {
7713 Fredraw_frame (frame);
7714 f->force_flush_display_p = 1;
7715 }
7716 clear_current_matrices (f);
7717 changed_count++;
7718 f->garbaged = 0;
7719 f->resized_p = 0;
7720 }
7721 }
7722
7723 frame_garbaged = 0;
7724 if (changed_count)
7725 ++windows_or_buffers_changed;
7726 }
7727 }
7728
7729
7730 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7731 is non-zero update selected_frame. Value is non-zero if the
7732 mini-windows height has been changed. */
7733
7734 static int
7735 echo_area_display (update_frame_p)
7736 int update_frame_p;
7737 {
7738 Lisp_Object mini_window;
7739 struct window *w;
7740 struct frame *f;
7741 int window_height_changed_p = 0;
7742 struct frame *sf = SELECTED_FRAME ();
7743
7744 mini_window = FRAME_MINIBUF_WINDOW (sf);
7745 w = XWINDOW (mini_window);
7746 f = XFRAME (WINDOW_FRAME (w));
7747
7748 /* Don't display if frame is invisible or not yet initialized. */
7749 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7750 return 0;
7751
7752 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7753 #ifndef MAC_OS8
7754 #ifdef HAVE_WINDOW_SYSTEM
7755 /* When Emacs starts, selected_frame may be a visible terminal
7756 frame, even if we run under a window system. If we let this
7757 through, a message would be displayed on the terminal. */
7758 if (EQ (selected_frame, Vterminal_frame)
7759 && !NILP (Vwindow_system))
7760 return 0;
7761 #endif /* HAVE_WINDOW_SYSTEM */
7762 #endif
7763
7764 /* Redraw garbaged frames. */
7765 if (frame_garbaged)
7766 clear_garbaged_frames ();
7767
7768 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7769 {
7770 echo_area_window = mini_window;
7771 window_height_changed_p = display_echo_area (w);
7772 w->must_be_updated_p = 1;
7773
7774 /* Update the display, unless called from redisplay_internal.
7775 Also don't update the screen during redisplay itself. The
7776 update will happen at the end of redisplay, and an update
7777 here could cause confusion. */
7778 if (update_frame_p && !redisplaying_p)
7779 {
7780 int n = 0;
7781
7782 /* If the display update has been interrupted by pending
7783 input, update mode lines in the frame. Due to the
7784 pending input, it might have been that redisplay hasn't
7785 been called, so that mode lines above the echo area are
7786 garbaged. This looks odd, so we prevent it here. */
7787 if (!display_completed)
7788 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7789
7790 if (window_height_changed_p
7791 /* Don't do this if Emacs is shutting down. Redisplay
7792 needs to run hooks. */
7793 && !NILP (Vrun_hooks))
7794 {
7795 /* Must update other windows. Likewise as in other
7796 cases, don't let this update be interrupted by
7797 pending input. */
7798 int count = SPECPDL_INDEX ();
7799 specbind (Qredisplay_dont_pause, Qt);
7800 windows_or_buffers_changed = 1;
7801 redisplay_internal (0);
7802 unbind_to (count, Qnil);
7803 }
7804 else if (FRAME_WINDOW_P (f) && n == 0)
7805 {
7806 /* Window configuration is the same as before.
7807 Can do with a display update of the echo area,
7808 unless we displayed some mode lines. */
7809 update_single_window (w, 1);
7810 rif->flush_display (f);
7811 }
7812 else
7813 update_frame (f, 1, 1);
7814
7815 /* If cursor is in the echo area, make sure that the next
7816 redisplay displays the minibuffer, so that the cursor will
7817 be replaced with what the minibuffer wants. */
7818 if (cursor_in_echo_area)
7819 ++windows_or_buffers_changed;
7820 }
7821 }
7822 else if (!EQ (mini_window, selected_window))
7823 windows_or_buffers_changed++;
7824
7825 /* Last displayed message is now the current message. */
7826 echo_area_buffer[1] = echo_area_buffer[0];
7827
7828 /* Prevent redisplay optimization in redisplay_internal by resetting
7829 this_line_start_pos. This is done because the mini-buffer now
7830 displays the message instead of its buffer text. */
7831 if (EQ (mini_window, selected_window))
7832 CHARPOS (this_line_start_pos) = 0;
7833
7834 return window_height_changed_p;
7835 }
7836
7837
7838 \f
7839 /***********************************************************************
7840 Frame Titles
7841 ***********************************************************************/
7842
7843
7844 /* The frame title buffering code is also used by Fformat_mode_line.
7845 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7846
7847 /* A buffer for constructing frame titles in it; allocated from the
7848 heap in init_xdisp and resized as needed in store_frame_title_char. */
7849
7850 static char *frame_title_buf;
7851
7852 /* The buffer's end, and a current output position in it. */
7853
7854 static char *frame_title_buf_end;
7855 static char *frame_title_ptr;
7856
7857
7858 /* Store a single character C for the frame title in frame_title_buf.
7859 Re-allocate frame_title_buf if necessary. */
7860
7861 static void
7862 #ifdef PROTOTYPES
7863 store_frame_title_char (char c)
7864 #else
7865 store_frame_title_char (c)
7866 char c;
7867 #endif
7868 {
7869 /* If output position has reached the end of the allocated buffer,
7870 double the buffer's size. */
7871 if (frame_title_ptr == frame_title_buf_end)
7872 {
7873 int len = frame_title_ptr - frame_title_buf;
7874 int new_size = 2 * len * sizeof *frame_title_buf;
7875 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7876 frame_title_buf_end = frame_title_buf + new_size;
7877 frame_title_ptr = frame_title_buf + len;
7878 }
7879
7880 *frame_title_ptr++ = c;
7881 }
7882
7883
7884 /* Store part of a frame title in frame_title_buf, beginning at
7885 frame_title_ptr. STR is the string to store. Do not copy
7886 characters that yield more columns than PRECISION; PRECISION <= 0
7887 means copy the whole string. Pad with spaces until FIELD_WIDTH
7888 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7889 pad. Called from display_mode_element when it is used to build a
7890 frame title. */
7891
7892 static int
7893 store_frame_title (str, field_width, precision)
7894 const unsigned char *str;
7895 int field_width, precision;
7896 {
7897 int n = 0;
7898 int dummy, nbytes;
7899
7900 /* Copy at most PRECISION chars from STR. */
7901 nbytes = strlen (str);
7902 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7903 while (nbytes--)
7904 store_frame_title_char (*str++);
7905
7906 /* Fill up with spaces until FIELD_WIDTH reached. */
7907 while (field_width > 0
7908 && n < field_width)
7909 {
7910 store_frame_title_char (' ');
7911 ++n;
7912 }
7913
7914 return n;
7915 }
7916
7917 #ifdef HAVE_WINDOW_SYSTEM
7918
7919 /* Set the title of FRAME, if it has changed. The title format is
7920 Vicon_title_format if FRAME is iconified, otherwise it is
7921 frame_title_format. */
7922
7923 static void
7924 x_consider_frame_title (frame)
7925 Lisp_Object frame;
7926 {
7927 struct frame *f = XFRAME (frame);
7928
7929 if (FRAME_WINDOW_P (f)
7930 || FRAME_MINIBUF_ONLY_P (f)
7931 || f->explicit_name)
7932 {
7933 /* Do we have more than one visible frame on this X display? */
7934 Lisp_Object tail;
7935 Lisp_Object fmt;
7936 struct buffer *obuf;
7937 int len;
7938 struct it it;
7939
7940 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7941 {
7942 Lisp_Object other_frame = XCAR (tail);
7943 struct frame *tf = XFRAME (other_frame);
7944
7945 if (tf != f
7946 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7947 && !FRAME_MINIBUF_ONLY_P (tf)
7948 && !EQ (other_frame, tip_frame)
7949 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7950 break;
7951 }
7952
7953 /* Set global variable indicating that multiple frames exist. */
7954 multiple_frames = CONSP (tail);
7955
7956 /* Switch to the buffer of selected window of the frame. Set up
7957 frame_title_ptr so that display_mode_element will output into it;
7958 then display the title. */
7959 obuf = current_buffer;
7960 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7961 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7962 frame_title_ptr = frame_title_buf;
7963 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7964 NULL, DEFAULT_FACE_ID);
7965 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7966 len = frame_title_ptr - frame_title_buf;
7967 frame_title_ptr = NULL;
7968 set_buffer_internal_1 (obuf);
7969
7970 /* Set the title only if it's changed. This avoids consing in
7971 the common case where it hasn't. (If it turns out that we've
7972 already wasted too much time by walking through the list with
7973 display_mode_element, then we might need to optimize at a
7974 higher level than this.) */
7975 if (! STRINGP (f->name)
7976 || SBYTES (f->name) != len
7977 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7978 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7979 }
7980 }
7981
7982 #endif /* not HAVE_WINDOW_SYSTEM */
7983
7984
7985
7986 \f
7987 /***********************************************************************
7988 Menu Bars
7989 ***********************************************************************/
7990
7991
7992 /* Prepare for redisplay by updating menu-bar item lists when
7993 appropriate. This can call eval. */
7994
7995 void
7996 prepare_menu_bars ()
7997 {
7998 int all_windows;
7999 struct gcpro gcpro1, gcpro2;
8000 struct frame *f;
8001 Lisp_Object tooltip_frame;
8002
8003 #ifdef HAVE_WINDOW_SYSTEM
8004 tooltip_frame = tip_frame;
8005 #else
8006 tooltip_frame = Qnil;
8007 #endif
8008
8009 /* Update all frame titles based on their buffer names, etc. We do
8010 this before the menu bars so that the buffer-menu will show the
8011 up-to-date frame titles. */
8012 #ifdef HAVE_WINDOW_SYSTEM
8013 if (windows_or_buffers_changed || update_mode_lines)
8014 {
8015 Lisp_Object tail, frame;
8016
8017 FOR_EACH_FRAME (tail, frame)
8018 {
8019 f = XFRAME (frame);
8020 if (!EQ (frame, tooltip_frame)
8021 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8022 x_consider_frame_title (frame);
8023 }
8024 }
8025 #endif /* HAVE_WINDOW_SYSTEM */
8026
8027 /* Update the menu bar item lists, if appropriate. This has to be
8028 done before any actual redisplay or generation of display lines. */
8029 all_windows = (update_mode_lines
8030 || buffer_shared > 1
8031 || windows_or_buffers_changed);
8032 if (all_windows)
8033 {
8034 Lisp_Object tail, frame;
8035 int count = SPECPDL_INDEX ();
8036
8037 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8038
8039 FOR_EACH_FRAME (tail, frame)
8040 {
8041 f = XFRAME (frame);
8042
8043 /* Ignore tooltip frame. */
8044 if (EQ (frame, tooltip_frame))
8045 continue;
8046
8047 /* If a window on this frame changed size, report that to
8048 the user and clear the size-change flag. */
8049 if (FRAME_WINDOW_SIZES_CHANGED (f))
8050 {
8051 Lisp_Object functions;
8052
8053 /* Clear flag first in case we get an error below. */
8054 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8055 functions = Vwindow_size_change_functions;
8056 GCPRO2 (tail, functions);
8057
8058 while (CONSP (functions))
8059 {
8060 call1 (XCAR (functions), frame);
8061 functions = XCDR (functions);
8062 }
8063 UNGCPRO;
8064 }
8065
8066 GCPRO1 (tail);
8067 update_menu_bar (f, 0);
8068 #ifdef HAVE_WINDOW_SYSTEM
8069 update_tool_bar (f, 0);
8070 #endif
8071 UNGCPRO;
8072 }
8073
8074 unbind_to (count, Qnil);
8075 }
8076 else
8077 {
8078 struct frame *sf = SELECTED_FRAME ();
8079 update_menu_bar (sf, 1);
8080 #ifdef HAVE_WINDOW_SYSTEM
8081 update_tool_bar (sf, 1);
8082 #endif
8083 }
8084
8085 /* Motif needs this. See comment in xmenu.c. Turn it off when
8086 pending_menu_activation is not defined. */
8087 #ifdef USE_X_TOOLKIT
8088 pending_menu_activation = 0;
8089 #endif
8090 }
8091
8092
8093 /* Update the menu bar item list for frame F. This has to be done
8094 before we start to fill in any display lines, because it can call
8095 eval.
8096
8097 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8098
8099 static void
8100 update_menu_bar (f, save_match_data)
8101 struct frame *f;
8102 int save_match_data;
8103 {
8104 Lisp_Object window;
8105 register struct window *w;
8106
8107 /* If called recursively during a menu update, do nothing. This can
8108 happen when, for instance, an activate-menubar-hook causes a
8109 redisplay. */
8110 if (inhibit_menubar_update)
8111 return;
8112
8113 window = FRAME_SELECTED_WINDOW (f);
8114 w = XWINDOW (window);
8115
8116 #if 0 /* The if statement below this if statement used to include the
8117 condition !NILP (w->update_mode_line), rather than using
8118 update_mode_lines directly, and this if statement may have
8119 been added to make that condition work. Now the if
8120 statement below matches its comment, this isn't needed. */
8121 if (update_mode_lines)
8122 w->update_mode_line = Qt;
8123 #endif
8124
8125 if (FRAME_WINDOW_P (f)
8126 ?
8127 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8128 || defined (USE_GTK)
8129 FRAME_EXTERNAL_MENU_BAR (f)
8130 #else
8131 FRAME_MENU_BAR_LINES (f) > 0
8132 #endif
8133 : FRAME_MENU_BAR_LINES (f) > 0)
8134 {
8135 /* If the user has switched buffers or windows, we need to
8136 recompute to reflect the new bindings. But we'll
8137 recompute when update_mode_lines is set too; that means
8138 that people can use force-mode-line-update to request
8139 that the menu bar be recomputed. The adverse effect on
8140 the rest of the redisplay algorithm is about the same as
8141 windows_or_buffers_changed anyway. */
8142 if (windows_or_buffers_changed
8143 /* This used to test w->update_mode_line, but we believe
8144 there is no need to recompute the menu in that case. */
8145 || update_mode_lines
8146 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8147 < BUF_MODIFF (XBUFFER (w->buffer)))
8148 != !NILP (w->last_had_star))
8149 || ((!NILP (Vtransient_mark_mode)
8150 && !NILP (XBUFFER (w->buffer)->mark_active))
8151 != !NILP (w->region_showing)))
8152 {
8153 struct buffer *prev = current_buffer;
8154 int count = SPECPDL_INDEX ();
8155
8156 specbind (Qinhibit_menubar_update, Qt);
8157
8158 set_buffer_internal_1 (XBUFFER (w->buffer));
8159 if (save_match_data)
8160 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8161 if (NILP (Voverriding_local_map_menu_flag))
8162 {
8163 specbind (Qoverriding_terminal_local_map, Qnil);
8164 specbind (Qoverriding_local_map, Qnil);
8165 }
8166
8167 /* Run the Lucid hook. */
8168 safe_run_hooks (Qactivate_menubar_hook);
8169
8170 /* If it has changed current-menubar from previous value,
8171 really recompute the menu-bar from the value. */
8172 if (! NILP (Vlucid_menu_bar_dirty_flag))
8173 call0 (Qrecompute_lucid_menubar);
8174
8175 safe_run_hooks (Qmenu_bar_update_hook);
8176 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8177
8178 /* Redisplay the menu bar in case we changed it. */
8179 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8180 || defined (USE_GTK)
8181 if (FRAME_WINDOW_P (f)
8182 #if defined (MAC_OS)
8183 /* All frames on Mac OS share the same menubar. So only the
8184 selected frame should be allowed to set it. */
8185 && f == SELECTED_FRAME ()
8186 #endif
8187 )
8188 set_frame_menubar (f, 0, 0);
8189 else
8190 /* On a terminal screen, the menu bar is an ordinary screen
8191 line, and this makes it get updated. */
8192 w->update_mode_line = Qt;
8193 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8194 /* In the non-toolkit version, the menu bar is an ordinary screen
8195 line, and this makes it get updated. */
8196 w->update_mode_line = Qt;
8197 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8198
8199 unbind_to (count, Qnil);
8200 set_buffer_internal_1 (prev);
8201 }
8202 }
8203 }
8204
8205
8206 \f
8207 /***********************************************************************
8208 Output Cursor
8209 ***********************************************************************/
8210
8211 #ifdef HAVE_WINDOW_SYSTEM
8212
8213 /* EXPORT:
8214 Nominal cursor position -- where to draw output.
8215 HPOS and VPOS are window relative glyph matrix coordinates.
8216 X and Y are window relative pixel coordinates. */
8217
8218 struct cursor_pos output_cursor;
8219
8220
8221 /* EXPORT:
8222 Set the global variable output_cursor to CURSOR. All cursor
8223 positions are relative to updated_window. */
8224
8225 void
8226 set_output_cursor (cursor)
8227 struct cursor_pos *cursor;
8228 {
8229 output_cursor.hpos = cursor->hpos;
8230 output_cursor.vpos = cursor->vpos;
8231 output_cursor.x = cursor->x;
8232 output_cursor.y = cursor->y;
8233 }
8234
8235
8236 /* EXPORT for RIF:
8237 Set a nominal cursor position.
8238
8239 HPOS and VPOS are column/row positions in a window glyph matrix. X
8240 and Y are window text area relative pixel positions.
8241
8242 If this is done during an update, updated_window will contain the
8243 window that is being updated and the position is the future output
8244 cursor position for that window. If updated_window is null, use
8245 selected_window and display the cursor at the given position. */
8246
8247 void
8248 x_cursor_to (vpos, hpos, y, x)
8249 int vpos, hpos, y, x;
8250 {
8251 struct window *w;
8252
8253 /* If updated_window is not set, work on selected_window. */
8254 if (updated_window)
8255 w = updated_window;
8256 else
8257 w = XWINDOW (selected_window);
8258
8259 /* Set the output cursor. */
8260 output_cursor.hpos = hpos;
8261 output_cursor.vpos = vpos;
8262 output_cursor.x = x;
8263 output_cursor.y = y;
8264
8265 /* If not called as part of an update, really display the cursor.
8266 This will also set the cursor position of W. */
8267 if (updated_window == NULL)
8268 {
8269 BLOCK_INPUT;
8270 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8271 if (rif->flush_display_optional)
8272 rif->flush_display_optional (SELECTED_FRAME ());
8273 UNBLOCK_INPUT;
8274 }
8275 }
8276
8277 #endif /* HAVE_WINDOW_SYSTEM */
8278
8279 \f
8280 /***********************************************************************
8281 Tool-bars
8282 ***********************************************************************/
8283
8284 #ifdef HAVE_WINDOW_SYSTEM
8285
8286 /* Where the mouse was last time we reported a mouse event. */
8287
8288 FRAME_PTR last_mouse_frame;
8289
8290 /* Tool-bar item index of the item on which a mouse button was pressed
8291 or -1. */
8292
8293 int last_tool_bar_item;
8294
8295
8296 /* Update the tool-bar item list for frame F. This has to be done
8297 before we start to fill in any display lines. Called from
8298 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8299 and restore it here. */
8300
8301 static void
8302 update_tool_bar (f, save_match_data)
8303 struct frame *f;
8304 int save_match_data;
8305 {
8306 #ifdef USE_GTK
8307 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8308 #else
8309 int do_update = WINDOWP (f->tool_bar_window)
8310 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8311 #endif
8312
8313 if (do_update)
8314 {
8315 Lisp_Object window;
8316 struct window *w;
8317
8318 window = FRAME_SELECTED_WINDOW (f);
8319 w = XWINDOW (window);
8320
8321 /* If the user has switched buffers or windows, we need to
8322 recompute to reflect the new bindings. But we'll
8323 recompute when update_mode_lines is set too; that means
8324 that people can use force-mode-line-update to request
8325 that the menu bar be recomputed. The adverse effect on
8326 the rest of the redisplay algorithm is about the same as
8327 windows_or_buffers_changed anyway. */
8328 if (windows_or_buffers_changed
8329 || !NILP (w->update_mode_line)
8330 || update_mode_lines
8331 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8332 < BUF_MODIFF (XBUFFER (w->buffer)))
8333 != !NILP (w->last_had_star))
8334 || ((!NILP (Vtransient_mark_mode)
8335 && !NILP (XBUFFER (w->buffer)->mark_active))
8336 != !NILP (w->region_showing)))
8337 {
8338 struct buffer *prev = current_buffer;
8339 int count = SPECPDL_INDEX ();
8340 Lisp_Object old_tool_bar;
8341 struct gcpro gcpro1;
8342
8343 /* Set current_buffer to the buffer of the selected
8344 window of the frame, so that we get the right local
8345 keymaps. */
8346 set_buffer_internal_1 (XBUFFER (w->buffer));
8347
8348 /* Save match data, if we must. */
8349 if (save_match_data)
8350 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8351
8352 /* Make sure that we don't accidentally use bogus keymaps. */
8353 if (NILP (Voverriding_local_map_menu_flag))
8354 {
8355 specbind (Qoverriding_terminal_local_map, Qnil);
8356 specbind (Qoverriding_local_map, Qnil);
8357 }
8358
8359 old_tool_bar = f->tool_bar_items;
8360 GCPRO1 (old_tool_bar);
8361
8362 /* Build desired tool-bar items from keymaps. */
8363 BLOCK_INPUT;
8364 f->tool_bar_items
8365 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8366 UNBLOCK_INPUT;
8367
8368 /* Redisplay the tool-bar if we changed it. */
8369 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8370 w->update_mode_line = Qt;
8371
8372 UNGCPRO;
8373
8374 unbind_to (count, Qnil);
8375 set_buffer_internal_1 (prev);
8376 }
8377 }
8378 }
8379
8380
8381 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8382 F's desired tool-bar contents. F->tool_bar_items must have
8383 been set up previously by calling prepare_menu_bars. */
8384
8385 static void
8386 build_desired_tool_bar_string (f)
8387 struct frame *f;
8388 {
8389 int i, size, size_needed;
8390 struct gcpro gcpro1, gcpro2, gcpro3;
8391 Lisp_Object image, plist, props;
8392
8393 image = plist = props = Qnil;
8394 GCPRO3 (image, plist, props);
8395
8396 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8397 Otherwise, make a new string. */
8398
8399 /* The size of the string we might be able to reuse. */
8400 size = (STRINGP (f->desired_tool_bar_string)
8401 ? SCHARS (f->desired_tool_bar_string)
8402 : 0);
8403
8404 /* We need one space in the string for each image. */
8405 size_needed = f->n_tool_bar_items;
8406
8407 /* Reuse f->desired_tool_bar_string, if possible. */
8408 if (size < size_needed || NILP (f->desired_tool_bar_string))
8409 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8410 make_number (' '));
8411 else
8412 {
8413 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8414 Fremove_text_properties (make_number (0), make_number (size),
8415 props, f->desired_tool_bar_string);
8416 }
8417
8418 /* Put a `display' property on the string for the images to display,
8419 put a `menu_item' property on tool-bar items with a value that
8420 is the index of the item in F's tool-bar item vector. */
8421 for (i = 0; i < f->n_tool_bar_items; ++i)
8422 {
8423 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8424
8425 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8426 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8427 int hmargin, vmargin, relief, idx, end;
8428 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8429
8430 /* If image is a vector, choose the image according to the
8431 button state. */
8432 image = PROP (TOOL_BAR_ITEM_IMAGES);
8433 if (VECTORP (image))
8434 {
8435 if (enabled_p)
8436 idx = (selected_p
8437 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8438 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8439 else
8440 idx = (selected_p
8441 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8442 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8443
8444 xassert (ASIZE (image) >= idx);
8445 image = AREF (image, idx);
8446 }
8447 else
8448 idx = -1;
8449
8450 /* Ignore invalid image specifications. */
8451 if (!valid_image_p (image))
8452 continue;
8453
8454 /* Display the tool-bar button pressed, or depressed. */
8455 plist = Fcopy_sequence (XCDR (image));
8456
8457 /* Compute margin and relief to draw. */
8458 relief = (tool_bar_button_relief >= 0
8459 ? tool_bar_button_relief
8460 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8461 hmargin = vmargin = relief;
8462
8463 if (INTEGERP (Vtool_bar_button_margin)
8464 && XINT (Vtool_bar_button_margin) > 0)
8465 {
8466 hmargin += XFASTINT (Vtool_bar_button_margin);
8467 vmargin += XFASTINT (Vtool_bar_button_margin);
8468 }
8469 else if (CONSP (Vtool_bar_button_margin))
8470 {
8471 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8472 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8473 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8474
8475 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8476 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8477 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8478 }
8479
8480 if (auto_raise_tool_bar_buttons_p)
8481 {
8482 /* Add a `:relief' property to the image spec if the item is
8483 selected. */
8484 if (selected_p)
8485 {
8486 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8487 hmargin -= relief;
8488 vmargin -= relief;
8489 }
8490 }
8491 else
8492 {
8493 /* If image is selected, display it pressed, i.e. with a
8494 negative relief. If it's not selected, display it with a
8495 raised relief. */
8496 plist = Fplist_put (plist, QCrelief,
8497 (selected_p
8498 ? make_number (-relief)
8499 : make_number (relief)));
8500 hmargin -= relief;
8501 vmargin -= relief;
8502 }
8503
8504 /* Put a margin around the image. */
8505 if (hmargin || vmargin)
8506 {
8507 if (hmargin == vmargin)
8508 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8509 else
8510 plist = Fplist_put (plist, QCmargin,
8511 Fcons (make_number (hmargin),
8512 make_number (vmargin)));
8513 }
8514
8515 /* If button is not enabled, and we don't have special images
8516 for the disabled state, make the image appear disabled by
8517 applying an appropriate algorithm to it. */
8518 if (!enabled_p && idx < 0)
8519 plist = Fplist_put (plist, QCconversion, Qdisabled);
8520
8521 /* Put a `display' text property on the string for the image to
8522 display. Put a `menu-item' property on the string that gives
8523 the start of this item's properties in the tool-bar items
8524 vector. */
8525 image = Fcons (Qimage, plist);
8526 props = list4 (Qdisplay, image,
8527 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8528
8529 /* Let the last image hide all remaining spaces in the tool bar
8530 string. The string can be longer than needed when we reuse a
8531 previous string. */
8532 if (i + 1 == f->n_tool_bar_items)
8533 end = SCHARS (f->desired_tool_bar_string);
8534 else
8535 end = i + 1;
8536 Fadd_text_properties (make_number (i), make_number (end),
8537 props, f->desired_tool_bar_string);
8538 #undef PROP
8539 }
8540
8541 UNGCPRO;
8542 }
8543
8544
8545 /* Display one line of the tool-bar of frame IT->f. */
8546
8547 static void
8548 display_tool_bar_line (it)
8549 struct it *it;
8550 {
8551 struct glyph_row *row = it->glyph_row;
8552 int max_x = it->last_visible_x;
8553 struct glyph *last;
8554
8555 prepare_desired_row (row);
8556 row->y = it->current_y;
8557
8558 /* Note that this isn't made use of if the face hasn't a box,
8559 so there's no need to check the face here. */
8560 it->start_of_box_run_p = 1;
8561
8562 while (it->current_x < max_x)
8563 {
8564 int x_before, x, n_glyphs_before, i, nglyphs;
8565
8566 /* Get the next display element. */
8567 if (!get_next_display_element (it))
8568 break;
8569
8570 /* Produce glyphs. */
8571 x_before = it->current_x;
8572 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8573 PRODUCE_GLYPHS (it);
8574
8575 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8576 i = 0;
8577 x = x_before;
8578 while (i < nglyphs)
8579 {
8580 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8581
8582 if (x + glyph->pixel_width > max_x)
8583 {
8584 /* Glyph doesn't fit on line. */
8585 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8586 it->current_x = x;
8587 goto out;
8588 }
8589
8590 ++it->hpos;
8591 x += glyph->pixel_width;
8592 ++i;
8593 }
8594
8595 /* Stop at line ends. */
8596 if (ITERATOR_AT_END_OF_LINE_P (it))
8597 break;
8598
8599 set_iterator_to_next (it, 1);
8600 }
8601
8602 out:;
8603
8604 row->displays_text_p = row->used[TEXT_AREA] != 0;
8605 extend_face_to_end_of_line (it);
8606 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8607 last->right_box_line_p = 1;
8608 if (last == row->glyphs[TEXT_AREA])
8609 last->left_box_line_p = 1;
8610 compute_line_metrics (it);
8611
8612 /* If line is empty, make it occupy the rest of the tool-bar. */
8613 if (!row->displays_text_p)
8614 {
8615 row->height = row->phys_height = it->last_visible_y - row->y;
8616 row->ascent = row->phys_ascent = 0;
8617 }
8618
8619 row->full_width_p = 1;
8620 row->continued_p = 0;
8621 row->truncated_on_left_p = 0;
8622 row->truncated_on_right_p = 0;
8623
8624 it->current_x = it->hpos = 0;
8625 it->current_y += row->height;
8626 ++it->vpos;
8627 ++it->glyph_row;
8628 }
8629
8630
8631 /* Value is the number of screen lines needed to make all tool-bar
8632 items of frame F visible. */
8633
8634 static int
8635 tool_bar_lines_needed (f)
8636 struct frame *f;
8637 {
8638 struct window *w = XWINDOW (f->tool_bar_window);
8639 struct it it;
8640
8641 /* Initialize an iterator for iteration over
8642 F->desired_tool_bar_string in the tool-bar window of frame F. */
8643 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8644 it.first_visible_x = 0;
8645 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8646 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8647
8648 while (!ITERATOR_AT_END_P (&it))
8649 {
8650 it.glyph_row = w->desired_matrix->rows;
8651 clear_glyph_row (it.glyph_row);
8652 display_tool_bar_line (&it);
8653 }
8654
8655 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8656 }
8657
8658
8659 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8660 0, 1, 0,
8661 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8662 (frame)
8663 Lisp_Object frame;
8664 {
8665 struct frame *f;
8666 struct window *w;
8667 int nlines = 0;
8668
8669 if (NILP (frame))
8670 frame = selected_frame;
8671 else
8672 CHECK_FRAME (frame);
8673 f = XFRAME (frame);
8674
8675 if (WINDOWP (f->tool_bar_window)
8676 || (w = XWINDOW (f->tool_bar_window),
8677 WINDOW_TOTAL_LINES (w) > 0))
8678 {
8679 update_tool_bar (f, 1);
8680 if (f->n_tool_bar_items)
8681 {
8682 build_desired_tool_bar_string (f);
8683 nlines = tool_bar_lines_needed (f);
8684 }
8685 }
8686
8687 return make_number (nlines);
8688 }
8689
8690
8691 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8692 height should be changed. */
8693
8694 static int
8695 redisplay_tool_bar (f)
8696 struct frame *f;
8697 {
8698 struct window *w;
8699 struct it it;
8700 struct glyph_row *row;
8701 int change_height_p = 0;
8702
8703 #ifdef USE_GTK
8704 if (FRAME_EXTERNAL_TOOL_BAR (f))
8705 update_frame_tool_bar (f);
8706 return 0;
8707 #endif
8708
8709 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8710 do anything. This means you must start with tool-bar-lines
8711 non-zero to get the auto-sizing effect. Or in other words, you
8712 can turn off tool-bars by specifying tool-bar-lines zero. */
8713 if (!WINDOWP (f->tool_bar_window)
8714 || (w = XWINDOW (f->tool_bar_window),
8715 WINDOW_TOTAL_LINES (w) == 0))
8716 return 0;
8717
8718 /* Set up an iterator for the tool-bar window. */
8719 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8720 it.first_visible_x = 0;
8721 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8722 row = it.glyph_row;
8723
8724 /* Build a string that represents the contents of the tool-bar. */
8725 build_desired_tool_bar_string (f);
8726 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8727
8728 /* Display as many lines as needed to display all tool-bar items. */
8729 while (it.current_y < it.last_visible_y)
8730 display_tool_bar_line (&it);
8731
8732 /* It doesn't make much sense to try scrolling in the tool-bar
8733 window, so don't do it. */
8734 w->desired_matrix->no_scrolling_p = 1;
8735 w->must_be_updated_p = 1;
8736
8737 if (auto_resize_tool_bars_p)
8738 {
8739 int nlines;
8740
8741 /* If we couldn't display everything, change the tool-bar's
8742 height. */
8743 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8744 change_height_p = 1;
8745
8746 /* If there are blank lines at the end, except for a partially
8747 visible blank line at the end that is smaller than
8748 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8749 row = it.glyph_row - 1;
8750 if (!row->displays_text_p
8751 && row->height >= FRAME_LINE_HEIGHT (f))
8752 change_height_p = 1;
8753
8754 /* If row displays tool-bar items, but is partially visible,
8755 change the tool-bar's height. */
8756 if (row->displays_text_p
8757 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8758 change_height_p = 1;
8759
8760 /* Resize windows as needed by changing the `tool-bar-lines'
8761 frame parameter. */
8762 if (change_height_p
8763 && (nlines = tool_bar_lines_needed (f),
8764 nlines != WINDOW_TOTAL_LINES (w)))
8765 {
8766 extern Lisp_Object Qtool_bar_lines;
8767 Lisp_Object frame;
8768 int old_height = WINDOW_TOTAL_LINES (w);
8769
8770 XSETFRAME (frame, f);
8771 clear_glyph_matrix (w->desired_matrix);
8772 Fmodify_frame_parameters (frame,
8773 Fcons (Fcons (Qtool_bar_lines,
8774 make_number (nlines)),
8775 Qnil));
8776 if (WINDOW_TOTAL_LINES (w) != old_height)
8777 fonts_changed_p = 1;
8778 }
8779 }
8780
8781 return change_height_p;
8782 }
8783
8784
8785 /* Get information about the tool-bar item which is displayed in GLYPH
8786 on frame F. Return in *PROP_IDX the index where tool-bar item
8787 properties start in F->tool_bar_items. Value is zero if
8788 GLYPH doesn't display a tool-bar item. */
8789
8790 static int
8791 tool_bar_item_info (f, glyph, prop_idx)
8792 struct frame *f;
8793 struct glyph *glyph;
8794 int *prop_idx;
8795 {
8796 Lisp_Object prop;
8797 int success_p;
8798 int charpos;
8799
8800 /* This function can be called asynchronously, which means we must
8801 exclude any possibility that Fget_text_property signals an
8802 error. */
8803 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8804 charpos = max (0, charpos);
8805
8806 /* Get the text property `menu-item' at pos. The value of that
8807 property is the start index of this item's properties in
8808 F->tool_bar_items. */
8809 prop = Fget_text_property (make_number (charpos),
8810 Qmenu_item, f->current_tool_bar_string);
8811 if (INTEGERP (prop))
8812 {
8813 *prop_idx = XINT (prop);
8814 success_p = 1;
8815 }
8816 else
8817 success_p = 0;
8818
8819 return success_p;
8820 }
8821
8822 \f
8823 /* Get information about the tool-bar item at position X/Y on frame F.
8824 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8825 the current matrix of the tool-bar window of F, or NULL if not
8826 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8827 item in F->tool_bar_items. Value is
8828
8829 -1 if X/Y is not on a tool-bar item
8830 0 if X/Y is on the same item that was highlighted before.
8831 1 otherwise. */
8832
8833 static int
8834 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8835 struct frame *f;
8836 int x, y;
8837 struct glyph **glyph;
8838 int *hpos, *vpos, *prop_idx;
8839 {
8840 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8841 struct window *w = XWINDOW (f->tool_bar_window);
8842 int area;
8843
8844 /* Find the glyph under X/Y. */
8845 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8846 if (*glyph == NULL)
8847 return -1;
8848
8849 /* Get the start of this tool-bar item's properties in
8850 f->tool_bar_items. */
8851 if (!tool_bar_item_info (f, *glyph, prop_idx))
8852 return -1;
8853
8854 /* Is mouse on the highlighted item? */
8855 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8856 && *vpos >= dpyinfo->mouse_face_beg_row
8857 && *vpos <= dpyinfo->mouse_face_end_row
8858 && (*vpos > dpyinfo->mouse_face_beg_row
8859 || *hpos >= dpyinfo->mouse_face_beg_col)
8860 && (*vpos < dpyinfo->mouse_face_end_row
8861 || *hpos < dpyinfo->mouse_face_end_col
8862 || dpyinfo->mouse_face_past_end))
8863 return 0;
8864
8865 return 1;
8866 }
8867
8868
8869 /* EXPORT:
8870 Handle mouse button event on the tool-bar of frame F, at
8871 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8872 0 for button release. MODIFIERS is event modifiers for button
8873 release. */
8874
8875 void
8876 handle_tool_bar_click (f, x, y, down_p, modifiers)
8877 struct frame *f;
8878 int x, y, down_p;
8879 unsigned int modifiers;
8880 {
8881 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8882 struct window *w = XWINDOW (f->tool_bar_window);
8883 int hpos, vpos, prop_idx;
8884 struct glyph *glyph;
8885 Lisp_Object enabled_p;
8886
8887 /* If not on the highlighted tool-bar item, return. */
8888 frame_to_window_pixel_xy (w, &x, &y);
8889 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8890 return;
8891
8892 /* If item is disabled, do nothing. */
8893 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8894 if (NILP (enabled_p))
8895 return;
8896
8897 if (down_p)
8898 {
8899 /* Show item in pressed state. */
8900 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8901 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8902 last_tool_bar_item = prop_idx;
8903 }
8904 else
8905 {
8906 Lisp_Object key, frame;
8907 struct input_event event;
8908 EVENT_INIT (event);
8909
8910 /* Show item in released state. */
8911 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8912 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8913
8914 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8915
8916 XSETFRAME (frame, f);
8917 event.kind = TOOL_BAR_EVENT;
8918 event.frame_or_window = frame;
8919 event.arg = frame;
8920 kbd_buffer_store_event (&event);
8921
8922 event.kind = TOOL_BAR_EVENT;
8923 event.frame_or_window = frame;
8924 event.arg = key;
8925 event.modifiers = modifiers;
8926 kbd_buffer_store_event (&event);
8927 last_tool_bar_item = -1;
8928 }
8929 }
8930
8931
8932 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8933 tool-bar window-relative coordinates X/Y. Called from
8934 note_mouse_highlight. */
8935
8936 static void
8937 note_tool_bar_highlight (f, x, y)
8938 struct frame *f;
8939 int x, y;
8940 {
8941 Lisp_Object window = f->tool_bar_window;
8942 struct window *w = XWINDOW (window);
8943 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8944 int hpos, vpos;
8945 struct glyph *glyph;
8946 struct glyph_row *row;
8947 int i;
8948 Lisp_Object enabled_p;
8949 int prop_idx;
8950 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8951 int mouse_down_p, rc;
8952
8953 /* Function note_mouse_highlight is called with negative x(y
8954 values when mouse moves outside of the frame. */
8955 if (x <= 0 || y <= 0)
8956 {
8957 clear_mouse_face (dpyinfo);
8958 return;
8959 }
8960
8961 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8962 if (rc < 0)
8963 {
8964 /* Not on tool-bar item. */
8965 clear_mouse_face (dpyinfo);
8966 return;
8967 }
8968 else if (rc == 0)
8969 /* On same tool-bar item as before. */
8970 goto set_help_echo;
8971
8972 clear_mouse_face (dpyinfo);
8973
8974 /* Mouse is down, but on different tool-bar item? */
8975 mouse_down_p = (dpyinfo->grabbed
8976 && f == last_mouse_frame
8977 && FRAME_LIVE_P (f));
8978 if (mouse_down_p
8979 && last_tool_bar_item != prop_idx)
8980 return;
8981
8982 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8983 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8984
8985 /* If tool-bar item is not enabled, don't highlight it. */
8986 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8987 if (!NILP (enabled_p))
8988 {
8989 /* Compute the x-position of the glyph. In front and past the
8990 image is a space. We include this in the highlighted area. */
8991 row = MATRIX_ROW (w->current_matrix, vpos);
8992 for (i = x = 0; i < hpos; ++i)
8993 x += row->glyphs[TEXT_AREA][i].pixel_width;
8994
8995 /* Record this as the current active region. */
8996 dpyinfo->mouse_face_beg_col = hpos;
8997 dpyinfo->mouse_face_beg_row = vpos;
8998 dpyinfo->mouse_face_beg_x = x;
8999 dpyinfo->mouse_face_beg_y = row->y;
9000 dpyinfo->mouse_face_past_end = 0;
9001
9002 dpyinfo->mouse_face_end_col = hpos + 1;
9003 dpyinfo->mouse_face_end_row = vpos;
9004 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9005 dpyinfo->mouse_face_end_y = row->y;
9006 dpyinfo->mouse_face_window = window;
9007 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9008
9009 /* Display it as active. */
9010 show_mouse_face (dpyinfo, draw);
9011 dpyinfo->mouse_face_image_state = draw;
9012 }
9013
9014 set_help_echo:
9015
9016 /* Set help_echo_string to a help string to display for this tool-bar item.
9017 XTread_socket does the rest. */
9018 help_echo_object = help_echo_window = Qnil;
9019 help_echo_pos = -1;
9020 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9021 if (NILP (help_echo_string))
9022 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9023 }
9024
9025 #endif /* HAVE_WINDOW_SYSTEM */
9026
9027
9028 \f
9029 /************************************************************************
9030 Horizontal scrolling
9031 ************************************************************************/
9032
9033 static int hscroll_window_tree P_ ((Lisp_Object));
9034 static int hscroll_windows P_ ((Lisp_Object));
9035
9036 /* For all leaf windows in the window tree rooted at WINDOW, set their
9037 hscroll value so that PT is (i) visible in the window, and (ii) so
9038 that it is not within a certain margin at the window's left and
9039 right border. Value is non-zero if any window's hscroll has been
9040 changed. */
9041
9042 static int
9043 hscroll_window_tree (window)
9044 Lisp_Object window;
9045 {
9046 int hscrolled_p = 0;
9047 int hscroll_relative_p = FLOATP (Vhscroll_step);
9048 int hscroll_step_abs = 0;
9049 double hscroll_step_rel = 0;
9050
9051 if (hscroll_relative_p)
9052 {
9053 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9054 if (hscroll_step_rel < 0)
9055 {
9056 hscroll_relative_p = 0;
9057 hscroll_step_abs = 0;
9058 }
9059 }
9060 else if (INTEGERP (Vhscroll_step))
9061 {
9062 hscroll_step_abs = XINT (Vhscroll_step);
9063 if (hscroll_step_abs < 0)
9064 hscroll_step_abs = 0;
9065 }
9066 else
9067 hscroll_step_abs = 0;
9068
9069 while (WINDOWP (window))
9070 {
9071 struct window *w = XWINDOW (window);
9072
9073 if (WINDOWP (w->hchild))
9074 hscrolled_p |= hscroll_window_tree (w->hchild);
9075 else if (WINDOWP (w->vchild))
9076 hscrolled_p |= hscroll_window_tree (w->vchild);
9077 else if (w->cursor.vpos >= 0)
9078 {
9079 int h_margin;
9080 int text_area_width;
9081 struct glyph_row *current_cursor_row
9082 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9083 struct glyph_row *desired_cursor_row
9084 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9085 struct glyph_row *cursor_row
9086 = (desired_cursor_row->enabled_p
9087 ? desired_cursor_row
9088 : current_cursor_row);
9089
9090 text_area_width = window_box_width (w, TEXT_AREA);
9091
9092 /* Scroll when cursor is inside this scroll margin. */
9093 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9094
9095 if ((XFASTINT (w->hscroll)
9096 && w->cursor.x <= h_margin)
9097 || (cursor_row->enabled_p
9098 && cursor_row->truncated_on_right_p
9099 && (w->cursor.x >= text_area_width - h_margin)))
9100 {
9101 struct it it;
9102 int hscroll;
9103 struct buffer *saved_current_buffer;
9104 int pt;
9105 int wanted_x;
9106
9107 /* Find point in a display of infinite width. */
9108 saved_current_buffer = current_buffer;
9109 current_buffer = XBUFFER (w->buffer);
9110
9111 if (w == XWINDOW (selected_window))
9112 pt = BUF_PT (current_buffer);
9113 else
9114 {
9115 pt = marker_position (w->pointm);
9116 pt = max (BEGV, pt);
9117 pt = min (ZV, pt);
9118 }
9119
9120 /* Move iterator to pt starting at cursor_row->start in
9121 a line with infinite width. */
9122 init_to_row_start (&it, w, cursor_row);
9123 it.last_visible_x = INFINITY;
9124 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9125 current_buffer = saved_current_buffer;
9126
9127 /* Position cursor in window. */
9128 if (!hscroll_relative_p && hscroll_step_abs == 0)
9129 hscroll = max (0, (it.current_x
9130 - (ITERATOR_AT_END_OF_LINE_P (&it)
9131 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9132 : (text_area_width / 2))))
9133 / FRAME_COLUMN_WIDTH (it.f);
9134 else if (w->cursor.x >= text_area_width - h_margin)
9135 {
9136 if (hscroll_relative_p)
9137 wanted_x = text_area_width * (1 - hscroll_step_rel)
9138 - h_margin;
9139 else
9140 wanted_x = text_area_width
9141 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9142 - h_margin;
9143 hscroll
9144 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9145 }
9146 else
9147 {
9148 if (hscroll_relative_p)
9149 wanted_x = text_area_width * hscroll_step_rel
9150 + h_margin;
9151 else
9152 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9153 + h_margin;
9154 hscroll
9155 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9156 }
9157 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9158
9159 /* Don't call Fset_window_hscroll if value hasn't
9160 changed because it will prevent redisplay
9161 optimizations. */
9162 if (XFASTINT (w->hscroll) != hscroll)
9163 {
9164 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9165 w->hscroll = make_number (hscroll);
9166 hscrolled_p = 1;
9167 }
9168 }
9169 }
9170
9171 window = w->next;
9172 }
9173
9174 /* Value is non-zero if hscroll of any leaf window has been changed. */
9175 return hscrolled_p;
9176 }
9177
9178
9179 /* Set hscroll so that cursor is visible and not inside horizontal
9180 scroll margins for all windows in the tree rooted at WINDOW. See
9181 also hscroll_window_tree above. Value is non-zero if any window's
9182 hscroll has been changed. If it has, desired matrices on the frame
9183 of WINDOW are cleared. */
9184
9185 static int
9186 hscroll_windows (window)
9187 Lisp_Object window;
9188 {
9189 int hscrolled_p;
9190
9191 if (automatic_hscrolling_p)
9192 {
9193 hscrolled_p = hscroll_window_tree (window);
9194 if (hscrolled_p)
9195 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9196 }
9197 else
9198 hscrolled_p = 0;
9199 return hscrolled_p;
9200 }
9201
9202
9203 \f
9204 /************************************************************************
9205 Redisplay
9206 ************************************************************************/
9207
9208 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9209 to a non-zero value. This is sometimes handy to have in a debugger
9210 session. */
9211
9212 #if GLYPH_DEBUG
9213
9214 /* First and last unchanged row for try_window_id. */
9215
9216 int debug_first_unchanged_at_end_vpos;
9217 int debug_last_unchanged_at_beg_vpos;
9218
9219 /* Delta vpos and y. */
9220
9221 int debug_dvpos, debug_dy;
9222
9223 /* Delta in characters and bytes for try_window_id. */
9224
9225 int debug_delta, debug_delta_bytes;
9226
9227 /* Values of window_end_pos and window_end_vpos at the end of
9228 try_window_id. */
9229
9230 EMACS_INT debug_end_pos, debug_end_vpos;
9231
9232 /* Append a string to W->desired_matrix->method. FMT is a printf
9233 format string. A1...A9 are a supplement for a variable-length
9234 argument list. If trace_redisplay_p is non-zero also printf the
9235 resulting string to stderr. */
9236
9237 static void
9238 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9239 struct window *w;
9240 char *fmt;
9241 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9242 {
9243 char buffer[512];
9244 char *method = w->desired_matrix->method;
9245 int len = strlen (method);
9246 int size = sizeof w->desired_matrix->method;
9247 int remaining = size - len - 1;
9248
9249 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9250 if (len && remaining)
9251 {
9252 method[len] = '|';
9253 --remaining, ++len;
9254 }
9255
9256 strncpy (method + len, buffer, remaining);
9257
9258 if (trace_redisplay_p)
9259 fprintf (stderr, "%p (%s): %s\n",
9260 w,
9261 ((BUFFERP (w->buffer)
9262 && STRINGP (XBUFFER (w->buffer)->name))
9263 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9264 : "no buffer"),
9265 buffer);
9266 }
9267
9268 #endif /* GLYPH_DEBUG */
9269
9270
9271 /* Value is non-zero if all changes in window W, which displays
9272 current_buffer, are in the text between START and END. START is a
9273 buffer position, END is given as a distance from Z. Used in
9274 redisplay_internal for display optimization. */
9275
9276 static INLINE int
9277 text_outside_line_unchanged_p (w, start, end)
9278 struct window *w;
9279 int start, end;
9280 {
9281 int unchanged_p = 1;
9282
9283 /* If text or overlays have changed, see where. */
9284 if (XFASTINT (w->last_modified) < MODIFF
9285 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9286 {
9287 /* Gap in the line? */
9288 if (GPT < start || Z - GPT < end)
9289 unchanged_p = 0;
9290
9291 /* Changes start in front of the line, or end after it? */
9292 if (unchanged_p
9293 && (BEG_UNCHANGED < start - 1
9294 || END_UNCHANGED < end))
9295 unchanged_p = 0;
9296
9297 /* If selective display, can't optimize if changes start at the
9298 beginning of the line. */
9299 if (unchanged_p
9300 && INTEGERP (current_buffer->selective_display)
9301 && XINT (current_buffer->selective_display) > 0
9302 && (BEG_UNCHANGED < start || GPT <= start))
9303 unchanged_p = 0;
9304
9305 /* If there are overlays at the start or end of the line, these
9306 may have overlay strings with newlines in them. A change at
9307 START, for instance, may actually concern the display of such
9308 overlay strings as well, and they are displayed on different
9309 lines. So, quickly rule out this case. (For the future, it
9310 might be desirable to implement something more telling than
9311 just BEG/END_UNCHANGED.) */
9312 if (unchanged_p)
9313 {
9314 if (BEG + BEG_UNCHANGED == start
9315 && overlay_touches_p (start))
9316 unchanged_p = 0;
9317 if (END_UNCHANGED == end
9318 && overlay_touches_p (Z - end))
9319 unchanged_p = 0;
9320 }
9321 }
9322
9323 return unchanged_p;
9324 }
9325
9326
9327 /* Do a frame update, taking possible shortcuts into account. This is
9328 the main external entry point for redisplay.
9329
9330 If the last redisplay displayed an echo area message and that message
9331 is no longer requested, we clear the echo area or bring back the
9332 mini-buffer if that is in use. */
9333
9334 void
9335 redisplay ()
9336 {
9337 redisplay_internal (0);
9338 }
9339
9340
9341 static Lisp_Object
9342 overlay_arrow_string_or_property (var, pbitmap)
9343 Lisp_Object var;
9344 int *pbitmap;
9345 {
9346 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9347 Lisp_Object bitmap;
9348
9349 if (pbitmap)
9350 {
9351 *pbitmap = 0;
9352 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9353 *pbitmap = XINT (bitmap);
9354 }
9355
9356 if (!NILP (pstr))
9357 return pstr;
9358 return Voverlay_arrow_string;
9359 }
9360
9361 /* Return 1 if there are any overlay-arrows in current_buffer. */
9362 static int
9363 overlay_arrow_in_current_buffer_p ()
9364 {
9365 Lisp_Object vlist;
9366
9367 for (vlist = Voverlay_arrow_variable_list;
9368 CONSP (vlist);
9369 vlist = XCDR (vlist))
9370 {
9371 Lisp_Object var = XCAR (vlist);
9372 Lisp_Object val;
9373
9374 if (!SYMBOLP (var))
9375 continue;
9376 val = find_symbol_value (var);
9377 if (MARKERP (val)
9378 && current_buffer == XMARKER (val)->buffer)
9379 return 1;
9380 }
9381 return 0;
9382 }
9383
9384
9385 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9386 has changed. */
9387
9388 static int
9389 overlay_arrows_changed_p ()
9390 {
9391 Lisp_Object vlist;
9392
9393 for (vlist = Voverlay_arrow_variable_list;
9394 CONSP (vlist);
9395 vlist = XCDR (vlist))
9396 {
9397 Lisp_Object var = XCAR (vlist);
9398 Lisp_Object val, pstr;
9399
9400 if (!SYMBOLP (var))
9401 continue;
9402 val = find_symbol_value (var);
9403 if (!MARKERP (val))
9404 continue;
9405 if (! EQ (COERCE_MARKER (val),
9406 Fget (var, Qlast_arrow_position))
9407 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9408 EQ (pstr, Fget (var, Qlast_arrow_string))))
9409 return 1;
9410 }
9411 return 0;
9412 }
9413
9414 /* Mark overlay arrows to be updated on next redisplay. */
9415
9416 static void
9417 update_overlay_arrows (up_to_date)
9418 int up_to_date;
9419 {
9420 Lisp_Object vlist;
9421
9422 for (vlist = Voverlay_arrow_variable_list;
9423 CONSP (vlist);
9424 vlist = XCDR (vlist))
9425 {
9426 Lisp_Object var = XCAR (vlist);
9427
9428 if (!SYMBOLP (var))
9429 continue;
9430
9431 if (up_to_date > 0)
9432 {
9433 Lisp_Object val = find_symbol_value (var);
9434 Fput (var, Qlast_arrow_position,
9435 COERCE_MARKER (val));
9436 Fput (var, Qlast_arrow_string,
9437 overlay_arrow_string_or_property (var, 0));
9438 }
9439 else if (up_to_date < 0
9440 || !NILP (Fget (var, Qlast_arrow_position)))
9441 {
9442 Fput (var, Qlast_arrow_position, Qt);
9443 Fput (var, Qlast_arrow_string, Qt);
9444 }
9445 }
9446 }
9447
9448
9449 /* Return overlay arrow string at row, or nil. */
9450
9451 static Lisp_Object
9452 overlay_arrow_at_row (f, row, pbitmap)
9453 struct frame *f;
9454 struct glyph_row *row;
9455 int *pbitmap;
9456 {
9457 Lisp_Object vlist;
9458
9459 for (vlist = Voverlay_arrow_variable_list;
9460 CONSP (vlist);
9461 vlist = XCDR (vlist))
9462 {
9463 Lisp_Object var = XCAR (vlist);
9464 Lisp_Object val;
9465
9466 if (!SYMBOLP (var))
9467 continue;
9468
9469 val = find_symbol_value (var);
9470
9471 if (MARKERP (val)
9472 && current_buffer == XMARKER (val)->buffer
9473 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9474 {
9475 val = overlay_arrow_string_or_property (var, pbitmap);
9476 if (FRAME_WINDOW_P (f))
9477 return Qt;
9478 else if (STRINGP (val))
9479 return val;
9480 break;
9481 }
9482 }
9483
9484 *pbitmap = 0;
9485 return Qnil;
9486 }
9487
9488 /* Return 1 if point moved out of or into a composition. Otherwise
9489 return 0. PREV_BUF and PREV_PT are the last point buffer and
9490 position. BUF and PT are the current point buffer and position. */
9491
9492 int
9493 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9494 struct buffer *prev_buf, *buf;
9495 int prev_pt, pt;
9496 {
9497 int start, end;
9498 Lisp_Object prop;
9499 Lisp_Object buffer;
9500
9501 XSETBUFFER (buffer, buf);
9502 /* Check a composition at the last point if point moved within the
9503 same buffer. */
9504 if (prev_buf == buf)
9505 {
9506 if (prev_pt == pt)
9507 /* Point didn't move. */
9508 return 0;
9509
9510 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9511 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9512 && COMPOSITION_VALID_P (start, end, prop)
9513 && start < prev_pt && end > prev_pt)
9514 /* The last point was within the composition. Return 1 iff
9515 point moved out of the composition. */
9516 return (pt <= start || pt >= end);
9517 }
9518
9519 /* Check a composition at the current point. */
9520 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9521 && find_composition (pt, -1, &start, &end, &prop, buffer)
9522 && COMPOSITION_VALID_P (start, end, prop)
9523 && start < pt && end > pt);
9524 }
9525
9526
9527 /* Reconsider the setting of B->clip_changed which is displayed
9528 in window W. */
9529
9530 static INLINE void
9531 reconsider_clip_changes (w, b)
9532 struct window *w;
9533 struct buffer *b;
9534 {
9535 if (b->clip_changed
9536 && !NILP (w->window_end_valid)
9537 && w->current_matrix->buffer == b
9538 && w->current_matrix->zv == BUF_ZV (b)
9539 && w->current_matrix->begv == BUF_BEGV (b))
9540 b->clip_changed = 0;
9541
9542 /* If display wasn't paused, and W is not a tool bar window, see if
9543 point has been moved into or out of a composition. In that case,
9544 we set b->clip_changed to 1 to force updating the screen. If
9545 b->clip_changed has already been set to 1, we can skip this
9546 check. */
9547 if (!b->clip_changed
9548 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9549 {
9550 int pt;
9551
9552 if (w == XWINDOW (selected_window))
9553 pt = BUF_PT (current_buffer);
9554 else
9555 pt = marker_position (w->pointm);
9556
9557 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9558 || pt != XINT (w->last_point))
9559 && check_point_in_composition (w->current_matrix->buffer,
9560 XINT (w->last_point),
9561 XBUFFER (w->buffer), pt))
9562 b->clip_changed = 1;
9563 }
9564 }
9565 \f
9566
9567 /* Select FRAME to forward the values of frame-local variables into C
9568 variables so that the redisplay routines can access those values
9569 directly. */
9570
9571 static void
9572 select_frame_for_redisplay (frame)
9573 Lisp_Object frame;
9574 {
9575 Lisp_Object tail, sym, val;
9576 Lisp_Object old = selected_frame;
9577
9578 selected_frame = frame;
9579
9580 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9581 if (CONSP (XCAR (tail))
9582 && (sym = XCAR (XCAR (tail)),
9583 SYMBOLP (sym))
9584 && (sym = indirect_variable (sym),
9585 val = SYMBOL_VALUE (sym),
9586 (BUFFER_LOCAL_VALUEP (val)
9587 || SOME_BUFFER_LOCAL_VALUEP (val)))
9588 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9589 Fsymbol_value (sym);
9590
9591 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9592 if (CONSP (XCAR (tail))
9593 && (sym = XCAR (XCAR (tail)),
9594 SYMBOLP (sym))
9595 && (sym = indirect_variable (sym),
9596 val = SYMBOL_VALUE (sym),
9597 (BUFFER_LOCAL_VALUEP (val)
9598 || SOME_BUFFER_LOCAL_VALUEP (val)))
9599 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9600 Fsymbol_value (sym);
9601 }
9602
9603
9604 #define STOP_POLLING \
9605 do { if (! polling_stopped_here) stop_polling (); \
9606 polling_stopped_here = 1; } while (0)
9607
9608 #define RESUME_POLLING \
9609 do { if (polling_stopped_here) start_polling (); \
9610 polling_stopped_here = 0; } while (0)
9611
9612
9613 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9614 response to any user action; therefore, we should preserve the echo
9615 area. (Actually, our caller does that job.) Perhaps in the future
9616 avoid recentering windows if it is not necessary; currently that
9617 causes some problems. */
9618
9619 static void
9620 redisplay_internal (preserve_echo_area)
9621 int preserve_echo_area;
9622 {
9623 struct window *w = XWINDOW (selected_window);
9624 struct frame *f = XFRAME (w->frame);
9625 int pause;
9626 int must_finish = 0;
9627 struct text_pos tlbufpos, tlendpos;
9628 int number_of_visible_frames;
9629 int count;
9630 struct frame *sf = SELECTED_FRAME ();
9631 int polling_stopped_here = 0;
9632
9633 /* Non-zero means redisplay has to consider all windows on all
9634 frames. Zero means, only selected_window is considered. */
9635 int consider_all_windows_p;
9636
9637 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9638
9639 /* No redisplay if running in batch mode or frame is not yet fully
9640 initialized, or redisplay is explicitly turned off by setting
9641 Vinhibit_redisplay. */
9642 if (noninteractive
9643 || !NILP (Vinhibit_redisplay)
9644 || !f->glyphs_initialized_p)
9645 return;
9646
9647 /* The flag redisplay_performed_directly_p is set by
9648 direct_output_for_insert when it already did the whole screen
9649 update necessary. */
9650 if (redisplay_performed_directly_p)
9651 {
9652 redisplay_performed_directly_p = 0;
9653 if (!hscroll_windows (selected_window))
9654 return;
9655 }
9656
9657 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9658 if (popup_activated ())
9659 return;
9660 #endif
9661
9662 /* I don't think this happens but let's be paranoid. */
9663 if (redisplaying_p)
9664 return;
9665
9666 /* Record a function that resets redisplaying_p to its old value
9667 when we leave this function. */
9668 count = SPECPDL_INDEX ();
9669 record_unwind_protect (unwind_redisplay,
9670 Fcons (make_number (redisplaying_p), selected_frame));
9671 ++redisplaying_p;
9672 specbind (Qinhibit_free_realized_faces, Qnil);
9673
9674 retry:
9675 pause = 0;
9676 reconsider_clip_changes (w, current_buffer);
9677
9678 /* If new fonts have been loaded that make a glyph matrix adjustment
9679 necessary, do it. */
9680 if (fonts_changed_p)
9681 {
9682 adjust_glyphs (NULL);
9683 ++windows_or_buffers_changed;
9684 fonts_changed_p = 0;
9685 }
9686
9687 /* If face_change_count is non-zero, init_iterator will free all
9688 realized faces, which includes the faces referenced from current
9689 matrices. So, we can't reuse current matrices in this case. */
9690 if (face_change_count)
9691 ++windows_or_buffers_changed;
9692
9693 if (! FRAME_WINDOW_P (sf)
9694 && previous_terminal_frame != sf)
9695 {
9696 /* Since frames on an ASCII terminal share the same display
9697 area, displaying a different frame means redisplay the whole
9698 thing. */
9699 windows_or_buffers_changed++;
9700 SET_FRAME_GARBAGED (sf);
9701 XSETFRAME (Vterminal_frame, sf);
9702 }
9703 previous_terminal_frame = sf;
9704
9705 /* Set the visible flags for all frames. Do this before checking
9706 for resized or garbaged frames; they want to know if their frames
9707 are visible. See the comment in frame.h for
9708 FRAME_SAMPLE_VISIBILITY. */
9709 {
9710 Lisp_Object tail, frame;
9711
9712 number_of_visible_frames = 0;
9713
9714 FOR_EACH_FRAME (tail, frame)
9715 {
9716 struct frame *f = XFRAME (frame);
9717
9718 FRAME_SAMPLE_VISIBILITY (f);
9719 if (FRAME_VISIBLE_P (f))
9720 ++number_of_visible_frames;
9721 clear_desired_matrices (f);
9722 }
9723 }
9724
9725 /* Notice any pending interrupt request to change frame size. */
9726 do_pending_window_change (1);
9727
9728 /* Clear frames marked as garbaged. */
9729 if (frame_garbaged)
9730 clear_garbaged_frames ();
9731
9732 /* Build menubar and tool-bar items. */
9733 prepare_menu_bars ();
9734
9735 if (windows_or_buffers_changed)
9736 update_mode_lines++;
9737
9738 /* Detect case that we need to write or remove a star in the mode line. */
9739 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9740 {
9741 w->update_mode_line = Qt;
9742 if (buffer_shared > 1)
9743 update_mode_lines++;
9744 }
9745
9746 /* If %c is in the mode line, update it if needed. */
9747 if (!NILP (w->column_number_displayed)
9748 /* This alternative quickly identifies a common case
9749 where no change is needed. */
9750 && !(PT == XFASTINT (w->last_point)
9751 && XFASTINT (w->last_modified) >= MODIFF
9752 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9753 && (XFASTINT (w->column_number_displayed)
9754 != (int) current_column ())) /* iftc */
9755 w->update_mode_line = Qt;
9756
9757 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9758
9759 /* The variable buffer_shared is set in redisplay_window and
9760 indicates that we redisplay a buffer in different windows. See
9761 there. */
9762 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9763 || cursor_type_changed);
9764
9765 /* If specs for an arrow have changed, do thorough redisplay
9766 to ensure we remove any arrow that should no longer exist. */
9767 if (overlay_arrows_changed_p ())
9768 consider_all_windows_p = windows_or_buffers_changed = 1;
9769
9770 /* Normally the message* functions will have already displayed and
9771 updated the echo area, but the frame may have been trashed, or
9772 the update may have been preempted, so display the echo area
9773 again here. Checking message_cleared_p captures the case that
9774 the echo area should be cleared. */
9775 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9776 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9777 || (message_cleared_p
9778 && minibuf_level == 0
9779 /* If the mini-window is currently selected, this means the
9780 echo-area doesn't show through. */
9781 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9782 {
9783 int window_height_changed_p = echo_area_display (0);
9784 must_finish = 1;
9785
9786 /* If we don't display the current message, don't clear the
9787 message_cleared_p flag, because, if we did, we wouldn't clear
9788 the echo area in the next redisplay which doesn't preserve
9789 the echo area. */
9790 if (!display_last_displayed_message_p)
9791 message_cleared_p = 0;
9792
9793 if (fonts_changed_p)
9794 goto retry;
9795 else if (window_height_changed_p)
9796 {
9797 consider_all_windows_p = 1;
9798 ++update_mode_lines;
9799 ++windows_or_buffers_changed;
9800
9801 /* If window configuration was changed, frames may have been
9802 marked garbaged. Clear them or we will experience
9803 surprises wrt scrolling. */
9804 if (frame_garbaged)
9805 clear_garbaged_frames ();
9806 }
9807 }
9808 else if (EQ (selected_window, minibuf_window)
9809 && (current_buffer->clip_changed
9810 || XFASTINT (w->last_modified) < MODIFF
9811 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9812 && resize_mini_window (w, 0))
9813 {
9814 /* Resized active mini-window to fit the size of what it is
9815 showing if its contents might have changed. */
9816 must_finish = 1;
9817 consider_all_windows_p = 1;
9818 ++windows_or_buffers_changed;
9819 ++update_mode_lines;
9820
9821 /* If window configuration was changed, frames may have been
9822 marked garbaged. Clear them or we will experience
9823 surprises wrt scrolling. */
9824 if (frame_garbaged)
9825 clear_garbaged_frames ();
9826 }
9827
9828
9829 /* If showing the region, and mark has changed, we must redisplay
9830 the whole window. The assignment to this_line_start_pos prevents
9831 the optimization directly below this if-statement. */
9832 if (((!NILP (Vtransient_mark_mode)
9833 && !NILP (XBUFFER (w->buffer)->mark_active))
9834 != !NILP (w->region_showing))
9835 || (!NILP (w->region_showing)
9836 && !EQ (w->region_showing,
9837 Fmarker_position (XBUFFER (w->buffer)->mark))))
9838 CHARPOS (this_line_start_pos) = 0;
9839
9840 /* Optimize the case that only the line containing the cursor in the
9841 selected window has changed. Variables starting with this_ are
9842 set in display_line and record information about the line
9843 containing the cursor. */
9844 tlbufpos = this_line_start_pos;
9845 tlendpos = this_line_end_pos;
9846 if (!consider_all_windows_p
9847 && CHARPOS (tlbufpos) > 0
9848 && NILP (w->update_mode_line)
9849 && !current_buffer->clip_changed
9850 && !current_buffer->prevent_redisplay_optimizations_p
9851 && FRAME_VISIBLE_P (XFRAME (w->frame))
9852 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9853 /* Make sure recorded data applies to current buffer, etc. */
9854 && this_line_buffer == current_buffer
9855 && current_buffer == XBUFFER (w->buffer)
9856 && NILP (w->force_start)
9857 && NILP (w->optional_new_start)
9858 /* Point must be on the line that we have info recorded about. */
9859 && PT >= CHARPOS (tlbufpos)
9860 && PT <= Z - CHARPOS (tlendpos)
9861 /* All text outside that line, including its final newline,
9862 must be unchanged */
9863 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9864 CHARPOS (tlendpos)))
9865 {
9866 if (CHARPOS (tlbufpos) > BEGV
9867 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9868 && (CHARPOS (tlbufpos) == ZV
9869 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9870 /* Former continuation line has disappeared by becoming empty */
9871 goto cancel;
9872 else if (XFASTINT (w->last_modified) < MODIFF
9873 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9874 || MINI_WINDOW_P (w))
9875 {
9876 /* We have to handle the case of continuation around a
9877 wide-column character (See the comment in indent.c around
9878 line 885).
9879
9880 For instance, in the following case:
9881
9882 -------- Insert --------
9883 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9884 J_I_ ==> J_I_ `^^' are cursors.
9885 ^^ ^^
9886 -------- --------
9887
9888 As we have to redraw the line above, we should goto cancel. */
9889
9890 struct it it;
9891 int line_height_before = this_line_pixel_height;
9892
9893 /* Note that start_display will handle the case that the
9894 line starting at tlbufpos is a continuation lines. */
9895 start_display (&it, w, tlbufpos);
9896
9897 /* Implementation note: It this still necessary? */
9898 if (it.current_x != this_line_start_x)
9899 goto cancel;
9900
9901 TRACE ((stderr, "trying display optimization 1\n"));
9902 w->cursor.vpos = -1;
9903 overlay_arrow_seen = 0;
9904 it.vpos = this_line_vpos;
9905 it.current_y = this_line_y;
9906 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9907 display_line (&it);
9908
9909 /* If line contains point, is not continued,
9910 and ends at same distance from eob as before, we win */
9911 if (w->cursor.vpos >= 0
9912 /* Line is not continued, otherwise this_line_start_pos
9913 would have been set to 0 in display_line. */
9914 && CHARPOS (this_line_start_pos)
9915 /* Line ends as before. */
9916 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9917 /* Line has same height as before. Otherwise other lines
9918 would have to be shifted up or down. */
9919 && this_line_pixel_height == line_height_before)
9920 {
9921 /* If this is not the window's last line, we must adjust
9922 the charstarts of the lines below. */
9923 if (it.current_y < it.last_visible_y)
9924 {
9925 struct glyph_row *row
9926 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9927 int delta, delta_bytes;
9928
9929 if (Z - CHARPOS (tlendpos) == ZV)
9930 {
9931 /* This line ends at end of (accessible part of)
9932 buffer. There is no newline to count. */
9933 delta = (Z
9934 - CHARPOS (tlendpos)
9935 - MATRIX_ROW_START_CHARPOS (row));
9936 delta_bytes = (Z_BYTE
9937 - BYTEPOS (tlendpos)
9938 - MATRIX_ROW_START_BYTEPOS (row));
9939 }
9940 else
9941 {
9942 /* This line ends in a newline. Must take
9943 account of the newline and the rest of the
9944 text that follows. */
9945 delta = (Z
9946 - CHARPOS (tlendpos)
9947 - MATRIX_ROW_START_CHARPOS (row));
9948 delta_bytes = (Z_BYTE
9949 - BYTEPOS (tlendpos)
9950 - MATRIX_ROW_START_BYTEPOS (row));
9951 }
9952
9953 increment_matrix_positions (w->current_matrix,
9954 this_line_vpos + 1,
9955 w->current_matrix->nrows,
9956 delta, delta_bytes);
9957 }
9958
9959 /* If this row displays text now but previously didn't,
9960 or vice versa, w->window_end_vpos may have to be
9961 adjusted. */
9962 if ((it.glyph_row - 1)->displays_text_p)
9963 {
9964 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9965 XSETINT (w->window_end_vpos, this_line_vpos);
9966 }
9967 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9968 && this_line_vpos > 0)
9969 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9970 w->window_end_valid = Qnil;
9971
9972 /* Update hint: No need to try to scroll in update_window. */
9973 w->desired_matrix->no_scrolling_p = 1;
9974
9975 #if GLYPH_DEBUG
9976 *w->desired_matrix->method = 0;
9977 debug_method_add (w, "optimization 1");
9978 #endif
9979 #ifdef HAVE_WINDOW_SYSTEM
9980 update_window_fringes (w, 0);
9981 #endif
9982 goto update;
9983 }
9984 else
9985 goto cancel;
9986 }
9987 else if (/* Cursor position hasn't changed. */
9988 PT == XFASTINT (w->last_point)
9989 /* Make sure the cursor was last displayed
9990 in this window. Otherwise we have to reposition it. */
9991 && 0 <= w->cursor.vpos
9992 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9993 {
9994 if (!must_finish)
9995 {
9996 do_pending_window_change (1);
9997
9998 /* We used to always goto end_of_redisplay here, but this
9999 isn't enough if we have a blinking cursor. */
10000 if (w->cursor_off_p == w->last_cursor_off_p)
10001 goto end_of_redisplay;
10002 }
10003 goto update;
10004 }
10005 /* If highlighting the region, or if the cursor is in the echo area,
10006 then we can't just move the cursor. */
10007 else if (! (!NILP (Vtransient_mark_mode)
10008 && !NILP (current_buffer->mark_active))
10009 && (EQ (selected_window, current_buffer->last_selected_window)
10010 || highlight_nonselected_windows)
10011 && NILP (w->region_showing)
10012 && NILP (Vshow_trailing_whitespace)
10013 && !cursor_in_echo_area)
10014 {
10015 struct it it;
10016 struct glyph_row *row;
10017
10018 /* Skip from tlbufpos to PT and see where it is. Note that
10019 PT may be in invisible text. If so, we will end at the
10020 next visible position. */
10021 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10022 NULL, DEFAULT_FACE_ID);
10023 it.current_x = this_line_start_x;
10024 it.current_y = this_line_y;
10025 it.vpos = this_line_vpos;
10026
10027 /* The call to move_it_to stops in front of PT, but
10028 moves over before-strings. */
10029 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10030
10031 if (it.vpos == this_line_vpos
10032 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10033 row->enabled_p))
10034 {
10035 xassert (this_line_vpos == it.vpos);
10036 xassert (this_line_y == it.current_y);
10037 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10038 #if GLYPH_DEBUG
10039 *w->desired_matrix->method = 0;
10040 debug_method_add (w, "optimization 3");
10041 #endif
10042 goto update;
10043 }
10044 else
10045 goto cancel;
10046 }
10047
10048 cancel:
10049 /* Text changed drastically or point moved off of line. */
10050 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10051 }
10052
10053 CHARPOS (this_line_start_pos) = 0;
10054 consider_all_windows_p |= buffer_shared > 1;
10055 ++clear_face_cache_count;
10056
10057
10058 /* Build desired matrices, and update the display. If
10059 consider_all_windows_p is non-zero, do it for all windows on all
10060 frames. Otherwise do it for selected_window, only. */
10061
10062 if (consider_all_windows_p)
10063 {
10064 Lisp_Object tail, frame;
10065 int i, n = 0, size = 50;
10066 struct frame **updated
10067 = (struct frame **) alloca (size * sizeof *updated);
10068
10069 /* Clear the face cache eventually. */
10070 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10071 {
10072 clear_face_cache (0);
10073 clear_face_cache_count = 0;
10074 }
10075
10076 /* Recompute # windows showing selected buffer. This will be
10077 incremented each time such a window is displayed. */
10078 buffer_shared = 0;
10079
10080 FOR_EACH_FRAME (tail, frame)
10081 {
10082 struct frame *f = XFRAME (frame);
10083
10084 if (FRAME_WINDOW_P (f) || f == sf)
10085 {
10086 if (! EQ (frame, selected_frame))
10087 /* Select the frame, for the sake of frame-local
10088 variables. */
10089 select_frame_for_redisplay (frame);
10090
10091 #ifdef HAVE_WINDOW_SYSTEM
10092 if (clear_face_cache_count % 50 == 0
10093 && FRAME_WINDOW_P (f))
10094 clear_image_cache (f, 0);
10095 #endif /* HAVE_WINDOW_SYSTEM */
10096
10097 /* Mark all the scroll bars to be removed; we'll redeem
10098 the ones we want when we redisplay their windows. */
10099 if (condemn_scroll_bars_hook)
10100 condemn_scroll_bars_hook (f);
10101
10102 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10103 redisplay_windows (FRAME_ROOT_WINDOW (f));
10104
10105 /* Any scroll bars which redisplay_windows should have
10106 nuked should now go away. */
10107 if (judge_scroll_bars_hook)
10108 judge_scroll_bars_hook (f);
10109
10110 /* If fonts changed, display again. */
10111 /* ??? rms: I suspect it is a mistake to jump all the way
10112 back to retry here. It should just retry this frame. */
10113 if (fonts_changed_p)
10114 goto retry;
10115
10116 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10117 {
10118 /* See if we have to hscroll. */
10119 if (hscroll_windows (f->root_window))
10120 goto retry;
10121
10122 /* Prevent various kinds of signals during display
10123 update. stdio is not robust about handling
10124 signals, which can cause an apparent I/O
10125 error. */
10126 if (interrupt_input)
10127 unrequest_sigio ();
10128 STOP_POLLING;
10129
10130 /* Update the display. */
10131 set_window_update_flags (XWINDOW (f->root_window), 1);
10132 pause |= update_frame (f, 0, 0);
10133 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10134 if (pause)
10135 break;
10136 #endif
10137
10138 if (n == size)
10139 {
10140 int nbytes = size * sizeof *updated;
10141 struct frame **p = (struct frame **) alloca (2 * nbytes);
10142 bcopy (updated, p, nbytes);
10143 size *= 2;
10144 }
10145
10146 updated[n++] = f;
10147 }
10148 }
10149 }
10150
10151 if (!pause)
10152 {
10153 /* Do the mark_window_display_accurate after all windows have
10154 been redisplayed because this call resets flags in buffers
10155 which are needed for proper redisplay. */
10156 for (i = 0; i < n; ++i)
10157 {
10158 struct frame *f = updated[i];
10159 mark_window_display_accurate (f->root_window, 1);
10160 if (frame_up_to_date_hook)
10161 frame_up_to_date_hook (f);
10162 }
10163 }
10164 }
10165 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10166 {
10167 Lisp_Object mini_window;
10168 struct frame *mini_frame;
10169
10170 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10171 /* Use list_of_error, not Qerror, so that
10172 we catch only errors and don't run the debugger. */
10173 internal_condition_case_1 (redisplay_window_1, selected_window,
10174 list_of_error,
10175 redisplay_window_error);
10176
10177 /* Compare desired and current matrices, perform output. */
10178
10179 update:
10180 /* If fonts changed, display again. */
10181 if (fonts_changed_p)
10182 goto retry;
10183
10184 /* Prevent various kinds of signals during display update.
10185 stdio is not robust about handling signals,
10186 which can cause an apparent I/O error. */
10187 if (interrupt_input)
10188 unrequest_sigio ();
10189 STOP_POLLING;
10190
10191 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10192 {
10193 if (hscroll_windows (selected_window))
10194 goto retry;
10195
10196 XWINDOW (selected_window)->must_be_updated_p = 1;
10197 pause = update_frame (sf, 0, 0);
10198 }
10199
10200 /* We may have called echo_area_display at the top of this
10201 function. If the echo area is on another frame, that may
10202 have put text on a frame other than the selected one, so the
10203 above call to update_frame would not have caught it. Catch
10204 it here. */
10205 mini_window = FRAME_MINIBUF_WINDOW (sf);
10206 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10207
10208 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10209 {
10210 XWINDOW (mini_window)->must_be_updated_p = 1;
10211 pause |= update_frame (mini_frame, 0, 0);
10212 if (!pause && hscroll_windows (mini_window))
10213 goto retry;
10214 }
10215 }
10216
10217 /* If display was paused because of pending input, make sure we do a
10218 thorough update the next time. */
10219 if (pause)
10220 {
10221 /* Prevent the optimization at the beginning of
10222 redisplay_internal that tries a single-line update of the
10223 line containing the cursor in the selected window. */
10224 CHARPOS (this_line_start_pos) = 0;
10225
10226 /* Let the overlay arrow be updated the next time. */
10227 update_overlay_arrows (0);
10228
10229 /* If we pause after scrolling, some rows in the current
10230 matrices of some windows are not valid. */
10231 if (!WINDOW_FULL_WIDTH_P (w)
10232 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10233 update_mode_lines = 1;
10234 }
10235 else
10236 {
10237 if (!consider_all_windows_p)
10238 {
10239 /* This has already been done above if
10240 consider_all_windows_p is set. */
10241 mark_window_display_accurate_1 (w, 1);
10242
10243 /* Say overlay arrows are up to date. */
10244 update_overlay_arrows (1);
10245
10246 if (frame_up_to_date_hook != 0)
10247 frame_up_to_date_hook (sf);
10248 }
10249
10250 update_mode_lines = 0;
10251 windows_or_buffers_changed = 0;
10252 cursor_type_changed = 0;
10253 }
10254
10255 /* Start SIGIO interrupts coming again. Having them off during the
10256 code above makes it less likely one will discard output, but not
10257 impossible, since there might be stuff in the system buffer here.
10258 But it is much hairier to try to do anything about that. */
10259 if (interrupt_input)
10260 request_sigio ();
10261 RESUME_POLLING;
10262
10263 /* If a frame has become visible which was not before, redisplay
10264 again, so that we display it. Expose events for such a frame
10265 (which it gets when becoming visible) don't call the parts of
10266 redisplay constructing glyphs, so simply exposing a frame won't
10267 display anything in this case. So, we have to display these
10268 frames here explicitly. */
10269 if (!pause)
10270 {
10271 Lisp_Object tail, frame;
10272 int new_count = 0;
10273
10274 FOR_EACH_FRAME (tail, frame)
10275 {
10276 int this_is_visible = 0;
10277
10278 if (XFRAME (frame)->visible)
10279 this_is_visible = 1;
10280 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10281 if (XFRAME (frame)->visible)
10282 this_is_visible = 1;
10283
10284 if (this_is_visible)
10285 new_count++;
10286 }
10287
10288 if (new_count != number_of_visible_frames)
10289 windows_or_buffers_changed++;
10290 }
10291
10292 /* Change frame size now if a change is pending. */
10293 do_pending_window_change (1);
10294
10295 /* If we just did a pending size change, or have additional
10296 visible frames, redisplay again. */
10297 if (windows_or_buffers_changed && !pause)
10298 goto retry;
10299
10300 end_of_redisplay:
10301 unbind_to (count, Qnil);
10302 RESUME_POLLING;
10303 }
10304
10305
10306 /* Redisplay, but leave alone any recent echo area message unless
10307 another message has been requested in its place.
10308
10309 This is useful in situations where you need to redisplay but no
10310 user action has occurred, making it inappropriate for the message
10311 area to be cleared. See tracking_off and
10312 wait_reading_process_input for examples of these situations.
10313
10314 FROM_WHERE is an integer saying from where this function was
10315 called. This is useful for debugging. */
10316
10317 void
10318 redisplay_preserve_echo_area (from_where)
10319 int from_where;
10320 {
10321 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10322
10323 if (!NILP (echo_area_buffer[1]))
10324 {
10325 /* We have a previously displayed message, but no current
10326 message. Redisplay the previous message. */
10327 display_last_displayed_message_p = 1;
10328 redisplay_internal (1);
10329 display_last_displayed_message_p = 0;
10330 }
10331 else
10332 redisplay_internal (1);
10333 }
10334
10335
10336 /* Function registered with record_unwind_protect in
10337 redisplay_internal. Reset redisplaying_p to the value it had
10338 before redisplay_internal was called, and clear
10339 prevent_freeing_realized_faces_p. It also selects the previously
10340 selected frame. */
10341
10342 static Lisp_Object
10343 unwind_redisplay (val)
10344 Lisp_Object val;
10345 {
10346 Lisp_Object old_redisplaying_p, old_frame;
10347
10348 old_redisplaying_p = XCAR (val);
10349 redisplaying_p = XFASTINT (old_redisplaying_p);
10350 old_frame = XCDR (val);
10351 if (! EQ (old_frame, selected_frame))
10352 select_frame_for_redisplay (old_frame);
10353 return Qnil;
10354 }
10355
10356
10357 /* Mark the display of window W as accurate or inaccurate. If
10358 ACCURATE_P is non-zero mark display of W as accurate. If
10359 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10360 redisplay_internal is called. */
10361
10362 static void
10363 mark_window_display_accurate_1 (w, accurate_p)
10364 struct window *w;
10365 int accurate_p;
10366 {
10367 if (BUFFERP (w->buffer))
10368 {
10369 struct buffer *b = XBUFFER (w->buffer);
10370
10371 w->last_modified
10372 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10373 w->last_overlay_modified
10374 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10375 w->last_had_star
10376 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10377
10378 if (accurate_p)
10379 {
10380 b->clip_changed = 0;
10381 b->prevent_redisplay_optimizations_p = 0;
10382
10383 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10384 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10385 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10386 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10387
10388 w->current_matrix->buffer = b;
10389 w->current_matrix->begv = BUF_BEGV (b);
10390 w->current_matrix->zv = BUF_ZV (b);
10391
10392 w->last_cursor = w->cursor;
10393 w->last_cursor_off_p = w->cursor_off_p;
10394
10395 if (w == XWINDOW (selected_window))
10396 w->last_point = make_number (BUF_PT (b));
10397 else
10398 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10399 }
10400 }
10401
10402 if (accurate_p)
10403 {
10404 w->window_end_valid = w->buffer;
10405 #if 0 /* This is incorrect with variable-height lines. */
10406 xassert (XINT (w->window_end_vpos)
10407 < (WINDOW_TOTAL_LINES (w)
10408 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10409 #endif
10410 w->update_mode_line = Qnil;
10411 }
10412 }
10413
10414
10415 /* Mark the display of windows in the window tree rooted at WINDOW as
10416 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10417 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10418 be redisplayed the next time redisplay_internal is called. */
10419
10420 void
10421 mark_window_display_accurate (window, accurate_p)
10422 Lisp_Object window;
10423 int accurate_p;
10424 {
10425 struct window *w;
10426
10427 for (; !NILP (window); window = w->next)
10428 {
10429 w = XWINDOW (window);
10430 mark_window_display_accurate_1 (w, accurate_p);
10431
10432 if (!NILP (w->vchild))
10433 mark_window_display_accurate (w->vchild, accurate_p);
10434 if (!NILP (w->hchild))
10435 mark_window_display_accurate (w->hchild, accurate_p);
10436 }
10437
10438 if (accurate_p)
10439 {
10440 update_overlay_arrows (1);
10441 }
10442 else
10443 {
10444 /* Force a thorough redisplay the next time by setting
10445 last_arrow_position and last_arrow_string to t, which is
10446 unequal to any useful value of Voverlay_arrow_... */
10447 update_overlay_arrows (-1);
10448 }
10449 }
10450
10451
10452 /* Return value in display table DP (Lisp_Char_Table *) for character
10453 C. Since a display table doesn't have any parent, we don't have to
10454 follow parent. Do not call this function directly but use the
10455 macro DISP_CHAR_VECTOR. */
10456
10457 Lisp_Object
10458 disp_char_vector (dp, c)
10459 struct Lisp_Char_Table *dp;
10460 int c;
10461 {
10462 int code[4], i;
10463 Lisp_Object val;
10464
10465 if (SINGLE_BYTE_CHAR_P (c))
10466 return (dp->contents[c]);
10467
10468 SPLIT_CHAR (c, code[0], code[1], code[2]);
10469 if (code[1] < 32)
10470 code[1] = -1;
10471 else if (code[2] < 32)
10472 code[2] = -1;
10473
10474 /* Here, the possible range of code[0] (== charset ID) is
10475 128..max_charset. Since the top level char table contains data
10476 for multibyte characters after 256th element, we must increment
10477 code[0] by 128 to get a correct index. */
10478 code[0] += 128;
10479 code[3] = -1; /* anchor */
10480
10481 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10482 {
10483 val = dp->contents[code[i]];
10484 if (!SUB_CHAR_TABLE_P (val))
10485 return (NILP (val) ? dp->defalt : val);
10486 }
10487
10488 /* Here, val is a sub char table. We return the default value of
10489 it. */
10490 return (dp->defalt);
10491 }
10492
10493
10494 \f
10495 /***********************************************************************
10496 Window Redisplay
10497 ***********************************************************************/
10498
10499 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10500
10501 static void
10502 redisplay_windows (window)
10503 Lisp_Object window;
10504 {
10505 while (!NILP (window))
10506 {
10507 struct window *w = XWINDOW (window);
10508
10509 if (!NILP (w->hchild))
10510 redisplay_windows (w->hchild);
10511 else if (!NILP (w->vchild))
10512 redisplay_windows (w->vchild);
10513 else
10514 {
10515 displayed_buffer = XBUFFER (w->buffer);
10516 /* Use list_of_error, not Qerror, so that
10517 we catch only errors and don't run the debugger. */
10518 internal_condition_case_1 (redisplay_window_0, window,
10519 list_of_error,
10520 redisplay_window_error);
10521 }
10522
10523 window = w->next;
10524 }
10525 }
10526
10527 static Lisp_Object
10528 redisplay_window_error ()
10529 {
10530 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10531 return Qnil;
10532 }
10533
10534 static Lisp_Object
10535 redisplay_window_0 (window)
10536 Lisp_Object window;
10537 {
10538 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10539 redisplay_window (window, 0);
10540 return Qnil;
10541 }
10542
10543 static Lisp_Object
10544 redisplay_window_1 (window)
10545 Lisp_Object window;
10546 {
10547 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10548 redisplay_window (window, 1);
10549 return Qnil;
10550 }
10551 \f
10552
10553 /* Increment GLYPH until it reaches END or CONDITION fails while
10554 adding (GLYPH)->pixel_width to X. */
10555
10556 #define SKIP_GLYPHS(glyph, end, x, condition) \
10557 do \
10558 { \
10559 (x) += (glyph)->pixel_width; \
10560 ++(glyph); \
10561 } \
10562 while ((glyph) < (end) && (condition))
10563
10564
10565 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10566 DELTA is the number of bytes by which positions recorded in ROW
10567 differ from current buffer positions. */
10568
10569 void
10570 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10571 struct window *w;
10572 struct glyph_row *row;
10573 struct glyph_matrix *matrix;
10574 int delta, delta_bytes, dy, dvpos;
10575 {
10576 struct glyph *glyph = row->glyphs[TEXT_AREA];
10577 struct glyph *end = glyph + row->used[TEXT_AREA];
10578 /* The first glyph that starts a sequence of glyphs from string. */
10579 struct glyph *string_start;
10580 /* The X coordinate of string_start. */
10581 int string_start_x;
10582 /* The last known character position. */
10583 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10584 /* The last known character position before string_start. */
10585 int string_before_pos;
10586 int x = row->x;
10587 int pt_old = PT - delta;
10588
10589 /* Skip over glyphs not having an object at the start of the row.
10590 These are special glyphs like truncation marks on terminal
10591 frames. */
10592 if (row->displays_text_p)
10593 while (glyph < end
10594 && INTEGERP (glyph->object)
10595 && glyph->charpos < 0)
10596 {
10597 x += glyph->pixel_width;
10598 ++glyph;
10599 }
10600
10601 string_start = NULL;
10602 while (glyph < end
10603 && !INTEGERP (glyph->object)
10604 && (!BUFFERP (glyph->object)
10605 || (last_pos = glyph->charpos) < pt_old))
10606 {
10607 if (! STRINGP (glyph->object))
10608 {
10609 string_start = NULL;
10610 x += glyph->pixel_width;
10611 ++glyph;
10612 }
10613 else
10614 {
10615 string_before_pos = last_pos;
10616 string_start = glyph;
10617 string_start_x = x;
10618 /* Skip all glyphs from string. */
10619 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10620 }
10621 }
10622
10623 if (string_start
10624 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10625 {
10626 /* We may have skipped over point because the previous glyphs
10627 are from string. As there's no easy way to know the
10628 character position of the current glyph, find the correct
10629 glyph on point by scanning from string_start again. */
10630 Lisp_Object limit;
10631 Lisp_Object string;
10632 int pos;
10633
10634 limit = make_number (pt_old + 1);
10635 end = glyph;
10636 glyph = string_start;
10637 x = string_start_x;
10638 string = glyph->object;
10639 pos = string_buffer_position (w, string, string_before_pos);
10640 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10641 because we always put cursor after overlay strings. */
10642 while (pos == 0 && glyph < end)
10643 {
10644 string = glyph->object;
10645 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10646 if (glyph < end)
10647 pos = string_buffer_position (w, glyph->object, string_before_pos);
10648 }
10649
10650 while (glyph < end)
10651 {
10652 pos = XINT (Fnext_single_char_property_change
10653 (make_number (pos), Qdisplay, Qnil, limit));
10654 if (pos > pt_old)
10655 break;
10656 /* Skip glyphs from the same string. */
10657 string = glyph->object;
10658 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10659 /* Skip glyphs from an overlay. */
10660 while (glyph < end
10661 && ! string_buffer_position (w, glyph->object, pos))
10662 {
10663 string = glyph->object;
10664 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10665 }
10666 }
10667 }
10668
10669 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10670 w->cursor.x = x;
10671 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10672 w->cursor.y = row->y + dy;
10673
10674 if (w == XWINDOW (selected_window))
10675 {
10676 if (!row->continued_p
10677 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10678 && row->x == 0)
10679 {
10680 this_line_buffer = XBUFFER (w->buffer);
10681
10682 CHARPOS (this_line_start_pos)
10683 = MATRIX_ROW_START_CHARPOS (row) + delta;
10684 BYTEPOS (this_line_start_pos)
10685 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10686
10687 CHARPOS (this_line_end_pos)
10688 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10689 BYTEPOS (this_line_end_pos)
10690 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10691
10692 this_line_y = w->cursor.y;
10693 this_line_pixel_height = row->height;
10694 this_line_vpos = w->cursor.vpos;
10695 this_line_start_x = row->x;
10696 }
10697 else
10698 CHARPOS (this_line_start_pos) = 0;
10699 }
10700 }
10701
10702
10703 /* Run window scroll functions, if any, for WINDOW with new window
10704 start STARTP. Sets the window start of WINDOW to that position.
10705
10706 We assume that the window's buffer is really current. */
10707
10708 static INLINE struct text_pos
10709 run_window_scroll_functions (window, startp)
10710 Lisp_Object window;
10711 struct text_pos startp;
10712 {
10713 struct window *w = XWINDOW (window);
10714 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10715
10716 if (current_buffer != XBUFFER (w->buffer))
10717 abort ();
10718
10719 if (!NILP (Vwindow_scroll_functions))
10720 {
10721 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10722 make_number (CHARPOS (startp)));
10723 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10724 /* In case the hook functions switch buffers. */
10725 if (current_buffer != XBUFFER (w->buffer))
10726 set_buffer_internal_1 (XBUFFER (w->buffer));
10727 }
10728
10729 return startp;
10730 }
10731
10732
10733 /* Make sure the line containing the cursor is fully visible.
10734 A value of 1 means there is nothing to be done.
10735 (Either the line is fully visible, or it cannot be made so,
10736 or we cannot tell.)
10737
10738 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10739 is higher than window.
10740
10741 A value of 0 means the caller should do scrolling
10742 as if point had gone off the screen. */
10743
10744 static int
10745 make_cursor_line_fully_visible (w, force_p)
10746 struct window *w;
10747 int force_p;
10748 {
10749 struct glyph_matrix *matrix;
10750 struct glyph_row *row;
10751 int window_height;
10752
10753 /* It's not always possible to find the cursor, e.g, when a window
10754 is full of overlay strings. Don't do anything in that case. */
10755 if (w->cursor.vpos < 0)
10756 return 1;
10757
10758 matrix = w->desired_matrix;
10759 row = MATRIX_ROW (matrix, w->cursor.vpos);
10760
10761 /* If the cursor row is not partially visible, there's nothing to do. */
10762 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10763 return 1;
10764
10765 if (force_p)
10766 return 0;
10767
10768 /* If the row the cursor is in is taller than the window's height,
10769 it's not clear what to do, so do nothing. */
10770 window_height = window_box_height (w);
10771 if (row->height >= window_height)
10772 return 1;
10773
10774 return 0;
10775
10776 #if 0
10777 /* This code used to try to scroll the window just enough to make
10778 the line visible. It returned 0 to say that the caller should
10779 allocate larger glyph matrices. */
10780
10781 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10782 {
10783 int dy = row->height - row->visible_height;
10784 w->vscroll = 0;
10785 w->cursor.y += dy;
10786 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10787 }
10788 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10789 {
10790 int dy = - (row->height - row->visible_height);
10791 w->vscroll = dy;
10792 w->cursor.y += dy;
10793 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10794 }
10795
10796 /* When we change the cursor y-position of the selected window,
10797 change this_line_y as well so that the display optimization for
10798 the cursor line of the selected window in redisplay_internal uses
10799 the correct y-position. */
10800 if (w == XWINDOW (selected_window))
10801 this_line_y = w->cursor.y;
10802
10803 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10804 redisplay with larger matrices. */
10805 if (matrix->nrows < required_matrix_height (w))
10806 {
10807 fonts_changed_p = 1;
10808 return 0;
10809 }
10810
10811 return 1;
10812 #endif /* 0 */
10813 }
10814
10815
10816 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10817 non-zero means only WINDOW is redisplayed in redisplay_internal.
10818 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10819 in redisplay_window to bring a partially visible line into view in
10820 the case that only the cursor has moved.
10821
10822 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10823 last screen line's vertical height extends past the end of the screen.
10824
10825 Value is
10826
10827 1 if scrolling succeeded
10828
10829 0 if scrolling didn't find point.
10830
10831 -1 if new fonts have been loaded so that we must interrupt
10832 redisplay, adjust glyph matrices, and try again. */
10833
10834 enum
10835 {
10836 SCROLLING_SUCCESS,
10837 SCROLLING_FAILED,
10838 SCROLLING_NEED_LARGER_MATRICES
10839 };
10840
10841 static int
10842 try_scrolling (window, just_this_one_p, scroll_conservatively,
10843 scroll_step, temp_scroll_step, last_line_misfit)
10844 Lisp_Object window;
10845 int just_this_one_p;
10846 EMACS_INT scroll_conservatively, scroll_step;
10847 int temp_scroll_step;
10848 int last_line_misfit;
10849 {
10850 struct window *w = XWINDOW (window);
10851 struct frame *f = XFRAME (w->frame);
10852 struct text_pos scroll_margin_pos;
10853 struct text_pos pos;
10854 struct text_pos startp;
10855 struct it it;
10856 Lisp_Object window_end;
10857 int this_scroll_margin;
10858 int dy = 0;
10859 int scroll_max;
10860 int rc;
10861 int amount_to_scroll = 0;
10862 Lisp_Object aggressive;
10863 int height;
10864 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10865
10866 #if GLYPH_DEBUG
10867 debug_method_add (w, "try_scrolling");
10868 #endif
10869
10870 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10871
10872 /* Compute scroll margin height in pixels. We scroll when point is
10873 within this distance from the top or bottom of the window. */
10874 if (scroll_margin > 0)
10875 {
10876 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10877 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10878 }
10879 else
10880 this_scroll_margin = 0;
10881
10882 /* Force scroll_conservatively to have a reasonable value so it doesn't
10883 cause an overflow while computing how much to scroll. */
10884 if (scroll_conservatively)
10885 scroll_conservatively = min (scroll_conservatively,
10886 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10887
10888 /* Compute how much we should try to scroll maximally to bring point
10889 into view. */
10890 if (scroll_step || scroll_conservatively || temp_scroll_step)
10891 scroll_max = max (scroll_step,
10892 max (scroll_conservatively, temp_scroll_step));
10893 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10894 || NUMBERP (current_buffer->scroll_up_aggressively))
10895 /* We're trying to scroll because of aggressive scrolling
10896 but no scroll_step is set. Choose an arbitrary one. Maybe
10897 there should be a variable for this. */
10898 scroll_max = 10;
10899 else
10900 scroll_max = 0;
10901 scroll_max *= FRAME_LINE_HEIGHT (f);
10902
10903 /* Decide whether we have to scroll down. Start at the window end
10904 and move this_scroll_margin up to find the position of the scroll
10905 margin. */
10906 window_end = Fwindow_end (window, Qt);
10907
10908 too_near_end:
10909
10910 CHARPOS (scroll_margin_pos) = XINT (window_end);
10911 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10912
10913 if (this_scroll_margin || extra_scroll_margin_lines)
10914 {
10915 start_display (&it, w, scroll_margin_pos);
10916 if (this_scroll_margin)
10917 move_it_vertically (&it, - this_scroll_margin);
10918 if (extra_scroll_margin_lines)
10919 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
10920 scroll_margin_pos = it.current.pos;
10921 }
10922
10923 if (PT >= CHARPOS (scroll_margin_pos))
10924 {
10925 int y0;
10926
10927 /* Point is in the scroll margin at the bottom of the window, or
10928 below. Compute a new window start that makes point visible. */
10929
10930 /* Compute the distance from the scroll margin to PT.
10931 Give up if the distance is greater than scroll_max. */
10932 start_display (&it, w, scroll_margin_pos);
10933 y0 = it.current_y;
10934 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10935 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10936
10937 /* To make point visible, we have to move the window start
10938 down so that the line the cursor is in is visible, which
10939 means we have to add in the height of the cursor line. */
10940 dy = line_bottom_y (&it) - y0;
10941
10942 if (dy > scroll_max)
10943 return SCROLLING_FAILED;
10944
10945 /* Move the window start down. If scrolling conservatively,
10946 move it just enough down to make point visible. If
10947 scroll_step is set, move it down by scroll_step. */
10948 start_display (&it, w, startp);
10949
10950 if (scroll_conservatively)
10951 /* Set AMOUNT_TO_SCROLL to at least one line,
10952 and at most scroll_conservatively lines. */
10953 amount_to_scroll
10954 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10955 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10956 else if (scroll_step || temp_scroll_step)
10957 amount_to_scroll = scroll_max;
10958 else
10959 {
10960 aggressive = current_buffer->scroll_up_aggressively;
10961 height = WINDOW_BOX_TEXT_HEIGHT (w);
10962 if (NUMBERP (aggressive))
10963 amount_to_scroll = XFLOATINT (aggressive) * height;
10964 }
10965
10966 if (amount_to_scroll <= 0)
10967 return SCROLLING_FAILED;
10968
10969 /* If moving by amount_to_scroll leaves STARTP unchanged,
10970 move it down one screen line. */
10971
10972 move_it_vertically (&it, amount_to_scroll);
10973 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10974 move_it_by_lines (&it, 1, 1);
10975 startp = it.current.pos;
10976 }
10977 else
10978 {
10979 /* See if point is inside the scroll margin at the top of the
10980 window. */
10981 scroll_margin_pos = startp;
10982 if (this_scroll_margin)
10983 {
10984 start_display (&it, w, startp);
10985 move_it_vertically (&it, this_scroll_margin);
10986 scroll_margin_pos = it.current.pos;
10987 }
10988
10989 if (PT < CHARPOS (scroll_margin_pos))
10990 {
10991 /* Point is in the scroll margin at the top of the window or
10992 above what is displayed in the window. */
10993 int y0;
10994
10995 /* Compute the vertical distance from PT to the scroll
10996 margin position. Give up if distance is greater than
10997 scroll_max. */
10998 SET_TEXT_POS (pos, PT, PT_BYTE);
10999 start_display (&it, w, pos);
11000 y0 = it.current_y;
11001 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11002 it.last_visible_y, -1,
11003 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11004 dy = it.current_y - y0;
11005 if (dy > scroll_max)
11006 return SCROLLING_FAILED;
11007
11008 /* Compute new window start. */
11009 start_display (&it, w, startp);
11010
11011 if (scroll_conservatively)
11012 amount_to_scroll =
11013 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11014 else if (scroll_step || temp_scroll_step)
11015 amount_to_scroll = scroll_max;
11016 else
11017 {
11018 aggressive = current_buffer->scroll_down_aggressively;
11019 height = WINDOW_BOX_TEXT_HEIGHT (w);
11020 if (NUMBERP (aggressive))
11021 amount_to_scroll = XFLOATINT (aggressive) * height;
11022 }
11023
11024 if (amount_to_scroll <= 0)
11025 return SCROLLING_FAILED;
11026
11027 move_it_vertically (&it, - amount_to_scroll);
11028 startp = it.current.pos;
11029 }
11030 }
11031
11032 /* Run window scroll functions. */
11033 startp = run_window_scroll_functions (window, startp);
11034
11035 /* Display the window. Give up if new fonts are loaded, or if point
11036 doesn't appear. */
11037 if (!try_window (window, startp))
11038 rc = SCROLLING_NEED_LARGER_MATRICES;
11039 else if (w->cursor.vpos < 0)
11040 {
11041 clear_glyph_matrix (w->desired_matrix);
11042 rc = SCROLLING_FAILED;
11043 }
11044 else
11045 {
11046 /* Maybe forget recorded base line for line number display. */
11047 if (!just_this_one_p
11048 || current_buffer->clip_changed
11049 || BEG_UNCHANGED < CHARPOS (startp))
11050 w->base_line_number = Qnil;
11051
11052 /* If cursor ends up on a partially visible line,
11053 treat that as being off the bottom of the screen. */
11054 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11055 {
11056 clear_glyph_matrix (w->desired_matrix);
11057 ++extra_scroll_margin_lines;
11058 goto too_near_end;
11059 }
11060 rc = SCROLLING_SUCCESS;
11061 }
11062
11063 return rc;
11064 }
11065
11066
11067 /* Compute a suitable window start for window W if display of W starts
11068 on a continuation line. Value is non-zero if a new window start
11069 was computed.
11070
11071 The new window start will be computed, based on W's width, starting
11072 from the start of the continued line. It is the start of the
11073 screen line with the minimum distance from the old start W->start. */
11074
11075 static int
11076 compute_window_start_on_continuation_line (w)
11077 struct window *w;
11078 {
11079 struct text_pos pos, start_pos;
11080 int window_start_changed_p = 0;
11081
11082 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11083
11084 /* If window start is on a continuation line... Window start may be
11085 < BEGV in case there's invisible text at the start of the
11086 buffer (M-x rmail, for example). */
11087 if (CHARPOS (start_pos) > BEGV
11088 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11089 {
11090 struct it it;
11091 struct glyph_row *row;
11092
11093 /* Handle the case that the window start is out of range. */
11094 if (CHARPOS (start_pos) < BEGV)
11095 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11096 else if (CHARPOS (start_pos) > ZV)
11097 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11098
11099 /* Find the start of the continued line. This should be fast
11100 because scan_buffer is fast (newline cache). */
11101 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11102 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11103 row, DEFAULT_FACE_ID);
11104 reseat_at_previous_visible_line_start (&it);
11105
11106 /* If the line start is "too far" away from the window start,
11107 say it takes too much time to compute a new window start. */
11108 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11109 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11110 {
11111 int min_distance, distance;
11112
11113 /* Move forward by display lines to find the new window
11114 start. If window width was enlarged, the new start can
11115 be expected to be > the old start. If window width was
11116 decreased, the new window start will be < the old start.
11117 So, we're looking for the display line start with the
11118 minimum distance from the old window start. */
11119 pos = it.current.pos;
11120 min_distance = INFINITY;
11121 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11122 distance < min_distance)
11123 {
11124 min_distance = distance;
11125 pos = it.current.pos;
11126 move_it_by_lines (&it, 1, 0);
11127 }
11128
11129 /* Set the window start there. */
11130 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11131 window_start_changed_p = 1;
11132 }
11133 }
11134
11135 return window_start_changed_p;
11136 }
11137
11138
11139 /* Try cursor movement in case text has not changed in window WINDOW,
11140 with window start STARTP. Value is
11141
11142 CURSOR_MOVEMENT_SUCCESS if successful
11143
11144 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11145
11146 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11147 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11148 we want to scroll as if scroll-step were set to 1. See the code.
11149
11150 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11151 which case we have to abort this redisplay, and adjust matrices
11152 first. */
11153
11154 enum
11155 {
11156 CURSOR_MOVEMENT_SUCCESS,
11157 CURSOR_MOVEMENT_CANNOT_BE_USED,
11158 CURSOR_MOVEMENT_MUST_SCROLL,
11159 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11160 };
11161
11162 static int
11163 try_cursor_movement (window, startp, scroll_step)
11164 Lisp_Object window;
11165 struct text_pos startp;
11166 int *scroll_step;
11167 {
11168 struct window *w = XWINDOW (window);
11169 struct frame *f = XFRAME (w->frame);
11170 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11171
11172 #if GLYPH_DEBUG
11173 if (inhibit_try_cursor_movement)
11174 return rc;
11175 #endif
11176
11177 /* Handle case where text has not changed, only point, and it has
11178 not moved off the frame. */
11179 if (/* Point may be in this window. */
11180 PT >= CHARPOS (startp)
11181 /* Selective display hasn't changed. */
11182 && !current_buffer->clip_changed
11183 /* Function force-mode-line-update is used to force a thorough
11184 redisplay. It sets either windows_or_buffers_changed or
11185 update_mode_lines. So don't take a shortcut here for these
11186 cases. */
11187 && !update_mode_lines
11188 && !windows_or_buffers_changed
11189 && !cursor_type_changed
11190 /* Can't use this case if highlighting a region. When a
11191 region exists, cursor movement has to do more than just
11192 set the cursor. */
11193 && !(!NILP (Vtransient_mark_mode)
11194 && !NILP (current_buffer->mark_active))
11195 && NILP (w->region_showing)
11196 && NILP (Vshow_trailing_whitespace)
11197 /* Right after splitting windows, last_point may be nil. */
11198 && INTEGERP (w->last_point)
11199 /* This code is not used for mini-buffer for the sake of the case
11200 of redisplaying to replace an echo area message; since in
11201 that case the mini-buffer contents per se are usually
11202 unchanged. This code is of no real use in the mini-buffer
11203 since the handling of this_line_start_pos, etc., in redisplay
11204 handles the same cases. */
11205 && !EQ (window, minibuf_window)
11206 /* When splitting windows or for new windows, it happens that
11207 redisplay is called with a nil window_end_vpos or one being
11208 larger than the window. This should really be fixed in
11209 window.c. I don't have this on my list, now, so we do
11210 approximately the same as the old redisplay code. --gerd. */
11211 && INTEGERP (w->window_end_vpos)
11212 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11213 && (FRAME_WINDOW_P (f)
11214 || !overlay_arrow_in_current_buffer_p ()))
11215 {
11216 int this_scroll_margin;
11217 struct glyph_row *row = NULL;
11218
11219 #if GLYPH_DEBUG
11220 debug_method_add (w, "cursor movement");
11221 #endif
11222
11223 /* Scroll if point within this distance from the top or bottom
11224 of the window. This is a pixel value. */
11225 this_scroll_margin = max (0, scroll_margin);
11226 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11227 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11228
11229 /* Start with the row the cursor was displayed during the last
11230 not paused redisplay. Give up if that row is not valid. */
11231 if (w->last_cursor.vpos < 0
11232 || w->last_cursor.vpos >= w->current_matrix->nrows)
11233 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11234 else
11235 {
11236 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11237 if (row->mode_line_p)
11238 ++row;
11239 if (!row->enabled_p)
11240 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11241 }
11242
11243 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11244 {
11245 int scroll_p = 0;
11246 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11247
11248 if (PT > XFASTINT (w->last_point))
11249 {
11250 /* Point has moved forward. */
11251 while (MATRIX_ROW_END_CHARPOS (row) < PT
11252 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11253 {
11254 xassert (row->enabled_p);
11255 ++row;
11256 }
11257
11258 /* The end position of a row equals the start position
11259 of the next row. If PT is there, we would rather
11260 display it in the next line. */
11261 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11262 && MATRIX_ROW_END_CHARPOS (row) == PT
11263 && !cursor_row_p (w, row))
11264 ++row;
11265
11266 /* If within the scroll margin, scroll. Note that
11267 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11268 the next line would be drawn, and that
11269 this_scroll_margin can be zero. */
11270 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11271 || PT > MATRIX_ROW_END_CHARPOS (row)
11272 /* Line is completely visible last line in window
11273 and PT is to be set in the next line. */
11274 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11275 && PT == MATRIX_ROW_END_CHARPOS (row)
11276 && !row->ends_at_zv_p
11277 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11278 scroll_p = 1;
11279 }
11280 else if (PT < XFASTINT (w->last_point))
11281 {
11282 /* Cursor has to be moved backward. Note that PT >=
11283 CHARPOS (startp) because of the outer
11284 if-statement. */
11285 while (!row->mode_line_p
11286 && (MATRIX_ROW_START_CHARPOS (row) > PT
11287 || (MATRIX_ROW_START_CHARPOS (row) == PT
11288 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11289 && (row->y > this_scroll_margin
11290 || CHARPOS (startp) == BEGV))
11291 {
11292 xassert (row->enabled_p);
11293 --row;
11294 }
11295
11296 /* Consider the following case: Window starts at BEGV,
11297 there is invisible, intangible text at BEGV, so that
11298 display starts at some point START > BEGV. It can
11299 happen that we are called with PT somewhere between
11300 BEGV and START. Try to handle that case. */
11301 if (row < w->current_matrix->rows
11302 || row->mode_line_p)
11303 {
11304 row = w->current_matrix->rows;
11305 if (row->mode_line_p)
11306 ++row;
11307 }
11308
11309 /* Due to newlines in overlay strings, we may have to
11310 skip forward over overlay strings. */
11311 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11312 && MATRIX_ROW_END_CHARPOS (row) == PT
11313 && !cursor_row_p (w, row))
11314 ++row;
11315
11316 /* If within the scroll margin, scroll. */
11317 if (row->y < this_scroll_margin
11318 && CHARPOS (startp) != BEGV)
11319 scroll_p = 1;
11320 }
11321
11322 if (PT < MATRIX_ROW_START_CHARPOS (row)
11323 || PT > MATRIX_ROW_END_CHARPOS (row))
11324 {
11325 /* if PT is not in the glyph row, give up. */
11326 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11327 }
11328 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11329 {
11330 if (PT == MATRIX_ROW_END_CHARPOS (row)
11331 && !row->ends_at_zv_p
11332 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11333 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11334 else if (row->height > window_box_height (w))
11335 {
11336 /* If we end up in a partially visible line, let's
11337 make it fully visible, except when it's taller
11338 than the window, in which case we can't do much
11339 about it. */
11340 *scroll_step = 1;
11341 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11342 }
11343 else
11344 {
11345 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11346 if (!make_cursor_line_fully_visible (w, 0))
11347 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11348 else
11349 rc = CURSOR_MOVEMENT_SUCCESS;
11350 }
11351 }
11352 else if (scroll_p)
11353 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11354 else
11355 {
11356 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11357 rc = CURSOR_MOVEMENT_SUCCESS;
11358 }
11359 }
11360 }
11361
11362 return rc;
11363 }
11364
11365 void
11366 set_vertical_scroll_bar (w)
11367 struct window *w;
11368 {
11369 int start, end, whole;
11370
11371 /* Calculate the start and end positions for the current window.
11372 At some point, it would be nice to choose between scrollbars
11373 which reflect the whole buffer size, with special markers
11374 indicating narrowing, and scrollbars which reflect only the
11375 visible region.
11376
11377 Note that mini-buffers sometimes aren't displaying any text. */
11378 if (!MINI_WINDOW_P (w)
11379 || (w == XWINDOW (minibuf_window)
11380 && NILP (echo_area_buffer[0])))
11381 {
11382 struct buffer *buf = XBUFFER (w->buffer);
11383 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11384 start = marker_position (w->start) - BUF_BEGV (buf);
11385 /* I don't think this is guaranteed to be right. For the
11386 moment, we'll pretend it is. */
11387 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11388
11389 if (end < start)
11390 end = start;
11391 if (whole < (end - start))
11392 whole = end - start;
11393 }
11394 else
11395 start = end = whole = 0;
11396
11397 /* Indicate what this scroll bar ought to be displaying now. */
11398 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11399 }
11400
11401
11402 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11403 selected_window is redisplayed.
11404
11405 We can return without actually redisplaying the window if
11406 fonts_changed_p is nonzero. In that case, redisplay_internal will
11407 retry. */
11408
11409 static void
11410 redisplay_window (window, just_this_one_p)
11411 Lisp_Object window;
11412 int just_this_one_p;
11413 {
11414 struct window *w = XWINDOW (window);
11415 struct frame *f = XFRAME (w->frame);
11416 struct buffer *buffer = XBUFFER (w->buffer);
11417 struct buffer *old = current_buffer;
11418 struct text_pos lpoint, opoint, startp;
11419 int update_mode_line;
11420 int tem;
11421 struct it it;
11422 /* Record it now because it's overwritten. */
11423 int current_matrix_up_to_date_p = 0;
11424 int used_current_matrix_p = 0;
11425 /* This is less strict than current_matrix_up_to_date_p.
11426 It indictes that the buffer contents and narrowing are unchanged. */
11427 int buffer_unchanged_p = 0;
11428 int temp_scroll_step = 0;
11429 int count = SPECPDL_INDEX ();
11430 int rc;
11431 int centering_position;
11432 int last_line_misfit = 0;
11433
11434 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11435 opoint = lpoint;
11436
11437 /* W must be a leaf window here. */
11438 xassert (!NILP (w->buffer));
11439 #if GLYPH_DEBUG
11440 *w->desired_matrix->method = 0;
11441 #endif
11442
11443 specbind (Qinhibit_point_motion_hooks, Qt);
11444
11445 reconsider_clip_changes (w, buffer);
11446
11447 /* Has the mode line to be updated? */
11448 update_mode_line = (!NILP (w->update_mode_line)
11449 || update_mode_lines
11450 || buffer->clip_changed
11451 || buffer->prevent_redisplay_optimizations_p);
11452
11453 if (MINI_WINDOW_P (w))
11454 {
11455 if (w == XWINDOW (echo_area_window)
11456 && !NILP (echo_area_buffer[0]))
11457 {
11458 if (update_mode_line)
11459 /* We may have to update a tty frame's menu bar or a
11460 tool-bar. Example `M-x C-h C-h C-g'. */
11461 goto finish_menu_bars;
11462 else
11463 /* We've already displayed the echo area glyphs in this window. */
11464 goto finish_scroll_bars;
11465 }
11466 else if ((w != XWINDOW (minibuf_window)
11467 || minibuf_level == 0)
11468 /* When buffer is nonempty, redisplay window normally. */
11469 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11470 /* Quail displays non-mini buffers in minibuffer window.
11471 In that case, redisplay the window normally. */
11472 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11473 {
11474 /* W is a mini-buffer window, but it's not active, so clear
11475 it. */
11476 int yb = window_text_bottom_y (w);
11477 struct glyph_row *row;
11478 int y;
11479
11480 for (y = 0, row = w->desired_matrix->rows;
11481 y < yb;
11482 y += row->height, ++row)
11483 blank_row (w, row, y);
11484 goto finish_scroll_bars;
11485 }
11486
11487 clear_glyph_matrix (w->desired_matrix);
11488 }
11489
11490 /* Otherwise set up data on this window; select its buffer and point
11491 value. */
11492 /* Really select the buffer, for the sake of buffer-local
11493 variables. */
11494 set_buffer_internal_1 (XBUFFER (w->buffer));
11495 SET_TEXT_POS (opoint, PT, PT_BYTE);
11496
11497 current_matrix_up_to_date_p
11498 = (!NILP (w->window_end_valid)
11499 && !current_buffer->clip_changed
11500 && !current_buffer->prevent_redisplay_optimizations_p
11501 && XFASTINT (w->last_modified) >= MODIFF
11502 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11503
11504 buffer_unchanged_p
11505 = (!NILP (w->window_end_valid)
11506 && !current_buffer->clip_changed
11507 && XFASTINT (w->last_modified) >= MODIFF
11508 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11509
11510 /* When windows_or_buffers_changed is non-zero, we can't rely on
11511 the window end being valid, so set it to nil there. */
11512 if (windows_or_buffers_changed)
11513 {
11514 /* If window starts on a continuation line, maybe adjust the
11515 window start in case the window's width changed. */
11516 if (XMARKER (w->start)->buffer == current_buffer)
11517 compute_window_start_on_continuation_line (w);
11518
11519 w->window_end_valid = Qnil;
11520 }
11521
11522 /* Some sanity checks. */
11523 CHECK_WINDOW_END (w);
11524 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11525 abort ();
11526 if (BYTEPOS (opoint) < CHARPOS (opoint))
11527 abort ();
11528
11529 /* If %c is in mode line, update it if needed. */
11530 if (!NILP (w->column_number_displayed)
11531 /* This alternative quickly identifies a common case
11532 where no change is needed. */
11533 && !(PT == XFASTINT (w->last_point)
11534 && XFASTINT (w->last_modified) >= MODIFF
11535 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11536 && (XFASTINT (w->column_number_displayed)
11537 != (int) current_column ())) /* iftc */
11538 update_mode_line = 1;
11539
11540 /* Count number of windows showing the selected buffer. An indirect
11541 buffer counts as its base buffer. */
11542 if (!just_this_one_p)
11543 {
11544 struct buffer *current_base, *window_base;
11545 current_base = current_buffer;
11546 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11547 if (current_base->base_buffer)
11548 current_base = current_base->base_buffer;
11549 if (window_base->base_buffer)
11550 window_base = window_base->base_buffer;
11551 if (current_base == window_base)
11552 buffer_shared++;
11553 }
11554
11555 /* Point refers normally to the selected window. For any other
11556 window, set up appropriate value. */
11557 if (!EQ (window, selected_window))
11558 {
11559 int new_pt = XMARKER (w->pointm)->charpos;
11560 int new_pt_byte = marker_byte_position (w->pointm);
11561 if (new_pt < BEGV)
11562 {
11563 new_pt = BEGV;
11564 new_pt_byte = BEGV_BYTE;
11565 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11566 }
11567 else if (new_pt > (ZV - 1))
11568 {
11569 new_pt = ZV;
11570 new_pt_byte = ZV_BYTE;
11571 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11572 }
11573
11574 /* We don't use SET_PT so that the point-motion hooks don't run. */
11575 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11576 }
11577
11578 /* If any of the character widths specified in the display table
11579 have changed, invalidate the width run cache. It's true that
11580 this may be a bit late to catch such changes, but the rest of
11581 redisplay goes (non-fatally) haywire when the display table is
11582 changed, so why should we worry about doing any better? */
11583 if (current_buffer->width_run_cache)
11584 {
11585 struct Lisp_Char_Table *disptab = buffer_display_table ();
11586
11587 if (! disptab_matches_widthtab (disptab,
11588 XVECTOR (current_buffer->width_table)))
11589 {
11590 invalidate_region_cache (current_buffer,
11591 current_buffer->width_run_cache,
11592 BEG, Z);
11593 recompute_width_table (current_buffer, disptab);
11594 }
11595 }
11596
11597 /* If window-start is screwed up, choose a new one. */
11598 if (XMARKER (w->start)->buffer != current_buffer)
11599 goto recenter;
11600
11601 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11602
11603 /* If someone specified a new starting point but did not insist,
11604 check whether it can be used. */
11605 if (!NILP (w->optional_new_start)
11606 && CHARPOS (startp) >= BEGV
11607 && CHARPOS (startp) <= ZV)
11608 {
11609 w->optional_new_start = Qnil;
11610 start_display (&it, w, startp);
11611 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11612 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11613 if (IT_CHARPOS (it) == PT)
11614 w->force_start = Qt;
11615 /* IT may overshoot PT if text at PT is invisible. */
11616 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11617 w->force_start = Qt;
11618
11619
11620 }
11621
11622 /* Handle case where place to start displaying has been specified,
11623 unless the specified location is outside the accessible range. */
11624 if (!NILP (w->force_start)
11625 || w->frozen_window_start_p)
11626 {
11627 /* We set this later on if we have to adjust point. */
11628 int new_vpos = -1;
11629
11630 w->force_start = Qnil;
11631 w->vscroll = 0;
11632 w->window_end_valid = Qnil;
11633
11634 /* Forget any recorded base line for line number display. */
11635 if (!buffer_unchanged_p)
11636 w->base_line_number = Qnil;
11637
11638 /* Redisplay the mode line. Select the buffer properly for that.
11639 Also, run the hook window-scroll-functions
11640 because we have scrolled. */
11641 /* Note, we do this after clearing force_start because
11642 if there's an error, it is better to forget about force_start
11643 than to get into an infinite loop calling the hook functions
11644 and having them get more errors. */
11645 if (!update_mode_line
11646 || ! NILP (Vwindow_scroll_functions))
11647 {
11648 update_mode_line = 1;
11649 w->update_mode_line = Qt;
11650 startp = run_window_scroll_functions (window, startp);
11651 }
11652
11653 w->last_modified = make_number (0);
11654 w->last_overlay_modified = make_number (0);
11655 if (CHARPOS (startp) < BEGV)
11656 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11657 else if (CHARPOS (startp) > ZV)
11658 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11659
11660 /* Redisplay, then check if cursor has been set during the
11661 redisplay. Give up if new fonts were loaded. */
11662 if (!try_window (window, startp))
11663 {
11664 w->force_start = Qt;
11665 clear_glyph_matrix (w->desired_matrix);
11666 goto need_larger_matrices;
11667 }
11668
11669 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11670 {
11671 /* If point does not appear, try to move point so it does
11672 appear. The desired matrix has been built above, so we
11673 can use it here. */
11674 new_vpos = window_box_height (w) / 2;
11675 }
11676
11677 if (!make_cursor_line_fully_visible (w, 0))
11678 {
11679 /* Point does appear, but on a line partly visible at end of window.
11680 Move it back to a fully-visible line. */
11681 new_vpos = window_box_height (w);
11682 }
11683
11684 /* If we need to move point for either of the above reasons,
11685 now actually do it. */
11686 if (new_vpos >= 0)
11687 {
11688 struct glyph_row *row;
11689
11690 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11691 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11692 ++row;
11693
11694 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11695 MATRIX_ROW_START_BYTEPOS (row));
11696
11697 if (w != XWINDOW (selected_window))
11698 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11699 else if (current_buffer == old)
11700 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11701
11702 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11703
11704 /* If we are highlighting the region, then we just changed
11705 the region, so redisplay to show it. */
11706 if (!NILP (Vtransient_mark_mode)
11707 && !NILP (current_buffer->mark_active))
11708 {
11709 clear_glyph_matrix (w->desired_matrix);
11710 if (!try_window (window, startp))
11711 goto need_larger_matrices;
11712 }
11713 }
11714
11715 #if GLYPH_DEBUG
11716 debug_method_add (w, "forced window start");
11717 #endif
11718 goto done;
11719 }
11720
11721 /* Handle case where text has not changed, only point, and it has
11722 not moved off the frame, and we are not retrying after hscroll.
11723 (current_matrix_up_to_date_p is nonzero when retrying.) */
11724 if (current_matrix_up_to_date_p
11725 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11726 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11727 {
11728 switch (rc)
11729 {
11730 case CURSOR_MOVEMENT_SUCCESS:
11731 used_current_matrix_p = 1;
11732 goto done;
11733
11734 #if 0 /* try_cursor_movement never returns this value. */
11735 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11736 goto need_larger_matrices;
11737 #endif
11738
11739 case CURSOR_MOVEMENT_MUST_SCROLL:
11740 goto try_to_scroll;
11741
11742 default:
11743 abort ();
11744 }
11745 }
11746 /* If current starting point was originally the beginning of a line
11747 but no longer is, find a new starting point. */
11748 else if (!NILP (w->start_at_line_beg)
11749 && !(CHARPOS (startp) <= BEGV
11750 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11751 {
11752 #if GLYPH_DEBUG
11753 debug_method_add (w, "recenter 1");
11754 #endif
11755 goto recenter;
11756 }
11757
11758 /* Try scrolling with try_window_id. Value is > 0 if update has
11759 been done, it is -1 if we know that the same window start will
11760 not work. It is 0 if unsuccessful for some other reason. */
11761 else if ((tem = try_window_id (w)) != 0)
11762 {
11763 #if GLYPH_DEBUG
11764 debug_method_add (w, "try_window_id %d", tem);
11765 #endif
11766
11767 if (fonts_changed_p)
11768 goto need_larger_matrices;
11769 if (tem > 0)
11770 goto done;
11771
11772 /* Otherwise try_window_id has returned -1 which means that we
11773 don't want the alternative below this comment to execute. */
11774 }
11775 else if (CHARPOS (startp) >= BEGV
11776 && CHARPOS (startp) <= ZV
11777 && PT >= CHARPOS (startp)
11778 && (CHARPOS (startp) < ZV
11779 /* Avoid starting at end of buffer. */
11780 || CHARPOS (startp) == BEGV
11781 || (XFASTINT (w->last_modified) >= MODIFF
11782 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11783 {
11784 #if GLYPH_DEBUG
11785 debug_method_add (w, "same window start");
11786 #endif
11787
11788 /* Try to redisplay starting at same place as before.
11789 If point has not moved off frame, accept the results. */
11790 if (!current_matrix_up_to_date_p
11791 /* Don't use try_window_reusing_current_matrix in this case
11792 because a window scroll function can have changed the
11793 buffer. */
11794 || !NILP (Vwindow_scroll_functions)
11795 || MINI_WINDOW_P (w)
11796 || !(used_current_matrix_p =
11797 try_window_reusing_current_matrix (w)))
11798 {
11799 IF_DEBUG (debug_method_add (w, "1"));
11800 try_window (window, startp);
11801 }
11802
11803 if (fonts_changed_p)
11804 goto need_larger_matrices;
11805
11806 if (w->cursor.vpos >= 0)
11807 {
11808 if (!just_this_one_p
11809 || current_buffer->clip_changed
11810 || BEG_UNCHANGED < CHARPOS (startp))
11811 /* Forget any recorded base line for line number display. */
11812 w->base_line_number = Qnil;
11813
11814 if (!make_cursor_line_fully_visible (w, 1))
11815 {
11816 clear_glyph_matrix (w->desired_matrix);
11817 last_line_misfit = 1;
11818 }
11819 /* Drop through and scroll. */
11820 else
11821 goto done;
11822 }
11823 else
11824 clear_glyph_matrix (w->desired_matrix);
11825 }
11826
11827 try_to_scroll:
11828
11829 w->last_modified = make_number (0);
11830 w->last_overlay_modified = make_number (0);
11831
11832 /* Redisplay the mode line. Select the buffer properly for that. */
11833 if (!update_mode_line)
11834 {
11835 update_mode_line = 1;
11836 w->update_mode_line = Qt;
11837 }
11838
11839 /* Try to scroll by specified few lines. */
11840 if ((scroll_conservatively
11841 || scroll_step
11842 || temp_scroll_step
11843 || NUMBERP (current_buffer->scroll_up_aggressively)
11844 || NUMBERP (current_buffer->scroll_down_aggressively))
11845 && !current_buffer->clip_changed
11846 && CHARPOS (startp) >= BEGV
11847 && CHARPOS (startp) <= ZV)
11848 {
11849 /* The function returns -1 if new fonts were loaded, 1 if
11850 successful, 0 if not successful. */
11851 int rc = try_scrolling (window, just_this_one_p,
11852 scroll_conservatively,
11853 scroll_step,
11854 temp_scroll_step, last_line_misfit);
11855 switch (rc)
11856 {
11857 case SCROLLING_SUCCESS:
11858 goto done;
11859
11860 case SCROLLING_NEED_LARGER_MATRICES:
11861 goto need_larger_matrices;
11862
11863 case SCROLLING_FAILED:
11864 break;
11865
11866 default:
11867 abort ();
11868 }
11869 }
11870
11871 /* Finally, just choose place to start which centers point */
11872
11873 recenter:
11874 centering_position = window_box_height (w) / 2;
11875
11876 point_at_top:
11877 /* Jump here with centering_position already set to 0. */
11878
11879 #if GLYPH_DEBUG
11880 debug_method_add (w, "recenter");
11881 #endif
11882
11883 /* w->vscroll = 0; */
11884
11885 /* Forget any previously recorded base line for line number display. */
11886 if (!buffer_unchanged_p)
11887 w->base_line_number = Qnil;
11888
11889 /* Move backward half the height of the window. */
11890 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11891 it.current_y = it.last_visible_y;
11892 move_it_vertically_backward (&it, centering_position);
11893 xassert (IT_CHARPOS (it) >= BEGV);
11894
11895 /* The function move_it_vertically_backward may move over more
11896 than the specified y-distance. If it->w is small, e.g. a
11897 mini-buffer window, we may end up in front of the window's
11898 display area. Start displaying at the start of the line
11899 containing PT in this case. */
11900 if (it.current_y <= 0)
11901 {
11902 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11903 move_it_vertically (&it, 0);
11904 xassert (IT_CHARPOS (it) <= PT);
11905 it.current_y = 0;
11906 }
11907
11908 it.current_x = it.hpos = 0;
11909
11910 /* Set startp here explicitly in case that helps avoid an infinite loop
11911 in case the window-scroll-functions functions get errors. */
11912 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11913
11914 /* Run scroll hooks. */
11915 startp = run_window_scroll_functions (window, it.current.pos);
11916
11917 /* Redisplay the window. */
11918 if (!current_matrix_up_to_date_p
11919 || windows_or_buffers_changed
11920 || cursor_type_changed
11921 /* Don't use try_window_reusing_current_matrix in this case
11922 because it can have changed the buffer. */
11923 || !NILP (Vwindow_scroll_functions)
11924 || !just_this_one_p
11925 || MINI_WINDOW_P (w)
11926 || !(used_current_matrix_p =
11927 try_window_reusing_current_matrix (w)))
11928 try_window (window, startp);
11929
11930 /* If new fonts have been loaded (due to fontsets), give up. We
11931 have to start a new redisplay since we need to re-adjust glyph
11932 matrices. */
11933 if (fonts_changed_p)
11934 goto need_larger_matrices;
11935
11936 /* If cursor did not appear assume that the middle of the window is
11937 in the first line of the window. Do it again with the next line.
11938 (Imagine a window of height 100, displaying two lines of height
11939 60. Moving back 50 from it->last_visible_y will end in the first
11940 line.) */
11941 if (w->cursor.vpos < 0)
11942 {
11943 if (!NILP (w->window_end_valid)
11944 && PT >= Z - XFASTINT (w->window_end_pos))
11945 {
11946 clear_glyph_matrix (w->desired_matrix);
11947 move_it_by_lines (&it, 1, 0);
11948 try_window (window, it.current.pos);
11949 }
11950 else if (PT < IT_CHARPOS (it))
11951 {
11952 clear_glyph_matrix (w->desired_matrix);
11953 move_it_by_lines (&it, -1, 0);
11954 try_window (window, it.current.pos);
11955 }
11956 else
11957 {
11958 /* Not much we can do about it. */
11959 }
11960 }
11961
11962 /* Consider the following case: Window starts at BEGV, there is
11963 invisible, intangible text at BEGV, so that display starts at
11964 some point START > BEGV. It can happen that we are called with
11965 PT somewhere between BEGV and START. Try to handle that case. */
11966 if (w->cursor.vpos < 0)
11967 {
11968 struct glyph_row *row = w->current_matrix->rows;
11969 if (row->mode_line_p)
11970 ++row;
11971 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11972 }
11973
11974 if (!make_cursor_line_fully_visible (w, centering_position > 0))
11975 {
11976 /* If vscroll is enabled, disable it and try again. */
11977 if (w->vscroll)
11978 {
11979 w->vscroll = 0;
11980 clear_glyph_matrix (w->desired_matrix);
11981 goto recenter;
11982 }
11983
11984 /* If centering point failed to make the whole line visible,
11985 put point at the top instead. That has to make the whole line
11986 visible, if it can be done. */
11987 clear_glyph_matrix (w->desired_matrix);
11988 centering_position = 0;
11989 goto point_at_top;
11990 }
11991
11992 done:
11993
11994 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11995 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11996 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11997 ? Qt : Qnil);
11998
11999 /* Display the mode line, if we must. */
12000 if ((update_mode_line
12001 /* If window not full width, must redo its mode line
12002 if (a) the window to its side is being redone and
12003 (b) we do a frame-based redisplay. This is a consequence
12004 of how inverted lines are drawn in frame-based redisplay. */
12005 || (!just_this_one_p
12006 && !FRAME_WINDOW_P (f)
12007 && !WINDOW_FULL_WIDTH_P (w))
12008 /* Line number to display. */
12009 || INTEGERP (w->base_line_pos)
12010 /* Column number is displayed and different from the one displayed. */
12011 || (!NILP (w->column_number_displayed)
12012 && (XFASTINT (w->column_number_displayed)
12013 != (int) current_column ()))) /* iftc */
12014 /* This means that the window has a mode line. */
12015 && (WINDOW_WANTS_MODELINE_P (w)
12016 || WINDOW_WANTS_HEADER_LINE_P (w)))
12017 {
12018 display_mode_lines (w);
12019
12020 /* If mode line height has changed, arrange for a thorough
12021 immediate redisplay using the correct mode line height. */
12022 if (WINDOW_WANTS_MODELINE_P (w)
12023 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12024 {
12025 fonts_changed_p = 1;
12026 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12027 = DESIRED_MODE_LINE_HEIGHT (w);
12028 }
12029
12030 /* If top line height has changed, arrange for a thorough
12031 immediate redisplay using the correct mode line height. */
12032 if (WINDOW_WANTS_HEADER_LINE_P (w)
12033 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12034 {
12035 fonts_changed_p = 1;
12036 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12037 = DESIRED_HEADER_LINE_HEIGHT (w);
12038 }
12039
12040 if (fonts_changed_p)
12041 goto need_larger_matrices;
12042 }
12043
12044 if (!line_number_displayed
12045 && !BUFFERP (w->base_line_pos))
12046 {
12047 w->base_line_pos = Qnil;
12048 w->base_line_number = Qnil;
12049 }
12050
12051 finish_menu_bars:
12052
12053 /* When we reach a frame's selected window, redo the frame's menu bar. */
12054 if (update_mode_line
12055 && EQ (FRAME_SELECTED_WINDOW (f), window))
12056 {
12057 int redisplay_menu_p = 0;
12058 int redisplay_tool_bar_p = 0;
12059
12060 if (FRAME_WINDOW_P (f))
12061 {
12062 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12063 || defined (USE_GTK)
12064 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12065 #else
12066 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12067 #endif
12068 }
12069 else
12070 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12071
12072 if (redisplay_menu_p)
12073 display_menu_bar (w);
12074
12075 #ifdef HAVE_WINDOW_SYSTEM
12076 #ifdef USE_GTK
12077 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12078 #else
12079 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12080 && (FRAME_TOOL_BAR_LINES (f) > 0
12081 || auto_resize_tool_bars_p);
12082
12083 #endif
12084
12085 if (redisplay_tool_bar_p)
12086 redisplay_tool_bar (f);
12087 #endif
12088 }
12089
12090 #ifdef HAVE_WINDOW_SYSTEM
12091 if (update_window_fringes (w, 0)
12092 && !just_this_one_p
12093 && (used_current_matrix_p || overlay_arrow_seen)
12094 && !w->pseudo_window_p)
12095 {
12096 update_begin (f);
12097 BLOCK_INPUT;
12098 draw_window_fringes (w);
12099 UNBLOCK_INPUT;
12100 update_end (f);
12101 }
12102 #endif /* HAVE_WINDOW_SYSTEM */
12103
12104 /* We go to this label, with fonts_changed_p nonzero,
12105 if it is necessary to try again using larger glyph matrices.
12106 We have to redeem the scroll bar even in this case,
12107 because the loop in redisplay_internal expects that. */
12108 need_larger_matrices:
12109 ;
12110 finish_scroll_bars:
12111
12112 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12113 {
12114 /* Set the thumb's position and size. */
12115 set_vertical_scroll_bar (w);
12116
12117 /* Note that we actually used the scroll bar attached to this
12118 window, so it shouldn't be deleted at the end of redisplay. */
12119 redeem_scroll_bar_hook (w);
12120 }
12121
12122 /* Restore current_buffer and value of point in it. */
12123 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12124 set_buffer_internal_1 (old);
12125 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12126
12127 unbind_to (count, Qnil);
12128 }
12129
12130
12131 /* Build the complete desired matrix of WINDOW with a window start
12132 buffer position POS. Value is non-zero if successful. It is zero
12133 if fonts were loaded during redisplay which makes re-adjusting
12134 glyph matrices necessary. */
12135
12136 int
12137 try_window (window, pos)
12138 Lisp_Object window;
12139 struct text_pos pos;
12140 {
12141 struct window *w = XWINDOW (window);
12142 struct it it;
12143 struct glyph_row *last_text_row = NULL;
12144
12145 /* Make POS the new window start. */
12146 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12147
12148 /* Mark cursor position as unknown. No overlay arrow seen. */
12149 w->cursor.vpos = -1;
12150 overlay_arrow_seen = 0;
12151
12152 /* Initialize iterator and info to start at POS. */
12153 start_display (&it, w, pos);
12154
12155 /* Display all lines of W. */
12156 while (it.current_y < it.last_visible_y)
12157 {
12158 if (display_line (&it))
12159 last_text_row = it.glyph_row - 1;
12160 if (fonts_changed_p)
12161 return 0;
12162 }
12163
12164 /* If bottom moved off end of frame, change mode line percentage. */
12165 if (XFASTINT (w->window_end_pos) <= 0
12166 && Z != IT_CHARPOS (it))
12167 w->update_mode_line = Qt;
12168
12169 /* Set window_end_pos to the offset of the last character displayed
12170 on the window from the end of current_buffer. Set
12171 window_end_vpos to its row number. */
12172 if (last_text_row)
12173 {
12174 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12175 w->window_end_bytepos
12176 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12177 w->window_end_pos
12178 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12179 w->window_end_vpos
12180 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12181 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12182 ->displays_text_p);
12183 }
12184 else
12185 {
12186 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12187 w->window_end_pos = make_number (Z - ZV);
12188 w->window_end_vpos = make_number (0);
12189 }
12190
12191 /* But that is not valid info until redisplay finishes. */
12192 w->window_end_valid = Qnil;
12193 return 1;
12194 }
12195
12196
12197 \f
12198 /************************************************************************
12199 Window redisplay reusing current matrix when buffer has not changed
12200 ************************************************************************/
12201
12202 /* Try redisplay of window W showing an unchanged buffer with a
12203 different window start than the last time it was displayed by
12204 reusing its current matrix. Value is non-zero if successful.
12205 W->start is the new window start. */
12206
12207 static int
12208 try_window_reusing_current_matrix (w)
12209 struct window *w;
12210 {
12211 struct frame *f = XFRAME (w->frame);
12212 struct glyph_row *row, *bottom_row;
12213 struct it it;
12214 struct run run;
12215 struct text_pos start, new_start;
12216 int nrows_scrolled, i;
12217 struct glyph_row *last_text_row;
12218 struct glyph_row *last_reused_text_row;
12219 struct glyph_row *start_row;
12220 int start_vpos, min_y, max_y;
12221
12222 #if GLYPH_DEBUG
12223 if (inhibit_try_window_reusing)
12224 return 0;
12225 #endif
12226
12227 if (/* This function doesn't handle terminal frames. */
12228 !FRAME_WINDOW_P (f)
12229 /* Don't try to reuse the display if windows have been split
12230 or such. */
12231 || windows_or_buffers_changed
12232 || cursor_type_changed)
12233 return 0;
12234
12235 /* Can't do this if region may have changed. */
12236 if ((!NILP (Vtransient_mark_mode)
12237 && !NILP (current_buffer->mark_active))
12238 || !NILP (w->region_showing)
12239 || !NILP (Vshow_trailing_whitespace))
12240 return 0;
12241
12242 /* If top-line visibility has changed, give up. */
12243 if (WINDOW_WANTS_HEADER_LINE_P (w)
12244 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12245 return 0;
12246
12247 /* Give up if old or new display is scrolled vertically. We could
12248 make this function handle this, but right now it doesn't. */
12249 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12250 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12251 return 0;
12252
12253 /* The variable new_start now holds the new window start. The old
12254 start `start' can be determined from the current matrix. */
12255 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12256 start = start_row->start.pos;
12257 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12258
12259 /* Clear the desired matrix for the display below. */
12260 clear_glyph_matrix (w->desired_matrix);
12261
12262 if (CHARPOS (new_start) <= CHARPOS (start))
12263 {
12264 int first_row_y;
12265
12266 /* Don't use this method if the display starts with an ellipsis
12267 displayed for invisible text. It's not easy to handle that case
12268 below, and it's certainly not worth the effort since this is
12269 not a frequent case. */
12270 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12271 return 0;
12272
12273 IF_DEBUG (debug_method_add (w, "twu1"));
12274
12275 /* Display up to a row that can be reused. The variable
12276 last_text_row is set to the last row displayed that displays
12277 text. Note that it.vpos == 0 if or if not there is a
12278 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12279 start_display (&it, w, new_start);
12280 first_row_y = it.current_y;
12281 w->cursor.vpos = -1;
12282 last_text_row = last_reused_text_row = NULL;
12283
12284 while (it.current_y < it.last_visible_y
12285 && IT_CHARPOS (it) < CHARPOS (start)
12286 && !fonts_changed_p)
12287 if (display_line (&it))
12288 last_text_row = it.glyph_row - 1;
12289
12290 /* A value of current_y < last_visible_y means that we stopped
12291 at the previous window start, which in turn means that we
12292 have at least one reusable row. */
12293 if (it.current_y < it.last_visible_y)
12294 {
12295 /* IT.vpos always starts from 0; it counts text lines. */
12296 nrows_scrolled = it.vpos;
12297
12298 /* Find PT if not already found in the lines displayed. */
12299 if (w->cursor.vpos < 0)
12300 {
12301 int dy = it.current_y - first_row_y;
12302
12303 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12304 row = row_containing_pos (w, PT, row, NULL, dy);
12305 if (row)
12306 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12307 dy, nrows_scrolled);
12308 else
12309 {
12310 clear_glyph_matrix (w->desired_matrix);
12311 return 0;
12312 }
12313 }
12314
12315 /* Scroll the display. Do it before the current matrix is
12316 changed. The problem here is that update has not yet
12317 run, i.e. part of the current matrix is not up to date.
12318 scroll_run_hook will clear the cursor, and use the
12319 current matrix to get the height of the row the cursor is
12320 in. */
12321 run.current_y = first_row_y;
12322 run.desired_y = it.current_y;
12323 run.height = it.last_visible_y - it.current_y;
12324
12325 if (run.height > 0 && run.current_y != run.desired_y)
12326 {
12327 update_begin (f);
12328 rif->update_window_begin_hook (w);
12329 rif->clear_window_mouse_face (w);
12330 rif->scroll_run_hook (w, &run);
12331 rif->update_window_end_hook (w, 0, 0);
12332 update_end (f);
12333 }
12334
12335 /* Shift current matrix down by nrows_scrolled lines. */
12336 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12337 rotate_matrix (w->current_matrix,
12338 start_vpos,
12339 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12340 nrows_scrolled);
12341
12342 /* Disable lines that must be updated. */
12343 for (i = 0; i < it.vpos; ++i)
12344 (start_row + i)->enabled_p = 0;
12345
12346 /* Re-compute Y positions. */
12347 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12348 max_y = it.last_visible_y;
12349 for (row = start_row + nrows_scrolled;
12350 row < bottom_row;
12351 ++row)
12352 {
12353 row->y = it.current_y;
12354 row->visible_height = row->height;
12355
12356 if (row->y < min_y)
12357 row->visible_height -= min_y - row->y;
12358 if (row->y + row->height > max_y)
12359 row->visible_height -= row->y + row->height - max_y;
12360 row->redraw_fringe_bitmaps_p = 1;
12361
12362 it.current_y += row->height;
12363
12364 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12365 last_reused_text_row = row;
12366 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12367 break;
12368 }
12369
12370 /* Disable lines in the current matrix which are now
12371 below the window. */
12372 for (++row; row < bottom_row; ++row)
12373 row->enabled_p = 0;
12374 }
12375
12376 /* Update window_end_pos etc.; last_reused_text_row is the last
12377 reused row from the current matrix containing text, if any.
12378 The value of last_text_row is the last displayed line
12379 containing text. */
12380 if (last_reused_text_row)
12381 {
12382 w->window_end_bytepos
12383 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12384 w->window_end_pos
12385 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12386 w->window_end_vpos
12387 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12388 w->current_matrix));
12389 }
12390 else if (last_text_row)
12391 {
12392 w->window_end_bytepos
12393 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12394 w->window_end_pos
12395 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12396 w->window_end_vpos
12397 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12398 }
12399 else
12400 {
12401 /* This window must be completely empty. */
12402 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12403 w->window_end_pos = make_number (Z - ZV);
12404 w->window_end_vpos = make_number (0);
12405 }
12406 w->window_end_valid = Qnil;
12407
12408 /* Update hint: don't try scrolling again in update_window. */
12409 w->desired_matrix->no_scrolling_p = 1;
12410
12411 #if GLYPH_DEBUG
12412 debug_method_add (w, "try_window_reusing_current_matrix 1");
12413 #endif
12414 return 1;
12415 }
12416 else if (CHARPOS (new_start) > CHARPOS (start))
12417 {
12418 struct glyph_row *pt_row, *row;
12419 struct glyph_row *first_reusable_row;
12420 struct glyph_row *first_row_to_display;
12421 int dy;
12422 int yb = window_text_bottom_y (w);
12423
12424 /* Find the row starting at new_start, if there is one. Don't
12425 reuse a partially visible line at the end. */
12426 first_reusable_row = start_row;
12427 while (first_reusable_row->enabled_p
12428 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12429 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12430 < CHARPOS (new_start)))
12431 ++first_reusable_row;
12432
12433 /* Give up if there is no row to reuse. */
12434 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12435 || !first_reusable_row->enabled_p
12436 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12437 != CHARPOS (new_start)))
12438 return 0;
12439
12440 /* We can reuse fully visible rows beginning with
12441 first_reusable_row to the end of the window. Set
12442 first_row_to_display to the first row that cannot be reused.
12443 Set pt_row to the row containing point, if there is any. */
12444 pt_row = NULL;
12445 for (first_row_to_display = first_reusable_row;
12446 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12447 ++first_row_to_display)
12448 {
12449 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12450 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12451 pt_row = first_row_to_display;
12452 }
12453
12454 /* Start displaying at the start of first_row_to_display. */
12455 xassert (first_row_to_display->y < yb);
12456 init_to_row_start (&it, w, first_row_to_display);
12457
12458 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12459 - start_vpos);
12460 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12461 - nrows_scrolled);
12462 it.current_y = (first_row_to_display->y - first_reusable_row->y
12463 + WINDOW_HEADER_LINE_HEIGHT (w));
12464
12465 /* Display lines beginning with first_row_to_display in the
12466 desired matrix. Set last_text_row to the last row displayed
12467 that displays text. */
12468 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12469 if (pt_row == NULL)
12470 w->cursor.vpos = -1;
12471 last_text_row = NULL;
12472 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12473 if (display_line (&it))
12474 last_text_row = it.glyph_row - 1;
12475
12476 /* Give up If point isn't in a row displayed or reused. */
12477 if (w->cursor.vpos < 0)
12478 {
12479 clear_glyph_matrix (w->desired_matrix);
12480 return 0;
12481 }
12482
12483 /* If point is in a reused row, adjust y and vpos of the cursor
12484 position. */
12485 if (pt_row)
12486 {
12487 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12488 w->current_matrix);
12489 w->cursor.y -= first_reusable_row->y;
12490 }
12491
12492 /* Scroll the display. */
12493 run.current_y = first_reusable_row->y;
12494 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12495 run.height = it.last_visible_y - run.current_y;
12496 dy = run.current_y - run.desired_y;
12497
12498 if (run.height)
12499 {
12500 update_begin (f);
12501 rif->update_window_begin_hook (w);
12502 rif->clear_window_mouse_face (w);
12503 rif->scroll_run_hook (w, &run);
12504 rif->update_window_end_hook (w, 0, 0);
12505 update_end (f);
12506 }
12507
12508 /* Adjust Y positions of reused rows. */
12509 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12510 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12511 max_y = it.last_visible_y;
12512 for (row = first_reusable_row; row < first_row_to_display; ++row)
12513 {
12514 row->y -= dy;
12515 row->visible_height = row->height;
12516 if (row->y < min_y)
12517 row->visible_height -= min_y - row->y;
12518 if (row->y + row->height > max_y)
12519 row->visible_height -= row->y + row->height - max_y;
12520 row->redraw_fringe_bitmaps_p = 1;
12521 }
12522
12523 /* Scroll the current matrix. */
12524 xassert (nrows_scrolled > 0);
12525 rotate_matrix (w->current_matrix,
12526 start_vpos,
12527 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12528 -nrows_scrolled);
12529
12530 /* Disable rows not reused. */
12531 for (row -= nrows_scrolled; row < bottom_row; ++row)
12532 row->enabled_p = 0;
12533
12534 /* Adjust window end. A null value of last_text_row means that
12535 the window end is in reused rows which in turn means that
12536 only its vpos can have changed. */
12537 if (last_text_row)
12538 {
12539 w->window_end_bytepos
12540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12541 w->window_end_pos
12542 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12543 w->window_end_vpos
12544 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12545 }
12546 else
12547 {
12548 w->window_end_vpos
12549 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12550 }
12551
12552 w->window_end_valid = Qnil;
12553 w->desired_matrix->no_scrolling_p = 1;
12554
12555 #if GLYPH_DEBUG
12556 debug_method_add (w, "try_window_reusing_current_matrix 2");
12557 #endif
12558 return 1;
12559 }
12560
12561 return 0;
12562 }
12563
12564
12565 \f
12566 /************************************************************************
12567 Window redisplay reusing current matrix when buffer has changed
12568 ************************************************************************/
12569
12570 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12571 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12572 int *, int *));
12573 static struct glyph_row *
12574 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12575 struct glyph_row *));
12576
12577
12578 /* Return the last row in MATRIX displaying text. If row START is
12579 non-null, start searching with that row. IT gives the dimensions
12580 of the display. Value is null if matrix is empty; otherwise it is
12581 a pointer to the row found. */
12582
12583 static struct glyph_row *
12584 find_last_row_displaying_text (matrix, it, start)
12585 struct glyph_matrix *matrix;
12586 struct it *it;
12587 struct glyph_row *start;
12588 {
12589 struct glyph_row *row, *row_found;
12590
12591 /* Set row_found to the last row in IT->w's current matrix
12592 displaying text. The loop looks funny but think of partially
12593 visible lines. */
12594 row_found = NULL;
12595 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12596 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12597 {
12598 xassert (row->enabled_p);
12599 row_found = row;
12600 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12601 break;
12602 ++row;
12603 }
12604
12605 return row_found;
12606 }
12607
12608
12609 /* Return the last row in the current matrix of W that is not affected
12610 by changes at the start of current_buffer that occurred since W's
12611 current matrix was built. Value is null if no such row exists.
12612
12613 BEG_UNCHANGED us the number of characters unchanged at the start of
12614 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12615 first changed character in current_buffer. Characters at positions <
12616 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12617 when the current matrix was built. */
12618
12619 static struct glyph_row *
12620 find_last_unchanged_at_beg_row (w)
12621 struct window *w;
12622 {
12623 int first_changed_pos = BEG + BEG_UNCHANGED;
12624 struct glyph_row *row;
12625 struct glyph_row *row_found = NULL;
12626 int yb = window_text_bottom_y (w);
12627
12628 /* Find the last row displaying unchanged text. */
12629 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12630 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12631 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12632 {
12633 if (/* If row ends before first_changed_pos, it is unchanged,
12634 except in some case. */
12635 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12636 /* When row ends in ZV and we write at ZV it is not
12637 unchanged. */
12638 && !row->ends_at_zv_p
12639 /* When first_changed_pos is the end of a continued line,
12640 row is not unchanged because it may be no longer
12641 continued. */
12642 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12643 && (row->continued_p
12644 || row->exact_window_width_line_p)))
12645 row_found = row;
12646
12647 /* Stop if last visible row. */
12648 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12649 break;
12650
12651 ++row;
12652 }
12653
12654 return row_found;
12655 }
12656
12657
12658 /* Find the first glyph row in the current matrix of W that is not
12659 affected by changes at the end of current_buffer since the
12660 time W's current matrix was built.
12661
12662 Return in *DELTA the number of chars by which buffer positions in
12663 unchanged text at the end of current_buffer must be adjusted.
12664
12665 Return in *DELTA_BYTES the corresponding number of bytes.
12666
12667 Value is null if no such row exists, i.e. all rows are affected by
12668 changes. */
12669
12670 static struct glyph_row *
12671 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12672 struct window *w;
12673 int *delta, *delta_bytes;
12674 {
12675 struct glyph_row *row;
12676 struct glyph_row *row_found = NULL;
12677
12678 *delta = *delta_bytes = 0;
12679
12680 /* Display must not have been paused, otherwise the current matrix
12681 is not up to date. */
12682 if (NILP (w->window_end_valid))
12683 abort ();
12684
12685 /* A value of window_end_pos >= END_UNCHANGED means that the window
12686 end is in the range of changed text. If so, there is no
12687 unchanged row at the end of W's current matrix. */
12688 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12689 return NULL;
12690
12691 /* Set row to the last row in W's current matrix displaying text. */
12692 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12693
12694 /* If matrix is entirely empty, no unchanged row exists. */
12695 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12696 {
12697 /* The value of row is the last glyph row in the matrix having a
12698 meaningful buffer position in it. The end position of row
12699 corresponds to window_end_pos. This allows us to translate
12700 buffer positions in the current matrix to current buffer
12701 positions for characters not in changed text. */
12702 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12703 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12704 int last_unchanged_pos, last_unchanged_pos_old;
12705 struct glyph_row *first_text_row
12706 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12707
12708 *delta = Z - Z_old;
12709 *delta_bytes = Z_BYTE - Z_BYTE_old;
12710
12711 /* Set last_unchanged_pos to the buffer position of the last
12712 character in the buffer that has not been changed. Z is the
12713 index + 1 of the last character in current_buffer, i.e. by
12714 subtracting END_UNCHANGED we get the index of the last
12715 unchanged character, and we have to add BEG to get its buffer
12716 position. */
12717 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12718 last_unchanged_pos_old = last_unchanged_pos - *delta;
12719
12720 /* Search backward from ROW for a row displaying a line that
12721 starts at a minimum position >= last_unchanged_pos_old. */
12722 for (; row > first_text_row; --row)
12723 {
12724 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12725 abort ();
12726
12727 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12728 row_found = row;
12729 }
12730 }
12731
12732 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12733 abort ();
12734
12735 return row_found;
12736 }
12737
12738
12739 /* Make sure that glyph rows in the current matrix of window W
12740 reference the same glyph memory as corresponding rows in the
12741 frame's frame matrix. This function is called after scrolling W's
12742 current matrix on a terminal frame in try_window_id and
12743 try_window_reusing_current_matrix. */
12744
12745 static void
12746 sync_frame_with_window_matrix_rows (w)
12747 struct window *w;
12748 {
12749 struct frame *f = XFRAME (w->frame);
12750 struct glyph_row *window_row, *window_row_end, *frame_row;
12751
12752 /* Preconditions: W must be a leaf window and full-width. Its frame
12753 must have a frame matrix. */
12754 xassert (NILP (w->hchild) && NILP (w->vchild));
12755 xassert (WINDOW_FULL_WIDTH_P (w));
12756 xassert (!FRAME_WINDOW_P (f));
12757
12758 /* If W is a full-width window, glyph pointers in W's current matrix
12759 have, by definition, to be the same as glyph pointers in the
12760 corresponding frame matrix. Note that frame matrices have no
12761 marginal areas (see build_frame_matrix). */
12762 window_row = w->current_matrix->rows;
12763 window_row_end = window_row + w->current_matrix->nrows;
12764 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12765 while (window_row < window_row_end)
12766 {
12767 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12768 struct glyph *end = window_row->glyphs[LAST_AREA];
12769
12770 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12771 frame_row->glyphs[TEXT_AREA] = start;
12772 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12773 frame_row->glyphs[LAST_AREA] = end;
12774
12775 /* Disable frame rows whose corresponding window rows have
12776 been disabled in try_window_id. */
12777 if (!window_row->enabled_p)
12778 frame_row->enabled_p = 0;
12779
12780 ++window_row, ++frame_row;
12781 }
12782 }
12783
12784
12785 /* Find the glyph row in window W containing CHARPOS. Consider all
12786 rows between START and END (not inclusive). END null means search
12787 all rows to the end of the display area of W. Value is the row
12788 containing CHARPOS or null. */
12789
12790 struct glyph_row *
12791 row_containing_pos (w, charpos, start, end, dy)
12792 struct window *w;
12793 int charpos;
12794 struct glyph_row *start, *end;
12795 int dy;
12796 {
12797 struct glyph_row *row = start;
12798 int last_y;
12799
12800 /* If we happen to start on a header-line, skip that. */
12801 if (row->mode_line_p)
12802 ++row;
12803
12804 if ((end && row >= end) || !row->enabled_p)
12805 return NULL;
12806
12807 last_y = window_text_bottom_y (w) - dy;
12808
12809 while (1)
12810 {
12811 /* Give up if we have gone too far. */
12812 if (end && row >= end)
12813 return NULL;
12814 /* This formerly returned if they were equal.
12815 I think that both quantities are of a "last plus one" type;
12816 if so, when they are equal, the row is within the screen. -- rms. */
12817 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12818 return NULL;
12819
12820 /* If it is in this row, return this row. */
12821 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12822 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12823 /* The end position of a row equals the start
12824 position of the next row. If CHARPOS is there, we
12825 would rather display it in the next line, except
12826 when this line ends in ZV. */
12827 && !row->ends_at_zv_p
12828 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12829 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12830 return row;
12831 ++row;
12832 }
12833 }
12834
12835
12836 /* Try to redisplay window W by reusing its existing display. W's
12837 current matrix must be up to date when this function is called,
12838 i.e. window_end_valid must not be nil.
12839
12840 Value is
12841
12842 1 if display has been updated
12843 0 if otherwise unsuccessful
12844 -1 if redisplay with same window start is known not to succeed
12845
12846 The following steps are performed:
12847
12848 1. Find the last row in the current matrix of W that is not
12849 affected by changes at the start of current_buffer. If no such row
12850 is found, give up.
12851
12852 2. Find the first row in W's current matrix that is not affected by
12853 changes at the end of current_buffer. Maybe there is no such row.
12854
12855 3. Display lines beginning with the row + 1 found in step 1 to the
12856 row found in step 2 or, if step 2 didn't find a row, to the end of
12857 the window.
12858
12859 4. If cursor is not known to appear on the window, give up.
12860
12861 5. If display stopped at the row found in step 2, scroll the
12862 display and current matrix as needed.
12863
12864 6. Maybe display some lines at the end of W, if we must. This can
12865 happen under various circumstances, like a partially visible line
12866 becoming fully visible, or because newly displayed lines are displayed
12867 in smaller font sizes.
12868
12869 7. Update W's window end information. */
12870
12871 static int
12872 try_window_id (w)
12873 struct window *w;
12874 {
12875 struct frame *f = XFRAME (w->frame);
12876 struct glyph_matrix *current_matrix = w->current_matrix;
12877 struct glyph_matrix *desired_matrix = w->desired_matrix;
12878 struct glyph_row *last_unchanged_at_beg_row;
12879 struct glyph_row *first_unchanged_at_end_row;
12880 struct glyph_row *row;
12881 struct glyph_row *bottom_row;
12882 int bottom_vpos;
12883 struct it it;
12884 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12885 struct text_pos start_pos;
12886 struct run run;
12887 int first_unchanged_at_end_vpos = 0;
12888 struct glyph_row *last_text_row, *last_text_row_at_end;
12889 struct text_pos start;
12890 int first_changed_charpos, last_changed_charpos;
12891
12892 #if GLYPH_DEBUG
12893 if (inhibit_try_window_id)
12894 return 0;
12895 #endif
12896
12897 /* This is handy for debugging. */
12898 #if 0
12899 #define GIVE_UP(X) \
12900 do { \
12901 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12902 return 0; \
12903 } while (0)
12904 #else
12905 #define GIVE_UP(X) return 0
12906 #endif
12907
12908 SET_TEXT_POS_FROM_MARKER (start, w->start);
12909
12910 /* Don't use this for mini-windows because these can show
12911 messages and mini-buffers, and we don't handle that here. */
12912 if (MINI_WINDOW_P (w))
12913 GIVE_UP (1);
12914
12915 /* This flag is used to prevent redisplay optimizations. */
12916 if (windows_or_buffers_changed || cursor_type_changed)
12917 GIVE_UP (2);
12918
12919 /* Verify that narrowing has not changed.
12920 Also verify that we were not told to prevent redisplay optimizations.
12921 It would be nice to further
12922 reduce the number of cases where this prevents try_window_id. */
12923 if (current_buffer->clip_changed
12924 || current_buffer->prevent_redisplay_optimizations_p)
12925 GIVE_UP (3);
12926
12927 /* Window must either use window-based redisplay or be full width. */
12928 if (!FRAME_WINDOW_P (f)
12929 && (!line_ins_del_ok
12930 || !WINDOW_FULL_WIDTH_P (w)))
12931 GIVE_UP (4);
12932
12933 /* Give up if point is not known NOT to appear in W. */
12934 if (PT < CHARPOS (start))
12935 GIVE_UP (5);
12936
12937 /* Another way to prevent redisplay optimizations. */
12938 if (XFASTINT (w->last_modified) == 0)
12939 GIVE_UP (6);
12940
12941 /* Verify that window is not hscrolled. */
12942 if (XFASTINT (w->hscroll) != 0)
12943 GIVE_UP (7);
12944
12945 /* Verify that display wasn't paused. */
12946 if (NILP (w->window_end_valid))
12947 GIVE_UP (8);
12948
12949 /* Can't use this if highlighting a region because a cursor movement
12950 will do more than just set the cursor. */
12951 if (!NILP (Vtransient_mark_mode)
12952 && !NILP (current_buffer->mark_active))
12953 GIVE_UP (9);
12954
12955 /* Likewise if highlighting trailing whitespace. */
12956 if (!NILP (Vshow_trailing_whitespace))
12957 GIVE_UP (11);
12958
12959 /* Likewise if showing a region. */
12960 if (!NILP (w->region_showing))
12961 GIVE_UP (10);
12962
12963 /* Can use this if overlay arrow position and or string have changed. */
12964 if (overlay_arrows_changed_p ())
12965 GIVE_UP (12);
12966
12967
12968 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12969 only if buffer has really changed. The reason is that the gap is
12970 initially at Z for freshly visited files. The code below would
12971 set end_unchanged to 0 in that case. */
12972 if (MODIFF > SAVE_MODIFF
12973 /* This seems to happen sometimes after saving a buffer. */
12974 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12975 {
12976 if (GPT - BEG < BEG_UNCHANGED)
12977 BEG_UNCHANGED = GPT - BEG;
12978 if (Z - GPT < END_UNCHANGED)
12979 END_UNCHANGED = Z - GPT;
12980 }
12981
12982 /* The position of the first and last character that has been changed. */
12983 first_changed_charpos = BEG + BEG_UNCHANGED;
12984 last_changed_charpos = Z - END_UNCHANGED;
12985
12986 /* If window starts after a line end, and the last change is in
12987 front of that newline, then changes don't affect the display.
12988 This case happens with stealth-fontification. Note that although
12989 the display is unchanged, glyph positions in the matrix have to
12990 be adjusted, of course. */
12991 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12992 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12993 && ((last_changed_charpos < CHARPOS (start)
12994 && CHARPOS (start) == BEGV)
12995 || (last_changed_charpos < CHARPOS (start) - 1
12996 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12997 {
12998 int Z_old, delta, Z_BYTE_old, delta_bytes;
12999 struct glyph_row *r0;
13000
13001 /* Compute how many chars/bytes have been added to or removed
13002 from the buffer. */
13003 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13004 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13005 delta = Z - Z_old;
13006 delta_bytes = Z_BYTE - Z_BYTE_old;
13007
13008 /* Give up if PT is not in the window. Note that it already has
13009 been checked at the start of try_window_id that PT is not in
13010 front of the window start. */
13011 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13012 GIVE_UP (13);
13013
13014 /* If window start is unchanged, we can reuse the whole matrix
13015 as is, after adjusting glyph positions. No need to compute
13016 the window end again, since its offset from Z hasn't changed. */
13017 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13018 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13019 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13020 /* PT must not be in a partially visible line. */
13021 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13022 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13023 {
13024 /* Adjust positions in the glyph matrix. */
13025 if (delta || delta_bytes)
13026 {
13027 struct glyph_row *r1
13028 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13029 increment_matrix_positions (w->current_matrix,
13030 MATRIX_ROW_VPOS (r0, current_matrix),
13031 MATRIX_ROW_VPOS (r1, current_matrix),
13032 delta, delta_bytes);
13033 }
13034
13035 /* Set the cursor. */
13036 row = row_containing_pos (w, PT, r0, NULL, 0);
13037 if (row)
13038 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13039 else
13040 abort ();
13041 return 1;
13042 }
13043 }
13044
13045 /* Handle the case that changes are all below what is displayed in
13046 the window, and that PT is in the window. This shortcut cannot
13047 be taken if ZV is visible in the window, and text has been added
13048 there that is visible in the window. */
13049 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13050 /* ZV is not visible in the window, or there are no
13051 changes at ZV, actually. */
13052 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13053 || first_changed_charpos == last_changed_charpos))
13054 {
13055 struct glyph_row *r0;
13056
13057 /* Give up if PT is not in the window. Note that it already has
13058 been checked at the start of try_window_id that PT is not in
13059 front of the window start. */
13060 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13061 GIVE_UP (14);
13062
13063 /* If window start is unchanged, we can reuse the whole matrix
13064 as is, without changing glyph positions since no text has
13065 been added/removed in front of the window end. */
13066 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13067 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13068 /* PT must not be in a partially visible line. */
13069 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13070 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13071 {
13072 /* We have to compute the window end anew since text
13073 can have been added/removed after it. */
13074 w->window_end_pos
13075 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13076 w->window_end_bytepos
13077 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13078
13079 /* Set the cursor. */
13080 row = row_containing_pos (w, PT, r0, NULL, 0);
13081 if (row)
13082 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13083 else
13084 abort ();
13085 return 2;
13086 }
13087 }
13088
13089 /* Give up if window start is in the changed area.
13090
13091 The condition used to read
13092
13093 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13094
13095 but why that was tested escapes me at the moment. */
13096 if (CHARPOS (start) >= first_changed_charpos
13097 && CHARPOS (start) <= last_changed_charpos)
13098 GIVE_UP (15);
13099
13100 /* Check that window start agrees with the start of the first glyph
13101 row in its current matrix. Check this after we know the window
13102 start is not in changed text, otherwise positions would not be
13103 comparable. */
13104 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13105 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13106 GIVE_UP (16);
13107
13108 /* Give up if the window ends in strings. Overlay strings
13109 at the end are difficult to handle, so don't try. */
13110 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13111 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13112 GIVE_UP (20);
13113
13114 /* Compute the position at which we have to start displaying new
13115 lines. Some of the lines at the top of the window might be
13116 reusable because they are not displaying changed text. Find the
13117 last row in W's current matrix not affected by changes at the
13118 start of current_buffer. Value is null if changes start in the
13119 first line of window. */
13120 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13121 if (last_unchanged_at_beg_row)
13122 {
13123 /* Avoid starting to display in the moddle of a character, a TAB
13124 for instance. This is easier than to set up the iterator
13125 exactly, and it's not a frequent case, so the additional
13126 effort wouldn't really pay off. */
13127 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13128 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13129 && last_unchanged_at_beg_row > w->current_matrix->rows)
13130 --last_unchanged_at_beg_row;
13131
13132 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13133 GIVE_UP (17);
13134
13135 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13136 GIVE_UP (18);
13137 start_pos = it.current.pos;
13138
13139 /* Start displaying new lines in the desired matrix at the same
13140 vpos we would use in the current matrix, i.e. below
13141 last_unchanged_at_beg_row. */
13142 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13143 current_matrix);
13144 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13145 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13146
13147 xassert (it.hpos == 0 && it.current_x == 0);
13148 }
13149 else
13150 {
13151 /* There are no reusable lines at the start of the window.
13152 Start displaying in the first text line. */
13153 start_display (&it, w, start);
13154 it.vpos = it.first_vpos;
13155 start_pos = it.current.pos;
13156 }
13157
13158 /* Find the first row that is not affected by changes at the end of
13159 the buffer. Value will be null if there is no unchanged row, in
13160 which case we must redisplay to the end of the window. delta
13161 will be set to the value by which buffer positions beginning with
13162 first_unchanged_at_end_row have to be adjusted due to text
13163 changes. */
13164 first_unchanged_at_end_row
13165 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13166 IF_DEBUG (debug_delta = delta);
13167 IF_DEBUG (debug_delta_bytes = delta_bytes);
13168
13169 /* Set stop_pos to the buffer position up to which we will have to
13170 display new lines. If first_unchanged_at_end_row != NULL, this
13171 is the buffer position of the start of the line displayed in that
13172 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13173 that we don't stop at a buffer position. */
13174 stop_pos = 0;
13175 if (first_unchanged_at_end_row)
13176 {
13177 xassert (last_unchanged_at_beg_row == NULL
13178 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13179
13180 /* If this is a continuation line, move forward to the next one
13181 that isn't. Changes in lines above affect this line.
13182 Caution: this may move first_unchanged_at_end_row to a row
13183 not displaying text. */
13184 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13185 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13186 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13187 < it.last_visible_y))
13188 ++first_unchanged_at_end_row;
13189
13190 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13191 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13192 >= it.last_visible_y))
13193 first_unchanged_at_end_row = NULL;
13194 else
13195 {
13196 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13197 + delta);
13198 first_unchanged_at_end_vpos
13199 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13200 xassert (stop_pos >= Z - END_UNCHANGED);
13201 }
13202 }
13203 else if (last_unchanged_at_beg_row == NULL)
13204 GIVE_UP (19);
13205
13206
13207 #if GLYPH_DEBUG
13208
13209 /* Either there is no unchanged row at the end, or the one we have
13210 now displays text. This is a necessary condition for the window
13211 end pos calculation at the end of this function. */
13212 xassert (first_unchanged_at_end_row == NULL
13213 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13214
13215 debug_last_unchanged_at_beg_vpos
13216 = (last_unchanged_at_beg_row
13217 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13218 : -1);
13219 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13220
13221 #endif /* GLYPH_DEBUG != 0 */
13222
13223
13224 /* Display new lines. Set last_text_row to the last new line
13225 displayed which has text on it, i.e. might end up as being the
13226 line where the window_end_vpos is. */
13227 w->cursor.vpos = -1;
13228 last_text_row = NULL;
13229 overlay_arrow_seen = 0;
13230 while (it.current_y < it.last_visible_y
13231 && !fonts_changed_p
13232 && (first_unchanged_at_end_row == NULL
13233 || IT_CHARPOS (it) < stop_pos))
13234 {
13235 if (display_line (&it))
13236 last_text_row = it.glyph_row - 1;
13237 }
13238
13239 if (fonts_changed_p)
13240 return -1;
13241
13242
13243 /* Compute differences in buffer positions, y-positions etc. for
13244 lines reused at the bottom of the window. Compute what we can
13245 scroll. */
13246 if (first_unchanged_at_end_row
13247 /* No lines reused because we displayed everything up to the
13248 bottom of the window. */
13249 && it.current_y < it.last_visible_y)
13250 {
13251 dvpos = (it.vpos
13252 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13253 current_matrix));
13254 dy = it.current_y - first_unchanged_at_end_row->y;
13255 run.current_y = first_unchanged_at_end_row->y;
13256 run.desired_y = run.current_y + dy;
13257 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13258 }
13259 else
13260 {
13261 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13262 first_unchanged_at_end_row = NULL;
13263 }
13264 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13265
13266
13267 /* Find the cursor if not already found. We have to decide whether
13268 PT will appear on this window (it sometimes doesn't, but this is
13269 not a very frequent case.) This decision has to be made before
13270 the current matrix is altered. A value of cursor.vpos < 0 means
13271 that PT is either in one of the lines beginning at
13272 first_unchanged_at_end_row or below the window. Don't care for
13273 lines that might be displayed later at the window end; as
13274 mentioned, this is not a frequent case. */
13275 if (w->cursor.vpos < 0)
13276 {
13277 /* Cursor in unchanged rows at the top? */
13278 if (PT < CHARPOS (start_pos)
13279 && last_unchanged_at_beg_row)
13280 {
13281 row = row_containing_pos (w, PT,
13282 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13283 last_unchanged_at_beg_row + 1, 0);
13284 if (row)
13285 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13286 }
13287
13288 /* Start from first_unchanged_at_end_row looking for PT. */
13289 else if (first_unchanged_at_end_row)
13290 {
13291 row = row_containing_pos (w, PT - delta,
13292 first_unchanged_at_end_row, NULL, 0);
13293 if (row)
13294 set_cursor_from_row (w, row, w->current_matrix, delta,
13295 delta_bytes, dy, dvpos);
13296 }
13297
13298 /* Give up if cursor was not found. */
13299 if (w->cursor.vpos < 0)
13300 {
13301 clear_glyph_matrix (w->desired_matrix);
13302 return -1;
13303 }
13304 }
13305
13306 /* Don't let the cursor end in the scroll margins. */
13307 {
13308 int this_scroll_margin, cursor_height;
13309
13310 this_scroll_margin = max (0, scroll_margin);
13311 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13312 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13313 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13314
13315 if ((w->cursor.y < this_scroll_margin
13316 && CHARPOS (start) > BEGV)
13317 /* Don't take scroll margin into account at the bottom because
13318 old redisplay didn't do it either. */
13319 || w->cursor.y + cursor_height > it.last_visible_y)
13320 {
13321 w->cursor.vpos = -1;
13322 clear_glyph_matrix (w->desired_matrix);
13323 return -1;
13324 }
13325 }
13326
13327 /* Scroll the display. Do it before changing the current matrix so
13328 that xterm.c doesn't get confused about where the cursor glyph is
13329 found. */
13330 if (dy && run.height)
13331 {
13332 update_begin (f);
13333
13334 if (FRAME_WINDOW_P (f))
13335 {
13336 rif->update_window_begin_hook (w);
13337 rif->clear_window_mouse_face (w);
13338 rif->scroll_run_hook (w, &run);
13339 rif->update_window_end_hook (w, 0, 0);
13340 }
13341 else
13342 {
13343 /* Terminal frame. In this case, dvpos gives the number of
13344 lines to scroll by; dvpos < 0 means scroll up. */
13345 int first_unchanged_at_end_vpos
13346 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13347 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13348 int end = (WINDOW_TOP_EDGE_LINE (w)
13349 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13350 + window_internal_height (w));
13351
13352 /* Perform the operation on the screen. */
13353 if (dvpos > 0)
13354 {
13355 /* Scroll last_unchanged_at_beg_row to the end of the
13356 window down dvpos lines. */
13357 set_terminal_window (end);
13358
13359 /* On dumb terminals delete dvpos lines at the end
13360 before inserting dvpos empty lines. */
13361 if (!scroll_region_ok)
13362 ins_del_lines (end - dvpos, -dvpos);
13363
13364 /* Insert dvpos empty lines in front of
13365 last_unchanged_at_beg_row. */
13366 ins_del_lines (from, dvpos);
13367 }
13368 else if (dvpos < 0)
13369 {
13370 /* Scroll up last_unchanged_at_beg_vpos to the end of
13371 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13372 set_terminal_window (end);
13373
13374 /* Delete dvpos lines in front of
13375 last_unchanged_at_beg_vpos. ins_del_lines will set
13376 the cursor to the given vpos and emit |dvpos| delete
13377 line sequences. */
13378 ins_del_lines (from + dvpos, dvpos);
13379
13380 /* On a dumb terminal insert dvpos empty lines at the
13381 end. */
13382 if (!scroll_region_ok)
13383 ins_del_lines (end + dvpos, -dvpos);
13384 }
13385
13386 set_terminal_window (0);
13387 }
13388
13389 update_end (f);
13390 }
13391
13392 /* Shift reused rows of the current matrix to the right position.
13393 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13394 text. */
13395 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13396 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13397 if (dvpos < 0)
13398 {
13399 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13400 bottom_vpos, dvpos);
13401 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13402 bottom_vpos, 0);
13403 }
13404 else if (dvpos > 0)
13405 {
13406 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13407 bottom_vpos, dvpos);
13408 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13409 first_unchanged_at_end_vpos + dvpos, 0);
13410 }
13411
13412 /* For frame-based redisplay, make sure that current frame and window
13413 matrix are in sync with respect to glyph memory. */
13414 if (!FRAME_WINDOW_P (f))
13415 sync_frame_with_window_matrix_rows (w);
13416
13417 /* Adjust buffer positions in reused rows. */
13418 if (delta)
13419 increment_matrix_positions (current_matrix,
13420 first_unchanged_at_end_vpos + dvpos,
13421 bottom_vpos, delta, delta_bytes);
13422
13423 /* Adjust Y positions. */
13424 if (dy)
13425 shift_glyph_matrix (w, current_matrix,
13426 first_unchanged_at_end_vpos + dvpos,
13427 bottom_vpos, dy);
13428
13429 if (first_unchanged_at_end_row)
13430 first_unchanged_at_end_row += dvpos;
13431
13432 /* If scrolling up, there may be some lines to display at the end of
13433 the window. */
13434 last_text_row_at_end = NULL;
13435 if (dy < 0)
13436 {
13437 /* Scrolling up can leave for example a partially visible line
13438 at the end of the window to be redisplayed. */
13439 /* Set last_row to the glyph row in the current matrix where the
13440 window end line is found. It has been moved up or down in
13441 the matrix by dvpos. */
13442 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13443 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13444
13445 /* If last_row is the window end line, it should display text. */
13446 xassert (last_row->displays_text_p);
13447
13448 /* If window end line was partially visible before, begin
13449 displaying at that line. Otherwise begin displaying with the
13450 line following it. */
13451 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13452 {
13453 init_to_row_start (&it, w, last_row);
13454 it.vpos = last_vpos;
13455 it.current_y = last_row->y;
13456 }
13457 else
13458 {
13459 init_to_row_end (&it, w, last_row);
13460 it.vpos = 1 + last_vpos;
13461 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13462 ++last_row;
13463 }
13464
13465 /* We may start in a continuation line. If so, we have to
13466 get the right continuation_lines_width and current_x. */
13467 it.continuation_lines_width = last_row->continuation_lines_width;
13468 it.hpos = it.current_x = 0;
13469
13470 /* Display the rest of the lines at the window end. */
13471 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13472 while (it.current_y < it.last_visible_y
13473 && !fonts_changed_p)
13474 {
13475 /* Is it always sure that the display agrees with lines in
13476 the current matrix? I don't think so, so we mark rows
13477 displayed invalid in the current matrix by setting their
13478 enabled_p flag to zero. */
13479 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13480 if (display_line (&it))
13481 last_text_row_at_end = it.glyph_row - 1;
13482 }
13483 }
13484
13485 /* Update window_end_pos and window_end_vpos. */
13486 if (first_unchanged_at_end_row
13487 && first_unchanged_at_end_row->y < it.last_visible_y
13488 && !last_text_row_at_end)
13489 {
13490 /* Window end line if one of the preserved rows from the current
13491 matrix. Set row to the last row displaying text in current
13492 matrix starting at first_unchanged_at_end_row, after
13493 scrolling. */
13494 xassert (first_unchanged_at_end_row->displays_text_p);
13495 row = find_last_row_displaying_text (w->current_matrix, &it,
13496 first_unchanged_at_end_row);
13497 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13498
13499 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13500 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13501 w->window_end_vpos
13502 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13503 xassert (w->window_end_bytepos >= 0);
13504 IF_DEBUG (debug_method_add (w, "A"));
13505 }
13506 else if (last_text_row_at_end)
13507 {
13508 w->window_end_pos
13509 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13510 w->window_end_bytepos
13511 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13512 w->window_end_vpos
13513 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13514 xassert (w->window_end_bytepos >= 0);
13515 IF_DEBUG (debug_method_add (w, "B"));
13516 }
13517 else if (last_text_row)
13518 {
13519 /* We have displayed either to the end of the window or at the
13520 end of the window, i.e. the last row with text is to be found
13521 in the desired matrix. */
13522 w->window_end_pos
13523 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13524 w->window_end_bytepos
13525 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13526 w->window_end_vpos
13527 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13528 xassert (w->window_end_bytepos >= 0);
13529 }
13530 else if (first_unchanged_at_end_row == NULL
13531 && last_text_row == NULL
13532 && last_text_row_at_end == NULL)
13533 {
13534 /* Displayed to end of window, but no line containing text was
13535 displayed. Lines were deleted at the end of the window. */
13536 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13537 int vpos = XFASTINT (w->window_end_vpos);
13538 struct glyph_row *current_row = current_matrix->rows + vpos;
13539 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13540
13541 for (row = NULL;
13542 row == NULL && vpos >= first_vpos;
13543 --vpos, --current_row, --desired_row)
13544 {
13545 if (desired_row->enabled_p)
13546 {
13547 if (desired_row->displays_text_p)
13548 row = desired_row;
13549 }
13550 else if (current_row->displays_text_p)
13551 row = current_row;
13552 }
13553
13554 xassert (row != NULL);
13555 w->window_end_vpos = make_number (vpos + 1);
13556 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13557 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13558 xassert (w->window_end_bytepos >= 0);
13559 IF_DEBUG (debug_method_add (w, "C"));
13560 }
13561 else
13562 abort ();
13563
13564 #if 0 /* This leads to problems, for instance when the cursor is
13565 at ZV, and the cursor line displays no text. */
13566 /* Disable rows below what's displayed in the window. This makes
13567 debugging easier. */
13568 enable_glyph_matrix_rows (current_matrix,
13569 XFASTINT (w->window_end_vpos) + 1,
13570 bottom_vpos, 0);
13571 #endif
13572
13573 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13574 debug_end_vpos = XFASTINT (w->window_end_vpos));
13575
13576 /* Record that display has not been completed. */
13577 w->window_end_valid = Qnil;
13578 w->desired_matrix->no_scrolling_p = 1;
13579 return 3;
13580
13581 #undef GIVE_UP
13582 }
13583
13584
13585 \f
13586 /***********************************************************************
13587 More debugging support
13588 ***********************************************************************/
13589
13590 #if GLYPH_DEBUG
13591
13592 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13593 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13594 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13595
13596
13597 /* Dump the contents of glyph matrix MATRIX on stderr.
13598
13599 GLYPHS 0 means don't show glyph contents.
13600 GLYPHS 1 means show glyphs in short form
13601 GLYPHS > 1 means show glyphs in long form. */
13602
13603 void
13604 dump_glyph_matrix (matrix, glyphs)
13605 struct glyph_matrix *matrix;
13606 int glyphs;
13607 {
13608 int i;
13609 for (i = 0; i < matrix->nrows; ++i)
13610 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13611 }
13612
13613
13614 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13615 the glyph row and area where the glyph comes from. */
13616
13617 void
13618 dump_glyph (row, glyph, area)
13619 struct glyph_row *row;
13620 struct glyph *glyph;
13621 int area;
13622 {
13623 if (glyph->type == CHAR_GLYPH)
13624 {
13625 fprintf (stderr,
13626 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13627 glyph - row->glyphs[TEXT_AREA],
13628 'C',
13629 glyph->charpos,
13630 (BUFFERP (glyph->object)
13631 ? 'B'
13632 : (STRINGP (glyph->object)
13633 ? 'S'
13634 : '-')),
13635 glyph->pixel_width,
13636 glyph->u.ch,
13637 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13638 ? glyph->u.ch
13639 : '.'),
13640 glyph->face_id,
13641 glyph->left_box_line_p,
13642 glyph->right_box_line_p);
13643 }
13644 else if (glyph->type == STRETCH_GLYPH)
13645 {
13646 fprintf (stderr,
13647 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13648 glyph - row->glyphs[TEXT_AREA],
13649 'S',
13650 glyph->charpos,
13651 (BUFFERP (glyph->object)
13652 ? 'B'
13653 : (STRINGP (glyph->object)
13654 ? 'S'
13655 : '-')),
13656 glyph->pixel_width,
13657 0,
13658 '.',
13659 glyph->face_id,
13660 glyph->left_box_line_p,
13661 glyph->right_box_line_p);
13662 }
13663 else if (glyph->type == IMAGE_GLYPH)
13664 {
13665 fprintf (stderr,
13666 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13667 glyph - row->glyphs[TEXT_AREA],
13668 'I',
13669 glyph->charpos,
13670 (BUFFERP (glyph->object)
13671 ? 'B'
13672 : (STRINGP (glyph->object)
13673 ? 'S'
13674 : '-')),
13675 glyph->pixel_width,
13676 glyph->u.img_id,
13677 '.',
13678 glyph->face_id,
13679 glyph->left_box_line_p,
13680 glyph->right_box_line_p);
13681 }
13682 }
13683
13684
13685 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13686 GLYPHS 0 means don't show glyph contents.
13687 GLYPHS 1 means show glyphs in short form
13688 GLYPHS > 1 means show glyphs in long form. */
13689
13690 void
13691 dump_glyph_row (row, vpos, glyphs)
13692 struct glyph_row *row;
13693 int vpos, glyphs;
13694 {
13695 if (glyphs != 1)
13696 {
13697 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13698 fprintf (stderr, "=======================================================================\n");
13699
13700 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13701 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13702 vpos,
13703 MATRIX_ROW_START_CHARPOS (row),
13704 MATRIX_ROW_END_CHARPOS (row),
13705 row->used[TEXT_AREA],
13706 row->contains_overlapping_glyphs_p,
13707 row->enabled_p,
13708 row->truncated_on_left_p,
13709 row->truncated_on_right_p,
13710 row->overlay_arrow_p,
13711 row->continued_p,
13712 MATRIX_ROW_CONTINUATION_LINE_P (row),
13713 row->displays_text_p,
13714 row->ends_at_zv_p,
13715 row->fill_line_p,
13716 row->ends_in_middle_of_char_p,
13717 row->starts_in_middle_of_char_p,
13718 row->mouse_face_p,
13719 row->x,
13720 row->y,
13721 row->pixel_width,
13722 row->height,
13723 row->visible_height,
13724 row->ascent,
13725 row->phys_ascent);
13726 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13727 row->end.overlay_string_index,
13728 row->continuation_lines_width);
13729 fprintf (stderr, "%9d %5d\n",
13730 CHARPOS (row->start.string_pos),
13731 CHARPOS (row->end.string_pos));
13732 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13733 row->end.dpvec_index);
13734 }
13735
13736 if (glyphs > 1)
13737 {
13738 int area;
13739
13740 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13741 {
13742 struct glyph *glyph = row->glyphs[area];
13743 struct glyph *glyph_end = glyph + row->used[area];
13744
13745 /* Glyph for a line end in text. */
13746 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13747 ++glyph_end;
13748
13749 if (glyph < glyph_end)
13750 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13751
13752 for (; glyph < glyph_end; ++glyph)
13753 dump_glyph (row, glyph, area);
13754 }
13755 }
13756 else if (glyphs == 1)
13757 {
13758 int area;
13759
13760 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13761 {
13762 char *s = (char *) alloca (row->used[area] + 1);
13763 int i;
13764
13765 for (i = 0; i < row->used[area]; ++i)
13766 {
13767 struct glyph *glyph = row->glyphs[area] + i;
13768 if (glyph->type == CHAR_GLYPH
13769 && glyph->u.ch < 0x80
13770 && glyph->u.ch >= ' ')
13771 s[i] = glyph->u.ch;
13772 else
13773 s[i] = '.';
13774 }
13775
13776 s[i] = '\0';
13777 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13778 }
13779 }
13780 }
13781
13782
13783 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13784 Sdump_glyph_matrix, 0, 1, "p",
13785 doc: /* Dump the current matrix of the selected window to stderr.
13786 Shows contents of glyph row structures. With non-nil
13787 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13788 glyphs in short form, otherwise show glyphs in long form. */)
13789 (glyphs)
13790 Lisp_Object glyphs;
13791 {
13792 struct window *w = XWINDOW (selected_window);
13793 struct buffer *buffer = XBUFFER (w->buffer);
13794
13795 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13796 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13797 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13798 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13799 fprintf (stderr, "=============================================\n");
13800 dump_glyph_matrix (w->current_matrix,
13801 NILP (glyphs) ? 0 : XINT (glyphs));
13802 return Qnil;
13803 }
13804
13805
13806 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13807 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13808 ()
13809 {
13810 struct frame *f = XFRAME (selected_frame);
13811 dump_glyph_matrix (f->current_matrix, 1);
13812 return Qnil;
13813 }
13814
13815
13816 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13817 doc: /* Dump glyph row ROW 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 glyph_matrix *matrix;
13825 int vpos;
13826
13827 CHECK_NUMBER (row);
13828 matrix = XWINDOW (selected_window)->current_matrix;
13829 vpos = XINT (row);
13830 if (vpos >= 0 && vpos < matrix->nrows)
13831 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13832 vpos,
13833 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13834 return Qnil;
13835 }
13836
13837
13838 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13839 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13840 GLYPH 0 means don't dump glyphs.
13841 GLYPH 1 means dump glyphs in short form.
13842 GLYPH > 1 or omitted means dump glyphs in long form. */)
13843 (row, glyphs)
13844 Lisp_Object row, glyphs;
13845 {
13846 struct frame *sf = SELECTED_FRAME ();
13847 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13848 int vpos;
13849
13850 CHECK_NUMBER (row);
13851 vpos = XINT (row);
13852 if (vpos >= 0 && vpos < m->nrows)
13853 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13854 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13855 return Qnil;
13856 }
13857
13858
13859 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13860 doc: /* Toggle tracing of redisplay.
13861 With ARG, turn tracing on if and only if ARG is positive. */)
13862 (arg)
13863 Lisp_Object arg;
13864 {
13865 if (NILP (arg))
13866 trace_redisplay_p = !trace_redisplay_p;
13867 else
13868 {
13869 arg = Fprefix_numeric_value (arg);
13870 trace_redisplay_p = XINT (arg) > 0;
13871 }
13872
13873 return Qnil;
13874 }
13875
13876
13877 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13878 doc: /* Like `format', but print result to stderr.
13879 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13880 (nargs, args)
13881 int nargs;
13882 Lisp_Object *args;
13883 {
13884 Lisp_Object s = Fformat (nargs, args);
13885 fprintf (stderr, "%s", SDATA (s));
13886 return Qnil;
13887 }
13888
13889 #endif /* GLYPH_DEBUG */
13890
13891
13892 \f
13893 /***********************************************************************
13894 Building Desired Matrix Rows
13895 ***********************************************************************/
13896
13897 /* Return a temporary glyph row holding the glyphs of an overlay
13898 arrow. Only used for non-window-redisplay windows. */
13899
13900 static struct glyph_row *
13901 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
13902 struct window *w;
13903 Lisp_Object overlay_arrow_string;
13904 {
13905 struct frame *f = XFRAME (WINDOW_FRAME (w));
13906 struct buffer *buffer = XBUFFER (w->buffer);
13907 struct buffer *old = current_buffer;
13908 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13909 int arrow_len = SCHARS (overlay_arrow_string);
13910 const unsigned char *arrow_end = arrow_string + arrow_len;
13911 const unsigned char *p;
13912 struct it it;
13913 int multibyte_p;
13914 int n_glyphs_before;
13915
13916 set_buffer_temp (buffer);
13917 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13918 it.glyph_row->used[TEXT_AREA] = 0;
13919 SET_TEXT_POS (it.position, 0, 0);
13920
13921 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13922 p = arrow_string;
13923 while (p < arrow_end)
13924 {
13925 Lisp_Object face, ilisp;
13926
13927 /* Get the next character. */
13928 if (multibyte_p)
13929 it.c = string_char_and_length (p, arrow_len, &it.len);
13930 else
13931 it.c = *p, it.len = 1;
13932 p += it.len;
13933
13934 /* Get its face. */
13935 ilisp = make_number (p - arrow_string);
13936 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
13937 it.face_id = compute_char_face (f, it.c, face);
13938
13939 /* Compute its width, get its glyphs. */
13940 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13941 SET_TEXT_POS (it.position, -1, -1);
13942 PRODUCE_GLYPHS (&it);
13943
13944 /* If this character doesn't fit any more in the line, we have
13945 to remove some glyphs. */
13946 if (it.current_x > it.last_visible_x)
13947 {
13948 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13949 break;
13950 }
13951 }
13952
13953 set_buffer_temp (old);
13954 return it.glyph_row;
13955 }
13956
13957
13958 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13959 glyphs are only inserted for terminal frames since we can't really
13960 win with truncation glyphs when partially visible glyphs are
13961 involved. Which glyphs to insert is determined by
13962 produce_special_glyphs. */
13963
13964 static void
13965 insert_left_trunc_glyphs (it)
13966 struct it *it;
13967 {
13968 struct it truncate_it;
13969 struct glyph *from, *end, *to, *toend;
13970
13971 xassert (!FRAME_WINDOW_P (it->f));
13972
13973 /* Get the truncation glyphs. */
13974 truncate_it = *it;
13975 truncate_it.current_x = 0;
13976 truncate_it.face_id = DEFAULT_FACE_ID;
13977 truncate_it.glyph_row = &scratch_glyph_row;
13978 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13979 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13980 truncate_it.object = make_number (0);
13981 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13982
13983 /* Overwrite glyphs from IT with truncation glyphs. */
13984 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13985 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13986 to = it->glyph_row->glyphs[TEXT_AREA];
13987 toend = to + it->glyph_row->used[TEXT_AREA];
13988
13989 while (from < end)
13990 *to++ = *from++;
13991
13992 /* There may be padding glyphs left over. Overwrite them too. */
13993 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13994 {
13995 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13996 while (from < end)
13997 *to++ = *from++;
13998 }
13999
14000 if (to > toend)
14001 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14002 }
14003
14004
14005 /* Compute the pixel height and width of IT->glyph_row.
14006
14007 Most of the time, ascent and height of a display line will be equal
14008 to the max_ascent and max_height values of the display iterator
14009 structure. This is not the case if
14010
14011 1. We hit ZV without displaying anything. In this case, max_ascent
14012 and max_height will be zero.
14013
14014 2. We have some glyphs that don't contribute to the line height.
14015 (The glyph row flag contributes_to_line_height_p is for future
14016 pixmap extensions).
14017
14018 The first case is easily covered by using default values because in
14019 these cases, the line height does not really matter, except that it
14020 must not be zero. */
14021
14022 static void
14023 compute_line_metrics (it)
14024 struct it *it;
14025 {
14026 struct glyph_row *row = it->glyph_row;
14027 int area, i;
14028
14029 if (FRAME_WINDOW_P (it->f))
14030 {
14031 int i, min_y, max_y;
14032
14033 /* The line may consist of one space only, that was added to
14034 place the cursor on it. If so, the row's height hasn't been
14035 computed yet. */
14036 if (row->height == 0)
14037 {
14038 if (it->max_ascent + it->max_descent == 0)
14039 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14040 row->ascent = it->max_ascent;
14041 row->height = it->max_ascent + it->max_descent;
14042 row->phys_ascent = it->max_phys_ascent;
14043 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14044 }
14045
14046 /* Compute the width of this line. */
14047 row->pixel_width = row->x;
14048 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14049 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14050
14051 xassert (row->pixel_width >= 0);
14052 xassert (row->ascent >= 0 && row->height > 0);
14053
14054 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14055 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14056
14057 /* If first line's physical ascent is larger than its logical
14058 ascent, use the physical ascent, and make the row taller.
14059 This makes accented characters fully visible. */
14060 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14061 && row->phys_ascent > row->ascent)
14062 {
14063 row->height += row->phys_ascent - row->ascent;
14064 row->ascent = row->phys_ascent;
14065 }
14066
14067 /* Compute how much of the line is visible. */
14068 row->visible_height = row->height;
14069
14070 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14071 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14072
14073 if (row->y < min_y)
14074 row->visible_height -= min_y - row->y;
14075 if (row->y + row->height > max_y)
14076 row->visible_height -= row->y + row->height - max_y;
14077 }
14078 else
14079 {
14080 row->pixel_width = row->used[TEXT_AREA];
14081 if (row->continued_p)
14082 row->pixel_width -= it->continuation_pixel_width;
14083 else if (row->truncated_on_right_p)
14084 row->pixel_width -= it->truncation_pixel_width;
14085 row->ascent = row->phys_ascent = 0;
14086 row->height = row->phys_height = row->visible_height = 1;
14087 }
14088
14089 /* Compute a hash code for this row. */
14090 row->hash = 0;
14091 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14092 for (i = 0; i < row->used[area]; ++i)
14093 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14094 + row->glyphs[area][i].u.val
14095 + row->glyphs[area][i].face_id
14096 + row->glyphs[area][i].padding_p
14097 + (row->glyphs[area][i].type << 2));
14098
14099 it->max_ascent = it->max_descent = 0;
14100 it->max_phys_ascent = it->max_phys_descent = 0;
14101 }
14102
14103
14104 /* Append one space to the glyph row of iterator IT if doing a
14105 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14106 space have the default face, otherwise let it have the same face as
14107 IT->face_id. Value is non-zero if a space was added.
14108
14109 This function is called to make sure that there is always one glyph
14110 at the end of a glyph row that the cursor can be set on under
14111 window-systems. (If there weren't such a glyph we would not know
14112 how wide and tall a box cursor should be displayed).
14113
14114 At the same time this space let's a nicely handle clearing to the
14115 end of the line if the row ends in italic text. */
14116
14117 static int
14118 append_space (it, default_face_p)
14119 struct it *it;
14120 int default_face_p;
14121 {
14122 if (FRAME_WINDOW_P (it->f))
14123 {
14124 int n = it->glyph_row->used[TEXT_AREA];
14125
14126 if (it->glyph_row->glyphs[TEXT_AREA] + n
14127 < it->glyph_row->glyphs[1 + TEXT_AREA])
14128 {
14129 /* Save some values that must not be changed.
14130 Must save IT->c and IT->len because otherwise
14131 ITERATOR_AT_END_P wouldn't work anymore after
14132 append_space has been called. */
14133 enum display_element_type saved_what = it->what;
14134 int saved_c = it->c, saved_len = it->len;
14135 int saved_x = it->current_x;
14136 int saved_face_id = it->face_id;
14137 struct text_pos saved_pos;
14138 Lisp_Object saved_object;
14139 struct face *face;
14140
14141 saved_object = it->object;
14142 saved_pos = it->position;
14143
14144 it->what = IT_CHARACTER;
14145 bzero (&it->position, sizeof it->position);
14146 it->object = make_number (0);
14147 it->c = ' ';
14148 it->len = 1;
14149
14150 if (default_face_p)
14151 it->face_id = DEFAULT_FACE_ID;
14152 else if (it->face_before_selective_p)
14153 it->face_id = it->saved_face_id;
14154 face = FACE_FROM_ID (it->f, it->face_id);
14155 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14156
14157 PRODUCE_GLYPHS (it);
14158
14159 it->current_x = saved_x;
14160 it->object = saved_object;
14161 it->position = saved_pos;
14162 it->what = saved_what;
14163 it->face_id = saved_face_id;
14164 it->len = saved_len;
14165 it->c = saved_c;
14166 return 1;
14167 }
14168 }
14169
14170 return 0;
14171 }
14172
14173
14174 /* Extend the face of the last glyph in the text area of IT->glyph_row
14175 to the end of the display line. Called from display_line.
14176 If the glyph row is empty, add a space glyph to it so that we
14177 know the face to draw. Set the glyph row flag fill_line_p. */
14178
14179 static void
14180 extend_face_to_end_of_line (it)
14181 struct it *it;
14182 {
14183 struct face *face;
14184 struct frame *f = it->f;
14185
14186 /* If line is already filled, do nothing. */
14187 if (it->current_x >= it->last_visible_x)
14188 return;
14189
14190 /* Face extension extends the background and box of IT->face_id
14191 to the end of the line. If the background equals the background
14192 of the frame, we don't have to do anything. */
14193 if (it->face_before_selective_p)
14194 face = FACE_FROM_ID (it->f, it->saved_face_id);
14195 else
14196 face = FACE_FROM_ID (f, it->face_id);
14197
14198 if (FRAME_WINDOW_P (f)
14199 && face->box == FACE_NO_BOX
14200 && face->background == FRAME_BACKGROUND_PIXEL (f)
14201 && !face->stipple)
14202 return;
14203
14204 /* Set the glyph row flag indicating that the face of the last glyph
14205 in the text area has to be drawn to the end of the text area. */
14206 it->glyph_row->fill_line_p = 1;
14207
14208 /* If current character of IT is not ASCII, make sure we have the
14209 ASCII face. This will be automatically undone the next time
14210 get_next_display_element returns a multibyte character. Note
14211 that the character will always be single byte in unibyte text. */
14212 if (!SINGLE_BYTE_CHAR_P (it->c))
14213 {
14214 it->face_id = FACE_FOR_CHAR (f, face, 0);
14215 }
14216
14217 if (FRAME_WINDOW_P (f))
14218 {
14219 /* If the row is empty, add a space with the current face of IT,
14220 so that we know which face to draw. */
14221 if (it->glyph_row->used[TEXT_AREA] == 0)
14222 {
14223 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14224 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14225 it->glyph_row->used[TEXT_AREA] = 1;
14226 }
14227 }
14228 else
14229 {
14230 /* Save some values that must not be changed. */
14231 int saved_x = it->current_x;
14232 struct text_pos saved_pos;
14233 Lisp_Object saved_object;
14234 enum display_element_type saved_what = it->what;
14235 int saved_face_id = it->face_id;
14236
14237 saved_object = it->object;
14238 saved_pos = it->position;
14239
14240 it->what = IT_CHARACTER;
14241 bzero (&it->position, sizeof it->position);
14242 it->object = make_number (0);
14243 it->c = ' ';
14244 it->len = 1;
14245 it->face_id = face->id;
14246
14247 PRODUCE_GLYPHS (it);
14248
14249 while (it->current_x <= it->last_visible_x)
14250 PRODUCE_GLYPHS (it);
14251
14252 /* Don't count these blanks really. It would let us insert a left
14253 truncation glyph below and make us set the cursor on them, maybe. */
14254 it->current_x = saved_x;
14255 it->object = saved_object;
14256 it->position = saved_pos;
14257 it->what = saved_what;
14258 it->face_id = saved_face_id;
14259 }
14260 }
14261
14262
14263 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14264 trailing whitespace. */
14265
14266 static int
14267 trailing_whitespace_p (charpos)
14268 int charpos;
14269 {
14270 int bytepos = CHAR_TO_BYTE (charpos);
14271 int c = 0;
14272
14273 while (bytepos < ZV_BYTE
14274 && (c = FETCH_CHAR (bytepos),
14275 c == ' ' || c == '\t'))
14276 ++bytepos;
14277
14278 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14279 {
14280 if (bytepos != PT_BYTE)
14281 return 1;
14282 }
14283 return 0;
14284 }
14285
14286
14287 /* Highlight trailing whitespace, if any, in ROW. */
14288
14289 void
14290 highlight_trailing_whitespace (f, row)
14291 struct frame *f;
14292 struct glyph_row *row;
14293 {
14294 int used = row->used[TEXT_AREA];
14295
14296 if (used)
14297 {
14298 struct glyph *start = row->glyphs[TEXT_AREA];
14299 struct glyph *glyph = start + used - 1;
14300
14301 /* Skip over glyphs inserted to display the cursor at the
14302 end of a line, for extending the face of the last glyph
14303 to the end of the line on terminals, and for truncation
14304 and continuation glyphs. */
14305 while (glyph >= start
14306 && glyph->type == CHAR_GLYPH
14307 && INTEGERP (glyph->object))
14308 --glyph;
14309
14310 /* If last glyph is a space or stretch, and it's trailing
14311 whitespace, set the face of all trailing whitespace glyphs in
14312 IT->glyph_row to `trailing-whitespace'. */
14313 if (glyph >= start
14314 && BUFFERP (glyph->object)
14315 && (glyph->type == STRETCH_GLYPH
14316 || (glyph->type == CHAR_GLYPH
14317 && glyph->u.ch == ' '))
14318 && trailing_whitespace_p (glyph->charpos))
14319 {
14320 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14321
14322 while (glyph >= start
14323 && BUFFERP (glyph->object)
14324 && (glyph->type == STRETCH_GLYPH
14325 || (glyph->type == CHAR_GLYPH
14326 && glyph->u.ch == ' ')))
14327 (glyph--)->face_id = face_id;
14328 }
14329 }
14330 }
14331
14332
14333 /* Value is non-zero if glyph row ROW in window W should be
14334 used to hold the cursor. */
14335
14336 static int
14337 cursor_row_p (w, row)
14338 struct window *w;
14339 struct glyph_row *row;
14340 {
14341 int cursor_row_p = 1;
14342
14343 if (PT == MATRIX_ROW_END_CHARPOS (row))
14344 {
14345 /* If the row ends with a newline from a string, we don't want
14346 the cursor there (if the row is continued it doesn't end in a
14347 newline). */
14348 if (CHARPOS (row->end.string_pos) >= 0
14349 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14350 cursor_row_p = row->continued_p;
14351
14352 /* If the row ends at ZV, display the cursor at the end of that
14353 row instead of at the start of the row below. */
14354 else if (row->ends_at_zv_p)
14355 cursor_row_p = 1;
14356 else
14357 cursor_row_p = 0;
14358 }
14359
14360 return cursor_row_p;
14361 }
14362
14363
14364 /* Construct the glyph row IT->glyph_row in the desired matrix of
14365 IT->w from text at the current position of IT. See dispextern.h
14366 for an overview of struct it. Value is non-zero if
14367 IT->glyph_row displays text, as opposed to a line displaying ZV
14368 only. */
14369
14370 static int
14371 display_line (it)
14372 struct it *it;
14373 {
14374 struct glyph_row *row = it->glyph_row;
14375 int overlay_arrow_bitmap;
14376 Lisp_Object overlay_arrow_string;
14377
14378 /* We always start displaying at hpos zero even if hscrolled. */
14379 xassert (it->hpos == 0 && it->current_x == 0);
14380
14381 /* We must not display in a row that's not a text row. */
14382 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14383 < it->w->desired_matrix->nrows);
14384
14385 /* Is IT->w showing the region? */
14386 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14387
14388 /* Clear the result glyph row and enable it. */
14389 prepare_desired_row (row);
14390
14391 row->y = it->current_y;
14392 row->start = it->start;
14393 row->continuation_lines_width = it->continuation_lines_width;
14394 row->displays_text_p = 1;
14395 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14396 it->starts_in_middle_of_char_p = 0;
14397
14398 /* Arrange the overlays nicely for our purposes. Usually, we call
14399 display_line on only one line at a time, in which case this
14400 can't really hurt too much, or we call it on lines which appear
14401 one after another in the buffer, in which case all calls to
14402 recenter_overlay_lists but the first will be pretty cheap. */
14403 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14404
14405 /* Move over display elements that are not visible because we are
14406 hscrolled. This may stop at an x-position < IT->first_visible_x
14407 if the first glyph is partially visible or if we hit a line end. */
14408 if (it->current_x < it->first_visible_x)
14409 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14410 MOVE_TO_POS | MOVE_TO_X);
14411
14412 /* Get the initial row height. This is either the height of the
14413 text hscrolled, if there is any, or zero. */
14414 row->ascent = it->max_ascent;
14415 row->height = it->max_ascent + it->max_descent;
14416 row->phys_ascent = it->max_phys_ascent;
14417 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14418
14419 /* Loop generating characters. The loop is left with IT on the next
14420 character to display. */
14421 while (1)
14422 {
14423 int n_glyphs_before, hpos_before, x_before;
14424 int x, i, nglyphs;
14425 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14426
14427 /* Retrieve the next thing to display. Value is zero if end of
14428 buffer reached. */
14429 if (!get_next_display_element (it))
14430 {
14431 /* Maybe add a space at the end of this line that is used to
14432 display the cursor there under X. Set the charpos of the
14433 first glyph of blank lines not corresponding to any text
14434 to -1. */
14435 #ifdef HAVE_WINDOW_SYSTEM
14436 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14437 row->exact_window_width_line_p = 1;
14438 else
14439 #endif /* HAVE_WINDOW_SYSTEM */
14440 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14441 || row->used[TEXT_AREA] == 0)
14442 {
14443 row->glyphs[TEXT_AREA]->charpos = -1;
14444 row->displays_text_p = 0;
14445
14446 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14447 && (!MINI_WINDOW_P (it->w)
14448 || (minibuf_level && EQ (it->window, minibuf_window))))
14449 row->indicate_empty_line_p = 1;
14450 }
14451
14452 it->continuation_lines_width = 0;
14453 row->ends_at_zv_p = 1;
14454 break;
14455 }
14456
14457 /* Now, get the metrics of what we want to display. This also
14458 generates glyphs in `row' (which is IT->glyph_row). */
14459 n_glyphs_before = row->used[TEXT_AREA];
14460 x = it->current_x;
14461
14462 /* Remember the line height so far in case the next element doesn't
14463 fit on the line. */
14464 if (!it->truncate_lines_p)
14465 {
14466 ascent = it->max_ascent;
14467 descent = it->max_descent;
14468 phys_ascent = it->max_phys_ascent;
14469 phys_descent = it->max_phys_descent;
14470 }
14471
14472 PRODUCE_GLYPHS (it);
14473
14474 /* If this display element was in marginal areas, continue with
14475 the next one. */
14476 if (it->area != TEXT_AREA)
14477 {
14478 row->ascent = max (row->ascent, it->max_ascent);
14479 row->height = max (row->height, it->max_ascent + it->max_descent);
14480 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14481 row->phys_height = max (row->phys_height,
14482 it->max_phys_ascent + it->max_phys_descent);
14483 set_iterator_to_next (it, 1);
14484 continue;
14485 }
14486
14487 /* Does the display element fit on the line? If we truncate
14488 lines, we should draw past the right edge of the window. If
14489 we don't truncate, we want to stop so that we can display the
14490 continuation glyph before the right margin. If lines are
14491 continued, there are two possible strategies for characters
14492 resulting in more than 1 glyph (e.g. tabs): Display as many
14493 glyphs as possible in this line and leave the rest for the
14494 continuation line, or display the whole element in the next
14495 line. Original redisplay did the former, so we do it also. */
14496 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14497 hpos_before = it->hpos;
14498 x_before = x;
14499
14500 if (/* Not a newline. */
14501 nglyphs > 0
14502 /* Glyphs produced fit entirely in the line. */
14503 && it->current_x < it->last_visible_x)
14504 {
14505 it->hpos += nglyphs;
14506 row->ascent = max (row->ascent, it->max_ascent);
14507 row->height = max (row->height, it->max_ascent + it->max_descent);
14508 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14509 row->phys_height = max (row->phys_height,
14510 it->max_phys_ascent + it->max_phys_descent);
14511 if (it->current_x - it->pixel_width < it->first_visible_x)
14512 row->x = x - it->first_visible_x;
14513 }
14514 else
14515 {
14516 int new_x;
14517 struct glyph *glyph;
14518
14519 for (i = 0; i < nglyphs; ++i, x = new_x)
14520 {
14521 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14522 new_x = x + glyph->pixel_width;
14523
14524 if (/* Lines are continued. */
14525 !it->truncate_lines_p
14526 && (/* Glyph doesn't fit on the line. */
14527 new_x > it->last_visible_x
14528 /* Or it fits exactly on a window system frame. */
14529 || (new_x == it->last_visible_x
14530 && FRAME_WINDOW_P (it->f))))
14531 {
14532 /* End of a continued line. */
14533
14534 if (it->hpos == 0
14535 || (new_x == it->last_visible_x
14536 && FRAME_WINDOW_P (it->f)))
14537 {
14538 /* Current glyph is the only one on the line or
14539 fits exactly on the line. We must continue
14540 the line because we can't draw the cursor
14541 after the glyph. */
14542 row->continued_p = 1;
14543 it->current_x = new_x;
14544 it->continuation_lines_width += new_x;
14545 ++it->hpos;
14546 if (i == nglyphs - 1)
14547 {
14548 set_iterator_to_next (it, 1);
14549 #ifdef HAVE_WINDOW_SYSTEM
14550 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14551 {
14552 if (!get_next_display_element (it))
14553 {
14554 row->exact_window_width_line_p = 1;
14555 it->continuation_lines_width = 0;
14556 row->continued_p = 0;
14557 row->ends_at_zv_p = 1;
14558 }
14559 else if (ITERATOR_AT_END_OF_LINE_P (it))
14560 {
14561 row->continued_p = 0;
14562 row->exact_window_width_line_p = 1;
14563 }
14564 }
14565 #endif /* HAVE_WINDOW_SYSTEM */
14566 }
14567 }
14568 else if (CHAR_GLYPH_PADDING_P (*glyph)
14569 && !FRAME_WINDOW_P (it->f))
14570 {
14571 /* A padding glyph that doesn't fit on this line.
14572 This means the whole character doesn't fit
14573 on the line. */
14574 row->used[TEXT_AREA] = n_glyphs_before;
14575
14576 /* Fill the rest of the row with continuation
14577 glyphs like in 20.x. */
14578 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14579 < row->glyphs[1 + TEXT_AREA])
14580 produce_special_glyphs (it, IT_CONTINUATION);
14581
14582 row->continued_p = 1;
14583 it->current_x = x_before;
14584 it->continuation_lines_width += x_before;
14585
14586 /* Restore the height to what it was before the
14587 element not fitting on the line. */
14588 it->max_ascent = ascent;
14589 it->max_descent = descent;
14590 it->max_phys_ascent = phys_ascent;
14591 it->max_phys_descent = phys_descent;
14592 }
14593 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14594 {
14595 /* A TAB that extends past the right edge of the
14596 window. This produces a single glyph on
14597 window system frames. We leave the glyph in
14598 this row and let it fill the row, but don't
14599 consume the TAB. */
14600 it->continuation_lines_width += it->last_visible_x;
14601 row->ends_in_middle_of_char_p = 1;
14602 row->continued_p = 1;
14603 glyph->pixel_width = it->last_visible_x - x;
14604 it->starts_in_middle_of_char_p = 1;
14605 }
14606 else
14607 {
14608 /* Something other than a TAB that draws past
14609 the right edge of the window. Restore
14610 positions to values before the element. */
14611 row->used[TEXT_AREA] = n_glyphs_before + i;
14612
14613 /* Display continuation glyphs. */
14614 if (!FRAME_WINDOW_P (it->f))
14615 produce_special_glyphs (it, IT_CONTINUATION);
14616 row->continued_p = 1;
14617
14618 it->continuation_lines_width += x;
14619
14620 if (nglyphs > 1 && i > 0)
14621 {
14622 row->ends_in_middle_of_char_p = 1;
14623 it->starts_in_middle_of_char_p = 1;
14624 }
14625
14626 /* Restore the height to what it was before the
14627 element not fitting on the line. */
14628 it->max_ascent = ascent;
14629 it->max_descent = descent;
14630 it->max_phys_ascent = phys_ascent;
14631 it->max_phys_descent = phys_descent;
14632 }
14633
14634 break;
14635 }
14636 else if (new_x > it->first_visible_x)
14637 {
14638 /* Increment number of glyphs actually displayed. */
14639 ++it->hpos;
14640
14641 if (x < it->first_visible_x)
14642 /* Glyph is partially visible, i.e. row starts at
14643 negative X position. */
14644 row->x = x - it->first_visible_x;
14645 }
14646 else
14647 {
14648 /* Glyph is completely off the left margin of the
14649 window. This should not happen because of the
14650 move_it_in_display_line at the start of this
14651 function, unless the text display area of the
14652 window is empty. */
14653 xassert (it->first_visible_x <= it->last_visible_x);
14654 }
14655 }
14656
14657 row->ascent = max (row->ascent, it->max_ascent);
14658 row->height = max (row->height, it->max_ascent + it->max_descent);
14659 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14660 row->phys_height = max (row->phys_height,
14661 it->max_phys_ascent + it->max_phys_descent);
14662
14663 /* End of this display line if row is continued. */
14664 if (row->continued_p || row->ends_at_zv_p)
14665 break;
14666 }
14667
14668 at_end_of_line:
14669 /* Is this a line end? If yes, we're also done, after making
14670 sure that a non-default face is extended up to the right
14671 margin of the window. */
14672 if (ITERATOR_AT_END_OF_LINE_P (it))
14673 {
14674 int used_before = row->used[TEXT_AREA];
14675
14676 row->ends_in_newline_from_string_p = STRINGP (it->object);
14677
14678 #ifdef HAVE_WINDOW_SYSTEM
14679 /* Add a space at the end of the line that is used to
14680 display the cursor there. */
14681 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14682 append_space (it, 0);
14683 #endif /* HAVE_WINDOW_SYSTEM */
14684
14685 /* Extend the face to the end of the line. */
14686 extend_face_to_end_of_line (it);
14687
14688 /* Make sure we have the position. */
14689 if (used_before == 0)
14690 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14691
14692 /* Consume the line end. This skips over invisible lines. */
14693 set_iterator_to_next (it, 1);
14694 it->continuation_lines_width = 0;
14695 break;
14696 }
14697
14698 /* Proceed with next display element. Note that this skips
14699 over lines invisible because of selective display. */
14700 set_iterator_to_next (it, 1);
14701
14702 /* If we truncate lines, we are done when the last displayed
14703 glyphs reach past the right margin of the window. */
14704 if (it->truncate_lines_p
14705 && (FRAME_WINDOW_P (it->f)
14706 ? (it->current_x >= it->last_visible_x)
14707 : (it->current_x > it->last_visible_x)))
14708 {
14709 /* Maybe add truncation glyphs. */
14710 if (!FRAME_WINDOW_P (it->f))
14711 {
14712 int i, n;
14713
14714 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14715 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14716 break;
14717
14718 for (n = row->used[TEXT_AREA]; i < n; ++i)
14719 {
14720 row->used[TEXT_AREA] = i;
14721 produce_special_glyphs (it, IT_TRUNCATION);
14722 }
14723 }
14724 #ifdef HAVE_WINDOW_SYSTEM
14725 else
14726 {
14727 /* Don't truncate if we can overflow newline into fringe. */
14728 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14729 {
14730 if (!get_next_display_element (it))
14731 {
14732 #ifdef HAVE_WINDOW_SYSTEM
14733 it->continuation_lines_width = 0;
14734 row->ends_at_zv_p = 1;
14735 row->exact_window_width_line_p = 1;
14736 break;
14737 #endif /* HAVE_WINDOW_SYSTEM */
14738 }
14739 if (ITERATOR_AT_END_OF_LINE_P (it))
14740 {
14741 row->exact_window_width_line_p = 1;
14742 goto at_end_of_line;
14743 }
14744 }
14745 }
14746 #endif /* HAVE_WINDOW_SYSTEM */
14747
14748 row->truncated_on_right_p = 1;
14749 it->continuation_lines_width = 0;
14750 reseat_at_next_visible_line_start (it, 0);
14751 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14752 it->hpos = hpos_before;
14753 it->current_x = x_before;
14754 break;
14755 }
14756 }
14757
14758 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14759 at the left window margin. */
14760 if (it->first_visible_x
14761 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14762 {
14763 if (!FRAME_WINDOW_P (it->f))
14764 insert_left_trunc_glyphs (it);
14765 row->truncated_on_left_p = 1;
14766 }
14767
14768 /* If the start of this line is the overlay arrow-position, then
14769 mark this glyph row as the one containing the overlay arrow.
14770 This is clearly a mess with variable size fonts. It would be
14771 better to let it be displayed like cursors under X. */
14772 if (! overlay_arrow_seen
14773 && (overlay_arrow_string
14774 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14775 !NILP (overlay_arrow_string)))
14776 {
14777 /* Overlay arrow in window redisplay is a fringe bitmap. */
14778 if (!FRAME_WINDOW_P (it->f))
14779 {
14780 struct glyph_row *arrow_row
14781 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14782 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14783 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14784 struct glyph *p = row->glyphs[TEXT_AREA];
14785 struct glyph *p2, *end;
14786
14787 /* Copy the arrow glyphs. */
14788 while (glyph < arrow_end)
14789 *p++ = *glyph++;
14790
14791 /* Throw away padding glyphs. */
14792 p2 = p;
14793 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14794 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14795 ++p2;
14796 if (p2 > p)
14797 {
14798 while (p2 < end)
14799 *p++ = *p2++;
14800 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14801 }
14802 }
14803
14804 overlay_arrow_seen = 1;
14805 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14806 row->overlay_arrow_p = 1;
14807 }
14808
14809 /* Compute pixel dimensions of this line. */
14810 compute_line_metrics (it);
14811
14812 /* Remember the position at which this line ends. */
14813 row->end = it->current;
14814
14815 /* Save fringe bitmaps in this row. */
14816 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14817 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14818 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14819 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14820
14821 it->left_user_fringe_bitmap = 0;
14822 it->left_user_fringe_face_id = 0;
14823 it->right_user_fringe_bitmap = 0;
14824 it->right_user_fringe_face_id = 0;
14825
14826 /* Maybe set the cursor. */
14827 if (it->w->cursor.vpos < 0
14828 && PT >= MATRIX_ROW_START_CHARPOS (row)
14829 && PT <= MATRIX_ROW_END_CHARPOS (row)
14830 && cursor_row_p (it->w, row))
14831 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14832
14833 /* Highlight trailing whitespace. */
14834 if (!NILP (Vshow_trailing_whitespace))
14835 highlight_trailing_whitespace (it->f, it->glyph_row);
14836
14837 /* Prepare for the next line. This line starts horizontally at (X
14838 HPOS) = (0 0). Vertical positions are incremented. As a
14839 convenience for the caller, IT->glyph_row is set to the next
14840 row to be used. */
14841 it->current_x = it->hpos = 0;
14842 it->current_y += row->height;
14843 ++it->vpos;
14844 ++it->glyph_row;
14845 it->start = it->current;
14846 return row->displays_text_p;
14847 }
14848
14849
14850 \f
14851 /***********************************************************************
14852 Menu Bar
14853 ***********************************************************************/
14854
14855 /* Redisplay the menu bar in the frame for window W.
14856
14857 The menu bar of X frames that don't have X toolkit support is
14858 displayed in a special window W->frame->menu_bar_window.
14859
14860 The menu bar of terminal frames is treated specially as far as
14861 glyph matrices are concerned. Menu bar lines are not part of
14862 windows, so the update is done directly on the frame matrix rows
14863 for the menu bar. */
14864
14865 static void
14866 display_menu_bar (w)
14867 struct window *w;
14868 {
14869 struct frame *f = XFRAME (WINDOW_FRAME (w));
14870 struct it it;
14871 Lisp_Object items;
14872 int i;
14873
14874 /* Don't do all this for graphical frames. */
14875 #ifdef HAVE_NTGUI
14876 if (!NILP (Vwindow_system))
14877 return;
14878 #endif
14879 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14880 if (FRAME_X_P (f))
14881 return;
14882 #endif
14883 #ifdef MAC_OS
14884 if (FRAME_MAC_P (f))
14885 return;
14886 #endif
14887
14888 #ifdef USE_X_TOOLKIT
14889 xassert (!FRAME_WINDOW_P (f));
14890 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14891 it.first_visible_x = 0;
14892 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14893 #else /* not USE_X_TOOLKIT */
14894 if (FRAME_WINDOW_P (f))
14895 {
14896 /* Menu bar lines are displayed in the desired matrix of the
14897 dummy window menu_bar_window. */
14898 struct window *menu_w;
14899 xassert (WINDOWP (f->menu_bar_window));
14900 menu_w = XWINDOW (f->menu_bar_window);
14901 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14902 MENU_FACE_ID);
14903 it.first_visible_x = 0;
14904 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14905 }
14906 else
14907 {
14908 /* This is a TTY frame, i.e. character hpos/vpos are used as
14909 pixel x/y. */
14910 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14911 MENU_FACE_ID);
14912 it.first_visible_x = 0;
14913 it.last_visible_x = FRAME_COLS (f);
14914 }
14915 #endif /* not USE_X_TOOLKIT */
14916
14917 if (! mode_line_inverse_video)
14918 /* Force the menu-bar to be displayed in the default face. */
14919 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14920
14921 /* Clear all rows of the menu bar. */
14922 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14923 {
14924 struct glyph_row *row = it.glyph_row + i;
14925 clear_glyph_row (row);
14926 row->enabled_p = 1;
14927 row->full_width_p = 1;
14928 }
14929
14930 /* Display all items of the menu bar. */
14931 items = FRAME_MENU_BAR_ITEMS (it.f);
14932 for (i = 0; i < XVECTOR (items)->size; i += 4)
14933 {
14934 Lisp_Object string;
14935
14936 /* Stop at nil string. */
14937 string = AREF (items, i + 1);
14938 if (NILP (string))
14939 break;
14940
14941 /* Remember where item was displayed. */
14942 AREF (items, i + 3) = make_number (it.hpos);
14943
14944 /* Display the item, pad with one space. */
14945 if (it.current_x < it.last_visible_x)
14946 display_string (NULL, string, Qnil, 0, 0, &it,
14947 SCHARS (string) + 1, 0, 0, -1);
14948 }
14949
14950 /* Fill out the line with spaces. */
14951 if (it.current_x < it.last_visible_x)
14952 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14953
14954 /* Compute the total height of the lines. */
14955 compute_line_metrics (&it);
14956 }
14957
14958
14959 \f
14960 /***********************************************************************
14961 Mode Line
14962 ***********************************************************************/
14963
14964 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14965 FORCE is non-zero, redisplay mode lines unconditionally.
14966 Otherwise, redisplay only mode lines that are garbaged. Value is
14967 the number of windows whose mode lines were redisplayed. */
14968
14969 static int
14970 redisplay_mode_lines (window, force)
14971 Lisp_Object window;
14972 int force;
14973 {
14974 int nwindows = 0;
14975
14976 while (!NILP (window))
14977 {
14978 struct window *w = XWINDOW (window);
14979
14980 if (WINDOWP (w->hchild))
14981 nwindows += redisplay_mode_lines (w->hchild, force);
14982 else if (WINDOWP (w->vchild))
14983 nwindows += redisplay_mode_lines (w->vchild, force);
14984 else if (force
14985 || FRAME_GARBAGED_P (XFRAME (w->frame))
14986 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14987 {
14988 struct text_pos lpoint;
14989 struct buffer *old = current_buffer;
14990
14991 /* Set the window's buffer for the mode line display. */
14992 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14993 set_buffer_internal_1 (XBUFFER (w->buffer));
14994
14995 /* Point refers normally to the selected window. For any
14996 other window, set up appropriate value. */
14997 if (!EQ (window, selected_window))
14998 {
14999 struct text_pos pt;
15000
15001 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15002 if (CHARPOS (pt) < BEGV)
15003 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15004 else if (CHARPOS (pt) > (ZV - 1))
15005 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15006 else
15007 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15008 }
15009
15010 /* Display mode lines. */
15011 clear_glyph_matrix (w->desired_matrix);
15012 if (display_mode_lines (w))
15013 {
15014 ++nwindows;
15015 w->must_be_updated_p = 1;
15016 }
15017
15018 /* Restore old settings. */
15019 set_buffer_internal_1 (old);
15020 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15021 }
15022
15023 window = w->next;
15024 }
15025
15026 return nwindows;
15027 }
15028
15029
15030 /* Display the mode and/or top line of window W. Value is the number
15031 of mode lines displayed. */
15032
15033 static int
15034 display_mode_lines (w)
15035 struct window *w;
15036 {
15037 Lisp_Object old_selected_window, old_selected_frame;
15038 int n = 0;
15039
15040 old_selected_frame = selected_frame;
15041 selected_frame = w->frame;
15042 old_selected_window = selected_window;
15043 XSETWINDOW (selected_window, w);
15044
15045 /* These will be set while the mode line specs are processed. */
15046 line_number_displayed = 0;
15047 w->column_number_displayed = Qnil;
15048
15049 if (WINDOW_WANTS_MODELINE_P (w))
15050 {
15051 struct window *sel_w = XWINDOW (old_selected_window);
15052
15053 /* Select mode line face based on the real selected window. */
15054 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15055 current_buffer->mode_line_format);
15056 ++n;
15057 }
15058
15059 if (WINDOW_WANTS_HEADER_LINE_P (w))
15060 {
15061 display_mode_line (w, HEADER_LINE_FACE_ID,
15062 current_buffer->header_line_format);
15063 ++n;
15064 }
15065
15066 selected_frame = old_selected_frame;
15067 selected_window = old_selected_window;
15068 return n;
15069 }
15070
15071
15072 /* Display mode or top line of window W. FACE_ID specifies which line
15073 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15074 FORMAT is the mode line format to display. Value is the pixel
15075 height of the mode line displayed. */
15076
15077 static int
15078 display_mode_line (w, face_id, format)
15079 struct window *w;
15080 enum face_id face_id;
15081 Lisp_Object format;
15082 {
15083 struct it it;
15084 struct face *face;
15085
15086 init_iterator (&it, w, -1, -1, NULL, face_id);
15087 prepare_desired_row (it.glyph_row);
15088
15089 it.glyph_row->mode_line_p = 1;
15090
15091 if (! mode_line_inverse_video)
15092 /* Force the mode-line to be displayed in the default face. */
15093 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15094
15095 /* Temporarily make frame's keyboard the current kboard so that
15096 kboard-local variables in the mode_line_format will get the right
15097 values. */
15098 push_frame_kboard (it.f);
15099 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15100 pop_frame_kboard ();
15101
15102 /* Fill up with spaces. */
15103 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15104
15105 compute_line_metrics (&it);
15106 it.glyph_row->full_width_p = 1;
15107 it.glyph_row->continued_p = 0;
15108 it.glyph_row->truncated_on_left_p = 0;
15109 it.glyph_row->truncated_on_right_p = 0;
15110
15111 /* Make a 3D mode-line have a shadow at its right end. */
15112 face = FACE_FROM_ID (it.f, face_id);
15113 extend_face_to_end_of_line (&it);
15114 if (face->box != FACE_NO_BOX)
15115 {
15116 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15117 + it.glyph_row->used[TEXT_AREA] - 1);
15118 last->right_box_line_p = 1;
15119 }
15120
15121 return it.glyph_row->height;
15122 }
15123
15124 /* Alist that caches the results of :propertize.
15125 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15126 Lisp_Object mode_line_proptrans_alist;
15127
15128 /* List of strings making up the mode-line. */
15129 Lisp_Object mode_line_string_list;
15130
15131 /* Base face property when building propertized mode line string. */
15132 static Lisp_Object mode_line_string_face;
15133 static Lisp_Object mode_line_string_face_prop;
15134
15135
15136 /* Contribute ELT to the mode line for window IT->w. How it
15137 translates into text depends on its data type.
15138
15139 IT describes the display environment in which we display, as usual.
15140
15141 DEPTH is the depth in recursion. It is used to prevent
15142 infinite recursion here.
15143
15144 FIELD_WIDTH is the number of characters the display of ELT should
15145 occupy in the mode line, and PRECISION is the maximum number of
15146 characters to display from ELT's representation. See
15147 display_string for details.
15148
15149 Returns the hpos of the end of the text generated by ELT.
15150
15151 PROPS is a property list to add to any string we encounter.
15152
15153 If RISKY is nonzero, remove (disregard) any properties in any string
15154 we encounter, and ignore :eval and :propertize.
15155
15156 If the global variable `frame_title_ptr' is non-NULL, then the output
15157 is passed to `store_frame_title' instead of `display_string'. */
15158
15159 static int
15160 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15161 struct it *it;
15162 int depth;
15163 int field_width, precision;
15164 Lisp_Object elt, props;
15165 int risky;
15166 {
15167 int n = 0, field, prec;
15168 int literal = 0;
15169
15170 tail_recurse:
15171 if (depth > 100)
15172 elt = build_string ("*too-deep*");
15173
15174 depth++;
15175
15176 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15177 {
15178 case Lisp_String:
15179 {
15180 /* A string: output it and check for %-constructs within it. */
15181 unsigned char c;
15182 const unsigned char *this, *lisp_string;
15183
15184 if (!NILP (props) || risky)
15185 {
15186 Lisp_Object oprops, aelt;
15187 oprops = Ftext_properties_at (make_number (0), elt);
15188
15189 if (NILP (Fequal (props, oprops)) || risky)
15190 {
15191 /* If the starting string has properties,
15192 merge the specified ones onto the existing ones. */
15193 if (! NILP (oprops) && !risky)
15194 {
15195 Lisp_Object tem;
15196
15197 oprops = Fcopy_sequence (oprops);
15198 tem = props;
15199 while (CONSP (tem))
15200 {
15201 oprops = Fplist_put (oprops, XCAR (tem),
15202 XCAR (XCDR (tem)));
15203 tem = XCDR (XCDR (tem));
15204 }
15205 props = oprops;
15206 }
15207
15208 aelt = Fassoc (elt, mode_line_proptrans_alist);
15209 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15210 {
15211 mode_line_proptrans_alist
15212 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15213 elt = XCAR (aelt);
15214 }
15215 else
15216 {
15217 Lisp_Object tem;
15218
15219 elt = Fcopy_sequence (elt);
15220 Fset_text_properties (make_number (0), Flength (elt),
15221 props, elt);
15222 /* Add this item to mode_line_proptrans_alist. */
15223 mode_line_proptrans_alist
15224 = Fcons (Fcons (elt, props),
15225 mode_line_proptrans_alist);
15226 /* Truncate mode_line_proptrans_alist
15227 to at most 50 elements. */
15228 tem = Fnthcdr (make_number (50),
15229 mode_line_proptrans_alist);
15230 if (! NILP (tem))
15231 XSETCDR (tem, Qnil);
15232 }
15233 }
15234 }
15235
15236 this = SDATA (elt);
15237 lisp_string = this;
15238
15239 if (literal)
15240 {
15241 prec = precision - n;
15242 if (frame_title_ptr)
15243 n += store_frame_title (SDATA (elt), -1, prec);
15244 else if (!NILP (mode_line_string_list))
15245 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15246 else
15247 n += display_string (NULL, elt, Qnil, 0, 0, it,
15248 0, prec, 0, STRING_MULTIBYTE (elt));
15249
15250 break;
15251 }
15252
15253 while ((precision <= 0 || n < precision)
15254 && *this
15255 && (frame_title_ptr
15256 || !NILP (mode_line_string_list)
15257 || it->current_x < it->last_visible_x))
15258 {
15259 const unsigned char *last = this;
15260
15261 /* Advance to end of string or next format specifier. */
15262 while ((c = *this++) != '\0' && c != '%')
15263 ;
15264
15265 if (this - 1 != last)
15266 {
15267 /* Output to end of string or up to '%'. Field width
15268 is length of string. Don't output more than
15269 PRECISION allows us. */
15270 --this;
15271
15272 prec = chars_in_text (last, this - last);
15273 if (precision > 0 && prec > precision - n)
15274 prec = precision - n;
15275
15276 if (frame_title_ptr)
15277 n += store_frame_title (last, 0, prec);
15278 else if (!NILP (mode_line_string_list))
15279 {
15280 int bytepos = last - lisp_string;
15281 int charpos = string_byte_to_char (elt, bytepos);
15282 n += store_mode_line_string (NULL,
15283 Fsubstring (elt, make_number (charpos),
15284 make_number (charpos + prec)),
15285 0, 0, 0, Qnil);
15286 }
15287 else
15288 {
15289 int bytepos = last - lisp_string;
15290 int charpos = string_byte_to_char (elt, bytepos);
15291 n += display_string (NULL, elt, Qnil, 0, charpos,
15292 it, 0, prec, 0,
15293 STRING_MULTIBYTE (elt));
15294 }
15295 }
15296 else /* c == '%' */
15297 {
15298 const unsigned char *percent_position = this;
15299
15300 /* Get the specified minimum width. Zero means
15301 don't pad. */
15302 field = 0;
15303 while ((c = *this++) >= '0' && c <= '9')
15304 field = field * 10 + c - '0';
15305
15306 /* Don't pad beyond the total padding allowed. */
15307 if (field_width - n > 0 && field > field_width - n)
15308 field = field_width - n;
15309
15310 /* Note that either PRECISION <= 0 or N < PRECISION. */
15311 prec = precision - n;
15312
15313 if (c == 'M')
15314 n += display_mode_element (it, depth, field, prec,
15315 Vglobal_mode_string, props,
15316 risky);
15317 else if (c != 0)
15318 {
15319 int multibyte;
15320 int bytepos, charpos;
15321 unsigned char *spec;
15322
15323 bytepos = percent_position - lisp_string;
15324 charpos = (STRING_MULTIBYTE (elt)
15325 ? string_byte_to_char (elt, bytepos)
15326 : bytepos);
15327
15328 spec
15329 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15330
15331 if (frame_title_ptr)
15332 n += store_frame_title (spec, field, prec);
15333 else if (!NILP (mode_line_string_list))
15334 {
15335 int len = strlen (spec);
15336 Lisp_Object tem = make_string (spec, len);
15337 props = Ftext_properties_at (make_number (charpos), elt);
15338 /* Should only keep face property in props */
15339 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15340 }
15341 else
15342 {
15343 int nglyphs_before, nwritten;
15344
15345 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15346 nwritten = display_string (spec, Qnil, elt,
15347 charpos, 0, it,
15348 field, prec, 0,
15349 multibyte);
15350
15351 /* Assign to the glyphs written above the
15352 string where the `%x' came from, position
15353 of the `%'. */
15354 if (nwritten > 0)
15355 {
15356 struct glyph *glyph
15357 = (it->glyph_row->glyphs[TEXT_AREA]
15358 + nglyphs_before);
15359 int i;
15360
15361 for (i = 0; i < nwritten; ++i)
15362 {
15363 glyph[i].object = elt;
15364 glyph[i].charpos = charpos;
15365 }
15366
15367 n += nwritten;
15368 }
15369 }
15370 }
15371 else /* c == 0 */
15372 break;
15373 }
15374 }
15375 }
15376 break;
15377
15378 case Lisp_Symbol:
15379 /* A symbol: process the value of the symbol recursively
15380 as if it appeared here directly. Avoid error if symbol void.
15381 Special case: if value of symbol is a string, output the string
15382 literally. */
15383 {
15384 register Lisp_Object tem;
15385
15386 /* If the variable is not marked as risky to set
15387 then its contents are risky to use. */
15388 if (NILP (Fget (elt, Qrisky_local_variable)))
15389 risky = 1;
15390
15391 tem = Fboundp (elt);
15392 if (!NILP (tem))
15393 {
15394 tem = Fsymbol_value (elt);
15395 /* If value is a string, output that string literally:
15396 don't check for % within it. */
15397 if (STRINGP (tem))
15398 literal = 1;
15399
15400 if (!EQ (tem, elt))
15401 {
15402 /* Give up right away for nil or t. */
15403 elt = tem;
15404 goto tail_recurse;
15405 }
15406 }
15407 }
15408 break;
15409
15410 case Lisp_Cons:
15411 {
15412 register Lisp_Object car, tem;
15413
15414 /* A cons cell: five distinct cases.
15415 If first element is :eval or :propertize, do something special.
15416 If first element is a string or a cons, process all the elements
15417 and effectively concatenate them.
15418 If first element is a negative number, truncate displaying cdr to
15419 at most that many characters. If positive, pad (with spaces)
15420 to at least that many characters.
15421 If first element is a symbol, process the cadr or caddr recursively
15422 according to whether the symbol's value is non-nil or nil. */
15423 car = XCAR (elt);
15424 if (EQ (car, QCeval))
15425 {
15426 /* An element of the form (:eval FORM) means evaluate FORM
15427 and use the result as mode line elements. */
15428
15429 if (risky)
15430 break;
15431
15432 if (CONSP (XCDR (elt)))
15433 {
15434 Lisp_Object spec;
15435 spec = safe_eval (XCAR (XCDR (elt)));
15436 n += display_mode_element (it, depth, field_width - n,
15437 precision - n, spec, props,
15438 risky);
15439 }
15440 }
15441 else if (EQ (car, QCpropertize))
15442 {
15443 /* An element of the form (:propertize ELT PROPS...)
15444 means display ELT but applying properties PROPS. */
15445
15446 if (risky)
15447 break;
15448
15449 if (CONSP (XCDR (elt)))
15450 n += display_mode_element (it, depth, field_width - n,
15451 precision - n, XCAR (XCDR (elt)),
15452 XCDR (XCDR (elt)), risky);
15453 }
15454 else if (SYMBOLP (car))
15455 {
15456 tem = Fboundp (car);
15457 elt = XCDR (elt);
15458 if (!CONSP (elt))
15459 goto invalid;
15460 /* elt is now the cdr, and we know it is a cons cell.
15461 Use its car if CAR has a non-nil value. */
15462 if (!NILP (tem))
15463 {
15464 tem = Fsymbol_value (car);
15465 if (!NILP (tem))
15466 {
15467 elt = XCAR (elt);
15468 goto tail_recurse;
15469 }
15470 }
15471 /* Symbol's value is nil (or symbol is unbound)
15472 Get the cddr of the original list
15473 and if possible find the caddr and use that. */
15474 elt = XCDR (elt);
15475 if (NILP (elt))
15476 break;
15477 else if (!CONSP (elt))
15478 goto invalid;
15479 elt = XCAR (elt);
15480 goto tail_recurse;
15481 }
15482 else if (INTEGERP (car))
15483 {
15484 register int lim = XINT (car);
15485 elt = XCDR (elt);
15486 if (lim < 0)
15487 {
15488 /* Negative int means reduce maximum width. */
15489 if (precision <= 0)
15490 precision = -lim;
15491 else
15492 precision = min (precision, -lim);
15493 }
15494 else if (lim > 0)
15495 {
15496 /* Padding specified. Don't let it be more than
15497 current maximum. */
15498 if (precision > 0)
15499 lim = min (precision, lim);
15500
15501 /* If that's more padding than already wanted, queue it.
15502 But don't reduce padding already specified even if
15503 that is beyond the current truncation point. */
15504 field_width = max (lim, field_width);
15505 }
15506 goto tail_recurse;
15507 }
15508 else if (STRINGP (car) || CONSP (car))
15509 {
15510 register int limit = 50;
15511 /* Limit is to protect against circular lists. */
15512 while (CONSP (elt)
15513 && --limit > 0
15514 && (precision <= 0 || n < precision))
15515 {
15516 n += display_mode_element (it, depth, field_width - n,
15517 precision - n, XCAR (elt),
15518 props, risky);
15519 elt = XCDR (elt);
15520 }
15521 }
15522 }
15523 break;
15524
15525 default:
15526 invalid:
15527 elt = build_string ("*invalid*");
15528 goto tail_recurse;
15529 }
15530
15531 /* Pad to FIELD_WIDTH. */
15532 if (field_width > 0 && n < field_width)
15533 {
15534 if (frame_title_ptr)
15535 n += store_frame_title ("", field_width - n, 0);
15536 else if (!NILP (mode_line_string_list))
15537 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15538 else
15539 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15540 0, 0, 0);
15541 }
15542
15543 return n;
15544 }
15545
15546 /* Store a mode-line string element in mode_line_string_list.
15547
15548 If STRING is non-null, display that C string. Otherwise, the Lisp
15549 string LISP_STRING is displayed.
15550
15551 FIELD_WIDTH is the minimum number of output glyphs to produce.
15552 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15553 with spaces. FIELD_WIDTH <= 0 means don't pad.
15554
15555 PRECISION is the maximum number of characters to output from
15556 STRING. PRECISION <= 0 means don't truncate the string.
15557
15558 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15559 properties to the string.
15560
15561 PROPS are the properties to add to the string.
15562 The mode_line_string_face face property is always added to the string.
15563 */
15564
15565 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15566 char *string;
15567 Lisp_Object lisp_string;
15568 int copy_string;
15569 int field_width;
15570 int precision;
15571 Lisp_Object props;
15572 {
15573 int len;
15574 int n = 0;
15575
15576 if (string != NULL)
15577 {
15578 len = strlen (string);
15579 if (precision > 0 && len > precision)
15580 len = precision;
15581 lisp_string = make_string (string, len);
15582 if (NILP (props))
15583 props = mode_line_string_face_prop;
15584 else if (!NILP (mode_line_string_face))
15585 {
15586 Lisp_Object face = Fplist_get (props, Qface);
15587 props = Fcopy_sequence (props);
15588 if (NILP (face))
15589 face = mode_line_string_face;
15590 else
15591 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15592 props = Fplist_put (props, Qface, face);
15593 }
15594 Fadd_text_properties (make_number (0), make_number (len),
15595 props, lisp_string);
15596 }
15597 else
15598 {
15599 len = XFASTINT (Flength (lisp_string));
15600 if (precision > 0 && len > precision)
15601 {
15602 len = precision;
15603 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15604 precision = -1;
15605 }
15606 if (!NILP (mode_line_string_face))
15607 {
15608 Lisp_Object face;
15609 if (NILP (props))
15610 props = Ftext_properties_at (make_number (0), lisp_string);
15611 face = Fplist_get (props, Qface);
15612 if (NILP (face))
15613 face = mode_line_string_face;
15614 else
15615 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15616 props = Fcons (Qface, Fcons (face, Qnil));
15617 if (copy_string)
15618 lisp_string = Fcopy_sequence (lisp_string);
15619 }
15620 if (!NILP (props))
15621 Fadd_text_properties (make_number (0), make_number (len),
15622 props, lisp_string);
15623 }
15624
15625 if (len > 0)
15626 {
15627 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15628 n += len;
15629 }
15630
15631 if (field_width > len)
15632 {
15633 field_width -= len;
15634 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15635 if (!NILP (props))
15636 Fadd_text_properties (make_number (0), make_number (field_width),
15637 props, lisp_string);
15638 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15639 n += field_width;
15640 }
15641
15642 return n;
15643 }
15644
15645
15646 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15647 0, 3, 0,
15648 doc: /* Return the mode-line of selected window as a string.
15649 First optional arg FORMAT specifies a different format string (see
15650 `mode-line-format' for details) to use. If FORMAT is t, return
15651 the buffer's header-line. Second optional arg WINDOW specifies a
15652 different window to use as the context for the formatting.
15653 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15654 (format, window, no_props)
15655 Lisp_Object format, window, no_props;
15656 {
15657 struct it it;
15658 int len;
15659 struct window *w;
15660 struct buffer *old_buffer = NULL;
15661 enum face_id face_id = DEFAULT_FACE_ID;
15662
15663 if (NILP (window))
15664 window = selected_window;
15665 CHECK_WINDOW (window);
15666 w = XWINDOW (window);
15667 CHECK_BUFFER (w->buffer);
15668
15669 if (XBUFFER (w->buffer) != current_buffer)
15670 {
15671 old_buffer = current_buffer;
15672 set_buffer_internal_1 (XBUFFER (w->buffer));
15673 }
15674
15675 if (NILP (format) || EQ (format, Qt))
15676 {
15677 face_id = NILP (format)
15678 ? CURRENT_MODE_LINE_FACE_ID (w) :
15679 HEADER_LINE_FACE_ID;
15680 format = NILP (format)
15681 ? current_buffer->mode_line_format
15682 : current_buffer->header_line_format;
15683 }
15684
15685 init_iterator (&it, w, -1, -1, NULL, face_id);
15686
15687 if (NILP (no_props))
15688 {
15689 mode_line_string_face =
15690 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15691 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15692 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15693
15694 mode_line_string_face_prop =
15695 NILP (mode_line_string_face) ? Qnil :
15696 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15697
15698 /* We need a dummy last element in mode_line_string_list to
15699 indicate we are building the propertized mode-line string.
15700 Using mode_line_string_face_prop here GC protects it. */
15701 mode_line_string_list =
15702 Fcons (mode_line_string_face_prop, Qnil);
15703 frame_title_ptr = NULL;
15704 }
15705 else
15706 {
15707 mode_line_string_face_prop = Qnil;
15708 mode_line_string_list = Qnil;
15709 frame_title_ptr = frame_title_buf;
15710 }
15711
15712 push_frame_kboard (it.f);
15713 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15714 pop_frame_kboard ();
15715
15716 if (old_buffer)
15717 set_buffer_internal_1 (old_buffer);
15718
15719 if (NILP (no_props))
15720 {
15721 Lisp_Object str;
15722 mode_line_string_list = Fnreverse (mode_line_string_list);
15723 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15724 make_string ("", 0));
15725 mode_line_string_face_prop = Qnil;
15726 mode_line_string_list = Qnil;
15727 return str;
15728 }
15729
15730 len = frame_title_ptr - frame_title_buf;
15731 if (len > 0 && frame_title_ptr[-1] == '-')
15732 {
15733 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15734 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15735 ;
15736 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15737 if (len > frame_title_ptr - frame_title_buf)
15738 len = frame_title_ptr - frame_title_buf;
15739 }
15740
15741 frame_title_ptr = NULL;
15742 return make_string (frame_title_buf, len);
15743 }
15744
15745 /* Write a null-terminated, right justified decimal representation of
15746 the positive integer D to BUF using a minimal field width WIDTH. */
15747
15748 static void
15749 pint2str (buf, width, d)
15750 register char *buf;
15751 register int width;
15752 register int d;
15753 {
15754 register char *p = buf;
15755
15756 if (d <= 0)
15757 *p++ = '0';
15758 else
15759 {
15760 while (d > 0)
15761 {
15762 *p++ = d % 10 + '0';
15763 d /= 10;
15764 }
15765 }
15766
15767 for (width -= (int) (p - buf); width > 0; --width)
15768 *p++ = ' ';
15769 *p-- = '\0';
15770 while (p > buf)
15771 {
15772 d = *buf;
15773 *buf++ = *p;
15774 *p-- = d;
15775 }
15776 }
15777
15778 /* Write a null-terminated, right justified decimal and "human
15779 readable" representation of the nonnegative integer D to BUF using
15780 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15781
15782 static const char power_letter[] =
15783 {
15784 0, /* not used */
15785 'k', /* kilo */
15786 'M', /* mega */
15787 'G', /* giga */
15788 'T', /* tera */
15789 'P', /* peta */
15790 'E', /* exa */
15791 'Z', /* zetta */
15792 'Y' /* yotta */
15793 };
15794
15795 static void
15796 pint2hrstr (buf, width, d)
15797 char *buf;
15798 int width;
15799 int d;
15800 {
15801 /* We aim to represent the nonnegative integer D as
15802 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15803 int quotient = d;
15804 int remainder = 0;
15805 /* -1 means: do not use TENTHS. */
15806 int tenths = -1;
15807 int exponent = 0;
15808
15809 /* Length of QUOTIENT.TENTHS as a string. */
15810 int length;
15811
15812 char * psuffix;
15813 char * p;
15814
15815 if (1000 <= quotient)
15816 {
15817 /* Scale to the appropriate EXPONENT. */
15818 do
15819 {
15820 remainder = quotient % 1000;
15821 quotient /= 1000;
15822 exponent++;
15823 }
15824 while (1000 <= quotient);
15825
15826 /* Round to nearest and decide whether to use TENTHS or not. */
15827 if (quotient <= 9)
15828 {
15829 tenths = remainder / 100;
15830 if (50 <= remainder % 100)
15831 if (tenths < 9)
15832 tenths++;
15833 else
15834 {
15835 quotient++;
15836 if (quotient == 10)
15837 tenths = -1;
15838 else
15839 tenths = 0;
15840 }
15841 }
15842 else
15843 if (500 <= remainder)
15844 if (quotient < 999)
15845 quotient++;
15846 else
15847 {
15848 quotient = 1;
15849 exponent++;
15850 tenths = 0;
15851 }
15852 }
15853
15854 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15855 if (tenths == -1 && quotient <= 99)
15856 if (quotient <= 9)
15857 length = 1;
15858 else
15859 length = 2;
15860 else
15861 length = 3;
15862 p = psuffix = buf + max (width, length);
15863
15864 /* Print EXPONENT. */
15865 if (exponent)
15866 *psuffix++ = power_letter[exponent];
15867 *psuffix = '\0';
15868
15869 /* Print TENTHS. */
15870 if (tenths >= 0)
15871 {
15872 *--p = '0' + tenths;
15873 *--p = '.';
15874 }
15875
15876 /* Print QUOTIENT. */
15877 do
15878 {
15879 int digit = quotient % 10;
15880 *--p = '0' + digit;
15881 }
15882 while ((quotient /= 10) != 0);
15883
15884 /* Print leading spaces. */
15885 while (buf < p)
15886 *--p = ' ';
15887 }
15888
15889 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15890 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15891 type of CODING_SYSTEM. Return updated pointer into BUF. */
15892
15893 static unsigned char invalid_eol_type[] = "(*invalid*)";
15894
15895 static char *
15896 decode_mode_spec_coding (coding_system, buf, eol_flag)
15897 Lisp_Object coding_system;
15898 register char *buf;
15899 int eol_flag;
15900 {
15901 Lisp_Object val;
15902 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15903 const unsigned char *eol_str;
15904 int eol_str_len;
15905 /* The EOL conversion we are using. */
15906 Lisp_Object eoltype;
15907
15908 val = Fget (coding_system, Qcoding_system);
15909 eoltype = Qnil;
15910
15911 if (!VECTORP (val)) /* Not yet decided. */
15912 {
15913 if (multibyte)
15914 *buf++ = '-';
15915 if (eol_flag)
15916 eoltype = eol_mnemonic_undecided;
15917 /* Don't mention EOL conversion if it isn't decided. */
15918 }
15919 else
15920 {
15921 Lisp_Object eolvalue;
15922
15923 eolvalue = Fget (coding_system, Qeol_type);
15924
15925 if (multibyte)
15926 *buf++ = XFASTINT (AREF (val, 1));
15927
15928 if (eol_flag)
15929 {
15930 /* The EOL conversion that is normal on this system. */
15931
15932 if (NILP (eolvalue)) /* Not yet decided. */
15933 eoltype = eol_mnemonic_undecided;
15934 else if (VECTORP (eolvalue)) /* Not yet decided. */
15935 eoltype = eol_mnemonic_undecided;
15936 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15937 eoltype = (XFASTINT (eolvalue) == 0
15938 ? eol_mnemonic_unix
15939 : (XFASTINT (eolvalue) == 1
15940 ? eol_mnemonic_dos : eol_mnemonic_mac));
15941 }
15942 }
15943
15944 if (eol_flag)
15945 {
15946 /* Mention the EOL conversion if it is not the usual one. */
15947 if (STRINGP (eoltype))
15948 {
15949 eol_str = SDATA (eoltype);
15950 eol_str_len = SBYTES (eoltype);
15951 }
15952 else if (INTEGERP (eoltype)
15953 && CHAR_VALID_P (XINT (eoltype), 0))
15954 {
15955 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15956 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15957 eol_str = tmp;
15958 }
15959 else
15960 {
15961 eol_str = invalid_eol_type;
15962 eol_str_len = sizeof (invalid_eol_type) - 1;
15963 }
15964 bcopy (eol_str, buf, eol_str_len);
15965 buf += eol_str_len;
15966 }
15967
15968 return buf;
15969 }
15970
15971 /* Return a string for the output of a mode line %-spec for window W,
15972 generated by character C. PRECISION >= 0 means don't return a
15973 string longer than that value. FIELD_WIDTH > 0 means pad the
15974 string returned with spaces to that value. Return 1 in *MULTIBYTE
15975 if the result is multibyte text. */
15976
15977 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15978
15979 static char *
15980 decode_mode_spec (w, c, field_width, precision, multibyte)
15981 struct window *w;
15982 register int c;
15983 int field_width, precision;
15984 int *multibyte;
15985 {
15986 Lisp_Object obj;
15987 struct frame *f = XFRAME (WINDOW_FRAME (w));
15988 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15989 struct buffer *b = XBUFFER (w->buffer);
15990
15991 obj = Qnil;
15992 *multibyte = 0;
15993
15994 switch (c)
15995 {
15996 case '*':
15997 if (!NILP (b->read_only))
15998 return "%";
15999 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16000 return "*";
16001 return "-";
16002
16003 case '+':
16004 /* This differs from %* only for a modified read-only buffer. */
16005 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16006 return "*";
16007 if (!NILP (b->read_only))
16008 return "%";
16009 return "-";
16010
16011 case '&':
16012 /* This differs from %* in ignoring read-only-ness. */
16013 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16014 return "*";
16015 return "-";
16016
16017 case '%':
16018 return "%";
16019
16020 case '[':
16021 {
16022 int i;
16023 char *p;
16024
16025 if (command_loop_level > 5)
16026 return "[[[... ";
16027 p = decode_mode_spec_buf;
16028 for (i = 0; i < command_loop_level; i++)
16029 *p++ = '[';
16030 *p = 0;
16031 return decode_mode_spec_buf;
16032 }
16033
16034 case ']':
16035 {
16036 int i;
16037 char *p;
16038
16039 if (command_loop_level > 5)
16040 return " ...]]]";
16041 p = decode_mode_spec_buf;
16042 for (i = 0; i < command_loop_level; i++)
16043 *p++ = ']';
16044 *p = 0;
16045 return decode_mode_spec_buf;
16046 }
16047
16048 case '-':
16049 {
16050 register int i;
16051
16052 /* Let lots_of_dashes be a string of infinite length. */
16053 if (!NILP (mode_line_string_list))
16054 return "--";
16055 if (field_width <= 0
16056 || field_width > sizeof (lots_of_dashes))
16057 {
16058 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16059 decode_mode_spec_buf[i] = '-';
16060 decode_mode_spec_buf[i] = '\0';
16061 return decode_mode_spec_buf;
16062 }
16063 else
16064 return lots_of_dashes;
16065 }
16066
16067 case 'b':
16068 obj = b->name;
16069 break;
16070
16071 case 'c':
16072 {
16073 int col = (int) current_column (); /* iftc */
16074 w->column_number_displayed = make_number (col);
16075 pint2str (decode_mode_spec_buf, field_width, col);
16076 return decode_mode_spec_buf;
16077 }
16078
16079 case 'F':
16080 /* %F displays the frame name. */
16081 if (!NILP (f->title))
16082 return (char *) SDATA (f->title);
16083 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16084 return (char *) SDATA (f->name);
16085 return "Emacs";
16086
16087 case 'f':
16088 obj = b->filename;
16089 break;
16090
16091 case 'i':
16092 {
16093 int size = ZV - BEGV;
16094 pint2str (decode_mode_spec_buf, field_width, size);
16095 return decode_mode_spec_buf;
16096 }
16097
16098 case 'I':
16099 {
16100 int size = ZV - BEGV;
16101 pint2hrstr (decode_mode_spec_buf, field_width, size);
16102 return decode_mode_spec_buf;
16103 }
16104
16105 case 'l':
16106 {
16107 int startpos = XMARKER (w->start)->charpos;
16108 int startpos_byte = marker_byte_position (w->start);
16109 int line, linepos, linepos_byte, topline;
16110 int nlines, junk;
16111 int height = WINDOW_TOTAL_LINES (w);
16112
16113 /* If we decided that this buffer isn't suitable for line numbers,
16114 don't forget that too fast. */
16115 if (EQ (w->base_line_pos, w->buffer))
16116 goto no_value;
16117 /* But do forget it, if the window shows a different buffer now. */
16118 else if (BUFFERP (w->base_line_pos))
16119 w->base_line_pos = Qnil;
16120
16121 /* If the buffer is very big, don't waste time. */
16122 if (INTEGERP (Vline_number_display_limit)
16123 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16124 {
16125 w->base_line_pos = Qnil;
16126 w->base_line_number = Qnil;
16127 goto no_value;
16128 }
16129
16130 if (!NILP (w->base_line_number)
16131 && !NILP (w->base_line_pos)
16132 && XFASTINT (w->base_line_pos) <= startpos)
16133 {
16134 line = XFASTINT (w->base_line_number);
16135 linepos = XFASTINT (w->base_line_pos);
16136 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16137 }
16138 else
16139 {
16140 line = 1;
16141 linepos = BUF_BEGV (b);
16142 linepos_byte = BUF_BEGV_BYTE (b);
16143 }
16144
16145 /* Count lines from base line to window start position. */
16146 nlines = display_count_lines (linepos, linepos_byte,
16147 startpos_byte,
16148 startpos, &junk);
16149
16150 topline = nlines + line;
16151
16152 /* Determine a new base line, if the old one is too close
16153 or too far away, or if we did not have one.
16154 "Too close" means it's plausible a scroll-down would
16155 go back past it. */
16156 if (startpos == BUF_BEGV (b))
16157 {
16158 w->base_line_number = make_number (topline);
16159 w->base_line_pos = make_number (BUF_BEGV (b));
16160 }
16161 else if (nlines < height + 25 || nlines > height * 3 + 50
16162 || linepos == BUF_BEGV (b))
16163 {
16164 int limit = BUF_BEGV (b);
16165 int limit_byte = BUF_BEGV_BYTE (b);
16166 int position;
16167 int distance = (height * 2 + 30) * line_number_display_limit_width;
16168
16169 if (startpos - distance > limit)
16170 {
16171 limit = startpos - distance;
16172 limit_byte = CHAR_TO_BYTE (limit);
16173 }
16174
16175 nlines = display_count_lines (startpos, startpos_byte,
16176 limit_byte,
16177 - (height * 2 + 30),
16178 &position);
16179 /* If we couldn't find the lines we wanted within
16180 line_number_display_limit_width chars per line,
16181 give up on line numbers for this window. */
16182 if (position == limit_byte && limit == startpos - distance)
16183 {
16184 w->base_line_pos = w->buffer;
16185 w->base_line_number = Qnil;
16186 goto no_value;
16187 }
16188
16189 w->base_line_number = make_number (topline - nlines);
16190 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16191 }
16192
16193 /* Now count lines from the start pos to point. */
16194 nlines = display_count_lines (startpos, startpos_byte,
16195 PT_BYTE, PT, &junk);
16196
16197 /* Record that we did display the line number. */
16198 line_number_displayed = 1;
16199
16200 /* Make the string to show. */
16201 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16202 return decode_mode_spec_buf;
16203 no_value:
16204 {
16205 char* p = decode_mode_spec_buf;
16206 int pad = field_width - 2;
16207 while (pad-- > 0)
16208 *p++ = ' ';
16209 *p++ = '?';
16210 *p++ = '?';
16211 *p = '\0';
16212 return decode_mode_spec_buf;
16213 }
16214 }
16215 break;
16216
16217 case 'm':
16218 obj = b->mode_name;
16219 break;
16220
16221 case 'n':
16222 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16223 return " Narrow";
16224 break;
16225
16226 case 'p':
16227 {
16228 int pos = marker_position (w->start);
16229 int total = BUF_ZV (b) - BUF_BEGV (b);
16230
16231 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16232 {
16233 if (pos <= BUF_BEGV (b))
16234 return "All";
16235 else
16236 return "Bottom";
16237 }
16238 else if (pos <= BUF_BEGV (b))
16239 return "Top";
16240 else
16241 {
16242 if (total > 1000000)
16243 /* Do it differently for a large value, to avoid overflow. */
16244 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16245 else
16246 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16247 /* We can't normally display a 3-digit number,
16248 so get us a 2-digit number that is close. */
16249 if (total == 100)
16250 total = 99;
16251 sprintf (decode_mode_spec_buf, "%2d%%", total);
16252 return decode_mode_spec_buf;
16253 }
16254 }
16255
16256 /* Display percentage of size above the bottom of the screen. */
16257 case 'P':
16258 {
16259 int toppos = marker_position (w->start);
16260 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16261 int total = BUF_ZV (b) - BUF_BEGV (b);
16262
16263 if (botpos >= BUF_ZV (b))
16264 {
16265 if (toppos <= BUF_BEGV (b))
16266 return "All";
16267 else
16268 return "Bottom";
16269 }
16270 else
16271 {
16272 if (total > 1000000)
16273 /* Do it differently for a large value, to avoid overflow. */
16274 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16275 else
16276 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16277 /* We can't normally display a 3-digit number,
16278 so get us a 2-digit number that is close. */
16279 if (total == 100)
16280 total = 99;
16281 if (toppos <= BUF_BEGV (b))
16282 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16283 else
16284 sprintf (decode_mode_spec_buf, "%2d%%", total);
16285 return decode_mode_spec_buf;
16286 }
16287 }
16288
16289 case 's':
16290 /* status of process */
16291 obj = Fget_buffer_process (w->buffer);
16292 if (NILP (obj))
16293 return "no process";
16294 #ifdef subprocesses
16295 obj = Fsymbol_name (Fprocess_status (obj));
16296 #endif
16297 break;
16298
16299 case 't': /* indicate TEXT or BINARY */
16300 #ifdef MODE_LINE_BINARY_TEXT
16301 return MODE_LINE_BINARY_TEXT (b);
16302 #else
16303 return "T";
16304 #endif
16305
16306 case 'z':
16307 /* coding-system (not including end-of-line format) */
16308 case 'Z':
16309 /* coding-system (including end-of-line type) */
16310 {
16311 int eol_flag = (c == 'Z');
16312 char *p = decode_mode_spec_buf;
16313
16314 if (! FRAME_WINDOW_P (f))
16315 {
16316 /* No need to mention EOL here--the terminal never needs
16317 to do EOL conversion. */
16318 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16319 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16320 }
16321 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16322 p, eol_flag);
16323
16324 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16325 #ifdef subprocesses
16326 obj = Fget_buffer_process (Fcurrent_buffer ());
16327 if (PROCESSP (obj))
16328 {
16329 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16330 p, eol_flag);
16331 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16332 p, eol_flag);
16333 }
16334 #endif /* subprocesses */
16335 #endif /* 0 */
16336 *p = 0;
16337 return decode_mode_spec_buf;
16338 }
16339 }
16340
16341 if (STRINGP (obj))
16342 {
16343 *multibyte = STRING_MULTIBYTE (obj);
16344 return (char *) SDATA (obj);
16345 }
16346 else
16347 return "";
16348 }
16349
16350
16351 /* Count up to COUNT lines starting from START / START_BYTE.
16352 But don't go beyond LIMIT_BYTE.
16353 Return the number of lines thus found (always nonnegative).
16354
16355 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16356
16357 static int
16358 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16359 int start, start_byte, limit_byte, count;
16360 int *byte_pos_ptr;
16361 {
16362 register unsigned char *cursor;
16363 unsigned char *base;
16364
16365 register int ceiling;
16366 register unsigned char *ceiling_addr;
16367 int orig_count = count;
16368
16369 /* If we are not in selective display mode,
16370 check only for newlines. */
16371 int selective_display = (!NILP (current_buffer->selective_display)
16372 && !INTEGERP (current_buffer->selective_display));
16373
16374 if (count > 0)
16375 {
16376 while (start_byte < limit_byte)
16377 {
16378 ceiling = BUFFER_CEILING_OF (start_byte);
16379 ceiling = min (limit_byte - 1, ceiling);
16380 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16381 base = (cursor = BYTE_POS_ADDR (start_byte));
16382 while (1)
16383 {
16384 if (selective_display)
16385 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16386 ;
16387 else
16388 while (*cursor != '\n' && ++cursor != ceiling_addr)
16389 ;
16390
16391 if (cursor != ceiling_addr)
16392 {
16393 if (--count == 0)
16394 {
16395 start_byte += cursor - base + 1;
16396 *byte_pos_ptr = start_byte;
16397 return orig_count;
16398 }
16399 else
16400 if (++cursor == ceiling_addr)
16401 break;
16402 }
16403 else
16404 break;
16405 }
16406 start_byte += cursor - base;
16407 }
16408 }
16409 else
16410 {
16411 while (start_byte > limit_byte)
16412 {
16413 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16414 ceiling = max (limit_byte, ceiling);
16415 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16416 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16417 while (1)
16418 {
16419 if (selective_display)
16420 while (--cursor != ceiling_addr
16421 && *cursor != '\n' && *cursor != 015)
16422 ;
16423 else
16424 while (--cursor != ceiling_addr && *cursor != '\n')
16425 ;
16426
16427 if (cursor != ceiling_addr)
16428 {
16429 if (++count == 0)
16430 {
16431 start_byte += cursor - base + 1;
16432 *byte_pos_ptr = start_byte;
16433 /* When scanning backwards, we should
16434 not count the newline posterior to which we stop. */
16435 return - orig_count - 1;
16436 }
16437 }
16438 else
16439 break;
16440 }
16441 /* Here we add 1 to compensate for the last decrement
16442 of CURSOR, which took it past the valid range. */
16443 start_byte += cursor - base + 1;
16444 }
16445 }
16446
16447 *byte_pos_ptr = limit_byte;
16448
16449 if (count < 0)
16450 return - orig_count + count;
16451 return orig_count - count;
16452
16453 }
16454
16455
16456 \f
16457 /***********************************************************************
16458 Displaying strings
16459 ***********************************************************************/
16460
16461 /* Display a NUL-terminated string, starting with index START.
16462
16463 If STRING is non-null, display that C string. Otherwise, the Lisp
16464 string LISP_STRING is displayed.
16465
16466 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16467 FACE_STRING. Display STRING or LISP_STRING with the face at
16468 FACE_STRING_POS in FACE_STRING:
16469
16470 Display the string in the environment given by IT, but use the
16471 standard display table, temporarily.
16472
16473 FIELD_WIDTH is the minimum number of output glyphs to produce.
16474 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16475 with spaces. If STRING has more characters, more than FIELD_WIDTH
16476 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16477
16478 PRECISION is the maximum number of characters to output from
16479 STRING. PRECISION < 0 means don't truncate the string.
16480
16481 This is roughly equivalent to printf format specifiers:
16482
16483 FIELD_WIDTH PRECISION PRINTF
16484 ----------------------------------------
16485 -1 -1 %s
16486 -1 10 %.10s
16487 10 -1 %10s
16488 20 10 %20.10s
16489
16490 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16491 display them, and < 0 means obey the current buffer's value of
16492 enable_multibyte_characters.
16493
16494 Value is the number of glyphs produced. */
16495
16496 static int
16497 display_string (string, lisp_string, face_string, face_string_pos,
16498 start, it, field_width, precision, max_x, multibyte)
16499 unsigned char *string;
16500 Lisp_Object lisp_string;
16501 Lisp_Object face_string;
16502 int face_string_pos;
16503 int start;
16504 struct it *it;
16505 int field_width, precision, max_x;
16506 int multibyte;
16507 {
16508 int hpos_at_start = it->hpos;
16509 int saved_face_id = it->face_id;
16510 struct glyph_row *row = it->glyph_row;
16511
16512 /* Initialize the iterator IT for iteration over STRING beginning
16513 with index START. */
16514 reseat_to_string (it, string, lisp_string, start,
16515 precision, field_width, multibyte);
16516
16517 /* If displaying STRING, set up the face of the iterator
16518 from LISP_STRING, if that's given. */
16519 if (STRINGP (face_string))
16520 {
16521 int endptr;
16522 struct face *face;
16523
16524 it->face_id
16525 = face_at_string_position (it->w, face_string, face_string_pos,
16526 0, it->region_beg_charpos,
16527 it->region_end_charpos,
16528 &endptr, it->base_face_id, 0);
16529 face = FACE_FROM_ID (it->f, it->face_id);
16530 it->face_box_p = face->box != FACE_NO_BOX;
16531 }
16532
16533 /* Set max_x to the maximum allowed X position. Don't let it go
16534 beyond the right edge of the window. */
16535 if (max_x <= 0)
16536 max_x = it->last_visible_x;
16537 else
16538 max_x = min (max_x, it->last_visible_x);
16539
16540 /* Skip over display elements that are not visible. because IT->w is
16541 hscrolled. */
16542 if (it->current_x < it->first_visible_x)
16543 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16544 MOVE_TO_POS | MOVE_TO_X);
16545
16546 row->ascent = it->max_ascent;
16547 row->height = it->max_ascent + it->max_descent;
16548 row->phys_ascent = it->max_phys_ascent;
16549 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16550
16551 /* This condition is for the case that we are called with current_x
16552 past last_visible_x. */
16553 while (it->current_x < max_x)
16554 {
16555 int x_before, x, n_glyphs_before, i, nglyphs;
16556
16557 /* Get the next display element. */
16558 if (!get_next_display_element (it))
16559 break;
16560
16561 /* Produce glyphs. */
16562 x_before = it->current_x;
16563 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16564 PRODUCE_GLYPHS (it);
16565
16566 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16567 i = 0;
16568 x = x_before;
16569 while (i < nglyphs)
16570 {
16571 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16572
16573 if (!it->truncate_lines_p
16574 && x + glyph->pixel_width > max_x)
16575 {
16576 /* End of continued line or max_x reached. */
16577 if (CHAR_GLYPH_PADDING_P (*glyph))
16578 {
16579 /* A wide character is unbreakable. */
16580 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16581 it->current_x = x_before;
16582 }
16583 else
16584 {
16585 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16586 it->current_x = x;
16587 }
16588 break;
16589 }
16590 else if (x + glyph->pixel_width > it->first_visible_x)
16591 {
16592 /* Glyph is at least partially visible. */
16593 ++it->hpos;
16594 if (x < it->first_visible_x)
16595 it->glyph_row->x = x - it->first_visible_x;
16596 }
16597 else
16598 {
16599 /* Glyph is off the left margin of the display area.
16600 Should not happen. */
16601 abort ();
16602 }
16603
16604 row->ascent = max (row->ascent, it->max_ascent);
16605 row->height = max (row->height, it->max_ascent + it->max_descent);
16606 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16607 row->phys_height = max (row->phys_height,
16608 it->max_phys_ascent + it->max_phys_descent);
16609 x += glyph->pixel_width;
16610 ++i;
16611 }
16612
16613 /* Stop if max_x reached. */
16614 if (i < nglyphs)
16615 break;
16616
16617 /* Stop at line ends. */
16618 if (ITERATOR_AT_END_OF_LINE_P (it))
16619 {
16620 it->continuation_lines_width = 0;
16621 break;
16622 }
16623
16624 set_iterator_to_next (it, 1);
16625
16626 /* Stop if truncating at the right edge. */
16627 if (it->truncate_lines_p
16628 && it->current_x >= it->last_visible_x)
16629 {
16630 /* Add truncation mark, but don't do it if the line is
16631 truncated at a padding space. */
16632 if (IT_CHARPOS (*it) < it->string_nchars)
16633 {
16634 if (!FRAME_WINDOW_P (it->f))
16635 {
16636 int i, n;
16637
16638 if (it->current_x > it->last_visible_x)
16639 {
16640 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16641 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16642 break;
16643 for (n = row->used[TEXT_AREA]; i < n; ++i)
16644 {
16645 row->used[TEXT_AREA] = i;
16646 produce_special_glyphs (it, IT_TRUNCATION);
16647 }
16648 }
16649 produce_special_glyphs (it, IT_TRUNCATION);
16650 }
16651 it->glyph_row->truncated_on_right_p = 1;
16652 }
16653 break;
16654 }
16655 }
16656
16657 /* Maybe insert a truncation at the left. */
16658 if (it->first_visible_x
16659 && IT_CHARPOS (*it) > 0)
16660 {
16661 if (!FRAME_WINDOW_P (it->f))
16662 insert_left_trunc_glyphs (it);
16663 it->glyph_row->truncated_on_left_p = 1;
16664 }
16665
16666 it->face_id = saved_face_id;
16667
16668 /* Value is number of columns displayed. */
16669 return it->hpos - hpos_at_start;
16670 }
16671
16672
16673 \f
16674 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16675 appears as an element of LIST or as the car of an element of LIST.
16676 If PROPVAL is a list, compare each element against LIST in that
16677 way, and return 1/2 if any element of PROPVAL is found in LIST.
16678 Otherwise return 0. This function cannot quit.
16679 The return value is 2 if the text is invisible but with an ellipsis
16680 and 1 if it's invisible and without an ellipsis. */
16681
16682 int
16683 invisible_p (propval, list)
16684 register Lisp_Object propval;
16685 Lisp_Object list;
16686 {
16687 register Lisp_Object tail, proptail;
16688
16689 for (tail = list; CONSP (tail); tail = XCDR (tail))
16690 {
16691 register Lisp_Object tem;
16692 tem = XCAR (tail);
16693 if (EQ (propval, tem))
16694 return 1;
16695 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16696 return NILP (XCDR (tem)) ? 1 : 2;
16697 }
16698
16699 if (CONSP (propval))
16700 {
16701 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16702 {
16703 Lisp_Object propelt;
16704 propelt = XCAR (proptail);
16705 for (tail = list; CONSP (tail); tail = XCDR (tail))
16706 {
16707 register Lisp_Object tem;
16708 tem = XCAR (tail);
16709 if (EQ (propelt, tem))
16710 return 1;
16711 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16712 return NILP (XCDR (tem)) ? 1 : 2;
16713 }
16714 }
16715 }
16716
16717 return 0;
16718 }
16719
16720 /* Calculate a width or height in pixels from a specification using
16721 the following elements:
16722
16723 SPEC ::=
16724 NUM - a (fractional) multiple of the default font width/height
16725 (NUM) - specifies exactly NUM pixels
16726 UNIT - a fixed number of pixels, see below.
16727 ELEMENT - size of a display element in pixels, see below.
16728 (NUM . SPEC) - equals NUM * SPEC
16729 (+ SPEC SPEC ...) - add pixel values
16730 (- SPEC SPEC ...) - subtract pixel values
16731 (- SPEC) - negate pixel value
16732
16733 NUM ::=
16734 INT or FLOAT - a number constant
16735 SYMBOL - use symbol's (buffer local) variable binding.
16736
16737 UNIT ::=
16738 in - pixels per inch *)
16739 mm - pixels per 1/1000 meter *)
16740 cm - pixels per 1/100 meter *)
16741 width - width of current font in pixels.
16742 height - height of current font in pixels.
16743
16744 *) using the ratio(s) defined in display-pixels-per-inch.
16745
16746 ELEMENT ::=
16747
16748 left-fringe - left fringe width in pixels
16749 right-fringe - right fringe width in pixels
16750
16751 left-margin - left margin width in pixels
16752 right-margin - right margin width in pixels
16753
16754 scroll-bar - scroll-bar area width in pixels
16755
16756 Examples:
16757
16758 Pixels corresponding to 5 inches:
16759 (5 . in)
16760
16761 Total width of non-text areas on left side of window (if scroll-bar is on left):
16762 '(space :width (+ left-fringe left-margin scroll-bar))
16763
16764 Align to first text column (in header line):
16765 '(space :align-to 0)
16766
16767 Align to middle of text area minus half the width of variable `my-image'
16768 containing a loaded image:
16769 '(space :align-to (0.5 . (- text my-image)))
16770
16771 Width of left margin minus width of 1 character in the default font:
16772 '(space :width (- left-margin 1))
16773
16774 Width of left margin minus width of 2 characters in the current font:
16775 '(space :width (- left-margin (2 . width)))
16776
16777 Center 1 character over left-margin (in header line):
16778 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16779
16780 Different ways to express width of left fringe plus left margin minus one pixel:
16781 '(space :width (- (+ left-fringe left-margin) (1)))
16782 '(space :width (+ left-fringe left-margin (- (1))))
16783 '(space :width (+ left-fringe left-margin (-1)))
16784
16785 */
16786
16787 #define NUMVAL(X) \
16788 ((INTEGERP (X) || FLOATP (X)) \
16789 ? XFLOATINT (X) \
16790 : - 1)
16791
16792 int
16793 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16794 double *res;
16795 struct it *it;
16796 Lisp_Object prop;
16797 void *font;
16798 int width_p, *align_to;
16799 {
16800 double pixels;
16801
16802 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16803 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16804
16805 if (NILP (prop))
16806 return OK_PIXELS (0);
16807
16808 if (SYMBOLP (prop))
16809 {
16810 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16811 {
16812 char *unit = SDATA (SYMBOL_NAME (prop));
16813
16814 if (unit[0] == 'i' && unit[1] == 'n')
16815 pixels = 1.0;
16816 else if (unit[0] == 'm' && unit[1] == 'm')
16817 pixels = 25.4;
16818 else if (unit[0] == 'c' && unit[1] == 'm')
16819 pixels = 2.54;
16820 else
16821 pixels = 0;
16822 if (pixels > 0)
16823 {
16824 double ppi;
16825 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16826 || (CONSP (Vdisplay_pixels_per_inch)
16827 && (ppi = (width_p
16828 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16829 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16830 ppi > 0)))
16831 return OK_PIXELS (ppi / pixels);
16832
16833 return 0;
16834 }
16835 }
16836
16837 #ifdef HAVE_WINDOW_SYSTEM
16838 if (EQ (prop, Qheight))
16839 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16840 if (EQ (prop, Qwidth))
16841 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16842 #else
16843 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16844 return OK_PIXELS (1);
16845 #endif
16846
16847 if (EQ (prop, Qtext))
16848 return OK_PIXELS (width_p
16849 ? window_box_width (it->w, TEXT_AREA)
16850 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16851
16852 if (align_to && *align_to < 0)
16853 {
16854 *res = 0;
16855 if (EQ (prop, Qleft))
16856 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16857 if (EQ (prop, Qright))
16858 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16859 if (EQ (prop, Qcenter))
16860 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16861 + window_box_width (it->w, TEXT_AREA) / 2);
16862 if (EQ (prop, Qleft_fringe))
16863 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16864 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16865 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16866 if (EQ (prop, Qright_fringe))
16867 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16868 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16869 : window_box_right_offset (it->w, TEXT_AREA));
16870 if (EQ (prop, Qleft_margin))
16871 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16872 if (EQ (prop, Qright_margin))
16873 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16874 if (EQ (prop, Qscroll_bar))
16875 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16876 ? 0
16877 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16878 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16879 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16880 : 0)));
16881 }
16882 else
16883 {
16884 if (EQ (prop, Qleft_fringe))
16885 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16886 if (EQ (prop, Qright_fringe))
16887 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
16888 if (EQ (prop, Qleft_margin))
16889 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
16890 if (EQ (prop, Qright_margin))
16891 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
16892 if (EQ (prop, Qscroll_bar))
16893 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
16894 }
16895
16896 prop = Fbuffer_local_value (prop, it->w->buffer);
16897 }
16898
16899 if (INTEGERP (prop) || FLOATP (prop))
16900 {
16901 int base_unit = (width_p
16902 ? FRAME_COLUMN_WIDTH (it->f)
16903 : FRAME_LINE_HEIGHT (it->f));
16904 return OK_PIXELS (XFLOATINT (prop) * base_unit);
16905 }
16906
16907 if (CONSP (prop))
16908 {
16909 Lisp_Object car = XCAR (prop);
16910 Lisp_Object cdr = XCDR (prop);
16911
16912 if (SYMBOLP (car))
16913 {
16914 #ifdef HAVE_WINDOW_SYSTEM
16915 if (valid_image_p (prop))
16916 {
16917 int id = lookup_image (it->f, prop);
16918 struct image *img = IMAGE_FROM_ID (it->f, id);
16919
16920 return OK_PIXELS (width_p ? img->width : img->height);
16921 }
16922 #endif
16923 if (EQ (car, Qplus) || EQ (car, Qminus))
16924 {
16925 int first = 1;
16926 double px;
16927
16928 pixels = 0;
16929 while (CONSP (cdr))
16930 {
16931 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
16932 font, width_p, align_to))
16933 return 0;
16934 if (first)
16935 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
16936 else
16937 pixels += px;
16938 cdr = XCDR (cdr);
16939 }
16940 if (EQ (car, Qminus))
16941 pixels = -pixels;
16942 return OK_PIXELS (pixels);
16943 }
16944
16945 car = Fbuffer_local_value (car, it->w->buffer);
16946 }
16947
16948 if (INTEGERP (car) || FLOATP (car))
16949 {
16950 double fact;
16951 pixels = XFLOATINT (car);
16952 if (NILP (cdr))
16953 return OK_PIXELS (pixels);
16954 if (calc_pixel_width_or_height (&fact, it, cdr,
16955 font, width_p, align_to))
16956 return OK_PIXELS (pixels * fact);
16957 return 0;
16958 }
16959
16960 return 0;
16961 }
16962
16963 return 0;
16964 }
16965
16966 \f
16967 /***********************************************************************
16968 Glyph Display
16969 ***********************************************************************/
16970
16971 #ifdef HAVE_WINDOW_SYSTEM
16972
16973 #if GLYPH_DEBUG
16974
16975 void
16976 dump_glyph_string (s)
16977 struct glyph_string *s;
16978 {
16979 fprintf (stderr, "glyph string\n");
16980 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16981 s->x, s->y, s->width, s->height);
16982 fprintf (stderr, " ybase = %d\n", s->ybase);
16983 fprintf (stderr, " hl = %d\n", s->hl);
16984 fprintf (stderr, " left overhang = %d, right = %d\n",
16985 s->left_overhang, s->right_overhang);
16986 fprintf (stderr, " nchars = %d\n", s->nchars);
16987 fprintf (stderr, " extends to end of line = %d\n",
16988 s->extends_to_end_of_line_p);
16989 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16990 fprintf (stderr, " bg width = %d\n", s->background_width);
16991 }
16992
16993 #endif /* GLYPH_DEBUG */
16994
16995 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16996 of XChar2b structures for S; it can't be allocated in
16997 init_glyph_string because it must be allocated via `alloca'. W
16998 is the window on which S is drawn. ROW and AREA are the glyph row
16999 and area within the row from which S is constructed. START is the
17000 index of the first glyph structure covered by S. HL is a
17001 face-override for drawing S. */
17002
17003 #ifdef HAVE_NTGUI
17004 #define OPTIONAL_HDC(hdc) hdc,
17005 #define DECLARE_HDC(hdc) HDC hdc;
17006 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17007 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17008 #endif
17009
17010 #ifndef OPTIONAL_HDC
17011 #define OPTIONAL_HDC(hdc)
17012 #define DECLARE_HDC(hdc)
17013 #define ALLOCATE_HDC(hdc, f)
17014 #define RELEASE_HDC(hdc, f)
17015 #endif
17016
17017 static void
17018 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17019 struct glyph_string *s;
17020 DECLARE_HDC (hdc)
17021 XChar2b *char2b;
17022 struct window *w;
17023 struct glyph_row *row;
17024 enum glyph_row_area area;
17025 int start;
17026 enum draw_glyphs_face hl;
17027 {
17028 bzero (s, sizeof *s);
17029 s->w = w;
17030 s->f = XFRAME (w->frame);
17031 #ifdef HAVE_NTGUI
17032 s->hdc = hdc;
17033 #endif
17034 s->display = FRAME_X_DISPLAY (s->f);
17035 s->window = FRAME_X_WINDOW (s->f);
17036 s->char2b = char2b;
17037 s->hl = hl;
17038 s->row = row;
17039 s->area = area;
17040 s->first_glyph = row->glyphs[area] + start;
17041 s->height = row->height;
17042 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17043
17044 /* Display the internal border below the tool-bar window. */
17045 if (s->w == XWINDOW (s->f->tool_bar_window))
17046 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17047
17048 s->ybase = s->y + row->ascent;
17049 }
17050
17051
17052 /* Append the list of glyph strings with head H and tail T to the list
17053 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17054
17055 static INLINE void
17056 append_glyph_string_lists (head, tail, h, t)
17057 struct glyph_string **head, **tail;
17058 struct glyph_string *h, *t;
17059 {
17060 if (h)
17061 {
17062 if (*head)
17063 (*tail)->next = h;
17064 else
17065 *head = h;
17066 h->prev = *tail;
17067 *tail = t;
17068 }
17069 }
17070
17071
17072 /* Prepend the list of glyph strings with head H and tail T to the
17073 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17074 result. */
17075
17076 static INLINE void
17077 prepend_glyph_string_lists (head, tail, h, t)
17078 struct glyph_string **head, **tail;
17079 struct glyph_string *h, *t;
17080 {
17081 if (h)
17082 {
17083 if (*head)
17084 (*head)->prev = t;
17085 else
17086 *tail = t;
17087 t->next = *head;
17088 *head = h;
17089 }
17090 }
17091
17092
17093 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17094 Set *HEAD and *TAIL to the resulting list. */
17095
17096 static INLINE void
17097 append_glyph_string (head, tail, s)
17098 struct glyph_string **head, **tail;
17099 struct glyph_string *s;
17100 {
17101 s->next = s->prev = NULL;
17102 append_glyph_string_lists (head, tail, s, s);
17103 }
17104
17105
17106 /* Get face and two-byte form of character glyph GLYPH on frame F.
17107 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17108 a pointer to a realized face that is ready for display. */
17109
17110 static INLINE struct face *
17111 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17112 struct frame *f;
17113 struct glyph *glyph;
17114 XChar2b *char2b;
17115 int *two_byte_p;
17116 {
17117 struct face *face;
17118
17119 xassert (glyph->type == CHAR_GLYPH);
17120 face = FACE_FROM_ID (f, glyph->face_id);
17121
17122 if (two_byte_p)
17123 *two_byte_p = 0;
17124
17125 if (!glyph->multibyte_p)
17126 {
17127 /* Unibyte case. We don't have to encode, but we have to make
17128 sure to use a face suitable for unibyte. */
17129 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17130 }
17131 else if (glyph->u.ch < 128
17132 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17133 {
17134 /* Case of ASCII in a face known to fit ASCII. */
17135 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17136 }
17137 else
17138 {
17139 int c1, c2, charset;
17140
17141 /* Split characters into bytes. If c2 is -1 afterwards, C is
17142 really a one-byte character so that byte1 is zero. */
17143 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17144 if (c2 > 0)
17145 STORE_XCHAR2B (char2b, c1, c2);
17146 else
17147 STORE_XCHAR2B (char2b, 0, c1);
17148
17149 /* Maybe encode the character in *CHAR2B. */
17150 if (charset != CHARSET_ASCII)
17151 {
17152 struct font_info *font_info
17153 = FONT_INFO_FROM_ID (f, face->font_info_id);
17154 if (font_info)
17155 glyph->font_type
17156 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17157 }
17158 }
17159
17160 /* Make sure X resources of the face are allocated. */
17161 xassert (face != NULL);
17162 PREPARE_FACE_FOR_DISPLAY (f, face);
17163 return face;
17164 }
17165
17166
17167 /* Fill glyph string S with composition components specified by S->cmp.
17168
17169 FACES is an array of faces for all components of this composition.
17170 S->gidx is the index of the first component for S.
17171 OVERLAPS_P non-zero means S should draw the foreground only, and
17172 use its physical height for clipping.
17173
17174 Value is the index of a component not in S. */
17175
17176 static int
17177 fill_composite_glyph_string (s, faces, overlaps_p)
17178 struct glyph_string *s;
17179 struct face **faces;
17180 int overlaps_p;
17181 {
17182 int i;
17183
17184 xassert (s);
17185
17186 s->for_overlaps_p = overlaps_p;
17187
17188 s->face = faces[s->gidx];
17189 s->font = s->face->font;
17190 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17191
17192 /* For all glyphs of this composition, starting at the offset
17193 S->gidx, until we reach the end of the definition or encounter a
17194 glyph that requires the different face, add it to S. */
17195 ++s->nchars;
17196 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17197 ++s->nchars;
17198
17199 /* All glyph strings for the same composition has the same width,
17200 i.e. the width set for the first component of the composition. */
17201
17202 s->width = s->first_glyph->pixel_width;
17203
17204 /* If the specified font could not be loaded, use the frame's
17205 default font, but record the fact that we couldn't load it in
17206 the glyph string so that we can draw rectangles for the
17207 characters of the glyph string. */
17208 if (s->font == NULL)
17209 {
17210 s->font_not_found_p = 1;
17211 s->font = FRAME_FONT (s->f);
17212 }
17213
17214 /* Adjust base line for subscript/superscript text. */
17215 s->ybase += s->first_glyph->voffset;
17216
17217 xassert (s->face && s->face->gc);
17218
17219 /* This glyph string must always be drawn with 16-bit functions. */
17220 s->two_byte_p = 1;
17221
17222 return s->gidx + s->nchars;
17223 }
17224
17225
17226 /* Fill glyph string S from a sequence of character glyphs.
17227
17228 FACE_ID is the face id of the string. START is the index of the
17229 first glyph to consider, END is the index of the last + 1.
17230 OVERLAPS_P non-zero means S should draw the foreground only, and
17231 use its physical height for clipping.
17232
17233 Value is the index of the first glyph not in S. */
17234
17235 static int
17236 fill_glyph_string (s, face_id, start, end, overlaps_p)
17237 struct glyph_string *s;
17238 int face_id;
17239 int start, end, overlaps_p;
17240 {
17241 struct glyph *glyph, *last;
17242 int voffset;
17243 int glyph_not_available_p;
17244
17245 xassert (s->f == XFRAME (s->w->frame));
17246 xassert (s->nchars == 0);
17247 xassert (start >= 0 && end > start);
17248
17249 s->for_overlaps_p = overlaps_p,
17250 glyph = s->row->glyphs[s->area] + start;
17251 last = s->row->glyphs[s->area] + end;
17252 voffset = glyph->voffset;
17253
17254 glyph_not_available_p = glyph->glyph_not_available_p;
17255
17256 while (glyph < last
17257 && glyph->type == CHAR_GLYPH
17258 && glyph->voffset == voffset
17259 /* Same face id implies same font, nowadays. */
17260 && glyph->face_id == face_id
17261 && glyph->glyph_not_available_p == glyph_not_available_p)
17262 {
17263 int two_byte_p;
17264
17265 s->face = get_glyph_face_and_encoding (s->f, glyph,
17266 s->char2b + s->nchars,
17267 &two_byte_p);
17268 s->two_byte_p = two_byte_p;
17269 ++s->nchars;
17270 xassert (s->nchars <= end - start);
17271 s->width += glyph->pixel_width;
17272 ++glyph;
17273 }
17274
17275 s->font = s->face->font;
17276 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17277
17278 /* If the specified font could not be loaded, use the frame's font,
17279 but record the fact that we couldn't load it in
17280 S->font_not_found_p so that we can draw rectangles for the
17281 characters of the glyph string. */
17282 if (s->font == NULL || glyph_not_available_p)
17283 {
17284 s->font_not_found_p = 1;
17285 s->font = FRAME_FONT (s->f);
17286 }
17287
17288 /* Adjust base line for subscript/superscript text. */
17289 s->ybase += voffset;
17290
17291 xassert (s->face && s->face->gc);
17292 return glyph - s->row->glyphs[s->area];
17293 }
17294
17295
17296 /* Fill glyph string S from image glyph S->first_glyph. */
17297
17298 static void
17299 fill_image_glyph_string (s)
17300 struct glyph_string *s;
17301 {
17302 xassert (s->first_glyph->type == IMAGE_GLYPH);
17303 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17304 xassert (s->img);
17305 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17306 s->font = s->face->font;
17307 s->width = s->first_glyph->pixel_width;
17308
17309 /* Adjust base line for subscript/superscript text. */
17310 s->ybase += s->first_glyph->voffset;
17311 }
17312
17313
17314 /* Fill glyph string S from a sequence of stretch glyphs.
17315
17316 ROW is the glyph row in which the glyphs are found, AREA is the
17317 area within the row. START is the index of the first glyph to
17318 consider, END is the index of the last + 1.
17319
17320 Value is the index of the first glyph not in S. */
17321
17322 static int
17323 fill_stretch_glyph_string (s, row, area, start, end)
17324 struct glyph_string *s;
17325 struct glyph_row *row;
17326 enum glyph_row_area area;
17327 int start, end;
17328 {
17329 struct glyph *glyph, *last;
17330 int voffset, face_id;
17331
17332 xassert (s->first_glyph->type == STRETCH_GLYPH);
17333
17334 glyph = s->row->glyphs[s->area] + start;
17335 last = s->row->glyphs[s->area] + end;
17336 face_id = glyph->face_id;
17337 s->face = FACE_FROM_ID (s->f, face_id);
17338 s->font = s->face->font;
17339 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17340 s->width = glyph->pixel_width;
17341 voffset = glyph->voffset;
17342
17343 for (++glyph;
17344 (glyph < last
17345 && glyph->type == STRETCH_GLYPH
17346 && glyph->voffset == voffset
17347 && glyph->face_id == face_id);
17348 ++glyph)
17349 s->width += glyph->pixel_width;
17350
17351 /* Adjust base line for subscript/superscript text. */
17352 s->ybase += voffset;
17353
17354 /* The case that face->gc == 0 is handled when drawing the glyph
17355 string by calling PREPARE_FACE_FOR_DISPLAY. */
17356 xassert (s->face);
17357 return glyph - s->row->glyphs[s->area];
17358 }
17359
17360
17361 /* EXPORT for RIF:
17362 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17363 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17364 assumed to be zero. */
17365
17366 void
17367 x_get_glyph_overhangs (glyph, f, left, right)
17368 struct glyph *glyph;
17369 struct frame *f;
17370 int *left, *right;
17371 {
17372 *left = *right = 0;
17373
17374 if (glyph->type == CHAR_GLYPH)
17375 {
17376 XFontStruct *font;
17377 struct face *face;
17378 struct font_info *font_info;
17379 XChar2b char2b;
17380 XCharStruct *pcm;
17381
17382 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17383 font = face->font;
17384 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17385 if (font /* ++KFS: Should this be font_info ? */
17386 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17387 {
17388 if (pcm->rbearing > pcm->width)
17389 *right = pcm->rbearing - pcm->width;
17390 if (pcm->lbearing < 0)
17391 *left = -pcm->lbearing;
17392 }
17393 }
17394 }
17395
17396
17397 /* Return the index of the first glyph preceding glyph string S that
17398 is overwritten by S because of S's left overhang. Value is -1
17399 if no glyphs are overwritten. */
17400
17401 static int
17402 left_overwritten (s)
17403 struct glyph_string *s;
17404 {
17405 int k;
17406
17407 if (s->left_overhang)
17408 {
17409 int x = 0, i;
17410 struct glyph *glyphs = s->row->glyphs[s->area];
17411 int first = s->first_glyph - glyphs;
17412
17413 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17414 x -= glyphs[i].pixel_width;
17415
17416 k = i + 1;
17417 }
17418 else
17419 k = -1;
17420
17421 return k;
17422 }
17423
17424
17425 /* Return the index of the first glyph preceding glyph string S that
17426 is overwriting S because of its right overhang. Value is -1 if no
17427 glyph in front of S overwrites S. */
17428
17429 static int
17430 left_overwriting (s)
17431 struct glyph_string *s;
17432 {
17433 int i, k, x;
17434 struct glyph *glyphs = s->row->glyphs[s->area];
17435 int first = s->first_glyph - glyphs;
17436
17437 k = -1;
17438 x = 0;
17439 for (i = first - 1; i >= 0; --i)
17440 {
17441 int left, right;
17442 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17443 if (x + right > 0)
17444 k = i;
17445 x -= glyphs[i].pixel_width;
17446 }
17447
17448 return k;
17449 }
17450
17451
17452 /* Return the index of the last glyph following glyph string S that is
17453 not overwritten by S because of S's right overhang. Value is -1 if
17454 no such glyph is found. */
17455
17456 static int
17457 right_overwritten (s)
17458 struct glyph_string *s;
17459 {
17460 int k = -1;
17461
17462 if (s->right_overhang)
17463 {
17464 int x = 0, i;
17465 struct glyph *glyphs = s->row->glyphs[s->area];
17466 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17467 int end = s->row->used[s->area];
17468
17469 for (i = first; i < end && s->right_overhang > x; ++i)
17470 x += glyphs[i].pixel_width;
17471
17472 k = i;
17473 }
17474
17475 return k;
17476 }
17477
17478
17479 /* Return the index of the last glyph following glyph string S that
17480 overwrites S because of its left overhang. Value is negative
17481 if no such glyph is found. */
17482
17483 static int
17484 right_overwriting (s)
17485 struct glyph_string *s;
17486 {
17487 int i, k, x;
17488 int end = s->row->used[s->area];
17489 struct glyph *glyphs = s->row->glyphs[s->area];
17490 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17491
17492 k = -1;
17493 x = 0;
17494 for (i = first; i < end; ++i)
17495 {
17496 int left, right;
17497 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17498 if (x - left < 0)
17499 k = i;
17500 x += glyphs[i].pixel_width;
17501 }
17502
17503 return k;
17504 }
17505
17506
17507 /* Get face and two-byte form of character C in face FACE_ID on frame
17508 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17509 means we want to display multibyte text. DISPLAY_P non-zero means
17510 make sure that X resources for the face returned are allocated.
17511 Value is a pointer to a realized face that is ready for display if
17512 DISPLAY_P is non-zero. */
17513
17514 static INLINE struct face *
17515 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17516 struct frame *f;
17517 int c, face_id;
17518 XChar2b *char2b;
17519 int multibyte_p, display_p;
17520 {
17521 struct face *face = FACE_FROM_ID (f, face_id);
17522
17523 if (!multibyte_p)
17524 {
17525 /* Unibyte case. We don't have to encode, but we have to make
17526 sure to use a face suitable for unibyte. */
17527 STORE_XCHAR2B (char2b, 0, c);
17528 face_id = FACE_FOR_CHAR (f, face, c);
17529 face = FACE_FROM_ID (f, face_id);
17530 }
17531 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17532 {
17533 /* Case of ASCII in a face known to fit ASCII. */
17534 STORE_XCHAR2B (char2b, 0, c);
17535 }
17536 else
17537 {
17538 int c1, c2, charset;
17539
17540 /* Split characters into bytes. If c2 is -1 afterwards, C is
17541 really a one-byte character so that byte1 is zero. */
17542 SPLIT_CHAR (c, charset, c1, c2);
17543 if (c2 > 0)
17544 STORE_XCHAR2B (char2b, c1, c2);
17545 else
17546 STORE_XCHAR2B (char2b, 0, c1);
17547
17548 /* Maybe encode the character in *CHAR2B. */
17549 if (face->font != NULL)
17550 {
17551 struct font_info *font_info
17552 = FONT_INFO_FROM_ID (f, face->font_info_id);
17553 if (font_info)
17554 rif->encode_char (c, char2b, font_info, 0);
17555 }
17556 }
17557
17558 /* Make sure X resources of the face are allocated. */
17559 #ifdef HAVE_X_WINDOWS
17560 if (display_p)
17561 #endif
17562 {
17563 xassert (face != NULL);
17564 PREPARE_FACE_FOR_DISPLAY (f, face);
17565 }
17566
17567 return face;
17568 }
17569
17570
17571 /* Set background width of glyph string S. START is the index of the
17572 first glyph following S. LAST_X is the right-most x-position + 1
17573 in the drawing area. */
17574
17575 static INLINE void
17576 set_glyph_string_background_width (s, start, last_x)
17577 struct glyph_string *s;
17578 int start;
17579 int last_x;
17580 {
17581 /* If the face of this glyph string has to be drawn to the end of
17582 the drawing area, set S->extends_to_end_of_line_p. */
17583 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17584
17585 if (start == s->row->used[s->area]
17586 && s->area == TEXT_AREA
17587 && ((s->hl == DRAW_NORMAL_TEXT
17588 && (s->row->fill_line_p
17589 || s->face->background != default_face->background
17590 || s->face->stipple != default_face->stipple
17591 || s->row->mouse_face_p))
17592 || s->hl == DRAW_MOUSE_FACE
17593 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17594 && s->row->fill_line_p)))
17595 s->extends_to_end_of_line_p = 1;
17596
17597 /* If S extends its face to the end of the line, set its
17598 background_width to the distance to the right edge of the drawing
17599 area. */
17600 if (s->extends_to_end_of_line_p)
17601 s->background_width = last_x - s->x + 1;
17602 else
17603 s->background_width = s->width;
17604 }
17605
17606
17607 /* Compute overhangs and x-positions for glyph string S and its
17608 predecessors, or successors. X is the starting x-position for S.
17609 BACKWARD_P non-zero means process predecessors. */
17610
17611 static void
17612 compute_overhangs_and_x (s, x, backward_p)
17613 struct glyph_string *s;
17614 int x;
17615 int backward_p;
17616 {
17617 if (backward_p)
17618 {
17619 while (s)
17620 {
17621 if (rif->compute_glyph_string_overhangs)
17622 rif->compute_glyph_string_overhangs (s);
17623 x -= s->width;
17624 s->x = x;
17625 s = s->prev;
17626 }
17627 }
17628 else
17629 {
17630 while (s)
17631 {
17632 if (rif->compute_glyph_string_overhangs)
17633 rif->compute_glyph_string_overhangs (s);
17634 s->x = x;
17635 x += s->width;
17636 s = s->next;
17637 }
17638 }
17639 }
17640
17641
17642
17643 /* The following macros are only called from draw_glyphs below.
17644 They reference the following parameters of that function directly:
17645 `w', `row', `area', and `overlap_p'
17646 as well as the following local variables:
17647 `s', `f', and `hdc' (in W32) */
17648
17649 #ifdef HAVE_NTGUI
17650 /* On W32, silently add local `hdc' variable to argument list of
17651 init_glyph_string. */
17652 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17653 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17654 #else
17655 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17656 init_glyph_string (s, char2b, w, row, area, start, hl)
17657 #endif
17658
17659 /* Add a glyph string for a stretch glyph to the list of strings
17660 between HEAD and TAIL. START is the index of the stretch glyph in
17661 row area AREA of glyph row ROW. END is the index of the last glyph
17662 in that glyph row area. X is the current output position assigned
17663 to the new glyph string constructed. HL overrides that face of the
17664 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17665 is the right-most x-position of the drawing area. */
17666
17667 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17668 and below -- keep them on one line. */
17669 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17670 do \
17671 { \
17672 s = (struct glyph_string *) alloca (sizeof *s); \
17673 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17674 START = fill_stretch_glyph_string (s, row, area, START, END); \
17675 append_glyph_string (&HEAD, &TAIL, s); \
17676 s->x = (X); \
17677 } \
17678 while (0)
17679
17680
17681 /* Add a glyph string for an image glyph to the list of strings
17682 between HEAD and TAIL. START is the index of the image glyph in
17683 row area AREA of glyph row ROW. END is the index of the last glyph
17684 in that glyph row area. X is the current output position assigned
17685 to the new glyph string constructed. HL overrides that face of the
17686 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17687 is the right-most x-position of the drawing area. */
17688
17689 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17690 do \
17691 { \
17692 s = (struct glyph_string *) alloca (sizeof *s); \
17693 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17694 fill_image_glyph_string (s); \
17695 append_glyph_string (&HEAD, &TAIL, s); \
17696 ++START; \
17697 s->x = (X); \
17698 } \
17699 while (0)
17700
17701
17702 /* Add a glyph string for a sequence of character glyphs to the list
17703 of strings between HEAD and TAIL. START is the index of the first
17704 glyph in row area AREA of glyph row ROW that is part of the new
17705 glyph string. END is the index of the last glyph in that glyph row
17706 area. X is the current output position assigned to the new glyph
17707 string constructed. HL overrides that face of the glyph; e.g. it
17708 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17709 right-most x-position of the drawing area. */
17710
17711 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17712 do \
17713 { \
17714 int c, face_id; \
17715 XChar2b *char2b; \
17716 \
17717 c = (row)->glyphs[area][START].u.ch; \
17718 face_id = (row)->glyphs[area][START].face_id; \
17719 \
17720 s = (struct glyph_string *) alloca (sizeof *s); \
17721 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17722 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17723 append_glyph_string (&HEAD, &TAIL, s); \
17724 s->x = (X); \
17725 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17726 } \
17727 while (0)
17728
17729
17730 /* Add a glyph string for a composite sequence to the list of strings
17731 between HEAD and TAIL. START is the index of the first glyph in
17732 row area AREA of glyph row ROW that is part of the new glyph
17733 string. END is the index of the last glyph in that glyph row area.
17734 X is the current output position assigned to the new glyph string
17735 constructed. HL overrides that face of the glyph; e.g. it is
17736 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17737 x-position of the drawing area. */
17738
17739 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17740 do { \
17741 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17742 int face_id = (row)->glyphs[area][START].face_id; \
17743 struct face *base_face = FACE_FROM_ID (f, face_id); \
17744 struct composition *cmp = composition_table[cmp_id]; \
17745 int glyph_len = cmp->glyph_len; \
17746 XChar2b *char2b; \
17747 struct face **faces; \
17748 struct glyph_string *first_s = NULL; \
17749 int n; \
17750 \
17751 base_face = base_face->ascii_face; \
17752 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17753 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17754 /* At first, fill in `char2b' and `faces'. */ \
17755 for (n = 0; n < glyph_len; n++) \
17756 { \
17757 int c = COMPOSITION_GLYPH (cmp, n); \
17758 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17759 faces[n] = FACE_FROM_ID (f, this_face_id); \
17760 get_char_face_and_encoding (f, c, this_face_id, \
17761 char2b + n, 1, 1); \
17762 } \
17763 \
17764 /* Make glyph_strings for each glyph sequence that is drawable by \
17765 the same face, and append them to HEAD/TAIL. */ \
17766 for (n = 0; n < cmp->glyph_len;) \
17767 { \
17768 s = (struct glyph_string *) alloca (sizeof *s); \
17769 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17770 append_glyph_string (&(HEAD), &(TAIL), s); \
17771 s->cmp = cmp; \
17772 s->gidx = n; \
17773 s->x = (X); \
17774 \
17775 if (n == 0) \
17776 first_s = s; \
17777 \
17778 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17779 } \
17780 \
17781 ++START; \
17782 s = first_s; \
17783 } while (0)
17784
17785
17786 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17787 of AREA of glyph row ROW on window W between indices START and END.
17788 HL overrides the face for drawing glyph strings, e.g. it is
17789 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17790 x-positions of the drawing area.
17791
17792 This is an ugly monster macro construct because we must use alloca
17793 to allocate glyph strings (because draw_glyphs can be called
17794 asynchronously). */
17795
17796 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17797 do \
17798 { \
17799 HEAD = TAIL = NULL; \
17800 while (START < END) \
17801 { \
17802 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17803 switch (first_glyph->type) \
17804 { \
17805 case CHAR_GLYPH: \
17806 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17807 HL, X, LAST_X); \
17808 break; \
17809 \
17810 case COMPOSITE_GLYPH: \
17811 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17812 HL, X, LAST_X); \
17813 break; \
17814 \
17815 case STRETCH_GLYPH: \
17816 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17817 HL, X, LAST_X); \
17818 break; \
17819 \
17820 case IMAGE_GLYPH: \
17821 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17822 HL, X, LAST_X); \
17823 break; \
17824 \
17825 default: \
17826 abort (); \
17827 } \
17828 \
17829 set_glyph_string_background_width (s, START, LAST_X); \
17830 (X) += s->width; \
17831 } \
17832 } \
17833 while (0)
17834
17835
17836 /* Draw glyphs between START and END in AREA of ROW on window W,
17837 starting at x-position X. X is relative to AREA in W. HL is a
17838 face-override with the following meaning:
17839
17840 DRAW_NORMAL_TEXT draw normally
17841 DRAW_CURSOR draw in cursor face
17842 DRAW_MOUSE_FACE draw in mouse face.
17843 DRAW_INVERSE_VIDEO draw in mode line face
17844 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17845 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17846
17847 If OVERLAPS_P is non-zero, draw only the foreground of characters
17848 and clip to the physical height of ROW.
17849
17850 Value is the x-position reached, relative to AREA of W. */
17851
17852 static int
17853 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17854 struct window *w;
17855 int x;
17856 struct glyph_row *row;
17857 enum glyph_row_area area;
17858 int start, end;
17859 enum draw_glyphs_face hl;
17860 int overlaps_p;
17861 {
17862 struct glyph_string *head, *tail;
17863 struct glyph_string *s;
17864 int last_x, area_width;
17865 int x_reached;
17866 int i, j;
17867 struct frame *f = XFRAME (WINDOW_FRAME (w));
17868 DECLARE_HDC (hdc);
17869
17870 ALLOCATE_HDC (hdc, f);
17871
17872 /* Let's rather be paranoid than getting a SEGV. */
17873 end = min (end, row->used[area]);
17874 start = max (0, start);
17875 start = min (end, start);
17876
17877 /* Translate X to frame coordinates. Set last_x to the right
17878 end of the drawing area. */
17879 if (row->full_width_p)
17880 {
17881 /* X is relative to the left edge of W, without scroll bars
17882 or fringes. */
17883 x += WINDOW_LEFT_EDGE_X (w);
17884 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17885 }
17886 else
17887 {
17888 int area_left = window_box_left (w, area);
17889 x += area_left;
17890 area_width = window_box_width (w, area);
17891 last_x = area_left + area_width;
17892 }
17893
17894 /* Build a doubly-linked list of glyph_string structures between
17895 head and tail from what we have to draw. Note that the macro
17896 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17897 the reason we use a separate variable `i'. */
17898 i = start;
17899 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17900 if (tail)
17901 x_reached = tail->x + tail->background_width;
17902 else
17903 x_reached = x;
17904
17905 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17906 the row, redraw some glyphs in front or following the glyph
17907 strings built above. */
17908 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17909 {
17910 int dummy_x = 0;
17911 struct glyph_string *h, *t;
17912
17913 /* Compute overhangs for all glyph strings. */
17914 if (rif->compute_glyph_string_overhangs)
17915 for (s = head; s; s = s->next)
17916 rif->compute_glyph_string_overhangs (s);
17917
17918 /* Prepend glyph strings for glyphs in front of the first glyph
17919 string that are overwritten because of the first glyph
17920 string's left overhang. The background of all strings
17921 prepended must be drawn because the first glyph string
17922 draws over it. */
17923 i = left_overwritten (head);
17924 if (i >= 0)
17925 {
17926 j = i;
17927 BUILD_GLYPH_STRINGS (j, start, h, t,
17928 DRAW_NORMAL_TEXT, dummy_x, last_x);
17929 start = i;
17930 compute_overhangs_and_x (t, head->x, 1);
17931 prepend_glyph_string_lists (&head, &tail, h, t);
17932 }
17933
17934 /* Prepend glyph strings for glyphs in front of the first glyph
17935 string that overwrite that glyph string because of their
17936 right overhang. For these strings, only the foreground must
17937 be drawn, because it draws over the glyph string at `head'.
17938 The background must not be drawn because this would overwrite
17939 right overhangs of preceding glyphs for which no glyph
17940 strings exist. */
17941 i = left_overwriting (head);
17942 if (i >= 0)
17943 {
17944 BUILD_GLYPH_STRINGS (i, start, h, t,
17945 DRAW_NORMAL_TEXT, dummy_x, last_x);
17946 for (s = h; s; s = s->next)
17947 s->background_filled_p = 1;
17948 compute_overhangs_and_x (t, head->x, 1);
17949 prepend_glyph_string_lists (&head, &tail, h, t);
17950 }
17951
17952 /* Append glyphs strings for glyphs following the last glyph
17953 string tail that are overwritten by tail. The background of
17954 these strings has to be drawn because tail's foreground draws
17955 over it. */
17956 i = right_overwritten (tail);
17957 if (i >= 0)
17958 {
17959 BUILD_GLYPH_STRINGS (end, i, h, t,
17960 DRAW_NORMAL_TEXT, x, last_x);
17961 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17962 append_glyph_string_lists (&head, &tail, h, t);
17963 }
17964
17965 /* Append glyph strings for glyphs following the last glyph
17966 string tail that overwrite tail. The foreground of such
17967 glyphs has to be drawn because it writes into the background
17968 of tail. The background must not be drawn because it could
17969 paint over the foreground of following glyphs. */
17970 i = right_overwriting (tail);
17971 if (i >= 0)
17972 {
17973 BUILD_GLYPH_STRINGS (end, i, h, t,
17974 DRAW_NORMAL_TEXT, x, last_x);
17975 for (s = h; s; s = s->next)
17976 s->background_filled_p = 1;
17977 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17978 append_glyph_string_lists (&head, &tail, h, t);
17979 }
17980 }
17981
17982 /* Draw all strings. */
17983 for (s = head; s; s = s->next)
17984 rif->draw_glyph_string (s);
17985
17986 if (area == TEXT_AREA
17987 && !row->full_width_p
17988 /* When drawing overlapping rows, only the glyph strings'
17989 foreground is drawn, which doesn't erase a cursor
17990 completely. */
17991 && !overlaps_p)
17992 {
17993 int x0 = head ? head->x : x;
17994 int x1 = tail ? tail->x + tail->background_width : x;
17995
17996 int text_left = window_box_left (w, TEXT_AREA);
17997 x0 -= text_left;
17998 x1 -= text_left;
17999
18000 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18001 row->y, MATRIX_ROW_BOTTOM_Y (row));
18002 }
18003
18004 /* Value is the x-position up to which drawn, relative to AREA of W.
18005 This doesn't include parts drawn because of overhangs. */
18006 if (row->full_width_p)
18007 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18008 else
18009 x_reached -= window_box_left (w, area);
18010
18011 RELEASE_HDC (hdc, f);
18012
18013 return x_reached;
18014 }
18015
18016
18017 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18018 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18019
18020 static INLINE void
18021 append_glyph (it)
18022 struct it *it;
18023 {
18024 struct glyph *glyph;
18025 enum glyph_row_area area = it->area;
18026
18027 xassert (it->glyph_row);
18028 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18029
18030 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18031 if (glyph < it->glyph_row->glyphs[area + 1])
18032 {
18033 glyph->charpos = CHARPOS (it->position);
18034 glyph->object = it->object;
18035 glyph->pixel_width = it->pixel_width;
18036 glyph->ascent = it->ascent;
18037 glyph->descent = it->descent;
18038 glyph->voffset = it->voffset;
18039 glyph->type = CHAR_GLYPH;
18040 glyph->multibyte_p = it->multibyte_p;
18041 glyph->left_box_line_p = it->start_of_box_run_p;
18042 glyph->right_box_line_p = it->end_of_box_run_p;
18043 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18044 || it->phys_descent > it->descent);
18045 glyph->padding_p = 0;
18046 glyph->glyph_not_available_p = it->glyph_not_available_p;
18047 glyph->face_id = it->face_id;
18048 glyph->u.ch = it->char_to_display;
18049 glyph->font_type = FONT_TYPE_UNKNOWN;
18050 ++it->glyph_row->used[area];
18051 }
18052 }
18053
18054 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18055 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18056
18057 static INLINE void
18058 append_composite_glyph (it)
18059 struct it *it;
18060 {
18061 struct glyph *glyph;
18062 enum glyph_row_area area = it->area;
18063
18064 xassert (it->glyph_row);
18065
18066 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18067 if (glyph < it->glyph_row->glyphs[area + 1])
18068 {
18069 glyph->charpos = CHARPOS (it->position);
18070 glyph->object = it->object;
18071 glyph->pixel_width = it->pixel_width;
18072 glyph->ascent = it->ascent;
18073 glyph->descent = it->descent;
18074 glyph->voffset = it->voffset;
18075 glyph->type = COMPOSITE_GLYPH;
18076 glyph->multibyte_p = it->multibyte_p;
18077 glyph->left_box_line_p = it->start_of_box_run_p;
18078 glyph->right_box_line_p = it->end_of_box_run_p;
18079 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18080 || it->phys_descent > it->descent);
18081 glyph->padding_p = 0;
18082 glyph->glyph_not_available_p = 0;
18083 glyph->face_id = it->face_id;
18084 glyph->u.cmp_id = it->cmp_id;
18085 glyph->font_type = FONT_TYPE_UNKNOWN;
18086 ++it->glyph_row->used[area];
18087 }
18088 }
18089
18090
18091 /* Change IT->ascent and IT->height according to the setting of
18092 IT->voffset. */
18093
18094 static INLINE void
18095 take_vertical_position_into_account (it)
18096 struct it *it;
18097 {
18098 if (it->voffset)
18099 {
18100 if (it->voffset < 0)
18101 /* Increase the ascent so that we can display the text higher
18102 in the line. */
18103 it->ascent += abs (it->voffset);
18104 else
18105 /* Increase the descent so that we can display the text lower
18106 in the line. */
18107 it->descent += it->voffset;
18108 }
18109 }
18110
18111
18112 /* Produce glyphs/get display metrics for the image IT is loaded with.
18113 See the description of struct display_iterator in dispextern.h for
18114 an overview of struct display_iterator. */
18115
18116 static void
18117 produce_image_glyph (it)
18118 struct it *it;
18119 {
18120 struct image *img;
18121 struct face *face;
18122 int face_ascent, glyph_ascent;
18123
18124 xassert (it->what == IT_IMAGE);
18125
18126 face = FACE_FROM_ID (it->f, it->face_id);
18127 xassert (face);
18128 /* Make sure X resources of the face is loaded. */
18129 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18130
18131 if (it->image_id < 0)
18132 {
18133 /* Fringe bitmap. */
18134 it->ascent = it->phys_ascent = 0;
18135 it->descent = it->phys_descent = 0;
18136 it->pixel_width = 0;
18137 it->nglyphs = 0;
18138 return;
18139 }
18140
18141 img = IMAGE_FROM_ID (it->f, it->image_id);
18142 xassert (img);
18143 /* Make sure X resources of the image is loaded. */
18144 prepare_image_for_display (it->f, img);
18145
18146 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face);
18147 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
18148 it->pixel_width = img->width + 2 * img->hmargin;
18149
18150 /* It's quite possible for images to have an ascent greater than
18151 their height, so don't get confused in that case. */
18152 if (it->descent < 0)
18153 it->descent = 0;
18154
18155 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18156 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18157 if (face_ascent > it->ascent)
18158 it->ascent = it->phys_ascent = face_ascent;
18159
18160 it->nglyphs = 1;
18161
18162 if (face->box != FACE_NO_BOX)
18163 {
18164 if (face->box_line_width > 0)
18165 {
18166 it->ascent += face->box_line_width;
18167 it->descent += face->box_line_width;
18168 }
18169
18170 if (it->start_of_box_run_p)
18171 it->pixel_width += abs (face->box_line_width);
18172 if (it->end_of_box_run_p)
18173 it->pixel_width += abs (face->box_line_width);
18174 }
18175
18176 take_vertical_position_into_account (it);
18177
18178 if (it->glyph_row)
18179 {
18180 struct glyph *glyph;
18181 enum glyph_row_area area = it->area;
18182
18183 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18184 if (glyph < it->glyph_row->glyphs[area + 1])
18185 {
18186 glyph->charpos = CHARPOS (it->position);
18187 glyph->object = it->object;
18188 glyph->pixel_width = it->pixel_width;
18189 glyph->ascent = glyph_ascent;
18190 glyph->descent = it->descent;
18191 glyph->voffset = it->voffset;
18192 glyph->type = IMAGE_GLYPH;
18193 glyph->multibyte_p = it->multibyte_p;
18194 glyph->left_box_line_p = it->start_of_box_run_p;
18195 glyph->right_box_line_p = it->end_of_box_run_p;
18196 glyph->overlaps_vertically_p = 0;
18197 glyph->padding_p = 0;
18198 glyph->glyph_not_available_p = 0;
18199 glyph->face_id = it->face_id;
18200 glyph->u.img_id = img->id;
18201 glyph->font_type = FONT_TYPE_UNKNOWN;
18202 ++it->glyph_row->used[area];
18203 }
18204 }
18205 }
18206
18207
18208 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18209 of the glyph, WIDTH and HEIGHT are the width and height of the
18210 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18211
18212 static void
18213 append_stretch_glyph (it, object, width, height, ascent)
18214 struct it *it;
18215 Lisp_Object object;
18216 int width, height;
18217 int ascent;
18218 {
18219 struct glyph *glyph;
18220 enum glyph_row_area area = it->area;
18221
18222 xassert (ascent >= 0 && ascent <= height);
18223
18224 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18225 if (glyph < it->glyph_row->glyphs[area + 1])
18226 {
18227 glyph->charpos = CHARPOS (it->position);
18228 glyph->object = object;
18229 glyph->pixel_width = width;
18230 glyph->ascent = ascent;
18231 glyph->descent = height - ascent;
18232 glyph->voffset = it->voffset;
18233 glyph->type = STRETCH_GLYPH;
18234 glyph->multibyte_p = it->multibyte_p;
18235 glyph->left_box_line_p = it->start_of_box_run_p;
18236 glyph->right_box_line_p = it->end_of_box_run_p;
18237 glyph->overlaps_vertically_p = 0;
18238 glyph->padding_p = 0;
18239 glyph->glyph_not_available_p = 0;
18240 glyph->face_id = it->face_id;
18241 glyph->u.stretch.ascent = ascent;
18242 glyph->u.stretch.height = height;
18243 glyph->font_type = FONT_TYPE_UNKNOWN;
18244 ++it->glyph_row->used[area];
18245 }
18246 }
18247
18248
18249 /* Produce a stretch glyph for iterator IT. IT->object is the value
18250 of the glyph property displayed. The value must be a list
18251 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18252 being recognized:
18253
18254 1. `:width WIDTH' specifies that the space should be WIDTH *
18255 canonical char width wide. WIDTH may be an integer or floating
18256 point number.
18257
18258 2. `:relative-width FACTOR' specifies that the width of the stretch
18259 should be computed from the width of the first character having the
18260 `glyph' property, and should be FACTOR times that width.
18261
18262 3. `:align-to HPOS' specifies that the space should be wide enough
18263 to reach HPOS, a value in canonical character units.
18264
18265 Exactly one of the above pairs must be present.
18266
18267 4. `:height HEIGHT' specifies that the height of the stretch produced
18268 should be HEIGHT, measured in canonical character units.
18269
18270 5. `:relative-height FACTOR' specifies that the height of the
18271 stretch should be FACTOR times the height of the characters having
18272 the glyph property.
18273
18274 Either none or exactly one of 4 or 5 must be present.
18275
18276 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18277 of the stretch should be used for the ascent of the stretch.
18278 ASCENT must be in the range 0 <= ASCENT <= 100. */
18279
18280 static void
18281 produce_stretch_glyph (it)
18282 struct it *it;
18283 {
18284 /* (space :width WIDTH :height HEIGHT ...) */
18285 Lisp_Object prop, plist;
18286 int width = 0, height = 0, align_to = -1;
18287 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18288 int ascent = 0;
18289 double tem;
18290 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18291 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18292
18293 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18294
18295 /* List should start with `space'. */
18296 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18297 plist = XCDR (it->object);
18298
18299 /* Compute the width of the stretch. */
18300 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18301 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18302 {
18303 /* Absolute width `:width WIDTH' specified and valid. */
18304 zero_width_ok_p = 1;
18305 width = (int)tem;
18306 }
18307 else if (prop = Fplist_get (plist, QCrelative_width),
18308 NUMVAL (prop) > 0)
18309 {
18310 /* Relative width `:relative-width FACTOR' specified and valid.
18311 Compute the width of the characters having the `glyph'
18312 property. */
18313 struct it it2;
18314 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18315
18316 it2 = *it;
18317 if (it->multibyte_p)
18318 {
18319 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18320 - IT_BYTEPOS (*it));
18321 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18322 }
18323 else
18324 it2.c = *p, it2.len = 1;
18325
18326 it2.glyph_row = NULL;
18327 it2.what = IT_CHARACTER;
18328 x_produce_glyphs (&it2);
18329 width = NUMVAL (prop) * it2.pixel_width;
18330 }
18331 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18332 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18333 {
18334 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18335 align_to = (align_to < 0
18336 ? 0
18337 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18338 else if (align_to < 0)
18339 align_to = window_box_left_offset (it->w, TEXT_AREA);
18340 width = max (0, (int)tem + align_to - it->current_x);
18341 zero_width_ok_p = 1;
18342 }
18343 else
18344 /* Nothing specified -> width defaults to canonical char width. */
18345 width = FRAME_COLUMN_WIDTH (it->f);
18346
18347 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18348 width = 1;
18349
18350 /* Compute height. */
18351 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18352 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18353 {
18354 height = (int)tem;
18355 zero_height_ok_p = 1;
18356 }
18357 else if (prop = Fplist_get (plist, QCrelative_height),
18358 NUMVAL (prop) > 0)
18359 height = FONT_HEIGHT (font) * NUMVAL (prop);
18360 else
18361 height = FONT_HEIGHT (font);
18362
18363 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18364 height = 1;
18365
18366 /* Compute percentage of height used for ascent. If
18367 `:ascent ASCENT' is present and valid, use that. Otherwise,
18368 derive the ascent from the font in use. */
18369 if (prop = Fplist_get (plist, QCascent),
18370 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18371 ascent = height * NUMVAL (prop) / 100.0;
18372 else if (!NILP (prop)
18373 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18374 ascent = min (max (0, (int)tem), height);
18375 else
18376 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18377
18378 if (width > 0 && height > 0 && it->glyph_row)
18379 {
18380 Lisp_Object object = it->stack[it->sp - 1].string;
18381 if (!STRINGP (object))
18382 object = it->w->buffer;
18383 append_stretch_glyph (it, object, width, height, ascent);
18384 }
18385
18386 it->pixel_width = width;
18387 it->ascent = it->phys_ascent = ascent;
18388 it->descent = it->phys_descent = height - it->ascent;
18389 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18390
18391 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18392 {
18393 if (face->box_line_width > 0)
18394 {
18395 it->ascent += face->box_line_width;
18396 it->descent += face->box_line_width;
18397 }
18398
18399 if (it->start_of_box_run_p)
18400 it->pixel_width += abs (face->box_line_width);
18401 if (it->end_of_box_run_p)
18402 it->pixel_width += abs (face->box_line_width);
18403 }
18404
18405 take_vertical_position_into_account (it);
18406 }
18407
18408 /* RIF:
18409 Produce glyphs/get display metrics for the display element IT is
18410 loaded with. See the description of struct display_iterator in
18411 dispextern.h for an overview of struct display_iterator. */
18412
18413 void
18414 x_produce_glyphs (it)
18415 struct it *it;
18416 {
18417 it->glyph_not_available_p = 0;
18418
18419 if (it->what == IT_CHARACTER)
18420 {
18421 XChar2b char2b;
18422 XFontStruct *font;
18423 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18424 XCharStruct *pcm;
18425 int font_not_found_p;
18426 struct font_info *font_info;
18427 int boff; /* baseline offset */
18428 /* We may change it->multibyte_p upon unibyte<->multibyte
18429 conversion. So, save the current value now and restore it
18430 later.
18431
18432 Note: It seems that we don't have to record multibyte_p in
18433 struct glyph because the character code itself tells if or
18434 not the character is multibyte. Thus, in the future, we must
18435 consider eliminating the field `multibyte_p' in the struct
18436 glyph. */
18437 int saved_multibyte_p = it->multibyte_p;
18438
18439 /* Maybe translate single-byte characters to multibyte, or the
18440 other way. */
18441 it->char_to_display = it->c;
18442 if (!ASCII_BYTE_P (it->c))
18443 {
18444 if (unibyte_display_via_language_environment
18445 && SINGLE_BYTE_CHAR_P (it->c)
18446 && (it->c >= 0240
18447 || !NILP (Vnonascii_translation_table)))
18448 {
18449 it->char_to_display = unibyte_char_to_multibyte (it->c);
18450 it->multibyte_p = 1;
18451 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18452 face = FACE_FROM_ID (it->f, it->face_id);
18453 }
18454 else if (!SINGLE_BYTE_CHAR_P (it->c)
18455 && !it->multibyte_p)
18456 {
18457 it->multibyte_p = 1;
18458 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18459 face = FACE_FROM_ID (it->f, it->face_id);
18460 }
18461 }
18462
18463 /* Get font to use. Encode IT->char_to_display. */
18464 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18465 &char2b, it->multibyte_p, 0);
18466 font = face->font;
18467
18468 /* When no suitable font found, use the default font. */
18469 font_not_found_p = font == NULL;
18470 if (font_not_found_p)
18471 {
18472 font = FRAME_FONT (it->f);
18473 boff = FRAME_BASELINE_OFFSET (it->f);
18474 font_info = NULL;
18475 }
18476 else
18477 {
18478 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18479 boff = font_info->baseline_offset;
18480 if (font_info->vertical_centering)
18481 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18482 }
18483
18484 if (it->char_to_display >= ' '
18485 && (!it->multibyte_p || it->char_to_display < 128))
18486 {
18487 /* Either unibyte or ASCII. */
18488 int stretched_p;
18489
18490 it->nglyphs = 1;
18491
18492 pcm = rif->per_char_metric (font, &char2b,
18493 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18494 it->ascent = FONT_BASE (font) + boff;
18495 it->descent = FONT_DESCENT (font) - boff;
18496
18497 if (pcm)
18498 {
18499 it->phys_ascent = pcm->ascent + boff;
18500 it->phys_descent = pcm->descent - boff;
18501 it->pixel_width = pcm->width;
18502 }
18503 else
18504 {
18505 it->glyph_not_available_p = 1;
18506 it->phys_ascent = FONT_BASE (font) + boff;
18507 it->phys_descent = FONT_DESCENT (font) - boff;
18508 it->pixel_width = FONT_WIDTH (font);
18509 }
18510
18511 /* If this is a space inside a region of text with
18512 `space-width' property, change its width. */
18513 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18514 if (stretched_p)
18515 it->pixel_width *= XFLOATINT (it->space_width);
18516
18517 /* If face has a box, add the box thickness to the character
18518 height. If character has a box line to the left and/or
18519 right, add the box line width to the character's width. */
18520 if (face->box != FACE_NO_BOX)
18521 {
18522 int thick = face->box_line_width;
18523
18524 if (thick > 0)
18525 {
18526 it->ascent += thick;
18527 it->descent += thick;
18528 }
18529 else
18530 thick = -thick;
18531
18532 if (it->start_of_box_run_p)
18533 it->pixel_width += thick;
18534 if (it->end_of_box_run_p)
18535 it->pixel_width += thick;
18536 }
18537
18538 /* If face has an overline, add the height of the overline
18539 (1 pixel) and a 1 pixel margin to the character height. */
18540 if (face->overline_p)
18541 it->ascent += 2;
18542
18543 take_vertical_position_into_account (it);
18544
18545 /* If we have to actually produce glyphs, do it. */
18546 if (it->glyph_row)
18547 {
18548 if (stretched_p)
18549 {
18550 /* Translate a space with a `space-width' property
18551 into a stretch glyph. */
18552 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18553 / FONT_HEIGHT (font));
18554 append_stretch_glyph (it, it->object, it->pixel_width,
18555 it->ascent + it->descent, ascent);
18556 }
18557 else
18558 append_glyph (it);
18559
18560 /* If characters with lbearing or rbearing are displayed
18561 in this line, record that fact in a flag of the
18562 glyph row. This is used to optimize X output code. */
18563 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18564 it->glyph_row->contains_overlapping_glyphs_p = 1;
18565 }
18566 }
18567 else if (it->char_to_display == '\n')
18568 {
18569 /* A newline has no width but we need the height of the line. */
18570 it->pixel_width = 0;
18571 it->nglyphs = 0;
18572 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18573 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18574
18575 if (face->box != FACE_NO_BOX
18576 && face->box_line_width > 0)
18577 {
18578 it->ascent += face->box_line_width;
18579 it->descent += face->box_line_width;
18580 }
18581 }
18582 else if (it->char_to_display == '\t')
18583 {
18584 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18585 int x = it->current_x + it->continuation_lines_width;
18586 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18587
18588 /* If the distance from the current position to the next tab
18589 stop is less than a canonical character width, use the
18590 tab stop after that. */
18591 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18592 next_tab_x += tab_width;
18593
18594 it->pixel_width = next_tab_x - x;
18595 it->nglyphs = 1;
18596 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18597 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18598
18599 if (it->glyph_row)
18600 {
18601 append_stretch_glyph (it, it->object, it->pixel_width,
18602 it->ascent + it->descent, it->ascent);
18603 }
18604 }
18605 else
18606 {
18607 /* A multi-byte character. Assume that the display width of the
18608 character is the width of the character multiplied by the
18609 width of the font. */
18610
18611 /* If we found a font, this font should give us the right
18612 metrics. If we didn't find a font, use the frame's
18613 default font and calculate the width of the character
18614 from the charset width; this is what old redisplay code
18615 did. */
18616
18617 pcm = rif->per_char_metric (font, &char2b,
18618 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18619
18620 if (font_not_found_p || !pcm)
18621 {
18622 int charset = CHAR_CHARSET (it->char_to_display);
18623
18624 it->glyph_not_available_p = 1;
18625 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18626 * CHARSET_WIDTH (charset));
18627 it->phys_ascent = FONT_BASE (font) + boff;
18628 it->phys_descent = FONT_DESCENT (font) - boff;
18629 }
18630 else
18631 {
18632 it->pixel_width = pcm->width;
18633 it->phys_ascent = pcm->ascent + boff;
18634 it->phys_descent = pcm->descent - boff;
18635 if (it->glyph_row
18636 && (pcm->lbearing < 0
18637 || pcm->rbearing > pcm->width))
18638 it->glyph_row->contains_overlapping_glyphs_p = 1;
18639 }
18640 it->nglyphs = 1;
18641 it->ascent = FONT_BASE (font) + boff;
18642 it->descent = FONT_DESCENT (font) - boff;
18643 if (face->box != FACE_NO_BOX)
18644 {
18645 int thick = face->box_line_width;
18646
18647 if (thick > 0)
18648 {
18649 it->ascent += thick;
18650 it->descent += thick;
18651 }
18652 else
18653 thick = - thick;
18654
18655 if (it->start_of_box_run_p)
18656 it->pixel_width += thick;
18657 if (it->end_of_box_run_p)
18658 it->pixel_width += thick;
18659 }
18660
18661 /* If face has an overline, add the height of the overline
18662 (1 pixel) and a 1 pixel margin to the character height. */
18663 if (face->overline_p)
18664 it->ascent += 2;
18665
18666 take_vertical_position_into_account (it);
18667
18668 if (it->glyph_row)
18669 append_glyph (it);
18670 }
18671 it->multibyte_p = saved_multibyte_p;
18672 }
18673 else if (it->what == IT_COMPOSITION)
18674 {
18675 /* Note: A composition is represented as one glyph in the
18676 glyph matrix. There are no padding glyphs. */
18677 XChar2b char2b;
18678 XFontStruct *font;
18679 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18680 XCharStruct *pcm;
18681 int font_not_found_p;
18682 struct font_info *font_info;
18683 int boff; /* baseline offset */
18684 struct composition *cmp = composition_table[it->cmp_id];
18685
18686 /* Maybe translate single-byte characters to multibyte. */
18687 it->char_to_display = it->c;
18688 if (unibyte_display_via_language_environment
18689 && SINGLE_BYTE_CHAR_P (it->c)
18690 && (it->c >= 0240
18691 || (it->c >= 0200
18692 && !NILP (Vnonascii_translation_table))))
18693 {
18694 it->char_to_display = unibyte_char_to_multibyte (it->c);
18695 }
18696
18697 /* Get face and font to use. Encode IT->char_to_display. */
18698 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18699 face = FACE_FROM_ID (it->f, it->face_id);
18700 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18701 &char2b, it->multibyte_p, 0);
18702 font = face->font;
18703
18704 /* When no suitable font found, use the default font. */
18705 font_not_found_p = font == NULL;
18706 if (font_not_found_p)
18707 {
18708 font = FRAME_FONT (it->f);
18709 boff = FRAME_BASELINE_OFFSET (it->f);
18710 font_info = NULL;
18711 }
18712 else
18713 {
18714 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18715 boff = font_info->baseline_offset;
18716 if (font_info->vertical_centering)
18717 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18718 }
18719
18720 /* There are no padding glyphs, so there is only one glyph to
18721 produce for the composition. Important is that pixel_width,
18722 ascent and descent are the values of what is drawn by
18723 draw_glyphs (i.e. the values of the overall glyphs composed). */
18724 it->nglyphs = 1;
18725
18726 /* If we have not yet calculated pixel size data of glyphs of
18727 the composition for the current face font, calculate them
18728 now. Theoretically, we have to check all fonts for the
18729 glyphs, but that requires much time and memory space. So,
18730 here we check only the font of the first glyph. This leads
18731 to incorrect display very rarely, and C-l (recenter) can
18732 correct the display anyway. */
18733 if (cmp->font != (void *) font)
18734 {
18735 /* Ascent and descent of the font of the first character of
18736 this composition (adjusted by baseline offset). Ascent
18737 and descent of overall glyphs should not be less than
18738 them respectively. */
18739 int font_ascent = FONT_BASE (font) + boff;
18740 int font_descent = FONT_DESCENT (font) - boff;
18741 /* Bounding box of the overall glyphs. */
18742 int leftmost, rightmost, lowest, highest;
18743 int i, width, ascent, descent;
18744
18745 cmp->font = (void *) font;
18746
18747 /* Initialize the bounding box. */
18748 if (font_info
18749 && (pcm = rif->per_char_metric (font, &char2b,
18750 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18751 {
18752 width = pcm->width;
18753 ascent = pcm->ascent;
18754 descent = pcm->descent;
18755 }
18756 else
18757 {
18758 width = FONT_WIDTH (font);
18759 ascent = FONT_BASE (font);
18760 descent = FONT_DESCENT (font);
18761 }
18762
18763 rightmost = width;
18764 lowest = - descent + boff;
18765 highest = ascent + boff;
18766 leftmost = 0;
18767
18768 if (font_info
18769 && font_info->default_ascent
18770 && CHAR_TABLE_P (Vuse_default_ascent)
18771 && !NILP (Faref (Vuse_default_ascent,
18772 make_number (it->char_to_display))))
18773 highest = font_info->default_ascent + boff;
18774
18775 /* Draw the first glyph at the normal position. It may be
18776 shifted to right later if some other glyphs are drawn at
18777 the left. */
18778 cmp->offsets[0] = 0;
18779 cmp->offsets[1] = boff;
18780
18781 /* Set cmp->offsets for the remaining glyphs. */
18782 for (i = 1; i < cmp->glyph_len; i++)
18783 {
18784 int left, right, btm, top;
18785 int ch = COMPOSITION_GLYPH (cmp, i);
18786 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18787
18788 face = FACE_FROM_ID (it->f, face_id);
18789 get_char_face_and_encoding (it->f, ch, face->id,
18790 &char2b, it->multibyte_p, 0);
18791 font = face->font;
18792 if (font == NULL)
18793 {
18794 font = FRAME_FONT (it->f);
18795 boff = FRAME_BASELINE_OFFSET (it->f);
18796 font_info = NULL;
18797 }
18798 else
18799 {
18800 font_info
18801 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18802 boff = font_info->baseline_offset;
18803 if (font_info->vertical_centering)
18804 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18805 }
18806
18807 if (font_info
18808 && (pcm = rif->per_char_metric (font, &char2b,
18809 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18810 {
18811 width = pcm->width;
18812 ascent = pcm->ascent;
18813 descent = pcm->descent;
18814 }
18815 else
18816 {
18817 width = FONT_WIDTH (font);
18818 ascent = 1;
18819 descent = 0;
18820 }
18821
18822 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18823 {
18824 /* Relative composition with or without
18825 alternate chars. */
18826 left = (leftmost + rightmost - width) / 2;
18827 btm = - descent + boff;
18828 if (font_info && font_info->relative_compose
18829 && (! CHAR_TABLE_P (Vignore_relative_composition)
18830 || NILP (Faref (Vignore_relative_composition,
18831 make_number (ch)))))
18832 {
18833
18834 if (- descent >= font_info->relative_compose)
18835 /* One extra pixel between two glyphs. */
18836 btm = highest + 1;
18837 else if (ascent <= 0)
18838 /* One extra pixel between two glyphs. */
18839 btm = lowest - 1 - ascent - descent;
18840 }
18841 }
18842 else
18843 {
18844 /* A composition rule is specified by an integer
18845 value that encodes global and new reference
18846 points (GREF and NREF). GREF and NREF are
18847 specified by numbers as below:
18848
18849 0---1---2 -- ascent
18850 | |
18851 | |
18852 | |
18853 9--10--11 -- center
18854 | |
18855 ---3---4---5--- baseline
18856 | |
18857 6---7---8 -- descent
18858 */
18859 int rule = COMPOSITION_RULE (cmp, i);
18860 int gref, nref, grefx, grefy, nrefx, nrefy;
18861
18862 COMPOSITION_DECODE_RULE (rule, gref, nref);
18863 grefx = gref % 3, nrefx = nref % 3;
18864 grefy = gref / 3, nrefy = nref / 3;
18865
18866 left = (leftmost
18867 + grefx * (rightmost - leftmost) / 2
18868 - nrefx * width / 2);
18869 btm = ((grefy == 0 ? highest
18870 : grefy == 1 ? 0
18871 : grefy == 2 ? lowest
18872 : (highest + lowest) / 2)
18873 - (nrefy == 0 ? ascent + descent
18874 : nrefy == 1 ? descent - boff
18875 : nrefy == 2 ? 0
18876 : (ascent + descent) / 2));
18877 }
18878
18879 cmp->offsets[i * 2] = left;
18880 cmp->offsets[i * 2 + 1] = btm + descent;
18881
18882 /* Update the bounding box of the overall glyphs. */
18883 right = left + width;
18884 top = btm + descent + ascent;
18885 if (left < leftmost)
18886 leftmost = left;
18887 if (right > rightmost)
18888 rightmost = right;
18889 if (top > highest)
18890 highest = top;
18891 if (btm < lowest)
18892 lowest = btm;
18893 }
18894
18895 /* If there are glyphs whose x-offsets are negative,
18896 shift all glyphs to the right and make all x-offsets
18897 non-negative. */
18898 if (leftmost < 0)
18899 {
18900 for (i = 0; i < cmp->glyph_len; i++)
18901 cmp->offsets[i * 2] -= leftmost;
18902 rightmost -= leftmost;
18903 }
18904
18905 cmp->pixel_width = rightmost;
18906 cmp->ascent = highest;
18907 cmp->descent = - lowest;
18908 if (cmp->ascent < font_ascent)
18909 cmp->ascent = font_ascent;
18910 if (cmp->descent < font_descent)
18911 cmp->descent = font_descent;
18912 }
18913
18914 it->pixel_width = cmp->pixel_width;
18915 it->ascent = it->phys_ascent = cmp->ascent;
18916 it->descent = it->phys_descent = cmp->descent;
18917
18918 if (face->box != FACE_NO_BOX)
18919 {
18920 int thick = face->box_line_width;
18921
18922 if (thick > 0)
18923 {
18924 it->ascent += thick;
18925 it->descent += thick;
18926 }
18927 else
18928 thick = - thick;
18929
18930 if (it->start_of_box_run_p)
18931 it->pixel_width += thick;
18932 if (it->end_of_box_run_p)
18933 it->pixel_width += thick;
18934 }
18935
18936 /* If face has an overline, add the height of the overline
18937 (1 pixel) and a 1 pixel margin to the character height. */
18938 if (face->overline_p)
18939 it->ascent += 2;
18940
18941 take_vertical_position_into_account (it);
18942
18943 if (it->glyph_row)
18944 append_composite_glyph (it);
18945 }
18946 else if (it->what == IT_IMAGE)
18947 produce_image_glyph (it);
18948 else if (it->what == IT_STRETCH)
18949 produce_stretch_glyph (it);
18950
18951 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18952 because this isn't true for images with `:ascent 100'. */
18953 xassert (it->ascent >= 0 && it->descent >= 0);
18954 if (it->area == TEXT_AREA)
18955 it->current_x += it->pixel_width;
18956
18957 it->descent += it->extra_line_spacing;
18958
18959 it->max_ascent = max (it->max_ascent, it->ascent);
18960 it->max_descent = max (it->max_descent, it->descent);
18961 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18962 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18963 }
18964
18965 /* EXPORT for RIF:
18966 Output LEN glyphs starting at START at the nominal cursor position.
18967 Advance the nominal cursor over the text. The global variable
18968 updated_window contains the window being updated, updated_row is
18969 the glyph row being updated, and updated_area is the area of that
18970 row being updated. */
18971
18972 void
18973 x_write_glyphs (start, len)
18974 struct glyph *start;
18975 int len;
18976 {
18977 int x, hpos;
18978
18979 xassert (updated_window && updated_row);
18980 BLOCK_INPUT;
18981
18982 /* Write glyphs. */
18983
18984 hpos = start - updated_row->glyphs[updated_area];
18985 x = draw_glyphs (updated_window, output_cursor.x,
18986 updated_row, updated_area,
18987 hpos, hpos + len,
18988 DRAW_NORMAL_TEXT, 0);
18989
18990 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18991 if (updated_area == TEXT_AREA
18992 && updated_window->phys_cursor_on_p
18993 && updated_window->phys_cursor.vpos == output_cursor.vpos
18994 && updated_window->phys_cursor.hpos >= hpos
18995 && updated_window->phys_cursor.hpos < hpos + len)
18996 updated_window->phys_cursor_on_p = 0;
18997
18998 UNBLOCK_INPUT;
18999
19000 /* Advance the output cursor. */
19001 output_cursor.hpos += len;
19002 output_cursor.x = x;
19003 }
19004
19005
19006 /* EXPORT for RIF:
19007 Insert LEN glyphs from START at the nominal cursor position. */
19008
19009 void
19010 x_insert_glyphs (start, len)
19011 struct glyph *start;
19012 int len;
19013 {
19014 struct frame *f;
19015 struct window *w;
19016 int line_height, shift_by_width, shifted_region_width;
19017 struct glyph_row *row;
19018 struct glyph *glyph;
19019 int frame_x, frame_y, hpos;
19020
19021 xassert (updated_window && updated_row);
19022 BLOCK_INPUT;
19023 w = updated_window;
19024 f = XFRAME (WINDOW_FRAME (w));
19025
19026 /* Get the height of the line we are in. */
19027 row = updated_row;
19028 line_height = row->height;
19029
19030 /* Get the width of the glyphs to insert. */
19031 shift_by_width = 0;
19032 for (glyph = start; glyph < start + len; ++glyph)
19033 shift_by_width += glyph->pixel_width;
19034
19035 /* Get the width of the region to shift right. */
19036 shifted_region_width = (window_box_width (w, updated_area)
19037 - output_cursor.x
19038 - shift_by_width);
19039
19040 /* Shift right. */
19041 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19042 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19043
19044 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19045 line_height, shift_by_width);
19046
19047 /* Write the glyphs. */
19048 hpos = start - row->glyphs[updated_area];
19049 draw_glyphs (w, output_cursor.x, row, updated_area,
19050 hpos, hpos + len,
19051 DRAW_NORMAL_TEXT, 0);
19052
19053 /* Advance the output cursor. */
19054 output_cursor.hpos += len;
19055 output_cursor.x += shift_by_width;
19056 UNBLOCK_INPUT;
19057 }
19058
19059
19060 /* EXPORT for RIF:
19061 Erase the current text line from the nominal cursor position
19062 (inclusive) to pixel column TO_X (exclusive). The idea is that
19063 everything from TO_X onward is already erased.
19064
19065 TO_X is a pixel position relative to updated_area of
19066 updated_window. TO_X == -1 means clear to the end of this area. */
19067
19068 void
19069 x_clear_end_of_line (to_x)
19070 int to_x;
19071 {
19072 struct frame *f;
19073 struct window *w = updated_window;
19074 int max_x, min_y, max_y;
19075 int from_x, from_y, to_y;
19076
19077 xassert (updated_window && updated_row);
19078 f = XFRAME (w->frame);
19079
19080 if (updated_row->full_width_p)
19081 max_x = WINDOW_TOTAL_WIDTH (w);
19082 else
19083 max_x = window_box_width (w, updated_area);
19084 max_y = window_text_bottom_y (w);
19085
19086 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19087 of window. For TO_X > 0, truncate to end of drawing area. */
19088 if (to_x == 0)
19089 return;
19090 else if (to_x < 0)
19091 to_x = max_x;
19092 else
19093 to_x = min (to_x, max_x);
19094
19095 to_y = min (max_y, output_cursor.y + updated_row->height);
19096
19097 /* Notice if the cursor will be cleared by this operation. */
19098 if (!updated_row->full_width_p)
19099 notice_overwritten_cursor (w, updated_area,
19100 output_cursor.x, -1,
19101 updated_row->y,
19102 MATRIX_ROW_BOTTOM_Y (updated_row));
19103
19104 from_x = output_cursor.x;
19105
19106 /* Translate to frame coordinates. */
19107 if (updated_row->full_width_p)
19108 {
19109 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19110 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19111 }
19112 else
19113 {
19114 int area_left = window_box_left (w, updated_area);
19115 from_x += area_left;
19116 to_x += area_left;
19117 }
19118
19119 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19120 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19121 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19122
19123 /* Prevent inadvertently clearing to end of the X window. */
19124 if (to_x > from_x && to_y > from_y)
19125 {
19126 BLOCK_INPUT;
19127 rif->clear_frame_area (f, from_x, from_y,
19128 to_x - from_x, to_y - from_y);
19129 UNBLOCK_INPUT;
19130 }
19131 }
19132
19133 #endif /* HAVE_WINDOW_SYSTEM */
19134
19135
19136 \f
19137 /***********************************************************************
19138 Cursor types
19139 ***********************************************************************/
19140
19141 /* Value is the internal representation of the specified cursor type
19142 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19143 of the bar cursor. */
19144
19145 static enum text_cursor_kinds
19146 get_specified_cursor_type (arg, width)
19147 Lisp_Object arg;
19148 int *width;
19149 {
19150 enum text_cursor_kinds type;
19151
19152 if (NILP (arg))
19153 return NO_CURSOR;
19154
19155 if (EQ (arg, Qbox))
19156 return FILLED_BOX_CURSOR;
19157
19158 if (EQ (arg, Qhollow))
19159 return HOLLOW_BOX_CURSOR;
19160
19161 if (EQ (arg, Qbar))
19162 {
19163 *width = 2;
19164 return BAR_CURSOR;
19165 }
19166
19167 if (CONSP (arg)
19168 && EQ (XCAR (arg), Qbar)
19169 && INTEGERP (XCDR (arg))
19170 && XINT (XCDR (arg)) >= 0)
19171 {
19172 *width = XINT (XCDR (arg));
19173 return BAR_CURSOR;
19174 }
19175
19176 if (EQ (arg, Qhbar))
19177 {
19178 *width = 2;
19179 return HBAR_CURSOR;
19180 }
19181
19182 if (CONSP (arg)
19183 && EQ (XCAR (arg), Qhbar)
19184 && INTEGERP (XCDR (arg))
19185 && XINT (XCDR (arg)) >= 0)
19186 {
19187 *width = XINT (XCDR (arg));
19188 return HBAR_CURSOR;
19189 }
19190
19191 /* Treat anything unknown as "hollow box cursor".
19192 It was bad to signal an error; people have trouble fixing
19193 .Xdefaults with Emacs, when it has something bad in it. */
19194 type = HOLLOW_BOX_CURSOR;
19195
19196 return type;
19197 }
19198
19199 /* Set the default cursor types for specified frame. */
19200 void
19201 set_frame_cursor_types (f, arg)
19202 struct frame *f;
19203 Lisp_Object arg;
19204 {
19205 int width;
19206 Lisp_Object tem;
19207
19208 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19209 FRAME_CURSOR_WIDTH (f) = width;
19210
19211 /* By default, set up the blink-off state depending on the on-state. */
19212
19213 tem = Fassoc (arg, Vblink_cursor_alist);
19214 if (!NILP (tem))
19215 {
19216 FRAME_BLINK_OFF_CURSOR (f)
19217 = get_specified_cursor_type (XCDR (tem), &width);
19218 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19219 }
19220 else
19221 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19222 }
19223
19224
19225 /* Return the cursor we want to be displayed in window W. Return
19226 width of bar/hbar cursor through WIDTH arg. Return with
19227 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19228 (i.e. if the `system caret' should track this cursor).
19229
19230 In a mini-buffer window, we want the cursor only to appear if we
19231 are reading input from this window. For the selected window, we
19232 want the cursor type given by the frame parameter or buffer local
19233 setting of cursor-type. If explicitly marked off, draw no cursor.
19234 In all other cases, we want a hollow box cursor. */
19235
19236 static enum text_cursor_kinds
19237 get_window_cursor_type (w, glyph, width, active_cursor)
19238 struct window *w;
19239 struct glyph *glyph;
19240 int *width;
19241 int *active_cursor;
19242 {
19243 struct frame *f = XFRAME (w->frame);
19244 struct buffer *b = XBUFFER (w->buffer);
19245 int cursor_type = DEFAULT_CURSOR;
19246 Lisp_Object alt_cursor;
19247 int non_selected = 0;
19248
19249 *active_cursor = 1;
19250
19251 /* Echo area */
19252 if (cursor_in_echo_area
19253 && FRAME_HAS_MINIBUF_P (f)
19254 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19255 {
19256 if (w == XWINDOW (echo_area_window))
19257 {
19258 *width = FRAME_CURSOR_WIDTH (f);
19259 return FRAME_DESIRED_CURSOR (f);
19260 }
19261
19262 *active_cursor = 0;
19263 non_selected = 1;
19264 }
19265
19266 /* Nonselected window or nonselected frame. */
19267 else if (w != XWINDOW (f->selected_window)
19268 #ifdef HAVE_WINDOW_SYSTEM
19269 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19270 #endif
19271 )
19272 {
19273 *active_cursor = 0;
19274
19275 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19276 return NO_CURSOR;
19277
19278 non_selected = 1;
19279 }
19280
19281 /* Never display a cursor in a window in which cursor-type is nil. */
19282 if (NILP (b->cursor_type))
19283 return NO_CURSOR;
19284
19285 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19286 if (non_selected)
19287 {
19288 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19289 return get_specified_cursor_type (alt_cursor, width);
19290 }
19291
19292 /* Get the normal cursor type for this window. */
19293 if (EQ (b->cursor_type, Qt))
19294 {
19295 cursor_type = FRAME_DESIRED_CURSOR (f);
19296 *width = FRAME_CURSOR_WIDTH (f);
19297 }
19298 else
19299 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19300
19301 /* Use normal cursor if not blinked off. */
19302 if (!w->cursor_off_p)
19303 {
19304 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19305 if (cursor_type == FILLED_BOX_CURSOR)
19306 cursor_type = HOLLOW_BOX_CURSOR;
19307 }
19308 return cursor_type;
19309 }
19310
19311 /* Cursor is blinked off, so determine how to "toggle" it. */
19312
19313 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19314 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19315 return get_specified_cursor_type (XCDR (alt_cursor), width);
19316
19317 /* Then see if frame has specified a specific blink off cursor type. */
19318 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19319 {
19320 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19321 return FRAME_BLINK_OFF_CURSOR (f);
19322 }
19323
19324 #if 0
19325 /* Some people liked having a permanently visible blinking cursor,
19326 while others had very strong opinions against it. So it was
19327 decided to remove it. KFS 2003-09-03 */
19328
19329 /* Finally perform built-in cursor blinking:
19330 filled box <-> hollow box
19331 wide [h]bar <-> narrow [h]bar
19332 narrow [h]bar <-> no cursor
19333 other type <-> no cursor */
19334
19335 if (cursor_type == FILLED_BOX_CURSOR)
19336 return HOLLOW_BOX_CURSOR;
19337
19338 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19339 {
19340 *width = 1;
19341 return cursor_type;
19342 }
19343 #endif
19344
19345 return NO_CURSOR;
19346 }
19347
19348
19349 #ifdef HAVE_WINDOW_SYSTEM
19350
19351 /* Notice when the text cursor of window W has been completely
19352 overwritten by a drawing operation that outputs glyphs in AREA
19353 starting at X0 and ending at X1 in the line starting at Y0 and
19354 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19355 the rest of the line after X0 has been written. Y coordinates
19356 are window-relative. */
19357
19358 static void
19359 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19360 struct window *w;
19361 enum glyph_row_area area;
19362 int x0, y0, x1, y1;
19363 {
19364 int cx0, cx1, cy0, cy1;
19365 struct glyph_row *row;
19366
19367 if (!w->phys_cursor_on_p)
19368 return;
19369 if (area != TEXT_AREA)
19370 return;
19371
19372 row = w->current_matrix->rows + w->phys_cursor.vpos;
19373 if (!row->displays_text_p)
19374 return;
19375
19376 if (row->cursor_in_fringe_p)
19377 {
19378 row->cursor_in_fringe_p = 0;
19379 draw_fringe_bitmap (w, row, 0);
19380 w->phys_cursor_on_p = 0;
19381 return;
19382 }
19383
19384 cx0 = w->phys_cursor.x;
19385 cx1 = cx0 + w->phys_cursor_width;
19386 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19387 return;
19388
19389 /* The cursor image will be completely removed from the
19390 screen if the output area intersects the cursor area in
19391 y-direction. When we draw in [y0 y1[, and some part of
19392 the cursor is at y < y0, that part must have been drawn
19393 before. When scrolling, the cursor is erased before
19394 actually scrolling, so we don't come here. When not
19395 scrolling, the rows above the old cursor row must have
19396 changed, and in this case these rows must have written
19397 over the cursor image.
19398
19399 Likewise if part of the cursor is below y1, with the
19400 exception of the cursor being in the first blank row at
19401 the buffer and window end because update_text_area
19402 doesn't draw that row. (Except when it does, but
19403 that's handled in update_text_area.) */
19404
19405 cy0 = w->phys_cursor.y;
19406 cy1 = cy0 + w->phys_cursor_height;
19407 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19408 return;
19409
19410 w->phys_cursor_on_p = 0;
19411 }
19412
19413 #endif /* HAVE_WINDOW_SYSTEM */
19414
19415 \f
19416 /************************************************************************
19417 Mouse Face
19418 ************************************************************************/
19419
19420 #ifdef HAVE_WINDOW_SYSTEM
19421
19422 /* EXPORT for RIF:
19423 Fix the display of area AREA of overlapping row ROW in window W. */
19424
19425 void
19426 x_fix_overlapping_area (w, row, area)
19427 struct window *w;
19428 struct glyph_row *row;
19429 enum glyph_row_area area;
19430 {
19431 int i, x;
19432
19433 BLOCK_INPUT;
19434
19435 x = 0;
19436 for (i = 0; i < row->used[area];)
19437 {
19438 if (row->glyphs[area][i].overlaps_vertically_p)
19439 {
19440 int start = i, start_x = x;
19441
19442 do
19443 {
19444 x += row->glyphs[area][i].pixel_width;
19445 ++i;
19446 }
19447 while (i < row->used[area]
19448 && row->glyphs[area][i].overlaps_vertically_p);
19449
19450 draw_glyphs (w, start_x, row, area,
19451 start, i,
19452 DRAW_NORMAL_TEXT, 1);
19453 }
19454 else
19455 {
19456 x += row->glyphs[area][i].pixel_width;
19457 ++i;
19458 }
19459 }
19460
19461 UNBLOCK_INPUT;
19462 }
19463
19464
19465 /* EXPORT:
19466 Draw the cursor glyph of window W in glyph row ROW. See the
19467 comment of draw_glyphs for the meaning of HL. */
19468
19469 void
19470 draw_phys_cursor_glyph (w, row, hl)
19471 struct window *w;
19472 struct glyph_row *row;
19473 enum draw_glyphs_face hl;
19474 {
19475 /* If cursor hpos is out of bounds, don't draw garbage. This can
19476 happen in mini-buffer windows when switching between echo area
19477 glyphs and mini-buffer. */
19478 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19479 {
19480 int on_p = w->phys_cursor_on_p;
19481 int x1;
19482 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19483 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19484 hl, 0);
19485 w->phys_cursor_on_p = on_p;
19486
19487 if (hl == DRAW_CURSOR)
19488 w->phys_cursor_width = x1 - w->phys_cursor.x;
19489 /* When we erase the cursor, and ROW is overlapped by other
19490 rows, make sure that these overlapping parts of other rows
19491 are redrawn. */
19492 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19493 {
19494 if (row > w->current_matrix->rows
19495 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19496 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19497
19498 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19499 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19500 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19501 }
19502 }
19503 }
19504
19505
19506 /* EXPORT:
19507 Erase the image of a cursor of window W from the screen. */
19508
19509 void
19510 erase_phys_cursor (w)
19511 struct window *w;
19512 {
19513 struct frame *f = XFRAME (w->frame);
19514 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19515 int hpos = w->phys_cursor.hpos;
19516 int vpos = w->phys_cursor.vpos;
19517 int mouse_face_here_p = 0;
19518 struct glyph_matrix *active_glyphs = w->current_matrix;
19519 struct glyph_row *cursor_row;
19520 struct glyph *cursor_glyph;
19521 enum draw_glyphs_face hl;
19522
19523 /* No cursor displayed or row invalidated => nothing to do on the
19524 screen. */
19525 if (w->phys_cursor_type == NO_CURSOR)
19526 goto mark_cursor_off;
19527
19528 /* VPOS >= active_glyphs->nrows means that window has been resized.
19529 Don't bother to erase the cursor. */
19530 if (vpos >= active_glyphs->nrows)
19531 goto mark_cursor_off;
19532
19533 /* If row containing cursor is marked invalid, there is nothing we
19534 can do. */
19535 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19536 if (!cursor_row->enabled_p)
19537 goto mark_cursor_off;
19538
19539 /* If row is completely invisible, don't attempt to delete a cursor which
19540 isn't there. This can happen if cursor is at top of a window, and
19541 we switch to a buffer with a header line in that window. */
19542 if (cursor_row->visible_height <= 0)
19543 goto mark_cursor_off;
19544
19545 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19546 if (cursor_row->cursor_in_fringe_p)
19547 {
19548 cursor_row->cursor_in_fringe_p = 0;
19549 draw_fringe_bitmap (w, cursor_row, 0);
19550 goto mark_cursor_off;
19551 }
19552
19553 /* This can happen when the new row is shorter than the old one.
19554 In this case, either draw_glyphs or clear_end_of_line
19555 should have cleared the cursor. Note that we wouldn't be
19556 able to erase the cursor in this case because we don't have a
19557 cursor glyph at hand. */
19558 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19559 goto mark_cursor_off;
19560
19561 /* If the cursor is in the mouse face area, redisplay that when
19562 we clear the cursor. */
19563 if (! NILP (dpyinfo->mouse_face_window)
19564 && w == XWINDOW (dpyinfo->mouse_face_window)
19565 && (vpos > dpyinfo->mouse_face_beg_row
19566 || (vpos == dpyinfo->mouse_face_beg_row
19567 && hpos >= dpyinfo->mouse_face_beg_col))
19568 && (vpos < dpyinfo->mouse_face_end_row
19569 || (vpos == dpyinfo->mouse_face_end_row
19570 && hpos < dpyinfo->mouse_face_end_col))
19571 /* Don't redraw the cursor's spot in mouse face if it is at the
19572 end of a line (on a newline). The cursor appears there, but
19573 mouse highlighting does not. */
19574 && cursor_row->used[TEXT_AREA] > hpos)
19575 mouse_face_here_p = 1;
19576
19577 /* Maybe clear the display under the cursor. */
19578 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19579 {
19580 int x, y;
19581 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19582
19583 cursor_glyph = get_phys_cursor_glyph (w);
19584 if (cursor_glyph == NULL)
19585 goto mark_cursor_off;
19586
19587 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19588 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19589
19590 rif->clear_frame_area (f, x, y,
19591 cursor_glyph->pixel_width, cursor_row->visible_height);
19592 }
19593
19594 /* Erase the cursor by redrawing the character underneath it. */
19595 if (mouse_face_here_p)
19596 hl = DRAW_MOUSE_FACE;
19597 else
19598 hl = DRAW_NORMAL_TEXT;
19599 draw_phys_cursor_glyph (w, cursor_row, hl);
19600
19601 mark_cursor_off:
19602 w->phys_cursor_on_p = 0;
19603 w->phys_cursor_type = NO_CURSOR;
19604 }
19605
19606
19607 /* EXPORT:
19608 Display or clear cursor of window W. If ON is zero, clear the
19609 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19610 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19611
19612 void
19613 display_and_set_cursor (w, on, hpos, vpos, x, y)
19614 struct window *w;
19615 int on, hpos, vpos, x, y;
19616 {
19617 struct frame *f = XFRAME (w->frame);
19618 int new_cursor_type;
19619 int new_cursor_width;
19620 int active_cursor;
19621 struct glyph_row *glyph_row;
19622 struct glyph *glyph;
19623
19624 /* This is pointless on invisible frames, and dangerous on garbaged
19625 windows and frames; in the latter case, the frame or window may
19626 be in the midst of changing its size, and x and y may be off the
19627 window. */
19628 if (! FRAME_VISIBLE_P (f)
19629 || FRAME_GARBAGED_P (f)
19630 || vpos >= w->current_matrix->nrows
19631 || hpos >= w->current_matrix->matrix_w)
19632 return;
19633
19634 /* If cursor is off and we want it off, return quickly. */
19635 if (!on && !w->phys_cursor_on_p)
19636 return;
19637
19638 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
19639 /* If cursor row is not enabled, we don't really know where to
19640 display the cursor. */
19641 if (!glyph_row->enabled_p)
19642 {
19643 w->phys_cursor_on_p = 0;
19644 return;
19645 }
19646
19647 glyph = NULL;
19648 if (!glyph_row->exact_window_width_line_p
19649 || hpos < glyph_row->used[TEXT_AREA])
19650 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19651
19652 xassert (interrupt_input_blocked);
19653
19654 /* Set new_cursor_type to the cursor we want to be displayed. */
19655 new_cursor_type = get_window_cursor_type (w, glyph,
19656 &new_cursor_width, &active_cursor);
19657
19658 /* If cursor is currently being shown and we don't want it to be or
19659 it is in the wrong place, or the cursor type is not what we want,
19660 erase it. */
19661 if (w->phys_cursor_on_p
19662 && (!on
19663 || w->phys_cursor.x != x
19664 || w->phys_cursor.y != y
19665 || new_cursor_type != w->phys_cursor_type
19666 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19667 && new_cursor_width != w->phys_cursor_width)))
19668 erase_phys_cursor (w);
19669
19670 /* Don't check phys_cursor_on_p here because that flag is only set
19671 to zero in some cases where we know that the cursor has been
19672 completely erased, to avoid the extra work of erasing the cursor
19673 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19674 still not be visible, or it has only been partly erased. */
19675 if (on)
19676 {
19677 w->phys_cursor_ascent = glyph_row->ascent;
19678 w->phys_cursor_height = glyph_row->height;
19679
19680 /* Set phys_cursor_.* before x_draw_.* is called because some
19681 of them may need the information. */
19682 w->phys_cursor.x = x;
19683 w->phys_cursor.y = glyph_row->y;
19684 w->phys_cursor.hpos = hpos;
19685 w->phys_cursor.vpos = vpos;
19686 }
19687
19688 rif->draw_window_cursor (w, glyph_row, x, y,
19689 new_cursor_type, new_cursor_width,
19690 on, active_cursor);
19691 }
19692
19693
19694 /* Switch the display of W's cursor on or off, according to the value
19695 of ON. */
19696
19697 static void
19698 update_window_cursor (w, on)
19699 struct window *w;
19700 int on;
19701 {
19702 /* Don't update cursor in windows whose frame is in the process
19703 of being deleted. */
19704 if (w->current_matrix)
19705 {
19706 BLOCK_INPUT;
19707 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19708 w->phys_cursor.x, w->phys_cursor.y);
19709 UNBLOCK_INPUT;
19710 }
19711 }
19712
19713
19714 /* Call update_window_cursor with parameter ON_P on all leaf windows
19715 in the window tree rooted at W. */
19716
19717 static void
19718 update_cursor_in_window_tree (w, on_p)
19719 struct window *w;
19720 int on_p;
19721 {
19722 while (w)
19723 {
19724 if (!NILP (w->hchild))
19725 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19726 else if (!NILP (w->vchild))
19727 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19728 else
19729 update_window_cursor (w, on_p);
19730
19731 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19732 }
19733 }
19734
19735
19736 /* EXPORT:
19737 Display the cursor on window W, or clear it, according to ON_P.
19738 Don't change the cursor's position. */
19739
19740 void
19741 x_update_cursor (f, on_p)
19742 struct frame *f;
19743 int on_p;
19744 {
19745 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19746 }
19747
19748
19749 /* EXPORT:
19750 Clear the cursor of window W to background color, and mark the
19751 cursor as not shown. This is used when the text where the cursor
19752 is is about to be rewritten. */
19753
19754 void
19755 x_clear_cursor (w)
19756 struct window *w;
19757 {
19758 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19759 update_window_cursor (w, 0);
19760 }
19761
19762
19763 /* EXPORT:
19764 Display the active region described by mouse_face_* according to DRAW. */
19765
19766 void
19767 show_mouse_face (dpyinfo, draw)
19768 Display_Info *dpyinfo;
19769 enum draw_glyphs_face draw;
19770 {
19771 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19772 struct frame *f = XFRAME (WINDOW_FRAME (w));
19773
19774 if (/* If window is in the process of being destroyed, don't bother
19775 to do anything. */
19776 w->current_matrix != NULL
19777 /* Don't update mouse highlight if hidden */
19778 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19779 /* Recognize when we are called to operate on rows that don't exist
19780 anymore. This can happen when a window is split. */
19781 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19782 {
19783 int phys_cursor_on_p = w->phys_cursor_on_p;
19784 struct glyph_row *row, *first, *last;
19785
19786 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19787 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19788
19789 for (row = first; row <= last && row->enabled_p; ++row)
19790 {
19791 int start_hpos, end_hpos, start_x;
19792
19793 /* For all but the first row, the highlight starts at column 0. */
19794 if (row == first)
19795 {
19796 start_hpos = dpyinfo->mouse_face_beg_col;
19797 start_x = dpyinfo->mouse_face_beg_x;
19798 }
19799 else
19800 {
19801 start_hpos = 0;
19802 start_x = 0;
19803 }
19804
19805 if (row == last)
19806 end_hpos = dpyinfo->mouse_face_end_col;
19807 else
19808 end_hpos = row->used[TEXT_AREA];
19809
19810 if (end_hpos > start_hpos)
19811 {
19812 draw_glyphs (w, start_x, row, TEXT_AREA,
19813 start_hpos, end_hpos,
19814 draw, 0);
19815
19816 row->mouse_face_p
19817 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19818 }
19819 }
19820
19821 /* When we've written over the cursor, arrange for it to
19822 be displayed again. */
19823 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19824 {
19825 BLOCK_INPUT;
19826 display_and_set_cursor (w, 1,
19827 w->phys_cursor.hpos, w->phys_cursor.vpos,
19828 w->phys_cursor.x, w->phys_cursor.y);
19829 UNBLOCK_INPUT;
19830 }
19831 }
19832
19833 /* Change the mouse cursor. */
19834 if (draw == DRAW_NORMAL_TEXT)
19835 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19836 else if (draw == DRAW_MOUSE_FACE)
19837 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19838 else
19839 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19840 }
19841
19842 /* EXPORT:
19843 Clear out the mouse-highlighted active region.
19844 Redraw it un-highlighted first. Value is non-zero if mouse
19845 face was actually drawn unhighlighted. */
19846
19847 int
19848 clear_mouse_face (dpyinfo)
19849 Display_Info *dpyinfo;
19850 {
19851 int cleared = 0;
19852
19853 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
19854 {
19855 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19856 cleared = 1;
19857 }
19858
19859 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19860 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19861 dpyinfo->mouse_face_window = Qnil;
19862 dpyinfo->mouse_face_overlay = Qnil;
19863 return cleared;
19864 }
19865
19866
19867 /* EXPORT:
19868 Non-zero if physical cursor of window W is within mouse face. */
19869
19870 int
19871 cursor_in_mouse_face_p (w)
19872 struct window *w;
19873 {
19874 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19875 int in_mouse_face = 0;
19876
19877 if (WINDOWP (dpyinfo->mouse_face_window)
19878 && XWINDOW (dpyinfo->mouse_face_window) == w)
19879 {
19880 int hpos = w->phys_cursor.hpos;
19881 int vpos = w->phys_cursor.vpos;
19882
19883 if (vpos >= dpyinfo->mouse_face_beg_row
19884 && vpos <= dpyinfo->mouse_face_end_row
19885 && (vpos > dpyinfo->mouse_face_beg_row
19886 || hpos >= dpyinfo->mouse_face_beg_col)
19887 && (vpos < dpyinfo->mouse_face_end_row
19888 || hpos < dpyinfo->mouse_face_end_col
19889 || dpyinfo->mouse_face_past_end))
19890 in_mouse_face = 1;
19891 }
19892
19893 return in_mouse_face;
19894 }
19895
19896
19897
19898 \f
19899 /* Find the glyph matrix position of buffer position CHARPOS in window
19900 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19901 current glyphs must be up to date. If CHARPOS is above window
19902 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19903 of last line in W. In the row containing CHARPOS, stop before glyphs
19904 having STOP as object. */
19905
19906 #if 1 /* This is a version of fast_find_position that's more correct
19907 in the presence of hscrolling, for example. I didn't install
19908 it right away because the problem fixed is minor, it failed
19909 in 20.x as well, and I think it's too risky to install
19910 so near the release of 21.1. 2001-09-25 gerd. */
19911
19912 static int
19913 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19914 struct window *w;
19915 int charpos;
19916 int *hpos, *vpos, *x, *y;
19917 Lisp_Object stop;
19918 {
19919 struct glyph_row *row, *first;
19920 struct glyph *glyph, *end;
19921 int past_end = 0;
19922
19923 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19924 row = row_containing_pos (w, charpos, first, NULL, 0);
19925 if (row == NULL)
19926 {
19927 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19928 {
19929 *x = *y = *hpos = *vpos = 0;
19930 return 1;
19931 }
19932 else
19933 {
19934 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19935 past_end = 1;
19936 }
19937 }
19938
19939 *x = row->x;
19940 *y = row->y;
19941 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19942
19943 glyph = row->glyphs[TEXT_AREA];
19944 end = glyph + row->used[TEXT_AREA];
19945
19946 /* Skip over glyphs not having an object at the start of the row.
19947 These are special glyphs like truncation marks on terminal
19948 frames. */
19949 if (row->displays_text_p)
19950 while (glyph < end
19951 && INTEGERP (glyph->object)
19952 && !EQ (stop, glyph->object)
19953 && glyph->charpos < 0)
19954 {
19955 *x += glyph->pixel_width;
19956 ++glyph;
19957 }
19958
19959 while (glyph < end
19960 && !INTEGERP (glyph->object)
19961 && !EQ (stop, glyph->object)
19962 && (!BUFFERP (glyph->object)
19963 || glyph->charpos < charpos))
19964 {
19965 *x += glyph->pixel_width;
19966 ++glyph;
19967 }
19968
19969 *hpos = glyph - row->glyphs[TEXT_AREA];
19970 return !past_end;
19971 }
19972
19973 #else /* not 1 */
19974
19975 static int
19976 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19977 struct window *w;
19978 int pos;
19979 int *hpos, *vpos, *x, *y;
19980 Lisp_Object stop;
19981 {
19982 int i;
19983 int lastcol;
19984 int maybe_next_line_p = 0;
19985 int line_start_position;
19986 int yb = window_text_bottom_y (w);
19987 struct glyph_row *row, *best_row;
19988 int row_vpos, best_row_vpos;
19989 int current_x;
19990
19991 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19992 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19993
19994 while (row->y < yb)
19995 {
19996 if (row->used[TEXT_AREA])
19997 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19998 else
19999 line_start_position = 0;
20000
20001 if (line_start_position > pos)
20002 break;
20003 /* If the position sought is the end of the buffer,
20004 don't include the blank lines at the bottom of the window. */
20005 else if (line_start_position == pos
20006 && pos == BUF_ZV (XBUFFER (w->buffer)))
20007 {
20008 maybe_next_line_p = 1;
20009 break;
20010 }
20011 else if (line_start_position > 0)
20012 {
20013 best_row = row;
20014 best_row_vpos = row_vpos;
20015 }
20016
20017 if (row->y + row->height >= yb)
20018 break;
20019
20020 ++row;
20021 ++row_vpos;
20022 }
20023
20024 /* Find the right column within BEST_ROW. */
20025 lastcol = 0;
20026 current_x = best_row->x;
20027 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20028 {
20029 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20030 int charpos = glyph->charpos;
20031
20032 if (BUFFERP (glyph->object))
20033 {
20034 if (charpos == pos)
20035 {
20036 *hpos = i;
20037 *vpos = best_row_vpos;
20038 *x = current_x;
20039 *y = best_row->y;
20040 return 1;
20041 }
20042 else if (charpos > pos)
20043 break;
20044 }
20045 else if (EQ (glyph->object, stop))
20046 break;
20047
20048 if (charpos > 0)
20049 lastcol = i;
20050 current_x += glyph->pixel_width;
20051 }
20052
20053 /* If we're looking for the end of the buffer,
20054 and we didn't find it in the line we scanned,
20055 use the start of the following line. */
20056 if (maybe_next_line_p)
20057 {
20058 ++best_row;
20059 ++best_row_vpos;
20060 lastcol = 0;
20061 current_x = best_row->x;
20062 }
20063
20064 *vpos = best_row_vpos;
20065 *hpos = lastcol + 1;
20066 *x = current_x;
20067 *y = best_row->y;
20068 return 0;
20069 }
20070
20071 #endif /* not 1 */
20072
20073
20074 /* Find the position of the glyph for position POS in OBJECT in
20075 window W's current matrix, and return in *X, *Y the pixel
20076 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20077
20078 RIGHT_P non-zero means return the position of the right edge of the
20079 glyph, RIGHT_P zero means return the left edge position.
20080
20081 If no glyph for POS exists in the matrix, return the position of
20082 the glyph with the next smaller position that is in the matrix, if
20083 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20084 exists in the matrix, return the position of the glyph with the
20085 next larger position in OBJECT.
20086
20087 Value is non-zero if a glyph was found. */
20088
20089 static int
20090 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20091 struct window *w;
20092 int pos;
20093 Lisp_Object object;
20094 int *hpos, *vpos, *x, *y;
20095 int right_p;
20096 {
20097 int yb = window_text_bottom_y (w);
20098 struct glyph_row *r;
20099 struct glyph *best_glyph = NULL;
20100 struct glyph_row *best_row = NULL;
20101 int best_x = 0;
20102
20103 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20104 r->enabled_p && r->y < yb;
20105 ++r)
20106 {
20107 struct glyph *g = r->glyphs[TEXT_AREA];
20108 struct glyph *e = g + r->used[TEXT_AREA];
20109 int gx;
20110
20111 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20112 if (EQ (g->object, object))
20113 {
20114 if (g->charpos == pos)
20115 {
20116 best_glyph = g;
20117 best_x = gx;
20118 best_row = r;
20119 goto found;
20120 }
20121 else if (best_glyph == NULL
20122 || ((abs (g->charpos - pos)
20123 < abs (best_glyph->charpos - pos))
20124 && (right_p
20125 ? g->charpos < pos
20126 : g->charpos > pos)))
20127 {
20128 best_glyph = g;
20129 best_x = gx;
20130 best_row = r;
20131 }
20132 }
20133 }
20134
20135 found:
20136
20137 if (best_glyph)
20138 {
20139 *x = best_x;
20140 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20141
20142 if (right_p)
20143 {
20144 *x += best_glyph->pixel_width;
20145 ++*hpos;
20146 }
20147
20148 *y = best_row->y;
20149 *vpos = best_row - w->current_matrix->rows;
20150 }
20151
20152 return best_glyph != NULL;
20153 }
20154
20155
20156 /* See if position X, Y is within a hot-spot of an image. */
20157
20158 static int
20159 on_hot_spot_p (hot_spot, x, y)
20160 Lisp_Object hot_spot;
20161 int x, y;
20162 {
20163 if (!CONSP (hot_spot))
20164 return 0;
20165
20166 if (EQ (XCAR (hot_spot), Qrect))
20167 {
20168 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20169 Lisp_Object rect = XCDR (hot_spot);
20170 Lisp_Object tem;
20171 if (!CONSP (rect))
20172 return 0;
20173 if (!CONSP (XCAR (rect)))
20174 return 0;
20175 if (!CONSP (XCDR (rect)))
20176 return 0;
20177 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20178 return 0;
20179 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20180 return 0;
20181 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20182 return 0;
20183 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20184 return 0;
20185 return 1;
20186 }
20187 else if (EQ (XCAR (hot_spot), Qcircle))
20188 {
20189 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20190 Lisp_Object circ = XCDR (hot_spot);
20191 Lisp_Object lr, lx0, ly0;
20192 if (CONSP (circ)
20193 && CONSP (XCAR (circ))
20194 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20195 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20196 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20197 {
20198 double r = XFLOATINT (lr);
20199 double dx = XINT (lx0) - x;
20200 double dy = XINT (ly0) - y;
20201 return (dx * dx + dy * dy <= r * r);
20202 }
20203 }
20204 else if (EQ (XCAR (hot_spot), Qpoly))
20205 {
20206 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20207 if (VECTORP (XCDR (hot_spot)))
20208 {
20209 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20210 Lisp_Object *poly = v->contents;
20211 int n = v->size;
20212 int i;
20213 int inside = 0;
20214 Lisp_Object lx, ly;
20215 int x0, y0;
20216
20217 /* Need an even number of coordinates, and at least 3 edges. */
20218 if (n < 6 || n & 1)
20219 return 0;
20220
20221 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20222 If count is odd, we are inside polygon. Pixels on edges
20223 may or may not be included depending on actual geometry of the
20224 polygon. */
20225 if ((lx = poly[n-2], !INTEGERP (lx))
20226 || (ly = poly[n-1], !INTEGERP (lx)))
20227 return 0;
20228 x0 = XINT (lx), y0 = XINT (ly);
20229 for (i = 0; i < n; i += 2)
20230 {
20231 int x1 = x0, y1 = y0;
20232 if ((lx = poly[i], !INTEGERP (lx))
20233 || (ly = poly[i+1], !INTEGERP (ly)))
20234 return 0;
20235 x0 = XINT (lx), y0 = XINT (ly);
20236
20237 /* Does this segment cross the X line? */
20238 if (x0 >= x)
20239 {
20240 if (x1 >= x)
20241 continue;
20242 }
20243 else if (x1 < x)
20244 continue;
20245 if (y > y0 && y > y1)
20246 continue;
20247 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20248 inside = !inside;
20249 }
20250 return inside;
20251 }
20252 }
20253 else
20254 return 0;
20255 }
20256
20257 Lisp_Object
20258 find_hot_spot (map, x, y)
20259 Lisp_Object map;
20260 int x, y;
20261 {
20262 while (CONSP (map))
20263 {
20264 if (CONSP (XCAR (map))
20265 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20266 return XCAR (map);
20267 map = XCDR (map);
20268 }
20269
20270 return Qnil;
20271 }
20272
20273 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20274 3, 3, 0,
20275 doc: /* Lookup in image map MAP coordinates X and Y.
20276 An image map is an alist where each element has the format (AREA ID PLIST).
20277 An AREA is specified as either a rectangle, a circle, or a polygon:
20278 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20279 pixel coordinates of the upper left and bottom right corners.
20280 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20281 and the radius of the circle; r may be a float or integer.
20282 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20283 vector describes one corner in the polygon.
20284 Returns the alist element for the first matching AREA in MAP. */)
20285 (map, x, y)
20286 Lisp_Object map;
20287 Lisp_Object x, y;
20288 {
20289 int ix, iy;
20290 if (NILP (map))
20291 return Qnil;
20292
20293 CHECK_NUMBER (x);
20294 CHECK_NUMBER (y);
20295
20296 return find_hot_spot (map, XINT (x), XINT (y));
20297 }
20298
20299
20300 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20301 static void
20302 define_frame_cursor1 (f, cursor, pointer)
20303 struct frame *f;
20304 Cursor cursor;
20305 Lisp_Object pointer;
20306 {
20307 if (!NILP (pointer))
20308 {
20309 if (EQ (pointer, Qarrow))
20310 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20311 else if (EQ (pointer, Qhand))
20312 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20313 else if (EQ (pointer, Qtext))
20314 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20315 else if (EQ (pointer, intern ("hdrag")))
20316 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20317 #ifdef HAVE_X_WINDOWS
20318 else if (EQ (pointer, intern ("vdrag")))
20319 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20320 #endif
20321 else if (EQ (pointer, intern ("hourglass")))
20322 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20323 else if (EQ (pointer, Qmodeline))
20324 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20325 else
20326 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20327 }
20328
20329 #ifndef HAVE_CARBON
20330 if (cursor != No_Cursor)
20331 #else
20332 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20333 #endif
20334 rif->define_frame_cursor (f, cursor);
20335 }
20336
20337 /* Take proper action when mouse has moved to the mode or header line
20338 or marginal area AREA of window W, x-position X and y-position Y.
20339 X is relative to the start of the text display area of W, so the
20340 width of bitmap areas and scroll bars must be subtracted to get a
20341 position relative to the start of the mode line. */
20342
20343 static void
20344 note_mode_line_or_margin_highlight (w, x, y, area)
20345 struct window *w;
20346 int x, y;
20347 enum window_part area;
20348 {
20349 struct frame *f = XFRAME (w->frame);
20350 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20351 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20352 Lisp_Object pointer = Qnil;
20353 int charpos, dx, dy, width, height;
20354 Lisp_Object string, object = Qnil;
20355 Lisp_Object pos, help, image;
20356
20357 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20358 string = mode_line_string (w, area, &x, &y, &charpos,
20359 &object, &dx, &dy, &width, &height);
20360 else
20361 {
20362 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20363 string = marginal_area_string (w, area, &x, &y, &charpos,
20364 &object, &dx, &dy, &width, &height);
20365 }
20366
20367 help = Qnil;
20368
20369 if (IMAGEP (object))
20370 {
20371 Lisp_Object image_map, hotspot;
20372 if ((image_map = Fplist_get (XCDR (object), QCmap),
20373 !NILP (image_map))
20374 && (hotspot = find_hot_spot (image_map, dx, dy),
20375 CONSP (hotspot))
20376 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20377 {
20378 Lisp_Object area_id, plist;
20379
20380 area_id = XCAR (hotspot);
20381 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20382 If so, we could look for mouse-enter, mouse-leave
20383 properties in PLIST (and do something...). */
20384 if ((plist = XCDR (hotspot), CONSP (plist)))
20385 {
20386 pointer = Fplist_get (plist, Qpointer);
20387 if (NILP (pointer))
20388 pointer = Qhand;
20389 help = Fplist_get (plist, Qhelp_echo);
20390 if (!NILP (help))
20391 {
20392 help_echo_string = help;
20393 /* Is this correct? ++kfs */
20394 XSETWINDOW (help_echo_window, w);
20395 help_echo_object = w->buffer;
20396 help_echo_pos = charpos;
20397 }
20398 }
20399 if (NILP (pointer))
20400 pointer = Fplist_get (XCDR (object), QCpointer);
20401 }
20402 }
20403
20404 if (STRINGP (string))
20405 {
20406 pos = make_number (charpos);
20407 /* If we're on a string with `help-echo' text property, arrange
20408 for the help to be displayed. This is done by setting the
20409 global variable help_echo_string to the help string. */
20410 help = Fget_text_property (pos, Qhelp_echo, string);
20411 if (!NILP (help))
20412 {
20413 help_echo_string = help;
20414 XSETWINDOW (help_echo_window, w);
20415 help_echo_object = string;
20416 help_echo_pos = charpos;
20417 }
20418
20419 if (NILP (pointer))
20420 pointer = Fget_text_property (pos, Qpointer, string);
20421
20422 /* Change the mouse pointer according to what is under X/Y. */
20423 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20424 {
20425 Lisp_Object map;
20426 map = Fget_text_property (pos, Qlocal_map, string);
20427 if (!KEYMAPP (map))
20428 map = Fget_text_property (pos, Qkeymap, string);
20429 if (!KEYMAPP (map))
20430 cursor = dpyinfo->vertical_scroll_bar_cursor;
20431 }
20432 }
20433
20434 define_frame_cursor1 (f, cursor, pointer);
20435 }
20436
20437
20438 /* EXPORT:
20439 Take proper action when the mouse has moved to position X, Y on
20440 frame F as regards highlighting characters that have mouse-face
20441 properties. Also de-highlighting chars where the mouse was before.
20442 X and Y can be negative or out of range. */
20443
20444 void
20445 note_mouse_highlight (f, x, y)
20446 struct frame *f;
20447 int x, y;
20448 {
20449 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20450 enum window_part part;
20451 Lisp_Object window;
20452 struct window *w;
20453 Cursor cursor = No_Cursor;
20454 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20455 struct buffer *b;
20456
20457 /* When a menu is active, don't highlight because this looks odd. */
20458 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20459 if (popup_activated ())
20460 return;
20461 #endif
20462
20463 if (NILP (Vmouse_highlight)
20464 || !f->glyphs_initialized_p)
20465 return;
20466
20467 dpyinfo->mouse_face_mouse_x = x;
20468 dpyinfo->mouse_face_mouse_y = y;
20469 dpyinfo->mouse_face_mouse_frame = f;
20470
20471 if (dpyinfo->mouse_face_defer)
20472 return;
20473
20474 if (gc_in_progress)
20475 {
20476 dpyinfo->mouse_face_deferred_gc = 1;
20477 return;
20478 }
20479
20480 /* Which window is that in? */
20481 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20482
20483 /* If we were displaying active text in another window, clear that. */
20484 if (! EQ (window, dpyinfo->mouse_face_window))
20485 clear_mouse_face (dpyinfo);
20486
20487 /* Not on a window -> return. */
20488 if (!WINDOWP (window))
20489 return;
20490
20491 /* Reset help_echo_string. It will get recomputed below. */
20492 help_echo_string = Qnil;
20493
20494 /* Convert to window-relative pixel coordinates. */
20495 w = XWINDOW (window);
20496 frame_to_window_pixel_xy (w, &x, &y);
20497
20498 /* Handle tool-bar window differently since it doesn't display a
20499 buffer. */
20500 if (EQ (window, f->tool_bar_window))
20501 {
20502 note_tool_bar_highlight (f, x, y);
20503 return;
20504 }
20505
20506 /* Mouse is on the mode, header line or margin? */
20507 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20508 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20509 {
20510 note_mode_line_or_margin_highlight (w, x, y, part);
20511 return;
20512 }
20513
20514 if (part == ON_VERTICAL_BORDER)
20515 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20516 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20517 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20518 else
20519 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20520
20521 /* Are we in a window whose display is up to date?
20522 And verify the buffer's text has not changed. */
20523 b = XBUFFER (w->buffer);
20524 if (part == ON_TEXT
20525 && EQ (w->window_end_valid, w->buffer)
20526 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20527 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20528 {
20529 int hpos, vpos, pos, i, dx, dy, area;
20530 struct glyph *glyph;
20531 Lisp_Object object;
20532 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20533 Lisp_Object *overlay_vec = NULL;
20534 int len, noverlays;
20535 struct buffer *obuf;
20536 int obegv, ozv, same_region;
20537
20538 /* Find the glyph under X/Y. */
20539 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20540
20541 /* Look for :pointer property on image. */
20542 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20543 {
20544 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20545 if (img != NULL && IMAGEP (img->spec))
20546 {
20547 Lisp_Object image_map, hotspot;
20548 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20549 !NILP (image_map))
20550 && (hotspot = find_hot_spot (image_map, dx, dy),
20551 CONSP (hotspot))
20552 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20553 {
20554 Lisp_Object area_id, plist;
20555
20556 area_id = XCAR (hotspot);
20557 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20558 If so, we could look for mouse-enter, mouse-leave
20559 properties in PLIST (and do something...). */
20560 if ((plist = XCDR (hotspot), CONSP (plist)))
20561 {
20562 pointer = Fplist_get (plist, Qpointer);
20563 if (NILP (pointer))
20564 pointer = Qhand;
20565 help_echo_string = Fplist_get (plist, Qhelp_echo);
20566 if (!NILP (help_echo_string))
20567 {
20568 help_echo_window = window;
20569 help_echo_object = glyph->object;
20570 help_echo_pos = glyph->charpos;
20571 }
20572 }
20573 }
20574 if (NILP (pointer))
20575 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20576 }
20577 }
20578
20579 /* Clear mouse face if X/Y not over text. */
20580 if (glyph == NULL
20581 || area != TEXT_AREA
20582 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20583 {
20584 if (clear_mouse_face (dpyinfo))
20585 cursor = No_Cursor;
20586 if (NILP (pointer))
20587 {
20588 if (area != TEXT_AREA)
20589 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20590 else
20591 pointer = Vvoid_text_area_pointer;
20592 }
20593 goto set_cursor;
20594 }
20595
20596 pos = glyph->charpos;
20597 object = glyph->object;
20598 if (!STRINGP (object) && !BUFFERP (object))
20599 goto set_cursor;
20600
20601 /* If we get an out-of-range value, return now; avoid an error. */
20602 if (BUFFERP (object) && pos > BUF_Z (b))
20603 goto set_cursor;
20604
20605 /* Make the window's buffer temporarily current for
20606 overlays_at and compute_char_face. */
20607 obuf = current_buffer;
20608 current_buffer = b;
20609 obegv = BEGV;
20610 ozv = ZV;
20611 BEGV = BEG;
20612 ZV = Z;
20613
20614 /* Is this char mouse-active or does it have help-echo? */
20615 position = make_number (pos);
20616
20617 if (BUFFERP (object))
20618 {
20619 /* Put all the overlays we want in a vector in overlay_vec.
20620 Store the length in len. If there are more than 10, make
20621 enough space for all, and try again. */
20622 len = 10;
20623 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20624 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20625 if (noverlays > len)
20626 {
20627 len = noverlays;
20628 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20629 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20630 }
20631
20632 /* Sort overlays into increasing priority order. */
20633 noverlays = sort_overlays (overlay_vec, noverlays, w);
20634 }
20635 else
20636 noverlays = 0;
20637
20638 same_region = (EQ (window, dpyinfo->mouse_face_window)
20639 && vpos >= dpyinfo->mouse_face_beg_row
20640 && vpos <= dpyinfo->mouse_face_end_row
20641 && (vpos > dpyinfo->mouse_face_beg_row
20642 || hpos >= dpyinfo->mouse_face_beg_col)
20643 && (vpos < dpyinfo->mouse_face_end_row
20644 || hpos < dpyinfo->mouse_face_end_col
20645 || dpyinfo->mouse_face_past_end));
20646
20647 if (same_region)
20648 cursor = No_Cursor;
20649
20650 /* Check mouse-face highlighting. */
20651 if (! same_region
20652 /* If there exists an overlay with mouse-face overlapping
20653 the one we are currently highlighting, we have to
20654 check if we enter the overlapping overlay, and then
20655 highlight only that. */
20656 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20657 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20658 {
20659 /* Find the highest priority overlay that has a mouse-face
20660 property. */
20661 overlay = Qnil;
20662 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20663 {
20664 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20665 if (!NILP (mouse_face))
20666 overlay = overlay_vec[i];
20667 }
20668
20669 /* If we're actually highlighting the same overlay as
20670 before, there's no need to do that again. */
20671 if (!NILP (overlay)
20672 && EQ (overlay, dpyinfo->mouse_face_overlay))
20673 goto check_help_echo;
20674
20675 dpyinfo->mouse_face_overlay = overlay;
20676
20677 /* Clear the display of the old active region, if any. */
20678 if (clear_mouse_face (dpyinfo))
20679 cursor = No_Cursor;
20680
20681 /* If no overlay applies, get a text property. */
20682 if (NILP (overlay))
20683 mouse_face = Fget_text_property (position, Qmouse_face, object);
20684
20685 /* Handle the overlay case. */
20686 if (!NILP (overlay))
20687 {
20688 /* Find the range of text around this char that
20689 should be active. */
20690 Lisp_Object before, after;
20691 int ignore;
20692
20693 before = Foverlay_start (overlay);
20694 after = Foverlay_end (overlay);
20695 /* Record this as the current active region. */
20696 fast_find_position (w, XFASTINT (before),
20697 &dpyinfo->mouse_face_beg_col,
20698 &dpyinfo->mouse_face_beg_row,
20699 &dpyinfo->mouse_face_beg_x,
20700 &dpyinfo->mouse_face_beg_y, Qnil);
20701
20702 dpyinfo->mouse_face_past_end
20703 = !fast_find_position (w, XFASTINT (after),
20704 &dpyinfo->mouse_face_end_col,
20705 &dpyinfo->mouse_face_end_row,
20706 &dpyinfo->mouse_face_end_x,
20707 &dpyinfo->mouse_face_end_y, Qnil);
20708 dpyinfo->mouse_face_window = window;
20709
20710 dpyinfo->mouse_face_face_id
20711 = face_at_buffer_position (w, pos, 0, 0,
20712 &ignore, pos + 1,
20713 !dpyinfo->mouse_face_hidden);
20714
20715 /* Display it as active. */
20716 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20717 cursor = No_Cursor;
20718 }
20719 /* Handle the text property case. */
20720 else if (!NILP (mouse_face) && BUFFERP (object))
20721 {
20722 /* Find the range of text around this char that
20723 should be active. */
20724 Lisp_Object before, after, beginning, end;
20725 int ignore;
20726
20727 beginning = Fmarker_position (w->start);
20728 end = make_number (BUF_Z (XBUFFER (object))
20729 - XFASTINT (w->window_end_pos));
20730 before
20731 = Fprevious_single_property_change (make_number (pos + 1),
20732 Qmouse_face,
20733 object, beginning);
20734 after
20735 = Fnext_single_property_change (position, Qmouse_face,
20736 object, end);
20737
20738 /* Record this as the current active region. */
20739 fast_find_position (w, XFASTINT (before),
20740 &dpyinfo->mouse_face_beg_col,
20741 &dpyinfo->mouse_face_beg_row,
20742 &dpyinfo->mouse_face_beg_x,
20743 &dpyinfo->mouse_face_beg_y, Qnil);
20744 dpyinfo->mouse_face_past_end
20745 = !fast_find_position (w, XFASTINT (after),
20746 &dpyinfo->mouse_face_end_col,
20747 &dpyinfo->mouse_face_end_row,
20748 &dpyinfo->mouse_face_end_x,
20749 &dpyinfo->mouse_face_end_y, Qnil);
20750 dpyinfo->mouse_face_window = window;
20751
20752 if (BUFFERP (object))
20753 dpyinfo->mouse_face_face_id
20754 = face_at_buffer_position (w, pos, 0, 0,
20755 &ignore, pos + 1,
20756 !dpyinfo->mouse_face_hidden);
20757
20758 /* Display it as active. */
20759 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20760 cursor = No_Cursor;
20761 }
20762 else if (!NILP (mouse_face) && STRINGP (object))
20763 {
20764 Lisp_Object b, e;
20765 int ignore;
20766
20767 b = Fprevious_single_property_change (make_number (pos + 1),
20768 Qmouse_face,
20769 object, Qnil);
20770 e = Fnext_single_property_change (position, Qmouse_face,
20771 object, Qnil);
20772 if (NILP (b))
20773 b = make_number (0);
20774 if (NILP (e))
20775 e = make_number (SCHARS (object) - 1);
20776 fast_find_string_pos (w, XINT (b), object,
20777 &dpyinfo->mouse_face_beg_col,
20778 &dpyinfo->mouse_face_beg_row,
20779 &dpyinfo->mouse_face_beg_x,
20780 &dpyinfo->mouse_face_beg_y, 0);
20781 fast_find_string_pos (w, XINT (e), object,
20782 &dpyinfo->mouse_face_end_col,
20783 &dpyinfo->mouse_face_end_row,
20784 &dpyinfo->mouse_face_end_x,
20785 &dpyinfo->mouse_face_end_y, 1);
20786 dpyinfo->mouse_face_past_end = 0;
20787 dpyinfo->mouse_face_window = window;
20788 dpyinfo->mouse_face_face_id
20789 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20790 glyph->face_id, 1);
20791 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20792 cursor = No_Cursor;
20793 }
20794 else if (STRINGP (object) && NILP (mouse_face))
20795 {
20796 /* A string which doesn't have mouse-face, but
20797 the text ``under'' it might have. */
20798 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20799 int start = MATRIX_ROW_START_CHARPOS (r);
20800
20801 pos = string_buffer_position (w, object, start);
20802 if (pos > 0)
20803 mouse_face = get_char_property_and_overlay (make_number (pos),
20804 Qmouse_face,
20805 w->buffer,
20806 &overlay);
20807 if (!NILP (mouse_face) && !NILP (overlay))
20808 {
20809 Lisp_Object before = Foverlay_start (overlay);
20810 Lisp_Object after = Foverlay_end (overlay);
20811 int ignore;
20812
20813 /* Note that we might not be able to find position
20814 BEFORE in the glyph matrix if the overlay is
20815 entirely covered by a `display' property. In
20816 this case, we overshoot. So let's stop in
20817 the glyph matrix before glyphs for OBJECT. */
20818 fast_find_position (w, XFASTINT (before),
20819 &dpyinfo->mouse_face_beg_col,
20820 &dpyinfo->mouse_face_beg_row,
20821 &dpyinfo->mouse_face_beg_x,
20822 &dpyinfo->mouse_face_beg_y,
20823 object);
20824
20825 dpyinfo->mouse_face_past_end
20826 = !fast_find_position (w, XFASTINT (after),
20827 &dpyinfo->mouse_face_end_col,
20828 &dpyinfo->mouse_face_end_row,
20829 &dpyinfo->mouse_face_end_x,
20830 &dpyinfo->mouse_face_end_y,
20831 Qnil);
20832 dpyinfo->mouse_face_window = window;
20833 dpyinfo->mouse_face_face_id
20834 = face_at_buffer_position (w, pos, 0, 0,
20835 &ignore, pos + 1,
20836 !dpyinfo->mouse_face_hidden);
20837
20838 /* Display it as active. */
20839 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20840 cursor = No_Cursor;
20841 }
20842 }
20843 }
20844
20845 check_help_echo:
20846
20847 /* Look for a `help-echo' property. */
20848 if (NILP (help_echo_string)) {
20849 Lisp_Object help, overlay;
20850
20851 /* Check overlays first. */
20852 help = overlay = Qnil;
20853 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20854 {
20855 overlay = overlay_vec[i];
20856 help = Foverlay_get (overlay, Qhelp_echo);
20857 }
20858
20859 if (!NILP (help))
20860 {
20861 help_echo_string = help;
20862 help_echo_window = window;
20863 help_echo_object = overlay;
20864 help_echo_pos = pos;
20865 }
20866 else
20867 {
20868 Lisp_Object object = glyph->object;
20869 int charpos = glyph->charpos;
20870
20871 /* Try text properties. */
20872 if (STRINGP (object)
20873 && charpos >= 0
20874 && charpos < SCHARS (object))
20875 {
20876 help = Fget_text_property (make_number (charpos),
20877 Qhelp_echo, object);
20878 if (NILP (help))
20879 {
20880 /* If the string itself doesn't specify a help-echo,
20881 see if the buffer text ``under'' it does. */
20882 struct glyph_row *r
20883 = MATRIX_ROW (w->current_matrix, vpos);
20884 int start = MATRIX_ROW_START_CHARPOS (r);
20885 int pos = string_buffer_position (w, object, start);
20886 if (pos > 0)
20887 {
20888 help = Fget_char_property (make_number (pos),
20889 Qhelp_echo, w->buffer);
20890 if (!NILP (help))
20891 {
20892 charpos = pos;
20893 object = w->buffer;
20894 }
20895 }
20896 }
20897 }
20898 else if (BUFFERP (object)
20899 && charpos >= BEGV
20900 && charpos < ZV)
20901 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20902 object);
20903
20904 if (!NILP (help))
20905 {
20906 help_echo_string = help;
20907 help_echo_window = window;
20908 help_echo_object = object;
20909 help_echo_pos = charpos;
20910 }
20911 }
20912 }
20913
20914 /* Look for a `pointer' property. */
20915 if (NILP (pointer))
20916 {
20917 /* Check overlays first. */
20918 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
20919 pointer = Foverlay_get (overlay_vec[i], Qpointer);
20920
20921 if (NILP (pointer))
20922 {
20923 Lisp_Object object = glyph->object;
20924 int charpos = glyph->charpos;
20925
20926 /* Try text properties. */
20927 if (STRINGP (object)
20928 && charpos >= 0
20929 && charpos < SCHARS (object))
20930 {
20931 pointer = Fget_text_property (make_number (charpos),
20932 Qpointer, object);
20933 if (NILP (pointer))
20934 {
20935 /* If the string itself doesn't specify a pointer,
20936 see if the buffer text ``under'' it does. */
20937 struct glyph_row *r
20938 = MATRIX_ROW (w->current_matrix, vpos);
20939 int start = MATRIX_ROW_START_CHARPOS (r);
20940 int pos = string_buffer_position (w, object, start);
20941 if (pos > 0)
20942 pointer = Fget_char_property (make_number (pos),
20943 Qpointer, w->buffer);
20944 }
20945 }
20946 else if (BUFFERP (object)
20947 && charpos >= BEGV
20948 && charpos < ZV)
20949 pointer = Fget_text_property (make_number (charpos),
20950 Qpointer, object);
20951 }
20952 }
20953
20954 BEGV = obegv;
20955 ZV = ozv;
20956 current_buffer = obuf;
20957 }
20958
20959 set_cursor:
20960
20961 define_frame_cursor1 (f, cursor, pointer);
20962 }
20963
20964
20965 /* EXPORT for RIF:
20966 Clear any mouse-face on window W. This function is part of the
20967 redisplay interface, and is called from try_window_id and similar
20968 functions to ensure the mouse-highlight is off. */
20969
20970 void
20971 x_clear_window_mouse_face (w)
20972 struct window *w;
20973 {
20974 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20975 Lisp_Object window;
20976
20977 BLOCK_INPUT;
20978 XSETWINDOW (window, w);
20979 if (EQ (window, dpyinfo->mouse_face_window))
20980 clear_mouse_face (dpyinfo);
20981 UNBLOCK_INPUT;
20982 }
20983
20984
20985 /* EXPORT:
20986 Just discard the mouse face information for frame F, if any.
20987 This is used when the size of F is changed. */
20988
20989 void
20990 cancel_mouse_face (f)
20991 struct frame *f;
20992 {
20993 Lisp_Object window;
20994 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20995
20996 window = dpyinfo->mouse_face_window;
20997 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20998 {
20999 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21000 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21001 dpyinfo->mouse_face_window = Qnil;
21002 }
21003 }
21004
21005
21006 #endif /* HAVE_WINDOW_SYSTEM */
21007
21008 \f
21009 /***********************************************************************
21010 Exposure Events
21011 ***********************************************************************/
21012
21013 #ifdef HAVE_WINDOW_SYSTEM
21014
21015 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21016 which intersects rectangle R. R is in window-relative coordinates. */
21017
21018 static void
21019 expose_area (w, row, r, area)
21020 struct window *w;
21021 struct glyph_row *row;
21022 XRectangle *r;
21023 enum glyph_row_area area;
21024 {
21025 struct glyph *first = row->glyphs[area];
21026 struct glyph *end = row->glyphs[area] + row->used[area];
21027 struct glyph *last;
21028 int first_x, start_x, x;
21029
21030 if (area == TEXT_AREA && row->fill_line_p)
21031 /* If row extends face to end of line write the whole line. */
21032 draw_glyphs (w, 0, row, area,
21033 0, row->used[area],
21034 DRAW_NORMAL_TEXT, 0);
21035 else
21036 {
21037 /* Set START_X to the window-relative start position for drawing glyphs of
21038 AREA. The first glyph of the text area can be partially visible.
21039 The first glyphs of other areas cannot. */
21040 start_x = window_box_left_offset (w, area);
21041 x = start_x;
21042 if (area == TEXT_AREA)
21043 x += row->x;
21044
21045 /* Find the first glyph that must be redrawn. */
21046 while (first < end
21047 && x + first->pixel_width < r->x)
21048 {
21049 x += first->pixel_width;
21050 ++first;
21051 }
21052
21053 /* Find the last one. */
21054 last = first;
21055 first_x = x;
21056 while (last < end
21057 && x < r->x + r->width)
21058 {
21059 x += last->pixel_width;
21060 ++last;
21061 }
21062
21063 /* Repaint. */
21064 if (last > first)
21065 draw_glyphs (w, first_x - start_x, row, area,
21066 first - row->glyphs[area], last - row->glyphs[area],
21067 DRAW_NORMAL_TEXT, 0);
21068 }
21069 }
21070
21071
21072 /* Redraw the parts of the glyph row ROW on window W intersecting
21073 rectangle R. R is in window-relative coordinates. Value is
21074 non-zero if mouse-face was overwritten. */
21075
21076 static int
21077 expose_line (w, row, r)
21078 struct window *w;
21079 struct glyph_row *row;
21080 XRectangle *r;
21081 {
21082 xassert (row->enabled_p);
21083
21084 if (row->mode_line_p || w->pseudo_window_p)
21085 draw_glyphs (w, 0, row, TEXT_AREA,
21086 0, row->used[TEXT_AREA],
21087 DRAW_NORMAL_TEXT, 0);
21088 else
21089 {
21090 if (row->used[LEFT_MARGIN_AREA])
21091 expose_area (w, row, r, LEFT_MARGIN_AREA);
21092 if (row->used[TEXT_AREA])
21093 expose_area (w, row, r, TEXT_AREA);
21094 if (row->used[RIGHT_MARGIN_AREA])
21095 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21096 draw_row_fringe_bitmaps (w, row);
21097 }
21098
21099 return row->mouse_face_p;
21100 }
21101
21102
21103 /* Redraw those parts of glyphs rows during expose event handling that
21104 overlap other rows. Redrawing of an exposed line writes over parts
21105 of lines overlapping that exposed line; this function fixes that.
21106
21107 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21108 row in W's current matrix that is exposed and overlaps other rows.
21109 LAST_OVERLAPPING_ROW is the last such row. */
21110
21111 static void
21112 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21113 struct window *w;
21114 struct glyph_row *first_overlapping_row;
21115 struct glyph_row *last_overlapping_row;
21116 {
21117 struct glyph_row *row;
21118
21119 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21120 if (row->overlapping_p)
21121 {
21122 xassert (row->enabled_p && !row->mode_line_p);
21123
21124 if (row->used[LEFT_MARGIN_AREA])
21125 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21126
21127 if (row->used[TEXT_AREA])
21128 x_fix_overlapping_area (w, row, TEXT_AREA);
21129
21130 if (row->used[RIGHT_MARGIN_AREA])
21131 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21132 }
21133 }
21134
21135
21136 /* Return non-zero if W's cursor intersects rectangle R. */
21137
21138 static int
21139 phys_cursor_in_rect_p (w, r)
21140 struct window *w;
21141 XRectangle *r;
21142 {
21143 XRectangle cr, result;
21144 struct glyph *cursor_glyph;
21145
21146 cursor_glyph = get_phys_cursor_glyph (w);
21147 if (cursor_glyph)
21148 {
21149 /* r is relative to W's box, but w->phys_cursor.x is relative
21150 to left edge of W's TEXT area. Adjust it. */
21151 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21152 cr.y = w->phys_cursor.y;
21153 cr.width = cursor_glyph->pixel_width;
21154 cr.height = w->phys_cursor_height;
21155 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21156 I assume the effect is the same -- and this is portable. */
21157 return x_intersect_rectangles (&cr, r, &result);
21158 }
21159 else
21160 return 0;
21161 }
21162
21163
21164 /* EXPORT:
21165 Draw a vertical window border to the right of window W if W doesn't
21166 have vertical scroll bars. */
21167
21168 void
21169 x_draw_vertical_border (w)
21170 struct window *w;
21171 {
21172 /* We could do better, if we knew what type of scroll-bar the adjacent
21173 windows (on either side) have... But we don't :-(
21174 However, I think this works ok. ++KFS 2003-04-25 */
21175
21176 /* Redraw borders between horizontally adjacent windows. Don't
21177 do it for frames with vertical scroll bars because either the
21178 right scroll bar of a window, or the left scroll bar of its
21179 neighbor will suffice as a border. */
21180 if (!WINDOW_RIGHTMOST_P (w)
21181 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21182 {
21183 int x0, x1, y0, y1;
21184
21185 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21186 y1 -= 1;
21187
21188 rif->draw_vertical_window_border (w, x1, y0, y1);
21189 }
21190 else if (!WINDOW_LEFTMOST_P (w)
21191 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21192 {
21193 int x0, x1, y0, y1;
21194
21195 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21196 y1 -= 1;
21197
21198 rif->draw_vertical_window_border (w, x0, y0, y1);
21199 }
21200 }
21201
21202
21203 /* Redraw the part of window W intersection rectangle FR. Pixel
21204 coordinates in FR are frame-relative. Call this function with
21205 input blocked. Value is non-zero if the exposure overwrites
21206 mouse-face. */
21207
21208 static int
21209 expose_window (w, fr)
21210 struct window *w;
21211 XRectangle *fr;
21212 {
21213 struct frame *f = XFRAME (w->frame);
21214 XRectangle wr, r;
21215 int mouse_face_overwritten_p = 0;
21216
21217 /* If window is not yet fully initialized, do nothing. This can
21218 happen when toolkit scroll bars are used and a window is split.
21219 Reconfiguring the scroll bar will generate an expose for a newly
21220 created window. */
21221 if (w->current_matrix == NULL)
21222 return 0;
21223
21224 /* When we're currently updating the window, display and current
21225 matrix usually don't agree. Arrange for a thorough display
21226 later. */
21227 if (w == updated_window)
21228 {
21229 SET_FRAME_GARBAGED (f);
21230 return 0;
21231 }
21232
21233 /* Frame-relative pixel rectangle of W. */
21234 wr.x = WINDOW_LEFT_EDGE_X (w);
21235 wr.y = WINDOW_TOP_EDGE_Y (w);
21236 wr.width = WINDOW_TOTAL_WIDTH (w);
21237 wr.height = WINDOW_TOTAL_HEIGHT (w);
21238
21239 if (x_intersect_rectangles (fr, &wr, &r))
21240 {
21241 int yb = window_text_bottom_y (w);
21242 struct glyph_row *row;
21243 int cursor_cleared_p;
21244 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21245
21246 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21247 r.x, r.y, r.width, r.height));
21248
21249 /* Convert to window coordinates. */
21250 r.x -= WINDOW_LEFT_EDGE_X (w);
21251 r.y -= WINDOW_TOP_EDGE_Y (w);
21252
21253 /* Turn off the cursor. */
21254 if (!w->pseudo_window_p
21255 && phys_cursor_in_rect_p (w, &r))
21256 {
21257 x_clear_cursor (w);
21258 cursor_cleared_p = 1;
21259 }
21260 else
21261 cursor_cleared_p = 0;
21262
21263 /* Update lines intersecting rectangle R. */
21264 first_overlapping_row = last_overlapping_row = NULL;
21265 for (row = w->current_matrix->rows;
21266 row->enabled_p;
21267 ++row)
21268 {
21269 int y0 = row->y;
21270 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21271
21272 if ((y0 >= r.y && y0 < r.y + r.height)
21273 || (y1 > r.y && y1 < r.y + r.height)
21274 || (r.y >= y0 && r.y < y1)
21275 || (r.y + r.height > y0 && r.y + r.height < y1))
21276 {
21277 if (row->overlapping_p)
21278 {
21279 if (first_overlapping_row == NULL)
21280 first_overlapping_row = row;
21281 last_overlapping_row = row;
21282 }
21283
21284 if (expose_line (w, row, &r))
21285 mouse_face_overwritten_p = 1;
21286 }
21287
21288 if (y1 >= yb)
21289 break;
21290 }
21291
21292 /* Display the mode line if there is one. */
21293 if (WINDOW_WANTS_MODELINE_P (w)
21294 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21295 row->enabled_p)
21296 && row->y < r.y + r.height)
21297 {
21298 if (expose_line (w, row, &r))
21299 mouse_face_overwritten_p = 1;
21300 }
21301
21302 if (!w->pseudo_window_p)
21303 {
21304 /* Fix the display of overlapping rows. */
21305 if (first_overlapping_row)
21306 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21307
21308 /* Draw border between windows. */
21309 x_draw_vertical_border (w);
21310
21311 /* Turn the cursor on again. */
21312 if (cursor_cleared_p)
21313 update_window_cursor (w, 1);
21314 }
21315 }
21316
21317 #ifdef HAVE_CARBON
21318 /* Display scroll bar for this window. */
21319 if (!NILP (w->vertical_scroll_bar))
21320 {
21321 /* ++KFS:
21322 If this doesn't work here (maybe some header files are missing),
21323 make a function in macterm.c and call it to do the job! */
21324 ControlHandle ch
21325 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21326
21327 Draw1Control (ch);
21328 }
21329 #endif
21330
21331 return mouse_face_overwritten_p;
21332 }
21333
21334
21335
21336 /* Redraw (parts) of all windows in the window tree rooted at W that
21337 intersect R. R contains frame pixel coordinates. Value is
21338 non-zero if the exposure overwrites mouse-face. */
21339
21340 static int
21341 expose_window_tree (w, r)
21342 struct window *w;
21343 XRectangle *r;
21344 {
21345 struct frame *f = XFRAME (w->frame);
21346 int mouse_face_overwritten_p = 0;
21347
21348 while (w && !FRAME_GARBAGED_P (f))
21349 {
21350 if (!NILP (w->hchild))
21351 mouse_face_overwritten_p
21352 |= expose_window_tree (XWINDOW (w->hchild), r);
21353 else if (!NILP (w->vchild))
21354 mouse_face_overwritten_p
21355 |= expose_window_tree (XWINDOW (w->vchild), r);
21356 else
21357 mouse_face_overwritten_p |= expose_window (w, r);
21358
21359 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21360 }
21361
21362 return mouse_face_overwritten_p;
21363 }
21364
21365
21366 /* EXPORT:
21367 Redisplay an exposed area of frame F. X and Y are the upper-left
21368 corner of the exposed rectangle. W and H are width and height of
21369 the exposed area. All are pixel values. W or H zero means redraw
21370 the entire frame. */
21371
21372 void
21373 expose_frame (f, x, y, w, h)
21374 struct frame *f;
21375 int x, y, w, h;
21376 {
21377 XRectangle r;
21378 int mouse_face_overwritten_p = 0;
21379
21380 TRACE ((stderr, "expose_frame "));
21381
21382 /* No need to redraw if frame will be redrawn soon. */
21383 if (FRAME_GARBAGED_P (f))
21384 {
21385 TRACE ((stderr, " garbaged\n"));
21386 return;
21387 }
21388
21389 #ifdef HAVE_CARBON
21390 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21391 or deactivated here, for unknown reasons, activated scroll bars
21392 are shown in deactivated frames in some instances. */
21393 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21394 activate_scroll_bars (f);
21395 else
21396 deactivate_scroll_bars (f);
21397 #endif
21398
21399 /* If basic faces haven't been realized yet, there is no point in
21400 trying to redraw anything. This can happen when we get an expose
21401 event while Emacs is starting, e.g. by moving another window. */
21402 if (FRAME_FACE_CACHE (f) == NULL
21403 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21404 {
21405 TRACE ((stderr, " no faces\n"));
21406 return;
21407 }
21408
21409 if (w == 0 || h == 0)
21410 {
21411 r.x = r.y = 0;
21412 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21413 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21414 }
21415 else
21416 {
21417 r.x = x;
21418 r.y = y;
21419 r.width = w;
21420 r.height = h;
21421 }
21422
21423 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21424 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21425
21426 if (WINDOWP (f->tool_bar_window))
21427 mouse_face_overwritten_p
21428 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21429
21430 #ifdef HAVE_X_WINDOWS
21431 #ifndef MSDOS
21432 #ifndef USE_X_TOOLKIT
21433 if (WINDOWP (f->menu_bar_window))
21434 mouse_face_overwritten_p
21435 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21436 #endif /* not USE_X_TOOLKIT */
21437 #endif
21438 #endif
21439
21440 /* Some window managers support a focus-follows-mouse style with
21441 delayed raising of frames. Imagine a partially obscured frame,
21442 and moving the mouse into partially obscured mouse-face on that
21443 frame. The visible part of the mouse-face will be highlighted,
21444 then the WM raises the obscured frame. With at least one WM, KDE
21445 2.1, Emacs is not getting any event for the raising of the frame
21446 (even tried with SubstructureRedirectMask), only Expose events.
21447 These expose events will draw text normally, i.e. not
21448 highlighted. Which means we must redo the highlight here.
21449 Subsume it under ``we love X''. --gerd 2001-08-15 */
21450 /* Included in Windows version because Windows most likely does not
21451 do the right thing if any third party tool offers
21452 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21453 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21454 {
21455 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21456 if (f == dpyinfo->mouse_face_mouse_frame)
21457 {
21458 int x = dpyinfo->mouse_face_mouse_x;
21459 int y = dpyinfo->mouse_face_mouse_y;
21460 clear_mouse_face (dpyinfo);
21461 note_mouse_highlight (f, x, y);
21462 }
21463 }
21464 }
21465
21466
21467 /* EXPORT:
21468 Determine the intersection of two rectangles R1 and R2. Return
21469 the intersection in *RESULT. Value is non-zero if RESULT is not
21470 empty. */
21471
21472 int
21473 x_intersect_rectangles (r1, r2, result)
21474 XRectangle *r1, *r2, *result;
21475 {
21476 XRectangle *left, *right;
21477 XRectangle *upper, *lower;
21478 int intersection_p = 0;
21479
21480 /* Rearrange so that R1 is the left-most rectangle. */
21481 if (r1->x < r2->x)
21482 left = r1, right = r2;
21483 else
21484 left = r2, right = r1;
21485
21486 /* X0 of the intersection is right.x0, if this is inside R1,
21487 otherwise there is no intersection. */
21488 if (right->x <= left->x + left->width)
21489 {
21490 result->x = right->x;
21491
21492 /* The right end of the intersection is the minimum of the
21493 the right ends of left and right. */
21494 result->width = (min (left->x + left->width, right->x + right->width)
21495 - result->x);
21496
21497 /* Same game for Y. */
21498 if (r1->y < r2->y)
21499 upper = r1, lower = r2;
21500 else
21501 upper = r2, lower = r1;
21502
21503 /* The upper end of the intersection is lower.y0, if this is inside
21504 of upper. Otherwise, there is no intersection. */
21505 if (lower->y <= upper->y + upper->height)
21506 {
21507 result->y = lower->y;
21508
21509 /* The lower end of the intersection is the minimum of the lower
21510 ends of upper and lower. */
21511 result->height = (min (lower->y + lower->height,
21512 upper->y + upper->height)
21513 - result->y);
21514 intersection_p = 1;
21515 }
21516 }
21517
21518 return intersection_p;
21519 }
21520
21521 #endif /* HAVE_WINDOW_SYSTEM */
21522
21523 \f
21524 /***********************************************************************
21525 Initialization
21526 ***********************************************************************/
21527
21528 void
21529 syms_of_xdisp ()
21530 {
21531 Vwith_echo_area_save_vector = Qnil;
21532 staticpro (&Vwith_echo_area_save_vector);
21533
21534 Vmessage_stack = Qnil;
21535 staticpro (&Vmessage_stack);
21536
21537 Qinhibit_redisplay = intern ("inhibit-redisplay");
21538 staticpro (&Qinhibit_redisplay);
21539
21540 message_dolog_marker1 = Fmake_marker ();
21541 staticpro (&message_dolog_marker1);
21542 message_dolog_marker2 = Fmake_marker ();
21543 staticpro (&message_dolog_marker2);
21544 message_dolog_marker3 = Fmake_marker ();
21545 staticpro (&message_dolog_marker3);
21546
21547 #if GLYPH_DEBUG
21548 defsubr (&Sdump_frame_glyph_matrix);
21549 defsubr (&Sdump_glyph_matrix);
21550 defsubr (&Sdump_glyph_row);
21551 defsubr (&Sdump_tool_bar_row);
21552 defsubr (&Strace_redisplay);
21553 defsubr (&Strace_to_stderr);
21554 #endif
21555 #ifdef HAVE_WINDOW_SYSTEM
21556 defsubr (&Stool_bar_lines_needed);
21557 defsubr (&Slookup_image_map);
21558 #endif
21559 defsubr (&Sformat_mode_line);
21560
21561 staticpro (&Qmenu_bar_update_hook);
21562 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21563
21564 staticpro (&Qoverriding_terminal_local_map);
21565 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21566
21567 staticpro (&Qoverriding_local_map);
21568 Qoverriding_local_map = intern ("overriding-local-map");
21569
21570 staticpro (&Qwindow_scroll_functions);
21571 Qwindow_scroll_functions = intern ("window-scroll-functions");
21572
21573 staticpro (&Qredisplay_end_trigger_functions);
21574 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21575
21576 staticpro (&Qinhibit_point_motion_hooks);
21577 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21578
21579 QCdata = intern (":data");
21580 staticpro (&QCdata);
21581 Qdisplay = intern ("display");
21582 staticpro (&Qdisplay);
21583 Qspace_width = intern ("space-width");
21584 staticpro (&Qspace_width);
21585 Qraise = intern ("raise");
21586 staticpro (&Qraise);
21587 Qspace = intern ("space");
21588 staticpro (&Qspace);
21589 Qmargin = intern ("margin");
21590 staticpro (&Qmargin);
21591 Qpointer = intern ("pointer");
21592 staticpro (&Qpointer);
21593 Qleft_margin = intern ("left-margin");
21594 staticpro (&Qleft_margin);
21595 Qright_margin = intern ("right-margin");
21596 staticpro (&Qright_margin);
21597 Qcenter = intern ("center");
21598 staticpro (&Qcenter);
21599 QCalign_to = intern (":align-to");
21600 staticpro (&QCalign_to);
21601 QCrelative_width = intern (":relative-width");
21602 staticpro (&QCrelative_width);
21603 QCrelative_height = intern (":relative-height");
21604 staticpro (&QCrelative_height);
21605 QCeval = intern (":eval");
21606 staticpro (&QCeval);
21607 QCpropertize = intern (":propertize");
21608 staticpro (&QCpropertize);
21609 QCfile = intern (":file");
21610 staticpro (&QCfile);
21611 Qfontified = intern ("fontified");
21612 staticpro (&Qfontified);
21613 Qfontification_functions = intern ("fontification-functions");
21614 staticpro (&Qfontification_functions);
21615 Qtrailing_whitespace = intern ("trailing-whitespace");
21616 staticpro (&Qtrailing_whitespace);
21617 Qimage = intern ("image");
21618 staticpro (&Qimage);
21619 QCmap = intern (":map");
21620 staticpro (&QCmap);
21621 QCpointer = intern (":pointer");
21622 staticpro (&QCpointer);
21623 Qrect = intern ("rect");
21624 staticpro (&Qrect);
21625 Qcircle = intern ("circle");
21626 staticpro (&Qcircle);
21627 Qpoly = intern ("poly");
21628 staticpro (&Qpoly);
21629 Qmessage_truncate_lines = intern ("message-truncate-lines");
21630 staticpro (&Qmessage_truncate_lines);
21631 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21632 staticpro (&Qcursor_in_non_selected_windows);
21633 Qgrow_only = intern ("grow-only");
21634 staticpro (&Qgrow_only);
21635 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21636 staticpro (&Qinhibit_menubar_update);
21637 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21638 staticpro (&Qinhibit_eval_during_redisplay);
21639 Qposition = intern ("position");
21640 staticpro (&Qposition);
21641 Qbuffer_position = intern ("buffer-position");
21642 staticpro (&Qbuffer_position);
21643 Qobject = intern ("object");
21644 staticpro (&Qobject);
21645 Qbar = intern ("bar");
21646 staticpro (&Qbar);
21647 Qhbar = intern ("hbar");
21648 staticpro (&Qhbar);
21649 Qbox = intern ("box");
21650 staticpro (&Qbox);
21651 Qhollow = intern ("hollow");
21652 staticpro (&Qhollow);
21653 Qhand = intern ("hand");
21654 staticpro (&Qhand);
21655 Qarrow = intern ("arrow");
21656 staticpro (&Qarrow);
21657 Qtext = intern ("text");
21658 staticpro (&Qtext);
21659 Qrisky_local_variable = intern ("risky-local-variable");
21660 staticpro (&Qrisky_local_variable);
21661 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21662 staticpro (&Qinhibit_free_realized_faces);
21663
21664 list_of_error = Fcons (Fcons (intern ("error"),
21665 Fcons (intern ("void-variable"), Qnil)),
21666 Qnil);
21667 staticpro (&list_of_error);
21668
21669 Qlast_arrow_position = intern ("last-arrow-position");
21670 staticpro (&Qlast_arrow_position);
21671 Qlast_arrow_string = intern ("last-arrow-string");
21672 staticpro (&Qlast_arrow_string);
21673
21674 Qoverlay_arrow_string = intern ("overlay-arrow-string");
21675 staticpro (&Qoverlay_arrow_string);
21676 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
21677 staticpro (&Qoverlay_arrow_bitmap);
21678
21679 echo_buffer[0] = echo_buffer[1] = Qnil;
21680 staticpro (&echo_buffer[0]);
21681 staticpro (&echo_buffer[1]);
21682
21683 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21684 staticpro (&echo_area_buffer[0]);
21685 staticpro (&echo_area_buffer[1]);
21686
21687 Vmessages_buffer_name = build_string ("*Messages*");
21688 staticpro (&Vmessages_buffer_name);
21689
21690 mode_line_proptrans_alist = Qnil;
21691 staticpro (&mode_line_proptrans_alist);
21692
21693 mode_line_string_list = Qnil;
21694 staticpro (&mode_line_string_list);
21695
21696 help_echo_string = Qnil;
21697 staticpro (&help_echo_string);
21698 help_echo_object = Qnil;
21699 staticpro (&help_echo_object);
21700 help_echo_window = Qnil;
21701 staticpro (&help_echo_window);
21702 previous_help_echo_string = Qnil;
21703 staticpro (&previous_help_echo_string);
21704 help_echo_pos = -1;
21705
21706 #ifdef HAVE_WINDOW_SYSTEM
21707 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
21708 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
21709 For example, if a block cursor is over a tab, it will be drawn as
21710 wide as that tab on the display. */);
21711 x_stretch_cursor_p = 0;
21712 #endif
21713
21714 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
21715 doc: /* *Non-nil means highlight trailing whitespace.
21716 The face used for trailing whitespace is `trailing-whitespace'. */);
21717 Vshow_trailing_whitespace = Qnil;
21718
21719 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
21720 doc: /* *The pointer shape to show in void text areas.
21721 Nil means to show the text pointer. Other options are `arrow', `text',
21722 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
21723 Vvoid_text_area_pointer = Qarrow;
21724
21725 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
21726 doc: /* Non-nil means don't actually do any redisplay.
21727 This is used for internal purposes. */);
21728 Vinhibit_redisplay = Qnil;
21729
21730 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
21731 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
21732 Vglobal_mode_string = Qnil;
21733
21734 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
21735 doc: /* Marker for where to display an arrow on top of the buffer text.
21736 This must be the beginning of a line in order to work.
21737 See also `overlay-arrow-string'. */);
21738 Voverlay_arrow_position = Qnil;
21739
21740 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
21741 doc: /* String to display as an arrow in non-window frames.
21742 See also `overlay-arrow-position'. */);
21743 Voverlay_arrow_string = Qnil;
21744
21745 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
21746 doc: /* List of variables (symbols) which hold markers for overlay arrows.
21747 The symbols on this list are examined during redisplay to determine
21748 where to display overlay arrows. */);
21749 Voverlay_arrow_variable_list
21750 = Fcons (intern ("overlay-arrow-position"), Qnil);
21751
21752 DEFVAR_INT ("scroll-step", &scroll_step,
21753 doc: /* *The number of lines to try scrolling a window by when point moves out.
21754 If that fails to bring point back on frame, point is centered instead.
21755 If this is zero, point is always centered after it moves off frame.
21756 If you want scrolling to always be a line at a time, you should set
21757 `scroll-conservatively' to a large value rather than set this to 1. */);
21758
21759 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
21760 doc: /* *Scroll up to this many lines, to bring point back on screen.
21761 A value of zero means to scroll the text to center point vertically
21762 in the window. */);
21763 scroll_conservatively = 0;
21764
21765 DEFVAR_INT ("scroll-margin", &scroll_margin,
21766 doc: /* *Number of lines of margin at the top and bottom of a window.
21767 Recenter the window whenever point gets within this many lines
21768 of the top or bottom of the window. */);
21769 scroll_margin = 0;
21770
21771 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
21772 doc: /* Pixels per inch on current display.
21773 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
21774 Vdisplay_pixels_per_inch = make_float (72.0);
21775
21776 #if GLYPH_DEBUG
21777 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
21778 #endif
21779
21780 DEFVAR_BOOL ("truncate-partial-width-windows",
21781 &truncate_partial_width_windows,
21782 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
21783 truncate_partial_width_windows = 1;
21784
21785 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
21786 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
21787 Any other value means to use the appropriate face, `mode-line',
21788 `header-line', or `menu' respectively. */);
21789 mode_line_inverse_video = 1;
21790
21791 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
21792 doc: /* *Maximum buffer size for which line number should be displayed.
21793 If the buffer is bigger than this, the line number does not appear
21794 in the mode line. A value of nil means no limit. */);
21795 Vline_number_display_limit = Qnil;
21796
21797 DEFVAR_INT ("line-number-display-limit-width",
21798 &line_number_display_limit_width,
21799 doc: /* *Maximum line width (in characters) for line number display.
21800 If the average length of the lines near point is bigger than this, then the
21801 line number may be omitted from the mode line. */);
21802 line_number_display_limit_width = 200;
21803
21804 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
21805 doc: /* *Non-nil means highlight region even in nonselected windows. */);
21806 highlight_nonselected_windows = 0;
21807
21808 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
21809 doc: /* Non-nil if more than one frame is visible on this display.
21810 Minibuffer-only frames don't count, but iconified frames do.
21811 This variable is not guaranteed to be accurate except while processing
21812 `frame-title-format' and `icon-title-format'. */);
21813
21814 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
21815 doc: /* Template for displaying the title bar of visible frames.
21816 \(Assuming the window manager supports this feature.)
21817 This variable has the same structure as `mode-line-format' (which see),
21818 and is used only on frames for which no explicit name has been set
21819 \(see `modify-frame-parameters'). */);
21820
21821 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
21822 doc: /* Template for displaying the title bar of an iconified frame.
21823 \(Assuming the window manager supports this feature.)
21824 This variable has the same structure as `mode-line-format' (which see),
21825 and is used only on frames for which no explicit name has been set
21826 \(see `modify-frame-parameters'). */);
21827 Vicon_title_format
21828 = Vframe_title_format
21829 = Fcons (intern ("multiple-frames"),
21830 Fcons (build_string ("%b"),
21831 Fcons (Fcons (empty_string,
21832 Fcons (intern ("invocation-name"),
21833 Fcons (build_string ("@"),
21834 Fcons (intern ("system-name"),
21835 Qnil)))),
21836 Qnil)));
21837
21838 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
21839 doc: /* Maximum number of lines to keep in the message log buffer.
21840 If nil, disable message logging. If t, log messages but don't truncate
21841 the buffer when it becomes large. */);
21842 Vmessage_log_max = make_number (50);
21843
21844 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
21845 doc: /* Functions called before redisplay, if window sizes have changed.
21846 The value should be a list of functions that take one argument.
21847 Just before redisplay, for each frame, if any of its windows have changed
21848 size since the last redisplay, or have been split or deleted,
21849 all the functions in the list are called, with the frame as argument. */);
21850 Vwindow_size_change_functions = Qnil;
21851
21852 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
21853 doc: /* List of Functions to call before redisplaying a window with scrolling.
21854 Each function is called with two arguments, the window
21855 and its new display-start position. Note that the value of `window-end'
21856 is not valid when these functions are called. */);
21857 Vwindow_scroll_functions = Qnil;
21858
21859 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
21860 doc: /* *Non-nil means autoselect window with mouse pointer. */);
21861 mouse_autoselect_window = 0;
21862
21863 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
21864 doc: /* *Non-nil means automatically resize tool-bars.
21865 This increases a tool-bar's height if not all tool-bar items are visible.
21866 It decreases a tool-bar's height when it would display blank lines
21867 otherwise. */);
21868 auto_resize_tool_bars_p = 1;
21869
21870 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21871 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
21872 auto_raise_tool_bar_buttons_p = 1;
21873
21874 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21875 doc: /* *Margin around tool-bar buttons in pixels.
21876 If an integer, use that for both horizontal and vertical margins.
21877 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
21878 HORZ specifying the horizontal margin, and VERT specifying the
21879 vertical margin. */);
21880 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
21881
21882 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
21883 doc: /* *Relief thickness of tool-bar buttons. */);
21884 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
21885
21886 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21887 doc: /* List of functions to call to fontify regions of text.
21888 Each function is called with one argument POS. Functions must
21889 fontify a region starting at POS in the current buffer, and give
21890 fontified regions the property `fontified'. */);
21891 Vfontification_functions = Qnil;
21892 Fmake_variable_buffer_local (Qfontification_functions);
21893
21894 DEFVAR_BOOL ("unibyte-display-via-language-environment",
21895 &unibyte_display_via_language_environment,
21896 doc: /* *Non-nil means display unibyte text according to language environment.
21897 Specifically this means that unibyte non-ASCII characters
21898 are displayed by converting them to the equivalent multibyte characters
21899 according to the current language environment. As a result, they are
21900 displayed according to the current fontset. */);
21901 unibyte_display_via_language_environment = 0;
21902
21903 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21904 doc: /* *Maximum height for resizing mini-windows.
21905 If a float, it specifies a fraction of the mini-window frame's height.
21906 If an integer, it specifies a number of lines. */);
21907 Vmax_mini_window_height = make_float (0.25);
21908
21909 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21910 doc: /* *How to resize mini-windows.
21911 A value of nil means don't automatically resize mini-windows.
21912 A value of t means resize them to fit the text displayed in them.
21913 A value of `grow-only', the default, means let mini-windows grow
21914 only, until their display becomes empty, at which point the windows
21915 go back to their normal size. */);
21916 Vresize_mini_windows = Qgrow_only;
21917
21918 DEFVAR_LISP ("cursor-in-non-selected-windows",
21919 &Vcursor_in_non_selected_windows,
21920 doc: /* *Cursor type to display in non-selected windows.
21921 t means to use hollow box cursor. See `cursor-type' for other values. */);
21922 Vcursor_in_non_selected_windows = Qt;
21923
21924 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21925 doc: /* Alist specifying how to blink the cursor off.
21926 Each element has the form (ON-STATE . OFF-STATE). Whenever the
21927 `cursor-type' frame-parameter or variable equals ON-STATE,
21928 comparing using `equal', Emacs uses OFF-STATE to specify
21929 how to blink it off. */);
21930 Vblink_cursor_alist = Qnil;
21931
21932 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
21933 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
21934 automatic_hscrolling_p = 1;
21935
21936 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
21937 doc: /* *How many columns away from the window edge point is allowed to get
21938 before automatic hscrolling will horizontally scroll the window. */);
21939 hscroll_margin = 5;
21940
21941 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
21942 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21943 When point is less than `automatic-hscroll-margin' columns from the window
21944 edge, automatic hscrolling will scroll the window by the amount of columns
21945 determined by this variable. If its value is a positive integer, scroll that
21946 many columns. If it's a positive floating-point number, it specifies the
21947 fraction of the window's width to scroll. If it's nil or zero, point will be
21948 centered horizontally after the scroll. Any other value, including negative
21949 numbers, are treated as if the value were zero.
21950
21951 Automatic hscrolling always moves point outside the scroll margin, so if
21952 point was more than scroll step columns inside the margin, the window will
21953 scroll more than the value given by the scroll step.
21954
21955 Note that the lower bound for automatic hscrolling specified by `scroll-left'
21956 and `scroll-right' overrides this variable's effect. */);
21957 Vhscroll_step = make_number (0);
21958
21959 DEFVAR_LISP ("image-types", &Vimage_types,
21960 doc: /* List of supported image types.
21961 Each element of the list is a symbol for a supported image type. */);
21962 Vimage_types = Qnil;
21963
21964 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21965 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
21966 Bind this around calls to `message' to let it take effect. */);
21967 message_truncate_lines = 0;
21968
21969 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21970 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
21971 Can be used to update submenus whose contents should vary. */);
21972 Vmenu_bar_update_hook = Qnil;
21973
21974 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21975 doc: /* Non-nil means don't update menu bars. Internal use only. */);
21976 inhibit_menubar_update = 0;
21977
21978 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21979 doc: /* Non-nil means don't eval Lisp during redisplay. */);
21980 inhibit_eval_during_redisplay = 0;
21981
21982 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21983 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21984 inhibit_free_realized_faces = 0;
21985
21986 #if GLYPH_DEBUG
21987 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21988 doc: /* Inhibit try_window_id display optimization. */);
21989 inhibit_try_window_id = 0;
21990
21991 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21992 doc: /* Inhibit try_window_reusing display optimization. */);
21993 inhibit_try_window_reusing = 0;
21994
21995 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21996 doc: /* Inhibit try_cursor_movement display optimization. */);
21997 inhibit_try_cursor_movement = 0;
21998 #endif /* GLYPH_DEBUG */
21999 }
22000
22001
22002 /* Initialize this module when Emacs starts. */
22003
22004 void
22005 init_xdisp ()
22006 {
22007 Lisp_Object root_window;
22008 struct window *mini_w;
22009
22010 current_header_line_height = current_mode_line_height = -1;
22011
22012 CHARPOS (this_line_start_pos) = 0;
22013
22014 mini_w = XWINDOW (minibuf_window);
22015 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22016
22017 if (!noninteractive)
22018 {
22019 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22020 int i;
22021
22022 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22023 set_window_height (root_window,
22024 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22025 0);
22026 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22027 set_window_height (minibuf_window, 1, 0);
22028
22029 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22030 mini_w->total_cols = make_number (FRAME_COLS (f));
22031
22032 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22033 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22034 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22035
22036 /* The default ellipsis glyphs `...'. */
22037 for (i = 0; i < 3; ++i)
22038 default_invis_vector[i] = make_number ('.');
22039 }
22040
22041 {
22042 /* Allocate the buffer for frame titles.
22043 Also used for `format-mode-line'. */
22044 int size = 100;
22045 frame_title_buf = (char *) xmalloc (size);
22046 frame_title_buf_end = frame_title_buf + size;
22047 frame_title_ptr = NULL;
22048 }
22049
22050 help_echo_showing_p = 0;
22051 }
22052
22053
22054 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22055 (do not change this comment) */