Merged in changes from CVS HEAD
[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 (noninteractive && 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 'T':
16679 /* %T is the frame name on a termcap frame, the empty string otherwise. */
16680 if (! FRAME_TERMCAP_P (f))
16681 return "";
16682 if (!NILP (f->title))
16683 return (char *) SDATA (f->title);
16684 return (char *) SDATA (f->name);
16685
16686 case 'z':
16687 /* coding-system (not including end-of-line format) */
16688 case 'Z':
16689 /* coding-system (including end-of-line type) */
16690 {
16691 int eol_flag = (c == 'Z');
16692 char *p = decode_mode_spec_buf;
16693
16694 if (! FRAME_WINDOW_P (f))
16695 {
16696 /* No need to mention EOL here--the terminal never needs
16697 to do EOL conversion. */
16698 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16699 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16700 }
16701 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16702 p, eol_flag);
16703
16704 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16705 #ifdef subprocesses
16706 obj = Fget_buffer_process (Fcurrent_buffer ());
16707 if (PROCESSP (obj))
16708 {
16709 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16710 p, eol_flag);
16711 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16712 p, eol_flag);
16713 }
16714 #endif /* subprocesses */
16715 #endif /* 0 */
16716 *p = 0;
16717 return decode_mode_spec_buf;
16718 }
16719 }
16720
16721 if (STRINGP (obj))
16722 {
16723 *multibyte = STRING_MULTIBYTE (obj);
16724 return (char *) SDATA (obj);
16725 }
16726 else
16727 return "";
16728 }
16729
16730
16731 /* Count up to COUNT lines starting from START / START_BYTE.
16732 But don't go beyond LIMIT_BYTE.
16733 Return the number of lines thus found (always nonnegative).
16734
16735 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16736
16737 static int
16738 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16739 int start, start_byte, limit_byte, count;
16740 int *byte_pos_ptr;
16741 {
16742 register unsigned char *cursor;
16743 unsigned char *base;
16744
16745 register int ceiling;
16746 register unsigned char *ceiling_addr;
16747 int orig_count = count;
16748
16749 /* If we are not in selective display mode,
16750 check only for newlines. */
16751 int selective_display = (!NILP (current_buffer->selective_display)
16752 && !INTEGERP (current_buffer->selective_display));
16753
16754 if (count > 0)
16755 {
16756 while (start_byte < limit_byte)
16757 {
16758 ceiling = BUFFER_CEILING_OF (start_byte);
16759 ceiling = min (limit_byte - 1, ceiling);
16760 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16761 base = (cursor = BYTE_POS_ADDR (start_byte));
16762 while (1)
16763 {
16764 if (selective_display)
16765 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16766 ;
16767 else
16768 while (*cursor != '\n' && ++cursor != ceiling_addr)
16769 ;
16770
16771 if (cursor != ceiling_addr)
16772 {
16773 if (--count == 0)
16774 {
16775 start_byte += cursor - base + 1;
16776 *byte_pos_ptr = start_byte;
16777 return orig_count;
16778 }
16779 else
16780 if (++cursor == ceiling_addr)
16781 break;
16782 }
16783 else
16784 break;
16785 }
16786 start_byte += cursor - base;
16787 }
16788 }
16789 else
16790 {
16791 while (start_byte > limit_byte)
16792 {
16793 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16794 ceiling = max (limit_byte, ceiling);
16795 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16796 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16797 while (1)
16798 {
16799 if (selective_display)
16800 while (--cursor != ceiling_addr
16801 && *cursor != '\n' && *cursor != 015)
16802 ;
16803 else
16804 while (--cursor != ceiling_addr && *cursor != '\n')
16805 ;
16806
16807 if (cursor != ceiling_addr)
16808 {
16809 if (++count == 0)
16810 {
16811 start_byte += cursor - base + 1;
16812 *byte_pos_ptr = start_byte;
16813 /* When scanning backwards, we should
16814 not count the newline posterior to which we stop. */
16815 return - orig_count - 1;
16816 }
16817 }
16818 else
16819 break;
16820 }
16821 /* Here we add 1 to compensate for the last decrement
16822 of CURSOR, which took it past the valid range. */
16823 start_byte += cursor - base + 1;
16824 }
16825 }
16826
16827 *byte_pos_ptr = limit_byte;
16828
16829 if (count < 0)
16830 return - orig_count + count;
16831 return orig_count - count;
16832
16833 }
16834
16835
16836 \f
16837 /***********************************************************************
16838 Displaying strings
16839 ***********************************************************************/
16840
16841 /* Display a NUL-terminated string, starting with index START.
16842
16843 If STRING is non-null, display that C string. Otherwise, the Lisp
16844 string LISP_STRING is displayed.
16845
16846 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16847 FACE_STRING. Display STRING or LISP_STRING with the face at
16848 FACE_STRING_POS in FACE_STRING:
16849
16850 Display the string in the environment given by IT, but use the
16851 standard display table, temporarily.
16852
16853 FIELD_WIDTH is the minimum number of output glyphs to produce.
16854 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16855 with spaces. If STRING has more characters, more than FIELD_WIDTH
16856 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16857
16858 PRECISION is the maximum number of characters to output from
16859 STRING. PRECISION < 0 means don't truncate the string.
16860
16861 This is roughly equivalent to printf format specifiers:
16862
16863 FIELD_WIDTH PRECISION PRINTF
16864 ----------------------------------------
16865 -1 -1 %s
16866 -1 10 %.10s
16867 10 -1 %10s
16868 20 10 %20.10s
16869
16870 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16871 display them, and < 0 means obey the current buffer's value of
16872 enable_multibyte_characters.
16873
16874 Value is the number of glyphs produced. */
16875
16876 static int
16877 display_string (string, lisp_string, face_string, face_string_pos,
16878 start, it, field_width, precision, max_x, multibyte)
16879 unsigned char *string;
16880 Lisp_Object lisp_string;
16881 Lisp_Object face_string;
16882 int face_string_pos;
16883 int start;
16884 struct it *it;
16885 int field_width, precision, max_x;
16886 int multibyte;
16887 {
16888 int hpos_at_start = it->hpos;
16889 int saved_face_id = it->face_id;
16890 struct glyph_row *row = it->glyph_row;
16891
16892 /* Initialize the iterator IT for iteration over STRING beginning
16893 with index START. */
16894 reseat_to_string (it, string, lisp_string, start,
16895 precision, field_width, multibyte);
16896
16897 /* If displaying STRING, set up the face of the iterator
16898 from LISP_STRING, if that's given. */
16899 if (STRINGP (face_string))
16900 {
16901 int endptr;
16902 struct face *face;
16903
16904 it->face_id
16905 = face_at_string_position (it->w, face_string, face_string_pos,
16906 0, it->region_beg_charpos,
16907 it->region_end_charpos,
16908 &endptr, it->base_face_id, 0);
16909 face = FACE_FROM_ID (it->f, it->face_id);
16910 it->face_box_p = face->box != FACE_NO_BOX;
16911 }
16912
16913 /* Set max_x to the maximum allowed X position. Don't let it go
16914 beyond the right edge of the window. */
16915 if (max_x <= 0)
16916 max_x = it->last_visible_x;
16917 else
16918 max_x = min (max_x, it->last_visible_x);
16919
16920 /* Skip over display elements that are not visible. because IT->w is
16921 hscrolled. */
16922 if (it->current_x < it->first_visible_x)
16923 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16924 MOVE_TO_POS | MOVE_TO_X);
16925
16926 row->ascent = it->max_ascent;
16927 row->height = it->max_ascent + it->max_descent;
16928 row->phys_ascent = it->max_phys_ascent;
16929 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16930
16931 /* This condition is for the case that we are called with current_x
16932 past last_visible_x. */
16933 while (it->current_x < max_x)
16934 {
16935 int x_before, x, n_glyphs_before, i, nglyphs;
16936
16937 /* Get the next display element. */
16938 if (!get_next_display_element (it))
16939 break;
16940
16941 /* Produce glyphs. */
16942 x_before = it->current_x;
16943 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16944 PRODUCE_GLYPHS (it);
16945
16946 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16947 i = 0;
16948 x = x_before;
16949 while (i < nglyphs)
16950 {
16951 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16952
16953 if (!it->truncate_lines_p
16954 && x + glyph->pixel_width > max_x)
16955 {
16956 /* End of continued line or max_x reached. */
16957 if (CHAR_GLYPH_PADDING_P (*glyph))
16958 {
16959 /* A wide character is unbreakable. */
16960 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16961 it->current_x = x_before;
16962 }
16963 else
16964 {
16965 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16966 it->current_x = x;
16967 }
16968 break;
16969 }
16970 else if (x + glyph->pixel_width > it->first_visible_x)
16971 {
16972 /* Glyph is at least partially visible. */
16973 ++it->hpos;
16974 if (x < it->first_visible_x)
16975 it->glyph_row->x = x - it->first_visible_x;
16976 }
16977 else
16978 {
16979 /* Glyph is off the left margin of the display area.
16980 Should not happen. */
16981 abort ();
16982 }
16983
16984 row->ascent = max (row->ascent, it->max_ascent);
16985 row->height = max (row->height, it->max_ascent + it->max_descent);
16986 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16987 row->phys_height = max (row->phys_height,
16988 it->max_phys_ascent + it->max_phys_descent);
16989 x += glyph->pixel_width;
16990 ++i;
16991 }
16992
16993 /* Stop if max_x reached. */
16994 if (i < nglyphs)
16995 break;
16996
16997 /* Stop at line ends. */
16998 if (ITERATOR_AT_END_OF_LINE_P (it))
16999 {
17000 it->continuation_lines_width = 0;
17001 break;
17002 }
17003
17004 set_iterator_to_next (it, 1);
17005
17006 /* Stop if truncating at the right edge. */
17007 if (it->truncate_lines_p
17008 && it->current_x >= it->last_visible_x)
17009 {
17010 /* Add truncation mark, but don't do it if the line is
17011 truncated at a padding space. */
17012 if (IT_CHARPOS (*it) < it->string_nchars)
17013 {
17014 if (!FRAME_WINDOW_P (it->f))
17015 {
17016 int i, n;
17017
17018 if (it->current_x > it->last_visible_x)
17019 {
17020 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17021 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17022 break;
17023 for (n = row->used[TEXT_AREA]; i < n; ++i)
17024 {
17025 row->used[TEXT_AREA] = i;
17026 produce_special_glyphs (it, IT_TRUNCATION);
17027 }
17028 }
17029 produce_special_glyphs (it, IT_TRUNCATION);
17030 }
17031 it->glyph_row->truncated_on_right_p = 1;
17032 }
17033 break;
17034 }
17035 }
17036
17037 /* Maybe insert a truncation at the left. */
17038 if (it->first_visible_x
17039 && IT_CHARPOS (*it) > 0)
17040 {
17041 if (!FRAME_WINDOW_P (it->f))
17042 insert_left_trunc_glyphs (it);
17043 it->glyph_row->truncated_on_left_p = 1;
17044 }
17045
17046 it->face_id = saved_face_id;
17047
17048 /* Value is number of columns displayed. */
17049 return it->hpos - hpos_at_start;
17050 }
17051
17052
17053 \f
17054 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17055 appears as an element of LIST or as the car of an element of LIST.
17056 If PROPVAL is a list, compare each element against LIST in that
17057 way, and return 1/2 if any element of PROPVAL is found in LIST.
17058 Otherwise return 0. This function cannot quit.
17059 The return value is 2 if the text is invisible but with an ellipsis
17060 and 1 if it's invisible and without an ellipsis. */
17061
17062 int
17063 invisible_p (propval, list)
17064 register Lisp_Object propval;
17065 Lisp_Object list;
17066 {
17067 register Lisp_Object tail, proptail;
17068
17069 for (tail = list; CONSP (tail); tail = XCDR (tail))
17070 {
17071 register Lisp_Object tem;
17072 tem = XCAR (tail);
17073 if (EQ (propval, tem))
17074 return 1;
17075 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17076 return NILP (XCDR (tem)) ? 1 : 2;
17077 }
17078
17079 if (CONSP (propval))
17080 {
17081 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17082 {
17083 Lisp_Object propelt;
17084 propelt = XCAR (proptail);
17085 for (tail = list; CONSP (tail); tail = XCDR (tail))
17086 {
17087 register Lisp_Object tem;
17088 tem = XCAR (tail);
17089 if (EQ (propelt, tem))
17090 return 1;
17091 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17092 return NILP (XCDR (tem)) ? 1 : 2;
17093 }
17094 }
17095 }
17096
17097 return 0;
17098 }
17099
17100 \f
17101 /***********************************************************************
17102 Glyph Display
17103 ***********************************************************************/
17104
17105 #ifdef HAVE_WINDOW_SYSTEM
17106
17107 #if GLYPH_DEBUG
17108
17109 void
17110 dump_glyph_string (s)
17111 struct glyph_string *s;
17112 {
17113 fprintf (stderr, "glyph string\n");
17114 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17115 s->x, s->y, s->width, s->height);
17116 fprintf (stderr, " ybase = %d\n", s->ybase);
17117 fprintf (stderr, " hl = %d\n", s->hl);
17118 fprintf (stderr, " left overhang = %d, right = %d\n",
17119 s->left_overhang, s->right_overhang);
17120 fprintf (stderr, " nchars = %d\n", s->nchars);
17121 fprintf (stderr, " extends to end of line = %d\n",
17122 s->extends_to_end_of_line_p);
17123 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17124 fprintf (stderr, " bg width = %d\n", s->background_width);
17125 }
17126
17127 #endif /* GLYPH_DEBUG */
17128
17129 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17130 of XChar2b structures for S; it can't be allocated in
17131 init_glyph_string because it must be allocated via `alloca'. W
17132 is the window on which S is drawn. ROW and AREA are the glyph row
17133 and area within the row from which S is constructed. START is the
17134 index of the first glyph structure covered by S. HL is a
17135 face-override for drawing S. */
17136
17137 #ifdef HAVE_NTGUI
17138 #define OPTIONAL_HDC(hdc) hdc,
17139 #define DECLARE_HDC(hdc) HDC hdc;
17140 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17141 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17142 #endif
17143
17144 #ifndef OPTIONAL_HDC
17145 #define OPTIONAL_HDC(hdc)
17146 #define DECLARE_HDC(hdc)
17147 #define ALLOCATE_HDC(hdc, f)
17148 #define RELEASE_HDC(hdc, f)
17149 #endif
17150
17151 static void
17152 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17153 struct glyph_string *s;
17154 DECLARE_HDC (hdc)
17155 XChar2b *char2b;
17156 struct window *w;
17157 struct glyph_row *row;
17158 enum glyph_row_area area;
17159 int start;
17160 enum draw_glyphs_face hl;
17161 {
17162 bzero (s, sizeof *s);
17163 s->w = w;
17164 s->f = XFRAME (w->frame);
17165 #ifdef HAVE_NTGUI
17166 s->hdc = hdc;
17167 #endif
17168 s->display = FRAME_X_DISPLAY (s->f);
17169 s->window = FRAME_X_WINDOW (s->f);
17170 s->char2b = char2b;
17171 s->hl = hl;
17172 s->row = row;
17173 s->area = area;
17174 s->first_glyph = row->glyphs[area] + start;
17175 s->height = row->height;
17176 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17177
17178 /* Display the internal border below the tool-bar window. */
17179 if (s->w == XWINDOW (s->f->tool_bar_window))
17180 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17181
17182 s->ybase = s->y + row->ascent;
17183 }
17184
17185
17186 /* Append the list of glyph strings with head H and tail T to the list
17187 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17188
17189 static INLINE void
17190 append_glyph_string_lists (head, tail, h, t)
17191 struct glyph_string **head, **tail;
17192 struct glyph_string *h, *t;
17193 {
17194 if (h)
17195 {
17196 if (*head)
17197 (*tail)->next = h;
17198 else
17199 *head = h;
17200 h->prev = *tail;
17201 *tail = t;
17202 }
17203 }
17204
17205
17206 /* Prepend the list of glyph strings with head H and tail T to the
17207 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17208 result. */
17209
17210 static INLINE void
17211 prepend_glyph_string_lists (head, tail, h, t)
17212 struct glyph_string **head, **tail;
17213 struct glyph_string *h, *t;
17214 {
17215 if (h)
17216 {
17217 if (*head)
17218 (*head)->prev = t;
17219 else
17220 *tail = t;
17221 t->next = *head;
17222 *head = h;
17223 }
17224 }
17225
17226
17227 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17228 Set *HEAD and *TAIL to the resulting list. */
17229
17230 static INLINE void
17231 append_glyph_string (head, tail, s)
17232 struct glyph_string **head, **tail;
17233 struct glyph_string *s;
17234 {
17235 s->next = s->prev = NULL;
17236 append_glyph_string_lists (head, tail, s, s);
17237 }
17238
17239
17240 /* Get face and two-byte form of character glyph GLYPH on frame F.
17241 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17242 a pointer to a realized face that is ready for display. */
17243
17244 static INLINE struct face *
17245 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17246 struct frame *f;
17247 struct glyph *glyph;
17248 XChar2b *char2b;
17249 int *two_byte_p;
17250 {
17251 struct face *face;
17252
17253 xassert (glyph->type == CHAR_GLYPH);
17254 face = FACE_FROM_ID (f, glyph->face_id);
17255
17256 if (two_byte_p)
17257 *two_byte_p = 0;
17258
17259 if (!glyph->multibyte_p)
17260 {
17261 /* Unibyte case. We don't have to encode, but we have to make
17262 sure to use a face suitable for unibyte. */
17263 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17264 }
17265 else if (glyph->u.ch < 128
17266 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17267 {
17268 /* Case of ASCII in a face known to fit ASCII. */
17269 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17270 }
17271 else
17272 {
17273 int c1, c2, charset;
17274
17275 /* Split characters into bytes. If c2 is -1 afterwards, C is
17276 really a one-byte character so that byte1 is zero. */
17277 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17278 if (c2 > 0)
17279 STORE_XCHAR2B (char2b, c1, c2);
17280 else
17281 STORE_XCHAR2B (char2b, 0, c1);
17282
17283 /* Maybe encode the character in *CHAR2B. */
17284 if (charset != CHARSET_ASCII)
17285 {
17286 struct font_info *font_info
17287 = FONT_INFO_FROM_ID (f, face->font_info_id);
17288 if (font_info)
17289 glyph->font_type
17290 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17291 }
17292 }
17293
17294 /* Make sure X resources of the face are allocated. */
17295 xassert (face != NULL);
17296 PREPARE_FACE_FOR_DISPLAY (f, face);
17297 return face;
17298 }
17299
17300
17301 /* Fill glyph string S with composition components specified by S->cmp.
17302
17303 FACES is an array of faces for all components of this composition.
17304 S->gidx is the index of the first component for S.
17305 OVERLAPS_P non-zero means S should draw the foreground only, and
17306 use its physical height for clipping.
17307
17308 Value is the index of a component not in S. */
17309
17310 static int
17311 fill_composite_glyph_string (s, faces, overlaps_p)
17312 struct glyph_string *s;
17313 struct face **faces;
17314 int overlaps_p;
17315 {
17316 int i;
17317
17318 xassert (s);
17319
17320 s->for_overlaps_p = overlaps_p;
17321
17322 s->face = faces[s->gidx];
17323 s->font = s->face->font;
17324 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17325
17326 /* For all glyphs of this composition, starting at the offset
17327 S->gidx, until we reach the end of the definition or encounter a
17328 glyph that requires the different face, add it to S. */
17329 ++s->nchars;
17330 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17331 ++s->nchars;
17332
17333 /* All glyph strings for the same composition has the same width,
17334 i.e. the width set for the first component of the composition. */
17335
17336 s->width = s->first_glyph->pixel_width;
17337
17338 /* If the specified font could not be loaded, use the frame's
17339 default font, but record the fact that we couldn't load it in
17340 the glyph string so that we can draw rectangles for the
17341 characters of the glyph string. */
17342 if (s->font == NULL)
17343 {
17344 s->font_not_found_p = 1;
17345 s->font = FRAME_FONT (s->f);
17346 }
17347
17348 /* Adjust base line for subscript/superscript text. */
17349 s->ybase += s->first_glyph->voffset;
17350
17351 xassert (s->face && s->face->gc);
17352
17353 /* This glyph string must always be drawn with 16-bit functions. */
17354 s->two_byte_p = 1;
17355
17356 return s->gidx + s->nchars;
17357 }
17358
17359
17360 /* Fill glyph string S from a sequence of character glyphs.
17361
17362 FACE_ID is the face id of the string. START is the index of the
17363 first glyph to consider, END is the index of the last + 1.
17364 OVERLAPS_P non-zero means S should draw the foreground only, and
17365 use its physical height for clipping.
17366
17367 Value is the index of the first glyph not in S. */
17368
17369 static int
17370 fill_glyph_string (s, face_id, start, end, overlaps_p)
17371 struct glyph_string *s;
17372 int face_id;
17373 int start, end, overlaps_p;
17374 {
17375 struct glyph *glyph, *last;
17376 int voffset;
17377 int glyph_not_available_p;
17378
17379 xassert (s->f == XFRAME (s->w->frame));
17380 xassert (s->nchars == 0);
17381 xassert (start >= 0 && end > start);
17382
17383 s->for_overlaps_p = overlaps_p,
17384 glyph = s->row->glyphs[s->area] + start;
17385 last = s->row->glyphs[s->area] + end;
17386 voffset = glyph->voffset;
17387
17388 glyph_not_available_p = glyph->glyph_not_available_p;
17389
17390 while (glyph < last
17391 && glyph->type == CHAR_GLYPH
17392 && glyph->voffset == voffset
17393 /* Same face id implies same font, nowadays. */
17394 && glyph->face_id == face_id
17395 && glyph->glyph_not_available_p == glyph_not_available_p)
17396 {
17397 int two_byte_p;
17398
17399 s->face = get_glyph_face_and_encoding (s->f, glyph,
17400 s->char2b + s->nchars,
17401 &two_byte_p);
17402 s->two_byte_p = two_byte_p;
17403 ++s->nchars;
17404 xassert (s->nchars <= end - start);
17405 s->width += glyph->pixel_width;
17406 ++glyph;
17407 }
17408
17409 s->font = s->face->font;
17410 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17411
17412 /* If the specified font could not be loaded, use the frame's font,
17413 but record the fact that we couldn't load it in
17414 S->font_not_found_p so that we can draw rectangles for the
17415 characters of the glyph string. */
17416 if (s->font == NULL || glyph_not_available_p)
17417 {
17418 s->font_not_found_p = 1;
17419 s->font = FRAME_FONT (s->f);
17420 }
17421
17422 /* Adjust base line for subscript/superscript text. */
17423 s->ybase += voffset;
17424
17425 xassert (s->face && s->face->gc);
17426 return glyph - s->row->glyphs[s->area];
17427 }
17428
17429
17430 /* Fill glyph string S from image glyph S->first_glyph. */
17431
17432 static void
17433 fill_image_glyph_string (s)
17434 struct glyph_string *s;
17435 {
17436 xassert (s->first_glyph->type == IMAGE_GLYPH);
17437 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17438 xassert (s->img);
17439 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17440 s->font = s->face->font;
17441 s->width = s->first_glyph->pixel_width;
17442
17443 /* Adjust base line for subscript/superscript text. */
17444 s->ybase += s->first_glyph->voffset;
17445 }
17446
17447
17448 /* Fill glyph string S from a sequence of stretch glyphs.
17449
17450 ROW is the glyph row in which the glyphs are found, AREA is the
17451 area within the row. START is the index of the first glyph to
17452 consider, END is the index of the last + 1.
17453
17454 Value is the index of the first glyph not in S. */
17455
17456 static int
17457 fill_stretch_glyph_string (s, row, area, start, end)
17458 struct glyph_string *s;
17459 struct glyph_row *row;
17460 enum glyph_row_area area;
17461 int start, end;
17462 {
17463 struct glyph *glyph, *last;
17464 int voffset, face_id;
17465
17466 xassert (s->first_glyph->type == STRETCH_GLYPH);
17467
17468 glyph = s->row->glyphs[s->area] + start;
17469 last = s->row->glyphs[s->area] + end;
17470 face_id = glyph->face_id;
17471 s->face = FACE_FROM_ID (s->f, face_id);
17472 s->font = s->face->font;
17473 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17474 s->width = glyph->pixel_width;
17475 voffset = glyph->voffset;
17476
17477 for (++glyph;
17478 (glyph < last
17479 && glyph->type == STRETCH_GLYPH
17480 && glyph->voffset == voffset
17481 && glyph->face_id == face_id);
17482 ++glyph)
17483 s->width += glyph->pixel_width;
17484
17485 /* Adjust base line for subscript/superscript text. */
17486 s->ybase += voffset;
17487
17488 /* The case that face->gc == 0 is handled when drawing the glyph
17489 string by calling PREPARE_FACE_FOR_DISPLAY. */
17490 xassert (s->face);
17491 return glyph - s->row->glyphs[s->area];
17492 }
17493
17494
17495 /* EXPORT for RIF:
17496 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17497 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17498 assumed to be zero. */
17499
17500 void
17501 x_get_glyph_overhangs (glyph, f, left, right)
17502 struct glyph *glyph;
17503 struct frame *f;
17504 int *left, *right;
17505 {
17506 *left = *right = 0;
17507
17508 if (glyph->type == CHAR_GLYPH)
17509 {
17510 XFontStruct *font;
17511 struct face *face;
17512 struct font_info *font_info;
17513 XChar2b char2b;
17514 XCharStruct *pcm;
17515
17516 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17517 font = face->font;
17518 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17519 if (font /* ++KFS: Should this be font_info ? */
17520 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17521 {
17522 if (pcm->rbearing > pcm->width)
17523 *right = pcm->rbearing - pcm->width;
17524 if (pcm->lbearing < 0)
17525 *left = -pcm->lbearing;
17526 }
17527 }
17528 }
17529
17530
17531 /* Return the index of the first glyph preceding glyph string S that
17532 is overwritten by S because of S's left overhang. Value is -1
17533 if no glyphs are overwritten. */
17534
17535 static int
17536 left_overwritten (s)
17537 struct glyph_string *s;
17538 {
17539 int k;
17540
17541 if (s->left_overhang)
17542 {
17543 int x = 0, i;
17544 struct glyph *glyphs = s->row->glyphs[s->area];
17545 int first = s->first_glyph - glyphs;
17546
17547 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17548 x -= glyphs[i].pixel_width;
17549
17550 k = i + 1;
17551 }
17552 else
17553 k = -1;
17554
17555 return k;
17556 }
17557
17558
17559 /* Return the index of the first glyph preceding glyph string S that
17560 is overwriting S because of its right overhang. Value is -1 if no
17561 glyph in front of S overwrites S. */
17562
17563 static int
17564 left_overwriting (s)
17565 struct glyph_string *s;
17566 {
17567 int i, k, x;
17568 struct glyph *glyphs = s->row->glyphs[s->area];
17569 int first = s->first_glyph - glyphs;
17570
17571 k = -1;
17572 x = 0;
17573 for (i = first - 1; i >= 0; --i)
17574 {
17575 int left, right;
17576 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17577 if (x + right > 0)
17578 k = i;
17579 x -= glyphs[i].pixel_width;
17580 }
17581
17582 return k;
17583 }
17584
17585
17586 /* Return the index of the last glyph following glyph string S that is
17587 not overwritten by S because of S's right overhang. Value is -1 if
17588 no such glyph is found. */
17589
17590 static int
17591 right_overwritten (s)
17592 struct glyph_string *s;
17593 {
17594 int k = -1;
17595
17596 if (s->right_overhang)
17597 {
17598 int x = 0, i;
17599 struct glyph *glyphs = s->row->glyphs[s->area];
17600 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17601 int end = s->row->used[s->area];
17602
17603 for (i = first; i < end && s->right_overhang > x; ++i)
17604 x += glyphs[i].pixel_width;
17605
17606 k = i;
17607 }
17608
17609 return k;
17610 }
17611
17612
17613 /* Return the index of the last glyph following glyph string S that
17614 overwrites S because of its left overhang. Value is negative
17615 if no such glyph is found. */
17616
17617 static int
17618 right_overwriting (s)
17619 struct glyph_string *s;
17620 {
17621 int i, k, x;
17622 int end = s->row->used[s->area];
17623 struct glyph *glyphs = s->row->glyphs[s->area];
17624 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17625
17626 k = -1;
17627 x = 0;
17628 for (i = first; i < end; ++i)
17629 {
17630 int left, right;
17631 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17632 if (x - left < 0)
17633 k = i;
17634 x += glyphs[i].pixel_width;
17635 }
17636
17637 return k;
17638 }
17639
17640
17641 /* Get face and two-byte form of character C in face FACE_ID on frame
17642 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17643 means we want to display multibyte text. DISPLAY_P non-zero means
17644 make sure that X resources for the face returned are allocated.
17645 Value is a pointer to a realized face that is ready for display if
17646 DISPLAY_P is non-zero. */
17647
17648 static INLINE struct face *
17649 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17650 struct frame *f;
17651 int c, face_id;
17652 XChar2b *char2b;
17653 int multibyte_p, display_p;
17654 {
17655 struct face *face = FACE_FROM_ID (f, face_id);
17656
17657 if (!multibyte_p)
17658 {
17659 /* Unibyte case. We don't have to encode, but we have to make
17660 sure to use a face suitable for unibyte. */
17661 STORE_XCHAR2B (char2b, 0, c);
17662 face_id = FACE_FOR_CHAR (f, face, c);
17663 face = FACE_FROM_ID (f, face_id);
17664 }
17665 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17666 {
17667 /* Case of ASCII in a face known to fit ASCII. */
17668 STORE_XCHAR2B (char2b, 0, c);
17669 }
17670 else
17671 {
17672 int c1, c2, charset;
17673
17674 /* Split characters into bytes. If c2 is -1 afterwards, C is
17675 really a one-byte character so that byte1 is zero. */
17676 SPLIT_CHAR (c, charset, c1, c2);
17677 if (c2 > 0)
17678 STORE_XCHAR2B (char2b, c1, c2);
17679 else
17680 STORE_XCHAR2B (char2b, 0, c1);
17681
17682 /* Maybe encode the character in *CHAR2B. */
17683 if (face->font != NULL)
17684 {
17685 struct font_info *font_info
17686 = FONT_INFO_FROM_ID (f, face->font_info_id);
17687 if (font_info)
17688 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17689 }
17690 }
17691
17692 /* Make sure X resources of the face are allocated. */
17693 #ifdef HAVE_X_WINDOWS
17694 if (display_p)
17695 #endif
17696 {
17697 xassert (face != NULL);
17698 PREPARE_FACE_FOR_DISPLAY (f, face);
17699 }
17700
17701 return face;
17702 }
17703
17704
17705 /* Set background width of glyph string S. START is the index of the
17706 first glyph following S. LAST_X is the right-most x-position + 1
17707 in the drawing area. */
17708
17709 static INLINE void
17710 set_glyph_string_background_width (s, start, last_x)
17711 struct glyph_string *s;
17712 int start;
17713 int last_x;
17714 {
17715 /* If the face of this glyph string has to be drawn to the end of
17716 the drawing area, set S->extends_to_end_of_line_p. */
17717 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17718
17719 if (start == s->row->used[s->area]
17720 && s->area == TEXT_AREA
17721 && ((s->hl == DRAW_NORMAL_TEXT
17722 && (s->row->fill_line_p
17723 || s->face->background != default_face->background
17724 || s->face->stipple != default_face->stipple
17725 || s->row->mouse_face_p))
17726 || s->hl == DRAW_MOUSE_FACE
17727 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17728 && s->row->fill_line_p)))
17729 s->extends_to_end_of_line_p = 1;
17730
17731 /* If S extends its face to the end of the line, set its
17732 background_width to the distance to the right edge of the drawing
17733 area. */
17734 if (s->extends_to_end_of_line_p)
17735 s->background_width = last_x - s->x + 1;
17736 else
17737 s->background_width = s->width;
17738 }
17739
17740
17741 /* Compute overhangs and x-positions for glyph string S and its
17742 predecessors, or successors. X is the starting x-position for S.
17743 BACKWARD_P non-zero means process predecessors. */
17744
17745 static void
17746 compute_overhangs_and_x (s, x, backward_p)
17747 struct glyph_string *s;
17748 int x;
17749 int backward_p;
17750 {
17751 if (backward_p)
17752 {
17753 while (s)
17754 {
17755 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17756 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17757 x -= s->width;
17758 s->x = x;
17759 s = s->prev;
17760 }
17761 }
17762 else
17763 {
17764 while (s)
17765 {
17766 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17767 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17768 s->x = x;
17769 x += s->width;
17770 s = s->next;
17771 }
17772 }
17773 }
17774
17775
17776
17777 /* The following macros are only called from draw_glyphs below.
17778 They reference the following parameters of that function directly:
17779 `w', `row', `area', and `overlap_p'
17780 as well as the following local variables:
17781 `s', `f', and `hdc' (in W32) */
17782
17783 #ifdef HAVE_NTGUI
17784 /* On W32, silently add local `hdc' variable to argument list of
17785 init_glyph_string. */
17786 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17787 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17788 #else
17789 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17790 init_glyph_string (s, char2b, w, row, area, start, hl)
17791 #endif
17792
17793 /* Add a glyph string for a stretch glyph to the list of strings
17794 between HEAD and TAIL. START is the index of the stretch glyph in
17795 row area AREA of glyph row ROW. END is the index of the last glyph
17796 in that glyph row area. X is the current output position assigned
17797 to the new glyph string constructed. HL overrides that face of the
17798 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17799 is the right-most x-position of the drawing area. */
17800
17801 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17802 and below -- keep them on one line. */
17803 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17804 do \
17805 { \
17806 s = (struct glyph_string *) alloca (sizeof *s); \
17807 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17808 START = fill_stretch_glyph_string (s, row, area, START, END); \
17809 append_glyph_string (&HEAD, &TAIL, s); \
17810 s->x = (X); \
17811 } \
17812 while (0)
17813
17814
17815 /* Add a glyph string for an image glyph to the list of strings
17816 between HEAD and TAIL. START is the index of the image glyph in
17817 row area AREA of glyph row ROW. END is the index of the last glyph
17818 in that glyph row area. X is the current output position assigned
17819 to the new glyph string constructed. HL overrides that face of the
17820 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17821 is the right-most x-position of the drawing area. */
17822
17823 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17824 do \
17825 { \
17826 s = (struct glyph_string *) alloca (sizeof *s); \
17827 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17828 fill_image_glyph_string (s); \
17829 append_glyph_string (&HEAD, &TAIL, s); \
17830 ++START; \
17831 s->x = (X); \
17832 } \
17833 while (0)
17834
17835
17836 /* Add a glyph string for a sequence of character glyphs to the list
17837 of strings between HEAD and TAIL. START is the index of the first
17838 glyph in row area AREA of glyph row ROW that is part of the new
17839 glyph string. END is the index of the last glyph in that glyph row
17840 area. X is the current output position assigned to the new glyph
17841 string constructed. HL overrides that face of the glyph; e.g. it
17842 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17843 right-most x-position of the drawing area. */
17844
17845 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17846 do \
17847 { \
17848 int c, face_id; \
17849 XChar2b *char2b; \
17850 \
17851 c = (row)->glyphs[area][START].u.ch; \
17852 face_id = (row)->glyphs[area][START].face_id; \
17853 \
17854 s = (struct glyph_string *) alloca (sizeof *s); \
17855 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17856 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17857 append_glyph_string (&HEAD, &TAIL, s); \
17858 s->x = (X); \
17859 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17860 } \
17861 while (0)
17862
17863
17864 /* Add a glyph string for a composite sequence to the list of strings
17865 between HEAD and TAIL. START is the index of the first glyph in
17866 row area AREA of glyph row ROW that is part of the new glyph
17867 string. END is the index of the last glyph in that glyph row area.
17868 X is the current output position assigned to the new glyph string
17869 constructed. HL overrides that face of the glyph; e.g. it is
17870 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17871 x-position of the drawing area. */
17872
17873 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17874 do { \
17875 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17876 int face_id = (row)->glyphs[area][START].face_id; \
17877 struct face *base_face = FACE_FROM_ID (f, face_id); \
17878 struct composition *cmp = composition_table[cmp_id]; \
17879 int glyph_len = cmp->glyph_len; \
17880 XChar2b *char2b; \
17881 struct face **faces; \
17882 struct glyph_string *first_s = NULL; \
17883 int n; \
17884 \
17885 base_face = base_face->ascii_face; \
17886 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17887 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17888 /* At first, fill in `char2b' and `faces'. */ \
17889 for (n = 0; n < glyph_len; n++) \
17890 { \
17891 int c = COMPOSITION_GLYPH (cmp, n); \
17892 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17893 faces[n] = FACE_FROM_ID (f, this_face_id); \
17894 get_char_face_and_encoding (f, c, this_face_id, \
17895 char2b + n, 1, 1); \
17896 } \
17897 \
17898 /* Make glyph_strings for each glyph sequence that is drawable by \
17899 the same face, and append them to HEAD/TAIL. */ \
17900 for (n = 0; n < cmp->glyph_len;) \
17901 { \
17902 s = (struct glyph_string *) alloca (sizeof *s); \
17903 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17904 append_glyph_string (&(HEAD), &(TAIL), s); \
17905 s->cmp = cmp; \
17906 s->gidx = n; \
17907 s->x = (X); \
17908 \
17909 if (n == 0) \
17910 first_s = s; \
17911 \
17912 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17913 } \
17914 \
17915 ++START; \
17916 s = first_s; \
17917 } while (0)
17918
17919
17920 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17921 of AREA of glyph row ROW on window W between indices START and END.
17922 HL overrides the face for drawing glyph strings, e.g. it is
17923 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17924 x-positions of the drawing area.
17925
17926 This is an ugly monster macro construct because we must use alloca
17927 to allocate glyph strings (because draw_glyphs can be called
17928 asynchronously). */
17929
17930 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17931 do \
17932 { \
17933 HEAD = TAIL = NULL; \
17934 while (START < END) \
17935 { \
17936 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17937 switch (first_glyph->type) \
17938 { \
17939 case CHAR_GLYPH: \
17940 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17941 HL, X, LAST_X); \
17942 break; \
17943 \
17944 case COMPOSITE_GLYPH: \
17945 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17946 HL, X, LAST_X); \
17947 break; \
17948 \
17949 case STRETCH_GLYPH: \
17950 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17951 HL, X, LAST_X); \
17952 break; \
17953 \
17954 case IMAGE_GLYPH: \
17955 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17956 HL, X, LAST_X); \
17957 break; \
17958 \
17959 default: \
17960 abort (); \
17961 } \
17962 \
17963 set_glyph_string_background_width (s, START, LAST_X); \
17964 (X) += s->width; \
17965 } \
17966 } \
17967 while (0)
17968
17969
17970 /* Draw glyphs between START and END in AREA of ROW on window W,
17971 starting at x-position X. X is relative to AREA in W. HL is a
17972 face-override with the following meaning:
17973
17974 DRAW_NORMAL_TEXT draw normally
17975 DRAW_CURSOR draw in cursor face
17976 DRAW_MOUSE_FACE draw in mouse face.
17977 DRAW_INVERSE_VIDEO draw in mode line face
17978 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17979 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17980
17981 If OVERLAPS_P is non-zero, draw only the foreground of characters
17982 and clip to the physical height of ROW.
17983
17984 Value is the x-position reached, relative to AREA of W. */
17985
17986 static int
17987 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17988 struct window *w;
17989 int x;
17990 struct glyph_row *row;
17991 enum glyph_row_area area;
17992 int start, end;
17993 enum draw_glyphs_face hl;
17994 int overlaps_p;
17995 {
17996 struct glyph_string *head, *tail;
17997 struct glyph_string *s;
17998 int last_x, area_width;
17999 int x_reached;
18000 int i, j;
18001 struct frame *f = XFRAME (WINDOW_FRAME (w));
18002 DECLARE_HDC (hdc);
18003
18004 ALLOCATE_HDC (hdc, f);
18005
18006 /* Let's rather be paranoid than getting a SEGV. */
18007 end = min (end, row->used[area]);
18008 start = max (0, start);
18009 start = min (end, start);
18010
18011 /* Translate X to frame coordinates. Set last_x to the right
18012 end of the drawing area. */
18013 if (row->full_width_p)
18014 {
18015 /* X is relative to the left edge of W, without scroll bars
18016 or fringes. */
18017 x += WINDOW_LEFT_EDGE_X (w);
18018 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18019 }
18020 else
18021 {
18022 int area_left = window_box_left (w, area);
18023 x += area_left;
18024 area_width = window_box_width (w, area);
18025 last_x = area_left + area_width;
18026 }
18027
18028 /* Build a doubly-linked list of glyph_string structures between
18029 head and tail from what we have to draw. Note that the macro
18030 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18031 the reason we use a separate variable `i'. */
18032 i = start;
18033 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18034 if (tail)
18035 x_reached = tail->x + tail->background_width;
18036 else
18037 x_reached = x;
18038
18039 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18040 the row, redraw some glyphs in front or following the glyph
18041 strings built above. */
18042 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18043 {
18044 int dummy_x = 0;
18045 struct glyph_string *h, *t;
18046
18047 /* Compute overhangs for all glyph strings. */
18048 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
18049 for (s = head; s; s = s->next)
18050 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
18051
18052 /* Prepend glyph strings for glyphs in front of the first glyph
18053 string that are overwritten because of the first glyph
18054 string's left overhang. The background of all strings
18055 prepended must be drawn because the first glyph string
18056 draws over it. */
18057 i = left_overwritten (head);
18058 if (i >= 0)
18059 {
18060 j = i;
18061 BUILD_GLYPH_STRINGS (j, start, h, t,
18062 DRAW_NORMAL_TEXT, dummy_x, last_x);
18063 start = i;
18064 compute_overhangs_and_x (t, head->x, 1);
18065 prepend_glyph_string_lists (&head, &tail, h, t);
18066 }
18067
18068 /* Prepend glyph strings for glyphs in front of the first glyph
18069 string that overwrite that glyph string because of their
18070 right overhang. For these strings, only the foreground must
18071 be drawn, because it draws over the glyph string at `head'.
18072 The background must not be drawn because this would overwrite
18073 right overhangs of preceding glyphs for which no glyph
18074 strings exist. */
18075 i = left_overwriting (head);
18076 if (i >= 0)
18077 {
18078 BUILD_GLYPH_STRINGS (i, start, h, t,
18079 DRAW_NORMAL_TEXT, dummy_x, last_x);
18080 for (s = h; s; s = s->next)
18081 s->background_filled_p = 1;
18082 compute_overhangs_and_x (t, head->x, 1);
18083 prepend_glyph_string_lists (&head, &tail, h, t);
18084 }
18085
18086 /* Append glyphs strings for glyphs following the last glyph
18087 string tail that are overwritten by tail. The background of
18088 these strings has to be drawn because tail's foreground draws
18089 over it. */
18090 i = right_overwritten (tail);
18091 if (i >= 0)
18092 {
18093 BUILD_GLYPH_STRINGS (end, i, h, t,
18094 DRAW_NORMAL_TEXT, x, last_x);
18095 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18096 append_glyph_string_lists (&head, &tail, h, t);
18097 }
18098
18099 /* Append glyph strings for glyphs following the last glyph
18100 string tail that overwrite tail. The foreground of such
18101 glyphs has to be drawn because it writes into the background
18102 of tail. The background must not be drawn because it could
18103 paint over the foreground of following glyphs. */
18104 i = right_overwriting (tail);
18105 if (i >= 0)
18106 {
18107 BUILD_GLYPH_STRINGS (end, i, h, t,
18108 DRAW_NORMAL_TEXT, x, last_x);
18109 for (s = h; s; s = s->next)
18110 s->background_filled_p = 1;
18111 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18112 append_glyph_string_lists (&head, &tail, h, t);
18113 }
18114 }
18115
18116 /* Draw all strings. */
18117 for (s = head; s; s = s->next)
18118 FRAME_RIF (f)->draw_glyph_string (s);
18119
18120 if (area == TEXT_AREA
18121 && !row->full_width_p
18122 /* When drawing overlapping rows, only the glyph strings'
18123 foreground is drawn, which doesn't erase a cursor
18124 completely. */
18125 && !overlaps_p)
18126 {
18127 int x0 = head ? head->x : x;
18128 int x1 = tail ? tail->x + tail->background_width : x;
18129
18130 int text_left = window_box_left (w, TEXT_AREA);
18131 x0 -= text_left;
18132 x1 -= text_left;
18133
18134 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18135 row->y, MATRIX_ROW_BOTTOM_Y (row));
18136 }
18137
18138 /* Value is the x-position up to which drawn, relative to AREA of W.
18139 This doesn't include parts drawn because of overhangs. */
18140 if (row->full_width_p)
18141 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18142 else
18143 x_reached -= window_box_left (w, area);
18144
18145 RELEASE_HDC (hdc, f);
18146
18147 return x_reached;
18148 }
18149
18150
18151 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18152 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18153
18154 static INLINE void
18155 append_glyph (it)
18156 struct it *it;
18157 {
18158 struct glyph *glyph;
18159 enum glyph_row_area area = it->area;
18160
18161 xassert (it->glyph_row);
18162 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18163
18164 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18165 if (glyph < it->glyph_row->glyphs[area + 1])
18166 {
18167 glyph->charpos = CHARPOS (it->position);
18168 glyph->object = it->object;
18169 glyph->pixel_width = it->pixel_width;
18170 glyph->ascent = it->ascent;
18171 glyph->descent = it->descent;
18172 glyph->voffset = it->voffset;
18173 glyph->type = CHAR_GLYPH;
18174 glyph->multibyte_p = it->multibyte_p;
18175 glyph->left_box_line_p = it->start_of_box_run_p;
18176 glyph->right_box_line_p = it->end_of_box_run_p;
18177 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18178 || it->phys_descent > it->descent);
18179 glyph->padding_p = 0;
18180 glyph->glyph_not_available_p = it->glyph_not_available_p;
18181 glyph->face_id = it->face_id;
18182 glyph->u.ch = it->char_to_display;
18183 glyph->font_type = FONT_TYPE_UNKNOWN;
18184 ++it->glyph_row->used[area];
18185 }
18186 }
18187
18188 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18189 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18190
18191 static INLINE void
18192 append_composite_glyph (it)
18193 struct it *it;
18194 {
18195 struct glyph *glyph;
18196 enum glyph_row_area area = it->area;
18197
18198 xassert (it->glyph_row);
18199
18200 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18201 if (glyph < it->glyph_row->glyphs[area + 1])
18202 {
18203 glyph->charpos = CHARPOS (it->position);
18204 glyph->object = it->object;
18205 glyph->pixel_width = it->pixel_width;
18206 glyph->ascent = it->ascent;
18207 glyph->descent = it->descent;
18208 glyph->voffset = it->voffset;
18209 glyph->type = COMPOSITE_GLYPH;
18210 glyph->multibyte_p = it->multibyte_p;
18211 glyph->left_box_line_p = it->start_of_box_run_p;
18212 glyph->right_box_line_p = it->end_of_box_run_p;
18213 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18214 || it->phys_descent > it->descent);
18215 glyph->padding_p = 0;
18216 glyph->glyph_not_available_p = 0;
18217 glyph->face_id = it->face_id;
18218 glyph->u.cmp_id = it->cmp_id;
18219 glyph->font_type = FONT_TYPE_UNKNOWN;
18220 ++it->glyph_row->used[area];
18221 }
18222 }
18223
18224
18225 /* Change IT->ascent and IT->height according to the setting of
18226 IT->voffset. */
18227
18228 static INLINE void
18229 take_vertical_position_into_account (it)
18230 struct it *it;
18231 {
18232 if (it->voffset)
18233 {
18234 if (it->voffset < 0)
18235 /* Increase the ascent so that we can display the text higher
18236 in the line. */
18237 it->ascent += abs (it->voffset);
18238 else
18239 /* Increase the descent so that we can display the text lower
18240 in the line. */
18241 it->descent += it->voffset;
18242 }
18243 }
18244
18245
18246 /* Produce glyphs/get display metrics for the image IT is loaded with.
18247 See the description of struct display_iterator in dispextern.h for
18248 an overview of struct display_iterator. */
18249
18250 static void
18251 produce_image_glyph (it)
18252 struct it *it;
18253 {
18254 struct image *img;
18255 struct face *face;
18256 int face_ascent, glyph_ascent;
18257
18258 xassert (it->what == IT_IMAGE);
18259
18260 face = FACE_FROM_ID (it->f, it->face_id);
18261 img = IMAGE_FROM_ID (it->f, it->image_id);
18262 xassert (img);
18263
18264 /* Make sure X resources of the face and image are loaded. */
18265 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18266 prepare_image_for_display (it->f, img);
18267
18268 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face);
18269 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
18270 it->pixel_width = img->width + 2 * img->hmargin;
18271
18272 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18273 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18274 if (face_ascent > it->ascent)
18275 it->ascent = it->phys_ascent = face_ascent;
18276
18277 it->nglyphs = 1;
18278
18279 if (face->box != FACE_NO_BOX)
18280 {
18281 if (face->box_line_width > 0)
18282 {
18283 it->ascent += face->box_line_width;
18284 it->descent += face->box_line_width;
18285 }
18286
18287 if (it->start_of_box_run_p)
18288 it->pixel_width += abs (face->box_line_width);
18289 if (it->end_of_box_run_p)
18290 it->pixel_width += abs (face->box_line_width);
18291 }
18292
18293 take_vertical_position_into_account (it);
18294
18295 if (it->glyph_row)
18296 {
18297 struct glyph *glyph;
18298 enum glyph_row_area area = it->area;
18299
18300 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18301 if (glyph < it->glyph_row->glyphs[area + 1])
18302 {
18303 glyph->charpos = CHARPOS (it->position);
18304 glyph->object = it->object;
18305 glyph->pixel_width = it->pixel_width;
18306 glyph->ascent = glyph_ascent;
18307 glyph->descent = it->descent;
18308 glyph->voffset = it->voffset;
18309 glyph->type = IMAGE_GLYPH;
18310 glyph->multibyte_p = it->multibyte_p;
18311 glyph->left_box_line_p = it->start_of_box_run_p;
18312 glyph->right_box_line_p = it->end_of_box_run_p;
18313 glyph->overlaps_vertically_p = 0;
18314 glyph->padding_p = 0;
18315 glyph->glyph_not_available_p = 0;
18316 glyph->face_id = it->face_id;
18317 glyph->u.img_id = img->id;
18318 glyph->font_type = FONT_TYPE_UNKNOWN;
18319 ++it->glyph_row->used[area];
18320 }
18321 }
18322 }
18323
18324
18325 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18326 of the glyph, WIDTH and HEIGHT are the width and height of the
18327 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18328
18329 static void
18330 append_stretch_glyph (it, object, width, height, ascent)
18331 struct it *it;
18332 Lisp_Object object;
18333 int width, height;
18334 int ascent;
18335 {
18336 struct glyph *glyph;
18337 enum glyph_row_area area = it->area;
18338
18339 xassert (ascent >= 0 && ascent <= height);
18340
18341 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18342 if (glyph < it->glyph_row->glyphs[area + 1])
18343 {
18344 glyph->charpos = CHARPOS (it->position);
18345 glyph->object = object;
18346 glyph->pixel_width = width;
18347 glyph->ascent = ascent;
18348 glyph->descent = height - ascent;
18349 glyph->voffset = it->voffset;
18350 glyph->type = STRETCH_GLYPH;
18351 glyph->multibyte_p = it->multibyte_p;
18352 glyph->left_box_line_p = it->start_of_box_run_p;
18353 glyph->right_box_line_p = it->end_of_box_run_p;
18354 glyph->overlaps_vertically_p = 0;
18355 glyph->padding_p = 0;
18356 glyph->glyph_not_available_p = 0;
18357 glyph->face_id = it->face_id;
18358 glyph->u.stretch.ascent = ascent;
18359 glyph->u.stretch.height = height;
18360 glyph->font_type = FONT_TYPE_UNKNOWN;
18361 ++it->glyph_row->used[area];
18362 }
18363 }
18364
18365
18366 /* Calculate a width or height in pixels from a specification using
18367 the following elements:
18368
18369 SPEC ::=
18370 NUM - a (fractional) multiple of the default font width/height
18371 (NUM) - specifies exactly NUM pixels
18372 UNIT - a fixed number of pixels, see below.
18373 ELEMENT - size of a display element in pixels, see below.
18374 (NUM . SPEC) - equals NUM * SPEC
18375 (+ SPEC SPEC ...) - add pixel values
18376 (- SPEC SPEC ...) - subtract pixel values
18377 (- SPEC) - negate pixel value
18378
18379 NUM ::=
18380 INT or FLOAT - a number constant
18381 SYMBOL - use symbol's (buffer local) variable binding.
18382
18383 UNIT ::=
18384 in - pixels per inch *)
18385 mm - pixels per 1/1000 meter *)
18386 cm - pixels per 1/100 meter *)
18387 width - width of current font in pixels.
18388 height - height of current font in pixels.
18389
18390 *) using the ratio(s) defined in display-pixels-per-inch.
18391
18392 ELEMENT ::=
18393
18394 left-fringe - left fringe width in pixels
18395 (left-fringe . nil) - left fringe width if inside margins, else 0
18396 (left-fringe . t) - left fringe width if outside margins, else 0
18397
18398 right-fringe - right fringe width in pixels
18399 (right-fringe . nil) - right fringe width if inside margins, else 0
18400 (right-fringe . t) - right fringe width if outside margins, else 0
18401
18402 left-margin - left margin width in pixels
18403 right-margin - right margin width in pixels
18404
18405 scroll-bar - scroll-bar area width in pixels
18406 (scroll-bar . left) - scroll-bar width if on left, else 0
18407 (scroll-bar . right) - scroll-bar width if on right, else 0
18408
18409 Examples:
18410
18411 Pixels corresponding to 5 inches:
18412 (5 . in)
18413
18414 Total width of non-text areas on left side of window:
18415 (+ left-fringe left-margin (scroll-bar . left))
18416
18417 Total width of fringes if inside display margins:
18418 (+ (left-fringe) (right-fringe))
18419
18420 Width of left margin minus width of 1 character in the default font:
18421 (- left-margin 1)
18422
18423 Width of left margin minus width of 2 characters in the current font:
18424 (- left-margin (2 . width))
18425
18426 Width of left fringe plus left margin minus one pixel:
18427 (- (+ left-fringe left-margin) (1))
18428 (+ left-fringe left-margin (- (1)))
18429 (+ left-fringe left-margin (-1))
18430
18431 */
18432
18433 #define NUMVAL(X) \
18434 ((INTEGERP (X) || FLOATP (X)) \
18435 ? XFLOATINT (X) \
18436 : - 1)
18437
18438 static int
18439 calc_pixel_width_or_height (res, it, prop, font, width_p)
18440 double *res;
18441 struct it *it;
18442 Lisp_Object prop;
18443 XFontStruct *font;
18444 int width_p;
18445 {
18446 double pixels;
18447
18448 #define OK_PIXELS(val) ((*res = (val)), 1)
18449
18450 if (SYMBOLP (prop))
18451 {
18452 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18453 {
18454 char *unit = SDATA (SYMBOL_NAME (prop));
18455
18456 if (unit[0] == 'i' && unit[1] == 'n')
18457 pixels = 1.0;
18458 else if (unit[0] == 'm' && unit[1] == 'm')
18459 pixels = 25.4;
18460 else if (unit[0] == 'c' && unit[1] == 'm')
18461 pixels = 2.54;
18462 else
18463 pixels = 0;
18464 if (pixels > 0)
18465 {
18466 double ppi;
18467 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18468 || (CONSP (Vdisplay_pixels_per_inch)
18469 && (ppi = (width_p
18470 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18471 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18472 ppi > 0)))
18473 return OK_PIXELS (ppi / pixels);
18474
18475 return 0;
18476 }
18477 }
18478
18479 if (EQ (prop, Qheight))
18480 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18481 if (EQ (prop, Qwidth))
18482 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18483 if (EQ (prop, Qleft_fringe))
18484 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18485 if (EQ (prop, Qright_fringe))
18486 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18487 if (EQ (prop, Qleft_margin))
18488 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18489 if (EQ (prop, Qright_margin))
18490 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18491 if (EQ (prop, Qscroll_bar))
18492 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18493
18494 prop = Fbuffer_local_value (prop, it->w->buffer);
18495 }
18496
18497 if (INTEGERP (prop) || FLOATP (prop))
18498 {
18499 int base_unit = (width_p
18500 ? FRAME_COLUMN_WIDTH (it->f)
18501 : FRAME_LINE_HEIGHT (it->f));
18502 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18503 }
18504
18505 if (CONSP (prop))
18506 {
18507 Lisp_Object car = XCAR (prop);
18508 Lisp_Object cdr = XCDR (prop);
18509
18510 if (SYMBOLP (car))
18511 {
18512 if (EQ (car, Qplus) || EQ (car, Qminus))
18513 {
18514 int first = 1;
18515 double px;
18516
18517 pixels = 0;
18518 while (CONSP (cdr))
18519 {
18520 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr), font, width_p))
18521 return 0;
18522 if (first)
18523 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18524 else
18525 pixels += px;
18526 cdr = XCDR (cdr);
18527 }
18528 if (EQ (car, Qminus))
18529 pixels = -pixels;
18530 return OK_PIXELS (pixels);
18531 }
18532
18533 if (EQ (car, Qleft_fringe))
18534 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18535 == !NILP (cdr))
18536 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
18537 : 0);
18538 if (EQ (car, Qright_fringe))
18539 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18540 == !NILP (cdr))
18541 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18542 : 0);
18543 if (EQ (car, Qscroll_bar))
18544 return OK_PIXELS ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18545 == EQ (cdr, Qleft))
18546 ? WINDOW_SCROLL_BAR_AREA_WIDTH (it->w)
18547 : 0);
18548
18549 car = Fbuffer_local_value (car, it->w->buffer);
18550 }
18551
18552 if (INTEGERP (car) || FLOATP (car))
18553 {
18554 double fact;
18555 pixels = XFLOATINT (car);
18556 if (NILP (cdr))
18557 return OK_PIXELS (pixels);
18558 if (calc_pixel_width_or_height (&fact, it, cdr, font, width_p))
18559 return OK_PIXELS (pixels * fact);
18560 return 0;
18561 }
18562
18563 return 0;
18564 }
18565
18566 return 0;
18567 }
18568
18569 /* Produce a stretch glyph for iterator IT. IT->object is the value
18570 of the glyph property displayed. The value must be a list
18571 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18572 being recognized:
18573
18574 1. `:width WIDTH' specifies that the space should be WIDTH *
18575 canonical char width wide. WIDTH may be an integer or floating
18576 point number.
18577
18578 2. `:relative-width FACTOR' specifies that the width of the stretch
18579 should be computed from the width of the first character having the
18580 `glyph' property, and should be FACTOR times that width.
18581
18582 3. `:align-to HPOS' specifies that the space should be wide enough
18583 to reach HPOS, a value in canonical character units.
18584
18585 Exactly one of the above pairs must be present.
18586
18587 4. `:height HEIGHT' specifies that the height of the stretch produced
18588 should be HEIGHT, measured in canonical character units.
18589
18590 5. `:relative-height FACTOR' specifies that the height of the
18591 stretch should be FACTOR times the height of the characters having
18592 the glyph property.
18593
18594 Either none or exactly one of 4 or 5 must be present.
18595
18596 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18597 of the stretch should be used for the ascent of the stretch.
18598 ASCENT must be in the range 0 <= ASCENT <= 100. */
18599
18600 static void
18601 produce_stretch_glyph (it)
18602 struct it *it;
18603 {
18604 /* (space :width WIDTH :height HEIGHT ...) */
18605 Lisp_Object prop, plist;
18606 int width = 0, height = 0;
18607 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18608 int ascent = 0;
18609 double tem;
18610 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18611 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18612
18613 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18614
18615 /* List should start with `space'. */
18616 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18617 plist = XCDR (it->object);
18618
18619 /* Compute the width of the stretch. */
18620 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18621 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18622 {
18623 /* Absolute width `:width WIDTH' specified and valid. */
18624 zero_width_ok_p = 1;
18625 width = (int)tem;
18626 }
18627 else if (prop = Fplist_get (plist, QCrelative_width),
18628 NUMVAL (prop) > 0)
18629 {
18630 /* Relative width `:relative-width FACTOR' specified and valid.
18631 Compute the width of the characters having the `glyph'
18632 property. */
18633 struct it it2;
18634 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18635
18636 it2 = *it;
18637 if (it->multibyte_p)
18638 {
18639 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18640 - IT_BYTEPOS (*it));
18641 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18642 }
18643 else
18644 it2.c = *p, it2.len = 1;
18645
18646 it2.glyph_row = NULL;
18647 it2.what = IT_CHARACTER;
18648 x_produce_glyphs (&it2);
18649 width = NUMVAL (prop) * it2.pixel_width;
18650 }
18651 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18652 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18653 {
18654 width = max (0, (int)tem - it->current_x);
18655 zero_width_ok_p = 1;
18656 }
18657 else
18658 /* Nothing specified -> width defaults to canonical char width. */
18659 width = FRAME_COLUMN_WIDTH (it->f);
18660
18661 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18662 width = 1;
18663
18664 /* Compute height. */
18665 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18666 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18667 {
18668 height = (int)tem;
18669 zero_height_ok_p = 1;
18670 }
18671 else if (prop = Fplist_get (plist, QCrelative_height),
18672 NUMVAL (prop) > 0)
18673 height = FONT_HEIGHT (font) * NUMVAL (prop);
18674 else
18675 height = FONT_HEIGHT (font);
18676
18677 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18678 height = 1;
18679
18680 /* Compute percentage of height used for ascent. If
18681 `:ascent ASCENT' is present and valid, use that. Otherwise,
18682 derive the ascent from the font in use. */
18683 if (prop = Fplist_get (plist, QCascent),
18684 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18685 ascent = height * NUMVAL (prop) / 100.0;
18686 else if (!NILP (prop)
18687 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18688 ascent = min (max (0, (int)tem), height);
18689 else
18690 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18691
18692 if (width > 0 && height > 0 && it->glyph_row)
18693 {
18694 Lisp_Object object = it->stack[it->sp - 1].string;
18695 if (!STRINGP (object))
18696 object = it->w->buffer;
18697 append_stretch_glyph (it, object, width, height, ascent);
18698 }
18699
18700 it->pixel_width = width;
18701 it->ascent = it->phys_ascent = ascent;
18702 it->descent = it->phys_descent = height - it->ascent;
18703 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18704
18705 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18706 {
18707 if (face->box_line_width > 0)
18708 {
18709 it->ascent += face->box_line_width;
18710 it->descent += face->box_line_width;
18711 }
18712
18713 if (it->start_of_box_run_p)
18714 it->pixel_width += abs (face->box_line_width);
18715 if (it->end_of_box_run_p)
18716 it->pixel_width += abs (face->box_line_width);
18717 }
18718
18719 take_vertical_position_into_account (it);
18720 }
18721
18722 /* RIF:
18723 Produce glyphs/get display metrics for the display element IT is
18724 loaded with. See the description of struct display_iterator in
18725 dispextern.h for an overview of struct display_iterator. */
18726
18727 void
18728 x_produce_glyphs (it)
18729 struct it *it;
18730 {
18731 it->glyph_not_available_p = 0;
18732
18733 if (it->what == IT_CHARACTER)
18734 {
18735 XChar2b char2b;
18736 XFontStruct *font;
18737 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18738 XCharStruct *pcm;
18739 int font_not_found_p;
18740 struct font_info *font_info;
18741 int boff; /* baseline offset */
18742 /* We may change it->multibyte_p upon unibyte<->multibyte
18743 conversion. So, save the current value now and restore it
18744 later.
18745
18746 Note: It seems that we don't have to record multibyte_p in
18747 struct glyph because the character code itself tells if or
18748 not the character is multibyte. Thus, in the future, we must
18749 consider eliminating the field `multibyte_p' in the struct
18750 glyph. */
18751 int saved_multibyte_p = it->multibyte_p;
18752
18753 /* Maybe translate single-byte characters to multibyte, or the
18754 other way. */
18755 it->char_to_display = it->c;
18756 if (!ASCII_BYTE_P (it->c))
18757 {
18758 if (unibyte_display_via_language_environment
18759 && SINGLE_BYTE_CHAR_P (it->c)
18760 && (it->c >= 0240
18761 || !NILP (Vnonascii_translation_table)))
18762 {
18763 it->char_to_display = unibyte_char_to_multibyte (it->c);
18764 it->multibyte_p = 1;
18765 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18766 face = FACE_FROM_ID (it->f, it->face_id);
18767 }
18768 else if (!SINGLE_BYTE_CHAR_P (it->c)
18769 && !it->multibyte_p)
18770 {
18771 it->multibyte_p = 1;
18772 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18773 face = FACE_FROM_ID (it->f, it->face_id);
18774 }
18775 }
18776
18777 /* Get font to use. Encode IT->char_to_display. */
18778 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18779 &char2b, it->multibyte_p, 0);
18780 font = face->font;
18781
18782 /* When no suitable font found, use the default font. */
18783 font_not_found_p = font == NULL;
18784 if (font_not_found_p)
18785 {
18786 font = FRAME_FONT (it->f);
18787 boff = FRAME_BASELINE_OFFSET (it->f);
18788 font_info = NULL;
18789 }
18790 else
18791 {
18792 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18793 boff = font_info->baseline_offset;
18794 if (font_info->vertical_centering)
18795 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18796 }
18797
18798 if (it->char_to_display >= ' '
18799 && (!it->multibyte_p || it->char_to_display < 128))
18800 {
18801 /* Either unibyte or ASCII. */
18802 int stretched_p;
18803
18804 it->nglyphs = 1;
18805
18806 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18807 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18808 it->ascent = FONT_BASE (font) + boff;
18809 it->descent = FONT_DESCENT (font) - boff;
18810
18811 if (pcm)
18812 {
18813 it->phys_ascent = pcm->ascent + boff;
18814 it->phys_descent = pcm->descent - boff;
18815 it->pixel_width = pcm->width;
18816 }
18817 else
18818 {
18819 it->glyph_not_available_p = 1;
18820 it->phys_ascent = FONT_BASE (font) + boff;
18821 it->phys_descent = FONT_DESCENT (font) - boff;
18822 it->pixel_width = FONT_WIDTH (font);
18823 }
18824
18825 /* If this is a space inside a region of text with
18826 `space-width' property, change its width. */
18827 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18828 if (stretched_p)
18829 it->pixel_width *= XFLOATINT (it->space_width);
18830
18831 /* If face has a box, add the box thickness to the character
18832 height. If character has a box line to the left and/or
18833 right, add the box line width to the character's width. */
18834 if (face->box != FACE_NO_BOX)
18835 {
18836 int thick = face->box_line_width;
18837
18838 if (thick > 0)
18839 {
18840 it->ascent += thick;
18841 it->descent += thick;
18842 }
18843 else
18844 thick = -thick;
18845
18846 if (it->start_of_box_run_p)
18847 it->pixel_width += thick;
18848 if (it->end_of_box_run_p)
18849 it->pixel_width += thick;
18850 }
18851
18852 /* If face has an overline, add the height of the overline
18853 (1 pixel) and a 1 pixel margin to the character height. */
18854 if (face->overline_p)
18855 it->ascent += 2;
18856
18857 take_vertical_position_into_account (it);
18858
18859 /* If we have to actually produce glyphs, do it. */
18860 if (it->glyph_row)
18861 {
18862 if (stretched_p)
18863 {
18864 /* Translate a space with a `space-width' property
18865 into a stretch glyph. */
18866 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18867 / FONT_HEIGHT (font));
18868 append_stretch_glyph (it, it->object, it->pixel_width,
18869 it->ascent + it->descent, ascent);
18870 }
18871 else
18872 append_glyph (it);
18873
18874 /* If characters with lbearing or rbearing are displayed
18875 in this line, record that fact in a flag of the
18876 glyph row. This is used to optimize X output code. */
18877 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18878 it->glyph_row->contains_overlapping_glyphs_p = 1;
18879 }
18880 }
18881 else if (it->char_to_display == '\n')
18882 {
18883 /* A newline has no width but we need the height of the line. */
18884 it->pixel_width = 0;
18885 it->nglyphs = 0;
18886 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18887 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18888
18889 if (face->box != FACE_NO_BOX
18890 && face->box_line_width > 0)
18891 {
18892 it->ascent += face->box_line_width;
18893 it->descent += face->box_line_width;
18894 }
18895 }
18896 else if (it->char_to_display == '\t')
18897 {
18898 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18899 int x = it->current_x + it->continuation_lines_width;
18900 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18901
18902 /* If the distance from the current position to the next tab
18903 stop is less than a canonical character width, use the
18904 tab stop after that. */
18905 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18906 next_tab_x += tab_width;
18907
18908 it->pixel_width = next_tab_x - x;
18909 it->nglyphs = 1;
18910 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18911 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18912
18913 if (it->glyph_row)
18914 {
18915 append_stretch_glyph (it, it->object, it->pixel_width,
18916 it->ascent + it->descent, it->ascent);
18917 }
18918 }
18919 else
18920 {
18921 /* A multi-byte character. Assume that the display width of the
18922 character is the width of the character multiplied by the
18923 width of the font. */
18924
18925 /* If we found a font, this font should give us the right
18926 metrics. If we didn't find a font, use the frame's
18927 default font and calculate the width of the character
18928 from the charset width; this is what old redisplay code
18929 did. */
18930
18931 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18932 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18933
18934 if (font_not_found_p || !pcm)
18935 {
18936 int charset = CHAR_CHARSET (it->char_to_display);
18937
18938 it->glyph_not_available_p = 1;
18939 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18940 * CHARSET_WIDTH (charset));
18941 it->phys_ascent = FONT_BASE (font) + boff;
18942 it->phys_descent = FONT_DESCENT (font) - boff;
18943 }
18944 else
18945 {
18946 it->pixel_width = pcm->width;
18947 it->phys_ascent = pcm->ascent + boff;
18948 it->phys_descent = pcm->descent - boff;
18949 if (it->glyph_row
18950 && (pcm->lbearing < 0
18951 || pcm->rbearing > pcm->width))
18952 it->glyph_row->contains_overlapping_glyphs_p = 1;
18953 }
18954 it->nglyphs = 1;
18955 it->ascent = FONT_BASE (font) + boff;
18956 it->descent = FONT_DESCENT (font) - boff;
18957 if (face->box != FACE_NO_BOX)
18958 {
18959 int thick = face->box_line_width;
18960
18961 if (thick > 0)
18962 {
18963 it->ascent += thick;
18964 it->descent += thick;
18965 }
18966 else
18967 thick = - thick;
18968
18969 if (it->start_of_box_run_p)
18970 it->pixel_width += thick;
18971 if (it->end_of_box_run_p)
18972 it->pixel_width += thick;
18973 }
18974
18975 /* If face has an overline, add the height of the overline
18976 (1 pixel) and a 1 pixel margin to the character height. */
18977 if (face->overline_p)
18978 it->ascent += 2;
18979
18980 take_vertical_position_into_account (it);
18981
18982 if (it->glyph_row)
18983 append_glyph (it);
18984 }
18985 it->multibyte_p = saved_multibyte_p;
18986 }
18987 else if (it->what == IT_COMPOSITION)
18988 {
18989 /* Note: A composition is represented as one glyph in the
18990 glyph matrix. There are no padding glyphs. */
18991 XChar2b char2b;
18992 XFontStruct *font;
18993 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18994 XCharStruct *pcm;
18995 int font_not_found_p;
18996 struct font_info *font_info;
18997 int boff; /* baseline offset */
18998 struct composition *cmp = composition_table[it->cmp_id];
18999
19000 /* Maybe translate single-byte characters to multibyte. */
19001 it->char_to_display = it->c;
19002 if (unibyte_display_via_language_environment
19003 && SINGLE_BYTE_CHAR_P (it->c)
19004 && (it->c >= 0240
19005 || (it->c >= 0200
19006 && !NILP (Vnonascii_translation_table))))
19007 {
19008 it->char_to_display = unibyte_char_to_multibyte (it->c);
19009 }
19010
19011 /* Get face and font to use. Encode IT->char_to_display. */
19012 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19013 face = FACE_FROM_ID (it->f, it->face_id);
19014 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19015 &char2b, it->multibyte_p, 0);
19016 font = face->font;
19017
19018 /* When no suitable font found, use the default font. */
19019 font_not_found_p = font == NULL;
19020 if (font_not_found_p)
19021 {
19022 font = FRAME_FONT (it->f);
19023 boff = FRAME_BASELINE_OFFSET (it->f);
19024 font_info = NULL;
19025 }
19026 else
19027 {
19028 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19029 boff = font_info->baseline_offset;
19030 if (font_info->vertical_centering)
19031 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19032 }
19033
19034 /* There are no padding glyphs, so there is only one glyph to
19035 produce for the composition. Important is that pixel_width,
19036 ascent and descent are the values of what is drawn by
19037 draw_glyphs (i.e. the values of the overall glyphs composed). */
19038 it->nglyphs = 1;
19039
19040 /* If we have not yet calculated pixel size data of glyphs of
19041 the composition for the current face font, calculate them
19042 now. Theoretically, we have to check all fonts for the
19043 glyphs, but that requires much time and memory space. So,
19044 here we check only the font of the first glyph. This leads
19045 to incorrect display very rarely, and C-l (recenter) can
19046 correct the display anyway. */
19047 if (cmp->font != (void *) font)
19048 {
19049 /* Ascent and descent of the font of the first character of
19050 this composition (adjusted by baseline offset). Ascent
19051 and descent of overall glyphs should not be less than
19052 them respectively. */
19053 int font_ascent = FONT_BASE (font) + boff;
19054 int font_descent = FONT_DESCENT (font) - boff;
19055 /* Bounding box of the overall glyphs. */
19056 int leftmost, rightmost, lowest, highest;
19057 int i, width, ascent, descent;
19058
19059 cmp->font = (void *) font;
19060
19061 /* Initialize the bounding box. */
19062 if (font_info
19063 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19064 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19065 {
19066 width = pcm->width;
19067 ascent = pcm->ascent;
19068 descent = pcm->descent;
19069 }
19070 else
19071 {
19072 width = FONT_WIDTH (font);
19073 ascent = FONT_BASE (font);
19074 descent = FONT_DESCENT (font);
19075 }
19076
19077 rightmost = width;
19078 lowest = - descent + boff;
19079 highest = ascent + boff;
19080 leftmost = 0;
19081
19082 if (font_info
19083 && font_info->default_ascent
19084 && CHAR_TABLE_P (Vuse_default_ascent)
19085 && !NILP (Faref (Vuse_default_ascent,
19086 make_number (it->char_to_display))))
19087 highest = font_info->default_ascent + boff;
19088
19089 /* Draw the first glyph at the normal position. It may be
19090 shifted to right later if some other glyphs are drawn at
19091 the left. */
19092 cmp->offsets[0] = 0;
19093 cmp->offsets[1] = boff;
19094
19095 /* Set cmp->offsets for the remaining glyphs. */
19096 for (i = 1; i < cmp->glyph_len; i++)
19097 {
19098 int left, right, btm, top;
19099 int ch = COMPOSITION_GLYPH (cmp, i);
19100 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19101
19102 face = FACE_FROM_ID (it->f, face_id);
19103 get_char_face_and_encoding (it->f, ch, face->id,
19104 &char2b, it->multibyte_p, 0);
19105 font = face->font;
19106 if (font == NULL)
19107 {
19108 font = FRAME_FONT (it->f);
19109 boff = FRAME_BASELINE_OFFSET (it->f);
19110 font_info = NULL;
19111 }
19112 else
19113 {
19114 font_info
19115 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19116 boff = font_info->baseline_offset;
19117 if (font_info->vertical_centering)
19118 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19119 }
19120
19121 if (font_info
19122 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19123 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19124 {
19125 width = pcm->width;
19126 ascent = pcm->ascent;
19127 descent = pcm->descent;
19128 }
19129 else
19130 {
19131 width = FONT_WIDTH (font);
19132 ascent = 1;
19133 descent = 0;
19134 }
19135
19136 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19137 {
19138 /* Relative composition with or without
19139 alternate chars. */
19140 left = (leftmost + rightmost - width) / 2;
19141 btm = - descent + boff;
19142 if (font_info && font_info->relative_compose
19143 && (! CHAR_TABLE_P (Vignore_relative_composition)
19144 || NILP (Faref (Vignore_relative_composition,
19145 make_number (ch)))))
19146 {
19147
19148 if (- descent >= font_info->relative_compose)
19149 /* One extra pixel between two glyphs. */
19150 btm = highest + 1;
19151 else if (ascent <= 0)
19152 /* One extra pixel between two glyphs. */
19153 btm = lowest - 1 - ascent - descent;
19154 }
19155 }
19156 else
19157 {
19158 /* A composition rule is specified by an integer
19159 value that encodes global and new reference
19160 points (GREF and NREF). GREF and NREF are
19161 specified by numbers as below:
19162
19163 0---1---2 -- ascent
19164 | |
19165 | |
19166 | |
19167 9--10--11 -- center
19168 | |
19169 ---3---4---5--- baseline
19170 | |
19171 6---7---8 -- descent
19172 */
19173 int rule = COMPOSITION_RULE (cmp, i);
19174 int gref, nref, grefx, grefy, nrefx, nrefy;
19175
19176 COMPOSITION_DECODE_RULE (rule, gref, nref);
19177 grefx = gref % 3, nrefx = nref % 3;
19178 grefy = gref / 3, nrefy = nref / 3;
19179
19180 left = (leftmost
19181 + grefx * (rightmost - leftmost) / 2
19182 - nrefx * width / 2);
19183 btm = ((grefy == 0 ? highest
19184 : grefy == 1 ? 0
19185 : grefy == 2 ? lowest
19186 : (highest + lowest) / 2)
19187 - (nrefy == 0 ? ascent + descent
19188 : nrefy == 1 ? descent - boff
19189 : nrefy == 2 ? 0
19190 : (ascent + descent) / 2));
19191 }
19192
19193 cmp->offsets[i * 2] = left;
19194 cmp->offsets[i * 2 + 1] = btm + descent;
19195
19196 /* Update the bounding box of the overall glyphs. */
19197 right = left + width;
19198 top = btm + descent + ascent;
19199 if (left < leftmost)
19200 leftmost = left;
19201 if (right > rightmost)
19202 rightmost = right;
19203 if (top > highest)
19204 highest = top;
19205 if (btm < lowest)
19206 lowest = btm;
19207 }
19208
19209 /* If there are glyphs whose x-offsets are negative,
19210 shift all glyphs to the right and make all x-offsets
19211 non-negative. */
19212 if (leftmost < 0)
19213 {
19214 for (i = 0; i < cmp->glyph_len; i++)
19215 cmp->offsets[i * 2] -= leftmost;
19216 rightmost -= leftmost;
19217 }
19218
19219 cmp->pixel_width = rightmost;
19220 cmp->ascent = highest;
19221 cmp->descent = - lowest;
19222 if (cmp->ascent < font_ascent)
19223 cmp->ascent = font_ascent;
19224 if (cmp->descent < font_descent)
19225 cmp->descent = font_descent;
19226 }
19227
19228 it->pixel_width = cmp->pixel_width;
19229 it->ascent = it->phys_ascent = cmp->ascent;
19230 it->descent = it->phys_descent = cmp->descent;
19231
19232 if (face->box != FACE_NO_BOX)
19233 {
19234 int thick = face->box_line_width;
19235
19236 if (thick > 0)
19237 {
19238 it->ascent += thick;
19239 it->descent += thick;
19240 }
19241 else
19242 thick = - thick;
19243
19244 if (it->start_of_box_run_p)
19245 it->pixel_width += thick;
19246 if (it->end_of_box_run_p)
19247 it->pixel_width += thick;
19248 }
19249
19250 /* If face has an overline, add the height of the overline
19251 (1 pixel) and a 1 pixel margin to the character height. */
19252 if (face->overline_p)
19253 it->ascent += 2;
19254
19255 take_vertical_position_into_account (it);
19256
19257 if (it->glyph_row)
19258 append_composite_glyph (it);
19259 }
19260 else if (it->what == IT_IMAGE)
19261 produce_image_glyph (it);
19262 else if (it->what == IT_STRETCH)
19263 produce_stretch_glyph (it);
19264
19265 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19266 because this isn't true for images with `:ascent 100'. */
19267 xassert (it->ascent >= 0 && it->descent >= 0);
19268 if (it->area == TEXT_AREA)
19269 it->current_x += it->pixel_width;
19270
19271 it->descent += it->extra_line_spacing;
19272
19273 it->max_ascent = max (it->max_ascent, it->ascent);
19274 it->max_descent = max (it->max_descent, it->descent);
19275 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19276 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19277 }
19278
19279 /* EXPORT for RIF:
19280 Output LEN glyphs starting at START at the nominal cursor position.
19281 Advance the nominal cursor over the text. The global variable
19282 updated_window contains the window being updated, updated_row is
19283 the glyph row being updated, and updated_area is the area of that
19284 row being updated. */
19285
19286 void
19287 x_write_glyphs (start, len)
19288 struct glyph *start;
19289 int len;
19290 {
19291 int x, hpos;
19292
19293 xassert (updated_window && updated_row);
19294 BLOCK_INPUT;
19295
19296 /* Write glyphs. */
19297
19298 hpos = start - updated_row->glyphs[updated_area];
19299 x = draw_glyphs (updated_window, output_cursor.x,
19300 updated_row, updated_area,
19301 hpos, hpos + len,
19302 DRAW_NORMAL_TEXT, 0);
19303
19304 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19305 if (updated_area == TEXT_AREA
19306 && updated_window->phys_cursor_on_p
19307 && updated_window->phys_cursor.vpos == output_cursor.vpos
19308 && updated_window->phys_cursor.hpos >= hpos
19309 && updated_window->phys_cursor.hpos < hpos + len)
19310 updated_window->phys_cursor_on_p = 0;
19311
19312 UNBLOCK_INPUT;
19313
19314 /* Advance the output cursor. */
19315 output_cursor.hpos += len;
19316 output_cursor.x = x;
19317 }
19318
19319
19320 /* EXPORT for RIF:
19321 Insert LEN glyphs from START at the nominal cursor position. */
19322
19323 void
19324 x_insert_glyphs (start, len)
19325 struct glyph *start;
19326 int len;
19327 {
19328 struct frame *f;
19329 struct window *w;
19330 int line_height, shift_by_width, shifted_region_width;
19331 struct glyph_row *row;
19332 struct glyph *glyph;
19333 int frame_x, frame_y, hpos;
19334
19335 xassert (updated_window && updated_row);
19336 BLOCK_INPUT;
19337 w = updated_window;
19338 f = XFRAME (WINDOW_FRAME (w));
19339
19340 /* Get the height of the line we are in. */
19341 row = updated_row;
19342 line_height = row->height;
19343
19344 /* Get the width of the glyphs to insert. */
19345 shift_by_width = 0;
19346 for (glyph = start; glyph < start + len; ++glyph)
19347 shift_by_width += glyph->pixel_width;
19348
19349 /* Get the width of the region to shift right. */
19350 shifted_region_width = (window_box_width (w, updated_area)
19351 - output_cursor.x
19352 - shift_by_width);
19353
19354 /* Shift right. */
19355 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19356 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19357
19358 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19359 line_height, shift_by_width);
19360
19361 /* Write the glyphs. */
19362 hpos = start - row->glyphs[updated_area];
19363 draw_glyphs (w, output_cursor.x, row, updated_area,
19364 hpos, hpos + len,
19365 DRAW_NORMAL_TEXT, 0);
19366
19367 /* Advance the output cursor. */
19368 output_cursor.hpos += len;
19369 output_cursor.x += shift_by_width;
19370 UNBLOCK_INPUT;
19371 }
19372
19373
19374 /* EXPORT for RIF:
19375 Erase the current text line from the nominal cursor position
19376 (inclusive) to pixel column TO_X (exclusive). The idea is that
19377 everything from TO_X onward is already erased.
19378
19379 TO_X is a pixel position relative to updated_area of
19380 updated_window. TO_X == -1 means clear to the end of this area. */
19381
19382 void
19383 x_clear_end_of_line (to_x)
19384 int to_x;
19385 {
19386 struct frame *f;
19387 struct window *w = updated_window;
19388 int max_x, min_y, max_y;
19389 int from_x, from_y, to_y;
19390
19391 xassert (updated_window && updated_row);
19392 f = XFRAME (w->frame);
19393
19394 if (updated_row->full_width_p)
19395 max_x = WINDOW_TOTAL_WIDTH (w);
19396 else
19397 max_x = window_box_width (w, updated_area);
19398 max_y = window_text_bottom_y (w);
19399
19400 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19401 of window. For TO_X > 0, truncate to end of drawing area. */
19402 if (to_x == 0)
19403 return;
19404 else if (to_x < 0)
19405 to_x = max_x;
19406 else
19407 to_x = min (to_x, max_x);
19408
19409 to_y = min (max_y, output_cursor.y + updated_row->height);
19410
19411 /* Notice if the cursor will be cleared by this operation. */
19412 if (!updated_row->full_width_p)
19413 notice_overwritten_cursor (w, updated_area,
19414 output_cursor.x, -1,
19415 updated_row->y,
19416 MATRIX_ROW_BOTTOM_Y (updated_row));
19417
19418 from_x = output_cursor.x;
19419
19420 /* Translate to frame coordinates. */
19421 if (updated_row->full_width_p)
19422 {
19423 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19424 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19425 }
19426 else
19427 {
19428 int area_left = window_box_left (w, updated_area);
19429 from_x += area_left;
19430 to_x += area_left;
19431 }
19432
19433 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19434 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19435 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19436
19437 /* Prevent inadvertently clearing to end of the X window. */
19438 if (to_x > from_x && to_y > from_y)
19439 {
19440 BLOCK_INPUT;
19441 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19442 to_x - from_x, to_y - from_y);
19443 UNBLOCK_INPUT;
19444 }
19445 }
19446
19447 #endif /* HAVE_WINDOW_SYSTEM */
19448
19449
19450 \f
19451 /***********************************************************************
19452 Cursor types
19453 ***********************************************************************/
19454
19455 /* Value is the internal representation of the specified cursor type
19456 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19457 of the bar cursor. */
19458
19459 static enum text_cursor_kinds
19460 get_specified_cursor_type (arg, width)
19461 Lisp_Object arg;
19462 int *width;
19463 {
19464 enum text_cursor_kinds type;
19465
19466 if (NILP (arg))
19467 return NO_CURSOR;
19468
19469 if (EQ (arg, Qbox))
19470 return FILLED_BOX_CURSOR;
19471
19472 if (EQ (arg, Qhollow))
19473 return HOLLOW_BOX_CURSOR;
19474
19475 if (EQ (arg, Qbar))
19476 {
19477 *width = 2;
19478 return BAR_CURSOR;
19479 }
19480
19481 if (CONSP (arg)
19482 && EQ (XCAR (arg), Qbar)
19483 && INTEGERP (XCDR (arg))
19484 && XINT (XCDR (arg)) >= 0)
19485 {
19486 *width = XINT (XCDR (arg));
19487 return BAR_CURSOR;
19488 }
19489
19490 if (EQ (arg, Qhbar))
19491 {
19492 *width = 2;
19493 return HBAR_CURSOR;
19494 }
19495
19496 if (CONSP (arg)
19497 && EQ (XCAR (arg), Qhbar)
19498 && INTEGERP (XCDR (arg))
19499 && XINT (XCDR (arg)) >= 0)
19500 {
19501 *width = XINT (XCDR (arg));
19502 return HBAR_CURSOR;
19503 }
19504
19505 /* Treat anything unknown as "hollow box cursor".
19506 It was bad to signal an error; people have trouble fixing
19507 .Xdefaults with Emacs, when it has something bad in it. */
19508 type = HOLLOW_BOX_CURSOR;
19509
19510 return type;
19511 }
19512
19513 /* Set the default cursor types for specified frame. */
19514 void
19515 set_frame_cursor_types (f, arg)
19516 struct frame *f;
19517 Lisp_Object arg;
19518 {
19519 int width;
19520 Lisp_Object tem;
19521
19522 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19523 FRAME_CURSOR_WIDTH (f) = width;
19524
19525 /* By default, set up the blink-off state depending on the on-state. */
19526
19527 tem = Fassoc (arg, Vblink_cursor_alist);
19528 if (!NILP (tem))
19529 {
19530 FRAME_BLINK_OFF_CURSOR (f)
19531 = get_specified_cursor_type (XCDR (tem), &width);
19532 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19533 }
19534 else
19535 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19536 }
19537
19538
19539 /* Return the cursor we want to be displayed in window W. Return
19540 width of bar/hbar cursor through WIDTH arg. Return with
19541 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19542 (i.e. if the `system caret' should track this cursor).
19543
19544 In a mini-buffer window, we want the cursor only to appear if we
19545 are reading input from this window. For the selected window, we
19546 want the cursor type given by the frame parameter or buffer local
19547 setting of cursor-type. If explicitly marked off, draw no cursor.
19548 In all other cases, we want a hollow box cursor. */
19549
19550 static enum text_cursor_kinds
19551 get_window_cursor_type (w, glyph, width, active_cursor)
19552 struct window *w;
19553 struct glyph *glyph;
19554 int *width;
19555 int *active_cursor;
19556 {
19557 struct frame *f = XFRAME (w->frame);
19558 struct buffer *b = XBUFFER (w->buffer);
19559 int cursor_type = DEFAULT_CURSOR;
19560 Lisp_Object alt_cursor;
19561 int non_selected = 0;
19562
19563 *active_cursor = 1;
19564
19565 /* Echo area */
19566 if (cursor_in_echo_area
19567 && FRAME_HAS_MINIBUF_P (f)
19568 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19569 {
19570 if (w == XWINDOW (echo_area_window))
19571 {
19572 *width = FRAME_CURSOR_WIDTH (f);
19573 return FRAME_DESIRED_CURSOR (f);
19574 }
19575
19576 *active_cursor = 0;
19577 non_selected = 1;
19578 }
19579
19580 /* Nonselected window or nonselected frame. */
19581 else if (w != XWINDOW (f->selected_window)
19582 #ifdef HAVE_WINDOW_SYSTEM
19583 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19584 #endif
19585 )
19586 {
19587 *active_cursor = 0;
19588
19589 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19590 return NO_CURSOR;
19591
19592 non_selected = 1;
19593 }
19594
19595 /* Never display a cursor in a window in which cursor-type is nil. */
19596 if (NILP (b->cursor_type))
19597 return NO_CURSOR;
19598
19599 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19600 if (non_selected)
19601 {
19602 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19603 return get_specified_cursor_type (alt_cursor, width);
19604 }
19605
19606 /* Get the normal cursor type for this window. */
19607 if (EQ (b->cursor_type, Qt))
19608 {
19609 cursor_type = FRAME_DESIRED_CURSOR (f);
19610 *width = FRAME_CURSOR_WIDTH (f);
19611 }
19612 else
19613 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19614
19615 /* Use normal cursor if not blinked off. */
19616 if (!w->cursor_off_p)
19617 {
19618 if (glyph->type == IMAGE_GLYPH) {
19619 if (cursor_type == FILLED_BOX_CURSOR)
19620 cursor_type = HOLLOW_BOX_CURSOR;
19621 }
19622 return cursor_type;
19623 }
19624
19625 /* Cursor is blinked off, so determine how to "toggle" it. */
19626
19627 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19628 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19629 return get_specified_cursor_type (XCDR (alt_cursor), width);
19630
19631 /* Then see if frame has specified a specific blink off cursor type. */
19632 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19633 {
19634 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19635 return FRAME_BLINK_OFF_CURSOR (f);
19636 }
19637
19638 #if 0
19639 /* Some people liked having a permanently visible blinking cursor,
19640 while others had very strong opinions against it. So it was
19641 decided to remove it. KFS 2003-09-03 */
19642
19643 /* Finally perform built-in cursor blinking:
19644 filled box <-> hollow box
19645 wide [h]bar <-> narrow [h]bar
19646 narrow [h]bar <-> no cursor
19647 other type <-> no cursor */
19648
19649 if (cursor_type == FILLED_BOX_CURSOR)
19650 return HOLLOW_BOX_CURSOR;
19651
19652 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19653 {
19654 *width = 1;
19655 return cursor_type;
19656 }
19657 #endif
19658
19659 return NO_CURSOR;
19660 }
19661
19662
19663 #ifdef HAVE_WINDOW_SYSTEM
19664
19665 /* Notice when the text cursor of window W has been completely
19666 overwritten by a drawing operation that outputs glyphs in AREA
19667 starting at X0 and ending at X1 in the line starting at Y0 and
19668 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19669 the rest of the line after X0 has been written. Y coordinates
19670 are window-relative. */
19671
19672 static void
19673 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19674 struct window *w;
19675 enum glyph_row_area area;
19676 int x0, y0, x1, y1;
19677 {
19678 int cx0, cx1, cy0, cy1;
19679 struct glyph_row *row;
19680
19681 if (!w->phys_cursor_on_p)
19682 return;
19683 if (area != TEXT_AREA)
19684 return;
19685
19686 row = w->current_matrix->rows + w->phys_cursor.vpos;
19687 if (!row->displays_text_p)
19688 return;
19689
19690 if (row->cursor_in_fringe_p)
19691 {
19692 row->cursor_in_fringe_p = 0;
19693 draw_fringe_bitmap (w, row, 0);
19694 w->phys_cursor_on_p = 0;
19695 return;
19696 }
19697
19698 cx0 = w->phys_cursor.x;
19699 cx1 = cx0 + w->phys_cursor_width;
19700 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19701 return;
19702
19703 /* The cursor image will be completely removed from the
19704 screen if the output area intersects the cursor area in
19705 y-direction. When we draw in [y0 y1[, and some part of
19706 the cursor is at y < y0, that part must have been drawn
19707 before. When scrolling, the cursor is erased before
19708 actually scrolling, so we don't come here. When not
19709 scrolling, the rows above the old cursor row must have
19710 changed, and in this case these rows must have written
19711 over the cursor image.
19712
19713 Likewise if part of the cursor is below y1, with the
19714 exception of the cursor being in the first blank row at
19715 the buffer and window end because update_text_area
19716 doesn't draw that row. (Except when it does, but
19717 that's handled in update_text_area.) */
19718
19719 cy0 = w->phys_cursor.y;
19720 cy1 = cy0 + w->phys_cursor_height;
19721 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19722 return;
19723
19724 w->phys_cursor_on_p = 0;
19725 }
19726
19727 #endif /* HAVE_WINDOW_SYSTEM */
19728
19729 \f
19730 /************************************************************************
19731 Mouse Face
19732 ************************************************************************/
19733
19734 #ifdef HAVE_WINDOW_SYSTEM
19735
19736 /* EXPORT for RIF:
19737 Fix the display of area AREA of overlapping row ROW in window W. */
19738
19739 void
19740 x_fix_overlapping_area (w, row, area)
19741 struct window *w;
19742 struct glyph_row *row;
19743 enum glyph_row_area area;
19744 {
19745 int i, x;
19746
19747 BLOCK_INPUT;
19748
19749 x = 0;
19750 for (i = 0; i < row->used[area];)
19751 {
19752 if (row->glyphs[area][i].overlaps_vertically_p)
19753 {
19754 int start = i, start_x = x;
19755
19756 do
19757 {
19758 x += row->glyphs[area][i].pixel_width;
19759 ++i;
19760 }
19761 while (i < row->used[area]
19762 && row->glyphs[area][i].overlaps_vertically_p);
19763
19764 draw_glyphs (w, start_x, row, area,
19765 start, i,
19766 DRAW_NORMAL_TEXT, 1);
19767 }
19768 else
19769 {
19770 x += row->glyphs[area][i].pixel_width;
19771 ++i;
19772 }
19773 }
19774
19775 UNBLOCK_INPUT;
19776 }
19777
19778
19779 /* EXPORT:
19780 Draw the cursor glyph of window W in glyph row ROW. See the
19781 comment of draw_glyphs for the meaning of HL. */
19782
19783 void
19784 draw_phys_cursor_glyph (w, row, hl)
19785 struct window *w;
19786 struct glyph_row *row;
19787 enum draw_glyphs_face hl;
19788 {
19789 /* If cursor hpos is out of bounds, don't draw garbage. This can
19790 happen in mini-buffer windows when switching between echo area
19791 glyphs and mini-buffer. */
19792 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19793 {
19794 int on_p = w->phys_cursor_on_p;
19795 int x1;
19796 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19797 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19798 hl, 0);
19799 w->phys_cursor_on_p = on_p;
19800
19801 if (hl == DRAW_CURSOR)
19802 w->phys_cursor_width = x1 - w->phys_cursor.x;
19803 /* When we erase the cursor, and ROW is overlapped by other
19804 rows, make sure that these overlapping parts of other rows
19805 are redrawn. */
19806 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19807 {
19808 if (row > w->current_matrix->rows
19809 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19810 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19811
19812 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19813 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19814 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19815 }
19816 }
19817 }
19818
19819
19820 /* EXPORT:
19821 Erase the image of a cursor of window W from the screen. */
19822
19823 void
19824 erase_phys_cursor (w)
19825 struct window *w;
19826 {
19827 struct frame *f = XFRAME (w->frame);
19828 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19829 int hpos = w->phys_cursor.hpos;
19830 int vpos = w->phys_cursor.vpos;
19831 int mouse_face_here_p = 0;
19832 struct glyph_matrix *active_glyphs = w->current_matrix;
19833 struct glyph_row *cursor_row;
19834 struct glyph *cursor_glyph;
19835 enum draw_glyphs_face hl;
19836
19837 /* No cursor displayed or row invalidated => nothing to do on the
19838 screen. */
19839 if (w->phys_cursor_type == NO_CURSOR)
19840 goto mark_cursor_off;
19841
19842 /* VPOS >= active_glyphs->nrows means that window has been resized.
19843 Don't bother to erase the cursor. */
19844 if (vpos >= active_glyphs->nrows)
19845 goto mark_cursor_off;
19846
19847 /* If row containing cursor is marked invalid, there is nothing we
19848 can do. */
19849 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19850 if (!cursor_row->enabled_p)
19851 goto mark_cursor_off;
19852
19853 /* If row is completely invisible, don't attempt to delete a cursor which
19854 isn't there. This can happen if cursor is at top of a window, and
19855 we switch to a buffer with a header line in that window. */
19856 if (cursor_row->visible_height <= 0)
19857 goto mark_cursor_off;
19858
19859 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19860 if (cursor_row->cursor_in_fringe_p)
19861 {
19862 cursor_row->cursor_in_fringe_p = 0;
19863 draw_fringe_bitmap (w, cursor_row, 0);
19864 goto mark_cursor_off;
19865 }
19866
19867 /* This can happen when the new row is shorter than the old one.
19868 In this case, either draw_glyphs or clear_end_of_line
19869 should have cleared the cursor. Note that we wouldn't be
19870 able to erase the cursor in this case because we don't have a
19871 cursor glyph at hand. */
19872 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19873 goto mark_cursor_off;
19874
19875 /* If the cursor is in the mouse face area, redisplay that when
19876 we clear the cursor. */
19877 if (! NILP (dpyinfo->mouse_face_window)
19878 && w == XWINDOW (dpyinfo->mouse_face_window)
19879 && (vpos > dpyinfo->mouse_face_beg_row
19880 || (vpos == dpyinfo->mouse_face_beg_row
19881 && hpos >= dpyinfo->mouse_face_beg_col))
19882 && (vpos < dpyinfo->mouse_face_end_row
19883 || (vpos == dpyinfo->mouse_face_end_row
19884 && hpos < dpyinfo->mouse_face_end_col))
19885 /* Don't redraw the cursor's spot in mouse face if it is at the
19886 end of a line (on a newline). The cursor appears there, but
19887 mouse highlighting does not. */
19888 && cursor_row->used[TEXT_AREA] > hpos)
19889 mouse_face_here_p = 1;
19890
19891 /* Maybe clear the display under the cursor. */
19892 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19893 {
19894 int x, y;
19895 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19896
19897 cursor_glyph = get_phys_cursor_glyph (w);
19898 if (cursor_glyph == NULL)
19899 goto mark_cursor_off;
19900
19901 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19902 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19903
19904 FRAME_RIF (f)->clear_frame_area (f, x, y,
19905 cursor_glyph->pixel_width, cursor_row->visible_height);
19906 }
19907
19908 /* Erase the cursor by redrawing the character underneath it. */
19909 if (mouse_face_here_p)
19910 hl = DRAW_MOUSE_FACE;
19911 else
19912 hl = DRAW_NORMAL_TEXT;
19913 draw_phys_cursor_glyph (w, cursor_row, hl);
19914
19915 mark_cursor_off:
19916 w->phys_cursor_on_p = 0;
19917 w->phys_cursor_type = NO_CURSOR;
19918 }
19919
19920
19921 /* EXPORT:
19922 Display or clear cursor of window W. If ON is zero, clear the
19923 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19924 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19925
19926 void
19927 display_and_set_cursor (w, on, hpos, vpos, x, y)
19928 struct window *w;
19929 int on, hpos, vpos, x, y;
19930 {
19931 struct frame *f = XFRAME (w->frame);
19932 int new_cursor_type;
19933 int new_cursor_width;
19934 int active_cursor;
19935 struct glyph_matrix *current_glyphs;
19936 struct glyph_row *glyph_row;
19937 struct glyph *glyph;
19938
19939 /* This is pointless on invisible frames, and dangerous on garbaged
19940 windows and frames; in the latter case, the frame or window may
19941 be in the midst of changing its size, and x and y may be off the
19942 window. */
19943 if (! FRAME_VISIBLE_P (f)
19944 || FRAME_GARBAGED_P (f)
19945 || vpos >= w->current_matrix->nrows
19946 || hpos >= w->current_matrix->matrix_w)
19947 return;
19948
19949 /* If cursor is off and we want it off, return quickly. */
19950 if (!on && !w->phys_cursor_on_p)
19951 return;
19952
19953 current_glyphs = w->current_matrix;
19954 glyph_row = MATRIX_ROW (current_glyphs, vpos);
19955 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19956
19957 /* If cursor row is not enabled, we don't really know where to
19958 display the cursor. */
19959 if (!glyph_row->enabled_p)
19960 {
19961 w->phys_cursor_on_p = 0;
19962 return;
19963 }
19964
19965 xassert (interrupt_input_blocked);
19966
19967 /* Set new_cursor_type to the cursor we want to be displayed. */
19968 new_cursor_type = get_window_cursor_type (w, glyph,
19969 &new_cursor_width, &active_cursor);
19970
19971 /* If cursor is currently being shown and we don't want it to be or
19972 it is in the wrong place, or the cursor type is not what we want,
19973 erase it. */
19974 if (w->phys_cursor_on_p
19975 && (!on
19976 || w->phys_cursor.x != x
19977 || w->phys_cursor.y != y
19978 || new_cursor_type != w->phys_cursor_type
19979 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19980 && new_cursor_width != w->phys_cursor_width)))
19981 erase_phys_cursor (w);
19982
19983 /* Don't check phys_cursor_on_p here because that flag is only set
19984 to zero in some cases where we know that the cursor has been
19985 completely erased, to avoid the extra work of erasing the cursor
19986 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19987 still not be visible, or it has only been partly erased. */
19988 if (on)
19989 {
19990 w->phys_cursor_ascent = glyph_row->ascent;
19991 w->phys_cursor_height = glyph_row->height;
19992
19993 /* Set phys_cursor_.* before x_draw_.* is called because some
19994 of them may need the information. */
19995 w->phys_cursor.x = x;
19996 w->phys_cursor.y = glyph_row->y;
19997 w->phys_cursor.hpos = hpos;
19998 w->phys_cursor.vpos = vpos;
19999 }
20000
20001 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
20002 new_cursor_type, new_cursor_width,
20003 on, active_cursor);
20004 }
20005
20006
20007 /* Switch the display of W's cursor on or off, according to the value
20008 of ON. */
20009
20010 static void
20011 update_window_cursor (w, on)
20012 struct window *w;
20013 int on;
20014 {
20015 /* Don't update cursor in windows whose frame is in the process
20016 of being deleted. */
20017 if (w->current_matrix)
20018 {
20019 BLOCK_INPUT;
20020 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20021 w->phys_cursor.x, w->phys_cursor.y);
20022 UNBLOCK_INPUT;
20023 }
20024 }
20025
20026
20027 /* Call update_window_cursor with parameter ON_P on all leaf windows
20028 in the window tree rooted at W. */
20029
20030 static void
20031 update_cursor_in_window_tree (w, on_p)
20032 struct window *w;
20033 int on_p;
20034 {
20035 while (w)
20036 {
20037 if (!NILP (w->hchild))
20038 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20039 else if (!NILP (w->vchild))
20040 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20041 else
20042 update_window_cursor (w, on_p);
20043
20044 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20045 }
20046 }
20047
20048
20049 /* EXPORT:
20050 Display the cursor on window W, or clear it, according to ON_P.
20051 Don't change the cursor's position. */
20052
20053 void
20054 x_update_cursor (f, on_p)
20055 struct frame *f;
20056 int on_p;
20057 {
20058 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20059 }
20060
20061
20062 /* EXPORT:
20063 Clear the cursor of window W to background color, and mark the
20064 cursor as not shown. This is used when the text where the cursor
20065 is is about to be rewritten. */
20066
20067 void
20068 x_clear_cursor (w)
20069 struct window *w;
20070 {
20071 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20072 update_window_cursor (w, 0);
20073 }
20074
20075
20076 /* EXPORT:
20077 Display the active region described by mouse_face_* according to DRAW. */
20078
20079 void
20080 show_mouse_face (dpyinfo, draw)
20081 Display_Info *dpyinfo;
20082 enum draw_glyphs_face draw;
20083 {
20084 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20085 struct frame *f = XFRAME (WINDOW_FRAME (w));
20086
20087 if (/* If window is in the process of being destroyed, don't bother
20088 to do anything. */
20089 w->current_matrix != NULL
20090 /* Don't update mouse highlight if hidden */
20091 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20092 /* Recognize when we are called to operate on rows that don't exist
20093 anymore. This can happen when a window is split. */
20094 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20095 {
20096 int phys_cursor_on_p = w->phys_cursor_on_p;
20097 struct glyph_row *row, *first, *last;
20098
20099 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20100 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20101
20102 for (row = first; row <= last && row->enabled_p; ++row)
20103 {
20104 int start_hpos, end_hpos, start_x;
20105
20106 /* For all but the first row, the highlight starts at column 0. */
20107 if (row == first)
20108 {
20109 start_hpos = dpyinfo->mouse_face_beg_col;
20110 start_x = dpyinfo->mouse_face_beg_x;
20111 }
20112 else
20113 {
20114 start_hpos = 0;
20115 start_x = 0;
20116 }
20117
20118 if (row == last)
20119 end_hpos = dpyinfo->mouse_face_end_col;
20120 else
20121 end_hpos = row->used[TEXT_AREA];
20122
20123 if (end_hpos > start_hpos)
20124 {
20125 draw_glyphs (w, start_x, row, TEXT_AREA,
20126 start_hpos, end_hpos,
20127 draw, 0);
20128
20129 row->mouse_face_p
20130 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20131 }
20132 }
20133
20134 /* When we've written over the cursor, arrange for it to
20135 be displayed again. */
20136 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20137 {
20138 BLOCK_INPUT;
20139 display_and_set_cursor (w, 1,
20140 w->phys_cursor.hpos, w->phys_cursor.vpos,
20141 w->phys_cursor.x, w->phys_cursor.y);
20142 UNBLOCK_INPUT;
20143 }
20144 }
20145
20146 /* Change the mouse cursor. */
20147 if (draw == DRAW_NORMAL_TEXT)
20148 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20149 else if (draw == DRAW_MOUSE_FACE)
20150 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20151 else
20152 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20153 }
20154
20155 /* EXPORT:
20156 Clear out the mouse-highlighted active region.
20157 Redraw it un-highlighted first. Value is non-zero if mouse
20158 face was actually drawn unhighlighted. */
20159
20160 int
20161 clear_mouse_face (dpyinfo)
20162 Display_Info *dpyinfo;
20163 {
20164 int cleared = 0;
20165
20166 if (!NILP (dpyinfo->mouse_face_window))
20167 {
20168 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20169 cleared = 1;
20170 }
20171
20172 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20173 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20174 dpyinfo->mouse_face_window = Qnil;
20175 dpyinfo->mouse_face_overlay = Qnil;
20176 return cleared;
20177 }
20178
20179
20180 /* EXPORT:
20181 Non-zero if physical cursor of window W is within mouse face. */
20182
20183 int
20184 cursor_in_mouse_face_p (w)
20185 struct window *w;
20186 {
20187 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20188 int in_mouse_face = 0;
20189
20190 if (WINDOWP (dpyinfo->mouse_face_window)
20191 && XWINDOW (dpyinfo->mouse_face_window) == w)
20192 {
20193 int hpos = w->phys_cursor.hpos;
20194 int vpos = w->phys_cursor.vpos;
20195
20196 if (vpos >= dpyinfo->mouse_face_beg_row
20197 && vpos <= dpyinfo->mouse_face_end_row
20198 && (vpos > dpyinfo->mouse_face_beg_row
20199 || hpos >= dpyinfo->mouse_face_beg_col)
20200 && (vpos < dpyinfo->mouse_face_end_row
20201 || hpos < dpyinfo->mouse_face_end_col
20202 || dpyinfo->mouse_face_past_end))
20203 in_mouse_face = 1;
20204 }
20205
20206 return in_mouse_face;
20207 }
20208
20209
20210
20211 \f
20212 /* Find the glyph matrix position of buffer position CHARPOS in window
20213 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20214 current glyphs must be up to date. If CHARPOS is above window
20215 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20216 of last line in W. In the row containing CHARPOS, stop before glyphs
20217 having STOP as object. */
20218
20219 #if 1 /* This is a version of fast_find_position that's more correct
20220 in the presence of hscrolling, for example. I didn't install
20221 it right away because the problem fixed is minor, it failed
20222 in 20.x as well, and I think it's too risky to install
20223 so near the release of 21.1. 2001-09-25 gerd. */
20224
20225 static int
20226 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20227 struct window *w;
20228 int charpos;
20229 int *hpos, *vpos, *x, *y;
20230 Lisp_Object stop;
20231 {
20232 struct glyph_row *row, *first;
20233 struct glyph *glyph, *end;
20234 int past_end = 0;
20235
20236 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20237 row = row_containing_pos (w, charpos, first, NULL, 0);
20238 if (row == NULL)
20239 {
20240 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20241 {
20242 *x = *y = *hpos = *vpos = 0;
20243 return 0;
20244 }
20245 else
20246 {
20247 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20248 past_end = 1;
20249 }
20250 }
20251
20252 *x = row->x;
20253 *y = row->y;
20254 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20255
20256 glyph = row->glyphs[TEXT_AREA];
20257 end = glyph + row->used[TEXT_AREA];
20258
20259 /* Skip over glyphs not having an object at the start of the row.
20260 These are special glyphs like truncation marks on terminal
20261 frames. */
20262 if (row->displays_text_p)
20263 while (glyph < end
20264 && INTEGERP (glyph->object)
20265 && !EQ (stop, glyph->object)
20266 && glyph->charpos < 0)
20267 {
20268 *x += glyph->pixel_width;
20269 ++glyph;
20270 }
20271
20272 while (glyph < end
20273 && !INTEGERP (glyph->object)
20274 && !EQ (stop, glyph->object)
20275 && (!BUFFERP (glyph->object)
20276 || glyph->charpos < charpos))
20277 {
20278 *x += glyph->pixel_width;
20279 ++glyph;
20280 }
20281
20282 *hpos = glyph - row->glyphs[TEXT_AREA];
20283 return past_end;
20284 }
20285
20286 #else /* not 1 */
20287
20288 static int
20289 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20290 struct window *w;
20291 int pos;
20292 int *hpos, *vpos, *x, *y;
20293 Lisp_Object stop;
20294 {
20295 int i;
20296 int lastcol;
20297 int maybe_next_line_p = 0;
20298 int line_start_position;
20299 int yb = window_text_bottom_y (w);
20300 struct glyph_row *row, *best_row;
20301 int row_vpos, best_row_vpos;
20302 int current_x;
20303
20304 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20305 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20306
20307 while (row->y < yb)
20308 {
20309 if (row->used[TEXT_AREA])
20310 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20311 else
20312 line_start_position = 0;
20313
20314 if (line_start_position > pos)
20315 break;
20316 /* If the position sought is the end of the buffer,
20317 don't include the blank lines at the bottom of the window. */
20318 else if (line_start_position == pos
20319 && pos == BUF_ZV (XBUFFER (w->buffer)))
20320 {
20321 maybe_next_line_p = 1;
20322 break;
20323 }
20324 else if (line_start_position > 0)
20325 {
20326 best_row = row;
20327 best_row_vpos = row_vpos;
20328 }
20329
20330 if (row->y + row->height >= yb)
20331 break;
20332
20333 ++row;
20334 ++row_vpos;
20335 }
20336
20337 /* Find the right column within BEST_ROW. */
20338 lastcol = 0;
20339 current_x = best_row->x;
20340 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20341 {
20342 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20343 int charpos = glyph->charpos;
20344
20345 if (BUFFERP (glyph->object))
20346 {
20347 if (charpos == pos)
20348 {
20349 *hpos = i;
20350 *vpos = best_row_vpos;
20351 *x = current_x;
20352 *y = best_row->y;
20353 return 1;
20354 }
20355 else if (charpos > pos)
20356 break;
20357 }
20358 else if (EQ (glyph->object, stop))
20359 break;
20360
20361 if (charpos > 0)
20362 lastcol = i;
20363 current_x += glyph->pixel_width;
20364 }
20365
20366 /* If we're looking for the end of the buffer,
20367 and we didn't find it in the line we scanned,
20368 use the start of the following line. */
20369 if (maybe_next_line_p)
20370 {
20371 ++best_row;
20372 ++best_row_vpos;
20373 lastcol = 0;
20374 current_x = best_row->x;
20375 }
20376
20377 *vpos = best_row_vpos;
20378 *hpos = lastcol + 1;
20379 *x = current_x;
20380 *y = best_row->y;
20381 return 0;
20382 }
20383
20384 #endif /* not 1 */
20385
20386
20387 /* Find the position of the glyph for position POS in OBJECT in
20388 window W's current matrix, and return in *X, *Y the pixel
20389 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20390
20391 RIGHT_P non-zero means return the position of the right edge of the
20392 glyph, RIGHT_P zero means return the left edge position.
20393
20394 If no glyph for POS exists in the matrix, return the position of
20395 the glyph with the next smaller position that is in the matrix, if
20396 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20397 exists in the matrix, return the position of the glyph with the
20398 next larger position in OBJECT.
20399
20400 Value is non-zero if a glyph was found. */
20401
20402 static int
20403 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20404 struct window *w;
20405 int pos;
20406 Lisp_Object object;
20407 int *hpos, *vpos, *x, *y;
20408 int right_p;
20409 {
20410 int yb = window_text_bottom_y (w);
20411 struct glyph_row *r;
20412 struct glyph *best_glyph = NULL;
20413 struct glyph_row *best_row = NULL;
20414 int best_x = 0;
20415
20416 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20417 r->enabled_p && r->y < yb;
20418 ++r)
20419 {
20420 struct glyph *g = r->glyphs[TEXT_AREA];
20421 struct glyph *e = g + r->used[TEXT_AREA];
20422 int gx;
20423
20424 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20425 if (EQ (g->object, object))
20426 {
20427 if (g->charpos == pos)
20428 {
20429 best_glyph = g;
20430 best_x = gx;
20431 best_row = r;
20432 goto found;
20433 }
20434 else if (best_glyph == NULL
20435 || ((abs (g->charpos - pos)
20436 < abs (best_glyph->charpos - pos))
20437 && (right_p
20438 ? g->charpos < pos
20439 : g->charpos > pos)))
20440 {
20441 best_glyph = g;
20442 best_x = gx;
20443 best_row = r;
20444 }
20445 }
20446 }
20447
20448 found:
20449
20450 if (best_glyph)
20451 {
20452 *x = best_x;
20453 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20454
20455 if (right_p)
20456 {
20457 *x += best_glyph->pixel_width;
20458 ++*hpos;
20459 }
20460
20461 *y = best_row->y;
20462 *vpos = best_row - w->current_matrix->rows;
20463 }
20464
20465 return best_glyph != NULL;
20466 }
20467
20468
20469 /* See if position X, Y is within a hot-spot of an image. */
20470
20471 static int
20472 on_hot_spot_p (hot_spot, x, y)
20473 Lisp_Object hot_spot;
20474 int x, y;
20475 {
20476 if (!CONSP (hot_spot))
20477 return 0;
20478
20479 if (EQ (XCAR (hot_spot), Qrect))
20480 {
20481 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20482 Lisp_Object rect = XCDR (hot_spot);
20483 Lisp_Object tem;
20484 if (!CONSP (rect))
20485 return 0;
20486 if (!CONSP (XCAR (rect)))
20487 return 0;
20488 if (!CONSP (XCDR (rect)))
20489 return 0;
20490 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20491 return 0;
20492 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20493 return 0;
20494 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20495 return 0;
20496 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20497 return 0;
20498 return 1;
20499 }
20500 else if (EQ (XCAR (hot_spot), Qcircle))
20501 {
20502 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20503 Lisp_Object circ = XCDR (hot_spot);
20504 Lisp_Object lr, lx0, ly0;
20505 if (CONSP (circ)
20506 && CONSP (XCAR (circ))
20507 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20508 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20509 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20510 {
20511 double r = XFLOATINT (lr);
20512 double dx = XINT (lx0) - x;
20513 double dy = XINT (ly0) - y;
20514 return (dx * dx + dy * dy <= r * r);
20515 }
20516 }
20517 else if (EQ (XCAR (hot_spot), Qpoly))
20518 {
20519 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20520 if (VECTORP (XCDR (hot_spot)))
20521 {
20522 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20523 Lisp_Object *poly = v->contents;
20524 int n = v->size;
20525 int i;
20526 int inside = 0;
20527 Lisp_Object lx, ly;
20528 int x0, y0;
20529
20530 /* Need an even number of coordinates, and at least 3 edges. */
20531 if (n < 6 || n & 1)
20532 return 0;
20533
20534 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20535 If count is odd, we are inside polygon. Pixels on edges
20536 may or may not be included depending on actual geometry of the
20537 polygon. */
20538 if ((lx = poly[n-2], !INTEGERP (lx))
20539 || (ly = poly[n-1], !INTEGERP (lx)))
20540 return 0;
20541 x0 = XINT (lx), y0 = XINT (ly);
20542 for (i = 0; i < n; i += 2)
20543 {
20544 int x1 = x0, y1 = y0;
20545 if ((lx = poly[i], !INTEGERP (lx))
20546 || (ly = poly[i+1], !INTEGERP (ly)))
20547 return 0;
20548 x0 = XINT (lx), y0 = XINT (ly);
20549
20550 /* Does this segment cross the X line? */
20551 if (x0 >= x)
20552 {
20553 if (x1 >= x)
20554 continue;
20555 }
20556 else if (x1 < x)
20557 continue;
20558 if (y > y0 && y > y1)
20559 continue;
20560 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20561 inside = !inside;
20562 }
20563 return inside;
20564 }
20565 }
20566 else
20567 return 0;
20568 }
20569
20570 Lisp_Object
20571 find_hot_spot (map, x, y)
20572 Lisp_Object map;
20573 int x, y;
20574 {
20575 while (CONSP (map))
20576 {
20577 if (CONSP (XCAR (map))
20578 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20579 return XCAR (map);
20580 map = XCDR (map);
20581 }
20582
20583 return Qnil;
20584 }
20585
20586 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20587 3, 3, 0,
20588 doc: /* Lookup in image map MAP coordinates X and Y.
20589 An image map is an alist where each element has the format (AREA ID PLIST).
20590 An AREA is specified as either a rectangle, a circle, or a polygon:
20591 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20592 pixel coordinates of the upper left and bottom right corners.
20593 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20594 and the radius of the circle; r may be a float or integer.
20595 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20596 vector describes one corner in the polygon.
20597 Returns the alist element for the first matching AREA in MAP. */)
20598 (map, x, y)
20599 Lisp_Object map;
20600 Lisp_Object x, y;
20601 {
20602 int ix, iy;
20603 if (NILP (map))
20604 return Qnil;
20605
20606 if (!INTEGERP (x))
20607 wrong_type_argument (Qintegerp, x);
20608 if (!INTEGERP (y))
20609 wrong_type_argument (Qintegerp, y);
20610
20611 return find_hot_spot (map, XINT (x), XINT (y));
20612 }
20613
20614
20615 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20616 static void
20617 define_frame_cursor1 (f, cursor, pointer)
20618 struct frame *f;
20619 Cursor cursor;
20620 Lisp_Object pointer;
20621 {
20622 if (!NILP (pointer))
20623 {
20624 if (EQ (pointer, Qarrow))
20625 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20626 else if (EQ (pointer, Qhand))
20627 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20628 else if (EQ (pointer, Qtext))
20629 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20630 else if (EQ (pointer, intern ("hdrag")))
20631 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20632 #ifdef HAVE_X_WINDOWS
20633 else if (EQ (pointer, intern ("vdrag")))
20634 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20635 #endif
20636 else if (EQ (pointer, intern ("hourglass")))
20637 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20638 else if (EQ (pointer, Qmodeline))
20639 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20640 else
20641 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20642 }
20643
20644 #ifndef HAVE_CARBON
20645 if (cursor != No_Cursor)
20646 #else
20647 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20648 #endif
20649 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20650 }
20651
20652 /* Take proper action when mouse has moved to the mode or header line
20653 or marginal area AREA of window W, x-position X and y-position Y.
20654 X is relative to the start of the text display area of W, so the
20655 width of bitmap areas and scroll bars must be subtracted to get a
20656 position relative to the start of the mode line. */
20657
20658 static void
20659 note_mode_line_or_margin_highlight (w, x, y, area)
20660 struct window *w;
20661 int x, y;
20662 enum window_part area;
20663 {
20664 struct frame *f = XFRAME (w->frame);
20665 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20666 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20667 Lisp_Object pointer = Qnil;
20668 int charpos, dx, dy, width, height;
20669 Lisp_Object string, object = Qnil;
20670 Lisp_Object pos, help, image;
20671
20672 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20673 string = mode_line_string (w, area, &x, &y, &charpos,
20674 &object, &dx, &dy, &width, &height);
20675 else
20676 {
20677 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20678 string = marginal_area_string (w, area, &x, &y, &charpos,
20679 &object, &dx, &dy, &width, &height);
20680 }
20681
20682 help = Qnil;
20683
20684 if (IMAGEP (object))
20685 {
20686 Lisp_Object image_map, hotspot;
20687 if ((image_map = Fplist_get (XCDR (object), QCmap),
20688 !NILP (image_map))
20689 && (hotspot = find_hot_spot (image_map, dx, dy),
20690 CONSP (hotspot))
20691 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20692 {
20693 Lisp_Object area_id, plist;
20694
20695 area_id = XCAR (hotspot);
20696 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20697 If so, we could look for mouse-enter, mouse-leave
20698 properties in PLIST (and do something...). */
20699 if ((plist = XCDR (hotspot), CONSP (plist)))
20700 {
20701 pointer = Fplist_get (plist, Qpointer);
20702 if (NILP (pointer))
20703 pointer = Qhand;
20704 help = Fplist_get (plist, Qhelp_echo);
20705 if (!NILP (help))
20706 {
20707 help_echo_string = help;
20708 /* Is this correct? ++kfs */
20709 XSETWINDOW (help_echo_window, w);
20710 help_echo_object = w->buffer;
20711 help_echo_pos = charpos;
20712 }
20713 }
20714 if (NILP (pointer))
20715 pointer = Fplist_get (XCDR (object), QCpointer);
20716 }
20717 }
20718
20719 if (STRINGP (string))
20720 {
20721 pos = make_number (charpos);
20722 /* If we're on a string with `help-echo' text property, arrange
20723 for the help to be displayed. This is done by setting the
20724 global variable help_echo_string to the help string. */
20725 help = Fget_text_property (pos, Qhelp_echo, string);
20726 if (!NILP (help))
20727 {
20728 help_echo_string = help;
20729 XSETWINDOW (help_echo_window, w);
20730 help_echo_object = string;
20731 help_echo_pos = charpos;
20732 }
20733
20734 if (NILP (pointer))
20735 pointer = Fget_text_property (pos, Qpointer, string);
20736
20737 /* Change the mouse pointer according to what is under X/Y. */
20738 if (NILP (pointer) && area == ON_MODE_LINE)
20739 {
20740 Lisp_Object map;
20741 map = Fget_text_property (pos, Qlocal_map, string);
20742 if (!KEYMAPP (map))
20743 map = Fget_text_property (pos, Qkeymap, string);
20744 if (!KEYMAPP (map))
20745 cursor = dpyinfo->vertical_scroll_bar_cursor;
20746 }
20747 }
20748
20749 define_frame_cursor1 (f, cursor, pointer);
20750 }
20751
20752
20753 /* EXPORT:
20754 Take proper action when the mouse has moved to position X, Y on
20755 frame F as regards highlighting characters that have mouse-face
20756 properties. Also de-highlighting chars where the mouse was before.
20757 X and Y can be negative or out of range. */
20758
20759 void
20760 note_mouse_highlight (f, x, y)
20761 struct frame *f;
20762 int x, y;
20763 {
20764 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20765 enum window_part part;
20766 Lisp_Object window;
20767 struct window *w;
20768 Cursor cursor = No_Cursor;
20769 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20770 struct buffer *b;
20771
20772 /* When a menu is active, don't highlight because this looks odd. */
20773 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20774 if (popup_activated ())
20775 return;
20776 #endif
20777
20778 if (NILP (Vmouse_highlight)
20779 || !f->glyphs_initialized_p)
20780 return;
20781
20782 dpyinfo->mouse_face_mouse_x = x;
20783 dpyinfo->mouse_face_mouse_y = y;
20784 dpyinfo->mouse_face_mouse_frame = f;
20785
20786 if (dpyinfo->mouse_face_defer)
20787 return;
20788
20789 if (gc_in_progress)
20790 {
20791 dpyinfo->mouse_face_deferred_gc = 1;
20792 return;
20793 }
20794
20795 /* Which window is that in? */
20796 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20797
20798 /* If we were displaying active text in another window, clear that. */
20799 if (! EQ (window, dpyinfo->mouse_face_window))
20800 clear_mouse_face (dpyinfo);
20801
20802 /* Not on a window -> return. */
20803 if (!WINDOWP (window))
20804 return;
20805
20806 /* Reset help_echo_string. It will get recomputed below. */
20807 help_echo_string = Qnil;
20808
20809 /* Convert to window-relative pixel coordinates. */
20810 w = XWINDOW (window);
20811 frame_to_window_pixel_xy (w, &x, &y);
20812
20813 /* Handle tool-bar window differently since it doesn't display a
20814 buffer. */
20815 if (EQ (window, f->tool_bar_window))
20816 {
20817 note_tool_bar_highlight (f, x, y);
20818 return;
20819 }
20820
20821 /* Mouse is on the mode, header line or margin? */
20822 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20823 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20824 {
20825 note_mode_line_or_margin_highlight (w, x, y, part);
20826 return;
20827 }
20828
20829 if (part == ON_VERTICAL_BORDER)
20830 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20831 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20832 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20833 else
20834 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20835
20836 /* Are we in a window whose display is up to date?
20837 And verify the buffer's text has not changed. */
20838 b = XBUFFER (w->buffer);
20839 if (part == ON_TEXT
20840 && EQ (w->window_end_valid, w->buffer)
20841 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20842 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20843 {
20844 int hpos, vpos, pos, i, dx, dy, area;
20845 struct glyph *glyph;
20846 Lisp_Object object;
20847 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20848 Lisp_Object *overlay_vec = NULL;
20849 int len, noverlays;
20850 struct buffer *obuf;
20851 int obegv, ozv, same_region;
20852
20853 /* Find the glyph under X/Y. */
20854 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20855
20856 /* Look for :pointer property on image. */
20857 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20858 {
20859 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20860 if (img != NULL && IMAGEP (img->spec))
20861 {
20862 Lisp_Object image_map, hotspot;
20863 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20864 !NILP (image_map))
20865 && (hotspot = find_hot_spot (image_map, dx, dy),
20866 CONSP (hotspot))
20867 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20868 {
20869 Lisp_Object area_id, plist;
20870
20871 area_id = XCAR (hotspot);
20872 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20873 If so, we could look for mouse-enter, mouse-leave
20874 properties in PLIST (and do something...). */
20875 if ((plist = XCDR (hotspot), CONSP (plist)))
20876 {
20877 pointer = Fplist_get (plist, Qpointer);
20878 if (NILP (pointer))
20879 pointer = Qhand;
20880 help_echo_string = Fplist_get (plist, Qhelp_echo);
20881 if (!NILP (help_echo_string))
20882 {
20883 help_echo_window = window;
20884 help_echo_object = glyph->object;
20885 help_echo_pos = glyph->charpos;
20886 }
20887 }
20888 }
20889 if (NILP (pointer))
20890 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20891 }
20892 }
20893
20894 /* Clear mouse face if X/Y not over text. */
20895 if (glyph == NULL
20896 || area != TEXT_AREA
20897 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20898 {
20899 if (clear_mouse_face (dpyinfo))
20900 cursor = No_Cursor;
20901 if (NILP (pointer))
20902 {
20903 if (area != TEXT_AREA)
20904 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20905 else
20906 pointer = Vvoid_text_area_pointer;
20907 }
20908 goto set_cursor;
20909 }
20910
20911 pos = glyph->charpos;
20912 object = glyph->object;
20913 if (!STRINGP (object) && !BUFFERP (object))
20914 goto set_cursor;
20915
20916 /* If we get an out-of-range value, return now; avoid an error. */
20917 if (BUFFERP (object) && pos > BUF_Z (b))
20918 goto set_cursor;
20919
20920 /* Make the window's buffer temporarily current for
20921 overlays_at and compute_char_face. */
20922 obuf = current_buffer;
20923 current_buffer = b;
20924 obegv = BEGV;
20925 ozv = ZV;
20926 BEGV = BEG;
20927 ZV = Z;
20928
20929 /* Is this char mouse-active or does it have help-echo? */
20930 position = make_number (pos);
20931
20932 if (BUFFERP (object))
20933 {
20934 /* Put all the overlays we want in a vector in overlay_vec.
20935 Store the length in len. If there are more than 10, make
20936 enough space for all, and try again. */
20937 len = 10;
20938 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20939 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20940 if (noverlays > len)
20941 {
20942 len = noverlays;
20943 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20944 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20945 }
20946
20947 /* Sort overlays into increasing priority order. */
20948 noverlays = sort_overlays (overlay_vec, noverlays, w);
20949 }
20950 else
20951 noverlays = 0;
20952
20953 same_region = (EQ (window, dpyinfo->mouse_face_window)
20954 && vpos >= dpyinfo->mouse_face_beg_row
20955 && vpos <= dpyinfo->mouse_face_end_row
20956 && (vpos > dpyinfo->mouse_face_beg_row
20957 || hpos >= dpyinfo->mouse_face_beg_col)
20958 && (vpos < dpyinfo->mouse_face_end_row
20959 || hpos < dpyinfo->mouse_face_end_col
20960 || dpyinfo->mouse_face_past_end));
20961
20962 if (same_region)
20963 cursor = No_Cursor;
20964
20965 /* Check mouse-face highlighting. */
20966 if (! same_region
20967 /* If there exists an overlay with mouse-face overlapping
20968 the one we are currently highlighting, we have to
20969 check if we enter the overlapping overlay, and then
20970 highlight only that. */
20971 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20972 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20973 {
20974 /* Find the highest priority overlay that has a mouse-face
20975 property. */
20976 overlay = Qnil;
20977 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20978 {
20979 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20980 if (!NILP (mouse_face))
20981 overlay = overlay_vec[i];
20982 }
20983
20984 /* If we're actually highlighting the same overlay as
20985 before, there's no need to do that again. */
20986 if (!NILP (overlay)
20987 && EQ (overlay, dpyinfo->mouse_face_overlay))
20988 goto check_help_echo;
20989
20990 dpyinfo->mouse_face_overlay = overlay;
20991
20992 /* Clear the display of the old active region, if any. */
20993 if (clear_mouse_face (dpyinfo))
20994 cursor = No_Cursor;
20995
20996 /* If no overlay applies, get a text property. */
20997 if (NILP (overlay))
20998 mouse_face = Fget_text_property (position, Qmouse_face, object);
20999
21000 /* Handle the overlay case. */
21001 if (!NILP (overlay))
21002 {
21003 /* Find the range of text around this char that
21004 should be active. */
21005 Lisp_Object before, after;
21006 int ignore;
21007
21008 before = Foverlay_start (overlay);
21009 after = Foverlay_end (overlay);
21010 /* Record this as the current active region. */
21011 fast_find_position (w, XFASTINT (before),
21012 &dpyinfo->mouse_face_beg_col,
21013 &dpyinfo->mouse_face_beg_row,
21014 &dpyinfo->mouse_face_beg_x,
21015 &dpyinfo->mouse_face_beg_y, Qnil);
21016
21017 dpyinfo->mouse_face_past_end
21018 = !fast_find_position (w, XFASTINT (after),
21019 &dpyinfo->mouse_face_end_col,
21020 &dpyinfo->mouse_face_end_row,
21021 &dpyinfo->mouse_face_end_x,
21022 &dpyinfo->mouse_face_end_y, Qnil);
21023 dpyinfo->mouse_face_window = window;
21024
21025 dpyinfo->mouse_face_face_id
21026 = face_at_buffer_position (w, pos, 0, 0,
21027 &ignore, pos + 1,
21028 !dpyinfo->mouse_face_hidden);
21029
21030 /* Display it as active. */
21031 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21032 cursor = No_Cursor;
21033 }
21034 /* Handle the text property case. */
21035 else if (!NILP (mouse_face) && BUFFERP (object))
21036 {
21037 /* Find the range of text around this char that
21038 should be active. */
21039 Lisp_Object before, after, beginning, end;
21040 int ignore;
21041
21042 beginning = Fmarker_position (w->start);
21043 end = make_number (BUF_Z (XBUFFER (object))
21044 - XFASTINT (w->window_end_pos));
21045 before
21046 = Fprevious_single_property_change (make_number (pos + 1),
21047 Qmouse_face,
21048 object, beginning);
21049 after
21050 = Fnext_single_property_change (position, Qmouse_face,
21051 object, end);
21052
21053 /* Record this as the current active region. */
21054 fast_find_position (w, XFASTINT (before),
21055 &dpyinfo->mouse_face_beg_col,
21056 &dpyinfo->mouse_face_beg_row,
21057 &dpyinfo->mouse_face_beg_x,
21058 &dpyinfo->mouse_face_beg_y, Qnil);
21059 dpyinfo->mouse_face_past_end
21060 = !fast_find_position (w, XFASTINT (after),
21061 &dpyinfo->mouse_face_end_col,
21062 &dpyinfo->mouse_face_end_row,
21063 &dpyinfo->mouse_face_end_x,
21064 &dpyinfo->mouse_face_end_y, Qnil);
21065 dpyinfo->mouse_face_window = window;
21066
21067 if (BUFFERP (object))
21068 dpyinfo->mouse_face_face_id
21069 = face_at_buffer_position (w, pos, 0, 0,
21070 &ignore, pos + 1,
21071 !dpyinfo->mouse_face_hidden);
21072
21073 /* Display it as active. */
21074 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21075 cursor = No_Cursor;
21076 }
21077 else if (!NILP (mouse_face) && STRINGP (object))
21078 {
21079 Lisp_Object b, e;
21080 int ignore;
21081
21082 b = Fprevious_single_property_change (make_number (pos + 1),
21083 Qmouse_face,
21084 object, Qnil);
21085 e = Fnext_single_property_change (position, Qmouse_face,
21086 object, Qnil);
21087 if (NILP (b))
21088 b = make_number (0);
21089 if (NILP (e))
21090 e = make_number (SCHARS (object) - 1);
21091 fast_find_string_pos (w, XINT (b), object,
21092 &dpyinfo->mouse_face_beg_col,
21093 &dpyinfo->mouse_face_beg_row,
21094 &dpyinfo->mouse_face_beg_x,
21095 &dpyinfo->mouse_face_beg_y, 0);
21096 fast_find_string_pos (w, XINT (e), object,
21097 &dpyinfo->mouse_face_end_col,
21098 &dpyinfo->mouse_face_end_row,
21099 &dpyinfo->mouse_face_end_x,
21100 &dpyinfo->mouse_face_end_y, 1);
21101 dpyinfo->mouse_face_past_end = 0;
21102 dpyinfo->mouse_face_window = window;
21103 dpyinfo->mouse_face_face_id
21104 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21105 glyph->face_id, 1);
21106 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21107 cursor = No_Cursor;
21108 }
21109 else if (STRINGP (object) && NILP (mouse_face))
21110 {
21111 /* A string which doesn't have mouse-face, but
21112 the text ``under'' it might have. */
21113 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21114 int start = MATRIX_ROW_START_CHARPOS (r);
21115
21116 pos = string_buffer_position (w, object, start);
21117 if (pos > 0)
21118 mouse_face = get_char_property_and_overlay (make_number (pos),
21119 Qmouse_face,
21120 w->buffer,
21121 &overlay);
21122 if (!NILP (mouse_face) && !NILP (overlay))
21123 {
21124 Lisp_Object before = Foverlay_start (overlay);
21125 Lisp_Object after = Foverlay_end (overlay);
21126 int ignore;
21127
21128 /* Note that we might not be able to find position
21129 BEFORE in the glyph matrix if the overlay is
21130 entirely covered by a `display' property. In
21131 this case, we overshoot. So let's stop in
21132 the glyph matrix before glyphs for OBJECT. */
21133 fast_find_position (w, XFASTINT (before),
21134 &dpyinfo->mouse_face_beg_col,
21135 &dpyinfo->mouse_face_beg_row,
21136 &dpyinfo->mouse_face_beg_x,
21137 &dpyinfo->mouse_face_beg_y,
21138 object);
21139
21140 dpyinfo->mouse_face_past_end
21141 = !fast_find_position (w, XFASTINT (after),
21142 &dpyinfo->mouse_face_end_col,
21143 &dpyinfo->mouse_face_end_row,
21144 &dpyinfo->mouse_face_end_x,
21145 &dpyinfo->mouse_face_end_y,
21146 Qnil);
21147 dpyinfo->mouse_face_window = window;
21148 dpyinfo->mouse_face_face_id
21149 = face_at_buffer_position (w, pos, 0, 0,
21150 &ignore, pos + 1,
21151 !dpyinfo->mouse_face_hidden);
21152
21153 /* Display it as active. */
21154 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21155 cursor = No_Cursor;
21156 }
21157 }
21158 }
21159
21160 check_help_echo:
21161
21162 /* Look for a `help-echo' property. */
21163 if (NILP (help_echo_string)) {
21164 Lisp_Object help, overlay;
21165
21166 /* Check overlays first. */
21167 help = overlay = Qnil;
21168 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21169 {
21170 overlay = overlay_vec[i];
21171 help = Foverlay_get (overlay, Qhelp_echo);
21172 }
21173
21174 if (!NILP (help))
21175 {
21176 help_echo_string = help;
21177 help_echo_window = window;
21178 help_echo_object = overlay;
21179 help_echo_pos = pos;
21180 }
21181 else
21182 {
21183 Lisp_Object object = glyph->object;
21184 int charpos = glyph->charpos;
21185
21186 /* Try text properties. */
21187 if (STRINGP (object)
21188 && charpos >= 0
21189 && charpos < SCHARS (object))
21190 {
21191 help = Fget_text_property (make_number (charpos),
21192 Qhelp_echo, object);
21193 if (NILP (help))
21194 {
21195 /* If the string itself doesn't specify a help-echo,
21196 see if the buffer text ``under'' it does. */
21197 struct glyph_row *r
21198 = MATRIX_ROW (w->current_matrix, vpos);
21199 int start = MATRIX_ROW_START_CHARPOS (r);
21200 int pos = string_buffer_position (w, object, start);
21201 if (pos > 0)
21202 {
21203 help = Fget_char_property (make_number (pos),
21204 Qhelp_echo, w->buffer);
21205 if (!NILP (help))
21206 {
21207 charpos = pos;
21208 object = w->buffer;
21209 }
21210 }
21211 }
21212 }
21213 else if (BUFFERP (object)
21214 && charpos >= BEGV
21215 && charpos < ZV)
21216 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21217 object);
21218
21219 if (!NILP (help))
21220 {
21221 help_echo_string = help;
21222 help_echo_window = window;
21223 help_echo_object = object;
21224 help_echo_pos = charpos;
21225 }
21226 }
21227 }
21228
21229 /* Look for a `pointer' property. */
21230 if (NILP (pointer))
21231 {
21232 /* Check overlays first. */
21233 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21234 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21235
21236 if (NILP (pointer))
21237 {
21238 Lisp_Object object = glyph->object;
21239 int charpos = glyph->charpos;
21240
21241 /* Try text properties. */
21242 if (STRINGP (object)
21243 && charpos >= 0
21244 && charpos < SCHARS (object))
21245 {
21246 pointer = Fget_text_property (make_number (charpos),
21247 Qpointer, object);
21248 if (NILP (pointer))
21249 {
21250 /* If the string itself doesn't specify a pointer,
21251 see if the buffer text ``under'' it does. */
21252 struct glyph_row *r
21253 = MATRIX_ROW (w->current_matrix, vpos);
21254 int start = MATRIX_ROW_START_CHARPOS (r);
21255 int pos = string_buffer_position (w, object, start);
21256 if (pos > 0)
21257 pointer = Fget_char_property (make_number (pos),
21258 Qpointer, w->buffer);
21259 }
21260 }
21261 else if (BUFFERP (object)
21262 && charpos >= BEGV
21263 && charpos < ZV)
21264 pointer = Fget_text_property (make_number (charpos),
21265 Qpointer, object);
21266 }
21267 }
21268
21269 BEGV = obegv;
21270 ZV = ozv;
21271 current_buffer = obuf;
21272 }
21273
21274 set_cursor:
21275
21276 define_frame_cursor1 (f, cursor, pointer);
21277 }
21278
21279
21280 /* EXPORT for RIF:
21281 Clear any mouse-face on window W. This function is part of the
21282 redisplay interface, and is called from try_window_id and similar
21283 functions to ensure the mouse-highlight is off. */
21284
21285 void
21286 x_clear_window_mouse_face (w)
21287 struct window *w;
21288 {
21289 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21290 Lisp_Object window;
21291
21292 BLOCK_INPUT;
21293 XSETWINDOW (window, w);
21294 if (EQ (window, dpyinfo->mouse_face_window))
21295 clear_mouse_face (dpyinfo);
21296 UNBLOCK_INPUT;
21297 }
21298
21299
21300 /* EXPORT:
21301 Just discard the mouse face information for frame F, if any.
21302 This is used when the size of F is changed. */
21303
21304 void
21305 cancel_mouse_face (f)
21306 struct frame *f;
21307 {
21308 Lisp_Object window;
21309 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21310
21311 window = dpyinfo->mouse_face_window;
21312 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21313 {
21314 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21315 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21316 dpyinfo->mouse_face_window = Qnil;
21317 }
21318 }
21319
21320
21321 #endif /* HAVE_WINDOW_SYSTEM */
21322
21323 \f
21324 /***********************************************************************
21325 Exposure Events
21326 ***********************************************************************/
21327
21328 #ifdef HAVE_WINDOW_SYSTEM
21329
21330 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21331 which intersects rectangle R. R is in window-relative coordinates. */
21332
21333 static void
21334 expose_area (w, row, r, area)
21335 struct window *w;
21336 struct glyph_row *row;
21337 XRectangle *r;
21338 enum glyph_row_area area;
21339 {
21340 struct glyph *first = row->glyphs[area];
21341 struct glyph *end = row->glyphs[area] + row->used[area];
21342 struct glyph *last;
21343 int first_x, start_x, x;
21344
21345 if (area == TEXT_AREA && row->fill_line_p)
21346 /* If row extends face to end of line write the whole line. */
21347 draw_glyphs (w, 0, row, area,
21348 0, row->used[area],
21349 DRAW_NORMAL_TEXT, 0);
21350 else
21351 {
21352 /* Set START_X to the window-relative start position for drawing glyphs of
21353 AREA. The first glyph of the text area can be partially visible.
21354 The first glyphs of other areas cannot. */
21355 start_x = window_box_left_offset (w, area);
21356 x = start_x;
21357 if (area == TEXT_AREA)
21358 x += row->x;
21359
21360 /* Find the first glyph that must be redrawn. */
21361 while (first < end
21362 && x + first->pixel_width < r->x)
21363 {
21364 x += first->pixel_width;
21365 ++first;
21366 }
21367
21368 /* Find the last one. */
21369 last = first;
21370 first_x = x;
21371 while (last < end
21372 && x < r->x + r->width)
21373 {
21374 x += last->pixel_width;
21375 ++last;
21376 }
21377
21378 /* Repaint. */
21379 if (last > first)
21380 draw_glyphs (w, first_x - start_x, row, area,
21381 first - row->glyphs[area], last - row->glyphs[area],
21382 DRAW_NORMAL_TEXT, 0);
21383 }
21384 }
21385
21386
21387 /* Redraw the parts of the glyph row ROW on window W intersecting
21388 rectangle R. R is in window-relative coordinates. Value is
21389 non-zero if mouse-face was overwritten. */
21390
21391 static int
21392 expose_line (w, row, r)
21393 struct window *w;
21394 struct glyph_row *row;
21395 XRectangle *r;
21396 {
21397 xassert (row->enabled_p);
21398
21399 if (row->mode_line_p || w->pseudo_window_p)
21400 draw_glyphs (w, 0, row, TEXT_AREA,
21401 0, row->used[TEXT_AREA],
21402 DRAW_NORMAL_TEXT, 0);
21403 else
21404 {
21405 if (row->used[LEFT_MARGIN_AREA])
21406 expose_area (w, row, r, LEFT_MARGIN_AREA);
21407 if (row->used[TEXT_AREA])
21408 expose_area (w, row, r, TEXT_AREA);
21409 if (row->used[RIGHT_MARGIN_AREA])
21410 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21411 draw_row_fringe_bitmaps (w, row);
21412 }
21413
21414 return row->mouse_face_p;
21415 }
21416
21417
21418 /* Redraw those parts of glyphs rows during expose event handling that
21419 overlap other rows. Redrawing of an exposed line writes over parts
21420 of lines overlapping that exposed line; this function fixes that.
21421
21422 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21423 row in W's current matrix that is exposed and overlaps other rows.
21424 LAST_OVERLAPPING_ROW is the last such row. */
21425
21426 static void
21427 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21428 struct window *w;
21429 struct glyph_row *first_overlapping_row;
21430 struct glyph_row *last_overlapping_row;
21431 {
21432 struct glyph_row *row;
21433
21434 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21435 if (row->overlapping_p)
21436 {
21437 xassert (row->enabled_p && !row->mode_line_p);
21438
21439 if (row->used[LEFT_MARGIN_AREA])
21440 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21441
21442 if (row->used[TEXT_AREA])
21443 x_fix_overlapping_area (w, row, TEXT_AREA);
21444
21445 if (row->used[RIGHT_MARGIN_AREA])
21446 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21447 }
21448 }
21449
21450
21451 /* Return non-zero if W's cursor intersects rectangle R. */
21452
21453 static int
21454 phys_cursor_in_rect_p (w, r)
21455 struct window *w;
21456 XRectangle *r;
21457 {
21458 XRectangle cr, result;
21459 struct glyph *cursor_glyph;
21460
21461 cursor_glyph = get_phys_cursor_glyph (w);
21462 if (cursor_glyph)
21463 {
21464 /* r is relative to W's box, but w->phys_cursor.x is relative
21465 to left edge of W's TEXT area. Adjust it. */
21466 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21467 cr.y = w->phys_cursor.y;
21468 cr.width = cursor_glyph->pixel_width;
21469 cr.height = w->phys_cursor_height;
21470 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21471 I assume the effect is the same -- and this is portable. */
21472 return x_intersect_rectangles (&cr, r, &result);
21473 }
21474 else
21475 return 0;
21476 }
21477
21478
21479 /* EXPORT:
21480 Draw a vertical window border to the right of window W if W doesn't
21481 have vertical scroll bars. */
21482
21483 void
21484 x_draw_vertical_border (w)
21485 struct window *w;
21486 {
21487 struct frame *f = XFRAME (WINDOW_FRAME (w));
21488
21489 /* We could do better, if we knew what type of scroll-bar the adjacent
21490 windows (on either side) have... But we don't :-(
21491 However, I think this works ok. ++KFS 2003-04-25 */
21492
21493 /* Redraw borders between horizontally adjacent windows. Don't
21494 do it for frames with vertical scroll bars because either the
21495 right scroll bar of a window, or the left scroll bar of its
21496 neighbor will suffice as a border. */
21497 if (!WINDOW_RIGHTMOST_P (w)
21498 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21499 {
21500 int x0, x1, y0, y1;
21501
21502 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21503 y1 -= 1;
21504
21505 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21506 }
21507 else if (!WINDOW_LEFTMOST_P (w)
21508 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21509 {
21510 int x0, x1, y0, y1;
21511
21512 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21513 y1 -= 1;
21514
21515 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21516 }
21517 }
21518
21519
21520 /* Redraw the part of window W intersection rectangle FR. Pixel
21521 coordinates in FR are frame-relative. Call this function with
21522 input blocked. Value is non-zero if the exposure overwrites
21523 mouse-face. */
21524
21525 static int
21526 expose_window (w, fr)
21527 struct window *w;
21528 XRectangle *fr;
21529 {
21530 struct frame *f = XFRAME (w->frame);
21531 XRectangle wr, r;
21532 int mouse_face_overwritten_p = 0;
21533
21534 /* If window is not yet fully initialized, do nothing. This can
21535 happen when toolkit scroll bars are used and a window is split.
21536 Reconfiguring the scroll bar will generate an expose for a newly
21537 created window. */
21538 if (w->current_matrix == NULL)
21539 return 0;
21540
21541 /* When we're currently updating the window, display and current
21542 matrix usually don't agree. Arrange for a thorough display
21543 later. */
21544 if (w == updated_window)
21545 {
21546 SET_FRAME_GARBAGED (f);
21547 return 0;
21548 }
21549
21550 /* Frame-relative pixel rectangle of W. */
21551 wr.x = WINDOW_LEFT_EDGE_X (w);
21552 wr.y = WINDOW_TOP_EDGE_Y (w);
21553 wr.width = WINDOW_TOTAL_WIDTH (w);
21554 wr.height = WINDOW_TOTAL_HEIGHT (w);
21555
21556 if (x_intersect_rectangles (fr, &wr, &r))
21557 {
21558 int yb = window_text_bottom_y (w);
21559 struct glyph_row *row;
21560 int cursor_cleared_p;
21561 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21562
21563 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21564 r.x, r.y, r.width, r.height));
21565
21566 /* Convert to window coordinates. */
21567 r.x -= WINDOW_LEFT_EDGE_X (w);
21568 r.y -= WINDOW_TOP_EDGE_Y (w);
21569
21570 /* Turn off the cursor. */
21571 if (!w->pseudo_window_p
21572 && phys_cursor_in_rect_p (w, &r))
21573 {
21574 x_clear_cursor (w);
21575 cursor_cleared_p = 1;
21576 }
21577 else
21578 cursor_cleared_p = 0;
21579
21580 /* Update lines intersecting rectangle R. */
21581 first_overlapping_row = last_overlapping_row = NULL;
21582 for (row = w->current_matrix->rows;
21583 row->enabled_p;
21584 ++row)
21585 {
21586 int y0 = row->y;
21587 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21588
21589 if ((y0 >= r.y && y0 < r.y + r.height)
21590 || (y1 > r.y && y1 < r.y + r.height)
21591 || (r.y >= y0 && r.y < y1)
21592 || (r.y + r.height > y0 && r.y + r.height < y1))
21593 {
21594 if (row->overlapping_p)
21595 {
21596 if (first_overlapping_row == NULL)
21597 first_overlapping_row = row;
21598 last_overlapping_row = row;
21599 }
21600
21601 if (expose_line (w, row, &r))
21602 mouse_face_overwritten_p = 1;
21603 }
21604
21605 if (y1 >= yb)
21606 break;
21607 }
21608
21609 /* Display the mode line if there is one. */
21610 if (WINDOW_WANTS_MODELINE_P (w)
21611 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21612 row->enabled_p)
21613 && row->y < r.y + r.height)
21614 {
21615 if (expose_line (w, row, &r))
21616 mouse_face_overwritten_p = 1;
21617 }
21618
21619 if (!w->pseudo_window_p)
21620 {
21621 /* Fix the display of overlapping rows. */
21622 if (first_overlapping_row)
21623 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21624
21625 /* Draw border between windows. */
21626 x_draw_vertical_border (w);
21627
21628 /* Turn the cursor on again. */
21629 if (cursor_cleared_p)
21630 update_window_cursor (w, 1);
21631 }
21632 }
21633
21634 #ifdef HAVE_CARBON
21635 /* Display scroll bar for this window. */
21636 if (!NILP (w->vertical_scroll_bar))
21637 {
21638 /* ++KFS:
21639 If this doesn't work here (maybe some header files are missing),
21640 make a function in macterm.c and call it to do the job! */
21641 ControlHandle ch
21642 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21643
21644 Draw1Control (ch);
21645 }
21646 #endif
21647
21648 return mouse_face_overwritten_p;
21649 }
21650
21651
21652
21653 /* Redraw (parts) of all windows in the window tree rooted at W that
21654 intersect R. R contains frame pixel coordinates. Value is
21655 non-zero if the exposure overwrites mouse-face. */
21656
21657 static int
21658 expose_window_tree (w, r)
21659 struct window *w;
21660 XRectangle *r;
21661 {
21662 struct frame *f = XFRAME (w->frame);
21663 int mouse_face_overwritten_p = 0;
21664
21665 while (w && !FRAME_GARBAGED_P (f))
21666 {
21667 if (!NILP (w->hchild))
21668 mouse_face_overwritten_p
21669 |= expose_window_tree (XWINDOW (w->hchild), r);
21670 else if (!NILP (w->vchild))
21671 mouse_face_overwritten_p
21672 |= expose_window_tree (XWINDOW (w->vchild), r);
21673 else
21674 mouse_face_overwritten_p |= expose_window (w, r);
21675
21676 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21677 }
21678
21679 return mouse_face_overwritten_p;
21680 }
21681
21682
21683 /* EXPORT:
21684 Redisplay an exposed area of frame F. X and Y are the upper-left
21685 corner of the exposed rectangle. W and H are width and height of
21686 the exposed area. All are pixel values. W or H zero means redraw
21687 the entire frame. */
21688
21689 void
21690 expose_frame (f, x, y, w, h)
21691 struct frame *f;
21692 int x, y, w, h;
21693 {
21694 XRectangle r;
21695 int mouse_face_overwritten_p = 0;
21696
21697 TRACE ((stderr, "expose_frame "));
21698
21699 /* No need to redraw if frame will be redrawn soon. */
21700 if (FRAME_GARBAGED_P (f))
21701 {
21702 TRACE ((stderr, " garbaged\n"));
21703 return;
21704 }
21705
21706 #ifdef HAVE_CARBON
21707 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21708 or deactivated here, for unknown reasons, activated scroll bars
21709 are shown in deactivated frames in some instances. */
21710 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21711 activate_scroll_bars (f);
21712 else
21713 deactivate_scroll_bars (f);
21714 #endif
21715
21716 /* If basic faces haven't been realized yet, there is no point in
21717 trying to redraw anything. This can happen when we get an expose
21718 event while Emacs is starting, e.g. by moving another window. */
21719 if (FRAME_FACE_CACHE (f) == NULL
21720 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21721 {
21722 TRACE ((stderr, " no faces\n"));
21723 return;
21724 }
21725
21726 if (w == 0 || h == 0)
21727 {
21728 r.x = r.y = 0;
21729 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21730 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21731 }
21732 else
21733 {
21734 r.x = x;
21735 r.y = y;
21736 r.width = w;
21737 r.height = h;
21738 }
21739
21740 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21741 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21742
21743 if (WINDOWP (f->tool_bar_window))
21744 mouse_face_overwritten_p
21745 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21746
21747 #ifdef HAVE_X_WINDOWS
21748 #ifndef MSDOS
21749 #ifndef USE_X_TOOLKIT
21750 if (WINDOWP (f->menu_bar_window))
21751 mouse_face_overwritten_p
21752 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21753 #endif /* not USE_X_TOOLKIT */
21754 #endif
21755 #endif
21756
21757 /* Some window managers support a focus-follows-mouse style with
21758 delayed raising of frames. Imagine a partially obscured frame,
21759 and moving the mouse into partially obscured mouse-face on that
21760 frame. The visible part of the mouse-face will be highlighted,
21761 then the WM raises the obscured frame. With at least one WM, KDE
21762 2.1, Emacs is not getting any event for the raising of the frame
21763 (even tried with SubstructureRedirectMask), only Expose events.
21764 These expose events will draw text normally, i.e. not
21765 highlighted. Which means we must redo the highlight here.
21766 Subsume it under ``we love X''. --gerd 2001-08-15 */
21767 /* Included in Windows version because Windows most likely does not
21768 do the right thing if any third party tool offers
21769 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21770 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21771 {
21772 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21773 if (f == dpyinfo->mouse_face_mouse_frame)
21774 {
21775 int x = dpyinfo->mouse_face_mouse_x;
21776 int y = dpyinfo->mouse_face_mouse_y;
21777 clear_mouse_face (dpyinfo);
21778 note_mouse_highlight (f, x, y);
21779 }
21780 }
21781 }
21782
21783
21784 /* EXPORT:
21785 Determine the intersection of two rectangles R1 and R2. Return
21786 the intersection in *RESULT. Value is non-zero if RESULT is not
21787 empty. */
21788
21789 int
21790 x_intersect_rectangles (r1, r2, result)
21791 XRectangle *r1, *r2, *result;
21792 {
21793 XRectangle *left, *right;
21794 XRectangle *upper, *lower;
21795 int intersection_p = 0;
21796
21797 /* Rearrange so that R1 is the left-most rectangle. */
21798 if (r1->x < r2->x)
21799 left = r1, right = r2;
21800 else
21801 left = r2, right = r1;
21802
21803 /* X0 of the intersection is right.x0, if this is inside R1,
21804 otherwise there is no intersection. */
21805 if (right->x <= left->x + left->width)
21806 {
21807 result->x = right->x;
21808
21809 /* The right end of the intersection is the minimum of the
21810 the right ends of left and right. */
21811 result->width = (min (left->x + left->width, right->x + right->width)
21812 - result->x);
21813
21814 /* Same game for Y. */
21815 if (r1->y < r2->y)
21816 upper = r1, lower = r2;
21817 else
21818 upper = r2, lower = r1;
21819
21820 /* The upper end of the intersection is lower.y0, if this is inside
21821 of upper. Otherwise, there is no intersection. */
21822 if (lower->y <= upper->y + upper->height)
21823 {
21824 result->y = lower->y;
21825
21826 /* The lower end of the intersection is the minimum of the lower
21827 ends of upper and lower. */
21828 result->height = (min (lower->y + lower->height,
21829 upper->y + upper->height)
21830 - result->y);
21831 intersection_p = 1;
21832 }
21833 }
21834
21835 return intersection_p;
21836 }
21837
21838 #endif /* HAVE_WINDOW_SYSTEM */
21839
21840 \f
21841 /***********************************************************************
21842 Initialization
21843 ***********************************************************************/
21844
21845 void
21846 syms_of_xdisp ()
21847 {
21848 Vwith_echo_area_save_vector = Qnil;
21849 staticpro (&Vwith_echo_area_save_vector);
21850
21851 Vmessage_stack = Qnil;
21852 staticpro (&Vmessage_stack);
21853
21854 Qinhibit_redisplay = intern ("inhibit-redisplay");
21855 staticpro (&Qinhibit_redisplay);
21856
21857 message_dolog_marker1 = Fmake_marker ();
21858 staticpro (&message_dolog_marker1);
21859 message_dolog_marker2 = Fmake_marker ();
21860 staticpro (&message_dolog_marker2);
21861 message_dolog_marker3 = Fmake_marker ();
21862 staticpro (&message_dolog_marker3);
21863
21864 #if GLYPH_DEBUG
21865 defsubr (&Sdump_frame_glyph_matrix);
21866 defsubr (&Sdump_glyph_matrix);
21867 defsubr (&Sdump_glyph_row);
21868 defsubr (&Sdump_tool_bar_row);
21869 defsubr (&Strace_redisplay);
21870 defsubr (&Strace_to_stderr);
21871 #endif
21872 #ifdef HAVE_WINDOW_SYSTEM
21873 defsubr (&Stool_bar_lines_needed);
21874 defsubr (&Slookup_image_map);
21875 #endif
21876 defsubr (&Sformat_mode_line);
21877
21878 staticpro (&Qmenu_bar_update_hook);
21879 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21880
21881 staticpro (&Qoverriding_terminal_local_map);
21882 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21883
21884 staticpro (&Qoverriding_local_map);
21885 Qoverriding_local_map = intern ("overriding-local-map");
21886
21887 staticpro (&Qwindow_scroll_functions);
21888 Qwindow_scroll_functions = intern ("window-scroll-functions");
21889
21890 staticpro (&Qredisplay_end_trigger_functions);
21891 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21892
21893 staticpro (&Qinhibit_point_motion_hooks);
21894 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21895
21896 QCdata = intern (":data");
21897 staticpro (&QCdata);
21898 Qdisplay = intern ("display");
21899 staticpro (&Qdisplay);
21900 Qspace_width = intern ("space-width");
21901 staticpro (&Qspace_width);
21902 Qraise = intern ("raise");
21903 staticpro (&Qraise);
21904 Qspace = intern ("space");
21905 staticpro (&Qspace);
21906 Qmargin = intern ("margin");
21907 staticpro (&Qmargin);
21908 Qpointer = intern ("pointer");
21909 staticpro (&Qpointer);
21910 Qleft_margin = intern ("left-margin");
21911 staticpro (&Qleft_margin);
21912 Qright_margin = intern ("right-margin");
21913 staticpro (&Qright_margin);
21914 QCalign_to = intern (":align-to");
21915 staticpro (&QCalign_to);
21916 QCrelative_width = intern (":relative-width");
21917 staticpro (&QCrelative_width);
21918 QCrelative_height = intern (":relative-height");
21919 staticpro (&QCrelative_height);
21920 QCeval = intern (":eval");
21921 staticpro (&QCeval);
21922 QCpropertize = intern (":propertize");
21923 staticpro (&QCpropertize);
21924 QCfile = intern (":file");
21925 staticpro (&QCfile);
21926 Qfontified = intern ("fontified");
21927 staticpro (&Qfontified);
21928 Qfontification_functions = intern ("fontification-functions");
21929 staticpro (&Qfontification_functions);
21930 Qtrailing_whitespace = intern ("trailing-whitespace");
21931 staticpro (&Qtrailing_whitespace);
21932 Qimage = intern ("image");
21933 staticpro (&Qimage);
21934 QCmap = intern (":map");
21935 staticpro (&QCmap);
21936 QCpointer = intern (":pointer");
21937 staticpro (&QCpointer);
21938 Qrect = intern ("rect");
21939 staticpro (&Qrect);
21940 Qcircle = intern ("circle");
21941 staticpro (&Qcircle);
21942 Qpoly = intern ("poly");
21943 staticpro (&Qpoly);
21944 Qmessage_truncate_lines = intern ("message-truncate-lines");
21945 staticpro (&Qmessage_truncate_lines);
21946 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21947 staticpro (&Qcursor_in_non_selected_windows);
21948 Qgrow_only = intern ("grow-only");
21949 staticpro (&Qgrow_only);
21950 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21951 staticpro (&Qinhibit_menubar_update);
21952 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21953 staticpro (&Qinhibit_eval_during_redisplay);
21954 Qposition = intern ("position");
21955 staticpro (&Qposition);
21956 Qbuffer_position = intern ("buffer-position");
21957 staticpro (&Qbuffer_position);
21958 Qobject = intern ("object");
21959 staticpro (&Qobject);
21960 Qbar = intern ("bar");
21961 staticpro (&Qbar);
21962 Qhbar = intern ("hbar");
21963 staticpro (&Qhbar);
21964 Qbox = intern ("box");
21965 staticpro (&Qbox);
21966 Qhollow = intern ("hollow");
21967 staticpro (&Qhollow);
21968 Qhand = intern ("hand");
21969 staticpro (&Qhand);
21970 Qarrow = intern ("arrow");
21971 staticpro (&Qarrow);
21972 Qtext = intern ("text");
21973 staticpro (&Qtext);
21974 Qrisky_local_variable = intern ("risky-local-variable");
21975 staticpro (&Qrisky_local_variable);
21976 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21977 staticpro (&Qinhibit_free_realized_faces);
21978
21979 list_of_error = Fcons (intern ("error"), Qnil);
21980 staticpro (&list_of_error);
21981
21982 last_arrow_position = Qnil;
21983 last_arrow_string = Qnil;
21984 staticpro (&last_arrow_position);
21985 staticpro (&last_arrow_string);
21986
21987 echo_buffer[0] = echo_buffer[1] = Qnil;
21988 staticpro (&echo_buffer[0]);
21989 staticpro (&echo_buffer[1]);
21990
21991 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21992 staticpro (&echo_area_buffer[0]);
21993 staticpro (&echo_area_buffer[1]);
21994
21995 Vmessages_buffer_name = build_string ("*Messages*");
21996 staticpro (&Vmessages_buffer_name);
21997
21998 mode_line_proptrans_alist = Qnil;
21999 staticpro (&mode_line_proptrans_alist);
22000
22001 mode_line_string_list = Qnil;
22002 staticpro (&mode_line_string_list);
22003
22004 help_echo_string = Qnil;
22005 staticpro (&help_echo_string);
22006 help_echo_object = Qnil;
22007 staticpro (&help_echo_object);
22008 help_echo_window = Qnil;
22009 staticpro (&help_echo_window);
22010 previous_help_echo_string = Qnil;
22011 staticpro (&previous_help_echo_string);
22012 help_echo_pos = -1;
22013
22014 #ifdef HAVE_WINDOW_SYSTEM
22015 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22016 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22017 For example, if a block cursor is over a tab, it will be drawn as
22018 wide as that tab on the display. */);
22019 x_stretch_cursor_p = 0;
22020 #endif
22021
22022 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22023 doc: /* *Non-nil means highlight trailing whitespace.
22024 The face used for trailing whitespace is `trailing-whitespace'. */);
22025 Vshow_trailing_whitespace = Qnil;
22026
22027 #ifdef HAVE_WINDOW_SYSTEM
22028 DEFVAR_LISP ("overflow-newline-into-fringe", &Voverflow_newline_into_fringe,
22029 doc: /* *Non-nil means that newline may flow into the right fringe.
22030 This means that display lines which are exactly as wide as the window
22031 (not counting the final newline) will only occupy one screen line, by
22032 showing (or hiding) the final newline in the right fringe; when point
22033 is at the final newline, the cursor is shown in the right fringe.
22034 If nil, also continue lines which are exactly as wide as the window. */);
22035 Voverflow_newline_into_fringe = Qt;
22036 #endif
22037
22038 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22039 doc: /* *The pointer shape to show in void text areas.
22040 Nil means to show the text pointer. Other options are `arrow', `text',
22041 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22042 Vvoid_text_area_pointer = Qarrow;
22043
22044 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22045 doc: /* Non-nil means don't actually do any redisplay.
22046 This is used for internal purposes. */);
22047 Vinhibit_redisplay = Qnil;
22048
22049 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22050 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22051 Vglobal_mode_string = Qnil;
22052
22053 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22054 doc: /* Marker for where to display an arrow on top of the buffer text.
22055 This must be the beginning of a line in order to work.
22056 See also `overlay-arrow-string'. */);
22057 Voverlay_arrow_position = Qnil;
22058
22059 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22060 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
22061 Voverlay_arrow_string = Qnil;
22062
22063 DEFVAR_INT ("scroll-step", &scroll_step,
22064 doc: /* *The number of lines to try scrolling a window by when point moves out.
22065 If that fails to bring point back on frame, point is centered instead.
22066 If this is zero, point is always centered after it moves off frame.
22067 If you want scrolling to always be a line at a time, you should set
22068 `scroll-conservatively' to a large value rather than set this to 1. */);
22069
22070 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22071 doc: /* *Scroll up to this many lines, to bring point back on screen.
22072 A value of zero means to scroll the text to center point vertically
22073 in the window. */);
22074 scroll_conservatively = 0;
22075
22076 DEFVAR_INT ("scroll-margin", &scroll_margin,
22077 doc: /* *Number of lines of margin at the top and bottom of a window.
22078 Recenter the window whenever point gets within this many lines
22079 of the top or bottom of the window. */);
22080 scroll_margin = 0;
22081
22082 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22083 doc: /* Pixels per inch on current display.
22084 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22085 Vdisplay_pixels_per_inch = make_float (72.0);
22086
22087 #if GLYPH_DEBUG
22088 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22089 #endif
22090
22091 DEFVAR_BOOL ("truncate-partial-width-windows",
22092 &truncate_partial_width_windows,
22093 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22094 truncate_partial_width_windows = 1;
22095
22096 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22097 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22098 Any other value means to use the appropriate face, `mode-line',
22099 `header-line', or `menu' respectively. */);
22100 mode_line_inverse_video = 1;
22101
22102 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22103 doc: /* *Maximum buffer size for which line number should be displayed.
22104 If the buffer is bigger than this, the line number does not appear
22105 in the mode line. A value of nil means no limit. */);
22106 Vline_number_display_limit = Qnil;
22107
22108 DEFVAR_INT ("line-number-display-limit-width",
22109 &line_number_display_limit_width,
22110 doc: /* *Maximum line width (in characters) for line number display.
22111 If the average length of the lines near point is bigger than this, then the
22112 line number may be omitted from the mode line. */);
22113 line_number_display_limit_width = 200;
22114
22115 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22116 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22117 highlight_nonselected_windows = 0;
22118
22119 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22120 doc: /* Non-nil if more than one frame is visible on this display.
22121 Minibuffer-only frames don't count, but iconified frames do.
22122 This variable is not guaranteed to be accurate except while processing
22123 `frame-title-format' and `icon-title-format'. */);
22124
22125 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22126 doc: /* Template for displaying the title bar of visible frames.
22127 \(Assuming the window manager supports this feature.)
22128 This variable has the same structure as `mode-line-format' (which see),
22129 and is used only on frames for which no explicit name has been set
22130 \(see `modify-frame-parameters'). */);
22131
22132 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22133 doc: /* Template for displaying the title bar of an iconified frame.
22134 \(Assuming the window manager supports this feature.)
22135 This variable has the same structure as `mode-line-format' (which see),
22136 and is used only on frames for which no explicit name has been set
22137 \(see `modify-frame-parameters'). */);
22138 Vicon_title_format
22139 = Vframe_title_format
22140 = Fcons (intern ("multiple-frames"),
22141 Fcons (build_string ("%b"),
22142 Fcons (Fcons (empty_string,
22143 Fcons (intern ("invocation-name"),
22144 Fcons (build_string ("@"),
22145 Fcons (intern ("system-name"),
22146 Qnil)))),
22147 Qnil)));
22148
22149 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22150 doc: /* Maximum number of lines to keep in the message log buffer.
22151 If nil, disable message logging. If t, log messages but don't truncate
22152 the buffer when it becomes large. */);
22153 Vmessage_log_max = make_number (50);
22154
22155 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22156 doc: /* Functions called before redisplay, if window sizes have changed.
22157 The value should be a list of functions that take one argument.
22158 Just before redisplay, for each frame, if any of its windows have changed
22159 size since the last redisplay, or have been split or deleted,
22160 all the functions in the list are called, with the frame as argument. */);
22161 Vwindow_size_change_functions = Qnil;
22162
22163 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22164 doc: /* List of Functions to call before redisplaying a window with scrolling.
22165 Each function is called with two arguments, the window
22166 and its new display-start position. Note that the value of `window-end'
22167 is not valid when these functions are called. */);
22168 Vwindow_scroll_functions = Qnil;
22169
22170 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22171 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22172 mouse_autoselect_window = 0;
22173
22174 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22175 doc: /* *Non-nil means automatically resize tool-bars.
22176 This increases a tool-bar's height if not all tool-bar items are visible.
22177 It decreases a tool-bar's height when it would display blank lines
22178 otherwise. */);
22179 auto_resize_tool_bars_p = 1;
22180
22181 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22182 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22183 auto_raise_tool_bar_buttons_p = 1;
22184
22185 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22186 doc: /* *Margin around tool-bar buttons in pixels.
22187 If an integer, use that for both horizontal and vertical margins.
22188 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22189 HORZ specifying the horizontal margin, and VERT specifying the
22190 vertical margin. */);
22191 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22192
22193 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22194 doc: /* *Relief thickness of tool-bar buttons. */);
22195 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22196
22197 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22198 doc: /* List of functions to call to fontify regions of text.
22199 Each function is called with one argument POS. Functions must
22200 fontify a region starting at POS in the current buffer, and give
22201 fontified regions the property `fontified'. */);
22202 Vfontification_functions = Qnil;
22203 Fmake_variable_buffer_local (Qfontification_functions);
22204
22205 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22206 &unibyte_display_via_language_environment,
22207 doc: /* *Non-nil means display unibyte text according to language environment.
22208 Specifically this means that unibyte non-ASCII characters
22209 are displayed by converting them to the equivalent multibyte characters
22210 according to the current language environment. As a result, they are
22211 displayed according to the current fontset. */);
22212 unibyte_display_via_language_environment = 0;
22213
22214 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22215 doc: /* *Maximum height for resizing mini-windows.
22216 If a float, it specifies a fraction of the mini-window frame's height.
22217 If an integer, it specifies a number of lines. */);
22218 Vmax_mini_window_height = make_float (0.25);
22219
22220 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22221 doc: /* *How to resize mini-windows.
22222 A value of nil means don't automatically resize mini-windows.
22223 A value of t means resize them to fit the text displayed in them.
22224 A value of `grow-only', the default, means let mini-windows grow
22225 only, until their display becomes empty, at which point the windows
22226 go back to their normal size. */);
22227 Vresize_mini_windows = Qgrow_only;
22228
22229 DEFVAR_LISP ("cursor-in-non-selected-windows",
22230 &Vcursor_in_non_selected_windows,
22231 doc: /* *Cursor type to display in non-selected windows.
22232 t means to use hollow box cursor. See `cursor-type' for other values. */);
22233 Vcursor_in_non_selected_windows = Qt;
22234
22235 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22236 doc: /* Alist specifying how to blink the cursor off.
22237 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22238 `cursor-type' frame-parameter or variable equals ON-STATE,
22239 comparing using `equal', Emacs uses OFF-STATE to specify
22240 how to blink it off. */);
22241 Vblink_cursor_alist = Qnil;
22242
22243 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22244 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22245 automatic_hscrolling_p = 1;
22246
22247 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22248 doc: /* *How many columns away from the window edge point is allowed to get
22249 before automatic hscrolling will horizontally scroll the window. */);
22250 hscroll_margin = 5;
22251
22252 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22253 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22254 When point is less than `automatic-hscroll-margin' columns from the window
22255 edge, automatic hscrolling will scroll the window by the amount of columns
22256 determined by this variable. If its value is a positive integer, scroll that
22257 many columns. If it's a positive floating-point number, it specifies the
22258 fraction of the window's width to scroll. If it's nil or zero, point will be
22259 centered horizontally after the scroll. Any other value, including negative
22260 numbers, are treated as if the value were zero.
22261
22262 Automatic hscrolling always moves point outside the scroll margin, so if
22263 point was more than scroll step columns inside the margin, the window will
22264 scroll more than the value given by the scroll step.
22265
22266 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22267 and `scroll-right' overrides this variable's effect. */);
22268 Vhscroll_step = make_number (0);
22269
22270 DEFVAR_LISP ("image-types", &Vimage_types,
22271 doc: /* List of supported image types.
22272 Each element of the list is a symbol for a supported image type. */);
22273 Vimage_types = Qnil;
22274
22275 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22276 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22277 Bind this around calls to `message' to let it take effect. */);
22278 message_truncate_lines = 0;
22279
22280 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22281 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22282 Can be used to update submenus whose contents should vary. */);
22283 Vmenu_bar_update_hook = Qnil;
22284
22285 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22286 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22287 inhibit_menubar_update = 0;
22288
22289 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22290 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22291 inhibit_eval_during_redisplay = 0;
22292
22293 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22294 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22295 inhibit_free_realized_faces = 0;
22296
22297 #if GLYPH_DEBUG
22298 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22299 doc: /* Inhibit try_window_id display optimization. */);
22300 inhibit_try_window_id = 0;
22301
22302 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22303 doc: /* Inhibit try_window_reusing display optimization. */);
22304 inhibit_try_window_reusing = 0;
22305
22306 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22307 doc: /* Inhibit try_cursor_movement display optimization. */);
22308 inhibit_try_cursor_movement = 0;
22309 #endif /* GLYPH_DEBUG */
22310 }
22311
22312
22313 /* Initialize this module when Emacs starts. */
22314
22315 void
22316 init_xdisp ()
22317 {
22318 Lisp_Object root_window;
22319 struct window *mini_w;
22320
22321 current_header_line_height = current_mode_line_height = -1;
22322
22323 CHARPOS (this_line_start_pos) = 0;
22324
22325 mini_w = XWINDOW (minibuf_window);
22326 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22327
22328 if (!noninteractive)
22329 {
22330 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22331 int i;
22332
22333 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22334 set_window_height (root_window,
22335 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22336 0);
22337 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22338 set_window_height (minibuf_window, 1, 0);
22339
22340 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22341 mini_w->total_cols = make_number (FRAME_COLS (f));
22342
22343 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22344 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22345 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22346
22347 /* The default ellipsis glyphs `...'. */
22348 for (i = 0; i < 3; ++i)
22349 default_invis_vector[i] = make_number ('.');
22350 }
22351
22352 {
22353 /* Allocate the buffer for frame titles.
22354 Also used for `format-mode-line'. */
22355 int size = 100;
22356 frame_title_buf = (char *) xmalloc (size);
22357 frame_title_buf_end = frame_title_buf + size;
22358 frame_title_ptr = NULL;
22359 }
22360
22361 help_echo_showing_p = 0;
22362 }
22363
22364
22365 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22366 (do not change this comment) */