Merge from emacs--rel--22
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
37
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
47
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
61 |
62 expose_window (asynchronous) |
63 |
64 X expose events -----+
65
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
70
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
80
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
86
87
88 Direct operations.
89
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
94
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
101
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
107
108
109 Desired matrices.
110
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
117
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
124
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
130
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
137
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
145
146
147 Frame matrices.
148
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
155
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
167
168 #include <config.h>
169 #include <stdio.h>
170
171 #include "lisp.h"
172 #include "keyboard.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "character.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 #endif
202
203 #ifdef HAVE_WINDOW_SYSTEM
204 #include "font.h"
205 #endif /* HAVE_WINDOW_SYSTEM */
206
207 #ifndef FRAME_X_OUTPUT
208 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
209 #endif
210
211 #define INFINITY 10000000
212
213 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
214 || defined (USE_GTK)
215 extern void set_frame_menubar P_ ((struct frame *f, int, int));
216 extern int pending_menu_activation;
217 #endif
218
219 extern int interrupt_input;
220 extern int command_loop_level;
221
222 extern Lisp_Object do_mouse_tracking;
223
224 extern int minibuffer_auto_raise;
225 extern Lisp_Object Vminibuffer_list;
226
227 extern Lisp_Object Qface;
228 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
229
230 extern Lisp_Object Voverriding_local_map;
231 extern Lisp_Object Voverriding_local_map_menu_flag;
232 extern Lisp_Object Qmenu_item;
233 extern Lisp_Object Qwhen;
234 extern Lisp_Object Qhelp_echo;
235
236 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
237 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
238 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
239 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
240 Lisp_Object Qinhibit_point_motion_hooks;
241 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
242 Lisp_Object Qfontified;
243 Lisp_Object Qgrow_only;
244 Lisp_Object Qinhibit_eval_during_redisplay;
245 Lisp_Object Qbuffer_position, Qposition, Qobject;
246
247 /* Cursor shapes */
248 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
249
250 /* Pointer shapes */
251 Lisp_Object Qarrow, Qhand, Qtext;
252
253 Lisp_Object Qrisky_local_variable;
254
255 /* Holds the list (error). */
256 Lisp_Object list_of_error;
257
258 /* Functions called to fontify regions of text. */
259
260 Lisp_Object Vfontification_functions;
261 Lisp_Object Qfontification_functions;
262
263 /* Non-nil means automatically select any window when the mouse
264 cursor moves into it. */
265 Lisp_Object Vmouse_autoselect_window;
266
267 /* Non-zero means draw tool bar buttons raised when the mouse moves
268 over them. */
269
270 int auto_raise_tool_bar_buttons_p;
271
272 /* Non-zero means to reposition window if cursor line is only partially visible. */
273
274 int make_cursor_line_fully_visible_p;
275
276 /* Margin below tool bar in pixels. 0 or nil means no margin.
277 If value is `internal-border-width' or `border-width',
278 the corresponding frame parameter is used. */
279
280 Lisp_Object Vtool_bar_border;
281
282 /* Margin around tool bar buttons in pixels. */
283
284 Lisp_Object Vtool_bar_button_margin;
285
286 /* Thickness of shadow to draw around tool bar buttons. */
287
288 EMACS_INT tool_bar_button_relief;
289
290 /* Non-nil means automatically resize tool-bars so that all tool-bar
291 items are visible, and no blank lines remain.
292
293 If value is `grow-only', only make tool-bar bigger. */
294
295 Lisp_Object Vauto_resize_tool_bars;
296
297 /* Non-zero means draw block and hollow cursor as wide as the glyph
298 under it. For example, if a block cursor is over a tab, it will be
299 drawn as wide as that tab on the display. */
300
301 int x_stretch_cursor_p;
302
303 /* Non-nil means don't actually do any redisplay. */
304
305 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
306
307 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
308
309 int inhibit_eval_during_redisplay;
310
311 /* Names of text properties relevant for redisplay. */
312
313 Lisp_Object Qdisplay;
314 extern Lisp_Object Qface, Qinvisible, Qwidth;
315
316 /* Symbols used in text property values. */
317
318 Lisp_Object Vdisplay_pixels_per_inch;
319 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
320 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
321 Lisp_Object Qslice;
322 Lisp_Object Qcenter;
323 Lisp_Object Qmargin, Qpointer;
324 Lisp_Object Qline_height;
325 extern Lisp_Object Qheight;
326 extern Lisp_Object QCwidth, QCheight, QCascent;
327 extern Lisp_Object Qscroll_bar;
328 extern Lisp_Object Qcursor;
329
330 /* Non-nil means highlight trailing whitespace. */
331
332 Lisp_Object Vshow_trailing_whitespace;
333
334 /* Non-nil means escape non-break space and hyphens. */
335
336 Lisp_Object Vnobreak_char_display;
337
338 #ifdef HAVE_WINDOW_SYSTEM
339 extern Lisp_Object Voverflow_newline_into_fringe;
340
341 /* Test if overflow newline into fringe. Called with iterator IT
342 at or past right window margin, and with IT->current_x set. */
343
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
345 (!NILP (Voverflow_newline_into_fringe) \
346 && FRAME_WINDOW_P (it->f) \
347 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
348 && it->current_x == it->last_visible_x)
349
350 #endif /* HAVE_WINDOW_SYSTEM */
351
352 /* Non-nil means show the text cursor in void text areas
353 i.e. in blank areas after eol and eob. This used to be
354 the default in 21.3. */
355
356 Lisp_Object Vvoid_text_area_pointer;
357
358 /* Name of the face used to highlight trailing whitespace. */
359
360 Lisp_Object Qtrailing_whitespace;
361
362 /* Name and number of the face used to highlight escape glyphs. */
363
364 Lisp_Object Qescape_glyph;
365
366 /* Name and number of the face used to highlight non-breaking spaces. */
367
368 Lisp_Object Qnobreak_space;
369
370 /* The symbol `image' which is the car of the lists used to represent
371 images in Lisp. */
372
373 Lisp_Object Qimage;
374
375 /* The image map types. */
376 Lisp_Object QCmap, QCpointer;
377 Lisp_Object Qrect, Qcircle, Qpoly;
378
379 /* Non-zero means print newline to stdout before next mini-buffer
380 message. */
381
382 int noninteractive_need_newline;
383
384 /* Non-zero means print newline to message log before next message. */
385
386 static int message_log_need_newline;
387
388 /* Three markers that message_dolog uses.
389 It could allocate them itself, but that causes trouble
390 in handling memory-full errors. */
391 static Lisp_Object message_dolog_marker1;
392 static Lisp_Object message_dolog_marker2;
393 static Lisp_Object message_dolog_marker3;
394 \f
395 /* The buffer position of the first character appearing entirely or
396 partially on the line of the selected window which contains the
397 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
398 redisplay optimization in redisplay_internal. */
399
400 static struct text_pos this_line_start_pos;
401
402 /* Number of characters past the end of the line above, including the
403 terminating newline. */
404
405 static struct text_pos this_line_end_pos;
406
407 /* The vertical positions and the height of this line. */
408
409 static int this_line_vpos;
410 static int this_line_y;
411 static int this_line_pixel_height;
412
413 /* X position at which this display line starts. Usually zero;
414 negative if first character is partially visible. */
415
416 static int this_line_start_x;
417
418 /* Buffer that this_line_.* variables are referring to. */
419
420 static struct buffer *this_line_buffer;
421
422 /* Nonzero means truncate lines in all windows less wide than the
423 frame. */
424
425 int truncate_partial_width_windows;
426
427 /* A flag to control how to display unibyte 8-bit character. */
428
429 int unibyte_display_via_language_environment;
430
431 /* Nonzero means we have more than one non-mini-buffer-only frame.
432 Not guaranteed to be accurate except while parsing
433 frame-title-format. */
434
435 int multiple_frames;
436
437 Lisp_Object Vglobal_mode_string;
438
439
440 /* List of variables (symbols) which hold markers for overlay arrows.
441 The symbols on this list are examined during redisplay to determine
442 where to display overlay arrows. */
443
444 Lisp_Object Voverlay_arrow_variable_list;
445
446 /* Marker for where to display an arrow on top of the buffer text. */
447
448 Lisp_Object Voverlay_arrow_position;
449
450 /* String to display for the arrow. Only used on terminal frames. */
451
452 Lisp_Object Voverlay_arrow_string;
453
454 /* Values of those variables at last redisplay are stored as
455 properties on `overlay-arrow-position' symbol. However, if
456 Voverlay_arrow_position is a marker, last-arrow-position is its
457 numerical position. */
458
459 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
460
461 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
462 properties on a symbol in overlay-arrow-variable-list. */
463
464 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
465
466 /* Like mode-line-format, but for the title bar on a visible frame. */
467
468 Lisp_Object Vframe_title_format;
469
470 /* Like mode-line-format, but for the title bar on an iconified frame. */
471
472 Lisp_Object Vicon_title_format;
473
474 /* List of functions to call when a window's size changes. These
475 functions get one arg, a frame on which one or more windows' sizes
476 have changed. */
477
478 static Lisp_Object Vwindow_size_change_functions;
479
480 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
481
482 /* Nonzero if an overlay arrow has been displayed in this window. */
483
484 static int overlay_arrow_seen;
485
486 /* Nonzero means highlight the region even in nonselected windows. */
487
488 int highlight_nonselected_windows;
489
490 /* If cursor motion alone moves point off frame, try scrolling this
491 many lines up or down if that will bring it back. */
492
493 static EMACS_INT scroll_step;
494
495 /* Nonzero means scroll just far enough to bring point back on the
496 screen, when appropriate. */
497
498 static EMACS_INT scroll_conservatively;
499
500 /* Recenter the window whenever point gets within this many lines of
501 the top or bottom of the window. This value is translated into a
502 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
503 that there is really a fixed pixel height scroll margin. */
504
505 EMACS_INT scroll_margin;
506
507 /* Number of windows showing the buffer of the selected window (or
508 another buffer with the same base buffer). keyboard.c refers to
509 this. */
510
511 int buffer_shared;
512
513 /* Vector containing glyphs for an ellipsis `...'. */
514
515 static Lisp_Object default_invis_vector[3];
516
517 /* Zero means display the mode-line/header-line/menu-bar in the default face
518 (this slightly odd definition is for compatibility with previous versions
519 of emacs), non-zero means display them using their respective faces.
520
521 This variable is deprecated. */
522
523 int mode_line_inverse_video;
524
525 /* Prompt to display in front of the mini-buffer contents. */
526
527 Lisp_Object minibuf_prompt;
528
529 /* Width of current mini-buffer prompt. Only set after display_line
530 of the line that contains the prompt. */
531
532 int minibuf_prompt_width;
533
534 /* This is the window where the echo area message was displayed. It
535 is always a mini-buffer window, but it may not be the same window
536 currently active as a mini-buffer. */
537
538 Lisp_Object echo_area_window;
539
540 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
541 pushes the current message and the value of
542 message_enable_multibyte on the stack, the function restore_message
543 pops the stack and displays MESSAGE again. */
544
545 Lisp_Object Vmessage_stack;
546
547 /* Nonzero means multibyte characters were enabled when the echo area
548 message was specified. */
549
550 int message_enable_multibyte;
551
552 /* Nonzero if we should redraw the mode lines on the next redisplay. */
553
554 int update_mode_lines;
555
556 /* Nonzero if window sizes or contents have changed since last
557 redisplay that finished. */
558
559 int windows_or_buffers_changed;
560
561 /* Nonzero means a frame's cursor type has been changed. */
562
563 int cursor_type_changed;
564
565 /* Nonzero after display_mode_line if %l was used and it displayed a
566 line number. */
567
568 int line_number_displayed;
569
570 /* Maximum buffer size for which to display line numbers. */
571
572 Lisp_Object Vline_number_display_limit;
573
574 /* Line width to consider when repositioning for line number display. */
575
576 static EMACS_INT line_number_display_limit_width;
577
578 /* Number of lines to keep in the message log buffer. t means
579 infinite. nil means don't log at all. */
580
581 Lisp_Object Vmessage_log_max;
582
583 /* The name of the *Messages* buffer, a string. */
584
585 static Lisp_Object Vmessages_buffer_name;
586
587 /* Current, index 0, and last displayed echo area message. Either
588 buffers from echo_buffers, or nil to indicate no message. */
589
590 Lisp_Object echo_area_buffer[2];
591
592 /* The buffers referenced from echo_area_buffer. */
593
594 static Lisp_Object echo_buffer[2];
595
596 /* A vector saved used in with_area_buffer to reduce consing. */
597
598 static Lisp_Object Vwith_echo_area_save_vector;
599
600 /* Non-zero means display_echo_area should display the last echo area
601 message again. Set by redisplay_preserve_echo_area. */
602
603 static int display_last_displayed_message_p;
604
605 /* Nonzero if echo area is being used by print; zero if being used by
606 message. */
607
608 int message_buf_print;
609
610 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
611
612 Lisp_Object Qinhibit_menubar_update;
613 int inhibit_menubar_update;
614
615 /* When evaluating expressions from menu bar items (enable conditions,
616 for instance), this is the frame they are being processed for. */
617
618 Lisp_Object Vmenu_updating_frame;
619
620 /* Maximum height for resizing mini-windows. Either a float
621 specifying a fraction of the available height, or an integer
622 specifying a number of lines. */
623
624 Lisp_Object Vmax_mini_window_height;
625
626 /* Non-zero means messages should be displayed with truncated
627 lines instead of being continued. */
628
629 int message_truncate_lines;
630 Lisp_Object Qmessage_truncate_lines;
631
632 /* Set to 1 in clear_message to make redisplay_internal aware
633 of an emptied echo area. */
634
635 static int message_cleared_p;
636
637 /* How to blink the default frame cursor off. */
638 Lisp_Object Vblink_cursor_alist;
639
640 /* A scratch glyph row with contents used for generating truncation
641 glyphs. Also used in direct_output_for_insert. */
642
643 #define MAX_SCRATCH_GLYPHS 100
644 struct glyph_row scratch_glyph_row;
645 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
646
647 /* Ascent and height of the last line processed by move_it_to. */
648
649 static int last_max_ascent, last_height;
650
651 /* Non-zero if there's a help-echo in the echo area. */
652
653 int help_echo_showing_p;
654
655 /* If >= 0, computed, exact values of mode-line and header-line height
656 to use in the macros CURRENT_MODE_LINE_HEIGHT and
657 CURRENT_HEADER_LINE_HEIGHT. */
658
659 int current_mode_line_height, current_header_line_height;
660
661 /* The maximum distance to look ahead for text properties. Values
662 that are too small let us call compute_char_face and similar
663 functions too often which is expensive. Values that are too large
664 let us call compute_char_face and alike too often because we
665 might not be interested in text properties that far away. */
666
667 #define TEXT_PROP_DISTANCE_LIMIT 100
668
669 #if GLYPH_DEBUG
670
671 /* Variables to turn off display optimizations from Lisp. */
672
673 int inhibit_try_window_id, inhibit_try_window_reusing;
674 int inhibit_try_cursor_movement;
675
676 /* Non-zero means print traces of redisplay if compiled with
677 GLYPH_DEBUG != 0. */
678
679 int trace_redisplay_p;
680
681 #endif /* GLYPH_DEBUG */
682
683 #ifdef DEBUG_TRACE_MOVE
684 /* Non-zero means trace with TRACE_MOVE to stderr. */
685 int trace_move;
686
687 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
688 #else
689 #define TRACE_MOVE(x) (void) 0
690 #endif
691
692 /* Non-zero means automatically scroll windows horizontally to make
693 point visible. */
694
695 int automatic_hscrolling_p;
696 Lisp_Object Qauto_hscroll_mode;
697
698 /* How close to the margin can point get before the window is scrolled
699 horizontally. */
700 EMACS_INT hscroll_margin;
701
702 /* How much to scroll horizontally when point is inside the above margin. */
703 Lisp_Object Vhscroll_step;
704
705 /* The variable `resize-mini-windows'. If nil, don't resize
706 mini-windows. If t, always resize them to fit the text they
707 display. If `grow-only', let mini-windows grow only until they
708 become empty. */
709
710 Lisp_Object Vresize_mini_windows;
711
712 /* Buffer being redisplayed -- for redisplay_window_error. */
713
714 struct buffer *displayed_buffer;
715
716 /* Space between overline and text. */
717
718 EMACS_INT overline_margin;
719
720 /* Value returned from text property handlers (see below). */
721
722 enum prop_handled
723 {
724 HANDLED_NORMALLY,
725 HANDLED_RECOMPUTE_PROPS,
726 HANDLED_OVERLAY_STRING_CONSUMED,
727 HANDLED_RETURN
728 };
729
730 /* A description of text properties that redisplay is interested
731 in. */
732
733 struct props
734 {
735 /* The name of the property. */
736 Lisp_Object *name;
737
738 /* A unique index for the property. */
739 enum prop_idx idx;
740
741 /* A handler function called to set up iterator IT from the property
742 at IT's current position. Value is used to steer handle_stop. */
743 enum prop_handled (*handler) P_ ((struct it *it));
744 };
745
746 static enum prop_handled handle_face_prop P_ ((struct it *));
747 static enum prop_handled handle_invisible_prop P_ ((struct it *));
748 static enum prop_handled handle_display_prop P_ ((struct it *));
749 static enum prop_handled handle_composition_prop P_ ((struct it *));
750 static enum prop_handled handle_overlay_change P_ ((struct it *));
751 static enum prop_handled handle_fontified_prop P_ ((struct it *));
752 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
753
754 /* Properties handled by iterators. */
755
756 static struct props it_props[] =
757 {
758 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
759 /* Handle `face' before `display' because some sub-properties of
760 `display' need to know the face. */
761 {&Qface, FACE_PROP_IDX, handle_face_prop},
762 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
763 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
764 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
765 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
766 {NULL, 0, NULL}
767 };
768
769 /* Value is the position described by X. If X is a marker, value is
770 the marker_position of X. Otherwise, value is X. */
771
772 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
773
774 /* Enumeration returned by some move_it_.* functions internally. */
775
776 enum move_it_result
777 {
778 /* Not used. Undefined value. */
779 MOVE_UNDEFINED,
780
781 /* Move ended at the requested buffer position or ZV. */
782 MOVE_POS_MATCH_OR_ZV,
783
784 /* Move ended at the requested X pixel position. */
785 MOVE_X_REACHED,
786
787 /* Move within a line ended at the end of a line that must be
788 continued. */
789 MOVE_LINE_CONTINUED,
790
791 /* Move within a line ended at the end of a line that would
792 be displayed truncated. */
793 MOVE_LINE_TRUNCATED,
794
795 /* Move within a line ended at a line end. */
796 MOVE_NEWLINE_OR_CR
797 };
798
799 /* This counter is used to clear the face cache every once in a while
800 in redisplay_internal. It is incremented for each redisplay.
801 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
802 cleared. */
803
804 #define CLEAR_FACE_CACHE_COUNT 500
805 static int clear_face_cache_count;
806
807 /* Similarly for the image cache. */
808
809 #ifdef HAVE_WINDOW_SYSTEM
810 #define CLEAR_IMAGE_CACHE_COUNT 101
811 static int clear_image_cache_count;
812 #endif
813
814 /* Non-zero while redisplay_internal is in progress. */
815
816 int redisplaying_p;
817
818 /* Non-zero means don't free realized faces. Bound while freeing
819 realized faces is dangerous because glyph matrices might still
820 reference them. */
821
822 int inhibit_free_realized_faces;
823 Lisp_Object Qinhibit_free_realized_faces;
824
825 /* If a string, XTread_socket generates an event to display that string.
826 (The display is done in read_char.) */
827
828 Lisp_Object help_echo_string;
829 Lisp_Object help_echo_window;
830 Lisp_Object help_echo_object;
831 int help_echo_pos;
832
833 /* Temporary variable for XTread_socket. */
834
835 Lisp_Object previous_help_echo_string;
836
837 /* Null glyph slice */
838
839 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
840
841 \f
842 /* Function prototypes. */
843
844 static void setup_for_ellipsis P_ ((struct it *, int));
845 static void mark_window_display_accurate_1 P_ ((struct window *, int));
846 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
847 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
848 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
849 static int redisplay_mode_lines P_ ((Lisp_Object, int));
850 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
851
852 #if 0
853 static int invisible_text_between_p P_ ((struct it *, int, int));
854 #endif
855
856 static void pint2str P_ ((char *, int, int));
857 static void pint2hrstr P_ ((char *, int, int));
858 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
859 struct text_pos));
860 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
861 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
862 static void store_mode_line_noprop_char P_ ((char));
863 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
864 static void x_consider_frame_title P_ ((Lisp_Object));
865 static void handle_stop P_ ((struct it *));
866 static int tool_bar_lines_needed P_ ((struct frame *, int *));
867 static int single_display_spec_intangible_p P_ ((Lisp_Object));
868 static void ensure_echo_area_buffers P_ ((void));
869 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
870 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
871 static int with_echo_area_buffer P_ ((struct window *, int,
872 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
873 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
874 static void clear_garbaged_frames P_ ((void));
875 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
877 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static int display_echo_area P_ ((struct window *));
879 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
881 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
882 static int string_char_and_length P_ ((const unsigned char *, int, int *));
883 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
884 struct text_pos));
885 static int compute_window_start_on_continuation_line P_ ((struct window *));
886 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
887 static void insert_left_trunc_glyphs P_ ((struct it *));
888 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
889 Lisp_Object));
890 static void extend_face_to_end_of_line P_ ((struct it *));
891 static int append_space_for_newline P_ ((struct it *, int));
892 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
893 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
894 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
895 static int trailing_whitespace_p P_ ((int));
896 static int message_log_check_duplicate P_ ((int, int, int, int));
897 static void push_it P_ ((struct it *));
898 static void pop_it P_ ((struct it *));
899 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
900 static void select_frame_for_redisplay P_ ((Lisp_Object));
901 static void redisplay_internal P_ ((int));
902 static int echo_area_display P_ ((int));
903 static void redisplay_windows P_ ((Lisp_Object));
904 static void redisplay_window P_ ((Lisp_Object, int));
905 static Lisp_Object redisplay_window_error ();
906 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
907 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
908 static int update_menu_bar P_ ((struct frame *, int, int));
909 static int try_window_reusing_current_matrix P_ ((struct window *));
910 static int try_window_id P_ ((struct window *));
911 static int display_line P_ ((struct it *));
912 static int display_mode_lines P_ ((struct window *));
913 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
914 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
915 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
916 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
917 static void display_menu_bar P_ ((struct window *));
918 static int display_count_lines P_ ((int, int, int, int, int *));
919 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
920 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
921 static void compute_line_metrics P_ ((struct it *));
922 static void run_redisplay_end_trigger_hook P_ ((struct it *));
923 static int get_overlay_strings P_ ((struct it *, int));
924 static int get_overlay_strings_1 P_ ((struct it *, int, int));
925 static void next_overlay_string P_ ((struct it *));
926 static void reseat P_ ((struct it *, struct text_pos, int));
927 static void reseat_1 P_ ((struct it *, struct text_pos, int));
928 static void back_to_previous_visible_line_start P_ ((struct it *));
929 void reseat_at_previous_visible_line_start P_ ((struct it *));
930 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
931 static int next_element_from_ellipsis P_ ((struct it *));
932 static int next_element_from_display_vector P_ ((struct it *));
933 static int next_element_from_string P_ ((struct it *));
934 static int next_element_from_c_string P_ ((struct it *));
935 static int next_element_from_buffer P_ ((struct it *));
936 static int next_element_from_composition P_ ((struct it *));
937 static int next_element_from_image P_ ((struct it *));
938 static int next_element_from_stretch P_ ((struct it *));
939 static void load_overlay_strings P_ ((struct it *, int));
940 static int init_from_display_pos P_ ((struct it *, struct window *,
941 struct display_pos *));
942 static void reseat_to_string P_ ((struct it *, unsigned char *,
943 Lisp_Object, int, int, int, int));
944 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
945 int, int, int));
946 void move_it_vertically_backward P_ ((struct it *, int));
947 static void init_to_row_start P_ ((struct it *, struct window *,
948 struct glyph_row *));
949 static int init_to_row_end P_ ((struct it *, struct window *,
950 struct glyph_row *));
951 static void back_to_previous_line_start P_ ((struct it *));
952 static int forward_to_next_line_start P_ ((struct it *, int *));
953 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
954 Lisp_Object, int));
955 static struct text_pos string_pos P_ ((int, Lisp_Object));
956 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
957 static int number_of_chars P_ ((unsigned char *, int));
958 static void compute_stop_pos P_ ((struct it *));
959 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
960 Lisp_Object));
961 static int face_before_or_after_it_pos P_ ((struct it *, int));
962 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
963 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
964 Lisp_Object, Lisp_Object,
965 struct text_pos *, int));
966 static int underlying_face_id P_ ((struct it *));
967 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
968 struct window *));
969
970 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
971 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
972
973 #ifdef HAVE_WINDOW_SYSTEM
974
975 static void update_tool_bar P_ ((struct frame *, int));
976 static void build_desired_tool_bar_string P_ ((struct frame *f));
977 static int redisplay_tool_bar P_ ((struct frame *));
978 static void display_tool_bar_line P_ ((struct it *, int));
979 static void notice_overwritten_cursor P_ ((struct window *,
980 enum glyph_row_area,
981 int, int, int, int));
982
983
984
985 #endif /* HAVE_WINDOW_SYSTEM */
986
987 \f
988 /***********************************************************************
989 Window display dimensions
990 ***********************************************************************/
991
992 /* Return the bottom boundary y-position for text lines in window W.
993 This is the first y position at which a line cannot start.
994 It is relative to the top of the window.
995
996 This is the height of W minus the height of a mode line, if any. */
997
998 INLINE int
999 window_text_bottom_y (w)
1000 struct window *w;
1001 {
1002 int height = WINDOW_TOTAL_HEIGHT (w);
1003
1004 if (WINDOW_WANTS_MODELINE_P (w))
1005 height -= CURRENT_MODE_LINE_HEIGHT (w);
1006 return height;
1007 }
1008
1009 /* Return the pixel width of display area AREA of window W. AREA < 0
1010 means return the total width of W, not including fringes to
1011 the left and right of the window. */
1012
1013 INLINE int
1014 window_box_width (w, area)
1015 struct window *w;
1016 int area;
1017 {
1018 int cols = XFASTINT (w->total_cols);
1019 int pixels = 0;
1020
1021 if (!w->pseudo_window_p)
1022 {
1023 cols -= WINDOW_SCROLL_BAR_COLS (w);
1024
1025 if (area == TEXT_AREA)
1026 {
1027 if (INTEGERP (w->left_margin_cols))
1028 cols -= XFASTINT (w->left_margin_cols);
1029 if (INTEGERP (w->right_margin_cols))
1030 cols -= XFASTINT (w->right_margin_cols);
1031 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1032 }
1033 else if (area == LEFT_MARGIN_AREA)
1034 {
1035 cols = (INTEGERP (w->left_margin_cols)
1036 ? XFASTINT (w->left_margin_cols) : 0);
1037 pixels = 0;
1038 }
1039 else if (area == RIGHT_MARGIN_AREA)
1040 {
1041 cols = (INTEGERP (w->right_margin_cols)
1042 ? XFASTINT (w->right_margin_cols) : 0);
1043 pixels = 0;
1044 }
1045 }
1046
1047 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1048 }
1049
1050
1051 /* Return the pixel height of the display area of window W, not
1052 including mode lines of W, if any. */
1053
1054 INLINE int
1055 window_box_height (w)
1056 struct window *w;
1057 {
1058 struct frame *f = XFRAME (w->frame);
1059 int height = WINDOW_TOTAL_HEIGHT (w);
1060
1061 xassert (height >= 0);
1062
1063 /* Note: the code below that determines the mode-line/header-line
1064 height is essentially the same as that contained in the macro
1065 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1066 the appropriate glyph row has its `mode_line_p' flag set,
1067 and if it doesn't, uses estimate_mode_line_height instead. */
1068
1069 if (WINDOW_WANTS_MODELINE_P (w))
1070 {
1071 struct glyph_row *ml_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (ml_row && ml_row->mode_line_p)
1076 height -= ml_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1079 }
1080
1081 if (WINDOW_WANTS_HEADER_LINE_P (w))
1082 {
1083 struct glyph_row *hl_row
1084 = (w->current_matrix && w->current_matrix->rows
1085 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1086 : 0);
1087 if (hl_row && hl_row->mode_line_p)
1088 height -= hl_row->height;
1089 else
1090 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1091 }
1092
1093 /* With a very small font and a mode-line that's taller than
1094 default, we might end up with a negative height. */
1095 return max (0, height);
1096 }
1097
1098 /* Return the window-relative coordinate of the left edge of display
1099 area AREA of window W. AREA < 0 means return the left edge of the
1100 whole window, to the right of the left fringe of W. */
1101
1102 INLINE int
1103 window_box_left_offset (w, area)
1104 struct window *w;
1105 int area;
1106 {
1107 int x;
1108
1109 if (w->pseudo_window_p)
1110 return 0;
1111
1112 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1113
1114 if (area == TEXT_AREA)
1115 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1116 + window_box_width (w, LEFT_MARGIN_AREA));
1117 else if (area == RIGHT_MARGIN_AREA)
1118 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1119 + window_box_width (w, LEFT_MARGIN_AREA)
1120 + window_box_width (w, TEXT_AREA)
1121 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1122 ? 0
1123 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1124 else if (area == LEFT_MARGIN_AREA
1125 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1126 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1127
1128 return x;
1129 }
1130
1131
1132 /* Return the window-relative coordinate of the right edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the left of the right fringe of W. */
1135
1136 INLINE int
1137 window_box_right_offset (w, area)
1138 struct window *w;
1139 int area;
1140 {
1141 return window_box_left_offset (w, area) + window_box_width (w, area);
1142 }
1143
1144 /* Return the frame-relative coordinate of the left edge of display
1145 area AREA of window W. AREA < 0 means return the left edge of the
1146 whole window, to the right of the left fringe of W. */
1147
1148 INLINE int
1149 window_box_left (w, area)
1150 struct window *w;
1151 int area;
1152 {
1153 struct frame *f = XFRAME (w->frame);
1154 int x;
1155
1156 if (w->pseudo_window_p)
1157 return FRAME_INTERNAL_BORDER_WIDTH (f);
1158
1159 x = (WINDOW_LEFT_EDGE_X (w)
1160 + window_box_left_offset (w, area));
1161
1162 return x;
1163 }
1164
1165
1166 /* Return the frame-relative coordinate of the right edge of display
1167 area AREA of window W. AREA < 0 means return the left edge of the
1168 whole window, to the left of the right fringe of W. */
1169
1170 INLINE int
1171 window_box_right (w, area)
1172 struct window *w;
1173 int area;
1174 {
1175 return window_box_left (w, area) + window_box_width (w, area);
1176 }
1177
1178 /* Get the bounding box of the display area AREA of window W, without
1179 mode lines, in frame-relative coordinates. AREA < 0 means the
1180 whole window, not including the left and right fringes of
1181 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1182 coordinates of the upper-left corner of the box. Return in
1183 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1184
1185 INLINE void
1186 window_box (w, area, box_x, box_y, box_width, box_height)
1187 struct window *w;
1188 int area;
1189 int *box_x, *box_y, *box_width, *box_height;
1190 {
1191 if (box_width)
1192 *box_width = window_box_width (w, area);
1193 if (box_height)
1194 *box_height = window_box_height (w);
1195 if (box_x)
1196 *box_x = window_box_left (w, area);
1197 if (box_y)
1198 {
1199 *box_y = WINDOW_TOP_EDGE_Y (w);
1200 if (WINDOW_WANTS_HEADER_LINE_P (w))
1201 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1202 }
1203 }
1204
1205
1206 /* Get the bounding box of the display area AREA of window W, without
1207 mode lines. AREA < 0 means the whole window, not including the
1208 left and right fringe of the window. Return in *TOP_LEFT_X
1209 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1210 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1211 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1212 box. */
1213
1214 INLINE void
1215 window_box_edges (w, area, top_left_x, top_left_y,
1216 bottom_right_x, bottom_right_y)
1217 struct window *w;
1218 int area;
1219 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1220 {
1221 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1222 bottom_right_y);
1223 *bottom_right_x += *top_left_x;
1224 *bottom_right_y += *top_left_y;
1225 }
1226
1227
1228 \f
1229 /***********************************************************************
1230 Utilities
1231 ***********************************************************************/
1232
1233 /* Return the bottom y-position of the line the iterator IT is in.
1234 This can modify IT's settings. */
1235
1236 int
1237 line_bottom_y (it)
1238 struct it *it;
1239 {
1240 int line_height = it->max_ascent + it->max_descent;
1241 int line_top_y = it->current_y;
1242
1243 if (line_height == 0)
1244 {
1245 if (last_height)
1246 line_height = last_height;
1247 else if (IT_CHARPOS (*it) < ZV)
1248 {
1249 move_it_by_lines (it, 1, 1);
1250 line_height = (it->max_ascent || it->max_descent
1251 ? it->max_ascent + it->max_descent
1252 : last_height);
1253 }
1254 else
1255 {
1256 struct glyph_row *row = it->glyph_row;
1257
1258 /* Use the default character height. */
1259 it->glyph_row = NULL;
1260 it->what = IT_CHARACTER;
1261 it->c = ' ';
1262 it->len = 1;
1263 PRODUCE_GLYPHS (it);
1264 line_height = it->ascent + it->descent;
1265 it->glyph_row = row;
1266 }
1267 }
1268
1269 return line_top_y + line_height;
1270 }
1271
1272
1273 /* Return 1 if position CHARPOS is visible in window W.
1274 CHARPOS < 0 means return info about WINDOW_END position.
1275 If visible, set *X and *Y to pixel coordinates of top left corner.
1276 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1277 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1278
1279 int
1280 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1281 struct window *w;
1282 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1283 {
1284 struct it it;
1285 struct text_pos top;
1286 int visible_p = 0;
1287 struct buffer *old_buffer = NULL;
1288
1289 if (noninteractive)
1290 return visible_p;
1291
1292 if (XBUFFER (w->buffer) != current_buffer)
1293 {
1294 old_buffer = current_buffer;
1295 set_buffer_internal_1 (XBUFFER (w->buffer));
1296 }
1297
1298 SET_TEXT_POS_FROM_MARKER (top, w->start);
1299
1300 /* Compute exact mode line heights. */
1301 if (WINDOW_WANTS_MODELINE_P (w))
1302 current_mode_line_height
1303 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1304 current_buffer->mode_line_format);
1305
1306 if (WINDOW_WANTS_HEADER_LINE_P (w))
1307 current_header_line_height
1308 = display_mode_line (w, HEADER_LINE_FACE_ID,
1309 current_buffer->header_line_format);
1310
1311 start_display (&it, w, top);
1312 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1313 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1314
1315 /* Note that we may overshoot because of invisible text. */
1316 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1317 {
1318 int top_x = it.current_x;
1319 int top_y = it.current_y;
1320 int bottom_y = (last_height = 0, line_bottom_y (&it));
1321 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1322
1323 if (top_y < window_top_y)
1324 visible_p = bottom_y > window_top_y;
1325 else if (top_y < it.last_visible_y)
1326 visible_p = 1;
1327 if (visible_p)
1328 {
1329 if (it.method == GET_FROM_BUFFER)
1330 {
1331 Lisp_Object window, prop;
1332
1333 XSETWINDOW (window, w);
1334 prop = Fget_char_property (make_number (it.position.charpos),
1335 Qinvisible, window);
1336
1337 /* If charpos coincides with invisible text covered with an
1338 ellipsis, use the first glyph of the ellipsis to compute
1339 the pixel positions. */
1340 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1341 {
1342 struct glyph_row *row = it.glyph_row;
1343 struct glyph *glyph = row->glyphs[TEXT_AREA];
1344 struct glyph *end = glyph + row->used[TEXT_AREA];
1345 int x = row->x;
1346
1347 for (; glyph < end && glyph->charpos < charpos; glyph++)
1348 x += glyph->pixel_width;
1349
1350 top_x = x;
1351 }
1352 }
1353
1354 *x = top_x;
1355 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1356 *rtop = max (0, window_top_y - top_y);
1357 *rbot = max (0, bottom_y - it.last_visible_y);
1358 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1359 - max (top_y, window_top_y)));
1360 *vpos = it.vpos;
1361 }
1362 }
1363 else
1364 {
1365 struct it it2;
1366
1367 it2 = it;
1368 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1369 move_it_by_lines (&it, 1, 0);
1370 if (charpos < IT_CHARPOS (it)
1371 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1372 {
1373 visible_p = 1;
1374 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1375 *x = it2.current_x;
1376 *y = it2.current_y + it2.max_ascent - it2.ascent;
1377 *rtop = max (0, -it2.current_y);
1378 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1379 - it.last_visible_y));
1380 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1381 it.last_visible_y)
1382 - max (it2.current_y,
1383 WINDOW_HEADER_LINE_HEIGHT (w))));
1384 *vpos = it2.vpos;
1385 }
1386 }
1387
1388 if (old_buffer)
1389 set_buffer_internal_1 (old_buffer);
1390
1391 current_header_line_height = current_mode_line_height = -1;
1392
1393 if (visible_p && XFASTINT (w->hscroll) > 0)
1394 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1395
1396 #if 0
1397 /* Debugging code. */
1398 if (visible_p)
1399 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1400 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1401 else
1402 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1403 #endif
1404
1405 return visible_p;
1406 }
1407
1408
1409 /* Return the next character from STR which is MAXLEN bytes long.
1410 Return in *LEN the length of the character. This is like
1411 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1412 we find one, we return a `?', but with the length of the invalid
1413 character. */
1414
1415 static INLINE int
1416 string_char_and_length (str, maxlen, len)
1417 const unsigned char *str;
1418 int maxlen, *len;
1419 {
1420 int c;
1421
1422 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1423 if (!CHAR_VALID_P (c, 1))
1424 /* We may not change the length here because other places in Emacs
1425 don't use this function, i.e. they silently accept invalid
1426 characters. */
1427 c = '?';
1428
1429 return c;
1430 }
1431
1432
1433
1434 /* Given a position POS containing a valid character and byte position
1435 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1436
1437 static struct text_pos
1438 string_pos_nchars_ahead (pos, string, nchars)
1439 struct text_pos pos;
1440 Lisp_Object string;
1441 int nchars;
1442 {
1443 xassert (STRINGP (string) && nchars >= 0);
1444
1445 if (STRING_MULTIBYTE (string))
1446 {
1447 int rest = SBYTES (string) - BYTEPOS (pos);
1448 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1449 int len;
1450
1451 while (nchars--)
1452 {
1453 string_char_and_length (p, rest, &len);
1454 p += len, rest -= len;
1455 xassert (rest >= 0);
1456 CHARPOS (pos) += 1;
1457 BYTEPOS (pos) += len;
1458 }
1459 }
1460 else
1461 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1462
1463 return pos;
1464 }
1465
1466
1467 /* Value is the text position, i.e. character and byte position,
1468 for character position CHARPOS in STRING. */
1469
1470 static INLINE struct text_pos
1471 string_pos (charpos, string)
1472 int charpos;
1473 Lisp_Object string;
1474 {
1475 struct text_pos pos;
1476 xassert (STRINGP (string));
1477 xassert (charpos >= 0);
1478 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1479 return pos;
1480 }
1481
1482
1483 /* Value is a text position, i.e. character and byte position, for
1484 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1485 means recognize multibyte characters. */
1486
1487 static struct text_pos
1488 c_string_pos (charpos, s, multibyte_p)
1489 int charpos;
1490 unsigned char *s;
1491 int multibyte_p;
1492 {
1493 struct text_pos pos;
1494
1495 xassert (s != NULL);
1496 xassert (charpos >= 0);
1497
1498 if (multibyte_p)
1499 {
1500 int rest = strlen (s), len;
1501
1502 SET_TEXT_POS (pos, 0, 0);
1503 while (charpos--)
1504 {
1505 string_char_and_length (s, rest, &len);
1506 s += len, rest -= len;
1507 xassert (rest >= 0);
1508 CHARPOS (pos) += 1;
1509 BYTEPOS (pos) += len;
1510 }
1511 }
1512 else
1513 SET_TEXT_POS (pos, charpos, charpos);
1514
1515 return pos;
1516 }
1517
1518
1519 /* Value is the number of characters in C string S. MULTIBYTE_P
1520 non-zero means recognize multibyte characters. */
1521
1522 static int
1523 number_of_chars (s, multibyte_p)
1524 unsigned char *s;
1525 int multibyte_p;
1526 {
1527 int nchars;
1528
1529 if (multibyte_p)
1530 {
1531 int rest = strlen (s), len;
1532 unsigned char *p = (unsigned char *) s;
1533
1534 for (nchars = 0; rest > 0; ++nchars)
1535 {
1536 string_char_and_length (p, rest, &len);
1537 rest -= len, p += len;
1538 }
1539 }
1540 else
1541 nchars = strlen (s);
1542
1543 return nchars;
1544 }
1545
1546
1547 /* Compute byte position NEWPOS->bytepos corresponding to
1548 NEWPOS->charpos. POS is a known position in string STRING.
1549 NEWPOS->charpos must be >= POS.charpos. */
1550
1551 static void
1552 compute_string_pos (newpos, pos, string)
1553 struct text_pos *newpos, pos;
1554 Lisp_Object string;
1555 {
1556 xassert (STRINGP (string));
1557 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1558
1559 if (STRING_MULTIBYTE (string))
1560 *newpos = string_pos_nchars_ahead (pos, string,
1561 CHARPOS (*newpos) - CHARPOS (pos));
1562 else
1563 BYTEPOS (*newpos) = CHARPOS (*newpos);
1564 }
1565
1566 /* EXPORT:
1567 Return an estimation of the pixel height of mode or top lines on
1568 frame F. FACE_ID specifies what line's height to estimate. */
1569
1570 int
1571 estimate_mode_line_height (f, face_id)
1572 struct frame *f;
1573 enum face_id face_id;
1574 {
1575 #ifdef HAVE_WINDOW_SYSTEM
1576 if (FRAME_WINDOW_P (f))
1577 {
1578 int height = FONT_HEIGHT (FRAME_FONT (f));
1579
1580 /* This function is called so early when Emacs starts that the face
1581 cache and mode line face are not yet initialized. */
1582 if (FRAME_FACE_CACHE (f))
1583 {
1584 struct face *face = FACE_FROM_ID (f, face_id);
1585 if (face)
1586 {
1587 if (face->font)
1588 height = FONT_HEIGHT (face->font);
1589 if (face->box_line_width > 0)
1590 height += 2 * face->box_line_width;
1591 }
1592 }
1593
1594 return height;
1595 }
1596 #endif
1597
1598 return 1;
1599 }
1600
1601 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1602 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1603 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1604 not force the value into range. */
1605
1606 void
1607 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1608 FRAME_PTR f;
1609 register int pix_x, pix_y;
1610 int *x, *y;
1611 NativeRectangle *bounds;
1612 int noclip;
1613 {
1614
1615 #ifdef HAVE_WINDOW_SYSTEM
1616 if (FRAME_WINDOW_P (f))
1617 {
1618 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1619 even for negative values. */
1620 if (pix_x < 0)
1621 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1622 if (pix_y < 0)
1623 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1624
1625 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1626 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1627
1628 if (bounds)
1629 STORE_NATIVE_RECT (*bounds,
1630 FRAME_COL_TO_PIXEL_X (f, pix_x),
1631 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1632 FRAME_COLUMN_WIDTH (f) - 1,
1633 FRAME_LINE_HEIGHT (f) - 1);
1634
1635 if (!noclip)
1636 {
1637 if (pix_x < 0)
1638 pix_x = 0;
1639 else if (pix_x > FRAME_TOTAL_COLS (f))
1640 pix_x = FRAME_TOTAL_COLS (f);
1641
1642 if (pix_y < 0)
1643 pix_y = 0;
1644 else if (pix_y > FRAME_LINES (f))
1645 pix_y = FRAME_LINES (f);
1646 }
1647 }
1648 #endif
1649
1650 *x = pix_x;
1651 *y = pix_y;
1652 }
1653
1654
1655 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1656 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1657 can't tell the positions because W's display is not up to date,
1658 return 0. */
1659
1660 int
1661 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1662 struct window *w;
1663 int hpos, vpos;
1664 int *frame_x, *frame_y;
1665 {
1666 #ifdef HAVE_WINDOW_SYSTEM
1667 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1668 {
1669 int success_p;
1670
1671 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1672 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1673
1674 if (display_completed)
1675 {
1676 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1677 struct glyph *glyph = row->glyphs[TEXT_AREA];
1678 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1679
1680 hpos = row->x;
1681 vpos = row->y;
1682 while (glyph < end)
1683 {
1684 hpos += glyph->pixel_width;
1685 ++glyph;
1686 }
1687
1688 /* If first glyph is partially visible, its first visible position is still 0. */
1689 if (hpos < 0)
1690 hpos = 0;
1691
1692 success_p = 1;
1693 }
1694 else
1695 {
1696 hpos = vpos = 0;
1697 success_p = 0;
1698 }
1699
1700 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1701 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1702 return success_p;
1703 }
1704 #endif
1705
1706 *frame_x = hpos;
1707 *frame_y = vpos;
1708 return 1;
1709 }
1710
1711
1712 #ifdef HAVE_WINDOW_SYSTEM
1713
1714 /* Find the glyph under window-relative coordinates X/Y in window W.
1715 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1716 strings. Return in *HPOS and *VPOS the row and column number of
1717 the glyph found. Return in *AREA the glyph area containing X.
1718 Value is a pointer to the glyph found or null if X/Y is not on
1719 text, or we can't tell because W's current matrix is not up to
1720 date. */
1721
1722 #ifndef HAVE_CARBON
1723 static
1724 #endif
1725 struct glyph *
1726 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1727 struct window *w;
1728 int x, y;
1729 int *hpos, *vpos, *dx, *dy, *area;
1730 {
1731 struct glyph *glyph, *end;
1732 struct glyph_row *row = NULL;
1733 int x0, i;
1734
1735 /* Find row containing Y. Give up if some row is not enabled. */
1736 for (i = 0; i < w->current_matrix->nrows; ++i)
1737 {
1738 row = MATRIX_ROW (w->current_matrix, i);
1739 if (!row->enabled_p)
1740 return NULL;
1741 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1742 break;
1743 }
1744
1745 *vpos = i;
1746 *hpos = 0;
1747
1748 /* Give up if Y is not in the window. */
1749 if (i == w->current_matrix->nrows)
1750 return NULL;
1751
1752 /* Get the glyph area containing X. */
1753 if (w->pseudo_window_p)
1754 {
1755 *area = TEXT_AREA;
1756 x0 = 0;
1757 }
1758 else
1759 {
1760 if (x < window_box_left_offset (w, TEXT_AREA))
1761 {
1762 *area = LEFT_MARGIN_AREA;
1763 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1764 }
1765 else if (x < window_box_right_offset (w, TEXT_AREA))
1766 {
1767 *area = TEXT_AREA;
1768 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1769 }
1770 else
1771 {
1772 *area = RIGHT_MARGIN_AREA;
1773 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1774 }
1775 }
1776
1777 /* Find glyph containing X. */
1778 glyph = row->glyphs[*area];
1779 end = glyph + row->used[*area];
1780 x -= x0;
1781 while (glyph < end && x >= glyph->pixel_width)
1782 {
1783 x -= glyph->pixel_width;
1784 ++glyph;
1785 }
1786
1787 if (glyph == end)
1788 return NULL;
1789
1790 if (dx)
1791 {
1792 *dx = x;
1793 *dy = y - (row->y + row->ascent - glyph->ascent);
1794 }
1795
1796 *hpos = glyph - row->glyphs[*area];
1797 return glyph;
1798 }
1799
1800
1801 /* EXPORT:
1802 Convert frame-relative x/y to coordinates relative to window W.
1803 Takes pseudo-windows into account. */
1804
1805 void
1806 frame_to_window_pixel_xy (w, x, y)
1807 struct window *w;
1808 int *x, *y;
1809 {
1810 if (w->pseudo_window_p)
1811 {
1812 /* A pseudo-window is always full-width, and starts at the
1813 left edge of the frame, plus a frame border. */
1814 struct frame *f = XFRAME (w->frame);
1815 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1816 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1817 }
1818 else
1819 {
1820 *x -= WINDOW_LEFT_EDGE_X (w);
1821 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1822 }
1823 }
1824
1825 /* EXPORT:
1826 Return in RECTS[] at most N clipping rectangles for glyph string S.
1827 Return the number of stored rectangles. */
1828
1829 int
1830 get_glyph_string_clip_rects (s, rects, n)
1831 struct glyph_string *s;
1832 NativeRectangle *rects;
1833 int n;
1834 {
1835 XRectangle r;
1836
1837 if (n <= 0)
1838 return 0;
1839
1840 if (s->row->full_width_p)
1841 {
1842 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1843 r.x = WINDOW_LEFT_EDGE_X (s->w);
1844 r.width = WINDOW_TOTAL_WIDTH (s->w);
1845
1846 /* Unless displaying a mode or menu bar line, which are always
1847 fully visible, clip to the visible part of the row. */
1848 if (s->w->pseudo_window_p)
1849 r.height = s->row->visible_height;
1850 else
1851 r.height = s->height;
1852 }
1853 else
1854 {
1855 /* This is a text line that may be partially visible. */
1856 r.x = window_box_left (s->w, s->area);
1857 r.width = window_box_width (s->w, s->area);
1858 r.height = s->row->visible_height;
1859 }
1860
1861 if (s->clip_head)
1862 if (r.x < s->clip_head->x)
1863 {
1864 if (r.width >= s->clip_head->x - r.x)
1865 r.width -= s->clip_head->x - r.x;
1866 else
1867 r.width = 0;
1868 r.x = s->clip_head->x;
1869 }
1870 if (s->clip_tail)
1871 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1872 {
1873 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1874 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1875 else
1876 r.width = 0;
1877 }
1878
1879 /* If S draws overlapping rows, it's sufficient to use the top and
1880 bottom of the window for clipping because this glyph string
1881 intentionally draws over other lines. */
1882 if (s->for_overlaps)
1883 {
1884 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1885 r.height = window_text_bottom_y (s->w) - r.y;
1886
1887 /* Alas, the above simple strategy does not work for the
1888 environments with anti-aliased text: if the same text is
1889 drawn onto the same place multiple times, it gets thicker.
1890 If the overlap we are processing is for the erased cursor, we
1891 take the intersection with the rectagle of the cursor. */
1892 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1893 {
1894 XRectangle rc, r_save = r;
1895
1896 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1897 rc.y = s->w->phys_cursor.y;
1898 rc.width = s->w->phys_cursor_width;
1899 rc.height = s->w->phys_cursor_height;
1900
1901 x_intersect_rectangles (&r_save, &rc, &r);
1902 }
1903 }
1904 else
1905 {
1906 /* Don't use S->y for clipping because it doesn't take partially
1907 visible lines into account. For example, it can be negative for
1908 partially visible lines at the top of a window. */
1909 if (!s->row->full_width_p
1910 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1911 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1912 else
1913 r.y = max (0, s->row->y);
1914
1915 /* If drawing a tool-bar window, draw it over the internal border
1916 at the top of the window. */
1917 if (WINDOWP (s->f->tool_bar_window)
1918 && s->w == XWINDOW (s->f->tool_bar_window))
1919 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1920 }
1921
1922 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1923
1924 /* If drawing the cursor, don't let glyph draw outside its
1925 advertised boundaries. Cleartype does this under some circumstances. */
1926 if (s->hl == DRAW_CURSOR)
1927 {
1928 struct glyph *glyph = s->first_glyph;
1929 int height, max_y;
1930
1931 if (s->x > r.x)
1932 {
1933 r.width -= s->x - r.x;
1934 r.x = s->x;
1935 }
1936 r.width = min (r.width, glyph->pixel_width);
1937
1938 /* If r.y is below window bottom, ensure that we still see a cursor. */
1939 height = min (glyph->ascent + glyph->descent,
1940 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1941 max_y = window_text_bottom_y (s->w) - height;
1942 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1943 if (s->ybase - glyph->ascent > max_y)
1944 {
1945 r.y = max_y;
1946 r.height = height;
1947 }
1948 else
1949 {
1950 /* Don't draw cursor glyph taller than our actual glyph. */
1951 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1952 if (height < r.height)
1953 {
1954 max_y = r.y + r.height;
1955 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1956 r.height = min (max_y - r.y, height);
1957 }
1958 }
1959 }
1960
1961 if (s->row->clip)
1962 {
1963 XRectangle r_save = r;
1964
1965 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1966 r.width = 0;
1967 }
1968
1969 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1970 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1971 {
1972 #ifdef CONVERT_FROM_XRECT
1973 CONVERT_FROM_XRECT (r, *rects);
1974 #else
1975 *rects = r;
1976 #endif
1977 return 1;
1978 }
1979 else
1980 {
1981 /* If we are processing overlapping and allowed to return
1982 multiple clipping rectangles, we exclude the row of the glyph
1983 string from the clipping rectangle. This is to avoid drawing
1984 the same text on the environment with anti-aliasing. */
1985 #ifdef CONVERT_FROM_XRECT
1986 XRectangle rs[2];
1987 #else
1988 XRectangle *rs = rects;
1989 #endif
1990 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1991
1992 if (s->for_overlaps & OVERLAPS_PRED)
1993 {
1994 rs[i] = r;
1995 if (r.y + r.height > row_y)
1996 {
1997 if (r.y < row_y)
1998 rs[i].height = row_y - r.y;
1999 else
2000 rs[i].height = 0;
2001 }
2002 i++;
2003 }
2004 if (s->for_overlaps & OVERLAPS_SUCC)
2005 {
2006 rs[i] = r;
2007 if (r.y < row_y + s->row->visible_height)
2008 {
2009 if (r.y + r.height > row_y + s->row->visible_height)
2010 {
2011 rs[i].y = row_y + s->row->visible_height;
2012 rs[i].height = r.y + r.height - rs[i].y;
2013 }
2014 else
2015 rs[i].height = 0;
2016 }
2017 i++;
2018 }
2019
2020 n = i;
2021 #ifdef CONVERT_FROM_XRECT
2022 for (i = 0; i < n; i++)
2023 CONVERT_FROM_XRECT (rs[i], rects[i]);
2024 #endif
2025 return n;
2026 }
2027 }
2028
2029 /* EXPORT:
2030 Return in *NR the clipping rectangle for glyph string S. */
2031
2032 void
2033 get_glyph_string_clip_rect (s, nr)
2034 struct glyph_string *s;
2035 NativeRectangle *nr;
2036 {
2037 get_glyph_string_clip_rects (s, nr, 1);
2038 }
2039
2040
2041 /* EXPORT:
2042 Return the position and height of the phys cursor in window W.
2043 Set w->phys_cursor_width to width of phys cursor.
2044 */
2045
2046 void
2047 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2048 struct window *w;
2049 struct glyph_row *row;
2050 struct glyph *glyph;
2051 int *xp, *yp, *heightp;
2052 {
2053 struct frame *f = XFRAME (WINDOW_FRAME (w));
2054 int x, y, wd, h, h0, y0;
2055
2056 /* Compute the width of the rectangle to draw. If on a stretch
2057 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2058 rectangle as wide as the glyph, but use a canonical character
2059 width instead. */
2060 wd = glyph->pixel_width - 1;
2061 #ifdef HAVE_NTGUI
2062 wd++; /* Why? */
2063 #endif
2064
2065 x = w->phys_cursor.x;
2066 if (x < 0)
2067 {
2068 wd += x;
2069 x = 0;
2070 }
2071
2072 if (glyph->type == STRETCH_GLYPH
2073 && !x_stretch_cursor_p)
2074 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2075 w->phys_cursor_width = wd;
2076
2077 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2078
2079 /* If y is below window bottom, ensure that we still see a cursor. */
2080 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2081
2082 h = max (h0, glyph->ascent + glyph->descent);
2083 h0 = min (h0, glyph->ascent + glyph->descent);
2084
2085 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2086 if (y < y0)
2087 {
2088 h = max (h - (y0 - y) + 1, h0);
2089 y = y0 - 1;
2090 }
2091 else
2092 {
2093 y0 = window_text_bottom_y (w) - h0;
2094 if (y > y0)
2095 {
2096 h += y - y0;
2097 y = y0;
2098 }
2099 }
2100
2101 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2102 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2103 *heightp = h;
2104 }
2105
2106 /*
2107 * Remember which glyph the mouse is over.
2108 */
2109
2110 void
2111 remember_mouse_glyph (f, gx, gy, rect)
2112 struct frame *f;
2113 int gx, gy;
2114 NativeRectangle *rect;
2115 {
2116 Lisp_Object window;
2117 struct window *w;
2118 struct glyph_row *r, *gr, *end_row;
2119 enum window_part part;
2120 enum glyph_row_area area;
2121 int x, y, width, height;
2122
2123 /* Try to determine frame pixel position and size of the glyph under
2124 frame pixel coordinates X/Y on frame F. */
2125
2126 if (!f->glyphs_initialized_p
2127 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2128 NILP (window)))
2129 {
2130 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2131 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2132 goto virtual_glyph;
2133 }
2134
2135 w = XWINDOW (window);
2136 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2137 height = WINDOW_FRAME_LINE_HEIGHT (w);
2138
2139 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2140 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2141
2142 if (w->pseudo_window_p)
2143 {
2144 area = TEXT_AREA;
2145 part = ON_MODE_LINE; /* Don't adjust margin. */
2146 goto text_glyph;
2147 }
2148
2149 switch (part)
2150 {
2151 case ON_LEFT_MARGIN:
2152 area = LEFT_MARGIN_AREA;
2153 goto text_glyph;
2154
2155 case ON_RIGHT_MARGIN:
2156 area = RIGHT_MARGIN_AREA;
2157 goto text_glyph;
2158
2159 case ON_HEADER_LINE:
2160 case ON_MODE_LINE:
2161 gr = (part == ON_HEADER_LINE
2162 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2163 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2164 gy = gr->y;
2165 area = TEXT_AREA;
2166 goto text_glyph_row_found;
2167
2168 case ON_TEXT:
2169 area = TEXT_AREA;
2170
2171 text_glyph:
2172 gr = 0; gy = 0;
2173 for (; r <= end_row && r->enabled_p; ++r)
2174 if (r->y + r->height > y)
2175 {
2176 gr = r; gy = r->y;
2177 break;
2178 }
2179
2180 text_glyph_row_found:
2181 if (gr && gy <= y)
2182 {
2183 struct glyph *g = gr->glyphs[area];
2184 struct glyph *end = g + gr->used[area];
2185
2186 height = gr->height;
2187 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2188 if (gx + g->pixel_width > x)
2189 break;
2190
2191 if (g < end)
2192 {
2193 if (g->type == IMAGE_GLYPH)
2194 {
2195 /* Don't remember when mouse is over image, as
2196 image may have hot-spots. */
2197 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2198 return;
2199 }
2200 width = g->pixel_width;
2201 }
2202 else
2203 {
2204 /* Use nominal char spacing at end of line. */
2205 x -= gx;
2206 gx += (x / width) * width;
2207 }
2208
2209 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2210 gx += window_box_left_offset (w, area);
2211 }
2212 else
2213 {
2214 /* Use nominal line height at end of window. */
2215 gx = (x / width) * width;
2216 y -= gy;
2217 gy += (y / height) * height;
2218 }
2219 break;
2220
2221 case ON_LEFT_FRINGE:
2222 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2223 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2224 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2225 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2226 goto row_glyph;
2227
2228 case ON_RIGHT_FRINGE:
2229 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2230 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2231 : window_box_right_offset (w, TEXT_AREA));
2232 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2233 goto row_glyph;
2234
2235 case ON_SCROLL_BAR:
2236 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2237 ? 0
2238 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2239 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2240 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2241 : 0)));
2242 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2243
2244 row_glyph:
2245 gr = 0, gy = 0;
2246 for (; r <= end_row && r->enabled_p; ++r)
2247 if (r->y + r->height > y)
2248 {
2249 gr = r; gy = r->y;
2250 break;
2251 }
2252
2253 if (gr && gy <= y)
2254 height = gr->height;
2255 else
2256 {
2257 /* Use nominal line height at end of window. */
2258 y -= gy;
2259 gy += (y / height) * height;
2260 }
2261 break;
2262
2263 default:
2264 ;
2265 virtual_glyph:
2266 /* If there is no glyph under the mouse, then we divide the screen
2267 into a grid of the smallest glyph in the frame, and use that
2268 as our "glyph". */
2269
2270 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2271 round down even for negative values. */
2272 if (gx < 0)
2273 gx -= width - 1;
2274 if (gy < 0)
2275 gy -= height - 1;
2276
2277 gx = (gx / width) * width;
2278 gy = (gy / height) * height;
2279
2280 goto store_rect;
2281 }
2282
2283 gx += WINDOW_LEFT_EDGE_X (w);
2284 gy += WINDOW_TOP_EDGE_Y (w);
2285
2286 store_rect:
2287 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2288
2289 /* Visible feedback for debugging. */
2290 #if 0
2291 #if HAVE_X_WINDOWS
2292 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2293 f->output_data.x->normal_gc,
2294 gx, gy, width, height);
2295 #endif
2296 #endif
2297 }
2298
2299
2300 #endif /* HAVE_WINDOW_SYSTEM */
2301
2302 \f
2303 /***********************************************************************
2304 Lisp form evaluation
2305 ***********************************************************************/
2306
2307 /* Error handler for safe_eval and safe_call. */
2308
2309 static Lisp_Object
2310 safe_eval_handler (arg)
2311 Lisp_Object arg;
2312 {
2313 add_to_log ("Error during redisplay: %s", arg, Qnil);
2314 return Qnil;
2315 }
2316
2317
2318 /* Evaluate SEXPR and return the result, or nil if something went
2319 wrong. Prevent redisplay during the evaluation. */
2320
2321 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2322 Return the result, or nil if something went wrong. Prevent
2323 redisplay during the evaluation. */
2324
2325 Lisp_Object
2326 safe_call (nargs, args)
2327 int nargs;
2328 Lisp_Object *args;
2329 {
2330 Lisp_Object val;
2331
2332 if (inhibit_eval_during_redisplay)
2333 val = Qnil;
2334 else
2335 {
2336 int count = SPECPDL_INDEX ();
2337 struct gcpro gcpro1;
2338
2339 GCPRO1 (args[0]);
2340 gcpro1.nvars = nargs;
2341 specbind (Qinhibit_redisplay, Qt);
2342 /* Use Qt to ensure debugger does not run,
2343 so there is no possibility of wanting to redisplay. */
2344 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2345 safe_eval_handler);
2346 UNGCPRO;
2347 val = unbind_to (count, val);
2348 }
2349
2350 return val;
2351 }
2352
2353
2354 /* Call function FN with one argument ARG.
2355 Return the result, or nil if something went wrong. */
2356
2357 Lisp_Object
2358 safe_call1 (fn, arg)
2359 Lisp_Object fn, arg;
2360 {
2361 Lisp_Object args[2];
2362 args[0] = fn;
2363 args[1] = arg;
2364 return safe_call (2, args);
2365 }
2366
2367 static Lisp_Object Qeval;
2368
2369 Lisp_Object
2370 safe_eval (Lisp_Object sexpr)
2371 {
2372 return safe_call1 (Qeval, sexpr);
2373 }
2374
2375 /* Call function FN with one argument ARG.
2376 Return the result, or nil if something went wrong. */
2377
2378 Lisp_Object
2379 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2380 {
2381 Lisp_Object args[3];
2382 args[0] = fn;
2383 args[1] = arg1;
2384 args[2] = arg2;
2385 return safe_call (3, args);
2386 }
2387
2388
2389 \f
2390 /***********************************************************************
2391 Debugging
2392 ***********************************************************************/
2393
2394 #if 0
2395
2396 /* Define CHECK_IT to perform sanity checks on iterators.
2397 This is for debugging. It is too slow to do unconditionally. */
2398
2399 static void
2400 check_it (it)
2401 struct it *it;
2402 {
2403 if (it->method == GET_FROM_STRING)
2404 {
2405 xassert (STRINGP (it->string));
2406 xassert (IT_STRING_CHARPOS (*it) >= 0);
2407 }
2408 else
2409 {
2410 xassert (IT_STRING_CHARPOS (*it) < 0);
2411 if (it->method == GET_FROM_BUFFER)
2412 {
2413 /* Check that character and byte positions agree. */
2414 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2415 }
2416 }
2417
2418 if (it->dpvec)
2419 xassert (it->current.dpvec_index >= 0);
2420 else
2421 xassert (it->current.dpvec_index < 0);
2422 }
2423
2424 #define CHECK_IT(IT) check_it ((IT))
2425
2426 #else /* not 0 */
2427
2428 #define CHECK_IT(IT) (void) 0
2429
2430 #endif /* not 0 */
2431
2432
2433 #if GLYPH_DEBUG
2434
2435 /* Check that the window end of window W is what we expect it
2436 to be---the last row in the current matrix displaying text. */
2437
2438 static void
2439 check_window_end (w)
2440 struct window *w;
2441 {
2442 if (!MINI_WINDOW_P (w)
2443 && !NILP (w->window_end_valid))
2444 {
2445 struct glyph_row *row;
2446 xassert ((row = MATRIX_ROW (w->current_matrix,
2447 XFASTINT (w->window_end_vpos)),
2448 !row->enabled_p
2449 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2450 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2451 }
2452 }
2453
2454 #define CHECK_WINDOW_END(W) check_window_end ((W))
2455
2456 #else /* not GLYPH_DEBUG */
2457
2458 #define CHECK_WINDOW_END(W) (void) 0
2459
2460 #endif /* not GLYPH_DEBUG */
2461
2462
2463 \f
2464 /***********************************************************************
2465 Iterator initialization
2466 ***********************************************************************/
2467
2468 /* Initialize IT for displaying current_buffer in window W, starting
2469 at character position CHARPOS. CHARPOS < 0 means that no buffer
2470 position is specified which is useful when the iterator is assigned
2471 a position later. BYTEPOS is the byte position corresponding to
2472 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2473
2474 If ROW is not null, calls to produce_glyphs with IT as parameter
2475 will produce glyphs in that row.
2476
2477 BASE_FACE_ID is the id of a base face to use. It must be one of
2478 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2479 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2480 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2481
2482 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2483 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2484 will be initialized to use the corresponding mode line glyph row of
2485 the desired matrix of W. */
2486
2487 void
2488 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2489 struct it *it;
2490 struct window *w;
2491 int charpos, bytepos;
2492 struct glyph_row *row;
2493 enum face_id base_face_id;
2494 {
2495 int highlight_region_p;
2496
2497 /* Some precondition checks. */
2498 xassert (w != NULL && it != NULL);
2499 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2500 && charpos <= ZV));
2501
2502 /* If face attributes have been changed since the last redisplay,
2503 free realized faces now because they depend on face definitions
2504 that might have changed. Don't free faces while there might be
2505 desired matrices pending which reference these faces. */
2506 if (face_change_count && !inhibit_free_realized_faces)
2507 {
2508 face_change_count = 0;
2509 free_all_realized_faces (Qnil);
2510 }
2511
2512 /* Use one of the mode line rows of W's desired matrix if
2513 appropriate. */
2514 if (row == NULL)
2515 {
2516 if (base_face_id == MODE_LINE_FACE_ID
2517 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2518 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2519 else if (base_face_id == HEADER_LINE_FACE_ID)
2520 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2521 }
2522
2523 /* Clear IT. */
2524 bzero (it, sizeof *it);
2525 it->current.overlay_string_index = -1;
2526 it->current.dpvec_index = -1;
2527 it->base_face_id = base_face_id;
2528 it->string = Qnil;
2529 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2530
2531 /* The window in which we iterate over current_buffer: */
2532 XSETWINDOW (it->window, w);
2533 it->w = w;
2534 it->f = XFRAME (w->frame);
2535
2536 /* Extra space between lines (on window systems only). */
2537 if (base_face_id == DEFAULT_FACE_ID
2538 && FRAME_WINDOW_P (it->f))
2539 {
2540 if (NATNUMP (current_buffer->extra_line_spacing))
2541 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2542 else if (FLOATP (current_buffer->extra_line_spacing))
2543 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2544 * FRAME_LINE_HEIGHT (it->f));
2545 else if (it->f->extra_line_spacing > 0)
2546 it->extra_line_spacing = it->f->extra_line_spacing;
2547 it->max_extra_line_spacing = 0;
2548 }
2549
2550 /* If realized faces have been removed, e.g. because of face
2551 attribute changes of named faces, recompute them. When running
2552 in batch mode, the face cache of the initial frame is null. If
2553 we happen to get called, make a dummy face cache. */
2554 if (FRAME_FACE_CACHE (it->f) == NULL)
2555 init_frame_faces (it->f);
2556 if (FRAME_FACE_CACHE (it->f)->used == 0)
2557 recompute_basic_faces (it->f);
2558
2559 /* Current value of the `slice', `space-width', and 'height' properties. */
2560 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2561 it->space_width = Qnil;
2562 it->font_height = Qnil;
2563 it->override_ascent = -1;
2564
2565 /* Are control characters displayed as `^C'? */
2566 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2567
2568 /* -1 means everything between a CR and the following line end
2569 is invisible. >0 means lines indented more than this value are
2570 invisible. */
2571 it->selective = (INTEGERP (current_buffer->selective_display)
2572 ? XFASTINT (current_buffer->selective_display)
2573 : (!NILP (current_buffer->selective_display)
2574 ? -1 : 0));
2575 it->selective_display_ellipsis_p
2576 = !NILP (current_buffer->selective_display_ellipses);
2577
2578 /* Display table to use. */
2579 it->dp = window_display_table (w);
2580
2581 /* Are multibyte characters enabled in current_buffer? */
2582 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2583
2584 /* Non-zero if we should highlight the region. */
2585 highlight_region_p
2586 = (!NILP (Vtransient_mark_mode)
2587 && !NILP (current_buffer->mark_active)
2588 && XMARKER (current_buffer->mark)->buffer != 0);
2589
2590 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2591 start and end of a visible region in window IT->w. Set both to
2592 -1 to indicate no region. */
2593 if (highlight_region_p
2594 /* Maybe highlight only in selected window. */
2595 && (/* Either show region everywhere. */
2596 highlight_nonselected_windows
2597 /* Or show region in the selected window. */
2598 || w == XWINDOW (selected_window)
2599 /* Or show the region if we are in the mini-buffer and W is
2600 the window the mini-buffer refers to. */
2601 || (MINI_WINDOW_P (XWINDOW (selected_window))
2602 && WINDOWP (minibuf_selected_window)
2603 && w == XWINDOW (minibuf_selected_window))))
2604 {
2605 int charpos = marker_position (current_buffer->mark);
2606 it->region_beg_charpos = min (PT, charpos);
2607 it->region_end_charpos = max (PT, charpos);
2608 }
2609 else
2610 it->region_beg_charpos = it->region_end_charpos = -1;
2611
2612 /* Get the position at which the redisplay_end_trigger hook should
2613 be run, if it is to be run at all. */
2614 if (MARKERP (w->redisplay_end_trigger)
2615 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2616 it->redisplay_end_trigger_charpos
2617 = marker_position (w->redisplay_end_trigger);
2618 else if (INTEGERP (w->redisplay_end_trigger))
2619 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2620
2621 /* Correct bogus values of tab_width. */
2622 it->tab_width = XINT (current_buffer->tab_width);
2623 if (it->tab_width <= 0 || it->tab_width > 1000)
2624 it->tab_width = 8;
2625
2626 /* Are lines in the display truncated? */
2627 it->truncate_lines_p
2628 = (base_face_id != DEFAULT_FACE_ID
2629 || XINT (it->w->hscroll)
2630 || (truncate_partial_width_windows
2631 && !WINDOW_FULL_WIDTH_P (it->w))
2632 || !NILP (current_buffer->truncate_lines));
2633
2634 /* Get dimensions of truncation and continuation glyphs. These are
2635 displayed as fringe bitmaps under X, so we don't need them for such
2636 frames. */
2637 if (!FRAME_WINDOW_P (it->f))
2638 {
2639 if (it->truncate_lines_p)
2640 {
2641 /* We will need the truncation glyph. */
2642 xassert (it->glyph_row == NULL);
2643 produce_special_glyphs (it, IT_TRUNCATION);
2644 it->truncation_pixel_width = it->pixel_width;
2645 }
2646 else
2647 {
2648 /* We will need the continuation glyph. */
2649 xassert (it->glyph_row == NULL);
2650 produce_special_glyphs (it, IT_CONTINUATION);
2651 it->continuation_pixel_width = it->pixel_width;
2652 }
2653
2654 /* Reset these values to zero because the produce_special_glyphs
2655 above has changed them. */
2656 it->pixel_width = it->ascent = it->descent = 0;
2657 it->phys_ascent = it->phys_descent = 0;
2658 }
2659
2660 /* Set this after getting the dimensions of truncation and
2661 continuation glyphs, so that we don't produce glyphs when calling
2662 produce_special_glyphs, above. */
2663 it->glyph_row = row;
2664 it->area = TEXT_AREA;
2665
2666 /* Get the dimensions of the display area. The display area
2667 consists of the visible window area plus a horizontally scrolled
2668 part to the left of the window. All x-values are relative to the
2669 start of this total display area. */
2670 if (base_face_id != DEFAULT_FACE_ID)
2671 {
2672 /* Mode lines, menu bar in terminal frames. */
2673 it->first_visible_x = 0;
2674 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2675 }
2676 else
2677 {
2678 it->first_visible_x
2679 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2680 it->last_visible_x = (it->first_visible_x
2681 + window_box_width (w, TEXT_AREA));
2682
2683 /* If we truncate lines, leave room for the truncator glyph(s) at
2684 the right margin. Otherwise, leave room for the continuation
2685 glyph(s). Truncation and continuation glyphs are not inserted
2686 for window-based redisplay. */
2687 if (!FRAME_WINDOW_P (it->f))
2688 {
2689 if (it->truncate_lines_p)
2690 it->last_visible_x -= it->truncation_pixel_width;
2691 else
2692 it->last_visible_x -= it->continuation_pixel_width;
2693 }
2694
2695 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2696 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2697 }
2698
2699 /* Leave room for a border glyph. */
2700 if (!FRAME_WINDOW_P (it->f)
2701 && !WINDOW_RIGHTMOST_P (it->w))
2702 it->last_visible_x -= 1;
2703
2704 it->last_visible_y = window_text_bottom_y (w);
2705
2706 /* For mode lines and alike, arrange for the first glyph having a
2707 left box line if the face specifies a box. */
2708 if (base_face_id != DEFAULT_FACE_ID)
2709 {
2710 struct face *face;
2711
2712 it->face_id = base_face_id;
2713
2714 /* If we have a boxed mode line, make the first character appear
2715 with a left box line. */
2716 face = FACE_FROM_ID (it->f, base_face_id);
2717 if (face->box != FACE_NO_BOX)
2718 it->start_of_box_run_p = 1;
2719 }
2720
2721 /* If a buffer position was specified, set the iterator there,
2722 getting overlays and face properties from that position. */
2723 if (charpos >= BUF_BEG (current_buffer))
2724 {
2725 it->end_charpos = ZV;
2726 it->face_id = -1;
2727 IT_CHARPOS (*it) = charpos;
2728
2729 /* Compute byte position if not specified. */
2730 if (bytepos < charpos)
2731 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2732 else
2733 IT_BYTEPOS (*it) = bytepos;
2734
2735 it->start = it->current;
2736
2737 /* Compute faces etc. */
2738 reseat (it, it->current.pos, 1);
2739 }
2740
2741 CHECK_IT (it);
2742 }
2743
2744
2745 /* Initialize IT for the display of window W with window start POS. */
2746
2747 void
2748 start_display (it, w, pos)
2749 struct it *it;
2750 struct window *w;
2751 struct text_pos pos;
2752 {
2753 struct glyph_row *row;
2754 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2755
2756 row = w->desired_matrix->rows + first_vpos;
2757 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2758 it->first_vpos = first_vpos;
2759
2760 /* Don't reseat to previous visible line start if current start
2761 position is in a string or image. */
2762 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2763 {
2764 int start_at_line_beg_p;
2765 int first_y = it->current_y;
2766
2767 /* If window start is not at a line start, skip forward to POS to
2768 get the correct continuation lines width. */
2769 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2770 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2771 if (!start_at_line_beg_p)
2772 {
2773 int new_x;
2774
2775 reseat_at_previous_visible_line_start (it);
2776 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2777
2778 new_x = it->current_x + it->pixel_width;
2779
2780 /* If lines are continued, this line may end in the middle
2781 of a multi-glyph character (e.g. a control character
2782 displayed as \003, or in the middle of an overlay
2783 string). In this case move_it_to above will not have
2784 taken us to the start of the continuation line but to the
2785 end of the continued line. */
2786 if (it->current_x > 0
2787 && !it->truncate_lines_p /* Lines are continued. */
2788 && (/* And glyph doesn't fit on the line. */
2789 new_x > it->last_visible_x
2790 /* Or it fits exactly and we're on a window
2791 system frame. */
2792 || (new_x == it->last_visible_x
2793 && FRAME_WINDOW_P (it->f))))
2794 {
2795 if (it->current.dpvec_index >= 0
2796 || it->current.overlay_string_index >= 0)
2797 {
2798 set_iterator_to_next (it, 1);
2799 move_it_in_display_line_to (it, -1, -1, 0);
2800 }
2801
2802 it->continuation_lines_width += it->current_x;
2803 }
2804
2805 /* We're starting a new display line, not affected by the
2806 height of the continued line, so clear the appropriate
2807 fields in the iterator structure. */
2808 it->max_ascent = it->max_descent = 0;
2809 it->max_phys_ascent = it->max_phys_descent = 0;
2810
2811 it->current_y = first_y;
2812 it->vpos = 0;
2813 it->current_x = it->hpos = 0;
2814 }
2815 }
2816
2817 #if 0 /* Don't assert the following because start_display is sometimes
2818 called intentionally with a window start that is not at a
2819 line start. Please leave this code in as a comment. */
2820
2821 /* Window start should be on a line start, now. */
2822 xassert (it->continuation_lines_width
2823 || IT_CHARPOS (it) == BEGV
2824 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2825 #endif /* 0 */
2826 }
2827
2828
2829 /* Return 1 if POS is a position in ellipses displayed for invisible
2830 text. W is the window we display, for text property lookup. */
2831
2832 static int
2833 in_ellipses_for_invisible_text_p (pos, w)
2834 struct display_pos *pos;
2835 struct window *w;
2836 {
2837 Lisp_Object prop, window;
2838 int ellipses_p = 0;
2839 int charpos = CHARPOS (pos->pos);
2840
2841 /* If POS specifies a position in a display vector, this might
2842 be for an ellipsis displayed for invisible text. We won't
2843 get the iterator set up for delivering that ellipsis unless
2844 we make sure that it gets aware of the invisible text. */
2845 if (pos->dpvec_index >= 0
2846 && pos->overlay_string_index < 0
2847 && CHARPOS (pos->string_pos) < 0
2848 && charpos > BEGV
2849 && (XSETWINDOW (window, w),
2850 prop = Fget_char_property (make_number (charpos),
2851 Qinvisible, window),
2852 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2853 {
2854 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2855 window);
2856 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2857 }
2858
2859 return ellipses_p;
2860 }
2861
2862
2863 /* Initialize IT for stepping through current_buffer in window W,
2864 starting at position POS that includes overlay string and display
2865 vector/ control character translation position information. Value
2866 is zero if there are overlay strings with newlines at POS. */
2867
2868 static int
2869 init_from_display_pos (it, w, pos)
2870 struct it *it;
2871 struct window *w;
2872 struct display_pos *pos;
2873 {
2874 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2875 int i, overlay_strings_with_newlines = 0;
2876
2877 /* If POS specifies a position in a display vector, this might
2878 be for an ellipsis displayed for invisible text. We won't
2879 get the iterator set up for delivering that ellipsis unless
2880 we make sure that it gets aware of the invisible text. */
2881 if (in_ellipses_for_invisible_text_p (pos, w))
2882 {
2883 --charpos;
2884 bytepos = 0;
2885 }
2886
2887 /* Keep in mind: the call to reseat in init_iterator skips invisible
2888 text, so we might end up at a position different from POS. This
2889 is only a problem when POS is a row start after a newline and an
2890 overlay starts there with an after-string, and the overlay has an
2891 invisible property. Since we don't skip invisible text in
2892 display_line and elsewhere immediately after consuming the
2893 newline before the row start, such a POS will not be in a string,
2894 but the call to init_iterator below will move us to the
2895 after-string. */
2896 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2897
2898 /* This only scans the current chunk -- it should scan all chunks.
2899 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2900 to 16 in 22.1 to make this a lesser problem. */
2901 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2902 {
2903 const char *s = SDATA (it->overlay_strings[i]);
2904 const char *e = s + SBYTES (it->overlay_strings[i]);
2905
2906 while (s < e && *s != '\n')
2907 ++s;
2908
2909 if (s < e)
2910 {
2911 overlay_strings_with_newlines = 1;
2912 break;
2913 }
2914 }
2915
2916 /* If position is within an overlay string, set up IT to the right
2917 overlay string. */
2918 if (pos->overlay_string_index >= 0)
2919 {
2920 int relative_index;
2921
2922 /* If the first overlay string happens to have a `display'
2923 property for an image, the iterator will be set up for that
2924 image, and we have to undo that setup first before we can
2925 correct the overlay string index. */
2926 if (it->method == GET_FROM_IMAGE)
2927 pop_it (it);
2928
2929 /* We already have the first chunk of overlay strings in
2930 IT->overlay_strings. Load more until the one for
2931 pos->overlay_string_index is in IT->overlay_strings. */
2932 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2933 {
2934 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2935 it->current.overlay_string_index = 0;
2936 while (n--)
2937 {
2938 load_overlay_strings (it, 0);
2939 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2940 }
2941 }
2942
2943 it->current.overlay_string_index = pos->overlay_string_index;
2944 relative_index = (it->current.overlay_string_index
2945 % OVERLAY_STRING_CHUNK_SIZE);
2946 it->string = it->overlay_strings[relative_index];
2947 xassert (STRINGP (it->string));
2948 it->current.string_pos = pos->string_pos;
2949 it->method = GET_FROM_STRING;
2950 }
2951
2952 #if 0 /* This is bogus because POS not having an overlay string
2953 position does not mean it's after the string. Example: A
2954 line starting with a before-string and initialization of IT
2955 to the previous row's end position. */
2956 else if (it->current.overlay_string_index >= 0)
2957 {
2958 /* If POS says we're already after an overlay string ending at
2959 POS, make sure to pop the iterator because it will be in
2960 front of that overlay string. When POS is ZV, we've thereby
2961 also ``processed'' overlay strings at ZV. */
2962 while (it->sp)
2963 pop_it (it);
2964 xassert (it->current.overlay_string_index == -1);
2965 xassert (it->method == GET_FROM_BUFFER);
2966 if (CHARPOS (pos->pos) == ZV)
2967 it->overlay_strings_at_end_processed_p = 1;
2968 }
2969 #endif /* 0 */
2970
2971 if (CHARPOS (pos->string_pos) >= 0)
2972 {
2973 /* Recorded position is not in an overlay string, but in another
2974 string. This can only be a string from a `display' property.
2975 IT should already be filled with that string. */
2976 it->current.string_pos = pos->string_pos;
2977 xassert (STRINGP (it->string));
2978 }
2979
2980 /* Restore position in display vector translations, control
2981 character translations or ellipses. */
2982 if (pos->dpvec_index >= 0)
2983 {
2984 if (it->dpvec == NULL)
2985 get_next_display_element (it);
2986 xassert (it->dpvec && it->current.dpvec_index == 0);
2987 it->current.dpvec_index = pos->dpvec_index;
2988 }
2989
2990 CHECK_IT (it);
2991 return !overlay_strings_with_newlines;
2992 }
2993
2994
2995 /* Initialize IT for stepping through current_buffer in window W
2996 starting at ROW->start. */
2997
2998 static void
2999 init_to_row_start (it, w, row)
3000 struct it *it;
3001 struct window *w;
3002 struct glyph_row *row;
3003 {
3004 init_from_display_pos (it, w, &row->start);
3005 it->start = row->start;
3006 it->continuation_lines_width = row->continuation_lines_width;
3007 CHECK_IT (it);
3008 }
3009
3010
3011 /* Initialize IT for stepping through current_buffer in window W
3012 starting in the line following ROW, i.e. starting at ROW->end.
3013 Value is zero if there are overlay strings with newlines at ROW's
3014 end position. */
3015
3016 static int
3017 init_to_row_end (it, w, row)
3018 struct it *it;
3019 struct window *w;
3020 struct glyph_row *row;
3021 {
3022 int success = 0;
3023
3024 if (init_from_display_pos (it, w, &row->end))
3025 {
3026 if (row->continued_p)
3027 it->continuation_lines_width
3028 = row->continuation_lines_width + row->pixel_width;
3029 CHECK_IT (it);
3030 success = 1;
3031 }
3032
3033 return success;
3034 }
3035
3036
3037
3038 \f
3039 /***********************************************************************
3040 Text properties
3041 ***********************************************************************/
3042
3043 /* Called when IT reaches IT->stop_charpos. Handle text property and
3044 overlay changes. Set IT->stop_charpos to the next position where
3045 to stop. */
3046
3047 static void
3048 handle_stop (it)
3049 struct it *it;
3050 {
3051 enum prop_handled handled;
3052 int handle_overlay_change_p;
3053 struct props *p;
3054
3055 it->dpvec = NULL;
3056 it->current.dpvec_index = -1;
3057 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3058 it->ignore_overlay_strings_at_pos_p = 0;
3059
3060 /* Use face of preceding text for ellipsis (if invisible) */
3061 if (it->selective_display_ellipsis_p)
3062 it->saved_face_id = it->face_id;
3063
3064 do
3065 {
3066 handled = HANDLED_NORMALLY;
3067
3068 /* Call text property handlers. */
3069 for (p = it_props; p->handler; ++p)
3070 {
3071 handled = p->handler (it);
3072
3073 if (handled == HANDLED_RECOMPUTE_PROPS)
3074 break;
3075 else if (handled == HANDLED_RETURN)
3076 {
3077 /* We still want to show before and after strings from
3078 overlays even if the actual buffer text is replaced. */
3079 if (!handle_overlay_change_p || it->sp > 1)
3080 return;
3081 if (!get_overlay_strings_1 (it, 0, 0))
3082 return;
3083 it->ignore_overlay_strings_at_pos_p = 1;
3084 it->string_from_display_prop_p = 0;
3085 handle_overlay_change_p = 0;
3086 handled = HANDLED_RECOMPUTE_PROPS;
3087 break;
3088 }
3089 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3090 handle_overlay_change_p = 0;
3091 }
3092
3093 if (handled != HANDLED_RECOMPUTE_PROPS)
3094 {
3095 /* Don't check for overlay strings below when set to deliver
3096 characters from a display vector. */
3097 if (it->method == GET_FROM_DISPLAY_VECTOR)
3098 handle_overlay_change_p = 0;
3099
3100 /* Handle overlay changes.
3101 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3102 if it finds overlays. */
3103 if (handle_overlay_change_p)
3104 handled = handle_overlay_change (it);
3105 }
3106 }
3107 while (handled == HANDLED_RECOMPUTE_PROPS);
3108
3109 /* Determine where to stop next. */
3110 if (handled == HANDLED_NORMALLY)
3111 compute_stop_pos (it);
3112 }
3113
3114
3115 /* Compute IT->stop_charpos from text property and overlay change
3116 information for IT's current position. */
3117
3118 static void
3119 compute_stop_pos (it)
3120 struct it *it;
3121 {
3122 register INTERVAL iv, next_iv;
3123 Lisp_Object object, limit, position;
3124
3125 /* If nowhere else, stop at the end. */
3126 it->stop_charpos = it->end_charpos;
3127
3128 if (STRINGP (it->string))
3129 {
3130 /* Strings are usually short, so don't limit the search for
3131 properties. */
3132 object = it->string;
3133 limit = Qnil;
3134 position = make_number (IT_STRING_CHARPOS (*it));
3135 }
3136 else
3137 {
3138 int charpos;
3139
3140 /* If next overlay change is in front of the current stop pos
3141 (which is IT->end_charpos), stop there. Note: value of
3142 next_overlay_change is point-max if no overlay change
3143 follows. */
3144 charpos = next_overlay_change (IT_CHARPOS (*it));
3145 if (charpos < it->stop_charpos)
3146 it->stop_charpos = charpos;
3147
3148 /* If showing the region, we have to stop at the region
3149 start or end because the face might change there. */
3150 if (it->region_beg_charpos > 0)
3151 {
3152 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3153 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3154 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3155 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3156 }
3157
3158 /* Set up variables for computing the stop position from text
3159 property changes. */
3160 XSETBUFFER (object, current_buffer);
3161 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3162 position = make_number (IT_CHARPOS (*it));
3163
3164 }
3165
3166 /* Get the interval containing IT's position. Value is a null
3167 interval if there isn't such an interval. */
3168 iv = validate_interval_range (object, &position, &position, 0);
3169 if (!NULL_INTERVAL_P (iv))
3170 {
3171 Lisp_Object values_here[LAST_PROP_IDX];
3172 struct props *p;
3173
3174 /* Get properties here. */
3175 for (p = it_props; p->handler; ++p)
3176 values_here[p->idx] = textget (iv->plist, *p->name);
3177
3178 /* Look for an interval following iv that has different
3179 properties. */
3180 for (next_iv = next_interval (iv);
3181 (!NULL_INTERVAL_P (next_iv)
3182 && (NILP (limit)
3183 || XFASTINT (limit) > next_iv->position));
3184 next_iv = next_interval (next_iv))
3185 {
3186 for (p = it_props; p->handler; ++p)
3187 {
3188 Lisp_Object new_value;
3189
3190 new_value = textget (next_iv->plist, *p->name);
3191 if (!EQ (values_here[p->idx], new_value))
3192 break;
3193 }
3194
3195 if (p->handler)
3196 break;
3197 }
3198
3199 if (!NULL_INTERVAL_P (next_iv))
3200 {
3201 if (INTEGERP (limit)
3202 && next_iv->position >= XFASTINT (limit))
3203 /* No text property change up to limit. */
3204 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3205 else
3206 /* Text properties change in next_iv. */
3207 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3208 }
3209 }
3210
3211 xassert (STRINGP (it->string)
3212 || (it->stop_charpos >= BEGV
3213 && it->stop_charpos >= IT_CHARPOS (*it)));
3214 }
3215
3216
3217 /* Return the position of the next overlay change after POS in
3218 current_buffer. Value is point-max if no overlay change
3219 follows. This is like `next-overlay-change' but doesn't use
3220 xmalloc. */
3221
3222 static EMACS_INT
3223 next_overlay_change (pos)
3224 EMACS_INT pos;
3225 {
3226 int noverlays;
3227 EMACS_INT endpos;
3228 Lisp_Object *overlays;
3229 int i;
3230
3231 /* Get all overlays at the given position. */
3232 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3233
3234 /* If any of these overlays ends before endpos,
3235 use its ending point instead. */
3236 for (i = 0; i < noverlays; ++i)
3237 {
3238 Lisp_Object oend;
3239 EMACS_INT oendpos;
3240
3241 oend = OVERLAY_END (overlays[i]);
3242 oendpos = OVERLAY_POSITION (oend);
3243 endpos = min (endpos, oendpos);
3244 }
3245
3246 return endpos;
3247 }
3248
3249
3250 \f
3251 /***********************************************************************
3252 Fontification
3253 ***********************************************************************/
3254
3255 /* Handle changes in the `fontified' property of the current buffer by
3256 calling hook functions from Qfontification_functions to fontify
3257 regions of text. */
3258
3259 static enum prop_handled
3260 handle_fontified_prop (it)
3261 struct it *it;
3262 {
3263 Lisp_Object prop, pos;
3264 enum prop_handled handled = HANDLED_NORMALLY;
3265
3266 if (!NILP (Vmemory_full))
3267 return handled;
3268
3269 /* Get the value of the `fontified' property at IT's current buffer
3270 position. (The `fontified' property doesn't have a special
3271 meaning in strings.) If the value is nil, call functions from
3272 Qfontification_functions. */
3273 if (!STRINGP (it->string)
3274 && it->s == NULL
3275 && !NILP (Vfontification_functions)
3276 && !NILP (Vrun_hooks)
3277 && (pos = make_number (IT_CHARPOS (*it)),
3278 prop = Fget_char_property (pos, Qfontified, Qnil),
3279 /* Ignore the special cased nil value always present at EOB since
3280 no amount of fontifying will be able to change it. */
3281 NILP (prop) && IT_CHARPOS (*it) < Z))
3282 {
3283 int count = SPECPDL_INDEX ();
3284 Lisp_Object val;
3285
3286 val = Vfontification_functions;
3287 specbind (Qfontification_functions, Qnil);
3288
3289 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3290 safe_call1 (val, pos);
3291 else
3292 {
3293 Lisp_Object globals, fn;
3294 struct gcpro gcpro1, gcpro2;
3295
3296 globals = Qnil;
3297 GCPRO2 (val, globals);
3298
3299 for (; CONSP (val); val = XCDR (val))
3300 {
3301 fn = XCAR (val);
3302
3303 if (EQ (fn, Qt))
3304 {
3305 /* A value of t indicates this hook has a local
3306 binding; it means to run the global binding too.
3307 In a global value, t should not occur. If it
3308 does, we must ignore it to avoid an endless
3309 loop. */
3310 for (globals = Fdefault_value (Qfontification_functions);
3311 CONSP (globals);
3312 globals = XCDR (globals))
3313 {
3314 fn = XCAR (globals);
3315 if (!EQ (fn, Qt))
3316 safe_call1 (fn, pos);
3317 }
3318 }
3319 else
3320 safe_call1 (fn, pos);
3321 }
3322
3323 UNGCPRO;
3324 }
3325
3326 unbind_to (count, Qnil);
3327
3328 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3329 something. This avoids an endless loop if they failed to
3330 fontify the text for which reason ever. */
3331 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3332 handled = HANDLED_RECOMPUTE_PROPS;
3333 }
3334
3335 return handled;
3336 }
3337
3338
3339 \f
3340 /***********************************************************************
3341 Faces
3342 ***********************************************************************/
3343
3344 /* Set up iterator IT from face properties at its current position.
3345 Called from handle_stop. */
3346
3347 static enum prop_handled
3348 handle_face_prop (it)
3349 struct it *it;
3350 {
3351 int new_face_id;
3352 EMACS_INT next_stop;
3353
3354 if (!STRINGP (it->string))
3355 {
3356 new_face_id
3357 = face_at_buffer_position (it->w,
3358 IT_CHARPOS (*it),
3359 it->region_beg_charpos,
3360 it->region_end_charpos,
3361 &next_stop,
3362 (IT_CHARPOS (*it)
3363 + TEXT_PROP_DISTANCE_LIMIT),
3364 0);
3365
3366 /* Is this a start of a run of characters with box face?
3367 Caveat: this can be called for a freshly initialized
3368 iterator; face_id is -1 in this case. We know that the new
3369 face will not change until limit, i.e. if the new face has a
3370 box, all characters up to limit will have one. But, as
3371 usual, we don't know whether limit is really the end. */
3372 if (new_face_id != it->face_id)
3373 {
3374 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3375
3376 /* If new face has a box but old face has not, this is
3377 the start of a run of characters with box, i.e. it has
3378 a shadow on the left side. The value of face_id of the
3379 iterator will be -1 if this is the initial call that gets
3380 the face. In this case, we have to look in front of IT's
3381 position and see whether there is a face != new_face_id. */
3382 it->start_of_box_run_p
3383 = (new_face->box != FACE_NO_BOX
3384 && (it->face_id >= 0
3385 || IT_CHARPOS (*it) == BEG
3386 || new_face_id != face_before_it_pos (it)));
3387 it->face_box_p = new_face->box != FACE_NO_BOX;
3388 }
3389 }
3390 else
3391 {
3392 int base_face_id, bufpos;
3393 int i;
3394 Lisp_Object from_overlay
3395 = (it->current.overlay_string_index >= 0
3396 ? it->string_overlays[it->current.overlay_string_index]
3397 : Qnil);
3398
3399 /* See if we got to this string directly or indirectly from
3400 an overlay property. That includes the before-string or
3401 after-string of an overlay, strings in display properties
3402 provided by an overlay, their text properties, etc.
3403
3404 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3405 if (! NILP (from_overlay))
3406 for (i = it->sp - 1; i >= 0; i--)
3407 {
3408 if (it->stack[i].current.overlay_string_index >= 0)
3409 from_overlay
3410 = it->string_overlays[it->stack[i].current.overlay_string_index];
3411 else if (! NILP (it->stack[i].from_overlay))
3412 from_overlay = it->stack[i].from_overlay;
3413
3414 if (!NILP (from_overlay))
3415 break;
3416 }
3417
3418 if (! NILP (from_overlay))
3419 {
3420 bufpos = IT_CHARPOS (*it);
3421 /* For a string from an overlay, the base face depends
3422 only on text properties and ignores overlays. */
3423 base_face_id
3424 = face_for_overlay_string (it->w,
3425 IT_CHARPOS (*it),
3426 it->region_beg_charpos,
3427 it->region_end_charpos,
3428 &next_stop,
3429 (IT_CHARPOS (*it)
3430 + TEXT_PROP_DISTANCE_LIMIT),
3431 0,
3432 from_overlay);
3433 }
3434 else
3435 {
3436 bufpos = 0;
3437
3438 /* For strings from a `display' property, use the face at
3439 IT's current buffer position as the base face to merge
3440 with, so that overlay strings appear in the same face as
3441 surrounding text, unless they specify their own
3442 faces. */
3443 base_face_id = underlying_face_id (it);
3444 }
3445
3446 new_face_id = face_at_string_position (it->w,
3447 it->string,
3448 IT_STRING_CHARPOS (*it),
3449 bufpos,
3450 it->region_beg_charpos,
3451 it->region_end_charpos,
3452 &next_stop,
3453 base_face_id, 0);
3454
3455 #if 0 /* This shouldn't be neccessary. Let's check it. */
3456 /* If IT is used to display a mode line we would really like to
3457 use the mode line face instead of the frame's default face. */
3458 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3459 && new_face_id == DEFAULT_FACE_ID)
3460 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3461 #endif
3462
3463 /* Is this a start of a run of characters with box? Caveat:
3464 this can be called for a freshly allocated iterator; face_id
3465 is -1 is this case. We know that the new face will not
3466 change until the next check pos, i.e. if the new face has a
3467 box, all characters up to that position will have a
3468 box. But, as usual, we don't know whether that position
3469 is really the end. */
3470 if (new_face_id != it->face_id)
3471 {
3472 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3473 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3474
3475 /* If new face has a box but old face hasn't, this is the
3476 start of a run of characters with box, i.e. it has a
3477 shadow on the left side. */
3478 it->start_of_box_run_p
3479 = new_face->box && (old_face == NULL || !old_face->box);
3480 it->face_box_p = new_face->box != FACE_NO_BOX;
3481 }
3482 }
3483
3484 it->face_id = new_face_id;
3485 return HANDLED_NORMALLY;
3486 }
3487
3488
3489 /* Return the ID of the face ``underlying'' IT's current position,
3490 which is in a string. If the iterator is associated with a
3491 buffer, return the face at IT's current buffer position.
3492 Otherwise, use the iterator's base_face_id. */
3493
3494 static int
3495 underlying_face_id (it)
3496 struct it *it;
3497 {
3498 int face_id = it->base_face_id, i;
3499
3500 xassert (STRINGP (it->string));
3501
3502 for (i = it->sp - 1; i >= 0; --i)
3503 if (NILP (it->stack[i].string))
3504 face_id = it->stack[i].face_id;
3505
3506 return face_id;
3507 }
3508
3509
3510 /* Compute the face one character before or after the current position
3511 of IT. BEFORE_P non-zero means get the face in front of IT's
3512 position. Value is the id of the face. */
3513
3514 static int
3515 face_before_or_after_it_pos (it, before_p)
3516 struct it *it;
3517 int before_p;
3518 {
3519 int face_id, limit;
3520 EMACS_INT next_check_charpos;
3521 struct text_pos pos;
3522
3523 xassert (it->s == NULL);
3524
3525 if (STRINGP (it->string))
3526 {
3527 int bufpos, base_face_id;
3528
3529 /* No face change past the end of the string (for the case
3530 we are padding with spaces). No face change before the
3531 string start. */
3532 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3533 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3534 return it->face_id;
3535
3536 /* Set pos to the position before or after IT's current position. */
3537 if (before_p)
3538 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3539 else
3540 /* For composition, we must check the character after the
3541 composition. */
3542 pos = (it->what == IT_COMPOSITION
3543 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3544 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3545
3546 if (it->current.overlay_string_index >= 0)
3547 bufpos = IT_CHARPOS (*it);
3548 else
3549 bufpos = 0;
3550
3551 base_face_id = underlying_face_id (it);
3552
3553 /* Get the face for ASCII, or unibyte. */
3554 face_id = face_at_string_position (it->w,
3555 it->string,
3556 CHARPOS (pos),
3557 bufpos,
3558 it->region_beg_charpos,
3559 it->region_end_charpos,
3560 &next_check_charpos,
3561 base_face_id, 0);
3562
3563 /* Correct the face for charsets different from ASCII. Do it
3564 for the multibyte case only. The face returned above is
3565 suitable for unibyte text if IT->string is unibyte. */
3566 if (STRING_MULTIBYTE (it->string))
3567 {
3568 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3569 int rest = SBYTES (it->string) - BYTEPOS (pos);
3570 int c, len;
3571 struct face *face = FACE_FROM_ID (it->f, face_id);
3572
3573 c = string_char_and_length (p, rest, &len);
3574 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3575 }
3576 }
3577 else
3578 {
3579 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3580 || (IT_CHARPOS (*it) <= BEGV && before_p))
3581 return it->face_id;
3582
3583 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3584 pos = it->current.pos;
3585
3586 if (before_p)
3587 DEC_TEXT_POS (pos, it->multibyte_p);
3588 else
3589 {
3590 if (it->what == IT_COMPOSITION)
3591 /* For composition, we must check the position after the
3592 composition. */
3593 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3594 else
3595 INC_TEXT_POS (pos, it->multibyte_p);
3596 }
3597
3598 /* Determine face for CHARSET_ASCII, or unibyte. */
3599 face_id = face_at_buffer_position (it->w,
3600 CHARPOS (pos),
3601 it->region_beg_charpos,
3602 it->region_end_charpos,
3603 &next_check_charpos,
3604 limit, 0);
3605
3606 /* Correct the face for charsets different from ASCII. Do it
3607 for the multibyte case only. The face returned above is
3608 suitable for unibyte text if current_buffer is unibyte. */
3609 if (it->multibyte_p)
3610 {
3611 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3612 struct face *face = FACE_FROM_ID (it->f, face_id);
3613 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3614 }
3615 }
3616
3617 return face_id;
3618 }
3619
3620
3621 \f
3622 /***********************************************************************
3623 Invisible text
3624 ***********************************************************************/
3625
3626 /* Set up iterator IT from invisible properties at its current
3627 position. Called from handle_stop. */
3628
3629 static enum prop_handled
3630 handle_invisible_prop (it)
3631 struct it *it;
3632 {
3633 enum prop_handled handled = HANDLED_NORMALLY;
3634
3635 if (STRINGP (it->string))
3636 {
3637 extern Lisp_Object Qinvisible;
3638 Lisp_Object prop, end_charpos, limit, charpos;
3639
3640 /* Get the value of the invisible text property at the
3641 current position. Value will be nil if there is no such
3642 property. */
3643 charpos = make_number (IT_STRING_CHARPOS (*it));
3644 prop = Fget_text_property (charpos, Qinvisible, it->string);
3645
3646 if (!NILP (prop)
3647 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3648 {
3649 handled = HANDLED_RECOMPUTE_PROPS;
3650
3651 /* Get the position at which the next change of the
3652 invisible text property can be found in IT->string.
3653 Value will be nil if the property value is the same for
3654 all the rest of IT->string. */
3655 XSETINT (limit, SCHARS (it->string));
3656 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3657 it->string, limit);
3658
3659 /* Text at current position is invisible. The next
3660 change in the property is at position end_charpos.
3661 Move IT's current position to that position. */
3662 if (INTEGERP (end_charpos)
3663 && XFASTINT (end_charpos) < XFASTINT (limit))
3664 {
3665 struct text_pos old;
3666 old = it->current.string_pos;
3667 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3668 compute_string_pos (&it->current.string_pos, old, it->string);
3669 }
3670 else
3671 {
3672 /* The rest of the string is invisible. If this is an
3673 overlay string, proceed with the next overlay string
3674 or whatever comes and return a character from there. */
3675 if (it->current.overlay_string_index >= 0)
3676 {
3677 next_overlay_string (it);
3678 /* Don't check for overlay strings when we just
3679 finished processing them. */
3680 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3681 }
3682 else
3683 {
3684 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3685 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3686 }
3687 }
3688 }
3689 }
3690 else
3691 {
3692 int invis_p;
3693 EMACS_INT newpos, next_stop, start_charpos;
3694 Lisp_Object pos, prop, overlay;
3695
3696 /* First of all, is there invisible text at this position? */
3697 start_charpos = IT_CHARPOS (*it);
3698 pos = make_number (IT_CHARPOS (*it));
3699 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3700 &overlay);
3701 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3702
3703 /* If we are on invisible text, skip over it. */
3704 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3705 {
3706 /* Record whether we have to display an ellipsis for the
3707 invisible text. */
3708 int display_ellipsis_p = invis_p == 2;
3709
3710 handled = HANDLED_RECOMPUTE_PROPS;
3711
3712 /* Loop skipping over invisible text. The loop is left at
3713 ZV or with IT on the first char being visible again. */
3714 do
3715 {
3716 /* Try to skip some invisible text. Return value is the
3717 position reached which can be equal to IT's position
3718 if there is nothing invisible here. This skips both
3719 over invisible text properties and overlays with
3720 invisible property. */
3721 newpos = skip_invisible (IT_CHARPOS (*it),
3722 &next_stop, ZV, it->window);
3723
3724 /* If we skipped nothing at all we weren't at invisible
3725 text in the first place. If everything to the end of
3726 the buffer was skipped, end the loop. */
3727 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3728 invis_p = 0;
3729 else
3730 {
3731 /* We skipped some characters but not necessarily
3732 all there are. Check if we ended up on visible
3733 text. Fget_char_property returns the property of
3734 the char before the given position, i.e. if we
3735 get invis_p = 0, this means that the char at
3736 newpos is visible. */
3737 pos = make_number (newpos);
3738 prop = Fget_char_property (pos, Qinvisible, it->window);
3739 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3740 }
3741
3742 /* If we ended up on invisible text, proceed to
3743 skip starting with next_stop. */
3744 if (invis_p)
3745 IT_CHARPOS (*it) = next_stop;
3746
3747 /* If there are adjacent invisible texts, don't lose the
3748 second one's ellipsis. */
3749 if (invis_p == 2)
3750 display_ellipsis_p = 1;
3751 }
3752 while (invis_p);
3753
3754 /* The position newpos is now either ZV or on visible text. */
3755 IT_CHARPOS (*it) = newpos;
3756 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3757
3758 /* If there are before-strings at the start of invisible
3759 text, and the text is invisible because of a text
3760 property, arrange to show before-strings because 20.x did
3761 it that way. (If the text is invisible because of an
3762 overlay property instead of a text property, this is
3763 already handled in the overlay code.) */
3764 if (NILP (overlay)
3765 && get_overlay_strings (it, start_charpos))
3766 {
3767 handled = HANDLED_RECOMPUTE_PROPS;
3768 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3769 }
3770 else if (display_ellipsis_p)
3771 {
3772 /* Make sure that the glyphs of the ellipsis will get
3773 correct `charpos' values. If we would not update
3774 it->position here, the glyphs would belong to the
3775 last visible character _before_ the invisible
3776 text, which confuses `set_cursor_from_row'.
3777
3778 We use the last invisible position instead of the
3779 first because this way the cursor is always drawn on
3780 the first "." of the ellipsis, whenever PT is inside
3781 the invisible text. Otherwise the cursor would be
3782 placed _after_ the ellipsis when the point is after the
3783 first invisible character. */
3784 if (!STRINGP (it->object))
3785 {
3786 it->position.charpos = IT_CHARPOS (*it) - 1;
3787 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3788 }
3789 setup_for_ellipsis (it, 0);
3790 /* Let the ellipsis display before
3791 considering any properties of the following char.
3792 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3793 handled = HANDLED_RETURN;
3794 }
3795 }
3796 }
3797
3798 return handled;
3799 }
3800
3801
3802 /* Make iterator IT return `...' next.
3803 Replaces LEN characters from buffer. */
3804
3805 static void
3806 setup_for_ellipsis (it, len)
3807 struct it *it;
3808 int len;
3809 {
3810 /* Use the display table definition for `...'. Invalid glyphs
3811 will be handled by the method returning elements from dpvec. */
3812 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3813 {
3814 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3815 it->dpvec = v->contents;
3816 it->dpend = v->contents + v->size;
3817 }
3818 else
3819 {
3820 /* Default `...'. */
3821 it->dpvec = default_invis_vector;
3822 it->dpend = default_invis_vector + 3;
3823 }
3824
3825 it->dpvec_char_len = len;
3826 it->current.dpvec_index = 0;
3827 it->dpvec_face_id = -1;
3828
3829 /* Remember the current face id in case glyphs specify faces.
3830 IT's face is restored in set_iterator_to_next.
3831 saved_face_id was set to preceding char's face in handle_stop. */
3832 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3833 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3834
3835 it->method = GET_FROM_DISPLAY_VECTOR;
3836 it->ellipsis_p = 1;
3837 }
3838
3839
3840 \f
3841 /***********************************************************************
3842 'display' property
3843 ***********************************************************************/
3844
3845 /* Set up iterator IT from `display' property at its current position.
3846 Called from handle_stop.
3847 We return HANDLED_RETURN if some part of the display property
3848 overrides the display of the buffer text itself.
3849 Otherwise we return HANDLED_NORMALLY. */
3850
3851 static enum prop_handled
3852 handle_display_prop (it)
3853 struct it *it;
3854 {
3855 Lisp_Object prop, object, overlay;
3856 struct text_pos *position;
3857 /* Nonzero if some property replaces the display of the text itself. */
3858 int display_replaced_p = 0;
3859
3860 if (STRINGP (it->string))
3861 {
3862 object = it->string;
3863 position = &it->current.string_pos;
3864 }
3865 else
3866 {
3867 XSETWINDOW (object, it->w);
3868 position = &it->current.pos;
3869 }
3870
3871 /* Reset those iterator values set from display property values. */
3872 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3873 it->space_width = Qnil;
3874 it->font_height = Qnil;
3875 it->voffset = 0;
3876
3877 /* We don't support recursive `display' properties, i.e. string
3878 values that have a string `display' property, that have a string
3879 `display' property etc. */
3880 if (!it->string_from_display_prop_p)
3881 it->area = TEXT_AREA;
3882
3883 prop = get_char_property_and_overlay (make_number (position->charpos),
3884 Qdisplay, object, &overlay);
3885 if (NILP (prop))
3886 return HANDLED_NORMALLY;
3887 /* Now OVERLAY is the overlay that gave us this property, or nil
3888 if it was a text property. */
3889
3890 if (!STRINGP (it->string))
3891 object = it->w->buffer;
3892
3893 if (CONSP (prop)
3894 /* Simple properties. */
3895 && !EQ (XCAR (prop), Qimage)
3896 && !EQ (XCAR (prop), Qspace)
3897 && !EQ (XCAR (prop), Qwhen)
3898 && !EQ (XCAR (prop), Qslice)
3899 && !EQ (XCAR (prop), Qspace_width)
3900 && !EQ (XCAR (prop), Qheight)
3901 && !EQ (XCAR (prop), Qraise)
3902 /* Marginal area specifications. */
3903 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3904 && !EQ (XCAR (prop), Qleft_fringe)
3905 && !EQ (XCAR (prop), Qright_fringe)
3906 && !NILP (XCAR (prop)))
3907 {
3908 for (; CONSP (prop); prop = XCDR (prop))
3909 {
3910 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3911 position, display_replaced_p))
3912 {
3913 display_replaced_p = 1;
3914 /* If some text in a string is replaced, `position' no
3915 longer points to the position of `object'. */
3916 if (STRINGP (object))
3917 break;
3918 }
3919 }
3920 }
3921 else if (VECTORP (prop))
3922 {
3923 int i;
3924 for (i = 0; i < ASIZE (prop); ++i)
3925 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3926 position, display_replaced_p))
3927 {
3928 display_replaced_p = 1;
3929 /* If some text in a string is replaced, `position' no
3930 longer points to the position of `object'. */
3931 if (STRINGP (object))
3932 break;
3933 }
3934 }
3935 else
3936 {
3937 int ret = handle_single_display_spec (it, prop, object, overlay,
3938 position, 0);
3939 if (ret < 0) /* Replaced by "", i.e. nothing. */
3940 return HANDLED_RECOMPUTE_PROPS;
3941 if (ret)
3942 display_replaced_p = 1;
3943 }
3944
3945 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3946 }
3947
3948
3949 /* Value is the position of the end of the `display' property starting
3950 at START_POS in OBJECT. */
3951
3952 static struct text_pos
3953 display_prop_end (it, object, start_pos)
3954 struct it *it;
3955 Lisp_Object object;
3956 struct text_pos start_pos;
3957 {
3958 Lisp_Object end;
3959 struct text_pos end_pos;
3960
3961 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3962 Qdisplay, object, Qnil);
3963 CHARPOS (end_pos) = XFASTINT (end);
3964 if (STRINGP (object))
3965 compute_string_pos (&end_pos, start_pos, it->string);
3966 else
3967 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3968
3969 return end_pos;
3970 }
3971
3972
3973 /* Set up IT from a single `display' specification PROP. OBJECT
3974 is the object in which the `display' property was found. *POSITION
3975 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3976 means that we previously saw a display specification which already
3977 replaced text display with something else, for example an image;
3978 we ignore such properties after the first one has been processed.
3979
3980 OVERLAY is the overlay this `display' property came from,
3981 or nil if it was a text property.
3982
3983 If PROP is a `space' or `image' specification, and in some other
3984 cases too, set *POSITION to the position where the `display'
3985 property ends.
3986
3987 Value is non-zero if something was found which replaces the display
3988 of buffer or string text. Specifically, the value is -1 if that
3989 "something" is "nothing". */
3990
3991 static int
3992 handle_single_display_spec (it, spec, object, overlay, position,
3993 display_replaced_before_p)
3994 struct it *it;
3995 Lisp_Object spec;
3996 Lisp_Object object;
3997 Lisp_Object overlay;
3998 struct text_pos *position;
3999 int display_replaced_before_p;
4000 {
4001 Lisp_Object form;
4002 Lisp_Object location, value;
4003 struct text_pos start_pos, save_pos;
4004 int valid_p;
4005
4006 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4007 If the result is non-nil, use VALUE instead of SPEC. */
4008 form = Qt;
4009 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4010 {
4011 spec = XCDR (spec);
4012 if (!CONSP (spec))
4013 return 0;
4014 form = XCAR (spec);
4015 spec = XCDR (spec);
4016 }
4017
4018 if (!NILP (form) && !EQ (form, Qt))
4019 {
4020 int count = SPECPDL_INDEX ();
4021 struct gcpro gcpro1;
4022
4023 /* Bind `object' to the object having the `display' property, a
4024 buffer or string. Bind `position' to the position in the
4025 object where the property was found, and `buffer-position'
4026 to the current position in the buffer. */
4027 specbind (Qobject, object);
4028 specbind (Qposition, make_number (CHARPOS (*position)));
4029 specbind (Qbuffer_position,
4030 make_number (STRINGP (object)
4031 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4032 GCPRO1 (form);
4033 form = safe_eval (form);
4034 UNGCPRO;
4035 unbind_to (count, Qnil);
4036 }
4037
4038 if (NILP (form))
4039 return 0;
4040
4041 /* Handle `(height HEIGHT)' specifications. */
4042 if (CONSP (spec)
4043 && EQ (XCAR (spec), Qheight)
4044 && CONSP (XCDR (spec)))
4045 {
4046 if (!FRAME_WINDOW_P (it->f))
4047 return 0;
4048
4049 it->font_height = XCAR (XCDR (spec));
4050 if (!NILP (it->font_height))
4051 {
4052 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4053 int new_height = -1;
4054
4055 if (CONSP (it->font_height)
4056 && (EQ (XCAR (it->font_height), Qplus)
4057 || EQ (XCAR (it->font_height), Qminus))
4058 && CONSP (XCDR (it->font_height))
4059 && INTEGERP (XCAR (XCDR (it->font_height))))
4060 {
4061 /* `(+ N)' or `(- N)' where N is an integer. */
4062 int steps = XINT (XCAR (XCDR (it->font_height)));
4063 if (EQ (XCAR (it->font_height), Qplus))
4064 steps = - steps;
4065 it->face_id = smaller_face (it->f, it->face_id, steps);
4066 }
4067 else if (FUNCTIONP (it->font_height))
4068 {
4069 /* Call function with current height as argument.
4070 Value is the new height. */
4071 Lisp_Object height;
4072 height = safe_call1 (it->font_height,
4073 face->lface[LFACE_HEIGHT_INDEX]);
4074 if (NUMBERP (height))
4075 new_height = XFLOATINT (height);
4076 }
4077 else if (NUMBERP (it->font_height))
4078 {
4079 /* Value is a multiple of the canonical char height. */
4080 struct face *face;
4081
4082 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4083 new_height = (XFLOATINT (it->font_height)
4084 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4085 }
4086 else
4087 {
4088 /* Evaluate IT->font_height with `height' bound to the
4089 current specified height to get the new height. */
4090 int count = SPECPDL_INDEX ();
4091
4092 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4093 value = safe_eval (it->font_height);
4094 unbind_to (count, Qnil);
4095
4096 if (NUMBERP (value))
4097 new_height = XFLOATINT (value);
4098 }
4099
4100 if (new_height > 0)
4101 it->face_id = face_with_height (it->f, it->face_id, new_height);
4102 }
4103
4104 return 0;
4105 }
4106
4107 /* Handle `(space-width WIDTH)'. */
4108 if (CONSP (spec)
4109 && EQ (XCAR (spec), Qspace_width)
4110 && CONSP (XCDR (spec)))
4111 {
4112 if (!FRAME_WINDOW_P (it->f))
4113 return 0;
4114
4115 value = XCAR (XCDR (spec));
4116 if (NUMBERP (value) && XFLOATINT (value) > 0)
4117 it->space_width = value;
4118
4119 return 0;
4120 }
4121
4122 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4123 if (CONSP (spec)
4124 && EQ (XCAR (spec), Qslice))
4125 {
4126 Lisp_Object tem;
4127
4128 if (!FRAME_WINDOW_P (it->f))
4129 return 0;
4130
4131 if (tem = XCDR (spec), CONSP (tem))
4132 {
4133 it->slice.x = XCAR (tem);
4134 if (tem = XCDR (tem), CONSP (tem))
4135 {
4136 it->slice.y = XCAR (tem);
4137 if (tem = XCDR (tem), CONSP (tem))
4138 {
4139 it->slice.width = XCAR (tem);
4140 if (tem = XCDR (tem), CONSP (tem))
4141 it->slice.height = XCAR (tem);
4142 }
4143 }
4144 }
4145
4146 return 0;
4147 }
4148
4149 /* Handle `(raise FACTOR)'. */
4150 if (CONSP (spec)
4151 && EQ (XCAR (spec), Qraise)
4152 && CONSP (XCDR (spec)))
4153 {
4154 if (!FRAME_WINDOW_P (it->f))
4155 return 0;
4156
4157 #ifdef HAVE_WINDOW_SYSTEM
4158 value = XCAR (XCDR (spec));
4159 if (NUMBERP (value))
4160 {
4161 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4162 it->voffset = - (XFLOATINT (value)
4163 * (FONT_HEIGHT (face->font)));
4164 }
4165 #endif /* HAVE_WINDOW_SYSTEM */
4166
4167 return 0;
4168 }
4169
4170 /* Don't handle the other kinds of display specifications
4171 inside a string that we got from a `display' property. */
4172 if (it->string_from_display_prop_p)
4173 return 0;
4174
4175 /* Characters having this form of property are not displayed, so
4176 we have to find the end of the property. */
4177 start_pos = *position;
4178 *position = display_prop_end (it, object, start_pos);
4179 value = Qnil;
4180
4181 /* Stop the scan at that end position--we assume that all
4182 text properties change there. */
4183 it->stop_charpos = position->charpos;
4184
4185 /* Handle `(left-fringe BITMAP [FACE])'
4186 and `(right-fringe BITMAP [FACE])'. */
4187 if (CONSP (spec)
4188 && (EQ (XCAR (spec), Qleft_fringe)
4189 || EQ (XCAR (spec), Qright_fringe))
4190 && CONSP (XCDR (spec)))
4191 {
4192 int face_id = DEFAULT_FACE_ID;
4193 int fringe_bitmap;
4194
4195 if (!FRAME_WINDOW_P (it->f))
4196 /* If we return here, POSITION has been advanced
4197 across the text with this property. */
4198 return 0;
4199
4200 #ifdef HAVE_WINDOW_SYSTEM
4201 value = XCAR (XCDR (spec));
4202 if (!SYMBOLP (value)
4203 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4204 /* If we return here, POSITION has been advanced
4205 across the text with this property. */
4206 return 0;
4207
4208 if (CONSP (XCDR (XCDR (spec))))
4209 {
4210 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4211 int face_id2 = lookup_derived_face (it->f, face_name,
4212 FRINGE_FACE_ID, 0);
4213 if (face_id2 >= 0)
4214 face_id = face_id2;
4215 }
4216
4217 /* Save current settings of IT so that we can restore them
4218 when we are finished with the glyph property value. */
4219
4220 save_pos = it->position;
4221 it->position = *position;
4222 push_it (it);
4223 it->position = save_pos;
4224
4225 it->area = TEXT_AREA;
4226 it->what = IT_IMAGE;
4227 it->image_id = -1; /* no image */
4228 it->position = start_pos;
4229 it->object = NILP (object) ? it->w->buffer : object;
4230 it->method = GET_FROM_IMAGE;
4231 it->from_overlay = Qnil;
4232 it->face_id = face_id;
4233
4234 /* Say that we haven't consumed the characters with
4235 `display' property yet. The call to pop_it in
4236 set_iterator_to_next will clean this up. */
4237 *position = start_pos;
4238
4239 if (EQ (XCAR (spec), Qleft_fringe))
4240 {
4241 it->left_user_fringe_bitmap = fringe_bitmap;
4242 it->left_user_fringe_face_id = face_id;
4243 }
4244 else
4245 {
4246 it->right_user_fringe_bitmap = fringe_bitmap;
4247 it->right_user_fringe_face_id = face_id;
4248 }
4249 #endif /* HAVE_WINDOW_SYSTEM */
4250 return 1;
4251 }
4252
4253 /* Prepare to handle `((margin left-margin) ...)',
4254 `((margin right-margin) ...)' and `((margin nil) ...)'
4255 prefixes for display specifications. */
4256 location = Qunbound;
4257 if (CONSP (spec) && CONSP (XCAR (spec)))
4258 {
4259 Lisp_Object tem;
4260
4261 value = XCDR (spec);
4262 if (CONSP (value))
4263 value = XCAR (value);
4264
4265 tem = XCAR (spec);
4266 if (EQ (XCAR (tem), Qmargin)
4267 && (tem = XCDR (tem),
4268 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4269 (NILP (tem)
4270 || EQ (tem, Qleft_margin)
4271 || EQ (tem, Qright_margin))))
4272 location = tem;
4273 }
4274
4275 if (EQ (location, Qunbound))
4276 {
4277 location = Qnil;
4278 value = spec;
4279 }
4280
4281 /* After this point, VALUE is the property after any
4282 margin prefix has been stripped. It must be a string,
4283 an image specification, or `(space ...)'.
4284
4285 LOCATION specifies where to display: `left-margin',
4286 `right-margin' or nil. */
4287
4288 valid_p = (STRINGP (value)
4289 #ifdef HAVE_WINDOW_SYSTEM
4290 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4291 #endif /* not HAVE_WINDOW_SYSTEM */
4292 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4293
4294 if (valid_p && !display_replaced_before_p)
4295 {
4296 /* Save current settings of IT so that we can restore them
4297 when we are finished with the glyph property value. */
4298 save_pos = it->position;
4299 it->position = *position;
4300 push_it (it);
4301 it->position = save_pos;
4302 it->from_overlay = overlay;
4303
4304 if (NILP (location))
4305 it->area = TEXT_AREA;
4306 else if (EQ (location, Qleft_margin))
4307 it->area = LEFT_MARGIN_AREA;
4308 else
4309 it->area = RIGHT_MARGIN_AREA;
4310
4311 if (STRINGP (value))
4312 {
4313 if (SCHARS (value) == 0)
4314 {
4315 pop_it (it);
4316 return -1; /* Replaced by "", i.e. nothing. */
4317 }
4318 it->string = value;
4319 it->multibyte_p = STRING_MULTIBYTE (it->string);
4320 it->current.overlay_string_index = -1;
4321 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4322 it->end_charpos = it->string_nchars = SCHARS (it->string);
4323 it->method = GET_FROM_STRING;
4324 it->stop_charpos = 0;
4325 it->string_from_display_prop_p = 1;
4326 /* Say that we haven't consumed the characters with
4327 `display' property yet. The call to pop_it in
4328 set_iterator_to_next will clean this up. */
4329 if (BUFFERP (object))
4330 *position = start_pos;
4331 }
4332 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4333 {
4334 it->method = GET_FROM_STRETCH;
4335 it->object = value;
4336 *position = it->position = start_pos;
4337 }
4338 #ifdef HAVE_WINDOW_SYSTEM
4339 else
4340 {
4341 it->what = IT_IMAGE;
4342 it->image_id = lookup_image (it->f, value);
4343 it->position = start_pos;
4344 it->object = NILP (object) ? it->w->buffer : object;
4345 it->method = GET_FROM_IMAGE;
4346
4347 /* Say that we haven't consumed the characters with
4348 `display' property yet. The call to pop_it in
4349 set_iterator_to_next will clean this up. */
4350 *position = start_pos;
4351 }
4352 #endif /* HAVE_WINDOW_SYSTEM */
4353
4354 return 1;
4355 }
4356
4357 /* Invalid property or property not supported. Restore
4358 POSITION to what it was before. */
4359 *position = start_pos;
4360 return 0;
4361 }
4362
4363
4364 /* Check if SPEC is a display sub-property value whose text should be
4365 treated as intangible. */
4366
4367 static int
4368 single_display_spec_intangible_p (prop)
4369 Lisp_Object prop;
4370 {
4371 /* Skip over `when FORM'. */
4372 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4373 {
4374 prop = XCDR (prop);
4375 if (!CONSP (prop))
4376 return 0;
4377 prop = XCDR (prop);
4378 }
4379
4380 if (STRINGP (prop))
4381 return 1;
4382
4383 if (!CONSP (prop))
4384 return 0;
4385
4386 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4387 we don't need to treat text as intangible. */
4388 if (EQ (XCAR (prop), Qmargin))
4389 {
4390 prop = XCDR (prop);
4391 if (!CONSP (prop))
4392 return 0;
4393
4394 prop = XCDR (prop);
4395 if (!CONSP (prop)
4396 || EQ (XCAR (prop), Qleft_margin)
4397 || EQ (XCAR (prop), Qright_margin))
4398 return 0;
4399 }
4400
4401 return (CONSP (prop)
4402 && (EQ (XCAR (prop), Qimage)
4403 || EQ (XCAR (prop), Qspace)));
4404 }
4405
4406
4407 /* Check if PROP is a display property value whose text should be
4408 treated as intangible. */
4409
4410 int
4411 display_prop_intangible_p (prop)
4412 Lisp_Object prop;
4413 {
4414 if (CONSP (prop)
4415 && CONSP (XCAR (prop))
4416 && !EQ (Qmargin, XCAR (XCAR (prop))))
4417 {
4418 /* A list of sub-properties. */
4419 while (CONSP (prop))
4420 {
4421 if (single_display_spec_intangible_p (XCAR (prop)))
4422 return 1;
4423 prop = XCDR (prop);
4424 }
4425 }
4426 else if (VECTORP (prop))
4427 {
4428 /* A vector of sub-properties. */
4429 int i;
4430 for (i = 0; i < ASIZE (prop); ++i)
4431 if (single_display_spec_intangible_p (AREF (prop, i)))
4432 return 1;
4433 }
4434 else
4435 return single_display_spec_intangible_p (prop);
4436
4437 return 0;
4438 }
4439
4440
4441 /* Return 1 if PROP is a display sub-property value containing STRING. */
4442
4443 static int
4444 single_display_spec_string_p (prop, string)
4445 Lisp_Object prop, string;
4446 {
4447 if (EQ (string, prop))
4448 return 1;
4449
4450 /* Skip over `when FORM'. */
4451 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4452 {
4453 prop = XCDR (prop);
4454 if (!CONSP (prop))
4455 return 0;
4456 prop = XCDR (prop);
4457 }
4458
4459 if (CONSP (prop))
4460 /* Skip over `margin LOCATION'. */
4461 if (EQ (XCAR (prop), Qmargin))
4462 {
4463 prop = XCDR (prop);
4464 if (!CONSP (prop))
4465 return 0;
4466
4467 prop = XCDR (prop);
4468 if (!CONSP (prop))
4469 return 0;
4470 }
4471
4472 return CONSP (prop) && EQ (XCAR (prop), string);
4473 }
4474
4475
4476 /* Return 1 if STRING appears in the `display' property PROP. */
4477
4478 static int
4479 display_prop_string_p (prop, string)
4480 Lisp_Object prop, string;
4481 {
4482 if (CONSP (prop)
4483 && CONSP (XCAR (prop))
4484 && !EQ (Qmargin, XCAR (XCAR (prop))))
4485 {
4486 /* A list of sub-properties. */
4487 while (CONSP (prop))
4488 {
4489 if (single_display_spec_string_p (XCAR (prop), string))
4490 return 1;
4491 prop = XCDR (prop);
4492 }
4493 }
4494 else if (VECTORP (prop))
4495 {
4496 /* A vector of sub-properties. */
4497 int i;
4498 for (i = 0; i < ASIZE (prop); ++i)
4499 if (single_display_spec_string_p (AREF (prop, i), string))
4500 return 1;
4501 }
4502 else
4503 return single_display_spec_string_p (prop, string);
4504
4505 return 0;
4506 }
4507
4508
4509 /* Determine from which buffer position in W's buffer STRING comes
4510 from. AROUND_CHARPOS is an approximate position where it could
4511 be from. Value is the buffer position or 0 if it couldn't be
4512 determined.
4513
4514 W's buffer must be current.
4515
4516 This function is necessary because we don't record buffer positions
4517 in glyphs generated from strings (to keep struct glyph small).
4518 This function may only use code that doesn't eval because it is
4519 called asynchronously from note_mouse_highlight. */
4520
4521 int
4522 string_buffer_position (w, string, around_charpos)
4523 struct window *w;
4524 Lisp_Object string;
4525 int around_charpos;
4526 {
4527 Lisp_Object limit, prop, pos;
4528 const int MAX_DISTANCE = 1000;
4529 int found = 0;
4530
4531 pos = make_number (around_charpos);
4532 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4533 while (!found && !EQ (pos, limit))
4534 {
4535 prop = Fget_char_property (pos, Qdisplay, Qnil);
4536 if (!NILP (prop) && display_prop_string_p (prop, string))
4537 found = 1;
4538 else
4539 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4540 }
4541
4542 if (!found)
4543 {
4544 pos = make_number (around_charpos);
4545 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4546 while (!found && !EQ (pos, limit))
4547 {
4548 prop = Fget_char_property (pos, Qdisplay, Qnil);
4549 if (!NILP (prop) && display_prop_string_p (prop, string))
4550 found = 1;
4551 else
4552 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4553 limit);
4554 }
4555 }
4556
4557 return found ? XINT (pos) : 0;
4558 }
4559
4560
4561 \f
4562 /***********************************************************************
4563 `composition' property
4564 ***********************************************************************/
4565
4566 static enum prop_handled
4567 handle_auto_composed_prop (it)
4568 struct it *it;
4569 {
4570 enum prop_handled handled = HANDLED_NORMALLY;
4571
4572 if (FRAME_WINDOW_P (it->f) && FUNCTIONP (Vauto_composition_function))
4573 {
4574 Lisp_Object val = Qnil;
4575 EMACS_INT pos, limit = -1;
4576
4577 if (STRINGP (it->string))
4578 pos = IT_STRING_CHARPOS (*it);
4579 else
4580 pos = IT_CHARPOS (*it);
4581
4582 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4583 if (! NILP (val))
4584 {
4585 Lisp_Object cmp_prop;
4586 EMACS_INT cmp_start, cmp_end;
4587
4588 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4589 &cmp_start, &cmp_end, it->string)
4590 && cmp_start == pos
4591 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4592 {
4593 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4594 Lisp_Object font_object = LGSTRING_FONT (gstring);
4595
4596 if (! EQ (font_object,
4597 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4598 it->w, it->string)))
4599 /* We must re-compute the composition for the
4600 different font. */
4601 val = Qnil;
4602 }
4603
4604 if (! NILP (val))
4605 {
4606 Lisp_Object end;
4607
4608 /* As Fnext_single_char_property_change is very slow, we
4609 limit the search to the current line. */
4610 if (STRINGP (it->string))
4611 limit = SCHARS (it->string);
4612 else
4613 limit = find_next_newline_no_quit (pos, 1);
4614 end = Fnext_single_char_property_change (make_number (pos),
4615 Qauto_composed,
4616 it->string,
4617 make_number (limit));
4618
4619 if (XINT (end) < limit)
4620 /* The current point is auto-composed, but there exist
4621 characters not yet composed beyond the
4622 auto-composed region. There's a possiblity that
4623 the last characters in the region may be newly
4624 composed. */
4625 val = Qnil;
4626 }
4627 }
4628 if (NILP (val) && ! STRINGP (it->string))
4629 {
4630 if (limit < 0)
4631 limit = (STRINGP (it->string) ? SCHARS (it->string)
4632 : find_next_newline_no_quit (pos, 1));
4633 if (pos < limit)
4634 {
4635 int count = SPECPDL_INDEX ();
4636 Lisp_Object args[5];
4637
4638 limit = font_range (pos, limit, FACE_FROM_ID (it->f, it->face_id),
4639 it->f, it->string);
4640 args[0] = Vauto_composition_function;
4641 specbind (Qauto_composition_function, Qnil);
4642 args[1] = make_number (pos);
4643 args[2] = make_number (limit);
4644 args[3] = it->window;
4645 args[4] = it->string;
4646 safe_call (5, args);
4647 unbind_to (count, Qnil);
4648 }
4649 }
4650 }
4651
4652 return handled;
4653 }
4654
4655 /* Set up iterator IT from `composition' property at its current
4656 position. Called from handle_stop. */
4657
4658 static enum prop_handled
4659 handle_composition_prop (it)
4660 struct it *it;
4661 {
4662 Lisp_Object prop, string;
4663 EMACS_INT pos, pos_byte, start, end;
4664 enum prop_handled handled = HANDLED_NORMALLY;
4665
4666 if (STRINGP (it->string))
4667 {
4668 unsigned char *s;
4669
4670 pos = IT_STRING_CHARPOS (*it);
4671 pos_byte = IT_STRING_BYTEPOS (*it);
4672 string = it->string;
4673 s = SDATA (string) + pos_byte;
4674 it->c = STRING_CHAR (s, 0);
4675 }
4676 else
4677 {
4678 pos = IT_CHARPOS (*it);
4679 pos_byte = IT_BYTEPOS (*it);
4680 string = Qnil;
4681 it->c = FETCH_CHAR (pos_byte);
4682 }
4683
4684 /* If there's a valid composition and point is not inside of the
4685 composition (in the case that the composition is from the current
4686 buffer), draw a glyph composed from the composition components. */
4687 if (find_composition (pos, -1, &start, &end, &prop, string)
4688 && COMPOSITION_VALID_P (start, end, prop)
4689 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4690 {
4691 int id;
4692
4693 if (start != pos)
4694 {
4695 if (STRINGP (it->string))
4696 pos_byte = string_char_to_byte (it->string, start);
4697 else
4698 pos_byte = CHAR_TO_BYTE (start);
4699 }
4700 id = get_composition_id (start, pos_byte, end - start, prop, string);
4701
4702 if (id >= 0)
4703 {
4704 struct composition *cmp = composition_table[id];
4705
4706 if (cmp->glyph_len == 0)
4707 {
4708 /* No glyph. */
4709 if (STRINGP (it->string))
4710 {
4711 IT_STRING_CHARPOS (*it) = end;
4712 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4713 end);
4714 }
4715 else
4716 {
4717 IT_CHARPOS (*it) = end;
4718 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4719 }
4720 return HANDLED_RECOMPUTE_PROPS;
4721 }
4722
4723 it->stop_charpos = end;
4724 push_it (it);
4725
4726 it->method = GET_FROM_COMPOSITION;
4727 it->cmp_id = id;
4728 it->cmp_len = COMPOSITION_LENGTH (prop);
4729 /* For a terminal, draw only the first (non-TAB) character
4730 of the components. */
4731 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4732 {
4733 /* FIXME: This doesn't do anything!?! */
4734 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4735 ->key_and_value,
4736 cmp->hash_index * 2);
4737 }
4738 else
4739 {
4740 int i;
4741
4742 for (i = 0; i < cmp->glyph_len; i++)
4743 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4744 != '\t')
4745 break;
4746 }
4747 if (it->c == '\t')
4748 it->c = ' ';
4749 it->len = (STRINGP (it->string)
4750 ? string_char_to_byte (it->string, end)
4751 : CHAR_TO_BYTE (end)) - pos_byte;
4752 handled = HANDLED_RETURN;
4753 }
4754 }
4755
4756 return handled;
4757 }
4758
4759
4760 \f
4761 /***********************************************************************
4762 Overlay strings
4763 ***********************************************************************/
4764
4765 /* The following structure is used to record overlay strings for
4766 later sorting in load_overlay_strings. */
4767
4768 struct overlay_entry
4769 {
4770 Lisp_Object overlay;
4771 Lisp_Object string;
4772 int priority;
4773 int after_string_p;
4774 };
4775
4776
4777 /* Set up iterator IT from overlay strings at its current position.
4778 Called from handle_stop. */
4779
4780 static enum prop_handled
4781 handle_overlay_change (it)
4782 struct it *it;
4783 {
4784 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4785 return HANDLED_RECOMPUTE_PROPS;
4786 else
4787 return HANDLED_NORMALLY;
4788 }
4789
4790
4791 /* Set up the next overlay string for delivery by IT, if there is an
4792 overlay string to deliver. Called by set_iterator_to_next when the
4793 end of the current overlay string is reached. If there are more
4794 overlay strings to display, IT->string and
4795 IT->current.overlay_string_index are set appropriately here.
4796 Otherwise IT->string is set to nil. */
4797
4798 static void
4799 next_overlay_string (it)
4800 struct it *it;
4801 {
4802 ++it->current.overlay_string_index;
4803 if (it->current.overlay_string_index == it->n_overlay_strings)
4804 {
4805 /* No more overlay strings. Restore IT's settings to what
4806 they were before overlay strings were processed, and
4807 continue to deliver from current_buffer. */
4808 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4809
4810 pop_it (it);
4811 xassert (it->sp > 0
4812 || it->method == GET_FROM_COMPOSITION
4813 || (NILP (it->string)
4814 && it->method == GET_FROM_BUFFER
4815 && it->stop_charpos >= BEGV
4816 && it->stop_charpos <= it->end_charpos));
4817 it->current.overlay_string_index = -1;
4818 it->n_overlay_strings = 0;
4819
4820 /* If we're at the end of the buffer, record that we have
4821 processed the overlay strings there already, so that
4822 next_element_from_buffer doesn't try it again. */
4823 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4824 it->overlay_strings_at_end_processed_p = 1;
4825
4826 /* If we have to display `...' for invisible text, set
4827 the iterator up for that. */
4828 if (display_ellipsis_p)
4829 setup_for_ellipsis (it, 0);
4830 }
4831 else
4832 {
4833 /* There are more overlay strings to process. If
4834 IT->current.overlay_string_index has advanced to a position
4835 where we must load IT->overlay_strings with more strings, do
4836 it. */
4837 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4838
4839 if (it->current.overlay_string_index && i == 0)
4840 load_overlay_strings (it, 0);
4841
4842 /* Initialize IT to deliver display elements from the overlay
4843 string. */
4844 it->string = it->overlay_strings[i];
4845 it->multibyte_p = STRING_MULTIBYTE (it->string);
4846 SET_TEXT_POS (it->current.string_pos, 0, 0);
4847 it->method = GET_FROM_STRING;
4848 it->stop_charpos = 0;
4849 }
4850
4851 CHECK_IT (it);
4852 }
4853
4854
4855 /* Compare two overlay_entry structures E1 and E2. Used as a
4856 comparison function for qsort in load_overlay_strings. Overlay
4857 strings for the same position are sorted so that
4858
4859 1. All after-strings come in front of before-strings, except
4860 when they come from the same overlay.
4861
4862 2. Within after-strings, strings are sorted so that overlay strings
4863 from overlays with higher priorities come first.
4864
4865 2. Within before-strings, strings are sorted so that overlay
4866 strings from overlays with higher priorities come last.
4867
4868 Value is analogous to strcmp. */
4869
4870
4871 static int
4872 compare_overlay_entries (e1, e2)
4873 void *e1, *e2;
4874 {
4875 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4876 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4877 int result;
4878
4879 if (entry1->after_string_p != entry2->after_string_p)
4880 {
4881 /* Let after-strings appear in front of before-strings if
4882 they come from different overlays. */
4883 if (EQ (entry1->overlay, entry2->overlay))
4884 result = entry1->after_string_p ? 1 : -1;
4885 else
4886 result = entry1->after_string_p ? -1 : 1;
4887 }
4888 else if (entry1->after_string_p)
4889 /* After-strings sorted in order of decreasing priority. */
4890 result = entry2->priority - entry1->priority;
4891 else
4892 /* Before-strings sorted in order of increasing priority. */
4893 result = entry1->priority - entry2->priority;
4894
4895 return result;
4896 }
4897
4898
4899 /* Load the vector IT->overlay_strings with overlay strings from IT's
4900 current buffer position, or from CHARPOS if that is > 0. Set
4901 IT->n_overlays to the total number of overlay strings found.
4902
4903 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4904 a time. On entry into load_overlay_strings,
4905 IT->current.overlay_string_index gives the number of overlay
4906 strings that have already been loaded by previous calls to this
4907 function.
4908
4909 IT->add_overlay_start contains an additional overlay start
4910 position to consider for taking overlay strings from, if non-zero.
4911 This position comes into play when the overlay has an `invisible'
4912 property, and both before and after-strings. When we've skipped to
4913 the end of the overlay, because of its `invisible' property, we
4914 nevertheless want its before-string to appear.
4915 IT->add_overlay_start will contain the overlay start position
4916 in this case.
4917
4918 Overlay strings are sorted so that after-string strings come in
4919 front of before-string strings. Within before and after-strings,
4920 strings are sorted by overlay priority. See also function
4921 compare_overlay_entries. */
4922
4923 static void
4924 load_overlay_strings (it, charpos)
4925 struct it *it;
4926 int charpos;
4927 {
4928 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4929 Lisp_Object overlay, window, str, invisible;
4930 struct Lisp_Overlay *ov;
4931 int start, end;
4932 int size = 20;
4933 int n = 0, i, j, invis_p;
4934 struct overlay_entry *entries
4935 = (struct overlay_entry *) alloca (size * sizeof *entries);
4936
4937 if (charpos <= 0)
4938 charpos = IT_CHARPOS (*it);
4939
4940 /* Append the overlay string STRING of overlay OVERLAY to vector
4941 `entries' which has size `size' and currently contains `n'
4942 elements. AFTER_P non-zero means STRING is an after-string of
4943 OVERLAY. */
4944 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4945 do \
4946 { \
4947 Lisp_Object priority; \
4948 \
4949 if (n == size) \
4950 { \
4951 int new_size = 2 * size; \
4952 struct overlay_entry *old = entries; \
4953 entries = \
4954 (struct overlay_entry *) alloca (new_size \
4955 * sizeof *entries); \
4956 bcopy (old, entries, size * sizeof *entries); \
4957 size = new_size; \
4958 } \
4959 \
4960 entries[n].string = (STRING); \
4961 entries[n].overlay = (OVERLAY); \
4962 priority = Foverlay_get ((OVERLAY), Qpriority); \
4963 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4964 entries[n].after_string_p = (AFTER_P); \
4965 ++n; \
4966 } \
4967 while (0)
4968
4969 /* Process overlay before the overlay center. */
4970 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4971 {
4972 XSETMISC (overlay, ov);
4973 xassert (OVERLAYP (overlay));
4974 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4975 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4976
4977 if (end < charpos)
4978 break;
4979
4980 /* Skip this overlay if it doesn't start or end at IT's current
4981 position. */
4982 if (end != charpos && start != charpos)
4983 continue;
4984
4985 /* Skip this overlay if it doesn't apply to IT->w. */
4986 window = Foverlay_get (overlay, Qwindow);
4987 if (WINDOWP (window) && XWINDOW (window) != it->w)
4988 continue;
4989
4990 /* If the text ``under'' the overlay is invisible, both before-
4991 and after-strings from this overlay are visible; start and
4992 end position are indistinguishable. */
4993 invisible = Foverlay_get (overlay, Qinvisible);
4994 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4995
4996 /* If overlay has a non-empty before-string, record it. */
4997 if ((start == charpos || (end == charpos && invis_p))
4998 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4999 && SCHARS (str))
5000 RECORD_OVERLAY_STRING (overlay, str, 0);
5001
5002 /* If overlay has a non-empty after-string, record it. */
5003 if ((end == charpos || (start == charpos && invis_p))
5004 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5005 && SCHARS (str))
5006 RECORD_OVERLAY_STRING (overlay, str, 1);
5007 }
5008
5009 /* Process overlays after the overlay center. */
5010 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5011 {
5012 XSETMISC (overlay, ov);
5013 xassert (OVERLAYP (overlay));
5014 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5015 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5016
5017 if (start > charpos)
5018 break;
5019
5020 /* Skip this overlay if it doesn't start or end at IT's current
5021 position. */
5022 if (end != charpos && start != charpos)
5023 continue;
5024
5025 /* Skip this overlay if it doesn't apply to IT->w. */
5026 window = Foverlay_get (overlay, Qwindow);
5027 if (WINDOWP (window) && XWINDOW (window) != it->w)
5028 continue;
5029
5030 /* If the text ``under'' the overlay is invisible, it has a zero
5031 dimension, and both before- and after-strings apply. */
5032 invisible = Foverlay_get (overlay, Qinvisible);
5033 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5034
5035 /* If overlay has a non-empty before-string, record it. */
5036 if ((start == charpos || (end == charpos && invis_p))
5037 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5038 && SCHARS (str))
5039 RECORD_OVERLAY_STRING (overlay, str, 0);
5040
5041 /* If overlay has a non-empty after-string, record it. */
5042 if ((end == charpos || (start == charpos && invis_p))
5043 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5044 && SCHARS (str))
5045 RECORD_OVERLAY_STRING (overlay, str, 1);
5046 }
5047
5048 #undef RECORD_OVERLAY_STRING
5049
5050 /* Sort entries. */
5051 if (n > 1)
5052 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5053
5054 /* Record the total number of strings to process. */
5055 it->n_overlay_strings = n;
5056
5057 /* IT->current.overlay_string_index is the number of overlay strings
5058 that have already been consumed by IT. Copy some of the
5059 remaining overlay strings to IT->overlay_strings. */
5060 i = 0;
5061 j = it->current.overlay_string_index;
5062 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5063 {
5064 it->overlay_strings[i] = entries[j].string;
5065 it->string_overlays[i++] = entries[j++].overlay;
5066 }
5067
5068 CHECK_IT (it);
5069 }
5070
5071
5072 /* Get the first chunk of overlay strings at IT's current buffer
5073 position, or at CHARPOS if that is > 0. Value is non-zero if at
5074 least one overlay string was found. */
5075
5076 static int
5077 get_overlay_strings_1 (it, charpos, compute_stop_p)
5078 struct it *it;
5079 int charpos;
5080 int compute_stop_p;
5081 {
5082 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5083 process. This fills IT->overlay_strings with strings, and sets
5084 IT->n_overlay_strings to the total number of strings to process.
5085 IT->pos.overlay_string_index has to be set temporarily to zero
5086 because load_overlay_strings needs this; it must be set to -1
5087 when no overlay strings are found because a zero value would
5088 indicate a position in the first overlay string. */
5089 it->current.overlay_string_index = 0;
5090 load_overlay_strings (it, charpos);
5091
5092 /* If we found overlay strings, set up IT to deliver display
5093 elements from the first one. Otherwise set up IT to deliver
5094 from current_buffer. */
5095 if (it->n_overlay_strings)
5096 {
5097 /* Make sure we know settings in current_buffer, so that we can
5098 restore meaningful values when we're done with the overlay
5099 strings. */
5100 if (compute_stop_p)
5101 compute_stop_pos (it);
5102 xassert (it->face_id >= 0);
5103
5104 /* Save IT's settings. They are restored after all overlay
5105 strings have been processed. */
5106 xassert (!compute_stop_p || it->sp == 0);
5107 push_it (it);
5108
5109 /* Set up IT to deliver display elements from the first overlay
5110 string. */
5111 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5112 it->string = it->overlay_strings[0];
5113 it->from_overlay = Qnil;
5114 it->stop_charpos = 0;
5115 xassert (STRINGP (it->string));
5116 it->end_charpos = SCHARS (it->string);
5117 it->multibyte_p = STRING_MULTIBYTE (it->string);
5118 it->method = GET_FROM_STRING;
5119 return 1;
5120 }
5121
5122 it->current.overlay_string_index = -1;
5123 return 0;
5124 }
5125
5126 static int
5127 get_overlay_strings (it, charpos)
5128 struct it *it;
5129 int charpos;
5130 {
5131 it->string = Qnil;
5132 it->method = GET_FROM_BUFFER;
5133
5134 (void) get_overlay_strings_1 (it, charpos, 1);
5135
5136 CHECK_IT (it);
5137
5138 /* Value is non-zero if we found at least one overlay string. */
5139 return STRINGP (it->string);
5140 }
5141
5142
5143 \f
5144 /***********************************************************************
5145 Saving and restoring state
5146 ***********************************************************************/
5147
5148 /* Save current settings of IT on IT->stack. Called, for example,
5149 before setting up IT for an overlay string, to be able to restore
5150 IT's settings to what they were after the overlay string has been
5151 processed. */
5152
5153 static void
5154 push_it (it)
5155 struct it *it;
5156 {
5157 struct iterator_stack_entry *p;
5158
5159 xassert (it->sp < IT_STACK_SIZE);
5160 p = it->stack + it->sp;
5161
5162 p->stop_charpos = it->stop_charpos;
5163 xassert (it->face_id >= 0);
5164 p->face_id = it->face_id;
5165 p->string = it->string;
5166 p->method = it->method;
5167 p->from_overlay = it->from_overlay;
5168 switch (p->method)
5169 {
5170 case GET_FROM_IMAGE:
5171 p->u.image.object = it->object;
5172 p->u.image.image_id = it->image_id;
5173 p->u.image.slice = it->slice;
5174 break;
5175 case GET_FROM_COMPOSITION:
5176 p->u.comp.object = it->object;
5177 p->u.comp.c = it->c;
5178 p->u.comp.len = it->len;
5179 p->u.comp.cmp_id = it->cmp_id;
5180 p->u.comp.cmp_len = it->cmp_len;
5181 break;
5182 case GET_FROM_STRETCH:
5183 p->u.stretch.object = it->object;
5184 break;
5185 }
5186 p->position = it->position;
5187 p->current = it->current;
5188 p->end_charpos = it->end_charpos;
5189 p->string_nchars = it->string_nchars;
5190 p->area = it->area;
5191 p->multibyte_p = it->multibyte_p;
5192 p->space_width = it->space_width;
5193 p->font_height = it->font_height;
5194 p->voffset = it->voffset;
5195 p->string_from_display_prop_p = it->string_from_display_prop_p;
5196 p->display_ellipsis_p = 0;
5197 ++it->sp;
5198 }
5199
5200
5201 /* Restore IT's settings from IT->stack. Called, for example, when no
5202 more overlay strings must be processed, and we return to delivering
5203 display elements from a buffer, or when the end of a string from a
5204 `display' property is reached and we return to delivering display
5205 elements from an overlay string, or from a buffer. */
5206
5207 static void
5208 pop_it (it)
5209 struct it *it;
5210 {
5211 struct iterator_stack_entry *p;
5212
5213 xassert (it->sp > 0);
5214 --it->sp;
5215 p = it->stack + it->sp;
5216 it->stop_charpos = p->stop_charpos;
5217 it->face_id = p->face_id;
5218 it->current = p->current;
5219 it->position = p->position;
5220 it->string = p->string;
5221 it->from_overlay = p->from_overlay;
5222 if (NILP (it->string))
5223 SET_TEXT_POS (it->current.string_pos, -1, -1);
5224 it->method = p->method;
5225 switch (it->method)
5226 {
5227 case GET_FROM_IMAGE:
5228 it->image_id = p->u.image.image_id;
5229 it->object = p->u.image.object;
5230 it->slice = p->u.image.slice;
5231 break;
5232 case GET_FROM_COMPOSITION:
5233 it->object = p->u.comp.object;
5234 it->c = p->u.comp.c;
5235 it->len = p->u.comp.len;
5236 it->cmp_id = p->u.comp.cmp_id;
5237 it->cmp_len = p->u.comp.cmp_len;
5238 break;
5239 case GET_FROM_STRETCH:
5240 it->object = p->u.comp.object;
5241 break;
5242 case GET_FROM_BUFFER:
5243 it->object = it->w->buffer;
5244 break;
5245 case GET_FROM_STRING:
5246 it->object = it->string;
5247 break;
5248 }
5249 it->end_charpos = p->end_charpos;
5250 it->string_nchars = p->string_nchars;
5251 it->area = p->area;
5252 it->multibyte_p = p->multibyte_p;
5253 it->space_width = p->space_width;
5254 it->font_height = p->font_height;
5255 it->voffset = p->voffset;
5256 it->string_from_display_prop_p = p->string_from_display_prop_p;
5257 }
5258
5259
5260 \f
5261 /***********************************************************************
5262 Moving over lines
5263 ***********************************************************************/
5264
5265 /* Set IT's current position to the previous line start. */
5266
5267 static void
5268 back_to_previous_line_start (it)
5269 struct it *it;
5270 {
5271 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5272 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5273 }
5274
5275
5276 /* Move IT to the next line start.
5277
5278 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5279 we skipped over part of the text (as opposed to moving the iterator
5280 continuously over the text). Otherwise, don't change the value
5281 of *SKIPPED_P.
5282
5283 Newlines may come from buffer text, overlay strings, or strings
5284 displayed via the `display' property. That's the reason we can't
5285 simply use find_next_newline_no_quit.
5286
5287 Note that this function may not skip over invisible text that is so
5288 because of text properties and immediately follows a newline. If
5289 it would, function reseat_at_next_visible_line_start, when called
5290 from set_iterator_to_next, would effectively make invisible
5291 characters following a newline part of the wrong glyph row, which
5292 leads to wrong cursor motion. */
5293
5294 static int
5295 forward_to_next_line_start (it, skipped_p)
5296 struct it *it;
5297 int *skipped_p;
5298 {
5299 int old_selective, newline_found_p, n;
5300 const int MAX_NEWLINE_DISTANCE = 500;
5301
5302 /* If already on a newline, just consume it to avoid unintended
5303 skipping over invisible text below. */
5304 if (it->what == IT_CHARACTER
5305 && it->c == '\n'
5306 && CHARPOS (it->position) == IT_CHARPOS (*it))
5307 {
5308 set_iterator_to_next (it, 0);
5309 it->c = 0;
5310 return 1;
5311 }
5312
5313 /* Don't handle selective display in the following. It's (a)
5314 unnecessary because it's done by the caller, and (b) leads to an
5315 infinite recursion because next_element_from_ellipsis indirectly
5316 calls this function. */
5317 old_selective = it->selective;
5318 it->selective = 0;
5319
5320 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5321 from buffer text. */
5322 for (n = newline_found_p = 0;
5323 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5324 n += STRINGP (it->string) ? 0 : 1)
5325 {
5326 if (!get_next_display_element (it))
5327 return 0;
5328 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5329 set_iterator_to_next (it, 0);
5330 }
5331
5332 /* If we didn't find a newline near enough, see if we can use a
5333 short-cut. */
5334 if (!newline_found_p)
5335 {
5336 int start = IT_CHARPOS (*it);
5337 int limit = find_next_newline_no_quit (start, 1);
5338 Lisp_Object pos;
5339
5340 xassert (!STRINGP (it->string));
5341
5342 /* If there isn't any `display' property in sight, and no
5343 overlays, we can just use the position of the newline in
5344 buffer text. */
5345 if (it->stop_charpos >= limit
5346 || ((pos = Fnext_single_property_change (make_number (start),
5347 Qdisplay,
5348 Qnil, make_number (limit)),
5349 NILP (pos))
5350 && next_overlay_change (start) == ZV))
5351 {
5352 IT_CHARPOS (*it) = limit;
5353 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5354 *skipped_p = newline_found_p = 1;
5355 }
5356 else
5357 {
5358 while (get_next_display_element (it)
5359 && !newline_found_p)
5360 {
5361 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5362 set_iterator_to_next (it, 0);
5363 }
5364 }
5365 }
5366
5367 it->selective = old_selective;
5368 return newline_found_p;
5369 }
5370
5371
5372 /* Set IT's current position to the previous visible line start. Skip
5373 invisible text that is so either due to text properties or due to
5374 selective display. Caution: this does not change IT->current_x and
5375 IT->hpos. */
5376
5377 static void
5378 back_to_previous_visible_line_start (it)
5379 struct it *it;
5380 {
5381 while (IT_CHARPOS (*it) > BEGV)
5382 {
5383 back_to_previous_line_start (it);
5384
5385 if (IT_CHARPOS (*it) <= BEGV)
5386 break;
5387
5388 /* If selective > 0, then lines indented more than that values
5389 are invisible. */
5390 if (it->selective > 0
5391 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5392 (double) it->selective)) /* iftc */
5393 continue;
5394
5395 /* Check the newline before point for invisibility. */
5396 {
5397 Lisp_Object prop;
5398 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5399 Qinvisible, it->window);
5400 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5401 continue;
5402 }
5403
5404 if (IT_CHARPOS (*it) <= BEGV)
5405 break;
5406
5407 {
5408 struct it it2;
5409 int pos;
5410 EMACS_INT beg, end;
5411 Lisp_Object val, overlay;
5412
5413 /* If newline is part of a composition, continue from start of composition */
5414 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5415 && beg < IT_CHARPOS (*it))
5416 goto replaced;
5417
5418 /* If newline is replaced by a display property, find start of overlay
5419 or interval and continue search from that point. */
5420 it2 = *it;
5421 pos = --IT_CHARPOS (it2);
5422 --IT_BYTEPOS (it2);
5423 it2.sp = 0;
5424 if (handle_display_prop (&it2) == HANDLED_RETURN
5425 && !NILP (val = get_char_property_and_overlay
5426 (make_number (pos), Qdisplay, Qnil, &overlay))
5427 && (OVERLAYP (overlay)
5428 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5429 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5430 goto replaced;
5431
5432 /* Newline is not replaced by anything -- so we are done. */
5433 break;
5434
5435 replaced:
5436 if (beg < BEGV)
5437 beg = BEGV;
5438 IT_CHARPOS (*it) = beg;
5439 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5440 }
5441 }
5442
5443 it->continuation_lines_width = 0;
5444
5445 xassert (IT_CHARPOS (*it) >= BEGV);
5446 xassert (IT_CHARPOS (*it) == BEGV
5447 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5448 CHECK_IT (it);
5449 }
5450
5451
5452 /* Reseat iterator IT at the previous visible line start. Skip
5453 invisible text that is so either due to text properties or due to
5454 selective display. At the end, update IT's overlay information,
5455 face information etc. */
5456
5457 void
5458 reseat_at_previous_visible_line_start (it)
5459 struct it *it;
5460 {
5461 back_to_previous_visible_line_start (it);
5462 reseat (it, it->current.pos, 1);
5463 CHECK_IT (it);
5464 }
5465
5466
5467 /* Reseat iterator IT on the next visible line start in the current
5468 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5469 preceding the line start. Skip over invisible text that is so
5470 because of selective display. Compute faces, overlays etc at the
5471 new position. Note that this function does not skip over text that
5472 is invisible because of text properties. */
5473
5474 static void
5475 reseat_at_next_visible_line_start (it, on_newline_p)
5476 struct it *it;
5477 int on_newline_p;
5478 {
5479 int newline_found_p, skipped_p = 0;
5480
5481 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5482
5483 /* Skip over lines that are invisible because they are indented
5484 more than the value of IT->selective. */
5485 if (it->selective > 0)
5486 while (IT_CHARPOS (*it) < ZV
5487 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5488 (double) it->selective)) /* iftc */
5489 {
5490 xassert (IT_BYTEPOS (*it) == BEGV
5491 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5492 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5493 }
5494
5495 /* Position on the newline if that's what's requested. */
5496 if (on_newline_p && newline_found_p)
5497 {
5498 if (STRINGP (it->string))
5499 {
5500 if (IT_STRING_CHARPOS (*it) > 0)
5501 {
5502 --IT_STRING_CHARPOS (*it);
5503 --IT_STRING_BYTEPOS (*it);
5504 }
5505 }
5506 else if (IT_CHARPOS (*it) > BEGV)
5507 {
5508 --IT_CHARPOS (*it);
5509 --IT_BYTEPOS (*it);
5510 reseat (it, it->current.pos, 0);
5511 }
5512 }
5513 else if (skipped_p)
5514 reseat (it, it->current.pos, 0);
5515
5516 CHECK_IT (it);
5517 }
5518
5519
5520 \f
5521 /***********************************************************************
5522 Changing an iterator's position
5523 ***********************************************************************/
5524
5525 /* Change IT's current position to POS in current_buffer. If FORCE_P
5526 is non-zero, always check for text properties at the new position.
5527 Otherwise, text properties are only looked up if POS >=
5528 IT->check_charpos of a property. */
5529
5530 static void
5531 reseat (it, pos, force_p)
5532 struct it *it;
5533 struct text_pos pos;
5534 int force_p;
5535 {
5536 int original_pos = IT_CHARPOS (*it);
5537
5538 reseat_1 (it, pos, 0);
5539
5540 /* Determine where to check text properties. Avoid doing it
5541 where possible because text property lookup is very expensive. */
5542 if (force_p
5543 || CHARPOS (pos) > it->stop_charpos
5544 || CHARPOS (pos) < original_pos)
5545 handle_stop (it);
5546
5547 CHECK_IT (it);
5548 }
5549
5550
5551 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5552 IT->stop_pos to POS, also. */
5553
5554 static void
5555 reseat_1 (it, pos, set_stop_p)
5556 struct it *it;
5557 struct text_pos pos;
5558 int set_stop_p;
5559 {
5560 /* Don't call this function when scanning a C string. */
5561 xassert (it->s == NULL);
5562
5563 /* POS must be a reasonable value. */
5564 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5565
5566 it->current.pos = it->position = pos;
5567 it->end_charpos = ZV;
5568 it->dpvec = NULL;
5569 it->current.dpvec_index = -1;
5570 it->current.overlay_string_index = -1;
5571 IT_STRING_CHARPOS (*it) = -1;
5572 IT_STRING_BYTEPOS (*it) = -1;
5573 it->string = Qnil;
5574 it->method = GET_FROM_BUFFER;
5575 it->object = it->w->buffer;
5576 it->area = TEXT_AREA;
5577 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5578 it->sp = 0;
5579 it->string_from_display_prop_p = 0;
5580 it->face_before_selective_p = 0;
5581
5582 if (set_stop_p)
5583 it->stop_charpos = CHARPOS (pos);
5584 }
5585
5586
5587 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5588 If S is non-null, it is a C string to iterate over. Otherwise,
5589 STRING gives a Lisp string to iterate over.
5590
5591 If PRECISION > 0, don't return more then PRECISION number of
5592 characters from the string.
5593
5594 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5595 characters have been returned. FIELD_WIDTH < 0 means an infinite
5596 field width.
5597
5598 MULTIBYTE = 0 means disable processing of multibyte characters,
5599 MULTIBYTE > 0 means enable it,
5600 MULTIBYTE < 0 means use IT->multibyte_p.
5601
5602 IT must be initialized via a prior call to init_iterator before
5603 calling this function. */
5604
5605 static void
5606 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5607 struct it *it;
5608 unsigned char *s;
5609 Lisp_Object string;
5610 int charpos;
5611 int precision, field_width, multibyte;
5612 {
5613 /* No region in strings. */
5614 it->region_beg_charpos = it->region_end_charpos = -1;
5615
5616 /* No text property checks performed by default, but see below. */
5617 it->stop_charpos = -1;
5618
5619 /* Set iterator position and end position. */
5620 bzero (&it->current, sizeof it->current);
5621 it->current.overlay_string_index = -1;
5622 it->current.dpvec_index = -1;
5623 xassert (charpos >= 0);
5624
5625 /* If STRING is specified, use its multibyteness, otherwise use the
5626 setting of MULTIBYTE, if specified. */
5627 if (multibyte >= 0)
5628 it->multibyte_p = multibyte > 0;
5629
5630 if (s == NULL)
5631 {
5632 xassert (STRINGP (string));
5633 it->string = string;
5634 it->s = NULL;
5635 it->end_charpos = it->string_nchars = SCHARS (string);
5636 it->method = GET_FROM_STRING;
5637 it->current.string_pos = string_pos (charpos, string);
5638 }
5639 else
5640 {
5641 it->s = s;
5642 it->string = Qnil;
5643
5644 /* Note that we use IT->current.pos, not it->current.string_pos,
5645 for displaying C strings. */
5646 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5647 if (it->multibyte_p)
5648 {
5649 it->current.pos = c_string_pos (charpos, s, 1);
5650 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5651 }
5652 else
5653 {
5654 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5655 it->end_charpos = it->string_nchars = strlen (s);
5656 }
5657
5658 it->method = GET_FROM_C_STRING;
5659 }
5660
5661 /* PRECISION > 0 means don't return more than PRECISION characters
5662 from the string. */
5663 if (precision > 0 && it->end_charpos - charpos > precision)
5664 it->end_charpos = it->string_nchars = charpos + precision;
5665
5666 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5667 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5668 FIELD_WIDTH < 0 means infinite field width. This is useful for
5669 padding with `-' at the end of a mode line. */
5670 if (field_width < 0)
5671 field_width = INFINITY;
5672 if (field_width > it->end_charpos - charpos)
5673 it->end_charpos = charpos + field_width;
5674
5675 /* Use the standard display table for displaying strings. */
5676 if (DISP_TABLE_P (Vstandard_display_table))
5677 it->dp = XCHAR_TABLE (Vstandard_display_table);
5678
5679 it->stop_charpos = charpos;
5680 CHECK_IT (it);
5681 }
5682
5683
5684 \f
5685 /***********************************************************************
5686 Iteration
5687 ***********************************************************************/
5688
5689 /* Map enum it_method value to corresponding next_element_from_* function. */
5690
5691 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5692 {
5693 next_element_from_buffer,
5694 next_element_from_display_vector,
5695 next_element_from_composition,
5696 next_element_from_string,
5697 next_element_from_c_string,
5698 next_element_from_image,
5699 next_element_from_stretch
5700 };
5701
5702 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5703
5704 /* Load IT's display element fields with information about the next
5705 display element from the current position of IT. Value is zero if
5706 end of buffer (or C string) is reached. */
5707
5708 static struct frame *last_escape_glyph_frame = NULL;
5709 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5710 static int last_escape_glyph_merged_face_id = 0;
5711
5712 int
5713 get_next_display_element (it)
5714 struct it *it;
5715 {
5716 /* Non-zero means that we found a display element. Zero means that
5717 we hit the end of what we iterate over. Performance note: the
5718 function pointer `method' used here turns out to be faster than
5719 using a sequence of if-statements. */
5720 int success_p;
5721
5722 get_next:
5723 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5724
5725 if (it->what == IT_CHARACTER)
5726 {
5727 /* Map via display table or translate control characters.
5728 IT->c, IT->len etc. have been set to the next character by
5729 the function call above. If we have a display table, and it
5730 contains an entry for IT->c, translate it. Don't do this if
5731 IT->c itself comes from a display table, otherwise we could
5732 end up in an infinite recursion. (An alternative could be to
5733 count the recursion depth of this function and signal an
5734 error when a certain maximum depth is reached.) Is it worth
5735 it? */
5736 if (success_p && it->dpvec == NULL)
5737 {
5738 Lisp_Object dv;
5739
5740 if (it->dp
5741 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5742 VECTORP (dv)))
5743 {
5744 struct Lisp_Vector *v = XVECTOR (dv);
5745
5746 /* Return the first character from the display table
5747 entry, if not empty. If empty, don't display the
5748 current character. */
5749 if (v->size)
5750 {
5751 it->dpvec_char_len = it->len;
5752 it->dpvec = v->contents;
5753 it->dpend = v->contents + v->size;
5754 it->current.dpvec_index = 0;
5755 it->dpvec_face_id = -1;
5756 it->saved_face_id = it->face_id;
5757 it->method = GET_FROM_DISPLAY_VECTOR;
5758 it->ellipsis_p = 0;
5759 }
5760 else
5761 {
5762 set_iterator_to_next (it, 0);
5763 }
5764 goto get_next;
5765 }
5766
5767 /* Translate control characters into `\003' or `^C' form.
5768 Control characters coming from a display table entry are
5769 currently not translated because we use IT->dpvec to hold
5770 the translation. This could easily be changed but I
5771 don't believe that it is worth doing.
5772
5773 If it->multibyte_p is nonzero, non-printable non-ASCII
5774 characters are also translated to octal form.
5775
5776 If it->multibyte_p is zero, eight-bit characters that
5777 don't have corresponding multibyte char code are also
5778 translated to octal form. */
5779 else if ((it->c < ' '
5780 ? (it->area != TEXT_AREA
5781 /* In mode line, treat \n, \t like other crl chars. */
5782 || (it->c != '\t'
5783 && it->glyph_row && it->glyph_row->mode_line_p)
5784 || (it->c != '\n' && it->c != '\t'))
5785 : (it->multibyte_p
5786 ? (!CHAR_PRINTABLE_P (it->c)
5787 || (!NILP (Vnobreak_char_display)
5788 && (it->c == 0xA0 /* NO-BREAK SPACE */
5789 || it->c == 0xAD /* SOFT HYPHEN */)))
5790 : (it->c >= 127
5791 && (! unibyte_display_via_language_environment
5792 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5793 {
5794 /* IT->c is a control character which must be displayed
5795 either as '\003' or as `^C' where the '\\' and '^'
5796 can be defined in the display table. Fill
5797 IT->ctl_chars with glyphs for what we have to
5798 display. Then, set IT->dpvec to these glyphs. */
5799 Lisp_Object gc;
5800 int ctl_len;
5801 int face_id, lface_id = 0 ;
5802 int escape_glyph;
5803
5804 /* Handle control characters with ^. */
5805
5806 if (it->c < 128 && it->ctl_arrow_p)
5807 {
5808 int g;
5809
5810 g = '^'; /* default glyph for Control */
5811 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5812 if (it->dp
5813 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5814 && GLYPH_CODE_CHAR_VALID_P (gc))
5815 {
5816 g = GLYPH_CODE_CHAR (gc);
5817 lface_id = GLYPH_CODE_FACE (gc);
5818 }
5819 if (lface_id)
5820 {
5821 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5822 }
5823 else if (it->f == last_escape_glyph_frame
5824 && it->face_id == last_escape_glyph_face_id)
5825 {
5826 face_id = last_escape_glyph_merged_face_id;
5827 }
5828 else
5829 {
5830 /* Merge the escape-glyph face into the current face. */
5831 face_id = merge_faces (it->f, Qescape_glyph, 0,
5832 it->face_id);
5833 last_escape_glyph_frame = it->f;
5834 last_escape_glyph_face_id = it->face_id;
5835 last_escape_glyph_merged_face_id = face_id;
5836 }
5837
5838 XSETINT (it->ctl_chars[0], g);
5839 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5840 ctl_len = 2;
5841 goto display_control;
5842 }
5843
5844 /* Handle non-break space in the mode where it only gets
5845 highlighting. */
5846
5847 if (EQ (Vnobreak_char_display, Qt)
5848 && it->c == 0xA0)
5849 {
5850 /* Merge the no-break-space face into the current face. */
5851 face_id = merge_faces (it->f, Qnobreak_space, 0,
5852 it->face_id);
5853
5854 it->c = ' ';
5855 XSETINT (it->ctl_chars[0], ' ');
5856 ctl_len = 1;
5857 goto display_control;
5858 }
5859
5860 /* Handle sequences that start with the "escape glyph". */
5861
5862 /* the default escape glyph is \. */
5863 escape_glyph = '\\';
5864
5865 if (it->dp
5866 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5867 && GLYPH_CODE_CHAR_VALID_P (gc))
5868 {
5869 escape_glyph = GLYPH_CODE_CHAR (gc);
5870 lface_id = GLYPH_CODE_FACE (gc);
5871 }
5872 if (lface_id)
5873 {
5874 /* The display table specified a face.
5875 Merge it into face_id and also into escape_glyph. */
5876 face_id = merge_faces (it->f, Qt, lface_id,
5877 it->face_id);
5878 }
5879 else if (it->f == last_escape_glyph_frame
5880 && it->face_id == last_escape_glyph_face_id)
5881 {
5882 face_id = last_escape_glyph_merged_face_id;
5883 }
5884 else
5885 {
5886 /* Merge the escape-glyph face into the current face. */
5887 face_id = merge_faces (it->f, Qescape_glyph, 0,
5888 it->face_id);
5889 last_escape_glyph_frame = it->f;
5890 last_escape_glyph_face_id = it->face_id;
5891 last_escape_glyph_merged_face_id = face_id;
5892 }
5893
5894 /* Handle soft hyphens in the mode where they only get
5895 highlighting. */
5896
5897 if (EQ (Vnobreak_char_display, Qt)
5898 && it->c == 0xAD)
5899 {
5900 it->c = '-';
5901 XSETINT (it->ctl_chars[0], '-');
5902 ctl_len = 1;
5903 goto display_control;
5904 }
5905
5906 /* Handle non-break space and soft hyphen
5907 with the escape glyph. */
5908
5909 if (it->c == 0xA0 || it->c == 0xAD)
5910 {
5911 XSETINT (it->ctl_chars[0], escape_glyph);
5912 it->c = (it->c == 0xA0 ? ' ' : '-');
5913 XSETINT (it->ctl_chars[1], it->c);
5914 ctl_len = 2;
5915 goto display_control;
5916 }
5917
5918 {
5919 unsigned char str[MAX_MULTIBYTE_LENGTH];
5920 int len;
5921 int i;
5922
5923 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5924 if (CHAR_BYTE8_P (it->c))
5925 {
5926 str[0] = CHAR_TO_BYTE8 (it->c);
5927 len = 1;
5928 }
5929 else if (it->c < 256)
5930 {
5931 str[0] = it->c;
5932 len = 1;
5933 }
5934 else
5935 {
5936 /* It's an invalid character, which shouldn't
5937 happen actually, but due to bugs it may
5938 happen. Let's print the char as is, there's
5939 not much meaningful we can do with it. */
5940 str[0] = it->c;
5941 str[1] = it->c >> 8;
5942 str[2] = it->c >> 16;
5943 str[3] = it->c >> 24;
5944 len = 4;
5945 }
5946
5947 for (i = 0; i < len; i++)
5948 {
5949 int g;
5950 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5951 /* Insert three more glyphs into IT->ctl_chars for
5952 the octal display of the character. */
5953 g = ((str[i] >> 6) & 7) + '0';
5954 XSETINT (it->ctl_chars[i * 4 + 1], g);
5955 g = ((str[i] >> 3) & 7) + '0';
5956 XSETINT (it->ctl_chars[i * 4 + 2], g);
5957 g = (str[i] & 7) + '0';
5958 XSETINT (it->ctl_chars[i * 4 + 3], g);
5959 }
5960 ctl_len = len * 4;
5961 }
5962
5963 display_control:
5964 /* Set up IT->dpvec and return first character from it. */
5965 it->dpvec_char_len = it->len;
5966 it->dpvec = it->ctl_chars;
5967 it->dpend = it->dpvec + ctl_len;
5968 it->current.dpvec_index = 0;
5969 it->dpvec_face_id = face_id;
5970 it->saved_face_id = it->face_id;
5971 it->method = GET_FROM_DISPLAY_VECTOR;
5972 it->ellipsis_p = 0;
5973 goto get_next;
5974 }
5975 }
5976 }
5977
5978 /* Adjust face id for a multibyte character. There are no multibyte
5979 character in unibyte text. */
5980 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5981 && it->multibyte_p
5982 && success_p
5983 && FRAME_WINDOW_P (it->f))
5984 {
5985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5986 int pos = (it->s ? -1
5987 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5988 : IT_CHARPOS (*it));
5989
5990 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5991 }
5992
5993 /* Is this character the last one of a run of characters with
5994 box? If yes, set IT->end_of_box_run_p to 1. */
5995 if (it->face_box_p
5996 && it->s == NULL)
5997 {
5998 int face_id;
5999 struct face *face;
6000
6001 it->end_of_box_run_p
6002 = ((face_id = face_after_it_pos (it),
6003 face_id != it->face_id)
6004 && (face = FACE_FROM_ID (it->f, face_id),
6005 face->box == FACE_NO_BOX));
6006 }
6007
6008 /* Value is 0 if end of buffer or string reached. */
6009 return success_p;
6010 }
6011
6012
6013 /* Move IT to the next display element.
6014
6015 RESEAT_P non-zero means if called on a newline in buffer text,
6016 skip to the next visible line start.
6017
6018 Functions get_next_display_element and set_iterator_to_next are
6019 separate because I find this arrangement easier to handle than a
6020 get_next_display_element function that also increments IT's
6021 position. The way it is we can first look at an iterator's current
6022 display element, decide whether it fits on a line, and if it does,
6023 increment the iterator position. The other way around we probably
6024 would either need a flag indicating whether the iterator has to be
6025 incremented the next time, or we would have to implement a
6026 decrement position function which would not be easy to write. */
6027
6028 void
6029 set_iterator_to_next (it, reseat_p)
6030 struct it *it;
6031 int reseat_p;
6032 {
6033 /* Reset flags indicating start and end of a sequence of characters
6034 with box. Reset them at the start of this function because
6035 moving the iterator to a new position might set them. */
6036 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6037
6038 switch (it->method)
6039 {
6040 case GET_FROM_BUFFER:
6041 /* The current display element of IT is a character from
6042 current_buffer. Advance in the buffer, and maybe skip over
6043 invisible lines that are so because of selective display. */
6044 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6045 reseat_at_next_visible_line_start (it, 0);
6046 else
6047 {
6048 xassert (it->len != 0);
6049 IT_BYTEPOS (*it) += it->len;
6050 IT_CHARPOS (*it) += 1;
6051 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6052 }
6053 break;
6054
6055 case GET_FROM_COMPOSITION:
6056 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6057 xassert (it->sp > 0);
6058 pop_it (it);
6059 if (it->method == GET_FROM_STRING)
6060 {
6061 IT_STRING_BYTEPOS (*it) += it->len;
6062 IT_STRING_CHARPOS (*it) += it->cmp_len;
6063 goto consider_string_end;
6064 }
6065 else if (it->method == GET_FROM_BUFFER)
6066 {
6067 IT_BYTEPOS (*it) += it->len;
6068 IT_CHARPOS (*it) += it->cmp_len;
6069 }
6070 break;
6071
6072 case GET_FROM_C_STRING:
6073 /* Current display element of IT is from a C string. */
6074 IT_BYTEPOS (*it) += it->len;
6075 IT_CHARPOS (*it) += 1;
6076 break;
6077
6078 case GET_FROM_DISPLAY_VECTOR:
6079 /* Current display element of IT is from a display table entry.
6080 Advance in the display table definition. Reset it to null if
6081 end reached, and continue with characters from buffers/
6082 strings. */
6083 ++it->current.dpvec_index;
6084
6085 /* Restore face of the iterator to what they were before the
6086 display vector entry (these entries may contain faces). */
6087 it->face_id = it->saved_face_id;
6088
6089 if (it->dpvec + it->current.dpvec_index == it->dpend)
6090 {
6091 int recheck_faces = it->ellipsis_p;
6092
6093 if (it->s)
6094 it->method = GET_FROM_C_STRING;
6095 else if (STRINGP (it->string))
6096 it->method = GET_FROM_STRING;
6097 else
6098 {
6099 it->method = GET_FROM_BUFFER;
6100 it->object = it->w->buffer;
6101 }
6102
6103 it->dpvec = NULL;
6104 it->current.dpvec_index = -1;
6105
6106 /* Skip over characters which were displayed via IT->dpvec. */
6107 if (it->dpvec_char_len < 0)
6108 reseat_at_next_visible_line_start (it, 1);
6109 else if (it->dpvec_char_len > 0)
6110 {
6111 if (it->method == GET_FROM_STRING
6112 && it->n_overlay_strings > 0)
6113 it->ignore_overlay_strings_at_pos_p = 1;
6114 it->len = it->dpvec_char_len;
6115 set_iterator_to_next (it, reseat_p);
6116 }
6117
6118 /* Maybe recheck faces after display vector */
6119 if (recheck_faces)
6120 it->stop_charpos = IT_CHARPOS (*it);
6121 }
6122 break;
6123
6124 case GET_FROM_STRING:
6125 /* Current display element is a character from a Lisp string. */
6126 xassert (it->s == NULL && STRINGP (it->string));
6127 IT_STRING_BYTEPOS (*it) += it->len;
6128 IT_STRING_CHARPOS (*it) += 1;
6129
6130 consider_string_end:
6131
6132 if (it->current.overlay_string_index >= 0)
6133 {
6134 /* IT->string is an overlay string. Advance to the
6135 next, if there is one. */
6136 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6137 next_overlay_string (it);
6138 }
6139 else
6140 {
6141 /* IT->string is not an overlay string. If we reached
6142 its end, and there is something on IT->stack, proceed
6143 with what is on the stack. This can be either another
6144 string, this time an overlay string, or a buffer. */
6145 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6146 && it->sp > 0)
6147 {
6148 pop_it (it);
6149 if (it->method == GET_FROM_STRING)
6150 goto consider_string_end;
6151 }
6152 }
6153 break;
6154
6155 case GET_FROM_IMAGE:
6156 case GET_FROM_STRETCH:
6157 /* The position etc with which we have to proceed are on
6158 the stack. The position may be at the end of a string,
6159 if the `display' property takes up the whole string. */
6160 xassert (it->sp > 0);
6161 pop_it (it);
6162 if (it->method == GET_FROM_STRING)
6163 goto consider_string_end;
6164 break;
6165
6166 default:
6167 /* There are no other methods defined, so this should be a bug. */
6168 abort ();
6169 }
6170
6171 xassert (it->method != GET_FROM_STRING
6172 || (STRINGP (it->string)
6173 && IT_STRING_CHARPOS (*it) >= 0));
6174 }
6175
6176 /* Load IT's display element fields with information about the next
6177 display element which comes from a display table entry or from the
6178 result of translating a control character to one of the forms `^C'
6179 or `\003'.
6180
6181 IT->dpvec holds the glyphs to return as characters.
6182 IT->saved_face_id holds the face id before the display vector--
6183 it is restored into IT->face_idin set_iterator_to_next. */
6184
6185 static int
6186 next_element_from_display_vector (it)
6187 struct it *it;
6188 {
6189 Lisp_Object gc;
6190
6191 /* Precondition. */
6192 xassert (it->dpvec && it->current.dpvec_index >= 0);
6193
6194 it->face_id = it->saved_face_id;
6195
6196 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6197 That seemed totally bogus - so I changed it... */
6198
6199 if ((gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc))
6200 && GLYPH_CODE_CHAR_VALID_P (gc))
6201 {
6202 it->c = GLYPH_CODE_CHAR (gc);
6203 it->len = CHAR_BYTES (it->c);
6204
6205 /* The entry may contain a face id to use. Such a face id is
6206 the id of a Lisp face, not a realized face. A face id of
6207 zero means no face is specified. */
6208 if (it->dpvec_face_id >= 0)
6209 it->face_id = it->dpvec_face_id;
6210 else
6211 {
6212 int lface_id = GLYPH_CODE_FACE (gc);
6213 if (lface_id > 0)
6214 it->face_id = merge_faces (it->f, Qt, lface_id,
6215 it->saved_face_id);
6216 }
6217 }
6218 else
6219 /* Display table entry is invalid. Return a space. */
6220 it->c = ' ', it->len = 1;
6221
6222 /* Don't change position and object of the iterator here. They are
6223 still the values of the character that had this display table
6224 entry or was translated, and that's what we want. */
6225 it->what = IT_CHARACTER;
6226 return 1;
6227 }
6228
6229
6230 /* Load IT with the next display element from Lisp string IT->string.
6231 IT->current.string_pos is the current position within the string.
6232 If IT->current.overlay_string_index >= 0, the Lisp string is an
6233 overlay string. */
6234
6235 static int
6236 next_element_from_string (it)
6237 struct it *it;
6238 {
6239 struct text_pos position;
6240
6241 xassert (STRINGP (it->string));
6242 xassert (IT_STRING_CHARPOS (*it) >= 0);
6243 position = it->current.string_pos;
6244
6245 /* Time to check for invisible text? */
6246 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6247 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6248 {
6249 handle_stop (it);
6250
6251 /* Since a handler may have changed IT->method, we must
6252 recurse here. */
6253 return GET_NEXT_DISPLAY_ELEMENT (it);
6254 }
6255
6256 if (it->current.overlay_string_index >= 0)
6257 {
6258 /* Get the next character from an overlay string. In overlay
6259 strings, There is no field width or padding with spaces to
6260 do. */
6261 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6262 {
6263 it->what = IT_EOB;
6264 return 0;
6265 }
6266 else if (STRING_MULTIBYTE (it->string))
6267 {
6268 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6269 const unsigned char *s = (SDATA (it->string)
6270 + IT_STRING_BYTEPOS (*it));
6271 it->c = string_char_and_length (s, remaining, &it->len);
6272 }
6273 else
6274 {
6275 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6276 it->len = 1;
6277 }
6278 }
6279 else
6280 {
6281 /* Get the next character from a Lisp string that is not an
6282 overlay string. Such strings come from the mode line, for
6283 example. We may have to pad with spaces, or truncate the
6284 string. See also next_element_from_c_string. */
6285 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6286 {
6287 it->what = IT_EOB;
6288 return 0;
6289 }
6290 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6291 {
6292 /* Pad with spaces. */
6293 it->c = ' ', it->len = 1;
6294 CHARPOS (position) = BYTEPOS (position) = -1;
6295 }
6296 else if (STRING_MULTIBYTE (it->string))
6297 {
6298 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6299 const unsigned char *s = (SDATA (it->string)
6300 + IT_STRING_BYTEPOS (*it));
6301 it->c = string_char_and_length (s, maxlen, &it->len);
6302 }
6303 else
6304 {
6305 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6306 it->len = 1;
6307 }
6308 }
6309
6310 /* Record what we have and where it came from. */
6311 it->what = IT_CHARACTER;
6312 it->object = it->string;
6313 it->position = position;
6314 return 1;
6315 }
6316
6317
6318 /* Load IT with next display element from C string IT->s.
6319 IT->string_nchars is the maximum number of characters to return
6320 from the string. IT->end_charpos may be greater than
6321 IT->string_nchars when this function is called, in which case we
6322 may have to return padding spaces. Value is zero if end of string
6323 reached, including padding spaces. */
6324
6325 static int
6326 next_element_from_c_string (it)
6327 struct it *it;
6328 {
6329 int success_p = 1;
6330
6331 xassert (it->s);
6332 it->what = IT_CHARACTER;
6333 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6334 it->object = Qnil;
6335
6336 /* IT's position can be greater IT->string_nchars in case a field
6337 width or precision has been specified when the iterator was
6338 initialized. */
6339 if (IT_CHARPOS (*it) >= it->end_charpos)
6340 {
6341 /* End of the game. */
6342 it->what = IT_EOB;
6343 success_p = 0;
6344 }
6345 else if (IT_CHARPOS (*it) >= it->string_nchars)
6346 {
6347 /* Pad with spaces. */
6348 it->c = ' ', it->len = 1;
6349 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6350 }
6351 else if (it->multibyte_p)
6352 {
6353 /* Implementation note: The calls to strlen apparently aren't a
6354 performance problem because there is no noticeable performance
6355 difference between Emacs running in unibyte or multibyte mode. */
6356 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6357 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6358 maxlen, &it->len);
6359 }
6360 else
6361 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6362
6363 return success_p;
6364 }
6365
6366
6367 /* Set up IT to return characters from an ellipsis, if appropriate.
6368 The definition of the ellipsis glyphs may come from a display table
6369 entry. This function Fills IT with the first glyph from the
6370 ellipsis if an ellipsis is to be displayed. */
6371
6372 static int
6373 next_element_from_ellipsis (it)
6374 struct it *it;
6375 {
6376 if (it->selective_display_ellipsis_p)
6377 setup_for_ellipsis (it, it->len);
6378 else
6379 {
6380 /* The face at the current position may be different from the
6381 face we find after the invisible text. Remember what it
6382 was in IT->saved_face_id, and signal that it's there by
6383 setting face_before_selective_p. */
6384 it->saved_face_id = it->face_id;
6385 it->method = GET_FROM_BUFFER;
6386 it->object = it->w->buffer;
6387 reseat_at_next_visible_line_start (it, 1);
6388 it->face_before_selective_p = 1;
6389 }
6390
6391 return GET_NEXT_DISPLAY_ELEMENT (it);
6392 }
6393
6394
6395 /* Deliver an image display element. The iterator IT is already
6396 filled with image information (done in handle_display_prop). Value
6397 is always 1. */
6398
6399
6400 static int
6401 next_element_from_image (it)
6402 struct it *it;
6403 {
6404 it->what = IT_IMAGE;
6405 return 1;
6406 }
6407
6408
6409 /* Fill iterator IT with next display element from a stretch glyph
6410 property. IT->object is the value of the text property. Value is
6411 always 1. */
6412
6413 static int
6414 next_element_from_stretch (it)
6415 struct it *it;
6416 {
6417 it->what = IT_STRETCH;
6418 return 1;
6419 }
6420
6421
6422 /* Load IT with the next display element from current_buffer. Value
6423 is zero if end of buffer reached. IT->stop_charpos is the next
6424 position at which to stop and check for text properties or buffer
6425 end. */
6426
6427 static int
6428 next_element_from_buffer (it)
6429 struct it *it;
6430 {
6431 int success_p = 1;
6432
6433 /* Check this assumption, otherwise, we would never enter the
6434 if-statement, below. */
6435 xassert (IT_CHARPOS (*it) >= BEGV
6436 && IT_CHARPOS (*it) <= it->stop_charpos);
6437
6438 if (IT_CHARPOS (*it) >= it->stop_charpos)
6439 {
6440 if (IT_CHARPOS (*it) >= it->end_charpos)
6441 {
6442 int overlay_strings_follow_p;
6443
6444 /* End of the game, except when overlay strings follow that
6445 haven't been returned yet. */
6446 if (it->overlay_strings_at_end_processed_p)
6447 overlay_strings_follow_p = 0;
6448 else
6449 {
6450 it->overlay_strings_at_end_processed_p = 1;
6451 overlay_strings_follow_p = get_overlay_strings (it, 0);
6452 }
6453
6454 if (overlay_strings_follow_p)
6455 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6456 else
6457 {
6458 it->what = IT_EOB;
6459 it->position = it->current.pos;
6460 success_p = 0;
6461 }
6462 }
6463 else
6464 {
6465 handle_stop (it);
6466 return GET_NEXT_DISPLAY_ELEMENT (it);
6467 }
6468 }
6469 else
6470 {
6471 /* No face changes, overlays etc. in sight, so just return a
6472 character from current_buffer. */
6473 unsigned char *p;
6474
6475 /* Maybe run the redisplay end trigger hook. Performance note:
6476 This doesn't seem to cost measurable time. */
6477 if (it->redisplay_end_trigger_charpos
6478 && it->glyph_row
6479 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6480 run_redisplay_end_trigger_hook (it);
6481
6482 /* Get the next character, maybe multibyte. */
6483 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6484 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6485 {
6486 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6487 - IT_BYTEPOS (*it));
6488 it->c = string_char_and_length (p, maxlen, &it->len);
6489 }
6490 else
6491 it->c = *p, it->len = 1;
6492
6493 /* Record what we have and where it came from. */
6494 it->what = IT_CHARACTER;
6495 it->object = it->w->buffer;
6496 it->position = it->current.pos;
6497
6498 /* Normally we return the character found above, except when we
6499 really want to return an ellipsis for selective display. */
6500 if (it->selective)
6501 {
6502 if (it->c == '\n')
6503 {
6504 /* A value of selective > 0 means hide lines indented more
6505 than that number of columns. */
6506 if (it->selective > 0
6507 && IT_CHARPOS (*it) + 1 < ZV
6508 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6509 IT_BYTEPOS (*it) + 1,
6510 (double) it->selective)) /* iftc */
6511 {
6512 success_p = next_element_from_ellipsis (it);
6513 it->dpvec_char_len = -1;
6514 }
6515 }
6516 else if (it->c == '\r' && it->selective == -1)
6517 {
6518 /* A value of selective == -1 means that everything from the
6519 CR to the end of the line is invisible, with maybe an
6520 ellipsis displayed for it. */
6521 success_p = next_element_from_ellipsis (it);
6522 it->dpvec_char_len = -1;
6523 }
6524 }
6525 }
6526
6527 /* Value is zero if end of buffer reached. */
6528 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6529 return success_p;
6530 }
6531
6532
6533 /* Run the redisplay end trigger hook for IT. */
6534
6535 static void
6536 run_redisplay_end_trigger_hook (it)
6537 struct it *it;
6538 {
6539 Lisp_Object args[3];
6540
6541 /* IT->glyph_row should be non-null, i.e. we should be actually
6542 displaying something, or otherwise we should not run the hook. */
6543 xassert (it->glyph_row);
6544
6545 /* Set up hook arguments. */
6546 args[0] = Qredisplay_end_trigger_functions;
6547 args[1] = it->window;
6548 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6549 it->redisplay_end_trigger_charpos = 0;
6550
6551 /* Since we are *trying* to run these functions, don't try to run
6552 them again, even if they get an error. */
6553 it->w->redisplay_end_trigger = Qnil;
6554 Frun_hook_with_args (3, args);
6555
6556 /* Notice if it changed the face of the character we are on. */
6557 handle_face_prop (it);
6558 }
6559
6560
6561 /* Deliver a composition display element. The iterator IT is already
6562 filled with composition information (done in
6563 handle_composition_prop). Value is always 1. */
6564
6565 static int
6566 next_element_from_composition (it)
6567 struct it *it;
6568 {
6569 it->what = IT_COMPOSITION;
6570 it->position = (STRINGP (it->string)
6571 ? it->current.string_pos
6572 : it->current.pos);
6573 if (STRINGP (it->string))
6574 it->object = it->string;
6575 else
6576 it->object = it->w->buffer;
6577 return 1;
6578 }
6579
6580
6581 \f
6582 /***********************************************************************
6583 Moving an iterator without producing glyphs
6584 ***********************************************************************/
6585
6586 /* Check if iterator is at a position corresponding to a valid buffer
6587 position after some move_it_ call. */
6588
6589 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6590 ((it)->method == GET_FROM_STRING \
6591 ? IT_STRING_CHARPOS (*it) == 0 \
6592 : 1)
6593
6594
6595 /* Move iterator IT to a specified buffer or X position within one
6596 line on the display without producing glyphs.
6597
6598 OP should be a bit mask including some or all of these bits:
6599 MOVE_TO_X: Stop on reaching x-position TO_X.
6600 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6601 Regardless of OP's value, stop in reaching the end of the display line.
6602
6603 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6604 This means, in particular, that TO_X includes window's horizontal
6605 scroll amount.
6606
6607 The return value has several possible values that
6608 say what condition caused the scan to stop:
6609
6610 MOVE_POS_MATCH_OR_ZV
6611 - when TO_POS or ZV was reached.
6612
6613 MOVE_X_REACHED
6614 -when TO_X was reached before TO_POS or ZV were reached.
6615
6616 MOVE_LINE_CONTINUED
6617 - when we reached the end of the display area and the line must
6618 be continued.
6619
6620 MOVE_LINE_TRUNCATED
6621 - when we reached the end of the display area and the line is
6622 truncated.
6623
6624 MOVE_NEWLINE_OR_CR
6625 - when we stopped at a line end, i.e. a newline or a CR and selective
6626 display is on. */
6627
6628 static enum move_it_result
6629 move_it_in_display_line_to (it, to_charpos, to_x, op)
6630 struct it *it;
6631 int to_charpos, to_x, op;
6632 {
6633 enum move_it_result result = MOVE_UNDEFINED;
6634 struct glyph_row *saved_glyph_row;
6635
6636 /* Don't produce glyphs in produce_glyphs. */
6637 saved_glyph_row = it->glyph_row;
6638 it->glyph_row = NULL;
6639
6640 #define BUFFER_POS_REACHED_P() \
6641 ((op & MOVE_TO_POS) != 0 \
6642 && BUFFERP (it->object) \
6643 && IT_CHARPOS (*it) >= to_charpos \
6644 && (it->method == GET_FROM_BUFFER \
6645 || (it->method == GET_FROM_DISPLAY_VECTOR \
6646 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6647
6648
6649 while (1)
6650 {
6651 int x, i, ascent = 0, descent = 0;
6652
6653 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6654 if ((op & MOVE_TO_POS) != 0
6655 && BUFFERP (it->object)
6656 && it->method == GET_FROM_BUFFER
6657 && IT_CHARPOS (*it) > to_charpos)
6658 {
6659 result = MOVE_POS_MATCH_OR_ZV;
6660 break;
6661 }
6662
6663 /* Stop when ZV reached.
6664 We used to stop here when TO_CHARPOS reached as well, but that is
6665 too soon if this glyph does not fit on this line. So we handle it
6666 explicitly below. */
6667 if (!get_next_display_element (it)
6668 || (it->truncate_lines_p
6669 && BUFFER_POS_REACHED_P ()))
6670 {
6671 result = MOVE_POS_MATCH_OR_ZV;
6672 break;
6673 }
6674
6675 /* The call to produce_glyphs will get the metrics of the
6676 display element IT is loaded with. We record in x the
6677 x-position before this display element in case it does not
6678 fit on the line. */
6679 x = it->current_x;
6680
6681 /* Remember the line height so far in case the next element doesn't
6682 fit on the line. */
6683 if (!it->truncate_lines_p)
6684 {
6685 ascent = it->max_ascent;
6686 descent = it->max_descent;
6687 }
6688
6689 PRODUCE_GLYPHS (it);
6690
6691 if (it->area != TEXT_AREA)
6692 {
6693 set_iterator_to_next (it, 1);
6694 continue;
6695 }
6696
6697 /* The number of glyphs we get back in IT->nglyphs will normally
6698 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6699 character on a terminal frame, or (iii) a line end. For the
6700 second case, IT->nglyphs - 1 padding glyphs will be present
6701 (on X frames, there is only one glyph produced for a
6702 composite character.
6703
6704 The behavior implemented below means, for continuation lines,
6705 that as many spaces of a TAB as fit on the current line are
6706 displayed there. For terminal frames, as many glyphs of a
6707 multi-glyph character are displayed in the current line, too.
6708 This is what the old redisplay code did, and we keep it that
6709 way. Under X, the whole shape of a complex character must
6710 fit on the line or it will be completely displayed in the
6711 next line.
6712
6713 Note that both for tabs and padding glyphs, all glyphs have
6714 the same width. */
6715 if (it->nglyphs)
6716 {
6717 /* More than one glyph or glyph doesn't fit on line. All
6718 glyphs have the same width. */
6719 int single_glyph_width = it->pixel_width / it->nglyphs;
6720 int new_x;
6721 int x_before_this_char = x;
6722 int hpos_before_this_char = it->hpos;
6723
6724 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6725 {
6726 new_x = x + single_glyph_width;
6727
6728 /* We want to leave anything reaching TO_X to the caller. */
6729 if ((op & MOVE_TO_X) && new_x > to_x)
6730 {
6731 if (BUFFER_POS_REACHED_P ())
6732 goto buffer_pos_reached;
6733 it->current_x = x;
6734 result = MOVE_X_REACHED;
6735 break;
6736 }
6737 else if (/* Lines are continued. */
6738 !it->truncate_lines_p
6739 && (/* And glyph doesn't fit on the line. */
6740 new_x > it->last_visible_x
6741 /* Or it fits exactly and we're on a window
6742 system frame. */
6743 || (new_x == it->last_visible_x
6744 && FRAME_WINDOW_P (it->f))))
6745 {
6746 if (/* IT->hpos == 0 means the very first glyph
6747 doesn't fit on the line, e.g. a wide image. */
6748 it->hpos == 0
6749 || (new_x == it->last_visible_x
6750 && FRAME_WINDOW_P (it->f)))
6751 {
6752 ++it->hpos;
6753 it->current_x = new_x;
6754
6755 /* The character's last glyph just barely fits
6756 in this row. */
6757 if (i == it->nglyphs - 1)
6758 {
6759 /* If this is the destination position,
6760 return a position *before* it in this row,
6761 now that we know it fits in this row. */
6762 if (BUFFER_POS_REACHED_P ())
6763 {
6764 it->hpos = hpos_before_this_char;
6765 it->current_x = x_before_this_char;
6766 result = MOVE_POS_MATCH_OR_ZV;
6767 break;
6768 }
6769
6770 set_iterator_to_next (it, 1);
6771 #ifdef HAVE_WINDOW_SYSTEM
6772 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6773 {
6774 if (!get_next_display_element (it))
6775 {
6776 result = MOVE_POS_MATCH_OR_ZV;
6777 break;
6778 }
6779 if (BUFFER_POS_REACHED_P ())
6780 {
6781 if (ITERATOR_AT_END_OF_LINE_P (it))
6782 result = MOVE_POS_MATCH_OR_ZV;
6783 else
6784 result = MOVE_LINE_CONTINUED;
6785 break;
6786 }
6787 if (ITERATOR_AT_END_OF_LINE_P (it))
6788 {
6789 result = MOVE_NEWLINE_OR_CR;
6790 break;
6791 }
6792 }
6793 #endif /* HAVE_WINDOW_SYSTEM */
6794 }
6795 }
6796 else
6797 {
6798 it->current_x = x;
6799 it->max_ascent = ascent;
6800 it->max_descent = descent;
6801 }
6802
6803 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6804 IT_CHARPOS (*it)));
6805 result = MOVE_LINE_CONTINUED;
6806 break;
6807 }
6808 else if (BUFFER_POS_REACHED_P ())
6809 goto buffer_pos_reached;
6810 else if (new_x > it->first_visible_x)
6811 {
6812 /* Glyph is visible. Increment number of glyphs that
6813 would be displayed. */
6814 ++it->hpos;
6815 }
6816 else
6817 {
6818 /* Glyph is completely off the left margin of the display
6819 area. Nothing to do. */
6820 }
6821 }
6822
6823 if (result != MOVE_UNDEFINED)
6824 break;
6825 }
6826 else if (BUFFER_POS_REACHED_P ())
6827 {
6828 buffer_pos_reached:
6829 it->current_x = x;
6830 it->max_ascent = ascent;
6831 it->max_descent = descent;
6832 result = MOVE_POS_MATCH_OR_ZV;
6833 break;
6834 }
6835 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6836 {
6837 /* Stop when TO_X specified and reached. This check is
6838 necessary here because of lines consisting of a line end,
6839 only. The line end will not produce any glyphs and we
6840 would never get MOVE_X_REACHED. */
6841 xassert (it->nglyphs == 0);
6842 result = MOVE_X_REACHED;
6843 break;
6844 }
6845
6846 /* Is this a line end? If yes, we're done. */
6847 if (ITERATOR_AT_END_OF_LINE_P (it))
6848 {
6849 result = MOVE_NEWLINE_OR_CR;
6850 break;
6851 }
6852
6853 /* The current display element has been consumed. Advance
6854 to the next. */
6855 set_iterator_to_next (it, 1);
6856
6857 /* Stop if lines are truncated and IT's current x-position is
6858 past the right edge of the window now. */
6859 if (it->truncate_lines_p
6860 && it->current_x >= it->last_visible_x)
6861 {
6862 #ifdef HAVE_WINDOW_SYSTEM
6863 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6864 {
6865 if (!get_next_display_element (it)
6866 || BUFFER_POS_REACHED_P ())
6867 {
6868 result = MOVE_POS_MATCH_OR_ZV;
6869 break;
6870 }
6871 if (ITERATOR_AT_END_OF_LINE_P (it))
6872 {
6873 result = MOVE_NEWLINE_OR_CR;
6874 break;
6875 }
6876 }
6877 #endif /* HAVE_WINDOW_SYSTEM */
6878 result = MOVE_LINE_TRUNCATED;
6879 break;
6880 }
6881 }
6882
6883 #undef BUFFER_POS_REACHED_P
6884
6885 /* Restore the iterator settings altered at the beginning of this
6886 function. */
6887 it->glyph_row = saved_glyph_row;
6888 return result;
6889 }
6890
6891
6892 /* Move IT forward until it satisfies one or more of the criteria in
6893 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6894
6895 OP is a bit-mask that specifies where to stop, and in particular,
6896 which of those four position arguments makes a difference. See the
6897 description of enum move_operation_enum.
6898
6899 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6900 screen line, this function will set IT to the next position >
6901 TO_CHARPOS. */
6902
6903 void
6904 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6905 struct it *it;
6906 int to_charpos, to_x, to_y, to_vpos;
6907 int op;
6908 {
6909 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6910 int line_height;
6911 int reached = 0;
6912
6913 for (;;)
6914 {
6915 if (op & MOVE_TO_VPOS)
6916 {
6917 /* If no TO_CHARPOS and no TO_X specified, stop at the
6918 start of the line TO_VPOS. */
6919 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6920 {
6921 if (it->vpos == to_vpos)
6922 {
6923 reached = 1;
6924 break;
6925 }
6926 else
6927 skip = move_it_in_display_line_to (it, -1, -1, 0);
6928 }
6929 else
6930 {
6931 /* TO_VPOS >= 0 means stop at TO_X in the line at
6932 TO_VPOS, or at TO_POS, whichever comes first. */
6933 if (it->vpos == to_vpos)
6934 {
6935 reached = 2;
6936 break;
6937 }
6938
6939 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6940
6941 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6942 {
6943 reached = 3;
6944 break;
6945 }
6946 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6947 {
6948 /* We have reached TO_X but not in the line we want. */
6949 skip = move_it_in_display_line_to (it, to_charpos,
6950 -1, MOVE_TO_POS);
6951 if (skip == MOVE_POS_MATCH_OR_ZV)
6952 {
6953 reached = 4;
6954 break;
6955 }
6956 }
6957 }
6958 }
6959 else if (op & MOVE_TO_Y)
6960 {
6961 struct it it_backup;
6962
6963 /* TO_Y specified means stop at TO_X in the line containing
6964 TO_Y---or at TO_CHARPOS if this is reached first. The
6965 problem is that we can't really tell whether the line
6966 contains TO_Y before we have completely scanned it, and
6967 this may skip past TO_X. What we do is to first scan to
6968 TO_X.
6969
6970 If TO_X is not specified, use a TO_X of zero. The reason
6971 is to make the outcome of this function more predictable.
6972 If we didn't use TO_X == 0, we would stop at the end of
6973 the line which is probably not what a caller would expect
6974 to happen. */
6975 skip = move_it_in_display_line_to (it, to_charpos,
6976 ((op & MOVE_TO_X)
6977 ? to_x : 0),
6978 (MOVE_TO_X
6979 | (op & MOVE_TO_POS)));
6980
6981 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6982 if (skip == MOVE_POS_MATCH_OR_ZV)
6983 {
6984 reached = 5;
6985 break;
6986 }
6987
6988 /* If TO_X was reached, we would like to know whether TO_Y
6989 is in the line. This can only be said if we know the
6990 total line height which requires us to scan the rest of
6991 the line. */
6992 if (skip == MOVE_X_REACHED)
6993 {
6994 /* Wait! We can conclude that TO_Y is in the line if
6995 the already scanned glyphs make the line tall enough
6996 because further scanning doesn't make it shorter. */
6997 line_height = it->max_ascent + it->max_descent;
6998 if (to_y >= it->current_y
6999 && to_y < it->current_y + line_height)
7000 {
7001 reached = 6;
7002 break;
7003 }
7004 it_backup = *it;
7005 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7006 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7007 op & MOVE_TO_POS);
7008 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7009 }
7010
7011 /* Now, decide whether TO_Y is in this line. */
7012 line_height = it->max_ascent + it->max_descent;
7013 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7014
7015 if (to_y >= it->current_y
7016 && to_y < it->current_y + line_height)
7017 {
7018 if (skip == MOVE_X_REACHED)
7019 /* If TO_Y is in this line and TO_X was reached above,
7020 we scanned too far. We have to restore IT's settings
7021 to the ones before skipping. */
7022 *it = it_backup;
7023 reached = 6;
7024 }
7025 else if (skip == MOVE_X_REACHED)
7026 {
7027 skip = skip2;
7028 if (skip == MOVE_POS_MATCH_OR_ZV)
7029 reached = 7;
7030 }
7031
7032 if (reached)
7033 break;
7034 }
7035 else if (BUFFERP (it->object)
7036 && it->method == GET_FROM_BUFFER
7037 && IT_CHARPOS (*it) >= to_charpos)
7038 skip = MOVE_POS_MATCH_OR_ZV;
7039 else
7040 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7041
7042 switch (skip)
7043 {
7044 case MOVE_POS_MATCH_OR_ZV:
7045 reached = 8;
7046 goto out;
7047
7048 case MOVE_NEWLINE_OR_CR:
7049 set_iterator_to_next (it, 1);
7050 it->continuation_lines_width = 0;
7051 break;
7052
7053 case MOVE_LINE_TRUNCATED:
7054 it->continuation_lines_width = 0;
7055 reseat_at_next_visible_line_start (it, 0);
7056 if ((op & MOVE_TO_POS) != 0
7057 && IT_CHARPOS (*it) > to_charpos)
7058 {
7059 reached = 9;
7060 goto out;
7061 }
7062 break;
7063
7064 case MOVE_LINE_CONTINUED:
7065 /* For continued lines ending in a tab, some of the glyphs
7066 associated with the tab are displayed on the current
7067 line. Since it->current_x does not include these glyphs,
7068 we use it->last_visible_x instead. */
7069 it->continuation_lines_width +=
7070 (it->c == '\t') ? it->last_visible_x : it->current_x;
7071 break;
7072
7073 default:
7074 abort ();
7075 }
7076
7077 /* Reset/increment for the next run. */
7078 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7079 it->current_x = it->hpos = 0;
7080 it->current_y += it->max_ascent + it->max_descent;
7081 ++it->vpos;
7082 last_height = it->max_ascent + it->max_descent;
7083 last_max_ascent = it->max_ascent;
7084 it->max_ascent = it->max_descent = 0;
7085 }
7086
7087 out:
7088
7089 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7090 }
7091
7092
7093 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7094
7095 If DY > 0, move IT backward at least that many pixels. DY = 0
7096 means move IT backward to the preceding line start or BEGV. This
7097 function may move over more than DY pixels if IT->current_y - DY
7098 ends up in the middle of a line; in this case IT->current_y will be
7099 set to the top of the line moved to. */
7100
7101 void
7102 move_it_vertically_backward (it, dy)
7103 struct it *it;
7104 int dy;
7105 {
7106 int nlines, h;
7107 struct it it2, it3;
7108 int start_pos;
7109
7110 move_further_back:
7111 xassert (dy >= 0);
7112
7113 start_pos = IT_CHARPOS (*it);
7114
7115 /* Estimate how many newlines we must move back. */
7116 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7117
7118 /* Set the iterator's position that many lines back. */
7119 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7120 back_to_previous_visible_line_start (it);
7121
7122 /* Reseat the iterator here. When moving backward, we don't want
7123 reseat to skip forward over invisible text, set up the iterator
7124 to deliver from overlay strings at the new position etc. So,
7125 use reseat_1 here. */
7126 reseat_1 (it, it->current.pos, 1);
7127
7128 /* We are now surely at a line start. */
7129 it->current_x = it->hpos = 0;
7130 it->continuation_lines_width = 0;
7131
7132 /* Move forward and see what y-distance we moved. First move to the
7133 start of the next line so that we get its height. We need this
7134 height to be able to tell whether we reached the specified
7135 y-distance. */
7136 it2 = *it;
7137 it2.max_ascent = it2.max_descent = 0;
7138 do
7139 {
7140 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7141 MOVE_TO_POS | MOVE_TO_VPOS);
7142 }
7143 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7144 xassert (IT_CHARPOS (*it) >= BEGV);
7145 it3 = it2;
7146
7147 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7148 xassert (IT_CHARPOS (*it) >= BEGV);
7149 /* H is the actual vertical distance from the position in *IT
7150 and the starting position. */
7151 h = it2.current_y - it->current_y;
7152 /* NLINES is the distance in number of lines. */
7153 nlines = it2.vpos - it->vpos;
7154
7155 /* Correct IT's y and vpos position
7156 so that they are relative to the starting point. */
7157 it->vpos -= nlines;
7158 it->current_y -= h;
7159
7160 if (dy == 0)
7161 {
7162 /* DY == 0 means move to the start of the screen line. The
7163 value of nlines is > 0 if continuation lines were involved. */
7164 if (nlines > 0)
7165 move_it_by_lines (it, nlines, 1);
7166 #if 0
7167 /* I think this assert is bogus if buffer contains
7168 invisible text or images. KFS. */
7169 xassert (IT_CHARPOS (*it) <= start_pos);
7170 #endif
7171 }
7172 else
7173 {
7174 /* The y-position we try to reach, relative to *IT.
7175 Note that H has been subtracted in front of the if-statement. */
7176 int target_y = it->current_y + h - dy;
7177 int y0 = it3.current_y;
7178 int y1 = line_bottom_y (&it3);
7179 int line_height = y1 - y0;
7180
7181 /* If we did not reach target_y, try to move further backward if
7182 we can. If we moved too far backward, try to move forward. */
7183 if (target_y < it->current_y
7184 /* This is heuristic. In a window that's 3 lines high, with
7185 a line height of 13 pixels each, recentering with point
7186 on the bottom line will try to move -39/2 = 19 pixels
7187 backward. Try to avoid moving into the first line. */
7188 && (it->current_y - target_y
7189 > min (window_box_height (it->w), line_height * 2 / 3))
7190 && IT_CHARPOS (*it) > BEGV)
7191 {
7192 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7193 target_y - it->current_y));
7194 dy = it->current_y - target_y;
7195 goto move_further_back;
7196 }
7197 else if (target_y >= it->current_y + line_height
7198 && IT_CHARPOS (*it) < ZV)
7199 {
7200 /* Should move forward by at least one line, maybe more.
7201
7202 Note: Calling move_it_by_lines can be expensive on
7203 terminal frames, where compute_motion is used (via
7204 vmotion) to do the job, when there are very long lines
7205 and truncate-lines is nil. That's the reason for
7206 treating terminal frames specially here. */
7207
7208 if (!FRAME_WINDOW_P (it->f))
7209 move_it_vertically (it, target_y - (it->current_y + line_height));
7210 else
7211 {
7212 do
7213 {
7214 move_it_by_lines (it, 1, 1);
7215 }
7216 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7217 }
7218
7219 #if 0
7220 /* I think this assert is bogus if buffer contains
7221 invisible text or images. KFS. */
7222 xassert (IT_CHARPOS (*it) >= BEGV);
7223 #endif
7224 }
7225 }
7226 }
7227
7228
7229 /* Move IT by a specified amount of pixel lines DY. DY negative means
7230 move backwards. DY = 0 means move to start of screen line. At the
7231 end, IT will be on the start of a screen line. */
7232
7233 void
7234 move_it_vertically (it, dy)
7235 struct it *it;
7236 int dy;
7237 {
7238 if (dy <= 0)
7239 move_it_vertically_backward (it, -dy);
7240 else
7241 {
7242 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7243 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7244 MOVE_TO_POS | MOVE_TO_Y);
7245 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7246
7247 /* If buffer ends in ZV without a newline, move to the start of
7248 the line to satisfy the post-condition. */
7249 if (IT_CHARPOS (*it) == ZV
7250 && ZV > BEGV
7251 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7252 move_it_by_lines (it, 0, 0);
7253 }
7254 }
7255
7256
7257 /* Move iterator IT past the end of the text line it is in. */
7258
7259 void
7260 move_it_past_eol (it)
7261 struct it *it;
7262 {
7263 enum move_it_result rc;
7264
7265 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7266 if (rc == MOVE_NEWLINE_OR_CR)
7267 set_iterator_to_next (it, 0);
7268 }
7269
7270
7271 #if 0 /* Currently not used. */
7272
7273 /* Return non-zero if some text between buffer positions START_CHARPOS
7274 and END_CHARPOS is invisible. IT->window is the window for text
7275 property lookup. */
7276
7277 static int
7278 invisible_text_between_p (it, start_charpos, end_charpos)
7279 struct it *it;
7280 int start_charpos, end_charpos;
7281 {
7282 Lisp_Object prop, limit;
7283 int invisible_found_p;
7284
7285 xassert (it != NULL && start_charpos <= end_charpos);
7286
7287 /* Is text at START invisible? */
7288 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7289 it->window);
7290 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7291 invisible_found_p = 1;
7292 else
7293 {
7294 limit = Fnext_single_char_property_change (make_number (start_charpos),
7295 Qinvisible, Qnil,
7296 make_number (end_charpos));
7297 invisible_found_p = XFASTINT (limit) < end_charpos;
7298 }
7299
7300 return invisible_found_p;
7301 }
7302
7303 #endif /* 0 */
7304
7305
7306 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7307 negative means move up. DVPOS == 0 means move to the start of the
7308 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7309 NEED_Y_P is zero, IT->current_y will be left unchanged.
7310
7311 Further optimization ideas: If we would know that IT->f doesn't use
7312 a face with proportional font, we could be faster for
7313 truncate-lines nil. */
7314
7315 void
7316 move_it_by_lines (it, dvpos, need_y_p)
7317 struct it *it;
7318 int dvpos, need_y_p;
7319 {
7320 struct position pos;
7321
7322 /* The commented-out optimization uses vmotion on terminals. This
7323 gives bad results, because elements like it->what, on which
7324 callers such as pos_visible_p rely, aren't updated. */
7325 /* if (!FRAME_WINDOW_P (it->f))
7326 {
7327 struct text_pos textpos;
7328
7329 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7330 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7331 reseat (it, textpos, 1);
7332 it->vpos += pos.vpos;
7333 it->current_y += pos.vpos;
7334 }
7335 else */
7336
7337 if (dvpos == 0)
7338 {
7339 /* DVPOS == 0 means move to the start of the screen line. */
7340 move_it_vertically_backward (it, 0);
7341 xassert (it->current_x == 0 && it->hpos == 0);
7342 /* Let next call to line_bottom_y calculate real line height */
7343 last_height = 0;
7344 }
7345 else if (dvpos > 0)
7346 {
7347 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7348 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7349 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7350 }
7351 else
7352 {
7353 struct it it2;
7354 int start_charpos, i;
7355
7356 /* Start at the beginning of the screen line containing IT's
7357 position. This may actually move vertically backwards,
7358 in case of overlays, so adjust dvpos accordingly. */
7359 dvpos += it->vpos;
7360 move_it_vertically_backward (it, 0);
7361 dvpos -= it->vpos;
7362
7363 /* Go back -DVPOS visible lines and reseat the iterator there. */
7364 start_charpos = IT_CHARPOS (*it);
7365 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7366 back_to_previous_visible_line_start (it);
7367 reseat (it, it->current.pos, 1);
7368
7369 /* Move further back if we end up in a string or an image. */
7370 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7371 {
7372 /* First try to move to start of display line. */
7373 dvpos += it->vpos;
7374 move_it_vertically_backward (it, 0);
7375 dvpos -= it->vpos;
7376 if (IT_POS_VALID_AFTER_MOVE_P (it))
7377 break;
7378 /* If start of line is still in string or image,
7379 move further back. */
7380 back_to_previous_visible_line_start (it);
7381 reseat (it, it->current.pos, 1);
7382 dvpos--;
7383 }
7384
7385 it->current_x = it->hpos = 0;
7386
7387 /* Above call may have moved too far if continuation lines
7388 are involved. Scan forward and see if it did. */
7389 it2 = *it;
7390 it2.vpos = it2.current_y = 0;
7391 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7392 it->vpos -= it2.vpos;
7393 it->current_y -= it2.current_y;
7394 it->current_x = it->hpos = 0;
7395
7396 /* If we moved too far back, move IT some lines forward. */
7397 if (it2.vpos > -dvpos)
7398 {
7399 int delta = it2.vpos + dvpos;
7400 it2 = *it;
7401 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7402 /* Move back again if we got too far ahead. */
7403 if (IT_CHARPOS (*it) >= start_charpos)
7404 *it = it2;
7405 }
7406 }
7407 }
7408
7409 /* Return 1 if IT points into the middle of a display vector. */
7410
7411 int
7412 in_display_vector_p (it)
7413 struct it *it;
7414 {
7415 return (it->method == GET_FROM_DISPLAY_VECTOR
7416 && it->current.dpvec_index > 0
7417 && it->dpvec + it->current.dpvec_index != it->dpend);
7418 }
7419
7420 \f
7421 /***********************************************************************
7422 Messages
7423 ***********************************************************************/
7424
7425
7426 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7427 to *Messages*. */
7428
7429 void
7430 add_to_log (format, arg1, arg2)
7431 char *format;
7432 Lisp_Object arg1, arg2;
7433 {
7434 Lisp_Object args[3];
7435 Lisp_Object msg, fmt;
7436 char *buffer;
7437 int len;
7438 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7439 USE_SAFE_ALLOCA;
7440
7441 /* Do nothing if called asynchronously. Inserting text into
7442 a buffer may call after-change-functions and alike and
7443 that would means running Lisp asynchronously. */
7444 if (handling_signal)
7445 return;
7446
7447 fmt = msg = Qnil;
7448 GCPRO4 (fmt, msg, arg1, arg2);
7449
7450 args[0] = fmt = build_string (format);
7451 args[1] = arg1;
7452 args[2] = arg2;
7453 msg = Fformat (3, args);
7454
7455 len = SBYTES (msg) + 1;
7456 SAFE_ALLOCA (buffer, char *, len);
7457 bcopy (SDATA (msg), buffer, len);
7458
7459 message_dolog (buffer, len - 1, 1, 0);
7460 SAFE_FREE ();
7461
7462 UNGCPRO;
7463 }
7464
7465
7466 /* Output a newline in the *Messages* buffer if "needs" one. */
7467
7468 void
7469 message_log_maybe_newline ()
7470 {
7471 if (message_log_need_newline)
7472 message_dolog ("", 0, 1, 0);
7473 }
7474
7475
7476 /* Add a string M of length NBYTES to the message log, optionally
7477 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7478 nonzero, means interpret the contents of M as multibyte. This
7479 function calls low-level routines in order to bypass text property
7480 hooks, etc. which might not be safe to run.
7481
7482 This may GC (insert may run before/after change hooks),
7483 so the buffer M must NOT point to a Lisp string. */
7484
7485 void
7486 message_dolog (m, nbytes, nlflag, multibyte)
7487 const char *m;
7488 int nbytes, nlflag, multibyte;
7489 {
7490 if (!NILP (Vmemory_full))
7491 return;
7492
7493 if (!NILP (Vmessage_log_max))
7494 {
7495 struct buffer *oldbuf;
7496 Lisp_Object oldpoint, oldbegv, oldzv;
7497 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7498 int point_at_end = 0;
7499 int zv_at_end = 0;
7500 Lisp_Object old_deactivate_mark, tem;
7501 struct gcpro gcpro1;
7502
7503 old_deactivate_mark = Vdeactivate_mark;
7504 oldbuf = current_buffer;
7505 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7506 current_buffer->undo_list = Qt;
7507
7508 oldpoint = message_dolog_marker1;
7509 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7510 oldbegv = message_dolog_marker2;
7511 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7512 oldzv = message_dolog_marker3;
7513 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7514 GCPRO1 (old_deactivate_mark);
7515
7516 if (PT == Z)
7517 point_at_end = 1;
7518 if (ZV == Z)
7519 zv_at_end = 1;
7520
7521 BEGV = BEG;
7522 BEGV_BYTE = BEG_BYTE;
7523 ZV = Z;
7524 ZV_BYTE = Z_BYTE;
7525 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7526
7527 /* Insert the string--maybe converting multibyte to single byte
7528 or vice versa, so that all the text fits the buffer. */
7529 if (multibyte
7530 && NILP (current_buffer->enable_multibyte_characters))
7531 {
7532 int i, c, char_bytes;
7533 unsigned char work[1];
7534
7535 /* Convert a multibyte string to single-byte
7536 for the *Message* buffer. */
7537 for (i = 0; i < nbytes; i += char_bytes)
7538 {
7539 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7540 work[0] = (ASCII_CHAR_P (c)
7541 ? c
7542 : multibyte_char_to_unibyte (c, Qnil));
7543 insert_1_both (work, 1, 1, 1, 0, 0);
7544 }
7545 }
7546 else if (! multibyte
7547 && ! NILP (current_buffer->enable_multibyte_characters))
7548 {
7549 int i, c, char_bytes;
7550 unsigned char *msg = (unsigned char *) m;
7551 unsigned char str[MAX_MULTIBYTE_LENGTH];
7552 /* Convert a single-byte string to multibyte
7553 for the *Message* buffer. */
7554 for (i = 0; i < nbytes; i++)
7555 {
7556 c = msg[i];
7557 c = unibyte_char_to_multibyte (c);
7558 char_bytes = CHAR_STRING (c, str);
7559 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7560 }
7561 }
7562 else if (nbytes)
7563 insert_1 (m, nbytes, 1, 0, 0);
7564
7565 if (nlflag)
7566 {
7567 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7568 insert_1 ("\n", 1, 1, 0, 0);
7569
7570 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7571 this_bol = PT;
7572 this_bol_byte = PT_BYTE;
7573
7574 /* See if this line duplicates the previous one.
7575 If so, combine duplicates. */
7576 if (this_bol > BEG)
7577 {
7578 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7579 prev_bol = PT;
7580 prev_bol_byte = PT_BYTE;
7581
7582 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7583 this_bol, this_bol_byte);
7584 if (dup)
7585 {
7586 del_range_both (prev_bol, prev_bol_byte,
7587 this_bol, this_bol_byte, 0);
7588 if (dup > 1)
7589 {
7590 char dupstr[40];
7591 int duplen;
7592
7593 /* If you change this format, don't forget to also
7594 change message_log_check_duplicate. */
7595 sprintf (dupstr, " [%d times]", dup);
7596 duplen = strlen (dupstr);
7597 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7598 insert_1 (dupstr, duplen, 1, 0, 1);
7599 }
7600 }
7601 }
7602
7603 /* If we have more than the desired maximum number of lines
7604 in the *Messages* buffer now, delete the oldest ones.
7605 This is safe because we don't have undo in this buffer. */
7606
7607 if (NATNUMP (Vmessage_log_max))
7608 {
7609 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7610 -XFASTINT (Vmessage_log_max) - 1, 0);
7611 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7612 }
7613 }
7614 BEGV = XMARKER (oldbegv)->charpos;
7615 BEGV_BYTE = marker_byte_position (oldbegv);
7616
7617 if (zv_at_end)
7618 {
7619 ZV = Z;
7620 ZV_BYTE = Z_BYTE;
7621 }
7622 else
7623 {
7624 ZV = XMARKER (oldzv)->charpos;
7625 ZV_BYTE = marker_byte_position (oldzv);
7626 }
7627
7628 if (point_at_end)
7629 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7630 else
7631 /* We can't do Fgoto_char (oldpoint) because it will run some
7632 Lisp code. */
7633 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7634 XMARKER (oldpoint)->bytepos);
7635
7636 UNGCPRO;
7637 unchain_marker (XMARKER (oldpoint));
7638 unchain_marker (XMARKER (oldbegv));
7639 unchain_marker (XMARKER (oldzv));
7640
7641 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7642 set_buffer_internal (oldbuf);
7643 if (NILP (tem))
7644 windows_or_buffers_changed = old_windows_or_buffers_changed;
7645 message_log_need_newline = !nlflag;
7646 Vdeactivate_mark = old_deactivate_mark;
7647 }
7648 }
7649
7650
7651 /* We are at the end of the buffer after just having inserted a newline.
7652 (Note: We depend on the fact we won't be crossing the gap.)
7653 Check to see if the most recent message looks a lot like the previous one.
7654 Return 0 if different, 1 if the new one should just replace it, or a
7655 value N > 1 if we should also append " [N times]". */
7656
7657 static int
7658 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7659 int prev_bol, this_bol;
7660 int prev_bol_byte, this_bol_byte;
7661 {
7662 int i;
7663 int len = Z_BYTE - 1 - this_bol_byte;
7664 int seen_dots = 0;
7665 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7666 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7667
7668 for (i = 0; i < len; i++)
7669 {
7670 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7671 seen_dots = 1;
7672 if (p1[i] != p2[i])
7673 return seen_dots;
7674 }
7675 p1 += len;
7676 if (*p1 == '\n')
7677 return 2;
7678 if (*p1++ == ' ' && *p1++ == '[')
7679 {
7680 int n = 0;
7681 while (*p1 >= '0' && *p1 <= '9')
7682 n = n * 10 + *p1++ - '0';
7683 if (strncmp (p1, " times]\n", 8) == 0)
7684 return n+1;
7685 }
7686 return 0;
7687 }
7688 \f
7689
7690 /* Display an echo area message M with a specified length of NBYTES
7691 bytes. The string may include null characters. If M is 0, clear
7692 out any existing message, and let the mini-buffer text show
7693 through.
7694
7695 This may GC, so the buffer M must NOT point to a Lisp string. */
7696
7697 void
7698 message2 (m, nbytes, multibyte)
7699 const char *m;
7700 int nbytes;
7701 int multibyte;
7702 {
7703 /* First flush out any partial line written with print. */
7704 message_log_maybe_newline ();
7705 if (m)
7706 message_dolog (m, nbytes, 1, multibyte);
7707 message2_nolog (m, nbytes, multibyte);
7708 }
7709
7710
7711 /* The non-logging counterpart of message2. */
7712
7713 void
7714 message2_nolog (m, nbytes, multibyte)
7715 const char *m;
7716 int nbytes, multibyte;
7717 {
7718 struct frame *sf = SELECTED_FRAME ();
7719 message_enable_multibyte = multibyte;
7720
7721 if (noninteractive)
7722 {
7723 if (noninteractive_need_newline)
7724 putc ('\n', stderr);
7725 noninteractive_need_newline = 0;
7726 if (m)
7727 fwrite (m, nbytes, 1, stderr);
7728 if (cursor_in_echo_area == 0)
7729 fprintf (stderr, "\n");
7730 fflush (stderr);
7731 }
7732 /* A null message buffer means that the frame hasn't really been
7733 initialized yet. Error messages get reported properly by
7734 cmd_error, so this must be just an informative message; toss it. */
7735 else if (INTERACTIVE
7736 && sf->glyphs_initialized_p
7737 && FRAME_MESSAGE_BUF (sf))
7738 {
7739 Lisp_Object mini_window;
7740 struct frame *f;
7741
7742 /* Get the frame containing the mini-buffer
7743 that the selected frame is using. */
7744 mini_window = FRAME_MINIBUF_WINDOW (sf);
7745 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7746
7747 FRAME_SAMPLE_VISIBILITY (f);
7748 if (FRAME_VISIBLE_P (sf)
7749 && ! FRAME_VISIBLE_P (f))
7750 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7751
7752 if (m)
7753 {
7754 set_message (m, Qnil, nbytes, multibyte);
7755 if (minibuffer_auto_raise)
7756 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7757 }
7758 else
7759 clear_message (1, 1);
7760
7761 do_pending_window_change (0);
7762 echo_area_display (1);
7763 do_pending_window_change (0);
7764 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7765 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7766 }
7767 }
7768
7769
7770 /* Display an echo area message M with a specified length of NBYTES
7771 bytes. The string may include null characters. If M is not a
7772 string, clear out any existing message, and let the mini-buffer
7773 text show through.
7774
7775 This function cancels echoing. */
7776
7777 void
7778 message3 (m, nbytes, multibyte)
7779 Lisp_Object m;
7780 int nbytes;
7781 int multibyte;
7782 {
7783 struct gcpro gcpro1;
7784
7785 GCPRO1 (m);
7786 clear_message (1,1);
7787 cancel_echoing ();
7788
7789 /* First flush out any partial line written with print. */
7790 message_log_maybe_newline ();
7791 if (STRINGP (m))
7792 {
7793 char *buffer;
7794 USE_SAFE_ALLOCA;
7795
7796 SAFE_ALLOCA (buffer, char *, nbytes);
7797 bcopy (SDATA (m), buffer, nbytes);
7798 message_dolog (buffer, nbytes, 1, multibyte);
7799 SAFE_FREE ();
7800 }
7801 message3_nolog (m, nbytes, multibyte);
7802
7803 UNGCPRO;
7804 }
7805
7806
7807 /* The non-logging version of message3.
7808 This does not cancel echoing, because it is used for echoing.
7809 Perhaps we need to make a separate function for echoing
7810 and make this cancel echoing. */
7811
7812 void
7813 message3_nolog (m, nbytes, multibyte)
7814 Lisp_Object m;
7815 int nbytes, multibyte;
7816 {
7817 struct frame *sf = SELECTED_FRAME ();
7818 message_enable_multibyte = multibyte;
7819
7820 if (noninteractive)
7821 {
7822 if (noninteractive_need_newline)
7823 putc ('\n', stderr);
7824 noninteractive_need_newline = 0;
7825 if (STRINGP (m))
7826 fwrite (SDATA (m), nbytes, 1, stderr);
7827 if (cursor_in_echo_area == 0)
7828 fprintf (stderr, "\n");
7829 fflush (stderr);
7830 }
7831 /* A null message buffer means that the frame hasn't really been
7832 initialized yet. Error messages get reported properly by
7833 cmd_error, so this must be just an informative message; toss it. */
7834 else if (INTERACTIVE
7835 && sf->glyphs_initialized_p
7836 && FRAME_MESSAGE_BUF (sf))
7837 {
7838 Lisp_Object mini_window;
7839 Lisp_Object frame;
7840 struct frame *f;
7841
7842 /* Get the frame containing the mini-buffer
7843 that the selected frame is using. */
7844 mini_window = FRAME_MINIBUF_WINDOW (sf);
7845 frame = XWINDOW (mini_window)->frame;
7846 f = XFRAME (frame);
7847
7848 FRAME_SAMPLE_VISIBILITY (f);
7849 if (FRAME_VISIBLE_P (sf)
7850 && !FRAME_VISIBLE_P (f))
7851 Fmake_frame_visible (frame);
7852
7853 if (STRINGP (m) && SCHARS (m) > 0)
7854 {
7855 set_message (NULL, m, nbytes, multibyte);
7856 if (minibuffer_auto_raise)
7857 Fraise_frame (frame);
7858 /* Assume we are not echoing.
7859 (If we are, echo_now will override this.) */
7860 echo_message_buffer = Qnil;
7861 }
7862 else
7863 clear_message (1, 1);
7864
7865 do_pending_window_change (0);
7866 echo_area_display (1);
7867 do_pending_window_change (0);
7868 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7869 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7870 }
7871 }
7872
7873
7874 /* Display a null-terminated echo area message M. If M is 0, clear
7875 out any existing message, and let the mini-buffer text show through.
7876
7877 The buffer M must continue to exist until after the echo area gets
7878 cleared or some other message gets displayed there. Do not pass
7879 text that is stored in a Lisp string. Do not pass text in a buffer
7880 that was alloca'd. */
7881
7882 void
7883 message1 (m)
7884 char *m;
7885 {
7886 message2 (m, (m ? strlen (m) : 0), 0);
7887 }
7888
7889
7890 /* The non-logging counterpart of message1. */
7891
7892 void
7893 message1_nolog (m)
7894 char *m;
7895 {
7896 message2_nolog (m, (m ? strlen (m) : 0), 0);
7897 }
7898
7899 /* Display a message M which contains a single %s
7900 which gets replaced with STRING. */
7901
7902 void
7903 message_with_string (m, string, log)
7904 char *m;
7905 Lisp_Object string;
7906 int log;
7907 {
7908 CHECK_STRING (string);
7909
7910 if (noninteractive)
7911 {
7912 if (m)
7913 {
7914 if (noninteractive_need_newline)
7915 putc ('\n', stderr);
7916 noninteractive_need_newline = 0;
7917 fprintf (stderr, m, SDATA (string));
7918 if (cursor_in_echo_area == 0)
7919 fprintf (stderr, "\n");
7920 fflush (stderr);
7921 }
7922 }
7923 else if (INTERACTIVE)
7924 {
7925 /* The frame whose minibuffer we're going to display the message on.
7926 It may be larger than the selected frame, so we need
7927 to use its buffer, not the selected frame's buffer. */
7928 Lisp_Object mini_window;
7929 struct frame *f, *sf = SELECTED_FRAME ();
7930
7931 /* Get the frame containing the minibuffer
7932 that the selected frame is using. */
7933 mini_window = FRAME_MINIBUF_WINDOW (sf);
7934 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7935
7936 /* A null message buffer means that the frame hasn't really been
7937 initialized yet. Error messages get reported properly by
7938 cmd_error, so this must be just an informative message; toss it. */
7939 if (FRAME_MESSAGE_BUF (f))
7940 {
7941 Lisp_Object args[2], message;
7942 struct gcpro gcpro1, gcpro2;
7943
7944 args[0] = build_string (m);
7945 args[1] = message = string;
7946 GCPRO2 (args[0], message);
7947 gcpro1.nvars = 2;
7948
7949 message = Fformat (2, args);
7950
7951 if (log)
7952 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7953 else
7954 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7955
7956 UNGCPRO;
7957
7958 /* Print should start at the beginning of the message
7959 buffer next time. */
7960 message_buf_print = 0;
7961 }
7962 }
7963 }
7964
7965
7966 /* Dump an informative message to the minibuf. If M is 0, clear out
7967 any existing message, and let the mini-buffer text show through. */
7968
7969 /* VARARGS 1 */
7970 void
7971 message (m, a1, a2, a3)
7972 char *m;
7973 EMACS_INT a1, a2, a3;
7974 {
7975 if (noninteractive)
7976 {
7977 if (m)
7978 {
7979 if (noninteractive_need_newline)
7980 putc ('\n', stderr);
7981 noninteractive_need_newline = 0;
7982 fprintf (stderr, m, a1, a2, a3);
7983 if (cursor_in_echo_area == 0)
7984 fprintf (stderr, "\n");
7985 fflush (stderr);
7986 }
7987 }
7988 else if (INTERACTIVE)
7989 {
7990 /* The frame whose mini-buffer we're going to display the message
7991 on. It may be larger than the selected frame, so we need to
7992 use its buffer, not the selected frame's buffer. */
7993 Lisp_Object mini_window;
7994 struct frame *f, *sf = SELECTED_FRAME ();
7995
7996 /* Get the frame containing the mini-buffer
7997 that the selected frame is using. */
7998 mini_window = FRAME_MINIBUF_WINDOW (sf);
7999 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8000
8001 /* A null message buffer means that the frame hasn't really been
8002 initialized yet. Error messages get reported properly by
8003 cmd_error, so this must be just an informative message; toss
8004 it. */
8005 if (FRAME_MESSAGE_BUF (f))
8006 {
8007 if (m)
8008 {
8009 int len;
8010 #ifdef NO_ARG_ARRAY
8011 char *a[3];
8012 a[0] = (char *) a1;
8013 a[1] = (char *) a2;
8014 a[2] = (char *) a3;
8015
8016 len = doprnt (FRAME_MESSAGE_BUF (f),
8017 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8018 #else
8019 len = doprnt (FRAME_MESSAGE_BUF (f),
8020 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8021 (char **) &a1);
8022 #endif /* NO_ARG_ARRAY */
8023
8024 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8025 }
8026 else
8027 message1 (0);
8028
8029 /* Print should start at the beginning of the message
8030 buffer next time. */
8031 message_buf_print = 0;
8032 }
8033 }
8034 }
8035
8036
8037 /* The non-logging version of message. */
8038
8039 void
8040 message_nolog (m, a1, a2, a3)
8041 char *m;
8042 EMACS_INT a1, a2, a3;
8043 {
8044 Lisp_Object old_log_max;
8045 old_log_max = Vmessage_log_max;
8046 Vmessage_log_max = Qnil;
8047 message (m, a1, a2, a3);
8048 Vmessage_log_max = old_log_max;
8049 }
8050
8051
8052 /* Display the current message in the current mini-buffer. This is
8053 only called from error handlers in process.c, and is not time
8054 critical. */
8055
8056 void
8057 update_echo_area ()
8058 {
8059 if (!NILP (echo_area_buffer[0]))
8060 {
8061 Lisp_Object string;
8062 string = Fcurrent_message ();
8063 message3 (string, SBYTES (string),
8064 !NILP (current_buffer->enable_multibyte_characters));
8065 }
8066 }
8067
8068
8069 /* Make sure echo area buffers in `echo_buffers' are live.
8070 If they aren't, make new ones. */
8071
8072 static void
8073 ensure_echo_area_buffers ()
8074 {
8075 int i;
8076
8077 for (i = 0; i < 2; ++i)
8078 if (!BUFFERP (echo_buffer[i])
8079 || NILP (XBUFFER (echo_buffer[i])->name))
8080 {
8081 char name[30];
8082 Lisp_Object old_buffer;
8083 int j;
8084
8085 old_buffer = echo_buffer[i];
8086 sprintf (name, " *Echo Area %d*", i);
8087 echo_buffer[i] = Fget_buffer_create (build_string (name));
8088 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8089
8090 for (j = 0; j < 2; ++j)
8091 if (EQ (old_buffer, echo_area_buffer[j]))
8092 echo_area_buffer[j] = echo_buffer[i];
8093 }
8094 }
8095
8096
8097 /* Call FN with args A1..A4 with either the current or last displayed
8098 echo_area_buffer as current buffer.
8099
8100 WHICH zero means use the current message buffer
8101 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8102 from echo_buffer[] and clear it.
8103
8104 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8105 suitable buffer from echo_buffer[] and clear it.
8106
8107 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8108 that the current message becomes the last displayed one, make
8109 choose a suitable buffer for echo_area_buffer[0], and clear it.
8110
8111 Value is what FN returns. */
8112
8113 static int
8114 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8115 struct window *w;
8116 int which;
8117 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8118 EMACS_INT a1;
8119 Lisp_Object a2;
8120 EMACS_INT a3, a4;
8121 {
8122 Lisp_Object buffer;
8123 int this_one, the_other, clear_buffer_p, rc;
8124 int count = SPECPDL_INDEX ();
8125
8126 /* If buffers aren't live, make new ones. */
8127 ensure_echo_area_buffers ();
8128
8129 clear_buffer_p = 0;
8130
8131 if (which == 0)
8132 this_one = 0, the_other = 1;
8133 else if (which > 0)
8134 this_one = 1, the_other = 0;
8135 else
8136 {
8137 this_one = 0, the_other = 1;
8138 clear_buffer_p = 1;
8139
8140 /* We need a fresh one in case the current echo buffer equals
8141 the one containing the last displayed echo area message. */
8142 if (!NILP (echo_area_buffer[this_one])
8143 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8144 echo_area_buffer[this_one] = Qnil;
8145 }
8146
8147 /* Choose a suitable buffer from echo_buffer[] is we don't
8148 have one. */
8149 if (NILP (echo_area_buffer[this_one]))
8150 {
8151 echo_area_buffer[this_one]
8152 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8153 ? echo_buffer[the_other]
8154 : echo_buffer[this_one]);
8155 clear_buffer_p = 1;
8156 }
8157
8158 buffer = echo_area_buffer[this_one];
8159
8160 /* Don't get confused by reusing the buffer used for echoing
8161 for a different purpose. */
8162 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8163 cancel_echoing ();
8164
8165 record_unwind_protect (unwind_with_echo_area_buffer,
8166 with_echo_area_buffer_unwind_data (w));
8167
8168 /* Make the echo area buffer current. Note that for display
8169 purposes, it is not necessary that the displayed window's buffer
8170 == current_buffer, except for text property lookup. So, let's
8171 only set that buffer temporarily here without doing a full
8172 Fset_window_buffer. We must also change w->pointm, though,
8173 because otherwise an assertions in unshow_buffer fails, and Emacs
8174 aborts. */
8175 set_buffer_internal_1 (XBUFFER (buffer));
8176 if (w)
8177 {
8178 w->buffer = buffer;
8179 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8180 }
8181
8182 current_buffer->undo_list = Qt;
8183 current_buffer->read_only = Qnil;
8184 specbind (Qinhibit_read_only, Qt);
8185 specbind (Qinhibit_modification_hooks, Qt);
8186
8187 if (clear_buffer_p && Z > BEG)
8188 del_range (BEG, Z);
8189
8190 xassert (BEGV >= BEG);
8191 xassert (ZV <= Z && ZV >= BEGV);
8192
8193 rc = fn (a1, a2, a3, a4);
8194
8195 xassert (BEGV >= BEG);
8196 xassert (ZV <= Z && ZV >= BEGV);
8197
8198 unbind_to (count, Qnil);
8199 return rc;
8200 }
8201
8202
8203 /* Save state that should be preserved around the call to the function
8204 FN called in with_echo_area_buffer. */
8205
8206 static Lisp_Object
8207 with_echo_area_buffer_unwind_data (w)
8208 struct window *w;
8209 {
8210 int i = 0;
8211 Lisp_Object vector, tmp;
8212
8213 /* Reduce consing by keeping one vector in
8214 Vwith_echo_area_save_vector. */
8215 vector = Vwith_echo_area_save_vector;
8216 Vwith_echo_area_save_vector = Qnil;
8217
8218 if (NILP (vector))
8219 vector = Fmake_vector (make_number (7), Qnil);
8220
8221 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8222 ASET (vector, i, Vdeactivate_mark); ++i;
8223 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8224
8225 if (w)
8226 {
8227 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8228 ASET (vector, i, w->buffer); ++i;
8229 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8230 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8231 }
8232 else
8233 {
8234 int end = i + 4;
8235 for (; i < end; ++i)
8236 ASET (vector, i, Qnil);
8237 }
8238
8239 xassert (i == ASIZE (vector));
8240 return vector;
8241 }
8242
8243
8244 /* Restore global state from VECTOR which was created by
8245 with_echo_area_buffer_unwind_data. */
8246
8247 static Lisp_Object
8248 unwind_with_echo_area_buffer (vector)
8249 Lisp_Object vector;
8250 {
8251 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8252 Vdeactivate_mark = AREF (vector, 1);
8253 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8254
8255 if (WINDOWP (AREF (vector, 3)))
8256 {
8257 struct window *w;
8258 Lisp_Object buffer, charpos, bytepos;
8259
8260 w = XWINDOW (AREF (vector, 3));
8261 buffer = AREF (vector, 4);
8262 charpos = AREF (vector, 5);
8263 bytepos = AREF (vector, 6);
8264
8265 w->buffer = buffer;
8266 set_marker_both (w->pointm, buffer,
8267 XFASTINT (charpos), XFASTINT (bytepos));
8268 }
8269
8270 Vwith_echo_area_save_vector = vector;
8271 return Qnil;
8272 }
8273
8274
8275 /* Set up the echo area for use by print functions. MULTIBYTE_P
8276 non-zero means we will print multibyte. */
8277
8278 void
8279 setup_echo_area_for_printing (multibyte_p)
8280 int multibyte_p;
8281 {
8282 /* If we can't find an echo area any more, exit. */
8283 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8284 Fkill_emacs (Qnil);
8285
8286 ensure_echo_area_buffers ();
8287
8288 if (!message_buf_print)
8289 {
8290 /* A message has been output since the last time we printed.
8291 Choose a fresh echo area buffer. */
8292 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8293 echo_area_buffer[0] = echo_buffer[1];
8294 else
8295 echo_area_buffer[0] = echo_buffer[0];
8296
8297 /* Switch to that buffer and clear it. */
8298 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8299 current_buffer->truncate_lines = Qnil;
8300
8301 if (Z > BEG)
8302 {
8303 int count = SPECPDL_INDEX ();
8304 specbind (Qinhibit_read_only, Qt);
8305 /* Note that undo recording is always disabled. */
8306 del_range (BEG, Z);
8307 unbind_to (count, Qnil);
8308 }
8309 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8310
8311 /* Set up the buffer for the multibyteness we need. */
8312 if (multibyte_p
8313 != !NILP (current_buffer->enable_multibyte_characters))
8314 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8315
8316 /* Raise the frame containing the echo area. */
8317 if (minibuffer_auto_raise)
8318 {
8319 struct frame *sf = SELECTED_FRAME ();
8320 Lisp_Object mini_window;
8321 mini_window = FRAME_MINIBUF_WINDOW (sf);
8322 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8323 }
8324
8325 message_log_maybe_newline ();
8326 message_buf_print = 1;
8327 }
8328 else
8329 {
8330 if (NILP (echo_area_buffer[0]))
8331 {
8332 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8333 echo_area_buffer[0] = echo_buffer[1];
8334 else
8335 echo_area_buffer[0] = echo_buffer[0];
8336 }
8337
8338 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8339 {
8340 /* Someone switched buffers between print requests. */
8341 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8342 current_buffer->truncate_lines = Qnil;
8343 }
8344 }
8345 }
8346
8347
8348 /* Display an echo area message in window W. Value is non-zero if W's
8349 height is changed. If display_last_displayed_message_p is
8350 non-zero, display the message that was last displayed, otherwise
8351 display the current message. */
8352
8353 static int
8354 display_echo_area (w)
8355 struct window *w;
8356 {
8357 int i, no_message_p, window_height_changed_p, count;
8358
8359 /* Temporarily disable garbage collections while displaying the echo
8360 area. This is done because a GC can print a message itself.
8361 That message would modify the echo area buffer's contents while a
8362 redisplay of the buffer is going on, and seriously confuse
8363 redisplay. */
8364 count = inhibit_garbage_collection ();
8365
8366 /* If there is no message, we must call display_echo_area_1
8367 nevertheless because it resizes the window. But we will have to
8368 reset the echo_area_buffer in question to nil at the end because
8369 with_echo_area_buffer will sets it to an empty buffer. */
8370 i = display_last_displayed_message_p ? 1 : 0;
8371 no_message_p = NILP (echo_area_buffer[i]);
8372
8373 window_height_changed_p
8374 = with_echo_area_buffer (w, display_last_displayed_message_p,
8375 display_echo_area_1,
8376 (EMACS_INT) w, Qnil, 0, 0);
8377
8378 if (no_message_p)
8379 echo_area_buffer[i] = Qnil;
8380
8381 unbind_to (count, Qnil);
8382 return window_height_changed_p;
8383 }
8384
8385
8386 /* Helper for display_echo_area. Display the current buffer which
8387 contains the current echo area message in window W, a mini-window,
8388 a pointer to which is passed in A1. A2..A4 are currently not used.
8389 Change the height of W so that all of the message is displayed.
8390 Value is non-zero if height of W was changed. */
8391
8392 static int
8393 display_echo_area_1 (a1, a2, a3, a4)
8394 EMACS_INT a1;
8395 Lisp_Object a2;
8396 EMACS_INT a3, a4;
8397 {
8398 struct window *w = (struct window *) a1;
8399 Lisp_Object window;
8400 struct text_pos start;
8401 int window_height_changed_p = 0;
8402
8403 /* Do this before displaying, so that we have a large enough glyph
8404 matrix for the display. If we can't get enough space for the
8405 whole text, display the last N lines. That works by setting w->start. */
8406 window_height_changed_p = resize_mini_window (w, 0);
8407
8408 /* Use the starting position chosen by resize_mini_window. */
8409 SET_TEXT_POS_FROM_MARKER (start, w->start);
8410
8411 /* Display. */
8412 clear_glyph_matrix (w->desired_matrix);
8413 XSETWINDOW (window, w);
8414 try_window (window, start, 0);
8415
8416 return window_height_changed_p;
8417 }
8418
8419
8420 /* Resize the echo area window to exactly the size needed for the
8421 currently displayed message, if there is one. If a mini-buffer
8422 is active, don't shrink it. */
8423
8424 void
8425 resize_echo_area_exactly ()
8426 {
8427 if (BUFFERP (echo_area_buffer[0])
8428 && WINDOWP (echo_area_window))
8429 {
8430 struct window *w = XWINDOW (echo_area_window);
8431 int resized_p;
8432 Lisp_Object resize_exactly;
8433
8434 if (minibuf_level == 0)
8435 resize_exactly = Qt;
8436 else
8437 resize_exactly = Qnil;
8438
8439 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8440 (EMACS_INT) w, resize_exactly, 0, 0);
8441 if (resized_p)
8442 {
8443 ++windows_or_buffers_changed;
8444 ++update_mode_lines;
8445 redisplay_internal (0);
8446 }
8447 }
8448 }
8449
8450
8451 /* Callback function for with_echo_area_buffer, when used from
8452 resize_echo_area_exactly. A1 contains a pointer to the window to
8453 resize, EXACTLY non-nil means resize the mini-window exactly to the
8454 size of the text displayed. A3 and A4 are not used. Value is what
8455 resize_mini_window returns. */
8456
8457 static int
8458 resize_mini_window_1 (a1, exactly, a3, a4)
8459 EMACS_INT a1;
8460 Lisp_Object exactly;
8461 EMACS_INT a3, a4;
8462 {
8463 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8464 }
8465
8466
8467 /* Resize mini-window W to fit the size of its contents. EXACT_P
8468 means size the window exactly to the size needed. Otherwise, it's
8469 only enlarged until W's buffer is empty.
8470
8471 Set W->start to the right place to begin display. If the whole
8472 contents fit, start at the beginning. Otherwise, start so as
8473 to make the end of the contents appear. This is particularly
8474 important for y-or-n-p, but seems desirable generally.
8475
8476 Value is non-zero if the window height has been changed. */
8477
8478 int
8479 resize_mini_window (w, exact_p)
8480 struct window *w;
8481 int exact_p;
8482 {
8483 struct frame *f = XFRAME (w->frame);
8484 int window_height_changed_p = 0;
8485
8486 xassert (MINI_WINDOW_P (w));
8487
8488 /* By default, start display at the beginning. */
8489 set_marker_both (w->start, w->buffer,
8490 BUF_BEGV (XBUFFER (w->buffer)),
8491 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8492
8493 /* Don't resize windows while redisplaying a window; it would
8494 confuse redisplay functions when the size of the window they are
8495 displaying changes from under them. Such a resizing can happen,
8496 for instance, when which-func prints a long message while
8497 we are running fontification-functions. We're running these
8498 functions with safe_call which binds inhibit-redisplay to t. */
8499 if (!NILP (Vinhibit_redisplay))
8500 return 0;
8501
8502 /* Nil means don't try to resize. */
8503 if (NILP (Vresize_mini_windows)
8504 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8505 return 0;
8506
8507 if (!FRAME_MINIBUF_ONLY_P (f))
8508 {
8509 struct it it;
8510 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8511 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8512 int height, max_height;
8513 int unit = FRAME_LINE_HEIGHT (f);
8514 struct text_pos start;
8515 struct buffer *old_current_buffer = NULL;
8516
8517 if (current_buffer != XBUFFER (w->buffer))
8518 {
8519 old_current_buffer = current_buffer;
8520 set_buffer_internal (XBUFFER (w->buffer));
8521 }
8522
8523 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8524
8525 /* Compute the max. number of lines specified by the user. */
8526 if (FLOATP (Vmax_mini_window_height))
8527 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8528 else if (INTEGERP (Vmax_mini_window_height))
8529 max_height = XINT (Vmax_mini_window_height);
8530 else
8531 max_height = total_height / 4;
8532
8533 /* Correct that max. height if it's bogus. */
8534 max_height = max (1, max_height);
8535 max_height = min (total_height, max_height);
8536
8537 /* Find out the height of the text in the window. */
8538 if (it.truncate_lines_p)
8539 height = 1;
8540 else
8541 {
8542 last_height = 0;
8543 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8544 if (it.max_ascent == 0 && it.max_descent == 0)
8545 height = it.current_y + last_height;
8546 else
8547 height = it.current_y + it.max_ascent + it.max_descent;
8548 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8549 height = (height + unit - 1) / unit;
8550 }
8551
8552 /* Compute a suitable window start. */
8553 if (height > max_height)
8554 {
8555 height = max_height;
8556 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8557 move_it_vertically_backward (&it, (height - 1) * unit);
8558 start = it.current.pos;
8559 }
8560 else
8561 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8562 SET_MARKER_FROM_TEXT_POS (w->start, start);
8563
8564 if (EQ (Vresize_mini_windows, Qgrow_only))
8565 {
8566 /* Let it grow only, until we display an empty message, in which
8567 case the window shrinks again. */
8568 if (height > WINDOW_TOTAL_LINES (w))
8569 {
8570 int old_height = WINDOW_TOTAL_LINES (w);
8571 freeze_window_starts (f, 1);
8572 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8573 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8574 }
8575 else if (height < WINDOW_TOTAL_LINES (w)
8576 && (exact_p || BEGV == ZV))
8577 {
8578 int old_height = WINDOW_TOTAL_LINES (w);
8579 freeze_window_starts (f, 0);
8580 shrink_mini_window (w);
8581 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8582 }
8583 }
8584 else
8585 {
8586 /* Always resize to exact size needed. */
8587 if (height > WINDOW_TOTAL_LINES (w))
8588 {
8589 int old_height = WINDOW_TOTAL_LINES (w);
8590 freeze_window_starts (f, 1);
8591 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8592 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8593 }
8594 else if (height < WINDOW_TOTAL_LINES (w))
8595 {
8596 int old_height = WINDOW_TOTAL_LINES (w);
8597 freeze_window_starts (f, 0);
8598 shrink_mini_window (w);
8599
8600 if (height)
8601 {
8602 freeze_window_starts (f, 1);
8603 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8604 }
8605
8606 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8607 }
8608 }
8609
8610 if (old_current_buffer)
8611 set_buffer_internal (old_current_buffer);
8612 }
8613
8614 return window_height_changed_p;
8615 }
8616
8617
8618 /* Value is the current message, a string, or nil if there is no
8619 current message. */
8620
8621 Lisp_Object
8622 current_message ()
8623 {
8624 Lisp_Object msg;
8625
8626 if (!BUFFERP (echo_area_buffer[0]))
8627 msg = Qnil;
8628 else
8629 {
8630 with_echo_area_buffer (0, 0, current_message_1,
8631 (EMACS_INT) &msg, Qnil, 0, 0);
8632 if (NILP (msg))
8633 echo_area_buffer[0] = Qnil;
8634 }
8635
8636 return msg;
8637 }
8638
8639
8640 static int
8641 current_message_1 (a1, a2, a3, a4)
8642 EMACS_INT a1;
8643 Lisp_Object a2;
8644 EMACS_INT a3, a4;
8645 {
8646 Lisp_Object *msg = (Lisp_Object *) a1;
8647
8648 if (Z > BEG)
8649 *msg = make_buffer_string (BEG, Z, 1);
8650 else
8651 *msg = Qnil;
8652 return 0;
8653 }
8654
8655
8656 /* Push the current message on Vmessage_stack for later restauration
8657 by restore_message. Value is non-zero if the current message isn't
8658 empty. This is a relatively infrequent operation, so it's not
8659 worth optimizing. */
8660
8661 int
8662 push_message ()
8663 {
8664 Lisp_Object msg;
8665 msg = current_message ();
8666 Vmessage_stack = Fcons (msg, Vmessage_stack);
8667 return STRINGP (msg);
8668 }
8669
8670
8671 /* Restore message display from the top of Vmessage_stack. */
8672
8673 void
8674 restore_message ()
8675 {
8676 Lisp_Object msg;
8677
8678 xassert (CONSP (Vmessage_stack));
8679 msg = XCAR (Vmessage_stack);
8680 if (STRINGP (msg))
8681 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8682 else
8683 message3_nolog (msg, 0, 0);
8684 }
8685
8686
8687 /* Handler for record_unwind_protect calling pop_message. */
8688
8689 Lisp_Object
8690 pop_message_unwind (dummy)
8691 Lisp_Object dummy;
8692 {
8693 pop_message ();
8694 return Qnil;
8695 }
8696
8697 /* Pop the top-most entry off Vmessage_stack. */
8698
8699 void
8700 pop_message ()
8701 {
8702 xassert (CONSP (Vmessage_stack));
8703 Vmessage_stack = XCDR (Vmessage_stack);
8704 }
8705
8706
8707 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8708 exits. If the stack is not empty, we have a missing pop_message
8709 somewhere. */
8710
8711 void
8712 check_message_stack ()
8713 {
8714 if (!NILP (Vmessage_stack))
8715 abort ();
8716 }
8717
8718
8719 /* Truncate to NCHARS what will be displayed in the echo area the next
8720 time we display it---but don't redisplay it now. */
8721
8722 void
8723 truncate_echo_area (nchars)
8724 int nchars;
8725 {
8726 if (nchars == 0)
8727 echo_area_buffer[0] = Qnil;
8728 /* A null message buffer means that the frame hasn't really been
8729 initialized yet. Error messages get reported properly by
8730 cmd_error, so this must be just an informative message; toss it. */
8731 else if (!noninteractive
8732 && INTERACTIVE
8733 && !NILP (echo_area_buffer[0]))
8734 {
8735 struct frame *sf = SELECTED_FRAME ();
8736 if (FRAME_MESSAGE_BUF (sf))
8737 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8738 }
8739 }
8740
8741
8742 /* Helper function for truncate_echo_area. Truncate the current
8743 message to at most NCHARS characters. */
8744
8745 static int
8746 truncate_message_1 (nchars, a2, a3, a4)
8747 EMACS_INT nchars;
8748 Lisp_Object a2;
8749 EMACS_INT a3, a4;
8750 {
8751 if (BEG + nchars < Z)
8752 del_range (BEG + nchars, Z);
8753 if (Z == BEG)
8754 echo_area_buffer[0] = Qnil;
8755 return 0;
8756 }
8757
8758
8759 /* Set the current message to a substring of S or STRING.
8760
8761 If STRING is a Lisp string, set the message to the first NBYTES
8762 bytes from STRING. NBYTES zero means use the whole string. If
8763 STRING is multibyte, the message will be displayed multibyte.
8764
8765 If S is not null, set the message to the first LEN bytes of S. LEN
8766 zero means use the whole string. MULTIBYTE_P non-zero means S is
8767 multibyte. Display the message multibyte in that case.
8768
8769 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8770 to t before calling set_message_1 (which calls insert).
8771 */
8772
8773 void
8774 set_message (s, string, nbytes, multibyte_p)
8775 const char *s;
8776 Lisp_Object string;
8777 int nbytes, multibyte_p;
8778 {
8779 message_enable_multibyte
8780 = ((s && multibyte_p)
8781 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8782
8783 with_echo_area_buffer (0, -1, set_message_1,
8784 (EMACS_INT) s, string, nbytes, multibyte_p);
8785 message_buf_print = 0;
8786 help_echo_showing_p = 0;
8787 }
8788
8789
8790 /* Helper function for set_message. Arguments have the same meaning
8791 as there, with A1 corresponding to S and A2 corresponding to STRING
8792 This function is called with the echo area buffer being
8793 current. */
8794
8795 static int
8796 set_message_1 (a1, a2, nbytes, multibyte_p)
8797 EMACS_INT a1;
8798 Lisp_Object a2;
8799 EMACS_INT nbytes, multibyte_p;
8800 {
8801 const char *s = (const char *) a1;
8802 Lisp_Object string = a2;
8803
8804 /* Change multibyteness of the echo buffer appropriately. */
8805 if (message_enable_multibyte
8806 != !NILP (current_buffer->enable_multibyte_characters))
8807 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8808
8809 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8810
8811 /* Insert new message at BEG. */
8812 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8813
8814 if (STRINGP (string))
8815 {
8816 int nchars;
8817
8818 if (nbytes == 0)
8819 nbytes = SBYTES (string);
8820 nchars = string_byte_to_char (string, nbytes);
8821
8822 /* This function takes care of single/multibyte conversion. We
8823 just have to ensure that the echo area buffer has the right
8824 setting of enable_multibyte_characters. */
8825 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8826 }
8827 else if (s)
8828 {
8829 if (nbytes == 0)
8830 nbytes = strlen (s);
8831
8832 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8833 {
8834 /* Convert from multi-byte to single-byte. */
8835 int i, c, n;
8836 unsigned char work[1];
8837
8838 /* Convert a multibyte string to single-byte. */
8839 for (i = 0; i < nbytes; i += n)
8840 {
8841 c = string_char_and_length (s + i, nbytes - i, &n);
8842 work[0] = (ASCII_CHAR_P (c)
8843 ? c
8844 : multibyte_char_to_unibyte (c, Qnil));
8845 insert_1_both (work, 1, 1, 1, 0, 0);
8846 }
8847 }
8848 else if (!multibyte_p
8849 && !NILP (current_buffer->enable_multibyte_characters))
8850 {
8851 /* Convert from single-byte to multi-byte. */
8852 int i, c, n;
8853 const unsigned char *msg = (const unsigned char *) s;
8854 unsigned char str[MAX_MULTIBYTE_LENGTH];
8855
8856 /* Convert a single-byte string to multibyte. */
8857 for (i = 0; i < nbytes; i++)
8858 {
8859 c = msg[i];
8860 c = unibyte_char_to_multibyte (c);
8861 n = CHAR_STRING (c, str);
8862 insert_1_both (str, 1, n, 1, 0, 0);
8863 }
8864 }
8865 else
8866 insert_1 (s, nbytes, 1, 0, 0);
8867 }
8868
8869 return 0;
8870 }
8871
8872
8873 /* Clear messages. CURRENT_P non-zero means clear the current
8874 message. LAST_DISPLAYED_P non-zero means clear the message
8875 last displayed. */
8876
8877 void
8878 clear_message (current_p, last_displayed_p)
8879 int current_p, last_displayed_p;
8880 {
8881 if (current_p)
8882 {
8883 echo_area_buffer[0] = Qnil;
8884 message_cleared_p = 1;
8885 }
8886
8887 if (last_displayed_p)
8888 echo_area_buffer[1] = Qnil;
8889
8890 message_buf_print = 0;
8891 }
8892
8893 /* Clear garbaged frames.
8894
8895 This function is used where the old redisplay called
8896 redraw_garbaged_frames which in turn called redraw_frame which in
8897 turn called clear_frame. The call to clear_frame was a source of
8898 flickering. I believe a clear_frame is not necessary. It should
8899 suffice in the new redisplay to invalidate all current matrices,
8900 and ensure a complete redisplay of all windows. */
8901
8902 static void
8903 clear_garbaged_frames ()
8904 {
8905 if (frame_garbaged)
8906 {
8907 Lisp_Object tail, frame;
8908 int changed_count = 0;
8909
8910 FOR_EACH_FRAME (tail, frame)
8911 {
8912 struct frame *f = XFRAME (frame);
8913
8914 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8915 {
8916 if (f->resized_p)
8917 {
8918 Fredraw_frame (frame);
8919 f->force_flush_display_p = 1;
8920 }
8921 clear_current_matrices (f);
8922 changed_count++;
8923 f->garbaged = 0;
8924 f->resized_p = 0;
8925 }
8926 }
8927
8928 frame_garbaged = 0;
8929 if (changed_count)
8930 ++windows_or_buffers_changed;
8931 }
8932 }
8933
8934
8935 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8936 is non-zero update selected_frame. Value is non-zero if the
8937 mini-windows height has been changed. */
8938
8939 static int
8940 echo_area_display (update_frame_p)
8941 int update_frame_p;
8942 {
8943 Lisp_Object mini_window;
8944 struct window *w;
8945 struct frame *f;
8946 int window_height_changed_p = 0;
8947 struct frame *sf = SELECTED_FRAME ();
8948
8949 mini_window = FRAME_MINIBUF_WINDOW (sf);
8950 w = XWINDOW (mini_window);
8951 f = XFRAME (WINDOW_FRAME (w));
8952
8953 /* Don't display if frame is invisible or not yet initialized. */
8954 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8955 return 0;
8956
8957 #ifdef HAVE_WINDOW_SYSTEM
8958 /* When Emacs starts, selected_frame may be the initial terminal
8959 frame. If we let this through, a message would be displayed on
8960 the terminal. */
8961 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8962 return 0;
8963 #endif /* HAVE_WINDOW_SYSTEM */
8964
8965 /* Redraw garbaged frames. */
8966 if (frame_garbaged)
8967 clear_garbaged_frames ();
8968
8969 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8970 {
8971 echo_area_window = mini_window;
8972 window_height_changed_p = display_echo_area (w);
8973 w->must_be_updated_p = 1;
8974
8975 /* Update the display, unless called from redisplay_internal.
8976 Also don't update the screen during redisplay itself. The
8977 update will happen at the end of redisplay, and an update
8978 here could cause confusion. */
8979 if (update_frame_p && !redisplaying_p)
8980 {
8981 int n = 0;
8982
8983 /* If the display update has been interrupted by pending
8984 input, update mode lines in the frame. Due to the
8985 pending input, it might have been that redisplay hasn't
8986 been called, so that mode lines above the echo area are
8987 garbaged. This looks odd, so we prevent it here. */
8988 if (!display_completed)
8989 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8990
8991 if (window_height_changed_p
8992 /* Don't do this if Emacs is shutting down. Redisplay
8993 needs to run hooks. */
8994 && !NILP (Vrun_hooks))
8995 {
8996 /* Must update other windows. Likewise as in other
8997 cases, don't let this update be interrupted by
8998 pending input. */
8999 int count = SPECPDL_INDEX ();
9000 specbind (Qredisplay_dont_pause, Qt);
9001 windows_or_buffers_changed = 1;
9002 redisplay_internal (0);
9003 unbind_to (count, Qnil);
9004 }
9005 else if (FRAME_WINDOW_P (f) && n == 0)
9006 {
9007 /* Window configuration is the same as before.
9008 Can do with a display update of the echo area,
9009 unless we displayed some mode lines. */
9010 update_single_window (w, 1);
9011 FRAME_RIF (f)->flush_display (f);
9012 }
9013 else
9014 update_frame (f, 1, 1);
9015
9016 /* If cursor is in the echo area, make sure that the next
9017 redisplay displays the minibuffer, so that the cursor will
9018 be replaced with what the minibuffer wants. */
9019 if (cursor_in_echo_area)
9020 ++windows_or_buffers_changed;
9021 }
9022 }
9023 else if (!EQ (mini_window, selected_window))
9024 windows_or_buffers_changed++;
9025
9026 /* Last displayed message is now the current message. */
9027 echo_area_buffer[1] = echo_area_buffer[0];
9028 /* Inform read_char that we're not echoing. */
9029 echo_message_buffer = Qnil;
9030
9031 /* Prevent redisplay optimization in redisplay_internal by resetting
9032 this_line_start_pos. This is done because the mini-buffer now
9033 displays the message instead of its buffer text. */
9034 if (EQ (mini_window, selected_window))
9035 CHARPOS (this_line_start_pos) = 0;
9036
9037 return window_height_changed_p;
9038 }
9039
9040
9041 \f
9042 /***********************************************************************
9043 Mode Lines and Frame Titles
9044 ***********************************************************************/
9045
9046 /* A buffer for constructing non-propertized mode-line strings and
9047 frame titles in it; allocated from the heap in init_xdisp and
9048 resized as needed in store_mode_line_noprop_char. */
9049
9050 static char *mode_line_noprop_buf;
9051
9052 /* The buffer's end, and a current output position in it. */
9053
9054 static char *mode_line_noprop_buf_end;
9055 static char *mode_line_noprop_ptr;
9056
9057 #define MODE_LINE_NOPROP_LEN(start) \
9058 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9059
9060 static enum {
9061 MODE_LINE_DISPLAY = 0,
9062 MODE_LINE_TITLE,
9063 MODE_LINE_NOPROP,
9064 MODE_LINE_STRING
9065 } mode_line_target;
9066
9067 /* Alist that caches the results of :propertize.
9068 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9069 static Lisp_Object mode_line_proptrans_alist;
9070
9071 /* List of strings making up the mode-line. */
9072 static Lisp_Object mode_line_string_list;
9073
9074 /* Base face property when building propertized mode line string. */
9075 static Lisp_Object mode_line_string_face;
9076 static Lisp_Object mode_line_string_face_prop;
9077
9078
9079 /* Unwind data for mode line strings */
9080
9081 static Lisp_Object Vmode_line_unwind_vector;
9082
9083 static Lisp_Object
9084 format_mode_line_unwind_data (struct buffer *obuf,
9085 Lisp_Object owin,
9086 int save_proptrans)
9087 {
9088 Lisp_Object vector, tmp;
9089
9090 /* Reduce consing by keeping one vector in
9091 Vwith_echo_area_save_vector. */
9092 vector = Vmode_line_unwind_vector;
9093 Vmode_line_unwind_vector = Qnil;
9094
9095 if (NILP (vector))
9096 vector = Fmake_vector (make_number (8), Qnil);
9097
9098 ASET (vector, 0, make_number (mode_line_target));
9099 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9100 ASET (vector, 2, mode_line_string_list);
9101 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9102 ASET (vector, 4, mode_line_string_face);
9103 ASET (vector, 5, mode_line_string_face_prop);
9104
9105 if (obuf)
9106 XSETBUFFER (tmp, obuf);
9107 else
9108 tmp = Qnil;
9109 ASET (vector, 6, tmp);
9110 ASET (vector, 7, owin);
9111
9112 return vector;
9113 }
9114
9115 static Lisp_Object
9116 unwind_format_mode_line (vector)
9117 Lisp_Object vector;
9118 {
9119 mode_line_target = XINT (AREF (vector, 0));
9120 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9121 mode_line_string_list = AREF (vector, 2);
9122 if (! EQ (AREF (vector, 3), Qt))
9123 mode_line_proptrans_alist = AREF (vector, 3);
9124 mode_line_string_face = AREF (vector, 4);
9125 mode_line_string_face_prop = AREF (vector, 5);
9126
9127 if (!NILP (AREF (vector, 7)))
9128 /* Select window before buffer, since it may change the buffer. */
9129 Fselect_window (AREF (vector, 7), Qt);
9130
9131 if (!NILP (AREF (vector, 6)))
9132 {
9133 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9134 ASET (vector, 6, Qnil);
9135 }
9136
9137 Vmode_line_unwind_vector = vector;
9138 return Qnil;
9139 }
9140
9141
9142 /* Store a single character C for the frame title in mode_line_noprop_buf.
9143 Re-allocate mode_line_noprop_buf if necessary. */
9144
9145 static void
9146 #ifdef PROTOTYPES
9147 store_mode_line_noprop_char (char c)
9148 #else
9149 store_mode_line_noprop_char (c)
9150 char c;
9151 #endif
9152 {
9153 /* If output position has reached the end of the allocated buffer,
9154 double the buffer's size. */
9155 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9156 {
9157 int len = MODE_LINE_NOPROP_LEN (0);
9158 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9159 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9160 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9161 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9162 }
9163
9164 *mode_line_noprop_ptr++ = c;
9165 }
9166
9167
9168 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9169 mode_line_noprop_ptr. STR is the string to store. Do not copy
9170 characters that yield more columns than PRECISION; PRECISION <= 0
9171 means copy the whole string. Pad with spaces until FIELD_WIDTH
9172 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9173 pad. Called from display_mode_element when it is used to build a
9174 frame title. */
9175
9176 static int
9177 store_mode_line_noprop (str, field_width, precision)
9178 const unsigned char *str;
9179 int field_width, precision;
9180 {
9181 int n = 0;
9182 int dummy, nbytes;
9183
9184 /* Copy at most PRECISION chars from STR. */
9185 nbytes = strlen (str);
9186 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9187 while (nbytes--)
9188 store_mode_line_noprop_char (*str++);
9189
9190 /* Fill up with spaces until FIELD_WIDTH reached. */
9191 while (field_width > 0
9192 && n < field_width)
9193 {
9194 store_mode_line_noprop_char (' ');
9195 ++n;
9196 }
9197
9198 return n;
9199 }
9200
9201 /***********************************************************************
9202 Frame Titles
9203 ***********************************************************************/
9204
9205 #ifdef HAVE_WINDOW_SYSTEM
9206
9207 /* Set the title of FRAME, if it has changed. The title format is
9208 Vicon_title_format if FRAME is iconified, otherwise it is
9209 frame_title_format. */
9210
9211 static void
9212 x_consider_frame_title (frame)
9213 Lisp_Object frame;
9214 {
9215 struct frame *f = XFRAME (frame);
9216
9217 if (FRAME_WINDOW_P (f)
9218 || FRAME_MINIBUF_ONLY_P (f)
9219 || f->explicit_name)
9220 {
9221 /* Do we have more than one visible frame on this X display? */
9222 Lisp_Object tail;
9223 Lisp_Object fmt;
9224 int title_start;
9225 char *title;
9226 int len;
9227 struct it it;
9228 int count = SPECPDL_INDEX ();
9229
9230 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9231 {
9232 Lisp_Object other_frame = XCAR (tail);
9233 struct frame *tf = XFRAME (other_frame);
9234
9235 if (tf != f
9236 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9237 && !FRAME_MINIBUF_ONLY_P (tf)
9238 && !EQ (other_frame, tip_frame)
9239 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9240 break;
9241 }
9242
9243 /* Set global variable indicating that multiple frames exist. */
9244 multiple_frames = CONSP (tail);
9245
9246 /* Switch to the buffer of selected window of the frame. Set up
9247 mode_line_target so that display_mode_element will output into
9248 mode_line_noprop_buf; then display the title. */
9249 record_unwind_protect (unwind_format_mode_line,
9250 format_mode_line_unwind_data
9251 (current_buffer, selected_window, 0));
9252
9253 Fselect_window (f->selected_window, Qt);
9254 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9255 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9256
9257 mode_line_target = MODE_LINE_TITLE;
9258 title_start = MODE_LINE_NOPROP_LEN (0);
9259 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9260 NULL, DEFAULT_FACE_ID);
9261 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9262 len = MODE_LINE_NOPROP_LEN (title_start);
9263 title = mode_line_noprop_buf + title_start;
9264 unbind_to (count, Qnil);
9265
9266 /* Set the title only if it's changed. This avoids consing in
9267 the common case where it hasn't. (If it turns out that we've
9268 already wasted too much time by walking through the list with
9269 display_mode_element, then we might need to optimize at a
9270 higher level than this.) */
9271 if (! STRINGP (f->name)
9272 || SBYTES (f->name) != len
9273 || bcmp (title, SDATA (f->name), len) != 0)
9274 x_implicitly_set_name (f, make_string (title, len), Qnil);
9275 }
9276 }
9277
9278 #endif /* not HAVE_WINDOW_SYSTEM */
9279
9280
9281
9282 \f
9283 /***********************************************************************
9284 Menu Bars
9285 ***********************************************************************/
9286
9287
9288 /* Prepare for redisplay by updating menu-bar item lists when
9289 appropriate. This can call eval. */
9290
9291 void
9292 prepare_menu_bars ()
9293 {
9294 int all_windows;
9295 struct gcpro gcpro1, gcpro2;
9296 struct frame *f;
9297 Lisp_Object tooltip_frame;
9298
9299 #ifdef HAVE_WINDOW_SYSTEM
9300 tooltip_frame = tip_frame;
9301 #else
9302 tooltip_frame = Qnil;
9303 #endif
9304
9305 /* Update all frame titles based on their buffer names, etc. We do
9306 this before the menu bars so that the buffer-menu will show the
9307 up-to-date frame titles. */
9308 #ifdef HAVE_WINDOW_SYSTEM
9309 if (windows_or_buffers_changed || update_mode_lines)
9310 {
9311 Lisp_Object tail, frame;
9312
9313 FOR_EACH_FRAME (tail, frame)
9314 {
9315 f = XFRAME (frame);
9316 if (!EQ (frame, tooltip_frame)
9317 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9318 x_consider_frame_title (frame);
9319 }
9320 }
9321 #endif /* HAVE_WINDOW_SYSTEM */
9322
9323 /* Update the menu bar item lists, if appropriate. This has to be
9324 done before any actual redisplay or generation of display lines. */
9325 all_windows = (update_mode_lines
9326 || buffer_shared > 1
9327 || windows_or_buffers_changed);
9328 if (all_windows)
9329 {
9330 Lisp_Object tail, frame;
9331 int count = SPECPDL_INDEX ();
9332 /* 1 means that update_menu_bar has run its hooks
9333 so any further calls to update_menu_bar shouldn't do so again. */
9334 int menu_bar_hooks_run = 0;
9335
9336 record_unwind_save_match_data ();
9337
9338 FOR_EACH_FRAME (tail, frame)
9339 {
9340 f = XFRAME (frame);
9341
9342 /* Ignore tooltip frame. */
9343 if (EQ (frame, tooltip_frame))
9344 continue;
9345
9346 /* If a window on this frame changed size, report that to
9347 the user and clear the size-change flag. */
9348 if (FRAME_WINDOW_SIZES_CHANGED (f))
9349 {
9350 Lisp_Object functions;
9351
9352 /* Clear flag first in case we get an error below. */
9353 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9354 functions = Vwindow_size_change_functions;
9355 GCPRO2 (tail, functions);
9356
9357 while (CONSP (functions))
9358 {
9359 call1 (XCAR (functions), frame);
9360 functions = XCDR (functions);
9361 }
9362 UNGCPRO;
9363 }
9364
9365 GCPRO1 (tail);
9366 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9367 #ifdef HAVE_WINDOW_SYSTEM
9368 update_tool_bar (f, 0);
9369 #ifdef MAC_OS
9370 mac_update_title_bar (f, 0);
9371 #endif
9372 #endif
9373 UNGCPRO;
9374 }
9375
9376 unbind_to (count, Qnil);
9377 }
9378 else
9379 {
9380 struct frame *sf = SELECTED_FRAME ();
9381 update_menu_bar (sf, 1, 0);
9382 #ifdef HAVE_WINDOW_SYSTEM
9383 update_tool_bar (sf, 1);
9384 #ifdef MAC_OS
9385 mac_update_title_bar (sf, 1);
9386 #endif
9387 #endif
9388 }
9389
9390 /* Motif needs this. See comment in xmenu.c. Turn it off when
9391 pending_menu_activation is not defined. */
9392 #ifdef USE_X_TOOLKIT
9393 pending_menu_activation = 0;
9394 #endif
9395 }
9396
9397
9398 /* Update the menu bar item list for frame F. This has to be done
9399 before we start to fill in any display lines, because it can call
9400 eval.
9401
9402 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9403
9404 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9405 already ran the menu bar hooks for this redisplay, so there
9406 is no need to run them again. The return value is the
9407 updated value of this flag, to pass to the next call. */
9408
9409 static int
9410 update_menu_bar (f, save_match_data, hooks_run)
9411 struct frame *f;
9412 int save_match_data;
9413 int hooks_run;
9414 {
9415 Lisp_Object window;
9416 register struct window *w;
9417
9418 /* If called recursively during a menu update, do nothing. This can
9419 happen when, for instance, an activate-menubar-hook causes a
9420 redisplay. */
9421 if (inhibit_menubar_update)
9422 return hooks_run;
9423
9424 window = FRAME_SELECTED_WINDOW (f);
9425 w = XWINDOW (window);
9426
9427 #if 0 /* The if statement below this if statement used to include the
9428 condition !NILP (w->update_mode_line), rather than using
9429 update_mode_lines directly, and this if statement may have
9430 been added to make that condition work. Now the if
9431 statement below matches its comment, this isn't needed. */
9432 if (update_mode_lines)
9433 w->update_mode_line = Qt;
9434 #endif
9435
9436 if (FRAME_WINDOW_P (f)
9437 ?
9438 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9439 || defined (USE_GTK)
9440 FRAME_EXTERNAL_MENU_BAR (f)
9441 #else
9442 FRAME_MENU_BAR_LINES (f) > 0
9443 #endif
9444 : FRAME_MENU_BAR_LINES (f) > 0)
9445 {
9446 /* If the user has switched buffers or windows, we need to
9447 recompute to reflect the new bindings. But we'll
9448 recompute when update_mode_lines is set too; that means
9449 that people can use force-mode-line-update to request
9450 that the menu bar be recomputed. The adverse effect on
9451 the rest of the redisplay algorithm is about the same as
9452 windows_or_buffers_changed anyway. */
9453 if (windows_or_buffers_changed
9454 /* This used to test w->update_mode_line, but we believe
9455 there is no need to recompute the menu in that case. */
9456 || update_mode_lines
9457 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9458 < BUF_MODIFF (XBUFFER (w->buffer)))
9459 != !NILP (w->last_had_star))
9460 || ((!NILP (Vtransient_mark_mode)
9461 && !NILP (XBUFFER (w->buffer)->mark_active))
9462 != !NILP (w->region_showing)))
9463 {
9464 struct buffer *prev = current_buffer;
9465 int count = SPECPDL_INDEX ();
9466
9467 specbind (Qinhibit_menubar_update, Qt);
9468
9469 set_buffer_internal_1 (XBUFFER (w->buffer));
9470 if (save_match_data)
9471 record_unwind_save_match_data ();
9472 if (NILP (Voverriding_local_map_menu_flag))
9473 {
9474 specbind (Qoverriding_terminal_local_map, Qnil);
9475 specbind (Qoverriding_local_map, Qnil);
9476 }
9477
9478 if (!hooks_run)
9479 {
9480 /* Run the Lucid hook. */
9481 safe_run_hooks (Qactivate_menubar_hook);
9482
9483 /* If it has changed current-menubar from previous value,
9484 really recompute the menu-bar from the value. */
9485 if (! NILP (Vlucid_menu_bar_dirty_flag))
9486 call0 (Qrecompute_lucid_menubar);
9487
9488 safe_run_hooks (Qmenu_bar_update_hook);
9489
9490 hooks_run = 1;
9491 }
9492
9493 XSETFRAME (Vmenu_updating_frame, f);
9494 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9495
9496 /* Redisplay the menu bar in case we changed it. */
9497 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9498 || defined (USE_GTK)
9499 if (FRAME_WINDOW_P (f))
9500 {
9501 #ifdef MAC_OS
9502 /* All frames on Mac OS share the same menubar. So only
9503 the selected frame should be allowed to set it. */
9504 if (f == SELECTED_FRAME ())
9505 #endif
9506 set_frame_menubar (f, 0, 0);
9507 }
9508 else
9509 /* On a terminal screen, the menu bar is an ordinary screen
9510 line, and this makes it get updated. */
9511 w->update_mode_line = Qt;
9512 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9513 /* In the non-toolkit version, the menu bar is an ordinary screen
9514 line, and this makes it get updated. */
9515 w->update_mode_line = Qt;
9516 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9517
9518 unbind_to (count, Qnil);
9519 set_buffer_internal_1 (prev);
9520 }
9521 }
9522
9523 return hooks_run;
9524 }
9525
9526
9527 \f
9528 /***********************************************************************
9529 Output Cursor
9530 ***********************************************************************/
9531
9532 #ifdef HAVE_WINDOW_SYSTEM
9533
9534 /* EXPORT:
9535 Nominal cursor position -- where to draw output.
9536 HPOS and VPOS are window relative glyph matrix coordinates.
9537 X and Y are window relative pixel coordinates. */
9538
9539 struct cursor_pos output_cursor;
9540
9541
9542 /* EXPORT:
9543 Set the global variable output_cursor to CURSOR. All cursor
9544 positions are relative to updated_window. */
9545
9546 void
9547 set_output_cursor (cursor)
9548 struct cursor_pos *cursor;
9549 {
9550 output_cursor.hpos = cursor->hpos;
9551 output_cursor.vpos = cursor->vpos;
9552 output_cursor.x = cursor->x;
9553 output_cursor.y = cursor->y;
9554 }
9555
9556
9557 /* EXPORT for RIF:
9558 Set a nominal cursor position.
9559
9560 HPOS and VPOS are column/row positions in a window glyph matrix. X
9561 and Y are window text area relative pixel positions.
9562
9563 If this is done during an update, updated_window will contain the
9564 window that is being updated and the position is the future output
9565 cursor position for that window. If updated_window is null, use
9566 selected_window and display the cursor at the given position. */
9567
9568 void
9569 x_cursor_to (vpos, hpos, y, x)
9570 int vpos, hpos, y, x;
9571 {
9572 struct window *w;
9573
9574 /* If updated_window is not set, work on selected_window. */
9575 if (updated_window)
9576 w = updated_window;
9577 else
9578 w = XWINDOW (selected_window);
9579
9580 /* Set the output cursor. */
9581 output_cursor.hpos = hpos;
9582 output_cursor.vpos = vpos;
9583 output_cursor.x = x;
9584 output_cursor.y = y;
9585
9586 /* If not called as part of an update, really display the cursor.
9587 This will also set the cursor position of W. */
9588 if (updated_window == NULL)
9589 {
9590 BLOCK_INPUT;
9591 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9592 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9593 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9594 UNBLOCK_INPUT;
9595 }
9596 }
9597
9598 #endif /* HAVE_WINDOW_SYSTEM */
9599
9600 \f
9601 /***********************************************************************
9602 Tool-bars
9603 ***********************************************************************/
9604
9605 #ifdef HAVE_WINDOW_SYSTEM
9606
9607 /* Where the mouse was last time we reported a mouse event. */
9608
9609 FRAME_PTR last_mouse_frame;
9610
9611 /* Tool-bar item index of the item on which a mouse button was pressed
9612 or -1. */
9613
9614 int last_tool_bar_item;
9615
9616
9617 /* Update the tool-bar item list for frame F. This has to be done
9618 before we start to fill in any display lines. Called from
9619 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9620 and restore it here. */
9621
9622 static void
9623 update_tool_bar (f, save_match_data)
9624 struct frame *f;
9625 int save_match_data;
9626 {
9627 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9628 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9629 #else
9630 int do_update = WINDOWP (f->tool_bar_window)
9631 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9632 #endif
9633
9634 if (do_update)
9635 {
9636 Lisp_Object window;
9637 struct window *w;
9638
9639 window = FRAME_SELECTED_WINDOW (f);
9640 w = XWINDOW (window);
9641
9642 /* If the user has switched buffers or windows, we need to
9643 recompute to reflect the new bindings. But we'll
9644 recompute when update_mode_lines is set too; that means
9645 that people can use force-mode-line-update to request
9646 that the menu bar be recomputed. The adverse effect on
9647 the rest of the redisplay algorithm is about the same as
9648 windows_or_buffers_changed anyway. */
9649 if (windows_or_buffers_changed
9650 || !NILP (w->update_mode_line)
9651 || update_mode_lines
9652 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9653 < BUF_MODIFF (XBUFFER (w->buffer)))
9654 != !NILP (w->last_had_star))
9655 || ((!NILP (Vtransient_mark_mode)
9656 && !NILP (XBUFFER (w->buffer)->mark_active))
9657 != !NILP (w->region_showing)))
9658 {
9659 struct buffer *prev = current_buffer;
9660 int count = SPECPDL_INDEX ();
9661 Lisp_Object new_tool_bar;
9662 int new_n_tool_bar;
9663 struct gcpro gcpro1;
9664
9665 /* Set current_buffer to the buffer of the selected
9666 window of the frame, so that we get the right local
9667 keymaps. */
9668 set_buffer_internal_1 (XBUFFER (w->buffer));
9669
9670 /* Save match data, if we must. */
9671 if (save_match_data)
9672 record_unwind_save_match_data ();
9673
9674 /* Make sure that we don't accidentally use bogus keymaps. */
9675 if (NILP (Voverriding_local_map_menu_flag))
9676 {
9677 specbind (Qoverriding_terminal_local_map, Qnil);
9678 specbind (Qoverriding_local_map, Qnil);
9679 }
9680
9681 GCPRO1 (new_tool_bar);
9682
9683 /* Build desired tool-bar items from keymaps. */
9684 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9685 &new_n_tool_bar);
9686
9687 /* Redisplay the tool-bar if we changed it. */
9688 if (new_n_tool_bar != f->n_tool_bar_items
9689 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9690 {
9691 /* Redisplay that happens asynchronously due to an expose event
9692 may access f->tool_bar_items. Make sure we update both
9693 variables within BLOCK_INPUT so no such event interrupts. */
9694 BLOCK_INPUT;
9695 f->tool_bar_items = new_tool_bar;
9696 f->n_tool_bar_items = new_n_tool_bar;
9697 w->update_mode_line = Qt;
9698 UNBLOCK_INPUT;
9699 }
9700
9701 UNGCPRO;
9702
9703 unbind_to (count, Qnil);
9704 set_buffer_internal_1 (prev);
9705 }
9706 }
9707 }
9708
9709
9710 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9711 F's desired tool-bar contents. F->tool_bar_items must have
9712 been set up previously by calling prepare_menu_bars. */
9713
9714 static void
9715 build_desired_tool_bar_string (f)
9716 struct frame *f;
9717 {
9718 int i, size, size_needed;
9719 struct gcpro gcpro1, gcpro2, gcpro3;
9720 Lisp_Object image, plist, props;
9721
9722 image = plist = props = Qnil;
9723 GCPRO3 (image, plist, props);
9724
9725 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9726 Otherwise, make a new string. */
9727
9728 /* The size of the string we might be able to reuse. */
9729 size = (STRINGP (f->desired_tool_bar_string)
9730 ? SCHARS (f->desired_tool_bar_string)
9731 : 0);
9732
9733 /* We need one space in the string for each image. */
9734 size_needed = f->n_tool_bar_items;
9735
9736 /* Reuse f->desired_tool_bar_string, if possible. */
9737 if (size < size_needed || NILP (f->desired_tool_bar_string))
9738 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9739 make_number (' '));
9740 else
9741 {
9742 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9743 Fremove_text_properties (make_number (0), make_number (size),
9744 props, f->desired_tool_bar_string);
9745 }
9746
9747 /* Put a `display' property on the string for the images to display,
9748 put a `menu_item' property on tool-bar items with a value that
9749 is the index of the item in F's tool-bar item vector. */
9750 for (i = 0; i < f->n_tool_bar_items; ++i)
9751 {
9752 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9753
9754 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9755 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9756 int hmargin, vmargin, relief, idx, end;
9757 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9758
9759 /* If image is a vector, choose the image according to the
9760 button state. */
9761 image = PROP (TOOL_BAR_ITEM_IMAGES);
9762 if (VECTORP (image))
9763 {
9764 if (enabled_p)
9765 idx = (selected_p
9766 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9767 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9768 else
9769 idx = (selected_p
9770 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9771 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9772
9773 xassert (ASIZE (image) >= idx);
9774 image = AREF (image, idx);
9775 }
9776 else
9777 idx = -1;
9778
9779 /* Ignore invalid image specifications. */
9780 if (!valid_image_p (image))
9781 continue;
9782
9783 /* Display the tool-bar button pressed, or depressed. */
9784 plist = Fcopy_sequence (XCDR (image));
9785
9786 /* Compute margin and relief to draw. */
9787 relief = (tool_bar_button_relief >= 0
9788 ? tool_bar_button_relief
9789 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9790 hmargin = vmargin = relief;
9791
9792 if (INTEGERP (Vtool_bar_button_margin)
9793 && XINT (Vtool_bar_button_margin) > 0)
9794 {
9795 hmargin += XFASTINT (Vtool_bar_button_margin);
9796 vmargin += XFASTINT (Vtool_bar_button_margin);
9797 }
9798 else if (CONSP (Vtool_bar_button_margin))
9799 {
9800 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9801 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9802 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9803
9804 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9805 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9806 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9807 }
9808
9809 if (auto_raise_tool_bar_buttons_p)
9810 {
9811 /* Add a `:relief' property to the image spec if the item is
9812 selected. */
9813 if (selected_p)
9814 {
9815 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9816 hmargin -= relief;
9817 vmargin -= relief;
9818 }
9819 }
9820 else
9821 {
9822 /* If image is selected, display it pressed, i.e. with a
9823 negative relief. If it's not selected, display it with a
9824 raised relief. */
9825 plist = Fplist_put (plist, QCrelief,
9826 (selected_p
9827 ? make_number (-relief)
9828 : make_number (relief)));
9829 hmargin -= relief;
9830 vmargin -= relief;
9831 }
9832
9833 /* Put a margin around the image. */
9834 if (hmargin || vmargin)
9835 {
9836 if (hmargin == vmargin)
9837 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9838 else
9839 plist = Fplist_put (plist, QCmargin,
9840 Fcons (make_number (hmargin),
9841 make_number (vmargin)));
9842 }
9843
9844 /* If button is not enabled, and we don't have special images
9845 for the disabled state, make the image appear disabled by
9846 applying an appropriate algorithm to it. */
9847 if (!enabled_p && idx < 0)
9848 plist = Fplist_put (plist, QCconversion, Qdisabled);
9849
9850 /* Put a `display' text property on the string for the image to
9851 display. Put a `menu-item' property on the string that gives
9852 the start of this item's properties in the tool-bar items
9853 vector. */
9854 image = Fcons (Qimage, plist);
9855 props = list4 (Qdisplay, image,
9856 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9857
9858 /* Let the last image hide all remaining spaces in the tool bar
9859 string. The string can be longer than needed when we reuse a
9860 previous string. */
9861 if (i + 1 == f->n_tool_bar_items)
9862 end = SCHARS (f->desired_tool_bar_string);
9863 else
9864 end = i + 1;
9865 Fadd_text_properties (make_number (i), make_number (end),
9866 props, f->desired_tool_bar_string);
9867 #undef PROP
9868 }
9869
9870 UNGCPRO;
9871 }
9872
9873
9874 /* Display one line of the tool-bar of frame IT->f.
9875
9876 HEIGHT specifies the desired height of the tool-bar line.
9877 If the actual height of the glyph row is less than HEIGHT, the
9878 row's height is increased to HEIGHT, and the icons are centered
9879 vertically in the new height.
9880
9881 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9882 count a final empty row in case the tool-bar width exactly matches
9883 the window width.
9884 */
9885
9886 static void
9887 display_tool_bar_line (it, height)
9888 struct it *it;
9889 int height;
9890 {
9891 struct glyph_row *row = it->glyph_row;
9892 int max_x = it->last_visible_x;
9893 struct glyph *last;
9894
9895 prepare_desired_row (row);
9896 row->y = it->current_y;
9897
9898 /* Note that this isn't made use of if the face hasn't a box,
9899 so there's no need to check the face here. */
9900 it->start_of_box_run_p = 1;
9901
9902 while (it->current_x < max_x)
9903 {
9904 int x, n_glyphs_before, i, nglyphs;
9905 struct it it_before;
9906
9907 /* Get the next display element. */
9908 if (!get_next_display_element (it))
9909 {
9910 /* Don't count empty row if we are counting needed tool-bar lines. */
9911 if (height < 0 && !it->hpos)
9912 return;
9913 break;
9914 }
9915
9916 /* Produce glyphs. */
9917 n_glyphs_before = row->used[TEXT_AREA];
9918 it_before = *it;
9919
9920 PRODUCE_GLYPHS (it);
9921
9922 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9923 i = 0;
9924 x = it_before.current_x;
9925 while (i < nglyphs)
9926 {
9927 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9928
9929 if (x + glyph->pixel_width > max_x)
9930 {
9931 /* Glyph doesn't fit on line. Backtrack. */
9932 row->used[TEXT_AREA] = n_glyphs_before;
9933 *it = it_before;
9934 /* If this is the only glyph on this line, it will never fit on the
9935 toolbar, so skip it. But ensure there is at least one glyph,
9936 so we don't accidentally disable the tool-bar. */
9937 if (n_glyphs_before == 0
9938 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9939 break;
9940 goto out;
9941 }
9942
9943 ++it->hpos;
9944 x += glyph->pixel_width;
9945 ++i;
9946 }
9947
9948 /* Stop at line ends. */
9949 if (ITERATOR_AT_END_OF_LINE_P (it))
9950 break;
9951
9952 set_iterator_to_next (it, 1);
9953 }
9954
9955 out:;
9956
9957 row->displays_text_p = row->used[TEXT_AREA] != 0;
9958
9959 /* Use default face for the border below the tool bar.
9960
9961 FIXME: When auto-resize-tool-bars is grow-only, there is
9962 no additional border below the possibly empty tool-bar lines.
9963 So to make the extra empty lines look "normal", we have to
9964 use the tool-bar face for the border too. */
9965 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9966 it->face_id = DEFAULT_FACE_ID;
9967
9968 extend_face_to_end_of_line (it);
9969 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9970 last->right_box_line_p = 1;
9971 if (last == row->glyphs[TEXT_AREA])
9972 last->left_box_line_p = 1;
9973
9974 /* Make line the desired height and center it vertically. */
9975 if ((height -= it->max_ascent + it->max_descent) > 0)
9976 {
9977 /* Don't add more than one line height. */
9978 height %= FRAME_LINE_HEIGHT (it->f);
9979 it->max_ascent += height / 2;
9980 it->max_descent += (height + 1) / 2;
9981 }
9982
9983 compute_line_metrics (it);
9984
9985 /* If line is empty, make it occupy the rest of the tool-bar. */
9986 if (!row->displays_text_p)
9987 {
9988 row->height = row->phys_height = it->last_visible_y - row->y;
9989 row->visible_height = row->height;
9990 row->ascent = row->phys_ascent = 0;
9991 row->extra_line_spacing = 0;
9992 }
9993
9994 row->full_width_p = 1;
9995 row->continued_p = 0;
9996 row->truncated_on_left_p = 0;
9997 row->truncated_on_right_p = 0;
9998
9999 it->current_x = it->hpos = 0;
10000 it->current_y += row->height;
10001 ++it->vpos;
10002 ++it->glyph_row;
10003 }
10004
10005
10006 /* Max tool-bar height. */
10007
10008 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10009 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10010
10011 /* Value is the number of screen lines needed to make all tool-bar
10012 items of frame F visible. The number of actual rows needed is
10013 returned in *N_ROWS if non-NULL. */
10014
10015 static int
10016 tool_bar_lines_needed (f, n_rows)
10017 struct frame *f;
10018 int *n_rows;
10019 {
10020 struct window *w = XWINDOW (f->tool_bar_window);
10021 struct it it;
10022 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10023 the desired matrix, so use (unused) mode-line row as temporary row to
10024 avoid destroying the first tool-bar row. */
10025 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10026
10027 /* Initialize an iterator for iteration over
10028 F->desired_tool_bar_string in the tool-bar window of frame F. */
10029 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10030 it.first_visible_x = 0;
10031 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10032 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10033
10034 while (!ITERATOR_AT_END_P (&it))
10035 {
10036 clear_glyph_row (temp_row);
10037 it.glyph_row = temp_row;
10038 display_tool_bar_line (&it, -1);
10039 }
10040 clear_glyph_row (temp_row);
10041
10042 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10043 if (n_rows)
10044 *n_rows = it.vpos > 0 ? it.vpos : -1;
10045
10046 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10047 }
10048
10049
10050 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10051 0, 1, 0,
10052 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10053 (frame)
10054 Lisp_Object frame;
10055 {
10056 struct frame *f;
10057 struct window *w;
10058 int nlines = 0;
10059
10060 if (NILP (frame))
10061 frame = selected_frame;
10062 else
10063 CHECK_FRAME (frame);
10064 f = XFRAME (frame);
10065
10066 if (WINDOWP (f->tool_bar_window)
10067 || (w = XWINDOW (f->tool_bar_window),
10068 WINDOW_TOTAL_LINES (w) > 0))
10069 {
10070 update_tool_bar (f, 1);
10071 if (f->n_tool_bar_items)
10072 {
10073 build_desired_tool_bar_string (f);
10074 nlines = tool_bar_lines_needed (f, NULL);
10075 }
10076 }
10077
10078 return make_number (nlines);
10079 }
10080
10081
10082 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10083 height should be changed. */
10084
10085 static int
10086 redisplay_tool_bar (f)
10087 struct frame *f;
10088 {
10089 struct window *w;
10090 struct it it;
10091 struct glyph_row *row;
10092
10093 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10094 if (FRAME_EXTERNAL_TOOL_BAR (f))
10095 update_frame_tool_bar (f);
10096 return 0;
10097 #endif
10098
10099 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10100 do anything. This means you must start with tool-bar-lines
10101 non-zero to get the auto-sizing effect. Or in other words, you
10102 can turn off tool-bars by specifying tool-bar-lines zero. */
10103 if (!WINDOWP (f->tool_bar_window)
10104 || (w = XWINDOW (f->tool_bar_window),
10105 WINDOW_TOTAL_LINES (w) == 0))
10106 return 0;
10107
10108 /* Set up an iterator for the tool-bar window. */
10109 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10110 it.first_visible_x = 0;
10111 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10112 row = it.glyph_row;
10113
10114 /* Build a string that represents the contents of the tool-bar. */
10115 build_desired_tool_bar_string (f);
10116 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10117
10118 if (f->n_tool_bar_rows == 0)
10119 {
10120 int nlines;
10121
10122 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10123 nlines != WINDOW_TOTAL_LINES (w)))
10124 {
10125 extern Lisp_Object Qtool_bar_lines;
10126 Lisp_Object frame;
10127 int old_height = WINDOW_TOTAL_LINES (w);
10128
10129 XSETFRAME (frame, f);
10130 Fmodify_frame_parameters (frame,
10131 Fcons (Fcons (Qtool_bar_lines,
10132 make_number (nlines)),
10133 Qnil));
10134 if (WINDOW_TOTAL_LINES (w) != old_height)
10135 {
10136 clear_glyph_matrix (w->desired_matrix);
10137 fonts_changed_p = 1;
10138 return 1;
10139 }
10140 }
10141 }
10142
10143 /* Display as many lines as needed to display all tool-bar items. */
10144
10145 if (f->n_tool_bar_rows > 0)
10146 {
10147 int border, rows, height, extra;
10148
10149 if (INTEGERP (Vtool_bar_border))
10150 border = XINT (Vtool_bar_border);
10151 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10152 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10153 else if (EQ (Vtool_bar_border, Qborder_width))
10154 border = f->border_width;
10155 else
10156 border = 0;
10157 if (border < 0)
10158 border = 0;
10159
10160 rows = f->n_tool_bar_rows;
10161 height = max (1, (it.last_visible_y - border) / rows);
10162 extra = it.last_visible_y - border - height * rows;
10163
10164 while (it.current_y < it.last_visible_y)
10165 {
10166 int h = 0;
10167 if (extra > 0 && rows-- > 0)
10168 {
10169 h = (extra + rows - 1) / rows;
10170 extra -= h;
10171 }
10172 display_tool_bar_line (&it, height + h);
10173 }
10174 }
10175 else
10176 {
10177 while (it.current_y < it.last_visible_y)
10178 display_tool_bar_line (&it, 0);
10179 }
10180
10181 /* It doesn't make much sense to try scrolling in the tool-bar
10182 window, so don't do it. */
10183 w->desired_matrix->no_scrolling_p = 1;
10184 w->must_be_updated_p = 1;
10185
10186 if (!NILP (Vauto_resize_tool_bars))
10187 {
10188 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10189 int change_height_p = 0;
10190
10191 /* If we couldn't display everything, change the tool-bar's
10192 height if there is room for more. */
10193 if (IT_STRING_CHARPOS (it) < it.end_charpos
10194 && it.current_y < max_tool_bar_height)
10195 change_height_p = 1;
10196
10197 row = it.glyph_row - 1;
10198
10199 /* If there are blank lines at the end, except for a partially
10200 visible blank line at the end that is smaller than
10201 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10202 if (!row->displays_text_p
10203 && row->height >= FRAME_LINE_HEIGHT (f))
10204 change_height_p = 1;
10205
10206 /* If row displays tool-bar items, but is partially visible,
10207 change the tool-bar's height. */
10208 if (row->displays_text_p
10209 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10210 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10211 change_height_p = 1;
10212
10213 /* Resize windows as needed by changing the `tool-bar-lines'
10214 frame parameter. */
10215 if (change_height_p)
10216 {
10217 extern Lisp_Object Qtool_bar_lines;
10218 Lisp_Object frame;
10219 int old_height = WINDOW_TOTAL_LINES (w);
10220 int nrows;
10221 int nlines = tool_bar_lines_needed (f, &nrows);
10222
10223 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10224 && !f->minimize_tool_bar_window_p)
10225 ? (nlines > old_height)
10226 : (nlines != old_height));
10227 f->minimize_tool_bar_window_p = 0;
10228
10229 if (change_height_p)
10230 {
10231 XSETFRAME (frame, f);
10232 Fmodify_frame_parameters (frame,
10233 Fcons (Fcons (Qtool_bar_lines,
10234 make_number (nlines)),
10235 Qnil));
10236 if (WINDOW_TOTAL_LINES (w) != old_height)
10237 {
10238 clear_glyph_matrix (w->desired_matrix);
10239 f->n_tool_bar_rows = nrows;
10240 fonts_changed_p = 1;
10241 return 1;
10242 }
10243 }
10244 }
10245 }
10246
10247 f->minimize_tool_bar_window_p = 0;
10248 return 0;
10249 }
10250
10251
10252 /* Get information about the tool-bar item which is displayed in GLYPH
10253 on frame F. Return in *PROP_IDX the index where tool-bar item
10254 properties start in F->tool_bar_items. Value is zero if
10255 GLYPH doesn't display a tool-bar item. */
10256
10257 static int
10258 tool_bar_item_info (f, glyph, prop_idx)
10259 struct frame *f;
10260 struct glyph *glyph;
10261 int *prop_idx;
10262 {
10263 Lisp_Object prop;
10264 int success_p;
10265 int charpos;
10266
10267 /* This function can be called asynchronously, which means we must
10268 exclude any possibility that Fget_text_property signals an
10269 error. */
10270 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10271 charpos = max (0, charpos);
10272
10273 /* Get the text property `menu-item' at pos. The value of that
10274 property is the start index of this item's properties in
10275 F->tool_bar_items. */
10276 prop = Fget_text_property (make_number (charpos),
10277 Qmenu_item, f->current_tool_bar_string);
10278 if (INTEGERP (prop))
10279 {
10280 *prop_idx = XINT (prop);
10281 success_p = 1;
10282 }
10283 else
10284 success_p = 0;
10285
10286 return success_p;
10287 }
10288
10289 \f
10290 /* Get information about the tool-bar item at position X/Y on frame F.
10291 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10292 the current matrix of the tool-bar window of F, or NULL if not
10293 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10294 item in F->tool_bar_items. Value is
10295
10296 -1 if X/Y is not on a tool-bar item
10297 0 if X/Y is on the same item that was highlighted before.
10298 1 otherwise. */
10299
10300 static int
10301 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10302 struct frame *f;
10303 int x, y;
10304 struct glyph **glyph;
10305 int *hpos, *vpos, *prop_idx;
10306 {
10307 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10308 struct window *w = XWINDOW (f->tool_bar_window);
10309 int area;
10310
10311 /* Find the glyph under X/Y. */
10312 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10313 if (*glyph == NULL)
10314 return -1;
10315
10316 /* Get the start of this tool-bar item's properties in
10317 f->tool_bar_items. */
10318 if (!tool_bar_item_info (f, *glyph, prop_idx))
10319 return -1;
10320
10321 /* Is mouse on the highlighted item? */
10322 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10323 && *vpos >= dpyinfo->mouse_face_beg_row
10324 && *vpos <= dpyinfo->mouse_face_end_row
10325 && (*vpos > dpyinfo->mouse_face_beg_row
10326 || *hpos >= dpyinfo->mouse_face_beg_col)
10327 && (*vpos < dpyinfo->mouse_face_end_row
10328 || *hpos < dpyinfo->mouse_face_end_col
10329 || dpyinfo->mouse_face_past_end))
10330 return 0;
10331
10332 return 1;
10333 }
10334
10335
10336 /* EXPORT:
10337 Handle mouse button event on the tool-bar of frame F, at
10338 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10339 0 for button release. MODIFIERS is event modifiers for button
10340 release. */
10341
10342 void
10343 handle_tool_bar_click (f, x, y, down_p, modifiers)
10344 struct frame *f;
10345 int x, y, down_p;
10346 unsigned int modifiers;
10347 {
10348 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10349 struct window *w = XWINDOW (f->tool_bar_window);
10350 int hpos, vpos, prop_idx;
10351 struct glyph *glyph;
10352 Lisp_Object enabled_p;
10353
10354 /* If not on the highlighted tool-bar item, return. */
10355 frame_to_window_pixel_xy (w, &x, &y);
10356 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10357 return;
10358
10359 /* If item is disabled, do nothing. */
10360 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10361 if (NILP (enabled_p))
10362 return;
10363
10364 if (down_p)
10365 {
10366 /* Show item in pressed state. */
10367 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10368 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10369 last_tool_bar_item = prop_idx;
10370 }
10371 else
10372 {
10373 Lisp_Object key, frame;
10374 struct input_event event;
10375 EVENT_INIT (event);
10376
10377 /* Show item in released state. */
10378 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10379 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10380
10381 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10382
10383 XSETFRAME (frame, f);
10384 event.kind = TOOL_BAR_EVENT;
10385 event.frame_or_window = frame;
10386 event.arg = frame;
10387 kbd_buffer_store_event (&event);
10388
10389 event.kind = TOOL_BAR_EVENT;
10390 event.frame_or_window = frame;
10391 event.arg = key;
10392 event.modifiers = modifiers;
10393 kbd_buffer_store_event (&event);
10394 last_tool_bar_item = -1;
10395 }
10396 }
10397
10398
10399 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10400 tool-bar window-relative coordinates X/Y. Called from
10401 note_mouse_highlight. */
10402
10403 static void
10404 note_tool_bar_highlight (f, x, y)
10405 struct frame *f;
10406 int x, y;
10407 {
10408 Lisp_Object window = f->tool_bar_window;
10409 struct window *w = XWINDOW (window);
10410 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10411 int hpos, vpos;
10412 struct glyph *glyph;
10413 struct glyph_row *row;
10414 int i;
10415 Lisp_Object enabled_p;
10416 int prop_idx;
10417 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10418 int mouse_down_p, rc;
10419
10420 /* Function note_mouse_highlight is called with negative x(y
10421 values when mouse moves outside of the frame. */
10422 if (x <= 0 || y <= 0)
10423 {
10424 clear_mouse_face (dpyinfo);
10425 return;
10426 }
10427
10428 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10429 if (rc < 0)
10430 {
10431 /* Not on tool-bar item. */
10432 clear_mouse_face (dpyinfo);
10433 return;
10434 }
10435 else if (rc == 0)
10436 /* On same tool-bar item as before. */
10437 goto set_help_echo;
10438
10439 clear_mouse_face (dpyinfo);
10440
10441 /* Mouse is down, but on different tool-bar item? */
10442 mouse_down_p = (dpyinfo->grabbed
10443 && f == last_mouse_frame
10444 && FRAME_LIVE_P (f));
10445 if (mouse_down_p
10446 && last_tool_bar_item != prop_idx)
10447 return;
10448
10449 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10450 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10451
10452 /* If tool-bar item is not enabled, don't highlight it. */
10453 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10454 if (!NILP (enabled_p))
10455 {
10456 /* Compute the x-position of the glyph. In front and past the
10457 image is a space. We include this in the highlighted area. */
10458 row = MATRIX_ROW (w->current_matrix, vpos);
10459 for (i = x = 0; i < hpos; ++i)
10460 x += row->glyphs[TEXT_AREA][i].pixel_width;
10461
10462 /* Record this as the current active region. */
10463 dpyinfo->mouse_face_beg_col = hpos;
10464 dpyinfo->mouse_face_beg_row = vpos;
10465 dpyinfo->mouse_face_beg_x = x;
10466 dpyinfo->mouse_face_beg_y = row->y;
10467 dpyinfo->mouse_face_past_end = 0;
10468
10469 dpyinfo->mouse_face_end_col = hpos + 1;
10470 dpyinfo->mouse_face_end_row = vpos;
10471 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10472 dpyinfo->mouse_face_end_y = row->y;
10473 dpyinfo->mouse_face_window = window;
10474 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10475
10476 /* Display it as active. */
10477 show_mouse_face (dpyinfo, draw);
10478 dpyinfo->mouse_face_image_state = draw;
10479 }
10480
10481 set_help_echo:
10482
10483 /* Set help_echo_string to a help string to display for this tool-bar item.
10484 XTread_socket does the rest. */
10485 help_echo_object = help_echo_window = Qnil;
10486 help_echo_pos = -1;
10487 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10488 if (NILP (help_echo_string))
10489 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10490 }
10491
10492 #endif /* HAVE_WINDOW_SYSTEM */
10493
10494
10495 \f
10496 /************************************************************************
10497 Horizontal scrolling
10498 ************************************************************************/
10499
10500 static int hscroll_window_tree P_ ((Lisp_Object));
10501 static int hscroll_windows P_ ((Lisp_Object));
10502
10503 /* For all leaf windows in the window tree rooted at WINDOW, set their
10504 hscroll value so that PT is (i) visible in the window, and (ii) so
10505 that it is not within a certain margin at the window's left and
10506 right border. Value is non-zero if any window's hscroll has been
10507 changed. */
10508
10509 static int
10510 hscroll_window_tree (window)
10511 Lisp_Object window;
10512 {
10513 int hscrolled_p = 0;
10514 int hscroll_relative_p = FLOATP (Vhscroll_step);
10515 int hscroll_step_abs = 0;
10516 double hscroll_step_rel = 0;
10517
10518 if (hscroll_relative_p)
10519 {
10520 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10521 if (hscroll_step_rel < 0)
10522 {
10523 hscroll_relative_p = 0;
10524 hscroll_step_abs = 0;
10525 }
10526 }
10527 else if (INTEGERP (Vhscroll_step))
10528 {
10529 hscroll_step_abs = XINT (Vhscroll_step);
10530 if (hscroll_step_abs < 0)
10531 hscroll_step_abs = 0;
10532 }
10533 else
10534 hscroll_step_abs = 0;
10535
10536 while (WINDOWP (window))
10537 {
10538 struct window *w = XWINDOW (window);
10539
10540 if (WINDOWP (w->hchild))
10541 hscrolled_p |= hscroll_window_tree (w->hchild);
10542 else if (WINDOWP (w->vchild))
10543 hscrolled_p |= hscroll_window_tree (w->vchild);
10544 else if (w->cursor.vpos >= 0)
10545 {
10546 int h_margin;
10547 int text_area_width;
10548 struct glyph_row *current_cursor_row
10549 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10550 struct glyph_row *desired_cursor_row
10551 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10552 struct glyph_row *cursor_row
10553 = (desired_cursor_row->enabled_p
10554 ? desired_cursor_row
10555 : current_cursor_row);
10556
10557 text_area_width = window_box_width (w, TEXT_AREA);
10558
10559 /* Scroll when cursor is inside this scroll margin. */
10560 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10561
10562 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10563 && ((XFASTINT (w->hscroll)
10564 && w->cursor.x <= h_margin)
10565 || (cursor_row->enabled_p
10566 && cursor_row->truncated_on_right_p
10567 && (w->cursor.x >= text_area_width - h_margin))))
10568 {
10569 struct it it;
10570 int hscroll;
10571 struct buffer *saved_current_buffer;
10572 int pt;
10573 int wanted_x;
10574
10575 /* Find point in a display of infinite width. */
10576 saved_current_buffer = current_buffer;
10577 current_buffer = XBUFFER (w->buffer);
10578
10579 if (w == XWINDOW (selected_window))
10580 pt = BUF_PT (current_buffer);
10581 else
10582 {
10583 pt = marker_position (w->pointm);
10584 pt = max (BEGV, pt);
10585 pt = min (ZV, pt);
10586 }
10587
10588 /* Move iterator to pt starting at cursor_row->start in
10589 a line with infinite width. */
10590 init_to_row_start (&it, w, cursor_row);
10591 it.last_visible_x = INFINITY;
10592 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10593 current_buffer = saved_current_buffer;
10594
10595 /* Position cursor in window. */
10596 if (!hscroll_relative_p && hscroll_step_abs == 0)
10597 hscroll = max (0, (it.current_x
10598 - (ITERATOR_AT_END_OF_LINE_P (&it)
10599 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10600 : (text_area_width / 2))))
10601 / FRAME_COLUMN_WIDTH (it.f);
10602 else if (w->cursor.x >= text_area_width - h_margin)
10603 {
10604 if (hscroll_relative_p)
10605 wanted_x = text_area_width * (1 - hscroll_step_rel)
10606 - h_margin;
10607 else
10608 wanted_x = text_area_width
10609 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10610 - h_margin;
10611 hscroll
10612 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10613 }
10614 else
10615 {
10616 if (hscroll_relative_p)
10617 wanted_x = text_area_width * hscroll_step_rel
10618 + h_margin;
10619 else
10620 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10621 + h_margin;
10622 hscroll
10623 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10624 }
10625 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10626
10627 /* Don't call Fset_window_hscroll if value hasn't
10628 changed because it will prevent redisplay
10629 optimizations. */
10630 if (XFASTINT (w->hscroll) != hscroll)
10631 {
10632 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10633 w->hscroll = make_number (hscroll);
10634 hscrolled_p = 1;
10635 }
10636 }
10637 }
10638
10639 window = w->next;
10640 }
10641
10642 /* Value is non-zero if hscroll of any leaf window has been changed. */
10643 return hscrolled_p;
10644 }
10645
10646
10647 /* Set hscroll so that cursor is visible and not inside horizontal
10648 scroll margins for all windows in the tree rooted at WINDOW. See
10649 also hscroll_window_tree above. Value is non-zero if any window's
10650 hscroll has been changed. If it has, desired matrices on the frame
10651 of WINDOW are cleared. */
10652
10653 static int
10654 hscroll_windows (window)
10655 Lisp_Object window;
10656 {
10657 int hscrolled_p = hscroll_window_tree (window);
10658 if (hscrolled_p)
10659 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10660 return hscrolled_p;
10661 }
10662
10663
10664 \f
10665 /************************************************************************
10666 Redisplay
10667 ************************************************************************/
10668
10669 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10670 to a non-zero value. This is sometimes handy to have in a debugger
10671 session. */
10672
10673 #if GLYPH_DEBUG
10674
10675 /* First and last unchanged row for try_window_id. */
10676
10677 int debug_first_unchanged_at_end_vpos;
10678 int debug_last_unchanged_at_beg_vpos;
10679
10680 /* Delta vpos and y. */
10681
10682 int debug_dvpos, debug_dy;
10683
10684 /* Delta in characters and bytes for try_window_id. */
10685
10686 int debug_delta, debug_delta_bytes;
10687
10688 /* Values of window_end_pos and window_end_vpos at the end of
10689 try_window_id. */
10690
10691 EMACS_INT debug_end_pos, debug_end_vpos;
10692
10693 /* Append a string to W->desired_matrix->method. FMT is a printf
10694 format string. A1...A9 are a supplement for a variable-length
10695 argument list. If trace_redisplay_p is non-zero also printf the
10696 resulting string to stderr. */
10697
10698 static void
10699 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10700 struct window *w;
10701 char *fmt;
10702 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10703 {
10704 char buffer[512];
10705 char *method = w->desired_matrix->method;
10706 int len = strlen (method);
10707 int size = sizeof w->desired_matrix->method;
10708 int remaining = size - len - 1;
10709
10710 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10711 if (len && remaining)
10712 {
10713 method[len] = '|';
10714 --remaining, ++len;
10715 }
10716
10717 strncpy (method + len, buffer, remaining);
10718
10719 if (trace_redisplay_p)
10720 fprintf (stderr, "%p (%s): %s\n",
10721 w,
10722 ((BUFFERP (w->buffer)
10723 && STRINGP (XBUFFER (w->buffer)->name))
10724 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10725 : "no buffer"),
10726 buffer);
10727 }
10728
10729 #endif /* GLYPH_DEBUG */
10730
10731
10732 /* Value is non-zero if all changes in window W, which displays
10733 current_buffer, are in the text between START and END. START is a
10734 buffer position, END is given as a distance from Z. Used in
10735 redisplay_internal for display optimization. */
10736
10737 static INLINE int
10738 text_outside_line_unchanged_p (w, start, end)
10739 struct window *w;
10740 int start, end;
10741 {
10742 int unchanged_p = 1;
10743
10744 /* If text or overlays have changed, see where. */
10745 if (XFASTINT (w->last_modified) < MODIFF
10746 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10747 {
10748 /* Gap in the line? */
10749 if (GPT < start || Z - GPT < end)
10750 unchanged_p = 0;
10751
10752 /* Changes start in front of the line, or end after it? */
10753 if (unchanged_p
10754 && (BEG_UNCHANGED < start - 1
10755 || END_UNCHANGED < end))
10756 unchanged_p = 0;
10757
10758 /* If selective display, can't optimize if changes start at the
10759 beginning of the line. */
10760 if (unchanged_p
10761 && INTEGERP (current_buffer->selective_display)
10762 && XINT (current_buffer->selective_display) > 0
10763 && (BEG_UNCHANGED < start || GPT <= start))
10764 unchanged_p = 0;
10765
10766 /* If there are overlays at the start or end of the line, these
10767 may have overlay strings with newlines in them. A change at
10768 START, for instance, may actually concern the display of such
10769 overlay strings as well, and they are displayed on different
10770 lines. So, quickly rule out this case. (For the future, it
10771 might be desirable to implement something more telling than
10772 just BEG/END_UNCHANGED.) */
10773 if (unchanged_p)
10774 {
10775 if (BEG + BEG_UNCHANGED == start
10776 && overlay_touches_p (start))
10777 unchanged_p = 0;
10778 if (END_UNCHANGED == end
10779 && overlay_touches_p (Z - end))
10780 unchanged_p = 0;
10781 }
10782 }
10783
10784 return unchanged_p;
10785 }
10786
10787
10788 /* Do a frame update, taking possible shortcuts into account. This is
10789 the main external entry point for redisplay.
10790
10791 If the last redisplay displayed an echo area message and that message
10792 is no longer requested, we clear the echo area or bring back the
10793 mini-buffer if that is in use. */
10794
10795 void
10796 redisplay ()
10797 {
10798 redisplay_internal (0);
10799 }
10800
10801
10802 static Lisp_Object
10803 overlay_arrow_string_or_property (var)
10804 Lisp_Object var;
10805 {
10806 Lisp_Object val;
10807
10808 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10809 return val;
10810
10811 return Voverlay_arrow_string;
10812 }
10813
10814 /* Return 1 if there are any overlay-arrows in current_buffer. */
10815 static int
10816 overlay_arrow_in_current_buffer_p ()
10817 {
10818 Lisp_Object vlist;
10819
10820 for (vlist = Voverlay_arrow_variable_list;
10821 CONSP (vlist);
10822 vlist = XCDR (vlist))
10823 {
10824 Lisp_Object var = XCAR (vlist);
10825 Lisp_Object val;
10826
10827 if (!SYMBOLP (var))
10828 continue;
10829 val = find_symbol_value (var);
10830 if (MARKERP (val)
10831 && current_buffer == XMARKER (val)->buffer)
10832 return 1;
10833 }
10834 return 0;
10835 }
10836
10837
10838 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10839 has changed. */
10840
10841 static int
10842 overlay_arrows_changed_p ()
10843 {
10844 Lisp_Object vlist;
10845
10846 for (vlist = Voverlay_arrow_variable_list;
10847 CONSP (vlist);
10848 vlist = XCDR (vlist))
10849 {
10850 Lisp_Object var = XCAR (vlist);
10851 Lisp_Object val, pstr;
10852
10853 if (!SYMBOLP (var))
10854 continue;
10855 val = find_symbol_value (var);
10856 if (!MARKERP (val))
10857 continue;
10858 if (! EQ (COERCE_MARKER (val),
10859 Fget (var, Qlast_arrow_position))
10860 || ! (pstr = overlay_arrow_string_or_property (var),
10861 EQ (pstr, Fget (var, Qlast_arrow_string))))
10862 return 1;
10863 }
10864 return 0;
10865 }
10866
10867 /* Mark overlay arrows to be updated on next redisplay. */
10868
10869 static void
10870 update_overlay_arrows (up_to_date)
10871 int up_to_date;
10872 {
10873 Lisp_Object vlist;
10874
10875 for (vlist = Voverlay_arrow_variable_list;
10876 CONSP (vlist);
10877 vlist = XCDR (vlist))
10878 {
10879 Lisp_Object var = XCAR (vlist);
10880
10881 if (!SYMBOLP (var))
10882 continue;
10883
10884 if (up_to_date > 0)
10885 {
10886 Lisp_Object val = find_symbol_value (var);
10887 Fput (var, Qlast_arrow_position,
10888 COERCE_MARKER (val));
10889 Fput (var, Qlast_arrow_string,
10890 overlay_arrow_string_or_property (var));
10891 }
10892 else if (up_to_date < 0
10893 || !NILP (Fget (var, Qlast_arrow_position)))
10894 {
10895 Fput (var, Qlast_arrow_position, Qt);
10896 Fput (var, Qlast_arrow_string, Qt);
10897 }
10898 }
10899 }
10900
10901
10902 /* Return overlay arrow string to display at row.
10903 Return integer (bitmap number) for arrow bitmap in left fringe.
10904 Return nil if no overlay arrow. */
10905
10906 static Lisp_Object
10907 overlay_arrow_at_row (it, row)
10908 struct it *it;
10909 struct glyph_row *row;
10910 {
10911 Lisp_Object vlist;
10912
10913 for (vlist = Voverlay_arrow_variable_list;
10914 CONSP (vlist);
10915 vlist = XCDR (vlist))
10916 {
10917 Lisp_Object var = XCAR (vlist);
10918 Lisp_Object val;
10919
10920 if (!SYMBOLP (var))
10921 continue;
10922
10923 val = find_symbol_value (var);
10924
10925 if (MARKERP (val)
10926 && current_buffer == XMARKER (val)->buffer
10927 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10928 {
10929 if (FRAME_WINDOW_P (it->f)
10930 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10931 {
10932 #ifdef HAVE_WINDOW_SYSTEM
10933 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10934 {
10935 int fringe_bitmap;
10936 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10937 return make_number (fringe_bitmap);
10938 }
10939 #endif
10940 return make_number (-1); /* Use default arrow bitmap */
10941 }
10942 return overlay_arrow_string_or_property (var);
10943 }
10944 }
10945
10946 return Qnil;
10947 }
10948
10949 /* Return 1 if point moved out of or into a composition. Otherwise
10950 return 0. PREV_BUF and PREV_PT are the last point buffer and
10951 position. BUF and PT are the current point buffer and position. */
10952
10953 int
10954 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10955 struct buffer *prev_buf, *buf;
10956 int prev_pt, pt;
10957 {
10958 EMACS_INT start, end;
10959 Lisp_Object prop;
10960 Lisp_Object buffer;
10961
10962 XSETBUFFER (buffer, buf);
10963 /* Check a composition at the last point if point moved within the
10964 same buffer. */
10965 if (prev_buf == buf)
10966 {
10967 if (prev_pt == pt)
10968 /* Point didn't move. */
10969 return 0;
10970
10971 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10972 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10973 && COMPOSITION_VALID_P (start, end, prop)
10974 && start < prev_pt && end > prev_pt)
10975 /* The last point was within the composition. Return 1 iff
10976 point moved out of the composition. */
10977 return (pt <= start || pt >= end);
10978 }
10979
10980 /* Check a composition at the current point. */
10981 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10982 && find_composition (pt, -1, &start, &end, &prop, buffer)
10983 && COMPOSITION_VALID_P (start, end, prop)
10984 && start < pt && end > pt);
10985 }
10986
10987
10988 /* Reconsider the setting of B->clip_changed which is displayed
10989 in window W. */
10990
10991 static INLINE void
10992 reconsider_clip_changes (w, b)
10993 struct window *w;
10994 struct buffer *b;
10995 {
10996 if (b->clip_changed
10997 && !NILP (w->window_end_valid)
10998 && w->current_matrix->buffer == b
10999 && w->current_matrix->zv == BUF_ZV (b)
11000 && w->current_matrix->begv == BUF_BEGV (b))
11001 b->clip_changed = 0;
11002
11003 /* If display wasn't paused, and W is not a tool bar window, see if
11004 point has been moved into or out of a composition. In that case,
11005 we set b->clip_changed to 1 to force updating the screen. If
11006 b->clip_changed has already been set to 1, we can skip this
11007 check. */
11008 if (!b->clip_changed
11009 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11010 {
11011 int pt;
11012
11013 if (w == XWINDOW (selected_window))
11014 pt = BUF_PT (current_buffer);
11015 else
11016 pt = marker_position (w->pointm);
11017
11018 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11019 || pt != XINT (w->last_point))
11020 && check_point_in_composition (w->current_matrix->buffer,
11021 XINT (w->last_point),
11022 XBUFFER (w->buffer), pt))
11023 b->clip_changed = 1;
11024 }
11025 }
11026 \f
11027
11028 /* Select FRAME to forward the values of frame-local variables into C
11029 variables so that the redisplay routines can access those values
11030 directly. */
11031
11032 static void
11033 select_frame_for_redisplay (frame)
11034 Lisp_Object frame;
11035 {
11036 Lisp_Object tail, symbol, val;
11037 Lisp_Object old = selected_frame;
11038 struct Lisp_Symbol *sym;
11039
11040 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11041
11042 selected_frame = frame;
11043
11044 do
11045 {
11046 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11047 if (CONSP (XCAR (tail))
11048 && (symbol = XCAR (XCAR (tail)),
11049 SYMBOLP (symbol))
11050 && (sym = indirect_variable (XSYMBOL (symbol)),
11051 val = sym->value,
11052 (BUFFER_LOCAL_VALUEP (val)))
11053 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11054 /* Use find_symbol_value rather than Fsymbol_value
11055 to avoid an error if it is void. */
11056 find_symbol_value (symbol);
11057 } while (!EQ (frame, old) && (frame = old, 1));
11058 }
11059
11060
11061 #define STOP_POLLING \
11062 do { if (! polling_stopped_here) stop_polling (); \
11063 polling_stopped_here = 1; } while (0)
11064
11065 #define RESUME_POLLING \
11066 do { if (polling_stopped_here) start_polling (); \
11067 polling_stopped_here = 0; } while (0)
11068
11069
11070 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11071 response to any user action; therefore, we should preserve the echo
11072 area. (Actually, our caller does that job.) Perhaps in the future
11073 avoid recentering windows if it is not necessary; currently that
11074 causes some problems. */
11075
11076 static void
11077 redisplay_internal (preserve_echo_area)
11078 int preserve_echo_area;
11079 {
11080 struct window *w = XWINDOW (selected_window);
11081 struct frame *f;
11082 int pause;
11083 int must_finish = 0;
11084 struct text_pos tlbufpos, tlendpos;
11085 int number_of_visible_frames;
11086 int count, count1;
11087 struct frame *sf;
11088 int polling_stopped_here = 0;
11089 Lisp_Object old_frame = selected_frame;
11090
11091 /* Non-zero means redisplay has to consider all windows on all
11092 frames. Zero means, only selected_window is considered. */
11093 int consider_all_windows_p;
11094
11095 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11096
11097 /* No redisplay if running in batch mode or frame is not yet fully
11098 initialized, or redisplay is explicitly turned off by setting
11099 Vinhibit_redisplay. */
11100 if (noninteractive
11101 || !NILP (Vinhibit_redisplay))
11102 return;
11103
11104 /* Don't examine these until after testing Vinhibit_redisplay.
11105 When Emacs is shutting down, perhaps because its connection to
11106 X has dropped, we should not look at them at all. */
11107 f = XFRAME (w->frame);
11108 sf = SELECTED_FRAME ();
11109
11110 if (!f->glyphs_initialized_p)
11111 return;
11112
11113 /* The flag redisplay_performed_directly_p is set by
11114 direct_output_for_insert when it already did the whole screen
11115 update necessary. */
11116 if (redisplay_performed_directly_p)
11117 {
11118 redisplay_performed_directly_p = 0;
11119 if (!hscroll_windows (selected_window))
11120 return;
11121 }
11122
11123 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11124 if (popup_activated ())
11125 return;
11126 #endif
11127
11128 /* I don't think this happens but let's be paranoid. */
11129 if (redisplaying_p)
11130 return;
11131
11132 /* Record a function that resets redisplaying_p to its old value
11133 when we leave this function. */
11134 count = SPECPDL_INDEX ();
11135 record_unwind_protect (unwind_redisplay,
11136 Fcons (make_number (redisplaying_p), selected_frame));
11137 ++redisplaying_p;
11138 specbind (Qinhibit_free_realized_faces, Qnil);
11139
11140 {
11141 Lisp_Object tail, frame;
11142
11143 FOR_EACH_FRAME (tail, frame)
11144 {
11145 struct frame *f = XFRAME (frame);
11146 f->already_hscrolled_p = 0;
11147 }
11148 }
11149
11150 retry:
11151 if (!EQ (old_frame, selected_frame)
11152 && FRAME_LIVE_P (XFRAME (old_frame)))
11153 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11154 selected_frame and selected_window to be temporarily out-of-sync so
11155 when we come back here via `goto retry', we need to resync because we
11156 may need to run Elisp code (via prepare_menu_bars). */
11157 select_frame_for_redisplay (old_frame);
11158
11159 pause = 0;
11160 reconsider_clip_changes (w, current_buffer);
11161 last_escape_glyph_frame = NULL;
11162 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11163
11164 /* If new fonts have been loaded that make a glyph matrix adjustment
11165 necessary, do it. */
11166 if (fonts_changed_p)
11167 {
11168 adjust_glyphs (NULL);
11169 ++windows_or_buffers_changed;
11170 fonts_changed_p = 0;
11171 }
11172
11173 /* If face_change_count is non-zero, init_iterator will free all
11174 realized faces, which includes the faces referenced from current
11175 matrices. So, we can't reuse current matrices in this case. */
11176 if (face_change_count)
11177 ++windows_or_buffers_changed;
11178
11179 if (FRAME_TERMCAP_P (sf)
11180 && FRAME_TTY (sf)->previous_frame != sf)
11181 {
11182 /* Since frames on a single ASCII terminal share the same
11183 display area, displaying a different frame means redisplay
11184 the whole thing. */
11185 windows_or_buffers_changed++;
11186 SET_FRAME_GARBAGED (sf);
11187 #ifndef WINDOWSNT
11188 set_tty_color_mode (FRAME_TTY (sf), sf);
11189 #endif
11190 FRAME_TTY (sf)->previous_frame = sf;
11191 }
11192
11193 /* Set the visible flags for all frames. Do this before checking
11194 for resized or garbaged frames; they want to know if their frames
11195 are visible. See the comment in frame.h for
11196 FRAME_SAMPLE_VISIBILITY. */
11197 {
11198 Lisp_Object tail, frame;
11199
11200 number_of_visible_frames = 0;
11201
11202 FOR_EACH_FRAME (tail, frame)
11203 {
11204 struct frame *f = XFRAME (frame);
11205
11206 FRAME_SAMPLE_VISIBILITY (f);
11207 if (FRAME_VISIBLE_P (f))
11208 ++number_of_visible_frames;
11209 clear_desired_matrices (f);
11210 }
11211 }
11212
11213
11214 /* Notice any pending interrupt request to change frame size. */
11215 do_pending_window_change (1);
11216
11217 /* Clear frames marked as garbaged. */
11218 if (frame_garbaged)
11219 clear_garbaged_frames ();
11220
11221 /* Build menubar and tool-bar items. */
11222 if (NILP (Vmemory_full))
11223 prepare_menu_bars ();
11224
11225 if (windows_or_buffers_changed)
11226 update_mode_lines++;
11227
11228 /* Detect case that we need to write or remove a star in the mode line. */
11229 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11230 {
11231 w->update_mode_line = Qt;
11232 if (buffer_shared > 1)
11233 update_mode_lines++;
11234 }
11235
11236 /* Avoid invocation of point motion hooks by `current_column' below. */
11237 count1 = SPECPDL_INDEX ();
11238 specbind (Qinhibit_point_motion_hooks, Qt);
11239
11240 /* If %c is in the mode line, update it if needed. */
11241 if (!NILP (w->column_number_displayed)
11242 /* This alternative quickly identifies a common case
11243 where no change is needed. */
11244 && !(PT == XFASTINT (w->last_point)
11245 && XFASTINT (w->last_modified) >= MODIFF
11246 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11247 && (XFASTINT (w->column_number_displayed)
11248 != (int) current_column ())) /* iftc */
11249 w->update_mode_line = Qt;
11250
11251 unbind_to (count1, Qnil);
11252
11253 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11254
11255 /* The variable buffer_shared is set in redisplay_window and
11256 indicates that we redisplay a buffer in different windows. See
11257 there. */
11258 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11259 || cursor_type_changed);
11260
11261 /* If specs for an arrow have changed, do thorough redisplay
11262 to ensure we remove any arrow that should no longer exist. */
11263 if (overlay_arrows_changed_p ())
11264 consider_all_windows_p = windows_or_buffers_changed = 1;
11265
11266 /* Normally the message* functions will have already displayed and
11267 updated the echo area, but the frame may have been trashed, or
11268 the update may have been preempted, so display the echo area
11269 again here. Checking message_cleared_p captures the case that
11270 the echo area should be cleared. */
11271 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11272 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11273 || (message_cleared_p
11274 && minibuf_level == 0
11275 /* If the mini-window is currently selected, this means the
11276 echo-area doesn't show through. */
11277 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11278 {
11279 int window_height_changed_p = echo_area_display (0);
11280 must_finish = 1;
11281
11282 /* If we don't display the current message, don't clear the
11283 message_cleared_p flag, because, if we did, we wouldn't clear
11284 the echo area in the next redisplay which doesn't preserve
11285 the echo area. */
11286 if (!display_last_displayed_message_p)
11287 message_cleared_p = 0;
11288
11289 if (fonts_changed_p)
11290 goto retry;
11291 else if (window_height_changed_p)
11292 {
11293 consider_all_windows_p = 1;
11294 ++update_mode_lines;
11295 ++windows_or_buffers_changed;
11296
11297 /* If window configuration was changed, frames may have been
11298 marked garbaged. Clear them or we will experience
11299 surprises wrt scrolling. */
11300 if (frame_garbaged)
11301 clear_garbaged_frames ();
11302 }
11303 }
11304 else if (EQ (selected_window, minibuf_window)
11305 && (current_buffer->clip_changed
11306 || XFASTINT (w->last_modified) < MODIFF
11307 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11308 && resize_mini_window (w, 0))
11309 {
11310 /* Resized active mini-window to fit the size of what it is
11311 showing if its contents might have changed. */
11312 must_finish = 1;
11313 consider_all_windows_p = 1;
11314 ++windows_or_buffers_changed;
11315 ++update_mode_lines;
11316
11317 /* If window configuration was changed, frames may have been
11318 marked garbaged. Clear them or we will experience
11319 surprises wrt scrolling. */
11320 if (frame_garbaged)
11321 clear_garbaged_frames ();
11322 }
11323
11324
11325 /* If showing the region, and mark has changed, we must redisplay
11326 the whole window. The assignment to this_line_start_pos prevents
11327 the optimization directly below this if-statement. */
11328 if (((!NILP (Vtransient_mark_mode)
11329 && !NILP (XBUFFER (w->buffer)->mark_active))
11330 != !NILP (w->region_showing))
11331 || (!NILP (w->region_showing)
11332 && !EQ (w->region_showing,
11333 Fmarker_position (XBUFFER (w->buffer)->mark))))
11334 CHARPOS (this_line_start_pos) = 0;
11335
11336 /* Optimize the case that only the line containing the cursor in the
11337 selected window has changed. Variables starting with this_ are
11338 set in display_line and record information about the line
11339 containing the cursor. */
11340 tlbufpos = this_line_start_pos;
11341 tlendpos = this_line_end_pos;
11342 if (!consider_all_windows_p
11343 && CHARPOS (tlbufpos) > 0
11344 && NILP (w->update_mode_line)
11345 && !current_buffer->clip_changed
11346 && !current_buffer->prevent_redisplay_optimizations_p
11347 && FRAME_VISIBLE_P (XFRAME (w->frame))
11348 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11349 /* Make sure recorded data applies to current buffer, etc. */
11350 && this_line_buffer == current_buffer
11351 && current_buffer == XBUFFER (w->buffer)
11352 && NILP (w->force_start)
11353 && NILP (w->optional_new_start)
11354 /* Point must be on the line that we have info recorded about. */
11355 && PT >= CHARPOS (tlbufpos)
11356 && PT <= Z - CHARPOS (tlendpos)
11357 /* All text outside that line, including its final newline,
11358 must be unchanged */
11359 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11360 CHARPOS (tlendpos)))
11361 {
11362 if (CHARPOS (tlbufpos) > BEGV
11363 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11364 && (CHARPOS (tlbufpos) == ZV
11365 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11366 /* Former continuation line has disappeared by becoming empty */
11367 goto cancel;
11368 else if (XFASTINT (w->last_modified) < MODIFF
11369 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11370 || MINI_WINDOW_P (w))
11371 {
11372 /* We have to handle the case of continuation around a
11373 wide-column character (See the comment in indent.c around
11374 line 885).
11375
11376 For instance, in the following case:
11377
11378 -------- Insert --------
11379 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11380 J_I_ ==> J_I_ `^^' are cursors.
11381 ^^ ^^
11382 -------- --------
11383
11384 As we have to redraw the line above, we should goto cancel. */
11385
11386 struct it it;
11387 int line_height_before = this_line_pixel_height;
11388
11389 /* Note that start_display will handle the case that the
11390 line starting at tlbufpos is a continuation lines. */
11391 start_display (&it, w, tlbufpos);
11392
11393 /* Implementation note: It this still necessary? */
11394 if (it.current_x != this_line_start_x)
11395 goto cancel;
11396
11397 TRACE ((stderr, "trying display optimization 1\n"));
11398 w->cursor.vpos = -1;
11399 overlay_arrow_seen = 0;
11400 it.vpos = this_line_vpos;
11401 it.current_y = this_line_y;
11402 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11403 display_line (&it);
11404
11405 /* If line contains point, is not continued,
11406 and ends at same distance from eob as before, we win */
11407 if (w->cursor.vpos >= 0
11408 /* Line is not continued, otherwise this_line_start_pos
11409 would have been set to 0 in display_line. */
11410 && CHARPOS (this_line_start_pos)
11411 /* Line ends as before. */
11412 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11413 /* Line has same height as before. Otherwise other lines
11414 would have to be shifted up or down. */
11415 && this_line_pixel_height == line_height_before)
11416 {
11417 /* If this is not the window's last line, we must adjust
11418 the charstarts of the lines below. */
11419 if (it.current_y < it.last_visible_y)
11420 {
11421 struct glyph_row *row
11422 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11423 int delta, delta_bytes;
11424
11425 if (Z - CHARPOS (tlendpos) == ZV)
11426 {
11427 /* This line ends at end of (accessible part of)
11428 buffer. There is no newline to count. */
11429 delta = (Z
11430 - CHARPOS (tlendpos)
11431 - MATRIX_ROW_START_CHARPOS (row));
11432 delta_bytes = (Z_BYTE
11433 - BYTEPOS (tlendpos)
11434 - MATRIX_ROW_START_BYTEPOS (row));
11435 }
11436 else
11437 {
11438 /* This line ends in a newline. Must take
11439 account of the newline and the rest of the
11440 text that follows. */
11441 delta = (Z
11442 - CHARPOS (tlendpos)
11443 - MATRIX_ROW_START_CHARPOS (row));
11444 delta_bytes = (Z_BYTE
11445 - BYTEPOS (tlendpos)
11446 - MATRIX_ROW_START_BYTEPOS (row));
11447 }
11448
11449 increment_matrix_positions (w->current_matrix,
11450 this_line_vpos + 1,
11451 w->current_matrix->nrows,
11452 delta, delta_bytes);
11453 }
11454
11455 /* If this row displays text now but previously didn't,
11456 or vice versa, w->window_end_vpos may have to be
11457 adjusted. */
11458 if ((it.glyph_row - 1)->displays_text_p)
11459 {
11460 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11461 XSETINT (w->window_end_vpos, this_line_vpos);
11462 }
11463 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11464 && this_line_vpos > 0)
11465 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11466 w->window_end_valid = Qnil;
11467
11468 /* Update hint: No need to try to scroll in update_window. */
11469 w->desired_matrix->no_scrolling_p = 1;
11470
11471 #if GLYPH_DEBUG
11472 *w->desired_matrix->method = 0;
11473 debug_method_add (w, "optimization 1");
11474 #endif
11475 #ifdef HAVE_WINDOW_SYSTEM
11476 update_window_fringes (w, 0);
11477 #endif
11478 goto update;
11479 }
11480 else
11481 goto cancel;
11482 }
11483 else if (/* Cursor position hasn't changed. */
11484 PT == XFASTINT (w->last_point)
11485 /* Make sure the cursor was last displayed
11486 in this window. Otherwise we have to reposition it. */
11487 && 0 <= w->cursor.vpos
11488 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11489 {
11490 if (!must_finish)
11491 {
11492 do_pending_window_change (1);
11493
11494 /* We used to always goto end_of_redisplay here, but this
11495 isn't enough if we have a blinking cursor. */
11496 if (w->cursor_off_p == w->last_cursor_off_p)
11497 goto end_of_redisplay;
11498 }
11499 goto update;
11500 }
11501 /* If highlighting the region, or if the cursor is in the echo area,
11502 then we can't just move the cursor. */
11503 else if (! (!NILP (Vtransient_mark_mode)
11504 && !NILP (current_buffer->mark_active))
11505 && (EQ (selected_window, current_buffer->last_selected_window)
11506 || highlight_nonselected_windows)
11507 && NILP (w->region_showing)
11508 && NILP (Vshow_trailing_whitespace)
11509 && !cursor_in_echo_area)
11510 {
11511 struct it it;
11512 struct glyph_row *row;
11513
11514 /* Skip from tlbufpos to PT and see where it is. Note that
11515 PT may be in invisible text. If so, we will end at the
11516 next visible position. */
11517 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11518 NULL, DEFAULT_FACE_ID);
11519 it.current_x = this_line_start_x;
11520 it.current_y = this_line_y;
11521 it.vpos = this_line_vpos;
11522
11523 /* The call to move_it_to stops in front of PT, but
11524 moves over before-strings. */
11525 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11526
11527 if (it.vpos == this_line_vpos
11528 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11529 row->enabled_p))
11530 {
11531 xassert (this_line_vpos == it.vpos);
11532 xassert (this_line_y == it.current_y);
11533 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11534 #if GLYPH_DEBUG
11535 *w->desired_matrix->method = 0;
11536 debug_method_add (w, "optimization 3");
11537 #endif
11538 goto update;
11539 }
11540 else
11541 goto cancel;
11542 }
11543
11544 cancel:
11545 /* Text changed drastically or point moved off of line. */
11546 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11547 }
11548
11549 CHARPOS (this_line_start_pos) = 0;
11550 consider_all_windows_p |= buffer_shared > 1;
11551 ++clear_face_cache_count;
11552 #ifdef HAVE_WINDOW_SYSTEM
11553 ++clear_image_cache_count;
11554 #endif
11555
11556 /* Build desired matrices, and update the display. If
11557 consider_all_windows_p is non-zero, do it for all windows on all
11558 frames. Otherwise do it for selected_window, only. */
11559
11560 if (consider_all_windows_p)
11561 {
11562 Lisp_Object tail, frame;
11563
11564 FOR_EACH_FRAME (tail, frame)
11565 XFRAME (frame)->updated_p = 0;
11566
11567 /* Recompute # windows showing selected buffer. This will be
11568 incremented each time such a window is displayed. */
11569 buffer_shared = 0;
11570
11571 FOR_EACH_FRAME (tail, frame)
11572 {
11573 struct frame *f = XFRAME (frame);
11574
11575 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11576 {
11577 if (! EQ (frame, selected_frame))
11578 /* Select the frame, for the sake of frame-local
11579 variables. */
11580 select_frame_for_redisplay (frame);
11581
11582 /* Mark all the scroll bars to be removed; we'll redeem
11583 the ones we want when we redisplay their windows. */
11584 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11585 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11586
11587 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11588 redisplay_windows (FRAME_ROOT_WINDOW (f));
11589
11590 /* Any scroll bars which redisplay_windows should have
11591 nuked should now go away. */
11592 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11593 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11594
11595 /* If fonts changed, display again. */
11596 /* ??? rms: I suspect it is a mistake to jump all the way
11597 back to retry here. It should just retry this frame. */
11598 if (fonts_changed_p)
11599 goto retry;
11600
11601 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11602 {
11603 /* See if we have to hscroll. */
11604 if (!f->already_hscrolled_p)
11605 {
11606 f->already_hscrolled_p = 1;
11607 if (hscroll_windows (f->root_window))
11608 goto retry;
11609 }
11610
11611 /* Prevent various kinds of signals during display
11612 update. stdio is not robust about handling
11613 signals, which can cause an apparent I/O
11614 error. */
11615 if (interrupt_input)
11616 unrequest_sigio ();
11617 STOP_POLLING;
11618
11619 /* Update the display. */
11620 set_window_update_flags (XWINDOW (f->root_window), 1);
11621 pause |= update_frame (f, 0, 0);
11622 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11623 if (pause)
11624 break;
11625 #endif
11626
11627 f->updated_p = 1;
11628 }
11629 }
11630 }
11631
11632 if (!EQ (old_frame, selected_frame)
11633 && FRAME_LIVE_P (XFRAME (old_frame)))
11634 /* We played a bit fast-and-loose above and allowed selected_frame
11635 and selected_window to be temporarily out-of-sync but let's make
11636 sure this stays contained. */
11637 select_frame_for_redisplay (old_frame);
11638 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11639
11640 if (!pause)
11641 {
11642 /* Do the mark_window_display_accurate after all windows have
11643 been redisplayed because this call resets flags in buffers
11644 which are needed for proper redisplay. */
11645 FOR_EACH_FRAME (tail, frame)
11646 {
11647 struct frame *f = XFRAME (frame);
11648 if (f->updated_p)
11649 {
11650 mark_window_display_accurate (f->root_window, 1);
11651 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11652 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11653 }
11654 }
11655 }
11656 }
11657 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11658 {
11659 Lisp_Object mini_window;
11660 struct frame *mini_frame;
11661
11662 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11663 /* Use list_of_error, not Qerror, so that
11664 we catch only errors and don't run the debugger. */
11665 internal_condition_case_1 (redisplay_window_1, selected_window,
11666 list_of_error,
11667 redisplay_window_error);
11668
11669 /* Compare desired and current matrices, perform output. */
11670
11671 update:
11672 /* If fonts changed, display again. */
11673 if (fonts_changed_p)
11674 goto retry;
11675
11676 /* Prevent various kinds of signals during display update.
11677 stdio is not robust about handling signals,
11678 which can cause an apparent I/O error. */
11679 if (interrupt_input)
11680 unrequest_sigio ();
11681 STOP_POLLING;
11682
11683 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11684 {
11685 if (hscroll_windows (selected_window))
11686 goto retry;
11687
11688 XWINDOW (selected_window)->must_be_updated_p = 1;
11689 pause = update_frame (sf, 0, 0);
11690 }
11691
11692 /* We may have called echo_area_display at the top of this
11693 function. If the echo area is on another frame, that may
11694 have put text on a frame other than the selected one, so the
11695 above call to update_frame would not have caught it. Catch
11696 it here. */
11697 mini_window = FRAME_MINIBUF_WINDOW (sf);
11698 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11699
11700 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11701 {
11702 XWINDOW (mini_window)->must_be_updated_p = 1;
11703 pause |= update_frame (mini_frame, 0, 0);
11704 if (!pause && hscroll_windows (mini_window))
11705 goto retry;
11706 }
11707 }
11708
11709 /* If display was paused because of pending input, make sure we do a
11710 thorough update the next time. */
11711 if (pause)
11712 {
11713 /* Prevent the optimization at the beginning of
11714 redisplay_internal that tries a single-line update of the
11715 line containing the cursor in the selected window. */
11716 CHARPOS (this_line_start_pos) = 0;
11717
11718 /* Let the overlay arrow be updated the next time. */
11719 update_overlay_arrows (0);
11720
11721 /* If we pause after scrolling, some rows in the current
11722 matrices of some windows are not valid. */
11723 if (!WINDOW_FULL_WIDTH_P (w)
11724 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11725 update_mode_lines = 1;
11726 }
11727 else
11728 {
11729 if (!consider_all_windows_p)
11730 {
11731 /* This has already been done above if
11732 consider_all_windows_p is set. */
11733 mark_window_display_accurate_1 (w, 1);
11734
11735 /* Say overlay arrows are up to date. */
11736 update_overlay_arrows (1);
11737
11738 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11739 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11740 }
11741
11742 update_mode_lines = 0;
11743 windows_or_buffers_changed = 0;
11744 cursor_type_changed = 0;
11745 }
11746
11747 /* Start SIGIO interrupts coming again. Having them off during the
11748 code above makes it less likely one will discard output, but not
11749 impossible, since there might be stuff in the system buffer here.
11750 But it is much hairier to try to do anything about that. */
11751 if (interrupt_input)
11752 request_sigio ();
11753 RESUME_POLLING;
11754
11755 /* If a frame has become visible which was not before, redisplay
11756 again, so that we display it. Expose events for such a frame
11757 (which it gets when becoming visible) don't call the parts of
11758 redisplay constructing glyphs, so simply exposing a frame won't
11759 display anything in this case. So, we have to display these
11760 frames here explicitly. */
11761 if (!pause)
11762 {
11763 Lisp_Object tail, frame;
11764 int new_count = 0;
11765
11766 FOR_EACH_FRAME (tail, frame)
11767 {
11768 int this_is_visible = 0;
11769
11770 if (XFRAME (frame)->visible)
11771 this_is_visible = 1;
11772 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11773 if (XFRAME (frame)->visible)
11774 this_is_visible = 1;
11775
11776 if (this_is_visible)
11777 new_count++;
11778 }
11779
11780 if (new_count != number_of_visible_frames)
11781 windows_or_buffers_changed++;
11782 }
11783
11784 /* Change frame size now if a change is pending. */
11785 do_pending_window_change (1);
11786
11787 /* If we just did a pending size change, or have additional
11788 visible frames, redisplay again. */
11789 if (windows_or_buffers_changed && !pause)
11790 goto retry;
11791
11792 /* Clear the face cache eventually. */
11793 if (consider_all_windows_p)
11794 {
11795 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11796 {
11797 clear_face_cache (0);
11798 clear_face_cache_count = 0;
11799 }
11800 #ifdef HAVE_WINDOW_SYSTEM
11801 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11802 {
11803 clear_image_caches (Qnil);
11804 clear_image_cache_count = 0;
11805 }
11806 #endif /* HAVE_WINDOW_SYSTEM */
11807 }
11808
11809 end_of_redisplay:
11810 unbind_to (count, Qnil);
11811 RESUME_POLLING;
11812 }
11813
11814
11815 /* Redisplay, but leave alone any recent echo area message unless
11816 another message has been requested in its place.
11817
11818 This is useful in situations where you need to redisplay but no
11819 user action has occurred, making it inappropriate for the message
11820 area to be cleared. See tracking_off and
11821 wait_reading_process_output for examples of these situations.
11822
11823 FROM_WHERE is an integer saying from where this function was
11824 called. This is useful for debugging. */
11825
11826 void
11827 redisplay_preserve_echo_area (from_where)
11828 int from_where;
11829 {
11830 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11831
11832 if (!NILP (echo_area_buffer[1]))
11833 {
11834 /* We have a previously displayed message, but no current
11835 message. Redisplay the previous message. */
11836 display_last_displayed_message_p = 1;
11837 redisplay_internal (1);
11838 display_last_displayed_message_p = 0;
11839 }
11840 else
11841 redisplay_internal (1);
11842
11843 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11844 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11845 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11846 }
11847
11848
11849 /* Function registered with record_unwind_protect in
11850 redisplay_internal. Reset redisplaying_p to the value it had
11851 before redisplay_internal was called, and clear
11852 prevent_freeing_realized_faces_p. It also selects the previously
11853 selected frame, unless it has been deleted (by an X connection
11854 failure during redisplay, for example). */
11855
11856 static Lisp_Object
11857 unwind_redisplay (val)
11858 Lisp_Object val;
11859 {
11860 Lisp_Object old_redisplaying_p, old_frame;
11861
11862 old_redisplaying_p = XCAR (val);
11863 redisplaying_p = XFASTINT (old_redisplaying_p);
11864 old_frame = XCDR (val);
11865 if (! EQ (old_frame, selected_frame)
11866 && FRAME_LIVE_P (XFRAME (old_frame)))
11867 select_frame_for_redisplay (old_frame);
11868 return Qnil;
11869 }
11870
11871
11872 /* Mark the display of window W as accurate or inaccurate. If
11873 ACCURATE_P is non-zero mark display of W as accurate. If
11874 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11875 redisplay_internal is called. */
11876
11877 static void
11878 mark_window_display_accurate_1 (w, accurate_p)
11879 struct window *w;
11880 int accurate_p;
11881 {
11882 if (BUFFERP (w->buffer))
11883 {
11884 struct buffer *b = XBUFFER (w->buffer);
11885
11886 w->last_modified
11887 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11888 w->last_overlay_modified
11889 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11890 w->last_had_star
11891 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11892
11893 if (accurate_p)
11894 {
11895 b->clip_changed = 0;
11896 b->prevent_redisplay_optimizations_p = 0;
11897
11898 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11899 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11900 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11901 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11902
11903 w->current_matrix->buffer = b;
11904 w->current_matrix->begv = BUF_BEGV (b);
11905 w->current_matrix->zv = BUF_ZV (b);
11906
11907 w->last_cursor = w->cursor;
11908 w->last_cursor_off_p = w->cursor_off_p;
11909
11910 if (w == XWINDOW (selected_window))
11911 w->last_point = make_number (BUF_PT (b));
11912 else
11913 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11914 }
11915 }
11916
11917 if (accurate_p)
11918 {
11919 w->window_end_valid = w->buffer;
11920 #if 0 /* This is incorrect with variable-height lines. */
11921 xassert (XINT (w->window_end_vpos)
11922 < (WINDOW_TOTAL_LINES (w)
11923 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11924 #endif
11925 w->update_mode_line = Qnil;
11926 }
11927 }
11928
11929
11930 /* Mark the display of windows in the window tree rooted at WINDOW as
11931 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11932 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11933 be redisplayed the next time redisplay_internal is called. */
11934
11935 void
11936 mark_window_display_accurate (window, accurate_p)
11937 Lisp_Object window;
11938 int accurate_p;
11939 {
11940 struct window *w;
11941
11942 for (; !NILP (window); window = w->next)
11943 {
11944 w = XWINDOW (window);
11945 mark_window_display_accurate_1 (w, accurate_p);
11946
11947 if (!NILP (w->vchild))
11948 mark_window_display_accurate (w->vchild, accurate_p);
11949 if (!NILP (w->hchild))
11950 mark_window_display_accurate (w->hchild, accurate_p);
11951 }
11952
11953 if (accurate_p)
11954 {
11955 update_overlay_arrows (1);
11956 }
11957 else
11958 {
11959 /* Force a thorough redisplay the next time by setting
11960 last_arrow_position and last_arrow_string to t, which is
11961 unequal to any useful value of Voverlay_arrow_... */
11962 update_overlay_arrows (-1);
11963 }
11964 }
11965
11966
11967 /* Return value in display table DP (Lisp_Char_Table *) for character
11968 C. Since a display table doesn't have any parent, we don't have to
11969 follow parent. Do not call this function directly but use the
11970 macro DISP_CHAR_VECTOR. */
11971
11972 Lisp_Object
11973 disp_char_vector (dp, c)
11974 struct Lisp_Char_Table *dp;
11975 int c;
11976 {
11977 Lisp_Object val;
11978
11979 if (ASCII_CHAR_P (c))
11980 {
11981 val = dp->ascii;
11982 if (SUB_CHAR_TABLE_P (val))
11983 val = XSUB_CHAR_TABLE (val)->contents[c];
11984 }
11985 else
11986 {
11987 Lisp_Object table;
11988
11989 XSETCHAR_TABLE (table, dp);
11990 val = char_table_ref (table, c);
11991 }
11992 if (NILP (val))
11993 val = dp->defalt;
11994 return val;
11995 }
11996
11997
11998 \f
11999 /***********************************************************************
12000 Window Redisplay
12001 ***********************************************************************/
12002
12003 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12004
12005 static void
12006 redisplay_windows (window)
12007 Lisp_Object window;
12008 {
12009 while (!NILP (window))
12010 {
12011 struct window *w = XWINDOW (window);
12012
12013 if (!NILP (w->hchild))
12014 redisplay_windows (w->hchild);
12015 else if (!NILP (w->vchild))
12016 redisplay_windows (w->vchild);
12017 else
12018 {
12019 displayed_buffer = XBUFFER (w->buffer);
12020 /* Use list_of_error, not Qerror, so that
12021 we catch only errors and don't run the debugger. */
12022 internal_condition_case_1 (redisplay_window_0, window,
12023 list_of_error,
12024 redisplay_window_error);
12025 }
12026
12027 window = w->next;
12028 }
12029 }
12030
12031 static Lisp_Object
12032 redisplay_window_error ()
12033 {
12034 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12035 return Qnil;
12036 }
12037
12038 static Lisp_Object
12039 redisplay_window_0 (window)
12040 Lisp_Object window;
12041 {
12042 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12043 redisplay_window (window, 0);
12044 return Qnil;
12045 }
12046
12047 static Lisp_Object
12048 redisplay_window_1 (window)
12049 Lisp_Object window;
12050 {
12051 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12052 redisplay_window (window, 1);
12053 return Qnil;
12054 }
12055 \f
12056
12057 /* Increment GLYPH until it reaches END or CONDITION fails while
12058 adding (GLYPH)->pixel_width to X. */
12059
12060 #define SKIP_GLYPHS(glyph, end, x, condition) \
12061 do \
12062 { \
12063 (x) += (glyph)->pixel_width; \
12064 ++(glyph); \
12065 } \
12066 while ((glyph) < (end) && (condition))
12067
12068
12069 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12070 DELTA is the number of bytes by which positions recorded in ROW
12071 differ from current buffer positions.
12072
12073 Return 0 if cursor is not on this row. 1 otherwise. */
12074
12075 int
12076 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12077 struct window *w;
12078 struct glyph_row *row;
12079 struct glyph_matrix *matrix;
12080 int delta, delta_bytes, dy, dvpos;
12081 {
12082 struct glyph *glyph = row->glyphs[TEXT_AREA];
12083 struct glyph *end = glyph + row->used[TEXT_AREA];
12084 struct glyph *cursor = NULL;
12085 /* The first glyph that starts a sequence of glyphs from string. */
12086 struct glyph *string_start;
12087 /* The X coordinate of string_start. */
12088 int string_start_x;
12089 /* The last known character position. */
12090 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12091 /* The last known character position before string_start. */
12092 int string_before_pos;
12093 int x = row->x;
12094 int cursor_x = x;
12095 int cursor_from_overlay_pos = 0;
12096 int pt_old = PT - delta;
12097
12098 /* Skip over glyphs not having an object at the start of the row.
12099 These are special glyphs like truncation marks on terminal
12100 frames. */
12101 if (row->displays_text_p)
12102 while (glyph < end
12103 && INTEGERP (glyph->object)
12104 && glyph->charpos < 0)
12105 {
12106 x += glyph->pixel_width;
12107 ++glyph;
12108 }
12109
12110 string_start = NULL;
12111 while (glyph < end
12112 && !INTEGERP (glyph->object)
12113 && (!BUFFERP (glyph->object)
12114 || (last_pos = glyph->charpos) < pt_old))
12115 {
12116 if (! STRINGP (glyph->object))
12117 {
12118 string_start = NULL;
12119 x += glyph->pixel_width;
12120 ++glyph;
12121 if (cursor_from_overlay_pos
12122 && last_pos >= cursor_from_overlay_pos)
12123 {
12124 cursor_from_overlay_pos = 0;
12125 cursor = 0;
12126 }
12127 }
12128 else
12129 {
12130 if (string_start == NULL)
12131 {
12132 string_before_pos = last_pos;
12133 string_start = glyph;
12134 string_start_x = x;
12135 }
12136 /* Skip all glyphs from string. */
12137 do
12138 {
12139 Lisp_Object cprop;
12140 int pos;
12141 if ((cursor == NULL || glyph > cursor)
12142 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12143 Qcursor, (glyph)->object),
12144 !NILP (cprop))
12145 && (pos = string_buffer_position (w, glyph->object,
12146 string_before_pos),
12147 (pos == 0 /* From overlay */
12148 || pos == pt_old)))
12149 {
12150 /* Estimate overlay buffer position from the buffer
12151 positions of the glyphs before and after the overlay.
12152 Add 1 to last_pos so that if point corresponds to the
12153 glyph right after the overlay, we still use a 'cursor'
12154 property found in that overlay. */
12155 cursor_from_overlay_pos = (pos ? 0 : last_pos
12156 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12157 cursor = glyph;
12158 cursor_x = x;
12159 }
12160 x += glyph->pixel_width;
12161 ++glyph;
12162 }
12163 while (glyph < end && EQ (glyph->object, string_start->object));
12164 }
12165 }
12166
12167 if (cursor != NULL)
12168 {
12169 glyph = cursor;
12170 x = cursor_x;
12171 }
12172 else if (row->ends_in_ellipsis_p && glyph == end)
12173 {
12174 /* Scan back over the ellipsis glyphs, decrementing positions. */
12175 while (glyph > row->glyphs[TEXT_AREA]
12176 && (glyph - 1)->charpos == last_pos)
12177 glyph--, x -= glyph->pixel_width;
12178 /* That loop always goes one position too far,
12179 including the glyph before the ellipsis.
12180 So scan forward over that one. */
12181 x += glyph->pixel_width;
12182 glyph++;
12183 }
12184 else if (string_start
12185 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12186 {
12187 /* We may have skipped over point because the previous glyphs
12188 are from string. As there's no easy way to know the
12189 character position of the current glyph, find the correct
12190 glyph on point by scanning from string_start again. */
12191 Lisp_Object limit;
12192 Lisp_Object string;
12193 struct glyph *stop = glyph;
12194 int pos;
12195
12196 limit = make_number (pt_old + 1);
12197 glyph = string_start;
12198 x = string_start_x;
12199 string = glyph->object;
12200 pos = string_buffer_position (w, string, string_before_pos);
12201 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12202 because we always put cursor after overlay strings. */
12203 while (pos == 0 && glyph < stop)
12204 {
12205 string = glyph->object;
12206 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12207 if (glyph < stop)
12208 pos = string_buffer_position (w, glyph->object, string_before_pos);
12209 }
12210
12211 while (glyph < stop)
12212 {
12213 pos = XINT (Fnext_single_char_property_change
12214 (make_number (pos), Qdisplay, Qnil, limit));
12215 if (pos > pt_old)
12216 break;
12217 /* Skip glyphs from the same string. */
12218 string = glyph->object;
12219 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12220 /* Skip glyphs from an overlay. */
12221 while (glyph < stop
12222 && ! string_buffer_position (w, glyph->object, pos))
12223 {
12224 string = glyph->object;
12225 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12226 }
12227 }
12228
12229 /* If we reached the end of the line, and end was from a string,
12230 cursor is not on this line. */
12231 if (glyph == end && row->continued_p)
12232 return 0;
12233 }
12234
12235 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12236 w->cursor.x = x;
12237 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12238 w->cursor.y = row->y + dy;
12239
12240 if (w == XWINDOW (selected_window))
12241 {
12242 if (!row->continued_p
12243 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12244 && row->x == 0)
12245 {
12246 this_line_buffer = XBUFFER (w->buffer);
12247
12248 CHARPOS (this_line_start_pos)
12249 = MATRIX_ROW_START_CHARPOS (row) + delta;
12250 BYTEPOS (this_line_start_pos)
12251 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12252
12253 CHARPOS (this_line_end_pos)
12254 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12255 BYTEPOS (this_line_end_pos)
12256 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12257
12258 this_line_y = w->cursor.y;
12259 this_line_pixel_height = row->height;
12260 this_line_vpos = w->cursor.vpos;
12261 this_line_start_x = row->x;
12262 }
12263 else
12264 CHARPOS (this_line_start_pos) = 0;
12265 }
12266
12267 return 1;
12268 }
12269
12270
12271 /* Run window scroll functions, if any, for WINDOW with new window
12272 start STARTP. Sets the window start of WINDOW to that position.
12273
12274 We assume that the window's buffer is really current. */
12275
12276 static INLINE struct text_pos
12277 run_window_scroll_functions (window, startp)
12278 Lisp_Object window;
12279 struct text_pos startp;
12280 {
12281 struct window *w = XWINDOW (window);
12282 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12283
12284 if (current_buffer != XBUFFER (w->buffer))
12285 abort ();
12286
12287 if (!NILP (Vwindow_scroll_functions))
12288 {
12289 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12290 make_number (CHARPOS (startp)));
12291 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12292 /* In case the hook functions switch buffers. */
12293 if (current_buffer != XBUFFER (w->buffer))
12294 set_buffer_internal_1 (XBUFFER (w->buffer));
12295 }
12296
12297 return startp;
12298 }
12299
12300
12301 /* Make sure the line containing the cursor is fully visible.
12302 A value of 1 means there is nothing to be done.
12303 (Either the line is fully visible, or it cannot be made so,
12304 or we cannot tell.)
12305
12306 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12307 is higher than window.
12308
12309 A value of 0 means the caller should do scrolling
12310 as if point had gone off the screen. */
12311
12312 static int
12313 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12314 struct window *w;
12315 int force_p;
12316 int current_matrix_p;
12317 {
12318 struct glyph_matrix *matrix;
12319 struct glyph_row *row;
12320 int window_height;
12321
12322 if (!make_cursor_line_fully_visible_p)
12323 return 1;
12324
12325 /* It's not always possible to find the cursor, e.g, when a window
12326 is full of overlay strings. Don't do anything in that case. */
12327 if (w->cursor.vpos < 0)
12328 return 1;
12329
12330 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12331 row = MATRIX_ROW (matrix, w->cursor.vpos);
12332
12333 /* If the cursor row is not partially visible, there's nothing to do. */
12334 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12335 return 1;
12336
12337 /* If the row the cursor is in is taller than the window's height,
12338 it's not clear what to do, so do nothing. */
12339 window_height = window_box_height (w);
12340 if (row->height >= window_height)
12341 {
12342 if (!force_p || MINI_WINDOW_P (w)
12343 || w->vscroll || w->cursor.vpos == 0)
12344 return 1;
12345 }
12346 return 0;
12347
12348 #if 0
12349 /* This code used to try to scroll the window just enough to make
12350 the line visible. It returned 0 to say that the caller should
12351 allocate larger glyph matrices. */
12352
12353 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12354 {
12355 int dy = row->height - row->visible_height;
12356 w->vscroll = 0;
12357 w->cursor.y += dy;
12358 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12359 }
12360 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12361 {
12362 int dy = - (row->height - row->visible_height);
12363 w->vscroll = dy;
12364 w->cursor.y += dy;
12365 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12366 }
12367
12368 /* When we change the cursor y-position of the selected window,
12369 change this_line_y as well so that the display optimization for
12370 the cursor line of the selected window in redisplay_internal uses
12371 the correct y-position. */
12372 if (w == XWINDOW (selected_window))
12373 this_line_y = w->cursor.y;
12374
12375 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12376 redisplay with larger matrices. */
12377 if (matrix->nrows < required_matrix_height (w))
12378 {
12379 fonts_changed_p = 1;
12380 return 0;
12381 }
12382
12383 return 1;
12384 #endif /* 0 */
12385 }
12386
12387
12388 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12389 non-zero means only WINDOW is redisplayed in redisplay_internal.
12390 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12391 in redisplay_window to bring a partially visible line into view in
12392 the case that only the cursor has moved.
12393
12394 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12395 last screen line's vertical height extends past the end of the screen.
12396
12397 Value is
12398
12399 1 if scrolling succeeded
12400
12401 0 if scrolling didn't find point.
12402
12403 -1 if new fonts have been loaded so that we must interrupt
12404 redisplay, adjust glyph matrices, and try again. */
12405
12406 enum
12407 {
12408 SCROLLING_SUCCESS,
12409 SCROLLING_FAILED,
12410 SCROLLING_NEED_LARGER_MATRICES
12411 };
12412
12413 static int
12414 try_scrolling (window, just_this_one_p, scroll_conservatively,
12415 scroll_step, temp_scroll_step, last_line_misfit)
12416 Lisp_Object window;
12417 int just_this_one_p;
12418 EMACS_INT scroll_conservatively, scroll_step;
12419 int temp_scroll_step;
12420 int last_line_misfit;
12421 {
12422 struct window *w = XWINDOW (window);
12423 struct frame *f = XFRAME (w->frame);
12424 struct text_pos scroll_margin_pos;
12425 struct text_pos pos;
12426 struct text_pos startp;
12427 struct it it;
12428 Lisp_Object window_end;
12429 int this_scroll_margin;
12430 int dy = 0;
12431 int scroll_max;
12432 int rc;
12433 int amount_to_scroll = 0;
12434 Lisp_Object aggressive;
12435 int height;
12436 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12437
12438 #if GLYPH_DEBUG
12439 debug_method_add (w, "try_scrolling");
12440 #endif
12441
12442 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12443
12444 /* Compute scroll margin height in pixels. We scroll when point is
12445 within this distance from the top or bottom of the window. */
12446 if (scroll_margin > 0)
12447 {
12448 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12449 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12450 }
12451 else
12452 this_scroll_margin = 0;
12453
12454 /* Force scroll_conservatively to have a reasonable value so it doesn't
12455 cause an overflow while computing how much to scroll. */
12456 if (scroll_conservatively)
12457 scroll_conservatively = min (scroll_conservatively,
12458 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12459
12460 /* Compute how much we should try to scroll maximally to bring point
12461 into view. */
12462 if (scroll_step || scroll_conservatively || temp_scroll_step)
12463 scroll_max = max (scroll_step,
12464 max (scroll_conservatively, temp_scroll_step));
12465 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12466 || NUMBERP (current_buffer->scroll_up_aggressively))
12467 /* We're trying to scroll because of aggressive scrolling
12468 but no scroll_step is set. Choose an arbitrary one. Maybe
12469 there should be a variable for this. */
12470 scroll_max = 10;
12471 else
12472 scroll_max = 0;
12473 scroll_max *= FRAME_LINE_HEIGHT (f);
12474
12475 /* Decide whether we have to scroll down. Start at the window end
12476 and move this_scroll_margin up to find the position of the scroll
12477 margin. */
12478 window_end = Fwindow_end (window, Qt);
12479
12480 too_near_end:
12481
12482 CHARPOS (scroll_margin_pos) = XINT (window_end);
12483 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12484
12485 if (this_scroll_margin || extra_scroll_margin_lines)
12486 {
12487 start_display (&it, w, scroll_margin_pos);
12488 if (this_scroll_margin)
12489 move_it_vertically_backward (&it, this_scroll_margin);
12490 if (extra_scroll_margin_lines)
12491 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12492 scroll_margin_pos = it.current.pos;
12493 }
12494
12495 if (PT >= CHARPOS (scroll_margin_pos))
12496 {
12497 int y0;
12498
12499 /* Point is in the scroll margin at the bottom of the window, or
12500 below. Compute a new window start that makes point visible. */
12501
12502 /* Compute the distance from the scroll margin to PT.
12503 Give up if the distance is greater than scroll_max. */
12504 start_display (&it, w, scroll_margin_pos);
12505 y0 = it.current_y;
12506 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12507 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12508
12509 /* To make point visible, we have to move the window start
12510 down so that the line the cursor is in is visible, which
12511 means we have to add in the height of the cursor line. */
12512 dy = line_bottom_y (&it) - y0;
12513
12514 if (dy > scroll_max)
12515 return SCROLLING_FAILED;
12516
12517 /* Move the window start down. If scrolling conservatively,
12518 move it just enough down to make point visible. If
12519 scroll_step is set, move it down by scroll_step. */
12520 start_display (&it, w, startp);
12521
12522 if (scroll_conservatively)
12523 /* Set AMOUNT_TO_SCROLL to at least one line,
12524 and at most scroll_conservatively lines. */
12525 amount_to_scroll
12526 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12527 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12528 else if (scroll_step || temp_scroll_step)
12529 amount_to_scroll = scroll_max;
12530 else
12531 {
12532 aggressive = current_buffer->scroll_up_aggressively;
12533 height = WINDOW_BOX_TEXT_HEIGHT (w);
12534 if (NUMBERP (aggressive))
12535 {
12536 double float_amount = XFLOATINT (aggressive) * height;
12537 amount_to_scroll = float_amount;
12538 if (amount_to_scroll == 0 && float_amount > 0)
12539 amount_to_scroll = 1;
12540 }
12541 }
12542
12543 if (amount_to_scroll <= 0)
12544 return SCROLLING_FAILED;
12545
12546 /* If moving by amount_to_scroll leaves STARTP unchanged,
12547 move it down one screen line. */
12548
12549 move_it_vertically (&it, amount_to_scroll);
12550 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12551 move_it_by_lines (&it, 1, 1);
12552 startp = it.current.pos;
12553 }
12554 else
12555 {
12556 /* See if point is inside the scroll margin at the top of the
12557 window. */
12558 scroll_margin_pos = startp;
12559 if (this_scroll_margin)
12560 {
12561 start_display (&it, w, startp);
12562 move_it_vertically (&it, this_scroll_margin);
12563 scroll_margin_pos = it.current.pos;
12564 }
12565
12566 if (PT < CHARPOS (scroll_margin_pos))
12567 {
12568 /* Point is in the scroll margin at the top of the window or
12569 above what is displayed in the window. */
12570 int y0;
12571
12572 /* Compute the vertical distance from PT to the scroll
12573 margin position. Give up if distance is greater than
12574 scroll_max. */
12575 SET_TEXT_POS (pos, PT, PT_BYTE);
12576 start_display (&it, w, pos);
12577 y0 = it.current_y;
12578 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12579 it.last_visible_y, -1,
12580 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12581 dy = it.current_y - y0;
12582 if (dy > scroll_max)
12583 return SCROLLING_FAILED;
12584
12585 /* Compute new window start. */
12586 start_display (&it, w, startp);
12587
12588 if (scroll_conservatively)
12589 amount_to_scroll
12590 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12591 else if (scroll_step || temp_scroll_step)
12592 amount_to_scroll = scroll_max;
12593 else
12594 {
12595 aggressive = current_buffer->scroll_down_aggressively;
12596 height = WINDOW_BOX_TEXT_HEIGHT (w);
12597 if (NUMBERP (aggressive))
12598 {
12599 double float_amount = XFLOATINT (aggressive) * height;
12600 amount_to_scroll = float_amount;
12601 if (amount_to_scroll == 0 && float_amount > 0)
12602 amount_to_scroll = 1;
12603 }
12604 }
12605
12606 if (amount_to_scroll <= 0)
12607 return SCROLLING_FAILED;
12608
12609 move_it_vertically_backward (&it, amount_to_scroll);
12610 startp = it.current.pos;
12611 }
12612 }
12613
12614 /* Run window scroll functions. */
12615 startp = run_window_scroll_functions (window, startp);
12616
12617 /* Display the window. Give up if new fonts are loaded, or if point
12618 doesn't appear. */
12619 if (!try_window (window, startp, 0))
12620 rc = SCROLLING_NEED_LARGER_MATRICES;
12621 else if (w->cursor.vpos < 0)
12622 {
12623 clear_glyph_matrix (w->desired_matrix);
12624 rc = SCROLLING_FAILED;
12625 }
12626 else
12627 {
12628 /* Maybe forget recorded base line for line number display. */
12629 if (!just_this_one_p
12630 || current_buffer->clip_changed
12631 || BEG_UNCHANGED < CHARPOS (startp))
12632 w->base_line_number = Qnil;
12633
12634 /* If cursor ends up on a partially visible line,
12635 treat that as being off the bottom of the screen. */
12636 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12637 {
12638 clear_glyph_matrix (w->desired_matrix);
12639 ++extra_scroll_margin_lines;
12640 goto too_near_end;
12641 }
12642 rc = SCROLLING_SUCCESS;
12643 }
12644
12645 return rc;
12646 }
12647
12648
12649 /* Compute a suitable window start for window W if display of W starts
12650 on a continuation line. Value is non-zero if a new window start
12651 was computed.
12652
12653 The new window start will be computed, based on W's width, starting
12654 from the start of the continued line. It is the start of the
12655 screen line with the minimum distance from the old start W->start. */
12656
12657 static int
12658 compute_window_start_on_continuation_line (w)
12659 struct window *w;
12660 {
12661 struct text_pos pos, start_pos;
12662 int window_start_changed_p = 0;
12663
12664 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12665
12666 /* If window start is on a continuation line... Window start may be
12667 < BEGV in case there's invisible text at the start of the
12668 buffer (M-x rmail, for example). */
12669 if (CHARPOS (start_pos) > BEGV
12670 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12671 {
12672 struct it it;
12673 struct glyph_row *row;
12674
12675 /* Handle the case that the window start is out of range. */
12676 if (CHARPOS (start_pos) < BEGV)
12677 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12678 else if (CHARPOS (start_pos) > ZV)
12679 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12680
12681 /* Find the start of the continued line. This should be fast
12682 because scan_buffer is fast (newline cache). */
12683 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12684 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12685 row, DEFAULT_FACE_ID);
12686 reseat_at_previous_visible_line_start (&it);
12687
12688 /* If the line start is "too far" away from the window start,
12689 say it takes too much time to compute a new window start. */
12690 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12691 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12692 {
12693 int min_distance, distance;
12694
12695 /* Move forward by display lines to find the new window
12696 start. If window width was enlarged, the new start can
12697 be expected to be > the old start. If window width was
12698 decreased, the new window start will be < the old start.
12699 So, we're looking for the display line start with the
12700 minimum distance from the old window start. */
12701 pos = it.current.pos;
12702 min_distance = INFINITY;
12703 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12704 distance < min_distance)
12705 {
12706 min_distance = distance;
12707 pos = it.current.pos;
12708 move_it_by_lines (&it, 1, 0);
12709 }
12710
12711 /* Set the window start there. */
12712 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12713 window_start_changed_p = 1;
12714 }
12715 }
12716
12717 return window_start_changed_p;
12718 }
12719
12720
12721 /* Try cursor movement in case text has not changed in window WINDOW,
12722 with window start STARTP. Value is
12723
12724 CURSOR_MOVEMENT_SUCCESS if successful
12725
12726 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12727
12728 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12729 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12730 we want to scroll as if scroll-step were set to 1. See the code.
12731
12732 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12733 which case we have to abort this redisplay, and adjust matrices
12734 first. */
12735
12736 enum
12737 {
12738 CURSOR_MOVEMENT_SUCCESS,
12739 CURSOR_MOVEMENT_CANNOT_BE_USED,
12740 CURSOR_MOVEMENT_MUST_SCROLL,
12741 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12742 };
12743
12744 static int
12745 try_cursor_movement (window, startp, scroll_step)
12746 Lisp_Object window;
12747 struct text_pos startp;
12748 int *scroll_step;
12749 {
12750 struct window *w = XWINDOW (window);
12751 struct frame *f = XFRAME (w->frame);
12752 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12753
12754 #if GLYPH_DEBUG
12755 if (inhibit_try_cursor_movement)
12756 return rc;
12757 #endif
12758
12759 /* Handle case where text has not changed, only point, and it has
12760 not moved off the frame. */
12761 if (/* Point may be in this window. */
12762 PT >= CHARPOS (startp)
12763 /* Selective display hasn't changed. */
12764 && !current_buffer->clip_changed
12765 /* Function force-mode-line-update is used to force a thorough
12766 redisplay. It sets either windows_or_buffers_changed or
12767 update_mode_lines. So don't take a shortcut here for these
12768 cases. */
12769 && !update_mode_lines
12770 && !windows_or_buffers_changed
12771 && !cursor_type_changed
12772 /* Can't use this case if highlighting a region. When a
12773 region exists, cursor movement has to do more than just
12774 set the cursor. */
12775 && !(!NILP (Vtransient_mark_mode)
12776 && !NILP (current_buffer->mark_active))
12777 && NILP (w->region_showing)
12778 && NILP (Vshow_trailing_whitespace)
12779 /* Right after splitting windows, last_point may be nil. */
12780 && INTEGERP (w->last_point)
12781 /* This code is not used for mini-buffer for the sake of the case
12782 of redisplaying to replace an echo area message; since in
12783 that case the mini-buffer contents per se are usually
12784 unchanged. This code is of no real use in the mini-buffer
12785 since the handling of this_line_start_pos, etc., in redisplay
12786 handles the same cases. */
12787 && !EQ (window, minibuf_window)
12788 /* When splitting windows or for new windows, it happens that
12789 redisplay is called with a nil window_end_vpos or one being
12790 larger than the window. This should really be fixed in
12791 window.c. I don't have this on my list, now, so we do
12792 approximately the same as the old redisplay code. --gerd. */
12793 && INTEGERP (w->window_end_vpos)
12794 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12795 && (FRAME_WINDOW_P (f)
12796 || !overlay_arrow_in_current_buffer_p ()))
12797 {
12798 int this_scroll_margin, top_scroll_margin;
12799 struct glyph_row *row = NULL;
12800
12801 #if GLYPH_DEBUG
12802 debug_method_add (w, "cursor movement");
12803 #endif
12804
12805 /* Scroll if point within this distance from the top or bottom
12806 of the window. This is a pixel value. */
12807 this_scroll_margin = max (0, scroll_margin);
12808 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12809 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12810
12811 top_scroll_margin = this_scroll_margin;
12812 if (WINDOW_WANTS_HEADER_LINE_P (w))
12813 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12814
12815 /* Start with the row the cursor was displayed during the last
12816 not paused redisplay. Give up if that row is not valid. */
12817 if (w->last_cursor.vpos < 0
12818 || w->last_cursor.vpos >= w->current_matrix->nrows)
12819 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12820 else
12821 {
12822 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12823 if (row->mode_line_p)
12824 ++row;
12825 if (!row->enabled_p)
12826 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12827 }
12828
12829 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12830 {
12831 int scroll_p = 0;
12832 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12833
12834 if (PT > XFASTINT (w->last_point))
12835 {
12836 /* Point has moved forward. */
12837 while (MATRIX_ROW_END_CHARPOS (row) < PT
12838 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12839 {
12840 xassert (row->enabled_p);
12841 ++row;
12842 }
12843
12844 /* The end position of a row equals the start position
12845 of the next row. If PT is there, we would rather
12846 display it in the next line. */
12847 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12848 && MATRIX_ROW_END_CHARPOS (row) == PT
12849 && !cursor_row_p (w, row))
12850 ++row;
12851
12852 /* If within the scroll margin, scroll. Note that
12853 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12854 the next line would be drawn, and that
12855 this_scroll_margin can be zero. */
12856 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12857 || PT > MATRIX_ROW_END_CHARPOS (row)
12858 /* Line is completely visible last line in window
12859 and PT is to be set in the next line. */
12860 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12861 && PT == MATRIX_ROW_END_CHARPOS (row)
12862 && !row->ends_at_zv_p
12863 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12864 scroll_p = 1;
12865 }
12866 else if (PT < XFASTINT (w->last_point))
12867 {
12868 /* Cursor has to be moved backward. Note that PT >=
12869 CHARPOS (startp) because of the outer if-statement. */
12870 while (!row->mode_line_p
12871 && (MATRIX_ROW_START_CHARPOS (row) > PT
12872 || (MATRIX_ROW_START_CHARPOS (row) == PT
12873 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12874 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12875 row > w->current_matrix->rows
12876 && (row-1)->ends_in_newline_from_string_p))))
12877 && (row->y > top_scroll_margin
12878 || CHARPOS (startp) == BEGV))
12879 {
12880 xassert (row->enabled_p);
12881 --row;
12882 }
12883
12884 /* Consider the following case: Window starts at BEGV,
12885 there is invisible, intangible text at BEGV, so that
12886 display starts at some point START > BEGV. It can
12887 happen that we are called with PT somewhere between
12888 BEGV and START. Try to handle that case. */
12889 if (row < w->current_matrix->rows
12890 || row->mode_line_p)
12891 {
12892 row = w->current_matrix->rows;
12893 if (row->mode_line_p)
12894 ++row;
12895 }
12896
12897 /* Due to newlines in overlay strings, we may have to
12898 skip forward over overlay strings. */
12899 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12900 && MATRIX_ROW_END_CHARPOS (row) == PT
12901 && !cursor_row_p (w, row))
12902 ++row;
12903
12904 /* If within the scroll margin, scroll. */
12905 if (row->y < top_scroll_margin
12906 && CHARPOS (startp) != BEGV)
12907 scroll_p = 1;
12908 }
12909 else
12910 {
12911 /* Cursor did not move. So don't scroll even if cursor line
12912 is partially visible, as it was so before. */
12913 rc = CURSOR_MOVEMENT_SUCCESS;
12914 }
12915
12916 if (PT < MATRIX_ROW_START_CHARPOS (row)
12917 || PT > MATRIX_ROW_END_CHARPOS (row))
12918 {
12919 /* if PT is not in the glyph row, give up. */
12920 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12921 }
12922 else if (rc != CURSOR_MOVEMENT_SUCCESS
12923 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12924 && make_cursor_line_fully_visible_p)
12925 {
12926 if (PT == MATRIX_ROW_END_CHARPOS (row)
12927 && !row->ends_at_zv_p
12928 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12929 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12930 else if (row->height > window_box_height (w))
12931 {
12932 /* If we end up in a partially visible line, let's
12933 make it fully visible, except when it's taller
12934 than the window, in which case we can't do much
12935 about it. */
12936 *scroll_step = 1;
12937 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12938 }
12939 else
12940 {
12941 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12942 if (!cursor_row_fully_visible_p (w, 0, 1))
12943 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12944 else
12945 rc = CURSOR_MOVEMENT_SUCCESS;
12946 }
12947 }
12948 else if (scroll_p)
12949 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12950 else
12951 {
12952 do
12953 {
12954 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12955 {
12956 rc = CURSOR_MOVEMENT_SUCCESS;
12957 break;
12958 }
12959 ++row;
12960 }
12961 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12962 && MATRIX_ROW_START_CHARPOS (row) == PT
12963 && cursor_row_p (w, row));
12964 }
12965 }
12966 }
12967
12968 return rc;
12969 }
12970
12971 void
12972 set_vertical_scroll_bar (w)
12973 struct window *w;
12974 {
12975 int start, end, whole;
12976
12977 /* Calculate the start and end positions for the current window.
12978 At some point, it would be nice to choose between scrollbars
12979 which reflect the whole buffer size, with special markers
12980 indicating narrowing, and scrollbars which reflect only the
12981 visible region.
12982
12983 Note that mini-buffers sometimes aren't displaying any text. */
12984 if (!MINI_WINDOW_P (w)
12985 || (w == XWINDOW (minibuf_window)
12986 && NILP (echo_area_buffer[0])))
12987 {
12988 struct buffer *buf = XBUFFER (w->buffer);
12989 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12990 start = marker_position (w->start) - BUF_BEGV (buf);
12991 /* I don't think this is guaranteed to be right. For the
12992 moment, we'll pretend it is. */
12993 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12994
12995 if (end < start)
12996 end = start;
12997 if (whole < (end - start))
12998 whole = end - start;
12999 }
13000 else
13001 start = end = whole = 0;
13002
13003 /* Indicate what this scroll bar ought to be displaying now. */
13004 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13005 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13006 (w, end - start, whole, start);
13007 }
13008
13009
13010 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13011 selected_window is redisplayed.
13012
13013 We can return without actually redisplaying the window if
13014 fonts_changed_p is nonzero. In that case, redisplay_internal will
13015 retry. */
13016
13017 static void
13018 redisplay_window (window, just_this_one_p)
13019 Lisp_Object window;
13020 int just_this_one_p;
13021 {
13022 struct window *w = XWINDOW (window);
13023 struct frame *f = XFRAME (w->frame);
13024 struct buffer *buffer = XBUFFER (w->buffer);
13025 struct buffer *old = current_buffer;
13026 struct text_pos lpoint, opoint, startp;
13027 int update_mode_line;
13028 int tem;
13029 struct it it;
13030 /* Record it now because it's overwritten. */
13031 int current_matrix_up_to_date_p = 0;
13032 int used_current_matrix_p = 0;
13033 /* This is less strict than current_matrix_up_to_date_p.
13034 It indictes that the buffer contents and narrowing are unchanged. */
13035 int buffer_unchanged_p = 0;
13036 int temp_scroll_step = 0;
13037 int count = SPECPDL_INDEX ();
13038 int rc;
13039 int centering_position = -1;
13040 int last_line_misfit = 0;
13041 int beg_unchanged, end_unchanged;
13042
13043 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13044 opoint = lpoint;
13045
13046 /* W must be a leaf window here. */
13047 xassert (!NILP (w->buffer));
13048 #if GLYPH_DEBUG
13049 *w->desired_matrix->method = 0;
13050 #endif
13051
13052 restart:
13053 reconsider_clip_changes (w, buffer);
13054
13055 /* Has the mode line to be updated? */
13056 update_mode_line = (!NILP (w->update_mode_line)
13057 || update_mode_lines
13058 || buffer->clip_changed
13059 || buffer->prevent_redisplay_optimizations_p);
13060
13061 if (MINI_WINDOW_P (w))
13062 {
13063 if (w == XWINDOW (echo_area_window)
13064 && !NILP (echo_area_buffer[0]))
13065 {
13066 if (update_mode_line)
13067 /* We may have to update a tty frame's menu bar or a
13068 tool-bar. Example `M-x C-h C-h C-g'. */
13069 goto finish_menu_bars;
13070 else
13071 /* We've already displayed the echo area glyphs in this window. */
13072 goto finish_scroll_bars;
13073 }
13074 else if ((w != XWINDOW (minibuf_window)
13075 || minibuf_level == 0)
13076 /* When buffer is nonempty, redisplay window normally. */
13077 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13078 /* Quail displays non-mini buffers in minibuffer window.
13079 In that case, redisplay the window normally. */
13080 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13081 {
13082 /* W is a mini-buffer window, but it's not active, so clear
13083 it. */
13084 int yb = window_text_bottom_y (w);
13085 struct glyph_row *row;
13086 int y;
13087
13088 for (y = 0, row = w->desired_matrix->rows;
13089 y < yb;
13090 y += row->height, ++row)
13091 blank_row (w, row, y);
13092 goto finish_scroll_bars;
13093 }
13094
13095 clear_glyph_matrix (w->desired_matrix);
13096 }
13097
13098 /* Otherwise set up data on this window; select its buffer and point
13099 value. */
13100 /* Really select the buffer, for the sake of buffer-local
13101 variables. */
13102 set_buffer_internal_1 (XBUFFER (w->buffer));
13103
13104 current_matrix_up_to_date_p
13105 = (!NILP (w->window_end_valid)
13106 && !current_buffer->clip_changed
13107 && !current_buffer->prevent_redisplay_optimizations_p
13108 && XFASTINT (w->last_modified) >= MODIFF
13109 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13110
13111 /* Run the window-bottom-change-functions
13112 if it is possible that the text on the screen has changed
13113 (either due to modification of the text, or any other reason). */
13114 if (!current_matrix_up_to_date_p
13115 && !NILP (Vwindow_text_change_functions))
13116 {
13117 safe_run_hooks (Qwindow_text_change_functions);
13118 goto restart;
13119 }
13120
13121 beg_unchanged = BEG_UNCHANGED;
13122 end_unchanged = END_UNCHANGED;
13123
13124 SET_TEXT_POS (opoint, PT, PT_BYTE);
13125
13126 specbind (Qinhibit_point_motion_hooks, Qt);
13127
13128 buffer_unchanged_p
13129 = (!NILP (w->window_end_valid)
13130 && !current_buffer->clip_changed
13131 && XFASTINT (w->last_modified) >= MODIFF
13132 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13133
13134 /* When windows_or_buffers_changed is non-zero, we can't rely on
13135 the window end being valid, so set it to nil there. */
13136 if (windows_or_buffers_changed)
13137 {
13138 /* If window starts on a continuation line, maybe adjust the
13139 window start in case the window's width changed. */
13140 if (XMARKER (w->start)->buffer == current_buffer)
13141 compute_window_start_on_continuation_line (w);
13142
13143 w->window_end_valid = Qnil;
13144 }
13145
13146 /* Some sanity checks. */
13147 CHECK_WINDOW_END (w);
13148 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13149 abort ();
13150 if (BYTEPOS (opoint) < CHARPOS (opoint))
13151 abort ();
13152
13153 /* If %c is in mode line, update it if needed. */
13154 if (!NILP (w->column_number_displayed)
13155 /* This alternative quickly identifies a common case
13156 where no change is needed. */
13157 && !(PT == XFASTINT (w->last_point)
13158 && XFASTINT (w->last_modified) >= MODIFF
13159 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13160 && (XFASTINT (w->column_number_displayed)
13161 != (int) current_column ())) /* iftc */
13162 update_mode_line = 1;
13163
13164 /* Count number of windows showing the selected buffer. An indirect
13165 buffer counts as its base buffer. */
13166 if (!just_this_one_p)
13167 {
13168 struct buffer *current_base, *window_base;
13169 current_base = current_buffer;
13170 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13171 if (current_base->base_buffer)
13172 current_base = current_base->base_buffer;
13173 if (window_base->base_buffer)
13174 window_base = window_base->base_buffer;
13175 if (current_base == window_base)
13176 buffer_shared++;
13177 }
13178
13179 /* Point refers normally to the selected window. For any other
13180 window, set up appropriate value. */
13181 if (!EQ (window, selected_window))
13182 {
13183 int new_pt = XMARKER (w->pointm)->charpos;
13184 int new_pt_byte = marker_byte_position (w->pointm);
13185 if (new_pt < BEGV)
13186 {
13187 new_pt = BEGV;
13188 new_pt_byte = BEGV_BYTE;
13189 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13190 }
13191 else if (new_pt > (ZV - 1))
13192 {
13193 new_pt = ZV;
13194 new_pt_byte = ZV_BYTE;
13195 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13196 }
13197
13198 /* We don't use SET_PT so that the point-motion hooks don't run. */
13199 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13200 }
13201
13202 /* If any of the character widths specified in the display table
13203 have changed, invalidate the width run cache. It's true that
13204 this may be a bit late to catch such changes, but the rest of
13205 redisplay goes (non-fatally) haywire when the display table is
13206 changed, so why should we worry about doing any better? */
13207 if (current_buffer->width_run_cache)
13208 {
13209 struct Lisp_Char_Table *disptab = buffer_display_table ();
13210
13211 if (! disptab_matches_widthtab (disptab,
13212 XVECTOR (current_buffer->width_table)))
13213 {
13214 invalidate_region_cache (current_buffer,
13215 current_buffer->width_run_cache,
13216 BEG, Z);
13217 recompute_width_table (current_buffer, disptab);
13218 }
13219 }
13220
13221 /* If window-start is screwed up, choose a new one. */
13222 if (XMARKER (w->start)->buffer != current_buffer)
13223 goto recenter;
13224
13225 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13226
13227 /* If someone specified a new starting point but did not insist,
13228 check whether it can be used. */
13229 if (!NILP (w->optional_new_start)
13230 && CHARPOS (startp) >= BEGV
13231 && CHARPOS (startp) <= ZV)
13232 {
13233 w->optional_new_start = Qnil;
13234 start_display (&it, w, startp);
13235 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13236 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13237 if (IT_CHARPOS (it) == PT)
13238 w->force_start = Qt;
13239 /* IT may overshoot PT if text at PT is invisible. */
13240 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13241 w->force_start = Qt;
13242 }
13243
13244 force_start:
13245
13246 /* Handle case where place to start displaying has been specified,
13247 unless the specified location is outside the accessible range. */
13248 if (!NILP (w->force_start)
13249 || w->frozen_window_start_p)
13250 {
13251 /* We set this later on if we have to adjust point. */
13252 int new_vpos = -1;
13253 int val;
13254
13255 w->force_start = Qnil;
13256 w->vscroll = 0;
13257 w->window_end_valid = Qnil;
13258
13259 /* Forget any recorded base line for line number display. */
13260 if (!buffer_unchanged_p)
13261 w->base_line_number = Qnil;
13262
13263 /* Redisplay the mode line. Select the buffer properly for that.
13264 Also, run the hook window-scroll-functions
13265 because we have scrolled. */
13266 /* Note, we do this after clearing force_start because
13267 if there's an error, it is better to forget about force_start
13268 than to get into an infinite loop calling the hook functions
13269 and having them get more errors. */
13270 if (!update_mode_line
13271 || ! NILP (Vwindow_scroll_functions))
13272 {
13273 update_mode_line = 1;
13274 w->update_mode_line = Qt;
13275 startp = run_window_scroll_functions (window, startp);
13276 }
13277
13278 w->last_modified = make_number (0);
13279 w->last_overlay_modified = make_number (0);
13280 if (CHARPOS (startp) < BEGV)
13281 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13282 else if (CHARPOS (startp) > ZV)
13283 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13284
13285 /* Redisplay, then check if cursor has been set during the
13286 redisplay. Give up if new fonts were loaded. */
13287 val = try_window (window, startp, 1);
13288 if (!val)
13289 {
13290 w->force_start = Qt;
13291 clear_glyph_matrix (w->desired_matrix);
13292 goto need_larger_matrices;
13293 }
13294 /* Point was outside the scroll margins. */
13295 if (val < 0)
13296 new_vpos = window_box_height (w) / 2;
13297
13298 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13299 {
13300 /* If point does not appear, try to move point so it does
13301 appear. The desired matrix has been built above, so we
13302 can use it here. */
13303 new_vpos = window_box_height (w) / 2;
13304 }
13305
13306 if (!cursor_row_fully_visible_p (w, 0, 0))
13307 {
13308 /* Point does appear, but on a line partly visible at end of window.
13309 Move it back to a fully-visible line. */
13310 new_vpos = window_box_height (w);
13311 }
13312
13313 /* If we need to move point for either of the above reasons,
13314 now actually do it. */
13315 if (new_vpos >= 0)
13316 {
13317 struct glyph_row *row;
13318
13319 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13320 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13321 ++row;
13322
13323 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13324 MATRIX_ROW_START_BYTEPOS (row));
13325
13326 if (w != XWINDOW (selected_window))
13327 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13328 else if (current_buffer == old)
13329 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13330
13331 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13332
13333 /* If we are highlighting the region, then we just changed
13334 the region, so redisplay to show it. */
13335 if (!NILP (Vtransient_mark_mode)
13336 && !NILP (current_buffer->mark_active))
13337 {
13338 clear_glyph_matrix (w->desired_matrix);
13339 if (!try_window (window, startp, 0))
13340 goto need_larger_matrices;
13341 }
13342 }
13343
13344 #if GLYPH_DEBUG
13345 debug_method_add (w, "forced window start");
13346 #endif
13347 goto done;
13348 }
13349
13350 /* Handle case where text has not changed, only point, and it has
13351 not moved off the frame, and we are not retrying after hscroll.
13352 (current_matrix_up_to_date_p is nonzero when retrying.) */
13353 if (current_matrix_up_to_date_p
13354 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13355 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13356 {
13357 switch (rc)
13358 {
13359 case CURSOR_MOVEMENT_SUCCESS:
13360 used_current_matrix_p = 1;
13361 goto done;
13362
13363 #if 0 /* try_cursor_movement never returns this value. */
13364 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13365 goto need_larger_matrices;
13366 #endif
13367
13368 case CURSOR_MOVEMENT_MUST_SCROLL:
13369 goto try_to_scroll;
13370
13371 default:
13372 abort ();
13373 }
13374 }
13375 /* If current starting point was originally the beginning of a line
13376 but no longer is, find a new starting point. */
13377 else if (!NILP (w->start_at_line_beg)
13378 && !(CHARPOS (startp) <= BEGV
13379 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13380 {
13381 #if GLYPH_DEBUG
13382 debug_method_add (w, "recenter 1");
13383 #endif
13384 goto recenter;
13385 }
13386
13387 /* Try scrolling with try_window_id. Value is > 0 if update has
13388 been done, it is -1 if we know that the same window start will
13389 not work. It is 0 if unsuccessful for some other reason. */
13390 else if ((tem = try_window_id (w)) != 0)
13391 {
13392 #if GLYPH_DEBUG
13393 debug_method_add (w, "try_window_id %d", tem);
13394 #endif
13395
13396 if (fonts_changed_p)
13397 goto need_larger_matrices;
13398 if (tem > 0)
13399 goto done;
13400
13401 /* Otherwise try_window_id has returned -1 which means that we
13402 don't want the alternative below this comment to execute. */
13403 }
13404 else if (CHARPOS (startp) >= BEGV
13405 && CHARPOS (startp) <= ZV
13406 && PT >= CHARPOS (startp)
13407 && (CHARPOS (startp) < ZV
13408 /* Avoid starting at end of buffer. */
13409 || CHARPOS (startp) == BEGV
13410 || (XFASTINT (w->last_modified) >= MODIFF
13411 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13412 {
13413
13414 /* If first window line is a continuation line, and window start
13415 is inside the modified region, but the first change is before
13416 current window start, we must select a new window start.
13417
13418 However, if this is the result of a down-mouse event (e.g. by
13419 extending the mouse-drag-overlay), we don't want to select a
13420 new window start, since that would change the position under
13421 the mouse, resulting in an unwanted mouse-movement rather
13422 than a simple mouse-click. */
13423 if (NILP (w->start_at_line_beg)
13424 && NILP (do_mouse_tracking)
13425 && CHARPOS (startp) > BEGV
13426 && CHARPOS (startp) > BEG + beg_unchanged
13427 && CHARPOS (startp) <= Z - end_unchanged)
13428 {
13429 w->force_start = Qt;
13430 if (XMARKER (w->start)->buffer == current_buffer)
13431 compute_window_start_on_continuation_line (w);
13432 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13433 goto force_start;
13434 }
13435
13436 #if GLYPH_DEBUG
13437 debug_method_add (w, "same window start");
13438 #endif
13439
13440 /* Try to redisplay starting at same place as before.
13441 If point has not moved off frame, accept the results. */
13442 if (!current_matrix_up_to_date_p
13443 /* Don't use try_window_reusing_current_matrix in this case
13444 because a window scroll function can have changed the
13445 buffer. */
13446 || !NILP (Vwindow_scroll_functions)
13447 || MINI_WINDOW_P (w)
13448 || !(used_current_matrix_p
13449 = try_window_reusing_current_matrix (w)))
13450 {
13451 IF_DEBUG (debug_method_add (w, "1"));
13452 if (try_window (window, startp, 1) < 0)
13453 /* -1 means we need to scroll.
13454 0 means we need new matrices, but fonts_changed_p
13455 is set in that case, so we will detect it below. */
13456 goto try_to_scroll;
13457 }
13458
13459 if (fonts_changed_p)
13460 goto need_larger_matrices;
13461
13462 if (w->cursor.vpos >= 0)
13463 {
13464 if (!just_this_one_p
13465 || current_buffer->clip_changed
13466 || BEG_UNCHANGED < CHARPOS (startp))
13467 /* Forget any recorded base line for line number display. */
13468 w->base_line_number = Qnil;
13469
13470 if (!cursor_row_fully_visible_p (w, 1, 0))
13471 {
13472 clear_glyph_matrix (w->desired_matrix);
13473 last_line_misfit = 1;
13474 }
13475 /* Drop through and scroll. */
13476 else
13477 goto done;
13478 }
13479 else
13480 clear_glyph_matrix (w->desired_matrix);
13481 }
13482
13483 try_to_scroll:
13484
13485 w->last_modified = make_number (0);
13486 w->last_overlay_modified = make_number (0);
13487
13488 /* Redisplay the mode line. Select the buffer properly for that. */
13489 if (!update_mode_line)
13490 {
13491 update_mode_line = 1;
13492 w->update_mode_line = Qt;
13493 }
13494
13495 /* Try to scroll by specified few lines. */
13496 if ((scroll_conservatively
13497 || scroll_step
13498 || temp_scroll_step
13499 || NUMBERP (current_buffer->scroll_up_aggressively)
13500 || NUMBERP (current_buffer->scroll_down_aggressively))
13501 && !current_buffer->clip_changed
13502 && CHARPOS (startp) >= BEGV
13503 && CHARPOS (startp) <= ZV)
13504 {
13505 /* The function returns -1 if new fonts were loaded, 1 if
13506 successful, 0 if not successful. */
13507 int rc = try_scrolling (window, just_this_one_p,
13508 scroll_conservatively,
13509 scroll_step,
13510 temp_scroll_step, last_line_misfit);
13511 switch (rc)
13512 {
13513 case SCROLLING_SUCCESS:
13514 goto done;
13515
13516 case SCROLLING_NEED_LARGER_MATRICES:
13517 goto need_larger_matrices;
13518
13519 case SCROLLING_FAILED:
13520 break;
13521
13522 default:
13523 abort ();
13524 }
13525 }
13526
13527 /* Finally, just choose place to start which centers point */
13528
13529 recenter:
13530 if (centering_position < 0)
13531 centering_position = window_box_height (w) / 2;
13532
13533 #if GLYPH_DEBUG
13534 debug_method_add (w, "recenter");
13535 #endif
13536
13537 /* w->vscroll = 0; */
13538
13539 /* Forget any previously recorded base line for line number display. */
13540 if (!buffer_unchanged_p)
13541 w->base_line_number = Qnil;
13542
13543 /* Move backward half the height of the window. */
13544 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13545 it.current_y = it.last_visible_y;
13546 move_it_vertically_backward (&it, centering_position);
13547 xassert (IT_CHARPOS (it) >= BEGV);
13548
13549 /* The function move_it_vertically_backward may move over more
13550 than the specified y-distance. If it->w is small, e.g. a
13551 mini-buffer window, we may end up in front of the window's
13552 display area. Start displaying at the start of the line
13553 containing PT in this case. */
13554 if (it.current_y <= 0)
13555 {
13556 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13557 move_it_vertically_backward (&it, 0);
13558 #if 0
13559 /* I think this assert is bogus if buffer contains
13560 invisible text or images. KFS. */
13561 xassert (IT_CHARPOS (it) <= PT);
13562 #endif
13563 it.current_y = 0;
13564 }
13565
13566 it.current_x = it.hpos = 0;
13567
13568 /* Set startp here explicitly in case that helps avoid an infinite loop
13569 in case the window-scroll-functions functions get errors. */
13570 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13571
13572 /* Run scroll hooks. */
13573 startp = run_window_scroll_functions (window, it.current.pos);
13574
13575 /* Redisplay the window. */
13576 if (!current_matrix_up_to_date_p
13577 || windows_or_buffers_changed
13578 || cursor_type_changed
13579 /* Don't use try_window_reusing_current_matrix in this case
13580 because it can have changed the buffer. */
13581 || !NILP (Vwindow_scroll_functions)
13582 || !just_this_one_p
13583 || MINI_WINDOW_P (w)
13584 || !(used_current_matrix_p
13585 = try_window_reusing_current_matrix (w)))
13586 try_window (window, startp, 0);
13587
13588 /* If new fonts have been loaded (due to fontsets), give up. We
13589 have to start a new redisplay since we need to re-adjust glyph
13590 matrices. */
13591 if (fonts_changed_p)
13592 goto need_larger_matrices;
13593
13594 /* If cursor did not appear assume that the middle of the window is
13595 in the first line of the window. Do it again with the next line.
13596 (Imagine a window of height 100, displaying two lines of height
13597 60. Moving back 50 from it->last_visible_y will end in the first
13598 line.) */
13599 if (w->cursor.vpos < 0)
13600 {
13601 if (!NILP (w->window_end_valid)
13602 && PT >= Z - XFASTINT (w->window_end_pos))
13603 {
13604 clear_glyph_matrix (w->desired_matrix);
13605 move_it_by_lines (&it, 1, 0);
13606 try_window (window, it.current.pos, 0);
13607 }
13608 else if (PT < IT_CHARPOS (it))
13609 {
13610 clear_glyph_matrix (w->desired_matrix);
13611 move_it_by_lines (&it, -1, 0);
13612 try_window (window, it.current.pos, 0);
13613 }
13614 else
13615 {
13616 /* Not much we can do about it. */
13617 }
13618 }
13619
13620 /* Consider the following case: Window starts at BEGV, there is
13621 invisible, intangible text at BEGV, so that display starts at
13622 some point START > BEGV. It can happen that we are called with
13623 PT somewhere between BEGV and START. Try to handle that case. */
13624 if (w->cursor.vpos < 0)
13625 {
13626 struct glyph_row *row = w->current_matrix->rows;
13627 if (row->mode_line_p)
13628 ++row;
13629 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13630 }
13631
13632 if (!cursor_row_fully_visible_p (w, 0, 0))
13633 {
13634 /* If vscroll is enabled, disable it and try again. */
13635 if (w->vscroll)
13636 {
13637 w->vscroll = 0;
13638 clear_glyph_matrix (w->desired_matrix);
13639 goto recenter;
13640 }
13641
13642 /* If centering point failed to make the whole line visible,
13643 put point at the top instead. That has to make the whole line
13644 visible, if it can be done. */
13645 if (centering_position == 0)
13646 goto done;
13647
13648 clear_glyph_matrix (w->desired_matrix);
13649 centering_position = 0;
13650 goto recenter;
13651 }
13652
13653 done:
13654
13655 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13656 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13657 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13658 ? Qt : Qnil);
13659
13660 /* Display the mode line, if we must. */
13661 if ((update_mode_line
13662 /* If window not full width, must redo its mode line
13663 if (a) the window to its side is being redone and
13664 (b) we do a frame-based redisplay. This is a consequence
13665 of how inverted lines are drawn in frame-based redisplay. */
13666 || (!just_this_one_p
13667 && !FRAME_WINDOW_P (f)
13668 && !WINDOW_FULL_WIDTH_P (w))
13669 /* Line number to display. */
13670 || INTEGERP (w->base_line_pos)
13671 /* Column number is displayed and different from the one displayed. */
13672 || (!NILP (w->column_number_displayed)
13673 && (XFASTINT (w->column_number_displayed)
13674 != (int) current_column ()))) /* iftc */
13675 /* This means that the window has a mode line. */
13676 && (WINDOW_WANTS_MODELINE_P (w)
13677 || WINDOW_WANTS_HEADER_LINE_P (w)))
13678 {
13679 display_mode_lines (w);
13680
13681 /* If mode line height has changed, arrange for a thorough
13682 immediate redisplay using the correct mode line height. */
13683 if (WINDOW_WANTS_MODELINE_P (w)
13684 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13685 {
13686 fonts_changed_p = 1;
13687 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13688 = DESIRED_MODE_LINE_HEIGHT (w);
13689 }
13690
13691 /* If top line height has changed, arrange for a thorough
13692 immediate redisplay using the correct mode line height. */
13693 if (WINDOW_WANTS_HEADER_LINE_P (w)
13694 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13695 {
13696 fonts_changed_p = 1;
13697 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13698 = DESIRED_HEADER_LINE_HEIGHT (w);
13699 }
13700
13701 if (fonts_changed_p)
13702 goto need_larger_matrices;
13703 }
13704
13705 if (!line_number_displayed
13706 && !BUFFERP (w->base_line_pos))
13707 {
13708 w->base_line_pos = Qnil;
13709 w->base_line_number = Qnil;
13710 }
13711
13712 finish_menu_bars:
13713
13714 /* When we reach a frame's selected window, redo the frame's menu bar. */
13715 if (update_mode_line
13716 && EQ (FRAME_SELECTED_WINDOW (f), window))
13717 {
13718 int redisplay_menu_p = 0;
13719 int redisplay_tool_bar_p = 0;
13720
13721 if (FRAME_WINDOW_P (f))
13722 {
13723 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13724 || defined (USE_GTK)
13725 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13726 #else
13727 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13728 #endif
13729 }
13730 else
13731 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13732
13733 if (redisplay_menu_p)
13734 display_menu_bar (w);
13735
13736 #ifdef HAVE_WINDOW_SYSTEM
13737 if (FRAME_WINDOW_P (f))
13738 {
13739 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13740 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13741 #else
13742 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13743 && (FRAME_TOOL_BAR_LINES (f) > 0
13744 || !NILP (Vauto_resize_tool_bars));
13745 #endif
13746
13747 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13748 {
13749 extern int ignore_mouse_drag_p;
13750 ignore_mouse_drag_p = 1;
13751 }
13752 }
13753 #endif
13754 }
13755
13756 #ifdef HAVE_WINDOW_SYSTEM
13757 if (FRAME_WINDOW_P (f)
13758 && update_window_fringes (w, (just_this_one_p
13759 || (!used_current_matrix_p && !overlay_arrow_seen)
13760 || w->pseudo_window_p)))
13761 {
13762 update_begin (f);
13763 BLOCK_INPUT;
13764 if (draw_window_fringes (w, 1))
13765 x_draw_vertical_border (w);
13766 UNBLOCK_INPUT;
13767 update_end (f);
13768 }
13769 #endif /* HAVE_WINDOW_SYSTEM */
13770
13771 /* We go to this label, with fonts_changed_p nonzero,
13772 if it is necessary to try again using larger glyph matrices.
13773 We have to redeem the scroll bar even in this case,
13774 because the loop in redisplay_internal expects that. */
13775 need_larger_matrices:
13776 ;
13777 finish_scroll_bars:
13778
13779 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13780 {
13781 /* Set the thumb's position and size. */
13782 set_vertical_scroll_bar (w);
13783
13784 /* Note that we actually used the scroll bar attached to this
13785 window, so it shouldn't be deleted at the end of redisplay. */
13786 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13787 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13788 }
13789
13790 /* Restore current_buffer and value of point in it. */
13791 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13792 set_buffer_internal_1 (old);
13793 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13794 shorter. This can be caused by log truncation in *Messages*. */
13795 if (CHARPOS (lpoint) <= ZV)
13796 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13797
13798 unbind_to (count, Qnil);
13799 }
13800
13801
13802 /* Build the complete desired matrix of WINDOW with a window start
13803 buffer position POS.
13804
13805 Value is 1 if successful. It is zero if fonts were loaded during
13806 redisplay which makes re-adjusting glyph matrices necessary, and -1
13807 if point would appear in the scroll margins.
13808 (We check that only if CHECK_MARGINS is nonzero. */
13809
13810 int
13811 try_window (window, pos, check_margins)
13812 Lisp_Object window;
13813 struct text_pos pos;
13814 int check_margins;
13815 {
13816 struct window *w = XWINDOW (window);
13817 struct it it;
13818 struct glyph_row *last_text_row = NULL;
13819 struct frame *f = XFRAME (w->frame);
13820
13821 /* Make POS the new window start. */
13822 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13823
13824 /* Mark cursor position as unknown. No overlay arrow seen. */
13825 w->cursor.vpos = -1;
13826 overlay_arrow_seen = 0;
13827
13828 /* Initialize iterator and info to start at POS. */
13829 start_display (&it, w, pos);
13830
13831 /* Display all lines of W. */
13832 while (it.current_y < it.last_visible_y)
13833 {
13834 if (display_line (&it))
13835 last_text_row = it.glyph_row - 1;
13836 if (fonts_changed_p)
13837 return 0;
13838 }
13839
13840 /* Don't let the cursor end in the scroll margins. */
13841 if (check_margins
13842 && !MINI_WINDOW_P (w))
13843 {
13844 int this_scroll_margin;
13845
13846 this_scroll_margin = max (0, scroll_margin);
13847 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13848 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13849
13850 if ((w->cursor.y >= 0 /* not vscrolled */
13851 && w->cursor.y < this_scroll_margin
13852 && CHARPOS (pos) > BEGV
13853 && IT_CHARPOS (it) < ZV)
13854 /* rms: considering make_cursor_line_fully_visible_p here
13855 seems to give wrong results. We don't want to recenter
13856 when the last line is partly visible, we want to allow
13857 that case to be handled in the usual way. */
13858 || (w->cursor.y + 1) > it.last_visible_y)
13859 {
13860 w->cursor.vpos = -1;
13861 clear_glyph_matrix (w->desired_matrix);
13862 return -1;
13863 }
13864 }
13865
13866 /* If bottom moved off end of frame, change mode line percentage. */
13867 if (XFASTINT (w->window_end_pos) <= 0
13868 && Z != IT_CHARPOS (it))
13869 w->update_mode_line = Qt;
13870
13871 /* Set window_end_pos to the offset of the last character displayed
13872 on the window from the end of current_buffer. Set
13873 window_end_vpos to its row number. */
13874 if (last_text_row)
13875 {
13876 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13877 w->window_end_bytepos
13878 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13879 w->window_end_pos
13880 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13881 w->window_end_vpos
13882 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13883 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13884 ->displays_text_p);
13885 }
13886 else
13887 {
13888 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13889 w->window_end_pos = make_number (Z - ZV);
13890 w->window_end_vpos = make_number (0);
13891 }
13892
13893 /* But that is not valid info until redisplay finishes. */
13894 w->window_end_valid = Qnil;
13895 return 1;
13896 }
13897
13898
13899 \f
13900 /************************************************************************
13901 Window redisplay reusing current matrix when buffer has not changed
13902 ************************************************************************/
13903
13904 /* Try redisplay of window W showing an unchanged buffer with a
13905 different window start than the last time it was displayed by
13906 reusing its current matrix. Value is non-zero if successful.
13907 W->start is the new window start. */
13908
13909 static int
13910 try_window_reusing_current_matrix (w)
13911 struct window *w;
13912 {
13913 struct frame *f = XFRAME (w->frame);
13914 struct glyph_row *row, *bottom_row;
13915 struct it it;
13916 struct run run;
13917 struct text_pos start, new_start;
13918 int nrows_scrolled, i;
13919 struct glyph_row *last_text_row;
13920 struct glyph_row *last_reused_text_row;
13921 struct glyph_row *start_row;
13922 int start_vpos, min_y, max_y;
13923
13924 #if GLYPH_DEBUG
13925 if (inhibit_try_window_reusing)
13926 return 0;
13927 #endif
13928
13929 if (/* This function doesn't handle terminal frames. */
13930 !FRAME_WINDOW_P (f)
13931 /* Don't try to reuse the display if windows have been split
13932 or such. */
13933 || windows_or_buffers_changed
13934 || cursor_type_changed)
13935 return 0;
13936
13937 /* Can't do this if region may have changed. */
13938 if ((!NILP (Vtransient_mark_mode)
13939 && !NILP (current_buffer->mark_active))
13940 || !NILP (w->region_showing)
13941 || !NILP (Vshow_trailing_whitespace))
13942 return 0;
13943
13944 /* If top-line visibility has changed, give up. */
13945 if (WINDOW_WANTS_HEADER_LINE_P (w)
13946 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13947 return 0;
13948
13949 /* Give up if old or new display is scrolled vertically. We could
13950 make this function handle this, but right now it doesn't. */
13951 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13952 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13953 return 0;
13954
13955 /* The variable new_start now holds the new window start. The old
13956 start `start' can be determined from the current matrix. */
13957 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13958 start = start_row->start.pos;
13959 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13960
13961 /* Clear the desired matrix for the display below. */
13962 clear_glyph_matrix (w->desired_matrix);
13963
13964 if (CHARPOS (new_start) <= CHARPOS (start))
13965 {
13966 int first_row_y;
13967
13968 /* Don't use this method if the display starts with an ellipsis
13969 displayed for invisible text. It's not easy to handle that case
13970 below, and it's certainly not worth the effort since this is
13971 not a frequent case. */
13972 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13973 return 0;
13974
13975 IF_DEBUG (debug_method_add (w, "twu1"));
13976
13977 /* Display up to a row that can be reused. The variable
13978 last_text_row is set to the last row displayed that displays
13979 text. Note that it.vpos == 0 if or if not there is a
13980 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13981 start_display (&it, w, new_start);
13982 first_row_y = it.current_y;
13983 w->cursor.vpos = -1;
13984 last_text_row = last_reused_text_row = NULL;
13985
13986 while (it.current_y < it.last_visible_y
13987 && !fonts_changed_p)
13988 {
13989 /* If we have reached into the characters in the START row,
13990 that means the line boundaries have changed. So we
13991 can't start copying with the row START. Maybe it will
13992 work to start copying with the following row. */
13993 while (IT_CHARPOS (it) > CHARPOS (start))
13994 {
13995 /* Advance to the next row as the "start". */
13996 start_row++;
13997 start = start_row->start.pos;
13998 /* If there are no more rows to try, or just one, give up. */
13999 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14000 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14001 || CHARPOS (start) == ZV)
14002 {
14003 clear_glyph_matrix (w->desired_matrix);
14004 return 0;
14005 }
14006
14007 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14008 }
14009 /* If we have reached alignment,
14010 we can copy the rest of the rows. */
14011 if (IT_CHARPOS (it) == CHARPOS (start))
14012 break;
14013
14014 if (display_line (&it))
14015 last_text_row = it.glyph_row - 1;
14016 }
14017
14018 /* A value of current_y < last_visible_y means that we stopped
14019 at the previous window start, which in turn means that we
14020 have at least one reusable row. */
14021 if (it.current_y < it.last_visible_y)
14022 {
14023 /* IT.vpos always starts from 0; it counts text lines. */
14024 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14025
14026 /* Find PT if not already found in the lines displayed. */
14027 if (w->cursor.vpos < 0)
14028 {
14029 int dy = it.current_y - start_row->y;
14030
14031 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14032 row = row_containing_pos (w, PT, row, NULL, dy);
14033 if (row)
14034 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14035 dy, nrows_scrolled);
14036 else
14037 {
14038 clear_glyph_matrix (w->desired_matrix);
14039 return 0;
14040 }
14041 }
14042
14043 /* Scroll the display. Do it before the current matrix is
14044 changed. The problem here is that update has not yet
14045 run, i.e. part of the current matrix is not up to date.
14046 scroll_run_hook will clear the cursor, and use the
14047 current matrix to get the height of the row the cursor is
14048 in. */
14049 run.current_y = start_row->y;
14050 run.desired_y = it.current_y;
14051 run.height = it.last_visible_y - it.current_y;
14052
14053 if (run.height > 0 && run.current_y != run.desired_y)
14054 {
14055 update_begin (f);
14056 FRAME_RIF (f)->update_window_begin_hook (w);
14057 FRAME_RIF (f)->clear_window_mouse_face (w);
14058 FRAME_RIF (f)->scroll_run_hook (w, &run);
14059 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14060 update_end (f);
14061 }
14062
14063 /* Shift current matrix down by nrows_scrolled lines. */
14064 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14065 rotate_matrix (w->current_matrix,
14066 start_vpos,
14067 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14068 nrows_scrolled);
14069
14070 /* Disable lines that must be updated. */
14071 for (i = 0; i < nrows_scrolled; ++i)
14072 (start_row + i)->enabled_p = 0;
14073
14074 /* Re-compute Y positions. */
14075 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14076 max_y = it.last_visible_y;
14077 for (row = start_row + nrows_scrolled;
14078 row < bottom_row;
14079 ++row)
14080 {
14081 row->y = it.current_y;
14082 row->visible_height = row->height;
14083
14084 if (row->y < min_y)
14085 row->visible_height -= min_y - row->y;
14086 if (row->y + row->height > max_y)
14087 row->visible_height -= row->y + row->height - max_y;
14088 row->redraw_fringe_bitmaps_p = 1;
14089
14090 it.current_y += row->height;
14091
14092 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14093 last_reused_text_row = row;
14094 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14095 break;
14096 }
14097
14098 /* Disable lines in the current matrix which are now
14099 below the window. */
14100 for (++row; row < bottom_row; ++row)
14101 row->enabled_p = row->mode_line_p = 0;
14102 }
14103
14104 /* Update window_end_pos etc.; last_reused_text_row is the last
14105 reused row from the current matrix containing text, if any.
14106 The value of last_text_row is the last displayed line
14107 containing text. */
14108 if (last_reused_text_row)
14109 {
14110 w->window_end_bytepos
14111 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14112 w->window_end_pos
14113 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14114 w->window_end_vpos
14115 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14116 w->current_matrix));
14117 }
14118 else if (last_text_row)
14119 {
14120 w->window_end_bytepos
14121 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14122 w->window_end_pos
14123 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14124 w->window_end_vpos
14125 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14126 }
14127 else
14128 {
14129 /* This window must be completely empty. */
14130 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14131 w->window_end_pos = make_number (Z - ZV);
14132 w->window_end_vpos = make_number (0);
14133 }
14134 w->window_end_valid = Qnil;
14135
14136 /* Update hint: don't try scrolling again in update_window. */
14137 w->desired_matrix->no_scrolling_p = 1;
14138
14139 #if GLYPH_DEBUG
14140 debug_method_add (w, "try_window_reusing_current_matrix 1");
14141 #endif
14142 return 1;
14143 }
14144 else if (CHARPOS (new_start) > CHARPOS (start))
14145 {
14146 struct glyph_row *pt_row, *row;
14147 struct glyph_row *first_reusable_row;
14148 struct glyph_row *first_row_to_display;
14149 int dy;
14150 int yb = window_text_bottom_y (w);
14151
14152 /* Find the row starting at new_start, if there is one. Don't
14153 reuse a partially visible line at the end. */
14154 first_reusable_row = start_row;
14155 while (first_reusable_row->enabled_p
14156 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14157 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14158 < CHARPOS (new_start)))
14159 ++first_reusable_row;
14160
14161 /* Give up if there is no row to reuse. */
14162 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14163 || !first_reusable_row->enabled_p
14164 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14165 != CHARPOS (new_start)))
14166 return 0;
14167
14168 /* We can reuse fully visible rows beginning with
14169 first_reusable_row to the end of the window. Set
14170 first_row_to_display to the first row that cannot be reused.
14171 Set pt_row to the row containing point, if there is any. */
14172 pt_row = NULL;
14173 for (first_row_to_display = first_reusable_row;
14174 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14175 ++first_row_to_display)
14176 {
14177 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14178 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14179 pt_row = first_row_to_display;
14180 }
14181
14182 /* Start displaying at the start of first_row_to_display. */
14183 xassert (first_row_to_display->y < yb);
14184 init_to_row_start (&it, w, first_row_to_display);
14185
14186 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14187 - start_vpos);
14188 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14189 - nrows_scrolled);
14190 it.current_y = (first_row_to_display->y - first_reusable_row->y
14191 + WINDOW_HEADER_LINE_HEIGHT (w));
14192
14193 /* Display lines beginning with first_row_to_display in the
14194 desired matrix. Set last_text_row to the last row displayed
14195 that displays text. */
14196 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14197 if (pt_row == NULL)
14198 w->cursor.vpos = -1;
14199 last_text_row = NULL;
14200 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14201 if (display_line (&it))
14202 last_text_row = it.glyph_row - 1;
14203
14204 /* Give up If point isn't in a row displayed or reused. */
14205 if (w->cursor.vpos < 0)
14206 {
14207 clear_glyph_matrix (w->desired_matrix);
14208 return 0;
14209 }
14210
14211 /* If point is in a reused row, adjust y and vpos of the cursor
14212 position. */
14213 if (pt_row)
14214 {
14215 w->cursor.vpos -= nrows_scrolled;
14216 w->cursor.y -= first_reusable_row->y - start_row->y;
14217 }
14218
14219 /* Scroll the display. */
14220 run.current_y = first_reusable_row->y;
14221 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14222 run.height = it.last_visible_y - run.current_y;
14223 dy = run.current_y - run.desired_y;
14224
14225 if (run.height)
14226 {
14227 update_begin (f);
14228 FRAME_RIF (f)->update_window_begin_hook (w);
14229 FRAME_RIF (f)->clear_window_mouse_face (w);
14230 FRAME_RIF (f)->scroll_run_hook (w, &run);
14231 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14232 update_end (f);
14233 }
14234
14235 /* Adjust Y positions of reused rows. */
14236 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14237 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14238 max_y = it.last_visible_y;
14239 for (row = first_reusable_row; row < first_row_to_display; ++row)
14240 {
14241 row->y -= dy;
14242 row->visible_height = row->height;
14243 if (row->y < min_y)
14244 row->visible_height -= min_y - row->y;
14245 if (row->y + row->height > max_y)
14246 row->visible_height -= row->y + row->height - max_y;
14247 row->redraw_fringe_bitmaps_p = 1;
14248 }
14249
14250 /* Scroll the current matrix. */
14251 xassert (nrows_scrolled > 0);
14252 rotate_matrix (w->current_matrix,
14253 start_vpos,
14254 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14255 -nrows_scrolled);
14256
14257 /* Disable rows not reused. */
14258 for (row -= nrows_scrolled; row < bottom_row; ++row)
14259 row->enabled_p = 0;
14260
14261 /* Point may have moved to a different line, so we cannot assume that
14262 the previous cursor position is valid; locate the correct row. */
14263 if (pt_row)
14264 {
14265 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14266 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14267 row++)
14268 {
14269 w->cursor.vpos++;
14270 w->cursor.y = row->y;
14271 }
14272 if (row < bottom_row)
14273 {
14274 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14275 while (glyph->charpos < PT)
14276 {
14277 w->cursor.hpos++;
14278 w->cursor.x += glyph->pixel_width;
14279 glyph++;
14280 }
14281 }
14282 }
14283
14284 /* Adjust window end. A null value of last_text_row means that
14285 the window end is in reused rows which in turn means that
14286 only its vpos can have changed. */
14287 if (last_text_row)
14288 {
14289 w->window_end_bytepos
14290 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14291 w->window_end_pos
14292 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14293 w->window_end_vpos
14294 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14295 }
14296 else
14297 {
14298 w->window_end_vpos
14299 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14300 }
14301
14302 w->window_end_valid = Qnil;
14303 w->desired_matrix->no_scrolling_p = 1;
14304
14305 #if GLYPH_DEBUG
14306 debug_method_add (w, "try_window_reusing_current_matrix 2");
14307 #endif
14308 return 1;
14309 }
14310
14311 return 0;
14312 }
14313
14314
14315 \f
14316 /************************************************************************
14317 Window redisplay reusing current matrix when buffer has changed
14318 ************************************************************************/
14319
14320 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14321 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14322 int *, int *));
14323 static struct glyph_row *
14324 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14325 struct glyph_row *));
14326
14327
14328 /* Return the last row in MATRIX displaying text. If row START is
14329 non-null, start searching with that row. IT gives the dimensions
14330 of the display. Value is null if matrix is empty; otherwise it is
14331 a pointer to the row found. */
14332
14333 static struct glyph_row *
14334 find_last_row_displaying_text (matrix, it, start)
14335 struct glyph_matrix *matrix;
14336 struct it *it;
14337 struct glyph_row *start;
14338 {
14339 struct glyph_row *row, *row_found;
14340
14341 /* Set row_found to the last row in IT->w's current matrix
14342 displaying text. The loop looks funny but think of partially
14343 visible lines. */
14344 row_found = NULL;
14345 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14346 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14347 {
14348 xassert (row->enabled_p);
14349 row_found = row;
14350 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14351 break;
14352 ++row;
14353 }
14354
14355 return row_found;
14356 }
14357
14358
14359 /* Return the last row in the current matrix of W that is not affected
14360 by changes at the start of current_buffer that occurred since W's
14361 current matrix was built. Value is null if no such row exists.
14362
14363 BEG_UNCHANGED us the number of characters unchanged at the start of
14364 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14365 first changed character in current_buffer. Characters at positions <
14366 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14367 when the current matrix was built. */
14368
14369 static struct glyph_row *
14370 find_last_unchanged_at_beg_row (w)
14371 struct window *w;
14372 {
14373 int first_changed_pos = BEG + BEG_UNCHANGED;
14374 struct glyph_row *row;
14375 struct glyph_row *row_found = NULL;
14376 int yb = window_text_bottom_y (w);
14377
14378 /* Find the last row displaying unchanged text. */
14379 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14380 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14381 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14382 {
14383 if (/* If row ends before first_changed_pos, it is unchanged,
14384 except in some case. */
14385 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14386 /* When row ends in ZV and we write at ZV it is not
14387 unchanged. */
14388 && !row->ends_at_zv_p
14389 /* When first_changed_pos is the end of a continued line,
14390 row is not unchanged because it may be no longer
14391 continued. */
14392 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14393 && (row->continued_p
14394 || row->exact_window_width_line_p)))
14395 row_found = row;
14396
14397 /* Stop if last visible row. */
14398 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14399 break;
14400
14401 ++row;
14402 }
14403
14404 return row_found;
14405 }
14406
14407
14408 /* Find the first glyph row in the current matrix of W that is not
14409 affected by changes at the end of current_buffer since the
14410 time W's current matrix was built.
14411
14412 Return in *DELTA the number of chars by which buffer positions in
14413 unchanged text at the end of current_buffer must be adjusted.
14414
14415 Return in *DELTA_BYTES the corresponding number of bytes.
14416
14417 Value is null if no such row exists, i.e. all rows are affected by
14418 changes. */
14419
14420 static struct glyph_row *
14421 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14422 struct window *w;
14423 int *delta, *delta_bytes;
14424 {
14425 struct glyph_row *row;
14426 struct glyph_row *row_found = NULL;
14427
14428 *delta = *delta_bytes = 0;
14429
14430 /* Display must not have been paused, otherwise the current matrix
14431 is not up to date. */
14432 eassert (!NILP (w->window_end_valid));
14433
14434 /* A value of window_end_pos >= END_UNCHANGED means that the window
14435 end is in the range of changed text. If so, there is no
14436 unchanged row at the end of W's current matrix. */
14437 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14438 return NULL;
14439
14440 /* Set row to the last row in W's current matrix displaying text. */
14441 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14442
14443 /* If matrix is entirely empty, no unchanged row exists. */
14444 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14445 {
14446 /* The value of row is the last glyph row in the matrix having a
14447 meaningful buffer position in it. The end position of row
14448 corresponds to window_end_pos. This allows us to translate
14449 buffer positions in the current matrix to current buffer
14450 positions for characters not in changed text. */
14451 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14452 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14453 int last_unchanged_pos, last_unchanged_pos_old;
14454 struct glyph_row *first_text_row
14455 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14456
14457 *delta = Z - Z_old;
14458 *delta_bytes = Z_BYTE - Z_BYTE_old;
14459
14460 /* Set last_unchanged_pos to the buffer position of the last
14461 character in the buffer that has not been changed. Z is the
14462 index + 1 of the last character in current_buffer, i.e. by
14463 subtracting END_UNCHANGED we get the index of the last
14464 unchanged character, and we have to add BEG to get its buffer
14465 position. */
14466 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14467 last_unchanged_pos_old = last_unchanged_pos - *delta;
14468
14469 /* Search backward from ROW for a row displaying a line that
14470 starts at a minimum position >= last_unchanged_pos_old. */
14471 for (; row > first_text_row; --row)
14472 {
14473 /* This used to abort, but it can happen.
14474 It is ok to just stop the search instead here. KFS. */
14475 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14476 break;
14477
14478 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14479 row_found = row;
14480 }
14481 }
14482
14483 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14484
14485 return row_found;
14486 }
14487
14488
14489 /* Make sure that glyph rows in the current matrix of window W
14490 reference the same glyph memory as corresponding rows in the
14491 frame's frame matrix. This function is called after scrolling W's
14492 current matrix on a terminal frame in try_window_id and
14493 try_window_reusing_current_matrix. */
14494
14495 static void
14496 sync_frame_with_window_matrix_rows (w)
14497 struct window *w;
14498 {
14499 struct frame *f = XFRAME (w->frame);
14500 struct glyph_row *window_row, *window_row_end, *frame_row;
14501
14502 /* Preconditions: W must be a leaf window and full-width. Its frame
14503 must have a frame matrix. */
14504 xassert (NILP (w->hchild) && NILP (w->vchild));
14505 xassert (WINDOW_FULL_WIDTH_P (w));
14506 xassert (!FRAME_WINDOW_P (f));
14507
14508 /* If W is a full-width window, glyph pointers in W's current matrix
14509 have, by definition, to be the same as glyph pointers in the
14510 corresponding frame matrix. Note that frame matrices have no
14511 marginal areas (see build_frame_matrix). */
14512 window_row = w->current_matrix->rows;
14513 window_row_end = window_row + w->current_matrix->nrows;
14514 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14515 while (window_row < window_row_end)
14516 {
14517 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14518 struct glyph *end = window_row->glyphs[LAST_AREA];
14519
14520 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14521 frame_row->glyphs[TEXT_AREA] = start;
14522 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14523 frame_row->glyphs[LAST_AREA] = end;
14524
14525 /* Disable frame rows whose corresponding window rows have
14526 been disabled in try_window_id. */
14527 if (!window_row->enabled_p)
14528 frame_row->enabled_p = 0;
14529
14530 ++window_row, ++frame_row;
14531 }
14532 }
14533
14534
14535 /* Find the glyph row in window W containing CHARPOS. Consider all
14536 rows between START and END (not inclusive). END null means search
14537 all rows to the end of the display area of W. Value is the row
14538 containing CHARPOS or null. */
14539
14540 struct glyph_row *
14541 row_containing_pos (w, charpos, start, end, dy)
14542 struct window *w;
14543 int charpos;
14544 struct glyph_row *start, *end;
14545 int dy;
14546 {
14547 struct glyph_row *row = start;
14548 int last_y;
14549
14550 /* If we happen to start on a header-line, skip that. */
14551 if (row->mode_line_p)
14552 ++row;
14553
14554 if ((end && row >= end) || !row->enabled_p)
14555 return NULL;
14556
14557 last_y = window_text_bottom_y (w) - dy;
14558
14559 while (1)
14560 {
14561 /* Give up if we have gone too far. */
14562 if (end && row >= end)
14563 return NULL;
14564 /* This formerly returned if they were equal.
14565 I think that both quantities are of a "last plus one" type;
14566 if so, when they are equal, the row is within the screen. -- rms. */
14567 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14568 return NULL;
14569
14570 /* If it is in this row, return this row. */
14571 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14572 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14573 /* The end position of a row equals the start
14574 position of the next row. If CHARPOS is there, we
14575 would rather display it in the next line, except
14576 when this line ends in ZV. */
14577 && !row->ends_at_zv_p
14578 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14579 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14580 return row;
14581 ++row;
14582 }
14583 }
14584
14585
14586 /* Try to redisplay window W by reusing its existing display. W's
14587 current matrix must be up to date when this function is called,
14588 i.e. window_end_valid must not be nil.
14589
14590 Value is
14591
14592 1 if display has been updated
14593 0 if otherwise unsuccessful
14594 -1 if redisplay with same window start is known not to succeed
14595
14596 The following steps are performed:
14597
14598 1. Find the last row in the current matrix of W that is not
14599 affected by changes at the start of current_buffer. If no such row
14600 is found, give up.
14601
14602 2. Find the first row in W's current matrix that is not affected by
14603 changes at the end of current_buffer. Maybe there is no such row.
14604
14605 3. Display lines beginning with the row + 1 found in step 1 to the
14606 row found in step 2 or, if step 2 didn't find a row, to the end of
14607 the window.
14608
14609 4. If cursor is not known to appear on the window, give up.
14610
14611 5. If display stopped at the row found in step 2, scroll the
14612 display and current matrix as needed.
14613
14614 6. Maybe display some lines at the end of W, if we must. This can
14615 happen under various circumstances, like a partially visible line
14616 becoming fully visible, or because newly displayed lines are displayed
14617 in smaller font sizes.
14618
14619 7. Update W's window end information. */
14620
14621 static int
14622 try_window_id (w)
14623 struct window *w;
14624 {
14625 struct frame *f = XFRAME (w->frame);
14626 struct glyph_matrix *current_matrix = w->current_matrix;
14627 struct glyph_matrix *desired_matrix = w->desired_matrix;
14628 struct glyph_row *last_unchanged_at_beg_row;
14629 struct glyph_row *first_unchanged_at_end_row;
14630 struct glyph_row *row;
14631 struct glyph_row *bottom_row;
14632 int bottom_vpos;
14633 struct it it;
14634 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14635 struct text_pos start_pos;
14636 struct run run;
14637 int first_unchanged_at_end_vpos = 0;
14638 struct glyph_row *last_text_row, *last_text_row_at_end;
14639 struct text_pos start;
14640 int first_changed_charpos, last_changed_charpos;
14641
14642 #if GLYPH_DEBUG
14643 if (inhibit_try_window_id)
14644 return 0;
14645 #endif
14646
14647 /* This is handy for debugging. */
14648 #if 0
14649 #define GIVE_UP(X) \
14650 do { \
14651 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14652 return 0; \
14653 } while (0)
14654 #else
14655 #define GIVE_UP(X) return 0
14656 #endif
14657
14658 SET_TEXT_POS_FROM_MARKER (start, w->start);
14659
14660 /* Don't use this for mini-windows because these can show
14661 messages and mini-buffers, and we don't handle that here. */
14662 if (MINI_WINDOW_P (w))
14663 GIVE_UP (1);
14664
14665 /* This flag is used to prevent redisplay optimizations. */
14666 if (windows_or_buffers_changed || cursor_type_changed)
14667 GIVE_UP (2);
14668
14669 /* Verify that narrowing has not changed.
14670 Also verify that we were not told to prevent redisplay optimizations.
14671 It would be nice to further
14672 reduce the number of cases where this prevents try_window_id. */
14673 if (current_buffer->clip_changed
14674 || current_buffer->prevent_redisplay_optimizations_p)
14675 GIVE_UP (3);
14676
14677 /* Window must either use window-based redisplay or be full width. */
14678 if (!FRAME_WINDOW_P (f)
14679 && (!FRAME_LINE_INS_DEL_OK (f)
14680 || !WINDOW_FULL_WIDTH_P (w)))
14681 GIVE_UP (4);
14682
14683 /* Give up if point is not known NOT to appear in W. */
14684 if (PT < CHARPOS (start))
14685 GIVE_UP (5);
14686
14687 /* Another way to prevent redisplay optimizations. */
14688 if (XFASTINT (w->last_modified) == 0)
14689 GIVE_UP (6);
14690
14691 /* Verify that window is not hscrolled. */
14692 if (XFASTINT (w->hscroll) != 0)
14693 GIVE_UP (7);
14694
14695 /* Verify that display wasn't paused. */
14696 if (NILP (w->window_end_valid))
14697 GIVE_UP (8);
14698
14699 /* Can't use this if highlighting a region because a cursor movement
14700 will do more than just set the cursor. */
14701 if (!NILP (Vtransient_mark_mode)
14702 && !NILP (current_buffer->mark_active))
14703 GIVE_UP (9);
14704
14705 /* Likewise if highlighting trailing whitespace. */
14706 if (!NILP (Vshow_trailing_whitespace))
14707 GIVE_UP (11);
14708
14709 /* Likewise if showing a region. */
14710 if (!NILP (w->region_showing))
14711 GIVE_UP (10);
14712
14713 /* Can use this if overlay arrow position and or string have changed. */
14714 if (overlay_arrows_changed_p ())
14715 GIVE_UP (12);
14716
14717
14718 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14719 only if buffer has really changed. The reason is that the gap is
14720 initially at Z for freshly visited files. The code below would
14721 set end_unchanged to 0 in that case. */
14722 if (MODIFF > SAVE_MODIFF
14723 /* This seems to happen sometimes after saving a buffer. */
14724 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14725 {
14726 if (GPT - BEG < BEG_UNCHANGED)
14727 BEG_UNCHANGED = GPT - BEG;
14728 if (Z - GPT < END_UNCHANGED)
14729 END_UNCHANGED = Z - GPT;
14730 }
14731
14732 /* The position of the first and last character that has been changed. */
14733 first_changed_charpos = BEG + BEG_UNCHANGED;
14734 last_changed_charpos = Z - END_UNCHANGED;
14735
14736 /* If window starts after a line end, and the last change is in
14737 front of that newline, then changes don't affect the display.
14738 This case happens with stealth-fontification. Note that although
14739 the display is unchanged, glyph positions in the matrix have to
14740 be adjusted, of course. */
14741 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14742 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14743 && ((last_changed_charpos < CHARPOS (start)
14744 && CHARPOS (start) == BEGV)
14745 || (last_changed_charpos < CHARPOS (start) - 1
14746 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14747 {
14748 int Z_old, delta, Z_BYTE_old, delta_bytes;
14749 struct glyph_row *r0;
14750
14751 /* Compute how many chars/bytes have been added to or removed
14752 from the buffer. */
14753 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14754 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14755 delta = Z - Z_old;
14756 delta_bytes = Z_BYTE - Z_BYTE_old;
14757
14758 /* Give up if PT is not in the window. Note that it already has
14759 been checked at the start of try_window_id that PT is not in
14760 front of the window start. */
14761 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14762 GIVE_UP (13);
14763
14764 /* If window start is unchanged, we can reuse the whole matrix
14765 as is, after adjusting glyph positions. No need to compute
14766 the window end again, since its offset from Z hasn't changed. */
14767 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14768 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14769 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14770 /* PT must not be in a partially visible line. */
14771 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14772 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14773 {
14774 /* Adjust positions in the glyph matrix. */
14775 if (delta || delta_bytes)
14776 {
14777 struct glyph_row *r1
14778 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14779 increment_matrix_positions (w->current_matrix,
14780 MATRIX_ROW_VPOS (r0, current_matrix),
14781 MATRIX_ROW_VPOS (r1, current_matrix),
14782 delta, delta_bytes);
14783 }
14784
14785 /* Set the cursor. */
14786 row = row_containing_pos (w, PT, r0, NULL, 0);
14787 if (row)
14788 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14789 else
14790 abort ();
14791 return 1;
14792 }
14793 }
14794
14795 /* Handle the case that changes are all below what is displayed in
14796 the window, and that PT is in the window. This shortcut cannot
14797 be taken if ZV is visible in the window, and text has been added
14798 there that is visible in the window. */
14799 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14800 /* ZV is not visible in the window, or there are no
14801 changes at ZV, actually. */
14802 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14803 || first_changed_charpos == last_changed_charpos))
14804 {
14805 struct glyph_row *r0;
14806
14807 /* Give up if PT is not in the window. Note that it already has
14808 been checked at the start of try_window_id that PT is not in
14809 front of the window start. */
14810 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14811 GIVE_UP (14);
14812
14813 /* If window start is unchanged, we can reuse the whole matrix
14814 as is, without changing glyph positions since no text has
14815 been added/removed in front of the window end. */
14816 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14817 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14818 /* PT must not be in a partially visible line. */
14819 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14820 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14821 {
14822 /* We have to compute the window end anew since text
14823 can have been added/removed after it. */
14824 w->window_end_pos
14825 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14826 w->window_end_bytepos
14827 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14828
14829 /* Set the cursor. */
14830 row = row_containing_pos (w, PT, r0, NULL, 0);
14831 if (row)
14832 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14833 else
14834 abort ();
14835 return 2;
14836 }
14837 }
14838
14839 /* Give up if window start is in the changed area.
14840
14841 The condition used to read
14842
14843 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14844
14845 but why that was tested escapes me at the moment. */
14846 if (CHARPOS (start) >= first_changed_charpos
14847 && CHARPOS (start) <= last_changed_charpos)
14848 GIVE_UP (15);
14849
14850 /* Check that window start agrees with the start of the first glyph
14851 row in its current matrix. Check this after we know the window
14852 start is not in changed text, otherwise positions would not be
14853 comparable. */
14854 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14855 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14856 GIVE_UP (16);
14857
14858 /* Give up if the window ends in strings. Overlay strings
14859 at the end are difficult to handle, so don't try. */
14860 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14861 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14862 GIVE_UP (20);
14863
14864 /* Compute the position at which we have to start displaying new
14865 lines. Some of the lines at the top of the window might be
14866 reusable because they are not displaying changed text. Find the
14867 last row in W's current matrix not affected by changes at the
14868 start of current_buffer. Value is null if changes start in the
14869 first line of window. */
14870 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14871 if (last_unchanged_at_beg_row)
14872 {
14873 /* Avoid starting to display in the moddle of a character, a TAB
14874 for instance. This is easier than to set up the iterator
14875 exactly, and it's not a frequent case, so the additional
14876 effort wouldn't really pay off. */
14877 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14878 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14879 && last_unchanged_at_beg_row > w->current_matrix->rows)
14880 --last_unchanged_at_beg_row;
14881
14882 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14883 GIVE_UP (17);
14884
14885 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14886 GIVE_UP (18);
14887 start_pos = it.current.pos;
14888
14889 /* Start displaying new lines in the desired matrix at the same
14890 vpos we would use in the current matrix, i.e. below
14891 last_unchanged_at_beg_row. */
14892 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14893 current_matrix);
14894 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14895 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14896
14897 xassert (it.hpos == 0 && it.current_x == 0);
14898 }
14899 else
14900 {
14901 /* There are no reusable lines at the start of the window.
14902 Start displaying in the first text line. */
14903 start_display (&it, w, start);
14904 it.vpos = it.first_vpos;
14905 start_pos = it.current.pos;
14906 }
14907
14908 /* Find the first row that is not affected by changes at the end of
14909 the buffer. Value will be null if there is no unchanged row, in
14910 which case we must redisplay to the end of the window. delta
14911 will be set to the value by which buffer positions beginning with
14912 first_unchanged_at_end_row have to be adjusted due to text
14913 changes. */
14914 first_unchanged_at_end_row
14915 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14916 IF_DEBUG (debug_delta = delta);
14917 IF_DEBUG (debug_delta_bytes = delta_bytes);
14918
14919 /* Set stop_pos to the buffer position up to which we will have to
14920 display new lines. If first_unchanged_at_end_row != NULL, this
14921 is the buffer position of the start of the line displayed in that
14922 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14923 that we don't stop at a buffer position. */
14924 stop_pos = 0;
14925 if (first_unchanged_at_end_row)
14926 {
14927 xassert (last_unchanged_at_beg_row == NULL
14928 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14929
14930 /* If this is a continuation line, move forward to the next one
14931 that isn't. Changes in lines above affect this line.
14932 Caution: this may move first_unchanged_at_end_row to a row
14933 not displaying text. */
14934 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14935 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14936 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14937 < it.last_visible_y))
14938 ++first_unchanged_at_end_row;
14939
14940 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14941 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14942 >= it.last_visible_y))
14943 first_unchanged_at_end_row = NULL;
14944 else
14945 {
14946 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14947 + delta);
14948 first_unchanged_at_end_vpos
14949 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14950 xassert (stop_pos >= Z - END_UNCHANGED);
14951 }
14952 }
14953 else if (last_unchanged_at_beg_row == NULL)
14954 GIVE_UP (19);
14955
14956
14957 #if GLYPH_DEBUG
14958
14959 /* Either there is no unchanged row at the end, or the one we have
14960 now displays text. This is a necessary condition for the window
14961 end pos calculation at the end of this function. */
14962 xassert (first_unchanged_at_end_row == NULL
14963 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14964
14965 debug_last_unchanged_at_beg_vpos
14966 = (last_unchanged_at_beg_row
14967 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14968 : -1);
14969 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14970
14971 #endif /* GLYPH_DEBUG != 0 */
14972
14973
14974 /* Display new lines. Set last_text_row to the last new line
14975 displayed which has text on it, i.e. might end up as being the
14976 line where the window_end_vpos is. */
14977 w->cursor.vpos = -1;
14978 last_text_row = NULL;
14979 overlay_arrow_seen = 0;
14980 while (it.current_y < it.last_visible_y
14981 && !fonts_changed_p
14982 && (first_unchanged_at_end_row == NULL
14983 || IT_CHARPOS (it) < stop_pos))
14984 {
14985 if (display_line (&it))
14986 last_text_row = it.glyph_row - 1;
14987 }
14988
14989 if (fonts_changed_p)
14990 return -1;
14991
14992
14993 /* Compute differences in buffer positions, y-positions etc. for
14994 lines reused at the bottom of the window. Compute what we can
14995 scroll. */
14996 if (first_unchanged_at_end_row
14997 /* No lines reused because we displayed everything up to the
14998 bottom of the window. */
14999 && it.current_y < it.last_visible_y)
15000 {
15001 dvpos = (it.vpos
15002 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15003 current_matrix));
15004 dy = it.current_y - first_unchanged_at_end_row->y;
15005 run.current_y = first_unchanged_at_end_row->y;
15006 run.desired_y = run.current_y + dy;
15007 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15008 }
15009 else
15010 {
15011 delta = delta_bytes = dvpos = dy
15012 = run.current_y = run.desired_y = run.height = 0;
15013 first_unchanged_at_end_row = NULL;
15014 }
15015 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15016
15017
15018 /* Find the cursor if not already found. We have to decide whether
15019 PT will appear on this window (it sometimes doesn't, but this is
15020 not a very frequent case.) This decision has to be made before
15021 the current matrix is altered. A value of cursor.vpos < 0 means
15022 that PT is either in one of the lines beginning at
15023 first_unchanged_at_end_row or below the window. Don't care for
15024 lines that might be displayed later at the window end; as
15025 mentioned, this is not a frequent case. */
15026 if (w->cursor.vpos < 0)
15027 {
15028 /* Cursor in unchanged rows at the top? */
15029 if (PT < CHARPOS (start_pos)
15030 && last_unchanged_at_beg_row)
15031 {
15032 row = row_containing_pos (w, PT,
15033 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15034 last_unchanged_at_beg_row + 1, 0);
15035 if (row)
15036 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15037 }
15038
15039 /* Start from first_unchanged_at_end_row looking for PT. */
15040 else if (first_unchanged_at_end_row)
15041 {
15042 row = row_containing_pos (w, PT - delta,
15043 first_unchanged_at_end_row, NULL, 0);
15044 if (row)
15045 set_cursor_from_row (w, row, w->current_matrix, delta,
15046 delta_bytes, dy, dvpos);
15047 }
15048
15049 /* Give up if cursor was not found. */
15050 if (w->cursor.vpos < 0)
15051 {
15052 clear_glyph_matrix (w->desired_matrix);
15053 return -1;
15054 }
15055 }
15056
15057 /* Don't let the cursor end in the scroll margins. */
15058 {
15059 int this_scroll_margin, cursor_height;
15060
15061 this_scroll_margin = max (0, scroll_margin);
15062 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15063 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15064 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15065
15066 if ((w->cursor.y < this_scroll_margin
15067 && CHARPOS (start) > BEGV)
15068 /* Old redisplay didn't take scroll margin into account at the bottom,
15069 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15070 || (w->cursor.y + (make_cursor_line_fully_visible_p
15071 ? cursor_height + this_scroll_margin
15072 : 1)) > it.last_visible_y)
15073 {
15074 w->cursor.vpos = -1;
15075 clear_glyph_matrix (w->desired_matrix);
15076 return -1;
15077 }
15078 }
15079
15080 /* Scroll the display. Do it before changing the current matrix so
15081 that xterm.c doesn't get confused about where the cursor glyph is
15082 found. */
15083 if (dy && run.height)
15084 {
15085 update_begin (f);
15086
15087 if (FRAME_WINDOW_P (f))
15088 {
15089 FRAME_RIF (f)->update_window_begin_hook (w);
15090 FRAME_RIF (f)->clear_window_mouse_face (w);
15091 FRAME_RIF (f)->scroll_run_hook (w, &run);
15092 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15093 }
15094 else
15095 {
15096 /* Terminal frame. In this case, dvpos gives the number of
15097 lines to scroll by; dvpos < 0 means scroll up. */
15098 int first_unchanged_at_end_vpos
15099 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15100 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15101 int end = (WINDOW_TOP_EDGE_LINE (w)
15102 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15103 + window_internal_height (w));
15104
15105 /* Perform the operation on the screen. */
15106 if (dvpos > 0)
15107 {
15108 /* Scroll last_unchanged_at_beg_row to the end of the
15109 window down dvpos lines. */
15110 set_terminal_window (f, end);
15111
15112 /* On dumb terminals delete dvpos lines at the end
15113 before inserting dvpos empty lines. */
15114 if (!FRAME_SCROLL_REGION_OK (f))
15115 ins_del_lines (f, end - dvpos, -dvpos);
15116
15117 /* Insert dvpos empty lines in front of
15118 last_unchanged_at_beg_row. */
15119 ins_del_lines (f, from, dvpos);
15120 }
15121 else if (dvpos < 0)
15122 {
15123 /* Scroll up last_unchanged_at_beg_vpos to the end of
15124 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15125 set_terminal_window (f, end);
15126
15127 /* Delete dvpos lines in front of
15128 last_unchanged_at_beg_vpos. ins_del_lines will set
15129 the cursor to the given vpos and emit |dvpos| delete
15130 line sequences. */
15131 ins_del_lines (f, from + dvpos, dvpos);
15132
15133 /* On a dumb terminal insert dvpos empty lines at the
15134 end. */
15135 if (!FRAME_SCROLL_REGION_OK (f))
15136 ins_del_lines (f, end + dvpos, -dvpos);
15137 }
15138
15139 set_terminal_window (f, 0);
15140 }
15141
15142 update_end (f);
15143 }
15144
15145 /* Shift reused rows of the current matrix to the right position.
15146 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15147 text. */
15148 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15149 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15150 if (dvpos < 0)
15151 {
15152 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15153 bottom_vpos, dvpos);
15154 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15155 bottom_vpos, 0);
15156 }
15157 else if (dvpos > 0)
15158 {
15159 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15160 bottom_vpos, dvpos);
15161 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15162 first_unchanged_at_end_vpos + dvpos, 0);
15163 }
15164
15165 /* For frame-based redisplay, make sure that current frame and window
15166 matrix are in sync with respect to glyph memory. */
15167 if (!FRAME_WINDOW_P (f))
15168 sync_frame_with_window_matrix_rows (w);
15169
15170 /* Adjust buffer positions in reused rows. */
15171 if (delta || delta_bytes)
15172 increment_matrix_positions (current_matrix,
15173 first_unchanged_at_end_vpos + dvpos,
15174 bottom_vpos, delta, delta_bytes);
15175
15176 /* Adjust Y positions. */
15177 if (dy)
15178 shift_glyph_matrix (w, current_matrix,
15179 first_unchanged_at_end_vpos + dvpos,
15180 bottom_vpos, dy);
15181
15182 if (first_unchanged_at_end_row)
15183 {
15184 first_unchanged_at_end_row += dvpos;
15185 if (first_unchanged_at_end_row->y >= it.last_visible_y
15186 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15187 first_unchanged_at_end_row = NULL;
15188 }
15189
15190 /* If scrolling up, there may be some lines to display at the end of
15191 the window. */
15192 last_text_row_at_end = NULL;
15193 if (dy < 0)
15194 {
15195 /* Scrolling up can leave for example a partially visible line
15196 at the end of the window to be redisplayed. */
15197 /* Set last_row to the glyph row in the current matrix where the
15198 window end line is found. It has been moved up or down in
15199 the matrix by dvpos. */
15200 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15201 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15202
15203 /* If last_row is the window end line, it should display text. */
15204 xassert (last_row->displays_text_p);
15205
15206 /* If window end line was partially visible before, begin
15207 displaying at that line. Otherwise begin displaying with the
15208 line following it. */
15209 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15210 {
15211 init_to_row_start (&it, w, last_row);
15212 it.vpos = last_vpos;
15213 it.current_y = last_row->y;
15214 }
15215 else
15216 {
15217 init_to_row_end (&it, w, last_row);
15218 it.vpos = 1 + last_vpos;
15219 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15220 ++last_row;
15221 }
15222
15223 /* We may start in a continuation line. If so, we have to
15224 get the right continuation_lines_width and current_x. */
15225 it.continuation_lines_width = last_row->continuation_lines_width;
15226 it.hpos = it.current_x = 0;
15227
15228 /* Display the rest of the lines at the window end. */
15229 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15230 while (it.current_y < it.last_visible_y
15231 && !fonts_changed_p)
15232 {
15233 /* Is it always sure that the display agrees with lines in
15234 the current matrix? I don't think so, so we mark rows
15235 displayed invalid in the current matrix by setting their
15236 enabled_p flag to zero. */
15237 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15238 if (display_line (&it))
15239 last_text_row_at_end = it.glyph_row - 1;
15240 }
15241 }
15242
15243 /* Update window_end_pos and window_end_vpos. */
15244 if (first_unchanged_at_end_row
15245 && !last_text_row_at_end)
15246 {
15247 /* Window end line if one of the preserved rows from the current
15248 matrix. Set row to the last row displaying text in current
15249 matrix starting at first_unchanged_at_end_row, after
15250 scrolling. */
15251 xassert (first_unchanged_at_end_row->displays_text_p);
15252 row = find_last_row_displaying_text (w->current_matrix, &it,
15253 first_unchanged_at_end_row);
15254 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15255
15256 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15257 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15258 w->window_end_vpos
15259 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15260 xassert (w->window_end_bytepos >= 0);
15261 IF_DEBUG (debug_method_add (w, "A"));
15262 }
15263 else if (last_text_row_at_end)
15264 {
15265 w->window_end_pos
15266 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15267 w->window_end_bytepos
15268 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15269 w->window_end_vpos
15270 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15271 xassert (w->window_end_bytepos >= 0);
15272 IF_DEBUG (debug_method_add (w, "B"));
15273 }
15274 else if (last_text_row)
15275 {
15276 /* We have displayed either to the end of the window or at the
15277 end of the window, i.e. the last row with text is to be found
15278 in the desired matrix. */
15279 w->window_end_pos
15280 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15281 w->window_end_bytepos
15282 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15283 w->window_end_vpos
15284 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15285 xassert (w->window_end_bytepos >= 0);
15286 }
15287 else if (first_unchanged_at_end_row == NULL
15288 && last_text_row == NULL
15289 && last_text_row_at_end == NULL)
15290 {
15291 /* Displayed to end of window, but no line containing text was
15292 displayed. Lines were deleted at the end of the window. */
15293 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15294 int vpos = XFASTINT (w->window_end_vpos);
15295 struct glyph_row *current_row = current_matrix->rows + vpos;
15296 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15297
15298 for (row = NULL;
15299 row == NULL && vpos >= first_vpos;
15300 --vpos, --current_row, --desired_row)
15301 {
15302 if (desired_row->enabled_p)
15303 {
15304 if (desired_row->displays_text_p)
15305 row = desired_row;
15306 }
15307 else if (current_row->displays_text_p)
15308 row = current_row;
15309 }
15310
15311 xassert (row != NULL);
15312 w->window_end_vpos = make_number (vpos + 1);
15313 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15314 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15315 xassert (w->window_end_bytepos >= 0);
15316 IF_DEBUG (debug_method_add (w, "C"));
15317 }
15318 else
15319 abort ();
15320
15321 #if 0 /* This leads to problems, for instance when the cursor is
15322 at ZV, and the cursor line displays no text. */
15323 /* Disable rows below what's displayed in the window. This makes
15324 debugging easier. */
15325 enable_glyph_matrix_rows (current_matrix,
15326 XFASTINT (w->window_end_vpos) + 1,
15327 bottom_vpos, 0);
15328 #endif
15329
15330 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15331 debug_end_vpos = XFASTINT (w->window_end_vpos));
15332
15333 /* Record that display has not been completed. */
15334 w->window_end_valid = Qnil;
15335 w->desired_matrix->no_scrolling_p = 1;
15336 return 3;
15337
15338 #undef GIVE_UP
15339 }
15340
15341
15342 \f
15343 /***********************************************************************
15344 More debugging support
15345 ***********************************************************************/
15346
15347 #if GLYPH_DEBUG
15348
15349 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15350 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15351 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15352
15353
15354 /* Dump the contents of glyph matrix MATRIX on stderr.
15355
15356 GLYPHS 0 means don't show glyph contents.
15357 GLYPHS 1 means show glyphs in short form
15358 GLYPHS > 1 means show glyphs in long form. */
15359
15360 void
15361 dump_glyph_matrix (matrix, glyphs)
15362 struct glyph_matrix *matrix;
15363 int glyphs;
15364 {
15365 int i;
15366 for (i = 0; i < matrix->nrows; ++i)
15367 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15368 }
15369
15370
15371 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15372 the glyph row and area where the glyph comes from. */
15373
15374 void
15375 dump_glyph (row, glyph, area)
15376 struct glyph_row *row;
15377 struct glyph *glyph;
15378 int area;
15379 {
15380 if (glyph->type == CHAR_GLYPH)
15381 {
15382 fprintf (stderr,
15383 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15384 glyph - row->glyphs[TEXT_AREA],
15385 'C',
15386 glyph->charpos,
15387 (BUFFERP (glyph->object)
15388 ? 'B'
15389 : (STRINGP (glyph->object)
15390 ? 'S'
15391 : '-')),
15392 glyph->pixel_width,
15393 glyph->u.ch,
15394 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15395 ? glyph->u.ch
15396 : '.'),
15397 glyph->face_id,
15398 glyph->left_box_line_p,
15399 glyph->right_box_line_p);
15400 }
15401 else if (glyph->type == STRETCH_GLYPH)
15402 {
15403 fprintf (stderr,
15404 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15405 glyph - row->glyphs[TEXT_AREA],
15406 'S',
15407 glyph->charpos,
15408 (BUFFERP (glyph->object)
15409 ? 'B'
15410 : (STRINGP (glyph->object)
15411 ? 'S'
15412 : '-')),
15413 glyph->pixel_width,
15414 0,
15415 '.',
15416 glyph->face_id,
15417 glyph->left_box_line_p,
15418 glyph->right_box_line_p);
15419 }
15420 else if (glyph->type == IMAGE_GLYPH)
15421 {
15422 fprintf (stderr,
15423 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15424 glyph - row->glyphs[TEXT_AREA],
15425 'I',
15426 glyph->charpos,
15427 (BUFFERP (glyph->object)
15428 ? 'B'
15429 : (STRINGP (glyph->object)
15430 ? 'S'
15431 : '-')),
15432 glyph->pixel_width,
15433 glyph->u.img_id,
15434 '.',
15435 glyph->face_id,
15436 glyph->left_box_line_p,
15437 glyph->right_box_line_p);
15438 }
15439 else if (glyph->type == COMPOSITE_GLYPH)
15440 {
15441 fprintf (stderr,
15442 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15443 glyph - row->glyphs[TEXT_AREA],
15444 '+',
15445 glyph->charpos,
15446 (BUFFERP (glyph->object)
15447 ? 'B'
15448 : (STRINGP (glyph->object)
15449 ? 'S'
15450 : '-')),
15451 glyph->pixel_width,
15452 glyph->u.cmp_id,
15453 '.',
15454 glyph->face_id,
15455 glyph->left_box_line_p,
15456 glyph->right_box_line_p);
15457 }
15458 }
15459
15460
15461 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15462 GLYPHS 0 means don't show glyph contents.
15463 GLYPHS 1 means show glyphs in short form
15464 GLYPHS > 1 means show glyphs in long form. */
15465
15466 void
15467 dump_glyph_row (row, vpos, glyphs)
15468 struct glyph_row *row;
15469 int vpos, glyphs;
15470 {
15471 if (glyphs != 1)
15472 {
15473 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15474 fprintf (stderr, "======================================================================\n");
15475
15476 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15477 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15478 vpos,
15479 MATRIX_ROW_START_CHARPOS (row),
15480 MATRIX_ROW_END_CHARPOS (row),
15481 row->used[TEXT_AREA],
15482 row->contains_overlapping_glyphs_p,
15483 row->enabled_p,
15484 row->truncated_on_left_p,
15485 row->truncated_on_right_p,
15486 row->continued_p,
15487 MATRIX_ROW_CONTINUATION_LINE_P (row),
15488 row->displays_text_p,
15489 row->ends_at_zv_p,
15490 row->fill_line_p,
15491 row->ends_in_middle_of_char_p,
15492 row->starts_in_middle_of_char_p,
15493 row->mouse_face_p,
15494 row->x,
15495 row->y,
15496 row->pixel_width,
15497 row->height,
15498 row->visible_height,
15499 row->ascent,
15500 row->phys_ascent);
15501 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15502 row->end.overlay_string_index,
15503 row->continuation_lines_width);
15504 fprintf (stderr, "%9d %5d\n",
15505 CHARPOS (row->start.string_pos),
15506 CHARPOS (row->end.string_pos));
15507 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15508 row->end.dpvec_index);
15509 }
15510
15511 if (glyphs > 1)
15512 {
15513 int area;
15514
15515 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15516 {
15517 struct glyph *glyph = row->glyphs[area];
15518 struct glyph *glyph_end = glyph + row->used[area];
15519
15520 /* Glyph for a line end in text. */
15521 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15522 ++glyph_end;
15523
15524 if (glyph < glyph_end)
15525 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15526
15527 for (; glyph < glyph_end; ++glyph)
15528 dump_glyph (row, glyph, area);
15529 }
15530 }
15531 else if (glyphs == 1)
15532 {
15533 int area;
15534
15535 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15536 {
15537 char *s = (char *) alloca (row->used[area] + 1);
15538 int i;
15539
15540 for (i = 0; i < row->used[area]; ++i)
15541 {
15542 struct glyph *glyph = row->glyphs[area] + i;
15543 if (glyph->type == CHAR_GLYPH
15544 && glyph->u.ch < 0x80
15545 && glyph->u.ch >= ' ')
15546 s[i] = glyph->u.ch;
15547 else
15548 s[i] = '.';
15549 }
15550
15551 s[i] = '\0';
15552 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15553 }
15554 }
15555 }
15556
15557
15558 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15559 Sdump_glyph_matrix, 0, 1, "p",
15560 doc: /* Dump the current matrix of the selected window to stderr.
15561 Shows contents of glyph row structures. With non-nil
15562 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15563 glyphs in short form, otherwise show glyphs in long form. */)
15564 (glyphs)
15565 Lisp_Object glyphs;
15566 {
15567 struct window *w = XWINDOW (selected_window);
15568 struct buffer *buffer = XBUFFER (w->buffer);
15569
15570 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15571 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15572 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15573 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15574 fprintf (stderr, "=============================================\n");
15575 dump_glyph_matrix (w->current_matrix,
15576 NILP (glyphs) ? 0 : XINT (glyphs));
15577 return Qnil;
15578 }
15579
15580
15581 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15582 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15583 ()
15584 {
15585 struct frame *f = XFRAME (selected_frame);
15586 dump_glyph_matrix (f->current_matrix, 1);
15587 return Qnil;
15588 }
15589
15590
15591 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15592 doc: /* Dump glyph row ROW to stderr.
15593 GLYPH 0 means don't dump glyphs.
15594 GLYPH 1 means dump glyphs in short form.
15595 GLYPH > 1 or omitted means dump glyphs in long form. */)
15596 (row, glyphs)
15597 Lisp_Object row, glyphs;
15598 {
15599 struct glyph_matrix *matrix;
15600 int vpos;
15601
15602 CHECK_NUMBER (row);
15603 matrix = XWINDOW (selected_window)->current_matrix;
15604 vpos = XINT (row);
15605 if (vpos >= 0 && vpos < matrix->nrows)
15606 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15607 vpos,
15608 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15609 return Qnil;
15610 }
15611
15612
15613 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15614 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15615 GLYPH 0 means don't dump glyphs.
15616 GLYPH 1 means dump glyphs in short form.
15617 GLYPH > 1 or omitted means dump glyphs in long form. */)
15618 (row, glyphs)
15619 Lisp_Object row, glyphs;
15620 {
15621 struct frame *sf = SELECTED_FRAME ();
15622 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15623 int vpos;
15624
15625 CHECK_NUMBER (row);
15626 vpos = XINT (row);
15627 if (vpos >= 0 && vpos < m->nrows)
15628 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15629 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15630 return Qnil;
15631 }
15632
15633
15634 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15635 doc: /* Toggle tracing of redisplay.
15636 With ARG, turn tracing on if and only if ARG is positive. */)
15637 (arg)
15638 Lisp_Object arg;
15639 {
15640 if (NILP (arg))
15641 trace_redisplay_p = !trace_redisplay_p;
15642 else
15643 {
15644 arg = Fprefix_numeric_value (arg);
15645 trace_redisplay_p = XINT (arg) > 0;
15646 }
15647
15648 return Qnil;
15649 }
15650
15651
15652 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15653 doc: /* Like `format', but print result to stderr.
15654 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15655 (nargs, args)
15656 int nargs;
15657 Lisp_Object *args;
15658 {
15659 Lisp_Object s = Fformat (nargs, args);
15660 fprintf (stderr, "%s", SDATA (s));
15661 return Qnil;
15662 }
15663
15664 #endif /* GLYPH_DEBUG */
15665
15666
15667 \f
15668 /***********************************************************************
15669 Building Desired Matrix Rows
15670 ***********************************************************************/
15671
15672 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15673 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15674
15675 static struct glyph_row *
15676 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15677 struct window *w;
15678 Lisp_Object overlay_arrow_string;
15679 {
15680 struct frame *f = XFRAME (WINDOW_FRAME (w));
15681 struct buffer *buffer = XBUFFER (w->buffer);
15682 struct buffer *old = current_buffer;
15683 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15684 int arrow_len = SCHARS (overlay_arrow_string);
15685 const unsigned char *arrow_end = arrow_string + arrow_len;
15686 const unsigned char *p;
15687 struct it it;
15688 int multibyte_p;
15689 int n_glyphs_before;
15690
15691 set_buffer_temp (buffer);
15692 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15693 it.glyph_row->used[TEXT_AREA] = 0;
15694 SET_TEXT_POS (it.position, 0, 0);
15695
15696 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15697 p = arrow_string;
15698 while (p < arrow_end)
15699 {
15700 Lisp_Object face, ilisp;
15701
15702 /* Get the next character. */
15703 if (multibyte_p)
15704 it.c = string_char_and_length (p, arrow_len, &it.len);
15705 else
15706 it.c = *p, it.len = 1;
15707 p += it.len;
15708
15709 /* Get its face. */
15710 ilisp = make_number (p - arrow_string);
15711 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15712 it.face_id = compute_char_face (f, it.c, face);
15713
15714 /* Compute its width, get its glyphs. */
15715 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15716 SET_TEXT_POS (it.position, -1, -1);
15717 PRODUCE_GLYPHS (&it);
15718
15719 /* If this character doesn't fit any more in the line, we have
15720 to remove some glyphs. */
15721 if (it.current_x > it.last_visible_x)
15722 {
15723 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15724 break;
15725 }
15726 }
15727
15728 set_buffer_temp (old);
15729 return it.glyph_row;
15730 }
15731
15732
15733 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15734 glyphs are only inserted for terminal frames since we can't really
15735 win with truncation glyphs when partially visible glyphs are
15736 involved. Which glyphs to insert is determined by
15737 produce_special_glyphs. */
15738
15739 static void
15740 insert_left_trunc_glyphs (it)
15741 struct it *it;
15742 {
15743 struct it truncate_it;
15744 struct glyph *from, *end, *to, *toend;
15745
15746 xassert (!FRAME_WINDOW_P (it->f));
15747
15748 /* Get the truncation glyphs. */
15749 truncate_it = *it;
15750 truncate_it.current_x = 0;
15751 truncate_it.face_id = DEFAULT_FACE_ID;
15752 truncate_it.glyph_row = &scratch_glyph_row;
15753 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15754 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15755 truncate_it.object = make_number (0);
15756 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15757
15758 /* Overwrite glyphs from IT with truncation glyphs. */
15759 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15760 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15761 to = it->glyph_row->glyphs[TEXT_AREA];
15762 toend = to + it->glyph_row->used[TEXT_AREA];
15763
15764 while (from < end)
15765 *to++ = *from++;
15766
15767 /* There may be padding glyphs left over. Overwrite them too. */
15768 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15769 {
15770 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15771 while (from < end)
15772 *to++ = *from++;
15773 }
15774
15775 if (to > toend)
15776 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15777 }
15778
15779
15780 /* Compute the pixel height and width of IT->glyph_row.
15781
15782 Most of the time, ascent and height of a display line will be equal
15783 to the max_ascent and max_height values of the display iterator
15784 structure. This is not the case if
15785
15786 1. We hit ZV without displaying anything. In this case, max_ascent
15787 and max_height will be zero.
15788
15789 2. We have some glyphs that don't contribute to the line height.
15790 (The glyph row flag contributes_to_line_height_p is for future
15791 pixmap extensions).
15792
15793 The first case is easily covered by using default values because in
15794 these cases, the line height does not really matter, except that it
15795 must not be zero. */
15796
15797 static void
15798 compute_line_metrics (it)
15799 struct it *it;
15800 {
15801 struct glyph_row *row = it->glyph_row;
15802 int area, i;
15803
15804 if (FRAME_WINDOW_P (it->f))
15805 {
15806 int i, min_y, max_y;
15807
15808 /* The line may consist of one space only, that was added to
15809 place the cursor on it. If so, the row's height hasn't been
15810 computed yet. */
15811 if (row->height == 0)
15812 {
15813 if (it->max_ascent + it->max_descent == 0)
15814 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15815 row->ascent = it->max_ascent;
15816 row->height = it->max_ascent + it->max_descent;
15817 row->phys_ascent = it->max_phys_ascent;
15818 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15819 row->extra_line_spacing = it->max_extra_line_spacing;
15820 }
15821
15822 /* Compute the width of this line. */
15823 row->pixel_width = row->x;
15824 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15825 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15826
15827 xassert (row->pixel_width >= 0);
15828 xassert (row->ascent >= 0 && row->height > 0);
15829
15830 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15831 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15832
15833 /* If first line's physical ascent is larger than its logical
15834 ascent, use the physical ascent, and make the row taller.
15835 This makes accented characters fully visible. */
15836 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15837 && row->phys_ascent > row->ascent)
15838 {
15839 row->height += row->phys_ascent - row->ascent;
15840 row->ascent = row->phys_ascent;
15841 }
15842
15843 /* Compute how much of the line is visible. */
15844 row->visible_height = row->height;
15845
15846 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15847 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15848
15849 if (row->y < min_y)
15850 row->visible_height -= min_y - row->y;
15851 if (row->y + row->height > max_y)
15852 row->visible_height -= row->y + row->height - max_y;
15853 }
15854 else
15855 {
15856 row->pixel_width = row->used[TEXT_AREA];
15857 if (row->continued_p)
15858 row->pixel_width -= it->continuation_pixel_width;
15859 else if (row->truncated_on_right_p)
15860 row->pixel_width -= it->truncation_pixel_width;
15861 row->ascent = row->phys_ascent = 0;
15862 row->height = row->phys_height = row->visible_height = 1;
15863 row->extra_line_spacing = 0;
15864 }
15865
15866 /* Compute a hash code for this row. */
15867 row->hash = 0;
15868 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15869 for (i = 0; i < row->used[area]; ++i)
15870 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15871 + row->glyphs[area][i].u.val
15872 + row->glyphs[area][i].face_id
15873 + row->glyphs[area][i].padding_p
15874 + (row->glyphs[area][i].type << 2));
15875
15876 it->max_ascent = it->max_descent = 0;
15877 it->max_phys_ascent = it->max_phys_descent = 0;
15878 }
15879
15880
15881 /* Append one space to the glyph row of iterator IT if doing a
15882 window-based redisplay. The space has the same face as
15883 IT->face_id. Value is non-zero if a space was added.
15884
15885 This function is called to make sure that there is always one glyph
15886 at the end of a glyph row that the cursor can be set on under
15887 window-systems. (If there weren't such a glyph we would not know
15888 how wide and tall a box cursor should be displayed).
15889
15890 At the same time this space let's a nicely handle clearing to the
15891 end of the line if the row ends in italic text. */
15892
15893 static int
15894 append_space_for_newline (it, default_face_p)
15895 struct it *it;
15896 int default_face_p;
15897 {
15898 if (FRAME_WINDOW_P (it->f))
15899 {
15900 int n = it->glyph_row->used[TEXT_AREA];
15901
15902 if (it->glyph_row->glyphs[TEXT_AREA] + n
15903 < it->glyph_row->glyphs[1 + TEXT_AREA])
15904 {
15905 /* Save some values that must not be changed.
15906 Must save IT->c and IT->len because otherwise
15907 ITERATOR_AT_END_P wouldn't work anymore after
15908 append_space_for_newline has been called. */
15909 enum display_element_type saved_what = it->what;
15910 int saved_c = it->c, saved_len = it->len;
15911 int saved_x = it->current_x;
15912 int saved_face_id = it->face_id;
15913 struct text_pos saved_pos;
15914 Lisp_Object saved_object;
15915 struct face *face;
15916
15917 saved_object = it->object;
15918 saved_pos = it->position;
15919
15920 it->what = IT_CHARACTER;
15921 bzero (&it->position, sizeof it->position);
15922 it->object = make_number (0);
15923 it->c = ' ';
15924 it->len = 1;
15925
15926 if (default_face_p)
15927 it->face_id = DEFAULT_FACE_ID;
15928 else if (it->face_before_selective_p)
15929 it->face_id = it->saved_face_id;
15930 face = FACE_FROM_ID (it->f, it->face_id);
15931 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15932
15933 PRODUCE_GLYPHS (it);
15934
15935 it->override_ascent = -1;
15936 it->constrain_row_ascent_descent_p = 0;
15937 it->current_x = saved_x;
15938 it->object = saved_object;
15939 it->position = saved_pos;
15940 it->what = saved_what;
15941 it->face_id = saved_face_id;
15942 it->len = saved_len;
15943 it->c = saved_c;
15944 return 1;
15945 }
15946 }
15947
15948 return 0;
15949 }
15950
15951
15952 /* Extend the face of the last glyph in the text area of IT->glyph_row
15953 to the end of the display line. Called from display_line.
15954 If the glyph row is empty, add a space glyph to it so that we
15955 know the face to draw. Set the glyph row flag fill_line_p. */
15956
15957 static void
15958 extend_face_to_end_of_line (it)
15959 struct it *it;
15960 {
15961 struct face *face;
15962 struct frame *f = it->f;
15963
15964 /* If line is already filled, do nothing. */
15965 if (it->current_x >= it->last_visible_x)
15966 return;
15967
15968 /* Face extension extends the background and box of IT->face_id
15969 to the end of the line. If the background equals the background
15970 of the frame, we don't have to do anything. */
15971 if (it->face_before_selective_p)
15972 face = FACE_FROM_ID (it->f, it->saved_face_id);
15973 else
15974 face = FACE_FROM_ID (f, it->face_id);
15975
15976 if (FRAME_WINDOW_P (f)
15977 && it->glyph_row->displays_text_p
15978 && face->box == FACE_NO_BOX
15979 && face->background == FRAME_BACKGROUND_PIXEL (f)
15980 && !face->stipple)
15981 return;
15982
15983 /* Set the glyph row flag indicating that the face of the last glyph
15984 in the text area has to be drawn to the end of the text area. */
15985 it->glyph_row->fill_line_p = 1;
15986
15987 /* If current character of IT is not ASCII, make sure we have the
15988 ASCII face. This will be automatically undone the next time
15989 get_next_display_element returns a multibyte character. Note
15990 that the character will always be single byte in unibyte text. */
15991 if (!ASCII_CHAR_P (it->c))
15992 {
15993 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15994 }
15995
15996 if (FRAME_WINDOW_P (f))
15997 {
15998 /* If the row is empty, add a space with the current face of IT,
15999 so that we know which face to draw. */
16000 if (it->glyph_row->used[TEXT_AREA] == 0)
16001 {
16002 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16003 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16004 it->glyph_row->used[TEXT_AREA] = 1;
16005 }
16006 }
16007 else
16008 {
16009 /* Save some values that must not be changed. */
16010 int saved_x = it->current_x;
16011 struct text_pos saved_pos;
16012 Lisp_Object saved_object;
16013 enum display_element_type saved_what = it->what;
16014 int saved_face_id = it->face_id;
16015
16016 saved_object = it->object;
16017 saved_pos = it->position;
16018
16019 it->what = IT_CHARACTER;
16020 bzero (&it->position, sizeof it->position);
16021 it->object = make_number (0);
16022 it->c = ' ';
16023 it->len = 1;
16024 it->face_id = face->id;
16025
16026 PRODUCE_GLYPHS (it);
16027
16028 while (it->current_x <= it->last_visible_x)
16029 PRODUCE_GLYPHS (it);
16030
16031 /* Don't count these blanks really. It would let us insert a left
16032 truncation glyph below and make us set the cursor on them, maybe. */
16033 it->current_x = saved_x;
16034 it->object = saved_object;
16035 it->position = saved_pos;
16036 it->what = saved_what;
16037 it->face_id = saved_face_id;
16038 }
16039 }
16040
16041
16042 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16043 trailing whitespace. */
16044
16045 static int
16046 trailing_whitespace_p (charpos)
16047 int charpos;
16048 {
16049 int bytepos = CHAR_TO_BYTE (charpos);
16050 int c = 0;
16051
16052 while (bytepos < ZV_BYTE
16053 && (c = FETCH_CHAR (bytepos),
16054 c == ' ' || c == '\t'))
16055 ++bytepos;
16056
16057 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16058 {
16059 if (bytepos != PT_BYTE)
16060 return 1;
16061 }
16062 return 0;
16063 }
16064
16065
16066 /* Highlight trailing whitespace, if any, in ROW. */
16067
16068 void
16069 highlight_trailing_whitespace (f, row)
16070 struct frame *f;
16071 struct glyph_row *row;
16072 {
16073 int used = row->used[TEXT_AREA];
16074
16075 if (used)
16076 {
16077 struct glyph *start = row->glyphs[TEXT_AREA];
16078 struct glyph *glyph = start + used - 1;
16079
16080 /* Skip over glyphs inserted to display the cursor at the
16081 end of a line, for extending the face of the last glyph
16082 to the end of the line on terminals, and for truncation
16083 and continuation glyphs. */
16084 while (glyph >= start
16085 && glyph->type == CHAR_GLYPH
16086 && INTEGERP (glyph->object))
16087 --glyph;
16088
16089 /* If last glyph is a space or stretch, and it's trailing
16090 whitespace, set the face of all trailing whitespace glyphs in
16091 IT->glyph_row to `trailing-whitespace'. */
16092 if (glyph >= start
16093 && BUFFERP (glyph->object)
16094 && (glyph->type == STRETCH_GLYPH
16095 || (glyph->type == CHAR_GLYPH
16096 && glyph->u.ch == ' '))
16097 && trailing_whitespace_p (glyph->charpos))
16098 {
16099 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16100 if (face_id < 0)
16101 return;
16102
16103 while (glyph >= start
16104 && BUFFERP (glyph->object)
16105 && (glyph->type == STRETCH_GLYPH
16106 || (glyph->type == CHAR_GLYPH
16107 && glyph->u.ch == ' ')))
16108 (glyph--)->face_id = face_id;
16109 }
16110 }
16111 }
16112
16113
16114 /* Value is non-zero if glyph row ROW in window W should be
16115 used to hold the cursor. */
16116
16117 static int
16118 cursor_row_p (w, row)
16119 struct window *w;
16120 struct glyph_row *row;
16121 {
16122 int cursor_row_p = 1;
16123
16124 if (PT == MATRIX_ROW_END_CHARPOS (row))
16125 {
16126 /* Suppose the row ends on a string.
16127 Unless the row is continued, that means it ends on a newline
16128 in the string. If it's anything other than a display string
16129 (e.g. a before-string from an overlay), we don't want the
16130 cursor there. (This heuristic seems to give the optimal
16131 behavior for the various types of multi-line strings.) */
16132 if (CHARPOS (row->end.string_pos) >= 0)
16133 {
16134 if (row->continued_p)
16135 cursor_row_p = 1;
16136 else
16137 {
16138 /* Check for `display' property. */
16139 struct glyph *beg = row->glyphs[TEXT_AREA];
16140 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16141 struct glyph *glyph;
16142
16143 cursor_row_p = 0;
16144 for (glyph = end; glyph >= beg; --glyph)
16145 if (STRINGP (glyph->object))
16146 {
16147 Lisp_Object prop
16148 = Fget_char_property (make_number (PT),
16149 Qdisplay, Qnil);
16150 cursor_row_p =
16151 (!NILP (prop)
16152 && display_prop_string_p (prop, glyph->object));
16153 break;
16154 }
16155 }
16156 }
16157 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16158 {
16159 /* If the row ends in middle of a real character,
16160 and the line is continued, we want the cursor here.
16161 That's because MATRIX_ROW_END_CHARPOS would equal
16162 PT if PT is before the character. */
16163 if (!row->ends_in_ellipsis_p)
16164 cursor_row_p = row->continued_p;
16165 else
16166 /* If the row ends in an ellipsis, then
16167 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16168 We want that position to be displayed after the ellipsis. */
16169 cursor_row_p = 0;
16170 }
16171 /* If the row ends at ZV, display the cursor at the end of that
16172 row instead of at the start of the row below. */
16173 else if (row->ends_at_zv_p)
16174 cursor_row_p = 1;
16175 else
16176 cursor_row_p = 0;
16177 }
16178
16179 return cursor_row_p;
16180 }
16181
16182
16183 /* Construct the glyph row IT->glyph_row in the desired matrix of
16184 IT->w from text at the current position of IT. See dispextern.h
16185 for an overview of struct it. Value is non-zero if
16186 IT->glyph_row displays text, as opposed to a line displaying ZV
16187 only. */
16188
16189 static int
16190 display_line (it)
16191 struct it *it;
16192 {
16193 struct glyph_row *row = it->glyph_row;
16194 Lisp_Object overlay_arrow_string;
16195
16196 /* We always start displaying at hpos zero even if hscrolled. */
16197 xassert (it->hpos == 0 && it->current_x == 0);
16198
16199 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16200 >= it->w->desired_matrix->nrows)
16201 {
16202 it->w->nrows_scale_factor++;
16203 fonts_changed_p = 1;
16204 return 0;
16205 }
16206
16207 /* Is IT->w showing the region? */
16208 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16209
16210 /* Clear the result glyph row and enable it. */
16211 prepare_desired_row (row);
16212
16213 row->y = it->current_y;
16214 row->start = it->start;
16215 row->continuation_lines_width = it->continuation_lines_width;
16216 row->displays_text_p = 1;
16217 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16218 it->starts_in_middle_of_char_p = 0;
16219
16220 /* Arrange the overlays nicely for our purposes. Usually, we call
16221 display_line on only one line at a time, in which case this
16222 can't really hurt too much, or we call it on lines which appear
16223 one after another in the buffer, in which case all calls to
16224 recenter_overlay_lists but the first will be pretty cheap. */
16225 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16226
16227 /* Move over display elements that are not visible because we are
16228 hscrolled. This may stop at an x-position < IT->first_visible_x
16229 if the first glyph is partially visible or if we hit a line end. */
16230 if (it->current_x < it->first_visible_x)
16231 {
16232 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16233 MOVE_TO_POS | MOVE_TO_X);
16234 }
16235
16236 /* Get the initial row height. This is either the height of the
16237 text hscrolled, if there is any, or zero. */
16238 row->ascent = it->max_ascent;
16239 row->height = it->max_ascent + it->max_descent;
16240 row->phys_ascent = it->max_phys_ascent;
16241 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16242 row->extra_line_spacing = it->max_extra_line_spacing;
16243
16244 /* Loop generating characters. The loop is left with IT on the next
16245 character to display. */
16246 while (1)
16247 {
16248 int n_glyphs_before, hpos_before, x_before;
16249 int x, i, nglyphs;
16250 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16251
16252 /* Retrieve the next thing to display. Value is zero if end of
16253 buffer reached. */
16254 if (!get_next_display_element (it))
16255 {
16256 /* Maybe add a space at the end of this line that is used to
16257 display the cursor there under X. Set the charpos of the
16258 first glyph of blank lines not corresponding to any text
16259 to -1. */
16260 #ifdef HAVE_WINDOW_SYSTEM
16261 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16262 row->exact_window_width_line_p = 1;
16263 else
16264 #endif /* HAVE_WINDOW_SYSTEM */
16265 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16266 || row->used[TEXT_AREA] == 0)
16267 {
16268 row->glyphs[TEXT_AREA]->charpos = -1;
16269 row->displays_text_p = 0;
16270
16271 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16272 && (!MINI_WINDOW_P (it->w)
16273 || (minibuf_level && EQ (it->window, minibuf_window))))
16274 row->indicate_empty_line_p = 1;
16275 }
16276
16277 it->continuation_lines_width = 0;
16278 row->ends_at_zv_p = 1;
16279 break;
16280 }
16281
16282 /* Now, get the metrics of what we want to display. This also
16283 generates glyphs in `row' (which is IT->glyph_row). */
16284 n_glyphs_before = row->used[TEXT_AREA];
16285 x = it->current_x;
16286
16287 /* Remember the line height so far in case the next element doesn't
16288 fit on the line. */
16289 if (!it->truncate_lines_p)
16290 {
16291 ascent = it->max_ascent;
16292 descent = it->max_descent;
16293 phys_ascent = it->max_phys_ascent;
16294 phys_descent = it->max_phys_descent;
16295 }
16296
16297 PRODUCE_GLYPHS (it);
16298
16299 /* If this display element was in marginal areas, continue with
16300 the next one. */
16301 if (it->area != TEXT_AREA)
16302 {
16303 row->ascent = max (row->ascent, it->max_ascent);
16304 row->height = max (row->height, it->max_ascent + it->max_descent);
16305 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16306 row->phys_height = max (row->phys_height,
16307 it->max_phys_ascent + it->max_phys_descent);
16308 row->extra_line_spacing = max (row->extra_line_spacing,
16309 it->max_extra_line_spacing);
16310 set_iterator_to_next (it, 1);
16311 continue;
16312 }
16313
16314 /* Does the display element fit on the line? If we truncate
16315 lines, we should draw past the right edge of the window. If
16316 we don't truncate, we want to stop so that we can display the
16317 continuation glyph before the right margin. If lines are
16318 continued, there are two possible strategies for characters
16319 resulting in more than 1 glyph (e.g. tabs): Display as many
16320 glyphs as possible in this line and leave the rest for the
16321 continuation line, or display the whole element in the next
16322 line. Original redisplay did the former, so we do it also. */
16323 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16324 hpos_before = it->hpos;
16325 x_before = x;
16326
16327 if (/* Not a newline. */
16328 nglyphs > 0
16329 /* Glyphs produced fit entirely in the line. */
16330 && it->current_x < it->last_visible_x)
16331 {
16332 it->hpos += nglyphs;
16333 row->ascent = max (row->ascent, it->max_ascent);
16334 row->height = max (row->height, it->max_ascent + it->max_descent);
16335 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16336 row->phys_height = max (row->phys_height,
16337 it->max_phys_ascent + it->max_phys_descent);
16338 row->extra_line_spacing = max (row->extra_line_spacing,
16339 it->max_extra_line_spacing);
16340 if (it->current_x - it->pixel_width < it->first_visible_x)
16341 row->x = x - it->first_visible_x;
16342 }
16343 else
16344 {
16345 int new_x;
16346 struct glyph *glyph;
16347
16348 for (i = 0; i < nglyphs; ++i, x = new_x)
16349 {
16350 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16351 new_x = x + glyph->pixel_width;
16352
16353 if (/* Lines are continued. */
16354 !it->truncate_lines_p
16355 && (/* Glyph doesn't fit on the line. */
16356 new_x > it->last_visible_x
16357 /* Or it fits exactly on a window system frame. */
16358 || (new_x == it->last_visible_x
16359 && FRAME_WINDOW_P (it->f))))
16360 {
16361 /* End of a continued line. */
16362
16363 if (it->hpos == 0
16364 || (new_x == it->last_visible_x
16365 && FRAME_WINDOW_P (it->f)))
16366 {
16367 /* Current glyph is the only one on the line or
16368 fits exactly on the line. We must continue
16369 the line because we can't draw the cursor
16370 after the glyph. */
16371 row->continued_p = 1;
16372 it->current_x = new_x;
16373 it->continuation_lines_width += new_x;
16374 ++it->hpos;
16375 if (i == nglyphs - 1)
16376 {
16377 set_iterator_to_next (it, 1);
16378 #ifdef HAVE_WINDOW_SYSTEM
16379 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16380 {
16381 if (!get_next_display_element (it))
16382 {
16383 row->exact_window_width_line_p = 1;
16384 it->continuation_lines_width = 0;
16385 row->continued_p = 0;
16386 row->ends_at_zv_p = 1;
16387 }
16388 else if (ITERATOR_AT_END_OF_LINE_P (it))
16389 {
16390 row->continued_p = 0;
16391 row->exact_window_width_line_p = 1;
16392 }
16393 }
16394 #endif /* HAVE_WINDOW_SYSTEM */
16395 }
16396 }
16397 else if (CHAR_GLYPH_PADDING_P (*glyph)
16398 && !FRAME_WINDOW_P (it->f))
16399 {
16400 /* A padding glyph that doesn't fit on this line.
16401 This means the whole character doesn't fit
16402 on the line. */
16403 row->used[TEXT_AREA] = n_glyphs_before;
16404
16405 /* Fill the rest of the row with continuation
16406 glyphs like in 20.x. */
16407 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16408 < row->glyphs[1 + TEXT_AREA])
16409 produce_special_glyphs (it, IT_CONTINUATION);
16410
16411 row->continued_p = 1;
16412 it->current_x = x_before;
16413 it->continuation_lines_width += x_before;
16414
16415 /* Restore the height to what it was before the
16416 element not fitting on the line. */
16417 it->max_ascent = ascent;
16418 it->max_descent = descent;
16419 it->max_phys_ascent = phys_ascent;
16420 it->max_phys_descent = phys_descent;
16421 }
16422 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16423 {
16424 /* A TAB that extends past the right edge of the
16425 window. This produces a single glyph on
16426 window system frames. We leave the glyph in
16427 this row and let it fill the row, but don't
16428 consume the TAB. */
16429 it->continuation_lines_width += it->last_visible_x;
16430 row->ends_in_middle_of_char_p = 1;
16431 row->continued_p = 1;
16432 glyph->pixel_width = it->last_visible_x - x;
16433 it->starts_in_middle_of_char_p = 1;
16434 }
16435 else
16436 {
16437 /* Something other than a TAB that draws past
16438 the right edge of the window. Restore
16439 positions to values before the element. */
16440 row->used[TEXT_AREA] = n_glyphs_before + i;
16441
16442 /* Display continuation glyphs. */
16443 if (!FRAME_WINDOW_P (it->f))
16444 produce_special_glyphs (it, IT_CONTINUATION);
16445 row->continued_p = 1;
16446
16447 it->current_x = x_before;
16448 it->continuation_lines_width += x;
16449 extend_face_to_end_of_line (it);
16450
16451 if (nglyphs > 1 && i > 0)
16452 {
16453 row->ends_in_middle_of_char_p = 1;
16454 it->starts_in_middle_of_char_p = 1;
16455 }
16456
16457 /* Restore the height to what it was before the
16458 element not fitting on the line. */
16459 it->max_ascent = ascent;
16460 it->max_descent = descent;
16461 it->max_phys_ascent = phys_ascent;
16462 it->max_phys_descent = phys_descent;
16463 }
16464
16465 break;
16466 }
16467 else if (new_x > it->first_visible_x)
16468 {
16469 /* Increment number of glyphs actually displayed. */
16470 ++it->hpos;
16471
16472 if (x < it->first_visible_x)
16473 /* Glyph is partially visible, i.e. row starts at
16474 negative X position. */
16475 row->x = x - it->first_visible_x;
16476 }
16477 else
16478 {
16479 /* Glyph is completely off the left margin of the
16480 window. This should not happen because of the
16481 move_it_in_display_line at the start of this
16482 function, unless the text display area of the
16483 window is empty. */
16484 xassert (it->first_visible_x <= it->last_visible_x);
16485 }
16486 }
16487
16488 row->ascent = max (row->ascent, it->max_ascent);
16489 row->height = max (row->height, it->max_ascent + it->max_descent);
16490 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16491 row->phys_height = max (row->phys_height,
16492 it->max_phys_ascent + it->max_phys_descent);
16493 row->extra_line_spacing = max (row->extra_line_spacing,
16494 it->max_extra_line_spacing);
16495
16496 /* End of this display line if row is continued. */
16497 if (row->continued_p || row->ends_at_zv_p)
16498 break;
16499 }
16500
16501 at_end_of_line:
16502 /* Is this a line end? If yes, we're also done, after making
16503 sure that a non-default face is extended up to the right
16504 margin of the window. */
16505 if (ITERATOR_AT_END_OF_LINE_P (it))
16506 {
16507 int used_before = row->used[TEXT_AREA];
16508
16509 row->ends_in_newline_from_string_p = STRINGP (it->object);
16510
16511 #ifdef HAVE_WINDOW_SYSTEM
16512 /* Add a space at the end of the line that is used to
16513 display the cursor there. */
16514 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16515 append_space_for_newline (it, 0);
16516 #endif /* HAVE_WINDOW_SYSTEM */
16517
16518 /* Extend the face to the end of the line. */
16519 extend_face_to_end_of_line (it);
16520
16521 /* Make sure we have the position. */
16522 if (used_before == 0)
16523 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16524
16525 /* Consume the line end. This skips over invisible lines. */
16526 set_iterator_to_next (it, 1);
16527 it->continuation_lines_width = 0;
16528 break;
16529 }
16530
16531 /* Proceed with next display element. Note that this skips
16532 over lines invisible because of selective display. */
16533 set_iterator_to_next (it, 1);
16534
16535 /* If we truncate lines, we are done when the last displayed
16536 glyphs reach past the right margin of the window. */
16537 if (it->truncate_lines_p
16538 && (FRAME_WINDOW_P (it->f)
16539 ? (it->current_x >= it->last_visible_x)
16540 : (it->current_x > it->last_visible_x)))
16541 {
16542 /* Maybe add truncation glyphs. */
16543 if (!FRAME_WINDOW_P (it->f))
16544 {
16545 int i, n;
16546
16547 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16548 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16549 break;
16550
16551 for (n = row->used[TEXT_AREA]; i < n; ++i)
16552 {
16553 row->used[TEXT_AREA] = i;
16554 produce_special_glyphs (it, IT_TRUNCATION);
16555 }
16556 }
16557 #ifdef HAVE_WINDOW_SYSTEM
16558 else
16559 {
16560 /* Don't truncate if we can overflow newline into fringe. */
16561 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16562 {
16563 if (!get_next_display_element (it))
16564 {
16565 it->continuation_lines_width = 0;
16566 row->ends_at_zv_p = 1;
16567 row->exact_window_width_line_p = 1;
16568 break;
16569 }
16570 if (ITERATOR_AT_END_OF_LINE_P (it))
16571 {
16572 row->exact_window_width_line_p = 1;
16573 goto at_end_of_line;
16574 }
16575 }
16576 }
16577 #endif /* HAVE_WINDOW_SYSTEM */
16578
16579 row->truncated_on_right_p = 1;
16580 it->continuation_lines_width = 0;
16581 reseat_at_next_visible_line_start (it, 0);
16582 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16583 it->hpos = hpos_before;
16584 it->current_x = x_before;
16585 break;
16586 }
16587 }
16588
16589 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16590 at the left window margin. */
16591 if (it->first_visible_x
16592 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16593 {
16594 if (!FRAME_WINDOW_P (it->f))
16595 insert_left_trunc_glyphs (it);
16596 row->truncated_on_left_p = 1;
16597 }
16598
16599 /* If the start of this line is the overlay arrow-position, then
16600 mark this glyph row as the one containing the overlay arrow.
16601 This is clearly a mess with variable size fonts. It would be
16602 better to let it be displayed like cursors under X. */
16603 if ((row->displays_text_p || !overlay_arrow_seen)
16604 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16605 !NILP (overlay_arrow_string)))
16606 {
16607 /* Overlay arrow in window redisplay is a fringe bitmap. */
16608 if (STRINGP (overlay_arrow_string))
16609 {
16610 struct glyph_row *arrow_row
16611 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16612 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16613 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16614 struct glyph *p = row->glyphs[TEXT_AREA];
16615 struct glyph *p2, *end;
16616
16617 /* Copy the arrow glyphs. */
16618 while (glyph < arrow_end)
16619 *p++ = *glyph++;
16620
16621 /* Throw away padding glyphs. */
16622 p2 = p;
16623 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16624 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16625 ++p2;
16626 if (p2 > p)
16627 {
16628 while (p2 < end)
16629 *p++ = *p2++;
16630 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16631 }
16632 }
16633 else
16634 {
16635 xassert (INTEGERP (overlay_arrow_string));
16636 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16637 }
16638 overlay_arrow_seen = 1;
16639 }
16640
16641 /* Compute pixel dimensions of this line. */
16642 compute_line_metrics (it);
16643
16644 /* Remember the position at which this line ends. */
16645 row->end = it->current;
16646
16647 /* Record whether this row ends inside an ellipsis. */
16648 row->ends_in_ellipsis_p
16649 = (it->method == GET_FROM_DISPLAY_VECTOR
16650 && it->ellipsis_p);
16651
16652 /* Save fringe bitmaps in this row. */
16653 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16654 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16655 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16656 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16657
16658 it->left_user_fringe_bitmap = 0;
16659 it->left_user_fringe_face_id = 0;
16660 it->right_user_fringe_bitmap = 0;
16661 it->right_user_fringe_face_id = 0;
16662
16663 /* Maybe set the cursor. */
16664 if (it->w->cursor.vpos < 0
16665 && PT >= MATRIX_ROW_START_CHARPOS (row)
16666 && PT <= MATRIX_ROW_END_CHARPOS (row)
16667 && cursor_row_p (it->w, row))
16668 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16669
16670 /* Highlight trailing whitespace. */
16671 if (!NILP (Vshow_trailing_whitespace))
16672 highlight_trailing_whitespace (it->f, it->glyph_row);
16673
16674 /* Prepare for the next line. This line starts horizontally at (X
16675 HPOS) = (0 0). Vertical positions are incremented. As a
16676 convenience for the caller, IT->glyph_row is set to the next
16677 row to be used. */
16678 it->current_x = it->hpos = 0;
16679 it->current_y += row->height;
16680 ++it->vpos;
16681 ++it->glyph_row;
16682 it->start = it->current;
16683 return row->displays_text_p;
16684 }
16685
16686
16687 \f
16688 /***********************************************************************
16689 Menu Bar
16690 ***********************************************************************/
16691
16692 /* Redisplay the menu bar in the frame for window W.
16693
16694 The menu bar of X frames that don't have X toolkit support is
16695 displayed in a special window W->frame->menu_bar_window.
16696
16697 The menu bar of terminal frames is treated specially as far as
16698 glyph matrices are concerned. Menu bar lines are not part of
16699 windows, so the update is done directly on the frame matrix rows
16700 for the menu bar. */
16701
16702 static void
16703 display_menu_bar (w)
16704 struct window *w;
16705 {
16706 struct frame *f = XFRAME (WINDOW_FRAME (w));
16707 struct it it;
16708 Lisp_Object items;
16709 int i;
16710
16711 /* Don't do all this for graphical frames. */
16712 #ifdef HAVE_NTGUI
16713 if (FRAME_W32_P (f))
16714 return;
16715 #endif
16716 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16717 if (FRAME_X_P (f))
16718 return;
16719 #endif
16720 #ifdef MAC_OS
16721 if (FRAME_MAC_P (f))
16722 return;
16723 #endif
16724
16725 #ifdef USE_X_TOOLKIT
16726 xassert (!FRAME_WINDOW_P (f));
16727 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16728 it.first_visible_x = 0;
16729 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16730 #else /* not USE_X_TOOLKIT */
16731 if (FRAME_WINDOW_P (f))
16732 {
16733 /* Menu bar lines are displayed in the desired matrix of the
16734 dummy window menu_bar_window. */
16735 struct window *menu_w;
16736 xassert (WINDOWP (f->menu_bar_window));
16737 menu_w = XWINDOW (f->menu_bar_window);
16738 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16739 MENU_FACE_ID);
16740 it.first_visible_x = 0;
16741 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16742 }
16743 else
16744 {
16745 /* This is a TTY frame, i.e. character hpos/vpos are used as
16746 pixel x/y. */
16747 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16748 MENU_FACE_ID);
16749 it.first_visible_x = 0;
16750 it.last_visible_x = FRAME_COLS (f);
16751 }
16752 #endif /* not USE_X_TOOLKIT */
16753
16754 if (! mode_line_inverse_video)
16755 /* Force the menu-bar to be displayed in the default face. */
16756 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16757
16758 /* Clear all rows of the menu bar. */
16759 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16760 {
16761 struct glyph_row *row = it.glyph_row + i;
16762 clear_glyph_row (row);
16763 row->enabled_p = 1;
16764 row->full_width_p = 1;
16765 }
16766
16767 /* Display all items of the menu bar. */
16768 items = FRAME_MENU_BAR_ITEMS (it.f);
16769 for (i = 0; i < XVECTOR (items)->size; i += 4)
16770 {
16771 Lisp_Object string;
16772
16773 /* Stop at nil string. */
16774 string = AREF (items, i + 1);
16775 if (NILP (string))
16776 break;
16777
16778 /* Remember where item was displayed. */
16779 ASET (items, i + 3, make_number (it.hpos));
16780
16781 /* Display the item, pad with one space. */
16782 if (it.current_x < it.last_visible_x)
16783 display_string (NULL, string, Qnil, 0, 0, &it,
16784 SCHARS (string) + 1, 0, 0, -1);
16785 }
16786
16787 /* Fill out the line with spaces. */
16788 if (it.current_x < it.last_visible_x)
16789 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16790
16791 /* Compute the total height of the lines. */
16792 compute_line_metrics (&it);
16793 }
16794
16795
16796 \f
16797 /***********************************************************************
16798 Mode Line
16799 ***********************************************************************/
16800
16801 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16802 FORCE is non-zero, redisplay mode lines unconditionally.
16803 Otherwise, redisplay only mode lines that are garbaged. Value is
16804 the number of windows whose mode lines were redisplayed. */
16805
16806 static int
16807 redisplay_mode_lines (window, force)
16808 Lisp_Object window;
16809 int force;
16810 {
16811 int nwindows = 0;
16812
16813 while (!NILP (window))
16814 {
16815 struct window *w = XWINDOW (window);
16816
16817 if (WINDOWP (w->hchild))
16818 nwindows += redisplay_mode_lines (w->hchild, force);
16819 else if (WINDOWP (w->vchild))
16820 nwindows += redisplay_mode_lines (w->vchild, force);
16821 else if (force
16822 || FRAME_GARBAGED_P (XFRAME (w->frame))
16823 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16824 {
16825 struct text_pos lpoint;
16826 struct buffer *old = current_buffer;
16827
16828 /* Set the window's buffer for the mode line display. */
16829 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16830 set_buffer_internal_1 (XBUFFER (w->buffer));
16831
16832 /* Point refers normally to the selected window. For any
16833 other window, set up appropriate value. */
16834 if (!EQ (window, selected_window))
16835 {
16836 struct text_pos pt;
16837
16838 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16839 if (CHARPOS (pt) < BEGV)
16840 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16841 else if (CHARPOS (pt) > (ZV - 1))
16842 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16843 else
16844 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16845 }
16846
16847 /* Display mode lines. */
16848 clear_glyph_matrix (w->desired_matrix);
16849 if (display_mode_lines (w))
16850 {
16851 ++nwindows;
16852 w->must_be_updated_p = 1;
16853 }
16854
16855 /* Restore old settings. */
16856 set_buffer_internal_1 (old);
16857 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16858 }
16859
16860 window = w->next;
16861 }
16862
16863 return nwindows;
16864 }
16865
16866
16867 /* Display the mode and/or top line of window W. Value is the number
16868 of mode lines displayed. */
16869
16870 static int
16871 display_mode_lines (w)
16872 struct window *w;
16873 {
16874 Lisp_Object old_selected_window, old_selected_frame;
16875 int n = 0;
16876
16877 old_selected_frame = selected_frame;
16878 selected_frame = w->frame;
16879 old_selected_window = selected_window;
16880 XSETWINDOW (selected_window, w);
16881
16882 /* These will be set while the mode line specs are processed. */
16883 line_number_displayed = 0;
16884 w->column_number_displayed = Qnil;
16885
16886 if (WINDOW_WANTS_MODELINE_P (w))
16887 {
16888 struct window *sel_w = XWINDOW (old_selected_window);
16889
16890 /* Select mode line face based on the real selected window. */
16891 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16892 current_buffer->mode_line_format);
16893 ++n;
16894 }
16895
16896 if (WINDOW_WANTS_HEADER_LINE_P (w))
16897 {
16898 display_mode_line (w, HEADER_LINE_FACE_ID,
16899 current_buffer->header_line_format);
16900 ++n;
16901 }
16902
16903 selected_frame = old_selected_frame;
16904 selected_window = old_selected_window;
16905 return n;
16906 }
16907
16908
16909 /* Display mode or top line of window W. FACE_ID specifies which line
16910 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16911 FORMAT is the mode line format to display. Value is the pixel
16912 height of the mode line displayed. */
16913
16914 static int
16915 display_mode_line (w, face_id, format)
16916 struct window *w;
16917 enum face_id face_id;
16918 Lisp_Object format;
16919 {
16920 struct it it;
16921 struct face *face;
16922 int count = SPECPDL_INDEX ();
16923
16924 init_iterator (&it, w, -1, -1, NULL, face_id);
16925 /* Don't extend on a previously drawn mode-line.
16926 This may happen if called from pos_visible_p. */
16927 it.glyph_row->enabled_p = 0;
16928 prepare_desired_row (it.glyph_row);
16929
16930 it.glyph_row->mode_line_p = 1;
16931
16932 if (! mode_line_inverse_video)
16933 /* Force the mode-line to be displayed in the default face. */
16934 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16935
16936 record_unwind_protect (unwind_format_mode_line,
16937 format_mode_line_unwind_data (NULL, Qnil, 0));
16938
16939 mode_line_target = MODE_LINE_DISPLAY;
16940
16941 /* Temporarily make frame's keyboard the current kboard so that
16942 kboard-local variables in the mode_line_format will get the right
16943 values. */
16944 push_kboard (FRAME_KBOARD (it.f));
16945 record_unwind_save_match_data ();
16946 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16947 pop_kboard ();
16948
16949 unbind_to (count, Qnil);
16950
16951 /* Fill up with spaces. */
16952 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16953
16954 compute_line_metrics (&it);
16955 it.glyph_row->full_width_p = 1;
16956 it.glyph_row->continued_p = 0;
16957 it.glyph_row->truncated_on_left_p = 0;
16958 it.glyph_row->truncated_on_right_p = 0;
16959
16960 /* Make a 3D mode-line have a shadow at its right end. */
16961 face = FACE_FROM_ID (it.f, face_id);
16962 extend_face_to_end_of_line (&it);
16963 if (face->box != FACE_NO_BOX)
16964 {
16965 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16966 + it.glyph_row->used[TEXT_AREA] - 1);
16967 last->right_box_line_p = 1;
16968 }
16969
16970 return it.glyph_row->height;
16971 }
16972
16973 /* Move element ELT in LIST to the front of LIST.
16974 Return the updated list. */
16975
16976 static Lisp_Object
16977 move_elt_to_front (elt, list)
16978 Lisp_Object elt, list;
16979 {
16980 register Lisp_Object tail, prev;
16981 register Lisp_Object tem;
16982
16983 tail = list;
16984 prev = Qnil;
16985 while (CONSP (tail))
16986 {
16987 tem = XCAR (tail);
16988
16989 if (EQ (elt, tem))
16990 {
16991 /* Splice out the link TAIL. */
16992 if (NILP (prev))
16993 list = XCDR (tail);
16994 else
16995 Fsetcdr (prev, XCDR (tail));
16996
16997 /* Now make it the first. */
16998 Fsetcdr (tail, list);
16999 return tail;
17000 }
17001 else
17002 prev = tail;
17003 tail = XCDR (tail);
17004 QUIT;
17005 }
17006
17007 /* Not found--return unchanged LIST. */
17008 return list;
17009 }
17010
17011 /* Contribute ELT to the mode line for window IT->w. How it
17012 translates into text depends on its data type.
17013
17014 IT describes the display environment in which we display, as usual.
17015
17016 DEPTH is the depth in recursion. It is used to prevent
17017 infinite recursion here.
17018
17019 FIELD_WIDTH is the number of characters the display of ELT should
17020 occupy in the mode line, and PRECISION is the maximum number of
17021 characters to display from ELT's representation. See
17022 display_string for details.
17023
17024 Returns the hpos of the end of the text generated by ELT.
17025
17026 PROPS is a property list to add to any string we encounter.
17027
17028 If RISKY is nonzero, remove (disregard) any properties in any string
17029 we encounter, and ignore :eval and :propertize.
17030
17031 The global variable `mode_line_target' determines whether the
17032 output is passed to `store_mode_line_noprop',
17033 `store_mode_line_string', or `display_string'. */
17034
17035 static int
17036 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17037 struct it *it;
17038 int depth;
17039 int field_width, precision;
17040 Lisp_Object elt, props;
17041 int risky;
17042 {
17043 int n = 0, field, prec;
17044 int literal = 0;
17045
17046 tail_recurse:
17047 if (depth > 100)
17048 elt = build_string ("*too-deep*");
17049
17050 depth++;
17051
17052 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17053 {
17054 case Lisp_String:
17055 {
17056 /* A string: output it and check for %-constructs within it. */
17057 unsigned char c;
17058 int offset = 0;
17059
17060 if (SCHARS (elt) > 0
17061 && (!NILP (props) || risky))
17062 {
17063 Lisp_Object oprops, aelt;
17064 oprops = Ftext_properties_at (make_number (0), elt);
17065
17066 /* If the starting string's properties are not what
17067 we want, translate the string. Also, if the string
17068 is risky, do that anyway. */
17069
17070 if (NILP (Fequal (props, oprops)) || risky)
17071 {
17072 /* If the starting string has properties,
17073 merge the specified ones onto the existing ones. */
17074 if (! NILP (oprops) && !risky)
17075 {
17076 Lisp_Object tem;
17077
17078 oprops = Fcopy_sequence (oprops);
17079 tem = props;
17080 while (CONSP (tem))
17081 {
17082 oprops = Fplist_put (oprops, XCAR (tem),
17083 XCAR (XCDR (tem)));
17084 tem = XCDR (XCDR (tem));
17085 }
17086 props = oprops;
17087 }
17088
17089 aelt = Fassoc (elt, mode_line_proptrans_alist);
17090 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17091 {
17092 /* AELT is what we want. Move it to the front
17093 without consing. */
17094 elt = XCAR (aelt);
17095 mode_line_proptrans_alist
17096 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17097 }
17098 else
17099 {
17100 Lisp_Object tem;
17101
17102 /* If AELT has the wrong props, it is useless.
17103 so get rid of it. */
17104 if (! NILP (aelt))
17105 mode_line_proptrans_alist
17106 = Fdelq (aelt, mode_line_proptrans_alist);
17107
17108 elt = Fcopy_sequence (elt);
17109 Fset_text_properties (make_number (0), Flength (elt),
17110 props, elt);
17111 /* Add this item to mode_line_proptrans_alist. */
17112 mode_line_proptrans_alist
17113 = Fcons (Fcons (elt, props),
17114 mode_line_proptrans_alist);
17115 /* Truncate mode_line_proptrans_alist
17116 to at most 50 elements. */
17117 tem = Fnthcdr (make_number (50),
17118 mode_line_proptrans_alist);
17119 if (! NILP (tem))
17120 XSETCDR (tem, Qnil);
17121 }
17122 }
17123 }
17124
17125 offset = 0;
17126
17127 if (literal)
17128 {
17129 prec = precision - n;
17130 switch (mode_line_target)
17131 {
17132 case MODE_LINE_NOPROP:
17133 case MODE_LINE_TITLE:
17134 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17135 break;
17136 case MODE_LINE_STRING:
17137 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17138 break;
17139 case MODE_LINE_DISPLAY:
17140 n += display_string (NULL, elt, Qnil, 0, 0, it,
17141 0, prec, 0, STRING_MULTIBYTE (elt));
17142 break;
17143 }
17144
17145 break;
17146 }
17147
17148 /* Handle the non-literal case. */
17149
17150 while ((precision <= 0 || n < precision)
17151 && SREF (elt, offset) != 0
17152 && (mode_line_target != MODE_LINE_DISPLAY
17153 || it->current_x < it->last_visible_x))
17154 {
17155 int last_offset = offset;
17156
17157 /* Advance to end of string or next format specifier. */
17158 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17159 ;
17160
17161 if (offset - 1 != last_offset)
17162 {
17163 int nchars, nbytes;
17164
17165 /* Output to end of string or up to '%'. Field width
17166 is length of string. Don't output more than
17167 PRECISION allows us. */
17168 offset--;
17169
17170 prec = c_string_width (SDATA (elt) + last_offset,
17171 offset - last_offset, precision - n,
17172 &nchars, &nbytes);
17173
17174 switch (mode_line_target)
17175 {
17176 case MODE_LINE_NOPROP:
17177 case MODE_LINE_TITLE:
17178 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17179 break;
17180 case MODE_LINE_STRING:
17181 {
17182 int bytepos = last_offset;
17183 int charpos = string_byte_to_char (elt, bytepos);
17184 int endpos = (precision <= 0
17185 ? string_byte_to_char (elt, offset)
17186 : charpos + nchars);
17187
17188 n += store_mode_line_string (NULL,
17189 Fsubstring (elt, make_number (charpos),
17190 make_number (endpos)),
17191 0, 0, 0, Qnil);
17192 }
17193 break;
17194 case MODE_LINE_DISPLAY:
17195 {
17196 int bytepos = last_offset;
17197 int charpos = string_byte_to_char (elt, bytepos);
17198
17199 if (precision <= 0)
17200 nchars = string_byte_to_char (elt, offset) - charpos;
17201 n += display_string (NULL, elt, Qnil, 0, charpos,
17202 it, 0, nchars, 0,
17203 STRING_MULTIBYTE (elt));
17204 }
17205 break;
17206 }
17207 }
17208 else /* c == '%' */
17209 {
17210 int percent_position = offset;
17211
17212 /* Get the specified minimum width. Zero means
17213 don't pad. */
17214 field = 0;
17215 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17216 field = field * 10 + c - '0';
17217
17218 /* Don't pad beyond the total padding allowed. */
17219 if (field_width - n > 0 && field > field_width - n)
17220 field = field_width - n;
17221
17222 /* Note that either PRECISION <= 0 or N < PRECISION. */
17223 prec = precision - n;
17224
17225 if (c == 'M')
17226 n += display_mode_element (it, depth, field, prec,
17227 Vglobal_mode_string, props,
17228 risky);
17229 else if (c != 0)
17230 {
17231 int multibyte;
17232 int bytepos, charpos;
17233 unsigned char *spec;
17234
17235 bytepos = percent_position;
17236 charpos = (STRING_MULTIBYTE (elt)
17237 ? string_byte_to_char (elt, bytepos)
17238 : bytepos);
17239 spec
17240 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17241
17242 switch (mode_line_target)
17243 {
17244 case MODE_LINE_NOPROP:
17245 case MODE_LINE_TITLE:
17246 n += store_mode_line_noprop (spec, field, prec);
17247 break;
17248 case MODE_LINE_STRING:
17249 {
17250 int len = strlen (spec);
17251 Lisp_Object tem = make_string (spec, len);
17252 props = Ftext_properties_at (make_number (charpos), elt);
17253 /* Should only keep face property in props */
17254 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17255 }
17256 break;
17257 case MODE_LINE_DISPLAY:
17258 {
17259 int nglyphs_before, nwritten;
17260
17261 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17262 nwritten = display_string (spec, Qnil, elt,
17263 charpos, 0, it,
17264 field, prec, 0,
17265 multibyte);
17266
17267 /* Assign to the glyphs written above the
17268 string where the `%x' came from, position
17269 of the `%'. */
17270 if (nwritten > 0)
17271 {
17272 struct glyph *glyph
17273 = (it->glyph_row->glyphs[TEXT_AREA]
17274 + nglyphs_before);
17275 int i;
17276
17277 for (i = 0; i < nwritten; ++i)
17278 {
17279 glyph[i].object = elt;
17280 glyph[i].charpos = charpos;
17281 }
17282
17283 n += nwritten;
17284 }
17285 }
17286 break;
17287 }
17288 }
17289 else /* c == 0 */
17290 break;
17291 }
17292 }
17293 }
17294 break;
17295
17296 case Lisp_Symbol:
17297 /* A symbol: process the value of the symbol recursively
17298 as if it appeared here directly. Avoid error if symbol void.
17299 Special case: if value of symbol is a string, output the string
17300 literally. */
17301 {
17302 register Lisp_Object tem;
17303
17304 /* If the variable is not marked as risky to set
17305 then its contents are risky to use. */
17306 if (NILP (Fget (elt, Qrisky_local_variable)))
17307 risky = 1;
17308
17309 tem = Fboundp (elt);
17310 if (!NILP (tem))
17311 {
17312 tem = Fsymbol_value (elt);
17313 /* If value is a string, output that string literally:
17314 don't check for % within it. */
17315 if (STRINGP (tem))
17316 literal = 1;
17317
17318 if (!EQ (tem, elt))
17319 {
17320 /* Give up right away for nil or t. */
17321 elt = tem;
17322 goto tail_recurse;
17323 }
17324 }
17325 }
17326 break;
17327
17328 case Lisp_Cons:
17329 {
17330 register Lisp_Object car, tem;
17331
17332 /* A cons cell: five distinct cases.
17333 If first element is :eval or :propertize, do something special.
17334 If first element is a string or a cons, process all the elements
17335 and effectively concatenate them.
17336 If first element is a negative number, truncate displaying cdr to
17337 at most that many characters. If positive, pad (with spaces)
17338 to at least that many characters.
17339 If first element is a symbol, process the cadr or caddr recursively
17340 according to whether the symbol's value is non-nil or nil. */
17341 car = XCAR (elt);
17342 if (EQ (car, QCeval))
17343 {
17344 /* An element of the form (:eval FORM) means evaluate FORM
17345 and use the result as mode line elements. */
17346
17347 if (risky)
17348 break;
17349
17350 if (CONSP (XCDR (elt)))
17351 {
17352 Lisp_Object spec;
17353 spec = safe_eval (XCAR (XCDR (elt)));
17354 n += display_mode_element (it, depth, field_width - n,
17355 precision - n, spec, props,
17356 risky);
17357 }
17358 }
17359 else if (EQ (car, QCpropertize))
17360 {
17361 /* An element of the form (:propertize ELT PROPS...)
17362 means display ELT but applying properties PROPS. */
17363
17364 if (risky)
17365 break;
17366
17367 if (CONSP (XCDR (elt)))
17368 n += display_mode_element (it, depth, field_width - n,
17369 precision - n, XCAR (XCDR (elt)),
17370 XCDR (XCDR (elt)), risky);
17371 }
17372 else if (SYMBOLP (car))
17373 {
17374 tem = Fboundp (car);
17375 elt = XCDR (elt);
17376 if (!CONSP (elt))
17377 goto invalid;
17378 /* elt is now the cdr, and we know it is a cons cell.
17379 Use its car if CAR has a non-nil value. */
17380 if (!NILP (tem))
17381 {
17382 tem = Fsymbol_value (car);
17383 if (!NILP (tem))
17384 {
17385 elt = XCAR (elt);
17386 goto tail_recurse;
17387 }
17388 }
17389 /* Symbol's value is nil (or symbol is unbound)
17390 Get the cddr of the original list
17391 and if possible find the caddr and use that. */
17392 elt = XCDR (elt);
17393 if (NILP (elt))
17394 break;
17395 else if (!CONSP (elt))
17396 goto invalid;
17397 elt = XCAR (elt);
17398 goto tail_recurse;
17399 }
17400 else if (INTEGERP (car))
17401 {
17402 register int lim = XINT (car);
17403 elt = XCDR (elt);
17404 if (lim < 0)
17405 {
17406 /* Negative int means reduce maximum width. */
17407 if (precision <= 0)
17408 precision = -lim;
17409 else
17410 precision = min (precision, -lim);
17411 }
17412 else if (lim > 0)
17413 {
17414 /* Padding specified. Don't let it be more than
17415 current maximum. */
17416 if (precision > 0)
17417 lim = min (precision, lim);
17418
17419 /* If that's more padding than already wanted, queue it.
17420 But don't reduce padding already specified even if
17421 that is beyond the current truncation point. */
17422 field_width = max (lim, field_width);
17423 }
17424 goto tail_recurse;
17425 }
17426 else if (STRINGP (car) || CONSP (car))
17427 {
17428 register int limit = 50;
17429 /* Limit is to protect against circular lists. */
17430 while (CONSP (elt)
17431 && --limit > 0
17432 && (precision <= 0 || n < precision))
17433 {
17434 n += display_mode_element (it, depth,
17435 /* Do padding only after the last
17436 element in the list. */
17437 (! CONSP (XCDR (elt))
17438 ? field_width - n
17439 : 0),
17440 precision - n, XCAR (elt),
17441 props, risky);
17442 elt = XCDR (elt);
17443 }
17444 }
17445 }
17446 break;
17447
17448 default:
17449 invalid:
17450 elt = build_string ("*invalid*");
17451 goto tail_recurse;
17452 }
17453
17454 /* Pad to FIELD_WIDTH. */
17455 if (field_width > 0 && n < field_width)
17456 {
17457 switch (mode_line_target)
17458 {
17459 case MODE_LINE_NOPROP:
17460 case MODE_LINE_TITLE:
17461 n += store_mode_line_noprop ("", field_width - n, 0);
17462 break;
17463 case MODE_LINE_STRING:
17464 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17465 break;
17466 case MODE_LINE_DISPLAY:
17467 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17468 0, 0, 0);
17469 break;
17470 }
17471 }
17472
17473 return n;
17474 }
17475
17476 /* Store a mode-line string element in mode_line_string_list.
17477
17478 If STRING is non-null, display that C string. Otherwise, the Lisp
17479 string LISP_STRING is displayed.
17480
17481 FIELD_WIDTH is the minimum number of output glyphs to produce.
17482 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17483 with spaces. FIELD_WIDTH <= 0 means don't pad.
17484
17485 PRECISION is the maximum number of characters to output from
17486 STRING. PRECISION <= 0 means don't truncate the string.
17487
17488 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17489 properties to the string.
17490
17491 PROPS are the properties to add to the string.
17492 The mode_line_string_face face property is always added to the string.
17493 */
17494
17495 static int
17496 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17497 char *string;
17498 Lisp_Object lisp_string;
17499 int copy_string;
17500 int field_width;
17501 int precision;
17502 Lisp_Object props;
17503 {
17504 int len;
17505 int n = 0;
17506
17507 if (string != NULL)
17508 {
17509 len = strlen (string);
17510 if (precision > 0 && len > precision)
17511 len = precision;
17512 lisp_string = make_string (string, len);
17513 if (NILP (props))
17514 props = mode_line_string_face_prop;
17515 else if (!NILP (mode_line_string_face))
17516 {
17517 Lisp_Object face = Fplist_get (props, Qface);
17518 props = Fcopy_sequence (props);
17519 if (NILP (face))
17520 face = mode_line_string_face;
17521 else
17522 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17523 props = Fplist_put (props, Qface, face);
17524 }
17525 Fadd_text_properties (make_number (0), make_number (len),
17526 props, lisp_string);
17527 }
17528 else
17529 {
17530 len = XFASTINT (Flength (lisp_string));
17531 if (precision > 0 && len > precision)
17532 {
17533 len = precision;
17534 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17535 precision = -1;
17536 }
17537 if (!NILP (mode_line_string_face))
17538 {
17539 Lisp_Object face;
17540 if (NILP (props))
17541 props = Ftext_properties_at (make_number (0), lisp_string);
17542 face = Fplist_get (props, Qface);
17543 if (NILP (face))
17544 face = mode_line_string_face;
17545 else
17546 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17547 props = Fcons (Qface, Fcons (face, Qnil));
17548 if (copy_string)
17549 lisp_string = Fcopy_sequence (lisp_string);
17550 }
17551 if (!NILP (props))
17552 Fadd_text_properties (make_number (0), make_number (len),
17553 props, lisp_string);
17554 }
17555
17556 if (len > 0)
17557 {
17558 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17559 n += len;
17560 }
17561
17562 if (field_width > len)
17563 {
17564 field_width -= len;
17565 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17566 if (!NILP (props))
17567 Fadd_text_properties (make_number (0), make_number (field_width),
17568 props, lisp_string);
17569 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17570 n += field_width;
17571 }
17572
17573 return n;
17574 }
17575
17576
17577 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17578 1, 4, 0,
17579 doc: /* Format a string out of a mode line format specification.
17580 First arg FORMAT specifies the mode line format (see `mode-line-format'
17581 for details) to use.
17582
17583 Optional second arg FACE specifies the face property to put
17584 on all characters for which no face is specified.
17585 The value t means whatever face the window's mode line currently uses
17586 \(either `mode-line' or `mode-line-inactive', depending).
17587 A value of nil means the default is no face property.
17588 If FACE is an integer, the value string has no text properties.
17589
17590 Optional third and fourth args WINDOW and BUFFER specify the window
17591 and buffer to use as the context for the formatting (defaults
17592 are the selected window and the window's buffer). */)
17593 (format, face, window, buffer)
17594 Lisp_Object format, face, window, buffer;
17595 {
17596 struct it it;
17597 int len;
17598 struct window *w;
17599 struct buffer *old_buffer = NULL;
17600 int face_id = -1;
17601 int no_props = INTEGERP (face);
17602 int count = SPECPDL_INDEX ();
17603 Lisp_Object str;
17604 int string_start = 0;
17605
17606 if (NILP (window))
17607 window = selected_window;
17608 CHECK_WINDOW (window);
17609 w = XWINDOW (window);
17610
17611 if (NILP (buffer))
17612 buffer = w->buffer;
17613 CHECK_BUFFER (buffer);
17614
17615 /* Make formatting the modeline a non-op when noninteractive, otherwise
17616 there will be problems later caused by a partially initialized frame. */
17617 if (NILP (format) || noninteractive)
17618 return empty_unibyte_string;
17619
17620 if (no_props)
17621 face = Qnil;
17622
17623 if (!NILP (face))
17624 {
17625 if (EQ (face, Qt))
17626 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17627 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17628 }
17629
17630 if (face_id < 0)
17631 face_id = DEFAULT_FACE_ID;
17632
17633 if (XBUFFER (buffer) != current_buffer)
17634 old_buffer = current_buffer;
17635
17636 /* Save things including mode_line_proptrans_alist,
17637 and set that to nil so that we don't alter the outer value. */
17638 record_unwind_protect (unwind_format_mode_line,
17639 format_mode_line_unwind_data
17640 (old_buffer, selected_window, 1));
17641 mode_line_proptrans_alist = Qnil;
17642
17643 Fselect_window (window, Qt);
17644 if (old_buffer)
17645 set_buffer_internal_1 (XBUFFER (buffer));
17646
17647 init_iterator (&it, w, -1, -1, NULL, face_id);
17648
17649 if (no_props)
17650 {
17651 mode_line_target = MODE_LINE_NOPROP;
17652 mode_line_string_face_prop = Qnil;
17653 mode_line_string_list = Qnil;
17654 string_start = MODE_LINE_NOPROP_LEN (0);
17655 }
17656 else
17657 {
17658 mode_line_target = MODE_LINE_STRING;
17659 mode_line_string_list = Qnil;
17660 mode_line_string_face = face;
17661 mode_line_string_face_prop
17662 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17663 }
17664
17665 push_kboard (FRAME_KBOARD (it.f));
17666 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17667 pop_kboard ();
17668
17669 if (no_props)
17670 {
17671 len = MODE_LINE_NOPROP_LEN (string_start);
17672 str = make_string (mode_line_noprop_buf + string_start, len);
17673 }
17674 else
17675 {
17676 mode_line_string_list = Fnreverse (mode_line_string_list);
17677 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17678 empty_unibyte_string);
17679 }
17680
17681 unbind_to (count, Qnil);
17682 return str;
17683 }
17684
17685 /* Write a null-terminated, right justified decimal representation of
17686 the positive integer D to BUF using a minimal field width WIDTH. */
17687
17688 static void
17689 pint2str (buf, width, d)
17690 register char *buf;
17691 register int width;
17692 register int d;
17693 {
17694 register char *p = buf;
17695
17696 if (d <= 0)
17697 *p++ = '0';
17698 else
17699 {
17700 while (d > 0)
17701 {
17702 *p++ = d % 10 + '0';
17703 d /= 10;
17704 }
17705 }
17706
17707 for (width -= (int) (p - buf); width > 0; --width)
17708 *p++ = ' ';
17709 *p-- = '\0';
17710 while (p > buf)
17711 {
17712 d = *buf;
17713 *buf++ = *p;
17714 *p-- = d;
17715 }
17716 }
17717
17718 /* Write a null-terminated, right justified decimal and "human
17719 readable" representation of the nonnegative integer D to BUF using
17720 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17721
17722 static const char power_letter[] =
17723 {
17724 0, /* not used */
17725 'k', /* kilo */
17726 'M', /* mega */
17727 'G', /* giga */
17728 'T', /* tera */
17729 'P', /* peta */
17730 'E', /* exa */
17731 'Z', /* zetta */
17732 'Y' /* yotta */
17733 };
17734
17735 static void
17736 pint2hrstr (buf, width, d)
17737 char *buf;
17738 int width;
17739 int d;
17740 {
17741 /* We aim to represent the nonnegative integer D as
17742 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17743 int quotient = d;
17744 int remainder = 0;
17745 /* -1 means: do not use TENTHS. */
17746 int tenths = -1;
17747 int exponent = 0;
17748
17749 /* Length of QUOTIENT.TENTHS as a string. */
17750 int length;
17751
17752 char * psuffix;
17753 char * p;
17754
17755 if (1000 <= quotient)
17756 {
17757 /* Scale to the appropriate EXPONENT. */
17758 do
17759 {
17760 remainder = quotient % 1000;
17761 quotient /= 1000;
17762 exponent++;
17763 }
17764 while (1000 <= quotient);
17765
17766 /* Round to nearest and decide whether to use TENTHS or not. */
17767 if (quotient <= 9)
17768 {
17769 tenths = remainder / 100;
17770 if (50 <= remainder % 100)
17771 {
17772 if (tenths < 9)
17773 tenths++;
17774 else
17775 {
17776 quotient++;
17777 if (quotient == 10)
17778 tenths = -1;
17779 else
17780 tenths = 0;
17781 }
17782 }
17783 }
17784 else
17785 if (500 <= remainder)
17786 {
17787 if (quotient < 999)
17788 quotient++;
17789 else
17790 {
17791 quotient = 1;
17792 exponent++;
17793 tenths = 0;
17794 }
17795 }
17796 }
17797
17798 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17799 if (tenths == -1 && quotient <= 99)
17800 if (quotient <= 9)
17801 length = 1;
17802 else
17803 length = 2;
17804 else
17805 length = 3;
17806 p = psuffix = buf + max (width, length);
17807
17808 /* Print EXPONENT. */
17809 if (exponent)
17810 *psuffix++ = power_letter[exponent];
17811 *psuffix = '\0';
17812
17813 /* Print TENTHS. */
17814 if (tenths >= 0)
17815 {
17816 *--p = '0' + tenths;
17817 *--p = '.';
17818 }
17819
17820 /* Print QUOTIENT. */
17821 do
17822 {
17823 int digit = quotient % 10;
17824 *--p = '0' + digit;
17825 }
17826 while ((quotient /= 10) != 0);
17827
17828 /* Print leading spaces. */
17829 while (buf < p)
17830 *--p = ' ';
17831 }
17832
17833 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17834 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17835 type of CODING_SYSTEM. Return updated pointer into BUF. */
17836
17837 static unsigned char invalid_eol_type[] = "(*invalid*)";
17838
17839 static char *
17840 decode_mode_spec_coding (coding_system, buf, eol_flag)
17841 Lisp_Object coding_system;
17842 register char *buf;
17843 int eol_flag;
17844 {
17845 Lisp_Object val;
17846 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17847 const unsigned char *eol_str;
17848 int eol_str_len;
17849 /* The EOL conversion we are using. */
17850 Lisp_Object eoltype;
17851
17852 val = CODING_SYSTEM_SPEC (coding_system);
17853 eoltype = Qnil;
17854
17855 if (!VECTORP (val)) /* Not yet decided. */
17856 {
17857 if (multibyte)
17858 *buf++ = '-';
17859 if (eol_flag)
17860 eoltype = eol_mnemonic_undecided;
17861 /* Don't mention EOL conversion if it isn't decided. */
17862 }
17863 else
17864 {
17865 Lisp_Object attrs;
17866 Lisp_Object eolvalue;
17867
17868 attrs = AREF (val, 0);
17869 eolvalue = AREF (val, 2);
17870
17871 if (multibyte)
17872 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17873
17874 if (eol_flag)
17875 {
17876 /* The EOL conversion that is normal on this system. */
17877
17878 if (NILP (eolvalue)) /* Not yet decided. */
17879 eoltype = eol_mnemonic_undecided;
17880 else if (VECTORP (eolvalue)) /* Not yet decided. */
17881 eoltype = eol_mnemonic_undecided;
17882 else /* eolvalue is Qunix, Qdos, or Qmac. */
17883 eoltype = (EQ (eolvalue, Qunix)
17884 ? eol_mnemonic_unix
17885 : (EQ (eolvalue, Qdos) == 1
17886 ? eol_mnemonic_dos : eol_mnemonic_mac));
17887 }
17888 }
17889
17890 if (eol_flag)
17891 {
17892 /* Mention the EOL conversion if it is not the usual one. */
17893 if (STRINGP (eoltype))
17894 {
17895 eol_str = SDATA (eoltype);
17896 eol_str_len = SBYTES (eoltype);
17897 }
17898 else if (CHARACTERP (eoltype))
17899 {
17900 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17901 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17902 eol_str = tmp;
17903 }
17904 else
17905 {
17906 eol_str = invalid_eol_type;
17907 eol_str_len = sizeof (invalid_eol_type) - 1;
17908 }
17909 bcopy (eol_str, buf, eol_str_len);
17910 buf += eol_str_len;
17911 }
17912
17913 return buf;
17914 }
17915
17916 /* Return a string for the output of a mode line %-spec for window W,
17917 generated by character C. PRECISION >= 0 means don't return a
17918 string longer than that value. FIELD_WIDTH > 0 means pad the
17919 string returned with spaces to that value. Return 1 in *MULTIBYTE
17920 if the result is multibyte text.
17921
17922 Note we operate on the current buffer for most purposes,
17923 the exception being w->base_line_pos. */
17924
17925 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17926
17927 static char *
17928 decode_mode_spec (w, c, field_width, precision, multibyte)
17929 struct window *w;
17930 register int c;
17931 int field_width, precision;
17932 int *multibyte;
17933 {
17934 Lisp_Object obj;
17935 struct frame *f = XFRAME (WINDOW_FRAME (w));
17936 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17937 struct buffer *b = current_buffer;
17938
17939 obj = Qnil;
17940 *multibyte = 0;
17941
17942 switch (c)
17943 {
17944 case '*':
17945 if (!NILP (b->read_only))
17946 return "%";
17947 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17948 return "*";
17949 return "-";
17950
17951 case '+':
17952 /* This differs from %* only for a modified read-only buffer. */
17953 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17954 return "*";
17955 if (!NILP (b->read_only))
17956 return "%";
17957 return "-";
17958
17959 case '&':
17960 /* This differs from %* in ignoring read-only-ness. */
17961 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17962 return "*";
17963 return "-";
17964
17965 case '%':
17966 return "%";
17967
17968 case '[':
17969 {
17970 int i;
17971 char *p;
17972
17973 if (command_loop_level > 5)
17974 return "[[[... ";
17975 p = decode_mode_spec_buf;
17976 for (i = 0; i < command_loop_level; i++)
17977 *p++ = '[';
17978 *p = 0;
17979 return decode_mode_spec_buf;
17980 }
17981
17982 case ']':
17983 {
17984 int i;
17985 char *p;
17986
17987 if (command_loop_level > 5)
17988 return " ...]]]";
17989 p = decode_mode_spec_buf;
17990 for (i = 0; i < command_loop_level; i++)
17991 *p++ = ']';
17992 *p = 0;
17993 return decode_mode_spec_buf;
17994 }
17995
17996 case '-':
17997 {
17998 register int i;
17999
18000 /* Let lots_of_dashes be a string of infinite length. */
18001 if (mode_line_target == MODE_LINE_NOPROP ||
18002 mode_line_target == MODE_LINE_STRING)
18003 return "--";
18004 if (field_width <= 0
18005 || field_width > sizeof (lots_of_dashes))
18006 {
18007 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18008 decode_mode_spec_buf[i] = '-';
18009 decode_mode_spec_buf[i] = '\0';
18010 return decode_mode_spec_buf;
18011 }
18012 else
18013 return lots_of_dashes;
18014 }
18015
18016 case 'b':
18017 obj = b->name;
18018 break;
18019
18020 case 'c':
18021 /* %c and %l are ignored in `frame-title-format'.
18022 (In redisplay_internal, the frame title is drawn _before_ the
18023 windows are updated, so the stuff which depends on actual
18024 window contents (such as %l) may fail to render properly, or
18025 even crash emacs.) */
18026 if (mode_line_target == MODE_LINE_TITLE)
18027 return "";
18028 else
18029 {
18030 int col = (int) current_column (); /* iftc */
18031 w->column_number_displayed = make_number (col);
18032 pint2str (decode_mode_spec_buf, field_width, col);
18033 return decode_mode_spec_buf;
18034 }
18035
18036 case 'e':
18037 #ifndef SYSTEM_MALLOC
18038 {
18039 if (NILP (Vmemory_full))
18040 return "";
18041 else
18042 return "!MEM FULL! ";
18043 }
18044 #else
18045 return "";
18046 #endif
18047
18048 case 'F':
18049 /* %F displays the frame name. */
18050 if (!NILP (f->title))
18051 return (char *) SDATA (f->title);
18052 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18053 return (char *) SDATA (f->name);
18054 return "Emacs";
18055
18056 case 'f':
18057 obj = b->filename;
18058 break;
18059
18060 case 'i':
18061 {
18062 int size = ZV - BEGV;
18063 pint2str (decode_mode_spec_buf, field_width, size);
18064 return decode_mode_spec_buf;
18065 }
18066
18067 case 'I':
18068 {
18069 int size = ZV - BEGV;
18070 pint2hrstr (decode_mode_spec_buf, field_width, size);
18071 return decode_mode_spec_buf;
18072 }
18073
18074 case 'l':
18075 {
18076 int startpos, startpos_byte, line, linepos, linepos_byte;
18077 int topline, nlines, junk, height;
18078
18079 /* %c and %l are ignored in `frame-title-format'. */
18080 if (mode_line_target == MODE_LINE_TITLE)
18081 return "";
18082
18083 startpos = XMARKER (w->start)->charpos;
18084 startpos_byte = marker_byte_position (w->start);
18085 height = WINDOW_TOTAL_LINES (w);
18086
18087 /* If we decided that this buffer isn't suitable for line numbers,
18088 don't forget that too fast. */
18089 if (EQ (w->base_line_pos, w->buffer))
18090 goto no_value;
18091 /* But do forget it, if the window shows a different buffer now. */
18092 else if (BUFFERP (w->base_line_pos))
18093 w->base_line_pos = Qnil;
18094
18095 /* If the buffer is very big, don't waste time. */
18096 if (INTEGERP (Vline_number_display_limit)
18097 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18098 {
18099 w->base_line_pos = Qnil;
18100 w->base_line_number = Qnil;
18101 goto no_value;
18102 }
18103
18104 if (INTEGERP (w->base_line_number)
18105 && INTEGERP (w->base_line_pos)
18106 && XFASTINT (w->base_line_pos) <= startpos)
18107 {
18108 line = XFASTINT (w->base_line_number);
18109 linepos = XFASTINT (w->base_line_pos);
18110 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18111 }
18112 else
18113 {
18114 line = 1;
18115 linepos = BUF_BEGV (b);
18116 linepos_byte = BUF_BEGV_BYTE (b);
18117 }
18118
18119 /* Count lines from base line to window start position. */
18120 nlines = display_count_lines (linepos, linepos_byte,
18121 startpos_byte,
18122 startpos, &junk);
18123
18124 topline = nlines + line;
18125
18126 /* Determine a new base line, if the old one is too close
18127 or too far away, or if we did not have one.
18128 "Too close" means it's plausible a scroll-down would
18129 go back past it. */
18130 if (startpos == BUF_BEGV (b))
18131 {
18132 w->base_line_number = make_number (topline);
18133 w->base_line_pos = make_number (BUF_BEGV (b));
18134 }
18135 else if (nlines < height + 25 || nlines > height * 3 + 50
18136 || linepos == BUF_BEGV (b))
18137 {
18138 int limit = BUF_BEGV (b);
18139 int limit_byte = BUF_BEGV_BYTE (b);
18140 int position;
18141 int distance = (height * 2 + 30) * line_number_display_limit_width;
18142
18143 if (startpos - distance > limit)
18144 {
18145 limit = startpos - distance;
18146 limit_byte = CHAR_TO_BYTE (limit);
18147 }
18148
18149 nlines = display_count_lines (startpos, startpos_byte,
18150 limit_byte,
18151 - (height * 2 + 30),
18152 &position);
18153 /* If we couldn't find the lines we wanted within
18154 line_number_display_limit_width chars per line,
18155 give up on line numbers for this window. */
18156 if (position == limit_byte && limit == startpos - distance)
18157 {
18158 w->base_line_pos = w->buffer;
18159 w->base_line_number = Qnil;
18160 goto no_value;
18161 }
18162
18163 w->base_line_number = make_number (topline - nlines);
18164 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18165 }
18166
18167 /* Now count lines from the start pos to point. */
18168 nlines = display_count_lines (startpos, startpos_byte,
18169 PT_BYTE, PT, &junk);
18170
18171 /* Record that we did display the line number. */
18172 line_number_displayed = 1;
18173
18174 /* Make the string to show. */
18175 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18176 return decode_mode_spec_buf;
18177 no_value:
18178 {
18179 char* p = decode_mode_spec_buf;
18180 int pad = field_width - 2;
18181 while (pad-- > 0)
18182 *p++ = ' ';
18183 *p++ = '?';
18184 *p++ = '?';
18185 *p = '\0';
18186 return decode_mode_spec_buf;
18187 }
18188 }
18189 break;
18190
18191 case 'm':
18192 obj = b->mode_name;
18193 break;
18194
18195 case 'n':
18196 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18197 return " Narrow";
18198 break;
18199
18200 case 'p':
18201 {
18202 int pos = marker_position (w->start);
18203 int total = BUF_ZV (b) - BUF_BEGV (b);
18204
18205 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18206 {
18207 if (pos <= BUF_BEGV (b))
18208 return "All";
18209 else
18210 return "Bottom";
18211 }
18212 else if (pos <= BUF_BEGV (b))
18213 return "Top";
18214 else
18215 {
18216 if (total > 1000000)
18217 /* Do it differently for a large value, to avoid overflow. */
18218 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18219 else
18220 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18221 /* We can't normally display a 3-digit number,
18222 so get us a 2-digit number that is close. */
18223 if (total == 100)
18224 total = 99;
18225 sprintf (decode_mode_spec_buf, "%2d%%", total);
18226 return decode_mode_spec_buf;
18227 }
18228 }
18229
18230 /* Display percentage of size above the bottom of the screen. */
18231 case 'P':
18232 {
18233 int toppos = marker_position (w->start);
18234 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18235 int total = BUF_ZV (b) - BUF_BEGV (b);
18236
18237 if (botpos >= BUF_ZV (b))
18238 {
18239 if (toppos <= BUF_BEGV (b))
18240 return "All";
18241 else
18242 return "Bottom";
18243 }
18244 else
18245 {
18246 if (total > 1000000)
18247 /* Do it differently for a large value, to avoid overflow. */
18248 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18249 else
18250 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18251 /* We can't normally display a 3-digit number,
18252 so get us a 2-digit number that is close. */
18253 if (total == 100)
18254 total = 99;
18255 if (toppos <= BUF_BEGV (b))
18256 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18257 else
18258 sprintf (decode_mode_spec_buf, "%2d%%", total);
18259 return decode_mode_spec_buf;
18260 }
18261 }
18262
18263 case 's':
18264 /* status of process */
18265 obj = Fget_buffer_process (Fcurrent_buffer ());
18266 if (NILP (obj))
18267 return "no process";
18268 #ifdef subprocesses
18269 obj = Fsymbol_name (Fprocess_status (obj));
18270 #endif
18271 break;
18272
18273 case '@':
18274 {
18275 Lisp_Object val;
18276 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18277 if (NILP (val))
18278 return "-";
18279 else
18280 return "@";
18281 }
18282
18283 case 't': /* indicate TEXT or BINARY */
18284 #ifdef MODE_LINE_BINARY_TEXT
18285 return MODE_LINE_BINARY_TEXT (b);
18286 #else
18287 return "T";
18288 #endif
18289
18290 case 'z':
18291 /* coding-system (not including end-of-line format) */
18292 case 'Z':
18293 /* coding-system (including end-of-line type) */
18294 {
18295 int eol_flag = (c == 'Z');
18296 char *p = decode_mode_spec_buf;
18297
18298 if (! FRAME_WINDOW_P (f))
18299 {
18300 /* No need to mention EOL here--the terminal never needs
18301 to do EOL conversion. */
18302 p = decode_mode_spec_coding (CODING_ID_NAME
18303 (FRAME_KEYBOARD_CODING (f)->id),
18304 p, 0);
18305 p = decode_mode_spec_coding (CODING_ID_NAME
18306 (FRAME_TERMINAL_CODING (f)->id),
18307 p, 0);
18308 }
18309 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18310 p, eol_flag);
18311
18312 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18313 #ifdef subprocesses
18314 obj = Fget_buffer_process (Fcurrent_buffer ());
18315 if (PROCESSP (obj))
18316 {
18317 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18318 p, eol_flag);
18319 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18320 p, eol_flag);
18321 }
18322 #endif /* subprocesses */
18323 #endif /* 0 */
18324 *p = 0;
18325 return decode_mode_spec_buf;
18326 }
18327 }
18328
18329 if (STRINGP (obj))
18330 {
18331 *multibyte = STRING_MULTIBYTE (obj);
18332 return (char *) SDATA (obj);
18333 }
18334 else
18335 return "";
18336 }
18337
18338
18339 /* Count up to COUNT lines starting from START / START_BYTE.
18340 But don't go beyond LIMIT_BYTE.
18341 Return the number of lines thus found (always nonnegative).
18342
18343 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18344
18345 static int
18346 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18347 int start, start_byte, limit_byte, count;
18348 int *byte_pos_ptr;
18349 {
18350 register unsigned char *cursor;
18351 unsigned char *base;
18352
18353 register int ceiling;
18354 register unsigned char *ceiling_addr;
18355 int orig_count = count;
18356
18357 /* If we are not in selective display mode,
18358 check only for newlines. */
18359 int selective_display = (!NILP (current_buffer->selective_display)
18360 && !INTEGERP (current_buffer->selective_display));
18361
18362 if (count > 0)
18363 {
18364 while (start_byte < limit_byte)
18365 {
18366 ceiling = BUFFER_CEILING_OF (start_byte);
18367 ceiling = min (limit_byte - 1, ceiling);
18368 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18369 base = (cursor = BYTE_POS_ADDR (start_byte));
18370 while (1)
18371 {
18372 if (selective_display)
18373 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18374 ;
18375 else
18376 while (*cursor != '\n' && ++cursor != ceiling_addr)
18377 ;
18378
18379 if (cursor != ceiling_addr)
18380 {
18381 if (--count == 0)
18382 {
18383 start_byte += cursor - base + 1;
18384 *byte_pos_ptr = start_byte;
18385 return orig_count;
18386 }
18387 else
18388 if (++cursor == ceiling_addr)
18389 break;
18390 }
18391 else
18392 break;
18393 }
18394 start_byte += cursor - base;
18395 }
18396 }
18397 else
18398 {
18399 while (start_byte > limit_byte)
18400 {
18401 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18402 ceiling = max (limit_byte, ceiling);
18403 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18404 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18405 while (1)
18406 {
18407 if (selective_display)
18408 while (--cursor != ceiling_addr
18409 && *cursor != '\n' && *cursor != 015)
18410 ;
18411 else
18412 while (--cursor != ceiling_addr && *cursor != '\n')
18413 ;
18414
18415 if (cursor != ceiling_addr)
18416 {
18417 if (++count == 0)
18418 {
18419 start_byte += cursor - base + 1;
18420 *byte_pos_ptr = start_byte;
18421 /* When scanning backwards, we should
18422 not count the newline posterior to which we stop. */
18423 return - orig_count - 1;
18424 }
18425 }
18426 else
18427 break;
18428 }
18429 /* Here we add 1 to compensate for the last decrement
18430 of CURSOR, which took it past the valid range. */
18431 start_byte += cursor - base + 1;
18432 }
18433 }
18434
18435 *byte_pos_ptr = limit_byte;
18436
18437 if (count < 0)
18438 return - orig_count + count;
18439 return orig_count - count;
18440
18441 }
18442
18443
18444 \f
18445 /***********************************************************************
18446 Displaying strings
18447 ***********************************************************************/
18448
18449 /* Display a NUL-terminated string, starting with index START.
18450
18451 If STRING is non-null, display that C string. Otherwise, the Lisp
18452 string LISP_STRING is displayed.
18453
18454 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18455 FACE_STRING. Display STRING or LISP_STRING with the face at
18456 FACE_STRING_POS in FACE_STRING:
18457
18458 Display the string in the environment given by IT, but use the
18459 standard display table, temporarily.
18460
18461 FIELD_WIDTH is the minimum number of output glyphs to produce.
18462 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18463 with spaces. If STRING has more characters, more than FIELD_WIDTH
18464 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18465
18466 PRECISION is the maximum number of characters to output from
18467 STRING. PRECISION < 0 means don't truncate the string.
18468
18469 This is roughly equivalent to printf format specifiers:
18470
18471 FIELD_WIDTH PRECISION PRINTF
18472 ----------------------------------------
18473 -1 -1 %s
18474 -1 10 %.10s
18475 10 -1 %10s
18476 20 10 %20.10s
18477
18478 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18479 display them, and < 0 means obey the current buffer's value of
18480 enable_multibyte_characters.
18481
18482 Value is the number of columns displayed. */
18483
18484 static int
18485 display_string (string, lisp_string, face_string, face_string_pos,
18486 start, it, field_width, precision, max_x, multibyte)
18487 unsigned char *string;
18488 Lisp_Object lisp_string;
18489 Lisp_Object face_string;
18490 EMACS_INT face_string_pos;
18491 EMACS_INT start;
18492 struct it *it;
18493 int field_width, precision, max_x;
18494 int multibyte;
18495 {
18496 int hpos_at_start = it->hpos;
18497 int saved_face_id = it->face_id;
18498 struct glyph_row *row = it->glyph_row;
18499
18500 /* Initialize the iterator IT for iteration over STRING beginning
18501 with index START. */
18502 reseat_to_string (it, string, lisp_string, start,
18503 precision, field_width, multibyte);
18504
18505 /* If displaying STRING, set up the face of the iterator
18506 from LISP_STRING, if that's given. */
18507 if (STRINGP (face_string))
18508 {
18509 EMACS_INT endptr;
18510 struct face *face;
18511
18512 it->face_id
18513 = face_at_string_position (it->w, face_string, face_string_pos,
18514 0, it->region_beg_charpos,
18515 it->region_end_charpos,
18516 &endptr, it->base_face_id, 0);
18517 face = FACE_FROM_ID (it->f, it->face_id);
18518 it->face_box_p = face->box != FACE_NO_BOX;
18519 }
18520
18521 /* Set max_x to the maximum allowed X position. Don't let it go
18522 beyond the right edge of the window. */
18523 if (max_x <= 0)
18524 max_x = it->last_visible_x;
18525 else
18526 max_x = min (max_x, it->last_visible_x);
18527
18528 /* Skip over display elements that are not visible. because IT->w is
18529 hscrolled. */
18530 if (it->current_x < it->first_visible_x)
18531 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18532 MOVE_TO_POS | MOVE_TO_X);
18533
18534 row->ascent = it->max_ascent;
18535 row->height = it->max_ascent + it->max_descent;
18536 row->phys_ascent = it->max_phys_ascent;
18537 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18538 row->extra_line_spacing = it->max_extra_line_spacing;
18539
18540 /* This condition is for the case that we are called with current_x
18541 past last_visible_x. */
18542 while (it->current_x < max_x)
18543 {
18544 int x_before, x, n_glyphs_before, i, nglyphs;
18545
18546 /* Get the next display element. */
18547 if (!get_next_display_element (it))
18548 break;
18549
18550 /* Produce glyphs. */
18551 x_before = it->current_x;
18552 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18553 PRODUCE_GLYPHS (it);
18554
18555 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18556 i = 0;
18557 x = x_before;
18558 while (i < nglyphs)
18559 {
18560 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18561
18562 if (!it->truncate_lines_p
18563 && x + glyph->pixel_width > max_x)
18564 {
18565 /* End of continued line or max_x reached. */
18566 if (CHAR_GLYPH_PADDING_P (*glyph))
18567 {
18568 /* A wide character is unbreakable. */
18569 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18570 it->current_x = x_before;
18571 }
18572 else
18573 {
18574 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18575 it->current_x = x;
18576 }
18577 break;
18578 }
18579 else if (x + glyph->pixel_width >= it->first_visible_x)
18580 {
18581 /* Glyph is at least partially visible. */
18582 ++it->hpos;
18583 if (x < it->first_visible_x)
18584 it->glyph_row->x = x - it->first_visible_x;
18585 }
18586 else
18587 {
18588 /* Glyph is off the left margin of the display area.
18589 Should not happen. */
18590 abort ();
18591 }
18592
18593 row->ascent = max (row->ascent, it->max_ascent);
18594 row->height = max (row->height, it->max_ascent + it->max_descent);
18595 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18596 row->phys_height = max (row->phys_height,
18597 it->max_phys_ascent + it->max_phys_descent);
18598 row->extra_line_spacing = max (row->extra_line_spacing,
18599 it->max_extra_line_spacing);
18600 x += glyph->pixel_width;
18601 ++i;
18602 }
18603
18604 /* Stop if max_x reached. */
18605 if (i < nglyphs)
18606 break;
18607
18608 /* Stop at line ends. */
18609 if (ITERATOR_AT_END_OF_LINE_P (it))
18610 {
18611 it->continuation_lines_width = 0;
18612 break;
18613 }
18614
18615 set_iterator_to_next (it, 1);
18616
18617 /* Stop if truncating at the right edge. */
18618 if (it->truncate_lines_p
18619 && it->current_x >= it->last_visible_x)
18620 {
18621 /* Add truncation mark, but don't do it if the line is
18622 truncated at a padding space. */
18623 if (IT_CHARPOS (*it) < it->string_nchars)
18624 {
18625 if (!FRAME_WINDOW_P (it->f))
18626 {
18627 int i, n;
18628
18629 if (it->current_x > it->last_visible_x)
18630 {
18631 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18632 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18633 break;
18634 for (n = row->used[TEXT_AREA]; i < n; ++i)
18635 {
18636 row->used[TEXT_AREA] = i;
18637 produce_special_glyphs (it, IT_TRUNCATION);
18638 }
18639 }
18640 produce_special_glyphs (it, IT_TRUNCATION);
18641 }
18642 it->glyph_row->truncated_on_right_p = 1;
18643 }
18644 break;
18645 }
18646 }
18647
18648 /* Maybe insert a truncation at the left. */
18649 if (it->first_visible_x
18650 && IT_CHARPOS (*it) > 0)
18651 {
18652 if (!FRAME_WINDOW_P (it->f))
18653 insert_left_trunc_glyphs (it);
18654 it->glyph_row->truncated_on_left_p = 1;
18655 }
18656
18657 it->face_id = saved_face_id;
18658
18659 /* Value is number of columns displayed. */
18660 return it->hpos - hpos_at_start;
18661 }
18662
18663
18664 \f
18665 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18666 appears as an element of LIST or as the car of an element of LIST.
18667 If PROPVAL is a list, compare each element against LIST in that
18668 way, and return 1/2 if any element of PROPVAL is found in LIST.
18669 Otherwise return 0. This function cannot quit.
18670 The return value is 2 if the text is invisible but with an ellipsis
18671 and 1 if it's invisible and without an ellipsis. */
18672
18673 int
18674 invisible_p (propval, list)
18675 register Lisp_Object propval;
18676 Lisp_Object list;
18677 {
18678 register Lisp_Object tail, proptail;
18679
18680 for (tail = list; CONSP (tail); tail = XCDR (tail))
18681 {
18682 register Lisp_Object tem;
18683 tem = XCAR (tail);
18684 if (EQ (propval, tem))
18685 return 1;
18686 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18687 return NILP (XCDR (tem)) ? 1 : 2;
18688 }
18689
18690 if (CONSP (propval))
18691 {
18692 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18693 {
18694 Lisp_Object propelt;
18695 propelt = XCAR (proptail);
18696 for (tail = list; CONSP (tail); tail = XCDR (tail))
18697 {
18698 register Lisp_Object tem;
18699 tem = XCAR (tail);
18700 if (EQ (propelt, tem))
18701 return 1;
18702 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18703 return NILP (XCDR (tem)) ? 1 : 2;
18704 }
18705 }
18706 }
18707
18708 return 0;
18709 }
18710
18711 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18712 doc: /* Non-nil if the property makes the text invisible.
18713 POS-OR-PROP can be a marker or number, in which case it is taken to be
18714 a position in the current buffer and the value of the `invisible' property
18715 is checked; or it can be some other value, which is then presumed to be the
18716 value of the `invisible' property of the text of interest.
18717 The non-nil value returned can be t for truly invisible text or something
18718 else if the text is replaced by an ellipsis. */)
18719 (pos_or_prop)
18720 Lisp_Object pos_or_prop;
18721 {
18722 Lisp_Object prop
18723 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18724 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18725 : pos_or_prop);
18726 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18727 return (invis == 0 ? Qnil
18728 : invis == 1 ? Qt
18729 : make_number (invis));
18730 }
18731
18732 /* Calculate a width or height in pixels from a specification using
18733 the following elements:
18734
18735 SPEC ::=
18736 NUM - a (fractional) multiple of the default font width/height
18737 (NUM) - specifies exactly NUM pixels
18738 UNIT - a fixed number of pixels, see below.
18739 ELEMENT - size of a display element in pixels, see below.
18740 (NUM . SPEC) - equals NUM * SPEC
18741 (+ SPEC SPEC ...) - add pixel values
18742 (- SPEC SPEC ...) - subtract pixel values
18743 (- SPEC) - negate pixel value
18744
18745 NUM ::=
18746 INT or FLOAT - a number constant
18747 SYMBOL - use symbol's (buffer local) variable binding.
18748
18749 UNIT ::=
18750 in - pixels per inch *)
18751 mm - pixels per 1/1000 meter *)
18752 cm - pixels per 1/100 meter *)
18753 width - width of current font in pixels.
18754 height - height of current font in pixels.
18755
18756 *) using the ratio(s) defined in display-pixels-per-inch.
18757
18758 ELEMENT ::=
18759
18760 left-fringe - left fringe width in pixels
18761 right-fringe - right fringe width in pixels
18762
18763 left-margin - left margin width in pixels
18764 right-margin - right margin width in pixels
18765
18766 scroll-bar - scroll-bar area width in pixels
18767
18768 Examples:
18769
18770 Pixels corresponding to 5 inches:
18771 (5 . in)
18772
18773 Total width of non-text areas on left side of window (if scroll-bar is on left):
18774 '(space :width (+ left-fringe left-margin scroll-bar))
18775
18776 Align to first text column (in header line):
18777 '(space :align-to 0)
18778
18779 Align to middle of text area minus half the width of variable `my-image'
18780 containing a loaded image:
18781 '(space :align-to (0.5 . (- text my-image)))
18782
18783 Width of left margin minus width of 1 character in the default font:
18784 '(space :width (- left-margin 1))
18785
18786 Width of left margin minus width of 2 characters in the current font:
18787 '(space :width (- left-margin (2 . width)))
18788
18789 Center 1 character over left-margin (in header line):
18790 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18791
18792 Different ways to express width of left fringe plus left margin minus one pixel:
18793 '(space :width (- (+ left-fringe left-margin) (1)))
18794 '(space :width (+ left-fringe left-margin (- (1))))
18795 '(space :width (+ left-fringe left-margin (-1)))
18796
18797 */
18798
18799 #define NUMVAL(X) \
18800 ((INTEGERP (X) || FLOATP (X)) \
18801 ? XFLOATINT (X) \
18802 : - 1)
18803
18804 int
18805 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18806 double *res;
18807 struct it *it;
18808 Lisp_Object prop;
18809 struct font *font;
18810 int width_p, *align_to;
18811 {
18812 double pixels;
18813
18814 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18815 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18816
18817 if (NILP (prop))
18818 return OK_PIXELS (0);
18819
18820 xassert (FRAME_LIVE_P (it->f));
18821
18822 if (SYMBOLP (prop))
18823 {
18824 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18825 {
18826 char *unit = SDATA (SYMBOL_NAME (prop));
18827
18828 if (unit[0] == 'i' && unit[1] == 'n')
18829 pixels = 1.0;
18830 else if (unit[0] == 'm' && unit[1] == 'm')
18831 pixels = 25.4;
18832 else if (unit[0] == 'c' && unit[1] == 'm')
18833 pixels = 2.54;
18834 else
18835 pixels = 0;
18836 if (pixels > 0)
18837 {
18838 double ppi;
18839 #ifdef HAVE_WINDOW_SYSTEM
18840 if (FRAME_WINDOW_P (it->f)
18841 && (ppi = (width_p
18842 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18843 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18844 ppi > 0))
18845 return OK_PIXELS (ppi / pixels);
18846 #endif
18847
18848 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18849 || (CONSP (Vdisplay_pixels_per_inch)
18850 && (ppi = (width_p
18851 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18852 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18853 ppi > 0)))
18854 return OK_PIXELS (ppi / pixels);
18855
18856 return 0;
18857 }
18858 }
18859
18860 #ifdef HAVE_WINDOW_SYSTEM
18861 if (EQ (prop, Qheight))
18862 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18863 if (EQ (prop, Qwidth))
18864 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18865 #else
18866 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18867 return OK_PIXELS (1);
18868 #endif
18869
18870 if (EQ (prop, Qtext))
18871 return OK_PIXELS (width_p
18872 ? window_box_width (it->w, TEXT_AREA)
18873 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18874
18875 if (align_to && *align_to < 0)
18876 {
18877 *res = 0;
18878 if (EQ (prop, Qleft))
18879 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18880 if (EQ (prop, Qright))
18881 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18882 if (EQ (prop, Qcenter))
18883 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18884 + window_box_width (it->w, TEXT_AREA) / 2);
18885 if (EQ (prop, Qleft_fringe))
18886 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18887 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18888 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18889 if (EQ (prop, Qright_fringe))
18890 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18891 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18892 : window_box_right_offset (it->w, TEXT_AREA));
18893 if (EQ (prop, Qleft_margin))
18894 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18895 if (EQ (prop, Qright_margin))
18896 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18897 if (EQ (prop, Qscroll_bar))
18898 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18899 ? 0
18900 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18901 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18902 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18903 : 0)));
18904 }
18905 else
18906 {
18907 if (EQ (prop, Qleft_fringe))
18908 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18909 if (EQ (prop, Qright_fringe))
18910 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18911 if (EQ (prop, Qleft_margin))
18912 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18913 if (EQ (prop, Qright_margin))
18914 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18915 if (EQ (prop, Qscroll_bar))
18916 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18917 }
18918
18919 prop = Fbuffer_local_value (prop, it->w->buffer);
18920 }
18921
18922 if (INTEGERP (prop) || FLOATP (prop))
18923 {
18924 int base_unit = (width_p
18925 ? FRAME_COLUMN_WIDTH (it->f)
18926 : FRAME_LINE_HEIGHT (it->f));
18927 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18928 }
18929
18930 if (CONSP (prop))
18931 {
18932 Lisp_Object car = XCAR (prop);
18933 Lisp_Object cdr = XCDR (prop);
18934
18935 if (SYMBOLP (car))
18936 {
18937 #ifdef HAVE_WINDOW_SYSTEM
18938 if (FRAME_WINDOW_P (it->f)
18939 && valid_image_p (prop))
18940 {
18941 int id = lookup_image (it->f, prop);
18942 struct image *img = IMAGE_FROM_ID (it->f, id);
18943
18944 return OK_PIXELS (width_p ? img->width : img->height);
18945 }
18946 #endif
18947 if (EQ (car, Qplus) || EQ (car, Qminus))
18948 {
18949 int first = 1;
18950 double px;
18951
18952 pixels = 0;
18953 while (CONSP (cdr))
18954 {
18955 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18956 font, width_p, align_to))
18957 return 0;
18958 if (first)
18959 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18960 else
18961 pixels += px;
18962 cdr = XCDR (cdr);
18963 }
18964 if (EQ (car, Qminus))
18965 pixels = -pixels;
18966 return OK_PIXELS (pixels);
18967 }
18968
18969 car = Fbuffer_local_value (car, it->w->buffer);
18970 }
18971
18972 if (INTEGERP (car) || FLOATP (car))
18973 {
18974 double fact;
18975 pixels = XFLOATINT (car);
18976 if (NILP (cdr))
18977 return OK_PIXELS (pixels);
18978 if (calc_pixel_width_or_height (&fact, it, cdr,
18979 font, width_p, align_to))
18980 return OK_PIXELS (pixels * fact);
18981 return 0;
18982 }
18983
18984 return 0;
18985 }
18986
18987 return 0;
18988 }
18989
18990 \f
18991 /***********************************************************************
18992 Glyph Display
18993 ***********************************************************************/
18994
18995 #ifdef HAVE_WINDOW_SYSTEM
18996
18997 #if GLYPH_DEBUG
18998
18999 void
19000 dump_glyph_string (s)
19001 struct glyph_string *s;
19002 {
19003 fprintf (stderr, "glyph string\n");
19004 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19005 s->x, s->y, s->width, s->height);
19006 fprintf (stderr, " ybase = %d\n", s->ybase);
19007 fprintf (stderr, " hl = %d\n", s->hl);
19008 fprintf (stderr, " left overhang = %d, right = %d\n",
19009 s->left_overhang, s->right_overhang);
19010 fprintf (stderr, " nchars = %d\n", s->nchars);
19011 fprintf (stderr, " extends to end of line = %d\n",
19012 s->extends_to_end_of_line_p);
19013 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19014 fprintf (stderr, " bg width = %d\n", s->background_width);
19015 }
19016
19017 #endif /* GLYPH_DEBUG */
19018
19019 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19020 of XChar2b structures for S; it can't be allocated in
19021 init_glyph_string because it must be allocated via `alloca'. W
19022 is the window on which S is drawn. ROW and AREA are the glyph row
19023 and area within the row from which S is constructed. START is the
19024 index of the first glyph structure covered by S. HL is a
19025 face-override for drawing S. */
19026
19027 #ifdef HAVE_NTGUI
19028 #define OPTIONAL_HDC(hdc) hdc,
19029 #define DECLARE_HDC(hdc) HDC hdc;
19030 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19031 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19032 #endif
19033
19034 #ifndef OPTIONAL_HDC
19035 #define OPTIONAL_HDC(hdc)
19036 #define DECLARE_HDC(hdc)
19037 #define ALLOCATE_HDC(hdc, f)
19038 #define RELEASE_HDC(hdc, f)
19039 #endif
19040
19041 static void
19042 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19043 struct glyph_string *s;
19044 DECLARE_HDC (hdc)
19045 XChar2b *char2b;
19046 struct window *w;
19047 struct glyph_row *row;
19048 enum glyph_row_area area;
19049 int start;
19050 enum draw_glyphs_face hl;
19051 {
19052 bzero (s, sizeof *s);
19053 s->w = w;
19054 s->f = XFRAME (w->frame);
19055 #ifdef HAVE_NTGUI
19056 s->hdc = hdc;
19057 #endif
19058 s->display = FRAME_X_DISPLAY (s->f);
19059 s->window = FRAME_X_WINDOW (s->f);
19060 s->char2b = char2b;
19061 s->hl = hl;
19062 s->row = row;
19063 s->area = area;
19064 s->first_glyph = row->glyphs[area] + start;
19065 s->height = row->height;
19066 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19067
19068 /* Display the internal border below the tool-bar window. */
19069 if (WINDOWP (s->f->tool_bar_window)
19070 && s->w == XWINDOW (s->f->tool_bar_window))
19071 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19072
19073 s->ybase = s->y + row->ascent;
19074 }
19075
19076
19077 /* Append the list of glyph strings with head H and tail T to the list
19078 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19079
19080 static INLINE void
19081 append_glyph_string_lists (head, tail, h, t)
19082 struct glyph_string **head, **tail;
19083 struct glyph_string *h, *t;
19084 {
19085 if (h)
19086 {
19087 if (*head)
19088 (*tail)->next = h;
19089 else
19090 *head = h;
19091 h->prev = *tail;
19092 *tail = t;
19093 }
19094 }
19095
19096
19097 /* Prepend the list of glyph strings with head H and tail T to the
19098 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19099 result. */
19100
19101 static INLINE void
19102 prepend_glyph_string_lists (head, tail, h, t)
19103 struct glyph_string **head, **tail;
19104 struct glyph_string *h, *t;
19105 {
19106 if (h)
19107 {
19108 if (*head)
19109 (*head)->prev = t;
19110 else
19111 *tail = t;
19112 t->next = *head;
19113 *head = h;
19114 }
19115 }
19116
19117
19118 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19119 Set *HEAD and *TAIL to the resulting list. */
19120
19121 static INLINE void
19122 append_glyph_string (head, tail, s)
19123 struct glyph_string **head, **tail;
19124 struct glyph_string *s;
19125 {
19126 s->next = s->prev = NULL;
19127 append_glyph_string_lists (head, tail, s, s);
19128 }
19129
19130
19131 /* Get face and two-byte form of character C in face FACE_ID on frame
19132 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19133 means we want to display multibyte text. DISPLAY_P non-zero means
19134 make sure that X resources for the face returned are allocated.
19135 Value is a pointer to a realized face that is ready for display if
19136 DISPLAY_P is non-zero. */
19137
19138 static INLINE struct face *
19139 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19140 struct frame *f;
19141 int c, face_id;
19142 XChar2b *char2b;
19143 int multibyte_p, display_p;
19144 {
19145 struct face *face = FACE_FROM_ID (f, face_id);
19146
19147 if (face->font)
19148 {
19149 unsigned code = face->font->driver->encode_char (face->font, c);
19150
19151 if (code != FONT_INVALID_CODE)
19152 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19153 else
19154 STORE_XCHAR2B (char2b, 0, 0);
19155 }
19156
19157 /* Make sure X resources of the face are allocated. */
19158 #ifdef HAVE_X_WINDOWS
19159 if (display_p)
19160 #endif
19161 {
19162 xassert (face != NULL);
19163 PREPARE_FACE_FOR_DISPLAY (f, face);
19164 }
19165
19166 return face;
19167 }
19168
19169
19170 /* Get face and two-byte form of character glyph GLYPH on frame F.
19171 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19172 a pointer to a realized face that is ready for display. */
19173
19174 static INLINE struct face *
19175 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19176 struct frame *f;
19177 struct glyph *glyph;
19178 XChar2b *char2b;
19179 int *two_byte_p;
19180 {
19181 struct face *face;
19182
19183 xassert (glyph->type == CHAR_GLYPH);
19184 face = FACE_FROM_ID (f, glyph->face_id);
19185
19186 if (two_byte_p)
19187 *two_byte_p = 0;
19188
19189 if (face->font)
19190 {
19191 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19192
19193 if (code != FONT_INVALID_CODE)
19194 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19195 else
19196 STORE_XCHAR2B (char2b, 0, code);
19197 }
19198
19199 /* Make sure X resources of the face are allocated. */
19200 xassert (face != NULL);
19201 PREPARE_FACE_FOR_DISPLAY (f, face);
19202 return face;
19203 }
19204
19205
19206 /* Fill glyph string S with composition components specified by S->cmp.
19207
19208 BASE_FACE is the base face of the composition.
19209 S->gidx is the index of the first component for S.
19210
19211 OVERLAPS non-zero means S should draw the foreground only, and use
19212 its physical height for clipping. See also draw_glyphs.
19213
19214 Value is the index of a component not in S. */
19215
19216 static int
19217 fill_composite_glyph_string (s, base_face, overlaps)
19218 struct glyph_string *s;
19219 struct face *base_face;
19220 int overlaps;
19221 {
19222 int i;
19223
19224 xassert (s);
19225
19226 s->for_overlaps = overlaps;
19227
19228 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19229 {
19230 Lisp_Object gstring
19231 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19232 s->cmp->hash_index * 2);
19233
19234 s->face = base_face;
19235 s->font = base_face->font;
19236 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19237 {
19238 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19239 unsigned code;
19240 XChar2b * store_pos;
19241 if (NILP (g))
19242 break;
19243 code = LGLYPH_CODE (g);
19244 store_pos = s->char2b + i;
19245 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19246 }
19247 s->width = s->cmp->pixel_width;
19248 }
19249 else
19250 {
19251 /* For all glyphs of this composition, starting at the offset
19252 S->gidx, until we reach the end of the definition or encounter a
19253 glyph that requires the different face, add it to S. */
19254 struct face *face;
19255
19256 s->face = NULL;
19257 s->font = NULL;
19258 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19259 {
19260 int c = COMPOSITION_GLYPH (s->cmp, i);
19261
19262 if (c != '\t')
19263 {
19264 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19265 -1, Qnil);
19266
19267 face = get_char_face_and_encoding (s->f, c, face_id,
19268 s->char2b + i, 1, 1);
19269 if (face)
19270 {
19271 if (! s->face)
19272 {
19273 s->face = face;
19274 s->font = s->face->font;
19275 }
19276 else if (s->face != face)
19277 break;
19278 }
19279 }
19280 ++s->nchars;
19281 }
19282
19283 /* All glyph strings for the same composition has the same width,
19284 i.e. the width set for the first component of the composition. */
19285 s->width = s->first_glyph->pixel_width;
19286 }
19287
19288 /* If the specified font could not be loaded, use the frame's
19289 default font, but record the fact that we couldn't load it in
19290 the glyph string so that we can draw rectangles for the
19291 characters of the glyph string. */
19292 if (s->font == NULL)
19293 {
19294 s->font_not_found_p = 1;
19295 s->font = FRAME_FONT (s->f);
19296 }
19297
19298 /* Adjust base line for subscript/superscript text. */
19299 s->ybase += s->first_glyph->voffset;
19300
19301 /* This glyph string must always be drawn with 16-bit functions. */
19302 s->two_byte_p = 1;
19303
19304 return s->gidx + s->nchars;
19305 }
19306
19307
19308 /* Fill glyph string S from a sequence of character glyphs.
19309
19310 FACE_ID is the face id of the string. START is the index of the
19311 first glyph to consider, END is the index of the last + 1.
19312 OVERLAPS non-zero means S should draw the foreground only, and use
19313 its physical height for clipping. See also draw_glyphs.
19314
19315 Value is the index of the first glyph not in S. */
19316
19317 static int
19318 fill_glyph_string (s, face_id, start, end, overlaps)
19319 struct glyph_string *s;
19320 int face_id;
19321 int start, end, overlaps;
19322 {
19323 struct glyph *glyph, *last;
19324 int voffset;
19325 int glyph_not_available_p;
19326
19327 xassert (s->f == XFRAME (s->w->frame));
19328 xassert (s->nchars == 0);
19329 xassert (start >= 0 && end > start);
19330
19331 s->for_overlaps = overlaps,
19332 glyph = s->row->glyphs[s->area] + start;
19333 last = s->row->glyphs[s->area] + end;
19334 voffset = glyph->voffset;
19335 s->padding_p = glyph->padding_p;
19336 glyph_not_available_p = glyph->glyph_not_available_p;
19337
19338 while (glyph < last
19339 && glyph->type == CHAR_GLYPH
19340 && glyph->voffset == voffset
19341 /* Same face id implies same font, nowadays. */
19342 && glyph->face_id == face_id
19343 && glyph->glyph_not_available_p == glyph_not_available_p)
19344 {
19345 int two_byte_p;
19346
19347 s->face = get_glyph_face_and_encoding (s->f, glyph,
19348 s->char2b + s->nchars,
19349 &two_byte_p);
19350 s->two_byte_p = two_byte_p;
19351 ++s->nchars;
19352 xassert (s->nchars <= end - start);
19353 s->width += glyph->pixel_width;
19354 if (glyph++->padding_p != s->padding_p)
19355 break;
19356 }
19357
19358 s->font = s->face->font;
19359
19360 /* If the specified font could not be loaded, use the frame's font,
19361 but record the fact that we couldn't load it in
19362 S->font_not_found_p so that we can draw rectangles for the
19363 characters of the glyph string. */
19364 if (s->font == NULL || glyph_not_available_p)
19365 {
19366 s->font_not_found_p = 1;
19367 s->font = FRAME_FONT (s->f);
19368 }
19369
19370 /* Adjust base line for subscript/superscript text. */
19371 s->ybase += voffset;
19372
19373 xassert (s->face && s->face->gc);
19374 return glyph - s->row->glyphs[s->area];
19375 }
19376
19377
19378 /* Fill glyph string S from image glyph S->first_glyph. */
19379
19380 static void
19381 fill_image_glyph_string (s)
19382 struct glyph_string *s;
19383 {
19384 xassert (s->first_glyph->type == IMAGE_GLYPH);
19385 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19386 xassert (s->img);
19387 s->slice = s->first_glyph->slice;
19388 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19389 s->font = s->face->font;
19390 s->width = s->first_glyph->pixel_width;
19391
19392 /* Adjust base line for subscript/superscript text. */
19393 s->ybase += s->first_glyph->voffset;
19394 }
19395
19396
19397 /* Fill glyph string S from a sequence of stretch glyphs.
19398
19399 ROW is the glyph row in which the glyphs are found, AREA is the
19400 area within the row. START is the index of the first glyph to
19401 consider, END is the index of the last + 1.
19402
19403 Value is the index of the first glyph not in S. */
19404
19405 static int
19406 fill_stretch_glyph_string (s, row, area, start, end)
19407 struct glyph_string *s;
19408 struct glyph_row *row;
19409 enum glyph_row_area area;
19410 int start, end;
19411 {
19412 struct glyph *glyph, *last;
19413 int voffset, face_id;
19414
19415 xassert (s->first_glyph->type == STRETCH_GLYPH);
19416
19417 glyph = s->row->glyphs[s->area] + start;
19418 last = s->row->glyphs[s->area] + end;
19419 face_id = glyph->face_id;
19420 s->face = FACE_FROM_ID (s->f, face_id);
19421 s->font = s->face->font;
19422 s->width = glyph->pixel_width;
19423 s->nchars = 1;
19424 voffset = glyph->voffset;
19425
19426 for (++glyph;
19427 (glyph < last
19428 && glyph->type == STRETCH_GLYPH
19429 && glyph->voffset == voffset
19430 && glyph->face_id == face_id);
19431 ++glyph)
19432 s->width += glyph->pixel_width;
19433
19434 /* Adjust base line for subscript/superscript text. */
19435 s->ybase += voffset;
19436
19437 /* The case that face->gc == 0 is handled when drawing the glyph
19438 string by calling PREPARE_FACE_FOR_DISPLAY. */
19439 xassert (s->face);
19440 return glyph - s->row->glyphs[s->area];
19441 }
19442
19443 static struct font_metrics *
19444 get_per_char_metric (f, font, char2b)
19445 struct frame *f;
19446 struct font *font;
19447 XChar2b *char2b;
19448 {
19449 static struct font_metrics metrics;
19450 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19451 struct font *fontp;
19452
19453 if (! font || code == FONT_INVALID_CODE)
19454 return NULL;
19455 font->driver->text_extents (font, &code, 1, &metrics);
19456 return &metrics;
19457 }
19458
19459 /* EXPORT for RIF:
19460 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19461 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19462 assumed to be zero. */
19463
19464 void
19465 x_get_glyph_overhangs (glyph, f, left, right)
19466 struct glyph *glyph;
19467 struct frame *f;
19468 int *left, *right;
19469 {
19470 *left = *right = 0;
19471
19472 if (glyph->type == CHAR_GLYPH)
19473 {
19474 struct face *face;
19475 XChar2b char2b;
19476 struct font_metrics *pcm;
19477
19478 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19479 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19480 {
19481 if (pcm->rbearing > pcm->width)
19482 *right = pcm->rbearing - pcm->width;
19483 if (pcm->lbearing < 0)
19484 *left = -pcm->lbearing;
19485 }
19486 }
19487 else if (glyph->type == COMPOSITE_GLYPH)
19488 {
19489 struct composition *cmp = composition_table[glyph->u.cmp_id];
19490
19491 *right = cmp->rbearing - cmp->pixel_width;
19492 *left = - cmp->lbearing;
19493 }
19494 }
19495
19496
19497 /* Return the index of the first glyph preceding glyph string S that
19498 is overwritten by S because of S's left overhang. Value is -1
19499 if no glyphs are overwritten. */
19500
19501 static int
19502 left_overwritten (s)
19503 struct glyph_string *s;
19504 {
19505 int k;
19506
19507 if (s->left_overhang)
19508 {
19509 int x = 0, i;
19510 struct glyph *glyphs = s->row->glyphs[s->area];
19511 int first = s->first_glyph - glyphs;
19512
19513 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19514 x -= glyphs[i].pixel_width;
19515
19516 k = i + 1;
19517 }
19518 else
19519 k = -1;
19520
19521 return k;
19522 }
19523
19524
19525 /* Return the index of the first glyph preceding glyph string S that
19526 is overwriting S because of its right overhang. Value is -1 if no
19527 glyph in front of S overwrites S. */
19528
19529 static int
19530 left_overwriting (s)
19531 struct glyph_string *s;
19532 {
19533 int i, k, x;
19534 struct glyph *glyphs = s->row->glyphs[s->area];
19535 int first = s->first_glyph - glyphs;
19536
19537 k = -1;
19538 x = 0;
19539 for (i = first - 1; i >= 0; --i)
19540 {
19541 int left, right;
19542 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19543 if (x + right > 0)
19544 k = i;
19545 x -= glyphs[i].pixel_width;
19546 }
19547
19548 return k;
19549 }
19550
19551
19552 /* Return the index of the last glyph following glyph string S that is
19553 not overwritten by S because of S's right overhang. Value is -1 if
19554 no such glyph is found. */
19555
19556 static int
19557 right_overwritten (s)
19558 struct glyph_string *s;
19559 {
19560 int k = -1;
19561
19562 if (s->right_overhang)
19563 {
19564 int x = 0, i;
19565 struct glyph *glyphs = s->row->glyphs[s->area];
19566 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19567 int end = s->row->used[s->area];
19568
19569 for (i = first; i < end && s->right_overhang > x; ++i)
19570 x += glyphs[i].pixel_width;
19571
19572 k = i;
19573 }
19574
19575 return k;
19576 }
19577
19578
19579 /* Return the index of the last glyph following glyph string S that
19580 overwrites S because of its left overhang. Value is negative
19581 if no such glyph is found. */
19582
19583 static int
19584 right_overwriting (s)
19585 struct glyph_string *s;
19586 {
19587 int i, k, x;
19588 int end = s->row->used[s->area];
19589 struct glyph *glyphs = s->row->glyphs[s->area];
19590 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19591
19592 k = -1;
19593 x = 0;
19594 for (i = first; i < end; ++i)
19595 {
19596 int left, right;
19597 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19598 if (x - left < 0)
19599 k = i;
19600 x += glyphs[i].pixel_width;
19601 }
19602
19603 return k;
19604 }
19605
19606
19607 /* Set background width of glyph string S. START is the index of the
19608 first glyph following S. LAST_X is the right-most x-position + 1
19609 in the drawing area. */
19610
19611 static INLINE void
19612 set_glyph_string_background_width (s, start, last_x)
19613 struct glyph_string *s;
19614 int start;
19615 int last_x;
19616 {
19617 /* If the face of this glyph string has to be drawn to the end of
19618 the drawing area, set S->extends_to_end_of_line_p. */
19619
19620 if (start == s->row->used[s->area]
19621 && s->area == TEXT_AREA
19622 && ((s->row->fill_line_p
19623 && (s->hl == DRAW_NORMAL_TEXT
19624 || s->hl == DRAW_IMAGE_RAISED
19625 || s->hl == DRAW_IMAGE_SUNKEN))
19626 || s->hl == DRAW_MOUSE_FACE))
19627 s->extends_to_end_of_line_p = 1;
19628
19629 /* If S extends its face to the end of the line, set its
19630 background_width to the distance to the right edge of the drawing
19631 area. */
19632 if (s->extends_to_end_of_line_p)
19633 s->background_width = last_x - s->x + 1;
19634 else
19635 s->background_width = s->width;
19636 }
19637
19638
19639 /* Compute overhangs and x-positions for glyph string S and its
19640 predecessors, or successors. X is the starting x-position for S.
19641 BACKWARD_P non-zero means process predecessors. */
19642
19643 static void
19644 compute_overhangs_and_x (s, x, backward_p)
19645 struct glyph_string *s;
19646 int x;
19647 int backward_p;
19648 {
19649 if (backward_p)
19650 {
19651 while (s)
19652 {
19653 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19654 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19655 x -= s->width;
19656 s->x = x;
19657 s = s->prev;
19658 }
19659 }
19660 else
19661 {
19662 while (s)
19663 {
19664 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19665 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19666 s->x = x;
19667 x += s->width;
19668 s = s->next;
19669 }
19670 }
19671 }
19672
19673
19674
19675 /* The following macros are only called from draw_glyphs below.
19676 They reference the following parameters of that function directly:
19677 `w', `row', `area', and `overlap_p'
19678 as well as the following local variables:
19679 `s', `f', and `hdc' (in W32) */
19680
19681 #ifdef HAVE_NTGUI
19682 /* On W32, silently add local `hdc' variable to argument list of
19683 init_glyph_string. */
19684 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19685 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19686 #else
19687 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19688 init_glyph_string (s, char2b, w, row, area, start, hl)
19689 #endif
19690
19691 /* Add a glyph string for a stretch glyph to the list of strings
19692 between HEAD and TAIL. START is the index of the stretch glyph in
19693 row area AREA of glyph row ROW. END is the index of the last glyph
19694 in that glyph row area. X is the current output position assigned
19695 to the new glyph string constructed. HL overrides that face of the
19696 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19697 is the right-most x-position of the drawing area. */
19698
19699 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19700 and below -- keep them on one line. */
19701 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19702 do \
19703 { \
19704 s = (struct glyph_string *) alloca (sizeof *s); \
19705 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19706 START = fill_stretch_glyph_string (s, row, area, START, END); \
19707 append_glyph_string (&HEAD, &TAIL, s); \
19708 s->x = (X); \
19709 } \
19710 while (0)
19711
19712
19713 /* Add a glyph string for an image glyph to the list of strings
19714 between HEAD and TAIL. START is the index of the image glyph in
19715 row area AREA of glyph row ROW. END is the index of the last glyph
19716 in that glyph row area. X is the current output position assigned
19717 to the new glyph string constructed. HL overrides that face of the
19718 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19719 is the right-most x-position of the drawing area. */
19720
19721 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19722 do \
19723 { \
19724 s = (struct glyph_string *) alloca (sizeof *s); \
19725 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19726 fill_image_glyph_string (s); \
19727 append_glyph_string (&HEAD, &TAIL, s); \
19728 ++START; \
19729 s->x = (X); \
19730 } \
19731 while (0)
19732
19733
19734 /* Add a glyph string for a sequence of character glyphs to the list
19735 of strings between HEAD and TAIL. START is the index of the first
19736 glyph in row area AREA of glyph row ROW that is part of the new
19737 glyph string. END is the index of the last glyph in that glyph row
19738 area. X is the current output position assigned to the new glyph
19739 string constructed. HL overrides that face of the glyph; e.g. it
19740 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19741 right-most x-position of the drawing area. */
19742
19743 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19744 do \
19745 { \
19746 int face_id; \
19747 XChar2b *char2b; \
19748 \
19749 face_id = (row)->glyphs[area][START].face_id; \
19750 \
19751 s = (struct glyph_string *) alloca (sizeof *s); \
19752 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19753 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19754 append_glyph_string (&HEAD, &TAIL, s); \
19755 s->x = (X); \
19756 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19757 } \
19758 while (0)
19759
19760
19761 /* Add a glyph string for a composite sequence to the list of strings
19762 between HEAD and TAIL. START is the index of the first glyph in
19763 row area AREA of glyph row ROW that is part of the new glyph
19764 string. END is the index of the last glyph in that glyph row area.
19765 X is the current output position assigned to the new glyph string
19766 constructed. HL overrides that face of the glyph; e.g. it is
19767 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19768 x-position of the drawing area. */
19769
19770 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19771 do { \
19772 int face_id = (row)->glyphs[area][START].face_id; \
19773 struct face *base_face = FACE_FROM_ID (f, face_id); \
19774 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19775 struct composition *cmp = composition_table[cmp_id]; \
19776 XChar2b *char2b; \
19777 struct glyph_string *first_s; \
19778 int n; \
19779 \
19780 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19781 \
19782 /* Make glyph_strings for each glyph sequence that is drawable by \
19783 the same face, and append them to HEAD/TAIL. */ \
19784 for (n = 0; n < cmp->glyph_len;) \
19785 { \
19786 s = (struct glyph_string *) alloca (sizeof *s); \
19787 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19788 append_glyph_string (&(HEAD), &(TAIL), s); \
19789 s->cmp = cmp; \
19790 s->gidx = n; \
19791 s->x = (X); \
19792 if (n == 0) \
19793 first_s = s; \
19794 n = fill_composite_glyph_string (s, base_face, overlaps); \
19795 } \
19796 \
19797 ++START; \
19798 s = first_s; \
19799 } while (0)
19800
19801
19802 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19803 of AREA of glyph row ROW on window W between indices START and END.
19804 HL overrides the face for drawing glyph strings, e.g. it is
19805 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19806 x-positions of the drawing area.
19807
19808 This is an ugly monster macro construct because we must use alloca
19809 to allocate glyph strings (because draw_glyphs can be called
19810 asynchronously). */
19811
19812 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19813 do \
19814 { \
19815 HEAD = TAIL = NULL; \
19816 while (START < END) \
19817 { \
19818 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19819 switch (first_glyph->type) \
19820 { \
19821 case CHAR_GLYPH: \
19822 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19823 HL, X, LAST_X); \
19824 break; \
19825 \
19826 case COMPOSITE_GLYPH: \
19827 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19828 HL, X, LAST_X); \
19829 break; \
19830 \
19831 case STRETCH_GLYPH: \
19832 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19833 HL, X, LAST_X); \
19834 break; \
19835 \
19836 case IMAGE_GLYPH: \
19837 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19838 HL, X, LAST_X); \
19839 break; \
19840 \
19841 default: \
19842 abort (); \
19843 } \
19844 \
19845 if (s) \
19846 { \
19847 set_glyph_string_background_width (s, START, LAST_X); \
19848 (X) += s->width; \
19849 } \
19850 } \
19851 } \
19852 while (0)
19853
19854
19855 /* Draw glyphs between START and END in AREA of ROW on window W,
19856 starting at x-position X. X is relative to AREA in W. HL is a
19857 face-override with the following meaning:
19858
19859 DRAW_NORMAL_TEXT draw normally
19860 DRAW_CURSOR draw in cursor face
19861 DRAW_MOUSE_FACE draw in mouse face.
19862 DRAW_INVERSE_VIDEO draw in mode line face
19863 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19864 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19865
19866 If OVERLAPS is non-zero, draw only the foreground of characters and
19867 clip to the physical height of ROW. Non-zero value also defines
19868 the overlapping part to be drawn:
19869
19870 OVERLAPS_PRED overlap with preceding rows
19871 OVERLAPS_SUCC overlap with succeeding rows
19872 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19873 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19874
19875 Value is the x-position reached, relative to AREA of W. */
19876
19877 static int
19878 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19879 struct window *w;
19880 int x;
19881 struct glyph_row *row;
19882 enum glyph_row_area area;
19883 EMACS_INT start, end;
19884 enum draw_glyphs_face hl;
19885 int overlaps;
19886 {
19887 struct glyph_string *head, *tail;
19888 struct glyph_string *s;
19889 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19890 int last_x, area_width;
19891 int x_reached;
19892 int i, j;
19893 struct frame *f = XFRAME (WINDOW_FRAME (w));
19894 DECLARE_HDC (hdc);
19895
19896 ALLOCATE_HDC (hdc, f);
19897
19898 /* Let's rather be paranoid than getting a SEGV. */
19899 end = min (end, row->used[area]);
19900 start = max (0, start);
19901 start = min (end, start);
19902
19903 /* Translate X to frame coordinates. Set last_x to the right
19904 end of the drawing area. */
19905 if (row->full_width_p)
19906 {
19907 /* X is relative to the left edge of W, without scroll bars
19908 or fringes. */
19909 x += WINDOW_LEFT_EDGE_X (w);
19910 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19911 }
19912 else
19913 {
19914 int area_left = window_box_left (w, area);
19915 x += area_left;
19916 area_width = window_box_width (w, area);
19917 last_x = area_left + area_width;
19918 }
19919
19920 /* Build a doubly-linked list of glyph_string structures between
19921 head and tail from what we have to draw. Note that the macro
19922 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19923 the reason we use a separate variable `i'. */
19924 i = start;
19925 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19926 if (tail)
19927 x_reached = tail->x + tail->background_width;
19928 else
19929 x_reached = x;
19930
19931 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19932 the row, redraw some glyphs in front or following the glyph
19933 strings built above. */
19934 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19935 {
19936 int dummy_x = 0;
19937 struct glyph_string *h, *t;
19938
19939 /* Compute overhangs for all glyph strings. */
19940 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19941 for (s = head; s; s = s->next)
19942 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19943
19944 /* Prepend glyph strings for glyphs in front of the first glyph
19945 string that are overwritten because of the first glyph
19946 string's left overhang. The background of all strings
19947 prepended must be drawn because the first glyph string
19948 draws over it. */
19949 i = left_overwritten (head);
19950 if (i >= 0)
19951 {
19952 j = i;
19953 BUILD_GLYPH_STRINGS (j, start, h, t,
19954 DRAW_NORMAL_TEXT, dummy_x, last_x);
19955 start = i;
19956 compute_overhangs_and_x (t, head->x, 1);
19957 prepend_glyph_string_lists (&head, &tail, h, t);
19958 clip_head = head;
19959 }
19960
19961 /* Prepend glyph strings for glyphs in front of the first glyph
19962 string that overwrite that glyph string because of their
19963 right overhang. For these strings, only the foreground must
19964 be drawn, because it draws over the glyph string at `head'.
19965 The background must not be drawn because this would overwrite
19966 right overhangs of preceding glyphs for which no glyph
19967 strings exist. */
19968 i = left_overwriting (head);
19969 if (i >= 0)
19970 {
19971 clip_head = head;
19972 BUILD_GLYPH_STRINGS (i, start, h, t,
19973 DRAW_NORMAL_TEXT, dummy_x, last_x);
19974 for (s = h; s; s = s->next)
19975 s->background_filled_p = 1;
19976 compute_overhangs_and_x (t, head->x, 1);
19977 prepend_glyph_string_lists (&head, &tail, h, t);
19978 }
19979
19980 /* Append glyphs strings for glyphs following the last glyph
19981 string tail that are overwritten by tail. The background of
19982 these strings has to be drawn because tail's foreground draws
19983 over it. */
19984 i = right_overwritten (tail);
19985 if (i >= 0)
19986 {
19987 BUILD_GLYPH_STRINGS (end, i, h, t,
19988 DRAW_NORMAL_TEXT, x, last_x);
19989 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19990 append_glyph_string_lists (&head, &tail, h, t);
19991 clip_tail = tail;
19992 }
19993
19994 /* Append glyph strings for glyphs following the last glyph
19995 string tail that overwrite tail. The foreground of such
19996 glyphs has to be drawn because it writes into the background
19997 of tail. The background must not be drawn because it could
19998 paint over the foreground of following glyphs. */
19999 i = right_overwriting (tail);
20000 if (i >= 0)
20001 {
20002 clip_tail = tail;
20003 i++; /* We must include the Ith glyph. */
20004 BUILD_GLYPH_STRINGS (end, i, h, t,
20005 DRAW_NORMAL_TEXT, x, last_x);
20006 for (s = h; s; s = s->next)
20007 s->background_filled_p = 1;
20008 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20009 append_glyph_string_lists (&head, &tail, h, t);
20010 }
20011 if (clip_head || clip_tail)
20012 for (s = head; s; s = s->next)
20013 {
20014 s->clip_head = clip_head;
20015 s->clip_tail = clip_tail;
20016 }
20017 }
20018
20019 /* Draw all strings. */
20020 for (s = head; s; s = s->next)
20021 FRAME_RIF (f)->draw_glyph_string (s);
20022
20023 if (area == TEXT_AREA
20024 && !row->full_width_p
20025 /* When drawing overlapping rows, only the glyph strings'
20026 foreground is drawn, which doesn't erase a cursor
20027 completely. */
20028 && !overlaps)
20029 {
20030 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20031 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20032 : (tail ? tail->x + tail->background_width : x));
20033
20034 int text_left = window_box_left (w, TEXT_AREA);
20035 x0 -= text_left;
20036 x1 -= text_left;
20037
20038 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20039 row->y, MATRIX_ROW_BOTTOM_Y (row));
20040 }
20041
20042 /* Value is the x-position up to which drawn, relative to AREA of W.
20043 This doesn't include parts drawn because of overhangs. */
20044 if (row->full_width_p)
20045 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20046 else
20047 x_reached -= window_box_left (w, area);
20048
20049 RELEASE_HDC (hdc, f);
20050
20051 return x_reached;
20052 }
20053
20054 /* Expand row matrix if too narrow. Don't expand if area
20055 is not present. */
20056
20057 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20058 { \
20059 if (!fonts_changed_p \
20060 && (it->glyph_row->glyphs[area] \
20061 < it->glyph_row->glyphs[area + 1])) \
20062 { \
20063 it->w->ncols_scale_factor++; \
20064 fonts_changed_p = 1; \
20065 } \
20066 }
20067
20068 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20069 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20070
20071 static INLINE void
20072 append_glyph (it)
20073 struct it *it;
20074 {
20075 struct glyph *glyph;
20076 enum glyph_row_area area = it->area;
20077
20078 xassert (it->glyph_row);
20079 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20080
20081 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20082 if (glyph < it->glyph_row->glyphs[area + 1])
20083 {
20084 glyph->charpos = CHARPOS (it->position);
20085 glyph->object = it->object;
20086 if (it->pixel_width > 0)
20087 {
20088 glyph->pixel_width = it->pixel_width;
20089 glyph->padding_p = 0;
20090 }
20091 else
20092 {
20093 /* Assure at least 1-pixel width. Otherwise, cursor can't
20094 be displayed correctly. */
20095 glyph->pixel_width = 1;
20096 glyph->padding_p = 1;
20097 }
20098 glyph->ascent = it->ascent;
20099 glyph->descent = it->descent;
20100 glyph->voffset = it->voffset;
20101 glyph->type = CHAR_GLYPH;
20102 glyph->multibyte_p = it->multibyte_p;
20103 glyph->left_box_line_p = it->start_of_box_run_p;
20104 glyph->right_box_line_p = it->end_of_box_run_p;
20105 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20106 || it->phys_descent > it->descent);
20107 glyph->glyph_not_available_p = it->glyph_not_available_p;
20108 glyph->face_id = it->face_id;
20109 glyph->u.ch = it->char_to_display;
20110 glyph->slice = null_glyph_slice;
20111 glyph->font_type = FONT_TYPE_UNKNOWN;
20112 ++it->glyph_row->used[area];
20113 }
20114 else
20115 IT_EXPAND_MATRIX_WIDTH (it, area);
20116 }
20117
20118 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20119 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20120
20121 static INLINE void
20122 append_composite_glyph (it)
20123 struct it *it;
20124 {
20125 struct glyph *glyph;
20126 enum glyph_row_area area = it->area;
20127
20128 xassert (it->glyph_row);
20129
20130 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20131 if (glyph < it->glyph_row->glyphs[area + 1])
20132 {
20133 glyph->charpos = CHARPOS (it->position);
20134 glyph->object = it->object;
20135 glyph->pixel_width = it->pixel_width;
20136 glyph->ascent = it->ascent;
20137 glyph->descent = it->descent;
20138 glyph->voffset = it->voffset;
20139 glyph->type = COMPOSITE_GLYPH;
20140 glyph->multibyte_p = it->multibyte_p;
20141 glyph->left_box_line_p = it->start_of_box_run_p;
20142 glyph->right_box_line_p = it->end_of_box_run_p;
20143 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20144 || it->phys_descent > it->descent);
20145 glyph->padding_p = 0;
20146 glyph->glyph_not_available_p = 0;
20147 glyph->face_id = it->face_id;
20148 glyph->u.cmp_id = it->cmp_id;
20149 glyph->slice = null_glyph_slice;
20150 glyph->font_type = FONT_TYPE_UNKNOWN;
20151 ++it->glyph_row->used[area];
20152 }
20153 else
20154 IT_EXPAND_MATRIX_WIDTH (it, area);
20155 }
20156
20157
20158 /* Change IT->ascent and IT->height according to the setting of
20159 IT->voffset. */
20160
20161 static INLINE void
20162 take_vertical_position_into_account (it)
20163 struct it *it;
20164 {
20165 if (it->voffset)
20166 {
20167 if (it->voffset < 0)
20168 /* Increase the ascent so that we can display the text higher
20169 in the line. */
20170 it->ascent -= it->voffset;
20171 else
20172 /* Increase the descent so that we can display the text lower
20173 in the line. */
20174 it->descent += it->voffset;
20175 }
20176 }
20177
20178
20179 /* Produce glyphs/get display metrics for the image IT is loaded with.
20180 See the description of struct display_iterator in dispextern.h for
20181 an overview of struct display_iterator. */
20182
20183 static void
20184 produce_image_glyph (it)
20185 struct it *it;
20186 {
20187 struct image *img;
20188 struct face *face;
20189 int glyph_ascent, crop;
20190 struct glyph_slice slice;
20191
20192 xassert (it->what == IT_IMAGE);
20193
20194 face = FACE_FROM_ID (it->f, it->face_id);
20195 xassert (face);
20196 /* Make sure X resources of the face is loaded. */
20197 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20198
20199 if (it->image_id < 0)
20200 {
20201 /* Fringe bitmap. */
20202 it->ascent = it->phys_ascent = 0;
20203 it->descent = it->phys_descent = 0;
20204 it->pixel_width = 0;
20205 it->nglyphs = 0;
20206 return;
20207 }
20208
20209 img = IMAGE_FROM_ID (it->f, it->image_id);
20210 xassert (img);
20211 /* Make sure X resources of the image is loaded. */
20212 prepare_image_for_display (it->f, img);
20213
20214 slice.x = slice.y = 0;
20215 slice.width = img->width;
20216 slice.height = img->height;
20217
20218 if (INTEGERP (it->slice.x))
20219 slice.x = XINT (it->slice.x);
20220 else if (FLOATP (it->slice.x))
20221 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20222
20223 if (INTEGERP (it->slice.y))
20224 slice.y = XINT (it->slice.y);
20225 else if (FLOATP (it->slice.y))
20226 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20227
20228 if (INTEGERP (it->slice.width))
20229 slice.width = XINT (it->slice.width);
20230 else if (FLOATP (it->slice.width))
20231 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20232
20233 if (INTEGERP (it->slice.height))
20234 slice.height = XINT (it->slice.height);
20235 else if (FLOATP (it->slice.height))
20236 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20237
20238 if (slice.x >= img->width)
20239 slice.x = img->width;
20240 if (slice.y >= img->height)
20241 slice.y = img->height;
20242 if (slice.x + slice.width >= img->width)
20243 slice.width = img->width - slice.x;
20244 if (slice.y + slice.height > img->height)
20245 slice.height = img->height - slice.y;
20246
20247 if (slice.width == 0 || slice.height == 0)
20248 return;
20249
20250 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20251
20252 it->descent = slice.height - glyph_ascent;
20253 if (slice.y == 0)
20254 it->descent += img->vmargin;
20255 if (slice.y + slice.height == img->height)
20256 it->descent += img->vmargin;
20257 it->phys_descent = it->descent;
20258
20259 it->pixel_width = slice.width;
20260 if (slice.x == 0)
20261 it->pixel_width += img->hmargin;
20262 if (slice.x + slice.width == img->width)
20263 it->pixel_width += img->hmargin;
20264
20265 /* It's quite possible for images to have an ascent greater than
20266 their height, so don't get confused in that case. */
20267 if (it->descent < 0)
20268 it->descent = 0;
20269
20270 #if 0 /* this breaks image tiling */
20271 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20272 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20273 if (face_ascent > it->ascent)
20274 it->ascent = it->phys_ascent = face_ascent;
20275 #endif
20276
20277 it->nglyphs = 1;
20278
20279 if (face->box != FACE_NO_BOX)
20280 {
20281 if (face->box_line_width > 0)
20282 {
20283 if (slice.y == 0)
20284 it->ascent += face->box_line_width;
20285 if (slice.y + slice.height == img->height)
20286 it->descent += face->box_line_width;
20287 }
20288
20289 if (it->start_of_box_run_p && slice.x == 0)
20290 it->pixel_width += eabs (face->box_line_width);
20291 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20292 it->pixel_width += eabs (face->box_line_width);
20293 }
20294
20295 take_vertical_position_into_account (it);
20296
20297 /* Automatically crop wide image glyphs at right edge so we can
20298 draw the cursor on same display row. */
20299 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20300 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20301 {
20302 it->pixel_width -= crop;
20303 slice.width -= crop;
20304 }
20305
20306 if (it->glyph_row)
20307 {
20308 struct glyph *glyph;
20309 enum glyph_row_area area = it->area;
20310
20311 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20312 if (glyph < it->glyph_row->glyphs[area + 1])
20313 {
20314 glyph->charpos = CHARPOS (it->position);
20315 glyph->object = it->object;
20316 glyph->pixel_width = it->pixel_width;
20317 glyph->ascent = glyph_ascent;
20318 glyph->descent = it->descent;
20319 glyph->voffset = it->voffset;
20320 glyph->type = IMAGE_GLYPH;
20321 glyph->multibyte_p = it->multibyte_p;
20322 glyph->left_box_line_p = it->start_of_box_run_p;
20323 glyph->right_box_line_p = it->end_of_box_run_p;
20324 glyph->overlaps_vertically_p = 0;
20325 glyph->padding_p = 0;
20326 glyph->glyph_not_available_p = 0;
20327 glyph->face_id = it->face_id;
20328 glyph->u.img_id = img->id;
20329 glyph->slice = slice;
20330 glyph->font_type = FONT_TYPE_UNKNOWN;
20331 ++it->glyph_row->used[area];
20332 }
20333 else
20334 IT_EXPAND_MATRIX_WIDTH (it, area);
20335 }
20336 }
20337
20338
20339 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20340 of the glyph, WIDTH and HEIGHT are the width and height of the
20341 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20342
20343 static void
20344 append_stretch_glyph (it, object, width, height, ascent)
20345 struct it *it;
20346 Lisp_Object object;
20347 int width, height;
20348 int ascent;
20349 {
20350 struct glyph *glyph;
20351 enum glyph_row_area area = it->area;
20352
20353 xassert (ascent >= 0 && ascent <= height);
20354
20355 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20356 if (glyph < it->glyph_row->glyphs[area + 1])
20357 {
20358 glyph->charpos = CHARPOS (it->position);
20359 glyph->object = object;
20360 glyph->pixel_width = width;
20361 glyph->ascent = ascent;
20362 glyph->descent = height - ascent;
20363 glyph->voffset = it->voffset;
20364 glyph->type = STRETCH_GLYPH;
20365 glyph->multibyte_p = it->multibyte_p;
20366 glyph->left_box_line_p = it->start_of_box_run_p;
20367 glyph->right_box_line_p = it->end_of_box_run_p;
20368 glyph->overlaps_vertically_p = 0;
20369 glyph->padding_p = 0;
20370 glyph->glyph_not_available_p = 0;
20371 glyph->face_id = it->face_id;
20372 glyph->u.stretch.ascent = ascent;
20373 glyph->u.stretch.height = height;
20374 glyph->slice = null_glyph_slice;
20375 glyph->font_type = FONT_TYPE_UNKNOWN;
20376 ++it->glyph_row->used[area];
20377 }
20378 else
20379 IT_EXPAND_MATRIX_WIDTH (it, area);
20380 }
20381
20382
20383 /* Produce a stretch glyph for iterator IT. IT->object is the value
20384 of the glyph property displayed. The value must be a list
20385 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20386 being recognized:
20387
20388 1. `:width WIDTH' specifies that the space should be WIDTH *
20389 canonical char width wide. WIDTH may be an integer or floating
20390 point number.
20391
20392 2. `:relative-width FACTOR' specifies that the width of the stretch
20393 should be computed from the width of the first character having the
20394 `glyph' property, and should be FACTOR times that width.
20395
20396 3. `:align-to HPOS' specifies that the space should be wide enough
20397 to reach HPOS, a value in canonical character units.
20398
20399 Exactly one of the above pairs must be present.
20400
20401 4. `:height HEIGHT' specifies that the height of the stretch produced
20402 should be HEIGHT, measured in canonical character units.
20403
20404 5. `:relative-height FACTOR' specifies that the height of the
20405 stretch should be FACTOR times the height of the characters having
20406 the glyph property.
20407
20408 Either none or exactly one of 4 or 5 must be present.
20409
20410 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20411 of the stretch should be used for the ascent of the stretch.
20412 ASCENT must be in the range 0 <= ASCENT <= 100. */
20413
20414 static void
20415 produce_stretch_glyph (it)
20416 struct it *it;
20417 {
20418 /* (space :width WIDTH :height HEIGHT ...) */
20419 Lisp_Object prop, plist;
20420 int width = 0, height = 0, align_to = -1;
20421 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20422 int ascent = 0;
20423 double tem;
20424 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20425 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20426
20427 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20428
20429 /* List should start with `space'. */
20430 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20431 plist = XCDR (it->object);
20432
20433 /* Compute the width of the stretch. */
20434 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20435 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20436 {
20437 /* Absolute width `:width WIDTH' specified and valid. */
20438 zero_width_ok_p = 1;
20439 width = (int)tem;
20440 }
20441 else if (prop = Fplist_get (plist, QCrelative_width),
20442 NUMVAL (prop) > 0)
20443 {
20444 /* Relative width `:relative-width FACTOR' specified and valid.
20445 Compute the width of the characters having the `glyph'
20446 property. */
20447 struct it it2;
20448 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20449
20450 it2 = *it;
20451 if (it->multibyte_p)
20452 {
20453 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20454 - IT_BYTEPOS (*it));
20455 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20456 }
20457 else
20458 it2.c = *p, it2.len = 1;
20459
20460 it2.glyph_row = NULL;
20461 it2.what = IT_CHARACTER;
20462 x_produce_glyphs (&it2);
20463 width = NUMVAL (prop) * it2.pixel_width;
20464 }
20465 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20466 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20467 {
20468 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20469 align_to = (align_to < 0
20470 ? 0
20471 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20472 else if (align_to < 0)
20473 align_to = window_box_left_offset (it->w, TEXT_AREA);
20474 width = max (0, (int)tem + align_to - it->current_x);
20475 zero_width_ok_p = 1;
20476 }
20477 else
20478 /* Nothing specified -> width defaults to canonical char width. */
20479 width = FRAME_COLUMN_WIDTH (it->f);
20480
20481 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20482 width = 1;
20483
20484 /* Compute height. */
20485 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20486 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20487 {
20488 height = (int)tem;
20489 zero_height_ok_p = 1;
20490 }
20491 else if (prop = Fplist_get (plist, QCrelative_height),
20492 NUMVAL (prop) > 0)
20493 height = FONT_HEIGHT (font) * NUMVAL (prop);
20494 else
20495 height = FONT_HEIGHT (font);
20496
20497 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20498 height = 1;
20499
20500 /* Compute percentage of height used for ascent. If
20501 `:ascent ASCENT' is present and valid, use that. Otherwise,
20502 derive the ascent from the font in use. */
20503 if (prop = Fplist_get (plist, QCascent),
20504 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20505 ascent = height * NUMVAL (prop) / 100.0;
20506 else if (!NILP (prop)
20507 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20508 ascent = min (max (0, (int)tem), height);
20509 else
20510 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20511
20512 if (width > 0 && !it->truncate_lines_p
20513 && it->current_x + width > it->last_visible_x)
20514 width = it->last_visible_x - it->current_x - 1;
20515
20516 if (width > 0 && height > 0 && it->glyph_row)
20517 {
20518 Lisp_Object object = it->stack[it->sp - 1].string;
20519 if (!STRINGP (object))
20520 object = it->w->buffer;
20521 append_stretch_glyph (it, object, width, height, ascent);
20522 }
20523
20524 it->pixel_width = width;
20525 it->ascent = it->phys_ascent = ascent;
20526 it->descent = it->phys_descent = height - it->ascent;
20527 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20528
20529 take_vertical_position_into_account (it);
20530 }
20531
20532 /* Get line-height and line-spacing property at point.
20533 If line-height has format (HEIGHT TOTAL), return TOTAL
20534 in TOTAL_HEIGHT. */
20535
20536 static Lisp_Object
20537 get_line_height_property (it, prop)
20538 struct it *it;
20539 Lisp_Object prop;
20540 {
20541 Lisp_Object position;
20542
20543 if (STRINGP (it->object))
20544 position = make_number (IT_STRING_CHARPOS (*it));
20545 else if (BUFFERP (it->object))
20546 position = make_number (IT_CHARPOS (*it));
20547 else
20548 return Qnil;
20549
20550 return Fget_char_property (position, prop, it->object);
20551 }
20552
20553 /* Calculate line-height and line-spacing properties.
20554 An integer value specifies explicit pixel value.
20555 A float value specifies relative value to current face height.
20556 A cons (float . face-name) specifies relative value to
20557 height of specified face font.
20558
20559 Returns height in pixels, or nil. */
20560
20561
20562 static Lisp_Object
20563 calc_line_height_property (it, val, font, boff, override)
20564 struct it *it;
20565 Lisp_Object val;
20566 struct font *font;
20567 int boff, override;
20568 {
20569 Lisp_Object face_name = Qnil;
20570 int ascent, descent, height;
20571
20572 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20573 return val;
20574
20575 if (CONSP (val))
20576 {
20577 face_name = XCAR (val);
20578 val = XCDR (val);
20579 if (!NUMBERP (val))
20580 val = make_number (1);
20581 if (NILP (face_name))
20582 {
20583 height = it->ascent + it->descent;
20584 goto scale;
20585 }
20586 }
20587
20588 if (NILP (face_name))
20589 {
20590 font = FRAME_FONT (it->f);
20591 boff = FRAME_BASELINE_OFFSET (it->f);
20592 }
20593 else if (EQ (face_name, Qt))
20594 {
20595 override = 0;
20596 }
20597 else
20598 {
20599 int face_id;
20600 struct face *face;
20601
20602 face_id = lookup_named_face (it->f, face_name, 0);
20603 if (face_id < 0)
20604 return make_number (-1);
20605
20606 face = FACE_FROM_ID (it->f, face_id);
20607 font = face->font;
20608 if (font == NULL)
20609 return make_number (-1);
20610 boff = font->baseline_offset;
20611 if (font->vertical_centering)
20612 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20613 }
20614
20615 ascent = FONT_BASE (font) + boff;
20616 descent = FONT_DESCENT (font) - boff;
20617
20618 if (override)
20619 {
20620 it->override_ascent = ascent;
20621 it->override_descent = descent;
20622 it->override_boff = boff;
20623 }
20624
20625 height = ascent + descent;
20626
20627 scale:
20628 if (FLOATP (val))
20629 height = (int)(XFLOAT_DATA (val) * height);
20630 else if (INTEGERP (val))
20631 height *= XINT (val);
20632
20633 return make_number (height);
20634 }
20635
20636
20637 /* RIF:
20638 Produce glyphs/get display metrics for the display element IT is
20639 loaded with. See the description of struct it in dispextern.h
20640 for an overview of struct it. */
20641
20642 void
20643 x_produce_glyphs (it)
20644 struct it *it;
20645 {
20646 int extra_line_spacing = it->extra_line_spacing;
20647
20648 it->glyph_not_available_p = 0;
20649
20650 if (it->what == IT_CHARACTER)
20651 {
20652 XChar2b char2b;
20653 struct font *font;
20654 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20655 struct font_metrics *pcm;
20656 int font_not_found_p;
20657 int boff; /* baseline offset */
20658 /* We may change it->multibyte_p upon unibyte<->multibyte
20659 conversion. So, save the current value now and restore it
20660 later.
20661
20662 Note: It seems that we don't have to record multibyte_p in
20663 struct glyph because the character code itself tells if or
20664 not the character is multibyte. Thus, in the future, we must
20665 consider eliminating the field `multibyte_p' in the struct
20666 glyph. */
20667 int saved_multibyte_p = it->multibyte_p;
20668
20669 /* Maybe translate single-byte characters to multibyte, or the
20670 other way. */
20671 it->char_to_display = it->c;
20672 if (!ASCII_BYTE_P (it->c)
20673 && ! it->multibyte_p)
20674 {
20675 if (SINGLE_BYTE_CHAR_P (it->c)
20676 && unibyte_display_via_language_environment)
20677 it->char_to_display = unibyte_char_to_multibyte (it->c);
20678 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20679 {
20680 it->multibyte_p = 1;
20681 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20682 -1, Qnil);
20683 face = FACE_FROM_ID (it->f, it->face_id);
20684 }
20685 }
20686
20687 /* Get font to use. Encode IT->char_to_display. */
20688 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20689 &char2b, it->multibyte_p, 0);
20690 font = face->font;
20691
20692 /* When no suitable font found, use the default font. */
20693 font_not_found_p = font == NULL;
20694 if (font_not_found_p)
20695 {
20696 font = FRAME_FONT (it->f);
20697 boff = FRAME_BASELINE_OFFSET (it->f);
20698 }
20699 else
20700 {
20701 boff = font->baseline_offset;
20702 if (font->vertical_centering)
20703 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20704 }
20705
20706 if (it->char_to_display >= ' '
20707 && (!it->multibyte_p || it->char_to_display < 128))
20708 {
20709 /* Either unibyte or ASCII. */
20710 int stretched_p;
20711
20712 it->nglyphs = 1;
20713
20714 pcm = get_per_char_metric (it->f, font, &char2b);
20715
20716 if (it->override_ascent >= 0)
20717 {
20718 it->ascent = it->override_ascent;
20719 it->descent = it->override_descent;
20720 boff = it->override_boff;
20721 }
20722 else
20723 {
20724 it->ascent = FONT_BASE (font) + boff;
20725 it->descent = FONT_DESCENT (font) - boff;
20726 }
20727
20728 if (pcm)
20729 {
20730 it->phys_ascent = pcm->ascent + boff;
20731 it->phys_descent = pcm->descent - boff;
20732 it->pixel_width = pcm->width;
20733 }
20734 else
20735 {
20736 it->glyph_not_available_p = 1;
20737 it->phys_ascent = it->ascent;
20738 it->phys_descent = it->descent;
20739 it->pixel_width = FONT_WIDTH (font);
20740 }
20741
20742 if (it->constrain_row_ascent_descent_p)
20743 {
20744 if (it->descent > it->max_descent)
20745 {
20746 it->ascent += it->descent - it->max_descent;
20747 it->descent = it->max_descent;
20748 }
20749 if (it->ascent > it->max_ascent)
20750 {
20751 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20752 it->ascent = it->max_ascent;
20753 }
20754 it->phys_ascent = min (it->phys_ascent, it->ascent);
20755 it->phys_descent = min (it->phys_descent, it->descent);
20756 extra_line_spacing = 0;
20757 }
20758
20759 /* If this is a space inside a region of text with
20760 `space-width' property, change its width. */
20761 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20762 if (stretched_p)
20763 it->pixel_width *= XFLOATINT (it->space_width);
20764
20765 /* If face has a box, add the box thickness to the character
20766 height. If character has a box line to the left and/or
20767 right, add the box line width to the character's width. */
20768 if (face->box != FACE_NO_BOX)
20769 {
20770 int thick = face->box_line_width;
20771
20772 if (thick > 0)
20773 {
20774 it->ascent += thick;
20775 it->descent += thick;
20776 }
20777 else
20778 thick = -thick;
20779
20780 if (it->start_of_box_run_p)
20781 it->pixel_width += thick;
20782 if (it->end_of_box_run_p)
20783 it->pixel_width += thick;
20784 }
20785
20786 /* If face has an overline, add the height of the overline
20787 (1 pixel) and a 1 pixel margin to the character height. */
20788 if (face->overline_p)
20789 it->ascent += overline_margin;
20790
20791 if (it->constrain_row_ascent_descent_p)
20792 {
20793 if (it->ascent > it->max_ascent)
20794 it->ascent = it->max_ascent;
20795 if (it->descent > it->max_descent)
20796 it->descent = it->max_descent;
20797 }
20798
20799 take_vertical_position_into_account (it);
20800
20801 /* If we have to actually produce glyphs, do it. */
20802 if (it->glyph_row)
20803 {
20804 if (stretched_p)
20805 {
20806 /* Translate a space with a `space-width' property
20807 into a stretch glyph. */
20808 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20809 / FONT_HEIGHT (font));
20810 append_stretch_glyph (it, it->object, it->pixel_width,
20811 it->ascent + it->descent, ascent);
20812 }
20813 else
20814 append_glyph (it);
20815
20816 /* If characters with lbearing or rbearing are displayed
20817 in this line, record that fact in a flag of the
20818 glyph row. This is used to optimize X output code. */
20819 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20820 it->glyph_row->contains_overlapping_glyphs_p = 1;
20821 }
20822 if (! stretched_p && it->pixel_width == 0)
20823 /* We assure that all visible glyphs have at least 1-pixel
20824 width. */
20825 it->pixel_width = 1;
20826 }
20827 else if (it->char_to_display == '\n')
20828 {
20829 /* A newline has no width but we need the height of the line.
20830 But if previous part of the line set a height, don't
20831 increase that height */
20832
20833 Lisp_Object height;
20834 Lisp_Object total_height = Qnil;
20835
20836 it->override_ascent = -1;
20837 it->pixel_width = 0;
20838 it->nglyphs = 0;
20839
20840 height = get_line_height_property(it, Qline_height);
20841 /* Split (line-height total-height) list */
20842 if (CONSP (height)
20843 && CONSP (XCDR (height))
20844 && NILP (XCDR (XCDR (height))))
20845 {
20846 total_height = XCAR (XCDR (height));
20847 height = XCAR (height);
20848 }
20849 height = calc_line_height_property(it, height, font, boff, 1);
20850
20851 if (it->override_ascent >= 0)
20852 {
20853 it->ascent = it->override_ascent;
20854 it->descent = it->override_descent;
20855 boff = it->override_boff;
20856 }
20857 else
20858 {
20859 it->ascent = FONT_BASE (font) + boff;
20860 it->descent = FONT_DESCENT (font) - boff;
20861 }
20862
20863 if (EQ (height, Qt))
20864 {
20865 if (it->descent > it->max_descent)
20866 {
20867 it->ascent += it->descent - it->max_descent;
20868 it->descent = it->max_descent;
20869 }
20870 if (it->ascent > it->max_ascent)
20871 {
20872 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20873 it->ascent = it->max_ascent;
20874 }
20875 it->phys_ascent = min (it->phys_ascent, it->ascent);
20876 it->phys_descent = min (it->phys_descent, it->descent);
20877 it->constrain_row_ascent_descent_p = 1;
20878 extra_line_spacing = 0;
20879 }
20880 else
20881 {
20882 Lisp_Object spacing;
20883
20884 it->phys_ascent = it->ascent;
20885 it->phys_descent = it->descent;
20886
20887 if ((it->max_ascent > 0 || it->max_descent > 0)
20888 && face->box != FACE_NO_BOX
20889 && face->box_line_width > 0)
20890 {
20891 it->ascent += face->box_line_width;
20892 it->descent += face->box_line_width;
20893 }
20894 if (!NILP (height)
20895 && XINT (height) > it->ascent + it->descent)
20896 it->ascent = XINT (height) - it->descent;
20897
20898 if (!NILP (total_height))
20899 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20900 else
20901 {
20902 spacing = get_line_height_property(it, Qline_spacing);
20903 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20904 }
20905 if (INTEGERP (spacing))
20906 {
20907 extra_line_spacing = XINT (spacing);
20908 if (!NILP (total_height))
20909 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20910 }
20911 }
20912 }
20913 else if (it->char_to_display == '\t')
20914 {
20915 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20916 int x = it->current_x + it->continuation_lines_width;
20917 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20918
20919 /* If the distance from the current position to the next tab
20920 stop is less than a space character width, use the
20921 tab stop after that. */
20922 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20923 next_tab_x += tab_width;
20924
20925 it->pixel_width = next_tab_x - x;
20926 it->nglyphs = 1;
20927 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20928 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20929
20930 if (it->glyph_row)
20931 {
20932 append_stretch_glyph (it, it->object, it->pixel_width,
20933 it->ascent + it->descent, it->ascent);
20934 }
20935 }
20936 else
20937 {
20938 /* A multi-byte character. Assume that the display width of the
20939 character is the width of the character multiplied by the
20940 width of the font. */
20941
20942 /* If we found a font, this font should give us the right
20943 metrics. If we didn't find a font, use the frame's
20944 default font and calculate the width of the character by
20945 multiplying the width of font by the width of the
20946 character. */
20947
20948 pcm = get_per_char_metric (it->f, font, &char2b);
20949
20950 if (font_not_found_p || !pcm)
20951 {
20952 int char_width = CHAR_WIDTH (it->char_to_display);
20953
20954 if (char_width == 0)
20955 /* This is a non spacing character. But, as we are
20956 going to display an empty box, the box must occupy
20957 at least one column. */
20958 char_width = 1;
20959 it->glyph_not_available_p = 1;
20960 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
20961 it->phys_ascent = FONT_BASE (font) + boff;
20962 it->phys_descent = FONT_DESCENT (font) - boff;
20963 }
20964 else
20965 {
20966 it->pixel_width = pcm->width;
20967 it->phys_ascent = pcm->ascent + boff;
20968 it->phys_descent = pcm->descent - boff;
20969 if (it->glyph_row
20970 && (pcm->lbearing < 0
20971 || pcm->rbearing > pcm->width))
20972 it->glyph_row->contains_overlapping_glyphs_p = 1;
20973 }
20974 it->nglyphs = 1;
20975 it->ascent = FONT_BASE (font) + boff;
20976 it->descent = FONT_DESCENT (font) - boff;
20977 if (face->box != FACE_NO_BOX)
20978 {
20979 int thick = face->box_line_width;
20980
20981 if (thick > 0)
20982 {
20983 it->ascent += thick;
20984 it->descent += thick;
20985 }
20986 else
20987 thick = - thick;
20988
20989 if (it->start_of_box_run_p)
20990 it->pixel_width += thick;
20991 if (it->end_of_box_run_p)
20992 it->pixel_width += thick;
20993 }
20994
20995 /* If face has an overline, add the height of the overline
20996 (1 pixel) and a 1 pixel margin to the character height. */
20997 if (face->overline_p)
20998 it->ascent += overline_margin;
20999
21000 take_vertical_position_into_account (it);
21001
21002 if (it->ascent < 0)
21003 it->ascent = 0;
21004 if (it->descent < 0)
21005 it->descent = 0;
21006
21007 if (it->glyph_row)
21008 append_glyph (it);
21009 if (it->pixel_width == 0)
21010 /* We assure that all visible glyphs have at least 1-pixel
21011 width. */
21012 it->pixel_width = 1;
21013 }
21014 it->multibyte_p = saved_multibyte_p;
21015 }
21016 else if (it->what == IT_COMPOSITION)
21017 {
21018 /* Note: A composition is represented as one glyph in the
21019 glyph matrix. There are no padding glyphs.
21020
21021 Important is that pixel_width, ascent, and descent are the
21022 values of what is drawn by draw_glyphs (i.e. the values of
21023 the overall glyphs composed). */
21024 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21025 int boff; /* baseline offset */
21026 struct composition *cmp = composition_table[it->cmp_id];
21027 int glyph_len = cmp->glyph_len;
21028 struct font *font = face->font;
21029
21030 it->nglyphs = 1;
21031
21032 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21033 {
21034 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21035 font_prepare_composition (cmp, it->f);
21036 }
21037 else
21038 /* If we have not yet calculated pixel size data of glyphs of
21039 the composition for the current face font, calculate them
21040 now. Theoretically, we have to check all fonts for the
21041 glyphs, but that requires much time and memory space. So,
21042 here we check only the font of the first glyph. This leads
21043 to incorrect display, but it's very rare, and C-l (recenter)
21044 can correct the display anyway. */
21045 if (! cmp->font || cmp->font != font)
21046 {
21047 /* Ascent and descent of the font of the first character
21048 of this composition (adjusted by baseline offset).
21049 Ascent and descent of overall glyphs should not be less
21050 than them respectively. */
21051 int font_ascent, font_descent, font_height;
21052 /* Bounding box of the overall glyphs. */
21053 int leftmost, rightmost, lowest, highest;
21054 int lbearing, rbearing;
21055 int i, width, ascent, descent;
21056 int left_padded = 0, right_padded = 0;
21057 int face_id;
21058 int c;
21059 XChar2b char2b;
21060 struct font_metrics *pcm;
21061 int font_not_found_p;
21062 int pos;
21063
21064 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21065 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21066 break;
21067 if (glyph_len < cmp->glyph_len)
21068 right_padded = 1;
21069 for (i = 0; i < glyph_len; i++)
21070 {
21071 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21072 break;
21073 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21074 }
21075 if (i > 0)
21076 left_padded = 1;
21077
21078 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21079 : IT_CHARPOS (*it));
21080 /* When no suitable font found, use the default font. */
21081 font_not_found_p = font == NULL;
21082 if (font_not_found_p)
21083 {
21084 face = face->ascii_face;
21085 font = face->font;
21086 }
21087 boff = font->baseline_offset;
21088 if (font->vertical_centering)
21089 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21090 font_ascent = FONT_BASE (font) + boff;
21091 font_descent = FONT_DESCENT (font) - boff;
21092 font_height = FONT_HEIGHT (font);
21093
21094 cmp->font = (void *) font;
21095
21096 pcm = NULL;
21097 if (! font_not_found_p)
21098 {
21099 get_char_face_and_encoding (it->f, c, it->face_id,
21100 &char2b, it->multibyte_p, 0);
21101 pcm = get_per_char_metric (it->f, font, &char2b);
21102 }
21103
21104 /* Initialize the bounding box. */
21105 if (pcm)
21106 {
21107 width = pcm->width;
21108 ascent = pcm->ascent;
21109 descent = pcm->descent;
21110 lbearing = pcm->lbearing;
21111 rbearing = pcm->rbearing;
21112 }
21113 else
21114 {
21115 width = FONT_WIDTH (font);
21116 ascent = FONT_BASE (font);
21117 descent = FONT_DESCENT (font);
21118 lbearing = 0;
21119 rbearing = width;
21120 }
21121
21122 rightmost = width;
21123 leftmost = 0;
21124 lowest = - descent + boff;
21125 highest = ascent + boff;
21126
21127 if (! font_not_found_p
21128 && font->default_ascent
21129 && CHAR_TABLE_P (Vuse_default_ascent)
21130 && !NILP (Faref (Vuse_default_ascent,
21131 make_number (it->char_to_display))))
21132 highest = font->default_ascent + boff;
21133
21134 /* Draw the first glyph at the normal position. It may be
21135 shifted to right later if some other glyphs are drawn
21136 at the left. */
21137 cmp->offsets[i * 2] = 0;
21138 cmp->offsets[i * 2 + 1] = boff;
21139 cmp->lbearing = lbearing;
21140 cmp->rbearing = rbearing;
21141
21142 /* Set cmp->offsets for the remaining glyphs. */
21143 for (i++; i < glyph_len; i++)
21144 {
21145 int left, right, btm, top;
21146 int ch = COMPOSITION_GLYPH (cmp, i);
21147 int face_id;
21148 struct face *this_face;
21149 int this_boff;
21150
21151 if (ch == '\t')
21152 ch = ' ';
21153 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21154 this_face = FACE_FROM_ID (it->f, face_id);
21155 font = this_face->font;
21156
21157 if (font == NULL)
21158 pcm = NULL;
21159 else
21160 {
21161 this_boff = font->baseline_offset;
21162 if (font->vertical_centering)
21163 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21164 get_char_face_and_encoding (it->f, ch, face_id,
21165 &char2b, it->multibyte_p, 0);
21166 pcm = get_per_char_metric (it->f, font, &char2b);
21167 }
21168 if (! pcm)
21169 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21170 else
21171 {
21172 width = pcm->width;
21173 ascent = pcm->ascent;
21174 descent = pcm->descent;
21175 lbearing = pcm->lbearing;
21176 rbearing = pcm->rbearing;
21177 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21178 {
21179 /* Relative composition with or without
21180 alternate chars. */
21181 left = (leftmost + rightmost - width) / 2;
21182 btm = - descent + boff;
21183 if (font->relative_compose
21184 && (! CHAR_TABLE_P (Vignore_relative_composition)
21185 || NILP (Faref (Vignore_relative_composition,
21186 make_number (ch)))))
21187 {
21188
21189 if (- descent >= font->relative_compose)
21190 /* One extra pixel between two glyphs. */
21191 btm = highest + 1;
21192 else if (ascent <= 0)
21193 /* One extra pixel between two glyphs. */
21194 btm = lowest - 1 - ascent - descent;
21195 }
21196 }
21197 else
21198 {
21199 /* A composition rule is specified by an integer
21200 value that encodes global and new reference
21201 points (GREF and NREF). GREF and NREF are
21202 specified by numbers as below:
21203
21204 0---1---2 -- ascent
21205 | |
21206 | |
21207 | |
21208 9--10--11 -- center
21209 | |
21210 ---3---4---5--- baseline
21211 | |
21212 6---7---8 -- descent
21213 */
21214 int rule = COMPOSITION_RULE (cmp, i);
21215 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21216
21217 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21218 grefx = gref % 3, nrefx = nref % 3;
21219 grefy = gref / 3, nrefy = nref / 3;
21220 if (xoff)
21221 xoff = font_height * (xoff - 128) / 256;
21222 if (yoff)
21223 yoff = font_height * (yoff - 128) / 256;
21224
21225 left = (leftmost
21226 + grefx * (rightmost - leftmost) / 2
21227 - nrefx * width / 2
21228 + xoff);
21229
21230 btm = ((grefy == 0 ? highest
21231 : grefy == 1 ? 0
21232 : grefy == 2 ? lowest
21233 : (highest + lowest) / 2)
21234 - (nrefy == 0 ? ascent + descent
21235 : nrefy == 1 ? descent - boff
21236 : nrefy == 2 ? 0
21237 : (ascent + descent) / 2)
21238 + yoff);
21239 }
21240
21241 cmp->offsets[i * 2] = left;
21242 cmp->offsets[i * 2 + 1] = btm + descent;
21243
21244 /* Update the bounding box of the overall glyphs. */
21245 if (width > 0)
21246 {
21247 right = left + width;
21248 if (left < leftmost)
21249 leftmost = left;
21250 if (right > rightmost)
21251 rightmost = right;
21252 }
21253 top = btm + descent + ascent;
21254 if (top > highest)
21255 highest = top;
21256 if (btm < lowest)
21257 lowest = btm;
21258
21259 if (cmp->lbearing > left + lbearing)
21260 cmp->lbearing = left + lbearing;
21261 if (cmp->rbearing < left + rbearing)
21262 cmp->rbearing = left + rbearing;
21263 }
21264 }
21265
21266 /* If there are glyphs whose x-offsets are negative,
21267 shift all glyphs to the right and make all x-offsets
21268 non-negative. */
21269 if (leftmost < 0)
21270 {
21271 for (i = 0; i < cmp->glyph_len; i++)
21272 cmp->offsets[i * 2] -= leftmost;
21273 rightmost -= leftmost;
21274 cmp->lbearing -= leftmost;
21275 cmp->rbearing -= leftmost;
21276 }
21277
21278 if (left_padded && cmp->lbearing < 0)
21279 {
21280 for (i = 0; i < cmp->glyph_len; i++)
21281 cmp->offsets[i * 2] -= cmp->lbearing;
21282 rightmost -= cmp->lbearing;
21283 cmp->rbearing -= cmp->lbearing;
21284 cmp->lbearing = 0;
21285 }
21286 if (right_padded && rightmost < cmp->rbearing)
21287 {
21288 rightmost = cmp->rbearing;
21289 }
21290
21291 cmp->pixel_width = rightmost;
21292 cmp->ascent = highest;
21293 cmp->descent = - lowest;
21294 if (cmp->ascent < font_ascent)
21295 cmp->ascent = font_ascent;
21296 if (cmp->descent < font_descent)
21297 cmp->descent = font_descent;
21298 }
21299
21300 if (it->glyph_row
21301 && (cmp->lbearing < 0
21302 || cmp->rbearing > cmp->pixel_width))
21303 it->glyph_row->contains_overlapping_glyphs_p = 1;
21304
21305 it->pixel_width = cmp->pixel_width;
21306 it->ascent = it->phys_ascent = cmp->ascent;
21307 it->descent = it->phys_descent = cmp->descent;
21308 if (face->box != FACE_NO_BOX)
21309 {
21310 int thick = face->box_line_width;
21311
21312 if (thick > 0)
21313 {
21314 it->ascent += thick;
21315 it->descent += thick;
21316 }
21317 else
21318 thick = - thick;
21319
21320 if (it->start_of_box_run_p)
21321 it->pixel_width += thick;
21322 if (it->end_of_box_run_p)
21323 it->pixel_width += thick;
21324 }
21325
21326 /* If face has an overline, add the height of the overline
21327 (1 pixel) and a 1 pixel margin to the character height. */
21328 if (face->overline_p)
21329 it->ascent += overline_margin;
21330
21331 take_vertical_position_into_account (it);
21332 if (it->ascent < 0)
21333 it->ascent = 0;
21334 if (it->descent < 0)
21335 it->descent = 0;
21336
21337 if (it->glyph_row)
21338 append_composite_glyph (it);
21339 }
21340 else if (it->what == IT_IMAGE)
21341 produce_image_glyph (it);
21342 else if (it->what == IT_STRETCH)
21343 produce_stretch_glyph (it);
21344
21345 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21346 because this isn't true for images with `:ascent 100'. */
21347 xassert (it->ascent >= 0 && it->descent >= 0);
21348 if (it->area == TEXT_AREA)
21349 it->current_x += it->pixel_width;
21350
21351 if (extra_line_spacing > 0)
21352 {
21353 it->descent += extra_line_spacing;
21354 if (extra_line_spacing > it->max_extra_line_spacing)
21355 it->max_extra_line_spacing = extra_line_spacing;
21356 }
21357
21358 it->max_ascent = max (it->max_ascent, it->ascent);
21359 it->max_descent = max (it->max_descent, it->descent);
21360 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21361 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21362 }
21363
21364 /* EXPORT for RIF:
21365 Output LEN glyphs starting at START at the nominal cursor position.
21366 Advance the nominal cursor over the text. The global variable
21367 updated_window contains the window being updated, updated_row is
21368 the glyph row being updated, and updated_area is the area of that
21369 row being updated. */
21370
21371 void
21372 x_write_glyphs (start, len)
21373 struct glyph *start;
21374 int len;
21375 {
21376 int x, hpos;
21377
21378 xassert (updated_window && updated_row);
21379 BLOCK_INPUT;
21380
21381 /* Write glyphs. */
21382
21383 hpos = start - updated_row->glyphs[updated_area];
21384 x = draw_glyphs (updated_window, output_cursor.x,
21385 updated_row, updated_area,
21386 hpos, hpos + len,
21387 DRAW_NORMAL_TEXT, 0);
21388
21389 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21390 if (updated_area == TEXT_AREA
21391 && updated_window->phys_cursor_on_p
21392 && updated_window->phys_cursor.vpos == output_cursor.vpos
21393 && updated_window->phys_cursor.hpos >= hpos
21394 && updated_window->phys_cursor.hpos < hpos + len)
21395 updated_window->phys_cursor_on_p = 0;
21396
21397 UNBLOCK_INPUT;
21398
21399 /* Advance the output cursor. */
21400 output_cursor.hpos += len;
21401 output_cursor.x = x;
21402 }
21403
21404
21405 /* EXPORT for RIF:
21406 Insert LEN glyphs from START at the nominal cursor position. */
21407
21408 void
21409 x_insert_glyphs (start, len)
21410 struct glyph *start;
21411 int len;
21412 {
21413 struct frame *f;
21414 struct window *w;
21415 int line_height, shift_by_width, shifted_region_width;
21416 struct glyph_row *row;
21417 struct glyph *glyph;
21418 int frame_x, frame_y;
21419 EMACS_INT hpos;
21420
21421 xassert (updated_window && updated_row);
21422 BLOCK_INPUT;
21423 w = updated_window;
21424 f = XFRAME (WINDOW_FRAME (w));
21425
21426 /* Get the height of the line we are in. */
21427 row = updated_row;
21428 line_height = row->height;
21429
21430 /* Get the width of the glyphs to insert. */
21431 shift_by_width = 0;
21432 for (glyph = start; glyph < start + len; ++glyph)
21433 shift_by_width += glyph->pixel_width;
21434
21435 /* Get the width of the region to shift right. */
21436 shifted_region_width = (window_box_width (w, updated_area)
21437 - output_cursor.x
21438 - shift_by_width);
21439
21440 /* Shift right. */
21441 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21442 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21443
21444 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21445 line_height, shift_by_width);
21446
21447 /* Write the glyphs. */
21448 hpos = start - row->glyphs[updated_area];
21449 draw_glyphs (w, output_cursor.x, row, updated_area,
21450 hpos, hpos + len,
21451 DRAW_NORMAL_TEXT, 0);
21452
21453 /* Advance the output cursor. */
21454 output_cursor.hpos += len;
21455 output_cursor.x += shift_by_width;
21456 UNBLOCK_INPUT;
21457 }
21458
21459
21460 /* EXPORT for RIF:
21461 Erase the current text line from the nominal cursor position
21462 (inclusive) to pixel column TO_X (exclusive). The idea is that
21463 everything from TO_X onward is already erased.
21464
21465 TO_X is a pixel position relative to updated_area of
21466 updated_window. TO_X == -1 means clear to the end of this area. */
21467
21468 void
21469 x_clear_end_of_line (to_x)
21470 int to_x;
21471 {
21472 struct frame *f;
21473 struct window *w = updated_window;
21474 int max_x, min_y, max_y;
21475 int from_x, from_y, to_y;
21476
21477 xassert (updated_window && updated_row);
21478 f = XFRAME (w->frame);
21479
21480 if (updated_row->full_width_p)
21481 max_x = WINDOW_TOTAL_WIDTH (w);
21482 else
21483 max_x = window_box_width (w, updated_area);
21484 max_y = window_text_bottom_y (w);
21485
21486 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21487 of window. For TO_X > 0, truncate to end of drawing area. */
21488 if (to_x == 0)
21489 return;
21490 else if (to_x < 0)
21491 to_x = max_x;
21492 else
21493 to_x = min (to_x, max_x);
21494
21495 to_y = min (max_y, output_cursor.y + updated_row->height);
21496
21497 /* Notice if the cursor will be cleared by this operation. */
21498 if (!updated_row->full_width_p)
21499 notice_overwritten_cursor (w, updated_area,
21500 output_cursor.x, -1,
21501 updated_row->y,
21502 MATRIX_ROW_BOTTOM_Y (updated_row));
21503
21504 from_x = output_cursor.x;
21505
21506 /* Translate to frame coordinates. */
21507 if (updated_row->full_width_p)
21508 {
21509 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21510 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21511 }
21512 else
21513 {
21514 int area_left = window_box_left (w, updated_area);
21515 from_x += area_left;
21516 to_x += area_left;
21517 }
21518
21519 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21520 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21521 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21522
21523 /* Prevent inadvertently clearing to end of the X window. */
21524 if (to_x > from_x && to_y > from_y)
21525 {
21526 BLOCK_INPUT;
21527 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21528 to_x - from_x, to_y - from_y);
21529 UNBLOCK_INPUT;
21530 }
21531 }
21532
21533 #endif /* HAVE_WINDOW_SYSTEM */
21534
21535
21536 \f
21537 /***********************************************************************
21538 Cursor types
21539 ***********************************************************************/
21540
21541 /* Value is the internal representation of the specified cursor type
21542 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21543 of the bar cursor. */
21544
21545 static enum text_cursor_kinds
21546 get_specified_cursor_type (arg, width)
21547 Lisp_Object arg;
21548 int *width;
21549 {
21550 enum text_cursor_kinds type;
21551
21552 if (NILP (arg))
21553 return NO_CURSOR;
21554
21555 if (EQ (arg, Qbox))
21556 return FILLED_BOX_CURSOR;
21557
21558 if (EQ (arg, Qhollow))
21559 return HOLLOW_BOX_CURSOR;
21560
21561 if (EQ (arg, Qbar))
21562 {
21563 *width = 2;
21564 return BAR_CURSOR;
21565 }
21566
21567 if (CONSP (arg)
21568 && EQ (XCAR (arg), Qbar)
21569 && INTEGERP (XCDR (arg))
21570 && XINT (XCDR (arg)) >= 0)
21571 {
21572 *width = XINT (XCDR (arg));
21573 return BAR_CURSOR;
21574 }
21575
21576 if (EQ (arg, Qhbar))
21577 {
21578 *width = 2;
21579 return HBAR_CURSOR;
21580 }
21581
21582 if (CONSP (arg)
21583 && EQ (XCAR (arg), Qhbar)
21584 && INTEGERP (XCDR (arg))
21585 && XINT (XCDR (arg)) >= 0)
21586 {
21587 *width = XINT (XCDR (arg));
21588 return HBAR_CURSOR;
21589 }
21590
21591 /* Treat anything unknown as "hollow box cursor".
21592 It was bad to signal an error; people have trouble fixing
21593 .Xdefaults with Emacs, when it has something bad in it. */
21594 type = HOLLOW_BOX_CURSOR;
21595
21596 return type;
21597 }
21598
21599 /* Set the default cursor types for specified frame. */
21600 void
21601 set_frame_cursor_types (f, arg)
21602 struct frame *f;
21603 Lisp_Object arg;
21604 {
21605 int width;
21606 Lisp_Object tem;
21607
21608 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21609 FRAME_CURSOR_WIDTH (f) = width;
21610
21611 /* By default, set up the blink-off state depending on the on-state. */
21612
21613 tem = Fassoc (arg, Vblink_cursor_alist);
21614 if (!NILP (tem))
21615 {
21616 FRAME_BLINK_OFF_CURSOR (f)
21617 = get_specified_cursor_type (XCDR (tem), &width);
21618 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21619 }
21620 else
21621 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21622 }
21623
21624
21625 /* Return the cursor we want to be displayed in window W. Return
21626 width of bar/hbar cursor through WIDTH arg. Return with
21627 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21628 (i.e. if the `system caret' should track this cursor).
21629
21630 In a mini-buffer window, we want the cursor only to appear if we
21631 are reading input from this window. For the selected window, we
21632 want the cursor type given by the frame parameter or buffer local
21633 setting of cursor-type. If explicitly marked off, draw no cursor.
21634 In all other cases, we want a hollow box cursor. */
21635
21636 static enum text_cursor_kinds
21637 get_window_cursor_type (w, glyph, width, active_cursor)
21638 struct window *w;
21639 struct glyph *glyph;
21640 int *width;
21641 int *active_cursor;
21642 {
21643 struct frame *f = XFRAME (w->frame);
21644 struct buffer *b = XBUFFER (w->buffer);
21645 int cursor_type = DEFAULT_CURSOR;
21646 Lisp_Object alt_cursor;
21647 int non_selected = 0;
21648
21649 *active_cursor = 1;
21650
21651 /* Echo area */
21652 if (cursor_in_echo_area
21653 && FRAME_HAS_MINIBUF_P (f)
21654 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21655 {
21656 if (w == XWINDOW (echo_area_window))
21657 {
21658 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21659 {
21660 *width = FRAME_CURSOR_WIDTH (f);
21661 return FRAME_DESIRED_CURSOR (f);
21662 }
21663 else
21664 return get_specified_cursor_type (b->cursor_type, width);
21665 }
21666
21667 *active_cursor = 0;
21668 non_selected = 1;
21669 }
21670
21671 /* Detect a nonselected window or nonselected frame. */
21672 else if (w != XWINDOW (f->selected_window)
21673 #ifdef HAVE_WINDOW_SYSTEM
21674 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21675 #endif
21676 )
21677 {
21678 *active_cursor = 0;
21679
21680 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21681 return NO_CURSOR;
21682
21683 non_selected = 1;
21684 }
21685
21686 /* Never display a cursor in a window in which cursor-type is nil. */
21687 if (NILP (b->cursor_type))
21688 return NO_CURSOR;
21689
21690 /* Get the normal cursor type for this window. */
21691 if (EQ (b->cursor_type, Qt))
21692 {
21693 cursor_type = FRAME_DESIRED_CURSOR (f);
21694 *width = FRAME_CURSOR_WIDTH (f);
21695 }
21696 else
21697 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21698
21699 /* Use cursor-in-non-selected-windows instead
21700 for non-selected window or frame. */
21701 if (non_selected)
21702 {
21703 alt_cursor = b->cursor_in_non_selected_windows;
21704 if (!EQ (Qt, alt_cursor))
21705 return get_specified_cursor_type (alt_cursor, width);
21706 /* t means modify the normal cursor type. */
21707 if (cursor_type == FILLED_BOX_CURSOR)
21708 cursor_type = HOLLOW_BOX_CURSOR;
21709 else if (cursor_type == BAR_CURSOR && *width > 1)
21710 --*width;
21711 return cursor_type;
21712 }
21713
21714 /* Use normal cursor if not blinked off. */
21715 if (!w->cursor_off_p)
21716 {
21717 #ifdef HAVE_WINDOW_SYSTEM
21718 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21719 {
21720 if (cursor_type == FILLED_BOX_CURSOR)
21721 {
21722 /* Using a block cursor on large images can be very annoying.
21723 So use a hollow cursor for "large" images.
21724 If image is not transparent (no mask), also use hollow cursor. */
21725 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21726 if (img != NULL && IMAGEP (img->spec))
21727 {
21728 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21729 where N = size of default frame font size.
21730 This should cover most of the "tiny" icons people may use. */
21731 if (!img->mask
21732 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21733 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21734 cursor_type = HOLLOW_BOX_CURSOR;
21735 }
21736 }
21737 else if (cursor_type != NO_CURSOR)
21738 {
21739 /* Display current only supports BOX and HOLLOW cursors for images.
21740 So for now, unconditionally use a HOLLOW cursor when cursor is
21741 not a solid box cursor. */
21742 cursor_type = HOLLOW_BOX_CURSOR;
21743 }
21744 }
21745 #endif
21746 return cursor_type;
21747 }
21748
21749 /* Cursor is blinked off, so determine how to "toggle" it. */
21750
21751 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21752 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21753 return get_specified_cursor_type (XCDR (alt_cursor), width);
21754
21755 /* Then see if frame has specified a specific blink off cursor type. */
21756 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21757 {
21758 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21759 return FRAME_BLINK_OFF_CURSOR (f);
21760 }
21761
21762 #if 0
21763 /* Some people liked having a permanently visible blinking cursor,
21764 while others had very strong opinions against it. So it was
21765 decided to remove it. KFS 2003-09-03 */
21766
21767 /* Finally perform built-in cursor blinking:
21768 filled box <-> hollow box
21769 wide [h]bar <-> narrow [h]bar
21770 narrow [h]bar <-> no cursor
21771 other type <-> no cursor */
21772
21773 if (cursor_type == FILLED_BOX_CURSOR)
21774 return HOLLOW_BOX_CURSOR;
21775
21776 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21777 {
21778 *width = 1;
21779 return cursor_type;
21780 }
21781 #endif
21782
21783 return NO_CURSOR;
21784 }
21785
21786
21787 #ifdef HAVE_WINDOW_SYSTEM
21788
21789 /* Notice when the text cursor of window W has been completely
21790 overwritten by a drawing operation that outputs glyphs in AREA
21791 starting at X0 and ending at X1 in the line starting at Y0 and
21792 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21793 the rest of the line after X0 has been written. Y coordinates
21794 are window-relative. */
21795
21796 static void
21797 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21798 struct window *w;
21799 enum glyph_row_area area;
21800 int x0, y0, x1, y1;
21801 {
21802 int cx0, cx1, cy0, cy1;
21803 struct glyph_row *row;
21804
21805 if (!w->phys_cursor_on_p)
21806 return;
21807 if (area != TEXT_AREA)
21808 return;
21809
21810 if (w->phys_cursor.vpos < 0
21811 || w->phys_cursor.vpos >= w->current_matrix->nrows
21812 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21813 !(row->enabled_p && row->displays_text_p)))
21814 return;
21815
21816 if (row->cursor_in_fringe_p)
21817 {
21818 row->cursor_in_fringe_p = 0;
21819 draw_fringe_bitmap (w, row, 0);
21820 w->phys_cursor_on_p = 0;
21821 return;
21822 }
21823
21824 cx0 = w->phys_cursor.x;
21825 cx1 = cx0 + w->phys_cursor_width;
21826 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21827 return;
21828
21829 /* The cursor image will be completely removed from the
21830 screen if the output area intersects the cursor area in
21831 y-direction. When we draw in [y0 y1[, and some part of
21832 the cursor is at y < y0, that part must have been drawn
21833 before. When scrolling, the cursor is erased before
21834 actually scrolling, so we don't come here. When not
21835 scrolling, the rows above the old cursor row must have
21836 changed, and in this case these rows must have written
21837 over the cursor image.
21838
21839 Likewise if part of the cursor is below y1, with the
21840 exception of the cursor being in the first blank row at
21841 the buffer and window end because update_text_area
21842 doesn't draw that row. (Except when it does, but
21843 that's handled in update_text_area.) */
21844
21845 cy0 = w->phys_cursor.y;
21846 cy1 = cy0 + w->phys_cursor_height;
21847 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21848 return;
21849
21850 w->phys_cursor_on_p = 0;
21851 }
21852
21853 #endif /* HAVE_WINDOW_SYSTEM */
21854
21855 \f
21856 /************************************************************************
21857 Mouse Face
21858 ************************************************************************/
21859
21860 #ifdef HAVE_WINDOW_SYSTEM
21861
21862 /* EXPORT for RIF:
21863 Fix the display of area AREA of overlapping row ROW in window W
21864 with respect to the overlapping part OVERLAPS. */
21865
21866 void
21867 x_fix_overlapping_area (w, row, area, overlaps)
21868 struct window *w;
21869 struct glyph_row *row;
21870 enum glyph_row_area area;
21871 int overlaps;
21872 {
21873 int i, x;
21874
21875 BLOCK_INPUT;
21876
21877 x = 0;
21878 for (i = 0; i < row->used[area];)
21879 {
21880 if (row->glyphs[area][i].overlaps_vertically_p)
21881 {
21882 int start = i, start_x = x;
21883
21884 do
21885 {
21886 x += row->glyphs[area][i].pixel_width;
21887 ++i;
21888 }
21889 while (i < row->used[area]
21890 && row->glyphs[area][i].overlaps_vertically_p);
21891
21892 draw_glyphs (w, start_x, row, area,
21893 start, i,
21894 DRAW_NORMAL_TEXT, overlaps);
21895 }
21896 else
21897 {
21898 x += row->glyphs[area][i].pixel_width;
21899 ++i;
21900 }
21901 }
21902
21903 UNBLOCK_INPUT;
21904 }
21905
21906
21907 /* EXPORT:
21908 Draw the cursor glyph of window W in glyph row ROW. See the
21909 comment of draw_glyphs for the meaning of HL. */
21910
21911 void
21912 draw_phys_cursor_glyph (w, row, hl)
21913 struct window *w;
21914 struct glyph_row *row;
21915 enum draw_glyphs_face hl;
21916 {
21917 /* If cursor hpos is out of bounds, don't draw garbage. This can
21918 happen in mini-buffer windows when switching between echo area
21919 glyphs and mini-buffer. */
21920 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21921 {
21922 int on_p = w->phys_cursor_on_p;
21923 int x1;
21924 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21925 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21926 hl, 0);
21927 w->phys_cursor_on_p = on_p;
21928
21929 if (hl == DRAW_CURSOR)
21930 w->phys_cursor_width = x1 - w->phys_cursor.x;
21931 /* When we erase the cursor, and ROW is overlapped by other
21932 rows, make sure that these overlapping parts of other rows
21933 are redrawn. */
21934 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21935 {
21936 w->phys_cursor_width = x1 - w->phys_cursor.x;
21937
21938 if (row > w->current_matrix->rows
21939 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21940 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21941 OVERLAPS_ERASED_CURSOR);
21942
21943 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21944 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21945 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21946 OVERLAPS_ERASED_CURSOR);
21947 }
21948 }
21949 }
21950
21951
21952 /* EXPORT:
21953 Erase the image of a cursor of window W from the screen. */
21954
21955 void
21956 erase_phys_cursor (w)
21957 struct window *w;
21958 {
21959 struct frame *f = XFRAME (w->frame);
21960 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21961 int hpos = w->phys_cursor.hpos;
21962 int vpos = w->phys_cursor.vpos;
21963 int mouse_face_here_p = 0;
21964 struct glyph_matrix *active_glyphs = w->current_matrix;
21965 struct glyph_row *cursor_row;
21966 struct glyph *cursor_glyph;
21967 enum draw_glyphs_face hl;
21968
21969 /* No cursor displayed or row invalidated => nothing to do on the
21970 screen. */
21971 if (w->phys_cursor_type == NO_CURSOR)
21972 goto mark_cursor_off;
21973
21974 /* VPOS >= active_glyphs->nrows means that window has been resized.
21975 Don't bother to erase the cursor. */
21976 if (vpos >= active_glyphs->nrows)
21977 goto mark_cursor_off;
21978
21979 /* If row containing cursor is marked invalid, there is nothing we
21980 can do. */
21981 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21982 if (!cursor_row->enabled_p)
21983 goto mark_cursor_off;
21984
21985 /* If line spacing is > 0, old cursor may only be partially visible in
21986 window after split-window. So adjust visible height. */
21987 cursor_row->visible_height = min (cursor_row->visible_height,
21988 window_text_bottom_y (w) - cursor_row->y);
21989
21990 /* If row is completely invisible, don't attempt to delete a cursor which
21991 isn't there. This can happen if cursor is at top of a window, and
21992 we switch to a buffer with a header line in that window. */
21993 if (cursor_row->visible_height <= 0)
21994 goto mark_cursor_off;
21995
21996 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21997 if (cursor_row->cursor_in_fringe_p)
21998 {
21999 cursor_row->cursor_in_fringe_p = 0;
22000 draw_fringe_bitmap (w, cursor_row, 0);
22001 goto mark_cursor_off;
22002 }
22003
22004 /* This can happen when the new row is shorter than the old one.
22005 In this case, either draw_glyphs or clear_end_of_line
22006 should have cleared the cursor. Note that we wouldn't be
22007 able to erase the cursor in this case because we don't have a
22008 cursor glyph at hand. */
22009 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22010 goto mark_cursor_off;
22011
22012 /* If the cursor is in the mouse face area, redisplay that when
22013 we clear the cursor. */
22014 if (! NILP (dpyinfo->mouse_face_window)
22015 && w == XWINDOW (dpyinfo->mouse_face_window)
22016 && (vpos > dpyinfo->mouse_face_beg_row
22017 || (vpos == dpyinfo->mouse_face_beg_row
22018 && hpos >= dpyinfo->mouse_face_beg_col))
22019 && (vpos < dpyinfo->mouse_face_end_row
22020 || (vpos == dpyinfo->mouse_face_end_row
22021 && hpos < dpyinfo->mouse_face_end_col))
22022 /* Don't redraw the cursor's spot in mouse face if it is at the
22023 end of a line (on a newline). The cursor appears there, but
22024 mouse highlighting does not. */
22025 && cursor_row->used[TEXT_AREA] > hpos)
22026 mouse_face_here_p = 1;
22027
22028 /* Maybe clear the display under the cursor. */
22029 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22030 {
22031 int x, y, left_x;
22032 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22033 int width;
22034
22035 cursor_glyph = get_phys_cursor_glyph (w);
22036 if (cursor_glyph == NULL)
22037 goto mark_cursor_off;
22038
22039 width = cursor_glyph->pixel_width;
22040 left_x = window_box_left_offset (w, TEXT_AREA);
22041 x = w->phys_cursor.x;
22042 if (x < left_x)
22043 width -= left_x - x;
22044 width = min (width, window_box_width (w, TEXT_AREA) - x);
22045 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22046 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22047
22048 if (width > 0)
22049 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22050 }
22051
22052 /* Erase the cursor by redrawing the character underneath it. */
22053 if (mouse_face_here_p)
22054 hl = DRAW_MOUSE_FACE;
22055 else
22056 hl = DRAW_NORMAL_TEXT;
22057 draw_phys_cursor_glyph (w, cursor_row, hl);
22058
22059 mark_cursor_off:
22060 w->phys_cursor_on_p = 0;
22061 w->phys_cursor_type = NO_CURSOR;
22062 }
22063
22064
22065 /* EXPORT:
22066 Display or clear cursor of window W. If ON is zero, clear the
22067 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22068 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22069
22070 void
22071 display_and_set_cursor (w, on, hpos, vpos, x, y)
22072 struct window *w;
22073 int on, hpos, vpos, x, y;
22074 {
22075 struct frame *f = XFRAME (w->frame);
22076 int new_cursor_type;
22077 int new_cursor_width;
22078 int active_cursor;
22079 struct glyph_row *glyph_row;
22080 struct glyph *glyph;
22081
22082 /* This is pointless on invisible frames, and dangerous on garbaged
22083 windows and frames; in the latter case, the frame or window may
22084 be in the midst of changing its size, and x and y may be off the
22085 window. */
22086 if (! FRAME_VISIBLE_P (f)
22087 || FRAME_GARBAGED_P (f)
22088 || vpos >= w->current_matrix->nrows
22089 || hpos >= w->current_matrix->matrix_w)
22090 return;
22091
22092 /* If cursor is off and we want it off, return quickly. */
22093 if (!on && !w->phys_cursor_on_p)
22094 return;
22095
22096 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22097 /* If cursor row is not enabled, we don't really know where to
22098 display the cursor. */
22099 if (!glyph_row->enabled_p)
22100 {
22101 w->phys_cursor_on_p = 0;
22102 return;
22103 }
22104
22105 glyph = NULL;
22106 if (!glyph_row->exact_window_width_line_p
22107 || hpos < glyph_row->used[TEXT_AREA])
22108 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22109
22110 xassert (interrupt_input_blocked);
22111
22112 /* Set new_cursor_type to the cursor we want to be displayed. */
22113 new_cursor_type = get_window_cursor_type (w, glyph,
22114 &new_cursor_width, &active_cursor);
22115
22116 /* If cursor is currently being shown and we don't want it to be or
22117 it is in the wrong place, or the cursor type is not what we want,
22118 erase it. */
22119 if (w->phys_cursor_on_p
22120 && (!on
22121 || w->phys_cursor.x != x
22122 || w->phys_cursor.y != y
22123 || new_cursor_type != w->phys_cursor_type
22124 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22125 && new_cursor_width != w->phys_cursor_width)))
22126 erase_phys_cursor (w);
22127
22128 /* Don't check phys_cursor_on_p here because that flag is only set
22129 to zero in some cases where we know that the cursor has been
22130 completely erased, to avoid the extra work of erasing the cursor
22131 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22132 still not be visible, or it has only been partly erased. */
22133 if (on)
22134 {
22135 w->phys_cursor_ascent = glyph_row->ascent;
22136 w->phys_cursor_height = glyph_row->height;
22137
22138 /* Set phys_cursor_.* before x_draw_.* is called because some
22139 of them may need the information. */
22140 w->phys_cursor.x = x;
22141 w->phys_cursor.y = glyph_row->y;
22142 w->phys_cursor.hpos = hpos;
22143 w->phys_cursor.vpos = vpos;
22144 }
22145
22146 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22147 new_cursor_type, new_cursor_width,
22148 on, active_cursor);
22149 }
22150
22151
22152 /* Switch the display of W's cursor on or off, according to the value
22153 of ON. */
22154
22155 static void
22156 update_window_cursor (w, on)
22157 struct window *w;
22158 int on;
22159 {
22160 /* Don't update cursor in windows whose frame is in the process
22161 of being deleted. */
22162 if (w->current_matrix)
22163 {
22164 BLOCK_INPUT;
22165 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22166 w->phys_cursor.x, w->phys_cursor.y);
22167 UNBLOCK_INPUT;
22168 }
22169 }
22170
22171
22172 /* Call update_window_cursor with parameter ON_P on all leaf windows
22173 in the window tree rooted at W. */
22174
22175 static void
22176 update_cursor_in_window_tree (w, on_p)
22177 struct window *w;
22178 int on_p;
22179 {
22180 while (w)
22181 {
22182 if (!NILP (w->hchild))
22183 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22184 else if (!NILP (w->vchild))
22185 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22186 else
22187 update_window_cursor (w, on_p);
22188
22189 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22190 }
22191 }
22192
22193
22194 /* EXPORT:
22195 Display the cursor on window W, or clear it, according to ON_P.
22196 Don't change the cursor's position. */
22197
22198 void
22199 x_update_cursor (f, on_p)
22200 struct frame *f;
22201 int on_p;
22202 {
22203 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22204 }
22205
22206
22207 /* EXPORT:
22208 Clear the cursor of window W to background color, and mark the
22209 cursor as not shown. This is used when the text where the cursor
22210 is is about to be rewritten. */
22211
22212 void
22213 x_clear_cursor (w)
22214 struct window *w;
22215 {
22216 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22217 update_window_cursor (w, 0);
22218 }
22219
22220
22221 /* EXPORT:
22222 Display the active region described by mouse_face_* according to DRAW. */
22223
22224 void
22225 show_mouse_face (dpyinfo, draw)
22226 Display_Info *dpyinfo;
22227 enum draw_glyphs_face draw;
22228 {
22229 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22230 struct frame *f = XFRAME (WINDOW_FRAME (w));
22231
22232 if (/* If window is in the process of being destroyed, don't bother
22233 to do anything. */
22234 w->current_matrix != NULL
22235 /* Don't update mouse highlight if hidden */
22236 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22237 /* Recognize when we are called to operate on rows that don't exist
22238 anymore. This can happen when a window is split. */
22239 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22240 {
22241 int phys_cursor_on_p = w->phys_cursor_on_p;
22242 struct glyph_row *row, *first, *last;
22243
22244 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22245 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22246
22247 for (row = first; row <= last && row->enabled_p; ++row)
22248 {
22249 int start_hpos, end_hpos, start_x;
22250
22251 /* For all but the first row, the highlight starts at column 0. */
22252 if (row == first)
22253 {
22254 start_hpos = dpyinfo->mouse_face_beg_col;
22255 start_x = dpyinfo->mouse_face_beg_x;
22256 }
22257 else
22258 {
22259 start_hpos = 0;
22260 start_x = 0;
22261 }
22262
22263 if (row == last)
22264 end_hpos = dpyinfo->mouse_face_end_col;
22265 else
22266 {
22267 end_hpos = row->used[TEXT_AREA];
22268 if (draw == DRAW_NORMAL_TEXT)
22269 row->fill_line_p = 1; /* Clear to end of line */
22270 }
22271
22272 if (end_hpos > start_hpos)
22273 {
22274 draw_glyphs (w, start_x, row, TEXT_AREA,
22275 start_hpos, end_hpos,
22276 draw, 0);
22277
22278 row->mouse_face_p
22279 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22280 }
22281 }
22282
22283 /* When we've written over the cursor, arrange for it to
22284 be displayed again. */
22285 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22286 {
22287 BLOCK_INPUT;
22288 display_and_set_cursor (w, 1,
22289 w->phys_cursor.hpos, w->phys_cursor.vpos,
22290 w->phys_cursor.x, w->phys_cursor.y);
22291 UNBLOCK_INPUT;
22292 }
22293 }
22294
22295 /* Change the mouse cursor. */
22296 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22297 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22298 else if (draw == DRAW_MOUSE_FACE)
22299 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22300 else
22301 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22302 }
22303
22304 /* EXPORT:
22305 Clear out the mouse-highlighted active region.
22306 Redraw it un-highlighted first. Value is non-zero if mouse
22307 face was actually drawn unhighlighted. */
22308
22309 int
22310 clear_mouse_face (dpyinfo)
22311 Display_Info *dpyinfo;
22312 {
22313 int cleared = 0;
22314
22315 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22316 {
22317 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22318 cleared = 1;
22319 }
22320
22321 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22322 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22323 dpyinfo->mouse_face_window = Qnil;
22324 dpyinfo->mouse_face_overlay = Qnil;
22325 return cleared;
22326 }
22327
22328
22329 /* EXPORT:
22330 Non-zero if physical cursor of window W is within mouse face. */
22331
22332 int
22333 cursor_in_mouse_face_p (w)
22334 struct window *w;
22335 {
22336 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22337 int in_mouse_face = 0;
22338
22339 if (WINDOWP (dpyinfo->mouse_face_window)
22340 && XWINDOW (dpyinfo->mouse_face_window) == w)
22341 {
22342 int hpos = w->phys_cursor.hpos;
22343 int vpos = w->phys_cursor.vpos;
22344
22345 if (vpos >= dpyinfo->mouse_face_beg_row
22346 && vpos <= dpyinfo->mouse_face_end_row
22347 && (vpos > dpyinfo->mouse_face_beg_row
22348 || hpos >= dpyinfo->mouse_face_beg_col)
22349 && (vpos < dpyinfo->mouse_face_end_row
22350 || hpos < dpyinfo->mouse_face_end_col
22351 || dpyinfo->mouse_face_past_end))
22352 in_mouse_face = 1;
22353 }
22354
22355 return in_mouse_face;
22356 }
22357
22358
22359
22360 \f
22361 /* Find the glyph matrix position of buffer position CHARPOS in window
22362 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22363 current glyphs must be up to date. If CHARPOS is above window
22364 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22365 of last line in W. In the row containing CHARPOS, stop before glyphs
22366 having STOP as object. */
22367
22368 #if 1 /* This is a version of fast_find_position that's more correct
22369 in the presence of hscrolling, for example. I didn't install
22370 it right away because the problem fixed is minor, it failed
22371 in 20.x as well, and I think it's too risky to install
22372 so near the release of 21.1. 2001-09-25 gerd. */
22373
22374 #ifndef HAVE_CARBON
22375 static
22376 #endif
22377 int
22378 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22379 struct window *w;
22380 EMACS_INT charpos;
22381 int *hpos, *vpos, *x, *y;
22382 Lisp_Object stop;
22383 {
22384 struct glyph_row *row, *first;
22385 struct glyph *glyph, *end;
22386 int past_end = 0;
22387
22388 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22389 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22390 {
22391 *x = first->x;
22392 *y = first->y;
22393 *hpos = 0;
22394 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22395 return 1;
22396 }
22397
22398 row = row_containing_pos (w, charpos, first, NULL, 0);
22399 if (row == NULL)
22400 {
22401 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22402 past_end = 1;
22403 }
22404
22405 /* If whole rows or last part of a row came from a display overlay,
22406 row_containing_pos will skip over such rows because their end pos
22407 equals the start pos of the overlay or interval.
22408
22409 Move back if we have a STOP object and previous row's
22410 end glyph came from STOP. */
22411 if (!NILP (stop))
22412 {
22413 struct glyph_row *prev;
22414 while ((prev = row - 1, prev >= first)
22415 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22416 && prev->used[TEXT_AREA] > 0)
22417 {
22418 struct glyph *beg = prev->glyphs[TEXT_AREA];
22419 glyph = beg + prev->used[TEXT_AREA];
22420 while (--glyph >= beg
22421 && INTEGERP (glyph->object));
22422 if (glyph < beg
22423 || !EQ (stop, glyph->object))
22424 break;
22425 row = prev;
22426 }
22427 }
22428
22429 *x = row->x;
22430 *y = row->y;
22431 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22432
22433 glyph = row->glyphs[TEXT_AREA];
22434 end = glyph + row->used[TEXT_AREA];
22435
22436 /* Skip over glyphs not having an object at the start of the row.
22437 These are special glyphs like truncation marks on terminal
22438 frames. */
22439 if (row->displays_text_p)
22440 while (glyph < end
22441 && INTEGERP (glyph->object)
22442 && !EQ (stop, glyph->object)
22443 && glyph->charpos < 0)
22444 {
22445 *x += glyph->pixel_width;
22446 ++glyph;
22447 }
22448
22449 while (glyph < end
22450 && !INTEGERP (glyph->object)
22451 && !EQ (stop, glyph->object)
22452 && (!BUFFERP (glyph->object)
22453 || glyph->charpos < charpos))
22454 {
22455 *x += glyph->pixel_width;
22456 ++glyph;
22457 }
22458
22459 *hpos = glyph - row->glyphs[TEXT_AREA];
22460 return !past_end;
22461 }
22462
22463 #else /* not 1 */
22464
22465 static int
22466 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22467 struct window *w;
22468 EMACS_INT pos;
22469 int *hpos, *vpos, *x, *y;
22470 Lisp_Object stop;
22471 {
22472 int i;
22473 int lastcol;
22474 int maybe_next_line_p = 0;
22475 int line_start_position;
22476 int yb = window_text_bottom_y (w);
22477 struct glyph_row *row, *best_row;
22478 int row_vpos, best_row_vpos;
22479 int current_x;
22480
22481 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22482 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22483
22484 while (row->y < yb)
22485 {
22486 if (row->used[TEXT_AREA])
22487 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22488 else
22489 line_start_position = 0;
22490
22491 if (line_start_position > pos)
22492 break;
22493 /* If the position sought is the end of the buffer,
22494 don't include the blank lines at the bottom of the window. */
22495 else if (line_start_position == pos
22496 && pos == BUF_ZV (XBUFFER (w->buffer)))
22497 {
22498 maybe_next_line_p = 1;
22499 break;
22500 }
22501 else if (line_start_position > 0)
22502 {
22503 best_row = row;
22504 best_row_vpos = row_vpos;
22505 }
22506
22507 if (row->y + row->height >= yb)
22508 break;
22509
22510 ++row;
22511 ++row_vpos;
22512 }
22513
22514 /* Find the right column within BEST_ROW. */
22515 lastcol = 0;
22516 current_x = best_row->x;
22517 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22518 {
22519 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22520 int charpos = glyph->charpos;
22521
22522 if (BUFFERP (glyph->object))
22523 {
22524 if (charpos == pos)
22525 {
22526 *hpos = i;
22527 *vpos = best_row_vpos;
22528 *x = current_x;
22529 *y = best_row->y;
22530 return 1;
22531 }
22532 else if (charpos > pos)
22533 break;
22534 }
22535 else if (EQ (glyph->object, stop))
22536 break;
22537
22538 if (charpos > 0)
22539 lastcol = i;
22540 current_x += glyph->pixel_width;
22541 }
22542
22543 /* If we're looking for the end of the buffer,
22544 and we didn't find it in the line we scanned,
22545 use the start of the following line. */
22546 if (maybe_next_line_p)
22547 {
22548 ++best_row;
22549 ++best_row_vpos;
22550 lastcol = 0;
22551 current_x = best_row->x;
22552 }
22553
22554 *vpos = best_row_vpos;
22555 *hpos = lastcol + 1;
22556 *x = current_x;
22557 *y = best_row->y;
22558 return 0;
22559 }
22560
22561 #endif /* not 1 */
22562
22563
22564 /* Find the position of the glyph for position POS in OBJECT in
22565 window W's current matrix, and return in *X, *Y the pixel
22566 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22567
22568 RIGHT_P non-zero means return the position of the right edge of the
22569 glyph, RIGHT_P zero means return the left edge position.
22570
22571 If no glyph for POS exists in the matrix, return the position of
22572 the glyph with the next smaller position that is in the matrix, if
22573 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22574 exists in the matrix, return the position of the glyph with the
22575 next larger position in OBJECT.
22576
22577 Value is non-zero if a glyph was found. */
22578
22579 static int
22580 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22581 struct window *w;
22582 EMACS_INT pos;
22583 Lisp_Object object;
22584 int *hpos, *vpos, *x, *y;
22585 int right_p;
22586 {
22587 int yb = window_text_bottom_y (w);
22588 struct glyph_row *r;
22589 struct glyph *best_glyph = NULL;
22590 struct glyph_row *best_row = NULL;
22591 int best_x = 0;
22592
22593 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22594 r->enabled_p && r->y < yb;
22595 ++r)
22596 {
22597 struct glyph *g = r->glyphs[TEXT_AREA];
22598 struct glyph *e = g + r->used[TEXT_AREA];
22599 int gx;
22600
22601 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22602 if (EQ (g->object, object))
22603 {
22604 if (g->charpos == pos)
22605 {
22606 best_glyph = g;
22607 best_x = gx;
22608 best_row = r;
22609 goto found;
22610 }
22611 else if (best_glyph == NULL
22612 || ((eabs (g->charpos - pos)
22613 < eabs (best_glyph->charpos - pos))
22614 && (right_p
22615 ? g->charpos < pos
22616 : g->charpos > pos)))
22617 {
22618 best_glyph = g;
22619 best_x = gx;
22620 best_row = r;
22621 }
22622 }
22623 }
22624
22625 found:
22626
22627 if (best_glyph)
22628 {
22629 *x = best_x;
22630 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22631
22632 if (right_p)
22633 {
22634 *x += best_glyph->pixel_width;
22635 ++*hpos;
22636 }
22637
22638 *y = best_row->y;
22639 *vpos = best_row - w->current_matrix->rows;
22640 }
22641
22642 return best_glyph != NULL;
22643 }
22644
22645
22646 /* See if position X, Y is within a hot-spot of an image. */
22647
22648 static int
22649 on_hot_spot_p (hot_spot, x, y)
22650 Lisp_Object hot_spot;
22651 int x, y;
22652 {
22653 if (!CONSP (hot_spot))
22654 return 0;
22655
22656 if (EQ (XCAR (hot_spot), Qrect))
22657 {
22658 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22659 Lisp_Object rect = XCDR (hot_spot);
22660 Lisp_Object tem;
22661 if (!CONSP (rect))
22662 return 0;
22663 if (!CONSP (XCAR (rect)))
22664 return 0;
22665 if (!CONSP (XCDR (rect)))
22666 return 0;
22667 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22668 return 0;
22669 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22670 return 0;
22671 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22672 return 0;
22673 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22674 return 0;
22675 return 1;
22676 }
22677 else if (EQ (XCAR (hot_spot), Qcircle))
22678 {
22679 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22680 Lisp_Object circ = XCDR (hot_spot);
22681 Lisp_Object lr, lx0, ly0;
22682 if (CONSP (circ)
22683 && CONSP (XCAR (circ))
22684 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22685 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22686 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22687 {
22688 double r = XFLOATINT (lr);
22689 double dx = XINT (lx0) - x;
22690 double dy = XINT (ly0) - y;
22691 return (dx * dx + dy * dy <= r * r);
22692 }
22693 }
22694 else if (EQ (XCAR (hot_spot), Qpoly))
22695 {
22696 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22697 if (VECTORP (XCDR (hot_spot)))
22698 {
22699 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22700 Lisp_Object *poly = v->contents;
22701 int n = v->size;
22702 int i;
22703 int inside = 0;
22704 Lisp_Object lx, ly;
22705 int x0, y0;
22706
22707 /* Need an even number of coordinates, and at least 3 edges. */
22708 if (n < 6 || n & 1)
22709 return 0;
22710
22711 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22712 If count is odd, we are inside polygon. Pixels on edges
22713 may or may not be included depending on actual geometry of the
22714 polygon. */
22715 if ((lx = poly[n-2], !INTEGERP (lx))
22716 || (ly = poly[n-1], !INTEGERP (lx)))
22717 return 0;
22718 x0 = XINT (lx), y0 = XINT (ly);
22719 for (i = 0; i < n; i += 2)
22720 {
22721 int x1 = x0, y1 = y0;
22722 if ((lx = poly[i], !INTEGERP (lx))
22723 || (ly = poly[i+1], !INTEGERP (ly)))
22724 return 0;
22725 x0 = XINT (lx), y0 = XINT (ly);
22726
22727 /* Does this segment cross the X line? */
22728 if (x0 >= x)
22729 {
22730 if (x1 >= x)
22731 continue;
22732 }
22733 else if (x1 < x)
22734 continue;
22735 if (y > y0 && y > y1)
22736 continue;
22737 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22738 inside = !inside;
22739 }
22740 return inside;
22741 }
22742 }
22743 return 0;
22744 }
22745
22746 Lisp_Object
22747 find_hot_spot (map, x, y)
22748 Lisp_Object map;
22749 int x, y;
22750 {
22751 while (CONSP (map))
22752 {
22753 if (CONSP (XCAR (map))
22754 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22755 return XCAR (map);
22756 map = XCDR (map);
22757 }
22758
22759 return Qnil;
22760 }
22761
22762 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22763 3, 3, 0,
22764 doc: /* Lookup in image map MAP coordinates X and Y.
22765 An image map is an alist where each element has the format (AREA ID PLIST).
22766 An AREA is specified as either a rectangle, a circle, or a polygon:
22767 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22768 pixel coordinates of the upper left and bottom right corners.
22769 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22770 and the radius of the circle; r may be a float or integer.
22771 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22772 vector describes one corner in the polygon.
22773 Returns the alist element for the first matching AREA in MAP. */)
22774 (map, x, y)
22775 Lisp_Object map;
22776 Lisp_Object x, y;
22777 {
22778 if (NILP (map))
22779 return Qnil;
22780
22781 CHECK_NUMBER (x);
22782 CHECK_NUMBER (y);
22783
22784 return find_hot_spot (map, XINT (x), XINT (y));
22785 }
22786
22787
22788 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22789 static void
22790 define_frame_cursor1 (f, cursor, pointer)
22791 struct frame *f;
22792 Cursor cursor;
22793 Lisp_Object pointer;
22794 {
22795 /* Do not change cursor shape while dragging mouse. */
22796 if (!NILP (do_mouse_tracking))
22797 return;
22798
22799 if (!NILP (pointer))
22800 {
22801 if (EQ (pointer, Qarrow))
22802 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22803 else if (EQ (pointer, Qhand))
22804 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22805 else if (EQ (pointer, Qtext))
22806 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22807 else if (EQ (pointer, intern ("hdrag")))
22808 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22809 #ifdef HAVE_X_WINDOWS
22810 else if (EQ (pointer, intern ("vdrag")))
22811 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22812 #endif
22813 else if (EQ (pointer, intern ("hourglass")))
22814 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22815 else if (EQ (pointer, Qmodeline))
22816 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22817 else
22818 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22819 }
22820
22821 if (cursor != No_Cursor)
22822 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22823 }
22824
22825 /* Take proper action when mouse has moved to the mode or header line
22826 or marginal area AREA of window W, x-position X and y-position Y.
22827 X is relative to the start of the text display area of W, so the
22828 width of bitmap areas and scroll bars must be subtracted to get a
22829 position relative to the start of the mode line. */
22830
22831 static void
22832 note_mode_line_or_margin_highlight (window, x, y, area)
22833 Lisp_Object window;
22834 int x, y;
22835 enum window_part area;
22836 {
22837 struct window *w = XWINDOW (window);
22838 struct frame *f = XFRAME (w->frame);
22839 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22840 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22841 Lisp_Object pointer = Qnil;
22842 int charpos, dx, dy, width, height;
22843 Lisp_Object string, object = Qnil;
22844 Lisp_Object pos, help;
22845
22846 Lisp_Object mouse_face;
22847 int original_x_pixel = x;
22848 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22849 struct glyph_row *row;
22850
22851 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22852 {
22853 int x0;
22854 struct glyph *end;
22855
22856 string = mode_line_string (w, area, &x, &y, &charpos,
22857 &object, &dx, &dy, &width, &height);
22858
22859 row = (area == ON_MODE_LINE
22860 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22861 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22862
22863 /* Find glyph */
22864 if (row->mode_line_p && row->enabled_p)
22865 {
22866 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22867 end = glyph + row->used[TEXT_AREA];
22868
22869 for (x0 = original_x_pixel;
22870 glyph < end && x0 >= glyph->pixel_width;
22871 ++glyph)
22872 x0 -= glyph->pixel_width;
22873
22874 if (glyph >= end)
22875 glyph = NULL;
22876 }
22877 }
22878 else
22879 {
22880 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22881 string = marginal_area_string (w, area, &x, &y, &charpos,
22882 &object, &dx, &dy, &width, &height);
22883 }
22884
22885 help = Qnil;
22886
22887 if (IMAGEP (object))
22888 {
22889 Lisp_Object image_map, hotspot;
22890 if ((image_map = Fplist_get (XCDR (object), QCmap),
22891 !NILP (image_map))
22892 && (hotspot = find_hot_spot (image_map, dx, dy),
22893 CONSP (hotspot))
22894 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22895 {
22896 Lisp_Object area_id, plist;
22897
22898 area_id = XCAR (hotspot);
22899 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22900 If so, we could look for mouse-enter, mouse-leave
22901 properties in PLIST (and do something...). */
22902 hotspot = XCDR (hotspot);
22903 if (CONSP (hotspot)
22904 && (plist = XCAR (hotspot), CONSP (plist)))
22905 {
22906 pointer = Fplist_get (plist, Qpointer);
22907 if (NILP (pointer))
22908 pointer = Qhand;
22909 help = Fplist_get (plist, Qhelp_echo);
22910 if (!NILP (help))
22911 {
22912 help_echo_string = help;
22913 /* Is this correct? ++kfs */
22914 XSETWINDOW (help_echo_window, w);
22915 help_echo_object = w->buffer;
22916 help_echo_pos = charpos;
22917 }
22918 }
22919 }
22920 if (NILP (pointer))
22921 pointer = Fplist_get (XCDR (object), QCpointer);
22922 }
22923
22924 if (STRINGP (string))
22925 {
22926 pos = make_number (charpos);
22927 /* If we're on a string with `help-echo' text property, arrange
22928 for the help to be displayed. This is done by setting the
22929 global variable help_echo_string to the help string. */
22930 if (NILP (help))
22931 {
22932 help = Fget_text_property (pos, Qhelp_echo, string);
22933 if (!NILP (help))
22934 {
22935 help_echo_string = help;
22936 XSETWINDOW (help_echo_window, w);
22937 help_echo_object = string;
22938 help_echo_pos = charpos;
22939 }
22940 }
22941
22942 if (NILP (pointer))
22943 pointer = Fget_text_property (pos, Qpointer, string);
22944
22945 /* Change the mouse pointer according to what is under X/Y. */
22946 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22947 {
22948 Lisp_Object map;
22949 map = Fget_text_property (pos, Qlocal_map, string);
22950 if (!KEYMAPP (map))
22951 map = Fget_text_property (pos, Qkeymap, string);
22952 if (!KEYMAPP (map))
22953 cursor = dpyinfo->vertical_scroll_bar_cursor;
22954 }
22955
22956 /* Change the mouse face according to what is under X/Y. */
22957 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22958 if (!NILP (mouse_face)
22959 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22960 && glyph)
22961 {
22962 Lisp_Object b, e;
22963
22964 struct glyph * tmp_glyph;
22965
22966 int gpos;
22967 int gseq_length;
22968 int total_pixel_width;
22969 EMACS_INT ignore;
22970
22971 int vpos, hpos;
22972
22973 b = Fprevious_single_property_change (make_number (charpos + 1),
22974 Qmouse_face, string, Qnil);
22975 if (NILP (b))
22976 b = make_number (0);
22977
22978 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22979 if (NILP (e))
22980 e = make_number (SCHARS (string));
22981
22982 /* Calculate the position(glyph position: GPOS) of GLYPH in
22983 displayed string. GPOS is different from CHARPOS.
22984
22985 CHARPOS is the position of glyph in internal string
22986 object. A mode line string format has structures which
22987 is converted to a flatten by emacs lisp interpreter.
22988 The internal string is an element of the structures.
22989 The displayed string is the flatten string. */
22990 gpos = 0;
22991 if (glyph > row_start_glyph)
22992 {
22993 tmp_glyph = glyph - 1;
22994 while (tmp_glyph >= row_start_glyph
22995 && tmp_glyph->charpos >= XINT (b)
22996 && EQ (tmp_glyph->object, glyph->object))
22997 {
22998 tmp_glyph--;
22999 gpos++;
23000 }
23001 }
23002
23003 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23004 displayed string holding GLYPH.
23005
23006 GSEQ_LENGTH is different from SCHARS (STRING).
23007 SCHARS (STRING) returns the length of the internal string. */
23008 for (tmp_glyph = glyph, gseq_length = gpos;
23009 tmp_glyph->charpos < XINT (e);
23010 tmp_glyph++, gseq_length++)
23011 {
23012 if (!EQ (tmp_glyph->object, glyph->object))
23013 break;
23014 }
23015
23016 total_pixel_width = 0;
23017 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23018 total_pixel_width += tmp_glyph->pixel_width;
23019
23020 /* Pre calculation of re-rendering position */
23021 vpos = (x - gpos);
23022 hpos = (area == ON_MODE_LINE
23023 ? (w->current_matrix)->nrows - 1
23024 : 0);
23025
23026 /* If the re-rendering position is included in the last
23027 re-rendering area, we should do nothing. */
23028 if ( EQ (window, dpyinfo->mouse_face_window)
23029 && dpyinfo->mouse_face_beg_col <= vpos
23030 && vpos < dpyinfo->mouse_face_end_col
23031 && dpyinfo->mouse_face_beg_row == hpos )
23032 return;
23033
23034 if (clear_mouse_face (dpyinfo))
23035 cursor = No_Cursor;
23036
23037 dpyinfo->mouse_face_beg_col = vpos;
23038 dpyinfo->mouse_face_beg_row = hpos;
23039
23040 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23041 dpyinfo->mouse_face_beg_y = 0;
23042
23043 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23044 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23045
23046 dpyinfo->mouse_face_end_x = 0;
23047 dpyinfo->mouse_face_end_y = 0;
23048
23049 dpyinfo->mouse_face_past_end = 0;
23050 dpyinfo->mouse_face_window = window;
23051
23052 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23053 charpos,
23054 0, 0, 0, &ignore,
23055 glyph->face_id, 1);
23056 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23057
23058 if (NILP (pointer))
23059 pointer = Qhand;
23060 }
23061 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23062 clear_mouse_face (dpyinfo);
23063 }
23064 define_frame_cursor1 (f, cursor, pointer);
23065 }
23066
23067
23068 /* EXPORT:
23069 Take proper action when the mouse has moved to position X, Y on
23070 frame F as regards highlighting characters that have mouse-face
23071 properties. Also de-highlighting chars where the mouse was before.
23072 X and Y can be negative or out of range. */
23073
23074 void
23075 note_mouse_highlight (f, x, y)
23076 struct frame *f;
23077 int x, y;
23078 {
23079 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23080 enum window_part part;
23081 Lisp_Object window;
23082 struct window *w;
23083 Cursor cursor = No_Cursor;
23084 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23085 struct buffer *b;
23086
23087 /* When a menu is active, don't highlight because this looks odd. */
23088 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23089 if (popup_activated ())
23090 return;
23091 #endif
23092
23093 if (NILP (Vmouse_highlight)
23094 || !f->glyphs_initialized_p)
23095 return;
23096
23097 dpyinfo->mouse_face_mouse_x = x;
23098 dpyinfo->mouse_face_mouse_y = y;
23099 dpyinfo->mouse_face_mouse_frame = f;
23100
23101 if (dpyinfo->mouse_face_defer)
23102 return;
23103
23104 if (gc_in_progress)
23105 {
23106 dpyinfo->mouse_face_deferred_gc = 1;
23107 return;
23108 }
23109
23110 /* Which window is that in? */
23111 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23112
23113 /* If we were displaying active text in another window, clear that.
23114 Also clear if we move out of text area in same window. */
23115 if (! EQ (window, dpyinfo->mouse_face_window)
23116 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23117 && !NILP (dpyinfo->mouse_face_window)))
23118 clear_mouse_face (dpyinfo);
23119
23120 /* Not on a window -> return. */
23121 if (!WINDOWP (window))
23122 return;
23123
23124 /* Reset help_echo_string. It will get recomputed below. */
23125 help_echo_string = Qnil;
23126
23127 /* Convert to window-relative pixel coordinates. */
23128 w = XWINDOW (window);
23129 frame_to_window_pixel_xy (w, &x, &y);
23130
23131 /* Handle tool-bar window differently since it doesn't display a
23132 buffer. */
23133 if (EQ (window, f->tool_bar_window))
23134 {
23135 note_tool_bar_highlight (f, x, y);
23136 return;
23137 }
23138
23139 /* Mouse is on the mode, header line or margin? */
23140 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23141 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23142 {
23143 note_mode_line_or_margin_highlight (window, x, y, part);
23144 return;
23145 }
23146
23147 if (part == ON_VERTICAL_BORDER)
23148 {
23149 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23150 help_echo_string = build_string ("drag-mouse-1: resize");
23151 }
23152 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23153 || part == ON_SCROLL_BAR)
23154 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23155 else
23156 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23157
23158 /* Are we in a window whose display is up to date?
23159 And verify the buffer's text has not changed. */
23160 b = XBUFFER (w->buffer);
23161 if (part == ON_TEXT
23162 && EQ (w->window_end_valid, w->buffer)
23163 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23164 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23165 {
23166 int hpos, vpos, pos, i, dx, dy, area;
23167 struct glyph *glyph;
23168 Lisp_Object object;
23169 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23170 Lisp_Object *overlay_vec = NULL;
23171 int noverlays;
23172 struct buffer *obuf;
23173 int obegv, ozv, same_region;
23174
23175 /* Find the glyph under X/Y. */
23176 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23177
23178 /* Look for :pointer property on image. */
23179 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23180 {
23181 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23182 if (img != NULL && IMAGEP (img->spec))
23183 {
23184 Lisp_Object image_map, hotspot;
23185 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23186 !NILP (image_map))
23187 && (hotspot = find_hot_spot (image_map,
23188 glyph->slice.x + dx,
23189 glyph->slice.y + dy),
23190 CONSP (hotspot))
23191 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23192 {
23193 Lisp_Object area_id, plist;
23194
23195 area_id = XCAR (hotspot);
23196 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23197 If so, we could look for mouse-enter, mouse-leave
23198 properties in PLIST (and do something...). */
23199 hotspot = XCDR (hotspot);
23200 if (CONSP (hotspot)
23201 && (plist = XCAR (hotspot), CONSP (plist)))
23202 {
23203 pointer = Fplist_get (plist, Qpointer);
23204 if (NILP (pointer))
23205 pointer = Qhand;
23206 help_echo_string = Fplist_get (plist, Qhelp_echo);
23207 if (!NILP (help_echo_string))
23208 {
23209 help_echo_window = window;
23210 help_echo_object = glyph->object;
23211 help_echo_pos = glyph->charpos;
23212 }
23213 }
23214 }
23215 if (NILP (pointer))
23216 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23217 }
23218 }
23219
23220 /* Clear mouse face if X/Y not over text. */
23221 if (glyph == NULL
23222 || area != TEXT_AREA
23223 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23224 {
23225 if (clear_mouse_face (dpyinfo))
23226 cursor = No_Cursor;
23227 if (NILP (pointer))
23228 {
23229 if (area != TEXT_AREA)
23230 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23231 else
23232 pointer = Vvoid_text_area_pointer;
23233 }
23234 goto set_cursor;
23235 }
23236
23237 pos = glyph->charpos;
23238 object = glyph->object;
23239 if (!STRINGP (object) && !BUFFERP (object))
23240 goto set_cursor;
23241
23242 /* If we get an out-of-range value, return now; avoid an error. */
23243 if (BUFFERP (object) && pos > BUF_Z (b))
23244 goto set_cursor;
23245
23246 /* Make the window's buffer temporarily current for
23247 overlays_at and compute_char_face. */
23248 obuf = current_buffer;
23249 current_buffer = b;
23250 obegv = BEGV;
23251 ozv = ZV;
23252 BEGV = BEG;
23253 ZV = Z;
23254
23255 /* Is this char mouse-active or does it have help-echo? */
23256 position = make_number (pos);
23257
23258 if (BUFFERP (object))
23259 {
23260 /* Put all the overlays we want in a vector in overlay_vec. */
23261 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23262 /* Sort overlays into increasing priority order. */
23263 noverlays = sort_overlays (overlay_vec, noverlays, w);
23264 }
23265 else
23266 noverlays = 0;
23267
23268 same_region = (EQ (window, dpyinfo->mouse_face_window)
23269 && vpos >= dpyinfo->mouse_face_beg_row
23270 && vpos <= dpyinfo->mouse_face_end_row
23271 && (vpos > dpyinfo->mouse_face_beg_row
23272 || hpos >= dpyinfo->mouse_face_beg_col)
23273 && (vpos < dpyinfo->mouse_face_end_row
23274 || hpos < dpyinfo->mouse_face_end_col
23275 || dpyinfo->mouse_face_past_end));
23276
23277 if (same_region)
23278 cursor = No_Cursor;
23279
23280 /* Check mouse-face highlighting. */
23281 if (! same_region
23282 /* If there exists an overlay with mouse-face overlapping
23283 the one we are currently highlighting, we have to
23284 check if we enter the overlapping overlay, and then
23285 highlight only that. */
23286 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23287 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23288 {
23289 /* Find the highest priority overlay that has a mouse-face
23290 property. */
23291 overlay = Qnil;
23292 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23293 {
23294 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23295 if (!NILP (mouse_face))
23296 overlay = overlay_vec[i];
23297 }
23298
23299 /* If we're actually highlighting the same overlay as
23300 before, there's no need to do that again. */
23301 if (!NILP (overlay)
23302 && EQ (overlay, dpyinfo->mouse_face_overlay))
23303 goto check_help_echo;
23304
23305 dpyinfo->mouse_face_overlay = overlay;
23306
23307 /* Clear the display of the old active region, if any. */
23308 if (clear_mouse_face (dpyinfo))
23309 cursor = No_Cursor;
23310
23311 /* If no overlay applies, get a text property. */
23312 if (NILP (overlay))
23313 mouse_face = Fget_text_property (position, Qmouse_face, object);
23314
23315 /* Handle the overlay case. */
23316 if (!NILP (overlay))
23317 {
23318 /* Find the range of text around this char that
23319 should be active. */
23320 Lisp_Object before, after;
23321 EMACS_INT ignore;
23322
23323 before = Foverlay_start (overlay);
23324 after = Foverlay_end (overlay);
23325 /* Record this as the current active region. */
23326 fast_find_position (w, XFASTINT (before),
23327 &dpyinfo->mouse_face_beg_col,
23328 &dpyinfo->mouse_face_beg_row,
23329 &dpyinfo->mouse_face_beg_x,
23330 &dpyinfo->mouse_face_beg_y, Qnil);
23331
23332 dpyinfo->mouse_face_past_end
23333 = !fast_find_position (w, XFASTINT (after),
23334 &dpyinfo->mouse_face_end_col,
23335 &dpyinfo->mouse_face_end_row,
23336 &dpyinfo->mouse_face_end_x,
23337 &dpyinfo->mouse_face_end_y, Qnil);
23338 dpyinfo->mouse_face_window = window;
23339
23340 dpyinfo->mouse_face_face_id
23341 = face_at_buffer_position (w, pos, 0, 0,
23342 &ignore, pos + 1,
23343 !dpyinfo->mouse_face_hidden);
23344
23345 /* Display it as active. */
23346 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23347 cursor = No_Cursor;
23348 }
23349 /* Handle the text property case. */
23350 else if (!NILP (mouse_face) && BUFFERP (object))
23351 {
23352 /* Find the range of text around this char that
23353 should be active. */
23354 Lisp_Object before, after, beginning, end;
23355 EMACS_INT ignore;
23356
23357 beginning = Fmarker_position (w->start);
23358 end = make_number (BUF_Z (XBUFFER (object))
23359 - XFASTINT (w->window_end_pos));
23360 before
23361 = Fprevious_single_property_change (make_number (pos + 1),
23362 Qmouse_face,
23363 object, beginning);
23364 after
23365 = Fnext_single_property_change (position, Qmouse_face,
23366 object, end);
23367
23368 /* Record this as the current active region. */
23369 fast_find_position (w, XFASTINT (before),
23370 &dpyinfo->mouse_face_beg_col,
23371 &dpyinfo->mouse_face_beg_row,
23372 &dpyinfo->mouse_face_beg_x,
23373 &dpyinfo->mouse_face_beg_y, Qnil);
23374 dpyinfo->mouse_face_past_end
23375 = !fast_find_position (w, XFASTINT (after),
23376 &dpyinfo->mouse_face_end_col,
23377 &dpyinfo->mouse_face_end_row,
23378 &dpyinfo->mouse_face_end_x,
23379 &dpyinfo->mouse_face_end_y, Qnil);
23380 dpyinfo->mouse_face_window = window;
23381
23382 if (BUFFERP (object))
23383 dpyinfo->mouse_face_face_id
23384 = face_at_buffer_position (w, pos, 0, 0,
23385 &ignore, pos + 1,
23386 !dpyinfo->mouse_face_hidden);
23387
23388 /* Display it as active. */
23389 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23390 cursor = No_Cursor;
23391 }
23392 else if (!NILP (mouse_face) && STRINGP (object))
23393 {
23394 Lisp_Object b, e;
23395 EMACS_INT ignore;
23396
23397 b = Fprevious_single_property_change (make_number (pos + 1),
23398 Qmouse_face,
23399 object, Qnil);
23400 e = Fnext_single_property_change (position, Qmouse_face,
23401 object, Qnil);
23402 if (NILP (b))
23403 b = make_number (0);
23404 if (NILP (e))
23405 e = make_number (SCHARS (object) - 1);
23406
23407 fast_find_string_pos (w, XINT (b), object,
23408 &dpyinfo->mouse_face_beg_col,
23409 &dpyinfo->mouse_face_beg_row,
23410 &dpyinfo->mouse_face_beg_x,
23411 &dpyinfo->mouse_face_beg_y, 0);
23412 fast_find_string_pos (w, XINT (e), object,
23413 &dpyinfo->mouse_face_end_col,
23414 &dpyinfo->mouse_face_end_row,
23415 &dpyinfo->mouse_face_end_x,
23416 &dpyinfo->mouse_face_end_y, 1);
23417 dpyinfo->mouse_face_past_end = 0;
23418 dpyinfo->mouse_face_window = window;
23419 dpyinfo->mouse_face_face_id
23420 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23421 glyph->face_id, 1);
23422 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23423 cursor = No_Cursor;
23424 }
23425 else if (STRINGP (object) && NILP (mouse_face))
23426 {
23427 /* A string which doesn't have mouse-face, but
23428 the text ``under'' it might have. */
23429 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23430 int start = MATRIX_ROW_START_CHARPOS (r);
23431
23432 pos = string_buffer_position (w, object, start);
23433 if (pos > 0)
23434 mouse_face = get_char_property_and_overlay (make_number (pos),
23435 Qmouse_face,
23436 w->buffer,
23437 &overlay);
23438 if (!NILP (mouse_face) && !NILP (overlay))
23439 {
23440 Lisp_Object before = Foverlay_start (overlay);
23441 Lisp_Object after = Foverlay_end (overlay);
23442 EMACS_INT ignore;
23443
23444 /* Note that we might not be able to find position
23445 BEFORE in the glyph matrix if the overlay is
23446 entirely covered by a `display' property. In
23447 this case, we overshoot. So let's stop in
23448 the glyph matrix before glyphs for OBJECT. */
23449 fast_find_position (w, XFASTINT (before),
23450 &dpyinfo->mouse_face_beg_col,
23451 &dpyinfo->mouse_face_beg_row,
23452 &dpyinfo->mouse_face_beg_x,
23453 &dpyinfo->mouse_face_beg_y,
23454 object);
23455
23456 dpyinfo->mouse_face_past_end
23457 = !fast_find_position (w, XFASTINT (after),
23458 &dpyinfo->mouse_face_end_col,
23459 &dpyinfo->mouse_face_end_row,
23460 &dpyinfo->mouse_face_end_x,
23461 &dpyinfo->mouse_face_end_y,
23462 Qnil);
23463 dpyinfo->mouse_face_window = window;
23464 dpyinfo->mouse_face_face_id
23465 = face_at_buffer_position (w, pos, 0, 0,
23466 &ignore, pos + 1,
23467 !dpyinfo->mouse_face_hidden);
23468
23469 /* Display it as active. */
23470 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23471 cursor = No_Cursor;
23472 }
23473 }
23474 }
23475
23476 check_help_echo:
23477
23478 /* Look for a `help-echo' property. */
23479 if (NILP (help_echo_string)) {
23480 Lisp_Object help, overlay;
23481
23482 /* Check overlays first. */
23483 help = overlay = Qnil;
23484 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23485 {
23486 overlay = overlay_vec[i];
23487 help = Foverlay_get (overlay, Qhelp_echo);
23488 }
23489
23490 if (!NILP (help))
23491 {
23492 help_echo_string = help;
23493 help_echo_window = window;
23494 help_echo_object = overlay;
23495 help_echo_pos = pos;
23496 }
23497 else
23498 {
23499 Lisp_Object object = glyph->object;
23500 int charpos = glyph->charpos;
23501
23502 /* Try text properties. */
23503 if (STRINGP (object)
23504 && charpos >= 0
23505 && charpos < SCHARS (object))
23506 {
23507 help = Fget_text_property (make_number (charpos),
23508 Qhelp_echo, object);
23509 if (NILP (help))
23510 {
23511 /* If the string itself doesn't specify a help-echo,
23512 see if the buffer text ``under'' it does. */
23513 struct glyph_row *r
23514 = MATRIX_ROW (w->current_matrix, vpos);
23515 int start = MATRIX_ROW_START_CHARPOS (r);
23516 int pos = string_buffer_position (w, object, start);
23517 if (pos > 0)
23518 {
23519 help = Fget_char_property (make_number (pos),
23520 Qhelp_echo, w->buffer);
23521 if (!NILP (help))
23522 {
23523 charpos = pos;
23524 object = w->buffer;
23525 }
23526 }
23527 }
23528 }
23529 else if (BUFFERP (object)
23530 && charpos >= BEGV
23531 && charpos < ZV)
23532 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23533 object);
23534
23535 if (!NILP (help))
23536 {
23537 help_echo_string = help;
23538 help_echo_window = window;
23539 help_echo_object = object;
23540 help_echo_pos = charpos;
23541 }
23542 }
23543 }
23544
23545 /* Look for a `pointer' property. */
23546 if (NILP (pointer))
23547 {
23548 /* Check overlays first. */
23549 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23550 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23551
23552 if (NILP (pointer))
23553 {
23554 Lisp_Object object = glyph->object;
23555 int charpos = glyph->charpos;
23556
23557 /* Try text properties. */
23558 if (STRINGP (object)
23559 && charpos >= 0
23560 && charpos < SCHARS (object))
23561 {
23562 pointer = Fget_text_property (make_number (charpos),
23563 Qpointer, object);
23564 if (NILP (pointer))
23565 {
23566 /* If the string itself doesn't specify a pointer,
23567 see if the buffer text ``under'' it does. */
23568 struct glyph_row *r
23569 = MATRIX_ROW (w->current_matrix, vpos);
23570 int start = MATRIX_ROW_START_CHARPOS (r);
23571 int pos = string_buffer_position (w, object, start);
23572 if (pos > 0)
23573 pointer = Fget_char_property (make_number (pos),
23574 Qpointer, w->buffer);
23575 }
23576 }
23577 else if (BUFFERP (object)
23578 && charpos >= BEGV
23579 && charpos < ZV)
23580 pointer = Fget_text_property (make_number (charpos),
23581 Qpointer, object);
23582 }
23583 }
23584
23585 BEGV = obegv;
23586 ZV = ozv;
23587 current_buffer = obuf;
23588 }
23589
23590 set_cursor:
23591
23592 define_frame_cursor1 (f, cursor, pointer);
23593 }
23594
23595
23596 /* EXPORT for RIF:
23597 Clear any mouse-face on window W. This function is part of the
23598 redisplay interface, and is called from try_window_id and similar
23599 functions to ensure the mouse-highlight is off. */
23600
23601 void
23602 x_clear_window_mouse_face (w)
23603 struct window *w;
23604 {
23605 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23606 Lisp_Object window;
23607
23608 BLOCK_INPUT;
23609 XSETWINDOW (window, w);
23610 if (EQ (window, dpyinfo->mouse_face_window))
23611 clear_mouse_face (dpyinfo);
23612 UNBLOCK_INPUT;
23613 }
23614
23615
23616 /* EXPORT:
23617 Just discard the mouse face information for frame F, if any.
23618 This is used when the size of F is changed. */
23619
23620 void
23621 cancel_mouse_face (f)
23622 struct frame *f;
23623 {
23624 Lisp_Object window;
23625 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23626
23627 window = dpyinfo->mouse_face_window;
23628 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23629 {
23630 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23631 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23632 dpyinfo->mouse_face_window = Qnil;
23633 }
23634 }
23635
23636
23637 #endif /* HAVE_WINDOW_SYSTEM */
23638
23639 \f
23640 /***********************************************************************
23641 Exposure Events
23642 ***********************************************************************/
23643
23644 #ifdef HAVE_WINDOW_SYSTEM
23645
23646 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23647 which intersects rectangle R. R is in window-relative coordinates. */
23648
23649 static void
23650 expose_area (w, row, r, area)
23651 struct window *w;
23652 struct glyph_row *row;
23653 XRectangle *r;
23654 enum glyph_row_area area;
23655 {
23656 struct glyph *first = row->glyphs[area];
23657 struct glyph *end = row->glyphs[area] + row->used[area];
23658 struct glyph *last;
23659 int first_x, start_x, x;
23660
23661 if (area == TEXT_AREA && row->fill_line_p)
23662 /* If row extends face to end of line write the whole line. */
23663 draw_glyphs (w, 0, row, area,
23664 0, row->used[area],
23665 DRAW_NORMAL_TEXT, 0);
23666 else
23667 {
23668 /* Set START_X to the window-relative start position for drawing glyphs of
23669 AREA. The first glyph of the text area can be partially visible.
23670 The first glyphs of other areas cannot. */
23671 start_x = window_box_left_offset (w, area);
23672 x = start_x;
23673 if (area == TEXT_AREA)
23674 x += row->x;
23675
23676 /* Find the first glyph that must be redrawn. */
23677 while (first < end
23678 && x + first->pixel_width < r->x)
23679 {
23680 x += first->pixel_width;
23681 ++first;
23682 }
23683
23684 /* Find the last one. */
23685 last = first;
23686 first_x = x;
23687 while (last < end
23688 && x < r->x + r->width)
23689 {
23690 x += last->pixel_width;
23691 ++last;
23692 }
23693
23694 /* Repaint. */
23695 if (last > first)
23696 draw_glyphs (w, first_x - start_x, row, area,
23697 first - row->glyphs[area], last - row->glyphs[area],
23698 DRAW_NORMAL_TEXT, 0);
23699 }
23700 }
23701
23702
23703 /* Redraw the parts of the glyph row ROW on window W intersecting
23704 rectangle R. R is in window-relative coordinates. Value is
23705 non-zero if mouse-face was overwritten. */
23706
23707 static int
23708 expose_line (w, row, r)
23709 struct window *w;
23710 struct glyph_row *row;
23711 XRectangle *r;
23712 {
23713 xassert (row->enabled_p);
23714
23715 if (row->mode_line_p || w->pseudo_window_p)
23716 draw_glyphs (w, 0, row, TEXT_AREA,
23717 0, row->used[TEXT_AREA],
23718 DRAW_NORMAL_TEXT, 0);
23719 else
23720 {
23721 if (row->used[LEFT_MARGIN_AREA])
23722 expose_area (w, row, r, LEFT_MARGIN_AREA);
23723 if (row->used[TEXT_AREA])
23724 expose_area (w, row, r, TEXT_AREA);
23725 if (row->used[RIGHT_MARGIN_AREA])
23726 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23727 draw_row_fringe_bitmaps (w, row);
23728 }
23729
23730 return row->mouse_face_p;
23731 }
23732
23733
23734 /* Redraw those parts of glyphs rows during expose event handling that
23735 overlap other rows. Redrawing of an exposed line writes over parts
23736 of lines overlapping that exposed line; this function fixes that.
23737
23738 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23739 row in W's current matrix that is exposed and overlaps other rows.
23740 LAST_OVERLAPPING_ROW is the last such row. */
23741
23742 static void
23743 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23744 struct window *w;
23745 struct glyph_row *first_overlapping_row;
23746 struct glyph_row *last_overlapping_row;
23747 XRectangle *r;
23748 {
23749 struct glyph_row *row;
23750
23751 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23752 if (row->overlapping_p)
23753 {
23754 xassert (row->enabled_p && !row->mode_line_p);
23755
23756 row->clip = r;
23757 if (row->used[LEFT_MARGIN_AREA])
23758 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23759
23760 if (row->used[TEXT_AREA])
23761 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23762
23763 if (row->used[RIGHT_MARGIN_AREA])
23764 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23765 row->clip = NULL;
23766 }
23767 }
23768
23769
23770 /* Return non-zero if W's cursor intersects rectangle R. */
23771
23772 static int
23773 phys_cursor_in_rect_p (w, r)
23774 struct window *w;
23775 XRectangle *r;
23776 {
23777 XRectangle cr, result;
23778 struct glyph *cursor_glyph;
23779 struct glyph_row *row;
23780
23781 if (w->phys_cursor.vpos >= 0
23782 && w->phys_cursor.vpos < w->current_matrix->nrows
23783 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23784 row->enabled_p)
23785 && row->cursor_in_fringe_p)
23786 {
23787 /* Cursor is in the fringe. */
23788 cr.x = window_box_right_offset (w,
23789 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23790 ? RIGHT_MARGIN_AREA
23791 : TEXT_AREA));
23792 cr.y = row->y;
23793 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23794 cr.height = row->height;
23795 return x_intersect_rectangles (&cr, r, &result);
23796 }
23797
23798 cursor_glyph = get_phys_cursor_glyph (w);
23799 if (cursor_glyph)
23800 {
23801 /* r is relative to W's box, but w->phys_cursor.x is relative
23802 to left edge of W's TEXT area. Adjust it. */
23803 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23804 cr.y = w->phys_cursor.y;
23805 cr.width = cursor_glyph->pixel_width;
23806 cr.height = w->phys_cursor_height;
23807 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23808 I assume the effect is the same -- and this is portable. */
23809 return x_intersect_rectangles (&cr, r, &result);
23810 }
23811 /* If we don't understand the format, pretend we're not in the hot-spot. */
23812 return 0;
23813 }
23814
23815
23816 /* EXPORT:
23817 Draw a vertical window border to the right of window W if W doesn't
23818 have vertical scroll bars. */
23819
23820 void
23821 x_draw_vertical_border (w)
23822 struct window *w;
23823 {
23824 struct frame *f = XFRAME (WINDOW_FRAME (w));
23825
23826 /* We could do better, if we knew what type of scroll-bar the adjacent
23827 windows (on either side) have... But we don't :-(
23828 However, I think this works ok. ++KFS 2003-04-25 */
23829
23830 /* Redraw borders between horizontally adjacent windows. Don't
23831 do it for frames with vertical scroll bars because either the
23832 right scroll bar of a window, or the left scroll bar of its
23833 neighbor will suffice as a border. */
23834 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23835 return;
23836
23837 if (!WINDOW_RIGHTMOST_P (w)
23838 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23839 {
23840 int x0, x1, y0, y1;
23841
23842 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23843 y1 -= 1;
23844
23845 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23846 x1 -= 1;
23847
23848 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23849 }
23850 else if (!WINDOW_LEFTMOST_P (w)
23851 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23852 {
23853 int x0, x1, y0, y1;
23854
23855 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23856 y1 -= 1;
23857
23858 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23859 x0 -= 1;
23860
23861 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23862 }
23863 }
23864
23865
23866 /* Redraw the part of window W intersection rectangle FR. Pixel
23867 coordinates in FR are frame-relative. Call this function with
23868 input blocked. Value is non-zero if the exposure overwrites
23869 mouse-face. */
23870
23871 static int
23872 expose_window (w, fr)
23873 struct window *w;
23874 XRectangle *fr;
23875 {
23876 struct frame *f = XFRAME (w->frame);
23877 XRectangle wr, r;
23878 int mouse_face_overwritten_p = 0;
23879
23880 /* If window is not yet fully initialized, do nothing. This can
23881 happen when toolkit scroll bars are used and a window is split.
23882 Reconfiguring the scroll bar will generate an expose for a newly
23883 created window. */
23884 if (w->current_matrix == NULL)
23885 return 0;
23886
23887 /* When we're currently updating the window, display and current
23888 matrix usually don't agree. Arrange for a thorough display
23889 later. */
23890 if (w == updated_window)
23891 {
23892 SET_FRAME_GARBAGED (f);
23893 return 0;
23894 }
23895
23896 /* Frame-relative pixel rectangle of W. */
23897 wr.x = WINDOW_LEFT_EDGE_X (w);
23898 wr.y = WINDOW_TOP_EDGE_Y (w);
23899 wr.width = WINDOW_TOTAL_WIDTH (w);
23900 wr.height = WINDOW_TOTAL_HEIGHT (w);
23901
23902 if (x_intersect_rectangles (fr, &wr, &r))
23903 {
23904 int yb = window_text_bottom_y (w);
23905 struct glyph_row *row;
23906 int cursor_cleared_p;
23907 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23908
23909 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23910 r.x, r.y, r.width, r.height));
23911
23912 /* Convert to window coordinates. */
23913 r.x -= WINDOW_LEFT_EDGE_X (w);
23914 r.y -= WINDOW_TOP_EDGE_Y (w);
23915
23916 /* Turn off the cursor. */
23917 if (!w->pseudo_window_p
23918 && phys_cursor_in_rect_p (w, &r))
23919 {
23920 x_clear_cursor (w);
23921 cursor_cleared_p = 1;
23922 }
23923 else
23924 cursor_cleared_p = 0;
23925
23926 /* Update lines intersecting rectangle R. */
23927 first_overlapping_row = last_overlapping_row = NULL;
23928 for (row = w->current_matrix->rows;
23929 row->enabled_p;
23930 ++row)
23931 {
23932 int y0 = row->y;
23933 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23934
23935 if ((y0 >= r.y && y0 < r.y + r.height)
23936 || (y1 > r.y && y1 < r.y + r.height)
23937 || (r.y >= y0 && r.y < y1)
23938 || (r.y + r.height > y0 && r.y + r.height < y1))
23939 {
23940 /* A header line may be overlapping, but there is no need
23941 to fix overlapping areas for them. KFS 2005-02-12 */
23942 if (row->overlapping_p && !row->mode_line_p)
23943 {
23944 if (first_overlapping_row == NULL)
23945 first_overlapping_row = row;
23946 last_overlapping_row = row;
23947 }
23948
23949 row->clip = fr;
23950 if (expose_line (w, row, &r))
23951 mouse_face_overwritten_p = 1;
23952 row->clip = NULL;
23953 }
23954 else if (row->overlapping_p)
23955 {
23956 /* We must redraw a row overlapping the exposed area. */
23957 if (y0 < r.y
23958 ? y0 + row->phys_height > r.y
23959 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
23960 {
23961 if (first_overlapping_row == NULL)
23962 first_overlapping_row = row;
23963 last_overlapping_row = row;
23964 }
23965 }
23966
23967 if (y1 >= yb)
23968 break;
23969 }
23970
23971 /* Display the mode line if there is one. */
23972 if (WINDOW_WANTS_MODELINE_P (w)
23973 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23974 row->enabled_p)
23975 && row->y < r.y + r.height)
23976 {
23977 if (expose_line (w, row, &r))
23978 mouse_face_overwritten_p = 1;
23979 }
23980
23981 if (!w->pseudo_window_p)
23982 {
23983 /* Fix the display of overlapping rows. */
23984 if (first_overlapping_row)
23985 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
23986 fr);
23987
23988 /* Draw border between windows. */
23989 x_draw_vertical_border (w);
23990
23991 /* Turn the cursor on again. */
23992 if (cursor_cleared_p)
23993 update_window_cursor (w, 1);
23994 }
23995 }
23996
23997 return mouse_face_overwritten_p;
23998 }
23999
24000
24001
24002 /* Redraw (parts) of all windows in the window tree rooted at W that
24003 intersect R. R contains frame pixel coordinates. Value is
24004 non-zero if the exposure overwrites mouse-face. */
24005
24006 static int
24007 expose_window_tree (w, r)
24008 struct window *w;
24009 XRectangle *r;
24010 {
24011 struct frame *f = XFRAME (w->frame);
24012 int mouse_face_overwritten_p = 0;
24013
24014 while (w && !FRAME_GARBAGED_P (f))
24015 {
24016 if (!NILP (w->hchild))
24017 mouse_face_overwritten_p
24018 |= expose_window_tree (XWINDOW (w->hchild), r);
24019 else if (!NILP (w->vchild))
24020 mouse_face_overwritten_p
24021 |= expose_window_tree (XWINDOW (w->vchild), r);
24022 else
24023 mouse_face_overwritten_p |= expose_window (w, r);
24024
24025 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24026 }
24027
24028 return mouse_face_overwritten_p;
24029 }
24030
24031
24032 /* EXPORT:
24033 Redisplay an exposed area of frame F. X and Y are the upper-left
24034 corner of the exposed rectangle. W and H are width and height of
24035 the exposed area. All are pixel values. W or H zero means redraw
24036 the entire frame. */
24037
24038 void
24039 expose_frame (f, x, y, w, h)
24040 struct frame *f;
24041 int x, y, w, h;
24042 {
24043 XRectangle r;
24044 int mouse_face_overwritten_p = 0;
24045
24046 TRACE ((stderr, "expose_frame "));
24047
24048 /* No need to redraw if frame will be redrawn soon. */
24049 if (FRAME_GARBAGED_P (f))
24050 {
24051 TRACE ((stderr, " garbaged\n"));
24052 return;
24053 }
24054
24055 /* If basic faces haven't been realized yet, there is no point in
24056 trying to redraw anything. This can happen when we get an expose
24057 event while Emacs is starting, e.g. by moving another window. */
24058 if (FRAME_FACE_CACHE (f) == NULL
24059 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24060 {
24061 TRACE ((stderr, " no faces\n"));
24062 return;
24063 }
24064
24065 if (w == 0 || h == 0)
24066 {
24067 r.x = r.y = 0;
24068 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24069 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24070 }
24071 else
24072 {
24073 r.x = x;
24074 r.y = y;
24075 r.width = w;
24076 r.height = h;
24077 }
24078
24079 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24080 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24081
24082 if (WINDOWP (f->tool_bar_window))
24083 mouse_face_overwritten_p
24084 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24085
24086 #ifdef HAVE_X_WINDOWS
24087 #ifndef MSDOS
24088 #ifndef USE_X_TOOLKIT
24089 if (WINDOWP (f->menu_bar_window))
24090 mouse_face_overwritten_p
24091 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24092 #endif /* not USE_X_TOOLKIT */
24093 #endif
24094 #endif
24095
24096 /* Some window managers support a focus-follows-mouse style with
24097 delayed raising of frames. Imagine a partially obscured frame,
24098 and moving the mouse into partially obscured mouse-face on that
24099 frame. The visible part of the mouse-face will be highlighted,
24100 then the WM raises the obscured frame. With at least one WM, KDE
24101 2.1, Emacs is not getting any event for the raising of the frame
24102 (even tried with SubstructureRedirectMask), only Expose events.
24103 These expose events will draw text normally, i.e. not
24104 highlighted. Which means we must redo the highlight here.
24105 Subsume it under ``we love X''. --gerd 2001-08-15 */
24106 /* Included in Windows version because Windows most likely does not
24107 do the right thing if any third party tool offers
24108 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24109 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24110 {
24111 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24112 if (f == dpyinfo->mouse_face_mouse_frame)
24113 {
24114 int x = dpyinfo->mouse_face_mouse_x;
24115 int y = dpyinfo->mouse_face_mouse_y;
24116 clear_mouse_face (dpyinfo);
24117 note_mouse_highlight (f, x, y);
24118 }
24119 }
24120 }
24121
24122
24123 /* EXPORT:
24124 Determine the intersection of two rectangles R1 and R2. Return
24125 the intersection in *RESULT. Value is non-zero if RESULT is not
24126 empty. */
24127
24128 int
24129 x_intersect_rectangles (r1, r2, result)
24130 XRectangle *r1, *r2, *result;
24131 {
24132 XRectangle *left, *right;
24133 XRectangle *upper, *lower;
24134 int intersection_p = 0;
24135
24136 /* Rearrange so that R1 is the left-most rectangle. */
24137 if (r1->x < r2->x)
24138 left = r1, right = r2;
24139 else
24140 left = r2, right = r1;
24141
24142 /* X0 of the intersection is right.x0, if this is inside R1,
24143 otherwise there is no intersection. */
24144 if (right->x <= left->x + left->width)
24145 {
24146 result->x = right->x;
24147
24148 /* The right end of the intersection is the minimum of the
24149 the right ends of left and right. */
24150 result->width = (min (left->x + left->width, right->x + right->width)
24151 - result->x);
24152
24153 /* Same game for Y. */
24154 if (r1->y < r2->y)
24155 upper = r1, lower = r2;
24156 else
24157 upper = r2, lower = r1;
24158
24159 /* The upper end of the intersection is lower.y0, if this is inside
24160 of upper. Otherwise, there is no intersection. */
24161 if (lower->y <= upper->y + upper->height)
24162 {
24163 result->y = lower->y;
24164
24165 /* The lower end of the intersection is the minimum of the lower
24166 ends of upper and lower. */
24167 result->height = (min (lower->y + lower->height,
24168 upper->y + upper->height)
24169 - result->y);
24170 intersection_p = 1;
24171 }
24172 }
24173
24174 return intersection_p;
24175 }
24176
24177 #endif /* HAVE_WINDOW_SYSTEM */
24178
24179 \f
24180 /***********************************************************************
24181 Initialization
24182 ***********************************************************************/
24183
24184 void
24185 syms_of_xdisp ()
24186 {
24187 Vwith_echo_area_save_vector = Qnil;
24188 staticpro (&Vwith_echo_area_save_vector);
24189
24190 Vmessage_stack = Qnil;
24191 staticpro (&Vmessage_stack);
24192
24193 Qinhibit_redisplay = intern ("inhibit-redisplay");
24194 staticpro (&Qinhibit_redisplay);
24195
24196 message_dolog_marker1 = Fmake_marker ();
24197 staticpro (&message_dolog_marker1);
24198 message_dolog_marker2 = Fmake_marker ();
24199 staticpro (&message_dolog_marker2);
24200 message_dolog_marker3 = Fmake_marker ();
24201 staticpro (&message_dolog_marker3);
24202
24203 #if GLYPH_DEBUG
24204 defsubr (&Sdump_frame_glyph_matrix);
24205 defsubr (&Sdump_glyph_matrix);
24206 defsubr (&Sdump_glyph_row);
24207 defsubr (&Sdump_tool_bar_row);
24208 defsubr (&Strace_redisplay);
24209 defsubr (&Strace_to_stderr);
24210 #endif
24211 #ifdef HAVE_WINDOW_SYSTEM
24212 defsubr (&Stool_bar_lines_needed);
24213 defsubr (&Slookup_image_map);
24214 #endif
24215 defsubr (&Sformat_mode_line);
24216 defsubr (&Sinvisible_p);
24217
24218 staticpro (&Qmenu_bar_update_hook);
24219 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24220
24221 staticpro (&Qoverriding_terminal_local_map);
24222 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24223
24224 staticpro (&Qoverriding_local_map);
24225 Qoverriding_local_map = intern ("overriding-local-map");
24226
24227 staticpro (&Qwindow_scroll_functions);
24228 Qwindow_scroll_functions = intern ("window-scroll-functions");
24229
24230 staticpro (&Qwindow_text_change_functions);
24231 Qwindow_text_change_functions = intern ("window-text-change-functions");
24232
24233 staticpro (&Qredisplay_end_trigger_functions);
24234 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24235
24236 staticpro (&Qinhibit_point_motion_hooks);
24237 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24238
24239 Qeval = intern ("eval");
24240 staticpro (&Qeval);
24241
24242 QCdata = intern (":data");
24243 staticpro (&QCdata);
24244 Qdisplay = intern ("display");
24245 staticpro (&Qdisplay);
24246 Qspace_width = intern ("space-width");
24247 staticpro (&Qspace_width);
24248 Qraise = intern ("raise");
24249 staticpro (&Qraise);
24250 Qslice = intern ("slice");
24251 staticpro (&Qslice);
24252 Qspace = intern ("space");
24253 staticpro (&Qspace);
24254 Qmargin = intern ("margin");
24255 staticpro (&Qmargin);
24256 Qpointer = intern ("pointer");
24257 staticpro (&Qpointer);
24258 Qleft_margin = intern ("left-margin");
24259 staticpro (&Qleft_margin);
24260 Qright_margin = intern ("right-margin");
24261 staticpro (&Qright_margin);
24262 Qcenter = intern ("center");
24263 staticpro (&Qcenter);
24264 Qline_height = intern ("line-height");
24265 staticpro (&Qline_height);
24266 QCalign_to = intern (":align-to");
24267 staticpro (&QCalign_to);
24268 QCrelative_width = intern (":relative-width");
24269 staticpro (&QCrelative_width);
24270 QCrelative_height = intern (":relative-height");
24271 staticpro (&QCrelative_height);
24272 QCeval = intern (":eval");
24273 staticpro (&QCeval);
24274 QCpropertize = intern (":propertize");
24275 staticpro (&QCpropertize);
24276 QCfile = intern (":file");
24277 staticpro (&QCfile);
24278 Qfontified = intern ("fontified");
24279 staticpro (&Qfontified);
24280 Qfontification_functions = intern ("fontification-functions");
24281 staticpro (&Qfontification_functions);
24282 Qtrailing_whitespace = intern ("trailing-whitespace");
24283 staticpro (&Qtrailing_whitespace);
24284 Qescape_glyph = intern ("escape-glyph");
24285 staticpro (&Qescape_glyph);
24286 Qnobreak_space = intern ("nobreak-space");
24287 staticpro (&Qnobreak_space);
24288 Qimage = intern ("image");
24289 staticpro (&Qimage);
24290 QCmap = intern (":map");
24291 staticpro (&QCmap);
24292 QCpointer = intern (":pointer");
24293 staticpro (&QCpointer);
24294 Qrect = intern ("rect");
24295 staticpro (&Qrect);
24296 Qcircle = intern ("circle");
24297 staticpro (&Qcircle);
24298 Qpoly = intern ("poly");
24299 staticpro (&Qpoly);
24300 Qmessage_truncate_lines = intern ("message-truncate-lines");
24301 staticpro (&Qmessage_truncate_lines);
24302 Qgrow_only = intern ("grow-only");
24303 staticpro (&Qgrow_only);
24304 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24305 staticpro (&Qinhibit_menubar_update);
24306 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24307 staticpro (&Qinhibit_eval_during_redisplay);
24308 Qposition = intern ("position");
24309 staticpro (&Qposition);
24310 Qbuffer_position = intern ("buffer-position");
24311 staticpro (&Qbuffer_position);
24312 Qobject = intern ("object");
24313 staticpro (&Qobject);
24314 Qbar = intern ("bar");
24315 staticpro (&Qbar);
24316 Qhbar = intern ("hbar");
24317 staticpro (&Qhbar);
24318 Qbox = intern ("box");
24319 staticpro (&Qbox);
24320 Qhollow = intern ("hollow");
24321 staticpro (&Qhollow);
24322 Qhand = intern ("hand");
24323 staticpro (&Qhand);
24324 Qarrow = intern ("arrow");
24325 staticpro (&Qarrow);
24326 Qtext = intern ("text");
24327 staticpro (&Qtext);
24328 Qrisky_local_variable = intern ("risky-local-variable");
24329 staticpro (&Qrisky_local_variable);
24330 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24331 staticpro (&Qinhibit_free_realized_faces);
24332
24333 list_of_error = Fcons (Fcons (intern ("error"),
24334 Fcons (intern ("void-variable"), Qnil)),
24335 Qnil);
24336 staticpro (&list_of_error);
24337
24338 Qlast_arrow_position = intern ("last-arrow-position");
24339 staticpro (&Qlast_arrow_position);
24340 Qlast_arrow_string = intern ("last-arrow-string");
24341 staticpro (&Qlast_arrow_string);
24342
24343 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24344 staticpro (&Qoverlay_arrow_string);
24345 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24346 staticpro (&Qoverlay_arrow_bitmap);
24347
24348 echo_buffer[0] = echo_buffer[1] = Qnil;
24349 staticpro (&echo_buffer[0]);
24350 staticpro (&echo_buffer[1]);
24351
24352 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24353 staticpro (&echo_area_buffer[0]);
24354 staticpro (&echo_area_buffer[1]);
24355
24356 Vmessages_buffer_name = build_string ("*Messages*");
24357 staticpro (&Vmessages_buffer_name);
24358
24359 mode_line_proptrans_alist = Qnil;
24360 staticpro (&mode_line_proptrans_alist);
24361 mode_line_string_list = Qnil;
24362 staticpro (&mode_line_string_list);
24363 mode_line_string_face = Qnil;
24364 staticpro (&mode_line_string_face);
24365 mode_line_string_face_prop = Qnil;
24366 staticpro (&mode_line_string_face_prop);
24367 Vmode_line_unwind_vector = Qnil;
24368 staticpro (&Vmode_line_unwind_vector);
24369
24370 help_echo_string = Qnil;
24371 staticpro (&help_echo_string);
24372 help_echo_object = Qnil;
24373 staticpro (&help_echo_object);
24374 help_echo_window = Qnil;
24375 staticpro (&help_echo_window);
24376 previous_help_echo_string = Qnil;
24377 staticpro (&previous_help_echo_string);
24378 help_echo_pos = -1;
24379
24380 #ifdef HAVE_WINDOW_SYSTEM
24381 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24382 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24383 For example, if a block cursor is over a tab, it will be drawn as
24384 wide as that tab on the display. */);
24385 x_stretch_cursor_p = 0;
24386 #endif
24387
24388 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24389 doc: /* *Non-nil means highlight trailing whitespace.
24390 The face used for trailing whitespace is `trailing-whitespace'. */);
24391 Vshow_trailing_whitespace = Qnil;
24392
24393 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24394 doc: /* *Control highlighting of nobreak space and soft hyphen.
24395 A value of t means highlight the character itself (for nobreak space,
24396 use face `nobreak-space').
24397 A value of nil means no highlighting.
24398 Other values mean display the escape glyph followed by an ordinary
24399 space or ordinary hyphen. */);
24400 Vnobreak_char_display = Qt;
24401
24402 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24403 doc: /* *The pointer shape to show in void text areas.
24404 A value of nil means to show the text pointer. Other options are `arrow',
24405 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24406 Vvoid_text_area_pointer = Qarrow;
24407
24408 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24409 doc: /* Non-nil means don't actually do any redisplay.
24410 This is used for internal purposes. */);
24411 Vinhibit_redisplay = Qnil;
24412
24413 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24414 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24415 Vglobal_mode_string = Qnil;
24416
24417 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24418 doc: /* Marker for where to display an arrow on top of the buffer text.
24419 This must be the beginning of a line in order to work.
24420 See also `overlay-arrow-string'. */);
24421 Voverlay_arrow_position = Qnil;
24422
24423 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24424 doc: /* String to display as an arrow in non-window frames.
24425 See also `overlay-arrow-position'. */);
24426 Voverlay_arrow_string = build_string ("=>");
24427
24428 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24429 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24430 The symbols on this list are examined during redisplay to determine
24431 where to display overlay arrows. */);
24432 Voverlay_arrow_variable_list
24433 = Fcons (intern ("overlay-arrow-position"), Qnil);
24434
24435 DEFVAR_INT ("scroll-step", &scroll_step,
24436 doc: /* *The number of lines to try scrolling a window by when point moves out.
24437 If that fails to bring point back on frame, point is centered instead.
24438 If this is zero, point is always centered after it moves off frame.
24439 If you want scrolling to always be a line at a time, you should set
24440 `scroll-conservatively' to a large value rather than set this to 1. */);
24441
24442 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24443 doc: /* *Scroll up to this many lines, to bring point back on screen.
24444 If point moves off-screen, redisplay will scroll by up to
24445 `scroll-conservatively' lines in order to bring point just barely
24446 onto the screen again. If that cannot be done, then redisplay
24447 recenters point as usual.
24448
24449 A value of zero means always recenter point if it moves off screen. */);
24450 scroll_conservatively = 0;
24451
24452 DEFVAR_INT ("scroll-margin", &scroll_margin,
24453 doc: /* *Number of lines of margin at the top and bottom of a window.
24454 Recenter the window whenever point gets within this many lines
24455 of the top or bottom of the window. */);
24456 scroll_margin = 0;
24457
24458 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24459 doc: /* Pixels per inch value for non-window system displays.
24460 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24461 Vdisplay_pixels_per_inch = make_float (72.0);
24462
24463 #if GLYPH_DEBUG
24464 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24465 #endif
24466
24467 DEFVAR_BOOL ("truncate-partial-width-windows",
24468 &truncate_partial_width_windows,
24469 doc: /* *Non-nil means truncate lines in all windows less than full frame wide.
24470 Nil means to respect the value of `truncate-lines'. */);
24471 truncate_partial_width_windows = 1;
24472
24473 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24474 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24475 Any other value means to use the appropriate face, `mode-line',
24476 `header-line', or `menu' respectively. */);
24477 mode_line_inverse_video = 1;
24478
24479 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24480 doc: /* *Maximum buffer size for which line number should be displayed.
24481 If the buffer is bigger than this, the line number does not appear
24482 in the mode line. A value of nil means no limit. */);
24483 Vline_number_display_limit = Qnil;
24484
24485 DEFVAR_INT ("line-number-display-limit-width",
24486 &line_number_display_limit_width,
24487 doc: /* *Maximum line width (in characters) for line number display.
24488 If the average length of the lines near point is bigger than this, then the
24489 line number may be omitted from the mode line. */);
24490 line_number_display_limit_width = 200;
24491
24492 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24493 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24494 highlight_nonselected_windows = 0;
24495
24496 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24497 doc: /* Non-nil if more than one frame is visible on this display.
24498 Minibuffer-only frames don't count, but iconified frames do.
24499 This variable is not guaranteed to be accurate except while processing
24500 `frame-title-format' and `icon-title-format'. */);
24501
24502 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24503 doc: /* Template for displaying the title bar of visible frames.
24504 \(Assuming the window manager supports this feature.)
24505
24506 This variable has the same structure as `mode-line-format', except that
24507 the %c and %l constructs are ignored. It is used only on frames for
24508 which no explicit name has been set \(see `modify-frame-parameters'). */);
24509
24510 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24511 doc: /* Template for displaying the title bar of an iconified frame.
24512 \(Assuming the window manager supports this feature.)
24513 This variable has the same structure as `mode-line-format' (which see),
24514 and is used only on frames for which no explicit name has been set
24515 \(see `modify-frame-parameters'). */);
24516 Vicon_title_format
24517 = Vframe_title_format
24518 = Fcons (intern ("multiple-frames"),
24519 Fcons (build_string ("%b"),
24520 Fcons (Fcons (empty_unibyte_string,
24521 Fcons (intern ("invocation-name"),
24522 Fcons (build_string ("@"),
24523 Fcons (intern ("system-name"),
24524 Qnil)))),
24525 Qnil)));
24526
24527 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24528 doc: /* Maximum number of lines to keep in the message log buffer.
24529 If nil, disable message logging. If t, log messages but don't truncate
24530 the buffer when it becomes large. */);
24531 Vmessage_log_max = make_number (100);
24532
24533 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24534 doc: /* Functions called before redisplay, if window sizes have changed.
24535 The value should be a list of functions that take one argument.
24536 Just before redisplay, for each frame, if any of its windows have changed
24537 size since the last redisplay, or have been split or deleted,
24538 all the functions in the list are called, with the frame as argument. */);
24539 Vwindow_size_change_functions = Qnil;
24540
24541 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24542 doc: /* List of functions to call before redisplaying a window with scrolling.
24543 Each function is called with two arguments, the window
24544 and its new display-start position. Note that the value of `window-end'
24545 is not valid when these functions are called. */);
24546 Vwindow_scroll_functions = Qnil;
24547
24548 DEFVAR_LISP ("window-text-change-functions",
24549 &Vwindow_text_change_functions,
24550 doc: /* Functions to call in redisplay when text in the window might change. */);
24551 Vwindow_text_change_functions = Qnil;
24552
24553 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24554 doc: /* Functions called when redisplay of a window reaches the end trigger.
24555 Each function is called with two arguments, the window and the end trigger value.
24556 See `set-window-redisplay-end-trigger'. */);
24557 Vredisplay_end_trigger_functions = Qnil;
24558
24559 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24560 doc: /* *Non-nil means autoselect window with mouse pointer.
24561 If nil, do not autoselect windows.
24562 A positive number means delay autoselection by that many seconds: a
24563 window is autoselected only after the mouse has remained in that
24564 window for the duration of the delay.
24565 A negative number has a similar effect, but causes windows to be
24566 autoselected only after the mouse has stopped moving. \(Because of
24567 the way Emacs compares mouse events, you will occasionally wait twice
24568 that time before the window gets selected.\)
24569 Any other value means to autoselect window instantaneously when the
24570 mouse pointer enters it.
24571
24572 Autoselection selects the minibuffer only if it is active, and never
24573 unselects the minibuffer if it is active.
24574
24575 When customizing this variable make sure that the actual value of
24576 `focus-follows-mouse' matches the behavior of your window manager. */);
24577 Vmouse_autoselect_window = Qnil;
24578
24579 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24580 doc: /* *Non-nil means automatically resize tool-bars.
24581 This dynamically changes the tool-bar's height to the minimum height
24582 that is needed to make all tool-bar items visible.
24583 If value is `grow-only', the tool-bar's height is only increased
24584 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24585 Vauto_resize_tool_bars = Qt;
24586
24587 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24588 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24589 auto_raise_tool_bar_buttons_p = 1;
24590
24591 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24592 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24593 make_cursor_line_fully_visible_p = 1;
24594
24595 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24596 doc: /* *Border below tool-bar in pixels.
24597 If an integer, use it as the height of the border.
24598 If it is one of `internal-border-width' or `border-width', use the
24599 value of the corresponding frame parameter.
24600 Otherwise, no border is added below the tool-bar. */);
24601 Vtool_bar_border = Qinternal_border_width;
24602
24603 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24604 doc: /* *Margin around tool-bar buttons in pixels.
24605 If an integer, use that for both horizontal and vertical margins.
24606 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24607 HORZ specifying the horizontal margin, and VERT specifying the
24608 vertical margin. */);
24609 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24610
24611 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24612 doc: /* *Relief thickness of tool-bar buttons. */);
24613 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24614
24615 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24616 doc: /* List of functions to call to fontify regions of text.
24617 Each function is called with one argument POS. Functions must
24618 fontify a region starting at POS in the current buffer, and give
24619 fontified regions the property `fontified'. */);
24620 Vfontification_functions = Qnil;
24621 Fmake_variable_buffer_local (Qfontification_functions);
24622
24623 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24624 &unibyte_display_via_language_environment,
24625 doc: /* *Non-nil means display unibyte text according to language environment.
24626 Specifically this means that unibyte non-ASCII characters
24627 are displayed by converting them to the equivalent multibyte characters
24628 according to the current language environment. As a result, they are
24629 displayed according to the current fontset. */);
24630 unibyte_display_via_language_environment = 0;
24631
24632 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24633 doc: /* *Maximum height for resizing mini-windows.
24634 If a float, it specifies a fraction of the mini-window frame's height.
24635 If an integer, it specifies a number of lines. */);
24636 Vmax_mini_window_height = make_float (0.25);
24637
24638 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24639 doc: /* *How to resize mini-windows.
24640 A value of nil means don't automatically resize mini-windows.
24641 A value of t means resize them to fit the text displayed in them.
24642 A value of `grow-only', the default, means let mini-windows grow
24643 only, until their display becomes empty, at which point the windows
24644 go back to their normal size. */);
24645 Vresize_mini_windows = Qgrow_only;
24646
24647 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24648 doc: /* Alist specifying how to blink the cursor off.
24649 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24650 `cursor-type' frame-parameter or variable equals ON-STATE,
24651 comparing using `equal', Emacs uses OFF-STATE to specify
24652 how to blink it off. ON-STATE and OFF-STATE are values for
24653 the `cursor-type' frame parameter.
24654
24655 If a frame's ON-STATE has no entry in this list,
24656 the frame's other specifications determine how to blink the cursor off. */);
24657 Vblink_cursor_alist = Qnil;
24658
24659 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24660 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24661 automatic_hscrolling_p = 1;
24662 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
24663 staticpro (&Qauto_hscroll_mode);
24664
24665 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24666 doc: /* *How many columns away from the window edge point is allowed to get
24667 before automatic hscrolling will horizontally scroll the window. */);
24668 hscroll_margin = 5;
24669
24670 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24671 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24672 When point is less than `hscroll-margin' columns from the window
24673 edge, automatic hscrolling will scroll the window by the amount of columns
24674 determined by this variable. If its value is a positive integer, scroll that
24675 many columns. If it's a positive floating-point number, it specifies the
24676 fraction of the window's width to scroll. If it's nil or zero, point will be
24677 centered horizontally after the scroll. Any other value, including negative
24678 numbers, are treated as if the value were zero.
24679
24680 Automatic hscrolling always moves point outside the scroll margin, so if
24681 point was more than scroll step columns inside the margin, the window will
24682 scroll more than the value given by the scroll step.
24683
24684 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24685 and `scroll-right' overrides this variable's effect. */);
24686 Vhscroll_step = make_number (0);
24687
24688 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24689 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24690 Bind this around calls to `message' to let it take effect. */);
24691 message_truncate_lines = 0;
24692
24693 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24694 doc: /* Normal hook run to update the menu bar definitions.
24695 Redisplay runs this hook before it redisplays the menu bar.
24696 This is used to update submenus such as Buffers,
24697 whose contents depend on various data. */);
24698 Vmenu_bar_update_hook = Qnil;
24699
24700 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24701 doc: /* Frame for which we are updating a menu.
24702 The enable predicate for a menu binding should check this variable. */);
24703 Vmenu_updating_frame = Qnil;
24704
24705 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24706 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24707 inhibit_menubar_update = 0;
24708
24709 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24710 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24711 inhibit_eval_during_redisplay = 0;
24712
24713 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24714 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24715 inhibit_free_realized_faces = 0;
24716
24717 #if GLYPH_DEBUG
24718 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24719 doc: /* Inhibit try_window_id display optimization. */);
24720 inhibit_try_window_id = 0;
24721
24722 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24723 doc: /* Inhibit try_window_reusing display optimization. */);
24724 inhibit_try_window_reusing = 0;
24725
24726 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24727 doc: /* Inhibit try_cursor_movement display optimization. */);
24728 inhibit_try_cursor_movement = 0;
24729 #endif /* GLYPH_DEBUG */
24730
24731 DEFVAR_INT ("overline-margin", &overline_margin,
24732 doc: /* *Space between overline and text, in pixels.
24733 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24734 margin to the caracter height. */);
24735 overline_margin = 2;
24736 }
24737
24738
24739 /* Initialize this module when Emacs starts. */
24740
24741 void
24742 init_xdisp ()
24743 {
24744 Lisp_Object root_window;
24745 struct window *mini_w;
24746
24747 current_header_line_height = current_mode_line_height = -1;
24748
24749 CHARPOS (this_line_start_pos) = 0;
24750
24751 mini_w = XWINDOW (minibuf_window);
24752 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24753
24754 if (!noninteractive)
24755 {
24756 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24757 int i;
24758
24759 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24760 set_window_height (root_window,
24761 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24762 0);
24763 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24764 set_window_height (minibuf_window, 1, 0);
24765
24766 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24767 mini_w->total_cols = make_number (FRAME_COLS (f));
24768
24769 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24770 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24771 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24772
24773 /* The default ellipsis glyphs `...'. */
24774 for (i = 0; i < 3; ++i)
24775 default_invis_vector[i] = make_number ('.');
24776 }
24777
24778 {
24779 /* Allocate the buffer for frame titles.
24780 Also used for `format-mode-line'. */
24781 int size = 100;
24782 mode_line_noprop_buf = (char *) xmalloc (size);
24783 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24784 mode_line_noprop_ptr = mode_line_noprop_buf;
24785 mode_line_target = MODE_LINE_DISPLAY;
24786 }
24787
24788 help_echo_showing_p = 0;
24789 }
24790
24791
24792 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24793 (do not change this comment) */