* window.el (display-buffer-function, special-display-p)
[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 #include "font.h"
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
219
220 extern Lisp_Object do_mouse_tracking;
221
222 extern int minibuffer_auto_raise;
223 extern Lisp_Object Vminibuffer_list;
224
225 extern Lisp_Object Qface;
226 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227
228 extern Lisp_Object Voverriding_local_map;
229 extern Lisp_Object Voverriding_local_map_menu_flag;
230 extern Lisp_Object Qmenu_item;
231 extern Lisp_Object Qwhen;
232 extern Lisp_Object Qhelp_echo;
233
234 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
235 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
236 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
237 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
238 Lisp_Object Qinhibit_point_motion_hooks;
239 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
240 Lisp_Object Qfontified;
241 Lisp_Object Qgrow_only;
242 Lisp_Object Qinhibit_eval_during_redisplay;
243 Lisp_Object Qbuffer_position, Qposition, Qobject;
244
245 /* Cursor shapes */
246 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
247
248 /* Pointer shapes */
249 Lisp_Object Qarrow, Qhand, Qtext;
250
251 Lisp_Object Qrisky_local_variable;
252
253 /* Holds the list (error). */
254 Lisp_Object list_of_error;
255
256 /* Functions called to fontify regions of text. */
257
258 Lisp_Object Vfontification_functions;
259 Lisp_Object Qfontification_functions;
260
261 /* Non-nil means automatically select any window when the mouse
262 cursor moves into it. */
263 Lisp_Object Vmouse_autoselect_window;
264
265 /* Non-zero means draw tool bar buttons raised when the mouse moves
266 over them. */
267
268 int auto_raise_tool_bar_buttons_p;
269
270 /* Non-zero means to reposition window if cursor line is only partially visible. */
271
272 int make_cursor_line_fully_visible_p;
273
274 /* Margin below tool bar in pixels. 0 or nil means no margin.
275 If value is `internal-border-width' or `border-width',
276 the corresponding frame parameter is used. */
277
278 Lisp_Object Vtool_bar_border;
279
280 /* Margin around tool bar buttons in pixels. */
281
282 Lisp_Object Vtool_bar_button_margin;
283
284 /* Thickness of shadow to draw around tool bar buttons. */
285
286 EMACS_INT tool_bar_button_relief;
287
288 /* Non-nil means automatically resize tool-bars so that all tool-bar
289 items are visible, and no blank lines remain.
290
291 If value is `grow-only', only make tool-bar bigger. */
292
293 Lisp_Object Vauto_resize_tool_bars;
294
295 /* Non-zero means draw block and hollow cursor as wide as the glyph
296 under it. For example, if a block cursor is over a tab, it will be
297 drawn as wide as that tab on the display. */
298
299 int x_stretch_cursor_p;
300
301 /* Non-nil means don't actually do any redisplay. */
302
303 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
304
305 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
306
307 int inhibit_eval_during_redisplay;
308
309 /* Names of text properties relevant for redisplay. */
310
311 Lisp_Object Qdisplay;
312 extern Lisp_Object Qface, Qinvisible, Qwidth;
313
314 /* Symbols used in text property values. */
315
316 Lisp_Object Vdisplay_pixels_per_inch;
317 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
318 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
319 Lisp_Object Qslice;
320 Lisp_Object Qcenter;
321 Lisp_Object Qmargin, Qpointer;
322 Lisp_Object Qline_height;
323 extern Lisp_Object Qheight;
324 extern Lisp_Object QCwidth, QCheight, QCascent;
325 extern Lisp_Object Qscroll_bar;
326 extern Lisp_Object Qcursor;
327
328 /* Non-nil means highlight trailing whitespace. */
329
330 Lisp_Object Vshow_trailing_whitespace;
331
332 /* Non-nil means escape non-break space and hyphens. */
333
334 Lisp_Object Vnobreak_char_display;
335
336 #ifdef HAVE_WINDOW_SYSTEM
337 extern Lisp_Object Voverflow_newline_into_fringe;
338
339 /* Test if overflow newline into fringe. Called with iterator IT
340 at or past right window margin, and with IT->current_x set. */
341
342 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
343 (!NILP (Voverflow_newline_into_fringe) \
344 && FRAME_WINDOW_P (it->f) \
345 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
346 && it->current_x == it->last_visible_x)
347
348 #endif /* HAVE_WINDOW_SYSTEM */
349
350 /* Non-nil means show the text cursor in void text areas
351 i.e. in blank areas after eol and eob. This used to be
352 the default in 21.3. */
353
354 Lisp_Object Vvoid_text_area_pointer;
355
356 /* Name of the face used to highlight trailing whitespace. */
357
358 Lisp_Object Qtrailing_whitespace;
359
360 /* Name and number of the face used to highlight escape glyphs. */
361
362 Lisp_Object Qescape_glyph;
363
364 /* Name and number of the face used to highlight non-breaking spaces. */
365
366 Lisp_Object Qnobreak_space;
367
368 /* The symbol `image' which is the car of the lists used to represent
369 images in Lisp. */
370
371 Lisp_Object Qimage;
372
373 /* The image map types. */
374 Lisp_Object QCmap, QCpointer;
375 Lisp_Object Qrect, Qcircle, Qpoly;
376
377 /* Non-zero means print newline to stdout before next mini-buffer
378 message. */
379
380 int noninteractive_need_newline;
381
382 /* Non-zero means print newline to message log before next message. */
383
384 static int message_log_need_newline;
385
386 /* Three markers that message_dolog uses.
387 It could allocate them itself, but that causes trouble
388 in handling memory-full errors. */
389 static Lisp_Object message_dolog_marker1;
390 static Lisp_Object message_dolog_marker2;
391 static Lisp_Object message_dolog_marker3;
392 \f
393 /* The buffer position of the first character appearing entirely or
394 partially on the line of the selected window which contains the
395 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
396 redisplay optimization in redisplay_internal. */
397
398 static struct text_pos this_line_start_pos;
399
400 /* Number of characters past the end of the line above, including the
401 terminating newline. */
402
403 static struct text_pos this_line_end_pos;
404
405 /* The vertical positions and the height of this line. */
406
407 static int this_line_vpos;
408 static int this_line_y;
409 static int this_line_pixel_height;
410
411 /* X position at which this display line starts. Usually zero;
412 negative if first character is partially visible. */
413
414 static int this_line_start_x;
415
416 /* Buffer that this_line_.* variables are referring to. */
417
418 static struct buffer *this_line_buffer;
419
420 /* Nonzero means truncate lines in all windows less wide than the
421 frame. */
422
423 int truncate_partial_width_windows;
424
425 /* A flag to control how to display unibyte 8-bit character. */
426
427 int unibyte_display_via_language_environment;
428
429 /* Nonzero means we have more than one non-mini-buffer-only frame.
430 Not guaranteed to be accurate except while parsing
431 frame-title-format. */
432
433 int multiple_frames;
434
435 Lisp_Object Vglobal_mode_string;
436
437
438 /* List of variables (symbols) which hold markers for overlay arrows.
439 The symbols on this list are examined during redisplay to determine
440 where to display overlay arrows. */
441
442 Lisp_Object Voverlay_arrow_variable_list;
443
444 /* Marker for where to display an arrow on top of the buffer text. */
445
446 Lisp_Object Voverlay_arrow_position;
447
448 /* String to display for the arrow. Only used on terminal frames. */
449
450 Lisp_Object Voverlay_arrow_string;
451
452 /* Values of those variables at last redisplay are stored as
453 properties on `overlay-arrow-position' symbol. However, if
454 Voverlay_arrow_position is a marker, last-arrow-position is its
455 numerical position. */
456
457 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
458
459 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
460 properties on a symbol in overlay-arrow-variable-list. */
461
462 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
463
464 /* Like mode-line-format, but for the title bar on a visible frame. */
465
466 Lisp_Object Vframe_title_format;
467
468 /* Like mode-line-format, but for the title bar on an iconified frame. */
469
470 Lisp_Object Vicon_title_format;
471
472 /* List of functions to call when a window's size changes. These
473 functions get one arg, a frame on which one or more windows' sizes
474 have changed. */
475
476 static Lisp_Object Vwindow_size_change_functions;
477
478 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
479
480 /* Nonzero if an overlay arrow has been displayed in this window. */
481
482 static int overlay_arrow_seen;
483
484 /* Nonzero means highlight the region even in nonselected windows. */
485
486 int highlight_nonselected_windows;
487
488 /* If cursor motion alone moves point off frame, try scrolling this
489 many lines up or down if that will bring it back. */
490
491 static EMACS_INT scroll_step;
492
493 /* Nonzero means scroll just far enough to bring point back on the
494 screen, when appropriate. */
495
496 static EMACS_INT scroll_conservatively;
497
498 /* Recenter the window whenever point gets within this many lines of
499 the top or bottom of the window. This value is translated into a
500 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
501 that there is really a fixed pixel height scroll margin. */
502
503 EMACS_INT scroll_margin;
504
505 /* Number of windows showing the buffer of the selected window (or
506 another buffer with the same base buffer). keyboard.c refers to
507 this. */
508
509 int buffer_shared;
510
511 /* Vector containing glyphs for an ellipsis `...'. */
512
513 static Lisp_Object default_invis_vector[3];
514
515 /* Zero means display the mode-line/header-line/menu-bar in the default face
516 (this slightly odd definition is for compatibility with previous versions
517 of emacs), non-zero means display them using their respective faces.
518
519 This variable is deprecated. */
520
521 int mode_line_inverse_video;
522
523 /* Prompt to display in front of the mini-buffer contents. */
524
525 Lisp_Object minibuf_prompt;
526
527 /* Width of current mini-buffer prompt. Only set after display_line
528 of the line that contains the prompt. */
529
530 int minibuf_prompt_width;
531
532 /* This is the window where the echo area message was displayed. It
533 is always a mini-buffer window, but it may not be the same window
534 currently active as a mini-buffer. */
535
536 Lisp_Object echo_area_window;
537
538 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
539 pushes the current message and the value of
540 message_enable_multibyte on the stack, the function restore_message
541 pops the stack and displays MESSAGE again. */
542
543 Lisp_Object Vmessage_stack;
544
545 /* Nonzero means multibyte characters were enabled when the echo area
546 message was specified. */
547
548 int message_enable_multibyte;
549
550 /* Nonzero if we should redraw the mode lines on the next redisplay. */
551
552 int update_mode_lines;
553
554 /* Nonzero if window sizes or contents have changed since last
555 redisplay that finished. */
556
557 int windows_or_buffers_changed;
558
559 /* Nonzero means a frame's cursor type has been changed. */
560
561 int cursor_type_changed;
562
563 /* Nonzero after display_mode_line if %l was used and it displayed a
564 line number. */
565
566 int line_number_displayed;
567
568 /* Maximum buffer size for which to display line numbers. */
569
570 Lisp_Object Vline_number_display_limit;
571
572 /* Line width to consider when repositioning for line number display. */
573
574 static EMACS_INT line_number_display_limit_width;
575
576 /* Number of lines to keep in the message log buffer. t means
577 infinite. nil means don't log at all. */
578
579 Lisp_Object Vmessage_log_max;
580
581 /* The name of the *Messages* buffer, a string. */
582
583 static Lisp_Object Vmessages_buffer_name;
584
585 /* Current, index 0, and last displayed echo area message. Either
586 buffers from echo_buffers, or nil to indicate no message. */
587
588 Lisp_Object echo_area_buffer[2];
589
590 /* The buffers referenced from echo_area_buffer. */
591
592 static Lisp_Object echo_buffer[2];
593
594 /* A vector saved used in with_area_buffer to reduce consing. */
595
596 static Lisp_Object Vwith_echo_area_save_vector;
597
598 /* Non-zero means display_echo_area should display the last echo area
599 message again. Set by redisplay_preserve_echo_area. */
600
601 static int display_last_displayed_message_p;
602
603 /* Nonzero if echo area is being used by print; zero if being used by
604 message. */
605
606 int message_buf_print;
607
608 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
609
610 Lisp_Object Qinhibit_menubar_update;
611 int inhibit_menubar_update;
612
613 /* When evaluating expressions from menu bar items (enable conditions,
614 for instance), this is the frame they are being processed for. */
615
616 Lisp_Object Vmenu_updating_frame;
617
618 /* Maximum height for resizing mini-windows. Either a float
619 specifying a fraction of the available height, or an integer
620 specifying a number of lines. */
621
622 Lisp_Object Vmax_mini_window_height;
623
624 /* Non-zero means messages should be displayed with truncated
625 lines instead of being continued. */
626
627 int message_truncate_lines;
628 Lisp_Object Qmessage_truncate_lines;
629
630 /* Set to 1 in clear_message to make redisplay_internal aware
631 of an emptied echo area. */
632
633 static int message_cleared_p;
634
635 /* How to blink the default frame cursor off. */
636 Lisp_Object Vblink_cursor_alist;
637
638 /* A scratch glyph row with contents used for generating truncation
639 glyphs. Also used in direct_output_for_insert. */
640
641 #define MAX_SCRATCH_GLYPHS 100
642 struct glyph_row scratch_glyph_row;
643 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
644
645 /* Ascent and height of the last line processed by move_it_to. */
646
647 static int last_max_ascent, last_height;
648
649 /* Non-zero if there's a help-echo in the echo area. */
650
651 int help_echo_showing_p;
652
653 /* If >= 0, computed, exact values of mode-line and header-line height
654 to use in the macros CURRENT_MODE_LINE_HEIGHT and
655 CURRENT_HEADER_LINE_HEIGHT. */
656
657 int current_mode_line_height, current_header_line_height;
658
659 /* The maximum distance to look ahead for text properties. Values
660 that are too small let us call compute_char_face and similar
661 functions too often which is expensive. Values that are too large
662 let us call compute_char_face and alike too often because we
663 might not be interested in text properties that far away. */
664
665 #define TEXT_PROP_DISTANCE_LIMIT 100
666
667 #if GLYPH_DEBUG
668
669 /* Variables to turn off display optimizations from Lisp. */
670
671 int inhibit_try_window_id, inhibit_try_window_reusing;
672 int inhibit_try_cursor_movement;
673
674 /* Non-zero means print traces of redisplay if compiled with
675 GLYPH_DEBUG != 0. */
676
677 int trace_redisplay_p;
678
679 #endif /* GLYPH_DEBUG */
680
681 #ifdef DEBUG_TRACE_MOVE
682 /* Non-zero means trace with TRACE_MOVE to stderr. */
683 int trace_move;
684
685 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
686 #else
687 #define TRACE_MOVE(x) (void) 0
688 #endif
689
690 /* Non-zero means automatically scroll windows horizontally to make
691 point visible. */
692
693 int automatic_hscrolling_p;
694 Lisp_Object Qauto_hscroll_mode;
695
696 /* How close to the margin can point get before the window is scrolled
697 horizontally. */
698 EMACS_INT hscroll_margin;
699
700 /* How much to scroll horizontally when point is inside the above margin. */
701 Lisp_Object Vhscroll_step;
702
703 /* The variable `resize-mini-windows'. If nil, don't resize
704 mini-windows. If t, always resize them to fit the text they
705 display. If `grow-only', let mini-windows grow only until they
706 become empty. */
707
708 Lisp_Object Vresize_mini_windows;
709
710 /* Buffer being redisplayed -- for redisplay_window_error. */
711
712 struct buffer *displayed_buffer;
713
714 /* Space between overline and text. */
715
716 EMACS_INT overline_margin;
717
718 /* Value returned from text property handlers (see below). */
719
720 enum prop_handled
721 {
722 HANDLED_NORMALLY,
723 HANDLED_RECOMPUTE_PROPS,
724 HANDLED_OVERLAY_STRING_CONSUMED,
725 HANDLED_RETURN
726 };
727
728 /* A description of text properties that redisplay is interested
729 in. */
730
731 struct props
732 {
733 /* The name of the property. */
734 Lisp_Object *name;
735
736 /* A unique index for the property. */
737 enum prop_idx idx;
738
739 /* A handler function called to set up iterator IT from the property
740 at IT's current position. Value is used to steer handle_stop. */
741 enum prop_handled (*handler) P_ ((struct it *it));
742 };
743
744 static enum prop_handled handle_face_prop P_ ((struct it *));
745 static enum prop_handled handle_invisible_prop P_ ((struct it *));
746 static enum prop_handled handle_display_prop P_ ((struct it *));
747 static enum prop_handled handle_composition_prop P_ ((struct it *));
748 static enum prop_handled handle_overlay_change P_ ((struct it *));
749 static enum prop_handled handle_fontified_prop P_ ((struct it *));
750 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
751
752 /* Properties handled by iterators. */
753
754 static struct props it_props[] =
755 {
756 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
757 /* Handle `face' before `display' because some sub-properties of
758 `display' need to know the face. */
759 {&Qface, FACE_PROP_IDX, handle_face_prop},
760 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
761 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
762 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
763 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
764 {NULL, 0, NULL}
765 };
766
767 /* Value is the position described by X. If X is a marker, value is
768 the marker_position of X. Otherwise, value is X. */
769
770 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
771
772 /* Enumeration returned by some move_it_.* functions internally. */
773
774 enum move_it_result
775 {
776 /* Not used. Undefined value. */
777 MOVE_UNDEFINED,
778
779 /* Move ended at the requested buffer position or ZV. */
780 MOVE_POS_MATCH_OR_ZV,
781
782 /* Move ended at the requested X pixel position. */
783 MOVE_X_REACHED,
784
785 /* Move within a line ended at the end of a line that must be
786 continued. */
787 MOVE_LINE_CONTINUED,
788
789 /* Move within a line ended at the end of a line that would
790 be displayed truncated. */
791 MOVE_LINE_TRUNCATED,
792
793 /* Move within a line ended at a line end. */
794 MOVE_NEWLINE_OR_CR
795 };
796
797 /* This counter is used to clear the face cache every once in a while
798 in redisplay_internal. It is incremented for each redisplay.
799 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
800 cleared. */
801
802 #define CLEAR_FACE_CACHE_COUNT 500
803 static int clear_face_cache_count;
804
805 /* Similarly for the image cache. */
806
807 #ifdef HAVE_WINDOW_SYSTEM
808 #define CLEAR_IMAGE_CACHE_COUNT 101
809 static int clear_image_cache_count;
810 #endif
811
812 /* Non-zero while redisplay_internal is in progress. */
813
814 int redisplaying_p;
815
816 /* Non-zero means don't free realized faces. Bound while freeing
817 realized faces is dangerous because glyph matrices might still
818 reference them. */
819
820 int inhibit_free_realized_faces;
821 Lisp_Object Qinhibit_free_realized_faces;
822
823 /* If a string, XTread_socket generates an event to display that string.
824 (The display is done in read_char.) */
825
826 Lisp_Object help_echo_string;
827 Lisp_Object help_echo_window;
828 Lisp_Object help_echo_object;
829 int help_echo_pos;
830
831 /* Temporary variable for XTread_socket. */
832
833 Lisp_Object previous_help_echo_string;
834
835 /* Null glyph slice */
836
837 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
838
839 \f
840 /* Function prototypes. */
841
842 static void setup_for_ellipsis P_ ((struct it *, int));
843 static void mark_window_display_accurate_1 P_ ((struct window *, int));
844 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
845 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
846 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
847 static int redisplay_mode_lines P_ ((Lisp_Object, int));
848 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
849
850 #if 0
851 static int invisible_text_between_p P_ ((struct it *, int, int));
852 #endif
853
854 static void pint2str P_ ((char *, int, int));
855 static void pint2hrstr P_ ((char *, int, int));
856 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
857 struct text_pos));
858 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
859 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
860 static void store_mode_line_noprop_char P_ ((char));
861 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
862 static void x_consider_frame_title P_ ((Lisp_Object));
863 static void handle_stop P_ ((struct it *));
864 static int tool_bar_lines_needed P_ ((struct frame *, int *));
865 static int single_display_spec_intangible_p P_ ((Lisp_Object));
866 static void ensure_echo_area_buffers P_ ((void));
867 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
868 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
869 static int with_echo_area_buffer P_ ((struct window *, int,
870 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
871 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static void clear_garbaged_frames P_ ((void));
873 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
874 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static int display_echo_area P_ ((struct window *));
877 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
879 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
880 static int string_char_and_length P_ ((const unsigned char *, int, int *));
881 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
882 struct text_pos));
883 static int compute_window_start_on_continuation_line P_ ((struct window *));
884 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
885 static void insert_left_trunc_glyphs P_ ((struct it *));
886 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
887 Lisp_Object));
888 static void extend_face_to_end_of_line P_ ((struct it *));
889 static int append_space_for_newline P_ ((struct it *, int));
890 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
891 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
892 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
893 static int trailing_whitespace_p P_ ((int));
894 static int message_log_check_duplicate P_ ((int, int, int, int));
895 static void push_it P_ ((struct it *));
896 static void pop_it P_ ((struct it *));
897 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
898 static void select_frame_for_redisplay P_ ((Lisp_Object));
899 static void redisplay_internal P_ ((int));
900 static int echo_area_display P_ ((int));
901 static void redisplay_windows P_ ((Lisp_Object));
902 static void redisplay_window P_ ((Lisp_Object, int));
903 static Lisp_Object redisplay_window_error ();
904 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
905 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
906 static int update_menu_bar P_ ((struct frame *, int, int));
907 static int try_window_reusing_current_matrix P_ ((struct window *));
908 static int try_window_id P_ ((struct window *));
909 static int display_line P_ ((struct it *));
910 static int display_mode_lines P_ ((struct window *));
911 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
912 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
913 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
914 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
915 static void display_menu_bar P_ ((struct window *));
916 static int display_count_lines P_ ((int, int, int, int, int *));
917 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
918 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
919 static void compute_line_metrics P_ ((struct it *));
920 static void run_redisplay_end_trigger_hook P_ ((struct it *));
921 static int get_overlay_strings P_ ((struct it *, int));
922 static int get_overlay_strings_1 P_ ((struct it *, int, int));
923 static void next_overlay_string P_ ((struct it *));
924 static void reseat P_ ((struct it *, struct text_pos, int));
925 static void reseat_1 P_ ((struct it *, struct text_pos, int));
926 static void back_to_previous_visible_line_start P_ ((struct it *));
927 void reseat_at_previous_visible_line_start P_ ((struct it *));
928 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
929 static int next_element_from_ellipsis P_ ((struct it *));
930 static int next_element_from_display_vector P_ ((struct it *));
931 static int next_element_from_string P_ ((struct it *));
932 static int next_element_from_c_string P_ ((struct it *));
933 static int next_element_from_buffer P_ ((struct it *));
934 static int next_element_from_composition P_ ((struct it *));
935 static int next_element_from_image P_ ((struct it *));
936 static int next_element_from_stretch P_ ((struct it *));
937 static void load_overlay_strings P_ ((struct it *, int));
938 static int init_from_display_pos P_ ((struct it *, struct window *,
939 struct display_pos *));
940 static void reseat_to_string P_ ((struct it *, unsigned char *,
941 Lisp_Object, int, int, int, int));
942 static enum move_it_result
943 move_it_in_display_line_to (struct it *, EMACS_INT, int,
944 enum move_operation_enum);
945 void move_it_vertically_backward P_ ((struct it *, int));
946 static void init_to_row_start P_ ((struct it *, struct window *,
947 struct glyph_row *));
948 static int init_to_row_end P_ ((struct it *, struct window *,
949 struct glyph_row *));
950 static void back_to_previous_line_start P_ ((struct it *));
951 static int forward_to_next_line_start P_ ((struct it *, int *));
952 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
953 Lisp_Object, int));
954 static struct text_pos string_pos P_ ((int, Lisp_Object));
955 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
956 static int number_of_chars P_ ((unsigned char *, int));
957 static void compute_stop_pos P_ ((struct it *));
958 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
959 Lisp_Object));
960 static int face_before_or_after_it_pos P_ ((struct it *, int));
961 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
962 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
963 Lisp_Object, Lisp_Object,
964 struct text_pos *, int));
965 static int underlying_face_id P_ ((struct it *));
966 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
967 struct window *));
968
969 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
970 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
971
972 #ifdef HAVE_WINDOW_SYSTEM
973
974 static void update_tool_bar P_ ((struct frame *, int));
975 static void build_desired_tool_bar_string P_ ((struct frame *f));
976 static int redisplay_tool_bar P_ ((struct frame *));
977 static void display_tool_bar_line P_ ((struct it *, int));
978 static void notice_overwritten_cursor P_ ((struct window *,
979 enum glyph_row_area,
980 int, int, int, int));
981
982
983
984 #endif /* HAVE_WINDOW_SYSTEM */
985
986 \f
987 /***********************************************************************
988 Window display dimensions
989 ***********************************************************************/
990
991 /* Return the bottom boundary y-position for text lines in window W.
992 This is the first y position at which a line cannot start.
993 It is relative to the top of the window.
994
995 This is the height of W minus the height of a mode line, if any. */
996
997 INLINE int
998 window_text_bottom_y (w)
999 struct window *w;
1000 {
1001 int height = WINDOW_TOTAL_HEIGHT (w);
1002
1003 if (WINDOW_WANTS_MODELINE_P (w))
1004 height -= CURRENT_MODE_LINE_HEIGHT (w);
1005 return height;
1006 }
1007
1008 /* Return the pixel width of display area AREA of window W. AREA < 0
1009 means return the total width of W, not including fringes to
1010 the left and right of the window. */
1011
1012 INLINE int
1013 window_box_width (w, area)
1014 struct window *w;
1015 int area;
1016 {
1017 int cols = XFASTINT (w->total_cols);
1018 int pixels = 0;
1019
1020 if (!w->pseudo_window_p)
1021 {
1022 cols -= WINDOW_SCROLL_BAR_COLS (w);
1023
1024 if (area == TEXT_AREA)
1025 {
1026 if (INTEGERP (w->left_margin_cols))
1027 cols -= XFASTINT (w->left_margin_cols);
1028 if (INTEGERP (w->right_margin_cols))
1029 cols -= XFASTINT (w->right_margin_cols);
1030 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1031 }
1032 else if (area == LEFT_MARGIN_AREA)
1033 {
1034 cols = (INTEGERP (w->left_margin_cols)
1035 ? XFASTINT (w->left_margin_cols) : 0);
1036 pixels = 0;
1037 }
1038 else if (area == RIGHT_MARGIN_AREA)
1039 {
1040 cols = (INTEGERP (w->right_margin_cols)
1041 ? XFASTINT (w->right_margin_cols) : 0);
1042 pixels = 0;
1043 }
1044 }
1045
1046 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1047 }
1048
1049
1050 /* Return the pixel height of the display area of window W, not
1051 including mode lines of W, if any. */
1052
1053 INLINE int
1054 window_box_height (w)
1055 struct window *w;
1056 {
1057 struct frame *f = XFRAME (w->frame);
1058 int height = WINDOW_TOTAL_HEIGHT (w);
1059
1060 xassert (height >= 0);
1061
1062 /* Note: the code below that determines the mode-line/header-line
1063 height is essentially the same as that contained in the macro
1064 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1065 the appropriate glyph row has its `mode_line_p' flag set,
1066 and if it doesn't, uses estimate_mode_line_height instead. */
1067
1068 if (WINDOW_WANTS_MODELINE_P (w))
1069 {
1070 struct glyph_row *ml_row
1071 = (w->current_matrix && w->current_matrix->rows
1072 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1073 : 0);
1074 if (ml_row && ml_row->mode_line_p)
1075 height -= ml_row->height;
1076 else
1077 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1078 }
1079
1080 if (WINDOW_WANTS_HEADER_LINE_P (w))
1081 {
1082 struct glyph_row *hl_row
1083 = (w->current_matrix && w->current_matrix->rows
1084 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1085 : 0);
1086 if (hl_row && hl_row->mode_line_p)
1087 height -= hl_row->height;
1088 else
1089 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1090 }
1091
1092 /* With a very small font and a mode-line that's taller than
1093 default, we might end up with a negative height. */
1094 return max (0, height);
1095 }
1096
1097 /* Return the window-relative coordinate of the left edge of display
1098 area AREA of window W. AREA < 0 means return the left edge of the
1099 whole window, to the right of the left fringe of W. */
1100
1101 INLINE int
1102 window_box_left_offset (w, area)
1103 struct window *w;
1104 int area;
1105 {
1106 int x;
1107
1108 if (w->pseudo_window_p)
1109 return 0;
1110
1111 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1112
1113 if (area == TEXT_AREA)
1114 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1115 + window_box_width (w, LEFT_MARGIN_AREA));
1116 else if (area == RIGHT_MARGIN_AREA)
1117 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1118 + window_box_width (w, LEFT_MARGIN_AREA)
1119 + window_box_width (w, TEXT_AREA)
1120 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1121 ? 0
1122 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1123 else if (area == LEFT_MARGIN_AREA
1124 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1125 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1126
1127 return x;
1128 }
1129
1130
1131 /* Return the window-relative coordinate of the right edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the left of the right fringe of W. */
1134
1135 INLINE int
1136 window_box_right_offset (w, area)
1137 struct window *w;
1138 int area;
1139 {
1140 return window_box_left_offset (w, area) + window_box_width (w, area);
1141 }
1142
1143 /* Return the frame-relative coordinate of the left edge of display
1144 area AREA of window W. AREA < 0 means return the left edge of the
1145 whole window, to the right of the left fringe of W. */
1146
1147 INLINE int
1148 window_box_left (w, area)
1149 struct window *w;
1150 int area;
1151 {
1152 struct frame *f = XFRAME (w->frame);
1153 int x;
1154
1155 if (w->pseudo_window_p)
1156 return FRAME_INTERNAL_BORDER_WIDTH (f);
1157
1158 x = (WINDOW_LEFT_EDGE_X (w)
1159 + window_box_left_offset (w, area));
1160
1161 return x;
1162 }
1163
1164
1165 /* Return the frame-relative coordinate of the right edge of display
1166 area AREA of window W. AREA < 0 means return the left edge of the
1167 whole window, to the left of the right fringe of W. */
1168
1169 INLINE int
1170 window_box_right (w, area)
1171 struct window *w;
1172 int area;
1173 {
1174 return window_box_left (w, area) + window_box_width (w, area);
1175 }
1176
1177 /* Get the bounding box of the display area AREA of window W, without
1178 mode lines, in frame-relative coordinates. AREA < 0 means the
1179 whole window, not including the left and right fringes of
1180 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1181 coordinates of the upper-left corner of the box. Return in
1182 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1183
1184 INLINE void
1185 window_box (w, area, box_x, box_y, box_width, box_height)
1186 struct window *w;
1187 int area;
1188 int *box_x, *box_y, *box_width, *box_height;
1189 {
1190 if (box_width)
1191 *box_width = window_box_width (w, area);
1192 if (box_height)
1193 *box_height = window_box_height (w);
1194 if (box_x)
1195 *box_x = window_box_left (w, area);
1196 if (box_y)
1197 {
1198 *box_y = WINDOW_TOP_EDGE_Y (w);
1199 if (WINDOW_WANTS_HEADER_LINE_P (w))
1200 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1201 }
1202 }
1203
1204
1205 /* Get the bounding box of the display area AREA of window W, without
1206 mode lines. AREA < 0 means the whole window, not including the
1207 left and right fringe of the window. Return in *TOP_LEFT_X
1208 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1209 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1210 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1211 box. */
1212
1213 INLINE void
1214 window_box_edges (w, area, top_left_x, top_left_y,
1215 bottom_right_x, bottom_right_y)
1216 struct window *w;
1217 int area;
1218 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1219 {
1220 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1221 bottom_right_y);
1222 *bottom_right_x += *top_left_x;
1223 *bottom_right_y += *top_left_y;
1224 }
1225
1226
1227 \f
1228 /***********************************************************************
1229 Utilities
1230 ***********************************************************************/
1231
1232 /* Return the bottom y-position of the line the iterator IT is in.
1233 This can modify IT's settings. */
1234
1235 int
1236 line_bottom_y (it)
1237 struct it *it;
1238 {
1239 int line_height = it->max_ascent + it->max_descent;
1240 int line_top_y = it->current_y;
1241
1242 if (line_height == 0)
1243 {
1244 if (last_height)
1245 line_height = last_height;
1246 else if (IT_CHARPOS (*it) < ZV)
1247 {
1248 move_it_by_lines (it, 1, 1);
1249 line_height = (it->max_ascent || it->max_descent
1250 ? it->max_ascent + it->max_descent
1251 : last_height);
1252 }
1253 else
1254 {
1255 struct glyph_row *row = it->glyph_row;
1256
1257 /* Use the default character height. */
1258 it->glyph_row = NULL;
1259 it->what = IT_CHARACTER;
1260 it->c = ' ';
1261 it->len = 1;
1262 PRODUCE_GLYPHS (it);
1263 line_height = it->ascent + it->descent;
1264 it->glyph_row = row;
1265 }
1266 }
1267
1268 return line_top_y + line_height;
1269 }
1270
1271
1272 /* Return 1 if position CHARPOS is visible in window W.
1273 CHARPOS < 0 means return info about WINDOW_END position.
1274 If visible, set *X and *Y to pixel coordinates of top left corner.
1275 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1276 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1277
1278 int
1279 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1280 struct window *w;
1281 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1282 {
1283 struct it it;
1284 struct text_pos top;
1285 int visible_p = 0;
1286 struct buffer *old_buffer = NULL;
1287
1288 if (noninteractive)
1289 return visible_p;
1290
1291 if (XBUFFER (w->buffer) != current_buffer)
1292 {
1293 old_buffer = current_buffer;
1294 set_buffer_internal_1 (XBUFFER (w->buffer));
1295 }
1296
1297 SET_TEXT_POS_FROM_MARKER (top, w->start);
1298
1299 /* Compute exact mode line heights. */
1300 if (WINDOW_WANTS_MODELINE_P (w))
1301 current_mode_line_height
1302 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1303 current_buffer->mode_line_format);
1304
1305 if (WINDOW_WANTS_HEADER_LINE_P (w))
1306 current_header_line_height
1307 = display_mode_line (w, HEADER_LINE_FACE_ID,
1308 current_buffer->header_line_format);
1309
1310 start_display (&it, w, top);
1311 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1312 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1313
1314 /* Note that we may overshoot because of invisible text. */
1315 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1316 {
1317 int top_x = it.current_x;
1318 int top_y = it.current_y;
1319 int bottom_y = (last_height = 0, line_bottom_y (&it));
1320 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1321
1322 if (top_y < window_top_y)
1323 visible_p = bottom_y > window_top_y;
1324 else if (top_y < it.last_visible_y)
1325 visible_p = 1;
1326 if (visible_p)
1327 {
1328 if (it.method == GET_FROM_BUFFER)
1329 {
1330 Lisp_Object window, prop;
1331
1332 XSETWINDOW (window, w);
1333 prop = Fget_char_property (make_number (it.position.charpos),
1334 Qinvisible, window);
1335
1336 /* If charpos coincides with invisible text covered with an
1337 ellipsis, use the first glyph of the ellipsis to compute
1338 the pixel positions. */
1339 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1340 {
1341 struct glyph_row *row = it.glyph_row;
1342 struct glyph *glyph = row->glyphs[TEXT_AREA];
1343 struct glyph *end = glyph + row->used[TEXT_AREA];
1344 int x = row->x;
1345
1346 for (; glyph < end && glyph->charpos < charpos; glyph++)
1347 x += glyph->pixel_width;
1348
1349 top_x = x;
1350 }
1351 }
1352
1353 *x = top_x;
1354 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1355 *rtop = max (0, window_top_y - top_y);
1356 *rbot = max (0, bottom_y - it.last_visible_y);
1357 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1358 - max (top_y, window_top_y)));
1359 *vpos = it.vpos;
1360 }
1361 }
1362 else
1363 {
1364 struct it it2;
1365
1366 it2 = it;
1367 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1368 move_it_by_lines (&it, 1, 0);
1369 if (charpos < IT_CHARPOS (it)
1370 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1371 {
1372 visible_p = 1;
1373 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1374 *x = it2.current_x;
1375 *y = it2.current_y + it2.max_ascent - it2.ascent;
1376 *rtop = max (0, -it2.current_y);
1377 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1378 - it.last_visible_y));
1379 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1380 it.last_visible_y)
1381 - max (it2.current_y,
1382 WINDOW_HEADER_LINE_HEIGHT (w))));
1383 *vpos = it2.vpos;
1384 }
1385 }
1386
1387 if (old_buffer)
1388 set_buffer_internal_1 (old_buffer);
1389
1390 current_header_line_height = current_mode_line_height = -1;
1391
1392 if (visible_p && XFASTINT (w->hscroll) > 0)
1393 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1394
1395 #if 0
1396 /* Debugging code. */
1397 if (visible_p)
1398 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1399 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1400 else
1401 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1402 #endif
1403
1404 return visible_p;
1405 }
1406
1407
1408 /* Return the next character from STR which is MAXLEN bytes long.
1409 Return in *LEN the length of the character. This is like
1410 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1411 we find one, we return a `?', but with the length of the invalid
1412 character. */
1413
1414 static INLINE int
1415 string_char_and_length (str, maxlen, len)
1416 const unsigned char *str;
1417 int maxlen, *len;
1418 {
1419 int c;
1420
1421 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1422 if (!CHAR_VALID_P (c, 1))
1423 /* We may not change the length here because other places in Emacs
1424 don't use this function, i.e. they silently accept invalid
1425 characters. */
1426 c = '?';
1427
1428 return c;
1429 }
1430
1431
1432
1433 /* Given a position POS containing a valid character and byte position
1434 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1435
1436 static struct text_pos
1437 string_pos_nchars_ahead (pos, string, nchars)
1438 struct text_pos pos;
1439 Lisp_Object string;
1440 int nchars;
1441 {
1442 xassert (STRINGP (string) && nchars >= 0);
1443
1444 if (STRING_MULTIBYTE (string))
1445 {
1446 int rest = SBYTES (string) - BYTEPOS (pos);
1447 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1448 int len;
1449
1450 while (nchars--)
1451 {
1452 string_char_and_length (p, rest, &len);
1453 p += len, rest -= len;
1454 xassert (rest >= 0);
1455 CHARPOS (pos) += 1;
1456 BYTEPOS (pos) += len;
1457 }
1458 }
1459 else
1460 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1461
1462 return pos;
1463 }
1464
1465
1466 /* Value is the text position, i.e. character and byte position,
1467 for character position CHARPOS in STRING. */
1468
1469 static INLINE struct text_pos
1470 string_pos (charpos, string)
1471 int charpos;
1472 Lisp_Object string;
1473 {
1474 struct text_pos pos;
1475 xassert (STRINGP (string));
1476 xassert (charpos >= 0);
1477 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1478 return pos;
1479 }
1480
1481
1482 /* Value is a text position, i.e. character and byte position, for
1483 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1484 means recognize multibyte characters. */
1485
1486 static struct text_pos
1487 c_string_pos (charpos, s, multibyte_p)
1488 int charpos;
1489 unsigned char *s;
1490 int multibyte_p;
1491 {
1492 struct text_pos pos;
1493
1494 xassert (s != NULL);
1495 xassert (charpos >= 0);
1496
1497 if (multibyte_p)
1498 {
1499 int rest = strlen (s), len;
1500
1501 SET_TEXT_POS (pos, 0, 0);
1502 while (charpos--)
1503 {
1504 string_char_and_length (s, rest, &len);
1505 s += len, rest -= len;
1506 xassert (rest >= 0);
1507 CHARPOS (pos) += 1;
1508 BYTEPOS (pos) += len;
1509 }
1510 }
1511 else
1512 SET_TEXT_POS (pos, charpos, charpos);
1513
1514 return pos;
1515 }
1516
1517
1518 /* Value is the number of characters in C string S. MULTIBYTE_P
1519 non-zero means recognize multibyte characters. */
1520
1521 static int
1522 number_of_chars (s, multibyte_p)
1523 unsigned char *s;
1524 int multibyte_p;
1525 {
1526 int nchars;
1527
1528 if (multibyte_p)
1529 {
1530 int rest = strlen (s), len;
1531 unsigned char *p = (unsigned char *) s;
1532
1533 for (nchars = 0; rest > 0; ++nchars)
1534 {
1535 string_char_and_length (p, rest, &len);
1536 rest -= len, p += len;
1537 }
1538 }
1539 else
1540 nchars = strlen (s);
1541
1542 return nchars;
1543 }
1544
1545
1546 /* Compute byte position NEWPOS->bytepos corresponding to
1547 NEWPOS->charpos. POS is a known position in string STRING.
1548 NEWPOS->charpos must be >= POS.charpos. */
1549
1550 static void
1551 compute_string_pos (newpos, pos, string)
1552 struct text_pos *newpos, pos;
1553 Lisp_Object string;
1554 {
1555 xassert (STRINGP (string));
1556 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1557
1558 if (STRING_MULTIBYTE (string))
1559 *newpos = string_pos_nchars_ahead (pos, string,
1560 CHARPOS (*newpos) - CHARPOS (pos));
1561 else
1562 BYTEPOS (*newpos) = CHARPOS (*newpos);
1563 }
1564
1565 /* EXPORT:
1566 Return an estimation of the pixel height of mode or top lines on
1567 frame F. FACE_ID specifies what line's height to estimate. */
1568
1569 int
1570 estimate_mode_line_height (f, face_id)
1571 struct frame *f;
1572 enum face_id face_id;
1573 {
1574 #ifdef HAVE_WINDOW_SYSTEM
1575 if (FRAME_WINDOW_P (f))
1576 {
1577 int height = FONT_HEIGHT (FRAME_FONT (f));
1578
1579 /* This function is called so early when Emacs starts that the face
1580 cache and mode line face are not yet initialized. */
1581 if (FRAME_FACE_CACHE (f))
1582 {
1583 struct face *face = FACE_FROM_ID (f, face_id);
1584 if (face)
1585 {
1586 if (face->font)
1587 height = FONT_HEIGHT (face->font);
1588 if (face->box_line_width > 0)
1589 height += 2 * face->box_line_width;
1590 }
1591 }
1592
1593 return height;
1594 }
1595 #endif
1596
1597 return 1;
1598 }
1599
1600 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1601 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1602 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1603 not force the value into range. */
1604
1605 void
1606 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1607 FRAME_PTR f;
1608 register int pix_x, pix_y;
1609 int *x, *y;
1610 NativeRectangle *bounds;
1611 int noclip;
1612 {
1613
1614 #ifdef HAVE_WINDOW_SYSTEM
1615 if (FRAME_WINDOW_P (f))
1616 {
1617 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1618 even for negative values. */
1619 if (pix_x < 0)
1620 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1621 if (pix_y < 0)
1622 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1623
1624 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1625 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1626
1627 if (bounds)
1628 STORE_NATIVE_RECT (*bounds,
1629 FRAME_COL_TO_PIXEL_X (f, pix_x),
1630 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1631 FRAME_COLUMN_WIDTH (f) - 1,
1632 FRAME_LINE_HEIGHT (f) - 1);
1633
1634 if (!noclip)
1635 {
1636 if (pix_x < 0)
1637 pix_x = 0;
1638 else if (pix_x > FRAME_TOTAL_COLS (f))
1639 pix_x = FRAME_TOTAL_COLS (f);
1640
1641 if (pix_y < 0)
1642 pix_y = 0;
1643 else if (pix_y > FRAME_LINES (f))
1644 pix_y = FRAME_LINES (f);
1645 }
1646 }
1647 #endif
1648
1649 *x = pix_x;
1650 *y = pix_y;
1651 }
1652
1653
1654 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1655 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1656 can't tell the positions because W's display is not up to date,
1657 return 0. */
1658
1659 int
1660 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1661 struct window *w;
1662 int hpos, vpos;
1663 int *frame_x, *frame_y;
1664 {
1665 #ifdef HAVE_WINDOW_SYSTEM
1666 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1667 {
1668 int success_p;
1669
1670 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1671 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1672
1673 if (display_completed)
1674 {
1675 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1676 struct glyph *glyph = row->glyphs[TEXT_AREA];
1677 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1678
1679 hpos = row->x;
1680 vpos = row->y;
1681 while (glyph < end)
1682 {
1683 hpos += glyph->pixel_width;
1684 ++glyph;
1685 }
1686
1687 /* If first glyph is partially visible, its first visible position is still 0. */
1688 if (hpos < 0)
1689 hpos = 0;
1690
1691 success_p = 1;
1692 }
1693 else
1694 {
1695 hpos = vpos = 0;
1696 success_p = 0;
1697 }
1698
1699 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1700 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1701 return success_p;
1702 }
1703 #endif
1704
1705 *frame_x = hpos;
1706 *frame_y = vpos;
1707 return 1;
1708 }
1709
1710
1711 #ifdef HAVE_WINDOW_SYSTEM
1712
1713 /* Find the glyph under window-relative coordinates X/Y in window W.
1714 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1715 strings. Return in *HPOS and *VPOS the row and column number of
1716 the glyph found. Return in *AREA the glyph area containing X.
1717 Value is a pointer to the glyph found or null if X/Y is not on
1718 text, or we can't tell because W's current matrix is not up to
1719 date. */
1720
1721 #ifndef HAVE_CARBON
1722 static
1723 #endif
1724 struct glyph *
1725 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1726 struct window *w;
1727 int x, y;
1728 int *hpos, *vpos, *dx, *dy, *area;
1729 {
1730 struct glyph *glyph, *end;
1731 struct glyph_row *row = NULL;
1732 int x0, i;
1733
1734 /* Find row containing Y. Give up if some row is not enabled. */
1735 for (i = 0; i < w->current_matrix->nrows; ++i)
1736 {
1737 row = MATRIX_ROW (w->current_matrix, i);
1738 if (!row->enabled_p)
1739 return NULL;
1740 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1741 break;
1742 }
1743
1744 *vpos = i;
1745 *hpos = 0;
1746
1747 /* Give up if Y is not in the window. */
1748 if (i == w->current_matrix->nrows)
1749 return NULL;
1750
1751 /* Get the glyph area containing X. */
1752 if (w->pseudo_window_p)
1753 {
1754 *area = TEXT_AREA;
1755 x0 = 0;
1756 }
1757 else
1758 {
1759 if (x < window_box_left_offset (w, TEXT_AREA))
1760 {
1761 *area = LEFT_MARGIN_AREA;
1762 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1763 }
1764 else if (x < window_box_right_offset (w, TEXT_AREA))
1765 {
1766 *area = TEXT_AREA;
1767 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1768 }
1769 else
1770 {
1771 *area = RIGHT_MARGIN_AREA;
1772 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1773 }
1774 }
1775
1776 /* Find glyph containing X. */
1777 glyph = row->glyphs[*area];
1778 end = glyph + row->used[*area];
1779 x -= x0;
1780 while (glyph < end && x >= glyph->pixel_width)
1781 {
1782 x -= glyph->pixel_width;
1783 ++glyph;
1784 }
1785
1786 if (glyph == end)
1787 return NULL;
1788
1789 if (dx)
1790 {
1791 *dx = x;
1792 *dy = y - (row->y + row->ascent - glyph->ascent);
1793 }
1794
1795 *hpos = glyph - row->glyphs[*area];
1796 return glyph;
1797 }
1798
1799
1800 /* EXPORT:
1801 Convert frame-relative x/y to coordinates relative to window W.
1802 Takes pseudo-windows into account. */
1803
1804 void
1805 frame_to_window_pixel_xy (w, x, y)
1806 struct window *w;
1807 int *x, *y;
1808 {
1809 if (w->pseudo_window_p)
1810 {
1811 /* A pseudo-window is always full-width, and starts at the
1812 left edge of the frame, plus a frame border. */
1813 struct frame *f = XFRAME (w->frame);
1814 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1815 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1816 }
1817 else
1818 {
1819 *x -= WINDOW_LEFT_EDGE_X (w);
1820 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1821 }
1822 }
1823
1824 /* EXPORT:
1825 Return in RECTS[] at most N clipping rectangles for glyph string S.
1826 Return the number of stored rectangles. */
1827
1828 int
1829 get_glyph_string_clip_rects (s, rects, n)
1830 struct glyph_string *s;
1831 NativeRectangle *rects;
1832 int n;
1833 {
1834 XRectangle r;
1835
1836 if (n <= 0)
1837 return 0;
1838
1839 if (s->row->full_width_p)
1840 {
1841 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1842 r.x = WINDOW_LEFT_EDGE_X (s->w);
1843 r.width = WINDOW_TOTAL_WIDTH (s->w);
1844
1845 /* Unless displaying a mode or menu bar line, which are always
1846 fully visible, clip to the visible part of the row. */
1847 if (s->w->pseudo_window_p)
1848 r.height = s->row->visible_height;
1849 else
1850 r.height = s->height;
1851 }
1852 else
1853 {
1854 /* This is a text line that may be partially visible. */
1855 r.x = window_box_left (s->w, s->area);
1856 r.width = window_box_width (s->w, s->area);
1857 r.height = s->row->visible_height;
1858 }
1859
1860 if (s->clip_head)
1861 if (r.x < s->clip_head->x)
1862 {
1863 if (r.width >= s->clip_head->x - r.x)
1864 r.width -= s->clip_head->x - r.x;
1865 else
1866 r.width = 0;
1867 r.x = s->clip_head->x;
1868 }
1869 if (s->clip_tail)
1870 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1871 {
1872 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1873 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1874 else
1875 r.width = 0;
1876 }
1877
1878 /* If S draws overlapping rows, it's sufficient to use the top and
1879 bottom of the window for clipping because this glyph string
1880 intentionally draws over other lines. */
1881 if (s->for_overlaps)
1882 {
1883 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1884 r.height = window_text_bottom_y (s->w) - r.y;
1885
1886 /* Alas, the above simple strategy does not work for the
1887 environments with anti-aliased text: if the same text is
1888 drawn onto the same place multiple times, it gets thicker.
1889 If the overlap we are processing is for the erased cursor, we
1890 take the intersection with the rectagle of the cursor. */
1891 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1892 {
1893 XRectangle rc, r_save = r;
1894
1895 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1896 rc.y = s->w->phys_cursor.y;
1897 rc.width = s->w->phys_cursor_width;
1898 rc.height = s->w->phys_cursor_height;
1899
1900 x_intersect_rectangles (&r_save, &rc, &r);
1901 }
1902 }
1903 else
1904 {
1905 /* Don't use S->y for clipping because it doesn't take partially
1906 visible lines into account. For example, it can be negative for
1907 partially visible lines at the top of a window. */
1908 if (!s->row->full_width_p
1909 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1910 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1911 else
1912 r.y = max (0, s->row->y);
1913
1914 /* If drawing a tool-bar window, draw it over the internal border
1915 at the top of the window. */
1916 if (WINDOWP (s->f->tool_bar_window)
1917 && s->w == XWINDOW (s->f->tool_bar_window))
1918 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1919 }
1920
1921 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1922
1923 /* If drawing the cursor, don't let glyph draw outside its
1924 advertised boundaries. Cleartype does this under some circumstances. */
1925 if (s->hl == DRAW_CURSOR)
1926 {
1927 struct glyph *glyph = s->first_glyph;
1928 int height, max_y;
1929
1930 if (s->x > r.x)
1931 {
1932 r.width -= s->x - r.x;
1933 r.x = s->x;
1934 }
1935 r.width = min (r.width, glyph->pixel_width);
1936
1937 /* If r.y is below window bottom, ensure that we still see a cursor. */
1938 height = min (glyph->ascent + glyph->descent,
1939 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1940 max_y = window_text_bottom_y (s->w) - height;
1941 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1942 if (s->ybase - glyph->ascent > max_y)
1943 {
1944 r.y = max_y;
1945 r.height = height;
1946 }
1947 else
1948 {
1949 /* Don't draw cursor glyph taller than our actual glyph. */
1950 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1951 if (height < r.height)
1952 {
1953 max_y = r.y + r.height;
1954 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1955 r.height = min (max_y - r.y, height);
1956 }
1957 }
1958 }
1959
1960 if (s->row->clip)
1961 {
1962 XRectangle r_save = r;
1963
1964 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1965 r.width = 0;
1966 }
1967
1968 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1969 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1970 {
1971 #ifdef CONVERT_FROM_XRECT
1972 CONVERT_FROM_XRECT (r, *rects);
1973 #else
1974 *rects = r;
1975 #endif
1976 return 1;
1977 }
1978 else
1979 {
1980 /* If we are processing overlapping and allowed to return
1981 multiple clipping rectangles, we exclude the row of the glyph
1982 string from the clipping rectangle. This is to avoid drawing
1983 the same text on the environment with anti-aliasing. */
1984 #ifdef CONVERT_FROM_XRECT
1985 XRectangle rs[2];
1986 #else
1987 XRectangle *rs = rects;
1988 #endif
1989 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1990
1991 if (s->for_overlaps & OVERLAPS_PRED)
1992 {
1993 rs[i] = r;
1994 if (r.y + r.height > row_y)
1995 {
1996 if (r.y < row_y)
1997 rs[i].height = row_y - r.y;
1998 else
1999 rs[i].height = 0;
2000 }
2001 i++;
2002 }
2003 if (s->for_overlaps & OVERLAPS_SUCC)
2004 {
2005 rs[i] = r;
2006 if (r.y < row_y + s->row->visible_height)
2007 {
2008 if (r.y + r.height > row_y + s->row->visible_height)
2009 {
2010 rs[i].y = row_y + s->row->visible_height;
2011 rs[i].height = r.y + r.height - rs[i].y;
2012 }
2013 else
2014 rs[i].height = 0;
2015 }
2016 i++;
2017 }
2018
2019 n = i;
2020 #ifdef CONVERT_FROM_XRECT
2021 for (i = 0; i < n; i++)
2022 CONVERT_FROM_XRECT (rs[i], rects[i]);
2023 #endif
2024 return n;
2025 }
2026 }
2027
2028 /* EXPORT:
2029 Return in *NR the clipping rectangle for glyph string S. */
2030
2031 void
2032 get_glyph_string_clip_rect (s, nr)
2033 struct glyph_string *s;
2034 NativeRectangle *nr;
2035 {
2036 get_glyph_string_clip_rects (s, nr, 1);
2037 }
2038
2039
2040 /* EXPORT:
2041 Return the position and height of the phys cursor in window W.
2042 Set w->phys_cursor_width to width of phys cursor.
2043 */
2044
2045 void
2046 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2047 struct window *w;
2048 struct glyph_row *row;
2049 struct glyph *glyph;
2050 int *xp, *yp, *heightp;
2051 {
2052 struct frame *f = XFRAME (WINDOW_FRAME (w));
2053 int x, y, wd, h, h0, y0;
2054
2055 /* Compute the width of the rectangle to draw. If on a stretch
2056 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2057 rectangle as wide as the glyph, but use a canonical character
2058 width instead. */
2059 wd = glyph->pixel_width - 1;
2060 #ifdef HAVE_NTGUI
2061 wd++; /* Why? */
2062 #endif
2063
2064 x = w->phys_cursor.x;
2065 if (x < 0)
2066 {
2067 wd += x;
2068 x = 0;
2069 }
2070
2071 if (glyph->type == STRETCH_GLYPH
2072 && !x_stretch_cursor_p)
2073 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2074 w->phys_cursor_width = wd;
2075
2076 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2077
2078 /* If y is below window bottom, ensure that we still see a cursor. */
2079 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2080
2081 h = max (h0, glyph->ascent + glyph->descent);
2082 h0 = min (h0, glyph->ascent + glyph->descent);
2083
2084 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2085 if (y < y0)
2086 {
2087 h = max (h - (y0 - y) + 1, h0);
2088 y = y0 - 1;
2089 }
2090 else
2091 {
2092 y0 = window_text_bottom_y (w) - h0;
2093 if (y > y0)
2094 {
2095 h += y - y0;
2096 y = y0;
2097 }
2098 }
2099
2100 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2101 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2102 *heightp = h;
2103 }
2104
2105 /*
2106 * Remember which glyph the mouse is over.
2107 */
2108
2109 void
2110 remember_mouse_glyph (f, gx, gy, rect)
2111 struct frame *f;
2112 int gx, gy;
2113 NativeRectangle *rect;
2114 {
2115 Lisp_Object window;
2116 struct window *w;
2117 struct glyph_row *r, *gr, *end_row;
2118 enum window_part part;
2119 enum glyph_row_area area;
2120 int x, y, width, height;
2121
2122 /* Try to determine frame pixel position and size of the glyph under
2123 frame pixel coordinates X/Y on frame F. */
2124
2125 if (!f->glyphs_initialized_p
2126 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2127 NILP (window)))
2128 {
2129 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2130 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2131 goto virtual_glyph;
2132 }
2133
2134 w = XWINDOW (window);
2135 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2136 height = WINDOW_FRAME_LINE_HEIGHT (w);
2137
2138 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2139 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2140
2141 if (w->pseudo_window_p)
2142 {
2143 area = TEXT_AREA;
2144 part = ON_MODE_LINE; /* Don't adjust margin. */
2145 goto text_glyph;
2146 }
2147
2148 switch (part)
2149 {
2150 case ON_LEFT_MARGIN:
2151 area = LEFT_MARGIN_AREA;
2152 goto text_glyph;
2153
2154 case ON_RIGHT_MARGIN:
2155 area = RIGHT_MARGIN_AREA;
2156 goto text_glyph;
2157
2158 case ON_HEADER_LINE:
2159 case ON_MODE_LINE:
2160 gr = (part == ON_HEADER_LINE
2161 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2162 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2163 gy = gr->y;
2164 area = TEXT_AREA;
2165 goto text_glyph_row_found;
2166
2167 case ON_TEXT:
2168 area = TEXT_AREA;
2169
2170 text_glyph:
2171 gr = 0; gy = 0;
2172 for (; r <= end_row && r->enabled_p; ++r)
2173 if (r->y + r->height > y)
2174 {
2175 gr = r; gy = r->y;
2176 break;
2177 }
2178
2179 text_glyph_row_found:
2180 if (gr && gy <= y)
2181 {
2182 struct glyph *g = gr->glyphs[area];
2183 struct glyph *end = g + gr->used[area];
2184
2185 height = gr->height;
2186 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2187 if (gx + g->pixel_width > x)
2188 break;
2189
2190 if (g < end)
2191 {
2192 if (g->type == IMAGE_GLYPH)
2193 {
2194 /* Don't remember when mouse is over image, as
2195 image may have hot-spots. */
2196 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2197 return;
2198 }
2199 width = g->pixel_width;
2200 }
2201 else
2202 {
2203 /* Use nominal char spacing at end of line. */
2204 x -= gx;
2205 gx += (x / width) * width;
2206 }
2207
2208 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2209 gx += window_box_left_offset (w, area);
2210 }
2211 else
2212 {
2213 /* Use nominal line height at end of window. */
2214 gx = (x / width) * width;
2215 y -= gy;
2216 gy += (y / height) * height;
2217 }
2218 break;
2219
2220 case ON_LEFT_FRINGE:
2221 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2222 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2223 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2224 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2225 goto row_glyph;
2226
2227 case ON_RIGHT_FRINGE:
2228 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2229 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2230 : window_box_right_offset (w, TEXT_AREA));
2231 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2232 goto row_glyph;
2233
2234 case ON_SCROLL_BAR:
2235 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2236 ? 0
2237 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2238 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2239 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2240 : 0)));
2241 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2242
2243 row_glyph:
2244 gr = 0, gy = 0;
2245 for (; r <= end_row && r->enabled_p; ++r)
2246 if (r->y + r->height > y)
2247 {
2248 gr = r; gy = r->y;
2249 break;
2250 }
2251
2252 if (gr && gy <= y)
2253 height = gr->height;
2254 else
2255 {
2256 /* Use nominal line height at end of window. */
2257 y -= gy;
2258 gy += (y / height) * height;
2259 }
2260 break;
2261
2262 default:
2263 ;
2264 virtual_glyph:
2265 /* If there is no glyph under the mouse, then we divide the screen
2266 into a grid of the smallest glyph in the frame, and use that
2267 as our "glyph". */
2268
2269 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2270 round down even for negative values. */
2271 if (gx < 0)
2272 gx -= width - 1;
2273 if (gy < 0)
2274 gy -= height - 1;
2275
2276 gx = (gx / width) * width;
2277 gy = (gy / height) * height;
2278
2279 goto store_rect;
2280 }
2281
2282 gx += WINDOW_LEFT_EDGE_X (w);
2283 gy += WINDOW_TOP_EDGE_Y (w);
2284
2285 store_rect:
2286 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2287
2288 /* Visible feedback for debugging. */
2289 #if 0
2290 #if HAVE_X_WINDOWS
2291 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2292 f->output_data.x->normal_gc,
2293 gx, gy, width, height);
2294 #endif
2295 #endif
2296 }
2297
2298
2299 #endif /* HAVE_WINDOW_SYSTEM */
2300
2301 \f
2302 /***********************************************************************
2303 Lisp form evaluation
2304 ***********************************************************************/
2305
2306 /* Error handler for safe_eval and safe_call. */
2307
2308 static Lisp_Object
2309 safe_eval_handler (arg)
2310 Lisp_Object arg;
2311 {
2312 add_to_log ("Error during redisplay: %s", arg, Qnil);
2313 return Qnil;
2314 }
2315
2316
2317 /* Evaluate SEXPR and return the result, or nil if something went
2318 wrong. Prevent redisplay during the evaluation. */
2319
2320 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2321 Return the result, or nil if something went wrong. Prevent
2322 redisplay during the evaluation. */
2323
2324 Lisp_Object
2325 safe_call (nargs, args)
2326 int nargs;
2327 Lisp_Object *args;
2328 {
2329 Lisp_Object val;
2330
2331 if (inhibit_eval_during_redisplay)
2332 val = Qnil;
2333 else
2334 {
2335 int count = SPECPDL_INDEX ();
2336 struct gcpro gcpro1;
2337
2338 GCPRO1 (args[0]);
2339 gcpro1.nvars = nargs;
2340 specbind (Qinhibit_redisplay, Qt);
2341 /* Use Qt to ensure debugger does not run,
2342 so there is no possibility of wanting to redisplay. */
2343 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2344 safe_eval_handler);
2345 UNGCPRO;
2346 val = unbind_to (count, val);
2347 }
2348
2349 return val;
2350 }
2351
2352
2353 /* Call function FN with one argument ARG.
2354 Return the result, or nil if something went wrong. */
2355
2356 Lisp_Object
2357 safe_call1 (fn, arg)
2358 Lisp_Object fn, arg;
2359 {
2360 Lisp_Object args[2];
2361 args[0] = fn;
2362 args[1] = arg;
2363 return safe_call (2, args);
2364 }
2365
2366 static Lisp_Object Qeval;
2367
2368 Lisp_Object
2369 safe_eval (Lisp_Object sexpr)
2370 {
2371 return safe_call1 (Qeval, sexpr);
2372 }
2373
2374 /* Call function FN with one argument ARG.
2375 Return the result, or nil if something went wrong. */
2376
2377 Lisp_Object
2378 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2379 {
2380 Lisp_Object args[3];
2381 args[0] = fn;
2382 args[1] = arg1;
2383 args[2] = arg2;
2384 return safe_call (3, args);
2385 }
2386
2387
2388 \f
2389 /***********************************************************************
2390 Debugging
2391 ***********************************************************************/
2392
2393 #if 0
2394
2395 /* Define CHECK_IT to perform sanity checks on iterators.
2396 This is for debugging. It is too slow to do unconditionally. */
2397
2398 static void
2399 check_it (it)
2400 struct it *it;
2401 {
2402 if (it->method == GET_FROM_STRING)
2403 {
2404 xassert (STRINGP (it->string));
2405 xassert (IT_STRING_CHARPOS (*it) >= 0);
2406 }
2407 else
2408 {
2409 xassert (IT_STRING_CHARPOS (*it) < 0);
2410 if (it->method == GET_FROM_BUFFER)
2411 {
2412 /* Check that character and byte positions agree. */
2413 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2414 }
2415 }
2416
2417 if (it->dpvec)
2418 xassert (it->current.dpvec_index >= 0);
2419 else
2420 xassert (it->current.dpvec_index < 0);
2421 }
2422
2423 #define CHECK_IT(IT) check_it ((IT))
2424
2425 #else /* not 0 */
2426
2427 #define CHECK_IT(IT) (void) 0
2428
2429 #endif /* not 0 */
2430
2431
2432 #if GLYPH_DEBUG
2433
2434 /* Check that the window end of window W is what we expect it
2435 to be---the last row in the current matrix displaying text. */
2436
2437 static void
2438 check_window_end (w)
2439 struct window *w;
2440 {
2441 if (!MINI_WINDOW_P (w)
2442 && !NILP (w->window_end_valid))
2443 {
2444 struct glyph_row *row;
2445 xassert ((row = MATRIX_ROW (w->current_matrix,
2446 XFASTINT (w->window_end_vpos)),
2447 !row->enabled_p
2448 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2449 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2450 }
2451 }
2452
2453 #define CHECK_WINDOW_END(W) check_window_end ((W))
2454
2455 #else /* not GLYPH_DEBUG */
2456
2457 #define CHECK_WINDOW_END(W) (void) 0
2458
2459 #endif /* not GLYPH_DEBUG */
2460
2461
2462 \f
2463 /***********************************************************************
2464 Iterator initialization
2465 ***********************************************************************/
2466
2467 /* Initialize IT for displaying current_buffer in window W, starting
2468 at character position CHARPOS. CHARPOS < 0 means that no buffer
2469 position is specified which is useful when the iterator is assigned
2470 a position later. BYTEPOS is the byte position corresponding to
2471 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2472
2473 If ROW is not null, calls to produce_glyphs with IT as parameter
2474 will produce glyphs in that row.
2475
2476 BASE_FACE_ID is the id of a base face to use. It must be one of
2477 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2478 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2479 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2480
2481 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2482 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2483 will be initialized to use the corresponding mode line glyph row of
2484 the desired matrix of W. */
2485
2486 void
2487 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2488 struct it *it;
2489 struct window *w;
2490 int charpos, bytepos;
2491 struct glyph_row *row;
2492 enum face_id base_face_id;
2493 {
2494 int highlight_region_p;
2495 enum face_id remapped_base_face_id = base_face_id;
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 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2513 if (! NILP (Vface_remapping_alist))
2514 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2515
2516 /* Use one of the mode line rows of W's desired matrix if
2517 appropriate. */
2518 if (row == NULL)
2519 {
2520 if (base_face_id == MODE_LINE_FACE_ID
2521 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2522 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2523 else if (base_face_id == HEADER_LINE_FACE_ID)
2524 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2525 }
2526
2527 /* Clear IT. */
2528 bzero (it, sizeof *it);
2529 it->current.overlay_string_index = -1;
2530 it->current.dpvec_index = -1;
2531 it->base_face_id = remapped_base_face_id;
2532 it->string = Qnil;
2533 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2534
2535 /* The window in which we iterate over current_buffer: */
2536 XSETWINDOW (it->window, w);
2537 it->w = w;
2538 it->f = XFRAME (w->frame);
2539
2540 /* Extra space between lines (on window systems only). */
2541 if (base_face_id == DEFAULT_FACE_ID
2542 && FRAME_WINDOW_P (it->f))
2543 {
2544 if (NATNUMP (current_buffer->extra_line_spacing))
2545 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2546 else if (FLOATP (current_buffer->extra_line_spacing))
2547 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2548 * FRAME_LINE_HEIGHT (it->f));
2549 else if (it->f->extra_line_spacing > 0)
2550 it->extra_line_spacing = it->f->extra_line_spacing;
2551 it->max_extra_line_spacing = 0;
2552 }
2553
2554 /* If realized faces have been removed, e.g. because of face
2555 attribute changes of named faces, recompute them. When running
2556 in batch mode, the face cache of the initial frame is null. If
2557 we happen to get called, make a dummy face cache. */
2558 if (FRAME_FACE_CACHE (it->f) == NULL)
2559 init_frame_faces (it->f);
2560 if (FRAME_FACE_CACHE (it->f)->used == 0)
2561 recompute_basic_faces (it->f);
2562
2563 /* Current value of the `slice', `space-width', and 'height' properties. */
2564 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2565 it->space_width = Qnil;
2566 it->font_height = Qnil;
2567 it->override_ascent = -1;
2568
2569 /* Are control characters displayed as `^C'? */
2570 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2571
2572 /* -1 means everything between a CR and the following line end
2573 is invisible. >0 means lines indented more than this value are
2574 invisible. */
2575 it->selective = (INTEGERP (current_buffer->selective_display)
2576 ? XFASTINT (current_buffer->selective_display)
2577 : (!NILP (current_buffer->selective_display)
2578 ? -1 : 0));
2579 it->selective_display_ellipsis_p
2580 = !NILP (current_buffer->selective_display_ellipses);
2581
2582 /* Display table to use. */
2583 it->dp = window_display_table (w);
2584
2585 /* Are multibyte characters enabled in current_buffer? */
2586 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2587
2588 /* Non-zero if we should highlight the region. */
2589 highlight_region_p
2590 = (!NILP (Vtransient_mark_mode)
2591 && !NILP (current_buffer->mark_active)
2592 && XMARKER (current_buffer->mark)->buffer != 0);
2593
2594 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2595 start and end of a visible region in window IT->w. Set both to
2596 -1 to indicate no region. */
2597 if (highlight_region_p
2598 /* Maybe highlight only in selected window. */
2599 && (/* Either show region everywhere. */
2600 highlight_nonselected_windows
2601 /* Or show region in the selected window. */
2602 || w == XWINDOW (selected_window)
2603 /* Or show the region if we are in the mini-buffer and W is
2604 the window the mini-buffer refers to. */
2605 || (MINI_WINDOW_P (XWINDOW (selected_window))
2606 && WINDOWP (minibuf_selected_window)
2607 && w == XWINDOW (minibuf_selected_window))))
2608 {
2609 int charpos = marker_position (current_buffer->mark);
2610 it->region_beg_charpos = min (PT, charpos);
2611 it->region_end_charpos = max (PT, charpos);
2612 }
2613 else
2614 it->region_beg_charpos = it->region_end_charpos = -1;
2615
2616 /* Get the position at which the redisplay_end_trigger hook should
2617 be run, if it is to be run at all. */
2618 if (MARKERP (w->redisplay_end_trigger)
2619 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2620 it->redisplay_end_trigger_charpos
2621 = marker_position (w->redisplay_end_trigger);
2622 else if (INTEGERP (w->redisplay_end_trigger))
2623 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2624
2625 /* Correct bogus values of tab_width. */
2626 it->tab_width = XINT (current_buffer->tab_width);
2627 if (it->tab_width <= 0 || it->tab_width > 1000)
2628 it->tab_width = 8;
2629
2630 /* Are lines in the display truncated? */
2631 it->truncate_lines_p
2632 = (base_face_id != DEFAULT_FACE_ID
2633 || XINT (it->w->hscroll)
2634 || (truncate_partial_width_windows
2635 && !WINDOW_FULL_WIDTH_P (it->w))
2636 || !NILP (current_buffer->truncate_lines));
2637
2638 /* Get dimensions of truncation and continuation glyphs. These are
2639 displayed as fringe bitmaps under X, so we don't need them for such
2640 frames. */
2641 if (!FRAME_WINDOW_P (it->f))
2642 {
2643 if (it->truncate_lines_p)
2644 {
2645 /* We will need the truncation glyph. */
2646 xassert (it->glyph_row == NULL);
2647 produce_special_glyphs (it, IT_TRUNCATION);
2648 it->truncation_pixel_width = it->pixel_width;
2649 }
2650 else
2651 {
2652 /* We will need the continuation glyph. */
2653 xassert (it->glyph_row == NULL);
2654 produce_special_glyphs (it, IT_CONTINUATION);
2655 it->continuation_pixel_width = it->pixel_width;
2656 }
2657
2658 /* Reset these values to zero because the produce_special_glyphs
2659 above has changed them. */
2660 it->pixel_width = it->ascent = it->descent = 0;
2661 it->phys_ascent = it->phys_descent = 0;
2662 }
2663
2664 /* Set this after getting the dimensions of truncation and
2665 continuation glyphs, so that we don't produce glyphs when calling
2666 produce_special_glyphs, above. */
2667 it->glyph_row = row;
2668 it->area = TEXT_AREA;
2669
2670 /* Get the dimensions of the display area. The display area
2671 consists of the visible window area plus a horizontally scrolled
2672 part to the left of the window. All x-values are relative to the
2673 start of this total display area. */
2674 if (base_face_id != DEFAULT_FACE_ID)
2675 {
2676 /* Mode lines, menu bar in terminal frames. */
2677 it->first_visible_x = 0;
2678 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2679 }
2680 else
2681 {
2682 it->first_visible_x
2683 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2684 it->last_visible_x = (it->first_visible_x
2685 + window_box_width (w, TEXT_AREA));
2686
2687 /* If we truncate lines, leave room for the truncator glyph(s) at
2688 the right margin. Otherwise, leave room for the continuation
2689 glyph(s). Truncation and continuation glyphs are not inserted
2690 for window-based redisplay. */
2691 if (!FRAME_WINDOW_P (it->f))
2692 {
2693 if (it->truncate_lines_p)
2694 it->last_visible_x -= it->truncation_pixel_width;
2695 else
2696 it->last_visible_x -= it->continuation_pixel_width;
2697 }
2698
2699 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2700 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2701 }
2702
2703 /* Leave room for a border glyph. */
2704 if (!FRAME_WINDOW_P (it->f)
2705 && !WINDOW_RIGHTMOST_P (it->w))
2706 it->last_visible_x -= 1;
2707
2708 it->last_visible_y = window_text_bottom_y (w);
2709
2710 /* For mode lines and alike, arrange for the first glyph having a
2711 left box line if the face specifies a box. */
2712 if (base_face_id != DEFAULT_FACE_ID)
2713 {
2714 struct face *face;
2715
2716 it->face_id = remapped_base_face_id;
2717
2718 /* If we have a boxed mode line, make the first character appear
2719 with a left box line. */
2720 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2721 if (face->box != FACE_NO_BOX)
2722 it->start_of_box_run_p = 1;
2723 }
2724
2725 /* If a buffer position was specified, set the iterator there,
2726 getting overlays and face properties from that position. */
2727 if (charpos >= BUF_BEG (current_buffer))
2728 {
2729 it->end_charpos = ZV;
2730 it->face_id = -1;
2731 IT_CHARPOS (*it) = charpos;
2732
2733 /* Compute byte position if not specified. */
2734 if (bytepos < charpos)
2735 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2736 else
2737 IT_BYTEPOS (*it) = bytepos;
2738
2739 it->start = it->current;
2740
2741 /* Compute faces etc. */
2742 reseat (it, it->current.pos, 1);
2743 }
2744
2745 CHECK_IT (it);
2746 }
2747
2748
2749 /* Initialize IT for the display of window W with window start POS. */
2750
2751 void
2752 start_display (it, w, pos)
2753 struct it *it;
2754 struct window *w;
2755 struct text_pos pos;
2756 {
2757 struct glyph_row *row;
2758 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2759
2760 row = w->desired_matrix->rows + first_vpos;
2761 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2762 it->first_vpos = first_vpos;
2763
2764 /* Don't reseat to previous visible line start if current start
2765 position is in a string or image. */
2766 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2767 {
2768 int start_at_line_beg_p;
2769 int first_y = it->current_y;
2770
2771 /* If window start is not at a line start, skip forward to POS to
2772 get the correct continuation lines width. */
2773 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2774 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2775 if (!start_at_line_beg_p)
2776 {
2777 int new_x;
2778
2779 reseat_at_previous_visible_line_start (it);
2780 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2781
2782 new_x = it->current_x + it->pixel_width;
2783
2784 /* If lines are continued, this line may end in the middle
2785 of a multi-glyph character (e.g. a control character
2786 displayed as \003, or in the middle of an overlay
2787 string). In this case move_it_to above will not have
2788 taken us to the start of the continuation line but to the
2789 end of the continued line. */
2790 if (it->current_x > 0
2791 && !it->truncate_lines_p /* Lines are continued. */
2792 && (/* And glyph doesn't fit on the line. */
2793 new_x > it->last_visible_x
2794 /* Or it fits exactly and we're on a window
2795 system frame. */
2796 || (new_x == it->last_visible_x
2797 && FRAME_WINDOW_P (it->f))))
2798 {
2799 if (it->current.dpvec_index >= 0
2800 || it->current.overlay_string_index >= 0)
2801 {
2802 set_iterator_to_next (it, 1);
2803 move_it_in_display_line_to (it, -1, -1, 0);
2804 }
2805
2806 it->continuation_lines_width += it->current_x;
2807 }
2808
2809 /* We're starting a new display line, not affected by the
2810 height of the continued line, so clear the appropriate
2811 fields in the iterator structure. */
2812 it->max_ascent = it->max_descent = 0;
2813 it->max_phys_ascent = it->max_phys_descent = 0;
2814
2815 it->current_y = first_y;
2816 it->vpos = 0;
2817 it->current_x = it->hpos = 0;
2818 }
2819 }
2820
2821 #if 0 /* Don't assert the following because start_display is sometimes
2822 called intentionally with a window start that is not at a
2823 line start. Please leave this code in as a comment. */
2824
2825 /* Window start should be on a line start, now. */
2826 xassert (it->continuation_lines_width
2827 || IT_CHARPOS (it) == BEGV
2828 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2829 #endif /* 0 */
2830 }
2831
2832
2833 /* Return 1 if POS is a position in ellipses displayed for invisible
2834 text. W is the window we display, for text property lookup. */
2835
2836 static int
2837 in_ellipses_for_invisible_text_p (pos, w)
2838 struct display_pos *pos;
2839 struct window *w;
2840 {
2841 Lisp_Object prop, window;
2842 int ellipses_p = 0;
2843 int charpos = CHARPOS (pos->pos);
2844
2845 /* If POS specifies a position in a display vector, this might
2846 be for an ellipsis displayed for invisible text. We won't
2847 get the iterator set up for delivering that ellipsis unless
2848 we make sure that it gets aware of the invisible text. */
2849 if (pos->dpvec_index >= 0
2850 && pos->overlay_string_index < 0
2851 && CHARPOS (pos->string_pos) < 0
2852 && charpos > BEGV
2853 && (XSETWINDOW (window, w),
2854 prop = Fget_char_property (make_number (charpos),
2855 Qinvisible, window),
2856 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2857 {
2858 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2859 window);
2860 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2861 }
2862
2863 return ellipses_p;
2864 }
2865
2866
2867 /* Initialize IT for stepping through current_buffer in window W,
2868 starting at position POS that includes overlay string and display
2869 vector/ control character translation position information. Value
2870 is zero if there are overlay strings with newlines at POS. */
2871
2872 static int
2873 init_from_display_pos (it, w, pos)
2874 struct it *it;
2875 struct window *w;
2876 struct display_pos *pos;
2877 {
2878 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2879 int i, overlay_strings_with_newlines = 0;
2880
2881 /* If POS specifies a position in a display vector, this might
2882 be for an ellipsis displayed for invisible text. We won't
2883 get the iterator set up for delivering that ellipsis unless
2884 we make sure that it gets aware of the invisible text. */
2885 if (in_ellipses_for_invisible_text_p (pos, w))
2886 {
2887 --charpos;
2888 bytepos = 0;
2889 }
2890
2891 /* Keep in mind: the call to reseat in init_iterator skips invisible
2892 text, so we might end up at a position different from POS. This
2893 is only a problem when POS is a row start after a newline and an
2894 overlay starts there with an after-string, and the overlay has an
2895 invisible property. Since we don't skip invisible text in
2896 display_line and elsewhere immediately after consuming the
2897 newline before the row start, such a POS will not be in a string,
2898 but the call to init_iterator below will move us to the
2899 after-string. */
2900 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2901
2902 /* This only scans the current chunk -- it should scan all chunks.
2903 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2904 to 16 in 22.1 to make this a lesser problem. */
2905 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2906 {
2907 const char *s = SDATA (it->overlay_strings[i]);
2908 const char *e = s + SBYTES (it->overlay_strings[i]);
2909
2910 while (s < e && *s != '\n')
2911 ++s;
2912
2913 if (s < e)
2914 {
2915 overlay_strings_with_newlines = 1;
2916 break;
2917 }
2918 }
2919
2920 /* If position is within an overlay string, set up IT to the right
2921 overlay string. */
2922 if (pos->overlay_string_index >= 0)
2923 {
2924 int relative_index;
2925
2926 /* If the first overlay string happens to have a `display'
2927 property for an image, the iterator will be set up for that
2928 image, and we have to undo that setup first before we can
2929 correct the overlay string index. */
2930 if (it->method == GET_FROM_IMAGE)
2931 pop_it (it);
2932
2933 /* We already have the first chunk of overlay strings in
2934 IT->overlay_strings. Load more until the one for
2935 pos->overlay_string_index is in IT->overlay_strings. */
2936 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2937 {
2938 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2939 it->current.overlay_string_index = 0;
2940 while (n--)
2941 {
2942 load_overlay_strings (it, 0);
2943 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2944 }
2945 }
2946
2947 it->current.overlay_string_index = pos->overlay_string_index;
2948 relative_index = (it->current.overlay_string_index
2949 % OVERLAY_STRING_CHUNK_SIZE);
2950 it->string = it->overlay_strings[relative_index];
2951 xassert (STRINGP (it->string));
2952 it->current.string_pos = pos->string_pos;
2953 it->method = GET_FROM_STRING;
2954 }
2955
2956 #if 0 /* This is bogus because POS not having an overlay string
2957 position does not mean it's after the string. Example: A
2958 line starting with a before-string and initialization of IT
2959 to the previous row's end position. */
2960 else if (it->current.overlay_string_index >= 0)
2961 {
2962 /* If POS says we're already after an overlay string ending at
2963 POS, make sure to pop the iterator because it will be in
2964 front of that overlay string. When POS is ZV, we've thereby
2965 also ``processed'' overlay strings at ZV. */
2966 while (it->sp)
2967 pop_it (it);
2968 xassert (it->current.overlay_string_index == -1);
2969 xassert (it->method == GET_FROM_BUFFER);
2970 if (CHARPOS (pos->pos) == ZV)
2971 it->overlay_strings_at_end_processed_p = 1;
2972 }
2973 #endif /* 0 */
2974
2975 if (CHARPOS (pos->string_pos) >= 0)
2976 {
2977 /* Recorded position is not in an overlay string, but in another
2978 string. This can only be a string from a `display' property.
2979 IT should already be filled with that string. */
2980 it->current.string_pos = pos->string_pos;
2981 xassert (STRINGP (it->string));
2982 }
2983
2984 /* Restore position in display vector translations, control
2985 character translations or ellipses. */
2986 if (pos->dpvec_index >= 0)
2987 {
2988 if (it->dpvec == NULL)
2989 get_next_display_element (it);
2990 xassert (it->dpvec && it->current.dpvec_index == 0);
2991 it->current.dpvec_index = pos->dpvec_index;
2992 }
2993
2994 CHECK_IT (it);
2995 return !overlay_strings_with_newlines;
2996 }
2997
2998
2999 /* Initialize IT for stepping through current_buffer in window W
3000 starting at ROW->start. */
3001
3002 static void
3003 init_to_row_start (it, w, row)
3004 struct it *it;
3005 struct window *w;
3006 struct glyph_row *row;
3007 {
3008 init_from_display_pos (it, w, &row->start);
3009 it->start = row->start;
3010 it->continuation_lines_width = row->continuation_lines_width;
3011 CHECK_IT (it);
3012 }
3013
3014
3015 /* Initialize IT for stepping through current_buffer in window W
3016 starting in the line following ROW, i.e. starting at ROW->end.
3017 Value is zero if there are overlay strings with newlines at ROW's
3018 end position. */
3019
3020 static int
3021 init_to_row_end (it, w, row)
3022 struct it *it;
3023 struct window *w;
3024 struct glyph_row *row;
3025 {
3026 int success = 0;
3027
3028 if (init_from_display_pos (it, w, &row->end))
3029 {
3030 if (row->continued_p)
3031 it->continuation_lines_width
3032 = row->continuation_lines_width + row->pixel_width;
3033 CHECK_IT (it);
3034 success = 1;
3035 }
3036
3037 return success;
3038 }
3039
3040
3041
3042 \f
3043 /***********************************************************************
3044 Text properties
3045 ***********************************************************************/
3046
3047 /* Called when IT reaches IT->stop_charpos. Handle text property and
3048 overlay changes. Set IT->stop_charpos to the next position where
3049 to stop. */
3050
3051 static void
3052 handle_stop (it)
3053 struct it *it;
3054 {
3055 enum prop_handled handled;
3056 int handle_overlay_change_p;
3057 struct props *p;
3058
3059 it->dpvec = NULL;
3060 it->current.dpvec_index = -1;
3061 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3062 it->ignore_overlay_strings_at_pos_p = 0;
3063
3064 /* Use face of preceding text for ellipsis (if invisible) */
3065 if (it->selective_display_ellipsis_p)
3066 it->saved_face_id = it->face_id;
3067
3068 do
3069 {
3070 handled = HANDLED_NORMALLY;
3071
3072 /* Call text property handlers. */
3073 for (p = it_props; p->handler; ++p)
3074 {
3075 handled = p->handler (it);
3076
3077 if (handled == HANDLED_RECOMPUTE_PROPS)
3078 break;
3079 else if (handled == HANDLED_RETURN)
3080 {
3081 /* We still want to show before and after strings from
3082 overlays even if the actual buffer text is replaced. */
3083 if (!handle_overlay_change_p || it->sp > 1)
3084 return;
3085 if (!get_overlay_strings_1 (it, 0, 0))
3086 return;
3087 it->ignore_overlay_strings_at_pos_p = 1;
3088 it->string_from_display_prop_p = 0;
3089 handle_overlay_change_p = 0;
3090 handled = HANDLED_RECOMPUTE_PROPS;
3091 break;
3092 }
3093 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3094 handle_overlay_change_p = 0;
3095 }
3096
3097 if (handled != HANDLED_RECOMPUTE_PROPS)
3098 {
3099 /* Don't check for overlay strings below when set to deliver
3100 characters from a display vector. */
3101 if (it->method == GET_FROM_DISPLAY_VECTOR)
3102 handle_overlay_change_p = 0;
3103
3104 /* Handle overlay changes.
3105 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3106 if it finds overlays. */
3107 if (handle_overlay_change_p)
3108 handled = handle_overlay_change (it);
3109 }
3110 }
3111 while (handled == HANDLED_RECOMPUTE_PROPS);
3112
3113 /* Determine where to stop next. */
3114 if (handled == HANDLED_NORMALLY)
3115 compute_stop_pos (it);
3116 }
3117
3118
3119 /* Compute IT->stop_charpos from text property and overlay change
3120 information for IT's current position. */
3121
3122 static void
3123 compute_stop_pos (it)
3124 struct it *it;
3125 {
3126 register INTERVAL iv, next_iv;
3127 Lisp_Object object, limit, position;
3128
3129 /* If nowhere else, stop at the end. */
3130 it->stop_charpos = it->end_charpos;
3131
3132 if (STRINGP (it->string))
3133 {
3134 /* Strings are usually short, so don't limit the search for
3135 properties. */
3136 object = it->string;
3137 limit = Qnil;
3138 position = make_number (IT_STRING_CHARPOS (*it));
3139 }
3140 else
3141 {
3142 int charpos;
3143
3144 /* If next overlay change is in front of the current stop pos
3145 (which is IT->end_charpos), stop there. Note: value of
3146 next_overlay_change is point-max if no overlay change
3147 follows. */
3148 charpos = next_overlay_change (IT_CHARPOS (*it));
3149 if (charpos < it->stop_charpos)
3150 it->stop_charpos = charpos;
3151
3152 /* If showing the region, we have to stop at the region
3153 start or end because the face might change there. */
3154 if (it->region_beg_charpos > 0)
3155 {
3156 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3157 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3158 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3159 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3160 }
3161
3162 /* Set up variables for computing the stop position from text
3163 property changes. */
3164 XSETBUFFER (object, current_buffer);
3165 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3166 position = make_number (IT_CHARPOS (*it));
3167
3168 }
3169
3170 /* Get the interval containing IT's position. Value is a null
3171 interval if there isn't such an interval. */
3172 iv = validate_interval_range (object, &position, &position, 0);
3173 if (!NULL_INTERVAL_P (iv))
3174 {
3175 Lisp_Object values_here[LAST_PROP_IDX];
3176 struct props *p;
3177
3178 /* Get properties here. */
3179 for (p = it_props; p->handler; ++p)
3180 values_here[p->idx] = textget (iv->plist, *p->name);
3181
3182 /* Look for an interval following iv that has different
3183 properties. */
3184 for (next_iv = next_interval (iv);
3185 (!NULL_INTERVAL_P (next_iv)
3186 && (NILP (limit)
3187 || XFASTINT (limit) > next_iv->position));
3188 next_iv = next_interval (next_iv))
3189 {
3190 for (p = it_props; p->handler; ++p)
3191 {
3192 Lisp_Object new_value;
3193
3194 new_value = textget (next_iv->plist, *p->name);
3195 if (!EQ (values_here[p->idx], new_value))
3196 break;
3197 }
3198
3199 if (p->handler)
3200 break;
3201 }
3202
3203 if (!NULL_INTERVAL_P (next_iv))
3204 {
3205 if (INTEGERP (limit)
3206 && next_iv->position >= XFASTINT (limit))
3207 /* No text property change up to limit. */
3208 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3209 else
3210 /* Text properties change in next_iv. */
3211 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3212 }
3213 }
3214
3215 xassert (STRINGP (it->string)
3216 || (it->stop_charpos >= BEGV
3217 && it->stop_charpos >= IT_CHARPOS (*it)));
3218 }
3219
3220
3221 /* Return the position of the next overlay change after POS in
3222 current_buffer. Value is point-max if no overlay change
3223 follows. This is like `next-overlay-change' but doesn't use
3224 xmalloc. */
3225
3226 static EMACS_INT
3227 next_overlay_change (pos)
3228 EMACS_INT pos;
3229 {
3230 int noverlays;
3231 EMACS_INT endpos;
3232 Lisp_Object *overlays;
3233 int i;
3234
3235 /* Get all overlays at the given position. */
3236 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3237
3238 /* If any of these overlays ends before endpos,
3239 use its ending point instead. */
3240 for (i = 0; i < noverlays; ++i)
3241 {
3242 Lisp_Object oend;
3243 EMACS_INT oendpos;
3244
3245 oend = OVERLAY_END (overlays[i]);
3246 oendpos = OVERLAY_POSITION (oend);
3247 endpos = min (endpos, oendpos);
3248 }
3249
3250 return endpos;
3251 }
3252
3253
3254 \f
3255 /***********************************************************************
3256 Fontification
3257 ***********************************************************************/
3258
3259 /* Handle changes in the `fontified' property of the current buffer by
3260 calling hook functions from Qfontification_functions to fontify
3261 regions of text. */
3262
3263 static enum prop_handled
3264 handle_fontified_prop (it)
3265 struct it *it;
3266 {
3267 Lisp_Object prop, pos;
3268 enum prop_handled handled = HANDLED_NORMALLY;
3269
3270 if (!NILP (Vmemory_full))
3271 return handled;
3272
3273 /* Get the value of the `fontified' property at IT's current buffer
3274 position. (The `fontified' property doesn't have a special
3275 meaning in strings.) If the value is nil, call functions from
3276 Qfontification_functions. */
3277 if (!STRINGP (it->string)
3278 && it->s == NULL
3279 && !NILP (Vfontification_functions)
3280 && !NILP (Vrun_hooks)
3281 && (pos = make_number (IT_CHARPOS (*it)),
3282 prop = Fget_char_property (pos, Qfontified, Qnil),
3283 /* Ignore the special cased nil value always present at EOB since
3284 no amount of fontifying will be able to change it. */
3285 NILP (prop) && IT_CHARPOS (*it) < Z))
3286 {
3287 int count = SPECPDL_INDEX ();
3288 Lisp_Object val;
3289
3290 val = Vfontification_functions;
3291 specbind (Qfontification_functions, Qnil);
3292
3293 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3294 safe_call1 (val, pos);
3295 else
3296 {
3297 Lisp_Object globals, fn;
3298 struct gcpro gcpro1, gcpro2;
3299
3300 globals = Qnil;
3301 GCPRO2 (val, globals);
3302
3303 for (; CONSP (val); val = XCDR (val))
3304 {
3305 fn = XCAR (val);
3306
3307 if (EQ (fn, Qt))
3308 {
3309 /* A value of t indicates this hook has a local
3310 binding; it means to run the global binding too.
3311 In a global value, t should not occur. If it
3312 does, we must ignore it to avoid an endless
3313 loop. */
3314 for (globals = Fdefault_value (Qfontification_functions);
3315 CONSP (globals);
3316 globals = XCDR (globals))
3317 {
3318 fn = XCAR (globals);
3319 if (!EQ (fn, Qt))
3320 safe_call1 (fn, pos);
3321 }
3322 }
3323 else
3324 safe_call1 (fn, pos);
3325 }
3326
3327 UNGCPRO;
3328 }
3329
3330 unbind_to (count, Qnil);
3331
3332 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3333 something. This avoids an endless loop if they failed to
3334 fontify the text for which reason ever. */
3335 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3336 handled = HANDLED_RECOMPUTE_PROPS;
3337 }
3338
3339 return handled;
3340 }
3341
3342
3343 \f
3344 /***********************************************************************
3345 Faces
3346 ***********************************************************************/
3347
3348 /* Set up iterator IT from face properties at its current position.
3349 Called from handle_stop. */
3350
3351 static enum prop_handled
3352 handle_face_prop (it)
3353 struct it *it;
3354 {
3355 int new_face_id;
3356 EMACS_INT next_stop;
3357
3358 if (!STRINGP (it->string))
3359 {
3360 new_face_id
3361 = face_at_buffer_position (it->w,
3362 IT_CHARPOS (*it),
3363 it->region_beg_charpos,
3364 it->region_end_charpos,
3365 &next_stop,
3366 (IT_CHARPOS (*it)
3367 + TEXT_PROP_DISTANCE_LIMIT),
3368 0);
3369
3370 /* Is this a start of a run of characters with box face?
3371 Caveat: this can be called for a freshly initialized
3372 iterator; face_id is -1 in this case. We know that the new
3373 face will not change until limit, i.e. if the new face has a
3374 box, all characters up to limit will have one. But, as
3375 usual, we don't know whether limit is really the end. */
3376 if (new_face_id != it->face_id)
3377 {
3378 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3379
3380 /* If new face has a box but old face has not, this is
3381 the start of a run of characters with box, i.e. it has
3382 a shadow on the left side. The value of face_id of the
3383 iterator will be -1 if this is the initial call that gets
3384 the face. In this case, we have to look in front of IT's
3385 position and see whether there is a face != new_face_id. */
3386 it->start_of_box_run_p
3387 = (new_face->box != FACE_NO_BOX
3388 && (it->face_id >= 0
3389 || IT_CHARPOS (*it) == BEG
3390 || new_face_id != face_before_it_pos (it)));
3391 it->face_box_p = new_face->box != FACE_NO_BOX;
3392 }
3393 }
3394 else
3395 {
3396 int base_face_id, bufpos;
3397 int i;
3398 Lisp_Object from_overlay
3399 = (it->current.overlay_string_index >= 0
3400 ? it->string_overlays[it->current.overlay_string_index]
3401 : Qnil);
3402
3403 /* See if we got to this string directly or indirectly from
3404 an overlay property. That includes the before-string or
3405 after-string of an overlay, strings in display properties
3406 provided by an overlay, their text properties, etc.
3407
3408 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3409 if (! NILP (from_overlay))
3410 for (i = it->sp - 1; i >= 0; i--)
3411 {
3412 if (it->stack[i].current.overlay_string_index >= 0)
3413 from_overlay
3414 = it->string_overlays[it->stack[i].current.overlay_string_index];
3415 else if (! NILP (it->stack[i].from_overlay))
3416 from_overlay = it->stack[i].from_overlay;
3417
3418 if (!NILP (from_overlay))
3419 break;
3420 }
3421
3422 if (! NILP (from_overlay))
3423 {
3424 bufpos = IT_CHARPOS (*it);
3425 /* For a string from an overlay, the base face depends
3426 only on text properties and ignores overlays. */
3427 base_face_id
3428 = face_for_overlay_string (it->w,
3429 IT_CHARPOS (*it),
3430 it->region_beg_charpos,
3431 it->region_end_charpos,
3432 &next_stop,
3433 (IT_CHARPOS (*it)
3434 + TEXT_PROP_DISTANCE_LIMIT),
3435 0,
3436 from_overlay);
3437 }
3438 else
3439 {
3440 bufpos = 0;
3441
3442 /* For strings from a `display' property, use the face at
3443 IT's current buffer position as the base face to merge
3444 with, so that overlay strings appear in the same face as
3445 surrounding text, unless they specify their own
3446 faces. */
3447 base_face_id = underlying_face_id (it);
3448 }
3449
3450 new_face_id = face_at_string_position (it->w,
3451 it->string,
3452 IT_STRING_CHARPOS (*it),
3453 bufpos,
3454 it->region_beg_charpos,
3455 it->region_end_charpos,
3456 &next_stop,
3457 base_face_id, 0);
3458
3459 #if 0 /* This shouldn't be neccessary. Let's check it. */
3460 /* If IT is used to display a mode line we would really like to
3461 use the mode line face instead of the frame's default face. */
3462 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3463 && new_face_id == DEFAULT_FACE_ID)
3464 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3465 #endif
3466
3467 /* Is this a start of a run of characters with box? Caveat:
3468 this can be called for a freshly allocated iterator; face_id
3469 is -1 is this case. We know that the new face will not
3470 change until the next check pos, i.e. if the new face has a
3471 box, all characters up to that position will have a
3472 box. But, as usual, we don't know whether that position
3473 is really the end. */
3474 if (new_face_id != it->face_id)
3475 {
3476 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3477 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3478
3479 /* If new face has a box but old face hasn't, this is the
3480 start of a run of characters with box, i.e. it has a
3481 shadow on the left side. */
3482 it->start_of_box_run_p
3483 = new_face->box && (old_face == NULL || !old_face->box);
3484 it->face_box_p = new_face->box != FACE_NO_BOX;
3485 }
3486 }
3487
3488 it->face_id = new_face_id;
3489 return HANDLED_NORMALLY;
3490 }
3491
3492
3493 /* Return the ID of the face ``underlying'' IT's current position,
3494 which is in a string. If the iterator is associated with a
3495 buffer, return the face at IT's current buffer position.
3496 Otherwise, use the iterator's base_face_id. */
3497
3498 static int
3499 underlying_face_id (it)
3500 struct it *it;
3501 {
3502 int face_id = it->base_face_id, i;
3503
3504 xassert (STRINGP (it->string));
3505
3506 for (i = it->sp - 1; i >= 0; --i)
3507 if (NILP (it->stack[i].string))
3508 face_id = it->stack[i].face_id;
3509
3510 return face_id;
3511 }
3512
3513
3514 /* Compute the face one character before or after the current position
3515 of IT. BEFORE_P non-zero means get the face in front of IT's
3516 position. Value is the id of the face. */
3517
3518 static int
3519 face_before_or_after_it_pos (it, before_p)
3520 struct it *it;
3521 int before_p;
3522 {
3523 int face_id, limit;
3524 EMACS_INT next_check_charpos;
3525 struct text_pos pos;
3526
3527 xassert (it->s == NULL);
3528
3529 if (STRINGP (it->string))
3530 {
3531 int bufpos, base_face_id;
3532
3533 /* No face change past the end of the string (for the case
3534 we are padding with spaces). No face change before the
3535 string start. */
3536 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3537 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3538 return it->face_id;
3539
3540 /* Set pos to the position before or after IT's current position. */
3541 if (before_p)
3542 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3543 else
3544 /* For composition, we must check the character after the
3545 composition. */
3546 pos = (it->what == IT_COMPOSITION
3547 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3548 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3549
3550 if (it->current.overlay_string_index >= 0)
3551 bufpos = IT_CHARPOS (*it);
3552 else
3553 bufpos = 0;
3554
3555 base_face_id = underlying_face_id (it);
3556
3557 /* Get the face for ASCII, or unibyte. */
3558 face_id = face_at_string_position (it->w,
3559 it->string,
3560 CHARPOS (pos),
3561 bufpos,
3562 it->region_beg_charpos,
3563 it->region_end_charpos,
3564 &next_check_charpos,
3565 base_face_id, 0);
3566
3567 /* Correct the face for charsets different from ASCII. Do it
3568 for the multibyte case only. The face returned above is
3569 suitable for unibyte text if IT->string is unibyte. */
3570 if (STRING_MULTIBYTE (it->string))
3571 {
3572 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3573 int rest = SBYTES (it->string) - BYTEPOS (pos);
3574 int c, len;
3575 struct face *face = FACE_FROM_ID (it->f, face_id);
3576
3577 c = string_char_and_length (p, rest, &len);
3578 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3579 }
3580 }
3581 else
3582 {
3583 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3584 || (IT_CHARPOS (*it) <= BEGV && before_p))
3585 return it->face_id;
3586
3587 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3588 pos = it->current.pos;
3589
3590 if (before_p)
3591 DEC_TEXT_POS (pos, it->multibyte_p);
3592 else
3593 {
3594 if (it->what == IT_COMPOSITION)
3595 /* For composition, we must check the position after the
3596 composition. */
3597 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3598 else
3599 INC_TEXT_POS (pos, it->multibyte_p);
3600 }
3601
3602 /* Determine face for CHARSET_ASCII, or unibyte. */
3603 face_id = face_at_buffer_position (it->w,
3604 CHARPOS (pos),
3605 it->region_beg_charpos,
3606 it->region_end_charpos,
3607 &next_check_charpos,
3608 limit, 0);
3609
3610 /* Correct the face for charsets different from ASCII. Do it
3611 for the multibyte case only. The face returned above is
3612 suitable for unibyte text if current_buffer is unibyte. */
3613 if (it->multibyte_p)
3614 {
3615 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3616 struct face *face = FACE_FROM_ID (it->f, face_id);
3617 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3618 }
3619 }
3620
3621 return face_id;
3622 }
3623
3624
3625 \f
3626 /***********************************************************************
3627 Invisible text
3628 ***********************************************************************/
3629
3630 /* Set up iterator IT from invisible properties at its current
3631 position. Called from handle_stop. */
3632
3633 static enum prop_handled
3634 handle_invisible_prop (it)
3635 struct it *it;
3636 {
3637 enum prop_handled handled = HANDLED_NORMALLY;
3638
3639 if (STRINGP (it->string))
3640 {
3641 extern Lisp_Object Qinvisible;
3642 Lisp_Object prop, end_charpos, limit, charpos;
3643
3644 /* Get the value of the invisible text property at the
3645 current position. Value will be nil if there is no such
3646 property. */
3647 charpos = make_number (IT_STRING_CHARPOS (*it));
3648 prop = Fget_text_property (charpos, Qinvisible, it->string);
3649
3650 if (!NILP (prop)
3651 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3652 {
3653 handled = HANDLED_RECOMPUTE_PROPS;
3654
3655 /* Get the position at which the next change of the
3656 invisible text property can be found in IT->string.
3657 Value will be nil if the property value is the same for
3658 all the rest of IT->string. */
3659 XSETINT (limit, SCHARS (it->string));
3660 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3661 it->string, limit);
3662
3663 /* Text at current position is invisible. The next
3664 change in the property is at position end_charpos.
3665 Move IT's current position to that position. */
3666 if (INTEGERP (end_charpos)
3667 && XFASTINT (end_charpos) < XFASTINT (limit))
3668 {
3669 struct text_pos old;
3670 old = it->current.string_pos;
3671 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3672 compute_string_pos (&it->current.string_pos, old, it->string);
3673 }
3674 else
3675 {
3676 /* The rest of the string is invisible. If this is an
3677 overlay string, proceed with the next overlay string
3678 or whatever comes and return a character from there. */
3679 if (it->current.overlay_string_index >= 0)
3680 {
3681 next_overlay_string (it);
3682 /* Don't check for overlay strings when we just
3683 finished processing them. */
3684 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3685 }
3686 else
3687 {
3688 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3689 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3690 }
3691 }
3692 }
3693 }
3694 else
3695 {
3696 int invis_p;
3697 EMACS_INT newpos, next_stop, start_charpos;
3698 Lisp_Object pos, prop, overlay;
3699
3700 /* First of all, is there invisible text at this position? */
3701 start_charpos = IT_CHARPOS (*it);
3702 pos = make_number (IT_CHARPOS (*it));
3703 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3704 &overlay);
3705 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3706
3707 /* If we are on invisible text, skip over it. */
3708 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3709 {
3710 /* Record whether we have to display an ellipsis for the
3711 invisible text. */
3712 int display_ellipsis_p = invis_p == 2;
3713
3714 handled = HANDLED_RECOMPUTE_PROPS;
3715
3716 /* Loop skipping over invisible text. The loop is left at
3717 ZV or with IT on the first char being visible again. */
3718 do
3719 {
3720 /* Try to skip some invisible text. Return value is the
3721 position reached which can be equal to IT's position
3722 if there is nothing invisible here. This skips both
3723 over invisible text properties and overlays with
3724 invisible property. */
3725 newpos = skip_invisible (IT_CHARPOS (*it),
3726 &next_stop, ZV, it->window);
3727
3728 /* If we skipped nothing at all we weren't at invisible
3729 text in the first place. If everything to the end of
3730 the buffer was skipped, end the loop. */
3731 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3732 invis_p = 0;
3733 else
3734 {
3735 /* We skipped some characters but not necessarily
3736 all there are. Check if we ended up on visible
3737 text. Fget_char_property returns the property of
3738 the char before the given position, i.e. if we
3739 get invis_p = 0, this means that the char at
3740 newpos is visible. */
3741 pos = make_number (newpos);
3742 prop = Fget_char_property (pos, Qinvisible, it->window);
3743 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3744 }
3745
3746 /* If we ended up on invisible text, proceed to
3747 skip starting with next_stop. */
3748 if (invis_p)
3749 IT_CHARPOS (*it) = next_stop;
3750
3751 /* If there are adjacent invisible texts, don't lose the
3752 second one's ellipsis. */
3753 if (invis_p == 2)
3754 display_ellipsis_p = 1;
3755 }
3756 while (invis_p);
3757
3758 /* The position newpos is now either ZV or on visible text. */
3759 IT_CHARPOS (*it) = newpos;
3760 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3761
3762 /* If there are before-strings at the start of invisible
3763 text, and the text is invisible because of a text
3764 property, arrange to show before-strings because 20.x did
3765 it that way. (If the text is invisible because of an
3766 overlay property instead of a text property, this is
3767 already handled in the overlay code.) */
3768 if (NILP (overlay)
3769 && get_overlay_strings (it, start_charpos))
3770 {
3771 handled = HANDLED_RECOMPUTE_PROPS;
3772 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3773 }
3774 else if (display_ellipsis_p)
3775 {
3776 /* Make sure that the glyphs of the ellipsis will get
3777 correct `charpos' values. If we would not update
3778 it->position here, the glyphs would belong to the
3779 last visible character _before_ the invisible
3780 text, which confuses `set_cursor_from_row'.
3781
3782 We use the last invisible position instead of the
3783 first because this way the cursor is always drawn on
3784 the first "." of the ellipsis, whenever PT is inside
3785 the invisible text. Otherwise the cursor would be
3786 placed _after_ the ellipsis when the point is after the
3787 first invisible character. */
3788 if (!STRINGP (it->object))
3789 {
3790 it->position.charpos = IT_CHARPOS (*it) - 1;
3791 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3792 }
3793 setup_for_ellipsis (it, 0);
3794 /* Let the ellipsis display before
3795 considering any properties of the following char.
3796 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3797 handled = HANDLED_RETURN;
3798 }
3799 }
3800 }
3801
3802 return handled;
3803 }
3804
3805
3806 /* Make iterator IT return `...' next.
3807 Replaces LEN characters from buffer. */
3808
3809 static void
3810 setup_for_ellipsis (it, len)
3811 struct it *it;
3812 int len;
3813 {
3814 /* Use the display table definition for `...'. Invalid glyphs
3815 will be handled by the method returning elements from dpvec. */
3816 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3817 {
3818 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3819 it->dpvec = v->contents;
3820 it->dpend = v->contents + v->size;
3821 }
3822 else
3823 {
3824 /* Default `...'. */
3825 it->dpvec = default_invis_vector;
3826 it->dpend = default_invis_vector + 3;
3827 }
3828
3829 it->dpvec_char_len = len;
3830 it->current.dpvec_index = 0;
3831 it->dpvec_face_id = -1;
3832
3833 /* Remember the current face id in case glyphs specify faces.
3834 IT's face is restored in set_iterator_to_next.
3835 saved_face_id was set to preceding char's face in handle_stop. */
3836 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3837 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3838
3839 it->method = GET_FROM_DISPLAY_VECTOR;
3840 it->ellipsis_p = 1;
3841 }
3842
3843
3844 \f
3845 /***********************************************************************
3846 'display' property
3847 ***********************************************************************/
3848
3849 /* Set up iterator IT from `display' property at its current position.
3850 Called from handle_stop.
3851 We return HANDLED_RETURN if some part of the display property
3852 overrides the display of the buffer text itself.
3853 Otherwise we return HANDLED_NORMALLY. */
3854
3855 static enum prop_handled
3856 handle_display_prop (it)
3857 struct it *it;
3858 {
3859 Lisp_Object prop, object, overlay;
3860 struct text_pos *position;
3861 /* Nonzero if some property replaces the display of the text itself. */
3862 int display_replaced_p = 0;
3863
3864 if (STRINGP (it->string))
3865 {
3866 object = it->string;
3867 position = &it->current.string_pos;
3868 }
3869 else
3870 {
3871 XSETWINDOW (object, it->w);
3872 position = &it->current.pos;
3873 }
3874
3875 /* Reset those iterator values set from display property values. */
3876 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3877 it->space_width = Qnil;
3878 it->font_height = Qnil;
3879 it->voffset = 0;
3880
3881 /* We don't support recursive `display' properties, i.e. string
3882 values that have a string `display' property, that have a string
3883 `display' property etc. */
3884 if (!it->string_from_display_prop_p)
3885 it->area = TEXT_AREA;
3886
3887 prop = get_char_property_and_overlay (make_number (position->charpos),
3888 Qdisplay, object, &overlay);
3889 if (NILP (prop))
3890 return HANDLED_NORMALLY;
3891 /* Now OVERLAY is the overlay that gave us this property, or nil
3892 if it was a text property. */
3893
3894 if (!STRINGP (it->string))
3895 object = it->w->buffer;
3896
3897 if (CONSP (prop)
3898 /* Simple properties. */
3899 && !EQ (XCAR (prop), Qimage)
3900 && !EQ (XCAR (prop), Qspace)
3901 && !EQ (XCAR (prop), Qwhen)
3902 && !EQ (XCAR (prop), Qslice)
3903 && !EQ (XCAR (prop), Qspace_width)
3904 && !EQ (XCAR (prop), Qheight)
3905 && !EQ (XCAR (prop), Qraise)
3906 /* Marginal area specifications. */
3907 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3908 && !EQ (XCAR (prop), Qleft_fringe)
3909 && !EQ (XCAR (prop), Qright_fringe)
3910 && !NILP (XCAR (prop)))
3911 {
3912 for (; CONSP (prop); prop = XCDR (prop))
3913 {
3914 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3915 position, display_replaced_p))
3916 {
3917 display_replaced_p = 1;
3918 /* If some text in a string is replaced, `position' no
3919 longer points to the position of `object'. */
3920 if (STRINGP (object))
3921 break;
3922 }
3923 }
3924 }
3925 else if (VECTORP (prop))
3926 {
3927 int i;
3928 for (i = 0; i < ASIZE (prop); ++i)
3929 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3930 position, display_replaced_p))
3931 {
3932 display_replaced_p = 1;
3933 /* If some text in a string is replaced, `position' no
3934 longer points to the position of `object'. */
3935 if (STRINGP (object))
3936 break;
3937 }
3938 }
3939 else
3940 {
3941 int ret = handle_single_display_spec (it, prop, object, overlay,
3942 position, 0);
3943 if (ret < 0) /* Replaced by "", i.e. nothing. */
3944 return HANDLED_RECOMPUTE_PROPS;
3945 if (ret)
3946 display_replaced_p = 1;
3947 }
3948
3949 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3950 }
3951
3952
3953 /* Value is the position of the end of the `display' property starting
3954 at START_POS in OBJECT. */
3955
3956 static struct text_pos
3957 display_prop_end (it, object, start_pos)
3958 struct it *it;
3959 Lisp_Object object;
3960 struct text_pos start_pos;
3961 {
3962 Lisp_Object end;
3963 struct text_pos end_pos;
3964
3965 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3966 Qdisplay, object, Qnil);
3967 CHARPOS (end_pos) = XFASTINT (end);
3968 if (STRINGP (object))
3969 compute_string_pos (&end_pos, start_pos, it->string);
3970 else
3971 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3972
3973 return end_pos;
3974 }
3975
3976
3977 /* Set up IT from a single `display' specification PROP. OBJECT
3978 is the object in which the `display' property was found. *POSITION
3979 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3980 means that we previously saw a display specification which already
3981 replaced text display with something else, for example an image;
3982 we ignore such properties after the first one has been processed.
3983
3984 OVERLAY is the overlay this `display' property came from,
3985 or nil if it was a text property.
3986
3987 If PROP is a `space' or `image' specification, and in some other
3988 cases too, set *POSITION to the position where the `display'
3989 property ends.
3990
3991 Value is non-zero if something was found which replaces the display
3992 of buffer or string text. Specifically, the value is -1 if that
3993 "something" is "nothing". */
3994
3995 static int
3996 handle_single_display_spec (it, spec, object, overlay, position,
3997 display_replaced_before_p)
3998 struct it *it;
3999 Lisp_Object spec;
4000 Lisp_Object object;
4001 Lisp_Object overlay;
4002 struct text_pos *position;
4003 int display_replaced_before_p;
4004 {
4005 Lisp_Object form;
4006 Lisp_Object location, value;
4007 struct text_pos start_pos, save_pos;
4008 int valid_p;
4009
4010 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4011 If the result is non-nil, use VALUE instead of SPEC. */
4012 form = Qt;
4013 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4014 {
4015 spec = XCDR (spec);
4016 if (!CONSP (spec))
4017 return 0;
4018 form = XCAR (spec);
4019 spec = XCDR (spec);
4020 }
4021
4022 if (!NILP (form) && !EQ (form, Qt))
4023 {
4024 int count = SPECPDL_INDEX ();
4025 struct gcpro gcpro1;
4026
4027 /* Bind `object' to the object having the `display' property, a
4028 buffer or string. Bind `position' to the position in the
4029 object where the property was found, and `buffer-position'
4030 to the current position in the buffer. */
4031 specbind (Qobject, object);
4032 specbind (Qposition, make_number (CHARPOS (*position)));
4033 specbind (Qbuffer_position,
4034 make_number (STRINGP (object)
4035 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4036 GCPRO1 (form);
4037 form = safe_eval (form);
4038 UNGCPRO;
4039 unbind_to (count, Qnil);
4040 }
4041
4042 if (NILP (form))
4043 return 0;
4044
4045 /* Handle `(height HEIGHT)' specifications. */
4046 if (CONSP (spec)
4047 && EQ (XCAR (spec), Qheight)
4048 && CONSP (XCDR (spec)))
4049 {
4050 if (!FRAME_WINDOW_P (it->f))
4051 return 0;
4052
4053 it->font_height = XCAR (XCDR (spec));
4054 if (!NILP (it->font_height))
4055 {
4056 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4057 int new_height = -1;
4058
4059 if (CONSP (it->font_height)
4060 && (EQ (XCAR (it->font_height), Qplus)
4061 || EQ (XCAR (it->font_height), Qminus))
4062 && CONSP (XCDR (it->font_height))
4063 && INTEGERP (XCAR (XCDR (it->font_height))))
4064 {
4065 /* `(+ N)' or `(- N)' where N is an integer. */
4066 int steps = XINT (XCAR (XCDR (it->font_height)));
4067 if (EQ (XCAR (it->font_height), Qplus))
4068 steps = - steps;
4069 it->face_id = smaller_face (it->f, it->face_id, steps);
4070 }
4071 else if (FUNCTIONP (it->font_height))
4072 {
4073 /* Call function with current height as argument.
4074 Value is the new height. */
4075 Lisp_Object height;
4076 height = safe_call1 (it->font_height,
4077 face->lface[LFACE_HEIGHT_INDEX]);
4078 if (NUMBERP (height))
4079 new_height = XFLOATINT (height);
4080 }
4081 else if (NUMBERP (it->font_height))
4082 {
4083 /* Value is a multiple of the canonical char height. */
4084 struct face *face;
4085
4086 face = FACE_FROM_ID (it->f,
4087 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4088 new_height = (XFLOATINT (it->font_height)
4089 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4090 }
4091 else
4092 {
4093 /* Evaluate IT->font_height with `height' bound to the
4094 current specified height to get the new height. */
4095 int count = SPECPDL_INDEX ();
4096
4097 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4098 value = safe_eval (it->font_height);
4099 unbind_to (count, Qnil);
4100
4101 if (NUMBERP (value))
4102 new_height = XFLOATINT (value);
4103 }
4104
4105 if (new_height > 0)
4106 it->face_id = face_with_height (it->f, it->face_id, new_height);
4107 }
4108
4109 return 0;
4110 }
4111
4112 /* Handle `(space-width WIDTH)'. */
4113 if (CONSP (spec)
4114 && EQ (XCAR (spec), Qspace_width)
4115 && CONSP (XCDR (spec)))
4116 {
4117 if (!FRAME_WINDOW_P (it->f))
4118 return 0;
4119
4120 value = XCAR (XCDR (spec));
4121 if (NUMBERP (value) && XFLOATINT (value) > 0)
4122 it->space_width = value;
4123
4124 return 0;
4125 }
4126
4127 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4128 if (CONSP (spec)
4129 && EQ (XCAR (spec), Qslice))
4130 {
4131 Lisp_Object tem;
4132
4133 if (!FRAME_WINDOW_P (it->f))
4134 return 0;
4135
4136 if (tem = XCDR (spec), CONSP (tem))
4137 {
4138 it->slice.x = XCAR (tem);
4139 if (tem = XCDR (tem), CONSP (tem))
4140 {
4141 it->slice.y = XCAR (tem);
4142 if (tem = XCDR (tem), CONSP (tem))
4143 {
4144 it->slice.width = XCAR (tem);
4145 if (tem = XCDR (tem), CONSP (tem))
4146 it->slice.height = XCAR (tem);
4147 }
4148 }
4149 }
4150
4151 return 0;
4152 }
4153
4154 /* Handle `(raise FACTOR)'. */
4155 if (CONSP (spec)
4156 && EQ (XCAR (spec), Qraise)
4157 && CONSP (XCDR (spec)))
4158 {
4159 if (!FRAME_WINDOW_P (it->f))
4160 return 0;
4161
4162 #ifdef HAVE_WINDOW_SYSTEM
4163 value = XCAR (XCDR (spec));
4164 if (NUMBERP (value))
4165 {
4166 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4167 it->voffset = - (XFLOATINT (value)
4168 * (FONT_HEIGHT (face->font)));
4169 }
4170 #endif /* HAVE_WINDOW_SYSTEM */
4171
4172 return 0;
4173 }
4174
4175 /* Don't handle the other kinds of display specifications
4176 inside a string that we got from a `display' property. */
4177 if (it->string_from_display_prop_p)
4178 return 0;
4179
4180 /* Characters having this form of property are not displayed, so
4181 we have to find the end of the property. */
4182 start_pos = *position;
4183 *position = display_prop_end (it, object, start_pos);
4184 value = Qnil;
4185
4186 /* Stop the scan at that end position--we assume that all
4187 text properties change there. */
4188 it->stop_charpos = position->charpos;
4189
4190 /* Handle `(left-fringe BITMAP [FACE])'
4191 and `(right-fringe BITMAP [FACE])'. */
4192 if (CONSP (spec)
4193 && (EQ (XCAR (spec), Qleft_fringe)
4194 || EQ (XCAR (spec), Qright_fringe))
4195 && CONSP (XCDR (spec)))
4196 {
4197 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4198 int fringe_bitmap;
4199
4200 if (!FRAME_WINDOW_P (it->f))
4201 /* If we return here, POSITION has been advanced
4202 across the text with this property. */
4203 return 0;
4204
4205 #ifdef HAVE_WINDOW_SYSTEM
4206 value = XCAR (XCDR (spec));
4207 if (!SYMBOLP (value)
4208 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4209 /* If we return here, POSITION has been advanced
4210 across the text with this property. */
4211 return 0;
4212
4213 if (CONSP (XCDR (XCDR (spec))))
4214 {
4215 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4216 int face_id2 = lookup_derived_face (it->f, face_name,
4217 FRINGE_FACE_ID, 0);
4218 if (face_id2 >= 0)
4219 face_id = face_id2;
4220 }
4221
4222 /* Save current settings of IT so that we can restore them
4223 when we are finished with the glyph property value. */
4224
4225 save_pos = it->position;
4226 it->position = *position;
4227 push_it (it);
4228 it->position = save_pos;
4229
4230 it->area = TEXT_AREA;
4231 it->what = IT_IMAGE;
4232 it->image_id = -1; /* no image */
4233 it->position = start_pos;
4234 it->object = NILP (object) ? it->w->buffer : object;
4235 it->method = GET_FROM_IMAGE;
4236 it->from_overlay = Qnil;
4237 it->face_id = face_id;
4238
4239 /* Say that we haven't consumed the characters with
4240 `display' property yet. The call to pop_it in
4241 set_iterator_to_next will clean this up. */
4242 *position = start_pos;
4243
4244 if (EQ (XCAR (spec), Qleft_fringe))
4245 {
4246 it->left_user_fringe_bitmap = fringe_bitmap;
4247 it->left_user_fringe_face_id = face_id;
4248 }
4249 else
4250 {
4251 it->right_user_fringe_bitmap = fringe_bitmap;
4252 it->right_user_fringe_face_id = face_id;
4253 }
4254 #endif /* HAVE_WINDOW_SYSTEM */
4255 return 1;
4256 }
4257
4258 /* Prepare to handle `((margin left-margin) ...)',
4259 `((margin right-margin) ...)' and `((margin nil) ...)'
4260 prefixes for display specifications. */
4261 location = Qunbound;
4262 if (CONSP (spec) && CONSP (XCAR (spec)))
4263 {
4264 Lisp_Object tem;
4265
4266 value = XCDR (spec);
4267 if (CONSP (value))
4268 value = XCAR (value);
4269
4270 tem = XCAR (spec);
4271 if (EQ (XCAR (tem), Qmargin)
4272 && (tem = XCDR (tem),
4273 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4274 (NILP (tem)
4275 || EQ (tem, Qleft_margin)
4276 || EQ (tem, Qright_margin))))
4277 location = tem;
4278 }
4279
4280 if (EQ (location, Qunbound))
4281 {
4282 location = Qnil;
4283 value = spec;
4284 }
4285
4286 /* After this point, VALUE is the property after any
4287 margin prefix has been stripped. It must be a string,
4288 an image specification, or `(space ...)'.
4289
4290 LOCATION specifies where to display: `left-margin',
4291 `right-margin' or nil. */
4292
4293 valid_p = (STRINGP (value)
4294 #ifdef HAVE_WINDOW_SYSTEM
4295 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4296 #endif /* not HAVE_WINDOW_SYSTEM */
4297 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4298
4299 if (valid_p && !display_replaced_before_p)
4300 {
4301 /* Save current settings of IT so that we can restore them
4302 when we are finished with the glyph property value. */
4303 save_pos = it->position;
4304 it->position = *position;
4305 push_it (it);
4306 it->position = save_pos;
4307 it->from_overlay = overlay;
4308
4309 if (NILP (location))
4310 it->area = TEXT_AREA;
4311 else if (EQ (location, Qleft_margin))
4312 it->area = LEFT_MARGIN_AREA;
4313 else
4314 it->area = RIGHT_MARGIN_AREA;
4315
4316 if (STRINGP (value))
4317 {
4318 if (SCHARS (value) == 0)
4319 {
4320 pop_it (it);
4321 return -1; /* Replaced by "", i.e. nothing. */
4322 }
4323 it->string = value;
4324 it->multibyte_p = STRING_MULTIBYTE (it->string);
4325 it->current.overlay_string_index = -1;
4326 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4327 it->end_charpos = it->string_nchars = SCHARS (it->string);
4328 it->method = GET_FROM_STRING;
4329 it->stop_charpos = 0;
4330 it->string_from_display_prop_p = 1;
4331 /* Say that we haven't consumed the characters with
4332 `display' property yet. The call to pop_it in
4333 set_iterator_to_next will clean this up. */
4334 if (BUFFERP (object))
4335 *position = start_pos;
4336 }
4337 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4338 {
4339 it->method = GET_FROM_STRETCH;
4340 it->object = value;
4341 *position = it->position = start_pos;
4342 }
4343 #ifdef HAVE_WINDOW_SYSTEM
4344 else
4345 {
4346 it->what = IT_IMAGE;
4347 it->image_id = lookup_image (it->f, value);
4348 it->position = start_pos;
4349 it->object = NILP (object) ? it->w->buffer : object;
4350 it->method = GET_FROM_IMAGE;
4351
4352 /* Say that we haven't consumed the characters with
4353 `display' property yet. The call to pop_it in
4354 set_iterator_to_next will clean this up. */
4355 *position = start_pos;
4356 }
4357 #endif /* HAVE_WINDOW_SYSTEM */
4358
4359 return 1;
4360 }
4361
4362 /* Invalid property or property not supported. Restore
4363 POSITION to what it was before. */
4364 *position = start_pos;
4365 return 0;
4366 }
4367
4368
4369 /* Check if SPEC is a display sub-property value whose text should be
4370 treated as intangible. */
4371
4372 static int
4373 single_display_spec_intangible_p (prop)
4374 Lisp_Object prop;
4375 {
4376 /* Skip over `when FORM'. */
4377 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4378 {
4379 prop = XCDR (prop);
4380 if (!CONSP (prop))
4381 return 0;
4382 prop = XCDR (prop);
4383 }
4384
4385 if (STRINGP (prop))
4386 return 1;
4387
4388 if (!CONSP (prop))
4389 return 0;
4390
4391 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4392 we don't need to treat text as intangible. */
4393 if (EQ (XCAR (prop), Qmargin))
4394 {
4395 prop = XCDR (prop);
4396 if (!CONSP (prop))
4397 return 0;
4398
4399 prop = XCDR (prop);
4400 if (!CONSP (prop)
4401 || EQ (XCAR (prop), Qleft_margin)
4402 || EQ (XCAR (prop), Qright_margin))
4403 return 0;
4404 }
4405
4406 return (CONSP (prop)
4407 && (EQ (XCAR (prop), Qimage)
4408 || EQ (XCAR (prop), Qspace)));
4409 }
4410
4411
4412 /* Check if PROP is a display property value whose text should be
4413 treated as intangible. */
4414
4415 int
4416 display_prop_intangible_p (prop)
4417 Lisp_Object prop;
4418 {
4419 if (CONSP (prop)
4420 && CONSP (XCAR (prop))
4421 && !EQ (Qmargin, XCAR (XCAR (prop))))
4422 {
4423 /* A list of sub-properties. */
4424 while (CONSP (prop))
4425 {
4426 if (single_display_spec_intangible_p (XCAR (prop)))
4427 return 1;
4428 prop = XCDR (prop);
4429 }
4430 }
4431 else if (VECTORP (prop))
4432 {
4433 /* A vector of sub-properties. */
4434 int i;
4435 for (i = 0; i < ASIZE (prop); ++i)
4436 if (single_display_spec_intangible_p (AREF (prop, i)))
4437 return 1;
4438 }
4439 else
4440 return single_display_spec_intangible_p (prop);
4441
4442 return 0;
4443 }
4444
4445
4446 /* Return 1 if PROP is a display sub-property value containing STRING. */
4447
4448 static int
4449 single_display_spec_string_p (prop, string)
4450 Lisp_Object prop, string;
4451 {
4452 if (EQ (string, prop))
4453 return 1;
4454
4455 /* Skip over `when FORM'. */
4456 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4457 {
4458 prop = XCDR (prop);
4459 if (!CONSP (prop))
4460 return 0;
4461 prop = XCDR (prop);
4462 }
4463
4464 if (CONSP (prop))
4465 /* Skip over `margin LOCATION'. */
4466 if (EQ (XCAR (prop), Qmargin))
4467 {
4468 prop = XCDR (prop);
4469 if (!CONSP (prop))
4470 return 0;
4471
4472 prop = XCDR (prop);
4473 if (!CONSP (prop))
4474 return 0;
4475 }
4476
4477 return CONSP (prop) && EQ (XCAR (prop), string);
4478 }
4479
4480
4481 /* Return 1 if STRING appears in the `display' property PROP. */
4482
4483 static int
4484 display_prop_string_p (prop, string)
4485 Lisp_Object prop, string;
4486 {
4487 if (CONSP (prop)
4488 && CONSP (XCAR (prop))
4489 && !EQ (Qmargin, XCAR (XCAR (prop))))
4490 {
4491 /* A list of sub-properties. */
4492 while (CONSP (prop))
4493 {
4494 if (single_display_spec_string_p (XCAR (prop), string))
4495 return 1;
4496 prop = XCDR (prop);
4497 }
4498 }
4499 else if (VECTORP (prop))
4500 {
4501 /* A vector of sub-properties. */
4502 int i;
4503 for (i = 0; i < ASIZE (prop); ++i)
4504 if (single_display_spec_string_p (AREF (prop, i), string))
4505 return 1;
4506 }
4507 else
4508 return single_display_spec_string_p (prop, string);
4509
4510 return 0;
4511 }
4512
4513
4514 /* Determine from which buffer position in W's buffer STRING comes
4515 from. AROUND_CHARPOS is an approximate position where it could
4516 be from. Value is the buffer position or 0 if it couldn't be
4517 determined.
4518
4519 W's buffer must be current.
4520
4521 This function is necessary because we don't record buffer positions
4522 in glyphs generated from strings (to keep struct glyph small).
4523 This function may only use code that doesn't eval because it is
4524 called asynchronously from note_mouse_highlight. */
4525
4526 int
4527 string_buffer_position (w, string, around_charpos)
4528 struct window *w;
4529 Lisp_Object string;
4530 int around_charpos;
4531 {
4532 Lisp_Object limit, prop, pos;
4533 const int MAX_DISTANCE = 1000;
4534 int found = 0;
4535
4536 pos = make_number (around_charpos);
4537 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4538 while (!found && !EQ (pos, limit))
4539 {
4540 prop = Fget_char_property (pos, Qdisplay, Qnil);
4541 if (!NILP (prop) && display_prop_string_p (prop, string))
4542 found = 1;
4543 else
4544 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4545 }
4546
4547 if (!found)
4548 {
4549 pos = make_number (around_charpos);
4550 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4551 while (!found && !EQ (pos, limit))
4552 {
4553 prop = Fget_char_property (pos, Qdisplay, Qnil);
4554 if (!NILP (prop) && display_prop_string_p (prop, string))
4555 found = 1;
4556 else
4557 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4558 limit);
4559 }
4560 }
4561
4562 return found ? XINT (pos) : 0;
4563 }
4564
4565
4566 \f
4567 /***********************************************************************
4568 `composition' property
4569 ***********************************************************************/
4570
4571 static enum prop_handled
4572 handle_auto_composed_prop (it)
4573 struct it *it;
4574 {
4575 enum prop_handled handled = HANDLED_NORMALLY;
4576
4577 if (FRAME_WINDOW_P (it->f) && FUNCTIONP (Vauto_composition_function))
4578 {
4579 Lisp_Object val = Qnil;
4580 EMACS_INT pos, limit = -1;
4581
4582 if (STRINGP (it->string))
4583 pos = IT_STRING_CHARPOS (*it);
4584 else
4585 pos = IT_CHARPOS (*it);
4586
4587 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4588 if (! NILP (val))
4589 {
4590 Lisp_Object cmp_prop;
4591 EMACS_INT cmp_start, cmp_end;
4592
4593 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4594 &cmp_start, &cmp_end, it->string)
4595 && cmp_start == pos
4596 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4597 {
4598 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4599 Lisp_Object font_object = LGSTRING_FONT (gstring);
4600
4601 if (! EQ (font_object,
4602 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4603 it->w, it->string)))
4604 /* We must re-compute the composition for the
4605 different font. */
4606 val = Qnil;
4607 }
4608
4609 if (! NILP (val))
4610 {
4611 Lisp_Object end;
4612
4613 /* As Fnext_single_char_property_change is very slow, we
4614 limit the search to the current line. */
4615 if (STRINGP (it->string))
4616 limit = SCHARS (it->string);
4617 else
4618 limit = find_next_newline_no_quit (pos, 1);
4619 end = Fnext_single_char_property_change (make_number (pos),
4620 Qauto_composed,
4621 it->string,
4622 make_number (limit));
4623
4624 if (XINT (end) < limit)
4625 /* The current point is auto-composed, but there exist
4626 characters not yet composed beyond the
4627 auto-composed region. There's a possiblity that
4628 the last characters in the region may be newly
4629 composed. */
4630 val = Qnil;
4631 }
4632 }
4633 if (NILP (val) && ! STRINGP (it->string))
4634 {
4635 if (limit < 0)
4636 limit = (STRINGP (it->string) ? SCHARS (it->string)
4637 : find_next_newline_no_quit (pos, 1));
4638 if (pos < limit)
4639 {
4640 int count = SPECPDL_INDEX ();
4641 Lisp_Object args[5];
4642
4643 limit = font_range (pos, limit, FACE_FROM_ID (it->f, it->face_id),
4644 it->f, it->string);
4645 args[0] = Vauto_composition_function;
4646 specbind (Qauto_composition_function, Qnil);
4647 args[1] = make_number (pos);
4648 args[2] = make_number (limit);
4649 args[3] = it->window;
4650 args[4] = it->string;
4651 safe_call (5, args);
4652 unbind_to (count, Qnil);
4653 }
4654 }
4655 }
4656
4657 return handled;
4658 }
4659
4660 /* Set up iterator IT from `composition' property at its current
4661 position. Called from handle_stop. */
4662
4663 static enum prop_handled
4664 handle_composition_prop (it)
4665 struct it *it;
4666 {
4667 Lisp_Object prop, string;
4668 EMACS_INT pos, pos_byte, start, end;
4669 enum prop_handled handled = HANDLED_NORMALLY;
4670
4671 if (STRINGP (it->string))
4672 {
4673 unsigned char *s;
4674
4675 pos = IT_STRING_CHARPOS (*it);
4676 pos_byte = IT_STRING_BYTEPOS (*it);
4677 string = it->string;
4678 s = SDATA (string) + pos_byte;
4679 it->c = STRING_CHAR (s, 0);
4680 }
4681 else
4682 {
4683 pos = IT_CHARPOS (*it);
4684 pos_byte = IT_BYTEPOS (*it);
4685 string = Qnil;
4686 it->c = FETCH_CHAR (pos_byte);
4687 }
4688
4689 /* If there's a valid composition and point is not inside of the
4690 composition (in the case that the composition is from the current
4691 buffer), draw a glyph composed from the composition components. */
4692 if (find_composition (pos, -1, &start, &end, &prop, string)
4693 && COMPOSITION_VALID_P (start, end, prop)
4694 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4695 {
4696 int id;
4697
4698 if (start != pos)
4699 {
4700 if (STRINGP (it->string))
4701 pos_byte = string_char_to_byte (it->string, start);
4702 else
4703 pos_byte = CHAR_TO_BYTE (start);
4704 }
4705 id = get_composition_id (start, pos_byte, end - start, prop, string);
4706
4707 if (id >= 0)
4708 {
4709 struct composition *cmp = composition_table[id];
4710
4711 if (cmp->glyph_len == 0)
4712 {
4713 /* No glyph. */
4714 if (STRINGP (it->string))
4715 {
4716 IT_STRING_CHARPOS (*it) = end;
4717 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4718 end);
4719 }
4720 else
4721 {
4722 IT_CHARPOS (*it) = end;
4723 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4724 }
4725 return HANDLED_RECOMPUTE_PROPS;
4726 }
4727
4728 it->stop_charpos = end;
4729 push_it (it);
4730
4731 it->method = GET_FROM_COMPOSITION;
4732 it->cmp_id = id;
4733 it->cmp_len = COMPOSITION_LENGTH (prop);
4734 /* For a terminal, draw only the first (non-TAB) character
4735 of the components. */
4736 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4737 {
4738 /* FIXME: This doesn't do anything!?! */
4739 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4740 ->key_and_value,
4741 cmp->hash_index * 2);
4742 }
4743 else
4744 {
4745 int i;
4746
4747 for (i = 0; i < cmp->glyph_len; i++)
4748 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4749 != '\t')
4750 break;
4751 }
4752 if (it->c == '\t')
4753 it->c = ' ';
4754 it->len = (STRINGP (it->string)
4755 ? string_char_to_byte (it->string, end)
4756 : CHAR_TO_BYTE (end)) - pos_byte;
4757 handled = HANDLED_RETURN;
4758 }
4759 }
4760
4761 return handled;
4762 }
4763
4764
4765 \f
4766 /***********************************************************************
4767 Overlay strings
4768 ***********************************************************************/
4769
4770 /* The following structure is used to record overlay strings for
4771 later sorting in load_overlay_strings. */
4772
4773 struct overlay_entry
4774 {
4775 Lisp_Object overlay;
4776 Lisp_Object string;
4777 int priority;
4778 int after_string_p;
4779 };
4780
4781
4782 /* Set up iterator IT from overlay strings at its current position.
4783 Called from handle_stop. */
4784
4785 static enum prop_handled
4786 handle_overlay_change (it)
4787 struct it *it;
4788 {
4789 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4790 return HANDLED_RECOMPUTE_PROPS;
4791 else
4792 return HANDLED_NORMALLY;
4793 }
4794
4795
4796 /* Set up the next overlay string for delivery by IT, if there is an
4797 overlay string to deliver. Called by set_iterator_to_next when the
4798 end of the current overlay string is reached. If there are more
4799 overlay strings to display, IT->string and
4800 IT->current.overlay_string_index are set appropriately here.
4801 Otherwise IT->string is set to nil. */
4802
4803 static void
4804 next_overlay_string (it)
4805 struct it *it;
4806 {
4807 ++it->current.overlay_string_index;
4808 if (it->current.overlay_string_index == it->n_overlay_strings)
4809 {
4810 /* No more overlay strings. Restore IT's settings to what
4811 they were before overlay strings were processed, and
4812 continue to deliver from current_buffer. */
4813 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4814
4815 pop_it (it);
4816 xassert (it->sp > 0
4817 || it->method == GET_FROM_COMPOSITION
4818 || (NILP (it->string)
4819 && it->method == GET_FROM_BUFFER
4820 && it->stop_charpos >= BEGV
4821 && it->stop_charpos <= it->end_charpos));
4822 it->current.overlay_string_index = -1;
4823 it->n_overlay_strings = 0;
4824
4825 /* If we're at the end of the buffer, record that we have
4826 processed the overlay strings there already, so that
4827 next_element_from_buffer doesn't try it again. */
4828 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4829 it->overlay_strings_at_end_processed_p = 1;
4830
4831 /* If we have to display `...' for invisible text, set
4832 the iterator up for that. */
4833 if (display_ellipsis_p)
4834 setup_for_ellipsis (it, 0);
4835 }
4836 else
4837 {
4838 /* There are more overlay strings to process. If
4839 IT->current.overlay_string_index has advanced to a position
4840 where we must load IT->overlay_strings with more strings, do
4841 it. */
4842 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4843
4844 if (it->current.overlay_string_index && i == 0)
4845 load_overlay_strings (it, 0);
4846
4847 /* Initialize IT to deliver display elements from the overlay
4848 string. */
4849 it->string = it->overlay_strings[i];
4850 it->multibyte_p = STRING_MULTIBYTE (it->string);
4851 SET_TEXT_POS (it->current.string_pos, 0, 0);
4852 it->method = GET_FROM_STRING;
4853 it->stop_charpos = 0;
4854 }
4855
4856 CHECK_IT (it);
4857 }
4858
4859
4860 /* Compare two overlay_entry structures E1 and E2. Used as a
4861 comparison function for qsort in load_overlay_strings. Overlay
4862 strings for the same position are sorted so that
4863
4864 1. All after-strings come in front of before-strings, except
4865 when they come from the same overlay.
4866
4867 2. Within after-strings, strings are sorted so that overlay strings
4868 from overlays with higher priorities come first.
4869
4870 2. Within before-strings, strings are sorted so that overlay
4871 strings from overlays with higher priorities come last.
4872
4873 Value is analogous to strcmp. */
4874
4875
4876 static int
4877 compare_overlay_entries (e1, e2)
4878 void *e1, *e2;
4879 {
4880 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4881 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4882 int result;
4883
4884 if (entry1->after_string_p != entry2->after_string_p)
4885 {
4886 /* Let after-strings appear in front of before-strings if
4887 they come from different overlays. */
4888 if (EQ (entry1->overlay, entry2->overlay))
4889 result = entry1->after_string_p ? 1 : -1;
4890 else
4891 result = entry1->after_string_p ? -1 : 1;
4892 }
4893 else if (entry1->after_string_p)
4894 /* After-strings sorted in order of decreasing priority. */
4895 result = entry2->priority - entry1->priority;
4896 else
4897 /* Before-strings sorted in order of increasing priority. */
4898 result = entry1->priority - entry2->priority;
4899
4900 return result;
4901 }
4902
4903
4904 /* Load the vector IT->overlay_strings with overlay strings from IT's
4905 current buffer position, or from CHARPOS if that is > 0. Set
4906 IT->n_overlays to the total number of overlay strings found.
4907
4908 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4909 a time. On entry into load_overlay_strings,
4910 IT->current.overlay_string_index gives the number of overlay
4911 strings that have already been loaded by previous calls to this
4912 function.
4913
4914 IT->add_overlay_start contains an additional overlay start
4915 position to consider for taking overlay strings from, if non-zero.
4916 This position comes into play when the overlay has an `invisible'
4917 property, and both before and after-strings. When we've skipped to
4918 the end of the overlay, because of its `invisible' property, we
4919 nevertheless want its before-string to appear.
4920 IT->add_overlay_start will contain the overlay start position
4921 in this case.
4922
4923 Overlay strings are sorted so that after-string strings come in
4924 front of before-string strings. Within before and after-strings,
4925 strings are sorted by overlay priority. See also function
4926 compare_overlay_entries. */
4927
4928 static void
4929 load_overlay_strings (it, charpos)
4930 struct it *it;
4931 int charpos;
4932 {
4933 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4934 Lisp_Object overlay, window, str, invisible;
4935 struct Lisp_Overlay *ov;
4936 int start, end;
4937 int size = 20;
4938 int n = 0, i, j, invis_p;
4939 struct overlay_entry *entries
4940 = (struct overlay_entry *) alloca (size * sizeof *entries);
4941
4942 if (charpos <= 0)
4943 charpos = IT_CHARPOS (*it);
4944
4945 /* Append the overlay string STRING of overlay OVERLAY to vector
4946 `entries' which has size `size' and currently contains `n'
4947 elements. AFTER_P non-zero means STRING is an after-string of
4948 OVERLAY. */
4949 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4950 do \
4951 { \
4952 Lisp_Object priority; \
4953 \
4954 if (n == size) \
4955 { \
4956 int new_size = 2 * size; \
4957 struct overlay_entry *old = entries; \
4958 entries = \
4959 (struct overlay_entry *) alloca (new_size \
4960 * sizeof *entries); \
4961 bcopy (old, entries, size * sizeof *entries); \
4962 size = new_size; \
4963 } \
4964 \
4965 entries[n].string = (STRING); \
4966 entries[n].overlay = (OVERLAY); \
4967 priority = Foverlay_get ((OVERLAY), Qpriority); \
4968 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4969 entries[n].after_string_p = (AFTER_P); \
4970 ++n; \
4971 } \
4972 while (0)
4973
4974 /* Process overlay before the overlay center. */
4975 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4976 {
4977 XSETMISC (overlay, ov);
4978 xassert (OVERLAYP (overlay));
4979 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4980 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4981
4982 if (end < charpos)
4983 break;
4984
4985 /* Skip this overlay if it doesn't start or end at IT's current
4986 position. */
4987 if (end != charpos && start != charpos)
4988 continue;
4989
4990 /* Skip this overlay if it doesn't apply to IT->w. */
4991 window = Foverlay_get (overlay, Qwindow);
4992 if (WINDOWP (window) && XWINDOW (window) != it->w)
4993 continue;
4994
4995 /* If the text ``under'' the overlay is invisible, both before-
4996 and after-strings from this overlay are visible; start and
4997 end position are indistinguishable. */
4998 invisible = Foverlay_get (overlay, Qinvisible);
4999 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5000
5001 /* If overlay has a non-empty before-string, record it. */
5002 if ((start == charpos || (end == charpos && invis_p))
5003 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5004 && SCHARS (str))
5005 RECORD_OVERLAY_STRING (overlay, str, 0);
5006
5007 /* If overlay has a non-empty after-string, record it. */
5008 if ((end == charpos || (start == charpos && invis_p))
5009 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5010 && SCHARS (str))
5011 RECORD_OVERLAY_STRING (overlay, str, 1);
5012 }
5013
5014 /* Process overlays after the overlay center. */
5015 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5016 {
5017 XSETMISC (overlay, ov);
5018 xassert (OVERLAYP (overlay));
5019 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5020 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5021
5022 if (start > charpos)
5023 break;
5024
5025 /* Skip this overlay if it doesn't start or end at IT's current
5026 position. */
5027 if (end != charpos && start != charpos)
5028 continue;
5029
5030 /* Skip this overlay if it doesn't apply to IT->w. */
5031 window = Foverlay_get (overlay, Qwindow);
5032 if (WINDOWP (window) && XWINDOW (window) != it->w)
5033 continue;
5034
5035 /* If the text ``under'' the overlay is invisible, it has a zero
5036 dimension, and both before- and after-strings apply. */
5037 invisible = Foverlay_get (overlay, Qinvisible);
5038 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5039
5040 /* If overlay has a non-empty before-string, record it. */
5041 if ((start == charpos || (end == charpos && invis_p))
5042 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5043 && SCHARS (str))
5044 RECORD_OVERLAY_STRING (overlay, str, 0);
5045
5046 /* If overlay has a non-empty after-string, record it. */
5047 if ((end == charpos || (start == charpos && invis_p))
5048 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5049 && SCHARS (str))
5050 RECORD_OVERLAY_STRING (overlay, str, 1);
5051 }
5052
5053 #undef RECORD_OVERLAY_STRING
5054
5055 /* Sort entries. */
5056 if (n > 1)
5057 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5058
5059 /* Record the total number of strings to process. */
5060 it->n_overlay_strings = n;
5061
5062 /* IT->current.overlay_string_index is the number of overlay strings
5063 that have already been consumed by IT. Copy some of the
5064 remaining overlay strings to IT->overlay_strings. */
5065 i = 0;
5066 j = it->current.overlay_string_index;
5067 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5068 {
5069 it->overlay_strings[i] = entries[j].string;
5070 it->string_overlays[i++] = entries[j++].overlay;
5071 }
5072
5073 CHECK_IT (it);
5074 }
5075
5076
5077 /* Get the first chunk of overlay strings at IT's current buffer
5078 position, or at CHARPOS if that is > 0. Value is non-zero if at
5079 least one overlay string was found. */
5080
5081 static int
5082 get_overlay_strings_1 (it, charpos, compute_stop_p)
5083 struct it *it;
5084 int charpos;
5085 int compute_stop_p;
5086 {
5087 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5088 process. This fills IT->overlay_strings with strings, and sets
5089 IT->n_overlay_strings to the total number of strings to process.
5090 IT->pos.overlay_string_index has to be set temporarily to zero
5091 because load_overlay_strings needs this; it must be set to -1
5092 when no overlay strings are found because a zero value would
5093 indicate a position in the first overlay string. */
5094 it->current.overlay_string_index = 0;
5095 load_overlay_strings (it, charpos);
5096
5097 /* If we found overlay strings, set up IT to deliver display
5098 elements from the first one. Otherwise set up IT to deliver
5099 from current_buffer. */
5100 if (it->n_overlay_strings)
5101 {
5102 /* Make sure we know settings in current_buffer, so that we can
5103 restore meaningful values when we're done with the overlay
5104 strings. */
5105 if (compute_stop_p)
5106 compute_stop_pos (it);
5107 xassert (it->face_id >= 0);
5108
5109 /* Save IT's settings. They are restored after all overlay
5110 strings have been processed. */
5111 xassert (!compute_stop_p || it->sp == 0);
5112 push_it (it);
5113
5114 /* Set up IT to deliver display elements from the first overlay
5115 string. */
5116 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5117 it->string = it->overlay_strings[0];
5118 it->from_overlay = Qnil;
5119 it->stop_charpos = 0;
5120 xassert (STRINGP (it->string));
5121 it->end_charpos = SCHARS (it->string);
5122 it->multibyte_p = STRING_MULTIBYTE (it->string);
5123 it->method = GET_FROM_STRING;
5124 return 1;
5125 }
5126
5127 it->current.overlay_string_index = -1;
5128 return 0;
5129 }
5130
5131 static int
5132 get_overlay_strings (it, charpos)
5133 struct it *it;
5134 int charpos;
5135 {
5136 it->string = Qnil;
5137 it->method = GET_FROM_BUFFER;
5138
5139 (void) get_overlay_strings_1 (it, charpos, 1);
5140
5141 CHECK_IT (it);
5142
5143 /* Value is non-zero if we found at least one overlay string. */
5144 return STRINGP (it->string);
5145 }
5146
5147
5148 \f
5149 /***********************************************************************
5150 Saving and restoring state
5151 ***********************************************************************/
5152
5153 /* Save current settings of IT on IT->stack. Called, for example,
5154 before setting up IT for an overlay string, to be able to restore
5155 IT's settings to what they were after the overlay string has been
5156 processed. */
5157
5158 static void
5159 push_it (it)
5160 struct it *it;
5161 {
5162 struct iterator_stack_entry *p;
5163
5164 xassert (it->sp < IT_STACK_SIZE);
5165 p = it->stack + it->sp;
5166
5167 p->stop_charpos = it->stop_charpos;
5168 xassert (it->face_id >= 0);
5169 p->face_id = it->face_id;
5170 p->string = it->string;
5171 p->method = it->method;
5172 p->from_overlay = it->from_overlay;
5173 switch (p->method)
5174 {
5175 case GET_FROM_IMAGE:
5176 p->u.image.object = it->object;
5177 p->u.image.image_id = it->image_id;
5178 p->u.image.slice = it->slice;
5179 break;
5180 case GET_FROM_COMPOSITION:
5181 p->u.comp.object = it->object;
5182 p->u.comp.c = it->c;
5183 p->u.comp.len = it->len;
5184 p->u.comp.cmp_id = it->cmp_id;
5185 p->u.comp.cmp_len = it->cmp_len;
5186 break;
5187 case GET_FROM_STRETCH:
5188 p->u.stretch.object = it->object;
5189 break;
5190 }
5191 p->position = it->position;
5192 p->current = it->current;
5193 p->end_charpos = it->end_charpos;
5194 p->string_nchars = it->string_nchars;
5195 p->area = it->area;
5196 p->multibyte_p = it->multibyte_p;
5197 p->space_width = it->space_width;
5198 p->font_height = it->font_height;
5199 p->voffset = it->voffset;
5200 p->string_from_display_prop_p = it->string_from_display_prop_p;
5201 p->display_ellipsis_p = 0;
5202 ++it->sp;
5203 }
5204
5205
5206 /* Restore IT's settings from IT->stack. Called, for example, when no
5207 more overlay strings must be processed, and we return to delivering
5208 display elements from a buffer, or when the end of a string from a
5209 `display' property is reached and we return to delivering display
5210 elements from an overlay string, or from a buffer. */
5211
5212 static void
5213 pop_it (it)
5214 struct it *it;
5215 {
5216 struct iterator_stack_entry *p;
5217
5218 xassert (it->sp > 0);
5219 --it->sp;
5220 p = it->stack + it->sp;
5221 it->stop_charpos = p->stop_charpos;
5222 it->face_id = p->face_id;
5223 it->current = p->current;
5224 it->position = p->position;
5225 it->string = p->string;
5226 it->from_overlay = p->from_overlay;
5227 if (NILP (it->string))
5228 SET_TEXT_POS (it->current.string_pos, -1, -1);
5229 it->method = p->method;
5230 switch (it->method)
5231 {
5232 case GET_FROM_IMAGE:
5233 it->image_id = p->u.image.image_id;
5234 it->object = p->u.image.object;
5235 it->slice = p->u.image.slice;
5236 break;
5237 case GET_FROM_COMPOSITION:
5238 it->object = p->u.comp.object;
5239 it->c = p->u.comp.c;
5240 it->len = p->u.comp.len;
5241 it->cmp_id = p->u.comp.cmp_id;
5242 it->cmp_len = p->u.comp.cmp_len;
5243 break;
5244 case GET_FROM_STRETCH:
5245 it->object = p->u.comp.object;
5246 break;
5247 case GET_FROM_BUFFER:
5248 it->object = it->w->buffer;
5249 break;
5250 case GET_FROM_STRING:
5251 it->object = it->string;
5252 break;
5253 }
5254 it->end_charpos = p->end_charpos;
5255 it->string_nchars = p->string_nchars;
5256 it->area = p->area;
5257 it->multibyte_p = p->multibyte_p;
5258 it->space_width = p->space_width;
5259 it->font_height = p->font_height;
5260 it->voffset = p->voffset;
5261 it->string_from_display_prop_p = p->string_from_display_prop_p;
5262 }
5263
5264
5265 \f
5266 /***********************************************************************
5267 Moving over lines
5268 ***********************************************************************/
5269
5270 /* Set IT's current position to the previous line start. */
5271
5272 static void
5273 back_to_previous_line_start (it)
5274 struct it *it;
5275 {
5276 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5277 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5278 }
5279
5280
5281 /* Move IT to the next line start.
5282
5283 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5284 we skipped over part of the text (as opposed to moving the iterator
5285 continuously over the text). Otherwise, don't change the value
5286 of *SKIPPED_P.
5287
5288 Newlines may come from buffer text, overlay strings, or strings
5289 displayed via the `display' property. That's the reason we can't
5290 simply use find_next_newline_no_quit.
5291
5292 Note that this function may not skip over invisible text that is so
5293 because of text properties and immediately follows a newline. If
5294 it would, function reseat_at_next_visible_line_start, when called
5295 from set_iterator_to_next, would effectively make invisible
5296 characters following a newline part of the wrong glyph row, which
5297 leads to wrong cursor motion. */
5298
5299 static int
5300 forward_to_next_line_start (it, skipped_p)
5301 struct it *it;
5302 int *skipped_p;
5303 {
5304 int old_selective, newline_found_p, n;
5305 const int MAX_NEWLINE_DISTANCE = 500;
5306
5307 /* If already on a newline, just consume it to avoid unintended
5308 skipping over invisible text below. */
5309 if (it->what == IT_CHARACTER
5310 && it->c == '\n'
5311 && CHARPOS (it->position) == IT_CHARPOS (*it))
5312 {
5313 set_iterator_to_next (it, 0);
5314 it->c = 0;
5315 return 1;
5316 }
5317
5318 /* Don't handle selective display in the following. It's (a)
5319 unnecessary because it's done by the caller, and (b) leads to an
5320 infinite recursion because next_element_from_ellipsis indirectly
5321 calls this function. */
5322 old_selective = it->selective;
5323 it->selective = 0;
5324
5325 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5326 from buffer text. */
5327 for (n = newline_found_p = 0;
5328 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5329 n += STRINGP (it->string) ? 0 : 1)
5330 {
5331 if (!get_next_display_element (it))
5332 return 0;
5333 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5334 set_iterator_to_next (it, 0);
5335 }
5336
5337 /* If we didn't find a newline near enough, see if we can use a
5338 short-cut. */
5339 if (!newline_found_p)
5340 {
5341 int start = IT_CHARPOS (*it);
5342 int limit = find_next_newline_no_quit (start, 1);
5343 Lisp_Object pos;
5344
5345 xassert (!STRINGP (it->string));
5346
5347 /* If there isn't any `display' property in sight, and no
5348 overlays, we can just use the position of the newline in
5349 buffer text. */
5350 if (it->stop_charpos >= limit
5351 || ((pos = Fnext_single_property_change (make_number (start),
5352 Qdisplay,
5353 Qnil, make_number (limit)),
5354 NILP (pos))
5355 && next_overlay_change (start) == ZV))
5356 {
5357 IT_CHARPOS (*it) = limit;
5358 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5359 *skipped_p = newline_found_p = 1;
5360 }
5361 else
5362 {
5363 while (get_next_display_element (it)
5364 && !newline_found_p)
5365 {
5366 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5367 set_iterator_to_next (it, 0);
5368 }
5369 }
5370 }
5371
5372 it->selective = old_selective;
5373 return newline_found_p;
5374 }
5375
5376
5377 /* Set IT's current position to the previous visible line start. Skip
5378 invisible text that is so either due to text properties or due to
5379 selective display. Caution: this does not change IT->current_x and
5380 IT->hpos. */
5381
5382 static void
5383 back_to_previous_visible_line_start (it)
5384 struct it *it;
5385 {
5386 while (IT_CHARPOS (*it) > BEGV)
5387 {
5388 back_to_previous_line_start (it);
5389
5390 if (IT_CHARPOS (*it) <= BEGV)
5391 break;
5392
5393 /* If selective > 0, then lines indented more than that values
5394 are invisible. */
5395 if (it->selective > 0
5396 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5397 (double) it->selective)) /* iftc */
5398 continue;
5399
5400 /* Check the newline before point for invisibility. */
5401 {
5402 Lisp_Object prop;
5403 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5404 Qinvisible, it->window);
5405 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5406 continue;
5407 }
5408
5409 if (IT_CHARPOS (*it) <= BEGV)
5410 break;
5411
5412 {
5413 struct it it2;
5414 int pos;
5415 EMACS_INT beg, end;
5416 Lisp_Object val, overlay;
5417
5418 /* If newline is part of a composition, continue from start of composition */
5419 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5420 && beg < IT_CHARPOS (*it))
5421 goto replaced;
5422
5423 /* If newline is replaced by a display property, find start of overlay
5424 or interval and continue search from that point. */
5425 it2 = *it;
5426 pos = --IT_CHARPOS (it2);
5427 --IT_BYTEPOS (it2);
5428 it2.sp = 0;
5429 if (handle_display_prop (&it2) == HANDLED_RETURN
5430 && !NILP (val = get_char_property_and_overlay
5431 (make_number (pos), Qdisplay, Qnil, &overlay))
5432 && (OVERLAYP (overlay)
5433 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5434 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5435 goto replaced;
5436
5437 /* Newline is not replaced by anything -- so we are done. */
5438 break;
5439
5440 replaced:
5441 if (beg < BEGV)
5442 beg = BEGV;
5443 IT_CHARPOS (*it) = beg;
5444 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5445 }
5446 }
5447
5448 it->continuation_lines_width = 0;
5449
5450 xassert (IT_CHARPOS (*it) >= BEGV);
5451 xassert (IT_CHARPOS (*it) == BEGV
5452 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5453 CHECK_IT (it);
5454 }
5455
5456
5457 /* Reseat iterator IT at the previous visible line start. Skip
5458 invisible text that is so either due to text properties or due to
5459 selective display. At the end, update IT's overlay information,
5460 face information etc. */
5461
5462 void
5463 reseat_at_previous_visible_line_start (it)
5464 struct it *it;
5465 {
5466 back_to_previous_visible_line_start (it);
5467 reseat (it, it->current.pos, 1);
5468 CHECK_IT (it);
5469 }
5470
5471
5472 /* Reseat iterator IT on the next visible line start in the current
5473 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5474 preceding the line start. Skip over invisible text that is so
5475 because of selective display. Compute faces, overlays etc at the
5476 new position. Note that this function does not skip over text that
5477 is invisible because of text properties. */
5478
5479 static void
5480 reseat_at_next_visible_line_start (it, on_newline_p)
5481 struct it *it;
5482 int on_newline_p;
5483 {
5484 int newline_found_p, skipped_p = 0;
5485
5486 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5487
5488 /* Skip over lines that are invisible because they are indented
5489 more than the value of IT->selective. */
5490 if (it->selective > 0)
5491 while (IT_CHARPOS (*it) < ZV
5492 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5493 (double) it->selective)) /* iftc */
5494 {
5495 xassert (IT_BYTEPOS (*it) == BEGV
5496 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5497 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5498 }
5499
5500 /* Position on the newline if that's what's requested. */
5501 if (on_newline_p && newline_found_p)
5502 {
5503 if (STRINGP (it->string))
5504 {
5505 if (IT_STRING_CHARPOS (*it) > 0)
5506 {
5507 --IT_STRING_CHARPOS (*it);
5508 --IT_STRING_BYTEPOS (*it);
5509 }
5510 }
5511 else if (IT_CHARPOS (*it) > BEGV)
5512 {
5513 --IT_CHARPOS (*it);
5514 --IT_BYTEPOS (*it);
5515 reseat (it, it->current.pos, 0);
5516 }
5517 }
5518 else if (skipped_p)
5519 reseat (it, it->current.pos, 0);
5520
5521 CHECK_IT (it);
5522 }
5523
5524
5525 \f
5526 /***********************************************************************
5527 Changing an iterator's position
5528 ***********************************************************************/
5529
5530 /* Change IT's current position to POS in current_buffer. If FORCE_P
5531 is non-zero, always check for text properties at the new position.
5532 Otherwise, text properties are only looked up if POS >=
5533 IT->check_charpos of a property. */
5534
5535 static void
5536 reseat (it, pos, force_p)
5537 struct it *it;
5538 struct text_pos pos;
5539 int force_p;
5540 {
5541 int original_pos = IT_CHARPOS (*it);
5542
5543 reseat_1 (it, pos, 0);
5544
5545 /* Determine where to check text properties. Avoid doing it
5546 where possible because text property lookup is very expensive. */
5547 if (force_p
5548 || CHARPOS (pos) > it->stop_charpos
5549 || CHARPOS (pos) < original_pos)
5550 handle_stop (it);
5551
5552 CHECK_IT (it);
5553 }
5554
5555
5556 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5557 IT->stop_pos to POS, also. */
5558
5559 static void
5560 reseat_1 (it, pos, set_stop_p)
5561 struct it *it;
5562 struct text_pos pos;
5563 int set_stop_p;
5564 {
5565 /* Don't call this function when scanning a C string. */
5566 xassert (it->s == NULL);
5567
5568 /* POS must be a reasonable value. */
5569 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5570
5571 it->current.pos = it->position = pos;
5572 it->end_charpos = ZV;
5573 it->dpvec = NULL;
5574 it->current.dpvec_index = -1;
5575 it->current.overlay_string_index = -1;
5576 IT_STRING_CHARPOS (*it) = -1;
5577 IT_STRING_BYTEPOS (*it) = -1;
5578 it->string = Qnil;
5579 it->method = GET_FROM_BUFFER;
5580 it->object = it->w->buffer;
5581 it->area = TEXT_AREA;
5582 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5583 it->sp = 0;
5584 it->string_from_display_prop_p = 0;
5585 it->face_before_selective_p = 0;
5586
5587 if (set_stop_p)
5588 it->stop_charpos = CHARPOS (pos);
5589 }
5590
5591
5592 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5593 If S is non-null, it is a C string to iterate over. Otherwise,
5594 STRING gives a Lisp string to iterate over.
5595
5596 If PRECISION > 0, don't return more then PRECISION number of
5597 characters from the string.
5598
5599 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5600 characters have been returned. FIELD_WIDTH < 0 means an infinite
5601 field width.
5602
5603 MULTIBYTE = 0 means disable processing of multibyte characters,
5604 MULTIBYTE > 0 means enable it,
5605 MULTIBYTE < 0 means use IT->multibyte_p.
5606
5607 IT must be initialized via a prior call to init_iterator before
5608 calling this function. */
5609
5610 static void
5611 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5612 struct it *it;
5613 unsigned char *s;
5614 Lisp_Object string;
5615 int charpos;
5616 int precision, field_width, multibyte;
5617 {
5618 /* No region in strings. */
5619 it->region_beg_charpos = it->region_end_charpos = -1;
5620
5621 /* No text property checks performed by default, but see below. */
5622 it->stop_charpos = -1;
5623
5624 /* Set iterator position and end position. */
5625 bzero (&it->current, sizeof it->current);
5626 it->current.overlay_string_index = -1;
5627 it->current.dpvec_index = -1;
5628 xassert (charpos >= 0);
5629
5630 /* If STRING is specified, use its multibyteness, otherwise use the
5631 setting of MULTIBYTE, if specified. */
5632 if (multibyte >= 0)
5633 it->multibyte_p = multibyte > 0;
5634
5635 if (s == NULL)
5636 {
5637 xassert (STRINGP (string));
5638 it->string = string;
5639 it->s = NULL;
5640 it->end_charpos = it->string_nchars = SCHARS (string);
5641 it->method = GET_FROM_STRING;
5642 it->current.string_pos = string_pos (charpos, string);
5643 }
5644 else
5645 {
5646 it->s = s;
5647 it->string = Qnil;
5648
5649 /* Note that we use IT->current.pos, not it->current.string_pos,
5650 for displaying C strings. */
5651 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5652 if (it->multibyte_p)
5653 {
5654 it->current.pos = c_string_pos (charpos, s, 1);
5655 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5656 }
5657 else
5658 {
5659 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5660 it->end_charpos = it->string_nchars = strlen (s);
5661 }
5662
5663 it->method = GET_FROM_C_STRING;
5664 }
5665
5666 /* PRECISION > 0 means don't return more than PRECISION characters
5667 from the string. */
5668 if (precision > 0 && it->end_charpos - charpos > precision)
5669 it->end_charpos = it->string_nchars = charpos + precision;
5670
5671 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5672 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5673 FIELD_WIDTH < 0 means infinite field width. This is useful for
5674 padding with `-' at the end of a mode line. */
5675 if (field_width < 0)
5676 field_width = INFINITY;
5677 if (field_width > it->end_charpos - charpos)
5678 it->end_charpos = charpos + field_width;
5679
5680 /* Use the standard display table for displaying strings. */
5681 if (DISP_TABLE_P (Vstandard_display_table))
5682 it->dp = XCHAR_TABLE (Vstandard_display_table);
5683
5684 it->stop_charpos = charpos;
5685 CHECK_IT (it);
5686 }
5687
5688
5689 \f
5690 /***********************************************************************
5691 Iteration
5692 ***********************************************************************/
5693
5694 /* Map enum it_method value to corresponding next_element_from_* function. */
5695
5696 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5697 {
5698 next_element_from_buffer,
5699 next_element_from_display_vector,
5700 next_element_from_composition,
5701 next_element_from_string,
5702 next_element_from_c_string,
5703 next_element_from_image,
5704 next_element_from_stretch
5705 };
5706
5707 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5708
5709 /* Load IT's display element fields with information about the next
5710 display element from the current position of IT. Value is zero if
5711 end of buffer (or C string) is reached. */
5712
5713 static struct frame *last_escape_glyph_frame = NULL;
5714 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5715 static int last_escape_glyph_merged_face_id = 0;
5716
5717 int
5718 get_next_display_element (it)
5719 struct it *it;
5720 {
5721 /* Non-zero means that we found a display element. Zero means that
5722 we hit the end of what we iterate over. Performance note: the
5723 function pointer `method' used here turns out to be faster than
5724 using a sequence of if-statements. */
5725 int success_p;
5726
5727 get_next:
5728 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5729
5730 if (it->what == IT_CHARACTER)
5731 {
5732 /* Map via display table or translate control characters.
5733 IT->c, IT->len etc. have been set to the next character by
5734 the function call above. If we have a display table, and it
5735 contains an entry for IT->c, translate it. Don't do this if
5736 IT->c itself comes from a display table, otherwise we could
5737 end up in an infinite recursion. (An alternative could be to
5738 count the recursion depth of this function and signal an
5739 error when a certain maximum depth is reached.) Is it worth
5740 it? */
5741 if (success_p && it->dpvec == NULL)
5742 {
5743 Lisp_Object dv;
5744
5745 if (it->dp
5746 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5747 VECTORP (dv)))
5748 {
5749 struct Lisp_Vector *v = XVECTOR (dv);
5750
5751 /* Return the first character from the display table
5752 entry, if not empty. If empty, don't display the
5753 current character. */
5754 if (v->size)
5755 {
5756 it->dpvec_char_len = it->len;
5757 it->dpvec = v->contents;
5758 it->dpend = v->contents + v->size;
5759 it->current.dpvec_index = 0;
5760 it->dpvec_face_id = -1;
5761 it->saved_face_id = it->face_id;
5762 it->method = GET_FROM_DISPLAY_VECTOR;
5763 it->ellipsis_p = 0;
5764 }
5765 else
5766 {
5767 set_iterator_to_next (it, 0);
5768 }
5769 goto get_next;
5770 }
5771
5772 /* Translate control characters into `\003' or `^C' form.
5773 Control characters coming from a display table entry are
5774 currently not translated because we use IT->dpvec to hold
5775 the translation. This could easily be changed but I
5776 don't believe that it is worth doing.
5777
5778 If it->multibyte_p is nonzero, non-printable non-ASCII
5779 characters are also translated to octal form.
5780
5781 If it->multibyte_p is zero, eight-bit characters that
5782 don't have corresponding multibyte char code are also
5783 translated to octal form. */
5784 else if ((it->c < ' '
5785 ? (it->area != TEXT_AREA
5786 /* In mode line, treat \n, \t like other crl chars. */
5787 || (it->c != '\t'
5788 && it->glyph_row && it->glyph_row->mode_line_p)
5789 || (it->c != '\n' && it->c != '\t'))
5790 : (it->multibyte_p
5791 ? (!CHAR_PRINTABLE_P (it->c)
5792 || (!NILP (Vnobreak_char_display)
5793 && (it->c == 0xA0 /* NO-BREAK SPACE */
5794 || it->c == 0xAD /* SOFT HYPHEN */)))
5795 : (it->c >= 127
5796 && (! unibyte_display_via_language_environment
5797 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5798 {
5799 /* IT->c is a control character which must be displayed
5800 either as '\003' or as `^C' where the '\\' and '^'
5801 can be defined in the display table. Fill
5802 IT->ctl_chars with glyphs for what we have to
5803 display. Then, set IT->dpvec to these glyphs. */
5804 Lisp_Object gc;
5805 int ctl_len;
5806 int face_id, lface_id = 0 ;
5807 int escape_glyph;
5808
5809 /* Handle control characters with ^. */
5810
5811 if (it->c < 128 && it->ctl_arrow_p)
5812 {
5813 int g;
5814
5815 g = '^'; /* default glyph for Control */
5816 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5817 if (it->dp
5818 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5819 && GLYPH_CODE_CHAR_VALID_P (gc))
5820 {
5821 g = GLYPH_CODE_CHAR (gc);
5822 lface_id = GLYPH_CODE_FACE (gc);
5823 }
5824 if (lface_id)
5825 {
5826 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5827 }
5828 else if (it->f == last_escape_glyph_frame
5829 && it->face_id == last_escape_glyph_face_id)
5830 {
5831 face_id = last_escape_glyph_merged_face_id;
5832 }
5833 else
5834 {
5835 /* Merge the escape-glyph face into the current face. */
5836 face_id = merge_faces (it->f, Qescape_glyph, 0,
5837 it->face_id);
5838 last_escape_glyph_frame = it->f;
5839 last_escape_glyph_face_id = it->face_id;
5840 last_escape_glyph_merged_face_id = face_id;
5841 }
5842
5843 XSETINT (it->ctl_chars[0], g);
5844 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5845 ctl_len = 2;
5846 goto display_control;
5847 }
5848
5849 /* Handle non-break space in the mode where it only gets
5850 highlighting. */
5851
5852 if (EQ (Vnobreak_char_display, Qt)
5853 && it->c == 0xA0)
5854 {
5855 /* Merge the no-break-space face into the current face. */
5856 face_id = merge_faces (it->f, Qnobreak_space, 0,
5857 it->face_id);
5858
5859 it->c = ' ';
5860 XSETINT (it->ctl_chars[0], ' ');
5861 ctl_len = 1;
5862 goto display_control;
5863 }
5864
5865 /* Handle sequences that start with the "escape glyph". */
5866
5867 /* the default escape glyph is \. */
5868 escape_glyph = '\\';
5869
5870 if (it->dp
5871 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5872 && GLYPH_CODE_CHAR_VALID_P (gc))
5873 {
5874 escape_glyph = GLYPH_CODE_CHAR (gc);
5875 lface_id = GLYPH_CODE_FACE (gc);
5876 }
5877 if (lface_id)
5878 {
5879 /* The display table specified a face.
5880 Merge it into face_id and also into escape_glyph. */
5881 face_id = merge_faces (it->f, Qt, lface_id,
5882 it->face_id);
5883 }
5884 else if (it->f == last_escape_glyph_frame
5885 && it->face_id == last_escape_glyph_face_id)
5886 {
5887 face_id = last_escape_glyph_merged_face_id;
5888 }
5889 else
5890 {
5891 /* Merge the escape-glyph face into the current face. */
5892 face_id = merge_faces (it->f, Qescape_glyph, 0,
5893 it->face_id);
5894 last_escape_glyph_frame = it->f;
5895 last_escape_glyph_face_id = it->face_id;
5896 last_escape_glyph_merged_face_id = face_id;
5897 }
5898
5899 /* Handle soft hyphens in the mode where they only get
5900 highlighting. */
5901
5902 if (EQ (Vnobreak_char_display, Qt)
5903 && it->c == 0xAD)
5904 {
5905 it->c = '-';
5906 XSETINT (it->ctl_chars[0], '-');
5907 ctl_len = 1;
5908 goto display_control;
5909 }
5910
5911 /* Handle non-break space and soft hyphen
5912 with the escape glyph. */
5913
5914 if (it->c == 0xA0 || it->c == 0xAD)
5915 {
5916 XSETINT (it->ctl_chars[0], escape_glyph);
5917 it->c = (it->c == 0xA0 ? ' ' : '-');
5918 XSETINT (it->ctl_chars[1], it->c);
5919 ctl_len = 2;
5920 goto display_control;
5921 }
5922
5923 {
5924 unsigned char str[MAX_MULTIBYTE_LENGTH];
5925 int len;
5926 int i;
5927
5928 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5929 if (CHAR_BYTE8_P (it->c))
5930 {
5931 str[0] = CHAR_TO_BYTE8 (it->c);
5932 len = 1;
5933 }
5934 else if (it->c < 256)
5935 {
5936 str[0] = it->c;
5937 len = 1;
5938 }
5939 else
5940 {
5941 /* It's an invalid character, which shouldn't
5942 happen actually, but due to bugs it may
5943 happen. Let's print the char as is, there's
5944 not much meaningful we can do with it. */
5945 str[0] = it->c;
5946 str[1] = it->c >> 8;
5947 str[2] = it->c >> 16;
5948 str[3] = it->c >> 24;
5949 len = 4;
5950 }
5951
5952 for (i = 0; i < len; i++)
5953 {
5954 int g;
5955 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5956 /* Insert three more glyphs into IT->ctl_chars for
5957 the octal display of the character. */
5958 g = ((str[i] >> 6) & 7) + '0';
5959 XSETINT (it->ctl_chars[i * 4 + 1], g);
5960 g = ((str[i] >> 3) & 7) + '0';
5961 XSETINT (it->ctl_chars[i * 4 + 2], g);
5962 g = (str[i] & 7) + '0';
5963 XSETINT (it->ctl_chars[i * 4 + 3], g);
5964 }
5965 ctl_len = len * 4;
5966 }
5967
5968 display_control:
5969 /* Set up IT->dpvec and return first character from it. */
5970 it->dpvec_char_len = it->len;
5971 it->dpvec = it->ctl_chars;
5972 it->dpend = it->dpvec + ctl_len;
5973 it->current.dpvec_index = 0;
5974 it->dpvec_face_id = face_id;
5975 it->saved_face_id = it->face_id;
5976 it->method = GET_FROM_DISPLAY_VECTOR;
5977 it->ellipsis_p = 0;
5978 goto get_next;
5979 }
5980 }
5981 }
5982
5983 /* Adjust face id for a multibyte character. There are no multibyte
5984 character in unibyte text. */
5985 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5986 && it->multibyte_p
5987 && success_p
5988 && FRAME_WINDOW_P (it->f))
5989 {
5990 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5991 int pos = (it->s ? -1
5992 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5993 : IT_CHARPOS (*it));
5994
5995 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5996 }
5997
5998 /* Is this character the last one of a run of characters with
5999 box? If yes, set IT->end_of_box_run_p to 1. */
6000 if (it->face_box_p
6001 && it->s == NULL)
6002 {
6003 int face_id;
6004 struct face *face;
6005
6006 it->end_of_box_run_p
6007 = ((face_id = face_after_it_pos (it),
6008 face_id != it->face_id)
6009 && (face = FACE_FROM_ID (it->f, face_id),
6010 face->box == FACE_NO_BOX));
6011 }
6012
6013 /* Value is 0 if end of buffer or string reached. */
6014 return success_p;
6015 }
6016
6017
6018 /* Move IT to the next display element.
6019
6020 RESEAT_P non-zero means if called on a newline in buffer text,
6021 skip to the next visible line start.
6022
6023 Functions get_next_display_element and set_iterator_to_next are
6024 separate because I find this arrangement easier to handle than a
6025 get_next_display_element function that also increments IT's
6026 position. The way it is we can first look at an iterator's current
6027 display element, decide whether it fits on a line, and if it does,
6028 increment the iterator position. The other way around we probably
6029 would either need a flag indicating whether the iterator has to be
6030 incremented the next time, or we would have to implement a
6031 decrement position function which would not be easy to write. */
6032
6033 void
6034 set_iterator_to_next (it, reseat_p)
6035 struct it *it;
6036 int reseat_p;
6037 {
6038 /* Reset flags indicating start and end of a sequence of characters
6039 with box. Reset them at the start of this function because
6040 moving the iterator to a new position might set them. */
6041 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6042
6043 switch (it->method)
6044 {
6045 case GET_FROM_BUFFER:
6046 /* The current display element of IT is a character from
6047 current_buffer. Advance in the buffer, and maybe skip over
6048 invisible lines that are so because of selective display. */
6049 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6050 reseat_at_next_visible_line_start (it, 0);
6051 else
6052 {
6053 xassert (it->len != 0);
6054 IT_BYTEPOS (*it) += it->len;
6055 IT_CHARPOS (*it) += 1;
6056 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6057 }
6058 break;
6059
6060 case GET_FROM_COMPOSITION:
6061 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6062 xassert (it->sp > 0);
6063 pop_it (it);
6064 if (it->method == GET_FROM_STRING)
6065 {
6066 IT_STRING_BYTEPOS (*it) += it->len;
6067 IT_STRING_CHARPOS (*it) += it->cmp_len;
6068 goto consider_string_end;
6069 }
6070 else if (it->method == GET_FROM_BUFFER)
6071 {
6072 IT_BYTEPOS (*it) += it->len;
6073 IT_CHARPOS (*it) += it->cmp_len;
6074 }
6075 break;
6076
6077 case GET_FROM_C_STRING:
6078 /* Current display element of IT is from a C string. */
6079 IT_BYTEPOS (*it) += it->len;
6080 IT_CHARPOS (*it) += 1;
6081 break;
6082
6083 case GET_FROM_DISPLAY_VECTOR:
6084 /* Current display element of IT is from a display table entry.
6085 Advance in the display table definition. Reset it to null if
6086 end reached, and continue with characters from buffers/
6087 strings. */
6088 ++it->current.dpvec_index;
6089
6090 /* Restore face of the iterator to what they were before the
6091 display vector entry (these entries may contain faces). */
6092 it->face_id = it->saved_face_id;
6093
6094 if (it->dpvec + it->current.dpvec_index == it->dpend)
6095 {
6096 int recheck_faces = it->ellipsis_p;
6097
6098 if (it->s)
6099 it->method = GET_FROM_C_STRING;
6100 else if (STRINGP (it->string))
6101 it->method = GET_FROM_STRING;
6102 else
6103 {
6104 it->method = GET_FROM_BUFFER;
6105 it->object = it->w->buffer;
6106 }
6107
6108 it->dpvec = NULL;
6109 it->current.dpvec_index = -1;
6110
6111 /* Skip over characters which were displayed via IT->dpvec. */
6112 if (it->dpvec_char_len < 0)
6113 reseat_at_next_visible_line_start (it, 1);
6114 else if (it->dpvec_char_len > 0)
6115 {
6116 if (it->method == GET_FROM_STRING
6117 && it->n_overlay_strings > 0)
6118 it->ignore_overlay_strings_at_pos_p = 1;
6119 it->len = it->dpvec_char_len;
6120 set_iterator_to_next (it, reseat_p);
6121 }
6122
6123 /* Maybe recheck faces after display vector */
6124 if (recheck_faces)
6125 it->stop_charpos = IT_CHARPOS (*it);
6126 }
6127 break;
6128
6129 case GET_FROM_STRING:
6130 /* Current display element is a character from a Lisp string. */
6131 xassert (it->s == NULL && STRINGP (it->string));
6132 IT_STRING_BYTEPOS (*it) += it->len;
6133 IT_STRING_CHARPOS (*it) += 1;
6134
6135 consider_string_end:
6136
6137 if (it->current.overlay_string_index >= 0)
6138 {
6139 /* IT->string is an overlay string. Advance to the
6140 next, if there is one. */
6141 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6142 next_overlay_string (it);
6143 }
6144 else
6145 {
6146 /* IT->string is not an overlay string. If we reached
6147 its end, and there is something on IT->stack, proceed
6148 with what is on the stack. This can be either another
6149 string, this time an overlay string, or a buffer. */
6150 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6151 && it->sp > 0)
6152 {
6153 pop_it (it);
6154 if (it->method == GET_FROM_STRING)
6155 goto consider_string_end;
6156 }
6157 }
6158 break;
6159
6160 case GET_FROM_IMAGE:
6161 case GET_FROM_STRETCH:
6162 /* The position etc with which we have to proceed are on
6163 the stack. The position may be at the end of a string,
6164 if the `display' property takes up the whole string. */
6165 xassert (it->sp > 0);
6166 pop_it (it);
6167 if (it->method == GET_FROM_STRING)
6168 goto consider_string_end;
6169 break;
6170
6171 default:
6172 /* There are no other methods defined, so this should be a bug. */
6173 abort ();
6174 }
6175
6176 xassert (it->method != GET_FROM_STRING
6177 || (STRINGP (it->string)
6178 && IT_STRING_CHARPOS (*it) >= 0));
6179 }
6180
6181 /* Load IT's display element fields with information about the next
6182 display element which comes from a display table entry or from the
6183 result of translating a control character to one of the forms `^C'
6184 or `\003'.
6185
6186 IT->dpvec holds the glyphs to return as characters.
6187 IT->saved_face_id holds the face id before the display vector--
6188 it is restored into IT->face_idin set_iterator_to_next. */
6189
6190 static int
6191 next_element_from_display_vector (it)
6192 struct it *it;
6193 {
6194 Lisp_Object gc;
6195
6196 /* Precondition. */
6197 xassert (it->dpvec && it->current.dpvec_index >= 0);
6198
6199 it->face_id = it->saved_face_id;
6200
6201 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6202 That seemed totally bogus - so I changed it... */
6203
6204 if ((gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc))
6205 && GLYPH_CODE_CHAR_VALID_P (gc))
6206 {
6207 it->c = GLYPH_CODE_CHAR (gc);
6208 it->len = CHAR_BYTES (it->c);
6209
6210 /* The entry may contain a face id to use. Such a face id is
6211 the id of a Lisp face, not a realized face. A face id of
6212 zero means no face is specified. */
6213 if (it->dpvec_face_id >= 0)
6214 it->face_id = it->dpvec_face_id;
6215 else
6216 {
6217 int lface_id = GLYPH_CODE_FACE (gc);
6218 if (lface_id > 0)
6219 it->face_id = merge_faces (it->f, Qt, lface_id,
6220 it->saved_face_id);
6221 }
6222 }
6223 else
6224 /* Display table entry is invalid. Return a space. */
6225 it->c = ' ', it->len = 1;
6226
6227 /* Don't change position and object of the iterator here. They are
6228 still the values of the character that had this display table
6229 entry or was translated, and that's what we want. */
6230 it->what = IT_CHARACTER;
6231 return 1;
6232 }
6233
6234
6235 /* Load IT with the next display element from Lisp string IT->string.
6236 IT->current.string_pos is the current position within the string.
6237 If IT->current.overlay_string_index >= 0, the Lisp string is an
6238 overlay string. */
6239
6240 static int
6241 next_element_from_string (it)
6242 struct it *it;
6243 {
6244 struct text_pos position;
6245
6246 xassert (STRINGP (it->string));
6247 xassert (IT_STRING_CHARPOS (*it) >= 0);
6248 position = it->current.string_pos;
6249
6250 /* Time to check for invisible text? */
6251 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6252 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6253 {
6254 handle_stop (it);
6255
6256 /* Since a handler may have changed IT->method, we must
6257 recurse here. */
6258 return GET_NEXT_DISPLAY_ELEMENT (it);
6259 }
6260
6261 if (it->current.overlay_string_index >= 0)
6262 {
6263 /* Get the next character from an overlay string. In overlay
6264 strings, There is no field width or padding with spaces to
6265 do. */
6266 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6267 {
6268 it->what = IT_EOB;
6269 return 0;
6270 }
6271 else if (STRING_MULTIBYTE (it->string))
6272 {
6273 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6274 const unsigned char *s = (SDATA (it->string)
6275 + IT_STRING_BYTEPOS (*it));
6276 it->c = string_char_and_length (s, remaining, &it->len);
6277 }
6278 else
6279 {
6280 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6281 it->len = 1;
6282 }
6283 }
6284 else
6285 {
6286 /* Get the next character from a Lisp string that is not an
6287 overlay string. Such strings come from the mode line, for
6288 example. We may have to pad with spaces, or truncate the
6289 string. See also next_element_from_c_string. */
6290 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6291 {
6292 it->what = IT_EOB;
6293 return 0;
6294 }
6295 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6296 {
6297 /* Pad with spaces. */
6298 it->c = ' ', it->len = 1;
6299 CHARPOS (position) = BYTEPOS (position) = -1;
6300 }
6301 else if (STRING_MULTIBYTE (it->string))
6302 {
6303 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6304 const unsigned char *s = (SDATA (it->string)
6305 + IT_STRING_BYTEPOS (*it));
6306 it->c = string_char_and_length (s, maxlen, &it->len);
6307 }
6308 else
6309 {
6310 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6311 it->len = 1;
6312 }
6313 }
6314
6315 /* Record what we have and where it came from. */
6316 it->what = IT_CHARACTER;
6317 it->object = it->string;
6318 it->position = position;
6319 return 1;
6320 }
6321
6322
6323 /* Load IT with next display element from C string IT->s.
6324 IT->string_nchars is the maximum number of characters to return
6325 from the string. IT->end_charpos may be greater than
6326 IT->string_nchars when this function is called, in which case we
6327 may have to return padding spaces. Value is zero if end of string
6328 reached, including padding spaces. */
6329
6330 static int
6331 next_element_from_c_string (it)
6332 struct it *it;
6333 {
6334 int success_p = 1;
6335
6336 xassert (it->s);
6337 it->what = IT_CHARACTER;
6338 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6339 it->object = Qnil;
6340
6341 /* IT's position can be greater IT->string_nchars in case a field
6342 width or precision has been specified when the iterator was
6343 initialized. */
6344 if (IT_CHARPOS (*it) >= it->end_charpos)
6345 {
6346 /* End of the game. */
6347 it->what = IT_EOB;
6348 success_p = 0;
6349 }
6350 else if (IT_CHARPOS (*it) >= it->string_nchars)
6351 {
6352 /* Pad with spaces. */
6353 it->c = ' ', it->len = 1;
6354 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6355 }
6356 else if (it->multibyte_p)
6357 {
6358 /* Implementation note: The calls to strlen apparently aren't a
6359 performance problem because there is no noticeable performance
6360 difference between Emacs running in unibyte or multibyte mode. */
6361 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6362 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6363 maxlen, &it->len);
6364 }
6365 else
6366 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6367
6368 return success_p;
6369 }
6370
6371
6372 /* Set up IT to return characters from an ellipsis, if appropriate.
6373 The definition of the ellipsis glyphs may come from a display table
6374 entry. This function Fills IT with the first glyph from the
6375 ellipsis if an ellipsis is to be displayed. */
6376
6377 static int
6378 next_element_from_ellipsis (it)
6379 struct it *it;
6380 {
6381 if (it->selective_display_ellipsis_p)
6382 setup_for_ellipsis (it, it->len);
6383 else
6384 {
6385 /* The face at the current position may be different from the
6386 face we find after the invisible text. Remember what it
6387 was in IT->saved_face_id, and signal that it's there by
6388 setting face_before_selective_p. */
6389 it->saved_face_id = it->face_id;
6390 it->method = GET_FROM_BUFFER;
6391 it->object = it->w->buffer;
6392 reseat_at_next_visible_line_start (it, 1);
6393 it->face_before_selective_p = 1;
6394 }
6395
6396 return GET_NEXT_DISPLAY_ELEMENT (it);
6397 }
6398
6399
6400 /* Deliver an image display element. The iterator IT is already
6401 filled with image information (done in handle_display_prop). Value
6402 is always 1. */
6403
6404
6405 static int
6406 next_element_from_image (it)
6407 struct it *it;
6408 {
6409 it->what = IT_IMAGE;
6410 return 1;
6411 }
6412
6413
6414 /* Fill iterator IT with next display element from a stretch glyph
6415 property. IT->object is the value of the text property. Value is
6416 always 1. */
6417
6418 static int
6419 next_element_from_stretch (it)
6420 struct it *it;
6421 {
6422 it->what = IT_STRETCH;
6423 return 1;
6424 }
6425
6426
6427 /* Load IT with the next display element from current_buffer. Value
6428 is zero if end of buffer reached. IT->stop_charpos is the next
6429 position at which to stop and check for text properties or buffer
6430 end. */
6431
6432 static int
6433 next_element_from_buffer (it)
6434 struct it *it;
6435 {
6436 int success_p = 1;
6437
6438 /* Check this assumption, otherwise, we would never enter the
6439 if-statement, below. */
6440 xassert (IT_CHARPOS (*it) >= BEGV
6441 && IT_CHARPOS (*it) <= it->stop_charpos);
6442
6443 if (IT_CHARPOS (*it) >= it->stop_charpos)
6444 {
6445 if (IT_CHARPOS (*it) >= it->end_charpos)
6446 {
6447 int overlay_strings_follow_p;
6448
6449 /* End of the game, except when overlay strings follow that
6450 haven't been returned yet. */
6451 if (it->overlay_strings_at_end_processed_p)
6452 overlay_strings_follow_p = 0;
6453 else
6454 {
6455 it->overlay_strings_at_end_processed_p = 1;
6456 overlay_strings_follow_p = get_overlay_strings (it, 0);
6457 }
6458
6459 if (overlay_strings_follow_p)
6460 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6461 else
6462 {
6463 it->what = IT_EOB;
6464 it->position = it->current.pos;
6465 success_p = 0;
6466 }
6467 }
6468 else
6469 {
6470 handle_stop (it);
6471 return GET_NEXT_DISPLAY_ELEMENT (it);
6472 }
6473 }
6474 else
6475 {
6476 /* No face changes, overlays etc. in sight, so just return a
6477 character from current_buffer. */
6478 unsigned char *p;
6479
6480 /* Maybe run the redisplay end trigger hook. Performance note:
6481 This doesn't seem to cost measurable time. */
6482 if (it->redisplay_end_trigger_charpos
6483 && it->glyph_row
6484 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6485 run_redisplay_end_trigger_hook (it);
6486
6487 /* Get the next character, maybe multibyte. */
6488 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6489 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6490 {
6491 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6492 - IT_BYTEPOS (*it));
6493 it->c = string_char_and_length (p, maxlen, &it->len);
6494 }
6495 else
6496 it->c = *p, it->len = 1;
6497
6498 /* Record what we have and where it came from. */
6499 it->what = IT_CHARACTER;
6500 it->object = it->w->buffer;
6501 it->position = it->current.pos;
6502
6503 /* Normally we return the character found above, except when we
6504 really want to return an ellipsis for selective display. */
6505 if (it->selective)
6506 {
6507 if (it->c == '\n')
6508 {
6509 /* A value of selective > 0 means hide lines indented more
6510 than that number of columns. */
6511 if (it->selective > 0
6512 && IT_CHARPOS (*it) + 1 < ZV
6513 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6514 IT_BYTEPOS (*it) + 1,
6515 (double) it->selective)) /* iftc */
6516 {
6517 success_p = next_element_from_ellipsis (it);
6518 it->dpvec_char_len = -1;
6519 }
6520 }
6521 else if (it->c == '\r' && it->selective == -1)
6522 {
6523 /* A value of selective == -1 means that everything from the
6524 CR to the end of the line is invisible, with maybe an
6525 ellipsis displayed for it. */
6526 success_p = next_element_from_ellipsis (it);
6527 it->dpvec_char_len = -1;
6528 }
6529 }
6530 }
6531
6532 /* Value is zero if end of buffer reached. */
6533 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6534 return success_p;
6535 }
6536
6537
6538 /* Run the redisplay end trigger hook for IT. */
6539
6540 static void
6541 run_redisplay_end_trigger_hook (it)
6542 struct it *it;
6543 {
6544 Lisp_Object args[3];
6545
6546 /* IT->glyph_row should be non-null, i.e. we should be actually
6547 displaying something, or otherwise we should not run the hook. */
6548 xassert (it->glyph_row);
6549
6550 /* Set up hook arguments. */
6551 args[0] = Qredisplay_end_trigger_functions;
6552 args[1] = it->window;
6553 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6554 it->redisplay_end_trigger_charpos = 0;
6555
6556 /* Since we are *trying* to run these functions, don't try to run
6557 them again, even if they get an error. */
6558 it->w->redisplay_end_trigger = Qnil;
6559 Frun_hook_with_args (3, args);
6560
6561 /* Notice if it changed the face of the character we are on. */
6562 handle_face_prop (it);
6563 }
6564
6565
6566 /* Deliver a composition display element. The iterator IT is already
6567 filled with composition information (done in
6568 handle_composition_prop). Value is always 1. */
6569
6570 static int
6571 next_element_from_composition (it)
6572 struct it *it;
6573 {
6574 it->what = IT_COMPOSITION;
6575 it->position = (STRINGP (it->string)
6576 ? it->current.string_pos
6577 : it->current.pos);
6578 if (STRINGP (it->string))
6579 it->object = it->string;
6580 else
6581 it->object = it->w->buffer;
6582 return 1;
6583 }
6584
6585
6586 \f
6587 /***********************************************************************
6588 Moving an iterator without producing glyphs
6589 ***********************************************************************/
6590
6591 /* Check if iterator is at a position corresponding to a valid buffer
6592 position after some move_it_ call. */
6593
6594 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6595 ((it)->method == GET_FROM_STRING \
6596 ? IT_STRING_CHARPOS (*it) == 0 \
6597 : 1)
6598
6599
6600 /* Move iterator IT to a specified buffer or X position within one
6601 line on the display without producing glyphs.
6602
6603 OP should be a bit mask including some or all of these bits:
6604 MOVE_TO_X: Stop on reaching x-position TO_X.
6605 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6606 Regardless of OP's value, stop in reaching the end of the display line.
6607
6608 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6609 This means, in particular, that TO_X includes window's horizontal
6610 scroll amount.
6611
6612 The return value has several possible values that
6613 say what condition caused the scan to stop:
6614
6615 MOVE_POS_MATCH_OR_ZV
6616 - when TO_POS or ZV was reached.
6617
6618 MOVE_X_REACHED
6619 -when TO_X was reached before TO_POS or ZV were reached.
6620
6621 MOVE_LINE_CONTINUED
6622 - when we reached the end of the display area and the line must
6623 be continued.
6624
6625 MOVE_LINE_TRUNCATED
6626 - when we reached the end of the display area and the line is
6627 truncated.
6628
6629 MOVE_NEWLINE_OR_CR
6630 - when we stopped at a line end, i.e. a newline or a CR and selective
6631 display is on. */
6632
6633 static enum move_it_result
6634 move_it_in_display_line_to (struct it *it,
6635 EMACS_INT to_charpos, int to_x,
6636 enum move_operation_enum op)
6637 {
6638 enum move_it_result result = MOVE_UNDEFINED;
6639 struct glyph_row *saved_glyph_row;
6640
6641 /* Don't produce glyphs in produce_glyphs. */
6642 saved_glyph_row = it->glyph_row;
6643 it->glyph_row = NULL;
6644
6645 #define BUFFER_POS_REACHED_P() \
6646 ((op & MOVE_TO_POS) != 0 \
6647 && BUFFERP (it->object) \
6648 && IT_CHARPOS (*it) >= to_charpos \
6649 && (it->method == GET_FROM_BUFFER \
6650 || (it->method == GET_FROM_DISPLAY_VECTOR \
6651 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6652
6653
6654 while (1)
6655 {
6656 int x, i, ascent = 0, descent = 0;
6657
6658 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6659 if ((op & MOVE_TO_POS) != 0
6660 && BUFFERP (it->object)
6661 && it->method == GET_FROM_BUFFER
6662 && IT_CHARPOS (*it) > to_charpos)
6663 {
6664 result = MOVE_POS_MATCH_OR_ZV;
6665 break;
6666 }
6667
6668 /* Stop when ZV reached.
6669 We used to stop here when TO_CHARPOS reached as well, but that is
6670 too soon if this glyph does not fit on this line. So we handle it
6671 explicitly below. */
6672 if (!get_next_display_element (it)
6673 || (it->truncate_lines_p
6674 && BUFFER_POS_REACHED_P ()))
6675 {
6676 result = MOVE_POS_MATCH_OR_ZV;
6677 break;
6678 }
6679
6680 /* The call to produce_glyphs will get the metrics of the
6681 display element IT is loaded with. We record in x the
6682 x-position before this display element in case it does not
6683 fit on the line. */
6684 x = it->current_x;
6685
6686 /* Remember the line height so far in case the next element doesn't
6687 fit on the line. */
6688 if (!it->truncate_lines_p)
6689 {
6690 ascent = it->max_ascent;
6691 descent = it->max_descent;
6692 }
6693
6694 PRODUCE_GLYPHS (it);
6695
6696 if (it->area != TEXT_AREA)
6697 {
6698 set_iterator_to_next (it, 1);
6699 continue;
6700 }
6701
6702 /* The number of glyphs we get back in IT->nglyphs will normally
6703 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6704 character on a terminal frame, or (iii) a line end. For the
6705 second case, IT->nglyphs - 1 padding glyphs will be present
6706 (on X frames, there is only one glyph produced for a
6707 composite character.
6708
6709 The behavior implemented below means, for continuation lines,
6710 that as many spaces of a TAB as fit on the current line are
6711 displayed there. For terminal frames, as many glyphs of a
6712 multi-glyph character are displayed in the current line, too.
6713 This is what the old redisplay code did, and we keep it that
6714 way. Under X, the whole shape of a complex character must
6715 fit on the line or it will be completely displayed in the
6716 next line.
6717
6718 Note that both for tabs and padding glyphs, all glyphs have
6719 the same width. */
6720 if (it->nglyphs)
6721 {
6722 /* More than one glyph or glyph doesn't fit on line. All
6723 glyphs have the same width. */
6724 int single_glyph_width = it->pixel_width / it->nglyphs;
6725 int new_x;
6726 int x_before_this_char = x;
6727 int hpos_before_this_char = it->hpos;
6728
6729 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6730 {
6731 new_x = x + single_glyph_width;
6732
6733 /* We want to leave anything reaching TO_X to the caller. */
6734 if ((op & MOVE_TO_X) && new_x > to_x)
6735 {
6736 if (BUFFER_POS_REACHED_P ())
6737 goto buffer_pos_reached;
6738 it->current_x = x;
6739 result = MOVE_X_REACHED;
6740 break;
6741 }
6742 else if (/* Lines are continued. */
6743 !it->truncate_lines_p
6744 && (/* And glyph doesn't fit on the line. */
6745 new_x > it->last_visible_x
6746 /* Or it fits exactly and we're on a window
6747 system frame. */
6748 || (new_x == it->last_visible_x
6749 && FRAME_WINDOW_P (it->f))))
6750 {
6751 if (/* IT->hpos == 0 means the very first glyph
6752 doesn't fit on the line, e.g. a wide image. */
6753 it->hpos == 0
6754 || (new_x == it->last_visible_x
6755 && FRAME_WINDOW_P (it->f)))
6756 {
6757 ++it->hpos;
6758 it->current_x = new_x;
6759
6760 /* The character's last glyph just barely fits
6761 in this row. */
6762 if (i == it->nglyphs - 1)
6763 {
6764 /* If this is the destination position,
6765 return a position *before* it in this row,
6766 now that we know it fits in this row. */
6767 if (BUFFER_POS_REACHED_P ())
6768 {
6769 it->hpos = hpos_before_this_char;
6770 it->current_x = x_before_this_char;
6771 result = MOVE_POS_MATCH_OR_ZV;
6772 break;
6773 }
6774
6775 set_iterator_to_next (it, 1);
6776 #ifdef HAVE_WINDOW_SYSTEM
6777 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6778 {
6779 if (!get_next_display_element (it))
6780 {
6781 result = MOVE_POS_MATCH_OR_ZV;
6782 break;
6783 }
6784 if (BUFFER_POS_REACHED_P ())
6785 {
6786 if (ITERATOR_AT_END_OF_LINE_P (it))
6787 result = MOVE_POS_MATCH_OR_ZV;
6788 else
6789 result = MOVE_LINE_CONTINUED;
6790 break;
6791 }
6792 if (ITERATOR_AT_END_OF_LINE_P (it))
6793 {
6794 result = MOVE_NEWLINE_OR_CR;
6795 break;
6796 }
6797 }
6798 #endif /* HAVE_WINDOW_SYSTEM */
6799 }
6800 }
6801 else
6802 {
6803 it->current_x = x;
6804 it->max_ascent = ascent;
6805 it->max_descent = descent;
6806 }
6807
6808 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6809 IT_CHARPOS (*it)));
6810 result = MOVE_LINE_CONTINUED;
6811 break;
6812 }
6813 else if (BUFFER_POS_REACHED_P ())
6814 goto buffer_pos_reached;
6815 else if (new_x > it->first_visible_x)
6816 {
6817 /* Glyph is visible. Increment number of glyphs that
6818 would be displayed. */
6819 ++it->hpos;
6820 }
6821 else
6822 {
6823 /* Glyph is completely off the left margin of the display
6824 area. Nothing to do. */
6825 }
6826 }
6827
6828 if (result != MOVE_UNDEFINED)
6829 break;
6830 }
6831 else if (BUFFER_POS_REACHED_P ())
6832 {
6833 buffer_pos_reached:
6834 it->current_x = x;
6835 it->max_ascent = ascent;
6836 it->max_descent = descent;
6837 result = MOVE_POS_MATCH_OR_ZV;
6838 break;
6839 }
6840 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6841 {
6842 /* Stop when TO_X specified and reached. This check is
6843 necessary here because of lines consisting of a line end,
6844 only. The line end will not produce any glyphs and we
6845 would never get MOVE_X_REACHED. */
6846 xassert (it->nglyphs == 0);
6847 result = MOVE_X_REACHED;
6848 break;
6849 }
6850
6851 /* Is this a line end? If yes, we're done. */
6852 if (ITERATOR_AT_END_OF_LINE_P (it))
6853 {
6854 result = MOVE_NEWLINE_OR_CR;
6855 break;
6856 }
6857
6858 /* The current display element has been consumed. Advance
6859 to the next. */
6860 set_iterator_to_next (it, 1);
6861
6862 /* Stop if lines are truncated and IT's current x-position is
6863 past the right edge of the window now. */
6864 if (it->truncate_lines_p
6865 && it->current_x >= it->last_visible_x)
6866 {
6867 #ifdef HAVE_WINDOW_SYSTEM
6868 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6869 {
6870 if (!get_next_display_element (it)
6871 || BUFFER_POS_REACHED_P ())
6872 {
6873 result = MOVE_POS_MATCH_OR_ZV;
6874 break;
6875 }
6876 if (ITERATOR_AT_END_OF_LINE_P (it))
6877 {
6878 result = MOVE_NEWLINE_OR_CR;
6879 break;
6880 }
6881 }
6882 #endif /* HAVE_WINDOW_SYSTEM */
6883 result = MOVE_LINE_TRUNCATED;
6884 break;
6885 }
6886 }
6887
6888 #undef BUFFER_POS_REACHED_P
6889
6890 /* Restore the iterator settings altered at the beginning of this
6891 function. */
6892 it->glyph_row = saved_glyph_row;
6893 return result;
6894 }
6895
6896 /* For external use. */
6897 void
6898 move_it_in_display_line (struct it *it,
6899 EMACS_INT to_charpos, int to_x,
6900 enum move_operation_enum op)
6901 {
6902 move_it_in_display_line_to (it, to_charpos, to_x, op);
6903 }
6904
6905
6906 /* Move IT forward until it satisfies one or more of the criteria in
6907 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6908
6909 OP is a bit-mask that specifies where to stop, and in particular,
6910 which of those four position arguments makes a difference. See the
6911 description of enum move_operation_enum.
6912
6913 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6914 screen line, this function will set IT to the next position >
6915 TO_CHARPOS. */
6916
6917 void
6918 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6919 struct it *it;
6920 int to_charpos, to_x, to_y, to_vpos;
6921 int op;
6922 {
6923 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6924 int line_height;
6925 int reached = 0;
6926
6927 for (;;)
6928 {
6929 if (op & MOVE_TO_VPOS)
6930 {
6931 /* If no TO_CHARPOS and no TO_X specified, stop at the
6932 start of the line TO_VPOS. */
6933 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6934 {
6935 if (it->vpos == to_vpos)
6936 {
6937 reached = 1;
6938 break;
6939 }
6940 else
6941 skip = move_it_in_display_line_to (it, -1, -1, 0);
6942 }
6943 else
6944 {
6945 /* TO_VPOS >= 0 means stop at TO_X in the line at
6946 TO_VPOS, or at TO_POS, whichever comes first. */
6947 if (it->vpos == to_vpos)
6948 {
6949 reached = 2;
6950 break;
6951 }
6952
6953 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6954
6955 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6956 {
6957 reached = 3;
6958 break;
6959 }
6960 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6961 {
6962 /* We have reached TO_X but not in the line we want. */
6963 skip = move_it_in_display_line_to (it, to_charpos,
6964 -1, MOVE_TO_POS);
6965 if (skip == MOVE_POS_MATCH_OR_ZV)
6966 {
6967 reached = 4;
6968 break;
6969 }
6970 }
6971 }
6972 }
6973 else if (op & MOVE_TO_Y)
6974 {
6975 struct it it_backup;
6976
6977 /* TO_Y specified means stop at TO_X in the line containing
6978 TO_Y---or at TO_CHARPOS if this is reached first. The
6979 problem is that we can't really tell whether the line
6980 contains TO_Y before we have completely scanned it, and
6981 this may skip past TO_X. What we do is to first scan to
6982 TO_X.
6983
6984 If TO_X is not specified, use a TO_X of zero. The reason
6985 is to make the outcome of this function more predictable.
6986 If we didn't use TO_X == 0, we would stop at the end of
6987 the line which is probably not what a caller would expect
6988 to happen. */
6989 skip = move_it_in_display_line_to (it, to_charpos,
6990 ((op & MOVE_TO_X)
6991 ? to_x : 0),
6992 (MOVE_TO_X
6993 | (op & MOVE_TO_POS)));
6994
6995 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6996 if (skip == MOVE_POS_MATCH_OR_ZV)
6997 {
6998 reached = 5;
6999 break;
7000 }
7001
7002 /* If TO_X was reached, we would like to know whether TO_Y
7003 is in the line. This can only be said if we know the
7004 total line height which requires us to scan the rest of
7005 the line. */
7006 if (skip == MOVE_X_REACHED)
7007 {
7008 /* Wait! We can conclude that TO_Y is in the line if
7009 the already scanned glyphs make the line tall enough
7010 because further scanning doesn't make it shorter. */
7011 line_height = it->max_ascent + it->max_descent;
7012 if (to_y >= it->current_y
7013 && to_y < it->current_y + line_height)
7014 {
7015 reached = 6;
7016 break;
7017 }
7018 it_backup = *it;
7019 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7020 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7021 op & MOVE_TO_POS);
7022 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7023 }
7024
7025 /* Now, decide whether TO_Y is in this line. */
7026 line_height = it->max_ascent + it->max_descent;
7027 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7028
7029 if (to_y >= it->current_y
7030 && to_y < it->current_y + line_height)
7031 {
7032 if (skip == MOVE_X_REACHED)
7033 /* If TO_Y is in this line and TO_X was reached above,
7034 we scanned too far. We have to restore IT's settings
7035 to the ones before skipping. */
7036 *it = it_backup;
7037 reached = 6;
7038 }
7039 else if (skip == MOVE_X_REACHED)
7040 {
7041 skip = skip2;
7042 if (skip == MOVE_POS_MATCH_OR_ZV)
7043 reached = 7;
7044 }
7045
7046 if (reached)
7047 break;
7048 }
7049 else if (BUFFERP (it->object)
7050 && it->method == GET_FROM_BUFFER
7051 && IT_CHARPOS (*it) >= to_charpos)
7052 skip = MOVE_POS_MATCH_OR_ZV;
7053 else
7054 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7055
7056 switch (skip)
7057 {
7058 case MOVE_POS_MATCH_OR_ZV:
7059 reached = 8;
7060 goto out;
7061
7062 case MOVE_NEWLINE_OR_CR:
7063 set_iterator_to_next (it, 1);
7064 it->continuation_lines_width = 0;
7065 break;
7066
7067 case MOVE_LINE_TRUNCATED:
7068 it->continuation_lines_width = 0;
7069 reseat_at_next_visible_line_start (it, 0);
7070 if ((op & MOVE_TO_POS) != 0
7071 && IT_CHARPOS (*it) > to_charpos)
7072 {
7073 reached = 9;
7074 goto out;
7075 }
7076 break;
7077
7078 case MOVE_LINE_CONTINUED:
7079 /* For continued lines ending in a tab, some of the glyphs
7080 associated with the tab are displayed on the current
7081 line. Since it->current_x does not include these glyphs,
7082 we use it->last_visible_x instead. */
7083 it->continuation_lines_width +=
7084 (it->c == '\t') ? it->last_visible_x : it->current_x;
7085 break;
7086
7087 default:
7088 abort ();
7089 }
7090
7091 /* Reset/increment for the next run. */
7092 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7093 it->current_x = it->hpos = 0;
7094 it->current_y += it->max_ascent + it->max_descent;
7095 ++it->vpos;
7096 last_height = it->max_ascent + it->max_descent;
7097 last_max_ascent = it->max_ascent;
7098 it->max_ascent = it->max_descent = 0;
7099 }
7100
7101 out:
7102
7103 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7104 }
7105
7106
7107 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7108
7109 If DY > 0, move IT backward at least that many pixels. DY = 0
7110 means move IT backward to the preceding line start or BEGV. This
7111 function may move over more than DY pixels if IT->current_y - DY
7112 ends up in the middle of a line; in this case IT->current_y will be
7113 set to the top of the line moved to. */
7114
7115 void
7116 move_it_vertically_backward (it, dy)
7117 struct it *it;
7118 int dy;
7119 {
7120 int nlines, h;
7121 struct it it2, it3;
7122 int start_pos;
7123
7124 move_further_back:
7125 xassert (dy >= 0);
7126
7127 start_pos = IT_CHARPOS (*it);
7128
7129 /* Estimate how many newlines we must move back. */
7130 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7131
7132 /* Set the iterator's position that many lines back. */
7133 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7134 back_to_previous_visible_line_start (it);
7135
7136 /* Reseat the iterator here. When moving backward, we don't want
7137 reseat to skip forward over invisible text, set up the iterator
7138 to deliver from overlay strings at the new position etc. So,
7139 use reseat_1 here. */
7140 reseat_1 (it, it->current.pos, 1);
7141
7142 /* We are now surely at a line start. */
7143 it->current_x = it->hpos = 0;
7144 it->continuation_lines_width = 0;
7145
7146 /* Move forward and see what y-distance we moved. First move to the
7147 start of the next line so that we get its height. We need this
7148 height to be able to tell whether we reached the specified
7149 y-distance. */
7150 it2 = *it;
7151 it2.max_ascent = it2.max_descent = 0;
7152 do
7153 {
7154 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7155 MOVE_TO_POS | MOVE_TO_VPOS);
7156 }
7157 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7158 xassert (IT_CHARPOS (*it) >= BEGV);
7159 it3 = it2;
7160
7161 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7162 xassert (IT_CHARPOS (*it) >= BEGV);
7163 /* H is the actual vertical distance from the position in *IT
7164 and the starting position. */
7165 h = it2.current_y - it->current_y;
7166 /* NLINES is the distance in number of lines. */
7167 nlines = it2.vpos - it->vpos;
7168
7169 /* Correct IT's y and vpos position
7170 so that they are relative to the starting point. */
7171 it->vpos -= nlines;
7172 it->current_y -= h;
7173
7174 if (dy == 0)
7175 {
7176 /* DY == 0 means move to the start of the screen line. The
7177 value of nlines is > 0 if continuation lines were involved. */
7178 if (nlines > 0)
7179 move_it_by_lines (it, nlines, 1);
7180 #if 0
7181 /* I think this assert is bogus if buffer contains
7182 invisible text or images. KFS. */
7183 xassert (IT_CHARPOS (*it) <= start_pos);
7184 #endif
7185 }
7186 else
7187 {
7188 /* The y-position we try to reach, relative to *IT.
7189 Note that H has been subtracted in front of the if-statement. */
7190 int target_y = it->current_y + h - dy;
7191 int y0 = it3.current_y;
7192 int y1 = line_bottom_y (&it3);
7193 int line_height = y1 - y0;
7194
7195 /* If we did not reach target_y, try to move further backward if
7196 we can. If we moved too far backward, try to move forward. */
7197 if (target_y < it->current_y
7198 /* This is heuristic. In a window that's 3 lines high, with
7199 a line height of 13 pixels each, recentering with point
7200 on the bottom line will try to move -39/2 = 19 pixels
7201 backward. Try to avoid moving into the first line. */
7202 && (it->current_y - target_y
7203 > min (window_box_height (it->w), line_height * 2 / 3))
7204 && IT_CHARPOS (*it) > BEGV)
7205 {
7206 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7207 target_y - it->current_y));
7208 dy = it->current_y - target_y;
7209 goto move_further_back;
7210 }
7211 else if (target_y >= it->current_y + line_height
7212 && IT_CHARPOS (*it) < ZV)
7213 {
7214 /* Should move forward by at least one line, maybe more.
7215
7216 Note: Calling move_it_by_lines can be expensive on
7217 terminal frames, where compute_motion is used (via
7218 vmotion) to do the job, when there are very long lines
7219 and truncate-lines is nil. That's the reason for
7220 treating terminal frames specially here. */
7221
7222 if (!FRAME_WINDOW_P (it->f))
7223 move_it_vertically (it, target_y - (it->current_y + line_height));
7224 else
7225 {
7226 do
7227 {
7228 move_it_by_lines (it, 1, 1);
7229 }
7230 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7231 }
7232
7233 #if 0
7234 /* I think this assert is bogus if buffer contains
7235 invisible text or images. KFS. */
7236 xassert (IT_CHARPOS (*it) >= BEGV);
7237 #endif
7238 }
7239 }
7240 }
7241
7242
7243 /* Move IT by a specified amount of pixel lines DY. DY negative means
7244 move backwards. DY = 0 means move to start of screen line. At the
7245 end, IT will be on the start of a screen line. */
7246
7247 void
7248 move_it_vertically (it, dy)
7249 struct it *it;
7250 int dy;
7251 {
7252 if (dy <= 0)
7253 move_it_vertically_backward (it, -dy);
7254 else
7255 {
7256 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7257 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7258 MOVE_TO_POS | MOVE_TO_Y);
7259 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7260
7261 /* If buffer ends in ZV without a newline, move to the start of
7262 the line to satisfy the post-condition. */
7263 if (IT_CHARPOS (*it) == ZV
7264 && ZV > BEGV
7265 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7266 move_it_by_lines (it, 0, 0);
7267 }
7268 }
7269
7270
7271 /* Move iterator IT past the end of the text line it is in. */
7272
7273 void
7274 move_it_past_eol (it)
7275 struct it *it;
7276 {
7277 enum move_it_result rc;
7278
7279 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7280 if (rc == MOVE_NEWLINE_OR_CR)
7281 set_iterator_to_next (it, 0);
7282 }
7283
7284
7285 #if 0 /* Currently not used. */
7286
7287 /* Return non-zero if some text between buffer positions START_CHARPOS
7288 and END_CHARPOS is invisible. IT->window is the window for text
7289 property lookup. */
7290
7291 static int
7292 invisible_text_between_p (it, start_charpos, end_charpos)
7293 struct it *it;
7294 int start_charpos, end_charpos;
7295 {
7296 Lisp_Object prop, limit;
7297 int invisible_found_p;
7298
7299 xassert (it != NULL && start_charpos <= end_charpos);
7300
7301 /* Is text at START invisible? */
7302 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7303 it->window);
7304 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7305 invisible_found_p = 1;
7306 else
7307 {
7308 limit = Fnext_single_char_property_change (make_number (start_charpos),
7309 Qinvisible, Qnil,
7310 make_number (end_charpos));
7311 invisible_found_p = XFASTINT (limit) < end_charpos;
7312 }
7313
7314 return invisible_found_p;
7315 }
7316
7317 #endif /* 0 */
7318
7319
7320 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7321 negative means move up. DVPOS == 0 means move to the start of the
7322 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7323 NEED_Y_P is zero, IT->current_y will be left unchanged.
7324
7325 Further optimization ideas: If we would know that IT->f doesn't use
7326 a face with proportional font, we could be faster for
7327 truncate-lines nil. */
7328
7329 void
7330 move_it_by_lines (it, dvpos, need_y_p)
7331 struct it *it;
7332 int dvpos, need_y_p;
7333 {
7334 struct position pos;
7335
7336 /* The commented-out optimization uses vmotion on terminals. This
7337 gives bad results, because elements like it->what, on which
7338 callers such as pos_visible_p rely, aren't updated. */
7339 /* if (!FRAME_WINDOW_P (it->f))
7340 {
7341 struct text_pos textpos;
7342
7343 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7344 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7345 reseat (it, textpos, 1);
7346 it->vpos += pos.vpos;
7347 it->current_y += pos.vpos;
7348 }
7349 else */
7350
7351 if (dvpos == 0)
7352 {
7353 /* DVPOS == 0 means move to the start of the screen line. */
7354 move_it_vertically_backward (it, 0);
7355 xassert (it->current_x == 0 && it->hpos == 0);
7356 /* Let next call to line_bottom_y calculate real line height */
7357 last_height = 0;
7358 }
7359 else if (dvpos > 0)
7360 {
7361 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7362 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7363 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7364 }
7365 else
7366 {
7367 struct it it2;
7368 int start_charpos, i;
7369
7370 /* Start at the beginning of the screen line containing IT's
7371 position. This may actually move vertically backwards,
7372 in case of overlays, so adjust dvpos accordingly. */
7373 dvpos += it->vpos;
7374 move_it_vertically_backward (it, 0);
7375 dvpos -= it->vpos;
7376
7377 /* Go back -DVPOS visible lines and reseat the iterator there. */
7378 start_charpos = IT_CHARPOS (*it);
7379 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7380 back_to_previous_visible_line_start (it);
7381 reseat (it, it->current.pos, 1);
7382
7383 /* Move further back if we end up in a string or an image. */
7384 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7385 {
7386 /* First try to move to start of display line. */
7387 dvpos += it->vpos;
7388 move_it_vertically_backward (it, 0);
7389 dvpos -= it->vpos;
7390 if (IT_POS_VALID_AFTER_MOVE_P (it))
7391 break;
7392 /* If start of line is still in string or image,
7393 move further back. */
7394 back_to_previous_visible_line_start (it);
7395 reseat (it, it->current.pos, 1);
7396 dvpos--;
7397 }
7398
7399 it->current_x = it->hpos = 0;
7400
7401 /* Above call may have moved too far if continuation lines
7402 are involved. Scan forward and see if it did. */
7403 it2 = *it;
7404 it2.vpos = it2.current_y = 0;
7405 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7406 it->vpos -= it2.vpos;
7407 it->current_y -= it2.current_y;
7408 it->current_x = it->hpos = 0;
7409
7410 /* If we moved too far back, move IT some lines forward. */
7411 if (it2.vpos > -dvpos)
7412 {
7413 int delta = it2.vpos + dvpos;
7414 it2 = *it;
7415 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7416 /* Move back again if we got too far ahead. */
7417 if (IT_CHARPOS (*it) >= start_charpos)
7418 *it = it2;
7419 }
7420 }
7421 }
7422
7423 /* Return 1 if IT points into the middle of a display vector. */
7424
7425 int
7426 in_display_vector_p (it)
7427 struct it *it;
7428 {
7429 return (it->method == GET_FROM_DISPLAY_VECTOR
7430 && it->current.dpvec_index > 0
7431 && it->dpvec + it->current.dpvec_index != it->dpend);
7432 }
7433
7434 \f
7435 /***********************************************************************
7436 Messages
7437 ***********************************************************************/
7438
7439
7440 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7441 to *Messages*. */
7442
7443 void
7444 add_to_log (format, arg1, arg2)
7445 char *format;
7446 Lisp_Object arg1, arg2;
7447 {
7448 Lisp_Object args[3];
7449 Lisp_Object msg, fmt;
7450 char *buffer;
7451 int len;
7452 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7453 USE_SAFE_ALLOCA;
7454
7455 /* Do nothing if called asynchronously. Inserting text into
7456 a buffer may call after-change-functions and alike and
7457 that would means running Lisp asynchronously. */
7458 if (handling_signal)
7459 return;
7460
7461 fmt = msg = Qnil;
7462 GCPRO4 (fmt, msg, arg1, arg2);
7463
7464 args[0] = fmt = build_string (format);
7465 args[1] = arg1;
7466 args[2] = arg2;
7467 msg = Fformat (3, args);
7468
7469 len = SBYTES (msg) + 1;
7470 SAFE_ALLOCA (buffer, char *, len);
7471 bcopy (SDATA (msg), buffer, len);
7472
7473 message_dolog (buffer, len - 1, 1, 0);
7474 SAFE_FREE ();
7475
7476 UNGCPRO;
7477 }
7478
7479
7480 /* Output a newline in the *Messages* buffer if "needs" one. */
7481
7482 void
7483 message_log_maybe_newline ()
7484 {
7485 if (message_log_need_newline)
7486 message_dolog ("", 0, 1, 0);
7487 }
7488
7489
7490 /* Add a string M of length NBYTES to the message log, optionally
7491 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7492 nonzero, means interpret the contents of M as multibyte. This
7493 function calls low-level routines in order to bypass text property
7494 hooks, etc. which might not be safe to run.
7495
7496 This may GC (insert may run before/after change hooks),
7497 so the buffer M must NOT point to a Lisp string. */
7498
7499 void
7500 message_dolog (m, nbytes, nlflag, multibyte)
7501 const char *m;
7502 int nbytes, nlflag, multibyte;
7503 {
7504 if (!NILP (Vmemory_full))
7505 return;
7506
7507 if (!NILP (Vmessage_log_max))
7508 {
7509 struct buffer *oldbuf;
7510 Lisp_Object oldpoint, oldbegv, oldzv;
7511 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7512 int point_at_end = 0;
7513 int zv_at_end = 0;
7514 Lisp_Object old_deactivate_mark, tem;
7515 struct gcpro gcpro1;
7516
7517 old_deactivate_mark = Vdeactivate_mark;
7518 oldbuf = current_buffer;
7519 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7520 current_buffer->undo_list = Qt;
7521
7522 oldpoint = message_dolog_marker1;
7523 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7524 oldbegv = message_dolog_marker2;
7525 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7526 oldzv = message_dolog_marker3;
7527 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7528 GCPRO1 (old_deactivate_mark);
7529
7530 if (PT == Z)
7531 point_at_end = 1;
7532 if (ZV == Z)
7533 zv_at_end = 1;
7534
7535 BEGV = BEG;
7536 BEGV_BYTE = BEG_BYTE;
7537 ZV = Z;
7538 ZV_BYTE = Z_BYTE;
7539 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7540
7541 /* Insert the string--maybe converting multibyte to single byte
7542 or vice versa, so that all the text fits the buffer. */
7543 if (multibyte
7544 && NILP (current_buffer->enable_multibyte_characters))
7545 {
7546 int i, c, char_bytes;
7547 unsigned char work[1];
7548
7549 /* Convert a multibyte string to single-byte
7550 for the *Message* buffer. */
7551 for (i = 0; i < nbytes; i += char_bytes)
7552 {
7553 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7554 work[0] = (ASCII_CHAR_P (c)
7555 ? c
7556 : multibyte_char_to_unibyte (c, Qnil));
7557 insert_1_both (work, 1, 1, 1, 0, 0);
7558 }
7559 }
7560 else if (! multibyte
7561 && ! NILP (current_buffer->enable_multibyte_characters))
7562 {
7563 int i, c, char_bytes;
7564 unsigned char *msg = (unsigned char *) m;
7565 unsigned char str[MAX_MULTIBYTE_LENGTH];
7566 /* Convert a single-byte string to multibyte
7567 for the *Message* buffer. */
7568 for (i = 0; i < nbytes; i++)
7569 {
7570 c = msg[i];
7571 c = unibyte_char_to_multibyte (c);
7572 char_bytes = CHAR_STRING (c, str);
7573 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7574 }
7575 }
7576 else if (nbytes)
7577 insert_1 (m, nbytes, 1, 0, 0);
7578
7579 if (nlflag)
7580 {
7581 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7582 insert_1 ("\n", 1, 1, 0, 0);
7583
7584 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7585 this_bol = PT;
7586 this_bol_byte = PT_BYTE;
7587
7588 /* See if this line duplicates the previous one.
7589 If so, combine duplicates. */
7590 if (this_bol > BEG)
7591 {
7592 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7593 prev_bol = PT;
7594 prev_bol_byte = PT_BYTE;
7595
7596 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7597 this_bol, this_bol_byte);
7598 if (dup)
7599 {
7600 del_range_both (prev_bol, prev_bol_byte,
7601 this_bol, this_bol_byte, 0);
7602 if (dup > 1)
7603 {
7604 char dupstr[40];
7605 int duplen;
7606
7607 /* If you change this format, don't forget to also
7608 change message_log_check_duplicate. */
7609 sprintf (dupstr, " [%d times]", dup);
7610 duplen = strlen (dupstr);
7611 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7612 insert_1 (dupstr, duplen, 1, 0, 1);
7613 }
7614 }
7615 }
7616
7617 /* If we have more than the desired maximum number of lines
7618 in the *Messages* buffer now, delete the oldest ones.
7619 This is safe because we don't have undo in this buffer. */
7620
7621 if (NATNUMP (Vmessage_log_max))
7622 {
7623 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7624 -XFASTINT (Vmessage_log_max) - 1, 0);
7625 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7626 }
7627 }
7628 BEGV = XMARKER (oldbegv)->charpos;
7629 BEGV_BYTE = marker_byte_position (oldbegv);
7630
7631 if (zv_at_end)
7632 {
7633 ZV = Z;
7634 ZV_BYTE = Z_BYTE;
7635 }
7636 else
7637 {
7638 ZV = XMARKER (oldzv)->charpos;
7639 ZV_BYTE = marker_byte_position (oldzv);
7640 }
7641
7642 if (point_at_end)
7643 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7644 else
7645 /* We can't do Fgoto_char (oldpoint) because it will run some
7646 Lisp code. */
7647 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7648 XMARKER (oldpoint)->bytepos);
7649
7650 UNGCPRO;
7651 unchain_marker (XMARKER (oldpoint));
7652 unchain_marker (XMARKER (oldbegv));
7653 unchain_marker (XMARKER (oldzv));
7654
7655 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7656 set_buffer_internal (oldbuf);
7657 if (NILP (tem))
7658 windows_or_buffers_changed = old_windows_or_buffers_changed;
7659 message_log_need_newline = !nlflag;
7660 Vdeactivate_mark = old_deactivate_mark;
7661 }
7662 }
7663
7664
7665 /* We are at the end of the buffer after just having inserted a newline.
7666 (Note: We depend on the fact we won't be crossing the gap.)
7667 Check to see if the most recent message looks a lot like the previous one.
7668 Return 0 if different, 1 if the new one should just replace it, or a
7669 value N > 1 if we should also append " [N times]". */
7670
7671 static int
7672 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7673 int prev_bol, this_bol;
7674 int prev_bol_byte, this_bol_byte;
7675 {
7676 int i;
7677 int len = Z_BYTE - 1 - this_bol_byte;
7678 int seen_dots = 0;
7679 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7680 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7681
7682 for (i = 0; i < len; i++)
7683 {
7684 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7685 seen_dots = 1;
7686 if (p1[i] != p2[i])
7687 return seen_dots;
7688 }
7689 p1 += len;
7690 if (*p1 == '\n')
7691 return 2;
7692 if (*p1++ == ' ' && *p1++ == '[')
7693 {
7694 int n = 0;
7695 while (*p1 >= '0' && *p1 <= '9')
7696 n = n * 10 + *p1++ - '0';
7697 if (strncmp (p1, " times]\n", 8) == 0)
7698 return n+1;
7699 }
7700 return 0;
7701 }
7702 \f
7703
7704 /* Display an echo area message M with a specified length of NBYTES
7705 bytes. The string may include null characters. If M is 0, clear
7706 out any existing message, and let the mini-buffer text show
7707 through.
7708
7709 This may GC, so the buffer M must NOT point to a Lisp string. */
7710
7711 void
7712 message2 (m, nbytes, multibyte)
7713 const char *m;
7714 int nbytes;
7715 int multibyte;
7716 {
7717 /* First flush out any partial line written with print. */
7718 message_log_maybe_newline ();
7719 if (m)
7720 message_dolog (m, nbytes, 1, multibyte);
7721 message2_nolog (m, nbytes, multibyte);
7722 }
7723
7724
7725 /* The non-logging counterpart of message2. */
7726
7727 void
7728 message2_nolog (m, nbytes, multibyte)
7729 const char *m;
7730 int nbytes, multibyte;
7731 {
7732 struct frame *sf = SELECTED_FRAME ();
7733 message_enable_multibyte = multibyte;
7734
7735 if (noninteractive)
7736 {
7737 if (noninteractive_need_newline)
7738 putc ('\n', stderr);
7739 noninteractive_need_newline = 0;
7740 if (m)
7741 fwrite (m, nbytes, 1, stderr);
7742 if (cursor_in_echo_area == 0)
7743 fprintf (stderr, "\n");
7744 fflush (stderr);
7745 }
7746 /* A null message buffer means that the frame hasn't really been
7747 initialized yet. Error messages get reported properly by
7748 cmd_error, so this must be just an informative message; toss it. */
7749 else if (INTERACTIVE
7750 && sf->glyphs_initialized_p
7751 && FRAME_MESSAGE_BUF (sf))
7752 {
7753 Lisp_Object mini_window;
7754 struct frame *f;
7755
7756 /* Get the frame containing the mini-buffer
7757 that the selected frame is using. */
7758 mini_window = FRAME_MINIBUF_WINDOW (sf);
7759 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7760
7761 FRAME_SAMPLE_VISIBILITY (f);
7762 if (FRAME_VISIBLE_P (sf)
7763 && ! FRAME_VISIBLE_P (f))
7764 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7765
7766 if (m)
7767 {
7768 set_message (m, Qnil, nbytes, multibyte);
7769 if (minibuffer_auto_raise)
7770 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7771 }
7772 else
7773 clear_message (1, 1);
7774
7775 do_pending_window_change (0);
7776 echo_area_display (1);
7777 do_pending_window_change (0);
7778 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7779 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7780 }
7781 }
7782
7783
7784 /* Display an echo area message M with a specified length of NBYTES
7785 bytes. The string may include null characters. If M is not a
7786 string, clear out any existing message, and let the mini-buffer
7787 text show through.
7788
7789 This function cancels echoing. */
7790
7791 void
7792 message3 (m, nbytes, multibyte)
7793 Lisp_Object m;
7794 int nbytes;
7795 int multibyte;
7796 {
7797 struct gcpro gcpro1;
7798
7799 GCPRO1 (m);
7800 clear_message (1,1);
7801 cancel_echoing ();
7802
7803 /* First flush out any partial line written with print. */
7804 message_log_maybe_newline ();
7805 if (STRINGP (m))
7806 {
7807 char *buffer;
7808 USE_SAFE_ALLOCA;
7809
7810 SAFE_ALLOCA (buffer, char *, nbytes);
7811 bcopy (SDATA (m), buffer, nbytes);
7812 message_dolog (buffer, nbytes, 1, multibyte);
7813 SAFE_FREE ();
7814 }
7815 message3_nolog (m, nbytes, multibyte);
7816
7817 UNGCPRO;
7818 }
7819
7820
7821 /* The non-logging version of message3.
7822 This does not cancel echoing, because it is used for echoing.
7823 Perhaps we need to make a separate function for echoing
7824 and make this cancel echoing. */
7825
7826 void
7827 message3_nolog (m, nbytes, multibyte)
7828 Lisp_Object m;
7829 int nbytes, multibyte;
7830 {
7831 struct frame *sf = SELECTED_FRAME ();
7832 message_enable_multibyte = multibyte;
7833
7834 if (noninteractive)
7835 {
7836 if (noninteractive_need_newline)
7837 putc ('\n', stderr);
7838 noninteractive_need_newline = 0;
7839 if (STRINGP (m))
7840 fwrite (SDATA (m), nbytes, 1, stderr);
7841 if (cursor_in_echo_area == 0)
7842 fprintf (stderr, "\n");
7843 fflush (stderr);
7844 }
7845 /* A null message buffer means that the frame hasn't really been
7846 initialized yet. Error messages get reported properly by
7847 cmd_error, so this must be just an informative message; toss it. */
7848 else if (INTERACTIVE
7849 && sf->glyphs_initialized_p
7850 && FRAME_MESSAGE_BUF (sf))
7851 {
7852 Lisp_Object mini_window;
7853 Lisp_Object frame;
7854 struct frame *f;
7855
7856 /* Get the frame containing the mini-buffer
7857 that the selected frame is using. */
7858 mini_window = FRAME_MINIBUF_WINDOW (sf);
7859 frame = XWINDOW (mini_window)->frame;
7860 f = XFRAME (frame);
7861
7862 FRAME_SAMPLE_VISIBILITY (f);
7863 if (FRAME_VISIBLE_P (sf)
7864 && !FRAME_VISIBLE_P (f))
7865 Fmake_frame_visible (frame);
7866
7867 if (STRINGP (m) && SCHARS (m) > 0)
7868 {
7869 set_message (NULL, m, nbytes, multibyte);
7870 if (minibuffer_auto_raise)
7871 Fraise_frame (frame);
7872 /* Assume we are not echoing.
7873 (If we are, echo_now will override this.) */
7874 echo_message_buffer = Qnil;
7875 }
7876 else
7877 clear_message (1, 1);
7878
7879 do_pending_window_change (0);
7880 echo_area_display (1);
7881 do_pending_window_change (0);
7882 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7883 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7884 }
7885 }
7886
7887
7888 /* Display a null-terminated echo area message M. If M is 0, clear
7889 out any existing message, and let the mini-buffer text show through.
7890
7891 The buffer M must continue to exist until after the echo area gets
7892 cleared or some other message gets displayed there. Do not pass
7893 text that is stored in a Lisp string. Do not pass text in a buffer
7894 that was alloca'd. */
7895
7896 void
7897 message1 (m)
7898 char *m;
7899 {
7900 message2 (m, (m ? strlen (m) : 0), 0);
7901 }
7902
7903
7904 /* The non-logging counterpart of message1. */
7905
7906 void
7907 message1_nolog (m)
7908 char *m;
7909 {
7910 message2_nolog (m, (m ? strlen (m) : 0), 0);
7911 }
7912
7913 /* Display a message M which contains a single %s
7914 which gets replaced with STRING. */
7915
7916 void
7917 message_with_string (m, string, log)
7918 char *m;
7919 Lisp_Object string;
7920 int log;
7921 {
7922 CHECK_STRING (string);
7923
7924 if (noninteractive)
7925 {
7926 if (m)
7927 {
7928 if (noninteractive_need_newline)
7929 putc ('\n', stderr);
7930 noninteractive_need_newline = 0;
7931 fprintf (stderr, m, SDATA (string));
7932 if (cursor_in_echo_area == 0)
7933 fprintf (stderr, "\n");
7934 fflush (stderr);
7935 }
7936 }
7937 else if (INTERACTIVE)
7938 {
7939 /* The frame whose minibuffer we're going to display the message on.
7940 It may be larger than the selected frame, so we need
7941 to use its buffer, not the selected frame's buffer. */
7942 Lisp_Object mini_window;
7943 struct frame *f, *sf = SELECTED_FRAME ();
7944
7945 /* Get the frame containing the minibuffer
7946 that the selected frame is using. */
7947 mini_window = FRAME_MINIBUF_WINDOW (sf);
7948 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7949
7950 /* A null message buffer means that the frame hasn't really been
7951 initialized yet. Error messages get reported properly by
7952 cmd_error, so this must be just an informative message; toss it. */
7953 if (FRAME_MESSAGE_BUF (f))
7954 {
7955 Lisp_Object args[2], message;
7956 struct gcpro gcpro1, gcpro2;
7957
7958 args[0] = build_string (m);
7959 args[1] = message = string;
7960 GCPRO2 (args[0], message);
7961 gcpro1.nvars = 2;
7962
7963 message = Fformat (2, args);
7964
7965 if (log)
7966 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7967 else
7968 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7969
7970 UNGCPRO;
7971
7972 /* Print should start at the beginning of the message
7973 buffer next time. */
7974 message_buf_print = 0;
7975 }
7976 }
7977 }
7978
7979
7980 /* Dump an informative message to the minibuf. If M is 0, clear out
7981 any existing message, and let the mini-buffer text show through. */
7982
7983 /* VARARGS 1 */
7984 void
7985 message (m, a1, a2, a3)
7986 char *m;
7987 EMACS_INT a1, a2, a3;
7988 {
7989 if (noninteractive)
7990 {
7991 if (m)
7992 {
7993 if (noninteractive_need_newline)
7994 putc ('\n', stderr);
7995 noninteractive_need_newline = 0;
7996 fprintf (stderr, m, a1, a2, a3);
7997 if (cursor_in_echo_area == 0)
7998 fprintf (stderr, "\n");
7999 fflush (stderr);
8000 }
8001 }
8002 else if (INTERACTIVE)
8003 {
8004 /* The frame whose mini-buffer we're going to display the message
8005 on. It may be larger than the selected frame, so we need to
8006 use its buffer, not the selected frame's buffer. */
8007 Lisp_Object mini_window;
8008 struct frame *f, *sf = SELECTED_FRAME ();
8009
8010 /* Get the frame containing the mini-buffer
8011 that the selected frame is using. */
8012 mini_window = FRAME_MINIBUF_WINDOW (sf);
8013 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8014
8015 /* A null message buffer means that the frame hasn't really been
8016 initialized yet. Error messages get reported properly by
8017 cmd_error, so this must be just an informative message; toss
8018 it. */
8019 if (FRAME_MESSAGE_BUF (f))
8020 {
8021 if (m)
8022 {
8023 int len;
8024 #ifdef NO_ARG_ARRAY
8025 char *a[3];
8026 a[0] = (char *) a1;
8027 a[1] = (char *) a2;
8028 a[2] = (char *) a3;
8029
8030 len = doprnt (FRAME_MESSAGE_BUF (f),
8031 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8032 #else
8033 len = doprnt (FRAME_MESSAGE_BUF (f),
8034 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8035 (char **) &a1);
8036 #endif /* NO_ARG_ARRAY */
8037
8038 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8039 }
8040 else
8041 message1 (0);
8042
8043 /* Print should start at the beginning of the message
8044 buffer next time. */
8045 message_buf_print = 0;
8046 }
8047 }
8048 }
8049
8050
8051 /* The non-logging version of message. */
8052
8053 void
8054 message_nolog (m, a1, a2, a3)
8055 char *m;
8056 EMACS_INT a1, a2, a3;
8057 {
8058 Lisp_Object old_log_max;
8059 old_log_max = Vmessage_log_max;
8060 Vmessage_log_max = Qnil;
8061 message (m, a1, a2, a3);
8062 Vmessage_log_max = old_log_max;
8063 }
8064
8065
8066 /* Display the current message in the current mini-buffer. This is
8067 only called from error handlers in process.c, and is not time
8068 critical. */
8069
8070 void
8071 update_echo_area ()
8072 {
8073 if (!NILP (echo_area_buffer[0]))
8074 {
8075 Lisp_Object string;
8076 string = Fcurrent_message ();
8077 message3 (string, SBYTES (string),
8078 !NILP (current_buffer->enable_multibyte_characters));
8079 }
8080 }
8081
8082
8083 /* Make sure echo area buffers in `echo_buffers' are live.
8084 If they aren't, make new ones. */
8085
8086 static void
8087 ensure_echo_area_buffers ()
8088 {
8089 int i;
8090
8091 for (i = 0; i < 2; ++i)
8092 if (!BUFFERP (echo_buffer[i])
8093 || NILP (XBUFFER (echo_buffer[i])->name))
8094 {
8095 char name[30];
8096 Lisp_Object old_buffer;
8097 int j;
8098
8099 old_buffer = echo_buffer[i];
8100 sprintf (name, " *Echo Area %d*", i);
8101 echo_buffer[i] = Fget_buffer_create (build_string (name));
8102 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8103
8104 for (j = 0; j < 2; ++j)
8105 if (EQ (old_buffer, echo_area_buffer[j]))
8106 echo_area_buffer[j] = echo_buffer[i];
8107 }
8108 }
8109
8110
8111 /* Call FN with args A1..A4 with either the current or last displayed
8112 echo_area_buffer as current buffer.
8113
8114 WHICH zero means use the current message buffer
8115 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8116 from echo_buffer[] and clear it.
8117
8118 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8119 suitable buffer from echo_buffer[] and clear it.
8120
8121 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8122 that the current message becomes the last displayed one, make
8123 choose a suitable buffer for echo_area_buffer[0], and clear it.
8124
8125 Value is what FN returns. */
8126
8127 static int
8128 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8129 struct window *w;
8130 int which;
8131 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8132 EMACS_INT a1;
8133 Lisp_Object a2;
8134 EMACS_INT a3, a4;
8135 {
8136 Lisp_Object buffer;
8137 int this_one, the_other, clear_buffer_p, rc;
8138 int count = SPECPDL_INDEX ();
8139
8140 /* If buffers aren't live, make new ones. */
8141 ensure_echo_area_buffers ();
8142
8143 clear_buffer_p = 0;
8144
8145 if (which == 0)
8146 this_one = 0, the_other = 1;
8147 else if (which > 0)
8148 this_one = 1, the_other = 0;
8149 else
8150 {
8151 this_one = 0, the_other = 1;
8152 clear_buffer_p = 1;
8153
8154 /* We need a fresh one in case the current echo buffer equals
8155 the one containing the last displayed echo area message. */
8156 if (!NILP (echo_area_buffer[this_one])
8157 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8158 echo_area_buffer[this_one] = Qnil;
8159 }
8160
8161 /* Choose a suitable buffer from echo_buffer[] is we don't
8162 have one. */
8163 if (NILP (echo_area_buffer[this_one]))
8164 {
8165 echo_area_buffer[this_one]
8166 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8167 ? echo_buffer[the_other]
8168 : echo_buffer[this_one]);
8169 clear_buffer_p = 1;
8170 }
8171
8172 buffer = echo_area_buffer[this_one];
8173
8174 /* Don't get confused by reusing the buffer used for echoing
8175 for a different purpose. */
8176 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8177 cancel_echoing ();
8178
8179 record_unwind_protect (unwind_with_echo_area_buffer,
8180 with_echo_area_buffer_unwind_data (w));
8181
8182 /* Make the echo area buffer current. Note that for display
8183 purposes, it is not necessary that the displayed window's buffer
8184 == current_buffer, except for text property lookup. So, let's
8185 only set that buffer temporarily here without doing a full
8186 Fset_window_buffer. We must also change w->pointm, though,
8187 because otherwise an assertions in unshow_buffer fails, and Emacs
8188 aborts. */
8189 set_buffer_internal_1 (XBUFFER (buffer));
8190 if (w)
8191 {
8192 w->buffer = buffer;
8193 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8194 }
8195
8196 current_buffer->undo_list = Qt;
8197 current_buffer->read_only = Qnil;
8198 specbind (Qinhibit_read_only, Qt);
8199 specbind (Qinhibit_modification_hooks, Qt);
8200
8201 if (clear_buffer_p && Z > BEG)
8202 del_range (BEG, Z);
8203
8204 xassert (BEGV >= BEG);
8205 xassert (ZV <= Z && ZV >= BEGV);
8206
8207 rc = fn (a1, a2, a3, a4);
8208
8209 xassert (BEGV >= BEG);
8210 xassert (ZV <= Z && ZV >= BEGV);
8211
8212 unbind_to (count, Qnil);
8213 return rc;
8214 }
8215
8216
8217 /* Save state that should be preserved around the call to the function
8218 FN called in with_echo_area_buffer. */
8219
8220 static Lisp_Object
8221 with_echo_area_buffer_unwind_data (w)
8222 struct window *w;
8223 {
8224 int i = 0;
8225 Lisp_Object vector, tmp;
8226
8227 /* Reduce consing by keeping one vector in
8228 Vwith_echo_area_save_vector. */
8229 vector = Vwith_echo_area_save_vector;
8230 Vwith_echo_area_save_vector = Qnil;
8231
8232 if (NILP (vector))
8233 vector = Fmake_vector (make_number (7), Qnil);
8234
8235 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8236 ASET (vector, i, Vdeactivate_mark); ++i;
8237 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8238
8239 if (w)
8240 {
8241 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8242 ASET (vector, i, w->buffer); ++i;
8243 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8244 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8245 }
8246 else
8247 {
8248 int end = i + 4;
8249 for (; i < end; ++i)
8250 ASET (vector, i, Qnil);
8251 }
8252
8253 xassert (i == ASIZE (vector));
8254 return vector;
8255 }
8256
8257
8258 /* Restore global state from VECTOR which was created by
8259 with_echo_area_buffer_unwind_data. */
8260
8261 static Lisp_Object
8262 unwind_with_echo_area_buffer (vector)
8263 Lisp_Object vector;
8264 {
8265 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8266 Vdeactivate_mark = AREF (vector, 1);
8267 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8268
8269 if (WINDOWP (AREF (vector, 3)))
8270 {
8271 struct window *w;
8272 Lisp_Object buffer, charpos, bytepos;
8273
8274 w = XWINDOW (AREF (vector, 3));
8275 buffer = AREF (vector, 4);
8276 charpos = AREF (vector, 5);
8277 bytepos = AREF (vector, 6);
8278
8279 w->buffer = buffer;
8280 set_marker_both (w->pointm, buffer,
8281 XFASTINT (charpos), XFASTINT (bytepos));
8282 }
8283
8284 Vwith_echo_area_save_vector = vector;
8285 return Qnil;
8286 }
8287
8288
8289 /* Set up the echo area for use by print functions. MULTIBYTE_P
8290 non-zero means we will print multibyte. */
8291
8292 void
8293 setup_echo_area_for_printing (multibyte_p)
8294 int multibyte_p;
8295 {
8296 /* If we can't find an echo area any more, exit. */
8297 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8298 Fkill_emacs (Qnil);
8299
8300 ensure_echo_area_buffers ();
8301
8302 if (!message_buf_print)
8303 {
8304 /* A message has been output since the last time we printed.
8305 Choose a fresh echo area buffer. */
8306 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8307 echo_area_buffer[0] = echo_buffer[1];
8308 else
8309 echo_area_buffer[0] = echo_buffer[0];
8310
8311 /* Switch to that buffer and clear it. */
8312 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8313 current_buffer->truncate_lines = Qnil;
8314
8315 if (Z > BEG)
8316 {
8317 int count = SPECPDL_INDEX ();
8318 specbind (Qinhibit_read_only, Qt);
8319 /* Note that undo recording is always disabled. */
8320 del_range (BEG, Z);
8321 unbind_to (count, Qnil);
8322 }
8323 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8324
8325 /* Set up the buffer for the multibyteness we need. */
8326 if (multibyte_p
8327 != !NILP (current_buffer->enable_multibyte_characters))
8328 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8329
8330 /* Raise the frame containing the echo area. */
8331 if (minibuffer_auto_raise)
8332 {
8333 struct frame *sf = SELECTED_FRAME ();
8334 Lisp_Object mini_window;
8335 mini_window = FRAME_MINIBUF_WINDOW (sf);
8336 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8337 }
8338
8339 message_log_maybe_newline ();
8340 message_buf_print = 1;
8341 }
8342 else
8343 {
8344 if (NILP (echo_area_buffer[0]))
8345 {
8346 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8347 echo_area_buffer[0] = echo_buffer[1];
8348 else
8349 echo_area_buffer[0] = echo_buffer[0];
8350 }
8351
8352 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8353 {
8354 /* Someone switched buffers between print requests. */
8355 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8356 current_buffer->truncate_lines = Qnil;
8357 }
8358 }
8359 }
8360
8361
8362 /* Display an echo area message in window W. Value is non-zero if W's
8363 height is changed. If display_last_displayed_message_p is
8364 non-zero, display the message that was last displayed, otherwise
8365 display the current message. */
8366
8367 static int
8368 display_echo_area (w)
8369 struct window *w;
8370 {
8371 int i, no_message_p, window_height_changed_p, count;
8372
8373 /* Temporarily disable garbage collections while displaying the echo
8374 area. This is done because a GC can print a message itself.
8375 That message would modify the echo area buffer's contents while a
8376 redisplay of the buffer is going on, and seriously confuse
8377 redisplay. */
8378 count = inhibit_garbage_collection ();
8379
8380 /* If there is no message, we must call display_echo_area_1
8381 nevertheless because it resizes the window. But we will have to
8382 reset the echo_area_buffer in question to nil at the end because
8383 with_echo_area_buffer will sets it to an empty buffer. */
8384 i = display_last_displayed_message_p ? 1 : 0;
8385 no_message_p = NILP (echo_area_buffer[i]);
8386
8387 window_height_changed_p
8388 = with_echo_area_buffer (w, display_last_displayed_message_p,
8389 display_echo_area_1,
8390 (EMACS_INT) w, Qnil, 0, 0);
8391
8392 if (no_message_p)
8393 echo_area_buffer[i] = Qnil;
8394
8395 unbind_to (count, Qnil);
8396 return window_height_changed_p;
8397 }
8398
8399
8400 /* Helper for display_echo_area. Display the current buffer which
8401 contains the current echo area message in window W, a mini-window,
8402 a pointer to which is passed in A1. A2..A4 are currently not used.
8403 Change the height of W so that all of the message is displayed.
8404 Value is non-zero if height of W was changed. */
8405
8406 static int
8407 display_echo_area_1 (a1, a2, a3, a4)
8408 EMACS_INT a1;
8409 Lisp_Object a2;
8410 EMACS_INT a3, a4;
8411 {
8412 struct window *w = (struct window *) a1;
8413 Lisp_Object window;
8414 struct text_pos start;
8415 int window_height_changed_p = 0;
8416
8417 /* Do this before displaying, so that we have a large enough glyph
8418 matrix for the display. If we can't get enough space for the
8419 whole text, display the last N lines. That works by setting w->start. */
8420 window_height_changed_p = resize_mini_window (w, 0);
8421
8422 /* Use the starting position chosen by resize_mini_window. */
8423 SET_TEXT_POS_FROM_MARKER (start, w->start);
8424
8425 /* Display. */
8426 clear_glyph_matrix (w->desired_matrix);
8427 XSETWINDOW (window, w);
8428 try_window (window, start, 0);
8429
8430 return window_height_changed_p;
8431 }
8432
8433
8434 /* Resize the echo area window to exactly the size needed for the
8435 currently displayed message, if there is one. If a mini-buffer
8436 is active, don't shrink it. */
8437
8438 void
8439 resize_echo_area_exactly ()
8440 {
8441 if (BUFFERP (echo_area_buffer[0])
8442 && WINDOWP (echo_area_window))
8443 {
8444 struct window *w = XWINDOW (echo_area_window);
8445 int resized_p;
8446 Lisp_Object resize_exactly;
8447
8448 if (minibuf_level == 0)
8449 resize_exactly = Qt;
8450 else
8451 resize_exactly = Qnil;
8452
8453 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8454 (EMACS_INT) w, resize_exactly, 0, 0);
8455 if (resized_p)
8456 {
8457 ++windows_or_buffers_changed;
8458 ++update_mode_lines;
8459 redisplay_internal (0);
8460 }
8461 }
8462 }
8463
8464
8465 /* Callback function for with_echo_area_buffer, when used from
8466 resize_echo_area_exactly. A1 contains a pointer to the window to
8467 resize, EXACTLY non-nil means resize the mini-window exactly to the
8468 size of the text displayed. A3 and A4 are not used. Value is what
8469 resize_mini_window returns. */
8470
8471 static int
8472 resize_mini_window_1 (a1, exactly, a3, a4)
8473 EMACS_INT a1;
8474 Lisp_Object exactly;
8475 EMACS_INT a3, a4;
8476 {
8477 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8478 }
8479
8480
8481 /* Resize mini-window W to fit the size of its contents. EXACT_P
8482 means size the window exactly to the size needed. Otherwise, it's
8483 only enlarged until W's buffer is empty.
8484
8485 Set W->start to the right place to begin display. If the whole
8486 contents fit, start at the beginning. Otherwise, start so as
8487 to make the end of the contents appear. This is particularly
8488 important for y-or-n-p, but seems desirable generally.
8489
8490 Value is non-zero if the window height has been changed. */
8491
8492 int
8493 resize_mini_window (w, exact_p)
8494 struct window *w;
8495 int exact_p;
8496 {
8497 struct frame *f = XFRAME (w->frame);
8498 int window_height_changed_p = 0;
8499
8500 xassert (MINI_WINDOW_P (w));
8501
8502 /* By default, start display at the beginning. */
8503 set_marker_both (w->start, w->buffer,
8504 BUF_BEGV (XBUFFER (w->buffer)),
8505 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8506
8507 /* Don't resize windows while redisplaying a window; it would
8508 confuse redisplay functions when the size of the window they are
8509 displaying changes from under them. Such a resizing can happen,
8510 for instance, when which-func prints a long message while
8511 we are running fontification-functions. We're running these
8512 functions with safe_call which binds inhibit-redisplay to t. */
8513 if (!NILP (Vinhibit_redisplay))
8514 return 0;
8515
8516 /* Nil means don't try to resize. */
8517 if (NILP (Vresize_mini_windows)
8518 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8519 return 0;
8520
8521 if (!FRAME_MINIBUF_ONLY_P (f))
8522 {
8523 struct it it;
8524 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8525 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8526 int height, max_height;
8527 int unit = FRAME_LINE_HEIGHT (f);
8528 struct text_pos start;
8529 struct buffer *old_current_buffer = NULL;
8530
8531 if (current_buffer != XBUFFER (w->buffer))
8532 {
8533 old_current_buffer = current_buffer;
8534 set_buffer_internal (XBUFFER (w->buffer));
8535 }
8536
8537 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8538
8539 /* Compute the max. number of lines specified by the user. */
8540 if (FLOATP (Vmax_mini_window_height))
8541 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8542 else if (INTEGERP (Vmax_mini_window_height))
8543 max_height = XINT (Vmax_mini_window_height);
8544 else
8545 max_height = total_height / 4;
8546
8547 /* Correct that max. height if it's bogus. */
8548 max_height = max (1, max_height);
8549 max_height = min (total_height, max_height);
8550
8551 /* Find out the height of the text in the window. */
8552 if (it.truncate_lines_p)
8553 height = 1;
8554 else
8555 {
8556 last_height = 0;
8557 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8558 if (it.max_ascent == 0 && it.max_descent == 0)
8559 height = it.current_y + last_height;
8560 else
8561 height = it.current_y + it.max_ascent + it.max_descent;
8562 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8563 height = (height + unit - 1) / unit;
8564 }
8565
8566 /* Compute a suitable window start. */
8567 if (height > max_height)
8568 {
8569 height = max_height;
8570 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8571 move_it_vertically_backward (&it, (height - 1) * unit);
8572 start = it.current.pos;
8573 }
8574 else
8575 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8576 SET_MARKER_FROM_TEXT_POS (w->start, start);
8577
8578 if (EQ (Vresize_mini_windows, Qgrow_only))
8579 {
8580 /* Let it grow only, until we display an empty message, in which
8581 case the window shrinks again. */
8582 if (height > WINDOW_TOTAL_LINES (w))
8583 {
8584 int old_height = WINDOW_TOTAL_LINES (w);
8585 freeze_window_starts (f, 1);
8586 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8587 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8588 }
8589 else if (height < WINDOW_TOTAL_LINES (w)
8590 && (exact_p || BEGV == ZV))
8591 {
8592 int old_height = WINDOW_TOTAL_LINES (w);
8593 freeze_window_starts (f, 0);
8594 shrink_mini_window (w);
8595 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8596 }
8597 }
8598 else
8599 {
8600 /* Always resize to exact size needed. */
8601 if (height > WINDOW_TOTAL_LINES (w))
8602 {
8603 int old_height = WINDOW_TOTAL_LINES (w);
8604 freeze_window_starts (f, 1);
8605 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8606 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8607 }
8608 else if (height < WINDOW_TOTAL_LINES (w))
8609 {
8610 int old_height = WINDOW_TOTAL_LINES (w);
8611 freeze_window_starts (f, 0);
8612 shrink_mini_window (w);
8613
8614 if (height)
8615 {
8616 freeze_window_starts (f, 1);
8617 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8618 }
8619
8620 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8621 }
8622 }
8623
8624 if (old_current_buffer)
8625 set_buffer_internal (old_current_buffer);
8626 }
8627
8628 return window_height_changed_p;
8629 }
8630
8631
8632 /* Value is the current message, a string, or nil if there is no
8633 current message. */
8634
8635 Lisp_Object
8636 current_message ()
8637 {
8638 Lisp_Object msg;
8639
8640 if (!BUFFERP (echo_area_buffer[0]))
8641 msg = Qnil;
8642 else
8643 {
8644 with_echo_area_buffer (0, 0, current_message_1,
8645 (EMACS_INT) &msg, Qnil, 0, 0);
8646 if (NILP (msg))
8647 echo_area_buffer[0] = Qnil;
8648 }
8649
8650 return msg;
8651 }
8652
8653
8654 static int
8655 current_message_1 (a1, a2, a3, a4)
8656 EMACS_INT a1;
8657 Lisp_Object a2;
8658 EMACS_INT a3, a4;
8659 {
8660 Lisp_Object *msg = (Lisp_Object *) a1;
8661
8662 if (Z > BEG)
8663 *msg = make_buffer_string (BEG, Z, 1);
8664 else
8665 *msg = Qnil;
8666 return 0;
8667 }
8668
8669
8670 /* Push the current message on Vmessage_stack for later restauration
8671 by restore_message. Value is non-zero if the current message isn't
8672 empty. This is a relatively infrequent operation, so it's not
8673 worth optimizing. */
8674
8675 int
8676 push_message ()
8677 {
8678 Lisp_Object msg;
8679 msg = current_message ();
8680 Vmessage_stack = Fcons (msg, Vmessage_stack);
8681 return STRINGP (msg);
8682 }
8683
8684
8685 /* Restore message display from the top of Vmessage_stack. */
8686
8687 void
8688 restore_message ()
8689 {
8690 Lisp_Object msg;
8691
8692 xassert (CONSP (Vmessage_stack));
8693 msg = XCAR (Vmessage_stack);
8694 if (STRINGP (msg))
8695 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8696 else
8697 message3_nolog (msg, 0, 0);
8698 }
8699
8700
8701 /* Handler for record_unwind_protect calling pop_message. */
8702
8703 Lisp_Object
8704 pop_message_unwind (dummy)
8705 Lisp_Object dummy;
8706 {
8707 pop_message ();
8708 return Qnil;
8709 }
8710
8711 /* Pop the top-most entry off Vmessage_stack. */
8712
8713 void
8714 pop_message ()
8715 {
8716 xassert (CONSP (Vmessage_stack));
8717 Vmessage_stack = XCDR (Vmessage_stack);
8718 }
8719
8720
8721 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8722 exits. If the stack is not empty, we have a missing pop_message
8723 somewhere. */
8724
8725 void
8726 check_message_stack ()
8727 {
8728 if (!NILP (Vmessage_stack))
8729 abort ();
8730 }
8731
8732
8733 /* Truncate to NCHARS what will be displayed in the echo area the next
8734 time we display it---but don't redisplay it now. */
8735
8736 void
8737 truncate_echo_area (nchars)
8738 int nchars;
8739 {
8740 if (nchars == 0)
8741 echo_area_buffer[0] = Qnil;
8742 /* A null message buffer means that the frame hasn't really been
8743 initialized yet. Error messages get reported properly by
8744 cmd_error, so this must be just an informative message; toss it. */
8745 else if (!noninteractive
8746 && INTERACTIVE
8747 && !NILP (echo_area_buffer[0]))
8748 {
8749 struct frame *sf = SELECTED_FRAME ();
8750 if (FRAME_MESSAGE_BUF (sf))
8751 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8752 }
8753 }
8754
8755
8756 /* Helper function for truncate_echo_area. Truncate the current
8757 message to at most NCHARS characters. */
8758
8759 static int
8760 truncate_message_1 (nchars, a2, a3, a4)
8761 EMACS_INT nchars;
8762 Lisp_Object a2;
8763 EMACS_INT a3, a4;
8764 {
8765 if (BEG + nchars < Z)
8766 del_range (BEG + nchars, Z);
8767 if (Z == BEG)
8768 echo_area_buffer[0] = Qnil;
8769 return 0;
8770 }
8771
8772
8773 /* Set the current message to a substring of S or STRING.
8774
8775 If STRING is a Lisp string, set the message to the first NBYTES
8776 bytes from STRING. NBYTES zero means use the whole string. If
8777 STRING is multibyte, the message will be displayed multibyte.
8778
8779 If S is not null, set the message to the first LEN bytes of S. LEN
8780 zero means use the whole string. MULTIBYTE_P non-zero means S is
8781 multibyte. Display the message multibyte in that case.
8782
8783 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8784 to t before calling set_message_1 (which calls insert).
8785 */
8786
8787 void
8788 set_message (s, string, nbytes, multibyte_p)
8789 const char *s;
8790 Lisp_Object string;
8791 int nbytes, multibyte_p;
8792 {
8793 message_enable_multibyte
8794 = ((s && multibyte_p)
8795 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8796
8797 with_echo_area_buffer (0, -1, set_message_1,
8798 (EMACS_INT) s, string, nbytes, multibyte_p);
8799 message_buf_print = 0;
8800 help_echo_showing_p = 0;
8801 }
8802
8803
8804 /* Helper function for set_message. Arguments have the same meaning
8805 as there, with A1 corresponding to S and A2 corresponding to STRING
8806 This function is called with the echo area buffer being
8807 current. */
8808
8809 static int
8810 set_message_1 (a1, a2, nbytes, multibyte_p)
8811 EMACS_INT a1;
8812 Lisp_Object a2;
8813 EMACS_INT nbytes, multibyte_p;
8814 {
8815 const char *s = (const char *) a1;
8816 Lisp_Object string = a2;
8817
8818 /* Change multibyteness of the echo buffer appropriately. */
8819 if (message_enable_multibyte
8820 != !NILP (current_buffer->enable_multibyte_characters))
8821 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8822
8823 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8824
8825 /* Insert new message at BEG. */
8826 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8827
8828 if (STRINGP (string))
8829 {
8830 int nchars;
8831
8832 if (nbytes == 0)
8833 nbytes = SBYTES (string);
8834 nchars = string_byte_to_char (string, nbytes);
8835
8836 /* This function takes care of single/multibyte conversion. We
8837 just have to ensure that the echo area buffer has the right
8838 setting of enable_multibyte_characters. */
8839 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8840 }
8841 else if (s)
8842 {
8843 if (nbytes == 0)
8844 nbytes = strlen (s);
8845
8846 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8847 {
8848 /* Convert from multi-byte to single-byte. */
8849 int i, c, n;
8850 unsigned char work[1];
8851
8852 /* Convert a multibyte string to single-byte. */
8853 for (i = 0; i < nbytes; i += n)
8854 {
8855 c = string_char_and_length (s + i, nbytes - i, &n);
8856 work[0] = (ASCII_CHAR_P (c)
8857 ? c
8858 : multibyte_char_to_unibyte (c, Qnil));
8859 insert_1_both (work, 1, 1, 1, 0, 0);
8860 }
8861 }
8862 else if (!multibyte_p
8863 && !NILP (current_buffer->enable_multibyte_characters))
8864 {
8865 /* Convert from single-byte to multi-byte. */
8866 int i, c, n;
8867 const unsigned char *msg = (const unsigned char *) s;
8868 unsigned char str[MAX_MULTIBYTE_LENGTH];
8869
8870 /* Convert a single-byte string to multibyte. */
8871 for (i = 0; i < nbytes; i++)
8872 {
8873 c = msg[i];
8874 c = unibyte_char_to_multibyte (c);
8875 n = CHAR_STRING (c, str);
8876 insert_1_both (str, 1, n, 1, 0, 0);
8877 }
8878 }
8879 else
8880 insert_1 (s, nbytes, 1, 0, 0);
8881 }
8882
8883 return 0;
8884 }
8885
8886
8887 /* Clear messages. CURRENT_P non-zero means clear the current
8888 message. LAST_DISPLAYED_P non-zero means clear the message
8889 last displayed. */
8890
8891 void
8892 clear_message (current_p, last_displayed_p)
8893 int current_p, last_displayed_p;
8894 {
8895 if (current_p)
8896 {
8897 echo_area_buffer[0] = Qnil;
8898 message_cleared_p = 1;
8899 }
8900
8901 if (last_displayed_p)
8902 echo_area_buffer[1] = Qnil;
8903
8904 message_buf_print = 0;
8905 }
8906
8907 /* Clear garbaged frames.
8908
8909 This function is used where the old redisplay called
8910 redraw_garbaged_frames which in turn called redraw_frame which in
8911 turn called clear_frame. The call to clear_frame was a source of
8912 flickering. I believe a clear_frame is not necessary. It should
8913 suffice in the new redisplay to invalidate all current matrices,
8914 and ensure a complete redisplay of all windows. */
8915
8916 static void
8917 clear_garbaged_frames ()
8918 {
8919 if (frame_garbaged)
8920 {
8921 Lisp_Object tail, frame;
8922 int changed_count = 0;
8923
8924 FOR_EACH_FRAME (tail, frame)
8925 {
8926 struct frame *f = XFRAME (frame);
8927
8928 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8929 {
8930 if (f->resized_p)
8931 {
8932 Fredraw_frame (frame);
8933 f->force_flush_display_p = 1;
8934 }
8935 clear_current_matrices (f);
8936 changed_count++;
8937 f->garbaged = 0;
8938 f->resized_p = 0;
8939 }
8940 }
8941
8942 frame_garbaged = 0;
8943 if (changed_count)
8944 ++windows_or_buffers_changed;
8945 }
8946 }
8947
8948
8949 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8950 is non-zero update selected_frame. Value is non-zero if the
8951 mini-windows height has been changed. */
8952
8953 static int
8954 echo_area_display (update_frame_p)
8955 int update_frame_p;
8956 {
8957 Lisp_Object mini_window;
8958 struct window *w;
8959 struct frame *f;
8960 int window_height_changed_p = 0;
8961 struct frame *sf = SELECTED_FRAME ();
8962
8963 mini_window = FRAME_MINIBUF_WINDOW (sf);
8964 w = XWINDOW (mini_window);
8965 f = XFRAME (WINDOW_FRAME (w));
8966
8967 /* Don't display if frame is invisible or not yet initialized. */
8968 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8969 return 0;
8970
8971 #ifdef HAVE_WINDOW_SYSTEM
8972 /* When Emacs starts, selected_frame may be the initial terminal
8973 frame. If we let this through, a message would be displayed on
8974 the terminal. */
8975 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8976 return 0;
8977 #endif /* HAVE_WINDOW_SYSTEM */
8978
8979 /* Redraw garbaged frames. */
8980 if (frame_garbaged)
8981 clear_garbaged_frames ();
8982
8983 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8984 {
8985 echo_area_window = mini_window;
8986 window_height_changed_p = display_echo_area (w);
8987 w->must_be_updated_p = 1;
8988
8989 /* Update the display, unless called from redisplay_internal.
8990 Also don't update the screen during redisplay itself. The
8991 update will happen at the end of redisplay, and an update
8992 here could cause confusion. */
8993 if (update_frame_p && !redisplaying_p)
8994 {
8995 int n = 0;
8996
8997 /* If the display update has been interrupted by pending
8998 input, update mode lines in the frame. Due to the
8999 pending input, it might have been that redisplay hasn't
9000 been called, so that mode lines above the echo area are
9001 garbaged. This looks odd, so we prevent it here. */
9002 if (!display_completed)
9003 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9004
9005 if (window_height_changed_p
9006 /* Don't do this if Emacs is shutting down. Redisplay
9007 needs to run hooks. */
9008 && !NILP (Vrun_hooks))
9009 {
9010 /* Must update other windows. Likewise as in other
9011 cases, don't let this update be interrupted by
9012 pending input. */
9013 int count = SPECPDL_INDEX ();
9014 specbind (Qredisplay_dont_pause, Qt);
9015 windows_or_buffers_changed = 1;
9016 redisplay_internal (0);
9017 unbind_to (count, Qnil);
9018 }
9019 else if (FRAME_WINDOW_P (f) && n == 0)
9020 {
9021 /* Window configuration is the same as before.
9022 Can do with a display update of the echo area,
9023 unless we displayed some mode lines. */
9024 update_single_window (w, 1);
9025 FRAME_RIF (f)->flush_display (f);
9026 }
9027 else
9028 update_frame (f, 1, 1);
9029
9030 /* If cursor is in the echo area, make sure that the next
9031 redisplay displays the minibuffer, so that the cursor will
9032 be replaced with what the minibuffer wants. */
9033 if (cursor_in_echo_area)
9034 ++windows_or_buffers_changed;
9035 }
9036 }
9037 else if (!EQ (mini_window, selected_window))
9038 windows_or_buffers_changed++;
9039
9040 /* Last displayed message is now the current message. */
9041 echo_area_buffer[1] = echo_area_buffer[0];
9042 /* Inform read_char that we're not echoing. */
9043 echo_message_buffer = Qnil;
9044
9045 /* Prevent redisplay optimization in redisplay_internal by resetting
9046 this_line_start_pos. This is done because the mini-buffer now
9047 displays the message instead of its buffer text. */
9048 if (EQ (mini_window, selected_window))
9049 CHARPOS (this_line_start_pos) = 0;
9050
9051 return window_height_changed_p;
9052 }
9053
9054
9055 \f
9056 /***********************************************************************
9057 Mode Lines and Frame Titles
9058 ***********************************************************************/
9059
9060 /* A buffer for constructing non-propertized mode-line strings and
9061 frame titles in it; allocated from the heap in init_xdisp and
9062 resized as needed in store_mode_line_noprop_char. */
9063
9064 static char *mode_line_noprop_buf;
9065
9066 /* The buffer's end, and a current output position in it. */
9067
9068 static char *mode_line_noprop_buf_end;
9069 static char *mode_line_noprop_ptr;
9070
9071 #define MODE_LINE_NOPROP_LEN(start) \
9072 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9073
9074 static enum {
9075 MODE_LINE_DISPLAY = 0,
9076 MODE_LINE_TITLE,
9077 MODE_LINE_NOPROP,
9078 MODE_LINE_STRING
9079 } mode_line_target;
9080
9081 /* Alist that caches the results of :propertize.
9082 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9083 static Lisp_Object mode_line_proptrans_alist;
9084
9085 /* List of strings making up the mode-line. */
9086 static Lisp_Object mode_line_string_list;
9087
9088 /* Base face property when building propertized mode line string. */
9089 static Lisp_Object mode_line_string_face;
9090 static Lisp_Object mode_line_string_face_prop;
9091
9092
9093 /* Unwind data for mode line strings */
9094
9095 static Lisp_Object Vmode_line_unwind_vector;
9096
9097 static Lisp_Object
9098 format_mode_line_unwind_data (struct buffer *obuf,
9099 Lisp_Object owin,
9100 int save_proptrans)
9101 {
9102 Lisp_Object vector, tmp;
9103
9104 /* Reduce consing by keeping one vector in
9105 Vwith_echo_area_save_vector. */
9106 vector = Vmode_line_unwind_vector;
9107 Vmode_line_unwind_vector = Qnil;
9108
9109 if (NILP (vector))
9110 vector = Fmake_vector (make_number (8), Qnil);
9111
9112 ASET (vector, 0, make_number (mode_line_target));
9113 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9114 ASET (vector, 2, mode_line_string_list);
9115 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9116 ASET (vector, 4, mode_line_string_face);
9117 ASET (vector, 5, mode_line_string_face_prop);
9118
9119 if (obuf)
9120 XSETBUFFER (tmp, obuf);
9121 else
9122 tmp = Qnil;
9123 ASET (vector, 6, tmp);
9124 ASET (vector, 7, owin);
9125
9126 return vector;
9127 }
9128
9129 static Lisp_Object
9130 unwind_format_mode_line (vector)
9131 Lisp_Object vector;
9132 {
9133 mode_line_target = XINT (AREF (vector, 0));
9134 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9135 mode_line_string_list = AREF (vector, 2);
9136 if (! EQ (AREF (vector, 3), Qt))
9137 mode_line_proptrans_alist = AREF (vector, 3);
9138 mode_line_string_face = AREF (vector, 4);
9139 mode_line_string_face_prop = AREF (vector, 5);
9140
9141 if (!NILP (AREF (vector, 7)))
9142 /* Select window before buffer, since it may change the buffer. */
9143 Fselect_window (AREF (vector, 7), Qt);
9144
9145 if (!NILP (AREF (vector, 6)))
9146 {
9147 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9148 ASET (vector, 6, Qnil);
9149 }
9150
9151 Vmode_line_unwind_vector = vector;
9152 return Qnil;
9153 }
9154
9155
9156 /* Store a single character C for the frame title in mode_line_noprop_buf.
9157 Re-allocate mode_line_noprop_buf if necessary. */
9158
9159 static void
9160 #ifdef PROTOTYPES
9161 store_mode_line_noprop_char (char c)
9162 #else
9163 store_mode_line_noprop_char (c)
9164 char c;
9165 #endif
9166 {
9167 /* If output position has reached the end of the allocated buffer,
9168 double the buffer's size. */
9169 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9170 {
9171 int len = MODE_LINE_NOPROP_LEN (0);
9172 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9173 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9174 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9175 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9176 }
9177
9178 *mode_line_noprop_ptr++ = c;
9179 }
9180
9181
9182 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9183 mode_line_noprop_ptr. STR is the string to store. Do not copy
9184 characters that yield more columns than PRECISION; PRECISION <= 0
9185 means copy the whole string. Pad with spaces until FIELD_WIDTH
9186 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9187 pad. Called from display_mode_element when it is used to build a
9188 frame title. */
9189
9190 static int
9191 store_mode_line_noprop (str, field_width, precision)
9192 const unsigned char *str;
9193 int field_width, precision;
9194 {
9195 int n = 0;
9196 int dummy, nbytes;
9197
9198 /* Copy at most PRECISION chars from STR. */
9199 nbytes = strlen (str);
9200 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9201 while (nbytes--)
9202 store_mode_line_noprop_char (*str++);
9203
9204 /* Fill up with spaces until FIELD_WIDTH reached. */
9205 while (field_width > 0
9206 && n < field_width)
9207 {
9208 store_mode_line_noprop_char (' ');
9209 ++n;
9210 }
9211
9212 return n;
9213 }
9214
9215 /***********************************************************************
9216 Frame Titles
9217 ***********************************************************************/
9218
9219 #ifdef HAVE_WINDOW_SYSTEM
9220
9221 /* Set the title of FRAME, if it has changed. The title format is
9222 Vicon_title_format if FRAME is iconified, otherwise it is
9223 frame_title_format. */
9224
9225 static void
9226 x_consider_frame_title (frame)
9227 Lisp_Object frame;
9228 {
9229 struct frame *f = XFRAME (frame);
9230
9231 if (FRAME_WINDOW_P (f)
9232 || FRAME_MINIBUF_ONLY_P (f)
9233 || f->explicit_name)
9234 {
9235 /* Do we have more than one visible frame on this X display? */
9236 Lisp_Object tail;
9237 Lisp_Object fmt;
9238 int title_start;
9239 char *title;
9240 int len;
9241 struct it it;
9242 int count = SPECPDL_INDEX ();
9243
9244 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9245 {
9246 Lisp_Object other_frame = XCAR (tail);
9247 struct frame *tf = XFRAME (other_frame);
9248
9249 if (tf != f
9250 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9251 && !FRAME_MINIBUF_ONLY_P (tf)
9252 && !EQ (other_frame, tip_frame)
9253 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9254 break;
9255 }
9256
9257 /* Set global variable indicating that multiple frames exist. */
9258 multiple_frames = CONSP (tail);
9259
9260 /* Switch to the buffer of selected window of the frame. Set up
9261 mode_line_target so that display_mode_element will output into
9262 mode_line_noprop_buf; then display the title. */
9263 record_unwind_protect (unwind_format_mode_line,
9264 format_mode_line_unwind_data
9265 (current_buffer, selected_window, 0));
9266
9267 Fselect_window (f->selected_window, Qt);
9268 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9269 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9270
9271 mode_line_target = MODE_LINE_TITLE;
9272 title_start = MODE_LINE_NOPROP_LEN (0);
9273 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9274 NULL, DEFAULT_FACE_ID);
9275 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9276 len = MODE_LINE_NOPROP_LEN (title_start);
9277 title = mode_line_noprop_buf + title_start;
9278 unbind_to (count, Qnil);
9279
9280 /* Set the title only if it's changed. This avoids consing in
9281 the common case where it hasn't. (If it turns out that we've
9282 already wasted too much time by walking through the list with
9283 display_mode_element, then we might need to optimize at a
9284 higher level than this.) */
9285 if (! STRINGP (f->name)
9286 || SBYTES (f->name) != len
9287 || bcmp (title, SDATA (f->name), len) != 0)
9288 x_implicitly_set_name (f, make_string (title, len), Qnil);
9289 }
9290 }
9291
9292 #endif /* not HAVE_WINDOW_SYSTEM */
9293
9294
9295
9296 \f
9297 /***********************************************************************
9298 Menu Bars
9299 ***********************************************************************/
9300
9301
9302 /* Prepare for redisplay by updating menu-bar item lists when
9303 appropriate. This can call eval. */
9304
9305 void
9306 prepare_menu_bars ()
9307 {
9308 int all_windows;
9309 struct gcpro gcpro1, gcpro2;
9310 struct frame *f;
9311 Lisp_Object tooltip_frame;
9312
9313 #ifdef HAVE_WINDOW_SYSTEM
9314 tooltip_frame = tip_frame;
9315 #else
9316 tooltip_frame = Qnil;
9317 #endif
9318
9319 /* Update all frame titles based on their buffer names, etc. We do
9320 this before the menu bars so that the buffer-menu will show the
9321 up-to-date frame titles. */
9322 #ifdef HAVE_WINDOW_SYSTEM
9323 if (windows_or_buffers_changed || update_mode_lines)
9324 {
9325 Lisp_Object tail, frame;
9326
9327 FOR_EACH_FRAME (tail, frame)
9328 {
9329 f = XFRAME (frame);
9330 if (!EQ (frame, tooltip_frame)
9331 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9332 x_consider_frame_title (frame);
9333 }
9334 }
9335 #endif /* HAVE_WINDOW_SYSTEM */
9336
9337 /* Update the menu bar item lists, if appropriate. This has to be
9338 done before any actual redisplay or generation of display lines. */
9339 all_windows = (update_mode_lines
9340 || buffer_shared > 1
9341 || windows_or_buffers_changed);
9342 if (all_windows)
9343 {
9344 Lisp_Object tail, frame;
9345 int count = SPECPDL_INDEX ();
9346 /* 1 means that update_menu_bar has run its hooks
9347 so any further calls to update_menu_bar shouldn't do so again. */
9348 int menu_bar_hooks_run = 0;
9349
9350 record_unwind_save_match_data ();
9351
9352 FOR_EACH_FRAME (tail, frame)
9353 {
9354 f = XFRAME (frame);
9355
9356 /* Ignore tooltip frame. */
9357 if (EQ (frame, tooltip_frame))
9358 continue;
9359
9360 /* If a window on this frame changed size, report that to
9361 the user and clear the size-change flag. */
9362 if (FRAME_WINDOW_SIZES_CHANGED (f))
9363 {
9364 Lisp_Object functions;
9365
9366 /* Clear flag first in case we get an error below. */
9367 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9368 functions = Vwindow_size_change_functions;
9369 GCPRO2 (tail, functions);
9370
9371 while (CONSP (functions))
9372 {
9373 call1 (XCAR (functions), frame);
9374 functions = XCDR (functions);
9375 }
9376 UNGCPRO;
9377 }
9378
9379 GCPRO1 (tail);
9380 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9381 #ifdef HAVE_WINDOW_SYSTEM
9382 update_tool_bar (f, 0);
9383 #ifdef MAC_OS
9384 mac_update_title_bar (f, 0);
9385 #endif
9386 #endif
9387 UNGCPRO;
9388 }
9389
9390 unbind_to (count, Qnil);
9391 }
9392 else
9393 {
9394 struct frame *sf = SELECTED_FRAME ();
9395 update_menu_bar (sf, 1, 0);
9396 #ifdef HAVE_WINDOW_SYSTEM
9397 update_tool_bar (sf, 1);
9398 #ifdef MAC_OS
9399 mac_update_title_bar (sf, 1);
9400 #endif
9401 #endif
9402 }
9403
9404 /* Motif needs this. See comment in xmenu.c. Turn it off when
9405 pending_menu_activation is not defined. */
9406 #ifdef USE_X_TOOLKIT
9407 pending_menu_activation = 0;
9408 #endif
9409 }
9410
9411
9412 /* Update the menu bar item list for frame F. This has to be done
9413 before we start to fill in any display lines, because it can call
9414 eval.
9415
9416 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9417
9418 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9419 already ran the menu bar hooks for this redisplay, so there
9420 is no need to run them again. The return value is the
9421 updated value of this flag, to pass to the next call. */
9422
9423 static int
9424 update_menu_bar (f, save_match_data, hooks_run)
9425 struct frame *f;
9426 int save_match_data;
9427 int hooks_run;
9428 {
9429 Lisp_Object window;
9430 register struct window *w;
9431
9432 /* If called recursively during a menu update, do nothing. This can
9433 happen when, for instance, an activate-menubar-hook causes a
9434 redisplay. */
9435 if (inhibit_menubar_update)
9436 return hooks_run;
9437
9438 window = FRAME_SELECTED_WINDOW (f);
9439 w = XWINDOW (window);
9440
9441 #if 0 /* The if statement below this if statement used to include the
9442 condition !NILP (w->update_mode_line), rather than using
9443 update_mode_lines directly, and this if statement may have
9444 been added to make that condition work. Now the if
9445 statement below matches its comment, this isn't needed. */
9446 if (update_mode_lines)
9447 w->update_mode_line = Qt;
9448 #endif
9449
9450 if (FRAME_WINDOW_P (f)
9451 ?
9452 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9453 || defined (USE_GTK)
9454 FRAME_EXTERNAL_MENU_BAR (f)
9455 #else
9456 FRAME_MENU_BAR_LINES (f) > 0
9457 #endif
9458 : FRAME_MENU_BAR_LINES (f) > 0)
9459 {
9460 /* If the user has switched buffers or windows, we need to
9461 recompute to reflect the new bindings. But we'll
9462 recompute when update_mode_lines is set too; that means
9463 that people can use force-mode-line-update to request
9464 that the menu bar be recomputed. The adverse effect on
9465 the rest of the redisplay algorithm is about the same as
9466 windows_or_buffers_changed anyway. */
9467 if (windows_or_buffers_changed
9468 /* This used to test w->update_mode_line, but we believe
9469 there is no need to recompute the menu in that case. */
9470 || update_mode_lines
9471 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9472 < BUF_MODIFF (XBUFFER (w->buffer)))
9473 != !NILP (w->last_had_star))
9474 || ((!NILP (Vtransient_mark_mode)
9475 && !NILP (XBUFFER (w->buffer)->mark_active))
9476 != !NILP (w->region_showing)))
9477 {
9478 struct buffer *prev = current_buffer;
9479 int count = SPECPDL_INDEX ();
9480
9481 specbind (Qinhibit_menubar_update, Qt);
9482
9483 set_buffer_internal_1 (XBUFFER (w->buffer));
9484 if (save_match_data)
9485 record_unwind_save_match_data ();
9486 if (NILP (Voverriding_local_map_menu_flag))
9487 {
9488 specbind (Qoverriding_terminal_local_map, Qnil);
9489 specbind (Qoverriding_local_map, Qnil);
9490 }
9491
9492 if (!hooks_run)
9493 {
9494 /* Run the Lucid hook. */
9495 safe_run_hooks (Qactivate_menubar_hook);
9496
9497 /* If it has changed current-menubar from previous value,
9498 really recompute the menu-bar from the value. */
9499 if (! NILP (Vlucid_menu_bar_dirty_flag))
9500 call0 (Qrecompute_lucid_menubar);
9501
9502 safe_run_hooks (Qmenu_bar_update_hook);
9503
9504 hooks_run = 1;
9505 }
9506
9507 XSETFRAME (Vmenu_updating_frame, f);
9508 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9509
9510 /* Redisplay the menu bar in case we changed it. */
9511 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9512 || defined (USE_GTK)
9513 if (FRAME_WINDOW_P (f))
9514 {
9515 #ifdef MAC_OS
9516 /* All frames on Mac OS share the same menubar. So only
9517 the selected frame should be allowed to set it. */
9518 if (f == SELECTED_FRAME ())
9519 #endif
9520 set_frame_menubar (f, 0, 0);
9521 }
9522 else
9523 /* On a terminal screen, the menu bar is an ordinary screen
9524 line, and this makes it get updated. */
9525 w->update_mode_line = Qt;
9526 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9527 /* In the non-toolkit version, the menu bar is an ordinary screen
9528 line, and this makes it get updated. */
9529 w->update_mode_line = Qt;
9530 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9531
9532 unbind_to (count, Qnil);
9533 set_buffer_internal_1 (prev);
9534 }
9535 }
9536
9537 return hooks_run;
9538 }
9539
9540
9541 \f
9542 /***********************************************************************
9543 Output Cursor
9544 ***********************************************************************/
9545
9546 #ifdef HAVE_WINDOW_SYSTEM
9547
9548 /* EXPORT:
9549 Nominal cursor position -- where to draw output.
9550 HPOS and VPOS are window relative glyph matrix coordinates.
9551 X and Y are window relative pixel coordinates. */
9552
9553 struct cursor_pos output_cursor;
9554
9555
9556 /* EXPORT:
9557 Set the global variable output_cursor to CURSOR. All cursor
9558 positions are relative to updated_window. */
9559
9560 void
9561 set_output_cursor (cursor)
9562 struct cursor_pos *cursor;
9563 {
9564 output_cursor.hpos = cursor->hpos;
9565 output_cursor.vpos = cursor->vpos;
9566 output_cursor.x = cursor->x;
9567 output_cursor.y = cursor->y;
9568 }
9569
9570
9571 /* EXPORT for RIF:
9572 Set a nominal cursor position.
9573
9574 HPOS and VPOS are column/row positions in a window glyph matrix. X
9575 and Y are window text area relative pixel positions.
9576
9577 If this is done during an update, updated_window will contain the
9578 window that is being updated and the position is the future output
9579 cursor position for that window. If updated_window is null, use
9580 selected_window and display the cursor at the given position. */
9581
9582 void
9583 x_cursor_to (vpos, hpos, y, x)
9584 int vpos, hpos, y, x;
9585 {
9586 struct window *w;
9587
9588 /* If updated_window is not set, work on selected_window. */
9589 if (updated_window)
9590 w = updated_window;
9591 else
9592 w = XWINDOW (selected_window);
9593
9594 /* Set the output cursor. */
9595 output_cursor.hpos = hpos;
9596 output_cursor.vpos = vpos;
9597 output_cursor.x = x;
9598 output_cursor.y = y;
9599
9600 /* If not called as part of an update, really display the cursor.
9601 This will also set the cursor position of W. */
9602 if (updated_window == NULL)
9603 {
9604 BLOCK_INPUT;
9605 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9606 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9607 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9608 UNBLOCK_INPUT;
9609 }
9610 }
9611
9612 #endif /* HAVE_WINDOW_SYSTEM */
9613
9614 \f
9615 /***********************************************************************
9616 Tool-bars
9617 ***********************************************************************/
9618
9619 #ifdef HAVE_WINDOW_SYSTEM
9620
9621 /* Where the mouse was last time we reported a mouse event. */
9622
9623 FRAME_PTR last_mouse_frame;
9624
9625 /* Tool-bar item index of the item on which a mouse button was pressed
9626 or -1. */
9627
9628 int last_tool_bar_item;
9629
9630
9631 /* Update the tool-bar item list for frame F. This has to be done
9632 before we start to fill in any display lines. Called from
9633 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9634 and restore it here. */
9635
9636 static void
9637 update_tool_bar (f, save_match_data)
9638 struct frame *f;
9639 int save_match_data;
9640 {
9641 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9642 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9643 #else
9644 int do_update = WINDOWP (f->tool_bar_window)
9645 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9646 #endif
9647
9648 if (do_update)
9649 {
9650 Lisp_Object window;
9651 struct window *w;
9652
9653 window = FRAME_SELECTED_WINDOW (f);
9654 w = XWINDOW (window);
9655
9656 /* If the user has switched buffers or windows, we need to
9657 recompute to reflect the new bindings. But we'll
9658 recompute when update_mode_lines is set too; that means
9659 that people can use force-mode-line-update to request
9660 that the menu bar be recomputed. The adverse effect on
9661 the rest of the redisplay algorithm is about the same as
9662 windows_or_buffers_changed anyway. */
9663 if (windows_or_buffers_changed
9664 || !NILP (w->update_mode_line)
9665 || update_mode_lines
9666 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9667 < BUF_MODIFF (XBUFFER (w->buffer)))
9668 != !NILP (w->last_had_star))
9669 || ((!NILP (Vtransient_mark_mode)
9670 && !NILP (XBUFFER (w->buffer)->mark_active))
9671 != !NILP (w->region_showing)))
9672 {
9673 struct buffer *prev = current_buffer;
9674 int count = SPECPDL_INDEX ();
9675 Lisp_Object new_tool_bar;
9676 int new_n_tool_bar;
9677 struct gcpro gcpro1;
9678
9679 /* Set current_buffer to the buffer of the selected
9680 window of the frame, so that we get the right local
9681 keymaps. */
9682 set_buffer_internal_1 (XBUFFER (w->buffer));
9683
9684 /* Save match data, if we must. */
9685 if (save_match_data)
9686 record_unwind_save_match_data ();
9687
9688 /* Make sure that we don't accidentally use bogus keymaps. */
9689 if (NILP (Voverriding_local_map_menu_flag))
9690 {
9691 specbind (Qoverriding_terminal_local_map, Qnil);
9692 specbind (Qoverriding_local_map, Qnil);
9693 }
9694
9695 GCPRO1 (new_tool_bar);
9696
9697 /* Build desired tool-bar items from keymaps. */
9698 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9699 &new_n_tool_bar);
9700
9701 /* Redisplay the tool-bar if we changed it. */
9702 if (new_n_tool_bar != f->n_tool_bar_items
9703 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9704 {
9705 /* Redisplay that happens asynchronously due to an expose event
9706 may access f->tool_bar_items. Make sure we update both
9707 variables within BLOCK_INPUT so no such event interrupts. */
9708 BLOCK_INPUT;
9709 f->tool_bar_items = new_tool_bar;
9710 f->n_tool_bar_items = new_n_tool_bar;
9711 w->update_mode_line = Qt;
9712 UNBLOCK_INPUT;
9713 }
9714
9715 UNGCPRO;
9716
9717 unbind_to (count, Qnil);
9718 set_buffer_internal_1 (prev);
9719 }
9720 }
9721 }
9722
9723
9724 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9725 F's desired tool-bar contents. F->tool_bar_items must have
9726 been set up previously by calling prepare_menu_bars. */
9727
9728 static void
9729 build_desired_tool_bar_string (f)
9730 struct frame *f;
9731 {
9732 int i, size, size_needed;
9733 struct gcpro gcpro1, gcpro2, gcpro3;
9734 Lisp_Object image, plist, props;
9735
9736 image = plist = props = Qnil;
9737 GCPRO3 (image, plist, props);
9738
9739 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9740 Otherwise, make a new string. */
9741
9742 /* The size of the string we might be able to reuse. */
9743 size = (STRINGP (f->desired_tool_bar_string)
9744 ? SCHARS (f->desired_tool_bar_string)
9745 : 0);
9746
9747 /* We need one space in the string for each image. */
9748 size_needed = f->n_tool_bar_items;
9749
9750 /* Reuse f->desired_tool_bar_string, if possible. */
9751 if (size < size_needed || NILP (f->desired_tool_bar_string))
9752 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9753 make_number (' '));
9754 else
9755 {
9756 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9757 Fremove_text_properties (make_number (0), make_number (size),
9758 props, f->desired_tool_bar_string);
9759 }
9760
9761 /* Put a `display' property on the string for the images to display,
9762 put a `menu_item' property on tool-bar items with a value that
9763 is the index of the item in F's tool-bar item vector. */
9764 for (i = 0; i < f->n_tool_bar_items; ++i)
9765 {
9766 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9767
9768 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9769 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9770 int hmargin, vmargin, relief, idx, end;
9771 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9772
9773 /* If image is a vector, choose the image according to the
9774 button state. */
9775 image = PROP (TOOL_BAR_ITEM_IMAGES);
9776 if (VECTORP (image))
9777 {
9778 if (enabled_p)
9779 idx = (selected_p
9780 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9781 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9782 else
9783 idx = (selected_p
9784 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9785 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9786
9787 xassert (ASIZE (image) >= idx);
9788 image = AREF (image, idx);
9789 }
9790 else
9791 idx = -1;
9792
9793 /* Ignore invalid image specifications. */
9794 if (!valid_image_p (image))
9795 continue;
9796
9797 /* Display the tool-bar button pressed, or depressed. */
9798 plist = Fcopy_sequence (XCDR (image));
9799
9800 /* Compute margin and relief to draw. */
9801 relief = (tool_bar_button_relief >= 0
9802 ? tool_bar_button_relief
9803 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9804 hmargin = vmargin = relief;
9805
9806 if (INTEGERP (Vtool_bar_button_margin)
9807 && XINT (Vtool_bar_button_margin) > 0)
9808 {
9809 hmargin += XFASTINT (Vtool_bar_button_margin);
9810 vmargin += XFASTINT (Vtool_bar_button_margin);
9811 }
9812 else if (CONSP (Vtool_bar_button_margin))
9813 {
9814 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9815 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9816 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9817
9818 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9819 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9820 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9821 }
9822
9823 if (auto_raise_tool_bar_buttons_p)
9824 {
9825 /* Add a `:relief' property to the image spec if the item is
9826 selected. */
9827 if (selected_p)
9828 {
9829 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9830 hmargin -= relief;
9831 vmargin -= relief;
9832 }
9833 }
9834 else
9835 {
9836 /* If image is selected, display it pressed, i.e. with a
9837 negative relief. If it's not selected, display it with a
9838 raised relief. */
9839 plist = Fplist_put (plist, QCrelief,
9840 (selected_p
9841 ? make_number (-relief)
9842 : make_number (relief)));
9843 hmargin -= relief;
9844 vmargin -= relief;
9845 }
9846
9847 /* Put a margin around the image. */
9848 if (hmargin || vmargin)
9849 {
9850 if (hmargin == vmargin)
9851 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9852 else
9853 plist = Fplist_put (plist, QCmargin,
9854 Fcons (make_number (hmargin),
9855 make_number (vmargin)));
9856 }
9857
9858 /* If button is not enabled, and we don't have special images
9859 for the disabled state, make the image appear disabled by
9860 applying an appropriate algorithm to it. */
9861 if (!enabled_p && idx < 0)
9862 plist = Fplist_put (plist, QCconversion, Qdisabled);
9863
9864 /* Put a `display' text property on the string for the image to
9865 display. Put a `menu-item' property on the string that gives
9866 the start of this item's properties in the tool-bar items
9867 vector. */
9868 image = Fcons (Qimage, plist);
9869 props = list4 (Qdisplay, image,
9870 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9871
9872 /* Let the last image hide all remaining spaces in the tool bar
9873 string. The string can be longer than needed when we reuse a
9874 previous string. */
9875 if (i + 1 == f->n_tool_bar_items)
9876 end = SCHARS (f->desired_tool_bar_string);
9877 else
9878 end = i + 1;
9879 Fadd_text_properties (make_number (i), make_number (end),
9880 props, f->desired_tool_bar_string);
9881 #undef PROP
9882 }
9883
9884 UNGCPRO;
9885 }
9886
9887
9888 /* Display one line of the tool-bar of frame IT->f.
9889
9890 HEIGHT specifies the desired height of the tool-bar line.
9891 If the actual height of the glyph row is less than HEIGHT, the
9892 row's height is increased to HEIGHT, and the icons are centered
9893 vertically in the new height.
9894
9895 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9896 count a final empty row in case the tool-bar width exactly matches
9897 the window width.
9898 */
9899
9900 static void
9901 display_tool_bar_line (it, height)
9902 struct it *it;
9903 int height;
9904 {
9905 struct glyph_row *row = it->glyph_row;
9906 int max_x = it->last_visible_x;
9907 struct glyph *last;
9908
9909 prepare_desired_row (row);
9910 row->y = it->current_y;
9911
9912 /* Note that this isn't made use of if the face hasn't a box,
9913 so there's no need to check the face here. */
9914 it->start_of_box_run_p = 1;
9915
9916 while (it->current_x < max_x)
9917 {
9918 int x, n_glyphs_before, i, nglyphs;
9919 struct it it_before;
9920
9921 /* Get the next display element. */
9922 if (!get_next_display_element (it))
9923 {
9924 /* Don't count empty row if we are counting needed tool-bar lines. */
9925 if (height < 0 && !it->hpos)
9926 return;
9927 break;
9928 }
9929
9930 /* Produce glyphs. */
9931 n_glyphs_before = row->used[TEXT_AREA];
9932 it_before = *it;
9933
9934 PRODUCE_GLYPHS (it);
9935
9936 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9937 i = 0;
9938 x = it_before.current_x;
9939 while (i < nglyphs)
9940 {
9941 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9942
9943 if (x + glyph->pixel_width > max_x)
9944 {
9945 /* Glyph doesn't fit on line. Backtrack. */
9946 row->used[TEXT_AREA] = n_glyphs_before;
9947 *it = it_before;
9948 /* If this is the only glyph on this line, it will never fit on the
9949 toolbar, so skip it. But ensure there is at least one glyph,
9950 so we don't accidentally disable the tool-bar. */
9951 if (n_glyphs_before == 0
9952 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9953 break;
9954 goto out;
9955 }
9956
9957 ++it->hpos;
9958 x += glyph->pixel_width;
9959 ++i;
9960 }
9961
9962 /* Stop at line ends. */
9963 if (ITERATOR_AT_END_OF_LINE_P (it))
9964 break;
9965
9966 set_iterator_to_next (it, 1);
9967 }
9968
9969 out:;
9970
9971 row->displays_text_p = row->used[TEXT_AREA] != 0;
9972
9973 /* Use default face for the border below the tool bar.
9974
9975 FIXME: When auto-resize-tool-bars is grow-only, there is
9976 no additional border below the possibly empty tool-bar lines.
9977 So to make the extra empty lines look "normal", we have to
9978 use the tool-bar face for the border too. */
9979 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9980 it->face_id = DEFAULT_FACE_ID;
9981
9982 extend_face_to_end_of_line (it);
9983 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9984 last->right_box_line_p = 1;
9985 if (last == row->glyphs[TEXT_AREA])
9986 last->left_box_line_p = 1;
9987
9988 /* Make line the desired height and center it vertically. */
9989 if ((height -= it->max_ascent + it->max_descent) > 0)
9990 {
9991 /* Don't add more than one line height. */
9992 height %= FRAME_LINE_HEIGHT (it->f);
9993 it->max_ascent += height / 2;
9994 it->max_descent += (height + 1) / 2;
9995 }
9996
9997 compute_line_metrics (it);
9998
9999 /* If line is empty, make it occupy the rest of the tool-bar. */
10000 if (!row->displays_text_p)
10001 {
10002 row->height = row->phys_height = it->last_visible_y - row->y;
10003 row->visible_height = row->height;
10004 row->ascent = row->phys_ascent = 0;
10005 row->extra_line_spacing = 0;
10006 }
10007
10008 row->full_width_p = 1;
10009 row->continued_p = 0;
10010 row->truncated_on_left_p = 0;
10011 row->truncated_on_right_p = 0;
10012
10013 it->current_x = it->hpos = 0;
10014 it->current_y += row->height;
10015 ++it->vpos;
10016 ++it->glyph_row;
10017 }
10018
10019
10020 /* Max tool-bar height. */
10021
10022 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10023 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10024
10025 /* Value is the number of screen lines needed to make all tool-bar
10026 items of frame F visible. The number of actual rows needed is
10027 returned in *N_ROWS if non-NULL. */
10028
10029 static int
10030 tool_bar_lines_needed (f, n_rows)
10031 struct frame *f;
10032 int *n_rows;
10033 {
10034 struct window *w = XWINDOW (f->tool_bar_window);
10035 struct it it;
10036 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10037 the desired matrix, so use (unused) mode-line row as temporary row to
10038 avoid destroying the first tool-bar row. */
10039 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10040
10041 /* Initialize an iterator for iteration over
10042 F->desired_tool_bar_string in the tool-bar window of frame F. */
10043 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10044 it.first_visible_x = 0;
10045 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10046 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10047
10048 while (!ITERATOR_AT_END_P (&it))
10049 {
10050 clear_glyph_row (temp_row);
10051 it.glyph_row = temp_row;
10052 display_tool_bar_line (&it, -1);
10053 }
10054 clear_glyph_row (temp_row);
10055
10056 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10057 if (n_rows)
10058 *n_rows = it.vpos > 0 ? it.vpos : -1;
10059
10060 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10061 }
10062
10063
10064 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10065 0, 1, 0,
10066 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10067 (frame)
10068 Lisp_Object frame;
10069 {
10070 struct frame *f;
10071 struct window *w;
10072 int nlines = 0;
10073
10074 if (NILP (frame))
10075 frame = selected_frame;
10076 else
10077 CHECK_FRAME (frame);
10078 f = XFRAME (frame);
10079
10080 if (WINDOWP (f->tool_bar_window)
10081 || (w = XWINDOW (f->tool_bar_window),
10082 WINDOW_TOTAL_LINES (w) > 0))
10083 {
10084 update_tool_bar (f, 1);
10085 if (f->n_tool_bar_items)
10086 {
10087 build_desired_tool_bar_string (f);
10088 nlines = tool_bar_lines_needed (f, NULL);
10089 }
10090 }
10091
10092 return make_number (nlines);
10093 }
10094
10095
10096 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10097 height should be changed. */
10098
10099 static int
10100 redisplay_tool_bar (f)
10101 struct frame *f;
10102 {
10103 struct window *w;
10104 struct it it;
10105 struct glyph_row *row;
10106
10107 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10108 if (FRAME_EXTERNAL_TOOL_BAR (f))
10109 update_frame_tool_bar (f);
10110 return 0;
10111 #endif
10112
10113 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10114 do anything. This means you must start with tool-bar-lines
10115 non-zero to get the auto-sizing effect. Or in other words, you
10116 can turn off tool-bars by specifying tool-bar-lines zero. */
10117 if (!WINDOWP (f->tool_bar_window)
10118 || (w = XWINDOW (f->tool_bar_window),
10119 WINDOW_TOTAL_LINES (w) == 0))
10120 return 0;
10121
10122 /* Set up an iterator for the tool-bar window. */
10123 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10124 it.first_visible_x = 0;
10125 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10126 row = it.glyph_row;
10127
10128 /* Build a string that represents the contents of the tool-bar. */
10129 build_desired_tool_bar_string (f);
10130 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10131
10132 if (f->n_tool_bar_rows == 0)
10133 {
10134 int nlines;
10135
10136 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10137 nlines != WINDOW_TOTAL_LINES (w)))
10138 {
10139 extern Lisp_Object Qtool_bar_lines;
10140 Lisp_Object frame;
10141 int old_height = WINDOW_TOTAL_LINES (w);
10142
10143 XSETFRAME (frame, f);
10144 Fmodify_frame_parameters (frame,
10145 Fcons (Fcons (Qtool_bar_lines,
10146 make_number (nlines)),
10147 Qnil));
10148 if (WINDOW_TOTAL_LINES (w) != old_height)
10149 {
10150 clear_glyph_matrix (w->desired_matrix);
10151 fonts_changed_p = 1;
10152 return 1;
10153 }
10154 }
10155 }
10156
10157 /* Display as many lines as needed to display all tool-bar items. */
10158
10159 if (f->n_tool_bar_rows > 0)
10160 {
10161 int border, rows, height, extra;
10162
10163 if (INTEGERP (Vtool_bar_border))
10164 border = XINT (Vtool_bar_border);
10165 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10166 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10167 else if (EQ (Vtool_bar_border, Qborder_width))
10168 border = f->border_width;
10169 else
10170 border = 0;
10171 if (border < 0)
10172 border = 0;
10173
10174 rows = f->n_tool_bar_rows;
10175 height = max (1, (it.last_visible_y - border) / rows);
10176 extra = it.last_visible_y - border - height * rows;
10177
10178 while (it.current_y < it.last_visible_y)
10179 {
10180 int h = 0;
10181 if (extra > 0 && rows-- > 0)
10182 {
10183 h = (extra + rows - 1) / rows;
10184 extra -= h;
10185 }
10186 display_tool_bar_line (&it, height + h);
10187 }
10188 }
10189 else
10190 {
10191 while (it.current_y < it.last_visible_y)
10192 display_tool_bar_line (&it, 0);
10193 }
10194
10195 /* It doesn't make much sense to try scrolling in the tool-bar
10196 window, so don't do it. */
10197 w->desired_matrix->no_scrolling_p = 1;
10198 w->must_be_updated_p = 1;
10199
10200 if (!NILP (Vauto_resize_tool_bars))
10201 {
10202 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10203 int change_height_p = 0;
10204
10205 /* If we couldn't display everything, change the tool-bar's
10206 height if there is room for more. */
10207 if (IT_STRING_CHARPOS (it) < it.end_charpos
10208 && it.current_y < max_tool_bar_height)
10209 change_height_p = 1;
10210
10211 row = it.glyph_row - 1;
10212
10213 /* If there are blank lines at the end, except for a partially
10214 visible blank line at the end that is smaller than
10215 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10216 if (!row->displays_text_p
10217 && row->height >= FRAME_LINE_HEIGHT (f))
10218 change_height_p = 1;
10219
10220 /* If row displays tool-bar items, but is partially visible,
10221 change the tool-bar's height. */
10222 if (row->displays_text_p
10223 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10224 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10225 change_height_p = 1;
10226
10227 /* Resize windows as needed by changing the `tool-bar-lines'
10228 frame parameter. */
10229 if (change_height_p)
10230 {
10231 extern Lisp_Object Qtool_bar_lines;
10232 Lisp_Object frame;
10233 int old_height = WINDOW_TOTAL_LINES (w);
10234 int nrows;
10235 int nlines = tool_bar_lines_needed (f, &nrows);
10236
10237 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10238 && !f->minimize_tool_bar_window_p)
10239 ? (nlines > old_height)
10240 : (nlines != old_height));
10241 f->minimize_tool_bar_window_p = 0;
10242
10243 if (change_height_p)
10244 {
10245 XSETFRAME (frame, f);
10246 Fmodify_frame_parameters (frame,
10247 Fcons (Fcons (Qtool_bar_lines,
10248 make_number (nlines)),
10249 Qnil));
10250 if (WINDOW_TOTAL_LINES (w) != old_height)
10251 {
10252 clear_glyph_matrix (w->desired_matrix);
10253 f->n_tool_bar_rows = nrows;
10254 fonts_changed_p = 1;
10255 return 1;
10256 }
10257 }
10258 }
10259 }
10260
10261 f->minimize_tool_bar_window_p = 0;
10262 return 0;
10263 }
10264
10265
10266 /* Get information about the tool-bar item which is displayed in GLYPH
10267 on frame F. Return in *PROP_IDX the index where tool-bar item
10268 properties start in F->tool_bar_items. Value is zero if
10269 GLYPH doesn't display a tool-bar item. */
10270
10271 static int
10272 tool_bar_item_info (f, glyph, prop_idx)
10273 struct frame *f;
10274 struct glyph *glyph;
10275 int *prop_idx;
10276 {
10277 Lisp_Object prop;
10278 int success_p;
10279 int charpos;
10280
10281 /* This function can be called asynchronously, which means we must
10282 exclude any possibility that Fget_text_property signals an
10283 error. */
10284 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10285 charpos = max (0, charpos);
10286
10287 /* Get the text property `menu-item' at pos. The value of that
10288 property is the start index of this item's properties in
10289 F->tool_bar_items. */
10290 prop = Fget_text_property (make_number (charpos),
10291 Qmenu_item, f->current_tool_bar_string);
10292 if (INTEGERP (prop))
10293 {
10294 *prop_idx = XINT (prop);
10295 success_p = 1;
10296 }
10297 else
10298 success_p = 0;
10299
10300 return success_p;
10301 }
10302
10303 \f
10304 /* Get information about the tool-bar item at position X/Y on frame F.
10305 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10306 the current matrix of the tool-bar window of F, or NULL if not
10307 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10308 item in F->tool_bar_items. Value is
10309
10310 -1 if X/Y is not on a tool-bar item
10311 0 if X/Y is on the same item that was highlighted before.
10312 1 otherwise. */
10313
10314 static int
10315 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10316 struct frame *f;
10317 int x, y;
10318 struct glyph **glyph;
10319 int *hpos, *vpos, *prop_idx;
10320 {
10321 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10322 struct window *w = XWINDOW (f->tool_bar_window);
10323 int area;
10324
10325 /* Find the glyph under X/Y. */
10326 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10327 if (*glyph == NULL)
10328 return -1;
10329
10330 /* Get the start of this tool-bar item's properties in
10331 f->tool_bar_items. */
10332 if (!tool_bar_item_info (f, *glyph, prop_idx))
10333 return -1;
10334
10335 /* Is mouse on the highlighted item? */
10336 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10337 && *vpos >= dpyinfo->mouse_face_beg_row
10338 && *vpos <= dpyinfo->mouse_face_end_row
10339 && (*vpos > dpyinfo->mouse_face_beg_row
10340 || *hpos >= dpyinfo->mouse_face_beg_col)
10341 && (*vpos < dpyinfo->mouse_face_end_row
10342 || *hpos < dpyinfo->mouse_face_end_col
10343 || dpyinfo->mouse_face_past_end))
10344 return 0;
10345
10346 return 1;
10347 }
10348
10349
10350 /* EXPORT:
10351 Handle mouse button event on the tool-bar of frame F, at
10352 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10353 0 for button release. MODIFIERS is event modifiers for button
10354 release. */
10355
10356 void
10357 handle_tool_bar_click (f, x, y, down_p, modifiers)
10358 struct frame *f;
10359 int x, y, down_p;
10360 unsigned int modifiers;
10361 {
10362 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10363 struct window *w = XWINDOW (f->tool_bar_window);
10364 int hpos, vpos, prop_idx;
10365 struct glyph *glyph;
10366 Lisp_Object enabled_p;
10367
10368 /* If not on the highlighted tool-bar item, return. */
10369 frame_to_window_pixel_xy (w, &x, &y);
10370 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10371 return;
10372
10373 /* If item is disabled, do nothing. */
10374 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10375 if (NILP (enabled_p))
10376 return;
10377
10378 if (down_p)
10379 {
10380 /* Show item in pressed state. */
10381 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10382 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10383 last_tool_bar_item = prop_idx;
10384 }
10385 else
10386 {
10387 Lisp_Object key, frame;
10388 struct input_event event;
10389 EVENT_INIT (event);
10390
10391 /* Show item in released state. */
10392 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10393 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10394
10395 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10396
10397 XSETFRAME (frame, f);
10398 event.kind = TOOL_BAR_EVENT;
10399 event.frame_or_window = frame;
10400 event.arg = frame;
10401 kbd_buffer_store_event (&event);
10402
10403 event.kind = TOOL_BAR_EVENT;
10404 event.frame_or_window = frame;
10405 event.arg = key;
10406 event.modifiers = modifiers;
10407 kbd_buffer_store_event (&event);
10408 last_tool_bar_item = -1;
10409 }
10410 }
10411
10412
10413 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10414 tool-bar window-relative coordinates X/Y. Called from
10415 note_mouse_highlight. */
10416
10417 static void
10418 note_tool_bar_highlight (f, x, y)
10419 struct frame *f;
10420 int x, y;
10421 {
10422 Lisp_Object window = f->tool_bar_window;
10423 struct window *w = XWINDOW (window);
10424 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10425 int hpos, vpos;
10426 struct glyph *glyph;
10427 struct glyph_row *row;
10428 int i;
10429 Lisp_Object enabled_p;
10430 int prop_idx;
10431 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10432 int mouse_down_p, rc;
10433
10434 /* Function note_mouse_highlight is called with negative x(y
10435 values when mouse moves outside of the frame. */
10436 if (x <= 0 || y <= 0)
10437 {
10438 clear_mouse_face (dpyinfo);
10439 return;
10440 }
10441
10442 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10443 if (rc < 0)
10444 {
10445 /* Not on tool-bar item. */
10446 clear_mouse_face (dpyinfo);
10447 return;
10448 }
10449 else if (rc == 0)
10450 /* On same tool-bar item as before. */
10451 goto set_help_echo;
10452
10453 clear_mouse_face (dpyinfo);
10454
10455 /* Mouse is down, but on different tool-bar item? */
10456 mouse_down_p = (dpyinfo->grabbed
10457 && f == last_mouse_frame
10458 && FRAME_LIVE_P (f));
10459 if (mouse_down_p
10460 && last_tool_bar_item != prop_idx)
10461 return;
10462
10463 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10464 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10465
10466 /* If tool-bar item is not enabled, don't highlight it. */
10467 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10468 if (!NILP (enabled_p))
10469 {
10470 /* Compute the x-position of the glyph. In front and past the
10471 image is a space. We include this in the highlighted area. */
10472 row = MATRIX_ROW (w->current_matrix, vpos);
10473 for (i = x = 0; i < hpos; ++i)
10474 x += row->glyphs[TEXT_AREA][i].pixel_width;
10475
10476 /* Record this as the current active region. */
10477 dpyinfo->mouse_face_beg_col = hpos;
10478 dpyinfo->mouse_face_beg_row = vpos;
10479 dpyinfo->mouse_face_beg_x = x;
10480 dpyinfo->mouse_face_beg_y = row->y;
10481 dpyinfo->mouse_face_past_end = 0;
10482
10483 dpyinfo->mouse_face_end_col = hpos + 1;
10484 dpyinfo->mouse_face_end_row = vpos;
10485 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10486 dpyinfo->mouse_face_end_y = row->y;
10487 dpyinfo->mouse_face_window = window;
10488 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10489
10490 /* Display it as active. */
10491 show_mouse_face (dpyinfo, draw);
10492 dpyinfo->mouse_face_image_state = draw;
10493 }
10494
10495 set_help_echo:
10496
10497 /* Set help_echo_string to a help string to display for this tool-bar item.
10498 XTread_socket does the rest. */
10499 help_echo_object = help_echo_window = Qnil;
10500 help_echo_pos = -1;
10501 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10502 if (NILP (help_echo_string))
10503 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10504 }
10505
10506 #endif /* HAVE_WINDOW_SYSTEM */
10507
10508
10509 \f
10510 /************************************************************************
10511 Horizontal scrolling
10512 ************************************************************************/
10513
10514 static int hscroll_window_tree P_ ((Lisp_Object));
10515 static int hscroll_windows P_ ((Lisp_Object));
10516
10517 /* For all leaf windows in the window tree rooted at WINDOW, set their
10518 hscroll value so that PT is (i) visible in the window, and (ii) so
10519 that it is not within a certain margin at the window's left and
10520 right border. Value is non-zero if any window's hscroll has been
10521 changed. */
10522
10523 static int
10524 hscroll_window_tree (window)
10525 Lisp_Object window;
10526 {
10527 int hscrolled_p = 0;
10528 int hscroll_relative_p = FLOATP (Vhscroll_step);
10529 int hscroll_step_abs = 0;
10530 double hscroll_step_rel = 0;
10531
10532 if (hscroll_relative_p)
10533 {
10534 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10535 if (hscroll_step_rel < 0)
10536 {
10537 hscroll_relative_p = 0;
10538 hscroll_step_abs = 0;
10539 }
10540 }
10541 else if (INTEGERP (Vhscroll_step))
10542 {
10543 hscroll_step_abs = XINT (Vhscroll_step);
10544 if (hscroll_step_abs < 0)
10545 hscroll_step_abs = 0;
10546 }
10547 else
10548 hscroll_step_abs = 0;
10549
10550 while (WINDOWP (window))
10551 {
10552 struct window *w = XWINDOW (window);
10553
10554 if (WINDOWP (w->hchild))
10555 hscrolled_p |= hscroll_window_tree (w->hchild);
10556 else if (WINDOWP (w->vchild))
10557 hscrolled_p |= hscroll_window_tree (w->vchild);
10558 else if (w->cursor.vpos >= 0)
10559 {
10560 int h_margin;
10561 int text_area_width;
10562 struct glyph_row *current_cursor_row
10563 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10564 struct glyph_row *desired_cursor_row
10565 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10566 struct glyph_row *cursor_row
10567 = (desired_cursor_row->enabled_p
10568 ? desired_cursor_row
10569 : current_cursor_row);
10570
10571 text_area_width = window_box_width (w, TEXT_AREA);
10572
10573 /* Scroll when cursor is inside this scroll margin. */
10574 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10575
10576 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10577 && ((XFASTINT (w->hscroll)
10578 && w->cursor.x <= h_margin)
10579 || (cursor_row->enabled_p
10580 && cursor_row->truncated_on_right_p
10581 && (w->cursor.x >= text_area_width - h_margin))))
10582 {
10583 struct it it;
10584 int hscroll;
10585 struct buffer *saved_current_buffer;
10586 int pt;
10587 int wanted_x;
10588
10589 /* Find point in a display of infinite width. */
10590 saved_current_buffer = current_buffer;
10591 current_buffer = XBUFFER (w->buffer);
10592
10593 if (w == XWINDOW (selected_window))
10594 pt = BUF_PT (current_buffer);
10595 else
10596 {
10597 pt = marker_position (w->pointm);
10598 pt = max (BEGV, pt);
10599 pt = min (ZV, pt);
10600 }
10601
10602 /* Move iterator to pt starting at cursor_row->start in
10603 a line with infinite width. */
10604 init_to_row_start (&it, w, cursor_row);
10605 it.last_visible_x = INFINITY;
10606 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10607 current_buffer = saved_current_buffer;
10608
10609 /* Position cursor in window. */
10610 if (!hscroll_relative_p && hscroll_step_abs == 0)
10611 hscroll = max (0, (it.current_x
10612 - (ITERATOR_AT_END_OF_LINE_P (&it)
10613 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10614 : (text_area_width / 2))))
10615 / FRAME_COLUMN_WIDTH (it.f);
10616 else if (w->cursor.x >= text_area_width - h_margin)
10617 {
10618 if (hscroll_relative_p)
10619 wanted_x = text_area_width * (1 - hscroll_step_rel)
10620 - h_margin;
10621 else
10622 wanted_x = text_area_width
10623 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10624 - h_margin;
10625 hscroll
10626 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10627 }
10628 else
10629 {
10630 if (hscroll_relative_p)
10631 wanted_x = text_area_width * hscroll_step_rel
10632 + h_margin;
10633 else
10634 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10635 + h_margin;
10636 hscroll
10637 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10638 }
10639 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10640
10641 /* Don't call Fset_window_hscroll if value hasn't
10642 changed because it will prevent redisplay
10643 optimizations. */
10644 if (XFASTINT (w->hscroll) != hscroll)
10645 {
10646 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10647 w->hscroll = make_number (hscroll);
10648 hscrolled_p = 1;
10649 }
10650 }
10651 }
10652
10653 window = w->next;
10654 }
10655
10656 /* Value is non-zero if hscroll of any leaf window has been changed. */
10657 return hscrolled_p;
10658 }
10659
10660
10661 /* Set hscroll so that cursor is visible and not inside horizontal
10662 scroll margins for all windows in the tree rooted at WINDOW. See
10663 also hscroll_window_tree above. Value is non-zero if any window's
10664 hscroll has been changed. If it has, desired matrices on the frame
10665 of WINDOW are cleared. */
10666
10667 static int
10668 hscroll_windows (window)
10669 Lisp_Object window;
10670 {
10671 int hscrolled_p = hscroll_window_tree (window);
10672 if (hscrolled_p)
10673 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10674 return hscrolled_p;
10675 }
10676
10677
10678 \f
10679 /************************************************************************
10680 Redisplay
10681 ************************************************************************/
10682
10683 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10684 to a non-zero value. This is sometimes handy to have in a debugger
10685 session. */
10686
10687 #if GLYPH_DEBUG
10688
10689 /* First and last unchanged row for try_window_id. */
10690
10691 int debug_first_unchanged_at_end_vpos;
10692 int debug_last_unchanged_at_beg_vpos;
10693
10694 /* Delta vpos and y. */
10695
10696 int debug_dvpos, debug_dy;
10697
10698 /* Delta in characters and bytes for try_window_id. */
10699
10700 int debug_delta, debug_delta_bytes;
10701
10702 /* Values of window_end_pos and window_end_vpos at the end of
10703 try_window_id. */
10704
10705 EMACS_INT debug_end_pos, debug_end_vpos;
10706
10707 /* Append a string to W->desired_matrix->method. FMT is a printf
10708 format string. A1...A9 are a supplement for a variable-length
10709 argument list. If trace_redisplay_p is non-zero also printf the
10710 resulting string to stderr. */
10711
10712 static void
10713 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10714 struct window *w;
10715 char *fmt;
10716 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10717 {
10718 char buffer[512];
10719 char *method = w->desired_matrix->method;
10720 int len = strlen (method);
10721 int size = sizeof w->desired_matrix->method;
10722 int remaining = size - len - 1;
10723
10724 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10725 if (len && remaining)
10726 {
10727 method[len] = '|';
10728 --remaining, ++len;
10729 }
10730
10731 strncpy (method + len, buffer, remaining);
10732
10733 if (trace_redisplay_p)
10734 fprintf (stderr, "%p (%s): %s\n",
10735 w,
10736 ((BUFFERP (w->buffer)
10737 && STRINGP (XBUFFER (w->buffer)->name))
10738 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10739 : "no buffer"),
10740 buffer);
10741 }
10742
10743 #endif /* GLYPH_DEBUG */
10744
10745
10746 /* Value is non-zero if all changes in window W, which displays
10747 current_buffer, are in the text between START and END. START is a
10748 buffer position, END is given as a distance from Z. Used in
10749 redisplay_internal for display optimization. */
10750
10751 static INLINE int
10752 text_outside_line_unchanged_p (w, start, end)
10753 struct window *w;
10754 int start, end;
10755 {
10756 int unchanged_p = 1;
10757
10758 /* If text or overlays have changed, see where. */
10759 if (XFASTINT (w->last_modified) < MODIFF
10760 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10761 {
10762 /* Gap in the line? */
10763 if (GPT < start || Z - GPT < end)
10764 unchanged_p = 0;
10765
10766 /* Changes start in front of the line, or end after it? */
10767 if (unchanged_p
10768 && (BEG_UNCHANGED < start - 1
10769 || END_UNCHANGED < end))
10770 unchanged_p = 0;
10771
10772 /* If selective display, can't optimize if changes start at the
10773 beginning of the line. */
10774 if (unchanged_p
10775 && INTEGERP (current_buffer->selective_display)
10776 && XINT (current_buffer->selective_display) > 0
10777 && (BEG_UNCHANGED < start || GPT <= start))
10778 unchanged_p = 0;
10779
10780 /* If there are overlays at the start or end of the line, these
10781 may have overlay strings with newlines in them. A change at
10782 START, for instance, may actually concern the display of such
10783 overlay strings as well, and they are displayed on different
10784 lines. So, quickly rule out this case. (For the future, it
10785 might be desirable to implement something more telling than
10786 just BEG/END_UNCHANGED.) */
10787 if (unchanged_p)
10788 {
10789 if (BEG + BEG_UNCHANGED == start
10790 && overlay_touches_p (start))
10791 unchanged_p = 0;
10792 if (END_UNCHANGED == end
10793 && overlay_touches_p (Z - end))
10794 unchanged_p = 0;
10795 }
10796 }
10797
10798 return unchanged_p;
10799 }
10800
10801
10802 /* Do a frame update, taking possible shortcuts into account. This is
10803 the main external entry point for redisplay.
10804
10805 If the last redisplay displayed an echo area message and that message
10806 is no longer requested, we clear the echo area or bring back the
10807 mini-buffer if that is in use. */
10808
10809 void
10810 redisplay ()
10811 {
10812 redisplay_internal (0);
10813 }
10814
10815
10816 static Lisp_Object
10817 overlay_arrow_string_or_property (var)
10818 Lisp_Object var;
10819 {
10820 Lisp_Object val;
10821
10822 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10823 return val;
10824
10825 return Voverlay_arrow_string;
10826 }
10827
10828 /* Return 1 if there are any overlay-arrows in current_buffer. */
10829 static int
10830 overlay_arrow_in_current_buffer_p ()
10831 {
10832 Lisp_Object vlist;
10833
10834 for (vlist = Voverlay_arrow_variable_list;
10835 CONSP (vlist);
10836 vlist = XCDR (vlist))
10837 {
10838 Lisp_Object var = XCAR (vlist);
10839 Lisp_Object val;
10840
10841 if (!SYMBOLP (var))
10842 continue;
10843 val = find_symbol_value (var);
10844 if (MARKERP (val)
10845 && current_buffer == XMARKER (val)->buffer)
10846 return 1;
10847 }
10848 return 0;
10849 }
10850
10851
10852 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10853 has changed. */
10854
10855 static int
10856 overlay_arrows_changed_p ()
10857 {
10858 Lisp_Object vlist;
10859
10860 for (vlist = Voverlay_arrow_variable_list;
10861 CONSP (vlist);
10862 vlist = XCDR (vlist))
10863 {
10864 Lisp_Object var = XCAR (vlist);
10865 Lisp_Object val, pstr;
10866
10867 if (!SYMBOLP (var))
10868 continue;
10869 val = find_symbol_value (var);
10870 if (!MARKERP (val))
10871 continue;
10872 if (! EQ (COERCE_MARKER (val),
10873 Fget (var, Qlast_arrow_position))
10874 || ! (pstr = overlay_arrow_string_or_property (var),
10875 EQ (pstr, Fget (var, Qlast_arrow_string))))
10876 return 1;
10877 }
10878 return 0;
10879 }
10880
10881 /* Mark overlay arrows to be updated on next redisplay. */
10882
10883 static void
10884 update_overlay_arrows (up_to_date)
10885 int up_to_date;
10886 {
10887 Lisp_Object vlist;
10888
10889 for (vlist = Voverlay_arrow_variable_list;
10890 CONSP (vlist);
10891 vlist = XCDR (vlist))
10892 {
10893 Lisp_Object var = XCAR (vlist);
10894
10895 if (!SYMBOLP (var))
10896 continue;
10897
10898 if (up_to_date > 0)
10899 {
10900 Lisp_Object val = find_symbol_value (var);
10901 Fput (var, Qlast_arrow_position,
10902 COERCE_MARKER (val));
10903 Fput (var, Qlast_arrow_string,
10904 overlay_arrow_string_or_property (var));
10905 }
10906 else if (up_to_date < 0
10907 || !NILP (Fget (var, Qlast_arrow_position)))
10908 {
10909 Fput (var, Qlast_arrow_position, Qt);
10910 Fput (var, Qlast_arrow_string, Qt);
10911 }
10912 }
10913 }
10914
10915
10916 /* Return overlay arrow string to display at row.
10917 Return integer (bitmap number) for arrow bitmap in left fringe.
10918 Return nil if no overlay arrow. */
10919
10920 static Lisp_Object
10921 overlay_arrow_at_row (it, row)
10922 struct it *it;
10923 struct glyph_row *row;
10924 {
10925 Lisp_Object vlist;
10926
10927 for (vlist = Voverlay_arrow_variable_list;
10928 CONSP (vlist);
10929 vlist = XCDR (vlist))
10930 {
10931 Lisp_Object var = XCAR (vlist);
10932 Lisp_Object val;
10933
10934 if (!SYMBOLP (var))
10935 continue;
10936
10937 val = find_symbol_value (var);
10938
10939 if (MARKERP (val)
10940 && current_buffer == XMARKER (val)->buffer
10941 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10942 {
10943 if (FRAME_WINDOW_P (it->f)
10944 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10945 {
10946 #ifdef HAVE_WINDOW_SYSTEM
10947 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10948 {
10949 int fringe_bitmap;
10950 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10951 return make_number (fringe_bitmap);
10952 }
10953 #endif
10954 return make_number (-1); /* Use default arrow bitmap */
10955 }
10956 return overlay_arrow_string_or_property (var);
10957 }
10958 }
10959
10960 return Qnil;
10961 }
10962
10963 /* Return 1 if point moved out of or into a composition. Otherwise
10964 return 0. PREV_BUF and PREV_PT are the last point buffer and
10965 position. BUF and PT are the current point buffer and position. */
10966
10967 int
10968 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10969 struct buffer *prev_buf, *buf;
10970 int prev_pt, pt;
10971 {
10972 EMACS_INT start, end;
10973 Lisp_Object prop;
10974 Lisp_Object buffer;
10975
10976 XSETBUFFER (buffer, buf);
10977 /* Check a composition at the last point if point moved within the
10978 same buffer. */
10979 if (prev_buf == buf)
10980 {
10981 if (prev_pt == pt)
10982 /* Point didn't move. */
10983 return 0;
10984
10985 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10986 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10987 && COMPOSITION_VALID_P (start, end, prop)
10988 && start < prev_pt && end > prev_pt)
10989 /* The last point was within the composition. Return 1 iff
10990 point moved out of the composition. */
10991 return (pt <= start || pt >= end);
10992 }
10993
10994 /* Check a composition at the current point. */
10995 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10996 && find_composition (pt, -1, &start, &end, &prop, buffer)
10997 && COMPOSITION_VALID_P (start, end, prop)
10998 && start < pt && end > pt);
10999 }
11000
11001
11002 /* Reconsider the setting of B->clip_changed which is displayed
11003 in window W. */
11004
11005 static INLINE void
11006 reconsider_clip_changes (w, b)
11007 struct window *w;
11008 struct buffer *b;
11009 {
11010 if (b->clip_changed
11011 && !NILP (w->window_end_valid)
11012 && w->current_matrix->buffer == b
11013 && w->current_matrix->zv == BUF_ZV (b)
11014 && w->current_matrix->begv == BUF_BEGV (b))
11015 b->clip_changed = 0;
11016
11017 /* If display wasn't paused, and W is not a tool bar window, see if
11018 point has been moved into or out of a composition. In that case,
11019 we set b->clip_changed to 1 to force updating the screen. If
11020 b->clip_changed has already been set to 1, we can skip this
11021 check. */
11022 if (!b->clip_changed
11023 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11024 {
11025 int pt;
11026
11027 if (w == XWINDOW (selected_window))
11028 pt = BUF_PT (current_buffer);
11029 else
11030 pt = marker_position (w->pointm);
11031
11032 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11033 || pt != XINT (w->last_point))
11034 && check_point_in_composition (w->current_matrix->buffer,
11035 XINT (w->last_point),
11036 XBUFFER (w->buffer), pt))
11037 b->clip_changed = 1;
11038 }
11039 }
11040 \f
11041
11042 /* Select FRAME to forward the values of frame-local variables into C
11043 variables so that the redisplay routines can access those values
11044 directly. */
11045
11046 static void
11047 select_frame_for_redisplay (frame)
11048 Lisp_Object frame;
11049 {
11050 Lisp_Object tail, symbol, val;
11051 Lisp_Object old = selected_frame;
11052 struct Lisp_Symbol *sym;
11053
11054 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11055
11056 selected_frame = frame;
11057
11058 do
11059 {
11060 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11061 if (CONSP (XCAR (tail))
11062 && (symbol = XCAR (XCAR (tail)),
11063 SYMBOLP (symbol))
11064 && (sym = indirect_variable (XSYMBOL (symbol)),
11065 val = sym->value,
11066 (BUFFER_LOCAL_VALUEP (val)))
11067 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11068 /* Use find_symbol_value rather than Fsymbol_value
11069 to avoid an error if it is void. */
11070 find_symbol_value (symbol);
11071 } while (!EQ (frame, old) && (frame = old, 1));
11072 }
11073
11074
11075 #define STOP_POLLING \
11076 do { if (! polling_stopped_here) stop_polling (); \
11077 polling_stopped_here = 1; } while (0)
11078
11079 #define RESUME_POLLING \
11080 do { if (polling_stopped_here) start_polling (); \
11081 polling_stopped_here = 0; } while (0)
11082
11083
11084 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11085 response to any user action; therefore, we should preserve the echo
11086 area. (Actually, our caller does that job.) Perhaps in the future
11087 avoid recentering windows if it is not necessary; currently that
11088 causes some problems. */
11089
11090 static void
11091 redisplay_internal (preserve_echo_area)
11092 int preserve_echo_area;
11093 {
11094 struct window *w = XWINDOW (selected_window);
11095 struct frame *f;
11096 int pause;
11097 int must_finish = 0;
11098 struct text_pos tlbufpos, tlendpos;
11099 int number_of_visible_frames;
11100 int count, count1;
11101 struct frame *sf;
11102 int polling_stopped_here = 0;
11103 Lisp_Object old_frame = selected_frame;
11104
11105 /* Non-zero means redisplay has to consider all windows on all
11106 frames. Zero means, only selected_window is considered. */
11107 int consider_all_windows_p;
11108
11109 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11110
11111 /* No redisplay if running in batch mode or frame is not yet fully
11112 initialized, or redisplay is explicitly turned off by setting
11113 Vinhibit_redisplay. */
11114 if (noninteractive
11115 || !NILP (Vinhibit_redisplay))
11116 return;
11117
11118 /* Don't examine these until after testing Vinhibit_redisplay.
11119 When Emacs is shutting down, perhaps because its connection to
11120 X has dropped, we should not look at them at all. */
11121 f = XFRAME (w->frame);
11122 sf = SELECTED_FRAME ();
11123
11124 if (!f->glyphs_initialized_p)
11125 return;
11126
11127 /* The flag redisplay_performed_directly_p is set by
11128 direct_output_for_insert when it already did the whole screen
11129 update necessary. */
11130 if (redisplay_performed_directly_p)
11131 {
11132 redisplay_performed_directly_p = 0;
11133 if (!hscroll_windows (selected_window))
11134 return;
11135 }
11136
11137 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11138 if (popup_activated ())
11139 return;
11140 #endif
11141
11142 /* I don't think this happens but let's be paranoid. */
11143 if (redisplaying_p)
11144 return;
11145
11146 /* Record a function that resets redisplaying_p to its old value
11147 when we leave this function. */
11148 count = SPECPDL_INDEX ();
11149 record_unwind_protect (unwind_redisplay,
11150 Fcons (make_number (redisplaying_p), selected_frame));
11151 ++redisplaying_p;
11152 specbind (Qinhibit_free_realized_faces, Qnil);
11153
11154 {
11155 Lisp_Object tail, frame;
11156
11157 FOR_EACH_FRAME (tail, frame)
11158 {
11159 struct frame *f = XFRAME (frame);
11160 f->already_hscrolled_p = 0;
11161 }
11162 }
11163
11164 retry:
11165 if (!EQ (old_frame, selected_frame)
11166 && FRAME_LIVE_P (XFRAME (old_frame)))
11167 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11168 selected_frame and selected_window to be temporarily out-of-sync so
11169 when we come back here via `goto retry', we need to resync because we
11170 may need to run Elisp code (via prepare_menu_bars). */
11171 select_frame_for_redisplay (old_frame);
11172
11173 pause = 0;
11174 reconsider_clip_changes (w, current_buffer);
11175 last_escape_glyph_frame = NULL;
11176 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11177
11178 /* If new fonts have been loaded that make a glyph matrix adjustment
11179 necessary, do it. */
11180 if (fonts_changed_p)
11181 {
11182 adjust_glyphs (NULL);
11183 ++windows_or_buffers_changed;
11184 fonts_changed_p = 0;
11185 }
11186
11187 /* If face_change_count is non-zero, init_iterator will free all
11188 realized faces, which includes the faces referenced from current
11189 matrices. So, we can't reuse current matrices in this case. */
11190 if (face_change_count)
11191 ++windows_or_buffers_changed;
11192
11193 if (FRAME_TERMCAP_P (sf)
11194 && FRAME_TTY (sf)->previous_frame != sf)
11195 {
11196 /* Since frames on a single ASCII terminal share the same
11197 display area, displaying a different frame means redisplay
11198 the whole thing. */
11199 windows_or_buffers_changed++;
11200 SET_FRAME_GARBAGED (sf);
11201 #ifndef WINDOWSNT
11202 set_tty_color_mode (FRAME_TTY (sf), sf);
11203 #endif
11204 FRAME_TTY (sf)->previous_frame = sf;
11205 }
11206
11207 /* Set the visible flags for all frames. Do this before checking
11208 for resized or garbaged frames; they want to know if their frames
11209 are visible. See the comment in frame.h for
11210 FRAME_SAMPLE_VISIBILITY. */
11211 {
11212 Lisp_Object tail, frame;
11213
11214 number_of_visible_frames = 0;
11215
11216 FOR_EACH_FRAME (tail, frame)
11217 {
11218 struct frame *f = XFRAME (frame);
11219
11220 FRAME_SAMPLE_VISIBILITY (f);
11221 if (FRAME_VISIBLE_P (f))
11222 ++number_of_visible_frames;
11223 clear_desired_matrices (f);
11224 }
11225 }
11226
11227
11228 /* Notice any pending interrupt request to change frame size. */
11229 do_pending_window_change (1);
11230
11231 /* Clear frames marked as garbaged. */
11232 if (frame_garbaged)
11233 clear_garbaged_frames ();
11234
11235 /* Build menubar and tool-bar items. */
11236 if (NILP (Vmemory_full))
11237 prepare_menu_bars ();
11238
11239 if (windows_or_buffers_changed)
11240 update_mode_lines++;
11241
11242 /* Detect case that we need to write or remove a star in the mode line. */
11243 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11244 {
11245 w->update_mode_line = Qt;
11246 if (buffer_shared > 1)
11247 update_mode_lines++;
11248 }
11249
11250 /* Avoid invocation of point motion hooks by `current_column' below. */
11251 count1 = SPECPDL_INDEX ();
11252 specbind (Qinhibit_point_motion_hooks, Qt);
11253
11254 /* If %c is in the mode line, update it if needed. */
11255 if (!NILP (w->column_number_displayed)
11256 /* This alternative quickly identifies a common case
11257 where no change is needed. */
11258 && !(PT == XFASTINT (w->last_point)
11259 && XFASTINT (w->last_modified) >= MODIFF
11260 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11261 && (XFASTINT (w->column_number_displayed)
11262 != (int) current_column ())) /* iftc */
11263 w->update_mode_line = Qt;
11264
11265 unbind_to (count1, Qnil);
11266
11267 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11268
11269 /* The variable buffer_shared is set in redisplay_window and
11270 indicates that we redisplay a buffer in different windows. See
11271 there. */
11272 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11273 || cursor_type_changed);
11274
11275 /* If specs for an arrow have changed, do thorough redisplay
11276 to ensure we remove any arrow that should no longer exist. */
11277 if (overlay_arrows_changed_p ())
11278 consider_all_windows_p = windows_or_buffers_changed = 1;
11279
11280 /* Normally the message* functions will have already displayed and
11281 updated the echo area, but the frame may have been trashed, or
11282 the update may have been preempted, so display the echo area
11283 again here. Checking message_cleared_p captures the case that
11284 the echo area should be cleared. */
11285 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11286 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11287 || (message_cleared_p
11288 && minibuf_level == 0
11289 /* If the mini-window is currently selected, this means the
11290 echo-area doesn't show through. */
11291 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11292 {
11293 int window_height_changed_p = echo_area_display (0);
11294 must_finish = 1;
11295
11296 /* If we don't display the current message, don't clear the
11297 message_cleared_p flag, because, if we did, we wouldn't clear
11298 the echo area in the next redisplay which doesn't preserve
11299 the echo area. */
11300 if (!display_last_displayed_message_p)
11301 message_cleared_p = 0;
11302
11303 if (fonts_changed_p)
11304 goto retry;
11305 else if (window_height_changed_p)
11306 {
11307 consider_all_windows_p = 1;
11308 ++update_mode_lines;
11309 ++windows_or_buffers_changed;
11310
11311 /* If window configuration was changed, frames may have been
11312 marked garbaged. Clear them or we will experience
11313 surprises wrt scrolling. */
11314 if (frame_garbaged)
11315 clear_garbaged_frames ();
11316 }
11317 }
11318 else if (EQ (selected_window, minibuf_window)
11319 && (current_buffer->clip_changed
11320 || XFASTINT (w->last_modified) < MODIFF
11321 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11322 && resize_mini_window (w, 0))
11323 {
11324 /* Resized active mini-window to fit the size of what it is
11325 showing if its contents might have changed. */
11326 must_finish = 1;
11327 consider_all_windows_p = 1;
11328 ++windows_or_buffers_changed;
11329 ++update_mode_lines;
11330
11331 /* If window configuration was changed, frames may have been
11332 marked garbaged. Clear them or we will experience
11333 surprises wrt scrolling. */
11334 if (frame_garbaged)
11335 clear_garbaged_frames ();
11336 }
11337
11338
11339 /* If showing the region, and mark has changed, we must redisplay
11340 the whole window. The assignment to this_line_start_pos prevents
11341 the optimization directly below this if-statement. */
11342 if (((!NILP (Vtransient_mark_mode)
11343 && !NILP (XBUFFER (w->buffer)->mark_active))
11344 != !NILP (w->region_showing))
11345 || (!NILP (w->region_showing)
11346 && !EQ (w->region_showing,
11347 Fmarker_position (XBUFFER (w->buffer)->mark))))
11348 CHARPOS (this_line_start_pos) = 0;
11349
11350 /* Optimize the case that only the line containing the cursor in the
11351 selected window has changed. Variables starting with this_ are
11352 set in display_line and record information about the line
11353 containing the cursor. */
11354 tlbufpos = this_line_start_pos;
11355 tlendpos = this_line_end_pos;
11356 if (!consider_all_windows_p
11357 && CHARPOS (tlbufpos) > 0
11358 && NILP (w->update_mode_line)
11359 && !current_buffer->clip_changed
11360 && !current_buffer->prevent_redisplay_optimizations_p
11361 && FRAME_VISIBLE_P (XFRAME (w->frame))
11362 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11363 /* Make sure recorded data applies to current buffer, etc. */
11364 && this_line_buffer == current_buffer
11365 && current_buffer == XBUFFER (w->buffer)
11366 && NILP (w->force_start)
11367 && NILP (w->optional_new_start)
11368 /* Point must be on the line that we have info recorded about. */
11369 && PT >= CHARPOS (tlbufpos)
11370 && PT <= Z - CHARPOS (tlendpos)
11371 /* All text outside that line, including its final newline,
11372 must be unchanged */
11373 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11374 CHARPOS (tlendpos)))
11375 {
11376 if (CHARPOS (tlbufpos) > BEGV
11377 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11378 && (CHARPOS (tlbufpos) == ZV
11379 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11380 /* Former continuation line has disappeared by becoming empty */
11381 goto cancel;
11382 else if (XFASTINT (w->last_modified) < MODIFF
11383 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11384 || MINI_WINDOW_P (w))
11385 {
11386 /* We have to handle the case of continuation around a
11387 wide-column character (See the comment in indent.c around
11388 line 885).
11389
11390 For instance, in the following case:
11391
11392 -------- Insert --------
11393 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11394 J_I_ ==> J_I_ `^^' are cursors.
11395 ^^ ^^
11396 -------- --------
11397
11398 As we have to redraw the line above, we should goto cancel. */
11399
11400 struct it it;
11401 int line_height_before = this_line_pixel_height;
11402
11403 /* Note that start_display will handle the case that the
11404 line starting at tlbufpos is a continuation lines. */
11405 start_display (&it, w, tlbufpos);
11406
11407 /* Implementation note: It this still necessary? */
11408 if (it.current_x != this_line_start_x)
11409 goto cancel;
11410
11411 TRACE ((stderr, "trying display optimization 1\n"));
11412 w->cursor.vpos = -1;
11413 overlay_arrow_seen = 0;
11414 it.vpos = this_line_vpos;
11415 it.current_y = this_line_y;
11416 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11417 display_line (&it);
11418
11419 /* If line contains point, is not continued,
11420 and ends at same distance from eob as before, we win */
11421 if (w->cursor.vpos >= 0
11422 /* Line is not continued, otherwise this_line_start_pos
11423 would have been set to 0 in display_line. */
11424 && CHARPOS (this_line_start_pos)
11425 /* Line ends as before. */
11426 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11427 /* Line has same height as before. Otherwise other lines
11428 would have to be shifted up or down. */
11429 && this_line_pixel_height == line_height_before)
11430 {
11431 /* If this is not the window's last line, we must adjust
11432 the charstarts of the lines below. */
11433 if (it.current_y < it.last_visible_y)
11434 {
11435 struct glyph_row *row
11436 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11437 int delta, delta_bytes;
11438
11439 if (Z - CHARPOS (tlendpos) == ZV)
11440 {
11441 /* This line ends at end of (accessible part of)
11442 buffer. There is no newline to count. */
11443 delta = (Z
11444 - CHARPOS (tlendpos)
11445 - MATRIX_ROW_START_CHARPOS (row));
11446 delta_bytes = (Z_BYTE
11447 - BYTEPOS (tlendpos)
11448 - MATRIX_ROW_START_BYTEPOS (row));
11449 }
11450 else
11451 {
11452 /* This line ends in a newline. Must take
11453 account of the newline and the rest of the
11454 text that follows. */
11455 delta = (Z
11456 - CHARPOS (tlendpos)
11457 - MATRIX_ROW_START_CHARPOS (row));
11458 delta_bytes = (Z_BYTE
11459 - BYTEPOS (tlendpos)
11460 - MATRIX_ROW_START_BYTEPOS (row));
11461 }
11462
11463 increment_matrix_positions (w->current_matrix,
11464 this_line_vpos + 1,
11465 w->current_matrix->nrows,
11466 delta, delta_bytes);
11467 }
11468
11469 /* If this row displays text now but previously didn't,
11470 or vice versa, w->window_end_vpos may have to be
11471 adjusted. */
11472 if ((it.glyph_row - 1)->displays_text_p)
11473 {
11474 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11475 XSETINT (w->window_end_vpos, this_line_vpos);
11476 }
11477 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11478 && this_line_vpos > 0)
11479 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11480 w->window_end_valid = Qnil;
11481
11482 /* Update hint: No need to try to scroll in update_window. */
11483 w->desired_matrix->no_scrolling_p = 1;
11484
11485 #if GLYPH_DEBUG
11486 *w->desired_matrix->method = 0;
11487 debug_method_add (w, "optimization 1");
11488 #endif
11489 #ifdef HAVE_WINDOW_SYSTEM
11490 update_window_fringes (w, 0);
11491 #endif
11492 goto update;
11493 }
11494 else
11495 goto cancel;
11496 }
11497 else if (/* Cursor position hasn't changed. */
11498 PT == XFASTINT (w->last_point)
11499 /* Make sure the cursor was last displayed
11500 in this window. Otherwise we have to reposition it. */
11501 && 0 <= w->cursor.vpos
11502 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11503 {
11504 if (!must_finish)
11505 {
11506 do_pending_window_change (1);
11507
11508 /* We used to always goto end_of_redisplay here, but this
11509 isn't enough if we have a blinking cursor. */
11510 if (w->cursor_off_p == w->last_cursor_off_p)
11511 goto end_of_redisplay;
11512 }
11513 goto update;
11514 }
11515 /* If highlighting the region, or if the cursor is in the echo area,
11516 then we can't just move the cursor. */
11517 else if (! (!NILP (Vtransient_mark_mode)
11518 && !NILP (current_buffer->mark_active))
11519 && (EQ (selected_window, current_buffer->last_selected_window)
11520 || highlight_nonselected_windows)
11521 && NILP (w->region_showing)
11522 && NILP (Vshow_trailing_whitespace)
11523 && !cursor_in_echo_area)
11524 {
11525 struct it it;
11526 struct glyph_row *row;
11527
11528 /* Skip from tlbufpos to PT and see where it is. Note that
11529 PT may be in invisible text. If so, we will end at the
11530 next visible position. */
11531 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11532 NULL, DEFAULT_FACE_ID);
11533 it.current_x = this_line_start_x;
11534 it.current_y = this_line_y;
11535 it.vpos = this_line_vpos;
11536
11537 /* The call to move_it_to stops in front of PT, but
11538 moves over before-strings. */
11539 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11540
11541 if (it.vpos == this_line_vpos
11542 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11543 row->enabled_p))
11544 {
11545 xassert (this_line_vpos == it.vpos);
11546 xassert (this_line_y == it.current_y);
11547 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11548 #if GLYPH_DEBUG
11549 *w->desired_matrix->method = 0;
11550 debug_method_add (w, "optimization 3");
11551 #endif
11552 goto update;
11553 }
11554 else
11555 goto cancel;
11556 }
11557
11558 cancel:
11559 /* Text changed drastically or point moved off of line. */
11560 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11561 }
11562
11563 CHARPOS (this_line_start_pos) = 0;
11564 consider_all_windows_p |= buffer_shared > 1;
11565 ++clear_face_cache_count;
11566 #ifdef HAVE_WINDOW_SYSTEM
11567 ++clear_image_cache_count;
11568 #endif
11569
11570 /* Build desired matrices, and update the display. If
11571 consider_all_windows_p is non-zero, do it for all windows on all
11572 frames. Otherwise do it for selected_window, only. */
11573
11574 if (consider_all_windows_p)
11575 {
11576 Lisp_Object tail, frame;
11577
11578 FOR_EACH_FRAME (tail, frame)
11579 XFRAME (frame)->updated_p = 0;
11580
11581 /* Recompute # windows showing selected buffer. This will be
11582 incremented each time such a window is displayed. */
11583 buffer_shared = 0;
11584
11585 FOR_EACH_FRAME (tail, frame)
11586 {
11587 struct frame *f = XFRAME (frame);
11588
11589 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11590 {
11591 if (! EQ (frame, selected_frame))
11592 /* Select the frame, for the sake of frame-local
11593 variables. */
11594 select_frame_for_redisplay (frame);
11595
11596 /* Mark all the scroll bars to be removed; we'll redeem
11597 the ones we want when we redisplay their windows. */
11598 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11599 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11600
11601 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11602 redisplay_windows (FRAME_ROOT_WINDOW (f));
11603
11604 /* Any scroll bars which redisplay_windows should have
11605 nuked should now go away. */
11606 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11607 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11608
11609 /* If fonts changed, display again. */
11610 /* ??? rms: I suspect it is a mistake to jump all the way
11611 back to retry here. It should just retry this frame. */
11612 if (fonts_changed_p)
11613 goto retry;
11614
11615 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11616 {
11617 /* See if we have to hscroll. */
11618 if (!f->already_hscrolled_p)
11619 {
11620 f->already_hscrolled_p = 1;
11621 if (hscroll_windows (f->root_window))
11622 goto retry;
11623 }
11624
11625 /* Prevent various kinds of signals during display
11626 update. stdio is not robust about handling
11627 signals, which can cause an apparent I/O
11628 error. */
11629 if (interrupt_input)
11630 unrequest_sigio ();
11631 STOP_POLLING;
11632
11633 /* Update the display. */
11634 set_window_update_flags (XWINDOW (f->root_window), 1);
11635 pause |= update_frame (f, 0, 0);
11636 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11637 if (pause)
11638 break;
11639 #endif
11640
11641 f->updated_p = 1;
11642 }
11643 }
11644 }
11645
11646 if (!EQ (old_frame, selected_frame)
11647 && FRAME_LIVE_P (XFRAME (old_frame)))
11648 /* We played a bit fast-and-loose above and allowed selected_frame
11649 and selected_window to be temporarily out-of-sync but let's make
11650 sure this stays contained. */
11651 select_frame_for_redisplay (old_frame);
11652 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11653
11654 if (!pause)
11655 {
11656 /* Do the mark_window_display_accurate after all windows have
11657 been redisplayed because this call resets flags in buffers
11658 which are needed for proper redisplay. */
11659 FOR_EACH_FRAME (tail, frame)
11660 {
11661 struct frame *f = XFRAME (frame);
11662 if (f->updated_p)
11663 {
11664 mark_window_display_accurate (f->root_window, 1);
11665 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11666 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11667 }
11668 }
11669 }
11670 }
11671 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11672 {
11673 Lisp_Object mini_window;
11674 struct frame *mini_frame;
11675
11676 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11677 /* Use list_of_error, not Qerror, so that
11678 we catch only errors and don't run the debugger. */
11679 internal_condition_case_1 (redisplay_window_1, selected_window,
11680 list_of_error,
11681 redisplay_window_error);
11682
11683 /* Compare desired and current matrices, perform output. */
11684
11685 update:
11686 /* If fonts changed, display again. */
11687 if (fonts_changed_p)
11688 goto retry;
11689
11690 /* Prevent various kinds of signals during display update.
11691 stdio is not robust about handling signals,
11692 which can cause an apparent I/O error. */
11693 if (interrupt_input)
11694 unrequest_sigio ();
11695 STOP_POLLING;
11696
11697 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11698 {
11699 if (hscroll_windows (selected_window))
11700 goto retry;
11701
11702 XWINDOW (selected_window)->must_be_updated_p = 1;
11703 pause = update_frame (sf, 0, 0);
11704 }
11705
11706 /* We may have called echo_area_display at the top of this
11707 function. If the echo area is on another frame, that may
11708 have put text on a frame other than the selected one, so the
11709 above call to update_frame would not have caught it. Catch
11710 it here. */
11711 mini_window = FRAME_MINIBUF_WINDOW (sf);
11712 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11713
11714 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11715 {
11716 XWINDOW (mini_window)->must_be_updated_p = 1;
11717 pause |= update_frame (mini_frame, 0, 0);
11718 if (!pause && hscroll_windows (mini_window))
11719 goto retry;
11720 }
11721 }
11722
11723 /* If display was paused because of pending input, make sure we do a
11724 thorough update the next time. */
11725 if (pause)
11726 {
11727 /* Prevent the optimization at the beginning of
11728 redisplay_internal that tries a single-line update of the
11729 line containing the cursor in the selected window. */
11730 CHARPOS (this_line_start_pos) = 0;
11731
11732 /* Let the overlay arrow be updated the next time. */
11733 update_overlay_arrows (0);
11734
11735 /* If we pause after scrolling, some rows in the current
11736 matrices of some windows are not valid. */
11737 if (!WINDOW_FULL_WIDTH_P (w)
11738 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11739 update_mode_lines = 1;
11740 }
11741 else
11742 {
11743 if (!consider_all_windows_p)
11744 {
11745 /* This has already been done above if
11746 consider_all_windows_p is set. */
11747 mark_window_display_accurate_1 (w, 1);
11748
11749 /* Say overlay arrows are up to date. */
11750 update_overlay_arrows (1);
11751
11752 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11753 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11754 }
11755
11756 update_mode_lines = 0;
11757 windows_or_buffers_changed = 0;
11758 cursor_type_changed = 0;
11759 }
11760
11761 /* Start SIGIO interrupts coming again. Having them off during the
11762 code above makes it less likely one will discard output, but not
11763 impossible, since there might be stuff in the system buffer here.
11764 But it is much hairier to try to do anything about that. */
11765 if (interrupt_input)
11766 request_sigio ();
11767 RESUME_POLLING;
11768
11769 /* If a frame has become visible which was not before, redisplay
11770 again, so that we display it. Expose events for such a frame
11771 (which it gets when becoming visible) don't call the parts of
11772 redisplay constructing glyphs, so simply exposing a frame won't
11773 display anything in this case. So, we have to display these
11774 frames here explicitly. */
11775 if (!pause)
11776 {
11777 Lisp_Object tail, frame;
11778 int new_count = 0;
11779
11780 FOR_EACH_FRAME (tail, frame)
11781 {
11782 int this_is_visible = 0;
11783
11784 if (XFRAME (frame)->visible)
11785 this_is_visible = 1;
11786 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11787 if (XFRAME (frame)->visible)
11788 this_is_visible = 1;
11789
11790 if (this_is_visible)
11791 new_count++;
11792 }
11793
11794 if (new_count != number_of_visible_frames)
11795 windows_or_buffers_changed++;
11796 }
11797
11798 /* Change frame size now if a change is pending. */
11799 do_pending_window_change (1);
11800
11801 /* If we just did a pending size change, or have additional
11802 visible frames, redisplay again. */
11803 if (windows_or_buffers_changed && !pause)
11804 goto retry;
11805
11806 /* Clear the face cache eventually. */
11807 if (consider_all_windows_p)
11808 {
11809 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11810 {
11811 clear_face_cache (0);
11812 clear_face_cache_count = 0;
11813 }
11814 #ifdef HAVE_WINDOW_SYSTEM
11815 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11816 {
11817 clear_image_caches (Qnil);
11818 clear_image_cache_count = 0;
11819 }
11820 #endif /* HAVE_WINDOW_SYSTEM */
11821 }
11822
11823 end_of_redisplay:
11824 unbind_to (count, Qnil);
11825 RESUME_POLLING;
11826 }
11827
11828
11829 /* Redisplay, but leave alone any recent echo area message unless
11830 another message has been requested in its place.
11831
11832 This is useful in situations where you need to redisplay but no
11833 user action has occurred, making it inappropriate for the message
11834 area to be cleared. See tracking_off and
11835 wait_reading_process_output for examples of these situations.
11836
11837 FROM_WHERE is an integer saying from where this function was
11838 called. This is useful for debugging. */
11839
11840 void
11841 redisplay_preserve_echo_area (from_where)
11842 int from_where;
11843 {
11844 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11845
11846 if (!NILP (echo_area_buffer[1]))
11847 {
11848 /* We have a previously displayed message, but no current
11849 message. Redisplay the previous message. */
11850 display_last_displayed_message_p = 1;
11851 redisplay_internal (1);
11852 display_last_displayed_message_p = 0;
11853 }
11854 else
11855 redisplay_internal (1);
11856
11857 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11858 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11859 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11860 }
11861
11862
11863 /* Function registered with record_unwind_protect in
11864 redisplay_internal. Reset redisplaying_p to the value it had
11865 before redisplay_internal was called, and clear
11866 prevent_freeing_realized_faces_p. It also selects the previously
11867 selected frame, unless it has been deleted (by an X connection
11868 failure during redisplay, for example). */
11869
11870 static Lisp_Object
11871 unwind_redisplay (val)
11872 Lisp_Object val;
11873 {
11874 Lisp_Object old_redisplaying_p, old_frame;
11875
11876 old_redisplaying_p = XCAR (val);
11877 redisplaying_p = XFASTINT (old_redisplaying_p);
11878 old_frame = XCDR (val);
11879 if (! EQ (old_frame, selected_frame)
11880 && FRAME_LIVE_P (XFRAME (old_frame)))
11881 select_frame_for_redisplay (old_frame);
11882 return Qnil;
11883 }
11884
11885
11886 /* Mark the display of window W as accurate or inaccurate. If
11887 ACCURATE_P is non-zero mark display of W as accurate. If
11888 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11889 redisplay_internal is called. */
11890
11891 static void
11892 mark_window_display_accurate_1 (w, accurate_p)
11893 struct window *w;
11894 int accurate_p;
11895 {
11896 if (BUFFERP (w->buffer))
11897 {
11898 struct buffer *b = XBUFFER (w->buffer);
11899
11900 w->last_modified
11901 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11902 w->last_overlay_modified
11903 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11904 w->last_had_star
11905 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11906
11907 if (accurate_p)
11908 {
11909 b->clip_changed = 0;
11910 b->prevent_redisplay_optimizations_p = 0;
11911
11912 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11913 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11914 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11915 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11916
11917 w->current_matrix->buffer = b;
11918 w->current_matrix->begv = BUF_BEGV (b);
11919 w->current_matrix->zv = BUF_ZV (b);
11920
11921 w->last_cursor = w->cursor;
11922 w->last_cursor_off_p = w->cursor_off_p;
11923
11924 if (w == XWINDOW (selected_window))
11925 w->last_point = make_number (BUF_PT (b));
11926 else
11927 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11928 }
11929 }
11930
11931 if (accurate_p)
11932 {
11933 w->window_end_valid = w->buffer;
11934 #if 0 /* This is incorrect with variable-height lines. */
11935 xassert (XINT (w->window_end_vpos)
11936 < (WINDOW_TOTAL_LINES (w)
11937 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11938 #endif
11939 w->update_mode_line = Qnil;
11940 }
11941 }
11942
11943
11944 /* Mark the display of windows in the window tree rooted at WINDOW as
11945 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11946 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11947 be redisplayed the next time redisplay_internal is called. */
11948
11949 void
11950 mark_window_display_accurate (window, accurate_p)
11951 Lisp_Object window;
11952 int accurate_p;
11953 {
11954 struct window *w;
11955
11956 for (; !NILP (window); window = w->next)
11957 {
11958 w = XWINDOW (window);
11959 mark_window_display_accurate_1 (w, accurate_p);
11960
11961 if (!NILP (w->vchild))
11962 mark_window_display_accurate (w->vchild, accurate_p);
11963 if (!NILP (w->hchild))
11964 mark_window_display_accurate (w->hchild, accurate_p);
11965 }
11966
11967 if (accurate_p)
11968 {
11969 update_overlay_arrows (1);
11970 }
11971 else
11972 {
11973 /* Force a thorough redisplay the next time by setting
11974 last_arrow_position and last_arrow_string to t, which is
11975 unequal to any useful value of Voverlay_arrow_... */
11976 update_overlay_arrows (-1);
11977 }
11978 }
11979
11980
11981 /* Return value in display table DP (Lisp_Char_Table *) for character
11982 C. Since a display table doesn't have any parent, we don't have to
11983 follow parent. Do not call this function directly but use the
11984 macro DISP_CHAR_VECTOR. */
11985
11986 Lisp_Object
11987 disp_char_vector (dp, c)
11988 struct Lisp_Char_Table *dp;
11989 int c;
11990 {
11991 Lisp_Object val;
11992
11993 if (ASCII_CHAR_P (c))
11994 {
11995 val = dp->ascii;
11996 if (SUB_CHAR_TABLE_P (val))
11997 val = XSUB_CHAR_TABLE (val)->contents[c];
11998 }
11999 else
12000 {
12001 Lisp_Object table;
12002
12003 XSETCHAR_TABLE (table, dp);
12004 val = char_table_ref (table, c);
12005 }
12006 if (NILP (val))
12007 val = dp->defalt;
12008 return val;
12009 }
12010
12011
12012 \f
12013 /***********************************************************************
12014 Window Redisplay
12015 ***********************************************************************/
12016
12017 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12018
12019 static void
12020 redisplay_windows (window)
12021 Lisp_Object window;
12022 {
12023 while (!NILP (window))
12024 {
12025 struct window *w = XWINDOW (window);
12026
12027 if (!NILP (w->hchild))
12028 redisplay_windows (w->hchild);
12029 else if (!NILP (w->vchild))
12030 redisplay_windows (w->vchild);
12031 else
12032 {
12033 displayed_buffer = XBUFFER (w->buffer);
12034 /* Use list_of_error, not Qerror, so that
12035 we catch only errors and don't run the debugger. */
12036 internal_condition_case_1 (redisplay_window_0, window,
12037 list_of_error,
12038 redisplay_window_error);
12039 }
12040
12041 window = w->next;
12042 }
12043 }
12044
12045 static Lisp_Object
12046 redisplay_window_error ()
12047 {
12048 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12049 return Qnil;
12050 }
12051
12052 static Lisp_Object
12053 redisplay_window_0 (window)
12054 Lisp_Object window;
12055 {
12056 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12057 redisplay_window (window, 0);
12058 return Qnil;
12059 }
12060
12061 static Lisp_Object
12062 redisplay_window_1 (window)
12063 Lisp_Object window;
12064 {
12065 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12066 redisplay_window (window, 1);
12067 return Qnil;
12068 }
12069 \f
12070
12071 /* Increment GLYPH until it reaches END or CONDITION fails while
12072 adding (GLYPH)->pixel_width to X. */
12073
12074 #define SKIP_GLYPHS(glyph, end, x, condition) \
12075 do \
12076 { \
12077 (x) += (glyph)->pixel_width; \
12078 ++(glyph); \
12079 } \
12080 while ((glyph) < (end) && (condition))
12081
12082
12083 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12084 DELTA is the number of bytes by which positions recorded in ROW
12085 differ from current buffer positions.
12086
12087 Return 0 if cursor is not on this row. 1 otherwise. */
12088
12089 int
12090 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12091 struct window *w;
12092 struct glyph_row *row;
12093 struct glyph_matrix *matrix;
12094 int delta, delta_bytes, dy, dvpos;
12095 {
12096 struct glyph *glyph = row->glyphs[TEXT_AREA];
12097 struct glyph *end = glyph + row->used[TEXT_AREA];
12098 struct glyph *cursor = NULL;
12099 /* The first glyph that starts a sequence of glyphs from string. */
12100 struct glyph *string_start;
12101 /* The X coordinate of string_start. */
12102 int string_start_x;
12103 /* The last known character position. */
12104 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12105 /* The last known character position before string_start. */
12106 int string_before_pos;
12107 int x = row->x;
12108 int cursor_x = x;
12109 int cursor_from_overlay_pos = 0;
12110 int pt_old = PT - delta;
12111
12112 /* Skip over glyphs not having an object at the start of the row.
12113 These are special glyphs like truncation marks on terminal
12114 frames. */
12115 if (row->displays_text_p)
12116 while (glyph < end
12117 && INTEGERP (glyph->object)
12118 && glyph->charpos < 0)
12119 {
12120 x += glyph->pixel_width;
12121 ++glyph;
12122 }
12123
12124 string_start = NULL;
12125 while (glyph < end
12126 && !INTEGERP (glyph->object)
12127 && (!BUFFERP (glyph->object)
12128 || (last_pos = glyph->charpos) < pt_old))
12129 {
12130 if (! STRINGP (glyph->object))
12131 {
12132 string_start = NULL;
12133 x += glyph->pixel_width;
12134 ++glyph;
12135 if (cursor_from_overlay_pos
12136 && last_pos >= cursor_from_overlay_pos)
12137 {
12138 cursor_from_overlay_pos = 0;
12139 cursor = 0;
12140 }
12141 }
12142 else
12143 {
12144 if (string_start == NULL)
12145 {
12146 string_before_pos = last_pos;
12147 string_start = glyph;
12148 string_start_x = x;
12149 }
12150 /* Skip all glyphs from string. */
12151 do
12152 {
12153 Lisp_Object cprop;
12154 int pos;
12155 if ((cursor == NULL || glyph > cursor)
12156 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12157 Qcursor, (glyph)->object),
12158 !NILP (cprop))
12159 && (pos = string_buffer_position (w, glyph->object,
12160 string_before_pos),
12161 (pos == 0 /* From overlay */
12162 || pos == pt_old)))
12163 {
12164 /* Estimate overlay buffer position from the buffer
12165 positions of the glyphs before and after the overlay.
12166 Add 1 to last_pos so that if point corresponds to the
12167 glyph right after the overlay, we still use a 'cursor'
12168 property found in that overlay. */
12169 cursor_from_overlay_pos = (pos ? 0 : last_pos
12170 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12171 cursor = glyph;
12172 cursor_x = x;
12173 }
12174 x += glyph->pixel_width;
12175 ++glyph;
12176 }
12177 while (glyph < end && EQ (glyph->object, string_start->object));
12178 }
12179 }
12180
12181 if (cursor != NULL)
12182 {
12183 glyph = cursor;
12184 x = cursor_x;
12185 }
12186 else if (row->ends_in_ellipsis_p && glyph == end)
12187 {
12188 /* Scan back over the ellipsis glyphs, decrementing positions. */
12189 while (glyph > row->glyphs[TEXT_AREA]
12190 && (glyph - 1)->charpos == last_pos)
12191 glyph--, x -= glyph->pixel_width;
12192 /* That loop always goes one position too far,
12193 including the glyph before the ellipsis.
12194 So scan forward over that one. */
12195 x += glyph->pixel_width;
12196 glyph++;
12197 }
12198 else if (string_start
12199 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12200 {
12201 /* We may have skipped over point because the previous glyphs
12202 are from string. As there's no easy way to know the
12203 character position of the current glyph, find the correct
12204 glyph on point by scanning from string_start again. */
12205 Lisp_Object limit;
12206 Lisp_Object string;
12207 struct glyph *stop = glyph;
12208 int pos;
12209
12210 limit = make_number (pt_old + 1);
12211 glyph = string_start;
12212 x = string_start_x;
12213 string = glyph->object;
12214 pos = string_buffer_position (w, string, string_before_pos);
12215 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12216 because we always put cursor after overlay strings. */
12217 while (pos == 0 && glyph < stop)
12218 {
12219 string = glyph->object;
12220 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12221 if (glyph < stop)
12222 pos = string_buffer_position (w, glyph->object, string_before_pos);
12223 }
12224
12225 while (glyph < stop)
12226 {
12227 pos = XINT (Fnext_single_char_property_change
12228 (make_number (pos), Qdisplay, Qnil, limit));
12229 if (pos > pt_old)
12230 break;
12231 /* Skip glyphs from the same string. */
12232 string = glyph->object;
12233 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12234 /* Skip glyphs from an overlay. */
12235 while (glyph < stop
12236 && ! string_buffer_position (w, glyph->object, pos))
12237 {
12238 string = glyph->object;
12239 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12240 }
12241 }
12242
12243 /* If we reached the end of the line, and end was from a string,
12244 cursor is not on this line. */
12245 if (glyph == end && row->continued_p)
12246 return 0;
12247 }
12248
12249 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12250 w->cursor.x = x;
12251 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12252 w->cursor.y = row->y + dy;
12253
12254 if (w == XWINDOW (selected_window))
12255 {
12256 if (!row->continued_p
12257 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12258 && row->x == 0)
12259 {
12260 this_line_buffer = XBUFFER (w->buffer);
12261
12262 CHARPOS (this_line_start_pos)
12263 = MATRIX_ROW_START_CHARPOS (row) + delta;
12264 BYTEPOS (this_line_start_pos)
12265 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12266
12267 CHARPOS (this_line_end_pos)
12268 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12269 BYTEPOS (this_line_end_pos)
12270 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12271
12272 this_line_y = w->cursor.y;
12273 this_line_pixel_height = row->height;
12274 this_line_vpos = w->cursor.vpos;
12275 this_line_start_x = row->x;
12276 }
12277 else
12278 CHARPOS (this_line_start_pos) = 0;
12279 }
12280
12281 return 1;
12282 }
12283
12284
12285 /* Run window scroll functions, if any, for WINDOW with new window
12286 start STARTP. Sets the window start of WINDOW to that position.
12287
12288 We assume that the window's buffer is really current. */
12289
12290 static INLINE struct text_pos
12291 run_window_scroll_functions (window, startp)
12292 Lisp_Object window;
12293 struct text_pos startp;
12294 {
12295 struct window *w = XWINDOW (window);
12296 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12297
12298 if (current_buffer != XBUFFER (w->buffer))
12299 abort ();
12300
12301 if (!NILP (Vwindow_scroll_functions))
12302 {
12303 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12304 make_number (CHARPOS (startp)));
12305 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12306 /* In case the hook functions switch buffers. */
12307 if (current_buffer != XBUFFER (w->buffer))
12308 set_buffer_internal_1 (XBUFFER (w->buffer));
12309 }
12310
12311 return startp;
12312 }
12313
12314
12315 /* Make sure the line containing the cursor is fully visible.
12316 A value of 1 means there is nothing to be done.
12317 (Either the line is fully visible, or it cannot be made so,
12318 or we cannot tell.)
12319
12320 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12321 is higher than window.
12322
12323 A value of 0 means the caller should do scrolling
12324 as if point had gone off the screen. */
12325
12326 static int
12327 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12328 struct window *w;
12329 int force_p;
12330 int current_matrix_p;
12331 {
12332 struct glyph_matrix *matrix;
12333 struct glyph_row *row;
12334 int window_height;
12335
12336 if (!make_cursor_line_fully_visible_p)
12337 return 1;
12338
12339 /* It's not always possible to find the cursor, e.g, when a window
12340 is full of overlay strings. Don't do anything in that case. */
12341 if (w->cursor.vpos < 0)
12342 return 1;
12343
12344 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12345 row = MATRIX_ROW (matrix, w->cursor.vpos);
12346
12347 /* If the cursor row is not partially visible, there's nothing to do. */
12348 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12349 return 1;
12350
12351 /* If the row the cursor is in is taller than the window's height,
12352 it's not clear what to do, so do nothing. */
12353 window_height = window_box_height (w);
12354 if (row->height >= window_height)
12355 {
12356 if (!force_p || MINI_WINDOW_P (w)
12357 || w->vscroll || w->cursor.vpos == 0)
12358 return 1;
12359 }
12360 return 0;
12361
12362 #if 0
12363 /* This code used to try to scroll the window just enough to make
12364 the line visible. It returned 0 to say that the caller should
12365 allocate larger glyph matrices. */
12366
12367 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12368 {
12369 int dy = row->height - row->visible_height;
12370 w->vscroll = 0;
12371 w->cursor.y += dy;
12372 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12373 }
12374 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12375 {
12376 int dy = - (row->height - row->visible_height);
12377 w->vscroll = dy;
12378 w->cursor.y += dy;
12379 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12380 }
12381
12382 /* When we change the cursor y-position of the selected window,
12383 change this_line_y as well so that the display optimization for
12384 the cursor line of the selected window in redisplay_internal uses
12385 the correct y-position. */
12386 if (w == XWINDOW (selected_window))
12387 this_line_y = w->cursor.y;
12388
12389 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12390 redisplay with larger matrices. */
12391 if (matrix->nrows < required_matrix_height (w))
12392 {
12393 fonts_changed_p = 1;
12394 return 0;
12395 }
12396
12397 return 1;
12398 #endif /* 0 */
12399 }
12400
12401
12402 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12403 non-zero means only WINDOW is redisplayed in redisplay_internal.
12404 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12405 in redisplay_window to bring a partially visible line into view in
12406 the case that only the cursor has moved.
12407
12408 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12409 last screen line's vertical height extends past the end of the screen.
12410
12411 Value is
12412
12413 1 if scrolling succeeded
12414
12415 0 if scrolling didn't find point.
12416
12417 -1 if new fonts have been loaded so that we must interrupt
12418 redisplay, adjust glyph matrices, and try again. */
12419
12420 enum
12421 {
12422 SCROLLING_SUCCESS,
12423 SCROLLING_FAILED,
12424 SCROLLING_NEED_LARGER_MATRICES
12425 };
12426
12427 static int
12428 try_scrolling (window, just_this_one_p, scroll_conservatively,
12429 scroll_step, temp_scroll_step, last_line_misfit)
12430 Lisp_Object window;
12431 int just_this_one_p;
12432 EMACS_INT scroll_conservatively, scroll_step;
12433 int temp_scroll_step;
12434 int last_line_misfit;
12435 {
12436 struct window *w = XWINDOW (window);
12437 struct frame *f = XFRAME (w->frame);
12438 struct text_pos scroll_margin_pos;
12439 struct text_pos pos;
12440 struct text_pos startp;
12441 struct it it;
12442 Lisp_Object window_end;
12443 int this_scroll_margin;
12444 int dy = 0;
12445 int scroll_max;
12446 int rc;
12447 int amount_to_scroll = 0;
12448 Lisp_Object aggressive;
12449 int height;
12450 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12451
12452 #if GLYPH_DEBUG
12453 debug_method_add (w, "try_scrolling");
12454 #endif
12455
12456 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12457
12458 /* Compute scroll margin height in pixels. We scroll when point is
12459 within this distance from the top or bottom of the window. */
12460 if (scroll_margin > 0)
12461 {
12462 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12463 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12464 }
12465 else
12466 this_scroll_margin = 0;
12467
12468 /* Force scroll_conservatively to have a reasonable value so it doesn't
12469 cause an overflow while computing how much to scroll. */
12470 if (scroll_conservatively)
12471 scroll_conservatively = min (scroll_conservatively,
12472 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12473
12474 /* Compute how much we should try to scroll maximally to bring point
12475 into view. */
12476 if (scroll_step || scroll_conservatively || temp_scroll_step)
12477 scroll_max = max (scroll_step,
12478 max (scroll_conservatively, temp_scroll_step));
12479 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12480 || NUMBERP (current_buffer->scroll_up_aggressively))
12481 /* We're trying to scroll because of aggressive scrolling
12482 but no scroll_step is set. Choose an arbitrary one. Maybe
12483 there should be a variable for this. */
12484 scroll_max = 10;
12485 else
12486 scroll_max = 0;
12487 scroll_max *= FRAME_LINE_HEIGHT (f);
12488
12489 /* Decide whether we have to scroll down. Start at the window end
12490 and move this_scroll_margin up to find the position of the scroll
12491 margin. */
12492 window_end = Fwindow_end (window, Qt);
12493
12494 too_near_end:
12495
12496 CHARPOS (scroll_margin_pos) = XINT (window_end);
12497 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12498
12499 if (this_scroll_margin || extra_scroll_margin_lines)
12500 {
12501 start_display (&it, w, scroll_margin_pos);
12502 if (this_scroll_margin)
12503 move_it_vertically_backward (&it, this_scroll_margin);
12504 if (extra_scroll_margin_lines)
12505 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12506 scroll_margin_pos = it.current.pos;
12507 }
12508
12509 if (PT >= CHARPOS (scroll_margin_pos))
12510 {
12511 int y0;
12512
12513 /* Point is in the scroll margin at the bottom of the window, or
12514 below. Compute a new window start that makes point visible. */
12515
12516 /* Compute the distance from the scroll margin to PT.
12517 Give up if the distance is greater than scroll_max. */
12518 start_display (&it, w, scroll_margin_pos);
12519 y0 = it.current_y;
12520 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12521 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12522
12523 /* To make point visible, we have to move the window start
12524 down so that the line the cursor is in is visible, which
12525 means we have to add in the height of the cursor line. */
12526 dy = line_bottom_y (&it) - y0;
12527
12528 if (dy > scroll_max)
12529 return SCROLLING_FAILED;
12530
12531 /* Move the window start down. If scrolling conservatively,
12532 move it just enough down to make point visible. If
12533 scroll_step is set, move it down by scroll_step. */
12534 start_display (&it, w, startp);
12535
12536 if (scroll_conservatively)
12537 /* Set AMOUNT_TO_SCROLL to at least one line,
12538 and at most scroll_conservatively lines. */
12539 amount_to_scroll
12540 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12541 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12542 else if (scroll_step || temp_scroll_step)
12543 amount_to_scroll = scroll_max;
12544 else
12545 {
12546 aggressive = current_buffer->scroll_up_aggressively;
12547 height = WINDOW_BOX_TEXT_HEIGHT (w);
12548 if (NUMBERP (aggressive))
12549 {
12550 double float_amount = XFLOATINT (aggressive) * height;
12551 amount_to_scroll = float_amount;
12552 if (amount_to_scroll == 0 && float_amount > 0)
12553 amount_to_scroll = 1;
12554 }
12555 }
12556
12557 if (amount_to_scroll <= 0)
12558 return SCROLLING_FAILED;
12559
12560 /* If moving by amount_to_scroll leaves STARTP unchanged,
12561 move it down one screen line. */
12562
12563 move_it_vertically (&it, amount_to_scroll);
12564 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12565 move_it_by_lines (&it, 1, 1);
12566 startp = it.current.pos;
12567 }
12568 else
12569 {
12570 /* See if point is inside the scroll margin at the top of the
12571 window. */
12572 scroll_margin_pos = startp;
12573 if (this_scroll_margin)
12574 {
12575 start_display (&it, w, startp);
12576 move_it_vertically (&it, this_scroll_margin);
12577 scroll_margin_pos = it.current.pos;
12578 }
12579
12580 if (PT < CHARPOS (scroll_margin_pos))
12581 {
12582 /* Point is in the scroll margin at the top of the window or
12583 above what is displayed in the window. */
12584 int y0;
12585
12586 /* Compute the vertical distance from PT to the scroll
12587 margin position. Give up if distance is greater than
12588 scroll_max. */
12589 SET_TEXT_POS (pos, PT, PT_BYTE);
12590 start_display (&it, w, pos);
12591 y0 = it.current_y;
12592 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12593 it.last_visible_y, -1,
12594 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12595 dy = it.current_y - y0;
12596 if (dy > scroll_max)
12597 return SCROLLING_FAILED;
12598
12599 /* Compute new window start. */
12600 start_display (&it, w, startp);
12601
12602 if (scroll_conservatively)
12603 amount_to_scroll
12604 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12605 else if (scroll_step || temp_scroll_step)
12606 amount_to_scroll = scroll_max;
12607 else
12608 {
12609 aggressive = current_buffer->scroll_down_aggressively;
12610 height = WINDOW_BOX_TEXT_HEIGHT (w);
12611 if (NUMBERP (aggressive))
12612 {
12613 double float_amount = XFLOATINT (aggressive) * height;
12614 amount_to_scroll = float_amount;
12615 if (amount_to_scroll == 0 && float_amount > 0)
12616 amount_to_scroll = 1;
12617 }
12618 }
12619
12620 if (amount_to_scroll <= 0)
12621 return SCROLLING_FAILED;
12622
12623 move_it_vertically_backward (&it, amount_to_scroll);
12624 startp = it.current.pos;
12625 }
12626 }
12627
12628 /* Run window scroll functions. */
12629 startp = run_window_scroll_functions (window, startp);
12630
12631 /* Display the window. Give up if new fonts are loaded, or if point
12632 doesn't appear. */
12633 if (!try_window (window, startp, 0))
12634 rc = SCROLLING_NEED_LARGER_MATRICES;
12635 else if (w->cursor.vpos < 0)
12636 {
12637 clear_glyph_matrix (w->desired_matrix);
12638 rc = SCROLLING_FAILED;
12639 }
12640 else
12641 {
12642 /* Maybe forget recorded base line for line number display. */
12643 if (!just_this_one_p
12644 || current_buffer->clip_changed
12645 || BEG_UNCHANGED < CHARPOS (startp))
12646 w->base_line_number = Qnil;
12647
12648 /* If cursor ends up on a partially visible line,
12649 treat that as being off the bottom of the screen. */
12650 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12651 {
12652 clear_glyph_matrix (w->desired_matrix);
12653 ++extra_scroll_margin_lines;
12654 goto too_near_end;
12655 }
12656 rc = SCROLLING_SUCCESS;
12657 }
12658
12659 return rc;
12660 }
12661
12662
12663 /* Compute a suitable window start for window W if display of W starts
12664 on a continuation line. Value is non-zero if a new window start
12665 was computed.
12666
12667 The new window start will be computed, based on W's width, starting
12668 from the start of the continued line. It is the start of the
12669 screen line with the minimum distance from the old start W->start. */
12670
12671 static int
12672 compute_window_start_on_continuation_line (w)
12673 struct window *w;
12674 {
12675 struct text_pos pos, start_pos;
12676 int window_start_changed_p = 0;
12677
12678 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12679
12680 /* If window start is on a continuation line... Window start may be
12681 < BEGV in case there's invisible text at the start of the
12682 buffer (M-x rmail, for example). */
12683 if (CHARPOS (start_pos) > BEGV
12684 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12685 {
12686 struct it it;
12687 struct glyph_row *row;
12688
12689 /* Handle the case that the window start is out of range. */
12690 if (CHARPOS (start_pos) < BEGV)
12691 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12692 else if (CHARPOS (start_pos) > ZV)
12693 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12694
12695 /* Find the start of the continued line. This should be fast
12696 because scan_buffer is fast (newline cache). */
12697 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12698 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12699 row, DEFAULT_FACE_ID);
12700 reseat_at_previous_visible_line_start (&it);
12701
12702 /* If the line start is "too far" away from the window start,
12703 say it takes too much time to compute a new window start. */
12704 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12705 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12706 {
12707 int min_distance, distance;
12708
12709 /* Move forward by display lines to find the new window
12710 start. If window width was enlarged, the new start can
12711 be expected to be > the old start. If window width was
12712 decreased, the new window start will be < the old start.
12713 So, we're looking for the display line start with the
12714 minimum distance from the old window start. */
12715 pos = it.current.pos;
12716 min_distance = INFINITY;
12717 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12718 distance < min_distance)
12719 {
12720 min_distance = distance;
12721 pos = it.current.pos;
12722 move_it_by_lines (&it, 1, 0);
12723 }
12724
12725 /* Set the window start there. */
12726 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12727 window_start_changed_p = 1;
12728 }
12729 }
12730
12731 return window_start_changed_p;
12732 }
12733
12734
12735 /* Try cursor movement in case text has not changed in window WINDOW,
12736 with window start STARTP. Value is
12737
12738 CURSOR_MOVEMENT_SUCCESS if successful
12739
12740 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12741
12742 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12743 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12744 we want to scroll as if scroll-step were set to 1. See the code.
12745
12746 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12747 which case we have to abort this redisplay, and adjust matrices
12748 first. */
12749
12750 enum
12751 {
12752 CURSOR_MOVEMENT_SUCCESS,
12753 CURSOR_MOVEMENT_CANNOT_BE_USED,
12754 CURSOR_MOVEMENT_MUST_SCROLL,
12755 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12756 };
12757
12758 static int
12759 try_cursor_movement (window, startp, scroll_step)
12760 Lisp_Object window;
12761 struct text_pos startp;
12762 int *scroll_step;
12763 {
12764 struct window *w = XWINDOW (window);
12765 struct frame *f = XFRAME (w->frame);
12766 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12767
12768 #if GLYPH_DEBUG
12769 if (inhibit_try_cursor_movement)
12770 return rc;
12771 #endif
12772
12773 /* Handle case where text has not changed, only point, and it has
12774 not moved off the frame. */
12775 if (/* Point may be in this window. */
12776 PT >= CHARPOS (startp)
12777 /* Selective display hasn't changed. */
12778 && !current_buffer->clip_changed
12779 /* Function force-mode-line-update is used to force a thorough
12780 redisplay. It sets either windows_or_buffers_changed or
12781 update_mode_lines. So don't take a shortcut here for these
12782 cases. */
12783 && !update_mode_lines
12784 && !windows_or_buffers_changed
12785 && !cursor_type_changed
12786 /* Can't use this case if highlighting a region. When a
12787 region exists, cursor movement has to do more than just
12788 set the cursor. */
12789 && !(!NILP (Vtransient_mark_mode)
12790 && !NILP (current_buffer->mark_active))
12791 && NILP (w->region_showing)
12792 && NILP (Vshow_trailing_whitespace)
12793 /* Right after splitting windows, last_point may be nil. */
12794 && INTEGERP (w->last_point)
12795 /* This code is not used for mini-buffer for the sake of the case
12796 of redisplaying to replace an echo area message; since in
12797 that case the mini-buffer contents per se are usually
12798 unchanged. This code is of no real use in the mini-buffer
12799 since the handling of this_line_start_pos, etc., in redisplay
12800 handles the same cases. */
12801 && !EQ (window, minibuf_window)
12802 /* When splitting windows or for new windows, it happens that
12803 redisplay is called with a nil window_end_vpos or one being
12804 larger than the window. This should really be fixed in
12805 window.c. I don't have this on my list, now, so we do
12806 approximately the same as the old redisplay code. --gerd. */
12807 && INTEGERP (w->window_end_vpos)
12808 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12809 && (FRAME_WINDOW_P (f)
12810 || !overlay_arrow_in_current_buffer_p ()))
12811 {
12812 int this_scroll_margin, top_scroll_margin;
12813 struct glyph_row *row = NULL;
12814
12815 #if GLYPH_DEBUG
12816 debug_method_add (w, "cursor movement");
12817 #endif
12818
12819 /* Scroll if point within this distance from the top or bottom
12820 of the window. This is a pixel value. */
12821 this_scroll_margin = max (0, scroll_margin);
12822 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12823 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12824
12825 top_scroll_margin = this_scroll_margin;
12826 if (WINDOW_WANTS_HEADER_LINE_P (w))
12827 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12828
12829 /* Start with the row the cursor was displayed during the last
12830 not paused redisplay. Give up if that row is not valid. */
12831 if (w->last_cursor.vpos < 0
12832 || w->last_cursor.vpos >= w->current_matrix->nrows)
12833 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12834 else
12835 {
12836 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12837 if (row->mode_line_p)
12838 ++row;
12839 if (!row->enabled_p)
12840 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12841 }
12842
12843 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12844 {
12845 int scroll_p = 0;
12846 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12847
12848 if (PT > XFASTINT (w->last_point))
12849 {
12850 /* Point has moved forward. */
12851 while (MATRIX_ROW_END_CHARPOS (row) < PT
12852 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12853 {
12854 xassert (row->enabled_p);
12855 ++row;
12856 }
12857
12858 /* The end position of a row equals the start position
12859 of the next row. If PT is there, we would rather
12860 display it in the next line. */
12861 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12862 && MATRIX_ROW_END_CHARPOS (row) == PT
12863 && !cursor_row_p (w, row))
12864 ++row;
12865
12866 /* If within the scroll margin, scroll. Note that
12867 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12868 the next line would be drawn, and that
12869 this_scroll_margin can be zero. */
12870 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12871 || PT > MATRIX_ROW_END_CHARPOS (row)
12872 /* Line is completely visible last line in window
12873 and PT is to be set in the next line. */
12874 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12875 && PT == MATRIX_ROW_END_CHARPOS (row)
12876 && !row->ends_at_zv_p
12877 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12878 scroll_p = 1;
12879 }
12880 else if (PT < XFASTINT (w->last_point))
12881 {
12882 /* Cursor has to be moved backward. Note that PT >=
12883 CHARPOS (startp) because of the outer if-statement. */
12884 while (!row->mode_line_p
12885 && (MATRIX_ROW_START_CHARPOS (row) > PT
12886 || (MATRIX_ROW_START_CHARPOS (row) == PT
12887 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12888 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12889 row > w->current_matrix->rows
12890 && (row-1)->ends_in_newline_from_string_p))))
12891 && (row->y > top_scroll_margin
12892 || CHARPOS (startp) == BEGV))
12893 {
12894 xassert (row->enabled_p);
12895 --row;
12896 }
12897
12898 /* Consider the following case: Window starts at BEGV,
12899 there is invisible, intangible text at BEGV, so that
12900 display starts at some point START > BEGV. It can
12901 happen that we are called with PT somewhere between
12902 BEGV and START. Try to handle that case. */
12903 if (row < w->current_matrix->rows
12904 || row->mode_line_p)
12905 {
12906 row = w->current_matrix->rows;
12907 if (row->mode_line_p)
12908 ++row;
12909 }
12910
12911 /* Due to newlines in overlay strings, we may have to
12912 skip forward over overlay strings. */
12913 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12914 && MATRIX_ROW_END_CHARPOS (row) == PT
12915 && !cursor_row_p (w, row))
12916 ++row;
12917
12918 /* If within the scroll margin, scroll. */
12919 if (row->y < top_scroll_margin
12920 && CHARPOS (startp) != BEGV)
12921 scroll_p = 1;
12922 }
12923 else
12924 {
12925 /* Cursor did not move. So don't scroll even if cursor line
12926 is partially visible, as it was so before. */
12927 rc = CURSOR_MOVEMENT_SUCCESS;
12928 }
12929
12930 if (PT < MATRIX_ROW_START_CHARPOS (row)
12931 || PT > MATRIX_ROW_END_CHARPOS (row))
12932 {
12933 /* if PT is not in the glyph row, give up. */
12934 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12935 }
12936 else if (rc != CURSOR_MOVEMENT_SUCCESS
12937 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12938 && make_cursor_line_fully_visible_p)
12939 {
12940 if (PT == MATRIX_ROW_END_CHARPOS (row)
12941 && !row->ends_at_zv_p
12942 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12943 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12944 else if (row->height > window_box_height (w))
12945 {
12946 /* If we end up in a partially visible line, let's
12947 make it fully visible, except when it's taller
12948 than the window, in which case we can't do much
12949 about it. */
12950 *scroll_step = 1;
12951 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12952 }
12953 else
12954 {
12955 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12956 if (!cursor_row_fully_visible_p (w, 0, 1))
12957 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12958 else
12959 rc = CURSOR_MOVEMENT_SUCCESS;
12960 }
12961 }
12962 else if (scroll_p)
12963 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12964 else
12965 {
12966 do
12967 {
12968 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12969 {
12970 rc = CURSOR_MOVEMENT_SUCCESS;
12971 break;
12972 }
12973 ++row;
12974 }
12975 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12976 && MATRIX_ROW_START_CHARPOS (row) == PT
12977 && cursor_row_p (w, row));
12978 }
12979 }
12980 }
12981
12982 return rc;
12983 }
12984
12985 void
12986 set_vertical_scroll_bar (w)
12987 struct window *w;
12988 {
12989 int start, end, whole;
12990
12991 /* Calculate the start and end positions for the current window.
12992 At some point, it would be nice to choose between scrollbars
12993 which reflect the whole buffer size, with special markers
12994 indicating narrowing, and scrollbars which reflect only the
12995 visible region.
12996
12997 Note that mini-buffers sometimes aren't displaying any text. */
12998 if (!MINI_WINDOW_P (w)
12999 || (w == XWINDOW (minibuf_window)
13000 && NILP (echo_area_buffer[0])))
13001 {
13002 struct buffer *buf = XBUFFER (w->buffer);
13003 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13004 start = marker_position (w->start) - BUF_BEGV (buf);
13005 /* I don't think this is guaranteed to be right. For the
13006 moment, we'll pretend it is. */
13007 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13008
13009 if (end < start)
13010 end = start;
13011 if (whole < (end - start))
13012 whole = end - start;
13013 }
13014 else
13015 start = end = whole = 0;
13016
13017 /* Indicate what this scroll bar ought to be displaying now. */
13018 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13019 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13020 (w, end - start, whole, start);
13021 }
13022
13023
13024 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13025 selected_window is redisplayed.
13026
13027 We can return without actually redisplaying the window if
13028 fonts_changed_p is nonzero. In that case, redisplay_internal will
13029 retry. */
13030
13031 static void
13032 redisplay_window (window, just_this_one_p)
13033 Lisp_Object window;
13034 int just_this_one_p;
13035 {
13036 struct window *w = XWINDOW (window);
13037 struct frame *f = XFRAME (w->frame);
13038 struct buffer *buffer = XBUFFER (w->buffer);
13039 struct buffer *old = current_buffer;
13040 struct text_pos lpoint, opoint, startp;
13041 int update_mode_line;
13042 int tem;
13043 struct it it;
13044 /* Record it now because it's overwritten. */
13045 int current_matrix_up_to_date_p = 0;
13046 int used_current_matrix_p = 0;
13047 /* This is less strict than current_matrix_up_to_date_p.
13048 It indictes that the buffer contents and narrowing are unchanged. */
13049 int buffer_unchanged_p = 0;
13050 int temp_scroll_step = 0;
13051 int count = SPECPDL_INDEX ();
13052 int rc;
13053 int centering_position = -1;
13054 int last_line_misfit = 0;
13055 int beg_unchanged, end_unchanged;
13056
13057 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13058 opoint = lpoint;
13059
13060 /* W must be a leaf window here. */
13061 xassert (!NILP (w->buffer));
13062 #if GLYPH_DEBUG
13063 *w->desired_matrix->method = 0;
13064 #endif
13065
13066 restart:
13067 reconsider_clip_changes (w, buffer);
13068
13069 /* Has the mode line to be updated? */
13070 update_mode_line = (!NILP (w->update_mode_line)
13071 || update_mode_lines
13072 || buffer->clip_changed
13073 || buffer->prevent_redisplay_optimizations_p);
13074
13075 if (MINI_WINDOW_P (w))
13076 {
13077 if (w == XWINDOW (echo_area_window)
13078 && !NILP (echo_area_buffer[0]))
13079 {
13080 if (update_mode_line)
13081 /* We may have to update a tty frame's menu bar or a
13082 tool-bar. Example `M-x C-h C-h C-g'. */
13083 goto finish_menu_bars;
13084 else
13085 /* We've already displayed the echo area glyphs in this window. */
13086 goto finish_scroll_bars;
13087 }
13088 else if ((w != XWINDOW (minibuf_window)
13089 || minibuf_level == 0)
13090 /* When buffer is nonempty, redisplay window normally. */
13091 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13092 /* Quail displays non-mini buffers in minibuffer window.
13093 In that case, redisplay the window normally. */
13094 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13095 {
13096 /* W is a mini-buffer window, but it's not active, so clear
13097 it. */
13098 int yb = window_text_bottom_y (w);
13099 struct glyph_row *row;
13100 int y;
13101
13102 for (y = 0, row = w->desired_matrix->rows;
13103 y < yb;
13104 y += row->height, ++row)
13105 blank_row (w, row, y);
13106 goto finish_scroll_bars;
13107 }
13108
13109 clear_glyph_matrix (w->desired_matrix);
13110 }
13111
13112 /* Otherwise set up data on this window; select its buffer and point
13113 value. */
13114 /* Really select the buffer, for the sake of buffer-local
13115 variables. */
13116 set_buffer_internal_1 (XBUFFER (w->buffer));
13117
13118 current_matrix_up_to_date_p
13119 = (!NILP (w->window_end_valid)
13120 && !current_buffer->clip_changed
13121 && !current_buffer->prevent_redisplay_optimizations_p
13122 && XFASTINT (w->last_modified) >= MODIFF
13123 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13124
13125 /* Run the window-bottom-change-functions
13126 if it is possible that the text on the screen has changed
13127 (either due to modification of the text, or any other reason). */
13128 if (!current_matrix_up_to_date_p
13129 && !NILP (Vwindow_text_change_functions))
13130 {
13131 safe_run_hooks (Qwindow_text_change_functions);
13132 goto restart;
13133 }
13134
13135 beg_unchanged = BEG_UNCHANGED;
13136 end_unchanged = END_UNCHANGED;
13137
13138 SET_TEXT_POS (opoint, PT, PT_BYTE);
13139
13140 specbind (Qinhibit_point_motion_hooks, Qt);
13141
13142 buffer_unchanged_p
13143 = (!NILP (w->window_end_valid)
13144 && !current_buffer->clip_changed
13145 && XFASTINT (w->last_modified) >= MODIFF
13146 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13147
13148 /* When windows_or_buffers_changed is non-zero, we can't rely on
13149 the window end being valid, so set it to nil there. */
13150 if (windows_or_buffers_changed)
13151 {
13152 /* If window starts on a continuation line, maybe adjust the
13153 window start in case the window's width changed. */
13154 if (XMARKER (w->start)->buffer == current_buffer)
13155 compute_window_start_on_continuation_line (w);
13156
13157 w->window_end_valid = Qnil;
13158 }
13159
13160 /* Some sanity checks. */
13161 CHECK_WINDOW_END (w);
13162 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13163 abort ();
13164 if (BYTEPOS (opoint) < CHARPOS (opoint))
13165 abort ();
13166
13167 /* If %c is in mode line, update it if needed. */
13168 if (!NILP (w->column_number_displayed)
13169 /* This alternative quickly identifies a common case
13170 where no change is needed. */
13171 && !(PT == XFASTINT (w->last_point)
13172 && XFASTINT (w->last_modified) >= MODIFF
13173 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13174 && (XFASTINT (w->column_number_displayed)
13175 != (int) current_column ())) /* iftc */
13176 update_mode_line = 1;
13177
13178 /* Count number of windows showing the selected buffer. An indirect
13179 buffer counts as its base buffer. */
13180 if (!just_this_one_p)
13181 {
13182 struct buffer *current_base, *window_base;
13183 current_base = current_buffer;
13184 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13185 if (current_base->base_buffer)
13186 current_base = current_base->base_buffer;
13187 if (window_base->base_buffer)
13188 window_base = window_base->base_buffer;
13189 if (current_base == window_base)
13190 buffer_shared++;
13191 }
13192
13193 /* Point refers normally to the selected window. For any other
13194 window, set up appropriate value. */
13195 if (!EQ (window, selected_window))
13196 {
13197 int new_pt = XMARKER (w->pointm)->charpos;
13198 int new_pt_byte = marker_byte_position (w->pointm);
13199 if (new_pt < BEGV)
13200 {
13201 new_pt = BEGV;
13202 new_pt_byte = BEGV_BYTE;
13203 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13204 }
13205 else if (new_pt > (ZV - 1))
13206 {
13207 new_pt = ZV;
13208 new_pt_byte = ZV_BYTE;
13209 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13210 }
13211
13212 /* We don't use SET_PT so that the point-motion hooks don't run. */
13213 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13214 }
13215
13216 /* If any of the character widths specified in the display table
13217 have changed, invalidate the width run cache. It's true that
13218 this may be a bit late to catch such changes, but the rest of
13219 redisplay goes (non-fatally) haywire when the display table is
13220 changed, so why should we worry about doing any better? */
13221 if (current_buffer->width_run_cache)
13222 {
13223 struct Lisp_Char_Table *disptab = buffer_display_table ();
13224
13225 if (! disptab_matches_widthtab (disptab,
13226 XVECTOR (current_buffer->width_table)))
13227 {
13228 invalidate_region_cache (current_buffer,
13229 current_buffer->width_run_cache,
13230 BEG, Z);
13231 recompute_width_table (current_buffer, disptab);
13232 }
13233 }
13234
13235 /* If window-start is screwed up, choose a new one. */
13236 if (XMARKER (w->start)->buffer != current_buffer)
13237 goto recenter;
13238
13239 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13240
13241 /* If someone specified a new starting point but did not insist,
13242 check whether it can be used. */
13243 if (!NILP (w->optional_new_start)
13244 && CHARPOS (startp) >= BEGV
13245 && CHARPOS (startp) <= ZV)
13246 {
13247 w->optional_new_start = Qnil;
13248 start_display (&it, w, startp);
13249 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13250 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13251 if (IT_CHARPOS (it) == PT)
13252 w->force_start = Qt;
13253 /* IT may overshoot PT if text at PT is invisible. */
13254 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13255 w->force_start = Qt;
13256 }
13257
13258 force_start:
13259
13260 /* Handle case where place to start displaying has been specified,
13261 unless the specified location is outside the accessible range. */
13262 if (!NILP (w->force_start)
13263 || w->frozen_window_start_p)
13264 {
13265 /* We set this later on if we have to adjust point. */
13266 int new_vpos = -1;
13267 int val;
13268
13269 w->force_start = Qnil;
13270 w->vscroll = 0;
13271 w->window_end_valid = Qnil;
13272
13273 /* Forget any recorded base line for line number display. */
13274 if (!buffer_unchanged_p)
13275 w->base_line_number = Qnil;
13276
13277 /* Redisplay the mode line. Select the buffer properly for that.
13278 Also, run the hook window-scroll-functions
13279 because we have scrolled. */
13280 /* Note, we do this after clearing force_start because
13281 if there's an error, it is better to forget about force_start
13282 than to get into an infinite loop calling the hook functions
13283 and having them get more errors. */
13284 if (!update_mode_line
13285 || ! NILP (Vwindow_scroll_functions))
13286 {
13287 update_mode_line = 1;
13288 w->update_mode_line = Qt;
13289 startp = run_window_scroll_functions (window, startp);
13290 }
13291
13292 w->last_modified = make_number (0);
13293 w->last_overlay_modified = make_number (0);
13294 if (CHARPOS (startp) < BEGV)
13295 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13296 else if (CHARPOS (startp) > ZV)
13297 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13298
13299 /* Redisplay, then check if cursor has been set during the
13300 redisplay. Give up if new fonts were loaded. */
13301 val = try_window (window, startp, 1);
13302 if (!val)
13303 {
13304 w->force_start = Qt;
13305 clear_glyph_matrix (w->desired_matrix);
13306 goto need_larger_matrices;
13307 }
13308 /* Point was outside the scroll margins. */
13309 if (val < 0)
13310 new_vpos = window_box_height (w) / 2;
13311
13312 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13313 {
13314 /* If point does not appear, try to move point so it does
13315 appear. The desired matrix has been built above, so we
13316 can use it here. */
13317 new_vpos = window_box_height (w) / 2;
13318 }
13319
13320 if (!cursor_row_fully_visible_p (w, 0, 0))
13321 {
13322 /* Point does appear, but on a line partly visible at end of window.
13323 Move it back to a fully-visible line. */
13324 new_vpos = window_box_height (w);
13325 }
13326
13327 /* If we need to move point for either of the above reasons,
13328 now actually do it. */
13329 if (new_vpos >= 0)
13330 {
13331 struct glyph_row *row;
13332
13333 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13334 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13335 ++row;
13336
13337 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13338 MATRIX_ROW_START_BYTEPOS (row));
13339
13340 if (w != XWINDOW (selected_window))
13341 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13342 else if (current_buffer == old)
13343 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13344
13345 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13346
13347 /* If we are highlighting the region, then we just changed
13348 the region, so redisplay to show it. */
13349 if (!NILP (Vtransient_mark_mode)
13350 && !NILP (current_buffer->mark_active))
13351 {
13352 clear_glyph_matrix (w->desired_matrix);
13353 if (!try_window (window, startp, 0))
13354 goto need_larger_matrices;
13355 }
13356 }
13357
13358 #if GLYPH_DEBUG
13359 debug_method_add (w, "forced window start");
13360 #endif
13361 goto done;
13362 }
13363
13364 /* Handle case where text has not changed, only point, and it has
13365 not moved off the frame, and we are not retrying after hscroll.
13366 (current_matrix_up_to_date_p is nonzero when retrying.) */
13367 if (current_matrix_up_to_date_p
13368 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13369 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13370 {
13371 switch (rc)
13372 {
13373 case CURSOR_MOVEMENT_SUCCESS:
13374 used_current_matrix_p = 1;
13375 goto done;
13376
13377 #if 0 /* try_cursor_movement never returns this value. */
13378 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13379 goto need_larger_matrices;
13380 #endif
13381
13382 case CURSOR_MOVEMENT_MUST_SCROLL:
13383 goto try_to_scroll;
13384
13385 default:
13386 abort ();
13387 }
13388 }
13389 /* If current starting point was originally the beginning of a line
13390 but no longer is, find a new starting point. */
13391 else if (!NILP (w->start_at_line_beg)
13392 && !(CHARPOS (startp) <= BEGV
13393 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13394 {
13395 #if GLYPH_DEBUG
13396 debug_method_add (w, "recenter 1");
13397 #endif
13398 goto recenter;
13399 }
13400
13401 /* Try scrolling with try_window_id. Value is > 0 if update has
13402 been done, it is -1 if we know that the same window start will
13403 not work. It is 0 if unsuccessful for some other reason. */
13404 else if ((tem = try_window_id (w)) != 0)
13405 {
13406 #if GLYPH_DEBUG
13407 debug_method_add (w, "try_window_id %d", tem);
13408 #endif
13409
13410 if (fonts_changed_p)
13411 goto need_larger_matrices;
13412 if (tem > 0)
13413 goto done;
13414
13415 /* Otherwise try_window_id has returned -1 which means that we
13416 don't want the alternative below this comment to execute. */
13417 }
13418 else if (CHARPOS (startp) >= BEGV
13419 && CHARPOS (startp) <= ZV
13420 && PT >= CHARPOS (startp)
13421 && (CHARPOS (startp) < ZV
13422 /* Avoid starting at end of buffer. */
13423 || CHARPOS (startp) == BEGV
13424 || (XFASTINT (w->last_modified) >= MODIFF
13425 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13426 {
13427
13428 /* If first window line is a continuation line, and window start
13429 is inside the modified region, but the first change is before
13430 current window start, we must select a new window start.
13431
13432 However, if this is the result of a down-mouse event (e.g. by
13433 extending the mouse-drag-overlay), we don't want to select a
13434 new window start, since that would change the position under
13435 the mouse, resulting in an unwanted mouse-movement rather
13436 than a simple mouse-click. */
13437 if (NILP (w->start_at_line_beg)
13438 && NILP (do_mouse_tracking)
13439 && CHARPOS (startp) > BEGV
13440 && CHARPOS (startp) > BEG + beg_unchanged
13441 && CHARPOS (startp) <= Z - end_unchanged)
13442 {
13443 w->force_start = Qt;
13444 if (XMARKER (w->start)->buffer == current_buffer)
13445 compute_window_start_on_continuation_line (w);
13446 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13447 goto force_start;
13448 }
13449
13450 #if GLYPH_DEBUG
13451 debug_method_add (w, "same window start");
13452 #endif
13453
13454 /* Try to redisplay starting at same place as before.
13455 If point has not moved off frame, accept the results. */
13456 if (!current_matrix_up_to_date_p
13457 /* Don't use try_window_reusing_current_matrix in this case
13458 because a window scroll function can have changed the
13459 buffer. */
13460 || !NILP (Vwindow_scroll_functions)
13461 || MINI_WINDOW_P (w)
13462 || !(used_current_matrix_p
13463 = try_window_reusing_current_matrix (w)))
13464 {
13465 IF_DEBUG (debug_method_add (w, "1"));
13466 if (try_window (window, startp, 1) < 0)
13467 /* -1 means we need to scroll.
13468 0 means we need new matrices, but fonts_changed_p
13469 is set in that case, so we will detect it below. */
13470 goto try_to_scroll;
13471 }
13472
13473 if (fonts_changed_p)
13474 goto need_larger_matrices;
13475
13476 if (w->cursor.vpos >= 0)
13477 {
13478 if (!just_this_one_p
13479 || current_buffer->clip_changed
13480 || BEG_UNCHANGED < CHARPOS (startp))
13481 /* Forget any recorded base line for line number display. */
13482 w->base_line_number = Qnil;
13483
13484 if (!cursor_row_fully_visible_p (w, 1, 0))
13485 {
13486 clear_glyph_matrix (w->desired_matrix);
13487 last_line_misfit = 1;
13488 }
13489 /* Drop through and scroll. */
13490 else
13491 goto done;
13492 }
13493 else
13494 clear_glyph_matrix (w->desired_matrix);
13495 }
13496
13497 try_to_scroll:
13498
13499 w->last_modified = make_number (0);
13500 w->last_overlay_modified = make_number (0);
13501
13502 /* Redisplay the mode line. Select the buffer properly for that. */
13503 if (!update_mode_line)
13504 {
13505 update_mode_line = 1;
13506 w->update_mode_line = Qt;
13507 }
13508
13509 /* Try to scroll by specified few lines. */
13510 if ((scroll_conservatively
13511 || scroll_step
13512 || temp_scroll_step
13513 || NUMBERP (current_buffer->scroll_up_aggressively)
13514 || NUMBERP (current_buffer->scroll_down_aggressively))
13515 && !current_buffer->clip_changed
13516 && CHARPOS (startp) >= BEGV
13517 && CHARPOS (startp) <= ZV)
13518 {
13519 /* The function returns -1 if new fonts were loaded, 1 if
13520 successful, 0 if not successful. */
13521 int rc = try_scrolling (window, just_this_one_p,
13522 scroll_conservatively,
13523 scroll_step,
13524 temp_scroll_step, last_line_misfit);
13525 switch (rc)
13526 {
13527 case SCROLLING_SUCCESS:
13528 goto done;
13529
13530 case SCROLLING_NEED_LARGER_MATRICES:
13531 goto need_larger_matrices;
13532
13533 case SCROLLING_FAILED:
13534 break;
13535
13536 default:
13537 abort ();
13538 }
13539 }
13540
13541 /* Finally, just choose place to start which centers point */
13542
13543 recenter:
13544 if (centering_position < 0)
13545 centering_position = window_box_height (w) / 2;
13546
13547 #if GLYPH_DEBUG
13548 debug_method_add (w, "recenter");
13549 #endif
13550
13551 /* w->vscroll = 0; */
13552
13553 /* Forget any previously recorded base line for line number display. */
13554 if (!buffer_unchanged_p)
13555 w->base_line_number = Qnil;
13556
13557 /* Move backward half the height of the window. */
13558 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13559 it.current_y = it.last_visible_y;
13560 move_it_vertically_backward (&it, centering_position);
13561 xassert (IT_CHARPOS (it) >= BEGV);
13562
13563 /* The function move_it_vertically_backward may move over more
13564 than the specified y-distance. If it->w is small, e.g. a
13565 mini-buffer window, we may end up in front of the window's
13566 display area. Start displaying at the start of the line
13567 containing PT in this case. */
13568 if (it.current_y <= 0)
13569 {
13570 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13571 move_it_vertically_backward (&it, 0);
13572 #if 0
13573 /* I think this assert is bogus if buffer contains
13574 invisible text or images. KFS. */
13575 xassert (IT_CHARPOS (it) <= PT);
13576 #endif
13577 it.current_y = 0;
13578 }
13579
13580 it.current_x = it.hpos = 0;
13581
13582 /* Set startp here explicitly in case that helps avoid an infinite loop
13583 in case the window-scroll-functions functions get errors. */
13584 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13585
13586 /* Run scroll hooks. */
13587 startp = run_window_scroll_functions (window, it.current.pos);
13588
13589 /* Redisplay the window. */
13590 if (!current_matrix_up_to_date_p
13591 || windows_or_buffers_changed
13592 || cursor_type_changed
13593 /* Don't use try_window_reusing_current_matrix in this case
13594 because it can have changed the buffer. */
13595 || !NILP (Vwindow_scroll_functions)
13596 || !just_this_one_p
13597 || MINI_WINDOW_P (w)
13598 || !(used_current_matrix_p
13599 = try_window_reusing_current_matrix (w)))
13600 try_window (window, startp, 0);
13601
13602 /* If new fonts have been loaded (due to fontsets), give up. We
13603 have to start a new redisplay since we need to re-adjust glyph
13604 matrices. */
13605 if (fonts_changed_p)
13606 goto need_larger_matrices;
13607
13608 /* If cursor did not appear assume that the middle of the window is
13609 in the first line of the window. Do it again with the next line.
13610 (Imagine a window of height 100, displaying two lines of height
13611 60. Moving back 50 from it->last_visible_y will end in the first
13612 line.) */
13613 if (w->cursor.vpos < 0)
13614 {
13615 if (!NILP (w->window_end_valid)
13616 && PT >= Z - XFASTINT (w->window_end_pos))
13617 {
13618 clear_glyph_matrix (w->desired_matrix);
13619 move_it_by_lines (&it, 1, 0);
13620 try_window (window, it.current.pos, 0);
13621 }
13622 else if (PT < IT_CHARPOS (it))
13623 {
13624 clear_glyph_matrix (w->desired_matrix);
13625 move_it_by_lines (&it, -1, 0);
13626 try_window (window, it.current.pos, 0);
13627 }
13628 else
13629 {
13630 /* Not much we can do about it. */
13631 }
13632 }
13633
13634 /* Consider the following case: Window starts at BEGV, there is
13635 invisible, intangible text at BEGV, so that display starts at
13636 some point START > BEGV. It can happen that we are called with
13637 PT somewhere between BEGV and START. Try to handle that case. */
13638 if (w->cursor.vpos < 0)
13639 {
13640 struct glyph_row *row = w->current_matrix->rows;
13641 if (row->mode_line_p)
13642 ++row;
13643 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13644 }
13645
13646 if (!cursor_row_fully_visible_p (w, 0, 0))
13647 {
13648 /* If vscroll is enabled, disable it and try again. */
13649 if (w->vscroll)
13650 {
13651 w->vscroll = 0;
13652 clear_glyph_matrix (w->desired_matrix);
13653 goto recenter;
13654 }
13655
13656 /* If centering point failed to make the whole line visible,
13657 put point at the top instead. That has to make the whole line
13658 visible, if it can be done. */
13659 if (centering_position == 0)
13660 goto done;
13661
13662 clear_glyph_matrix (w->desired_matrix);
13663 centering_position = 0;
13664 goto recenter;
13665 }
13666
13667 done:
13668
13669 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13670 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13671 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13672 ? Qt : Qnil);
13673
13674 /* Display the mode line, if we must. */
13675 if ((update_mode_line
13676 /* If window not full width, must redo its mode line
13677 if (a) the window to its side is being redone and
13678 (b) we do a frame-based redisplay. This is a consequence
13679 of how inverted lines are drawn in frame-based redisplay. */
13680 || (!just_this_one_p
13681 && !FRAME_WINDOW_P (f)
13682 && !WINDOW_FULL_WIDTH_P (w))
13683 /* Line number to display. */
13684 || INTEGERP (w->base_line_pos)
13685 /* Column number is displayed and different from the one displayed. */
13686 || (!NILP (w->column_number_displayed)
13687 && (XFASTINT (w->column_number_displayed)
13688 != (int) current_column ()))) /* iftc */
13689 /* This means that the window has a mode line. */
13690 && (WINDOW_WANTS_MODELINE_P (w)
13691 || WINDOW_WANTS_HEADER_LINE_P (w)))
13692 {
13693 display_mode_lines (w);
13694
13695 /* If mode line height has changed, arrange for a thorough
13696 immediate redisplay using the correct mode line height. */
13697 if (WINDOW_WANTS_MODELINE_P (w)
13698 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13699 {
13700 fonts_changed_p = 1;
13701 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13702 = DESIRED_MODE_LINE_HEIGHT (w);
13703 }
13704
13705 /* If top line height has changed, arrange for a thorough
13706 immediate redisplay using the correct mode line height. */
13707 if (WINDOW_WANTS_HEADER_LINE_P (w)
13708 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13709 {
13710 fonts_changed_p = 1;
13711 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13712 = DESIRED_HEADER_LINE_HEIGHT (w);
13713 }
13714
13715 if (fonts_changed_p)
13716 goto need_larger_matrices;
13717 }
13718
13719 if (!line_number_displayed
13720 && !BUFFERP (w->base_line_pos))
13721 {
13722 w->base_line_pos = Qnil;
13723 w->base_line_number = Qnil;
13724 }
13725
13726 finish_menu_bars:
13727
13728 /* When we reach a frame's selected window, redo the frame's menu bar. */
13729 if (update_mode_line
13730 && EQ (FRAME_SELECTED_WINDOW (f), window))
13731 {
13732 int redisplay_menu_p = 0;
13733 int redisplay_tool_bar_p = 0;
13734
13735 if (FRAME_WINDOW_P (f))
13736 {
13737 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13738 || defined (USE_GTK)
13739 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13740 #else
13741 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13742 #endif
13743 }
13744 else
13745 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13746
13747 if (redisplay_menu_p)
13748 display_menu_bar (w);
13749
13750 #ifdef HAVE_WINDOW_SYSTEM
13751 if (FRAME_WINDOW_P (f))
13752 {
13753 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13754 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13755 #else
13756 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13757 && (FRAME_TOOL_BAR_LINES (f) > 0
13758 || !NILP (Vauto_resize_tool_bars));
13759 #endif
13760
13761 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13762 {
13763 extern int ignore_mouse_drag_p;
13764 ignore_mouse_drag_p = 1;
13765 }
13766 }
13767 #endif
13768 }
13769
13770 #ifdef HAVE_WINDOW_SYSTEM
13771 if (FRAME_WINDOW_P (f)
13772 && update_window_fringes (w, (just_this_one_p
13773 || (!used_current_matrix_p && !overlay_arrow_seen)
13774 || w->pseudo_window_p)))
13775 {
13776 update_begin (f);
13777 BLOCK_INPUT;
13778 if (draw_window_fringes (w, 1))
13779 x_draw_vertical_border (w);
13780 UNBLOCK_INPUT;
13781 update_end (f);
13782 }
13783 #endif /* HAVE_WINDOW_SYSTEM */
13784
13785 /* We go to this label, with fonts_changed_p nonzero,
13786 if it is necessary to try again using larger glyph matrices.
13787 We have to redeem the scroll bar even in this case,
13788 because the loop in redisplay_internal expects that. */
13789 need_larger_matrices:
13790 ;
13791 finish_scroll_bars:
13792
13793 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13794 {
13795 /* Set the thumb's position and size. */
13796 set_vertical_scroll_bar (w);
13797
13798 /* Note that we actually used the scroll bar attached to this
13799 window, so it shouldn't be deleted at the end of redisplay. */
13800 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13801 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13802 }
13803
13804 /* Restore current_buffer and value of point in it. */
13805 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13806 set_buffer_internal_1 (old);
13807 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13808 shorter. This can be caused by log truncation in *Messages*. */
13809 if (CHARPOS (lpoint) <= ZV)
13810 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13811
13812 unbind_to (count, Qnil);
13813 }
13814
13815
13816 /* Build the complete desired matrix of WINDOW with a window start
13817 buffer position POS.
13818
13819 Value is 1 if successful. It is zero if fonts were loaded during
13820 redisplay which makes re-adjusting glyph matrices necessary, and -1
13821 if point would appear in the scroll margins.
13822 (We check that only if CHECK_MARGINS is nonzero. */
13823
13824 int
13825 try_window (window, pos, check_margins)
13826 Lisp_Object window;
13827 struct text_pos pos;
13828 int check_margins;
13829 {
13830 struct window *w = XWINDOW (window);
13831 struct it it;
13832 struct glyph_row *last_text_row = NULL;
13833 struct frame *f = XFRAME (w->frame);
13834
13835 /* Make POS the new window start. */
13836 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13837
13838 /* Mark cursor position as unknown. No overlay arrow seen. */
13839 w->cursor.vpos = -1;
13840 overlay_arrow_seen = 0;
13841
13842 /* Initialize iterator and info to start at POS. */
13843 start_display (&it, w, pos);
13844
13845 /* Display all lines of W. */
13846 while (it.current_y < it.last_visible_y)
13847 {
13848 if (display_line (&it))
13849 last_text_row = it.glyph_row - 1;
13850 if (fonts_changed_p)
13851 return 0;
13852 }
13853
13854 /* Don't let the cursor end in the scroll margins. */
13855 if (check_margins
13856 && !MINI_WINDOW_P (w))
13857 {
13858 int this_scroll_margin;
13859
13860 this_scroll_margin = max (0, scroll_margin);
13861 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13862 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13863
13864 if ((w->cursor.y >= 0 /* not vscrolled */
13865 && w->cursor.y < this_scroll_margin
13866 && CHARPOS (pos) > BEGV
13867 && IT_CHARPOS (it) < ZV)
13868 /* rms: considering make_cursor_line_fully_visible_p here
13869 seems to give wrong results. We don't want to recenter
13870 when the last line is partly visible, we want to allow
13871 that case to be handled in the usual way. */
13872 || (w->cursor.y + 1) > it.last_visible_y)
13873 {
13874 w->cursor.vpos = -1;
13875 clear_glyph_matrix (w->desired_matrix);
13876 return -1;
13877 }
13878 }
13879
13880 /* If bottom moved off end of frame, change mode line percentage. */
13881 if (XFASTINT (w->window_end_pos) <= 0
13882 && Z != IT_CHARPOS (it))
13883 w->update_mode_line = Qt;
13884
13885 /* Set window_end_pos to the offset of the last character displayed
13886 on the window from the end of current_buffer. Set
13887 window_end_vpos to its row number. */
13888 if (last_text_row)
13889 {
13890 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13891 w->window_end_bytepos
13892 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13893 w->window_end_pos
13894 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13895 w->window_end_vpos
13896 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13897 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13898 ->displays_text_p);
13899 }
13900 else
13901 {
13902 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13903 w->window_end_pos = make_number (Z - ZV);
13904 w->window_end_vpos = make_number (0);
13905 }
13906
13907 /* But that is not valid info until redisplay finishes. */
13908 w->window_end_valid = Qnil;
13909 return 1;
13910 }
13911
13912
13913 \f
13914 /************************************************************************
13915 Window redisplay reusing current matrix when buffer has not changed
13916 ************************************************************************/
13917
13918 /* Try redisplay of window W showing an unchanged buffer with a
13919 different window start than the last time it was displayed by
13920 reusing its current matrix. Value is non-zero if successful.
13921 W->start is the new window start. */
13922
13923 static int
13924 try_window_reusing_current_matrix (w)
13925 struct window *w;
13926 {
13927 struct frame *f = XFRAME (w->frame);
13928 struct glyph_row *row, *bottom_row;
13929 struct it it;
13930 struct run run;
13931 struct text_pos start, new_start;
13932 int nrows_scrolled, i;
13933 struct glyph_row *last_text_row;
13934 struct glyph_row *last_reused_text_row;
13935 struct glyph_row *start_row;
13936 int start_vpos, min_y, max_y;
13937
13938 #if GLYPH_DEBUG
13939 if (inhibit_try_window_reusing)
13940 return 0;
13941 #endif
13942
13943 if (/* This function doesn't handle terminal frames. */
13944 !FRAME_WINDOW_P (f)
13945 /* Don't try to reuse the display if windows have been split
13946 or such. */
13947 || windows_or_buffers_changed
13948 || cursor_type_changed)
13949 return 0;
13950
13951 /* Can't do this if region may have changed. */
13952 if ((!NILP (Vtransient_mark_mode)
13953 && !NILP (current_buffer->mark_active))
13954 || !NILP (w->region_showing)
13955 || !NILP (Vshow_trailing_whitespace))
13956 return 0;
13957
13958 /* If top-line visibility has changed, give up. */
13959 if (WINDOW_WANTS_HEADER_LINE_P (w)
13960 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13961 return 0;
13962
13963 /* Give up if old or new display is scrolled vertically. We could
13964 make this function handle this, but right now it doesn't. */
13965 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13966 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13967 return 0;
13968
13969 /* The variable new_start now holds the new window start. The old
13970 start `start' can be determined from the current matrix. */
13971 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13972 start = start_row->start.pos;
13973 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13974
13975 /* Clear the desired matrix for the display below. */
13976 clear_glyph_matrix (w->desired_matrix);
13977
13978 if (CHARPOS (new_start) <= CHARPOS (start))
13979 {
13980 int first_row_y;
13981
13982 /* Don't use this method if the display starts with an ellipsis
13983 displayed for invisible text. It's not easy to handle that case
13984 below, and it's certainly not worth the effort since this is
13985 not a frequent case. */
13986 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13987 return 0;
13988
13989 IF_DEBUG (debug_method_add (w, "twu1"));
13990
13991 /* Display up to a row that can be reused. The variable
13992 last_text_row is set to the last row displayed that displays
13993 text. Note that it.vpos == 0 if or if not there is a
13994 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13995 start_display (&it, w, new_start);
13996 first_row_y = it.current_y;
13997 w->cursor.vpos = -1;
13998 last_text_row = last_reused_text_row = NULL;
13999
14000 while (it.current_y < it.last_visible_y
14001 && !fonts_changed_p)
14002 {
14003 /* If we have reached into the characters in the START row,
14004 that means the line boundaries have changed. So we
14005 can't start copying with the row START. Maybe it will
14006 work to start copying with the following row. */
14007 while (IT_CHARPOS (it) > CHARPOS (start))
14008 {
14009 /* Advance to the next row as the "start". */
14010 start_row++;
14011 start = start_row->start.pos;
14012 /* If there are no more rows to try, or just one, give up. */
14013 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14014 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14015 || CHARPOS (start) == ZV)
14016 {
14017 clear_glyph_matrix (w->desired_matrix);
14018 return 0;
14019 }
14020
14021 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14022 }
14023 /* If we have reached alignment,
14024 we can copy the rest of the rows. */
14025 if (IT_CHARPOS (it) == CHARPOS (start))
14026 break;
14027
14028 if (display_line (&it))
14029 last_text_row = it.glyph_row - 1;
14030 }
14031
14032 /* A value of current_y < last_visible_y means that we stopped
14033 at the previous window start, which in turn means that we
14034 have at least one reusable row. */
14035 if (it.current_y < it.last_visible_y)
14036 {
14037 /* IT.vpos always starts from 0; it counts text lines. */
14038 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14039
14040 /* Find PT if not already found in the lines displayed. */
14041 if (w->cursor.vpos < 0)
14042 {
14043 int dy = it.current_y - start_row->y;
14044
14045 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14046 row = row_containing_pos (w, PT, row, NULL, dy);
14047 if (row)
14048 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14049 dy, nrows_scrolled);
14050 else
14051 {
14052 clear_glyph_matrix (w->desired_matrix);
14053 return 0;
14054 }
14055 }
14056
14057 /* Scroll the display. Do it before the current matrix is
14058 changed. The problem here is that update has not yet
14059 run, i.e. part of the current matrix is not up to date.
14060 scroll_run_hook will clear the cursor, and use the
14061 current matrix to get the height of the row the cursor is
14062 in. */
14063 run.current_y = start_row->y;
14064 run.desired_y = it.current_y;
14065 run.height = it.last_visible_y - it.current_y;
14066
14067 if (run.height > 0 && run.current_y != run.desired_y)
14068 {
14069 update_begin (f);
14070 FRAME_RIF (f)->update_window_begin_hook (w);
14071 FRAME_RIF (f)->clear_window_mouse_face (w);
14072 FRAME_RIF (f)->scroll_run_hook (w, &run);
14073 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14074 update_end (f);
14075 }
14076
14077 /* Shift current matrix down by nrows_scrolled lines. */
14078 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14079 rotate_matrix (w->current_matrix,
14080 start_vpos,
14081 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14082 nrows_scrolled);
14083
14084 /* Disable lines that must be updated. */
14085 for (i = 0; i < nrows_scrolled; ++i)
14086 (start_row + i)->enabled_p = 0;
14087
14088 /* Re-compute Y positions. */
14089 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14090 max_y = it.last_visible_y;
14091 for (row = start_row + nrows_scrolled;
14092 row < bottom_row;
14093 ++row)
14094 {
14095 row->y = it.current_y;
14096 row->visible_height = row->height;
14097
14098 if (row->y < min_y)
14099 row->visible_height -= min_y - row->y;
14100 if (row->y + row->height > max_y)
14101 row->visible_height -= row->y + row->height - max_y;
14102 row->redraw_fringe_bitmaps_p = 1;
14103
14104 it.current_y += row->height;
14105
14106 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14107 last_reused_text_row = row;
14108 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14109 break;
14110 }
14111
14112 /* Disable lines in the current matrix which are now
14113 below the window. */
14114 for (++row; row < bottom_row; ++row)
14115 row->enabled_p = row->mode_line_p = 0;
14116 }
14117
14118 /* Update window_end_pos etc.; last_reused_text_row is the last
14119 reused row from the current matrix containing text, if any.
14120 The value of last_text_row is the last displayed line
14121 containing text. */
14122 if (last_reused_text_row)
14123 {
14124 w->window_end_bytepos
14125 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14126 w->window_end_pos
14127 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14128 w->window_end_vpos
14129 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14130 w->current_matrix));
14131 }
14132 else if (last_text_row)
14133 {
14134 w->window_end_bytepos
14135 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14136 w->window_end_pos
14137 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14138 w->window_end_vpos
14139 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14140 }
14141 else
14142 {
14143 /* This window must be completely empty. */
14144 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14145 w->window_end_pos = make_number (Z - ZV);
14146 w->window_end_vpos = make_number (0);
14147 }
14148 w->window_end_valid = Qnil;
14149
14150 /* Update hint: don't try scrolling again in update_window. */
14151 w->desired_matrix->no_scrolling_p = 1;
14152
14153 #if GLYPH_DEBUG
14154 debug_method_add (w, "try_window_reusing_current_matrix 1");
14155 #endif
14156 return 1;
14157 }
14158 else if (CHARPOS (new_start) > CHARPOS (start))
14159 {
14160 struct glyph_row *pt_row, *row;
14161 struct glyph_row *first_reusable_row;
14162 struct glyph_row *first_row_to_display;
14163 int dy;
14164 int yb = window_text_bottom_y (w);
14165
14166 /* Find the row starting at new_start, if there is one. Don't
14167 reuse a partially visible line at the end. */
14168 first_reusable_row = start_row;
14169 while (first_reusable_row->enabled_p
14170 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14171 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14172 < CHARPOS (new_start)))
14173 ++first_reusable_row;
14174
14175 /* Give up if there is no row to reuse. */
14176 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14177 || !first_reusable_row->enabled_p
14178 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14179 != CHARPOS (new_start)))
14180 return 0;
14181
14182 /* We can reuse fully visible rows beginning with
14183 first_reusable_row to the end of the window. Set
14184 first_row_to_display to the first row that cannot be reused.
14185 Set pt_row to the row containing point, if there is any. */
14186 pt_row = NULL;
14187 for (first_row_to_display = first_reusable_row;
14188 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14189 ++first_row_to_display)
14190 {
14191 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14192 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14193 pt_row = first_row_to_display;
14194 }
14195
14196 /* Start displaying at the start of first_row_to_display. */
14197 xassert (first_row_to_display->y < yb);
14198 init_to_row_start (&it, w, first_row_to_display);
14199
14200 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14201 - start_vpos);
14202 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14203 - nrows_scrolled);
14204 it.current_y = (first_row_to_display->y - first_reusable_row->y
14205 + WINDOW_HEADER_LINE_HEIGHT (w));
14206
14207 /* Display lines beginning with first_row_to_display in the
14208 desired matrix. Set last_text_row to the last row displayed
14209 that displays text. */
14210 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14211 if (pt_row == NULL)
14212 w->cursor.vpos = -1;
14213 last_text_row = NULL;
14214 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14215 if (display_line (&it))
14216 last_text_row = it.glyph_row - 1;
14217
14218 /* Give up If point isn't in a row displayed or reused. */
14219 if (w->cursor.vpos < 0)
14220 {
14221 clear_glyph_matrix (w->desired_matrix);
14222 return 0;
14223 }
14224
14225 /* If point is in a reused row, adjust y and vpos of the cursor
14226 position. */
14227 if (pt_row)
14228 {
14229 w->cursor.vpos -= nrows_scrolled;
14230 w->cursor.y -= first_reusable_row->y - start_row->y;
14231 }
14232
14233 /* Scroll the display. */
14234 run.current_y = first_reusable_row->y;
14235 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14236 run.height = it.last_visible_y - run.current_y;
14237 dy = run.current_y - run.desired_y;
14238
14239 if (run.height)
14240 {
14241 update_begin (f);
14242 FRAME_RIF (f)->update_window_begin_hook (w);
14243 FRAME_RIF (f)->clear_window_mouse_face (w);
14244 FRAME_RIF (f)->scroll_run_hook (w, &run);
14245 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14246 update_end (f);
14247 }
14248
14249 /* Adjust Y positions of reused rows. */
14250 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14251 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14252 max_y = it.last_visible_y;
14253 for (row = first_reusable_row; row < first_row_to_display; ++row)
14254 {
14255 row->y -= dy;
14256 row->visible_height = row->height;
14257 if (row->y < min_y)
14258 row->visible_height -= min_y - row->y;
14259 if (row->y + row->height > max_y)
14260 row->visible_height -= row->y + row->height - max_y;
14261 row->redraw_fringe_bitmaps_p = 1;
14262 }
14263
14264 /* Scroll the current matrix. */
14265 xassert (nrows_scrolled > 0);
14266 rotate_matrix (w->current_matrix,
14267 start_vpos,
14268 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14269 -nrows_scrolled);
14270
14271 /* Disable rows not reused. */
14272 for (row -= nrows_scrolled; row < bottom_row; ++row)
14273 row->enabled_p = 0;
14274
14275 /* Point may have moved to a different line, so we cannot assume that
14276 the previous cursor position is valid; locate the correct row. */
14277 if (pt_row)
14278 {
14279 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14280 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14281 row++)
14282 {
14283 w->cursor.vpos++;
14284 w->cursor.y = row->y;
14285 }
14286 if (row < bottom_row)
14287 {
14288 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14289 while (glyph->charpos < PT)
14290 {
14291 w->cursor.hpos++;
14292 w->cursor.x += glyph->pixel_width;
14293 glyph++;
14294 }
14295 }
14296 }
14297
14298 /* Adjust window end. A null value of last_text_row means that
14299 the window end is in reused rows which in turn means that
14300 only its vpos can have changed. */
14301 if (last_text_row)
14302 {
14303 w->window_end_bytepos
14304 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14305 w->window_end_pos
14306 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14307 w->window_end_vpos
14308 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14309 }
14310 else
14311 {
14312 w->window_end_vpos
14313 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14314 }
14315
14316 w->window_end_valid = Qnil;
14317 w->desired_matrix->no_scrolling_p = 1;
14318
14319 #if GLYPH_DEBUG
14320 debug_method_add (w, "try_window_reusing_current_matrix 2");
14321 #endif
14322 return 1;
14323 }
14324
14325 return 0;
14326 }
14327
14328
14329 \f
14330 /************************************************************************
14331 Window redisplay reusing current matrix when buffer has changed
14332 ************************************************************************/
14333
14334 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14335 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14336 int *, int *));
14337 static struct glyph_row *
14338 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14339 struct glyph_row *));
14340
14341
14342 /* Return the last row in MATRIX displaying text. If row START is
14343 non-null, start searching with that row. IT gives the dimensions
14344 of the display. Value is null if matrix is empty; otherwise it is
14345 a pointer to the row found. */
14346
14347 static struct glyph_row *
14348 find_last_row_displaying_text (matrix, it, start)
14349 struct glyph_matrix *matrix;
14350 struct it *it;
14351 struct glyph_row *start;
14352 {
14353 struct glyph_row *row, *row_found;
14354
14355 /* Set row_found to the last row in IT->w's current matrix
14356 displaying text. The loop looks funny but think of partially
14357 visible lines. */
14358 row_found = NULL;
14359 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14360 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14361 {
14362 xassert (row->enabled_p);
14363 row_found = row;
14364 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14365 break;
14366 ++row;
14367 }
14368
14369 return row_found;
14370 }
14371
14372
14373 /* Return the last row in the current matrix of W that is not affected
14374 by changes at the start of current_buffer that occurred since W's
14375 current matrix was built. Value is null if no such row exists.
14376
14377 BEG_UNCHANGED us the number of characters unchanged at the start of
14378 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14379 first changed character in current_buffer. Characters at positions <
14380 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14381 when the current matrix was built. */
14382
14383 static struct glyph_row *
14384 find_last_unchanged_at_beg_row (w)
14385 struct window *w;
14386 {
14387 int first_changed_pos = BEG + BEG_UNCHANGED;
14388 struct glyph_row *row;
14389 struct glyph_row *row_found = NULL;
14390 int yb = window_text_bottom_y (w);
14391
14392 /* Find the last row displaying unchanged text. */
14393 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14394 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14395 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14396 {
14397 if (/* If row ends before first_changed_pos, it is unchanged,
14398 except in some case. */
14399 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14400 /* When row ends in ZV and we write at ZV it is not
14401 unchanged. */
14402 && !row->ends_at_zv_p
14403 /* When first_changed_pos is the end of a continued line,
14404 row is not unchanged because it may be no longer
14405 continued. */
14406 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14407 && (row->continued_p
14408 || row->exact_window_width_line_p)))
14409 row_found = row;
14410
14411 /* Stop if last visible row. */
14412 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14413 break;
14414
14415 ++row;
14416 }
14417
14418 return row_found;
14419 }
14420
14421
14422 /* Find the first glyph row in the current matrix of W that is not
14423 affected by changes at the end of current_buffer since the
14424 time W's current matrix was built.
14425
14426 Return in *DELTA the number of chars by which buffer positions in
14427 unchanged text at the end of current_buffer must be adjusted.
14428
14429 Return in *DELTA_BYTES the corresponding number of bytes.
14430
14431 Value is null if no such row exists, i.e. all rows are affected by
14432 changes. */
14433
14434 static struct glyph_row *
14435 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14436 struct window *w;
14437 int *delta, *delta_bytes;
14438 {
14439 struct glyph_row *row;
14440 struct glyph_row *row_found = NULL;
14441
14442 *delta = *delta_bytes = 0;
14443
14444 /* Display must not have been paused, otherwise the current matrix
14445 is not up to date. */
14446 eassert (!NILP (w->window_end_valid));
14447
14448 /* A value of window_end_pos >= END_UNCHANGED means that the window
14449 end is in the range of changed text. If so, there is no
14450 unchanged row at the end of W's current matrix. */
14451 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14452 return NULL;
14453
14454 /* Set row to the last row in W's current matrix displaying text. */
14455 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14456
14457 /* If matrix is entirely empty, no unchanged row exists. */
14458 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14459 {
14460 /* The value of row is the last glyph row in the matrix having a
14461 meaningful buffer position in it. The end position of row
14462 corresponds to window_end_pos. This allows us to translate
14463 buffer positions in the current matrix to current buffer
14464 positions for characters not in changed text. */
14465 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14466 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14467 int last_unchanged_pos, last_unchanged_pos_old;
14468 struct glyph_row *first_text_row
14469 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14470
14471 *delta = Z - Z_old;
14472 *delta_bytes = Z_BYTE - Z_BYTE_old;
14473
14474 /* Set last_unchanged_pos to the buffer position of the last
14475 character in the buffer that has not been changed. Z is the
14476 index + 1 of the last character in current_buffer, i.e. by
14477 subtracting END_UNCHANGED we get the index of the last
14478 unchanged character, and we have to add BEG to get its buffer
14479 position. */
14480 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14481 last_unchanged_pos_old = last_unchanged_pos - *delta;
14482
14483 /* Search backward from ROW for a row displaying a line that
14484 starts at a minimum position >= last_unchanged_pos_old. */
14485 for (; row > first_text_row; --row)
14486 {
14487 /* This used to abort, but it can happen.
14488 It is ok to just stop the search instead here. KFS. */
14489 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14490 break;
14491
14492 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14493 row_found = row;
14494 }
14495 }
14496
14497 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14498
14499 return row_found;
14500 }
14501
14502
14503 /* Make sure that glyph rows in the current matrix of window W
14504 reference the same glyph memory as corresponding rows in the
14505 frame's frame matrix. This function is called after scrolling W's
14506 current matrix on a terminal frame in try_window_id and
14507 try_window_reusing_current_matrix. */
14508
14509 static void
14510 sync_frame_with_window_matrix_rows (w)
14511 struct window *w;
14512 {
14513 struct frame *f = XFRAME (w->frame);
14514 struct glyph_row *window_row, *window_row_end, *frame_row;
14515
14516 /* Preconditions: W must be a leaf window and full-width. Its frame
14517 must have a frame matrix. */
14518 xassert (NILP (w->hchild) && NILP (w->vchild));
14519 xassert (WINDOW_FULL_WIDTH_P (w));
14520 xassert (!FRAME_WINDOW_P (f));
14521
14522 /* If W is a full-width window, glyph pointers in W's current matrix
14523 have, by definition, to be the same as glyph pointers in the
14524 corresponding frame matrix. Note that frame matrices have no
14525 marginal areas (see build_frame_matrix). */
14526 window_row = w->current_matrix->rows;
14527 window_row_end = window_row + w->current_matrix->nrows;
14528 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14529 while (window_row < window_row_end)
14530 {
14531 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14532 struct glyph *end = window_row->glyphs[LAST_AREA];
14533
14534 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14535 frame_row->glyphs[TEXT_AREA] = start;
14536 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14537 frame_row->glyphs[LAST_AREA] = end;
14538
14539 /* Disable frame rows whose corresponding window rows have
14540 been disabled in try_window_id. */
14541 if (!window_row->enabled_p)
14542 frame_row->enabled_p = 0;
14543
14544 ++window_row, ++frame_row;
14545 }
14546 }
14547
14548
14549 /* Find the glyph row in window W containing CHARPOS. Consider all
14550 rows between START and END (not inclusive). END null means search
14551 all rows to the end of the display area of W. Value is the row
14552 containing CHARPOS or null. */
14553
14554 struct glyph_row *
14555 row_containing_pos (w, charpos, start, end, dy)
14556 struct window *w;
14557 int charpos;
14558 struct glyph_row *start, *end;
14559 int dy;
14560 {
14561 struct glyph_row *row = start;
14562 int last_y;
14563
14564 /* If we happen to start on a header-line, skip that. */
14565 if (row->mode_line_p)
14566 ++row;
14567
14568 if ((end && row >= end) || !row->enabled_p)
14569 return NULL;
14570
14571 last_y = window_text_bottom_y (w) - dy;
14572
14573 while (1)
14574 {
14575 /* Give up if we have gone too far. */
14576 if (end && row >= end)
14577 return NULL;
14578 /* This formerly returned if they were equal.
14579 I think that both quantities are of a "last plus one" type;
14580 if so, when they are equal, the row is within the screen. -- rms. */
14581 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14582 return NULL;
14583
14584 /* If it is in this row, return this row. */
14585 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14586 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14587 /* The end position of a row equals the start
14588 position of the next row. If CHARPOS is there, we
14589 would rather display it in the next line, except
14590 when this line ends in ZV. */
14591 && !row->ends_at_zv_p
14592 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14593 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14594 return row;
14595 ++row;
14596 }
14597 }
14598
14599
14600 /* Try to redisplay window W by reusing its existing display. W's
14601 current matrix must be up to date when this function is called,
14602 i.e. window_end_valid must not be nil.
14603
14604 Value is
14605
14606 1 if display has been updated
14607 0 if otherwise unsuccessful
14608 -1 if redisplay with same window start is known not to succeed
14609
14610 The following steps are performed:
14611
14612 1. Find the last row in the current matrix of W that is not
14613 affected by changes at the start of current_buffer. If no such row
14614 is found, give up.
14615
14616 2. Find the first row in W's current matrix that is not affected by
14617 changes at the end of current_buffer. Maybe there is no such row.
14618
14619 3. Display lines beginning with the row + 1 found in step 1 to the
14620 row found in step 2 or, if step 2 didn't find a row, to the end of
14621 the window.
14622
14623 4. If cursor is not known to appear on the window, give up.
14624
14625 5. If display stopped at the row found in step 2, scroll the
14626 display and current matrix as needed.
14627
14628 6. Maybe display some lines at the end of W, if we must. This can
14629 happen under various circumstances, like a partially visible line
14630 becoming fully visible, or because newly displayed lines are displayed
14631 in smaller font sizes.
14632
14633 7. Update W's window end information. */
14634
14635 static int
14636 try_window_id (w)
14637 struct window *w;
14638 {
14639 struct frame *f = XFRAME (w->frame);
14640 struct glyph_matrix *current_matrix = w->current_matrix;
14641 struct glyph_matrix *desired_matrix = w->desired_matrix;
14642 struct glyph_row *last_unchanged_at_beg_row;
14643 struct glyph_row *first_unchanged_at_end_row;
14644 struct glyph_row *row;
14645 struct glyph_row *bottom_row;
14646 int bottom_vpos;
14647 struct it it;
14648 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14649 struct text_pos start_pos;
14650 struct run run;
14651 int first_unchanged_at_end_vpos = 0;
14652 struct glyph_row *last_text_row, *last_text_row_at_end;
14653 struct text_pos start;
14654 int first_changed_charpos, last_changed_charpos;
14655
14656 #if GLYPH_DEBUG
14657 if (inhibit_try_window_id)
14658 return 0;
14659 #endif
14660
14661 /* This is handy for debugging. */
14662 #if 0
14663 #define GIVE_UP(X) \
14664 do { \
14665 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14666 return 0; \
14667 } while (0)
14668 #else
14669 #define GIVE_UP(X) return 0
14670 #endif
14671
14672 SET_TEXT_POS_FROM_MARKER (start, w->start);
14673
14674 /* Don't use this for mini-windows because these can show
14675 messages and mini-buffers, and we don't handle that here. */
14676 if (MINI_WINDOW_P (w))
14677 GIVE_UP (1);
14678
14679 /* This flag is used to prevent redisplay optimizations. */
14680 if (windows_or_buffers_changed || cursor_type_changed)
14681 GIVE_UP (2);
14682
14683 /* Verify that narrowing has not changed.
14684 Also verify that we were not told to prevent redisplay optimizations.
14685 It would be nice to further
14686 reduce the number of cases where this prevents try_window_id. */
14687 if (current_buffer->clip_changed
14688 || current_buffer->prevent_redisplay_optimizations_p)
14689 GIVE_UP (3);
14690
14691 /* Window must either use window-based redisplay or be full width. */
14692 if (!FRAME_WINDOW_P (f)
14693 && (!FRAME_LINE_INS_DEL_OK (f)
14694 || !WINDOW_FULL_WIDTH_P (w)))
14695 GIVE_UP (4);
14696
14697 /* Give up if point is not known NOT to appear in W. */
14698 if (PT < CHARPOS (start))
14699 GIVE_UP (5);
14700
14701 /* Another way to prevent redisplay optimizations. */
14702 if (XFASTINT (w->last_modified) == 0)
14703 GIVE_UP (6);
14704
14705 /* Verify that window is not hscrolled. */
14706 if (XFASTINT (w->hscroll) != 0)
14707 GIVE_UP (7);
14708
14709 /* Verify that display wasn't paused. */
14710 if (NILP (w->window_end_valid))
14711 GIVE_UP (8);
14712
14713 /* Can't use this if highlighting a region because a cursor movement
14714 will do more than just set the cursor. */
14715 if (!NILP (Vtransient_mark_mode)
14716 && !NILP (current_buffer->mark_active))
14717 GIVE_UP (9);
14718
14719 /* Likewise if highlighting trailing whitespace. */
14720 if (!NILP (Vshow_trailing_whitespace))
14721 GIVE_UP (11);
14722
14723 /* Likewise if showing a region. */
14724 if (!NILP (w->region_showing))
14725 GIVE_UP (10);
14726
14727 /* Can use this if overlay arrow position and or string have changed. */
14728 if (overlay_arrows_changed_p ())
14729 GIVE_UP (12);
14730
14731
14732 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14733 only if buffer has really changed. The reason is that the gap is
14734 initially at Z for freshly visited files. The code below would
14735 set end_unchanged to 0 in that case. */
14736 if (MODIFF > SAVE_MODIFF
14737 /* This seems to happen sometimes after saving a buffer. */
14738 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14739 {
14740 if (GPT - BEG < BEG_UNCHANGED)
14741 BEG_UNCHANGED = GPT - BEG;
14742 if (Z - GPT < END_UNCHANGED)
14743 END_UNCHANGED = Z - GPT;
14744 }
14745
14746 /* The position of the first and last character that has been changed. */
14747 first_changed_charpos = BEG + BEG_UNCHANGED;
14748 last_changed_charpos = Z - END_UNCHANGED;
14749
14750 /* If window starts after a line end, and the last change is in
14751 front of that newline, then changes don't affect the display.
14752 This case happens with stealth-fontification. Note that although
14753 the display is unchanged, glyph positions in the matrix have to
14754 be adjusted, of course. */
14755 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14756 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14757 && ((last_changed_charpos < CHARPOS (start)
14758 && CHARPOS (start) == BEGV)
14759 || (last_changed_charpos < CHARPOS (start) - 1
14760 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14761 {
14762 int Z_old, delta, Z_BYTE_old, delta_bytes;
14763 struct glyph_row *r0;
14764
14765 /* Compute how many chars/bytes have been added to or removed
14766 from the buffer. */
14767 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14768 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14769 delta = Z - Z_old;
14770 delta_bytes = Z_BYTE - Z_BYTE_old;
14771
14772 /* Give up if PT is not in the window. Note that it already has
14773 been checked at the start of try_window_id that PT is not in
14774 front of the window start. */
14775 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14776 GIVE_UP (13);
14777
14778 /* If window start is unchanged, we can reuse the whole matrix
14779 as is, after adjusting glyph positions. No need to compute
14780 the window end again, since its offset from Z hasn't changed. */
14781 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14782 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14783 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14784 /* PT must not be in a partially visible line. */
14785 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14786 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14787 {
14788 /* Adjust positions in the glyph matrix. */
14789 if (delta || delta_bytes)
14790 {
14791 struct glyph_row *r1
14792 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14793 increment_matrix_positions (w->current_matrix,
14794 MATRIX_ROW_VPOS (r0, current_matrix),
14795 MATRIX_ROW_VPOS (r1, current_matrix),
14796 delta, delta_bytes);
14797 }
14798
14799 /* Set the cursor. */
14800 row = row_containing_pos (w, PT, r0, NULL, 0);
14801 if (row)
14802 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14803 else
14804 abort ();
14805 return 1;
14806 }
14807 }
14808
14809 /* Handle the case that changes are all below what is displayed in
14810 the window, and that PT is in the window. This shortcut cannot
14811 be taken if ZV is visible in the window, and text has been added
14812 there that is visible in the window. */
14813 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14814 /* ZV is not visible in the window, or there are no
14815 changes at ZV, actually. */
14816 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14817 || first_changed_charpos == last_changed_charpos))
14818 {
14819 struct glyph_row *r0;
14820
14821 /* Give up if PT is not in the window. Note that it already has
14822 been checked at the start of try_window_id that PT is not in
14823 front of the window start. */
14824 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14825 GIVE_UP (14);
14826
14827 /* If window start is unchanged, we can reuse the whole matrix
14828 as is, without changing glyph positions since no text has
14829 been added/removed in front of the window end. */
14830 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14831 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14832 /* PT must not be in a partially visible line. */
14833 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14834 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14835 {
14836 /* We have to compute the window end anew since text
14837 can have been added/removed after it. */
14838 w->window_end_pos
14839 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14840 w->window_end_bytepos
14841 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14842
14843 /* Set the cursor. */
14844 row = row_containing_pos (w, PT, r0, NULL, 0);
14845 if (row)
14846 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14847 else
14848 abort ();
14849 return 2;
14850 }
14851 }
14852
14853 /* Give up if window start is in the changed area.
14854
14855 The condition used to read
14856
14857 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14858
14859 but why that was tested escapes me at the moment. */
14860 if (CHARPOS (start) >= first_changed_charpos
14861 && CHARPOS (start) <= last_changed_charpos)
14862 GIVE_UP (15);
14863
14864 /* Check that window start agrees with the start of the first glyph
14865 row in its current matrix. Check this after we know the window
14866 start is not in changed text, otherwise positions would not be
14867 comparable. */
14868 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14869 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14870 GIVE_UP (16);
14871
14872 /* Give up if the window ends in strings. Overlay strings
14873 at the end are difficult to handle, so don't try. */
14874 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14875 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14876 GIVE_UP (20);
14877
14878 /* Compute the position at which we have to start displaying new
14879 lines. Some of the lines at the top of the window might be
14880 reusable because they are not displaying changed text. Find the
14881 last row in W's current matrix not affected by changes at the
14882 start of current_buffer. Value is null if changes start in the
14883 first line of window. */
14884 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14885 if (last_unchanged_at_beg_row)
14886 {
14887 /* Avoid starting to display in the moddle of a character, a TAB
14888 for instance. This is easier than to set up the iterator
14889 exactly, and it's not a frequent case, so the additional
14890 effort wouldn't really pay off. */
14891 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14892 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14893 && last_unchanged_at_beg_row > w->current_matrix->rows)
14894 --last_unchanged_at_beg_row;
14895
14896 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14897 GIVE_UP (17);
14898
14899 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14900 GIVE_UP (18);
14901 start_pos = it.current.pos;
14902
14903 /* Start displaying new lines in the desired matrix at the same
14904 vpos we would use in the current matrix, i.e. below
14905 last_unchanged_at_beg_row. */
14906 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14907 current_matrix);
14908 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14909 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14910
14911 xassert (it.hpos == 0 && it.current_x == 0);
14912 }
14913 else
14914 {
14915 /* There are no reusable lines at the start of the window.
14916 Start displaying in the first text line. */
14917 start_display (&it, w, start);
14918 it.vpos = it.first_vpos;
14919 start_pos = it.current.pos;
14920 }
14921
14922 /* Find the first row that is not affected by changes at the end of
14923 the buffer. Value will be null if there is no unchanged row, in
14924 which case we must redisplay to the end of the window. delta
14925 will be set to the value by which buffer positions beginning with
14926 first_unchanged_at_end_row have to be adjusted due to text
14927 changes. */
14928 first_unchanged_at_end_row
14929 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14930 IF_DEBUG (debug_delta = delta);
14931 IF_DEBUG (debug_delta_bytes = delta_bytes);
14932
14933 /* Set stop_pos to the buffer position up to which we will have to
14934 display new lines. If first_unchanged_at_end_row != NULL, this
14935 is the buffer position of the start of the line displayed in that
14936 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14937 that we don't stop at a buffer position. */
14938 stop_pos = 0;
14939 if (first_unchanged_at_end_row)
14940 {
14941 xassert (last_unchanged_at_beg_row == NULL
14942 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14943
14944 /* If this is a continuation line, move forward to the next one
14945 that isn't. Changes in lines above affect this line.
14946 Caution: this may move first_unchanged_at_end_row to a row
14947 not displaying text. */
14948 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14949 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14950 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14951 < it.last_visible_y))
14952 ++first_unchanged_at_end_row;
14953
14954 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14955 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14956 >= it.last_visible_y))
14957 first_unchanged_at_end_row = NULL;
14958 else
14959 {
14960 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14961 + delta);
14962 first_unchanged_at_end_vpos
14963 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14964 xassert (stop_pos >= Z - END_UNCHANGED);
14965 }
14966 }
14967 else if (last_unchanged_at_beg_row == NULL)
14968 GIVE_UP (19);
14969
14970
14971 #if GLYPH_DEBUG
14972
14973 /* Either there is no unchanged row at the end, or the one we have
14974 now displays text. This is a necessary condition for the window
14975 end pos calculation at the end of this function. */
14976 xassert (first_unchanged_at_end_row == NULL
14977 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14978
14979 debug_last_unchanged_at_beg_vpos
14980 = (last_unchanged_at_beg_row
14981 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14982 : -1);
14983 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14984
14985 #endif /* GLYPH_DEBUG != 0 */
14986
14987
14988 /* Display new lines. Set last_text_row to the last new line
14989 displayed which has text on it, i.e. might end up as being the
14990 line where the window_end_vpos is. */
14991 w->cursor.vpos = -1;
14992 last_text_row = NULL;
14993 overlay_arrow_seen = 0;
14994 while (it.current_y < it.last_visible_y
14995 && !fonts_changed_p
14996 && (first_unchanged_at_end_row == NULL
14997 || IT_CHARPOS (it) < stop_pos))
14998 {
14999 if (display_line (&it))
15000 last_text_row = it.glyph_row - 1;
15001 }
15002
15003 if (fonts_changed_p)
15004 return -1;
15005
15006
15007 /* Compute differences in buffer positions, y-positions etc. for
15008 lines reused at the bottom of the window. Compute what we can
15009 scroll. */
15010 if (first_unchanged_at_end_row
15011 /* No lines reused because we displayed everything up to the
15012 bottom of the window. */
15013 && it.current_y < it.last_visible_y)
15014 {
15015 dvpos = (it.vpos
15016 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15017 current_matrix));
15018 dy = it.current_y - first_unchanged_at_end_row->y;
15019 run.current_y = first_unchanged_at_end_row->y;
15020 run.desired_y = run.current_y + dy;
15021 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15022 }
15023 else
15024 {
15025 delta = delta_bytes = dvpos = dy
15026 = run.current_y = run.desired_y = run.height = 0;
15027 first_unchanged_at_end_row = NULL;
15028 }
15029 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15030
15031
15032 /* Find the cursor if not already found. We have to decide whether
15033 PT will appear on this window (it sometimes doesn't, but this is
15034 not a very frequent case.) This decision has to be made before
15035 the current matrix is altered. A value of cursor.vpos < 0 means
15036 that PT is either in one of the lines beginning at
15037 first_unchanged_at_end_row or below the window. Don't care for
15038 lines that might be displayed later at the window end; as
15039 mentioned, this is not a frequent case. */
15040 if (w->cursor.vpos < 0)
15041 {
15042 /* Cursor in unchanged rows at the top? */
15043 if (PT < CHARPOS (start_pos)
15044 && last_unchanged_at_beg_row)
15045 {
15046 row = row_containing_pos (w, PT,
15047 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15048 last_unchanged_at_beg_row + 1, 0);
15049 if (row)
15050 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15051 }
15052
15053 /* Start from first_unchanged_at_end_row looking for PT. */
15054 else if (first_unchanged_at_end_row)
15055 {
15056 row = row_containing_pos (w, PT - delta,
15057 first_unchanged_at_end_row, NULL, 0);
15058 if (row)
15059 set_cursor_from_row (w, row, w->current_matrix, delta,
15060 delta_bytes, dy, dvpos);
15061 }
15062
15063 /* Give up if cursor was not found. */
15064 if (w->cursor.vpos < 0)
15065 {
15066 clear_glyph_matrix (w->desired_matrix);
15067 return -1;
15068 }
15069 }
15070
15071 /* Don't let the cursor end in the scroll margins. */
15072 {
15073 int this_scroll_margin, cursor_height;
15074
15075 this_scroll_margin = max (0, scroll_margin);
15076 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15077 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15078 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15079
15080 if ((w->cursor.y < this_scroll_margin
15081 && CHARPOS (start) > BEGV)
15082 /* Old redisplay didn't take scroll margin into account at the bottom,
15083 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15084 || (w->cursor.y + (make_cursor_line_fully_visible_p
15085 ? cursor_height + this_scroll_margin
15086 : 1)) > it.last_visible_y)
15087 {
15088 w->cursor.vpos = -1;
15089 clear_glyph_matrix (w->desired_matrix);
15090 return -1;
15091 }
15092 }
15093
15094 /* Scroll the display. Do it before changing the current matrix so
15095 that xterm.c doesn't get confused about where the cursor glyph is
15096 found. */
15097 if (dy && run.height)
15098 {
15099 update_begin (f);
15100
15101 if (FRAME_WINDOW_P (f))
15102 {
15103 FRAME_RIF (f)->update_window_begin_hook (w);
15104 FRAME_RIF (f)->clear_window_mouse_face (w);
15105 FRAME_RIF (f)->scroll_run_hook (w, &run);
15106 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15107 }
15108 else
15109 {
15110 /* Terminal frame. In this case, dvpos gives the number of
15111 lines to scroll by; dvpos < 0 means scroll up. */
15112 int first_unchanged_at_end_vpos
15113 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15114 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15115 int end = (WINDOW_TOP_EDGE_LINE (w)
15116 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15117 + window_internal_height (w));
15118
15119 /* Perform the operation on the screen. */
15120 if (dvpos > 0)
15121 {
15122 /* Scroll last_unchanged_at_beg_row to the end of the
15123 window down dvpos lines. */
15124 set_terminal_window (f, end);
15125
15126 /* On dumb terminals delete dvpos lines at the end
15127 before inserting dvpos empty lines. */
15128 if (!FRAME_SCROLL_REGION_OK (f))
15129 ins_del_lines (f, end - dvpos, -dvpos);
15130
15131 /* Insert dvpos empty lines in front of
15132 last_unchanged_at_beg_row. */
15133 ins_del_lines (f, from, dvpos);
15134 }
15135 else if (dvpos < 0)
15136 {
15137 /* Scroll up last_unchanged_at_beg_vpos to the end of
15138 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15139 set_terminal_window (f, end);
15140
15141 /* Delete dvpos lines in front of
15142 last_unchanged_at_beg_vpos. ins_del_lines will set
15143 the cursor to the given vpos and emit |dvpos| delete
15144 line sequences. */
15145 ins_del_lines (f, from + dvpos, dvpos);
15146
15147 /* On a dumb terminal insert dvpos empty lines at the
15148 end. */
15149 if (!FRAME_SCROLL_REGION_OK (f))
15150 ins_del_lines (f, end + dvpos, -dvpos);
15151 }
15152
15153 set_terminal_window (f, 0);
15154 }
15155
15156 update_end (f);
15157 }
15158
15159 /* Shift reused rows of the current matrix to the right position.
15160 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15161 text. */
15162 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15163 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15164 if (dvpos < 0)
15165 {
15166 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15167 bottom_vpos, dvpos);
15168 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15169 bottom_vpos, 0);
15170 }
15171 else if (dvpos > 0)
15172 {
15173 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15174 bottom_vpos, dvpos);
15175 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15176 first_unchanged_at_end_vpos + dvpos, 0);
15177 }
15178
15179 /* For frame-based redisplay, make sure that current frame and window
15180 matrix are in sync with respect to glyph memory. */
15181 if (!FRAME_WINDOW_P (f))
15182 sync_frame_with_window_matrix_rows (w);
15183
15184 /* Adjust buffer positions in reused rows. */
15185 if (delta || delta_bytes)
15186 increment_matrix_positions (current_matrix,
15187 first_unchanged_at_end_vpos + dvpos,
15188 bottom_vpos, delta, delta_bytes);
15189
15190 /* Adjust Y positions. */
15191 if (dy)
15192 shift_glyph_matrix (w, current_matrix,
15193 first_unchanged_at_end_vpos + dvpos,
15194 bottom_vpos, dy);
15195
15196 if (first_unchanged_at_end_row)
15197 {
15198 first_unchanged_at_end_row += dvpos;
15199 if (first_unchanged_at_end_row->y >= it.last_visible_y
15200 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15201 first_unchanged_at_end_row = NULL;
15202 }
15203
15204 /* If scrolling up, there may be some lines to display at the end of
15205 the window. */
15206 last_text_row_at_end = NULL;
15207 if (dy < 0)
15208 {
15209 /* Scrolling up can leave for example a partially visible line
15210 at the end of the window to be redisplayed. */
15211 /* Set last_row to the glyph row in the current matrix where the
15212 window end line is found. It has been moved up or down in
15213 the matrix by dvpos. */
15214 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15215 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15216
15217 /* If last_row is the window end line, it should display text. */
15218 xassert (last_row->displays_text_p);
15219
15220 /* If window end line was partially visible before, begin
15221 displaying at that line. Otherwise begin displaying with the
15222 line following it. */
15223 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15224 {
15225 init_to_row_start (&it, w, last_row);
15226 it.vpos = last_vpos;
15227 it.current_y = last_row->y;
15228 }
15229 else
15230 {
15231 init_to_row_end (&it, w, last_row);
15232 it.vpos = 1 + last_vpos;
15233 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15234 ++last_row;
15235 }
15236
15237 /* We may start in a continuation line. If so, we have to
15238 get the right continuation_lines_width and current_x. */
15239 it.continuation_lines_width = last_row->continuation_lines_width;
15240 it.hpos = it.current_x = 0;
15241
15242 /* Display the rest of the lines at the window end. */
15243 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15244 while (it.current_y < it.last_visible_y
15245 && !fonts_changed_p)
15246 {
15247 /* Is it always sure that the display agrees with lines in
15248 the current matrix? I don't think so, so we mark rows
15249 displayed invalid in the current matrix by setting their
15250 enabled_p flag to zero. */
15251 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15252 if (display_line (&it))
15253 last_text_row_at_end = it.glyph_row - 1;
15254 }
15255 }
15256
15257 /* Update window_end_pos and window_end_vpos. */
15258 if (first_unchanged_at_end_row
15259 && !last_text_row_at_end)
15260 {
15261 /* Window end line if one of the preserved rows from the current
15262 matrix. Set row to the last row displaying text in current
15263 matrix starting at first_unchanged_at_end_row, after
15264 scrolling. */
15265 xassert (first_unchanged_at_end_row->displays_text_p);
15266 row = find_last_row_displaying_text (w->current_matrix, &it,
15267 first_unchanged_at_end_row);
15268 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15269
15270 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15271 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15272 w->window_end_vpos
15273 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15274 xassert (w->window_end_bytepos >= 0);
15275 IF_DEBUG (debug_method_add (w, "A"));
15276 }
15277 else if (last_text_row_at_end)
15278 {
15279 w->window_end_pos
15280 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15281 w->window_end_bytepos
15282 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15283 w->window_end_vpos
15284 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15285 xassert (w->window_end_bytepos >= 0);
15286 IF_DEBUG (debug_method_add (w, "B"));
15287 }
15288 else if (last_text_row)
15289 {
15290 /* We have displayed either to the end of the window or at the
15291 end of the window, i.e. the last row with text is to be found
15292 in the desired matrix. */
15293 w->window_end_pos
15294 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15295 w->window_end_bytepos
15296 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15297 w->window_end_vpos
15298 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15299 xassert (w->window_end_bytepos >= 0);
15300 }
15301 else if (first_unchanged_at_end_row == NULL
15302 && last_text_row == NULL
15303 && last_text_row_at_end == NULL)
15304 {
15305 /* Displayed to end of window, but no line containing text was
15306 displayed. Lines were deleted at the end of the window. */
15307 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15308 int vpos = XFASTINT (w->window_end_vpos);
15309 struct glyph_row *current_row = current_matrix->rows + vpos;
15310 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15311
15312 for (row = NULL;
15313 row == NULL && vpos >= first_vpos;
15314 --vpos, --current_row, --desired_row)
15315 {
15316 if (desired_row->enabled_p)
15317 {
15318 if (desired_row->displays_text_p)
15319 row = desired_row;
15320 }
15321 else if (current_row->displays_text_p)
15322 row = current_row;
15323 }
15324
15325 xassert (row != NULL);
15326 w->window_end_vpos = make_number (vpos + 1);
15327 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15328 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15329 xassert (w->window_end_bytepos >= 0);
15330 IF_DEBUG (debug_method_add (w, "C"));
15331 }
15332 else
15333 abort ();
15334
15335 #if 0 /* This leads to problems, for instance when the cursor is
15336 at ZV, and the cursor line displays no text. */
15337 /* Disable rows below what's displayed in the window. This makes
15338 debugging easier. */
15339 enable_glyph_matrix_rows (current_matrix,
15340 XFASTINT (w->window_end_vpos) + 1,
15341 bottom_vpos, 0);
15342 #endif
15343
15344 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15345 debug_end_vpos = XFASTINT (w->window_end_vpos));
15346
15347 /* Record that display has not been completed. */
15348 w->window_end_valid = Qnil;
15349 w->desired_matrix->no_scrolling_p = 1;
15350 return 3;
15351
15352 #undef GIVE_UP
15353 }
15354
15355
15356 \f
15357 /***********************************************************************
15358 More debugging support
15359 ***********************************************************************/
15360
15361 #if GLYPH_DEBUG
15362
15363 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15364 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15365 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15366
15367
15368 /* Dump the contents of glyph matrix MATRIX on stderr.
15369
15370 GLYPHS 0 means don't show glyph contents.
15371 GLYPHS 1 means show glyphs in short form
15372 GLYPHS > 1 means show glyphs in long form. */
15373
15374 void
15375 dump_glyph_matrix (matrix, glyphs)
15376 struct glyph_matrix *matrix;
15377 int glyphs;
15378 {
15379 int i;
15380 for (i = 0; i < matrix->nrows; ++i)
15381 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15382 }
15383
15384
15385 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15386 the glyph row and area where the glyph comes from. */
15387
15388 void
15389 dump_glyph (row, glyph, area)
15390 struct glyph_row *row;
15391 struct glyph *glyph;
15392 int area;
15393 {
15394 if (glyph->type == CHAR_GLYPH)
15395 {
15396 fprintf (stderr,
15397 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15398 glyph - row->glyphs[TEXT_AREA],
15399 'C',
15400 glyph->charpos,
15401 (BUFFERP (glyph->object)
15402 ? 'B'
15403 : (STRINGP (glyph->object)
15404 ? 'S'
15405 : '-')),
15406 glyph->pixel_width,
15407 glyph->u.ch,
15408 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15409 ? glyph->u.ch
15410 : '.'),
15411 glyph->face_id,
15412 glyph->left_box_line_p,
15413 glyph->right_box_line_p);
15414 }
15415 else if (glyph->type == STRETCH_GLYPH)
15416 {
15417 fprintf (stderr,
15418 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15419 glyph - row->glyphs[TEXT_AREA],
15420 'S',
15421 glyph->charpos,
15422 (BUFFERP (glyph->object)
15423 ? 'B'
15424 : (STRINGP (glyph->object)
15425 ? 'S'
15426 : '-')),
15427 glyph->pixel_width,
15428 0,
15429 '.',
15430 glyph->face_id,
15431 glyph->left_box_line_p,
15432 glyph->right_box_line_p);
15433 }
15434 else if (glyph->type == IMAGE_GLYPH)
15435 {
15436 fprintf (stderr,
15437 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15438 glyph - row->glyphs[TEXT_AREA],
15439 'I',
15440 glyph->charpos,
15441 (BUFFERP (glyph->object)
15442 ? 'B'
15443 : (STRINGP (glyph->object)
15444 ? 'S'
15445 : '-')),
15446 glyph->pixel_width,
15447 glyph->u.img_id,
15448 '.',
15449 glyph->face_id,
15450 glyph->left_box_line_p,
15451 glyph->right_box_line_p);
15452 }
15453 else if (glyph->type == COMPOSITE_GLYPH)
15454 {
15455 fprintf (stderr,
15456 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15457 glyph - row->glyphs[TEXT_AREA],
15458 '+',
15459 glyph->charpos,
15460 (BUFFERP (glyph->object)
15461 ? 'B'
15462 : (STRINGP (glyph->object)
15463 ? 'S'
15464 : '-')),
15465 glyph->pixel_width,
15466 glyph->u.cmp_id,
15467 '.',
15468 glyph->face_id,
15469 glyph->left_box_line_p,
15470 glyph->right_box_line_p);
15471 }
15472 }
15473
15474
15475 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15476 GLYPHS 0 means don't show glyph contents.
15477 GLYPHS 1 means show glyphs in short form
15478 GLYPHS > 1 means show glyphs in long form. */
15479
15480 void
15481 dump_glyph_row (row, vpos, glyphs)
15482 struct glyph_row *row;
15483 int vpos, glyphs;
15484 {
15485 if (glyphs != 1)
15486 {
15487 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15488 fprintf (stderr, "======================================================================\n");
15489
15490 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15491 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15492 vpos,
15493 MATRIX_ROW_START_CHARPOS (row),
15494 MATRIX_ROW_END_CHARPOS (row),
15495 row->used[TEXT_AREA],
15496 row->contains_overlapping_glyphs_p,
15497 row->enabled_p,
15498 row->truncated_on_left_p,
15499 row->truncated_on_right_p,
15500 row->continued_p,
15501 MATRIX_ROW_CONTINUATION_LINE_P (row),
15502 row->displays_text_p,
15503 row->ends_at_zv_p,
15504 row->fill_line_p,
15505 row->ends_in_middle_of_char_p,
15506 row->starts_in_middle_of_char_p,
15507 row->mouse_face_p,
15508 row->x,
15509 row->y,
15510 row->pixel_width,
15511 row->height,
15512 row->visible_height,
15513 row->ascent,
15514 row->phys_ascent);
15515 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15516 row->end.overlay_string_index,
15517 row->continuation_lines_width);
15518 fprintf (stderr, "%9d %5d\n",
15519 CHARPOS (row->start.string_pos),
15520 CHARPOS (row->end.string_pos));
15521 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15522 row->end.dpvec_index);
15523 }
15524
15525 if (glyphs > 1)
15526 {
15527 int area;
15528
15529 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15530 {
15531 struct glyph *glyph = row->glyphs[area];
15532 struct glyph *glyph_end = glyph + row->used[area];
15533
15534 /* Glyph for a line end in text. */
15535 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15536 ++glyph_end;
15537
15538 if (glyph < glyph_end)
15539 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15540
15541 for (; glyph < glyph_end; ++glyph)
15542 dump_glyph (row, glyph, area);
15543 }
15544 }
15545 else if (glyphs == 1)
15546 {
15547 int area;
15548
15549 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15550 {
15551 char *s = (char *) alloca (row->used[area] + 1);
15552 int i;
15553
15554 for (i = 0; i < row->used[area]; ++i)
15555 {
15556 struct glyph *glyph = row->glyphs[area] + i;
15557 if (glyph->type == CHAR_GLYPH
15558 && glyph->u.ch < 0x80
15559 && glyph->u.ch >= ' ')
15560 s[i] = glyph->u.ch;
15561 else
15562 s[i] = '.';
15563 }
15564
15565 s[i] = '\0';
15566 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15567 }
15568 }
15569 }
15570
15571
15572 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15573 Sdump_glyph_matrix, 0, 1, "p",
15574 doc: /* Dump the current matrix of the selected window to stderr.
15575 Shows contents of glyph row structures. With non-nil
15576 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15577 glyphs in short form, otherwise show glyphs in long form. */)
15578 (glyphs)
15579 Lisp_Object glyphs;
15580 {
15581 struct window *w = XWINDOW (selected_window);
15582 struct buffer *buffer = XBUFFER (w->buffer);
15583
15584 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15585 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15586 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15587 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15588 fprintf (stderr, "=============================================\n");
15589 dump_glyph_matrix (w->current_matrix,
15590 NILP (glyphs) ? 0 : XINT (glyphs));
15591 return Qnil;
15592 }
15593
15594
15595 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15596 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15597 ()
15598 {
15599 struct frame *f = XFRAME (selected_frame);
15600 dump_glyph_matrix (f->current_matrix, 1);
15601 return Qnil;
15602 }
15603
15604
15605 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15606 doc: /* Dump glyph row ROW to stderr.
15607 GLYPH 0 means don't dump glyphs.
15608 GLYPH 1 means dump glyphs in short form.
15609 GLYPH > 1 or omitted means dump glyphs in long form. */)
15610 (row, glyphs)
15611 Lisp_Object row, glyphs;
15612 {
15613 struct glyph_matrix *matrix;
15614 int vpos;
15615
15616 CHECK_NUMBER (row);
15617 matrix = XWINDOW (selected_window)->current_matrix;
15618 vpos = XINT (row);
15619 if (vpos >= 0 && vpos < matrix->nrows)
15620 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15621 vpos,
15622 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15623 return Qnil;
15624 }
15625
15626
15627 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15628 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15629 GLYPH 0 means don't dump glyphs.
15630 GLYPH 1 means dump glyphs in short form.
15631 GLYPH > 1 or omitted means dump glyphs in long form. */)
15632 (row, glyphs)
15633 Lisp_Object row, glyphs;
15634 {
15635 struct frame *sf = SELECTED_FRAME ();
15636 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15637 int vpos;
15638
15639 CHECK_NUMBER (row);
15640 vpos = XINT (row);
15641 if (vpos >= 0 && vpos < m->nrows)
15642 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15643 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15644 return Qnil;
15645 }
15646
15647
15648 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15649 doc: /* Toggle tracing of redisplay.
15650 With ARG, turn tracing on if and only if ARG is positive. */)
15651 (arg)
15652 Lisp_Object arg;
15653 {
15654 if (NILP (arg))
15655 trace_redisplay_p = !trace_redisplay_p;
15656 else
15657 {
15658 arg = Fprefix_numeric_value (arg);
15659 trace_redisplay_p = XINT (arg) > 0;
15660 }
15661
15662 return Qnil;
15663 }
15664
15665
15666 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15667 doc: /* Like `format', but print result to stderr.
15668 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15669 (nargs, args)
15670 int nargs;
15671 Lisp_Object *args;
15672 {
15673 Lisp_Object s = Fformat (nargs, args);
15674 fprintf (stderr, "%s", SDATA (s));
15675 return Qnil;
15676 }
15677
15678 #endif /* GLYPH_DEBUG */
15679
15680
15681 \f
15682 /***********************************************************************
15683 Building Desired Matrix Rows
15684 ***********************************************************************/
15685
15686 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15687 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15688
15689 static struct glyph_row *
15690 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15691 struct window *w;
15692 Lisp_Object overlay_arrow_string;
15693 {
15694 struct frame *f = XFRAME (WINDOW_FRAME (w));
15695 struct buffer *buffer = XBUFFER (w->buffer);
15696 struct buffer *old = current_buffer;
15697 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15698 int arrow_len = SCHARS (overlay_arrow_string);
15699 const unsigned char *arrow_end = arrow_string + arrow_len;
15700 const unsigned char *p;
15701 struct it it;
15702 int multibyte_p;
15703 int n_glyphs_before;
15704
15705 set_buffer_temp (buffer);
15706 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15707 it.glyph_row->used[TEXT_AREA] = 0;
15708 SET_TEXT_POS (it.position, 0, 0);
15709
15710 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15711 p = arrow_string;
15712 while (p < arrow_end)
15713 {
15714 Lisp_Object face, ilisp;
15715
15716 /* Get the next character. */
15717 if (multibyte_p)
15718 it.c = string_char_and_length (p, arrow_len, &it.len);
15719 else
15720 it.c = *p, it.len = 1;
15721 p += it.len;
15722
15723 /* Get its face. */
15724 ilisp = make_number (p - arrow_string);
15725 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15726 it.face_id = compute_char_face (f, it.c, face);
15727
15728 /* Compute its width, get its glyphs. */
15729 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15730 SET_TEXT_POS (it.position, -1, -1);
15731 PRODUCE_GLYPHS (&it);
15732
15733 /* If this character doesn't fit any more in the line, we have
15734 to remove some glyphs. */
15735 if (it.current_x > it.last_visible_x)
15736 {
15737 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15738 break;
15739 }
15740 }
15741
15742 set_buffer_temp (old);
15743 return it.glyph_row;
15744 }
15745
15746
15747 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15748 glyphs are only inserted for terminal frames since we can't really
15749 win with truncation glyphs when partially visible glyphs are
15750 involved. Which glyphs to insert is determined by
15751 produce_special_glyphs. */
15752
15753 static void
15754 insert_left_trunc_glyphs (it)
15755 struct it *it;
15756 {
15757 struct it truncate_it;
15758 struct glyph *from, *end, *to, *toend;
15759
15760 xassert (!FRAME_WINDOW_P (it->f));
15761
15762 /* Get the truncation glyphs. */
15763 truncate_it = *it;
15764 truncate_it.current_x = 0;
15765 truncate_it.face_id = DEFAULT_FACE_ID;
15766 truncate_it.glyph_row = &scratch_glyph_row;
15767 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15768 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15769 truncate_it.object = make_number (0);
15770 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15771
15772 /* Overwrite glyphs from IT with truncation glyphs. */
15773 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15774 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15775 to = it->glyph_row->glyphs[TEXT_AREA];
15776 toend = to + it->glyph_row->used[TEXT_AREA];
15777
15778 while (from < end)
15779 *to++ = *from++;
15780
15781 /* There may be padding glyphs left over. Overwrite them too. */
15782 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15783 {
15784 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15785 while (from < end)
15786 *to++ = *from++;
15787 }
15788
15789 if (to > toend)
15790 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15791 }
15792
15793
15794 /* Compute the pixel height and width of IT->glyph_row.
15795
15796 Most of the time, ascent and height of a display line will be equal
15797 to the max_ascent and max_height values of the display iterator
15798 structure. This is not the case if
15799
15800 1. We hit ZV without displaying anything. In this case, max_ascent
15801 and max_height will be zero.
15802
15803 2. We have some glyphs that don't contribute to the line height.
15804 (The glyph row flag contributes_to_line_height_p is for future
15805 pixmap extensions).
15806
15807 The first case is easily covered by using default values because in
15808 these cases, the line height does not really matter, except that it
15809 must not be zero. */
15810
15811 static void
15812 compute_line_metrics (it)
15813 struct it *it;
15814 {
15815 struct glyph_row *row = it->glyph_row;
15816 int area, i;
15817
15818 if (FRAME_WINDOW_P (it->f))
15819 {
15820 int i, min_y, max_y;
15821
15822 /* The line may consist of one space only, that was added to
15823 place the cursor on it. If so, the row's height hasn't been
15824 computed yet. */
15825 if (row->height == 0)
15826 {
15827 if (it->max_ascent + it->max_descent == 0)
15828 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15829 row->ascent = it->max_ascent;
15830 row->height = it->max_ascent + it->max_descent;
15831 row->phys_ascent = it->max_phys_ascent;
15832 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15833 row->extra_line_spacing = it->max_extra_line_spacing;
15834 }
15835
15836 /* Compute the width of this line. */
15837 row->pixel_width = row->x;
15838 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15839 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15840
15841 xassert (row->pixel_width >= 0);
15842 xassert (row->ascent >= 0 && row->height > 0);
15843
15844 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15845 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15846
15847 /* If first line's physical ascent is larger than its logical
15848 ascent, use the physical ascent, and make the row taller.
15849 This makes accented characters fully visible. */
15850 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15851 && row->phys_ascent > row->ascent)
15852 {
15853 row->height += row->phys_ascent - row->ascent;
15854 row->ascent = row->phys_ascent;
15855 }
15856
15857 /* Compute how much of the line is visible. */
15858 row->visible_height = row->height;
15859
15860 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15861 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15862
15863 if (row->y < min_y)
15864 row->visible_height -= min_y - row->y;
15865 if (row->y + row->height > max_y)
15866 row->visible_height -= row->y + row->height - max_y;
15867 }
15868 else
15869 {
15870 row->pixel_width = row->used[TEXT_AREA];
15871 if (row->continued_p)
15872 row->pixel_width -= it->continuation_pixel_width;
15873 else if (row->truncated_on_right_p)
15874 row->pixel_width -= it->truncation_pixel_width;
15875 row->ascent = row->phys_ascent = 0;
15876 row->height = row->phys_height = row->visible_height = 1;
15877 row->extra_line_spacing = 0;
15878 }
15879
15880 /* Compute a hash code for this row. */
15881 row->hash = 0;
15882 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15883 for (i = 0; i < row->used[area]; ++i)
15884 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15885 + row->glyphs[area][i].u.val
15886 + row->glyphs[area][i].face_id
15887 + row->glyphs[area][i].padding_p
15888 + (row->glyphs[area][i].type << 2));
15889
15890 it->max_ascent = it->max_descent = 0;
15891 it->max_phys_ascent = it->max_phys_descent = 0;
15892 }
15893
15894
15895 /* Append one space to the glyph row of iterator IT if doing a
15896 window-based redisplay. The space has the same face as
15897 IT->face_id. Value is non-zero if a space was added.
15898
15899 This function is called to make sure that there is always one glyph
15900 at the end of a glyph row that the cursor can be set on under
15901 window-systems. (If there weren't such a glyph we would not know
15902 how wide and tall a box cursor should be displayed).
15903
15904 At the same time this space let's a nicely handle clearing to the
15905 end of the line if the row ends in italic text. */
15906
15907 static int
15908 append_space_for_newline (it, default_face_p)
15909 struct it *it;
15910 int default_face_p;
15911 {
15912 if (FRAME_WINDOW_P (it->f))
15913 {
15914 int n = it->glyph_row->used[TEXT_AREA];
15915
15916 if (it->glyph_row->glyphs[TEXT_AREA] + n
15917 < it->glyph_row->glyphs[1 + TEXT_AREA])
15918 {
15919 /* Save some values that must not be changed.
15920 Must save IT->c and IT->len because otherwise
15921 ITERATOR_AT_END_P wouldn't work anymore after
15922 append_space_for_newline has been called. */
15923 enum display_element_type saved_what = it->what;
15924 int saved_c = it->c, saved_len = it->len;
15925 int saved_x = it->current_x;
15926 int saved_face_id = it->face_id;
15927 struct text_pos saved_pos;
15928 Lisp_Object saved_object;
15929 struct face *face;
15930
15931 saved_object = it->object;
15932 saved_pos = it->position;
15933
15934 it->what = IT_CHARACTER;
15935 bzero (&it->position, sizeof it->position);
15936 it->object = make_number (0);
15937 it->c = ' ';
15938 it->len = 1;
15939
15940 if (default_face_p)
15941 it->face_id = DEFAULT_FACE_ID;
15942 else if (it->face_before_selective_p)
15943 it->face_id = it->saved_face_id;
15944 face = FACE_FROM_ID (it->f, it->face_id);
15945 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15946
15947 PRODUCE_GLYPHS (it);
15948
15949 it->override_ascent = -1;
15950 it->constrain_row_ascent_descent_p = 0;
15951 it->current_x = saved_x;
15952 it->object = saved_object;
15953 it->position = saved_pos;
15954 it->what = saved_what;
15955 it->face_id = saved_face_id;
15956 it->len = saved_len;
15957 it->c = saved_c;
15958 return 1;
15959 }
15960 }
15961
15962 return 0;
15963 }
15964
15965
15966 /* Extend the face of the last glyph in the text area of IT->glyph_row
15967 to the end of the display line. Called from display_line.
15968 If the glyph row is empty, add a space glyph to it so that we
15969 know the face to draw. Set the glyph row flag fill_line_p. */
15970
15971 static void
15972 extend_face_to_end_of_line (it)
15973 struct it *it;
15974 {
15975 struct face *face;
15976 struct frame *f = it->f;
15977
15978 /* If line is already filled, do nothing. */
15979 if (it->current_x >= it->last_visible_x)
15980 return;
15981
15982 /* Face extension extends the background and box of IT->face_id
15983 to the end of the line. If the background equals the background
15984 of the frame, we don't have to do anything. */
15985 if (it->face_before_selective_p)
15986 face = FACE_FROM_ID (it->f, it->saved_face_id);
15987 else
15988 face = FACE_FROM_ID (f, it->face_id);
15989
15990 if (FRAME_WINDOW_P (f)
15991 && it->glyph_row->displays_text_p
15992 && face->box == FACE_NO_BOX
15993 && face->background == FRAME_BACKGROUND_PIXEL (f)
15994 && !face->stipple)
15995 return;
15996
15997 /* Set the glyph row flag indicating that the face of the last glyph
15998 in the text area has to be drawn to the end of the text area. */
15999 it->glyph_row->fill_line_p = 1;
16000
16001 /* If current character of IT is not ASCII, make sure we have the
16002 ASCII face. This will be automatically undone the next time
16003 get_next_display_element returns a multibyte character. Note
16004 that the character will always be single byte in unibyte text. */
16005 if (!ASCII_CHAR_P (it->c))
16006 {
16007 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16008 }
16009
16010 if (FRAME_WINDOW_P (f))
16011 {
16012 /* If the row is empty, add a space with the current face of IT,
16013 so that we know which face to draw. */
16014 if (it->glyph_row->used[TEXT_AREA] == 0)
16015 {
16016 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16017 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16018 it->glyph_row->used[TEXT_AREA] = 1;
16019 }
16020 }
16021 else
16022 {
16023 /* Save some values that must not be changed. */
16024 int saved_x = it->current_x;
16025 struct text_pos saved_pos;
16026 Lisp_Object saved_object;
16027 enum display_element_type saved_what = it->what;
16028 int saved_face_id = it->face_id;
16029
16030 saved_object = it->object;
16031 saved_pos = it->position;
16032
16033 it->what = IT_CHARACTER;
16034 bzero (&it->position, sizeof it->position);
16035 it->object = make_number (0);
16036 it->c = ' ';
16037 it->len = 1;
16038 it->face_id = face->id;
16039
16040 PRODUCE_GLYPHS (it);
16041
16042 while (it->current_x <= it->last_visible_x)
16043 PRODUCE_GLYPHS (it);
16044
16045 /* Don't count these blanks really. It would let us insert a left
16046 truncation glyph below and make us set the cursor on them, maybe. */
16047 it->current_x = saved_x;
16048 it->object = saved_object;
16049 it->position = saved_pos;
16050 it->what = saved_what;
16051 it->face_id = saved_face_id;
16052 }
16053 }
16054
16055
16056 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16057 trailing whitespace. */
16058
16059 static int
16060 trailing_whitespace_p (charpos)
16061 int charpos;
16062 {
16063 int bytepos = CHAR_TO_BYTE (charpos);
16064 int c = 0;
16065
16066 while (bytepos < ZV_BYTE
16067 && (c = FETCH_CHAR (bytepos),
16068 c == ' ' || c == '\t'))
16069 ++bytepos;
16070
16071 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16072 {
16073 if (bytepos != PT_BYTE)
16074 return 1;
16075 }
16076 return 0;
16077 }
16078
16079
16080 /* Highlight trailing whitespace, if any, in ROW. */
16081
16082 void
16083 highlight_trailing_whitespace (f, row)
16084 struct frame *f;
16085 struct glyph_row *row;
16086 {
16087 int used = row->used[TEXT_AREA];
16088
16089 if (used)
16090 {
16091 struct glyph *start = row->glyphs[TEXT_AREA];
16092 struct glyph *glyph = start + used - 1;
16093
16094 /* Skip over glyphs inserted to display the cursor at the
16095 end of a line, for extending the face of the last glyph
16096 to the end of the line on terminals, and for truncation
16097 and continuation glyphs. */
16098 while (glyph >= start
16099 && glyph->type == CHAR_GLYPH
16100 && INTEGERP (glyph->object))
16101 --glyph;
16102
16103 /* If last glyph is a space or stretch, and it's trailing
16104 whitespace, set the face of all trailing whitespace glyphs in
16105 IT->glyph_row to `trailing-whitespace'. */
16106 if (glyph >= start
16107 && BUFFERP (glyph->object)
16108 && (glyph->type == STRETCH_GLYPH
16109 || (glyph->type == CHAR_GLYPH
16110 && glyph->u.ch == ' '))
16111 && trailing_whitespace_p (glyph->charpos))
16112 {
16113 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16114 if (face_id < 0)
16115 return;
16116
16117 while (glyph >= start
16118 && BUFFERP (glyph->object)
16119 && (glyph->type == STRETCH_GLYPH
16120 || (glyph->type == CHAR_GLYPH
16121 && glyph->u.ch == ' ')))
16122 (glyph--)->face_id = face_id;
16123 }
16124 }
16125 }
16126
16127
16128 /* Value is non-zero if glyph row ROW in window W should be
16129 used to hold the cursor. */
16130
16131 static int
16132 cursor_row_p (w, row)
16133 struct window *w;
16134 struct glyph_row *row;
16135 {
16136 int cursor_row_p = 1;
16137
16138 if (PT == MATRIX_ROW_END_CHARPOS (row))
16139 {
16140 /* Suppose the row ends on a string.
16141 Unless the row is continued, that means it ends on a newline
16142 in the string. If it's anything other than a display string
16143 (e.g. a before-string from an overlay), we don't want the
16144 cursor there. (This heuristic seems to give the optimal
16145 behavior for the various types of multi-line strings.) */
16146 if (CHARPOS (row->end.string_pos) >= 0)
16147 {
16148 if (row->continued_p)
16149 cursor_row_p = 1;
16150 else
16151 {
16152 /* Check for `display' property. */
16153 struct glyph *beg = row->glyphs[TEXT_AREA];
16154 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16155 struct glyph *glyph;
16156
16157 cursor_row_p = 0;
16158 for (glyph = end; glyph >= beg; --glyph)
16159 if (STRINGP (glyph->object))
16160 {
16161 Lisp_Object prop
16162 = Fget_char_property (make_number (PT),
16163 Qdisplay, Qnil);
16164 cursor_row_p =
16165 (!NILP (prop)
16166 && display_prop_string_p (prop, glyph->object));
16167 break;
16168 }
16169 }
16170 }
16171 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16172 {
16173 /* If the row ends in middle of a real character,
16174 and the line is continued, we want the cursor here.
16175 That's because MATRIX_ROW_END_CHARPOS would equal
16176 PT if PT is before the character. */
16177 if (!row->ends_in_ellipsis_p)
16178 cursor_row_p = row->continued_p;
16179 else
16180 /* If the row ends in an ellipsis, then
16181 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16182 We want that position to be displayed after the ellipsis. */
16183 cursor_row_p = 0;
16184 }
16185 /* If the row ends at ZV, display the cursor at the end of that
16186 row instead of at the start of the row below. */
16187 else if (row->ends_at_zv_p)
16188 cursor_row_p = 1;
16189 else
16190 cursor_row_p = 0;
16191 }
16192
16193 return cursor_row_p;
16194 }
16195
16196
16197 /* Construct the glyph row IT->glyph_row in the desired matrix of
16198 IT->w from text at the current position of IT. See dispextern.h
16199 for an overview of struct it. Value is non-zero if
16200 IT->glyph_row displays text, as opposed to a line displaying ZV
16201 only. */
16202
16203 static int
16204 display_line (it)
16205 struct it *it;
16206 {
16207 struct glyph_row *row = it->glyph_row;
16208 Lisp_Object overlay_arrow_string;
16209
16210 /* We always start displaying at hpos zero even if hscrolled. */
16211 xassert (it->hpos == 0 && it->current_x == 0);
16212
16213 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16214 >= it->w->desired_matrix->nrows)
16215 {
16216 it->w->nrows_scale_factor++;
16217 fonts_changed_p = 1;
16218 return 0;
16219 }
16220
16221 /* Is IT->w showing the region? */
16222 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16223
16224 /* Clear the result glyph row and enable it. */
16225 prepare_desired_row (row);
16226
16227 row->y = it->current_y;
16228 row->start = it->start;
16229 row->continuation_lines_width = it->continuation_lines_width;
16230 row->displays_text_p = 1;
16231 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16232 it->starts_in_middle_of_char_p = 0;
16233
16234 /* Arrange the overlays nicely for our purposes. Usually, we call
16235 display_line on only one line at a time, in which case this
16236 can't really hurt too much, or we call it on lines which appear
16237 one after another in the buffer, in which case all calls to
16238 recenter_overlay_lists but the first will be pretty cheap. */
16239 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16240
16241 /* Move over display elements that are not visible because we are
16242 hscrolled. This may stop at an x-position < IT->first_visible_x
16243 if the first glyph is partially visible or if we hit a line end. */
16244 if (it->current_x < it->first_visible_x)
16245 {
16246 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16247 MOVE_TO_POS | MOVE_TO_X);
16248 }
16249
16250 /* Get the initial row height. This is either the height of the
16251 text hscrolled, if there is any, or zero. */
16252 row->ascent = it->max_ascent;
16253 row->height = it->max_ascent + it->max_descent;
16254 row->phys_ascent = it->max_phys_ascent;
16255 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16256 row->extra_line_spacing = it->max_extra_line_spacing;
16257
16258 /* Loop generating characters. The loop is left with IT on the next
16259 character to display. */
16260 while (1)
16261 {
16262 int n_glyphs_before, hpos_before, x_before;
16263 int x, i, nglyphs;
16264 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16265
16266 /* Retrieve the next thing to display. Value is zero if end of
16267 buffer reached. */
16268 if (!get_next_display_element (it))
16269 {
16270 /* Maybe add a space at the end of this line that is used to
16271 display the cursor there under X. Set the charpos of the
16272 first glyph of blank lines not corresponding to any text
16273 to -1. */
16274 #ifdef HAVE_WINDOW_SYSTEM
16275 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16276 row->exact_window_width_line_p = 1;
16277 else
16278 #endif /* HAVE_WINDOW_SYSTEM */
16279 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16280 || row->used[TEXT_AREA] == 0)
16281 {
16282 row->glyphs[TEXT_AREA]->charpos = -1;
16283 row->displays_text_p = 0;
16284
16285 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16286 && (!MINI_WINDOW_P (it->w)
16287 || (minibuf_level && EQ (it->window, minibuf_window))))
16288 row->indicate_empty_line_p = 1;
16289 }
16290
16291 it->continuation_lines_width = 0;
16292 row->ends_at_zv_p = 1;
16293 break;
16294 }
16295
16296 /* Now, get the metrics of what we want to display. This also
16297 generates glyphs in `row' (which is IT->glyph_row). */
16298 n_glyphs_before = row->used[TEXT_AREA];
16299 x = it->current_x;
16300
16301 /* Remember the line height so far in case the next element doesn't
16302 fit on the line. */
16303 if (!it->truncate_lines_p)
16304 {
16305 ascent = it->max_ascent;
16306 descent = it->max_descent;
16307 phys_ascent = it->max_phys_ascent;
16308 phys_descent = it->max_phys_descent;
16309 }
16310
16311 PRODUCE_GLYPHS (it);
16312
16313 /* If this display element was in marginal areas, continue with
16314 the next one. */
16315 if (it->area != TEXT_AREA)
16316 {
16317 row->ascent = max (row->ascent, it->max_ascent);
16318 row->height = max (row->height, it->max_ascent + it->max_descent);
16319 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16320 row->phys_height = max (row->phys_height,
16321 it->max_phys_ascent + it->max_phys_descent);
16322 row->extra_line_spacing = max (row->extra_line_spacing,
16323 it->max_extra_line_spacing);
16324 set_iterator_to_next (it, 1);
16325 continue;
16326 }
16327
16328 /* Does the display element fit on the line? If we truncate
16329 lines, we should draw past the right edge of the window. If
16330 we don't truncate, we want to stop so that we can display the
16331 continuation glyph before the right margin. If lines are
16332 continued, there are two possible strategies for characters
16333 resulting in more than 1 glyph (e.g. tabs): Display as many
16334 glyphs as possible in this line and leave the rest for the
16335 continuation line, or display the whole element in the next
16336 line. Original redisplay did the former, so we do it also. */
16337 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16338 hpos_before = it->hpos;
16339 x_before = x;
16340
16341 if (/* Not a newline. */
16342 nglyphs > 0
16343 /* Glyphs produced fit entirely in the line. */
16344 && it->current_x < it->last_visible_x)
16345 {
16346 it->hpos += nglyphs;
16347 row->ascent = max (row->ascent, it->max_ascent);
16348 row->height = max (row->height, it->max_ascent + it->max_descent);
16349 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16350 row->phys_height = max (row->phys_height,
16351 it->max_phys_ascent + it->max_phys_descent);
16352 row->extra_line_spacing = max (row->extra_line_spacing,
16353 it->max_extra_line_spacing);
16354 if (it->current_x - it->pixel_width < it->first_visible_x)
16355 row->x = x - it->first_visible_x;
16356 }
16357 else
16358 {
16359 int new_x;
16360 struct glyph *glyph;
16361
16362 for (i = 0; i < nglyphs; ++i, x = new_x)
16363 {
16364 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16365 new_x = x + glyph->pixel_width;
16366
16367 if (/* Lines are continued. */
16368 !it->truncate_lines_p
16369 && (/* Glyph doesn't fit on the line. */
16370 new_x > it->last_visible_x
16371 /* Or it fits exactly on a window system frame. */
16372 || (new_x == it->last_visible_x
16373 && FRAME_WINDOW_P (it->f))))
16374 {
16375 /* End of a continued line. */
16376
16377 if (it->hpos == 0
16378 || (new_x == it->last_visible_x
16379 && FRAME_WINDOW_P (it->f)))
16380 {
16381 /* Current glyph is the only one on the line or
16382 fits exactly on the line. We must continue
16383 the line because we can't draw the cursor
16384 after the glyph. */
16385 row->continued_p = 1;
16386 it->current_x = new_x;
16387 it->continuation_lines_width += new_x;
16388 ++it->hpos;
16389 if (i == nglyphs - 1)
16390 {
16391 set_iterator_to_next (it, 1);
16392 #ifdef HAVE_WINDOW_SYSTEM
16393 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16394 {
16395 if (!get_next_display_element (it))
16396 {
16397 row->exact_window_width_line_p = 1;
16398 it->continuation_lines_width = 0;
16399 row->continued_p = 0;
16400 row->ends_at_zv_p = 1;
16401 }
16402 else if (ITERATOR_AT_END_OF_LINE_P (it))
16403 {
16404 row->continued_p = 0;
16405 row->exact_window_width_line_p = 1;
16406 }
16407 }
16408 #endif /* HAVE_WINDOW_SYSTEM */
16409 }
16410 }
16411 else if (CHAR_GLYPH_PADDING_P (*glyph)
16412 && !FRAME_WINDOW_P (it->f))
16413 {
16414 /* A padding glyph that doesn't fit on this line.
16415 This means the whole character doesn't fit
16416 on the line. */
16417 row->used[TEXT_AREA] = n_glyphs_before;
16418
16419 /* Fill the rest of the row with continuation
16420 glyphs like in 20.x. */
16421 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16422 < row->glyphs[1 + TEXT_AREA])
16423 produce_special_glyphs (it, IT_CONTINUATION);
16424
16425 row->continued_p = 1;
16426 it->current_x = x_before;
16427 it->continuation_lines_width += x_before;
16428
16429 /* Restore the height to what it was before the
16430 element not fitting on the line. */
16431 it->max_ascent = ascent;
16432 it->max_descent = descent;
16433 it->max_phys_ascent = phys_ascent;
16434 it->max_phys_descent = phys_descent;
16435 }
16436 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16437 {
16438 /* A TAB that extends past the right edge of the
16439 window. This produces a single glyph on
16440 window system frames. We leave the glyph in
16441 this row and let it fill the row, but don't
16442 consume the TAB. */
16443 it->continuation_lines_width += it->last_visible_x;
16444 row->ends_in_middle_of_char_p = 1;
16445 row->continued_p = 1;
16446 glyph->pixel_width = it->last_visible_x - x;
16447 it->starts_in_middle_of_char_p = 1;
16448 }
16449 else
16450 {
16451 /* Something other than a TAB that draws past
16452 the right edge of the window. Restore
16453 positions to values before the element. */
16454 row->used[TEXT_AREA] = n_glyphs_before + i;
16455
16456 /* Display continuation glyphs. */
16457 if (!FRAME_WINDOW_P (it->f))
16458 produce_special_glyphs (it, IT_CONTINUATION);
16459 row->continued_p = 1;
16460
16461 it->current_x = x_before;
16462 it->continuation_lines_width += x;
16463 extend_face_to_end_of_line (it);
16464
16465 if (nglyphs > 1 && i > 0)
16466 {
16467 row->ends_in_middle_of_char_p = 1;
16468 it->starts_in_middle_of_char_p = 1;
16469 }
16470
16471 /* Restore the height to what it was before the
16472 element not fitting on the line. */
16473 it->max_ascent = ascent;
16474 it->max_descent = descent;
16475 it->max_phys_ascent = phys_ascent;
16476 it->max_phys_descent = phys_descent;
16477 }
16478
16479 break;
16480 }
16481 else if (new_x > it->first_visible_x)
16482 {
16483 /* Increment number of glyphs actually displayed. */
16484 ++it->hpos;
16485
16486 if (x < it->first_visible_x)
16487 /* Glyph is partially visible, i.e. row starts at
16488 negative X position. */
16489 row->x = x - it->first_visible_x;
16490 }
16491 else
16492 {
16493 /* Glyph is completely off the left margin of the
16494 window. This should not happen because of the
16495 move_it_in_display_line at the start of this
16496 function, unless the text display area of the
16497 window is empty. */
16498 xassert (it->first_visible_x <= it->last_visible_x);
16499 }
16500 }
16501
16502 row->ascent = max (row->ascent, it->max_ascent);
16503 row->height = max (row->height, it->max_ascent + it->max_descent);
16504 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16505 row->phys_height = max (row->phys_height,
16506 it->max_phys_ascent + it->max_phys_descent);
16507 row->extra_line_spacing = max (row->extra_line_spacing,
16508 it->max_extra_line_spacing);
16509
16510 /* End of this display line if row is continued. */
16511 if (row->continued_p || row->ends_at_zv_p)
16512 break;
16513 }
16514
16515 at_end_of_line:
16516 /* Is this a line end? If yes, we're also done, after making
16517 sure that a non-default face is extended up to the right
16518 margin of the window. */
16519 if (ITERATOR_AT_END_OF_LINE_P (it))
16520 {
16521 int used_before = row->used[TEXT_AREA];
16522
16523 row->ends_in_newline_from_string_p = STRINGP (it->object);
16524
16525 #ifdef HAVE_WINDOW_SYSTEM
16526 /* Add a space at the end of the line that is used to
16527 display the cursor there. */
16528 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16529 append_space_for_newline (it, 0);
16530 #endif /* HAVE_WINDOW_SYSTEM */
16531
16532 /* Extend the face to the end of the line. */
16533 extend_face_to_end_of_line (it);
16534
16535 /* Make sure we have the position. */
16536 if (used_before == 0)
16537 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16538
16539 /* Consume the line end. This skips over invisible lines. */
16540 set_iterator_to_next (it, 1);
16541 it->continuation_lines_width = 0;
16542 break;
16543 }
16544
16545 /* Proceed with next display element. Note that this skips
16546 over lines invisible because of selective display. */
16547 set_iterator_to_next (it, 1);
16548
16549 /* If we truncate lines, we are done when the last displayed
16550 glyphs reach past the right margin of the window. */
16551 if (it->truncate_lines_p
16552 && (FRAME_WINDOW_P (it->f)
16553 ? (it->current_x >= it->last_visible_x)
16554 : (it->current_x > it->last_visible_x)))
16555 {
16556 /* Maybe add truncation glyphs. */
16557 if (!FRAME_WINDOW_P (it->f))
16558 {
16559 int i, n;
16560
16561 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16562 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16563 break;
16564
16565 for (n = row->used[TEXT_AREA]; i < n; ++i)
16566 {
16567 row->used[TEXT_AREA] = i;
16568 produce_special_glyphs (it, IT_TRUNCATION);
16569 }
16570 }
16571 #ifdef HAVE_WINDOW_SYSTEM
16572 else
16573 {
16574 /* Don't truncate if we can overflow newline into fringe. */
16575 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16576 {
16577 if (!get_next_display_element (it))
16578 {
16579 it->continuation_lines_width = 0;
16580 row->ends_at_zv_p = 1;
16581 row->exact_window_width_line_p = 1;
16582 break;
16583 }
16584 if (ITERATOR_AT_END_OF_LINE_P (it))
16585 {
16586 row->exact_window_width_line_p = 1;
16587 goto at_end_of_line;
16588 }
16589 }
16590 }
16591 #endif /* HAVE_WINDOW_SYSTEM */
16592
16593 row->truncated_on_right_p = 1;
16594 it->continuation_lines_width = 0;
16595 reseat_at_next_visible_line_start (it, 0);
16596 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16597 it->hpos = hpos_before;
16598 it->current_x = x_before;
16599 break;
16600 }
16601 }
16602
16603 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16604 at the left window margin. */
16605 if (it->first_visible_x
16606 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16607 {
16608 if (!FRAME_WINDOW_P (it->f))
16609 insert_left_trunc_glyphs (it);
16610 row->truncated_on_left_p = 1;
16611 }
16612
16613 /* If the start of this line is the overlay arrow-position, then
16614 mark this glyph row as the one containing the overlay arrow.
16615 This is clearly a mess with variable size fonts. It would be
16616 better to let it be displayed like cursors under X. */
16617 if ((row->displays_text_p || !overlay_arrow_seen)
16618 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16619 !NILP (overlay_arrow_string)))
16620 {
16621 /* Overlay arrow in window redisplay is a fringe bitmap. */
16622 if (STRINGP (overlay_arrow_string))
16623 {
16624 struct glyph_row *arrow_row
16625 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16626 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16627 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16628 struct glyph *p = row->glyphs[TEXT_AREA];
16629 struct glyph *p2, *end;
16630
16631 /* Copy the arrow glyphs. */
16632 while (glyph < arrow_end)
16633 *p++ = *glyph++;
16634
16635 /* Throw away padding glyphs. */
16636 p2 = p;
16637 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16638 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16639 ++p2;
16640 if (p2 > p)
16641 {
16642 while (p2 < end)
16643 *p++ = *p2++;
16644 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16645 }
16646 }
16647 else
16648 {
16649 xassert (INTEGERP (overlay_arrow_string));
16650 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16651 }
16652 overlay_arrow_seen = 1;
16653 }
16654
16655 /* Compute pixel dimensions of this line. */
16656 compute_line_metrics (it);
16657
16658 /* Remember the position at which this line ends. */
16659 row->end = it->current;
16660
16661 /* Record whether this row ends inside an ellipsis. */
16662 row->ends_in_ellipsis_p
16663 = (it->method == GET_FROM_DISPLAY_VECTOR
16664 && it->ellipsis_p);
16665
16666 /* Save fringe bitmaps in this row. */
16667 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16668 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16669 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16670 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16671
16672 it->left_user_fringe_bitmap = 0;
16673 it->left_user_fringe_face_id = 0;
16674 it->right_user_fringe_bitmap = 0;
16675 it->right_user_fringe_face_id = 0;
16676
16677 /* Maybe set the cursor. */
16678 if (it->w->cursor.vpos < 0
16679 && PT >= MATRIX_ROW_START_CHARPOS (row)
16680 && PT <= MATRIX_ROW_END_CHARPOS (row)
16681 && cursor_row_p (it->w, row))
16682 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16683
16684 /* Highlight trailing whitespace. */
16685 if (!NILP (Vshow_trailing_whitespace))
16686 highlight_trailing_whitespace (it->f, it->glyph_row);
16687
16688 /* Prepare for the next line. This line starts horizontally at (X
16689 HPOS) = (0 0). Vertical positions are incremented. As a
16690 convenience for the caller, IT->glyph_row is set to the next
16691 row to be used. */
16692 it->current_x = it->hpos = 0;
16693 it->current_y += row->height;
16694 ++it->vpos;
16695 ++it->glyph_row;
16696 it->start = it->current;
16697 return row->displays_text_p;
16698 }
16699
16700
16701 \f
16702 /***********************************************************************
16703 Menu Bar
16704 ***********************************************************************/
16705
16706 /* Redisplay the menu bar in the frame for window W.
16707
16708 The menu bar of X frames that don't have X toolkit support is
16709 displayed in a special window W->frame->menu_bar_window.
16710
16711 The menu bar of terminal frames is treated specially as far as
16712 glyph matrices are concerned. Menu bar lines are not part of
16713 windows, so the update is done directly on the frame matrix rows
16714 for the menu bar. */
16715
16716 static void
16717 display_menu_bar (w)
16718 struct window *w;
16719 {
16720 struct frame *f = XFRAME (WINDOW_FRAME (w));
16721 struct it it;
16722 Lisp_Object items;
16723 int i;
16724
16725 /* Don't do all this for graphical frames. */
16726 #ifdef HAVE_NTGUI
16727 if (FRAME_W32_P (f))
16728 return;
16729 #endif
16730 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16731 if (FRAME_X_P (f))
16732 return;
16733 #endif
16734 #ifdef MAC_OS
16735 if (FRAME_MAC_P (f))
16736 return;
16737 #endif
16738
16739 #ifdef USE_X_TOOLKIT
16740 xassert (!FRAME_WINDOW_P (f));
16741 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16742 it.first_visible_x = 0;
16743 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16744 #else /* not USE_X_TOOLKIT */
16745 if (FRAME_WINDOW_P (f))
16746 {
16747 /* Menu bar lines are displayed in the desired matrix of the
16748 dummy window menu_bar_window. */
16749 struct window *menu_w;
16750 xassert (WINDOWP (f->menu_bar_window));
16751 menu_w = XWINDOW (f->menu_bar_window);
16752 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16753 MENU_FACE_ID);
16754 it.first_visible_x = 0;
16755 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16756 }
16757 else
16758 {
16759 /* This is a TTY frame, i.e. character hpos/vpos are used as
16760 pixel x/y. */
16761 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16762 MENU_FACE_ID);
16763 it.first_visible_x = 0;
16764 it.last_visible_x = FRAME_COLS (f);
16765 }
16766 #endif /* not USE_X_TOOLKIT */
16767
16768 if (! mode_line_inverse_video)
16769 /* Force the menu-bar to be displayed in the default face. */
16770 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16771
16772 /* Clear all rows of the menu bar. */
16773 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16774 {
16775 struct glyph_row *row = it.glyph_row + i;
16776 clear_glyph_row (row);
16777 row->enabled_p = 1;
16778 row->full_width_p = 1;
16779 }
16780
16781 /* Display all items of the menu bar. */
16782 items = FRAME_MENU_BAR_ITEMS (it.f);
16783 for (i = 0; i < XVECTOR (items)->size; i += 4)
16784 {
16785 Lisp_Object string;
16786
16787 /* Stop at nil string. */
16788 string = AREF (items, i + 1);
16789 if (NILP (string))
16790 break;
16791
16792 /* Remember where item was displayed. */
16793 ASET (items, i + 3, make_number (it.hpos));
16794
16795 /* Display the item, pad with one space. */
16796 if (it.current_x < it.last_visible_x)
16797 display_string (NULL, string, Qnil, 0, 0, &it,
16798 SCHARS (string) + 1, 0, 0, -1);
16799 }
16800
16801 /* Fill out the line with spaces. */
16802 if (it.current_x < it.last_visible_x)
16803 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16804
16805 /* Compute the total height of the lines. */
16806 compute_line_metrics (&it);
16807 }
16808
16809
16810 \f
16811 /***********************************************************************
16812 Mode Line
16813 ***********************************************************************/
16814
16815 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16816 FORCE is non-zero, redisplay mode lines unconditionally.
16817 Otherwise, redisplay only mode lines that are garbaged. Value is
16818 the number of windows whose mode lines were redisplayed. */
16819
16820 static int
16821 redisplay_mode_lines (window, force)
16822 Lisp_Object window;
16823 int force;
16824 {
16825 int nwindows = 0;
16826
16827 while (!NILP (window))
16828 {
16829 struct window *w = XWINDOW (window);
16830
16831 if (WINDOWP (w->hchild))
16832 nwindows += redisplay_mode_lines (w->hchild, force);
16833 else if (WINDOWP (w->vchild))
16834 nwindows += redisplay_mode_lines (w->vchild, force);
16835 else if (force
16836 || FRAME_GARBAGED_P (XFRAME (w->frame))
16837 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16838 {
16839 struct text_pos lpoint;
16840 struct buffer *old = current_buffer;
16841
16842 /* Set the window's buffer for the mode line display. */
16843 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16844 set_buffer_internal_1 (XBUFFER (w->buffer));
16845
16846 /* Point refers normally to the selected window. For any
16847 other window, set up appropriate value. */
16848 if (!EQ (window, selected_window))
16849 {
16850 struct text_pos pt;
16851
16852 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16853 if (CHARPOS (pt) < BEGV)
16854 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16855 else if (CHARPOS (pt) > (ZV - 1))
16856 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16857 else
16858 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16859 }
16860
16861 /* Display mode lines. */
16862 clear_glyph_matrix (w->desired_matrix);
16863 if (display_mode_lines (w))
16864 {
16865 ++nwindows;
16866 w->must_be_updated_p = 1;
16867 }
16868
16869 /* Restore old settings. */
16870 set_buffer_internal_1 (old);
16871 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16872 }
16873
16874 window = w->next;
16875 }
16876
16877 return nwindows;
16878 }
16879
16880
16881 /* Display the mode and/or top line of window W. Value is the number
16882 of mode lines displayed. */
16883
16884 static int
16885 display_mode_lines (w)
16886 struct window *w;
16887 {
16888 Lisp_Object old_selected_window, old_selected_frame;
16889 int n = 0;
16890
16891 old_selected_frame = selected_frame;
16892 selected_frame = w->frame;
16893 old_selected_window = selected_window;
16894 XSETWINDOW (selected_window, w);
16895
16896 /* These will be set while the mode line specs are processed. */
16897 line_number_displayed = 0;
16898 w->column_number_displayed = Qnil;
16899
16900 if (WINDOW_WANTS_MODELINE_P (w))
16901 {
16902 struct window *sel_w = XWINDOW (old_selected_window);
16903
16904 /* Select mode line face based on the real selected window. */
16905 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16906 current_buffer->mode_line_format);
16907 ++n;
16908 }
16909
16910 if (WINDOW_WANTS_HEADER_LINE_P (w))
16911 {
16912 display_mode_line (w, HEADER_LINE_FACE_ID,
16913 current_buffer->header_line_format);
16914 ++n;
16915 }
16916
16917 selected_frame = old_selected_frame;
16918 selected_window = old_selected_window;
16919 return n;
16920 }
16921
16922
16923 /* Display mode or top line of window W. FACE_ID specifies which line
16924 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16925 FORMAT is the mode line format to display. Value is the pixel
16926 height of the mode line displayed. */
16927
16928 static int
16929 display_mode_line (w, face_id, format)
16930 struct window *w;
16931 enum face_id face_id;
16932 Lisp_Object format;
16933 {
16934 struct it it;
16935 struct face *face;
16936 int count = SPECPDL_INDEX ();
16937
16938 init_iterator (&it, w, -1, -1, NULL, face_id);
16939 /* Don't extend on a previously drawn mode-line.
16940 This may happen if called from pos_visible_p. */
16941 it.glyph_row->enabled_p = 0;
16942 prepare_desired_row (it.glyph_row);
16943
16944 it.glyph_row->mode_line_p = 1;
16945
16946 if (! mode_line_inverse_video)
16947 /* Force the mode-line to be displayed in the default face. */
16948 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16949
16950 record_unwind_protect (unwind_format_mode_line,
16951 format_mode_line_unwind_data (NULL, Qnil, 0));
16952
16953 mode_line_target = MODE_LINE_DISPLAY;
16954
16955 /* Temporarily make frame's keyboard the current kboard so that
16956 kboard-local variables in the mode_line_format will get the right
16957 values. */
16958 push_kboard (FRAME_KBOARD (it.f));
16959 record_unwind_save_match_data ();
16960 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16961 pop_kboard ();
16962
16963 unbind_to (count, Qnil);
16964
16965 /* Fill up with spaces. */
16966 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16967
16968 compute_line_metrics (&it);
16969 it.glyph_row->full_width_p = 1;
16970 it.glyph_row->continued_p = 0;
16971 it.glyph_row->truncated_on_left_p = 0;
16972 it.glyph_row->truncated_on_right_p = 0;
16973
16974 /* Make a 3D mode-line have a shadow at its right end. */
16975 face = FACE_FROM_ID (it.f, face_id);
16976 extend_face_to_end_of_line (&it);
16977 if (face->box != FACE_NO_BOX)
16978 {
16979 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16980 + it.glyph_row->used[TEXT_AREA] - 1);
16981 last->right_box_line_p = 1;
16982 }
16983
16984 return it.glyph_row->height;
16985 }
16986
16987 /* Move element ELT in LIST to the front of LIST.
16988 Return the updated list. */
16989
16990 static Lisp_Object
16991 move_elt_to_front (elt, list)
16992 Lisp_Object elt, list;
16993 {
16994 register Lisp_Object tail, prev;
16995 register Lisp_Object tem;
16996
16997 tail = list;
16998 prev = Qnil;
16999 while (CONSP (tail))
17000 {
17001 tem = XCAR (tail);
17002
17003 if (EQ (elt, tem))
17004 {
17005 /* Splice out the link TAIL. */
17006 if (NILP (prev))
17007 list = XCDR (tail);
17008 else
17009 Fsetcdr (prev, XCDR (tail));
17010
17011 /* Now make it the first. */
17012 Fsetcdr (tail, list);
17013 return tail;
17014 }
17015 else
17016 prev = tail;
17017 tail = XCDR (tail);
17018 QUIT;
17019 }
17020
17021 /* Not found--return unchanged LIST. */
17022 return list;
17023 }
17024
17025 /* Contribute ELT to the mode line for window IT->w. How it
17026 translates into text depends on its data type.
17027
17028 IT describes the display environment in which we display, as usual.
17029
17030 DEPTH is the depth in recursion. It is used to prevent
17031 infinite recursion here.
17032
17033 FIELD_WIDTH is the number of characters the display of ELT should
17034 occupy in the mode line, and PRECISION is the maximum number of
17035 characters to display from ELT's representation. See
17036 display_string for details.
17037
17038 Returns the hpos of the end of the text generated by ELT.
17039
17040 PROPS is a property list to add to any string we encounter.
17041
17042 If RISKY is nonzero, remove (disregard) any properties in any string
17043 we encounter, and ignore :eval and :propertize.
17044
17045 The global variable `mode_line_target' determines whether the
17046 output is passed to `store_mode_line_noprop',
17047 `store_mode_line_string', or `display_string'. */
17048
17049 static int
17050 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17051 struct it *it;
17052 int depth;
17053 int field_width, precision;
17054 Lisp_Object elt, props;
17055 int risky;
17056 {
17057 int n = 0, field, prec;
17058 int literal = 0;
17059
17060 tail_recurse:
17061 if (depth > 100)
17062 elt = build_string ("*too-deep*");
17063
17064 depth++;
17065
17066 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17067 {
17068 case Lisp_String:
17069 {
17070 /* A string: output it and check for %-constructs within it. */
17071 unsigned char c;
17072 int offset = 0;
17073
17074 if (SCHARS (elt) > 0
17075 && (!NILP (props) || risky))
17076 {
17077 Lisp_Object oprops, aelt;
17078 oprops = Ftext_properties_at (make_number (0), elt);
17079
17080 /* If the starting string's properties are not what
17081 we want, translate the string. Also, if the string
17082 is risky, do that anyway. */
17083
17084 if (NILP (Fequal (props, oprops)) || risky)
17085 {
17086 /* If the starting string has properties,
17087 merge the specified ones onto the existing ones. */
17088 if (! NILP (oprops) && !risky)
17089 {
17090 Lisp_Object tem;
17091
17092 oprops = Fcopy_sequence (oprops);
17093 tem = props;
17094 while (CONSP (tem))
17095 {
17096 oprops = Fplist_put (oprops, XCAR (tem),
17097 XCAR (XCDR (tem)));
17098 tem = XCDR (XCDR (tem));
17099 }
17100 props = oprops;
17101 }
17102
17103 aelt = Fassoc (elt, mode_line_proptrans_alist);
17104 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17105 {
17106 /* AELT is what we want. Move it to the front
17107 without consing. */
17108 elt = XCAR (aelt);
17109 mode_line_proptrans_alist
17110 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17111 }
17112 else
17113 {
17114 Lisp_Object tem;
17115
17116 /* If AELT has the wrong props, it is useless.
17117 so get rid of it. */
17118 if (! NILP (aelt))
17119 mode_line_proptrans_alist
17120 = Fdelq (aelt, mode_line_proptrans_alist);
17121
17122 elt = Fcopy_sequence (elt);
17123 Fset_text_properties (make_number (0), Flength (elt),
17124 props, elt);
17125 /* Add this item to mode_line_proptrans_alist. */
17126 mode_line_proptrans_alist
17127 = Fcons (Fcons (elt, props),
17128 mode_line_proptrans_alist);
17129 /* Truncate mode_line_proptrans_alist
17130 to at most 50 elements. */
17131 tem = Fnthcdr (make_number (50),
17132 mode_line_proptrans_alist);
17133 if (! NILP (tem))
17134 XSETCDR (tem, Qnil);
17135 }
17136 }
17137 }
17138
17139 offset = 0;
17140
17141 if (literal)
17142 {
17143 prec = precision - n;
17144 switch (mode_line_target)
17145 {
17146 case MODE_LINE_NOPROP:
17147 case MODE_LINE_TITLE:
17148 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17149 break;
17150 case MODE_LINE_STRING:
17151 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17152 break;
17153 case MODE_LINE_DISPLAY:
17154 n += display_string (NULL, elt, Qnil, 0, 0, it,
17155 0, prec, 0, STRING_MULTIBYTE (elt));
17156 break;
17157 }
17158
17159 break;
17160 }
17161
17162 /* Handle the non-literal case. */
17163
17164 while ((precision <= 0 || n < precision)
17165 && SREF (elt, offset) != 0
17166 && (mode_line_target != MODE_LINE_DISPLAY
17167 || it->current_x < it->last_visible_x))
17168 {
17169 int last_offset = offset;
17170
17171 /* Advance to end of string or next format specifier. */
17172 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17173 ;
17174
17175 if (offset - 1 != last_offset)
17176 {
17177 int nchars, nbytes;
17178
17179 /* Output to end of string or up to '%'. Field width
17180 is length of string. Don't output more than
17181 PRECISION allows us. */
17182 offset--;
17183
17184 prec = c_string_width (SDATA (elt) + last_offset,
17185 offset - last_offset, precision - n,
17186 &nchars, &nbytes);
17187
17188 switch (mode_line_target)
17189 {
17190 case MODE_LINE_NOPROP:
17191 case MODE_LINE_TITLE:
17192 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17193 break;
17194 case MODE_LINE_STRING:
17195 {
17196 int bytepos = last_offset;
17197 int charpos = string_byte_to_char (elt, bytepos);
17198 int endpos = (precision <= 0
17199 ? string_byte_to_char (elt, offset)
17200 : charpos + nchars);
17201
17202 n += store_mode_line_string (NULL,
17203 Fsubstring (elt, make_number (charpos),
17204 make_number (endpos)),
17205 0, 0, 0, Qnil);
17206 }
17207 break;
17208 case MODE_LINE_DISPLAY:
17209 {
17210 int bytepos = last_offset;
17211 int charpos = string_byte_to_char (elt, bytepos);
17212
17213 if (precision <= 0)
17214 nchars = string_byte_to_char (elt, offset) - charpos;
17215 n += display_string (NULL, elt, Qnil, 0, charpos,
17216 it, 0, nchars, 0,
17217 STRING_MULTIBYTE (elt));
17218 }
17219 break;
17220 }
17221 }
17222 else /* c == '%' */
17223 {
17224 int percent_position = offset;
17225
17226 /* Get the specified minimum width. Zero means
17227 don't pad. */
17228 field = 0;
17229 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17230 field = field * 10 + c - '0';
17231
17232 /* Don't pad beyond the total padding allowed. */
17233 if (field_width - n > 0 && field > field_width - n)
17234 field = field_width - n;
17235
17236 /* Note that either PRECISION <= 0 or N < PRECISION. */
17237 prec = precision - n;
17238
17239 if (c == 'M')
17240 n += display_mode_element (it, depth, field, prec,
17241 Vglobal_mode_string, props,
17242 risky);
17243 else if (c != 0)
17244 {
17245 int multibyte;
17246 int bytepos, charpos;
17247 unsigned char *spec;
17248
17249 bytepos = percent_position;
17250 charpos = (STRING_MULTIBYTE (elt)
17251 ? string_byte_to_char (elt, bytepos)
17252 : bytepos);
17253 spec
17254 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17255
17256 switch (mode_line_target)
17257 {
17258 case MODE_LINE_NOPROP:
17259 case MODE_LINE_TITLE:
17260 n += store_mode_line_noprop (spec, field, prec);
17261 break;
17262 case MODE_LINE_STRING:
17263 {
17264 int len = strlen (spec);
17265 Lisp_Object tem = make_string (spec, len);
17266 props = Ftext_properties_at (make_number (charpos), elt);
17267 /* Should only keep face property in props */
17268 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17269 }
17270 break;
17271 case MODE_LINE_DISPLAY:
17272 {
17273 int nglyphs_before, nwritten;
17274
17275 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17276 nwritten = display_string (spec, Qnil, elt,
17277 charpos, 0, it,
17278 field, prec, 0,
17279 multibyte);
17280
17281 /* Assign to the glyphs written above the
17282 string where the `%x' came from, position
17283 of the `%'. */
17284 if (nwritten > 0)
17285 {
17286 struct glyph *glyph
17287 = (it->glyph_row->glyphs[TEXT_AREA]
17288 + nglyphs_before);
17289 int i;
17290
17291 for (i = 0; i < nwritten; ++i)
17292 {
17293 glyph[i].object = elt;
17294 glyph[i].charpos = charpos;
17295 }
17296
17297 n += nwritten;
17298 }
17299 }
17300 break;
17301 }
17302 }
17303 else /* c == 0 */
17304 break;
17305 }
17306 }
17307 }
17308 break;
17309
17310 case Lisp_Symbol:
17311 /* A symbol: process the value of the symbol recursively
17312 as if it appeared here directly. Avoid error if symbol void.
17313 Special case: if value of symbol is a string, output the string
17314 literally. */
17315 {
17316 register Lisp_Object tem;
17317
17318 /* If the variable is not marked as risky to set
17319 then its contents are risky to use. */
17320 if (NILP (Fget (elt, Qrisky_local_variable)))
17321 risky = 1;
17322
17323 tem = Fboundp (elt);
17324 if (!NILP (tem))
17325 {
17326 tem = Fsymbol_value (elt);
17327 /* If value is a string, output that string literally:
17328 don't check for % within it. */
17329 if (STRINGP (tem))
17330 literal = 1;
17331
17332 if (!EQ (tem, elt))
17333 {
17334 /* Give up right away for nil or t. */
17335 elt = tem;
17336 goto tail_recurse;
17337 }
17338 }
17339 }
17340 break;
17341
17342 case Lisp_Cons:
17343 {
17344 register Lisp_Object car, tem;
17345
17346 /* A cons cell: five distinct cases.
17347 If first element is :eval or :propertize, do something special.
17348 If first element is a string or a cons, process all the elements
17349 and effectively concatenate them.
17350 If first element is a negative number, truncate displaying cdr to
17351 at most that many characters. If positive, pad (with spaces)
17352 to at least that many characters.
17353 If first element is a symbol, process the cadr or caddr recursively
17354 according to whether the symbol's value is non-nil or nil. */
17355 car = XCAR (elt);
17356 if (EQ (car, QCeval))
17357 {
17358 /* An element of the form (:eval FORM) means evaluate FORM
17359 and use the result as mode line elements. */
17360
17361 if (risky)
17362 break;
17363
17364 if (CONSP (XCDR (elt)))
17365 {
17366 Lisp_Object spec;
17367 spec = safe_eval (XCAR (XCDR (elt)));
17368 n += display_mode_element (it, depth, field_width - n,
17369 precision - n, spec, props,
17370 risky);
17371 }
17372 }
17373 else if (EQ (car, QCpropertize))
17374 {
17375 /* An element of the form (:propertize ELT PROPS...)
17376 means display ELT but applying properties PROPS. */
17377
17378 if (risky)
17379 break;
17380
17381 if (CONSP (XCDR (elt)))
17382 n += display_mode_element (it, depth, field_width - n,
17383 precision - n, XCAR (XCDR (elt)),
17384 XCDR (XCDR (elt)), risky);
17385 }
17386 else if (SYMBOLP (car))
17387 {
17388 tem = Fboundp (car);
17389 elt = XCDR (elt);
17390 if (!CONSP (elt))
17391 goto invalid;
17392 /* elt is now the cdr, and we know it is a cons cell.
17393 Use its car if CAR has a non-nil value. */
17394 if (!NILP (tem))
17395 {
17396 tem = Fsymbol_value (car);
17397 if (!NILP (tem))
17398 {
17399 elt = XCAR (elt);
17400 goto tail_recurse;
17401 }
17402 }
17403 /* Symbol's value is nil (or symbol is unbound)
17404 Get the cddr of the original list
17405 and if possible find the caddr and use that. */
17406 elt = XCDR (elt);
17407 if (NILP (elt))
17408 break;
17409 else if (!CONSP (elt))
17410 goto invalid;
17411 elt = XCAR (elt);
17412 goto tail_recurse;
17413 }
17414 else if (INTEGERP (car))
17415 {
17416 register int lim = XINT (car);
17417 elt = XCDR (elt);
17418 if (lim < 0)
17419 {
17420 /* Negative int means reduce maximum width. */
17421 if (precision <= 0)
17422 precision = -lim;
17423 else
17424 precision = min (precision, -lim);
17425 }
17426 else if (lim > 0)
17427 {
17428 /* Padding specified. Don't let it be more than
17429 current maximum. */
17430 if (precision > 0)
17431 lim = min (precision, lim);
17432
17433 /* If that's more padding than already wanted, queue it.
17434 But don't reduce padding already specified even if
17435 that is beyond the current truncation point. */
17436 field_width = max (lim, field_width);
17437 }
17438 goto tail_recurse;
17439 }
17440 else if (STRINGP (car) || CONSP (car))
17441 {
17442 register int limit = 50;
17443 /* Limit is to protect against circular lists. */
17444 while (CONSP (elt)
17445 && --limit > 0
17446 && (precision <= 0 || n < precision))
17447 {
17448 n += display_mode_element (it, depth,
17449 /* Do padding only after the last
17450 element in the list. */
17451 (! CONSP (XCDR (elt))
17452 ? field_width - n
17453 : 0),
17454 precision - n, XCAR (elt),
17455 props, risky);
17456 elt = XCDR (elt);
17457 }
17458 }
17459 }
17460 break;
17461
17462 default:
17463 invalid:
17464 elt = build_string ("*invalid*");
17465 goto tail_recurse;
17466 }
17467
17468 /* Pad to FIELD_WIDTH. */
17469 if (field_width > 0 && n < field_width)
17470 {
17471 switch (mode_line_target)
17472 {
17473 case MODE_LINE_NOPROP:
17474 case MODE_LINE_TITLE:
17475 n += store_mode_line_noprop ("", field_width - n, 0);
17476 break;
17477 case MODE_LINE_STRING:
17478 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17479 break;
17480 case MODE_LINE_DISPLAY:
17481 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17482 0, 0, 0);
17483 break;
17484 }
17485 }
17486
17487 return n;
17488 }
17489
17490 /* Store a mode-line string element in mode_line_string_list.
17491
17492 If STRING is non-null, display that C string. Otherwise, the Lisp
17493 string LISP_STRING is displayed.
17494
17495 FIELD_WIDTH is the minimum number of output glyphs to produce.
17496 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17497 with spaces. FIELD_WIDTH <= 0 means don't pad.
17498
17499 PRECISION is the maximum number of characters to output from
17500 STRING. PRECISION <= 0 means don't truncate the string.
17501
17502 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17503 properties to the string.
17504
17505 PROPS are the properties to add to the string.
17506 The mode_line_string_face face property is always added to the string.
17507 */
17508
17509 static int
17510 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17511 char *string;
17512 Lisp_Object lisp_string;
17513 int copy_string;
17514 int field_width;
17515 int precision;
17516 Lisp_Object props;
17517 {
17518 int len;
17519 int n = 0;
17520
17521 if (string != NULL)
17522 {
17523 len = strlen (string);
17524 if (precision > 0 && len > precision)
17525 len = precision;
17526 lisp_string = make_string (string, len);
17527 if (NILP (props))
17528 props = mode_line_string_face_prop;
17529 else if (!NILP (mode_line_string_face))
17530 {
17531 Lisp_Object face = Fplist_get (props, Qface);
17532 props = Fcopy_sequence (props);
17533 if (NILP (face))
17534 face = mode_line_string_face;
17535 else
17536 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17537 props = Fplist_put (props, Qface, face);
17538 }
17539 Fadd_text_properties (make_number (0), make_number (len),
17540 props, lisp_string);
17541 }
17542 else
17543 {
17544 len = XFASTINT (Flength (lisp_string));
17545 if (precision > 0 && len > precision)
17546 {
17547 len = precision;
17548 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17549 precision = -1;
17550 }
17551 if (!NILP (mode_line_string_face))
17552 {
17553 Lisp_Object face;
17554 if (NILP (props))
17555 props = Ftext_properties_at (make_number (0), lisp_string);
17556 face = Fplist_get (props, Qface);
17557 if (NILP (face))
17558 face = mode_line_string_face;
17559 else
17560 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17561 props = Fcons (Qface, Fcons (face, Qnil));
17562 if (copy_string)
17563 lisp_string = Fcopy_sequence (lisp_string);
17564 }
17565 if (!NILP (props))
17566 Fadd_text_properties (make_number (0), make_number (len),
17567 props, lisp_string);
17568 }
17569
17570 if (len > 0)
17571 {
17572 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17573 n += len;
17574 }
17575
17576 if (field_width > len)
17577 {
17578 field_width -= len;
17579 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17580 if (!NILP (props))
17581 Fadd_text_properties (make_number (0), make_number (field_width),
17582 props, lisp_string);
17583 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17584 n += field_width;
17585 }
17586
17587 return n;
17588 }
17589
17590
17591 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17592 1, 4, 0,
17593 doc: /* Format a string out of a mode line format specification.
17594 First arg FORMAT specifies the mode line format (see `mode-line-format'
17595 for details) to use.
17596
17597 Optional second arg FACE specifies the face property to put
17598 on all characters for which no face is specified.
17599 The value t means whatever face the window's mode line currently uses
17600 \(either `mode-line' or `mode-line-inactive', depending).
17601 A value of nil means the default is no face property.
17602 If FACE is an integer, the value string has no text properties.
17603
17604 Optional third and fourth args WINDOW and BUFFER specify the window
17605 and buffer to use as the context for the formatting (defaults
17606 are the selected window and the window's buffer). */)
17607 (format, face, window, buffer)
17608 Lisp_Object format, face, window, buffer;
17609 {
17610 struct it it;
17611 int len;
17612 struct window *w;
17613 struct buffer *old_buffer = NULL;
17614 int face_id = -1;
17615 int no_props = INTEGERP (face);
17616 int count = SPECPDL_INDEX ();
17617 Lisp_Object str;
17618 int string_start = 0;
17619
17620 if (NILP (window))
17621 window = selected_window;
17622 CHECK_WINDOW (window);
17623 w = XWINDOW (window);
17624
17625 if (NILP (buffer))
17626 buffer = w->buffer;
17627 CHECK_BUFFER (buffer);
17628
17629 /* Make formatting the modeline a non-op when noninteractive, otherwise
17630 there will be problems later caused by a partially initialized frame. */
17631 if (NILP (format) || noninteractive)
17632 return empty_unibyte_string;
17633
17634 if (no_props)
17635 face = Qnil;
17636
17637 if (!NILP (face))
17638 {
17639 if (EQ (face, Qt))
17640 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17641 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17642 }
17643
17644 if (face_id < 0)
17645 face_id = DEFAULT_FACE_ID;
17646
17647 if (XBUFFER (buffer) != current_buffer)
17648 old_buffer = current_buffer;
17649
17650 /* Save things including mode_line_proptrans_alist,
17651 and set that to nil so that we don't alter the outer value. */
17652 record_unwind_protect (unwind_format_mode_line,
17653 format_mode_line_unwind_data
17654 (old_buffer, selected_window, 1));
17655 mode_line_proptrans_alist = Qnil;
17656
17657 Fselect_window (window, Qt);
17658 if (old_buffer)
17659 set_buffer_internal_1 (XBUFFER (buffer));
17660
17661 init_iterator (&it, w, -1, -1, NULL, face_id);
17662
17663 if (no_props)
17664 {
17665 mode_line_target = MODE_LINE_NOPROP;
17666 mode_line_string_face_prop = Qnil;
17667 mode_line_string_list = Qnil;
17668 string_start = MODE_LINE_NOPROP_LEN (0);
17669 }
17670 else
17671 {
17672 mode_line_target = MODE_LINE_STRING;
17673 mode_line_string_list = Qnil;
17674 mode_line_string_face = face;
17675 mode_line_string_face_prop
17676 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17677 }
17678
17679 push_kboard (FRAME_KBOARD (it.f));
17680 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17681 pop_kboard ();
17682
17683 if (no_props)
17684 {
17685 len = MODE_LINE_NOPROP_LEN (string_start);
17686 str = make_string (mode_line_noprop_buf + string_start, len);
17687 }
17688 else
17689 {
17690 mode_line_string_list = Fnreverse (mode_line_string_list);
17691 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17692 empty_unibyte_string);
17693 }
17694
17695 unbind_to (count, Qnil);
17696 return str;
17697 }
17698
17699 /* Write a null-terminated, right justified decimal representation of
17700 the positive integer D to BUF using a minimal field width WIDTH. */
17701
17702 static void
17703 pint2str (buf, width, d)
17704 register char *buf;
17705 register int width;
17706 register int d;
17707 {
17708 register char *p = buf;
17709
17710 if (d <= 0)
17711 *p++ = '0';
17712 else
17713 {
17714 while (d > 0)
17715 {
17716 *p++ = d % 10 + '0';
17717 d /= 10;
17718 }
17719 }
17720
17721 for (width -= (int) (p - buf); width > 0; --width)
17722 *p++ = ' ';
17723 *p-- = '\0';
17724 while (p > buf)
17725 {
17726 d = *buf;
17727 *buf++ = *p;
17728 *p-- = d;
17729 }
17730 }
17731
17732 /* Write a null-terminated, right justified decimal and "human
17733 readable" representation of the nonnegative integer D to BUF using
17734 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17735
17736 static const char power_letter[] =
17737 {
17738 0, /* not used */
17739 'k', /* kilo */
17740 'M', /* mega */
17741 'G', /* giga */
17742 'T', /* tera */
17743 'P', /* peta */
17744 'E', /* exa */
17745 'Z', /* zetta */
17746 'Y' /* yotta */
17747 };
17748
17749 static void
17750 pint2hrstr (buf, width, d)
17751 char *buf;
17752 int width;
17753 int d;
17754 {
17755 /* We aim to represent the nonnegative integer D as
17756 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17757 int quotient = d;
17758 int remainder = 0;
17759 /* -1 means: do not use TENTHS. */
17760 int tenths = -1;
17761 int exponent = 0;
17762
17763 /* Length of QUOTIENT.TENTHS as a string. */
17764 int length;
17765
17766 char * psuffix;
17767 char * p;
17768
17769 if (1000 <= quotient)
17770 {
17771 /* Scale to the appropriate EXPONENT. */
17772 do
17773 {
17774 remainder = quotient % 1000;
17775 quotient /= 1000;
17776 exponent++;
17777 }
17778 while (1000 <= quotient);
17779
17780 /* Round to nearest and decide whether to use TENTHS or not. */
17781 if (quotient <= 9)
17782 {
17783 tenths = remainder / 100;
17784 if (50 <= remainder % 100)
17785 {
17786 if (tenths < 9)
17787 tenths++;
17788 else
17789 {
17790 quotient++;
17791 if (quotient == 10)
17792 tenths = -1;
17793 else
17794 tenths = 0;
17795 }
17796 }
17797 }
17798 else
17799 if (500 <= remainder)
17800 {
17801 if (quotient < 999)
17802 quotient++;
17803 else
17804 {
17805 quotient = 1;
17806 exponent++;
17807 tenths = 0;
17808 }
17809 }
17810 }
17811
17812 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17813 if (tenths == -1 && quotient <= 99)
17814 if (quotient <= 9)
17815 length = 1;
17816 else
17817 length = 2;
17818 else
17819 length = 3;
17820 p = psuffix = buf + max (width, length);
17821
17822 /* Print EXPONENT. */
17823 if (exponent)
17824 *psuffix++ = power_letter[exponent];
17825 *psuffix = '\0';
17826
17827 /* Print TENTHS. */
17828 if (tenths >= 0)
17829 {
17830 *--p = '0' + tenths;
17831 *--p = '.';
17832 }
17833
17834 /* Print QUOTIENT. */
17835 do
17836 {
17837 int digit = quotient % 10;
17838 *--p = '0' + digit;
17839 }
17840 while ((quotient /= 10) != 0);
17841
17842 /* Print leading spaces. */
17843 while (buf < p)
17844 *--p = ' ';
17845 }
17846
17847 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17848 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17849 type of CODING_SYSTEM. Return updated pointer into BUF. */
17850
17851 static unsigned char invalid_eol_type[] = "(*invalid*)";
17852
17853 static char *
17854 decode_mode_spec_coding (coding_system, buf, eol_flag)
17855 Lisp_Object coding_system;
17856 register char *buf;
17857 int eol_flag;
17858 {
17859 Lisp_Object val;
17860 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17861 const unsigned char *eol_str;
17862 int eol_str_len;
17863 /* The EOL conversion we are using. */
17864 Lisp_Object eoltype;
17865
17866 val = CODING_SYSTEM_SPEC (coding_system);
17867 eoltype = Qnil;
17868
17869 if (!VECTORP (val)) /* Not yet decided. */
17870 {
17871 if (multibyte)
17872 *buf++ = '-';
17873 if (eol_flag)
17874 eoltype = eol_mnemonic_undecided;
17875 /* Don't mention EOL conversion if it isn't decided. */
17876 }
17877 else
17878 {
17879 Lisp_Object attrs;
17880 Lisp_Object eolvalue;
17881
17882 attrs = AREF (val, 0);
17883 eolvalue = AREF (val, 2);
17884
17885 if (multibyte)
17886 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17887
17888 if (eol_flag)
17889 {
17890 /* The EOL conversion that is normal on this system. */
17891
17892 if (NILP (eolvalue)) /* Not yet decided. */
17893 eoltype = eol_mnemonic_undecided;
17894 else if (VECTORP (eolvalue)) /* Not yet decided. */
17895 eoltype = eol_mnemonic_undecided;
17896 else /* eolvalue is Qunix, Qdos, or Qmac. */
17897 eoltype = (EQ (eolvalue, Qunix)
17898 ? eol_mnemonic_unix
17899 : (EQ (eolvalue, Qdos) == 1
17900 ? eol_mnemonic_dos : eol_mnemonic_mac));
17901 }
17902 }
17903
17904 if (eol_flag)
17905 {
17906 /* Mention the EOL conversion if it is not the usual one. */
17907 if (STRINGP (eoltype))
17908 {
17909 eol_str = SDATA (eoltype);
17910 eol_str_len = SBYTES (eoltype);
17911 }
17912 else if (CHARACTERP (eoltype))
17913 {
17914 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17915 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17916 eol_str = tmp;
17917 }
17918 else
17919 {
17920 eol_str = invalid_eol_type;
17921 eol_str_len = sizeof (invalid_eol_type) - 1;
17922 }
17923 bcopy (eol_str, buf, eol_str_len);
17924 buf += eol_str_len;
17925 }
17926
17927 return buf;
17928 }
17929
17930 /* Return a string for the output of a mode line %-spec for window W,
17931 generated by character C. PRECISION >= 0 means don't return a
17932 string longer than that value. FIELD_WIDTH > 0 means pad the
17933 string returned with spaces to that value. Return 1 in *MULTIBYTE
17934 if the result is multibyte text.
17935
17936 Note we operate on the current buffer for most purposes,
17937 the exception being w->base_line_pos. */
17938
17939 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17940
17941 static char *
17942 decode_mode_spec (w, c, field_width, precision, multibyte)
17943 struct window *w;
17944 register int c;
17945 int field_width, precision;
17946 int *multibyte;
17947 {
17948 Lisp_Object obj;
17949 struct frame *f = XFRAME (WINDOW_FRAME (w));
17950 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17951 struct buffer *b = current_buffer;
17952
17953 obj = Qnil;
17954 *multibyte = 0;
17955
17956 switch (c)
17957 {
17958 case '*':
17959 if (!NILP (b->read_only))
17960 return "%";
17961 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17962 return "*";
17963 return "-";
17964
17965 case '+':
17966 /* This differs from %* only for a modified read-only buffer. */
17967 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17968 return "*";
17969 if (!NILP (b->read_only))
17970 return "%";
17971 return "-";
17972
17973 case '&':
17974 /* This differs from %* in ignoring read-only-ness. */
17975 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17976 return "*";
17977 return "-";
17978
17979 case '%':
17980 return "%";
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 int i;
17999 char *p;
18000
18001 if (command_loop_level > 5)
18002 return " ...]]]";
18003 p = decode_mode_spec_buf;
18004 for (i = 0; i < command_loop_level; i++)
18005 *p++ = ']';
18006 *p = 0;
18007 return decode_mode_spec_buf;
18008 }
18009
18010 case '-':
18011 {
18012 register int i;
18013
18014 /* Let lots_of_dashes be a string of infinite length. */
18015 if (mode_line_target == MODE_LINE_NOPROP ||
18016 mode_line_target == MODE_LINE_STRING)
18017 return "--";
18018 if (field_width <= 0
18019 || field_width > sizeof (lots_of_dashes))
18020 {
18021 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18022 decode_mode_spec_buf[i] = '-';
18023 decode_mode_spec_buf[i] = '\0';
18024 return decode_mode_spec_buf;
18025 }
18026 else
18027 return lots_of_dashes;
18028 }
18029
18030 case 'b':
18031 obj = b->name;
18032 break;
18033
18034 case 'c':
18035 /* %c and %l are ignored in `frame-title-format'.
18036 (In redisplay_internal, the frame title is drawn _before_ the
18037 windows are updated, so the stuff which depends on actual
18038 window contents (such as %l) may fail to render properly, or
18039 even crash emacs.) */
18040 if (mode_line_target == MODE_LINE_TITLE)
18041 return "";
18042 else
18043 {
18044 int col = (int) current_column (); /* iftc */
18045 w->column_number_displayed = make_number (col);
18046 pint2str (decode_mode_spec_buf, field_width, col);
18047 return decode_mode_spec_buf;
18048 }
18049
18050 case 'e':
18051 #ifndef SYSTEM_MALLOC
18052 {
18053 if (NILP (Vmemory_full))
18054 return "";
18055 else
18056 return "!MEM FULL! ";
18057 }
18058 #else
18059 return "";
18060 #endif
18061
18062 case 'F':
18063 /* %F displays the frame name. */
18064 if (!NILP (f->title))
18065 return (char *) SDATA (f->title);
18066 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18067 return (char *) SDATA (f->name);
18068 return "Emacs";
18069
18070 case 'f':
18071 obj = b->filename;
18072 break;
18073
18074 case 'i':
18075 {
18076 int size = ZV - BEGV;
18077 pint2str (decode_mode_spec_buf, field_width, size);
18078 return decode_mode_spec_buf;
18079 }
18080
18081 case 'I':
18082 {
18083 int size = ZV - BEGV;
18084 pint2hrstr (decode_mode_spec_buf, field_width, size);
18085 return decode_mode_spec_buf;
18086 }
18087
18088 case 'l':
18089 {
18090 int startpos, startpos_byte, line, linepos, linepos_byte;
18091 int topline, nlines, junk, height;
18092
18093 /* %c and %l are ignored in `frame-title-format'. */
18094 if (mode_line_target == MODE_LINE_TITLE)
18095 return "";
18096
18097 startpos = XMARKER (w->start)->charpos;
18098 startpos_byte = marker_byte_position (w->start);
18099 height = WINDOW_TOTAL_LINES (w);
18100
18101 /* If we decided that this buffer isn't suitable for line numbers,
18102 don't forget that too fast. */
18103 if (EQ (w->base_line_pos, w->buffer))
18104 goto no_value;
18105 /* But do forget it, if the window shows a different buffer now. */
18106 else if (BUFFERP (w->base_line_pos))
18107 w->base_line_pos = Qnil;
18108
18109 /* If the buffer is very big, don't waste time. */
18110 if (INTEGERP (Vline_number_display_limit)
18111 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18112 {
18113 w->base_line_pos = Qnil;
18114 w->base_line_number = Qnil;
18115 goto no_value;
18116 }
18117
18118 if (INTEGERP (w->base_line_number)
18119 && INTEGERP (w->base_line_pos)
18120 && XFASTINT (w->base_line_pos) <= startpos)
18121 {
18122 line = XFASTINT (w->base_line_number);
18123 linepos = XFASTINT (w->base_line_pos);
18124 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18125 }
18126 else
18127 {
18128 line = 1;
18129 linepos = BUF_BEGV (b);
18130 linepos_byte = BUF_BEGV_BYTE (b);
18131 }
18132
18133 /* Count lines from base line to window start position. */
18134 nlines = display_count_lines (linepos, linepos_byte,
18135 startpos_byte,
18136 startpos, &junk);
18137
18138 topline = nlines + line;
18139
18140 /* Determine a new base line, if the old one is too close
18141 or too far away, or if we did not have one.
18142 "Too close" means it's plausible a scroll-down would
18143 go back past it. */
18144 if (startpos == BUF_BEGV (b))
18145 {
18146 w->base_line_number = make_number (topline);
18147 w->base_line_pos = make_number (BUF_BEGV (b));
18148 }
18149 else if (nlines < height + 25 || nlines > height * 3 + 50
18150 || linepos == BUF_BEGV (b))
18151 {
18152 int limit = BUF_BEGV (b);
18153 int limit_byte = BUF_BEGV_BYTE (b);
18154 int position;
18155 int distance = (height * 2 + 30) * line_number_display_limit_width;
18156
18157 if (startpos - distance > limit)
18158 {
18159 limit = startpos - distance;
18160 limit_byte = CHAR_TO_BYTE (limit);
18161 }
18162
18163 nlines = display_count_lines (startpos, startpos_byte,
18164 limit_byte,
18165 - (height * 2 + 30),
18166 &position);
18167 /* If we couldn't find the lines we wanted within
18168 line_number_display_limit_width chars per line,
18169 give up on line numbers for this window. */
18170 if (position == limit_byte && limit == startpos - distance)
18171 {
18172 w->base_line_pos = w->buffer;
18173 w->base_line_number = Qnil;
18174 goto no_value;
18175 }
18176
18177 w->base_line_number = make_number (topline - nlines);
18178 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18179 }
18180
18181 /* Now count lines from the start pos to point. */
18182 nlines = display_count_lines (startpos, startpos_byte,
18183 PT_BYTE, PT, &junk);
18184
18185 /* Record that we did display the line number. */
18186 line_number_displayed = 1;
18187
18188 /* Make the string to show. */
18189 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18190 return decode_mode_spec_buf;
18191 no_value:
18192 {
18193 char* p = decode_mode_spec_buf;
18194 int pad = field_width - 2;
18195 while (pad-- > 0)
18196 *p++ = ' ';
18197 *p++ = '?';
18198 *p++ = '?';
18199 *p = '\0';
18200 return decode_mode_spec_buf;
18201 }
18202 }
18203 break;
18204
18205 case 'm':
18206 obj = b->mode_name;
18207 break;
18208
18209 case 'n':
18210 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18211 return " Narrow";
18212 break;
18213
18214 case 'p':
18215 {
18216 int pos = marker_position (w->start);
18217 int total = BUF_ZV (b) - BUF_BEGV (b);
18218
18219 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18220 {
18221 if (pos <= BUF_BEGV (b))
18222 return "All";
18223 else
18224 return "Bottom";
18225 }
18226 else if (pos <= BUF_BEGV (b))
18227 return "Top";
18228 else
18229 {
18230 if (total > 1000000)
18231 /* Do it differently for a large value, to avoid overflow. */
18232 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18233 else
18234 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18235 /* We can't normally display a 3-digit number,
18236 so get us a 2-digit number that is close. */
18237 if (total == 100)
18238 total = 99;
18239 sprintf (decode_mode_spec_buf, "%2d%%", total);
18240 return decode_mode_spec_buf;
18241 }
18242 }
18243
18244 /* Display percentage of size above the bottom of the screen. */
18245 case 'P':
18246 {
18247 int toppos = marker_position (w->start);
18248 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18249 int total = BUF_ZV (b) - BUF_BEGV (b);
18250
18251 if (botpos >= BUF_ZV (b))
18252 {
18253 if (toppos <= BUF_BEGV (b))
18254 return "All";
18255 else
18256 return "Bottom";
18257 }
18258 else
18259 {
18260 if (total > 1000000)
18261 /* Do it differently for a large value, to avoid overflow. */
18262 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18263 else
18264 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18265 /* We can't normally display a 3-digit number,
18266 so get us a 2-digit number that is close. */
18267 if (total == 100)
18268 total = 99;
18269 if (toppos <= BUF_BEGV (b))
18270 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18271 else
18272 sprintf (decode_mode_spec_buf, "%2d%%", total);
18273 return decode_mode_spec_buf;
18274 }
18275 }
18276
18277 case 's':
18278 /* status of process */
18279 obj = Fget_buffer_process (Fcurrent_buffer ());
18280 if (NILP (obj))
18281 return "no process";
18282 #ifdef subprocesses
18283 obj = Fsymbol_name (Fprocess_status (obj));
18284 #endif
18285 break;
18286
18287 case '@':
18288 {
18289 Lisp_Object val;
18290 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18291 if (NILP (val))
18292 return "-";
18293 else
18294 return "@";
18295 }
18296
18297 case 't': /* indicate TEXT or BINARY */
18298 #ifdef MODE_LINE_BINARY_TEXT
18299 return MODE_LINE_BINARY_TEXT (b);
18300 #else
18301 return "T";
18302 #endif
18303
18304 case 'z':
18305 /* coding-system (not including end-of-line format) */
18306 case 'Z':
18307 /* coding-system (including end-of-line type) */
18308 {
18309 int eol_flag = (c == 'Z');
18310 char *p = decode_mode_spec_buf;
18311
18312 if (! FRAME_WINDOW_P (f))
18313 {
18314 /* No need to mention EOL here--the terminal never needs
18315 to do EOL conversion. */
18316 p = decode_mode_spec_coding (CODING_ID_NAME
18317 (FRAME_KEYBOARD_CODING (f)->id),
18318 p, 0);
18319 p = decode_mode_spec_coding (CODING_ID_NAME
18320 (FRAME_TERMINAL_CODING (f)->id),
18321 p, 0);
18322 }
18323 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18324 p, eol_flag);
18325
18326 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18327 #ifdef subprocesses
18328 obj = Fget_buffer_process (Fcurrent_buffer ());
18329 if (PROCESSP (obj))
18330 {
18331 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18332 p, eol_flag);
18333 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18334 p, eol_flag);
18335 }
18336 #endif /* subprocesses */
18337 #endif /* 0 */
18338 *p = 0;
18339 return decode_mode_spec_buf;
18340 }
18341 }
18342
18343 if (STRINGP (obj))
18344 {
18345 *multibyte = STRING_MULTIBYTE (obj);
18346 return (char *) SDATA (obj);
18347 }
18348 else
18349 return "";
18350 }
18351
18352
18353 /* Count up to COUNT lines starting from START / START_BYTE.
18354 But don't go beyond LIMIT_BYTE.
18355 Return the number of lines thus found (always nonnegative).
18356
18357 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18358
18359 static int
18360 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18361 int start, start_byte, limit_byte, count;
18362 int *byte_pos_ptr;
18363 {
18364 register unsigned char *cursor;
18365 unsigned char *base;
18366
18367 register int ceiling;
18368 register unsigned char *ceiling_addr;
18369 int orig_count = count;
18370
18371 /* If we are not in selective display mode,
18372 check only for newlines. */
18373 int selective_display = (!NILP (current_buffer->selective_display)
18374 && !INTEGERP (current_buffer->selective_display));
18375
18376 if (count > 0)
18377 {
18378 while (start_byte < limit_byte)
18379 {
18380 ceiling = BUFFER_CEILING_OF (start_byte);
18381 ceiling = min (limit_byte - 1, ceiling);
18382 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18383 base = (cursor = BYTE_POS_ADDR (start_byte));
18384 while (1)
18385 {
18386 if (selective_display)
18387 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18388 ;
18389 else
18390 while (*cursor != '\n' && ++cursor != ceiling_addr)
18391 ;
18392
18393 if (cursor != ceiling_addr)
18394 {
18395 if (--count == 0)
18396 {
18397 start_byte += cursor - base + 1;
18398 *byte_pos_ptr = start_byte;
18399 return orig_count;
18400 }
18401 else
18402 if (++cursor == ceiling_addr)
18403 break;
18404 }
18405 else
18406 break;
18407 }
18408 start_byte += cursor - base;
18409 }
18410 }
18411 else
18412 {
18413 while (start_byte > limit_byte)
18414 {
18415 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18416 ceiling = max (limit_byte, ceiling);
18417 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18418 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18419 while (1)
18420 {
18421 if (selective_display)
18422 while (--cursor != ceiling_addr
18423 && *cursor != '\n' && *cursor != 015)
18424 ;
18425 else
18426 while (--cursor != ceiling_addr && *cursor != '\n')
18427 ;
18428
18429 if (cursor != ceiling_addr)
18430 {
18431 if (++count == 0)
18432 {
18433 start_byte += cursor - base + 1;
18434 *byte_pos_ptr = start_byte;
18435 /* When scanning backwards, we should
18436 not count the newline posterior to which we stop. */
18437 return - orig_count - 1;
18438 }
18439 }
18440 else
18441 break;
18442 }
18443 /* Here we add 1 to compensate for the last decrement
18444 of CURSOR, which took it past the valid range. */
18445 start_byte += cursor - base + 1;
18446 }
18447 }
18448
18449 *byte_pos_ptr = limit_byte;
18450
18451 if (count < 0)
18452 return - orig_count + count;
18453 return orig_count - count;
18454
18455 }
18456
18457
18458 \f
18459 /***********************************************************************
18460 Displaying strings
18461 ***********************************************************************/
18462
18463 /* Display a NUL-terminated string, starting with index START.
18464
18465 If STRING is non-null, display that C string. Otherwise, the Lisp
18466 string LISP_STRING is displayed.
18467
18468 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18469 FACE_STRING. Display STRING or LISP_STRING with the face at
18470 FACE_STRING_POS in FACE_STRING:
18471
18472 Display the string in the environment given by IT, but use the
18473 standard display table, temporarily.
18474
18475 FIELD_WIDTH is the minimum number of output glyphs to produce.
18476 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18477 with spaces. If STRING has more characters, more than FIELD_WIDTH
18478 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18479
18480 PRECISION is the maximum number of characters to output from
18481 STRING. PRECISION < 0 means don't truncate the string.
18482
18483 This is roughly equivalent to printf format specifiers:
18484
18485 FIELD_WIDTH PRECISION PRINTF
18486 ----------------------------------------
18487 -1 -1 %s
18488 -1 10 %.10s
18489 10 -1 %10s
18490 20 10 %20.10s
18491
18492 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18493 display them, and < 0 means obey the current buffer's value of
18494 enable_multibyte_characters.
18495
18496 Value is the number of columns displayed. */
18497
18498 static int
18499 display_string (string, lisp_string, face_string, face_string_pos,
18500 start, it, field_width, precision, max_x, multibyte)
18501 unsigned char *string;
18502 Lisp_Object lisp_string;
18503 Lisp_Object face_string;
18504 EMACS_INT face_string_pos;
18505 EMACS_INT start;
18506 struct it *it;
18507 int field_width, precision, max_x;
18508 int multibyte;
18509 {
18510 int hpos_at_start = it->hpos;
18511 int saved_face_id = it->face_id;
18512 struct glyph_row *row = it->glyph_row;
18513
18514 /* Initialize the iterator IT for iteration over STRING beginning
18515 with index START. */
18516 reseat_to_string (it, string, lisp_string, start,
18517 precision, field_width, multibyte);
18518
18519 /* If displaying STRING, set up the face of the iterator
18520 from LISP_STRING, if that's given. */
18521 if (STRINGP (face_string))
18522 {
18523 EMACS_INT endptr;
18524 struct face *face;
18525
18526 it->face_id
18527 = face_at_string_position (it->w, face_string, face_string_pos,
18528 0, it->region_beg_charpos,
18529 it->region_end_charpos,
18530 &endptr, it->base_face_id, 0);
18531 face = FACE_FROM_ID (it->f, it->face_id);
18532 it->face_box_p = face->box != FACE_NO_BOX;
18533 }
18534
18535 /* Set max_x to the maximum allowed X position. Don't let it go
18536 beyond the right edge of the window. */
18537 if (max_x <= 0)
18538 max_x = it->last_visible_x;
18539 else
18540 max_x = min (max_x, it->last_visible_x);
18541
18542 /* Skip over display elements that are not visible. because IT->w is
18543 hscrolled. */
18544 if (it->current_x < it->first_visible_x)
18545 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18546 MOVE_TO_POS | MOVE_TO_X);
18547
18548 row->ascent = it->max_ascent;
18549 row->height = it->max_ascent + it->max_descent;
18550 row->phys_ascent = it->max_phys_ascent;
18551 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18552 row->extra_line_spacing = it->max_extra_line_spacing;
18553
18554 /* This condition is for the case that we are called with current_x
18555 past last_visible_x. */
18556 while (it->current_x < max_x)
18557 {
18558 int x_before, x, n_glyphs_before, i, nglyphs;
18559
18560 /* Get the next display element. */
18561 if (!get_next_display_element (it))
18562 break;
18563
18564 /* Produce glyphs. */
18565 x_before = it->current_x;
18566 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18567 PRODUCE_GLYPHS (it);
18568
18569 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18570 i = 0;
18571 x = x_before;
18572 while (i < nglyphs)
18573 {
18574 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18575
18576 if (!it->truncate_lines_p
18577 && x + glyph->pixel_width > max_x)
18578 {
18579 /* End of continued line or max_x reached. */
18580 if (CHAR_GLYPH_PADDING_P (*glyph))
18581 {
18582 /* A wide character is unbreakable. */
18583 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18584 it->current_x = x_before;
18585 }
18586 else
18587 {
18588 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18589 it->current_x = x;
18590 }
18591 break;
18592 }
18593 else if (x + glyph->pixel_width >= it->first_visible_x)
18594 {
18595 /* Glyph is at least partially visible. */
18596 ++it->hpos;
18597 if (x < it->first_visible_x)
18598 it->glyph_row->x = x - it->first_visible_x;
18599 }
18600 else
18601 {
18602 /* Glyph is off the left margin of the display area.
18603 Should not happen. */
18604 abort ();
18605 }
18606
18607 row->ascent = max (row->ascent, it->max_ascent);
18608 row->height = max (row->height, it->max_ascent + it->max_descent);
18609 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18610 row->phys_height = max (row->phys_height,
18611 it->max_phys_ascent + it->max_phys_descent);
18612 row->extra_line_spacing = max (row->extra_line_spacing,
18613 it->max_extra_line_spacing);
18614 x += glyph->pixel_width;
18615 ++i;
18616 }
18617
18618 /* Stop if max_x reached. */
18619 if (i < nglyphs)
18620 break;
18621
18622 /* Stop at line ends. */
18623 if (ITERATOR_AT_END_OF_LINE_P (it))
18624 {
18625 it->continuation_lines_width = 0;
18626 break;
18627 }
18628
18629 set_iterator_to_next (it, 1);
18630
18631 /* Stop if truncating at the right edge. */
18632 if (it->truncate_lines_p
18633 && it->current_x >= it->last_visible_x)
18634 {
18635 /* Add truncation mark, but don't do it if the line is
18636 truncated at a padding space. */
18637 if (IT_CHARPOS (*it) < it->string_nchars)
18638 {
18639 if (!FRAME_WINDOW_P (it->f))
18640 {
18641 int i, n;
18642
18643 if (it->current_x > it->last_visible_x)
18644 {
18645 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18646 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18647 break;
18648 for (n = row->used[TEXT_AREA]; i < n; ++i)
18649 {
18650 row->used[TEXT_AREA] = i;
18651 produce_special_glyphs (it, IT_TRUNCATION);
18652 }
18653 }
18654 produce_special_glyphs (it, IT_TRUNCATION);
18655 }
18656 it->glyph_row->truncated_on_right_p = 1;
18657 }
18658 break;
18659 }
18660 }
18661
18662 /* Maybe insert a truncation at the left. */
18663 if (it->first_visible_x
18664 && IT_CHARPOS (*it) > 0)
18665 {
18666 if (!FRAME_WINDOW_P (it->f))
18667 insert_left_trunc_glyphs (it);
18668 it->glyph_row->truncated_on_left_p = 1;
18669 }
18670
18671 it->face_id = saved_face_id;
18672
18673 /* Value is number of columns displayed. */
18674 return it->hpos - hpos_at_start;
18675 }
18676
18677
18678 \f
18679 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18680 appears as an element of LIST or as the car of an element of LIST.
18681 If PROPVAL is a list, compare each element against LIST in that
18682 way, and return 1/2 if any element of PROPVAL is found in LIST.
18683 Otherwise return 0. This function cannot quit.
18684 The return value is 2 if the text is invisible but with an ellipsis
18685 and 1 if it's invisible and without an ellipsis. */
18686
18687 int
18688 invisible_p (propval, list)
18689 register Lisp_Object propval;
18690 Lisp_Object list;
18691 {
18692 register Lisp_Object tail, proptail;
18693
18694 for (tail = list; CONSP (tail); tail = XCDR (tail))
18695 {
18696 register Lisp_Object tem;
18697 tem = XCAR (tail);
18698 if (EQ (propval, tem))
18699 return 1;
18700 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18701 return NILP (XCDR (tem)) ? 1 : 2;
18702 }
18703
18704 if (CONSP (propval))
18705 {
18706 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18707 {
18708 Lisp_Object propelt;
18709 propelt = XCAR (proptail);
18710 for (tail = list; CONSP (tail); tail = XCDR (tail))
18711 {
18712 register Lisp_Object tem;
18713 tem = XCAR (tail);
18714 if (EQ (propelt, tem))
18715 return 1;
18716 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18717 return NILP (XCDR (tem)) ? 1 : 2;
18718 }
18719 }
18720 }
18721
18722 return 0;
18723 }
18724
18725 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18726 doc: /* Non-nil if the property makes the text invisible.
18727 POS-OR-PROP can be a marker or number, in which case it is taken to be
18728 a position in the current buffer and the value of the `invisible' property
18729 is checked; or it can be some other value, which is then presumed to be the
18730 value of the `invisible' property of the text of interest.
18731 The non-nil value returned can be t for truly invisible text or something
18732 else if the text is replaced by an ellipsis. */)
18733 (pos_or_prop)
18734 Lisp_Object pos_or_prop;
18735 {
18736 Lisp_Object prop
18737 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18738 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18739 : pos_or_prop);
18740 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18741 return (invis == 0 ? Qnil
18742 : invis == 1 ? Qt
18743 : make_number (invis));
18744 }
18745
18746 /* Calculate a width or height in pixels from a specification using
18747 the following elements:
18748
18749 SPEC ::=
18750 NUM - a (fractional) multiple of the default font width/height
18751 (NUM) - specifies exactly NUM pixels
18752 UNIT - a fixed number of pixels, see below.
18753 ELEMENT - size of a display element in pixels, see below.
18754 (NUM . SPEC) - equals NUM * SPEC
18755 (+ SPEC SPEC ...) - add pixel values
18756 (- SPEC SPEC ...) - subtract pixel values
18757 (- SPEC) - negate pixel value
18758
18759 NUM ::=
18760 INT or FLOAT - a number constant
18761 SYMBOL - use symbol's (buffer local) variable binding.
18762
18763 UNIT ::=
18764 in - pixels per inch *)
18765 mm - pixels per 1/1000 meter *)
18766 cm - pixels per 1/100 meter *)
18767 width - width of current font in pixels.
18768 height - height of current font in pixels.
18769
18770 *) using the ratio(s) defined in display-pixels-per-inch.
18771
18772 ELEMENT ::=
18773
18774 left-fringe - left fringe width in pixels
18775 right-fringe - right fringe width in pixels
18776
18777 left-margin - left margin width in pixels
18778 right-margin - right margin width in pixels
18779
18780 scroll-bar - scroll-bar area width in pixels
18781
18782 Examples:
18783
18784 Pixels corresponding to 5 inches:
18785 (5 . in)
18786
18787 Total width of non-text areas on left side of window (if scroll-bar is on left):
18788 '(space :width (+ left-fringe left-margin scroll-bar))
18789
18790 Align to first text column (in header line):
18791 '(space :align-to 0)
18792
18793 Align to middle of text area minus half the width of variable `my-image'
18794 containing a loaded image:
18795 '(space :align-to (0.5 . (- text my-image)))
18796
18797 Width of left margin minus width of 1 character in the default font:
18798 '(space :width (- left-margin 1))
18799
18800 Width of left margin minus width of 2 characters in the current font:
18801 '(space :width (- left-margin (2 . width)))
18802
18803 Center 1 character over left-margin (in header line):
18804 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18805
18806 Different ways to express width of left fringe plus left margin minus one pixel:
18807 '(space :width (- (+ left-fringe left-margin) (1)))
18808 '(space :width (+ left-fringe left-margin (- (1))))
18809 '(space :width (+ left-fringe left-margin (-1)))
18810
18811 */
18812
18813 #define NUMVAL(X) \
18814 ((INTEGERP (X) || FLOATP (X)) \
18815 ? XFLOATINT (X) \
18816 : - 1)
18817
18818 int
18819 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18820 double *res;
18821 struct it *it;
18822 Lisp_Object prop;
18823 struct font *font;
18824 int width_p, *align_to;
18825 {
18826 double pixels;
18827
18828 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18829 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18830
18831 if (NILP (prop))
18832 return OK_PIXELS (0);
18833
18834 xassert (FRAME_LIVE_P (it->f));
18835
18836 if (SYMBOLP (prop))
18837 {
18838 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18839 {
18840 char *unit = SDATA (SYMBOL_NAME (prop));
18841
18842 if (unit[0] == 'i' && unit[1] == 'n')
18843 pixels = 1.0;
18844 else if (unit[0] == 'm' && unit[1] == 'm')
18845 pixels = 25.4;
18846 else if (unit[0] == 'c' && unit[1] == 'm')
18847 pixels = 2.54;
18848 else
18849 pixels = 0;
18850 if (pixels > 0)
18851 {
18852 double ppi;
18853 #ifdef HAVE_WINDOW_SYSTEM
18854 if (FRAME_WINDOW_P (it->f)
18855 && (ppi = (width_p
18856 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18857 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18858 ppi > 0))
18859 return OK_PIXELS (ppi / pixels);
18860 #endif
18861
18862 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18863 || (CONSP (Vdisplay_pixels_per_inch)
18864 && (ppi = (width_p
18865 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18866 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18867 ppi > 0)))
18868 return OK_PIXELS (ppi / pixels);
18869
18870 return 0;
18871 }
18872 }
18873
18874 #ifdef HAVE_WINDOW_SYSTEM
18875 if (EQ (prop, Qheight))
18876 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18877 if (EQ (prop, Qwidth))
18878 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18879 #else
18880 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18881 return OK_PIXELS (1);
18882 #endif
18883
18884 if (EQ (prop, Qtext))
18885 return OK_PIXELS (width_p
18886 ? window_box_width (it->w, TEXT_AREA)
18887 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18888
18889 if (align_to && *align_to < 0)
18890 {
18891 *res = 0;
18892 if (EQ (prop, Qleft))
18893 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18894 if (EQ (prop, Qright))
18895 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18896 if (EQ (prop, Qcenter))
18897 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18898 + window_box_width (it->w, TEXT_AREA) / 2);
18899 if (EQ (prop, Qleft_fringe))
18900 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18901 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18902 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18903 if (EQ (prop, Qright_fringe))
18904 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18905 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18906 : window_box_right_offset (it->w, TEXT_AREA));
18907 if (EQ (prop, Qleft_margin))
18908 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18909 if (EQ (prop, Qright_margin))
18910 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18911 if (EQ (prop, Qscroll_bar))
18912 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18913 ? 0
18914 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18915 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18916 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18917 : 0)));
18918 }
18919 else
18920 {
18921 if (EQ (prop, Qleft_fringe))
18922 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18923 if (EQ (prop, Qright_fringe))
18924 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18925 if (EQ (prop, Qleft_margin))
18926 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18927 if (EQ (prop, Qright_margin))
18928 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18929 if (EQ (prop, Qscroll_bar))
18930 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18931 }
18932
18933 prop = Fbuffer_local_value (prop, it->w->buffer);
18934 }
18935
18936 if (INTEGERP (prop) || FLOATP (prop))
18937 {
18938 int base_unit = (width_p
18939 ? FRAME_COLUMN_WIDTH (it->f)
18940 : FRAME_LINE_HEIGHT (it->f));
18941 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18942 }
18943
18944 if (CONSP (prop))
18945 {
18946 Lisp_Object car = XCAR (prop);
18947 Lisp_Object cdr = XCDR (prop);
18948
18949 if (SYMBOLP (car))
18950 {
18951 #ifdef HAVE_WINDOW_SYSTEM
18952 if (FRAME_WINDOW_P (it->f)
18953 && valid_image_p (prop))
18954 {
18955 int id = lookup_image (it->f, prop);
18956 struct image *img = IMAGE_FROM_ID (it->f, id);
18957
18958 return OK_PIXELS (width_p ? img->width : img->height);
18959 }
18960 #endif
18961 if (EQ (car, Qplus) || EQ (car, Qminus))
18962 {
18963 int first = 1;
18964 double px;
18965
18966 pixels = 0;
18967 while (CONSP (cdr))
18968 {
18969 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18970 font, width_p, align_to))
18971 return 0;
18972 if (first)
18973 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18974 else
18975 pixels += px;
18976 cdr = XCDR (cdr);
18977 }
18978 if (EQ (car, Qminus))
18979 pixels = -pixels;
18980 return OK_PIXELS (pixels);
18981 }
18982
18983 car = Fbuffer_local_value (car, it->w->buffer);
18984 }
18985
18986 if (INTEGERP (car) || FLOATP (car))
18987 {
18988 double fact;
18989 pixels = XFLOATINT (car);
18990 if (NILP (cdr))
18991 return OK_PIXELS (pixels);
18992 if (calc_pixel_width_or_height (&fact, it, cdr,
18993 font, width_p, align_to))
18994 return OK_PIXELS (pixels * fact);
18995 return 0;
18996 }
18997
18998 return 0;
18999 }
19000
19001 return 0;
19002 }
19003
19004 \f
19005 /***********************************************************************
19006 Glyph Display
19007 ***********************************************************************/
19008
19009 #ifdef HAVE_WINDOW_SYSTEM
19010
19011 #if GLYPH_DEBUG
19012
19013 void
19014 dump_glyph_string (s)
19015 struct glyph_string *s;
19016 {
19017 fprintf (stderr, "glyph string\n");
19018 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19019 s->x, s->y, s->width, s->height);
19020 fprintf (stderr, " ybase = %d\n", s->ybase);
19021 fprintf (stderr, " hl = %d\n", s->hl);
19022 fprintf (stderr, " left overhang = %d, right = %d\n",
19023 s->left_overhang, s->right_overhang);
19024 fprintf (stderr, " nchars = %d\n", s->nchars);
19025 fprintf (stderr, " extends to end of line = %d\n",
19026 s->extends_to_end_of_line_p);
19027 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19028 fprintf (stderr, " bg width = %d\n", s->background_width);
19029 }
19030
19031 #endif /* GLYPH_DEBUG */
19032
19033 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19034 of XChar2b structures for S; it can't be allocated in
19035 init_glyph_string because it must be allocated via `alloca'. W
19036 is the window on which S is drawn. ROW and AREA are the glyph row
19037 and area within the row from which S is constructed. START is the
19038 index of the first glyph structure covered by S. HL is a
19039 face-override for drawing S. */
19040
19041 #ifdef HAVE_NTGUI
19042 #define OPTIONAL_HDC(hdc) hdc,
19043 #define DECLARE_HDC(hdc) HDC hdc;
19044 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19045 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19046 #endif
19047
19048 #ifndef OPTIONAL_HDC
19049 #define OPTIONAL_HDC(hdc)
19050 #define DECLARE_HDC(hdc)
19051 #define ALLOCATE_HDC(hdc, f)
19052 #define RELEASE_HDC(hdc, f)
19053 #endif
19054
19055 static void
19056 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19057 struct glyph_string *s;
19058 DECLARE_HDC (hdc)
19059 XChar2b *char2b;
19060 struct window *w;
19061 struct glyph_row *row;
19062 enum glyph_row_area area;
19063 int start;
19064 enum draw_glyphs_face hl;
19065 {
19066 bzero (s, sizeof *s);
19067 s->w = w;
19068 s->f = XFRAME (w->frame);
19069 #ifdef HAVE_NTGUI
19070 s->hdc = hdc;
19071 #endif
19072 s->display = FRAME_X_DISPLAY (s->f);
19073 s->window = FRAME_X_WINDOW (s->f);
19074 s->char2b = char2b;
19075 s->hl = hl;
19076 s->row = row;
19077 s->area = area;
19078 s->first_glyph = row->glyphs[area] + start;
19079 s->height = row->height;
19080 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19081
19082 /* Display the internal border below the tool-bar window. */
19083 if (WINDOWP (s->f->tool_bar_window)
19084 && s->w == XWINDOW (s->f->tool_bar_window))
19085 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19086
19087 s->ybase = s->y + row->ascent;
19088 }
19089
19090
19091 /* Append the list of glyph strings with head H and tail T to the list
19092 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19093
19094 static INLINE void
19095 append_glyph_string_lists (head, tail, h, t)
19096 struct glyph_string **head, **tail;
19097 struct glyph_string *h, *t;
19098 {
19099 if (h)
19100 {
19101 if (*head)
19102 (*tail)->next = h;
19103 else
19104 *head = h;
19105 h->prev = *tail;
19106 *tail = t;
19107 }
19108 }
19109
19110
19111 /* Prepend the list of glyph strings with head H and tail T to the
19112 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19113 result. */
19114
19115 static INLINE void
19116 prepend_glyph_string_lists (head, tail, h, t)
19117 struct glyph_string **head, **tail;
19118 struct glyph_string *h, *t;
19119 {
19120 if (h)
19121 {
19122 if (*head)
19123 (*head)->prev = t;
19124 else
19125 *tail = t;
19126 t->next = *head;
19127 *head = h;
19128 }
19129 }
19130
19131
19132 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19133 Set *HEAD and *TAIL to the resulting list. */
19134
19135 static INLINE void
19136 append_glyph_string (head, tail, s)
19137 struct glyph_string **head, **tail;
19138 struct glyph_string *s;
19139 {
19140 s->next = s->prev = NULL;
19141 append_glyph_string_lists (head, tail, s, s);
19142 }
19143
19144
19145 /* Get face and two-byte form of character C in face FACE_ID on frame
19146 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19147 means we want to display multibyte text. DISPLAY_P non-zero means
19148 make sure that X resources for the face returned are allocated.
19149 Value is a pointer to a realized face that is ready for display if
19150 DISPLAY_P is non-zero. */
19151
19152 static INLINE struct face *
19153 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19154 struct frame *f;
19155 int c, face_id;
19156 XChar2b *char2b;
19157 int multibyte_p, display_p;
19158 {
19159 struct face *face = FACE_FROM_ID (f, face_id);
19160
19161 if (face->font)
19162 {
19163 unsigned code = face->font->driver->encode_char (face->font, c);
19164
19165 if (code != FONT_INVALID_CODE)
19166 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19167 else
19168 STORE_XCHAR2B (char2b, 0, 0);
19169 }
19170
19171 /* Make sure X resources of the face are allocated. */
19172 #ifdef HAVE_X_WINDOWS
19173 if (display_p)
19174 #endif
19175 {
19176 xassert (face != NULL);
19177 PREPARE_FACE_FOR_DISPLAY (f, face);
19178 }
19179
19180 return face;
19181 }
19182
19183
19184 /* Get face and two-byte form of character glyph GLYPH on frame F.
19185 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19186 a pointer to a realized face that is ready for display. */
19187
19188 static INLINE struct face *
19189 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19190 struct frame *f;
19191 struct glyph *glyph;
19192 XChar2b *char2b;
19193 int *two_byte_p;
19194 {
19195 struct face *face;
19196
19197 xassert (glyph->type == CHAR_GLYPH);
19198 face = FACE_FROM_ID (f, glyph->face_id);
19199
19200 if (two_byte_p)
19201 *two_byte_p = 0;
19202
19203 if (face->font)
19204 {
19205 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19206
19207 if (code != FONT_INVALID_CODE)
19208 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19209 else
19210 STORE_XCHAR2B (char2b, 0, code);
19211 }
19212
19213 /* Make sure X resources of the face are allocated. */
19214 xassert (face != NULL);
19215 PREPARE_FACE_FOR_DISPLAY (f, face);
19216 return face;
19217 }
19218
19219
19220 /* Fill glyph string S with composition components specified by S->cmp.
19221
19222 BASE_FACE is the base face of the composition.
19223 S->gidx is the index of the first component for S.
19224
19225 OVERLAPS non-zero means S should draw the foreground only, and use
19226 its physical height for clipping. See also draw_glyphs.
19227
19228 Value is the index of a component not in S. */
19229
19230 static int
19231 fill_composite_glyph_string (s, base_face, overlaps)
19232 struct glyph_string *s;
19233 struct face *base_face;
19234 int overlaps;
19235 {
19236 int i;
19237
19238 xassert (s);
19239
19240 s->for_overlaps = overlaps;
19241
19242 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19243 {
19244 Lisp_Object gstring
19245 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19246 s->cmp->hash_index * 2);
19247
19248 s->face = base_face;
19249 s->font = base_face->font;
19250 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19251 {
19252 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19253 unsigned code;
19254 XChar2b * store_pos;
19255 if (NILP (g))
19256 break;
19257 code = LGLYPH_CODE (g);
19258 store_pos = s->char2b + i;
19259 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19260 }
19261 s->width = s->cmp->pixel_width;
19262 }
19263 else
19264 {
19265 /* For all glyphs of this composition, starting at the offset
19266 S->gidx, until we reach the end of the definition or encounter a
19267 glyph that requires the different face, add it to S. */
19268 struct face *face;
19269
19270 s->face = NULL;
19271 s->font = NULL;
19272 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19273 {
19274 int c = COMPOSITION_GLYPH (s->cmp, i);
19275
19276 if (c != '\t')
19277 {
19278 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19279 -1, Qnil);
19280
19281 face = get_char_face_and_encoding (s->f, c, face_id,
19282 s->char2b + i, 1, 1);
19283 if (face)
19284 {
19285 if (! s->face)
19286 {
19287 s->face = face;
19288 s->font = s->face->font;
19289 }
19290 else if (s->face != face)
19291 break;
19292 }
19293 }
19294 ++s->nchars;
19295 }
19296
19297 /* All glyph strings for the same composition has the same width,
19298 i.e. the width set for the first component of the composition. */
19299 s->width = s->first_glyph->pixel_width;
19300 }
19301
19302 /* If the specified font could not be loaded, use the frame's
19303 default font, but record the fact that we couldn't load it in
19304 the glyph string so that we can draw rectangles for the
19305 characters of the glyph string. */
19306 if (s->font == NULL)
19307 {
19308 s->font_not_found_p = 1;
19309 s->font = FRAME_FONT (s->f);
19310 }
19311
19312 /* Adjust base line for subscript/superscript text. */
19313 s->ybase += s->first_glyph->voffset;
19314
19315 /* This glyph string must always be drawn with 16-bit functions. */
19316 s->two_byte_p = 1;
19317
19318 return s->gidx + s->nchars;
19319 }
19320
19321
19322 /* Fill glyph string S from a sequence of character glyphs.
19323
19324 FACE_ID is the face id of the string. START is the index of the
19325 first glyph to consider, END is the index of the last + 1.
19326 OVERLAPS non-zero means S should draw the foreground only, and use
19327 its physical height for clipping. See also draw_glyphs.
19328
19329 Value is the index of the first glyph not in S. */
19330
19331 static int
19332 fill_glyph_string (s, face_id, start, end, overlaps)
19333 struct glyph_string *s;
19334 int face_id;
19335 int start, end, overlaps;
19336 {
19337 struct glyph *glyph, *last;
19338 int voffset;
19339 int glyph_not_available_p;
19340
19341 xassert (s->f == XFRAME (s->w->frame));
19342 xassert (s->nchars == 0);
19343 xassert (start >= 0 && end > start);
19344
19345 s->for_overlaps = overlaps,
19346 glyph = s->row->glyphs[s->area] + start;
19347 last = s->row->glyphs[s->area] + end;
19348 voffset = glyph->voffset;
19349 s->padding_p = glyph->padding_p;
19350 glyph_not_available_p = glyph->glyph_not_available_p;
19351
19352 while (glyph < last
19353 && glyph->type == CHAR_GLYPH
19354 && glyph->voffset == voffset
19355 /* Same face id implies same font, nowadays. */
19356 && glyph->face_id == face_id
19357 && glyph->glyph_not_available_p == glyph_not_available_p)
19358 {
19359 int two_byte_p;
19360
19361 s->face = get_glyph_face_and_encoding (s->f, glyph,
19362 s->char2b + s->nchars,
19363 &two_byte_p);
19364 s->two_byte_p = two_byte_p;
19365 ++s->nchars;
19366 xassert (s->nchars <= end - start);
19367 s->width += glyph->pixel_width;
19368 if (glyph++->padding_p != s->padding_p)
19369 break;
19370 }
19371
19372 s->font = s->face->font;
19373
19374 /* If the specified font could not be loaded, use the frame's font,
19375 but record the fact that we couldn't load it in
19376 S->font_not_found_p so that we can draw rectangles for the
19377 characters of the glyph string. */
19378 if (s->font == NULL || glyph_not_available_p)
19379 {
19380 s->font_not_found_p = 1;
19381 s->font = FRAME_FONT (s->f);
19382 }
19383
19384 /* Adjust base line for subscript/superscript text. */
19385 s->ybase += voffset;
19386
19387 xassert (s->face && s->face->gc);
19388 return glyph - s->row->glyphs[s->area];
19389 }
19390
19391
19392 /* Fill glyph string S from image glyph S->first_glyph. */
19393
19394 static void
19395 fill_image_glyph_string (s)
19396 struct glyph_string *s;
19397 {
19398 xassert (s->first_glyph->type == IMAGE_GLYPH);
19399 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19400 xassert (s->img);
19401 s->slice = s->first_glyph->slice;
19402 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19403 s->font = s->face->font;
19404 s->width = s->first_glyph->pixel_width;
19405
19406 /* Adjust base line for subscript/superscript text. */
19407 s->ybase += s->first_glyph->voffset;
19408 }
19409
19410
19411 /* Fill glyph string S from a sequence of stretch glyphs.
19412
19413 ROW is the glyph row in which the glyphs are found, AREA is the
19414 area within the row. START is the index of the first glyph to
19415 consider, END is the index of the last + 1.
19416
19417 Value is the index of the first glyph not in S. */
19418
19419 static int
19420 fill_stretch_glyph_string (s, row, area, start, end)
19421 struct glyph_string *s;
19422 struct glyph_row *row;
19423 enum glyph_row_area area;
19424 int start, end;
19425 {
19426 struct glyph *glyph, *last;
19427 int voffset, face_id;
19428
19429 xassert (s->first_glyph->type == STRETCH_GLYPH);
19430
19431 glyph = s->row->glyphs[s->area] + start;
19432 last = s->row->glyphs[s->area] + end;
19433 face_id = glyph->face_id;
19434 s->face = FACE_FROM_ID (s->f, face_id);
19435 s->font = s->face->font;
19436 s->width = glyph->pixel_width;
19437 s->nchars = 1;
19438 voffset = glyph->voffset;
19439
19440 for (++glyph;
19441 (glyph < last
19442 && glyph->type == STRETCH_GLYPH
19443 && glyph->voffset == voffset
19444 && glyph->face_id == face_id);
19445 ++glyph)
19446 s->width += glyph->pixel_width;
19447
19448 /* Adjust base line for subscript/superscript text. */
19449 s->ybase += voffset;
19450
19451 /* The case that face->gc == 0 is handled when drawing the glyph
19452 string by calling PREPARE_FACE_FOR_DISPLAY. */
19453 xassert (s->face);
19454 return glyph - s->row->glyphs[s->area];
19455 }
19456
19457 static struct font_metrics *
19458 get_per_char_metric (f, font, char2b)
19459 struct frame *f;
19460 struct font *font;
19461 XChar2b *char2b;
19462 {
19463 static struct font_metrics metrics;
19464 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19465 struct font *fontp;
19466
19467 if (! font || code == FONT_INVALID_CODE)
19468 return NULL;
19469 font->driver->text_extents (font, &code, 1, &metrics);
19470 return &metrics;
19471 }
19472
19473 /* EXPORT for RIF:
19474 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19475 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19476 assumed to be zero. */
19477
19478 void
19479 x_get_glyph_overhangs (glyph, f, left, right)
19480 struct glyph *glyph;
19481 struct frame *f;
19482 int *left, *right;
19483 {
19484 *left = *right = 0;
19485
19486 if (glyph->type == CHAR_GLYPH)
19487 {
19488 struct face *face;
19489 XChar2b char2b;
19490 struct font_metrics *pcm;
19491
19492 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19493 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19494 {
19495 if (pcm->rbearing > pcm->width)
19496 *right = pcm->rbearing - pcm->width;
19497 if (pcm->lbearing < 0)
19498 *left = -pcm->lbearing;
19499 }
19500 }
19501 else if (glyph->type == COMPOSITE_GLYPH)
19502 {
19503 struct composition *cmp = composition_table[glyph->u.cmp_id];
19504
19505 *right = cmp->rbearing - cmp->pixel_width;
19506 *left = - cmp->lbearing;
19507 }
19508 }
19509
19510
19511 /* Return the index of the first glyph preceding glyph string S that
19512 is overwritten by S because of S's left overhang. Value is -1
19513 if no glyphs are overwritten. */
19514
19515 static int
19516 left_overwritten (s)
19517 struct glyph_string *s;
19518 {
19519 int k;
19520
19521 if (s->left_overhang)
19522 {
19523 int x = 0, i;
19524 struct glyph *glyphs = s->row->glyphs[s->area];
19525 int first = s->first_glyph - glyphs;
19526
19527 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19528 x -= glyphs[i].pixel_width;
19529
19530 k = i + 1;
19531 }
19532 else
19533 k = -1;
19534
19535 return k;
19536 }
19537
19538
19539 /* Return the index of the first glyph preceding glyph string S that
19540 is overwriting S because of its right overhang. Value is -1 if no
19541 glyph in front of S overwrites S. */
19542
19543 static int
19544 left_overwriting (s)
19545 struct glyph_string *s;
19546 {
19547 int i, k, x;
19548 struct glyph *glyphs = s->row->glyphs[s->area];
19549 int first = s->first_glyph - glyphs;
19550
19551 k = -1;
19552 x = 0;
19553 for (i = first - 1; i >= 0; --i)
19554 {
19555 int left, right;
19556 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19557 if (x + right > 0)
19558 k = i;
19559 x -= glyphs[i].pixel_width;
19560 }
19561
19562 return k;
19563 }
19564
19565
19566 /* Return the index of the last glyph following glyph string S that is
19567 not overwritten by S because of S's right overhang. Value is -1 if
19568 no such glyph is found. */
19569
19570 static int
19571 right_overwritten (s)
19572 struct glyph_string *s;
19573 {
19574 int k = -1;
19575
19576 if (s->right_overhang)
19577 {
19578 int x = 0, i;
19579 struct glyph *glyphs = s->row->glyphs[s->area];
19580 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19581 int end = s->row->used[s->area];
19582
19583 for (i = first; i < end && s->right_overhang > x; ++i)
19584 x += glyphs[i].pixel_width;
19585
19586 k = i;
19587 }
19588
19589 return k;
19590 }
19591
19592
19593 /* Return the index of the last glyph following glyph string S that
19594 overwrites S because of its left overhang. Value is negative
19595 if no such glyph is found. */
19596
19597 static int
19598 right_overwriting (s)
19599 struct glyph_string *s;
19600 {
19601 int i, k, x;
19602 int end = s->row->used[s->area];
19603 struct glyph *glyphs = s->row->glyphs[s->area];
19604 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19605
19606 k = -1;
19607 x = 0;
19608 for (i = first; i < end; ++i)
19609 {
19610 int left, right;
19611 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19612 if (x - left < 0)
19613 k = i;
19614 x += glyphs[i].pixel_width;
19615 }
19616
19617 return k;
19618 }
19619
19620
19621 /* Set background width of glyph string S. START is the index of the
19622 first glyph following S. LAST_X is the right-most x-position + 1
19623 in the drawing area. */
19624
19625 static INLINE void
19626 set_glyph_string_background_width (s, start, last_x)
19627 struct glyph_string *s;
19628 int start;
19629 int last_x;
19630 {
19631 /* If the face of this glyph string has to be drawn to the end of
19632 the drawing area, set S->extends_to_end_of_line_p. */
19633
19634 if (start == s->row->used[s->area]
19635 && s->area == TEXT_AREA
19636 && ((s->row->fill_line_p
19637 && (s->hl == DRAW_NORMAL_TEXT
19638 || s->hl == DRAW_IMAGE_RAISED
19639 || s->hl == DRAW_IMAGE_SUNKEN))
19640 || s->hl == DRAW_MOUSE_FACE))
19641 s->extends_to_end_of_line_p = 1;
19642
19643 /* If S extends its face to the end of the line, set its
19644 background_width to the distance to the right edge of the drawing
19645 area. */
19646 if (s->extends_to_end_of_line_p)
19647 s->background_width = last_x - s->x + 1;
19648 else
19649 s->background_width = s->width;
19650 }
19651
19652
19653 /* Compute overhangs and x-positions for glyph string S and its
19654 predecessors, or successors. X is the starting x-position for S.
19655 BACKWARD_P non-zero means process predecessors. */
19656
19657 static void
19658 compute_overhangs_and_x (s, x, backward_p)
19659 struct glyph_string *s;
19660 int x;
19661 int backward_p;
19662 {
19663 if (backward_p)
19664 {
19665 while (s)
19666 {
19667 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19668 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19669 x -= s->width;
19670 s->x = x;
19671 s = s->prev;
19672 }
19673 }
19674 else
19675 {
19676 while (s)
19677 {
19678 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19679 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19680 s->x = x;
19681 x += s->width;
19682 s = s->next;
19683 }
19684 }
19685 }
19686
19687
19688
19689 /* The following macros are only called from draw_glyphs below.
19690 They reference the following parameters of that function directly:
19691 `w', `row', `area', and `overlap_p'
19692 as well as the following local variables:
19693 `s', `f', and `hdc' (in W32) */
19694
19695 #ifdef HAVE_NTGUI
19696 /* On W32, silently add local `hdc' variable to argument list of
19697 init_glyph_string. */
19698 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19699 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19700 #else
19701 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19702 init_glyph_string (s, char2b, w, row, area, start, hl)
19703 #endif
19704
19705 /* Add a glyph string for a stretch glyph to the list of strings
19706 between HEAD and TAIL. START is the index of the stretch glyph in
19707 row area AREA of glyph row ROW. END is the index of the last glyph
19708 in that glyph row area. X is the current output position assigned
19709 to the new glyph string constructed. HL overrides that face of the
19710 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19711 is the right-most x-position of the drawing area. */
19712
19713 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19714 and below -- keep them on one line. */
19715 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19716 do \
19717 { \
19718 s = (struct glyph_string *) alloca (sizeof *s); \
19719 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19720 START = fill_stretch_glyph_string (s, row, area, START, END); \
19721 append_glyph_string (&HEAD, &TAIL, s); \
19722 s->x = (X); \
19723 } \
19724 while (0)
19725
19726
19727 /* Add a glyph string for an image glyph to the list of strings
19728 between HEAD and TAIL. START is the index of the image glyph in
19729 row area AREA of glyph row ROW. END is the index of the last glyph
19730 in that glyph row area. X is the current output position assigned
19731 to the new glyph string constructed. HL overrides that face of the
19732 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19733 is the right-most x-position of the drawing area. */
19734
19735 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19736 do \
19737 { \
19738 s = (struct glyph_string *) alloca (sizeof *s); \
19739 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19740 fill_image_glyph_string (s); \
19741 append_glyph_string (&HEAD, &TAIL, s); \
19742 ++START; \
19743 s->x = (X); \
19744 } \
19745 while (0)
19746
19747
19748 /* Add a glyph string for a sequence of character glyphs to the list
19749 of strings between HEAD and TAIL. START is the index of the first
19750 glyph in row area AREA of glyph row ROW that is part of the new
19751 glyph string. END is the index of the last glyph in that glyph row
19752 area. X is the current output position assigned to the new glyph
19753 string constructed. HL overrides that face of the glyph; e.g. it
19754 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19755 right-most x-position of the drawing area. */
19756
19757 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19758 do \
19759 { \
19760 int face_id; \
19761 XChar2b *char2b; \
19762 \
19763 face_id = (row)->glyphs[area][START].face_id; \
19764 \
19765 s = (struct glyph_string *) alloca (sizeof *s); \
19766 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19767 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19768 append_glyph_string (&HEAD, &TAIL, s); \
19769 s->x = (X); \
19770 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19771 } \
19772 while (0)
19773
19774
19775 /* Add a glyph string for a composite sequence to the list of strings
19776 between HEAD and TAIL. START is the index of the first glyph in
19777 row area AREA of glyph row ROW that is part of the new glyph
19778 string. END is the index of the last glyph in that glyph row area.
19779 X is the current output position assigned to the new glyph string
19780 constructed. HL overrides that face of the glyph; e.g. it is
19781 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19782 x-position of the drawing area. */
19783
19784 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19785 do { \
19786 int face_id = (row)->glyphs[area][START].face_id; \
19787 struct face *base_face = FACE_FROM_ID (f, face_id); \
19788 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19789 struct composition *cmp = composition_table[cmp_id]; \
19790 XChar2b *char2b; \
19791 struct glyph_string *first_s; \
19792 int n; \
19793 \
19794 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19795 \
19796 /* Make glyph_strings for each glyph sequence that is drawable by \
19797 the same face, and append them to HEAD/TAIL. */ \
19798 for (n = 0; n < cmp->glyph_len;) \
19799 { \
19800 s = (struct glyph_string *) alloca (sizeof *s); \
19801 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19802 append_glyph_string (&(HEAD), &(TAIL), s); \
19803 s->cmp = cmp; \
19804 s->gidx = n; \
19805 s->x = (X); \
19806 if (n == 0) \
19807 first_s = s; \
19808 n = fill_composite_glyph_string (s, base_face, overlaps); \
19809 } \
19810 \
19811 ++START; \
19812 s = first_s; \
19813 } while (0)
19814
19815
19816 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19817 of AREA of glyph row ROW on window W between indices START and END.
19818 HL overrides the face for drawing glyph strings, e.g. it is
19819 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19820 x-positions of the drawing area.
19821
19822 This is an ugly monster macro construct because we must use alloca
19823 to allocate glyph strings (because draw_glyphs can be called
19824 asynchronously). */
19825
19826 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19827 do \
19828 { \
19829 HEAD = TAIL = NULL; \
19830 while (START < END) \
19831 { \
19832 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19833 switch (first_glyph->type) \
19834 { \
19835 case CHAR_GLYPH: \
19836 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19837 HL, X, LAST_X); \
19838 break; \
19839 \
19840 case COMPOSITE_GLYPH: \
19841 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19842 HL, X, LAST_X); \
19843 break; \
19844 \
19845 case STRETCH_GLYPH: \
19846 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19847 HL, X, LAST_X); \
19848 break; \
19849 \
19850 case IMAGE_GLYPH: \
19851 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19852 HL, X, LAST_X); \
19853 break; \
19854 \
19855 default: \
19856 abort (); \
19857 } \
19858 \
19859 if (s) \
19860 { \
19861 set_glyph_string_background_width (s, START, LAST_X); \
19862 (X) += s->width; \
19863 } \
19864 } \
19865 } \
19866 while (0)
19867
19868
19869 /* Draw glyphs between START and END in AREA of ROW on window W,
19870 starting at x-position X. X is relative to AREA in W. HL is a
19871 face-override with the following meaning:
19872
19873 DRAW_NORMAL_TEXT draw normally
19874 DRAW_CURSOR draw in cursor face
19875 DRAW_MOUSE_FACE draw in mouse face.
19876 DRAW_INVERSE_VIDEO draw in mode line face
19877 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19878 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19879
19880 If OVERLAPS is non-zero, draw only the foreground of characters and
19881 clip to the physical height of ROW. Non-zero value also defines
19882 the overlapping part to be drawn:
19883
19884 OVERLAPS_PRED overlap with preceding rows
19885 OVERLAPS_SUCC overlap with succeeding rows
19886 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19887 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19888
19889 Value is the x-position reached, relative to AREA of W. */
19890
19891 static int
19892 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19893 struct window *w;
19894 int x;
19895 struct glyph_row *row;
19896 enum glyph_row_area area;
19897 EMACS_INT start, end;
19898 enum draw_glyphs_face hl;
19899 int overlaps;
19900 {
19901 struct glyph_string *head, *tail;
19902 struct glyph_string *s;
19903 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19904 int i, j, x_reached, last_x, area_left = 0;
19905 struct frame *f = XFRAME (WINDOW_FRAME (w));
19906 DECLARE_HDC (hdc);
19907
19908 ALLOCATE_HDC (hdc, f);
19909
19910 /* Let's rather be paranoid than getting a SEGV. */
19911 end = min (end, row->used[area]);
19912 start = max (0, start);
19913 start = min (end, start);
19914
19915 /* Translate X to frame coordinates. Set last_x to the right
19916 end of the drawing area. */
19917 if (row->full_width_p)
19918 {
19919 /* X is relative to the left edge of W, without scroll bars
19920 or fringes. */
19921 area_left = WINDOW_LEFT_EDGE_X (w);
19922 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19923 }
19924 else
19925 {
19926 area_left = window_box_left (w, area);
19927 last_x = area_left + window_box_width (w, area);
19928 }
19929 x += area_left;
19930
19931 /* Build a doubly-linked list of glyph_string structures between
19932 head and tail from what we have to draw. Note that the macro
19933 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19934 the reason we use a separate variable `i'. */
19935 i = start;
19936 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19937 if (tail)
19938 x_reached = tail->x + tail->background_width;
19939 else
19940 x_reached = x;
19941
19942 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19943 the row, redraw some glyphs in front or following the glyph
19944 strings built above. */
19945 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19946 {
19947 struct glyph_string *h, *t;
19948 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19949 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
19950 int dummy_x = 0;
19951
19952 /* If mouse highlighting is on, we may need to draw adjacent
19953 glyphs using mouse-face highlighting. */
19954 if (area == TEXT_AREA && row->mouse_face_p)
19955 {
19956 struct glyph_row *mouse_beg_row, *mouse_end_row;
19957
19958 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19959 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19960
19961 if (row >= mouse_beg_row && row <= mouse_end_row)
19962 {
19963 check_mouse_face = 1;
19964 mouse_beg_col = (row == mouse_beg_row)
19965 ? dpyinfo->mouse_face_beg_col : 0;
19966 mouse_end_col = (row == mouse_end_row)
19967 ? dpyinfo->mouse_face_end_col
19968 : row->used[TEXT_AREA];
19969 }
19970 }
19971
19972 /* Compute overhangs for all glyph strings. */
19973 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19974 for (s = head; s; s = s->next)
19975 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19976
19977 /* Prepend glyph strings for glyphs in front of the first glyph
19978 string that are overwritten because of the first glyph
19979 string's left overhang. The background of all strings
19980 prepended must be drawn because the first glyph string
19981 draws over it. */
19982 i = left_overwritten (head);
19983 if (i >= 0)
19984 {
19985 enum draw_glyphs_face overlap_hl;
19986
19987 /* If this row contains mouse highlighting, attempt to draw
19988 the overlapped glyphs with the correct highlight. This
19989 code fails if the overlap encompasses more than one glyph
19990 and mouse-highlight spans only some of these glyphs.
19991 However, making it work perfectly involves a lot more
19992 code, and I don't know if the pathological case occurs in
19993 practice, so we'll stick to this for now. --- cyd */
19994 if (check_mouse_face
19995 && mouse_beg_col < start && mouse_end_col > i)
19996 overlap_hl = DRAW_MOUSE_FACE;
19997 else
19998 overlap_hl = DRAW_NORMAL_TEXT;
19999
20000 j = i;
20001 BUILD_GLYPH_STRINGS (j, start, h, t,
20002 overlap_hl, dummy_x, last_x);
20003 compute_overhangs_and_x (t, head->x, 1);
20004 prepend_glyph_string_lists (&head, &tail, h, t);
20005 clip_head = head;
20006 }
20007
20008 /* Prepend glyph strings for glyphs in front of the first glyph
20009 string that overwrite that glyph string because of their
20010 right overhang. For these strings, only the foreground must
20011 be drawn, because it draws over the glyph string at `head'.
20012 The background must not be drawn because this would overwrite
20013 right overhangs of preceding glyphs for which no glyph
20014 strings exist. */
20015 i = left_overwriting (head);
20016 if (i >= 0)
20017 {
20018 enum draw_glyphs_face overlap_hl;
20019
20020 if (check_mouse_face
20021 && mouse_beg_col < start && mouse_end_col > i)
20022 overlap_hl = DRAW_MOUSE_FACE;
20023 else
20024 overlap_hl = DRAW_NORMAL_TEXT;
20025
20026 clip_head = head;
20027 BUILD_GLYPH_STRINGS (i, start, h, t,
20028 overlap_hl, dummy_x, last_x);
20029 for (s = h; s; s = s->next)
20030 s->background_filled_p = 1;
20031 compute_overhangs_and_x (t, head->x, 1);
20032 prepend_glyph_string_lists (&head, &tail, h, t);
20033 }
20034
20035 /* Append glyphs strings for glyphs following the last glyph
20036 string tail that are overwritten by tail. The background of
20037 these strings has to be drawn because tail's foreground draws
20038 over it. */
20039 i = right_overwritten (tail);
20040 if (i >= 0)
20041 {
20042 enum draw_glyphs_face overlap_hl;
20043
20044 if (check_mouse_face
20045 && mouse_beg_col < i && mouse_end_col > end)
20046 overlap_hl = DRAW_MOUSE_FACE;
20047 else
20048 overlap_hl = DRAW_NORMAL_TEXT;
20049
20050 BUILD_GLYPH_STRINGS (end, i, h, t,
20051 overlap_hl, x, last_x);
20052 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20053 append_glyph_string_lists (&head, &tail, h, t);
20054 clip_tail = tail;
20055 }
20056
20057 /* Append glyph strings for glyphs following the last glyph
20058 string tail that overwrite tail. The foreground of such
20059 glyphs has to be drawn because it writes into the background
20060 of tail. The background must not be drawn because it could
20061 paint over the foreground of following glyphs. */
20062 i = right_overwriting (tail);
20063 if (i >= 0)
20064 {
20065 enum draw_glyphs_face overlap_hl;
20066 if (check_mouse_face
20067 && mouse_beg_col < i && mouse_end_col > end)
20068 overlap_hl = DRAW_MOUSE_FACE;
20069 else
20070 overlap_hl = DRAW_NORMAL_TEXT;
20071
20072 clip_tail = tail;
20073 i++; /* We must include the Ith glyph. */
20074 BUILD_GLYPH_STRINGS (end, i, h, t,
20075 overlap_hl, x, last_x);
20076 for (s = h; s; s = s->next)
20077 s->background_filled_p = 1;
20078 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20079 append_glyph_string_lists (&head, &tail, h, t);
20080 }
20081 if (clip_head || clip_tail)
20082 for (s = head; s; s = s->next)
20083 {
20084 s->clip_head = clip_head;
20085 s->clip_tail = clip_tail;
20086 }
20087 }
20088
20089 /* Draw all strings. */
20090 for (s = head; s; s = s->next)
20091 FRAME_RIF (f)->draw_glyph_string (s);
20092
20093 if (area == TEXT_AREA
20094 && !row->full_width_p
20095 /* When drawing overlapping rows, only the glyph strings'
20096 foreground is drawn, which doesn't erase a cursor
20097 completely. */
20098 && !overlaps)
20099 {
20100 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20101 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20102 : (tail ? tail->x + tail->background_width : x));
20103 x0 -= area_left;
20104 x1 -= area_left;
20105
20106 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20107 row->y, MATRIX_ROW_BOTTOM_Y (row));
20108 }
20109
20110 /* Value is the x-position up to which drawn, relative to AREA of W.
20111 This doesn't include parts drawn because of overhangs. */
20112 if (row->full_width_p)
20113 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20114 else
20115 x_reached -= area_left;
20116
20117 RELEASE_HDC (hdc, f);
20118
20119 return x_reached;
20120 }
20121
20122 /* Expand row matrix if too narrow. Don't expand if area
20123 is not present. */
20124
20125 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20126 { \
20127 if (!fonts_changed_p \
20128 && (it->glyph_row->glyphs[area] \
20129 < it->glyph_row->glyphs[area + 1])) \
20130 { \
20131 it->w->ncols_scale_factor++; \
20132 fonts_changed_p = 1; \
20133 } \
20134 }
20135
20136 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20137 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20138
20139 static INLINE void
20140 append_glyph (it)
20141 struct it *it;
20142 {
20143 struct glyph *glyph;
20144 enum glyph_row_area area = it->area;
20145
20146 xassert (it->glyph_row);
20147 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20148
20149 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20150 if (glyph < it->glyph_row->glyphs[area + 1])
20151 {
20152 glyph->charpos = CHARPOS (it->position);
20153 glyph->object = it->object;
20154 if (it->pixel_width > 0)
20155 {
20156 glyph->pixel_width = it->pixel_width;
20157 glyph->padding_p = 0;
20158 }
20159 else
20160 {
20161 /* Assure at least 1-pixel width. Otherwise, cursor can't
20162 be displayed correctly. */
20163 glyph->pixel_width = 1;
20164 glyph->padding_p = 1;
20165 }
20166 glyph->ascent = it->ascent;
20167 glyph->descent = it->descent;
20168 glyph->voffset = it->voffset;
20169 glyph->type = CHAR_GLYPH;
20170 glyph->multibyte_p = it->multibyte_p;
20171 glyph->left_box_line_p = it->start_of_box_run_p;
20172 glyph->right_box_line_p = it->end_of_box_run_p;
20173 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20174 || it->phys_descent > it->descent);
20175 glyph->glyph_not_available_p = it->glyph_not_available_p;
20176 glyph->face_id = it->face_id;
20177 glyph->u.ch = it->char_to_display;
20178 glyph->slice = null_glyph_slice;
20179 glyph->font_type = FONT_TYPE_UNKNOWN;
20180 ++it->glyph_row->used[area];
20181 }
20182 else
20183 IT_EXPAND_MATRIX_WIDTH (it, area);
20184 }
20185
20186 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20187 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20188
20189 static INLINE void
20190 append_composite_glyph (it)
20191 struct it *it;
20192 {
20193 struct glyph *glyph;
20194 enum glyph_row_area area = it->area;
20195
20196 xassert (it->glyph_row);
20197
20198 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20199 if (glyph < it->glyph_row->glyphs[area + 1])
20200 {
20201 glyph->charpos = CHARPOS (it->position);
20202 glyph->object = it->object;
20203 glyph->pixel_width = it->pixel_width;
20204 glyph->ascent = it->ascent;
20205 glyph->descent = it->descent;
20206 glyph->voffset = it->voffset;
20207 glyph->type = COMPOSITE_GLYPH;
20208 glyph->multibyte_p = it->multibyte_p;
20209 glyph->left_box_line_p = it->start_of_box_run_p;
20210 glyph->right_box_line_p = it->end_of_box_run_p;
20211 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20212 || it->phys_descent > it->descent);
20213 glyph->padding_p = 0;
20214 glyph->glyph_not_available_p = 0;
20215 glyph->face_id = it->face_id;
20216 glyph->u.cmp_id = it->cmp_id;
20217 glyph->slice = null_glyph_slice;
20218 glyph->font_type = FONT_TYPE_UNKNOWN;
20219 ++it->glyph_row->used[area];
20220 }
20221 else
20222 IT_EXPAND_MATRIX_WIDTH (it, area);
20223 }
20224
20225
20226 /* Change IT->ascent and IT->height according to the setting of
20227 IT->voffset. */
20228
20229 static INLINE void
20230 take_vertical_position_into_account (it)
20231 struct it *it;
20232 {
20233 if (it->voffset)
20234 {
20235 if (it->voffset < 0)
20236 /* Increase the ascent so that we can display the text higher
20237 in the line. */
20238 it->ascent -= it->voffset;
20239 else
20240 /* Increase the descent so that we can display the text lower
20241 in the line. */
20242 it->descent += it->voffset;
20243 }
20244 }
20245
20246
20247 /* Produce glyphs/get display metrics for the image IT is loaded with.
20248 See the description of struct display_iterator in dispextern.h for
20249 an overview of struct display_iterator. */
20250
20251 static void
20252 produce_image_glyph (it)
20253 struct it *it;
20254 {
20255 struct image *img;
20256 struct face *face;
20257 int glyph_ascent, crop;
20258 struct glyph_slice slice;
20259
20260 xassert (it->what == IT_IMAGE);
20261
20262 face = FACE_FROM_ID (it->f, it->face_id);
20263 xassert (face);
20264 /* Make sure X resources of the face is loaded. */
20265 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20266
20267 if (it->image_id < 0)
20268 {
20269 /* Fringe bitmap. */
20270 it->ascent = it->phys_ascent = 0;
20271 it->descent = it->phys_descent = 0;
20272 it->pixel_width = 0;
20273 it->nglyphs = 0;
20274 return;
20275 }
20276
20277 img = IMAGE_FROM_ID (it->f, it->image_id);
20278 xassert (img);
20279 /* Make sure X resources of the image is loaded. */
20280 prepare_image_for_display (it->f, img);
20281
20282 slice.x = slice.y = 0;
20283 slice.width = img->width;
20284 slice.height = img->height;
20285
20286 if (INTEGERP (it->slice.x))
20287 slice.x = XINT (it->slice.x);
20288 else if (FLOATP (it->slice.x))
20289 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20290
20291 if (INTEGERP (it->slice.y))
20292 slice.y = XINT (it->slice.y);
20293 else if (FLOATP (it->slice.y))
20294 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20295
20296 if (INTEGERP (it->slice.width))
20297 slice.width = XINT (it->slice.width);
20298 else if (FLOATP (it->slice.width))
20299 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20300
20301 if (INTEGERP (it->slice.height))
20302 slice.height = XINT (it->slice.height);
20303 else if (FLOATP (it->slice.height))
20304 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20305
20306 if (slice.x >= img->width)
20307 slice.x = img->width;
20308 if (slice.y >= img->height)
20309 slice.y = img->height;
20310 if (slice.x + slice.width >= img->width)
20311 slice.width = img->width - slice.x;
20312 if (slice.y + slice.height > img->height)
20313 slice.height = img->height - slice.y;
20314
20315 if (slice.width == 0 || slice.height == 0)
20316 return;
20317
20318 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20319
20320 it->descent = slice.height - glyph_ascent;
20321 if (slice.y == 0)
20322 it->descent += img->vmargin;
20323 if (slice.y + slice.height == img->height)
20324 it->descent += img->vmargin;
20325 it->phys_descent = it->descent;
20326
20327 it->pixel_width = slice.width;
20328 if (slice.x == 0)
20329 it->pixel_width += img->hmargin;
20330 if (slice.x + slice.width == img->width)
20331 it->pixel_width += img->hmargin;
20332
20333 /* It's quite possible for images to have an ascent greater than
20334 their height, so don't get confused in that case. */
20335 if (it->descent < 0)
20336 it->descent = 0;
20337
20338 #if 0 /* this breaks image tiling */
20339 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20340 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20341 if (face_ascent > it->ascent)
20342 it->ascent = it->phys_ascent = face_ascent;
20343 #endif
20344
20345 it->nglyphs = 1;
20346
20347 if (face->box != FACE_NO_BOX)
20348 {
20349 if (face->box_line_width > 0)
20350 {
20351 if (slice.y == 0)
20352 it->ascent += face->box_line_width;
20353 if (slice.y + slice.height == img->height)
20354 it->descent += face->box_line_width;
20355 }
20356
20357 if (it->start_of_box_run_p && slice.x == 0)
20358 it->pixel_width += eabs (face->box_line_width);
20359 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20360 it->pixel_width += eabs (face->box_line_width);
20361 }
20362
20363 take_vertical_position_into_account (it);
20364
20365 /* Automatically crop wide image glyphs at right edge so we can
20366 draw the cursor on same display row. */
20367 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20368 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20369 {
20370 it->pixel_width -= crop;
20371 slice.width -= crop;
20372 }
20373
20374 if (it->glyph_row)
20375 {
20376 struct glyph *glyph;
20377 enum glyph_row_area area = it->area;
20378
20379 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20380 if (glyph < it->glyph_row->glyphs[area + 1])
20381 {
20382 glyph->charpos = CHARPOS (it->position);
20383 glyph->object = it->object;
20384 glyph->pixel_width = it->pixel_width;
20385 glyph->ascent = glyph_ascent;
20386 glyph->descent = it->descent;
20387 glyph->voffset = it->voffset;
20388 glyph->type = IMAGE_GLYPH;
20389 glyph->multibyte_p = it->multibyte_p;
20390 glyph->left_box_line_p = it->start_of_box_run_p;
20391 glyph->right_box_line_p = it->end_of_box_run_p;
20392 glyph->overlaps_vertically_p = 0;
20393 glyph->padding_p = 0;
20394 glyph->glyph_not_available_p = 0;
20395 glyph->face_id = it->face_id;
20396 glyph->u.img_id = img->id;
20397 glyph->slice = slice;
20398 glyph->font_type = FONT_TYPE_UNKNOWN;
20399 ++it->glyph_row->used[area];
20400 }
20401 else
20402 IT_EXPAND_MATRIX_WIDTH (it, area);
20403 }
20404 }
20405
20406
20407 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20408 of the glyph, WIDTH and HEIGHT are the width and height of the
20409 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20410
20411 static void
20412 append_stretch_glyph (it, object, width, height, ascent)
20413 struct it *it;
20414 Lisp_Object object;
20415 int width, height;
20416 int ascent;
20417 {
20418 struct glyph *glyph;
20419 enum glyph_row_area area = it->area;
20420
20421 xassert (ascent >= 0 && ascent <= height);
20422
20423 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20424 if (glyph < it->glyph_row->glyphs[area + 1])
20425 {
20426 glyph->charpos = CHARPOS (it->position);
20427 glyph->object = object;
20428 glyph->pixel_width = width;
20429 glyph->ascent = ascent;
20430 glyph->descent = height - ascent;
20431 glyph->voffset = it->voffset;
20432 glyph->type = STRETCH_GLYPH;
20433 glyph->multibyte_p = it->multibyte_p;
20434 glyph->left_box_line_p = it->start_of_box_run_p;
20435 glyph->right_box_line_p = it->end_of_box_run_p;
20436 glyph->overlaps_vertically_p = 0;
20437 glyph->padding_p = 0;
20438 glyph->glyph_not_available_p = 0;
20439 glyph->face_id = it->face_id;
20440 glyph->u.stretch.ascent = ascent;
20441 glyph->u.stretch.height = height;
20442 glyph->slice = null_glyph_slice;
20443 glyph->font_type = FONT_TYPE_UNKNOWN;
20444 ++it->glyph_row->used[area];
20445 }
20446 else
20447 IT_EXPAND_MATRIX_WIDTH (it, area);
20448 }
20449
20450
20451 /* Produce a stretch glyph for iterator IT. IT->object is the value
20452 of the glyph property displayed. The value must be a list
20453 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20454 being recognized:
20455
20456 1. `:width WIDTH' specifies that the space should be WIDTH *
20457 canonical char width wide. WIDTH may be an integer or floating
20458 point number.
20459
20460 2. `:relative-width FACTOR' specifies that the width of the stretch
20461 should be computed from the width of the first character having the
20462 `glyph' property, and should be FACTOR times that width.
20463
20464 3. `:align-to HPOS' specifies that the space should be wide enough
20465 to reach HPOS, a value in canonical character units.
20466
20467 Exactly one of the above pairs must be present.
20468
20469 4. `:height HEIGHT' specifies that the height of the stretch produced
20470 should be HEIGHT, measured in canonical character units.
20471
20472 5. `:relative-height FACTOR' specifies that the height of the
20473 stretch should be FACTOR times the height of the characters having
20474 the glyph property.
20475
20476 Either none or exactly one of 4 or 5 must be present.
20477
20478 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20479 of the stretch should be used for the ascent of the stretch.
20480 ASCENT must be in the range 0 <= ASCENT <= 100. */
20481
20482 static void
20483 produce_stretch_glyph (it)
20484 struct it *it;
20485 {
20486 /* (space :width WIDTH :height HEIGHT ...) */
20487 Lisp_Object prop, plist;
20488 int width = 0, height = 0, align_to = -1;
20489 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20490 int ascent = 0;
20491 double tem;
20492 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20493 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20494
20495 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20496
20497 /* List should start with `space'. */
20498 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20499 plist = XCDR (it->object);
20500
20501 /* Compute the width of the stretch. */
20502 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20503 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20504 {
20505 /* Absolute width `:width WIDTH' specified and valid. */
20506 zero_width_ok_p = 1;
20507 width = (int)tem;
20508 }
20509 else if (prop = Fplist_get (plist, QCrelative_width),
20510 NUMVAL (prop) > 0)
20511 {
20512 /* Relative width `:relative-width FACTOR' specified and valid.
20513 Compute the width of the characters having the `glyph'
20514 property. */
20515 struct it it2;
20516 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20517
20518 it2 = *it;
20519 if (it->multibyte_p)
20520 {
20521 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20522 - IT_BYTEPOS (*it));
20523 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20524 }
20525 else
20526 it2.c = *p, it2.len = 1;
20527
20528 it2.glyph_row = NULL;
20529 it2.what = IT_CHARACTER;
20530 x_produce_glyphs (&it2);
20531 width = NUMVAL (prop) * it2.pixel_width;
20532 }
20533 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20534 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20535 {
20536 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20537 align_to = (align_to < 0
20538 ? 0
20539 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20540 else if (align_to < 0)
20541 align_to = window_box_left_offset (it->w, TEXT_AREA);
20542 width = max (0, (int)tem + align_to - it->current_x);
20543 zero_width_ok_p = 1;
20544 }
20545 else
20546 /* Nothing specified -> width defaults to canonical char width. */
20547 width = FRAME_COLUMN_WIDTH (it->f);
20548
20549 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20550 width = 1;
20551
20552 /* Compute height. */
20553 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20554 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20555 {
20556 height = (int)tem;
20557 zero_height_ok_p = 1;
20558 }
20559 else if (prop = Fplist_get (plist, QCrelative_height),
20560 NUMVAL (prop) > 0)
20561 height = FONT_HEIGHT (font) * NUMVAL (prop);
20562 else
20563 height = FONT_HEIGHT (font);
20564
20565 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20566 height = 1;
20567
20568 /* Compute percentage of height used for ascent. If
20569 `:ascent ASCENT' is present and valid, use that. Otherwise,
20570 derive the ascent from the font in use. */
20571 if (prop = Fplist_get (plist, QCascent),
20572 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20573 ascent = height * NUMVAL (prop) / 100.0;
20574 else if (!NILP (prop)
20575 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20576 ascent = min (max (0, (int)tem), height);
20577 else
20578 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20579
20580 if (width > 0 && !it->truncate_lines_p
20581 && it->current_x + width > it->last_visible_x)
20582 width = it->last_visible_x - it->current_x - 1;
20583
20584 if (width > 0 && height > 0 && it->glyph_row)
20585 {
20586 Lisp_Object object = it->stack[it->sp - 1].string;
20587 if (!STRINGP (object))
20588 object = it->w->buffer;
20589 append_stretch_glyph (it, object, width, height, ascent);
20590 }
20591
20592 it->pixel_width = width;
20593 it->ascent = it->phys_ascent = ascent;
20594 it->descent = it->phys_descent = height - it->ascent;
20595 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20596
20597 take_vertical_position_into_account (it);
20598 }
20599
20600 /* Get line-height and line-spacing property at point.
20601 If line-height has format (HEIGHT TOTAL), return TOTAL
20602 in TOTAL_HEIGHT. */
20603
20604 static Lisp_Object
20605 get_line_height_property (it, prop)
20606 struct it *it;
20607 Lisp_Object prop;
20608 {
20609 Lisp_Object position;
20610
20611 if (STRINGP (it->object))
20612 position = make_number (IT_STRING_CHARPOS (*it));
20613 else if (BUFFERP (it->object))
20614 position = make_number (IT_CHARPOS (*it));
20615 else
20616 return Qnil;
20617
20618 return Fget_char_property (position, prop, it->object);
20619 }
20620
20621 /* Calculate line-height and line-spacing properties.
20622 An integer value specifies explicit pixel value.
20623 A float value specifies relative value to current face height.
20624 A cons (float . face-name) specifies relative value to
20625 height of specified face font.
20626
20627 Returns height in pixels, or nil. */
20628
20629
20630 static Lisp_Object
20631 calc_line_height_property (it, val, font, boff, override)
20632 struct it *it;
20633 Lisp_Object val;
20634 struct font *font;
20635 int boff, override;
20636 {
20637 Lisp_Object face_name = Qnil;
20638 int ascent, descent, height;
20639
20640 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20641 return val;
20642
20643 if (CONSP (val))
20644 {
20645 face_name = XCAR (val);
20646 val = XCDR (val);
20647 if (!NUMBERP (val))
20648 val = make_number (1);
20649 if (NILP (face_name))
20650 {
20651 height = it->ascent + it->descent;
20652 goto scale;
20653 }
20654 }
20655
20656 if (NILP (face_name))
20657 {
20658 font = FRAME_FONT (it->f);
20659 boff = FRAME_BASELINE_OFFSET (it->f);
20660 }
20661 else if (EQ (face_name, Qt))
20662 {
20663 override = 0;
20664 }
20665 else
20666 {
20667 int face_id;
20668 struct face *face;
20669
20670 face_id = lookup_named_face (it->f, face_name, 0);
20671 if (face_id < 0)
20672 return make_number (-1);
20673
20674 face = FACE_FROM_ID (it->f, face_id);
20675 font = face->font;
20676 if (font == NULL)
20677 return make_number (-1);
20678 boff = font->baseline_offset;
20679 if (font->vertical_centering)
20680 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20681 }
20682
20683 ascent = FONT_BASE (font) + boff;
20684 descent = FONT_DESCENT (font) - boff;
20685
20686 if (override)
20687 {
20688 it->override_ascent = ascent;
20689 it->override_descent = descent;
20690 it->override_boff = boff;
20691 }
20692
20693 height = ascent + descent;
20694
20695 scale:
20696 if (FLOATP (val))
20697 height = (int)(XFLOAT_DATA (val) * height);
20698 else if (INTEGERP (val))
20699 height *= XINT (val);
20700
20701 return make_number (height);
20702 }
20703
20704
20705 /* RIF:
20706 Produce glyphs/get display metrics for the display element IT is
20707 loaded with. See the description of struct it in dispextern.h
20708 for an overview of struct it. */
20709
20710 void
20711 x_produce_glyphs (it)
20712 struct it *it;
20713 {
20714 int extra_line_spacing = it->extra_line_spacing;
20715
20716 it->glyph_not_available_p = 0;
20717
20718 if (it->what == IT_CHARACTER)
20719 {
20720 XChar2b char2b;
20721 struct font *font;
20722 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20723 struct font_metrics *pcm;
20724 int font_not_found_p;
20725 int boff; /* baseline offset */
20726 /* We may change it->multibyte_p upon unibyte<->multibyte
20727 conversion. So, save the current value now and restore it
20728 later.
20729
20730 Note: It seems that we don't have to record multibyte_p in
20731 struct glyph because the character code itself tells if or
20732 not the character is multibyte. Thus, in the future, we must
20733 consider eliminating the field `multibyte_p' in the struct
20734 glyph. */
20735 int saved_multibyte_p = it->multibyte_p;
20736
20737 /* Maybe translate single-byte characters to multibyte, or the
20738 other way. */
20739 it->char_to_display = it->c;
20740 if (!ASCII_BYTE_P (it->c)
20741 && ! it->multibyte_p)
20742 {
20743 if (SINGLE_BYTE_CHAR_P (it->c)
20744 && unibyte_display_via_language_environment)
20745 it->char_to_display = unibyte_char_to_multibyte (it->c);
20746 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20747 {
20748 it->multibyte_p = 1;
20749 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20750 -1, Qnil);
20751 face = FACE_FROM_ID (it->f, it->face_id);
20752 }
20753 }
20754
20755 /* Get font to use. Encode IT->char_to_display. */
20756 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20757 &char2b, it->multibyte_p, 0);
20758 font = face->font;
20759
20760 /* When no suitable font found, use the default font. */
20761 font_not_found_p = font == NULL;
20762 if (font_not_found_p)
20763 {
20764 font = FRAME_FONT (it->f);
20765 boff = FRAME_BASELINE_OFFSET (it->f);
20766 }
20767 else
20768 {
20769 boff = font->baseline_offset;
20770 if (font->vertical_centering)
20771 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20772 }
20773
20774 if (it->char_to_display >= ' '
20775 && (!it->multibyte_p || it->char_to_display < 128))
20776 {
20777 /* Either unibyte or ASCII. */
20778 int stretched_p;
20779
20780 it->nglyphs = 1;
20781
20782 pcm = get_per_char_metric (it->f, font, &char2b);
20783
20784 if (it->override_ascent >= 0)
20785 {
20786 it->ascent = it->override_ascent;
20787 it->descent = it->override_descent;
20788 boff = it->override_boff;
20789 }
20790 else
20791 {
20792 it->ascent = FONT_BASE (font) + boff;
20793 it->descent = FONT_DESCENT (font) - boff;
20794 }
20795
20796 if (pcm)
20797 {
20798 it->phys_ascent = pcm->ascent + boff;
20799 it->phys_descent = pcm->descent - boff;
20800 it->pixel_width = pcm->width;
20801 }
20802 else
20803 {
20804 it->glyph_not_available_p = 1;
20805 it->phys_ascent = it->ascent;
20806 it->phys_descent = it->descent;
20807 it->pixel_width = FONT_WIDTH (font);
20808 }
20809
20810 if (it->constrain_row_ascent_descent_p)
20811 {
20812 if (it->descent > it->max_descent)
20813 {
20814 it->ascent += it->descent - it->max_descent;
20815 it->descent = it->max_descent;
20816 }
20817 if (it->ascent > it->max_ascent)
20818 {
20819 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20820 it->ascent = it->max_ascent;
20821 }
20822 it->phys_ascent = min (it->phys_ascent, it->ascent);
20823 it->phys_descent = min (it->phys_descent, it->descent);
20824 extra_line_spacing = 0;
20825 }
20826
20827 /* If this is a space inside a region of text with
20828 `space-width' property, change its width. */
20829 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20830 if (stretched_p)
20831 it->pixel_width *= XFLOATINT (it->space_width);
20832
20833 /* If face has a box, add the box thickness to the character
20834 height. If character has a box line to the left and/or
20835 right, add the box line width to the character's width. */
20836 if (face->box != FACE_NO_BOX)
20837 {
20838 int thick = face->box_line_width;
20839
20840 if (thick > 0)
20841 {
20842 it->ascent += thick;
20843 it->descent += thick;
20844 }
20845 else
20846 thick = -thick;
20847
20848 if (it->start_of_box_run_p)
20849 it->pixel_width += thick;
20850 if (it->end_of_box_run_p)
20851 it->pixel_width += thick;
20852 }
20853
20854 /* If face has an overline, add the height of the overline
20855 (1 pixel) and a 1 pixel margin to the character height. */
20856 if (face->overline_p)
20857 it->ascent += overline_margin;
20858
20859 if (it->constrain_row_ascent_descent_p)
20860 {
20861 if (it->ascent > it->max_ascent)
20862 it->ascent = it->max_ascent;
20863 if (it->descent > it->max_descent)
20864 it->descent = it->max_descent;
20865 }
20866
20867 take_vertical_position_into_account (it);
20868
20869 /* If we have to actually produce glyphs, do it. */
20870 if (it->glyph_row)
20871 {
20872 if (stretched_p)
20873 {
20874 /* Translate a space with a `space-width' property
20875 into a stretch glyph. */
20876 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20877 / FONT_HEIGHT (font));
20878 append_stretch_glyph (it, it->object, it->pixel_width,
20879 it->ascent + it->descent, ascent);
20880 }
20881 else
20882 append_glyph (it);
20883
20884 /* If characters with lbearing or rbearing are displayed
20885 in this line, record that fact in a flag of the
20886 glyph row. This is used to optimize X output code. */
20887 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20888 it->glyph_row->contains_overlapping_glyphs_p = 1;
20889 }
20890 if (! stretched_p && it->pixel_width == 0)
20891 /* We assure that all visible glyphs have at least 1-pixel
20892 width. */
20893 it->pixel_width = 1;
20894 }
20895 else if (it->char_to_display == '\n')
20896 {
20897 /* A newline has no width but we need the height of the line.
20898 But if previous part of the line set a height, don't
20899 increase that height */
20900
20901 Lisp_Object height;
20902 Lisp_Object total_height = Qnil;
20903
20904 it->override_ascent = -1;
20905 it->pixel_width = 0;
20906 it->nglyphs = 0;
20907
20908 height = get_line_height_property(it, Qline_height);
20909 /* Split (line-height total-height) list */
20910 if (CONSP (height)
20911 && CONSP (XCDR (height))
20912 && NILP (XCDR (XCDR (height))))
20913 {
20914 total_height = XCAR (XCDR (height));
20915 height = XCAR (height);
20916 }
20917 height = calc_line_height_property(it, height, font, boff, 1);
20918
20919 if (it->override_ascent >= 0)
20920 {
20921 it->ascent = it->override_ascent;
20922 it->descent = it->override_descent;
20923 boff = it->override_boff;
20924 }
20925 else
20926 {
20927 it->ascent = FONT_BASE (font) + boff;
20928 it->descent = FONT_DESCENT (font) - boff;
20929 }
20930
20931 if (EQ (height, Qt))
20932 {
20933 if (it->descent > it->max_descent)
20934 {
20935 it->ascent += it->descent - it->max_descent;
20936 it->descent = it->max_descent;
20937 }
20938 if (it->ascent > it->max_ascent)
20939 {
20940 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20941 it->ascent = it->max_ascent;
20942 }
20943 it->phys_ascent = min (it->phys_ascent, it->ascent);
20944 it->phys_descent = min (it->phys_descent, it->descent);
20945 it->constrain_row_ascent_descent_p = 1;
20946 extra_line_spacing = 0;
20947 }
20948 else
20949 {
20950 Lisp_Object spacing;
20951
20952 it->phys_ascent = it->ascent;
20953 it->phys_descent = it->descent;
20954
20955 if ((it->max_ascent > 0 || it->max_descent > 0)
20956 && face->box != FACE_NO_BOX
20957 && face->box_line_width > 0)
20958 {
20959 it->ascent += face->box_line_width;
20960 it->descent += face->box_line_width;
20961 }
20962 if (!NILP (height)
20963 && XINT (height) > it->ascent + it->descent)
20964 it->ascent = XINT (height) - it->descent;
20965
20966 if (!NILP (total_height))
20967 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20968 else
20969 {
20970 spacing = get_line_height_property(it, Qline_spacing);
20971 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20972 }
20973 if (INTEGERP (spacing))
20974 {
20975 extra_line_spacing = XINT (spacing);
20976 if (!NILP (total_height))
20977 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20978 }
20979 }
20980 }
20981 else if (it->char_to_display == '\t')
20982 {
20983 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20984 int x = it->current_x + it->continuation_lines_width;
20985 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20986
20987 /* If the distance from the current position to the next tab
20988 stop is less than a space character width, use the
20989 tab stop after that. */
20990 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20991 next_tab_x += tab_width;
20992
20993 it->pixel_width = next_tab_x - x;
20994 it->nglyphs = 1;
20995 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20996 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20997
20998 if (it->glyph_row)
20999 {
21000 append_stretch_glyph (it, it->object, it->pixel_width,
21001 it->ascent + it->descent, it->ascent);
21002 }
21003 }
21004 else
21005 {
21006 /* A multi-byte character. Assume that the display width of the
21007 character is the width of the character multiplied by the
21008 width of the font. */
21009
21010 /* If we found a font, this font should give us the right
21011 metrics. If we didn't find a font, use the frame's
21012 default font and calculate the width of the character by
21013 multiplying the width of font by the width of the
21014 character. */
21015
21016 pcm = get_per_char_metric (it->f, font, &char2b);
21017
21018 if (font_not_found_p || !pcm)
21019 {
21020 int char_width = CHAR_WIDTH (it->char_to_display);
21021
21022 if (char_width == 0)
21023 /* This is a non spacing character. But, as we are
21024 going to display an empty box, the box must occupy
21025 at least one column. */
21026 char_width = 1;
21027 it->glyph_not_available_p = 1;
21028 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21029 it->phys_ascent = FONT_BASE (font) + boff;
21030 it->phys_descent = FONT_DESCENT (font) - boff;
21031 }
21032 else
21033 {
21034 it->pixel_width = pcm->width;
21035 it->phys_ascent = pcm->ascent + boff;
21036 it->phys_descent = pcm->descent - boff;
21037 if (it->glyph_row
21038 && (pcm->lbearing < 0
21039 || pcm->rbearing > pcm->width))
21040 it->glyph_row->contains_overlapping_glyphs_p = 1;
21041 }
21042 it->nglyphs = 1;
21043 it->ascent = FONT_BASE (font) + boff;
21044 it->descent = FONT_DESCENT (font) - boff;
21045 if (face->box != FACE_NO_BOX)
21046 {
21047 int thick = face->box_line_width;
21048
21049 if (thick > 0)
21050 {
21051 it->ascent += thick;
21052 it->descent += thick;
21053 }
21054 else
21055 thick = - thick;
21056
21057 if (it->start_of_box_run_p)
21058 it->pixel_width += thick;
21059 if (it->end_of_box_run_p)
21060 it->pixel_width += thick;
21061 }
21062
21063 /* If face has an overline, add the height of the overline
21064 (1 pixel) and a 1 pixel margin to the character height. */
21065 if (face->overline_p)
21066 it->ascent += overline_margin;
21067
21068 take_vertical_position_into_account (it);
21069
21070 if (it->ascent < 0)
21071 it->ascent = 0;
21072 if (it->descent < 0)
21073 it->descent = 0;
21074
21075 if (it->glyph_row)
21076 append_glyph (it);
21077 if (it->pixel_width == 0)
21078 /* We assure that all visible glyphs have at least 1-pixel
21079 width. */
21080 it->pixel_width = 1;
21081 }
21082 it->multibyte_p = saved_multibyte_p;
21083 }
21084 else if (it->what == IT_COMPOSITION)
21085 {
21086 /* Note: A composition is represented as one glyph in the
21087 glyph matrix. There are no padding glyphs.
21088
21089 Important is that pixel_width, ascent, and descent are the
21090 values of what is drawn by draw_glyphs (i.e. the values of
21091 the overall glyphs composed). */
21092 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21093 int boff; /* baseline offset */
21094 struct composition *cmp = composition_table[it->cmp_id];
21095 int glyph_len = cmp->glyph_len;
21096 struct font *font = face->font;
21097
21098 it->nglyphs = 1;
21099
21100 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21101 {
21102 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21103 font_prepare_composition (cmp, it->f);
21104 }
21105 else
21106 /* If we have not yet calculated pixel size data of glyphs of
21107 the composition for the current face font, calculate them
21108 now. Theoretically, we have to check all fonts for the
21109 glyphs, but that requires much time and memory space. So,
21110 here we check only the font of the first glyph. This leads
21111 to incorrect display, but it's very rare, and C-l (recenter)
21112 can correct the display anyway. */
21113 if (! cmp->font || cmp->font != font)
21114 {
21115 /* Ascent and descent of the font of the first character
21116 of this composition (adjusted by baseline offset).
21117 Ascent and descent of overall glyphs should not be less
21118 than them respectively. */
21119 int font_ascent, font_descent, font_height;
21120 /* Bounding box of the overall glyphs. */
21121 int leftmost, rightmost, lowest, highest;
21122 int lbearing, rbearing;
21123 int i, width, ascent, descent;
21124 int left_padded = 0, right_padded = 0;
21125 int face_id;
21126 int c;
21127 XChar2b char2b;
21128 struct font_metrics *pcm;
21129 int font_not_found_p;
21130 int pos;
21131
21132 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21133 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21134 break;
21135 if (glyph_len < cmp->glyph_len)
21136 right_padded = 1;
21137 for (i = 0; i < glyph_len; i++)
21138 {
21139 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21140 break;
21141 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21142 }
21143 if (i > 0)
21144 left_padded = 1;
21145
21146 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21147 : IT_CHARPOS (*it));
21148 /* When no suitable font found, use the default font. */
21149 font_not_found_p = font == NULL;
21150 if (font_not_found_p)
21151 {
21152 face = face->ascii_face;
21153 font = face->font;
21154 }
21155 boff = font->baseline_offset;
21156 if (font->vertical_centering)
21157 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21158 font_ascent = FONT_BASE (font) + boff;
21159 font_descent = FONT_DESCENT (font) - boff;
21160 font_height = FONT_HEIGHT (font);
21161
21162 cmp->font = (void *) font;
21163
21164 pcm = NULL;
21165 if (! font_not_found_p)
21166 {
21167 get_char_face_and_encoding (it->f, c, it->face_id,
21168 &char2b, it->multibyte_p, 0);
21169 pcm = get_per_char_metric (it->f, font, &char2b);
21170 }
21171
21172 /* Initialize the bounding box. */
21173 if (pcm)
21174 {
21175 width = pcm->width;
21176 ascent = pcm->ascent;
21177 descent = pcm->descent;
21178 lbearing = pcm->lbearing;
21179 rbearing = pcm->rbearing;
21180 }
21181 else
21182 {
21183 width = FONT_WIDTH (font);
21184 ascent = FONT_BASE (font);
21185 descent = FONT_DESCENT (font);
21186 lbearing = 0;
21187 rbearing = width;
21188 }
21189
21190 rightmost = width;
21191 leftmost = 0;
21192 lowest = - descent + boff;
21193 highest = ascent + boff;
21194
21195 if (! font_not_found_p
21196 && font->default_ascent
21197 && CHAR_TABLE_P (Vuse_default_ascent)
21198 && !NILP (Faref (Vuse_default_ascent,
21199 make_number (it->char_to_display))))
21200 highest = font->default_ascent + boff;
21201
21202 /* Draw the first glyph at the normal position. It may be
21203 shifted to right later if some other glyphs are drawn
21204 at the left. */
21205 cmp->offsets[i * 2] = 0;
21206 cmp->offsets[i * 2 + 1] = boff;
21207 cmp->lbearing = lbearing;
21208 cmp->rbearing = rbearing;
21209
21210 /* Set cmp->offsets for the remaining glyphs. */
21211 for (i++; i < glyph_len; i++)
21212 {
21213 int left, right, btm, top;
21214 int ch = COMPOSITION_GLYPH (cmp, i);
21215 int face_id;
21216 struct face *this_face;
21217 int this_boff;
21218
21219 if (ch == '\t')
21220 ch = ' ';
21221 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21222 this_face = FACE_FROM_ID (it->f, face_id);
21223 font = this_face->font;
21224
21225 if (font == NULL)
21226 pcm = NULL;
21227 else
21228 {
21229 this_boff = font->baseline_offset;
21230 if (font->vertical_centering)
21231 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21232 get_char_face_and_encoding (it->f, ch, face_id,
21233 &char2b, it->multibyte_p, 0);
21234 pcm = get_per_char_metric (it->f, font, &char2b);
21235 }
21236 if (! pcm)
21237 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21238 else
21239 {
21240 width = pcm->width;
21241 ascent = pcm->ascent;
21242 descent = pcm->descent;
21243 lbearing = pcm->lbearing;
21244 rbearing = pcm->rbearing;
21245 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21246 {
21247 /* Relative composition with or without
21248 alternate chars. */
21249 left = (leftmost + rightmost - width) / 2;
21250 btm = - descent + boff;
21251 if (font->relative_compose
21252 && (! CHAR_TABLE_P (Vignore_relative_composition)
21253 || NILP (Faref (Vignore_relative_composition,
21254 make_number (ch)))))
21255 {
21256
21257 if (- descent >= font->relative_compose)
21258 /* One extra pixel between two glyphs. */
21259 btm = highest + 1;
21260 else if (ascent <= 0)
21261 /* One extra pixel between two glyphs. */
21262 btm = lowest - 1 - ascent - descent;
21263 }
21264 }
21265 else
21266 {
21267 /* A composition rule is specified by an integer
21268 value that encodes global and new reference
21269 points (GREF and NREF). GREF and NREF are
21270 specified by numbers as below:
21271
21272 0---1---2 -- ascent
21273 | |
21274 | |
21275 | |
21276 9--10--11 -- center
21277 | |
21278 ---3---4---5--- baseline
21279 | |
21280 6---7---8 -- descent
21281 */
21282 int rule = COMPOSITION_RULE (cmp, i);
21283 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21284
21285 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21286 grefx = gref % 3, nrefx = nref % 3;
21287 grefy = gref / 3, nrefy = nref / 3;
21288 if (xoff)
21289 xoff = font_height * (xoff - 128) / 256;
21290 if (yoff)
21291 yoff = font_height * (yoff - 128) / 256;
21292
21293 left = (leftmost
21294 + grefx * (rightmost - leftmost) / 2
21295 - nrefx * width / 2
21296 + xoff);
21297
21298 btm = ((grefy == 0 ? highest
21299 : grefy == 1 ? 0
21300 : grefy == 2 ? lowest
21301 : (highest + lowest) / 2)
21302 - (nrefy == 0 ? ascent + descent
21303 : nrefy == 1 ? descent - boff
21304 : nrefy == 2 ? 0
21305 : (ascent + descent) / 2)
21306 + yoff);
21307 }
21308
21309 cmp->offsets[i * 2] = left;
21310 cmp->offsets[i * 2 + 1] = btm + descent;
21311
21312 /* Update the bounding box of the overall glyphs. */
21313 if (width > 0)
21314 {
21315 right = left + width;
21316 if (left < leftmost)
21317 leftmost = left;
21318 if (right > rightmost)
21319 rightmost = right;
21320 }
21321 top = btm + descent + ascent;
21322 if (top > highest)
21323 highest = top;
21324 if (btm < lowest)
21325 lowest = btm;
21326
21327 if (cmp->lbearing > left + lbearing)
21328 cmp->lbearing = left + lbearing;
21329 if (cmp->rbearing < left + rbearing)
21330 cmp->rbearing = left + rbearing;
21331 }
21332 }
21333
21334 /* If there are glyphs whose x-offsets are negative,
21335 shift all glyphs to the right and make all x-offsets
21336 non-negative. */
21337 if (leftmost < 0)
21338 {
21339 for (i = 0; i < cmp->glyph_len; i++)
21340 cmp->offsets[i * 2] -= leftmost;
21341 rightmost -= leftmost;
21342 cmp->lbearing -= leftmost;
21343 cmp->rbearing -= leftmost;
21344 }
21345
21346 if (left_padded && cmp->lbearing < 0)
21347 {
21348 for (i = 0; i < cmp->glyph_len; i++)
21349 cmp->offsets[i * 2] -= cmp->lbearing;
21350 rightmost -= cmp->lbearing;
21351 cmp->rbearing -= cmp->lbearing;
21352 cmp->lbearing = 0;
21353 }
21354 if (right_padded && rightmost < cmp->rbearing)
21355 {
21356 rightmost = cmp->rbearing;
21357 }
21358
21359 cmp->pixel_width = rightmost;
21360 cmp->ascent = highest;
21361 cmp->descent = - lowest;
21362 if (cmp->ascent < font_ascent)
21363 cmp->ascent = font_ascent;
21364 if (cmp->descent < font_descent)
21365 cmp->descent = font_descent;
21366 }
21367
21368 if (it->glyph_row
21369 && (cmp->lbearing < 0
21370 || cmp->rbearing > cmp->pixel_width))
21371 it->glyph_row->contains_overlapping_glyphs_p = 1;
21372
21373 it->pixel_width = cmp->pixel_width;
21374 it->ascent = it->phys_ascent = cmp->ascent;
21375 it->descent = it->phys_descent = cmp->descent;
21376 if (face->box != FACE_NO_BOX)
21377 {
21378 int thick = face->box_line_width;
21379
21380 if (thick > 0)
21381 {
21382 it->ascent += thick;
21383 it->descent += thick;
21384 }
21385 else
21386 thick = - thick;
21387
21388 if (it->start_of_box_run_p)
21389 it->pixel_width += thick;
21390 if (it->end_of_box_run_p)
21391 it->pixel_width += thick;
21392 }
21393
21394 /* If face has an overline, add the height of the overline
21395 (1 pixel) and a 1 pixel margin to the character height. */
21396 if (face->overline_p)
21397 it->ascent += overline_margin;
21398
21399 take_vertical_position_into_account (it);
21400 if (it->ascent < 0)
21401 it->ascent = 0;
21402 if (it->descent < 0)
21403 it->descent = 0;
21404
21405 if (it->glyph_row)
21406 append_composite_glyph (it);
21407 }
21408 else if (it->what == IT_IMAGE)
21409 produce_image_glyph (it);
21410 else if (it->what == IT_STRETCH)
21411 produce_stretch_glyph (it);
21412
21413 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21414 because this isn't true for images with `:ascent 100'. */
21415 xassert (it->ascent >= 0 && it->descent >= 0);
21416 if (it->area == TEXT_AREA)
21417 it->current_x += it->pixel_width;
21418
21419 if (extra_line_spacing > 0)
21420 {
21421 it->descent += extra_line_spacing;
21422 if (extra_line_spacing > it->max_extra_line_spacing)
21423 it->max_extra_line_spacing = extra_line_spacing;
21424 }
21425
21426 it->max_ascent = max (it->max_ascent, it->ascent);
21427 it->max_descent = max (it->max_descent, it->descent);
21428 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21429 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21430 }
21431
21432 /* EXPORT for RIF:
21433 Output LEN glyphs starting at START at the nominal cursor position.
21434 Advance the nominal cursor over the text. The global variable
21435 updated_window contains the window being updated, updated_row is
21436 the glyph row being updated, and updated_area is the area of that
21437 row being updated. */
21438
21439 void
21440 x_write_glyphs (start, len)
21441 struct glyph *start;
21442 int len;
21443 {
21444 int x, hpos;
21445
21446 xassert (updated_window && updated_row);
21447 BLOCK_INPUT;
21448
21449 /* Write glyphs. */
21450
21451 hpos = start - updated_row->glyphs[updated_area];
21452 x = draw_glyphs (updated_window, output_cursor.x,
21453 updated_row, updated_area,
21454 hpos, hpos + len,
21455 DRAW_NORMAL_TEXT, 0);
21456
21457 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21458 if (updated_area == TEXT_AREA
21459 && updated_window->phys_cursor_on_p
21460 && updated_window->phys_cursor.vpos == output_cursor.vpos
21461 && updated_window->phys_cursor.hpos >= hpos
21462 && updated_window->phys_cursor.hpos < hpos + len)
21463 updated_window->phys_cursor_on_p = 0;
21464
21465 UNBLOCK_INPUT;
21466
21467 /* Advance the output cursor. */
21468 output_cursor.hpos += len;
21469 output_cursor.x = x;
21470 }
21471
21472
21473 /* EXPORT for RIF:
21474 Insert LEN glyphs from START at the nominal cursor position. */
21475
21476 void
21477 x_insert_glyphs (start, len)
21478 struct glyph *start;
21479 int len;
21480 {
21481 struct frame *f;
21482 struct window *w;
21483 int line_height, shift_by_width, shifted_region_width;
21484 struct glyph_row *row;
21485 struct glyph *glyph;
21486 int frame_x, frame_y;
21487 EMACS_INT hpos;
21488
21489 xassert (updated_window && updated_row);
21490 BLOCK_INPUT;
21491 w = updated_window;
21492 f = XFRAME (WINDOW_FRAME (w));
21493
21494 /* Get the height of the line we are in. */
21495 row = updated_row;
21496 line_height = row->height;
21497
21498 /* Get the width of the glyphs to insert. */
21499 shift_by_width = 0;
21500 for (glyph = start; glyph < start + len; ++glyph)
21501 shift_by_width += glyph->pixel_width;
21502
21503 /* Get the width of the region to shift right. */
21504 shifted_region_width = (window_box_width (w, updated_area)
21505 - output_cursor.x
21506 - shift_by_width);
21507
21508 /* Shift right. */
21509 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21510 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21511
21512 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21513 line_height, shift_by_width);
21514
21515 /* Write the glyphs. */
21516 hpos = start - row->glyphs[updated_area];
21517 draw_glyphs (w, output_cursor.x, row, updated_area,
21518 hpos, hpos + len,
21519 DRAW_NORMAL_TEXT, 0);
21520
21521 /* Advance the output cursor. */
21522 output_cursor.hpos += len;
21523 output_cursor.x += shift_by_width;
21524 UNBLOCK_INPUT;
21525 }
21526
21527
21528 /* EXPORT for RIF:
21529 Erase the current text line from the nominal cursor position
21530 (inclusive) to pixel column TO_X (exclusive). The idea is that
21531 everything from TO_X onward is already erased.
21532
21533 TO_X is a pixel position relative to updated_area of
21534 updated_window. TO_X == -1 means clear to the end of this area. */
21535
21536 void
21537 x_clear_end_of_line (to_x)
21538 int to_x;
21539 {
21540 struct frame *f;
21541 struct window *w = updated_window;
21542 int max_x, min_y, max_y;
21543 int from_x, from_y, to_y;
21544
21545 xassert (updated_window && updated_row);
21546 f = XFRAME (w->frame);
21547
21548 if (updated_row->full_width_p)
21549 max_x = WINDOW_TOTAL_WIDTH (w);
21550 else
21551 max_x = window_box_width (w, updated_area);
21552 max_y = window_text_bottom_y (w);
21553
21554 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21555 of window. For TO_X > 0, truncate to end of drawing area. */
21556 if (to_x == 0)
21557 return;
21558 else if (to_x < 0)
21559 to_x = max_x;
21560 else
21561 to_x = min (to_x, max_x);
21562
21563 to_y = min (max_y, output_cursor.y + updated_row->height);
21564
21565 /* Notice if the cursor will be cleared by this operation. */
21566 if (!updated_row->full_width_p)
21567 notice_overwritten_cursor (w, updated_area,
21568 output_cursor.x, -1,
21569 updated_row->y,
21570 MATRIX_ROW_BOTTOM_Y (updated_row));
21571
21572 from_x = output_cursor.x;
21573
21574 /* Translate to frame coordinates. */
21575 if (updated_row->full_width_p)
21576 {
21577 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21578 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21579 }
21580 else
21581 {
21582 int area_left = window_box_left (w, updated_area);
21583 from_x += area_left;
21584 to_x += area_left;
21585 }
21586
21587 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21588 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21589 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21590
21591 /* Prevent inadvertently clearing to end of the X window. */
21592 if (to_x > from_x && to_y > from_y)
21593 {
21594 BLOCK_INPUT;
21595 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21596 to_x - from_x, to_y - from_y);
21597 UNBLOCK_INPUT;
21598 }
21599 }
21600
21601 #endif /* HAVE_WINDOW_SYSTEM */
21602
21603
21604 \f
21605 /***********************************************************************
21606 Cursor types
21607 ***********************************************************************/
21608
21609 /* Value is the internal representation of the specified cursor type
21610 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21611 of the bar cursor. */
21612
21613 static enum text_cursor_kinds
21614 get_specified_cursor_type (arg, width)
21615 Lisp_Object arg;
21616 int *width;
21617 {
21618 enum text_cursor_kinds type;
21619
21620 if (NILP (arg))
21621 return NO_CURSOR;
21622
21623 if (EQ (arg, Qbox))
21624 return FILLED_BOX_CURSOR;
21625
21626 if (EQ (arg, Qhollow))
21627 return HOLLOW_BOX_CURSOR;
21628
21629 if (EQ (arg, Qbar))
21630 {
21631 *width = 2;
21632 return BAR_CURSOR;
21633 }
21634
21635 if (CONSP (arg)
21636 && EQ (XCAR (arg), Qbar)
21637 && INTEGERP (XCDR (arg))
21638 && XINT (XCDR (arg)) >= 0)
21639 {
21640 *width = XINT (XCDR (arg));
21641 return BAR_CURSOR;
21642 }
21643
21644 if (EQ (arg, Qhbar))
21645 {
21646 *width = 2;
21647 return HBAR_CURSOR;
21648 }
21649
21650 if (CONSP (arg)
21651 && EQ (XCAR (arg), Qhbar)
21652 && INTEGERP (XCDR (arg))
21653 && XINT (XCDR (arg)) >= 0)
21654 {
21655 *width = XINT (XCDR (arg));
21656 return HBAR_CURSOR;
21657 }
21658
21659 /* Treat anything unknown as "hollow box cursor".
21660 It was bad to signal an error; people have trouble fixing
21661 .Xdefaults with Emacs, when it has something bad in it. */
21662 type = HOLLOW_BOX_CURSOR;
21663
21664 return type;
21665 }
21666
21667 /* Set the default cursor types for specified frame. */
21668 void
21669 set_frame_cursor_types (f, arg)
21670 struct frame *f;
21671 Lisp_Object arg;
21672 {
21673 int width;
21674 Lisp_Object tem;
21675
21676 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21677 FRAME_CURSOR_WIDTH (f) = width;
21678
21679 /* By default, set up the blink-off state depending on the on-state. */
21680
21681 tem = Fassoc (arg, Vblink_cursor_alist);
21682 if (!NILP (tem))
21683 {
21684 FRAME_BLINK_OFF_CURSOR (f)
21685 = get_specified_cursor_type (XCDR (tem), &width);
21686 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21687 }
21688 else
21689 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21690 }
21691
21692
21693 /* Return the cursor we want to be displayed in window W. Return
21694 width of bar/hbar cursor through WIDTH arg. Return with
21695 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21696 (i.e. if the `system caret' should track this cursor).
21697
21698 In a mini-buffer window, we want the cursor only to appear if we
21699 are reading input from this window. For the selected window, we
21700 want the cursor type given by the frame parameter or buffer local
21701 setting of cursor-type. If explicitly marked off, draw no cursor.
21702 In all other cases, we want a hollow box cursor. */
21703
21704 static enum text_cursor_kinds
21705 get_window_cursor_type (w, glyph, width, active_cursor)
21706 struct window *w;
21707 struct glyph *glyph;
21708 int *width;
21709 int *active_cursor;
21710 {
21711 struct frame *f = XFRAME (w->frame);
21712 struct buffer *b = XBUFFER (w->buffer);
21713 int cursor_type = DEFAULT_CURSOR;
21714 Lisp_Object alt_cursor;
21715 int non_selected = 0;
21716
21717 *active_cursor = 1;
21718
21719 /* Echo area */
21720 if (cursor_in_echo_area
21721 && FRAME_HAS_MINIBUF_P (f)
21722 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21723 {
21724 if (w == XWINDOW (echo_area_window))
21725 {
21726 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21727 {
21728 *width = FRAME_CURSOR_WIDTH (f);
21729 return FRAME_DESIRED_CURSOR (f);
21730 }
21731 else
21732 return get_specified_cursor_type (b->cursor_type, width);
21733 }
21734
21735 *active_cursor = 0;
21736 non_selected = 1;
21737 }
21738
21739 /* Detect a nonselected window or nonselected frame. */
21740 else if (w != XWINDOW (f->selected_window)
21741 #ifdef HAVE_WINDOW_SYSTEM
21742 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21743 #endif
21744 )
21745 {
21746 *active_cursor = 0;
21747
21748 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21749 return NO_CURSOR;
21750
21751 non_selected = 1;
21752 }
21753
21754 /* Never display a cursor in a window in which cursor-type is nil. */
21755 if (NILP (b->cursor_type))
21756 return NO_CURSOR;
21757
21758 /* Get the normal cursor type for this window. */
21759 if (EQ (b->cursor_type, Qt))
21760 {
21761 cursor_type = FRAME_DESIRED_CURSOR (f);
21762 *width = FRAME_CURSOR_WIDTH (f);
21763 }
21764 else
21765 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21766
21767 /* Use cursor-in-non-selected-windows instead
21768 for non-selected window or frame. */
21769 if (non_selected)
21770 {
21771 alt_cursor = b->cursor_in_non_selected_windows;
21772 if (!EQ (Qt, alt_cursor))
21773 return get_specified_cursor_type (alt_cursor, width);
21774 /* t means modify the normal cursor type. */
21775 if (cursor_type == FILLED_BOX_CURSOR)
21776 cursor_type = HOLLOW_BOX_CURSOR;
21777 else if (cursor_type == BAR_CURSOR && *width > 1)
21778 --*width;
21779 return cursor_type;
21780 }
21781
21782 /* Use normal cursor if not blinked off. */
21783 if (!w->cursor_off_p)
21784 {
21785 #ifdef HAVE_WINDOW_SYSTEM
21786 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21787 {
21788 if (cursor_type == FILLED_BOX_CURSOR)
21789 {
21790 /* Using a block cursor on large images can be very annoying.
21791 So use a hollow cursor for "large" images.
21792 If image is not transparent (no mask), also use hollow cursor. */
21793 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21794 if (img != NULL && IMAGEP (img->spec))
21795 {
21796 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21797 where N = size of default frame font size.
21798 This should cover most of the "tiny" icons people may use. */
21799 if (!img->mask
21800 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21801 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21802 cursor_type = HOLLOW_BOX_CURSOR;
21803 }
21804 }
21805 else if (cursor_type != NO_CURSOR)
21806 {
21807 /* Display current only supports BOX and HOLLOW cursors for images.
21808 So for now, unconditionally use a HOLLOW cursor when cursor is
21809 not a solid box cursor. */
21810 cursor_type = HOLLOW_BOX_CURSOR;
21811 }
21812 }
21813 #endif
21814 return cursor_type;
21815 }
21816
21817 /* Cursor is blinked off, so determine how to "toggle" it. */
21818
21819 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21820 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21821 return get_specified_cursor_type (XCDR (alt_cursor), width);
21822
21823 /* Then see if frame has specified a specific blink off cursor type. */
21824 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21825 {
21826 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21827 return FRAME_BLINK_OFF_CURSOR (f);
21828 }
21829
21830 #if 0
21831 /* Some people liked having a permanently visible blinking cursor,
21832 while others had very strong opinions against it. So it was
21833 decided to remove it. KFS 2003-09-03 */
21834
21835 /* Finally perform built-in cursor blinking:
21836 filled box <-> hollow box
21837 wide [h]bar <-> narrow [h]bar
21838 narrow [h]bar <-> no cursor
21839 other type <-> no cursor */
21840
21841 if (cursor_type == FILLED_BOX_CURSOR)
21842 return HOLLOW_BOX_CURSOR;
21843
21844 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21845 {
21846 *width = 1;
21847 return cursor_type;
21848 }
21849 #endif
21850
21851 return NO_CURSOR;
21852 }
21853
21854
21855 #ifdef HAVE_WINDOW_SYSTEM
21856
21857 /* Notice when the text cursor of window W has been completely
21858 overwritten by a drawing operation that outputs glyphs in AREA
21859 starting at X0 and ending at X1 in the line starting at Y0 and
21860 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21861 the rest of the line after X0 has been written. Y coordinates
21862 are window-relative. */
21863
21864 static void
21865 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21866 struct window *w;
21867 enum glyph_row_area area;
21868 int x0, y0, x1, y1;
21869 {
21870 int cx0, cx1, cy0, cy1;
21871 struct glyph_row *row;
21872
21873 if (!w->phys_cursor_on_p)
21874 return;
21875 if (area != TEXT_AREA)
21876 return;
21877
21878 if (w->phys_cursor.vpos < 0
21879 || w->phys_cursor.vpos >= w->current_matrix->nrows
21880 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21881 !(row->enabled_p && row->displays_text_p)))
21882 return;
21883
21884 if (row->cursor_in_fringe_p)
21885 {
21886 row->cursor_in_fringe_p = 0;
21887 draw_fringe_bitmap (w, row, 0);
21888 w->phys_cursor_on_p = 0;
21889 return;
21890 }
21891
21892 cx0 = w->phys_cursor.x;
21893 cx1 = cx0 + w->phys_cursor_width;
21894 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21895 return;
21896
21897 /* The cursor image will be completely removed from the
21898 screen if the output area intersects the cursor area in
21899 y-direction. When we draw in [y0 y1[, and some part of
21900 the cursor is at y < y0, that part must have been drawn
21901 before. When scrolling, the cursor is erased before
21902 actually scrolling, so we don't come here. When not
21903 scrolling, the rows above the old cursor row must have
21904 changed, and in this case these rows must have written
21905 over the cursor image.
21906
21907 Likewise if part of the cursor is below y1, with the
21908 exception of the cursor being in the first blank row at
21909 the buffer and window end because update_text_area
21910 doesn't draw that row. (Except when it does, but
21911 that's handled in update_text_area.) */
21912
21913 cy0 = w->phys_cursor.y;
21914 cy1 = cy0 + w->phys_cursor_height;
21915 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21916 return;
21917
21918 w->phys_cursor_on_p = 0;
21919 }
21920
21921 #endif /* HAVE_WINDOW_SYSTEM */
21922
21923 \f
21924 /************************************************************************
21925 Mouse Face
21926 ************************************************************************/
21927
21928 #ifdef HAVE_WINDOW_SYSTEM
21929
21930 /* EXPORT for RIF:
21931 Fix the display of area AREA of overlapping row ROW in window W
21932 with respect to the overlapping part OVERLAPS. */
21933
21934 void
21935 x_fix_overlapping_area (w, row, area, overlaps)
21936 struct window *w;
21937 struct glyph_row *row;
21938 enum glyph_row_area area;
21939 int overlaps;
21940 {
21941 int i, x;
21942
21943 BLOCK_INPUT;
21944
21945 x = 0;
21946 for (i = 0; i < row->used[area];)
21947 {
21948 if (row->glyphs[area][i].overlaps_vertically_p)
21949 {
21950 int start = i, start_x = x;
21951
21952 do
21953 {
21954 x += row->glyphs[area][i].pixel_width;
21955 ++i;
21956 }
21957 while (i < row->used[area]
21958 && row->glyphs[area][i].overlaps_vertically_p);
21959
21960 draw_glyphs (w, start_x, row, area,
21961 start, i,
21962 DRAW_NORMAL_TEXT, overlaps);
21963 }
21964 else
21965 {
21966 x += row->glyphs[area][i].pixel_width;
21967 ++i;
21968 }
21969 }
21970
21971 UNBLOCK_INPUT;
21972 }
21973
21974
21975 /* EXPORT:
21976 Draw the cursor glyph of window W in glyph row ROW. See the
21977 comment of draw_glyphs for the meaning of HL. */
21978
21979 void
21980 draw_phys_cursor_glyph (w, row, hl)
21981 struct window *w;
21982 struct glyph_row *row;
21983 enum draw_glyphs_face hl;
21984 {
21985 /* If cursor hpos is out of bounds, don't draw garbage. This can
21986 happen in mini-buffer windows when switching between echo area
21987 glyphs and mini-buffer. */
21988 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21989 {
21990 int on_p = w->phys_cursor_on_p;
21991 int x1;
21992 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21993 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21994 hl, 0);
21995 w->phys_cursor_on_p = on_p;
21996
21997 if (hl == DRAW_CURSOR)
21998 w->phys_cursor_width = x1 - w->phys_cursor.x;
21999 /* When we erase the cursor, and ROW is overlapped by other
22000 rows, make sure that these overlapping parts of other rows
22001 are redrawn. */
22002 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22003 {
22004 w->phys_cursor_width = x1 - w->phys_cursor.x;
22005
22006 if (row > w->current_matrix->rows
22007 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22008 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22009 OVERLAPS_ERASED_CURSOR);
22010
22011 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22012 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22013 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22014 OVERLAPS_ERASED_CURSOR);
22015 }
22016 }
22017 }
22018
22019
22020 /* EXPORT:
22021 Erase the image of a cursor of window W from the screen. */
22022
22023 void
22024 erase_phys_cursor (w)
22025 struct window *w;
22026 {
22027 struct frame *f = XFRAME (w->frame);
22028 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22029 int hpos = w->phys_cursor.hpos;
22030 int vpos = w->phys_cursor.vpos;
22031 int mouse_face_here_p = 0;
22032 struct glyph_matrix *active_glyphs = w->current_matrix;
22033 struct glyph_row *cursor_row;
22034 struct glyph *cursor_glyph;
22035 enum draw_glyphs_face hl;
22036
22037 /* No cursor displayed or row invalidated => nothing to do on the
22038 screen. */
22039 if (w->phys_cursor_type == NO_CURSOR)
22040 goto mark_cursor_off;
22041
22042 /* VPOS >= active_glyphs->nrows means that window has been resized.
22043 Don't bother to erase the cursor. */
22044 if (vpos >= active_glyphs->nrows)
22045 goto mark_cursor_off;
22046
22047 /* If row containing cursor is marked invalid, there is nothing we
22048 can do. */
22049 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22050 if (!cursor_row->enabled_p)
22051 goto mark_cursor_off;
22052
22053 /* If line spacing is > 0, old cursor may only be partially visible in
22054 window after split-window. So adjust visible height. */
22055 cursor_row->visible_height = min (cursor_row->visible_height,
22056 window_text_bottom_y (w) - cursor_row->y);
22057
22058 /* If row is completely invisible, don't attempt to delete a cursor which
22059 isn't there. This can happen if cursor is at top of a window, and
22060 we switch to a buffer with a header line in that window. */
22061 if (cursor_row->visible_height <= 0)
22062 goto mark_cursor_off;
22063
22064 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22065 if (cursor_row->cursor_in_fringe_p)
22066 {
22067 cursor_row->cursor_in_fringe_p = 0;
22068 draw_fringe_bitmap (w, cursor_row, 0);
22069 goto mark_cursor_off;
22070 }
22071
22072 /* This can happen when the new row is shorter than the old one.
22073 In this case, either draw_glyphs or clear_end_of_line
22074 should have cleared the cursor. Note that we wouldn't be
22075 able to erase the cursor in this case because we don't have a
22076 cursor glyph at hand. */
22077 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22078 goto mark_cursor_off;
22079
22080 /* If the cursor is in the mouse face area, redisplay that when
22081 we clear the cursor. */
22082 if (! NILP (dpyinfo->mouse_face_window)
22083 && w == XWINDOW (dpyinfo->mouse_face_window)
22084 && (vpos > dpyinfo->mouse_face_beg_row
22085 || (vpos == dpyinfo->mouse_face_beg_row
22086 && hpos >= dpyinfo->mouse_face_beg_col))
22087 && (vpos < dpyinfo->mouse_face_end_row
22088 || (vpos == dpyinfo->mouse_face_end_row
22089 && hpos < dpyinfo->mouse_face_end_col))
22090 /* Don't redraw the cursor's spot in mouse face if it is at the
22091 end of a line (on a newline). The cursor appears there, but
22092 mouse highlighting does not. */
22093 && cursor_row->used[TEXT_AREA] > hpos)
22094 mouse_face_here_p = 1;
22095
22096 /* Maybe clear the display under the cursor. */
22097 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22098 {
22099 int x, y, left_x;
22100 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22101 int width;
22102
22103 cursor_glyph = get_phys_cursor_glyph (w);
22104 if (cursor_glyph == NULL)
22105 goto mark_cursor_off;
22106
22107 width = cursor_glyph->pixel_width;
22108 left_x = window_box_left_offset (w, TEXT_AREA);
22109 x = w->phys_cursor.x;
22110 if (x < left_x)
22111 width -= left_x - x;
22112 width = min (width, window_box_width (w, TEXT_AREA) - x);
22113 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22114 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22115
22116 if (width > 0)
22117 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22118 }
22119
22120 /* Erase the cursor by redrawing the character underneath it. */
22121 if (mouse_face_here_p)
22122 hl = DRAW_MOUSE_FACE;
22123 else
22124 hl = DRAW_NORMAL_TEXT;
22125 draw_phys_cursor_glyph (w, cursor_row, hl);
22126
22127 mark_cursor_off:
22128 w->phys_cursor_on_p = 0;
22129 w->phys_cursor_type = NO_CURSOR;
22130 }
22131
22132
22133 /* EXPORT:
22134 Display or clear cursor of window W. If ON is zero, clear the
22135 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22136 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22137
22138 void
22139 display_and_set_cursor (w, on, hpos, vpos, x, y)
22140 struct window *w;
22141 int on, hpos, vpos, x, y;
22142 {
22143 struct frame *f = XFRAME (w->frame);
22144 int new_cursor_type;
22145 int new_cursor_width;
22146 int active_cursor;
22147 struct glyph_row *glyph_row;
22148 struct glyph *glyph;
22149
22150 /* This is pointless on invisible frames, and dangerous on garbaged
22151 windows and frames; in the latter case, the frame or window may
22152 be in the midst of changing its size, and x and y may be off the
22153 window. */
22154 if (! FRAME_VISIBLE_P (f)
22155 || FRAME_GARBAGED_P (f)
22156 || vpos >= w->current_matrix->nrows
22157 || hpos >= w->current_matrix->matrix_w)
22158 return;
22159
22160 /* If cursor is off and we want it off, return quickly. */
22161 if (!on && !w->phys_cursor_on_p)
22162 return;
22163
22164 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22165 /* If cursor row is not enabled, we don't really know where to
22166 display the cursor. */
22167 if (!glyph_row->enabled_p)
22168 {
22169 w->phys_cursor_on_p = 0;
22170 return;
22171 }
22172
22173 glyph = NULL;
22174 if (!glyph_row->exact_window_width_line_p
22175 || hpos < glyph_row->used[TEXT_AREA])
22176 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22177
22178 xassert (interrupt_input_blocked);
22179
22180 /* Set new_cursor_type to the cursor we want to be displayed. */
22181 new_cursor_type = get_window_cursor_type (w, glyph,
22182 &new_cursor_width, &active_cursor);
22183
22184 /* If cursor is currently being shown and we don't want it to be or
22185 it is in the wrong place, or the cursor type is not what we want,
22186 erase it. */
22187 if (w->phys_cursor_on_p
22188 && (!on
22189 || w->phys_cursor.x != x
22190 || w->phys_cursor.y != y
22191 || new_cursor_type != w->phys_cursor_type
22192 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22193 && new_cursor_width != w->phys_cursor_width)))
22194 erase_phys_cursor (w);
22195
22196 /* Don't check phys_cursor_on_p here because that flag is only set
22197 to zero in some cases where we know that the cursor has been
22198 completely erased, to avoid the extra work of erasing the cursor
22199 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22200 still not be visible, or it has only been partly erased. */
22201 if (on)
22202 {
22203 w->phys_cursor_ascent = glyph_row->ascent;
22204 w->phys_cursor_height = glyph_row->height;
22205
22206 /* Set phys_cursor_.* before x_draw_.* is called because some
22207 of them may need the information. */
22208 w->phys_cursor.x = x;
22209 w->phys_cursor.y = glyph_row->y;
22210 w->phys_cursor.hpos = hpos;
22211 w->phys_cursor.vpos = vpos;
22212 }
22213
22214 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22215 new_cursor_type, new_cursor_width,
22216 on, active_cursor);
22217 }
22218
22219
22220 /* Switch the display of W's cursor on or off, according to the value
22221 of ON. */
22222
22223 static void
22224 update_window_cursor (w, on)
22225 struct window *w;
22226 int on;
22227 {
22228 /* Don't update cursor in windows whose frame is in the process
22229 of being deleted. */
22230 if (w->current_matrix)
22231 {
22232 BLOCK_INPUT;
22233 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22234 w->phys_cursor.x, w->phys_cursor.y);
22235 UNBLOCK_INPUT;
22236 }
22237 }
22238
22239
22240 /* Call update_window_cursor with parameter ON_P on all leaf windows
22241 in the window tree rooted at W. */
22242
22243 static void
22244 update_cursor_in_window_tree (w, on_p)
22245 struct window *w;
22246 int on_p;
22247 {
22248 while (w)
22249 {
22250 if (!NILP (w->hchild))
22251 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22252 else if (!NILP (w->vchild))
22253 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22254 else
22255 update_window_cursor (w, on_p);
22256
22257 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22258 }
22259 }
22260
22261
22262 /* EXPORT:
22263 Display the cursor on window W, or clear it, according to ON_P.
22264 Don't change the cursor's position. */
22265
22266 void
22267 x_update_cursor (f, on_p)
22268 struct frame *f;
22269 int on_p;
22270 {
22271 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22272 }
22273
22274
22275 /* EXPORT:
22276 Clear the cursor of window W to background color, and mark the
22277 cursor as not shown. This is used when the text where the cursor
22278 is is about to be rewritten. */
22279
22280 void
22281 x_clear_cursor (w)
22282 struct window *w;
22283 {
22284 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22285 update_window_cursor (w, 0);
22286 }
22287
22288
22289 /* EXPORT:
22290 Display the active region described by mouse_face_* according to DRAW. */
22291
22292 void
22293 show_mouse_face (dpyinfo, draw)
22294 Display_Info *dpyinfo;
22295 enum draw_glyphs_face draw;
22296 {
22297 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22298 struct frame *f = XFRAME (WINDOW_FRAME (w));
22299
22300 if (/* If window is in the process of being destroyed, don't bother
22301 to do anything. */
22302 w->current_matrix != NULL
22303 /* Don't update mouse highlight if hidden */
22304 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22305 /* Recognize when we are called to operate on rows that don't exist
22306 anymore. This can happen when a window is split. */
22307 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22308 {
22309 int phys_cursor_on_p = w->phys_cursor_on_p;
22310 struct glyph_row *row, *first, *last;
22311
22312 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22313 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22314
22315 for (row = first; row <= last && row->enabled_p; ++row)
22316 {
22317 int start_hpos, end_hpos, start_x;
22318
22319 /* For all but the first row, the highlight starts at column 0. */
22320 if (row == first)
22321 {
22322 start_hpos = dpyinfo->mouse_face_beg_col;
22323 start_x = dpyinfo->mouse_face_beg_x;
22324 }
22325 else
22326 {
22327 start_hpos = 0;
22328 start_x = 0;
22329 }
22330
22331 if (row == last)
22332 end_hpos = dpyinfo->mouse_face_end_col;
22333 else
22334 {
22335 end_hpos = row->used[TEXT_AREA];
22336 if (draw == DRAW_NORMAL_TEXT)
22337 row->fill_line_p = 1; /* Clear to end of line */
22338 }
22339
22340 if (end_hpos > start_hpos)
22341 {
22342 draw_glyphs (w, start_x, row, TEXT_AREA,
22343 start_hpos, end_hpos,
22344 draw, 0);
22345
22346 row->mouse_face_p
22347 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22348 }
22349 }
22350
22351 /* When we've written over the cursor, arrange for it to
22352 be displayed again. */
22353 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22354 {
22355 BLOCK_INPUT;
22356 display_and_set_cursor (w, 1,
22357 w->phys_cursor.hpos, w->phys_cursor.vpos,
22358 w->phys_cursor.x, w->phys_cursor.y);
22359 UNBLOCK_INPUT;
22360 }
22361 }
22362
22363 /* Change the mouse cursor. */
22364 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22365 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22366 else if (draw == DRAW_MOUSE_FACE)
22367 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22368 else
22369 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22370 }
22371
22372 /* EXPORT:
22373 Clear out the mouse-highlighted active region.
22374 Redraw it un-highlighted first. Value is non-zero if mouse
22375 face was actually drawn unhighlighted. */
22376
22377 int
22378 clear_mouse_face (dpyinfo)
22379 Display_Info *dpyinfo;
22380 {
22381 int cleared = 0;
22382
22383 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22384 {
22385 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22386 cleared = 1;
22387 }
22388
22389 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22390 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22391 dpyinfo->mouse_face_window = Qnil;
22392 dpyinfo->mouse_face_overlay = Qnil;
22393 return cleared;
22394 }
22395
22396
22397 /* EXPORT:
22398 Non-zero if physical cursor of window W is within mouse face. */
22399
22400 int
22401 cursor_in_mouse_face_p (w)
22402 struct window *w;
22403 {
22404 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22405 int in_mouse_face = 0;
22406
22407 if (WINDOWP (dpyinfo->mouse_face_window)
22408 && XWINDOW (dpyinfo->mouse_face_window) == w)
22409 {
22410 int hpos = w->phys_cursor.hpos;
22411 int vpos = w->phys_cursor.vpos;
22412
22413 if (vpos >= dpyinfo->mouse_face_beg_row
22414 && vpos <= dpyinfo->mouse_face_end_row
22415 && (vpos > dpyinfo->mouse_face_beg_row
22416 || hpos >= dpyinfo->mouse_face_beg_col)
22417 && (vpos < dpyinfo->mouse_face_end_row
22418 || hpos < dpyinfo->mouse_face_end_col
22419 || dpyinfo->mouse_face_past_end))
22420 in_mouse_face = 1;
22421 }
22422
22423 return in_mouse_face;
22424 }
22425
22426
22427
22428 \f
22429 /* Find the glyph matrix position of buffer position CHARPOS in window
22430 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22431 current glyphs must be up to date. If CHARPOS is above window
22432 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22433 of last line in W. In the row containing CHARPOS, stop before glyphs
22434 having STOP as object. */
22435
22436 #if 1 /* This is a version of fast_find_position that's more correct
22437 in the presence of hscrolling, for example. I didn't install
22438 it right away because the problem fixed is minor, it failed
22439 in 20.x as well, and I think it's too risky to install
22440 so near the release of 21.1. 2001-09-25 gerd. */
22441
22442 #ifndef HAVE_CARBON
22443 static
22444 #endif
22445 int
22446 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22447 struct window *w;
22448 EMACS_INT charpos;
22449 int *hpos, *vpos, *x, *y;
22450 Lisp_Object stop;
22451 {
22452 struct glyph_row *row, *first;
22453 struct glyph *glyph, *end;
22454 int past_end = 0;
22455
22456 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22457 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22458 {
22459 *x = first->x;
22460 *y = first->y;
22461 *hpos = 0;
22462 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22463 return 1;
22464 }
22465
22466 row = row_containing_pos (w, charpos, first, NULL, 0);
22467 if (row == NULL)
22468 {
22469 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22470 past_end = 1;
22471 }
22472
22473 /* If whole rows or last part of a row came from a display overlay,
22474 row_containing_pos will skip over such rows because their end pos
22475 equals the start pos of the overlay or interval.
22476
22477 Move back if we have a STOP object and previous row's
22478 end glyph came from STOP. */
22479 if (!NILP (stop))
22480 {
22481 struct glyph_row *prev;
22482 while ((prev = row - 1, prev >= first)
22483 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22484 && prev->used[TEXT_AREA] > 0)
22485 {
22486 struct glyph *beg = prev->glyphs[TEXT_AREA];
22487 glyph = beg + prev->used[TEXT_AREA];
22488 while (--glyph >= beg
22489 && INTEGERP (glyph->object));
22490 if (glyph < beg
22491 || !EQ (stop, glyph->object))
22492 break;
22493 row = prev;
22494 }
22495 }
22496
22497 *x = row->x;
22498 *y = row->y;
22499 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22500
22501 glyph = row->glyphs[TEXT_AREA];
22502 end = glyph + row->used[TEXT_AREA];
22503
22504 /* Skip over glyphs not having an object at the start of the row.
22505 These are special glyphs like truncation marks on terminal
22506 frames. */
22507 if (row->displays_text_p)
22508 while (glyph < end
22509 && INTEGERP (glyph->object)
22510 && !EQ (stop, glyph->object)
22511 && glyph->charpos < 0)
22512 {
22513 *x += glyph->pixel_width;
22514 ++glyph;
22515 }
22516
22517 while (glyph < end
22518 && !INTEGERP (glyph->object)
22519 && !EQ (stop, glyph->object)
22520 && (!BUFFERP (glyph->object)
22521 || glyph->charpos < charpos))
22522 {
22523 *x += glyph->pixel_width;
22524 ++glyph;
22525 }
22526
22527 *hpos = glyph - row->glyphs[TEXT_AREA];
22528 return !past_end;
22529 }
22530
22531 #else /* not 1 */
22532
22533 static int
22534 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22535 struct window *w;
22536 EMACS_INT pos;
22537 int *hpos, *vpos, *x, *y;
22538 Lisp_Object stop;
22539 {
22540 int i;
22541 int lastcol;
22542 int maybe_next_line_p = 0;
22543 int line_start_position;
22544 int yb = window_text_bottom_y (w);
22545 struct glyph_row *row, *best_row;
22546 int row_vpos, best_row_vpos;
22547 int current_x;
22548
22549 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22550 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22551
22552 while (row->y < yb)
22553 {
22554 if (row->used[TEXT_AREA])
22555 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22556 else
22557 line_start_position = 0;
22558
22559 if (line_start_position > pos)
22560 break;
22561 /* If the position sought is the end of the buffer,
22562 don't include the blank lines at the bottom of the window. */
22563 else if (line_start_position == pos
22564 && pos == BUF_ZV (XBUFFER (w->buffer)))
22565 {
22566 maybe_next_line_p = 1;
22567 break;
22568 }
22569 else if (line_start_position > 0)
22570 {
22571 best_row = row;
22572 best_row_vpos = row_vpos;
22573 }
22574
22575 if (row->y + row->height >= yb)
22576 break;
22577
22578 ++row;
22579 ++row_vpos;
22580 }
22581
22582 /* Find the right column within BEST_ROW. */
22583 lastcol = 0;
22584 current_x = best_row->x;
22585 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22586 {
22587 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22588 int charpos = glyph->charpos;
22589
22590 if (BUFFERP (glyph->object))
22591 {
22592 if (charpos == pos)
22593 {
22594 *hpos = i;
22595 *vpos = best_row_vpos;
22596 *x = current_x;
22597 *y = best_row->y;
22598 return 1;
22599 }
22600 else if (charpos > pos)
22601 break;
22602 }
22603 else if (EQ (glyph->object, stop))
22604 break;
22605
22606 if (charpos > 0)
22607 lastcol = i;
22608 current_x += glyph->pixel_width;
22609 }
22610
22611 /* If we're looking for the end of the buffer,
22612 and we didn't find it in the line we scanned,
22613 use the start of the following line. */
22614 if (maybe_next_line_p)
22615 {
22616 ++best_row;
22617 ++best_row_vpos;
22618 lastcol = 0;
22619 current_x = best_row->x;
22620 }
22621
22622 *vpos = best_row_vpos;
22623 *hpos = lastcol + 1;
22624 *x = current_x;
22625 *y = best_row->y;
22626 return 0;
22627 }
22628
22629 #endif /* not 1 */
22630
22631
22632 /* Find the position of the glyph for position POS in OBJECT in
22633 window W's current matrix, and return in *X, *Y the pixel
22634 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22635
22636 RIGHT_P non-zero means return the position of the right edge of the
22637 glyph, RIGHT_P zero means return the left edge position.
22638
22639 If no glyph for POS exists in the matrix, return the position of
22640 the glyph with the next smaller position that is in the matrix, if
22641 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22642 exists in the matrix, return the position of the glyph with the
22643 next larger position in OBJECT.
22644
22645 Value is non-zero if a glyph was found. */
22646
22647 static int
22648 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22649 struct window *w;
22650 EMACS_INT pos;
22651 Lisp_Object object;
22652 int *hpos, *vpos, *x, *y;
22653 int right_p;
22654 {
22655 int yb = window_text_bottom_y (w);
22656 struct glyph_row *r;
22657 struct glyph *best_glyph = NULL;
22658 struct glyph_row *best_row = NULL;
22659 int best_x = 0;
22660
22661 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22662 r->enabled_p && r->y < yb;
22663 ++r)
22664 {
22665 struct glyph *g = r->glyphs[TEXT_AREA];
22666 struct glyph *e = g + r->used[TEXT_AREA];
22667 int gx;
22668
22669 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22670 if (EQ (g->object, object))
22671 {
22672 if (g->charpos == pos)
22673 {
22674 best_glyph = g;
22675 best_x = gx;
22676 best_row = r;
22677 goto found;
22678 }
22679 else if (best_glyph == NULL
22680 || ((eabs (g->charpos - pos)
22681 < eabs (best_glyph->charpos - pos))
22682 && (right_p
22683 ? g->charpos < pos
22684 : g->charpos > pos)))
22685 {
22686 best_glyph = g;
22687 best_x = gx;
22688 best_row = r;
22689 }
22690 }
22691 }
22692
22693 found:
22694
22695 if (best_glyph)
22696 {
22697 *x = best_x;
22698 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22699
22700 if (right_p)
22701 {
22702 *x += best_glyph->pixel_width;
22703 ++*hpos;
22704 }
22705
22706 *y = best_row->y;
22707 *vpos = best_row - w->current_matrix->rows;
22708 }
22709
22710 return best_glyph != NULL;
22711 }
22712
22713
22714 /* See if position X, Y is within a hot-spot of an image. */
22715
22716 static int
22717 on_hot_spot_p (hot_spot, x, y)
22718 Lisp_Object hot_spot;
22719 int x, y;
22720 {
22721 if (!CONSP (hot_spot))
22722 return 0;
22723
22724 if (EQ (XCAR (hot_spot), Qrect))
22725 {
22726 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22727 Lisp_Object rect = XCDR (hot_spot);
22728 Lisp_Object tem;
22729 if (!CONSP (rect))
22730 return 0;
22731 if (!CONSP (XCAR (rect)))
22732 return 0;
22733 if (!CONSP (XCDR (rect)))
22734 return 0;
22735 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22736 return 0;
22737 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22738 return 0;
22739 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22740 return 0;
22741 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22742 return 0;
22743 return 1;
22744 }
22745 else if (EQ (XCAR (hot_spot), Qcircle))
22746 {
22747 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22748 Lisp_Object circ = XCDR (hot_spot);
22749 Lisp_Object lr, lx0, ly0;
22750 if (CONSP (circ)
22751 && CONSP (XCAR (circ))
22752 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22753 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22754 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22755 {
22756 double r = XFLOATINT (lr);
22757 double dx = XINT (lx0) - x;
22758 double dy = XINT (ly0) - y;
22759 return (dx * dx + dy * dy <= r * r);
22760 }
22761 }
22762 else if (EQ (XCAR (hot_spot), Qpoly))
22763 {
22764 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22765 if (VECTORP (XCDR (hot_spot)))
22766 {
22767 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22768 Lisp_Object *poly = v->contents;
22769 int n = v->size;
22770 int i;
22771 int inside = 0;
22772 Lisp_Object lx, ly;
22773 int x0, y0;
22774
22775 /* Need an even number of coordinates, and at least 3 edges. */
22776 if (n < 6 || n & 1)
22777 return 0;
22778
22779 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22780 If count is odd, we are inside polygon. Pixels on edges
22781 may or may not be included depending on actual geometry of the
22782 polygon. */
22783 if ((lx = poly[n-2], !INTEGERP (lx))
22784 || (ly = poly[n-1], !INTEGERP (lx)))
22785 return 0;
22786 x0 = XINT (lx), y0 = XINT (ly);
22787 for (i = 0; i < n; i += 2)
22788 {
22789 int x1 = x0, y1 = y0;
22790 if ((lx = poly[i], !INTEGERP (lx))
22791 || (ly = poly[i+1], !INTEGERP (ly)))
22792 return 0;
22793 x0 = XINT (lx), y0 = XINT (ly);
22794
22795 /* Does this segment cross the X line? */
22796 if (x0 >= x)
22797 {
22798 if (x1 >= x)
22799 continue;
22800 }
22801 else if (x1 < x)
22802 continue;
22803 if (y > y0 && y > y1)
22804 continue;
22805 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22806 inside = !inside;
22807 }
22808 return inside;
22809 }
22810 }
22811 return 0;
22812 }
22813
22814 Lisp_Object
22815 find_hot_spot (map, x, y)
22816 Lisp_Object map;
22817 int x, y;
22818 {
22819 while (CONSP (map))
22820 {
22821 if (CONSP (XCAR (map))
22822 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22823 return XCAR (map);
22824 map = XCDR (map);
22825 }
22826
22827 return Qnil;
22828 }
22829
22830 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22831 3, 3, 0,
22832 doc: /* Lookup in image map MAP coordinates X and Y.
22833 An image map is an alist where each element has the format (AREA ID PLIST).
22834 An AREA is specified as either a rectangle, a circle, or a polygon:
22835 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22836 pixel coordinates of the upper left and bottom right corners.
22837 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22838 and the radius of the circle; r may be a float or integer.
22839 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22840 vector describes one corner in the polygon.
22841 Returns the alist element for the first matching AREA in MAP. */)
22842 (map, x, y)
22843 Lisp_Object map;
22844 Lisp_Object x, y;
22845 {
22846 if (NILP (map))
22847 return Qnil;
22848
22849 CHECK_NUMBER (x);
22850 CHECK_NUMBER (y);
22851
22852 return find_hot_spot (map, XINT (x), XINT (y));
22853 }
22854
22855
22856 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22857 static void
22858 define_frame_cursor1 (f, cursor, pointer)
22859 struct frame *f;
22860 Cursor cursor;
22861 Lisp_Object pointer;
22862 {
22863 /* Do not change cursor shape while dragging mouse. */
22864 if (!NILP (do_mouse_tracking))
22865 return;
22866
22867 if (!NILP (pointer))
22868 {
22869 if (EQ (pointer, Qarrow))
22870 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22871 else if (EQ (pointer, Qhand))
22872 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22873 else if (EQ (pointer, Qtext))
22874 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22875 else if (EQ (pointer, intern ("hdrag")))
22876 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22877 #ifdef HAVE_X_WINDOWS
22878 else if (EQ (pointer, intern ("vdrag")))
22879 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22880 #endif
22881 else if (EQ (pointer, intern ("hourglass")))
22882 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22883 else if (EQ (pointer, Qmodeline))
22884 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22885 else
22886 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22887 }
22888
22889 if (cursor != No_Cursor)
22890 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22891 }
22892
22893 /* Take proper action when mouse has moved to the mode or header line
22894 or marginal area AREA of window W, x-position X and y-position Y.
22895 X is relative to the start of the text display area of W, so the
22896 width of bitmap areas and scroll bars must be subtracted to get a
22897 position relative to the start of the mode line. */
22898
22899 static void
22900 note_mode_line_or_margin_highlight (window, x, y, area)
22901 Lisp_Object window;
22902 int x, y;
22903 enum window_part area;
22904 {
22905 struct window *w = XWINDOW (window);
22906 struct frame *f = XFRAME (w->frame);
22907 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22908 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22909 Lisp_Object pointer = Qnil;
22910 int charpos, dx, dy, width, height;
22911 Lisp_Object string, object = Qnil;
22912 Lisp_Object pos, help;
22913
22914 Lisp_Object mouse_face;
22915 int original_x_pixel = x;
22916 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22917 struct glyph_row *row;
22918
22919 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22920 {
22921 int x0;
22922 struct glyph *end;
22923
22924 string = mode_line_string (w, area, &x, &y, &charpos,
22925 &object, &dx, &dy, &width, &height);
22926
22927 row = (area == ON_MODE_LINE
22928 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22929 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22930
22931 /* Find glyph */
22932 if (row->mode_line_p && row->enabled_p)
22933 {
22934 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22935 end = glyph + row->used[TEXT_AREA];
22936
22937 for (x0 = original_x_pixel;
22938 glyph < end && x0 >= glyph->pixel_width;
22939 ++glyph)
22940 x0 -= glyph->pixel_width;
22941
22942 if (glyph >= end)
22943 glyph = NULL;
22944 }
22945 }
22946 else
22947 {
22948 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22949 string = marginal_area_string (w, area, &x, &y, &charpos,
22950 &object, &dx, &dy, &width, &height);
22951 }
22952
22953 help = Qnil;
22954
22955 if (IMAGEP (object))
22956 {
22957 Lisp_Object image_map, hotspot;
22958 if ((image_map = Fplist_get (XCDR (object), QCmap),
22959 !NILP (image_map))
22960 && (hotspot = find_hot_spot (image_map, dx, dy),
22961 CONSP (hotspot))
22962 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22963 {
22964 Lisp_Object area_id, plist;
22965
22966 area_id = XCAR (hotspot);
22967 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22968 If so, we could look for mouse-enter, mouse-leave
22969 properties in PLIST (and do something...). */
22970 hotspot = XCDR (hotspot);
22971 if (CONSP (hotspot)
22972 && (plist = XCAR (hotspot), CONSP (plist)))
22973 {
22974 pointer = Fplist_get (plist, Qpointer);
22975 if (NILP (pointer))
22976 pointer = Qhand;
22977 help = Fplist_get (plist, Qhelp_echo);
22978 if (!NILP (help))
22979 {
22980 help_echo_string = help;
22981 /* Is this correct? ++kfs */
22982 XSETWINDOW (help_echo_window, w);
22983 help_echo_object = w->buffer;
22984 help_echo_pos = charpos;
22985 }
22986 }
22987 }
22988 if (NILP (pointer))
22989 pointer = Fplist_get (XCDR (object), QCpointer);
22990 }
22991
22992 if (STRINGP (string))
22993 {
22994 pos = make_number (charpos);
22995 /* If we're on a string with `help-echo' text property, arrange
22996 for the help to be displayed. This is done by setting the
22997 global variable help_echo_string to the help string. */
22998 if (NILP (help))
22999 {
23000 help = Fget_text_property (pos, Qhelp_echo, string);
23001 if (!NILP (help))
23002 {
23003 help_echo_string = help;
23004 XSETWINDOW (help_echo_window, w);
23005 help_echo_object = string;
23006 help_echo_pos = charpos;
23007 }
23008 }
23009
23010 if (NILP (pointer))
23011 pointer = Fget_text_property (pos, Qpointer, string);
23012
23013 /* Change the mouse pointer according to what is under X/Y. */
23014 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23015 {
23016 Lisp_Object map;
23017 map = Fget_text_property (pos, Qlocal_map, string);
23018 if (!KEYMAPP (map))
23019 map = Fget_text_property (pos, Qkeymap, string);
23020 if (!KEYMAPP (map))
23021 cursor = dpyinfo->vertical_scroll_bar_cursor;
23022 }
23023
23024 /* Change the mouse face according to what is under X/Y. */
23025 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23026 if (!NILP (mouse_face)
23027 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23028 && glyph)
23029 {
23030 Lisp_Object b, e;
23031
23032 struct glyph * tmp_glyph;
23033
23034 int gpos;
23035 int gseq_length;
23036 int total_pixel_width;
23037 EMACS_INT ignore;
23038
23039 int vpos, hpos;
23040
23041 b = Fprevious_single_property_change (make_number (charpos + 1),
23042 Qmouse_face, string, Qnil);
23043 if (NILP (b))
23044 b = make_number (0);
23045
23046 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23047 if (NILP (e))
23048 e = make_number (SCHARS (string));
23049
23050 /* Calculate the position(glyph position: GPOS) of GLYPH in
23051 displayed string. GPOS is different from CHARPOS.
23052
23053 CHARPOS is the position of glyph in internal string
23054 object. A mode line string format has structures which
23055 is converted to a flatten by emacs lisp interpreter.
23056 The internal string is an element of the structures.
23057 The displayed string is the flatten string. */
23058 gpos = 0;
23059 if (glyph > row_start_glyph)
23060 {
23061 tmp_glyph = glyph - 1;
23062 while (tmp_glyph >= row_start_glyph
23063 && tmp_glyph->charpos >= XINT (b)
23064 && EQ (tmp_glyph->object, glyph->object))
23065 {
23066 tmp_glyph--;
23067 gpos++;
23068 }
23069 }
23070
23071 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23072 displayed string holding GLYPH.
23073
23074 GSEQ_LENGTH is different from SCHARS (STRING).
23075 SCHARS (STRING) returns the length of the internal string. */
23076 for (tmp_glyph = glyph, gseq_length = gpos;
23077 tmp_glyph->charpos < XINT (e);
23078 tmp_glyph++, gseq_length++)
23079 {
23080 if (!EQ (tmp_glyph->object, glyph->object))
23081 break;
23082 }
23083
23084 total_pixel_width = 0;
23085 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23086 total_pixel_width += tmp_glyph->pixel_width;
23087
23088 /* Pre calculation of re-rendering position */
23089 vpos = (x - gpos);
23090 hpos = (area == ON_MODE_LINE
23091 ? (w->current_matrix)->nrows - 1
23092 : 0);
23093
23094 /* If the re-rendering position is included in the last
23095 re-rendering area, we should do nothing. */
23096 if ( EQ (window, dpyinfo->mouse_face_window)
23097 && dpyinfo->mouse_face_beg_col <= vpos
23098 && vpos < dpyinfo->mouse_face_end_col
23099 && dpyinfo->mouse_face_beg_row == hpos )
23100 return;
23101
23102 if (clear_mouse_face (dpyinfo))
23103 cursor = No_Cursor;
23104
23105 dpyinfo->mouse_face_beg_col = vpos;
23106 dpyinfo->mouse_face_beg_row = hpos;
23107
23108 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23109 dpyinfo->mouse_face_beg_y = 0;
23110
23111 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23112 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23113
23114 dpyinfo->mouse_face_end_x = 0;
23115 dpyinfo->mouse_face_end_y = 0;
23116
23117 dpyinfo->mouse_face_past_end = 0;
23118 dpyinfo->mouse_face_window = window;
23119
23120 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23121 charpos,
23122 0, 0, 0, &ignore,
23123 glyph->face_id, 1);
23124 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23125
23126 if (NILP (pointer))
23127 pointer = Qhand;
23128 }
23129 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23130 clear_mouse_face (dpyinfo);
23131 }
23132 define_frame_cursor1 (f, cursor, pointer);
23133 }
23134
23135
23136 /* EXPORT:
23137 Take proper action when the mouse has moved to position X, Y on
23138 frame F as regards highlighting characters that have mouse-face
23139 properties. Also de-highlighting chars where the mouse was before.
23140 X and Y can be negative or out of range. */
23141
23142 void
23143 note_mouse_highlight (f, x, y)
23144 struct frame *f;
23145 int x, y;
23146 {
23147 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23148 enum window_part part;
23149 Lisp_Object window;
23150 struct window *w;
23151 Cursor cursor = No_Cursor;
23152 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23153 struct buffer *b;
23154
23155 /* When a menu is active, don't highlight because this looks odd. */
23156 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23157 if (popup_activated ())
23158 return;
23159 #endif
23160
23161 if (NILP (Vmouse_highlight)
23162 || !f->glyphs_initialized_p)
23163 return;
23164
23165 dpyinfo->mouse_face_mouse_x = x;
23166 dpyinfo->mouse_face_mouse_y = y;
23167 dpyinfo->mouse_face_mouse_frame = f;
23168
23169 if (dpyinfo->mouse_face_defer)
23170 return;
23171
23172 if (gc_in_progress)
23173 {
23174 dpyinfo->mouse_face_deferred_gc = 1;
23175 return;
23176 }
23177
23178 /* Which window is that in? */
23179 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23180
23181 /* If we were displaying active text in another window, clear that.
23182 Also clear if we move out of text area in same window. */
23183 if (! EQ (window, dpyinfo->mouse_face_window)
23184 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23185 && !NILP (dpyinfo->mouse_face_window)))
23186 clear_mouse_face (dpyinfo);
23187
23188 /* Not on a window -> return. */
23189 if (!WINDOWP (window))
23190 return;
23191
23192 /* Reset help_echo_string. It will get recomputed below. */
23193 help_echo_string = Qnil;
23194
23195 /* Convert to window-relative pixel coordinates. */
23196 w = XWINDOW (window);
23197 frame_to_window_pixel_xy (w, &x, &y);
23198
23199 /* Handle tool-bar window differently since it doesn't display a
23200 buffer. */
23201 if (EQ (window, f->tool_bar_window))
23202 {
23203 note_tool_bar_highlight (f, x, y);
23204 return;
23205 }
23206
23207 /* Mouse is on the mode, header line or margin? */
23208 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23209 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23210 {
23211 note_mode_line_or_margin_highlight (window, x, y, part);
23212 return;
23213 }
23214
23215 if (part == ON_VERTICAL_BORDER)
23216 {
23217 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23218 help_echo_string = build_string ("drag-mouse-1: resize");
23219 }
23220 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23221 || part == ON_SCROLL_BAR)
23222 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23223 else
23224 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23225
23226 /* Are we in a window whose display is up to date?
23227 And verify the buffer's text has not changed. */
23228 b = XBUFFER (w->buffer);
23229 if (part == ON_TEXT
23230 && EQ (w->window_end_valid, w->buffer)
23231 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23232 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23233 {
23234 int hpos, vpos, pos, i, dx, dy, area;
23235 struct glyph *glyph;
23236 Lisp_Object object;
23237 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23238 Lisp_Object *overlay_vec = NULL;
23239 int noverlays;
23240 struct buffer *obuf;
23241 int obegv, ozv, same_region;
23242
23243 /* Find the glyph under X/Y. */
23244 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23245
23246 /* Look for :pointer property on image. */
23247 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23248 {
23249 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23250 if (img != NULL && IMAGEP (img->spec))
23251 {
23252 Lisp_Object image_map, hotspot;
23253 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23254 !NILP (image_map))
23255 && (hotspot = find_hot_spot (image_map,
23256 glyph->slice.x + dx,
23257 glyph->slice.y + dy),
23258 CONSP (hotspot))
23259 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23260 {
23261 Lisp_Object area_id, plist;
23262
23263 area_id = XCAR (hotspot);
23264 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23265 If so, we could look for mouse-enter, mouse-leave
23266 properties in PLIST (and do something...). */
23267 hotspot = XCDR (hotspot);
23268 if (CONSP (hotspot)
23269 && (plist = XCAR (hotspot), CONSP (plist)))
23270 {
23271 pointer = Fplist_get (plist, Qpointer);
23272 if (NILP (pointer))
23273 pointer = Qhand;
23274 help_echo_string = Fplist_get (plist, Qhelp_echo);
23275 if (!NILP (help_echo_string))
23276 {
23277 help_echo_window = window;
23278 help_echo_object = glyph->object;
23279 help_echo_pos = glyph->charpos;
23280 }
23281 }
23282 }
23283 if (NILP (pointer))
23284 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23285 }
23286 }
23287
23288 /* Clear mouse face if X/Y not over text. */
23289 if (glyph == NULL
23290 || area != TEXT_AREA
23291 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23292 {
23293 if (clear_mouse_face (dpyinfo))
23294 cursor = No_Cursor;
23295 if (NILP (pointer))
23296 {
23297 if (area != TEXT_AREA)
23298 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23299 else
23300 pointer = Vvoid_text_area_pointer;
23301 }
23302 goto set_cursor;
23303 }
23304
23305 pos = glyph->charpos;
23306 object = glyph->object;
23307 if (!STRINGP (object) && !BUFFERP (object))
23308 goto set_cursor;
23309
23310 /* If we get an out-of-range value, return now; avoid an error. */
23311 if (BUFFERP (object) && pos > BUF_Z (b))
23312 goto set_cursor;
23313
23314 /* Make the window's buffer temporarily current for
23315 overlays_at and compute_char_face. */
23316 obuf = current_buffer;
23317 current_buffer = b;
23318 obegv = BEGV;
23319 ozv = ZV;
23320 BEGV = BEG;
23321 ZV = Z;
23322
23323 /* Is this char mouse-active or does it have help-echo? */
23324 position = make_number (pos);
23325
23326 if (BUFFERP (object))
23327 {
23328 /* Put all the overlays we want in a vector in overlay_vec. */
23329 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23330 /* Sort overlays into increasing priority order. */
23331 noverlays = sort_overlays (overlay_vec, noverlays, w);
23332 }
23333 else
23334 noverlays = 0;
23335
23336 same_region = (EQ (window, dpyinfo->mouse_face_window)
23337 && vpos >= dpyinfo->mouse_face_beg_row
23338 && vpos <= dpyinfo->mouse_face_end_row
23339 && (vpos > dpyinfo->mouse_face_beg_row
23340 || hpos >= dpyinfo->mouse_face_beg_col)
23341 && (vpos < dpyinfo->mouse_face_end_row
23342 || hpos < dpyinfo->mouse_face_end_col
23343 || dpyinfo->mouse_face_past_end));
23344
23345 if (same_region)
23346 cursor = No_Cursor;
23347
23348 /* Check mouse-face highlighting. */
23349 if (! same_region
23350 /* If there exists an overlay with mouse-face overlapping
23351 the one we are currently highlighting, we have to
23352 check if we enter the overlapping overlay, and then
23353 highlight only that. */
23354 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23355 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23356 {
23357 /* Find the highest priority overlay that has a mouse-face
23358 property. */
23359 overlay = Qnil;
23360 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23361 {
23362 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23363 if (!NILP (mouse_face))
23364 overlay = overlay_vec[i];
23365 }
23366
23367 /* If we're actually highlighting the same overlay as
23368 before, there's no need to do that again. */
23369 if (!NILP (overlay)
23370 && EQ (overlay, dpyinfo->mouse_face_overlay))
23371 goto check_help_echo;
23372
23373 dpyinfo->mouse_face_overlay = overlay;
23374
23375 /* Clear the display of the old active region, if any. */
23376 if (clear_mouse_face (dpyinfo))
23377 cursor = No_Cursor;
23378
23379 /* If no overlay applies, get a text property. */
23380 if (NILP (overlay))
23381 mouse_face = Fget_text_property (position, Qmouse_face, object);
23382
23383 /* Handle the overlay case. */
23384 if (!NILP (overlay))
23385 {
23386 /* Find the range of text around this char that
23387 should be active. */
23388 Lisp_Object before, after;
23389 EMACS_INT ignore;
23390
23391 before = Foverlay_start (overlay);
23392 after = Foverlay_end (overlay);
23393 /* Record this as the current active region. */
23394 fast_find_position (w, XFASTINT (before),
23395 &dpyinfo->mouse_face_beg_col,
23396 &dpyinfo->mouse_face_beg_row,
23397 &dpyinfo->mouse_face_beg_x,
23398 &dpyinfo->mouse_face_beg_y, Qnil);
23399
23400 dpyinfo->mouse_face_past_end
23401 = !fast_find_position (w, XFASTINT (after),
23402 &dpyinfo->mouse_face_end_col,
23403 &dpyinfo->mouse_face_end_row,
23404 &dpyinfo->mouse_face_end_x,
23405 &dpyinfo->mouse_face_end_y, Qnil);
23406 dpyinfo->mouse_face_window = window;
23407
23408 dpyinfo->mouse_face_face_id
23409 = face_at_buffer_position (w, pos, 0, 0,
23410 &ignore, pos + 1,
23411 !dpyinfo->mouse_face_hidden);
23412
23413 /* Display it as active. */
23414 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23415 cursor = No_Cursor;
23416 }
23417 /* Handle the text property case. */
23418 else if (!NILP (mouse_face) && BUFFERP (object))
23419 {
23420 /* Find the range of text around this char that
23421 should be active. */
23422 Lisp_Object before, after, beginning, end;
23423 EMACS_INT ignore;
23424
23425 beginning = Fmarker_position (w->start);
23426 end = make_number (BUF_Z (XBUFFER (object))
23427 - XFASTINT (w->window_end_pos));
23428 before
23429 = Fprevious_single_property_change (make_number (pos + 1),
23430 Qmouse_face,
23431 object, beginning);
23432 after
23433 = Fnext_single_property_change (position, Qmouse_face,
23434 object, end);
23435
23436 /* Record this as the current active region. */
23437 fast_find_position (w, XFASTINT (before),
23438 &dpyinfo->mouse_face_beg_col,
23439 &dpyinfo->mouse_face_beg_row,
23440 &dpyinfo->mouse_face_beg_x,
23441 &dpyinfo->mouse_face_beg_y, Qnil);
23442 dpyinfo->mouse_face_past_end
23443 = !fast_find_position (w, XFASTINT (after),
23444 &dpyinfo->mouse_face_end_col,
23445 &dpyinfo->mouse_face_end_row,
23446 &dpyinfo->mouse_face_end_x,
23447 &dpyinfo->mouse_face_end_y, Qnil);
23448 dpyinfo->mouse_face_window = window;
23449
23450 if (BUFFERP (object))
23451 dpyinfo->mouse_face_face_id
23452 = face_at_buffer_position (w, pos, 0, 0,
23453 &ignore, pos + 1,
23454 !dpyinfo->mouse_face_hidden);
23455
23456 /* Display it as active. */
23457 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23458 cursor = No_Cursor;
23459 }
23460 else if (!NILP (mouse_face) && STRINGP (object))
23461 {
23462 Lisp_Object b, e;
23463 EMACS_INT ignore;
23464
23465 b = Fprevious_single_property_change (make_number (pos + 1),
23466 Qmouse_face,
23467 object, Qnil);
23468 e = Fnext_single_property_change (position, Qmouse_face,
23469 object, Qnil);
23470 if (NILP (b))
23471 b = make_number (0);
23472 if (NILP (e))
23473 e = make_number (SCHARS (object) - 1);
23474
23475 fast_find_string_pos (w, XINT (b), object,
23476 &dpyinfo->mouse_face_beg_col,
23477 &dpyinfo->mouse_face_beg_row,
23478 &dpyinfo->mouse_face_beg_x,
23479 &dpyinfo->mouse_face_beg_y, 0);
23480 fast_find_string_pos (w, XINT (e), object,
23481 &dpyinfo->mouse_face_end_col,
23482 &dpyinfo->mouse_face_end_row,
23483 &dpyinfo->mouse_face_end_x,
23484 &dpyinfo->mouse_face_end_y, 1);
23485 dpyinfo->mouse_face_past_end = 0;
23486 dpyinfo->mouse_face_window = window;
23487 dpyinfo->mouse_face_face_id
23488 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23489 glyph->face_id, 1);
23490 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23491 cursor = No_Cursor;
23492 }
23493 else if (STRINGP (object) && NILP (mouse_face))
23494 {
23495 /* A string which doesn't have mouse-face, but
23496 the text ``under'' it might have. */
23497 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23498 int start = MATRIX_ROW_START_CHARPOS (r);
23499
23500 pos = string_buffer_position (w, object, start);
23501 if (pos > 0)
23502 mouse_face = get_char_property_and_overlay (make_number (pos),
23503 Qmouse_face,
23504 w->buffer,
23505 &overlay);
23506 if (!NILP (mouse_face) && !NILP (overlay))
23507 {
23508 Lisp_Object before = Foverlay_start (overlay);
23509 Lisp_Object after = Foverlay_end (overlay);
23510 EMACS_INT ignore;
23511
23512 /* Note that we might not be able to find position
23513 BEFORE in the glyph matrix if the overlay is
23514 entirely covered by a `display' property. In
23515 this case, we overshoot. So let's stop in
23516 the glyph matrix before glyphs for OBJECT. */
23517 fast_find_position (w, XFASTINT (before),
23518 &dpyinfo->mouse_face_beg_col,
23519 &dpyinfo->mouse_face_beg_row,
23520 &dpyinfo->mouse_face_beg_x,
23521 &dpyinfo->mouse_face_beg_y,
23522 object);
23523
23524 dpyinfo->mouse_face_past_end
23525 = !fast_find_position (w, XFASTINT (after),
23526 &dpyinfo->mouse_face_end_col,
23527 &dpyinfo->mouse_face_end_row,
23528 &dpyinfo->mouse_face_end_x,
23529 &dpyinfo->mouse_face_end_y,
23530 Qnil);
23531 dpyinfo->mouse_face_window = window;
23532 dpyinfo->mouse_face_face_id
23533 = face_at_buffer_position (w, pos, 0, 0,
23534 &ignore, pos + 1,
23535 !dpyinfo->mouse_face_hidden);
23536
23537 /* Display it as active. */
23538 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23539 cursor = No_Cursor;
23540 }
23541 }
23542 }
23543
23544 check_help_echo:
23545
23546 /* Look for a `help-echo' property. */
23547 if (NILP (help_echo_string)) {
23548 Lisp_Object help, overlay;
23549
23550 /* Check overlays first. */
23551 help = overlay = Qnil;
23552 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23553 {
23554 overlay = overlay_vec[i];
23555 help = Foverlay_get (overlay, Qhelp_echo);
23556 }
23557
23558 if (!NILP (help))
23559 {
23560 help_echo_string = help;
23561 help_echo_window = window;
23562 help_echo_object = overlay;
23563 help_echo_pos = pos;
23564 }
23565 else
23566 {
23567 Lisp_Object object = glyph->object;
23568 int charpos = glyph->charpos;
23569
23570 /* Try text properties. */
23571 if (STRINGP (object)
23572 && charpos >= 0
23573 && charpos < SCHARS (object))
23574 {
23575 help = Fget_text_property (make_number (charpos),
23576 Qhelp_echo, object);
23577 if (NILP (help))
23578 {
23579 /* If the string itself doesn't specify a help-echo,
23580 see if the buffer text ``under'' it does. */
23581 struct glyph_row *r
23582 = MATRIX_ROW (w->current_matrix, vpos);
23583 int start = MATRIX_ROW_START_CHARPOS (r);
23584 int pos = string_buffer_position (w, object, start);
23585 if (pos > 0)
23586 {
23587 help = Fget_char_property (make_number (pos),
23588 Qhelp_echo, w->buffer);
23589 if (!NILP (help))
23590 {
23591 charpos = pos;
23592 object = w->buffer;
23593 }
23594 }
23595 }
23596 }
23597 else if (BUFFERP (object)
23598 && charpos >= BEGV
23599 && charpos < ZV)
23600 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23601 object);
23602
23603 if (!NILP (help))
23604 {
23605 help_echo_string = help;
23606 help_echo_window = window;
23607 help_echo_object = object;
23608 help_echo_pos = charpos;
23609 }
23610 }
23611 }
23612
23613 /* Look for a `pointer' property. */
23614 if (NILP (pointer))
23615 {
23616 /* Check overlays first. */
23617 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23618 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23619
23620 if (NILP (pointer))
23621 {
23622 Lisp_Object object = glyph->object;
23623 int charpos = glyph->charpos;
23624
23625 /* Try text properties. */
23626 if (STRINGP (object)
23627 && charpos >= 0
23628 && charpos < SCHARS (object))
23629 {
23630 pointer = Fget_text_property (make_number (charpos),
23631 Qpointer, object);
23632 if (NILP (pointer))
23633 {
23634 /* If the string itself doesn't specify a pointer,
23635 see if the buffer text ``under'' it does. */
23636 struct glyph_row *r
23637 = MATRIX_ROW (w->current_matrix, vpos);
23638 int start = MATRIX_ROW_START_CHARPOS (r);
23639 int pos = string_buffer_position (w, object, start);
23640 if (pos > 0)
23641 pointer = Fget_char_property (make_number (pos),
23642 Qpointer, w->buffer);
23643 }
23644 }
23645 else if (BUFFERP (object)
23646 && charpos >= BEGV
23647 && charpos < ZV)
23648 pointer = Fget_text_property (make_number (charpos),
23649 Qpointer, object);
23650 }
23651 }
23652
23653 BEGV = obegv;
23654 ZV = ozv;
23655 current_buffer = obuf;
23656 }
23657
23658 set_cursor:
23659
23660 define_frame_cursor1 (f, cursor, pointer);
23661 }
23662
23663
23664 /* EXPORT for RIF:
23665 Clear any mouse-face on window W. This function is part of the
23666 redisplay interface, and is called from try_window_id and similar
23667 functions to ensure the mouse-highlight is off. */
23668
23669 void
23670 x_clear_window_mouse_face (w)
23671 struct window *w;
23672 {
23673 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23674 Lisp_Object window;
23675
23676 BLOCK_INPUT;
23677 XSETWINDOW (window, w);
23678 if (EQ (window, dpyinfo->mouse_face_window))
23679 clear_mouse_face (dpyinfo);
23680 UNBLOCK_INPUT;
23681 }
23682
23683
23684 /* EXPORT:
23685 Just discard the mouse face information for frame F, if any.
23686 This is used when the size of F is changed. */
23687
23688 void
23689 cancel_mouse_face (f)
23690 struct frame *f;
23691 {
23692 Lisp_Object window;
23693 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23694
23695 window = dpyinfo->mouse_face_window;
23696 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23697 {
23698 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23699 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23700 dpyinfo->mouse_face_window = Qnil;
23701 }
23702 }
23703
23704
23705 #endif /* HAVE_WINDOW_SYSTEM */
23706
23707 \f
23708 /***********************************************************************
23709 Exposure Events
23710 ***********************************************************************/
23711
23712 #ifdef HAVE_WINDOW_SYSTEM
23713
23714 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23715 which intersects rectangle R. R is in window-relative coordinates. */
23716
23717 static void
23718 expose_area (w, row, r, area)
23719 struct window *w;
23720 struct glyph_row *row;
23721 XRectangle *r;
23722 enum glyph_row_area area;
23723 {
23724 struct glyph *first = row->glyphs[area];
23725 struct glyph *end = row->glyphs[area] + row->used[area];
23726 struct glyph *last;
23727 int first_x, start_x, x;
23728
23729 if (area == TEXT_AREA && row->fill_line_p)
23730 /* If row extends face to end of line write the whole line. */
23731 draw_glyphs (w, 0, row, area,
23732 0, row->used[area],
23733 DRAW_NORMAL_TEXT, 0);
23734 else
23735 {
23736 /* Set START_X to the window-relative start position for drawing glyphs of
23737 AREA. The first glyph of the text area can be partially visible.
23738 The first glyphs of other areas cannot. */
23739 start_x = window_box_left_offset (w, area);
23740 x = start_x;
23741 if (area == TEXT_AREA)
23742 x += row->x;
23743
23744 /* Find the first glyph that must be redrawn. */
23745 while (first < end
23746 && x + first->pixel_width < r->x)
23747 {
23748 x += first->pixel_width;
23749 ++first;
23750 }
23751
23752 /* Find the last one. */
23753 last = first;
23754 first_x = x;
23755 while (last < end
23756 && x < r->x + r->width)
23757 {
23758 x += last->pixel_width;
23759 ++last;
23760 }
23761
23762 /* Repaint. */
23763 if (last > first)
23764 draw_glyphs (w, first_x - start_x, row, area,
23765 first - row->glyphs[area], last - row->glyphs[area],
23766 DRAW_NORMAL_TEXT, 0);
23767 }
23768 }
23769
23770
23771 /* Redraw the parts of the glyph row ROW on window W intersecting
23772 rectangle R. R is in window-relative coordinates. Value is
23773 non-zero if mouse-face was overwritten. */
23774
23775 static int
23776 expose_line (w, row, r)
23777 struct window *w;
23778 struct glyph_row *row;
23779 XRectangle *r;
23780 {
23781 xassert (row->enabled_p);
23782
23783 if (row->mode_line_p || w->pseudo_window_p)
23784 draw_glyphs (w, 0, row, TEXT_AREA,
23785 0, row->used[TEXT_AREA],
23786 DRAW_NORMAL_TEXT, 0);
23787 else
23788 {
23789 if (row->used[LEFT_MARGIN_AREA])
23790 expose_area (w, row, r, LEFT_MARGIN_AREA);
23791 if (row->used[TEXT_AREA])
23792 expose_area (w, row, r, TEXT_AREA);
23793 if (row->used[RIGHT_MARGIN_AREA])
23794 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23795 draw_row_fringe_bitmaps (w, row);
23796 }
23797
23798 return row->mouse_face_p;
23799 }
23800
23801
23802 /* Redraw those parts of glyphs rows during expose event handling that
23803 overlap other rows. Redrawing of an exposed line writes over parts
23804 of lines overlapping that exposed line; this function fixes that.
23805
23806 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23807 row in W's current matrix that is exposed and overlaps other rows.
23808 LAST_OVERLAPPING_ROW is the last such row. */
23809
23810 static void
23811 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23812 struct window *w;
23813 struct glyph_row *first_overlapping_row;
23814 struct glyph_row *last_overlapping_row;
23815 XRectangle *r;
23816 {
23817 struct glyph_row *row;
23818
23819 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23820 if (row->overlapping_p)
23821 {
23822 xassert (row->enabled_p && !row->mode_line_p);
23823
23824 row->clip = r;
23825 if (row->used[LEFT_MARGIN_AREA])
23826 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23827
23828 if (row->used[TEXT_AREA])
23829 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23830
23831 if (row->used[RIGHT_MARGIN_AREA])
23832 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23833 row->clip = NULL;
23834 }
23835 }
23836
23837
23838 /* Return non-zero if W's cursor intersects rectangle R. */
23839
23840 static int
23841 phys_cursor_in_rect_p (w, r)
23842 struct window *w;
23843 XRectangle *r;
23844 {
23845 XRectangle cr, result;
23846 struct glyph *cursor_glyph;
23847 struct glyph_row *row;
23848
23849 if (w->phys_cursor.vpos >= 0
23850 && w->phys_cursor.vpos < w->current_matrix->nrows
23851 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23852 row->enabled_p)
23853 && row->cursor_in_fringe_p)
23854 {
23855 /* Cursor is in the fringe. */
23856 cr.x = window_box_right_offset (w,
23857 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23858 ? RIGHT_MARGIN_AREA
23859 : TEXT_AREA));
23860 cr.y = row->y;
23861 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23862 cr.height = row->height;
23863 return x_intersect_rectangles (&cr, r, &result);
23864 }
23865
23866 cursor_glyph = get_phys_cursor_glyph (w);
23867 if (cursor_glyph)
23868 {
23869 /* r is relative to W's box, but w->phys_cursor.x is relative
23870 to left edge of W's TEXT area. Adjust it. */
23871 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23872 cr.y = w->phys_cursor.y;
23873 cr.width = cursor_glyph->pixel_width;
23874 cr.height = w->phys_cursor_height;
23875 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23876 I assume the effect is the same -- and this is portable. */
23877 return x_intersect_rectangles (&cr, r, &result);
23878 }
23879 /* If we don't understand the format, pretend we're not in the hot-spot. */
23880 return 0;
23881 }
23882
23883
23884 /* EXPORT:
23885 Draw a vertical window border to the right of window W if W doesn't
23886 have vertical scroll bars. */
23887
23888 void
23889 x_draw_vertical_border (w)
23890 struct window *w;
23891 {
23892 struct frame *f = XFRAME (WINDOW_FRAME (w));
23893
23894 /* We could do better, if we knew what type of scroll-bar the adjacent
23895 windows (on either side) have... But we don't :-(
23896 However, I think this works ok. ++KFS 2003-04-25 */
23897
23898 /* Redraw borders between horizontally adjacent windows. Don't
23899 do it for frames with vertical scroll bars because either the
23900 right scroll bar of a window, or the left scroll bar of its
23901 neighbor will suffice as a border. */
23902 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23903 return;
23904
23905 if (!WINDOW_RIGHTMOST_P (w)
23906 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23907 {
23908 int x0, x1, y0, y1;
23909
23910 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23911 y1 -= 1;
23912
23913 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23914 x1 -= 1;
23915
23916 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23917 }
23918 else if (!WINDOW_LEFTMOST_P (w)
23919 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23920 {
23921 int x0, x1, y0, y1;
23922
23923 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23924 y1 -= 1;
23925
23926 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23927 x0 -= 1;
23928
23929 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23930 }
23931 }
23932
23933
23934 /* Redraw the part of window W intersection rectangle FR. Pixel
23935 coordinates in FR are frame-relative. Call this function with
23936 input blocked. Value is non-zero if the exposure overwrites
23937 mouse-face. */
23938
23939 static int
23940 expose_window (w, fr)
23941 struct window *w;
23942 XRectangle *fr;
23943 {
23944 struct frame *f = XFRAME (w->frame);
23945 XRectangle wr, r;
23946 int mouse_face_overwritten_p = 0;
23947
23948 /* If window is not yet fully initialized, do nothing. This can
23949 happen when toolkit scroll bars are used and a window is split.
23950 Reconfiguring the scroll bar will generate an expose for a newly
23951 created window. */
23952 if (w->current_matrix == NULL)
23953 return 0;
23954
23955 /* When we're currently updating the window, display and current
23956 matrix usually don't agree. Arrange for a thorough display
23957 later. */
23958 if (w == updated_window)
23959 {
23960 SET_FRAME_GARBAGED (f);
23961 return 0;
23962 }
23963
23964 /* Frame-relative pixel rectangle of W. */
23965 wr.x = WINDOW_LEFT_EDGE_X (w);
23966 wr.y = WINDOW_TOP_EDGE_Y (w);
23967 wr.width = WINDOW_TOTAL_WIDTH (w);
23968 wr.height = WINDOW_TOTAL_HEIGHT (w);
23969
23970 if (x_intersect_rectangles (fr, &wr, &r))
23971 {
23972 int yb = window_text_bottom_y (w);
23973 struct glyph_row *row;
23974 int cursor_cleared_p;
23975 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23976
23977 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23978 r.x, r.y, r.width, r.height));
23979
23980 /* Convert to window coordinates. */
23981 r.x -= WINDOW_LEFT_EDGE_X (w);
23982 r.y -= WINDOW_TOP_EDGE_Y (w);
23983
23984 /* Turn off the cursor. */
23985 if (!w->pseudo_window_p
23986 && phys_cursor_in_rect_p (w, &r))
23987 {
23988 x_clear_cursor (w);
23989 cursor_cleared_p = 1;
23990 }
23991 else
23992 cursor_cleared_p = 0;
23993
23994 /* Update lines intersecting rectangle R. */
23995 first_overlapping_row = last_overlapping_row = NULL;
23996 for (row = w->current_matrix->rows;
23997 row->enabled_p;
23998 ++row)
23999 {
24000 int y0 = row->y;
24001 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24002
24003 if ((y0 >= r.y && y0 < r.y + r.height)
24004 || (y1 > r.y && y1 < r.y + r.height)
24005 || (r.y >= y0 && r.y < y1)
24006 || (r.y + r.height > y0 && r.y + r.height < y1))
24007 {
24008 /* A header line may be overlapping, but there is no need
24009 to fix overlapping areas for them. KFS 2005-02-12 */
24010 if (row->overlapping_p && !row->mode_line_p)
24011 {
24012 if (first_overlapping_row == NULL)
24013 first_overlapping_row = row;
24014 last_overlapping_row = row;
24015 }
24016
24017 row->clip = fr;
24018 if (expose_line (w, row, &r))
24019 mouse_face_overwritten_p = 1;
24020 row->clip = NULL;
24021 }
24022 else if (row->overlapping_p)
24023 {
24024 /* We must redraw a row overlapping the exposed area. */
24025 if (y0 < r.y
24026 ? y0 + row->phys_height > r.y
24027 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24028 {
24029 if (first_overlapping_row == NULL)
24030 first_overlapping_row = row;
24031 last_overlapping_row = row;
24032 }
24033 }
24034
24035 if (y1 >= yb)
24036 break;
24037 }
24038
24039 /* Display the mode line if there is one. */
24040 if (WINDOW_WANTS_MODELINE_P (w)
24041 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24042 row->enabled_p)
24043 && row->y < r.y + r.height)
24044 {
24045 if (expose_line (w, row, &r))
24046 mouse_face_overwritten_p = 1;
24047 }
24048
24049 if (!w->pseudo_window_p)
24050 {
24051 /* Fix the display of overlapping rows. */
24052 if (first_overlapping_row)
24053 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24054 fr);
24055
24056 /* Draw border between windows. */
24057 x_draw_vertical_border (w);
24058
24059 /* Turn the cursor on again. */
24060 if (cursor_cleared_p)
24061 update_window_cursor (w, 1);
24062 }
24063 }
24064
24065 return mouse_face_overwritten_p;
24066 }
24067
24068
24069
24070 /* Redraw (parts) of all windows in the window tree rooted at W that
24071 intersect R. R contains frame pixel coordinates. Value is
24072 non-zero if the exposure overwrites mouse-face. */
24073
24074 static int
24075 expose_window_tree (w, r)
24076 struct window *w;
24077 XRectangle *r;
24078 {
24079 struct frame *f = XFRAME (w->frame);
24080 int mouse_face_overwritten_p = 0;
24081
24082 while (w && !FRAME_GARBAGED_P (f))
24083 {
24084 if (!NILP (w->hchild))
24085 mouse_face_overwritten_p
24086 |= expose_window_tree (XWINDOW (w->hchild), r);
24087 else if (!NILP (w->vchild))
24088 mouse_face_overwritten_p
24089 |= expose_window_tree (XWINDOW (w->vchild), r);
24090 else
24091 mouse_face_overwritten_p |= expose_window (w, r);
24092
24093 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24094 }
24095
24096 return mouse_face_overwritten_p;
24097 }
24098
24099
24100 /* EXPORT:
24101 Redisplay an exposed area of frame F. X and Y are the upper-left
24102 corner of the exposed rectangle. W and H are width and height of
24103 the exposed area. All are pixel values. W or H zero means redraw
24104 the entire frame. */
24105
24106 void
24107 expose_frame (f, x, y, w, h)
24108 struct frame *f;
24109 int x, y, w, h;
24110 {
24111 XRectangle r;
24112 int mouse_face_overwritten_p = 0;
24113
24114 TRACE ((stderr, "expose_frame "));
24115
24116 /* No need to redraw if frame will be redrawn soon. */
24117 if (FRAME_GARBAGED_P (f))
24118 {
24119 TRACE ((stderr, " garbaged\n"));
24120 return;
24121 }
24122
24123 /* If basic faces haven't been realized yet, there is no point in
24124 trying to redraw anything. This can happen when we get an expose
24125 event while Emacs is starting, e.g. by moving another window. */
24126 if (FRAME_FACE_CACHE (f) == NULL
24127 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24128 {
24129 TRACE ((stderr, " no faces\n"));
24130 return;
24131 }
24132
24133 if (w == 0 || h == 0)
24134 {
24135 r.x = r.y = 0;
24136 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24137 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24138 }
24139 else
24140 {
24141 r.x = x;
24142 r.y = y;
24143 r.width = w;
24144 r.height = h;
24145 }
24146
24147 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24148 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24149
24150 if (WINDOWP (f->tool_bar_window))
24151 mouse_face_overwritten_p
24152 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24153
24154 #ifdef HAVE_X_WINDOWS
24155 #ifndef MSDOS
24156 #ifndef USE_X_TOOLKIT
24157 if (WINDOWP (f->menu_bar_window))
24158 mouse_face_overwritten_p
24159 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24160 #endif /* not USE_X_TOOLKIT */
24161 #endif
24162 #endif
24163
24164 /* Some window managers support a focus-follows-mouse style with
24165 delayed raising of frames. Imagine a partially obscured frame,
24166 and moving the mouse into partially obscured mouse-face on that
24167 frame. The visible part of the mouse-face will be highlighted,
24168 then the WM raises the obscured frame. With at least one WM, KDE
24169 2.1, Emacs is not getting any event for the raising of the frame
24170 (even tried with SubstructureRedirectMask), only Expose events.
24171 These expose events will draw text normally, i.e. not
24172 highlighted. Which means we must redo the highlight here.
24173 Subsume it under ``we love X''. --gerd 2001-08-15 */
24174 /* Included in Windows version because Windows most likely does not
24175 do the right thing if any third party tool offers
24176 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24177 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24178 {
24179 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24180 if (f == dpyinfo->mouse_face_mouse_frame)
24181 {
24182 int x = dpyinfo->mouse_face_mouse_x;
24183 int y = dpyinfo->mouse_face_mouse_y;
24184 clear_mouse_face (dpyinfo);
24185 note_mouse_highlight (f, x, y);
24186 }
24187 }
24188 }
24189
24190
24191 /* EXPORT:
24192 Determine the intersection of two rectangles R1 and R2. Return
24193 the intersection in *RESULT. Value is non-zero if RESULT is not
24194 empty. */
24195
24196 int
24197 x_intersect_rectangles (r1, r2, result)
24198 XRectangle *r1, *r2, *result;
24199 {
24200 XRectangle *left, *right;
24201 XRectangle *upper, *lower;
24202 int intersection_p = 0;
24203
24204 /* Rearrange so that R1 is the left-most rectangle. */
24205 if (r1->x < r2->x)
24206 left = r1, right = r2;
24207 else
24208 left = r2, right = r1;
24209
24210 /* X0 of the intersection is right.x0, if this is inside R1,
24211 otherwise there is no intersection. */
24212 if (right->x <= left->x + left->width)
24213 {
24214 result->x = right->x;
24215
24216 /* The right end of the intersection is the minimum of the
24217 the right ends of left and right. */
24218 result->width = (min (left->x + left->width, right->x + right->width)
24219 - result->x);
24220
24221 /* Same game for Y. */
24222 if (r1->y < r2->y)
24223 upper = r1, lower = r2;
24224 else
24225 upper = r2, lower = r1;
24226
24227 /* The upper end of the intersection is lower.y0, if this is inside
24228 of upper. Otherwise, there is no intersection. */
24229 if (lower->y <= upper->y + upper->height)
24230 {
24231 result->y = lower->y;
24232
24233 /* The lower end of the intersection is the minimum of the lower
24234 ends of upper and lower. */
24235 result->height = (min (lower->y + lower->height,
24236 upper->y + upper->height)
24237 - result->y);
24238 intersection_p = 1;
24239 }
24240 }
24241
24242 return intersection_p;
24243 }
24244
24245 #endif /* HAVE_WINDOW_SYSTEM */
24246
24247 \f
24248 /***********************************************************************
24249 Initialization
24250 ***********************************************************************/
24251
24252 void
24253 syms_of_xdisp ()
24254 {
24255 Vwith_echo_area_save_vector = Qnil;
24256 staticpro (&Vwith_echo_area_save_vector);
24257
24258 Vmessage_stack = Qnil;
24259 staticpro (&Vmessage_stack);
24260
24261 Qinhibit_redisplay = intern ("inhibit-redisplay");
24262 staticpro (&Qinhibit_redisplay);
24263
24264 message_dolog_marker1 = Fmake_marker ();
24265 staticpro (&message_dolog_marker1);
24266 message_dolog_marker2 = Fmake_marker ();
24267 staticpro (&message_dolog_marker2);
24268 message_dolog_marker3 = Fmake_marker ();
24269 staticpro (&message_dolog_marker3);
24270
24271 #if GLYPH_DEBUG
24272 defsubr (&Sdump_frame_glyph_matrix);
24273 defsubr (&Sdump_glyph_matrix);
24274 defsubr (&Sdump_glyph_row);
24275 defsubr (&Sdump_tool_bar_row);
24276 defsubr (&Strace_redisplay);
24277 defsubr (&Strace_to_stderr);
24278 #endif
24279 #ifdef HAVE_WINDOW_SYSTEM
24280 defsubr (&Stool_bar_lines_needed);
24281 defsubr (&Slookup_image_map);
24282 #endif
24283 defsubr (&Sformat_mode_line);
24284 defsubr (&Sinvisible_p);
24285
24286 staticpro (&Qmenu_bar_update_hook);
24287 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24288
24289 staticpro (&Qoverriding_terminal_local_map);
24290 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24291
24292 staticpro (&Qoverriding_local_map);
24293 Qoverriding_local_map = intern ("overriding-local-map");
24294
24295 staticpro (&Qwindow_scroll_functions);
24296 Qwindow_scroll_functions = intern ("window-scroll-functions");
24297
24298 staticpro (&Qwindow_text_change_functions);
24299 Qwindow_text_change_functions = intern ("window-text-change-functions");
24300
24301 staticpro (&Qredisplay_end_trigger_functions);
24302 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24303
24304 staticpro (&Qinhibit_point_motion_hooks);
24305 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24306
24307 Qeval = intern ("eval");
24308 staticpro (&Qeval);
24309
24310 QCdata = intern (":data");
24311 staticpro (&QCdata);
24312 Qdisplay = intern ("display");
24313 staticpro (&Qdisplay);
24314 Qspace_width = intern ("space-width");
24315 staticpro (&Qspace_width);
24316 Qraise = intern ("raise");
24317 staticpro (&Qraise);
24318 Qslice = intern ("slice");
24319 staticpro (&Qslice);
24320 Qspace = intern ("space");
24321 staticpro (&Qspace);
24322 Qmargin = intern ("margin");
24323 staticpro (&Qmargin);
24324 Qpointer = intern ("pointer");
24325 staticpro (&Qpointer);
24326 Qleft_margin = intern ("left-margin");
24327 staticpro (&Qleft_margin);
24328 Qright_margin = intern ("right-margin");
24329 staticpro (&Qright_margin);
24330 Qcenter = intern ("center");
24331 staticpro (&Qcenter);
24332 Qline_height = intern ("line-height");
24333 staticpro (&Qline_height);
24334 QCalign_to = intern (":align-to");
24335 staticpro (&QCalign_to);
24336 QCrelative_width = intern (":relative-width");
24337 staticpro (&QCrelative_width);
24338 QCrelative_height = intern (":relative-height");
24339 staticpro (&QCrelative_height);
24340 QCeval = intern (":eval");
24341 staticpro (&QCeval);
24342 QCpropertize = intern (":propertize");
24343 staticpro (&QCpropertize);
24344 QCfile = intern (":file");
24345 staticpro (&QCfile);
24346 Qfontified = intern ("fontified");
24347 staticpro (&Qfontified);
24348 Qfontification_functions = intern ("fontification-functions");
24349 staticpro (&Qfontification_functions);
24350 Qtrailing_whitespace = intern ("trailing-whitespace");
24351 staticpro (&Qtrailing_whitespace);
24352 Qescape_glyph = intern ("escape-glyph");
24353 staticpro (&Qescape_glyph);
24354 Qnobreak_space = intern ("nobreak-space");
24355 staticpro (&Qnobreak_space);
24356 Qimage = intern ("image");
24357 staticpro (&Qimage);
24358 QCmap = intern (":map");
24359 staticpro (&QCmap);
24360 QCpointer = intern (":pointer");
24361 staticpro (&QCpointer);
24362 Qrect = intern ("rect");
24363 staticpro (&Qrect);
24364 Qcircle = intern ("circle");
24365 staticpro (&Qcircle);
24366 Qpoly = intern ("poly");
24367 staticpro (&Qpoly);
24368 Qmessage_truncate_lines = intern ("message-truncate-lines");
24369 staticpro (&Qmessage_truncate_lines);
24370 Qgrow_only = intern ("grow-only");
24371 staticpro (&Qgrow_only);
24372 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24373 staticpro (&Qinhibit_menubar_update);
24374 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24375 staticpro (&Qinhibit_eval_during_redisplay);
24376 Qposition = intern ("position");
24377 staticpro (&Qposition);
24378 Qbuffer_position = intern ("buffer-position");
24379 staticpro (&Qbuffer_position);
24380 Qobject = intern ("object");
24381 staticpro (&Qobject);
24382 Qbar = intern ("bar");
24383 staticpro (&Qbar);
24384 Qhbar = intern ("hbar");
24385 staticpro (&Qhbar);
24386 Qbox = intern ("box");
24387 staticpro (&Qbox);
24388 Qhollow = intern ("hollow");
24389 staticpro (&Qhollow);
24390 Qhand = intern ("hand");
24391 staticpro (&Qhand);
24392 Qarrow = intern ("arrow");
24393 staticpro (&Qarrow);
24394 Qtext = intern ("text");
24395 staticpro (&Qtext);
24396 Qrisky_local_variable = intern ("risky-local-variable");
24397 staticpro (&Qrisky_local_variable);
24398 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24399 staticpro (&Qinhibit_free_realized_faces);
24400
24401 list_of_error = Fcons (Fcons (intern ("error"),
24402 Fcons (intern ("void-variable"), Qnil)),
24403 Qnil);
24404 staticpro (&list_of_error);
24405
24406 Qlast_arrow_position = intern ("last-arrow-position");
24407 staticpro (&Qlast_arrow_position);
24408 Qlast_arrow_string = intern ("last-arrow-string");
24409 staticpro (&Qlast_arrow_string);
24410
24411 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24412 staticpro (&Qoverlay_arrow_string);
24413 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24414 staticpro (&Qoverlay_arrow_bitmap);
24415
24416 echo_buffer[0] = echo_buffer[1] = Qnil;
24417 staticpro (&echo_buffer[0]);
24418 staticpro (&echo_buffer[1]);
24419
24420 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24421 staticpro (&echo_area_buffer[0]);
24422 staticpro (&echo_area_buffer[1]);
24423
24424 Vmessages_buffer_name = build_string ("*Messages*");
24425 staticpro (&Vmessages_buffer_name);
24426
24427 mode_line_proptrans_alist = Qnil;
24428 staticpro (&mode_line_proptrans_alist);
24429 mode_line_string_list = Qnil;
24430 staticpro (&mode_line_string_list);
24431 mode_line_string_face = Qnil;
24432 staticpro (&mode_line_string_face);
24433 mode_line_string_face_prop = Qnil;
24434 staticpro (&mode_line_string_face_prop);
24435 Vmode_line_unwind_vector = Qnil;
24436 staticpro (&Vmode_line_unwind_vector);
24437
24438 help_echo_string = Qnil;
24439 staticpro (&help_echo_string);
24440 help_echo_object = Qnil;
24441 staticpro (&help_echo_object);
24442 help_echo_window = Qnil;
24443 staticpro (&help_echo_window);
24444 previous_help_echo_string = Qnil;
24445 staticpro (&previous_help_echo_string);
24446 help_echo_pos = -1;
24447
24448 #ifdef HAVE_WINDOW_SYSTEM
24449 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24450 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24451 For example, if a block cursor is over a tab, it will be drawn as
24452 wide as that tab on the display. */);
24453 x_stretch_cursor_p = 0;
24454 #endif
24455
24456 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24457 doc: /* *Non-nil means highlight trailing whitespace.
24458 The face used for trailing whitespace is `trailing-whitespace'. */);
24459 Vshow_trailing_whitespace = Qnil;
24460
24461 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24462 doc: /* *Control highlighting of nobreak space and soft hyphen.
24463 A value of t means highlight the character itself (for nobreak space,
24464 use face `nobreak-space').
24465 A value of nil means no highlighting.
24466 Other values mean display the escape glyph followed by an ordinary
24467 space or ordinary hyphen. */);
24468 Vnobreak_char_display = Qt;
24469
24470 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24471 doc: /* *The pointer shape to show in void text areas.
24472 A value of nil means to show the text pointer. Other options are `arrow',
24473 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24474 Vvoid_text_area_pointer = Qarrow;
24475
24476 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24477 doc: /* Non-nil means don't actually do any redisplay.
24478 This is used for internal purposes. */);
24479 Vinhibit_redisplay = Qnil;
24480
24481 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24482 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24483 Vglobal_mode_string = Qnil;
24484
24485 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24486 doc: /* Marker for where to display an arrow on top of the buffer text.
24487 This must be the beginning of a line in order to work.
24488 See also `overlay-arrow-string'. */);
24489 Voverlay_arrow_position = Qnil;
24490
24491 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24492 doc: /* String to display as an arrow in non-window frames.
24493 See also `overlay-arrow-position'. */);
24494 Voverlay_arrow_string = build_string ("=>");
24495
24496 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24497 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24498 The symbols on this list are examined during redisplay to determine
24499 where to display overlay arrows. */);
24500 Voverlay_arrow_variable_list
24501 = Fcons (intern ("overlay-arrow-position"), Qnil);
24502
24503 DEFVAR_INT ("scroll-step", &scroll_step,
24504 doc: /* *The number of lines to try scrolling a window by when point moves out.
24505 If that fails to bring point back on frame, point is centered instead.
24506 If this is zero, point is always centered after it moves off frame.
24507 If you want scrolling to always be a line at a time, you should set
24508 `scroll-conservatively' to a large value rather than set this to 1. */);
24509
24510 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24511 doc: /* *Scroll up to this many lines, to bring point back on screen.
24512 If point moves off-screen, redisplay will scroll by up to
24513 `scroll-conservatively' lines in order to bring point just barely
24514 onto the screen again. If that cannot be done, then redisplay
24515 recenters point as usual.
24516
24517 A value of zero means always recenter point if it moves off screen. */);
24518 scroll_conservatively = 0;
24519
24520 DEFVAR_INT ("scroll-margin", &scroll_margin,
24521 doc: /* *Number of lines of margin at the top and bottom of a window.
24522 Recenter the window whenever point gets within this many lines
24523 of the top or bottom of the window. */);
24524 scroll_margin = 0;
24525
24526 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24527 doc: /* Pixels per inch value for non-window system displays.
24528 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24529 Vdisplay_pixels_per_inch = make_float (72.0);
24530
24531 #if GLYPH_DEBUG
24532 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24533 #endif
24534
24535 DEFVAR_BOOL ("truncate-partial-width-windows",
24536 &truncate_partial_width_windows,
24537 doc: /* *Non-nil means truncate lines in all windows less than full frame wide.
24538 Nil means to respect the value of `truncate-lines'. */);
24539 truncate_partial_width_windows = 1;
24540
24541 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24542 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24543 Any other value means to use the appropriate face, `mode-line',
24544 `header-line', or `menu' respectively. */);
24545 mode_line_inverse_video = 1;
24546
24547 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24548 doc: /* *Maximum buffer size for which line number should be displayed.
24549 If the buffer is bigger than this, the line number does not appear
24550 in the mode line. A value of nil means no limit. */);
24551 Vline_number_display_limit = Qnil;
24552
24553 DEFVAR_INT ("line-number-display-limit-width",
24554 &line_number_display_limit_width,
24555 doc: /* *Maximum line width (in characters) for line number display.
24556 If the average length of the lines near point is bigger than this, then the
24557 line number may be omitted from the mode line. */);
24558 line_number_display_limit_width = 200;
24559
24560 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24561 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24562 highlight_nonselected_windows = 0;
24563
24564 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24565 doc: /* Non-nil if more than one frame is visible on this display.
24566 Minibuffer-only frames don't count, but iconified frames do.
24567 This variable is not guaranteed to be accurate except while processing
24568 `frame-title-format' and `icon-title-format'. */);
24569
24570 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24571 doc: /* Template for displaying the title bar of visible frames.
24572 \(Assuming the window manager supports this feature.)
24573
24574 This variable has the same structure as `mode-line-format', except that
24575 the %c and %l constructs are ignored. It is used only on frames for
24576 which no explicit name has been set \(see `modify-frame-parameters'). */);
24577
24578 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24579 doc: /* Template for displaying the title bar of an iconified frame.
24580 \(Assuming the window manager supports this feature.)
24581 This variable has the same structure as `mode-line-format' (which see),
24582 and is used only on frames for which no explicit name has been set
24583 \(see `modify-frame-parameters'). */);
24584 Vicon_title_format
24585 = Vframe_title_format
24586 = Fcons (intern ("multiple-frames"),
24587 Fcons (build_string ("%b"),
24588 Fcons (Fcons (empty_unibyte_string,
24589 Fcons (intern ("invocation-name"),
24590 Fcons (build_string ("@"),
24591 Fcons (intern ("system-name"),
24592 Qnil)))),
24593 Qnil)));
24594
24595 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24596 doc: /* Maximum number of lines to keep in the message log buffer.
24597 If nil, disable message logging. If t, log messages but don't truncate
24598 the buffer when it becomes large. */);
24599 Vmessage_log_max = make_number (100);
24600
24601 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24602 doc: /* Functions called before redisplay, if window sizes have changed.
24603 The value should be a list of functions that take one argument.
24604 Just before redisplay, for each frame, if any of its windows have changed
24605 size since the last redisplay, or have been split or deleted,
24606 all the functions in the list are called, with the frame as argument. */);
24607 Vwindow_size_change_functions = Qnil;
24608
24609 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24610 doc: /* List of functions to call before redisplaying a window with scrolling.
24611 Each function is called with two arguments, the window
24612 and its new display-start position. Note that the value of `window-end'
24613 is not valid when these functions are called. */);
24614 Vwindow_scroll_functions = Qnil;
24615
24616 DEFVAR_LISP ("window-text-change-functions",
24617 &Vwindow_text_change_functions,
24618 doc: /* Functions to call in redisplay when text in the window might change. */);
24619 Vwindow_text_change_functions = Qnil;
24620
24621 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24622 doc: /* Functions called when redisplay of a window reaches the end trigger.
24623 Each function is called with two arguments, the window and the end trigger value.
24624 See `set-window-redisplay-end-trigger'. */);
24625 Vredisplay_end_trigger_functions = Qnil;
24626
24627 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24628 doc: /* *Non-nil means autoselect window with mouse pointer.
24629 If nil, do not autoselect windows.
24630 A positive number means delay autoselection by that many seconds: a
24631 window is autoselected only after the mouse has remained in that
24632 window for the duration of the delay.
24633 A negative number has a similar effect, but causes windows to be
24634 autoselected only after the mouse has stopped moving. \(Because of
24635 the way Emacs compares mouse events, you will occasionally wait twice
24636 that time before the window gets selected.\)
24637 Any other value means to autoselect window instantaneously when the
24638 mouse pointer enters it.
24639
24640 Autoselection selects the minibuffer only if it is active, and never
24641 unselects the minibuffer if it is active.
24642
24643 When customizing this variable make sure that the actual value of
24644 `focus-follows-mouse' matches the behavior of your window manager. */);
24645 Vmouse_autoselect_window = Qnil;
24646
24647 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24648 doc: /* *Non-nil means automatically resize tool-bars.
24649 This dynamically changes the tool-bar's height to the minimum height
24650 that is needed to make all tool-bar items visible.
24651 If value is `grow-only', the tool-bar's height is only increased
24652 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24653 Vauto_resize_tool_bars = Qt;
24654
24655 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24656 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24657 auto_raise_tool_bar_buttons_p = 1;
24658
24659 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24660 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24661 make_cursor_line_fully_visible_p = 1;
24662
24663 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24664 doc: /* *Border below tool-bar in pixels.
24665 If an integer, use it as the height of the border.
24666 If it is one of `internal-border-width' or `border-width', use the
24667 value of the corresponding frame parameter.
24668 Otherwise, no border is added below the tool-bar. */);
24669 Vtool_bar_border = Qinternal_border_width;
24670
24671 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24672 doc: /* *Margin around tool-bar buttons in pixels.
24673 If an integer, use that for both horizontal and vertical margins.
24674 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24675 HORZ specifying the horizontal margin, and VERT specifying the
24676 vertical margin. */);
24677 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24678
24679 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24680 doc: /* *Relief thickness of tool-bar buttons. */);
24681 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24682
24683 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24684 doc: /* List of functions to call to fontify regions of text.
24685 Each function is called with one argument POS. Functions must
24686 fontify a region starting at POS in the current buffer, and give
24687 fontified regions the property `fontified'. */);
24688 Vfontification_functions = Qnil;
24689 Fmake_variable_buffer_local (Qfontification_functions);
24690
24691 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24692 &unibyte_display_via_language_environment,
24693 doc: /* *Non-nil means display unibyte text according to language environment.
24694 Specifically this means that unibyte non-ASCII characters
24695 are displayed by converting them to the equivalent multibyte characters
24696 according to the current language environment. As a result, they are
24697 displayed according to the current fontset. */);
24698 unibyte_display_via_language_environment = 0;
24699
24700 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24701 doc: /* *Maximum height for resizing mini-windows.
24702 If a float, it specifies a fraction of the mini-window frame's height.
24703 If an integer, it specifies a number of lines. */);
24704 Vmax_mini_window_height = make_float (0.25);
24705
24706 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24707 doc: /* *How to resize mini-windows.
24708 A value of nil means don't automatically resize mini-windows.
24709 A value of t means resize them to fit the text displayed in them.
24710 A value of `grow-only', the default, means let mini-windows grow
24711 only, until their display becomes empty, at which point the windows
24712 go back to their normal size. */);
24713 Vresize_mini_windows = Qgrow_only;
24714
24715 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24716 doc: /* Alist specifying how to blink the cursor off.
24717 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24718 `cursor-type' frame-parameter or variable equals ON-STATE,
24719 comparing using `equal', Emacs uses OFF-STATE to specify
24720 how to blink it off. ON-STATE and OFF-STATE are values for
24721 the `cursor-type' frame parameter.
24722
24723 If a frame's ON-STATE has no entry in this list,
24724 the frame's other specifications determine how to blink the cursor off. */);
24725 Vblink_cursor_alist = Qnil;
24726
24727 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24728 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24729 automatic_hscrolling_p = 1;
24730 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
24731 staticpro (&Qauto_hscroll_mode);
24732
24733 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24734 doc: /* *How many columns away from the window edge point is allowed to get
24735 before automatic hscrolling will horizontally scroll the window. */);
24736 hscroll_margin = 5;
24737
24738 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24739 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24740 When point is less than `hscroll-margin' columns from the window
24741 edge, automatic hscrolling will scroll the window by the amount of columns
24742 determined by this variable. If its value is a positive integer, scroll that
24743 many columns. If it's a positive floating-point number, it specifies the
24744 fraction of the window's width to scroll. If it's nil or zero, point will be
24745 centered horizontally after the scroll. Any other value, including negative
24746 numbers, are treated as if the value were zero.
24747
24748 Automatic hscrolling always moves point outside the scroll margin, so if
24749 point was more than scroll step columns inside the margin, the window will
24750 scroll more than the value given by the scroll step.
24751
24752 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24753 and `scroll-right' overrides this variable's effect. */);
24754 Vhscroll_step = make_number (0);
24755
24756 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24757 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24758 Bind this around calls to `message' to let it take effect. */);
24759 message_truncate_lines = 0;
24760
24761 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24762 doc: /* Normal hook run to update the menu bar definitions.
24763 Redisplay runs this hook before it redisplays the menu bar.
24764 This is used to update submenus such as Buffers,
24765 whose contents depend on various data. */);
24766 Vmenu_bar_update_hook = Qnil;
24767
24768 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24769 doc: /* Frame for which we are updating a menu.
24770 The enable predicate for a menu binding should check this variable. */);
24771 Vmenu_updating_frame = Qnil;
24772
24773 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24774 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24775 inhibit_menubar_update = 0;
24776
24777 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24778 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24779 inhibit_eval_during_redisplay = 0;
24780
24781 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24782 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24783 inhibit_free_realized_faces = 0;
24784
24785 #if GLYPH_DEBUG
24786 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24787 doc: /* Inhibit try_window_id display optimization. */);
24788 inhibit_try_window_id = 0;
24789
24790 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24791 doc: /* Inhibit try_window_reusing display optimization. */);
24792 inhibit_try_window_reusing = 0;
24793
24794 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24795 doc: /* Inhibit try_cursor_movement display optimization. */);
24796 inhibit_try_cursor_movement = 0;
24797 #endif /* GLYPH_DEBUG */
24798
24799 DEFVAR_INT ("overline-margin", &overline_margin,
24800 doc: /* *Space between overline and text, in pixels.
24801 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24802 margin to the caracter height. */);
24803 overline_margin = 2;
24804 }
24805
24806
24807 /* Initialize this module when Emacs starts. */
24808
24809 void
24810 init_xdisp ()
24811 {
24812 Lisp_Object root_window;
24813 struct window *mini_w;
24814
24815 current_header_line_height = current_mode_line_height = -1;
24816
24817 CHARPOS (this_line_start_pos) = 0;
24818
24819 mini_w = XWINDOW (minibuf_window);
24820 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24821
24822 if (!noninteractive)
24823 {
24824 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24825 int i;
24826
24827 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24828 set_window_height (root_window,
24829 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24830 0);
24831 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24832 set_window_height (minibuf_window, 1, 0);
24833
24834 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24835 mini_w->total_cols = make_number (FRAME_COLS (f));
24836
24837 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24838 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24839 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24840
24841 /* The default ellipsis glyphs `...'. */
24842 for (i = 0; i < 3; ++i)
24843 default_invis_vector[i] = make_number ('.');
24844 }
24845
24846 {
24847 /* Allocate the buffer for frame titles.
24848 Also used for `format-mode-line'. */
24849 int size = 100;
24850 mode_line_noprop_buf = (char *) xmalloc (size);
24851 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24852 mode_line_noprop_ptr = mode_line_noprop_buf;
24853 mode_line_target = MODE_LINE_DISPLAY;
24854 }
24855
24856 help_echo_showing_p = 0;
24857 }
24858
24859
24860 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24861 (do not change this comment) */