Removed %T in mode-line-format. Trivial documentation changes.
[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 Qmargin, Qpointer;
305 extern Lisp_Object Qheight;
306 extern Lisp_Object QCwidth, QCheight, QCascent;
307 extern Lisp_Object Qscroll_bar;
308
309 /* Non-nil means highlight trailing whitespace. */
310
311 Lisp_Object Vshow_trailing_whitespace;
312
313 #ifdef HAVE_WINDOW_SYSTEM
314 /* Non-nil means that newline may flow into the right fringe. */
315
316 Lisp_Object Voverflow_newline_into_fringe;
317 #endif /* HAVE_WINDOW_SYSTEM */
318
319 /* Test if overflow newline into fringe. Called with iterator IT
320 at or past right window margin, and with IT->current_x set. */
321
322 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
323 (!NILP (Voverflow_newline_into_fringe) \
324 && FRAME_WINDOW_P (it->f) \
325 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
326 && it->current_x == it->last_visible_x)
327
328 /* 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 /* Marker for where to display an arrow on top of the buffer text. */
408
409 Lisp_Object Voverlay_arrow_position;
410
411 /* String to display for the arrow. Only used on terminal frames. */
412
413 Lisp_Object Voverlay_arrow_string;
414
415 /* Values of those variables at last redisplay. However, if
416 Voverlay_arrow_position is a marker, last_arrow_position is its
417 numerical position. */
418
419 static Lisp_Object last_arrow_position, last_arrow_string;
420
421 /* Like mode-line-format, but for the title bar on a visible frame. */
422
423 Lisp_Object Vframe_title_format;
424
425 /* Like mode-line-format, but for the title bar on an iconified frame. */
426
427 Lisp_Object Vicon_title_format;
428
429 /* List of functions to call when a window's size changes. These
430 functions get one arg, a frame on which one or more windows' sizes
431 have changed. */
432
433 static Lisp_Object Vwindow_size_change_functions;
434
435 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
436
437 /* Nonzero if overlay arrow has been displayed once in this window. */
438
439 static int overlay_arrow_seen;
440
441 /* Nonzero means highlight the region even in nonselected windows. */
442
443 int highlight_nonselected_windows;
444
445 /* If cursor motion alone moves point off frame, try scrolling this
446 many lines up or down if that will bring it back. */
447
448 static EMACS_INT scroll_step;
449
450 /* Nonzero means scroll just far enough to bring point back on the
451 screen, when appropriate. */
452
453 static EMACS_INT scroll_conservatively;
454
455 /* Recenter the window whenever point gets within this many lines of
456 the top or bottom of the window. This value is translated into a
457 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
458 that there is really a fixed pixel height scroll margin. */
459
460 EMACS_INT scroll_margin;
461
462 /* Number of windows showing the buffer of the selected window (or
463 another buffer with the same base buffer). keyboard.c refers to
464 this. */
465
466 int buffer_shared;
467
468 /* Vector containing glyphs for an ellipsis `...'. */
469
470 static Lisp_Object default_invis_vector[3];
471
472 /* Zero means display the mode-line/header-line/menu-bar in the default face
473 (this slightly odd definition is for compatibility with previous versions
474 of emacs), non-zero means display them using their respective faces.
475
476 This variable is deprecated. */
477
478 int mode_line_inverse_video;
479
480 /* Prompt to display in front of the mini-buffer contents. */
481
482 Lisp_Object minibuf_prompt;
483
484 /* Width of current mini-buffer prompt. Only set after display_line
485 of the line that contains the prompt. */
486
487 int minibuf_prompt_width;
488
489 /* This is the window where the echo area message was displayed. It
490 is always a mini-buffer window, but it may not be the same window
491 currently active as a mini-buffer. */
492
493 Lisp_Object echo_area_window;
494
495 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
496 pushes the current message and the value of
497 message_enable_multibyte on the stack, the function restore_message
498 pops the stack and displays MESSAGE again. */
499
500 Lisp_Object Vmessage_stack;
501
502 /* Nonzero means multibyte characters were enabled when the echo area
503 message was specified. */
504
505 int message_enable_multibyte;
506
507 /* Nonzero if we should redraw the mode lines on the next redisplay. */
508
509 int update_mode_lines;
510
511 /* Nonzero if window sizes or contents have changed since last
512 redisplay that finished. */
513
514 int windows_or_buffers_changed;
515
516 /* Nonzero means a frame's cursor type has been changed. */
517
518 int cursor_type_changed;
519
520 /* Nonzero after display_mode_line if %l was used and it displayed a
521 line number. */
522
523 int line_number_displayed;
524
525 /* Maximum buffer size for which to display line numbers. */
526
527 Lisp_Object Vline_number_display_limit;
528
529 /* Line width to consider when repositioning for line number display. */
530
531 static EMACS_INT line_number_display_limit_width;
532
533 /* Number of lines to keep in the message log buffer. t means
534 infinite. nil means don't log at all. */
535
536 Lisp_Object Vmessage_log_max;
537
538 /* The name of the *Messages* buffer, a string. */
539
540 static Lisp_Object Vmessages_buffer_name;
541
542 /* Current, index 0, and last displayed echo area message. Either
543 buffers from echo_buffers, or nil to indicate no message. */
544
545 Lisp_Object echo_area_buffer[2];
546
547 /* The buffers referenced from echo_area_buffer. */
548
549 static Lisp_Object echo_buffer[2];
550
551 /* A vector saved used in with_area_buffer to reduce consing. */
552
553 static Lisp_Object Vwith_echo_area_save_vector;
554
555 /* Non-zero means display_echo_area should display the last echo area
556 message again. Set by redisplay_preserve_echo_area. */
557
558 static int display_last_displayed_message_p;
559
560 /* Nonzero if echo area is being used by print; zero if being used by
561 message. */
562
563 int message_buf_print;
564
565 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
566
567 Lisp_Object Qinhibit_menubar_update;
568 int inhibit_menubar_update;
569
570 /* Maximum height for resizing mini-windows. Either a float
571 specifying a fraction of the available height, or an integer
572 specifying a number of lines. */
573
574 Lisp_Object Vmax_mini_window_height;
575
576 /* Non-zero means messages should be displayed with truncated
577 lines instead of being continued. */
578
579 int message_truncate_lines;
580 Lisp_Object Qmessage_truncate_lines;
581
582 /* Set to 1 in clear_message to make redisplay_internal aware
583 of an emptied echo area. */
584
585 static int message_cleared_p;
586
587 /* Non-zero means we want a hollow cursor in windows that are not
588 selected. Zero means there's no cursor in such windows. */
589
590 Lisp_Object Vcursor_in_non_selected_windows;
591 Lisp_Object Qcursor_in_non_selected_windows;
592
593 /* How to blink the default frame cursor off. */
594 Lisp_Object Vblink_cursor_alist;
595
596 /* A scratch glyph row with contents used for generating truncation
597 glyphs. Also used in direct_output_for_insert. */
598
599 #define MAX_SCRATCH_GLYPHS 100
600 struct glyph_row scratch_glyph_row;
601 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
602
603 /* Ascent and height of the last line processed by move_it_to. */
604
605 static int last_max_ascent, last_height;
606
607 /* Non-zero if there's a help-echo in the echo area. */
608
609 int help_echo_showing_p;
610
611 /* If >= 0, computed, exact values of mode-line and header-line height
612 to use in the macros CURRENT_MODE_LINE_HEIGHT and
613 CURRENT_HEADER_LINE_HEIGHT. */
614
615 int current_mode_line_height, current_header_line_height;
616
617 /* The maximum distance to look ahead for text properties. Values
618 that are too small let us call compute_char_face and similar
619 functions too often which is expensive. Values that are too large
620 let us call compute_char_face and alike too often because we
621 might not be interested in text properties that far away. */
622
623 #define TEXT_PROP_DISTANCE_LIMIT 100
624
625 #if GLYPH_DEBUG
626
627 /* Variables to turn off display optimizations from Lisp. */
628
629 int inhibit_try_window_id, inhibit_try_window_reusing;
630 int inhibit_try_cursor_movement;
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG != 0. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 /* Non-zero means automatically scroll windows horizontally to make
649 point visible. */
650
651 int automatic_hscrolling_p;
652
653 /* How close to the margin can point get before the window is scrolled
654 horizontally. */
655 EMACS_INT hscroll_margin;
656
657 /* How much to scroll horizontally when point is inside the above margin. */
658 Lisp_Object Vhscroll_step;
659
660 /* A list of symbols, one for each supported image type. */
661
662 Lisp_Object Vimage_types;
663
664 /* The variable `resize-mini-windows'. If nil, don't resize
665 mini-windows. If t, always resize them to fit the text they
666 display. If `grow-only', let mini-windows grow only until they
667 become empty. */
668
669 Lisp_Object Vresize_mini_windows;
670
671 /* Buffer being redisplayed -- for redisplay_window_error. */
672
673 struct buffer *displayed_buffer;
674
675 /* Value returned from text property handlers (see below). */
676
677 enum prop_handled
678 {
679 HANDLED_NORMALLY,
680 HANDLED_RECOMPUTE_PROPS,
681 HANDLED_OVERLAY_STRING_CONSUMED,
682 HANDLED_RETURN
683 };
684
685 /* A description of text properties that redisplay is interested
686 in. */
687
688 struct props
689 {
690 /* The name of the property. */
691 Lisp_Object *name;
692
693 /* A unique index for the property. */
694 enum prop_idx idx;
695
696 /* A handler function called to set up iterator IT from the property
697 at IT's current position. Value is used to steer handle_stop. */
698 enum prop_handled (*handler) P_ ((struct it *it));
699 };
700
701 static enum prop_handled handle_face_prop P_ ((struct it *));
702 static enum prop_handled handle_invisible_prop P_ ((struct it *));
703 static enum prop_handled handle_display_prop P_ ((struct it *));
704 static enum prop_handled handle_composition_prop P_ ((struct it *));
705 static enum prop_handled handle_overlay_change P_ ((struct it *));
706 static enum prop_handled handle_fontified_prop P_ ((struct it *));
707
708 /* Properties handled by iterators. */
709
710 static struct props it_props[] =
711 {
712 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
713 /* Handle `face' before `display' because some sub-properties of
714 `display' need to know the face. */
715 {&Qface, FACE_PROP_IDX, handle_face_prop},
716 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
717 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
718 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
719 {NULL, 0, NULL}
720 };
721
722 /* Value is the position described by X. If X is a marker, value is
723 the marker_position of X. Otherwise, value is X. */
724
725 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
726
727 /* Enumeration returned by some move_it_.* functions internally. */
728
729 enum move_it_result
730 {
731 /* Not used. Undefined value. */
732 MOVE_UNDEFINED,
733
734 /* Move ended at the requested buffer position or ZV. */
735 MOVE_POS_MATCH_OR_ZV,
736
737 /* Move ended at the requested X pixel position. */
738 MOVE_X_REACHED,
739
740 /* Move within a line ended at the end of a line that must be
741 continued. */
742 MOVE_LINE_CONTINUED,
743
744 /* Move within a line ended at the end of a line that would
745 be displayed truncated. */
746 MOVE_LINE_TRUNCATED,
747
748 /* Move within a line ended at a line end. */
749 MOVE_NEWLINE_OR_CR
750 };
751
752 /* This counter is used to clear the face cache every once in a while
753 in redisplay_internal. It is incremented for each redisplay.
754 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
755 cleared. */
756
757 #define CLEAR_FACE_CACHE_COUNT 500
758 static int clear_face_cache_count;
759
760 /* Non-zero while redisplay_internal is in progress. */
761
762 int redisplaying_p;
763
764 /* Non-zero means don't free realized faces. Bound while freeing
765 realized faces is dangerous because glyph matrices might still
766 reference them. */
767
768 int inhibit_free_realized_faces;
769 Lisp_Object Qinhibit_free_realized_faces;
770
771 /* If a string, XTread_socket generates an event to display that string.
772 (The display is done in read_char.) */
773
774 Lisp_Object help_echo_string;
775 Lisp_Object help_echo_window;
776 Lisp_Object help_echo_object;
777 int help_echo_pos;
778
779 /* Temporary variable for XTread_socket. */
780
781 Lisp_Object previous_help_echo_string;
782
783
784 \f
785 /* Function prototypes. */
786
787 static void setup_for_ellipsis P_ ((struct it *));
788 static void mark_window_display_accurate_1 P_ ((struct window *, int));
789 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
790 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
791 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
792 static int redisplay_mode_lines P_ ((Lisp_Object, int));
793 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
794
795 #if 0
796 static int invisible_text_between_p P_ ((struct it *, int, int));
797 #endif
798
799 static int next_element_from_ellipsis P_ ((struct it *));
800 static void pint2str P_ ((char *, int, int));
801 static void pint2hrstr P_ ((char *, int, int));
802 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
803 struct text_pos));
804 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
805 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
806 static void store_frame_title_char P_ ((char));
807 static int store_frame_title P_ ((const unsigned char *, int, int));
808 static void x_consider_frame_title P_ ((Lisp_Object));
809 static void handle_stop P_ ((struct it *));
810 static int tool_bar_lines_needed P_ ((struct frame *));
811 static int single_display_prop_intangible_p P_ ((Lisp_Object));
812 static void ensure_echo_area_buffers P_ ((void));
813 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
814 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
815 static int with_echo_area_buffer P_ ((struct window *, int,
816 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
817 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
818 static void clear_garbaged_frames P_ ((void));
819 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
820 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
821 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
822 static int display_echo_area P_ ((struct window *));
823 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
824 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
825 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
826 static int string_char_and_length P_ ((const unsigned char *, int, int *));
827 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
828 struct text_pos));
829 static int compute_window_start_on_continuation_line P_ ((struct window *));
830 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
831 static void insert_left_trunc_glyphs P_ ((struct it *));
832 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
833 static void extend_face_to_end_of_line P_ ((struct it *));
834 static int append_space P_ ((struct it *, int));
835 static int make_cursor_line_fully_visible P_ ((struct window *));
836 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
837 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
838 static int trailing_whitespace_p P_ ((int));
839 static int message_log_check_duplicate P_ ((int, int, int, int));
840 static void push_it P_ ((struct it *));
841 static void pop_it P_ ((struct it *));
842 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
843 static void select_frame_for_redisplay P_ ((Lisp_Object));
844 static void redisplay_internal P_ ((int));
845 static int echo_area_display P_ ((int));
846 static void redisplay_windows P_ ((Lisp_Object));
847 static void redisplay_window P_ ((Lisp_Object, int));
848 static Lisp_Object redisplay_window_error ();
849 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
850 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
851 static void update_menu_bar P_ ((struct frame *, int));
852 static int try_window_reusing_current_matrix P_ ((struct window *));
853 static int try_window_id P_ ((struct window *));
854 static int display_line P_ ((struct it *));
855 static int display_mode_lines P_ ((struct window *));
856 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
857 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
858 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
859 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
860 static void display_menu_bar P_ ((struct window *));
861 static int display_count_lines P_ ((int, int, int, int, int *));
862 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
863 int, int, struct it *, int, int, int, int));
864 static void compute_line_metrics P_ ((struct it *));
865 static void run_redisplay_end_trigger_hook P_ ((struct it *));
866 static int get_overlay_strings P_ ((struct it *, int));
867 static void next_overlay_string P_ ((struct it *));
868 static void reseat P_ ((struct it *, struct text_pos, int));
869 static void reseat_1 P_ ((struct it *, struct text_pos, int));
870 static void back_to_previous_visible_line_start P_ ((struct it *));
871 static void reseat_at_previous_visible_line_start P_ ((struct it *));
872 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
873 static int next_element_from_display_vector P_ ((struct it *));
874 static int next_element_from_string P_ ((struct it *));
875 static int next_element_from_c_string P_ ((struct it *));
876 static int next_element_from_buffer P_ ((struct it *));
877 static int next_element_from_composition P_ ((struct it *));
878 static int next_element_from_image P_ ((struct it *));
879 static int next_element_from_stretch P_ ((struct it *));
880 static void load_overlay_strings P_ ((struct it *, int));
881 static int init_from_display_pos P_ ((struct it *, struct window *,
882 struct display_pos *));
883 static void reseat_to_string P_ ((struct it *, unsigned char *,
884 Lisp_Object, int, int, int, int));
885 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
886 int, int, int));
887 void move_it_vertically_backward P_ ((struct it *, int));
888 static void init_to_row_start P_ ((struct it *, struct window *,
889 struct glyph_row *));
890 static int init_to_row_end P_ ((struct it *, struct window *,
891 struct glyph_row *));
892 static void back_to_previous_line_start P_ ((struct it *));
893 static int forward_to_next_line_start P_ ((struct it *, int *));
894 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
895 Lisp_Object, int));
896 static struct text_pos string_pos P_ ((int, Lisp_Object));
897 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
898 static int number_of_chars P_ ((unsigned char *, int));
899 static void compute_stop_pos P_ ((struct it *));
900 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
901 Lisp_Object));
902 static int face_before_or_after_it_pos P_ ((struct it *, int));
903 static int next_overlay_change P_ ((int));
904 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
905 Lisp_Object, struct text_pos *,
906 int));
907 static int underlying_face_id P_ ((struct it *));
908 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
909 struct window *));
910
911 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
912 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
913
914 #ifdef HAVE_WINDOW_SYSTEM
915
916 static void update_tool_bar P_ ((struct frame *, int));
917 static void build_desired_tool_bar_string P_ ((struct frame *f));
918 static int redisplay_tool_bar P_ ((struct frame *));
919 static void display_tool_bar_line P_ ((struct it *));
920 static void notice_overwritten_cursor P_ ((struct window *,
921 enum glyph_row_area,
922 int, int, int, int));
923
924
925
926 #endif /* HAVE_WINDOW_SYSTEM */
927
928 \f
929 /***********************************************************************
930 Window display dimensions
931 ***********************************************************************/
932
933 /* Return the bottom boundary y-position for text lines in window W.
934 This is the first y position at which a line cannot start.
935 It is relative to the top of the window.
936
937 This is the height of W minus the height of a mode line, if any. */
938
939 INLINE int
940 window_text_bottom_y (w)
941 struct window *w;
942 {
943 int height = WINDOW_TOTAL_HEIGHT (w);
944
945 if (WINDOW_WANTS_MODELINE_P (w))
946 height -= CURRENT_MODE_LINE_HEIGHT (w);
947 return height;
948 }
949
950 /* Return the pixel width of display area AREA of window W. AREA < 0
951 means return the total width of W, not including fringes to
952 the left and right of the window. */
953
954 INLINE int
955 window_box_width (w, area)
956 struct window *w;
957 int area;
958 {
959 int cols = XFASTINT (w->total_cols);
960 int pixels = 0;
961
962 if (!w->pseudo_window_p)
963 {
964 cols -= WINDOW_SCROLL_BAR_COLS (w);
965
966 if (area == TEXT_AREA)
967 {
968 if (INTEGERP (w->left_margin_cols))
969 cols -= XFASTINT (w->left_margin_cols);
970 if (INTEGERP (w->right_margin_cols))
971 cols -= XFASTINT (w->right_margin_cols);
972 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
973 }
974 else if (area == LEFT_MARGIN_AREA)
975 {
976 cols = (INTEGERP (w->left_margin_cols)
977 ? XFASTINT (w->left_margin_cols) : 0);
978 pixels = 0;
979 }
980 else if (area == RIGHT_MARGIN_AREA)
981 {
982 cols = (INTEGERP (w->right_margin_cols)
983 ? XFASTINT (w->right_margin_cols) : 0);
984 pixels = 0;
985 }
986 }
987
988 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
989 }
990
991
992 /* Return the pixel height of the display area of window W, not
993 including mode lines of W, if any. */
994
995 INLINE int
996 window_box_height (w)
997 struct window *w;
998 {
999 struct frame *f = XFRAME (w->frame);
1000 int height = WINDOW_TOTAL_HEIGHT (w);
1001
1002 xassert (height >= 0);
1003
1004 /* Note: the code below that determines the mode-line/header-line
1005 height is essentially the same as that contained in the macro
1006 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1007 the appropriate glyph row has its `mode_line_p' flag set,
1008 and if it doesn't, uses estimate_mode_line_height instead. */
1009
1010 if (WINDOW_WANTS_MODELINE_P (w))
1011 {
1012 struct glyph_row *ml_row
1013 = (w->current_matrix && w->current_matrix->rows
1014 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1015 : 0);
1016 if (ml_row && ml_row->mode_line_p)
1017 height -= ml_row->height;
1018 else
1019 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1020 }
1021
1022 if (WINDOW_WANTS_HEADER_LINE_P (w))
1023 {
1024 struct glyph_row *hl_row
1025 = (w->current_matrix && w->current_matrix->rows
1026 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1027 : 0);
1028 if (hl_row && hl_row->mode_line_p)
1029 height -= hl_row->height;
1030 else
1031 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1032 }
1033
1034 /* With a very small font and a mode-line that's taller than
1035 default, we might end up with a negative height. */
1036 return max (0, height);
1037 }
1038
1039 /* Return the window-relative coordinate of the left edge of display
1040 area AREA of window W. AREA < 0 means return the left edge of the
1041 whole window, to the right of the left fringe of W. */
1042
1043 INLINE int
1044 window_box_left_offset (w, area)
1045 struct window *w;
1046 int area;
1047 {
1048 int x;
1049
1050 if (w->pseudo_window_p)
1051 return 0;
1052
1053 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1054
1055 if (area == TEXT_AREA)
1056 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1057 + window_box_width (w, LEFT_MARGIN_AREA));
1058 else if (area == RIGHT_MARGIN_AREA)
1059 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1060 + window_box_width (w, LEFT_MARGIN_AREA)
1061 + window_box_width (w, TEXT_AREA)
1062 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1063 ? 0
1064 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1065 else if (area == LEFT_MARGIN_AREA
1066 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1067 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1068
1069 return x;
1070 }
1071
1072
1073 /* Return the window-relative coordinate of the right edge of display
1074 area AREA of window W. AREA < 0 means return the left edge of the
1075 whole window, to the left of the right fringe of W. */
1076
1077 INLINE int
1078 window_box_right_offset (w, area)
1079 struct window *w;
1080 int area;
1081 {
1082 return window_box_left_offset (w, area) + window_box_width (w, area);
1083 }
1084
1085 /* Return the frame-relative coordinate of the left edge of display
1086 area AREA of window W. AREA < 0 means return the left edge of the
1087 whole window, to the right of the left fringe of W. */
1088
1089 INLINE int
1090 window_box_left (w, area)
1091 struct window *w;
1092 int area;
1093 {
1094 struct frame *f = XFRAME (w->frame);
1095 int x;
1096
1097 if (w->pseudo_window_p)
1098 return FRAME_INTERNAL_BORDER_WIDTH (f);
1099
1100 x = (WINDOW_LEFT_EDGE_X (w)
1101 + window_box_left_offset (w, area));
1102
1103 return x;
1104 }
1105
1106
1107 /* Return the frame-relative coordinate of the right edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the left of the right fringe of W. */
1110
1111 INLINE int
1112 window_box_right (w, area)
1113 struct window *w;
1114 int area;
1115 {
1116 return window_box_left (w, area) + window_box_width (w, area);
1117 }
1118
1119 /* Get the bounding box of the display area AREA of window W, without
1120 mode lines, in frame-relative coordinates. AREA < 0 means the
1121 whole window, not including the left and right fringes of
1122 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1123 coordinates of the upper-left corner of the box. Return in
1124 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1125
1126 INLINE void
1127 window_box (w, area, box_x, box_y, box_width, box_height)
1128 struct window *w;
1129 int area;
1130 int *box_x, *box_y, *box_width, *box_height;
1131 {
1132 if (box_width)
1133 *box_width = window_box_width (w, area);
1134 if (box_height)
1135 *box_height = window_box_height (w);
1136 if (box_x)
1137 *box_x = window_box_left (w, area);
1138 if (box_y)
1139 {
1140 *box_y = WINDOW_TOP_EDGE_Y (w);
1141 if (WINDOW_WANTS_HEADER_LINE_P (w))
1142 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1143 }
1144 }
1145
1146
1147 /* Get the bounding box of the display area AREA of window W, without
1148 mode lines. AREA < 0 means the whole window, not including the
1149 left and right fringe of the window. Return in *TOP_LEFT_X
1150 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1151 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1152 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1153 box. */
1154
1155 INLINE void
1156 window_box_edges (w, area, top_left_x, top_left_y,
1157 bottom_right_x, bottom_right_y)
1158 struct window *w;
1159 int area;
1160 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1161 {
1162 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1163 bottom_right_y);
1164 *bottom_right_x += *top_left_x;
1165 *bottom_right_y += *top_left_y;
1166 }
1167
1168
1169 \f
1170 /***********************************************************************
1171 Utilities
1172 ***********************************************************************/
1173
1174 /* Return the bottom y-position of the line the iterator IT is in.
1175 This can modify IT's settings. */
1176
1177 int
1178 line_bottom_y (it)
1179 struct it *it;
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213
1214 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1215 1 if POS is visible and the line containing POS is fully visible.
1216 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1217 and header-lines heights. */
1218
1219 int
1220 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1221 struct window *w;
1222 int charpos, *fully, exact_mode_line_heights_p;
1223 {
1224 struct it it;
1225 struct text_pos top;
1226 int visible_p;
1227 struct buffer *old_buffer = NULL;
1228
1229 if (XBUFFER (w->buffer) != current_buffer)
1230 {
1231 old_buffer = current_buffer;
1232 set_buffer_internal_1 (XBUFFER (w->buffer));
1233 }
1234
1235 *fully = visible_p = 0;
1236 SET_TEXT_POS_FROM_MARKER (top, w->start);
1237
1238 /* Compute exact mode line heights, if requested. */
1239 if (exact_mode_line_heights_p)
1240 {
1241 if (WINDOW_WANTS_MODELINE_P (w))
1242 current_mode_line_height
1243 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1244 current_buffer->mode_line_format);
1245
1246 if (WINDOW_WANTS_HEADER_LINE_P (w))
1247 current_header_line_height
1248 = display_mode_line (w, HEADER_LINE_FACE_ID,
1249 current_buffer->header_line_format);
1250 }
1251
1252 start_display (&it, w, top);
1253 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1254 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1255
1256 /* Note that we may overshoot because of invisible text. */
1257 if (IT_CHARPOS (it) >= charpos)
1258 {
1259 int top_y = it.current_y;
1260 int bottom_y = line_bottom_y (&it);
1261 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1262
1263 if (top_y < window_top_y)
1264 visible_p = bottom_y > window_top_y;
1265 else if (top_y < it.last_visible_y)
1266 {
1267 visible_p = 1;
1268 *fully = bottom_y <= it.last_visible_y;
1269 }
1270 }
1271 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1272 {
1273 move_it_by_lines (&it, 1, 0);
1274 if (charpos < IT_CHARPOS (it))
1275 {
1276 visible_p = 1;
1277 *fully = 0;
1278 }
1279 }
1280
1281 if (old_buffer)
1282 set_buffer_internal_1 (old_buffer);
1283
1284 current_header_line_height = current_mode_line_height = -1;
1285 return visible_p;
1286 }
1287
1288
1289 /* Return the next character from STR which is MAXLEN bytes long.
1290 Return in *LEN the length of the character. This is like
1291 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1292 we find one, we return a `?', but with the length of the invalid
1293 character. */
1294
1295 static INLINE int
1296 string_char_and_length (str, maxlen, len)
1297 const unsigned char *str;
1298 int maxlen, *len;
1299 {
1300 int c;
1301
1302 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1303 if (!CHAR_VALID_P (c, 1))
1304 /* We may not change the length here because other places in Emacs
1305 don't use this function, i.e. they silently accept invalid
1306 characters. */
1307 c = '?';
1308
1309 return c;
1310 }
1311
1312
1313
1314 /* Given a position POS containing a valid character and byte position
1315 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1316
1317 static struct text_pos
1318 string_pos_nchars_ahead (pos, string, nchars)
1319 struct text_pos pos;
1320 Lisp_Object string;
1321 int nchars;
1322 {
1323 xassert (STRINGP (string) && nchars >= 0);
1324
1325 if (STRING_MULTIBYTE (string))
1326 {
1327 int rest = SBYTES (string) - BYTEPOS (pos);
1328 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1329 int len;
1330
1331 while (nchars--)
1332 {
1333 string_char_and_length (p, rest, &len);
1334 p += len, rest -= len;
1335 xassert (rest >= 0);
1336 CHARPOS (pos) += 1;
1337 BYTEPOS (pos) += len;
1338 }
1339 }
1340 else
1341 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1342
1343 return pos;
1344 }
1345
1346
1347 /* Value is the text position, i.e. character and byte position,
1348 for character position CHARPOS in STRING. */
1349
1350 static INLINE struct text_pos
1351 string_pos (charpos, string)
1352 int charpos;
1353 Lisp_Object string;
1354 {
1355 struct text_pos pos;
1356 xassert (STRINGP (string));
1357 xassert (charpos >= 0);
1358 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1359 return pos;
1360 }
1361
1362
1363 /* Value is a text position, i.e. character and byte position, for
1364 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1365 means recognize multibyte characters. */
1366
1367 static struct text_pos
1368 c_string_pos (charpos, s, multibyte_p)
1369 int charpos;
1370 unsigned char *s;
1371 int multibyte_p;
1372 {
1373 struct text_pos pos;
1374
1375 xassert (s != NULL);
1376 xassert (charpos >= 0);
1377
1378 if (multibyte_p)
1379 {
1380 int rest = strlen (s), len;
1381
1382 SET_TEXT_POS (pos, 0, 0);
1383 while (charpos--)
1384 {
1385 string_char_and_length (s, rest, &len);
1386 s += len, rest -= len;
1387 xassert (rest >= 0);
1388 CHARPOS (pos) += 1;
1389 BYTEPOS (pos) += len;
1390 }
1391 }
1392 else
1393 SET_TEXT_POS (pos, charpos, charpos);
1394
1395 return pos;
1396 }
1397
1398
1399 /* Value is the number of characters in C string S. MULTIBYTE_P
1400 non-zero means recognize multibyte characters. */
1401
1402 static int
1403 number_of_chars (s, multibyte_p)
1404 unsigned char *s;
1405 int multibyte_p;
1406 {
1407 int nchars;
1408
1409 if (multibyte_p)
1410 {
1411 int rest = strlen (s), len;
1412 unsigned char *p = (unsigned char *) s;
1413
1414 for (nchars = 0; rest > 0; ++nchars)
1415 {
1416 string_char_and_length (p, rest, &len);
1417 rest -= len, p += len;
1418 }
1419 }
1420 else
1421 nchars = strlen (s);
1422
1423 return nchars;
1424 }
1425
1426
1427 /* Compute byte position NEWPOS->bytepos corresponding to
1428 NEWPOS->charpos. POS is a known position in string STRING.
1429 NEWPOS->charpos must be >= POS.charpos. */
1430
1431 static void
1432 compute_string_pos (newpos, pos, string)
1433 struct text_pos *newpos, pos;
1434 Lisp_Object string;
1435 {
1436 xassert (STRINGP (string));
1437 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1438
1439 if (STRING_MULTIBYTE (string))
1440 *newpos = string_pos_nchars_ahead (pos, string,
1441 CHARPOS (*newpos) - CHARPOS (pos));
1442 else
1443 BYTEPOS (*newpos) = CHARPOS (*newpos);
1444 }
1445
1446 /* EXPORT:
1447 Return an estimation of the pixel height of mode or top lines on
1448 frame F. FACE_ID specifies what line's height to estimate. */
1449
1450 int
1451 estimate_mode_line_height (f, face_id)
1452 struct frame *f;
1453 enum face_id face_id;
1454 {
1455 #ifdef HAVE_WINDOW_SYSTEM
1456 if (FRAME_WINDOW_P (f))
1457 {
1458 int height = FONT_HEIGHT (FRAME_FONT (f));
1459
1460 /* This function is called so early when Emacs starts that the face
1461 cache and mode line face are not yet initialized. */
1462 if (FRAME_FACE_CACHE (f))
1463 {
1464 struct face *face = FACE_FROM_ID (f, face_id);
1465 if (face)
1466 {
1467 if (face->font)
1468 height = FONT_HEIGHT (face->font);
1469 if (face->box_line_width > 0)
1470 height += 2 * face->box_line_width;
1471 }
1472 }
1473
1474 return height;
1475 }
1476 #endif
1477
1478 return 1;
1479 }
1480
1481 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1482 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1483 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1484 not force the value into range. */
1485
1486 void
1487 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1488 FRAME_PTR f;
1489 register int pix_x, pix_y;
1490 int *x, *y;
1491 NativeRectangle *bounds;
1492 int noclip;
1493 {
1494
1495 #ifdef HAVE_WINDOW_SYSTEM
1496 if (FRAME_WINDOW_P (f))
1497 {
1498 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1499 even for negative values. */
1500 if (pix_x < 0)
1501 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1502 if (pix_y < 0)
1503 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1504
1505 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1506 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1507
1508 if (bounds)
1509 STORE_NATIVE_RECT (*bounds,
1510 FRAME_COL_TO_PIXEL_X (f, pix_x),
1511 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1512 FRAME_COLUMN_WIDTH (f) - 1,
1513 FRAME_LINE_HEIGHT (f) - 1);
1514
1515 if (!noclip)
1516 {
1517 if (pix_x < 0)
1518 pix_x = 0;
1519 else if (pix_x > FRAME_TOTAL_COLS (f))
1520 pix_x = FRAME_TOTAL_COLS (f);
1521
1522 if (pix_y < 0)
1523 pix_y = 0;
1524 else if (pix_y > FRAME_LINES (f))
1525 pix_y = FRAME_LINES (f);
1526 }
1527 }
1528 #endif
1529
1530 *x = pix_x;
1531 *y = pix_y;
1532 }
1533
1534
1535 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1536 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1537 can't tell the positions because W's display is not up to date,
1538 return 0. */
1539
1540 int
1541 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1542 struct window *w;
1543 int hpos, vpos;
1544 int *frame_x, *frame_y;
1545 {
1546 #ifdef HAVE_WINDOW_SYSTEM
1547 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1548 {
1549 int success_p;
1550
1551 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1552 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1553
1554 if (display_completed)
1555 {
1556 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1557 struct glyph *glyph = row->glyphs[TEXT_AREA];
1558 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1559
1560 hpos = row->x;
1561 vpos = row->y;
1562 while (glyph < end)
1563 {
1564 hpos += glyph->pixel_width;
1565 ++glyph;
1566 }
1567
1568 /* If first glyph is partially visible, its first visible position is still 0. */
1569 if (hpos < 0)
1570 hpos = 0;
1571
1572 success_p = 1;
1573 }
1574 else
1575 {
1576 hpos = vpos = 0;
1577 success_p = 0;
1578 }
1579
1580 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1581 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1582 return success_p;
1583 }
1584 #endif
1585
1586 *frame_x = hpos;
1587 *frame_y = vpos;
1588 return 1;
1589 }
1590
1591
1592 #ifdef HAVE_WINDOW_SYSTEM
1593
1594 /* Find the glyph under window-relative coordinates X/Y in window W.
1595 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1596 strings. Return in *HPOS and *VPOS the row and column number of
1597 the glyph found. Return in *AREA the glyph area containing X.
1598 Value is a pointer to the glyph found or null if X/Y is not on
1599 text, or we can't tell because W's current matrix is not up to
1600 date. */
1601
1602 static struct glyph *
1603 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1604 struct window *w;
1605 int x, y;
1606 int *hpos, *vpos, *dx, *dy, *area;
1607 {
1608 struct glyph *glyph, *end;
1609 struct glyph_row *row = NULL;
1610 int x0, i;
1611
1612 /* Find row containing Y. Give up if some row is not enabled. */
1613 for (i = 0; i < w->current_matrix->nrows; ++i)
1614 {
1615 row = MATRIX_ROW (w->current_matrix, i);
1616 if (!row->enabled_p)
1617 return NULL;
1618 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1619 break;
1620 }
1621
1622 *vpos = i;
1623 *hpos = 0;
1624
1625 /* Give up if Y is not in the window. */
1626 if (i == w->current_matrix->nrows)
1627 return NULL;
1628
1629 /* Get the glyph area containing X. */
1630 if (w->pseudo_window_p)
1631 {
1632 *area = TEXT_AREA;
1633 x0 = 0;
1634 }
1635 else
1636 {
1637 if (x < window_box_left_offset (w, TEXT_AREA))
1638 {
1639 *area = LEFT_MARGIN_AREA;
1640 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1641 }
1642 else if (x < window_box_right_offset (w, TEXT_AREA))
1643 {
1644 *area = TEXT_AREA;
1645 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1646 }
1647 else
1648 {
1649 *area = RIGHT_MARGIN_AREA;
1650 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1651 }
1652 }
1653
1654 /* Find glyph containing X. */
1655 glyph = row->glyphs[*area];
1656 end = glyph + row->used[*area];
1657 x -= x0;
1658 while (glyph < end && x >= glyph->pixel_width)
1659 {
1660 x -= glyph->pixel_width;
1661 ++glyph;
1662 }
1663
1664 if (glyph == end)
1665 return NULL;
1666
1667 if (dx)
1668 {
1669 *dx = x;
1670 *dy = y - (row->y + row->ascent - glyph->ascent);
1671 }
1672
1673 *hpos = glyph - row->glyphs[*area];
1674 return glyph;
1675 }
1676
1677
1678 /* EXPORT:
1679 Convert frame-relative x/y to coordinates relative to window W.
1680 Takes pseudo-windows into account. */
1681
1682 void
1683 frame_to_window_pixel_xy (w, x, y)
1684 struct window *w;
1685 int *x, *y;
1686 {
1687 if (w->pseudo_window_p)
1688 {
1689 /* A pseudo-window is always full-width, and starts at the
1690 left edge of the frame, plus a frame border. */
1691 struct frame *f = XFRAME (w->frame);
1692 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1693 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1694 }
1695 else
1696 {
1697 *x -= WINDOW_LEFT_EDGE_X (w);
1698 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1699 }
1700 }
1701
1702 /* EXPORT:
1703 Return in *R the clipping rectangle for glyph string S. */
1704
1705 void
1706 get_glyph_string_clip_rect (s, nr)
1707 struct glyph_string *s;
1708 NativeRectangle *nr;
1709 {
1710 XRectangle r;
1711
1712 if (s->row->full_width_p)
1713 {
1714 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1715 r.x = WINDOW_LEFT_EDGE_X (s->w);
1716 r.width = WINDOW_TOTAL_WIDTH (s->w);
1717
1718 /* Unless displaying a mode or menu bar line, which are always
1719 fully visible, clip to the visible part of the row. */
1720 if (s->w->pseudo_window_p)
1721 r.height = s->row->visible_height;
1722 else
1723 r.height = s->height;
1724 }
1725 else
1726 {
1727 /* This is a text line that may be partially visible. */
1728 r.x = window_box_left (s->w, s->area);
1729 r.width = window_box_width (s->w, s->area);
1730 r.height = s->row->visible_height;
1731 }
1732
1733 /* If S draws overlapping rows, it's sufficient to use the top and
1734 bottom of the window for clipping because this glyph string
1735 intentionally draws over other lines. */
1736 if (s->for_overlaps_p)
1737 {
1738 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1739 r.height = window_text_bottom_y (s->w) - r.y;
1740 }
1741 else
1742 {
1743 /* Don't use S->y for clipping because it doesn't take partially
1744 visible lines into account. For example, it can be negative for
1745 partially visible lines at the top of a window. */
1746 if (!s->row->full_width_p
1747 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1748 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1749 else
1750 r.y = max (0, s->row->y);
1751
1752 /* If drawing a tool-bar window, draw it over the internal border
1753 at the top of the window. */
1754 if (s->w == XWINDOW (s->f->tool_bar_window))
1755 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1756 }
1757
1758 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1759
1760 /* If drawing the cursor, don't let glyph draw outside its
1761 advertised boundaries. Cleartype does this under some circumstances. */
1762 if (s->hl == DRAW_CURSOR)
1763 {
1764 struct glyph *glyph = s->first_glyph;
1765 int height;
1766
1767 if (s->x > r.x)
1768 {
1769 r.width -= s->x - r.x;
1770 r.x = s->x;
1771 }
1772 r.width = min (r.width, glyph->pixel_width);
1773
1774 /* Don't draw cursor glyph taller than our actual glyph. */
1775 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1776 if (height < r.height)
1777 {
1778 r.y = s->ybase + glyph->descent - height;
1779 r.height = height;
1780 }
1781 }
1782
1783 #ifdef CONVERT_FROM_XRECT
1784 CONVERT_FROM_XRECT (r, *nr);
1785 #else
1786 *nr = r;
1787 #endif
1788 }
1789
1790 #endif /* HAVE_WINDOW_SYSTEM */
1791
1792 \f
1793 /***********************************************************************
1794 Lisp form evaluation
1795 ***********************************************************************/
1796
1797 /* Error handler for safe_eval and safe_call. */
1798
1799 static Lisp_Object
1800 safe_eval_handler (arg)
1801 Lisp_Object arg;
1802 {
1803 add_to_log ("Error during redisplay: %s", arg, Qnil);
1804 return Qnil;
1805 }
1806
1807
1808 /* Evaluate SEXPR and return the result, or nil if something went
1809 wrong. Prevent redisplay during the evaluation. */
1810
1811 Lisp_Object
1812 safe_eval (sexpr)
1813 Lisp_Object sexpr;
1814 {
1815 Lisp_Object val;
1816
1817 if (inhibit_eval_during_redisplay)
1818 val = Qnil;
1819 else
1820 {
1821 int count = SPECPDL_INDEX ();
1822 struct gcpro gcpro1;
1823
1824 GCPRO1 (sexpr);
1825 specbind (Qinhibit_redisplay, Qt);
1826 /* Use Qt to ensure debugger does not run,
1827 so there is no possibility of wanting to redisplay. */
1828 val = internal_condition_case_1 (Feval, sexpr, Qt,
1829 safe_eval_handler);
1830 UNGCPRO;
1831 val = unbind_to (count, val);
1832 }
1833
1834 return val;
1835 }
1836
1837
1838 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1839 Return the result, or nil if something went wrong. Prevent
1840 redisplay during the evaluation. */
1841
1842 Lisp_Object
1843 safe_call (nargs, args)
1844 int nargs;
1845 Lisp_Object *args;
1846 {
1847 Lisp_Object val;
1848
1849 if (inhibit_eval_during_redisplay)
1850 val = Qnil;
1851 else
1852 {
1853 int count = SPECPDL_INDEX ();
1854 struct gcpro gcpro1;
1855
1856 GCPRO1 (args[0]);
1857 gcpro1.nvars = nargs;
1858 specbind (Qinhibit_redisplay, Qt);
1859 /* Use Qt to ensure debugger does not run,
1860 so there is no possibility of wanting to redisplay. */
1861 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1862 safe_eval_handler);
1863 UNGCPRO;
1864 val = unbind_to (count, val);
1865 }
1866
1867 return val;
1868 }
1869
1870
1871 /* Call function FN with one argument ARG.
1872 Return the result, or nil if something went wrong. */
1873
1874 Lisp_Object
1875 safe_call1 (fn, arg)
1876 Lisp_Object fn, arg;
1877 {
1878 Lisp_Object args[2];
1879 args[0] = fn;
1880 args[1] = arg;
1881 return safe_call (2, args);
1882 }
1883
1884
1885 \f
1886 /***********************************************************************
1887 Debugging
1888 ***********************************************************************/
1889
1890 #if 0
1891
1892 /* Define CHECK_IT to perform sanity checks on iterators.
1893 This is for debugging. It is too slow to do unconditionally. */
1894
1895 static void
1896 check_it (it)
1897 struct it *it;
1898 {
1899 if (it->method == next_element_from_string)
1900 {
1901 xassert (STRINGP (it->string));
1902 xassert (IT_STRING_CHARPOS (*it) >= 0);
1903 }
1904 else if (it->method == next_element_from_buffer)
1905 {
1906 /* Check that character and byte positions agree. */
1907 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1908 }
1909
1910 if (it->dpvec)
1911 xassert (it->current.dpvec_index >= 0);
1912 else
1913 xassert (it->current.dpvec_index < 0);
1914 }
1915
1916 #define CHECK_IT(IT) check_it ((IT))
1917
1918 #else /* not 0 */
1919
1920 #define CHECK_IT(IT) (void) 0
1921
1922 #endif /* not 0 */
1923
1924
1925 #if GLYPH_DEBUG
1926
1927 /* Check that the window end of window W is what we expect it
1928 to be---the last row in the current matrix displaying text. */
1929
1930 static void
1931 check_window_end (w)
1932 struct window *w;
1933 {
1934 if (!MINI_WINDOW_P (w)
1935 && !NILP (w->window_end_valid))
1936 {
1937 struct glyph_row *row;
1938 xassert ((row = MATRIX_ROW (w->current_matrix,
1939 XFASTINT (w->window_end_vpos)),
1940 !row->enabled_p
1941 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1942 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1943 }
1944 }
1945
1946 #define CHECK_WINDOW_END(W) check_window_end ((W))
1947
1948 #else /* not GLYPH_DEBUG */
1949
1950 #define CHECK_WINDOW_END(W) (void) 0
1951
1952 #endif /* not GLYPH_DEBUG */
1953
1954
1955 \f
1956 /***********************************************************************
1957 Iterator initialization
1958 ***********************************************************************/
1959
1960 /* Initialize IT for displaying current_buffer in window W, starting
1961 at character position CHARPOS. CHARPOS < 0 means that no buffer
1962 position is specified which is useful when the iterator is assigned
1963 a position later. BYTEPOS is the byte position corresponding to
1964 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1965
1966 If ROW is not null, calls to produce_glyphs with IT as parameter
1967 will produce glyphs in that row.
1968
1969 BASE_FACE_ID is the id of a base face to use. It must be one of
1970 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1971 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1972 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1973
1974 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1975 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1976 will be initialized to use the corresponding mode line glyph row of
1977 the desired matrix of W. */
1978
1979 void
1980 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1981 struct it *it;
1982 struct window *w;
1983 int charpos, bytepos;
1984 struct glyph_row *row;
1985 enum face_id base_face_id;
1986 {
1987 int highlight_region_p;
1988
1989 /* Some precondition checks. */
1990 xassert (w != NULL && it != NULL);
1991 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1992 && charpos <= ZV));
1993
1994 /* If face attributes have been changed since the last redisplay,
1995 free realized faces now because they depend on face definitions
1996 that might have changed. Don't free faces while there might be
1997 desired matrices pending which reference these faces. */
1998 if (face_change_count && !inhibit_free_realized_faces)
1999 {
2000 face_change_count = 0;
2001 free_all_realized_faces (Qnil);
2002 }
2003
2004 /* Use one of the mode line rows of W's desired matrix if
2005 appropriate. */
2006 if (row == NULL)
2007 {
2008 if (base_face_id == MODE_LINE_FACE_ID
2009 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2010 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2011 else if (base_face_id == HEADER_LINE_FACE_ID)
2012 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2013 }
2014
2015 /* Clear IT. */
2016 bzero (it, sizeof *it);
2017 it->current.overlay_string_index = -1;
2018 it->current.dpvec_index = -1;
2019 it->base_face_id = base_face_id;
2020
2021 /* The window in which we iterate over current_buffer: */
2022 XSETWINDOW (it->window, w);
2023 it->w = w;
2024 it->f = XFRAME (w->frame);
2025
2026 /* Extra space between lines (on window systems only). */
2027 if (base_face_id == DEFAULT_FACE_ID
2028 && FRAME_WINDOW_P (it->f))
2029 {
2030 if (NATNUMP (current_buffer->extra_line_spacing))
2031 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2032 else if (it->f->extra_line_spacing > 0)
2033 it->extra_line_spacing = it->f->extra_line_spacing;
2034 }
2035
2036 /* If realized faces have been removed, e.g. because of face
2037 attribute changes of named faces, recompute them. When running
2038 in batch mode, the face cache of the initial frame is null. If
2039 we happen to get called, make a dummy face cache. */
2040 if (FRAME_FACE_CACHE (it->f) == NULL)
2041 init_frame_faces (it->f);
2042 if (FRAME_FACE_CACHE (it->f)->used == 0)
2043 recompute_basic_faces (it->f);
2044
2045 /* Current value of the `space-width', and 'height' properties. */
2046 it->space_width = Qnil;
2047 it->font_height = Qnil;
2048
2049 /* Are control characters displayed as `^C'? */
2050 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2051
2052 /* -1 means everything between a CR and the following line end
2053 is invisible. >0 means lines indented more than this value are
2054 invisible. */
2055 it->selective = (INTEGERP (current_buffer->selective_display)
2056 ? XFASTINT (current_buffer->selective_display)
2057 : (!NILP (current_buffer->selective_display)
2058 ? -1 : 0));
2059 it->selective_display_ellipsis_p
2060 = !NILP (current_buffer->selective_display_ellipses);
2061
2062 /* Display table to use. */
2063 it->dp = window_display_table (w);
2064
2065 /* Are multibyte characters enabled in current_buffer? */
2066 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2067
2068 /* Non-zero if we should highlight the region. */
2069 highlight_region_p
2070 = (!NILP (Vtransient_mark_mode)
2071 && !NILP (current_buffer->mark_active)
2072 && XMARKER (current_buffer->mark)->buffer != 0);
2073
2074 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2075 start and end of a visible region in window IT->w. Set both to
2076 -1 to indicate no region. */
2077 if (highlight_region_p
2078 /* Maybe highlight only in selected window. */
2079 && (/* Either show region everywhere. */
2080 highlight_nonselected_windows
2081 /* Or show region in the selected window. */
2082 || w == XWINDOW (selected_window)
2083 /* Or show the region if we are in the mini-buffer and W is
2084 the window the mini-buffer refers to. */
2085 || (MINI_WINDOW_P (XWINDOW (selected_window))
2086 && WINDOWP (minibuf_selected_window)
2087 && w == XWINDOW (minibuf_selected_window))))
2088 {
2089 int charpos = marker_position (current_buffer->mark);
2090 it->region_beg_charpos = min (PT, charpos);
2091 it->region_end_charpos = max (PT, charpos);
2092 }
2093 else
2094 it->region_beg_charpos = it->region_end_charpos = -1;
2095
2096 /* Get the position at which the redisplay_end_trigger hook should
2097 be run, if it is to be run at all. */
2098 if (MARKERP (w->redisplay_end_trigger)
2099 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2100 it->redisplay_end_trigger_charpos
2101 = marker_position (w->redisplay_end_trigger);
2102 else if (INTEGERP (w->redisplay_end_trigger))
2103 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2104
2105 /* Correct bogus values of tab_width. */
2106 it->tab_width = XINT (current_buffer->tab_width);
2107 if (it->tab_width <= 0 || it->tab_width > 1000)
2108 it->tab_width = 8;
2109
2110 /* Are lines in the display truncated? */
2111 it->truncate_lines_p
2112 = (base_face_id != DEFAULT_FACE_ID
2113 || XINT (it->w->hscroll)
2114 || (truncate_partial_width_windows
2115 && !WINDOW_FULL_WIDTH_P (it->w))
2116 || !NILP (current_buffer->truncate_lines));
2117
2118 /* Get dimensions of truncation and continuation glyphs. These are
2119 displayed as fringe bitmaps under X, so we don't need them for such
2120 frames. */
2121 if (!FRAME_WINDOW_P (it->f))
2122 {
2123 if (it->truncate_lines_p)
2124 {
2125 /* We will need the truncation glyph. */
2126 xassert (it->glyph_row == NULL);
2127 produce_special_glyphs (it, IT_TRUNCATION);
2128 it->truncation_pixel_width = it->pixel_width;
2129 }
2130 else
2131 {
2132 /* We will need the continuation glyph. */
2133 xassert (it->glyph_row == NULL);
2134 produce_special_glyphs (it, IT_CONTINUATION);
2135 it->continuation_pixel_width = it->pixel_width;
2136 }
2137
2138 /* Reset these values to zero because the produce_special_glyphs
2139 above has changed them. */
2140 it->pixel_width = it->ascent = it->descent = 0;
2141 it->phys_ascent = it->phys_descent = 0;
2142 }
2143
2144 /* Set this after getting the dimensions of truncation and
2145 continuation glyphs, so that we don't produce glyphs when calling
2146 produce_special_glyphs, above. */
2147 it->glyph_row = row;
2148 it->area = TEXT_AREA;
2149
2150 /* Get the dimensions of the display area. The display area
2151 consists of the visible window area plus a horizontally scrolled
2152 part to the left of the window. All x-values are relative to the
2153 start of this total display area. */
2154 if (base_face_id != DEFAULT_FACE_ID)
2155 {
2156 /* Mode lines, menu bar in terminal frames. */
2157 it->first_visible_x = 0;
2158 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2159 }
2160 else
2161 {
2162 it->first_visible_x
2163 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2164 it->last_visible_x = (it->first_visible_x
2165 + window_box_width (w, TEXT_AREA));
2166
2167 /* If we truncate lines, leave room for the truncator glyph(s) at
2168 the right margin. Otherwise, leave room for the continuation
2169 glyph(s). Truncation and continuation glyphs are not inserted
2170 for window-based redisplay. */
2171 if (!FRAME_WINDOW_P (it->f))
2172 {
2173 if (it->truncate_lines_p)
2174 it->last_visible_x -= it->truncation_pixel_width;
2175 else
2176 it->last_visible_x -= it->continuation_pixel_width;
2177 }
2178
2179 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2180 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2181 }
2182
2183 /* Leave room for a border glyph. */
2184 if (!FRAME_WINDOW_P (it->f)
2185 && !WINDOW_RIGHTMOST_P (it->w))
2186 it->last_visible_x -= 1;
2187
2188 it->last_visible_y = window_text_bottom_y (w);
2189
2190 /* For mode lines and alike, arrange for the first glyph having a
2191 left box line if the face specifies a box. */
2192 if (base_face_id != DEFAULT_FACE_ID)
2193 {
2194 struct face *face;
2195
2196 it->face_id = base_face_id;
2197
2198 /* If we have a boxed mode line, make the first character appear
2199 with a left box line. */
2200 face = FACE_FROM_ID (it->f, base_face_id);
2201 if (face->box != FACE_NO_BOX)
2202 it->start_of_box_run_p = 1;
2203 }
2204
2205 /* If a buffer position was specified, set the iterator there,
2206 getting overlays and face properties from that position. */
2207 if (charpos >= BUF_BEG (current_buffer))
2208 {
2209 it->end_charpos = ZV;
2210 it->face_id = -1;
2211 IT_CHARPOS (*it) = charpos;
2212
2213 /* Compute byte position if not specified. */
2214 if (bytepos < charpos)
2215 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2216 else
2217 IT_BYTEPOS (*it) = bytepos;
2218
2219 it->start = it->current;
2220
2221 /* Compute faces etc. */
2222 reseat (it, it->current.pos, 1);
2223 }
2224
2225 CHECK_IT (it);
2226 }
2227
2228
2229 /* Initialize IT for the display of window W with window start POS. */
2230
2231 void
2232 start_display (it, w, pos)
2233 struct it *it;
2234 struct window *w;
2235 struct text_pos pos;
2236 {
2237 struct glyph_row *row;
2238 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2239
2240 row = w->desired_matrix->rows + first_vpos;
2241 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2242 it->first_vpos = first_vpos;
2243
2244 if (!it->truncate_lines_p)
2245 {
2246 int start_at_line_beg_p;
2247 int first_y = it->current_y;
2248
2249 /* If window start is not at a line start, skip forward to POS to
2250 get the correct continuation lines width. */
2251 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2252 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2253 if (!start_at_line_beg_p)
2254 {
2255 int new_x;
2256
2257 reseat_at_previous_visible_line_start (it);
2258 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2259
2260 new_x = it->current_x + it->pixel_width;
2261
2262 /* If lines are continued, this line may end in the middle
2263 of a multi-glyph character (e.g. a control character
2264 displayed as \003, or in the middle of an overlay
2265 string). In this case move_it_to above will not have
2266 taken us to the start of the continuation line but to the
2267 end of the continued line. */
2268 if (it->current_x > 0
2269 && !it->truncate_lines_p /* Lines are continued. */
2270 && (/* And glyph doesn't fit on the line. */
2271 new_x > it->last_visible_x
2272 /* Or it fits exactly and we're on a window
2273 system frame. */
2274 || (new_x == it->last_visible_x
2275 && FRAME_WINDOW_P (it->f))))
2276 {
2277 if (it->current.dpvec_index >= 0
2278 || it->current.overlay_string_index >= 0)
2279 {
2280 set_iterator_to_next (it, 1);
2281 move_it_in_display_line_to (it, -1, -1, 0);
2282 }
2283
2284 it->continuation_lines_width += it->current_x;
2285 }
2286
2287 /* We're starting a new display line, not affected by the
2288 height of the continued line, so clear the appropriate
2289 fields in the iterator structure. */
2290 it->max_ascent = it->max_descent = 0;
2291 it->max_phys_ascent = it->max_phys_descent = 0;
2292
2293 it->current_y = first_y;
2294 it->vpos = 0;
2295 it->current_x = it->hpos = 0;
2296 }
2297 }
2298
2299 #if 0 /* Don't assert the following because start_display is sometimes
2300 called intentionally with a window start that is not at a
2301 line start. Please leave this code in as a comment. */
2302
2303 /* Window start should be on a line start, now. */
2304 xassert (it->continuation_lines_width
2305 || IT_CHARPOS (it) == BEGV
2306 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2307 #endif /* 0 */
2308 }
2309
2310
2311 /* Return 1 if POS is a position in ellipses displayed for invisible
2312 text. W is the window we display, for text property lookup. */
2313
2314 static int
2315 in_ellipses_for_invisible_text_p (pos, w)
2316 struct display_pos *pos;
2317 struct window *w;
2318 {
2319 Lisp_Object prop, window;
2320 int ellipses_p = 0;
2321 int charpos = CHARPOS (pos->pos);
2322
2323 /* If POS specifies a position in a display vector, this might
2324 be for an ellipsis displayed for invisible text. We won't
2325 get the iterator set up for delivering that ellipsis unless
2326 we make sure that it gets aware of the invisible text. */
2327 if (pos->dpvec_index >= 0
2328 && pos->overlay_string_index < 0
2329 && CHARPOS (pos->string_pos) < 0
2330 && charpos > BEGV
2331 && (XSETWINDOW (window, w),
2332 prop = Fget_char_property (make_number (charpos),
2333 Qinvisible, window),
2334 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2335 {
2336 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2337 window);
2338 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2339 }
2340
2341 return ellipses_p;
2342 }
2343
2344
2345 /* Initialize IT for stepping through current_buffer in window W,
2346 starting at position POS that includes overlay string and display
2347 vector/ control character translation position information. Value
2348 is zero if there are overlay strings with newlines at POS. */
2349
2350 static int
2351 init_from_display_pos (it, w, pos)
2352 struct it *it;
2353 struct window *w;
2354 struct display_pos *pos;
2355 {
2356 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2357 int i, overlay_strings_with_newlines = 0;
2358
2359 /* If POS specifies a position in a display vector, this might
2360 be for an ellipsis displayed for invisible text. We won't
2361 get the iterator set up for delivering that ellipsis unless
2362 we make sure that it gets aware of the invisible text. */
2363 if (in_ellipses_for_invisible_text_p (pos, w))
2364 {
2365 --charpos;
2366 bytepos = 0;
2367 }
2368
2369 /* Keep in mind: the call to reseat in init_iterator skips invisible
2370 text, so we might end up at a position different from POS. This
2371 is only a problem when POS is a row start after a newline and an
2372 overlay starts there with an after-string, and the overlay has an
2373 invisible property. Since we don't skip invisible text in
2374 display_line and elsewhere immediately after consuming the
2375 newline before the row start, such a POS will not be in a string,
2376 but the call to init_iterator below will move us to the
2377 after-string. */
2378 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2379
2380 for (i = 0; i < it->n_overlay_strings; ++i)
2381 {
2382 const char *s = SDATA (it->overlay_strings[i]);
2383 const char *e = s + SBYTES (it->overlay_strings[i]);
2384
2385 while (s < e && *s != '\n')
2386 ++s;
2387
2388 if (s < e)
2389 {
2390 overlay_strings_with_newlines = 1;
2391 break;
2392 }
2393 }
2394
2395 /* If position is within an overlay string, set up IT to the right
2396 overlay string. */
2397 if (pos->overlay_string_index >= 0)
2398 {
2399 int relative_index;
2400
2401 /* If the first overlay string happens to have a `display'
2402 property for an image, the iterator will be set up for that
2403 image, and we have to undo that setup first before we can
2404 correct the overlay string index. */
2405 if (it->method == next_element_from_image)
2406 pop_it (it);
2407
2408 /* We already have the first chunk of overlay strings in
2409 IT->overlay_strings. Load more until the one for
2410 pos->overlay_string_index is in IT->overlay_strings. */
2411 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2412 {
2413 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2414 it->current.overlay_string_index = 0;
2415 while (n--)
2416 {
2417 load_overlay_strings (it, 0);
2418 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2419 }
2420 }
2421
2422 it->current.overlay_string_index = pos->overlay_string_index;
2423 relative_index = (it->current.overlay_string_index
2424 % OVERLAY_STRING_CHUNK_SIZE);
2425 it->string = it->overlay_strings[relative_index];
2426 xassert (STRINGP (it->string));
2427 it->current.string_pos = pos->string_pos;
2428 it->method = next_element_from_string;
2429 }
2430
2431 #if 0 /* This is bogus because POS not having an overlay string
2432 position does not mean it's after the string. Example: A
2433 line starting with a before-string and initialization of IT
2434 to the previous row's end position. */
2435 else if (it->current.overlay_string_index >= 0)
2436 {
2437 /* If POS says we're already after an overlay string ending at
2438 POS, make sure to pop the iterator because it will be in
2439 front of that overlay string. When POS is ZV, we've thereby
2440 also ``processed'' overlay strings at ZV. */
2441 while (it->sp)
2442 pop_it (it);
2443 it->current.overlay_string_index = -1;
2444 it->method = next_element_from_buffer;
2445 if (CHARPOS (pos->pos) == ZV)
2446 it->overlay_strings_at_end_processed_p = 1;
2447 }
2448 #endif /* 0 */
2449
2450 if (CHARPOS (pos->string_pos) >= 0)
2451 {
2452 /* Recorded position is not in an overlay string, but in another
2453 string. This can only be a string from a `display' property.
2454 IT should already be filled with that string. */
2455 it->current.string_pos = pos->string_pos;
2456 xassert (STRINGP (it->string));
2457 }
2458
2459 /* Restore position in display vector translations, control
2460 character translations or ellipses. */
2461 if (pos->dpvec_index >= 0)
2462 {
2463 if (it->dpvec == NULL)
2464 get_next_display_element (it);
2465 xassert (it->dpvec && it->current.dpvec_index == 0);
2466 it->current.dpvec_index = pos->dpvec_index;
2467 }
2468
2469 CHECK_IT (it);
2470 return !overlay_strings_with_newlines;
2471 }
2472
2473
2474 /* Initialize IT for stepping through current_buffer in window W
2475 starting at ROW->start. */
2476
2477 static void
2478 init_to_row_start (it, w, row)
2479 struct it *it;
2480 struct window *w;
2481 struct glyph_row *row;
2482 {
2483 init_from_display_pos (it, w, &row->start);
2484 it->start = row->start;
2485 it->continuation_lines_width = row->continuation_lines_width;
2486 CHECK_IT (it);
2487 }
2488
2489
2490 /* Initialize IT for stepping through current_buffer in window W
2491 starting in the line following ROW, i.e. starting at ROW->end.
2492 Value is zero if there are overlay strings with newlines at ROW's
2493 end position. */
2494
2495 static int
2496 init_to_row_end (it, w, row)
2497 struct it *it;
2498 struct window *w;
2499 struct glyph_row *row;
2500 {
2501 int success = 0;
2502
2503 if (init_from_display_pos (it, w, &row->end))
2504 {
2505 if (row->continued_p)
2506 it->continuation_lines_width
2507 = row->continuation_lines_width + row->pixel_width;
2508 CHECK_IT (it);
2509 success = 1;
2510 }
2511
2512 return success;
2513 }
2514
2515
2516
2517 \f
2518 /***********************************************************************
2519 Text properties
2520 ***********************************************************************/
2521
2522 /* Called when IT reaches IT->stop_charpos. Handle text property and
2523 overlay changes. Set IT->stop_charpos to the next position where
2524 to stop. */
2525
2526 static void
2527 handle_stop (it)
2528 struct it *it;
2529 {
2530 enum prop_handled handled;
2531 int handle_overlay_change_p = 1;
2532 struct props *p;
2533
2534 it->dpvec = NULL;
2535 it->current.dpvec_index = -1;
2536
2537 do
2538 {
2539 handled = HANDLED_NORMALLY;
2540
2541 /* Call text property handlers. */
2542 for (p = it_props; p->handler; ++p)
2543 {
2544 handled = p->handler (it);
2545
2546 if (handled == HANDLED_RECOMPUTE_PROPS)
2547 break;
2548 else if (handled == HANDLED_RETURN)
2549 return;
2550 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2551 handle_overlay_change_p = 0;
2552 }
2553
2554 if (handled != HANDLED_RECOMPUTE_PROPS)
2555 {
2556 /* Don't check for overlay strings below when set to deliver
2557 characters from a display vector. */
2558 if (it->method == next_element_from_display_vector)
2559 handle_overlay_change_p = 0;
2560
2561 /* Handle overlay changes. */
2562 if (handle_overlay_change_p)
2563 handled = handle_overlay_change (it);
2564
2565 /* Determine where to stop next. */
2566 if (handled == HANDLED_NORMALLY)
2567 compute_stop_pos (it);
2568 }
2569 }
2570 while (handled == HANDLED_RECOMPUTE_PROPS);
2571 }
2572
2573
2574 /* Compute IT->stop_charpos from text property and overlay change
2575 information for IT's current position. */
2576
2577 static void
2578 compute_stop_pos (it)
2579 struct it *it;
2580 {
2581 register INTERVAL iv, next_iv;
2582 Lisp_Object object, limit, position;
2583
2584 /* If nowhere else, stop at the end. */
2585 it->stop_charpos = it->end_charpos;
2586
2587 if (STRINGP (it->string))
2588 {
2589 /* Strings are usually short, so don't limit the search for
2590 properties. */
2591 object = it->string;
2592 limit = Qnil;
2593 position = make_number (IT_STRING_CHARPOS (*it));
2594 }
2595 else
2596 {
2597 int charpos;
2598
2599 /* If next overlay change is in front of the current stop pos
2600 (which is IT->end_charpos), stop there. Note: value of
2601 next_overlay_change is point-max if no overlay change
2602 follows. */
2603 charpos = next_overlay_change (IT_CHARPOS (*it));
2604 if (charpos < it->stop_charpos)
2605 it->stop_charpos = charpos;
2606
2607 /* If showing the region, we have to stop at the region
2608 start or end because the face might change there. */
2609 if (it->region_beg_charpos > 0)
2610 {
2611 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2612 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2613 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2614 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2615 }
2616
2617 /* Set up variables for computing the stop position from text
2618 property changes. */
2619 XSETBUFFER (object, current_buffer);
2620 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2621 position = make_number (IT_CHARPOS (*it));
2622
2623 }
2624
2625 /* Get the interval containing IT's position. Value is a null
2626 interval if there isn't such an interval. */
2627 iv = validate_interval_range (object, &position, &position, 0);
2628 if (!NULL_INTERVAL_P (iv))
2629 {
2630 Lisp_Object values_here[LAST_PROP_IDX];
2631 struct props *p;
2632
2633 /* Get properties here. */
2634 for (p = it_props; p->handler; ++p)
2635 values_here[p->idx] = textget (iv->plist, *p->name);
2636
2637 /* Look for an interval following iv that has different
2638 properties. */
2639 for (next_iv = next_interval (iv);
2640 (!NULL_INTERVAL_P (next_iv)
2641 && (NILP (limit)
2642 || XFASTINT (limit) > next_iv->position));
2643 next_iv = next_interval (next_iv))
2644 {
2645 for (p = it_props; p->handler; ++p)
2646 {
2647 Lisp_Object new_value;
2648
2649 new_value = textget (next_iv->plist, *p->name);
2650 if (!EQ (values_here[p->idx], new_value))
2651 break;
2652 }
2653
2654 if (p->handler)
2655 break;
2656 }
2657
2658 if (!NULL_INTERVAL_P (next_iv))
2659 {
2660 if (INTEGERP (limit)
2661 && next_iv->position >= XFASTINT (limit))
2662 /* No text property change up to limit. */
2663 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2664 else
2665 /* Text properties change in next_iv. */
2666 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2667 }
2668 }
2669
2670 xassert (STRINGP (it->string)
2671 || (it->stop_charpos >= BEGV
2672 && it->stop_charpos >= IT_CHARPOS (*it)));
2673 }
2674
2675
2676 /* Return the position of the next overlay change after POS in
2677 current_buffer. Value is point-max if no overlay change
2678 follows. This is like `next-overlay-change' but doesn't use
2679 xmalloc. */
2680
2681 static int
2682 next_overlay_change (pos)
2683 int pos;
2684 {
2685 int noverlays;
2686 int endpos;
2687 Lisp_Object *overlays;
2688 int len;
2689 int i;
2690
2691 /* Get all overlays at the given position. */
2692 len = 10;
2693 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2694 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2695 if (noverlays > len)
2696 {
2697 len = noverlays;
2698 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2699 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2700 }
2701
2702 /* If any of these overlays ends before endpos,
2703 use its ending point instead. */
2704 for (i = 0; i < noverlays; ++i)
2705 {
2706 Lisp_Object oend;
2707 int oendpos;
2708
2709 oend = OVERLAY_END (overlays[i]);
2710 oendpos = OVERLAY_POSITION (oend);
2711 endpos = min (endpos, oendpos);
2712 }
2713
2714 return endpos;
2715 }
2716
2717
2718 \f
2719 /***********************************************************************
2720 Fontification
2721 ***********************************************************************/
2722
2723 /* Handle changes in the `fontified' property of the current buffer by
2724 calling hook functions from Qfontification_functions to fontify
2725 regions of text. */
2726
2727 static enum prop_handled
2728 handle_fontified_prop (it)
2729 struct it *it;
2730 {
2731 Lisp_Object prop, pos;
2732 enum prop_handled handled = HANDLED_NORMALLY;
2733
2734 /* Get the value of the `fontified' property at IT's current buffer
2735 position. (The `fontified' property doesn't have a special
2736 meaning in strings.) If the value is nil, call functions from
2737 Qfontification_functions. */
2738 if (!STRINGP (it->string)
2739 && it->s == NULL
2740 && !NILP (Vfontification_functions)
2741 && !NILP (Vrun_hooks)
2742 && (pos = make_number (IT_CHARPOS (*it)),
2743 prop = Fget_char_property (pos, Qfontified, Qnil),
2744 NILP (prop)))
2745 {
2746 int count = SPECPDL_INDEX ();
2747 Lisp_Object val;
2748
2749 val = Vfontification_functions;
2750 specbind (Qfontification_functions, Qnil);
2751
2752 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2753 safe_call1 (val, pos);
2754 else
2755 {
2756 Lisp_Object globals, fn;
2757 struct gcpro gcpro1, gcpro2;
2758
2759 globals = Qnil;
2760 GCPRO2 (val, globals);
2761
2762 for (; CONSP (val); val = XCDR (val))
2763 {
2764 fn = XCAR (val);
2765
2766 if (EQ (fn, Qt))
2767 {
2768 /* A value of t indicates this hook has a local
2769 binding; it means to run the global binding too.
2770 In a global value, t should not occur. If it
2771 does, we must ignore it to avoid an endless
2772 loop. */
2773 for (globals = Fdefault_value (Qfontification_functions);
2774 CONSP (globals);
2775 globals = XCDR (globals))
2776 {
2777 fn = XCAR (globals);
2778 if (!EQ (fn, Qt))
2779 safe_call1 (fn, pos);
2780 }
2781 }
2782 else
2783 safe_call1 (fn, pos);
2784 }
2785
2786 UNGCPRO;
2787 }
2788
2789 unbind_to (count, Qnil);
2790
2791 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2792 something. This avoids an endless loop if they failed to
2793 fontify the text for which reason ever. */
2794 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2795 handled = HANDLED_RECOMPUTE_PROPS;
2796 }
2797
2798 return handled;
2799 }
2800
2801
2802 \f
2803 /***********************************************************************
2804 Faces
2805 ***********************************************************************/
2806
2807 /* Set up iterator IT from face properties at its current position.
2808 Called from handle_stop. */
2809
2810 static enum prop_handled
2811 handle_face_prop (it)
2812 struct it *it;
2813 {
2814 int new_face_id, next_stop;
2815
2816 if (!STRINGP (it->string))
2817 {
2818 new_face_id
2819 = face_at_buffer_position (it->w,
2820 IT_CHARPOS (*it),
2821 it->region_beg_charpos,
2822 it->region_end_charpos,
2823 &next_stop,
2824 (IT_CHARPOS (*it)
2825 + TEXT_PROP_DISTANCE_LIMIT),
2826 0);
2827
2828 /* Is this a start of a run of characters with box face?
2829 Caveat: this can be called for a freshly initialized
2830 iterator; face_id is -1 in this case. We know that the new
2831 face will not change until limit, i.e. if the new face has a
2832 box, all characters up to limit will have one. But, as
2833 usual, we don't know whether limit is really the end. */
2834 if (new_face_id != it->face_id)
2835 {
2836 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2837
2838 /* If new face has a box but old face has not, this is
2839 the start of a run of characters with box, i.e. it has
2840 a shadow on the left side. The value of face_id of the
2841 iterator will be -1 if this is the initial call that gets
2842 the face. In this case, we have to look in front of IT's
2843 position and see whether there is a face != new_face_id. */
2844 it->start_of_box_run_p
2845 = (new_face->box != FACE_NO_BOX
2846 && (it->face_id >= 0
2847 || IT_CHARPOS (*it) == BEG
2848 || new_face_id != face_before_it_pos (it)));
2849 it->face_box_p = new_face->box != FACE_NO_BOX;
2850 }
2851 }
2852 else
2853 {
2854 int base_face_id, bufpos;
2855
2856 if (it->current.overlay_string_index >= 0)
2857 bufpos = IT_CHARPOS (*it);
2858 else
2859 bufpos = 0;
2860
2861 /* For strings from a buffer, i.e. overlay strings or strings
2862 from a `display' property, use the face at IT's current
2863 buffer position as the base face to merge with, so that
2864 overlay strings appear in the same face as surrounding
2865 text, unless they specify their own faces. */
2866 base_face_id = underlying_face_id (it);
2867
2868 new_face_id = face_at_string_position (it->w,
2869 it->string,
2870 IT_STRING_CHARPOS (*it),
2871 bufpos,
2872 it->region_beg_charpos,
2873 it->region_end_charpos,
2874 &next_stop,
2875 base_face_id, 0);
2876
2877 #if 0 /* This shouldn't be neccessary. Let's check it. */
2878 /* If IT is used to display a mode line we would really like to
2879 use the mode line face instead of the frame's default face. */
2880 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2881 && new_face_id == DEFAULT_FACE_ID)
2882 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2883 #endif
2884
2885 /* Is this a start of a run of characters with box? Caveat:
2886 this can be called for a freshly allocated iterator; face_id
2887 is -1 is this case. We know that the new face will not
2888 change until the next check pos, i.e. if the new face has a
2889 box, all characters up to that position will have a
2890 box. But, as usual, we don't know whether that position
2891 is really the end. */
2892 if (new_face_id != it->face_id)
2893 {
2894 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2895 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2896
2897 /* If new face has a box but old face hasn't, this is the
2898 start of a run of characters with box, i.e. it has a
2899 shadow on the left side. */
2900 it->start_of_box_run_p
2901 = new_face->box && (old_face == NULL || !old_face->box);
2902 it->face_box_p = new_face->box != FACE_NO_BOX;
2903 }
2904 }
2905
2906 it->face_id = new_face_id;
2907 return HANDLED_NORMALLY;
2908 }
2909
2910
2911 /* Return the ID of the face ``underlying'' IT's current position,
2912 which is in a string. If the iterator is associated with a
2913 buffer, return the face at IT's current buffer position.
2914 Otherwise, use the iterator's base_face_id. */
2915
2916 static int
2917 underlying_face_id (it)
2918 struct it *it;
2919 {
2920 int face_id = it->base_face_id, i;
2921
2922 xassert (STRINGP (it->string));
2923
2924 for (i = it->sp - 1; i >= 0; --i)
2925 if (NILP (it->stack[i].string))
2926 face_id = it->stack[i].face_id;
2927
2928 return face_id;
2929 }
2930
2931
2932 /* Compute the face one character before or after the current position
2933 of IT. BEFORE_P non-zero means get the face in front of IT's
2934 position. Value is the id of the face. */
2935
2936 static int
2937 face_before_or_after_it_pos (it, before_p)
2938 struct it *it;
2939 int before_p;
2940 {
2941 int face_id, limit;
2942 int next_check_charpos;
2943 struct text_pos pos;
2944
2945 xassert (it->s == NULL);
2946
2947 if (STRINGP (it->string))
2948 {
2949 int bufpos, base_face_id;
2950
2951 /* No face change past the end of the string (for the case
2952 we are padding with spaces). No face change before the
2953 string start. */
2954 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2955 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2956 return it->face_id;
2957
2958 /* Set pos to the position before or after IT's current position. */
2959 if (before_p)
2960 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2961 else
2962 /* For composition, we must check the character after the
2963 composition. */
2964 pos = (it->what == IT_COMPOSITION
2965 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2966 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2967
2968 if (it->current.overlay_string_index >= 0)
2969 bufpos = IT_CHARPOS (*it);
2970 else
2971 bufpos = 0;
2972
2973 base_face_id = underlying_face_id (it);
2974
2975 /* Get the face for ASCII, or unibyte. */
2976 face_id = face_at_string_position (it->w,
2977 it->string,
2978 CHARPOS (pos),
2979 bufpos,
2980 it->region_beg_charpos,
2981 it->region_end_charpos,
2982 &next_check_charpos,
2983 base_face_id, 0);
2984
2985 /* Correct the face for charsets different from ASCII. Do it
2986 for the multibyte case only. The face returned above is
2987 suitable for unibyte text if IT->string is unibyte. */
2988 if (STRING_MULTIBYTE (it->string))
2989 {
2990 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2991 int rest = SBYTES (it->string) - BYTEPOS (pos);
2992 int c, len;
2993 struct face *face = FACE_FROM_ID (it->f, face_id);
2994
2995 c = string_char_and_length (p, rest, &len);
2996 face_id = FACE_FOR_CHAR (it->f, face, c);
2997 }
2998 }
2999 else
3000 {
3001 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3002 || (IT_CHARPOS (*it) <= BEGV && before_p))
3003 return it->face_id;
3004
3005 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3006 pos = it->current.pos;
3007
3008 if (before_p)
3009 DEC_TEXT_POS (pos, it->multibyte_p);
3010 else
3011 {
3012 if (it->what == IT_COMPOSITION)
3013 /* For composition, we must check the position after the
3014 composition. */
3015 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3016 else
3017 INC_TEXT_POS (pos, it->multibyte_p);
3018 }
3019
3020 /* Determine face for CHARSET_ASCII, or unibyte. */
3021 face_id = face_at_buffer_position (it->w,
3022 CHARPOS (pos),
3023 it->region_beg_charpos,
3024 it->region_end_charpos,
3025 &next_check_charpos,
3026 limit, 0);
3027
3028 /* Correct the face for charsets different from ASCII. Do it
3029 for the multibyte case only. The face returned above is
3030 suitable for unibyte text if current_buffer is unibyte. */
3031 if (it->multibyte_p)
3032 {
3033 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3034 struct face *face = FACE_FROM_ID (it->f, face_id);
3035 face_id = FACE_FOR_CHAR (it->f, face, c);
3036 }
3037 }
3038
3039 return face_id;
3040 }
3041
3042
3043 \f
3044 /***********************************************************************
3045 Invisible text
3046 ***********************************************************************/
3047
3048 /* Set up iterator IT from invisible properties at its current
3049 position. Called from handle_stop. */
3050
3051 static enum prop_handled
3052 handle_invisible_prop (it)
3053 struct it *it;
3054 {
3055 enum prop_handled handled = HANDLED_NORMALLY;
3056
3057 if (STRINGP (it->string))
3058 {
3059 extern Lisp_Object Qinvisible;
3060 Lisp_Object prop, end_charpos, limit, charpos;
3061
3062 /* Get the value of the invisible text property at the
3063 current position. Value will be nil if there is no such
3064 property. */
3065 charpos = make_number (IT_STRING_CHARPOS (*it));
3066 prop = Fget_text_property (charpos, Qinvisible, it->string);
3067
3068 if (!NILP (prop)
3069 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3070 {
3071 handled = HANDLED_RECOMPUTE_PROPS;
3072
3073 /* Get the position at which the next change of the
3074 invisible text property can be found in IT->string.
3075 Value will be nil if the property value is the same for
3076 all the rest of IT->string. */
3077 XSETINT (limit, SCHARS (it->string));
3078 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3079 it->string, limit);
3080
3081 /* Text at current position is invisible. The next
3082 change in the property is at position end_charpos.
3083 Move IT's current position to that position. */
3084 if (INTEGERP (end_charpos)
3085 && XFASTINT (end_charpos) < XFASTINT (limit))
3086 {
3087 struct text_pos old;
3088 old = it->current.string_pos;
3089 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3090 compute_string_pos (&it->current.string_pos, old, it->string);
3091 }
3092 else
3093 {
3094 /* The rest of the string is invisible. If this is an
3095 overlay string, proceed with the next overlay string
3096 or whatever comes and return a character from there. */
3097 if (it->current.overlay_string_index >= 0)
3098 {
3099 next_overlay_string (it);
3100 /* Don't check for overlay strings when we just
3101 finished processing them. */
3102 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3103 }
3104 else
3105 {
3106 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3107 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3108 }
3109 }
3110 }
3111 }
3112 else
3113 {
3114 int invis_p, newpos, next_stop, start_charpos;
3115 Lisp_Object pos, prop, overlay;
3116
3117 /* First of all, is there invisible text at this position? */
3118 start_charpos = IT_CHARPOS (*it);
3119 pos = make_number (IT_CHARPOS (*it));
3120 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3121 &overlay);
3122 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3123
3124 /* If we are on invisible text, skip over it. */
3125 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3126 {
3127 /* Record whether we have to display an ellipsis for the
3128 invisible text. */
3129 int display_ellipsis_p = invis_p == 2;
3130
3131 handled = HANDLED_RECOMPUTE_PROPS;
3132
3133 /* Loop skipping over invisible text. The loop is left at
3134 ZV or with IT on the first char being visible again. */
3135 do
3136 {
3137 /* Try to skip some invisible text. Return value is the
3138 position reached which can be equal to IT's position
3139 if there is nothing invisible here. This skips both
3140 over invisible text properties and overlays with
3141 invisible property. */
3142 newpos = skip_invisible (IT_CHARPOS (*it),
3143 &next_stop, ZV, it->window);
3144
3145 /* If we skipped nothing at all we weren't at invisible
3146 text in the first place. If everything to the end of
3147 the buffer was skipped, end the loop. */
3148 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3149 invis_p = 0;
3150 else
3151 {
3152 /* We skipped some characters but not necessarily
3153 all there are. Check if we ended up on visible
3154 text. Fget_char_property returns the property of
3155 the char before the given position, i.e. if we
3156 get invis_p = 0, this means that the char at
3157 newpos is visible. */
3158 pos = make_number (newpos);
3159 prop = Fget_char_property (pos, Qinvisible, it->window);
3160 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3161 }
3162
3163 /* If we ended up on invisible text, proceed to
3164 skip starting with next_stop. */
3165 if (invis_p)
3166 IT_CHARPOS (*it) = next_stop;
3167 }
3168 while (invis_p);
3169
3170 /* The position newpos is now either ZV or on visible text. */
3171 IT_CHARPOS (*it) = newpos;
3172 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3173
3174 /* If there are before-strings at the start of invisible
3175 text, and the text is invisible because of a text
3176 property, arrange to show before-strings because 20.x did
3177 it that way. (If the text is invisible because of an
3178 overlay property instead of a text property, this is
3179 already handled in the overlay code.) */
3180 if (NILP (overlay)
3181 && get_overlay_strings (it, start_charpos))
3182 {
3183 handled = HANDLED_RECOMPUTE_PROPS;
3184 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3185 }
3186 else if (display_ellipsis_p)
3187 setup_for_ellipsis (it);
3188 }
3189 }
3190
3191 return handled;
3192 }
3193
3194
3195 /* Make iterator IT return `...' next. */
3196
3197 static void
3198 setup_for_ellipsis (it)
3199 struct it *it;
3200 {
3201 if (it->dp
3202 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3203 {
3204 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3205 it->dpvec = v->contents;
3206 it->dpend = v->contents + v->size;
3207 }
3208 else
3209 {
3210 /* Default `...'. */
3211 it->dpvec = default_invis_vector;
3212 it->dpend = default_invis_vector + 3;
3213 }
3214
3215 /* The ellipsis display does not replace the display of the
3216 character at the new position. Indicate this by setting
3217 IT->dpvec_char_len to zero. */
3218 it->dpvec_char_len = 0;
3219
3220 it->current.dpvec_index = 0;
3221 it->method = next_element_from_display_vector;
3222 }
3223
3224
3225 \f
3226 /***********************************************************************
3227 'display' property
3228 ***********************************************************************/
3229
3230 /* Set up iterator IT from `display' property at its current position.
3231 Called from handle_stop. */
3232
3233 static enum prop_handled
3234 handle_display_prop (it)
3235 struct it *it;
3236 {
3237 Lisp_Object prop, object;
3238 struct text_pos *position;
3239 int display_replaced_p = 0;
3240
3241 if (STRINGP (it->string))
3242 {
3243 object = it->string;
3244 position = &it->current.string_pos;
3245 }
3246 else
3247 {
3248 object = it->w->buffer;
3249 position = &it->current.pos;
3250 }
3251
3252 /* Reset those iterator values set from display property values. */
3253 it->font_height = Qnil;
3254 it->space_width = Qnil;
3255 it->voffset = 0;
3256
3257 /* We don't support recursive `display' properties, i.e. string
3258 values that have a string `display' property, that have a string
3259 `display' property etc. */
3260 if (!it->string_from_display_prop_p)
3261 it->area = TEXT_AREA;
3262
3263 prop = Fget_char_property (make_number (position->charpos),
3264 Qdisplay, object);
3265 if (NILP (prop))
3266 return HANDLED_NORMALLY;
3267
3268 if (CONSP (prop)
3269 /* Simple properties. */
3270 && !EQ (XCAR (prop), Qimage)
3271 && !EQ (XCAR (prop), Qspace)
3272 && !EQ (XCAR (prop), Qwhen)
3273 && !EQ (XCAR (prop), Qspace_width)
3274 && !EQ (XCAR (prop), Qheight)
3275 && !EQ (XCAR (prop), Qraise)
3276 /* Marginal area specifications. */
3277 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3278 && !NILP (XCAR (prop)))
3279 {
3280 for (; CONSP (prop); prop = XCDR (prop))
3281 {
3282 if (handle_single_display_prop (it, XCAR (prop), object,
3283 position, display_replaced_p))
3284 display_replaced_p = 1;
3285 }
3286 }
3287 else if (VECTORP (prop))
3288 {
3289 int i;
3290 for (i = 0; i < ASIZE (prop); ++i)
3291 if (handle_single_display_prop (it, AREF (prop, i), object,
3292 position, display_replaced_p))
3293 display_replaced_p = 1;
3294 }
3295 else
3296 {
3297 if (handle_single_display_prop (it, prop, object, position, 0))
3298 display_replaced_p = 1;
3299 }
3300
3301 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3302 }
3303
3304
3305 /* Value is the position of the end of the `display' property starting
3306 at START_POS in OBJECT. */
3307
3308 static struct text_pos
3309 display_prop_end (it, object, start_pos)
3310 struct it *it;
3311 Lisp_Object object;
3312 struct text_pos start_pos;
3313 {
3314 Lisp_Object end;
3315 struct text_pos end_pos;
3316
3317 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3318 Qdisplay, object, Qnil);
3319 CHARPOS (end_pos) = XFASTINT (end);
3320 if (STRINGP (object))
3321 compute_string_pos (&end_pos, start_pos, it->string);
3322 else
3323 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3324
3325 return end_pos;
3326 }
3327
3328
3329 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3330 is the object in which the `display' property was found. *POSITION
3331 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3332 means that we previously saw a display sub-property which already
3333 replaced text display with something else, for example an image;
3334 ignore such properties after the first one has been processed.
3335
3336 If PROP is a `space' or `image' sub-property, set *POSITION to the
3337 end position of the `display' property.
3338
3339 Value is non-zero if something was found which replaces the display
3340 of buffer or string text. */
3341
3342 static int
3343 handle_single_display_prop (it, prop, object, position,
3344 display_replaced_before_p)
3345 struct it *it;
3346 Lisp_Object prop;
3347 Lisp_Object object;
3348 struct text_pos *position;
3349 int display_replaced_before_p;
3350 {
3351 Lisp_Object value;
3352 int replaces_text_display_p = 0;
3353 Lisp_Object form;
3354
3355 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3356 evaluated. If the result is nil, VALUE is ignored. */
3357 form = Qt;
3358 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3359 {
3360 prop = XCDR (prop);
3361 if (!CONSP (prop))
3362 return 0;
3363 form = XCAR (prop);
3364 prop = XCDR (prop);
3365 }
3366
3367 if (!NILP (form) && !EQ (form, Qt))
3368 {
3369 int count = SPECPDL_INDEX ();
3370 struct gcpro gcpro1;
3371
3372 /* Bind `object' to the object having the `display' property, a
3373 buffer or string. Bind `position' to the position in the
3374 object where the property was found, and `buffer-position'
3375 to the current position in the buffer. */
3376 specbind (Qobject, object);
3377 specbind (Qposition, make_number (CHARPOS (*position)));
3378 specbind (Qbuffer_position,
3379 make_number (STRINGP (object)
3380 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3381 GCPRO1 (form);
3382 form = safe_eval (form);
3383 UNGCPRO;
3384 unbind_to (count, Qnil);
3385 }
3386
3387 if (NILP (form))
3388 return 0;
3389
3390 if (CONSP (prop)
3391 && EQ (XCAR (prop), Qheight)
3392 && CONSP (XCDR (prop)))
3393 {
3394 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3395 return 0;
3396
3397 /* `(height HEIGHT)'. */
3398 it->font_height = XCAR (XCDR (prop));
3399 if (!NILP (it->font_height))
3400 {
3401 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3402 int new_height = -1;
3403
3404 if (CONSP (it->font_height)
3405 && (EQ (XCAR (it->font_height), Qplus)
3406 || EQ (XCAR (it->font_height), Qminus))
3407 && CONSP (XCDR (it->font_height))
3408 && INTEGERP (XCAR (XCDR (it->font_height))))
3409 {
3410 /* `(+ N)' or `(- N)' where N is an integer. */
3411 int steps = XINT (XCAR (XCDR (it->font_height)));
3412 if (EQ (XCAR (it->font_height), Qplus))
3413 steps = - steps;
3414 it->face_id = smaller_face (it->f, it->face_id, steps);
3415 }
3416 else if (FUNCTIONP (it->font_height))
3417 {
3418 /* Call function with current height as argument.
3419 Value is the new height. */
3420 Lisp_Object height;
3421 height = safe_call1 (it->font_height,
3422 face->lface[LFACE_HEIGHT_INDEX]);
3423 if (NUMBERP (height))
3424 new_height = XFLOATINT (height);
3425 }
3426 else if (NUMBERP (it->font_height))
3427 {
3428 /* Value is a multiple of the canonical char height. */
3429 struct face *face;
3430
3431 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3432 new_height = (XFLOATINT (it->font_height)
3433 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3434 }
3435 else
3436 {
3437 /* Evaluate IT->font_height with `height' bound to the
3438 current specified height to get the new height. */
3439 Lisp_Object value;
3440 int count = SPECPDL_INDEX ();
3441
3442 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3443 value = safe_eval (it->font_height);
3444 unbind_to (count, Qnil);
3445
3446 if (NUMBERP (value))
3447 new_height = XFLOATINT (value);
3448 }
3449
3450 if (new_height > 0)
3451 it->face_id = face_with_height (it->f, it->face_id, new_height);
3452 }
3453 }
3454 else if (CONSP (prop)
3455 && EQ (XCAR (prop), Qspace_width)
3456 && CONSP (XCDR (prop)))
3457 {
3458 /* `(space_width WIDTH)'. */
3459 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3460 return 0;
3461
3462 value = XCAR (XCDR (prop));
3463 if (NUMBERP (value) && XFLOATINT (value) > 0)
3464 it->space_width = value;
3465 }
3466 else if (CONSP (prop)
3467 && EQ (XCAR (prop), Qraise)
3468 && CONSP (XCDR (prop)))
3469 {
3470 /* `(raise FACTOR)'. */
3471 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3472 return 0;
3473
3474 #ifdef HAVE_WINDOW_SYSTEM
3475 value = XCAR (XCDR (prop));
3476 if (NUMBERP (value))
3477 {
3478 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3479 it->voffset = - (XFLOATINT (value)
3480 * (FONT_HEIGHT (face->font)));
3481 }
3482 #endif /* HAVE_WINDOW_SYSTEM */
3483 }
3484 else if (!it->string_from_display_prop_p)
3485 {
3486 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3487 VALUE) or `((margin nil) VALUE)' or VALUE. */
3488 Lisp_Object location, value;
3489 struct text_pos start_pos;
3490 int valid_p;
3491
3492 /* Characters having this form of property are not displayed, so
3493 we have to find the end of the property. */
3494 start_pos = *position;
3495 *position = display_prop_end (it, object, start_pos);
3496 value = Qnil;
3497
3498 /* Let's stop at the new position and assume that all
3499 text properties change there. */
3500 it->stop_charpos = position->charpos;
3501
3502 location = Qunbound;
3503 if (CONSP (prop) && CONSP (XCAR (prop)))
3504 {
3505 Lisp_Object tem;
3506
3507 value = XCDR (prop);
3508 if (CONSP (value))
3509 value = XCAR (value);
3510
3511 tem = XCAR (prop);
3512 if (EQ (XCAR (tem), Qmargin)
3513 && (tem = XCDR (tem),
3514 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3515 (NILP (tem)
3516 || EQ (tem, Qleft_margin)
3517 || EQ (tem, Qright_margin))))
3518 location = tem;
3519 }
3520
3521 if (EQ (location, Qunbound))
3522 {
3523 location = Qnil;
3524 value = prop;
3525 }
3526
3527 #ifdef HAVE_WINDOW_SYSTEM
3528 if (FRAME_TERMCAP_P (it->f))
3529 valid_p = STRINGP (value);
3530 else
3531 valid_p = (STRINGP (value)
3532 || (CONSP (value) && EQ (XCAR (value), Qspace))
3533 || valid_image_p (value));
3534 #else /* not HAVE_WINDOW_SYSTEM */
3535 valid_p = STRINGP (value);
3536 #endif /* not HAVE_WINDOW_SYSTEM */
3537
3538 if ((EQ (location, Qleft_margin)
3539 || EQ (location, Qright_margin)
3540 || NILP (location))
3541 && valid_p
3542 && !display_replaced_before_p)
3543 {
3544 replaces_text_display_p = 1;
3545
3546 /* Save current settings of IT so that we can restore them
3547 when we are finished with the glyph property value. */
3548 push_it (it);
3549
3550 if (NILP (location))
3551 it->area = TEXT_AREA;
3552 else if (EQ (location, Qleft_margin))
3553 it->area = LEFT_MARGIN_AREA;
3554 else
3555 it->area = RIGHT_MARGIN_AREA;
3556
3557 if (STRINGP (value))
3558 {
3559 it->string = value;
3560 it->multibyte_p = STRING_MULTIBYTE (it->string);
3561 it->current.overlay_string_index = -1;
3562 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3563 it->end_charpos = it->string_nchars = SCHARS (it->string);
3564 it->method = next_element_from_string;
3565 it->stop_charpos = 0;
3566 it->string_from_display_prop_p = 1;
3567 /* Say that we haven't consumed the characters with
3568 `display' property yet. The call to pop_it in
3569 set_iterator_to_next will clean this up. */
3570 *position = start_pos;
3571 }
3572 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3573 {
3574 it->method = next_element_from_stretch;
3575 it->object = value;
3576 it->current.pos = it->position = start_pos;
3577 }
3578 #ifdef HAVE_WINDOW_SYSTEM
3579 else
3580 {
3581 it->what = IT_IMAGE;
3582 it->image_id = lookup_image (it->f, value);
3583 it->position = start_pos;
3584 it->object = NILP (object) ? it->w->buffer : object;
3585 it->method = next_element_from_image;
3586
3587 /* Say that we haven't consumed the characters with
3588 `display' property yet. The call to pop_it in
3589 set_iterator_to_next will clean this up. */
3590 *position = start_pos;
3591 }
3592 #endif /* HAVE_WINDOW_SYSTEM */
3593 }
3594 else
3595 /* Invalid property or property not supported. Restore
3596 the position to what it was before. */
3597 *position = start_pos;
3598 }
3599
3600 return replaces_text_display_p;
3601 }
3602
3603
3604 /* Check if PROP is a display sub-property value whose text should be
3605 treated as intangible. */
3606
3607 static int
3608 single_display_prop_intangible_p (prop)
3609 Lisp_Object prop;
3610 {
3611 /* Skip over `when FORM'. */
3612 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3613 {
3614 prop = XCDR (prop);
3615 if (!CONSP (prop))
3616 return 0;
3617 prop = XCDR (prop);
3618 }
3619
3620 if (STRINGP (prop))
3621 return 1;
3622
3623 if (!CONSP (prop))
3624 return 0;
3625
3626 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3627 we don't need to treat text as intangible. */
3628 if (EQ (XCAR (prop), Qmargin))
3629 {
3630 prop = XCDR (prop);
3631 if (!CONSP (prop))
3632 return 0;
3633
3634 prop = XCDR (prop);
3635 if (!CONSP (prop)
3636 || EQ (XCAR (prop), Qleft_margin)
3637 || EQ (XCAR (prop), Qright_margin))
3638 return 0;
3639 }
3640
3641 return (CONSP (prop)
3642 && (EQ (XCAR (prop), Qimage)
3643 || EQ (XCAR (prop), Qspace)));
3644 }
3645
3646
3647 /* Check if PROP is a display property value whose text should be
3648 treated as intangible. */
3649
3650 int
3651 display_prop_intangible_p (prop)
3652 Lisp_Object prop;
3653 {
3654 if (CONSP (prop)
3655 && CONSP (XCAR (prop))
3656 && !EQ (Qmargin, XCAR (XCAR (prop))))
3657 {
3658 /* A list of sub-properties. */
3659 while (CONSP (prop))
3660 {
3661 if (single_display_prop_intangible_p (XCAR (prop)))
3662 return 1;
3663 prop = XCDR (prop);
3664 }
3665 }
3666 else if (VECTORP (prop))
3667 {
3668 /* A vector of sub-properties. */
3669 int i;
3670 for (i = 0; i < ASIZE (prop); ++i)
3671 if (single_display_prop_intangible_p (AREF (prop, i)))
3672 return 1;
3673 }
3674 else
3675 return single_display_prop_intangible_p (prop);
3676
3677 return 0;
3678 }
3679
3680
3681 /* Return 1 if PROP is a display sub-property value containing STRING. */
3682
3683 static int
3684 single_display_prop_string_p (prop, string)
3685 Lisp_Object prop, string;
3686 {
3687 if (EQ (string, prop))
3688 return 1;
3689
3690 /* Skip over `when FORM'. */
3691 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3692 {
3693 prop = XCDR (prop);
3694 if (!CONSP (prop))
3695 return 0;
3696 prop = XCDR (prop);
3697 }
3698
3699 if (CONSP (prop))
3700 /* Skip over `margin LOCATION'. */
3701 if (EQ (XCAR (prop), Qmargin))
3702 {
3703 prop = XCDR (prop);
3704 if (!CONSP (prop))
3705 return 0;
3706
3707 prop = XCDR (prop);
3708 if (!CONSP (prop))
3709 return 0;
3710 }
3711
3712 return CONSP (prop) && EQ (XCAR (prop), string);
3713 }
3714
3715
3716 /* Return 1 if STRING appears in the `display' property PROP. */
3717
3718 static int
3719 display_prop_string_p (prop, string)
3720 Lisp_Object prop, string;
3721 {
3722 if (CONSP (prop)
3723 && CONSP (XCAR (prop))
3724 && !EQ (Qmargin, XCAR (XCAR (prop))))
3725 {
3726 /* A list of sub-properties. */
3727 while (CONSP (prop))
3728 {
3729 if (single_display_prop_string_p (XCAR (prop), string))
3730 return 1;
3731 prop = XCDR (prop);
3732 }
3733 }
3734 else if (VECTORP (prop))
3735 {
3736 /* A vector of sub-properties. */
3737 int i;
3738 for (i = 0; i < ASIZE (prop); ++i)
3739 if (single_display_prop_string_p (AREF (prop, i), string))
3740 return 1;
3741 }
3742 else
3743 return single_display_prop_string_p (prop, string);
3744
3745 return 0;
3746 }
3747
3748
3749 /* Determine from which buffer position in W's buffer STRING comes
3750 from. AROUND_CHARPOS is an approximate position where it could
3751 be from. Value is the buffer position or 0 if it couldn't be
3752 determined.
3753
3754 W's buffer must be current.
3755
3756 This function is necessary because we don't record buffer positions
3757 in glyphs generated from strings (to keep struct glyph small).
3758 This function may only use code that doesn't eval because it is
3759 called asynchronously from note_mouse_highlight. */
3760
3761 int
3762 string_buffer_position (w, string, around_charpos)
3763 struct window *w;
3764 Lisp_Object string;
3765 int around_charpos;
3766 {
3767 Lisp_Object limit, prop, pos;
3768 const int MAX_DISTANCE = 1000;
3769 int found = 0;
3770
3771 pos = make_number (around_charpos);
3772 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3773 while (!found && !EQ (pos, limit))
3774 {
3775 prop = Fget_char_property (pos, Qdisplay, Qnil);
3776 if (!NILP (prop) && display_prop_string_p (prop, string))
3777 found = 1;
3778 else
3779 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3780 }
3781
3782 if (!found)
3783 {
3784 pos = make_number (around_charpos);
3785 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3786 while (!found && !EQ (pos, limit))
3787 {
3788 prop = Fget_char_property (pos, Qdisplay, Qnil);
3789 if (!NILP (prop) && display_prop_string_p (prop, string))
3790 found = 1;
3791 else
3792 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3793 limit);
3794 }
3795 }
3796
3797 return found ? XINT (pos) : 0;
3798 }
3799
3800
3801 \f
3802 /***********************************************************************
3803 `composition' property
3804 ***********************************************************************/
3805
3806 /* Set up iterator IT from `composition' property at its current
3807 position. Called from handle_stop. */
3808
3809 static enum prop_handled
3810 handle_composition_prop (it)
3811 struct it *it;
3812 {
3813 Lisp_Object prop, string;
3814 int pos, pos_byte, end;
3815 enum prop_handled handled = HANDLED_NORMALLY;
3816
3817 if (STRINGP (it->string))
3818 {
3819 pos = IT_STRING_CHARPOS (*it);
3820 pos_byte = IT_STRING_BYTEPOS (*it);
3821 string = it->string;
3822 }
3823 else
3824 {
3825 pos = IT_CHARPOS (*it);
3826 pos_byte = IT_BYTEPOS (*it);
3827 string = Qnil;
3828 }
3829
3830 /* If there's a valid composition and point is not inside of the
3831 composition (in the case that the composition is from the current
3832 buffer), draw a glyph composed from the composition components. */
3833 if (find_composition (pos, -1, &pos, &end, &prop, string)
3834 && COMPOSITION_VALID_P (pos, end, prop)
3835 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3836 {
3837 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3838
3839 if (id >= 0)
3840 {
3841 it->method = next_element_from_composition;
3842 it->cmp_id = id;
3843 it->cmp_len = COMPOSITION_LENGTH (prop);
3844 /* For a terminal, draw only the first character of the
3845 components. */
3846 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3847 it->len = (STRINGP (it->string)
3848 ? string_char_to_byte (it->string, end)
3849 : CHAR_TO_BYTE (end)) - pos_byte;
3850 it->stop_charpos = end;
3851 handled = HANDLED_RETURN;
3852 }
3853 }
3854
3855 return handled;
3856 }
3857
3858
3859 \f
3860 /***********************************************************************
3861 Overlay strings
3862 ***********************************************************************/
3863
3864 /* The following structure is used to record overlay strings for
3865 later sorting in load_overlay_strings. */
3866
3867 struct overlay_entry
3868 {
3869 Lisp_Object overlay;
3870 Lisp_Object string;
3871 int priority;
3872 int after_string_p;
3873 };
3874
3875
3876 /* Set up iterator IT from overlay strings at its current position.
3877 Called from handle_stop. */
3878
3879 static enum prop_handled
3880 handle_overlay_change (it)
3881 struct it *it;
3882 {
3883 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3884 return HANDLED_RECOMPUTE_PROPS;
3885 else
3886 return HANDLED_NORMALLY;
3887 }
3888
3889
3890 /* Set up the next overlay string for delivery by IT, if there is an
3891 overlay string to deliver. Called by set_iterator_to_next when the
3892 end of the current overlay string is reached. If there are more
3893 overlay strings to display, IT->string and
3894 IT->current.overlay_string_index are set appropriately here.
3895 Otherwise IT->string is set to nil. */
3896
3897 static void
3898 next_overlay_string (it)
3899 struct it *it;
3900 {
3901 ++it->current.overlay_string_index;
3902 if (it->current.overlay_string_index == it->n_overlay_strings)
3903 {
3904 /* No more overlay strings. Restore IT's settings to what
3905 they were before overlay strings were processed, and
3906 continue to deliver from current_buffer. */
3907 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3908
3909 pop_it (it);
3910 xassert (it->stop_charpos >= BEGV
3911 && it->stop_charpos <= it->end_charpos);
3912 it->string = Qnil;
3913 it->current.overlay_string_index = -1;
3914 SET_TEXT_POS (it->current.string_pos, -1, -1);
3915 it->n_overlay_strings = 0;
3916 it->method = next_element_from_buffer;
3917
3918 /* If we're at the end of the buffer, record that we have
3919 processed the overlay strings there already, so that
3920 next_element_from_buffer doesn't try it again. */
3921 if (IT_CHARPOS (*it) >= it->end_charpos)
3922 it->overlay_strings_at_end_processed_p = 1;
3923
3924 /* If we have to display `...' for invisible text, set
3925 the iterator up for that. */
3926 if (display_ellipsis_p)
3927 setup_for_ellipsis (it);
3928 }
3929 else
3930 {
3931 /* There are more overlay strings to process. If
3932 IT->current.overlay_string_index has advanced to a position
3933 where we must load IT->overlay_strings with more strings, do
3934 it. */
3935 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3936
3937 if (it->current.overlay_string_index && i == 0)
3938 load_overlay_strings (it, 0);
3939
3940 /* Initialize IT to deliver display elements from the overlay
3941 string. */
3942 it->string = it->overlay_strings[i];
3943 it->multibyte_p = STRING_MULTIBYTE (it->string);
3944 SET_TEXT_POS (it->current.string_pos, 0, 0);
3945 it->method = next_element_from_string;
3946 it->stop_charpos = 0;
3947 }
3948
3949 CHECK_IT (it);
3950 }
3951
3952
3953 /* Compare two overlay_entry structures E1 and E2. Used as a
3954 comparison function for qsort in load_overlay_strings. Overlay
3955 strings for the same position are sorted so that
3956
3957 1. All after-strings come in front of before-strings, except
3958 when they come from the same overlay.
3959
3960 2. Within after-strings, strings are sorted so that overlay strings
3961 from overlays with higher priorities come first.
3962
3963 2. Within before-strings, strings are sorted so that overlay
3964 strings from overlays with higher priorities come last.
3965
3966 Value is analogous to strcmp. */
3967
3968
3969 static int
3970 compare_overlay_entries (e1, e2)
3971 void *e1, *e2;
3972 {
3973 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3974 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3975 int result;
3976
3977 if (entry1->after_string_p != entry2->after_string_p)
3978 {
3979 /* Let after-strings appear in front of before-strings if
3980 they come from different overlays. */
3981 if (EQ (entry1->overlay, entry2->overlay))
3982 result = entry1->after_string_p ? 1 : -1;
3983 else
3984 result = entry1->after_string_p ? -1 : 1;
3985 }
3986 else if (entry1->after_string_p)
3987 /* After-strings sorted in order of decreasing priority. */
3988 result = entry2->priority - entry1->priority;
3989 else
3990 /* Before-strings sorted in order of increasing priority. */
3991 result = entry1->priority - entry2->priority;
3992
3993 return result;
3994 }
3995
3996
3997 /* Load the vector IT->overlay_strings with overlay strings from IT's
3998 current buffer position, or from CHARPOS if that is > 0. Set
3999 IT->n_overlays to the total number of overlay strings found.
4000
4001 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4002 a time. On entry into load_overlay_strings,
4003 IT->current.overlay_string_index gives the number of overlay
4004 strings that have already been loaded by previous calls to this
4005 function.
4006
4007 IT->add_overlay_start contains an additional overlay start
4008 position to consider for taking overlay strings from, if non-zero.
4009 This position comes into play when the overlay has an `invisible'
4010 property, and both before and after-strings. When we've skipped to
4011 the end of the overlay, because of its `invisible' property, we
4012 nevertheless want its before-string to appear.
4013 IT->add_overlay_start will contain the overlay start position
4014 in this case.
4015
4016 Overlay strings are sorted so that after-string strings come in
4017 front of before-string strings. Within before and after-strings,
4018 strings are sorted by overlay priority. See also function
4019 compare_overlay_entries. */
4020
4021 static void
4022 load_overlay_strings (it, charpos)
4023 struct it *it;
4024 int charpos;
4025 {
4026 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4027 Lisp_Object overlay, window, str, invisible;
4028 struct Lisp_Overlay *ov;
4029 int start, end;
4030 int size = 20;
4031 int n = 0, i, j, invis_p;
4032 struct overlay_entry *entries
4033 = (struct overlay_entry *) alloca (size * sizeof *entries);
4034
4035 if (charpos <= 0)
4036 charpos = IT_CHARPOS (*it);
4037
4038 /* Append the overlay string STRING of overlay OVERLAY to vector
4039 `entries' which has size `size' and currently contains `n'
4040 elements. AFTER_P non-zero means STRING is an after-string of
4041 OVERLAY. */
4042 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4043 do \
4044 { \
4045 Lisp_Object priority; \
4046 \
4047 if (n == size) \
4048 { \
4049 int new_size = 2 * size; \
4050 struct overlay_entry *old = entries; \
4051 entries = \
4052 (struct overlay_entry *) alloca (new_size \
4053 * sizeof *entries); \
4054 bcopy (old, entries, size * sizeof *entries); \
4055 size = new_size; \
4056 } \
4057 \
4058 entries[n].string = (STRING); \
4059 entries[n].overlay = (OVERLAY); \
4060 priority = Foverlay_get ((OVERLAY), Qpriority); \
4061 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4062 entries[n].after_string_p = (AFTER_P); \
4063 ++n; \
4064 } \
4065 while (0)
4066
4067 /* Process overlay before the overlay center. */
4068 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4069 {
4070 XSETMISC (overlay, ov);
4071 xassert (OVERLAYP (overlay));
4072 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4073 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4074
4075 if (end < charpos)
4076 break;
4077
4078 /* Skip this overlay if it doesn't start or end at IT's current
4079 position. */
4080 if (end != charpos && start != charpos)
4081 continue;
4082
4083 /* Skip this overlay if it doesn't apply to IT->w. */
4084 window = Foverlay_get (overlay, Qwindow);
4085 if (WINDOWP (window) && XWINDOW (window) != it->w)
4086 continue;
4087
4088 /* If the text ``under'' the overlay is invisible, both before-
4089 and after-strings from this overlay are visible; start and
4090 end position are indistinguishable. */
4091 invisible = Foverlay_get (overlay, Qinvisible);
4092 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4093
4094 /* If overlay has a non-empty before-string, record it. */
4095 if ((start == charpos || (end == charpos && invis_p))
4096 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4097 && SCHARS (str))
4098 RECORD_OVERLAY_STRING (overlay, str, 0);
4099
4100 /* If overlay has a non-empty after-string, record it. */
4101 if ((end == charpos || (start == charpos && invis_p))
4102 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4103 && SCHARS (str))
4104 RECORD_OVERLAY_STRING (overlay, str, 1);
4105 }
4106
4107 /* Process overlays after the overlay center. */
4108 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4109 {
4110 XSETMISC (overlay, ov);
4111 xassert (OVERLAYP (overlay));
4112 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4113 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4114
4115 if (start > charpos)
4116 break;
4117
4118 /* Skip this overlay if it doesn't start or end at IT's current
4119 position. */
4120 if (end != charpos && start != charpos)
4121 continue;
4122
4123 /* Skip this overlay if it doesn't apply to IT->w. */
4124 window = Foverlay_get (overlay, Qwindow);
4125 if (WINDOWP (window) && XWINDOW (window) != it->w)
4126 continue;
4127
4128 /* If the text ``under'' the overlay is invisible, it has a zero
4129 dimension, and both before- and after-strings apply. */
4130 invisible = Foverlay_get (overlay, Qinvisible);
4131 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4132
4133 /* If overlay has a non-empty before-string, record it. */
4134 if ((start == charpos || (end == charpos && invis_p))
4135 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4136 && SCHARS (str))
4137 RECORD_OVERLAY_STRING (overlay, str, 0);
4138
4139 /* If overlay has a non-empty after-string, record it. */
4140 if ((end == charpos || (start == charpos && invis_p))
4141 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4142 && SCHARS (str))
4143 RECORD_OVERLAY_STRING (overlay, str, 1);
4144 }
4145
4146 #undef RECORD_OVERLAY_STRING
4147
4148 /* Sort entries. */
4149 if (n > 1)
4150 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4151
4152 /* Record the total number of strings to process. */
4153 it->n_overlay_strings = n;
4154
4155 /* IT->current.overlay_string_index is the number of overlay strings
4156 that have already been consumed by IT. Copy some of the
4157 remaining overlay strings to IT->overlay_strings. */
4158 i = 0;
4159 j = it->current.overlay_string_index;
4160 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4161 it->overlay_strings[i++] = entries[j++].string;
4162
4163 CHECK_IT (it);
4164 }
4165
4166
4167 /* Get the first chunk of overlay strings at IT's current buffer
4168 position, or at CHARPOS if that is > 0. Value is non-zero if at
4169 least one overlay string was found. */
4170
4171 static int
4172 get_overlay_strings (it, charpos)
4173 struct it *it;
4174 int charpos;
4175 {
4176 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4177 process. This fills IT->overlay_strings with strings, and sets
4178 IT->n_overlay_strings to the total number of strings to process.
4179 IT->pos.overlay_string_index has to be set temporarily to zero
4180 because load_overlay_strings needs this; it must be set to -1
4181 when no overlay strings are found because a zero value would
4182 indicate a position in the first overlay string. */
4183 it->current.overlay_string_index = 0;
4184 load_overlay_strings (it, charpos);
4185
4186 /* If we found overlay strings, set up IT to deliver display
4187 elements from the first one. Otherwise set up IT to deliver
4188 from current_buffer. */
4189 if (it->n_overlay_strings)
4190 {
4191 /* Make sure we know settings in current_buffer, so that we can
4192 restore meaningful values when we're done with the overlay
4193 strings. */
4194 compute_stop_pos (it);
4195 xassert (it->face_id >= 0);
4196
4197 /* Save IT's settings. They are restored after all overlay
4198 strings have been processed. */
4199 xassert (it->sp == 0);
4200 push_it (it);
4201
4202 /* Set up IT to deliver display elements from the first overlay
4203 string. */
4204 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4205 it->string = it->overlay_strings[0];
4206 it->stop_charpos = 0;
4207 xassert (STRINGP (it->string));
4208 it->end_charpos = SCHARS (it->string);
4209 it->multibyte_p = STRING_MULTIBYTE (it->string);
4210 it->method = next_element_from_string;
4211 }
4212 else
4213 {
4214 it->string = Qnil;
4215 it->current.overlay_string_index = -1;
4216 it->method = next_element_from_buffer;
4217 }
4218
4219 CHECK_IT (it);
4220
4221 /* Value is non-zero if we found at least one overlay string. */
4222 return STRINGP (it->string);
4223 }
4224
4225
4226 \f
4227 /***********************************************************************
4228 Saving and restoring state
4229 ***********************************************************************/
4230
4231 /* Save current settings of IT on IT->stack. Called, for example,
4232 before setting up IT for an overlay string, to be able to restore
4233 IT's settings to what they were after the overlay string has been
4234 processed. */
4235
4236 static void
4237 push_it (it)
4238 struct it *it;
4239 {
4240 struct iterator_stack_entry *p;
4241
4242 xassert (it->sp < 2);
4243 p = it->stack + it->sp;
4244
4245 p->stop_charpos = it->stop_charpos;
4246 xassert (it->face_id >= 0);
4247 p->face_id = it->face_id;
4248 p->string = it->string;
4249 p->pos = it->current;
4250 p->end_charpos = it->end_charpos;
4251 p->string_nchars = it->string_nchars;
4252 p->area = it->area;
4253 p->multibyte_p = it->multibyte_p;
4254 p->space_width = it->space_width;
4255 p->font_height = it->font_height;
4256 p->voffset = it->voffset;
4257 p->string_from_display_prop_p = it->string_from_display_prop_p;
4258 p->display_ellipsis_p = 0;
4259 ++it->sp;
4260 }
4261
4262
4263 /* Restore IT's settings from IT->stack. Called, for example, when no
4264 more overlay strings must be processed, and we return to delivering
4265 display elements from a buffer, or when the end of a string from a
4266 `display' property is reached and we return to delivering display
4267 elements from an overlay string, or from a buffer. */
4268
4269 static void
4270 pop_it (it)
4271 struct it *it;
4272 {
4273 struct iterator_stack_entry *p;
4274
4275 xassert (it->sp > 0);
4276 --it->sp;
4277 p = it->stack + it->sp;
4278 it->stop_charpos = p->stop_charpos;
4279 it->face_id = p->face_id;
4280 it->string = p->string;
4281 it->current = p->pos;
4282 it->end_charpos = p->end_charpos;
4283 it->string_nchars = p->string_nchars;
4284 it->area = p->area;
4285 it->multibyte_p = p->multibyte_p;
4286 it->space_width = p->space_width;
4287 it->font_height = p->font_height;
4288 it->voffset = p->voffset;
4289 it->string_from_display_prop_p = p->string_from_display_prop_p;
4290 }
4291
4292
4293 \f
4294 /***********************************************************************
4295 Moving over lines
4296 ***********************************************************************/
4297
4298 /* Set IT's current position to the previous line start. */
4299
4300 static void
4301 back_to_previous_line_start (it)
4302 struct it *it;
4303 {
4304 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4305 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4306 }
4307
4308
4309 /* Move IT to the next line start.
4310
4311 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4312 we skipped over part of the text (as opposed to moving the iterator
4313 continuously over the text). Otherwise, don't change the value
4314 of *SKIPPED_P.
4315
4316 Newlines may come from buffer text, overlay strings, or strings
4317 displayed via the `display' property. That's the reason we can't
4318 simply use find_next_newline_no_quit.
4319
4320 Note that this function may not skip over invisible text that is so
4321 because of text properties and immediately follows a newline. If
4322 it would, function reseat_at_next_visible_line_start, when called
4323 from set_iterator_to_next, would effectively make invisible
4324 characters following a newline part of the wrong glyph row, which
4325 leads to wrong cursor motion. */
4326
4327 static int
4328 forward_to_next_line_start (it, skipped_p)
4329 struct it *it;
4330 int *skipped_p;
4331 {
4332 int old_selective, newline_found_p, n;
4333 const int MAX_NEWLINE_DISTANCE = 500;
4334
4335 /* If already on a newline, just consume it to avoid unintended
4336 skipping over invisible text below. */
4337 if (it->what == IT_CHARACTER
4338 && it->c == '\n'
4339 && CHARPOS (it->position) == IT_CHARPOS (*it))
4340 {
4341 set_iterator_to_next (it, 0);
4342 it->c = 0;
4343 return 1;
4344 }
4345
4346 /* Don't handle selective display in the following. It's (a)
4347 unnecessary because it's done by the caller, and (b) leads to an
4348 infinite recursion because next_element_from_ellipsis indirectly
4349 calls this function. */
4350 old_selective = it->selective;
4351 it->selective = 0;
4352
4353 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4354 from buffer text. */
4355 for (n = newline_found_p = 0;
4356 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4357 n += STRINGP (it->string) ? 0 : 1)
4358 {
4359 if (!get_next_display_element (it))
4360 return 0;
4361 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4362 set_iterator_to_next (it, 0);
4363 }
4364
4365 /* If we didn't find a newline near enough, see if we can use a
4366 short-cut. */
4367 if (!newline_found_p)
4368 {
4369 int start = IT_CHARPOS (*it);
4370 int limit = find_next_newline_no_quit (start, 1);
4371 Lisp_Object pos;
4372
4373 xassert (!STRINGP (it->string));
4374
4375 /* If there isn't any `display' property in sight, and no
4376 overlays, we can just use the position of the newline in
4377 buffer text. */
4378 if (it->stop_charpos >= limit
4379 || ((pos = Fnext_single_property_change (make_number (start),
4380 Qdisplay,
4381 Qnil, make_number (limit)),
4382 NILP (pos))
4383 && next_overlay_change (start) == ZV))
4384 {
4385 IT_CHARPOS (*it) = limit;
4386 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4387 *skipped_p = newline_found_p = 1;
4388 }
4389 else
4390 {
4391 while (get_next_display_element (it)
4392 && !newline_found_p)
4393 {
4394 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4395 set_iterator_to_next (it, 0);
4396 }
4397 }
4398 }
4399
4400 it->selective = old_selective;
4401 return newline_found_p;
4402 }
4403
4404
4405 /* Set IT's current position to the previous visible line start. Skip
4406 invisible text that is so either due to text properties or due to
4407 selective display. Caution: this does not change IT->current_x and
4408 IT->hpos. */
4409
4410 static void
4411 back_to_previous_visible_line_start (it)
4412 struct it *it;
4413 {
4414 int visible_p = 0;
4415
4416 /* Go back one newline if not on BEGV already. */
4417 if (IT_CHARPOS (*it) > BEGV)
4418 back_to_previous_line_start (it);
4419
4420 /* Move over lines that are invisible because of selective display
4421 or text properties. */
4422 while (IT_CHARPOS (*it) > BEGV
4423 && !visible_p)
4424 {
4425 visible_p = 1;
4426
4427 /* If selective > 0, then lines indented more than that values
4428 are invisible. */
4429 if (it->selective > 0
4430 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4431 (double) it->selective)) /* iftc */
4432 visible_p = 0;
4433 else
4434 {
4435 Lisp_Object prop;
4436
4437 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4438 Qinvisible, it->window);
4439 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4440 visible_p = 0;
4441 }
4442
4443 /* Back one more newline if the current one is invisible. */
4444 if (!visible_p)
4445 back_to_previous_line_start (it);
4446 }
4447
4448 xassert (IT_CHARPOS (*it) >= BEGV);
4449 xassert (IT_CHARPOS (*it) == BEGV
4450 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4451 CHECK_IT (it);
4452 }
4453
4454
4455 /* Reseat iterator IT at the previous visible line start. Skip
4456 invisible text that is so either due to text properties or due to
4457 selective display. At the end, update IT's overlay information,
4458 face information etc. */
4459
4460 static void
4461 reseat_at_previous_visible_line_start (it)
4462 struct it *it;
4463 {
4464 back_to_previous_visible_line_start (it);
4465 reseat (it, it->current.pos, 1);
4466 CHECK_IT (it);
4467 }
4468
4469
4470 /* Reseat iterator IT on the next visible line start in the current
4471 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4472 preceding the line start. Skip over invisible text that is so
4473 because of selective display. Compute faces, overlays etc at the
4474 new position. Note that this function does not skip over text that
4475 is invisible because of text properties. */
4476
4477 static void
4478 reseat_at_next_visible_line_start (it, on_newline_p)
4479 struct it *it;
4480 int on_newline_p;
4481 {
4482 int newline_found_p, skipped_p = 0;
4483
4484 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4485
4486 /* Skip over lines that are invisible because they are indented
4487 more than the value of IT->selective. */
4488 if (it->selective > 0)
4489 while (IT_CHARPOS (*it) < ZV
4490 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4491 (double) it->selective)) /* iftc */
4492 {
4493 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4494 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4495 }
4496
4497 /* Position on the newline if that's what's requested. */
4498 if (on_newline_p && newline_found_p)
4499 {
4500 if (STRINGP (it->string))
4501 {
4502 if (IT_STRING_CHARPOS (*it) > 0)
4503 {
4504 --IT_STRING_CHARPOS (*it);
4505 --IT_STRING_BYTEPOS (*it);
4506 }
4507 }
4508 else if (IT_CHARPOS (*it) > BEGV)
4509 {
4510 --IT_CHARPOS (*it);
4511 --IT_BYTEPOS (*it);
4512 reseat (it, it->current.pos, 0);
4513 }
4514 }
4515 else if (skipped_p)
4516 reseat (it, it->current.pos, 0);
4517
4518 CHECK_IT (it);
4519 }
4520
4521
4522 \f
4523 /***********************************************************************
4524 Changing an iterator's position
4525 ***********************************************************************/
4526
4527 /* Change IT's current position to POS in current_buffer. If FORCE_P
4528 is non-zero, always check for text properties at the new position.
4529 Otherwise, text properties are only looked up if POS >=
4530 IT->check_charpos of a property. */
4531
4532 static void
4533 reseat (it, pos, force_p)
4534 struct it *it;
4535 struct text_pos pos;
4536 int force_p;
4537 {
4538 int original_pos = IT_CHARPOS (*it);
4539
4540 reseat_1 (it, pos, 0);
4541
4542 /* Determine where to check text properties. Avoid doing it
4543 where possible because text property lookup is very expensive. */
4544 if (force_p
4545 || CHARPOS (pos) > it->stop_charpos
4546 || CHARPOS (pos) < original_pos)
4547 handle_stop (it);
4548
4549 CHECK_IT (it);
4550 }
4551
4552
4553 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4554 IT->stop_pos to POS, also. */
4555
4556 static void
4557 reseat_1 (it, pos, set_stop_p)
4558 struct it *it;
4559 struct text_pos pos;
4560 int set_stop_p;
4561 {
4562 /* Don't call this function when scanning a C string. */
4563 xassert (it->s == NULL);
4564
4565 /* POS must be a reasonable value. */
4566 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4567
4568 it->current.pos = it->position = pos;
4569 XSETBUFFER (it->object, current_buffer);
4570 it->end_charpos = ZV;
4571 it->dpvec = NULL;
4572 it->current.dpvec_index = -1;
4573 it->current.overlay_string_index = -1;
4574 IT_STRING_CHARPOS (*it) = -1;
4575 IT_STRING_BYTEPOS (*it) = -1;
4576 it->string = Qnil;
4577 it->method = next_element_from_buffer;
4578 /* RMS: I added this to fix a bug in move_it_vertically_backward
4579 where it->area continued to relate to the starting point
4580 for the backward motion. Bug report from
4581 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4582 However, I am not sure whether reseat still does the right thing
4583 in general after this change. */
4584 it->area = TEXT_AREA;
4585 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4586 it->sp = 0;
4587 it->face_before_selective_p = 0;
4588
4589 if (set_stop_p)
4590 it->stop_charpos = CHARPOS (pos);
4591 }
4592
4593
4594 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4595 If S is non-null, it is a C string to iterate over. Otherwise,
4596 STRING gives a Lisp string to iterate over.
4597
4598 If PRECISION > 0, don't return more then PRECISION number of
4599 characters from the string.
4600
4601 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4602 characters have been returned. FIELD_WIDTH < 0 means an infinite
4603 field width.
4604
4605 MULTIBYTE = 0 means disable processing of multibyte characters,
4606 MULTIBYTE > 0 means enable it,
4607 MULTIBYTE < 0 means use IT->multibyte_p.
4608
4609 IT must be initialized via a prior call to init_iterator before
4610 calling this function. */
4611
4612 static void
4613 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4614 struct it *it;
4615 unsigned char *s;
4616 Lisp_Object string;
4617 int charpos;
4618 int precision, field_width, multibyte;
4619 {
4620 /* No region in strings. */
4621 it->region_beg_charpos = it->region_end_charpos = -1;
4622
4623 /* No text property checks performed by default, but see below. */
4624 it->stop_charpos = -1;
4625
4626 /* Set iterator position and end position. */
4627 bzero (&it->current, sizeof it->current);
4628 it->current.overlay_string_index = -1;
4629 it->current.dpvec_index = -1;
4630 xassert (charpos >= 0);
4631
4632 /* If STRING is specified, use its multibyteness, otherwise use the
4633 setting of MULTIBYTE, if specified. */
4634 if (multibyte >= 0)
4635 it->multibyte_p = multibyte > 0;
4636
4637 if (s == NULL)
4638 {
4639 xassert (STRINGP (string));
4640 it->string = string;
4641 it->s = NULL;
4642 it->end_charpos = it->string_nchars = SCHARS (string);
4643 it->method = next_element_from_string;
4644 it->current.string_pos = string_pos (charpos, string);
4645 }
4646 else
4647 {
4648 it->s = s;
4649 it->string = Qnil;
4650
4651 /* Note that we use IT->current.pos, not it->current.string_pos,
4652 for displaying C strings. */
4653 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4654 if (it->multibyte_p)
4655 {
4656 it->current.pos = c_string_pos (charpos, s, 1);
4657 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4658 }
4659 else
4660 {
4661 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4662 it->end_charpos = it->string_nchars = strlen (s);
4663 }
4664
4665 it->method = next_element_from_c_string;
4666 }
4667
4668 /* PRECISION > 0 means don't return more than PRECISION characters
4669 from the string. */
4670 if (precision > 0 && it->end_charpos - charpos > precision)
4671 it->end_charpos = it->string_nchars = charpos + precision;
4672
4673 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4674 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4675 FIELD_WIDTH < 0 means infinite field width. This is useful for
4676 padding with `-' at the end of a mode line. */
4677 if (field_width < 0)
4678 field_width = INFINITY;
4679 if (field_width > it->end_charpos - charpos)
4680 it->end_charpos = charpos + field_width;
4681
4682 /* Use the standard display table for displaying strings. */
4683 if (DISP_TABLE_P (Vstandard_display_table))
4684 it->dp = XCHAR_TABLE (Vstandard_display_table);
4685
4686 it->stop_charpos = charpos;
4687 CHECK_IT (it);
4688 }
4689
4690
4691 \f
4692 /***********************************************************************
4693 Iteration
4694 ***********************************************************************/
4695
4696 /* Load IT's display element fields with information about the next
4697 display element from the current position of IT. Value is zero if
4698 end of buffer (or C string) is reached. */
4699
4700 int
4701 get_next_display_element (it)
4702 struct it *it;
4703 {
4704 /* Non-zero means that we found a display element. Zero means that
4705 we hit the end of what we iterate over. Performance note: the
4706 function pointer `method' used here turns out to be faster than
4707 using a sequence of if-statements. */
4708 int success_p = (*it->method) (it);
4709
4710 if (it->what == IT_CHARACTER)
4711 {
4712 /* Map via display table or translate control characters.
4713 IT->c, IT->len etc. have been set to the next character by
4714 the function call above. If we have a display table, and it
4715 contains an entry for IT->c, translate it. Don't do this if
4716 IT->c itself comes from a display table, otherwise we could
4717 end up in an infinite recursion. (An alternative could be to
4718 count the recursion depth of this function and signal an
4719 error when a certain maximum depth is reached.) Is it worth
4720 it? */
4721 if (success_p && it->dpvec == NULL)
4722 {
4723 Lisp_Object dv;
4724
4725 if (it->dp
4726 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4727 VECTORP (dv)))
4728 {
4729 struct Lisp_Vector *v = XVECTOR (dv);
4730
4731 /* Return the first character from the display table
4732 entry, if not empty. If empty, don't display the
4733 current character. */
4734 if (v->size)
4735 {
4736 it->dpvec_char_len = it->len;
4737 it->dpvec = v->contents;
4738 it->dpend = v->contents + v->size;
4739 it->current.dpvec_index = 0;
4740 it->method = next_element_from_display_vector;
4741 success_p = get_next_display_element (it);
4742 }
4743 else
4744 {
4745 set_iterator_to_next (it, 0);
4746 success_p = get_next_display_element (it);
4747 }
4748 }
4749
4750 /* Translate control characters into `\003' or `^C' form.
4751 Control characters coming from a display table entry are
4752 currently not translated because we use IT->dpvec to hold
4753 the translation. This could easily be changed but I
4754 don't believe that it is worth doing.
4755
4756 If it->multibyte_p is nonzero, eight-bit characters and
4757 non-printable multibyte characters are also translated to
4758 octal form.
4759
4760 If it->multibyte_p is zero, eight-bit characters that
4761 don't have corresponding multibyte char code are also
4762 translated to octal form. */
4763 else if ((it->c < ' '
4764 && (it->area != TEXT_AREA
4765 || (it->c != '\n' && it->c != '\t')))
4766 || (it->multibyte_p
4767 ? ((it->c >= 127
4768 && it->len == 1)
4769 || !CHAR_PRINTABLE_P (it->c))
4770 : (it->c >= 127
4771 && it->c == unibyte_char_to_multibyte (it->c))))
4772 {
4773 /* IT->c is a control character which must be displayed
4774 either as '\003' or as `^C' where the '\\' and '^'
4775 can be defined in the display table. Fill
4776 IT->ctl_chars with glyphs for what we have to
4777 display. Then, set IT->dpvec to these glyphs. */
4778 GLYPH g;
4779
4780 if (it->c < 128 && it->ctl_arrow_p)
4781 {
4782 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4783 if (it->dp
4784 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4785 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4786 g = XINT (DISP_CTRL_GLYPH (it->dp));
4787 else
4788 g = FAST_MAKE_GLYPH ('^', 0);
4789 XSETINT (it->ctl_chars[0], g);
4790
4791 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4792 XSETINT (it->ctl_chars[1], g);
4793
4794 /* Set up IT->dpvec and return first character from it. */
4795 it->dpvec_char_len = it->len;
4796 it->dpvec = it->ctl_chars;
4797 it->dpend = it->dpvec + 2;
4798 it->current.dpvec_index = 0;
4799 it->method = next_element_from_display_vector;
4800 get_next_display_element (it);
4801 }
4802 else
4803 {
4804 unsigned char str[MAX_MULTIBYTE_LENGTH];
4805 int len;
4806 int i;
4807 GLYPH escape_glyph;
4808
4809 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4810 if (it->dp
4811 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4812 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4813 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4814 else
4815 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4816
4817 if (SINGLE_BYTE_CHAR_P (it->c))
4818 str[0] = it->c, len = 1;
4819 else
4820 {
4821 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4822 if (len < 0)
4823 {
4824 /* It's an invalid character, which
4825 shouldn't happen actually, but due to
4826 bugs it may happen. Let's print the char
4827 as is, there's not much meaningful we can
4828 do with it. */
4829 str[0] = it->c;
4830 str[1] = it->c >> 8;
4831 str[2] = it->c >> 16;
4832 str[3] = it->c >> 24;
4833 len = 4;
4834 }
4835 }
4836
4837 for (i = 0; i < len; i++)
4838 {
4839 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4840 /* Insert three more glyphs into IT->ctl_chars for
4841 the octal display of the character. */
4842 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4843 XSETINT (it->ctl_chars[i * 4 + 1], g);
4844 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4845 XSETINT (it->ctl_chars[i * 4 + 2], g);
4846 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4847 XSETINT (it->ctl_chars[i * 4 + 3], g);
4848 }
4849
4850 /* Set up IT->dpvec and return the first character
4851 from it. */
4852 it->dpvec_char_len = it->len;
4853 it->dpvec = it->ctl_chars;
4854 it->dpend = it->dpvec + len * 4;
4855 it->current.dpvec_index = 0;
4856 it->method = next_element_from_display_vector;
4857 get_next_display_element (it);
4858 }
4859 }
4860 }
4861
4862 /* Adjust face id for a multibyte character. There are no
4863 multibyte character in unibyte text. */
4864 if (it->multibyte_p
4865 && success_p
4866 && FRAME_WINDOW_P (it->f))
4867 {
4868 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4869 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4870 }
4871 }
4872
4873 /* Is this character the last one of a run of characters with
4874 box? If yes, set IT->end_of_box_run_p to 1. */
4875 if (it->face_box_p
4876 && it->s == NULL)
4877 {
4878 int face_id;
4879 struct face *face;
4880
4881 it->end_of_box_run_p
4882 = ((face_id = face_after_it_pos (it),
4883 face_id != it->face_id)
4884 && (face = FACE_FROM_ID (it->f, face_id),
4885 face->box == FACE_NO_BOX));
4886 }
4887
4888 /* Value is 0 if end of buffer or string reached. */
4889 return success_p;
4890 }
4891
4892
4893 /* Move IT to the next display element.
4894
4895 RESEAT_P non-zero means if called on a newline in buffer text,
4896 skip to the next visible line start.
4897
4898 Functions get_next_display_element and set_iterator_to_next are
4899 separate because I find this arrangement easier to handle than a
4900 get_next_display_element function that also increments IT's
4901 position. The way it is we can first look at an iterator's current
4902 display element, decide whether it fits on a line, and if it does,
4903 increment the iterator position. The other way around we probably
4904 would either need a flag indicating whether the iterator has to be
4905 incremented the next time, or we would have to implement a
4906 decrement position function which would not be easy to write. */
4907
4908 void
4909 set_iterator_to_next (it, reseat_p)
4910 struct it *it;
4911 int reseat_p;
4912 {
4913 /* Reset flags indicating start and end of a sequence of characters
4914 with box. Reset them at the start of this function because
4915 moving the iterator to a new position might set them. */
4916 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4917
4918 if (it->method == next_element_from_buffer)
4919 {
4920 /* The current display element of IT is a character from
4921 current_buffer. Advance in the buffer, and maybe skip over
4922 invisible lines that are so because of selective display. */
4923 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4924 reseat_at_next_visible_line_start (it, 0);
4925 else
4926 {
4927 xassert (it->len != 0);
4928 IT_BYTEPOS (*it) += it->len;
4929 IT_CHARPOS (*it) += 1;
4930 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4931 }
4932 }
4933 else if (it->method == next_element_from_composition)
4934 {
4935 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4936 if (STRINGP (it->string))
4937 {
4938 IT_STRING_BYTEPOS (*it) += it->len;
4939 IT_STRING_CHARPOS (*it) += it->cmp_len;
4940 it->method = next_element_from_string;
4941 goto consider_string_end;
4942 }
4943 else
4944 {
4945 IT_BYTEPOS (*it) += it->len;
4946 IT_CHARPOS (*it) += it->cmp_len;
4947 it->method = next_element_from_buffer;
4948 }
4949 }
4950 else if (it->method == next_element_from_c_string)
4951 {
4952 /* Current display element of IT is from a C string. */
4953 IT_BYTEPOS (*it) += it->len;
4954 IT_CHARPOS (*it) += 1;
4955 }
4956 else if (it->method == next_element_from_display_vector)
4957 {
4958 /* Current display element of IT is from a display table entry.
4959 Advance in the display table definition. Reset it to null if
4960 end reached, and continue with characters from buffers/
4961 strings. */
4962 ++it->current.dpvec_index;
4963
4964 /* Restore face of the iterator to what they were before the
4965 display vector entry (these entries may contain faces). */
4966 it->face_id = it->saved_face_id;
4967
4968 if (it->dpvec + it->current.dpvec_index == it->dpend)
4969 {
4970 if (it->s)
4971 it->method = next_element_from_c_string;
4972 else if (STRINGP (it->string))
4973 it->method = next_element_from_string;
4974 else
4975 it->method = next_element_from_buffer;
4976
4977 it->dpvec = NULL;
4978 it->current.dpvec_index = -1;
4979
4980 /* Skip over characters which were displayed via IT->dpvec. */
4981 if (it->dpvec_char_len < 0)
4982 reseat_at_next_visible_line_start (it, 1);
4983 else if (it->dpvec_char_len > 0)
4984 {
4985 it->len = it->dpvec_char_len;
4986 set_iterator_to_next (it, reseat_p);
4987 }
4988 }
4989 }
4990 else if (it->method == next_element_from_string)
4991 {
4992 /* Current display element is a character from a Lisp string. */
4993 xassert (it->s == NULL && STRINGP (it->string));
4994 IT_STRING_BYTEPOS (*it) += it->len;
4995 IT_STRING_CHARPOS (*it) += 1;
4996
4997 consider_string_end:
4998
4999 if (it->current.overlay_string_index >= 0)
5000 {
5001 /* IT->string is an overlay string. Advance to the
5002 next, if there is one. */
5003 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5004 next_overlay_string (it);
5005 }
5006 else
5007 {
5008 /* IT->string is not an overlay string. If we reached
5009 its end, and there is something on IT->stack, proceed
5010 with what is on the stack. This can be either another
5011 string, this time an overlay string, or a buffer. */
5012 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5013 && it->sp > 0)
5014 {
5015 pop_it (it);
5016 if (!STRINGP (it->string))
5017 it->method = next_element_from_buffer;
5018 else
5019 goto consider_string_end;
5020 }
5021 }
5022 }
5023 else if (it->method == next_element_from_image
5024 || it->method == next_element_from_stretch)
5025 {
5026 /* The position etc with which we have to proceed are on
5027 the stack. The position may be at the end of a string,
5028 if the `display' property takes up the whole string. */
5029 pop_it (it);
5030 it->image_id = 0;
5031 if (STRINGP (it->string))
5032 {
5033 it->method = next_element_from_string;
5034 goto consider_string_end;
5035 }
5036 else
5037 it->method = next_element_from_buffer;
5038 }
5039 else
5040 /* There are no other methods defined, so this should be a bug. */
5041 abort ();
5042
5043 xassert (it->method != next_element_from_string
5044 || (STRINGP (it->string)
5045 && IT_STRING_CHARPOS (*it) >= 0));
5046 }
5047
5048
5049 /* Load IT's display element fields with information about the next
5050 display element which comes from a display table entry or from the
5051 result of translating a control character to one of the forms `^C'
5052 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5053
5054 static int
5055 next_element_from_display_vector (it)
5056 struct it *it;
5057 {
5058 /* Precondition. */
5059 xassert (it->dpvec && it->current.dpvec_index >= 0);
5060
5061 /* Remember the current face id in case glyphs specify faces.
5062 IT's face is restored in set_iterator_to_next. */
5063 it->saved_face_id = it->face_id;
5064
5065 if (INTEGERP (*it->dpvec)
5066 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5067 {
5068 int lface_id;
5069 GLYPH g;
5070
5071 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5072 it->c = FAST_GLYPH_CHAR (g);
5073 it->len = CHAR_BYTES (it->c);
5074
5075 /* The entry may contain a face id to use. Such a face id is
5076 the id of a Lisp face, not a realized face. A face id of
5077 zero means no face is specified. */
5078 lface_id = FAST_GLYPH_FACE (g);
5079 if (lface_id)
5080 {
5081 /* The function returns -1 if lface_id is invalid. */
5082 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5083 if (face_id >= 0)
5084 it->face_id = face_id;
5085 }
5086 }
5087 else
5088 /* Display table entry is invalid. Return a space. */
5089 it->c = ' ', it->len = 1;
5090
5091 /* Don't change position and object of the iterator here. They are
5092 still the values of the character that had this display table
5093 entry or was translated, and that's what we want. */
5094 it->what = IT_CHARACTER;
5095 return 1;
5096 }
5097
5098
5099 /* Load IT with the next display element from Lisp string IT->string.
5100 IT->current.string_pos is the current position within the string.
5101 If IT->current.overlay_string_index >= 0, the Lisp string is an
5102 overlay string. */
5103
5104 static int
5105 next_element_from_string (it)
5106 struct it *it;
5107 {
5108 struct text_pos position;
5109
5110 xassert (STRINGP (it->string));
5111 xassert (IT_STRING_CHARPOS (*it) >= 0);
5112 position = it->current.string_pos;
5113
5114 /* Time to check for invisible text? */
5115 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5116 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5117 {
5118 handle_stop (it);
5119
5120 /* Since a handler may have changed IT->method, we must
5121 recurse here. */
5122 return get_next_display_element (it);
5123 }
5124
5125 if (it->current.overlay_string_index >= 0)
5126 {
5127 /* Get the next character from an overlay string. In overlay
5128 strings, There is no field width or padding with spaces to
5129 do. */
5130 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5131 {
5132 it->what = IT_EOB;
5133 return 0;
5134 }
5135 else if (STRING_MULTIBYTE (it->string))
5136 {
5137 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5138 const unsigned char *s = (SDATA (it->string)
5139 + IT_STRING_BYTEPOS (*it));
5140 it->c = string_char_and_length (s, remaining, &it->len);
5141 }
5142 else
5143 {
5144 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5145 it->len = 1;
5146 }
5147 }
5148 else
5149 {
5150 /* Get the next character from a Lisp string that is not an
5151 overlay string. Such strings come from the mode line, for
5152 example. We may have to pad with spaces, or truncate the
5153 string. See also next_element_from_c_string. */
5154 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5155 {
5156 it->what = IT_EOB;
5157 return 0;
5158 }
5159 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5160 {
5161 /* Pad with spaces. */
5162 it->c = ' ', it->len = 1;
5163 CHARPOS (position) = BYTEPOS (position) = -1;
5164 }
5165 else if (STRING_MULTIBYTE (it->string))
5166 {
5167 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5168 const unsigned char *s = (SDATA (it->string)
5169 + IT_STRING_BYTEPOS (*it));
5170 it->c = string_char_and_length (s, maxlen, &it->len);
5171 }
5172 else
5173 {
5174 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5175 it->len = 1;
5176 }
5177 }
5178
5179 /* Record what we have and where it came from. Note that we store a
5180 buffer position in IT->position although it could arguably be a
5181 string position. */
5182 it->what = IT_CHARACTER;
5183 it->object = it->string;
5184 it->position = position;
5185 return 1;
5186 }
5187
5188
5189 /* Load IT with next display element from C string IT->s.
5190 IT->string_nchars is the maximum number of characters to return
5191 from the string. IT->end_charpos may be greater than
5192 IT->string_nchars when this function is called, in which case we
5193 may have to return padding spaces. Value is zero if end of string
5194 reached, including padding spaces. */
5195
5196 static int
5197 next_element_from_c_string (it)
5198 struct it *it;
5199 {
5200 int success_p = 1;
5201
5202 xassert (it->s);
5203 it->what = IT_CHARACTER;
5204 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5205 it->object = Qnil;
5206
5207 /* IT's position can be greater IT->string_nchars in case a field
5208 width or precision has been specified when the iterator was
5209 initialized. */
5210 if (IT_CHARPOS (*it) >= it->end_charpos)
5211 {
5212 /* End of the game. */
5213 it->what = IT_EOB;
5214 success_p = 0;
5215 }
5216 else if (IT_CHARPOS (*it) >= it->string_nchars)
5217 {
5218 /* Pad with spaces. */
5219 it->c = ' ', it->len = 1;
5220 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5221 }
5222 else if (it->multibyte_p)
5223 {
5224 /* Implementation note: The calls to strlen apparently aren't a
5225 performance problem because there is no noticeable performance
5226 difference between Emacs running in unibyte or multibyte mode. */
5227 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5228 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5229 maxlen, &it->len);
5230 }
5231 else
5232 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5233
5234 return success_p;
5235 }
5236
5237
5238 /* Set up IT to return characters from an ellipsis, if appropriate.
5239 The definition of the ellipsis glyphs may come from a display table
5240 entry. This function Fills IT with the first glyph from the
5241 ellipsis if an ellipsis is to be displayed. */
5242
5243 static int
5244 next_element_from_ellipsis (it)
5245 struct it *it;
5246 {
5247 if (it->selective_display_ellipsis_p)
5248 {
5249 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5250 {
5251 /* Use the display table definition for `...'. Invalid glyphs
5252 will be handled by the method returning elements from dpvec. */
5253 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5254 it->dpvec_char_len = it->len;
5255 it->dpvec = v->contents;
5256 it->dpend = v->contents + v->size;
5257 it->current.dpvec_index = 0;
5258 it->method = next_element_from_display_vector;
5259 }
5260 else
5261 {
5262 /* Use default `...' which is stored in default_invis_vector. */
5263 it->dpvec_char_len = it->len;
5264 it->dpvec = default_invis_vector;
5265 it->dpend = default_invis_vector + 3;
5266 it->current.dpvec_index = 0;
5267 it->method = next_element_from_display_vector;
5268 }
5269 }
5270 else
5271 {
5272 /* The face at the current position may be different from the
5273 face we find after the invisible text. Remember what it
5274 was in IT->saved_face_id, and signal that it's there by
5275 setting face_before_selective_p. */
5276 it->saved_face_id = it->face_id;
5277 it->method = next_element_from_buffer;
5278 reseat_at_next_visible_line_start (it, 1);
5279 it->face_before_selective_p = 1;
5280 }
5281
5282 return get_next_display_element (it);
5283 }
5284
5285
5286 /* Deliver an image display element. The iterator IT is already
5287 filled with image information (done in handle_display_prop). Value
5288 is always 1. */
5289
5290
5291 static int
5292 next_element_from_image (it)
5293 struct it *it;
5294 {
5295 it->what = IT_IMAGE;
5296 return 1;
5297 }
5298
5299
5300 /* Fill iterator IT with next display element from a stretch glyph
5301 property. IT->object is the value of the text property. Value is
5302 always 1. */
5303
5304 static int
5305 next_element_from_stretch (it)
5306 struct it *it;
5307 {
5308 it->what = IT_STRETCH;
5309 return 1;
5310 }
5311
5312
5313 /* Load IT with the next display element from current_buffer. Value
5314 is zero if end of buffer reached. IT->stop_charpos is the next
5315 position at which to stop and check for text properties or buffer
5316 end. */
5317
5318 static int
5319 next_element_from_buffer (it)
5320 struct it *it;
5321 {
5322 int success_p = 1;
5323
5324 /* Check this assumption, otherwise, we would never enter the
5325 if-statement, below. */
5326 xassert (IT_CHARPOS (*it) >= BEGV
5327 && IT_CHARPOS (*it) <= it->stop_charpos);
5328
5329 if (IT_CHARPOS (*it) >= it->stop_charpos)
5330 {
5331 if (IT_CHARPOS (*it) >= it->end_charpos)
5332 {
5333 int overlay_strings_follow_p;
5334
5335 /* End of the game, except when overlay strings follow that
5336 haven't been returned yet. */
5337 if (it->overlay_strings_at_end_processed_p)
5338 overlay_strings_follow_p = 0;
5339 else
5340 {
5341 it->overlay_strings_at_end_processed_p = 1;
5342 overlay_strings_follow_p = get_overlay_strings (it, 0);
5343 }
5344
5345 if (overlay_strings_follow_p)
5346 success_p = get_next_display_element (it);
5347 else
5348 {
5349 it->what = IT_EOB;
5350 it->position = it->current.pos;
5351 success_p = 0;
5352 }
5353 }
5354 else
5355 {
5356 handle_stop (it);
5357 return get_next_display_element (it);
5358 }
5359 }
5360 else
5361 {
5362 /* No face changes, overlays etc. in sight, so just return a
5363 character from current_buffer. */
5364 unsigned char *p;
5365
5366 /* Maybe run the redisplay end trigger hook. Performance note:
5367 This doesn't seem to cost measurable time. */
5368 if (it->redisplay_end_trigger_charpos
5369 && it->glyph_row
5370 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5371 run_redisplay_end_trigger_hook (it);
5372
5373 /* Get the next character, maybe multibyte. */
5374 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5375 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5376 {
5377 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5378 - IT_BYTEPOS (*it));
5379 it->c = string_char_and_length (p, maxlen, &it->len);
5380 }
5381 else
5382 it->c = *p, it->len = 1;
5383
5384 /* Record what we have and where it came from. */
5385 it->what = IT_CHARACTER;;
5386 it->object = it->w->buffer;
5387 it->position = it->current.pos;
5388
5389 /* Normally we return the character found above, except when we
5390 really want to return an ellipsis for selective display. */
5391 if (it->selective)
5392 {
5393 if (it->c == '\n')
5394 {
5395 /* A value of selective > 0 means hide lines indented more
5396 than that number of columns. */
5397 if (it->selective > 0
5398 && IT_CHARPOS (*it) + 1 < ZV
5399 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5400 IT_BYTEPOS (*it) + 1,
5401 (double) it->selective)) /* iftc */
5402 {
5403 success_p = next_element_from_ellipsis (it);
5404 it->dpvec_char_len = -1;
5405 }
5406 }
5407 else if (it->c == '\r' && it->selective == -1)
5408 {
5409 /* A value of selective == -1 means that everything from the
5410 CR to the end of the line is invisible, with maybe an
5411 ellipsis displayed for it. */
5412 success_p = next_element_from_ellipsis (it);
5413 it->dpvec_char_len = -1;
5414 }
5415 }
5416 }
5417
5418 /* Value is zero if end of buffer reached. */
5419 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5420 return success_p;
5421 }
5422
5423
5424 /* Run the redisplay end trigger hook for IT. */
5425
5426 static void
5427 run_redisplay_end_trigger_hook (it)
5428 struct it *it;
5429 {
5430 Lisp_Object args[3];
5431
5432 /* IT->glyph_row should be non-null, i.e. we should be actually
5433 displaying something, or otherwise we should not run the hook. */
5434 xassert (it->glyph_row);
5435
5436 /* Set up hook arguments. */
5437 args[0] = Qredisplay_end_trigger_functions;
5438 args[1] = it->window;
5439 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5440 it->redisplay_end_trigger_charpos = 0;
5441
5442 /* Since we are *trying* to run these functions, don't try to run
5443 them again, even if they get an error. */
5444 it->w->redisplay_end_trigger = Qnil;
5445 Frun_hook_with_args (3, args);
5446
5447 /* Notice if it changed the face of the character we are on. */
5448 handle_face_prop (it);
5449 }
5450
5451
5452 /* Deliver a composition display element. The iterator IT is already
5453 filled with composition information (done in
5454 handle_composition_prop). Value is always 1. */
5455
5456 static int
5457 next_element_from_composition (it)
5458 struct it *it;
5459 {
5460 it->what = IT_COMPOSITION;
5461 it->position = (STRINGP (it->string)
5462 ? it->current.string_pos
5463 : it->current.pos);
5464 return 1;
5465 }
5466
5467
5468 \f
5469 /***********************************************************************
5470 Moving an iterator without producing glyphs
5471 ***********************************************************************/
5472
5473 /* Move iterator IT to a specified buffer or X position within one
5474 line on the display without producing glyphs.
5475
5476 OP should be a bit mask including some or all of these bits:
5477 MOVE_TO_X: Stop on reaching x-position TO_X.
5478 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5479 Regardless of OP's value, stop in reaching the end of the display line.
5480
5481 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5482 This means, in particular, that TO_X includes window's horizontal
5483 scroll amount.
5484
5485 The return value has several possible values that
5486 say what condition caused the scan to stop:
5487
5488 MOVE_POS_MATCH_OR_ZV
5489 - when TO_POS or ZV was reached.
5490
5491 MOVE_X_REACHED
5492 -when TO_X was reached before TO_POS or ZV were reached.
5493
5494 MOVE_LINE_CONTINUED
5495 - when we reached the end of the display area and the line must
5496 be continued.
5497
5498 MOVE_LINE_TRUNCATED
5499 - when we reached the end of the display area and the line is
5500 truncated.
5501
5502 MOVE_NEWLINE_OR_CR
5503 - when we stopped at a line end, i.e. a newline or a CR and selective
5504 display is on. */
5505
5506 static enum move_it_result
5507 move_it_in_display_line_to (it, to_charpos, to_x, op)
5508 struct it *it;
5509 int to_charpos, to_x, op;
5510 {
5511 enum move_it_result result = MOVE_UNDEFINED;
5512 struct glyph_row *saved_glyph_row;
5513
5514 /* Don't produce glyphs in produce_glyphs. */
5515 saved_glyph_row = it->glyph_row;
5516 it->glyph_row = NULL;
5517
5518 while (1)
5519 {
5520 int x, i, ascent = 0, descent = 0;
5521
5522 /* Stop when ZV or TO_CHARPOS reached. */
5523 if (!get_next_display_element (it)
5524 || ((op & MOVE_TO_POS) != 0
5525 && BUFFERP (it->object)
5526 && IT_CHARPOS (*it) >= to_charpos))
5527 {
5528 result = MOVE_POS_MATCH_OR_ZV;
5529 break;
5530 }
5531
5532 /* The call to produce_glyphs will get the metrics of the
5533 display element IT is loaded with. We record in x the
5534 x-position before this display element in case it does not
5535 fit on the line. */
5536 x = it->current_x;
5537
5538 /* Remember the line height so far in case the next element doesn't
5539 fit on the line. */
5540 if (!it->truncate_lines_p)
5541 {
5542 ascent = it->max_ascent;
5543 descent = it->max_descent;
5544 }
5545
5546 PRODUCE_GLYPHS (it);
5547
5548 if (it->area != TEXT_AREA)
5549 {
5550 set_iterator_to_next (it, 1);
5551 continue;
5552 }
5553
5554 /* The number of glyphs we get back in IT->nglyphs will normally
5555 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5556 character on a terminal frame, or (iii) a line end. For the
5557 second case, IT->nglyphs - 1 padding glyphs will be present
5558 (on X frames, there is only one glyph produced for a
5559 composite character.
5560
5561 The behavior implemented below means, for continuation lines,
5562 that as many spaces of a TAB as fit on the current line are
5563 displayed there. For terminal frames, as many glyphs of a
5564 multi-glyph character are displayed in the current line, too.
5565 This is what the old redisplay code did, and we keep it that
5566 way. Under X, the whole shape of a complex character must
5567 fit on the line or it will be completely displayed in the
5568 next line.
5569
5570 Note that both for tabs and padding glyphs, all glyphs have
5571 the same width. */
5572 if (it->nglyphs)
5573 {
5574 /* More than one glyph or glyph doesn't fit on line. All
5575 glyphs have the same width. */
5576 int single_glyph_width = it->pixel_width / it->nglyphs;
5577 int new_x;
5578
5579 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5580 {
5581 new_x = x + single_glyph_width;
5582
5583 /* We want to leave anything reaching TO_X to the caller. */
5584 if ((op & MOVE_TO_X) && new_x > to_x)
5585 {
5586 it->current_x = x;
5587 result = MOVE_X_REACHED;
5588 break;
5589 }
5590 else if (/* Lines are continued. */
5591 !it->truncate_lines_p
5592 && (/* And glyph doesn't fit on the line. */
5593 new_x > it->last_visible_x
5594 /* Or it fits exactly and we're on a window
5595 system frame. */
5596 || (new_x == it->last_visible_x
5597 && FRAME_WINDOW_P (it->f))))
5598 {
5599 if (/* IT->hpos == 0 means the very first glyph
5600 doesn't fit on the line, e.g. a wide image. */
5601 it->hpos == 0
5602 || (new_x == it->last_visible_x
5603 && FRAME_WINDOW_P (it->f)))
5604 {
5605 ++it->hpos;
5606 it->current_x = new_x;
5607 if (i == it->nglyphs - 1)
5608 {
5609 set_iterator_to_next (it, 1);
5610 #ifdef HAVE_WINDOW_SYSTEM
5611 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5612 {
5613 get_next_display_element (it);
5614 if (ITERATOR_AT_END_OF_LINE_P (it))
5615 {
5616 result = MOVE_NEWLINE_OR_CR;
5617 break;
5618 }
5619 }
5620 #endif /* HAVE_WINDOW_SYSTEM */
5621 }
5622 }
5623 else
5624 {
5625 it->current_x = x;
5626 it->max_ascent = ascent;
5627 it->max_descent = descent;
5628 }
5629
5630 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5631 IT_CHARPOS (*it)));
5632 result = MOVE_LINE_CONTINUED;
5633 break;
5634 }
5635 else if (new_x > it->first_visible_x)
5636 {
5637 /* Glyph is visible. Increment number of glyphs that
5638 would be displayed. */
5639 ++it->hpos;
5640 }
5641 else
5642 {
5643 /* Glyph is completely off the left margin of the display
5644 area. Nothing to do. */
5645 }
5646 }
5647
5648 if (result != MOVE_UNDEFINED)
5649 break;
5650 }
5651 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5652 {
5653 /* Stop when TO_X specified and reached. This check is
5654 necessary here because of lines consisting of a line end,
5655 only. The line end will not produce any glyphs and we
5656 would never get MOVE_X_REACHED. */
5657 xassert (it->nglyphs == 0);
5658 result = MOVE_X_REACHED;
5659 break;
5660 }
5661
5662 /* Is this a line end? If yes, we're done. */
5663 if (ITERATOR_AT_END_OF_LINE_P (it))
5664 {
5665 result = MOVE_NEWLINE_OR_CR;
5666 break;
5667 }
5668
5669 /* The current display element has been consumed. Advance
5670 to the next. */
5671 set_iterator_to_next (it, 1);
5672
5673 /* Stop if lines are truncated and IT's current x-position is
5674 past the right edge of the window now. */
5675 if (it->truncate_lines_p
5676 && it->current_x >= it->last_visible_x)
5677 {
5678 #ifdef HAVE_WINDOW_SYSTEM
5679 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5680 {
5681 get_next_display_element (it);
5682 if (ITERATOR_AT_END_OF_LINE_P (it))
5683 {
5684 result = MOVE_NEWLINE_OR_CR;
5685 break;
5686 }
5687 }
5688 #endif /* HAVE_WINDOW_SYSTEM */
5689 result = MOVE_LINE_TRUNCATED;
5690 break;
5691 }
5692 }
5693
5694 /* Restore the iterator settings altered at the beginning of this
5695 function. */
5696 it->glyph_row = saved_glyph_row;
5697 return result;
5698 }
5699
5700
5701 /* Move IT forward until it satisfies one or more of the criteria in
5702 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5703
5704 OP is a bit-mask that specifies where to stop, and in particular,
5705 which of those four position arguments makes a difference. See the
5706 description of enum move_operation_enum.
5707
5708 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5709 screen line, this function will set IT to the next position >
5710 TO_CHARPOS. */
5711
5712 void
5713 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5714 struct it *it;
5715 int to_charpos, to_x, to_y, to_vpos;
5716 int op;
5717 {
5718 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5719 int line_height;
5720 int reached = 0;
5721
5722 for (;;)
5723 {
5724 if (op & MOVE_TO_VPOS)
5725 {
5726 /* If no TO_CHARPOS and no TO_X specified, stop at the
5727 start of the line TO_VPOS. */
5728 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5729 {
5730 if (it->vpos == to_vpos)
5731 {
5732 reached = 1;
5733 break;
5734 }
5735 else
5736 skip = move_it_in_display_line_to (it, -1, -1, 0);
5737 }
5738 else
5739 {
5740 /* TO_VPOS >= 0 means stop at TO_X in the line at
5741 TO_VPOS, or at TO_POS, whichever comes first. */
5742 if (it->vpos == to_vpos)
5743 {
5744 reached = 2;
5745 break;
5746 }
5747
5748 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5749
5750 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5751 {
5752 reached = 3;
5753 break;
5754 }
5755 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5756 {
5757 /* We have reached TO_X but not in the line we want. */
5758 skip = move_it_in_display_line_to (it, to_charpos,
5759 -1, MOVE_TO_POS);
5760 if (skip == MOVE_POS_MATCH_OR_ZV)
5761 {
5762 reached = 4;
5763 break;
5764 }
5765 }
5766 }
5767 }
5768 else if (op & MOVE_TO_Y)
5769 {
5770 struct it it_backup;
5771
5772 /* TO_Y specified means stop at TO_X in the line containing
5773 TO_Y---or at TO_CHARPOS if this is reached first. The
5774 problem is that we can't really tell whether the line
5775 contains TO_Y before we have completely scanned it, and
5776 this may skip past TO_X. What we do is to first scan to
5777 TO_X.
5778
5779 If TO_X is not specified, use a TO_X of zero. The reason
5780 is to make the outcome of this function more predictable.
5781 If we didn't use TO_X == 0, we would stop at the end of
5782 the line which is probably not what a caller would expect
5783 to happen. */
5784 skip = move_it_in_display_line_to (it, to_charpos,
5785 ((op & MOVE_TO_X)
5786 ? to_x : 0),
5787 (MOVE_TO_X
5788 | (op & MOVE_TO_POS)));
5789
5790 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5791 if (skip == MOVE_POS_MATCH_OR_ZV)
5792 {
5793 reached = 5;
5794 break;
5795 }
5796
5797 /* If TO_X was reached, we would like to know whether TO_Y
5798 is in the line. This can only be said if we know the
5799 total line height which requires us to scan the rest of
5800 the line. */
5801 if (skip == MOVE_X_REACHED)
5802 {
5803 it_backup = *it;
5804 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5805 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5806 op & MOVE_TO_POS);
5807 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5808 }
5809
5810 /* Now, decide whether TO_Y is in this line. */
5811 line_height = it->max_ascent + it->max_descent;
5812 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5813
5814 if (to_y >= it->current_y
5815 && to_y < it->current_y + line_height)
5816 {
5817 if (skip == MOVE_X_REACHED)
5818 /* If TO_Y is in this line and TO_X was reached above,
5819 we scanned too far. We have to restore IT's settings
5820 to the ones before skipping. */
5821 *it = it_backup;
5822 reached = 6;
5823 }
5824 else if (skip == MOVE_X_REACHED)
5825 {
5826 skip = skip2;
5827 if (skip == MOVE_POS_MATCH_OR_ZV)
5828 reached = 7;
5829 }
5830
5831 if (reached)
5832 break;
5833 }
5834 else
5835 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5836
5837 switch (skip)
5838 {
5839 case MOVE_POS_MATCH_OR_ZV:
5840 reached = 8;
5841 goto out;
5842
5843 case MOVE_NEWLINE_OR_CR:
5844 set_iterator_to_next (it, 1);
5845 it->continuation_lines_width = 0;
5846 break;
5847
5848 case MOVE_LINE_TRUNCATED:
5849 it->continuation_lines_width = 0;
5850 reseat_at_next_visible_line_start (it, 0);
5851 if ((op & MOVE_TO_POS) != 0
5852 && IT_CHARPOS (*it) > to_charpos)
5853 {
5854 reached = 9;
5855 goto out;
5856 }
5857 break;
5858
5859 case MOVE_LINE_CONTINUED:
5860 it->continuation_lines_width += it->current_x;
5861 break;
5862
5863 default:
5864 abort ();
5865 }
5866
5867 /* Reset/increment for the next run. */
5868 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5869 it->current_x = it->hpos = 0;
5870 it->current_y += it->max_ascent + it->max_descent;
5871 ++it->vpos;
5872 last_height = it->max_ascent + it->max_descent;
5873 last_max_ascent = it->max_ascent;
5874 it->max_ascent = it->max_descent = 0;
5875 }
5876
5877 out:
5878
5879 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5880 }
5881
5882
5883 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5884
5885 If DY > 0, move IT backward at least that many pixels. DY = 0
5886 means move IT backward to the preceding line start or BEGV. This
5887 function may move over more than DY pixels if IT->current_y - DY
5888 ends up in the middle of a line; in this case IT->current_y will be
5889 set to the top of the line moved to. */
5890
5891 void
5892 move_it_vertically_backward (it, dy)
5893 struct it *it;
5894 int dy;
5895 {
5896 int nlines, h;
5897 struct it it2, it3;
5898 int start_pos = IT_CHARPOS (*it);
5899
5900 xassert (dy >= 0);
5901
5902 /* Estimate how many newlines we must move back. */
5903 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5904
5905 /* Set the iterator's position that many lines back. */
5906 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5907 back_to_previous_visible_line_start (it);
5908
5909 /* Reseat the iterator here. When moving backward, we don't want
5910 reseat to skip forward over invisible text, set up the iterator
5911 to deliver from overlay strings at the new position etc. So,
5912 use reseat_1 here. */
5913 reseat_1 (it, it->current.pos, 1);
5914
5915 /* We are now surely at a line start. */
5916 it->current_x = it->hpos = 0;
5917 it->continuation_lines_width = 0;
5918
5919 /* Move forward and see what y-distance we moved. First move to the
5920 start of the next line so that we get its height. We need this
5921 height to be able to tell whether we reached the specified
5922 y-distance. */
5923 it2 = *it;
5924 it2.max_ascent = it2.max_descent = 0;
5925 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5926 MOVE_TO_POS | MOVE_TO_VPOS);
5927 xassert (IT_CHARPOS (*it) >= BEGV);
5928 it3 = it2;
5929
5930 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5931 xassert (IT_CHARPOS (*it) >= BEGV);
5932 /* H is the actual vertical distance from the position in *IT
5933 and the starting position. */
5934 h = it2.current_y - it->current_y;
5935 /* NLINES is the distance in number of lines. */
5936 nlines = it2.vpos - it->vpos;
5937
5938 /* Correct IT's y and vpos position
5939 so that they are relative to the starting point. */
5940 it->vpos -= nlines;
5941 it->current_y -= h;
5942
5943 if (dy == 0)
5944 {
5945 /* DY == 0 means move to the start of the screen line. The
5946 value of nlines is > 0 if continuation lines were involved. */
5947 if (nlines > 0)
5948 move_it_by_lines (it, nlines, 1);
5949 xassert (IT_CHARPOS (*it) <= start_pos);
5950 }
5951 else
5952 {
5953 /* The y-position we try to reach, relative to *IT.
5954 Note that H has been subtracted in front of the if-statement. */
5955 int target_y = it->current_y + h - dy;
5956 int y0 = it3.current_y;
5957 int y1 = line_bottom_y (&it3);
5958 int line_height = y1 - y0;
5959
5960 /* If we did not reach target_y, try to move further backward if
5961 we can. If we moved too far backward, try to move forward. */
5962 if (target_y < it->current_y
5963 /* This is heuristic. In a window that's 3 lines high, with
5964 a line height of 13 pixels each, recentering with point
5965 on the bottom line will try to move -39/2 = 19 pixels
5966 backward. Try to avoid moving into the first line. */
5967 && it->current_y - target_y > line_height / 3 * 2
5968 && IT_CHARPOS (*it) > BEGV)
5969 {
5970 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5971 target_y - it->current_y));
5972 move_it_vertically (it, target_y - it->current_y);
5973 xassert (IT_CHARPOS (*it) >= BEGV);
5974 }
5975 else if (target_y >= it->current_y + line_height
5976 && IT_CHARPOS (*it) < ZV)
5977 {
5978 /* Should move forward by at least one line, maybe more.
5979
5980 Note: Calling move_it_by_lines can be expensive on
5981 terminal frames, where compute_motion is used (via
5982 vmotion) to do the job, when there are very long lines
5983 and truncate-lines is nil. That's the reason for
5984 treating terminal frames specially here. */
5985
5986 if (!FRAME_WINDOW_P (it->f))
5987 move_it_vertically (it, target_y - (it->current_y + line_height));
5988 else
5989 {
5990 do
5991 {
5992 move_it_by_lines (it, 1, 1);
5993 }
5994 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5995 }
5996
5997 xassert (IT_CHARPOS (*it) >= BEGV);
5998 }
5999 }
6000 }
6001
6002
6003 /* Move IT by a specified amount of pixel lines DY. DY negative means
6004 move backwards. DY = 0 means move to start of screen line. At the
6005 end, IT will be on the start of a screen line. */
6006
6007 void
6008 move_it_vertically (it, dy)
6009 struct it *it;
6010 int dy;
6011 {
6012 if (dy <= 0)
6013 move_it_vertically_backward (it, -dy);
6014 else if (dy > 0)
6015 {
6016 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6017 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6018 MOVE_TO_POS | MOVE_TO_Y);
6019 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6020
6021 /* If buffer ends in ZV without a newline, move to the start of
6022 the line to satisfy the post-condition. */
6023 if (IT_CHARPOS (*it) == ZV
6024 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6025 move_it_by_lines (it, 0, 0);
6026 }
6027 }
6028
6029
6030 /* Move iterator IT past the end of the text line it is in. */
6031
6032 void
6033 move_it_past_eol (it)
6034 struct it *it;
6035 {
6036 enum move_it_result rc;
6037
6038 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6039 if (rc == MOVE_NEWLINE_OR_CR)
6040 set_iterator_to_next (it, 0);
6041 }
6042
6043
6044 #if 0 /* Currently not used. */
6045
6046 /* Return non-zero if some text between buffer positions START_CHARPOS
6047 and END_CHARPOS is invisible. IT->window is the window for text
6048 property lookup. */
6049
6050 static int
6051 invisible_text_between_p (it, start_charpos, end_charpos)
6052 struct it *it;
6053 int start_charpos, end_charpos;
6054 {
6055 Lisp_Object prop, limit;
6056 int invisible_found_p;
6057
6058 xassert (it != NULL && start_charpos <= end_charpos);
6059
6060 /* Is text at START invisible? */
6061 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6062 it->window);
6063 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6064 invisible_found_p = 1;
6065 else
6066 {
6067 limit = Fnext_single_char_property_change (make_number (start_charpos),
6068 Qinvisible, Qnil,
6069 make_number (end_charpos));
6070 invisible_found_p = XFASTINT (limit) < end_charpos;
6071 }
6072
6073 return invisible_found_p;
6074 }
6075
6076 #endif /* 0 */
6077
6078
6079 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6080 negative means move up. DVPOS == 0 means move to the start of the
6081 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6082 NEED_Y_P is zero, IT->current_y will be left unchanged.
6083
6084 Further optimization ideas: If we would know that IT->f doesn't use
6085 a face with proportional font, we could be faster for
6086 truncate-lines nil. */
6087
6088 void
6089 move_it_by_lines (it, dvpos, need_y_p)
6090 struct it *it;
6091 int dvpos, need_y_p;
6092 {
6093 struct position pos;
6094
6095 if (!FRAME_WINDOW_P (it->f))
6096 {
6097 struct text_pos textpos;
6098
6099 /* We can use vmotion on frames without proportional fonts. */
6100 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6101 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6102 reseat (it, textpos, 1);
6103 it->vpos += pos.vpos;
6104 it->current_y += pos.vpos;
6105 }
6106 else if (dvpos == 0)
6107 {
6108 /* DVPOS == 0 means move to the start of the screen line. */
6109 move_it_vertically_backward (it, 0);
6110 xassert (it->current_x == 0 && it->hpos == 0);
6111 }
6112 else if (dvpos > 0)
6113 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6114 else
6115 {
6116 struct it it2;
6117 int start_charpos, i;
6118
6119 /* Start at the beginning of the screen line containing IT's
6120 position. */
6121 move_it_vertically_backward (it, 0);
6122
6123 /* Go back -DVPOS visible lines and reseat the iterator there. */
6124 start_charpos = IT_CHARPOS (*it);
6125 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6126 back_to_previous_visible_line_start (it);
6127 reseat (it, it->current.pos, 1);
6128 it->current_x = it->hpos = 0;
6129
6130 /* Above call may have moved too far if continuation lines
6131 are involved. Scan forward and see if it did. */
6132 it2 = *it;
6133 it2.vpos = it2.current_y = 0;
6134 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6135 it->vpos -= it2.vpos;
6136 it->current_y -= it2.current_y;
6137 it->current_x = it->hpos = 0;
6138
6139 /* If we moved too far, move IT some lines forward. */
6140 if (it2.vpos > -dvpos)
6141 {
6142 int delta = it2.vpos + dvpos;
6143 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6144 }
6145 }
6146 }
6147
6148 /* Return 1 if IT points into the middle of a display vector. */
6149
6150 int
6151 in_display_vector_p (it)
6152 struct it *it;
6153 {
6154 return (it->method == next_element_from_display_vector
6155 && it->current.dpvec_index > 0
6156 && it->dpvec + it->current.dpvec_index != it->dpend);
6157 }
6158
6159 \f
6160 /***********************************************************************
6161 Messages
6162 ***********************************************************************/
6163
6164
6165 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6166 to *Messages*. */
6167
6168 void
6169 add_to_log (format, arg1, arg2)
6170 char *format;
6171 Lisp_Object arg1, arg2;
6172 {
6173 Lisp_Object args[3];
6174 Lisp_Object msg, fmt;
6175 char *buffer;
6176 int len;
6177 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6178
6179 /* Do nothing if called asynchronously. Inserting text into
6180 a buffer may call after-change-functions and alike and
6181 that would means running Lisp asynchronously. */
6182 if (handling_signal)
6183 return;
6184
6185 fmt = msg = Qnil;
6186 GCPRO4 (fmt, msg, arg1, arg2);
6187
6188 args[0] = fmt = build_string (format);
6189 args[1] = arg1;
6190 args[2] = arg2;
6191 msg = Fformat (3, args);
6192
6193 len = SBYTES (msg) + 1;
6194 buffer = (char *) alloca (len);
6195 bcopy (SDATA (msg), buffer, len);
6196
6197 message_dolog (buffer, len - 1, 1, 0);
6198 UNGCPRO;
6199 }
6200
6201
6202 /* Output a newline in the *Messages* buffer if "needs" one. */
6203
6204 void
6205 message_log_maybe_newline ()
6206 {
6207 if (message_log_need_newline)
6208 message_dolog ("", 0, 1, 0);
6209 }
6210
6211
6212 /* Add a string M of length NBYTES to the message log, optionally
6213 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6214 nonzero, means interpret the contents of M as multibyte. This
6215 function calls low-level routines in order to bypass text property
6216 hooks, etc. which might not be safe to run. */
6217
6218 void
6219 message_dolog (m, nbytes, nlflag, multibyte)
6220 const char *m;
6221 int nbytes, nlflag, multibyte;
6222 {
6223 if (!NILP (Vmemory_full))
6224 return;
6225
6226 if (!NILP (Vmessage_log_max))
6227 {
6228 struct buffer *oldbuf;
6229 Lisp_Object oldpoint, oldbegv, oldzv;
6230 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6231 int point_at_end = 0;
6232 int zv_at_end = 0;
6233 Lisp_Object old_deactivate_mark, tem;
6234 struct gcpro gcpro1;
6235
6236 old_deactivate_mark = Vdeactivate_mark;
6237 oldbuf = current_buffer;
6238 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6239 current_buffer->undo_list = Qt;
6240
6241 oldpoint = message_dolog_marker1;
6242 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6243 oldbegv = message_dolog_marker2;
6244 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6245 oldzv = message_dolog_marker3;
6246 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6247 GCPRO1 (old_deactivate_mark);
6248
6249 if (PT == Z)
6250 point_at_end = 1;
6251 if (ZV == Z)
6252 zv_at_end = 1;
6253
6254 BEGV = BEG;
6255 BEGV_BYTE = BEG_BYTE;
6256 ZV = Z;
6257 ZV_BYTE = Z_BYTE;
6258 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6259
6260 /* Insert the string--maybe converting multibyte to single byte
6261 or vice versa, so that all the text fits the buffer. */
6262 if (multibyte
6263 && NILP (current_buffer->enable_multibyte_characters))
6264 {
6265 int i, c, char_bytes;
6266 unsigned char work[1];
6267
6268 /* Convert a multibyte string to single-byte
6269 for the *Message* buffer. */
6270 for (i = 0; i < nbytes; i += char_bytes)
6271 {
6272 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6273 work[0] = (SINGLE_BYTE_CHAR_P (c)
6274 ? c
6275 : multibyte_char_to_unibyte (c, Qnil));
6276 insert_1_both (work, 1, 1, 1, 0, 0);
6277 }
6278 }
6279 else if (! multibyte
6280 && ! NILP (current_buffer->enable_multibyte_characters))
6281 {
6282 int i, c, char_bytes;
6283 unsigned char *msg = (unsigned char *) m;
6284 unsigned char str[MAX_MULTIBYTE_LENGTH];
6285 /* Convert a single-byte string to multibyte
6286 for the *Message* buffer. */
6287 for (i = 0; i < nbytes; i++)
6288 {
6289 c = unibyte_char_to_multibyte (msg[i]);
6290 char_bytes = CHAR_STRING (c, str);
6291 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6292 }
6293 }
6294 else if (nbytes)
6295 insert_1 (m, nbytes, 1, 0, 0);
6296
6297 if (nlflag)
6298 {
6299 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6300 insert_1 ("\n", 1, 1, 0, 0);
6301
6302 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6303 this_bol = PT;
6304 this_bol_byte = PT_BYTE;
6305
6306 /* See if this line duplicates the previous one.
6307 If so, combine duplicates. */
6308 if (this_bol > BEG)
6309 {
6310 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6311 prev_bol = PT;
6312 prev_bol_byte = PT_BYTE;
6313
6314 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6315 this_bol, this_bol_byte);
6316 if (dup)
6317 {
6318 del_range_both (prev_bol, prev_bol_byte,
6319 this_bol, this_bol_byte, 0);
6320 if (dup > 1)
6321 {
6322 char dupstr[40];
6323 int duplen;
6324
6325 /* If you change this format, don't forget to also
6326 change message_log_check_duplicate. */
6327 sprintf (dupstr, " [%d times]", dup);
6328 duplen = strlen (dupstr);
6329 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6330 insert_1 (dupstr, duplen, 1, 0, 1);
6331 }
6332 }
6333 }
6334
6335 /* If we have more than the desired maximum number of lines
6336 in the *Messages* buffer now, delete the oldest ones.
6337 This is safe because we don't have undo in this buffer. */
6338
6339 if (NATNUMP (Vmessage_log_max))
6340 {
6341 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6342 -XFASTINT (Vmessage_log_max) - 1, 0);
6343 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6344 }
6345 }
6346 BEGV = XMARKER (oldbegv)->charpos;
6347 BEGV_BYTE = marker_byte_position (oldbegv);
6348
6349 if (zv_at_end)
6350 {
6351 ZV = Z;
6352 ZV_BYTE = Z_BYTE;
6353 }
6354 else
6355 {
6356 ZV = XMARKER (oldzv)->charpos;
6357 ZV_BYTE = marker_byte_position (oldzv);
6358 }
6359
6360 if (point_at_end)
6361 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6362 else
6363 /* We can't do Fgoto_char (oldpoint) because it will run some
6364 Lisp code. */
6365 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6366 XMARKER (oldpoint)->bytepos);
6367
6368 UNGCPRO;
6369 unchain_marker (XMARKER (oldpoint));
6370 unchain_marker (XMARKER (oldbegv));
6371 unchain_marker (XMARKER (oldzv));
6372
6373 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6374 set_buffer_internal (oldbuf);
6375 if (NILP (tem))
6376 windows_or_buffers_changed = old_windows_or_buffers_changed;
6377 message_log_need_newline = !nlflag;
6378 Vdeactivate_mark = old_deactivate_mark;
6379 }
6380 }
6381
6382
6383 /* We are at the end of the buffer after just having inserted a newline.
6384 (Note: We depend on the fact we won't be crossing the gap.)
6385 Check to see if the most recent message looks a lot like the previous one.
6386 Return 0 if different, 1 if the new one should just replace it, or a
6387 value N > 1 if we should also append " [N times]". */
6388
6389 static int
6390 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6391 int prev_bol, this_bol;
6392 int prev_bol_byte, this_bol_byte;
6393 {
6394 int i;
6395 int len = Z_BYTE - 1 - this_bol_byte;
6396 int seen_dots = 0;
6397 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6398 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6399
6400 for (i = 0; i < len; i++)
6401 {
6402 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6403 seen_dots = 1;
6404 if (p1[i] != p2[i])
6405 return seen_dots;
6406 }
6407 p1 += len;
6408 if (*p1 == '\n')
6409 return 2;
6410 if (*p1++ == ' ' && *p1++ == '[')
6411 {
6412 int n = 0;
6413 while (*p1 >= '0' && *p1 <= '9')
6414 n = n * 10 + *p1++ - '0';
6415 if (strncmp (p1, " times]\n", 8) == 0)
6416 return n+1;
6417 }
6418 return 0;
6419 }
6420
6421
6422 /* Display an echo area message M with a specified length of NBYTES
6423 bytes. The string may include null characters. If M is 0, clear
6424 out any existing message, and let the mini-buffer text show
6425 through.
6426
6427 The buffer M must continue to exist until after the echo area gets
6428 cleared or some other message gets displayed there. This means do
6429 not pass text that is stored in a Lisp string; do not pass text in
6430 a buffer that was alloca'd. */
6431
6432 void
6433 message2 (m, nbytes, multibyte)
6434 const char *m;
6435 int nbytes;
6436 int multibyte;
6437 {
6438 /* First flush out any partial line written with print. */
6439 message_log_maybe_newline ();
6440 if (m)
6441 message_dolog (m, nbytes, 1, multibyte);
6442 message2_nolog (m, nbytes, multibyte);
6443 }
6444
6445
6446 /* The non-logging counterpart of message2. */
6447
6448 void
6449 message2_nolog (m, nbytes, multibyte)
6450 const char *m;
6451 int nbytes, multibyte;
6452 {
6453 struct frame *sf = SELECTED_FRAME ();
6454 message_enable_multibyte = multibyte;
6455
6456 if (noninteractive)
6457 {
6458 if (noninteractive_need_newline)
6459 putc ('\n', stderr);
6460 noninteractive_need_newline = 0;
6461 if (m)
6462 fwrite (m, nbytes, 1, stderr);
6463 if (cursor_in_echo_area == 0)
6464 fprintf (stderr, "\n");
6465 fflush (stderr);
6466 }
6467 /* A null message buffer means that the frame hasn't really been
6468 initialized yet. Error messages get reported properly by
6469 cmd_error, so this must be just an informative message; toss it. */
6470 else if (INTERACTIVE
6471 && sf->glyphs_initialized_p
6472 && FRAME_MESSAGE_BUF (sf))
6473 {
6474 Lisp_Object mini_window;
6475 struct frame *f;
6476
6477 /* Get the frame containing the mini-buffer
6478 that the selected frame is using. */
6479 mini_window = FRAME_MINIBUF_WINDOW (sf);
6480 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6481
6482 FRAME_SAMPLE_VISIBILITY (f);
6483 if (FRAME_VISIBLE_P (sf)
6484 && ! FRAME_VISIBLE_P (f))
6485 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6486
6487 if (m)
6488 {
6489 set_message (m, Qnil, nbytes, multibyte);
6490 if (minibuffer_auto_raise)
6491 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6492 }
6493 else
6494 clear_message (1, 1);
6495
6496 do_pending_window_change (0);
6497 echo_area_display (1);
6498 do_pending_window_change (0);
6499 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6500 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6501 }
6502 }
6503
6504
6505 /* Display an echo area message M with a specified length of NBYTES
6506 bytes. The string may include null characters. If M is not a
6507 string, clear out any existing message, and let the mini-buffer
6508 text show through. */
6509
6510 void
6511 message3 (m, nbytes, multibyte)
6512 Lisp_Object m;
6513 int nbytes;
6514 int multibyte;
6515 {
6516 struct gcpro gcpro1;
6517
6518 GCPRO1 (m);
6519
6520 /* First flush out any partial line written with print. */
6521 message_log_maybe_newline ();
6522 if (STRINGP (m))
6523 message_dolog (SDATA (m), nbytes, 1, multibyte);
6524 message3_nolog (m, nbytes, multibyte);
6525
6526 UNGCPRO;
6527 }
6528
6529
6530 /* The non-logging version of message3. */
6531
6532 void
6533 message3_nolog (m, nbytes, multibyte)
6534 Lisp_Object m;
6535 int nbytes, multibyte;
6536 {
6537 struct frame *sf = SELECTED_FRAME ();
6538 message_enable_multibyte = multibyte;
6539
6540 if (noninteractive)
6541 {
6542 if (noninteractive_need_newline)
6543 putc ('\n', stderr);
6544 noninteractive_need_newline = 0;
6545 if (STRINGP (m))
6546 fwrite (SDATA (m), nbytes, 1, stderr);
6547 if (cursor_in_echo_area == 0)
6548 fprintf (stderr, "\n");
6549 fflush (stderr);
6550 }
6551 /* A null message buffer means that the frame hasn't really been
6552 initialized yet. Error messages get reported properly by
6553 cmd_error, so this must be just an informative message; toss it. */
6554 else if (INTERACTIVE
6555 && sf->glyphs_initialized_p
6556 && FRAME_MESSAGE_BUF (sf))
6557 {
6558 Lisp_Object mini_window;
6559 Lisp_Object frame;
6560 struct frame *f;
6561
6562 /* Get the frame containing the mini-buffer
6563 that the selected frame is using. */
6564 mini_window = FRAME_MINIBUF_WINDOW (sf);
6565 frame = XWINDOW (mini_window)->frame;
6566 f = XFRAME (frame);
6567
6568 FRAME_SAMPLE_VISIBILITY (f);
6569 if (FRAME_VISIBLE_P (sf)
6570 && !FRAME_VISIBLE_P (f))
6571 Fmake_frame_visible (frame);
6572
6573 if (STRINGP (m) && SCHARS (m) > 0)
6574 {
6575 set_message (NULL, m, nbytes, multibyte);
6576 if (minibuffer_auto_raise)
6577 Fraise_frame (frame);
6578 }
6579 else
6580 clear_message (1, 1);
6581
6582 do_pending_window_change (0);
6583 echo_area_display (1);
6584 do_pending_window_change (0);
6585 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6586 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6587 }
6588 }
6589
6590
6591 /* Display a null-terminated echo area message M. If M is 0, clear
6592 out any existing message, and let the mini-buffer text show through.
6593
6594 The buffer M must continue to exist until after the echo area gets
6595 cleared or some other message gets displayed there. Do not pass
6596 text that is stored in a Lisp string. Do not pass text in a buffer
6597 that was alloca'd. */
6598
6599 void
6600 message1 (m)
6601 char *m;
6602 {
6603 message2 (m, (m ? strlen (m) : 0), 0);
6604 }
6605
6606
6607 /* The non-logging counterpart of message1. */
6608
6609 void
6610 message1_nolog (m)
6611 char *m;
6612 {
6613 message2_nolog (m, (m ? strlen (m) : 0), 0);
6614 }
6615
6616 /* Display a message M which contains a single %s
6617 which gets replaced with STRING. */
6618
6619 void
6620 message_with_string (m, string, log)
6621 char *m;
6622 Lisp_Object string;
6623 int log;
6624 {
6625 CHECK_STRING (string);
6626
6627 if (noninteractive)
6628 {
6629 if (m)
6630 {
6631 if (noninteractive_need_newline)
6632 putc ('\n', stderr);
6633 noninteractive_need_newline = 0;
6634 fprintf (stderr, m, SDATA (string));
6635 if (cursor_in_echo_area == 0)
6636 fprintf (stderr, "\n");
6637 fflush (stderr);
6638 }
6639 }
6640 else if (INTERACTIVE)
6641 {
6642 /* The frame whose minibuffer we're going to display the message on.
6643 It may be larger than the selected frame, so we need
6644 to use its buffer, not the selected frame's buffer. */
6645 Lisp_Object mini_window;
6646 struct frame *f, *sf = SELECTED_FRAME ();
6647
6648 /* Get the frame containing the minibuffer
6649 that the selected frame is using. */
6650 mini_window = FRAME_MINIBUF_WINDOW (sf);
6651 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6652
6653 /* A null message buffer means that the frame hasn't really been
6654 initialized yet. Error messages get reported properly by
6655 cmd_error, so this must be just an informative message; toss it. */
6656 if (FRAME_MESSAGE_BUF (f))
6657 {
6658 Lisp_Object args[2], message;
6659 struct gcpro gcpro1, gcpro2;
6660
6661 args[0] = build_string (m);
6662 args[1] = message = string;
6663 GCPRO2 (args[0], message);
6664 gcpro1.nvars = 2;
6665
6666 message = Fformat (2, args);
6667
6668 if (log)
6669 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6670 else
6671 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6672
6673 UNGCPRO;
6674
6675 /* Print should start at the beginning of the message
6676 buffer next time. */
6677 message_buf_print = 0;
6678 }
6679 }
6680 }
6681
6682
6683 /* Dump an informative message to the minibuf. If M is 0, clear out
6684 any existing message, and let the mini-buffer text show through. */
6685
6686 /* VARARGS 1 */
6687 void
6688 message (m, a1, a2, a3)
6689 char *m;
6690 EMACS_INT a1, a2, a3;
6691 {
6692 if (noninteractive)
6693 {
6694 if (m)
6695 {
6696 if (noninteractive_need_newline)
6697 putc ('\n', stderr);
6698 noninteractive_need_newline = 0;
6699 fprintf (stderr, m, a1, a2, a3);
6700 if (cursor_in_echo_area == 0)
6701 fprintf (stderr, "\n");
6702 fflush (stderr);
6703 }
6704 }
6705 else if (INTERACTIVE)
6706 {
6707 /* The frame whose mini-buffer we're going to display the message
6708 on. It may be larger than the selected frame, so we need to
6709 use its buffer, not the selected frame's buffer. */
6710 Lisp_Object mini_window;
6711 struct frame *f, *sf = SELECTED_FRAME ();
6712
6713 /* Get the frame containing the mini-buffer
6714 that the selected frame is using. */
6715 mini_window = FRAME_MINIBUF_WINDOW (sf);
6716 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6717
6718 /* A null message buffer means that the frame hasn't really been
6719 initialized yet. Error messages get reported properly by
6720 cmd_error, so this must be just an informative message; toss
6721 it. */
6722 if (FRAME_MESSAGE_BUF (f))
6723 {
6724 if (m)
6725 {
6726 int len;
6727 #ifdef NO_ARG_ARRAY
6728 char *a[3];
6729 a[0] = (char *) a1;
6730 a[1] = (char *) a2;
6731 a[2] = (char *) a3;
6732
6733 len = doprnt (FRAME_MESSAGE_BUF (f),
6734 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6735 #else
6736 len = doprnt (FRAME_MESSAGE_BUF (f),
6737 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6738 (char **) &a1);
6739 #endif /* NO_ARG_ARRAY */
6740
6741 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6742 }
6743 else
6744 message1 (0);
6745
6746 /* Print should start at the beginning of the message
6747 buffer next time. */
6748 message_buf_print = 0;
6749 }
6750 }
6751 }
6752
6753
6754 /* The non-logging version of message. */
6755
6756 void
6757 message_nolog (m, a1, a2, a3)
6758 char *m;
6759 EMACS_INT a1, a2, a3;
6760 {
6761 Lisp_Object old_log_max;
6762 old_log_max = Vmessage_log_max;
6763 Vmessage_log_max = Qnil;
6764 message (m, a1, a2, a3);
6765 Vmessage_log_max = old_log_max;
6766 }
6767
6768
6769 /* Display the current message in the current mini-buffer. This is
6770 only called from error handlers in process.c, and is not time
6771 critical. */
6772
6773 void
6774 update_echo_area ()
6775 {
6776 if (!NILP (echo_area_buffer[0]))
6777 {
6778 Lisp_Object string;
6779 string = Fcurrent_message ();
6780 message3 (string, SBYTES (string),
6781 !NILP (current_buffer->enable_multibyte_characters));
6782 }
6783 }
6784
6785
6786 /* Make sure echo area buffers in `echo_buffers' are live.
6787 If they aren't, make new ones. */
6788
6789 static void
6790 ensure_echo_area_buffers ()
6791 {
6792 int i;
6793
6794 for (i = 0; i < 2; ++i)
6795 if (!BUFFERP (echo_buffer[i])
6796 || NILP (XBUFFER (echo_buffer[i])->name))
6797 {
6798 char name[30];
6799 Lisp_Object old_buffer;
6800 int j;
6801
6802 old_buffer = echo_buffer[i];
6803 sprintf (name, " *Echo Area %d*", i);
6804 echo_buffer[i] = Fget_buffer_create (build_string (name));
6805 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6806
6807 for (j = 0; j < 2; ++j)
6808 if (EQ (old_buffer, echo_area_buffer[j]))
6809 echo_area_buffer[j] = echo_buffer[i];
6810 }
6811 }
6812
6813
6814 /* Call FN with args A1..A4 with either the current or last displayed
6815 echo_area_buffer as current buffer.
6816
6817 WHICH zero means use the current message buffer
6818 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6819 from echo_buffer[] and clear it.
6820
6821 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6822 suitable buffer from echo_buffer[] and clear it.
6823
6824 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6825 that the current message becomes the last displayed one, make
6826 choose a suitable buffer for echo_area_buffer[0], and clear it.
6827
6828 Value is what FN returns. */
6829
6830 static int
6831 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6832 struct window *w;
6833 int which;
6834 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6835 EMACS_INT a1;
6836 Lisp_Object a2;
6837 EMACS_INT a3, a4;
6838 {
6839 Lisp_Object buffer;
6840 int this_one, the_other, clear_buffer_p, rc;
6841 int count = SPECPDL_INDEX ();
6842
6843 /* If buffers aren't live, make new ones. */
6844 ensure_echo_area_buffers ();
6845
6846 clear_buffer_p = 0;
6847
6848 if (which == 0)
6849 this_one = 0, the_other = 1;
6850 else if (which > 0)
6851 this_one = 1, the_other = 0;
6852 else
6853 {
6854 this_one = 0, the_other = 1;
6855 clear_buffer_p = 1;
6856
6857 /* We need a fresh one in case the current echo buffer equals
6858 the one containing the last displayed echo area message. */
6859 if (!NILP (echo_area_buffer[this_one])
6860 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6861 echo_area_buffer[this_one] = Qnil;
6862 }
6863
6864 /* Choose a suitable buffer from echo_buffer[] is we don't
6865 have one. */
6866 if (NILP (echo_area_buffer[this_one]))
6867 {
6868 echo_area_buffer[this_one]
6869 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6870 ? echo_buffer[the_other]
6871 : echo_buffer[this_one]);
6872 clear_buffer_p = 1;
6873 }
6874
6875 buffer = echo_area_buffer[this_one];
6876
6877 /* Don't get confused by reusing the buffer used for echoing
6878 for a different purpose. */
6879 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6880 cancel_echoing ();
6881
6882 record_unwind_protect (unwind_with_echo_area_buffer,
6883 with_echo_area_buffer_unwind_data (w));
6884
6885 /* Make the echo area buffer current. Note that for display
6886 purposes, it is not necessary that the displayed window's buffer
6887 == current_buffer, except for text property lookup. So, let's
6888 only set that buffer temporarily here without doing a full
6889 Fset_window_buffer. We must also change w->pointm, though,
6890 because otherwise an assertions in unshow_buffer fails, and Emacs
6891 aborts. */
6892 set_buffer_internal_1 (XBUFFER (buffer));
6893 if (w)
6894 {
6895 w->buffer = buffer;
6896 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6897 }
6898
6899 current_buffer->undo_list = Qt;
6900 current_buffer->read_only = Qnil;
6901 specbind (Qinhibit_read_only, Qt);
6902 specbind (Qinhibit_modification_hooks, Qt);
6903
6904 if (clear_buffer_p && Z > BEG)
6905 del_range (BEG, Z);
6906
6907 xassert (BEGV >= BEG);
6908 xassert (ZV <= Z && ZV >= BEGV);
6909
6910 rc = fn (a1, a2, a3, a4);
6911
6912 xassert (BEGV >= BEG);
6913 xassert (ZV <= Z && ZV >= BEGV);
6914
6915 unbind_to (count, Qnil);
6916 return rc;
6917 }
6918
6919
6920 /* Save state that should be preserved around the call to the function
6921 FN called in with_echo_area_buffer. */
6922
6923 static Lisp_Object
6924 with_echo_area_buffer_unwind_data (w)
6925 struct window *w;
6926 {
6927 int i = 0;
6928 Lisp_Object vector;
6929
6930 /* Reduce consing by keeping one vector in
6931 Vwith_echo_area_save_vector. */
6932 vector = Vwith_echo_area_save_vector;
6933 Vwith_echo_area_save_vector = Qnil;
6934
6935 if (NILP (vector))
6936 vector = Fmake_vector (make_number (7), Qnil);
6937
6938 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6939 AREF (vector, i) = Vdeactivate_mark, ++i;
6940 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6941
6942 if (w)
6943 {
6944 XSETWINDOW (AREF (vector, i), w); ++i;
6945 AREF (vector, i) = w->buffer; ++i;
6946 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6947 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6948 }
6949 else
6950 {
6951 int end = i + 4;
6952 for (; i < end; ++i)
6953 AREF (vector, i) = Qnil;
6954 }
6955
6956 xassert (i == ASIZE (vector));
6957 return vector;
6958 }
6959
6960
6961 /* Restore global state from VECTOR which was created by
6962 with_echo_area_buffer_unwind_data. */
6963
6964 static Lisp_Object
6965 unwind_with_echo_area_buffer (vector)
6966 Lisp_Object vector;
6967 {
6968 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6969 Vdeactivate_mark = AREF (vector, 1);
6970 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6971
6972 if (WINDOWP (AREF (vector, 3)))
6973 {
6974 struct window *w;
6975 Lisp_Object buffer, charpos, bytepos;
6976
6977 w = XWINDOW (AREF (vector, 3));
6978 buffer = AREF (vector, 4);
6979 charpos = AREF (vector, 5);
6980 bytepos = AREF (vector, 6);
6981
6982 w->buffer = buffer;
6983 set_marker_both (w->pointm, buffer,
6984 XFASTINT (charpos), XFASTINT (bytepos));
6985 }
6986
6987 Vwith_echo_area_save_vector = vector;
6988 return Qnil;
6989 }
6990
6991
6992 /* Set up the echo area for use by print functions. MULTIBYTE_P
6993 non-zero means we will print multibyte. */
6994
6995 void
6996 setup_echo_area_for_printing (multibyte_p)
6997 int multibyte_p;
6998 {
6999 /* If we can't find an echo area any more, exit. */
7000 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7001 Fkill_emacs (Qnil);
7002
7003 ensure_echo_area_buffers ();
7004
7005 if (!message_buf_print)
7006 {
7007 /* A message has been output since the last time we printed.
7008 Choose a fresh echo area buffer. */
7009 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7010 echo_area_buffer[0] = echo_buffer[1];
7011 else
7012 echo_area_buffer[0] = echo_buffer[0];
7013
7014 /* Switch to that buffer and clear it. */
7015 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7016 current_buffer->truncate_lines = Qnil;
7017
7018 if (Z > BEG)
7019 {
7020 int count = SPECPDL_INDEX ();
7021 specbind (Qinhibit_read_only, Qt);
7022 /* Note that undo recording is always disabled. */
7023 del_range (BEG, Z);
7024 unbind_to (count, Qnil);
7025 }
7026 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7027
7028 /* Set up the buffer for the multibyteness we need. */
7029 if (multibyte_p
7030 != !NILP (current_buffer->enable_multibyte_characters))
7031 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7032
7033 /* Raise the frame containing the echo area. */
7034 if (minibuffer_auto_raise)
7035 {
7036 struct frame *sf = SELECTED_FRAME ();
7037 Lisp_Object mini_window;
7038 mini_window = FRAME_MINIBUF_WINDOW (sf);
7039 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7040 }
7041
7042 message_log_maybe_newline ();
7043 message_buf_print = 1;
7044 }
7045 else
7046 {
7047 if (NILP (echo_area_buffer[0]))
7048 {
7049 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7050 echo_area_buffer[0] = echo_buffer[1];
7051 else
7052 echo_area_buffer[0] = echo_buffer[0];
7053 }
7054
7055 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7056 {
7057 /* Someone switched buffers between print requests. */
7058 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7059 current_buffer->truncate_lines = Qnil;
7060 }
7061 }
7062 }
7063
7064
7065 /* Display an echo area message in window W. Value is non-zero if W's
7066 height is changed. If display_last_displayed_message_p is
7067 non-zero, display the message that was last displayed, otherwise
7068 display the current message. */
7069
7070 static int
7071 display_echo_area (w)
7072 struct window *w;
7073 {
7074 int i, no_message_p, window_height_changed_p, count;
7075
7076 /* Temporarily disable garbage collections while displaying the echo
7077 area. This is done because a GC can print a message itself.
7078 That message would modify the echo area buffer's contents while a
7079 redisplay of the buffer is going on, and seriously confuse
7080 redisplay. */
7081 count = inhibit_garbage_collection ();
7082
7083 /* If there is no message, we must call display_echo_area_1
7084 nevertheless because it resizes the window. But we will have to
7085 reset the echo_area_buffer in question to nil at the end because
7086 with_echo_area_buffer will sets it to an empty buffer. */
7087 i = display_last_displayed_message_p ? 1 : 0;
7088 no_message_p = NILP (echo_area_buffer[i]);
7089
7090 window_height_changed_p
7091 = with_echo_area_buffer (w, display_last_displayed_message_p,
7092 display_echo_area_1,
7093 (EMACS_INT) w, Qnil, 0, 0);
7094
7095 if (no_message_p)
7096 echo_area_buffer[i] = Qnil;
7097
7098 unbind_to (count, Qnil);
7099 return window_height_changed_p;
7100 }
7101
7102
7103 /* Helper for display_echo_area. Display the current buffer which
7104 contains the current echo area message in window W, a mini-window,
7105 a pointer to which is passed in A1. A2..A4 are currently not used.
7106 Change the height of W so that all of the message is displayed.
7107 Value is non-zero if height of W was changed. */
7108
7109 static int
7110 display_echo_area_1 (a1, a2, a3, a4)
7111 EMACS_INT a1;
7112 Lisp_Object a2;
7113 EMACS_INT a3, a4;
7114 {
7115 struct window *w = (struct window *) a1;
7116 Lisp_Object window;
7117 struct text_pos start;
7118 int window_height_changed_p = 0;
7119
7120 /* Do this before displaying, so that we have a large enough glyph
7121 matrix for the display. */
7122 window_height_changed_p = resize_mini_window (w, 0);
7123
7124 /* Display. */
7125 clear_glyph_matrix (w->desired_matrix);
7126 XSETWINDOW (window, w);
7127 SET_TEXT_POS (start, BEG, BEG_BYTE);
7128 try_window (window, start);
7129
7130 return window_height_changed_p;
7131 }
7132
7133
7134 /* Resize the echo area window to exactly the size needed for the
7135 currently displayed message, if there is one. If a mini-buffer
7136 is active, don't shrink it. */
7137
7138 void
7139 resize_echo_area_exactly ()
7140 {
7141 if (BUFFERP (echo_area_buffer[0])
7142 && WINDOWP (echo_area_window))
7143 {
7144 struct window *w = XWINDOW (echo_area_window);
7145 int resized_p;
7146 Lisp_Object resize_exactly;
7147
7148 if (minibuf_level == 0)
7149 resize_exactly = Qt;
7150 else
7151 resize_exactly = Qnil;
7152
7153 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7154 (EMACS_INT) w, resize_exactly, 0, 0);
7155 if (resized_p)
7156 {
7157 ++windows_or_buffers_changed;
7158 ++update_mode_lines;
7159 redisplay_internal (0);
7160 }
7161 }
7162 }
7163
7164
7165 /* Callback function for with_echo_area_buffer, when used from
7166 resize_echo_area_exactly. A1 contains a pointer to the window to
7167 resize, EXACTLY non-nil means resize the mini-window exactly to the
7168 size of the text displayed. A3 and A4 are not used. Value is what
7169 resize_mini_window returns. */
7170
7171 static int
7172 resize_mini_window_1 (a1, exactly, a3, a4)
7173 EMACS_INT a1;
7174 Lisp_Object exactly;
7175 EMACS_INT a3, a4;
7176 {
7177 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7178 }
7179
7180
7181 /* Resize mini-window W to fit the size of its contents. EXACT:P
7182 means size the window exactly to the size needed. Otherwise, it's
7183 only enlarged until W's buffer is empty. Value is non-zero if
7184 the window height has been changed. */
7185
7186 int
7187 resize_mini_window (w, exact_p)
7188 struct window *w;
7189 int exact_p;
7190 {
7191 struct frame *f = XFRAME (w->frame);
7192 int window_height_changed_p = 0;
7193
7194 xassert (MINI_WINDOW_P (w));
7195
7196 /* Don't resize windows while redisplaying a window; it would
7197 confuse redisplay functions when the size of the window they are
7198 displaying changes from under them. Such a resizing can happen,
7199 for instance, when which-func prints a long message while
7200 we are running fontification-functions. We're running these
7201 functions with safe_call which binds inhibit-redisplay to t. */
7202 if (!NILP (Vinhibit_redisplay))
7203 return 0;
7204
7205 /* Nil means don't try to resize. */
7206 if (NILP (Vresize_mini_windows)
7207 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7208 return 0;
7209
7210 if (!FRAME_MINIBUF_ONLY_P (f))
7211 {
7212 struct it it;
7213 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7214 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7215 int height, max_height;
7216 int unit = FRAME_LINE_HEIGHT (f);
7217 struct text_pos start;
7218 struct buffer *old_current_buffer = NULL;
7219
7220 if (current_buffer != XBUFFER (w->buffer))
7221 {
7222 old_current_buffer = current_buffer;
7223 set_buffer_internal (XBUFFER (w->buffer));
7224 }
7225
7226 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7227
7228 /* Compute the max. number of lines specified by the user. */
7229 if (FLOATP (Vmax_mini_window_height))
7230 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7231 else if (INTEGERP (Vmax_mini_window_height))
7232 max_height = XINT (Vmax_mini_window_height);
7233 else
7234 max_height = total_height / 4;
7235
7236 /* Correct that max. height if it's bogus. */
7237 max_height = max (1, max_height);
7238 max_height = min (total_height, max_height);
7239
7240 /* Find out the height of the text in the window. */
7241 if (it.truncate_lines_p)
7242 height = 1;
7243 else
7244 {
7245 last_height = 0;
7246 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7247 if (it.max_ascent == 0 && it.max_descent == 0)
7248 height = it.current_y + last_height;
7249 else
7250 height = it.current_y + it.max_ascent + it.max_descent;
7251 height -= it.extra_line_spacing;
7252 height = (height + unit - 1) / unit;
7253 }
7254
7255 /* Compute a suitable window start. */
7256 if (height > max_height)
7257 {
7258 height = max_height;
7259 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7260 move_it_vertically_backward (&it, (height - 1) * unit);
7261 start = it.current.pos;
7262 }
7263 else
7264 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7265 SET_MARKER_FROM_TEXT_POS (w->start, start);
7266
7267 if (EQ (Vresize_mini_windows, Qgrow_only))
7268 {
7269 /* Let it grow only, until we display an empty message, in which
7270 case the window shrinks again. */
7271 if (height > WINDOW_TOTAL_LINES (w))
7272 {
7273 int old_height = WINDOW_TOTAL_LINES (w);
7274 freeze_window_starts (f, 1);
7275 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7276 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7277 }
7278 else if (height < WINDOW_TOTAL_LINES (w)
7279 && (exact_p || BEGV == ZV))
7280 {
7281 int old_height = WINDOW_TOTAL_LINES (w);
7282 freeze_window_starts (f, 0);
7283 shrink_mini_window (w);
7284 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7285 }
7286 }
7287 else
7288 {
7289 /* Always resize to exact size needed. */
7290 if (height > WINDOW_TOTAL_LINES (w))
7291 {
7292 int old_height = WINDOW_TOTAL_LINES (w);
7293 freeze_window_starts (f, 1);
7294 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7295 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7296 }
7297 else if (height < WINDOW_TOTAL_LINES (w))
7298 {
7299 int old_height = WINDOW_TOTAL_LINES (w);
7300 freeze_window_starts (f, 0);
7301 shrink_mini_window (w);
7302
7303 if (height)
7304 {
7305 freeze_window_starts (f, 1);
7306 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7307 }
7308
7309 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7310 }
7311 }
7312
7313 if (old_current_buffer)
7314 set_buffer_internal (old_current_buffer);
7315 }
7316
7317 return window_height_changed_p;
7318 }
7319
7320
7321 /* Value is the current message, a string, or nil if there is no
7322 current message. */
7323
7324 Lisp_Object
7325 current_message ()
7326 {
7327 Lisp_Object msg;
7328
7329 if (NILP (echo_area_buffer[0]))
7330 msg = Qnil;
7331 else
7332 {
7333 with_echo_area_buffer (0, 0, current_message_1,
7334 (EMACS_INT) &msg, Qnil, 0, 0);
7335 if (NILP (msg))
7336 echo_area_buffer[0] = Qnil;
7337 }
7338
7339 return msg;
7340 }
7341
7342
7343 static int
7344 current_message_1 (a1, a2, a3, a4)
7345 EMACS_INT a1;
7346 Lisp_Object a2;
7347 EMACS_INT a3, a4;
7348 {
7349 Lisp_Object *msg = (Lisp_Object *) a1;
7350
7351 if (Z > BEG)
7352 *msg = make_buffer_string (BEG, Z, 1);
7353 else
7354 *msg = Qnil;
7355 return 0;
7356 }
7357
7358
7359 /* Push the current message on Vmessage_stack for later restauration
7360 by restore_message. Value is non-zero if the current message isn't
7361 empty. This is a relatively infrequent operation, so it's not
7362 worth optimizing. */
7363
7364 int
7365 push_message ()
7366 {
7367 Lisp_Object msg;
7368 msg = current_message ();
7369 Vmessage_stack = Fcons (msg, Vmessage_stack);
7370 return STRINGP (msg);
7371 }
7372
7373
7374 /* Restore message display from the top of Vmessage_stack. */
7375
7376 void
7377 restore_message ()
7378 {
7379 Lisp_Object msg;
7380
7381 xassert (CONSP (Vmessage_stack));
7382 msg = XCAR (Vmessage_stack);
7383 if (STRINGP (msg))
7384 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7385 else
7386 message3_nolog (msg, 0, 0);
7387 }
7388
7389
7390 /* Handler for record_unwind_protect calling pop_message. */
7391
7392 Lisp_Object
7393 pop_message_unwind (dummy)
7394 Lisp_Object dummy;
7395 {
7396 pop_message ();
7397 return Qnil;
7398 }
7399
7400 /* Pop the top-most entry off Vmessage_stack. */
7401
7402 void
7403 pop_message ()
7404 {
7405 xassert (CONSP (Vmessage_stack));
7406 Vmessage_stack = XCDR (Vmessage_stack);
7407 }
7408
7409
7410 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7411 exits. If the stack is not empty, we have a missing pop_message
7412 somewhere. */
7413
7414 void
7415 check_message_stack ()
7416 {
7417 if (!NILP (Vmessage_stack))
7418 abort ();
7419 }
7420
7421
7422 /* Truncate to NCHARS what will be displayed in the echo area the next
7423 time we display it---but don't redisplay it now. */
7424
7425 void
7426 truncate_echo_area (nchars)
7427 int nchars;
7428 {
7429 if (nchars == 0)
7430 echo_area_buffer[0] = Qnil;
7431 /* A null message buffer means that the frame hasn't really been
7432 initialized yet. Error messages get reported properly by
7433 cmd_error, so this must be just an informative message; toss it. */
7434 else if (!noninteractive
7435 && INTERACTIVE
7436 && !NILP (echo_area_buffer[0]))
7437 {
7438 struct frame *sf = SELECTED_FRAME ();
7439 if (FRAME_MESSAGE_BUF (sf))
7440 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7441 }
7442 }
7443
7444
7445 /* Helper function for truncate_echo_area. Truncate the current
7446 message to at most NCHARS characters. */
7447
7448 static int
7449 truncate_message_1 (nchars, a2, a3, a4)
7450 EMACS_INT nchars;
7451 Lisp_Object a2;
7452 EMACS_INT a3, a4;
7453 {
7454 if (BEG + nchars < Z)
7455 del_range (BEG + nchars, Z);
7456 if (Z == BEG)
7457 echo_area_buffer[0] = Qnil;
7458 return 0;
7459 }
7460
7461
7462 /* Set the current message to a substring of S or STRING.
7463
7464 If STRING is a Lisp string, set the message to the first NBYTES
7465 bytes from STRING. NBYTES zero means use the whole string. If
7466 STRING is multibyte, the message will be displayed multibyte.
7467
7468 If S is not null, set the message to the first LEN bytes of S. LEN
7469 zero means use the whole string. MULTIBYTE_P non-zero means S is
7470 multibyte. Display the message multibyte in that case. */
7471
7472 void
7473 set_message (s, string, nbytes, multibyte_p)
7474 const char *s;
7475 Lisp_Object string;
7476 int nbytes, multibyte_p;
7477 {
7478 message_enable_multibyte
7479 = ((s && multibyte_p)
7480 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7481
7482 with_echo_area_buffer (0, -1, set_message_1,
7483 (EMACS_INT) s, string, nbytes, multibyte_p);
7484 message_buf_print = 0;
7485 help_echo_showing_p = 0;
7486 }
7487
7488
7489 /* Helper function for set_message. Arguments have the same meaning
7490 as there, with A1 corresponding to S and A2 corresponding to STRING
7491 This function is called with the echo area buffer being
7492 current. */
7493
7494 static int
7495 set_message_1 (a1, a2, nbytes, multibyte_p)
7496 EMACS_INT a1;
7497 Lisp_Object a2;
7498 EMACS_INT nbytes, multibyte_p;
7499 {
7500 const char *s = (const char *) a1;
7501 Lisp_Object string = a2;
7502
7503 xassert (BEG == Z);
7504
7505 /* Change multibyteness of the echo buffer appropriately. */
7506 if (message_enable_multibyte
7507 != !NILP (current_buffer->enable_multibyte_characters))
7508 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7509
7510 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7511
7512 /* Insert new message at BEG. */
7513 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7514
7515 if (STRINGP (string))
7516 {
7517 int nchars;
7518
7519 if (nbytes == 0)
7520 nbytes = SBYTES (string);
7521 nchars = string_byte_to_char (string, nbytes);
7522
7523 /* This function takes care of single/multibyte conversion. We
7524 just have to ensure that the echo area buffer has the right
7525 setting of enable_multibyte_characters. */
7526 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7527 }
7528 else if (s)
7529 {
7530 if (nbytes == 0)
7531 nbytes = strlen (s);
7532
7533 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7534 {
7535 /* Convert from multi-byte to single-byte. */
7536 int i, c, n;
7537 unsigned char work[1];
7538
7539 /* Convert a multibyte string to single-byte. */
7540 for (i = 0; i < nbytes; i += n)
7541 {
7542 c = string_char_and_length (s + i, nbytes - i, &n);
7543 work[0] = (SINGLE_BYTE_CHAR_P (c)
7544 ? c
7545 : multibyte_char_to_unibyte (c, Qnil));
7546 insert_1_both (work, 1, 1, 1, 0, 0);
7547 }
7548 }
7549 else if (!multibyte_p
7550 && !NILP (current_buffer->enable_multibyte_characters))
7551 {
7552 /* Convert from single-byte to multi-byte. */
7553 int i, c, n;
7554 const unsigned char *msg = (const unsigned char *) s;
7555 unsigned char str[MAX_MULTIBYTE_LENGTH];
7556
7557 /* Convert a single-byte string to multibyte. */
7558 for (i = 0; i < nbytes; i++)
7559 {
7560 c = unibyte_char_to_multibyte (msg[i]);
7561 n = CHAR_STRING (c, str);
7562 insert_1_both (str, 1, n, 1, 0, 0);
7563 }
7564 }
7565 else
7566 insert_1 (s, nbytes, 1, 0, 0);
7567 }
7568
7569 return 0;
7570 }
7571
7572
7573 /* Clear messages. CURRENT_P non-zero means clear the current
7574 message. LAST_DISPLAYED_P non-zero means clear the message
7575 last displayed. */
7576
7577 void
7578 clear_message (current_p, last_displayed_p)
7579 int current_p, last_displayed_p;
7580 {
7581 if (current_p)
7582 {
7583 echo_area_buffer[0] = Qnil;
7584 message_cleared_p = 1;
7585 }
7586
7587 if (last_displayed_p)
7588 echo_area_buffer[1] = Qnil;
7589
7590 message_buf_print = 0;
7591 }
7592
7593 /* Clear garbaged frames.
7594
7595 This function is used where the old redisplay called
7596 redraw_garbaged_frames which in turn called redraw_frame which in
7597 turn called clear_frame. The call to clear_frame was a source of
7598 flickering. I believe a clear_frame is not necessary. It should
7599 suffice in the new redisplay to invalidate all current matrices,
7600 and ensure a complete redisplay of all windows. */
7601
7602 static void
7603 clear_garbaged_frames ()
7604 {
7605 if (frame_garbaged)
7606 {
7607 Lisp_Object tail, frame;
7608 int changed_count = 0;
7609
7610 FOR_EACH_FRAME (tail, frame)
7611 {
7612 struct frame *f = XFRAME (frame);
7613
7614 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7615 {
7616 if (f->resized_p)
7617 Fredraw_frame (frame);
7618 clear_current_matrices (f);
7619 changed_count++;
7620 f->garbaged = 0;
7621 f->resized_p = 0;
7622 }
7623 }
7624
7625 frame_garbaged = 0;
7626 if (changed_count)
7627 ++windows_or_buffers_changed;
7628 }
7629 }
7630
7631
7632 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7633 is non-zero update selected_frame. Value is non-zero if the
7634 mini-windows height has been changed. */
7635
7636 static int
7637 echo_area_display (update_frame_p)
7638 int update_frame_p;
7639 {
7640 Lisp_Object mini_window;
7641 struct window *w;
7642 struct frame *f;
7643 int window_height_changed_p = 0;
7644 struct frame *sf = SELECTED_FRAME ();
7645
7646 mini_window = FRAME_MINIBUF_WINDOW (sf);
7647 w = XWINDOW (mini_window);
7648 f = XFRAME (WINDOW_FRAME (w));
7649
7650 /* Don't display if frame is invisible or not yet initialized. */
7651 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7652 return 0;
7653
7654 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7655 #ifndef MAC_OS8
7656 #ifdef HAVE_WINDOW_SYSTEM
7657 /* When Emacs starts, selected_frame may be the initial terminal
7658 frame. If we let this through, a message would be displayed on
7659 the terminal. */
7660 if (FRAME_TERMCAP_P (XFRAME (selected_frame))
7661 && FRAME_TTY (XFRAME (selected_frame))->type == NULL)
7662 return 0;
7663 #endif /* HAVE_WINDOW_SYSTEM */
7664 #endif
7665
7666 /* Redraw garbaged frames. */
7667 if (frame_garbaged)
7668 clear_garbaged_frames ();
7669
7670 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7671 {
7672 echo_area_window = mini_window;
7673 window_height_changed_p = display_echo_area (w);
7674 w->must_be_updated_p = 1;
7675
7676 /* Update the display, unless called from redisplay_internal.
7677 Also don't update the screen during redisplay itself. The
7678 update will happen at the end of redisplay, and an update
7679 here could cause confusion. */
7680 if (update_frame_p && !redisplaying_p)
7681 {
7682 int n = 0;
7683
7684 /* If the display update has been interrupted by pending
7685 input, update mode lines in the frame. Due to the
7686 pending input, it might have been that redisplay hasn't
7687 been called, so that mode lines above the echo area are
7688 garbaged. This looks odd, so we prevent it here. */
7689 if (!display_completed)
7690 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7691
7692 if (window_height_changed_p
7693 /* Don't do this if Emacs is shutting down. Redisplay
7694 needs to run hooks. */
7695 && !NILP (Vrun_hooks))
7696 {
7697 /* Must update other windows. Likewise as in other
7698 cases, don't let this update be interrupted by
7699 pending input. */
7700 int count = SPECPDL_INDEX ();
7701 specbind (Qredisplay_dont_pause, Qt);
7702 windows_or_buffers_changed = 1;
7703 redisplay_internal (0);
7704 unbind_to (count, Qnil);
7705 }
7706 else if (FRAME_WINDOW_P (f) && n == 0)
7707 {
7708 /* Window configuration is the same as before.
7709 Can do with a display update of the echo area,
7710 unless we displayed some mode lines. */
7711 update_single_window (w, 1);
7712 FRAME_RIF (f)->flush_display (f);
7713 }
7714 else
7715 update_frame (f, 1, 1);
7716
7717 /* If cursor is in the echo area, make sure that the next
7718 redisplay displays the minibuffer, so that the cursor will
7719 be replaced with what the minibuffer wants. */
7720 if (cursor_in_echo_area)
7721 ++windows_or_buffers_changed;
7722 }
7723 }
7724 else if (!EQ (mini_window, selected_window))
7725 windows_or_buffers_changed++;
7726
7727 /* Last displayed message is now the current message. */
7728 echo_area_buffer[1] = echo_area_buffer[0];
7729
7730 /* Prevent redisplay optimization in redisplay_internal by resetting
7731 this_line_start_pos. This is done because the mini-buffer now
7732 displays the message instead of its buffer text. */
7733 if (EQ (mini_window, selected_window))
7734 CHARPOS (this_line_start_pos) = 0;
7735
7736 return window_height_changed_p;
7737 }
7738
7739
7740 \f
7741 /***********************************************************************
7742 Frame Titles
7743 ***********************************************************************/
7744
7745
7746 /* The frame title buffering code is also used by Fformat_mode_line.
7747 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7748
7749 /* A buffer for constructing frame titles in it; allocated from the
7750 heap in init_xdisp and resized as needed in store_frame_title_char. */
7751
7752 static char *frame_title_buf;
7753
7754 /* The buffer's end, and a current output position in it. */
7755
7756 static char *frame_title_buf_end;
7757 static char *frame_title_ptr;
7758
7759
7760 /* Store a single character C for the frame title in frame_title_buf.
7761 Re-allocate frame_title_buf if necessary. */
7762
7763 static void
7764 #ifdef PROTOTYPES
7765 store_frame_title_char (char c)
7766 #else
7767 store_frame_title_char (c)
7768 char c;
7769 #endif
7770 {
7771 /* If output position has reached the end of the allocated buffer,
7772 double the buffer's size. */
7773 if (frame_title_ptr == frame_title_buf_end)
7774 {
7775 int len = frame_title_ptr - frame_title_buf;
7776 int new_size = 2 * len * sizeof *frame_title_buf;
7777 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7778 frame_title_buf_end = frame_title_buf + new_size;
7779 frame_title_ptr = frame_title_buf + len;
7780 }
7781
7782 *frame_title_ptr++ = c;
7783 }
7784
7785
7786 /* Store part of a frame title in frame_title_buf, beginning at
7787 frame_title_ptr. STR is the string to store. Do not copy
7788 characters that yield more columns than PRECISION; PRECISION <= 0
7789 means copy the whole string. Pad with spaces until FIELD_WIDTH
7790 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7791 pad. Called from display_mode_element when it is used to build a
7792 frame title. */
7793
7794 static int
7795 store_frame_title (str, field_width, precision)
7796 const unsigned char *str;
7797 int field_width, precision;
7798 {
7799 int n = 0;
7800 int dummy, nbytes;
7801
7802 /* Copy at most PRECISION chars from STR. */
7803 nbytes = strlen (str);
7804 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7805 while (nbytes--)
7806 store_frame_title_char (*str++);
7807
7808 /* Fill up with spaces until FIELD_WIDTH reached. */
7809 while (field_width > 0
7810 && n < field_width)
7811 {
7812 store_frame_title_char (' ');
7813 ++n;
7814 }
7815
7816 return n;
7817 }
7818
7819 #ifdef HAVE_WINDOW_SYSTEM
7820
7821 /* Set the title of FRAME, if it has changed. The title format is
7822 Vicon_title_format if FRAME is iconified, otherwise it is
7823 frame_title_format. */
7824
7825 static void
7826 x_consider_frame_title (frame)
7827 Lisp_Object frame;
7828 {
7829 struct frame *f = XFRAME (frame);
7830
7831 if (FRAME_WINDOW_P (f)
7832 || FRAME_MINIBUF_ONLY_P (f)
7833 || f->explicit_name)
7834 {
7835 /* Do we have more than one visible frame on this X display? */
7836 Lisp_Object tail;
7837 Lisp_Object fmt;
7838 struct buffer *obuf;
7839 int len;
7840 struct it it;
7841
7842 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7843 {
7844 Lisp_Object other_frame = XCAR (tail);
7845 struct frame *tf = XFRAME (other_frame);
7846
7847 if (tf != f
7848 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7849 && !FRAME_MINIBUF_ONLY_P (tf)
7850 && !EQ (other_frame, tip_frame)
7851 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7852 break;
7853 }
7854
7855 /* Set global variable indicating that multiple frames exist. */
7856 multiple_frames = CONSP (tail);
7857
7858 /* Switch to the buffer of selected window of the frame. Set up
7859 frame_title_ptr so that display_mode_element will output into it;
7860 then display the title. */
7861 obuf = current_buffer;
7862 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7863 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7864 frame_title_ptr = frame_title_buf;
7865 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7866 NULL, DEFAULT_FACE_ID);
7867 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7868 len = frame_title_ptr - frame_title_buf;
7869 frame_title_ptr = NULL;
7870 set_buffer_internal_1 (obuf);
7871
7872 /* Set the title only if it's changed. This avoids consing in
7873 the common case where it hasn't. (If it turns out that we've
7874 already wasted too much time by walking through the list with
7875 display_mode_element, then we might need to optimize at a
7876 higher level than this.) */
7877 if (! STRINGP (f->name)
7878 || SBYTES (f->name) != len
7879 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7880 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7881 }
7882 }
7883
7884 #endif /* not HAVE_WINDOW_SYSTEM */
7885
7886
7887
7888 \f
7889 /***********************************************************************
7890 Menu Bars
7891 ***********************************************************************/
7892
7893
7894 /* Prepare for redisplay by updating menu-bar item lists when
7895 appropriate. This can call eval. */
7896
7897 void
7898 prepare_menu_bars ()
7899 {
7900 int all_windows;
7901 struct gcpro gcpro1, gcpro2;
7902 struct frame *f;
7903 Lisp_Object tooltip_frame;
7904
7905 #ifdef HAVE_WINDOW_SYSTEM
7906 tooltip_frame = tip_frame;
7907 #else
7908 tooltip_frame = Qnil;
7909 #endif
7910
7911 /* Update all frame titles based on their buffer names, etc. We do
7912 this before the menu bars so that the buffer-menu will show the
7913 up-to-date frame titles. */
7914 #ifdef HAVE_WINDOW_SYSTEM
7915 if (windows_or_buffers_changed || update_mode_lines)
7916 {
7917 Lisp_Object tail, frame;
7918
7919 FOR_EACH_FRAME (tail, frame)
7920 {
7921 f = XFRAME (frame);
7922 if (!EQ (frame, tooltip_frame)
7923 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7924 x_consider_frame_title (frame);
7925 }
7926 }
7927 #endif /* HAVE_WINDOW_SYSTEM */
7928
7929 /* Update the menu bar item lists, if appropriate. This has to be
7930 done before any actual redisplay or generation of display lines. */
7931 all_windows = (update_mode_lines
7932 || buffer_shared > 1
7933 || windows_or_buffers_changed);
7934 if (all_windows)
7935 {
7936 Lisp_Object tail, frame;
7937 int count = SPECPDL_INDEX ();
7938
7939 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7940
7941 FOR_EACH_FRAME (tail, frame)
7942 {
7943 f = XFRAME (frame);
7944
7945 /* Ignore tooltip frame. */
7946 if (EQ (frame, tooltip_frame))
7947 continue;
7948
7949 /* If a window on this frame changed size, report that to
7950 the user and clear the size-change flag. */
7951 if (FRAME_WINDOW_SIZES_CHANGED (f))
7952 {
7953 Lisp_Object functions;
7954
7955 /* Clear flag first in case we get an error below. */
7956 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7957 functions = Vwindow_size_change_functions;
7958 GCPRO2 (tail, functions);
7959
7960 while (CONSP (functions))
7961 {
7962 call1 (XCAR (functions), frame);
7963 functions = XCDR (functions);
7964 }
7965 UNGCPRO;
7966 }
7967
7968 GCPRO1 (tail);
7969 update_menu_bar (f, 0);
7970 #ifdef HAVE_WINDOW_SYSTEM
7971 update_tool_bar (f, 0);
7972 #endif
7973 UNGCPRO;
7974 }
7975
7976 unbind_to (count, Qnil);
7977 }
7978 else
7979 {
7980 struct frame *sf = SELECTED_FRAME ();
7981 update_menu_bar (sf, 1);
7982 #ifdef HAVE_WINDOW_SYSTEM
7983 update_tool_bar (sf, 1);
7984 #endif
7985 }
7986
7987 /* Motif needs this. See comment in xmenu.c. Turn it off when
7988 pending_menu_activation is not defined. */
7989 #ifdef USE_X_TOOLKIT
7990 pending_menu_activation = 0;
7991 #endif
7992 }
7993
7994
7995 /* Update the menu bar item list for frame F. This has to be done
7996 before we start to fill in any display lines, because it can call
7997 eval.
7998
7999 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8000
8001 static void
8002 update_menu_bar (f, save_match_data)
8003 struct frame *f;
8004 int save_match_data;
8005 {
8006 Lisp_Object window;
8007 register struct window *w;
8008
8009 /* If called recursively during a menu update, do nothing. This can
8010 happen when, for instance, an activate-menubar-hook causes a
8011 redisplay. */
8012 if (inhibit_menubar_update)
8013 return;
8014
8015 window = FRAME_SELECTED_WINDOW (f);
8016 w = XWINDOW (window);
8017
8018 #if 0 /* The if statement below this if statement used to include the
8019 condition !NILP (w->update_mode_line), rather than using
8020 update_mode_lines directly, and this if statement may have
8021 been added to make that condition work. Now the if
8022 statement below matches its comment, this isn't needed. */
8023 if (update_mode_lines)
8024 w->update_mode_line = Qt;
8025 #endif
8026
8027 if (FRAME_WINDOW_P (f)
8028 ?
8029 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8030 || defined (USE_GTK)
8031 FRAME_EXTERNAL_MENU_BAR (f)
8032 #else
8033 FRAME_MENU_BAR_LINES (f) > 0
8034 #endif
8035 : FRAME_MENU_BAR_LINES (f) > 0)
8036 {
8037 /* If the user has switched buffers or windows, we need to
8038 recompute to reflect the new bindings. But we'll
8039 recompute when update_mode_lines is set too; that means
8040 that people can use force-mode-line-update to request
8041 that the menu bar be recomputed. The adverse effect on
8042 the rest of the redisplay algorithm is about the same as
8043 windows_or_buffers_changed anyway. */
8044 if (windows_or_buffers_changed
8045 /* This used to test w->update_mode_line, but we believe
8046 there is no need to recompute the menu in that case. */
8047 || update_mode_lines
8048 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8049 < BUF_MODIFF (XBUFFER (w->buffer)))
8050 != !NILP (w->last_had_star))
8051 || ((!NILP (Vtransient_mark_mode)
8052 && !NILP (XBUFFER (w->buffer)->mark_active))
8053 != !NILP (w->region_showing)))
8054 {
8055 struct buffer *prev = current_buffer;
8056 int count = SPECPDL_INDEX ();
8057
8058 specbind (Qinhibit_menubar_update, Qt);
8059
8060 set_buffer_internal_1 (XBUFFER (w->buffer));
8061 if (save_match_data)
8062 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8063 if (NILP (Voverriding_local_map_menu_flag))
8064 {
8065 specbind (Qoverriding_terminal_local_map, Qnil);
8066 specbind (Qoverriding_local_map, Qnil);
8067 }
8068
8069 /* Run the Lucid hook. */
8070 safe_run_hooks (Qactivate_menubar_hook);
8071
8072 /* If it has changed current-menubar from previous value,
8073 really recompute the menu-bar from the value. */
8074 if (! NILP (Vlucid_menu_bar_dirty_flag))
8075 call0 (Qrecompute_lucid_menubar);
8076
8077 safe_run_hooks (Qmenu_bar_update_hook);
8078 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8079
8080 /* Redisplay the menu bar in case we changed it. */
8081 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8082 || defined (USE_GTK)
8083 if (FRAME_WINDOW_P (f)
8084 #if defined (MAC_OS)
8085 /* All frames on Mac OS share the same menubar. So only the
8086 selected frame should be allowed to set it. */
8087 && f == SELECTED_FRAME ()
8088 #endif
8089 )
8090 set_frame_menubar (f, 0, 0);
8091 else
8092 /* On a terminal screen, the menu bar is an ordinary screen
8093 line, and this makes it get updated. */
8094 w->update_mode_line = Qt;
8095 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8096 /* In the non-toolkit version, the menu bar is an ordinary screen
8097 line, and this makes it get updated. */
8098 w->update_mode_line = Qt;
8099 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8100
8101 unbind_to (count, Qnil);
8102 set_buffer_internal_1 (prev);
8103 }
8104 }
8105 }
8106
8107
8108 \f
8109 /***********************************************************************
8110 Output Cursor
8111 ***********************************************************************/
8112
8113 #ifdef HAVE_WINDOW_SYSTEM
8114
8115 /* EXPORT:
8116 Nominal cursor position -- where to draw output.
8117 HPOS and VPOS are window relative glyph matrix coordinates.
8118 X and Y are window relative pixel coordinates. */
8119
8120 struct cursor_pos output_cursor;
8121
8122
8123 /* EXPORT:
8124 Set the global variable output_cursor to CURSOR. All cursor
8125 positions are relative to updated_window. */
8126
8127 void
8128 set_output_cursor (cursor)
8129 struct cursor_pos *cursor;
8130 {
8131 output_cursor.hpos = cursor->hpos;
8132 output_cursor.vpos = cursor->vpos;
8133 output_cursor.x = cursor->x;
8134 output_cursor.y = cursor->y;
8135 }
8136
8137
8138 /* EXPORT for RIF:
8139 Set a nominal cursor position.
8140
8141 HPOS and VPOS are column/row positions in a window glyph matrix. X
8142 and Y are window text area relative pixel positions.
8143
8144 If this is done during an update, updated_window will contain the
8145 window that is being updated and the position is the future output
8146 cursor position for that window. If updated_window is null, use
8147 selected_window and display the cursor at the given position. */
8148
8149 void
8150 x_cursor_to (vpos, hpos, y, x)
8151 int vpos, hpos, y, x;
8152 {
8153 struct window *w;
8154
8155 /* If updated_window is not set, work on selected_window. */
8156 if (updated_window)
8157 w = updated_window;
8158 else
8159 w = XWINDOW (selected_window);
8160
8161 /* Set the output cursor. */
8162 output_cursor.hpos = hpos;
8163 output_cursor.vpos = vpos;
8164 output_cursor.x = x;
8165 output_cursor.y = y;
8166
8167 /* If not called as part of an update, really display the cursor.
8168 This will also set the cursor position of W. */
8169 if (updated_window == NULL)
8170 {
8171 BLOCK_INPUT;
8172 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8173 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8174 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8175 UNBLOCK_INPUT;
8176 }
8177 }
8178
8179 #endif /* HAVE_WINDOW_SYSTEM */
8180
8181 \f
8182 /***********************************************************************
8183 Tool-bars
8184 ***********************************************************************/
8185
8186 #ifdef HAVE_WINDOW_SYSTEM
8187
8188 /* Where the mouse was last time we reported a mouse event. */
8189
8190 FRAME_PTR last_mouse_frame;
8191
8192 /* Tool-bar item index of the item on which a mouse button was pressed
8193 or -1. */
8194
8195 int last_tool_bar_item;
8196
8197
8198 /* Update the tool-bar item list for frame F. This has to be done
8199 before we start to fill in any display lines. Called from
8200 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8201 and restore it here. */
8202
8203 static void
8204 update_tool_bar (f, save_match_data)
8205 struct frame *f;
8206 int save_match_data;
8207 {
8208 #ifdef USE_GTK
8209 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8210 #else
8211 int do_update = WINDOWP (f->tool_bar_window)
8212 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8213 #endif
8214
8215 if (do_update)
8216 {
8217 Lisp_Object window;
8218 struct window *w;
8219
8220 window = FRAME_SELECTED_WINDOW (f);
8221 w = XWINDOW (window);
8222
8223 /* If the user has switched buffers or windows, we need to
8224 recompute to reflect the new bindings. But we'll
8225 recompute when update_mode_lines is set too; that means
8226 that people can use force-mode-line-update to request
8227 that the menu bar be recomputed. The adverse effect on
8228 the rest of the redisplay algorithm is about the same as
8229 windows_or_buffers_changed anyway. */
8230 if (windows_or_buffers_changed
8231 || !NILP (w->update_mode_line)
8232 || update_mode_lines
8233 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8234 < BUF_MODIFF (XBUFFER (w->buffer)))
8235 != !NILP (w->last_had_star))
8236 || ((!NILP (Vtransient_mark_mode)
8237 && !NILP (XBUFFER (w->buffer)->mark_active))
8238 != !NILP (w->region_showing)))
8239 {
8240 struct buffer *prev = current_buffer;
8241 int count = SPECPDL_INDEX ();
8242 Lisp_Object old_tool_bar;
8243 struct gcpro gcpro1;
8244
8245 /* Set current_buffer to the buffer of the selected
8246 window of the frame, so that we get the right local
8247 keymaps. */
8248 set_buffer_internal_1 (XBUFFER (w->buffer));
8249
8250 /* Save match data, if we must. */
8251 if (save_match_data)
8252 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8253
8254 /* Make sure that we don't accidentally use bogus keymaps. */
8255 if (NILP (Voverriding_local_map_menu_flag))
8256 {
8257 specbind (Qoverriding_terminal_local_map, Qnil);
8258 specbind (Qoverriding_local_map, Qnil);
8259 }
8260
8261 old_tool_bar = f->tool_bar_items;
8262 GCPRO1 (old_tool_bar);
8263
8264 /* Build desired tool-bar items from keymaps. */
8265 BLOCK_INPUT;
8266 f->tool_bar_items
8267 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8268 UNBLOCK_INPUT;
8269
8270 /* Redisplay the tool-bar if we changed it. */
8271 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8272 w->update_mode_line = Qt;
8273
8274 UNGCPRO;
8275
8276 unbind_to (count, Qnil);
8277 set_buffer_internal_1 (prev);
8278 }
8279 }
8280 }
8281
8282
8283 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8284 F's desired tool-bar contents. F->tool_bar_items must have
8285 been set up previously by calling prepare_menu_bars. */
8286
8287 static void
8288 build_desired_tool_bar_string (f)
8289 struct frame *f;
8290 {
8291 int i, size, size_needed;
8292 struct gcpro gcpro1, gcpro2, gcpro3;
8293 Lisp_Object image, plist, props;
8294
8295 image = plist = props = Qnil;
8296 GCPRO3 (image, plist, props);
8297
8298 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8299 Otherwise, make a new string. */
8300
8301 /* The size of the string we might be able to reuse. */
8302 size = (STRINGP (f->desired_tool_bar_string)
8303 ? SCHARS (f->desired_tool_bar_string)
8304 : 0);
8305
8306 /* We need one space in the string for each image. */
8307 size_needed = f->n_tool_bar_items;
8308
8309 /* Reuse f->desired_tool_bar_string, if possible. */
8310 if (size < size_needed || NILP (f->desired_tool_bar_string))
8311 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8312 make_number (' '));
8313 else
8314 {
8315 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8316 Fremove_text_properties (make_number (0), make_number (size),
8317 props, f->desired_tool_bar_string);
8318 }
8319
8320 /* Put a `display' property on the string for the images to display,
8321 put a `menu_item' property on tool-bar items with a value that
8322 is the index of the item in F's tool-bar item vector. */
8323 for (i = 0; i < f->n_tool_bar_items; ++i)
8324 {
8325 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8326
8327 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8328 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8329 int hmargin, vmargin, relief, idx, end;
8330 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8331
8332 /* If image is a vector, choose the image according to the
8333 button state. */
8334 image = PROP (TOOL_BAR_ITEM_IMAGES);
8335 if (VECTORP (image))
8336 {
8337 if (enabled_p)
8338 idx = (selected_p
8339 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8340 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8341 else
8342 idx = (selected_p
8343 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8344 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8345
8346 xassert (ASIZE (image) >= idx);
8347 image = AREF (image, idx);
8348 }
8349 else
8350 idx = -1;
8351
8352 /* Ignore invalid image specifications. */
8353 if (!valid_image_p (image))
8354 continue;
8355
8356 /* Display the tool-bar button pressed, or depressed. */
8357 plist = Fcopy_sequence (XCDR (image));
8358
8359 /* Compute margin and relief to draw. */
8360 relief = (tool_bar_button_relief >= 0
8361 ? tool_bar_button_relief
8362 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8363 hmargin = vmargin = relief;
8364
8365 if (INTEGERP (Vtool_bar_button_margin)
8366 && XINT (Vtool_bar_button_margin) > 0)
8367 {
8368 hmargin += XFASTINT (Vtool_bar_button_margin);
8369 vmargin += XFASTINT (Vtool_bar_button_margin);
8370 }
8371 else if (CONSP (Vtool_bar_button_margin))
8372 {
8373 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8374 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8375 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8376
8377 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8378 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8379 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8380 }
8381
8382 if (auto_raise_tool_bar_buttons_p)
8383 {
8384 /* Add a `:relief' property to the image spec if the item is
8385 selected. */
8386 if (selected_p)
8387 {
8388 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8389 hmargin -= relief;
8390 vmargin -= relief;
8391 }
8392 }
8393 else
8394 {
8395 /* If image is selected, display it pressed, i.e. with a
8396 negative relief. If it's not selected, display it with a
8397 raised relief. */
8398 plist = Fplist_put (plist, QCrelief,
8399 (selected_p
8400 ? make_number (-relief)
8401 : make_number (relief)));
8402 hmargin -= relief;
8403 vmargin -= relief;
8404 }
8405
8406 /* Put a margin around the image. */
8407 if (hmargin || vmargin)
8408 {
8409 if (hmargin == vmargin)
8410 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8411 else
8412 plist = Fplist_put (plist, QCmargin,
8413 Fcons (make_number (hmargin),
8414 make_number (vmargin)));
8415 }
8416
8417 /* If button is not enabled, and we don't have special images
8418 for the disabled state, make the image appear disabled by
8419 applying an appropriate algorithm to it. */
8420 if (!enabled_p && idx < 0)
8421 plist = Fplist_put (plist, QCconversion, Qdisabled);
8422
8423 /* Put a `display' text property on the string for the image to
8424 display. Put a `menu-item' property on the string that gives
8425 the start of this item's properties in the tool-bar items
8426 vector. */
8427 image = Fcons (Qimage, plist);
8428 props = list4 (Qdisplay, image,
8429 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8430
8431 /* Let the last image hide all remaining spaces in the tool bar
8432 string. The string can be longer than needed when we reuse a
8433 previous string. */
8434 if (i + 1 == f->n_tool_bar_items)
8435 end = SCHARS (f->desired_tool_bar_string);
8436 else
8437 end = i + 1;
8438 Fadd_text_properties (make_number (i), make_number (end),
8439 props, f->desired_tool_bar_string);
8440 #undef PROP
8441 }
8442
8443 UNGCPRO;
8444 }
8445
8446
8447 /* Display one line of the tool-bar of frame IT->f. */
8448
8449 static void
8450 display_tool_bar_line (it)
8451 struct it *it;
8452 {
8453 struct glyph_row *row = it->glyph_row;
8454 int max_x = it->last_visible_x;
8455 struct glyph *last;
8456
8457 prepare_desired_row (row);
8458 row->y = it->current_y;
8459
8460 /* Note that this isn't made use of if the face hasn't a box,
8461 so there's no need to check the face here. */
8462 it->start_of_box_run_p = 1;
8463
8464 while (it->current_x < max_x)
8465 {
8466 int x_before, x, n_glyphs_before, i, nglyphs;
8467
8468 /* Get the next display element. */
8469 if (!get_next_display_element (it))
8470 break;
8471
8472 /* Produce glyphs. */
8473 x_before = it->current_x;
8474 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8475 PRODUCE_GLYPHS (it);
8476
8477 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8478 i = 0;
8479 x = x_before;
8480 while (i < nglyphs)
8481 {
8482 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8483
8484 if (x + glyph->pixel_width > max_x)
8485 {
8486 /* Glyph doesn't fit on line. */
8487 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8488 it->current_x = x;
8489 goto out;
8490 }
8491
8492 ++it->hpos;
8493 x += glyph->pixel_width;
8494 ++i;
8495 }
8496
8497 /* Stop at line ends. */
8498 if (ITERATOR_AT_END_OF_LINE_P (it))
8499 break;
8500
8501 set_iterator_to_next (it, 1);
8502 }
8503
8504 out:;
8505
8506 row->displays_text_p = row->used[TEXT_AREA] != 0;
8507 extend_face_to_end_of_line (it);
8508 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8509 last->right_box_line_p = 1;
8510 if (last == row->glyphs[TEXT_AREA])
8511 last->left_box_line_p = 1;
8512 compute_line_metrics (it);
8513
8514 /* If line is empty, make it occupy the rest of the tool-bar. */
8515 if (!row->displays_text_p)
8516 {
8517 row->height = row->phys_height = it->last_visible_y - row->y;
8518 row->ascent = row->phys_ascent = 0;
8519 }
8520
8521 row->full_width_p = 1;
8522 row->continued_p = 0;
8523 row->truncated_on_left_p = 0;
8524 row->truncated_on_right_p = 0;
8525
8526 it->current_x = it->hpos = 0;
8527 it->current_y += row->height;
8528 ++it->vpos;
8529 ++it->glyph_row;
8530 }
8531
8532
8533 /* Value is the number of screen lines needed to make all tool-bar
8534 items of frame F visible. */
8535
8536 static int
8537 tool_bar_lines_needed (f)
8538 struct frame *f;
8539 {
8540 struct window *w = XWINDOW (f->tool_bar_window);
8541 struct it it;
8542
8543 /* Initialize an iterator for iteration over
8544 F->desired_tool_bar_string in the tool-bar window of frame F. */
8545 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8546 it.first_visible_x = 0;
8547 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8548 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8549
8550 while (!ITERATOR_AT_END_P (&it))
8551 {
8552 it.glyph_row = w->desired_matrix->rows;
8553 clear_glyph_row (it.glyph_row);
8554 display_tool_bar_line (&it);
8555 }
8556
8557 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8558 }
8559
8560
8561 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8562 0, 1, 0,
8563 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8564 (frame)
8565 Lisp_Object frame;
8566 {
8567 struct frame *f;
8568 struct window *w;
8569 int nlines = 0;
8570
8571 if (NILP (frame))
8572 frame = selected_frame;
8573 else
8574 CHECK_FRAME (frame);
8575 f = XFRAME (frame);
8576
8577 if (WINDOWP (f->tool_bar_window)
8578 || (w = XWINDOW (f->tool_bar_window),
8579 WINDOW_TOTAL_LINES (w) > 0))
8580 {
8581 update_tool_bar (f, 1);
8582 if (f->n_tool_bar_items)
8583 {
8584 build_desired_tool_bar_string (f);
8585 nlines = tool_bar_lines_needed (f);
8586 }
8587 }
8588
8589 return make_number (nlines);
8590 }
8591
8592
8593 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8594 height should be changed. */
8595
8596 static int
8597 redisplay_tool_bar (f)
8598 struct frame *f;
8599 {
8600 struct window *w;
8601 struct it it;
8602 struct glyph_row *row;
8603 int change_height_p = 0;
8604
8605 #ifdef USE_GTK
8606 if (FRAME_EXTERNAL_TOOL_BAR (f))
8607 update_frame_tool_bar (f);
8608 return 0;
8609 #endif
8610
8611 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8612 do anything. This means you must start with tool-bar-lines
8613 non-zero to get the auto-sizing effect. Or in other words, you
8614 can turn off tool-bars by specifying tool-bar-lines zero. */
8615 if (!WINDOWP (f->tool_bar_window)
8616 || (w = XWINDOW (f->tool_bar_window),
8617 WINDOW_TOTAL_LINES (w) == 0))
8618 return 0;
8619
8620 /* Set up an iterator for the tool-bar window. */
8621 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8622 it.first_visible_x = 0;
8623 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8624 row = it.glyph_row;
8625
8626 /* Build a string that represents the contents of the tool-bar. */
8627 build_desired_tool_bar_string (f);
8628 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8629
8630 /* Display as many lines as needed to display all tool-bar items. */
8631 while (it.current_y < it.last_visible_y)
8632 display_tool_bar_line (&it);
8633
8634 /* It doesn't make much sense to try scrolling in the tool-bar
8635 window, so don't do it. */
8636 w->desired_matrix->no_scrolling_p = 1;
8637 w->must_be_updated_p = 1;
8638
8639 if (auto_resize_tool_bars_p)
8640 {
8641 int nlines;
8642
8643 /* If we couldn't display everything, change the tool-bar's
8644 height. */
8645 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8646 change_height_p = 1;
8647
8648 /* If there are blank lines at the end, except for a partially
8649 visible blank line at the end that is smaller than
8650 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8651 row = it.glyph_row - 1;
8652 if (!row->displays_text_p
8653 && row->height >= FRAME_LINE_HEIGHT (f))
8654 change_height_p = 1;
8655
8656 /* If row displays tool-bar items, but is partially visible,
8657 change the tool-bar's height. */
8658 if (row->displays_text_p
8659 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8660 change_height_p = 1;
8661
8662 /* Resize windows as needed by changing the `tool-bar-lines'
8663 frame parameter. */
8664 if (change_height_p
8665 && (nlines = tool_bar_lines_needed (f),
8666 nlines != WINDOW_TOTAL_LINES (w)))
8667 {
8668 extern Lisp_Object Qtool_bar_lines;
8669 Lisp_Object frame;
8670 int old_height = WINDOW_TOTAL_LINES (w);
8671
8672 XSETFRAME (frame, f);
8673 clear_glyph_matrix (w->desired_matrix);
8674 Fmodify_frame_parameters (frame,
8675 Fcons (Fcons (Qtool_bar_lines,
8676 make_number (nlines)),
8677 Qnil));
8678 if (WINDOW_TOTAL_LINES (w) != old_height)
8679 fonts_changed_p = 1;
8680 }
8681 }
8682
8683 return change_height_p;
8684 }
8685
8686
8687 /* Get information about the tool-bar item which is displayed in GLYPH
8688 on frame F. Return in *PROP_IDX the index where tool-bar item
8689 properties start in F->tool_bar_items. Value is zero if
8690 GLYPH doesn't display a tool-bar item. */
8691
8692 static int
8693 tool_bar_item_info (f, glyph, prop_idx)
8694 struct frame *f;
8695 struct glyph *glyph;
8696 int *prop_idx;
8697 {
8698 Lisp_Object prop;
8699 int success_p;
8700 int charpos;
8701
8702 /* This function can be called asynchronously, which means we must
8703 exclude any possibility that Fget_text_property signals an
8704 error. */
8705 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8706 charpos = max (0, charpos);
8707
8708 /* Get the text property `menu-item' at pos. The value of that
8709 property is the start index of this item's properties in
8710 F->tool_bar_items. */
8711 prop = Fget_text_property (make_number (charpos),
8712 Qmenu_item, f->current_tool_bar_string);
8713 if (INTEGERP (prop))
8714 {
8715 *prop_idx = XINT (prop);
8716 success_p = 1;
8717 }
8718 else
8719 success_p = 0;
8720
8721 return success_p;
8722 }
8723
8724 \f
8725 /* Get information about the tool-bar item at position X/Y on frame F.
8726 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8727 the current matrix of the tool-bar window of F, or NULL if not
8728 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8729 item in F->tool_bar_items. Value is
8730
8731 -1 if X/Y is not on a tool-bar item
8732 0 if X/Y is on the same item that was highlighted before.
8733 1 otherwise. */
8734
8735 static int
8736 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8737 struct frame *f;
8738 int x, y;
8739 struct glyph **glyph;
8740 int *hpos, *vpos, *prop_idx;
8741 {
8742 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8743 struct window *w = XWINDOW (f->tool_bar_window);
8744 int area;
8745
8746 /* Find the glyph under X/Y. */
8747 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8748 if (*glyph == NULL)
8749 return -1;
8750
8751 /* Get the start of this tool-bar item's properties in
8752 f->tool_bar_items. */
8753 if (!tool_bar_item_info (f, *glyph, prop_idx))
8754 return -1;
8755
8756 /* Is mouse on the highlighted item? */
8757 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8758 && *vpos >= dpyinfo->mouse_face_beg_row
8759 && *vpos <= dpyinfo->mouse_face_end_row
8760 && (*vpos > dpyinfo->mouse_face_beg_row
8761 || *hpos >= dpyinfo->mouse_face_beg_col)
8762 && (*vpos < dpyinfo->mouse_face_end_row
8763 || *hpos < dpyinfo->mouse_face_end_col
8764 || dpyinfo->mouse_face_past_end))
8765 return 0;
8766
8767 return 1;
8768 }
8769
8770
8771 /* EXPORT:
8772 Handle mouse button event on the tool-bar of frame F, at
8773 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8774 0 for button release. MODIFIERS is event modifiers for button
8775 release. */
8776
8777 void
8778 handle_tool_bar_click (f, x, y, down_p, modifiers)
8779 struct frame *f;
8780 int x, y, down_p;
8781 unsigned int modifiers;
8782 {
8783 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8784 struct window *w = XWINDOW (f->tool_bar_window);
8785 int hpos, vpos, prop_idx;
8786 struct glyph *glyph;
8787 Lisp_Object enabled_p;
8788
8789 /* If not on the highlighted tool-bar item, return. */
8790 frame_to_window_pixel_xy (w, &x, &y);
8791 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8792 return;
8793
8794 /* If item is disabled, do nothing. */
8795 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8796 if (NILP (enabled_p))
8797 return;
8798
8799 if (down_p)
8800 {
8801 /* Show item in pressed state. */
8802 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8803 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8804 last_tool_bar_item = prop_idx;
8805 }
8806 else
8807 {
8808 Lisp_Object key, frame;
8809 struct input_event event;
8810 EVENT_INIT (event);
8811
8812 /* Show item in released state. */
8813 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8814 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8815
8816 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8817
8818 XSETFRAME (frame, f);
8819 event.kind = TOOL_BAR_EVENT;
8820 event.frame_or_window = frame;
8821 event.arg = frame;
8822 kbd_buffer_store_event (&event);
8823
8824 event.kind = TOOL_BAR_EVENT;
8825 event.frame_or_window = frame;
8826 event.arg = key;
8827 event.modifiers = modifiers;
8828 kbd_buffer_store_event (&event);
8829 last_tool_bar_item = -1;
8830 }
8831 }
8832
8833
8834 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8835 tool-bar window-relative coordinates X/Y. Called from
8836 note_mouse_highlight. */
8837
8838 static void
8839 note_tool_bar_highlight (f, x, y)
8840 struct frame *f;
8841 int x, y;
8842 {
8843 Lisp_Object window = f->tool_bar_window;
8844 struct window *w = XWINDOW (window);
8845 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8846 int hpos, vpos;
8847 struct glyph *glyph;
8848 struct glyph_row *row;
8849 int i;
8850 Lisp_Object enabled_p;
8851 int prop_idx;
8852 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8853 int mouse_down_p, rc;
8854
8855 /* Function note_mouse_highlight is called with negative x(y
8856 values when mouse moves outside of the frame. */
8857 if (x <= 0 || y <= 0)
8858 {
8859 clear_mouse_face (dpyinfo);
8860 return;
8861 }
8862
8863 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8864 if (rc < 0)
8865 {
8866 /* Not on tool-bar item. */
8867 clear_mouse_face (dpyinfo);
8868 return;
8869 }
8870 else if (rc == 0)
8871 /* On same tool-bar item as before. */
8872 goto set_help_echo;
8873
8874 clear_mouse_face (dpyinfo);
8875
8876 /* Mouse is down, but on different tool-bar item? */
8877 mouse_down_p = (dpyinfo->grabbed
8878 && f == last_mouse_frame
8879 && FRAME_LIVE_P (f));
8880 if (mouse_down_p
8881 && last_tool_bar_item != prop_idx)
8882 return;
8883
8884 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8885 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8886
8887 /* If tool-bar item is not enabled, don't highlight it. */
8888 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8889 if (!NILP (enabled_p))
8890 {
8891 /* Compute the x-position of the glyph. In front and past the
8892 image is a space. We include this in the highlighted area. */
8893 row = MATRIX_ROW (w->current_matrix, vpos);
8894 for (i = x = 0; i < hpos; ++i)
8895 x += row->glyphs[TEXT_AREA][i].pixel_width;
8896
8897 /* Record this as the current active region. */
8898 dpyinfo->mouse_face_beg_col = hpos;
8899 dpyinfo->mouse_face_beg_row = vpos;
8900 dpyinfo->mouse_face_beg_x = x;
8901 dpyinfo->mouse_face_beg_y = row->y;
8902 dpyinfo->mouse_face_past_end = 0;
8903
8904 dpyinfo->mouse_face_end_col = hpos + 1;
8905 dpyinfo->mouse_face_end_row = vpos;
8906 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8907 dpyinfo->mouse_face_end_y = row->y;
8908 dpyinfo->mouse_face_window = window;
8909 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8910
8911 /* Display it as active. */
8912 show_mouse_face (dpyinfo, draw);
8913 dpyinfo->mouse_face_image_state = draw;
8914 }
8915
8916 set_help_echo:
8917
8918 /* Set help_echo_string to a help string to display for this tool-bar item.
8919 XTread_socket does the rest. */
8920 help_echo_object = help_echo_window = Qnil;
8921 help_echo_pos = -1;
8922 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8923 if (NILP (help_echo_string))
8924 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8925 }
8926
8927 #endif /* HAVE_WINDOW_SYSTEM */
8928
8929
8930 \f
8931 /***********************************************************************
8932 Fringes
8933 ***********************************************************************/
8934
8935 #ifdef HAVE_WINDOW_SYSTEM
8936
8937 /* Notice that all bitmaps bits are "mirrored". */
8938
8939 /* An arrow like this: `<-'. */
8940 /*
8941 ...xx...
8942 ....xx..
8943 .....xx.
8944 ..xxxxxx
8945 ..xxxxxx
8946 .....xx.
8947 ....xx..
8948 ...xx...
8949 */
8950 static unsigned char left_bits[] = {
8951 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8952
8953
8954 /* Right truncation arrow bitmap `->'. */
8955 /*
8956 ...xx...
8957 ..xx....
8958 .xx.....
8959 xxxxxx..
8960 xxxxxx..
8961 .xx.....
8962 ..xx....
8963 ...xx...
8964 */
8965 static unsigned char right_bits[] = {
8966 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8967
8968
8969 /* Up arrow bitmap. */
8970 /*
8971 ...xx...
8972 ..xxxx..
8973 .xxxxxx.
8974 xxxxxxxx
8975 ...xx...
8976 ...xx...
8977 ...xx...
8978 ...xx...
8979 */
8980 static unsigned char up_arrow_bits[] = {
8981 0x18, 0x3c, 0x7e, 0xff, 0x18, 0x18, 0x18, 0x18};
8982
8983
8984 /* Down arrow bitmap. */
8985 /*
8986 ...xx...
8987 ...xx...
8988 ...xx...
8989 ...xx...
8990 xxxxxxxx
8991 .xxxxxx.
8992 ..xxxx..
8993 ...xx...
8994 */
8995 static unsigned char down_arrow_bits[] = {
8996 0x18, 0x18, 0x18, 0x18, 0xff, 0x7e, 0x3c, 0x18};
8997
8998 /* Marker for continued lines. */
8999 /*
9000 ..xxxx..
9001 .xxxxx..
9002 xx......
9003 xxx..x..
9004 xxxxxx..
9005 .xxxxx..
9006 ..xxxx..
9007 .xxxxx..
9008 */
9009 static unsigned char continued_bits[] = {
9010 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
9011
9012 /* Marker for continuation lines. */
9013 /*
9014 ..xxxx..
9015 ..xxxxx.
9016 ......xx
9017 ..x..xxx
9018 ..xxxxxx
9019 ..xxxxx.
9020 ..xxxx..
9021 ..xxxxx.
9022 */
9023 static unsigned char continuation_bits[] = {
9024 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
9025
9026 /* Overlay arrow bitmap. A triangular arrow. */
9027 /*
9028 ......xx
9029 ....xxxx
9030 ...xxxxx
9031 ..xxxxxx
9032 ..xxxxxx
9033 ...xxxxx
9034 ....xxxx
9035 ......xx
9036 */
9037 static unsigned char ov_bits[] = {
9038 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
9039
9040
9041 /* First line bitmap. An left-up angle. */
9042 /*
9043 ..xxxxxx
9044 ..xxxxxx
9045 ......xx
9046 ......xx
9047 ......xx
9048 ......xx
9049 ......xx
9050 ........
9051 */
9052 static unsigned char first_line_bits[] = {
9053 0x3f, 0x3f, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00};
9054
9055
9056 /* Last line bitmap. An left-down angle. */
9057 /*
9058 ........
9059 xx......
9060 xx......
9061 xx......
9062 xx......
9063 xx......
9064 xxxxxx..
9065 xxxxxx..
9066 */
9067 static unsigned char last_line_bits[] = {
9068 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xfc};
9069
9070 /* Filled box cursor bitmap. A filled box; max 13 pixels high. */
9071 /*
9072 .xxxxxxx
9073 .xxxxxxx
9074 .xxxxxxx
9075 .xxxxxxx
9076 .xxxxxxx
9077 .xxxxxxx
9078 .xxxxxxx
9079 .xxxxxxx
9080 .xxxxxxx
9081 .xxxxxxx
9082 .xxxxxxx
9083 .xxxxxxx
9084 .xxxxxxx
9085 */
9086 static unsigned char filled_box_cursor_bits[] = {
9087 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f};
9088
9089 /* Hollow box cursor bitmap. A hollow box; max 13 pixels high. */
9090 /*
9091 .xxxxxxx
9092 .x.....x
9093 .x.....x
9094 .x.....x
9095 .x.....x
9096 .x.....x
9097 .x.....x
9098 .x.....x
9099 .x.....x
9100 .x.....x
9101 .x.....x
9102 .x.....x
9103 .xxxxxxx
9104 */
9105 static unsigned char hollow_box_cursor_bits[] = {
9106 0x7f, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x7f};
9107
9108 /* Bar cursor bitmap. A vertical bar; max 13 pixels high. */
9109 /*
9110 ......xx
9111 ......xx
9112 ......xx
9113 ......xx
9114 ......xx
9115 ......xx
9116 ......xx
9117 ......xx
9118 ......xx
9119 ......xx
9120 ......xx
9121 ......xx
9122 ......xx
9123 */
9124 static unsigned char bar_cursor_bits[] = {
9125 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
9126
9127 /* HBar cursor bitmap. A horisontal bar; 2 pixels high. */
9128 /*
9129 .xxxxxxx
9130 .xxxxxxx
9131 */
9132 static unsigned char hbar_cursor_bits[] = {
9133 0x7f, 0x7f};
9134
9135
9136 /* Bitmap drawn to indicate lines not displaying text if
9137 `indicate-empty-lines' is non-nil. */
9138 static unsigned char zv_bits[] = {
9139 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
9140 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
9141 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
9142 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
9143 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
9144 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
9145 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
9146 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
9147
9148 /* Hollow square bitmap. */
9149 /*
9150 .xxxxxx.
9151 .x....x.
9152 .x....x.
9153 .x....x.
9154 .x....x.
9155 .xxxxxx.
9156 */
9157 static unsigned char hollow_square_bits[] = {
9158 0x7e, 0x42, 0x42, 0x42, 0x42, 0x7e};
9159
9160
9161 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
9162 {
9163 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
9164 { 8, sizeof (left_bits), 0, left_bits },
9165 { 8, sizeof (right_bits), 0, right_bits },
9166 { 8, sizeof (up_arrow_bits), -1, up_arrow_bits },
9167 { 8, sizeof (down_arrow_bits), -2, down_arrow_bits },
9168 { 8, sizeof (continued_bits), 0, continued_bits },
9169 { 8, sizeof (continuation_bits), 0, continuation_bits },
9170 { 8, sizeof (ov_bits), 0, ov_bits },
9171 { 8, sizeof (first_line_bits), -1, first_line_bits },
9172 { 8, sizeof (last_line_bits), -2, last_line_bits },
9173 { 8, sizeof (filled_box_cursor_bits), 0, filled_box_cursor_bits },
9174 { 8, sizeof (hollow_box_cursor_bits), 0, hollow_box_cursor_bits },
9175 { 8, sizeof (bar_cursor_bits), 0, bar_cursor_bits },
9176 { 8, sizeof (hbar_cursor_bits), -2, hbar_cursor_bits },
9177 { 8, sizeof (zv_bits), 3, zv_bits },
9178 { 8, sizeof (hollow_square_bits), 0, hollow_square_bits },
9179 };
9180
9181
9182 /* Draw the bitmap WHICH in one of the left or right fringes of
9183 window W. ROW is the glyph row for which to display the bitmap; it
9184 determines the vertical position at which the bitmap has to be
9185 drawn.
9186 LEFT_P is 1 for left fringe, 0 for right fringe.
9187 */
9188
9189 void
9190 draw_fringe_bitmap (w, row, left_p)
9191 struct window *w;
9192 struct glyph_row *row;
9193 int left_p;
9194 {
9195 struct frame *f = XFRAME (WINDOW_FRAME (w));
9196 struct draw_fringe_bitmap_params p;
9197 enum fringe_bitmap_type which;
9198 int period;
9199
9200 if (left_p)
9201 which = row->left_fringe_bitmap;
9202 else if (!row->cursor_in_fringe_p)
9203 which = row->right_fringe_bitmap;
9204 else
9205 switch (w->phys_cursor_type)
9206 {
9207 case HOLLOW_BOX_CURSOR:
9208 if (row->visible_height >= sizeof(hollow_box_cursor_bits))
9209 which = HOLLOW_BOX_CURSOR_BITMAP;
9210 else
9211 which = HOLLOW_SQUARE_BITMAP;
9212 break;
9213 case FILLED_BOX_CURSOR:
9214 which = FILLED_BOX_CURSOR_BITMAP;
9215 break;
9216 case BAR_CURSOR:
9217 which = BAR_CURSOR_BITMAP;
9218 break;
9219 case HBAR_CURSOR:
9220 which = HBAR_CURSOR_BITMAP;
9221 break;
9222 case NO_CURSOR:
9223 default:
9224 w->phys_cursor_on_p = 0;
9225 row->cursor_in_fringe_p = 0;
9226 which = row->right_fringe_bitmap;
9227 break;
9228 }
9229
9230 period = fringe_bitmaps[which].period;
9231
9232 /* Convert row to frame coordinates. */
9233 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9234
9235 p.which = which;
9236 p.wd = fringe_bitmaps[which].width;
9237
9238 p.h = fringe_bitmaps[which].height;
9239 p.dh = (period > 0 ? (p.y % period) : 0);
9240 p.h -= p.dh;
9241 /* Clip bitmap if too high. */
9242 if (p.h > row->height)
9243 p.h = row->height;
9244
9245 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
9246 PREPARE_FACE_FOR_DISPLAY (f, p.face);
9247
9248 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
9249 the fringe. */
9250 p.bx = -1;
9251 if (left_p)
9252 {
9253 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
9254 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
9255 ? LEFT_MARGIN_AREA
9256 : TEXT_AREA));
9257 if (p.wd > wd)
9258 p.wd = wd;
9259 p.x = x - p.wd - (wd - p.wd) / 2;
9260
9261 if (p.wd < wd || row->height > p.h)
9262 {
9263 /* If W has a vertical border to its left, don't draw over it. */
9264 wd -= ((!WINDOW_LEFTMOST_P (w)
9265 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
9266 ? 1 : 0);
9267 p.bx = x - wd;
9268 p.nx = wd;
9269 }
9270 }
9271 else
9272 {
9273 int x = window_box_right (w,
9274 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
9275 ? RIGHT_MARGIN_AREA
9276 : TEXT_AREA));
9277 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
9278 if (p.wd > wd)
9279 p.wd = wd;
9280 p.x = x + (wd - p.wd) / 2;
9281 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
9282 the fringe. */
9283 if (p.wd < wd || row->height > p.h)
9284 {
9285 p.bx = x;
9286 p.nx = wd;
9287 }
9288 }
9289
9290 if (p.bx >= 0)
9291 {
9292 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
9293
9294 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
9295 p.ny = row->visible_height;
9296 }
9297
9298 /* Adjust y to the offset in the row to start drawing the bitmap. */
9299 if (period == 0)
9300 p.y += (row->height - p.h) / 2;
9301 else if (period == -2)
9302 {
9303 p.h = fringe_bitmaps[which].height;
9304 p.y += (row->visible_height - p.h);
9305 }
9306
9307 FRAME_RIF (f)->draw_fringe_bitmap (w, row, &p);
9308 }
9309
9310 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
9311 function with input blocked. */
9312
9313 void
9314 draw_row_fringe_bitmaps (w, row)
9315 struct window *w;
9316 struct glyph_row *row;
9317 {
9318 xassert (interrupt_input_blocked);
9319
9320 /* If row is completely invisible, because of vscrolling, we
9321 don't have to draw anything. */
9322 if (row->visible_height <= 0)
9323 return;
9324
9325 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
9326 draw_fringe_bitmap (w, row, 1);
9327
9328 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
9329 draw_fringe_bitmap (w, row, 0);
9330 }
9331
9332 /* Draw the fringes of window W. Only fringes for rows marked for
9333 update in redraw_fringe_bitmaps_p are drawn. */
9334
9335 void
9336 draw_window_fringes (w)
9337 struct window *w;
9338 {
9339 struct glyph_row *row;
9340 int yb = window_text_bottom_y (w);
9341 int nrows = w->current_matrix->nrows;
9342 int y = 0, rn;
9343
9344 if (w->pseudo_window_p)
9345 return;
9346
9347 for (y = 0, rn = 0, row = w->current_matrix->rows;
9348 y < yb && rn < nrows;
9349 y += row->height, ++row, ++rn)
9350 {
9351 if (!row->redraw_fringe_bitmaps_p)
9352 continue;
9353 draw_row_fringe_bitmaps (w, row);
9354 row->redraw_fringe_bitmaps_p = 0;
9355 }
9356 }
9357
9358
9359 /* Compute actual fringe widths for frame F.
9360
9361 If REDRAW is 1, redraw F if the fringe settings was actually
9362 modified and F is visible.
9363
9364 Since the combined left and right fringe must occupy an integral
9365 number of columns, we may need to add some pixels to each fringe.
9366 Typically, we add an equal amount (+/- 1 pixel) to each fringe,
9367 but a negative width value is taken literally (after negating it).
9368
9369 We never make the fringes narrower than specified. It is planned
9370 to make fringe bitmaps customizable and expandable, and at that
9371 time, the user will typically specify the minimum number of pixels
9372 needed for his bitmaps, so we shouldn't select anything less than
9373 what is specified.
9374 */
9375
9376 void
9377 compute_fringe_widths (f, redraw)
9378 struct frame *f;
9379 int redraw;
9380 {
9381 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9382 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9383 int o_cols = FRAME_FRINGE_COLS (f);
9384
9385 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9386 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9387 int left_fringe_width, right_fringe_width;
9388
9389 if (!NILP (left_fringe))
9390 left_fringe = Fcdr (left_fringe);
9391 if (!NILP (right_fringe))
9392 right_fringe = Fcdr (right_fringe);
9393
9394 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9395 XINT (left_fringe));
9396 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9397 XINT (right_fringe));
9398
9399 if (left_fringe_width || right_fringe_width)
9400 {
9401 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9402 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9403 int conf_wid = left_wid + right_wid;
9404 int font_wid = FRAME_COLUMN_WIDTH (f);
9405 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9406 int real_wid = cols * font_wid;
9407 if (left_wid && right_wid)
9408 {
9409 if (left_fringe_width < 0)
9410 {
9411 /* Left fringe width is fixed, adjust right fringe if necessary */
9412 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9413 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9414 }
9415 else if (right_fringe_width < 0)
9416 {
9417 /* Right fringe width is fixed, adjust left fringe if necessary */
9418 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9419 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
9420 }
9421 else
9422 {
9423 /* Adjust both fringes with an equal amount.
9424 Note that we are doing integer arithmetic here, so don't
9425 lose a pixel if the total width is an odd number. */
9426 int fill = real_wid - conf_wid;
9427 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9428 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9429 }
9430 }
9431 else if (left_fringe_width)
9432 {
9433 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9434 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9435 }
9436 else
9437 {
9438 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9439 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
9440 }
9441 FRAME_FRINGE_COLS (f) = cols;
9442 }
9443 else
9444 {
9445 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9446 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9447 FRAME_FRINGE_COLS (f) = 0;
9448 }
9449
9450 if (redraw && FRAME_VISIBLE_P (f))
9451 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9452 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9453 o_cols != FRAME_FRINGE_COLS (f))
9454 redraw_frame (f);
9455 }
9456
9457 #endif /* HAVE_WINDOW_SYSTEM */
9458
9459
9460 \f
9461 /************************************************************************
9462 Horizontal scrolling
9463 ************************************************************************/
9464
9465 static int hscroll_window_tree P_ ((Lisp_Object));
9466 static int hscroll_windows P_ ((Lisp_Object));
9467
9468 /* For all leaf windows in the window tree rooted at WINDOW, set their
9469 hscroll value so that PT is (i) visible in the window, and (ii) so
9470 that it is not within a certain margin at the window's left and
9471 right border. Value is non-zero if any window's hscroll has been
9472 changed. */
9473
9474 static int
9475 hscroll_window_tree (window)
9476 Lisp_Object window;
9477 {
9478 int hscrolled_p = 0;
9479 int hscroll_relative_p = FLOATP (Vhscroll_step);
9480 int hscroll_step_abs = 0;
9481 double hscroll_step_rel = 0;
9482
9483 if (hscroll_relative_p)
9484 {
9485 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9486 if (hscroll_step_rel < 0)
9487 {
9488 hscroll_relative_p = 0;
9489 hscroll_step_abs = 0;
9490 }
9491 }
9492 else if (INTEGERP (Vhscroll_step))
9493 {
9494 hscroll_step_abs = XINT (Vhscroll_step);
9495 if (hscroll_step_abs < 0)
9496 hscroll_step_abs = 0;
9497 }
9498 else
9499 hscroll_step_abs = 0;
9500
9501 while (WINDOWP (window))
9502 {
9503 struct window *w = XWINDOW (window);
9504
9505 if (WINDOWP (w->hchild))
9506 hscrolled_p |= hscroll_window_tree (w->hchild);
9507 else if (WINDOWP (w->vchild))
9508 hscrolled_p |= hscroll_window_tree (w->vchild);
9509 else if (w->cursor.vpos >= 0)
9510 {
9511 int h_margin;
9512 int text_area_width;
9513 struct glyph_row *current_cursor_row
9514 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9515 struct glyph_row *desired_cursor_row
9516 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9517 struct glyph_row *cursor_row
9518 = (desired_cursor_row->enabled_p
9519 ? desired_cursor_row
9520 : current_cursor_row);
9521
9522 text_area_width = window_box_width (w, TEXT_AREA);
9523
9524 /* Scroll when cursor is inside this scroll margin. */
9525 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9526
9527 if ((XFASTINT (w->hscroll)
9528 && w->cursor.x <= h_margin)
9529 || (cursor_row->enabled_p
9530 && cursor_row->truncated_on_right_p
9531 && (w->cursor.x >= text_area_width - h_margin)))
9532 {
9533 struct it it;
9534 int hscroll;
9535 struct buffer *saved_current_buffer;
9536 int pt;
9537 int wanted_x;
9538
9539 /* Find point in a display of infinite width. */
9540 saved_current_buffer = current_buffer;
9541 current_buffer = XBUFFER (w->buffer);
9542
9543 if (w == XWINDOW (selected_window))
9544 pt = BUF_PT (current_buffer);
9545 else
9546 {
9547 pt = marker_position (w->pointm);
9548 pt = max (BEGV, pt);
9549 pt = min (ZV, pt);
9550 }
9551
9552 /* Move iterator to pt starting at cursor_row->start in
9553 a line with infinite width. */
9554 init_to_row_start (&it, w, cursor_row);
9555 it.last_visible_x = INFINITY;
9556 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9557 current_buffer = saved_current_buffer;
9558
9559 /* Position cursor in window. */
9560 if (!hscroll_relative_p && hscroll_step_abs == 0)
9561 hscroll = max (0, it.current_x - text_area_width / 2)
9562 / FRAME_COLUMN_WIDTH (it.f);
9563 else if (w->cursor.x >= text_area_width - h_margin)
9564 {
9565 if (hscroll_relative_p)
9566 wanted_x = text_area_width * (1 - hscroll_step_rel)
9567 - h_margin;
9568 else
9569 wanted_x = text_area_width
9570 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9571 - h_margin;
9572 hscroll
9573 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9574 }
9575 else
9576 {
9577 if (hscroll_relative_p)
9578 wanted_x = text_area_width * hscroll_step_rel
9579 + h_margin;
9580 else
9581 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9582 + h_margin;
9583 hscroll
9584 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9585 }
9586 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9587
9588 /* Don't call Fset_window_hscroll if value hasn't
9589 changed because it will prevent redisplay
9590 optimizations. */
9591 if (XFASTINT (w->hscroll) != hscroll)
9592 {
9593 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9594 w->hscroll = make_number (hscroll);
9595 hscrolled_p = 1;
9596 }
9597 }
9598 }
9599
9600 window = w->next;
9601 }
9602
9603 /* Value is non-zero if hscroll of any leaf window has been changed. */
9604 return hscrolled_p;
9605 }
9606
9607
9608 /* Set hscroll so that cursor is visible and not inside horizontal
9609 scroll margins for all windows in the tree rooted at WINDOW. See
9610 also hscroll_window_tree above. Value is non-zero if any window's
9611 hscroll has been changed. If it has, desired matrices on the frame
9612 of WINDOW are cleared. */
9613
9614 static int
9615 hscroll_windows (window)
9616 Lisp_Object window;
9617 {
9618 int hscrolled_p;
9619
9620 if (automatic_hscrolling_p)
9621 {
9622 hscrolled_p = hscroll_window_tree (window);
9623 if (hscrolled_p)
9624 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9625 }
9626 else
9627 hscrolled_p = 0;
9628 return hscrolled_p;
9629 }
9630
9631
9632 \f
9633 /************************************************************************
9634 Redisplay
9635 ************************************************************************/
9636
9637 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9638 to a non-zero value. This is sometimes handy to have in a debugger
9639 session. */
9640
9641 #if GLYPH_DEBUG
9642
9643 /* First and last unchanged row for try_window_id. */
9644
9645 int debug_first_unchanged_at_end_vpos;
9646 int debug_last_unchanged_at_beg_vpos;
9647
9648 /* Delta vpos and y. */
9649
9650 int debug_dvpos, debug_dy;
9651
9652 /* Delta in characters and bytes for try_window_id. */
9653
9654 int debug_delta, debug_delta_bytes;
9655
9656 /* Values of window_end_pos and window_end_vpos at the end of
9657 try_window_id. */
9658
9659 EMACS_INT debug_end_pos, debug_end_vpos;
9660
9661 /* Append a string to W->desired_matrix->method. FMT is a printf
9662 format string. A1...A9 are a supplement for a variable-length
9663 argument list. If trace_redisplay_p is non-zero also printf the
9664 resulting string to stderr. */
9665
9666 static void
9667 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9668 struct window *w;
9669 char *fmt;
9670 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9671 {
9672 char buffer[512];
9673 char *method = w->desired_matrix->method;
9674 int len = strlen (method);
9675 int size = sizeof w->desired_matrix->method;
9676 int remaining = size - len - 1;
9677
9678 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9679 if (len && remaining)
9680 {
9681 method[len] = '|';
9682 --remaining, ++len;
9683 }
9684
9685 strncpy (method + len, buffer, remaining);
9686
9687 if (trace_redisplay_p)
9688 fprintf (stderr, "%p (%s): %s\n",
9689 w,
9690 ((BUFFERP (w->buffer)
9691 && STRINGP (XBUFFER (w->buffer)->name))
9692 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9693 : "no buffer"),
9694 buffer);
9695 }
9696
9697 #endif /* GLYPH_DEBUG */
9698
9699
9700 /* Value is non-zero if all changes in window W, which displays
9701 current_buffer, are in the text between START and END. START is a
9702 buffer position, END is given as a distance from Z. Used in
9703 redisplay_internal for display optimization. */
9704
9705 static INLINE int
9706 text_outside_line_unchanged_p (w, start, end)
9707 struct window *w;
9708 int start, end;
9709 {
9710 int unchanged_p = 1;
9711
9712 /* If text or overlays have changed, see where. */
9713 if (XFASTINT (w->last_modified) < MODIFF
9714 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9715 {
9716 /* Gap in the line? */
9717 if (GPT < start || Z - GPT < end)
9718 unchanged_p = 0;
9719
9720 /* Changes start in front of the line, or end after it? */
9721 if (unchanged_p
9722 && (BEG_UNCHANGED < start - 1
9723 || END_UNCHANGED < end))
9724 unchanged_p = 0;
9725
9726 /* If selective display, can't optimize if changes start at the
9727 beginning of the line. */
9728 if (unchanged_p
9729 && INTEGERP (current_buffer->selective_display)
9730 && XINT (current_buffer->selective_display) > 0
9731 && (BEG_UNCHANGED < start || GPT <= start))
9732 unchanged_p = 0;
9733
9734 /* If there are overlays at the start or end of the line, these
9735 may have overlay strings with newlines in them. A change at
9736 START, for instance, may actually concern the display of such
9737 overlay strings as well, and they are displayed on different
9738 lines. So, quickly rule out this case. (For the future, it
9739 might be desirable to implement something more telling than
9740 just BEG/END_UNCHANGED.) */
9741 if (unchanged_p)
9742 {
9743 if (BEG + BEG_UNCHANGED == start
9744 && overlay_touches_p (start))
9745 unchanged_p = 0;
9746 if (END_UNCHANGED == end
9747 && overlay_touches_p (Z - end))
9748 unchanged_p = 0;
9749 }
9750 }
9751
9752 return unchanged_p;
9753 }
9754
9755
9756 /* Do a frame update, taking possible shortcuts into account. This is
9757 the main external entry point for redisplay.
9758
9759 If the last redisplay displayed an echo area message and that message
9760 is no longer requested, we clear the echo area or bring back the
9761 mini-buffer if that is in use. */
9762
9763 void
9764 redisplay ()
9765 {
9766 redisplay_internal (0);
9767 }
9768
9769
9770 /* Return 1 if point moved out of or into a composition. Otherwise
9771 return 0. PREV_BUF and PREV_PT are the last point buffer and
9772 position. BUF and PT are the current point buffer and position. */
9773
9774 int
9775 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9776 struct buffer *prev_buf, *buf;
9777 int prev_pt, pt;
9778 {
9779 int start, end;
9780 Lisp_Object prop;
9781 Lisp_Object buffer;
9782
9783 XSETBUFFER (buffer, buf);
9784 /* Check a composition at the last point if point moved within the
9785 same buffer. */
9786 if (prev_buf == buf)
9787 {
9788 if (prev_pt == pt)
9789 /* Point didn't move. */
9790 return 0;
9791
9792 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9793 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9794 && COMPOSITION_VALID_P (start, end, prop)
9795 && start < prev_pt && end > prev_pt)
9796 /* The last point was within the composition. Return 1 iff
9797 point moved out of the composition. */
9798 return (pt <= start || pt >= end);
9799 }
9800
9801 /* Check a composition at the current point. */
9802 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9803 && find_composition (pt, -1, &start, &end, &prop, buffer)
9804 && COMPOSITION_VALID_P (start, end, prop)
9805 && start < pt && end > pt);
9806 }
9807
9808
9809 /* Reconsider the setting of B->clip_changed which is displayed
9810 in window W. */
9811
9812 static INLINE void
9813 reconsider_clip_changes (w, b)
9814 struct window *w;
9815 struct buffer *b;
9816 {
9817 if (b->clip_changed
9818 && !NILP (w->window_end_valid)
9819 && w->current_matrix->buffer == b
9820 && w->current_matrix->zv == BUF_ZV (b)
9821 && w->current_matrix->begv == BUF_BEGV (b))
9822 b->clip_changed = 0;
9823
9824 /* If display wasn't paused, and W is not a tool bar window, see if
9825 point has been moved into or out of a composition. In that case,
9826 we set b->clip_changed to 1 to force updating the screen. If
9827 b->clip_changed has already been set to 1, we can skip this
9828 check. */
9829 if (!b->clip_changed
9830 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9831 {
9832 int pt;
9833
9834 if (w == XWINDOW (selected_window))
9835 pt = BUF_PT (current_buffer);
9836 else
9837 pt = marker_position (w->pointm);
9838
9839 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9840 || pt != XINT (w->last_point))
9841 && check_point_in_composition (w->current_matrix->buffer,
9842 XINT (w->last_point),
9843 XBUFFER (w->buffer), pt))
9844 b->clip_changed = 1;
9845 }
9846 }
9847 \f
9848
9849 /* Select FRAME to forward the values of frame-local variables into C
9850 variables so that the redisplay routines can access those values
9851 directly. */
9852
9853 static void
9854 select_frame_for_redisplay (frame)
9855 Lisp_Object frame;
9856 {
9857 Lisp_Object tail, sym, val;
9858 Lisp_Object old = selected_frame;
9859
9860 selected_frame = frame;
9861
9862 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9863 if (CONSP (XCAR (tail))
9864 && (sym = XCAR (XCAR (tail)),
9865 SYMBOLP (sym))
9866 && (sym = indirect_variable (sym),
9867 val = SYMBOL_VALUE (sym),
9868 (BUFFER_LOCAL_VALUEP (val)
9869 || SOME_BUFFER_LOCAL_VALUEP (val)))
9870 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9871 Fsymbol_value (sym);
9872
9873 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9874 if (CONSP (XCAR (tail))
9875 && (sym = XCAR (XCAR (tail)),
9876 SYMBOLP (sym))
9877 && (sym = indirect_variable (sym),
9878 val = SYMBOL_VALUE (sym),
9879 (BUFFER_LOCAL_VALUEP (val)
9880 || SOME_BUFFER_LOCAL_VALUEP (val)))
9881 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9882 Fsymbol_value (sym);
9883 }
9884
9885
9886 #define STOP_POLLING \
9887 do { if (! polling_stopped_here) stop_polling (); \
9888 polling_stopped_here = 1; } while (0)
9889
9890 #define RESUME_POLLING \
9891 do { if (polling_stopped_here) start_polling (); \
9892 polling_stopped_here = 0; } while (0)
9893
9894
9895 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9896 response to any user action; therefore, we should preserve the echo
9897 area. (Actually, our caller does that job.) Perhaps in the future
9898 avoid recentering windows if it is not necessary; currently that
9899 causes some problems. */
9900
9901 static void
9902 redisplay_internal (preserve_echo_area)
9903 int preserve_echo_area;
9904 {
9905 struct window *w = XWINDOW (selected_window);
9906 struct frame *f = XFRAME (w->frame);
9907 int pause;
9908 int must_finish = 0;
9909 struct text_pos tlbufpos, tlendpos;
9910 int number_of_visible_frames;
9911 int count;
9912 struct frame *sf = SELECTED_FRAME ();
9913 int polling_stopped_here = 0;
9914
9915 /* Non-zero means redisplay has to consider all windows on all
9916 frames. Zero means, only selected_window is considered. */
9917 int consider_all_windows_p;
9918
9919 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9920
9921 /* No redisplay if running in batch mode or frame is not yet fully
9922 initialized, or redisplay is explicitly turned off by setting
9923 Vinhibit_redisplay. */
9924 if (noninteractive
9925 || !NILP (Vinhibit_redisplay)
9926 || !f->glyphs_initialized_p)
9927 return;
9928
9929 /* The flag redisplay_performed_directly_p is set by
9930 direct_output_for_insert when it already did the whole screen
9931 update necessary. */
9932 if (redisplay_performed_directly_p)
9933 {
9934 redisplay_performed_directly_p = 0;
9935 if (!hscroll_windows (selected_window))
9936 return;
9937 }
9938
9939 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9940 if (popup_activated ())
9941 return;
9942 #endif
9943
9944 /* I don't think this happens but let's be paranoid. */
9945 if (redisplaying_p)
9946 return;
9947
9948 /* Record a function that resets redisplaying_p to its old value
9949 when we leave this function. */
9950 count = SPECPDL_INDEX ();
9951 record_unwind_protect (unwind_redisplay,
9952 Fcons (make_number (redisplaying_p), selected_frame));
9953 ++redisplaying_p;
9954 specbind (Qinhibit_free_realized_faces, Qnil);
9955
9956 retry:
9957 pause = 0;
9958 reconsider_clip_changes (w, current_buffer);
9959
9960 /* If new fonts have been loaded that make a glyph matrix adjustment
9961 necessary, do it. */
9962 if (fonts_changed_p)
9963 {
9964 adjust_glyphs (NULL);
9965 ++windows_or_buffers_changed;
9966 fonts_changed_p = 0;
9967 }
9968
9969 /* If face_change_count is non-zero, init_iterator will free all
9970 realized faces, which includes the faces referenced from current
9971 matrices. So, we can't reuse current matrices in this case. */
9972 if (face_change_count)
9973 ++windows_or_buffers_changed;
9974
9975 if (FRAME_TERMCAP_P (sf)
9976 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9977 {
9978 /* Since frames on a single ASCII terminal share the same
9979 display area, displaying a different frame means redisplay
9980 the whole thing. */
9981 windows_or_buffers_changed++;
9982 SET_FRAME_GARBAGED (sf);
9983 FRAME_TTY (sf)->previous_terminal_frame = sf;
9984 }
9985
9986 /* Set the visible flags for all frames. Do this before checking
9987 for resized or garbaged frames; they want to know if their frames
9988 are visible. See the comment in frame.h for
9989 FRAME_SAMPLE_VISIBILITY. */
9990 {
9991 Lisp_Object tail, frame;
9992
9993 number_of_visible_frames = 0;
9994
9995 FOR_EACH_FRAME (tail, frame)
9996 {
9997 struct frame *f = XFRAME (frame);
9998
9999 FRAME_SAMPLE_VISIBILITY (f);
10000 if (FRAME_VISIBLE_P (f))
10001 ++number_of_visible_frames;
10002 clear_desired_matrices (f);
10003 }
10004 }
10005
10006
10007 /* Notice any pending interrupt request to change frame size. */
10008 do_pending_window_change (1);
10009
10010 /* Clear frames marked as garbaged. */
10011 if (frame_garbaged)
10012 clear_garbaged_frames ();
10013
10014 /* Build menubar and tool-bar items. */
10015 prepare_menu_bars ();
10016
10017 if (windows_or_buffers_changed)
10018 update_mode_lines++;
10019
10020 /* Detect case that we need to write or remove a star in the mode line. */
10021 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10022 {
10023 w->update_mode_line = Qt;
10024 if (buffer_shared > 1)
10025 update_mode_lines++;
10026 }
10027
10028 /* If %c is in the mode line, update it if needed. */
10029 if (!NILP (w->column_number_displayed)
10030 /* This alternative quickly identifies a common case
10031 where no change is needed. */
10032 && !(PT == XFASTINT (w->last_point)
10033 && XFASTINT (w->last_modified) >= MODIFF
10034 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10035 && (XFASTINT (w->column_number_displayed)
10036 != (int) current_column ())) /* iftc */
10037 w->update_mode_line = Qt;
10038
10039 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10040
10041 /* The variable buffer_shared is set in redisplay_window and
10042 indicates that we redisplay a buffer in different windows. See
10043 there. */
10044 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10045 || cursor_type_changed);
10046
10047 /* If specs for an arrow have changed, do thorough redisplay
10048 to ensure we remove any arrow that should no longer exist. */
10049 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
10050 || ! EQ (Voverlay_arrow_string, last_arrow_string))
10051 consider_all_windows_p = windows_or_buffers_changed = 1;
10052
10053 /* Normally the message* functions will have already displayed and
10054 updated the echo area, but the frame may have been trashed, or
10055 the update may have been preempted, so display the echo area
10056 again here. Checking message_cleared_p captures the case that
10057 the echo area should be cleared. */
10058 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10059 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10060 || (message_cleared_p
10061 && minibuf_level == 0
10062 /* If the mini-window is currently selected, this means the
10063 echo-area doesn't show through. */
10064 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10065 {
10066 int window_height_changed_p = echo_area_display (0);
10067 must_finish = 1;
10068
10069 /* If we don't display the current message, don't clear the
10070 message_cleared_p flag, because, if we did, we wouldn't clear
10071 the echo area in the next redisplay which doesn't preserve
10072 the echo area. */
10073 if (!display_last_displayed_message_p)
10074 message_cleared_p = 0;
10075
10076 if (fonts_changed_p)
10077 goto retry;
10078 else if (window_height_changed_p)
10079 {
10080 consider_all_windows_p = 1;
10081 ++update_mode_lines;
10082 ++windows_or_buffers_changed;
10083
10084 /* If window configuration was changed, frames may have been
10085 marked garbaged. Clear them or we will experience
10086 surprises wrt scrolling. */
10087 if (frame_garbaged)
10088 clear_garbaged_frames ();
10089 }
10090 }
10091 else if (EQ (selected_window, minibuf_window)
10092 && (current_buffer->clip_changed
10093 || XFASTINT (w->last_modified) < MODIFF
10094 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10095 && resize_mini_window (w, 0))
10096 {
10097 /* Resized active mini-window to fit the size of what it is
10098 showing if its contents might have changed. */
10099 must_finish = 1;
10100 consider_all_windows_p = 1;
10101 ++windows_or_buffers_changed;
10102 ++update_mode_lines;
10103
10104 /* If window configuration was changed, frames may have been
10105 marked garbaged. Clear them or we will experience
10106 surprises wrt scrolling. */
10107 if (frame_garbaged)
10108 clear_garbaged_frames ();
10109 }
10110
10111
10112 /* If showing the region, and mark has changed, we must redisplay
10113 the whole window. The assignment to this_line_start_pos prevents
10114 the optimization directly below this if-statement. */
10115 if (((!NILP (Vtransient_mark_mode)
10116 && !NILP (XBUFFER (w->buffer)->mark_active))
10117 != !NILP (w->region_showing))
10118 || (!NILP (w->region_showing)
10119 && !EQ (w->region_showing,
10120 Fmarker_position (XBUFFER (w->buffer)->mark))))
10121 CHARPOS (this_line_start_pos) = 0;
10122
10123 /* Optimize the case that only the line containing the cursor in the
10124 selected window has changed. Variables starting with this_ are
10125 set in display_line and record information about the line
10126 containing the cursor. */
10127 tlbufpos = this_line_start_pos;
10128 tlendpos = this_line_end_pos;
10129 if (!consider_all_windows_p
10130 && CHARPOS (tlbufpos) > 0
10131 && NILP (w->update_mode_line)
10132 && !current_buffer->clip_changed
10133 && !current_buffer->prevent_redisplay_optimizations_p
10134 && FRAME_VISIBLE_P (XFRAME (w->frame))
10135 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10136 /* Make sure recorded data applies to current buffer, etc. */
10137 && this_line_buffer == current_buffer
10138 && current_buffer == XBUFFER (w->buffer)
10139 && NILP (w->force_start)
10140 && NILP (w->optional_new_start)
10141 /* Point must be on the line that we have info recorded about. */
10142 && PT >= CHARPOS (tlbufpos)
10143 && PT <= Z - CHARPOS (tlendpos)
10144 /* All text outside that line, including its final newline,
10145 must be unchanged */
10146 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10147 CHARPOS (tlendpos)))
10148 {
10149 if (CHARPOS (tlbufpos) > BEGV
10150 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10151 && (CHARPOS (tlbufpos) == ZV
10152 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10153 /* Former continuation line has disappeared by becoming empty */
10154 goto cancel;
10155 else if (XFASTINT (w->last_modified) < MODIFF
10156 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10157 || MINI_WINDOW_P (w))
10158 {
10159 /* We have to handle the case of continuation around a
10160 wide-column character (See the comment in indent.c around
10161 line 885).
10162
10163 For instance, in the following case:
10164
10165 -------- Insert --------
10166 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10167 J_I_ ==> J_I_ `^^' are cursors.
10168 ^^ ^^
10169 -------- --------
10170
10171 As we have to redraw the line above, we should goto cancel. */
10172
10173 struct it it;
10174 int line_height_before = this_line_pixel_height;
10175
10176 /* Note that start_display will handle the case that the
10177 line starting at tlbufpos is a continuation lines. */
10178 start_display (&it, w, tlbufpos);
10179
10180 /* Implementation note: It this still necessary? */
10181 if (it.current_x != this_line_start_x)
10182 goto cancel;
10183
10184 TRACE ((stderr, "trying display optimization 1\n"));
10185 w->cursor.vpos = -1;
10186 overlay_arrow_seen = 0;
10187 it.vpos = this_line_vpos;
10188 it.current_y = this_line_y;
10189 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10190 display_line (&it);
10191
10192 /* If line contains point, is not continued,
10193 and ends at same distance from eob as before, we win */
10194 if (w->cursor.vpos >= 0
10195 /* Line is not continued, otherwise this_line_start_pos
10196 would have been set to 0 in display_line. */
10197 && CHARPOS (this_line_start_pos)
10198 /* Line ends as before. */
10199 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10200 /* Line has same height as before. Otherwise other lines
10201 would have to be shifted up or down. */
10202 && this_line_pixel_height == line_height_before)
10203 {
10204 /* If this is not the window's last line, we must adjust
10205 the charstarts of the lines below. */
10206 if (it.current_y < it.last_visible_y)
10207 {
10208 struct glyph_row *row
10209 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10210 int delta, delta_bytes;
10211
10212 if (Z - CHARPOS (tlendpos) == ZV)
10213 {
10214 /* This line ends at end of (accessible part of)
10215 buffer. There is no newline to count. */
10216 delta = (Z
10217 - CHARPOS (tlendpos)
10218 - MATRIX_ROW_START_CHARPOS (row));
10219 delta_bytes = (Z_BYTE
10220 - BYTEPOS (tlendpos)
10221 - MATRIX_ROW_START_BYTEPOS (row));
10222 }
10223 else
10224 {
10225 /* This line ends in a newline. Must take
10226 account of the newline and the rest of the
10227 text that follows. */
10228 delta = (Z
10229 - CHARPOS (tlendpos)
10230 - MATRIX_ROW_START_CHARPOS (row));
10231 delta_bytes = (Z_BYTE
10232 - BYTEPOS (tlendpos)
10233 - MATRIX_ROW_START_BYTEPOS (row));
10234 }
10235
10236 increment_matrix_positions (w->current_matrix,
10237 this_line_vpos + 1,
10238 w->current_matrix->nrows,
10239 delta, delta_bytes);
10240 }
10241
10242 /* If this row displays text now but previously didn't,
10243 or vice versa, w->window_end_vpos may have to be
10244 adjusted. */
10245 if ((it.glyph_row - 1)->displays_text_p)
10246 {
10247 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10248 XSETINT (w->window_end_vpos, this_line_vpos);
10249 }
10250 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10251 && this_line_vpos > 0)
10252 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10253 w->window_end_valid = Qnil;
10254
10255 /* Update hint: No need to try to scroll in update_window. */
10256 w->desired_matrix->no_scrolling_p = 1;
10257
10258 #if GLYPH_DEBUG
10259 *w->desired_matrix->method = 0;
10260 debug_method_add (w, "optimization 1");
10261 #endif
10262 #ifdef HAVE_WINDOW_SYSTEM
10263 update_window_fringes (w, 0);
10264 #endif
10265 goto update;
10266 }
10267 else
10268 goto cancel;
10269 }
10270 else if (/* Cursor position hasn't changed. */
10271 PT == XFASTINT (w->last_point)
10272 /* Make sure the cursor was last displayed
10273 in this window. Otherwise we have to reposition it. */
10274 && 0 <= w->cursor.vpos
10275 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10276 {
10277 if (!must_finish)
10278 {
10279 do_pending_window_change (1);
10280
10281 /* We used to always goto end_of_redisplay here, but this
10282 isn't enough if we have a blinking cursor. */
10283 if (w->cursor_off_p == w->last_cursor_off_p)
10284 goto end_of_redisplay;
10285 }
10286 goto update;
10287 }
10288 /* If highlighting the region, or if the cursor is in the echo area,
10289 then we can't just move the cursor. */
10290 else if (! (!NILP (Vtransient_mark_mode)
10291 && !NILP (current_buffer->mark_active))
10292 && (EQ (selected_window, current_buffer->last_selected_window)
10293 || highlight_nonselected_windows)
10294 && NILP (w->region_showing)
10295 && NILP (Vshow_trailing_whitespace)
10296 && !cursor_in_echo_area)
10297 {
10298 struct it it;
10299 struct glyph_row *row;
10300
10301 /* Skip from tlbufpos to PT and see where it is. Note that
10302 PT may be in invisible text. If so, we will end at the
10303 next visible position. */
10304 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10305 NULL, DEFAULT_FACE_ID);
10306 it.current_x = this_line_start_x;
10307 it.current_y = this_line_y;
10308 it.vpos = this_line_vpos;
10309
10310 /* The call to move_it_to stops in front of PT, but
10311 moves over before-strings. */
10312 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10313
10314 if (it.vpos == this_line_vpos
10315 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10316 row->enabled_p))
10317 {
10318 xassert (this_line_vpos == it.vpos);
10319 xassert (this_line_y == it.current_y);
10320 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10321 #if GLYPH_DEBUG
10322 *w->desired_matrix->method = 0;
10323 debug_method_add (w, "optimization 3");
10324 #endif
10325 goto update;
10326 }
10327 else
10328 goto cancel;
10329 }
10330
10331 cancel:
10332 /* Text changed drastically or point moved off of line. */
10333 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10334 }
10335
10336 CHARPOS (this_line_start_pos) = 0;
10337 consider_all_windows_p |= buffer_shared > 1;
10338 ++clear_face_cache_count;
10339
10340
10341 /* Build desired matrices, and update the display. If
10342 consider_all_windows_p is non-zero, do it for all windows on all
10343 frames. Otherwise do it for selected_window, only. */
10344
10345 if (consider_all_windows_p)
10346 {
10347 Lisp_Object tail, frame;
10348 int i, n = 0, size = 50;
10349 struct frame **updated
10350 = (struct frame **) alloca (size * sizeof *updated);
10351
10352 /* Clear the face cache eventually. */
10353 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10354 {
10355 clear_face_cache (0);
10356 clear_face_cache_count = 0;
10357 }
10358
10359 /* Recompute # windows showing selected buffer. This will be
10360 incremented each time such a window is displayed. */
10361 buffer_shared = 0;
10362
10363 FOR_EACH_FRAME (tail, frame)
10364 {
10365 struct frame *f = XFRAME (frame);
10366
10367 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10368 {
10369 if (! EQ (frame, selected_frame))
10370 /* Select the frame, for the sake of frame-local
10371 variables. */
10372 select_frame_for_redisplay (frame);
10373
10374 #ifdef HAVE_WINDOW_SYSTEM
10375 if (clear_face_cache_count % 50 == 0
10376 && FRAME_WINDOW_P (f))
10377 clear_image_cache (f, 0);
10378 #endif /* HAVE_WINDOW_SYSTEM */
10379
10380 /* Mark all the scroll bars to be removed; we'll redeem
10381 the ones we want when we redisplay their windows. */
10382 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10383 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10384
10385 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10386 redisplay_windows (FRAME_ROOT_WINDOW (f));
10387
10388 /* Any scroll bars which redisplay_windows should have
10389 nuked should now go away. */
10390 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10391 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10392
10393 /* If fonts changed, display again. */
10394 /* ??? rms: I suspect it is a mistake to jump all the way
10395 back to retry here. It should just retry this frame. */
10396 if (fonts_changed_p)
10397 goto retry;
10398
10399 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10400 {
10401 /* See if we have to hscroll. */
10402 if (hscroll_windows (f->root_window))
10403 goto retry;
10404
10405 /* Prevent various kinds of signals during display
10406 update. stdio is not robust about handling
10407 signals, which can cause an apparent I/O
10408 error. */
10409 if (interrupt_input)
10410 unrequest_sigio ();
10411 STOP_POLLING;
10412
10413 /* Update the display. */
10414 set_window_update_flags (XWINDOW (f->root_window), 1);
10415 pause |= update_frame (f, 0, 0);
10416 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10417 if (pause)
10418 break;
10419 #endif
10420
10421 if (n == size)
10422 {
10423 int nbytes = size * sizeof *updated;
10424 struct frame **p = (struct frame **) alloca (2 * nbytes);
10425 bcopy (updated, p, nbytes);
10426 size *= 2;
10427 }
10428
10429 updated[n++] = f;
10430 }
10431 }
10432 }
10433
10434 if (!pause)
10435 {
10436 /* Do the mark_window_display_accurate after all windows have
10437 been redisplayed because this call resets flags in buffers
10438 which are needed for proper redisplay. */
10439 for (i = 0; i < n; ++i)
10440 {
10441 struct frame *f = updated[i];
10442 mark_window_display_accurate (f->root_window, 1);
10443 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10444 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10445 }
10446 }
10447 }
10448 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10449 {
10450 Lisp_Object mini_window;
10451 struct frame *mini_frame;
10452
10453 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10454 /* Use list_of_error, not Qerror, so that
10455 we catch only errors and don't run the debugger. */
10456 internal_condition_case_1 (redisplay_window_1, selected_window,
10457 list_of_error,
10458 redisplay_window_error);
10459
10460 /* Compare desired and current matrices, perform output. */
10461
10462 update:
10463 /* If fonts changed, display again. */
10464 if (fonts_changed_p)
10465 goto retry;
10466
10467 /* Prevent various kinds of signals during display update.
10468 stdio is not robust about handling signals,
10469 which can cause an apparent I/O error. */
10470 if (interrupt_input)
10471 unrequest_sigio ();
10472 STOP_POLLING;
10473
10474 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10475 {
10476 if (hscroll_windows (selected_window))
10477 goto retry;
10478
10479 XWINDOW (selected_window)->must_be_updated_p = 1;
10480 pause = update_frame (sf, 0, 0);
10481 }
10482
10483 /* We may have called echo_area_display at the top of this
10484 function. If the echo area is on another frame, that may
10485 have put text on a frame other than the selected one, so the
10486 above call to update_frame would not have caught it. Catch
10487 it here. */
10488 mini_window = FRAME_MINIBUF_WINDOW (sf);
10489 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10490
10491 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10492 {
10493 XWINDOW (mini_window)->must_be_updated_p = 1;
10494 pause |= update_frame (mini_frame, 0, 0);
10495 if (!pause && hscroll_windows (mini_window))
10496 goto retry;
10497 }
10498 }
10499
10500 /* If display was paused because of pending input, make sure we do a
10501 thorough update the next time. */
10502 if (pause)
10503 {
10504 /* Prevent the optimization at the beginning of
10505 redisplay_internal that tries a single-line update of the
10506 line containing the cursor in the selected window. */
10507 CHARPOS (this_line_start_pos) = 0;
10508
10509 /* Let the overlay arrow be updated the next time. */
10510 if (!NILP (last_arrow_position))
10511 {
10512 last_arrow_position = Qt;
10513 last_arrow_string = Qt;
10514 }
10515
10516 /* If we pause after scrolling, some rows in the current
10517 matrices of some windows are not valid. */
10518 if (!WINDOW_FULL_WIDTH_P (w)
10519 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10520 update_mode_lines = 1;
10521 }
10522 else
10523 {
10524 if (!consider_all_windows_p)
10525 {
10526 /* This has already been done above if
10527 consider_all_windows_p is set. */
10528 mark_window_display_accurate_1 (w, 1);
10529
10530 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10531 last_arrow_string = Voverlay_arrow_string;
10532
10533 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10534 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10535 }
10536
10537 update_mode_lines = 0;
10538 windows_or_buffers_changed = 0;
10539 cursor_type_changed = 0;
10540 }
10541
10542 /* Start SIGIO interrupts coming again. Having them off during the
10543 code above makes it less likely one will discard output, but not
10544 impossible, since there might be stuff in the system buffer here.
10545 But it is much hairier to try to do anything about that. */
10546 if (interrupt_input)
10547 request_sigio ();
10548 RESUME_POLLING;
10549
10550 /* If a frame has become visible which was not before, redisplay
10551 again, so that we display it. Expose events for such a frame
10552 (which it gets when becoming visible) don't call the parts of
10553 redisplay constructing glyphs, so simply exposing a frame won't
10554 display anything in this case. So, we have to display these
10555 frames here explicitly. */
10556 if (!pause)
10557 {
10558 Lisp_Object tail, frame;
10559 int new_count = 0;
10560
10561 FOR_EACH_FRAME (tail, frame)
10562 {
10563 int this_is_visible = 0;
10564
10565 if (XFRAME (frame)->visible)
10566 this_is_visible = 1;
10567 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10568 if (XFRAME (frame)->visible)
10569 this_is_visible = 1;
10570
10571 if (this_is_visible)
10572 new_count++;
10573 }
10574
10575 if (new_count != number_of_visible_frames)
10576 windows_or_buffers_changed++;
10577 }
10578
10579 /* Change frame size now if a change is pending. */
10580 do_pending_window_change (1);
10581
10582 /* If we just did a pending size change, or have additional
10583 visible frames, redisplay again. */
10584 if (windows_or_buffers_changed && !pause)
10585 goto retry;
10586
10587 end_of_redisplay:
10588 unbind_to (count, Qnil);
10589 RESUME_POLLING;
10590 }
10591
10592
10593 /* Redisplay, but leave alone any recent echo area message unless
10594 another message has been requested in its place.
10595
10596 This is useful in situations where you need to redisplay but no
10597 user action has occurred, making it inappropriate for the message
10598 area to be cleared. See tracking_off and
10599 wait_reading_process_input for examples of these situations.
10600
10601 FROM_WHERE is an integer saying from where this function was
10602 called. This is useful for debugging. */
10603
10604 void
10605 redisplay_preserve_echo_area (from_where)
10606 int from_where;
10607 {
10608 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10609
10610 if (!NILP (echo_area_buffer[1]))
10611 {
10612 /* We have a previously displayed message, but no current
10613 message. Redisplay the previous message. */
10614 display_last_displayed_message_p = 1;
10615 redisplay_internal (1);
10616 display_last_displayed_message_p = 0;
10617 }
10618 else
10619 redisplay_internal (1);
10620 }
10621
10622
10623 /* Function registered with record_unwind_protect in
10624 redisplay_internal. Reset redisplaying_p to the value it had
10625 before redisplay_internal was called, and clear
10626 prevent_freeing_realized_faces_p. It also selects the previously
10627 selected frame. */
10628
10629 static Lisp_Object
10630 unwind_redisplay (val)
10631 Lisp_Object val;
10632 {
10633 Lisp_Object old_redisplaying_p, old_frame;
10634
10635 old_redisplaying_p = XCAR (val);
10636 redisplaying_p = XFASTINT (old_redisplaying_p);
10637 old_frame = XCDR (val);
10638 if (! EQ (old_frame, selected_frame))
10639 select_frame_for_redisplay (old_frame);
10640 return Qnil;
10641 }
10642
10643
10644 /* Mark the display of window W as accurate or inaccurate. If
10645 ACCURATE_P is non-zero mark display of W as accurate. If
10646 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10647 redisplay_internal is called. */
10648
10649 static void
10650 mark_window_display_accurate_1 (w, accurate_p)
10651 struct window *w;
10652 int accurate_p;
10653 {
10654 if (BUFFERP (w->buffer))
10655 {
10656 struct buffer *b = XBUFFER (w->buffer);
10657
10658 w->last_modified
10659 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10660 w->last_overlay_modified
10661 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10662 w->last_had_star
10663 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10664
10665 if (accurate_p)
10666 {
10667 b->clip_changed = 0;
10668 b->prevent_redisplay_optimizations_p = 0;
10669
10670 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10671 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10672 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10673 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10674
10675 w->current_matrix->buffer = b;
10676 w->current_matrix->begv = BUF_BEGV (b);
10677 w->current_matrix->zv = BUF_ZV (b);
10678
10679 w->last_cursor = w->cursor;
10680 w->last_cursor_off_p = w->cursor_off_p;
10681
10682 if (w == XWINDOW (selected_window))
10683 w->last_point = make_number (BUF_PT (b));
10684 else
10685 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10686 }
10687 }
10688
10689 if (accurate_p)
10690 {
10691 w->window_end_valid = w->buffer;
10692 #if 0 /* This is incorrect with variable-height lines. */
10693 xassert (XINT (w->window_end_vpos)
10694 < (WINDOW_TOTAL_LINES (w)
10695 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10696 #endif
10697 w->update_mode_line = Qnil;
10698 }
10699 }
10700
10701
10702 /* Mark the display of windows in the window tree rooted at WINDOW as
10703 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10704 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10705 be redisplayed the next time redisplay_internal is called. */
10706
10707 void
10708 mark_window_display_accurate (window, accurate_p)
10709 Lisp_Object window;
10710 int accurate_p;
10711 {
10712 struct window *w;
10713
10714 for (; !NILP (window); window = w->next)
10715 {
10716 w = XWINDOW (window);
10717 mark_window_display_accurate_1 (w, accurate_p);
10718
10719 if (!NILP (w->vchild))
10720 mark_window_display_accurate (w->vchild, accurate_p);
10721 if (!NILP (w->hchild))
10722 mark_window_display_accurate (w->hchild, accurate_p);
10723 }
10724
10725 if (accurate_p)
10726 {
10727 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10728 last_arrow_string = Voverlay_arrow_string;
10729 }
10730 else
10731 {
10732 /* Force a thorough redisplay the next time by setting
10733 last_arrow_position and last_arrow_string to t, which is
10734 unequal to any useful value of Voverlay_arrow_... */
10735 last_arrow_position = Qt;
10736 last_arrow_string = Qt;
10737 }
10738 }
10739
10740
10741 /* Return value in display table DP (Lisp_Char_Table *) for character
10742 C. Since a display table doesn't have any parent, we don't have to
10743 follow parent. Do not call this function directly but use the
10744 macro DISP_CHAR_VECTOR. */
10745
10746 Lisp_Object
10747 disp_char_vector (dp, c)
10748 struct Lisp_Char_Table *dp;
10749 int c;
10750 {
10751 int code[4], i;
10752 Lisp_Object val;
10753
10754 if (SINGLE_BYTE_CHAR_P (c))
10755 return (dp->contents[c]);
10756
10757 SPLIT_CHAR (c, code[0], code[1], code[2]);
10758 if (code[1] < 32)
10759 code[1] = -1;
10760 else if (code[2] < 32)
10761 code[2] = -1;
10762
10763 /* Here, the possible range of code[0] (== charset ID) is
10764 128..max_charset. Since the top level char table contains data
10765 for multibyte characters after 256th element, we must increment
10766 code[0] by 128 to get a correct index. */
10767 code[0] += 128;
10768 code[3] = -1; /* anchor */
10769
10770 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10771 {
10772 val = dp->contents[code[i]];
10773 if (!SUB_CHAR_TABLE_P (val))
10774 return (NILP (val) ? dp->defalt : val);
10775 }
10776
10777 /* Here, val is a sub char table. We return the default value of
10778 it. */
10779 return (dp->defalt);
10780 }
10781
10782
10783 \f
10784 /***********************************************************************
10785 Window Redisplay
10786 ***********************************************************************/
10787
10788 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10789
10790 static void
10791 redisplay_windows (window)
10792 Lisp_Object window;
10793 {
10794 while (!NILP (window))
10795 {
10796 struct window *w = XWINDOW (window);
10797
10798 if (!NILP (w->hchild))
10799 redisplay_windows (w->hchild);
10800 else if (!NILP (w->vchild))
10801 redisplay_windows (w->vchild);
10802 else
10803 {
10804 displayed_buffer = XBUFFER (w->buffer);
10805 /* Use list_of_error, not Qerror, so that
10806 we catch only errors and don't run the debugger. */
10807 internal_condition_case_1 (redisplay_window_0, window,
10808 list_of_error,
10809 redisplay_window_error);
10810 }
10811
10812 window = w->next;
10813 }
10814 }
10815
10816 static Lisp_Object
10817 redisplay_window_error ()
10818 {
10819 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10820 return Qnil;
10821 }
10822
10823 static Lisp_Object
10824 redisplay_window_0 (window)
10825 Lisp_Object window;
10826 {
10827 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10828 redisplay_window (window, 0);
10829 return Qnil;
10830 }
10831
10832 static Lisp_Object
10833 redisplay_window_1 (window)
10834 Lisp_Object window;
10835 {
10836 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10837 redisplay_window (window, 1);
10838 return Qnil;
10839 }
10840 \f
10841
10842 /* Increment GLYPH until it reaches END or CONDITION fails while
10843 adding (GLYPH)->pixel_width to X. */
10844
10845 #define SKIP_GLYPHS(glyph, end, x, condition) \
10846 do \
10847 { \
10848 (x) += (glyph)->pixel_width; \
10849 ++(glyph); \
10850 } \
10851 while ((glyph) < (end) && (condition))
10852
10853
10854 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10855 DELTA is the number of bytes by which positions recorded in ROW
10856 differ from current buffer positions. */
10857
10858 void
10859 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10860 struct window *w;
10861 struct glyph_row *row;
10862 struct glyph_matrix *matrix;
10863 int delta, delta_bytes, dy, dvpos;
10864 {
10865 struct glyph *glyph = row->glyphs[TEXT_AREA];
10866 struct glyph *end = glyph + row->used[TEXT_AREA];
10867 /* The first glyph that starts a sequence of glyphs from string. */
10868 struct glyph *string_start;
10869 /* The X coordinate of string_start. */
10870 int string_start_x;
10871 /* The last known character position. */
10872 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10873 /* The last known character position before string_start. */
10874 int string_before_pos;
10875 int x = row->x;
10876 int pt_old = PT - delta;
10877
10878 /* Skip over glyphs not having an object at the start of the row.
10879 These are special glyphs like truncation marks on terminal
10880 frames. */
10881 if (row->displays_text_p)
10882 while (glyph < end
10883 && INTEGERP (glyph->object)
10884 && glyph->charpos < 0)
10885 {
10886 x += glyph->pixel_width;
10887 ++glyph;
10888 }
10889
10890 string_start = NULL;
10891 while (glyph < end
10892 && !INTEGERP (glyph->object)
10893 && (!BUFFERP (glyph->object)
10894 || (last_pos = glyph->charpos) < pt_old))
10895 {
10896 if (! STRINGP (glyph->object))
10897 {
10898 string_start = NULL;
10899 x += glyph->pixel_width;
10900 ++glyph;
10901 }
10902 else
10903 {
10904 string_before_pos = last_pos;
10905 string_start = glyph;
10906 string_start_x = x;
10907 /* Skip all glyphs from string. */
10908 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10909 }
10910 }
10911
10912 if (string_start
10913 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10914 {
10915 /* We may have skipped over point because the previous glyphs
10916 are from string. As there's no easy way to know the
10917 character position of the current glyph, find the correct
10918 glyph on point by scanning from string_start again. */
10919 Lisp_Object limit;
10920 Lisp_Object string;
10921 int pos;
10922
10923 limit = make_number (pt_old + 1);
10924 end = glyph;
10925 glyph = string_start;
10926 x = string_start_x;
10927 string = glyph->object;
10928 pos = string_buffer_position (w, string, string_before_pos);
10929 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10930 because we always put cursor after overlay strings. */
10931 while (pos == 0 && glyph < end)
10932 {
10933 string = glyph->object;
10934 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10935 if (glyph < end)
10936 pos = string_buffer_position (w, glyph->object, string_before_pos);
10937 }
10938
10939 while (glyph < end)
10940 {
10941 pos = XINT (Fnext_single_char_property_change
10942 (make_number (pos), Qdisplay, Qnil, limit));
10943 if (pos > pt_old)
10944 break;
10945 /* Skip glyphs from the same string. */
10946 string = glyph->object;
10947 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10948 /* Skip glyphs from an overlay. */
10949 while (glyph < end
10950 && ! string_buffer_position (w, glyph->object, pos))
10951 {
10952 string = glyph->object;
10953 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10954 }
10955 }
10956 }
10957
10958 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10959 w->cursor.x = x;
10960 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10961 w->cursor.y = row->y + dy;
10962
10963 if (w == XWINDOW (selected_window))
10964 {
10965 if (!row->continued_p
10966 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10967 && row->x == 0)
10968 {
10969 this_line_buffer = XBUFFER (w->buffer);
10970
10971 CHARPOS (this_line_start_pos)
10972 = MATRIX_ROW_START_CHARPOS (row) + delta;
10973 BYTEPOS (this_line_start_pos)
10974 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10975
10976 CHARPOS (this_line_end_pos)
10977 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10978 BYTEPOS (this_line_end_pos)
10979 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10980
10981 this_line_y = w->cursor.y;
10982 this_line_pixel_height = row->height;
10983 this_line_vpos = w->cursor.vpos;
10984 this_line_start_x = row->x;
10985 }
10986 else
10987 CHARPOS (this_line_start_pos) = 0;
10988 }
10989 }
10990
10991
10992 /* Run window scroll functions, if any, for WINDOW with new window
10993 start STARTP. Sets the window start of WINDOW to that position.
10994
10995 We assume that the window's buffer is really current. */
10996
10997 static INLINE struct text_pos
10998 run_window_scroll_functions (window, startp)
10999 Lisp_Object window;
11000 struct text_pos startp;
11001 {
11002 struct window *w = XWINDOW (window);
11003 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11004
11005 if (current_buffer != XBUFFER (w->buffer))
11006 abort ();
11007
11008 if (!NILP (Vwindow_scroll_functions))
11009 {
11010 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11011 make_number (CHARPOS (startp)));
11012 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11013 /* In case the hook functions switch buffers. */
11014 if (current_buffer != XBUFFER (w->buffer))
11015 set_buffer_internal_1 (XBUFFER (w->buffer));
11016 }
11017
11018 return startp;
11019 }
11020
11021
11022 /* Make sure the line containing the cursor is fully visible.
11023 A value of 1 means there is nothing to be done.
11024 (Either the line is fully visible, or it cannot be made so,
11025 or we cannot tell.)
11026 A value of 0 means the caller should do scrolling
11027 as if point had gone off the screen. */
11028
11029 static int
11030 make_cursor_line_fully_visible (w)
11031 struct window *w;
11032 {
11033 struct glyph_matrix *matrix;
11034 struct glyph_row *row;
11035 int window_height;
11036
11037 /* It's not always possible to find the cursor, e.g, when a window
11038 is full of overlay strings. Don't do anything in that case. */
11039 if (w->cursor.vpos < 0)
11040 return 1;
11041
11042 matrix = w->desired_matrix;
11043 row = MATRIX_ROW (matrix, w->cursor.vpos);
11044
11045 /* If the cursor row is not partially visible, there's nothing to do. */
11046 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11047 return 1;
11048
11049 /* If the row the cursor is in is taller than the window's height,
11050 it's not clear what to do, so do nothing. */
11051 window_height = window_box_height (w);
11052 if (row->height >= window_height)
11053 return 1;
11054
11055 return 0;
11056
11057 #if 0
11058 /* This code used to try to scroll the window just enough to make
11059 the line visible. It returned 0 to say that the caller should
11060 allocate larger glyph matrices. */
11061
11062 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11063 {
11064 int dy = row->height - row->visible_height;
11065 w->vscroll = 0;
11066 w->cursor.y += dy;
11067 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11068 }
11069 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11070 {
11071 int dy = - (row->height - row->visible_height);
11072 w->vscroll = dy;
11073 w->cursor.y += dy;
11074 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11075 }
11076
11077 /* When we change the cursor y-position of the selected window,
11078 change this_line_y as well so that the display optimization for
11079 the cursor line of the selected window in redisplay_internal uses
11080 the correct y-position. */
11081 if (w == XWINDOW (selected_window))
11082 this_line_y = w->cursor.y;
11083
11084 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11085 redisplay with larger matrices. */
11086 if (matrix->nrows < required_matrix_height (w))
11087 {
11088 fonts_changed_p = 1;
11089 return 0;
11090 }
11091
11092 return 1;
11093 #endif /* 0 */
11094 }
11095
11096
11097 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11098 non-zero means only WINDOW is redisplayed in redisplay_internal.
11099 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11100 in redisplay_window to bring a partially visible line into view in
11101 the case that only the cursor has moved.
11102
11103 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11104 last screen line's vertical height extends past the end of the screen.
11105
11106 Value is
11107
11108 1 if scrolling succeeded
11109
11110 0 if scrolling didn't find point.
11111
11112 -1 if new fonts have been loaded so that we must interrupt
11113 redisplay, adjust glyph matrices, and try again. */
11114
11115 enum
11116 {
11117 SCROLLING_SUCCESS,
11118 SCROLLING_FAILED,
11119 SCROLLING_NEED_LARGER_MATRICES
11120 };
11121
11122 static int
11123 try_scrolling (window, just_this_one_p, scroll_conservatively,
11124 scroll_step, temp_scroll_step, last_line_misfit)
11125 Lisp_Object window;
11126 int just_this_one_p;
11127 EMACS_INT scroll_conservatively, scroll_step;
11128 int temp_scroll_step;
11129 int last_line_misfit;
11130 {
11131 struct window *w = XWINDOW (window);
11132 struct frame *f = XFRAME (w->frame);
11133 struct text_pos scroll_margin_pos;
11134 struct text_pos pos;
11135 struct text_pos startp;
11136 struct it it;
11137 Lisp_Object window_end;
11138 int this_scroll_margin;
11139 int dy = 0;
11140 int scroll_max;
11141 int rc;
11142 int amount_to_scroll = 0;
11143 Lisp_Object aggressive;
11144 int height;
11145 int end_scroll_margin;
11146
11147 #if GLYPH_DEBUG
11148 debug_method_add (w, "try_scrolling");
11149 #endif
11150
11151 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11152
11153 /* Compute scroll margin height in pixels. We scroll when point is
11154 within this distance from the top or bottom of the window. */
11155 if (scroll_margin > 0)
11156 {
11157 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11158 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11159 }
11160 else
11161 this_scroll_margin = 0;
11162
11163 /* Compute how much we should try to scroll maximally to bring point
11164 into view. */
11165 if (scroll_step || scroll_conservatively || temp_scroll_step)
11166 scroll_max = max (scroll_step,
11167 max (scroll_conservatively, temp_scroll_step));
11168 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11169 || NUMBERP (current_buffer->scroll_up_aggressively))
11170 /* We're trying to scroll because of aggressive scrolling
11171 but no scroll_step is set. Choose an arbitrary one. Maybe
11172 there should be a variable for this. */
11173 scroll_max = 10;
11174 else
11175 scroll_max = 0;
11176 scroll_max *= FRAME_LINE_HEIGHT (f);
11177
11178 /* Decide whether we have to scroll down. Start at the window end
11179 and move this_scroll_margin up to find the position of the scroll
11180 margin. */
11181 window_end = Fwindow_end (window, Qt);
11182
11183 too_near_end:
11184
11185 CHARPOS (scroll_margin_pos) = XINT (window_end);
11186 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11187
11188 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
11189 if (end_scroll_margin)
11190 {
11191 start_display (&it, w, scroll_margin_pos);
11192 move_it_vertically (&it, - end_scroll_margin);
11193 scroll_margin_pos = it.current.pos;
11194 }
11195
11196 if (PT >= CHARPOS (scroll_margin_pos))
11197 {
11198 int y0;
11199
11200 /* Point is in the scroll margin at the bottom of the window, or
11201 below. Compute a new window start that makes point visible. */
11202
11203 /* Compute the distance from the scroll margin to PT.
11204 Give up if the distance is greater than scroll_max. */
11205 start_display (&it, w, scroll_margin_pos);
11206 y0 = it.current_y;
11207 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11208 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11209
11210 /* To make point visible, we have to move the window start
11211 down so that the line the cursor is in is visible, which
11212 means we have to add in the height of the cursor line. */
11213 dy = line_bottom_y (&it) - y0;
11214
11215 if (dy > scroll_max)
11216 return SCROLLING_FAILED;
11217
11218 /* Move the window start down. If scrolling conservatively,
11219 move it just enough down to make point visible. If
11220 scroll_step is set, move it down by scroll_step. */
11221 start_display (&it, w, startp);
11222
11223 if (scroll_conservatively)
11224 /* Set AMOUNT_TO_SCROLL to at least one line,
11225 and at most scroll_conservatively lines. */
11226 amount_to_scroll
11227 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11228 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11229 else if (scroll_step || temp_scroll_step)
11230 amount_to_scroll = scroll_max;
11231 else
11232 {
11233 aggressive = current_buffer->scroll_up_aggressively;
11234 height = WINDOW_BOX_TEXT_HEIGHT (w);
11235 if (NUMBERP (aggressive))
11236 amount_to_scroll = XFLOATINT (aggressive) * height;
11237 }
11238
11239 if (amount_to_scroll <= 0)
11240 return SCROLLING_FAILED;
11241
11242 /* If moving by amount_to_scroll leaves STARTP unchanged,
11243 move it down one screen line. */
11244
11245 move_it_vertically (&it, amount_to_scroll);
11246 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11247 move_it_by_lines (&it, 1, 1);
11248 startp = it.current.pos;
11249 }
11250 else
11251 {
11252 /* See if point is inside the scroll margin at the top of the
11253 window. */
11254 scroll_margin_pos = startp;
11255 if (this_scroll_margin)
11256 {
11257 start_display (&it, w, startp);
11258 move_it_vertically (&it, this_scroll_margin);
11259 scroll_margin_pos = it.current.pos;
11260 }
11261
11262 if (PT < CHARPOS (scroll_margin_pos))
11263 {
11264 /* Point is in the scroll margin at the top of the window or
11265 above what is displayed in the window. */
11266 int y0;
11267
11268 /* Compute the vertical distance from PT to the scroll
11269 margin position. Give up if distance is greater than
11270 scroll_max. */
11271 SET_TEXT_POS (pos, PT, PT_BYTE);
11272 start_display (&it, w, pos);
11273 y0 = it.current_y;
11274 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11275 it.last_visible_y, -1,
11276 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11277 dy = it.current_y - y0;
11278 if (dy > scroll_max)
11279 return SCROLLING_FAILED;
11280
11281 /* Compute new window start. */
11282 start_display (&it, w, startp);
11283
11284 if (scroll_conservatively)
11285 amount_to_scroll =
11286 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11287 else if (scroll_step || temp_scroll_step)
11288 amount_to_scroll = scroll_max;
11289 else
11290 {
11291 aggressive = current_buffer->scroll_down_aggressively;
11292 height = WINDOW_BOX_TEXT_HEIGHT (w);
11293 if (NUMBERP (aggressive))
11294 amount_to_scroll = XFLOATINT (aggressive) * height;
11295 }
11296
11297 if (amount_to_scroll <= 0)
11298 return SCROLLING_FAILED;
11299
11300 move_it_vertically (&it, - amount_to_scroll);
11301 startp = it.current.pos;
11302 }
11303 }
11304
11305 /* Run window scroll functions. */
11306 startp = run_window_scroll_functions (window, startp);
11307
11308 /* Display the window. Give up if new fonts are loaded, or if point
11309 doesn't appear. */
11310 if (!try_window (window, startp))
11311 rc = SCROLLING_NEED_LARGER_MATRICES;
11312 else if (w->cursor.vpos < 0)
11313 {
11314 clear_glyph_matrix (w->desired_matrix);
11315 rc = SCROLLING_FAILED;
11316 }
11317 else
11318 {
11319 /* Maybe forget recorded base line for line number display. */
11320 if (!just_this_one_p
11321 || current_buffer->clip_changed
11322 || BEG_UNCHANGED < CHARPOS (startp))
11323 w->base_line_number = Qnil;
11324
11325 /* If cursor ends up on a partially visible line,
11326 treat that as being off the bottom of the screen. */
11327 if (! make_cursor_line_fully_visible (w))
11328 {
11329 clear_glyph_matrix (w->desired_matrix);
11330 last_line_misfit = 1;
11331 goto too_near_end;
11332 }
11333 rc = SCROLLING_SUCCESS;
11334 }
11335
11336 return rc;
11337 }
11338
11339
11340 /* Compute a suitable window start for window W if display of W starts
11341 on a continuation line. Value is non-zero if a new window start
11342 was computed.
11343
11344 The new window start will be computed, based on W's width, starting
11345 from the start of the continued line. It is the start of the
11346 screen line with the minimum distance from the old start W->start. */
11347
11348 static int
11349 compute_window_start_on_continuation_line (w)
11350 struct window *w;
11351 {
11352 struct text_pos pos, start_pos;
11353 int window_start_changed_p = 0;
11354
11355 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11356
11357 /* If window start is on a continuation line... Window start may be
11358 < BEGV in case there's invisible text at the start of the
11359 buffer (M-x rmail, for example). */
11360 if (CHARPOS (start_pos) > BEGV
11361 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11362 {
11363 struct it it;
11364 struct glyph_row *row;
11365
11366 /* Handle the case that the window start is out of range. */
11367 if (CHARPOS (start_pos) < BEGV)
11368 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11369 else if (CHARPOS (start_pos) > ZV)
11370 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11371
11372 /* Find the start of the continued line. This should be fast
11373 because scan_buffer is fast (newline cache). */
11374 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11375 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11376 row, DEFAULT_FACE_ID);
11377 reseat_at_previous_visible_line_start (&it);
11378
11379 /* If the line start is "too far" away from the window start,
11380 say it takes too much time to compute a new window start. */
11381 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11382 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11383 {
11384 int min_distance, distance;
11385
11386 /* Move forward by display lines to find the new window
11387 start. If window width was enlarged, the new start can
11388 be expected to be > the old start. If window width was
11389 decreased, the new window start will be < the old start.
11390 So, we're looking for the display line start with the
11391 minimum distance from the old window start. */
11392 pos = it.current.pos;
11393 min_distance = INFINITY;
11394 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11395 distance < min_distance)
11396 {
11397 min_distance = distance;
11398 pos = it.current.pos;
11399 move_it_by_lines (&it, 1, 0);
11400 }
11401
11402 /* Set the window start there. */
11403 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11404 window_start_changed_p = 1;
11405 }
11406 }
11407
11408 return window_start_changed_p;
11409 }
11410
11411
11412 /* Try cursor movement in case text has not changed in window WINDOW,
11413 with window start STARTP. Value is
11414
11415 CURSOR_MOVEMENT_SUCCESS if successful
11416
11417 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11418
11419 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11420 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11421 we want to scroll as if scroll-step were set to 1. See the code.
11422
11423 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11424 which case we have to abort this redisplay, and adjust matrices
11425 first. */
11426
11427 enum
11428 {
11429 CURSOR_MOVEMENT_SUCCESS,
11430 CURSOR_MOVEMENT_CANNOT_BE_USED,
11431 CURSOR_MOVEMENT_MUST_SCROLL,
11432 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11433 };
11434
11435 static int
11436 try_cursor_movement (window, startp, scroll_step)
11437 Lisp_Object window;
11438 struct text_pos startp;
11439 int *scroll_step;
11440 {
11441 struct window *w = XWINDOW (window);
11442 struct frame *f = XFRAME (w->frame);
11443 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11444
11445 #if GLYPH_DEBUG
11446 if (inhibit_try_cursor_movement)
11447 return rc;
11448 #endif
11449
11450 /* Handle case where text has not changed, only point, and it has
11451 not moved off the frame. */
11452 if (/* Point may be in this window. */
11453 PT >= CHARPOS (startp)
11454 /* Selective display hasn't changed. */
11455 && !current_buffer->clip_changed
11456 /* Function force-mode-line-update is used to force a thorough
11457 redisplay. It sets either windows_or_buffers_changed or
11458 update_mode_lines. So don't take a shortcut here for these
11459 cases. */
11460 && !update_mode_lines
11461 && !windows_or_buffers_changed
11462 && !cursor_type_changed
11463 /* Can't use this case if highlighting a region. When a
11464 region exists, cursor movement has to do more than just
11465 set the cursor. */
11466 && !(!NILP (Vtransient_mark_mode)
11467 && !NILP (current_buffer->mark_active))
11468 && NILP (w->region_showing)
11469 && NILP (Vshow_trailing_whitespace)
11470 /* Right after splitting windows, last_point may be nil. */
11471 && INTEGERP (w->last_point)
11472 /* This code is not used for mini-buffer for the sake of the case
11473 of redisplaying to replace an echo area message; since in
11474 that case the mini-buffer contents per se are usually
11475 unchanged. This code is of no real use in the mini-buffer
11476 since the handling of this_line_start_pos, etc., in redisplay
11477 handles the same cases. */
11478 && !EQ (window, minibuf_window)
11479 /* When splitting windows or for new windows, it happens that
11480 redisplay is called with a nil window_end_vpos or one being
11481 larger than the window. This should really be fixed in
11482 window.c. I don't have this on my list, now, so we do
11483 approximately the same as the old redisplay code. --gerd. */
11484 && INTEGERP (w->window_end_vpos)
11485 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11486 && (FRAME_WINDOW_P (f)
11487 || !MARKERP (Voverlay_arrow_position)
11488 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11489 {
11490 int this_scroll_margin;
11491 struct glyph_row *row = NULL;
11492
11493 #if GLYPH_DEBUG
11494 debug_method_add (w, "cursor movement");
11495 #endif
11496
11497 /* Scroll if point within this distance from the top or bottom
11498 of the window. This is a pixel value. */
11499 this_scroll_margin = max (0, scroll_margin);
11500 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11501 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11502
11503 /* Start with the row the cursor was displayed during the last
11504 not paused redisplay. Give up if that row is not valid. */
11505 if (w->last_cursor.vpos < 0
11506 || w->last_cursor.vpos >= w->current_matrix->nrows)
11507 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11508 else
11509 {
11510 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11511 if (row->mode_line_p)
11512 ++row;
11513 if (!row->enabled_p)
11514 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11515 }
11516
11517 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11518 {
11519 int scroll_p = 0;
11520 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11521
11522 if (PT > XFASTINT (w->last_point))
11523 {
11524 /* Point has moved forward. */
11525 while (MATRIX_ROW_END_CHARPOS (row) < PT
11526 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11527 {
11528 xassert (row->enabled_p);
11529 ++row;
11530 }
11531
11532 /* The end position of a row equals the start position
11533 of the next row. If PT is there, we would rather
11534 display it in the next line. */
11535 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11536 && MATRIX_ROW_END_CHARPOS (row) == PT
11537 && !cursor_row_p (w, row))
11538 ++row;
11539
11540 /* If within the scroll margin, scroll. Note that
11541 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11542 the next line would be drawn, and that
11543 this_scroll_margin can be zero. */
11544 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11545 || PT > MATRIX_ROW_END_CHARPOS (row)
11546 /* Line is completely visible last line in window
11547 and PT is to be set in the next line. */
11548 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11549 && PT == MATRIX_ROW_END_CHARPOS (row)
11550 && !row->ends_at_zv_p
11551 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11552 scroll_p = 1;
11553 }
11554 else if (PT < XFASTINT (w->last_point))
11555 {
11556 /* Cursor has to be moved backward. Note that PT >=
11557 CHARPOS (startp) because of the outer
11558 if-statement. */
11559 while (!row->mode_line_p
11560 && (MATRIX_ROW_START_CHARPOS (row) > PT
11561 || (MATRIX_ROW_START_CHARPOS (row) == PT
11562 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11563 && (row->y > this_scroll_margin
11564 || CHARPOS (startp) == BEGV))
11565 {
11566 xassert (row->enabled_p);
11567 --row;
11568 }
11569
11570 /* Consider the following case: Window starts at BEGV,
11571 there is invisible, intangible text at BEGV, so that
11572 display starts at some point START > BEGV. It can
11573 happen that we are called with PT somewhere between
11574 BEGV and START. Try to handle that case. */
11575 if (row < w->current_matrix->rows
11576 || row->mode_line_p)
11577 {
11578 row = w->current_matrix->rows;
11579 if (row->mode_line_p)
11580 ++row;
11581 }
11582
11583 /* Due to newlines in overlay strings, we may have to
11584 skip forward over overlay strings. */
11585 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11586 && MATRIX_ROW_END_CHARPOS (row) == PT
11587 && !cursor_row_p (w, row))
11588 ++row;
11589
11590 /* If within the scroll margin, scroll. */
11591 if (row->y < this_scroll_margin
11592 && CHARPOS (startp) != BEGV)
11593 scroll_p = 1;
11594 }
11595
11596 if (PT < MATRIX_ROW_START_CHARPOS (row)
11597 || PT > MATRIX_ROW_END_CHARPOS (row))
11598 {
11599 /* if PT is not in the glyph row, give up. */
11600 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11601 }
11602 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11603 {
11604 if (PT == MATRIX_ROW_END_CHARPOS (row)
11605 && !row->ends_at_zv_p
11606 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11607 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11608 else if (row->height > window_box_height (w))
11609 {
11610 /* If we end up in a partially visible line, let's
11611 make it fully visible, except when it's taller
11612 than the window, in which case we can't do much
11613 about it. */
11614 *scroll_step = 1;
11615 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11616 }
11617 else
11618 {
11619 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11620 if (!make_cursor_line_fully_visible (w))
11621 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11622 else
11623 rc = CURSOR_MOVEMENT_SUCCESS;
11624 }
11625 }
11626 else if (scroll_p)
11627 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11628 else
11629 {
11630 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11631 rc = CURSOR_MOVEMENT_SUCCESS;
11632 }
11633 }
11634 }
11635
11636 return rc;
11637 }
11638
11639 void
11640 set_vertical_scroll_bar (w)
11641 struct window *w;
11642 {
11643 int start, end, whole;
11644
11645 /* Calculate the start and end positions for the current window.
11646 At some point, it would be nice to choose between scrollbars
11647 which reflect the whole buffer size, with special markers
11648 indicating narrowing, and scrollbars which reflect only the
11649 visible region.
11650
11651 Note that mini-buffers sometimes aren't displaying any text. */
11652 if (!MINI_WINDOW_P (w)
11653 || (w == XWINDOW (minibuf_window)
11654 && NILP (echo_area_buffer[0])))
11655 {
11656 struct buffer *buf = XBUFFER (w->buffer);
11657 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11658 start = marker_position (w->start) - BUF_BEGV (buf);
11659 /* I don't think this is guaranteed to be right. For the
11660 moment, we'll pretend it is. */
11661 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11662
11663 if (end < start)
11664 end = start;
11665 if (whole < (end - start))
11666 whole = end - start;
11667 }
11668 else
11669 start = end = whole = 0;
11670
11671 /* Indicate what this scroll bar ought to be displaying now. */
11672 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11673 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11674 (w, end - start, whole, start);
11675 }
11676
11677 #ifdef HAVE_WINDOW_SYSTEM
11678
11679 /* Recalculate the bitmaps to show in the fringes of window W.
11680 If FORCE_P is 0, only mark rows with modified bitmaps for update in
11681 redraw_fringe_bitmaps_p; else mark all rows for update. */
11682
11683 int
11684 update_window_fringes (w, force_p)
11685 struct window *w;
11686 int force_p;
11687 {
11688 struct glyph_row *row, *cur = 0;
11689 int yb = window_text_bottom_y (w);
11690 int rn, nrows = w->current_matrix->nrows;
11691 int y;
11692 int redraw_p = 0;
11693 Lisp_Object ind;
11694
11695 if (w->pseudo_window_p)
11696 return 0;
11697
11698 if (!MINI_WINDOW_P (w)
11699 && (ind = XBUFFER (w->buffer)->indicate_buffer_boundaries, !NILP (ind)))
11700 {
11701 int do_eob = 1, do_bob = 1;
11702
11703 for (y = 0, rn = 0;
11704 y < yb && rn < nrows;
11705 y += row->height, ++rn)
11706 {
11707 unsigned indicate_bob_p, indicate_top_line_p;
11708 unsigned indicate_eob_p, indicate_bottom_line_p;
11709
11710 row = w->desired_matrix->rows + rn;
11711 if (!row->enabled_p)
11712 row = w->current_matrix->rows + rn;
11713
11714 indicate_bob_p = row->indicate_bob_p;
11715 indicate_top_line_p = row->indicate_top_line_p;
11716 indicate_eob_p = row->indicate_eob_p;
11717 indicate_bottom_line_p = row->indicate_bottom_line_p;
11718
11719 row->indicate_bob_p = row->indicate_top_line_p = 0;
11720 row->indicate_eob_p = row->indicate_bottom_line_p = 0;
11721
11722 if (MATRIX_ROW_START_CHARPOS (row) <= BUF_BEGV (XBUFFER (w->buffer)))
11723 row->indicate_bob_p = do_bob, do_bob = 0;
11724 else if (EQ (ind, Qt)
11725 && (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0) == rn)
11726 row->indicate_top_line_p = 1;
11727
11728 if (MATRIX_ROW_END_CHARPOS (row) >= BUF_ZV (XBUFFER (w->buffer)))
11729 row->indicate_eob_p = do_eob, do_eob = 0;
11730 else if (EQ (ind, Qt)
11731 && y + row->height >= yb)
11732 row->indicate_bottom_line_p = 1;
11733
11734 if (indicate_bob_p != row->indicate_bob_p
11735 || indicate_top_line_p != row->indicate_top_line_p
11736 || indicate_eob_p != row->indicate_eob_p
11737 || indicate_bottom_line_p != row->indicate_bottom_line_p)
11738 row->redraw_fringe_bitmaps_p = 1;
11739 }
11740 }
11741
11742 for (y = 0, rn = 0;
11743 y < yb && rn < nrows;
11744 y += row->height, rn++)
11745 {
11746 enum fringe_bitmap_type left, right;
11747
11748 row = w->desired_matrix->rows + rn;
11749 cur = w->current_matrix->rows + rn;
11750 if (!row->enabled_p)
11751 row = cur;
11752
11753 /* Decide which bitmap to draw in the left fringe. */
11754 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
11755 left = NO_FRINGE_BITMAP;
11756 else if (row->overlay_arrow_p)
11757 left = OVERLAY_ARROW_BITMAP;
11758 else if (row->truncated_on_left_p)
11759 left = LEFT_TRUNCATION_BITMAP;
11760 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
11761 left = CONTINUATION_LINE_BITMAP;
11762 else if (row->indicate_empty_line_p)
11763 left = ZV_LINE_BITMAP;
11764 else if (row->indicate_bob_p)
11765 left = FIRST_LINE_BITMAP;
11766 else
11767 left = NO_FRINGE_BITMAP;
11768
11769 /* Decide which bitmap to draw in the right fringe. */
11770 if (WINDOW_RIGHT_FRINGE_WIDTH (w) == 0)
11771 right = NO_FRINGE_BITMAP;
11772 else if (row->truncated_on_right_p)
11773 right = RIGHT_TRUNCATION_BITMAP;
11774 else if (row->continued_p)
11775 right = CONTINUED_LINE_BITMAP;
11776 else if (row->indicate_eob_p)
11777 right = LAST_LINE_BITMAP;
11778 else if (row->indicate_top_line_p)
11779 right = UP_ARROW_BITMAP;
11780 else if (row->indicate_bottom_line_p)
11781 right = DOWN_ARROW_BITMAP;
11782 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
11783 right = ZV_LINE_BITMAP;
11784 else
11785 right = NO_FRINGE_BITMAP;
11786
11787 if (force_p
11788 || row->y != cur->y
11789 || row->visible_height != cur->visible_height
11790 || left != cur->left_fringe_bitmap
11791 || right != cur->right_fringe_bitmap
11792 || cur->redraw_fringe_bitmaps_p)
11793 {
11794 redraw_p = row->redraw_fringe_bitmaps_p = cur->redraw_fringe_bitmaps_p = 1;
11795 cur->left_fringe_bitmap = left;
11796 cur->right_fringe_bitmap = right;
11797 }
11798
11799 row->left_fringe_bitmap = left;
11800 row->right_fringe_bitmap = right;
11801 }
11802
11803 return redraw_p;
11804 }
11805
11806 #endif /* HAVE_WINDOW_SYSTEM */
11807
11808 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11809 selected_window is redisplayed.
11810
11811 We can return without actually redisplaying the window if
11812 fonts_changed_p is nonzero. In that case, redisplay_internal will
11813 retry. */
11814
11815 static void
11816 redisplay_window (window, just_this_one_p)
11817 Lisp_Object window;
11818 int just_this_one_p;
11819 {
11820 struct window *w = XWINDOW (window);
11821 struct frame *f = XFRAME (w->frame);
11822 struct buffer *buffer = XBUFFER (w->buffer);
11823 struct buffer *old = current_buffer;
11824 struct text_pos lpoint, opoint, startp;
11825 int update_mode_line;
11826 int tem;
11827 struct it it;
11828 /* Record it now because it's overwritten. */
11829 int current_matrix_up_to_date_p = 0;
11830 int used_current_matrix_p = 0;
11831 /* This is less strict than current_matrix_up_to_date_p.
11832 It indictes that the buffer contents and narrowing are unchanged. */
11833 int buffer_unchanged_p = 0;
11834 int temp_scroll_step = 0;
11835 int count = SPECPDL_INDEX ();
11836 int rc;
11837 int centering_position;
11838 int last_line_misfit = 0;
11839
11840 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11841 opoint = lpoint;
11842
11843 /* W must be a leaf window here. */
11844 xassert (!NILP (w->buffer));
11845 #if GLYPH_DEBUG
11846 *w->desired_matrix->method = 0;
11847 #endif
11848
11849 specbind (Qinhibit_point_motion_hooks, Qt);
11850
11851 reconsider_clip_changes (w, buffer);
11852
11853 /* Has the mode line to be updated? */
11854 update_mode_line = (!NILP (w->update_mode_line)
11855 || update_mode_lines
11856 || buffer->clip_changed
11857 || buffer->prevent_redisplay_optimizations_p);
11858
11859 if (MINI_WINDOW_P (w))
11860 {
11861 if (w == XWINDOW (echo_area_window)
11862 && !NILP (echo_area_buffer[0]))
11863 {
11864 if (update_mode_line)
11865 /* We may have to update a tty frame's menu bar or a
11866 tool-bar. Example `M-x C-h C-h C-g'. */
11867 goto finish_menu_bars;
11868 else
11869 /* We've already displayed the echo area glyphs in this window. */
11870 goto finish_scroll_bars;
11871 }
11872 else if ((w != XWINDOW (minibuf_window)
11873 || minibuf_level == 0)
11874 /* When buffer is nonempty, redisplay window normally. */
11875 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11876 /* Quail displays non-mini buffers in minibuffer window.
11877 In that case, redisplay the window normally. */
11878 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11879 {
11880 /* W is a mini-buffer window, but it's not active, so clear
11881 it. */
11882 int yb = window_text_bottom_y (w);
11883 struct glyph_row *row;
11884 int y;
11885
11886 for (y = 0, row = w->desired_matrix->rows;
11887 y < yb;
11888 y += row->height, ++row)
11889 blank_row (w, row, y);
11890 goto finish_scroll_bars;
11891 }
11892
11893 clear_glyph_matrix (w->desired_matrix);
11894 }
11895
11896 /* Otherwise set up data on this window; select its buffer and point
11897 value. */
11898 /* Really select the buffer, for the sake of buffer-local
11899 variables. */
11900 set_buffer_internal_1 (XBUFFER (w->buffer));
11901 SET_TEXT_POS (opoint, PT, PT_BYTE);
11902
11903 current_matrix_up_to_date_p
11904 = (!NILP (w->window_end_valid)
11905 && !current_buffer->clip_changed
11906 && !current_buffer->prevent_redisplay_optimizations_p
11907 && XFASTINT (w->last_modified) >= MODIFF
11908 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11909
11910 buffer_unchanged_p
11911 = (!NILP (w->window_end_valid)
11912 && !current_buffer->clip_changed
11913 && XFASTINT (w->last_modified) >= MODIFF
11914 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11915
11916 /* When windows_or_buffers_changed is non-zero, we can't rely on
11917 the window end being valid, so set it to nil there. */
11918 if (windows_or_buffers_changed)
11919 {
11920 /* If window starts on a continuation line, maybe adjust the
11921 window start in case the window's width changed. */
11922 if (XMARKER (w->start)->buffer == current_buffer)
11923 compute_window_start_on_continuation_line (w);
11924
11925 w->window_end_valid = Qnil;
11926 }
11927
11928 /* Some sanity checks. */
11929 CHECK_WINDOW_END (w);
11930 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11931 abort ();
11932 if (BYTEPOS (opoint) < CHARPOS (opoint))
11933 abort ();
11934
11935 /* If %c is in mode line, update it if needed. */
11936 if (!NILP (w->column_number_displayed)
11937 /* This alternative quickly identifies a common case
11938 where no change is needed. */
11939 && !(PT == XFASTINT (w->last_point)
11940 && XFASTINT (w->last_modified) >= MODIFF
11941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11942 && (XFASTINT (w->column_number_displayed)
11943 != (int) current_column ())) /* iftc */
11944 update_mode_line = 1;
11945
11946 /* Count number of windows showing the selected buffer. An indirect
11947 buffer counts as its base buffer. */
11948 if (!just_this_one_p)
11949 {
11950 struct buffer *current_base, *window_base;
11951 current_base = current_buffer;
11952 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11953 if (current_base->base_buffer)
11954 current_base = current_base->base_buffer;
11955 if (window_base->base_buffer)
11956 window_base = window_base->base_buffer;
11957 if (current_base == window_base)
11958 buffer_shared++;
11959 }
11960
11961 /* Point refers normally to the selected window. For any other
11962 window, set up appropriate value. */
11963 if (!EQ (window, selected_window))
11964 {
11965 int new_pt = XMARKER (w->pointm)->charpos;
11966 int new_pt_byte = marker_byte_position (w->pointm);
11967 if (new_pt < BEGV)
11968 {
11969 new_pt = BEGV;
11970 new_pt_byte = BEGV_BYTE;
11971 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11972 }
11973 else if (new_pt > (ZV - 1))
11974 {
11975 new_pt = ZV;
11976 new_pt_byte = ZV_BYTE;
11977 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11978 }
11979
11980 /* We don't use SET_PT so that the point-motion hooks don't run. */
11981 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11982 }
11983
11984 /* If any of the character widths specified in the display table
11985 have changed, invalidate the width run cache. It's true that
11986 this may be a bit late to catch such changes, but the rest of
11987 redisplay goes (non-fatally) haywire when the display table is
11988 changed, so why should we worry about doing any better? */
11989 if (current_buffer->width_run_cache)
11990 {
11991 struct Lisp_Char_Table *disptab = buffer_display_table ();
11992
11993 if (! disptab_matches_widthtab (disptab,
11994 XVECTOR (current_buffer->width_table)))
11995 {
11996 invalidate_region_cache (current_buffer,
11997 current_buffer->width_run_cache,
11998 BEG, Z);
11999 recompute_width_table (current_buffer, disptab);
12000 }
12001 }
12002
12003 /* If window-start is screwed up, choose a new one. */
12004 if (XMARKER (w->start)->buffer != current_buffer)
12005 goto recenter;
12006
12007 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12008
12009 /* If someone specified a new starting point but did not insist,
12010 check whether it can be used. */
12011 if (!NILP (w->optional_new_start)
12012 && CHARPOS (startp) >= BEGV
12013 && CHARPOS (startp) <= ZV)
12014 {
12015 w->optional_new_start = Qnil;
12016 start_display (&it, w, startp);
12017 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12018 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12019 if (IT_CHARPOS (it) == PT)
12020 w->force_start = Qt;
12021 /* IT may overshoot PT if text at PT is invisible. */
12022 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12023 w->force_start = Qt;
12024
12025
12026 }
12027
12028 /* Handle case where place to start displaying has been specified,
12029 unless the specified location is outside the accessible range. */
12030 if (!NILP (w->force_start)
12031 || w->frozen_window_start_p)
12032 {
12033 /* We set this later on if we have to adjust point. */
12034 int new_vpos = -1;
12035
12036 w->force_start = Qnil;
12037 w->vscroll = 0;
12038 w->window_end_valid = Qnil;
12039
12040 /* Forget any recorded base line for line number display. */
12041 if (!buffer_unchanged_p)
12042 w->base_line_number = Qnil;
12043
12044 /* Redisplay the mode line. Select the buffer properly for that.
12045 Also, run the hook window-scroll-functions
12046 because we have scrolled. */
12047 /* Note, we do this after clearing force_start because
12048 if there's an error, it is better to forget about force_start
12049 than to get into an infinite loop calling the hook functions
12050 and having them get more errors. */
12051 if (!update_mode_line
12052 || ! NILP (Vwindow_scroll_functions))
12053 {
12054 update_mode_line = 1;
12055 w->update_mode_line = Qt;
12056 startp = run_window_scroll_functions (window, startp);
12057 }
12058
12059 w->last_modified = make_number (0);
12060 w->last_overlay_modified = make_number (0);
12061 if (CHARPOS (startp) < BEGV)
12062 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12063 else if (CHARPOS (startp) > ZV)
12064 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12065
12066 /* Redisplay, then check if cursor has been set during the
12067 redisplay. Give up if new fonts were loaded. */
12068 if (!try_window (window, startp))
12069 {
12070 w->force_start = Qt;
12071 clear_glyph_matrix (w->desired_matrix);
12072 goto need_larger_matrices;
12073 }
12074
12075 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12076 {
12077 /* If point does not appear, try to move point so it does
12078 appear. The desired matrix has been built above, so we
12079 can use it here. */
12080 new_vpos = window_box_height (w) / 2;
12081 }
12082
12083 if (!make_cursor_line_fully_visible (w))
12084 {
12085 /* Point does appear, but on a line partly visible at end of window.
12086 Move it back to a fully-visible line. */
12087 new_vpos = window_box_height (w);
12088 }
12089
12090 /* If we need to move point for either of the above reasons,
12091 now actually do it. */
12092 if (new_vpos >= 0)
12093 {
12094 struct glyph_row *row;
12095
12096 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12097 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12098 ++row;
12099
12100 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12101 MATRIX_ROW_START_BYTEPOS (row));
12102
12103 if (w != XWINDOW (selected_window))
12104 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12105 else if (current_buffer == old)
12106 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12107
12108 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12109
12110 /* If we are highlighting the region, then we just changed
12111 the region, so redisplay to show it. */
12112 if (!NILP (Vtransient_mark_mode)
12113 && !NILP (current_buffer->mark_active))
12114 {
12115 clear_glyph_matrix (w->desired_matrix);
12116 if (!try_window (window, startp))
12117 goto need_larger_matrices;
12118 }
12119 }
12120
12121 #if GLYPH_DEBUG
12122 debug_method_add (w, "forced window start");
12123 #endif
12124 goto done;
12125 }
12126
12127 /* Handle case where text has not changed, only point, and it has
12128 not moved off the frame, and we are not retrying after hscroll.
12129 (current_matrix_up_to_date_p is nonzero when retrying.) */
12130 if (current_matrix_up_to_date_p
12131 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12132 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12133 {
12134 switch (rc)
12135 {
12136 case CURSOR_MOVEMENT_SUCCESS:
12137 used_current_matrix_p = 1;
12138 goto done;
12139
12140 #if 0 /* try_cursor_movement never returns this value. */
12141 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12142 goto need_larger_matrices;
12143 #endif
12144
12145 case CURSOR_MOVEMENT_MUST_SCROLL:
12146 goto try_to_scroll;
12147
12148 default:
12149 abort ();
12150 }
12151 }
12152 /* If current starting point was originally the beginning of a line
12153 but no longer is, find a new starting point. */
12154 else if (!NILP (w->start_at_line_beg)
12155 && !(CHARPOS (startp) <= BEGV
12156 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12157 {
12158 #if GLYPH_DEBUG
12159 debug_method_add (w, "recenter 1");
12160 #endif
12161 goto recenter;
12162 }
12163
12164 /* Try scrolling with try_window_id. Value is > 0 if update has
12165 been done, it is -1 if we know that the same window start will
12166 not work. It is 0 if unsuccessful for some other reason. */
12167 else if ((tem = try_window_id (w)) != 0)
12168 {
12169 #if GLYPH_DEBUG
12170 debug_method_add (w, "try_window_id %d", tem);
12171 #endif
12172
12173 if (fonts_changed_p)
12174 goto need_larger_matrices;
12175 if (tem > 0)
12176 goto done;
12177
12178 /* Otherwise try_window_id has returned -1 which means that we
12179 don't want the alternative below this comment to execute. */
12180 }
12181 else if (CHARPOS (startp) >= BEGV
12182 && CHARPOS (startp) <= ZV
12183 && PT >= CHARPOS (startp)
12184 && (CHARPOS (startp) < ZV
12185 /* Avoid starting at end of buffer. */
12186 || CHARPOS (startp) == BEGV
12187 || (XFASTINT (w->last_modified) >= MODIFF
12188 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12189 {
12190 #if GLYPH_DEBUG
12191 debug_method_add (w, "same window start");
12192 #endif
12193
12194 /* Try to redisplay starting at same place as before.
12195 If point has not moved off frame, accept the results. */
12196 if (!current_matrix_up_to_date_p
12197 /* Don't use try_window_reusing_current_matrix in this case
12198 because a window scroll function can have changed the
12199 buffer. */
12200 || !NILP (Vwindow_scroll_functions)
12201 || MINI_WINDOW_P (w)
12202 || !(used_current_matrix_p =
12203 try_window_reusing_current_matrix (w)))
12204 {
12205 IF_DEBUG (debug_method_add (w, "1"));
12206 try_window (window, startp);
12207 }
12208
12209 if (fonts_changed_p)
12210 goto need_larger_matrices;
12211
12212 if (w->cursor.vpos >= 0)
12213 {
12214 if (!just_this_one_p
12215 || current_buffer->clip_changed
12216 || BEG_UNCHANGED < CHARPOS (startp))
12217 /* Forget any recorded base line for line number display. */
12218 w->base_line_number = Qnil;
12219
12220 if (!make_cursor_line_fully_visible (w))
12221 {
12222 clear_glyph_matrix (w->desired_matrix);
12223 last_line_misfit = 1;
12224 }
12225 /* Drop through and scroll. */
12226 else
12227 goto done;
12228 }
12229 else
12230 clear_glyph_matrix (w->desired_matrix);
12231 }
12232
12233 try_to_scroll:
12234
12235 w->last_modified = make_number (0);
12236 w->last_overlay_modified = make_number (0);
12237
12238 /* Redisplay the mode line. Select the buffer properly for that. */
12239 if (!update_mode_line)
12240 {
12241 update_mode_line = 1;
12242 w->update_mode_line = Qt;
12243 }
12244
12245 /* Try to scroll by specified few lines. */
12246 if ((scroll_conservatively
12247 || scroll_step
12248 || temp_scroll_step
12249 || NUMBERP (current_buffer->scroll_up_aggressively)
12250 || NUMBERP (current_buffer->scroll_down_aggressively))
12251 && !current_buffer->clip_changed
12252 && CHARPOS (startp) >= BEGV
12253 && CHARPOS (startp) <= ZV)
12254 {
12255 /* The function returns -1 if new fonts were loaded, 1 if
12256 successful, 0 if not successful. */
12257 int rc = try_scrolling (window, just_this_one_p,
12258 scroll_conservatively,
12259 scroll_step,
12260 temp_scroll_step, last_line_misfit);
12261 switch (rc)
12262 {
12263 case SCROLLING_SUCCESS:
12264 goto done;
12265
12266 case SCROLLING_NEED_LARGER_MATRICES:
12267 goto need_larger_matrices;
12268
12269 case SCROLLING_FAILED:
12270 break;
12271
12272 default:
12273 abort ();
12274 }
12275 }
12276
12277 /* Finally, just choose place to start which centers point */
12278
12279 recenter:
12280 centering_position = window_box_height (w) / 2;
12281
12282 point_at_top:
12283 /* Jump here with centering_position already set to 0. */
12284
12285 #if GLYPH_DEBUG
12286 debug_method_add (w, "recenter");
12287 #endif
12288
12289 /* w->vscroll = 0; */
12290
12291 /* Forget any previously recorded base line for line number display. */
12292 if (!buffer_unchanged_p)
12293 w->base_line_number = Qnil;
12294
12295 /* Move backward half the height of the window. */
12296 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12297 it.current_y = it.last_visible_y;
12298 move_it_vertically_backward (&it, centering_position);
12299 xassert (IT_CHARPOS (it) >= BEGV);
12300
12301 /* The function move_it_vertically_backward may move over more
12302 than the specified y-distance. If it->w is small, e.g. a
12303 mini-buffer window, we may end up in front of the window's
12304 display area. Start displaying at the start of the line
12305 containing PT in this case. */
12306 if (it.current_y <= 0)
12307 {
12308 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12309 move_it_vertically (&it, 0);
12310 xassert (IT_CHARPOS (it) <= PT);
12311 it.current_y = 0;
12312 }
12313
12314 it.current_x = it.hpos = 0;
12315
12316 /* Set startp here explicitly in case that helps avoid an infinite loop
12317 in case the window-scroll-functions functions get errors. */
12318 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12319
12320 /* Run scroll hooks. */
12321 startp = run_window_scroll_functions (window, it.current.pos);
12322
12323 /* Redisplay the window. */
12324 if (!current_matrix_up_to_date_p
12325 || windows_or_buffers_changed
12326 || cursor_type_changed
12327 /* Don't use try_window_reusing_current_matrix in this case
12328 because it can have changed the buffer. */
12329 || !NILP (Vwindow_scroll_functions)
12330 || !just_this_one_p
12331 || MINI_WINDOW_P (w)
12332 || !(used_current_matrix_p =
12333 try_window_reusing_current_matrix (w)))
12334 try_window (window, startp);
12335
12336 /* If new fonts have been loaded (due to fontsets), give up. We
12337 have to start a new redisplay since we need to re-adjust glyph
12338 matrices. */
12339 if (fonts_changed_p)
12340 goto need_larger_matrices;
12341
12342 /* If cursor did not appear assume that the middle of the window is
12343 in the first line of the window. Do it again with the next line.
12344 (Imagine a window of height 100, displaying two lines of height
12345 60. Moving back 50 from it->last_visible_y will end in the first
12346 line.) */
12347 if (w->cursor.vpos < 0)
12348 {
12349 if (!NILP (w->window_end_valid)
12350 && PT >= Z - XFASTINT (w->window_end_pos))
12351 {
12352 clear_glyph_matrix (w->desired_matrix);
12353 move_it_by_lines (&it, 1, 0);
12354 try_window (window, it.current.pos);
12355 }
12356 else if (PT < IT_CHARPOS (it))
12357 {
12358 clear_glyph_matrix (w->desired_matrix);
12359 move_it_by_lines (&it, -1, 0);
12360 try_window (window, it.current.pos);
12361 }
12362 else
12363 {
12364 /* Not much we can do about it. */
12365 }
12366 }
12367
12368 /* Consider the following case: Window starts at BEGV, there is
12369 invisible, intangible text at BEGV, so that display starts at
12370 some point START > BEGV. It can happen that we are called with
12371 PT somewhere between BEGV and START. Try to handle that case. */
12372 if (w->cursor.vpos < 0)
12373 {
12374 struct glyph_row *row = w->current_matrix->rows;
12375 if (row->mode_line_p)
12376 ++row;
12377 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12378 }
12379
12380 if (!make_cursor_line_fully_visible (w))
12381 {
12382 /* If vscroll is enabled, disable it and try again. */
12383 if (w->vscroll)
12384 {
12385 w->vscroll = 0;
12386 clear_glyph_matrix (w->desired_matrix);
12387 goto recenter;
12388 }
12389
12390 /* If centering point failed to make the whole line visible,
12391 put point at the top instead. That has to make the whole line
12392 visible, if it can be done. */
12393 centering_position = 0;
12394 goto point_at_top;
12395 }
12396
12397 done:
12398
12399 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12400 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12401 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12402 ? Qt : Qnil);
12403
12404 /* Display the mode line, if we must. */
12405 if ((update_mode_line
12406 /* If window not full width, must redo its mode line
12407 if (a) the window to its side is being redone and
12408 (b) we do a frame-based redisplay. This is a consequence
12409 of how inverted lines are drawn in frame-based redisplay. */
12410 || (!just_this_one_p
12411 && !FRAME_WINDOW_P (f)
12412 && !WINDOW_FULL_WIDTH_P (w))
12413 /* Line number to display. */
12414 || INTEGERP (w->base_line_pos)
12415 /* Column number is displayed and different from the one displayed. */
12416 || (!NILP (w->column_number_displayed)
12417 && (XFASTINT (w->column_number_displayed)
12418 != (int) current_column ()))) /* iftc */
12419 /* This means that the window has a mode line. */
12420 && (WINDOW_WANTS_MODELINE_P (w)
12421 || WINDOW_WANTS_HEADER_LINE_P (w)))
12422 {
12423 display_mode_lines (w);
12424
12425 /* If mode line height has changed, arrange for a thorough
12426 immediate redisplay using the correct mode line height. */
12427 if (WINDOW_WANTS_MODELINE_P (w)
12428 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12429 {
12430 fonts_changed_p = 1;
12431 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12432 = DESIRED_MODE_LINE_HEIGHT (w);
12433 }
12434
12435 /* If top line height has changed, arrange for a thorough
12436 immediate redisplay using the correct mode line height. */
12437 if (WINDOW_WANTS_HEADER_LINE_P (w)
12438 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12439 {
12440 fonts_changed_p = 1;
12441 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12442 = DESIRED_HEADER_LINE_HEIGHT (w);
12443 }
12444
12445 if (fonts_changed_p)
12446 goto need_larger_matrices;
12447 }
12448
12449 if (!line_number_displayed
12450 && !BUFFERP (w->base_line_pos))
12451 {
12452 w->base_line_pos = Qnil;
12453 w->base_line_number = Qnil;
12454 }
12455
12456 finish_menu_bars:
12457
12458 /* When we reach a frame's selected window, redo the frame's menu bar. */
12459 if (update_mode_line
12460 && EQ (FRAME_SELECTED_WINDOW (f), window))
12461 {
12462 int redisplay_menu_p = 0;
12463 int redisplay_tool_bar_p = 0;
12464
12465 if (FRAME_WINDOW_P (f))
12466 {
12467 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12468 || defined (USE_GTK)
12469 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12470 #else
12471 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12472 #endif
12473 }
12474 else
12475 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12476
12477 if (redisplay_menu_p)
12478 display_menu_bar (w);
12479
12480 #ifdef HAVE_WINDOW_SYSTEM
12481 #ifdef USE_GTK
12482 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12483 #else
12484 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12485 && (FRAME_TOOL_BAR_LINES (f) > 0
12486 || auto_resize_tool_bars_p);
12487
12488 #endif
12489
12490 if (redisplay_tool_bar_p)
12491 redisplay_tool_bar (f);
12492 #endif
12493 }
12494
12495 #ifdef HAVE_WINDOW_SYSTEM
12496 if (update_window_fringes (w, 0)
12497 && (used_current_matrix_p || overlay_arrow_seen)
12498 && !w->pseudo_window_p)
12499 {
12500 update_begin (f);
12501 BLOCK_INPUT;
12502 draw_window_fringes (w);
12503 UNBLOCK_INPUT;
12504 update_end (f);
12505 }
12506 #endif /* HAVE_WINDOW_SYSTEM */
12507
12508 /* We go to this label, with fonts_changed_p nonzero,
12509 if it is necessary to try again using larger glyph matrices.
12510 We have to redeem the scroll bar even in this case,
12511 because the loop in redisplay_internal expects that. */
12512 need_larger_matrices:
12513 ;
12514 finish_scroll_bars:
12515
12516 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12517 {
12518 /* Set the thumb's position and size. */
12519 set_vertical_scroll_bar (w);
12520
12521 /* Note that we actually used the scroll bar attached to this
12522 window, so it shouldn't be deleted at the end of redisplay. */
12523 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12524 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12525 }
12526
12527 /* Restore current_buffer and value of point in it. */
12528 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12529 set_buffer_internal_1 (old);
12530 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12531
12532 unbind_to (count, Qnil);
12533 }
12534
12535
12536 /* Build the complete desired matrix of WINDOW with a window start
12537 buffer position POS. Value is non-zero if successful. It is zero
12538 if fonts were loaded during redisplay which makes re-adjusting
12539 glyph matrices necessary. */
12540
12541 int
12542 try_window (window, pos)
12543 Lisp_Object window;
12544 struct text_pos pos;
12545 {
12546 struct window *w = XWINDOW (window);
12547 struct it it;
12548 struct glyph_row *last_text_row = NULL;
12549
12550 /* Make POS the new window start. */
12551 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12552
12553 /* Mark cursor position as unknown. No overlay arrow seen. */
12554 w->cursor.vpos = -1;
12555 overlay_arrow_seen = 0;
12556
12557 /* Initialize iterator and info to start at POS. */
12558 start_display (&it, w, pos);
12559
12560 /* Display all lines of W. */
12561 while (it.current_y < it.last_visible_y)
12562 {
12563 if (display_line (&it))
12564 last_text_row = it.glyph_row - 1;
12565 if (fonts_changed_p)
12566 return 0;
12567 }
12568
12569 /* If bottom moved off end of frame, change mode line percentage. */
12570 if (XFASTINT (w->window_end_pos) <= 0
12571 && Z != IT_CHARPOS (it))
12572 w->update_mode_line = Qt;
12573
12574 /* Set window_end_pos to the offset of the last character displayed
12575 on the window from the end of current_buffer. Set
12576 window_end_vpos to its row number. */
12577 if (last_text_row)
12578 {
12579 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12580 w->window_end_bytepos
12581 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12582 w->window_end_pos
12583 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12584 w->window_end_vpos
12585 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12586 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12587 ->displays_text_p);
12588 }
12589 else
12590 {
12591 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12592 w->window_end_pos = make_number (Z - ZV);
12593 w->window_end_vpos = make_number (0);
12594 }
12595
12596 /* But that is not valid info until redisplay finishes. */
12597 w->window_end_valid = Qnil;
12598 return 1;
12599 }
12600
12601
12602 \f
12603 /************************************************************************
12604 Window redisplay reusing current matrix when buffer has not changed
12605 ************************************************************************/
12606
12607 /* Try redisplay of window W showing an unchanged buffer with a
12608 different window start than the last time it was displayed by
12609 reusing its current matrix. Value is non-zero if successful.
12610 W->start is the new window start. */
12611
12612 static int
12613 try_window_reusing_current_matrix (w)
12614 struct window *w;
12615 {
12616 struct frame *f = XFRAME (w->frame);
12617 struct glyph_row *row, *bottom_row;
12618 struct it it;
12619 struct run run;
12620 struct text_pos start, new_start;
12621 int nrows_scrolled, i;
12622 struct glyph_row *last_text_row;
12623 struct glyph_row *last_reused_text_row;
12624 struct glyph_row *start_row;
12625 int start_vpos, min_y, max_y;
12626
12627 #if GLYPH_DEBUG
12628 if (inhibit_try_window_reusing)
12629 return 0;
12630 #endif
12631
12632 if (/* This function doesn't handle terminal frames. */
12633 !FRAME_WINDOW_P (f)
12634 /* Don't try to reuse the display if windows have been split
12635 or such. */
12636 || windows_or_buffers_changed
12637 || cursor_type_changed)
12638 return 0;
12639
12640 /* Can't do this if region may have changed. */
12641 if ((!NILP (Vtransient_mark_mode)
12642 && !NILP (current_buffer->mark_active))
12643 || !NILP (w->region_showing)
12644 || !NILP (Vshow_trailing_whitespace))
12645 return 0;
12646
12647 /* If top-line visibility has changed, give up. */
12648 if (WINDOW_WANTS_HEADER_LINE_P (w)
12649 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12650 return 0;
12651
12652 /* Give up if old or new display is scrolled vertically. We could
12653 make this function handle this, but right now it doesn't. */
12654 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12655 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12656 return 0;
12657
12658 /* The variable new_start now holds the new window start. The old
12659 start `start' can be determined from the current matrix. */
12660 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12661 start = start_row->start.pos;
12662 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12663
12664 /* Clear the desired matrix for the display below. */
12665 clear_glyph_matrix (w->desired_matrix);
12666
12667 if (CHARPOS (new_start) <= CHARPOS (start))
12668 {
12669 int first_row_y;
12670
12671 /* Don't use this method if the display starts with an ellipsis
12672 displayed for invisible text. It's not easy to handle that case
12673 below, and it's certainly not worth the effort since this is
12674 not a frequent case. */
12675 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12676 return 0;
12677
12678 IF_DEBUG (debug_method_add (w, "twu1"));
12679
12680 /* Display up to a row that can be reused. The variable
12681 last_text_row is set to the last row displayed that displays
12682 text. Note that it.vpos == 0 if or if not there is a
12683 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12684 start_display (&it, w, new_start);
12685 first_row_y = it.current_y;
12686 w->cursor.vpos = -1;
12687 last_text_row = last_reused_text_row = NULL;
12688
12689 while (it.current_y < it.last_visible_y
12690 && IT_CHARPOS (it) < CHARPOS (start)
12691 && !fonts_changed_p)
12692 if (display_line (&it))
12693 last_text_row = it.glyph_row - 1;
12694
12695 /* A value of current_y < last_visible_y means that we stopped
12696 at the previous window start, which in turn means that we
12697 have at least one reusable row. */
12698 if (it.current_y < it.last_visible_y)
12699 {
12700 /* IT.vpos always starts from 0; it counts text lines. */
12701 nrows_scrolled = it.vpos;
12702
12703 /* Find PT if not already found in the lines displayed. */
12704 if (w->cursor.vpos < 0)
12705 {
12706 int dy = it.current_y - first_row_y;
12707
12708 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12709 row = row_containing_pos (w, PT, row, NULL, dy);
12710 if (row)
12711 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12712 dy, nrows_scrolled);
12713 else
12714 {
12715 clear_glyph_matrix (w->desired_matrix);
12716 return 0;
12717 }
12718 }
12719
12720 /* Scroll the display. Do it before the current matrix is
12721 changed. The problem here is that update has not yet
12722 run, i.e. part of the current matrix is not up to date.
12723 scroll_run_hook will clear the cursor, and use the
12724 current matrix to get the height of the row the cursor is
12725 in. */
12726 run.current_y = first_row_y;
12727 run.desired_y = it.current_y;
12728 run.height = it.last_visible_y - it.current_y;
12729
12730 if (run.height > 0 && run.current_y != run.desired_y)
12731 {
12732 update_begin (f);
12733 FRAME_RIF (f)->update_window_begin_hook (w);
12734 FRAME_RIF (f)->clear_window_mouse_face (w);
12735 FRAME_RIF (f)->scroll_run_hook (w, &run);
12736 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12737 update_end (f);
12738 }
12739
12740 /* Shift current matrix down by nrows_scrolled lines. */
12741 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12742 rotate_matrix (w->current_matrix,
12743 start_vpos,
12744 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12745 nrows_scrolled);
12746
12747 /* Disable lines that must be updated. */
12748 for (i = 0; i < it.vpos; ++i)
12749 (start_row + i)->enabled_p = 0;
12750
12751 /* Re-compute Y positions. */
12752 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12753 max_y = it.last_visible_y;
12754 for (row = start_row + nrows_scrolled;
12755 row < bottom_row;
12756 ++row)
12757 {
12758 row->y = it.current_y;
12759 row->visible_height = row->height;
12760
12761 if (row->y < min_y)
12762 row->visible_height -= min_y - row->y;
12763 if (row->y + row->height > max_y)
12764 row->visible_height -= row->y + row->height - max_y;
12765 row->redraw_fringe_bitmaps_p = 1;
12766
12767 it.current_y += row->height;
12768
12769 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12770 last_reused_text_row = row;
12771 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12772 break;
12773 }
12774
12775 /* Disable lines in the current matrix which are now
12776 below the window. */
12777 for (++row; row < bottom_row; ++row)
12778 row->enabled_p = 0;
12779 }
12780
12781 /* Update window_end_pos etc.; last_reused_text_row is the last
12782 reused row from the current matrix containing text, if any.
12783 The value of last_text_row is the last displayed line
12784 containing text. */
12785 if (last_reused_text_row)
12786 {
12787 w->window_end_bytepos
12788 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12789 w->window_end_pos
12790 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12791 w->window_end_vpos
12792 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12793 w->current_matrix));
12794 }
12795 else if (last_text_row)
12796 {
12797 w->window_end_bytepos
12798 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12799 w->window_end_pos
12800 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12801 w->window_end_vpos
12802 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12803 }
12804 else
12805 {
12806 /* This window must be completely empty. */
12807 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12808 w->window_end_pos = make_number (Z - ZV);
12809 w->window_end_vpos = make_number (0);
12810 }
12811 w->window_end_valid = Qnil;
12812
12813 /* Update hint: don't try scrolling again in update_window. */
12814 w->desired_matrix->no_scrolling_p = 1;
12815
12816 #if GLYPH_DEBUG
12817 debug_method_add (w, "try_window_reusing_current_matrix 1");
12818 #endif
12819 return 1;
12820 }
12821 else if (CHARPOS (new_start) > CHARPOS (start))
12822 {
12823 struct glyph_row *pt_row, *row;
12824 struct glyph_row *first_reusable_row;
12825 struct glyph_row *first_row_to_display;
12826 int dy;
12827 int yb = window_text_bottom_y (w);
12828
12829 /* Find the row starting at new_start, if there is one. Don't
12830 reuse a partially visible line at the end. */
12831 first_reusable_row = start_row;
12832 while (first_reusable_row->enabled_p
12833 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12834 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12835 < CHARPOS (new_start)))
12836 ++first_reusable_row;
12837
12838 /* Give up if there is no row to reuse. */
12839 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12840 || !first_reusable_row->enabled_p
12841 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12842 != CHARPOS (new_start)))
12843 return 0;
12844
12845 /* We can reuse fully visible rows beginning with
12846 first_reusable_row to the end of the window. Set
12847 first_row_to_display to the first row that cannot be reused.
12848 Set pt_row to the row containing point, if there is any. */
12849 pt_row = NULL;
12850 for (first_row_to_display = first_reusable_row;
12851 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12852 ++first_row_to_display)
12853 {
12854 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12855 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12856 pt_row = first_row_to_display;
12857 }
12858
12859 /* Start displaying at the start of first_row_to_display. */
12860 xassert (first_row_to_display->y < yb);
12861 init_to_row_start (&it, w, first_row_to_display);
12862
12863 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12864 - start_vpos);
12865 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12866 - nrows_scrolled);
12867 it.current_y = (first_row_to_display->y - first_reusable_row->y
12868 + WINDOW_HEADER_LINE_HEIGHT (w));
12869
12870 /* Display lines beginning with first_row_to_display in the
12871 desired matrix. Set last_text_row to the last row displayed
12872 that displays text. */
12873 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12874 if (pt_row == NULL)
12875 w->cursor.vpos = -1;
12876 last_text_row = NULL;
12877 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12878 if (display_line (&it))
12879 last_text_row = it.glyph_row - 1;
12880
12881 /* Give up If point isn't in a row displayed or reused. */
12882 if (w->cursor.vpos < 0)
12883 {
12884 clear_glyph_matrix (w->desired_matrix);
12885 return 0;
12886 }
12887
12888 /* If point is in a reused row, adjust y and vpos of the cursor
12889 position. */
12890 if (pt_row)
12891 {
12892 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12893 w->current_matrix);
12894 w->cursor.y -= first_reusable_row->y;
12895 }
12896
12897 /* Scroll the display. */
12898 run.current_y = first_reusable_row->y;
12899 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12900 run.height = it.last_visible_y - run.current_y;
12901 dy = run.current_y - run.desired_y;
12902
12903 if (run.height)
12904 {
12905 update_begin (f);
12906 FRAME_RIF (f)->update_window_begin_hook (w);
12907 FRAME_RIF (f)->clear_window_mouse_face (w);
12908 FRAME_RIF (f)->scroll_run_hook (w, &run);
12909 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12910 update_end (f);
12911 }
12912
12913 /* Adjust Y positions of reused rows. */
12914 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12915 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12916 max_y = it.last_visible_y;
12917 for (row = first_reusable_row; row < first_row_to_display; ++row)
12918 {
12919 row->y -= dy;
12920 row->visible_height = row->height;
12921 if (row->y < min_y)
12922 row->visible_height -= min_y - row->y;
12923 if (row->y + row->height > max_y)
12924 row->visible_height -= row->y + row->height - max_y;
12925 row->redraw_fringe_bitmaps_p = 1;
12926 }
12927
12928 /* Scroll the current matrix. */
12929 xassert (nrows_scrolled > 0);
12930 rotate_matrix (w->current_matrix,
12931 start_vpos,
12932 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12933 -nrows_scrolled);
12934
12935 /* Disable rows not reused. */
12936 for (row -= nrows_scrolled; row < bottom_row; ++row)
12937 row->enabled_p = 0;
12938
12939 /* Adjust window end. A null value of last_text_row means that
12940 the window end is in reused rows which in turn means that
12941 only its vpos can have changed. */
12942 if (last_text_row)
12943 {
12944 w->window_end_bytepos
12945 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12946 w->window_end_pos
12947 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12948 w->window_end_vpos
12949 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12950 }
12951 else
12952 {
12953 w->window_end_vpos
12954 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12955 }
12956
12957 w->window_end_valid = Qnil;
12958 w->desired_matrix->no_scrolling_p = 1;
12959
12960 #if GLYPH_DEBUG
12961 debug_method_add (w, "try_window_reusing_current_matrix 2");
12962 #endif
12963 return 1;
12964 }
12965
12966 return 0;
12967 }
12968
12969
12970 \f
12971 /************************************************************************
12972 Window redisplay reusing current matrix when buffer has changed
12973 ************************************************************************/
12974
12975 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12976 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12977 int *, int *));
12978 static struct glyph_row *
12979 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12980 struct glyph_row *));
12981
12982
12983 /* Return the last row in MATRIX displaying text. If row START is
12984 non-null, start searching with that row. IT gives the dimensions
12985 of the display. Value is null if matrix is empty; otherwise it is
12986 a pointer to the row found. */
12987
12988 static struct glyph_row *
12989 find_last_row_displaying_text (matrix, it, start)
12990 struct glyph_matrix *matrix;
12991 struct it *it;
12992 struct glyph_row *start;
12993 {
12994 struct glyph_row *row, *row_found;
12995
12996 /* Set row_found to the last row in IT->w's current matrix
12997 displaying text. The loop looks funny but think of partially
12998 visible lines. */
12999 row_found = NULL;
13000 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13001 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13002 {
13003 xassert (row->enabled_p);
13004 row_found = row;
13005 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13006 break;
13007 ++row;
13008 }
13009
13010 return row_found;
13011 }
13012
13013
13014 /* Return the last row in the current matrix of W that is not affected
13015 by changes at the start of current_buffer that occurred since W's
13016 current matrix was built. Value is null if no such row exists.
13017
13018 BEG_UNCHANGED us the number of characters unchanged at the start of
13019 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13020 first changed character in current_buffer. Characters at positions <
13021 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13022 when the current matrix was built. */
13023
13024 static struct glyph_row *
13025 find_last_unchanged_at_beg_row (w)
13026 struct window *w;
13027 {
13028 int first_changed_pos = BEG + BEG_UNCHANGED;
13029 struct glyph_row *row;
13030 struct glyph_row *row_found = NULL;
13031 int yb = window_text_bottom_y (w);
13032
13033 /* Find the last row displaying unchanged text. */
13034 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13035 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13036 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13037 {
13038 if (/* If row ends before first_changed_pos, it is unchanged,
13039 except in some case. */
13040 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13041 /* When row ends in ZV and we write at ZV it is not
13042 unchanged. */
13043 && !row->ends_at_zv_p
13044 /* When first_changed_pos is the end of a continued line,
13045 row is not unchanged because it may be no longer
13046 continued. */
13047 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13048 && (row->continued_p
13049 || row->exact_window_width_line_p)))
13050 row_found = row;
13051
13052 /* Stop if last visible row. */
13053 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13054 break;
13055
13056 ++row;
13057 }
13058
13059 return row_found;
13060 }
13061
13062
13063 /* Find the first glyph row in the current matrix of W that is not
13064 affected by changes at the end of current_buffer since the
13065 time W's current matrix was built.
13066
13067 Return in *DELTA the number of chars by which buffer positions in
13068 unchanged text at the end of current_buffer must be adjusted.
13069
13070 Return in *DELTA_BYTES the corresponding number of bytes.
13071
13072 Value is null if no such row exists, i.e. all rows are affected by
13073 changes. */
13074
13075 static struct glyph_row *
13076 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13077 struct window *w;
13078 int *delta, *delta_bytes;
13079 {
13080 struct glyph_row *row;
13081 struct glyph_row *row_found = NULL;
13082
13083 *delta = *delta_bytes = 0;
13084
13085 /* Display must not have been paused, otherwise the current matrix
13086 is not up to date. */
13087 if (NILP (w->window_end_valid))
13088 abort ();
13089
13090 /* A value of window_end_pos >= END_UNCHANGED means that the window
13091 end is in the range of changed text. If so, there is no
13092 unchanged row at the end of W's current matrix. */
13093 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13094 return NULL;
13095
13096 /* Set row to the last row in W's current matrix displaying text. */
13097 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13098
13099 /* If matrix is entirely empty, no unchanged row exists. */
13100 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13101 {
13102 /* The value of row is the last glyph row in the matrix having a
13103 meaningful buffer position in it. The end position of row
13104 corresponds to window_end_pos. This allows us to translate
13105 buffer positions in the current matrix to current buffer
13106 positions for characters not in changed text. */
13107 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13108 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13109 int last_unchanged_pos, last_unchanged_pos_old;
13110 struct glyph_row *first_text_row
13111 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13112
13113 *delta = Z - Z_old;
13114 *delta_bytes = Z_BYTE - Z_BYTE_old;
13115
13116 /* Set last_unchanged_pos to the buffer position of the last
13117 character in the buffer that has not been changed. Z is the
13118 index + 1 of the last character in current_buffer, i.e. by
13119 subtracting END_UNCHANGED we get the index of the last
13120 unchanged character, and we have to add BEG to get its buffer
13121 position. */
13122 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13123 last_unchanged_pos_old = last_unchanged_pos - *delta;
13124
13125 /* Search backward from ROW for a row displaying a line that
13126 starts at a minimum position >= last_unchanged_pos_old. */
13127 for (; row > first_text_row; --row)
13128 {
13129 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13130 abort ();
13131
13132 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13133 row_found = row;
13134 }
13135 }
13136
13137 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13138 abort ();
13139
13140 return row_found;
13141 }
13142
13143
13144 /* Make sure that glyph rows in the current matrix of window W
13145 reference the same glyph memory as corresponding rows in the
13146 frame's frame matrix. This function is called after scrolling W's
13147 current matrix on a terminal frame in try_window_id and
13148 try_window_reusing_current_matrix. */
13149
13150 static void
13151 sync_frame_with_window_matrix_rows (w)
13152 struct window *w;
13153 {
13154 struct frame *f = XFRAME (w->frame);
13155 struct glyph_row *window_row, *window_row_end, *frame_row;
13156
13157 /* Preconditions: W must be a leaf window and full-width. Its frame
13158 must have a frame matrix. */
13159 xassert (NILP (w->hchild) && NILP (w->vchild));
13160 xassert (WINDOW_FULL_WIDTH_P (w));
13161 xassert (!FRAME_WINDOW_P (f));
13162
13163 /* If W is a full-width window, glyph pointers in W's current matrix
13164 have, by definition, to be the same as glyph pointers in the
13165 corresponding frame matrix. Note that frame matrices have no
13166 marginal areas (see build_frame_matrix). */
13167 window_row = w->current_matrix->rows;
13168 window_row_end = window_row + w->current_matrix->nrows;
13169 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13170 while (window_row < window_row_end)
13171 {
13172 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13173 struct glyph *end = window_row->glyphs[LAST_AREA];
13174
13175 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13176 frame_row->glyphs[TEXT_AREA] = start;
13177 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13178 frame_row->glyphs[LAST_AREA] = end;
13179
13180 /* Disable frame rows whose corresponding window rows have
13181 been disabled in try_window_id. */
13182 if (!window_row->enabled_p)
13183 frame_row->enabled_p = 0;
13184
13185 ++window_row, ++frame_row;
13186 }
13187 }
13188
13189
13190 /* Find the glyph row in window W containing CHARPOS. Consider all
13191 rows between START and END (not inclusive). END null means search
13192 all rows to the end of the display area of W. Value is the row
13193 containing CHARPOS or null. */
13194
13195 struct glyph_row *
13196 row_containing_pos (w, charpos, start, end, dy)
13197 struct window *w;
13198 int charpos;
13199 struct glyph_row *start, *end;
13200 int dy;
13201 {
13202 struct glyph_row *row = start;
13203 int last_y;
13204
13205 /* If we happen to start on a header-line, skip that. */
13206 if (row->mode_line_p)
13207 ++row;
13208
13209 if ((end && row >= end) || !row->enabled_p)
13210 return NULL;
13211
13212 last_y = window_text_bottom_y (w) - dy;
13213
13214 while (1)
13215 {
13216 /* Give up if we have gone too far. */
13217 if (end && row >= end)
13218 return NULL;
13219 /* This formerly returned if they were equal.
13220 I think that both quantities are of a "last plus one" type;
13221 if so, when they are equal, the row is within the screen. -- rms. */
13222 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13223 return NULL;
13224
13225 /* If it is in this row, return this row. */
13226 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13227 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13228 /* The end position of a row equals the start
13229 position of the next row. If CHARPOS is there, we
13230 would rather display it in the next line, except
13231 when this line ends in ZV. */
13232 && !row->ends_at_zv_p
13233 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13234 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13235 return row;
13236 ++row;
13237 }
13238 }
13239
13240
13241 /* Try to redisplay window W by reusing its existing display. W's
13242 current matrix must be up to date when this function is called,
13243 i.e. window_end_valid must not be nil.
13244
13245 Value is
13246
13247 1 if display has been updated
13248 0 if otherwise unsuccessful
13249 -1 if redisplay with same window start is known not to succeed
13250
13251 The following steps are performed:
13252
13253 1. Find the last row in the current matrix of W that is not
13254 affected by changes at the start of current_buffer. If no such row
13255 is found, give up.
13256
13257 2. Find the first row in W's current matrix that is not affected by
13258 changes at the end of current_buffer. Maybe there is no such row.
13259
13260 3. Display lines beginning with the row + 1 found in step 1 to the
13261 row found in step 2 or, if step 2 didn't find a row, to the end of
13262 the window.
13263
13264 4. If cursor is not known to appear on the window, give up.
13265
13266 5. If display stopped at the row found in step 2, scroll the
13267 display and current matrix as needed.
13268
13269 6. Maybe display some lines at the end of W, if we must. This can
13270 happen under various circumstances, like a partially visible line
13271 becoming fully visible, or because newly displayed lines are displayed
13272 in smaller font sizes.
13273
13274 7. Update W's window end information. */
13275
13276 static int
13277 try_window_id (w)
13278 struct window *w;
13279 {
13280 struct frame *f = XFRAME (w->frame);
13281 struct glyph_matrix *current_matrix = w->current_matrix;
13282 struct glyph_matrix *desired_matrix = w->desired_matrix;
13283 struct glyph_row *last_unchanged_at_beg_row;
13284 struct glyph_row *first_unchanged_at_end_row;
13285 struct glyph_row *row;
13286 struct glyph_row *bottom_row;
13287 int bottom_vpos;
13288 struct it it;
13289 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13290 struct text_pos start_pos;
13291 struct run run;
13292 int first_unchanged_at_end_vpos = 0;
13293 struct glyph_row *last_text_row, *last_text_row_at_end;
13294 struct text_pos start;
13295 int first_changed_charpos, last_changed_charpos;
13296
13297 #if GLYPH_DEBUG
13298 if (inhibit_try_window_id)
13299 return 0;
13300 #endif
13301
13302 /* This is handy for debugging. */
13303 #if 0
13304 #define GIVE_UP(X) \
13305 do { \
13306 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13307 return 0; \
13308 } while (0)
13309 #else
13310 #define GIVE_UP(X) return 0
13311 #endif
13312
13313 SET_TEXT_POS_FROM_MARKER (start, w->start);
13314
13315 /* Don't use this for mini-windows because these can show
13316 messages and mini-buffers, and we don't handle that here. */
13317 if (MINI_WINDOW_P (w))
13318 GIVE_UP (1);
13319
13320 /* This flag is used to prevent redisplay optimizations. */
13321 if (windows_or_buffers_changed || cursor_type_changed)
13322 GIVE_UP (2);
13323
13324 /* Verify that narrowing has not changed.
13325 Also verify that we were not told to prevent redisplay optimizations.
13326 It would be nice to further
13327 reduce the number of cases where this prevents try_window_id. */
13328 if (current_buffer->clip_changed
13329 || current_buffer->prevent_redisplay_optimizations_p)
13330 GIVE_UP (3);
13331
13332 /* Window must either use window-based redisplay or be full width. */
13333 if (!FRAME_WINDOW_P (f)
13334 && (!FRAME_LINE_INS_DEL_OK (f)
13335 || !WINDOW_FULL_WIDTH_P (w)))
13336 GIVE_UP (4);
13337
13338 /* Give up if point is not known NOT to appear in W. */
13339 if (PT < CHARPOS (start))
13340 GIVE_UP (5);
13341
13342 /* Another way to prevent redisplay optimizations. */
13343 if (XFASTINT (w->last_modified) == 0)
13344 GIVE_UP (6);
13345
13346 /* Verify that window is not hscrolled. */
13347 if (XFASTINT (w->hscroll) != 0)
13348 GIVE_UP (7);
13349
13350 /* Verify that display wasn't paused. */
13351 if (NILP (w->window_end_valid))
13352 GIVE_UP (8);
13353
13354 /* Can't use this if highlighting a region because a cursor movement
13355 will do more than just set the cursor. */
13356 if (!NILP (Vtransient_mark_mode)
13357 && !NILP (current_buffer->mark_active))
13358 GIVE_UP (9);
13359
13360 /* Likewise if highlighting trailing whitespace. */
13361 if (!NILP (Vshow_trailing_whitespace))
13362 GIVE_UP (11);
13363
13364 /* Likewise if showing a region. */
13365 if (!NILP (w->region_showing))
13366 GIVE_UP (10);
13367
13368 /* Can use this if overlay arrow position and or string have changed. */
13369 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
13370 || !EQ (last_arrow_string, Voverlay_arrow_string))
13371 GIVE_UP (12);
13372
13373
13374 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13375 only if buffer has really changed. The reason is that the gap is
13376 initially at Z for freshly visited files. The code below would
13377 set end_unchanged to 0 in that case. */
13378 if (MODIFF > SAVE_MODIFF
13379 /* This seems to happen sometimes after saving a buffer. */
13380 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13381 {
13382 if (GPT - BEG < BEG_UNCHANGED)
13383 BEG_UNCHANGED = GPT - BEG;
13384 if (Z - GPT < END_UNCHANGED)
13385 END_UNCHANGED = Z - GPT;
13386 }
13387
13388 /* The position of the first and last character that has been changed. */
13389 first_changed_charpos = BEG + BEG_UNCHANGED;
13390 last_changed_charpos = Z - END_UNCHANGED;
13391
13392 /* If window starts after a line end, and the last change is in
13393 front of that newline, then changes don't affect the display.
13394 This case happens with stealth-fontification. Note that although
13395 the display is unchanged, glyph positions in the matrix have to
13396 be adjusted, of course. */
13397 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13398 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13399 && ((last_changed_charpos < CHARPOS (start)
13400 && CHARPOS (start) == BEGV)
13401 || (last_changed_charpos < CHARPOS (start) - 1
13402 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13403 {
13404 int Z_old, delta, Z_BYTE_old, delta_bytes;
13405 struct glyph_row *r0;
13406
13407 /* Compute how many chars/bytes have been added to or removed
13408 from the buffer. */
13409 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13410 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13411 delta = Z - Z_old;
13412 delta_bytes = Z_BYTE - Z_BYTE_old;
13413
13414 /* Give up if PT is not in the window. Note that it already has
13415 been checked at the start of try_window_id that PT is not in
13416 front of the window start. */
13417 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13418 GIVE_UP (13);
13419
13420 /* If window start is unchanged, we can reuse the whole matrix
13421 as is, after adjusting glyph positions. No need to compute
13422 the window end again, since its offset from Z hasn't changed. */
13423 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13424 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13425 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13426 /* PT must not be in a partially visible line. */
13427 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13428 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13429 {
13430 /* Adjust positions in the glyph matrix. */
13431 if (delta || delta_bytes)
13432 {
13433 struct glyph_row *r1
13434 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13435 increment_matrix_positions (w->current_matrix,
13436 MATRIX_ROW_VPOS (r0, current_matrix),
13437 MATRIX_ROW_VPOS (r1, current_matrix),
13438 delta, delta_bytes);
13439 }
13440
13441 /* Set the cursor. */
13442 row = row_containing_pos (w, PT, r0, NULL, 0);
13443 if (row)
13444 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13445 else
13446 abort ();
13447 return 1;
13448 }
13449 }
13450
13451 /* Handle the case that changes are all below what is displayed in
13452 the window, and that PT is in the window. This shortcut cannot
13453 be taken if ZV is visible in the window, and text has been added
13454 there that is visible in the window. */
13455 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13456 /* ZV is not visible in the window, or there are no
13457 changes at ZV, actually. */
13458 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13459 || first_changed_charpos == last_changed_charpos))
13460 {
13461 struct glyph_row *r0;
13462
13463 /* Give up if PT is not in the window. Note that it already has
13464 been checked at the start of try_window_id that PT is not in
13465 front of the window start. */
13466 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13467 GIVE_UP (14);
13468
13469 /* If window start is unchanged, we can reuse the whole matrix
13470 as is, without changing glyph positions since no text has
13471 been added/removed in front of the window end. */
13472 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13473 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13474 /* PT must not be in a partially visible line. */
13475 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13476 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13477 {
13478 /* We have to compute the window end anew since text
13479 can have been added/removed after it. */
13480 w->window_end_pos
13481 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13482 w->window_end_bytepos
13483 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13484
13485 /* Set the cursor. */
13486 row = row_containing_pos (w, PT, r0, NULL, 0);
13487 if (row)
13488 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13489 else
13490 abort ();
13491 return 2;
13492 }
13493 }
13494
13495 /* Give up if window start is in the changed area.
13496
13497 The condition used to read
13498
13499 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13500
13501 but why that was tested escapes me at the moment. */
13502 if (CHARPOS (start) >= first_changed_charpos
13503 && CHARPOS (start) <= last_changed_charpos)
13504 GIVE_UP (15);
13505
13506 /* Check that window start agrees with the start of the first glyph
13507 row in its current matrix. Check this after we know the window
13508 start is not in changed text, otherwise positions would not be
13509 comparable. */
13510 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13511 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13512 GIVE_UP (16);
13513
13514 /* Give up if the window ends in strings. Overlay strings
13515 at the end are difficult to handle, so don't try. */
13516 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13517 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13518 GIVE_UP (20);
13519
13520 /* Compute the position at which we have to start displaying new
13521 lines. Some of the lines at the top of the window might be
13522 reusable because they are not displaying changed text. Find the
13523 last row in W's current matrix not affected by changes at the
13524 start of current_buffer. Value is null if changes start in the
13525 first line of window. */
13526 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13527 if (last_unchanged_at_beg_row)
13528 {
13529 /* Avoid starting to display in the moddle of a character, a TAB
13530 for instance. This is easier than to set up the iterator
13531 exactly, and it's not a frequent case, so the additional
13532 effort wouldn't really pay off. */
13533 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13534 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13535 && last_unchanged_at_beg_row > w->current_matrix->rows)
13536 --last_unchanged_at_beg_row;
13537
13538 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13539 GIVE_UP (17);
13540
13541 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13542 GIVE_UP (18);
13543 start_pos = it.current.pos;
13544
13545 /* Start displaying new lines in the desired matrix at the same
13546 vpos we would use in the current matrix, i.e. below
13547 last_unchanged_at_beg_row. */
13548 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13549 current_matrix);
13550 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13551 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13552
13553 xassert (it.hpos == 0 && it.current_x == 0);
13554 }
13555 else
13556 {
13557 /* There are no reusable lines at the start of the window.
13558 Start displaying in the first text line. */
13559 start_display (&it, w, start);
13560 it.vpos = it.first_vpos;
13561 start_pos = it.current.pos;
13562 }
13563
13564 /* Find the first row that is not affected by changes at the end of
13565 the buffer. Value will be null if there is no unchanged row, in
13566 which case we must redisplay to the end of the window. delta
13567 will be set to the value by which buffer positions beginning with
13568 first_unchanged_at_end_row have to be adjusted due to text
13569 changes. */
13570 first_unchanged_at_end_row
13571 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13572 IF_DEBUG (debug_delta = delta);
13573 IF_DEBUG (debug_delta_bytes = delta_bytes);
13574
13575 /* Set stop_pos to the buffer position up to which we will have to
13576 display new lines. If first_unchanged_at_end_row != NULL, this
13577 is the buffer position of the start of the line displayed in that
13578 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13579 that we don't stop at a buffer position. */
13580 stop_pos = 0;
13581 if (first_unchanged_at_end_row)
13582 {
13583 xassert (last_unchanged_at_beg_row == NULL
13584 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13585
13586 /* If this is a continuation line, move forward to the next one
13587 that isn't. Changes in lines above affect this line.
13588 Caution: this may move first_unchanged_at_end_row to a row
13589 not displaying text. */
13590 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13591 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13592 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13593 < it.last_visible_y))
13594 ++first_unchanged_at_end_row;
13595
13596 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13597 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13598 >= it.last_visible_y))
13599 first_unchanged_at_end_row = NULL;
13600 else
13601 {
13602 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13603 + delta);
13604 first_unchanged_at_end_vpos
13605 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13606 xassert (stop_pos >= Z - END_UNCHANGED);
13607 }
13608 }
13609 else if (last_unchanged_at_beg_row == NULL)
13610 GIVE_UP (19);
13611
13612
13613 #if GLYPH_DEBUG
13614
13615 /* Either there is no unchanged row at the end, or the one we have
13616 now displays text. This is a necessary condition for the window
13617 end pos calculation at the end of this function. */
13618 xassert (first_unchanged_at_end_row == NULL
13619 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13620
13621 debug_last_unchanged_at_beg_vpos
13622 = (last_unchanged_at_beg_row
13623 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13624 : -1);
13625 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13626
13627 #endif /* GLYPH_DEBUG != 0 */
13628
13629
13630 /* Display new lines. Set last_text_row to the last new line
13631 displayed which has text on it, i.e. might end up as being the
13632 line where the window_end_vpos is. */
13633 w->cursor.vpos = -1;
13634 last_text_row = NULL;
13635 overlay_arrow_seen = 0;
13636 while (it.current_y < it.last_visible_y
13637 && !fonts_changed_p
13638 && (first_unchanged_at_end_row == NULL
13639 || IT_CHARPOS (it) < stop_pos))
13640 {
13641 if (display_line (&it))
13642 last_text_row = it.glyph_row - 1;
13643 }
13644
13645 if (fonts_changed_p)
13646 return -1;
13647
13648
13649 /* Compute differences in buffer positions, y-positions etc. for
13650 lines reused at the bottom of the window. Compute what we can
13651 scroll. */
13652 if (first_unchanged_at_end_row
13653 /* No lines reused because we displayed everything up to the
13654 bottom of the window. */
13655 && it.current_y < it.last_visible_y)
13656 {
13657 dvpos = (it.vpos
13658 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13659 current_matrix));
13660 dy = it.current_y - first_unchanged_at_end_row->y;
13661 run.current_y = first_unchanged_at_end_row->y;
13662 run.desired_y = run.current_y + dy;
13663 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13664 }
13665 else
13666 {
13667 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13668 first_unchanged_at_end_row = NULL;
13669 }
13670 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13671
13672
13673 /* Find the cursor if not already found. We have to decide whether
13674 PT will appear on this window (it sometimes doesn't, but this is
13675 not a very frequent case.) This decision has to be made before
13676 the current matrix is altered. A value of cursor.vpos < 0 means
13677 that PT is either in one of the lines beginning at
13678 first_unchanged_at_end_row or below the window. Don't care for
13679 lines that might be displayed later at the window end; as
13680 mentioned, this is not a frequent case. */
13681 if (w->cursor.vpos < 0)
13682 {
13683 /* Cursor in unchanged rows at the top? */
13684 if (PT < CHARPOS (start_pos)
13685 && last_unchanged_at_beg_row)
13686 {
13687 row = row_containing_pos (w, PT,
13688 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13689 last_unchanged_at_beg_row + 1, 0);
13690 if (row)
13691 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13692 }
13693
13694 /* Start from first_unchanged_at_end_row looking for PT. */
13695 else if (first_unchanged_at_end_row)
13696 {
13697 row = row_containing_pos (w, PT - delta,
13698 first_unchanged_at_end_row, NULL, 0);
13699 if (row)
13700 set_cursor_from_row (w, row, w->current_matrix, delta,
13701 delta_bytes, dy, dvpos);
13702 }
13703
13704 /* Give up if cursor was not found. */
13705 if (w->cursor.vpos < 0)
13706 {
13707 clear_glyph_matrix (w->desired_matrix);
13708 return -1;
13709 }
13710 }
13711
13712 /* Don't let the cursor end in the scroll margins. */
13713 {
13714 int this_scroll_margin, cursor_height;
13715
13716 this_scroll_margin = max (0, scroll_margin);
13717 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13718 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13719 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13720
13721 if ((w->cursor.y < this_scroll_margin
13722 && CHARPOS (start) > BEGV)
13723 /* Don't take scroll margin into account at the bottom because
13724 old redisplay didn't do it either. */
13725 || w->cursor.y + cursor_height > it.last_visible_y)
13726 {
13727 w->cursor.vpos = -1;
13728 clear_glyph_matrix (w->desired_matrix);
13729 return -1;
13730 }
13731 }
13732
13733 /* Scroll the display. Do it before changing the current matrix so
13734 that xterm.c doesn't get confused about where the cursor glyph is
13735 found. */
13736 if (dy && run.height)
13737 {
13738 update_begin (f);
13739
13740 if (FRAME_WINDOW_P (f))
13741 {
13742 FRAME_RIF (f)->update_window_begin_hook (w);
13743 FRAME_RIF (f)->clear_window_mouse_face (w);
13744 FRAME_RIF (f)->scroll_run_hook (w, &run);
13745 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13746 }
13747 else
13748 {
13749 /* Terminal frame. In this case, dvpos gives the number of
13750 lines to scroll by; dvpos < 0 means scroll up. */
13751 int first_unchanged_at_end_vpos
13752 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13753 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13754 int end = (WINDOW_TOP_EDGE_LINE (w)
13755 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13756 + window_internal_height (w));
13757
13758 /* Perform the operation on the screen. */
13759 if (dvpos > 0)
13760 {
13761 /* Scroll last_unchanged_at_beg_row to the end of the
13762 window down dvpos lines. */
13763 set_terminal_window (end);
13764
13765 /* On dumb terminals delete dvpos lines at the end
13766 before inserting dvpos empty lines. */
13767 if (!FRAME_SCROLL_REGION_OK (f))
13768 ins_del_lines (end - dvpos, -dvpos);
13769
13770 /* Insert dvpos empty lines in front of
13771 last_unchanged_at_beg_row. */
13772 ins_del_lines (from, dvpos);
13773 }
13774 else if (dvpos < 0)
13775 {
13776 /* Scroll up last_unchanged_at_beg_vpos to the end of
13777 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13778 set_terminal_window (end);
13779
13780 /* Delete dvpos lines in front of
13781 last_unchanged_at_beg_vpos. ins_del_lines will set
13782 the cursor to the given vpos and emit |dvpos| delete
13783 line sequences. */
13784 ins_del_lines (from + dvpos, dvpos);
13785
13786 /* On a dumb terminal insert dvpos empty lines at the
13787 end. */
13788 if (!FRAME_SCROLL_REGION_OK (f))
13789 ins_del_lines (end + dvpos, -dvpos);
13790 }
13791
13792 set_terminal_window (0);
13793 }
13794
13795 update_end (f);
13796 }
13797
13798 /* Shift reused rows of the current matrix to the right position.
13799 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13800 text. */
13801 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13802 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13803 if (dvpos < 0)
13804 {
13805 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13806 bottom_vpos, dvpos);
13807 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13808 bottom_vpos, 0);
13809 }
13810 else if (dvpos > 0)
13811 {
13812 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13813 bottom_vpos, dvpos);
13814 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13815 first_unchanged_at_end_vpos + dvpos, 0);
13816 }
13817
13818 /* For frame-based redisplay, make sure that current frame and window
13819 matrix are in sync with respect to glyph memory. */
13820 if (!FRAME_WINDOW_P (f))
13821 sync_frame_with_window_matrix_rows (w);
13822
13823 /* Adjust buffer positions in reused rows. */
13824 if (delta)
13825 increment_matrix_positions (current_matrix,
13826 first_unchanged_at_end_vpos + dvpos,
13827 bottom_vpos, delta, delta_bytes);
13828
13829 /* Adjust Y positions. */
13830 if (dy)
13831 shift_glyph_matrix (w, current_matrix,
13832 first_unchanged_at_end_vpos + dvpos,
13833 bottom_vpos, dy);
13834
13835 if (first_unchanged_at_end_row)
13836 first_unchanged_at_end_row += dvpos;
13837
13838 /* If scrolling up, there may be some lines to display at the end of
13839 the window. */
13840 last_text_row_at_end = NULL;
13841 if (dy < 0)
13842 {
13843 /* Scrolling up can leave for example a partially visible line
13844 at the end of the window to be redisplayed. */
13845 /* Set last_row to the glyph row in the current matrix where the
13846 window end line is found. It has been moved up or down in
13847 the matrix by dvpos. */
13848 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13849 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13850
13851 /* If last_row is the window end line, it should display text. */
13852 xassert (last_row->displays_text_p);
13853
13854 /* If window end line was partially visible before, begin
13855 displaying at that line. Otherwise begin displaying with the
13856 line following it. */
13857 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13858 {
13859 init_to_row_start (&it, w, last_row);
13860 it.vpos = last_vpos;
13861 it.current_y = last_row->y;
13862 }
13863 else
13864 {
13865 init_to_row_end (&it, w, last_row);
13866 it.vpos = 1 + last_vpos;
13867 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13868 ++last_row;
13869 }
13870
13871 /* We may start in a continuation line. If so, we have to
13872 get the right continuation_lines_width and current_x. */
13873 it.continuation_lines_width = last_row->continuation_lines_width;
13874 it.hpos = it.current_x = 0;
13875
13876 /* Display the rest of the lines at the window end. */
13877 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13878 while (it.current_y < it.last_visible_y
13879 && !fonts_changed_p)
13880 {
13881 /* Is it always sure that the display agrees with lines in
13882 the current matrix? I don't think so, so we mark rows
13883 displayed invalid in the current matrix by setting their
13884 enabled_p flag to zero. */
13885 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13886 if (display_line (&it))
13887 last_text_row_at_end = it.glyph_row - 1;
13888 }
13889 }
13890
13891 /* Update window_end_pos and window_end_vpos. */
13892 if (first_unchanged_at_end_row
13893 && first_unchanged_at_end_row->y < it.last_visible_y
13894 && !last_text_row_at_end)
13895 {
13896 /* Window end line if one of the preserved rows from the current
13897 matrix. Set row to the last row displaying text in current
13898 matrix starting at first_unchanged_at_end_row, after
13899 scrolling. */
13900 xassert (first_unchanged_at_end_row->displays_text_p);
13901 row = find_last_row_displaying_text (w->current_matrix, &it,
13902 first_unchanged_at_end_row);
13903 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13904
13905 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13906 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13907 w->window_end_vpos
13908 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13909 xassert (w->window_end_bytepos >= 0);
13910 IF_DEBUG (debug_method_add (w, "A"));
13911 }
13912 else if (last_text_row_at_end)
13913 {
13914 w->window_end_pos
13915 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13916 w->window_end_bytepos
13917 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13918 w->window_end_vpos
13919 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13920 xassert (w->window_end_bytepos >= 0);
13921 IF_DEBUG (debug_method_add (w, "B"));
13922 }
13923 else if (last_text_row)
13924 {
13925 /* We have displayed either to the end of the window or at the
13926 end of the window, i.e. the last row with text is to be found
13927 in the desired matrix. */
13928 w->window_end_pos
13929 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13930 w->window_end_bytepos
13931 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13932 w->window_end_vpos
13933 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13934 xassert (w->window_end_bytepos >= 0);
13935 }
13936 else if (first_unchanged_at_end_row == NULL
13937 && last_text_row == NULL
13938 && last_text_row_at_end == NULL)
13939 {
13940 /* Displayed to end of window, but no line containing text was
13941 displayed. Lines were deleted at the end of the window. */
13942 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13943 int vpos = XFASTINT (w->window_end_vpos);
13944 struct glyph_row *current_row = current_matrix->rows + vpos;
13945 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13946
13947 for (row = NULL;
13948 row == NULL && vpos >= first_vpos;
13949 --vpos, --current_row, --desired_row)
13950 {
13951 if (desired_row->enabled_p)
13952 {
13953 if (desired_row->displays_text_p)
13954 row = desired_row;
13955 }
13956 else if (current_row->displays_text_p)
13957 row = current_row;
13958 }
13959
13960 xassert (row != NULL);
13961 w->window_end_vpos = make_number (vpos + 1);
13962 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13963 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13964 xassert (w->window_end_bytepos >= 0);
13965 IF_DEBUG (debug_method_add (w, "C"));
13966 }
13967 else
13968 abort ();
13969
13970 #if 0 /* This leads to problems, for instance when the cursor is
13971 at ZV, and the cursor line displays no text. */
13972 /* Disable rows below what's displayed in the window. This makes
13973 debugging easier. */
13974 enable_glyph_matrix_rows (current_matrix,
13975 XFASTINT (w->window_end_vpos) + 1,
13976 bottom_vpos, 0);
13977 #endif
13978
13979 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13980 debug_end_vpos = XFASTINT (w->window_end_vpos));
13981
13982 /* Record that display has not been completed. */
13983 w->window_end_valid = Qnil;
13984 w->desired_matrix->no_scrolling_p = 1;
13985 return 3;
13986
13987 #undef GIVE_UP
13988 }
13989
13990
13991 \f
13992 /***********************************************************************
13993 More debugging support
13994 ***********************************************************************/
13995
13996 #if GLYPH_DEBUG
13997
13998 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13999 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14000 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14001
14002
14003 /* Dump the contents of glyph matrix MATRIX on stderr.
14004
14005 GLYPHS 0 means don't show glyph contents.
14006 GLYPHS 1 means show glyphs in short form
14007 GLYPHS > 1 means show glyphs in long form. */
14008
14009 void
14010 dump_glyph_matrix (matrix, glyphs)
14011 struct glyph_matrix *matrix;
14012 int glyphs;
14013 {
14014 int i;
14015 for (i = 0; i < matrix->nrows; ++i)
14016 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14017 }
14018
14019
14020 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14021 the glyph row and area where the glyph comes from. */
14022
14023 void
14024 dump_glyph (row, glyph, area)
14025 struct glyph_row *row;
14026 struct glyph *glyph;
14027 int area;
14028 {
14029 if (glyph->type == CHAR_GLYPH)
14030 {
14031 fprintf (stderr,
14032 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14033 glyph - row->glyphs[TEXT_AREA],
14034 'C',
14035 glyph->charpos,
14036 (BUFFERP (glyph->object)
14037 ? 'B'
14038 : (STRINGP (glyph->object)
14039 ? 'S'
14040 : '-')),
14041 glyph->pixel_width,
14042 glyph->u.ch,
14043 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14044 ? glyph->u.ch
14045 : '.'),
14046 glyph->face_id,
14047 glyph->left_box_line_p,
14048 glyph->right_box_line_p);
14049 }
14050 else if (glyph->type == STRETCH_GLYPH)
14051 {
14052 fprintf (stderr,
14053 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14054 glyph - row->glyphs[TEXT_AREA],
14055 'S',
14056 glyph->charpos,
14057 (BUFFERP (glyph->object)
14058 ? 'B'
14059 : (STRINGP (glyph->object)
14060 ? 'S'
14061 : '-')),
14062 glyph->pixel_width,
14063 0,
14064 '.',
14065 glyph->face_id,
14066 glyph->left_box_line_p,
14067 glyph->right_box_line_p);
14068 }
14069 else if (glyph->type == IMAGE_GLYPH)
14070 {
14071 fprintf (stderr,
14072 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14073 glyph - row->glyphs[TEXT_AREA],
14074 'I',
14075 glyph->charpos,
14076 (BUFFERP (glyph->object)
14077 ? 'B'
14078 : (STRINGP (glyph->object)
14079 ? 'S'
14080 : '-')),
14081 glyph->pixel_width,
14082 glyph->u.img_id,
14083 '.',
14084 glyph->face_id,
14085 glyph->left_box_line_p,
14086 glyph->right_box_line_p);
14087 }
14088 }
14089
14090
14091 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14092 GLYPHS 0 means don't show glyph contents.
14093 GLYPHS 1 means show glyphs in short form
14094 GLYPHS > 1 means show glyphs in long form. */
14095
14096 void
14097 dump_glyph_row (row, vpos, glyphs)
14098 struct glyph_row *row;
14099 int vpos, glyphs;
14100 {
14101 if (glyphs != 1)
14102 {
14103 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
14104 fprintf (stderr, "=======================================================================\n");
14105
14106 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
14107 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14108 vpos,
14109 MATRIX_ROW_START_CHARPOS (row),
14110 MATRIX_ROW_END_CHARPOS (row),
14111 row->used[TEXT_AREA],
14112 row->contains_overlapping_glyphs_p,
14113 row->enabled_p,
14114 row->truncated_on_left_p,
14115 row->truncated_on_right_p,
14116 row->overlay_arrow_p,
14117 row->continued_p,
14118 MATRIX_ROW_CONTINUATION_LINE_P (row),
14119 row->displays_text_p,
14120 row->ends_at_zv_p,
14121 row->fill_line_p,
14122 row->ends_in_middle_of_char_p,
14123 row->starts_in_middle_of_char_p,
14124 row->mouse_face_p,
14125 row->x,
14126 row->y,
14127 row->pixel_width,
14128 row->height,
14129 row->visible_height,
14130 row->ascent,
14131 row->phys_ascent);
14132 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14133 row->end.overlay_string_index,
14134 row->continuation_lines_width);
14135 fprintf (stderr, "%9d %5d\n",
14136 CHARPOS (row->start.string_pos),
14137 CHARPOS (row->end.string_pos));
14138 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14139 row->end.dpvec_index);
14140 }
14141
14142 if (glyphs > 1)
14143 {
14144 int area;
14145
14146 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14147 {
14148 struct glyph *glyph = row->glyphs[area];
14149 struct glyph *glyph_end = glyph + row->used[area];
14150
14151 /* Glyph for a line end in text. */
14152 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14153 ++glyph_end;
14154
14155 if (glyph < glyph_end)
14156 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14157
14158 for (; glyph < glyph_end; ++glyph)
14159 dump_glyph (row, glyph, area);
14160 }
14161 }
14162 else if (glyphs == 1)
14163 {
14164 int area;
14165
14166 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14167 {
14168 char *s = (char *) alloca (row->used[area] + 1);
14169 int i;
14170
14171 for (i = 0; i < row->used[area]; ++i)
14172 {
14173 struct glyph *glyph = row->glyphs[area] + i;
14174 if (glyph->type == CHAR_GLYPH
14175 && glyph->u.ch < 0x80
14176 && glyph->u.ch >= ' ')
14177 s[i] = glyph->u.ch;
14178 else
14179 s[i] = '.';
14180 }
14181
14182 s[i] = '\0';
14183 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14184 }
14185 }
14186 }
14187
14188
14189 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14190 Sdump_glyph_matrix, 0, 1, "p",
14191 doc: /* Dump the current matrix of the selected window to stderr.
14192 Shows contents of glyph row structures. With non-nil
14193 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14194 glyphs in short form, otherwise show glyphs in long form. */)
14195 (glyphs)
14196 Lisp_Object glyphs;
14197 {
14198 struct window *w = XWINDOW (selected_window);
14199 struct buffer *buffer = XBUFFER (w->buffer);
14200
14201 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14202 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14203 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14204 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14205 fprintf (stderr, "=============================================\n");
14206 dump_glyph_matrix (w->current_matrix,
14207 NILP (glyphs) ? 0 : XINT (glyphs));
14208 return Qnil;
14209 }
14210
14211
14212 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14213 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14214 ()
14215 {
14216 struct frame *f = XFRAME (selected_frame);
14217 dump_glyph_matrix (f->current_matrix, 1);
14218 return Qnil;
14219 }
14220
14221
14222 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14223 doc: /* Dump glyph row ROW to stderr.
14224 GLYPH 0 means don't dump glyphs.
14225 GLYPH 1 means dump glyphs in short form.
14226 GLYPH > 1 or omitted means dump glyphs in long form. */)
14227 (row, glyphs)
14228 Lisp_Object row, glyphs;
14229 {
14230 struct glyph_matrix *matrix;
14231 int vpos;
14232
14233 CHECK_NUMBER (row);
14234 matrix = XWINDOW (selected_window)->current_matrix;
14235 vpos = XINT (row);
14236 if (vpos >= 0 && vpos < matrix->nrows)
14237 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14238 vpos,
14239 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14240 return Qnil;
14241 }
14242
14243
14244 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14245 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14246 GLYPH 0 means don't dump glyphs.
14247 GLYPH 1 means dump glyphs in short form.
14248 GLYPH > 1 or omitted means dump glyphs in long form. */)
14249 (row, glyphs)
14250 Lisp_Object row, glyphs;
14251 {
14252 struct frame *sf = SELECTED_FRAME ();
14253 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14254 int vpos;
14255
14256 CHECK_NUMBER (row);
14257 vpos = XINT (row);
14258 if (vpos >= 0 && vpos < m->nrows)
14259 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14260 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14261 return Qnil;
14262 }
14263
14264
14265 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14266 doc: /* Toggle tracing of redisplay.
14267 With ARG, turn tracing on if and only if ARG is positive. */)
14268 (arg)
14269 Lisp_Object arg;
14270 {
14271 if (NILP (arg))
14272 trace_redisplay_p = !trace_redisplay_p;
14273 else
14274 {
14275 arg = Fprefix_numeric_value (arg);
14276 trace_redisplay_p = XINT (arg) > 0;
14277 }
14278
14279 return Qnil;
14280 }
14281
14282
14283 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14284 doc: /* Like `format', but print result to stderr.
14285 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14286 (nargs, args)
14287 int nargs;
14288 Lisp_Object *args;
14289 {
14290 Lisp_Object s = Fformat (nargs, args);
14291 fprintf (stderr, "%s", SDATA (s));
14292 return Qnil;
14293 }
14294
14295 #endif /* GLYPH_DEBUG */
14296
14297
14298 \f
14299 /***********************************************************************
14300 Building Desired Matrix Rows
14301 ***********************************************************************/
14302
14303 /* Return a temporary glyph row holding the glyphs of an overlay
14304 arrow. Only used for non-window-redisplay windows. */
14305
14306 static struct glyph_row *
14307 get_overlay_arrow_glyph_row (w)
14308 struct window *w;
14309 {
14310 struct frame *f = XFRAME (WINDOW_FRAME (w));
14311 struct buffer *buffer = XBUFFER (w->buffer);
14312 struct buffer *old = current_buffer;
14313 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
14314 int arrow_len = SCHARS (Voverlay_arrow_string);
14315 const unsigned char *arrow_end = arrow_string + arrow_len;
14316 const unsigned char *p;
14317 struct it it;
14318 int multibyte_p;
14319 int n_glyphs_before;
14320
14321 set_buffer_temp (buffer);
14322 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14323 it.glyph_row->used[TEXT_AREA] = 0;
14324 SET_TEXT_POS (it.position, 0, 0);
14325
14326 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14327 p = arrow_string;
14328 while (p < arrow_end)
14329 {
14330 Lisp_Object face, ilisp;
14331
14332 /* Get the next character. */
14333 if (multibyte_p)
14334 it.c = string_char_and_length (p, arrow_len, &it.len);
14335 else
14336 it.c = *p, it.len = 1;
14337 p += it.len;
14338
14339 /* Get its face. */
14340 ilisp = make_number (p - arrow_string);
14341 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
14342 it.face_id = compute_char_face (f, it.c, face);
14343
14344 /* Compute its width, get its glyphs. */
14345 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14346 SET_TEXT_POS (it.position, -1, -1);
14347 PRODUCE_GLYPHS (&it);
14348
14349 /* If this character doesn't fit any more in the line, we have
14350 to remove some glyphs. */
14351 if (it.current_x > it.last_visible_x)
14352 {
14353 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14354 break;
14355 }
14356 }
14357
14358 set_buffer_temp (old);
14359 return it.glyph_row;
14360 }
14361
14362
14363 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14364 glyphs are only inserted for terminal frames since we can't really
14365 win with truncation glyphs when partially visible glyphs are
14366 involved. Which glyphs to insert is determined by
14367 produce_special_glyphs. */
14368
14369 static void
14370 insert_left_trunc_glyphs (it)
14371 struct it *it;
14372 {
14373 struct it truncate_it;
14374 struct glyph *from, *end, *to, *toend;
14375
14376 xassert (!FRAME_WINDOW_P (it->f));
14377
14378 /* Get the truncation glyphs. */
14379 truncate_it = *it;
14380 truncate_it.current_x = 0;
14381 truncate_it.face_id = DEFAULT_FACE_ID;
14382 truncate_it.glyph_row = &scratch_glyph_row;
14383 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14384 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14385 truncate_it.object = make_number (0);
14386 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14387
14388 /* Overwrite glyphs from IT with truncation glyphs. */
14389 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14390 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14391 to = it->glyph_row->glyphs[TEXT_AREA];
14392 toend = to + it->glyph_row->used[TEXT_AREA];
14393
14394 while (from < end)
14395 *to++ = *from++;
14396
14397 /* There may be padding glyphs left over. Overwrite them too. */
14398 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14399 {
14400 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14401 while (from < end)
14402 *to++ = *from++;
14403 }
14404
14405 if (to > toend)
14406 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14407 }
14408
14409
14410 /* Compute the pixel height and width of IT->glyph_row.
14411
14412 Most of the time, ascent and height of a display line will be equal
14413 to the max_ascent and max_height values of the display iterator
14414 structure. This is not the case if
14415
14416 1. We hit ZV without displaying anything. In this case, max_ascent
14417 and max_height will be zero.
14418
14419 2. We have some glyphs that don't contribute to the line height.
14420 (The glyph row flag contributes_to_line_height_p is for future
14421 pixmap extensions).
14422
14423 The first case is easily covered by using default values because in
14424 these cases, the line height does not really matter, except that it
14425 must not be zero. */
14426
14427 static void
14428 compute_line_metrics (it)
14429 struct it *it;
14430 {
14431 struct glyph_row *row = it->glyph_row;
14432 int area, i;
14433
14434 if (FRAME_WINDOW_P (it->f))
14435 {
14436 int i, min_y, max_y;
14437
14438 /* The line may consist of one space only, that was added to
14439 place the cursor on it. If so, the row's height hasn't been
14440 computed yet. */
14441 if (row->height == 0)
14442 {
14443 if (it->max_ascent + it->max_descent == 0)
14444 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14445 row->ascent = it->max_ascent;
14446 row->height = it->max_ascent + it->max_descent;
14447 row->phys_ascent = it->max_phys_ascent;
14448 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14449 }
14450
14451 /* Compute the width of this line. */
14452 row->pixel_width = row->x;
14453 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14454 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14455
14456 xassert (row->pixel_width >= 0);
14457 xassert (row->ascent >= 0 && row->height > 0);
14458
14459 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14460 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14461
14462 /* If first line's physical ascent is larger than its logical
14463 ascent, use the physical ascent, and make the row taller.
14464 This makes accented characters fully visible. */
14465 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14466 && row->phys_ascent > row->ascent)
14467 {
14468 row->height += row->phys_ascent - row->ascent;
14469 row->ascent = row->phys_ascent;
14470 }
14471
14472 /* Compute how much of the line is visible. */
14473 row->visible_height = row->height;
14474
14475 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14476 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14477
14478 if (row->y < min_y)
14479 row->visible_height -= min_y - row->y;
14480 if (row->y + row->height > max_y)
14481 row->visible_height -= row->y + row->height - max_y;
14482 }
14483 else
14484 {
14485 row->pixel_width = row->used[TEXT_AREA];
14486 if (row->continued_p)
14487 row->pixel_width -= it->continuation_pixel_width;
14488 else if (row->truncated_on_right_p)
14489 row->pixel_width -= it->truncation_pixel_width;
14490 row->ascent = row->phys_ascent = 0;
14491 row->height = row->phys_height = row->visible_height = 1;
14492 }
14493
14494 /* Compute a hash code for this row. */
14495 row->hash = 0;
14496 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14497 for (i = 0; i < row->used[area]; ++i)
14498 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14499 + row->glyphs[area][i].u.val
14500 + row->glyphs[area][i].face_id
14501 + row->glyphs[area][i].padding_p
14502 + (row->glyphs[area][i].type << 2));
14503
14504 it->max_ascent = it->max_descent = 0;
14505 it->max_phys_ascent = it->max_phys_descent = 0;
14506 }
14507
14508
14509 /* Append one space to the glyph row of iterator IT if doing a
14510 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14511 space have the default face, otherwise let it have the same face as
14512 IT->face_id. Value is non-zero if a space was added.
14513
14514 This function is called to make sure that there is always one glyph
14515 at the end of a glyph row that the cursor can be set on under
14516 window-systems. (If there weren't such a glyph we would not know
14517 how wide and tall a box cursor should be displayed).
14518
14519 At the same time this space let's a nicely handle clearing to the
14520 end of the line if the row ends in italic text. */
14521
14522 static int
14523 append_space (it, default_face_p)
14524 struct it *it;
14525 int default_face_p;
14526 {
14527 if (FRAME_WINDOW_P (it->f))
14528 {
14529 int n = it->glyph_row->used[TEXT_AREA];
14530
14531 if (it->glyph_row->glyphs[TEXT_AREA] + n
14532 < it->glyph_row->glyphs[1 + TEXT_AREA])
14533 {
14534 /* Save some values that must not be changed.
14535 Must save IT->c and IT->len because otherwise
14536 ITERATOR_AT_END_P wouldn't work anymore after
14537 append_space has been called. */
14538 enum display_element_type saved_what = it->what;
14539 int saved_c = it->c, saved_len = it->len;
14540 int saved_x = it->current_x;
14541 int saved_face_id = it->face_id;
14542 struct text_pos saved_pos;
14543 Lisp_Object saved_object;
14544 struct face *face;
14545
14546 saved_object = it->object;
14547 saved_pos = it->position;
14548
14549 it->what = IT_CHARACTER;
14550 bzero (&it->position, sizeof it->position);
14551 it->object = make_number (0);
14552 it->c = ' ';
14553 it->len = 1;
14554
14555 if (default_face_p)
14556 it->face_id = DEFAULT_FACE_ID;
14557 else if (it->face_before_selective_p)
14558 it->face_id = it->saved_face_id;
14559 face = FACE_FROM_ID (it->f, it->face_id);
14560 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14561
14562 PRODUCE_GLYPHS (it);
14563
14564 it->current_x = saved_x;
14565 it->object = saved_object;
14566 it->position = saved_pos;
14567 it->what = saved_what;
14568 it->face_id = saved_face_id;
14569 it->len = saved_len;
14570 it->c = saved_c;
14571 return 1;
14572 }
14573 }
14574
14575 return 0;
14576 }
14577
14578
14579 /* Extend the face of the last glyph in the text area of IT->glyph_row
14580 to the end of the display line. Called from display_line.
14581 If the glyph row is empty, add a space glyph to it so that we
14582 know the face to draw. Set the glyph row flag fill_line_p. */
14583
14584 static void
14585 extend_face_to_end_of_line (it)
14586 struct it *it;
14587 {
14588 struct face *face;
14589 struct frame *f = it->f;
14590
14591 /* If line is already filled, do nothing. */
14592 if (it->current_x >= it->last_visible_x)
14593 return;
14594
14595 /* Face extension extends the background and box of IT->face_id
14596 to the end of the line. If the background equals the background
14597 of the frame, we don't have to do anything. */
14598 if (it->face_before_selective_p)
14599 face = FACE_FROM_ID (it->f, it->saved_face_id);
14600 else
14601 face = FACE_FROM_ID (f, it->face_id);
14602
14603 if (FRAME_WINDOW_P (f)
14604 && face->box == FACE_NO_BOX
14605 && face->background == FRAME_BACKGROUND_PIXEL (f)
14606 && !face->stipple)
14607 return;
14608
14609 /* Set the glyph row flag indicating that the face of the last glyph
14610 in the text area has to be drawn to the end of the text area. */
14611 it->glyph_row->fill_line_p = 1;
14612
14613 /* If current character of IT is not ASCII, make sure we have the
14614 ASCII face. This will be automatically undone the next time
14615 get_next_display_element returns a multibyte character. Note
14616 that the character will always be single byte in unibyte text. */
14617 if (!SINGLE_BYTE_CHAR_P (it->c))
14618 {
14619 it->face_id = FACE_FOR_CHAR (f, face, 0);
14620 }
14621
14622 if (FRAME_WINDOW_P (f))
14623 {
14624 /* If the row is empty, add a space with the current face of IT,
14625 so that we know which face to draw. */
14626 if (it->glyph_row->used[TEXT_AREA] == 0)
14627 {
14628 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14629 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14630 it->glyph_row->used[TEXT_AREA] = 1;
14631 }
14632 }
14633 else
14634 {
14635 /* Save some values that must not be changed. */
14636 int saved_x = it->current_x;
14637 struct text_pos saved_pos;
14638 Lisp_Object saved_object;
14639 enum display_element_type saved_what = it->what;
14640 int saved_face_id = it->face_id;
14641
14642 saved_object = it->object;
14643 saved_pos = it->position;
14644
14645 it->what = IT_CHARACTER;
14646 bzero (&it->position, sizeof it->position);
14647 it->object = make_number (0);
14648 it->c = ' ';
14649 it->len = 1;
14650 it->face_id = face->id;
14651
14652 PRODUCE_GLYPHS (it);
14653
14654 while (it->current_x <= it->last_visible_x)
14655 PRODUCE_GLYPHS (it);
14656
14657 /* Don't count these blanks really. It would let us insert a left
14658 truncation glyph below and make us set the cursor on them, maybe. */
14659 it->current_x = saved_x;
14660 it->object = saved_object;
14661 it->position = saved_pos;
14662 it->what = saved_what;
14663 it->face_id = saved_face_id;
14664 }
14665 }
14666
14667
14668 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14669 trailing whitespace. */
14670
14671 static int
14672 trailing_whitespace_p (charpos)
14673 int charpos;
14674 {
14675 int bytepos = CHAR_TO_BYTE (charpos);
14676 int c = 0;
14677
14678 while (bytepos < ZV_BYTE
14679 && (c = FETCH_CHAR (bytepos),
14680 c == ' ' || c == '\t'))
14681 ++bytepos;
14682
14683 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14684 {
14685 if (bytepos != PT_BYTE)
14686 return 1;
14687 }
14688 return 0;
14689 }
14690
14691
14692 /* Highlight trailing whitespace, if any, in ROW. */
14693
14694 void
14695 highlight_trailing_whitespace (f, row)
14696 struct frame *f;
14697 struct glyph_row *row;
14698 {
14699 int used = row->used[TEXT_AREA];
14700
14701 if (used)
14702 {
14703 struct glyph *start = row->glyphs[TEXT_AREA];
14704 struct glyph *glyph = start + used - 1;
14705
14706 /* Skip over glyphs inserted to display the cursor at the
14707 end of a line, for extending the face of the last glyph
14708 to the end of the line on terminals, and for truncation
14709 and continuation glyphs. */
14710 while (glyph >= start
14711 && glyph->type == CHAR_GLYPH
14712 && INTEGERP (glyph->object))
14713 --glyph;
14714
14715 /* If last glyph is a space or stretch, and it's trailing
14716 whitespace, set the face of all trailing whitespace glyphs in
14717 IT->glyph_row to `trailing-whitespace'. */
14718 if (glyph >= start
14719 && BUFFERP (glyph->object)
14720 && (glyph->type == STRETCH_GLYPH
14721 || (glyph->type == CHAR_GLYPH
14722 && glyph->u.ch == ' '))
14723 && trailing_whitespace_p (glyph->charpos))
14724 {
14725 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14726
14727 while (glyph >= start
14728 && BUFFERP (glyph->object)
14729 && (glyph->type == STRETCH_GLYPH
14730 || (glyph->type == CHAR_GLYPH
14731 && glyph->u.ch == ' ')))
14732 (glyph--)->face_id = face_id;
14733 }
14734 }
14735 }
14736
14737
14738 /* Value is non-zero if glyph row ROW in window W should be
14739 used to hold the cursor. */
14740
14741 static int
14742 cursor_row_p (w, row)
14743 struct window *w;
14744 struct glyph_row *row;
14745 {
14746 int cursor_row_p = 1;
14747
14748 if (PT == MATRIX_ROW_END_CHARPOS (row))
14749 {
14750 /* If the row ends with a newline from a string, we don't want
14751 the cursor there (if the row is continued it doesn't end in a
14752 newline). */
14753 if (CHARPOS (row->end.string_pos) >= 0
14754 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14755 cursor_row_p = row->continued_p;
14756
14757 /* If the row ends at ZV, display the cursor at the end of that
14758 row instead of at the start of the row below. */
14759 else if (row->ends_at_zv_p)
14760 cursor_row_p = 1;
14761 else
14762 cursor_row_p = 0;
14763 }
14764
14765 return cursor_row_p;
14766 }
14767
14768
14769 /* Construct the glyph row IT->glyph_row in the desired matrix of
14770 IT->w from text at the current position of IT. See dispextern.h
14771 for an overview of struct it. Value is non-zero if
14772 IT->glyph_row displays text, as opposed to a line displaying ZV
14773 only. */
14774
14775 static int
14776 display_line (it)
14777 struct it *it;
14778 {
14779 struct glyph_row *row = it->glyph_row;
14780
14781 /* We always start displaying at hpos zero even if hscrolled. */
14782 xassert (it->hpos == 0 && it->current_x == 0);
14783
14784 /* We must not display in a row that's not a text row. */
14785 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14786 < it->w->desired_matrix->nrows);
14787
14788 /* Is IT->w showing the region? */
14789 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14790
14791 /* Clear the result glyph row and enable it. */
14792 prepare_desired_row (row);
14793
14794 row->y = it->current_y;
14795 row->start = it->start;
14796 row->continuation_lines_width = it->continuation_lines_width;
14797 row->displays_text_p = 1;
14798 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14799 it->starts_in_middle_of_char_p = 0;
14800
14801 /* Arrange the overlays nicely for our purposes. Usually, we call
14802 display_line on only one line at a time, in which case this
14803 can't really hurt too much, or we call it on lines which appear
14804 one after another in the buffer, in which case all calls to
14805 recenter_overlay_lists but the first will be pretty cheap. */
14806 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14807
14808 /* Move over display elements that are not visible because we are
14809 hscrolled. This may stop at an x-position < IT->first_visible_x
14810 if the first glyph is partially visible or if we hit a line end. */
14811 if (it->current_x < it->first_visible_x)
14812 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14813 MOVE_TO_POS | MOVE_TO_X);
14814
14815 /* Get the initial row height. This is either the height of the
14816 text hscrolled, if there is any, or zero. */
14817 row->ascent = it->max_ascent;
14818 row->height = it->max_ascent + it->max_descent;
14819 row->phys_ascent = it->max_phys_ascent;
14820 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14821
14822 /* Loop generating characters. The loop is left with IT on the next
14823 character to display. */
14824 while (1)
14825 {
14826 int n_glyphs_before, hpos_before, x_before;
14827 int x, i, nglyphs;
14828 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14829
14830 /* Retrieve the next thing to display. Value is zero if end of
14831 buffer reached. */
14832 if (!get_next_display_element (it))
14833 {
14834 /* Maybe add a space at the end of this line that is used to
14835 display the cursor there under X. Set the charpos of the
14836 first glyph of blank lines not corresponding to any text
14837 to -1. */
14838 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14839 || row->used[TEXT_AREA] == 0)
14840 {
14841 row->glyphs[TEXT_AREA]->charpos = -1;
14842 row->displays_text_p = 0;
14843
14844 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14845 && (!MINI_WINDOW_P (it->w)
14846 || (minibuf_level && EQ (it->window, minibuf_window))))
14847 row->indicate_empty_line_p = 1;
14848 }
14849
14850 it->continuation_lines_width = 0;
14851 row->ends_at_zv_p = 1;
14852 break;
14853 }
14854
14855 /* Now, get the metrics of what we want to display. This also
14856 generates glyphs in `row' (which is IT->glyph_row). */
14857 n_glyphs_before = row->used[TEXT_AREA];
14858 x = it->current_x;
14859
14860 /* Remember the line height so far in case the next element doesn't
14861 fit on the line. */
14862 if (!it->truncate_lines_p)
14863 {
14864 ascent = it->max_ascent;
14865 descent = it->max_descent;
14866 phys_ascent = it->max_phys_ascent;
14867 phys_descent = it->max_phys_descent;
14868 }
14869
14870 PRODUCE_GLYPHS (it);
14871
14872 /* If this display element was in marginal areas, continue with
14873 the next one. */
14874 if (it->area != TEXT_AREA)
14875 {
14876 row->ascent = max (row->ascent, it->max_ascent);
14877 row->height = max (row->height, it->max_ascent + it->max_descent);
14878 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14879 row->phys_height = max (row->phys_height,
14880 it->max_phys_ascent + it->max_phys_descent);
14881 set_iterator_to_next (it, 1);
14882 continue;
14883 }
14884
14885 /* Does the display element fit on the line? If we truncate
14886 lines, we should draw past the right edge of the window. If
14887 we don't truncate, we want to stop so that we can display the
14888 continuation glyph before the right margin. If lines are
14889 continued, there are two possible strategies for characters
14890 resulting in more than 1 glyph (e.g. tabs): Display as many
14891 glyphs as possible in this line and leave the rest for the
14892 continuation line, or display the whole element in the next
14893 line. Original redisplay did the former, so we do it also. */
14894 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14895 hpos_before = it->hpos;
14896 x_before = x;
14897
14898 if (/* Not a newline. */
14899 nglyphs > 0
14900 /* Glyphs produced fit entirely in the line. */
14901 && it->current_x < it->last_visible_x)
14902 {
14903 it->hpos += nglyphs;
14904 row->ascent = max (row->ascent, it->max_ascent);
14905 row->height = max (row->height, it->max_ascent + it->max_descent);
14906 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14907 row->phys_height = max (row->phys_height,
14908 it->max_phys_ascent + it->max_phys_descent);
14909 if (it->current_x - it->pixel_width < it->first_visible_x)
14910 row->x = x - it->first_visible_x;
14911 }
14912 else
14913 {
14914 int new_x;
14915 struct glyph *glyph;
14916
14917 for (i = 0; i < nglyphs; ++i, x = new_x)
14918 {
14919 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14920 new_x = x + glyph->pixel_width;
14921
14922 if (/* Lines are continued. */
14923 !it->truncate_lines_p
14924 && (/* Glyph doesn't fit on the line. */
14925 new_x > it->last_visible_x
14926 /* Or it fits exactly on a window system frame. */
14927 || (new_x == it->last_visible_x
14928 && FRAME_WINDOW_P (it->f))))
14929 {
14930 /* End of a continued line. */
14931
14932 if (it->hpos == 0
14933 || (new_x == it->last_visible_x
14934 && FRAME_WINDOW_P (it->f)))
14935 {
14936 /* Current glyph is the only one on the line or
14937 fits exactly on the line. We must continue
14938 the line because we can't draw the cursor
14939 after the glyph. */
14940 row->continued_p = 1;
14941 it->current_x = new_x;
14942 it->continuation_lines_width += new_x;
14943 ++it->hpos;
14944 if (i == nglyphs - 1)
14945 {
14946 set_iterator_to_next (it, 1);
14947 #ifdef HAVE_WINDOW_SYSTEM
14948 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14949 {
14950 get_next_display_element (it);
14951 if (ITERATOR_AT_END_OF_LINE_P (it))
14952 {
14953 row->continued_p = 0;
14954 row->exact_window_width_line_p = 1;
14955 }
14956 }
14957 #endif /* HAVE_WINDOW_SYSTEM */
14958 }
14959 }
14960 else if (CHAR_GLYPH_PADDING_P (*glyph)
14961 && !FRAME_WINDOW_P (it->f))
14962 {
14963 /* A padding glyph that doesn't fit on this line.
14964 This means the whole character doesn't fit
14965 on the line. */
14966 row->used[TEXT_AREA] = n_glyphs_before;
14967
14968 /* Fill the rest of the row with continuation
14969 glyphs like in 20.x. */
14970 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14971 < row->glyphs[1 + TEXT_AREA])
14972 produce_special_glyphs (it, IT_CONTINUATION);
14973
14974 row->continued_p = 1;
14975 it->current_x = x_before;
14976 it->continuation_lines_width += x_before;
14977
14978 /* Restore the height to what it was before the
14979 element not fitting on the line. */
14980 it->max_ascent = ascent;
14981 it->max_descent = descent;
14982 it->max_phys_ascent = phys_ascent;
14983 it->max_phys_descent = phys_descent;
14984 }
14985 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14986 {
14987 /* A TAB that extends past the right edge of the
14988 window. This produces a single glyph on
14989 window system frames. We leave the glyph in
14990 this row and let it fill the row, but don't
14991 consume the TAB. */
14992 it->continuation_lines_width += it->last_visible_x;
14993 row->ends_in_middle_of_char_p = 1;
14994 row->continued_p = 1;
14995 glyph->pixel_width = it->last_visible_x - x;
14996 it->starts_in_middle_of_char_p = 1;
14997 }
14998 else
14999 {
15000 /* Something other than a TAB that draws past
15001 the right edge of the window. Restore
15002 positions to values before the element. */
15003 row->used[TEXT_AREA] = n_glyphs_before + i;
15004
15005 /* Display continuation glyphs. */
15006 if (!FRAME_WINDOW_P (it->f))
15007 produce_special_glyphs (it, IT_CONTINUATION);
15008 row->continued_p = 1;
15009
15010 it->continuation_lines_width += x;
15011
15012 if (nglyphs > 1 && i > 0)
15013 {
15014 row->ends_in_middle_of_char_p = 1;
15015 it->starts_in_middle_of_char_p = 1;
15016 }
15017
15018 /* Restore the height to what it was before the
15019 element not fitting on the line. */
15020 it->max_ascent = ascent;
15021 it->max_descent = descent;
15022 it->max_phys_ascent = phys_ascent;
15023 it->max_phys_descent = phys_descent;
15024 }
15025
15026 break;
15027 }
15028 else if (new_x > it->first_visible_x)
15029 {
15030 /* Increment number of glyphs actually displayed. */
15031 ++it->hpos;
15032
15033 if (x < it->first_visible_x)
15034 /* Glyph is partially visible, i.e. row starts at
15035 negative X position. */
15036 row->x = x - it->first_visible_x;
15037 }
15038 else
15039 {
15040 /* Glyph is completely off the left margin of the
15041 window. This should not happen because of the
15042 move_it_in_display_line at the start of this
15043 function, unless the text display area of the
15044 window is empty. */
15045 xassert (it->first_visible_x <= it->last_visible_x);
15046 }
15047 }
15048
15049 row->ascent = max (row->ascent, it->max_ascent);
15050 row->height = max (row->height, it->max_ascent + it->max_descent);
15051 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15052 row->phys_height = max (row->phys_height,
15053 it->max_phys_ascent + it->max_phys_descent);
15054
15055 /* End of this display line if row is continued. */
15056 if (row->continued_p)
15057 break;
15058 }
15059
15060 at_end_of_line:
15061 /* Is this a line end? If yes, we're also done, after making
15062 sure that a non-default face is extended up to the right
15063 margin of the window. */
15064 if (ITERATOR_AT_END_OF_LINE_P (it))
15065 {
15066 int used_before = row->used[TEXT_AREA];
15067
15068 row->ends_in_newline_from_string_p = STRINGP (it->object);
15069
15070 #ifdef HAVE_WINDOW_SYSTEM
15071 /* Add a space at the end of the line that is used to
15072 display the cursor there. */
15073 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15074 append_space (it, 0);
15075 #endif /* HAVE_WINDOW_SYSTEM */
15076
15077 /* Extend the face to the end of the line. */
15078 extend_face_to_end_of_line (it);
15079
15080 /* Make sure we have the position. */
15081 if (used_before == 0)
15082 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15083
15084 /* Consume the line end. This skips over invisible lines. */
15085 set_iterator_to_next (it, 1);
15086 it->continuation_lines_width = 0;
15087 break;
15088 }
15089
15090 /* Proceed with next display element. Note that this skips
15091 over lines invisible because of selective display. */
15092 set_iterator_to_next (it, 1);
15093
15094 /* If we truncate lines, we are done when the last displayed
15095 glyphs reach past the right margin of the window. */
15096 if (it->truncate_lines_p
15097 && (FRAME_WINDOW_P (it->f)
15098 ? (it->current_x >= it->last_visible_x)
15099 : (it->current_x > it->last_visible_x)))
15100 {
15101 /* Maybe add truncation glyphs. */
15102 if (!FRAME_WINDOW_P (it->f))
15103 {
15104 int i, n;
15105
15106 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15107 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15108 break;
15109
15110 for (n = row->used[TEXT_AREA]; i < n; ++i)
15111 {
15112 row->used[TEXT_AREA] = i;
15113 produce_special_glyphs (it, IT_TRUNCATION);
15114 }
15115 }
15116 #ifdef HAVE_WINDOW_SYSTEM
15117 else
15118 {
15119 /* Don't truncate if we can overflow newline into fringe. */
15120 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15121 {
15122 get_next_display_element (it);
15123 if (ITERATOR_AT_END_OF_LINE_P (it))
15124 {
15125 row->exact_window_width_line_p = 1;
15126 goto at_end_of_line;
15127 }
15128 }
15129 }
15130 #endif /* HAVE_WINDOW_SYSTEM */
15131
15132 row->truncated_on_right_p = 1;
15133 it->continuation_lines_width = 0;
15134 reseat_at_next_visible_line_start (it, 0);
15135 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15136 it->hpos = hpos_before;
15137 it->current_x = x_before;
15138 break;
15139 }
15140 }
15141
15142 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15143 at the left window margin. */
15144 if (it->first_visible_x
15145 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15146 {
15147 if (!FRAME_WINDOW_P (it->f))
15148 insert_left_trunc_glyphs (it);
15149 row->truncated_on_left_p = 1;
15150 }
15151
15152 /* If the start of this line is the overlay arrow-position, then
15153 mark this glyph row as the one containing the overlay arrow.
15154 This is clearly a mess with variable size fonts. It would be
15155 better to let it be displayed like cursors under X. */
15156 if (MARKERP (Voverlay_arrow_position)
15157 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
15158 && (MATRIX_ROW_START_CHARPOS (row)
15159 == marker_position (Voverlay_arrow_position))
15160 && STRINGP (Voverlay_arrow_string)
15161 && ! overlay_arrow_seen)
15162 {
15163 /* Overlay arrow in window redisplay is a fringe bitmap. */
15164 if (!FRAME_WINDOW_P (it->f))
15165 {
15166 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
15167 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15168 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15169 struct glyph *p = row->glyphs[TEXT_AREA];
15170 struct glyph *p2, *end;
15171
15172 /* Copy the arrow glyphs. */
15173 while (glyph < arrow_end)
15174 *p++ = *glyph++;
15175
15176 /* Throw away padding glyphs. */
15177 p2 = p;
15178 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15179 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15180 ++p2;
15181 if (p2 > p)
15182 {
15183 while (p2 < end)
15184 *p++ = *p2++;
15185 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15186 }
15187 }
15188
15189 overlay_arrow_seen = 1;
15190 row->overlay_arrow_p = 1;
15191 }
15192
15193 /* Compute pixel dimensions of this line. */
15194 compute_line_metrics (it);
15195
15196 /* Remember the position at which this line ends. */
15197 row->end = it->current;
15198
15199 /* Maybe set the cursor. */
15200 if (it->w->cursor.vpos < 0
15201 && PT >= MATRIX_ROW_START_CHARPOS (row)
15202 && PT <= MATRIX_ROW_END_CHARPOS (row)
15203 && cursor_row_p (it->w, row))
15204 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15205
15206 /* Highlight trailing whitespace. */
15207 if (!NILP (Vshow_trailing_whitespace))
15208 highlight_trailing_whitespace (it->f, it->glyph_row);
15209
15210 /* Prepare for the next line. This line starts horizontally at (X
15211 HPOS) = (0 0). Vertical positions are incremented. As a
15212 convenience for the caller, IT->glyph_row is set to the next
15213 row to be used. */
15214 it->current_x = it->hpos = 0;
15215 it->current_y += row->height;
15216 ++it->vpos;
15217 ++it->glyph_row;
15218 it->start = it->current;
15219 return row->displays_text_p;
15220 }
15221
15222
15223 \f
15224 /***********************************************************************
15225 Menu Bar
15226 ***********************************************************************/
15227
15228 /* Redisplay the menu bar in the frame for window W.
15229
15230 The menu bar of X frames that don't have X toolkit support is
15231 displayed in a special window W->frame->menu_bar_window.
15232
15233 The menu bar of terminal frames is treated specially as far as
15234 glyph matrices are concerned. Menu bar lines are not part of
15235 windows, so the update is done directly on the frame matrix rows
15236 for the menu bar. */
15237
15238 static void
15239 display_menu_bar (w)
15240 struct window *w;
15241 {
15242 struct frame *f = XFRAME (WINDOW_FRAME (w));
15243 struct it it;
15244 Lisp_Object items;
15245 int i;
15246
15247 /* Don't do all this for graphical frames. */
15248 #ifdef HAVE_NTGUI
15249 if (!NILP (Vwindow_system))
15250 return;
15251 #endif
15252 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15253 if (FRAME_X_P (f))
15254 return;
15255 #endif
15256 #ifdef MAC_OS
15257 if (FRAME_MAC_P (f))
15258 return;
15259 #endif
15260
15261 #ifdef USE_X_TOOLKIT
15262 xassert (!FRAME_WINDOW_P (f));
15263 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15264 it.first_visible_x = 0;
15265 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15266 #else /* not USE_X_TOOLKIT */
15267 if (FRAME_WINDOW_P (f))
15268 {
15269 /* Menu bar lines are displayed in the desired matrix of the
15270 dummy window menu_bar_window. */
15271 struct window *menu_w;
15272 xassert (WINDOWP (f->menu_bar_window));
15273 menu_w = XWINDOW (f->menu_bar_window);
15274 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15275 MENU_FACE_ID);
15276 it.first_visible_x = 0;
15277 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15278 }
15279 else
15280 {
15281 /* This is a TTY frame, i.e. character hpos/vpos are used as
15282 pixel x/y. */
15283 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15284 MENU_FACE_ID);
15285 it.first_visible_x = 0;
15286 it.last_visible_x = FRAME_COLS (f);
15287 }
15288 #endif /* not USE_X_TOOLKIT */
15289
15290 if (! mode_line_inverse_video)
15291 /* Force the menu-bar to be displayed in the default face. */
15292 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15293
15294 /* Clear all rows of the menu bar. */
15295 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15296 {
15297 struct glyph_row *row = it.glyph_row + i;
15298 clear_glyph_row (row);
15299 row->enabled_p = 1;
15300 row->full_width_p = 1;
15301 }
15302
15303 /* Display all items of the menu bar. */
15304 items = FRAME_MENU_BAR_ITEMS (it.f);
15305 for (i = 0; i < XVECTOR (items)->size; i += 4)
15306 {
15307 Lisp_Object string;
15308
15309 /* Stop at nil string. */
15310 string = AREF (items, i + 1);
15311 if (NILP (string))
15312 break;
15313
15314 /* Remember where item was displayed. */
15315 AREF (items, i + 3) = make_number (it.hpos);
15316
15317 /* Display the item, pad with one space. */
15318 if (it.current_x < it.last_visible_x)
15319 display_string (NULL, string, Qnil, 0, 0, &it,
15320 SCHARS (string) + 1, 0, 0, -1);
15321 }
15322
15323 /* Fill out the line with spaces. */
15324 if (it.current_x < it.last_visible_x)
15325 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15326
15327 /* Compute the total height of the lines. */
15328 compute_line_metrics (&it);
15329 }
15330
15331
15332 \f
15333 /***********************************************************************
15334 Mode Line
15335 ***********************************************************************/
15336
15337 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15338 FORCE is non-zero, redisplay mode lines unconditionally.
15339 Otherwise, redisplay only mode lines that are garbaged. Value is
15340 the number of windows whose mode lines were redisplayed. */
15341
15342 static int
15343 redisplay_mode_lines (window, force)
15344 Lisp_Object window;
15345 int force;
15346 {
15347 int nwindows = 0;
15348
15349 while (!NILP (window))
15350 {
15351 struct window *w = XWINDOW (window);
15352
15353 if (WINDOWP (w->hchild))
15354 nwindows += redisplay_mode_lines (w->hchild, force);
15355 else if (WINDOWP (w->vchild))
15356 nwindows += redisplay_mode_lines (w->vchild, force);
15357 else if (force
15358 || FRAME_GARBAGED_P (XFRAME (w->frame))
15359 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15360 {
15361 struct text_pos lpoint;
15362 struct buffer *old = current_buffer;
15363
15364 /* Set the window's buffer for the mode line display. */
15365 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15366 set_buffer_internal_1 (XBUFFER (w->buffer));
15367
15368 /* Point refers normally to the selected window. For any
15369 other window, set up appropriate value. */
15370 if (!EQ (window, selected_window))
15371 {
15372 struct text_pos pt;
15373
15374 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15375 if (CHARPOS (pt) < BEGV)
15376 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15377 else if (CHARPOS (pt) > (ZV - 1))
15378 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15379 else
15380 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15381 }
15382
15383 /* Display mode lines. */
15384 clear_glyph_matrix (w->desired_matrix);
15385 if (display_mode_lines (w))
15386 {
15387 ++nwindows;
15388 w->must_be_updated_p = 1;
15389 }
15390
15391 /* Restore old settings. */
15392 set_buffer_internal_1 (old);
15393 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15394 }
15395
15396 window = w->next;
15397 }
15398
15399 return nwindows;
15400 }
15401
15402
15403 /* Display the mode and/or top line of window W. Value is the number
15404 of mode lines displayed. */
15405
15406 static int
15407 display_mode_lines (w)
15408 struct window *w;
15409 {
15410 Lisp_Object old_selected_window, old_selected_frame;
15411 int n = 0;
15412
15413 old_selected_frame = selected_frame;
15414 selected_frame = w->frame;
15415 old_selected_window = selected_window;
15416 XSETWINDOW (selected_window, w);
15417
15418 /* These will be set while the mode line specs are processed. */
15419 line_number_displayed = 0;
15420 w->column_number_displayed = Qnil;
15421
15422 if (WINDOW_WANTS_MODELINE_P (w))
15423 {
15424 struct window *sel_w = XWINDOW (old_selected_window);
15425
15426 /* Select mode line face based on the real selected window. */
15427 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15428 current_buffer->mode_line_format);
15429 ++n;
15430 }
15431
15432 if (WINDOW_WANTS_HEADER_LINE_P (w))
15433 {
15434 display_mode_line (w, HEADER_LINE_FACE_ID,
15435 current_buffer->header_line_format);
15436 ++n;
15437 }
15438
15439 selected_frame = old_selected_frame;
15440 selected_window = old_selected_window;
15441 return n;
15442 }
15443
15444
15445 /* Display mode or top line of window W. FACE_ID specifies which line
15446 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15447 FORMAT is the mode line format to display. Value is the pixel
15448 height of the mode line displayed. */
15449
15450 static int
15451 display_mode_line (w, face_id, format)
15452 struct window *w;
15453 enum face_id face_id;
15454 Lisp_Object format;
15455 {
15456 struct it it;
15457 struct face *face;
15458
15459 init_iterator (&it, w, -1, -1, NULL, face_id);
15460 prepare_desired_row (it.glyph_row);
15461
15462 if (! mode_line_inverse_video)
15463 /* Force the mode-line to be displayed in the default face. */
15464 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15465
15466 /* Temporarily make frame's keyboard the current kboard so that
15467 kboard-local variables in the mode_line_format will get the right
15468 values. */
15469 push_frame_kboard (it.f);
15470 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15471 pop_frame_kboard ();
15472
15473 /* Fill up with spaces. */
15474 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15475
15476 compute_line_metrics (&it);
15477 it.glyph_row->full_width_p = 1;
15478 it.glyph_row->mode_line_p = 1;
15479 it.glyph_row->continued_p = 0;
15480 it.glyph_row->truncated_on_left_p = 0;
15481 it.glyph_row->truncated_on_right_p = 0;
15482
15483 /* Make a 3D mode-line have a shadow at its right end. */
15484 face = FACE_FROM_ID (it.f, face_id);
15485 extend_face_to_end_of_line (&it);
15486 if (face->box != FACE_NO_BOX)
15487 {
15488 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15489 + it.glyph_row->used[TEXT_AREA] - 1);
15490 last->right_box_line_p = 1;
15491 }
15492
15493 return it.glyph_row->height;
15494 }
15495
15496 /* Alist that caches the results of :propertize.
15497 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15498 Lisp_Object mode_line_proptrans_alist;
15499
15500 /* List of strings making up the mode-line. */
15501 Lisp_Object mode_line_string_list;
15502
15503 /* Base face property when building propertized mode line string. */
15504 static Lisp_Object mode_line_string_face;
15505 static Lisp_Object mode_line_string_face_prop;
15506
15507
15508 /* Contribute ELT to the mode line for window IT->w. How it
15509 translates into text depends on its data type.
15510
15511 IT describes the display environment in which we display, as usual.
15512
15513 DEPTH is the depth in recursion. It is used to prevent
15514 infinite recursion here.
15515
15516 FIELD_WIDTH is the number of characters the display of ELT should
15517 occupy in the mode line, and PRECISION is the maximum number of
15518 characters to display from ELT's representation. See
15519 display_string for details.
15520
15521 Returns the hpos of the end of the text generated by ELT.
15522
15523 PROPS is a property list to add to any string we encounter.
15524
15525 If RISKY is nonzero, remove (disregard) any properties in any string
15526 we encounter, and ignore :eval and :propertize.
15527
15528 If the global variable `frame_title_ptr' is non-NULL, then the output
15529 is passed to `store_frame_title' instead of `display_string'. */
15530
15531 static int
15532 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15533 struct it *it;
15534 int depth;
15535 int field_width, precision;
15536 Lisp_Object elt, props;
15537 int risky;
15538 {
15539 int n = 0, field, prec;
15540 int literal = 0;
15541
15542 tail_recurse:
15543 if (depth > 100)
15544 elt = build_string ("*too-deep*");
15545
15546 depth++;
15547
15548 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15549 {
15550 case Lisp_String:
15551 {
15552 /* A string: output it and check for %-constructs within it. */
15553 unsigned char c;
15554 const unsigned char *this, *lisp_string;
15555
15556 if (!NILP (props) || risky)
15557 {
15558 Lisp_Object oprops, aelt;
15559 oprops = Ftext_properties_at (make_number (0), elt);
15560
15561 if (NILP (Fequal (props, oprops)) || risky)
15562 {
15563 /* If the starting string has properties,
15564 merge the specified ones onto the existing ones. */
15565 if (! NILP (oprops) && !risky)
15566 {
15567 Lisp_Object tem;
15568
15569 oprops = Fcopy_sequence (oprops);
15570 tem = props;
15571 while (CONSP (tem))
15572 {
15573 oprops = Fplist_put (oprops, XCAR (tem),
15574 XCAR (XCDR (tem)));
15575 tem = XCDR (XCDR (tem));
15576 }
15577 props = oprops;
15578 }
15579
15580 aelt = Fassoc (elt, mode_line_proptrans_alist);
15581 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15582 {
15583 mode_line_proptrans_alist
15584 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15585 elt = XCAR (aelt);
15586 }
15587 else
15588 {
15589 Lisp_Object tem;
15590
15591 elt = Fcopy_sequence (elt);
15592 Fset_text_properties (make_number (0), Flength (elt),
15593 props, elt);
15594 /* Add this item to mode_line_proptrans_alist. */
15595 mode_line_proptrans_alist
15596 = Fcons (Fcons (elt, props),
15597 mode_line_proptrans_alist);
15598 /* Truncate mode_line_proptrans_alist
15599 to at most 50 elements. */
15600 tem = Fnthcdr (make_number (50),
15601 mode_line_proptrans_alist);
15602 if (! NILP (tem))
15603 XSETCDR (tem, Qnil);
15604 }
15605 }
15606 }
15607
15608 this = SDATA (elt);
15609 lisp_string = this;
15610
15611 if (literal)
15612 {
15613 prec = precision - n;
15614 if (frame_title_ptr)
15615 n += store_frame_title (SDATA (elt), -1, prec);
15616 else if (!NILP (mode_line_string_list))
15617 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15618 else
15619 n += display_string (NULL, elt, Qnil, 0, 0, it,
15620 0, prec, 0, STRING_MULTIBYTE (elt));
15621
15622 break;
15623 }
15624
15625 while ((precision <= 0 || n < precision)
15626 && *this
15627 && (frame_title_ptr
15628 || !NILP (mode_line_string_list)
15629 || it->current_x < it->last_visible_x))
15630 {
15631 const unsigned char *last = this;
15632
15633 /* Advance to end of string or next format specifier. */
15634 while ((c = *this++) != '\0' && c != '%')
15635 ;
15636
15637 if (this - 1 != last)
15638 {
15639 /* Output to end of string or up to '%'. Field width
15640 is length of string. Don't output more than
15641 PRECISION allows us. */
15642 --this;
15643
15644 prec = chars_in_text (last, this - last);
15645 if (precision > 0 && prec > precision - n)
15646 prec = precision - n;
15647
15648 if (frame_title_ptr)
15649 n += store_frame_title (last, 0, prec);
15650 else if (!NILP (mode_line_string_list))
15651 {
15652 int bytepos = last - lisp_string;
15653 int charpos = string_byte_to_char (elt, bytepos);
15654 n += store_mode_line_string (NULL,
15655 Fsubstring (elt, make_number (charpos),
15656 make_number (charpos + prec)),
15657 0, 0, 0, Qnil);
15658 }
15659 else
15660 {
15661 int bytepos = last - lisp_string;
15662 int charpos = string_byte_to_char (elt, bytepos);
15663 n += display_string (NULL, elt, Qnil, 0, charpos,
15664 it, 0, prec, 0,
15665 STRING_MULTIBYTE (elt));
15666 }
15667 }
15668 else /* c == '%' */
15669 {
15670 const unsigned char *percent_position = this;
15671
15672 /* Get the specified minimum width. Zero means
15673 don't pad. */
15674 field = 0;
15675 while ((c = *this++) >= '0' && c <= '9')
15676 field = field * 10 + c - '0';
15677
15678 /* Don't pad beyond the total padding allowed. */
15679 if (field_width - n > 0 && field > field_width - n)
15680 field = field_width - n;
15681
15682 /* Note that either PRECISION <= 0 or N < PRECISION. */
15683 prec = precision - n;
15684
15685 if (c == 'M')
15686 n += display_mode_element (it, depth, field, prec,
15687 Vglobal_mode_string, props,
15688 risky);
15689 else if (c != 0)
15690 {
15691 int multibyte;
15692 int bytepos, charpos;
15693 unsigned char *spec;
15694
15695 bytepos = percent_position - lisp_string;
15696 charpos = (STRING_MULTIBYTE (elt)
15697 ? string_byte_to_char (elt, bytepos)
15698 : bytepos);
15699
15700 spec
15701 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15702
15703 if (frame_title_ptr)
15704 n += store_frame_title (spec, field, prec);
15705 else if (!NILP (mode_line_string_list))
15706 {
15707 int len = strlen (spec);
15708 Lisp_Object tem = make_string (spec, len);
15709 props = Ftext_properties_at (make_number (charpos), elt);
15710 /* Should only keep face property in props */
15711 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15712 }
15713 else
15714 {
15715 int nglyphs_before, nwritten;
15716
15717 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15718 nwritten = display_string (spec, Qnil, elt,
15719 charpos, 0, it,
15720 field, prec, 0,
15721 multibyte);
15722
15723 /* Assign to the glyphs written above the
15724 string where the `%x' came from, position
15725 of the `%'. */
15726 if (nwritten > 0)
15727 {
15728 struct glyph *glyph
15729 = (it->glyph_row->glyphs[TEXT_AREA]
15730 + nglyphs_before);
15731 int i;
15732
15733 for (i = 0; i < nwritten; ++i)
15734 {
15735 glyph[i].object = elt;
15736 glyph[i].charpos = charpos;
15737 }
15738
15739 n += nwritten;
15740 }
15741 }
15742 }
15743 else /* c == 0 */
15744 break;
15745 }
15746 }
15747 }
15748 break;
15749
15750 case Lisp_Symbol:
15751 /* A symbol: process the value of the symbol recursively
15752 as if it appeared here directly. Avoid error if symbol void.
15753 Special case: if value of symbol is a string, output the string
15754 literally. */
15755 {
15756 register Lisp_Object tem;
15757
15758 /* If the variable is not marked as risky to set
15759 then its contents are risky to use. */
15760 if (NILP (Fget (elt, Qrisky_local_variable)))
15761 risky = 1;
15762
15763 tem = Fboundp (elt);
15764 if (!NILP (tem))
15765 {
15766 tem = Fsymbol_value (elt);
15767 /* If value is a string, output that string literally:
15768 don't check for % within it. */
15769 if (STRINGP (tem))
15770 literal = 1;
15771
15772 if (!EQ (tem, elt))
15773 {
15774 /* Give up right away for nil or t. */
15775 elt = tem;
15776 goto tail_recurse;
15777 }
15778 }
15779 }
15780 break;
15781
15782 case Lisp_Cons:
15783 {
15784 register Lisp_Object car, tem;
15785
15786 /* A cons cell: five distinct cases.
15787 If first element is :eval or :propertize, do something special.
15788 If first element is a string or a cons, process all the elements
15789 and effectively concatenate them.
15790 If first element is a negative number, truncate displaying cdr to
15791 at most that many characters. If positive, pad (with spaces)
15792 to at least that many characters.
15793 If first element is a symbol, process the cadr or caddr recursively
15794 according to whether the symbol's value is non-nil or nil. */
15795 car = XCAR (elt);
15796 if (EQ (car, QCeval))
15797 {
15798 /* An element of the form (:eval FORM) means evaluate FORM
15799 and use the result as mode line elements. */
15800
15801 if (risky)
15802 break;
15803
15804 if (CONSP (XCDR (elt)))
15805 {
15806 Lisp_Object spec;
15807 spec = safe_eval (XCAR (XCDR (elt)));
15808 n += display_mode_element (it, depth, field_width - n,
15809 precision - n, spec, props,
15810 risky);
15811 }
15812 }
15813 else if (EQ (car, QCpropertize))
15814 {
15815 /* An element of the form (:propertize ELT PROPS...)
15816 means display ELT but applying properties PROPS. */
15817
15818 if (risky)
15819 break;
15820
15821 if (CONSP (XCDR (elt)))
15822 n += display_mode_element (it, depth, field_width - n,
15823 precision - n, XCAR (XCDR (elt)),
15824 XCDR (XCDR (elt)), risky);
15825 }
15826 else if (SYMBOLP (car))
15827 {
15828 tem = Fboundp (car);
15829 elt = XCDR (elt);
15830 if (!CONSP (elt))
15831 goto invalid;
15832 /* elt is now the cdr, and we know it is a cons cell.
15833 Use its car if CAR has a non-nil value. */
15834 if (!NILP (tem))
15835 {
15836 tem = Fsymbol_value (car);
15837 if (!NILP (tem))
15838 {
15839 elt = XCAR (elt);
15840 goto tail_recurse;
15841 }
15842 }
15843 /* Symbol's value is nil (or symbol is unbound)
15844 Get the cddr of the original list
15845 and if possible find the caddr and use that. */
15846 elt = XCDR (elt);
15847 if (NILP (elt))
15848 break;
15849 else if (!CONSP (elt))
15850 goto invalid;
15851 elt = XCAR (elt);
15852 goto tail_recurse;
15853 }
15854 else if (INTEGERP (car))
15855 {
15856 register int lim = XINT (car);
15857 elt = XCDR (elt);
15858 if (lim < 0)
15859 {
15860 /* Negative int means reduce maximum width. */
15861 if (precision <= 0)
15862 precision = -lim;
15863 else
15864 precision = min (precision, -lim);
15865 }
15866 else if (lim > 0)
15867 {
15868 /* Padding specified. Don't let it be more than
15869 current maximum. */
15870 if (precision > 0)
15871 lim = min (precision, lim);
15872
15873 /* If that's more padding than already wanted, queue it.
15874 But don't reduce padding already specified even if
15875 that is beyond the current truncation point. */
15876 field_width = max (lim, field_width);
15877 }
15878 goto tail_recurse;
15879 }
15880 else if (STRINGP (car) || CONSP (car))
15881 {
15882 register int limit = 50;
15883 /* Limit is to protect against circular lists. */
15884 while (CONSP (elt)
15885 && --limit > 0
15886 && (precision <= 0 || n < precision))
15887 {
15888 n += display_mode_element (it, depth, field_width - n,
15889 precision - n, XCAR (elt),
15890 props, risky);
15891 elt = XCDR (elt);
15892 }
15893 }
15894 }
15895 break;
15896
15897 default:
15898 invalid:
15899 elt = build_string ("*invalid*");
15900 goto tail_recurse;
15901 }
15902
15903 /* Pad to FIELD_WIDTH. */
15904 if (field_width > 0 && n < field_width)
15905 {
15906 if (frame_title_ptr)
15907 n += store_frame_title ("", field_width - n, 0);
15908 else if (!NILP (mode_line_string_list))
15909 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15910 else
15911 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15912 0, 0, 0);
15913 }
15914
15915 return n;
15916 }
15917
15918 /* Store a mode-line string element in mode_line_string_list.
15919
15920 If STRING is non-null, display that C string. Otherwise, the Lisp
15921 string LISP_STRING is displayed.
15922
15923 FIELD_WIDTH is the minimum number of output glyphs to produce.
15924 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15925 with spaces. FIELD_WIDTH <= 0 means don't pad.
15926
15927 PRECISION is the maximum number of characters to output from
15928 STRING. PRECISION <= 0 means don't truncate the string.
15929
15930 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15931 properties to the string.
15932
15933 PROPS are the properties to add to the string.
15934 The mode_line_string_face face property is always added to the string.
15935 */
15936
15937 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15938 char *string;
15939 Lisp_Object lisp_string;
15940 int copy_string;
15941 int field_width;
15942 int precision;
15943 Lisp_Object props;
15944 {
15945 int len;
15946 int n = 0;
15947
15948 if (string != NULL)
15949 {
15950 len = strlen (string);
15951 if (precision > 0 && len > precision)
15952 len = precision;
15953 lisp_string = make_string (string, len);
15954 if (NILP (props))
15955 props = mode_line_string_face_prop;
15956 else if (!NILP (mode_line_string_face))
15957 {
15958 Lisp_Object face = Fplist_get (props, Qface);
15959 props = Fcopy_sequence (props);
15960 if (NILP (face))
15961 face = mode_line_string_face;
15962 else
15963 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15964 props = Fplist_put (props, Qface, face);
15965 }
15966 Fadd_text_properties (make_number (0), make_number (len),
15967 props, lisp_string);
15968 }
15969 else
15970 {
15971 len = XFASTINT (Flength (lisp_string));
15972 if (precision > 0 && len > precision)
15973 {
15974 len = precision;
15975 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15976 precision = -1;
15977 }
15978 if (!NILP (mode_line_string_face))
15979 {
15980 Lisp_Object face;
15981 if (NILP (props))
15982 props = Ftext_properties_at (make_number (0), lisp_string);
15983 face = Fplist_get (props, Qface);
15984 if (NILP (face))
15985 face = mode_line_string_face;
15986 else
15987 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15988 props = Fcons (Qface, Fcons (face, Qnil));
15989 if (copy_string)
15990 lisp_string = Fcopy_sequence (lisp_string);
15991 }
15992 if (!NILP (props))
15993 Fadd_text_properties (make_number (0), make_number (len),
15994 props, lisp_string);
15995 }
15996
15997 if (len > 0)
15998 {
15999 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16000 n += len;
16001 }
16002
16003 if (field_width > len)
16004 {
16005 field_width -= len;
16006 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16007 if (!NILP (props))
16008 Fadd_text_properties (make_number (0), make_number (field_width),
16009 props, lisp_string);
16010 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16011 n += field_width;
16012 }
16013
16014 return n;
16015 }
16016
16017
16018 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16019 0, 3, 0,
16020 doc: /* Return the mode-line of selected window as a string.
16021 First optional arg FORMAT specifies a different format string (see
16022 `mode-line-format' for details) to use. If FORMAT is t, return
16023 the buffer's header-line. Second optional arg WINDOW specifies a
16024 different window to use as the context for the formatting.
16025 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
16026 (format, window, no_props)
16027 Lisp_Object format, window, no_props;
16028 {
16029 struct it it;
16030 int len;
16031 struct window *w;
16032 struct buffer *old_buffer = NULL;
16033 enum face_id face_id = DEFAULT_FACE_ID;
16034
16035 if (NILP (window))
16036 window = selected_window;
16037 CHECK_WINDOW (window);
16038 w = XWINDOW (window);
16039 CHECK_BUFFER (w->buffer);
16040
16041 if (XBUFFER (w->buffer) != current_buffer)
16042 {
16043 old_buffer = current_buffer;
16044 set_buffer_internal_1 (XBUFFER (w->buffer));
16045 }
16046
16047 if (NILP (format) || EQ (format, Qt))
16048 {
16049 face_id = NILP (format)
16050 ? CURRENT_MODE_LINE_FACE_ID (w) :
16051 HEADER_LINE_FACE_ID;
16052 format = NILP (format)
16053 ? current_buffer->mode_line_format
16054 : current_buffer->header_line_format;
16055 }
16056
16057 init_iterator (&it, w, -1, -1, NULL, face_id);
16058
16059 if (NILP (no_props))
16060 {
16061 mode_line_string_face =
16062 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
16063 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
16064 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
16065
16066 mode_line_string_face_prop =
16067 NILP (mode_line_string_face) ? Qnil :
16068 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
16069
16070 /* We need a dummy last element in mode_line_string_list to
16071 indicate we are building the propertized mode-line string.
16072 Using mode_line_string_face_prop here GC protects it. */
16073 mode_line_string_list =
16074 Fcons (mode_line_string_face_prop, Qnil);
16075 frame_title_ptr = NULL;
16076 }
16077 else
16078 {
16079 mode_line_string_face_prop = Qnil;
16080 mode_line_string_list = Qnil;
16081 frame_title_ptr = frame_title_buf;
16082 }
16083
16084 push_frame_kboard (it.f);
16085 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16086 pop_frame_kboard ();
16087
16088 if (old_buffer)
16089 set_buffer_internal_1 (old_buffer);
16090
16091 if (NILP (no_props))
16092 {
16093 Lisp_Object str;
16094 mode_line_string_list = Fnreverse (mode_line_string_list);
16095 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16096 make_string ("", 0));
16097 mode_line_string_face_prop = Qnil;
16098 mode_line_string_list = Qnil;
16099 return str;
16100 }
16101
16102 len = frame_title_ptr - frame_title_buf;
16103 if (len > 0 && frame_title_ptr[-1] == '-')
16104 {
16105 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16106 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16107 ;
16108 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16109 if (len > frame_title_ptr - frame_title_buf)
16110 len = frame_title_ptr - frame_title_buf;
16111 }
16112
16113 frame_title_ptr = NULL;
16114 return make_string (frame_title_buf, len);
16115 }
16116
16117 /* Write a null-terminated, right justified decimal representation of
16118 the positive integer D to BUF using a minimal field width WIDTH. */
16119
16120 static void
16121 pint2str (buf, width, d)
16122 register char *buf;
16123 register int width;
16124 register int d;
16125 {
16126 register char *p = buf;
16127
16128 if (d <= 0)
16129 *p++ = '0';
16130 else
16131 {
16132 while (d > 0)
16133 {
16134 *p++ = d % 10 + '0';
16135 d /= 10;
16136 }
16137 }
16138
16139 for (width -= (int) (p - buf); width > 0; --width)
16140 *p++ = ' ';
16141 *p-- = '\0';
16142 while (p > buf)
16143 {
16144 d = *buf;
16145 *buf++ = *p;
16146 *p-- = d;
16147 }
16148 }
16149
16150 /* Write a null-terminated, right justified decimal and "human
16151 readable" representation of the nonnegative integer D to BUF using
16152 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16153
16154 static const char power_letter[] =
16155 {
16156 0, /* not used */
16157 'k', /* kilo */
16158 'M', /* mega */
16159 'G', /* giga */
16160 'T', /* tera */
16161 'P', /* peta */
16162 'E', /* exa */
16163 'Z', /* zetta */
16164 'Y' /* yotta */
16165 };
16166
16167 static void
16168 pint2hrstr (buf, width, d)
16169 char *buf;
16170 int width;
16171 int d;
16172 {
16173 /* We aim to represent the nonnegative integer D as
16174 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16175 int quotient = d;
16176 int remainder = 0;
16177 /* -1 means: do not use TENTHS. */
16178 int tenths = -1;
16179 int exponent = 0;
16180
16181 /* Length of QUOTIENT.TENTHS as a string. */
16182 int length;
16183
16184 char * psuffix;
16185 char * p;
16186
16187 if (1000 <= quotient)
16188 {
16189 /* Scale to the appropriate EXPONENT. */
16190 do
16191 {
16192 remainder = quotient % 1000;
16193 quotient /= 1000;
16194 exponent++;
16195 }
16196 while (1000 <= quotient);
16197
16198 /* Round to nearest and decide whether to use TENTHS or not. */
16199 if (quotient <= 9)
16200 {
16201 tenths = remainder / 100;
16202 if (50 <= remainder % 100)
16203 if (tenths < 9)
16204 tenths++;
16205 else
16206 {
16207 quotient++;
16208 if (quotient == 10)
16209 tenths = -1;
16210 else
16211 tenths = 0;
16212 }
16213 }
16214 else
16215 if (500 <= remainder)
16216 if (quotient < 999)
16217 quotient++;
16218 else
16219 {
16220 quotient = 1;
16221 exponent++;
16222 tenths = 0;
16223 }
16224 }
16225
16226 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16227 if (tenths == -1 && quotient <= 99)
16228 if (quotient <= 9)
16229 length = 1;
16230 else
16231 length = 2;
16232 else
16233 length = 3;
16234 p = psuffix = buf + max (width, length);
16235
16236 /* Print EXPONENT. */
16237 if (exponent)
16238 *psuffix++ = power_letter[exponent];
16239 *psuffix = '\0';
16240
16241 /* Print TENTHS. */
16242 if (tenths >= 0)
16243 {
16244 *--p = '0' + tenths;
16245 *--p = '.';
16246 }
16247
16248 /* Print QUOTIENT. */
16249 do
16250 {
16251 int digit = quotient % 10;
16252 *--p = '0' + digit;
16253 }
16254 while ((quotient /= 10) != 0);
16255
16256 /* Print leading spaces. */
16257 while (buf < p)
16258 *--p = ' ';
16259 }
16260
16261 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16262 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16263 type of CODING_SYSTEM. Return updated pointer into BUF. */
16264
16265 static unsigned char invalid_eol_type[] = "(*invalid*)";
16266
16267 static char *
16268 decode_mode_spec_coding (coding_system, buf, eol_flag)
16269 Lisp_Object coding_system;
16270 register char *buf;
16271 int eol_flag;
16272 {
16273 Lisp_Object val;
16274 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16275 const unsigned char *eol_str;
16276 int eol_str_len;
16277 /* The EOL conversion we are using. */
16278 Lisp_Object eoltype;
16279
16280 val = Fget (coding_system, Qcoding_system);
16281 eoltype = Qnil;
16282
16283 if (!VECTORP (val)) /* Not yet decided. */
16284 {
16285 if (multibyte)
16286 *buf++ = '-';
16287 if (eol_flag)
16288 eoltype = eol_mnemonic_undecided;
16289 /* Don't mention EOL conversion if it isn't decided. */
16290 }
16291 else
16292 {
16293 Lisp_Object eolvalue;
16294
16295 eolvalue = Fget (coding_system, Qeol_type);
16296
16297 if (multibyte)
16298 *buf++ = XFASTINT (AREF (val, 1));
16299
16300 if (eol_flag)
16301 {
16302 /* The EOL conversion that is normal on this system. */
16303
16304 if (NILP (eolvalue)) /* Not yet decided. */
16305 eoltype = eol_mnemonic_undecided;
16306 else if (VECTORP (eolvalue)) /* Not yet decided. */
16307 eoltype = eol_mnemonic_undecided;
16308 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16309 eoltype = (XFASTINT (eolvalue) == 0
16310 ? eol_mnemonic_unix
16311 : (XFASTINT (eolvalue) == 1
16312 ? eol_mnemonic_dos : eol_mnemonic_mac));
16313 }
16314 }
16315
16316 if (eol_flag)
16317 {
16318 /* Mention the EOL conversion if it is not the usual one. */
16319 if (STRINGP (eoltype))
16320 {
16321 eol_str = SDATA (eoltype);
16322 eol_str_len = SBYTES (eoltype);
16323 }
16324 else if (INTEGERP (eoltype)
16325 && CHAR_VALID_P (XINT (eoltype), 0))
16326 {
16327 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16328 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16329 eol_str = tmp;
16330 }
16331 else
16332 {
16333 eol_str = invalid_eol_type;
16334 eol_str_len = sizeof (invalid_eol_type) - 1;
16335 }
16336 bcopy (eol_str, buf, eol_str_len);
16337 buf += eol_str_len;
16338 }
16339
16340 return buf;
16341 }
16342
16343 /* Return a string for the output of a mode line %-spec for window W,
16344 generated by character C. PRECISION >= 0 means don't return a
16345 string longer than that value. FIELD_WIDTH > 0 means pad the
16346 string returned with spaces to that value. Return 1 in *MULTIBYTE
16347 if the result is multibyte text. */
16348
16349 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16350
16351 static char *
16352 decode_mode_spec (w, c, field_width, precision, multibyte)
16353 struct window *w;
16354 register int c;
16355 int field_width, precision;
16356 int *multibyte;
16357 {
16358 Lisp_Object obj;
16359 struct frame *f = XFRAME (WINDOW_FRAME (w));
16360 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16361 struct buffer *b = XBUFFER (w->buffer);
16362
16363 obj = Qnil;
16364 *multibyte = 0;
16365
16366 switch (c)
16367 {
16368 case '*':
16369 if (!NILP (b->read_only))
16370 return "%";
16371 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16372 return "*";
16373 return "-";
16374
16375 case '+':
16376 /* This differs from %* only for a modified read-only buffer. */
16377 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16378 return "*";
16379 if (!NILP (b->read_only))
16380 return "%";
16381 return "-";
16382
16383 case '&':
16384 /* This differs from %* in ignoring read-only-ness. */
16385 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16386 return "*";
16387 return "-";
16388
16389 case '%':
16390 return "%";
16391
16392 case '[':
16393 {
16394 int i;
16395 char *p;
16396
16397 if (command_loop_level > 5)
16398 return "[[[... ";
16399 p = decode_mode_spec_buf;
16400 for (i = 0; i < command_loop_level; i++)
16401 *p++ = '[';
16402 *p = 0;
16403 return decode_mode_spec_buf;
16404 }
16405
16406 case ']':
16407 {
16408 int i;
16409 char *p;
16410
16411 if (command_loop_level > 5)
16412 return " ...]]]";
16413 p = decode_mode_spec_buf;
16414 for (i = 0; i < command_loop_level; i++)
16415 *p++ = ']';
16416 *p = 0;
16417 return decode_mode_spec_buf;
16418 }
16419
16420 case '-':
16421 {
16422 register int i;
16423
16424 /* Let lots_of_dashes be a string of infinite length. */
16425 if (!NILP (mode_line_string_list))
16426 return "--";
16427 if (field_width <= 0
16428 || field_width > sizeof (lots_of_dashes))
16429 {
16430 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16431 decode_mode_spec_buf[i] = '-';
16432 decode_mode_spec_buf[i] = '\0';
16433 return decode_mode_spec_buf;
16434 }
16435 else
16436 return lots_of_dashes;
16437 }
16438
16439 case 'b':
16440 obj = b->name;
16441 break;
16442
16443 case 'c':
16444 {
16445 int col = (int) current_column (); /* iftc */
16446 w->column_number_displayed = make_number (col);
16447 pint2str (decode_mode_spec_buf, field_width, col);
16448 return decode_mode_spec_buf;
16449 }
16450
16451 case 'F':
16452 /* %F displays the frame name. */
16453 if (!NILP (f->title))
16454 return (char *) SDATA (f->title);
16455 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16456 return (char *) SDATA (f->name);
16457 return "Emacs";
16458
16459 case 'f':
16460 obj = b->filename;
16461 break;
16462
16463 case 'i':
16464 {
16465 int size = ZV - BEGV;
16466 pint2str (decode_mode_spec_buf, field_width, size);
16467 return decode_mode_spec_buf;
16468 }
16469
16470 case 'I':
16471 {
16472 int size = ZV - BEGV;
16473 pint2hrstr (decode_mode_spec_buf, field_width, size);
16474 return decode_mode_spec_buf;
16475 }
16476
16477 case 'l':
16478 {
16479 int startpos = XMARKER (w->start)->charpos;
16480 int startpos_byte = marker_byte_position (w->start);
16481 int line, linepos, linepos_byte, topline;
16482 int nlines, junk;
16483 int height = WINDOW_TOTAL_LINES (w);
16484
16485 /* If we decided that this buffer isn't suitable for line numbers,
16486 don't forget that too fast. */
16487 if (EQ (w->base_line_pos, w->buffer))
16488 goto no_value;
16489 /* But do forget it, if the window shows a different buffer now. */
16490 else if (BUFFERP (w->base_line_pos))
16491 w->base_line_pos = Qnil;
16492
16493 /* If the buffer is very big, don't waste time. */
16494 if (INTEGERP (Vline_number_display_limit)
16495 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16496 {
16497 w->base_line_pos = Qnil;
16498 w->base_line_number = Qnil;
16499 goto no_value;
16500 }
16501
16502 if (!NILP (w->base_line_number)
16503 && !NILP (w->base_line_pos)
16504 && XFASTINT (w->base_line_pos) <= startpos)
16505 {
16506 line = XFASTINT (w->base_line_number);
16507 linepos = XFASTINT (w->base_line_pos);
16508 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16509 }
16510 else
16511 {
16512 line = 1;
16513 linepos = BUF_BEGV (b);
16514 linepos_byte = BUF_BEGV_BYTE (b);
16515 }
16516
16517 /* Count lines from base line to window start position. */
16518 nlines = display_count_lines (linepos, linepos_byte,
16519 startpos_byte,
16520 startpos, &junk);
16521
16522 topline = nlines + line;
16523
16524 /* Determine a new base line, if the old one is too close
16525 or too far away, or if we did not have one.
16526 "Too close" means it's plausible a scroll-down would
16527 go back past it. */
16528 if (startpos == BUF_BEGV (b))
16529 {
16530 w->base_line_number = make_number (topline);
16531 w->base_line_pos = make_number (BUF_BEGV (b));
16532 }
16533 else if (nlines < height + 25 || nlines > height * 3 + 50
16534 || linepos == BUF_BEGV (b))
16535 {
16536 int limit = BUF_BEGV (b);
16537 int limit_byte = BUF_BEGV_BYTE (b);
16538 int position;
16539 int distance = (height * 2 + 30) * line_number_display_limit_width;
16540
16541 if (startpos - distance > limit)
16542 {
16543 limit = startpos - distance;
16544 limit_byte = CHAR_TO_BYTE (limit);
16545 }
16546
16547 nlines = display_count_lines (startpos, startpos_byte,
16548 limit_byte,
16549 - (height * 2 + 30),
16550 &position);
16551 /* If we couldn't find the lines we wanted within
16552 line_number_display_limit_width chars per line,
16553 give up on line numbers for this window. */
16554 if (position == limit_byte && limit == startpos - distance)
16555 {
16556 w->base_line_pos = w->buffer;
16557 w->base_line_number = Qnil;
16558 goto no_value;
16559 }
16560
16561 w->base_line_number = make_number (topline - nlines);
16562 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16563 }
16564
16565 /* Now count lines from the start pos to point. */
16566 nlines = display_count_lines (startpos, startpos_byte,
16567 PT_BYTE, PT, &junk);
16568
16569 /* Record that we did display the line number. */
16570 line_number_displayed = 1;
16571
16572 /* Make the string to show. */
16573 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16574 return decode_mode_spec_buf;
16575 no_value:
16576 {
16577 char* p = decode_mode_spec_buf;
16578 int pad = field_width - 2;
16579 while (pad-- > 0)
16580 *p++ = ' ';
16581 *p++ = '?';
16582 *p++ = '?';
16583 *p = '\0';
16584 return decode_mode_spec_buf;
16585 }
16586 }
16587 break;
16588
16589 case 'm':
16590 obj = b->mode_name;
16591 break;
16592
16593 case 'n':
16594 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16595 return " Narrow";
16596 break;
16597
16598 case 'p':
16599 {
16600 int pos = marker_position (w->start);
16601 int total = BUF_ZV (b) - BUF_BEGV (b);
16602
16603 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16604 {
16605 if (pos <= BUF_BEGV (b))
16606 return "All";
16607 else
16608 return "Bottom";
16609 }
16610 else if (pos <= BUF_BEGV (b))
16611 return "Top";
16612 else
16613 {
16614 if (total > 1000000)
16615 /* Do it differently for a large value, to avoid overflow. */
16616 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16617 else
16618 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16619 /* We can't normally display a 3-digit number,
16620 so get us a 2-digit number that is close. */
16621 if (total == 100)
16622 total = 99;
16623 sprintf (decode_mode_spec_buf, "%2d%%", total);
16624 return decode_mode_spec_buf;
16625 }
16626 }
16627
16628 /* Display percentage of size above the bottom of the screen. */
16629 case 'P':
16630 {
16631 int toppos = marker_position (w->start);
16632 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16633 int total = BUF_ZV (b) - BUF_BEGV (b);
16634
16635 if (botpos >= BUF_ZV (b))
16636 {
16637 if (toppos <= BUF_BEGV (b))
16638 return "All";
16639 else
16640 return "Bottom";
16641 }
16642 else
16643 {
16644 if (total > 1000000)
16645 /* Do it differently for a large value, to avoid overflow. */
16646 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16647 else
16648 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16649 /* We can't normally display a 3-digit number,
16650 so get us a 2-digit number that is close. */
16651 if (total == 100)
16652 total = 99;
16653 if (toppos <= BUF_BEGV (b))
16654 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16655 else
16656 sprintf (decode_mode_spec_buf, "%2d%%", total);
16657 return decode_mode_spec_buf;
16658 }
16659 }
16660
16661 case 's':
16662 /* status of process */
16663 obj = Fget_buffer_process (w->buffer);
16664 if (NILP (obj))
16665 return "no process";
16666 #ifdef subprocesses
16667 obj = Fsymbol_name (Fprocess_status (obj));
16668 #endif
16669 break;
16670
16671 case 't': /* indicate TEXT or BINARY */
16672 #ifdef MODE_LINE_BINARY_TEXT
16673 return MODE_LINE_BINARY_TEXT (b);
16674 #else
16675 return "T";
16676 #endif
16677
16678 case 'z':
16679 /* coding-system (not including end-of-line format) */
16680 case 'Z':
16681 /* coding-system (including end-of-line type) */
16682 {
16683 int eol_flag = (c == 'Z');
16684 char *p = decode_mode_spec_buf;
16685
16686 if (! FRAME_WINDOW_P (f))
16687 {
16688 /* No need to mention EOL here--the terminal never needs
16689 to do EOL conversion. */
16690 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16691 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16692 }
16693 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16694 p, eol_flag);
16695
16696 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16697 #ifdef subprocesses
16698 obj = Fget_buffer_process (Fcurrent_buffer ());
16699 if (PROCESSP (obj))
16700 {
16701 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16702 p, eol_flag);
16703 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16704 p, eol_flag);
16705 }
16706 #endif /* subprocesses */
16707 #endif /* 0 */
16708 *p = 0;
16709 return decode_mode_spec_buf;
16710 }
16711 }
16712
16713 if (STRINGP (obj))
16714 {
16715 *multibyte = STRING_MULTIBYTE (obj);
16716 return (char *) SDATA (obj);
16717 }
16718 else
16719 return "";
16720 }
16721
16722
16723 /* Count up to COUNT lines starting from START / START_BYTE.
16724 But don't go beyond LIMIT_BYTE.
16725 Return the number of lines thus found (always nonnegative).
16726
16727 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16728
16729 static int
16730 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16731 int start, start_byte, limit_byte, count;
16732 int *byte_pos_ptr;
16733 {
16734 register unsigned char *cursor;
16735 unsigned char *base;
16736
16737 register int ceiling;
16738 register unsigned char *ceiling_addr;
16739 int orig_count = count;
16740
16741 /* If we are not in selective display mode,
16742 check only for newlines. */
16743 int selective_display = (!NILP (current_buffer->selective_display)
16744 && !INTEGERP (current_buffer->selective_display));
16745
16746 if (count > 0)
16747 {
16748 while (start_byte < limit_byte)
16749 {
16750 ceiling = BUFFER_CEILING_OF (start_byte);
16751 ceiling = min (limit_byte - 1, ceiling);
16752 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16753 base = (cursor = BYTE_POS_ADDR (start_byte));
16754 while (1)
16755 {
16756 if (selective_display)
16757 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16758 ;
16759 else
16760 while (*cursor != '\n' && ++cursor != ceiling_addr)
16761 ;
16762
16763 if (cursor != ceiling_addr)
16764 {
16765 if (--count == 0)
16766 {
16767 start_byte += cursor - base + 1;
16768 *byte_pos_ptr = start_byte;
16769 return orig_count;
16770 }
16771 else
16772 if (++cursor == ceiling_addr)
16773 break;
16774 }
16775 else
16776 break;
16777 }
16778 start_byte += cursor - base;
16779 }
16780 }
16781 else
16782 {
16783 while (start_byte > limit_byte)
16784 {
16785 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16786 ceiling = max (limit_byte, ceiling);
16787 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16788 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16789 while (1)
16790 {
16791 if (selective_display)
16792 while (--cursor != ceiling_addr
16793 && *cursor != '\n' && *cursor != 015)
16794 ;
16795 else
16796 while (--cursor != ceiling_addr && *cursor != '\n')
16797 ;
16798
16799 if (cursor != ceiling_addr)
16800 {
16801 if (++count == 0)
16802 {
16803 start_byte += cursor - base + 1;
16804 *byte_pos_ptr = start_byte;
16805 /* When scanning backwards, we should
16806 not count the newline posterior to which we stop. */
16807 return - orig_count - 1;
16808 }
16809 }
16810 else
16811 break;
16812 }
16813 /* Here we add 1 to compensate for the last decrement
16814 of CURSOR, which took it past the valid range. */
16815 start_byte += cursor - base + 1;
16816 }
16817 }
16818
16819 *byte_pos_ptr = limit_byte;
16820
16821 if (count < 0)
16822 return - orig_count + count;
16823 return orig_count - count;
16824
16825 }
16826
16827
16828 \f
16829 /***********************************************************************
16830 Displaying strings
16831 ***********************************************************************/
16832
16833 /* Display a NUL-terminated string, starting with index START.
16834
16835 If STRING is non-null, display that C string. Otherwise, the Lisp
16836 string LISP_STRING is displayed.
16837
16838 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16839 FACE_STRING. Display STRING or LISP_STRING with the face at
16840 FACE_STRING_POS in FACE_STRING:
16841
16842 Display the string in the environment given by IT, but use the
16843 standard display table, temporarily.
16844
16845 FIELD_WIDTH is the minimum number of output glyphs to produce.
16846 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16847 with spaces. If STRING has more characters, more than FIELD_WIDTH
16848 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16849
16850 PRECISION is the maximum number of characters to output from
16851 STRING. PRECISION < 0 means don't truncate the string.
16852
16853 This is roughly equivalent to printf format specifiers:
16854
16855 FIELD_WIDTH PRECISION PRINTF
16856 ----------------------------------------
16857 -1 -1 %s
16858 -1 10 %.10s
16859 10 -1 %10s
16860 20 10 %20.10s
16861
16862 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16863 display them, and < 0 means obey the current buffer's value of
16864 enable_multibyte_characters.
16865
16866 Value is the number of glyphs produced. */
16867
16868 static int
16869 display_string (string, lisp_string, face_string, face_string_pos,
16870 start, it, field_width, precision, max_x, multibyte)
16871 unsigned char *string;
16872 Lisp_Object lisp_string;
16873 Lisp_Object face_string;
16874 int face_string_pos;
16875 int start;
16876 struct it *it;
16877 int field_width, precision, max_x;
16878 int multibyte;
16879 {
16880 int hpos_at_start = it->hpos;
16881 int saved_face_id = it->face_id;
16882 struct glyph_row *row = it->glyph_row;
16883
16884 /* Initialize the iterator IT for iteration over STRING beginning
16885 with index START. */
16886 reseat_to_string (it, string, lisp_string, start,
16887 precision, field_width, multibyte);
16888
16889 /* If displaying STRING, set up the face of the iterator
16890 from LISP_STRING, if that's given. */
16891 if (STRINGP (face_string))
16892 {
16893 int endptr;
16894 struct face *face;
16895
16896 it->face_id
16897 = face_at_string_position (it->w, face_string, face_string_pos,
16898 0, it->region_beg_charpos,
16899 it->region_end_charpos,
16900 &endptr, it->base_face_id, 0);
16901 face = FACE_FROM_ID (it->f, it->face_id);
16902 it->face_box_p = face->box != FACE_NO_BOX;
16903 }
16904
16905 /* Set max_x to the maximum allowed X position. Don't let it go
16906 beyond the right edge of the window. */
16907 if (max_x <= 0)
16908 max_x = it->last_visible_x;
16909 else
16910 max_x = min (max_x, it->last_visible_x);
16911
16912 /* Skip over display elements that are not visible. because IT->w is
16913 hscrolled. */
16914 if (it->current_x < it->first_visible_x)
16915 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16916 MOVE_TO_POS | MOVE_TO_X);
16917
16918 row->ascent = it->max_ascent;
16919 row->height = it->max_ascent + it->max_descent;
16920 row->phys_ascent = it->max_phys_ascent;
16921 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16922
16923 /* This condition is for the case that we are called with current_x
16924 past last_visible_x. */
16925 while (it->current_x < max_x)
16926 {
16927 int x_before, x, n_glyphs_before, i, nglyphs;
16928
16929 /* Get the next display element. */
16930 if (!get_next_display_element (it))
16931 break;
16932
16933 /* Produce glyphs. */
16934 x_before = it->current_x;
16935 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16936 PRODUCE_GLYPHS (it);
16937
16938 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16939 i = 0;
16940 x = x_before;
16941 while (i < nglyphs)
16942 {
16943 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16944
16945 if (!it->truncate_lines_p
16946 && x + glyph->pixel_width > max_x)
16947 {
16948 /* End of continued line or max_x reached. */
16949 if (CHAR_GLYPH_PADDING_P (*glyph))
16950 {
16951 /* A wide character is unbreakable. */
16952 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16953 it->current_x = x_before;
16954 }
16955 else
16956 {
16957 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16958 it->current_x = x;
16959 }
16960 break;
16961 }
16962 else if (x + glyph->pixel_width > it->first_visible_x)
16963 {
16964 /* Glyph is at least partially visible. */
16965 ++it->hpos;
16966 if (x < it->first_visible_x)
16967 it->glyph_row->x = x - it->first_visible_x;
16968 }
16969 else
16970 {
16971 /* Glyph is off the left margin of the display area.
16972 Should not happen. */
16973 abort ();
16974 }
16975
16976 row->ascent = max (row->ascent, it->max_ascent);
16977 row->height = max (row->height, it->max_ascent + it->max_descent);
16978 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16979 row->phys_height = max (row->phys_height,
16980 it->max_phys_ascent + it->max_phys_descent);
16981 x += glyph->pixel_width;
16982 ++i;
16983 }
16984
16985 /* Stop if max_x reached. */
16986 if (i < nglyphs)
16987 break;
16988
16989 /* Stop at line ends. */
16990 if (ITERATOR_AT_END_OF_LINE_P (it))
16991 {
16992 it->continuation_lines_width = 0;
16993 break;
16994 }
16995
16996 set_iterator_to_next (it, 1);
16997
16998 /* Stop if truncating at the right edge. */
16999 if (it->truncate_lines_p
17000 && it->current_x >= it->last_visible_x)
17001 {
17002 /* Add truncation mark, but don't do it if the line is
17003 truncated at a padding space. */
17004 if (IT_CHARPOS (*it) < it->string_nchars)
17005 {
17006 if (!FRAME_WINDOW_P (it->f))
17007 {
17008 int i, n;
17009
17010 if (it->current_x > it->last_visible_x)
17011 {
17012 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17013 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17014 break;
17015 for (n = row->used[TEXT_AREA]; i < n; ++i)
17016 {
17017 row->used[TEXT_AREA] = i;
17018 produce_special_glyphs (it, IT_TRUNCATION);
17019 }
17020 }
17021 produce_special_glyphs (it, IT_TRUNCATION);
17022 }
17023 it->glyph_row->truncated_on_right_p = 1;
17024 }
17025 break;
17026 }
17027 }
17028
17029 /* Maybe insert a truncation at the left. */
17030 if (it->first_visible_x
17031 && IT_CHARPOS (*it) > 0)
17032 {
17033 if (!FRAME_WINDOW_P (it->f))
17034 insert_left_trunc_glyphs (it);
17035 it->glyph_row->truncated_on_left_p = 1;
17036 }
17037
17038 it->face_id = saved_face_id;
17039
17040 /* Value is number of columns displayed. */
17041 return it->hpos - hpos_at_start;
17042 }
17043
17044
17045 \f
17046 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17047 appears as an element of LIST or as the car of an element of LIST.
17048 If PROPVAL is a list, compare each element against LIST in that
17049 way, and return 1/2 if any element of PROPVAL is found in LIST.
17050 Otherwise return 0. This function cannot quit.
17051 The return value is 2 if the text is invisible but with an ellipsis
17052 and 1 if it's invisible and without an ellipsis. */
17053
17054 int
17055 invisible_p (propval, list)
17056 register Lisp_Object propval;
17057 Lisp_Object list;
17058 {
17059 register Lisp_Object tail, proptail;
17060
17061 for (tail = list; CONSP (tail); tail = XCDR (tail))
17062 {
17063 register Lisp_Object tem;
17064 tem = XCAR (tail);
17065 if (EQ (propval, tem))
17066 return 1;
17067 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17068 return NILP (XCDR (tem)) ? 1 : 2;
17069 }
17070
17071 if (CONSP (propval))
17072 {
17073 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17074 {
17075 Lisp_Object propelt;
17076 propelt = XCAR (proptail);
17077 for (tail = list; CONSP (tail); tail = XCDR (tail))
17078 {
17079 register Lisp_Object tem;
17080 tem = XCAR (tail);
17081 if (EQ (propelt, tem))
17082 return 1;
17083 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17084 return NILP (XCDR (tem)) ? 1 : 2;
17085 }
17086 }
17087 }
17088
17089 return 0;
17090 }
17091
17092 \f
17093 /***********************************************************************
17094 Glyph Display
17095 ***********************************************************************/
17096
17097 #ifdef HAVE_WINDOW_SYSTEM
17098
17099 #if GLYPH_DEBUG
17100
17101 void
17102 dump_glyph_string (s)
17103 struct glyph_string *s;
17104 {
17105 fprintf (stderr, "glyph string\n");
17106 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17107 s->x, s->y, s->width, s->height);
17108 fprintf (stderr, " ybase = %d\n", s->ybase);
17109 fprintf (stderr, " hl = %d\n", s->hl);
17110 fprintf (stderr, " left overhang = %d, right = %d\n",
17111 s->left_overhang, s->right_overhang);
17112 fprintf (stderr, " nchars = %d\n", s->nchars);
17113 fprintf (stderr, " extends to end of line = %d\n",
17114 s->extends_to_end_of_line_p);
17115 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17116 fprintf (stderr, " bg width = %d\n", s->background_width);
17117 }
17118
17119 #endif /* GLYPH_DEBUG */
17120
17121 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17122 of XChar2b structures for S; it can't be allocated in
17123 init_glyph_string because it must be allocated via `alloca'. W
17124 is the window on which S is drawn. ROW and AREA are the glyph row
17125 and area within the row from which S is constructed. START is the
17126 index of the first glyph structure covered by S. HL is a
17127 face-override for drawing S. */
17128
17129 #ifdef HAVE_NTGUI
17130 #define OPTIONAL_HDC(hdc) hdc,
17131 #define DECLARE_HDC(hdc) HDC hdc;
17132 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17133 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17134 #endif
17135
17136 #ifndef OPTIONAL_HDC
17137 #define OPTIONAL_HDC(hdc)
17138 #define DECLARE_HDC(hdc)
17139 #define ALLOCATE_HDC(hdc, f)
17140 #define RELEASE_HDC(hdc, f)
17141 #endif
17142
17143 static void
17144 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17145 struct glyph_string *s;
17146 DECLARE_HDC (hdc)
17147 XChar2b *char2b;
17148 struct window *w;
17149 struct glyph_row *row;
17150 enum glyph_row_area area;
17151 int start;
17152 enum draw_glyphs_face hl;
17153 {
17154 bzero (s, sizeof *s);
17155 s->w = w;
17156 s->f = XFRAME (w->frame);
17157 #ifdef HAVE_NTGUI
17158 s->hdc = hdc;
17159 #endif
17160 s->display = FRAME_X_DISPLAY (s->f);
17161 s->window = FRAME_X_WINDOW (s->f);
17162 s->char2b = char2b;
17163 s->hl = hl;
17164 s->row = row;
17165 s->area = area;
17166 s->first_glyph = row->glyphs[area] + start;
17167 s->height = row->height;
17168 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17169
17170 /* Display the internal border below the tool-bar window. */
17171 if (s->w == XWINDOW (s->f->tool_bar_window))
17172 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17173
17174 s->ybase = s->y + row->ascent;
17175 }
17176
17177
17178 /* Append the list of glyph strings with head H and tail T to the list
17179 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17180
17181 static INLINE void
17182 append_glyph_string_lists (head, tail, h, t)
17183 struct glyph_string **head, **tail;
17184 struct glyph_string *h, *t;
17185 {
17186 if (h)
17187 {
17188 if (*head)
17189 (*tail)->next = h;
17190 else
17191 *head = h;
17192 h->prev = *tail;
17193 *tail = t;
17194 }
17195 }
17196
17197
17198 /* Prepend the list of glyph strings with head H and tail T to the
17199 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17200 result. */
17201
17202 static INLINE void
17203 prepend_glyph_string_lists (head, tail, h, t)
17204 struct glyph_string **head, **tail;
17205 struct glyph_string *h, *t;
17206 {
17207 if (h)
17208 {
17209 if (*head)
17210 (*head)->prev = t;
17211 else
17212 *tail = t;
17213 t->next = *head;
17214 *head = h;
17215 }
17216 }
17217
17218
17219 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17220 Set *HEAD and *TAIL to the resulting list. */
17221
17222 static INLINE void
17223 append_glyph_string (head, tail, s)
17224 struct glyph_string **head, **tail;
17225 struct glyph_string *s;
17226 {
17227 s->next = s->prev = NULL;
17228 append_glyph_string_lists (head, tail, s, s);
17229 }
17230
17231
17232 /* Get face and two-byte form of character glyph GLYPH on frame F.
17233 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17234 a pointer to a realized face that is ready for display. */
17235
17236 static INLINE struct face *
17237 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17238 struct frame *f;
17239 struct glyph *glyph;
17240 XChar2b *char2b;
17241 int *two_byte_p;
17242 {
17243 struct face *face;
17244
17245 xassert (glyph->type == CHAR_GLYPH);
17246 face = FACE_FROM_ID (f, glyph->face_id);
17247
17248 if (two_byte_p)
17249 *two_byte_p = 0;
17250
17251 if (!glyph->multibyte_p)
17252 {
17253 /* Unibyte case. We don't have to encode, but we have to make
17254 sure to use a face suitable for unibyte. */
17255 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17256 }
17257 else if (glyph->u.ch < 128
17258 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17259 {
17260 /* Case of ASCII in a face known to fit ASCII. */
17261 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17262 }
17263 else
17264 {
17265 int c1, c2, charset;
17266
17267 /* Split characters into bytes. If c2 is -1 afterwards, C is
17268 really a one-byte character so that byte1 is zero. */
17269 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17270 if (c2 > 0)
17271 STORE_XCHAR2B (char2b, c1, c2);
17272 else
17273 STORE_XCHAR2B (char2b, 0, c1);
17274
17275 /* Maybe encode the character in *CHAR2B. */
17276 if (charset != CHARSET_ASCII)
17277 {
17278 struct font_info *font_info
17279 = FONT_INFO_FROM_ID (f, face->font_info_id);
17280 if (font_info)
17281 glyph->font_type
17282 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17283 }
17284 }
17285
17286 /* Make sure X resources of the face are allocated. */
17287 xassert (face != NULL);
17288 PREPARE_FACE_FOR_DISPLAY (f, face);
17289 return face;
17290 }
17291
17292
17293 /* Fill glyph string S with composition components specified by S->cmp.
17294
17295 FACES is an array of faces for all components of this composition.
17296 S->gidx is the index of the first component for S.
17297 OVERLAPS_P non-zero means S should draw the foreground only, and
17298 use its physical height for clipping.
17299
17300 Value is the index of a component not in S. */
17301
17302 static int
17303 fill_composite_glyph_string (s, faces, overlaps_p)
17304 struct glyph_string *s;
17305 struct face **faces;
17306 int overlaps_p;
17307 {
17308 int i;
17309
17310 xassert (s);
17311
17312 s->for_overlaps_p = overlaps_p;
17313
17314 s->face = faces[s->gidx];
17315 s->font = s->face->font;
17316 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17317
17318 /* For all glyphs of this composition, starting at the offset
17319 S->gidx, until we reach the end of the definition or encounter a
17320 glyph that requires the different face, add it to S. */
17321 ++s->nchars;
17322 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17323 ++s->nchars;
17324
17325 /* All glyph strings for the same composition has the same width,
17326 i.e. the width set for the first component of the composition. */
17327
17328 s->width = s->first_glyph->pixel_width;
17329
17330 /* If the specified font could not be loaded, use the frame's
17331 default font, but record the fact that we couldn't load it in
17332 the glyph string so that we can draw rectangles for the
17333 characters of the glyph string. */
17334 if (s->font == NULL)
17335 {
17336 s->font_not_found_p = 1;
17337 s->font = FRAME_FONT (s->f);
17338 }
17339
17340 /* Adjust base line for subscript/superscript text. */
17341 s->ybase += s->first_glyph->voffset;
17342
17343 xassert (s->face && s->face->gc);
17344
17345 /* This glyph string must always be drawn with 16-bit functions. */
17346 s->two_byte_p = 1;
17347
17348 return s->gidx + s->nchars;
17349 }
17350
17351
17352 /* Fill glyph string S from a sequence of character glyphs.
17353
17354 FACE_ID is the face id of the string. START is the index of the
17355 first glyph to consider, END is the index of the last + 1.
17356 OVERLAPS_P non-zero means S should draw the foreground only, and
17357 use its physical height for clipping.
17358
17359 Value is the index of the first glyph not in S. */
17360
17361 static int
17362 fill_glyph_string (s, face_id, start, end, overlaps_p)
17363 struct glyph_string *s;
17364 int face_id;
17365 int start, end, overlaps_p;
17366 {
17367 struct glyph *glyph, *last;
17368 int voffset;
17369 int glyph_not_available_p;
17370
17371 xassert (s->f == XFRAME (s->w->frame));
17372 xassert (s->nchars == 0);
17373 xassert (start >= 0 && end > start);
17374
17375 s->for_overlaps_p = overlaps_p,
17376 glyph = s->row->glyphs[s->area] + start;
17377 last = s->row->glyphs[s->area] + end;
17378 voffset = glyph->voffset;
17379
17380 glyph_not_available_p = glyph->glyph_not_available_p;
17381
17382 while (glyph < last
17383 && glyph->type == CHAR_GLYPH
17384 && glyph->voffset == voffset
17385 /* Same face id implies same font, nowadays. */
17386 && glyph->face_id == face_id
17387 && glyph->glyph_not_available_p == glyph_not_available_p)
17388 {
17389 int two_byte_p;
17390
17391 s->face = get_glyph_face_and_encoding (s->f, glyph,
17392 s->char2b + s->nchars,
17393 &two_byte_p);
17394 s->two_byte_p = two_byte_p;
17395 ++s->nchars;
17396 xassert (s->nchars <= end - start);
17397 s->width += glyph->pixel_width;
17398 ++glyph;
17399 }
17400
17401 s->font = s->face->font;
17402 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17403
17404 /* If the specified font could not be loaded, use the frame's font,
17405 but record the fact that we couldn't load it in
17406 S->font_not_found_p so that we can draw rectangles for the
17407 characters of the glyph string. */
17408 if (s->font == NULL || glyph_not_available_p)
17409 {
17410 s->font_not_found_p = 1;
17411 s->font = FRAME_FONT (s->f);
17412 }
17413
17414 /* Adjust base line for subscript/superscript text. */
17415 s->ybase += voffset;
17416
17417 xassert (s->face && s->face->gc);
17418 return glyph - s->row->glyphs[s->area];
17419 }
17420
17421
17422 /* Fill glyph string S from image glyph S->first_glyph. */
17423
17424 static void
17425 fill_image_glyph_string (s)
17426 struct glyph_string *s;
17427 {
17428 xassert (s->first_glyph->type == IMAGE_GLYPH);
17429 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17430 xassert (s->img);
17431 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17432 s->font = s->face->font;
17433 s->width = s->first_glyph->pixel_width;
17434
17435 /* Adjust base line for subscript/superscript text. */
17436 s->ybase += s->first_glyph->voffset;
17437 }
17438
17439
17440 /* Fill glyph string S from a sequence of stretch glyphs.
17441
17442 ROW is the glyph row in which the glyphs are found, AREA is the
17443 area within the row. START is the index of the first glyph to
17444 consider, END is the index of the last + 1.
17445
17446 Value is the index of the first glyph not in S. */
17447
17448 static int
17449 fill_stretch_glyph_string (s, row, area, start, end)
17450 struct glyph_string *s;
17451 struct glyph_row *row;
17452 enum glyph_row_area area;
17453 int start, end;
17454 {
17455 struct glyph *glyph, *last;
17456 int voffset, face_id;
17457
17458 xassert (s->first_glyph->type == STRETCH_GLYPH);
17459
17460 glyph = s->row->glyphs[s->area] + start;
17461 last = s->row->glyphs[s->area] + end;
17462 face_id = glyph->face_id;
17463 s->face = FACE_FROM_ID (s->f, face_id);
17464 s->font = s->face->font;
17465 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17466 s->width = glyph->pixel_width;
17467 voffset = glyph->voffset;
17468
17469 for (++glyph;
17470 (glyph < last
17471 && glyph->type == STRETCH_GLYPH
17472 && glyph->voffset == voffset
17473 && glyph->face_id == face_id);
17474 ++glyph)
17475 s->width += glyph->pixel_width;
17476
17477 /* Adjust base line for subscript/superscript text. */
17478 s->ybase += voffset;
17479
17480 /* The case that face->gc == 0 is handled when drawing the glyph
17481 string by calling PREPARE_FACE_FOR_DISPLAY. */
17482 xassert (s->face);
17483 return glyph - s->row->glyphs[s->area];
17484 }
17485
17486
17487 /* EXPORT for RIF:
17488 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17489 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17490 assumed to be zero. */
17491
17492 void
17493 x_get_glyph_overhangs (glyph, f, left, right)
17494 struct glyph *glyph;
17495 struct frame *f;
17496 int *left, *right;
17497 {
17498 *left = *right = 0;
17499
17500 if (glyph->type == CHAR_GLYPH)
17501 {
17502 XFontStruct *font;
17503 struct face *face;
17504 struct font_info *font_info;
17505 XChar2b char2b;
17506 XCharStruct *pcm;
17507
17508 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17509 font = face->font;
17510 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17511 if (font /* ++KFS: Should this be font_info ? */
17512 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17513 {
17514 if (pcm->rbearing > pcm->width)
17515 *right = pcm->rbearing - pcm->width;
17516 if (pcm->lbearing < 0)
17517 *left = -pcm->lbearing;
17518 }
17519 }
17520 }
17521
17522
17523 /* Return the index of the first glyph preceding glyph string S that
17524 is overwritten by S because of S's left overhang. Value is -1
17525 if no glyphs are overwritten. */
17526
17527 static int
17528 left_overwritten (s)
17529 struct glyph_string *s;
17530 {
17531 int k;
17532
17533 if (s->left_overhang)
17534 {
17535 int x = 0, i;
17536 struct glyph *glyphs = s->row->glyphs[s->area];
17537 int first = s->first_glyph - glyphs;
17538
17539 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17540 x -= glyphs[i].pixel_width;
17541
17542 k = i + 1;
17543 }
17544 else
17545 k = -1;
17546
17547 return k;
17548 }
17549
17550
17551 /* Return the index of the first glyph preceding glyph string S that
17552 is overwriting S because of its right overhang. Value is -1 if no
17553 glyph in front of S overwrites S. */
17554
17555 static int
17556 left_overwriting (s)
17557 struct glyph_string *s;
17558 {
17559 int i, k, x;
17560 struct glyph *glyphs = s->row->glyphs[s->area];
17561 int first = s->first_glyph - glyphs;
17562
17563 k = -1;
17564 x = 0;
17565 for (i = first - 1; i >= 0; --i)
17566 {
17567 int left, right;
17568 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17569 if (x + right > 0)
17570 k = i;
17571 x -= glyphs[i].pixel_width;
17572 }
17573
17574 return k;
17575 }
17576
17577
17578 /* Return the index of the last glyph following glyph string S that is
17579 not overwritten by S because of S's right overhang. Value is -1 if
17580 no such glyph is found. */
17581
17582 static int
17583 right_overwritten (s)
17584 struct glyph_string *s;
17585 {
17586 int k = -1;
17587
17588 if (s->right_overhang)
17589 {
17590 int x = 0, i;
17591 struct glyph *glyphs = s->row->glyphs[s->area];
17592 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17593 int end = s->row->used[s->area];
17594
17595 for (i = first; i < end && s->right_overhang > x; ++i)
17596 x += glyphs[i].pixel_width;
17597
17598 k = i;
17599 }
17600
17601 return k;
17602 }
17603
17604
17605 /* Return the index of the last glyph following glyph string S that
17606 overwrites S because of its left overhang. Value is negative
17607 if no such glyph is found. */
17608
17609 static int
17610 right_overwriting (s)
17611 struct glyph_string *s;
17612 {
17613 int i, k, x;
17614 int end = s->row->used[s->area];
17615 struct glyph *glyphs = s->row->glyphs[s->area];
17616 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17617
17618 k = -1;
17619 x = 0;
17620 for (i = first; i < end; ++i)
17621 {
17622 int left, right;
17623 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17624 if (x - left < 0)
17625 k = i;
17626 x += glyphs[i].pixel_width;
17627 }
17628
17629 return k;
17630 }
17631
17632
17633 /* Get face and two-byte form of character C in face FACE_ID on frame
17634 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17635 means we want to display multibyte text. DISPLAY_P non-zero means
17636 make sure that X resources for the face returned are allocated.
17637 Value is a pointer to a realized face that is ready for display if
17638 DISPLAY_P is non-zero. */
17639
17640 static INLINE struct face *
17641 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17642 struct frame *f;
17643 int c, face_id;
17644 XChar2b *char2b;
17645 int multibyte_p, display_p;
17646 {
17647 struct face *face = FACE_FROM_ID (f, face_id);
17648
17649 if (!multibyte_p)
17650 {
17651 /* Unibyte case. We don't have to encode, but we have to make
17652 sure to use a face suitable for unibyte. */
17653 STORE_XCHAR2B (char2b, 0, c);
17654 face_id = FACE_FOR_CHAR (f, face, c);
17655 face = FACE_FROM_ID (f, face_id);
17656 }
17657 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17658 {
17659 /* Case of ASCII in a face known to fit ASCII. */
17660 STORE_XCHAR2B (char2b, 0, c);
17661 }
17662 else
17663 {
17664 int c1, c2, charset;
17665
17666 /* Split characters into bytes. If c2 is -1 afterwards, C is
17667 really a one-byte character so that byte1 is zero. */
17668 SPLIT_CHAR (c, charset, c1, c2);
17669 if (c2 > 0)
17670 STORE_XCHAR2B (char2b, c1, c2);
17671 else
17672 STORE_XCHAR2B (char2b, 0, c1);
17673
17674 /* Maybe encode the character in *CHAR2B. */
17675 if (face->font != NULL)
17676 {
17677 struct font_info *font_info
17678 = FONT_INFO_FROM_ID (f, face->font_info_id);
17679 if (font_info)
17680 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17681 }
17682 }
17683
17684 /* Make sure X resources of the face are allocated. */
17685 #ifdef HAVE_X_WINDOWS
17686 if (display_p)
17687 #endif
17688 {
17689 xassert (face != NULL);
17690 PREPARE_FACE_FOR_DISPLAY (f, face);
17691 }
17692
17693 return face;
17694 }
17695
17696
17697 /* Set background width of glyph string S. START is the index of the
17698 first glyph following S. LAST_X is the right-most x-position + 1
17699 in the drawing area. */
17700
17701 static INLINE void
17702 set_glyph_string_background_width (s, start, last_x)
17703 struct glyph_string *s;
17704 int start;
17705 int last_x;
17706 {
17707 /* If the face of this glyph string has to be drawn to the end of
17708 the drawing area, set S->extends_to_end_of_line_p. */
17709 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17710
17711 if (start == s->row->used[s->area]
17712 && s->area == TEXT_AREA
17713 && ((s->hl == DRAW_NORMAL_TEXT
17714 && (s->row->fill_line_p
17715 || s->face->background != default_face->background
17716 || s->face->stipple != default_face->stipple
17717 || s->row->mouse_face_p))
17718 || s->hl == DRAW_MOUSE_FACE
17719 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17720 && s->row->fill_line_p)))
17721 s->extends_to_end_of_line_p = 1;
17722
17723 /* If S extends its face to the end of the line, set its
17724 background_width to the distance to the right edge of the drawing
17725 area. */
17726 if (s->extends_to_end_of_line_p)
17727 s->background_width = last_x - s->x + 1;
17728 else
17729 s->background_width = s->width;
17730 }
17731
17732
17733 /* Compute overhangs and x-positions for glyph string S and its
17734 predecessors, or successors. X is the starting x-position for S.
17735 BACKWARD_P non-zero means process predecessors. */
17736
17737 static void
17738 compute_overhangs_and_x (s, x, backward_p)
17739 struct glyph_string *s;
17740 int x;
17741 int backward_p;
17742 {
17743 if (backward_p)
17744 {
17745 while (s)
17746 {
17747 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17748 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17749 x -= s->width;
17750 s->x = x;
17751 s = s->prev;
17752 }
17753 }
17754 else
17755 {
17756 while (s)
17757 {
17758 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17759 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17760 s->x = x;
17761 x += s->width;
17762 s = s->next;
17763 }
17764 }
17765 }
17766
17767
17768
17769 /* The following macros are only called from draw_glyphs below.
17770 They reference the following parameters of that function directly:
17771 `w', `row', `area', and `overlap_p'
17772 as well as the following local variables:
17773 `s', `f', and `hdc' (in W32) */
17774
17775 #ifdef HAVE_NTGUI
17776 /* On W32, silently add local `hdc' variable to argument list of
17777 init_glyph_string. */
17778 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17779 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17780 #else
17781 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17782 init_glyph_string (s, char2b, w, row, area, start, hl)
17783 #endif
17784
17785 /* Add a glyph string for a stretch glyph to the list of strings
17786 between HEAD and TAIL. START is the index of the stretch glyph in
17787 row area AREA of glyph row ROW. END is the index of the last glyph
17788 in that glyph row area. X is the current output position assigned
17789 to the new glyph string constructed. HL overrides that face of the
17790 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17791 is the right-most x-position of the drawing area. */
17792
17793 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17794 and below -- keep them on one line. */
17795 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17796 do \
17797 { \
17798 s = (struct glyph_string *) alloca (sizeof *s); \
17799 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17800 START = fill_stretch_glyph_string (s, row, area, START, END); \
17801 append_glyph_string (&HEAD, &TAIL, s); \
17802 s->x = (X); \
17803 } \
17804 while (0)
17805
17806
17807 /* Add a glyph string for an image glyph to the list of strings
17808 between HEAD and TAIL. START is the index of the image glyph in
17809 row area AREA of glyph row ROW. END is the index of the last glyph
17810 in that glyph row area. X is the current output position assigned
17811 to the new glyph string constructed. HL overrides that face of the
17812 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17813 is the right-most x-position of the drawing area. */
17814
17815 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17816 do \
17817 { \
17818 s = (struct glyph_string *) alloca (sizeof *s); \
17819 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17820 fill_image_glyph_string (s); \
17821 append_glyph_string (&HEAD, &TAIL, s); \
17822 ++START; \
17823 s->x = (X); \
17824 } \
17825 while (0)
17826
17827
17828 /* Add a glyph string for a sequence of character glyphs to the list
17829 of strings between HEAD and TAIL. START is the index of the first
17830 glyph in row area AREA of glyph row ROW that is part of the new
17831 glyph string. END is the index of the last glyph in that glyph row
17832 area. X is the current output position assigned to the new glyph
17833 string constructed. HL overrides that face of the glyph; e.g. it
17834 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17835 right-most x-position of the drawing area. */
17836
17837 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17838 do \
17839 { \
17840 int c, face_id; \
17841 XChar2b *char2b; \
17842 \
17843 c = (row)->glyphs[area][START].u.ch; \
17844 face_id = (row)->glyphs[area][START].face_id; \
17845 \
17846 s = (struct glyph_string *) alloca (sizeof *s); \
17847 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17848 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17849 append_glyph_string (&HEAD, &TAIL, s); \
17850 s->x = (X); \
17851 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17852 } \
17853 while (0)
17854
17855
17856 /* Add a glyph string for a composite sequence to the list of strings
17857 between HEAD and TAIL. START is the index of the first glyph in
17858 row area AREA of glyph row ROW that is part of the new glyph
17859 string. END is the index of the last glyph in that glyph row area.
17860 X is the current output position assigned to the new glyph string
17861 constructed. HL overrides that face of the glyph; e.g. it is
17862 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17863 x-position of the drawing area. */
17864
17865 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17866 do { \
17867 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17868 int face_id = (row)->glyphs[area][START].face_id; \
17869 struct face *base_face = FACE_FROM_ID (f, face_id); \
17870 struct composition *cmp = composition_table[cmp_id]; \
17871 int glyph_len = cmp->glyph_len; \
17872 XChar2b *char2b; \
17873 struct face **faces; \
17874 struct glyph_string *first_s = NULL; \
17875 int n; \
17876 \
17877 base_face = base_face->ascii_face; \
17878 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17879 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17880 /* At first, fill in `char2b' and `faces'. */ \
17881 for (n = 0; n < glyph_len; n++) \
17882 { \
17883 int c = COMPOSITION_GLYPH (cmp, n); \
17884 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17885 faces[n] = FACE_FROM_ID (f, this_face_id); \
17886 get_char_face_and_encoding (f, c, this_face_id, \
17887 char2b + n, 1, 1); \
17888 } \
17889 \
17890 /* Make glyph_strings for each glyph sequence that is drawable by \
17891 the same face, and append them to HEAD/TAIL. */ \
17892 for (n = 0; n < cmp->glyph_len;) \
17893 { \
17894 s = (struct glyph_string *) alloca (sizeof *s); \
17895 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17896 append_glyph_string (&(HEAD), &(TAIL), s); \
17897 s->cmp = cmp; \
17898 s->gidx = n; \
17899 s->x = (X); \
17900 \
17901 if (n == 0) \
17902 first_s = s; \
17903 \
17904 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17905 } \
17906 \
17907 ++START; \
17908 s = first_s; \
17909 } while (0)
17910
17911
17912 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17913 of AREA of glyph row ROW on window W between indices START and END.
17914 HL overrides the face for drawing glyph strings, e.g. it is
17915 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17916 x-positions of the drawing area.
17917
17918 This is an ugly monster macro construct because we must use alloca
17919 to allocate glyph strings (because draw_glyphs can be called
17920 asynchronously). */
17921
17922 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17923 do \
17924 { \
17925 HEAD = TAIL = NULL; \
17926 while (START < END) \
17927 { \
17928 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17929 switch (first_glyph->type) \
17930 { \
17931 case CHAR_GLYPH: \
17932 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17933 HL, X, LAST_X); \
17934 break; \
17935 \
17936 case COMPOSITE_GLYPH: \
17937 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17938 HL, X, LAST_X); \
17939 break; \
17940 \
17941 case STRETCH_GLYPH: \
17942 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17943 HL, X, LAST_X); \
17944 break; \
17945 \
17946 case IMAGE_GLYPH: \
17947 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17948 HL, X, LAST_X); \
17949 break; \
17950 \
17951 default: \
17952 abort (); \
17953 } \
17954 \
17955 set_glyph_string_background_width (s, START, LAST_X); \
17956 (X) += s->width; \
17957 } \
17958 } \
17959 while (0)
17960
17961
17962 /* Draw glyphs between START and END in AREA of ROW on window W,
17963 starting at x-position X. X is relative to AREA in W. HL is a
17964 face-override with the following meaning:
17965
17966 DRAW_NORMAL_TEXT draw normally
17967 DRAW_CURSOR draw in cursor face
17968 DRAW_MOUSE_FACE draw in mouse face.
17969 DRAW_INVERSE_VIDEO draw in mode line face
17970 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17971 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17972
17973 If OVERLAPS_P is non-zero, draw only the foreground of characters
17974 and clip to the physical height of ROW.
17975
17976 Value is the x-position reached, relative to AREA of W. */
17977
17978 static int
17979 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17980 struct window *w;
17981 int x;
17982 struct glyph_row *row;
17983 enum glyph_row_area area;
17984 int start, end;
17985 enum draw_glyphs_face hl;
17986 int overlaps_p;
17987 {
17988 struct glyph_string *head, *tail;
17989 struct glyph_string *s;
17990 int last_x, area_width;
17991 int x_reached;
17992 int i, j;
17993 struct frame *f = XFRAME (WINDOW_FRAME (w));
17994 DECLARE_HDC (hdc);
17995
17996 ALLOCATE_HDC (hdc, f);
17997
17998 /* Let's rather be paranoid than getting a SEGV. */
17999 end = min (end, row->used[area]);
18000 start = max (0, start);
18001 start = min (end, start);
18002
18003 /* Translate X to frame coordinates. Set last_x to the right
18004 end of the drawing area. */
18005 if (row->full_width_p)
18006 {
18007 /* X is relative to the left edge of W, without scroll bars
18008 or fringes. */
18009 x += WINDOW_LEFT_EDGE_X (w);
18010 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18011 }
18012 else
18013 {
18014 int area_left = window_box_left (w, area);
18015 x += area_left;
18016 area_width = window_box_width (w, area);
18017 last_x = area_left + area_width;
18018 }
18019
18020 /* Build a doubly-linked list of glyph_string structures between
18021 head and tail from what we have to draw. Note that the macro
18022 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18023 the reason we use a separate variable `i'. */
18024 i = start;
18025 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18026 if (tail)
18027 x_reached = tail->x + tail->background_width;
18028 else
18029 x_reached = x;
18030
18031 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18032 the row, redraw some glyphs in front or following the glyph
18033 strings built above. */
18034 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18035 {
18036 int dummy_x = 0;
18037 struct glyph_string *h, *t;
18038
18039 /* Compute overhangs for all glyph strings. */
18040 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
18041 for (s = head; s; s = s->next)
18042 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
18043
18044 /* Prepend glyph strings for glyphs in front of the first glyph
18045 string that are overwritten because of the first glyph
18046 string's left overhang. The background of all strings
18047 prepended must be drawn because the first glyph string
18048 draws over it. */
18049 i = left_overwritten (head);
18050 if (i >= 0)
18051 {
18052 j = i;
18053 BUILD_GLYPH_STRINGS (j, start, h, t,
18054 DRAW_NORMAL_TEXT, dummy_x, last_x);
18055 start = i;
18056 compute_overhangs_and_x (t, head->x, 1);
18057 prepend_glyph_string_lists (&head, &tail, h, t);
18058 }
18059
18060 /* Prepend glyph strings for glyphs in front of the first glyph
18061 string that overwrite that glyph string because of their
18062 right overhang. For these strings, only the foreground must
18063 be drawn, because it draws over the glyph string at `head'.
18064 The background must not be drawn because this would overwrite
18065 right overhangs of preceding glyphs for which no glyph
18066 strings exist. */
18067 i = left_overwriting (head);
18068 if (i >= 0)
18069 {
18070 BUILD_GLYPH_STRINGS (i, start, h, t,
18071 DRAW_NORMAL_TEXT, dummy_x, last_x);
18072 for (s = h; s; s = s->next)
18073 s->background_filled_p = 1;
18074 compute_overhangs_and_x (t, head->x, 1);
18075 prepend_glyph_string_lists (&head, &tail, h, t);
18076 }
18077
18078 /* Append glyphs strings for glyphs following the last glyph
18079 string tail that are overwritten by tail. The background of
18080 these strings has to be drawn because tail's foreground draws
18081 over it. */
18082 i = right_overwritten (tail);
18083 if (i >= 0)
18084 {
18085 BUILD_GLYPH_STRINGS (end, i, h, t,
18086 DRAW_NORMAL_TEXT, x, last_x);
18087 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18088 append_glyph_string_lists (&head, &tail, h, t);
18089 }
18090
18091 /* Append glyph strings for glyphs following the last glyph
18092 string tail that overwrite tail. The foreground of such
18093 glyphs has to be drawn because it writes into the background
18094 of tail. The background must not be drawn because it could
18095 paint over the foreground of following glyphs. */
18096 i = right_overwriting (tail);
18097 if (i >= 0)
18098 {
18099 BUILD_GLYPH_STRINGS (end, i, h, t,
18100 DRAW_NORMAL_TEXT, x, last_x);
18101 for (s = h; s; s = s->next)
18102 s->background_filled_p = 1;
18103 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18104 append_glyph_string_lists (&head, &tail, h, t);
18105 }
18106 }
18107
18108 /* Draw all strings. */
18109 for (s = head; s; s = s->next)
18110 FRAME_RIF (f)->draw_glyph_string (s);
18111
18112 if (area == TEXT_AREA
18113 && !row->full_width_p
18114 /* When drawing overlapping rows, only the glyph strings'
18115 foreground is drawn, which doesn't erase a cursor
18116 completely. */
18117 && !overlaps_p)
18118 {
18119 int x0 = head ? head->x : x;
18120 int x1 = tail ? tail->x + tail->background_width : x;
18121
18122 int text_left = window_box_left (w, TEXT_AREA);
18123 x0 -= text_left;
18124 x1 -= text_left;
18125
18126 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18127 row->y, MATRIX_ROW_BOTTOM_Y (row));
18128 }
18129
18130 /* Value is the x-position up to which drawn, relative to AREA of W.
18131 This doesn't include parts drawn because of overhangs. */
18132 if (row->full_width_p)
18133 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18134 else
18135 x_reached -= window_box_left (w, area);
18136
18137 RELEASE_HDC (hdc, f);
18138
18139 return x_reached;
18140 }
18141
18142
18143 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18144 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18145
18146 static INLINE void
18147 append_glyph (it)
18148 struct it *it;
18149 {
18150 struct glyph *glyph;
18151 enum glyph_row_area area = it->area;
18152
18153 xassert (it->glyph_row);
18154 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18155
18156 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18157 if (glyph < it->glyph_row->glyphs[area + 1])
18158 {
18159 glyph->charpos = CHARPOS (it->position);
18160 glyph->object = it->object;
18161 glyph->pixel_width = it->pixel_width;
18162 glyph->ascent = it->ascent;
18163 glyph->descent = it->descent;
18164 glyph->voffset = it->voffset;
18165 glyph->type = CHAR_GLYPH;
18166 glyph->multibyte_p = it->multibyte_p;
18167 glyph->left_box_line_p = it->start_of_box_run_p;
18168 glyph->right_box_line_p = it->end_of_box_run_p;
18169 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18170 || it->phys_descent > it->descent);
18171 glyph->padding_p = 0;
18172 glyph->glyph_not_available_p = it->glyph_not_available_p;
18173 glyph->face_id = it->face_id;
18174 glyph->u.ch = it->char_to_display;
18175 glyph->font_type = FONT_TYPE_UNKNOWN;
18176 ++it->glyph_row->used[area];
18177 }
18178 }
18179
18180 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18181 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18182
18183 static INLINE void
18184 append_composite_glyph (it)
18185 struct it *it;
18186 {
18187 struct glyph *glyph;
18188 enum glyph_row_area area = it->area;
18189
18190 xassert (it->glyph_row);
18191
18192 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18193 if (glyph < it->glyph_row->glyphs[area + 1])
18194 {
18195 glyph->charpos = CHARPOS (it->position);
18196 glyph->object = it->object;
18197 glyph->pixel_width = it->pixel_width;
18198 glyph->ascent = it->ascent;
18199 glyph->descent = it->descent;
18200 glyph->voffset = it->voffset;
18201 glyph->type = COMPOSITE_GLYPH;
18202 glyph->multibyte_p = it->multibyte_p;
18203 glyph->left_box_line_p = it->start_of_box_run_p;
18204 glyph->right_box_line_p = it->end_of_box_run_p;
18205 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18206 || it->phys_descent > it->descent);
18207 glyph->padding_p = 0;
18208 glyph->glyph_not_available_p = 0;
18209 glyph->face_id = it->face_id;
18210 glyph->u.cmp_id = it->cmp_id;
18211 glyph->font_type = FONT_TYPE_UNKNOWN;
18212 ++it->glyph_row->used[area];
18213 }
18214 }
18215
18216
18217 /* Change IT->ascent and IT->height according to the setting of
18218 IT->voffset. */
18219
18220 static INLINE void
18221 take_vertical_position_into_account (it)
18222 struct it *it;
18223 {
18224 if (it->voffset)
18225 {
18226 if (it->voffset < 0)
18227 /* Increase the ascent so that we can display the text higher
18228 in the line. */
18229 it->ascent += abs (it->voffset);
18230 else
18231 /* Increase the descent so that we can display the text lower
18232 in the line. */
18233 it->descent += it->voffset;
18234 }
18235 }
18236
18237
18238 /* Produce glyphs/get display metrics for the image IT is loaded with.
18239 See the description of struct display_iterator in dispextern.h for
18240 an overview of struct display_iterator. */
18241
18242 static void
18243 produce_image_glyph (it)
18244 struct it *it;
18245 {
18246 struct image *img;
18247 struct face *face;
18248 int face_ascent, glyph_ascent;
18249
18250 xassert (it->what == IT_IMAGE);
18251
18252 face = FACE_FROM_ID (it->f, it->face_id);
18253 img = IMAGE_FROM_ID (it->f, it->image_id);
18254 xassert (img);
18255
18256 /* Make sure X resources of the face and image are loaded. */
18257 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18258 prepare_image_for_display (it->f, img);
18259
18260 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face);
18261 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
18262 it->pixel_width = img->width + 2 * img->hmargin;
18263
18264 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18265 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18266 if (face_ascent > it->ascent)
18267 it->ascent = it->phys_ascent = face_ascent;
18268
18269 it->nglyphs = 1;
18270
18271 if (face->box != FACE_NO_BOX)
18272 {
18273 if (face->box_line_width > 0)
18274 {
18275 it->ascent += face->box_line_width;
18276 it->descent += face->box_line_width;
18277 }
18278
18279 if (it->start_of_box_run_p)
18280 it->pixel_width += abs (face->box_line_width);
18281 if (it->end_of_box_run_p)
18282 it->pixel_width += abs (face->box_line_width);
18283 }
18284
18285 take_vertical_position_into_account (it);
18286
18287 if (it->glyph_row)
18288 {
18289 struct glyph *glyph;
18290 enum glyph_row_area area = it->area;
18291
18292 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18293 if (glyph < it->glyph_row->glyphs[area + 1])
18294 {
18295 glyph->charpos = CHARPOS (it->position);
18296 glyph->object = it->object;
18297 glyph->pixel_width = it->pixel_width;
18298 glyph->ascent = glyph_ascent;
18299 glyph->descent = it->descent;
18300 glyph->voffset = it->voffset;
18301 glyph->type = IMAGE_GLYPH;
18302 glyph->multibyte_p = it->multibyte_p;
18303 glyph->left_box_line_p = it->start_of_box_run_p;
18304 glyph->right_box_line_p = it->end_of_box_run_p;
18305 glyph->overlaps_vertically_p = 0;
18306 glyph->padding_p = 0;
18307 glyph->glyph_not_available_p = 0;
18308 glyph->face_id = it->face_id;
18309 glyph->u.img_id = img->id;
18310 glyph->font_type = FONT_TYPE_UNKNOWN;
18311 ++it->glyph_row->used[area];
18312 }
18313 }
18314 }
18315
18316
18317 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18318 of the glyph, WIDTH and HEIGHT are the width and height of the
18319 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18320
18321 static void
18322 append_stretch_glyph (it, object, width, height, ascent)
18323 struct it *it;
18324 Lisp_Object object;
18325 int width, height;
18326 int ascent;
18327 {
18328 struct glyph *glyph;
18329 enum glyph_row_area area = it->area;
18330
18331 xassert (ascent >= 0 && ascent <= height);
18332
18333 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18334 if (glyph < it->glyph_row->glyphs[area + 1])
18335 {
18336 glyph->charpos = CHARPOS (it->position);
18337 glyph->object = object;
18338 glyph->pixel_width = width;
18339 glyph->ascent = ascent;
18340 glyph->descent = height - ascent;
18341 glyph->voffset = it->voffset;
18342 glyph->type = STRETCH_GLYPH;
18343 glyph->multibyte_p = it->multibyte_p;
18344 glyph->left_box_line_p = it->start_of_box_run_p;
18345 glyph->right_box_line_p = it->end_of_box_run_p;
18346 glyph->overlaps_vertically_p = 0;
18347 glyph->padding_p = 0;
18348 glyph->glyph_not_available_p = 0;
18349 glyph->face_id = it->face_id;
18350 glyph->u.stretch.ascent = ascent;
18351 glyph->u.stretch.height = height;
18352 glyph->font_type = FONT_TYPE_UNKNOWN;
18353 ++it->glyph_row->used[area];
18354 }
18355 }
18356
18357
18358 /* Calculate a width or height in pixels from a specification using
18359 the following elements:
18360
18361 SPEC ::=
18362 NUM - a (fractional) multiple of the default font width/height
18363 (NUM) - specifies exactly NUM pixels
18364 UNIT - a fixed number of pixels, see below.
18365 ELEMENT - size of a display element in pixels, see below.
18366 (NUM . SPEC) - equals NUM * SPEC
18367 (+ SPEC SPEC ...) - add pixel values
18368 (- SPEC SPEC ...) - subtract pixel values
18369 (- SPEC) - negate pixel value
18370
18371 NUM ::=
18372 INT or FLOAT - a number constant
18373 SYMBOL - use symbol's (buffer local) variable binding.
18374
18375 UNIT ::=
18376 in - pixels per inch *)
18377 mm - pixels per 1/1000 meter *)
18378 cm - pixels per 1/100 meter *)
18379 width - width of current font in pixels.
18380 height - height of current font in pixels.
18381
18382 *) using the ratio(s) defined in display-pixels-per-inch.
18383
18384 ELEMENT ::=
18385
18386 left-fringe - left fringe width in pixels
18387 (left-fringe . nil) - left fringe width if inside margins, else 0
18388 (left-fringe . t) - left fringe width if outside margins, else 0
18389
18390 right-fringe - right fringe width in pixels
18391 (right-fringe . nil) - right fringe width if inside margins, else 0
18392 (right-fringe . t) - right fringe width if outside margins, else 0
18393
18394 left-margin - left margin width in pixels
18395 right-margin - right margin width in pixels
18396
18397 scroll-bar - scroll-bar area width in pixels
18398 (scroll-bar . left) - scroll-bar width if on left, else 0
18399 (scroll-bar . right) - scroll-bar width if on right, else 0
18400
18401 Examples:
18402
18403 Pixels corresponding to 5 inches:
18404 (5 . in)
18405
18406 Total width of non-text areas on left side of window:
18407 (+ left-fringe left-margin (scroll-bar . left))
18408
18409 Total width of fringes if inside display margins:
18410 (+ (left-fringe) (right-fringe))
18411
18412 Width of left margin minus width of 1 character in the default font:
18413 (- left-margin 1)
18414
18415 Width of left margin minus width of 2 characters in the current font:
18416 (- left-margin (2 . width))
18417
18418 Width of left fringe plus left margin minus one pixel:
18419 (- (+ left-fringe left-margin) (1))
18420 (+ left-fringe left-margin (- (1)))
18421 (+ left-fringe left-margin (-1))
18422
18423 */
18424
18425 #define NUMVAL(X) \
18426 ((INTEGERP (X) || FLOATP (X)) \
18427 ? XFLOATINT (X) \
18428 : - 1)
18429
18430 static int
18431 calc_pixel_width_or_height (res, it, prop, font, width_p)
18432 double *res;
18433 struct it *it;
18434 Lisp_Object prop;
18435 XFontStruct *font;
18436 int width_p;
18437 {
18438 double pixels;
18439
18440 #define OK_PIXELS(val) ((*res = (val)), 1)
18441
18442 if (SYMBOLP (prop))
18443 {
18444 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18445 {
18446 char *unit = SDATA (SYMBOL_NAME (prop));
18447
18448 if (unit[0] == 'i' && unit[1] == 'n')
18449 pixels = 1.0;
18450 else if (unit[0] == 'm' && unit[1] == 'm')
18451 pixels = 25.4;
18452 else if (unit[0] == 'c' && unit[1] == 'm')
18453 pixels = 2.54;
18454 else
18455 pixels = 0;
18456 if (pixels > 0)
18457 {
18458 double ppi;
18459 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18460 || (CONSP (Vdisplay_pixels_per_inch)
18461 && (ppi = (width_p
18462 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18463 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18464 ppi > 0)))
18465 return OK_PIXELS (ppi / pixels);
18466
18467 return 0;
18468 }
18469 }
18470
18471 if (EQ (prop, Qheight))
18472 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18473 if (EQ (prop, Qwidth))
18474 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18475 if (EQ (prop, Qleft_fringe))
18476 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18477 if (EQ (prop, Qright_fringe))
18478 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18479 if (EQ (prop, Qleft_margin))
18480 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18481 if (EQ (prop, Qright_margin))
18482 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18483 if (EQ (prop, Qscroll_bar))
18484 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18485
18486 prop = Fbuffer_local_value (prop, it->w->buffer);
18487 }
18488
18489 if (INTEGERP (prop) || FLOATP (prop))
18490 {
18491 int base_unit = (width_p
18492 ? FRAME_COLUMN_WIDTH (it->f)
18493 : FRAME_LINE_HEIGHT (it->f));
18494 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18495 }
18496
18497 if (CONSP (prop))
18498 {
18499 Lisp_Object car = XCAR (prop);
18500 Lisp_Object cdr = XCDR (prop);
18501
18502 if (SYMBOLP (car))
18503 {
18504 if (EQ (car, Qplus) || EQ (car, Qminus))
18505 {
18506 int first = 1;
18507 double px;
18508
18509 pixels = 0;
18510 while (CONSP (cdr))
18511 {
18512 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr), font, width_p))
18513 return 0;
18514 if (first)
18515 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18516 else
18517 pixels += px;
18518 cdr = XCDR (cdr);
18519 }
18520 if (EQ (car, Qminus))
18521 pixels = -pixels;
18522 return OK_PIXELS (pixels);
18523 }
18524
18525 if (EQ (car, Qleft_fringe))
18526 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18527 == !NILP (cdr))
18528 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
18529 : 0);
18530 if (EQ (car, Qright_fringe))
18531 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18532 == !NILP (cdr))
18533 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18534 : 0);
18535 if (EQ (car, Qscroll_bar))
18536 return OK_PIXELS ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18537 == EQ (cdr, Qleft))
18538 ? WINDOW_SCROLL_BAR_AREA_WIDTH (it->w)
18539 : 0);
18540
18541 car = Fbuffer_local_value (car, it->w->buffer);
18542 }
18543
18544 if (INTEGERP (car) || FLOATP (car))
18545 {
18546 double fact;
18547 pixels = XFLOATINT (car);
18548 if (NILP (cdr))
18549 return OK_PIXELS (pixels);
18550 if (calc_pixel_width_or_height (&fact, it, cdr, font, width_p))
18551 return OK_PIXELS (pixels * fact);
18552 return 0;
18553 }
18554
18555 return 0;
18556 }
18557
18558 return 0;
18559 }
18560
18561 /* Produce a stretch glyph for iterator IT. IT->object is the value
18562 of the glyph property displayed. The value must be a list
18563 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18564 being recognized:
18565
18566 1. `:width WIDTH' specifies that the space should be WIDTH *
18567 canonical char width wide. WIDTH may be an integer or floating
18568 point number.
18569
18570 2. `:relative-width FACTOR' specifies that the width of the stretch
18571 should be computed from the width of the first character having the
18572 `glyph' property, and should be FACTOR times that width.
18573
18574 3. `:align-to HPOS' specifies that the space should be wide enough
18575 to reach HPOS, a value in canonical character units.
18576
18577 Exactly one of the above pairs must be present.
18578
18579 4. `:height HEIGHT' specifies that the height of the stretch produced
18580 should be HEIGHT, measured in canonical character units.
18581
18582 5. `:relative-height FACTOR' specifies that the height of the
18583 stretch should be FACTOR times the height of the characters having
18584 the glyph property.
18585
18586 Either none or exactly one of 4 or 5 must be present.
18587
18588 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18589 of the stretch should be used for the ascent of the stretch.
18590 ASCENT must be in the range 0 <= ASCENT <= 100. */
18591
18592 static void
18593 produce_stretch_glyph (it)
18594 struct it *it;
18595 {
18596 /* (space :width WIDTH :height HEIGHT ...) */
18597 Lisp_Object prop, plist;
18598 int width = 0, height = 0;
18599 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18600 int ascent = 0;
18601 double tem;
18602 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18603 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18604
18605 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18606
18607 /* List should start with `space'. */
18608 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18609 plist = XCDR (it->object);
18610
18611 /* Compute the width of the stretch. */
18612 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18613 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18614 {
18615 /* Absolute width `:width WIDTH' specified and valid. */
18616 zero_width_ok_p = 1;
18617 width = (int)tem;
18618 }
18619 else if (prop = Fplist_get (plist, QCrelative_width),
18620 NUMVAL (prop) > 0)
18621 {
18622 /* Relative width `:relative-width FACTOR' specified and valid.
18623 Compute the width of the characters having the `glyph'
18624 property. */
18625 struct it it2;
18626 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18627
18628 it2 = *it;
18629 if (it->multibyte_p)
18630 {
18631 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18632 - IT_BYTEPOS (*it));
18633 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18634 }
18635 else
18636 it2.c = *p, it2.len = 1;
18637
18638 it2.glyph_row = NULL;
18639 it2.what = IT_CHARACTER;
18640 x_produce_glyphs (&it2);
18641 width = NUMVAL (prop) * it2.pixel_width;
18642 }
18643 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18644 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18645 {
18646 width = max (0, (int)tem - it->current_x);
18647 zero_width_ok_p = 1;
18648 }
18649 else
18650 /* Nothing specified -> width defaults to canonical char width. */
18651 width = FRAME_COLUMN_WIDTH (it->f);
18652
18653 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18654 width = 1;
18655
18656 /* Compute height. */
18657 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18658 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18659 {
18660 height = (int)tem;
18661 zero_height_ok_p = 1;
18662 }
18663 else if (prop = Fplist_get (plist, QCrelative_height),
18664 NUMVAL (prop) > 0)
18665 height = FONT_HEIGHT (font) * NUMVAL (prop);
18666 else
18667 height = FONT_HEIGHT (font);
18668
18669 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18670 height = 1;
18671
18672 /* Compute percentage of height used for ascent. If
18673 `:ascent ASCENT' is present and valid, use that. Otherwise,
18674 derive the ascent from the font in use. */
18675 if (prop = Fplist_get (plist, QCascent),
18676 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18677 ascent = height * NUMVAL (prop) / 100.0;
18678 else if (!NILP (prop)
18679 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18680 ascent = min (max (0, (int)tem), height);
18681 else
18682 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18683
18684 if (width > 0 && height > 0 && it->glyph_row)
18685 {
18686 Lisp_Object object = it->stack[it->sp - 1].string;
18687 if (!STRINGP (object))
18688 object = it->w->buffer;
18689 append_stretch_glyph (it, object, width, height, ascent);
18690 }
18691
18692 it->pixel_width = width;
18693 it->ascent = it->phys_ascent = ascent;
18694 it->descent = it->phys_descent = height - it->ascent;
18695 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18696
18697 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18698 {
18699 if (face->box_line_width > 0)
18700 {
18701 it->ascent += face->box_line_width;
18702 it->descent += face->box_line_width;
18703 }
18704
18705 if (it->start_of_box_run_p)
18706 it->pixel_width += abs (face->box_line_width);
18707 if (it->end_of_box_run_p)
18708 it->pixel_width += abs (face->box_line_width);
18709 }
18710
18711 take_vertical_position_into_account (it);
18712 }
18713
18714 /* RIF:
18715 Produce glyphs/get display metrics for the display element IT is
18716 loaded with. See the description of struct display_iterator in
18717 dispextern.h for an overview of struct display_iterator. */
18718
18719 void
18720 x_produce_glyphs (it)
18721 struct it *it;
18722 {
18723 it->glyph_not_available_p = 0;
18724
18725 if (it->what == IT_CHARACTER)
18726 {
18727 XChar2b char2b;
18728 XFontStruct *font;
18729 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18730 XCharStruct *pcm;
18731 int font_not_found_p;
18732 struct font_info *font_info;
18733 int boff; /* baseline offset */
18734 /* We may change it->multibyte_p upon unibyte<->multibyte
18735 conversion. So, save the current value now and restore it
18736 later.
18737
18738 Note: It seems that we don't have to record multibyte_p in
18739 struct glyph because the character code itself tells if or
18740 not the character is multibyte. Thus, in the future, we must
18741 consider eliminating the field `multibyte_p' in the struct
18742 glyph. */
18743 int saved_multibyte_p = it->multibyte_p;
18744
18745 /* Maybe translate single-byte characters to multibyte, or the
18746 other way. */
18747 it->char_to_display = it->c;
18748 if (!ASCII_BYTE_P (it->c))
18749 {
18750 if (unibyte_display_via_language_environment
18751 && SINGLE_BYTE_CHAR_P (it->c)
18752 && (it->c >= 0240
18753 || !NILP (Vnonascii_translation_table)))
18754 {
18755 it->char_to_display = unibyte_char_to_multibyte (it->c);
18756 it->multibyte_p = 1;
18757 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18758 face = FACE_FROM_ID (it->f, it->face_id);
18759 }
18760 else if (!SINGLE_BYTE_CHAR_P (it->c)
18761 && !it->multibyte_p)
18762 {
18763 it->multibyte_p = 1;
18764 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18765 face = FACE_FROM_ID (it->f, it->face_id);
18766 }
18767 }
18768
18769 /* Get font to use. Encode IT->char_to_display. */
18770 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18771 &char2b, it->multibyte_p, 0);
18772 font = face->font;
18773
18774 /* When no suitable font found, use the default font. */
18775 font_not_found_p = font == NULL;
18776 if (font_not_found_p)
18777 {
18778 font = FRAME_FONT (it->f);
18779 boff = FRAME_BASELINE_OFFSET (it->f);
18780 font_info = NULL;
18781 }
18782 else
18783 {
18784 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18785 boff = font_info->baseline_offset;
18786 if (font_info->vertical_centering)
18787 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18788 }
18789
18790 if (it->char_to_display >= ' '
18791 && (!it->multibyte_p || it->char_to_display < 128))
18792 {
18793 /* Either unibyte or ASCII. */
18794 int stretched_p;
18795
18796 it->nglyphs = 1;
18797
18798 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18799 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18800 it->ascent = FONT_BASE (font) + boff;
18801 it->descent = FONT_DESCENT (font) - boff;
18802
18803 if (pcm)
18804 {
18805 it->phys_ascent = pcm->ascent + boff;
18806 it->phys_descent = pcm->descent - boff;
18807 it->pixel_width = pcm->width;
18808 }
18809 else
18810 {
18811 it->glyph_not_available_p = 1;
18812 it->phys_ascent = FONT_BASE (font) + boff;
18813 it->phys_descent = FONT_DESCENT (font) - boff;
18814 it->pixel_width = FONT_WIDTH (font);
18815 }
18816
18817 /* If this is a space inside a region of text with
18818 `space-width' property, change its width. */
18819 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18820 if (stretched_p)
18821 it->pixel_width *= XFLOATINT (it->space_width);
18822
18823 /* If face has a box, add the box thickness to the character
18824 height. If character has a box line to the left and/or
18825 right, add the box line width to the character's width. */
18826 if (face->box != FACE_NO_BOX)
18827 {
18828 int thick = face->box_line_width;
18829
18830 if (thick > 0)
18831 {
18832 it->ascent += thick;
18833 it->descent += thick;
18834 }
18835 else
18836 thick = -thick;
18837
18838 if (it->start_of_box_run_p)
18839 it->pixel_width += thick;
18840 if (it->end_of_box_run_p)
18841 it->pixel_width += thick;
18842 }
18843
18844 /* If face has an overline, add the height of the overline
18845 (1 pixel) and a 1 pixel margin to the character height. */
18846 if (face->overline_p)
18847 it->ascent += 2;
18848
18849 take_vertical_position_into_account (it);
18850
18851 /* If we have to actually produce glyphs, do it. */
18852 if (it->glyph_row)
18853 {
18854 if (stretched_p)
18855 {
18856 /* Translate a space with a `space-width' property
18857 into a stretch glyph. */
18858 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18859 / FONT_HEIGHT (font));
18860 append_stretch_glyph (it, it->object, it->pixel_width,
18861 it->ascent + it->descent, ascent);
18862 }
18863 else
18864 append_glyph (it);
18865
18866 /* If characters with lbearing or rbearing are displayed
18867 in this line, record that fact in a flag of the
18868 glyph row. This is used to optimize X output code. */
18869 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18870 it->glyph_row->contains_overlapping_glyphs_p = 1;
18871 }
18872 }
18873 else if (it->char_to_display == '\n')
18874 {
18875 /* A newline has no width but we need the height of the line. */
18876 it->pixel_width = 0;
18877 it->nglyphs = 0;
18878 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18879 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18880
18881 if (face->box != FACE_NO_BOX
18882 && face->box_line_width > 0)
18883 {
18884 it->ascent += face->box_line_width;
18885 it->descent += face->box_line_width;
18886 }
18887 }
18888 else if (it->char_to_display == '\t')
18889 {
18890 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18891 int x = it->current_x + it->continuation_lines_width;
18892 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18893
18894 /* If the distance from the current position to the next tab
18895 stop is less than a canonical character width, use the
18896 tab stop after that. */
18897 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18898 next_tab_x += tab_width;
18899
18900 it->pixel_width = next_tab_x - x;
18901 it->nglyphs = 1;
18902 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18903 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18904
18905 if (it->glyph_row)
18906 {
18907 append_stretch_glyph (it, it->object, it->pixel_width,
18908 it->ascent + it->descent, it->ascent);
18909 }
18910 }
18911 else
18912 {
18913 /* A multi-byte character. Assume that the display width of the
18914 character is the width of the character multiplied by the
18915 width of the font. */
18916
18917 /* If we found a font, this font should give us the right
18918 metrics. If we didn't find a font, use the frame's
18919 default font and calculate the width of the character
18920 from the charset width; this is what old redisplay code
18921 did. */
18922
18923 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18924 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18925
18926 if (font_not_found_p || !pcm)
18927 {
18928 int charset = CHAR_CHARSET (it->char_to_display);
18929
18930 it->glyph_not_available_p = 1;
18931 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18932 * CHARSET_WIDTH (charset));
18933 it->phys_ascent = FONT_BASE (font) + boff;
18934 it->phys_descent = FONT_DESCENT (font) - boff;
18935 }
18936 else
18937 {
18938 it->pixel_width = pcm->width;
18939 it->phys_ascent = pcm->ascent + boff;
18940 it->phys_descent = pcm->descent - boff;
18941 if (it->glyph_row
18942 && (pcm->lbearing < 0
18943 || pcm->rbearing > pcm->width))
18944 it->glyph_row->contains_overlapping_glyphs_p = 1;
18945 }
18946 it->nglyphs = 1;
18947 it->ascent = FONT_BASE (font) + boff;
18948 it->descent = FONT_DESCENT (font) - boff;
18949 if (face->box != FACE_NO_BOX)
18950 {
18951 int thick = face->box_line_width;
18952
18953 if (thick > 0)
18954 {
18955 it->ascent += thick;
18956 it->descent += thick;
18957 }
18958 else
18959 thick = - thick;
18960
18961 if (it->start_of_box_run_p)
18962 it->pixel_width += thick;
18963 if (it->end_of_box_run_p)
18964 it->pixel_width += thick;
18965 }
18966
18967 /* If face has an overline, add the height of the overline
18968 (1 pixel) and a 1 pixel margin to the character height. */
18969 if (face->overline_p)
18970 it->ascent += 2;
18971
18972 take_vertical_position_into_account (it);
18973
18974 if (it->glyph_row)
18975 append_glyph (it);
18976 }
18977 it->multibyte_p = saved_multibyte_p;
18978 }
18979 else if (it->what == IT_COMPOSITION)
18980 {
18981 /* Note: A composition is represented as one glyph in the
18982 glyph matrix. There are no padding glyphs. */
18983 XChar2b char2b;
18984 XFontStruct *font;
18985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18986 XCharStruct *pcm;
18987 int font_not_found_p;
18988 struct font_info *font_info;
18989 int boff; /* baseline offset */
18990 struct composition *cmp = composition_table[it->cmp_id];
18991
18992 /* Maybe translate single-byte characters to multibyte. */
18993 it->char_to_display = it->c;
18994 if (unibyte_display_via_language_environment
18995 && SINGLE_BYTE_CHAR_P (it->c)
18996 && (it->c >= 0240
18997 || (it->c >= 0200
18998 && !NILP (Vnonascii_translation_table))))
18999 {
19000 it->char_to_display = unibyte_char_to_multibyte (it->c);
19001 }
19002
19003 /* Get face and font to use. Encode IT->char_to_display. */
19004 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19005 face = FACE_FROM_ID (it->f, it->face_id);
19006 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19007 &char2b, it->multibyte_p, 0);
19008 font = face->font;
19009
19010 /* When no suitable font found, use the default font. */
19011 font_not_found_p = font == NULL;
19012 if (font_not_found_p)
19013 {
19014 font = FRAME_FONT (it->f);
19015 boff = FRAME_BASELINE_OFFSET (it->f);
19016 font_info = NULL;
19017 }
19018 else
19019 {
19020 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19021 boff = font_info->baseline_offset;
19022 if (font_info->vertical_centering)
19023 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19024 }
19025
19026 /* There are no padding glyphs, so there is only one glyph to
19027 produce for the composition. Important is that pixel_width,
19028 ascent and descent are the values of what is drawn by
19029 draw_glyphs (i.e. the values of the overall glyphs composed). */
19030 it->nglyphs = 1;
19031
19032 /* If we have not yet calculated pixel size data of glyphs of
19033 the composition for the current face font, calculate them
19034 now. Theoretically, we have to check all fonts for the
19035 glyphs, but that requires much time and memory space. So,
19036 here we check only the font of the first glyph. This leads
19037 to incorrect display very rarely, and C-l (recenter) can
19038 correct the display anyway. */
19039 if (cmp->font != (void *) font)
19040 {
19041 /* Ascent and descent of the font of the first character of
19042 this composition (adjusted by baseline offset). Ascent
19043 and descent of overall glyphs should not be less than
19044 them respectively. */
19045 int font_ascent = FONT_BASE (font) + boff;
19046 int font_descent = FONT_DESCENT (font) - boff;
19047 /* Bounding box of the overall glyphs. */
19048 int leftmost, rightmost, lowest, highest;
19049 int i, width, ascent, descent;
19050
19051 cmp->font = (void *) font;
19052
19053 /* Initialize the bounding box. */
19054 if (font_info
19055 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19056 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19057 {
19058 width = pcm->width;
19059 ascent = pcm->ascent;
19060 descent = pcm->descent;
19061 }
19062 else
19063 {
19064 width = FONT_WIDTH (font);
19065 ascent = FONT_BASE (font);
19066 descent = FONT_DESCENT (font);
19067 }
19068
19069 rightmost = width;
19070 lowest = - descent + boff;
19071 highest = ascent + boff;
19072 leftmost = 0;
19073
19074 if (font_info
19075 && font_info->default_ascent
19076 && CHAR_TABLE_P (Vuse_default_ascent)
19077 && !NILP (Faref (Vuse_default_ascent,
19078 make_number (it->char_to_display))))
19079 highest = font_info->default_ascent + boff;
19080
19081 /* Draw the first glyph at the normal position. It may be
19082 shifted to right later if some other glyphs are drawn at
19083 the left. */
19084 cmp->offsets[0] = 0;
19085 cmp->offsets[1] = boff;
19086
19087 /* Set cmp->offsets for the remaining glyphs. */
19088 for (i = 1; i < cmp->glyph_len; i++)
19089 {
19090 int left, right, btm, top;
19091 int ch = COMPOSITION_GLYPH (cmp, i);
19092 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19093
19094 face = FACE_FROM_ID (it->f, face_id);
19095 get_char_face_and_encoding (it->f, ch, face->id,
19096 &char2b, it->multibyte_p, 0);
19097 font = face->font;
19098 if (font == NULL)
19099 {
19100 font = FRAME_FONT (it->f);
19101 boff = FRAME_BASELINE_OFFSET (it->f);
19102 font_info = NULL;
19103 }
19104 else
19105 {
19106 font_info
19107 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19108 boff = font_info->baseline_offset;
19109 if (font_info->vertical_centering)
19110 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19111 }
19112
19113 if (font_info
19114 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19115 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19116 {
19117 width = pcm->width;
19118 ascent = pcm->ascent;
19119 descent = pcm->descent;
19120 }
19121 else
19122 {
19123 width = FONT_WIDTH (font);
19124 ascent = 1;
19125 descent = 0;
19126 }
19127
19128 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19129 {
19130 /* Relative composition with or without
19131 alternate chars. */
19132 left = (leftmost + rightmost - width) / 2;
19133 btm = - descent + boff;
19134 if (font_info && font_info->relative_compose
19135 && (! CHAR_TABLE_P (Vignore_relative_composition)
19136 || NILP (Faref (Vignore_relative_composition,
19137 make_number (ch)))))
19138 {
19139
19140 if (- descent >= font_info->relative_compose)
19141 /* One extra pixel between two glyphs. */
19142 btm = highest + 1;
19143 else if (ascent <= 0)
19144 /* One extra pixel between two glyphs. */
19145 btm = lowest - 1 - ascent - descent;
19146 }
19147 }
19148 else
19149 {
19150 /* A composition rule is specified by an integer
19151 value that encodes global and new reference
19152 points (GREF and NREF). GREF and NREF are
19153 specified by numbers as below:
19154
19155 0---1---2 -- ascent
19156 | |
19157 | |
19158 | |
19159 9--10--11 -- center
19160 | |
19161 ---3---4---5--- baseline
19162 | |
19163 6---7---8 -- descent
19164 */
19165 int rule = COMPOSITION_RULE (cmp, i);
19166 int gref, nref, grefx, grefy, nrefx, nrefy;
19167
19168 COMPOSITION_DECODE_RULE (rule, gref, nref);
19169 grefx = gref % 3, nrefx = nref % 3;
19170 grefy = gref / 3, nrefy = nref / 3;
19171
19172 left = (leftmost
19173 + grefx * (rightmost - leftmost) / 2
19174 - nrefx * width / 2);
19175 btm = ((grefy == 0 ? highest
19176 : grefy == 1 ? 0
19177 : grefy == 2 ? lowest
19178 : (highest + lowest) / 2)
19179 - (nrefy == 0 ? ascent + descent
19180 : nrefy == 1 ? descent - boff
19181 : nrefy == 2 ? 0
19182 : (ascent + descent) / 2));
19183 }
19184
19185 cmp->offsets[i * 2] = left;
19186 cmp->offsets[i * 2 + 1] = btm + descent;
19187
19188 /* Update the bounding box of the overall glyphs. */
19189 right = left + width;
19190 top = btm + descent + ascent;
19191 if (left < leftmost)
19192 leftmost = left;
19193 if (right > rightmost)
19194 rightmost = right;
19195 if (top > highest)
19196 highest = top;
19197 if (btm < lowest)
19198 lowest = btm;
19199 }
19200
19201 /* If there are glyphs whose x-offsets are negative,
19202 shift all glyphs to the right and make all x-offsets
19203 non-negative. */
19204 if (leftmost < 0)
19205 {
19206 for (i = 0; i < cmp->glyph_len; i++)
19207 cmp->offsets[i * 2] -= leftmost;
19208 rightmost -= leftmost;
19209 }
19210
19211 cmp->pixel_width = rightmost;
19212 cmp->ascent = highest;
19213 cmp->descent = - lowest;
19214 if (cmp->ascent < font_ascent)
19215 cmp->ascent = font_ascent;
19216 if (cmp->descent < font_descent)
19217 cmp->descent = font_descent;
19218 }
19219
19220 it->pixel_width = cmp->pixel_width;
19221 it->ascent = it->phys_ascent = cmp->ascent;
19222 it->descent = it->phys_descent = cmp->descent;
19223
19224 if (face->box != FACE_NO_BOX)
19225 {
19226 int thick = face->box_line_width;
19227
19228 if (thick > 0)
19229 {
19230 it->ascent += thick;
19231 it->descent += thick;
19232 }
19233 else
19234 thick = - thick;
19235
19236 if (it->start_of_box_run_p)
19237 it->pixel_width += thick;
19238 if (it->end_of_box_run_p)
19239 it->pixel_width += thick;
19240 }
19241
19242 /* If face has an overline, add the height of the overline
19243 (1 pixel) and a 1 pixel margin to the character height. */
19244 if (face->overline_p)
19245 it->ascent += 2;
19246
19247 take_vertical_position_into_account (it);
19248
19249 if (it->glyph_row)
19250 append_composite_glyph (it);
19251 }
19252 else if (it->what == IT_IMAGE)
19253 produce_image_glyph (it);
19254 else if (it->what == IT_STRETCH)
19255 produce_stretch_glyph (it);
19256
19257 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19258 because this isn't true for images with `:ascent 100'. */
19259 xassert (it->ascent >= 0 && it->descent >= 0);
19260 if (it->area == TEXT_AREA)
19261 it->current_x += it->pixel_width;
19262
19263 it->descent += it->extra_line_spacing;
19264
19265 it->max_ascent = max (it->max_ascent, it->ascent);
19266 it->max_descent = max (it->max_descent, it->descent);
19267 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19268 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19269 }
19270
19271 /* EXPORT for RIF:
19272 Output LEN glyphs starting at START at the nominal cursor position.
19273 Advance the nominal cursor over the text. The global variable
19274 updated_window contains the window being updated, updated_row is
19275 the glyph row being updated, and updated_area is the area of that
19276 row being updated. */
19277
19278 void
19279 x_write_glyphs (start, len)
19280 struct glyph *start;
19281 int len;
19282 {
19283 int x, hpos;
19284
19285 xassert (updated_window && updated_row);
19286 BLOCK_INPUT;
19287
19288 /* Write glyphs. */
19289
19290 hpos = start - updated_row->glyphs[updated_area];
19291 x = draw_glyphs (updated_window, output_cursor.x,
19292 updated_row, updated_area,
19293 hpos, hpos + len,
19294 DRAW_NORMAL_TEXT, 0);
19295
19296 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19297 if (updated_area == TEXT_AREA
19298 && updated_window->phys_cursor_on_p
19299 && updated_window->phys_cursor.vpos == output_cursor.vpos
19300 && updated_window->phys_cursor.hpos >= hpos
19301 && updated_window->phys_cursor.hpos < hpos + len)
19302 updated_window->phys_cursor_on_p = 0;
19303
19304 UNBLOCK_INPUT;
19305
19306 /* Advance the output cursor. */
19307 output_cursor.hpos += len;
19308 output_cursor.x = x;
19309 }
19310
19311
19312 /* EXPORT for RIF:
19313 Insert LEN glyphs from START at the nominal cursor position. */
19314
19315 void
19316 x_insert_glyphs (start, len)
19317 struct glyph *start;
19318 int len;
19319 {
19320 struct frame *f;
19321 struct window *w;
19322 int line_height, shift_by_width, shifted_region_width;
19323 struct glyph_row *row;
19324 struct glyph *glyph;
19325 int frame_x, frame_y, hpos;
19326
19327 xassert (updated_window && updated_row);
19328 BLOCK_INPUT;
19329 w = updated_window;
19330 f = XFRAME (WINDOW_FRAME (w));
19331
19332 /* Get the height of the line we are in. */
19333 row = updated_row;
19334 line_height = row->height;
19335
19336 /* Get the width of the glyphs to insert. */
19337 shift_by_width = 0;
19338 for (glyph = start; glyph < start + len; ++glyph)
19339 shift_by_width += glyph->pixel_width;
19340
19341 /* Get the width of the region to shift right. */
19342 shifted_region_width = (window_box_width (w, updated_area)
19343 - output_cursor.x
19344 - shift_by_width);
19345
19346 /* Shift right. */
19347 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19348 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19349
19350 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19351 line_height, shift_by_width);
19352
19353 /* Write the glyphs. */
19354 hpos = start - row->glyphs[updated_area];
19355 draw_glyphs (w, output_cursor.x, row, updated_area,
19356 hpos, hpos + len,
19357 DRAW_NORMAL_TEXT, 0);
19358
19359 /* Advance the output cursor. */
19360 output_cursor.hpos += len;
19361 output_cursor.x += shift_by_width;
19362 UNBLOCK_INPUT;
19363 }
19364
19365
19366 /* EXPORT for RIF:
19367 Erase the current text line from the nominal cursor position
19368 (inclusive) to pixel column TO_X (exclusive). The idea is that
19369 everything from TO_X onward is already erased.
19370
19371 TO_X is a pixel position relative to updated_area of
19372 updated_window. TO_X == -1 means clear to the end of this area. */
19373
19374 void
19375 x_clear_end_of_line (to_x)
19376 int to_x;
19377 {
19378 struct frame *f;
19379 struct window *w = updated_window;
19380 int max_x, min_y, max_y;
19381 int from_x, from_y, to_y;
19382
19383 xassert (updated_window && updated_row);
19384 f = XFRAME (w->frame);
19385
19386 if (updated_row->full_width_p)
19387 max_x = WINDOW_TOTAL_WIDTH (w);
19388 else
19389 max_x = window_box_width (w, updated_area);
19390 max_y = window_text_bottom_y (w);
19391
19392 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19393 of window. For TO_X > 0, truncate to end of drawing area. */
19394 if (to_x == 0)
19395 return;
19396 else if (to_x < 0)
19397 to_x = max_x;
19398 else
19399 to_x = min (to_x, max_x);
19400
19401 to_y = min (max_y, output_cursor.y + updated_row->height);
19402
19403 /* Notice if the cursor will be cleared by this operation. */
19404 if (!updated_row->full_width_p)
19405 notice_overwritten_cursor (w, updated_area,
19406 output_cursor.x, -1,
19407 updated_row->y,
19408 MATRIX_ROW_BOTTOM_Y (updated_row));
19409
19410 from_x = output_cursor.x;
19411
19412 /* Translate to frame coordinates. */
19413 if (updated_row->full_width_p)
19414 {
19415 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19416 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19417 }
19418 else
19419 {
19420 int area_left = window_box_left (w, updated_area);
19421 from_x += area_left;
19422 to_x += area_left;
19423 }
19424
19425 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19426 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19427 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19428
19429 /* Prevent inadvertently clearing to end of the X window. */
19430 if (to_x > from_x && to_y > from_y)
19431 {
19432 BLOCK_INPUT;
19433 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19434 to_x - from_x, to_y - from_y);
19435 UNBLOCK_INPUT;
19436 }
19437 }
19438
19439 #endif /* HAVE_WINDOW_SYSTEM */
19440
19441
19442 \f
19443 /***********************************************************************
19444 Cursor types
19445 ***********************************************************************/
19446
19447 /* Value is the internal representation of the specified cursor type
19448 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19449 of the bar cursor. */
19450
19451 static enum text_cursor_kinds
19452 get_specified_cursor_type (arg, width)
19453 Lisp_Object arg;
19454 int *width;
19455 {
19456 enum text_cursor_kinds type;
19457
19458 if (NILP (arg))
19459 return NO_CURSOR;
19460
19461 if (EQ (arg, Qbox))
19462 return FILLED_BOX_CURSOR;
19463
19464 if (EQ (arg, Qhollow))
19465 return HOLLOW_BOX_CURSOR;
19466
19467 if (EQ (arg, Qbar))
19468 {
19469 *width = 2;
19470 return BAR_CURSOR;
19471 }
19472
19473 if (CONSP (arg)
19474 && EQ (XCAR (arg), Qbar)
19475 && INTEGERP (XCDR (arg))
19476 && XINT (XCDR (arg)) >= 0)
19477 {
19478 *width = XINT (XCDR (arg));
19479 return BAR_CURSOR;
19480 }
19481
19482 if (EQ (arg, Qhbar))
19483 {
19484 *width = 2;
19485 return HBAR_CURSOR;
19486 }
19487
19488 if (CONSP (arg)
19489 && EQ (XCAR (arg), Qhbar)
19490 && INTEGERP (XCDR (arg))
19491 && XINT (XCDR (arg)) >= 0)
19492 {
19493 *width = XINT (XCDR (arg));
19494 return HBAR_CURSOR;
19495 }
19496
19497 /* Treat anything unknown as "hollow box cursor".
19498 It was bad to signal an error; people have trouble fixing
19499 .Xdefaults with Emacs, when it has something bad in it. */
19500 type = HOLLOW_BOX_CURSOR;
19501
19502 return type;
19503 }
19504
19505 /* Set the default cursor types for specified frame. */
19506 void
19507 set_frame_cursor_types (f, arg)
19508 struct frame *f;
19509 Lisp_Object arg;
19510 {
19511 int width;
19512 Lisp_Object tem;
19513
19514 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19515 FRAME_CURSOR_WIDTH (f) = width;
19516
19517 /* By default, set up the blink-off state depending on the on-state. */
19518
19519 tem = Fassoc (arg, Vblink_cursor_alist);
19520 if (!NILP (tem))
19521 {
19522 FRAME_BLINK_OFF_CURSOR (f)
19523 = get_specified_cursor_type (XCDR (tem), &width);
19524 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19525 }
19526 else
19527 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19528 }
19529
19530
19531 /* Return the cursor we want to be displayed in window W. Return
19532 width of bar/hbar cursor through WIDTH arg. Return with
19533 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19534 (i.e. if the `system caret' should track this cursor).
19535
19536 In a mini-buffer window, we want the cursor only to appear if we
19537 are reading input from this window. For the selected window, we
19538 want the cursor type given by the frame parameter or buffer local
19539 setting of cursor-type. If explicitly marked off, draw no cursor.
19540 In all other cases, we want a hollow box cursor. */
19541
19542 static enum text_cursor_kinds
19543 get_window_cursor_type (w, glyph, width, active_cursor)
19544 struct window *w;
19545 struct glyph *glyph;
19546 int *width;
19547 int *active_cursor;
19548 {
19549 struct frame *f = XFRAME (w->frame);
19550 struct buffer *b = XBUFFER (w->buffer);
19551 int cursor_type = DEFAULT_CURSOR;
19552 Lisp_Object alt_cursor;
19553 int non_selected = 0;
19554
19555 *active_cursor = 1;
19556
19557 /* Echo area */
19558 if (cursor_in_echo_area
19559 && FRAME_HAS_MINIBUF_P (f)
19560 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19561 {
19562 if (w == XWINDOW (echo_area_window))
19563 {
19564 *width = FRAME_CURSOR_WIDTH (f);
19565 return FRAME_DESIRED_CURSOR (f);
19566 }
19567
19568 *active_cursor = 0;
19569 non_selected = 1;
19570 }
19571
19572 /* Nonselected window or nonselected frame. */
19573 else if (w != XWINDOW (f->selected_window)
19574 #ifdef HAVE_WINDOW_SYSTEM
19575 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19576 #endif
19577 )
19578 {
19579 *active_cursor = 0;
19580
19581 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19582 return NO_CURSOR;
19583
19584 non_selected = 1;
19585 }
19586
19587 /* Never display a cursor in a window in which cursor-type is nil. */
19588 if (NILP (b->cursor_type))
19589 return NO_CURSOR;
19590
19591 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19592 if (non_selected)
19593 {
19594 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19595 return get_specified_cursor_type (alt_cursor, width);
19596 }
19597
19598 /* Get the normal cursor type for this window. */
19599 if (EQ (b->cursor_type, Qt))
19600 {
19601 cursor_type = FRAME_DESIRED_CURSOR (f);
19602 *width = FRAME_CURSOR_WIDTH (f);
19603 }
19604 else
19605 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19606
19607 /* Use normal cursor if not blinked off. */
19608 if (!w->cursor_off_p)
19609 {
19610 if (glyph->type == IMAGE_GLYPH) {
19611 if (cursor_type == FILLED_BOX_CURSOR)
19612 cursor_type = HOLLOW_BOX_CURSOR;
19613 }
19614 return cursor_type;
19615 }
19616
19617 /* Cursor is blinked off, so determine how to "toggle" it. */
19618
19619 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19620 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19621 return get_specified_cursor_type (XCDR (alt_cursor), width);
19622
19623 /* Then see if frame has specified a specific blink off cursor type. */
19624 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19625 {
19626 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19627 return FRAME_BLINK_OFF_CURSOR (f);
19628 }
19629
19630 #if 0
19631 /* Some people liked having a permanently visible blinking cursor,
19632 while others had very strong opinions against it. So it was
19633 decided to remove it. KFS 2003-09-03 */
19634
19635 /* Finally perform built-in cursor blinking:
19636 filled box <-> hollow box
19637 wide [h]bar <-> narrow [h]bar
19638 narrow [h]bar <-> no cursor
19639 other type <-> no cursor */
19640
19641 if (cursor_type == FILLED_BOX_CURSOR)
19642 return HOLLOW_BOX_CURSOR;
19643
19644 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19645 {
19646 *width = 1;
19647 return cursor_type;
19648 }
19649 #endif
19650
19651 return NO_CURSOR;
19652 }
19653
19654
19655 #ifdef HAVE_WINDOW_SYSTEM
19656
19657 /* Notice when the text cursor of window W has been completely
19658 overwritten by a drawing operation that outputs glyphs in AREA
19659 starting at X0 and ending at X1 in the line starting at Y0 and
19660 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19661 the rest of the line after X0 has been written. Y coordinates
19662 are window-relative. */
19663
19664 static void
19665 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19666 struct window *w;
19667 enum glyph_row_area area;
19668 int x0, y0, x1, y1;
19669 {
19670 int cx0, cx1, cy0, cy1;
19671 struct glyph_row *row;
19672
19673 if (!w->phys_cursor_on_p)
19674 return;
19675 if (area != TEXT_AREA)
19676 return;
19677
19678 row = w->current_matrix->rows + w->phys_cursor.vpos;
19679 if (!row->displays_text_p)
19680 return;
19681
19682 if (row->cursor_in_fringe_p)
19683 {
19684 row->cursor_in_fringe_p = 0;
19685 draw_fringe_bitmap (w, row, 0);
19686 w->phys_cursor_on_p = 0;
19687 return;
19688 }
19689
19690 cx0 = w->phys_cursor.x;
19691 cx1 = cx0 + w->phys_cursor_width;
19692 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19693 return;
19694
19695 /* The cursor image will be completely removed from the
19696 screen if the output area intersects the cursor area in
19697 y-direction. When we draw in [y0 y1[, and some part of
19698 the cursor is at y < y0, that part must have been drawn
19699 before. When scrolling, the cursor is erased before
19700 actually scrolling, so we don't come here. When not
19701 scrolling, the rows above the old cursor row must have
19702 changed, and in this case these rows must have written
19703 over the cursor image.
19704
19705 Likewise if part of the cursor is below y1, with the
19706 exception of the cursor being in the first blank row at
19707 the buffer and window end because update_text_area
19708 doesn't draw that row. (Except when it does, but
19709 that's handled in update_text_area.) */
19710
19711 cy0 = w->phys_cursor.y;
19712 cy1 = cy0 + w->phys_cursor_height;
19713 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19714 return;
19715
19716 w->phys_cursor_on_p = 0;
19717 }
19718
19719 #endif /* HAVE_WINDOW_SYSTEM */
19720
19721 \f
19722 /************************************************************************
19723 Mouse Face
19724 ************************************************************************/
19725
19726 #ifdef HAVE_WINDOW_SYSTEM
19727
19728 /* EXPORT for RIF:
19729 Fix the display of area AREA of overlapping row ROW in window W. */
19730
19731 void
19732 x_fix_overlapping_area (w, row, area)
19733 struct window *w;
19734 struct glyph_row *row;
19735 enum glyph_row_area area;
19736 {
19737 int i, x;
19738
19739 BLOCK_INPUT;
19740
19741 x = 0;
19742 for (i = 0; i < row->used[area];)
19743 {
19744 if (row->glyphs[area][i].overlaps_vertically_p)
19745 {
19746 int start = i, start_x = x;
19747
19748 do
19749 {
19750 x += row->glyphs[area][i].pixel_width;
19751 ++i;
19752 }
19753 while (i < row->used[area]
19754 && row->glyphs[area][i].overlaps_vertically_p);
19755
19756 draw_glyphs (w, start_x, row, area,
19757 start, i,
19758 DRAW_NORMAL_TEXT, 1);
19759 }
19760 else
19761 {
19762 x += row->glyphs[area][i].pixel_width;
19763 ++i;
19764 }
19765 }
19766
19767 UNBLOCK_INPUT;
19768 }
19769
19770
19771 /* EXPORT:
19772 Draw the cursor glyph of window W in glyph row ROW. See the
19773 comment of draw_glyphs for the meaning of HL. */
19774
19775 void
19776 draw_phys_cursor_glyph (w, row, hl)
19777 struct window *w;
19778 struct glyph_row *row;
19779 enum draw_glyphs_face hl;
19780 {
19781 /* If cursor hpos is out of bounds, don't draw garbage. This can
19782 happen in mini-buffer windows when switching between echo area
19783 glyphs and mini-buffer. */
19784 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19785 {
19786 int on_p = w->phys_cursor_on_p;
19787 int x1;
19788 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19789 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19790 hl, 0);
19791 w->phys_cursor_on_p = on_p;
19792
19793 if (hl == DRAW_CURSOR)
19794 w->phys_cursor_width = x1 - w->phys_cursor.x;
19795 /* When we erase the cursor, and ROW is overlapped by other
19796 rows, make sure that these overlapping parts of other rows
19797 are redrawn. */
19798 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19799 {
19800 if (row > w->current_matrix->rows
19801 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19802 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19803
19804 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19805 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19806 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19807 }
19808 }
19809 }
19810
19811
19812 /* EXPORT:
19813 Erase the image of a cursor of window W from the screen. */
19814
19815 void
19816 erase_phys_cursor (w)
19817 struct window *w;
19818 {
19819 struct frame *f = XFRAME (w->frame);
19820 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19821 int hpos = w->phys_cursor.hpos;
19822 int vpos = w->phys_cursor.vpos;
19823 int mouse_face_here_p = 0;
19824 struct glyph_matrix *active_glyphs = w->current_matrix;
19825 struct glyph_row *cursor_row;
19826 struct glyph *cursor_glyph;
19827 enum draw_glyphs_face hl;
19828
19829 /* No cursor displayed or row invalidated => nothing to do on the
19830 screen. */
19831 if (w->phys_cursor_type == NO_CURSOR)
19832 goto mark_cursor_off;
19833
19834 /* VPOS >= active_glyphs->nrows means that window has been resized.
19835 Don't bother to erase the cursor. */
19836 if (vpos >= active_glyphs->nrows)
19837 goto mark_cursor_off;
19838
19839 /* If row containing cursor is marked invalid, there is nothing we
19840 can do. */
19841 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19842 if (!cursor_row->enabled_p)
19843 goto mark_cursor_off;
19844
19845 /* If row is completely invisible, don't attempt to delete a cursor which
19846 isn't there. This can happen if cursor is at top of a window, and
19847 we switch to a buffer with a header line in that window. */
19848 if (cursor_row->visible_height <= 0)
19849 goto mark_cursor_off;
19850
19851 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19852 if (cursor_row->cursor_in_fringe_p)
19853 {
19854 cursor_row->cursor_in_fringe_p = 0;
19855 draw_fringe_bitmap (w, cursor_row, 0);
19856 goto mark_cursor_off;
19857 }
19858
19859 /* This can happen when the new row is shorter than the old one.
19860 In this case, either draw_glyphs or clear_end_of_line
19861 should have cleared the cursor. Note that we wouldn't be
19862 able to erase the cursor in this case because we don't have a
19863 cursor glyph at hand. */
19864 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19865 goto mark_cursor_off;
19866
19867 /* If the cursor is in the mouse face area, redisplay that when
19868 we clear the cursor. */
19869 if (! NILP (dpyinfo->mouse_face_window)
19870 && w == XWINDOW (dpyinfo->mouse_face_window)
19871 && (vpos > dpyinfo->mouse_face_beg_row
19872 || (vpos == dpyinfo->mouse_face_beg_row
19873 && hpos >= dpyinfo->mouse_face_beg_col))
19874 && (vpos < dpyinfo->mouse_face_end_row
19875 || (vpos == dpyinfo->mouse_face_end_row
19876 && hpos < dpyinfo->mouse_face_end_col))
19877 /* Don't redraw the cursor's spot in mouse face if it is at the
19878 end of a line (on a newline). The cursor appears there, but
19879 mouse highlighting does not. */
19880 && cursor_row->used[TEXT_AREA] > hpos)
19881 mouse_face_here_p = 1;
19882
19883 /* Maybe clear the display under the cursor. */
19884 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19885 {
19886 int x, y;
19887 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19888
19889 cursor_glyph = get_phys_cursor_glyph (w);
19890 if (cursor_glyph == NULL)
19891 goto mark_cursor_off;
19892
19893 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19894 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19895
19896 FRAME_RIF (f)->clear_frame_area (f, x, y,
19897 cursor_glyph->pixel_width, cursor_row->visible_height);
19898 }
19899
19900 /* Erase the cursor by redrawing the character underneath it. */
19901 if (mouse_face_here_p)
19902 hl = DRAW_MOUSE_FACE;
19903 else
19904 hl = DRAW_NORMAL_TEXT;
19905 draw_phys_cursor_glyph (w, cursor_row, hl);
19906
19907 mark_cursor_off:
19908 w->phys_cursor_on_p = 0;
19909 w->phys_cursor_type = NO_CURSOR;
19910 }
19911
19912
19913 /* EXPORT:
19914 Display or clear cursor of window W. If ON is zero, clear the
19915 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19916 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19917
19918 void
19919 display_and_set_cursor (w, on, hpos, vpos, x, y)
19920 struct window *w;
19921 int on, hpos, vpos, x, y;
19922 {
19923 struct frame *f = XFRAME (w->frame);
19924 int new_cursor_type;
19925 int new_cursor_width;
19926 int active_cursor;
19927 struct glyph_matrix *current_glyphs;
19928 struct glyph_row *glyph_row;
19929 struct glyph *glyph;
19930
19931 /* This is pointless on invisible frames, and dangerous on garbaged
19932 windows and frames; in the latter case, the frame or window may
19933 be in the midst of changing its size, and x and y may be off the
19934 window. */
19935 if (! FRAME_VISIBLE_P (f)
19936 || FRAME_GARBAGED_P (f)
19937 || vpos >= w->current_matrix->nrows
19938 || hpos >= w->current_matrix->matrix_w)
19939 return;
19940
19941 /* If cursor is off and we want it off, return quickly. */
19942 if (!on && !w->phys_cursor_on_p)
19943 return;
19944
19945 current_glyphs = w->current_matrix;
19946 glyph_row = MATRIX_ROW (current_glyphs, vpos);
19947 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19948
19949 /* If cursor row is not enabled, we don't really know where to
19950 display the cursor. */
19951 if (!glyph_row->enabled_p)
19952 {
19953 w->phys_cursor_on_p = 0;
19954 return;
19955 }
19956
19957 xassert (interrupt_input_blocked);
19958
19959 /* Set new_cursor_type to the cursor we want to be displayed. */
19960 new_cursor_type = get_window_cursor_type (w, glyph,
19961 &new_cursor_width, &active_cursor);
19962
19963 /* If cursor is currently being shown and we don't want it to be or
19964 it is in the wrong place, or the cursor type is not what we want,
19965 erase it. */
19966 if (w->phys_cursor_on_p
19967 && (!on
19968 || w->phys_cursor.x != x
19969 || w->phys_cursor.y != y
19970 || new_cursor_type != w->phys_cursor_type
19971 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19972 && new_cursor_width != w->phys_cursor_width)))
19973 erase_phys_cursor (w);
19974
19975 /* Don't check phys_cursor_on_p here because that flag is only set
19976 to zero in some cases where we know that the cursor has been
19977 completely erased, to avoid the extra work of erasing the cursor
19978 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19979 still not be visible, or it has only been partly erased. */
19980 if (on)
19981 {
19982 w->phys_cursor_ascent = glyph_row->ascent;
19983 w->phys_cursor_height = glyph_row->height;
19984
19985 /* Set phys_cursor_.* before x_draw_.* is called because some
19986 of them may need the information. */
19987 w->phys_cursor.x = x;
19988 w->phys_cursor.y = glyph_row->y;
19989 w->phys_cursor.hpos = hpos;
19990 w->phys_cursor.vpos = vpos;
19991 }
19992
19993 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
19994 new_cursor_type, new_cursor_width,
19995 on, active_cursor);
19996 }
19997
19998
19999 /* Switch the display of W's cursor on or off, according to the value
20000 of ON. */
20001
20002 static void
20003 update_window_cursor (w, on)
20004 struct window *w;
20005 int on;
20006 {
20007 /* Don't update cursor in windows whose frame is in the process
20008 of being deleted. */
20009 if (w->current_matrix)
20010 {
20011 BLOCK_INPUT;
20012 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20013 w->phys_cursor.x, w->phys_cursor.y);
20014 UNBLOCK_INPUT;
20015 }
20016 }
20017
20018
20019 /* Call update_window_cursor with parameter ON_P on all leaf windows
20020 in the window tree rooted at W. */
20021
20022 static void
20023 update_cursor_in_window_tree (w, on_p)
20024 struct window *w;
20025 int on_p;
20026 {
20027 while (w)
20028 {
20029 if (!NILP (w->hchild))
20030 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20031 else if (!NILP (w->vchild))
20032 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20033 else
20034 update_window_cursor (w, on_p);
20035
20036 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20037 }
20038 }
20039
20040
20041 /* EXPORT:
20042 Display the cursor on window W, or clear it, according to ON_P.
20043 Don't change the cursor's position. */
20044
20045 void
20046 x_update_cursor (f, on_p)
20047 struct frame *f;
20048 int on_p;
20049 {
20050 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20051 }
20052
20053
20054 /* EXPORT:
20055 Clear the cursor of window W to background color, and mark the
20056 cursor as not shown. This is used when the text where the cursor
20057 is is about to be rewritten. */
20058
20059 void
20060 x_clear_cursor (w)
20061 struct window *w;
20062 {
20063 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20064 update_window_cursor (w, 0);
20065 }
20066
20067
20068 /* EXPORT:
20069 Display the active region described by mouse_face_* according to DRAW. */
20070
20071 void
20072 show_mouse_face (dpyinfo, draw)
20073 Display_Info *dpyinfo;
20074 enum draw_glyphs_face draw;
20075 {
20076 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20077 struct frame *f = XFRAME (WINDOW_FRAME (w));
20078
20079 if (/* If window is in the process of being destroyed, don't bother
20080 to do anything. */
20081 w->current_matrix != NULL
20082 /* Don't update mouse highlight if hidden */
20083 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20084 /* Recognize when we are called to operate on rows that don't exist
20085 anymore. This can happen when a window is split. */
20086 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20087 {
20088 int phys_cursor_on_p = w->phys_cursor_on_p;
20089 struct glyph_row *row, *first, *last;
20090
20091 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20092 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20093
20094 for (row = first; row <= last && row->enabled_p; ++row)
20095 {
20096 int start_hpos, end_hpos, start_x;
20097
20098 /* For all but the first row, the highlight starts at column 0. */
20099 if (row == first)
20100 {
20101 start_hpos = dpyinfo->mouse_face_beg_col;
20102 start_x = dpyinfo->mouse_face_beg_x;
20103 }
20104 else
20105 {
20106 start_hpos = 0;
20107 start_x = 0;
20108 }
20109
20110 if (row == last)
20111 end_hpos = dpyinfo->mouse_face_end_col;
20112 else
20113 end_hpos = row->used[TEXT_AREA];
20114
20115 if (end_hpos > start_hpos)
20116 {
20117 draw_glyphs (w, start_x, row, TEXT_AREA,
20118 start_hpos, end_hpos,
20119 draw, 0);
20120
20121 row->mouse_face_p
20122 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20123 }
20124 }
20125
20126 /* When we've written over the cursor, arrange for it to
20127 be displayed again. */
20128 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20129 {
20130 BLOCK_INPUT;
20131 display_and_set_cursor (w, 1,
20132 w->phys_cursor.hpos, w->phys_cursor.vpos,
20133 w->phys_cursor.x, w->phys_cursor.y);
20134 UNBLOCK_INPUT;
20135 }
20136 }
20137
20138 /* Change the mouse cursor. */
20139 if (draw == DRAW_NORMAL_TEXT)
20140 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20141 else if (draw == DRAW_MOUSE_FACE)
20142 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20143 else
20144 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20145 }
20146
20147 /* EXPORT:
20148 Clear out the mouse-highlighted active region.
20149 Redraw it un-highlighted first. Value is non-zero if mouse
20150 face was actually drawn unhighlighted. */
20151
20152 int
20153 clear_mouse_face (dpyinfo)
20154 Display_Info *dpyinfo;
20155 {
20156 int cleared = 0;
20157
20158 if (!NILP (dpyinfo->mouse_face_window))
20159 {
20160 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20161 cleared = 1;
20162 }
20163
20164 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20165 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20166 dpyinfo->mouse_face_window = Qnil;
20167 dpyinfo->mouse_face_overlay = Qnil;
20168 return cleared;
20169 }
20170
20171
20172 /* EXPORT:
20173 Non-zero if physical cursor of window W is within mouse face. */
20174
20175 int
20176 cursor_in_mouse_face_p (w)
20177 struct window *w;
20178 {
20179 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20180 int in_mouse_face = 0;
20181
20182 if (WINDOWP (dpyinfo->mouse_face_window)
20183 && XWINDOW (dpyinfo->mouse_face_window) == w)
20184 {
20185 int hpos = w->phys_cursor.hpos;
20186 int vpos = w->phys_cursor.vpos;
20187
20188 if (vpos >= dpyinfo->mouse_face_beg_row
20189 && vpos <= dpyinfo->mouse_face_end_row
20190 && (vpos > dpyinfo->mouse_face_beg_row
20191 || hpos >= dpyinfo->mouse_face_beg_col)
20192 && (vpos < dpyinfo->mouse_face_end_row
20193 || hpos < dpyinfo->mouse_face_end_col
20194 || dpyinfo->mouse_face_past_end))
20195 in_mouse_face = 1;
20196 }
20197
20198 return in_mouse_face;
20199 }
20200
20201
20202
20203 \f
20204 /* Find the glyph matrix position of buffer position CHARPOS in window
20205 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20206 current glyphs must be up to date. If CHARPOS is above window
20207 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20208 of last line in W. In the row containing CHARPOS, stop before glyphs
20209 having STOP as object. */
20210
20211 #if 1 /* This is a version of fast_find_position that's more correct
20212 in the presence of hscrolling, for example. I didn't install
20213 it right away because the problem fixed is minor, it failed
20214 in 20.x as well, and I think it's too risky to install
20215 so near the release of 21.1. 2001-09-25 gerd. */
20216
20217 static int
20218 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20219 struct window *w;
20220 int charpos;
20221 int *hpos, *vpos, *x, *y;
20222 Lisp_Object stop;
20223 {
20224 struct glyph_row *row, *first;
20225 struct glyph *glyph, *end;
20226 int past_end = 0;
20227
20228 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20229 row = row_containing_pos (w, charpos, first, NULL, 0);
20230 if (row == NULL)
20231 {
20232 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20233 {
20234 *x = *y = *hpos = *vpos = 0;
20235 return 0;
20236 }
20237 else
20238 {
20239 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20240 past_end = 1;
20241 }
20242 }
20243
20244 *x = row->x;
20245 *y = row->y;
20246 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20247
20248 glyph = row->glyphs[TEXT_AREA];
20249 end = glyph + row->used[TEXT_AREA];
20250
20251 /* Skip over glyphs not having an object at the start of the row.
20252 These are special glyphs like truncation marks on terminal
20253 frames. */
20254 if (row->displays_text_p)
20255 while (glyph < end
20256 && INTEGERP (glyph->object)
20257 && !EQ (stop, glyph->object)
20258 && glyph->charpos < 0)
20259 {
20260 *x += glyph->pixel_width;
20261 ++glyph;
20262 }
20263
20264 while (glyph < end
20265 && !INTEGERP (glyph->object)
20266 && !EQ (stop, glyph->object)
20267 && (!BUFFERP (glyph->object)
20268 || glyph->charpos < charpos))
20269 {
20270 *x += glyph->pixel_width;
20271 ++glyph;
20272 }
20273
20274 *hpos = glyph - row->glyphs[TEXT_AREA];
20275 return past_end;
20276 }
20277
20278 #else /* not 1 */
20279
20280 static int
20281 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20282 struct window *w;
20283 int pos;
20284 int *hpos, *vpos, *x, *y;
20285 Lisp_Object stop;
20286 {
20287 int i;
20288 int lastcol;
20289 int maybe_next_line_p = 0;
20290 int line_start_position;
20291 int yb = window_text_bottom_y (w);
20292 struct glyph_row *row, *best_row;
20293 int row_vpos, best_row_vpos;
20294 int current_x;
20295
20296 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20297 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20298
20299 while (row->y < yb)
20300 {
20301 if (row->used[TEXT_AREA])
20302 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20303 else
20304 line_start_position = 0;
20305
20306 if (line_start_position > pos)
20307 break;
20308 /* If the position sought is the end of the buffer,
20309 don't include the blank lines at the bottom of the window. */
20310 else if (line_start_position == pos
20311 && pos == BUF_ZV (XBUFFER (w->buffer)))
20312 {
20313 maybe_next_line_p = 1;
20314 break;
20315 }
20316 else if (line_start_position > 0)
20317 {
20318 best_row = row;
20319 best_row_vpos = row_vpos;
20320 }
20321
20322 if (row->y + row->height >= yb)
20323 break;
20324
20325 ++row;
20326 ++row_vpos;
20327 }
20328
20329 /* Find the right column within BEST_ROW. */
20330 lastcol = 0;
20331 current_x = best_row->x;
20332 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20333 {
20334 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20335 int charpos = glyph->charpos;
20336
20337 if (BUFFERP (glyph->object))
20338 {
20339 if (charpos == pos)
20340 {
20341 *hpos = i;
20342 *vpos = best_row_vpos;
20343 *x = current_x;
20344 *y = best_row->y;
20345 return 1;
20346 }
20347 else if (charpos > pos)
20348 break;
20349 }
20350 else if (EQ (glyph->object, stop))
20351 break;
20352
20353 if (charpos > 0)
20354 lastcol = i;
20355 current_x += glyph->pixel_width;
20356 }
20357
20358 /* If we're looking for the end of the buffer,
20359 and we didn't find it in the line we scanned,
20360 use the start of the following line. */
20361 if (maybe_next_line_p)
20362 {
20363 ++best_row;
20364 ++best_row_vpos;
20365 lastcol = 0;
20366 current_x = best_row->x;
20367 }
20368
20369 *vpos = best_row_vpos;
20370 *hpos = lastcol + 1;
20371 *x = current_x;
20372 *y = best_row->y;
20373 return 0;
20374 }
20375
20376 #endif /* not 1 */
20377
20378
20379 /* Find the position of the glyph for position POS in OBJECT in
20380 window W's current matrix, and return in *X, *Y the pixel
20381 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20382
20383 RIGHT_P non-zero means return the position of the right edge of the
20384 glyph, RIGHT_P zero means return the left edge position.
20385
20386 If no glyph for POS exists in the matrix, return the position of
20387 the glyph with the next smaller position that is in the matrix, if
20388 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20389 exists in the matrix, return the position of the glyph with the
20390 next larger position in OBJECT.
20391
20392 Value is non-zero if a glyph was found. */
20393
20394 static int
20395 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20396 struct window *w;
20397 int pos;
20398 Lisp_Object object;
20399 int *hpos, *vpos, *x, *y;
20400 int right_p;
20401 {
20402 int yb = window_text_bottom_y (w);
20403 struct glyph_row *r;
20404 struct glyph *best_glyph = NULL;
20405 struct glyph_row *best_row = NULL;
20406 int best_x = 0;
20407
20408 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20409 r->enabled_p && r->y < yb;
20410 ++r)
20411 {
20412 struct glyph *g = r->glyphs[TEXT_AREA];
20413 struct glyph *e = g + r->used[TEXT_AREA];
20414 int gx;
20415
20416 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20417 if (EQ (g->object, object))
20418 {
20419 if (g->charpos == pos)
20420 {
20421 best_glyph = g;
20422 best_x = gx;
20423 best_row = r;
20424 goto found;
20425 }
20426 else if (best_glyph == NULL
20427 || ((abs (g->charpos - pos)
20428 < abs (best_glyph->charpos - pos))
20429 && (right_p
20430 ? g->charpos < pos
20431 : g->charpos > pos)))
20432 {
20433 best_glyph = g;
20434 best_x = gx;
20435 best_row = r;
20436 }
20437 }
20438 }
20439
20440 found:
20441
20442 if (best_glyph)
20443 {
20444 *x = best_x;
20445 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20446
20447 if (right_p)
20448 {
20449 *x += best_glyph->pixel_width;
20450 ++*hpos;
20451 }
20452
20453 *y = best_row->y;
20454 *vpos = best_row - w->current_matrix->rows;
20455 }
20456
20457 return best_glyph != NULL;
20458 }
20459
20460
20461 /* See if position X, Y is within a hot-spot of an image. */
20462
20463 static int
20464 on_hot_spot_p (hot_spot, x, y)
20465 Lisp_Object hot_spot;
20466 int x, y;
20467 {
20468 if (!CONSP (hot_spot))
20469 return 0;
20470
20471 if (EQ (XCAR (hot_spot), Qrect))
20472 {
20473 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20474 Lisp_Object rect = XCDR (hot_spot);
20475 Lisp_Object tem;
20476 if (!CONSP (rect))
20477 return 0;
20478 if (!CONSP (XCAR (rect)))
20479 return 0;
20480 if (!CONSP (XCDR (rect)))
20481 return 0;
20482 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20483 return 0;
20484 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20485 return 0;
20486 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20487 return 0;
20488 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20489 return 0;
20490 return 1;
20491 }
20492 else if (EQ (XCAR (hot_spot), Qcircle))
20493 {
20494 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20495 Lisp_Object circ = XCDR (hot_spot);
20496 Lisp_Object lr, lx0, ly0;
20497 if (CONSP (circ)
20498 && CONSP (XCAR (circ))
20499 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20500 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20501 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20502 {
20503 double r = XFLOATINT (lr);
20504 double dx = XINT (lx0) - x;
20505 double dy = XINT (ly0) - y;
20506 return (dx * dx + dy * dy <= r * r);
20507 }
20508 }
20509 else if (EQ (XCAR (hot_spot), Qpoly))
20510 {
20511 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20512 if (VECTORP (XCDR (hot_spot)))
20513 {
20514 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20515 Lisp_Object *poly = v->contents;
20516 int n = v->size;
20517 int i;
20518 int inside = 0;
20519 Lisp_Object lx, ly;
20520 int x0, y0;
20521
20522 /* Need an even number of coordinates, and at least 3 edges. */
20523 if (n < 6 || n & 1)
20524 return 0;
20525
20526 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20527 If count is odd, we are inside polygon. Pixels on edges
20528 may or may not be included depending on actual geometry of the
20529 polygon. */
20530 if ((lx = poly[n-2], !INTEGERP (lx))
20531 || (ly = poly[n-1], !INTEGERP (lx)))
20532 return 0;
20533 x0 = XINT (lx), y0 = XINT (ly);
20534 for (i = 0; i < n; i += 2)
20535 {
20536 int x1 = x0, y1 = y0;
20537 if ((lx = poly[i], !INTEGERP (lx))
20538 || (ly = poly[i+1], !INTEGERP (ly)))
20539 return 0;
20540 x0 = XINT (lx), y0 = XINT (ly);
20541
20542 /* Does this segment cross the X line? */
20543 if (x0 >= x)
20544 {
20545 if (x1 >= x)
20546 continue;
20547 }
20548 else if (x1 < x)
20549 continue;
20550 if (y > y0 && y > y1)
20551 continue;
20552 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20553 inside = !inside;
20554 }
20555 return inside;
20556 }
20557 }
20558 else
20559 return 0;
20560 }
20561
20562 Lisp_Object
20563 find_hot_spot (map, x, y)
20564 Lisp_Object map;
20565 int x, y;
20566 {
20567 while (CONSP (map))
20568 {
20569 if (CONSP (XCAR (map))
20570 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20571 return XCAR (map);
20572 map = XCDR (map);
20573 }
20574
20575 return Qnil;
20576 }
20577
20578 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20579 3, 3, 0,
20580 doc: /* Lookup in image map MAP coordinates X and Y.
20581 An image map is an alist where each element has the format (AREA ID PLIST).
20582 An AREA is specified as either a rectangle, a circle, or a polygon:
20583 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20584 pixel coordinates of the upper left and bottom right corners.
20585 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20586 and the radius of the circle; r may be a float or integer.
20587 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20588 vector describes one corner in the polygon.
20589 Returns the alist element for the first matching AREA in MAP. */)
20590 (map, x, y)
20591 Lisp_Object map;
20592 Lisp_Object x, y;
20593 {
20594 int ix, iy;
20595 if (NILP (map))
20596 return Qnil;
20597
20598 if (!INTEGERP (x))
20599 wrong_type_argument (Qintegerp, x);
20600 if (!INTEGERP (y))
20601 wrong_type_argument (Qintegerp, y);
20602
20603 return find_hot_spot (map, XINT (x), XINT (y));
20604 }
20605
20606
20607 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20608 static void
20609 define_frame_cursor1 (f, cursor, pointer)
20610 struct frame *f;
20611 Cursor cursor;
20612 Lisp_Object pointer;
20613 {
20614 if (!NILP (pointer))
20615 {
20616 if (EQ (pointer, Qarrow))
20617 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20618 else if (EQ (pointer, Qhand))
20619 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20620 else if (EQ (pointer, Qtext))
20621 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20622 else if (EQ (pointer, intern ("hdrag")))
20623 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20624 #ifdef HAVE_X_WINDOWS
20625 else if (EQ (pointer, intern ("vdrag")))
20626 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20627 #endif
20628 else if (EQ (pointer, intern ("hourglass")))
20629 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20630 else if (EQ (pointer, Qmodeline))
20631 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20632 else
20633 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20634 }
20635
20636 #ifndef HAVE_CARBON
20637 if (cursor != No_Cursor)
20638 #else
20639 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20640 #endif
20641 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20642 }
20643
20644 /* Take proper action when mouse has moved to the mode or header line
20645 or marginal area AREA of window W, x-position X and y-position Y.
20646 X is relative to the start of the text display area of W, so the
20647 width of bitmap areas and scroll bars must be subtracted to get a
20648 position relative to the start of the mode line. */
20649
20650 static void
20651 note_mode_line_or_margin_highlight (w, x, y, area)
20652 struct window *w;
20653 int x, y;
20654 enum window_part area;
20655 {
20656 struct frame *f = XFRAME (w->frame);
20657 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20658 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20659 Lisp_Object pointer = Qnil;
20660 int charpos, dx, dy, width, height;
20661 Lisp_Object string, object = Qnil;
20662 Lisp_Object pos, help, image;
20663
20664 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20665 string = mode_line_string (w, area, &x, &y, &charpos,
20666 &object, &dx, &dy, &width, &height);
20667 else
20668 {
20669 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20670 string = marginal_area_string (w, area, &x, &y, &charpos,
20671 &object, &dx, &dy, &width, &height);
20672 }
20673
20674 help = Qnil;
20675
20676 if (IMAGEP (object))
20677 {
20678 Lisp_Object image_map, hotspot;
20679 if ((image_map = Fplist_get (XCDR (object), QCmap),
20680 !NILP (image_map))
20681 && (hotspot = find_hot_spot (image_map, dx, dy),
20682 CONSP (hotspot))
20683 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20684 {
20685 Lisp_Object area_id, plist;
20686
20687 area_id = XCAR (hotspot);
20688 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20689 If so, we could look for mouse-enter, mouse-leave
20690 properties in PLIST (and do something...). */
20691 if ((plist = XCDR (hotspot), CONSP (plist)))
20692 {
20693 pointer = Fplist_get (plist, Qpointer);
20694 if (NILP (pointer))
20695 pointer = Qhand;
20696 help = Fplist_get (plist, Qhelp_echo);
20697 if (!NILP (help))
20698 {
20699 help_echo_string = help;
20700 /* Is this correct? ++kfs */
20701 XSETWINDOW (help_echo_window, w);
20702 help_echo_object = w->buffer;
20703 help_echo_pos = charpos;
20704 }
20705 }
20706 if (NILP (pointer))
20707 pointer = Fplist_get (XCDR (object), QCpointer);
20708 }
20709 }
20710
20711 if (STRINGP (string))
20712 {
20713 pos = make_number (charpos);
20714 /* If we're on a string with `help-echo' text property, arrange
20715 for the help to be displayed. This is done by setting the
20716 global variable help_echo_string to the help string. */
20717 help = Fget_text_property (pos, Qhelp_echo, string);
20718 if (!NILP (help))
20719 {
20720 help_echo_string = help;
20721 XSETWINDOW (help_echo_window, w);
20722 help_echo_object = string;
20723 help_echo_pos = charpos;
20724 }
20725
20726 if (NILP (pointer))
20727 pointer = Fget_text_property (pos, Qpointer, string);
20728
20729 /* Change the mouse pointer according to what is under X/Y. */
20730 if (NILP (pointer) && area == ON_MODE_LINE)
20731 {
20732 Lisp_Object map;
20733 map = Fget_text_property (pos, Qlocal_map, string);
20734 if (!KEYMAPP (map))
20735 map = Fget_text_property (pos, Qkeymap, string);
20736 if (!KEYMAPP (map))
20737 cursor = dpyinfo->vertical_scroll_bar_cursor;
20738 }
20739 }
20740
20741 define_frame_cursor1 (f, cursor, pointer);
20742 }
20743
20744
20745 /* EXPORT:
20746 Take proper action when the mouse has moved to position X, Y on
20747 frame F as regards highlighting characters that have mouse-face
20748 properties. Also de-highlighting chars where the mouse was before.
20749 X and Y can be negative or out of range. */
20750
20751 void
20752 note_mouse_highlight (f, x, y)
20753 struct frame *f;
20754 int x, y;
20755 {
20756 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20757 enum window_part part;
20758 Lisp_Object window;
20759 struct window *w;
20760 Cursor cursor = No_Cursor;
20761 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20762 struct buffer *b;
20763
20764 /* When a menu is active, don't highlight because this looks odd. */
20765 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20766 if (popup_activated ())
20767 return;
20768 #endif
20769
20770 if (NILP (Vmouse_highlight)
20771 || !f->glyphs_initialized_p)
20772 return;
20773
20774 dpyinfo->mouse_face_mouse_x = x;
20775 dpyinfo->mouse_face_mouse_y = y;
20776 dpyinfo->mouse_face_mouse_frame = f;
20777
20778 if (dpyinfo->mouse_face_defer)
20779 return;
20780
20781 if (gc_in_progress)
20782 {
20783 dpyinfo->mouse_face_deferred_gc = 1;
20784 return;
20785 }
20786
20787 /* Which window is that in? */
20788 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20789
20790 /* If we were displaying active text in another window, clear that. */
20791 if (! EQ (window, dpyinfo->mouse_face_window))
20792 clear_mouse_face (dpyinfo);
20793
20794 /* Not on a window -> return. */
20795 if (!WINDOWP (window))
20796 return;
20797
20798 /* Reset help_echo_string. It will get recomputed below. */
20799 help_echo_string = Qnil;
20800
20801 /* Convert to window-relative pixel coordinates. */
20802 w = XWINDOW (window);
20803 frame_to_window_pixel_xy (w, &x, &y);
20804
20805 /* Handle tool-bar window differently since it doesn't display a
20806 buffer. */
20807 if (EQ (window, f->tool_bar_window))
20808 {
20809 note_tool_bar_highlight (f, x, y);
20810 return;
20811 }
20812
20813 /* Mouse is on the mode, header line or margin? */
20814 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20815 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20816 {
20817 note_mode_line_or_margin_highlight (w, x, y, part);
20818 return;
20819 }
20820
20821 if (part == ON_VERTICAL_BORDER)
20822 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20823 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20824 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20825 else
20826 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20827
20828 /* Are we in a window whose display is up to date?
20829 And verify the buffer's text has not changed. */
20830 b = XBUFFER (w->buffer);
20831 if (part == ON_TEXT
20832 && EQ (w->window_end_valid, w->buffer)
20833 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20834 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20835 {
20836 int hpos, vpos, pos, i, dx, dy, area;
20837 struct glyph *glyph;
20838 Lisp_Object object;
20839 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20840 Lisp_Object *overlay_vec = NULL;
20841 int len, noverlays;
20842 struct buffer *obuf;
20843 int obegv, ozv, same_region;
20844
20845 /* Find the glyph under X/Y. */
20846 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20847
20848 /* Look for :pointer property on image. */
20849 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20850 {
20851 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20852 if (img != NULL && IMAGEP (img->spec))
20853 {
20854 Lisp_Object image_map, hotspot;
20855 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20856 !NILP (image_map))
20857 && (hotspot = find_hot_spot (image_map, dx, dy),
20858 CONSP (hotspot))
20859 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20860 {
20861 Lisp_Object area_id, plist;
20862
20863 area_id = XCAR (hotspot);
20864 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20865 If so, we could look for mouse-enter, mouse-leave
20866 properties in PLIST (and do something...). */
20867 if ((plist = XCDR (hotspot), CONSP (plist)))
20868 {
20869 pointer = Fplist_get (plist, Qpointer);
20870 if (NILP (pointer))
20871 pointer = Qhand;
20872 help_echo_string = Fplist_get (plist, Qhelp_echo);
20873 if (!NILP (help_echo_string))
20874 {
20875 help_echo_window = window;
20876 help_echo_object = glyph->object;
20877 help_echo_pos = glyph->charpos;
20878 }
20879 }
20880 }
20881 if (NILP (pointer))
20882 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20883 }
20884 }
20885
20886 /* Clear mouse face if X/Y not over text. */
20887 if (glyph == NULL
20888 || area != TEXT_AREA
20889 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20890 {
20891 if (clear_mouse_face (dpyinfo))
20892 cursor = No_Cursor;
20893 if (NILP (pointer))
20894 {
20895 if (area != TEXT_AREA)
20896 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20897 else
20898 pointer = Vvoid_text_area_pointer;
20899 }
20900 goto set_cursor;
20901 }
20902
20903 pos = glyph->charpos;
20904 object = glyph->object;
20905 if (!STRINGP (object) && !BUFFERP (object))
20906 goto set_cursor;
20907
20908 /* If we get an out-of-range value, return now; avoid an error. */
20909 if (BUFFERP (object) && pos > BUF_Z (b))
20910 goto set_cursor;
20911
20912 /* Make the window's buffer temporarily current for
20913 overlays_at and compute_char_face. */
20914 obuf = current_buffer;
20915 current_buffer = b;
20916 obegv = BEGV;
20917 ozv = ZV;
20918 BEGV = BEG;
20919 ZV = Z;
20920
20921 /* Is this char mouse-active or does it have help-echo? */
20922 position = make_number (pos);
20923
20924 if (BUFFERP (object))
20925 {
20926 /* Put all the overlays we want in a vector in overlay_vec.
20927 Store the length in len. If there are more than 10, make
20928 enough space for all, and try again. */
20929 len = 10;
20930 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20931 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20932 if (noverlays > len)
20933 {
20934 len = noverlays;
20935 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20936 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20937 }
20938
20939 /* Sort overlays into increasing priority order. */
20940 noverlays = sort_overlays (overlay_vec, noverlays, w);
20941 }
20942 else
20943 noverlays = 0;
20944
20945 same_region = (EQ (window, dpyinfo->mouse_face_window)
20946 && vpos >= dpyinfo->mouse_face_beg_row
20947 && vpos <= dpyinfo->mouse_face_end_row
20948 && (vpos > dpyinfo->mouse_face_beg_row
20949 || hpos >= dpyinfo->mouse_face_beg_col)
20950 && (vpos < dpyinfo->mouse_face_end_row
20951 || hpos < dpyinfo->mouse_face_end_col
20952 || dpyinfo->mouse_face_past_end));
20953
20954 if (same_region)
20955 cursor = No_Cursor;
20956
20957 /* Check mouse-face highlighting. */
20958 if (! same_region
20959 /* If there exists an overlay with mouse-face overlapping
20960 the one we are currently highlighting, we have to
20961 check if we enter the overlapping overlay, and then
20962 highlight only that. */
20963 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20964 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20965 {
20966 /* Find the highest priority overlay that has a mouse-face
20967 property. */
20968 overlay = Qnil;
20969 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20970 {
20971 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20972 if (!NILP (mouse_face))
20973 overlay = overlay_vec[i];
20974 }
20975
20976 /* If we're actually highlighting the same overlay as
20977 before, there's no need to do that again. */
20978 if (!NILP (overlay)
20979 && EQ (overlay, dpyinfo->mouse_face_overlay))
20980 goto check_help_echo;
20981
20982 dpyinfo->mouse_face_overlay = overlay;
20983
20984 /* Clear the display of the old active region, if any. */
20985 if (clear_mouse_face (dpyinfo))
20986 cursor = No_Cursor;
20987
20988 /* If no overlay applies, get a text property. */
20989 if (NILP (overlay))
20990 mouse_face = Fget_text_property (position, Qmouse_face, object);
20991
20992 /* Handle the overlay case. */
20993 if (!NILP (overlay))
20994 {
20995 /* Find the range of text around this char that
20996 should be active. */
20997 Lisp_Object before, after;
20998 int ignore;
20999
21000 before = Foverlay_start (overlay);
21001 after = Foverlay_end (overlay);
21002 /* Record this as the current active region. */
21003 fast_find_position (w, XFASTINT (before),
21004 &dpyinfo->mouse_face_beg_col,
21005 &dpyinfo->mouse_face_beg_row,
21006 &dpyinfo->mouse_face_beg_x,
21007 &dpyinfo->mouse_face_beg_y, Qnil);
21008
21009 dpyinfo->mouse_face_past_end
21010 = !fast_find_position (w, XFASTINT (after),
21011 &dpyinfo->mouse_face_end_col,
21012 &dpyinfo->mouse_face_end_row,
21013 &dpyinfo->mouse_face_end_x,
21014 &dpyinfo->mouse_face_end_y, Qnil);
21015 dpyinfo->mouse_face_window = window;
21016
21017 dpyinfo->mouse_face_face_id
21018 = face_at_buffer_position (w, pos, 0, 0,
21019 &ignore, pos + 1,
21020 !dpyinfo->mouse_face_hidden);
21021
21022 /* Display it as active. */
21023 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21024 cursor = No_Cursor;
21025 }
21026 /* Handle the text property case. */
21027 else if (!NILP (mouse_face) && BUFFERP (object))
21028 {
21029 /* Find the range of text around this char that
21030 should be active. */
21031 Lisp_Object before, after, beginning, end;
21032 int ignore;
21033
21034 beginning = Fmarker_position (w->start);
21035 end = make_number (BUF_Z (XBUFFER (object))
21036 - XFASTINT (w->window_end_pos));
21037 before
21038 = Fprevious_single_property_change (make_number (pos + 1),
21039 Qmouse_face,
21040 object, beginning);
21041 after
21042 = Fnext_single_property_change (position, Qmouse_face,
21043 object, end);
21044
21045 /* Record this as the current active region. */
21046 fast_find_position (w, XFASTINT (before),
21047 &dpyinfo->mouse_face_beg_col,
21048 &dpyinfo->mouse_face_beg_row,
21049 &dpyinfo->mouse_face_beg_x,
21050 &dpyinfo->mouse_face_beg_y, Qnil);
21051 dpyinfo->mouse_face_past_end
21052 = !fast_find_position (w, XFASTINT (after),
21053 &dpyinfo->mouse_face_end_col,
21054 &dpyinfo->mouse_face_end_row,
21055 &dpyinfo->mouse_face_end_x,
21056 &dpyinfo->mouse_face_end_y, Qnil);
21057 dpyinfo->mouse_face_window = window;
21058
21059 if (BUFFERP (object))
21060 dpyinfo->mouse_face_face_id
21061 = face_at_buffer_position (w, pos, 0, 0,
21062 &ignore, pos + 1,
21063 !dpyinfo->mouse_face_hidden);
21064
21065 /* Display it as active. */
21066 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21067 cursor = No_Cursor;
21068 }
21069 else if (!NILP (mouse_face) && STRINGP (object))
21070 {
21071 Lisp_Object b, e;
21072 int ignore;
21073
21074 b = Fprevious_single_property_change (make_number (pos + 1),
21075 Qmouse_face,
21076 object, Qnil);
21077 e = Fnext_single_property_change (position, Qmouse_face,
21078 object, Qnil);
21079 if (NILP (b))
21080 b = make_number (0);
21081 if (NILP (e))
21082 e = make_number (SCHARS (object) - 1);
21083 fast_find_string_pos (w, XINT (b), object,
21084 &dpyinfo->mouse_face_beg_col,
21085 &dpyinfo->mouse_face_beg_row,
21086 &dpyinfo->mouse_face_beg_x,
21087 &dpyinfo->mouse_face_beg_y, 0);
21088 fast_find_string_pos (w, XINT (e), object,
21089 &dpyinfo->mouse_face_end_col,
21090 &dpyinfo->mouse_face_end_row,
21091 &dpyinfo->mouse_face_end_x,
21092 &dpyinfo->mouse_face_end_y, 1);
21093 dpyinfo->mouse_face_past_end = 0;
21094 dpyinfo->mouse_face_window = window;
21095 dpyinfo->mouse_face_face_id
21096 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21097 glyph->face_id, 1);
21098 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21099 cursor = No_Cursor;
21100 }
21101 else if (STRINGP (object) && NILP (mouse_face))
21102 {
21103 /* A string which doesn't have mouse-face, but
21104 the text ``under'' it might have. */
21105 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21106 int start = MATRIX_ROW_START_CHARPOS (r);
21107
21108 pos = string_buffer_position (w, object, start);
21109 if (pos > 0)
21110 mouse_face = get_char_property_and_overlay (make_number (pos),
21111 Qmouse_face,
21112 w->buffer,
21113 &overlay);
21114 if (!NILP (mouse_face) && !NILP (overlay))
21115 {
21116 Lisp_Object before = Foverlay_start (overlay);
21117 Lisp_Object after = Foverlay_end (overlay);
21118 int ignore;
21119
21120 /* Note that we might not be able to find position
21121 BEFORE in the glyph matrix if the overlay is
21122 entirely covered by a `display' property. In
21123 this case, we overshoot. So let's stop in
21124 the glyph matrix before glyphs for OBJECT. */
21125 fast_find_position (w, XFASTINT (before),
21126 &dpyinfo->mouse_face_beg_col,
21127 &dpyinfo->mouse_face_beg_row,
21128 &dpyinfo->mouse_face_beg_x,
21129 &dpyinfo->mouse_face_beg_y,
21130 object);
21131
21132 dpyinfo->mouse_face_past_end
21133 = !fast_find_position (w, XFASTINT (after),
21134 &dpyinfo->mouse_face_end_col,
21135 &dpyinfo->mouse_face_end_row,
21136 &dpyinfo->mouse_face_end_x,
21137 &dpyinfo->mouse_face_end_y,
21138 Qnil);
21139 dpyinfo->mouse_face_window = window;
21140 dpyinfo->mouse_face_face_id
21141 = face_at_buffer_position (w, pos, 0, 0,
21142 &ignore, pos + 1,
21143 !dpyinfo->mouse_face_hidden);
21144
21145 /* Display it as active. */
21146 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21147 cursor = No_Cursor;
21148 }
21149 }
21150 }
21151
21152 check_help_echo:
21153
21154 /* Look for a `help-echo' property. */
21155 if (NILP (help_echo_string)) {
21156 Lisp_Object help, overlay;
21157
21158 /* Check overlays first. */
21159 help = overlay = Qnil;
21160 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21161 {
21162 overlay = overlay_vec[i];
21163 help = Foverlay_get (overlay, Qhelp_echo);
21164 }
21165
21166 if (!NILP (help))
21167 {
21168 help_echo_string = help;
21169 help_echo_window = window;
21170 help_echo_object = overlay;
21171 help_echo_pos = pos;
21172 }
21173 else
21174 {
21175 Lisp_Object object = glyph->object;
21176 int charpos = glyph->charpos;
21177
21178 /* Try text properties. */
21179 if (STRINGP (object)
21180 && charpos >= 0
21181 && charpos < SCHARS (object))
21182 {
21183 help = Fget_text_property (make_number (charpos),
21184 Qhelp_echo, object);
21185 if (NILP (help))
21186 {
21187 /* If the string itself doesn't specify a help-echo,
21188 see if the buffer text ``under'' it does. */
21189 struct glyph_row *r
21190 = MATRIX_ROW (w->current_matrix, vpos);
21191 int start = MATRIX_ROW_START_CHARPOS (r);
21192 int pos = string_buffer_position (w, object, start);
21193 if (pos > 0)
21194 {
21195 help = Fget_char_property (make_number (pos),
21196 Qhelp_echo, w->buffer);
21197 if (!NILP (help))
21198 {
21199 charpos = pos;
21200 object = w->buffer;
21201 }
21202 }
21203 }
21204 }
21205 else if (BUFFERP (object)
21206 && charpos >= BEGV
21207 && charpos < ZV)
21208 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21209 object);
21210
21211 if (!NILP (help))
21212 {
21213 help_echo_string = help;
21214 help_echo_window = window;
21215 help_echo_object = object;
21216 help_echo_pos = charpos;
21217 }
21218 }
21219 }
21220
21221 /* Look for a `pointer' property. */
21222 if (NILP (pointer))
21223 {
21224 /* Check overlays first. */
21225 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21226 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21227
21228 if (NILP (pointer))
21229 {
21230 Lisp_Object object = glyph->object;
21231 int charpos = glyph->charpos;
21232
21233 /* Try text properties. */
21234 if (STRINGP (object)
21235 && charpos >= 0
21236 && charpos < SCHARS (object))
21237 {
21238 pointer = Fget_text_property (make_number (charpos),
21239 Qpointer, object);
21240 if (NILP (pointer))
21241 {
21242 /* If the string itself doesn't specify a pointer,
21243 see if the buffer text ``under'' it does. */
21244 struct glyph_row *r
21245 = MATRIX_ROW (w->current_matrix, vpos);
21246 int start = MATRIX_ROW_START_CHARPOS (r);
21247 int pos = string_buffer_position (w, object, start);
21248 if (pos > 0)
21249 pointer = Fget_char_property (make_number (pos),
21250 Qpointer, w->buffer);
21251 }
21252 }
21253 else if (BUFFERP (object)
21254 && charpos >= BEGV
21255 && charpos < ZV)
21256 pointer = Fget_text_property (make_number (charpos),
21257 Qpointer, object);
21258 }
21259 }
21260
21261 BEGV = obegv;
21262 ZV = ozv;
21263 current_buffer = obuf;
21264 }
21265
21266 set_cursor:
21267
21268 define_frame_cursor1 (f, cursor, pointer);
21269 }
21270
21271
21272 /* EXPORT for RIF:
21273 Clear any mouse-face on window W. This function is part of the
21274 redisplay interface, and is called from try_window_id and similar
21275 functions to ensure the mouse-highlight is off. */
21276
21277 void
21278 x_clear_window_mouse_face (w)
21279 struct window *w;
21280 {
21281 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21282 Lisp_Object window;
21283
21284 BLOCK_INPUT;
21285 XSETWINDOW (window, w);
21286 if (EQ (window, dpyinfo->mouse_face_window))
21287 clear_mouse_face (dpyinfo);
21288 UNBLOCK_INPUT;
21289 }
21290
21291
21292 /* EXPORT:
21293 Just discard the mouse face information for frame F, if any.
21294 This is used when the size of F is changed. */
21295
21296 void
21297 cancel_mouse_face (f)
21298 struct frame *f;
21299 {
21300 Lisp_Object window;
21301 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21302
21303 window = dpyinfo->mouse_face_window;
21304 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21305 {
21306 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21307 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21308 dpyinfo->mouse_face_window = Qnil;
21309 }
21310 }
21311
21312
21313 #endif /* HAVE_WINDOW_SYSTEM */
21314
21315 \f
21316 /***********************************************************************
21317 Exposure Events
21318 ***********************************************************************/
21319
21320 #ifdef HAVE_WINDOW_SYSTEM
21321
21322 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21323 which intersects rectangle R. R is in window-relative coordinates. */
21324
21325 static void
21326 expose_area (w, row, r, area)
21327 struct window *w;
21328 struct glyph_row *row;
21329 XRectangle *r;
21330 enum glyph_row_area area;
21331 {
21332 struct glyph *first = row->glyphs[area];
21333 struct glyph *end = row->glyphs[area] + row->used[area];
21334 struct glyph *last;
21335 int first_x, start_x, x;
21336
21337 if (area == TEXT_AREA && row->fill_line_p)
21338 /* If row extends face to end of line write the whole line. */
21339 draw_glyphs (w, 0, row, area,
21340 0, row->used[area],
21341 DRAW_NORMAL_TEXT, 0);
21342 else
21343 {
21344 /* Set START_X to the window-relative start position for drawing glyphs of
21345 AREA. The first glyph of the text area can be partially visible.
21346 The first glyphs of other areas cannot. */
21347 start_x = window_box_left_offset (w, area);
21348 x = start_x;
21349 if (area == TEXT_AREA)
21350 x += row->x;
21351
21352 /* Find the first glyph that must be redrawn. */
21353 while (first < end
21354 && x + first->pixel_width < r->x)
21355 {
21356 x += first->pixel_width;
21357 ++first;
21358 }
21359
21360 /* Find the last one. */
21361 last = first;
21362 first_x = x;
21363 while (last < end
21364 && x < r->x + r->width)
21365 {
21366 x += last->pixel_width;
21367 ++last;
21368 }
21369
21370 /* Repaint. */
21371 if (last > first)
21372 draw_glyphs (w, first_x - start_x, row, area,
21373 first - row->glyphs[area], last - row->glyphs[area],
21374 DRAW_NORMAL_TEXT, 0);
21375 }
21376 }
21377
21378
21379 /* Redraw the parts of the glyph row ROW on window W intersecting
21380 rectangle R. R is in window-relative coordinates. Value is
21381 non-zero if mouse-face was overwritten. */
21382
21383 static int
21384 expose_line (w, row, r)
21385 struct window *w;
21386 struct glyph_row *row;
21387 XRectangle *r;
21388 {
21389 xassert (row->enabled_p);
21390
21391 if (row->mode_line_p || w->pseudo_window_p)
21392 draw_glyphs (w, 0, row, TEXT_AREA,
21393 0, row->used[TEXT_AREA],
21394 DRAW_NORMAL_TEXT, 0);
21395 else
21396 {
21397 if (row->used[LEFT_MARGIN_AREA])
21398 expose_area (w, row, r, LEFT_MARGIN_AREA);
21399 if (row->used[TEXT_AREA])
21400 expose_area (w, row, r, TEXT_AREA);
21401 if (row->used[RIGHT_MARGIN_AREA])
21402 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21403 draw_row_fringe_bitmaps (w, row);
21404 }
21405
21406 return row->mouse_face_p;
21407 }
21408
21409
21410 /* Redraw those parts of glyphs rows during expose event handling that
21411 overlap other rows. Redrawing of an exposed line writes over parts
21412 of lines overlapping that exposed line; this function fixes that.
21413
21414 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21415 row in W's current matrix that is exposed and overlaps other rows.
21416 LAST_OVERLAPPING_ROW is the last such row. */
21417
21418 static void
21419 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21420 struct window *w;
21421 struct glyph_row *first_overlapping_row;
21422 struct glyph_row *last_overlapping_row;
21423 {
21424 struct glyph_row *row;
21425
21426 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21427 if (row->overlapping_p)
21428 {
21429 xassert (row->enabled_p && !row->mode_line_p);
21430
21431 if (row->used[LEFT_MARGIN_AREA])
21432 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21433
21434 if (row->used[TEXT_AREA])
21435 x_fix_overlapping_area (w, row, TEXT_AREA);
21436
21437 if (row->used[RIGHT_MARGIN_AREA])
21438 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21439 }
21440 }
21441
21442
21443 /* Return non-zero if W's cursor intersects rectangle R. */
21444
21445 static int
21446 phys_cursor_in_rect_p (w, r)
21447 struct window *w;
21448 XRectangle *r;
21449 {
21450 XRectangle cr, result;
21451 struct glyph *cursor_glyph;
21452
21453 cursor_glyph = get_phys_cursor_glyph (w);
21454 if (cursor_glyph)
21455 {
21456 /* r is relative to W's box, but w->phys_cursor.x is relative
21457 to left edge of W's TEXT area. Adjust it. */
21458 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21459 cr.y = w->phys_cursor.y;
21460 cr.width = cursor_glyph->pixel_width;
21461 cr.height = w->phys_cursor_height;
21462 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21463 I assume the effect is the same -- and this is portable. */
21464 return x_intersect_rectangles (&cr, r, &result);
21465 }
21466 else
21467 return 0;
21468 }
21469
21470
21471 /* EXPORT:
21472 Draw a vertical window border to the right of window W if W doesn't
21473 have vertical scroll bars. */
21474
21475 void
21476 x_draw_vertical_border (w)
21477 struct window *w;
21478 {
21479 struct frame *f = XFRAME (WINDOW_FRAME (w));
21480
21481 /* We could do better, if we knew what type of scroll-bar the adjacent
21482 windows (on either side) have... But we don't :-(
21483 However, I think this works ok. ++KFS 2003-04-25 */
21484
21485 /* Redraw borders between horizontally adjacent windows. Don't
21486 do it for frames with vertical scroll bars because either the
21487 right scroll bar of a window, or the left scroll bar of its
21488 neighbor will suffice as a border. */
21489 if (!WINDOW_RIGHTMOST_P (w)
21490 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21491 {
21492 int x0, x1, y0, y1;
21493
21494 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21495 y1 -= 1;
21496
21497 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21498 }
21499 else if (!WINDOW_LEFTMOST_P (w)
21500 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21501 {
21502 int x0, x1, y0, y1;
21503
21504 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21505 y1 -= 1;
21506
21507 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21508 }
21509 }
21510
21511
21512 /* Redraw the part of window W intersection rectangle FR. Pixel
21513 coordinates in FR are frame-relative. Call this function with
21514 input blocked. Value is non-zero if the exposure overwrites
21515 mouse-face. */
21516
21517 static int
21518 expose_window (w, fr)
21519 struct window *w;
21520 XRectangle *fr;
21521 {
21522 struct frame *f = XFRAME (w->frame);
21523 XRectangle wr, r;
21524 int mouse_face_overwritten_p = 0;
21525
21526 /* If window is not yet fully initialized, do nothing. This can
21527 happen when toolkit scroll bars are used and a window is split.
21528 Reconfiguring the scroll bar will generate an expose for a newly
21529 created window. */
21530 if (w->current_matrix == NULL)
21531 return 0;
21532
21533 /* When we're currently updating the window, display and current
21534 matrix usually don't agree. Arrange for a thorough display
21535 later. */
21536 if (w == updated_window)
21537 {
21538 SET_FRAME_GARBAGED (f);
21539 return 0;
21540 }
21541
21542 /* Frame-relative pixel rectangle of W. */
21543 wr.x = WINDOW_LEFT_EDGE_X (w);
21544 wr.y = WINDOW_TOP_EDGE_Y (w);
21545 wr.width = WINDOW_TOTAL_WIDTH (w);
21546 wr.height = WINDOW_TOTAL_HEIGHT (w);
21547
21548 if (x_intersect_rectangles (fr, &wr, &r))
21549 {
21550 int yb = window_text_bottom_y (w);
21551 struct glyph_row *row;
21552 int cursor_cleared_p;
21553 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21554
21555 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21556 r.x, r.y, r.width, r.height));
21557
21558 /* Convert to window coordinates. */
21559 r.x -= WINDOW_LEFT_EDGE_X (w);
21560 r.y -= WINDOW_TOP_EDGE_Y (w);
21561
21562 /* Turn off the cursor. */
21563 if (!w->pseudo_window_p
21564 && phys_cursor_in_rect_p (w, &r))
21565 {
21566 x_clear_cursor (w);
21567 cursor_cleared_p = 1;
21568 }
21569 else
21570 cursor_cleared_p = 0;
21571
21572 /* Update lines intersecting rectangle R. */
21573 first_overlapping_row = last_overlapping_row = NULL;
21574 for (row = w->current_matrix->rows;
21575 row->enabled_p;
21576 ++row)
21577 {
21578 int y0 = row->y;
21579 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21580
21581 if ((y0 >= r.y && y0 < r.y + r.height)
21582 || (y1 > r.y && y1 < r.y + r.height)
21583 || (r.y >= y0 && r.y < y1)
21584 || (r.y + r.height > y0 && r.y + r.height < y1))
21585 {
21586 if (row->overlapping_p)
21587 {
21588 if (first_overlapping_row == NULL)
21589 first_overlapping_row = row;
21590 last_overlapping_row = row;
21591 }
21592
21593 if (expose_line (w, row, &r))
21594 mouse_face_overwritten_p = 1;
21595 }
21596
21597 if (y1 >= yb)
21598 break;
21599 }
21600
21601 /* Display the mode line if there is one. */
21602 if (WINDOW_WANTS_MODELINE_P (w)
21603 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21604 row->enabled_p)
21605 && row->y < r.y + r.height)
21606 {
21607 if (expose_line (w, row, &r))
21608 mouse_face_overwritten_p = 1;
21609 }
21610
21611 if (!w->pseudo_window_p)
21612 {
21613 /* Fix the display of overlapping rows. */
21614 if (first_overlapping_row)
21615 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21616
21617 /* Draw border between windows. */
21618 x_draw_vertical_border (w);
21619
21620 /* Turn the cursor on again. */
21621 if (cursor_cleared_p)
21622 update_window_cursor (w, 1);
21623 }
21624 }
21625
21626 #ifdef HAVE_CARBON
21627 /* Display scroll bar for this window. */
21628 if (!NILP (w->vertical_scroll_bar))
21629 {
21630 /* ++KFS:
21631 If this doesn't work here (maybe some header files are missing),
21632 make a function in macterm.c and call it to do the job! */
21633 ControlHandle ch
21634 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21635
21636 Draw1Control (ch);
21637 }
21638 #endif
21639
21640 return mouse_face_overwritten_p;
21641 }
21642
21643
21644
21645 /* Redraw (parts) of all windows in the window tree rooted at W that
21646 intersect R. R contains frame pixel coordinates. Value is
21647 non-zero if the exposure overwrites mouse-face. */
21648
21649 static int
21650 expose_window_tree (w, r)
21651 struct window *w;
21652 XRectangle *r;
21653 {
21654 struct frame *f = XFRAME (w->frame);
21655 int mouse_face_overwritten_p = 0;
21656
21657 while (w && !FRAME_GARBAGED_P (f))
21658 {
21659 if (!NILP (w->hchild))
21660 mouse_face_overwritten_p
21661 |= expose_window_tree (XWINDOW (w->hchild), r);
21662 else if (!NILP (w->vchild))
21663 mouse_face_overwritten_p
21664 |= expose_window_tree (XWINDOW (w->vchild), r);
21665 else
21666 mouse_face_overwritten_p |= expose_window (w, r);
21667
21668 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21669 }
21670
21671 return mouse_face_overwritten_p;
21672 }
21673
21674
21675 /* EXPORT:
21676 Redisplay an exposed area of frame F. X and Y are the upper-left
21677 corner of the exposed rectangle. W and H are width and height of
21678 the exposed area. All are pixel values. W or H zero means redraw
21679 the entire frame. */
21680
21681 void
21682 expose_frame (f, x, y, w, h)
21683 struct frame *f;
21684 int x, y, w, h;
21685 {
21686 XRectangle r;
21687 int mouse_face_overwritten_p = 0;
21688
21689 TRACE ((stderr, "expose_frame "));
21690
21691 /* No need to redraw if frame will be redrawn soon. */
21692 if (FRAME_GARBAGED_P (f))
21693 {
21694 TRACE ((stderr, " garbaged\n"));
21695 return;
21696 }
21697
21698 #ifdef HAVE_CARBON
21699 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21700 or deactivated here, for unknown reasons, activated scroll bars
21701 are shown in deactivated frames in some instances. */
21702 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21703 activate_scroll_bars (f);
21704 else
21705 deactivate_scroll_bars (f);
21706 #endif
21707
21708 /* If basic faces haven't been realized yet, there is no point in
21709 trying to redraw anything. This can happen when we get an expose
21710 event while Emacs is starting, e.g. by moving another window. */
21711 if (FRAME_FACE_CACHE (f) == NULL
21712 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21713 {
21714 TRACE ((stderr, " no faces\n"));
21715 return;
21716 }
21717
21718 if (w == 0 || h == 0)
21719 {
21720 r.x = r.y = 0;
21721 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21722 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21723 }
21724 else
21725 {
21726 r.x = x;
21727 r.y = y;
21728 r.width = w;
21729 r.height = h;
21730 }
21731
21732 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21733 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21734
21735 if (WINDOWP (f->tool_bar_window))
21736 mouse_face_overwritten_p
21737 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21738
21739 #ifdef HAVE_X_WINDOWS
21740 #ifndef MSDOS
21741 #ifndef USE_X_TOOLKIT
21742 if (WINDOWP (f->menu_bar_window))
21743 mouse_face_overwritten_p
21744 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21745 #endif /* not USE_X_TOOLKIT */
21746 #endif
21747 #endif
21748
21749 /* Some window managers support a focus-follows-mouse style with
21750 delayed raising of frames. Imagine a partially obscured frame,
21751 and moving the mouse into partially obscured mouse-face on that
21752 frame. The visible part of the mouse-face will be highlighted,
21753 then the WM raises the obscured frame. With at least one WM, KDE
21754 2.1, Emacs is not getting any event for the raising of the frame
21755 (even tried with SubstructureRedirectMask), only Expose events.
21756 These expose events will draw text normally, i.e. not
21757 highlighted. Which means we must redo the highlight here.
21758 Subsume it under ``we love X''. --gerd 2001-08-15 */
21759 /* Included in Windows version because Windows most likely does not
21760 do the right thing if any third party tool offers
21761 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21762 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21763 {
21764 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21765 if (f == dpyinfo->mouse_face_mouse_frame)
21766 {
21767 int x = dpyinfo->mouse_face_mouse_x;
21768 int y = dpyinfo->mouse_face_mouse_y;
21769 clear_mouse_face (dpyinfo);
21770 note_mouse_highlight (f, x, y);
21771 }
21772 }
21773 }
21774
21775
21776 /* EXPORT:
21777 Determine the intersection of two rectangles R1 and R2. Return
21778 the intersection in *RESULT. Value is non-zero if RESULT is not
21779 empty. */
21780
21781 int
21782 x_intersect_rectangles (r1, r2, result)
21783 XRectangle *r1, *r2, *result;
21784 {
21785 XRectangle *left, *right;
21786 XRectangle *upper, *lower;
21787 int intersection_p = 0;
21788
21789 /* Rearrange so that R1 is the left-most rectangle. */
21790 if (r1->x < r2->x)
21791 left = r1, right = r2;
21792 else
21793 left = r2, right = r1;
21794
21795 /* X0 of the intersection is right.x0, if this is inside R1,
21796 otherwise there is no intersection. */
21797 if (right->x <= left->x + left->width)
21798 {
21799 result->x = right->x;
21800
21801 /* The right end of the intersection is the minimum of the
21802 the right ends of left and right. */
21803 result->width = (min (left->x + left->width, right->x + right->width)
21804 - result->x);
21805
21806 /* Same game for Y. */
21807 if (r1->y < r2->y)
21808 upper = r1, lower = r2;
21809 else
21810 upper = r2, lower = r1;
21811
21812 /* The upper end of the intersection is lower.y0, if this is inside
21813 of upper. Otherwise, there is no intersection. */
21814 if (lower->y <= upper->y + upper->height)
21815 {
21816 result->y = lower->y;
21817
21818 /* The lower end of the intersection is the minimum of the lower
21819 ends of upper and lower. */
21820 result->height = (min (lower->y + lower->height,
21821 upper->y + upper->height)
21822 - result->y);
21823 intersection_p = 1;
21824 }
21825 }
21826
21827 return intersection_p;
21828 }
21829
21830 #endif /* HAVE_WINDOW_SYSTEM */
21831
21832 \f
21833 /***********************************************************************
21834 Initialization
21835 ***********************************************************************/
21836
21837 void
21838 syms_of_xdisp ()
21839 {
21840 Vwith_echo_area_save_vector = Qnil;
21841 staticpro (&Vwith_echo_area_save_vector);
21842
21843 Vmessage_stack = Qnil;
21844 staticpro (&Vmessage_stack);
21845
21846 Qinhibit_redisplay = intern ("inhibit-redisplay");
21847 staticpro (&Qinhibit_redisplay);
21848
21849 message_dolog_marker1 = Fmake_marker ();
21850 staticpro (&message_dolog_marker1);
21851 message_dolog_marker2 = Fmake_marker ();
21852 staticpro (&message_dolog_marker2);
21853 message_dolog_marker3 = Fmake_marker ();
21854 staticpro (&message_dolog_marker3);
21855
21856 #if GLYPH_DEBUG
21857 defsubr (&Sdump_frame_glyph_matrix);
21858 defsubr (&Sdump_glyph_matrix);
21859 defsubr (&Sdump_glyph_row);
21860 defsubr (&Sdump_tool_bar_row);
21861 defsubr (&Strace_redisplay);
21862 defsubr (&Strace_to_stderr);
21863 #endif
21864 #ifdef HAVE_WINDOW_SYSTEM
21865 defsubr (&Stool_bar_lines_needed);
21866 defsubr (&Slookup_image_map);
21867 #endif
21868 defsubr (&Sformat_mode_line);
21869
21870 staticpro (&Qmenu_bar_update_hook);
21871 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21872
21873 staticpro (&Qoverriding_terminal_local_map);
21874 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21875
21876 staticpro (&Qoverriding_local_map);
21877 Qoverriding_local_map = intern ("overriding-local-map");
21878
21879 staticpro (&Qwindow_scroll_functions);
21880 Qwindow_scroll_functions = intern ("window-scroll-functions");
21881
21882 staticpro (&Qredisplay_end_trigger_functions);
21883 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21884
21885 staticpro (&Qinhibit_point_motion_hooks);
21886 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21887
21888 QCdata = intern (":data");
21889 staticpro (&QCdata);
21890 Qdisplay = intern ("display");
21891 staticpro (&Qdisplay);
21892 Qspace_width = intern ("space-width");
21893 staticpro (&Qspace_width);
21894 Qraise = intern ("raise");
21895 staticpro (&Qraise);
21896 Qspace = intern ("space");
21897 staticpro (&Qspace);
21898 Qmargin = intern ("margin");
21899 staticpro (&Qmargin);
21900 Qpointer = intern ("pointer");
21901 staticpro (&Qpointer);
21902 Qleft_margin = intern ("left-margin");
21903 staticpro (&Qleft_margin);
21904 Qright_margin = intern ("right-margin");
21905 staticpro (&Qright_margin);
21906 QCalign_to = intern (":align-to");
21907 staticpro (&QCalign_to);
21908 QCrelative_width = intern (":relative-width");
21909 staticpro (&QCrelative_width);
21910 QCrelative_height = intern (":relative-height");
21911 staticpro (&QCrelative_height);
21912 QCeval = intern (":eval");
21913 staticpro (&QCeval);
21914 QCpropertize = intern (":propertize");
21915 staticpro (&QCpropertize);
21916 QCfile = intern (":file");
21917 staticpro (&QCfile);
21918 Qfontified = intern ("fontified");
21919 staticpro (&Qfontified);
21920 Qfontification_functions = intern ("fontification-functions");
21921 staticpro (&Qfontification_functions);
21922 Qtrailing_whitespace = intern ("trailing-whitespace");
21923 staticpro (&Qtrailing_whitespace);
21924 Qimage = intern ("image");
21925 staticpro (&Qimage);
21926 QCmap = intern (":map");
21927 staticpro (&QCmap);
21928 QCpointer = intern (":pointer");
21929 staticpro (&QCpointer);
21930 Qrect = intern ("rect");
21931 staticpro (&Qrect);
21932 Qcircle = intern ("circle");
21933 staticpro (&Qcircle);
21934 Qpoly = intern ("poly");
21935 staticpro (&Qpoly);
21936 Qmessage_truncate_lines = intern ("message-truncate-lines");
21937 staticpro (&Qmessage_truncate_lines);
21938 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21939 staticpro (&Qcursor_in_non_selected_windows);
21940 Qgrow_only = intern ("grow-only");
21941 staticpro (&Qgrow_only);
21942 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21943 staticpro (&Qinhibit_menubar_update);
21944 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21945 staticpro (&Qinhibit_eval_during_redisplay);
21946 Qposition = intern ("position");
21947 staticpro (&Qposition);
21948 Qbuffer_position = intern ("buffer-position");
21949 staticpro (&Qbuffer_position);
21950 Qobject = intern ("object");
21951 staticpro (&Qobject);
21952 Qbar = intern ("bar");
21953 staticpro (&Qbar);
21954 Qhbar = intern ("hbar");
21955 staticpro (&Qhbar);
21956 Qbox = intern ("box");
21957 staticpro (&Qbox);
21958 Qhollow = intern ("hollow");
21959 staticpro (&Qhollow);
21960 Qhand = intern ("hand");
21961 staticpro (&Qhand);
21962 Qarrow = intern ("arrow");
21963 staticpro (&Qarrow);
21964 Qtext = intern ("text");
21965 staticpro (&Qtext);
21966 Qrisky_local_variable = intern ("risky-local-variable");
21967 staticpro (&Qrisky_local_variable);
21968 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21969 staticpro (&Qinhibit_free_realized_faces);
21970
21971 list_of_error = Fcons (intern ("error"), Qnil);
21972 staticpro (&list_of_error);
21973
21974 last_arrow_position = Qnil;
21975 last_arrow_string = Qnil;
21976 staticpro (&last_arrow_position);
21977 staticpro (&last_arrow_string);
21978
21979 echo_buffer[0] = echo_buffer[1] = Qnil;
21980 staticpro (&echo_buffer[0]);
21981 staticpro (&echo_buffer[1]);
21982
21983 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21984 staticpro (&echo_area_buffer[0]);
21985 staticpro (&echo_area_buffer[1]);
21986
21987 Vmessages_buffer_name = build_string ("*Messages*");
21988 staticpro (&Vmessages_buffer_name);
21989
21990 mode_line_proptrans_alist = Qnil;
21991 staticpro (&mode_line_proptrans_alist);
21992
21993 mode_line_string_list = Qnil;
21994 staticpro (&mode_line_string_list);
21995
21996 help_echo_string = Qnil;
21997 staticpro (&help_echo_string);
21998 help_echo_object = Qnil;
21999 staticpro (&help_echo_object);
22000 help_echo_window = Qnil;
22001 staticpro (&help_echo_window);
22002 previous_help_echo_string = Qnil;
22003 staticpro (&previous_help_echo_string);
22004 help_echo_pos = -1;
22005
22006 #ifdef HAVE_WINDOW_SYSTEM
22007 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22008 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22009 For example, if a block cursor is over a tab, it will be drawn as
22010 wide as that tab on the display. */);
22011 x_stretch_cursor_p = 0;
22012 #endif
22013
22014 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22015 doc: /* *Non-nil means highlight trailing whitespace.
22016 The face used for trailing whitespace is `trailing-whitespace'. */);
22017 Vshow_trailing_whitespace = Qnil;
22018
22019 #ifdef HAVE_WINDOW_SYSTEM
22020 DEFVAR_LISP ("overflow-newline-into-fringe", &Voverflow_newline_into_fringe,
22021 doc: /* *Non-nil means that newline may flow into the right fringe.
22022 This means that display lines which are exactly as wide as the window
22023 (not counting the final newline) will only occupy one screen line, by
22024 showing (or hiding) the final newline in the right fringe; when point
22025 is at the final newline, the cursor is shown in the right fringe.
22026 If nil, also continue lines which are exactly as wide as the window. */);
22027 Voverflow_newline_into_fringe = Qt;
22028 #endif
22029
22030 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22031 doc: /* *The pointer shape to show in void text areas.
22032 Nil means to show the text pointer. Other options are `arrow', `text',
22033 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22034 Vvoid_text_area_pointer = Qarrow;
22035
22036 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22037 doc: /* Non-nil means don't actually do any redisplay.
22038 This is used for internal purposes. */);
22039 Vinhibit_redisplay = Qnil;
22040
22041 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22042 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22043 Vglobal_mode_string = Qnil;
22044
22045 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22046 doc: /* Marker for where to display an arrow on top of the buffer text.
22047 This must be the beginning of a line in order to work.
22048 See also `overlay-arrow-string'. */);
22049 Voverlay_arrow_position = Qnil;
22050
22051 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22052 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
22053 Voverlay_arrow_string = Qnil;
22054
22055 DEFVAR_INT ("scroll-step", &scroll_step,
22056 doc: /* *The number of lines to try scrolling a window by when point moves out.
22057 If that fails to bring point back on frame, point is centered instead.
22058 If this is zero, point is always centered after it moves off frame.
22059 If you want scrolling to always be a line at a time, you should set
22060 `scroll-conservatively' to a large value rather than set this to 1. */);
22061
22062 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22063 doc: /* *Scroll up to this many lines, to bring point back on screen.
22064 A value of zero means to scroll the text to center point vertically
22065 in the window. */);
22066 scroll_conservatively = 0;
22067
22068 DEFVAR_INT ("scroll-margin", &scroll_margin,
22069 doc: /* *Number of lines of margin at the top and bottom of a window.
22070 Recenter the window whenever point gets within this many lines
22071 of the top or bottom of the window. */);
22072 scroll_margin = 0;
22073
22074 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22075 doc: /* Pixels per inch on current display.
22076 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22077 Vdisplay_pixels_per_inch = make_float (72.0);
22078
22079 #if GLYPH_DEBUG
22080 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22081 #endif
22082
22083 DEFVAR_BOOL ("truncate-partial-width-windows",
22084 &truncate_partial_width_windows,
22085 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22086 truncate_partial_width_windows = 1;
22087
22088 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22089 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22090 Any other value means to use the appropriate face, `mode-line',
22091 `header-line', or `menu' respectively. */);
22092 mode_line_inverse_video = 1;
22093
22094 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22095 doc: /* *Maximum buffer size for which line number should be displayed.
22096 If the buffer is bigger than this, the line number does not appear
22097 in the mode line. A value of nil means no limit. */);
22098 Vline_number_display_limit = Qnil;
22099
22100 DEFVAR_INT ("line-number-display-limit-width",
22101 &line_number_display_limit_width,
22102 doc: /* *Maximum line width (in characters) for line number display.
22103 If the average length of the lines near point is bigger than this, then the
22104 line number may be omitted from the mode line. */);
22105 line_number_display_limit_width = 200;
22106
22107 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22108 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22109 highlight_nonselected_windows = 0;
22110
22111 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22112 doc: /* Non-nil if more than one frame is visible on this display.
22113 Minibuffer-only frames don't count, but iconified frames do.
22114 This variable is not guaranteed to be accurate except while processing
22115 `frame-title-format' and `icon-title-format'. */);
22116
22117 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22118 doc: /* Template for displaying the title bar of visible frames.
22119 \(Assuming the window manager supports this feature.)
22120 This variable has the same structure as `mode-line-format' (which see),
22121 and is used only on frames for which no explicit name has been set
22122 \(see `modify-frame-parameters'). */);
22123
22124 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22125 doc: /* Template for displaying the title bar of an iconified frame.
22126 \(Assuming the window manager supports this feature.)
22127 This variable has the same structure as `mode-line-format' (which see),
22128 and is used only on frames for which no explicit name has been set
22129 \(see `modify-frame-parameters'). */);
22130 Vicon_title_format
22131 = Vframe_title_format
22132 = Fcons (intern ("multiple-frames"),
22133 Fcons (build_string ("%b"),
22134 Fcons (Fcons (empty_string,
22135 Fcons (intern ("invocation-name"),
22136 Fcons (build_string ("@"),
22137 Fcons (intern ("system-name"),
22138 Qnil)))),
22139 Qnil)));
22140
22141 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22142 doc: /* Maximum number of lines to keep in the message log buffer.
22143 If nil, disable message logging. If t, log messages but don't truncate
22144 the buffer when it becomes large. */);
22145 Vmessage_log_max = make_number (50);
22146
22147 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22148 doc: /* Functions called before redisplay, if window sizes have changed.
22149 The value should be a list of functions that take one argument.
22150 Just before redisplay, for each frame, if any of its windows have changed
22151 size since the last redisplay, or have been split or deleted,
22152 all the functions in the list are called, with the frame as argument. */);
22153 Vwindow_size_change_functions = Qnil;
22154
22155 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22156 doc: /* List of Functions to call before redisplaying a window with scrolling.
22157 Each function is called with two arguments, the window
22158 and its new display-start position. Note that the value of `window-end'
22159 is not valid when these functions are called. */);
22160 Vwindow_scroll_functions = Qnil;
22161
22162 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22163 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22164 mouse_autoselect_window = 0;
22165
22166 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22167 doc: /* *Non-nil means automatically resize tool-bars.
22168 This increases a tool-bar's height if not all tool-bar items are visible.
22169 It decreases a tool-bar's height when it would display blank lines
22170 otherwise. */);
22171 auto_resize_tool_bars_p = 1;
22172
22173 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22174 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22175 auto_raise_tool_bar_buttons_p = 1;
22176
22177 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22178 doc: /* *Margin around tool-bar buttons in pixels.
22179 If an integer, use that for both horizontal and vertical margins.
22180 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22181 HORZ specifying the horizontal margin, and VERT specifying the
22182 vertical margin. */);
22183 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22184
22185 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22186 doc: /* *Relief thickness of tool-bar buttons. */);
22187 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22188
22189 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22190 doc: /* List of functions to call to fontify regions of text.
22191 Each function is called with one argument POS. Functions must
22192 fontify a region starting at POS in the current buffer, and give
22193 fontified regions the property `fontified'. */);
22194 Vfontification_functions = Qnil;
22195 Fmake_variable_buffer_local (Qfontification_functions);
22196
22197 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22198 &unibyte_display_via_language_environment,
22199 doc: /* *Non-nil means display unibyte text according to language environment.
22200 Specifically this means that unibyte non-ASCII characters
22201 are displayed by converting them to the equivalent multibyte characters
22202 according to the current language environment. As a result, they are
22203 displayed according to the current fontset. */);
22204 unibyte_display_via_language_environment = 0;
22205
22206 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22207 doc: /* *Maximum height for resizing mini-windows.
22208 If a float, it specifies a fraction of the mini-window frame's height.
22209 If an integer, it specifies a number of lines. */);
22210 Vmax_mini_window_height = make_float (0.25);
22211
22212 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22213 doc: /* *How to resize mini-windows.
22214 A value of nil means don't automatically resize mini-windows.
22215 A value of t means resize them to fit the text displayed in them.
22216 A value of `grow-only', the default, means let mini-windows grow
22217 only, until their display becomes empty, at which point the windows
22218 go back to their normal size. */);
22219 Vresize_mini_windows = Qgrow_only;
22220
22221 DEFVAR_LISP ("cursor-in-non-selected-windows",
22222 &Vcursor_in_non_selected_windows,
22223 doc: /* *Cursor type to display in non-selected windows.
22224 t means to use hollow box cursor. See `cursor-type' for other values. */);
22225 Vcursor_in_non_selected_windows = Qt;
22226
22227 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22228 doc: /* Alist specifying how to blink the cursor off.
22229 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22230 `cursor-type' frame-parameter or variable equals ON-STATE,
22231 comparing using `equal', Emacs uses OFF-STATE to specify
22232 how to blink it off. */);
22233 Vblink_cursor_alist = Qnil;
22234
22235 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22236 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22237 automatic_hscrolling_p = 1;
22238
22239 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22240 doc: /* *How many columns away from the window edge point is allowed to get
22241 before automatic hscrolling will horizontally scroll the window. */);
22242 hscroll_margin = 5;
22243
22244 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22245 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22246 When point is less than `automatic-hscroll-margin' columns from the window
22247 edge, automatic hscrolling will scroll the window by the amount of columns
22248 determined by this variable. If its value is a positive integer, scroll that
22249 many columns. If it's a positive floating-point number, it specifies the
22250 fraction of the window's width to scroll. If it's nil or zero, point will be
22251 centered horizontally after the scroll. Any other value, including negative
22252 numbers, are treated as if the value were zero.
22253
22254 Automatic hscrolling always moves point outside the scroll margin, so if
22255 point was more than scroll step columns inside the margin, the window will
22256 scroll more than the value given by the scroll step.
22257
22258 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22259 and `scroll-right' overrides this variable's effect. */);
22260 Vhscroll_step = make_number (0);
22261
22262 DEFVAR_LISP ("image-types", &Vimage_types,
22263 doc: /* List of supported image types.
22264 Each element of the list is a symbol for a supported image type. */);
22265 Vimage_types = Qnil;
22266
22267 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22268 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22269 Bind this around calls to `message' to let it take effect. */);
22270 message_truncate_lines = 0;
22271
22272 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22273 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22274 Can be used to update submenus whose contents should vary. */);
22275 Vmenu_bar_update_hook = Qnil;
22276
22277 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22278 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22279 inhibit_menubar_update = 0;
22280
22281 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22282 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22283 inhibit_eval_during_redisplay = 0;
22284
22285 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22286 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22287 inhibit_free_realized_faces = 0;
22288
22289 #if GLYPH_DEBUG
22290 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22291 doc: /* Inhibit try_window_id display optimization. */);
22292 inhibit_try_window_id = 0;
22293
22294 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22295 doc: /* Inhibit try_window_reusing display optimization. */);
22296 inhibit_try_window_reusing = 0;
22297
22298 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22299 doc: /* Inhibit try_cursor_movement display optimization. */);
22300 inhibit_try_cursor_movement = 0;
22301 #endif /* GLYPH_DEBUG */
22302 }
22303
22304
22305 /* Initialize this module when Emacs starts. */
22306
22307 void
22308 init_xdisp ()
22309 {
22310 Lisp_Object root_window;
22311 struct window *mini_w;
22312
22313 current_header_line_height = current_mode_line_height = -1;
22314
22315 CHARPOS (this_line_start_pos) = 0;
22316
22317 mini_w = XWINDOW (minibuf_window);
22318 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22319
22320 if (!noninteractive)
22321 {
22322 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22323 int i;
22324
22325 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22326 set_window_height (root_window,
22327 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22328 0);
22329 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22330 set_window_height (minibuf_window, 1, 0);
22331
22332 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22333 mini_w->total_cols = make_number (FRAME_COLS (f));
22334
22335 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22336 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22337 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22338
22339 /* The default ellipsis glyphs `...'. */
22340 for (i = 0; i < 3; ++i)
22341 default_invis_vector[i] = make_number ('.');
22342 }
22343
22344 {
22345 /* Allocate the buffer for frame titles.
22346 Also used for `format-mode-line'. */
22347 int size = 100;
22348 frame_title_buf = (char *) xmalloc (size);
22349 frame_title_buf_end = frame_title_buf + size;
22350 frame_title_ptr = NULL;
22351 }
22352
22353 help_echo_showing_p = 0;
22354 }
22355
22356
22357 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22358 (do not change this comment) */